[MIPS] Add Loongson 3A1000 proccessor support.
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
CommitLineData
252b5132 1/* tc-mips.c -- assemble code for a MIPS chip.
219d1afa 2 Copyright (C) 1993-2018 Free Software Foundation, Inc.
252b5132
RH
3 Contributed by the OSF and Ralph Campbell.
4 Written by Keith Knowles and Ralph Campbell, working independently.
5 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
6 Support.
7
8 This file is part of GAS.
9
10 GAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
ec2655a6 12 the Free Software Foundation; either version 3, or (at your option)
252b5132
RH
13 any later version.
14
15 GAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GAS; see the file COPYING. If not, write to the Free
4b4da160
NC
22 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
23 02110-1301, USA. */
252b5132
RH
24
25#include "as.h"
26#include "config.h"
27#include "subsegs.h"
3882b010 28#include "safe-ctype.h"
252b5132 29
252b5132
RH
30#include "opcode/mips.h"
31#include "itbl-ops.h"
c5dd6aab 32#include "dwarf2dbg.h"
5862107c 33#include "dw2gencfi.h"
252b5132 34
42429eac
RS
35/* Check assumptions made in this file. */
36typedef char static_assert1[sizeof (offsetT) < 8 ? -1 : 1];
37typedef char static_assert2[sizeof (valueT) < 8 ? -1 : 1];
38
252b5132
RH
39#ifdef DEBUG
40#define DBG(x) printf x
41#else
42#define DBG(x)
43#endif
44
263b2574 45#define streq(a, b) (strcmp (a, b) == 0)
46
9e12b7a2
RS
47#define SKIP_SPACE_TABS(S) \
48 do { while (*(S) == ' ' || *(S) == '\t') ++(S); } while (0)
49
252b5132 50/* Clean up namespace so we can include obj-elf.h too. */
17a2f251
TS
51static int mips_output_flavor (void);
52static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
252b5132
RH
53#undef OBJ_PROCESS_STAB
54#undef OUTPUT_FLAVOR
55#undef S_GET_ALIGN
56#undef S_GET_SIZE
57#undef S_SET_ALIGN
58#undef S_SET_SIZE
252b5132
RH
59#undef obj_frob_file
60#undef obj_frob_file_after_relocs
61#undef obj_frob_symbol
62#undef obj_pop_insert
63#undef obj_sec_sym_ok_for_reloc
64#undef OBJ_COPY_SYMBOL_ATTRIBUTES
65
66#include "obj-elf.h"
67/* Fix any of them that we actually care about. */
68#undef OUTPUT_FLAVOR
69#define OUTPUT_FLAVOR mips_output_flavor()
252b5132 70
252b5132 71#include "elf/mips.h"
252b5132
RH
72
73#ifndef ECOFF_DEBUGGING
74#define NO_ECOFF_DEBUGGING
75#define ECOFF_DEBUGGING 0
76#endif
77
ecb4347a
DJ
78int mips_flag_mdebug = -1;
79
dcd410fe
RO
80/* Control generation of .pdr sections. Off by default on IRIX: the native
81 linker doesn't know about and discards them, but relocations against them
82 remain, leading to rld crashes. */
83#ifdef TE_IRIX
84int mips_flag_pdr = FALSE;
85#else
86int mips_flag_pdr = TRUE;
87#endif
88
252b5132
RH
89#include "ecoff.h"
90
252b5132 91static char *mips_regmask_frag;
351cdf24 92static char *mips_flags_frag;
252b5132 93
85b51719 94#define ZERO 0
741fe287 95#define ATREG 1
df58fc94
RS
96#define S0 16
97#define S7 23
252b5132
RH
98#define TREG 24
99#define PIC_CALL_REG 25
100#define KT0 26
101#define KT1 27
102#define GP 28
103#define SP 29
104#define FP 30
105#define RA 31
106
107#define ILLEGAL_REG (32)
108
741fe287
MR
109#define AT mips_opts.at
110
252b5132
RH
111extern int target_big_endian;
112
252b5132 113/* The name of the readonly data section. */
e8044f35 114#define RDATA_SECTION_NAME ".rodata"
252b5132 115
a4e06468
RS
116/* Ways in which an instruction can be "appended" to the output. */
117enum append_method {
118 /* Just add it normally. */
119 APPEND_ADD,
120
121 /* Add it normally and then add a nop. */
122 APPEND_ADD_WITH_NOP,
123
124 /* Turn an instruction with a delay slot into a "compact" version. */
125 APPEND_ADD_COMPACT,
126
127 /* Insert the instruction before the last one. */
128 APPEND_SWAP
129};
130
47e39b9d
RS
131/* Information about an instruction, including its format, operands
132 and fixups. */
133struct mips_cl_insn
134{
135 /* The opcode's entry in mips_opcodes or mips16_opcodes. */
136 const struct mips_opcode *insn_mo;
137
47e39b9d 138 /* The 16-bit or 32-bit bitstring of the instruction itself. This is
5c04167a
RS
139 a copy of INSN_MO->match with the operands filled in. If we have
140 decided to use an extended MIPS16 instruction, this includes the
141 extension. */
47e39b9d
RS
142 unsigned long insn_opcode;
143
144 /* The frag that contains the instruction. */
145 struct frag *frag;
146
147 /* The offset into FRAG of the first instruction byte. */
148 long where;
149
150 /* The relocs associated with the instruction, if any. */
151 fixS *fixp[3];
152
a38419a5
RS
153 /* True if this entry cannot be moved from its current position. */
154 unsigned int fixed_p : 1;
47e39b9d 155
708587a4 156 /* True if this instruction occurred in a .set noreorder block. */
47e39b9d
RS
157 unsigned int noreorder_p : 1;
158
2fa15973
RS
159 /* True for mips16 instructions that jump to an absolute address. */
160 unsigned int mips16_absolute_jump_p : 1;
15be625d
CM
161
162 /* True if this instruction is complete. */
163 unsigned int complete_p : 1;
e407c74b
NC
164
165 /* True if this instruction is cleared from history by unconditional
166 branch. */
167 unsigned int cleared_p : 1;
47e39b9d
RS
168};
169
a325df1d
TS
170/* The ABI to use. */
171enum mips_abi_level
172{
173 NO_ABI = 0,
174 O32_ABI,
175 O64_ABI,
176 N32_ABI,
177 N64_ABI,
178 EABI_ABI
179};
180
181/* MIPS ABI we are using for this output file. */
316f5878 182static enum mips_abi_level mips_abi = NO_ABI;
a325df1d 183
143d77c5
EC
184/* Whether or not we have code that can call pic code. */
185int mips_abicalls = FALSE;
186
aa6975fb
ILT
187/* Whether or not we have code which can be put into a shared
188 library. */
189static bfd_boolean mips_in_shared = TRUE;
190
252b5132
RH
191/* This is the set of options which may be modified by the .set
192 pseudo-op. We use a struct so that .set push and .set pop are more
193 reliable. */
194
e972090a
NC
195struct mips_set_options
196{
252b5132
RH
197 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
198 if it has not been initialized. Changed by `.set mipsN', and the
199 -mipsN command line option, and the default CPU. */
200 int isa;
846ef2d0
RS
201 /* Enabled Application Specific Extensions (ASEs). Changed by `.set
202 <asename>', by command line options, and based on the default
203 architecture. */
204 int ase;
252b5132
RH
205 /* Whether we are assembling for the mips16 processor. 0 if we are
206 not, 1 if we are, and -1 if the value has not been initialized.
207 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
208 -nomips16 command line options, and the default CPU. */
209 int mips16;
df58fc94
RS
210 /* Whether we are assembling for the mipsMIPS ASE. 0 if we are not,
211 1 if we are, and -1 if the value has not been initialized. Changed
212 by `.set micromips' and `.set nomicromips', and the -mmicromips
213 and -mno-micromips command line options, and the default CPU. */
214 int micromips;
252b5132
RH
215 /* Non-zero if we should not reorder instructions. Changed by `.set
216 reorder' and `.set noreorder'. */
217 int noreorder;
741fe287
MR
218 /* Non-zero if we should not permit the register designated "assembler
219 temporary" to be used in instructions. The value is the register
220 number, normally $at ($1). Changed by `.set at=REG', `.set noat'
221 (same as `.set at=$0') and `.set at' (same as `.set at=$1'). */
222 unsigned int at;
252b5132
RH
223 /* Non-zero if we should warn when a macro instruction expands into
224 more than one machine instruction. Changed by `.set nomacro' and
225 `.set macro'. */
226 int warn_about_macros;
227 /* Non-zero if we should not move instructions. Changed by `.set
228 move', `.set volatile', `.set nomove', and `.set novolatile'. */
229 int nomove;
230 /* Non-zero if we should not optimize branches by moving the target
231 of the branch into the delay slot. Actually, we don't perform
232 this optimization anyhow. Changed by `.set bopt' and `.set
233 nobopt'. */
234 int nobopt;
235 /* Non-zero if we should not autoextend mips16 instructions.
236 Changed by `.set autoextend' and `.set noautoextend'. */
237 int noautoextend;
833794fc
MR
238 /* True if we should only emit 32-bit microMIPS instructions.
239 Changed by `.set insn32' and `.set noinsn32', and the -minsn32
240 and -mno-insn32 command line options. */
241 bfd_boolean insn32;
a325df1d
TS
242 /* Restrict general purpose registers and floating point registers
243 to 32 bit. This is initially determined when -mgp32 or -mfp32
244 is passed but can changed if the assembler code uses .set mipsN. */
bad1aba3 245 int gp;
0b35dfee 246 int fp;
fef14a42
TS
247 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
248 command line option, and the default CPU. */
249 int arch;
aed1a261
RS
250 /* True if ".set sym32" is in effect. */
251 bfd_boolean sym32;
037b32b9
AN
252 /* True if floating-point operations are not allowed. Changed by .set
253 softfloat or .set hardfloat, by command line options -msoft-float or
254 -mhard-float. The default is false. */
255 bfd_boolean soft_float;
256
257 /* True if only single-precision floating-point operations are allowed.
258 Changed by .set singlefloat or .set doublefloat, command-line options
259 -msingle-float or -mdouble-float. The default is false. */
260 bfd_boolean single_float;
351cdf24
MF
261
262 /* 1 if single-precision operations on odd-numbered registers are
263 allowed. */
264 int oddspreg;
252b5132
RH
265};
266
919731af 267/* Specifies whether module level options have been checked yet. */
268static bfd_boolean file_mips_opts_checked = FALSE;
269
7361da2c
AB
270/* Do we support nan2008? 0 if we don't, 1 if we do, and -1 if the
271 value has not been initialized. Changed by `.nan legacy' and
272 `.nan 2008', and the -mnan=legacy and -mnan=2008 command line
273 options, and the default CPU. */
274static int mips_nan2008 = -1;
a325df1d 275
0b35dfee 276/* This is the struct we use to hold the module level set of options.
bad1aba3 277 Note that we must set the isa field to ISA_UNKNOWN and the ASE, gp and
0b35dfee 278 fp fields to -1 to indicate that they have not been initialized. */
037b32b9 279
0b35dfee 280static struct mips_set_options file_mips_opts =
281{
282 /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
283 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
284 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
bad1aba3 285 /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
351cdf24 286 /* soft_float */ FALSE, /* single_float */ FALSE, /* oddspreg */ -1
0b35dfee 287};
252b5132 288
0b35dfee 289/* This is similar to file_mips_opts, but for the current set of options. */
ba92f887 290
e972090a
NC
291static struct mips_set_options mips_opts =
292{
846ef2d0 293 /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
b015e599 294 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
833794fc 295 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
bad1aba3 296 /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
351cdf24 297 /* soft_float */ FALSE, /* single_float */ FALSE, /* oddspreg */ -1
e7af610e 298};
252b5132 299
846ef2d0
RS
300/* Which bits of file_ase were explicitly set or cleared by ASE options. */
301static unsigned int file_ase_explicit;
302
252b5132
RH
303/* These variables are filled in with the masks of registers used.
304 The object format code reads them and puts them in the appropriate
305 place. */
306unsigned long mips_gprmask;
307unsigned long mips_cprmask[4];
308
738f4d98 309/* True if any MIPS16 code was produced. */
a4672219
TS
310static int file_ase_mips16;
311
3994f87e
TS
312#define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32 \
313 || mips_opts.isa == ISA_MIPS32R2 \
ae52f483
AB
314 || mips_opts.isa == ISA_MIPS32R3 \
315 || mips_opts.isa == ISA_MIPS32R5 \
3994f87e 316 || mips_opts.isa == ISA_MIPS64 \
ae52f483
AB
317 || mips_opts.isa == ISA_MIPS64R2 \
318 || mips_opts.isa == ISA_MIPS64R3 \
319 || mips_opts.isa == ISA_MIPS64R5)
3994f87e 320
df58fc94
RS
321/* True if any microMIPS code was produced. */
322static int file_ase_micromips;
323
b12dd2e4
CF
324/* True if we want to create R_MIPS_JALR for jalr $25. */
325#ifdef TE_IRIX
1180b5a4 326#define MIPS_JALR_HINT_P(EXPR) HAVE_NEWABI
b12dd2e4 327#else
1180b5a4
RS
328/* As a GNU extension, we use R_MIPS_JALR for o32 too. However,
329 because there's no place for any addend, the only acceptable
330 expression is a bare symbol. */
331#define MIPS_JALR_HINT_P(EXPR) \
332 (!HAVE_IN_PLACE_ADDENDS \
333 || ((EXPR)->X_op == O_symbol && (EXPR)->X_add_number == 0))
b12dd2e4
CF
334#endif
335
ec68c924 336/* The argument of the -march= flag. The architecture we are assembling. */
316f5878 337static const char *mips_arch_string;
ec68c924
EC
338
339/* The argument of the -mtune= flag. The architecture for which we
340 are optimizing. */
341static int mips_tune = CPU_UNKNOWN;
316f5878 342static const char *mips_tune_string;
ec68c924 343
316f5878 344/* True when generating 32-bit code for a 64-bit processor. */
252b5132
RH
345static int mips_32bitmode = 0;
346
316f5878
RS
347/* True if the given ABI requires 32-bit registers. */
348#define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
349
350/* Likewise 64-bit registers. */
707bfff6 351#define ABI_NEEDS_64BIT_REGS(ABI) \
134c0c8b 352 ((ABI) == N32_ABI \
707bfff6 353 || (ABI) == N64_ABI \
316f5878
RS
354 || (ABI) == O64_ABI)
355
7361da2c
AB
356#define ISA_IS_R6(ISA) \
357 ((ISA) == ISA_MIPS32R6 \
358 || (ISA) == ISA_MIPS64R6)
359
ad3fea08 360/* Return true if ISA supports 64 bit wide gp registers. */
707bfff6
TS
361#define ISA_HAS_64BIT_REGS(ISA) \
362 ((ISA) == ISA_MIPS3 \
363 || (ISA) == ISA_MIPS4 \
364 || (ISA) == ISA_MIPS5 \
365 || (ISA) == ISA_MIPS64 \
ae52f483
AB
366 || (ISA) == ISA_MIPS64R2 \
367 || (ISA) == ISA_MIPS64R3 \
7361da2c
AB
368 || (ISA) == ISA_MIPS64R5 \
369 || (ISA) == ISA_MIPS64R6)
9ce8a5dd 370
ad3fea08
TS
371/* Return true if ISA supports 64 bit wide float registers. */
372#define ISA_HAS_64BIT_FPRS(ISA) \
373 ((ISA) == ISA_MIPS3 \
374 || (ISA) == ISA_MIPS4 \
375 || (ISA) == ISA_MIPS5 \
376 || (ISA) == ISA_MIPS32R2 \
ae52f483
AB
377 || (ISA) == ISA_MIPS32R3 \
378 || (ISA) == ISA_MIPS32R5 \
7361da2c 379 || (ISA) == ISA_MIPS32R6 \
ad3fea08 380 || (ISA) == ISA_MIPS64 \
ae52f483
AB
381 || (ISA) == ISA_MIPS64R2 \
382 || (ISA) == ISA_MIPS64R3 \
7361da2c
AB
383 || (ISA) == ISA_MIPS64R5 \
384 || (ISA) == ISA_MIPS64R6)
ad3fea08 385
af7ee8bf
CD
386/* Return true if ISA supports 64-bit right rotate (dror et al.)
387 instructions. */
707bfff6 388#define ISA_HAS_DROR(ISA) \
df58fc94 389 ((ISA) == ISA_MIPS64R2 \
ae52f483
AB
390 || (ISA) == ISA_MIPS64R3 \
391 || (ISA) == ISA_MIPS64R5 \
7361da2c 392 || (ISA) == ISA_MIPS64R6 \
df58fc94
RS
393 || (mips_opts.micromips \
394 && ISA_HAS_64BIT_REGS (ISA)) \
395 )
af7ee8bf
CD
396
397/* Return true if ISA supports 32-bit right rotate (ror et al.)
398 instructions. */
707bfff6
TS
399#define ISA_HAS_ROR(ISA) \
400 ((ISA) == ISA_MIPS32R2 \
ae52f483
AB
401 || (ISA) == ISA_MIPS32R3 \
402 || (ISA) == ISA_MIPS32R5 \
7361da2c 403 || (ISA) == ISA_MIPS32R6 \
707bfff6 404 || (ISA) == ISA_MIPS64R2 \
ae52f483
AB
405 || (ISA) == ISA_MIPS64R3 \
406 || (ISA) == ISA_MIPS64R5 \
7361da2c 407 || (ISA) == ISA_MIPS64R6 \
846ef2d0 408 || (mips_opts.ase & ASE_SMARTMIPS) \
df58fc94
RS
409 || mips_opts.micromips \
410 )
707bfff6 411
7455baf8 412/* Return true if ISA supports single-precision floats in odd registers. */
351cdf24
MF
413#define ISA_HAS_ODD_SINGLE_FPR(ISA, CPU)\
414 (((ISA) == ISA_MIPS32 \
415 || (ISA) == ISA_MIPS32R2 \
416 || (ISA) == ISA_MIPS32R3 \
417 || (ISA) == ISA_MIPS32R5 \
7361da2c 418 || (ISA) == ISA_MIPS32R6 \
351cdf24
MF
419 || (ISA) == ISA_MIPS64 \
420 || (ISA) == ISA_MIPS64R2 \
421 || (ISA) == ISA_MIPS64R3 \
422 || (ISA) == ISA_MIPS64R5 \
7361da2c 423 || (ISA) == ISA_MIPS64R6 \
351cdf24 424 || (CPU) == CPU_R5900) \
ac8cb70f 425 && (CPU) != CPU_GS464)
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,
8095d2f7
CX
1532 OPTION_LOONGSON_MMI,
1533 OPTION_NO_LOONGSON_MMI,
716c08de
CX
1534 OPTION_LOONGSON_CAM,
1535 OPTION_NO_LOONGSON_CAM,
bdc6c06e
CX
1536 OPTION_LOONGSON_EXT,
1537 OPTION_NO_LOONGSON_EXT,
a693765e
CX
1538 OPTION_LOONGSON_EXT2,
1539 OPTION_NO_LOONGSON_EXT2,
c31f3936
RS
1540 OPTION_END_OF_ENUM
1541 };
1542
1543struct option md_longopts[] =
1544{
1545 /* Options which specify architecture. */
1546 {"march", required_argument, NULL, OPTION_MARCH},
1547 {"mtune", required_argument, NULL, OPTION_MTUNE},
1548 {"mips0", no_argument, NULL, OPTION_MIPS1},
1549 {"mips1", no_argument, NULL, OPTION_MIPS1},
1550 {"mips2", no_argument, NULL, OPTION_MIPS2},
1551 {"mips3", no_argument, NULL, OPTION_MIPS3},
1552 {"mips4", no_argument, NULL, OPTION_MIPS4},
1553 {"mips5", no_argument, NULL, OPTION_MIPS5},
1554 {"mips32", no_argument, NULL, OPTION_MIPS32},
1555 {"mips64", no_argument, NULL, OPTION_MIPS64},
1556 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
ae52f483
AB
1557 {"mips32r3", no_argument, NULL, OPTION_MIPS32R3},
1558 {"mips32r5", no_argument, NULL, OPTION_MIPS32R5},
7361da2c 1559 {"mips32r6", no_argument, NULL, OPTION_MIPS32R6},
c31f3936 1560 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
ae52f483
AB
1561 {"mips64r3", no_argument, NULL, OPTION_MIPS64R3},
1562 {"mips64r5", no_argument, NULL, OPTION_MIPS64R5},
7361da2c 1563 {"mips64r6", no_argument, NULL, OPTION_MIPS64R6},
c31f3936
RS
1564
1565 /* Options which specify Application Specific Extensions (ASEs). */
1566 {"mips16", no_argument, NULL, OPTION_MIPS16},
1567 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1568 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1569 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1570 {"mdmx", no_argument, NULL, OPTION_MDMX},
1571 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1572 {"mdsp", no_argument, NULL, OPTION_DSP},
1573 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1574 {"mmt", no_argument, NULL, OPTION_MT},
1575 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1576 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1577 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1578 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1579 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
8f4f9071
MF
1580 {"mdspr3", no_argument, NULL, OPTION_DSPR3},
1581 {"mno-dspr3", no_argument, NULL, OPTION_NO_DSPR3},
c31f3936
RS
1582 {"meva", no_argument, NULL, OPTION_EVA},
1583 {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1584 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1585 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1586 {"mmcu", no_argument, NULL, OPTION_MCU},
1587 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1588 {"mvirt", no_argument, NULL, OPTION_VIRT},
1589 {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
56d438b1
CF
1590 {"mmsa", no_argument, NULL, OPTION_MSA},
1591 {"mno-msa", no_argument, NULL, OPTION_NO_MSA},
7d64c587
AB
1592 {"mxpa", no_argument, NULL, OPTION_XPA},
1593 {"mno-xpa", no_argument, NULL, OPTION_NO_XPA},
25499ac7
MR
1594 {"mmips16e2", no_argument, NULL, OPTION_MIPS16E2},
1595 {"mno-mips16e2", no_argument, NULL, OPTION_NO_MIPS16E2},
730c3174
SE
1596 {"mcrc", no_argument, NULL, OPTION_CRC},
1597 {"mno-crc", no_argument, NULL, OPTION_NO_CRC},
6f20c942
FS
1598 {"mginv", no_argument, NULL, OPTION_GINV},
1599 {"mno-ginv", no_argument, NULL, OPTION_NO_GINV},
8095d2f7
CX
1600 {"mloongson-mmi", no_argument, NULL, OPTION_LOONGSON_MMI},
1601 {"mno-loongson-mmi", no_argument, NULL, OPTION_NO_LOONGSON_MMI},
716c08de
CX
1602 {"mloongson-cam", no_argument, NULL, OPTION_LOONGSON_CAM},
1603 {"mno-loongson-cam", no_argument, NULL, OPTION_NO_LOONGSON_CAM},
bdc6c06e
CX
1604 {"mloongson-ext", no_argument, NULL, OPTION_LOONGSON_EXT},
1605 {"mno-loongson-ext", no_argument, NULL, OPTION_NO_LOONGSON_EXT},
a693765e
CX
1606 {"mloongson-ext2", no_argument, NULL, OPTION_LOONGSON_EXT2},
1607 {"mno-loongson-ext2", no_argument, NULL, OPTION_NO_LOONGSON_EXT2},
c31f3936
RS
1608
1609 /* Old-style architecture options. Don't add more of these. */
1610 {"m4650", no_argument, NULL, OPTION_M4650},
1611 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1612 {"m4010", no_argument, NULL, OPTION_M4010},
1613 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1614 {"m4100", no_argument, NULL, OPTION_M4100},
1615 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1616 {"m3900", no_argument, NULL, OPTION_M3900},
1617 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1618
1619 /* Options which enable bug fixes. */
1620 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1621 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1622 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1623 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1624 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1625 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1626 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1627 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
1628 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1629 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
1630 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1631 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
1632 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
a8d14a88
CM
1633 {"mfix-rm7000", no_argument, NULL, OPTION_FIX_RM7000},
1634 {"mno-fix-rm7000", no_argument, NULL, OPTION_NO_FIX_RM7000},
c31f3936
RS
1635 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1636 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
1637
1638 /* Miscellaneous options. */
1639 {"trap", no_argument, NULL, OPTION_TRAP},
1640 {"no-break", no_argument, NULL, OPTION_TRAP},
1641 {"break", no_argument, NULL, OPTION_BREAK},
1642 {"no-trap", no_argument, NULL, OPTION_BREAK},
1643 {"EB", no_argument, NULL, OPTION_EB},
1644 {"EL", no_argument, NULL, OPTION_EL},
1645 {"mfp32", no_argument, NULL, OPTION_FP32},
1646 {"mgp32", no_argument, NULL, OPTION_GP32},
1647 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1648 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1649 {"mfp64", no_argument, NULL, OPTION_FP64},
351cdf24 1650 {"mfpxx", no_argument, NULL, OPTION_FPXX},
c31f3936
RS
1651 {"mgp64", no_argument, NULL, OPTION_GP64},
1652 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1653 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
8b10b0b3
MR
1654 {"mignore-branch-isa", no_argument, NULL, OPTION_IGNORE_BRANCH_ISA},
1655 {"mno-ignore-branch-isa", no_argument, NULL, OPTION_NO_IGNORE_BRANCH_ISA},
833794fc
MR
1656 {"minsn32", no_argument, NULL, OPTION_INSN32},
1657 {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
c31f3936
RS
1658 {"mshared", no_argument, NULL, OPTION_MSHARED},
1659 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1660 {"msym32", no_argument, NULL, OPTION_MSYM32},
1661 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1662 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1663 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1664 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1665 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
351cdf24
MF
1666 {"modd-spreg", no_argument, NULL, OPTION_ODD_SPREG},
1667 {"mno-odd-spreg", no_argument, NULL, OPTION_NO_ODD_SPREG},
c31f3936
RS
1668
1669 /* Strictly speaking this next option is ELF specific,
1670 but we allow it for other ports as well in order to
1671 make testing easier. */
1672 {"32", no_argument, NULL, OPTION_32},
1673
1674 /* ELF-specific options. */
c31f3936
RS
1675 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1676 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1677 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1678 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
1679 {"xgot", no_argument, NULL, OPTION_XGOT},
1680 {"mabi", required_argument, NULL, OPTION_MABI},
1681 {"n32", no_argument, NULL, OPTION_N32},
1682 {"64", no_argument, NULL, OPTION_64},
1683 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1684 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1685 {"mpdr", no_argument, NULL, OPTION_PDR},
1686 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1687 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
ba92f887 1688 {"mnan", required_argument, NULL, OPTION_NAN},
c31f3936
RS
1689
1690 {NULL, no_argument, NULL, 0}
1691};
1692size_t md_longopts_size = sizeof (md_longopts);
1693\f
c6278170
RS
1694/* Information about either an Application Specific Extension or an
1695 optional architecture feature that, for simplicity, we treat in the
1696 same way as an ASE. */
1697struct mips_ase
1698{
1699 /* The name of the ASE, used in both the command-line and .set options. */
1700 const char *name;
1701
1702 /* The associated ASE_* flags. If the ASE is available on both 32-bit
1703 and 64-bit architectures, the flags here refer to the subset that
1704 is available on both. */
1705 unsigned int flags;
1706
1707 /* The ASE_* flag used for instructions that are available on 64-bit
1708 architectures but that are not included in FLAGS. */
1709 unsigned int flags64;
1710
1711 /* The command-line options that turn the ASE on and off. */
1712 int option_on;
1713 int option_off;
1714
1715 /* The minimum required architecture revisions for MIPS32, MIPS64,
1716 microMIPS32 and microMIPS64, or -1 if the extension isn't supported. */
1717 int mips32_rev;
1718 int mips64_rev;
1719 int micromips32_rev;
1720 int micromips64_rev;
7361da2c
AB
1721
1722 /* The architecture where the ASE was removed or -1 if the extension has not
1723 been removed. */
1724 int rem_rev;
c6278170
RS
1725};
1726
1727/* A table of all supported ASEs. */
1728static const struct mips_ase mips_ases[] = {
1729 { "dsp", ASE_DSP, ASE_DSP64,
1730 OPTION_DSP, OPTION_NO_DSP,
7361da2c
AB
1731 2, 2, 2, 2,
1732 -1 },
c6278170
RS
1733
1734 { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1735 OPTION_DSPR2, OPTION_NO_DSPR2,
7361da2c
AB
1736 2, 2, 2, 2,
1737 -1 },
c6278170 1738
8f4f9071
MF
1739 { "dspr3", ASE_DSP | ASE_DSPR2 | ASE_DSPR3, 0,
1740 OPTION_DSPR3, OPTION_NO_DSPR3,
1741 6, 6, -1, -1,
1742 -1 },
1743
c6278170
RS
1744 { "eva", ASE_EVA, 0,
1745 OPTION_EVA, OPTION_NO_EVA,
7361da2c
AB
1746 2, 2, 2, 2,
1747 -1 },
c6278170
RS
1748
1749 { "mcu", ASE_MCU, 0,
1750 OPTION_MCU, OPTION_NO_MCU,
7361da2c
AB
1751 2, 2, 2, 2,
1752 -1 },
c6278170
RS
1753
1754 /* Deprecated in MIPS64r5, but we don't implement that yet. */
1755 { "mdmx", ASE_MDMX, 0,
1756 OPTION_MDMX, OPTION_NO_MDMX,
7361da2c
AB
1757 -1, 1, -1, -1,
1758 6 },
c6278170
RS
1759
1760 /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2. */
1761 { "mips3d", ASE_MIPS3D, 0,
1762 OPTION_MIPS3D, OPTION_NO_MIPS3D,
7361da2c
AB
1763 2, 1, -1, -1,
1764 6 },
c6278170
RS
1765
1766 { "mt", ASE_MT, 0,
1767 OPTION_MT, OPTION_NO_MT,
7361da2c
AB
1768 2, 2, -1, -1,
1769 -1 },
c6278170
RS
1770
1771 { "smartmips", ASE_SMARTMIPS, 0,
1772 OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
7361da2c
AB
1773 1, -1, -1, -1,
1774 6 },
c6278170
RS
1775
1776 { "virt", ASE_VIRT, ASE_VIRT64,
1777 OPTION_VIRT, OPTION_NO_VIRT,
7361da2c
AB
1778 2, 2, 2, 2,
1779 -1 },
56d438b1
CF
1780
1781 { "msa", ASE_MSA, ASE_MSA64,
1782 OPTION_MSA, OPTION_NO_MSA,
7361da2c
AB
1783 2, 2, 2, 2,
1784 -1 },
7d64c587
AB
1785
1786 { "xpa", ASE_XPA, 0,
1787 OPTION_XPA, OPTION_NO_XPA,
909b4e3d 1788 2, 2, 2, 2,
7361da2c 1789 -1 },
25499ac7
MR
1790
1791 { "mips16e2", ASE_MIPS16E2, 0,
1792 OPTION_MIPS16E2, OPTION_NO_MIPS16E2,
1793 2, 2, -1, -1,
1794 6 },
730c3174
SE
1795
1796 { "crc", ASE_CRC, ASE_CRC64,
1797 OPTION_CRC, OPTION_NO_CRC,
1798 6, 6, -1, -1,
1799 -1 },
6f20c942
FS
1800
1801 { "ginv", ASE_GINV, 0,
1802 OPTION_GINV, OPTION_NO_GINV,
1803 6, 6, 6, 6,
1804 -1 },
8095d2f7
CX
1805
1806 { "loongson-mmi", ASE_LOONGSON_MMI, 0,
1807 OPTION_LOONGSON_MMI, OPTION_NO_LOONGSON_MMI,
1808 0, 0, -1, -1,
1809 -1 },
716c08de
CX
1810
1811 { "loongson-cam", ASE_LOONGSON_CAM, 0,
1812 OPTION_LOONGSON_CAM, OPTION_NO_LOONGSON_CAM,
1813 0, 0, -1, -1,
1814 -1 },
bdc6c06e
CX
1815
1816 { "loongson-ext", ASE_LOONGSON_EXT, 0,
1817 OPTION_LOONGSON_EXT, OPTION_NO_LOONGSON_EXT,
1818 0, 0, -1, -1,
1819 -1 },
a693765e
CX
1820
1821 { "loongson-ext2", ASE_LOONGSON_EXT | ASE_LOONGSON_EXT2, 0,
1822 OPTION_LOONGSON_EXT2, OPTION_NO_LOONGSON_EXT2,
1823 0, 0, -1, -1,
1824 -1 },
c6278170
RS
1825};
1826
1827/* The set of ASEs that require -mfp64. */
82bda27b 1828#define FP64_ASES (ASE_MIPS3D | ASE_MDMX | ASE_MSA)
c6278170
RS
1829
1830/* Groups of ASE_* flags that represent different revisions of an ASE. */
1831static const unsigned int mips_ase_groups[] = {
a693765e
CX
1832 ASE_DSP | ASE_DSPR2 | ASE_DSPR3,
1833 ASE_LOONGSON_EXT | ASE_LOONGSON_EXT2
c6278170
RS
1834};
1835\f
252b5132
RH
1836/* Pseudo-op table.
1837
1838 The following pseudo-ops from the Kane and Heinrich MIPS book
1839 should be defined here, but are currently unsupported: .alias,
1840 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1841
1842 The following pseudo-ops from the Kane and Heinrich MIPS book are
1843 specific to the type of debugging information being generated, and
1844 should be defined by the object format: .aent, .begin, .bend,
1845 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1846 .vreg.
1847
1848 The following pseudo-ops from the Kane and Heinrich MIPS book are
1849 not MIPS CPU specific, but are also not specific to the object file
1850 format. This file is probably the best place to define them, but
d84bcf09 1851 they are not currently supported: .asm0, .endr, .lab, .struct. */
252b5132 1852
e972090a
NC
1853static const pseudo_typeS mips_pseudo_table[] =
1854{
beae10d5 1855 /* MIPS specific pseudo-ops. */
252b5132
RH
1856 {"option", s_option, 0},
1857 {"set", s_mipsset, 0},
1858 {"rdata", s_change_sec, 'r'},
1859 {"sdata", s_change_sec, 's'},
1860 {"livereg", s_ignore, 0},
1861 {"abicalls", s_abicalls, 0},
1862 {"cpload", s_cpload, 0},
6478892d
TS
1863 {"cpsetup", s_cpsetup, 0},
1864 {"cplocal", s_cplocal, 0},
252b5132 1865 {"cprestore", s_cprestore, 0},
6478892d 1866 {"cpreturn", s_cpreturn, 0},
741d6ea8
JM
1867 {"dtprelword", s_dtprelword, 0},
1868 {"dtpreldword", s_dtpreldword, 0},
d0f13682
CLT
1869 {"tprelword", s_tprelword, 0},
1870 {"tpreldword", s_tpreldword, 0},
6478892d 1871 {"gpvalue", s_gpvalue, 0},
252b5132 1872 {"gpword", s_gpword, 0},
10181a0d 1873 {"gpdword", s_gpdword, 0},
a3f278e2 1874 {"ehword", s_ehword, 0},
252b5132
RH
1875 {"cpadd", s_cpadd, 0},
1876 {"insn", s_insn, 0},
ba92f887 1877 {"nan", s_nan, 0},
919731af 1878 {"module", s_module, 0},
252b5132 1879
beae10d5 1880 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132 1881 chips. */
38a57ae7 1882 {"asciiz", stringer, 8 + 1},
252b5132
RH
1883 {"bss", s_change_sec, 'b'},
1884 {"err", s_err, 0},
1885 {"half", s_cons, 1},
1886 {"dword", s_cons, 3},
1887 {"weakext", s_mips_weakext, 0},
7c752c2a
TS
1888 {"origin", s_org, 0},
1889 {"repeat", s_rept, 0},
252b5132 1890
998b3c36
MR
1891 /* For MIPS this is non-standard, but we define it for consistency. */
1892 {"sbss", s_change_sec, 'B'},
1893
beae10d5 1894 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1895 here for one reason or another. */
1896 {"align", s_align, 0},
1897 {"byte", s_cons, 0},
1898 {"data", s_change_sec, 'd'},
1899 {"double", s_float_cons, 'd'},
1900 {"float", s_float_cons, 'f'},
1901 {"globl", s_mips_globl, 0},
1902 {"global", s_mips_globl, 0},
1903 {"hword", s_cons, 1},
1904 {"int", s_cons, 2},
1905 {"long", s_cons, 2},
1906 {"octa", s_cons, 4},
1907 {"quad", s_cons, 3},
cca86cc8 1908 {"section", s_change_section, 0},
252b5132
RH
1909 {"short", s_cons, 1},
1910 {"single", s_float_cons, 'f'},
754e2bb9 1911 {"stabd", s_mips_stab, 'd'},
252b5132 1912 {"stabn", s_mips_stab, 'n'},
754e2bb9 1913 {"stabs", s_mips_stab, 's'},
252b5132
RH
1914 {"text", s_change_sec, 't'},
1915 {"word", s_cons, 2},
add56521 1916
add56521 1917 { "extern", ecoff_directive_extern, 0},
add56521 1918
43841e91 1919 { NULL, NULL, 0 },
252b5132
RH
1920};
1921
e972090a
NC
1922static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1923{
beae10d5
KH
1924 /* These pseudo-ops should be defined by the object file format.
1925 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1926 {"aent", s_mips_ent, 1},
1927 {"bgnb", s_ignore, 0},
1928 {"end", s_mips_end, 0},
1929 {"endb", s_ignore, 0},
1930 {"ent", s_mips_ent, 0},
c5dd6aab 1931 {"file", s_mips_file, 0},
252b5132
RH
1932 {"fmask", s_mips_mask, 'F'},
1933 {"frame", s_mips_frame, 0},
c5dd6aab 1934 {"loc", s_mips_loc, 0},
252b5132
RH
1935 {"mask", s_mips_mask, 'R'},
1936 {"verstamp", s_ignore, 0},
43841e91 1937 { NULL, NULL, 0 },
252b5132
RH
1938};
1939
3ae8dd8d
MR
1940/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1941 purpose of the `.dc.a' internal pseudo-op. */
1942
1943int
1944mips_address_bytes (void)
1945{
919731af 1946 file_mips_check_options ();
3ae8dd8d
MR
1947 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1948}
1949
17a2f251 1950extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1951
1952void
17a2f251 1953mips_pop_insert (void)
252b5132
RH
1954{
1955 pop_insert (mips_pseudo_table);
1956 if (! ECOFF_DEBUGGING)
1957 pop_insert (mips_nonecoff_pseudo_table);
1958}
1959\f
1960/* Symbols labelling the current insn. */
1961
e972090a
NC
1962struct insn_label_list
1963{
252b5132
RH
1964 struct insn_label_list *next;
1965 symbolS *label;
1966};
1967
252b5132 1968static struct insn_label_list *free_insn_labels;
742a56fe 1969#define label_list tc_segment_info_data.labels
252b5132 1970
17a2f251 1971static void mips_clear_insn_labels (void);
df58fc94
RS
1972static void mips_mark_labels (void);
1973static void mips_compressed_mark_labels (void);
252b5132
RH
1974
1975static inline void
17a2f251 1976mips_clear_insn_labels (void)
252b5132 1977{
ed9e98c2 1978 struct insn_label_list **pl;
a8dbcb85 1979 segment_info_type *si;
252b5132 1980
a8dbcb85
TS
1981 if (now_seg)
1982 {
1983 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1984 ;
3739860c 1985
a8dbcb85
TS
1986 si = seg_info (now_seg);
1987 *pl = si->label_list;
1988 si->label_list = NULL;
1989 }
252b5132 1990}
a8dbcb85 1991
df58fc94
RS
1992/* Mark instruction labels in MIPS16/microMIPS mode. */
1993
1994static inline void
1995mips_mark_labels (void)
1996{
1997 if (HAVE_CODE_COMPRESSION)
1998 mips_compressed_mark_labels ();
1999}
252b5132
RH
2000\f
2001static char *expr_end;
2002
e423441d 2003/* An expression in a macro instruction. This is set by mips_ip and
b0e6f033 2004 mips16_ip and when populated is always an O_constant. */
252b5132
RH
2005
2006static expressionS imm_expr;
252b5132 2007
77bd4346
RS
2008/* The relocatable field in an instruction and the relocs associated
2009 with it. These variables are used for instructions like LUI and
2010 JAL as well as true offsets. They are also used for address
2011 operands in macros. */
252b5132 2012
77bd4346 2013static expressionS offset_expr;
f6688943
TS
2014static bfd_reloc_code_real_type offset_reloc[3]
2015 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 2016
df58fc94
RS
2017/* This is set to the resulting size of the instruction to be produced
2018 by mips16_ip if an explicit extension is used or by mips_ip if an
2019 explicit size is supplied. */
252b5132 2020
df58fc94 2021static unsigned int forced_insn_length;
252b5132 2022
e1b47bd5
RS
2023/* True if we are assembling an instruction. All dot symbols defined during
2024 this time should be treated as code labels. */
2025
2026static bfd_boolean mips_assembling_insn;
2027
ecb4347a
DJ
2028/* The pdr segment for per procedure frame/regmask info. Not used for
2029 ECOFF debugging. */
252b5132
RH
2030
2031static segT pdr_seg;
252b5132 2032
e013f690
TS
2033/* The default target format to use. */
2034
aeffff67
RS
2035#if defined (TE_FreeBSD)
2036#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
2037#elif defined (TE_TMIPS)
2038#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
2039#else
2040#define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
2041#endif
2042
e013f690 2043const char *
17a2f251 2044mips_target_format (void)
e013f690
TS
2045{
2046 switch (OUTPUT_FLAVOR)
2047 {
e013f690 2048 case bfd_target_elf_flavour:
0a44bf69
RS
2049#ifdef TE_VXWORKS
2050 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
2051 return (target_big_endian
2052 ? "elf32-bigmips-vxworks"
2053 : "elf32-littlemips-vxworks");
2054#endif
e013f690 2055 return (target_big_endian
cfe86eaa 2056 ? (HAVE_64BIT_OBJECTS
aeffff67 2057 ? ELF_TARGET ("elf64-", "big")
cfe86eaa 2058 : (HAVE_NEWABI
aeffff67
RS
2059 ? ELF_TARGET ("elf32-n", "big")
2060 : ELF_TARGET ("elf32-", "big")))
cfe86eaa 2061 : (HAVE_64BIT_OBJECTS
aeffff67 2062 ? ELF_TARGET ("elf64-", "little")
cfe86eaa 2063 : (HAVE_NEWABI
aeffff67
RS
2064 ? ELF_TARGET ("elf32-n", "little")
2065 : ELF_TARGET ("elf32-", "little"))));
e013f690
TS
2066 default:
2067 abort ();
2068 return NULL;
2069 }
2070}
2071
c6278170
RS
2072/* Return the ISA revision that is currently in use, or 0 if we are
2073 generating code for MIPS V or below. */
2074
2075static int
2076mips_isa_rev (void)
2077{
2078 if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
2079 return 2;
2080
ae52f483
AB
2081 if (mips_opts.isa == ISA_MIPS32R3 || mips_opts.isa == ISA_MIPS64R3)
2082 return 3;
2083
2084 if (mips_opts.isa == ISA_MIPS32R5 || mips_opts.isa == ISA_MIPS64R5)
2085 return 5;
2086
7361da2c
AB
2087 if (mips_opts.isa == ISA_MIPS32R6 || mips_opts.isa == ISA_MIPS64R6)
2088 return 6;
2089
c6278170
RS
2090 /* microMIPS implies revision 2 or above. */
2091 if (mips_opts.micromips)
2092 return 2;
2093
2094 if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
2095 return 1;
2096
2097 return 0;
2098}
2099
2100/* Return the mask of all ASEs that are revisions of those in FLAGS. */
2101
2102static unsigned int
2103mips_ase_mask (unsigned int flags)
2104{
2105 unsigned int i;
2106
2107 for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
2108 if (flags & mips_ase_groups[i])
2109 flags |= mips_ase_groups[i];
2110 return flags;
2111}
2112
2113/* Check whether the current ISA supports ASE. Issue a warning if
2114 appropriate. */
2115
2116static void
2117mips_check_isa_supports_ase (const struct mips_ase *ase)
2118{
2119 const char *base;
2120 int min_rev, size;
2121 static unsigned int warned_isa;
2122 static unsigned int warned_fp32;
2123
2124 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
2125 min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
2126 else
2127 min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
2128 if ((min_rev < 0 || mips_isa_rev () < min_rev)
2129 && (warned_isa & ase->flags) != ase->flags)
2130 {
2131 warned_isa |= ase->flags;
2132 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2133 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2134 if (min_rev < 0)
1661c76c 2135 as_warn (_("the %d-bit %s architecture does not support the"
c6278170
RS
2136 " `%s' extension"), size, base, ase->name);
2137 else
1661c76c 2138 as_warn (_("the `%s' extension requires %s%d revision %d or greater"),
c6278170
RS
2139 ase->name, base, size, min_rev);
2140 }
7361da2c
AB
2141 else if ((ase->rem_rev > 0 && mips_isa_rev () >= ase->rem_rev)
2142 && (warned_isa & ase->flags) != ase->flags)
2143 {
2144 warned_isa |= ase->flags;
2145 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2146 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2147 as_warn (_("the `%s' extension was removed in %s%d revision %d"),
2148 ase->name, base, size, ase->rem_rev);
2149 }
2150
c6278170 2151 if ((ase->flags & FP64_ASES)
0b35dfee 2152 && mips_opts.fp != 64
c6278170
RS
2153 && (warned_fp32 & ase->flags) != ase->flags)
2154 {
2155 warned_fp32 |= ase->flags;
1661c76c 2156 as_warn (_("the `%s' extension requires 64-bit FPRs"), ase->name);
c6278170
RS
2157 }
2158}
2159
2160/* Check all enabled ASEs to see whether they are supported by the
2161 chosen architecture. */
2162
2163static void
2164mips_check_isa_supports_ases (void)
2165{
2166 unsigned int i, mask;
2167
2168 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2169 {
2170 mask = mips_ase_mask (mips_ases[i].flags);
2171 if ((mips_opts.ase & mask) == mips_ases[i].flags)
2172 mips_check_isa_supports_ase (&mips_ases[i]);
2173 }
2174}
2175
2176/* Set the state of ASE to ENABLED_P. Return the mask of ASE_* flags
2177 that were affected. */
2178
2179static unsigned int
919731af 2180mips_set_ase (const struct mips_ase *ase, struct mips_set_options *opts,
2181 bfd_boolean enabled_p)
c6278170
RS
2182{
2183 unsigned int mask;
2184
2185 mask = mips_ase_mask (ase->flags);
919731af 2186 opts->ase &= ~mask;
92cebb3d
MR
2187
2188 /* Clear combination ASE flags, which need to be recalculated based on
2189 updated regular ASE settings. */
9785fc2a 2190 opts->ase &= ~(ASE_MIPS16E2_MT | ASE_XPA_VIRT);
92cebb3d 2191
c6278170 2192 if (enabled_p)
919731af 2193 opts->ase |= ase->flags;
25499ac7 2194
9785fc2a
MR
2195 /* The Virtualization ASE has eXtended Physical Addressing (XPA)
2196 instructions which are only valid when both ASEs are enabled.
2197 This sets the ASE_XPA_VIRT flag when both ASEs are present. */
2198 if ((opts->ase & (ASE_XPA | ASE_VIRT)) == (ASE_XPA | ASE_VIRT))
2199 {
2200 opts->ase |= ASE_XPA_VIRT;
2201 mask |= ASE_XPA_VIRT;
2202 }
25499ac7
MR
2203 if ((opts->ase & (ASE_MIPS16E2 | ASE_MT)) == (ASE_MIPS16E2 | ASE_MT))
2204 {
2205 opts->ase |= ASE_MIPS16E2_MT;
2206 mask |= ASE_MIPS16E2_MT;
2207 }
2208
c6278170
RS
2209 return mask;
2210}
2211
2212/* Return the ASE called NAME, or null if none. */
2213
2214static const struct mips_ase *
2215mips_lookup_ase (const char *name)
2216{
2217 unsigned int i;
2218
2219 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2220 if (strcmp (name, mips_ases[i].name) == 0)
2221 return &mips_ases[i];
2222 return NULL;
2223}
2224
df58fc94 2225/* Return the length of a microMIPS instruction in bytes. If bits of
100b4f2e
MR
2226 the mask beyond the low 16 are 0, then it is a 16-bit instruction,
2227 otherwise it is a 32-bit instruction. */
df58fc94
RS
2228
2229static inline unsigned int
2230micromips_insn_length (const struct mips_opcode *mo)
2231{
7fd53920 2232 return mips_opcode_32bit_p (mo) ? 4 : 2;
df58fc94
RS
2233}
2234
5c04167a
RS
2235/* Return the length of MIPS16 instruction OPCODE. */
2236
2237static inline unsigned int
2238mips16_opcode_length (unsigned long opcode)
2239{
2240 return (opcode >> 16) == 0 ? 2 : 4;
2241}
2242
1e915849
RS
2243/* Return the length of instruction INSN. */
2244
2245static inline unsigned int
2246insn_length (const struct mips_cl_insn *insn)
2247{
df58fc94
RS
2248 if (mips_opts.micromips)
2249 return micromips_insn_length (insn->insn_mo);
2250 else if (mips_opts.mips16)
5c04167a 2251 return mips16_opcode_length (insn->insn_opcode);
df58fc94 2252 else
1e915849 2253 return 4;
1e915849
RS
2254}
2255
2256/* Initialise INSN from opcode entry MO. Leave its position unspecified. */
2257
2258static void
2259create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
2260{
2261 size_t i;
2262
2263 insn->insn_mo = mo;
1e915849
RS
2264 insn->insn_opcode = mo->match;
2265 insn->frag = NULL;
2266 insn->where = 0;
2267 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2268 insn->fixp[i] = NULL;
2269 insn->fixed_p = (mips_opts.noreorder > 0);
2270 insn->noreorder_p = (mips_opts.noreorder > 0);
2271 insn->mips16_absolute_jump_p = 0;
15be625d 2272 insn->complete_p = 0;
e407c74b 2273 insn->cleared_p = 0;
1e915849
RS
2274}
2275
fc76e730
RS
2276/* Get a list of all the operands in INSN. */
2277
2278static const struct mips_operand_array *
2279insn_operands (const struct mips_cl_insn *insn)
2280{
2281 if (insn->insn_mo >= &mips_opcodes[0]
2282 && insn->insn_mo < &mips_opcodes[NUMOPCODES])
2283 return &mips_operands[insn->insn_mo - &mips_opcodes[0]];
2284
2285 if (insn->insn_mo >= &mips16_opcodes[0]
2286 && insn->insn_mo < &mips16_opcodes[bfd_mips16_num_opcodes])
2287 return &mips16_operands[insn->insn_mo - &mips16_opcodes[0]];
2288
2289 if (insn->insn_mo >= &micromips_opcodes[0]
2290 && insn->insn_mo < &micromips_opcodes[bfd_micromips_num_opcodes])
2291 return &micromips_operands[insn->insn_mo - &micromips_opcodes[0]];
2292
2293 abort ();
2294}
2295
2296/* Get a description of operand OPNO of INSN. */
2297
2298static const struct mips_operand *
2299insn_opno (const struct mips_cl_insn *insn, unsigned opno)
2300{
2301 const struct mips_operand_array *operands;
2302
2303 operands = insn_operands (insn);
2304 if (opno >= MAX_OPERANDS || !operands->operand[opno])
2305 abort ();
2306 return operands->operand[opno];
2307}
2308
e077a1c8
RS
2309/* Install UVAL as the value of OPERAND in INSN. */
2310
2311static inline void
2312insn_insert_operand (struct mips_cl_insn *insn,
2313 const struct mips_operand *operand, unsigned int uval)
2314{
25499ac7
MR
2315 if (mips_opts.mips16
2316 && operand->type == OP_INT && operand->lsb == 0
2317 && mips_opcode_32bit_p (insn->insn_mo))
2318 insn->insn_opcode |= mips16_immed_extend (uval, operand->size);
2319 else
2320 insn->insn_opcode = mips_insert_operand (operand, insn->insn_opcode, uval);
e077a1c8
RS
2321}
2322
fc76e730
RS
2323/* Extract the value of OPERAND from INSN. */
2324
2325static inline unsigned
2326insn_extract_operand (const struct mips_cl_insn *insn,
2327 const struct mips_operand *operand)
2328{
2329 return mips_extract_operand (operand, insn->insn_opcode);
2330}
2331
df58fc94 2332/* Record the current MIPS16/microMIPS mode in now_seg. */
742a56fe
RS
2333
2334static void
df58fc94 2335mips_record_compressed_mode (void)
742a56fe
RS
2336{
2337 segment_info_type *si;
2338
2339 si = seg_info (now_seg);
2340 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2341 si->tc_segment_info_data.mips16 = mips_opts.mips16;
df58fc94
RS
2342 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2343 si->tc_segment_info_data.micromips = mips_opts.micromips;
742a56fe
RS
2344}
2345
4d68580a
RS
2346/* Read a standard MIPS instruction from BUF. */
2347
2348static unsigned long
2349read_insn (char *buf)
2350{
2351 if (target_big_endian)
2352 return bfd_getb32 ((bfd_byte *) buf);
2353 else
2354 return bfd_getl32 ((bfd_byte *) buf);
2355}
2356
2357/* Write standard MIPS instruction INSN to BUF. Return a pointer to
2358 the next byte. */
2359
2360static char *
2361write_insn (char *buf, unsigned int insn)
2362{
2363 md_number_to_chars (buf, insn, 4);
2364 return buf + 4;
2365}
2366
2367/* Read a microMIPS or MIPS16 opcode from BUF, given that it
2368 has length LENGTH. */
2369
2370static unsigned long
2371read_compressed_insn (char *buf, unsigned int length)
2372{
2373 unsigned long insn;
2374 unsigned int i;
2375
2376 insn = 0;
2377 for (i = 0; i < length; i += 2)
2378 {
2379 insn <<= 16;
2380 if (target_big_endian)
2381 insn |= bfd_getb16 ((char *) buf);
2382 else
2383 insn |= bfd_getl16 ((char *) buf);
2384 buf += 2;
2385 }
2386 return insn;
2387}
2388
5c04167a
RS
2389/* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2390 instruction is LENGTH bytes long. Return a pointer to the next byte. */
2391
2392static char *
2393write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2394{
2395 unsigned int i;
2396
2397 for (i = 0; i < length; i += 2)
2398 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2399 return buf + length;
2400}
2401
1e915849
RS
2402/* Install INSN at the location specified by its "frag" and "where" fields. */
2403
2404static void
2405install_insn (const struct mips_cl_insn *insn)
2406{
2407 char *f = insn->frag->fr_literal + insn->where;
5c04167a
RS
2408 if (HAVE_CODE_COMPRESSION)
2409 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
1e915849 2410 else
4d68580a 2411 write_insn (f, insn->insn_opcode);
df58fc94 2412 mips_record_compressed_mode ();
1e915849
RS
2413}
2414
2415/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
2416 and install the opcode in the new location. */
2417
2418static void
2419move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2420{
2421 size_t i;
2422
2423 insn->frag = frag;
2424 insn->where = where;
2425 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2426 if (insn->fixp[i] != NULL)
2427 {
2428 insn->fixp[i]->fx_frag = frag;
2429 insn->fixp[i]->fx_where = where;
2430 }
2431 install_insn (insn);
2432}
2433
2434/* Add INSN to the end of the output. */
2435
2436static void
2437add_fixed_insn (struct mips_cl_insn *insn)
2438{
2439 char *f = frag_more (insn_length (insn));
2440 move_insn (insn, frag_now, f - frag_now->fr_literal);
2441}
2442
2443/* Start a variant frag and move INSN to the start of the variant part,
2444 marking it as fixed. The other arguments are as for frag_var. */
2445
2446static void
2447add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2448 relax_substateT subtype, symbolS *symbol, offsetT offset)
2449{
2450 frag_grow (max_chars);
2451 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2452 insn->fixed_p = 1;
2453 frag_var (rs_machine_dependent, max_chars, var,
2454 subtype, symbol, offset, NULL);
2455}
2456
2457/* Insert N copies of INSN into the history buffer, starting at
2458 position FIRST. Neither FIRST nor N need to be clipped. */
2459
2460static void
2461insert_into_history (unsigned int first, unsigned int n,
2462 const struct mips_cl_insn *insn)
2463{
2464 if (mips_relax.sequence != 2)
2465 {
2466 unsigned int i;
2467
2468 for (i = ARRAY_SIZE (history); i-- > first;)
2469 if (i >= first + n)
2470 history[i] = history[i - n];
2471 else
2472 history[i] = *insn;
2473 }
2474}
2475
e3de51ce
RS
2476/* Clear the error in insn_error. */
2477
2478static void
2479clear_insn_error (void)
2480{
2481 memset (&insn_error, 0, sizeof (insn_error));
2482}
2483
2484/* Possibly record error message MSG for the current instruction.
2485 If the error is about a particular argument, ARGNUM is the 1-based
2486 number of that argument, otherwise it is 0. FORMAT is the format
2487 of MSG. Return true if MSG was used, false if the current message
2488 was kept. */
2489
2490static bfd_boolean
2491set_insn_error_format (int argnum, enum mips_insn_error_format format,
2492 const char *msg)
2493{
2494 if (argnum == 0)
2495 {
2496 /* Give priority to errors against specific arguments, and to
2497 the first whole-instruction message. */
2498 if (insn_error.msg)
2499 return FALSE;
2500 }
2501 else
2502 {
2503 /* Keep insn_error if it is against a later argument. */
2504 if (argnum < insn_error.min_argnum)
2505 return FALSE;
2506
2507 /* If both errors are against the same argument but are different,
2508 give up on reporting a specific error for this argument.
2509 See the comment about mips_insn_error for details. */
2510 if (argnum == insn_error.min_argnum
2511 && insn_error.msg
2512 && strcmp (insn_error.msg, msg) != 0)
2513 {
2514 insn_error.msg = 0;
2515 insn_error.min_argnum += 1;
2516 return FALSE;
2517 }
2518 }
2519 insn_error.min_argnum = argnum;
2520 insn_error.format = format;
2521 insn_error.msg = msg;
2522 return TRUE;
2523}
2524
2525/* Record an instruction error with no % format fields. ARGNUM and MSG are
2526 as for set_insn_error_format. */
2527
2528static void
2529set_insn_error (int argnum, const char *msg)
2530{
2531 set_insn_error_format (argnum, ERR_FMT_PLAIN, msg);
2532}
2533
2534/* Record an instruction error with one %d field I. ARGNUM and MSG are
2535 as for set_insn_error_format. */
2536
2537static void
2538set_insn_error_i (int argnum, const char *msg, int i)
2539{
2540 if (set_insn_error_format (argnum, ERR_FMT_I, msg))
2541 insn_error.u.i = i;
2542}
2543
2544/* Record an instruction error with two %s fields S1 and S2. ARGNUM and MSG
2545 are as for set_insn_error_format. */
2546
2547static void
2548set_insn_error_ss (int argnum, const char *msg, const char *s1, const char *s2)
2549{
2550 if (set_insn_error_format (argnum, ERR_FMT_SS, msg))
2551 {
2552 insn_error.u.ss[0] = s1;
2553 insn_error.u.ss[1] = s2;
2554 }
2555}
2556
2557/* Report the error in insn_error, which is against assembly code STR. */
2558
2559static void
2560report_insn_error (const char *str)
2561{
e1fa0163 2562 const char *msg = concat (insn_error.msg, " `%s'", NULL);
e3de51ce 2563
e3de51ce
RS
2564 switch (insn_error.format)
2565 {
2566 case ERR_FMT_PLAIN:
2567 as_bad (msg, str);
2568 break;
2569
2570 case ERR_FMT_I:
2571 as_bad (msg, insn_error.u.i, str);
2572 break;
2573
2574 case ERR_FMT_SS:
2575 as_bad (msg, insn_error.u.ss[0], insn_error.u.ss[1], str);
2576 break;
2577 }
e1fa0163
NC
2578
2579 free ((char *) msg);
e3de51ce
RS
2580}
2581
71400594
RS
2582/* Initialize vr4120_conflicts. There is a bit of duplication here:
2583 the idea is to make it obvious at a glance that each errata is
2584 included. */
2585
2586static void
2587init_vr4120_conflicts (void)
2588{
2589#define CONFLICT(FIRST, SECOND) \
2590 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2591
2592 /* Errata 21 - [D]DIV[U] after [D]MACC */
2593 CONFLICT (MACC, DIV);
2594 CONFLICT (DMACC, DIV);
2595
2596 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
2597 CONFLICT (DMULT, DMULT);
2598 CONFLICT (DMULT, DMACC);
2599 CONFLICT (DMACC, DMULT);
2600 CONFLICT (DMACC, DMACC);
2601
2602 /* Errata 24 - MT{LO,HI} after [D]MACC */
2603 CONFLICT (MACC, MTHILO);
2604 CONFLICT (DMACC, MTHILO);
2605
2606 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2607 instruction is executed immediately after a MACC or DMACC
2608 instruction, the result of [either instruction] is incorrect." */
2609 CONFLICT (MACC, MULT);
2610 CONFLICT (MACC, DMULT);
2611 CONFLICT (DMACC, MULT);
2612 CONFLICT (DMACC, DMULT);
2613
2614 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2615 executed immediately after a DMULT, DMULTU, DIV, DIVU,
2616 DDIV or DDIVU instruction, the result of the MACC or
2617 DMACC instruction is incorrect.". */
2618 CONFLICT (DMULT, MACC);
2619 CONFLICT (DMULT, DMACC);
2620 CONFLICT (DIV, MACC);
2621 CONFLICT (DIV, DMACC);
2622
2623#undef CONFLICT
2624}
2625
707bfff6
TS
2626struct regname {
2627 const char *name;
2628 unsigned int num;
2629};
2630
14daeee3 2631#define RNUM_MASK 0x00000ff
56d438b1 2632#define RTYPE_MASK 0x0ffff00
14daeee3
RS
2633#define RTYPE_NUM 0x0000100
2634#define RTYPE_FPU 0x0000200
2635#define RTYPE_FCC 0x0000400
2636#define RTYPE_VEC 0x0000800
2637#define RTYPE_GP 0x0001000
2638#define RTYPE_CP0 0x0002000
2639#define RTYPE_PC 0x0004000
2640#define RTYPE_ACC 0x0008000
2641#define RTYPE_CCC 0x0010000
2642#define RTYPE_VI 0x0020000
2643#define RTYPE_VF 0x0040000
2644#define RTYPE_R5900_I 0x0080000
2645#define RTYPE_R5900_Q 0x0100000
2646#define RTYPE_R5900_R 0x0200000
2647#define RTYPE_R5900_ACC 0x0400000
56d438b1 2648#define RTYPE_MSA 0x0800000
14daeee3 2649#define RWARN 0x8000000
707bfff6
TS
2650
2651#define GENERIC_REGISTER_NUMBERS \
2652 {"$0", RTYPE_NUM | 0}, \
2653 {"$1", RTYPE_NUM | 1}, \
2654 {"$2", RTYPE_NUM | 2}, \
2655 {"$3", RTYPE_NUM | 3}, \
2656 {"$4", RTYPE_NUM | 4}, \
2657 {"$5", RTYPE_NUM | 5}, \
2658 {"$6", RTYPE_NUM | 6}, \
2659 {"$7", RTYPE_NUM | 7}, \
2660 {"$8", RTYPE_NUM | 8}, \
2661 {"$9", RTYPE_NUM | 9}, \
2662 {"$10", RTYPE_NUM | 10}, \
2663 {"$11", RTYPE_NUM | 11}, \
2664 {"$12", RTYPE_NUM | 12}, \
2665 {"$13", RTYPE_NUM | 13}, \
2666 {"$14", RTYPE_NUM | 14}, \
2667 {"$15", RTYPE_NUM | 15}, \
2668 {"$16", RTYPE_NUM | 16}, \
2669 {"$17", RTYPE_NUM | 17}, \
2670 {"$18", RTYPE_NUM | 18}, \
2671 {"$19", RTYPE_NUM | 19}, \
2672 {"$20", RTYPE_NUM | 20}, \
2673 {"$21", RTYPE_NUM | 21}, \
2674 {"$22", RTYPE_NUM | 22}, \
2675 {"$23", RTYPE_NUM | 23}, \
2676 {"$24", RTYPE_NUM | 24}, \
2677 {"$25", RTYPE_NUM | 25}, \
2678 {"$26", RTYPE_NUM | 26}, \
2679 {"$27", RTYPE_NUM | 27}, \
2680 {"$28", RTYPE_NUM | 28}, \
2681 {"$29", RTYPE_NUM | 29}, \
2682 {"$30", RTYPE_NUM | 30}, \
3739860c 2683 {"$31", RTYPE_NUM | 31}
707bfff6
TS
2684
2685#define FPU_REGISTER_NAMES \
2686 {"$f0", RTYPE_FPU | 0}, \
2687 {"$f1", RTYPE_FPU | 1}, \
2688 {"$f2", RTYPE_FPU | 2}, \
2689 {"$f3", RTYPE_FPU | 3}, \
2690 {"$f4", RTYPE_FPU | 4}, \
2691 {"$f5", RTYPE_FPU | 5}, \
2692 {"$f6", RTYPE_FPU | 6}, \
2693 {"$f7", RTYPE_FPU | 7}, \
2694 {"$f8", RTYPE_FPU | 8}, \
2695 {"$f9", RTYPE_FPU | 9}, \
2696 {"$f10", RTYPE_FPU | 10}, \
2697 {"$f11", RTYPE_FPU | 11}, \
2698 {"$f12", RTYPE_FPU | 12}, \
2699 {"$f13", RTYPE_FPU | 13}, \
2700 {"$f14", RTYPE_FPU | 14}, \
2701 {"$f15", RTYPE_FPU | 15}, \
2702 {"$f16", RTYPE_FPU | 16}, \
2703 {"$f17", RTYPE_FPU | 17}, \
2704 {"$f18", RTYPE_FPU | 18}, \
2705 {"$f19", RTYPE_FPU | 19}, \
2706 {"$f20", RTYPE_FPU | 20}, \
2707 {"$f21", RTYPE_FPU | 21}, \
2708 {"$f22", RTYPE_FPU | 22}, \
2709 {"$f23", RTYPE_FPU | 23}, \
2710 {"$f24", RTYPE_FPU | 24}, \
2711 {"$f25", RTYPE_FPU | 25}, \
2712 {"$f26", RTYPE_FPU | 26}, \
2713 {"$f27", RTYPE_FPU | 27}, \
2714 {"$f28", RTYPE_FPU | 28}, \
2715 {"$f29", RTYPE_FPU | 29}, \
2716 {"$f30", RTYPE_FPU | 30}, \
2717 {"$f31", RTYPE_FPU | 31}
2718
2719#define FPU_CONDITION_CODE_NAMES \
2720 {"$fcc0", RTYPE_FCC | 0}, \
2721 {"$fcc1", RTYPE_FCC | 1}, \
2722 {"$fcc2", RTYPE_FCC | 2}, \
2723 {"$fcc3", RTYPE_FCC | 3}, \
2724 {"$fcc4", RTYPE_FCC | 4}, \
2725 {"$fcc5", RTYPE_FCC | 5}, \
2726 {"$fcc6", RTYPE_FCC | 6}, \
2727 {"$fcc7", RTYPE_FCC | 7}
2728
2729#define COPROC_CONDITION_CODE_NAMES \
2730 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
2731 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
2732 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
2733 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
2734 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
2735 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
2736 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
2737 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
2738
2739#define N32N64_SYMBOLIC_REGISTER_NAMES \
2740 {"$a4", RTYPE_GP | 8}, \
2741 {"$a5", RTYPE_GP | 9}, \
2742 {"$a6", RTYPE_GP | 10}, \
2743 {"$a7", RTYPE_GP | 11}, \
2744 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
2745 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
2746 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
2747 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
2748 {"$t0", RTYPE_GP | 12}, \
2749 {"$t1", RTYPE_GP | 13}, \
2750 {"$t2", RTYPE_GP | 14}, \
2751 {"$t3", RTYPE_GP | 15}
2752
2753#define O32_SYMBOLIC_REGISTER_NAMES \
2754 {"$t0", RTYPE_GP | 8}, \
2755 {"$t1", RTYPE_GP | 9}, \
2756 {"$t2", RTYPE_GP | 10}, \
2757 {"$t3", RTYPE_GP | 11}, \
2758 {"$t4", RTYPE_GP | 12}, \
2759 {"$t5", RTYPE_GP | 13}, \
2760 {"$t6", RTYPE_GP | 14}, \
2761 {"$t7", RTYPE_GP | 15}, \
2762 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2763 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2764 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
3739860c 2765 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
707bfff6
TS
2766
2767/* Remaining symbolic register names */
2768#define SYMBOLIC_REGISTER_NAMES \
2769 {"$zero", RTYPE_GP | 0}, \
2770 {"$at", RTYPE_GP | 1}, \
2771 {"$AT", RTYPE_GP | 1}, \
2772 {"$v0", RTYPE_GP | 2}, \
2773 {"$v1", RTYPE_GP | 3}, \
2774 {"$a0", RTYPE_GP | 4}, \
2775 {"$a1", RTYPE_GP | 5}, \
2776 {"$a2", RTYPE_GP | 6}, \
2777 {"$a3", RTYPE_GP | 7}, \
2778 {"$s0", RTYPE_GP | 16}, \
2779 {"$s1", RTYPE_GP | 17}, \
2780 {"$s2", RTYPE_GP | 18}, \
2781 {"$s3", RTYPE_GP | 19}, \
2782 {"$s4", RTYPE_GP | 20}, \
2783 {"$s5", RTYPE_GP | 21}, \
2784 {"$s6", RTYPE_GP | 22}, \
2785 {"$s7", RTYPE_GP | 23}, \
2786 {"$t8", RTYPE_GP | 24}, \
2787 {"$t9", RTYPE_GP | 25}, \
2788 {"$k0", RTYPE_GP | 26}, \
2789 {"$kt0", RTYPE_GP | 26}, \
2790 {"$k1", RTYPE_GP | 27}, \
2791 {"$kt1", RTYPE_GP | 27}, \
2792 {"$gp", RTYPE_GP | 28}, \
2793 {"$sp", RTYPE_GP | 29}, \
2794 {"$s8", RTYPE_GP | 30}, \
2795 {"$fp", RTYPE_GP | 30}, \
2796 {"$ra", RTYPE_GP | 31}
2797
2798#define MIPS16_SPECIAL_REGISTER_NAMES \
2799 {"$pc", RTYPE_PC | 0}
2800
2801#define MDMX_VECTOR_REGISTER_NAMES \
2802 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
2803 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
2804 {"$v2", RTYPE_VEC | 2}, \
2805 {"$v3", RTYPE_VEC | 3}, \
2806 {"$v4", RTYPE_VEC | 4}, \
2807 {"$v5", RTYPE_VEC | 5}, \
2808 {"$v6", RTYPE_VEC | 6}, \
2809 {"$v7", RTYPE_VEC | 7}, \
2810 {"$v8", RTYPE_VEC | 8}, \
2811 {"$v9", RTYPE_VEC | 9}, \
2812 {"$v10", RTYPE_VEC | 10}, \
2813 {"$v11", RTYPE_VEC | 11}, \
2814 {"$v12", RTYPE_VEC | 12}, \
2815 {"$v13", RTYPE_VEC | 13}, \
2816 {"$v14", RTYPE_VEC | 14}, \
2817 {"$v15", RTYPE_VEC | 15}, \
2818 {"$v16", RTYPE_VEC | 16}, \
2819 {"$v17", RTYPE_VEC | 17}, \
2820 {"$v18", RTYPE_VEC | 18}, \
2821 {"$v19", RTYPE_VEC | 19}, \
2822 {"$v20", RTYPE_VEC | 20}, \
2823 {"$v21", RTYPE_VEC | 21}, \
2824 {"$v22", RTYPE_VEC | 22}, \
2825 {"$v23", RTYPE_VEC | 23}, \
2826 {"$v24", RTYPE_VEC | 24}, \
2827 {"$v25", RTYPE_VEC | 25}, \
2828 {"$v26", RTYPE_VEC | 26}, \
2829 {"$v27", RTYPE_VEC | 27}, \
2830 {"$v28", RTYPE_VEC | 28}, \
2831 {"$v29", RTYPE_VEC | 29}, \
2832 {"$v30", RTYPE_VEC | 30}, \
2833 {"$v31", RTYPE_VEC | 31}
2834
14daeee3
RS
2835#define R5900_I_NAMES \
2836 {"$I", RTYPE_R5900_I | 0}
2837
2838#define R5900_Q_NAMES \
2839 {"$Q", RTYPE_R5900_Q | 0}
2840
2841#define R5900_R_NAMES \
2842 {"$R", RTYPE_R5900_R | 0}
2843
2844#define R5900_ACC_NAMES \
2845 {"$ACC", RTYPE_R5900_ACC | 0 }
2846
707bfff6
TS
2847#define MIPS_DSP_ACCUMULATOR_NAMES \
2848 {"$ac0", RTYPE_ACC | 0}, \
2849 {"$ac1", RTYPE_ACC | 1}, \
2850 {"$ac2", RTYPE_ACC | 2}, \
2851 {"$ac3", RTYPE_ACC | 3}
2852
2853static const struct regname reg_names[] = {
2854 GENERIC_REGISTER_NUMBERS,
2855 FPU_REGISTER_NAMES,
2856 FPU_CONDITION_CODE_NAMES,
2857 COPROC_CONDITION_CODE_NAMES,
2858
2859 /* The $txx registers depends on the abi,
2860 these will be added later into the symbol table from
3739860c 2861 one of the tables below once mips_abi is set after
707bfff6
TS
2862 parsing of arguments from the command line. */
2863 SYMBOLIC_REGISTER_NAMES,
2864
2865 MIPS16_SPECIAL_REGISTER_NAMES,
2866 MDMX_VECTOR_REGISTER_NAMES,
14daeee3
RS
2867 R5900_I_NAMES,
2868 R5900_Q_NAMES,
2869 R5900_R_NAMES,
2870 R5900_ACC_NAMES,
707bfff6
TS
2871 MIPS_DSP_ACCUMULATOR_NAMES,
2872 {0, 0}
2873};
2874
2875static const struct regname reg_names_o32[] = {
2876 O32_SYMBOLIC_REGISTER_NAMES,
2877 {0, 0}
2878};
2879
2880static const struct regname reg_names_n32n64[] = {
2881 N32N64_SYMBOLIC_REGISTER_NAMES,
2882 {0, 0}
2883};
2884
a92713e6
RS
2885/* Register symbols $v0 and $v1 map to GPRs 2 and 3, but they can also be
2886 interpreted as vector registers 0 and 1. If SYMVAL is the value of one
2887 of these register symbols, return the associated vector register,
2888 otherwise return SYMVAL itself. */
df58fc94 2889
a92713e6
RS
2890static unsigned int
2891mips_prefer_vec_regno (unsigned int symval)
707bfff6 2892{
a92713e6
RS
2893 if ((symval & -2) == (RTYPE_GP | 2))
2894 return RTYPE_VEC | (symval & 1);
2895 return symval;
2896}
2897
14daeee3
RS
2898/* Return true if string [S, E) is a valid register name, storing its
2899 symbol value in *SYMVAL_PTR if so. */
a92713e6
RS
2900
2901static bfd_boolean
14daeee3 2902mips_parse_register_1 (char *s, char *e, unsigned int *symval_ptr)
a92713e6 2903{
707bfff6 2904 char save_c;
14daeee3 2905 symbolS *symbol;
707bfff6
TS
2906
2907 /* Terminate name. */
2908 save_c = *e;
2909 *e = '\0';
2910
a92713e6
RS
2911 /* Look up the name. */
2912 symbol = symbol_find (s);
2913 *e = save_c;
2914
2915 if (!symbol || S_GET_SEGMENT (symbol) != reg_section)
2916 return FALSE;
2917
14daeee3
RS
2918 *symval_ptr = S_GET_VALUE (symbol);
2919 return TRUE;
2920}
2921
2922/* Return true if the string at *SPTR is a valid register name. Allow it
2923 to have a VU0-style channel suffix of the form x?y?z?w? if CHANNELS_PTR
2924 is nonnull.
2925
2926 When returning true, move *SPTR past the register, store the
2927 register's symbol value in *SYMVAL_PTR and the channel mask in
2928 *CHANNELS_PTR (if nonnull). The symbol value includes the register
2929 number (RNUM_MASK) and register type (RTYPE_MASK). The channel mask
2930 is a 4-bit value of the form XYZW and is 0 if no suffix was given. */
2931
2932static bfd_boolean
2933mips_parse_register (char **sptr, unsigned int *symval_ptr,
2934 unsigned int *channels_ptr)
2935{
2936 char *s, *e, *m;
2937 const char *q;
2938 unsigned int channels, symval, bit;
2939
2940 /* Find end of name. */
2941 s = e = *sptr;
2942 if (is_name_beginner (*e))
2943 ++e;
2944 while (is_part_of_name (*e))
2945 ++e;
2946
2947 channels = 0;
2948 if (!mips_parse_register_1 (s, e, &symval))
2949 {
2950 if (!channels_ptr)
2951 return FALSE;
2952
2953 /* Eat characters from the end of the string that are valid
2954 channel suffixes. The preceding register must be $ACC or
2955 end with a digit, so there is no ambiguity. */
2956 bit = 1;
2957 m = e;
2958 for (q = "wzyx"; *q; q++, bit <<= 1)
2959 if (m > s && m[-1] == *q)
2960 {
2961 --m;
2962 channels |= bit;
2963 }
2964
2965 if (channels == 0
2966 || !mips_parse_register_1 (s, m, &symval)
2967 || (symval & (RTYPE_VI | RTYPE_VF | RTYPE_R5900_ACC)) == 0)
2968 return FALSE;
2969 }
2970
a92713e6 2971 *sptr = e;
14daeee3
RS
2972 *symval_ptr = symval;
2973 if (channels_ptr)
2974 *channels_ptr = channels;
a92713e6
RS
2975 return TRUE;
2976}
2977
2978/* Check if SPTR points at a valid register specifier according to TYPES.
2979 If so, then return 1, advance S to consume the specifier and store
2980 the register's number in REGNOP, otherwise return 0. */
2981
2982static int
2983reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2984{
2985 unsigned int regno;
2986
14daeee3 2987 if (mips_parse_register (s, &regno, NULL))
707bfff6 2988 {
a92713e6
RS
2989 if (types & RTYPE_VEC)
2990 regno = mips_prefer_vec_regno (regno);
2991 if (regno & types)
2992 regno &= RNUM_MASK;
2993 else
2994 regno = ~0;
707bfff6 2995 }
a92713e6 2996 else
707bfff6 2997 {
a92713e6 2998 if (types & RWARN)
1661c76c 2999 as_warn (_("unrecognized register name `%s'"), *s);
a92713e6 3000 regno = ~0;
707bfff6 3001 }
707bfff6 3002 if (regnop)
a92713e6
RS
3003 *regnop = regno;
3004 return regno <= RNUM_MASK;
707bfff6
TS
3005}
3006
14daeee3
RS
3007/* Parse a VU0 "x?y?z?w?" channel mask at S and store the associated
3008 mask in *CHANNELS. Return a pointer to the first unconsumed character. */
3009
3010static char *
3011mips_parse_vu0_channels (char *s, unsigned int *channels)
3012{
3013 unsigned int i;
3014
3015 *channels = 0;
3016 for (i = 0; i < 4; i++)
3017 if (*s == "xyzw"[i])
3018 {
3019 *channels |= 1 << (3 - i);
3020 ++s;
3021 }
3022 return s;
3023}
3024
a92713e6
RS
3025/* Token types for parsed operand lists. */
3026enum mips_operand_token_type {
3027 /* A plain register, e.g. $f2. */
3028 OT_REG,
df58fc94 3029
14daeee3
RS
3030 /* A 4-bit XYZW channel mask. */
3031 OT_CHANNELS,
3032
56d438b1
CF
3033 /* A constant vector index, e.g. [1]. */
3034 OT_INTEGER_INDEX,
3035
3036 /* A register vector index, e.g. [$2]. */
3037 OT_REG_INDEX,
df58fc94 3038
a92713e6
RS
3039 /* A continuous range of registers, e.g. $s0-$s4. */
3040 OT_REG_RANGE,
3041
3042 /* A (possibly relocated) expression. */
3043 OT_INTEGER,
3044
3045 /* A floating-point value. */
3046 OT_FLOAT,
3047
3048 /* A single character. This can be '(', ')' or ',', but '(' only appears
3049 before OT_REGs. */
3050 OT_CHAR,
3051
14daeee3
RS
3052 /* A doubled character, either "--" or "++". */
3053 OT_DOUBLE_CHAR,
3054
a92713e6
RS
3055 /* The end of the operand list. */
3056 OT_END
3057};
3058
3059/* A parsed operand token. */
3060struct mips_operand_token
3061{
3062 /* The type of token. */
3063 enum mips_operand_token_type type;
3064 union
3065 {
56d438b1 3066 /* The register symbol value for an OT_REG or OT_REG_INDEX. */
a92713e6
RS
3067 unsigned int regno;
3068
14daeee3
RS
3069 /* The 4-bit channel mask for an OT_CHANNEL_SUFFIX. */
3070 unsigned int channels;
3071
56d438b1
CF
3072 /* The integer value of an OT_INTEGER_INDEX. */
3073 addressT index;
a92713e6
RS
3074
3075 /* The two register symbol values involved in an OT_REG_RANGE. */
3076 struct {
3077 unsigned int regno1;
3078 unsigned int regno2;
3079 } reg_range;
3080
3081 /* The value of an OT_INTEGER. The value is represented as an
3082 expression and the relocation operators that were applied to
3083 that expression. The reloc entries are BFD_RELOC_UNUSED if no
3084 relocation operators were used. */
3085 struct {
3086 expressionS value;
3087 bfd_reloc_code_real_type relocs[3];
3088 } integer;
3089
3090 /* The binary data for an OT_FLOAT constant, and the number of bytes
3091 in the constant. */
3092 struct {
3093 unsigned char data[8];
3094 int length;
3095 } flt;
3096
14daeee3 3097 /* The character represented by an OT_CHAR or OT_DOUBLE_CHAR. */
a92713e6
RS
3098 char ch;
3099 } u;
3100};
3101
3102/* An obstack used to construct lists of mips_operand_tokens. */
3103static struct obstack mips_operand_tokens;
3104
3105/* Give TOKEN type TYPE and add it to mips_operand_tokens. */
3106
3107static void
3108mips_add_token (struct mips_operand_token *token,
3109 enum mips_operand_token_type type)
3110{
3111 token->type = type;
3112 obstack_grow (&mips_operand_tokens, token, sizeof (*token));
3113}
3114
3115/* Check whether S is '(' followed by a register name. Add OT_CHAR
3116 and OT_REG tokens for them if so, and return a pointer to the first
3117 unconsumed character. Return null otherwise. */
3118
3119static char *
3120mips_parse_base_start (char *s)
3121{
3122 struct mips_operand_token token;
14daeee3
RS
3123 unsigned int regno, channels;
3124 bfd_boolean decrement_p;
df58fc94 3125
a92713e6
RS
3126 if (*s != '(')
3127 return 0;
3128
3129 ++s;
3130 SKIP_SPACE_TABS (s);
14daeee3
RS
3131
3132 /* Only match "--" as part of a base expression. In other contexts "--X"
3133 is a double negative. */
3134 decrement_p = (s[0] == '-' && s[1] == '-');
3135 if (decrement_p)
3136 {
3137 s += 2;
3138 SKIP_SPACE_TABS (s);
3139 }
3140
3141 /* Allow a channel specifier because that leads to better error messages
3142 than treating something like "$vf0x++" as an expression. */
3143 if (!mips_parse_register (&s, &regno, &channels))
a92713e6
RS
3144 return 0;
3145
3146 token.u.ch = '(';
3147 mips_add_token (&token, OT_CHAR);
3148
14daeee3
RS
3149 if (decrement_p)
3150 {
3151 token.u.ch = '-';
3152 mips_add_token (&token, OT_DOUBLE_CHAR);
3153 }
3154
a92713e6
RS
3155 token.u.regno = regno;
3156 mips_add_token (&token, OT_REG);
3157
14daeee3
RS
3158 if (channels)
3159 {
3160 token.u.channels = channels;
3161 mips_add_token (&token, OT_CHANNELS);
3162 }
3163
3164 /* For consistency, only match "++" as part of base expressions too. */
3165 SKIP_SPACE_TABS (s);
3166 if (s[0] == '+' && s[1] == '+')
3167 {
3168 s += 2;
3169 token.u.ch = '+';
3170 mips_add_token (&token, OT_DOUBLE_CHAR);
3171 }
3172
a92713e6
RS
3173 return s;
3174}
3175
3176/* Parse one or more tokens from S. Return a pointer to the first
3177 unconsumed character on success. Return null if an error was found
3178 and store the error text in insn_error. FLOAT_FORMAT is as for
3179 mips_parse_arguments. */
3180
3181static char *
3182mips_parse_argument_token (char *s, char float_format)
3183{
6d4af3c2
AM
3184 char *end, *save_in;
3185 const char *err;
14daeee3 3186 unsigned int regno1, regno2, channels;
a92713e6
RS
3187 struct mips_operand_token token;
3188
3189 /* First look for "($reg", since we want to treat that as an
3190 OT_CHAR and OT_REG rather than an expression. */
3191 end = mips_parse_base_start (s);
3192 if (end)
3193 return end;
3194
3195 /* Handle other characters that end up as OT_CHARs. */
3196 if (*s == ')' || *s == ',')
3197 {
3198 token.u.ch = *s;
3199 mips_add_token (&token, OT_CHAR);
3200 ++s;
3201 return s;
3202 }
3203
3204 /* Handle tokens that start with a register. */
14daeee3 3205 if (mips_parse_register (&s, &regno1, &channels))
df58fc94 3206 {
14daeee3
RS
3207 if (channels)
3208 {
3209 /* A register and a VU0 channel suffix. */
3210 token.u.regno = regno1;
3211 mips_add_token (&token, OT_REG);
3212
3213 token.u.channels = channels;
3214 mips_add_token (&token, OT_CHANNELS);
3215 return s;
3216 }
3217
a92713e6
RS
3218 SKIP_SPACE_TABS (s);
3219 if (*s == '-')
df58fc94 3220 {
a92713e6
RS
3221 /* A register range. */
3222 ++s;
3223 SKIP_SPACE_TABS (s);
14daeee3 3224 if (!mips_parse_register (&s, &regno2, NULL))
a92713e6 3225 {
1661c76c 3226 set_insn_error (0, _("invalid register range"));
a92713e6
RS
3227 return 0;
3228 }
df58fc94 3229
a92713e6
RS
3230 token.u.reg_range.regno1 = regno1;
3231 token.u.reg_range.regno2 = regno2;
3232 mips_add_token (&token, OT_REG_RANGE);
3233 return s;
3234 }
a92713e6 3235
56d438b1
CF
3236 /* Add the register itself. */
3237 token.u.regno = regno1;
3238 mips_add_token (&token, OT_REG);
3239
3240 /* Check for a vector index. */
3241 if (*s == '[')
3242 {
a92713e6
RS
3243 ++s;
3244 SKIP_SPACE_TABS (s);
56d438b1
CF
3245 if (mips_parse_register (&s, &token.u.regno, NULL))
3246 mips_add_token (&token, OT_REG_INDEX);
3247 else
a92713e6 3248 {
56d438b1
CF
3249 expressionS element;
3250
3251 my_getExpression (&element, s);
3252 if (element.X_op != O_constant)
3253 {
3254 set_insn_error (0, _("vector element must be constant"));
3255 return 0;
3256 }
3257 s = expr_end;
3258 token.u.index = element.X_add_number;
3259 mips_add_token (&token, OT_INTEGER_INDEX);
a92713e6 3260 }
a92713e6
RS
3261 SKIP_SPACE_TABS (s);
3262 if (*s != ']')
3263 {
1661c76c 3264 set_insn_error (0, _("missing `]'"));
a92713e6
RS
3265 return 0;
3266 }
3267 ++s;
df58fc94 3268 }
a92713e6 3269 return s;
df58fc94
RS
3270 }
3271
a92713e6
RS
3272 if (float_format)
3273 {
3274 /* First try to treat expressions as floats. */
3275 save_in = input_line_pointer;
3276 input_line_pointer = s;
3277 err = md_atof (float_format, (char *) token.u.flt.data,
3278 &token.u.flt.length);
3279 end = input_line_pointer;
3280 input_line_pointer = save_in;
3281 if (err && *err)
3282 {
e3de51ce 3283 set_insn_error (0, err);
a92713e6
RS
3284 return 0;
3285 }
3286 if (s != end)
3287 {
3288 mips_add_token (&token, OT_FLOAT);
3289 return end;
3290 }
3291 }
3292
3293 /* Treat everything else as an integer expression. */
3294 token.u.integer.relocs[0] = BFD_RELOC_UNUSED;
3295 token.u.integer.relocs[1] = BFD_RELOC_UNUSED;
3296 token.u.integer.relocs[2] = BFD_RELOC_UNUSED;
3297 my_getSmallExpression (&token.u.integer.value, token.u.integer.relocs, s);
3298 s = expr_end;
3299 mips_add_token (&token, OT_INTEGER);
3300 return s;
3301}
3302
3303/* S points to the operand list for an instruction. FLOAT_FORMAT is 'f'
3304 if expressions should be treated as 32-bit floating-point constants,
3305 'd' if they should be treated as 64-bit floating-point constants,
3306 or 0 if they should be treated as integer expressions (the usual case).
3307
3308 Return a list of tokens on success, otherwise return 0. The caller
3309 must obstack_free the list after use. */
3310
3311static struct mips_operand_token *
3312mips_parse_arguments (char *s, char float_format)
3313{
3314 struct mips_operand_token token;
3315
3316 SKIP_SPACE_TABS (s);
3317 while (*s)
3318 {
3319 s = mips_parse_argument_token (s, float_format);
3320 if (!s)
3321 {
3322 obstack_free (&mips_operand_tokens,
3323 obstack_finish (&mips_operand_tokens));
3324 return 0;
3325 }
3326 SKIP_SPACE_TABS (s);
3327 }
3328 mips_add_token (&token, OT_END);
3329 return (struct mips_operand_token *) obstack_finish (&mips_operand_tokens);
df58fc94
RS
3330}
3331
d301a56b
RS
3332/* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
3333 and architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
037b32b9
AN
3334
3335static bfd_boolean
f79e2745 3336is_opcode_valid (const struct mips_opcode *mo)
037b32b9
AN
3337{
3338 int isa = mips_opts.isa;
846ef2d0 3339 int ase = mips_opts.ase;
037b32b9 3340 int fp_s, fp_d;
c6278170 3341 unsigned int i;
037b32b9 3342
be0fcbee 3343 if (ISA_HAS_64BIT_REGS (isa))
c6278170
RS
3344 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3345 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3346 ase |= mips_ases[i].flags64;
037b32b9 3347
d301a56b 3348 if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
037b32b9
AN
3349 return FALSE;
3350
3351 /* Check whether the instruction or macro requires single-precision or
3352 double-precision floating-point support. Note that this information is
3353 stored differently in the opcode table for insns and macros. */
3354 if (mo->pinfo == INSN_MACRO)
3355 {
3356 fp_s = mo->pinfo2 & INSN2_M_FP_S;
3357 fp_d = mo->pinfo2 & INSN2_M_FP_D;
3358 }
3359 else
3360 {
3361 fp_s = mo->pinfo & FP_S;
3362 fp_d = mo->pinfo & FP_D;
3363 }
3364
3365 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
3366 return FALSE;
3367
3368 if (fp_s && mips_opts.soft_float)
3369 return FALSE;
3370
3371 return TRUE;
3372}
3373
3374/* Return TRUE if the MIPS16 opcode MO is valid on the currently
3375 selected ISA and architecture. */
3376
3377static bfd_boolean
3378is_opcode_valid_16 (const struct mips_opcode *mo)
3379{
25499ac7
MR
3380 int isa = mips_opts.isa;
3381 int ase = mips_opts.ase;
3382 unsigned int i;
3383
3384 if (ISA_HAS_64BIT_REGS (isa))
3385 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3386 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3387 ase |= mips_ases[i].flags64;
3388
3389 return opcode_is_member (mo, isa, ase, mips_opts.arch);
037b32b9
AN
3390}
3391
df58fc94 3392/* Return TRUE if the size of the microMIPS opcode MO matches one
7fd53920
MR
3393 explicitly requested. Always TRUE in the standard MIPS mode.
3394 Use is_size_valid_16 for MIPS16 opcodes. */
df58fc94
RS
3395
3396static bfd_boolean
3397is_size_valid (const struct mips_opcode *mo)
3398{
3399 if (!mips_opts.micromips)
3400 return TRUE;
3401
833794fc
MR
3402 if (mips_opts.insn32)
3403 {
3404 if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
3405 return FALSE;
3406 if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
3407 return FALSE;
3408 }
df58fc94
RS
3409 if (!forced_insn_length)
3410 return TRUE;
3411 if (mo->pinfo == INSN_MACRO)
3412 return FALSE;
3413 return forced_insn_length == micromips_insn_length (mo);
3414}
3415
7fd53920
MR
3416/* Return TRUE if the size of the MIPS16 opcode MO matches one
3417 explicitly requested. */
3418
3419static bfd_boolean
3420is_size_valid_16 (const struct mips_opcode *mo)
3421{
3422 if (!forced_insn_length)
3423 return TRUE;
3424 if (mo->pinfo == INSN_MACRO)
3425 return FALSE;
3426 if (forced_insn_length == 2 && mips_opcode_32bit_p (mo))
3427 return FALSE;
0674ee5d
MR
3428 if (forced_insn_length == 4 && (mo->pinfo2 & INSN2_SHORT_ONLY))
3429 return FALSE;
7fd53920
MR
3430 return TRUE;
3431}
3432
df58fc94 3433/* Return TRUE if the microMIPS opcode MO is valid for the delay slot
e64af278
MR
3434 of the preceding instruction. Always TRUE in the standard MIPS mode.
3435
3436 We don't accept macros in 16-bit delay slots to avoid a case where
3437 a macro expansion fails because it relies on a preceding 32-bit real
3438 instruction to have matched and does not handle the operands correctly.
3439 The only macros that may expand to 16-bit instructions are JAL that
3440 cannot be placed in a delay slot anyway, and corner cases of BALIGN
3441 and BGT (that likewise cannot be placed in a delay slot) that decay to
3442 a NOP. In all these cases the macros precede any corresponding real
3443 instruction definitions in the opcode table, so they will match in the
3444 second pass where the size of the delay slot is ignored and therefore
3445 produce correct code. */
df58fc94
RS
3446
3447static bfd_boolean
3448is_delay_slot_valid (const struct mips_opcode *mo)
3449{
3450 if (!mips_opts.micromips)
3451 return TRUE;
3452
3453 if (mo->pinfo == INSN_MACRO)
c06dec14 3454 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
df58fc94
RS
3455 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
3456 && micromips_insn_length (mo) != 4)
3457 return FALSE;
3458 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
3459 && micromips_insn_length (mo) != 2)
3460 return FALSE;
3461
3462 return TRUE;
3463}
3464
fc76e730
RS
3465/* For consistency checking, verify that all bits of OPCODE are specified
3466 either by the match/mask part of the instruction definition, or by the
3467 operand list. Also build up a list of operands in OPERANDS.
3468
3469 INSN_BITS says which bits of the instruction are significant.
3470 If OPCODE is a standard or microMIPS instruction, DECODE_OPERAND
3471 provides the mips_operand description of each operand. DECODE_OPERAND
3472 is null for MIPS16 instructions. */
ab902481
RS
3473
3474static int
3475validate_mips_insn (const struct mips_opcode *opcode,
3476 unsigned long insn_bits,
fc76e730
RS
3477 const struct mips_operand *(*decode_operand) (const char *),
3478 struct mips_operand_array *operands)
ab902481
RS
3479{
3480 const char *s;
fc76e730 3481 unsigned long used_bits, doubled, undefined, opno, mask;
ab902481
RS
3482 const struct mips_operand *operand;
3483
fc76e730
RS
3484 mask = (opcode->pinfo == INSN_MACRO ? 0 : opcode->mask);
3485 if ((mask & opcode->match) != opcode->match)
ab902481
RS
3486 {
3487 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
3488 opcode->name, opcode->args);
3489 return 0;
3490 }
3491 used_bits = 0;
fc76e730 3492 opno = 0;
14daeee3
RS
3493 if (opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX)
3494 used_bits = mips_insert_operand (&mips_vu0_channel_mask, used_bits, -1);
ab902481
RS
3495 for (s = opcode->args; *s; ++s)
3496 switch (*s)
3497 {
3498 case ',':
3499 case '(':
3500 case ')':
3501 break;
3502
14daeee3
RS
3503 case '#':
3504 s++;
3505 break;
3506
ab902481 3507 default:
fc76e730 3508 if (!decode_operand)
7fd53920 3509 operand = decode_mips16_operand (*s, mips_opcode_32bit_p (opcode));
fc76e730
RS
3510 else
3511 operand = decode_operand (s);
3512 if (!operand && opcode->pinfo != INSN_MACRO)
ab902481
RS
3513 {
3514 as_bad (_("internal: unknown operand type: %s %s"),
3515 opcode->name, opcode->args);
3516 return 0;
3517 }
fc76e730
RS
3518 gas_assert (opno < MAX_OPERANDS);
3519 operands->operand[opno] = operand;
25499ac7
MR
3520 if (!decode_operand && operand
3521 && operand->type == OP_INT && operand->lsb == 0
3522 && mips_opcode_32bit_p (opcode))
3523 used_bits |= mips16_immed_extend (-1, operand->size);
3524 else if (operand && operand->type != OP_VU0_MATCH_SUFFIX)
fc76e730 3525 {
14daeee3 3526 used_bits = mips_insert_operand (operand, used_bits, -1);
fc76e730
RS
3527 if (operand->type == OP_MDMX_IMM_REG)
3528 /* Bit 5 is the format selector (OB vs QH). The opcode table
3529 has separate entries for each format. */
3530 used_bits &= ~(1 << (operand->lsb + 5));
3531 if (operand->type == OP_ENTRY_EXIT_LIST)
3532 used_bits &= ~(mask & 0x700);
38bf472a
MR
3533 /* interAptiv MR2 SAVE/RESTORE instructions have a discontiguous
3534 operand field that cannot be fully described with LSB/SIZE. */
3535 if (operand->type == OP_SAVE_RESTORE_LIST && operand->lsb == 6)
3536 used_bits &= ~0x6000;
fc76e730 3537 }
ab902481 3538 /* Skip prefix characters. */
7361da2c 3539 if (decode_operand && (*s == '+' || *s == 'm' || *s == '-'))
ab902481 3540 ++s;
fc76e730 3541 opno += 1;
ab902481
RS
3542 break;
3543 }
fc76e730 3544 doubled = used_bits & mask & insn_bits;
ab902481
RS
3545 if (doubled)
3546 {
3547 as_bad (_("internal: bad mips opcode (bits 0x%08lx doubly defined):"
3548 " %s %s"), doubled, opcode->name, opcode->args);
3549 return 0;
3550 }
fc76e730 3551 used_bits |= mask;
ab902481 3552 undefined = ~used_bits & insn_bits;
fc76e730 3553 if (opcode->pinfo != INSN_MACRO && undefined)
ab902481
RS
3554 {
3555 as_bad (_("internal: bad mips opcode (bits 0x%08lx undefined): %s %s"),
3556 undefined, opcode->name, opcode->args);
3557 return 0;
3558 }
3559 used_bits &= ~insn_bits;
3560 if (used_bits)
3561 {
3562 as_bad (_("internal: bad mips opcode (bits 0x%08lx defined): %s %s"),
3563 used_bits, opcode->name, opcode->args);
3564 return 0;
3565 }
3566 return 1;
3567}
3568
fc76e730
RS
3569/* The MIPS16 version of validate_mips_insn. */
3570
3571static int
3572validate_mips16_insn (const struct mips_opcode *opcode,
3573 struct mips_operand_array *operands)
3574{
7fd53920 3575 unsigned long insn_bits = mips_opcode_32bit_p (opcode) ? 0xffffffff : 0xffff;
fc76e730 3576
7fd53920 3577 return validate_mips_insn (opcode, insn_bits, 0, operands);
fc76e730
RS
3578}
3579
ab902481
RS
3580/* The microMIPS version of validate_mips_insn. */
3581
3582static int
fc76e730
RS
3583validate_micromips_insn (const struct mips_opcode *opc,
3584 struct mips_operand_array *operands)
ab902481
RS
3585{
3586 unsigned long insn_bits;
3587 unsigned long major;
3588 unsigned int length;
3589
fc76e730
RS
3590 if (opc->pinfo == INSN_MACRO)
3591 return validate_mips_insn (opc, 0xffffffff, decode_micromips_operand,
3592 operands);
3593
ab902481
RS
3594 length = micromips_insn_length (opc);
3595 if (length != 2 && length != 4)
3596 {
1661c76c 3597 as_bad (_("internal error: bad microMIPS opcode (incorrect length: %u): "
ab902481
RS
3598 "%s %s"), length, opc->name, opc->args);
3599 return 0;
3600 }
3601 major = opc->match >> (10 + 8 * (length - 2));
3602 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
3603 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
3604 {
1661c76c 3605 as_bad (_("internal error: bad microMIPS opcode "
ab902481
RS
3606 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
3607 return 0;
3608 }
3609
3610 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
3611 insn_bits = 1 << 4 * length;
3612 insn_bits <<= 4 * length;
3613 insn_bits -= 1;
fc76e730
RS
3614 return validate_mips_insn (opc, insn_bits, decode_micromips_operand,
3615 operands);
ab902481
RS
3616}
3617
707bfff6
TS
3618/* This function is called once, at assembler startup time. It should set up
3619 all the tables, etc. that the MD part of the assembler will need. */
156c2f8b 3620
252b5132 3621void
17a2f251 3622md_begin (void)
252b5132 3623{
3994f87e 3624 const char *retval = NULL;
156c2f8b 3625 int i = 0;
252b5132 3626 int broken = 0;
1f25f5d3 3627
0a44bf69
RS
3628 if (mips_pic != NO_PIC)
3629 {
3630 if (g_switch_seen && g_switch_value != 0)
3631 as_bad (_("-G may not be used in position-independent code"));
3632 g_switch_value = 0;
3633 }
00acd688
CM
3634 else if (mips_abicalls)
3635 {
3636 if (g_switch_seen && g_switch_value != 0)
3637 as_bad (_("-G may not be used with abicalls"));
3638 g_switch_value = 0;
3639 }
0a44bf69 3640
0b35dfee 3641 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
1661c76c 3642 as_warn (_("could not set architecture and machine"));
252b5132 3643
252b5132
RH
3644 op_hash = hash_new ();
3645
fc76e730 3646 mips_operands = XCNEWVEC (struct mips_operand_array, NUMOPCODES);
252b5132
RH
3647 for (i = 0; i < NUMOPCODES;)
3648 {
3649 const char *name = mips_opcodes[i].name;
3650
17a2f251 3651 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
3652 if (retval != NULL)
3653 {
3654 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
3655 mips_opcodes[i].name, retval);
3656 /* Probably a memory allocation problem? Give up now. */
1661c76c 3657 as_fatal (_("broken assembler, no assembly attempted"));
252b5132
RH
3658 }
3659 do
3660 {
fc76e730
RS
3661 if (!validate_mips_insn (&mips_opcodes[i], 0xffffffff,
3662 decode_mips_operand, &mips_operands[i]))
3663 broken = 1;
3664 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
252b5132 3665 {
fc76e730
RS
3666 create_insn (&nop_insn, mips_opcodes + i);
3667 if (mips_fix_loongson2f_nop)
3668 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
3669 nop_insn.fixed_p = 1;
252b5132
RH
3670 }
3671 ++i;
3672 }
3673 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
3674 }
3675
3676 mips16_op_hash = hash_new ();
fc76e730
RS
3677 mips16_operands = XCNEWVEC (struct mips_operand_array,
3678 bfd_mips16_num_opcodes);
252b5132
RH
3679
3680 i = 0;
3681 while (i < bfd_mips16_num_opcodes)
3682 {
3683 const char *name = mips16_opcodes[i].name;
3684
17a2f251 3685 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
3686 if (retval != NULL)
3687 as_fatal (_("internal: can't hash `%s': %s"),
3688 mips16_opcodes[i].name, retval);
3689 do
3690 {
fc76e730
RS
3691 if (!validate_mips16_insn (&mips16_opcodes[i], &mips16_operands[i]))
3692 broken = 1;
1e915849
RS
3693 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3694 {
3695 create_insn (&mips16_nop_insn, mips16_opcodes + i);
3696 mips16_nop_insn.fixed_p = 1;
3697 }
252b5132
RH
3698 ++i;
3699 }
3700 while (i < bfd_mips16_num_opcodes
3701 && strcmp (mips16_opcodes[i].name, name) == 0);
3702 }
3703
df58fc94 3704 micromips_op_hash = hash_new ();
fc76e730
RS
3705 micromips_operands = XCNEWVEC (struct mips_operand_array,
3706 bfd_micromips_num_opcodes);
df58fc94
RS
3707
3708 i = 0;
3709 while (i < bfd_micromips_num_opcodes)
3710 {
3711 const char *name = micromips_opcodes[i].name;
3712
3713 retval = hash_insert (micromips_op_hash, name,
3714 (void *) &micromips_opcodes[i]);
3715 if (retval != NULL)
3716 as_fatal (_("internal: can't hash `%s': %s"),
3717 micromips_opcodes[i].name, retval);
3718 do
fc76e730
RS
3719 {
3720 struct mips_cl_insn *micromips_nop_insn;
3721
3722 if (!validate_micromips_insn (&micromips_opcodes[i],
3723 &micromips_operands[i]))
3724 broken = 1;
3725
3726 if (micromips_opcodes[i].pinfo != INSN_MACRO)
3727 {
3728 if (micromips_insn_length (micromips_opcodes + i) == 2)
3729 micromips_nop_insn = &micromips_nop16_insn;
3730 else if (micromips_insn_length (micromips_opcodes + i) == 4)
3731 micromips_nop_insn = &micromips_nop32_insn;
3732 else
3733 continue;
3734
3735 if (micromips_nop_insn->insn_mo == NULL
3736 && strcmp (name, "nop") == 0)
3737 {
3738 create_insn (micromips_nop_insn, micromips_opcodes + i);
3739 micromips_nop_insn->fixed_p = 1;
3740 }
3741 }
3742 }
df58fc94
RS
3743 while (++i < bfd_micromips_num_opcodes
3744 && strcmp (micromips_opcodes[i].name, name) == 0);
3745 }
3746
252b5132 3747 if (broken)
1661c76c 3748 as_fatal (_("broken assembler, no assembly attempted"));
252b5132
RH
3749
3750 /* We add all the general register names to the symbol table. This
3751 helps us detect invalid uses of them. */
3739860c 3752 for (i = 0; reg_names[i].name; i++)
707bfff6 3753 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
8fc4ee9b 3754 reg_names[i].num, /* & RNUM_MASK, */
707bfff6
TS
3755 &zero_address_frag));
3756 if (HAVE_NEWABI)
3739860c 3757 for (i = 0; reg_names_n32n64[i].name; i++)
707bfff6 3758 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
8fc4ee9b 3759 reg_names_n32n64[i].num, /* & RNUM_MASK, */
252b5132 3760 &zero_address_frag));
707bfff6 3761 else
3739860c 3762 for (i = 0; reg_names_o32[i].name; i++)
707bfff6 3763 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
8fc4ee9b 3764 reg_names_o32[i].num, /* & RNUM_MASK, */
6047c971 3765 &zero_address_frag));
6047c971 3766
14daeee3
RS
3767 for (i = 0; i < 32; i++)
3768 {
92fce9bd 3769 char regname[6];
14daeee3
RS
3770
3771 /* R5900 VU0 floating-point register. */
92fce9bd 3772 sprintf (regname, "$vf%d", i);
14daeee3
RS
3773 symbol_table_insert (symbol_new (regname, reg_section,
3774 RTYPE_VF | i, &zero_address_frag));
3775
3776 /* R5900 VU0 integer register. */
92fce9bd 3777 sprintf (regname, "$vi%d", i);
14daeee3
RS
3778 symbol_table_insert (symbol_new (regname, reg_section,
3779 RTYPE_VI | i, &zero_address_frag));
3780
56d438b1 3781 /* MSA register. */
92fce9bd 3782 sprintf (regname, "$w%d", i);
56d438b1
CF
3783 symbol_table_insert (symbol_new (regname, reg_section,
3784 RTYPE_MSA | i, &zero_address_frag));
14daeee3
RS
3785 }
3786
a92713e6
RS
3787 obstack_init (&mips_operand_tokens);
3788
7d10b47d 3789 mips_no_prev_insn ();
252b5132
RH
3790
3791 mips_gprmask = 0;
3792 mips_cprmask[0] = 0;
3793 mips_cprmask[1] = 0;
3794 mips_cprmask[2] = 0;
3795 mips_cprmask[3] = 0;
3796
3797 /* set the default alignment for the text section (2**2) */
3798 record_alignment (text_section, 2);
3799
4d0d148d 3800 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132 3801
f3ded42a
RS
3802 /* On a native system other than VxWorks, sections must be aligned
3803 to 16 byte boundaries. When configured for an embedded ELF
3804 target, we don't bother. */
3805 if (strncmp (TARGET_OS, "elf", 3) != 0
3806 && strncmp (TARGET_OS, "vxworks", 7) != 0)
252b5132 3807 {
f3ded42a
RS
3808 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
3809 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
3810 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
3811 }
252b5132 3812
f3ded42a
RS
3813 /* Create a .reginfo section for register masks and a .mdebug
3814 section for debugging information. */
3815 {
3816 segT seg;
3817 subsegT subseg;
3818 flagword flags;
3819 segT sec;
3820
3821 seg = now_seg;
3822 subseg = now_subseg;
3823
3824 /* The ABI says this section should be loaded so that the
3825 running program can access it. However, we don't load it
3826 if we are configured for an embedded target */
3827 flags = SEC_READONLY | SEC_DATA;
3828 if (strncmp (TARGET_OS, "elf", 3) != 0)
3829 flags |= SEC_ALLOC | SEC_LOAD;
3830
3831 if (mips_abi != N64_ABI)
252b5132 3832 {
f3ded42a 3833 sec = subseg_new (".reginfo", (subsegT) 0);
bdaaa2e1 3834
f3ded42a
RS
3835 bfd_set_section_flags (stdoutput, sec, flags);
3836 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
252b5132 3837
f3ded42a
RS
3838 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
3839 }
3840 else
3841 {
3842 /* The 64-bit ABI uses a .MIPS.options section rather than
3843 .reginfo section. */
3844 sec = subseg_new (".MIPS.options", (subsegT) 0);
3845 bfd_set_section_flags (stdoutput, sec, flags);
3846 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132 3847
f3ded42a
RS
3848 /* Set up the option header. */
3849 {
3850 Elf_Internal_Options opthdr;
3851 char *f;
3852
3853 opthdr.kind = ODK_REGINFO;
3854 opthdr.size = (sizeof (Elf_External_Options)
3855 + sizeof (Elf64_External_RegInfo));
3856 opthdr.section = 0;
3857 opthdr.info = 0;
3858 f = frag_more (sizeof (Elf_External_Options));
3859 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
3860 (Elf_External_Options *) f);
3861
3862 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
3863 }
3864 }
252b5132 3865
351cdf24
MF
3866 sec = subseg_new (".MIPS.abiflags", (subsegT) 0);
3867 bfd_set_section_flags (stdoutput, sec,
3868 SEC_READONLY | SEC_DATA | SEC_ALLOC | SEC_LOAD);
3869 bfd_set_section_alignment (stdoutput, sec, 3);
3870 mips_flags_frag = frag_more (sizeof (Elf_External_ABIFlags_v0));
3871
f3ded42a
RS
3872 if (ECOFF_DEBUGGING)
3873 {
3874 sec = subseg_new (".mdebug", (subsegT) 0);
3875 (void) bfd_set_section_flags (stdoutput, sec,
3876 SEC_HAS_CONTENTS | SEC_READONLY);
3877 (void) bfd_set_section_alignment (stdoutput, sec, 2);
252b5132 3878 }
f3ded42a
RS
3879 else if (mips_flag_pdr)
3880 {
3881 pdr_seg = subseg_new (".pdr", (subsegT) 0);
3882 (void) bfd_set_section_flags (stdoutput, pdr_seg,
3883 SEC_READONLY | SEC_RELOC
3884 | SEC_DEBUGGING);
3885 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
3886 }
3887
3888 subseg_set (seg, subseg);
3889 }
252b5132 3890
71400594
RS
3891 if (mips_fix_vr4120)
3892 init_vr4120_conflicts ();
252b5132
RH
3893}
3894
351cdf24
MF
3895static inline void
3896fpabi_incompatible_with (int fpabi, const char *what)
3897{
3898 as_warn (_(".gnu_attribute %d,%d is incompatible with `%s'"),
3899 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3900}
3901
3902static inline void
3903fpabi_requires (int fpabi, const char *what)
3904{
3905 as_warn (_(".gnu_attribute %d,%d requires `%s'"),
3906 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3907}
3908
3909/* Check -mabi and register sizes against the specified FP ABI. */
3910static void
3911check_fpabi (int fpabi)
3912{
351cdf24
MF
3913 switch (fpabi)
3914 {
3915 case Val_GNU_MIPS_ABI_FP_DOUBLE:
ea79f94a
MF
3916 if (file_mips_opts.soft_float)
3917 fpabi_incompatible_with (fpabi, "softfloat");
3918 else if (file_mips_opts.single_float)
3919 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3920 if (file_mips_opts.gp == 64 && file_mips_opts.fp == 32)
3921 fpabi_incompatible_with (fpabi, "gp=64 fp=32");
3922 else if (file_mips_opts.gp == 32 && file_mips_opts.fp == 64)
3923 fpabi_incompatible_with (fpabi, "gp=32 fp=64");
351cdf24
MF
3924 break;
3925
3926 case Val_GNU_MIPS_ABI_FP_XX:
3927 if (mips_abi != O32_ABI)
3928 fpabi_requires (fpabi, "-mabi=32");
ea79f94a
MF
3929 else if (file_mips_opts.soft_float)
3930 fpabi_incompatible_with (fpabi, "softfloat");
3931 else if (file_mips_opts.single_float)
3932 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3933 else if (file_mips_opts.fp != 0)
3934 fpabi_requires (fpabi, "fp=xx");
351cdf24
MF
3935 break;
3936
3937 case Val_GNU_MIPS_ABI_FP_64A:
3938 case Val_GNU_MIPS_ABI_FP_64:
3939 if (mips_abi != O32_ABI)
3940 fpabi_requires (fpabi, "-mabi=32");
ea79f94a
MF
3941 else if (file_mips_opts.soft_float)
3942 fpabi_incompatible_with (fpabi, "softfloat");
3943 else if (file_mips_opts.single_float)
3944 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3945 else if (file_mips_opts.fp != 64)
3946 fpabi_requires (fpabi, "fp=64");
3947 else if (fpabi == Val_GNU_MIPS_ABI_FP_64 && !file_mips_opts.oddspreg)
3948 fpabi_incompatible_with (fpabi, "nooddspreg");
3949 else if (fpabi == Val_GNU_MIPS_ABI_FP_64A && file_mips_opts.oddspreg)
3950 fpabi_requires (fpabi, "nooddspreg");
351cdf24
MF
3951 break;
3952
3953 case Val_GNU_MIPS_ABI_FP_SINGLE:
3954 if (file_mips_opts.soft_float)
3955 fpabi_incompatible_with (fpabi, "softfloat");
3956 else if (!file_mips_opts.single_float)
3957 fpabi_requires (fpabi, "singlefloat");
3958 break;
3959
3960 case Val_GNU_MIPS_ABI_FP_SOFT:
3961 if (!file_mips_opts.soft_float)
3962 fpabi_requires (fpabi, "softfloat");
3963 break;
3964
3965 case Val_GNU_MIPS_ABI_FP_OLD_64:
3966 as_warn (_(".gnu_attribute %d,%d is no longer supported"),
3967 Tag_GNU_MIPS_ABI_FP, fpabi);
3968 break;
3969
3350cc01
CM
3970 case Val_GNU_MIPS_ABI_FP_NAN2008:
3971 /* Silently ignore compatibility value. */
3972 break;
3973
351cdf24
MF
3974 default:
3975 as_warn (_(".gnu_attribute %d,%d is not a recognized"
3976 " floating-point ABI"), Tag_GNU_MIPS_ABI_FP, fpabi);
3977 break;
3978 }
351cdf24
MF
3979}
3980
919731af 3981/* Perform consistency checks on the current options. */
3982
3983static void
3984mips_check_options (struct mips_set_options *opts, bfd_boolean abi_checks)
3985{
3986 /* Check the size of integer registers agrees with the ABI and ISA. */
3987 if (opts->gp == 64 && !ISA_HAS_64BIT_REGS (opts->isa))
3988 as_bad (_("`gp=64' used with a 32-bit processor"));
3989 else if (abi_checks
3990 && opts->gp == 32 && ABI_NEEDS_64BIT_REGS (mips_abi))
3991 as_bad (_("`gp=32' used with a 64-bit ABI"));
3992 else if (abi_checks
3993 && opts->gp == 64 && ABI_NEEDS_32BIT_REGS (mips_abi))
3994 as_bad (_("`gp=64' used with a 32-bit ABI"));
3995
3996 /* Check the size of the float registers agrees with the ABI and ISA. */
3997 switch (opts->fp)
3998 {
351cdf24
MF
3999 case 0:
4000 if (!CPU_HAS_LDC1_SDC1 (opts->arch))
4001 as_bad (_("`fp=xx' used with a cpu lacking ldc1/sdc1 instructions"));
4002 else if (opts->single_float == 1)
4003 as_bad (_("`fp=xx' cannot be used with `singlefloat'"));
4004 break;
919731af 4005 case 64:
4006 if (!ISA_HAS_64BIT_FPRS (opts->isa))
4007 as_bad (_("`fp=64' used with a 32-bit fpu"));
4008 else if (abi_checks
4009 && ABI_NEEDS_32BIT_REGS (mips_abi)
4010 && !ISA_HAS_MXHC1 (opts->isa))
4011 as_warn (_("`fp=64' used with a 32-bit ABI"));
4012 break;
4013 case 32:
4014 if (abi_checks
4015 && ABI_NEEDS_64BIT_REGS (mips_abi))
4016 as_warn (_("`fp=32' used with a 64-bit ABI"));
5f4678bb 4017 if (ISA_IS_R6 (opts->isa) && opts->single_float == 0)
7361da2c 4018 as_bad (_("`fp=32' used with a MIPS R6 cpu"));
919731af 4019 break;
4020 default:
4021 as_bad (_("Unknown size of floating point registers"));
4022 break;
4023 }
4024
351cdf24
MF
4025 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !opts->oddspreg)
4026 as_bad (_("`nooddspreg` cannot be used with a 64-bit ABI"));
4027
919731af 4028 if (opts->micromips == 1 && opts->mips16 == 1)
1357373c 4029 as_bad (_("`%s' cannot be used with `%s'"), "mips16", "micromips");
5f4678bb 4030 else if (ISA_IS_R6 (opts->isa)
7361da2c
AB
4031 && (opts->micromips == 1
4032 || opts->mips16 == 1))
1357373c 4033 as_fatal (_("`%s' cannot be used with `%s'"),
7361da2c 4034 opts->micromips ? "micromips" : "mips16",
5f4678bb 4035 mips_cpu_info_from_isa (opts->isa)->name);
7361da2c
AB
4036
4037 if (ISA_IS_R6 (opts->isa) && mips_relax_branch)
4038 as_fatal (_("branch relaxation is not supported in `%s'"),
4039 mips_cpu_info_from_isa (opts->isa)->name);
919731af 4040}
4041
4042/* Perform consistency checks on the module level options exactly once.
4043 This is a deferred check that happens:
4044 at the first .set directive
4045 or, at the first pseudo op that generates code (inc .dc.a)
4046 or, at the first instruction
4047 or, at the end. */
4048
4049static void
4050file_mips_check_options (void)
4051{
4052 const struct mips_cpu_info *arch_info = 0;
4053
4054 if (file_mips_opts_checked)
4055 return;
4056
4057 /* The following code determines the register size.
4058 Similar code was added to GCC 3.3 (see override_options() in
4059 config/mips/mips.c). The GAS and GCC code should be kept in sync
4060 as much as possible. */
4061
4062 if (file_mips_opts.gp < 0)
4063 {
4064 /* Infer the integer register size from the ABI and processor.
4065 Restrict ourselves to 32-bit registers if that's all the
4066 processor has, or if the ABI cannot handle 64-bit registers. */
4067 file_mips_opts.gp = (ABI_NEEDS_32BIT_REGS (mips_abi)
4068 || !ISA_HAS_64BIT_REGS (file_mips_opts.isa))
4069 ? 32 : 64;
4070 }
4071
4072 if (file_mips_opts.fp < 0)
4073 {
4074 /* No user specified float register size.
4075 ??? GAS treats single-float processors as though they had 64-bit
4076 float registers (although it complains when double-precision
4077 instructions are used). As things stand, saying they have 32-bit
4078 registers would lead to spurious "register must be even" messages.
4079 So here we assume float registers are never smaller than the
4080 integer ones. */
4081 if (file_mips_opts.gp == 64)
4082 /* 64-bit integer registers implies 64-bit float registers. */
4083 file_mips_opts.fp = 64;
4084 else if ((file_mips_opts.ase & FP64_ASES)
4085 && ISA_HAS_64BIT_FPRS (file_mips_opts.isa))
4086 /* Handle ASEs that require 64-bit float registers, if possible. */
4087 file_mips_opts.fp = 64;
7361da2c
AB
4088 else if (ISA_IS_R6 (mips_opts.isa))
4089 /* R6 implies 64-bit float registers. */
4090 file_mips_opts.fp = 64;
919731af 4091 else
4092 /* 32-bit float registers. */
4093 file_mips_opts.fp = 32;
4094 }
4095
4096 arch_info = mips_cpu_info_from_arch (file_mips_opts.arch);
4097
351cdf24
MF
4098 /* Disable operations on odd-numbered floating-point registers by default
4099 when using the FPXX ABI. */
4100 if (file_mips_opts.oddspreg < 0)
4101 {
4102 if (file_mips_opts.fp == 0)
4103 file_mips_opts.oddspreg = 0;
4104 else
4105 file_mips_opts.oddspreg = 1;
4106 }
4107
919731af 4108 /* End of GCC-shared inference code. */
4109
4110 /* This flag is set when we have a 64-bit capable CPU but use only
4111 32-bit wide registers. Note that EABI does not use it. */
4112 if (ISA_HAS_64BIT_REGS (file_mips_opts.isa)
4113 && ((mips_abi == NO_ABI && file_mips_opts.gp == 32)
4114 || mips_abi == O32_ABI))
4115 mips_32bitmode = 1;
4116
4117 if (file_mips_opts.isa == ISA_MIPS1 && mips_trap)
4118 as_bad (_("trap exception not supported at ISA 1"));
4119
4120 /* If the selected architecture includes support for ASEs, enable
4121 generation of code for them. */
4122 if (file_mips_opts.mips16 == -1)
4123 file_mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_opts.arch)) ? 1 : 0;
4124 if (file_mips_opts.micromips == -1)
4125 file_mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_opts.arch))
4126 ? 1 : 0;
4127
7361da2c
AB
4128 if (mips_nan2008 == -1)
4129 mips_nan2008 = (ISA_HAS_LEGACY_NAN (file_mips_opts.isa)) ? 0 : 1;
4130 else if (!ISA_HAS_LEGACY_NAN (file_mips_opts.isa) && mips_nan2008 == 0)
4131 as_fatal (_("`%s' does not support legacy NaN"),
4132 mips_cpu_info_from_arch (file_mips_opts.arch)->name);
4133
919731af 4134 /* Some ASEs require 64-bit FPRs, so -mfp32 should stop those ASEs from
4135 being selected implicitly. */
4136 if (file_mips_opts.fp != 64)
4137 file_ase_explicit |= ASE_MIPS3D | ASE_MDMX | ASE_MSA;
4138
4139 /* If the user didn't explicitly select or deselect a particular ASE,
4140 use the default setting for the CPU. */
4141 file_mips_opts.ase |= (arch_info->ase & ~file_ase_explicit);
4142
4143 /* Set up the current options. These may change throughout assembly. */
4144 mips_opts = file_mips_opts;
4145
4146 mips_check_isa_supports_ases ();
4147 mips_check_options (&file_mips_opts, TRUE);
4148 file_mips_opts_checked = TRUE;
4149
4150 if (!bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
4151 as_warn (_("could not set architecture and machine"));
4152}
4153
252b5132 4154void
17a2f251 4155md_assemble (char *str)
252b5132
RH
4156{
4157 struct mips_cl_insn insn;
f6688943
TS
4158 bfd_reloc_code_real_type unused_reloc[3]
4159 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 4160
919731af 4161 file_mips_check_options ();
4162
252b5132 4163 imm_expr.X_op = O_absent;
252b5132 4164 offset_expr.X_op = O_absent;
f6688943
TS
4165 offset_reloc[0] = BFD_RELOC_UNUSED;
4166 offset_reloc[1] = BFD_RELOC_UNUSED;
4167 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132 4168
e1b47bd5
RS
4169 mips_mark_labels ();
4170 mips_assembling_insn = TRUE;
e3de51ce 4171 clear_insn_error ();
e1b47bd5 4172
252b5132
RH
4173 if (mips_opts.mips16)
4174 mips16_ip (str, &insn);
4175 else
4176 {
4177 mips_ip (str, &insn);
beae10d5
KH
4178 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
4179 str, insn.insn_opcode));
252b5132
RH
4180 }
4181
e3de51ce
RS
4182 if (insn_error.msg)
4183 report_insn_error (str);
e1b47bd5 4184 else if (insn.insn_mo->pinfo == INSN_MACRO)
252b5132 4185 {
584892a6 4186 macro_start ();
252b5132
RH
4187 if (mips_opts.mips16)
4188 mips16_macro (&insn);
4189 else
833794fc 4190 macro (&insn, str);
584892a6 4191 macro_end ();
252b5132
RH
4192 }
4193 else
4194 {
77bd4346 4195 if (offset_expr.X_op != O_absent)
df58fc94 4196 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
252b5132 4197 else
df58fc94 4198 append_insn (&insn, NULL, unused_reloc, FALSE);
252b5132 4199 }
e1b47bd5
RS
4200
4201 mips_assembling_insn = FALSE;
252b5132
RH
4202}
4203
738e5348
RS
4204/* Convenience functions for abstracting away the differences between
4205 MIPS16 and non-MIPS16 relocations. */
4206
4207static inline bfd_boolean
4208mips16_reloc_p (bfd_reloc_code_real_type reloc)
4209{
4210 switch (reloc)
4211 {
4212 case BFD_RELOC_MIPS16_JMP:
4213 case BFD_RELOC_MIPS16_GPREL:
4214 case BFD_RELOC_MIPS16_GOT16:
4215 case BFD_RELOC_MIPS16_CALL16:
4216 case BFD_RELOC_MIPS16_HI16_S:
4217 case BFD_RELOC_MIPS16_HI16:
4218 case BFD_RELOC_MIPS16_LO16:
c9775dde 4219 case BFD_RELOC_MIPS16_16_PCREL_S1:
738e5348
RS
4220 return TRUE;
4221
4222 default:
4223 return FALSE;
4224 }
4225}
4226
df58fc94
RS
4227static inline bfd_boolean
4228micromips_reloc_p (bfd_reloc_code_real_type reloc)
4229{
4230 switch (reloc)
4231 {
4232 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4233 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4234 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4235 case BFD_RELOC_MICROMIPS_GPREL16:
4236 case BFD_RELOC_MICROMIPS_JMP:
4237 case BFD_RELOC_MICROMIPS_HI16:
4238 case BFD_RELOC_MICROMIPS_HI16_S:
4239 case BFD_RELOC_MICROMIPS_LO16:
4240 case BFD_RELOC_MICROMIPS_LITERAL:
4241 case BFD_RELOC_MICROMIPS_GOT16:
4242 case BFD_RELOC_MICROMIPS_CALL16:
4243 case BFD_RELOC_MICROMIPS_GOT_HI16:
4244 case BFD_RELOC_MICROMIPS_GOT_LO16:
4245 case BFD_RELOC_MICROMIPS_CALL_HI16:
4246 case BFD_RELOC_MICROMIPS_CALL_LO16:
4247 case BFD_RELOC_MICROMIPS_SUB:
4248 case BFD_RELOC_MICROMIPS_GOT_PAGE:
4249 case BFD_RELOC_MICROMIPS_GOT_OFST:
4250 case BFD_RELOC_MICROMIPS_GOT_DISP:
4251 case BFD_RELOC_MICROMIPS_HIGHEST:
4252 case BFD_RELOC_MICROMIPS_HIGHER:
4253 case BFD_RELOC_MICROMIPS_SCN_DISP:
4254 case BFD_RELOC_MICROMIPS_JALR:
4255 return TRUE;
4256
4257 default:
4258 return FALSE;
4259 }
4260}
4261
2309ddf2
MR
4262static inline bfd_boolean
4263jmp_reloc_p (bfd_reloc_code_real_type reloc)
4264{
4265 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
4266}
4267
0e9c5a5c
MR
4268static inline bfd_boolean
4269b_reloc_p (bfd_reloc_code_real_type reloc)
4270{
4271 return (reloc == BFD_RELOC_MIPS_26_PCREL_S2
4272 || reloc == BFD_RELOC_MIPS_21_PCREL_S2
4273 || reloc == BFD_RELOC_16_PCREL_S2
c9775dde 4274 || reloc == BFD_RELOC_MIPS16_16_PCREL_S1
0e9c5a5c
MR
4275 || reloc == BFD_RELOC_MICROMIPS_16_PCREL_S1
4276 || reloc == BFD_RELOC_MICROMIPS_10_PCREL_S1
4277 || reloc == BFD_RELOC_MICROMIPS_7_PCREL_S1);
4278}
4279
738e5348
RS
4280static inline bfd_boolean
4281got16_reloc_p (bfd_reloc_code_real_type reloc)
4282{
2309ddf2 4283 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
df58fc94 4284 || reloc == BFD_RELOC_MICROMIPS_GOT16);
738e5348
RS
4285}
4286
4287static inline bfd_boolean
4288hi16_reloc_p (bfd_reloc_code_real_type reloc)
4289{
2309ddf2 4290 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
df58fc94 4291 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
738e5348
RS
4292}
4293
4294static inline bfd_boolean
4295lo16_reloc_p (bfd_reloc_code_real_type reloc)
4296{
2309ddf2 4297 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
df58fc94
RS
4298 || reloc == BFD_RELOC_MICROMIPS_LO16);
4299}
4300
df58fc94
RS
4301static inline bfd_boolean
4302jalr_reloc_p (bfd_reloc_code_real_type reloc)
4303{
2309ddf2 4304 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
738e5348
RS
4305}
4306
f2ae14a1
RS
4307static inline bfd_boolean
4308gprel16_reloc_p (bfd_reloc_code_real_type reloc)
4309{
4310 return (reloc == BFD_RELOC_GPREL16 || reloc == BFD_RELOC_MIPS16_GPREL
4311 || reloc == BFD_RELOC_MICROMIPS_GPREL16);
4312}
4313
2de39019
CM
4314/* Return true if RELOC is a PC-relative relocation that does not have
4315 full address range. */
4316
4317static inline bfd_boolean
4318limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
4319{
4320 switch (reloc)
4321 {
4322 case BFD_RELOC_16_PCREL_S2:
c9775dde 4323 case BFD_RELOC_MIPS16_16_PCREL_S1:
2de39019
CM
4324 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4325 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4326 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
7361da2c
AB
4327 case BFD_RELOC_MIPS_21_PCREL_S2:
4328 case BFD_RELOC_MIPS_26_PCREL_S2:
4329 case BFD_RELOC_MIPS_18_PCREL_S3:
4330 case BFD_RELOC_MIPS_19_PCREL_S2:
2de39019
CM
4331 return TRUE;
4332
b47468a6 4333 case BFD_RELOC_32_PCREL:
7361da2c
AB
4334 case BFD_RELOC_HI16_S_PCREL:
4335 case BFD_RELOC_LO16_PCREL:
b47468a6
CM
4336 return HAVE_64BIT_ADDRESSES;
4337
2de39019
CM
4338 default:
4339 return FALSE;
4340 }
4341}
b47468a6 4342
5919d012 4343/* Return true if the given relocation might need a matching %lo().
0a44bf69
RS
4344 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
4345 need a matching %lo() when applied to local symbols. */
5919d012
RS
4346
4347static inline bfd_boolean
17a2f251 4348reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 4349{
3b91255e 4350 return (HAVE_IN_PLACE_ADDENDS
738e5348 4351 && (hi16_reloc_p (reloc)
0a44bf69
RS
4352 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
4353 all GOT16 relocations evaluate to "G". */
738e5348
RS
4354 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
4355}
4356
4357/* Return the type of %lo() reloc needed by RELOC, given that
4358 reloc_needs_lo_p. */
4359
4360static inline bfd_reloc_code_real_type
4361matching_lo_reloc (bfd_reloc_code_real_type reloc)
4362{
df58fc94
RS
4363 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
4364 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
4365 : BFD_RELOC_LO16));
5919d012
RS
4366}
4367
4368/* Return true if the given fixup is followed by a matching R_MIPS_LO16
4369 relocation. */
4370
4371static inline bfd_boolean
17a2f251 4372fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
4373{
4374 return (fixp->fx_next != NULL
738e5348 4375 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
5919d012
RS
4376 && fixp->fx_addsy == fixp->fx_next->fx_addsy
4377 && fixp->fx_offset == fixp->fx_next->fx_offset);
4378}
4379
462427c4
RS
4380/* Move all labels in LABELS to the current insertion point. TEXT_P
4381 says whether the labels refer to text or data. */
404a8071
RS
4382
4383static void
462427c4 4384mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
404a8071
RS
4385{
4386 struct insn_label_list *l;
4387 valueT val;
4388
462427c4 4389 for (l = labels; l != NULL; l = l->next)
404a8071 4390 {
9c2799c2 4391 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
404a8071
RS
4392 symbol_set_frag (l->label, frag_now);
4393 val = (valueT) frag_now_fix ();
df58fc94 4394 /* MIPS16/microMIPS text labels are stored as odd. */
462427c4 4395 if (text_p && HAVE_CODE_COMPRESSION)
404a8071
RS
4396 ++val;
4397 S_SET_VALUE (l->label, val);
4398 }
4399}
4400
462427c4
RS
4401/* Move all labels in insn_labels to the current insertion point
4402 and treat them as text labels. */
4403
4404static void
4405mips_move_text_labels (void)
4406{
4407 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
4408}
4409
9e009953
MR
4410/* Duplicate the test for LINK_ONCE sections as in `adjust_reloc_syms'. */
4411
5f0fe04b
TS
4412static bfd_boolean
4413s_is_linkonce (symbolS *sym, segT from_seg)
4414{
4415 bfd_boolean linkonce = FALSE;
4416 segT symseg = S_GET_SEGMENT (sym);
4417
4418 if (symseg != from_seg && !S_IS_LOCAL (sym))
4419 {
4420 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
4421 linkonce = TRUE;
5f0fe04b
TS
4422 /* The GNU toolchain uses an extension for ELF: a section
4423 beginning with the magic string .gnu.linkonce is a
4424 linkonce section. */
4425 if (strncmp (segment_name (symseg), ".gnu.linkonce",
4426 sizeof ".gnu.linkonce" - 1) == 0)
4427 linkonce = TRUE;
5f0fe04b
TS
4428 }
4429 return linkonce;
4430}
4431
e1b47bd5 4432/* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
df58fc94
RS
4433 linker to handle them specially, such as generating jalx instructions
4434 when needed. We also make them odd for the duration of the assembly,
4435 in order to generate the right sort of code. We will make them even
252b5132
RH
4436 in the adjust_symtab routine, while leaving them marked. This is
4437 convenient for the debugger and the disassembler. The linker knows
4438 to make them odd again. */
4439
4440static void
e1b47bd5 4441mips_compressed_mark_label (symbolS *label)
252b5132 4442{
df58fc94 4443 gas_assert (HAVE_CODE_COMPRESSION);
a8dbcb85 4444
f3ded42a
RS
4445 if (mips_opts.mips16)
4446 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
4447 else
4448 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
e1b47bd5
RS
4449 if ((S_GET_VALUE (label) & 1) == 0
4450 /* Don't adjust the address if the label is global or weak, or
4451 in a link-once section, since we'll be emitting symbol reloc
4452 references to it which will be patched up by the linker, and
4453 the final value of the symbol may or may not be MIPS16/microMIPS. */
4454 && !S_IS_WEAK (label)
4455 && !S_IS_EXTERNAL (label)
4456 && !s_is_linkonce (label, now_seg))
4457 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
4458}
4459
4460/* Mark preceding MIPS16 or microMIPS instruction labels. */
4461
4462static void
4463mips_compressed_mark_labels (void)
4464{
4465 struct insn_label_list *l;
4466
4467 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
4468 mips_compressed_mark_label (l->label);
252b5132
RH
4469}
4470
4d7206a2
RS
4471/* End the current frag. Make it a variant frag and record the
4472 relaxation info. */
4473
4474static void
4475relax_close_frag (void)
4476{
584892a6 4477 mips_macro_warning.first_frag = frag_now;
4d7206a2 4478 frag_var (rs_machine_dependent, 0, 0,
ce8ad872
MR
4479 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1],
4480 mips_pic != NO_PIC),
4d7206a2
RS
4481 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
4482
4483 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
4484 mips_relax.first_fixup = 0;
4485}
4486
4487/* Start a new relaxation sequence whose expansion depends on SYMBOL.
4488 See the comment above RELAX_ENCODE for more details. */
4489
4490static void
4491relax_start (symbolS *symbol)
4492{
9c2799c2 4493 gas_assert (mips_relax.sequence == 0);
4d7206a2
RS
4494 mips_relax.sequence = 1;
4495 mips_relax.symbol = symbol;
4496}
4497
4498/* Start generating the second version of a relaxable sequence.
4499 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
4500
4501static void
4d7206a2
RS
4502relax_switch (void)
4503{
9c2799c2 4504 gas_assert (mips_relax.sequence == 1);
4d7206a2
RS
4505 mips_relax.sequence = 2;
4506}
4507
4508/* End the current relaxable sequence. */
4509
4510static void
4511relax_end (void)
4512{
9c2799c2 4513 gas_assert (mips_relax.sequence == 2);
4d7206a2
RS
4514 relax_close_frag ();
4515 mips_relax.sequence = 0;
4516}
4517
11625dd8
RS
4518/* Return true if IP is a delayed branch or jump. */
4519
4520static inline bfd_boolean
4521delayed_branch_p (const struct mips_cl_insn *ip)
4522{
4523 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
4524 | INSN_COND_BRANCH_DELAY
4525 | INSN_COND_BRANCH_LIKELY)) != 0;
4526}
4527
4528/* Return true if IP is a compact branch or jump. */
4529
4530static inline bfd_boolean
4531compact_branch_p (const struct mips_cl_insn *ip)
4532{
26545944
RS
4533 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
4534 | INSN2_COND_BRANCH)) != 0;
11625dd8
RS
4535}
4536
4537/* Return true if IP is an unconditional branch or jump. */
4538
4539static inline bfd_boolean
4540uncond_branch_p (const struct mips_cl_insn *ip)
4541{
4542 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
26545944 4543 || (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0);
11625dd8
RS
4544}
4545
4546/* Return true if IP is a branch-likely instruction. */
4547
4548static inline bfd_boolean
4549branch_likely_p (const struct mips_cl_insn *ip)
4550{
4551 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
4552}
4553
14fe068b
RS
4554/* Return the type of nop that should be used to fill the delay slot
4555 of delayed branch IP. */
4556
4557static struct mips_cl_insn *
4558get_delay_slot_nop (const struct mips_cl_insn *ip)
4559{
4560 if (mips_opts.micromips
4561 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
4562 return &micromips_nop32_insn;
4563 return NOP_INSN;
4564}
4565
fc76e730
RS
4566/* Return a mask that has bit N set if OPCODE reads the register(s)
4567 in operand N. */
df58fc94
RS
4568
4569static unsigned int
fc76e730 4570insn_read_mask (const struct mips_opcode *opcode)
df58fc94 4571{
fc76e730
RS
4572 return (opcode->pinfo & INSN_READ_ALL) >> INSN_READ_SHIFT;
4573}
df58fc94 4574
fc76e730
RS
4575/* Return a mask that has bit N set if OPCODE writes to the register(s)
4576 in operand N. */
4577
4578static unsigned int
4579insn_write_mask (const struct mips_opcode *opcode)
4580{
4581 return (opcode->pinfo & INSN_WRITE_ALL) >> INSN_WRITE_SHIFT;
4582}
4583
4584/* Return a mask of the registers specified by operand OPERAND of INSN.
4585 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4586 is set. */
4587
4588static unsigned int
4589operand_reg_mask (const struct mips_cl_insn *insn,
4590 const struct mips_operand *operand,
4591 unsigned int type_mask)
4592{
4593 unsigned int uval, vsel;
4594
4595 switch (operand->type)
df58fc94 4596 {
fc76e730
RS
4597 case OP_INT:
4598 case OP_MAPPED_INT:
4599 case OP_MSB:
4600 case OP_PCREL:
4601 case OP_PERF_REG:
4602 case OP_ADDIUSP_INT:
4603 case OP_ENTRY_EXIT_LIST:
4604 case OP_REPEAT_DEST_REG:
4605 case OP_REPEAT_PREV_REG:
4606 case OP_PC:
14daeee3
RS
4607 case OP_VU0_SUFFIX:
4608 case OP_VU0_MATCH_SUFFIX:
56d438b1 4609 case OP_IMM_INDEX:
fc76e730
RS
4610 abort ();
4611
25499ac7
MR
4612 case OP_REG28:
4613 return 1 << 28;
4614
fc76e730 4615 case OP_REG:
0f35dbc4 4616 case OP_OPTIONAL_REG:
fc76e730
RS
4617 {
4618 const struct mips_reg_operand *reg_op;
4619
4620 reg_op = (const struct mips_reg_operand *) operand;
4621 if (!(type_mask & (1 << reg_op->reg_type)))
4622 return 0;
4623 uval = insn_extract_operand (insn, operand);
4624 return 1 << mips_decode_reg_operand (reg_op, uval);
4625 }
4626
4627 case OP_REG_PAIR:
4628 {
4629 const struct mips_reg_pair_operand *pair_op;
4630
4631 pair_op = (const struct mips_reg_pair_operand *) operand;
4632 if (!(type_mask & (1 << pair_op->reg_type)))
4633 return 0;
4634 uval = insn_extract_operand (insn, operand);
4635 return (1 << pair_op->reg1_map[uval]) | (1 << pair_op->reg2_map[uval]);
4636 }
4637
4638 case OP_CLO_CLZ_DEST:
4639 if (!(type_mask & (1 << OP_REG_GP)))
4640 return 0;
4641 uval = insn_extract_operand (insn, operand);
4642 return (1 << (uval & 31)) | (1 << (uval >> 5));
4643
7361da2c
AB
4644 case OP_SAME_RS_RT:
4645 if (!(type_mask & (1 << OP_REG_GP)))
4646 return 0;
4647 uval = insn_extract_operand (insn, operand);
4648 gas_assert ((uval & 31) == (uval >> 5));
4649 return 1 << (uval & 31);
4650
4651 case OP_CHECK_PREV:
4652 case OP_NON_ZERO_REG:
4653 if (!(type_mask & (1 << OP_REG_GP)))
4654 return 0;
4655 uval = insn_extract_operand (insn, operand);
4656 return 1 << (uval & 31);
4657
fc76e730
RS
4658 case OP_LWM_SWM_LIST:
4659 abort ();
4660
4661 case OP_SAVE_RESTORE_LIST:
4662 abort ();
4663
4664 case OP_MDMX_IMM_REG:
4665 if (!(type_mask & (1 << OP_REG_VEC)))
4666 return 0;
4667 uval = insn_extract_operand (insn, operand);
4668 vsel = uval >> 5;
4669 if ((vsel & 0x18) == 0x18)
4670 return 0;
4671 return 1 << (uval & 31);
56d438b1
CF
4672
4673 case OP_REG_INDEX:
4674 if (!(type_mask & (1 << OP_REG_GP)))
4675 return 0;
4676 return 1 << insn_extract_operand (insn, operand);
df58fc94 4677 }
fc76e730
RS
4678 abort ();
4679}
4680
4681/* Return a mask of the registers specified by operands OPNO_MASK of INSN,
4682 where bit N of OPNO_MASK is set if operand N should be included.
4683 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4684 is set. */
4685
4686static unsigned int
4687insn_reg_mask (const struct mips_cl_insn *insn,
4688 unsigned int type_mask, unsigned int opno_mask)
4689{
4690 unsigned int opno, reg_mask;
4691
4692 opno = 0;
4693 reg_mask = 0;
4694 while (opno_mask != 0)
4695 {
4696 if (opno_mask & 1)
4697 reg_mask |= operand_reg_mask (insn, insn_opno (insn, opno), type_mask);
4698 opno_mask >>= 1;
4699 opno += 1;
4700 }
4701 return reg_mask;
df58fc94
RS
4702}
4703
4c260379
RS
4704/* Return the mask of core registers that IP reads. */
4705
4706static unsigned int
4707gpr_read_mask (const struct mips_cl_insn *ip)
4708{
4709 unsigned long pinfo, pinfo2;
4710 unsigned int mask;
4711
fc76e730 4712 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_read_mask (ip->insn_mo));
4c260379
RS
4713 pinfo = ip->insn_mo->pinfo;
4714 pinfo2 = ip->insn_mo->pinfo2;
fc76e730 4715 if (pinfo & INSN_UDI)
4c260379 4716 {
fc76e730
RS
4717 /* UDI instructions have traditionally been assumed to read RS
4718 and RT. */
4719 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4720 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4c260379 4721 }
fc76e730
RS
4722 if (pinfo & INSN_READ_GPR_24)
4723 mask |= 1 << 24;
4724 if (pinfo2 & INSN2_READ_GPR_16)
4725 mask |= 1 << 16;
4726 if (pinfo2 & INSN2_READ_SP)
4727 mask |= 1 << SP;
26545944 4728 if (pinfo2 & INSN2_READ_GPR_31)
fc76e730 4729 mask |= 1 << 31;
fe35f09f
RS
4730 /* Don't include register 0. */
4731 return mask & ~1;
4c260379
RS
4732}
4733
4734/* Return the mask of core registers that IP writes. */
4735
4736static unsigned int
4737gpr_write_mask (const struct mips_cl_insn *ip)
4738{
4739 unsigned long pinfo, pinfo2;
4740 unsigned int mask;
4741
fc76e730 4742 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_write_mask (ip->insn_mo));
4c260379
RS
4743 pinfo = ip->insn_mo->pinfo;
4744 pinfo2 = ip->insn_mo->pinfo2;
fc76e730
RS
4745 if (pinfo & INSN_WRITE_GPR_24)
4746 mask |= 1 << 24;
4747 if (pinfo & INSN_WRITE_GPR_31)
4748 mask |= 1 << 31;
4749 if (pinfo & INSN_UDI)
4750 /* UDI instructions have traditionally been assumed to write to RD. */
4751 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4752 if (pinfo2 & INSN2_WRITE_SP)
4753 mask |= 1 << SP;
fe35f09f
RS
4754 /* Don't include register 0. */
4755 return mask & ~1;
4c260379
RS
4756}
4757
4758/* Return the mask of floating-point registers that IP reads. */
4759
4760static unsigned int
4761fpr_read_mask (const struct mips_cl_insn *ip)
4762{
fc76e730 4763 unsigned long pinfo;
4c260379
RS
4764 unsigned int mask;
4765
9d5de888
CF
4766 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4767 | (1 << OP_REG_MSA)),
fc76e730 4768 insn_read_mask (ip->insn_mo));
4c260379 4769 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4770 /* Conservatively treat all operands to an FP_D instruction are doubles.
4771 (This is overly pessimistic for things like cvt.d.s.) */
bad1aba3 4772 if (FPR_SIZE != 64 && (pinfo & FP_D))
4c260379
RS
4773 mask |= mask << 1;
4774 return mask;
4775}
4776
4777/* Return the mask of floating-point registers that IP writes. */
4778
4779static unsigned int
4780fpr_write_mask (const struct mips_cl_insn *ip)
4781{
fc76e730 4782 unsigned long pinfo;
4c260379
RS
4783 unsigned int mask;
4784
9d5de888
CF
4785 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4786 | (1 << OP_REG_MSA)),
fc76e730 4787 insn_write_mask (ip->insn_mo));
4c260379 4788 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4789 /* Conservatively treat all operands to an FP_D instruction are doubles.
4790 (This is overly pessimistic for things like cvt.s.d.) */
bad1aba3 4791 if (FPR_SIZE != 64 && (pinfo & FP_D))
4c260379
RS
4792 mask |= mask << 1;
4793 return mask;
4794}
4795
a1d78564
RS
4796/* Operand OPNUM of INSN is an odd-numbered floating-point register.
4797 Check whether that is allowed. */
4798
4799static bfd_boolean
4800mips_oddfpreg_ok (const struct mips_opcode *insn, int opnum)
4801{
4802 const char *s = insn->name;
351cdf24
MF
4803 bfd_boolean oddspreg = (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa, mips_opts.arch)
4804 || FPR_SIZE == 64)
4805 && mips_opts.oddspreg;
a1d78564
RS
4806
4807 if (insn->pinfo == INSN_MACRO)
4808 /* Let a macro pass, we'll catch it later when it is expanded. */
4809 return TRUE;
4810
351cdf24
MF
4811 /* Single-precision coprocessor loads and moves are OK for 32-bit registers,
4812 otherwise it depends on oddspreg. */
4813 if ((insn->pinfo & FP_S)
4814 && (insn->pinfo & (INSN_LOAD_MEMORY | INSN_STORE_MEMORY
43885403 4815 | INSN_LOAD_COPROC | INSN_COPROC_MOVE)))
351cdf24 4816 return FPR_SIZE == 32 || oddspreg;
a1d78564 4817
351cdf24
MF
4818 /* Allow odd registers for single-precision ops and double-precision if the
4819 floating-point registers are 64-bit wide. */
4820 switch (insn->pinfo & (FP_S | FP_D))
4821 {
4822 case FP_S:
4823 case 0:
4824 return oddspreg;
4825 case FP_D:
4826 return FPR_SIZE == 64;
4827 default:
4828 break;
a1d78564
RS
4829 }
4830
351cdf24
MF
4831 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
4832 s = strchr (insn->name, '.');
4833 if (s != NULL && opnum == 2)
4834 s = strchr (s + 1, '.');
4835 if (s != NULL && (s[1] == 'w' || s[1] == 's'))
4836 return oddspreg;
a1d78564 4837
351cdf24 4838 return FPR_SIZE == 64;
a1d78564
RS
4839}
4840
a1d78564
RS
4841/* Information about an instruction argument that we're trying to match. */
4842struct mips_arg_info
4843{
4844 /* The instruction so far. */
4845 struct mips_cl_insn *insn;
4846
a92713e6
RS
4847 /* The first unconsumed operand token. */
4848 struct mips_operand_token *token;
4849
a1d78564
RS
4850 /* The 1-based operand number, in terms of insn->insn_mo->args. */
4851 int opnum;
4852
4853 /* The 1-based argument number, for error reporting. This does not
4854 count elided optional registers, etc.. */
4855 int argnum;
4856
4857 /* The last OP_REG operand seen, or ILLEGAL_REG if none. */
4858 unsigned int last_regno;
4859
4860 /* If the first operand was an OP_REG, this is the register that it
4861 specified, otherwise it is ILLEGAL_REG. */
4862 unsigned int dest_regno;
4863
4864 /* The value of the last OP_INT operand. Only used for OP_MSB,
4865 where it gives the lsb position. */
4866 unsigned int last_op_int;
4867
60f20e8b 4868 /* If true, match routines should assume that no later instruction
2b0f3761 4869 alternative matches and should therefore be as accommodating as
60f20e8b
RS
4870 possible. Match routines should not report errors if something
4871 is only invalid for !LAX_MATCH. */
4872 bfd_boolean lax_match;
a1d78564 4873
a1d78564
RS
4874 /* True if a reference to the current AT register was seen. */
4875 bfd_boolean seen_at;
4876};
4877
1a00e612
RS
4878/* Record that the argument is out of range. */
4879
4880static void
4881match_out_of_range (struct mips_arg_info *arg)
4882{
4883 set_insn_error_i (arg->argnum, _("operand %d out of range"), arg->argnum);
4884}
4885
4886/* Record that the argument isn't constant but needs to be. */
4887
4888static void
4889match_not_constant (struct mips_arg_info *arg)
4890{
4891 set_insn_error_i (arg->argnum, _("operand %d must be constant"),
4892 arg->argnum);
4893}
4894
a92713e6
RS
4895/* Try to match an OT_CHAR token for character CH. Consume the token
4896 and return true on success, otherwise return false. */
a1d78564 4897
a92713e6
RS
4898static bfd_boolean
4899match_char (struct mips_arg_info *arg, char ch)
a1d78564 4900{
a92713e6
RS
4901 if (arg->token->type == OT_CHAR && arg->token->u.ch == ch)
4902 {
4903 ++arg->token;
4904 if (ch == ',')
4905 arg->argnum += 1;
4906 return TRUE;
4907 }
4908 return FALSE;
4909}
a1d78564 4910
a92713e6
RS
4911/* Try to get an expression from the next tokens in ARG. Consume the
4912 tokens and return true on success, storing the expression value in
4913 VALUE and relocation types in R. */
4914
4915static bfd_boolean
4916match_expression (struct mips_arg_info *arg, expressionS *value,
4917 bfd_reloc_code_real_type *r)
4918{
d436c1c2
RS
4919 /* If the next token is a '(' that was parsed as being part of a base
4920 expression, assume we have an elided offset. The later match will fail
4921 if this turns out to be wrong. */
4922 if (arg->token->type == OT_CHAR && arg->token->u.ch == '(')
a1d78564 4923 {
d436c1c2
RS
4924 value->X_op = O_constant;
4925 value->X_add_number = 0;
4926 r[0] = r[1] = r[2] = BFD_RELOC_UNUSED;
a92713e6
RS
4927 return TRUE;
4928 }
4929
d436c1c2
RS
4930 /* Reject register-based expressions such as "0+$2" and "(($2))".
4931 For plain registers the default error seems more appropriate. */
4932 if (arg->token->type == OT_INTEGER
4933 && arg->token->u.integer.value.X_op == O_register)
a92713e6 4934 {
d436c1c2
RS
4935 set_insn_error (arg->argnum, _("register value used as expression"));
4936 return FALSE;
a1d78564 4937 }
d436c1c2
RS
4938
4939 if (arg->token->type == OT_INTEGER)
a92713e6 4940 {
d436c1c2
RS
4941 *value = arg->token->u.integer.value;
4942 memcpy (r, arg->token->u.integer.relocs, 3 * sizeof (*r));
4943 ++arg->token;
4944 return TRUE;
a92713e6 4945 }
a92713e6 4946
d436c1c2
RS
4947 set_insn_error_i
4948 (arg->argnum, _("operand %d must be an immediate expression"),
4949 arg->argnum);
4950 return FALSE;
a92713e6
RS
4951}
4952
4953/* Try to get a constant expression from the next tokens in ARG. Consume
de194d85 4954 the tokens and return true on success, storing the constant value
a54d5f8b 4955 in *VALUE. */
a92713e6
RS
4956
4957static bfd_boolean
1a00e612 4958match_const_int (struct mips_arg_info *arg, offsetT *value)
a92713e6
RS
4959{
4960 expressionS ex;
4961 bfd_reloc_code_real_type r[3];
a1d78564 4962
a92713e6
RS
4963 if (!match_expression (arg, &ex, r))
4964 return FALSE;
4965
4966 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_constant)
a1d78564
RS
4967 *value = ex.X_add_number;
4968 else
4969 {
c96425c5
MR
4970 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_big)
4971 match_out_of_range (arg);
4972 else
4973 match_not_constant (arg);
1a00e612 4974 return FALSE;
a1d78564 4975 }
a92713e6 4976 return TRUE;
a1d78564
RS
4977}
4978
4979/* Return the RTYPE_* flags for a register operand of type TYPE that
4980 appears in instruction OPCODE. */
4981
4982static unsigned int
4983convert_reg_type (const struct mips_opcode *opcode,
4984 enum mips_reg_operand_type type)
4985{
4986 switch (type)
4987 {
4988 case OP_REG_GP:
4989 return RTYPE_NUM | RTYPE_GP;
4990
4991 case OP_REG_FP:
4992 /* Allow vector register names for MDMX if the instruction is a 64-bit
4993 FPR load, store or move (including moves to and from GPRs). */
4994 if ((mips_opts.ase & ASE_MDMX)
4995 && (opcode->pinfo & FP_D)
43885403 4996 && (opcode->pinfo & (INSN_COPROC_MOVE
a1d78564 4997 | INSN_COPROC_MEMORY_DELAY
43885403 4998 | INSN_LOAD_COPROC
67dc82bc 4999 | INSN_LOAD_MEMORY
a1d78564
RS
5000 | INSN_STORE_MEMORY)))
5001 return RTYPE_FPU | RTYPE_VEC;
5002 return RTYPE_FPU;
5003
5004 case OP_REG_CCC:
5005 if (opcode->pinfo & (FP_D | FP_S))
5006 return RTYPE_CCC | RTYPE_FCC;
5007 return RTYPE_CCC;
5008
5009 case OP_REG_VEC:
5010 if (opcode->membership & INSN_5400)
5011 return RTYPE_FPU;
5012 return RTYPE_FPU | RTYPE_VEC;
5013
5014 case OP_REG_ACC:
5015 return RTYPE_ACC;
5016
5017 case OP_REG_COPRO:
5018 if (opcode->name[strlen (opcode->name) - 1] == '0')
5019 return RTYPE_NUM | RTYPE_CP0;
5020 return RTYPE_NUM;
5021
5022 case OP_REG_HW:
5023 return RTYPE_NUM;
14daeee3
RS
5024
5025 case OP_REG_VI:
5026 return RTYPE_NUM | RTYPE_VI;
5027
5028 case OP_REG_VF:
5029 return RTYPE_NUM | RTYPE_VF;
5030
5031 case OP_REG_R5900_I:
5032 return RTYPE_R5900_I;
5033
5034 case OP_REG_R5900_Q:
5035 return RTYPE_R5900_Q;
5036
5037 case OP_REG_R5900_R:
5038 return RTYPE_R5900_R;
5039
5040 case OP_REG_R5900_ACC:
5041 return RTYPE_R5900_ACC;
56d438b1
CF
5042
5043 case OP_REG_MSA:
5044 return RTYPE_MSA;
5045
5046 case OP_REG_MSA_CTRL:
5047 return RTYPE_NUM;
a1d78564
RS
5048 }
5049 abort ();
5050}
5051
5052/* ARG is register REGNO, of type TYPE. Warn about any dubious registers. */
5053
5054static void
5055check_regno (struct mips_arg_info *arg,
5056 enum mips_reg_operand_type type, unsigned int regno)
5057{
5058 if (AT && type == OP_REG_GP && regno == AT)
5059 arg->seen_at = TRUE;
5060
5061 if (type == OP_REG_FP
5062 && (regno & 1) != 0
a1d78564 5063 && !mips_oddfpreg_ok (arg->insn->insn_mo, arg->opnum))
351cdf24
MF
5064 {
5065 /* This was a warning prior to introducing O32 FPXX and FP64 support
5066 so maintain a warning for FP32 but raise an error for the new
5067 cases. */
5068 if (FPR_SIZE == 32)
5069 as_warn (_("float register should be even, was %d"), regno);
5070 else
5071 as_bad (_("float register should be even, was %d"), regno);
5072 }
a1d78564
RS
5073
5074 if (type == OP_REG_CCC)
5075 {
5076 const char *name;
5077 size_t length;
5078
5079 name = arg->insn->insn_mo->name;
5080 length = strlen (name);
5081 if ((regno & 1) != 0
5082 && ((length >= 3 && strcmp (name + length - 3, ".ps") == 0)
5083 || (length >= 5 && strncmp (name + length - 5, "any2", 4) == 0)))
1661c76c 5084 as_warn (_("condition code register should be even for %s, was %d"),
a1d78564
RS
5085 name, regno);
5086
5087 if ((regno & 3) != 0
5088 && (length >= 5 && strncmp (name + length - 5, "any4", 4) == 0))
1661c76c 5089 as_warn (_("condition code register should be 0 or 4 for %s, was %d"),
a1d78564
RS
5090 name, regno);
5091 }
5092}
5093
a92713e6
RS
5094/* ARG is a register with symbol value SYMVAL. Try to interpret it as
5095 a register of type TYPE. Return true on success, storing the register
5096 number in *REGNO and warning about any dubious uses. */
5097
5098static bfd_boolean
5099match_regno (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5100 unsigned int symval, unsigned int *regno)
5101{
5102 if (type == OP_REG_VEC)
5103 symval = mips_prefer_vec_regno (symval);
5104 if (!(symval & convert_reg_type (arg->insn->insn_mo, type)))
5105 return FALSE;
5106
5107 *regno = symval & RNUM_MASK;
5108 check_regno (arg, type, *regno);
5109 return TRUE;
5110}
5111
5112/* Try to interpret the next token in ARG as a register of type TYPE.
5113 Consume the token and return true on success, storing the register
5114 number in *REGNO. Return false on failure. */
5115
5116static bfd_boolean
5117match_reg (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5118 unsigned int *regno)
5119{
5120 if (arg->token->type == OT_REG
5121 && match_regno (arg, type, arg->token->u.regno, regno))
5122 {
5123 ++arg->token;
5124 return TRUE;
5125 }
5126 return FALSE;
5127}
5128
5129/* Try to interpret the next token in ARG as a range of registers of type TYPE.
5130 Consume the token and return true on success, storing the register numbers
5131 in *REGNO1 and *REGNO2. Return false on failure. */
5132
5133static bfd_boolean
5134match_reg_range (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5135 unsigned int *regno1, unsigned int *regno2)
5136{
5137 if (match_reg (arg, type, regno1))
5138 {
5139 *regno2 = *regno1;
5140 return TRUE;
5141 }
5142 if (arg->token->type == OT_REG_RANGE
5143 && match_regno (arg, type, arg->token->u.reg_range.regno1, regno1)
5144 && match_regno (arg, type, arg->token->u.reg_range.regno2, regno2)
5145 && *regno1 <= *regno2)
5146 {
5147 ++arg->token;
5148 return TRUE;
5149 }
5150 return FALSE;
5151}
5152
a1d78564
RS
5153/* OP_INT matcher. */
5154
a92713e6 5155static bfd_boolean
a1d78564 5156match_int_operand (struct mips_arg_info *arg,
a92713e6 5157 const struct mips_operand *operand_base)
a1d78564
RS
5158{
5159 const struct mips_int_operand *operand;
3ccad066 5160 unsigned int uval;
a1d78564
RS
5161 int min_val, max_val, factor;
5162 offsetT sval;
a1d78564
RS
5163
5164 operand = (const struct mips_int_operand *) operand_base;
5165 factor = 1 << operand->shift;
3ccad066
RS
5166 min_val = mips_int_operand_min (operand);
5167 max_val = mips_int_operand_max (operand);
a1d78564 5168
d436c1c2
RS
5169 if (operand_base->lsb == 0
5170 && operand_base->size == 16
5171 && operand->shift == 0
5172 && operand->bias == 0
5173 && (operand->max_val == 32767 || operand->max_val == 65535))
a1d78564
RS
5174 {
5175 /* The operand can be relocated. */
a92713e6
RS
5176 if (!match_expression (arg, &offset_expr, offset_reloc))
5177 return FALSE;
5178
c96425c5
MR
5179 if (offset_expr.X_op == O_big)
5180 {
5181 match_out_of_range (arg);
5182 return FALSE;
5183 }
5184
a92713e6 5185 if (offset_reloc[0] != BFD_RELOC_UNUSED)
33eaf5de 5186 /* Relocation operators were used. Accept the argument and
a1d78564
RS
5187 leave the relocation value in offset_expr and offset_relocs
5188 for the caller to process. */
a92713e6
RS
5189 return TRUE;
5190
5191 if (offset_expr.X_op != O_constant)
a1d78564 5192 {
60f20e8b
RS
5193 /* Accept non-constant operands if no later alternative matches,
5194 leaving it for the caller to process. */
5195 if (!arg->lax_match)
602b88e3
MR
5196 {
5197 match_not_constant (arg);
5198 return FALSE;
5199 }
a92713e6
RS
5200 offset_reloc[0] = BFD_RELOC_LO16;
5201 return TRUE;
a1d78564 5202 }
a92713e6 5203
a1d78564
RS
5204 /* Clear the global state; we're going to install the operand
5205 ourselves. */
a92713e6 5206 sval = offset_expr.X_add_number;
a1d78564 5207 offset_expr.X_op = O_absent;
60f20e8b
RS
5208
5209 /* For compatibility with older assemblers, we accept
5210 0x8000-0xffff as signed 16-bit numbers when only
5211 signed numbers are allowed. */
5212 if (sval > max_val)
5213 {
5214 max_val = ((1 << operand_base->size) - 1) << operand->shift;
5215 if (!arg->lax_match && sval <= max_val)
20c59b84
MR
5216 {
5217 match_out_of_range (arg);
5218 return FALSE;
5219 }
60f20e8b 5220 }
a1d78564
RS
5221 }
5222 else
5223 {
1a00e612 5224 if (!match_const_int (arg, &sval))
a92713e6 5225 return FALSE;
a1d78564
RS
5226 }
5227
5228 arg->last_op_int = sval;
5229
1a00e612 5230 if (sval < min_val || sval > max_val || sval % factor)
a1d78564 5231 {
1a00e612
RS
5232 match_out_of_range (arg);
5233 return FALSE;
a1d78564
RS
5234 }
5235
5236 uval = (unsigned int) sval >> operand->shift;
5237 uval -= operand->bias;
5238
5239 /* Handle -mfix-cn63xxp1. */
5240 if (arg->opnum == 1
5241 && mips_fix_cn63xxp1
5242 && !mips_opts.micromips
5243 && strcmp ("pref", arg->insn->insn_mo->name) == 0)
5244 switch (uval)
5245 {
5246 case 5:
5247 case 25:
5248 case 26:
5249 case 27:
5250 case 28:
5251 case 29:
5252 case 30:
5253 case 31:
5254 /* These are ok. */
5255 break;
5256
5257 default:
5258 /* The rest must be changed to 28. */
5259 uval = 28;
5260 break;
5261 }
5262
5263 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5264 return TRUE;
a1d78564
RS
5265}
5266
5267/* OP_MAPPED_INT matcher. */
5268
a92713e6 5269static bfd_boolean
a1d78564 5270match_mapped_int_operand (struct mips_arg_info *arg,
a92713e6 5271 const struct mips_operand *operand_base)
a1d78564
RS
5272{
5273 const struct mips_mapped_int_operand *operand;
5274 unsigned int uval, num_vals;
5275 offsetT sval;
5276
5277 operand = (const struct mips_mapped_int_operand *) operand_base;
1a00e612 5278 if (!match_const_int (arg, &sval))
a92713e6 5279 return FALSE;
a1d78564
RS
5280
5281 num_vals = 1 << operand_base->size;
5282 for (uval = 0; uval < num_vals; uval++)
5283 if (operand->int_map[uval] == sval)
5284 break;
5285 if (uval == num_vals)
1a00e612
RS
5286 {
5287 match_out_of_range (arg);
5288 return FALSE;
5289 }
a1d78564
RS
5290
5291 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5292 return TRUE;
a1d78564
RS
5293}
5294
5295/* OP_MSB matcher. */
5296
a92713e6 5297static bfd_boolean
a1d78564 5298match_msb_operand (struct mips_arg_info *arg,
a92713e6 5299 const struct mips_operand *operand_base)
a1d78564
RS
5300{
5301 const struct mips_msb_operand *operand;
5302 int min_val, max_val, max_high;
5303 offsetT size, sval, high;
5304
5305 operand = (const struct mips_msb_operand *) operand_base;
5306 min_val = operand->bias;
5307 max_val = min_val + (1 << operand_base->size) - 1;
5308 max_high = operand->opsize;
5309
1a00e612 5310 if (!match_const_int (arg, &size))
a92713e6 5311 return FALSE;
a1d78564
RS
5312
5313 high = size + arg->last_op_int;
5314 sval = operand->add_lsb ? high : size;
5315
5316 if (size < 0 || high > max_high || sval < min_val || sval > max_val)
5317 {
1a00e612
RS
5318 match_out_of_range (arg);
5319 return FALSE;
a1d78564
RS
5320 }
5321 insn_insert_operand (arg->insn, operand_base, sval - min_val);
a92713e6 5322 return TRUE;
a1d78564
RS
5323}
5324
5325/* OP_REG matcher. */
5326
a92713e6 5327static bfd_boolean
a1d78564 5328match_reg_operand (struct mips_arg_info *arg,
a92713e6 5329 const struct mips_operand *operand_base)
a1d78564
RS
5330{
5331 const struct mips_reg_operand *operand;
a92713e6 5332 unsigned int regno, uval, num_vals;
a1d78564
RS
5333
5334 operand = (const struct mips_reg_operand *) operand_base;
a92713e6
RS
5335 if (!match_reg (arg, operand->reg_type, &regno))
5336 return FALSE;
a1d78564
RS
5337
5338 if (operand->reg_map)
5339 {
5340 num_vals = 1 << operand->root.size;
5341 for (uval = 0; uval < num_vals; uval++)
5342 if (operand->reg_map[uval] == regno)
5343 break;
5344 if (num_vals == uval)
a92713e6 5345 return FALSE;
a1d78564
RS
5346 }
5347 else
5348 uval = regno;
5349
a1d78564
RS
5350 arg->last_regno = regno;
5351 if (arg->opnum == 1)
5352 arg->dest_regno = regno;
5353 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5354 return TRUE;
a1d78564
RS
5355}
5356
5357/* OP_REG_PAIR matcher. */
5358
a92713e6 5359static bfd_boolean
a1d78564 5360match_reg_pair_operand (struct mips_arg_info *arg,
a92713e6 5361 const struct mips_operand *operand_base)
a1d78564
RS
5362{
5363 const struct mips_reg_pair_operand *operand;
a92713e6 5364 unsigned int regno1, regno2, uval, num_vals;
a1d78564
RS
5365
5366 operand = (const struct mips_reg_pair_operand *) operand_base;
a92713e6
RS
5367 if (!match_reg (arg, operand->reg_type, &regno1)
5368 || !match_char (arg, ',')
5369 || !match_reg (arg, operand->reg_type, &regno2))
5370 return FALSE;
a1d78564
RS
5371
5372 num_vals = 1 << operand_base->size;
5373 for (uval = 0; uval < num_vals; uval++)
5374 if (operand->reg1_map[uval] == regno1 && operand->reg2_map[uval] == regno2)
5375 break;
5376 if (uval == num_vals)
a92713e6 5377 return FALSE;
a1d78564 5378
a1d78564 5379 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5380 return TRUE;
a1d78564
RS
5381}
5382
5383/* OP_PCREL matcher. The caller chooses the relocation type. */
5384
a92713e6
RS
5385static bfd_boolean
5386match_pcrel_operand (struct mips_arg_info *arg)
a1d78564 5387{
a92713e6
RS
5388 bfd_reloc_code_real_type r[3];
5389
5390 return match_expression (arg, &offset_expr, r) && r[0] == BFD_RELOC_UNUSED;
a1d78564
RS
5391}
5392
5393/* OP_PERF_REG matcher. */
5394
a92713e6 5395static bfd_boolean
a1d78564 5396match_perf_reg_operand (struct mips_arg_info *arg,
a92713e6 5397 const struct mips_operand *operand)
a1d78564
RS
5398{
5399 offsetT sval;
5400
1a00e612 5401 if (!match_const_int (arg, &sval))
a92713e6 5402 return FALSE;
a1d78564
RS
5403
5404 if (sval != 0
5405 && (sval != 1
5406 || (mips_opts.arch == CPU_R5900
5407 && (strcmp (arg->insn->insn_mo->name, "mfps") == 0
5408 || strcmp (arg->insn->insn_mo->name, "mtps") == 0))))
5409 {
1a00e612
RS
5410 set_insn_error (arg->argnum, _("invalid performance register"));
5411 return FALSE;
a1d78564
RS
5412 }
5413
5414 insn_insert_operand (arg->insn, operand, sval);
a92713e6 5415 return TRUE;
a1d78564
RS
5416}
5417
5418/* OP_ADDIUSP matcher. */
5419
a92713e6 5420static bfd_boolean
a1d78564 5421match_addiusp_operand (struct mips_arg_info *arg,
a92713e6 5422 const struct mips_operand *operand)
a1d78564
RS
5423{
5424 offsetT sval;
5425 unsigned int uval;
5426
1a00e612 5427 if (!match_const_int (arg, &sval))
a92713e6 5428 return FALSE;
a1d78564
RS
5429
5430 if (sval % 4)
1a00e612
RS
5431 {
5432 match_out_of_range (arg);
5433 return FALSE;
5434 }
a1d78564
RS
5435
5436 sval /= 4;
5437 if (!(sval >= -258 && sval <= 257) || (sval >= -2 && sval <= 1))
1a00e612
RS
5438 {
5439 match_out_of_range (arg);
5440 return FALSE;
5441 }
a1d78564
RS
5442
5443 uval = (unsigned int) sval;
5444 uval = ((uval >> 1) & ~0xff) | (uval & 0xff);
5445 insn_insert_operand (arg->insn, operand, uval);
a92713e6 5446 return TRUE;
a1d78564
RS
5447}
5448
5449/* OP_CLO_CLZ_DEST matcher. */
5450
a92713e6 5451static bfd_boolean
a1d78564 5452match_clo_clz_dest_operand (struct mips_arg_info *arg,
a92713e6 5453 const struct mips_operand *operand)
a1d78564
RS
5454{
5455 unsigned int regno;
5456
a92713e6
RS
5457 if (!match_reg (arg, OP_REG_GP, &regno))
5458 return FALSE;
a1d78564 5459
a1d78564 5460 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
a92713e6 5461 return TRUE;
a1d78564
RS
5462}
5463
7361da2c
AB
5464/* OP_CHECK_PREV matcher. */
5465
5466static bfd_boolean
5467match_check_prev_operand (struct mips_arg_info *arg,
5468 const struct mips_operand *operand_base)
5469{
5470 const struct mips_check_prev_operand *operand;
5471 unsigned int regno;
5472
5473 operand = (const struct mips_check_prev_operand *) operand_base;
5474
5475 if (!match_reg (arg, OP_REG_GP, &regno))
5476 return FALSE;
5477
5478 if (!operand->zero_ok && regno == 0)
5479 return FALSE;
5480
5481 if ((operand->less_than_ok && regno < arg->last_regno)
5482 || (operand->greater_than_ok && regno > arg->last_regno)
5483 || (operand->equal_ok && regno == arg->last_regno))
5484 {
5485 arg->last_regno = regno;
5486 insn_insert_operand (arg->insn, operand_base, regno);
5487 return TRUE;
5488 }
5489
5490 return FALSE;
5491}
5492
5493/* OP_SAME_RS_RT matcher. */
5494
5495static bfd_boolean
5496match_same_rs_rt_operand (struct mips_arg_info *arg,
5497 const struct mips_operand *operand)
5498{
5499 unsigned int regno;
5500
5501 if (!match_reg (arg, OP_REG_GP, &regno))
5502 return FALSE;
5503
5504 if (regno == 0)
5505 {
5506 set_insn_error (arg->argnum, _("the source register must not be $0"));
5507 return FALSE;
5508 }
5509
5510 arg->last_regno = regno;
5511
5512 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5513 return TRUE;
5514}
5515
a1d78564
RS
5516/* OP_LWM_SWM_LIST matcher. */
5517
a92713e6 5518static bfd_boolean
a1d78564 5519match_lwm_swm_list_operand (struct mips_arg_info *arg,
a92713e6 5520 const struct mips_operand *operand)
a1d78564 5521{
a92713e6
RS
5522 unsigned int reglist, sregs, ra, regno1, regno2;
5523 struct mips_arg_info reset;
a1d78564 5524
a92713e6
RS
5525 reglist = 0;
5526 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5527 return FALSE;
5528 do
5529 {
5530 if (regno2 == FP && regno1 >= S0 && regno1 <= S7)
5531 {
5532 reglist |= 1 << FP;
5533 regno2 = S7;
5534 }
5535 reglist |= ((1U << regno2 << 1) - 1) & -(1U << regno1);
5536 reset = *arg;
5537 }
5538 while (match_char (arg, ',')
5539 && match_reg_range (arg, OP_REG_GP, &regno1, &regno2));
5540 *arg = reset;
a1d78564
RS
5541
5542 if (operand->size == 2)
5543 {
5544 /* The list must include both ra and s0-sN, for 0 <= N <= 3. E.g.:
5545
5546 s0, ra
5547 s0, s1, ra, s2, s3
5548 s0-s2, ra
5549
5550 and any permutations of these. */
5551 if ((reglist & 0xfff1ffff) != 0x80010000)
a92713e6 5552 return FALSE;
a1d78564
RS
5553
5554 sregs = (reglist >> 17) & 7;
5555 ra = 0;
5556 }
5557 else
5558 {
5559 /* The list must include at least one of ra and s0-sN,
5560 for 0 <= N <= 8. (Note that there is a gap between s7 and s8,
5561 which are $23 and $30 respectively.) E.g.:
5562
5563 ra
5564 s0
5565 ra, s0, s1, s2
5566 s0-s8
5567 s0-s5, ra
5568
5569 and any permutations of these. */
5570 if ((reglist & 0x3f00ffff) != 0)
a92713e6 5571 return FALSE;
a1d78564
RS
5572
5573 ra = (reglist >> 27) & 0x10;
5574 sregs = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
5575 }
5576 sregs += 1;
5577 if ((sregs & -sregs) != sregs)
a92713e6 5578 return FALSE;
a1d78564
RS
5579
5580 insn_insert_operand (arg->insn, operand, (ffs (sregs) - 1) | ra);
a92713e6 5581 return TRUE;
a1d78564
RS
5582}
5583
364215c8
RS
5584/* OP_ENTRY_EXIT_LIST matcher. */
5585
a92713e6 5586static unsigned int
364215c8 5587match_entry_exit_operand (struct mips_arg_info *arg,
a92713e6 5588 const struct mips_operand *operand)
364215c8
RS
5589{
5590 unsigned int mask;
5591 bfd_boolean is_exit;
5592
5593 /* The format is the same for both ENTRY and EXIT, but the constraints
5594 are different. */
5595 is_exit = strcmp (arg->insn->insn_mo->name, "exit") == 0;
5596 mask = (is_exit ? 7 << 3 : 0);
a92713e6 5597 do
364215c8
RS
5598 {
5599 unsigned int regno1, regno2;
5600 bfd_boolean is_freg;
5601
a92713e6 5602 if (match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
364215c8 5603 is_freg = FALSE;
a92713e6 5604 else if (match_reg_range (arg, OP_REG_FP, &regno1, &regno2))
364215c8
RS
5605 is_freg = TRUE;
5606 else
a92713e6 5607 return FALSE;
364215c8
RS
5608
5609 if (is_exit && is_freg && regno1 == 0 && regno2 < 2)
5610 {
5611 mask &= ~(7 << 3);
5612 mask |= (5 + regno2) << 3;
5613 }
5614 else if (!is_exit && regno1 == 4 && regno2 >= 4 && regno2 <= 7)
5615 mask |= (regno2 - 3) << 3;
5616 else if (regno1 == 16 && regno2 >= 16 && regno2 <= 17)
5617 mask |= (regno2 - 15) << 1;
5618 else if (regno1 == RA && regno2 == RA)
5619 mask |= 1;
5620 else
a92713e6 5621 return FALSE;
364215c8 5622 }
a92713e6
RS
5623 while (match_char (arg, ','));
5624
364215c8 5625 insn_insert_operand (arg->insn, operand, mask);
a92713e6 5626 return TRUE;
364215c8
RS
5627}
5628
38bf472a
MR
5629/* Encode regular MIPS SAVE/RESTORE instruction operands according to
5630 the argument register mask AMASK, the number of static registers
5631 saved NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5632 respectively, and the frame size FRAME_SIZE. */
5633
5634static unsigned int
5635mips_encode_save_restore (unsigned int amask, unsigned int nsreg,
5636 unsigned int ra, unsigned int s0, unsigned int s1,
5637 unsigned int frame_size)
5638{
5639 return ((nsreg << 23) | ((frame_size & 0xf0) << 15) | (amask << 15)
5640 | (ra << 12) | (s0 << 11) | (s1 << 10) | ((frame_size & 0xf) << 6));
5641}
5642
5643/* Encode MIPS16 SAVE/RESTORE instruction operands according to the
5644 argument register mask AMASK, the number of static registers saved
5645 NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5646 respectively, and the frame size FRAME_SIZE. */
5647
5648static unsigned int
5649mips16_encode_save_restore (unsigned int amask, unsigned int nsreg,
5650 unsigned int ra, unsigned int s0, unsigned int s1,
5651 unsigned int frame_size)
5652{
5653 unsigned int args;
5654
5655 args = (ra << 6) | (s0 << 5) | (s1 << 4) | (frame_size & 0xf);
5656 if (nsreg || amask || frame_size == 0 || frame_size > 16)
5657 args |= (MIPS16_EXTEND | (nsreg << 24) | (amask << 16)
5658 | ((frame_size & 0xf0) << 16));
5659 return args;
5660}
5661
364215c8
RS
5662/* OP_SAVE_RESTORE_LIST matcher. */
5663
a92713e6
RS
5664static bfd_boolean
5665match_save_restore_list_operand (struct mips_arg_info *arg)
364215c8
RS
5666{
5667 unsigned int opcode, args, statics, sregs;
5668 unsigned int num_frame_sizes, num_args, num_statics, num_sregs;
38bf472a 5669 unsigned int arg_mask, ra, s0, s1;
364215c8 5670 offsetT frame_size;
364215c8 5671
364215c8
RS
5672 opcode = arg->insn->insn_opcode;
5673 frame_size = 0;
5674 num_frame_sizes = 0;
5675 args = 0;
5676 statics = 0;
5677 sregs = 0;
38bf472a
MR
5678 ra = 0;
5679 s0 = 0;
5680 s1 = 0;
a92713e6 5681 do
364215c8
RS
5682 {
5683 unsigned int regno1, regno2;
5684
a92713e6 5685 if (arg->token->type == OT_INTEGER)
364215c8
RS
5686 {
5687 /* Handle the frame size. */
1a00e612 5688 if (!match_const_int (arg, &frame_size))
a92713e6 5689 return FALSE;
364215c8 5690 num_frame_sizes += 1;
364215c8
RS
5691 }
5692 else
5693 {
a92713e6
RS
5694 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5695 return FALSE;
364215c8
RS
5696
5697 while (regno1 <= regno2)
5698 {
5699 if (regno1 >= 4 && regno1 <= 7)
5700 {
5701 if (num_frame_sizes == 0)
5702 /* args $a0-$a3 */
5703 args |= 1 << (regno1 - 4);
5704 else
5705 /* statics $a0-$a3 */
5706 statics |= 1 << (regno1 - 4);
5707 }
5708 else if (regno1 >= 16 && regno1 <= 23)
5709 /* $s0-$s7 */
5710 sregs |= 1 << (regno1 - 16);
5711 else if (regno1 == 30)
5712 /* $s8 */
5713 sregs |= 1 << 8;
5714 else if (regno1 == 31)
5715 /* Add $ra to insn. */
38bf472a 5716 ra = 1;
364215c8 5717 else
a92713e6 5718 return FALSE;
364215c8
RS
5719 regno1 += 1;
5720 if (regno1 == 24)
5721 regno1 = 30;
5722 }
5723 }
364215c8 5724 }
a92713e6 5725 while (match_char (arg, ','));
364215c8
RS
5726
5727 /* Encode args/statics combination. */
5728 if (args & statics)
a92713e6 5729 return FALSE;
364215c8
RS
5730 else if (args == 0xf)
5731 /* All $a0-$a3 are args. */
38bf472a 5732 arg_mask = MIPS_SVRS_ALL_ARGS;
364215c8
RS
5733 else if (statics == 0xf)
5734 /* All $a0-$a3 are statics. */
38bf472a 5735 arg_mask = MIPS_SVRS_ALL_STATICS;
364215c8
RS
5736 else
5737 {
5738 /* Count arg registers. */
5739 num_args = 0;
5740 while (args & 0x1)
5741 {
5742 args >>= 1;
5743 num_args += 1;
5744 }
5745 if (args != 0)
a92713e6 5746 return FALSE;
364215c8
RS
5747
5748 /* Count static registers. */
5749 num_statics = 0;
5750 while (statics & 0x8)
5751 {
5752 statics = (statics << 1) & 0xf;
5753 num_statics += 1;
5754 }
5755 if (statics != 0)
a92713e6 5756 return FALSE;
364215c8
RS
5757
5758 /* Encode args/statics. */
38bf472a 5759 arg_mask = (num_args << 2) | num_statics;
364215c8
RS
5760 }
5761
5762 /* Encode $s0/$s1. */
5763 if (sregs & (1 << 0)) /* $s0 */
38bf472a 5764 s0 = 1;
364215c8 5765 if (sregs & (1 << 1)) /* $s1 */
38bf472a 5766 s1 = 1;
364215c8
RS
5767 sregs >>= 2;
5768
5769 /* Encode $s2-$s8. */
5770 num_sregs = 0;
5771 while (sregs & 1)
5772 {
5773 sregs >>= 1;
5774 num_sregs += 1;
5775 }
5776 if (sregs != 0)
a92713e6 5777 return FALSE;
364215c8
RS
5778
5779 /* Encode frame size. */
5780 if (num_frame_sizes == 0)
1a00e612
RS
5781 {
5782 set_insn_error (arg->argnum, _("missing frame size"));
5783 return FALSE;
5784 }
5785 if (num_frame_sizes > 1)
5786 {
5787 set_insn_error (arg->argnum, _("frame size specified twice"));
5788 return FALSE;
5789 }
5790 if ((frame_size & 7) != 0 || frame_size < 0 || frame_size > 0xff * 8)
5791 {
5792 set_insn_error (arg->argnum, _("invalid frame size"));
5793 return FALSE;
5794 }
38bf472a 5795 frame_size /= 8;
364215c8 5796
364215c8 5797 /* Finally build the instruction. */
38bf472a
MR
5798 if (mips_opts.mips16)
5799 opcode |= mips16_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5800 frame_size);
5801 else if (!mips_opts.micromips)
5802 opcode |= mips_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5803 frame_size);
5804 else
5805 abort ();
5806
364215c8 5807 arg->insn->insn_opcode = opcode;
a92713e6 5808 return TRUE;
364215c8
RS
5809}
5810
a1d78564
RS
5811/* OP_MDMX_IMM_REG matcher. */
5812
a92713e6 5813static bfd_boolean
a1d78564 5814match_mdmx_imm_reg_operand (struct mips_arg_info *arg,
a92713e6 5815 const struct mips_operand *operand)
a1d78564 5816{
a92713e6 5817 unsigned int regno, uval;
a1d78564
RS
5818 bfd_boolean is_qh;
5819 const struct mips_opcode *opcode;
5820
5821 /* The mips_opcode records whether this is an octobyte or quadhalf
5822 instruction. Start out with that bit in place. */
5823 opcode = arg->insn->insn_mo;
5824 uval = mips_extract_operand (operand, opcode->match);
5825 is_qh = (uval != 0);
5826
56d438b1 5827 if (arg->token->type == OT_REG)
a1d78564
RS
5828 {
5829 if ((opcode->membership & INSN_5400)
5830 && strcmp (opcode->name, "rzu.ob") == 0)
5831 {
1a00e612
RS
5832 set_insn_error_i (arg->argnum, _("operand %d must be an immediate"),
5833 arg->argnum);
5834 return FALSE;
a1d78564
RS
5835 }
5836
56d438b1
CF
5837 if (!match_regno (arg, OP_REG_VEC, arg->token->u.regno, &regno))
5838 return FALSE;
5839 ++arg->token;
5840
a1d78564
RS
5841 /* Check whether this is a vector register or a broadcast of
5842 a single element. */
56d438b1 5843 if (arg->token->type == OT_INTEGER_INDEX)
a1d78564 5844 {
56d438b1 5845 if (arg->token->u.index > (is_qh ? 3 : 7))
a1d78564 5846 {
1a00e612
RS
5847 set_insn_error (arg->argnum, _("invalid element selector"));
5848 return FALSE;
a1d78564 5849 }
56d438b1
CF
5850 uval |= arg->token->u.index << (is_qh ? 2 : 1) << 5;
5851 ++arg->token;
a1d78564
RS
5852 }
5853 else
5854 {
5855 /* A full vector. */
5856 if ((opcode->membership & INSN_5400)
5857 && (strcmp (opcode->name, "sll.ob") == 0
5858 || strcmp (opcode->name, "srl.ob") == 0))
5859 {
1a00e612
RS
5860 set_insn_error_i (arg->argnum, _("operand %d must be scalar"),
5861 arg->argnum);
5862 return FALSE;
a1d78564
RS
5863 }
5864
5865 if (is_qh)
5866 uval |= MDMX_FMTSEL_VEC_QH << 5;
5867 else
5868 uval |= MDMX_FMTSEL_VEC_OB << 5;
5869 }
a1d78564
RS
5870 uval |= regno;
5871 }
5872 else
5873 {
5874 offsetT sval;
5875
1a00e612 5876 if (!match_const_int (arg, &sval))
a92713e6 5877 return FALSE;
a1d78564
RS
5878 if (sval < 0 || sval > 31)
5879 {
1a00e612
RS
5880 match_out_of_range (arg);
5881 return FALSE;
a1d78564
RS
5882 }
5883 uval |= (sval & 31);
5884 if (is_qh)
5885 uval |= MDMX_FMTSEL_IMM_QH << 5;
5886 else
5887 uval |= MDMX_FMTSEL_IMM_OB << 5;
5888 }
5889 insn_insert_operand (arg->insn, operand, uval);
a92713e6 5890 return TRUE;
a1d78564
RS
5891}
5892
56d438b1
CF
5893/* OP_IMM_INDEX matcher. */
5894
5895static bfd_boolean
5896match_imm_index_operand (struct mips_arg_info *arg,
5897 const struct mips_operand *operand)
5898{
5899 unsigned int max_val;
5900
5901 if (arg->token->type != OT_INTEGER_INDEX)
5902 return FALSE;
5903
5904 max_val = (1 << operand->size) - 1;
5905 if (arg->token->u.index > max_val)
5906 {
5907 match_out_of_range (arg);
5908 return FALSE;
5909 }
5910 insn_insert_operand (arg->insn, operand, arg->token->u.index);
5911 ++arg->token;
5912 return TRUE;
5913}
5914
5915/* OP_REG_INDEX matcher. */
5916
5917static bfd_boolean
5918match_reg_index_operand (struct mips_arg_info *arg,
5919 const struct mips_operand *operand)
5920{
5921 unsigned int regno;
5922
5923 if (arg->token->type != OT_REG_INDEX)
5924 return FALSE;
5925
5926 if (!match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno))
5927 return FALSE;
5928
5929 insn_insert_operand (arg->insn, operand, regno);
5930 ++arg->token;
5931 return TRUE;
5932}
5933
a1d78564
RS
5934/* OP_PC matcher. */
5935
a92713e6
RS
5936static bfd_boolean
5937match_pc_operand (struct mips_arg_info *arg)
a1d78564 5938{
a92713e6
RS
5939 if (arg->token->type == OT_REG && (arg->token->u.regno & RTYPE_PC))
5940 {
5941 ++arg->token;
5942 return TRUE;
5943 }
5944 return FALSE;
a1d78564
RS
5945}
5946
25499ac7
MR
5947/* OP_REG28 matcher. */
5948
5949static bfd_boolean
5950match_reg28_operand (struct mips_arg_info *arg)
5951{
5952 unsigned int regno;
5953
5954 if (arg->token->type == OT_REG
5955 && match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno)
5956 && regno == GP)
5957 {
5958 ++arg->token;
5959 return TRUE;
5960 }
5961 return FALSE;
5962}
5963
7361da2c
AB
5964/* OP_NON_ZERO_REG matcher. */
5965
5966static bfd_boolean
5967match_non_zero_reg_operand (struct mips_arg_info *arg,
5968 const struct mips_operand *operand)
5969{
5970 unsigned int regno;
5971
5972 if (!match_reg (arg, OP_REG_GP, &regno))
5973 return FALSE;
5974
5975 if (regno == 0)
5976 return FALSE;
5977
5978 arg->last_regno = regno;
5979 insn_insert_operand (arg->insn, operand, regno);
5980 return TRUE;
5981}
5982
a1d78564
RS
5983/* OP_REPEAT_DEST_REG and OP_REPEAT_PREV_REG matcher. OTHER_REGNO is the
5984 register that we need to match. */
5985
a92713e6
RS
5986static bfd_boolean
5987match_tied_reg_operand (struct mips_arg_info *arg, unsigned int other_regno)
a1d78564
RS
5988{
5989 unsigned int regno;
5990
a92713e6 5991 return match_reg (arg, OP_REG_GP, &regno) && regno == other_regno;
a1d78564
RS
5992}
5993
33f46696
MR
5994/* Try to match a floating-point constant from ARG for LI.S or LI.D.
5995 LENGTH is the length of the value in bytes (4 for float, 8 for double)
5996 and USING_GPRS says whether the destination is a GPR rather than an FPR.
89565f1b
RS
5997
5998 Return the constant in IMM and OFFSET as follows:
5999
6000 - If the constant should be loaded via memory, set IMM to O_absent and
6001 OFFSET to the memory address.
6002
6003 - Otherwise, if the constant should be loaded into two 32-bit registers,
6004 set IMM to the O_constant to load into the high register and OFFSET
6005 to the corresponding value for the low register.
6006
6007 - Otherwise, set IMM to the full O_constant and set OFFSET to O_absent.
6008
6009 These constants only appear as the last operand in an instruction,
6010 and every instruction that accepts them in any variant accepts them
6011 in all variants. This means we don't have to worry about backing out
6012 any changes if the instruction does not match. We just match
6013 unconditionally and report an error if the constant is invalid. */
6014
a92713e6
RS
6015static bfd_boolean
6016match_float_constant (struct mips_arg_info *arg, expressionS *imm,
6017 expressionS *offset, int length, bfd_boolean using_gprs)
89565f1b 6018{
a92713e6 6019 char *p;
89565f1b
RS
6020 segT seg, new_seg;
6021 subsegT subseg;
6022 const char *newname;
a92713e6 6023 unsigned char *data;
89565f1b
RS
6024
6025 /* Where the constant is placed is based on how the MIPS assembler
6026 does things:
6027
6028 length == 4 && using_gprs -- immediate value only
6029 length == 8 && using_gprs -- .rdata or immediate value
6030 length == 4 && !using_gprs -- .lit4 or immediate value
6031 length == 8 && !using_gprs -- .lit8 or immediate value
6032
6033 The .lit4 and .lit8 sections are only used if permitted by the
6034 -G argument. */
a92713e6 6035 if (arg->token->type != OT_FLOAT)
1a00e612
RS
6036 {
6037 set_insn_error (arg->argnum, _("floating-point expression required"));
6038 return FALSE;
6039 }
a92713e6
RS
6040
6041 gas_assert (arg->token->u.flt.length == length);
6042 data = arg->token->u.flt.data;
6043 ++arg->token;
89565f1b
RS
6044
6045 /* Handle 32-bit constants for which an immediate value is best. */
6046 if (length == 4
6047 && (using_gprs
6048 || g_switch_value < 4
6049 || (data[0] == 0 && data[1] == 0)
6050 || (data[2] == 0 && data[3] == 0)))
6051 {
6052 imm->X_op = O_constant;
6053 if (!target_big_endian)
6054 imm->X_add_number = bfd_getl32 (data);
6055 else
6056 imm->X_add_number = bfd_getb32 (data);
6057 offset->X_op = O_absent;
a92713e6 6058 return TRUE;
89565f1b
RS
6059 }
6060
6061 /* Handle 64-bit constants for which an immediate value is best. */
6062 if (length == 8
6063 && !mips_disable_float_construction
351cdf24
MF
6064 /* Constants can only be constructed in GPRs and copied to FPRs if the
6065 GPRs are at least as wide as the FPRs or MTHC1 is available.
6066 Unlike most tests for 32-bit floating-point registers this check
6067 specifically looks for GPR_SIZE == 32 as the FPXX ABI does not
6068 permit 64-bit moves without MXHC1.
6069 Force the constant into memory otherwise. */
6070 && (using_gprs
6071 || GPR_SIZE == 64
6072 || ISA_HAS_MXHC1 (mips_opts.isa)
6073 || FPR_SIZE == 32)
89565f1b
RS
6074 && ((data[0] == 0 && data[1] == 0)
6075 || (data[2] == 0 && data[3] == 0))
6076 && ((data[4] == 0 && data[5] == 0)
6077 || (data[6] == 0 && data[7] == 0)))
6078 {
6079 /* The value is simple enough to load with a couple of instructions.
6080 If using 32-bit registers, set IMM to the high order 32 bits and
6081 OFFSET to the low order 32 bits. Otherwise, set IMM to the entire
6082 64 bit constant. */
351cdf24 6083 if (GPR_SIZE == 32 || (!using_gprs && FPR_SIZE != 64))
89565f1b
RS
6084 {
6085 imm->X_op = O_constant;
6086 offset->X_op = O_constant;
6087 if (!target_big_endian)
6088 {
6089 imm->X_add_number = bfd_getl32 (data + 4);
6090 offset->X_add_number = bfd_getl32 (data);
6091 }
6092 else
6093 {
6094 imm->X_add_number = bfd_getb32 (data);
6095 offset->X_add_number = bfd_getb32 (data + 4);
6096 }
6097 if (offset->X_add_number == 0)
6098 offset->X_op = O_absent;
6099 }
6100 else
6101 {
6102 imm->X_op = O_constant;
6103 if (!target_big_endian)
6104 imm->X_add_number = bfd_getl64 (data);
6105 else
6106 imm->X_add_number = bfd_getb64 (data);
6107 offset->X_op = O_absent;
6108 }
a92713e6 6109 return TRUE;
89565f1b
RS
6110 }
6111
6112 /* Switch to the right section. */
6113 seg = now_seg;
6114 subseg = now_subseg;
6115 if (length == 4)
6116 {
6117 gas_assert (!using_gprs && g_switch_value >= 4);
6118 newname = ".lit4";
6119 }
6120 else
6121 {
6122 if (using_gprs || g_switch_value < 8)
6123 newname = RDATA_SECTION_NAME;
6124 else
6125 newname = ".lit8";
6126 }
6127
6128 new_seg = subseg_new (newname, (subsegT) 0);
6129 bfd_set_section_flags (stdoutput, new_seg,
6130 SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA);
6131 frag_align (length == 4 ? 2 : 3, 0, 0);
6132 if (strncmp (TARGET_OS, "elf", 3) != 0)
6133 record_alignment (new_seg, 4);
6134 else
6135 record_alignment (new_seg, length == 4 ? 2 : 3);
6136 if (seg == now_seg)
1661c76c 6137 as_bad (_("cannot use `%s' in this section"), arg->insn->insn_mo->name);
89565f1b
RS
6138
6139 /* Set the argument to the current address in the section. */
6140 imm->X_op = O_absent;
6141 offset->X_op = O_symbol;
6142 offset->X_add_symbol = symbol_temp_new_now ();
6143 offset->X_add_number = 0;
6144
6145 /* Put the floating point number into the section. */
6146 p = frag_more (length);
6147 memcpy (p, data, length);
6148
6149 /* Switch back to the original section. */
6150 subseg_set (seg, subseg);
a92713e6 6151 return TRUE;
89565f1b
RS
6152}
6153
14daeee3
RS
6154/* OP_VU0_SUFFIX and OP_VU0_MATCH_SUFFIX matcher; MATCH_P selects between
6155 them. */
6156
6157static bfd_boolean
6158match_vu0_suffix_operand (struct mips_arg_info *arg,
6159 const struct mips_operand *operand,
6160 bfd_boolean match_p)
6161{
6162 unsigned int uval;
6163
6164 /* The operand can be an XYZW mask or a single 2-bit channel index
6165 (with X being 0). */
6166 gas_assert (operand->size == 2 || operand->size == 4);
6167
ee5734f0 6168 /* The suffix can be omitted when it is already part of the opcode. */
14daeee3 6169 if (arg->token->type != OT_CHANNELS)
ee5734f0 6170 return match_p;
14daeee3
RS
6171
6172 uval = arg->token->u.channels;
6173 if (operand->size == 2)
6174 {
6175 /* Check that a single bit is set and convert it into a 2-bit index. */
6176 if ((uval & -uval) != uval)
6177 return FALSE;
6178 uval = 4 - ffs (uval);
6179 }
6180
6181 if (match_p && insn_extract_operand (arg->insn, operand) != uval)
6182 return FALSE;
6183
6184 ++arg->token;
6185 if (!match_p)
6186 insn_insert_operand (arg->insn, operand, uval);
6187 return TRUE;
6188}
6189
33f46696
MR
6190/* Try to match a token from ARG against OPERAND. Consume the token
6191 and return true on success, otherwise return false. */
a1d78564 6192
a92713e6 6193static bfd_boolean
a1d78564 6194match_operand (struct mips_arg_info *arg,
a92713e6 6195 const struct mips_operand *operand)
a1d78564
RS
6196{
6197 switch (operand->type)
6198 {
6199 case OP_INT:
a92713e6 6200 return match_int_operand (arg, operand);
a1d78564
RS
6201
6202 case OP_MAPPED_INT:
a92713e6 6203 return match_mapped_int_operand (arg, operand);
a1d78564
RS
6204
6205 case OP_MSB:
a92713e6 6206 return match_msb_operand (arg, operand);
a1d78564
RS
6207
6208 case OP_REG:
0f35dbc4 6209 case OP_OPTIONAL_REG:
a92713e6 6210 return match_reg_operand (arg, operand);
a1d78564
RS
6211
6212 case OP_REG_PAIR:
a92713e6 6213 return match_reg_pair_operand (arg, operand);
a1d78564
RS
6214
6215 case OP_PCREL:
a92713e6 6216 return match_pcrel_operand (arg);
a1d78564
RS
6217
6218 case OP_PERF_REG:
a92713e6 6219 return match_perf_reg_operand (arg, operand);
a1d78564
RS
6220
6221 case OP_ADDIUSP_INT:
a92713e6 6222 return match_addiusp_operand (arg, operand);
a1d78564
RS
6223
6224 case OP_CLO_CLZ_DEST:
a92713e6 6225 return match_clo_clz_dest_operand (arg, operand);
a1d78564
RS
6226
6227 case OP_LWM_SWM_LIST:
a92713e6 6228 return match_lwm_swm_list_operand (arg, operand);
a1d78564
RS
6229
6230 case OP_ENTRY_EXIT_LIST:
a92713e6 6231 return match_entry_exit_operand (arg, operand);
364215c8 6232
a1d78564 6233 case OP_SAVE_RESTORE_LIST:
a92713e6 6234 return match_save_restore_list_operand (arg);
a1d78564
RS
6235
6236 case OP_MDMX_IMM_REG:
a92713e6 6237 return match_mdmx_imm_reg_operand (arg, operand);
a1d78564
RS
6238
6239 case OP_REPEAT_DEST_REG:
a92713e6 6240 return match_tied_reg_operand (arg, arg->dest_regno);
a1d78564
RS
6241
6242 case OP_REPEAT_PREV_REG:
a92713e6 6243 return match_tied_reg_operand (arg, arg->last_regno);
a1d78564
RS
6244
6245 case OP_PC:
a92713e6 6246 return match_pc_operand (arg);
14daeee3 6247
25499ac7
MR
6248 case OP_REG28:
6249 return match_reg28_operand (arg);
6250
14daeee3
RS
6251 case OP_VU0_SUFFIX:
6252 return match_vu0_suffix_operand (arg, operand, FALSE);
6253
6254 case OP_VU0_MATCH_SUFFIX:
6255 return match_vu0_suffix_operand (arg, operand, TRUE);
56d438b1
CF
6256
6257 case OP_IMM_INDEX:
6258 return match_imm_index_operand (arg, operand);
6259
6260 case OP_REG_INDEX:
6261 return match_reg_index_operand (arg, operand);
7361da2c
AB
6262
6263 case OP_SAME_RS_RT:
6264 return match_same_rs_rt_operand (arg, operand);
6265
6266 case OP_CHECK_PREV:
6267 return match_check_prev_operand (arg, operand);
6268
6269 case OP_NON_ZERO_REG:
6270 return match_non_zero_reg_operand (arg, operand);
a1d78564
RS
6271 }
6272 abort ();
6273}
6274
6275/* ARG is the state after successfully matching an instruction.
6276 Issue any queued-up warnings. */
6277
6278static void
6279check_completed_insn (struct mips_arg_info *arg)
6280{
6281 if (arg->seen_at)
6282 {
6283 if (AT == ATREG)
1661c76c 6284 as_warn (_("used $at without \".set noat\""));
a1d78564 6285 else
1661c76c 6286 as_warn (_("used $%u with \".set at=$%u\""), AT, AT);
a1d78564
RS
6287 }
6288}
a1d78564 6289
85fcb30f
RS
6290/* Return true if modifying general-purpose register REG needs a delay. */
6291
6292static bfd_boolean
6293reg_needs_delay (unsigned int reg)
6294{
6295 unsigned long prev_pinfo;
6296
6297 prev_pinfo = history[0].insn_mo->pinfo;
6298 if (!mips_opts.noreorder
67dc82bc 6299 && (((prev_pinfo & INSN_LOAD_MEMORY) && !gpr_interlocks)
43885403 6300 || ((prev_pinfo & INSN_LOAD_COPROC) && !cop_interlocks))
85fcb30f
RS
6301 && (gpr_write_mask (&history[0]) & (1 << reg)))
6302 return TRUE;
6303
6304 return FALSE;
6305}
6306
71400594
RS
6307/* Classify an instruction according to the FIX_VR4120_* enumeration.
6308 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
6309 by VR4120 errata. */
4d7206a2 6310
71400594
RS
6311static unsigned int
6312classify_vr4120_insn (const char *name)
252b5132 6313{
71400594
RS
6314 if (strncmp (name, "macc", 4) == 0)
6315 return FIX_VR4120_MACC;
6316 if (strncmp (name, "dmacc", 5) == 0)
6317 return FIX_VR4120_DMACC;
6318 if (strncmp (name, "mult", 4) == 0)
6319 return FIX_VR4120_MULT;
6320 if (strncmp (name, "dmult", 5) == 0)
6321 return FIX_VR4120_DMULT;
6322 if (strstr (name, "div"))
6323 return FIX_VR4120_DIV;
6324 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
6325 return FIX_VR4120_MTHILO;
6326 return NUM_FIX_VR4120_CLASSES;
6327}
252b5132 6328
a8d14a88
CM
6329#define INSN_ERET 0x42000018
6330#define INSN_DERET 0x4200001f
6331#define INSN_DMULT 0x1c
6332#define INSN_DMULTU 0x1d
ff239038 6333
71400594
RS
6334/* Return the number of instructions that must separate INSN1 and INSN2,
6335 where INSN1 is the earlier instruction. Return the worst-case value
6336 for any INSN2 if INSN2 is null. */
252b5132 6337
71400594
RS
6338static unsigned int
6339insns_between (const struct mips_cl_insn *insn1,
6340 const struct mips_cl_insn *insn2)
6341{
6342 unsigned long pinfo1, pinfo2;
4c260379 6343 unsigned int mask;
71400594 6344
85fcb30f
RS
6345 /* If INFO2 is null, pessimistically assume that all flags are set for
6346 the second instruction. */
71400594
RS
6347 pinfo1 = insn1->insn_mo->pinfo;
6348 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
252b5132 6349
71400594
RS
6350 /* For most targets, write-after-read dependencies on the HI and LO
6351 registers must be separated by at least two instructions. */
6352 if (!hilo_interlocks)
252b5132 6353 {
71400594
RS
6354 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
6355 return 2;
6356 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
6357 return 2;
6358 }
6359
6360 /* If we're working around r7000 errata, there must be two instructions
6361 between an mfhi or mflo and any instruction that uses the result. */
6362 if (mips_7000_hilo_fix
df58fc94 6363 && !mips_opts.micromips
71400594 6364 && MF_HILO_INSN (pinfo1)
85fcb30f 6365 && (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1))))
71400594
RS
6366 return 2;
6367
ff239038
CM
6368 /* If we're working around 24K errata, one instruction is required
6369 if an ERET or DERET is followed by a branch instruction. */
df58fc94 6370 if (mips_fix_24k && !mips_opts.micromips)
ff239038
CM
6371 {
6372 if (insn1->insn_opcode == INSN_ERET
6373 || insn1->insn_opcode == INSN_DERET)
6374 {
6375 if (insn2 == NULL
6376 || insn2->insn_opcode == INSN_ERET
6377 || insn2->insn_opcode == INSN_DERET
11625dd8 6378 || delayed_branch_p (insn2))
ff239038
CM
6379 return 1;
6380 }
6381 }
6382
a8d14a88
CM
6383 /* If we're working around PMC RM7000 errata, there must be three
6384 nops between a dmult and a load instruction. */
6385 if (mips_fix_rm7000 && !mips_opts.micromips)
6386 {
6387 if ((insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULT
6388 || (insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULTU)
6389 {
6390 if (pinfo2 & INSN_LOAD_MEMORY)
6391 return 3;
6392 }
6393 }
6394
71400594
RS
6395 /* If working around VR4120 errata, check for combinations that need
6396 a single intervening instruction. */
df58fc94 6397 if (mips_fix_vr4120 && !mips_opts.micromips)
71400594
RS
6398 {
6399 unsigned int class1, class2;
252b5132 6400
71400594
RS
6401 class1 = classify_vr4120_insn (insn1->insn_mo->name);
6402 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
252b5132 6403 {
71400594
RS
6404 if (insn2 == NULL)
6405 return 1;
6406 class2 = classify_vr4120_insn (insn2->insn_mo->name);
6407 if (vr4120_conflicts[class1] & (1 << class2))
6408 return 1;
252b5132 6409 }
71400594
RS
6410 }
6411
df58fc94 6412 if (!HAVE_CODE_COMPRESSION)
71400594
RS
6413 {
6414 /* Check for GPR or coprocessor load delays. All such delays
6415 are on the RT register. */
6416 /* Itbl support may require additional care here. */
67dc82bc 6417 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY))
43885403 6418 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC)))
252b5132 6419 {
85fcb30f 6420 if (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1)))
71400594
RS
6421 return 1;
6422 }
6423
6424 /* Check for generic coprocessor hazards.
6425
6426 This case is not handled very well. There is no special
6427 knowledge of CP0 handling, and the coprocessors other than
6428 the floating point unit are not distinguished at all. */
6429 /* Itbl support may require additional care here. FIXME!
6430 Need to modify this to include knowledge about
6431 user specified delays! */
43885403 6432 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE))
71400594
RS
6433 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
6434 {
6435 /* Handle cases where INSN1 writes to a known general coprocessor
6436 register. There must be a one instruction delay before INSN2
6437 if INSN2 reads that register, otherwise no delay is needed. */
4c260379
RS
6438 mask = fpr_write_mask (insn1);
6439 if (mask != 0)
252b5132 6440 {
4c260379 6441 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
71400594 6442 return 1;
252b5132
RH
6443 }
6444 else
6445 {
71400594
RS
6446 /* Read-after-write dependencies on the control registers
6447 require a two-instruction gap. */
6448 if ((pinfo1 & INSN_WRITE_COND_CODE)
6449 && (pinfo2 & INSN_READ_COND_CODE))
6450 return 2;
6451
6452 /* We don't know exactly what INSN1 does. If INSN2 is
6453 also a coprocessor instruction, assume there must be
6454 a one instruction gap. */
6455 if (pinfo2 & INSN_COP)
6456 return 1;
252b5132
RH
6457 }
6458 }
6b76fefe 6459
71400594
RS
6460 /* Check for read-after-write dependencies on the coprocessor
6461 control registers in cases where INSN1 does not need a general
6462 coprocessor delay. This means that INSN1 is a floating point
6463 comparison instruction. */
6464 /* Itbl support may require additional care here. */
6465 else if (!cop_interlocks
6466 && (pinfo1 & INSN_WRITE_COND_CODE)
6467 && (pinfo2 & INSN_READ_COND_CODE))
6468 return 1;
6469 }
6b76fefe 6470
7361da2c
AB
6471 /* Forbidden slots can not contain Control Transfer Instructions (CTIs)
6472 CTIs include all branches and jumps, nal, eret, eretnc, deret, wait
6473 and pause. */
6474 if ((insn1->insn_mo->pinfo2 & INSN2_FORBIDDEN_SLOT)
6475 && ((pinfo2 & INSN_NO_DELAY_SLOT)
6476 || (insn2 && delayed_branch_p (insn2))))
6477 return 1;
6478
71400594
RS
6479 return 0;
6480}
6b76fefe 6481
7d8e00cf
RS
6482/* Return the number of nops that would be needed to work around the
6483 VR4130 mflo/mfhi errata if instruction INSN immediately followed
932d1a1b
RS
6484 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
6485 that are contained within the first IGNORE instructions of HIST. */
7d8e00cf
RS
6486
6487static int
932d1a1b 6488nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
7d8e00cf
RS
6489 const struct mips_cl_insn *insn)
6490{
4c260379
RS
6491 int i, j;
6492 unsigned int mask;
7d8e00cf
RS
6493
6494 /* Check if the instruction writes to HI or LO. MTHI and MTLO
6495 are not affected by the errata. */
6496 if (insn != 0
6497 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
6498 || strcmp (insn->insn_mo->name, "mtlo") == 0
6499 || strcmp (insn->insn_mo->name, "mthi") == 0))
6500 return 0;
6501
6502 /* Search for the first MFLO or MFHI. */
6503 for (i = 0; i < MAX_VR4130_NOPS; i++)
91d6fa6a 6504 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
7d8e00cf
RS
6505 {
6506 /* Extract the destination register. */
4c260379 6507 mask = gpr_write_mask (&hist[i]);
7d8e00cf
RS
6508
6509 /* No nops are needed if INSN reads that register. */
4c260379 6510 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
7d8e00cf
RS
6511 return 0;
6512
6513 /* ...or if any of the intervening instructions do. */
6514 for (j = 0; j < i; j++)
4c260379 6515 if (gpr_read_mask (&hist[j]) & mask)
7d8e00cf
RS
6516 return 0;
6517
932d1a1b
RS
6518 if (i >= ignore)
6519 return MAX_VR4130_NOPS - i;
7d8e00cf
RS
6520 }
6521 return 0;
6522}
6523
134c0c8b
MR
6524#define BASE_REG_EQ(INSN1, INSN2) \
6525 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
15be625d
CM
6526 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
6527
6528/* Return the minimum alignment for this store instruction. */
6529
6530static int
6531fix_24k_align_to (const struct mips_opcode *mo)
6532{
6533 if (strcmp (mo->name, "sh") == 0)
6534 return 2;
6535
6536 if (strcmp (mo->name, "swc1") == 0
6537 || strcmp (mo->name, "swc2") == 0
6538 || strcmp (mo->name, "sw") == 0
6539 || strcmp (mo->name, "sc") == 0
6540 || strcmp (mo->name, "s.s") == 0)
6541 return 4;
6542
6543 if (strcmp (mo->name, "sdc1") == 0
6544 || strcmp (mo->name, "sdc2") == 0
6545 || strcmp (mo->name, "s.d") == 0)
6546 return 8;
6547
6548 /* sb, swl, swr */
6549 return 1;
6550}
6551
6552struct fix_24k_store_info
6553 {
6554 /* Immediate offset, if any, for this store instruction. */
6555 short off;
6556 /* Alignment required by this store instruction. */
6557 int align_to;
6558 /* True for register offsets. */
6559 int register_offset;
6560 };
6561
6562/* Comparison function used by qsort. */
6563
6564static int
6565fix_24k_sort (const void *a, const void *b)
6566{
6567 const struct fix_24k_store_info *pos1 = a;
6568 const struct fix_24k_store_info *pos2 = b;
6569
6570 return (pos1->off - pos2->off);
6571}
6572
6573/* INSN is a store instruction. Try to record the store information
6574 in STINFO. Return false if the information isn't known. */
6575
6576static bfd_boolean
6577fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
ab9794cf 6578 const struct mips_cl_insn *insn)
15be625d
CM
6579{
6580 /* The instruction must have a known offset. */
6581 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
6582 return FALSE;
6583
6584 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
6585 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
6586 return TRUE;
6587}
6588
932d1a1b
RS
6589/* Return the number of nops that would be needed to work around the 24k
6590 "lost data on stores during refill" errata if instruction INSN
6591 immediately followed the 2 instructions described by HIST.
6592 Ignore hazards that are contained within the first IGNORE
6593 instructions of HIST.
6594
6595 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
6596 for the data cache refills and store data. The following describes
6597 the scenario where the store data could be lost.
6598
6599 * A data cache miss, due to either a load or a store, causing fill
6600 data to be supplied by the memory subsystem
6601 * The first three doublewords of fill data are returned and written
6602 into the cache
6603 * A sequence of four stores occurs in consecutive cycles around the
6604 final doubleword of the fill:
6605 * Store A
6606 * Store B
6607 * Store C
6608 * Zero, One or more instructions
6609 * Store D
6610
6611 The four stores A-D must be to different doublewords of the line that
6612 is being filled. The fourth instruction in the sequence above permits
6613 the fill of the final doubleword to be transferred from the FSB into
6614 the cache. In the sequence above, the stores may be either integer
6615 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
6616 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
6617 different doublewords on the line. If the floating point unit is
6618 running in 1:2 mode, it is not possible to create the sequence above
6619 using only floating point store instructions.
15be625d
CM
6620
6621 In this case, the cache line being filled is incorrectly marked
6622 invalid, thereby losing the data from any store to the line that
6623 occurs between the original miss and the completion of the five
6624 cycle sequence shown above.
6625
932d1a1b 6626 The workarounds are:
15be625d 6627
932d1a1b
RS
6628 * Run the data cache in write-through mode.
6629 * Insert a non-store instruction between
6630 Store A and Store B or Store B and Store C. */
3739860c 6631
15be625d 6632static int
932d1a1b 6633nops_for_24k (int ignore, const struct mips_cl_insn *hist,
15be625d
CM
6634 const struct mips_cl_insn *insn)
6635{
6636 struct fix_24k_store_info pos[3];
6637 int align, i, base_offset;
6638
932d1a1b
RS
6639 if (ignore >= 2)
6640 return 0;
6641
ab9794cf
RS
6642 /* If the previous instruction wasn't a store, there's nothing to
6643 worry about. */
15be625d
CM
6644 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6645 return 0;
6646
ab9794cf
RS
6647 /* If the instructions after the previous one are unknown, we have
6648 to assume the worst. */
6649 if (!insn)
15be625d
CM
6650 return 1;
6651
ab9794cf
RS
6652 /* Check whether we are dealing with three consecutive stores. */
6653 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
6654 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
15be625d
CM
6655 return 0;
6656
6657 /* If we don't know the relationship between the store addresses,
6658 assume the worst. */
ab9794cf 6659 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
15be625d
CM
6660 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
6661 return 1;
6662
6663 if (!fix_24k_record_store_info (&pos[0], insn)
6664 || !fix_24k_record_store_info (&pos[1], &hist[0])
6665 || !fix_24k_record_store_info (&pos[2], &hist[1]))
6666 return 1;
6667
6668 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
6669
6670 /* Pick a value of ALIGN and X such that all offsets are adjusted by
6671 X bytes and such that the base register + X is known to be aligned
6672 to align bytes. */
6673
6674 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
6675 align = 8;
6676 else
6677 {
6678 align = pos[0].align_to;
6679 base_offset = pos[0].off;
6680 for (i = 1; i < 3; i++)
6681 if (align < pos[i].align_to)
6682 {
6683 align = pos[i].align_to;
6684 base_offset = pos[i].off;
6685 }
6686 for (i = 0; i < 3; i++)
6687 pos[i].off -= base_offset;
6688 }
6689
6690 pos[0].off &= ~align + 1;
6691 pos[1].off &= ~align + 1;
6692 pos[2].off &= ~align + 1;
6693
6694 /* If any two stores write to the same chunk, they also write to the
6695 same doubleword. The offsets are still sorted at this point. */
6696 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
6697 return 0;
6698
6699 /* A range of at least 9 bytes is needed for the stores to be in
6700 non-overlapping doublewords. */
6701 if (pos[2].off - pos[0].off <= 8)
6702 return 0;
6703
6704 if (pos[2].off - pos[1].off >= 24
6705 || pos[1].off - pos[0].off >= 24
6706 || pos[2].off - pos[0].off >= 32)
6707 return 0;
6708
6709 return 1;
6710}
6711
71400594 6712/* Return the number of nops that would be needed if instruction INSN
91d6fa6a 6713 immediately followed the MAX_NOPS instructions given by HIST,
932d1a1b
RS
6714 where HIST[0] is the most recent instruction. Ignore hazards
6715 between INSN and the first IGNORE instructions in HIST.
6716
6717 If INSN is null, return the worse-case number of nops for any
6718 instruction. */
bdaaa2e1 6719
71400594 6720static int
932d1a1b 6721nops_for_insn (int ignore, const struct mips_cl_insn *hist,
71400594
RS
6722 const struct mips_cl_insn *insn)
6723{
6724 int i, nops, tmp_nops;
bdaaa2e1 6725
71400594 6726 nops = 0;
932d1a1b 6727 for (i = ignore; i < MAX_DELAY_NOPS; i++)
65b02341 6728 {
91d6fa6a 6729 tmp_nops = insns_between (hist + i, insn) - i;
65b02341
RS
6730 if (tmp_nops > nops)
6731 nops = tmp_nops;
6732 }
7d8e00cf 6733
df58fc94 6734 if (mips_fix_vr4130 && !mips_opts.micromips)
7d8e00cf 6735 {
932d1a1b 6736 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
7d8e00cf
RS
6737 if (tmp_nops > nops)
6738 nops = tmp_nops;
6739 }
6740
df58fc94 6741 if (mips_fix_24k && !mips_opts.micromips)
15be625d 6742 {
932d1a1b 6743 tmp_nops = nops_for_24k (ignore, hist, insn);
15be625d
CM
6744 if (tmp_nops > nops)
6745 nops = tmp_nops;
6746 }
6747
71400594
RS
6748 return nops;
6749}
252b5132 6750
71400594 6751/* The variable arguments provide NUM_INSNS extra instructions that
91d6fa6a 6752 might be added to HIST. Return the largest number of nops that
932d1a1b
RS
6753 would be needed after the extended sequence, ignoring hazards
6754 in the first IGNORE instructions. */
252b5132 6755
71400594 6756static int
932d1a1b
RS
6757nops_for_sequence (int num_insns, int ignore,
6758 const struct mips_cl_insn *hist, ...)
71400594
RS
6759{
6760 va_list args;
6761 struct mips_cl_insn buffer[MAX_NOPS];
6762 struct mips_cl_insn *cursor;
6763 int nops;
6764
91d6fa6a 6765 va_start (args, hist);
71400594 6766 cursor = buffer + num_insns;
91d6fa6a 6767 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
71400594
RS
6768 while (cursor > buffer)
6769 *--cursor = *va_arg (args, const struct mips_cl_insn *);
6770
932d1a1b 6771 nops = nops_for_insn (ignore, buffer, NULL);
71400594
RS
6772 va_end (args);
6773 return nops;
6774}
252b5132 6775
71400594
RS
6776/* Like nops_for_insn, but if INSN is a branch, take into account the
6777 worst-case delay for the branch target. */
252b5132 6778
71400594 6779static int
932d1a1b 6780nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
71400594
RS
6781 const struct mips_cl_insn *insn)
6782{
6783 int nops, tmp_nops;
60b63b72 6784
932d1a1b 6785 nops = nops_for_insn (ignore, hist, insn);
11625dd8 6786 if (delayed_branch_p (insn))
71400594 6787 {
932d1a1b 6788 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
14fe068b 6789 hist, insn, get_delay_slot_nop (insn));
71400594
RS
6790 if (tmp_nops > nops)
6791 nops = tmp_nops;
6792 }
11625dd8 6793 else if (compact_branch_p (insn))
71400594 6794 {
932d1a1b 6795 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
71400594
RS
6796 if (tmp_nops > nops)
6797 nops = tmp_nops;
6798 }
6799 return nops;
6800}
6801
c67a084a
NC
6802/* Fix NOP issue: Replace nops by "or at,at,zero". */
6803
6804static void
6805fix_loongson2f_nop (struct mips_cl_insn * ip)
6806{
df58fc94 6807 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
6808 if (strcmp (ip->insn_mo->name, "nop") == 0)
6809 ip->insn_opcode = LOONGSON2F_NOP_INSN;
6810}
6811
6812/* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
6813 jr target pc &= 'hffff_ffff_cfff_ffff. */
6814
6815static void
6816fix_loongson2f_jump (struct mips_cl_insn * ip)
6817{
df58fc94 6818 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
6819 if (strcmp (ip->insn_mo->name, "j") == 0
6820 || strcmp (ip->insn_mo->name, "jr") == 0
6821 || strcmp (ip->insn_mo->name, "jalr") == 0)
6822 {
6823 int sreg;
6824 expressionS ep;
6825
6826 if (! mips_opts.at)
6827 return;
6828
df58fc94 6829 sreg = EXTRACT_OPERAND (0, RS, *ip);
c67a084a
NC
6830 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
6831 return;
6832
6833 ep.X_op = O_constant;
6834 ep.X_add_number = 0xcfff0000;
6835 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
6836 ep.X_add_number = 0xffff;
6837 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
6838 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
6839 }
6840}
6841
6842static void
6843fix_loongson2f (struct mips_cl_insn * ip)
6844{
6845 if (mips_fix_loongson2f_nop)
6846 fix_loongson2f_nop (ip);
6847
6848 if (mips_fix_loongson2f_jump)
6849 fix_loongson2f_jump (ip);
6850}
6851
a4e06468
RS
6852/* IP is a branch that has a delay slot, and we need to fill it
6853 automatically. Return true if we can do that by swapping IP
e407c74b
NC
6854 with the previous instruction.
6855 ADDRESS_EXPR is an operand of the instruction to be used with
6856 RELOC_TYPE. */
a4e06468
RS
6857
6858static bfd_boolean
e407c74b 6859can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 6860 bfd_reloc_code_real_type *reloc_type)
a4e06468 6861{
2b0c8b40 6862 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
a4e06468 6863 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
9d5de888 6864 unsigned int fpr_read, prev_fpr_write;
a4e06468
RS
6865
6866 /* -O2 and above is required for this optimization. */
6867 if (mips_optimize < 2)
6868 return FALSE;
6869
6870 /* If we have seen .set volatile or .set nomove, don't optimize. */
6871 if (mips_opts.nomove)
6872 return FALSE;
6873
6874 /* We can't swap if the previous instruction's position is fixed. */
6875 if (history[0].fixed_p)
6876 return FALSE;
6877
6878 /* If the previous previous insn was in a .set noreorder, we can't
6879 swap. Actually, the MIPS assembler will swap in this situation.
6880 However, gcc configured -with-gnu-as will generate code like
6881
6882 .set noreorder
6883 lw $4,XXX
6884 .set reorder
6885 INSN
6886 bne $4,$0,foo
6887
6888 in which we can not swap the bne and INSN. If gcc is not configured
6889 -with-gnu-as, it does not output the .set pseudo-ops. */
6890 if (history[1].noreorder_p)
6891 return FALSE;
6892
87333bb7
MR
6893 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
6894 This means that the previous instruction was a 4-byte one anyhow. */
a4e06468
RS
6895 if (mips_opts.mips16 && history[0].fixp[0])
6896 return FALSE;
6897
6898 /* If the branch is itself the target of a branch, we can not swap.
6899 We cheat on this; all we check for is whether there is a label on
6900 this instruction. If there are any branches to anything other than
6901 a label, users must use .set noreorder. */
6902 if (seg_info (now_seg)->label_list)
6903 return FALSE;
6904
6905 /* If the previous instruction is in a variant frag other than this
2309ddf2 6906 branch's one, we cannot do the swap. This does not apply to
9301f9c3
MR
6907 MIPS16 code, which uses variant frags for different purposes. */
6908 if (!mips_opts.mips16
a4e06468
RS
6909 && history[0].frag
6910 && history[0].frag->fr_type == rs_machine_dependent)
6911 return FALSE;
6912
bcd530a7
RS
6913 /* We do not swap with instructions that cannot architecturally
6914 be placed in a branch delay slot, such as SYNC or ERET. We
6915 also refrain from swapping with a trap instruction, since it
6916 complicates trap handlers to have the trap instruction be in
6917 a delay slot. */
a4e06468 6918 prev_pinfo = history[0].insn_mo->pinfo;
bcd530a7 6919 if (prev_pinfo & INSN_NO_DELAY_SLOT)
a4e06468
RS
6920 return FALSE;
6921
6922 /* Check for conflicts between the branch and the instructions
6923 before the candidate delay slot. */
6924 if (nops_for_insn (0, history + 1, ip) > 0)
6925 return FALSE;
6926
6927 /* Check for conflicts between the swapped sequence and the
6928 target of the branch. */
6929 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
6930 return FALSE;
6931
6932 /* If the branch reads a register that the previous
6933 instruction sets, we can not swap. */
6934 gpr_read = gpr_read_mask (ip);
6935 prev_gpr_write = gpr_write_mask (&history[0]);
6936 if (gpr_read & prev_gpr_write)
6937 return FALSE;
6938
9d5de888
CF
6939 fpr_read = fpr_read_mask (ip);
6940 prev_fpr_write = fpr_write_mask (&history[0]);
6941 if (fpr_read & prev_fpr_write)
6942 return FALSE;
6943
a4e06468
RS
6944 /* If the branch writes a register that the previous
6945 instruction sets, we can not swap. */
6946 gpr_write = gpr_write_mask (ip);
6947 if (gpr_write & prev_gpr_write)
6948 return FALSE;
6949
6950 /* If the branch writes a register that the previous
6951 instruction reads, we can not swap. */
6952 prev_gpr_read = gpr_read_mask (&history[0]);
6953 if (gpr_write & prev_gpr_read)
6954 return FALSE;
6955
6956 /* If one instruction sets a condition code and the
6957 other one uses a condition code, we can not swap. */
6958 pinfo = ip->insn_mo->pinfo;
6959 if ((pinfo & INSN_READ_COND_CODE)
6960 && (prev_pinfo & INSN_WRITE_COND_CODE))
6961 return FALSE;
6962 if ((pinfo & INSN_WRITE_COND_CODE)
6963 && (prev_pinfo & INSN_READ_COND_CODE))
6964 return FALSE;
6965
6966 /* If the previous instruction uses the PC, we can not swap. */
2b0c8b40 6967 prev_pinfo2 = history[0].insn_mo->pinfo2;
26545944 6968 if (prev_pinfo2 & INSN2_READ_PC)
2b0c8b40 6969 return FALSE;
a4e06468 6970
df58fc94
RS
6971 /* If the previous instruction has an incorrect size for a fixed
6972 branch delay slot in microMIPS mode, we cannot swap. */
2309ddf2
MR
6973 pinfo2 = ip->insn_mo->pinfo2;
6974 if (mips_opts.micromips
6975 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
6976 && insn_length (history) != 2)
6977 return FALSE;
6978 if (mips_opts.micromips
6979 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
6980 && insn_length (history) != 4)
6981 return FALSE;
6982
e407c74b
NC
6983 /* On R5900 short loops need to be fixed by inserting a nop in
6984 the branch delay slots.
6985 A short loop can be terminated too early. */
6986 if (mips_opts.arch == CPU_R5900
6987 /* Check if instruction has a parameter, ignore "j $31". */
6988 && (address_expr != NULL)
6989 /* Parameter must be 16 bit. */
6990 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
6991 /* Branch to same segment. */
41065f5e 6992 && (S_GET_SEGMENT (address_expr->X_add_symbol) == now_seg)
e407c74b 6993 /* Branch to same code fragment. */
41065f5e 6994 && (symbol_get_frag (address_expr->X_add_symbol) == frag_now)
e407c74b 6995 /* Can only calculate branch offset if value is known. */
41065f5e 6996 && symbol_constant_p (address_expr->X_add_symbol)
e407c74b
NC
6997 /* Check if branch is really conditional. */
6998 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
6999 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
7000 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
7001 {
7002 int distance;
7003 /* Check if loop is shorter than 6 instructions including
7004 branch and delay slot. */
41065f5e 7005 distance = frag_now_fix () - S_GET_VALUE (address_expr->X_add_symbol);
e407c74b
NC
7006 if (distance <= 20)
7007 {
7008 int i;
7009 int rv;
7010
7011 rv = FALSE;
7012 /* When the loop includes branches or jumps,
7013 it is not a short loop. */
7014 for (i = 0; i < (distance / 4); i++)
7015 {
7016 if ((history[i].cleared_p)
41065f5e 7017 || delayed_branch_p (&history[i]))
e407c74b
NC
7018 {
7019 rv = TRUE;
7020 break;
7021 }
7022 }
535b785f 7023 if (!rv)
e407c74b
NC
7024 {
7025 /* Insert nop after branch to fix short loop. */
7026 return FALSE;
7027 }
7028 }
7029 }
7030
a4e06468
RS
7031 return TRUE;
7032}
7033
e407c74b
NC
7034/* Decide how we should add IP to the instruction stream.
7035 ADDRESS_EXPR is an operand of the instruction to be used with
7036 RELOC_TYPE. */
a4e06468
RS
7037
7038static enum append_method
e407c74b 7039get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 7040 bfd_reloc_code_real_type *reloc_type)
a4e06468 7041{
a4e06468
RS
7042 /* The relaxed version of a macro sequence must be inherently
7043 hazard-free. */
7044 if (mips_relax.sequence == 2)
7045 return APPEND_ADD;
7046
3b821a28 7047 /* We must not dabble with instructions in a ".set noreorder" block. */
a4e06468
RS
7048 if (mips_opts.noreorder)
7049 return APPEND_ADD;
7050
7051 /* Otherwise, it's our responsibility to fill branch delay slots. */
11625dd8 7052 if (delayed_branch_p (ip))
a4e06468 7053 {
e407c74b
NC
7054 if (!branch_likely_p (ip)
7055 && can_swap_branch_p (ip, address_expr, reloc_type))
a4e06468
RS
7056 return APPEND_SWAP;
7057
7058 if (mips_opts.mips16
7059 && ISA_SUPPORTS_MIPS16E
fc76e730 7060 && gpr_read_mask (ip) != 0)
a4e06468
RS
7061 return APPEND_ADD_COMPACT;
7062
7bd374a4
MR
7063 if (mips_opts.micromips
7064 && ((ip->insn_opcode & 0xffe0) == 0x4580
7065 || (!forced_insn_length
7066 && ((ip->insn_opcode & 0xfc00) == 0xcc00
7067 || (ip->insn_opcode & 0xdc00) == 0x8c00))
7068 || (ip->insn_opcode & 0xdfe00000) == 0x94000000
7069 || (ip->insn_opcode & 0xdc1f0000) == 0x94000000))
7070 return APPEND_ADD_COMPACT;
7071
a4e06468
RS
7072 return APPEND_ADD_WITH_NOP;
7073 }
7074
a4e06468
RS
7075 return APPEND_ADD;
7076}
7077
7bd374a4
MR
7078/* IP is an instruction whose opcode we have just changed, END points
7079 to the end of the opcode table processed. Point IP->insn_mo to the
7080 new opcode's definition. */
ceb94aa5
RS
7081
7082static void
7bd374a4 7083find_altered_opcode (struct mips_cl_insn *ip, const struct mips_opcode *end)
ceb94aa5 7084{
7bd374a4 7085 const struct mips_opcode *mo;
ceb94aa5 7086
ceb94aa5 7087 for (mo = ip->insn_mo; mo < end; mo++)
7bd374a4
MR
7088 if (mo->pinfo != INSN_MACRO
7089 && (ip->insn_opcode & mo->mask) == mo->match)
ceb94aa5
RS
7090 {
7091 ip->insn_mo = mo;
7092 return;
7093 }
7094 abort ();
7095}
7096
7bd374a4
MR
7097/* IP is a MIPS16 instruction whose opcode we have just changed.
7098 Point IP->insn_mo to the new opcode's definition. */
7099
7100static void
7101find_altered_mips16_opcode (struct mips_cl_insn *ip)
7102{
7103 find_altered_opcode (ip, &mips16_opcodes[bfd_mips16_num_opcodes]);
7104}
7105
7106/* IP is a microMIPS instruction whose opcode we have just changed.
7107 Point IP->insn_mo to the new opcode's definition. */
7108
7109static void
7110find_altered_micromips_opcode (struct mips_cl_insn *ip)
7111{
7112 find_altered_opcode (ip, &micromips_opcodes[bfd_micromips_num_opcodes]);
7113}
7114
df58fc94
RS
7115/* For microMIPS macros, we need to generate a local number label
7116 as the target of branches. */
7117#define MICROMIPS_LABEL_CHAR '\037'
7118static unsigned long micromips_target_label;
7119static char micromips_target_name[32];
7120
7121static char *
7122micromips_label_name (void)
7123{
7124 char *p = micromips_target_name;
7125 char symbol_name_temporary[24];
7126 unsigned long l;
7127 int i;
7128
7129 if (*p)
7130 return p;
7131
7132 i = 0;
7133 l = micromips_target_label;
7134#ifdef LOCAL_LABEL_PREFIX
7135 *p++ = LOCAL_LABEL_PREFIX;
7136#endif
7137 *p++ = 'L';
7138 *p++ = MICROMIPS_LABEL_CHAR;
7139 do
7140 {
7141 symbol_name_temporary[i++] = l % 10 + '0';
7142 l /= 10;
7143 }
7144 while (l != 0);
7145 while (i > 0)
7146 *p++ = symbol_name_temporary[--i];
7147 *p = '\0';
7148
7149 return micromips_target_name;
7150}
7151
7152static void
7153micromips_label_expr (expressionS *label_expr)
7154{
7155 label_expr->X_op = O_symbol;
7156 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
7157 label_expr->X_add_number = 0;
7158}
7159
7160static void
7161micromips_label_inc (void)
7162{
7163 micromips_target_label++;
7164 *micromips_target_name = '\0';
7165}
7166
7167static void
7168micromips_add_label (void)
7169{
7170 symbolS *s;
7171
7172 s = colon (micromips_label_name ());
7173 micromips_label_inc ();
f3ded42a 7174 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
df58fc94
RS
7175}
7176
7177/* If assembling microMIPS code, then return the microMIPS reloc
7178 corresponding to the requested one if any. Otherwise return
7179 the reloc unchanged. */
7180
7181static bfd_reloc_code_real_type
7182micromips_map_reloc (bfd_reloc_code_real_type reloc)
7183{
7184 static const bfd_reloc_code_real_type relocs[][2] =
7185 {
7186 /* Keep sorted incrementally by the left-hand key. */
7187 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
7188 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
7189 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
7190 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
7191 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
7192 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
7193 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
7194 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
7195 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
7196 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
7197 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
7198 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
7199 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
7200 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
7201 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
7202 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
7203 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
7204 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
7205 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
7206 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
7207 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
7208 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
7209 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
7210 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
7211 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
7212 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
7213 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
7214 };
7215 bfd_reloc_code_real_type r;
7216 size_t i;
7217
7218 if (!mips_opts.micromips)
7219 return reloc;
7220 for (i = 0; i < ARRAY_SIZE (relocs); i++)
7221 {
7222 r = relocs[i][0];
7223 if (r > reloc)
7224 return reloc;
7225 if (r == reloc)
7226 return relocs[i][1];
7227 }
7228 return reloc;
7229}
7230
b886a2ab
RS
7231/* Try to resolve relocation RELOC against constant OPERAND at assembly time.
7232 Return true on success, storing the resolved value in RESULT. */
7233
7234static bfd_boolean
7235calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
7236 offsetT *result)
7237{
7238 switch (reloc)
7239 {
7240 case BFD_RELOC_MIPS_HIGHEST:
7241 case BFD_RELOC_MICROMIPS_HIGHEST:
7242 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
7243 return TRUE;
7244
7245 case BFD_RELOC_MIPS_HIGHER:
7246 case BFD_RELOC_MICROMIPS_HIGHER:
7247 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
7248 return TRUE;
7249
7250 case BFD_RELOC_HI16_S:
41947d9e 7251 case BFD_RELOC_HI16_S_PCREL:
b886a2ab
RS
7252 case BFD_RELOC_MICROMIPS_HI16_S:
7253 case BFD_RELOC_MIPS16_HI16_S:
7254 *result = ((operand + 0x8000) >> 16) & 0xffff;
7255 return TRUE;
7256
7257 case BFD_RELOC_HI16:
7258 case BFD_RELOC_MICROMIPS_HI16:
7259 case BFD_RELOC_MIPS16_HI16:
7260 *result = (operand >> 16) & 0xffff;
7261 return TRUE;
7262
7263 case BFD_RELOC_LO16:
41947d9e 7264 case BFD_RELOC_LO16_PCREL:
b886a2ab
RS
7265 case BFD_RELOC_MICROMIPS_LO16:
7266 case BFD_RELOC_MIPS16_LO16:
7267 *result = operand & 0xffff;
7268 return TRUE;
7269
7270 case BFD_RELOC_UNUSED:
7271 *result = operand;
7272 return TRUE;
7273
7274 default:
7275 return FALSE;
7276 }
7277}
7278
71400594
RS
7279/* Output an instruction. IP is the instruction information.
7280 ADDRESS_EXPR is an operand of the instruction to be used with
df58fc94
RS
7281 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
7282 a macro expansion. */
71400594
RS
7283
7284static void
7285append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
df58fc94 7286 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
71400594 7287{
14fe068b 7288 unsigned long prev_pinfo2, pinfo;
71400594 7289 bfd_boolean relaxed_branch = FALSE;
a4e06468 7290 enum append_method method;
2309ddf2 7291 bfd_boolean relax32;
2b0c8b40 7292 int branch_disp;
71400594 7293
2309ddf2 7294 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
c67a084a
NC
7295 fix_loongson2f (ip);
7296
738f4d98 7297 file_ase_mips16 |= mips_opts.mips16;
df58fc94 7298 file_ase_micromips |= mips_opts.micromips;
738f4d98 7299
df58fc94 7300 prev_pinfo2 = history[0].insn_mo->pinfo2;
71400594 7301 pinfo = ip->insn_mo->pinfo;
df58fc94 7302
7bd374a4
MR
7303 /* Don't raise alarm about `nods' frags as they'll fill in the right
7304 kind of nop in relaxation if required. */
df58fc94
RS
7305 if (mips_opts.micromips
7306 && !expansionp
7bd374a4
MR
7307 && !(history[0].frag
7308 && history[0].frag->fr_type == rs_machine_dependent
7309 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
7310 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
df58fc94
RS
7311 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
7312 && micromips_insn_length (ip->insn_mo) != 2)
7313 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
7314 && micromips_insn_length (ip->insn_mo) != 4)))
1661c76c 7315 as_warn (_("wrong size instruction in a %u-bit branch delay slot"),
df58fc94 7316 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
71400594 7317
15be625d
CM
7318 if (address_expr == NULL)
7319 ip->complete_p = 1;
b886a2ab
RS
7320 else if (reloc_type[0] <= BFD_RELOC_UNUSED
7321 && reloc_type[1] == BFD_RELOC_UNUSED
7322 && reloc_type[2] == BFD_RELOC_UNUSED
15be625d
CM
7323 && address_expr->X_op == O_constant)
7324 {
15be625d
CM
7325 switch (*reloc_type)
7326 {
15be625d 7327 case BFD_RELOC_MIPS_JMP:
df58fc94
RS
7328 {
7329 int shift;
7330
17c6c9d9
MR
7331 /* Shift is 2, unusually, for microMIPS JALX. */
7332 shift = (mips_opts.micromips
7333 && strcmp (ip->insn_mo->name, "jalx") != 0) ? 1 : 2;
df58fc94
RS
7334 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7335 as_bad (_("jump to misaligned address (0x%lx)"),
7336 (unsigned long) address_expr->X_add_number);
7337 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7338 & 0x3ffffff);
335574df 7339 ip->complete_p = 1;
df58fc94 7340 }
15be625d
CM
7341 break;
7342
7343 case BFD_RELOC_MIPS16_JMP:
7344 if ((address_expr->X_add_number & 3) != 0)
7345 as_bad (_("jump to misaligned address (0x%lx)"),
7346 (unsigned long) address_expr->X_add_number);
7347 ip->insn_opcode |=
7348 (((address_expr->X_add_number & 0x7c0000) << 3)
7349 | ((address_expr->X_add_number & 0xf800000) >> 7)
7350 | ((address_expr->X_add_number & 0x3fffc) >> 2));
335574df 7351 ip->complete_p = 1;
15be625d
CM
7352 break;
7353
7354 case BFD_RELOC_16_PCREL_S2:
df58fc94
RS
7355 {
7356 int shift;
7357
7358 shift = mips_opts.micromips ? 1 : 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 (!mips_relax_branch)
7363 {
7364 if ((address_expr->X_add_number + (1 << (shift + 15)))
7365 & ~((1 << (shift + 16)) - 1))
7366 as_bad (_("branch address range overflow (0x%lx)"),
7367 (unsigned long) address_expr->X_add_number);
7368 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7369 & 0xffff);
7370 }
df58fc94 7371 }
15be625d
CM
7372 break;
7373
7361da2c
AB
7374 case BFD_RELOC_MIPS_21_PCREL_S2:
7375 {
7376 int shift;
7377
7378 shift = 2;
7379 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7380 as_bad (_("branch to misaligned address (0x%lx)"),
7381 (unsigned long) address_expr->X_add_number);
7382 if ((address_expr->X_add_number + (1 << (shift + 20)))
7383 & ~((1 << (shift + 21)) - 1))
7384 as_bad (_("branch address range overflow (0x%lx)"),
7385 (unsigned long) address_expr->X_add_number);
7386 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7387 & 0x1fffff);
7388 }
7389 break;
7390
7391 case BFD_RELOC_MIPS_26_PCREL_S2:
7392 {
7393 int shift;
7394
7395 shift = 2;
7396 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7397 as_bad (_("branch to misaligned address (0x%lx)"),
7398 (unsigned long) address_expr->X_add_number);
7399 if ((address_expr->X_add_number + (1 << (shift + 25)))
7400 & ~((1 << (shift + 26)) - 1))
7401 as_bad (_("branch address range overflow (0x%lx)"),
7402 (unsigned long) address_expr->X_add_number);
7403 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7404 & 0x3ffffff);
7405 }
7406 break;
7407
15be625d 7408 default:
b886a2ab
RS
7409 {
7410 offsetT value;
7411
7412 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
7413 &value))
7414 {
7415 ip->insn_opcode |= value & 0xffff;
7416 ip->complete_p = 1;
7417 }
7418 }
7419 break;
7420 }
15be625d
CM
7421 }
7422
71400594
RS
7423 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
7424 {
7425 /* There are a lot of optimizations we could do that we don't.
7426 In particular, we do not, in general, reorder instructions.
7427 If you use gcc with optimization, it will reorder
7428 instructions and generally do much more optimization then we
7429 do here; repeating all that work in the assembler would only
7430 benefit hand written assembly code, and does not seem worth
7431 it. */
7432 int nops = (mips_optimize == 0
932d1a1b
RS
7433 ? nops_for_insn (0, history, NULL)
7434 : nops_for_insn_or_target (0, history, ip));
71400594 7435 if (nops > 0)
252b5132
RH
7436 {
7437 fragS *old_frag;
7438 unsigned long old_frag_offset;
7439 int i;
252b5132
RH
7440
7441 old_frag = frag_now;
7442 old_frag_offset = frag_now_fix ();
7443
7444 for (i = 0; i < nops; i++)
14fe068b
RS
7445 add_fixed_insn (NOP_INSN);
7446 insert_into_history (0, nops, NOP_INSN);
252b5132
RH
7447
7448 if (listing)
7449 {
7450 listing_prev_line ();
7451 /* We may be at the start of a variant frag. In case we
7452 are, make sure there is enough space for the frag
7453 after the frags created by listing_prev_line. The
7454 argument to frag_grow here must be at least as large
7455 as the argument to all other calls to frag_grow in
7456 this file. We don't have to worry about being in the
7457 middle of a variant frag, because the variants insert
7458 all needed nop instructions themselves. */
7459 frag_grow (40);
7460 }
7461
462427c4 7462 mips_move_text_labels ();
252b5132
RH
7463
7464#ifndef NO_ECOFF_DEBUGGING
7465 if (ECOFF_DEBUGGING)
7466 ecoff_fix_loc (old_frag, old_frag_offset);
7467#endif
7468 }
71400594
RS
7469 }
7470 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
7471 {
932d1a1b
RS
7472 int nops;
7473
7474 /* Work out how many nops in prev_nop_frag are needed by IP,
7475 ignoring hazards generated by the first prev_nop_frag_since
7476 instructions. */
7477 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
9c2799c2 7478 gas_assert (nops <= prev_nop_frag_holds);
252b5132 7479
71400594
RS
7480 /* Enforce NOPS as a minimum. */
7481 if (nops > prev_nop_frag_required)
7482 prev_nop_frag_required = nops;
252b5132 7483
71400594
RS
7484 if (prev_nop_frag_holds == prev_nop_frag_required)
7485 {
7486 /* Settle for the current number of nops. Update the history
7487 accordingly (for the benefit of any future .set reorder code). */
7488 prev_nop_frag = NULL;
7489 insert_into_history (prev_nop_frag_since,
7490 prev_nop_frag_holds, NOP_INSN);
7491 }
7492 else
7493 {
7494 /* Allow this instruction to replace one of the nops that was
7495 tentatively added to prev_nop_frag. */
df58fc94 7496 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
71400594
RS
7497 prev_nop_frag_holds--;
7498 prev_nop_frag_since++;
252b5132
RH
7499 }
7500 }
7501
e407c74b 7502 method = get_append_method (ip, address_expr, reloc_type);
2b0c8b40 7503 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
a4e06468 7504
e410add4
RS
7505 dwarf2_emit_insn (0);
7506 /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
7507 so "move" the instruction address accordingly.
7508
7509 Also, it doesn't seem appropriate for the assembler to reorder .loc
7510 entries. If this instruction is a branch that we are going to swap
7511 with the previous instruction, the two instructions should be
7512 treated as a unit, and the debug information for both instructions
7513 should refer to the start of the branch sequence. Using the
7514 current position is certainly wrong when swapping a 32-bit branch
7515 and a 16-bit delay slot, since the current position would then be
7516 in the middle of a branch. */
7517 dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
58e2ea4d 7518
df58fc94
RS
7519 relax32 = (mips_relax_branch
7520 /* Don't try branch relaxation within .set nomacro, or within
7521 .set noat if we use $at for PIC computations. If it turns
7522 out that the branch was out-of-range, we'll get an error. */
7523 && !mips_opts.warn_about_macros
7524 && (mips_opts.at || mips_pic == NO_PIC)
3bf0dbfb
MR
7525 /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
7526 as they have no complementing branches. */
7527 && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
df58fc94
RS
7528
7529 if (!HAVE_CODE_COMPRESSION
7530 && address_expr
7531 && relax32
0b25d3e6 7532 && *reloc_type == BFD_RELOC_16_PCREL_S2
11625dd8 7533 && delayed_branch_p (ip))
4a6a3df4 7534 {
895921c9 7535 relaxed_branch = TRUE;
1e915849
RS
7536 add_relaxed_insn (ip, (relaxed_branch_length
7537 (NULL, NULL,
11625dd8
RS
7538 uncond_branch_p (ip) ? -1
7539 : branch_likely_p (ip) ? 1
1e915849
RS
7540 : 0)), 4,
7541 RELAX_BRANCH_ENCODE
ce8ad872 7542 (AT, mips_pic != NO_PIC,
11625dd8
RS
7543 uncond_branch_p (ip),
7544 branch_likely_p (ip),
1e915849
RS
7545 pinfo & INSN_WRITE_GPR_31,
7546 0),
7547 address_expr->X_add_symbol,
7548 address_expr->X_add_number);
4a6a3df4
AO
7549 *reloc_type = BFD_RELOC_UNUSED;
7550 }
df58fc94
RS
7551 else if (mips_opts.micromips
7552 && address_expr
7553 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
7554 || *reloc_type > BFD_RELOC_UNUSED)
40209cad
MR
7555 && (delayed_branch_p (ip) || compact_branch_p (ip))
7556 /* Don't try branch relaxation when users specify
7557 16-bit/32-bit instructions. */
7558 && !forced_insn_length)
df58fc94 7559 {
7bd374a4
MR
7560 bfd_boolean relax16 = (method != APPEND_ADD_COMPACT
7561 && *reloc_type > BFD_RELOC_UNUSED);
df58fc94 7562 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
11625dd8 7563 int uncond = uncond_branch_p (ip) ? -1 : 0;
7bd374a4
MR
7564 int compact = compact_branch_p (ip) || method == APPEND_ADD_COMPACT;
7565 int nods = method == APPEND_ADD_WITH_NOP;
df58fc94 7566 int al = pinfo & INSN_WRITE_GPR_31;
7bd374a4 7567 int length32 = nods ? 8 : 4;
df58fc94
RS
7568
7569 gas_assert (address_expr != NULL);
7570 gas_assert (!mips_relax.sequence);
7571
2b0c8b40 7572 relaxed_branch = TRUE;
7bd374a4
MR
7573 if (nods)
7574 method = APPEND_ADD;
7575 if (relax32)
7576 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
7577 add_relaxed_insn (ip, length32, relax16 ? 2 : 4,
8484fb75 7578 RELAX_MICROMIPS_ENCODE (type, AT, mips_opts.insn32,
ce8ad872 7579 mips_pic != NO_PIC,
7bd374a4 7580 uncond, compact, al, nods,
40209cad 7581 relax32, 0, 0),
df58fc94
RS
7582 address_expr->X_add_symbol,
7583 address_expr->X_add_number);
7584 *reloc_type = BFD_RELOC_UNUSED;
7585 }
7586 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
252b5132 7587 {
7fd53920
MR
7588 bfd_boolean require_unextended;
7589 bfd_boolean require_extended;
88a7ef16
MR
7590 symbolS *symbol;
7591 offsetT offset;
7592
7fd53920
MR
7593 if (forced_insn_length != 0)
7594 {
7595 require_unextended = forced_insn_length == 2;
7596 require_extended = forced_insn_length == 4;
7597 }
7598 else
7599 {
7600 require_unextended = (mips_opts.noautoextend
7601 && !mips_opcode_32bit_p (ip->insn_mo));
7602 require_extended = 0;
7603 }
7604
252b5132 7605 /* We need to set up a variant frag. */
df58fc94 7606 gas_assert (address_expr != NULL);
88a7ef16
MR
7607 /* Pass any `O_symbol' expression unchanged as an `expr_section'
7608 symbol created by `make_expr_symbol' may not get a necessary
7609 external relocation produced. */
7610 if (address_expr->X_op == O_symbol)
7611 {
7612 symbol = address_expr->X_add_symbol;
7613 offset = address_expr->X_add_number;
7614 }
7615 else
7616 {
7617 symbol = make_expr_symbol (address_expr);
82d808ed 7618 symbol_append (symbol, symbol_lastP, &symbol_rootP, &symbol_lastP);
88a7ef16
MR
7619 offset = 0;
7620 }
8507b6e7 7621 add_relaxed_insn (ip, 12, 0,
1e915849
RS
7622 RELAX_MIPS16_ENCODE
7623 (*reloc_type - BFD_RELOC_UNUSED,
25499ac7 7624 mips_opts.ase & ASE_MIPS16E2,
8507b6e7
MR
7625 mips_pic != NO_PIC,
7626 HAVE_32BIT_SYMBOLS,
7627 mips_opts.warn_about_macros,
7fd53920 7628 require_unextended, require_extended,
11625dd8 7629 delayed_branch_p (&history[0]),
1e915849 7630 history[0].mips16_absolute_jump_p),
88a7ef16 7631 symbol, offset);
252b5132 7632 }
5c04167a 7633 else if (mips_opts.mips16 && insn_length (ip) == 2)
9497f5ac 7634 {
11625dd8 7635 if (!delayed_branch_p (ip))
b8ee1a6e
DU
7636 /* Make sure there is enough room to swap this instruction with
7637 a following jump instruction. */
7638 frag_grow (6);
1e915849 7639 add_fixed_insn (ip);
252b5132
RH
7640 }
7641 else
7642 {
7643 if (mips_opts.mips16
7644 && mips_opts.noreorder
11625dd8 7645 && delayed_branch_p (&history[0]))
252b5132
RH
7646 as_warn (_("extended instruction in delay slot"));
7647
4d7206a2
RS
7648 if (mips_relax.sequence)
7649 {
7650 /* If we've reached the end of this frag, turn it into a variant
7651 frag and record the information for the instructions we've
7652 written so far. */
7653 if (frag_room () < 4)
7654 relax_close_frag ();
df58fc94 7655 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4d7206a2
RS
7656 }
7657
584892a6 7658 if (mips_relax.sequence != 2)
df58fc94
RS
7659 {
7660 if (mips_macro_warning.first_insn_sizes[0] == 0)
7661 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
7662 mips_macro_warning.sizes[0] += insn_length (ip);
7663 mips_macro_warning.insns[0]++;
7664 }
584892a6 7665 if (mips_relax.sequence != 1)
df58fc94
RS
7666 {
7667 if (mips_macro_warning.first_insn_sizes[1] == 0)
7668 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
7669 mips_macro_warning.sizes[1] += insn_length (ip);
7670 mips_macro_warning.insns[1]++;
7671 }
584892a6 7672
1e915849
RS
7673 if (mips_opts.mips16)
7674 {
7675 ip->fixed_p = 1;
7676 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
7677 }
7678 add_fixed_insn (ip);
252b5132
RH
7679 }
7680
9fe77896 7681 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
252b5132 7682 {
df58fc94 7683 bfd_reloc_code_real_type final_type[3];
2309ddf2 7684 reloc_howto_type *howto0;
9fe77896
RS
7685 reloc_howto_type *howto;
7686 int i;
34ce925e 7687
df58fc94
RS
7688 /* Perform any necessary conversion to microMIPS relocations
7689 and find out how many relocations there actually are. */
7690 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
7691 final_type[i] = micromips_map_reloc (reloc_type[i]);
7692
9fe77896
RS
7693 /* In a compound relocation, it is the final (outermost)
7694 operator that determines the relocated field. */
2309ddf2 7695 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
e8044f35
RS
7696 if (!howto)
7697 abort ();
2309ddf2
MR
7698
7699 if (i > 1)
7700 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
9fe77896
RS
7701 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
7702 bfd_get_reloc_size (howto),
7703 address_expr,
2309ddf2
MR
7704 howto0 && howto0->pc_relative,
7705 final_type[0]);
ce8ad872
MR
7706 /* Record non-PIC mode in `fx_tcbit2' for `md_apply_fix'. */
7707 ip->fixp[0]->fx_tcbit2 = mips_pic == NO_PIC;
9fe77896
RS
7708
7709 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
2309ddf2 7710 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
9fe77896
RS
7711 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
7712
7713 /* These relocations can have an addend that won't fit in
7714 4 octets for 64bit assembly. */
bad1aba3 7715 if (GPR_SIZE == 64
9fe77896
RS
7716 && ! howto->partial_inplace
7717 && (reloc_type[0] == BFD_RELOC_16
7718 || reloc_type[0] == BFD_RELOC_32
7719 || reloc_type[0] == BFD_RELOC_MIPS_JMP
7720 || reloc_type[0] == BFD_RELOC_GPREL16
7721 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
7722 || reloc_type[0] == BFD_RELOC_GPREL32
7723 || reloc_type[0] == BFD_RELOC_64
7724 || reloc_type[0] == BFD_RELOC_CTOR
7725 || reloc_type[0] == BFD_RELOC_MIPS_SUB
7726 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
7727 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
7728 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
7729 || reloc_type[0] == BFD_RELOC_MIPS_REL16
7730 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
7731 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
7732 || hi16_reloc_p (reloc_type[0])
7733 || lo16_reloc_p (reloc_type[0])))
7734 ip->fixp[0]->fx_no_overflow = 1;
7735
ddaf2c41
MR
7736 /* These relocations can have an addend that won't fit in 2 octets. */
7737 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
7738 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
7739 ip->fixp[0]->fx_no_overflow = 1;
7740
9fe77896
RS
7741 if (mips_relax.sequence)
7742 {
7743 if (mips_relax.first_fixup == 0)
7744 mips_relax.first_fixup = ip->fixp[0];
7745 }
7746 else if (reloc_needs_lo_p (*reloc_type))
7747 {
7748 struct mips_hi_fixup *hi_fixup;
7749
7750 /* Reuse the last entry if it already has a matching %lo. */
7751 hi_fixup = mips_hi_fixup_list;
7752 if (hi_fixup == 0
7753 || !fixup_has_matching_lo_p (hi_fixup->fixp))
4d7206a2 7754 {
325801bd 7755 hi_fixup = XNEW (struct mips_hi_fixup);
9fe77896
RS
7756 hi_fixup->next = mips_hi_fixup_list;
7757 mips_hi_fixup_list = hi_fixup;
4d7206a2 7758 }
9fe77896
RS
7759 hi_fixup->fixp = ip->fixp[0];
7760 hi_fixup->seg = now_seg;
7761 }
252b5132 7762
9fe77896
RS
7763 /* Add fixups for the second and third relocations, if given.
7764 Note that the ABI allows the second relocation to be
7765 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
7766 moment we only use RSS_UNDEF, but we could add support
7767 for the others if it ever becomes necessary. */
7768 for (i = 1; i < 3; i++)
7769 if (reloc_type[i] != BFD_RELOC_UNUSED)
7770 {
7771 ip->fixp[i] = fix_new (ip->frag, ip->where,
7772 ip->fixp[0]->fx_size, NULL, 0,
df58fc94 7773 FALSE, final_type[i]);
f6688943 7774
9fe77896
RS
7775 /* Use fx_tcbit to mark compound relocs. */
7776 ip->fixp[0]->fx_tcbit = 1;
7777 ip->fixp[i]->fx_tcbit = 1;
7778 }
252b5132 7779 }
252b5132
RH
7780
7781 /* Update the register mask information. */
4c260379
RS
7782 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
7783 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
252b5132 7784
a4e06468 7785 switch (method)
252b5132 7786 {
a4e06468
RS
7787 case APPEND_ADD:
7788 insert_into_history (0, 1, ip);
7789 break;
7790
7791 case APPEND_ADD_WITH_NOP:
14fe068b
RS
7792 {
7793 struct mips_cl_insn *nop;
7794
7795 insert_into_history (0, 1, ip);
7796 nop = get_delay_slot_nop (ip);
7797 add_fixed_insn (nop);
7798 insert_into_history (0, 1, nop);
7799 if (mips_relax.sequence)
7800 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
7801 }
a4e06468
RS
7802 break;
7803
7804 case APPEND_ADD_COMPACT:
7805 /* Convert MIPS16 jr/jalr into a "compact" jump. */
7bd374a4
MR
7806 if (mips_opts.mips16)
7807 {
7808 ip->insn_opcode |= 0x0080;
7809 find_altered_mips16_opcode (ip);
7810 }
7811 /* Convert microMIPS instructions. */
7812 else if (mips_opts.micromips)
7813 {
7814 /* jr16->jrc */
7815 if ((ip->insn_opcode & 0xffe0) == 0x4580)
7816 ip->insn_opcode |= 0x0020;
7817 /* b16->bc */
7818 else if ((ip->insn_opcode & 0xfc00) == 0xcc00)
7819 ip->insn_opcode = 0x40e00000;
7820 /* beqz16->beqzc, bnez16->bnezc */
7821 else if ((ip->insn_opcode & 0xdc00) == 0x8c00)
7822 {
7823 unsigned long regno;
7824
7825 regno = ip->insn_opcode >> MICROMIPSOP_SH_MD;
7826 regno &= MICROMIPSOP_MASK_MD;
7827 regno = micromips_to_32_reg_d_map[regno];
7828 ip->insn_opcode = (((ip->insn_opcode << 9) & 0x00400000)
7829 | (regno << MICROMIPSOP_SH_RS)
7830 | 0x40a00000) ^ 0x00400000;
7831 }
7832 /* beqz->beqzc, bnez->bnezc */
7833 else if ((ip->insn_opcode & 0xdfe00000) == 0x94000000)
7834 ip->insn_opcode = ((ip->insn_opcode & 0x001f0000)
7835 | ((ip->insn_opcode >> 7) & 0x00400000)
7836 | 0x40a00000) ^ 0x00400000;
7837 /* beq $0->beqzc, bne $0->bnezc */
7838 else if ((ip->insn_opcode & 0xdc1f0000) == 0x94000000)
7839 ip->insn_opcode = (((ip->insn_opcode >>
7840 (MICROMIPSOP_SH_RT - MICROMIPSOP_SH_RS))
7841 & (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS))
7842 | ((ip->insn_opcode >> 7) & 0x00400000)
7843 | 0x40a00000) ^ 0x00400000;
7844 else
7845 abort ();
7846 find_altered_micromips_opcode (ip);
7847 }
7848 else
7849 abort ();
a4e06468
RS
7850 install_insn (ip);
7851 insert_into_history (0, 1, ip);
7852 break;
7853
7854 case APPEND_SWAP:
7855 {
7856 struct mips_cl_insn delay = history[0];
99e7978b
MF
7857
7858 if (relaxed_branch || delay.frag != ip->frag)
a4e06468
RS
7859 {
7860 /* Add the delay slot instruction to the end of the
7861 current frag and shrink the fixed part of the
7862 original frag. If the branch occupies the tail of
7863 the latter, move it backwards to cover the gap. */
2b0c8b40 7864 delay.frag->fr_fix -= branch_disp;
a4e06468 7865 if (delay.frag == ip->frag)
2b0c8b40 7866 move_insn (ip, ip->frag, ip->where - branch_disp);
a4e06468
RS
7867 add_fixed_insn (&delay);
7868 }
7869 else
7870 {
5e35670b
MR
7871 /* If this is not a relaxed branch and we are in the
7872 same frag, then just swap the instructions. */
7873 move_insn (ip, delay.frag, delay.where);
7874 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
a4e06468
RS
7875 }
7876 history[0] = *ip;
7877 delay.fixed_p = 1;
7878 insert_into_history (0, 1, &delay);
7879 }
7880 break;
252b5132
RH
7881 }
7882
13408f1e 7883 /* If we have just completed an unconditional branch, clear the history. */
11625dd8
RS
7884 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
7885 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
e407c74b
NC
7886 {
7887 unsigned int i;
7888
79850f26 7889 mips_no_prev_insn ();
13408f1e 7890
e407c74b 7891 for (i = 0; i < ARRAY_SIZE (history); i++)
79850f26 7892 history[i].cleared_p = 1;
e407c74b
NC
7893 }
7894
df58fc94
RS
7895 /* We need to emit a label at the end of branch-likely macros. */
7896 if (emit_branch_likely_macro)
7897 {
7898 emit_branch_likely_macro = FALSE;
7899 micromips_add_label ();
7900 }
7901
252b5132
RH
7902 /* We just output an insn, so the next one doesn't have a label. */
7903 mips_clear_insn_labels ();
252b5132
RH
7904}
7905
e407c74b
NC
7906/* Forget that there was any previous instruction or label.
7907 When BRANCH is true, the branch history is also flushed. */
252b5132
RH
7908
7909static void
7d10b47d 7910mips_no_prev_insn (void)
252b5132 7911{
7d10b47d
RS
7912 prev_nop_frag = NULL;
7913 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
7914 mips_clear_insn_labels ();
7915}
7916
7d10b47d
RS
7917/* This function must be called before we emit something other than
7918 instructions. It is like mips_no_prev_insn except that it inserts
7919 any NOPS that might be needed by previous instructions. */
252b5132 7920
7d10b47d
RS
7921void
7922mips_emit_delays (void)
252b5132
RH
7923{
7924 if (! mips_opts.noreorder)
7925 {
932d1a1b 7926 int nops = nops_for_insn (0, history, NULL);
252b5132
RH
7927 if (nops > 0)
7928 {
7d10b47d
RS
7929 while (nops-- > 0)
7930 add_fixed_insn (NOP_INSN);
462427c4 7931 mips_move_text_labels ();
7d10b47d
RS
7932 }
7933 }
7934 mips_no_prev_insn ();
7935}
7936
7937/* Start a (possibly nested) noreorder block. */
7938
7939static void
7940start_noreorder (void)
7941{
7942 if (mips_opts.noreorder == 0)
7943 {
7944 unsigned int i;
7945 int nops;
7946
7947 /* None of the instructions before the .set noreorder can be moved. */
7948 for (i = 0; i < ARRAY_SIZE (history); i++)
7949 history[i].fixed_p = 1;
7950
7951 /* Insert any nops that might be needed between the .set noreorder
7952 block and the previous instructions. We will later remove any
7953 nops that turn out not to be needed. */
932d1a1b 7954 nops = nops_for_insn (0, history, NULL);
7d10b47d
RS
7955 if (nops > 0)
7956 {
7957 if (mips_optimize != 0)
252b5132
RH
7958 {
7959 /* Record the frag which holds the nop instructions, so
7960 that we can remove them if we don't need them. */
df58fc94 7961 frag_grow (nops * NOP_INSN_SIZE);
252b5132
RH
7962 prev_nop_frag = frag_now;
7963 prev_nop_frag_holds = nops;
7964 prev_nop_frag_required = 0;
7965 prev_nop_frag_since = 0;
7966 }
7967
7968 for (; nops > 0; --nops)
1e915849 7969 add_fixed_insn (NOP_INSN);
252b5132 7970
7d10b47d
RS
7971 /* Move on to a new frag, so that it is safe to simply
7972 decrease the size of prev_nop_frag. */
7973 frag_wane (frag_now);
7974 frag_new (0);
462427c4 7975 mips_move_text_labels ();
252b5132 7976 }
df58fc94 7977 mips_mark_labels ();
7d10b47d 7978 mips_clear_insn_labels ();
252b5132 7979 }
7d10b47d
RS
7980 mips_opts.noreorder++;
7981 mips_any_noreorder = 1;
7982}
252b5132 7983
7d10b47d 7984/* End a nested noreorder block. */
252b5132 7985
7d10b47d
RS
7986static void
7987end_noreorder (void)
7988{
7989 mips_opts.noreorder--;
7990 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
7991 {
7992 /* Commit to inserting prev_nop_frag_required nops and go back to
7993 handling nop insertion the .set reorder way. */
7994 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
df58fc94 7995 * NOP_INSN_SIZE);
7d10b47d
RS
7996 insert_into_history (prev_nop_frag_since,
7997 prev_nop_frag_required, NOP_INSN);
7998 prev_nop_frag = NULL;
7999 }
252b5132
RH
8000}
8001
97d87491
RS
8002/* Sign-extend 32-bit mode constants that have bit 31 set and all
8003 higher bits unset. */
8004
8005static void
8006normalize_constant_expr (expressionS *ex)
8007{
8008 if (ex->X_op == O_constant
8009 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
8010 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
8011 - 0x80000000);
8012}
8013
8014/* Sign-extend 32-bit mode address offsets that have bit 31 set and
8015 all higher bits unset. */
8016
8017static void
8018normalize_address_expr (expressionS *ex)
8019{
8020 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
8021 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
8022 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
8023 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
8024 - 0x80000000);
8025}
8026
8027/* Try to match TOKENS against OPCODE, storing the result in INSN.
8028 Return true if the match was successful.
8029
8030 OPCODE_EXTRA is a value that should be ORed into the opcode
8031 (used for VU0 channel suffixes, etc.). MORE_ALTS is true if
8032 there are more alternatives after OPCODE and SOFT_MATCH is
8033 as for mips_arg_info. */
8034
8035static bfd_boolean
8036match_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
8037 struct mips_operand_token *tokens, unsigned int opcode_extra,
60f20e8b 8038 bfd_boolean lax_match, bfd_boolean complete_p)
97d87491
RS
8039{
8040 const char *args;
8041 struct mips_arg_info arg;
8042 const struct mips_operand *operand;
8043 char c;
8044
8045 imm_expr.X_op = O_absent;
97d87491
RS
8046 offset_expr.X_op = O_absent;
8047 offset_reloc[0] = BFD_RELOC_UNUSED;
8048 offset_reloc[1] = BFD_RELOC_UNUSED;
8049 offset_reloc[2] = BFD_RELOC_UNUSED;
8050
8051 create_insn (insn, opcode);
60f20e8b
RS
8052 /* When no opcode suffix is specified, assume ".xyzw". */
8053 if ((opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0 && opcode_extra == 0)
8054 insn->insn_opcode |= 0xf << mips_vu0_channel_mask.lsb;
8055 else
8056 insn->insn_opcode |= opcode_extra;
97d87491
RS
8057 memset (&arg, 0, sizeof (arg));
8058 arg.insn = insn;
8059 arg.token = tokens;
8060 arg.argnum = 1;
8061 arg.last_regno = ILLEGAL_REG;
8062 arg.dest_regno = ILLEGAL_REG;
60f20e8b 8063 arg.lax_match = lax_match;
97d87491
RS
8064 for (args = opcode->args;; ++args)
8065 {
8066 if (arg.token->type == OT_END)
8067 {
8068 /* Handle unary instructions in which only one operand is given.
8069 The source is then the same as the destination. */
8070 if (arg.opnum == 1 && *args == ',')
8071 {
8072 operand = (mips_opts.micromips
8073 ? decode_micromips_operand (args + 1)
8074 : decode_mips_operand (args + 1));
8075 if (operand && mips_optional_operand_p (operand))
8076 {
8077 arg.token = tokens;
8078 arg.argnum = 1;
8079 continue;
8080 }
8081 }
8082
8083 /* Treat elided base registers as $0. */
8084 if (strcmp (args, "(b)") == 0)
8085 args += 3;
8086
8087 if (args[0] == '+')
8088 switch (args[1])
8089 {
8090 case 'K':
8091 case 'N':
8092 /* The register suffix is optional. */
8093 args += 2;
8094 break;
8095 }
8096
8097 /* Fail the match if there were too few operands. */
8098 if (*args)
8099 return FALSE;
8100
8101 /* Successful match. */
60f20e8b
RS
8102 if (!complete_p)
8103 return TRUE;
e3de51ce 8104 clear_insn_error ();
97d87491
RS
8105 if (arg.dest_regno == arg.last_regno
8106 && strncmp (insn->insn_mo->name, "jalr", 4) == 0)
8107 {
8108 if (arg.opnum == 2)
e3de51ce 8109 set_insn_error
1661c76c 8110 (0, _("source and destination must be different"));
97d87491 8111 else if (arg.last_regno == 31)
e3de51ce 8112 set_insn_error
1661c76c 8113 (0, _("a destination register must be supplied"));
97d87491 8114 }
173d3447
CF
8115 else if (arg.last_regno == 31
8116 && (strncmp (insn->insn_mo->name, "bltzal", 6) == 0
8117 || strncmp (insn->insn_mo->name, "bgezal", 6) == 0))
8118 set_insn_error (0, _("the source register must not be $31"));
97d87491
RS
8119 check_completed_insn (&arg);
8120 return TRUE;
8121 }
8122
8123 /* Fail the match if the line has too many operands. */
8124 if (*args == 0)
8125 return FALSE;
8126
8127 /* Handle characters that need to match exactly. */
8128 if (*args == '(' || *args == ')' || *args == ',')
8129 {
8130 if (match_char (&arg, *args))
8131 continue;
8132 return FALSE;
8133 }
8134 if (*args == '#')
8135 {
8136 ++args;
8137 if (arg.token->type == OT_DOUBLE_CHAR
8138 && arg.token->u.ch == *args)
8139 {
8140 ++arg.token;
8141 continue;
8142 }
8143 return FALSE;
8144 }
8145
8146 /* Handle special macro operands. Work out the properties of
8147 other operands. */
8148 arg.opnum += 1;
97d87491
RS
8149 switch (*args)
8150 {
7361da2c
AB
8151 case '-':
8152 switch (args[1])
8153 {
8154 case 'A':
8155 *offset_reloc = BFD_RELOC_MIPS_19_PCREL_S2;
8156 break;
8157
8158 case 'B':
8159 *offset_reloc = BFD_RELOC_MIPS_18_PCREL_S3;
8160 break;
8161 }
8162 break;
8163
97d87491
RS
8164 case '+':
8165 switch (args[1])
8166 {
97d87491
RS
8167 case 'i':
8168 *offset_reloc = BFD_RELOC_MIPS_JMP;
8169 break;
7361da2c
AB
8170
8171 case '\'':
8172 *offset_reloc = BFD_RELOC_MIPS_26_PCREL_S2;
8173 break;
8174
8175 case '\"':
8176 *offset_reloc = BFD_RELOC_MIPS_21_PCREL_S2;
8177 break;
97d87491
RS
8178 }
8179 break;
8180
97d87491 8181 case 'I':
1a00e612
RS
8182 if (!match_const_int (&arg, &imm_expr.X_add_number))
8183 return FALSE;
8184 imm_expr.X_op = O_constant;
bad1aba3 8185 if (GPR_SIZE == 32)
97d87491
RS
8186 normalize_constant_expr (&imm_expr);
8187 continue;
8188
8189 case 'A':
8190 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8191 {
8192 /* Assume that the offset has been elided and that what
8193 we saw was a base register. The match will fail later
8194 if that assumption turns out to be wrong. */
8195 offset_expr.X_op = O_constant;
8196 offset_expr.X_add_number = 0;
8197 }
97d87491 8198 else
1a00e612
RS
8199 {
8200 if (!match_expression (&arg, &offset_expr, offset_reloc))
8201 return FALSE;
8202 normalize_address_expr (&offset_expr);
8203 }
97d87491
RS
8204 continue;
8205
8206 case 'F':
8207 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8208 8, TRUE))
1a00e612 8209 return FALSE;
97d87491
RS
8210 continue;
8211
8212 case 'L':
8213 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8214 8, FALSE))
1a00e612 8215 return FALSE;
97d87491
RS
8216 continue;
8217
8218 case 'f':
8219 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8220 4, TRUE))
1a00e612 8221 return FALSE;
97d87491
RS
8222 continue;
8223
8224 case 'l':
8225 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8226 4, FALSE))
1a00e612 8227 return FALSE;
97d87491
RS
8228 continue;
8229
97d87491
RS
8230 case 'p':
8231 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8232 break;
8233
8234 case 'a':
8235 *offset_reloc = BFD_RELOC_MIPS_JMP;
8236 break;
8237
8238 case 'm':
8239 gas_assert (mips_opts.micromips);
8240 c = args[1];
8241 switch (c)
8242 {
8243 case 'D':
8244 case 'E':
8245 if (!forced_insn_length)
8246 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
8247 else if (c == 'D')
8248 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
8249 else
8250 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
8251 break;
8252 }
8253 break;
8254 }
8255
8256 operand = (mips_opts.micromips
8257 ? decode_micromips_operand (args)
8258 : decode_mips_operand (args));
8259 if (!operand)
8260 abort ();
8261
8262 /* Skip prefixes. */
7361da2c 8263 if (*args == '+' || *args == 'm' || *args == '-')
97d87491
RS
8264 args++;
8265
8266 if (mips_optional_operand_p (operand)
8267 && args[1] == ','
8268 && (arg.token[0].type != OT_REG
8269 || arg.token[1].type == OT_END))
8270 {
8271 /* Assume that the register has been elided and is the
8272 same as the first operand. */
8273 arg.token = tokens;
8274 arg.argnum = 1;
8275 }
8276
8277 if (!match_operand (&arg, operand))
8278 return FALSE;
8279 }
8280}
8281
8282/* Like match_insn, but for MIPS16. */
8283
8284static bfd_boolean
8285match_mips16_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
1a00e612 8286 struct mips_operand_token *tokens)
97d87491
RS
8287{
8288 const char *args;
8289 const struct mips_operand *operand;
8290 const struct mips_operand *ext_operand;
82d808ed 8291 bfd_boolean pcrel = FALSE;
7fd53920 8292 int required_insn_length;
97d87491
RS
8293 struct mips_arg_info arg;
8294 int relax_char;
8295
7fd53920
MR
8296 if (forced_insn_length)
8297 required_insn_length = forced_insn_length;
8298 else if (mips_opts.noautoextend && !mips_opcode_32bit_p (opcode))
8299 required_insn_length = 2;
8300 else
8301 required_insn_length = 0;
8302
97d87491
RS
8303 create_insn (insn, opcode);
8304 imm_expr.X_op = O_absent;
97d87491
RS
8305 offset_expr.X_op = O_absent;
8306 offset_reloc[0] = BFD_RELOC_UNUSED;
8307 offset_reloc[1] = BFD_RELOC_UNUSED;
8308 offset_reloc[2] = BFD_RELOC_UNUSED;
8309 relax_char = 0;
8310
8311 memset (&arg, 0, sizeof (arg));
8312 arg.insn = insn;
8313 arg.token = tokens;
8314 arg.argnum = 1;
8315 arg.last_regno = ILLEGAL_REG;
8316 arg.dest_regno = ILLEGAL_REG;
97d87491
RS
8317 relax_char = 0;
8318 for (args = opcode->args;; ++args)
8319 {
8320 int c;
8321
8322 if (arg.token->type == OT_END)
8323 {
8324 offsetT value;
8325
8326 /* Handle unary instructions in which only one operand is given.
8327 The source is then the same as the destination. */
8328 if (arg.opnum == 1 && *args == ',')
8329 {
8330 operand = decode_mips16_operand (args[1], FALSE);
8331 if (operand && mips_optional_operand_p (operand))
8332 {
8333 arg.token = tokens;
8334 arg.argnum = 1;
8335 continue;
8336 }
8337 }
8338
8339 /* Fail the match if there were too few operands. */
8340 if (*args)
8341 return FALSE;
8342
8343 /* Successful match. Stuff the immediate value in now, if
8344 we can. */
e3de51ce 8345 clear_insn_error ();
97d87491
RS
8346 if (opcode->pinfo == INSN_MACRO)
8347 {
8348 gas_assert (relax_char == 0 || relax_char == 'p');
8349 gas_assert (*offset_reloc == BFD_RELOC_UNUSED);
8350 }
8351 else if (relax_char
8352 && offset_expr.X_op == O_constant
82d808ed 8353 && !pcrel
97d87491
RS
8354 && calculate_reloc (*offset_reloc,
8355 offset_expr.X_add_number,
8356 &value))
8357 {
8358 mips16_immed (NULL, 0, relax_char, *offset_reloc, value,
7fd53920 8359 required_insn_length, &insn->insn_opcode);
97d87491
RS
8360 offset_expr.X_op = O_absent;
8361 *offset_reloc = BFD_RELOC_UNUSED;
8362 }
8363 else if (relax_char && *offset_reloc != BFD_RELOC_UNUSED)
8364 {
7fd53920 8365 if (required_insn_length == 2)
e3de51ce 8366 set_insn_error (0, _("invalid unextended operand value"));
25499ac7 8367 else if (!mips_opcode_32bit_p (opcode))
1da43acc
MR
8368 {
8369 forced_insn_length = 4;
8370 insn->insn_opcode |= MIPS16_EXTEND;
8371 }
97d87491
RS
8372 }
8373 else if (relax_char)
8374 *offset_reloc = (int) BFD_RELOC_UNUSED + relax_char;
8375
8376 check_completed_insn (&arg);
8377 return TRUE;
8378 }
8379
8380 /* Fail the match if the line has too many operands. */
8381 if (*args == 0)
8382 return FALSE;
8383
8384 /* Handle characters that need to match exactly. */
8385 if (*args == '(' || *args == ')' || *args == ',')
8386 {
8387 if (match_char (&arg, *args))
8388 continue;
8389 return FALSE;
8390 }
8391
8392 arg.opnum += 1;
8393 c = *args;
8394 switch (c)
8395 {
8396 case 'p':
8397 case 'q':
8398 case 'A':
8399 case 'B':
8400 case 'E':
25499ac7
MR
8401 case 'V':
8402 case 'u':
97d87491
RS
8403 relax_char = c;
8404 break;
8405
8406 case 'I':
1a00e612
RS
8407 if (!match_const_int (&arg, &imm_expr.X_add_number))
8408 return FALSE;
8409 imm_expr.X_op = O_constant;
bad1aba3 8410 if (GPR_SIZE == 32)
97d87491
RS
8411 normalize_constant_expr (&imm_expr);
8412 continue;
8413
8414 case 'a':
8415 case 'i':
8416 *offset_reloc = BFD_RELOC_MIPS16_JMP;
97d87491
RS
8417 break;
8418 }
8419
7fd53920 8420 operand = decode_mips16_operand (c, mips_opcode_32bit_p (opcode));
97d87491
RS
8421 if (!operand)
8422 abort ();
8423
82d808ed
MR
8424 if (operand->type == OP_PCREL)
8425 pcrel = TRUE;
8426 else
97d87491
RS
8427 {
8428 ext_operand = decode_mips16_operand (c, TRUE);
8429 if (operand != ext_operand)
8430 {
8431 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8432 {
8433 offset_expr.X_op = O_constant;
8434 offset_expr.X_add_number = 0;
8435 relax_char = c;
8436 continue;
8437 }
8438
1a7bf198 8439 if (!match_expression (&arg, &offset_expr, offset_reloc))
97d87491
RS
8440 return FALSE;
8441
8442 /* '8' is used for SLTI(U) and has traditionally not
8443 been allowed to take relocation operators. */
8444 if (offset_reloc[0] != BFD_RELOC_UNUSED
8445 && (ext_operand->size != 16 || c == '8'))
e295202f
MR
8446 {
8447 match_not_constant (&arg);
8448 return FALSE;
8449 }
97d87491 8450
c96425c5
MR
8451 if (offset_expr.X_op == O_big)
8452 {
8453 match_out_of_range (&arg);
8454 return FALSE;
8455 }
8456
97d87491
RS
8457 relax_char = c;
8458 continue;
8459 }
8460 }
8461
8462 if (mips_optional_operand_p (operand)
8463 && args[1] == ','
8464 && (arg.token[0].type != OT_REG
8465 || arg.token[1].type == OT_END))
8466 {
8467 /* Assume that the register has been elided and is the
8468 same as the first operand. */
8469 arg.token = tokens;
8470 arg.argnum = 1;
8471 }
8472
8473 if (!match_operand (&arg, operand))
8474 return FALSE;
8475 }
8476}
8477
60f20e8b
RS
8478/* Record that the current instruction is invalid for the current ISA. */
8479
8480static void
8481match_invalid_for_isa (void)
8482{
8483 set_insn_error_ss
1661c76c 8484 (0, _("opcode not supported on this processor: %s (%s)"),
60f20e8b
RS
8485 mips_cpu_info_from_arch (mips_opts.arch)->name,
8486 mips_cpu_info_from_isa (mips_opts.isa)->name);
8487}
8488
8489/* Try to match TOKENS against a series of opcode entries, starting at FIRST.
8490 Return true if a definite match or failure was found, storing any match
8491 in INSN. OPCODE_EXTRA is a value that should be ORed into the opcode
8492 (to handle things like VU0 suffixes). LAX_MATCH is true if we have already
8493 tried and failed to match under normal conditions and now want to try a
8494 more relaxed match. */
8495
8496static bfd_boolean
8497match_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8498 const struct mips_opcode *past, struct mips_operand_token *tokens,
8499 int opcode_extra, bfd_boolean lax_match)
8500{
8501 const struct mips_opcode *opcode;
8502 const struct mips_opcode *invalid_delay_slot;
8503 bfd_boolean seen_valid_for_isa, seen_valid_for_size;
8504
8505 /* Search for a match, ignoring alternatives that don't satisfy the
8506 current ISA or forced_length. */
8507 invalid_delay_slot = 0;
8508 seen_valid_for_isa = FALSE;
8509 seen_valid_for_size = FALSE;
8510 opcode = first;
8511 do
8512 {
8513 gas_assert (strcmp (opcode->name, first->name) == 0);
8514 if (is_opcode_valid (opcode))
8515 {
8516 seen_valid_for_isa = TRUE;
8517 if (is_size_valid (opcode))
8518 {
8519 bfd_boolean delay_slot_ok;
8520
8521 seen_valid_for_size = TRUE;
8522 delay_slot_ok = is_delay_slot_valid (opcode);
8523 if (match_insn (insn, opcode, tokens, opcode_extra,
8524 lax_match, delay_slot_ok))
8525 {
8526 if (!delay_slot_ok)
8527 {
8528 if (!invalid_delay_slot)
8529 invalid_delay_slot = opcode;
8530 }
8531 else
8532 return TRUE;
8533 }
8534 }
8535 }
8536 ++opcode;
8537 }
8538 while (opcode < past && strcmp (opcode->name, first->name) == 0);
8539
8540 /* If the only matches we found had the wrong length for the delay slot,
8541 pick the first such match. We'll issue an appropriate warning later. */
8542 if (invalid_delay_slot)
8543 {
8544 if (match_insn (insn, invalid_delay_slot, tokens, opcode_extra,
8545 lax_match, TRUE))
8546 return TRUE;
8547 abort ();
8548 }
8549
8550 /* Handle the case where we didn't try to match an instruction because
8551 all the alternatives were incompatible with the current ISA. */
8552 if (!seen_valid_for_isa)
8553 {
8554 match_invalid_for_isa ();
8555 return TRUE;
8556 }
8557
8558 /* Handle the case where we didn't try to match an instruction because
8559 all the alternatives were of the wrong size. */
8560 if (!seen_valid_for_size)
8561 {
8562 if (mips_opts.insn32)
1661c76c 8563 set_insn_error (0, _("opcode not supported in the `insn32' mode"));
60f20e8b
RS
8564 else
8565 set_insn_error_i
1661c76c 8566 (0, _("unrecognized %d-bit version of microMIPS opcode"),
60f20e8b
RS
8567 8 * forced_insn_length);
8568 return TRUE;
8569 }
8570
8571 return FALSE;
8572}
8573
8574/* Like match_insns, but for MIPS16. */
8575
8576static bfd_boolean
8577match_mips16_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8578 struct mips_operand_token *tokens)
8579{
8580 const struct mips_opcode *opcode;
8581 bfd_boolean seen_valid_for_isa;
7fd53920 8582 bfd_boolean seen_valid_for_size;
60f20e8b
RS
8583
8584 /* Search for a match, ignoring alternatives that don't satisfy the
8585 current ISA. There are no separate entries for extended forms so
8586 we deal with forced_length later. */
8587 seen_valid_for_isa = FALSE;
7fd53920 8588 seen_valid_for_size = FALSE;
60f20e8b
RS
8589 opcode = first;
8590 do
8591 {
8592 gas_assert (strcmp (opcode->name, first->name) == 0);
8593 if (is_opcode_valid_16 (opcode))
8594 {
8595 seen_valid_for_isa = TRUE;
7fd53920
MR
8596 if (is_size_valid_16 (opcode))
8597 {
8598 seen_valid_for_size = TRUE;
8599 if (match_mips16_insn (insn, opcode, tokens))
8600 return TRUE;
8601 }
60f20e8b
RS
8602 }
8603 ++opcode;
8604 }
8605 while (opcode < &mips16_opcodes[bfd_mips16_num_opcodes]
8606 && strcmp (opcode->name, first->name) == 0);
8607
8608 /* Handle the case where we didn't try to match an instruction because
8609 all the alternatives were incompatible with the current ISA. */
8610 if (!seen_valid_for_isa)
8611 {
8612 match_invalid_for_isa ();
8613 return TRUE;
8614 }
8615
7fd53920
MR
8616 /* Handle the case where we didn't try to match an instruction because
8617 all the alternatives were of the wrong size. */
8618 if (!seen_valid_for_size)
8619 {
8620 if (forced_insn_length == 2)
8621 set_insn_error
8622 (0, _("unrecognized unextended version of MIPS16 opcode"));
8623 else
8624 set_insn_error
8625 (0, _("unrecognized extended version of MIPS16 opcode"));
8626 return TRUE;
8627 }
8628
60f20e8b
RS
8629 return FALSE;
8630}
8631
584892a6
RS
8632/* Set up global variables for the start of a new macro. */
8633
8634static void
8635macro_start (void)
8636{
8637 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
df58fc94
RS
8638 memset (&mips_macro_warning.first_insn_sizes, 0,
8639 sizeof (mips_macro_warning.first_insn_sizes));
8640 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
584892a6 8641 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
11625dd8 8642 && delayed_branch_p (&history[0]));
7bd374a4
MR
8643 if (history[0].frag
8644 && history[0].frag->fr_type == rs_machine_dependent
8645 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
8646 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
8647 mips_macro_warning.delay_slot_length = 0;
8648 else
8649 switch (history[0].insn_mo->pinfo2
8650 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
8651 {
8652 case INSN2_BRANCH_DELAY_32BIT:
8653 mips_macro_warning.delay_slot_length = 4;
8654 break;
8655 case INSN2_BRANCH_DELAY_16BIT:
8656 mips_macro_warning.delay_slot_length = 2;
8657 break;
8658 default:
8659 mips_macro_warning.delay_slot_length = 0;
8660 break;
8661 }
df58fc94 8662 mips_macro_warning.first_frag = NULL;
584892a6
RS
8663}
8664
df58fc94
RS
8665/* Given that a macro is longer than one instruction or of the wrong size,
8666 return the appropriate warning for it. Return null if no warning is
8667 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
8668 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
8669 and RELAX_NOMACRO. */
584892a6
RS
8670
8671static const char *
8672macro_warning (relax_substateT subtype)
8673{
8674 if (subtype & RELAX_DELAY_SLOT)
1661c76c 8675 return _("macro instruction expanded into multiple instructions"
584892a6
RS
8676 " in a branch delay slot");
8677 else if (subtype & RELAX_NOMACRO)
1661c76c 8678 return _("macro instruction expanded into multiple instructions");
df58fc94
RS
8679 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
8680 | RELAX_DELAY_SLOT_SIZE_SECOND))
8681 return ((subtype & RELAX_DELAY_SLOT_16BIT)
1661c76c 8682 ? _("macro instruction expanded into a wrong size instruction"
df58fc94 8683 " in a 16-bit branch delay slot")
1661c76c 8684 : _("macro instruction expanded into a wrong size instruction"
df58fc94 8685 " in a 32-bit branch delay slot"));
584892a6
RS
8686 else
8687 return 0;
8688}
8689
8690/* Finish up a macro. Emit warnings as appropriate. */
8691
8692static void
8693macro_end (void)
8694{
df58fc94
RS
8695 /* Relaxation warning flags. */
8696 relax_substateT subtype = 0;
8697
8698 /* Check delay slot size requirements. */
8699 if (mips_macro_warning.delay_slot_length == 2)
8700 subtype |= RELAX_DELAY_SLOT_16BIT;
8701 if (mips_macro_warning.delay_slot_length != 0)
584892a6 8702 {
df58fc94
RS
8703 if (mips_macro_warning.delay_slot_length
8704 != mips_macro_warning.first_insn_sizes[0])
8705 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
8706 if (mips_macro_warning.delay_slot_length
8707 != mips_macro_warning.first_insn_sizes[1])
8708 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
8709 }
584892a6 8710
df58fc94
RS
8711 /* Check instruction count requirements. */
8712 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
8713 {
8714 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
584892a6
RS
8715 subtype |= RELAX_SECOND_LONGER;
8716 if (mips_opts.warn_about_macros)
8717 subtype |= RELAX_NOMACRO;
8718 if (mips_macro_warning.delay_slot_p)
8719 subtype |= RELAX_DELAY_SLOT;
df58fc94 8720 }
584892a6 8721
df58fc94
RS
8722 /* If both alternatives fail to fill a delay slot correctly,
8723 emit the warning now. */
8724 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
8725 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
8726 {
8727 relax_substateT s;
8728 const char *msg;
8729
8730 s = subtype & (RELAX_DELAY_SLOT_16BIT
8731 | RELAX_DELAY_SLOT_SIZE_FIRST
8732 | RELAX_DELAY_SLOT_SIZE_SECOND);
8733 msg = macro_warning (s);
8734 if (msg != NULL)
8735 as_warn ("%s", msg);
8736 subtype &= ~s;
8737 }
8738
8739 /* If both implementations are longer than 1 instruction, then emit the
8740 warning now. */
8741 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
8742 {
8743 relax_substateT s;
8744 const char *msg;
8745
8746 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
8747 msg = macro_warning (s);
8748 if (msg != NULL)
8749 as_warn ("%s", msg);
8750 subtype &= ~s;
584892a6 8751 }
df58fc94
RS
8752
8753 /* If any flags still set, then one implementation might need a warning
8754 and the other either will need one of a different kind or none at all.
8755 Pass any remaining flags over to relaxation. */
8756 if (mips_macro_warning.first_frag != NULL)
8757 mips_macro_warning.first_frag->fr_subtype |= subtype;
584892a6
RS
8758}
8759
df58fc94
RS
8760/* Instruction operand formats used in macros that vary between
8761 standard MIPS and microMIPS code. */
8762
833794fc 8763static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
df58fc94
RS
8764static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
8765static const char * const jalr_fmt[2] = { "d,s", "t,s" };
8766static const char * const lui_fmt[2] = { "t,u", "s,u" };
8767static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
833794fc 8768static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
df58fc94
RS
8769static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
8770static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
8771
833794fc 8772#define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
7361da2c
AB
8773#define COP12_FMT (ISA_IS_R6 (mips_opts.isa) ? "E,+:(d)" \
8774 : cop12_fmt[mips_opts.micromips])
df58fc94
RS
8775#define JALR_FMT (jalr_fmt[mips_opts.micromips])
8776#define LUI_FMT (lui_fmt[mips_opts.micromips])
8777#define MEM12_FMT (mem12_fmt[mips_opts.micromips])
7361da2c
AB
8778#define LL_SC_FMT (ISA_IS_R6 (mips_opts.isa) ? "t,+j(b)" \
8779 : mem12_fmt[mips_opts.micromips])
833794fc 8780#define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
df58fc94
RS
8781#define SHFT_FMT (shft_fmt[mips_opts.micromips])
8782#define TRAP_FMT (trap_fmt[mips_opts.micromips])
8783
6e1304d8
RS
8784/* Read a macro's relocation codes from *ARGS and store them in *R.
8785 The first argument in *ARGS will be either the code for a single
8786 relocation or -1 followed by the three codes that make up a
8787 composite relocation. */
8788
8789static void
8790macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
8791{
8792 int i, next;
8793
8794 next = va_arg (*args, int);
8795 if (next >= 0)
8796 r[0] = (bfd_reloc_code_real_type) next;
8797 else
f2ae14a1
RS
8798 {
8799 for (i = 0; i < 3; i++)
8800 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
8801 /* This function is only used for 16-bit relocation fields.
8802 To make the macro code simpler, treat an unrelocated value
8803 in the same way as BFD_RELOC_LO16. */
8804 if (r[0] == BFD_RELOC_UNUSED)
8805 r[0] = BFD_RELOC_LO16;
8806 }
6e1304d8
RS
8807}
8808
252b5132
RH
8809/* Build an instruction created by a macro expansion. This is passed
8810 a pointer to the count of instructions created so far, an
8811 expression, the name of the instruction to build, an operand format
8812 string, and corresponding arguments. */
8813
252b5132 8814static void
67c0d1eb 8815macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 8816{
df58fc94 8817 const struct mips_opcode *mo = NULL;
f6688943 8818 bfd_reloc_code_real_type r[3];
df58fc94 8819 const struct mips_opcode *amo;
e077a1c8 8820 const struct mips_operand *operand;
df58fc94
RS
8821 struct hash_control *hash;
8822 struct mips_cl_insn insn;
252b5132 8823 va_list args;
e077a1c8 8824 unsigned int uval;
252b5132 8825
252b5132 8826 va_start (args, fmt);
252b5132 8827
252b5132
RH
8828 if (mips_opts.mips16)
8829 {
03ea81db 8830 mips16_macro_build (ep, name, fmt, &args);
252b5132
RH
8831 va_end (args);
8832 return;
8833 }
8834
f6688943
TS
8835 r[0] = BFD_RELOC_UNUSED;
8836 r[1] = BFD_RELOC_UNUSED;
8837 r[2] = BFD_RELOC_UNUSED;
df58fc94
RS
8838 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
8839 amo = (struct mips_opcode *) hash_find (hash, name);
8840 gas_assert (amo);
8841 gas_assert (strcmp (name, amo->name) == 0);
1e915849 8842
df58fc94 8843 do
8b082fb1
TS
8844 {
8845 /* Search until we get a match for NAME. It is assumed here that
df58fc94 8846 macros will never generate MDMX, MIPS-3D, or MT instructions.
33eaf5de 8847 We try to match an instruction that fulfills the branch delay
df58fc94
RS
8848 slot instruction length requirement (if any) of the previous
8849 instruction. While doing this we record the first instruction
8850 seen that matches all the other conditions and use it anyway
8851 if the requirement cannot be met; we will issue an appropriate
8852 warning later on. */
8853 if (strcmp (fmt, amo->args) == 0
8854 && amo->pinfo != INSN_MACRO
8855 && is_opcode_valid (amo)
8856 && is_size_valid (amo))
8857 {
8858 if (is_delay_slot_valid (amo))
8859 {
8860 mo = amo;
8861 break;
8862 }
8863 else if (!mo)
8864 mo = amo;
8865 }
8b082fb1 8866
df58fc94
RS
8867 ++amo;
8868 gas_assert (amo->name);
252b5132 8869 }
df58fc94 8870 while (strcmp (name, amo->name) == 0);
252b5132 8871
df58fc94 8872 gas_assert (mo);
1e915849 8873 create_insn (&insn, mo);
e077a1c8 8874 for (; *fmt; ++fmt)
252b5132 8875 {
e077a1c8 8876 switch (*fmt)
252b5132 8877 {
252b5132
RH
8878 case ',':
8879 case '(':
8880 case ')':
252b5132 8881 case 'z':
e077a1c8 8882 break;
252b5132
RH
8883
8884 case 'i':
8885 case 'j':
6e1304d8 8886 macro_read_relocs (&args, r);
9c2799c2 8887 gas_assert (*r == BFD_RELOC_GPREL16
e391c024
RS
8888 || *r == BFD_RELOC_MIPS_HIGHER
8889 || *r == BFD_RELOC_HI16_S
8890 || *r == BFD_RELOC_LO16
14c80123
MR
8891 || *r == BFD_RELOC_MIPS_GOT_OFST
8892 || (mips_opts.micromips
8893 && (*r == BFD_RELOC_16
8894 || *r == BFD_RELOC_MIPS_GOT16
8895 || *r == BFD_RELOC_MIPS_CALL16
8896 || *r == BFD_RELOC_MIPS_GOT_HI16
8897 || *r == BFD_RELOC_MIPS_GOT_LO16
8898 || *r == BFD_RELOC_MIPS_CALL_HI16
8899 || *r == BFD_RELOC_MIPS_CALL_LO16
8900 || *r == BFD_RELOC_MIPS_SUB
8901 || *r == BFD_RELOC_MIPS_GOT_PAGE
8902 || *r == BFD_RELOC_MIPS_HIGHEST
8903 || *r == BFD_RELOC_MIPS_GOT_DISP
8904 || *r == BFD_RELOC_MIPS_TLS_GD
8905 || *r == BFD_RELOC_MIPS_TLS_LDM
8906 || *r == BFD_RELOC_MIPS_TLS_DTPREL_HI16
8907 || *r == BFD_RELOC_MIPS_TLS_DTPREL_LO16
8908 || *r == BFD_RELOC_MIPS_TLS_GOTTPREL
8909 || *r == BFD_RELOC_MIPS_TLS_TPREL_HI16
8910 || *r == BFD_RELOC_MIPS_TLS_TPREL_LO16)));
e077a1c8 8911 break;
e391c024
RS
8912
8913 case 'o':
8914 macro_read_relocs (&args, r);
e077a1c8 8915 break;
252b5132
RH
8916
8917 case 'u':
6e1304d8 8918 macro_read_relocs (&args, r);
9c2799c2 8919 gas_assert (ep != NULL
90ecf173
MR
8920 && (ep->X_op == O_constant
8921 || (ep->X_op == O_symbol
8922 && (*r == BFD_RELOC_MIPS_HIGHEST
8923 || *r == BFD_RELOC_HI16_S
8924 || *r == BFD_RELOC_HI16
8925 || *r == BFD_RELOC_GPREL16
8926 || *r == BFD_RELOC_MIPS_GOT_HI16
8927 || *r == BFD_RELOC_MIPS_CALL_HI16))));
e077a1c8 8928 break;
252b5132
RH
8929
8930 case 'p':
9c2799c2 8931 gas_assert (ep != NULL);
bad36eac 8932
252b5132
RH
8933 /*
8934 * This allows macro() to pass an immediate expression for
8935 * creating short branches without creating a symbol.
bad36eac
DJ
8936 *
8937 * We don't allow branch relaxation for these branches, as
8938 * they should only appear in ".set nomacro" anyway.
252b5132
RH
8939 */
8940 if (ep->X_op == O_constant)
8941 {
df58fc94
RS
8942 /* For microMIPS we always use relocations for branches.
8943 So we should not resolve immediate values. */
8944 gas_assert (!mips_opts.micromips);
8945
bad36eac
DJ
8946 if ((ep->X_add_number & 3) != 0)
8947 as_bad (_("branch to misaligned address (0x%lx)"),
8948 (unsigned long) ep->X_add_number);
8949 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
8950 as_bad (_("branch address range overflow (0x%lx)"),
8951 (unsigned long) ep->X_add_number);
252b5132
RH
8952 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
8953 ep = NULL;
8954 }
8955 else
0b25d3e6 8956 *r = BFD_RELOC_16_PCREL_S2;
e077a1c8 8957 break;
252b5132
RH
8958
8959 case 'a':
9c2799c2 8960 gas_assert (ep != NULL);
f6688943 8961 *r = BFD_RELOC_MIPS_JMP;
e077a1c8 8962 break;
d43b4baf 8963
252b5132 8964 default:
e077a1c8
RS
8965 operand = (mips_opts.micromips
8966 ? decode_micromips_operand (fmt)
8967 : decode_mips_operand (fmt));
8968 if (!operand)
8969 abort ();
8970
8971 uval = va_arg (args, int);
8972 if (operand->type == OP_CLO_CLZ_DEST)
8973 uval |= (uval << 5);
8974 insn_insert_operand (&insn, operand, uval);
8975
7361da2c 8976 if (*fmt == '+' || *fmt == 'm' || *fmt == '-')
e077a1c8
RS
8977 ++fmt;
8978 break;
252b5132 8979 }
252b5132
RH
8980 }
8981 va_end (args);
9c2799c2 8982 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 8983
df58fc94 8984 append_insn (&insn, ep, r, TRUE);
252b5132
RH
8985}
8986
8987static void
67c0d1eb 8988mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
03ea81db 8989 va_list *args)
252b5132 8990{
1e915849 8991 struct mips_opcode *mo;
252b5132 8992 struct mips_cl_insn insn;
e077a1c8 8993 const struct mips_operand *operand;
f6688943
TS
8994 bfd_reloc_code_real_type r[3]
8995 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 8996
1e915849 8997 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
9c2799c2
NC
8998 gas_assert (mo);
8999 gas_assert (strcmp (name, mo->name) == 0);
252b5132 9000
1e915849 9001 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 9002 {
1e915849 9003 ++mo;
9c2799c2
NC
9004 gas_assert (mo->name);
9005 gas_assert (strcmp (name, mo->name) == 0);
252b5132
RH
9006 }
9007
1e915849 9008 create_insn (&insn, mo);
e077a1c8 9009 for (; *fmt; ++fmt)
252b5132
RH
9010 {
9011 int c;
9012
e077a1c8 9013 c = *fmt;
252b5132
RH
9014 switch (c)
9015 {
252b5132
RH
9016 case ',':
9017 case '(':
9018 case ')':
e077a1c8 9019 break;
252b5132 9020
d8722d76 9021 case '.':
252b5132
RH
9022 case 'S':
9023 case 'P':
9024 case 'R':
e077a1c8 9025 break;
252b5132
RH
9026
9027 case '<':
252b5132 9028 case '5':
d8722d76 9029 case 'F':
252b5132
RH
9030 case 'H':
9031 case 'W':
9032 case 'D':
9033 case 'j':
9034 case '8':
9035 case 'V':
9036 case 'C':
9037 case 'U':
9038 case 'k':
9039 case 'K':
9040 case 'p':
9041 case 'q':
9042 {
b886a2ab
RS
9043 offsetT value;
9044
9c2799c2 9045 gas_assert (ep != NULL);
252b5132
RH
9046
9047 if (ep->X_op != O_constant)
874e8986 9048 *r = (int) BFD_RELOC_UNUSED + c;
b886a2ab 9049 else if (calculate_reloc (*r, ep->X_add_number, &value))
252b5132 9050 {
b886a2ab 9051 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
252b5132 9052 ep = NULL;
f6688943 9053 *r = BFD_RELOC_UNUSED;
252b5132
RH
9054 }
9055 }
e077a1c8 9056 break;
252b5132 9057
e077a1c8
RS
9058 default:
9059 operand = decode_mips16_operand (c, FALSE);
9060 if (!operand)
9061 abort ();
252b5132 9062
4a06e5a2 9063 insn_insert_operand (&insn, operand, va_arg (*args, int));
e077a1c8
RS
9064 break;
9065 }
252b5132
RH
9066 }
9067
9c2799c2 9068 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 9069
df58fc94 9070 append_insn (&insn, ep, r, TRUE);
252b5132
RH
9071}
9072
438c16b8
TS
9073/*
9074 * Generate a "jalr" instruction with a relocation hint to the called
9075 * function. This occurs in NewABI PIC code.
9076 */
9077static void
df58fc94 9078macro_build_jalr (expressionS *ep, int cprestore)
438c16b8 9079{
df58fc94
RS
9080 static const bfd_reloc_code_real_type jalr_relocs[2]
9081 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
9082 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
9083 const char *jalr;
685736be 9084 char *f = NULL;
b34976b6 9085
1180b5a4 9086 if (MIPS_JALR_HINT_P (ep))
f21f8242 9087 {
cc3d92a5 9088 frag_grow (8);
f21f8242
AO
9089 f = frag_more (0);
9090 }
2906b037 9091 if (mips_opts.micromips)
df58fc94 9092 {
833794fc
MR
9093 jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
9094 ? "jalr" : "jalrs");
e64af278 9095 if (MIPS_JALR_HINT_P (ep)
833794fc 9096 || mips_opts.insn32
e64af278 9097 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
9098 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
9099 else
9100 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
9101 }
2906b037
MR
9102 else
9103 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
1180b5a4 9104 if (MIPS_JALR_HINT_P (ep))
df58fc94 9105 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
438c16b8
TS
9106}
9107
252b5132
RH
9108/*
9109 * Generate a "lui" instruction.
9110 */
9111static void
67c0d1eb 9112macro_build_lui (expressionS *ep, int regnum)
252b5132 9113{
9c2799c2 9114 gas_assert (! mips_opts.mips16);
252b5132 9115
df58fc94 9116 if (ep->X_op != O_constant)
252b5132 9117 {
9c2799c2 9118 gas_assert (ep->X_op == O_symbol);
bbe506e8
TS
9119 /* _gp_disp is a special case, used from s_cpload.
9120 __gnu_local_gp is used if mips_no_shared. */
9c2799c2 9121 gas_assert (mips_pic == NO_PIC
78e1bb40 9122 || (! HAVE_NEWABI
aa6975fb
ILT
9123 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
9124 || (! mips_in_shared
bbe506e8
TS
9125 && strcmp (S_GET_NAME (ep->X_add_symbol),
9126 "__gnu_local_gp") == 0));
252b5132
RH
9127 }
9128
df58fc94 9129 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
252b5132
RH
9130}
9131
885add95
CD
9132/* Generate a sequence of instructions to do a load or store from a constant
9133 offset off of a base register (breg) into/from a target register (treg),
9134 using AT if necessary. */
9135static void
67c0d1eb
RS
9136macro_build_ldst_constoffset (expressionS *ep, const char *op,
9137 int treg, int breg, int dbl)
885add95 9138{
9c2799c2 9139 gas_assert (ep->X_op == O_constant);
885add95 9140
256ab948 9141 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
9142 if (!dbl)
9143 normalize_constant_expr (ep);
256ab948 9144
67c1ffbe 9145 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 9146 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
9147 as_warn (_("operand overflow"));
9148
9149 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
9150 {
9151 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 9152 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
9153 }
9154 else
9155 {
9156 /* 32-bit offset, need multiple instructions and AT, like:
9157 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
9158 addu $tempreg,$tempreg,$breg
9159 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
9160 to handle the complete offset. */
67c0d1eb
RS
9161 macro_build_lui (ep, AT);
9162 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
9163 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95 9164
741fe287 9165 if (!mips_opts.at)
1661c76c 9166 as_bad (_("macro used $at after \".set noat\""));
885add95
CD
9167 }
9168}
9169
252b5132
RH
9170/* set_at()
9171 * Generates code to set the $at register to true (one)
9172 * if reg is less than the immediate expression.
9173 */
9174static void
67c0d1eb 9175set_at (int reg, int unsignedp)
252b5132 9176{
b0e6f033 9177 if (imm_expr.X_add_number >= -0x8000
252b5132 9178 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
9179 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
9180 AT, reg, BFD_RELOC_LO16);
252b5132
RH
9181 else
9182 {
bad1aba3 9183 load_register (AT, &imm_expr, GPR_SIZE == 64);
67c0d1eb 9184 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
9185 }
9186}
9187
252b5132
RH
9188/* Count the leading zeroes by performing a binary chop. This is a
9189 bulky bit of source, but performance is a LOT better for the
9190 majority of values than a simple loop to count the bits:
9191 for (lcnt = 0; (lcnt < 32); lcnt++)
9192 if ((v) & (1 << (31 - lcnt)))
9193 break;
9194 However it is not code size friendly, and the gain will drop a bit
9195 on certain cached systems.
9196*/
9197#define COUNT_TOP_ZEROES(v) \
9198 (((v) & ~0xffff) == 0 \
9199 ? ((v) & ~0xff) == 0 \
9200 ? ((v) & ~0xf) == 0 \
9201 ? ((v) & ~0x3) == 0 \
9202 ? ((v) & ~0x1) == 0 \
9203 ? !(v) \
9204 ? 32 \
9205 : 31 \
9206 : 30 \
9207 : ((v) & ~0x7) == 0 \
9208 ? 29 \
9209 : 28 \
9210 : ((v) & ~0x3f) == 0 \
9211 ? ((v) & ~0x1f) == 0 \
9212 ? 27 \
9213 : 26 \
9214 : ((v) & ~0x7f) == 0 \
9215 ? 25 \
9216 : 24 \
9217 : ((v) & ~0xfff) == 0 \
9218 ? ((v) & ~0x3ff) == 0 \
9219 ? ((v) & ~0x1ff) == 0 \
9220 ? 23 \
9221 : 22 \
9222 : ((v) & ~0x7ff) == 0 \
9223 ? 21 \
9224 : 20 \
9225 : ((v) & ~0x3fff) == 0 \
9226 ? ((v) & ~0x1fff) == 0 \
9227 ? 19 \
9228 : 18 \
9229 : ((v) & ~0x7fff) == 0 \
9230 ? 17 \
9231 : 16 \
9232 : ((v) & ~0xffffff) == 0 \
9233 ? ((v) & ~0xfffff) == 0 \
9234 ? ((v) & ~0x3ffff) == 0 \
9235 ? ((v) & ~0x1ffff) == 0 \
9236 ? 15 \
9237 : 14 \
9238 : ((v) & ~0x7ffff) == 0 \
9239 ? 13 \
9240 : 12 \
9241 : ((v) & ~0x3fffff) == 0 \
9242 ? ((v) & ~0x1fffff) == 0 \
9243 ? 11 \
9244 : 10 \
9245 : ((v) & ~0x7fffff) == 0 \
9246 ? 9 \
9247 : 8 \
9248 : ((v) & ~0xfffffff) == 0 \
9249 ? ((v) & ~0x3ffffff) == 0 \
9250 ? ((v) & ~0x1ffffff) == 0 \
9251 ? 7 \
9252 : 6 \
9253 : ((v) & ~0x7ffffff) == 0 \
9254 ? 5 \
9255 : 4 \
9256 : ((v) & ~0x3fffffff) == 0 \
9257 ? ((v) & ~0x1fffffff) == 0 \
9258 ? 3 \
9259 : 2 \
9260 : ((v) & ~0x7fffffff) == 0 \
9261 ? 1 \
9262 : 0)
9263
9264/* load_register()
67c1ffbe 9265 * This routine generates the least number of instructions necessary to load
252b5132
RH
9266 * an absolute expression value into a register.
9267 */
9268static void
67c0d1eb 9269load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
9270{
9271 int freg;
9272 expressionS hi32, lo32;
9273
9274 if (ep->X_op != O_big)
9275 {
9c2799c2 9276 gas_assert (ep->X_op == O_constant);
256ab948
TS
9277
9278 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
9279 if (!dbl)
9280 normalize_constant_expr (ep);
256ab948
TS
9281
9282 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
9283 {
9284 /* We can handle 16 bit signed values with an addiu to
9285 $zero. No need to ever use daddiu here, since $zero and
9286 the result are always correct in 32 bit mode. */
67c0d1eb 9287 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9288 return;
9289 }
9290 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
9291 {
9292 /* We can handle 16 bit unsigned values with an ori to
9293 $zero. */
67c0d1eb 9294 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9295 return;
9296 }
256ab948 9297 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
9298 {
9299 /* 32 bit values require an lui. */
df58fc94 9300 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 9301 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 9302 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
9303 return;
9304 }
9305 }
9306
9307 /* The value is larger than 32 bits. */
9308
bad1aba3 9309 if (!dbl || GPR_SIZE == 32)
252b5132 9310 {
55e08f71
NC
9311 char value[32];
9312
9313 sprintf_vma (value, ep->X_add_number);
1661c76c 9314 as_bad (_("number (0x%s) larger than 32 bits"), value);
67c0d1eb 9315 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9316 return;
9317 }
9318
9319 if (ep->X_op != O_big)
9320 {
9321 hi32 = *ep;
9322 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9323 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9324 hi32.X_add_number &= 0xffffffff;
9325 lo32 = *ep;
9326 lo32.X_add_number &= 0xffffffff;
9327 }
9328 else
9329 {
9c2799c2 9330 gas_assert (ep->X_add_number > 2);
252b5132
RH
9331 if (ep->X_add_number == 3)
9332 generic_bignum[3] = 0;
9333 else if (ep->X_add_number > 4)
1661c76c 9334 as_bad (_("number larger than 64 bits"));
252b5132
RH
9335 lo32.X_op = O_constant;
9336 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
9337 hi32.X_op = O_constant;
9338 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
9339 }
9340
9341 if (hi32.X_add_number == 0)
9342 freg = 0;
9343 else
9344 {
9345 int shift, bit;
9346 unsigned long hi, lo;
9347
956cd1d6 9348 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
9349 {
9350 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
9351 {
67c0d1eb 9352 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
9353 return;
9354 }
9355 if (lo32.X_add_number & 0x80000000)
9356 {
df58fc94 9357 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 9358 if (lo32.X_add_number & 0xffff)
67c0d1eb 9359 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
9360 return;
9361 }
9362 }
252b5132
RH
9363
9364 /* Check for 16bit shifted constant. We know that hi32 is
9365 non-zero, so start the mask on the first bit of the hi32
9366 value. */
9367 shift = 17;
9368 do
beae10d5
KH
9369 {
9370 unsigned long himask, lomask;
9371
9372 if (shift < 32)
9373 {
9374 himask = 0xffff >> (32 - shift);
9375 lomask = (0xffff << shift) & 0xffffffff;
9376 }
9377 else
9378 {
9379 himask = 0xffff << (shift - 32);
9380 lomask = 0;
9381 }
9382 if ((hi32.X_add_number & ~(offsetT) himask) == 0
9383 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
9384 {
9385 expressionS tmp;
9386
9387 tmp.X_op = O_constant;
9388 if (shift < 32)
9389 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
9390 | (lo32.X_add_number >> shift));
9391 else
9392 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb 9393 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
df58fc94 9394 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 9395 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
9396 return;
9397 }
f9419b05 9398 ++shift;
beae10d5
KH
9399 }
9400 while (shift <= (64 - 16));
252b5132
RH
9401
9402 /* Find the bit number of the lowest one bit, and store the
9403 shifted value in hi/lo. */
9404 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
9405 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
9406 if (lo != 0)
9407 {
9408 bit = 0;
9409 while ((lo & 1) == 0)
9410 {
9411 lo >>= 1;
9412 ++bit;
9413 }
9414 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
9415 hi >>= bit;
9416 }
9417 else
9418 {
9419 bit = 32;
9420 while ((hi & 1) == 0)
9421 {
9422 hi >>= 1;
9423 ++bit;
9424 }
9425 lo = hi;
9426 hi = 0;
9427 }
9428
9429 /* Optimize if the shifted value is a (power of 2) - 1. */
9430 if ((hi == 0 && ((lo + 1) & lo) == 0)
9431 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
9432 {
9433 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 9434 if (shift != 0)
beae10d5 9435 {
252b5132
RH
9436 expressionS tmp;
9437
9438 /* This instruction will set the register to be all
9439 ones. */
beae10d5
KH
9440 tmp.X_op = O_constant;
9441 tmp.X_add_number = (offsetT) -1;
67c0d1eb 9442 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
9443 if (bit != 0)
9444 {
9445 bit += shift;
df58fc94 9446 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 9447 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 9448 }
df58fc94 9449 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
67c0d1eb 9450 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
9451 return;
9452 }
9453 }
252b5132
RH
9454
9455 /* Sign extend hi32 before calling load_register, because we can
9456 generally get better code when we load a sign extended value. */
9457 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 9458 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 9459 load_register (reg, &hi32, 0);
252b5132
RH
9460 freg = reg;
9461 }
9462 if ((lo32.X_add_number & 0xffff0000) == 0)
9463 {
9464 if (freg != 0)
9465 {
df58fc94 9466 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
252b5132
RH
9467 freg = reg;
9468 }
9469 }
9470 else
9471 {
9472 expressionS mid16;
9473
956cd1d6 9474 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 9475 {
df58fc94
RS
9476 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9477 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
beae10d5
KH
9478 return;
9479 }
252b5132
RH
9480
9481 if (freg != 0)
9482 {
df58fc94 9483 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
252b5132
RH
9484 freg = reg;
9485 }
9486 mid16 = lo32;
9487 mid16.X_add_number >>= 16;
67c0d1eb 9488 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
df58fc94 9489 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
252b5132
RH
9490 freg = reg;
9491 }
9492 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 9493 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
9494}
9495
269137b2
TS
9496static inline void
9497load_delay_nop (void)
9498{
9499 if (!gpr_interlocks)
9500 macro_build (NULL, "nop", "");
9501}
9502
252b5132
RH
9503/* Load an address into a register. */
9504
9505static void
67c0d1eb 9506load_address (int reg, expressionS *ep, int *used_at)
252b5132 9507{
252b5132
RH
9508 if (ep->X_op != O_constant
9509 && ep->X_op != O_symbol)
9510 {
9511 as_bad (_("expression too complex"));
9512 ep->X_op = O_constant;
9513 }
9514
9515 if (ep->X_op == O_constant)
9516 {
67c0d1eb 9517 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
9518 return;
9519 }
9520
9521 if (mips_pic == NO_PIC)
9522 {
9523 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 9524 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
9525 Otherwise we want
9526 lui $reg,<sym> (BFD_RELOC_HI16_S)
9527 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 9528 If we have an addend, we always use the latter form.
76b3015f 9529
d6bc6245
TS
9530 With 64bit address space and a usable $at we want
9531 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9532 lui $at,<sym> (BFD_RELOC_HI16_S)
9533 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9534 daddiu $at,<sym> (BFD_RELOC_LO16)
9535 dsll32 $reg,0
3a482fd5 9536 daddu $reg,$reg,$at
76b3015f 9537
c03099e6 9538 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
9539 on superscalar processors.
9540 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9541 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9542 dsll $reg,16
9543 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
9544 dsll $reg,16
9545 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
9546
9547 For GP relative symbols in 64bit address space we can use
9548 the same sequence as in 32bit address space. */
aed1a261 9549 if (HAVE_64BIT_SYMBOLS)
d6bc6245 9550 {
6caf9ef4
TS
9551 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9552 && !nopic_need_relax (ep->X_add_symbol, 1))
9553 {
9554 relax_start (ep->X_add_symbol);
9555 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9556 mips_gp_register, BFD_RELOC_GPREL16);
9557 relax_switch ();
9558 }
d6bc6245 9559
741fe287 9560 if (*used_at == 0 && mips_opts.at)
d6bc6245 9561 {
df58fc94
RS
9562 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9563 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
67c0d1eb
RS
9564 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9565 BFD_RELOC_MIPS_HIGHER);
9566 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
df58fc94 9567 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
67c0d1eb 9568 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
9569 *used_at = 1;
9570 }
9571 else
9572 {
df58fc94 9573 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb
RS
9574 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9575 BFD_RELOC_MIPS_HIGHER);
df58fc94 9576 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 9577 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
df58fc94 9578 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 9579 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 9580 }
6caf9ef4
TS
9581
9582 if (mips_relax.sequence)
9583 relax_end ();
d6bc6245 9584 }
252b5132
RH
9585 else
9586 {
d6bc6245 9587 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 9588 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 9589 {
4d7206a2 9590 relax_start (ep->X_add_symbol);
67c0d1eb 9591 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 9592 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 9593 relax_switch ();
d6bc6245 9594 }
67c0d1eb
RS
9595 macro_build_lui (ep, reg);
9596 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
9597 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
9598 if (mips_relax.sequence)
9599 relax_end ();
d6bc6245 9600 }
252b5132 9601 }
0a44bf69 9602 else if (!mips_big_got)
252b5132
RH
9603 {
9604 expressionS ex;
9605
9606 /* If this is a reference to an external symbol, we want
9607 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9608 Otherwise we want
9609 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9610 nop
9611 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
9612 If there is a constant, it must be added in after.
9613
ed6fb7bd 9614 If we have NewABI, we want
f5040a92
AO
9615 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
9616 unless we're referencing a global symbol with a non-zero
9617 offset, in which case cst must be added separately. */
ed6fb7bd
SC
9618 if (HAVE_NEWABI)
9619 {
f5040a92
AO
9620 if (ep->X_add_number)
9621 {
4d7206a2 9622 ex.X_add_number = ep->X_add_number;
f5040a92 9623 ep->X_add_number = 0;
4d7206a2 9624 relax_start (ep->X_add_symbol);
67c0d1eb
RS
9625 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9626 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
9627 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9628 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9629 ex.X_op = O_constant;
67c0d1eb 9630 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 9631 reg, reg, BFD_RELOC_LO16);
f5040a92 9632 ep->X_add_number = ex.X_add_number;
4d7206a2 9633 relax_switch ();
f5040a92 9634 }
67c0d1eb 9635 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9636 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
9637 if (mips_relax.sequence)
9638 relax_end ();
ed6fb7bd
SC
9639 }
9640 else
9641 {
f5040a92
AO
9642 ex.X_add_number = ep->X_add_number;
9643 ep->X_add_number = 0;
67c0d1eb
RS
9644 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9645 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9646 load_delay_nop ();
4d7206a2
RS
9647 relax_start (ep->X_add_symbol);
9648 relax_switch ();
67c0d1eb 9649 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 9650 BFD_RELOC_LO16);
4d7206a2 9651 relax_end ();
ed6fb7bd 9652
f5040a92
AO
9653 if (ex.X_add_number != 0)
9654 {
9655 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9656 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9657 ex.X_op = O_constant;
67c0d1eb 9658 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 9659 reg, reg, BFD_RELOC_LO16);
f5040a92 9660 }
252b5132
RH
9661 }
9662 }
0a44bf69 9663 else if (mips_big_got)
252b5132
RH
9664 {
9665 expressionS ex;
252b5132
RH
9666
9667 /* This is the large GOT case. If this is a reference to an
9668 external symbol, we want
9669 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9670 addu $reg,$reg,$gp
9671 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
9672
9673 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
9674 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9675 nop
9676 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 9677 If there is a constant, it must be added in after.
f5040a92
AO
9678
9679 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
9680 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
9681 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 9682 */
438c16b8
TS
9683 if (HAVE_NEWABI)
9684 {
4d7206a2 9685 ex.X_add_number = ep->X_add_number;
f5040a92 9686 ep->X_add_number = 0;
4d7206a2 9687 relax_start (ep->X_add_symbol);
df58fc94 9688 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
9689 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9690 reg, reg, mips_gp_register);
9691 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9692 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
9693 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9694 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9695 else if (ex.X_add_number)
9696 {
9697 ex.X_op = O_constant;
67c0d1eb
RS
9698 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9699 BFD_RELOC_LO16);
f5040a92
AO
9700 }
9701
9702 ep->X_add_number = ex.X_add_number;
4d7206a2 9703 relax_switch ();
67c0d1eb 9704 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9705 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
9706 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9707 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 9708 relax_end ();
438c16b8 9709 }
252b5132 9710 else
438c16b8 9711 {
f5040a92
AO
9712 ex.X_add_number = ep->X_add_number;
9713 ep->X_add_number = 0;
4d7206a2 9714 relax_start (ep->X_add_symbol);
df58fc94 9715 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
9716 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9717 reg, reg, mips_gp_register);
9718 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9719 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
9720 relax_switch ();
9721 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
9722 {
9723 /* We need a nop before loading from $gp. This special
9724 check is required because the lui which starts the main
9725 instruction stream does not refer to $gp, and so will not
9726 insert the nop which may be required. */
67c0d1eb 9727 macro_build (NULL, "nop", "");
438c16b8 9728 }
67c0d1eb 9729 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9730 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9731 load_delay_nop ();
67c0d1eb 9732 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 9733 BFD_RELOC_LO16);
4d7206a2 9734 relax_end ();
438c16b8 9735
f5040a92
AO
9736 if (ex.X_add_number != 0)
9737 {
9738 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9739 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9740 ex.X_op = O_constant;
67c0d1eb
RS
9741 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9742 BFD_RELOC_LO16);
f5040a92 9743 }
252b5132
RH
9744 }
9745 }
252b5132
RH
9746 else
9747 abort ();
8fc2e39e 9748
741fe287 9749 if (!mips_opts.at && *used_at == 1)
1661c76c 9750 as_bad (_("macro used $at after \".set noat\""));
252b5132
RH
9751}
9752
ea1fb5dc
RS
9753/* Move the contents of register SOURCE into register DEST. */
9754
9755static void
67c0d1eb 9756move_register (int dest, int source)
ea1fb5dc 9757{
df58fc94
RS
9758 /* Prefer to use a 16-bit microMIPS instruction unless the previous
9759 instruction specifically requires a 32-bit one. */
9760 if (mips_opts.micromips
833794fc 9761 && !mips_opts.insn32
df58fc94 9762 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7951ca42 9763 macro_build (NULL, "move", "mp,mj", dest, source);
df58fc94 9764 else
40fc1451 9765 macro_build (NULL, "or", "d,v,t", dest, source, 0);
ea1fb5dc
RS
9766}
9767
4d7206a2 9768/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
9769 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
9770 The two alternatives are:
4d7206a2 9771
33eaf5de 9772 Global symbol Local symbol
4d7206a2
RS
9773 ------------- ------------
9774 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
9775 ... ...
9776 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
9777
9778 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
9779 emits the second for a 16-bit offset or add_got_offset_hilo emits
9780 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
9781
9782static void
67c0d1eb 9783load_got_offset (int dest, expressionS *local)
4d7206a2
RS
9784{
9785 expressionS global;
9786
9787 global = *local;
9788 global.X_add_number = 0;
9789
9790 relax_start (local->X_add_symbol);
67c0d1eb
RS
9791 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9792 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 9793 relax_switch ();
67c0d1eb
RS
9794 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9795 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
9796 relax_end ();
9797}
9798
9799static void
67c0d1eb 9800add_got_offset (int dest, expressionS *local)
4d7206a2
RS
9801{
9802 expressionS global;
9803
9804 global.X_op = O_constant;
9805 global.X_op_symbol = NULL;
9806 global.X_add_symbol = NULL;
9807 global.X_add_number = local->X_add_number;
9808
9809 relax_start (local->X_add_symbol);
67c0d1eb 9810 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
9811 dest, dest, BFD_RELOC_LO16);
9812 relax_switch ();
67c0d1eb 9813 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
9814 relax_end ();
9815}
9816
f6a22291
MR
9817static void
9818add_got_offset_hilo (int dest, expressionS *local, int tmp)
9819{
9820 expressionS global;
9821 int hold_mips_optimize;
9822
9823 global.X_op = O_constant;
9824 global.X_op_symbol = NULL;
9825 global.X_add_symbol = NULL;
9826 global.X_add_number = local->X_add_number;
9827
9828 relax_start (local->X_add_symbol);
9829 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
9830 relax_switch ();
9831 /* Set mips_optimize around the lui instruction to avoid
9832 inserting an unnecessary nop after the lw. */
9833 hold_mips_optimize = mips_optimize;
9834 mips_optimize = 2;
9835 macro_build_lui (&global, tmp);
9836 mips_optimize = hold_mips_optimize;
9837 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
9838 relax_end ();
9839
9840 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
9841}
9842
df58fc94
RS
9843/* Emit a sequence of instructions to emulate a branch likely operation.
9844 BR is an ordinary branch corresponding to one to be emulated. BRNEG
9845 is its complementing branch with the original condition negated.
9846 CALL is set if the original branch specified the link operation.
9847 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
9848
9849 Code like this is produced in the noreorder mode:
9850
9851 BRNEG <args>, 1f
9852 nop
9853 b <sym>
9854 delay slot (executed only if branch taken)
9855 1:
9856
9857 or, if CALL is set:
9858
9859 BRNEG <args>, 1f
9860 nop
9861 bal <sym>
9862 delay slot (executed only if branch taken)
9863 1:
9864
9865 In the reorder mode the delay slot would be filled with a nop anyway,
9866 so code produced is simply:
9867
9868 BR <args>, <sym>
9869 nop
9870
9871 This function is used when producing code for the microMIPS ASE that
9872 does not implement branch likely instructions in hardware. */
9873
9874static void
9875macro_build_branch_likely (const char *br, const char *brneg,
9876 int call, expressionS *ep, const char *fmt,
9877 unsigned int sreg, unsigned int treg)
9878{
9879 int noreorder = mips_opts.noreorder;
9880 expressionS expr1;
9881
9882 gas_assert (mips_opts.micromips);
9883 start_noreorder ();
9884 if (noreorder)
9885 {
9886 micromips_label_expr (&expr1);
9887 macro_build (&expr1, brneg, fmt, sreg, treg);
9888 macro_build (NULL, "nop", "");
9889 macro_build (ep, call ? "bal" : "b", "p");
9890
9891 /* Set to true so that append_insn adds a label. */
9892 emit_branch_likely_macro = TRUE;
9893 }
9894 else
9895 {
9896 macro_build (ep, br, fmt, sreg, treg);
9897 macro_build (NULL, "nop", "");
9898 }
9899 end_noreorder ();
9900}
9901
9902/* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
9903 the condition code tested. EP specifies the branch target. */
9904
9905static void
9906macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
9907{
9908 const int call = 0;
9909 const char *brneg;
9910 const char *br;
9911
9912 switch (type)
9913 {
9914 case M_BC1FL:
9915 br = "bc1f";
9916 brneg = "bc1t";
9917 break;
9918 case M_BC1TL:
9919 br = "bc1t";
9920 brneg = "bc1f";
9921 break;
9922 case M_BC2FL:
9923 br = "bc2f";
9924 brneg = "bc2t";
9925 break;
9926 case M_BC2TL:
9927 br = "bc2t";
9928 brneg = "bc2f";
9929 break;
9930 default:
9931 abort ();
9932 }
9933 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
9934}
9935
9936/* Emit a two-argument branch macro specified by TYPE, using SREG as
9937 the register tested. EP specifies the branch target. */
9938
9939static void
9940macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
9941{
9942 const char *brneg = NULL;
9943 const char *br;
9944 int call = 0;
9945
9946 switch (type)
9947 {
9948 case M_BGEZ:
9949 br = "bgez";
9950 break;
9951 case M_BGEZL:
9952 br = mips_opts.micromips ? "bgez" : "bgezl";
9953 brneg = "bltz";
9954 break;
9955 case M_BGEZALL:
9956 gas_assert (mips_opts.micromips);
833794fc 9957 br = mips_opts.insn32 ? "bgezal" : "bgezals";
df58fc94
RS
9958 brneg = "bltz";
9959 call = 1;
9960 break;
9961 case M_BGTZ:
9962 br = "bgtz";
9963 break;
9964 case M_BGTZL:
9965 br = mips_opts.micromips ? "bgtz" : "bgtzl";
9966 brneg = "blez";
9967 break;
9968 case M_BLEZ:
9969 br = "blez";
9970 break;
9971 case M_BLEZL:
9972 br = mips_opts.micromips ? "blez" : "blezl";
9973 brneg = "bgtz";
9974 break;
9975 case M_BLTZ:
9976 br = "bltz";
9977 break;
9978 case M_BLTZL:
9979 br = mips_opts.micromips ? "bltz" : "bltzl";
9980 brneg = "bgez";
9981 break;
9982 case M_BLTZALL:
9983 gas_assert (mips_opts.micromips);
833794fc 9984 br = mips_opts.insn32 ? "bltzal" : "bltzals";
df58fc94
RS
9985 brneg = "bgez";
9986 call = 1;
9987 break;
9988 default:
9989 abort ();
9990 }
9991 if (mips_opts.micromips && brneg)
9992 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
9993 else
9994 macro_build (ep, br, "s,p", sreg);
9995}
9996
9997/* Emit a three-argument branch macro specified by TYPE, using SREG and
9998 TREG as the registers tested. EP specifies the branch target. */
9999
10000static void
10001macro_build_branch_rsrt (int type, expressionS *ep,
10002 unsigned int sreg, unsigned int treg)
10003{
10004 const char *brneg = NULL;
10005 const int call = 0;
10006 const char *br;
10007
10008 switch (type)
10009 {
10010 case M_BEQ:
10011 case M_BEQ_I:
10012 br = "beq";
10013 break;
10014 case M_BEQL:
10015 case M_BEQL_I:
10016 br = mips_opts.micromips ? "beq" : "beql";
10017 brneg = "bne";
10018 break;
10019 case M_BNE:
10020 case M_BNE_I:
10021 br = "bne";
10022 break;
10023 case M_BNEL:
10024 case M_BNEL_I:
10025 br = mips_opts.micromips ? "bne" : "bnel";
10026 brneg = "beq";
10027 break;
10028 default:
10029 abort ();
10030 }
10031 if (mips_opts.micromips && brneg)
10032 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
10033 else
10034 macro_build (ep, br, "s,t,p", sreg, treg);
10035}
10036
f2ae14a1
RS
10037/* Return the high part that should be loaded in order to make the low
10038 part of VALUE accessible using an offset of OFFBITS bits. */
10039
10040static offsetT
10041offset_high_part (offsetT value, unsigned int offbits)
10042{
10043 offsetT bias;
10044 addressT low_mask;
10045
10046 if (offbits == 0)
10047 return value;
10048 bias = 1 << (offbits - 1);
10049 low_mask = bias * 2 - 1;
10050 return (value + bias) & ~low_mask;
10051}
10052
10053/* Return true if the value stored in offset_expr and offset_reloc
10054 fits into a signed offset of OFFBITS bits. RANGE is the maximum
10055 amount that the caller wants to add without inducing overflow
10056 and ALIGN is the known alignment of the value in bytes. */
10057
10058static bfd_boolean
10059small_offset_p (unsigned int range, unsigned int align, unsigned int offbits)
10060{
10061 if (offbits == 16)
10062 {
10063 /* Accept any relocation operator if overflow isn't a concern. */
10064 if (range < align && *offset_reloc != BFD_RELOC_UNUSED)
10065 return TRUE;
10066
10067 /* These relocations are guaranteed not to overflow in correct links. */
10068 if (*offset_reloc == BFD_RELOC_MIPS_LITERAL
10069 || gprel16_reloc_p (*offset_reloc))
10070 return TRUE;
10071 }
10072 if (offset_expr.X_op == O_constant
10073 && offset_high_part (offset_expr.X_add_number, offbits) == 0
10074 && offset_high_part (offset_expr.X_add_number + range, offbits) == 0)
10075 return TRUE;
10076 return FALSE;
10077}
10078
252b5132
RH
10079/*
10080 * Build macros
10081 * This routine implements the seemingly endless macro or synthesized
10082 * instructions and addressing modes in the mips assembly language. Many
10083 * of these macros are simple and are similar to each other. These could
67c1ffbe 10084 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
10085 * this verbose method. Others are not simple macros but are more like
10086 * optimizing code generation.
10087 * One interesting optimization is when several store macros appear
67c1ffbe 10088 * consecutively that would load AT with the upper half of the same address.
2b0f3761 10089 * The ensuing load upper instructions are omitted. This implies some kind
252b5132
RH
10090 * of global optimization. We currently only optimize within a single macro.
10091 * For many of the load and store macros if the address is specified as a
10092 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
10093 * first load register 'at' with zero and use it as the base register. The
10094 * mips assembler simply uses register $zero. Just one tiny optimization
10095 * we're missing.
10096 */
10097static void
833794fc 10098macro (struct mips_cl_insn *ip, char *str)
252b5132 10099{
c0ebe874
RS
10100 const struct mips_operand_array *operands;
10101 unsigned int breg, i;
741fe287 10102 unsigned int tempreg;
252b5132 10103 int mask;
43841e91 10104 int used_at = 0;
df58fc94 10105 expressionS label_expr;
252b5132 10106 expressionS expr1;
df58fc94 10107 expressionS *ep;
252b5132
RH
10108 const char *s;
10109 const char *s2;
10110 const char *fmt;
10111 int likely = 0;
252b5132 10112 int coproc = 0;
7f3c4072 10113 int offbits = 16;
1abe91b1 10114 int call = 0;
df58fc94
RS
10115 int jals = 0;
10116 int dbl = 0;
10117 int imm = 0;
10118 int ust = 0;
10119 int lp = 0;
f2ae14a1 10120 bfd_boolean large_offset;
252b5132 10121 int off;
252b5132 10122 int hold_mips_optimize;
f2ae14a1 10123 unsigned int align;
c0ebe874 10124 unsigned int op[MAX_OPERANDS];
252b5132 10125
9c2799c2 10126 gas_assert (! mips_opts.mips16);
252b5132 10127
c0ebe874
RS
10128 operands = insn_operands (ip);
10129 for (i = 0; i < MAX_OPERANDS; i++)
10130 if (operands->operand[i])
10131 op[i] = insn_extract_operand (ip, operands->operand[i]);
10132 else
10133 op[i] = -1;
10134
252b5132
RH
10135 mask = ip->insn_mo->mask;
10136
df58fc94
RS
10137 label_expr.X_op = O_constant;
10138 label_expr.X_op_symbol = NULL;
10139 label_expr.X_add_symbol = NULL;
10140 label_expr.X_add_number = 0;
10141
252b5132
RH
10142 expr1.X_op = O_constant;
10143 expr1.X_op_symbol = NULL;
10144 expr1.X_add_symbol = NULL;
10145 expr1.X_add_number = 1;
f2ae14a1 10146 align = 1;
252b5132
RH
10147
10148 switch (mask)
10149 {
10150 case M_DABS:
10151 dbl = 1;
1a0670f3 10152 /* Fall through. */
252b5132 10153 case M_ABS:
df58fc94
RS
10154 /* bgez $a0,1f
10155 move v0,$a0
10156 sub v0,$zero,$a0
10157 1:
10158 */
252b5132 10159
7d10b47d 10160 start_noreorder ();
252b5132 10161
df58fc94
RS
10162 if (mips_opts.micromips)
10163 micromips_label_expr (&label_expr);
10164 else
10165 label_expr.X_add_number = 8;
c0ebe874
RS
10166 macro_build (&label_expr, "bgez", "s,p", op[1]);
10167 if (op[0] == op[1])
a605d2b3 10168 macro_build (NULL, "nop", "");
252b5132 10169 else
c0ebe874
RS
10170 move_register (op[0], op[1]);
10171 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", op[0], 0, op[1]);
df58fc94
RS
10172 if (mips_opts.micromips)
10173 micromips_add_label ();
252b5132 10174
7d10b47d 10175 end_noreorder ();
8fc2e39e 10176 break;
252b5132
RH
10177
10178 case M_ADD_I:
10179 s = "addi";
10180 s2 = "add";
10181 goto do_addi;
10182 case M_ADDU_I:
10183 s = "addiu";
10184 s2 = "addu";
10185 goto do_addi;
10186 case M_DADD_I:
10187 dbl = 1;
10188 s = "daddi";
10189 s2 = "dadd";
df58fc94
RS
10190 if (!mips_opts.micromips)
10191 goto do_addi;
b0e6f033 10192 if (imm_expr.X_add_number >= -0x200
df58fc94
RS
10193 && imm_expr.X_add_number < 0x200)
10194 {
b0e6f033
RS
10195 macro_build (NULL, s, "t,r,.", op[0], op[1],
10196 (int) imm_expr.X_add_number);
df58fc94
RS
10197 break;
10198 }
10199 goto do_addi_i;
252b5132
RH
10200 case M_DADDU_I:
10201 dbl = 1;
10202 s = "daddiu";
10203 s2 = "daddu";
10204 do_addi:
b0e6f033 10205 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
10206 && imm_expr.X_add_number < 0x8000)
10207 {
c0ebe874 10208 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 10209 break;
252b5132 10210 }
df58fc94 10211 do_addi_i:
8fc2e39e 10212 used_at = 1;
67c0d1eb 10213 load_register (AT, &imm_expr, dbl);
c0ebe874 10214 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
10215 break;
10216
10217 case M_AND_I:
10218 s = "andi";
10219 s2 = "and";
10220 goto do_bit;
10221 case M_OR_I:
10222 s = "ori";
10223 s2 = "or";
10224 goto do_bit;
10225 case M_NOR_I:
10226 s = "";
10227 s2 = "nor";
10228 goto do_bit;
10229 case M_XOR_I:
10230 s = "xori";
10231 s2 = "xor";
10232 do_bit:
b0e6f033 10233 if (imm_expr.X_add_number >= 0
252b5132
RH
10234 && imm_expr.X_add_number < 0x10000)
10235 {
10236 if (mask != M_NOR_I)
c0ebe874 10237 macro_build (&imm_expr, s, "t,r,i", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
10238 else
10239 {
67c0d1eb 10240 macro_build (&imm_expr, "ori", "t,r,i",
c0ebe874
RS
10241 op[0], op[1], BFD_RELOC_LO16);
10242 macro_build (NULL, "nor", "d,v,t", op[0], op[0], 0);
252b5132 10243 }
8fc2e39e 10244 break;
252b5132
RH
10245 }
10246
8fc2e39e 10247 used_at = 1;
bad1aba3 10248 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 10249 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
10250 break;
10251
8b082fb1
TS
10252 case M_BALIGN:
10253 switch (imm_expr.X_add_number)
10254 {
10255 case 0:
10256 macro_build (NULL, "nop", "");
10257 break;
10258 case 2:
c0ebe874 10259 macro_build (NULL, "packrl.ph", "d,s,t", op[0], op[0], op[1]);
8b082fb1 10260 break;
03f66e8a
MR
10261 case 1:
10262 case 3:
c0ebe874 10263 macro_build (NULL, "balign", "t,s,2", op[0], op[1],
90ecf173 10264 (int) imm_expr.X_add_number);
8b082fb1 10265 break;
03f66e8a
MR
10266 default:
10267 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
10268 (unsigned long) imm_expr.X_add_number);
10269 break;
8b082fb1
TS
10270 }
10271 break;
10272
df58fc94
RS
10273 case M_BC1FL:
10274 case M_BC1TL:
10275 case M_BC2FL:
10276 case M_BC2TL:
10277 gas_assert (mips_opts.micromips);
10278 macro_build_branch_ccl (mask, &offset_expr,
10279 EXTRACT_OPERAND (1, BCC, *ip));
10280 break;
10281
252b5132 10282 case M_BEQ_I:
252b5132 10283 case M_BEQL_I:
252b5132 10284 case M_BNE_I:
252b5132 10285 case M_BNEL_I:
b0e6f033 10286 if (imm_expr.X_add_number == 0)
c0ebe874 10287 op[1] = 0;
df58fc94 10288 else
252b5132 10289 {
c0ebe874 10290 op[1] = AT;
df58fc94 10291 used_at = 1;
bad1aba3 10292 load_register (op[1], &imm_expr, GPR_SIZE == 64);
252b5132 10293 }
df58fc94
RS
10294 /* Fall through. */
10295 case M_BEQL:
10296 case M_BNEL:
c0ebe874 10297 macro_build_branch_rsrt (mask, &offset_expr, op[0], op[1]);
252b5132
RH
10298 break;
10299
10300 case M_BGEL:
10301 likely = 1;
1a0670f3 10302 /* Fall through. */
252b5132 10303 case M_BGE:
c0ebe874
RS
10304 if (op[1] == 0)
10305 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[0]);
10306 else if (op[0] == 0)
10307 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[1]);
df58fc94 10308 else
252b5132 10309 {
df58fc94 10310 used_at = 1;
c0ebe874 10311 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10312 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10313 &offset_expr, AT, ZERO);
252b5132 10314 }
df58fc94
RS
10315 break;
10316
10317 case M_BGEZL:
10318 case M_BGEZALL:
10319 case M_BGTZL:
10320 case M_BLEZL:
10321 case M_BLTZL:
10322 case M_BLTZALL:
c0ebe874 10323 macro_build_branch_rs (mask, &offset_expr, op[0]);
252b5132
RH
10324 break;
10325
10326 case M_BGTL_I:
10327 likely = 1;
1a0670f3 10328 /* Fall through. */
252b5132 10329 case M_BGT_I:
90ecf173 10330 /* Check for > max integer. */
b0e6f033 10331 if (imm_expr.X_add_number >= GPR_SMAX)
252b5132
RH
10332 {
10333 do_false:
90ecf173 10334 /* Result is always false. */
252b5132 10335 if (! likely)
a605d2b3 10336 macro_build (NULL, "nop", "");
252b5132 10337 else
df58fc94 10338 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
8fc2e39e 10339 break;
252b5132 10340 }
f9419b05 10341 ++imm_expr.X_add_number;
252b5132
RH
10342 /* FALLTHROUGH */
10343 case M_BGE_I:
10344 case M_BGEL_I:
10345 if (mask == M_BGEL_I)
10346 likely = 1;
b0e6f033 10347 if (imm_expr.X_add_number == 0)
252b5132 10348 {
df58fc94 10349 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
c0ebe874 10350 &offset_expr, op[0]);
8fc2e39e 10351 break;
252b5132 10352 }
b0e6f033 10353 if (imm_expr.X_add_number == 1)
252b5132 10354 {
df58fc94 10355 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
c0ebe874 10356 &offset_expr, op[0]);
8fc2e39e 10357 break;
252b5132 10358 }
b0e6f033 10359 if (imm_expr.X_add_number <= GPR_SMIN)
252b5132
RH
10360 {
10361 do_true:
10362 /* result is always true */
1661c76c 10363 as_warn (_("branch %s is always true"), ip->insn_mo->name);
67c0d1eb 10364 macro_build (&offset_expr, "b", "p");
8fc2e39e 10365 break;
252b5132 10366 }
8fc2e39e 10367 used_at = 1;
c0ebe874 10368 set_at (op[0], 0);
df58fc94
RS
10369 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10370 &offset_expr, AT, ZERO);
252b5132
RH
10371 break;
10372
10373 case M_BGEUL:
10374 likely = 1;
1a0670f3 10375 /* Fall through. */
252b5132 10376 case M_BGEU:
c0ebe874 10377 if (op[1] == 0)
252b5132 10378 goto do_true;
c0ebe874 10379 else if (op[0] == 0)
df58fc94 10380 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 10381 &offset_expr, ZERO, op[1]);
df58fc94 10382 else
252b5132 10383 {
df58fc94 10384 used_at = 1;
c0ebe874 10385 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10386 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10387 &offset_expr, AT, ZERO);
252b5132 10388 }
252b5132
RH
10389 break;
10390
10391 case M_BGTUL_I:
10392 likely = 1;
1a0670f3 10393 /* Fall through. */
252b5132 10394 case M_BGTU_I:
c0ebe874 10395 if (op[0] == 0
bad1aba3 10396 || (GPR_SIZE == 32
f01dc953 10397 && imm_expr.X_add_number == -1))
252b5132 10398 goto do_false;
f9419b05 10399 ++imm_expr.X_add_number;
252b5132
RH
10400 /* FALLTHROUGH */
10401 case M_BGEU_I:
10402 case M_BGEUL_I:
10403 if (mask == M_BGEUL_I)
10404 likely = 1;
b0e6f033 10405 if (imm_expr.X_add_number == 0)
252b5132 10406 goto do_true;
b0e6f033 10407 else if (imm_expr.X_add_number == 1)
df58fc94 10408 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 10409 &offset_expr, op[0], ZERO);
df58fc94 10410 else
252b5132 10411 {
df58fc94 10412 used_at = 1;
c0ebe874 10413 set_at (op[0], 1);
df58fc94
RS
10414 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10415 &offset_expr, AT, ZERO);
252b5132 10416 }
252b5132
RH
10417 break;
10418
10419 case M_BGTL:
10420 likely = 1;
1a0670f3 10421 /* Fall through. */
252b5132 10422 case M_BGT:
c0ebe874
RS
10423 if (op[1] == 0)
10424 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[0]);
10425 else if (op[0] == 0)
10426 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[1]);
df58fc94 10427 else
252b5132 10428 {
df58fc94 10429 used_at = 1;
c0ebe874 10430 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10431 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10432 &offset_expr, AT, ZERO);
252b5132 10433 }
252b5132
RH
10434 break;
10435
10436 case M_BGTUL:
10437 likely = 1;
1a0670f3 10438 /* Fall through. */
252b5132 10439 case M_BGTU:
c0ebe874 10440 if (op[1] == 0)
df58fc94 10441 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874
RS
10442 &offset_expr, op[0], ZERO);
10443 else if (op[0] == 0)
df58fc94
RS
10444 goto do_false;
10445 else
252b5132 10446 {
df58fc94 10447 used_at = 1;
c0ebe874 10448 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10449 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10450 &offset_expr, AT, ZERO);
252b5132 10451 }
252b5132
RH
10452 break;
10453
10454 case M_BLEL:
10455 likely = 1;
1a0670f3 10456 /* Fall through. */
252b5132 10457 case M_BLE:
c0ebe874
RS
10458 if (op[1] == 0)
10459 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10460 else if (op[0] == 0)
10461 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[1]);
df58fc94 10462 else
252b5132 10463 {
df58fc94 10464 used_at = 1;
c0ebe874 10465 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10466 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10467 &offset_expr, AT, ZERO);
252b5132 10468 }
252b5132
RH
10469 break;
10470
10471 case M_BLEL_I:
10472 likely = 1;
1a0670f3 10473 /* Fall through. */
252b5132 10474 case M_BLE_I:
b0e6f033 10475 if (imm_expr.X_add_number >= GPR_SMAX)
252b5132 10476 goto do_true;
f9419b05 10477 ++imm_expr.X_add_number;
252b5132
RH
10478 /* FALLTHROUGH */
10479 case M_BLT_I:
10480 case M_BLTL_I:
10481 if (mask == M_BLTL_I)
10482 likely = 1;
b0e6f033 10483 if (imm_expr.X_add_number == 0)
c0ebe874 10484 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
b0e6f033 10485 else if (imm_expr.X_add_number == 1)
c0ebe874 10486 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
df58fc94 10487 else
252b5132 10488 {
df58fc94 10489 used_at = 1;
c0ebe874 10490 set_at (op[0], 0);
df58fc94
RS
10491 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10492 &offset_expr, AT, ZERO);
252b5132 10493 }
252b5132
RH
10494 break;
10495
10496 case M_BLEUL:
10497 likely = 1;
1a0670f3 10498 /* Fall through. */
252b5132 10499 case M_BLEU:
c0ebe874 10500 if (op[1] == 0)
df58fc94 10501 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874
RS
10502 &offset_expr, op[0], ZERO);
10503 else if (op[0] == 0)
df58fc94
RS
10504 goto do_true;
10505 else
252b5132 10506 {
df58fc94 10507 used_at = 1;
c0ebe874 10508 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10509 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10510 &offset_expr, AT, ZERO);
252b5132 10511 }
252b5132
RH
10512 break;
10513
10514 case M_BLEUL_I:
10515 likely = 1;
1a0670f3 10516 /* Fall through. */
252b5132 10517 case M_BLEU_I:
c0ebe874 10518 if (op[0] == 0
bad1aba3 10519 || (GPR_SIZE == 32
f01dc953 10520 && imm_expr.X_add_number == -1))
252b5132 10521 goto do_true;
f9419b05 10522 ++imm_expr.X_add_number;
252b5132
RH
10523 /* FALLTHROUGH */
10524 case M_BLTU_I:
10525 case M_BLTUL_I:
10526 if (mask == M_BLTUL_I)
10527 likely = 1;
b0e6f033 10528 if (imm_expr.X_add_number == 0)
252b5132 10529 goto do_false;
b0e6f033 10530 else if (imm_expr.X_add_number == 1)
df58fc94 10531 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 10532 &offset_expr, op[0], ZERO);
df58fc94 10533 else
252b5132 10534 {
df58fc94 10535 used_at = 1;
c0ebe874 10536 set_at (op[0], 1);
df58fc94
RS
10537 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10538 &offset_expr, AT, ZERO);
252b5132 10539 }
252b5132
RH
10540 break;
10541
10542 case M_BLTL:
10543 likely = 1;
1a0670f3 10544 /* Fall through. */
252b5132 10545 case M_BLT:
c0ebe874
RS
10546 if (op[1] == 0)
10547 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10548 else if (op[0] == 0)
10549 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[1]);
df58fc94 10550 else
252b5132 10551 {
df58fc94 10552 used_at = 1;
c0ebe874 10553 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10554 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10555 &offset_expr, AT, ZERO);
252b5132 10556 }
252b5132
RH
10557 break;
10558
10559 case M_BLTUL:
10560 likely = 1;
1a0670f3 10561 /* Fall through. */
252b5132 10562 case M_BLTU:
c0ebe874 10563 if (op[1] == 0)
252b5132 10564 goto do_false;
c0ebe874 10565 else if (op[0] == 0)
df58fc94 10566 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 10567 &offset_expr, ZERO, op[1]);
df58fc94 10568 else
252b5132 10569 {
df58fc94 10570 used_at = 1;
c0ebe874 10571 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10572 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10573 &offset_expr, AT, ZERO);
252b5132 10574 }
252b5132
RH
10575 break;
10576
10577 case M_DDIV_3:
10578 dbl = 1;
1a0670f3 10579 /* Fall through. */
252b5132
RH
10580 case M_DIV_3:
10581 s = "mflo";
10582 goto do_div3;
10583 case M_DREM_3:
10584 dbl = 1;
1a0670f3 10585 /* Fall through. */
252b5132
RH
10586 case M_REM_3:
10587 s = "mfhi";
10588 do_div3:
c0ebe874 10589 if (op[2] == 0)
252b5132 10590 {
1661c76c 10591 as_warn (_("divide by zero"));
252b5132 10592 if (mips_trap)
df58fc94 10593 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 10594 else
df58fc94 10595 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 10596 break;
252b5132
RH
10597 }
10598
7d10b47d 10599 start_noreorder ();
252b5132
RH
10600 if (mips_trap)
10601 {
c0ebe874
RS
10602 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10603 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
252b5132
RH
10604 }
10605 else
10606 {
df58fc94
RS
10607 if (mips_opts.micromips)
10608 micromips_label_expr (&label_expr);
10609 else
10610 label_expr.X_add_number = 8;
c0ebe874
RS
10611 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10612 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
df58fc94
RS
10613 macro_build (NULL, "break", BRK_FMT, 7);
10614 if (mips_opts.micromips)
10615 micromips_add_label ();
252b5132
RH
10616 }
10617 expr1.X_add_number = -1;
8fc2e39e 10618 used_at = 1;
f6a22291 10619 load_register (AT, &expr1, dbl);
df58fc94
RS
10620 if (mips_opts.micromips)
10621 micromips_label_expr (&label_expr);
10622 else
10623 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
c0ebe874 10624 macro_build (&label_expr, "bne", "s,t,p", op[2], AT);
252b5132
RH
10625 if (dbl)
10626 {
10627 expr1.X_add_number = 1;
f6a22291 10628 load_register (AT, &expr1, dbl);
df58fc94 10629 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
252b5132
RH
10630 }
10631 else
10632 {
10633 expr1.X_add_number = 0x80000000;
df58fc94 10634 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
252b5132
RH
10635 }
10636 if (mips_trap)
10637 {
c0ebe874 10638 macro_build (NULL, "teq", TRAP_FMT, op[1], AT, 6);
252b5132
RH
10639 /* We want to close the noreorder block as soon as possible, so
10640 that later insns are available for delay slot filling. */
7d10b47d 10641 end_noreorder ();
252b5132
RH
10642 }
10643 else
10644 {
df58fc94
RS
10645 if (mips_opts.micromips)
10646 micromips_label_expr (&label_expr);
10647 else
10648 label_expr.X_add_number = 8;
c0ebe874 10649 macro_build (&label_expr, "bne", "s,t,p", op[1], AT);
a605d2b3 10650 macro_build (NULL, "nop", "");
252b5132
RH
10651
10652 /* We want to close the noreorder block as soon as possible, so
10653 that later insns are available for delay slot filling. */
7d10b47d 10654 end_noreorder ();
252b5132 10655
df58fc94 10656 macro_build (NULL, "break", BRK_FMT, 6);
252b5132 10657 }
df58fc94
RS
10658 if (mips_opts.micromips)
10659 micromips_add_label ();
c0ebe874 10660 macro_build (NULL, s, MFHL_FMT, op[0]);
252b5132
RH
10661 break;
10662
10663 case M_DIV_3I:
10664 s = "div";
10665 s2 = "mflo";
10666 goto do_divi;
10667 case M_DIVU_3I:
10668 s = "divu";
10669 s2 = "mflo";
10670 goto do_divi;
10671 case M_REM_3I:
10672 s = "div";
10673 s2 = "mfhi";
10674 goto do_divi;
10675 case M_REMU_3I:
10676 s = "divu";
10677 s2 = "mfhi";
10678 goto do_divi;
10679 case M_DDIV_3I:
10680 dbl = 1;
10681 s = "ddiv";
10682 s2 = "mflo";
10683 goto do_divi;
10684 case M_DDIVU_3I:
10685 dbl = 1;
10686 s = "ddivu";
10687 s2 = "mflo";
10688 goto do_divi;
10689 case M_DREM_3I:
10690 dbl = 1;
10691 s = "ddiv";
10692 s2 = "mfhi";
10693 goto do_divi;
10694 case M_DREMU_3I:
10695 dbl = 1;
10696 s = "ddivu";
10697 s2 = "mfhi";
10698 do_divi:
b0e6f033 10699 if (imm_expr.X_add_number == 0)
252b5132 10700 {
1661c76c 10701 as_warn (_("divide by zero"));
252b5132 10702 if (mips_trap)
df58fc94 10703 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 10704 else
df58fc94 10705 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 10706 break;
252b5132 10707 }
b0e6f033 10708 if (imm_expr.X_add_number == 1)
252b5132
RH
10709 {
10710 if (strcmp (s2, "mflo") == 0)
c0ebe874 10711 move_register (op[0], op[1]);
252b5132 10712 else
c0ebe874 10713 move_register (op[0], ZERO);
8fc2e39e 10714 break;
252b5132 10715 }
b0e6f033 10716 if (imm_expr.X_add_number == -1 && s[strlen (s) - 1] != 'u')
252b5132
RH
10717 {
10718 if (strcmp (s2, "mflo") == 0)
c0ebe874 10719 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", op[0], op[1]);
252b5132 10720 else
c0ebe874 10721 move_register (op[0], ZERO);
8fc2e39e 10722 break;
252b5132
RH
10723 }
10724
8fc2e39e 10725 used_at = 1;
67c0d1eb 10726 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
10727 macro_build (NULL, s, "z,s,t", op[1], AT);
10728 macro_build (NULL, s2, MFHL_FMT, op[0]);
252b5132
RH
10729 break;
10730
10731 case M_DIVU_3:
10732 s = "divu";
10733 s2 = "mflo";
10734 goto do_divu3;
10735 case M_REMU_3:
10736 s = "divu";
10737 s2 = "mfhi";
10738 goto do_divu3;
10739 case M_DDIVU_3:
10740 s = "ddivu";
10741 s2 = "mflo";
10742 goto do_divu3;
10743 case M_DREMU_3:
10744 s = "ddivu";
10745 s2 = "mfhi";
10746 do_divu3:
7d10b47d 10747 start_noreorder ();
252b5132
RH
10748 if (mips_trap)
10749 {
c0ebe874
RS
10750 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10751 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
10752 /* We want to close the noreorder block as soon as possible, so
10753 that later insns are available for delay slot filling. */
7d10b47d 10754 end_noreorder ();
252b5132
RH
10755 }
10756 else
10757 {
df58fc94
RS
10758 if (mips_opts.micromips)
10759 micromips_label_expr (&label_expr);
10760 else
10761 label_expr.X_add_number = 8;
c0ebe874
RS
10762 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10763 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
10764
10765 /* We want to close the noreorder block as soon as possible, so
10766 that later insns are available for delay slot filling. */
7d10b47d 10767 end_noreorder ();
df58fc94
RS
10768 macro_build (NULL, "break", BRK_FMT, 7);
10769 if (mips_opts.micromips)
10770 micromips_add_label ();
252b5132 10771 }
c0ebe874 10772 macro_build (NULL, s2, MFHL_FMT, op[0]);
8fc2e39e 10773 break;
252b5132 10774
1abe91b1
MR
10775 case M_DLCA_AB:
10776 dbl = 1;
1a0670f3 10777 /* Fall through. */
1abe91b1
MR
10778 case M_LCA_AB:
10779 call = 1;
10780 goto do_la;
252b5132
RH
10781 case M_DLA_AB:
10782 dbl = 1;
1a0670f3 10783 /* Fall through. */
252b5132 10784 case M_LA_AB:
1abe91b1 10785 do_la:
252b5132
RH
10786 /* Load the address of a symbol into a register. If breg is not
10787 zero, we then add a base register to it. */
10788
c0ebe874 10789 breg = op[2];
bad1aba3 10790 if (dbl && GPR_SIZE == 32)
ece794d9
MF
10791 as_warn (_("dla used to load 32-bit register; recommend using la "
10792 "instead"));
3bec30a8 10793
90ecf173 10794 if (!dbl && HAVE_64BIT_OBJECTS)
ece794d9
MF
10795 as_warn (_("la used to load 64-bit address; recommend using dla "
10796 "instead"));
3bec30a8 10797
f2ae14a1 10798 if (small_offset_p (0, align, 16))
0c11417f 10799 {
c0ebe874 10800 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", op[0], breg,
f2ae14a1 10801 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8fc2e39e 10802 break;
0c11417f
MR
10803 }
10804
c0ebe874 10805 if (mips_opts.at && (op[0] == breg))
afdbd6d0
CD
10806 {
10807 tempreg = AT;
10808 used_at = 1;
10809 }
10810 else
c0ebe874 10811 tempreg = op[0];
afdbd6d0 10812
252b5132
RH
10813 if (offset_expr.X_op != O_symbol
10814 && offset_expr.X_op != O_constant)
10815 {
1661c76c 10816 as_bad (_("expression too complex"));
252b5132
RH
10817 offset_expr.X_op = O_constant;
10818 }
10819
252b5132 10820 if (offset_expr.X_op == O_constant)
aed1a261 10821 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
10822 else if (mips_pic == NO_PIC)
10823 {
d6bc6245 10824 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 10825 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
10826 Otherwise we want
10827 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
10828 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10829 If we have a constant, we need two instructions anyhow,
d6bc6245 10830 so we may as well always use the latter form.
76b3015f 10831
6caf9ef4
TS
10832 With 64bit address space and a usable $at we want
10833 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10834 lui $at,<sym> (BFD_RELOC_HI16_S)
10835 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10836 daddiu $at,<sym> (BFD_RELOC_LO16)
10837 dsll32 $tempreg,0
10838 daddu $tempreg,$tempreg,$at
10839
10840 If $at is already in use, we use a path which is suboptimal
10841 on superscalar processors.
10842 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10843 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10844 dsll $tempreg,16
10845 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
10846 dsll $tempreg,16
10847 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
10848
10849 For GP relative symbols in 64bit address space we can use
10850 the same sequence as in 32bit address space. */
aed1a261 10851 if (HAVE_64BIT_SYMBOLS)
252b5132 10852 {
6caf9ef4
TS
10853 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
10854 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
10855 {
10856 relax_start (offset_expr.X_add_symbol);
10857 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10858 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
10859 relax_switch ();
10860 }
d6bc6245 10861
741fe287 10862 if (used_at == 0 && mips_opts.at)
98d3f06f 10863 {
df58fc94 10864 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 10865 tempreg, BFD_RELOC_MIPS_HIGHEST);
df58fc94 10866 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 10867 AT, BFD_RELOC_HI16_S);
67c0d1eb 10868 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10869 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 10870 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10871 AT, AT, BFD_RELOC_LO16);
df58fc94 10872 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 10873 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
10874 used_at = 1;
10875 }
10876 else
10877 {
df58fc94 10878 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 10879 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 10880 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10881 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 10882 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 10883 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10884 tempreg, tempreg, BFD_RELOC_HI16_S);
df58fc94 10885 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 10886 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10887 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 10888 }
6caf9ef4
TS
10889
10890 if (mips_relax.sequence)
10891 relax_end ();
98d3f06f
KH
10892 }
10893 else
10894 {
10895 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 10896 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 10897 {
4d7206a2 10898 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
10899 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10900 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 10901 relax_switch ();
98d3f06f 10902 }
6943caf0 10903 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
1661c76c 10904 as_bad (_("offset too large"));
67c0d1eb
RS
10905 macro_build_lui (&offset_expr, tempreg);
10906 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10907 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
10908 if (mips_relax.sequence)
10909 relax_end ();
98d3f06f 10910 }
252b5132 10911 }
0a44bf69 10912 else if (!mips_big_got && !HAVE_NEWABI)
252b5132 10913 {
9117d219
NC
10914 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
10915
252b5132
RH
10916 /* If this is a reference to an external symbol, and there
10917 is no constant, we want
10918 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 10919 or for lca or if tempreg is PIC_CALL_REG
9117d219 10920 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
10921 For a local symbol, we want
10922 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10923 nop
10924 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10925
10926 If we have a small constant, and this is a reference to
10927 an external symbol, we want
10928 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10929 nop
10930 addiu $tempreg,$tempreg,<constant>
10931 For a local symbol, we want the same instruction
10932 sequence, but we output a BFD_RELOC_LO16 reloc on the
10933 addiu instruction.
10934
10935 If we have a large constant, and this is a reference to
10936 an external symbol, we want
10937 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10938 lui $at,<hiconstant>
10939 addiu $at,$at,<loconstant>
10940 addu $tempreg,$tempreg,$at
10941 For a local symbol, we want the same instruction
10942 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 10943 addiu instruction.
ed6fb7bd
SC
10944 */
10945
4d7206a2 10946 if (offset_expr.X_add_number == 0)
252b5132 10947 {
0a44bf69
RS
10948 if (mips_pic == SVR4_PIC
10949 && breg == 0
10950 && (call || tempreg == PIC_CALL_REG))
4d7206a2
RS
10951 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
10952
10953 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
10954 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10955 lw_reloc_type, mips_gp_register);
4d7206a2 10956 if (breg != 0)
252b5132
RH
10957 {
10958 /* We're going to put in an addu instruction using
10959 tempreg, so we may as well insert the nop right
10960 now. */
269137b2 10961 load_delay_nop ();
252b5132 10962 }
4d7206a2 10963 relax_switch ();
67c0d1eb
RS
10964 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10965 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 10966 load_delay_nop ();
67c0d1eb
RS
10967 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10968 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 10969 relax_end ();
252b5132
RH
10970 /* FIXME: If breg == 0, and the next instruction uses
10971 $tempreg, then if this variant case is used an extra
10972 nop will be generated. */
10973 }
4d7206a2
RS
10974 else if (offset_expr.X_add_number >= -0x8000
10975 && offset_expr.X_add_number < 0x8000)
252b5132 10976 {
67c0d1eb 10977 load_got_offset (tempreg, &offset_expr);
269137b2 10978 load_delay_nop ();
67c0d1eb 10979 add_got_offset (tempreg, &offset_expr);
252b5132
RH
10980 }
10981 else
10982 {
4d7206a2
RS
10983 expr1.X_add_number = offset_expr.X_add_number;
10984 offset_expr.X_add_number =
43c0598f 10985 SEXT_16BIT (offset_expr.X_add_number);
67c0d1eb 10986 load_got_offset (tempreg, &offset_expr);
f6a22291 10987 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
10988 /* If we are going to add in a base register, and the
10989 target register and the base register are the same,
10990 then we are using AT as a temporary register. Since
10991 we want to load the constant into AT, we add our
10992 current AT (from the global offset table) and the
10993 register into the register now, and pretend we were
10994 not using a base register. */
c0ebe874 10995 if (breg == op[0])
252b5132 10996 {
269137b2 10997 load_delay_nop ();
67c0d1eb 10998 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 10999 op[0], AT, breg);
252b5132 11000 breg = 0;
c0ebe874 11001 tempreg = op[0];
252b5132 11002 }
f6a22291 11003 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
11004 used_at = 1;
11005 }
11006 }
0a44bf69 11007 else if (!mips_big_got && HAVE_NEWABI)
f5040a92 11008 {
67c0d1eb 11009 int add_breg_early = 0;
f5040a92
AO
11010
11011 /* If this is a reference to an external, and there is no
11012 constant, or local symbol (*), with or without a
11013 constant, we want
11014 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 11015 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
11016 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11017
11018 If we have a small constant, and this is a reference to
11019 an external symbol, we want
11020 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
11021 addiu $tempreg,$tempreg,<constant>
11022
11023 If we have a large constant, and this is a reference to
11024 an external symbol, we want
11025 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
11026 lui $at,<hiconstant>
11027 addiu $at,$at,<loconstant>
11028 addu $tempreg,$tempreg,$at
11029
11030 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
11031 local symbols, even though it introduces an additional
11032 instruction. */
11033
f5040a92
AO
11034 if (offset_expr.X_add_number)
11035 {
4d7206a2 11036 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
11037 offset_expr.X_add_number = 0;
11038
4d7206a2 11039 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11040 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11041 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
11042
11043 if (expr1.X_add_number >= -0x8000
11044 && expr1.X_add_number < 0x8000)
11045 {
67c0d1eb
RS
11046 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11047 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 11048 }
ecd13cd3 11049 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 11050 {
c0ebe874
RS
11051 unsigned int dreg;
11052
f5040a92
AO
11053 /* If we are going to add in a base register, and the
11054 target register and the base register are the same,
11055 then we are using AT as a temporary register. Since
11056 we want to load the constant into AT, we add our
11057 current AT (from the global offset table) and the
11058 register into the register now, and pretend we were
11059 not using a base register. */
c0ebe874 11060 if (breg != op[0])
f5040a92
AO
11061 dreg = tempreg;
11062 else
11063 {
9c2799c2 11064 gas_assert (tempreg == AT);
67c0d1eb 11065 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11066 op[0], AT, breg);
11067 dreg = op[0];
67c0d1eb 11068 add_breg_early = 1;
f5040a92
AO
11069 }
11070
f6a22291 11071 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 11072 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11073 dreg, dreg, AT);
f5040a92 11074
f5040a92
AO
11075 used_at = 1;
11076 }
11077 else
11078 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11079
4d7206a2 11080 relax_switch ();
f5040a92
AO
11081 offset_expr.X_add_number = expr1.X_add_number;
11082
67c0d1eb
RS
11083 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11084 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11085 if (add_breg_early)
f5040a92 11086 {
67c0d1eb 11087 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 11088 op[0], tempreg, breg);
f5040a92 11089 breg = 0;
c0ebe874 11090 tempreg = op[0];
f5040a92 11091 }
4d7206a2 11092 relax_end ();
f5040a92 11093 }
4d7206a2 11094 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 11095 {
4d7206a2 11096 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11097 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11098 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 11099 relax_switch ();
67c0d1eb
RS
11100 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11101 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 11102 relax_end ();
f5040a92 11103 }
4d7206a2 11104 else
f5040a92 11105 {
67c0d1eb
RS
11106 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11107 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
11108 }
11109 }
0a44bf69 11110 else if (mips_big_got && !HAVE_NEWABI)
252b5132 11111 {
67c0d1eb 11112 int gpdelay;
9117d219
NC
11113 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11114 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 11115 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
11116
11117 /* This is the large GOT case. If this is a reference to an
11118 external symbol, and there is no constant, we want
11119 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11120 addu $tempreg,$tempreg,$gp
11121 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 11122 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
11123 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11124 addu $tempreg,$tempreg,$gp
11125 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
11126 For a local symbol, we want
11127 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11128 nop
11129 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
11130
11131 If we have a small constant, and this is a reference to
11132 an external symbol, we want
11133 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11134 addu $tempreg,$tempreg,$gp
11135 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11136 nop
11137 addiu $tempreg,$tempreg,<constant>
11138 For a local symbol, we want
11139 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11140 nop
11141 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
11142
11143 If we have a large constant, and this is a reference to
11144 an external symbol, we want
11145 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11146 addu $tempreg,$tempreg,$gp
11147 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11148 lui $at,<hiconstant>
11149 addiu $at,$at,<loconstant>
11150 addu $tempreg,$tempreg,$at
11151 For a local symbol, we want
11152 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11153 lui $at,<hiconstant>
11154 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
11155 addu $tempreg,$tempreg,$at
f5040a92 11156 */
438c16b8 11157
252b5132
RH
11158 expr1.X_add_number = offset_expr.X_add_number;
11159 offset_expr.X_add_number = 0;
4d7206a2 11160 relax_start (offset_expr.X_add_symbol);
67c0d1eb 11161 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
11162 if (expr1.X_add_number == 0 && breg == 0
11163 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
11164 {
11165 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11166 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11167 }
df58fc94 11168 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 11169 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11170 tempreg, tempreg, mips_gp_register);
67c0d1eb 11171 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 11172 tempreg, lw_reloc_type, tempreg);
252b5132
RH
11173 if (expr1.X_add_number == 0)
11174 {
67c0d1eb 11175 if (breg != 0)
252b5132
RH
11176 {
11177 /* We're going to put in an addu instruction using
11178 tempreg, so we may as well insert the nop right
11179 now. */
269137b2 11180 load_delay_nop ();
252b5132 11181 }
252b5132
RH
11182 }
11183 else if (expr1.X_add_number >= -0x8000
11184 && expr1.X_add_number < 0x8000)
11185 {
269137b2 11186 load_delay_nop ();
67c0d1eb 11187 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11188 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
11189 }
11190 else
11191 {
c0ebe874
RS
11192 unsigned int dreg;
11193
252b5132
RH
11194 /* If we are going to add in a base register, and the
11195 target register and the base register are the same,
11196 then we are using AT as a temporary register. Since
11197 we want to load the constant into AT, we add our
11198 current AT (from the global offset table) and the
11199 register into the register now, and pretend we were
11200 not using a base register. */
c0ebe874 11201 if (breg != op[0])
67c0d1eb 11202 dreg = tempreg;
252b5132
RH
11203 else
11204 {
9c2799c2 11205 gas_assert (tempreg == AT);
269137b2 11206 load_delay_nop ();
67c0d1eb 11207 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11208 op[0], AT, breg);
11209 dreg = op[0];
252b5132
RH
11210 }
11211
f6a22291 11212 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 11213 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 11214
252b5132
RH
11215 used_at = 1;
11216 }
43c0598f 11217 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
4d7206a2 11218 relax_switch ();
252b5132 11219
67c0d1eb 11220 if (gpdelay)
252b5132
RH
11221 {
11222 /* This is needed because this instruction uses $gp, but
f5040a92 11223 the first instruction on the main stream does not. */
67c0d1eb 11224 macro_build (NULL, "nop", "");
252b5132 11225 }
ed6fb7bd 11226
67c0d1eb
RS
11227 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11228 local_reloc_type, mips_gp_register);
f5040a92 11229 if (expr1.X_add_number >= -0x8000
252b5132
RH
11230 && expr1.X_add_number < 0x8000)
11231 {
269137b2 11232 load_delay_nop ();
67c0d1eb
RS
11233 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11234 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 11235 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
11236 register, the external symbol case ended with a load,
11237 so if the symbol turns out to not be external, and
11238 the next instruction uses tempreg, an unnecessary nop
11239 will be inserted. */
252b5132
RH
11240 }
11241 else
11242 {
c0ebe874 11243 if (breg == op[0])
252b5132
RH
11244 {
11245 /* We must add in the base register now, as in the
f5040a92 11246 external symbol case. */
9c2799c2 11247 gas_assert (tempreg == AT);
269137b2 11248 load_delay_nop ();
67c0d1eb 11249 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11250 op[0], AT, breg);
11251 tempreg = op[0];
252b5132 11252 /* We set breg to 0 because we have arranged to add
f5040a92 11253 it in in both cases. */
252b5132
RH
11254 breg = 0;
11255 }
11256
67c0d1eb
RS
11257 macro_build_lui (&expr1, AT);
11258 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11259 AT, AT, BFD_RELOC_LO16);
67c0d1eb 11260 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11261 tempreg, tempreg, AT);
8fc2e39e 11262 used_at = 1;
252b5132 11263 }
4d7206a2 11264 relax_end ();
252b5132 11265 }
0a44bf69 11266 else if (mips_big_got && HAVE_NEWABI)
f5040a92 11267 {
f5040a92
AO
11268 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11269 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 11270 int add_breg_early = 0;
f5040a92
AO
11271
11272 /* This is the large GOT case. If this is a reference to an
11273 external symbol, and there is no constant, we want
11274 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11275 add $tempreg,$tempreg,$gp
11276 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 11277 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
11278 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11279 add $tempreg,$tempreg,$gp
11280 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
11281
11282 If we have a small constant, and this is a reference to
11283 an external symbol, we want
11284 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11285 add $tempreg,$tempreg,$gp
11286 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11287 addi $tempreg,$tempreg,<constant>
11288
11289 If we have a large constant, and this is a reference to
11290 an external symbol, we want
11291 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11292 addu $tempreg,$tempreg,$gp
11293 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11294 lui $at,<hiconstant>
11295 addi $at,$at,<loconstant>
11296 add $tempreg,$tempreg,$at
11297
11298 If we have NewABI, and we know it's a local symbol, we want
11299 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
11300 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
11301 otherwise we have to resort to GOT_HI16/GOT_LO16. */
11302
4d7206a2 11303 relax_start (offset_expr.X_add_symbol);
f5040a92 11304
4d7206a2 11305 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
11306 offset_expr.X_add_number = 0;
11307
1abe91b1
MR
11308 if (expr1.X_add_number == 0 && breg == 0
11309 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
11310 {
11311 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11312 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11313 }
df58fc94 11314 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 11315 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11316 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
11317 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11318 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
11319
11320 if (expr1.X_add_number == 0)
4d7206a2 11321 ;
f5040a92
AO
11322 else if (expr1.X_add_number >= -0x8000
11323 && expr1.X_add_number < 0x8000)
11324 {
67c0d1eb 11325 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11326 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 11327 }
ecd13cd3 11328 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 11329 {
c0ebe874
RS
11330 unsigned int dreg;
11331
f5040a92
AO
11332 /* If we are going to add in a base register, and the
11333 target register and the base register are the same,
11334 then we are using AT as a temporary register. Since
11335 we want to load the constant into AT, we add our
11336 current AT (from the global offset table) and the
11337 register into the register now, and pretend we were
11338 not using a base register. */
c0ebe874 11339 if (breg != op[0])
f5040a92
AO
11340 dreg = tempreg;
11341 else
11342 {
9c2799c2 11343 gas_assert (tempreg == AT);
67c0d1eb 11344 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11345 op[0], AT, breg);
11346 dreg = op[0];
67c0d1eb 11347 add_breg_early = 1;
f5040a92
AO
11348 }
11349
f6a22291 11350 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 11351 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 11352
f5040a92
AO
11353 used_at = 1;
11354 }
11355 else
11356 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11357
4d7206a2 11358 relax_switch ();
f5040a92 11359 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
11360 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11361 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
11362 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11363 tempreg, BFD_RELOC_MIPS_GOT_OFST);
11364 if (add_breg_early)
f5040a92 11365 {
67c0d1eb 11366 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 11367 op[0], tempreg, breg);
f5040a92 11368 breg = 0;
c0ebe874 11369 tempreg = op[0];
f5040a92 11370 }
4d7206a2 11371 relax_end ();
f5040a92 11372 }
252b5132
RH
11373 else
11374 abort ();
11375
11376 if (breg != 0)
c0ebe874 11377 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", op[0], tempreg, breg);
252b5132
RH
11378 break;
11379
52b6b6b9 11380 case M_MSGSND:
df58fc94 11381 gas_assert (!mips_opts.micromips);
c0ebe874 11382 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x01);
c7af4273 11383 break;
52b6b6b9
JM
11384
11385 case M_MSGLD:
df58fc94 11386 gas_assert (!mips_opts.micromips);
c8276761 11387 macro_build (NULL, "c2", "C", 0x02);
c7af4273 11388 break;
52b6b6b9
JM
11389
11390 case M_MSGLD_T:
df58fc94 11391 gas_assert (!mips_opts.micromips);
c0ebe874 11392 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x02);
c7af4273 11393 break;
52b6b6b9
JM
11394
11395 case M_MSGWAIT:
df58fc94 11396 gas_assert (!mips_opts.micromips);
52b6b6b9 11397 macro_build (NULL, "c2", "C", 3);
c7af4273 11398 break;
52b6b6b9
JM
11399
11400 case M_MSGWAIT_T:
df58fc94 11401 gas_assert (!mips_opts.micromips);
c0ebe874 11402 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x03);
c7af4273 11403 break;
52b6b6b9 11404
252b5132
RH
11405 case M_J_A:
11406 /* The j instruction may not be used in PIC code, since it
11407 requires an absolute address. We convert it to a b
11408 instruction. */
11409 if (mips_pic == NO_PIC)
67c0d1eb 11410 macro_build (&offset_expr, "j", "a");
252b5132 11411 else
67c0d1eb 11412 macro_build (&offset_expr, "b", "p");
8fc2e39e 11413 break;
252b5132
RH
11414
11415 /* The jal instructions must be handled as macros because when
11416 generating PIC code they expand to multi-instruction
11417 sequences. Normally they are simple instructions. */
df58fc94 11418 case M_JALS_1:
c0ebe874
RS
11419 op[1] = op[0];
11420 op[0] = RA;
df58fc94
RS
11421 /* Fall through. */
11422 case M_JALS_2:
11423 gas_assert (mips_opts.micromips);
833794fc
MR
11424 if (mips_opts.insn32)
11425 {
1661c76c 11426 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
833794fc
MR
11427 break;
11428 }
df58fc94
RS
11429 jals = 1;
11430 goto jal;
252b5132 11431 case M_JAL_1:
c0ebe874
RS
11432 op[1] = op[0];
11433 op[0] = RA;
252b5132
RH
11434 /* Fall through. */
11435 case M_JAL_2:
df58fc94 11436 jal:
3e722fb5 11437 if (mips_pic == NO_PIC)
df58fc94
RS
11438 {
11439 s = jals ? "jalrs" : "jalr";
e64af278 11440 if (mips_opts.micromips
833794fc 11441 && !mips_opts.insn32
c0ebe874 11442 && op[0] == RA
e64af278 11443 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 11444 macro_build (NULL, s, "mj", op[1]);
df58fc94 11445 else
c0ebe874 11446 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
df58fc94 11447 }
0a44bf69 11448 else
252b5132 11449 {
df58fc94
RS
11450 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
11451 && mips_cprestore_offset >= 0);
11452
c0ebe874 11453 if (op[1] != PIC_CALL_REG)
252b5132 11454 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 11455
833794fc
MR
11456 s = ((mips_opts.micromips
11457 && !mips_opts.insn32
11458 && (!mips_opts.noreorder || cprestore))
df58fc94 11459 ? "jalrs" : "jalr");
e64af278 11460 if (mips_opts.micromips
833794fc 11461 && !mips_opts.insn32
c0ebe874 11462 && op[0] == RA
e64af278 11463 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 11464 macro_build (NULL, s, "mj", op[1]);
df58fc94 11465 else
c0ebe874 11466 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
0a44bf69 11467 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
252b5132 11468 {
6478892d 11469 if (mips_cprestore_offset < 0)
1661c76c 11470 as_warn (_("no .cprestore pseudo-op used in PIC code"));
6478892d
TS
11471 else
11472 {
90ecf173 11473 if (!mips_frame_reg_valid)
7a621144 11474 {
1661c76c 11475 as_warn (_("no .frame pseudo-op used in PIC code"));
7a621144
DJ
11476 /* Quiet this warning. */
11477 mips_frame_reg_valid = 1;
11478 }
90ecf173 11479 if (!mips_cprestore_valid)
7a621144 11480 {
1661c76c 11481 as_warn (_("no .cprestore pseudo-op used in PIC code"));
7a621144
DJ
11482 /* Quiet this warning. */
11483 mips_cprestore_valid = 1;
11484 }
d3fca0b5
MR
11485 if (mips_opts.noreorder)
11486 macro_build (NULL, "nop", "");
6478892d 11487 expr1.X_add_number = mips_cprestore_offset;
134c0c8b 11488 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 11489 mips_gp_register,
256ab948
TS
11490 mips_frame_reg,
11491 HAVE_64BIT_ADDRESSES);
6478892d 11492 }
252b5132
RH
11493 }
11494 }
252b5132 11495
8fc2e39e 11496 break;
252b5132 11497
df58fc94
RS
11498 case M_JALS_A:
11499 gas_assert (mips_opts.micromips);
833794fc
MR
11500 if (mips_opts.insn32)
11501 {
1661c76c 11502 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
833794fc
MR
11503 break;
11504 }
df58fc94
RS
11505 jals = 1;
11506 /* Fall through. */
252b5132
RH
11507 case M_JAL_A:
11508 if (mips_pic == NO_PIC)
df58fc94 11509 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
252b5132
RH
11510 else if (mips_pic == SVR4_PIC)
11511 {
11512 /* If this is a reference to an external symbol, and we are
11513 using a small GOT, we want
11514 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11515 nop
f9419b05 11516 jalr $ra,$25
252b5132
RH
11517 nop
11518 lw $gp,cprestore($sp)
11519 The cprestore value is set using the .cprestore
11520 pseudo-op. If we are using a big GOT, we want
11521 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11522 addu $25,$25,$gp
11523 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
11524 nop
f9419b05 11525 jalr $ra,$25
252b5132
RH
11526 nop
11527 lw $gp,cprestore($sp)
11528 If the symbol is not external, we want
11529 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11530 nop
11531 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 11532 jalr $ra,$25
252b5132 11533 nop
438c16b8 11534 lw $gp,cprestore($sp)
f5040a92
AO
11535
11536 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
11537 sequences above, minus nops, unless the symbol is local,
11538 which enables us to use GOT_PAGE/GOT_OFST (big got) or
11539 GOT_DISP. */
438c16b8 11540 if (HAVE_NEWABI)
252b5132 11541 {
90ecf173 11542 if (!mips_big_got)
f5040a92 11543 {
4d7206a2 11544 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11545 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11546 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 11547 mips_gp_register);
4d7206a2 11548 relax_switch ();
67c0d1eb
RS
11549 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11550 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
11551 mips_gp_register);
11552 relax_end ();
f5040a92
AO
11553 }
11554 else
11555 {
4d7206a2 11556 relax_start (offset_expr.X_add_symbol);
df58fc94 11557 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
11558 BFD_RELOC_MIPS_CALL_HI16);
11559 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11560 PIC_CALL_REG, mips_gp_register);
11561 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11562 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11563 PIC_CALL_REG);
4d7206a2 11564 relax_switch ();
67c0d1eb
RS
11565 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11566 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
11567 mips_gp_register);
11568 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11569 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 11570 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 11571 relax_end ();
f5040a92 11572 }
684022ea 11573
df58fc94 11574 macro_build_jalr (&offset_expr, 0);
252b5132
RH
11575 }
11576 else
11577 {
4d7206a2 11578 relax_start (offset_expr.X_add_symbol);
90ecf173 11579 if (!mips_big_got)
438c16b8 11580 {
67c0d1eb
RS
11581 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11582 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 11583 mips_gp_register);
269137b2 11584 load_delay_nop ();
4d7206a2 11585 relax_switch ();
438c16b8 11586 }
252b5132 11587 else
252b5132 11588 {
67c0d1eb
RS
11589 int gpdelay;
11590
11591 gpdelay = reg_needs_delay (mips_gp_register);
df58fc94 11592 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
11593 BFD_RELOC_MIPS_CALL_HI16);
11594 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11595 PIC_CALL_REG, mips_gp_register);
11596 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11597 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11598 PIC_CALL_REG);
269137b2 11599 load_delay_nop ();
4d7206a2 11600 relax_switch ();
67c0d1eb
RS
11601 if (gpdelay)
11602 macro_build (NULL, "nop", "");
252b5132 11603 }
67c0d1eb
RS
11604 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11605 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 11606 mips_gp_register);
269137b2 11607 load_delay_nop ();
67c0d1eb
RS
11608 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11609 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 11610 relax_end ();
df58fc94 11611 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
438c16b8 11612
6478892d 11613 if (mips_cprestore_offset < 0)
1661c76c 11614 as_warn (_("no .cprestore pseudo-op used in PIC code"));
6478892d
TS
11615 else
11616 {
90ecf173 11617 if (!mips_frame_reg_valid)
7a621144 11618 {
1661c76c 11619 as_warn (_("no .frame pseudo-op used in PIC code"));
7a621144
DJ
11620 /* Quiet this warning. */
11621 mips_frame_reg_valid = 1;
11622 }
90ecf173 11623 if (!mips_cprestore_valid)
7a621144 11624 {
1661c76c 11625 as_warn (_("no .cprestore pseudo-op used in PIC code"));
7a621144
DJ
11626 /* Quiet this warning. */
11627 mips_cprestore_valid = 1;
11628 }
6478892d 11629 if (mips_opts.noreorder)
67c0d1eb 11630 macro_build (NULL, "nop", "");
6478892d 11631 expr1.X_add_number = mips_cprestore_offset;
134c0c8b 11632 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 11633 mips_gp_register,
256ab948
TS
11634 mips_frame_reg,
11635 HAVE_64BIT_ADDRESSES);
6478892d 11636 }
252b5132
RH
11637 }
11638 }
0a44bf69 11639 else if (mips_pic == VXWORKS_PIC)
1661c76c 11640 as_bad (_("non-PIC jump used in PIC library"));
252b5132
RH
11641 else
11642 abort ();
11643
8fc2e39e 11644 break;
252b5132 11645
7f3c4072 11646 case M_LBUE_AB:
7f3c4072
CM
11647 s = "lbue";
11648 fmt = "t,+j(b)";
11649 offbits = 9;
11650 goto ld_st;
11651 case M_LHUE_AB:
7f3c4072
CM
11652 s = "lhue";
11653 fmt = "t,+j(b)";
11654 offbits = 9;
11655 goto ld_st;
11656 case M_LBE_AB:
7f3c4072
CM
11657 s = "lbe";
11658 fmt = "t,+j(b)";
11659 offbits = 9;
11660 goto ld_st;
11661 case M_LHE_AB:
7f3c4072
CM
11662 s = "lhe";
11663 fmt = "t,+j(b)";
11664 offbits = 9;
11665 goto ld_st;
11666 case M_LLE_AB:
7f3c4072
CM
11667 s = "lle";
11668 fmt = "t,+j(b)";
11669 offbits = 9;
11670 goto ld_st;
11671 case M_LWE_AB:
7f3c4072
CM
11672 s = "lwe";
11673 fmt = "t,+j(b)";
11674 offbits = 9;
11675 goto ld_st;
11676 case M_LWLE_AB:
7f3c4072
CM
11677 s = "lwle";
11678 fmt = "t,+j(b)";
11679 offbits = 9;
11680 goto ld_st;
11681 case M_LWRE_AB:
7f3c4072
CM
11682 s = "lwre";
11683 fmt = "t,+j(b)";
11684 offbits = 9;
11685 goto ld_st;
11686 case M_SBE_AB:
7f3c4072
CM
11687 s = "sbe";
11688 fmt = "t,+j(b)";
11689 offbits = 9;
11690 goto ld_st;
11691 case M_SCE_AB:
7f3c4072
CM
11692 s = "sce";
11693 fmt = "t,+j(b)";
11694 offbits = 9;
11695 goto ld_st;
11696 case M_SHE_AB:
7f3c4072
CM
11697 s = "she";
11698 fmt = "t,+j(b)";
11699 offbits = 9;
11700 goto ld_st;
11701 case M_SWE_AB:
7f3c4072
CM
11702 s = "swe";
11703 fmt = "t,+j(b)";
11704 offbits = 9;
11705 goto ld_st;
11706 case M_SWLE_AB:
7f3c4072
CM
11707 s = "swle";
11708 fmt = "t,+j(b)";
11709 offbits = 9;
11710 goto ld_st;
11711 case M_SWRE_AB:
7f3c4072
CM
11712 s = "swre";
11713 fmt = "t,+j(b)";
11714 offbits = 9;
11715 goto ld_st;
dec0624d 11716 case M_ACLR_AB:
dec0624d 11717 s = "aclr";
dec0624d 11718 fmt = "\\,~(b)";
7f3c4072 11719 offbits = 12;
dec0624d
MR
11720 goto ld_st;
11721 case M_ASET_AB:
dec0624d 11722 s = "aset";
dec0624d 11723 fmt = "\\,~(b)";
7f3c4072 11724 offbits = 12;
dec0624d 11725 goto ld_st;
252b5132
RH
11726 case M_LB_AB:
11727 s = "lb";
df58fc94 11728 fmt = "t,o(b)";
252b5132
RH
11729 goto ld;
11730 case M_LBU_AB:
11731 s = "lbu";
df58fc94 11732 fmt = "t,o(b)";
252b5132
RH
11733 goto ld;
11734 case M_LH_AB:
11735 s = "lh";
df58fc94 11736 fmt = "t,o(b)";
252b5132
RH
11737 goto ld;
11738 case M_LHU_AB:
11739 s = "lhu";
df58fc94 11740 fmt = "t,o(b)";
252b5132
RH
11741 goto ld;
11742 case M_LW_AB:
11743 s = "lw";
df58fc94 11744 fmt = "t,o(b)";
252b5132
RH
11745 goto ld;
11746 case M_LWC0_AB:
df58fc94 11747 gas_assert (!mips_opts.micromips);
252b5132 11748 s = "lwc0";
df58fc94 11749 fmt = "E,o(b)";
bdaaa2e1 11750 /* Itbl support may require additional care here. */
252b5132 11751 coproc = 1;
df58fc94 11752 goto ld_st;
252b5132
RH
11753 case M_LWC1_AB:
11754 s = "lwc1";
df58fc94 11755 fmt = "T,o(b)";
bdaaa2e1 11756 /* Itbl support may require additional care here. */
252b5132 11757 coproc = 1;
df58fc94 11758 goto ld_st;
252b5132
RH
11759 case M_LWC2_AB:
11760 s = "lwc2";
df58fc94 11761 fmt = COP12_FMT;
7361da2c
AB
11762 offbits = (mips_opts.micromips ? 12
11763 : ISA_IS_R6 (mips_opts.isa) ? 11
11764 : 16);
bdaaa2e1 11765 /* Itbl support may require additional care here. */
252b5132 11766 coproc = 1;
df58fc94 11767 goto ld_st;
252b5132 11768 case M_LWC3_AB:
df58fc94 11769 gas_assert (!mips_opts.micromips);
252b5132 11770 s = "lwc3";
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_LWL_AB:
11776 s = "lwl";
df58fc94 11777 fmt = MEM12_FMT;
7f3c4072 11778 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11779 goto ld_st;
252b5132
RH
11780 case M_LWR_AB:
11781 s = "lwr";
df58fc94 11782 fmt = MEM12_FMT;
7f3c4072 11783 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11784 goto ld_st;
252b5132 11785 case M_LDC1_AB:
252b5132 11786 s = "ldc1";
df58fc94 11787 fmt = "T,o(b)";
bdaaa2e1 11788 /* Itbl support may require additional care here. */
252b5132 11789 coproc = 1;
df58fc94 11790 goto ld_st;
252b5132
RH
11791 case M_LDC2_AB:
11792 s = "ldc2";
df58fc94 11793 fmt = COP12_FMT;
7361da2c
AB
11794 offbits = (mips_opts.micromips ? 12
11795 : ISA_IS_R6 (mips_opts.isa) ? 11
11796 : 16);
bdaaa2e1 11797 /* Itbl support may require additional care here. */
252b5132 11798 coproc = 1;
df58fc94 11799 goto ld_st;
c77c0862 11800 case M_LQC2_AB:
c77c0862 11801 s = "lqc2";
14daeee3 11802 fmt = "+7,o(b)";
c77c0862
RS
11803 /* Itbl support may require additional care here. */
11804 coproc = 1;
11805 goto ld_st;
252b5132
RH
11806 case M_LDC3_AB:
11807 s = "ldc3";
df58fc94 11808 fmt = "E,o(b)";
bdaaa2e1 11809 /* Itbl support may require additional care here. */
252b5132 11810 coproc = 1;
df58fc94 11811 goto ld_st;
252b5132
RH
11812 case M_LDL_AB:
11813 s = "ldl";
df58fc94 11814 fmt = MEM12_FMT;
7f3c4072 11815 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11816 goto ld_st;
252b5132
RH
11817 case M_LDR_AB:
11818 s = "ldr";
df58fc94 11819 fmt = MEM12_FMT;
7f3c4072 11820 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11821 goto ld_st;
252b5132
RH
11822 case M_LL_AB:
11823 s = "ll";
7361da2c
AB
11824 fmt = LL_SC_FMT;
11825 offbits = (mips_opts.micromips ? 12
11826 : ISA_IS_R6 (mips_opts.isa) ? 9
11827 : 16);
252b5132
RH
11828 goto ld;
11829 case M_LLD_AB:
11830 s = "lld";
7361da2c
AB
11831 fmt = LL_SC_FMT;
11832 offbits = (mips_opts.micromips ? 12
11833 : ISA_IS_R6 (mips_opts.isa) ? 9
11834 : 16);
252b5132
RH
11835 goto ld;
11836 case M_LWU_AB:
11837 s = "lwu";
df58fc94 11838 fmt = MEM12_FMT;
7f3c4072 11839 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
11840 goto ld;
11841 case M_LWP_AB:
df58fc94
RS
11842 gas_assert (mips_opts.micromips);
11843 s = "lwp";
11844 fmt = "t,~(b)";
7f3c4072 11845 offbits = 12;
df58fc94
RS
11846 lp = 1;
11847 goto ld;
11848 case M_LDP_AB:
df58fc94
RS
11849 gas_assert (mips_opts.micromips);
11850 s = "ldp";
11851 fmt = "t,~(b)";
7f3c4072 11852 offbits = 12;
df58fc94
RS
11853 lp = 1;
11854 goto ld;
11855 case M_LWM_AB:
df58fc94
RS
11856 gas_assert (mips_opts.micromips);
11857 s = "lwm";
11858 fmt = "n,~(b)";
7f3c4072 11859 offbits = 12;
df58fc94
RS
11860 goto ld_st;
11861 case M_LDM_AB:
df58fc94
RS
11862 gas_assert (mips_opts.micromips);
11863 s = "ldm";
11864 fmt = "n,~(b)";
7f3c4072 11865 offbits = 12;
df58fc94
RS
11866 goto ld_st;
11867
252b5132 11868 ld:
f19ccbda 11869 /* We don't want to use $0 as tempreg. */
c0ebe874 11870 if (op[2] == op[0] + lp || op[0] + lp == ZERO)
df58fc94 11871 goto ld_st;
252b5132 11872 else
c0ebe874 11873 tempreg = op[0] + lp;
df58fc94
RS
11874 goto ld_noat;
11875
252b5132
RH
11876 case M_SB_AB:
11877 s = "sb";
df58fc94
RS
11878 fmt = "t,o(b)";
11879 goto ld_st;
252b5132
RH
11880 case M_SH_AB:
11881 s = "sh";
df58fc94
RS
11882 fmt = "t,o(b)";
11883 goto ld_st;
252b5132
RH
11884 case M_SW_AB:
11885 s = "sw";
df58fc94
RS
11886 fmt = "t,o(b)";
11887 goto ld_st;
252b5132 11888 case M_SWC0_AB:
df58fc94 11889 gas_assert (!mips_opts.micromips);
252b5132 11890 s = "swc0";
df58fc94 11891 fmt = "E,o(b)";
bdaaa2e1 11892 /* Itbl support may require additional care here. */
252b5132 11893 coproc = 1;
df58fc94 11894 goto ld_st;
252b5132
RH
11895 case M_SWC1_AB:
11896 s = "swc1";
df58fc94 11897 fmt = "T,o(b)";
bdaaa2e1 11898 /* Itbl support may require additional care here. */
252b5132 11899 coproc = 1;
df58fc94 11900 goto ld_st;
252b5132
RH
11901 case M_SWC2_AB:
11902 s = "swc2";
df58fc94 11903 fmt = COP12_FMT;
7361da2c
AB
11904 offbits = (mips_opts.micromips ? 12
11905 : ISA_IS_R6 (mips_opts.isa) ? 11
11906 : 16);
bdaaa2e1 11907 /* Itbl support may require additional care here. */
252b5132 11908 coproc = 1;
df58fc94 11909 goto ld_st;
252b5132 11910 case M_SWC3_AB:
df58fc94 11911 gas_assert (!mips_opts.micromips);
252b5132 11912 s = "swc3";
df58fc94 11913 fmt = "E,o(b)";
bdaaa2e1 11914 /* Itbl support may require additional care here. */
252b5132 11915 coproc = 1;
df58fc94 11916 goto ld_st;
252b5132
RH
11917 case M_SWL_AB:
11918 s = "swl";
df58fc94 11919 fmt = MEM12_FMT;
7f3c4072 11920 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11921 goto ld_st;
252b5132
RH
11922 case M_SWR_AB:
11923 s = "swr";
df58fc94 11924 fmt = MEM12_FMT;
7f3c4072 11925 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11926 goto ld_st;
252b5132
RH
11927 case M_SC_AB:
11928 s = "sc";
7361da2c
AB
11929 fmt = LL_SC_FMT;
11930 offbits = (mips_opts.micromips ? 12
11931 : ISA_IS_R6 (mips_opts.isa) ? 9
11932 : 16);
df58fc94 11933 goto ld_st;
252b5132
RH
11934 case M_SCD_AB:
11935 s = "scd";
7361da2c
AB
11936 fmt = LL_SC_FMT;
11937 offbits = (mips_opts.micromips ? 12
11938 : ISA_IS_R6 (mips_opts.isa) ? 9
11939 : 16);
df58fc94 11940 goto ld_st;
d43b4baf
TS
11941 case M_CACHE_AB:
11942 s = "cache";
7361da2c
AB
11943 fmt = (mips_opts.micromips ? "k,~(b)"
11944 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
11945 : "k,o(b)");
11946 offbits = (mips_opts.micromips ? 12
11947 : ISA_IS_R6 (mips_opts.isa) ? 9
11948 : 16);
7f3c4072
CM
11949 goto ld_st;
11950 case M_CACHEE_AB:
7f3c4072
CM
11951 s = "cachee";
11952 fmt = "k,+j(b)";
11953 offbits = 9;
df58fc94 11954 goto ld_st;
3eebd5eb
MR
11955 case M_PREF_AB:
11956 s = "pref";
7361da2c
AB
11957 fmt = (mips_opts.micromips ? "k,~(b)"
11958 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
11959 : "k,o(b)");
11960 offbits = (mips_opts.micromips ? 12
11961 : ISA_IS_R6 (mips_opts.isa) ? 9
11962 : 16);
7f3c4072
CM
11963 goto ld_st;
11964 case M_PREFE_AB:
7f3c4072
CM
11965 s = "prefe";
11966 fmt = "k,+j(b)";
11967 offbits = 9;
df58fc94 11968 goto ld_st;
252b5132 11969 case M_SDC1_AB:
252b5132 11970 s = "sdc1";
df58fc94 11971 fmt = "T,o(b)";
252b5132 11972 coproc = 1;
bdaaa2e1 11973 /* Itbl support may require additional care here. */
df58fc94 11974 goto ld_st;
252b5132
RH
11975 case M_SDC2_AB:
11976 s = "sdc2";
df58fc94 11977 fmt = COP12_FMT;
7361da2c
AB
11978 offbits = (mips_opts.micromips ? 12
11979 : ISA_IS_R6 (mips_opts.isa) ? 11
11980 : 16);
c77c0862
RS
11981 /* Itbl support may require additional care here. */
11982 coproc = 1;
11983 goto ld_st;
11984 case M_SQC2_AB:
c77c0862 11985 s = "sqc2";
14daeee3 11986 fmt = "+7,o(b)";
bdaaa2e1 11987 /* Itbl support may require additional care here. */
252b5132 11988 coproc = 1;
df58fc94 11989 goto ld_st;
252b5132 11990 case M_SDC3_AB:
df58fc94 11991 gas_assert (!mips_opts.micromips);
252b5132 11992 s = "sdc3";
df58fc94 11993 fmt = "E,o(b)";
bdaaa2e1 11994 /* Itbl support may require additional care here. */
252b5132 11995 coproc = 1;
df58fc94 11996 goto ld_st;
252b5132
RH
11997 case M_SDL_AB:
11998 s = "sdl";
df58fc94 11999 fmt = MEM12_FMT;
7f3c4072 12000 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 12001 goto ld_st;
252b5132
RH
12002 case M_SDR_AB:
12003 s = "sdr";
df58fc94 12004 fmt = MEM12_FMT;
7f3c4072 12005 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
12006 goto ld_st;
12007 case M_SWP_AB:
df58fc94
RS
12008 gas_assert (mips_opts.micromips);
12009 s = "swp";
12010 fmt = "t,~(b)";
7f3c4072 12011 offbits = 12;
df58fc94
RS
12012 goto ld_st;
12013 case M_SDP_AB:
df58fc94
RS
12014 gas_assert (mips_opts.micromips);
12015 s = "sdp";
12016 fmt = "t,~(b)";
7f3c4072 12017 offbits = 12;
df58fc94
RS
12018 goto ld_st;
12019 case M_SWM_AB:
df58fc94
RS
12020 gas_assert (mips_opts.micromips);
12021 s = "swm";
12022 fmt = "n,~(b)";
7f3c4072 12023 offbits = 12;
df58fc94
RS
12024 goto ld_st;
12025 case M_SDM_AB:
df58fc94
RS
12026 gas_assert (mips_opts.micromips);
12027 s = "sdm";
12028 fmt = "n,~(b)";
7f3c4072 12029 offbits = 12;
df58fc94
RS
12030
12031 ld_st:
8fc2e39e 12032 tempreg = AT;
df58fc94 12033 ld_noat:
c0ebe874 12034 breg = op[2];
f2ae14a1
RS
12035 if (small_offset_p (0, align, 16))
12036 {
12037 /* The first case exists for M_LD_AB and M_SD_AB, which are
12038 macros for o32 but which should act like normal instructions
12039 otherwise. */
12040 if (offbits == 16)
c0ebe874 12041 macro_build (&offset_expr, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
12042 offset_reloc[1], offset_reloc[2], breg);
12043 else if (small_offset_p (0, align, offbits))
12044 {
12045 if (offbits == 0)
c0ebe874 12046 macro_build (NULL, s, fmt, op[0], breg);
f2ae14a1 12047 else
c0ebe874 12048 macro_build (NULL, s, fmt, op[0],
c8276761 12049 (int) offset_expr.X_add_number, breg);
f2ae14a1
RS
12050 }
12051 else
12052 {
12053 if (tempreg == AT)
12054 used_at = 1;
12055 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
12056 tempreg, breg, -1, offset_reloc[0],
12057 offset_reloc[1], offset_reloc[2]);
12058 if (offbits == 0)
c0ebe874 12059 macro_build (NULL, s, fmt, op[0], tempreg);
f2ae14a1 12060 else
c0ebe874 12061 macro_build (NULL, s, fmt, op[0], 0, tempreg);
f2ae14a1
RS
12062 }
12063 break;
12064 }
12065
12066 if (tempreg == AT)
12067 used_at = 1;
12068
252b5132
RH
12069 if (offset_expr.X_op != O_constant
12070 && offset_expr.X_op != O_symbol)
12071 {
1661c76c 12072 as_bad (_("expression too complex"));
252b5132
RH
12073 offset_expr.X_op = O_constant;
12074 }
12075
2051e8c4
MR
12076 if (HAVE_32BIT_ADDRESSES
12077 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
12078 {
12079 char value [32];
12080
12081 sprintf_vma (value, offset_expr.X_add_number);
1661c76c 12082 as_bad (_("number (0x%s) larger than 32 bits"), value);
55e08f71 12083 }
2051e8c4 12084
252b5132
RH
12085 /* A constant expression in PIC code can be handled just as it
12086 is in non PIC code. */
aed1a261
RS
12087 if (offset_expr.X_op == O_constant)
12088 {
f2ae14a1
RS
12089 expr1.X_add_number = offset_high_part (offset_expr.X_add_number,
12090 offbits == 0 ? 16 : offbits);
12091 offset_expr.X_add_number -= expr1.X_add_number;
df58fc94 12092
f2ae14a1
RS
12093 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
12094 if (breg != 0)
12095 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12096 tempreg, tempreg, breg);
7f3c4072 12097 if (offbits == 0)
dd6a37e7 12098 {
f2ae14a1 12099 if (offset_expr.X_add_number != 0)
dd6a37e7 12100 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
f2ae14a1 12101 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
c0ebe874 12102 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 12103 }
7f3c4072 12104 else if (offbits == 16)
c0ebe874 12105 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
df58fc94 12106 else
c0ebe874 12107 macro_build (NULL, s, fmt, op[0],
c8276761 12108 (int) offset_expr.X_add_number, tempreg);
df58fc94 12109 }
7f3c4072 12110 else if (offbits != 16)
df58fc94 12111 {
7f3c4072 12112 /* The offset field is too narrow to be used for a low-part
2b0f3761 12113 relocation, so load the whole address into the auxiliary
f2ae14a1
RS
12114 register. */
12115 load_address (tempreg, &offset_expr, &used_at);
12116 if (breg != 0)
12117 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12118 tempreg, tempreg, breg);
7f3c4072 12119 if (offbits == 0)
c0ebe874 12120 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 12121 else
c0ebe874 12122 macro_build (NULL, s, fmt, op[0], 0, tempreg);
aed1a261
RS
12123 }
12124 else if (mips_pic == NO_PIC)
252b5132
RH
12125 {
12126 /* If this is a reference to a GP relative symbol, and there
12127 is no base register, we want
c0ebe874 12128 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
12129 Otherwise, if there is no base register, we want
12130 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
c0ebe874 12131 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
252b5132
RH
12132 If we have a constant, we need two instructions anyhow,
12133 so we always use the latter form.
12134
12135 If we have a base register, and this is a reference to a
12136 GP relative symbol, we want
12137 addu $tempreg,$breg,$gp
c0ebe874 12138 <op> op[0],<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
12139 Otherwise we want
12140 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
12141 addu $tempreg,$tempreg,$breg
c0ebe874 12142 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 12143 With a constant we always use the latter case.
76b3015f 12144
d6bc6245
TS
12145 With 64bit address space and no base register and $at usable,
12146 we want
12147 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12148 lui $at,<sym> (BFD_RELOC_HI16_S)
12149 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12150 dsll32 $tempreg,0
12151 daddu $tempreg,$at
c0ebe874 12152 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
12153 If we have a base register, we want
12154 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12155 lui $at,<sym> (BFD_RELOC_HI16_S)
12156 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12157 daddu $at,$breg
12158 dsll32 $tempreg,0
12159 daddu $tempreg,$at
c0ebe874 12160 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
12161
12162 Without $at we can't generate the optimal path for superscalar
12163 processors here since this would require two temporary registers.
12164 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12165 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12166 dsll $tempreg,16
12167 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
12168 dsll $tempreg,16
c0ebe874 12169 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
12170 If we have a base register, we want
12171 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12172 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12173 dsll $tempreg,16
12174 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
12175 dsll $tempreg,16
12176 daddu $tempreg,$tempreg,$breg
c0ebe874 12177 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 12178
6caf9ef4 12179 For GP relative symbols in 64bit address space we can use
aed1a261
RS
12180 the same sequence as in 32bit address space. */
12181 if (HAVE_64BIT_SYMBOLS)
d6bc6245 12182 {
aed1a261 12183 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
12184 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12185 {
12186 relax_start (offset_expr.X_add_symbol);
12187 if (breg == 0)
12188 {
c0ebe874 12189 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
12190 BFD_RELOC_GPREL16, mips_gp_register);
12191 }
12192 else
12193 {
12194 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12195 tempreg, breg, mips_gp_register);
c0ebe874 12196 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
12197 BFD_RELOC_GPREL16, tempreg);
12198 }
12199 relax_switch ();
12200 }
d6bc6245 12201
741fe287 12202 if (used_at == 0 && mips_opts.at)
d6bc6245 12203 {
df58fc94 12204 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb 12205 BFD_RELOC_MIPS_HIGHEST);
df58fc94 12206 macro_build (&offset_expr, "lui", LUI_FMT, AT,
67c0d1eb
RS
12207 BFD_RELOC_HI16_S);
12208 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12209 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 12210 if (breg != 0)
67c0d1eb 12211 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
df58fc94 12212 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 12213 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
c0ebe874 12214 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16,
67c0d1eb 12215 tempreg);
d6bc6245
TS
12216 used_at = 1;
12217 }
12218 else
12219 {
df58fc94 12220 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb
RS
12221 BFD_RELOC_MIPS_HIGHEST);
12222 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12223 tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 12224 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb
RS
12225 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12226 tempreg, BFD_RELOC_HI16_S);
df58fc94 12227 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
d6bc6245 12228 if (breg != 0)
67c0d1eb 12229 macro_build (NULL, "daddu", "d,v,t",
17a2f251 12230 tempreg, tempreg, breg);
c0ebe874 12231 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12232 BFD_RELOC_LO16, tempreg);
d6bc6245 12233 }
6caf9ef4
TS
12234
12235 if (mips_relax.sequence)
12236 relax_end ();
8fc2e39e 12237 break;
d6bc6245 12238 }
256ab948 12239
252b5132
RH
12240 if (breg == 0)
12241 {
67c0d1eb 12242 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12243 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12244 {
4d7206a2 12245 relax_start (offset_expr.X_add_symbol);
c0ebe874 12246 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_GPREL16,
67c0d1eb 12247 mips_gp_register);
4d7206a2 12248 relax_switch ();
252b5132 12249 }
67c0d1eb 12250 macro_build_lui (&offset_expr, tempreg);
c0ebe874 12251 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12252 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
12253 if (mips_relax.sequence)
12254 relax_end ();
252b5132
RH
12255 }
12256 else
12257 {
67c0d1eb 12258 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12259 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12260 {
4d7206a2 12261 relax_start (offset_expr.X_add_symbol);
67c0d1eb 12262 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12263 tempreg, breg, mips_gp_register);
c0ebe874 12264 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12265 BFD_RELOC_GPREL16, tempreg);
4d7206a2 12266 relax_switch ();
252b5132 12267 }
67c0d1eb
RS
12268 macro_build_lui (&offset_expr, tempreg);
12269 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12270 tempreg, tempreg, breg);
c0ebe874 12271 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12272 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
12273 if (mips_relax.sequence)
12274 relax_end ();
252b5132
RH
12275 }
12276 }
0a44bf69 12277 else if (!mips_big_got)
252b5132 12278 {
ed6fb7bd 12279 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 12280
252b5132
RH
12281 /* If this is a reference to an external symbol, we want
12282 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12283 nop
c0ebe874 12284 <op> op[0],0($tempreg)
252b5132
RH
12285 Otherwise we want
12286 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12287 nop
12288 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 12289 <op> op[0],0($tempreg)
f5040a92
AO
12290
12291 For NewABI, we want
12292 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 12293 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 12294
252b5132
RH
12295 If there is a base register, we add it to $tempreg before
12296 the <op>. If there is a constant, we stick it in the
12297 <op> instruction. We don't handle constants larger than
12298 16 bits, because we have no way to load the upper 16 bits
12299 (actually, we could handle them for the subset of cases
12300 in which we are not using $at). */
9c2799c2 12301 gas_assert (offset_expr.X_op == O_symbol);
f5040a92
AO
12302 if (HAVE_NEWABI)
12303 {
67c0d1eb
RS
12304 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12305 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 12306 if (breg != 0)
67c0d1eb 12307 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12308 tempreg, tempreg, breg);
c0ebe874 12309 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12310 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
12311 break;
12312 }
252b5132
RH
12313 expr1.X_add_number = offset_expr.X_add_number;
12314 offset_expr.X_add_number = 0;
12315 if (expr1.X_add_number < -0x8000
12316 || expr1.X_add_number >= 0x8000)
12317 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
12318 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12319 lw_reloc_type, mips_gp_register);
269137b2 12320 load_delay_nop ();
4d7206a2
RS
12321 relax_start (offset_expr.X_add_symbol);
12322 relax_switch ();
67c0d1eb
RS
12323 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12324 tempreg, BFD_RELOC_LO16);
4d7206a2 12325 relax_end ();
252b5132 12326 if (breg != 0)
67c0d1eb 12327 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12328 tempreg, tempreg, breg);
c0ebe874 12329 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 12330 }
0a44bf69 12331 else if (mips_big_got && !HAVE_NEWABI)
252b5132 12332 {
67c0d1eb 12333 int gpdelay;
252b5132
RH
12334
12335 /* If this is a reference to an external symbol, we want
12336 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12337 addu $tempreg,$tempreg,$gp
12338 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 12339 <op> op[0],0($tempreg)
252b5132
RH
12340 Otherwise we want
12341 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12342 nop
12343 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 12344 <op> op[0],0($tempreg)
252b5132
RH
12345 If there is a base register, we add it to $tempreg before
12346 the <op>. If there is a constant, we stick it in the
12347 <op> instruction. We don't handle constants larger than
12348 16 bits, because we have no way to load the upper 16 bits
12349 (actually, we could handle them for the subset of cases
f5040a92 12350 in which we are not using $at). */
9c2799c2 12351 gas_assert (offset_expr.X_op == O_symbol);
252b5132
RH
12352 expr1.X_add_number = offset_expr.X_add_number;
12353 offset_expr.X_add_number = 0;
12354 if (expr1.X_add_number < -0x8000
12355 || expr1.X_add_number >= 0x8000)
12356 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 12357 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 12358 relax_start (offset_expr.X_add_symbol);
df58fc94 12359 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 12360 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
12361 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12362 mips_gp_register);
12363 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12364 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 12365 relax_switch ();
67c0d1eb
RS
12366 if (gpdelay)
12367 macro_build (NULL, "nop", "");
12368 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12369 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 12370 load_delay_nop ();
67c0d1eb
RS
12371 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12372 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
12373 relax_end ();
12374
252b5132 12375 if (breg != 0)
67c0d1eb 12376 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12377 tempreg, tempreg, breg);
c0ebe874 12378 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 12379 }
0a44bf69 12380 else if (mips_big_got && HAVE_NEWABI)
f5040a92 12381 {
f5040a92
AO
12382 /* If this is a reference to an external symbol, we want
12383 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12384 add $tempreg,$tempreg,$gp
12385 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 12386 <op> op[0],<ofst>($tempreg)
f5040a92
AO
12387 Otherwise, for local symbols, we want:
12388 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 12389 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
9c2799c2 12390 gas_assert (offset_expr.X_op == O_symbol);
4d7206a2 12391 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
12392 offset_expr.X_add_number = 0;
12393 if (expr1.X_add_number < -0x8000
12394 || expr1.X_add_number >= 0x8000)
12395 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 12396 relax_start (offset_expr.X_add_symbol);
df58fc94 12397 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 12398 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
12399 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12400 mips_gp_register);
12401 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12402 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 12403 if (breg != 0)
67c0d1eb 12404 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12405 tempreg, tempreg, breg);
c0ebe874 12406 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
684022ea 12407
4d7206a2 12408 relax_switch ();
f5040a92 12409 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
12410 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12411 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 12412 if (breg != 0)
67c0d1eb 12413 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12414 tempreg, tempreg, breg);
c0ebe874 12415 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12416 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 12417 relax_end ();
f5040a92 12418 }
252b5132
RH
12419 else
12420 abort ();
12421
252b5132
RH
12422 break;
12423
833794fc
MR
12424 case M_JRADDIUSP:
12425 gas_assert (mips_opts.micromips);
12426 gas_assert (mips_opts.insn32);
12427 start_noreorder ();
12428 macro_build (NULL, "jr", "s", RA);
c0ebe874 12429 expr1.X_add_number = op[0] << 2;
833794fc
MR
12430 macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
12431 end_noreorder ();
12432 break;
12433
12434 case M_JRC:
12435 gas_assert (mips_opts.micromips);
12436 gas_assert (mips_opts.insn32);
c0ebe874 12437 macro_build (NULL, "jr", "s", op[0]);
833794fc
MR
12438 if (mips_opts.noreorder)
12439 macro_build (NULL, "nop", "");
12440 break;
12441
252b5132
RH
12442 case M_LI:
12443 case M_LI_S:
c0ebe874 12444 load_register (op[0], &imm_expr, 0);
8fc2e39e 12445 break;
252b5132
RH
12446
12447 case M_DLI:
c0ebe874 12448 load_register (op[0], &imm_expr, 1);
8fc2e39e 12449 break;
252b5132
RH
12450
12451 case M_LI_SS:
12452 if (imm_expr.X_op == O_constant)
12453 {
8fc2e39e 12454 used_at = 1;
67c0d1eb 12455 load_register (AT, &imm_expr, 0);
c0ebe874 12456 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
12457 break;
12458 }
12459 else
12460 {
b0e6f033
RS
12461 gas_assert (imm_expr.X_op == O_absent
12462 && offset_expr.X_op == O_symbol
90ecf173
MR
12463 && strcmp (segment_name (S_GET_SEGMENT
12464 (offset_expr.X_add_symbol)),
12465 ".lit4") == 0
12466 && offset_expr.X_add_number == 0);
c0ebe874 12467 macro_build (&offset_expr, "lwc1", "T,o(b)", op[0],
17a2f251 12468 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 12469 break;
252b5132
RH
12470 }
12471
12472 case M_LI_D:
ca4e0257
RS
12473 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
12474 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
12475 order 32 bits of the value and the low order 32 bits are either
12476 zero or in OFFSET_EXPR. */
b0e6f033 12477 if (imm_expr.X_op == O_constant)
252b5132 12478 {
bad1aba3 12479 if (GPR_SIZE == 64)
c0ebe874 12480 load_register (op[0], &imm_expr, 1);
252b5132
RH
12481 else
12482 {
12483 int hreg, lreg;
12484
12485 if (target_big_endian)
12486 {
c0ebe874
RS
12487 hreg = op[0];
12488 lreg = op[0] + 1;
252b5132
RH
12489 }
12490 else
12491 {
c0ebe874
RS
12492 hreg = op[0] + 1;
12493 lreg = op[0];
252b5132
RH
12494 }
12495
12496 if (hreg <= 31)
67c0d1eb 12497 load_register (hreg, &imm_expr, 0);
252b5132
RH
12498 if (lreg <= 31)
12499 {
12500 if (offset_expr.X_op == O_absent)
67c0d1eb 12501 move_register (lreg, 0);
252b5132
RH
12502 else
12503 {
9c2799c2 12504 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 12505 load_register (lreg, &offset_expr, 0);
252b5132
RH
12506 }
12507 }
12508 }
8fc2e39e 12509 break;
252b5132 12510 }
b0e6f033 12511 gas_assert (imm_expr.X_op == O_absent);
252b5132
RH
12512
12513 /* We know that sym is in the .rdata section. First we get the
12514 upper 16 bits of the address. */
12515 if (mips_pic == NO_PIC)
12516 {
67c0d1eb 12517 macro_build_lui (&offset_expr, AT);
8fc2e39e 12518 used_at = 1;
252b5132 12519 }
0a44bf69 12520 else
252b5132 12521 {
67c0d1eb
RS
12522 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12523 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 12524 used_at = 1;
252b5132 12525 }
bdaaa2e1 12526
252b5132 12527 /* Now we load the register(s). */
bad1aba3 12528 if (GPR_SIZE == 64)
8fc2e39e
TS
12529 {
12530 used_at = 1;
c0ebe874
RS
12531 macro_build (&offset_expr, "ld", "t,o(b)", op[0],
12532 BFD_RELOC_LO16, AT);
8fc2e39e 12533 }
252b5132
RH
12534 else
12535 {
8fc2e39e 12536 used_at = 1;
c0ebe874
RS
12537 macro_build (&offset_expr, "lw", "t,o(b)", op[0],
12538 BFD_RELOC_LO16, AT);
12539 if (op[0] != RA)
252b5132
RH
12540 {
12541 /* FIXME: How in the world do we deal with the possible
12542 overflow here? */
12543 offset_expr.X_add_number += 4;
67c0d1eb 12544 macro_build (&offset_expr, "lw", "t,o(b)",
c0ebe874 12545 op[0] + 1, BFD_RELOC_LO16, AT);
252b5132
RH
12546 }
12547 }
252b5132
RH
12548 break;
12549
12550 case M_LI_DD:
ca4e0257
RS
12551 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
12552 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
12553 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
12554 the value and the low order 32 bits are either zero or in
12555 OFFSET_EXPR. */
b0e6f033 12556 if (imm_expr.X_op == O_constant)
252b5132 12557 {
8fc2e39e 12558 used_at = 1;
bad1aba3 12559 load_register (AT, &imm_expr, FPR_SIZE == 64);
351cdf24
MF
12560 if (FPR_SIZE == 64 && GPR_SIZE == 64)
12561 macro_build (NULL, "dmtc1", "t,S", AT, op[0]);
252b5132
RH
12562 else
12563 {
351cdf24
MF
12564 if (ISA_HAS_MXHC1 (mips_opts.isa))
12565 macro_build (NULL, "mthc1", "t,G", AT, op[0]);
12566 else if (FPR_SIZE != 32)
12567 as_bad (_("Unable to generate `%s' compliant code "
12568 "without mthc1"),
12569 (FPR_SIZE == 64) ? "fp64" : "fpxx");
12570 else
12571 macro_build (NULL, "mtc1", "t,G", AT, op[0] + 1);
252b5132 12572 if (offset_expr.X_op == O_absent)
c0ebe874 12573 macro_build (NULL, "mtc1", "t,G", 0, op[0]);
252b5132
RH
12574 else
12575 {
9c2799c2 12576 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 12577 load_register (AT, &offset_expr, 0);
c0ebe874 12578 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
12579 }
12580 }
12581 break;
12582 }
12583
b0e6f033
RS
12584 gas_assert (imm_expr.X_op == O_absent
12585 && offset_expr.X_op == O_symbol
90ecf173 12586 && offset_expr.X_add_number == 0);
252b5132
RH
12587 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
12588 if (strcmp (s, ".lit8") == 0)
134c0c8b
MR
12589 {
12590 op[2] = mips_gp_register;
f2ae14a1
RS
12591 offset_reloc[0] = BFD_RELOC_MIPS_LITERAL;
12592 offset_reloc[1] = BFD_RELOC_UNUSED;
12593 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
12594 }
12595 else
12596 {
9c2799c2 12597 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 12598 used_at = 1;
0a44bf69 12599 if (mips_pic != NO_PIC)
67c0d1eb
RS
12600 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12601 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
12602 else
12603 {
12604 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 12605 macro_build_lui (&offset_expr, AT);
252b5132 12606 }
bdaaa2e1 12607
c0ebe874 12608 op[2] = AT;
f2ae14a1
RS
12609 offset_reloc[0] = BFD_RELOC_LO16;
12610 offset_reloc[1] = BFD_RELOC_UNUSED;
12611 offset_reloc[2] = BFD_RELOC_UNUSED;
134c0c8b 12612 }
f2ae14a1
RS
12613 align = 8;
12614 /* Fall through */
c4a68bea 12615
252b5132
RH
12616 case M_L_DAB:
12617 /*
12618 * The MIPS assembler seems to check for X_add_number not
12619 * being double aligned and generating:
12620 * lui at,%hi(foo+1)
12621 * addu at,at,v1
12622 * addiu at,at,%lo(foo+1)
12623 * lwc1 f2,0(at)
12624 * lwc1 f3,4(at)
12625 * But, the resulting address is the same after relocation so why
12626 * generate the extra instruction?
12627 */
bdaaa2e1 12628 /* Itbl support may require additional care here. */
252b5132 12629 coproc = 1;
df58fc94 12630 fmt = "T,o(b)";
0aa27725 12631 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
12632 {
12633 s = "ldc1";
df58fc94 12634 goto ld_st;
252b5132 12635 }
252b5132 12636 s = "lwc1";
252b5132
RH
12637 goto ldd_std;
12638
12639 case M_S_DAB:
df58fc94
RS
12640 gas_assert (!mips_opts.micromips);
12641 /* Itbl support may require additional care here. */
12642 coproc = 1;
12643 fmt = "T,o(b)";
0aa27725 12644 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
12645 {
12646 s = "sdc1";
df58fc94 12647 goto ld_st;
252b5132 12648 }
252b5132 12649 s = "swc1";
252b5132
RH
12650 goto ldd_std;
12651
e407c74b
NC
12652 case M_LQ_AB:
12653 fmt = "t,o(b)";
12654 s = "lq";
12655 goto ld;
12656
12657 case M_SQ_AB:
12658 fmt = "t,o(b)";
12659 s = "sq";
12660 goto ld_st;
12661
252b5132 12662 case M_LD_AB:
df58fc94 12663 fmt = "t,o(b)";
bad1aba3 12664 if (GPR_SIZE == 64)
252b5132
RH
12665 {
12666 s = "ld";
12667 goto ld;
12668 }
252b5132 12669 s = "lw";
252b5132
RH
12670 goto ldd_std;
12671
12672 case M_SD_AB:
df58fc94 12673 fmt = "t,o(b)";
bad1aba3 12674 if (GPR_SIZE == 64)
252b5132
RH
12675 {
12676 s = "sd";
df58fc94 12677 goto ld_st;
252b5132 12678 }
252b5132 12679 s = "sw";
252b5132
RH
12680
12681 ldd_std:
f2ae14a1
RS
12682 /* Even on a big endian machine $fn comes before $fn+1. We have
12683 to adjust when loading from memory. We set coproc if we must
12684 load $fn+1 first. */
12685 /* Itbl support may require additional care here. */
12686 if (!target_big_endian)
12687 coproc = 0;
12688
c0ebe874 12689 breg = op[2];
f2ae14a1
RS
12690 if (small_offset_p (0, align, 16))
12691 {
12692 ep = &offset_expr;
12693 if (!small_offset_p (4, align, 16))
12694 {
12695 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT, breg,
12696 -1, offset_reloc[0], offset_reloc[1],
12697 offset_reloc[2]);
12698 expr1.X_add_number = 0;
12699 ep = &expr1;
12700 breg = AT;
12701 used_at = 1;
12702 offset_reloc[0] = BFD_RELOC_LO16;
12703 offset_reloc[1] = BFD_RELOC_UNUSED;
12704 offset_reloc[2] = BFD_RELOC_UNUSED;
12705 }
c0ebe874 12706 if (strcmp (s, "lw") == 0 && op[0] == breg)
f2ae14a1
RS
12707 {
12708 ep->X_add_number += 4;
c0ebe874 12709 macro_build (ep, s, fmt, op[0] + 1, -1, offset_reloc[0],
f2ae14a1
RS
12710 offset_reloc[1], offset_reloc[2], breg);
12711 ep->X_add_number -= 4;
c0ebe874 12712 macro_build (ep, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
12713 offset_reloc[1], offset_reloc[2], breg);
12714 }
12715 else
12716 {
c0ebe874 12717 macro_build (ep, s, fmt, coproc ? op[0] + 1 : op[0], -1,
f2ae14a1
RS
12718 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12719 breg);
12720 ep->X_add_number += 4;
c0ebe874 12721 macro_build (ep, s, fmt, coproc ? op[0] : op[0] + 1, -1,
f2ae14a1
RS
12722 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12723 breg);
12724 }
12725 break;
12726 }
12727
252b5132
RH
12728 if (offset_expr.X_op != O_symbol
12729 && offset_expr.X_op != O_constant)
12730 {
1661c76c 12731 as_bad (_("expression too complex"));
252b5132
RH
12732 offset_expr.X_op = O_constant;
12733 }
12734
2051e8c4
MR
12735 if (HAVE_32BIT_ADDRESSES
12736 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
12737 {
12738 char value [32];
12739
12740 sprintf_vma (value, offset_expr.X_add_number);
1661c76c 12741 as_bad (_("number (0x%s) larger than 32 bits"), value);
55e08f71 12742 }
2051e8c4 12743
90ecf173 12744 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
252b5132
RH
12745 {
12746 /* If this is a reference to a GP relative symbol, we want
c0ebe874
RS
12747 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
12748 <op> op[0]+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
12749 If we have a base register, we use this
12750 addu $at,$breg,$gp
c0ebe874
RS
12751 <op> op[0],<sym>($at) (BFD_RELOC_GPREL16)
12752 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
12753 If this is not a GP relative symbol, we want
12754 lui $at,<sym> (BFD_RELOC_HI16_S)
c0ebe874
RS
12755 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12756 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
12757 If there is a base register, we add it to $at after the
12758 lui instruction. If there is a constant, we always use
12759 the last case. */
39a59cf8
MR
12760 if (offset_expr.X_op == O_symbol
12761 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12762 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12763 {
4d7206a2 12764 relax_start (offset_expr.X_add_symbol);
252b5132
RH
12765 if (breg == 0)
12766 {
c9914766 12767 tempreg = mips_gp_register;
252b5132
RH
12768 }
12769 else
12770 {
67c0d1eb 12771 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12772 AT, breg, mips_gp_register);
252b5132 12773 tempreg = AT;
252b5132
RH
12774 used_at = 1;
12775 }
12776
beae10d5 12777 /* Itbl support may require additional care here. */
c0ebe874 12778 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 12779 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
12780 offset_expr.X_add_number += 4;
12781
12782 /* Set mips_optimize to 2 to avoid inserting an
12783 undesired nop. */
12784 hold_mips_optimize = mips_optimize;
12785 mips_optimize = 2;
beae10d5 12786 /* Itbl support may require additional care here. */
c0ebe874 12787 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 12788 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
12789 mips_optimize = hold_mips_optimize;
12790
4d7206a2 12791 relax_switch ();
252b5132 12792
0970e49e 12793 offset_expr.X_add_number -= 4;
252b5132 12794 }
8fc2e39e 12795 used_at = 1;
f2ae14a1
RS
12796 if (offset_high_part (offset_expr.X_add_number, 16)
12797 != offset_high_part (offset_expr.X_add_number + 4, 16))
12798 {
12799 load_address (AT, &offset_expr, &used_at);
12800 offset_expr.X_op = O_constant;
12801 offset_expr.X_add_number = 0;
12802 }
12803 else
12804 macro_build_lui (&offset_expr, AT);
252b5132 12805 if (breg != 0)
67c0d1eb 12806 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 12807 /* Itbl support may require additional care here. */
c0ebe874 12808 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 12809 BFD_RELOC_LO16, AT);
252b5132
RH
12810 /* FIXME: How do we handle overflow here? */
12811 offset_expr.X_add_number += 4;
beae10d5 12812 /* Itbl support may require additional care here. */
c0ebe874 12813 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 12814 BFD_RELOC_LO16, AT);
4d7206a2
RS
12815 if (mips_relax.sequence)
12816 relax_end ();
bdaaa2e1 12817 }
0a44bf69 12818 else if (!mips_big_got)
252b5132 12819 {
252b5132
RH
12820 /* If this is a reference to an external symbol, we want
12821 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12822 nop
c0ebe874
RS
12823 <op> op[0],0($at)
12824 <op> op[0]+1,4($at)
252b5132
RH
12825 Otherwise we want
12826 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12827 nop
c0ebe874
RS
12828 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12829 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
12830 If there is a base register we add it to $at before the
12831 lwc1 instructions. If there is a constant we include it
12832 in the lwc1 instructions. */
12833 used_at = 1;
12834 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
12835 if (expr1.X_add_number < -0x8000
12836 || expr1.X_add_number >= 0x8000 - 4)
12837 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 12838 load_got_offset (AT, &offset_expr);
269137b2 12839 load_delay_nop ();
252b5132 12840 if (breg != 0)
67c0d1eb 12841 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
12842
12843 /* Set mips_optimize to 2 to avoid inserting an undesired
12844 nop. */
12845 hold_mips_optimize = mips_optimize;
12846 mips_optimize = 2;
4d7206a2 12847
beae10d5 12848 /* Itbl support may require additional care here. */
4d7206a2 12849 relax_start (offset_expr.X_add_symbol);
c0ebe874 12850 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 12851 BFD_RELOC_LO16, AT);
4d7206a2 12852 expr1.X_add_number += 4;
c0ebe874 12853 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 12854 BFD_RELOC_LO16, AT);
4d7206a2 12855 relax_switch ();
c0ebe874 12856 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 12857 BFD_RELOC_LO16, AT);
4d7206a2 12858 offset_expr.X_add_number += 4;
c0ebe874 12859 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 12860 BFD_RELOC_LO16, AT);
4d7206a2 12861 relax_end ();
252b5132 12862
4d7206a2 12863 mips_optimize = hold_mips_optimize;
252b5132 12864 }
0a44bf69 12865 else if (mips_big_got)
252b5132 12866 {
67c0d1eb 12867 int gpdelay;
252b5132
RH
12868
12869 /* If this is a reference to an external symbol, we want
12870 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12871 addu $at,$at,$gp
12872 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
12873 nop
c0ebe874
RS
12874 <op> op[0],0($at)
12875 <op> op[0]+1,4($at)
252b5132
RH
12876 Otherwise we want
12877 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12878 nop
c0ebe874
RS
12879 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12880 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
12881 If there is a base register we add it to $at before the
12882 lwc1 instructions. If there is a constant we include it
12883 in the lwc1 instructions. */
12884 used_at = 1;
12885 expr1.X_add_number = offset_expr.X_add_number;
12886 offset_expr.X_add_number = 0;
12887 if (expr1.X_add_number < -0x8000
12888 || expr1.X_add_number >= 0x8000 - 4)
12889 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 12890 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 12891 relax_start (offset_expr.X_add_symbol);
df58fc94 12892 macro_build (&offset_expr, "lui", LUI_FMT,
67c0d1eb
RS
12893 AT, BFD_RELOC_MIPS_GOT_HI16);
12894 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12895 AT, AT, mips_gp_register);
67c0d1eb 12896 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 12897 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 12898 load_delay_nop ();
252b5132 12899 if (breg != 0)
67c0d1eb 12900 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 12901 /* Itbl support may require additional care here. */
c0ebe874 12902 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 12903 BFD_RELOC_LO16, AT);
252b5132
RH
12904 expr1.X_add_number += 4;
12905
12906 /* Set mips_optimize to 2 to avoid inserting an undesired
12907 nop. */
12908 hold_mips_optimize = mips_optimize;
12909 mips_optimize = 2;
beae10d5 12910 /* Itbl support may require additional care here. */
c0ebe874 12911 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 12912 BFD_RELOC_LO16, AT);
252b5132
RH
12913 mips_optimize = hold_mips_optimize;
12914 expr1.X_add_number -= 4;
12915
4d7206a2
RS
12916 relax_switch ();
12917 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
12918 if (gpdelay)
12919 macro_build (NULL, "nop", "");
12920 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12921 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 12922 load_delay_nop ();
252b5132 12923 if (breg != 0)
67c0d1eb 12924 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 12925 /* Itbl support may require additional care here. */
c0ebe874 12926 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 12927 BFD_RELOC_LO16, AT);
4d7206a2 12928 offset_expr.X_add_number += 4;
252b5132
RH
12929
12930 /* Set mips_optimize to 2 to avoid inserting an undesired
12931 nop. */
12932 hold_mips_optimize = mips_optimize;
12933 mips_optimize = 2;
beae10d5 12934 /* Itbl support may require additional care here. */
c0ebe874 12935 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 12936 BFD_RELOC_LO16, AT);
252b5132 12937 mips_optimize = hold_mips_optimize;
4d7206a2 12938 relax_end ();
252b5132 12939 }
252b5132
RH
12940 else
12941 abort ();
12942
252b5132 12943 break;
3739860c 12944
dd6a37e7 12945 case M_SAA_AB:
dd6a37e7 12946 s = "saa";
0db377d0 12947 goto saa_saad;
dd6a37e7 12948 case M_SAAD_AB:
dd6a37e7 12949 s = "saad";
0db377d0
MR
12950 saa_saad:
12951 gas_assert (!mips_opts.micromips);
7f3c4072 12952 offbits = 0;
dd6a37e7
AP
12953 fmt = "t,(b)";
12954 goto ld_st;
12955
252b5132
RH
12956 /* New code added to support COPZ instructions.
12957 This code builds table entries out of the macros in mip_opcodes.
12958 R4000 uses interlocks to handle coproc delays.
12959 Other chips (like the R3000) require nops to be inserted for delays.
12960
f72c8c98 12961 FIXME: Currently, we require that the user handle delays.
252b5132
RH
12962 In order to fill delay slots for non-interlocked chips,
12963 we must have a way to specify delays based on the coprocessor.
12964 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
12965 What are the side-effects of the cop instruction?
12966 What cache support might we have and what are its effects?
12967 Both coprocessor & memory require delays. how long???
bdaaa2e1 12968 What registers are read/set/modified?
252b5132
RH
12969
12970 If an itbl is provided to interpret cop instructions,
bdaaa2e1 12971 this knowledge can be encoded in the itbl spec. */
252b5132
RH
12972
12973 case M_COP0:
12974 s = "c0";
12975 goto copz;
12976 case M_COP1:
12977 s = "c1";
12978 goto copz;
12979 case M_COP2:
12980 s = "c2";
12981 goto copz;
12982 case M_COP3:
12983 s = "c3";
12984 copz:
df58fc94 12985 gas_assert (!mips_opts.micromips);
252b5132
RH
12986 /* For now we just do C (same as Cz). The parameter will be
12987 stored in insn_opcode by mips_ip. */
c8276761 12988 macro_build (NULL, s, "C", (int) ip->insn_opcode);
8fc2e39e 12989 break;
252b5132 12990
ea1fb5dc 12991 case M_MOVE:
c0ebe874 12992 move_register (op[0], op[1]);
8fc2e39e 12993 break;
ea1fb5dc 12994
833794fc
MR
12995 case M_MOVEP:
12996 gas_assert (mips_opts.micromips);
12997 gas_assert (mips_opts.insn32);
c0ebe874
RS
12998 move_register (micromips_to_32_reg_h_map1[op[0]],
12999 micromips_to_32_reg_m_map[op[1]]);
13000 move_register (micromips_to_32_reg_h_map2[op[0]],
13001 micromips_to_32_reg_n_map[op[2]]);
833794fc
MR
13002 break;
13003
252b5132
RH
13004 case M_DMUL:
13005 dbl = 1;
1a0670f3 13006 /* Fall through. */
252b5132 13007 case M_MUL:
e407c74b 13008 if (mips_opts.arch == CPU_R5900)
c0ebe874
RS
13009 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", op[0], op[1],
13010 op[2]);
e407c74b
NC
13011 else
13012 {
c0ebe874
RS
13013 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", op[1], op[2]);
13014 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
e407c74b 13015 }
8fc2e39e 13016 break;
252b5132
RH
13017
13018 case M_DMUL_I:
13019 dbl = 1;
1a0670f3 13020 /* Fall through. */
252b5132
RH
13021 case M_MUL_I:
13022 /* The MIPS assembler some times generates shifts and adds. I'm
13023 not trying to be that fancy. GCC should do this for us
13024 anyway. */
8fc2e39e 13025 used_at = 1;
67c0d1eb 13026 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
13027 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", op[1], AT);
13028 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
13029 break;
13030
13031 case M_DMULO_I:
13032 dbl = 1;
1a0670f3 13033 /* Fall through. */
252b5132
RH
13034 case M_MULO_I:
13035 imm = 1;
13036 goto do_mulo;
13037
13038 case M_DMULO:
13039 dbl = 1;
1a0670f3 13040 /* Fall through. */
252b5132
RH
13041 case M_MULO:
13042 do_mulo:
7d10b47d 13043 start_noreorder ();
8fc2e39e 13044 used_at = 1;
252b5132 13045 if (imm)
67c0d1eb 13046 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
13047 macro_build (NULL, dbl ? "dmult" : "mult", "s,t",
13048 op[1], imm ? AT : op[2]);
13049 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13050 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, op[0], op[0], 31);
df58fc94 13051 macro_build (NULL, "mfhi", MFHL_FMT, AT);
252b5132 13052 if (mips_trap)
c0ebe874 13053 macro_build (NULL, "tne", TRAP_FMT, op[0], AT, 6);
252b5132
RH
13054 else
13055 {
df58fc94
RS
13056 if (mips_opts.micromips)
13057 micromips_label_expr (&label_expr);
13058 else
13059 label_expr.X_add_number = 8;
c0ebe874 13060 macro_build (&label_expr, "beq", "s,t,p", op[0], AT);
a605d2b3 13061 macro_build (NULL, "nop", "");
df58fc94
RS
13062 macro_build (NULL, "break", BRK_FMT, 6);
13063 if (mips_opts.micromips)
13064 micromips_add_label ();
252b5132 13065 }
7d10b47d 13066 end_noreorder ();
c0ebe874 13067 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
13068 break;
13069
13070 case M_DMULOU_I:
13071 dbl = 1;
1a0670f3 13072 /* Fall through. */
252b5132
RH
13073 case M_MULOU_I:
13074 imm = 1;
13075 goto do_mulou;
13076
13077 case M_DMULOU:
13078 dbl = 1;
1a0670f3 13079 /* Fall through. */
252b5132
RH
13080 case M_MULOU:
13081 do_mulou:
7d10b47d 13082 start_noreorder ();
8fc2e39e 13083 used_at = 1;
252b5132 13084 if (imm)
67c0d1eb
RS
13085 load_register (AT, &imm_expr, dbl);
13086 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
c0ebe874 13087 op[1], imm ? AT : op[2]);
df58fc94 13088 macro_build (NULL, "mfhi", MFHL_FMT, AT);
c0ebe874 13089 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132 13090 if (mips_trap)
df58fc94 13091 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
252b5132
RH
13092 else
13093 {
df58fc94
RS
13094 if (mips_opts.micromips)
13095 micromips_label_expr (&label_expr);
13096 else
13097 label_expr.X_add_number = 8;
13098 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
a605d2b3 13099 macro_build (NULL, "nop", "");
df58fc94
RS
13100 macro_build (NULL, "break", BRK_FMT, 6);
13101 if (mips_opts.micromips)
13102 micromips_add_label ();
252b5132 13103 }
7d10b47d 13104 end_noreorder ();
252b5132
RH
13105 break;
13106
771c7ce4 13107 case M_DROL:
fef14a42 13108 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 13109 {
c0ebe874 13110 if (op[0] == op[1])
82dd0097
CD
13111 {
13112 tempreg = AT;
13113 used_at = 1;
13114 }
13115 else
c0ebe874
RS
13116 tempreg = op[0];
13117 macro_build (NULL, "dnegu", "d,w", tempreg, op[2]);
13118 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 13119 break;
82dd0097 13120 }
8fc2e39e 13121 used_at = 1;
c0ebe874
RS
13122 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13123 macro_build (NULL, "dsrlv", "d,t,s", AT, op[1], AT);
13124 macro_build (NULL, "dsllv", "d,t,s", op[0], op[1], op[2]);
13125 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13126 break;
13127
252b5132 13128 case M_ROL:
fef14a42 13129 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13130 {
c0ebe874 13131 if (op[0] == op[1])
82dd0097
CD
13132 {
13133 tempreg = AT;
13134 used_at = 1;
13135 }
13136 else
c0ebe874
RS
13137 tempreg = op[0];
13138 macro_build (NULL, "negu", "d,w", tempreg, op[2]);
13139 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 13140 break;
82dd0097 13141 }
8fc2e39e 13142 used_at = 1;
c0ebe874
RS
13143 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13144 macro_build (NULL, "srlv", "d,t,s", AT, op[1], AT);
13145 macro_build (NULL, "sllv", "d,t,s", op[0], op[1], op[2]);
13146 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13147 break;
13148
771c7ce4
TS
13149 case M_DROL_I:
13150 {
13151 unsigned int rot;
e0471c16
TS
13152 const char *l;
13153 const char *rr;
771c7ce4 13154
771c7ce4 13155 rot = imm_expr.X_add_number & 0x3f;
fef14a42 13156 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
13157 {
13158 rot = (64 - rot) & 0x3f;
13159 if (rot >= 32)
c0ebe874 13160 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
60b63b72 13161 else
c0ebe874 13162 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13163 break;
60b63b72 13164 }
483fc7cd 13165 if (rot == 0)
483fc7cd 13166 {
c0ebe874 13167 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13168 break;
483fc7cd 13169 }
82dd0097 13170 l = (rot < 0x20) ? "dsll" : "dsll32";
91d6fa6a 13171 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
82dd0097 13172 rot &= 0x1f;
8fc2e39e 13173 used_at = 1;
c0ebe874
RS
13174 macro_build (NULL, l, SHFT_FMT, AT, op[1], rot);
13175 macro_build (NULL, rr, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13176 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13177 }
13178 break;
13179
252b5132 13180 case M_ROL_I:
771c7ce4
TS
13181 {
13182 unsigned int rot;
13183
771c7ce4 13184 rot = imm_expr.X_add_number & 0x1f;
fef14a42 13185 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 13186 {
c0ebe874
RS
13187 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1],
13188 (32 - rot) & 0x1f);
8fc2e39e 13189 break;
60b63b72 13190 }
483fc7cd 13191 if (rot == 0)
483fc7cd 13192 {
c0ebe874 13193 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13194 break;
483fc7cd 13195 }
8fc2e39e 13196 used_at = 1;
c0ebe874
RS
13197 macro_build (NULL, "sll", SHFT_FMT, AT, op[1], rot);
13198 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13199 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13200 }
13201 break;
13202
13203 case M_DROR:
fef14a42 13204 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 13205 {
c0ebe874 13206 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 13207 break;
82dd0097 13208 }
8fc2e39e 13209 used_at = 1;
c0ebe874
RS
13210 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13211 macro_build (NULL, "dsllv", "d,t,s", AT, op[1], AT);
13212 macro_build (NULL, "dsrlv", "d,t,s", op[0], op[1], op[2]);
13213 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13214 break;
13215
13216 case M_ROR:
fef14a42 13217 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13218 {
c0ebe874 13219 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 13220 break;
82dd0097 13221 }
8fc2e39e 13222 used_at = 1;
c0ebe874
RS
13223 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13224 macro_build (NULL, "sllv", "d,t,s", AT, op[1], AT);
13225 macro_build (NULL, "srlv", "d,t,s", op[0], op[1], op[2]);
13226 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13227 break;
13228
771c7ce4
TS
13229 case M_DROR_I:
13230 {
13231 unsigned int rot;
e0471c16
TS
13232 const char *l;
13233 const char *rr;
771c7ce4 13234
771c7ce4 13235 rot = imm_expr.X_add_number & 0x3f;
fef14a42 13236 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
13237 {
13238 if (rot >= 32)
c0ebe874 13239 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
82dd0097 13240 else
c0ebe874 13241 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13242 break;
82dd0097 13243 }
483fc7cd 13244 if (rot == 0)
483fc7cd 13245 {
c0ebe874 13246 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13247 break;
483fc7cd 13248 }
91d6fa6a 13249 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
82dd0097
CD
13250 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
13251 rot &= 0x1f;
8fc2e39e 13252 used_at = 1;
c0ebe874
RS
13253 macro_build (NULL, rr, SHFT_FMT, AT, op[1], rot);
13254 macro_build (NULL, l, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13255 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13256 }
13257 break;
13258
252b5132 13259 case M_ROR_I:
771c7ce4
TS
13260 {
13261 unsigned int rot;
13262
771c7ce4 13263 rot = imm_expr.X_add_number & 0x1f;
fef14a42 13264 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13265 {
c0ebe874 13266 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13267 break;
82dd0097 13268 }
483fc7cd 13269 if (rot == 0)
483fc7cd 13270 {
c0ebe874 13271 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13272 break;
483fc7cd 13273 }
8fc2e39e 13274 used_at = 1;
c0ebe874
RS
13275 macro_build (NULL, "srl", SHFT_FMT, AT, op[1], rot);
13276 macro_build (NULL, "sll", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13277 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4 13278 }
252b5132
RH
13279 break;
13280
252b5132 13281 case M_SEQ:
c0ebe874
RS
13282 if (op[1] == 0)
13283 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[2], BFD_RELOC_LO16);
13284 else if (op[2] == 0)
13285 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
13286 else
13287 {
c0ebe874
RS
13288 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13289 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
252b5132 13290 }
8fc2e39e 13291 break;
252b5132
RH
13292
13293 case M_SEQ_I:
b0e6f033 13294 if (imm_expr.X_add_number == 0)
252b5132 13295 {
c0ebe874 13296 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 13297 break;
252b5132 13298 }
c0ebe874 13299 if (op[1] == 0)
252b5132 13300 {
1661c76c 13301 as_warn (_("instruction %s: result is always false"),
252b5132 13302 ip->insn_mo->name);
c0ebe874 13303 move_register (op[0], 0);
8fc2e39e 13304 break;
252b5132 13305 }
dd3cbb7e
NC
13306 if (CPU_HAS_SEQ (mips_opts.arch)
13307 && -512 <= imm_expr.X_add_number
13308 && imm_expr.X_add_number < 512)
13309 {
c0ebe874 13310 macro_build (NULL, "seqi", "t,r,+Q", op[0], op[1],
750bdd57 13311 (int) imm_expr.X_add_number);
dd3cbb7e
NC
13312 break;
13313 }
b0e6f033 13314 if (imm_expr.X_add_number >= 0
252b5132 13315 && imm_expr.X_add_number < 0x10000)
c0ebe874 13316 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1], BFD_RELOC_LO16);
b0e6f033 13317 else if (imm_expr.X_add_number > -0x8000
252b5132
RH
13318 && imm_expr.X_add_number < 0)
13319 {
13320 imm_expr.X_add_number = -imm_expr.X_add_number;
bad1aba3 13321 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
c0ebe874 13322 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 13323 }
dd3cbb7e
NC
13324 else if (CPU_HAS_SEQ (mips_opts.arch))
13325 {
13326 used_at = 1;
bad1aba3 13327 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13328 macro_build (NULL, "seq", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
13329 break;
13330 }
252b5132
RH
13331 else
13332 {
bad1aba3 13333 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13334 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
13335 used_at = 1;
13336 }
c0ebe874 13337 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13338 break;
252b5132 13339
c0ebe874 13340 case M_SGE: /* X >= Y <==> not (X < Y) */
252b5132
RH
13341 s = "slt";
13342 goto sge;
13343 case M_SGEU:
13344 s = "sltu";
13345 sge:
c0ebe874
RS
13346 macro_build (NULL, s, "d,v,t", op[0], op[1], op[2]);
13347 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13348 break;
252b5132 13349
c0ebe874 13350 case M_SGE_I: /* X >= I <==> not (X < I) */
252b5132 13351 case M_SGEU_I:
b0e6f033 13352 if (imm_expr.X_add_number >= -0x8000
252b5132 13353 && imm_expr.X_add_number < 0x8000)
c0ebe874
RS
13354 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
13355 op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
13356 else
13357 {
bad1aba3 13358 load_register (AT, &imm_expr, GPR_SIZE == 64);
67c0d1eb 13359 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
c0ebe874 13360 op[0], op[1], AT);
252b5132
RH
13361 used_at = 1;
13362 }
c0ebe874 13363 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13364 break;
252b5132 13365
c0ebe874 13366 case M_SGT: /* X > Y <==> Y < X */
252b5132
RH
13367 s = "slt";
13368 goto sgt;
13369 case M_SGTU:
13370 s = "sltu";
13371 sgt:
c0ebe874 13372 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
8fc2e39e 13373 break;
252b5132 13374
c0ebe874 13375 case M_SGT_I: /* X > I <==> I < X */
252b5132
RH
13376 s = "slt";
13377 goto sgti;
13378 case M_SGTU_I:
13379 s = "sltu";
13380 sgti:
8fc2e39e 13381 used_at = 1;
bad1aba3 13382 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13383 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
252b5132
RH
13384 break;
13385
c0ebe874 13386 case M_SLE: /* X <= Y <==> Y >= X <==> not (Y < X) */
252b5132
RH
13387 s = "slt";
13388 goto sle;
13389 case M_SLEU:
13390 s = "sltu";
13391 sle:
c0ebe874
RS
13392 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13393 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13394 break;
252b5132 13395
c0ebe874 13396 case M_SLE_I: /* X <= I <==> I >= X <==> not (I < X) */
252b5132
RH
13397 s = "slt";
13398 goto slei;
13399 case M_SLEU_I:
13400 s = "sltu";
13401 slei:
8fc2e39e 13402 used_at = 1;
bad1aba3 13403 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874
RS
13404 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13405 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
252b5132
RH
13406 break;
13407
13408 case M_SLT_I:
b0e6f033 13409 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
13410 && imm_expr.X_add_number < 0x8000)
13411 {
c0ebe874
RS
13412 macro_build (&imm_expr, "slti", "t,r,j", op[0], op[1],
13413 BFD_RELOC_LO16);
8fc2e39e 13414 break;
252b5132 13415 }
8fc2e39e 13416 used_at = 1;
bad1aba3 13417 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13418 macro_build (NULL, "slt", "d,v,t", op[0], op[1], AT);
252b5132
RH
13419 break;
13420
13421 case M_SLTU_I:
b0e6f033 13422 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
13423 && imm_expr.X_add_number < 0x8000)
13424 {
c0ebe874 13425 macro_build (&imm_expr, "sltiu", "t,r,j", op[0], op[1],
17a2f251 13426 BFD_RELOC_LO16);
8fc2e39e 13427 break;
252b5132 13428 }
8fc2e39e 13429 used_at = 1;
bad1aba3 13430 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13431 macro_build (NULL, "sltu", "d,v,t", op[0], op[1], AT);
252b5132
RH
13432 break;
13433
13434 case M_SNE:
c0ebe874
RS
13435 if (op[1] == 0)
13436 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[2]);
13437 else if (op[2] == 0)
13438 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
252b5132
RH
13439 else
13440 {
c0ebe874
RS
13441 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13442 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
252b5132 13443 }
8fc2e39e 13444 break;
252b5132
RH
13445
13446 case M_SNE_I:
b0e6f033 13447 if (imm_expr.X_add_number == 0)
252b5132 13448 {
c0ebe874 13449 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
8fc2e39e 13450 break;
252b5132 13451 }
c0ebe874 13452 if (op[1] == 0)
252b5132 13453 {
1661c76c 13454 as_warn (_("instruction %s: result is always true"),
252b5132 13455 ip->insn_mo->name);
bad1aba3 13456 macro_build (&expr1, GPR_SIZE == 32 ? "addiu" : "daddiu", "t,r,j",
c0ebe874 13457 op[0], 0, BFD_RELOC_LO16);
8fc2e39e 13458 break;
252b5132 13459 }
dd3cbb7e
NC
13460 if (CPU_HAS_SEQ (mips_opts.arch)
13461 && -512 <= imm_expr.X_add_number
13462 && imm_expr.X_add_number < 512)
13463 {
c0ebe874 13464 macro_build (NULL, "snei", "t,r,+Q", op[0], op[1],
750bdd57 13465 (int) imm_expr.X_add_number);
dd3cbb7e
NC
13466 break;
13467 }
b0e6f033 13468 if (imm_expr.X_add_number >= 0
252b5132
RH
13469 && imm_expr.X_add_number < 0x10000)
13470 {
c0ebe874
RS
13471 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1],
13472 BFD_RELOC_LO16);
252b5132 13473 }
b0e6f033 13474 else if (imm_expr.X_add_number > -0x8000
252b5132
RH
13475 && imm_expr.X_add_number < 0)
13476 {
13477 imm_expr.X_add_number = -imm_expr.X_add_number;
bad1aba3 13478 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
c0ebe874 13479 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 13480 }
dd3cbb7e
NC
13481 else if (CPU_HAS_SEQ (mips_opts.arch))
13482 {
13483 used_at = 1;
bad1aba3 13484 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13485 macro_build (NULL, "sne", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
13486 break;
13487 }
252b5132
RH
13488 else
13489 {
bad1aba3 13490 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13491 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
13492 used_at = 1;
13493 }
c0ebe874 13494 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
8fc2e39e 13495 break;
252b5132 13496
df58fc94
RS
13497 case M_SUB_I:
13498 s = "addi";
13499 s2 = "sub";
13500 goto do_subi;
13501 case M_SUBU_I:
13502 s = "addiu";
13503 s2 = "subu";
13504 goto do_subi;
252b5132
RH
13505 case M_DSUB_I:
13506 dbl = 1;
df58fc94
RS
13507 s = "daddi";
13508 s2 = "dsub";
13509 if (!mips_opts.micromips)
13510 goto do_subi;
b0e6f033 13511 if (imm_expr.X_add_number > -0x200
df58fc94 13512 && imm_expr.X_add_number <= 0x200)
252b5132 13513 {
b0e6f033
RS
13514 macro_build (NULL, s, "t,r,.", op[0], op[1],
13515 (int) -imm_expr.X_add_number);
8fc2e39e 13516 break;
252b5132 13517 }
df58fc94 13518 goto do_subi_i;
252b5132
RH
13519 case M_DSUBU_I:
13520 dbl = 1;
df58fc94
RS
13521 s = "daddiu";
13522 s2 = "dsubu";
13523 do_subi:
b0e6f033 13524 if (imm_expr.X_add_number > -0x8000
252b5132
RH
13525 && imm_expr.X_add_number <= 0x8000)
13526 {
13527 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 13528 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 13529 break;
252b5132 13530 }
df58fc94 13531 do_subi_i:
8fc2e39e 13532 used_at = 1;
67c0d1eb 13533 load_register (AT, &imm_expr, dbl);
c0ebe874 13534 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
13535 break;
13536
13537 case M_TEQ_I:
13538 s = "teq";
13539 goto trap;
13540 case M_TGE_I:
13541 s = "tge";
13542 goto trap;
13543 case M_TGEU_I:
13544 s = "tgeu";
13545 goto trap;
13546 case M_TLT_I:
13547 s = "tlt";
13548 goto trap;
13549 case M_TLTU_I:
13550 s = "tltu";
13551 goto trap;
13552 case M_TNE_I:
13553 s = "tne";
13554 trap:
8fc2e39e 13555 used_at = 1;
bad1aba3 13556 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13557 macro_build (NULL, s, "s,t", op[0], AT);
252b5132
RH
13558 break;
13559
252b5132 13560 case M_TRUNCWS:
43841e91 13561 case M_TRUNCWD:
df58fc94 13562 gas_assert (!mips_opts.micromips);
0aa27725 13563 gas_assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 13564 used_at = 1;
252b5132
RH
13565
13566 /*
13567 * Is the double cfc1 instruction a bug in the mips assembler;
13568 * or is there a reason for it?
13569 */
7d10b47d 13570 start_noreorder ();
c0ebe874
RS
13571 macro_build (NULL, "cfc1", "t,G", op[2], RA);
13572 macro_build (NULL, "cfc1", "t,G", op[2], RA);
67c0d1eb 13573 macro_build (NULL, "nop", "");
252b5132 13574 expr1.X_add_number = 3;
c0ebe874 13575 macro_build (&expr1, "ori", "t,r,i", AT, op[2], BFD_RELOC_LO16);
252b5132 13576 expr1.X_add_number = 2;
67c0d1eb
RS
13577 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
13578 macro_build (NULL, "ctc1", "t,G", AT, RA);
13579 macro_build (NULL, "nop", "");
13580 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
c0ebe874
RS
13581 op[0], op[1]);
13582 macro_build (NULL, "ctc1", "t,G", op[2], RA);
67c0d1eb 13583 macro_build (NULL, "nop", "");
7d10b47d 13584 end_noreorder ();
252b5132
RH
13585 break;
13586
f2ae14a1 13587 case M_ULH_AB:
252b5132 13588 s = "lb";
df58fc94
RS
13589 s2 = "lbu";
13590 off = 1;
13591 goto uld_st;
f2ae14a1 13592 case M_ULHU_AB:
252b5132 13593 s = "lbu";
df58fc94
RS
13594 s2 = "lbu";
13595 off = 1;
13596 goto uld_st;
f2ae14a1 13597 case M_ULW_AB:
df58fc94
RS
13598 s = "lwl";
13599 s2 = "lwr";
7f3c4072 13600 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
13601 off = 3;
13602 goto uld_st;
f2ae14a1 13603 case M_ULD_AB:
252b5132
RH
13604 s = "ldl";
13605 s2 = "ldr";
7f3c4072 13606 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 13607 off = 7;
df58fc94 13608 goto uld_st;
f2ae14a1 13609 case M_USH_AB:
df58fc94
RS
13610 s = "sb";
13611 s2 = "sb";
13612 off = 1;
13613 ust = 1;
13614 goto uld_st;
f2ae14a1 13615 case M_USW_AB:
df58fc94
RS
13616 s = "swl";
13617 s2 = "swr";
7f3c4072 13618 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 13619 off = 3;
df58fc94
RS
13620 ust = 1;
13621 goto uld_st;
f2ae14a1 13622 case M_USD_AB:
df58fc94
RS
13623 s = "sdl";
13624 s2 = "sdr";
7f3c4072 13625 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
13626 off = 7;
13627 ust = 1;
13628
13629 uld_st:
c0ebe874 13630 breg = op[2];
f2ae14a1 13631 large_offset = !small_offset_p (off, align, offbits);
df58fc94
RS
13632 ep = &offset_expr;
13633 expr1.X_add_number = 0;
f2ae14a1 13634 if (large_offset)
df58fc94
RS
13635 {
13636 used_at = 1;
13637 tempreg = AT;
f2ae14a1
RS
13638 if (small_offset_p (0, align, 16))
13639 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg, -1,
13640 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
13641 else
13642 {
13643 load_address (tempreg, ep, &used_at);
13644 if (breg != 0)
13645 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
13646 tempreg, tempreg, breg);
13647 }
13648 offset_reloc[0] = BFD_RELOC_LO16;
13649 offset_reloc[1] = BFD_RELOC_UNUSED;
13650 offset_reloc[2] = BFD_RELOC_UNUSED;
df58fc94 13651 breg = tempreg;
c0ebe874 13652 tempreg = op[0];
df58fc94
RS
13653 ep = &expr1;
13654 }
c0ebe874 13655 else if (!ust && op[0] == breg)
8fc2e39e
TS
13656 {
13657 used_at = 1;
13658 tempreg = AT;
13659 }
252b5132 13660 else
c0ebe874 13661 tempreg = op[0];
af22f5b2 13662
df58fc94
RS
13663 if (off == 1)
13664 goto ulh_sh;
252b5132 13665
90ecf173 13666 if (!target_big_endian)
df58fc94 13667 ep->X_add_number += off;
f2ae14a1 13668 if (offbits == 12)
c8276761 13669 macro_build (NULL, s, "t,~(b)", tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
13670 else
13671 macro_build (ep, s, "t,o(b)", tempreg, -1,
13672 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94 13673
90ecf173 13674 if (!target_big_endian)
df58fc94 13675 ep->X_add_number -= off;
252b5132 13676 else
df58fc94 13677 ep->X_add_number += off;
f2ae14a1 13678 if (offbits == 12)
df58fc94 13679 macro_build (NULL, s2, "t,~(b)",
c8276761 13680 tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
13681 else
13682 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13683 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 13684
df58fc94 13685 /* If necessary, move the result in tempreg to the final destination. */
c0ebe874 13686 if (!ust && op[0] != tempreg)
df58fc94
RS
13687 {
13688 /* Protect second load's delay slot. */
13689 load_delay_nop ();
c0ebe874 13690 move_register (op[0], tempreg);
df58fc94 13691 }
8fc2e39e 13692 break;
252b5132 13693
df58fc94 13694 ulh_sh:
d6bc6245 13695 used_at = 1;
df58fc94
RS
13696 if (target_big_endian == ust)
13697 ep->X_add_number += off;
c0ebe874 13698 tempreg = ust || large_offset ? op[0] : AT;
f2ae14a1
RS
13699 macro_build (ep, s, "t,o(b)", tempreg, -1,
13700 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94
RS
13701
13702 /* For halfword transfers we need a temporary register to shuffle
13703 bytes. Unfortunately for M_USH_A we have none available before
13704 the next store as AT holds the base address. We deal with this
13705 case by clobbering TREG and then restoring it as with ULH. */
c0ebe874 13706 tempreg = ust == large_offset ? op[0] : AT;
df58fc94 13707 if (ust)
c0ebe874 13708 macro_build (NULL, "srl", SHFT_FMT, tempreg, op[0], 8);
df58fc94
RS
13709
13710 if (target_big_endian == ust)
13711 ep->X_add_number -= off;
252b5132 13712 else
df58fc94 13713 ep->X_add_number += off;
f2ae14a1
RS
13714 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13715 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 13716
df58fc94 13717 /* For M_USH_A re-retrieve the LSB. */
f2ae14a1 13718 if (ust && large_offset)
df58fc94
RS
13719 {
13720 if (target_big_endian)
13721 ep->X_add_number += off;
13722 else
13723 ep->X_add_number -= off;
f2ae14a1
RS
13724 macro_build (&expr1, "lbu", "t,o(b)", AT, -1,
13725 offset_reloc[0], offset_reloc[1], offset_reloc[2], AT);
df58fc94
RS
13726 }
13727 /* For ULH and M_USH_A OR the LSB in. */
f2ae14a1 13728 if (!ust || large_offset)
df58fc94 13729 {
c0ebe874 13730 tempreg = !large_offset ? AT : op[0];
df58fc94 13731 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
c0ebe874 13732 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
df58fc94 13733 }
252b5132
RH
13734 break;
13735
13736 default:
13737 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 13738 are added dynamically. */
1661c76c 13739 as_bad (_("macro %s not implemented yet"), ip->insn_mo->name);
252b5132
RH
13740 break;
13741 }
741fe287 13742 if (!mips_opts.at && used_at)
1661c76c 13743 as_bad (_("macro used $at after \".set noat\""));
252b5132
RH
13744}
13745
13746/* Implement macros in mips16 mode. */
13747
13748static void
17a2f251 13749mips16_macro (struct mips_cl_insn *ip)
252b5132 13750{
c0ebe874 13751 const struct mips_operand_array *operands;
252b5132 13752 int mask;
c0ebe874 13753 int tmp;
252b5132
RH
13754 expressionS expr1;
13755 int dbl;
13756 const char *s, *s2, *s3;
c0ebe874
RS
13757 unsigned int op[MAX_OPERANDS];
13758 unsigned int i;
252b5132
RH
13759
13760 mask = ip->insn_mo->mask;
13761
c0ebe874
RS
13762 operands = insn_operands (ip);
13763 for (i = 0; i < MAX_OPERANDS; i++)
13764 if (operands->operand[i])
13765 op[i] = insn_extract_operand (ip, operands->operand[i]);
13766 else
13767 op[i] = -1;
252b5132 13768
252b5132
RH
13769 expr1.X_op = O_constant;
13770 expr1.X_op_symbol = NULL;
13771 expr1.X_add_symbol = NULL;
13772 expr1.X_add_number = 1;
13773
13774 dbl = 0;
13775
13776 switch (mask)
13777 {
13778 default:
b37df7c4 13779 abort ();
252b5132
RH
13780
13781 case M_DDIV_3:
13782 dbl = 1;
1a0670f3 13783 /* Fall through. */
252b5132
RH
13784 case M_DIV_3:
13785 s = "mflo";
13786 goto do_div3;
13787 case M_DREM_3:
13788 dbl = 1;
1a0670f3 13789 /* Fall through. */
252b5132
RH
13790 case M_REM_3:
13791 s = "mfhi";
13792 do_div3:
7d10b47d 13793 start_noreorder ();
d8722d76 13794 macro_build (NULL, dbl ? "ddiv" : "div", ".,x,y", op[1], op[2]);
252b5132 13795 expr1.X_add_number = 2;
c0ebe874 13796 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 13797 macro_build (NULL, "break", "6", 7);
bdaaa2e1 13798
252b5132
RH
13799 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
13800 since that causes an overflow. We should do that as well,
13801 but I don't see how to do the comparisons without a temporary
13802 register. */
7d10b47d 13803 end_noreorder ();
c0ebe874 13804 macro_build (NULL, s, "x", op[0]);
252b5132
RH
13805 break;
13806
13807 case M_DIVU_3:
13808 s = "divu";
13809 s2 = "mflo";
13810 goto do_divu3;
13811 case M_REMU_3:
13812 s = "divu";
13813 s2 = "mfhi";
13814 goto do_divu3;
13815 case M_DDIVU_3:
13816 s = "ddivu";
13817 s2 = "mflo";
13818 goto do_divu3;
13819 case M_DREMU_3:
13820 s = "ddivu";
13821 s2 = "mfhi";
13822 do_divu3:
7d10b47d 13823 start_noreorder ();
d8722d76 13824 macro_build (NULL, s, ".,x,y", op[1], op[2]);
252b5132 13825 expr1.X_add_number = 2;
c0ebe874 13826 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 13827 macro_build (NULL, "break", "6", 7);
7d10b47d 13828 end_noreorder ();
c0ebe874 13829 macro_build (NULL, s2, "x", op[0]);
252b5132
RH
13830 break;
13831
13832 case M_DMUL:
13833 dbl = 1;
1a0670f3 13834 /* Fall through. */
252b5132 13835 case M_MUL:
c0ebe874
RS
13836 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", op[1], op[2]);
13837 macro_build (NULL, "mflo", "x", op[0]);
8fc2e39e 13838 break;
252b5132
RH
13839
13840 case M_DSUBU_I:
13841 dbl = 1;
13842 goto do_subu;
13843 case M_SUBU_I:
13844 do_subu:
252b5132 13845 imm_expr.X_add_number = -imm_expr.X_add_number;
d8722d76 13846 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,F", op[0], op[1]);
252b5132
RH
13847 break;
13848
13849 case M_SUBU_I_2:
252b5132 13850 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 13851 macro_build (&imm_expr, "addiu", "x,k", op[0]);
252b5132
RH
13852 break;
13853
13854 case M_DSUBU_I_2:
252b5132 13855 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 13856 macro_build (&imm_expr, "daddiu", "y,j", op[0]);
252b5132
RH
13857 break;
13858
13859 case M_BEQ:
13860 s = "cmp";
13861 s2 = "bteqz";
13862 goto do_branch;
13863 case M_BNE:
13864 s = "cmp";
13865 s2 = "btnez";
13866 goto do_branch;
13867 case M_BLT:
13868 s = "slt";
13869 s2 = "btnez";
13870 goto do_branch;
13871 case M_BLTU:
13872 s = "sltu";
13873 s2 = "btnez";
13874 goto do_branch;
13875 case M_BLE:
13876 s = "slt";
13877 s2 = "bteqz";
13878 goto do_reverse_branch;
13879 case M_BLEU:
13880 s = "sltu";
13881 s2 = "bteqz";
13882 goto do_reverse_branch;
13883 case M_BGE:
13884 s = "slt";
13885 s2 = "bteqz";
13886 goto do_branch;
13887 case M_BGEU:
13888 s = "sltu";
13889 s2 = "bteqz";
13890 goto do_branch;
13891 case M_BGT:
13892 s = "slt";
13893 s2 = "btnez";
13894 goto do_reverse_branch;
13895 case M_BGTU:
13896 s = "sltu";
13897 s2 = "btnez";
13898
13899 do_reverse_branch:
c0ebe874
RS
13900 tmp = op[1];
13901 op[1] = op[0];
13902 op[0] = tmp;
252b5132
RH
13903
13904 do_branch:
c0ebe874 13905 macro_build (NULL, s, "x,y", op[0], op[1]);
67c0d1eb 13906 macro_build (&offset_expr, s2, "p");
252b5132
RH
13907 break;
13908
13909 case M_BEQ_I:
13910 s = "cmpi";
13911 s2 = "bteqz";
13912 s3 = "x,U";
13913 goto do_branch_i;
13914 case M_BNE_I:
13915 s = "cmpi";
13916 s2 = "btnez";
13917 s3 = "x,U";
13918 goto do_branch_i;
13919 case M_BLT_I:
13920 s = "slti";
13921 s2 = "btnez";
13922 s3 = "x,8";
13923 goto do_branch_i;
13924 case M_BLTU_I:
13925 s = "sltiu";
13926 s2 = "btnez";
13927 s3 = "x,8";
13928 goto do_branch_i;
13929 case M_BLE_I:
13930 s = "slti";
13931 s2 = "btnez";
13932 s3 = "x,8";
13933 goto do_addone_branch_i;
13934 case M_BLEU_I:
13935 s = "sltiu";
13936 s2 = "btnez";
13937 s3 = "x,8";
13938 goto do_addone_branch_i;
13939 case M_BGE_I:
13940 s = "slti";
13941 s2 = "bteqz";
13942 s3 = "x,8";
13943 goto do_branch_i;
13944 case M_BGEU_I:
13945 s = "sltiu";
13946 s2 = "bteqz";
13947 s3 = "x,8";
13948 goto do_branch_i;
13949 case M_BGT_I:
13950 s = "slti";
13951 s2 = "bteqz";
13952 s3 = "x,8";
13953 goto do_addone_branch_i;
13954 case M_BGTU_I:
13955 s = "sltiu";
13956 s2 = "bteqz";
13957 s3 = "x,8";
13958
13959 do_addone_branch_i:
252b5132
RH
13960 ++imm_expr.X_add_number;
13961
13962 do_branch_i:
c0ebe874 13963 macro_build (&imm_expr, s, s3, op[0]);
67c0d1eb 13964 macro_build (&offset_expr, s2, "p");
252b5132
RH
13965 break;
13966
13967 case M_ABS:
13968 expr1.X_add_number = 0;
c0ebe874
RS
13969 macro_build (&expr1, "slti", "x,8", op[1]);
13970 if (op[0] != op[1])
13971 macro_build (NULL, "move", "y,X", op[0], mips16_to_32_reg_map[op[1]]);
252b5132 13972 expr1.X_add_number = 2;
67c0d1eb 13973 macro_build (&expr1, "bteqz", "p");
c0ebe874 13974 macro_build (NULL, "neg", "x,w", op[0], op[0]);
0acfaea6 13975 break;
252b5132
RH
13976 }
13977}
13978
14daeee3
RS
13979/* Look up instruction [START, START + LENGTH) in HASH. Record any extra
13980 opcode bits in *OPCODE_EXTRA. */
13981
13982static struct mips_opcode *
13983mips_lookup_insn (struct hash_control *hash, const char *start,
da8bca91 13984 ssize_t length, unsigned int *opcode_extra)
14daeee3
RS
13985{
13986 char *name, *dot, *p;
13987 unsigned int mask, suffix;
da8bca91 13988 ssize_t opend;
14daeee3
RS
13989 struct mips_opcode *insn;
13990
13991 /* Make a copy of the instruction so that we can fiddle with it. */
4ec9d7d5 13992 name = xstrndup (start, length);
14daeee3
RS
13993
13994 /* Look up the instruction as-is. */
13995 insn = (struct mips_opcode *) hash_find (hash, name);
ee5734f0 13996 if (insn)
e1fa0163 13997 goto end;
14daeee3
RS
13998
13999 dot = strchr (name, '.');
14000 if (dot && dot[1])
14001 {
14002 /* Try to interpret the text after the dot as a VU0 channel suffix. */
14003 p = mips_parse_vu0_channels (dot + 1, &mask);
14004 if (*p == 0 && mask != 0)
14005 {
14006 *dot = 0;
14007 insn = (struct mips_opcode *) hash_find (hash, name);
14008 *dot = '.';
14009 if (insn && (insn->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0)
14010 {
14011 *opcode_extra |= mask << mips_vu0_channel_mask.lsb;
e1fa0163 14012 goto end;
14daeee3
RS
14013 }
14014 }
14015 }
14016
14017 if (mips_opts.micromips)
14018 {
14019 /* See if there's an instruction size override suffix,
14020 either `16' or `32', at the end of the mnemonic proper,
14021 that defines the operation, i.e. before the first `.'
14022 character if any. Strip it and retry. */
14023 opend = dot != NULL ? dot - name : length;
14024 if (opend >= 3 && name[opend - 2] == '1' && name[opend - 1] == '6')
14025 suffix = 2;
14026 else if (name[opend - 2] == '3' && name[opend - 1] == '2')
14027 suffix = 4;
14028 else
14029 suffix = 0;
14030 if (suffix)
14031 {
39334a61 14032 memmove (name + opend - 2, name + opend, length - opend + 1);
14daeee3 14033 insn = (struct mips_opcode *) hash_find (hash, name);
ee5734f0 14034 if (insn)
14daeee3
RS
14035 {
14036 forced_insn_length = suffix;
e1fa0163 14037 goto end;
14daeee3
RS
14038 }
14039 }
14040 }
14041
e1fa0163
NC
14042 insn = NULL;
14043 end:
14044 free (name);
14045 return insn;
14daeee3
RS
14046}
14047
77bd4346 14048/* Assemble an instruction into its binary format. If the instruction
e423441d
RS
14049 is a macro, set imm_expr and offset_expr to the values associated
14050 with "I" and "A" operands respectively. Otherwise store the value
14051 of the relocatable field (if any) in offset_expr. In both cases
14052 set offset_reloc to the relocation operators applied to offset_expr. */
252b5132
RH
14053
14054static void
60f20e8b 14055mips_ip (char *str, struct mips_cl_insn *insn)
252b5132 14056{
60f20e8b 14057 const struct mips_opcode *first, *past;
df58fc94 14058 struct hash_control *hash;
a92713e6 14059 char format;
14daeee3 14060 size_t end;
a92713e6 14061 struct mips_operand_token *tokens;
14daeee3 14062 unsigned int opcode_extra;
252b5132 14063
df58fc94
RS
14064 if (mips_opts.micromips)
14065 {
14066 hash = micromips_op_hash;
14067 past = &micromips_opcodes[bfd_micromips_num_opcodes];
14068 }
14069 else
14070 {
14071 hash = op_hash;
14072 past = &mips_opcodes[NUMOPCODES];
14073 }
14074 forced_insn_length = 0;
14daeee3 14075 opcode_extra = 0;
252b5132 14076
df58fc94 14077 /* We first try to match an instruction up to a space or to the end. */
a40bc9dd
RS
14078 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
14079 continue;
bdaaa2e1 14080
60f20e8b
RS
14081 first = mips_lookup_insn (hash, str, end, &opcode_extra);
14082 if (first == NULL)
252b5132 14083 {
1661c76c 14084 set_insn_error (0, _("unrecognized opcode"));
a40bc9dd 14085 return;
252b5132
RH
14086 }
14087
60f20e8b 14088 if (strcmp (first->name, "li.s") == 0)
a92713e6 14089 format = 'f';
60f20e8b 14090 else if (strcmp (first->name, "li.d") == 0)
a92713e6
RS
14091 format = 'd';
14092 else
14093 format = 0;
14094 tokens = mips_parse_arguments (str + end, format);
14095 if (!tokens)
14096 return;
14097
60f20e8b
RS
14098 if (!match_insns (insn, first, past, tokens, opcode_extra, FALSE)
14099 && !match_insns (insn, first, past, tokens, opcode_extra, TRUE))
1661c76c 14100 set_insn_error (0, _("invalid operands"));
df58fc94 14101
e3de51ce 14102 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
14103}
14104
77bd4346
RS
14105/* As for mips_ip, but used when assembling MIPS16 code.
14106 Also set forced_insn_length to the resulting instruction size in
14107 bytes if the user explicitly requested a small or extended instruction. */
252b5132
RH
14108
14109static void
60f20e8b 14110mips16_ip (char *str, struct mips_cl_insn *insn)
252b5132 14111{
1a00e612 14112 char *end, *s, c;
60f20e8b 14113 struct mips_opcode *first;
a92713e6 14114 struct mips_operand_token *tokens;
3fb49709 14115 unsigned int l;
252b5132 14116
25499ac7 14117 for (s = str; *s != '\0' && *s != '.' && *s != ' '; ++s)
252b5132 14118 ;
1a00e612
RS
14119 end = s;
14120 c = *end;
3fb49709
MR
14121
14122 l = 0;
1a00e612 14123 switch (c)
252b5132
RH
14124 {
14125 case '\0':
14126 break;
14127
14128 case ' ':
1a00e612 14129 s++;
252b5132
RH
14130 break;
14131
14132 case '.':
3fb49709
MR
14133 s++;
14134 if (*s == 't')
252b5132 14135 {
3fb49709
MR
14136 l = 2;
14137 s++;
252b5132 14138 }
3fb49709 14139 else if (*s == 'e')
252b5132 14140 {
3fb49709
MR
14141 l = 4;
14142 s++;
252b5132 14143 }
3fb49709
MR
14144 if (*s == '\0')
14145 break;
14146 else if (*s++ == ' ')
14147 break;
1661c76c 14148 set_insn_error (0, _("unrecognized opcode"));
252b5132
RH
14149 return;
14150 }
3fb49709 14151 forced_insn_length = l;
252b5132 14152
1a00e612 14153 *end = 0;
60f20e8b 14154 first = (struct mips_opcode *) hash_find (mips16_op_hash, str);
1a00e612
RS
14155 *end = c;
14156
60f20e8b 14157 if (!first)
252b5132 14158 {
1661c76c 14159 set_insn_error (0, _("unrecognized opcode"));
252b5132
RH
14160 return;
14161 }
14162
a92713e6
RS
14163 tokens = mips_parse_arguments (s, 0);
14164 if (!tokens)
14165 return;
14166
60f20e8b 14167 if (!match_mips16_insns (insn, first, tokens))
1661c76c 14168 set_insn_error (0, _("invalid operands"));
252b5132 14169
e3de51ce 14170 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
14171}
14172
b886a2ab
RS
14173/* Marshal immediate value VAL for an extended MIPS16 instruction.
14174 NBITS is the number of significant bits in VAL. */
14175
14176static unsigned long
14177mips16_immed_extend (offsetT val, unsigned int nbits)
14178{
14179 int extval;
25499ac7
MR
14180
14181 extval = 0;
14182 val &= (1U << nbits) - 1;
14183 if (nbits == 16 || nbits == 9)
b886a2ab
RS
14184 {
14185 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14186 val &= 0x1f;
14187 }
14188 else if (nbits == 15)
14189 {
14190 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14191 val &= 0xf;
14192 }
25499ac7 14193 else if (nbits == 6)
b886a2ab
RS
14194 {
14195 extval = ((val & 0x1f) << 6) | (val & 0x20);
14196 val = 0;
14197 }
14198 return (extval << 16) | val;
14199}
14200
3ccad066
RS
14201/* Like decode_mips16_operand, but require the operand to be defined and
14202 require it to be an integer. */
14203
14204static const struct mips_int_operand *
14205mips16_immed_operand (int type, bfd_boolean extended_p)
14206{
14207 const struct mips_operand *operand;
14208
14209 operand = decode_mips16_operand (type, extended_p);
14210 if (!operand || (operand->type != OP_INT && operand->type != OP_PCREL))
14211 abort ();
14212 return (const struct mips_int_operand *) operand;
14213}
14214
14215/* Return true if SVAL fits OPERAND. RELOC is as for mips16_immed. */
14216
14217static bfd_boolean
14218mips16_immed_in_range_p (const struct mips_int_operand *operand,
14219 bfd_reloc_code_real_type reloc, offsetT sval)
14220{
14221 int min_val, max_val;
14222
14223 min_val = mips_int_operand_min (operand);
14224 max_val = mips_int_operand_max (operand);
14225 if (reloc != BFD_RELOC_UNUSED)
14226 {
14227 if (min_val < 0)
14228 sval = SEXT_16BIT (sval);
14229 else
14230 sval &= 0xffff;
14231 }
14232
14233 return (sval >= min_val
14234 && sval <= max_val
14235 && (sval & ((1 << operand->shift) - 1)) == 0);
14236}
14237
5c04167a
RS
14238/* Install immediate value VAL into MIPS16 instruction *INSN,
14239 extending it if necessary. The instruction in *INSN may
14240 already be extended.
14241
43c0598f
RS
14242 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14243 if none. In the former case, VAL is a 16-bit number with no
14244 defined signedness.
14245
14246 TYPE is the type of the immediate field. USER_INSN_LENGTH
14247 is the length that the user requested, or 0 if none. */
252b5132
RH
14248
14249static void
3b4dbbbf 14250mips16_immed (const char *file, unsigned int line, int type,
43c0598f 14251 bfd_reloc_code_real_type reloc, offsetT val,
5c04167a 14252 unsigned int user_insn_length, unsigned long *insn)
252b5132 14253{
3ccad066
RS
14254 const struct mips_int_operand *operand;
14255 unsigned int uval, length;
252b5132 14256
3ccad066
RS
14257 operand = mips16_immed_operand (type, FALSE);
14258 if (!mips16_immed_in_range_p (operand, reloc, val))
5c04167a
RS
14259 {
14260 /* We need an extended instruction. */
14261 if (user_insn_length == 2)
14262 as_bad_where (file, line, _("invalid unextended operand value"));
14263 else
14264 *insn |= MIPS16_EXTEND;
14265 }
14266 else if (user_insn_length == 4)
14267 {
14268 /* The operand doesn't force an unextended instruction to be extended.
14269 Warn if the user wanted an extended instruction anyway. */
14270 *insn |= MIPS16_EXTEND;
14271 as_warn_where (file, line,
14272 _("extended operand requested but not required"));
14273 }
252b5132 14274
3ccad066
RS
14275 length = mips16_opcode_length (*insn);
14276 if (length == 4)
252b5132 14277 {
3ccad066
RS
14278 operand = mips16_immed_operand (type, TRUE);
14279 if (!mips16_immed_in_range_p (operand, reloc, val))
14280 as_bad_where (file, line,
14281 _("operand value out of range for instruction"));
252b5132 14282 }
3ccad066 14283 uval = ((unsigned int) val >> operand->shift) - operand->bias;
bdd15286 14284 if (length == 2 || operand->root.lsb != 0)
3ccad066 14285 *insn = mips_insert_operand (&operand->root, *insn, uval);
252b5132 14286 else
3ccad066 14287 *insn |= mips16_immed_extend (uval, operand->root.size);
252b5132
RH
14288}
14289\f
d6f16593 14290struct percent_op_match
ad8d3bb3 14291{
5e0116d5
RS
14292 const char *str;
14293 bfd_reloc_code_real_type reloc;
d6f16593
MR
14294};
14295
14296static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 14297{
5e0116d5 14298 {"%lo", BFD_RELOC_LO16},
5e0116d5
RS
14299 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14300 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14301 {"%call16", BFD_RELOC_MIPS_CALL16},
14302 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14303 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14304 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14305 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14306 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14307 {"%got", BFD_RELOC_MIPS_GOT16},
14308 {"%gp_rel", BFD_RELOC_GPREL16},
be3f1006 14309 {"%gprel", BFD_RELOC_GPREL16},
5e0116d5
RS
14310 {"%half", BFD_RELOC_16},
14311 {"%highest", BFD_RELOC_MIPS_HIGHEST},
14312 {"%higher", BFD_RELOC_MIPS_HIGHER},
14313 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
14314 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14315 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14316 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14317 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14318 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14319 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14320 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
7361da2c
AB
14321 {"%hi", BFD_RELOC_HI16_S},
14322 {"%pcrel_hi", BFD_RELOC_HI16_S_PCREL},
14323 {"%pcrel_lo", BFD_RELOC_LO16_PCREL}
ad8d3bb3
TS
14324};
14325
d6f16593
MR
14326static const struct percent_op_match mips16_percent_op[] =
14327{
14328 {"%lo", BFD_RELOC_MIPS16_LO16},
be3f1006 14329 {"%gp_rel", BFD_RELOC_MIPS16_GPREL},
d6f16593 14330 {"%gprel", BFD_RELOC_MIPS16_GPREL},
738e5348
RS
14331 {"%got", BFD_RELOC_MIPS16_GOT16},
14332 {"%call16", BFD_RELOC_MIPS16_CALL16},
d0f13682
CLT
14333 {"%hi", BFD_RELOC_MIPS16_HI16_S},
14334 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14335 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14336 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14337 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14338 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14339 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14340 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
d6f16593
MR
14341};
14342
252b5132 14343
5e0116d5
RS
14344/* Return true if *STR points to a relocation operator. When returning true,
14345 move *STR over the operator and store its relocation code in *RELOC.
14346 Leave both *STR and *RELOC alone when returning false. */
14347
14348static bfd_boolean
17a2f251 14349parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 14350{
d6f16593
MR
14351 const struct percent_op_match *percent_op;
14352 size_t limit, i;
14353
14354 if (mips_opts.mips16)
14355 {
14356 percent_op = mips16_percent_op;
14357 limit = ARRAY_SIZE (mips16_percent_op);
14358 }
14359 else
14360 {
14361 percent_op = mips_percent_op;
14362 limit = ARRAY_SIZE (mips_percent_op);
14363 }
76b3015f 14364
d6f16593 14365 for (i = 0; i < limit; i++)
5e0116d5 14366 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 14367 {
3f98094e
DJ
14368 int len = strlen (percent_op[i].str);
14369
14370 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14371 continue;
14372
5e0116d5
RS
14373 *str += strlen (percent_op[i].str);
14374 *reloc = percent_op[i].reloc;
394f9b3a 14375
5e0116d5
RS
14376 /* Check whether the output BFD supports this relocation.
14377 If not, issue an error and fall back on something safe. */
14378 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 14379 {
20203fb9 14380 as_bad (_("relocation %s isn't supported by the current ABI"),
5e0116d5 14381 percent_op[i].str);
01a3f561 14382 *reloc = BFD_RELOC_UNUSED;
394f9b3a 14383 }
5e0116d5 14384 return TRUE;
394f9b3a 14385 }
5e0116d5 14386 return FALSE;
394f9b3a 14387}
ad8d3bb3 14388
ad8d3bb3 14389
5e0116d5
RS
14390/* Parse string STR as a 16-bit relocatable operand. Store the
14391 expression in *EP and the relocations in the array starting
14392 at RELOC. Return the number of relocation operators used.
ad8d3bb3 14393
01a3f561 14394 On exit, EXPR_END points to the first character after the expression. */
ad8d3bb3 14395
5e0116d5 14396static size_t
17a2f251
TS
14397my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14398 char *str)
ad8d3bb3 14399{
5e0116d5
RS
14400 bfd_reloc_code_real_type reversed_reloc[3];
14401 size_t reloc_index, i;
09b8f35a
RS
14402 int crux_depth, str_depth;
14403 char *crux;
5e0116d5
RS
14404
14405 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
14406 in REVERSED_RELOC. End the loop with CRUX pointing to the start
14407 of the main expression and with CRUX_DEPTH containing the number
14408 of open brackets at that point. */
14409 reloc_index = -1;
14410 str_depth = 0;
14411 do
fb1b3232 14412 {
09b8f35a
RS
14413 reloc_index++;
14414 crux = str;
14415 crux_depth = str_depth;
14416
14417 /* Skip over whitespace and brackets, keeping count of the number
14418 of brackets. */
14419 while (*str == ' ' || *str == '\t' || *str == '(')
14420 if (*str++ == '(')
14421 str_depth++;
5e0116d5 14422 }
09b8f35a
RS
14423 while (*str == '%'
14424 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14425 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 14426
09b8f35a 14427 my_getExpression (ep, crux);
5e0116d5 14428 str = expr_end;
394f9b3a 14429
5e0116d5 14430 /* Match every open bracket. */
09b8f35a 14431 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 14432 if (*str++ == ')')
09b8f35a 14433 crux_depth--;
394f9b3a 14434
09b8f35a 14435 if (crux_depth > 0)
20203fb9 14436 as_bad (_("unclosed '('"));
394f9b3a 14437
5e0116d5 14438 expr_end = str;
252b5132 14439
01a3f561 14440 if (reloc_index != 0)
64bdfcaf
RS
14441 {
14442 prev_reloc_op_frag = frag_now;
14443 for (i = 0; i < reloc_index; i++)
14444 reloc[i] = reversed_reloc[reloc_index - 1 - i];
14445 }
fb1b3232 14446
5e0116d5 14447 return reloc_index;
252b5132
RH
14448}
14449
14450static void
17a2f251 14451my_getExpression (expressionS *ep, char *str)
252b5132
RH
14452{
14453 char *save_in;
14454
14455 save_in = input_line_pointer;
14456 input_line_pointer = str;
14457 expression (ep);
14458 expr_end = input_line_pointer;
14459 input_line_pointer = save_in;
252b5132
RH
14460}
14461
6d4af3c2 14462const char *
17a2f251 14463md_atof (int type, char *litP, int *sizeP)
252b5132 14464{
499ac353 14465 return ieee_md_atof (type, litP, sizeP, target_big_endian);
252b5132
RH
14466}
14467
14468void
17a2f251 14469md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
14470{
14471 if (target_big_endian)
14472 number_to_chars_bigendian (buf, val, n);
14473 else
14474 number_to_chars_littleendian (buf, val, n);
14475}
14476\f
e013f690
TS
14477static int support_64bit_objects(void)
14478{
14479 const char **list, **l;
aa3d8fdf 14480 int yes;
e013f690
TS
14481
14482 list = bfd_target_list ();
14483 for (l = list; *l != NULL; l++)
aeffff67
RS
14484 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14485 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
e013f690 14486 break;
aa3d8fdf 14487 yes = (*l != NULL);
e013f690 14488 free (list);
aa3d8fdf 14489 return yes;
e013f690
TS
14490}
14491
316f5878
RS
14492/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14493 NEW_VALUE. Warn if another value was already specified. Note:
14494 we have to defer parsing the -march and -mtune arguments in order
14495 to handle 'from-abi' correctly, since the ABI might be specified
14496 in a later argument. */
14497
14498static void
17a2f251 14499mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
14500{
14501 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
1661c76c 14502 as_warn (_("a different %s was already specified, is now %s"),
316f5878
RS
14503 string_ptr == &mips_arch_string ? "-march" : "-mtune",
14504 new_value);
14505
14506 *string_ptr = new_value;
14507}
14508
252b5132 14509int
17b9d67d 14510md_parse_option (int c, const char *arg)
252b5132 14511{
c6278170
RS
14512 unsigned int i;
14513
14514 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
14515 if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
14516 {
919731af 14517 file_ase_explicit |= mips_set_ase (&mips_ases[i], &file_mips_opts,
c6278170
RS
14518 c == mips_ases[i].option_on);
14519 return 1;
14520 }
14521
252b5132
RH
14522 switch (c)
14523 {
119d663a
NC
14524 case OPTION_CONSTRUCT_FLOATS:
14525 mips_disable_float_construction = 0;
14526 break;
bdaaa2e1 14527
119d663a
NC
14528 case OPTION_NO_CONSTRUCT_FLOATS:
14529 mips_disable_float_construction = 1;
14530 break;
bdaaa2e1 14531
252b5132
RH
14532 case OPTION_TRAP:
14533 mips_trap = 1;
14534 break;
14535
14536 case OPTION_BREAK:
14537 mips_trap = 0;
14538 break;
14539
14540 case OPTION_EB:
14541 target_big_endian = 1;
14542 break;
14543
14544 case OPTION_EL:
14545 target_big_endian = 0;
14546 break;
14547
14548 case 'O':
4ffff32f
TS
14549 if (arg == NULL)
14550 mips_optimize = 1;
14551 else if (arg[0] == '0')
14552 mips_optimize = 0;
14553 else if (arg[0] == '1')
252b5132
RH
14554 mips_optimize = 1;
14555 else
14556 mips_optimize = 2;
14557 break;
14558
14559 case 'g':
14560 if (arg == NULL)
14561 mips_debug = 2;
14562 else
14563 mips_debug = atoi (arg);
252b5132
RH
14564 break;
14565
14566 case OPTION_MIPS1:
0b35dfee 14567 file_mips_opts.isa = ISA_MIPS1;
252b5132
RH
14568 break;
14569
14570 case OPTION_MIPS2:
0b35dfee 14571 file_mips_opts.isa = ISA_MIPS2;
252b5132
RH
14572 break;
14573
14574 case OPTION_MIPS3:
0b35dfee 14575 file_mips_opts.isa = ISA_MIPS3;
252b5132
RH
14576 break;
14577
14578 case OPTION_MIPS4:
0b35dfee 14579 file_mips_opts.isa = ISA_MIPS4;
e7af610e
NC
14580 break;
14581
84ea6cf2 14582 case OPTION_MIPS5:
0b35dfee 14583 file_mips_opts.isa = ISA_MIPS5;
84ea6cf2
NC
14584 break;
14585
e7af610e 14586 case OPTION_MIPS32:
0b35dfee 14587 file_mips_opts.isa = ISA_MIPS32;
252b5132
RH
14588 break;
14589
af7ee8bf 14590 case OPTION_MIPS32R2:
0b35dfee 14591 file_mips_opts.isa = ISA_MIPS32R2;
af7ee8bf
CD
14592 break;
14593
ae52f483 14594 case OPTION_MIPS32R3:
0ae19f05 14595 file_mips_opts.isa = ISA_MIPS32R3;
ae52f483
AB
14596 break;
14597
14598 case OPTION_MIPS32R5:
0ae19f05 14599 file_mips_opts.isa = ISA_MIPS32R5;
ae52f483
AB
14600 break;
14601
7361da2c
AB
14602 case OPTION_MIPS32R6:
14603 file_mips_opts.isa = ISA_MIPS32R6;
14604 break;
14605
5f74bc13 14606 case OPTION_MIPS64R2:
0b35dfee 14607 file_mips_opts.isa = ISA_MIPS64R2;
5f74bc13
CD
14608 break;
14609
ae52f483 14610 case OPTION_MIPS64R3:
0ae19f05 14611 file_mips_opts.isa = ISA_MIPS64R3;
ae52f483
AB
14612 break;
14613
14614 case OPTION_MIPS64R5:
0ae19f05 14615 file_mips_opts.isa = ISA_MIPS64R5;
ae52f483
AB
14616 break;
14617
7361da2c
AB
14618 case OPTION_MIPS64R6:
14619 file_mips_opts.isa = ISA_MIPS64R6;
14620 break;
14621
84ea6cf2 14622 case OPTION_MIPS64:
0b35dfee 14623 file_mips_opts.isa = ISA_MIPS64;
84ea6cf2
NC
14624 break;
14625
ec68c924 14626 case OPTION_MTUNE:
316f5878
RS
14627 mips_set_option_string (&mips_tune_string, arg);
14628 break;
ec68c924 14629
316f5878
RS
14630 case OPTION_MARCH:
14631 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
14632 break;
14633
14634 case OPTION_M4650:
316f5878
RS
14635 mips_set_option_string (&mips_arch_string, "4650");
14636 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
14637 break;
14638
14639 case OPTION_NO_M4650:
14640 break;
14641
14642 case OPTION_M4010:
316f5878
RS
14643 mips_set_option_string (&mips_arch_string, "4010");
14644 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
14645 break;
14646
14647 case OPTION_NO_M4010:
14648 break;
14649
14650 case OPTION_M4100:
316f5878
RS
14651 mips_set_option_string (&mips_arch_string, "4100");
14652 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
14653 break;
14654
14655 case OPTION_NO_M4100:
14656 break;
14657
252b5132 14658 case OPTION_M3900:
316f5878
RS
14659 mips_set_option_string (&mips_arch_string, "3900");
14660 mips_set_option_string (&mips_tune_string, "3900");
252b5132 14661 break;
bdaaa2e1 14662
252b5132
RH
14663 case OPTION_NO_M3900:
14664 break;
14665
df58fc94 14666 case OPTION_MICROMIPS:
919731af 14667 if (file_mips_opts.mips16 == 1)
df58fc94
RS
14668 {
14669 as_bad (_("-mmicromips cannot be used with -mips16"));
14670 return 0;
14671 }
919731af 14672 file_mips_opts.micromips = 1;
df58fc94
RS
14673 mips_no_prev_insn ();
14674 break;
14675
14676 case OPTION_NO_MICROMIPS:
919731af 14677 file_mips_opts.micromips = 0;
df58fc94
RS
14678 mips_no_prev_insn ();
14679 break;
14680
252b5132 14681 case OPTION_MIPS16:
919731af 14682 if (file_mips_opts.micromips == 1)
df58fc94
RS
14683 {
14684 as_bad (_("-mips16 cannot be used with -micromips"));
14685 return 0;
14686 }
919731af 14687 file_mips_opts.mips16 = 1;
7d10b47d 14688 mips_no_prev_insn ();
252b5132
RH
14689 break;
14690
14691 case OPTION_NO_MIPS16:
919731af 14692 file_mips_opts.mips16 = 0;
7d10b47d 14693 mips_no_prev_insn ();
252b5132
RH
14694 break;
14695
6a32d874
CM
14696 case OPTION_FIX_24K:
14697 mips_fix_24k = 1;
14698 break;
14699
14700 case OPTION_NO_FIX_24K:
14701 mips_fix_24k = 0;
14702 break;
14703
a8d14a88
CM
14704 case OPTION_FIX_RM7000:
14705 mips_fix_rm7000 = 1;
14706 break;
14707
14708 case OPTION_NO_FIX_RM7000:
14709 mips_fix_rm7000 = 0;
14710 break;
14711
c67a084a
NC
14712 case OPTION_FIX_LOONGSON2F_JUMP:
14713 mips_fix_loongson2f_jump = TRUE;
14714 break;
14715
14716 case OPTION_NO_FIX_LOONGSON2F_JUMP:
14717 mips_fix_loongson2f_jump = FALSE;
14718 break;
14719
14720 case OPTION_FIX_LOONGSON2F_NOP:
14721 mips_fix_loongson2f_nop = TRUE;
14722 break;
14723
14724 case OPTION_NO_FIX_LOONGSON2F_NOP:
14725 mips_fix_loongson2f_nop = FALSE;
14726 break;
14727
d766e8ec
RS
14728 case OPTION_FIX_VR4120:
14729 mips_fix_vr4120 = 1;
60b63b72
RS
14730 break;
14731
d766e8ec
RS
14732 case OPTION_NO_FIX_VR4120:
14733 mips_fix_vr4120 = 0;
60b63b72
RS
14734 break;
14735
7d8e00cf
RS
14736 case OPTION_FIX_VR4130:
14737 mips_fix_vr4130 = 1;
14738 break;
14739
14740 case OPTION_NO_FIX_VR4130:
14741 mips_fix_vr4130 = 0;
14742 break;
14743
d954098f
DD
14744 case OPTION_FIX_CN63XXP1:
14745 mips_fix_cn63xxp1 = TRUE;
14746 break;
14747
14748 case OPTION_NO_FIX_CN63XXP1:
14749 mips_fix_cn63xxp1 = FALSE;
14750 break;
14751
4a6a3df4
AO
14752 case OPTION_RELAX_BRANCH:
14753 mips_relax_branch = 1;
14754 break;
14755
14756 case OPTION_NO_RELAX_BRANCH:
14757 mips_relax_branch = 0;
14758 break;
14759
8b10b0b3
MR
14760 case OPTION_IGNORE_BRANCH_ISA:
14761 mips_ignore_branch_isa = TRUE;
14762 break;
14763
14764 case OPTION_NO_IGNORE_BRANCH_ISA:
14765 mips_ignore_branch_isa = FALSE;
14766 break;
14767
833794fc 14768 case OPTION_INSN32:
919731af 14769 file_mips_opts.insn32 = TRUE;
833794fc
MR
14770 break;
14771
14772 case OPTION_NO_INSN32:
919731af 14773 file_mips_opts.insn32 = FALSE;
833794fc
MR
14774 break;
14775
aa6975fb
ILT
14776 case OPTION_MSHARED:
14777 mips_in_shared = TRUE;
14778 break;
14779
14780 case OPTION_MNO_SHARED:
14781 mips_in_shared = FALSE;
14782 break;
14783
aed1a261 14784 case OPTION_MSYM32:
919731af 14785 file_mips_opts.sym32 = TRUE;
aed1a261
RS
14786 break;
14787
14788 case OPTION_MNO_SYM32:
919731af 14789 file_mips_opts.sym32 = FALSE;
aed1a261
RS
14790 break;
14791
252b5132
RH
14792 /* When generating ELF code, we permit -KPIC and -call_shared to
14793 select SVR4_PIC, and -non_shared to select no PIC. This is
14794 intended to be compatible with Irix 5. */
14795 case OPTION_CALL_SHARED:
252b5132 14796 mips_pic = SVR4_PIC;
143d77c5 14797 mips_abicalls = TRUE;
252b5132
RH
14798 break;
14799
861fb55a 14800 case OPTION_CALL_NONPIC:
861fb55a
DJ
14801 mips_pic = NO_PIC;
14802 mips_abicalls = TRUE;
14803 break;
14804
252b5132 14805 case OPTION_NON_SHARED:
252b5132 14806 mips_pic = NO_PIC;
143d77c5 14807 mips_abicalls = FALSE;
252b5132
RH
14808 break;
14809
44075ae2
TS
14810 /* The -xgot option tells the assembler to use 32 bit offsets
14811 when accessing the got in SVR4_PIC mode. It is for Irix
252b5132
RH
14812 compatibility. */
14813 case OPTION_XGOT:
14814 mips_big_got = 1;
14815 break;
14816
14817 case 'G':
6caf9ef4
TS
14818 g_switch_value = atoi (arg);
14819 g_switch_seen = 1;
252b5132
RH
14820 break;
14821
34ba82a8
TS
14822 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
14823 and -mabi=64. */
252b5132 14824 case OPTION_32:
f3ded42a 14825 mips_abi = O32_ABI;
252b5132
RH
14826 break;
14827
e013f690 14828 case OPTION_N32:
316f5878 14829 mips_abi = N32_ABI;
e013f690 14830 break;
252b5132 14831
e013f690 14832 case OPTION_64:
316f5878 14833 mips_abi = N64_ABI;
f43abd2b 14834 if (!support_64bit_objects())
1661c76c 14835 as_fatal (_("no compiled in support for 64 bit object file format"));
252b5132
RH
14836 break;
14837
c97ef257 14838 case OPTION_GP32:
bad1aba3 14839 file_mips_opts.gp = 32;
c97ef257
AH
14840 break;
14841
14842 case OPTION_GP64:
bad1aba3 14843 file_mips_opts.gp = 64;
c97ef257 14844 break;
252b5132 14845
ca4e0257 14846 case OPTION_FP32:
0b35dfee 14847 file_mips_opts.fp = 32;
316f5878
RS
14848 break;
14849
351cdf24
MF
14850 case OPTION_FPXX:
14851 file_mips_opts.fp = 0;
14852 break;
14853
316f5878 14854 case OPTION_FP64:
0b35dfee 14855 file_mips_opts.fp = 64;
ca4e0257
RS
14856 break;
14857
351cdf24
MF
14858 case OPTION_ODD_SPREG:
14859 file_mips_opts.oddspreg = 1;
14860 break;
14861
14862 case OPTION_NO_ODD_SPREG:
14863 file_mips_opts.oddspreg = 0;
14864 break;
14865
037b32b9 14866 case OPTION_SINGLE_FLOAT:
0b35dfee 14867 file_mips_opts.single_float = 1;
037b32b9
AN
14868 break;
14869
14870 case OPTION_DOUBLE_FLOAT:
0b35dfee 14871 file_mips_opts.single_float = 0;
037b32b9
AN
14872 break;
14873
14874 case OPTION_SOFT_FLOAT:
0b35dfee 14875 file_mips_opts.soft_float = 1;
037b32b9
AN
14876 break;
14877
14878 case OPTION_HARD_FLOAT:
0b35dfee 14879 file_mips_opts.soft_float = 0;
037b32b9
AN
14880 break;
14881
252b5132 14882 case OPTION_MABI:
e013f690 14883 if (strcmp (arg, "32") == 0)
316f5878 14884 mips_abi = O32_ABI;
e013f690 14885 else if (strcmp (arg, "o64") == 0)
316f5878 14886 mips_abi = O64_ABI;
e013f690 14887 else if (strcmp (arg, "n32") == 0)
316f5878 14888 mips_abi = N32_ABI;
e013f690
TS
14889 else if (strcmp (arg, "64") == 0)
14890 {
316f5878 14891 mips_abi = N64_ABI;
e013f690 14892 if (! support_64bit_objects())
1661c76c 14893 as_fatal (_("no compiled in support for 64 bit object file "
e013f690
TS
14894 "format"));
14895 }
14896 else if (strcmp (arg, "eabi") == 0)
316f5878 14897 mips_abi = EABI_ABI;
e013f690 14898 else
da0e507f
TS
14899 {
14900 as_fatal (_("invalid abi -mabi=%s"), arg);
14901 return 0;
14902 }
252b5132
RH
14903 break;
14904
6b76fefe 14905 case OPTION_M7000_HILO_FIX:
b34976b6 14906 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
14907 break;
14908
9ee72ff1 14909 case OPTION_MNO_7000_HILO_FIX:
b34976b6 14910 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
14911 break;
14912
ecb4347a 14913 case OPTION_MDEBUG:
b34976b6 14914 mips_flag_mdebug = TRUE;
ecb4347a
DJ
14915 break;
14916
14917 case OPTION_NO_MDEBUG:
b34976b6 14918 mips_flag_mdebug = FALSE;
ecb4347a 14919 break;
dcd410fe
RO
14920
14921 case OPTION_PDR:
14922 mips_flag_pdr = TRUE;
14923 break;
14924
14925 case OPTION_NO_PDR:
14926 mips_flag_pdr = FALSE;
14927 break;
0a44bf69
RS
14928
14929 case OPTION_MVXWORKS_PIC:
14930 mips_pic = VXWORKS_PIC;
14931 break;
ecb4347a 14932
ba92f887
MR
14933 case OPTION_NAN:
14934 if (strcmp (arg, "2008") == 0)
7361da2c 14935 mips_nan2008 = 1;
ba92f887 14936 else if (strcmp (arg, "legacy") == 0)
7361da2c 14937 mips_nan2008 = 0;
ba92f887
MR
14938 else
14939 {
1661c76c 14940 as_fatal (_("invalid NaN setting -mnan=%s"), arg);
ba92f887
MR
14941 return 0;
14942 }
14943 break;
14944
252b5132
RH
14945 default:
14946 return 0;
14947 }
14948
c67a084a
NC
14949 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
14950
252b5132
RH
14951 return 1;
14952}
316f5878 14953\f
919731af 14954/* Set up globals to tune for the ISA or processor described by INFO. */
252b5132 14955
316f5878 14956static void
17a2f251 14957mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
14958{
14959 if (info != 0)
fef14a42 14960 mips_tune = info->cpu;
316f5878 14961}
80cc45a5 14962
34ba82a8 14963
252b5132 14964void
17a2f251 14965mips_after_parse_args (void)
e9670677 14966{
fef14a42
TS
14967 const struct mips_cpu_info *arch_info = 0;
14968 const struct mips_cpu_info *tune_info = 0;
14969
e9670677 14970 /* GP relative stuff not working for PE */
6caf9ef4 14971 if (strncmp (TARGET_OS, "pe", 2) == 0)
e9670677 14972 {
6caf9ef4 14973 if (g_switch_seen && g_switch_value != 0)
1661c76c 14974 as_bad (_("-G not supported in this configuration"));
e9670677
MR
14975 g_switch_value = 0;
14976 }
14977
cac012d6
AO
14978 if (mips_abi == NO_ABI)
14979 mips_abi = MIPS_DEFAULT_ABI;
14980
919731af 14981 /* The following code determines the architecture.
22923709
RS
14982 Similar code was added to GCC 3.3 (see override_options() in
14983 config/mips/mips.c). The GAS and GCC code should be kept in sync
14984 as much as possible. */
e9670677 14985
316f5878 14986 if (mips_arch_string != 0)
fef14a42 14987 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 14988
0b35dfee 14989 if (file_mips_opts.isa != ISA_UNKNOWN)
e9670677 14990 {
0b35dfee 14991 /* Handle -mipsN. At this point, file_mips_opts.isa contains the
fef14a42 14992 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 14993 the -march selection (if any). */
fef14a42 14994 if (arch_info != 0)
e9670677 14995 {
316f5878
RS
14996 /* -march takes precedence over -mipsN, since it is more descriptive.
14997 There's no harm in specifying both as long as the ISA levels
14998 are the same. */
0b35dfee 14999 if (file_mips_opts.isa != arch_info->isa)
1661c76c
RS
15000 as_bad (_("-%s conflicts with the other architecture options,"
15001 " which imply -%s"),
0b35dfee 15002 mips_cpu_info_from_isa (file_mips_opts.isa)->name,
fef14a42 15003 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 15004 }
316f5878 15005 else
0b35dfee 15006 arch_info = mips_cpu_info_from_isa (file_mips_opts.isa);
e9670677
MR
15007 }
15008
fef14a42 15009 if (arch_info == 0)
95bfe26e
MF
15010 {
15011 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
15012 gas_assert (arch_info);
15013 }
e9670677 15014
fef14a42 15015 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
20203fb9 15016 as_bad (_("-march=%s is not compatible with the selected ABI"),
fef14a42
TS
15017 arch_info->name);
15018
919731af 15019 file_mips_opts.arch = arch_info->cpu;
15020 file_mips_opts.isa = arch_info->isa;
15021
15022 /* Set up initial mips_opts state. */
15023 mips_opts = file_mips_opts;
15024
15025 /* The register size inference code is now placed in
15026 file_mips_check_options. */
fef14a42 15027
0b35dfee 15028 /* Optimize for file_mips_opts.arch, unless -mtune selects a different
15029 processor. */
fef14a42
TS
15030 if (mips_tune_string != 0)
15031 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 15032
fef14a42
TS
15033 if (tune_info == 0)
15034 mips_set_tune (arch_info);
15035 else
15036 mips_set_tune (tune_info);
e9670677 15037
ecb4347a 15038 if (mips_flag_mdebug < 0)
e8044f35 15039 mips_flag_mdebug = 0;
e9670677
MR
15040}
15041\f
15042void
17a2f251 15043mips_init_after_args (void)
252b5132
RH
15044{
15045 /* initialize opcodes */
15046 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 15047 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
15048}
15049
15050long
17a2f251 15051md_pcrel_from (fixS *fixP)
252b5132 15052{
a7ebbfdf
TS
15053 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
15054 switch (fixP->fx_r_type)
15055 {
df58fc94
RS
15056 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15057 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15058 /* Return the address of the delay slot. */
15059 return addr + 2;
15060
15061 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15062 case BFD_RELOC_MICROMIPS_JMP:
c9775dde 15063 case BFD_RELOC_MIPS16_16_PCREL_S1:
a7ebbfdf 15064 case BFD_RELOC_16_PCREL_S2:
7361da2c
AB
15065 case BFD_RELOC_MIPS_21_PCREL_S2:
15066 case BFD_RELOC_MIPS_26_PCREL_S2:
a7ebbfdf
TS
15067 case BFD_RELOC_MIPS_JMP:
15068 /* Return the address of the delay slot. */
15069 return addr + 4;
df58fc94 15070
51f6035b
MR
15071 case BFD_RELOC_MIPS_18_PCREL_S3:
15072 /* Return the aligned address of the doubleword containing
15073 the instruction. */
15074 return addr & ~7;
15075
a7ebbfdf
TS
15076 default:
15077 return addr;
15078 }
252b5132
RH
15079}
15080
252b5132
RH
15081/* This is called before the symbol table is processed. In order to
15082 work with gcc when using mips-tfile, we must keep all local labels.
15083 However, in other cases, we want to discard them. If we were
15084 called with -g, but we didn't see any debugging information, it may
15085 mean that gcc is smuggling debugging information through to
15086 mips-tfile, in which case we must generate all local labels. */
15087
15088void
17a2f251 15089mips_frob_file_before_adjust (void)
252b5132
RH
15090{
15091#ifndef NO_ECOFF_DEBUGGING
15092 if (ECOFF_DEBUGGING
15093 && mips_debug != 0
15094 && ! ecoff_debugging_seen)
15095 flag_keep_locals = 1;
15096#endif
15097}
15098
3b91255e 15099/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 15100 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
15101 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
15102 relocation operators.
15103
15104 For our purposes, a %lo() expression matches a %got() or %hi()
15105 expression if:
15106
15107 (a) it refers to the same symbol; and
15108 (b) the offset applied in the %lo() expression is no lower than
15109 the offset applied in the %got() or %hi().
15110
15111 (b) allows us to cope with code like:
15112
15113 lui $4,%hi(foo)
15114 lh $4,%lo(foo+2)($4)
15115
15116 ...which is legal on RELA targets, and has a well-defined behaviour
15117 if the user knows that adding 2 to "foo" will not induce a carry to
15118 the high 16 bits.
15119
15120 When several %lo()s match a particular %got() or %hi(), we use the
15121 following rules to distinguish them:
15122
15123 (1) %lo()s with smaller offsets are a better match than %lo()s with
15124 higher offsets.
15125
15126 (2) %lo()s with no matching %got() or %hi() are better than those
15127 that already have a matching %got() or %hi().
15128
15129 (3) later %lo()s are better than earlier %lo()s.
15130
15131 These rules are applied in order.
15132
15133 (1) means, among other things, that %lo()s with identical offsets are
15134 chosen if they exist.
15135
15136 (2) means that we won't associate several high-part relocations with
15137 the same low-part relocation unless there's no alternative. Having
15138 several high parts for the same low part is a GNU extension; this rule
15139 allows careful users to avoid it.
15140
15141 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
15142 with the last high-part relocation being at the front of the list.
15143 It therefore makes sense to choose the last matching low-part
15144 relocation, all other things being equal. It's also easier
15145 to code that way. */
252b5132
RH
15146
15147void
17a2f251 15148mips_frob_file (void)
252b5132
RH
15149{
15150 struct mips_hi_fixup *l;
35903be0 15151 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
252b5132
RH
15152
15153 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15154 {
15155 segment_info_type *seginfo;
3b91255e
RS
15156 bfd_boolean matched_lo_p;
15157 fixS **hi_pos, **lo_pos, **pos;
252b5132 15158
9c2799c2 15159 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 15160
5919d012 15161 /* If a GOT16 relocation turns out to be against a global symbol,
b886a2ab
RS
15162 there isn't supposed to be a matching LO. Ignore %gots against
15163 constants; we'll report an error for those later. */
738e5348 15164 if (got16_reloc_p (l->fixp->fx_r_type)
b886a2ab 15165 && !(l->fixp->fx_addsy
9e009953 15166 && pic_need_relax (l->fixp->fx_addsy)))
5919d012
RS
15167 continue;
15168
15169 /* Check quickly whether the next fixup happens to be a matching %lo. */
15170 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
15171 continue;
15172
252b5132 15173 seginfo = seg_info (l->seg);
252b5132 15174
3b91255e
RS
15175 /* Set HI_POS to the position of this relocation in the chain.
15176 Set LO_POS to the position of the chosen low-part relocation.
15177 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15178 relocation that matches an immediately-preceding high-part
15179 relocation. */
15180 hi_pos = NULL;
15181 lo_pos = NULL;
15182 matched_lo_p = FALSE;
738e5348 15183 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
35903be0 15184
3b91255e
RS
15185 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15186 {
15187 if (*pos == l->fixp)
15188 hi_pos = pos;
15189
35903be0 15190 if ((*pos)->fx_r_type == looking_for_rtype
30cfc97a 15191 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
3b91255e
RS
15192 && (*pos)->fx_offset >= l->fixp->fx_offset
15193 && (lo_pos == NULL
15194 || (*pos)->fx_offset < (*lo_pos)->fx_offset
15195 || (!matched_lo_p
15196 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15197 lo_pos = pos;
15198
15199 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15200 && fixup_has_matching_lo_p (*pos));
15201 }
15202
15203 /* If we found a match, remove the high-part relocation from its
15204 current position and insert it before the low-part relocation.
15205 Make the offsets match so that fixup_has_matching_lo_p()
15206 will return true.
15207
15208 We don't warn about unmatched high-part relocations since some
15209 versions of gcc have been known to emit dead "lui ...%hi(...)"
15210 instructions. */
15211 if (lo_pos != NULL)
15212 {
15213 l->fixp->fx_offset = (*lo_pos)->fx_offset;
15214 if (l->fixp->fx_next != *lo_pos)
252b5132 15215 {
3b91255e
RS
15216 *hi_pos = l->fixp->fx_next;
15217 l->fixp->fx_next = *lo_pos;
15218 *lo_pos = l->fixp;
252b5132 15219 }
252b5132
RH
15220 }
15221 }
15222}
15223
252b5132 15224int
17a2f251 15225mips_force_relocation (fixS *fixp)
252b5132 15226{
ae6063d4 15227 if (generic_force_reloc (fixp))
252b5132
RH
15228 return 1;
15229
df58fc94
RS
15230 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15231 so that the linker relaxation can update targets. */
15232 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15233 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15234 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15235 return 1;
15236
5caa2b07
MR
15237 /* We want to keep BFD_RELOC_16_PCREL_S2 BFD_RELOC_MIPS_21_PCREL_S2
15238 and BFD_RELOC_MIPS_26_PCREL_S2 relocations against MIPS16 and
15239 microMIPS symbols so that we can do cross-mode branch diagnostics
15240 and BAL to JALX conversion by the linker. */
15241 if ((fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
9d862524
MR
15242 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15243 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2)
15244 && fixp->fx_addsy
15245 && ELF_ST_IS_COMPRESSED (S_GET_OTHER (fixp->fx_addsy)))
15246 return 1;
15247
7361da2c 15248 /* We want all PC-relative relocations to be kept for R6 relaxation. */
912815f0 15249 if (ISA_IS_R6 (file_mips_opts.isa)
7361da2c
AB
15250 && (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
15251 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15252 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
15253 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
15254 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
15255 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
15256 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL))
15257 return 1;
15258
3e722fb5 15259 return 0;
252b5132
RH
15260}
15261
b416ba9b
MR
15262/* Implement TC_FORCE_RELOCATION_ABS. */
15263
15264bfd_boolean
15265mips_force_relocation_abs (fixS *fixp)
15266{
15267 if (generic_force_reloc (fixp))
15268 return TRUE;
15269
15270 /* These relocations do not have enough bits in the in-place addend
15271 to hold an arbitrary absolute section's offset. */
15272 if (HAVE_IN_PLACE_ADDENDS && limited_pcrel_reloc_p (fixp->fx_r_type))
15273 return TRUE;
15274
15275 return FALSE;
15276}
15277
b886a2ab
RS
15278/* Read the instruction associated with RELOC from BUF. */
15279
15280static unsigned int
15281read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15282{
15283 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15284 return read_compressed_insn (buf, 4);
15285 else
15286 return read_insn (buf);
15287}
15288
15289/* Write instruction INSN to BUF, given that it has been relocated
15290 by RELOC. */
15291
15292static void
15293write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15294 unsigned long insn)
15295{
15296 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15297 write_compressed_insn (buf, insn, 4);
15298 else
15299 write_insn (buf, insn);
15300}
15301
9d862524
MR
15302/* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15303 to a symbol in another ISA mode, which cannot be converted to JALX. */
15304
15305static bfd_boolean
15306fix_bad_cross_mode_jump_p (fixS *fixP)
15307{
15308 unsigned long opcode;
15309 int other;
15310 char *buf;
15311
15312 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15313 return FALSE;
15314
15315 other = S_GET_OTHER (fixP->fx_addsy);
15316 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15317 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15318 switch (fixP->fx_r_type)
15319 {
15320 case BFD_RELOC_MIPS_JMP:
15321 return opcode != 0x1d && opcode != 0x03 && ELF_ST_IS_COMPRESSED (other);
15322 case BFD_RELOC_MICROMIPS_JMP:
15323 return opcode != 0x3c && opcode != 0x3d && !ELF_ST_IS_MICROMIPS (other);
15324 default:
15325 return FALSE;
15326 }
15327}
15328
15329/* Return TRUE if the instruction pointed to by FIXP is an invalid JALX
15330 jump to a symbol in the same ISA mode. */
15331
15332static bfd_boolean
15333fix_bad_same_mode_jalx_p (fixS *fixP)
15334{
15335 unsigned long opcode;
15336 int other;
15337 char *buf;
15338
15339 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15340 return FALSE;
15341
15342 other = S_GET_OTHER (fixP->fx_addsy);
15343 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15344 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15345 switch (fixP->fx_r_type)
15346 {
15347 case BFD_RELOC_MIPS_JMP:
15348 return opcode == 0x1d && !ELF_ST_IS_COMPRESSED (other);
15349 case BFD_RELOC_MIPS16_JMP:
15350 return opcode == 0x07 && ELF_ST_IS_COMPRESSED (other);
15351 case BFD_RELOC_MICROMIPS_JMP:
15352 return opcode == 0x3c && ELF_ST_IS_COMPRESSED (other);
15353 default:
15354 return FALSE;
15355 }
15356}
15357
15358/* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15359 to a symbol whose value plus addend is not aligned according to the
15360 ultimate (after linker relaxation) jump instruction's immediate field
15361 requirement, either to (1 << SHIFT), or, for jumps from microMIPS to
15362 regular MIPS code, to (1 << 2). */
15363
15364static bfd_boolean
15365fix_bad_misaligned_jump_p (fixS *fixP, int shift)
15366{
15367 bfd_boolean micro_to_mips_p;
15368 valueT val;
15369 int other;
15370
15371 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15372 return FALSE;
15373
15374 other = S_GET_OTHER (fixP->fx_addsy);
15375 val = S_GET_VALUE (fixP->fx_addsy) | ELF_ST_IS_COMPRESSED (other);
15376 val += fixP->fx_offset;
15377 micro_to_mips_p = (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15378 && !ELF_ST_IS_MICROMIPS (other));
15379 return ((val & ((1 << (micro_to_mips_p ? 2 : shift)) - 1))
15380 != ELF_ST_IS_COMPRESSED (other));
15381}
15382
15383/* Return TRUE if the instruction pointed to by FIXP is an invalid branch
15384 to a symbol whose annotation indicates another ISA mode. For absolute
a6ebf616
MR
15385 symbols check the ISA bit instead.
15386
15387 We accept BFD_RELOC_16_PCREL_S2 relocations against MIPS16 and microMIPS
15388 symbols or BFD_RELOC_MICROMIPS_16_PCREL_S1 relocations against regular
15389 MIPS symbols and associated with BAL instructions as these instructions
de194d85 15390 may be converted to JALX by the linker. */
9d862524
MR
15391
15392static bfd_boolean
15393fix_bad_cross_mode_branch_p (fixS *fixP)
15394{
15395 bfd_boolean absolute_p;
15396 unsigned long opcode;
15397 asection *symsec;
15398 valueT val;
15399 int other;
15400 char *buf;
15401
8b10b0b3
MR
15402 if (mips_ignore_branch_isa)
15403 return FALSE;
15404
9d862524
MR
15405 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15406 return FALSE;
15407
15408 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15409 absolute_p = bfd_is_abs_section (symsec);
15410
15411 val = S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset;
15412 other = S_GET_OTHER (fixP->fx_addsy);
15413
15414 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15415 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 16;
15416 switch (fixP->fx_r_type)
15417 {
15418 case BFD_RELOC_16_PCREL_S2:
a6ebf616
MR
15419 return ((absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other))
15420 && opcode != 0x0411);
15421 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15422 return ((absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other))
15423 && opcode != 0x4060);
9d862524
MR
15424 case BFD_RELOC_MIPS_21_PCREL_S2:
15425 case BFD_RELOC_MIPS_26_PCREL_S2:
15426 return absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other);
15427 case BFD_RELOC_MIPS16_16_PCREL_S1:
15428 return absolute_p ? !(val & 1) : !ELF_ST_IS_MIPS16 (other);
15429 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15430 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
9d862524
MR
15431 return absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other);
15432 default:
15433 abort ();
15434 }
15435}
15436
15437/* Return TRUE if the symbol plus addend associated with a regular MIPS
15438 branch instruction pointed to by FIXP is not aligned according to the
15439 branch instruction's immediate field requirement. We need the addend
15440 to preserve the ISA bit and also the sum must not have bit 2 set. We
15441 must explicitly OR in the ISA bit from symbol annotation as the bit
15442 won't be set in the symbol's value then. */
15443
15444static bfd_boolean
15445fix_bad_misaligned_branch_p (fixS *fixP)
15446{
15447 bfd_boolean absolute_p;
15448 asection *symsec;
15449 valueT isa_bit;
15450 valueT val;
15451 valueT off;
15452 int other;
15453
15454 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15455 return FALSE;
15456
15457 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15458 absolute_p = bfd_is_abs_section (symsec);
15459
15460 val = S_GET_VALUE (fixP->fx_addsy);
15461 other = S_GET_OTHER (fixP->fx_addsy);
15462 off = fixP->fx_offset;
15463
15464 isa_bit = absolute_p ? (val + off) & 1 : ELF_ST_IS_COMPRESSED (other);
15465 val |= ELF_ST_IS_COMPRESSED (other);
15466 val += off;
15467 return (val & 0x3) != isa_bit;
15468}
15469
15470/* Make the necessary checks on a regular MIPS branch pointed to by FIXP
15471 and its calculated value VAL. */
15472
15473static void
15474fix_validate_branch (fixS *fixP, valueT val)
15475{
15476 if (fixP->fx_done && (val & 0x3) != 0)
15477 as_bad_where (fixP->fx_file, fixP->fx_line,
15478 _("branch to misaligned address (0x%lx)"),
15479 (long) (val + md_pcrel_from (fixP)));
15480 else if (fix_bad_cross_mode_branch_p (fixP))
15481 as_bad_where (fixP->fx_file, fixP->fx_line,
15482 _("branch to a symbol in another ISA mode"));
15483 else if (fix_bad_misaligned_branch_p (fixP))
15484 as_bad_where (fixP->fx_file, fixP->fx_line,
15485 _("branch to misaligned address (0x%lx)"),
15486 (long) (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset));
15487 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x3) != 0)
15488 as_bad_where (fixP->fx_file, fixP->fx_line,
15489 _("cannot encode misaligned addend "
15490 "in the relocatable field (0x%lx)"),
15491 (long) fixP->fx_offset);
15492}
15493
252b5132
RH
15494/* Apply a fixup to the object file. */
15495
94f592af 15496void
55cf6793 15497md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 15498{
4d68580a 15499 char *buf;
b886a2ab 15500 unsigned long insn;
a7ebbfdf 15501 reloc_howto_type *howto;
252b5132 15502
d56a8dda
RS
15503 if (fixP->fx_pcrel)
15504 switch (fixP->fx_r_type)
15505 {
15506 case BFD_RELOC_16_PCREL_S2:
c9775dde 15507 case BFD_RELOC_MIPS16_16_PCREL_S1:
d56a8dda
RS
15508 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15509 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15510 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15511 case BFD_RELOC_32_PCREL:
7361da2c
AB
15512 case BFD_RELOC_MIPS_21_PCREL_S2:
15513 case BFD_RELOC_MIPS_26_PCREL_S2:
15514 case BFD_RELOC_MIPS_18_PCREL_S3:
15515 case BFD_RELOC_MIPS_19_PCREL_S2:
15516 case BFD_RELOC_HI16_S_PCREL:
15517 case BFD_RELOC_LO16_PCREL:
d56a8dda
RS
15518 break;
15519
15520 case BFD_RELOC_32:
15521 fixP->fx_r_type = BFD_RELOC_32_PCREL;
15522 break;
15523
15524 default:
15525 as_bad_where (fixP->fx_file, fixP->fx_line,
15526 _("PC-relative reference to a different section"));
15527 break;
15528 }
15529
15530 /* Handle BFD_RELOC_8, since it's easy. Punt on other bfd relocations
15531 that have no MIPS ELF equivalent. */
15532 if (fixP->fx_r_type != BFD_RELOC_8)
15533 {
15534 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15535 if (!howto)
15536 return;
15537 }
65551fa4 15538
df58fc94
RS
15539 gas_assert (fixP->fx_size == 2
15540 || fixP->fx_size == 4
d56a8dda 15541 || fixP->fx_r_type == BFD_RELOC_8
90ecf173
MR
15542 || fixP->fx_r_type == BFD_RELOC_16
15543 || fixP->fx_r_type == BFD_RELOC_64
15544 || fixP->fx_r_type == BFD_RELOC_CTOR
15545 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
df58fc94 15546 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
90ecf173
MR
15547 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15548 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
2f0c68f2
CM
15549 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64
15550 || fixP->fx_r_type == BFD_RELOC_NONE);
252b5132 15551
4d68580a 15552 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132 15553
b1dca8ee
RS
15554 /* Don't treat parts of a composite relocation as done. There are two
15555 reasons for this:
15556
15557 (1) The second and third parts will be against 0 (RSS_UNDEF) but
15558 should nevertheless be emitted if the first part is.
15559
15560 (2) In normal usage, composite relocations are never assembly-time
15561 constants. The easiest way of dealing with the pathological
15562 exceptions is to generate a relocation against STN_UNDEF and
15563 leave everything up to the linker. */
3994f87e 15564 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
252b5132
RH
15565 fixP->fx_done = 1;
15566
15567 switch (fixP->fx_r_type)
15568 {
3f98094e
DJ
15569 case BFD_RELOC_MIPS_TLS_GD:
15570 case BFD_RELOC_MIPS_TLS_LDM:
741d6ea8
JM
15571 case BFD_RELOC_MIPS_TLS_DTPREL32:
15572 case BFD_RELOC_MIPS_TLS_DTPREL64:
3f98094e
DJ
15573 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15574 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15575 case BFD_RELOC_MIPS_TLS_GOTTPREL:
d0f13682
CLT
15576 case BFD_RELOC_MIPS_TLS_TPREL32:
15577 case BFD_RELOC_MIPS_TLS_TPREL64:
3f98094e
DJ
15578 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15579 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
df58fc94
RS
15580 case BFD_RELOC_MICROMIPS_TLS_GD:
15581 case BFD_RELOC_MICROMIPS_TLS_LDM:
15582 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15583 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15584 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15585 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15586 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
d0f13682
CLT
15587 case BFD_RELOC_MIPS16_TLS_GD:
15588 case BFD_RELOC_MIPS16_TLS_LDM:
15589 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15590 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15591 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15592 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15593 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
4512dafa
MR
15594 if (fixP->fx_addsy)
15595 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15596 else
15597 as_bad_where (fixP->fx_file, fixP->fx_line,
15598 _("TLS relocation against a constant"));
15599 break;
3f98094e 15600
252b5132 15601 case BFD_RELOC_MIPS_JMP:
9d862524
MR
15602 case BFD_RELOC_MIPS16_JMP:
15603 case BFD_RELOC_MICROMIPS_JMP:
15604 {
15605 int shift;
15606
15607 gas_assert (!fixP->fx_done);
15608
15609 /* Shift is 2, unusually, for microMIPS JALX. */
15610 if (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15611 && (read_compressed_insn (buf, 4) >> 26) != 0x3c)
15612 shift = 1;
15613 else
15614 shift = 2;
15615
15616 if (fix_bad_cross_mode_jump_p (fixP))
15617 as_bad_where (fixP->fx_file, fixP->fx_line,
15618 _("jump to a symbol in another ISA mode"));
15619 else if (fix_bad_same_mode_jalx_p (fixP))
15620 as_bad_where (fixP->fx_file, fixP->fx_line,
15621 _("JALX to a symbol in the same ISA mode"));
15622 else if (fix_bad_misaligned_jump_p (fixP, shift))
15623 as_bad_where (fixP->fx_file, fixP->fx_line,
15624 _("jump to misaligned address (0x%lx)"),
15625 (long) (S_GET_VALUE (fixP->fx_addsy)
15626 + fixP->fx_offset));
15627 else if (HAVE_IN_PLACE_ADDENDS
15628 && (fixP->fx_offset & ((1 << shift) - 1)) != 0)
15629 as_bad_where (fixP->fx_file, fixP->fx_line,
15630 _("cannot encode misaligned addend "
15631 "in the relocatable field (0x%lx)"),
15632 (long) fixP->fx_offset);
15633 }
15634 /* Fall through. */
15635
e369bcce
TS
15636 case BFD_RELOC_MIPS_SHIFT5:
15637 case BFD_RELOC_MIPS_SHIFT6:
15638 case BFD_RELOC_MIPS_GOT_DISP:
15639 case BFD_RELOC_MIPS_GOT_PAGE:
15640 case BFD_RELOC_MIPS_GOT_OFST:
15641 case BFD_RELOC_MIPS_SUB:
15642 case BFD_RELOC_MIPS_INSERT_A:
15643 case BFD_RELOC_MIPS_INSERT_B:
15644 case BFD_RELOC_MIPS_DELETE:
15645 case BFD_RELOC_MIPS_HIGHEST:
15646 case BFD_RELOC_MIPS_HIGHER:
15647 case BFD_RELOC_MIPS_SCN_DISP:
15648 case BFD_RELOC_MIPS_REL16:
15649 case BFD_RELOC_MIPS_RELGOT:
15650 case BFD_RELOC_MIPS_JALR:
252b5132
RH
15651 case BFD_RELOC_HI16:
15652 case BFD_RELOC_HI16_S:
b886a2ab 15653 case BFD_RELOC_LO16:
cdf6fd85 15654 case BFD_RELOC_GPREL16:
252b5132
RH
15655 case BFD_RELOC_MIPS_LITERAL:
15656 case BFD_RELOC_MIPS_CALL16:
15657 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 15658 case BFD_RELOC_GPREL32:
252b5132
RH
15659 case BFD_RELOC_MIPS_GOT_HI16:
15660 case BFD_RELOC_MIPS_GOT_LO16:
15661 case BFD_RELOC_MIPS_CALL_HI16:
15662 case BFD_RELOC_MIPS_CALL_LO16:
41947d9e
MR
15663 case BFD_RELOC_HI16_S_PCREL:
15664 case BFD_RELOC_LO16_PCREL:
252b5132 15665 case BFD_RELOC_MIPS16_GPREL:
738e5348
RS
15666 case BFD_RELOC_MIPS16_GOT16:
15667 case BFD_RELOC_MIPS16_CALL16:
d6f16593
MR
15668 case BFD_RELOC_MIPS16_HI16:
15669 case BFD_RELOC_MIPS16_HI16_S:
b886a2ab 15670 case BFD_RELOC_MIPS16_LO16:
df58fc94
RS
15671 case BFD_RELOC_MICROMIPS_GOT_DISP:
15672 case BFD_RELOC_MICROMIPS_GOT_PAGE:
15673 case BFD_RELOC_MICROMIPS_GOT_OFST:
15674 case BFD_RELOC_MICROMIPS_SUB:
15675 case BFD_RELOC_MICROMIPS_HIGHEST:
15676 case BFD_RELOC_MICROMIPS_HIGHER:
15677 case BFD_RELOC_MICROMIPS_SCN_DISP:
15678 case BFD_RELOC_MICROMIPS_JALR:
15679 case BFD_RELOC_MICROMIPS_HI16:
15680 case BFD_RELOC_MICROMIPS_HI16_S:
b886a2ab 15681 case BFD_RELOC_MICROMIPS_LO16:
df58fc94
RS
15682 case BFD_RELOC_MICROMIPS_GPREL16:
15683 case BFD_RELOC_MICROMIPS_LITERAL:
15684 case BFD_RELOC_MICROMIPS_CALL16:
15685 case BFD_RELOC_MICROMIPS_GOT16:
15686 case BFD_RELOC_MICROMIPS_GOT_HI16:
15687 case BFD_RELOC_MICROMIPS_GOT_LO16:
15688 case BFD_RELOC_MICROMIPS_CALL_HI16:
15689 case BFD_RELOC_MICROMIPS_CALL_LO16:
067ec077 15690 case BFD_RELOC_MIPS_EH:
b886a2ab
RS
15691 if (fixP->fx_done)
15692 {
15693 offsetT value;
15694
15695 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
15696 {
15697 insn = read_reloc_insn (buf, fixP->fx_r_type);
15698 if (mips16_reloc_p (fixP->fx_r_type))
15699 insn |= mips16_immed_extend (value, 16);
15700 else
15701 insn |= (value & 0xffff);
15702 write_reloc_insn (buf, fixP->fx_r_type, insn);
15703 }
15704 else
15705 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 15706 _("unsupported constant in relocation"));
b886a2ab 15707 }
252b5132
RH
15708 break;
15709
252b5132
RH
15710 case BFD_RELOC_64:
15711 /* This is handled like BFD_RELOC_32, but we output a sign
15712 extended value if we are only 32 bits. */
3e722fb5 15713 if (fixP->fx_done)
252b5132
RH
15714 {
15715 if (8 <= sizeof (valueT))
4d68580a 15716 md_number_to_chars (buf, *valP, 8);
252b5132
RH
15717 else
15718 {
a7ebbfdf 15719 valueT hiv;
252b5132 15720
a7ebbfdf 15721 if ((*valP & 0x80000000) != 0)
252b5132
RH
15722 hiv = 0xffffffff;
15723 else
15724 hiv = 0;
4d68580a
RS
15725 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
15726 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
252b5132
RH
15727 }
15728 }
15729 break;
15730
056350c6 15731 case BFD_RELOC_RVA:
252b5132 15732 case BFD_RELOC_32:
b47468a6 15733 case BFD_RELOC_32_PCREL:
252b5132 15734 case BFD_RELOC_16:
d56a8dda 15735 case BFD_RELOC_8:
252b5132 15736 /* If we are deleting this reloc entry, we must fill in the
54f4ddb3
TS
15737 value now. This can happen if we have a .word which is not
15738 resolved when it appears but is later defined. */
252b5132 15739 if (fixP->fx_done)
4d68580a 15740 md_number_to_chars (buf, *valP, fixP->fx_size);
252b5132
RH
15741 break;
15742
7361da2c 15743 case BFD_RELOC_MIPS_21_PCREL_S2:
9d862524 15744 fix_validate_branch (fixP, *valP);
41947d9e
MR
15745 if (!fixP->fx_done)
15746 break;
15747
15748 if (*valP + 0x400000 <= 0x7fffff)
15749 {
15750 insn = read_insn (buf);
15751 insn |= (*valP >> 2) & 0x1fffff;
15752 write_insn (buf, insn);
15753 }
15754 else
15755 as_bad_where (fixP->fx_file, fixP->fx_line,
15756 _("branch out of range"));
15757 break;
15758
7361da2c 15759 case BFD_RELOC_MIPS_26_PCREL_S2:
9d862524 15760 fix_validate_branch (fixP, *valP);
41947d9e
MR
15761 if (!fixP->fx_done)
15762 break;
7361da2c 15763
41947d9e
MR
15764 if (*valP + 0x8000000 <= 0xfffffff)
15765 {
15766 insn = read_insn (buf);
15767 insn |= (*valP >> 2) & 0x3ffffff;
15768 write_insn (buf, insn);
15769 }
15770 else
15771 as_bad_where (fixP->fx_file, fixP->fx_line,
15772 _("branch out of range"));
7361da2c
AB
15773 break;
15774
15775 case BFD_RELOC_MIPS_18_PCREL_S3:
717ba204 15776 if (fixP->fx_addsy && (S_GET_VALUE (fixP->fx_addsy) & 0x7) != 0)
7361da2c 15777 as_bad_where (fixP->fx_file, fixP->fx_line,
0866e94c
MF
15778 _("PC-relative access using misaligned symbol (%lx)"),
15779 (long) S_GET_VALUE (fixP->fx_addsy));
15780 if ((fixP->fx_offset & 0x7) != 0)
15781 as_bad_where (fixP->fx_file, fixP->fx_line,
15782 _("PC-relative access using misaligned offset (%lx)"),
15783 (long) fixP->fx_offset);
41947d9e
MR
15784 if (!fixP->fx_done)
15785 break;
7361da2c 15786
41947d9e
MR
15787 if (*valP + 0x100000 <= 0x1fffff)
15788 {
15789 insn = read_insn (buf);
15790 insn |= (*valP >> 3) & 0x3ffff;
15791 write_insn (buf, insn);
15792 }
15793 else
15794 as_bad_where (fixP->fx_file, fixP->fx_line,
15795 _("PC-relative access out of range"));
7361da2c
AB
15796 break;
15797
15798 case BFD_RELOC_MIPS_19_PCREL_S2:
15799 if ((*valP & 0x3) != 0)
15800 as_bad_where (fixP->fx_file, fixP->fx_line,
15801 _("PC-relative access to misaligned address (%lx)"),
717ba204 15802 (long) *valP);
41947d9e
MR
15803 if (!fixP->fx_done)
15804 break;
7361da2c 15805
41947d9e
MR
15806 if (*valP + 0x100000 <= 0x1fffff)
15807 {
15808 insn = read_insn (buf);
15809 insn |= (*valP >> 2) & 0x7ffff;
15810 write_insn (buf, insn);
15811 }
15812 else
15813 as_bad_where (fixP->fx_file, fixP->fx_line,
15814 _("PC-relative access out of range"));
7361da2c
AB
15815 break;
15816
252b5132 15817 case BFD_RELOC_16_PCREL_S2:
9d862524 15818 fix_validate_branch (fixP, *valP);
cb56d3d3 15819
54f4ddb3
TS
15820 /* We need to save the bits in the instruction since fixup_segment()
15821 might be deleting the relocation entry (i.e., a branch within
15822 the current segment). */
a7ebbfdf 15823 if (! fixP->fx_done)
bb2d6cd7 15824 break;
252b5132 15825
54f4ddb3 15826 /* Update old instruction data. */
4d68580a 15827 insn = read_insn (buf);
252b5132 15828
a7ebbfdf
TS
15829 if (*valP + 0x20000 <= 0x3ffff)
15830 {
15831 insn |= (*valP >> 2) & 0xffff;
4d68580a 15832 write_insn (buf, insn);
a7ebbfdf 15833 }
ce8ad872 15834 else if (fixP->fx_tcbit2
a7ebbfdf
TS
15835 && fixP->fx_done
15836 && fixP->fx_frag->fr_address >= text_section->vma
15837 && (fixP->fx_frag->fr_address
587aac4e 15838 < text_section->vma + bfd_get_section_size (text_section))
a7ebbfdf
TS
15839 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
15840 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
15841 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
15842 {
15843 /* The branch offset is too large. If this is an
15844 unconditional branch, and we are not generating PIC code,
15845 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
15846 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
15847 insn = 0x0c000000; /* jal */
252b5132 15848 else
a7ebbfdf
TS
15849 insn = 0x08000000; /* j */
15850 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
15851 fixP->fx_done = 0;
15852 fixP->fx_addsy = section_symbol (text_section);
15853 *valP += md_pcrel_from (fixP);
4d68580a 15854 write_insn (buf, insn);
a7ebbfdf
TS
15855 }
15856 else
15857 {
15858 /* If we got here, we have branch-relaxation disabled,
15859 and there's nothing we can do to fix this instruction
15860 without turning it into a longer sequence. */
15861 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 15862 _("branch out of range"));
252b5132 15863 }
252b5132
RH
15864 break;
15865
c9775dde 15866 case BFD_RELOC_MIPS16_16_PCREL_S1:
df58fc94
RS
15867 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15868 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15869 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
96e9ba5f 15870 gas_assert (!fixP->fx_done);
9d862524
MR
15871 if (fix_bad_cross_mode_branch_p (fixP))
15872 as_bad_where (fixP->fx_file, fixP->fx_line,
15873 _("branch to a symbol in another ISA mode"));
15874 else if (fixP->fx_addsy
15875 && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
15876 && !bfd_is_abs_section (S_GET_SEGMENT (fixP->fx_addsy))
15877 && (fixP->fx_offset & 0x1) != 0)
15878 as_bad_where (fixP->fx_file, fixP->fx_line,
15879 _("branch to misaligned address (0x%lx)"),
15880 (long) (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset));
15881 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x1) != 0)
15882 as_bad_where (fixP->fx_file, fixP->fx_line,
15883 _("cannot encode misaligned addend "
15884 "in the relocatable field (0x%lx)"),
15885 (long) fixP->fx_offset);
df58fc94
RS
15886 break;
15887
252b5132
RH
15888 case BFD_RELOC_VTABLE_INHERIT:
15889 fixP->fx_done = 0;
15890 if (fixP->fx_addsy
15891 && !S_IS_DEFINED (fixP->fx_addsy)
15892 && !S_IS_WEAK (fixP->fx_addsy))
15893 S_SET_WEAK (fixP->fx_addsy);
15894 break;
15895
2f0c68f2 15896 case BFD_RELOC_NONE:
252b5132
RH
15897 case BFD_RELOC_VTABLE_ENTRY:
15898 fixP->fx_done = 0;
15899 break;
15900
15901 default:
b37df7c4 15902 abort ();
252b5132 15903 }
a7ebbfdf
TS
15904
15905 /* Remember value for tc_gen_reloc. */
15906 fixP->fx_addnumber = *valP;
252b5132
RH
15907}
15908
252b5132 15909static symbolS *
17a2f251 15910get_symbol (void)
252b5132
RH
15911{
15912 int c;
15913 char *name;
15914 symbolS *p;
15915
d02603dc 15916 c = get_symbol_name (&name);
252b5132 15917 p = (symbolS *) symbol_find_or_make (name);
d02603dc 15918 (void) restore_line_pointer (c);
252b5132
RH
15919 return p;
15920}
15921
742a56fe
RS
15922/* Align the current frag to a given power of two. If a particular
15923 fill byte should be used, FILL points to an integer that contains
15924 that byte, otherwise FILL is null.
15925
462427c4
RS
15926 This function used to have the comment:
15927
15928 The MIPS assembler also automatically adjusts any preceding label.
15929
15930 The implementation therefore applied the adjustment to a maximum of
15931 one label. However, other label adjustments are applied to batches
15932 of labels, and adjusting just one caused problems when new labels
15933 were added for the sake of debugging or unwind information.
15934 We therefore adjust all preceding labels (given as LABELS) instead. */
252b5132
RH
15935
15936static void
462427c4 15937mips_align (int to, int *fill, struct insn_label_list *labels)
252b5132 15938{
7d10b47d 15939 mips_emit_delays ();
df58fc94 15940 mips_record_compressed_mode ();
742a56fe
RS
15941 if (fill == NULL && subseg_text_p (now_seg))
15942 frag_align_code (to, 0);
15943 else
15944 frag_align (to, fill ? *fill : 0, 0);
252b5132 15945 record_alignment (now_seg, to);
462427c4 15946 mips_move_labels (labels, FALSE);
252b5132
RH
15947}
15948
15949/* Align to a given power of two. .align 0 turns off the automatic
15950 alignment used by the data creating pseudo-ops. */
15951
15952static void
17a2f251 15953s_align (int x ATTRIBUTE_UNUSED)
252b5132 15954{
742a56fe 15955 int temp, fill_value, *fill_ptr;
49954fb4 15956 long max_alignment = 28;
252b5132 15957
54f4ddb3 15958 /* o Note that the assembler pulls down any immediately preceding label
252b5132 15959 to the aligned address.
54f4ddb3 15960 o It's not documented but auto alignment is reinstated by
252b5132 15961 a .align pseudo instruction.
54f4ddb3 15962 o Note also that after auto alignment is turned off the mips assembler
252b5132 15963 issues an error on attempt to assemble an improperly aligned data item.
54f4ddb3 15964 We don't. */
252b5132
RH
15965
15966 temp = get_absolute_expression ();
15967 if (temp > max_alignment)
1661c76c 15968 as_bad (_("alignment too large, %d assumed"), temp = max_alignment);
252b5132
RH
15969 else if (temp < 0)
15970 {
1661c76c 15971 as_warn (_("alignment negative, 0 assumed"));
252b5132
RH
15972 temp = 0;
15973 }
15974 if (*input_line_pointer == ',')
15975 {
f9419b05 15976 ++input_line_pointer;
742a56fe
RS
15977 fill_value = get_absolute_expression ();
15978 fill_ptr = &fill_value;
252b5132
RH
15979 }
15980 else
742a56fe 15981 fill_ptr = 0;
252b5132
RH
15982 if (temp)
15983 {
a8dbcb85
TS
15984 segment_info_type *si = seg_info (now_seg);
15985 struct insn_label_list *l = si->label_list;
54f4ddb3 15986 /* Auto alignment should be switched on by next section change. */
252b5132 15987 auto_align = 1;
462427c4 15988 mips_align (temp, fill_ptr, l);
252b5132
RH
15989 }
15990 else
15991 {
15992 auto_align = 0;
15993 }
15994
15995 demand_empty_rest_of_line ();
15996}
15997
252b5132 15998static void
17a2f251 15999s_change_sec (int sec)
252b5132
RH
16000{
16001 segT seg;
16002
252b5132
RH
16003 /* The ELF backend needs to know that we are changing sections, so
16004 that .previous works correctly. We could do something like check
b6ff326e 16005 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
16006 as it would not be appropriate to use it in the section changing
16007 functions in read.c, since obj-elf.c intercepts those. FIXME:
16008 This should be cleaner, somehow. */
f3ded42a 16009 obj_elf_section_change_hook ();
252b5132 16010
7d10b47d 16011 mips_emit_delays ();
6a32d874 16012
252b5132
RH
16013 switch (sec)
16014 {
16015 case 't':
16016 s_text (0);
16017 break;
16018 case 'd':
16019 s_data (0);
16020 break;
16021 case 'b':
16022 subseg_set (bss_section, (subsegT) get_absolute_expression ());
16023 demand_empty_rest_of_line ();
16024 break;
16025
16026 case 'r':
4d0d148d
TS
16027 seg = subseg_new (RDATA_SECTION_NAME,
16028 (subsegT) get_absolute_expression ());
f3ded42a
RS
16029 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
16030 | SEC_READONLY | SEC_RELOC
16031 | SEC_DATA));
16032 if (strncmp (TARGET_OS, "elf", 3) != 0)
16033 record_alignment (seg, 4);
4d0d148d 16034 demand_empty_rest_of_line ();
252b5132
RH
16035 break;
16036
16037 case 's':
4d0d148d 16038 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
f3ded42a
RS
16039 bfd_set_section_flags (stdoutput, seg,
16040 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
16041 if (strncmp (TARGET_OS, "elf", 3) != 0)
16042 record_alignment (seg, 4);
4d0d148d
TS
16043 demand_empty_rest_of_line ();
16044 break;
998b3c36
MR
16045
16046 case 'B':
16047 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
f3ded42a
RS
16048 bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
16049 if (strncmp (TARGET_OS, "elf", 3) != 0)
16050 record_alignment (seg, 4);
998b3c36
MR
16051 demand_empty_rest_of_line ();
16052 break;
252b5132
RH
16053 }
16054
16055 auto_align = 1;
16056}
b34976b6 16057
cca86cc8 16058void
17a2f251 16059s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 16060{
d02603dc 16061 char *saved_ilp;
cca86cc8 16062 char *section_name;
d02603dc 16063 char c, endc;
684022ea 16064 char next_c = 0;
cca86cc8
SC
16065 int section_type;
16066 int section_flag;
16067 int section_entry_size;
16068 int section_alignment;
b34976b6 16069
d02603dc
NC
16070 saved_ilp = input_line_pointer;
16071 endc = get_symbol_name (&section_name);
16072 c = (endc == '"' ? input_line_pointer[1] : endc);
a816d1ed 16073 if (c)
d02603dc 16074 next_c = input_line_pointer [(endc == '"' ? 2 : 1)];
cca86cc8 16075
4cf0dd0d
TS
16076 /* Do we have .section Name<,"flags">? */
16077 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 16078 {
d02603dc
NC
16079 /* Just after name is now '\0'. */
16080 (void) restore_line_pointer (endc);
16081 input_line_pointer = saved_ilp;
cca86cc8
SC
16082 obj_elf_section (ignore);
16083 return;
16084 }
d02603dc
NC
16085
16086 section_name = xstrdup (section_name);
16087 c = restore_line_pointer (endc);
16088
cca86cc8
SC
16089 input_line_pointer++;
16090
16091 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
16092 if (c == ',')
16093 section_type = get_absolute_expression ();
16094 else
16095 section_type = 0;
d02603dc 16096
cca86cc8
SC
16097 if (*input_line_pointer++ == ',')
16098 section_flag = get_absolute_expression ();
16099 else
16100 section_flag = 0;
d02603dc 16101
cca86cc8
SC
16102 if (*input_line_pointer++ == ',')
16103 section_entry_size = get_absolute_expression ();
16104 else
16105 section_entry_size = 0;
d02603dc 16106
cca86cc8
SC
16107 if (*input_line_pointer++ == ',')
16108 section_alignment = get_absolute_expression ();
16109 else
16110 section_alignment = 0;
d02603dc 16111
87975d2a
AM
16112 /* FIXME: really ignore? */
16113 (void) section_alignment;
cca86cc8 16114
8ab8a5c8
RS
16115 /* When using the generic form of .section (as implemented by obj-elf.c),
16116 there's no way to set the section type to SHT_MIPS_DWARF. Users have
16117 traditionally had to fall back on the more common @progbits instead.
16118
16119 There's nothing really harmful in this, since bfd will correct
16120 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
708587a4 16121 means that, for backwards compatibility, the special_section entries
8ab8a5c8
RS
16122 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
16123
16124 Even so, we shouldn't force users of the MIPS .section syntax to
16125 incorrectly label the sections as SHT_PROGBITS. The best compromise
16126 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
16127 generic type-checking code. */
16128 if (section_type == SHT_MIPS_DWARF)
16129 section_type = SHT_PROGBITS;
16130
a91e1603 16131 obj_elf_change_section (section_name, section_type, 0, section_flag,
cca86cc8 16132 section_entry_size, 0, 0, 0);
a816d1ed
AO
16133
16134 if (now_seg->name != section_name)
16135 free (section_name);
cca86cc8 16136}
252b5132
RH
16137
16138void
17a2f251 16139mips_enable_auto_align (void)
252b5132
RH
16140{
16141 auto_align = 1;
16142}
16143
16144static void
17a2f251 16145s_cons (int log_size)
252b5132 16146{
a8dbcb85
TS
16147 segment_info_type *si = seg_info (now_seg);
16148 struct insn_label_list *l = si->label_list;
252b5132 16149
7d10b47d 16150 mips_emit_delays ();
252b5132 16151 if (log_size > 0 && auto_align)
462427c4 16152 mips_align (log_size, 0, l);
252b5132 16153 cons (1 << log_size);
a1facbec 16154 mips_clear_insn_labels ();
252b5132
RH
16155}
16156
16157static void
17a2f251 16158s_float_cons (int type)
252b5132 16159{
a8dbcb85
TS
16160 segment_info_type *si = seg_info (now_seg);
16161 struct insn_label_list *l = si->label_list;
252b5132 16162
7d10b47d 16163 mips_emit_delays ();
252b5132
RH
16164
16165 if (auto_align)
49309057
ILT
16166 {
16167 if (type == 'd')
462427c4 16168 mips_align (3, 0, l);
49309057 16169 else
462427c4 16170 mips_align (2, 0, l);
49309057 16171 }
252b5132 16172
252b5132 16173 float_cons (type);
a1facbec 16174 mips_clear_insn_labels ();
252b5132
RH
16175}
16176
16177/* Handle .globl. We need to override it because on Irix 5 you are
16178 permitted to say
16179 .globl foo .text
16180 where foo is an undefined symbol, to mean that foo should be
16181 considered to be the address of a function. */
16182
16183static void
17a2f251 16184s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
16185{
16186 char *name;
16187 int c;
16188 symbolS *symbolP;
16189 flagword flag;
16190
8a06b769 16191 do
252b5132 16192 {
d02603dc 16193 c = get_symbol_name (&name);
8a06b769
TS
16194 symbolP = symbol_find_or_make (name);
16195 S_SET_EXTERNAL (symbolP);
16196
252b5132 16197 *input_line_pointer = c;
d02603dc 16198 SKIP_WHITESPACE_AFTER_NAME ();
252b5132 16199
8a06b769
TS
16200 /* On Irix 5, every global symbol that is not explicitly labelled as
16201 being a function is apparently labelled as being an object. */
16202 flag = BSF_OBJECT;
252b5132 16203
8a06b769
TS
16204 if (!is_end_of_line[(unsigned char) *input_line_pointer]
16205 && (*input_line_pointer != ','))
16206 {
16207 char *secname;
16208 asection *sec;
16209
d02603dc 16210 c = get_symbol_name (&secname);
8a06b769
TS
16211 sec = bfd_get_section_by_name (stdoutput, secname);
16212 if (sec == NULL)
16213 as_bad (_("%s: no such section"), secname);
d02603dc 16214 (void) restore_line_pointer (c);
8a06b769
TS
16215
16216 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
16217 flag = BSF_FUNCTION;
16218 }
16219
16220 symbol_get_bfdsym (symbolP)->flags |= flag;
16221
16222 c = *input_line_pointer;
16223 if (c == ',')
16224 {
16225 input_line_pointer++;
16226 SKIP_WHITESPACE ();
16227 if (is_end_of_line[(unsigned char) *input_line_pointer])
16228 c = '\n';
16229 }
16230 }
16231 while (c == ',');
252b5132 16232
252b5132
RH
16233 demand_empty_rest_of_line ();
16234}
16235
16236static void
17a2f251 16237s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
16238{
16239 char *opt;
16240 char c;
16241
d02603dc 16242 c = get_symbol_name (&opt);
252b5132
RH
16243
16244 if (*opt == 'O')
16245 {
16246 /* FIXME: What does this mean? */
16247 }
41a1578e 16248 else if (strncmp (opt, "pic", 3) == 0 && ISDIGIT (opt[3]) && opt[4] == '\0')
252b5132
RH
16249 {
16250 int i;
16251
16252 i = atoi (opt + 3);
668c5ebc
MR
16253 if (i != 0 && i != 2)
16254 as_bad (_(".option pic%d not supported"), i);
16255 else if (mips_pic == VXWORKS_PIC)
16256 as_bad (_(".option pic%d not supported in VxWorks PIC mode"), i);
16257 else if (i == 0)
252b5132
RH
16258 mips_pic = NO_PIC;
16259 else if (i == 2)
143d77c5 16260 {
8b828383 16261 mips_pic = SVR4_PIC;
143d77c5
EC
16262 mips_abicalls = TRUE;
16263 }
252b5132 16264
4d0d148d 16265 if (mips_pic == SVR4_PIC)
252b5132
RH
16266 {
16267 if (g_switch_seen && g_switch_value != 0)
16268 as_warn (_("-G may not be used with SVR4 PIC code"));
16269 g_switch_value = 0;
16270 bfd_set_gp_size (stdoutput, 0);
16271 }
16272 }
16273 else
1661c76c 16274 as_warn (_("unrecognized option \"%s\""), opt);
252b5132 16275
d02603dc 16276 (void) restore_line_pointer (c);
252b5132
RH
16277 demand_empty_rest_of_line ();
16278}
16279
16280/* This structure is used to hold a stack of .set values. */
16281
e972090a
NC
16282struct mips_option_stack
16283{
252b5132
RH
16284 struct mips_option_stack *next;
16285 struct mips_set_options options;
16286};
16287
16288static struct mips_option_stack *mips_opts_stack;
16289
22522f88
MR
16290/* Return status for .set/.module option handling. */
16291
16292enum code_option_type
16293{
16294 /* Unrecognized option. */
16295 OPTION_TYPE_BAD = -1,
16296
16297 /* Ordinary option. */
16298 OPTION_TYPE_NORMAL,
16299
16300 /* ISA changing option. */
16301 OPTION_TYPE_ISA
16302};
16303
16304/* Handle common .set/.module options. Return status indicating option
16305 type. */
16306
16307static enum code_option_type
919731af 16308parse_code_option (char * name)
252b5132 16309{
22522f88 16310 bfd_boolean isa_set = FALSE;
c6278170 16311 const struct mips_ase *ase;
22522f88 16312
919731af 16313 if (strncmp (name, "at=", 3) == 0)
741fe287
MR
16314 {
16315 char *s = name + 3;
16316
16317 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
1661c76c 16318 as_bad (_("unrecognized register name `%s'"), s);
741fe287 16319 }
252b5132 16320 else if (strcmp (name, "at") == 0)
919731af 16321 mips_opts.at = ATREG;
252b5132 16322 else if (strcmp (name, "noat") == 0)
919731af 16323 mips_opts.at = ZERO;
252b5132 16324 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
919731af 16325 mips_opts.nomove = 0;
252b5132 16326 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
919731af 16327 mips_opts.nomove = 1;
252b5132 16328 else if (strcmp (name, "bopt") == 0)
919731af 16329 mips_opts.nobopt = 0;
252b5132 16330 else if (strcmp (name, "nobopt") == 0)
919731af 16331 mips_opts.nobopt = 1;
ad3fea08 16332 else if (strcmp (name, "gp=32") == 0)
bad1aba3 16333 mips_opts.gp = 32;
ad3fea08 16334 else if (strcmp (name, "gp=64") == 0)
919731af 16335 mips_opts.gp = 64;
ad3fea08 16336 else if (strcmp (name, "fp=32") == 0)
0b35dfee 16337 mips_opts.fp = 32;
351cdf24
MF
16338 else if (strcmp (name, "fp=xx") == 0)
16339 mips_opts.fp = 0;
ad3fea08 16340 else if (strcmp (name, "fp=64") == 0)
919731af 16341 mips_opts.fp = 64;
037b32b9
AN
16342 else if (strcmp (name, "softfloat") == 0)
16343 mips_opts.soft_float = 1;
16344 else if (strcmp (name, "hardfloat") == 0)
16345 mips_opts.soft_float = 0;
16346 else if (strcmp (name, "singlefloat") == 0)
16347 mips_opts.single_float = 1;
16348 else if (strcmp (name, "doublefloat") == 0)
16349 mips_opts.single_float = 0;
351cdf24
MF
16350 else if (strcmp (name, "nooddspreg") == 0)
16351 mips_opts.oddspreg = 0;
16352 else if (strcmp (name, "oddspreg") == 0)
16353 mips_opts.oddspreg = 1;
252b5132
RH
16354 else if (strcmp (name, "mips16") == 0
16355 || strcmp (name, "MIPS-16") == 0)
919731af 16356 mips_opts.mips16 = 1;
252b5132
RH
16357 else if (strcmp (name, "nomips16") == 0
16358 || strcmp (name, "noMIPS-16") == 0)
16359 mips_opts.mips16 = 0;
df58fc94 16360 else if (strcmp (name, "micromips") == 0)
919731af 16361 mips_opts.micromips = 1;
df58fc94
RS
16362 else if (strcmp (name, "nomicromips") == 0)
16363 mips_opts.micromips = 0;
c6278170
RS
16364 else if (name[0] == 'n'
16365 && name[1] == 'o'
16366 && (ase = mips_lookup_ase (name + 2)))
919731af 16367 mips_set_ase (ase, &mips_opts, FALSE);
c6278170 16368 else if ((ase = mips_lookup_ase (name)))
919731af 16369 mips_set_ase (ase, &mips_opts, TRUE);
1a2c1fad 16370 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 16371 {
1a2c1fad
CD
16372 /* Permit the user to change the ISA and architecture on the fly.
16373 Needless to say, misuse can cause serious problems. */
919731af 16374 if (strncmp (name, "arch=", 5) == 0)
1a2c1fad
CD
16375 {
16376 const struct mips_cpu_info *p;
16377
919731af 16378 p = mips_parse_cpu ("internal use", name + 5);
1a2c1fad
CD
16379 if (!p)
16380 as_bad (_("unknown architecture %s"), name + 5);
16381 else
16382 {
16383 mips_opts.arch = p->cpu;
16384 mips_opts.isa = p->isa;
22522f88 16385 isa_set = TRUE;
1a2c1fad
CD
16386 }
16387 }
81a21e38
TS
16388 else if (strncmp (name, "mips", 4) == 0)
16389 {
16390 const struct mips_cpu_info *p;
16391
919731af 16392 p = mips_parse_cpu ("internal use", name);
81a21e38
TS
16393 if (!p)
16394 as_bad (_("unknown ISA level %s"), name + 4);
16395 else
16396 {
16397 mips_opts.arch = p->cpu;
16398 mips_opts.isa = p->isa;
22522f88 16399 isa_set = TRUE;
81a21e38
TS
16400 }
16401 }
af7ee8bf 16402 else
81a21e38 16403 as_bad (_("unknown ISA or architecture %s"), name);
252b5132
RH
16404 }
16405 else if (strcmp (name, "autoextend") == 0)
16406 mips_opts.noautoextend = 0;
16407 else if (strcmp (name, "noautoextend") == 0)
16408 mips_opts.noautoextend = 1;
833794fc
MR
16409 else if (strcmp (name, "insn32") == 0)
16410 mips_opts.insn32 = TRUE;
16411 else if (strcmp (name, "noinsn32") == 0)
16412 mips_opts.insn32 = FALSE;
919731af 16413 else if (strcmp (name, "sym32") == 0)
16414 mips_opts.sym32 = TRUE;
16415 else if (strcmp (name, "nosym32") == 0)
16416 mips_opts.sym32 = FALSE;
16417 else
22522f88
MR
16418 return OPTION_TYPE_BAD;
16419
16420 return isa_set ? OPTION_TYPE_ISA : OPTION_TYPE_NORMAL;
919731af 16421}
16422
16423/* Handle the .set pseudo-op. */
16424
16425static void
16426s_mipsset (int x ATTRIBUTE_UNUSED)
16427{
22522f88 16428 enum code_option_type type = OPTION_TYPE_NORMAL;
919731af 16429 char *name = input_line_pointer, ch;
919731af 16430
16431 file_mips_check_options ();
16432
16433 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16434 ++input_line_pointer;
16435 ch = *input_line_pointer;
16436 *input_line_pointer = '\0';
16437
16438 if (strchr (name, ','))
16439 {
16440 /* Generic ".set" directive; use the generic handler. */
16441 *input_line_pointer = ch;
16442 input_line_pointer = name;
16443 s_set (0);
16444 return;
16445 }
16446
16447 if (strcmp (name, "reorder") == 0)
16448 {
16449 if (mips_opts.noreorder)
16450 end_noreorder ();
16451 }
16452 else if (strcmp (name, "noreorder") == 0)
16453 {
16454 if (!mips_opts.noreorder)
16455 start_noreorder ();
16456 }
16457 else if (strcmp (name, "macro") == 0)
16458 mips_opts.warn_about_macros = 0;
16459 else if (strcmp (name, "nomacro") == 0)
16460 {
16461 if (mips_opts.noreorder == 0)
16462 as_bad (_("`noreorder' must be set before `nomacro'"));
16463 mips_opts.warn_about_macros = 1;
16464 }
16465 else if (strcmp (name, "gp=default") == 0)
16466 mips_opts.gp = file_mips_opts.gp;
16467 else if (strcmp (name, "fp=default") == 0)
16468 mips_opts.fp = file_mips_opts.fp;
16469 else if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
16470 {
16471 mips_opts.isa = file_mips_opts.isa;
16472 mips_opts.arch = file_mips_opts.arch;
16473 mips_opts.gp = file_mips_opts.gp;
16474 mips_opts.fp = file_mips_opts.fp;
16475 }
252b5132
RH
16476 else if (strcmp (name, "push") == 0)
16477 {
16478 struct mips_option_stack *s;
16479
325801bd 16480 s = XNEW (struct mips_option_stack);
252b5132
RH
16481 s->next = mips_opts_stack;
16482 s->options = mips_opts;
16483 mips_opts_stack = s;
16484 }
16485 else if (strcmp (name, "pop") == 0)
16486 {
16487 struct mips_option_stack *s;
16488
16489 s = mips_opts_stack;
16490 if (s == NULL)
16491 as_bad (_(".set pop with no .set push"));
16492 else
16493 {
16494 /* If we're changing the reorder mode we need to handle
16495 delay slots correctly. */
16496 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 16497 start_noreorder ();
252b5132 16498 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 16499 end_noreorder ();
252b5132
RH
16500
16501 mips_opts = s->options;
16502 mips_opts_stack = s->next;
16503 free (s);
16504 }
16505 }
22522f88
MR
16506 else
16507 {
16508 type = parse_code_option (name);
16509 if (type == OPTION_TYPE_BAD)
16510 as_warn (_("tried to set unrecognized symbol: %s\n"), name);
16511 }
919731af 16512
16513 /* The use of .set [arch|cpu]= historically 'fixes' the width of gp and fp
16514 registers based on what is supported by the arch/cpu. */
22522f88 16515 if (type == OPTION_TYPE_ISA)
e6559e01 16516 {
919731af 16517 switch (mips_opts.isa)
16518 {
16519 case 0:
16520 break;
16521 case ISA_MIPS1:
351cdf24
MF
16522 /* MIPS I cannot support FPXX. */
16523 mips_opts.fp = 32;
16524 /* fall-through. */
919731af 16525 case ISA_MIPS2:
16526 case ISA_MIPS32:
16527 case ISA_MIPS32R2:
16528 case ISA_MIPS32R3:
16529 case ISA_MIPS32R5:
16530 mips_opts.gp = 32;
351cdf24
MF
16531 if (mips_opts.fp != 0)
16532 mips_opts.fp = 32;
919731af 16533 break;
7361da2c
AB
16534 case ISA_MIPS32R6:
16535 mips_opts.gp = 32;
16536 mips_opts.fp = 64;
16537 break;
919731af 16538 case ISA_MIPS3:
16539 case ISA_MIPS4:
16540 case ISA_MIPS5:
16541 case ISA_MIPS64:
16542 case ISA_MIPS64R2:
16543 case ISA_MIPS64R3:
16544 case ISA_MIPS64R5:
7361da2c 16545 case ISA_MIPS64R6:
919731af 16546 mips_opts.gp = 64;
351cdf24
MF
16547 if (mips_opts.fp != 0)
16548 {
16549 if (mips_opts.arch == CPU_R5900)
16550 mips_opts.fp = 32;
16551 else
16552 mips_opts.fp = 64;
16553 }
919731af 16554 break;
16555 default:
16556 as_bad (_("unknown ISA level %s"), name + 4);
16557 break;
16558 }
e6559e01 16559 }
919731af 16560
16561 mips_check_options (&mips_opts, FALSE);
16562
16563 mips_check_isa_supports_ases ();
16564 *input_line_pointer = ch;
16565 demand_empty_rest_of_line ();
16566}
16567
16568/* Handle the .module pseudo-op. */
16569
16570static void
16571s_module (int ignore ATTRIBUTE_UNUSED)
16572{
16573 char *name = input_line_pointer, ch;
16574
16575 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16576 ++input_line_pointer;
16577 ch = *input_line_pointer;
16578 *input_line_pointer = '\0';
16579
16580 if (!file_mips_opts_checked)
252b5132 16581 {
22522f88 16582 if (parse_code_option (name) == OPTION_TYPE_BAD)
919731af 16583 as_bad (_(".module used with unrecognized symbol: %s\n"), name);
16584
16585 /* Update module level settings from mips_opts. */
16586 file_mips_opts = mips_opts;
252b5132 16587 }
919731af 16588 else
16589 as_bad (_(".module is not permitted after generating code"));
16590
252b5132
RH
16591 *input_line_pointer = ch;
16592 demand_empty_rest_of_line ();
16593}
16594
16595/* Handle the .abicalls pseudo-op. I believe this is equivalent to
16596 .option pic2. It means to generate SVR4 PIC calls. */
16597
16598static void
17a2f251 16599s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16600{
16601 mips_pic = SVR4_PIC;
143d77c5 16602 mips_abicalls = TRUE;
4d0d148d
TS
16603
16604 if (g_switch_seen && g_switch_value != 0)
16605 as_warn (_("-G may not be used with SVR4 PIC code"));
16606 g_switch_value = 0;
16607
252b5132
RH
16608 bfd_set_gp_size (stdoutput, 0);
16609 demand_empty_rest_of_line ();
16610}
16611
16612/* Handle the .cpload pseudo-op. This is used when generating SVR4
16613 PIC code. It sets the $gp register for the function based on the
16614 function address, which is in the register named in the argument.
16615 This uses a relocation against _gp_disp, which is handled specially
16616 by the linker. The result is:
16617 lui $gp,%hi(_gp_disp)
16618 addiu $gp,$gp,%lo(_gp_disp)
16619 addu $gp,$gp,.cpload argument
aa6975fb
ILT
16620 The .cpload argument is normally $25 == $t9.
16621
16622 The -mno-shared option changes this to:
bbe506e8
TS
16623 lui $gp,%hi(__gnu_local_gp)
16624 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
16625 and the argument is ignored. This saves an instruction, but the
16626 resulting code is not position independent; it uses an absolute
bbe506e8
TS
16627 address for __gnu_local_gp. Thus code assembled with -mno-shared
16628 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
16629
16630static void
17a2f251 16631s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16632{
16633 expressionS ex;
aa6975fb
ILT
16634 int reg;
16635 int in_shared;
252b5132 16636
919731af 16637 file_mips_check_options ();
16638
6478892d
TS
16639 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16640 .cpload is ignored. */
16641 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16642 {
16643 s_ignore (0);
16644 return;
16645 }
16646
a276b80c
MR
16647 if (mips_opts.mips16)
16648 {
16649 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16650 ignore_rest_of_line ();
16651 return;
16652 }
16653
d3ecfc59 16654 /* .cpload should be in a .set noreorder section. */
252b5132
RH
16655 if (mips_opts.noreorder == 0)
16656 as_warn (_(".cpload not in noreorder section"));
16657
aa6975fb
ILT
16658 reg = tc_get_register (0);
16659
16660 /* If we need to produce a 64-bit address, we are better off using
16661 the default instruction sequence. */
aed1a261 16662 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 16663
252b5132 16664 ex.X_op = O_symbol;
bbe506e8
TS
16665 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16666 "__gnu_local_gp");
252b5132
RH
16667 ex.X_op_symbol = NULL;
16668 ex.X_add_number = 0;
16669
16670 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 16671 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 16672
8a75745d
MR
16673 mips_mark_labels ();
16674 mips_assembling_insn = TRUE;
16675
584892a6 16676 macro_start ();
67c0d1eb
RS
16677 macro_build_lui (&ex, mips_gp_register);
16678 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 16679 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
16680 if (in_shared)
16681 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16682 mips_gp_register, reg);
584892a6 16683 macro_end ();
252b5132 16684
8a75745d 16685 mips_assembling_insn = FALSE;
252b5132
RH
16686 demand_empty_rest_of_line ();
16687}
16688
6478892d
TS
16689/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
16690 .cpsetup $reg1, offset|$reg2, label
16691
16692 If offset is given, this results in:
16693 sd $gp, offset($sp)
956cd1d6 16694 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
16695 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16696 daddu $gp, $gp, $reg1
6478892d
TS
16697
16698 If $reg2 is given, this results in:
40fc1451 16699 or $reg2, $gp, $0
956cd1d6 16700 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
16701 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16702 daddu $gp, $gp, $reg1
aa6975fb
ILT
16703 $reg1 is normally $25 == $t9.
16704
16705 The -mno-shared option replaces the last three instructions with
16706 lui $gp,%hi(_gp)
54f4ddb3 16707 addiu $gp,$gp,%lo(_gp) */
aa6975fb 16708
6478892d 16709static void
17a2f251 16710s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16711{
16712 expressionS ex_off;
16713 expressionS ex_sym;
16714 int reg1;
6478892d 16715
919731af 16716 file_mips_check_options ();
16717
8586fc66 16718 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
16719 We also need NewABI support. */
16720 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16721 {
16722 s_ignore (0);
16723 return;
16724 }
16725
a276b80c
MR
16726 if (mips_opts.mips16)
16727 {
16728 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
16729 ignore_rest_of_line ();
16730 return;
16731 }
16732
6478892d
TS
16733 reg1 = tc_get_register (0);
16734 SKIP_WHITESPACE ();
16735 if (*input_line_pointer != ',')
16736 {
16737 as_bad (_("missing argument separator ',' for .cpsetup"));
16738 return;
16739 }
16740 else
80245285 16741 ++input_line_pointer;
6478892d
TS
16742 SKIP_WHITESPACE ();
16743 if (*input_line_pointer == '$')
80245285
TS
16744 {
16745 mips_cpreturn_register = tc_get_register (0);
16746 mips_cpreturn_offset = -1;
16747 }
6478892d 16748 else
80245285
TS
16749 {
16750 mips_cpreturn_offset = get_absolute_expression ();
16751 mips_cpreturn_register = -1;
16752 }
6478892d
TS
16753 SKIP_WHITESPACE ();
16754 if (*input_line_pointer != ',')
16755 {
16756 as_bad (_("missing argument separator ',' for .cpsetup"));
16757 return;
16758 }
16759 else
f9419b05 16760 ++input_line_pointer;
6478892d 16761 SKIP_WHITESPACE ();
f21f8242 16762 expression (&ex_sym);
6478892d 16763
8a75745d
MR
16764 mips_mark_labels ();
16765 mips_assembling_insn = TRUE;
16766
584892a6 16767 macro_start ();
6478892d
TS
16768 if (mips_cpreturn_register == -1)
16769 {
16770 ex_off.X_op = O_constant;
16771 ex_off.X_add_symbol = NULL;
16772 ex_off.X_op_symbol = NULL;
16773 ex_off.X_add_number = mips_cpreturn_offset;
16774
67c0d1eb 16775 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 16776 BFD_RELOC_LO16, SP);
6478892d
TS
16777 }
16778 else
40fc1451 16779 move_register (mips_cpreturn_register, mips_gp_register);
6478892d 16780
aed1a261 16781 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb 16782 {
df58fc94 16783 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
aa6975fb
ILT
16784 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
16785 BFD_RELOC_HI16_S);
16786
16787 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
16788 mips_gp_register, -1, BFD_RELOC_GPREL16,
16789 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
16790
16791 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
16792 mips_gp_register, reg1);
16793 }
16794 else
16795 {
16796 expressionS ex;
16797
16798 ex.X_op = O_symbol;
4184909a 16799 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
16800 ex.X_op_symbol = NULL;
16801 ex.X_add_number = 0;
6e1304d8 16802
aa6975fb
ILT
16803 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16804 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16805
16806 macro_build_lui (&ex, mips_gp_register);
16807 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16808 mips_gp_register, BFD_RELOC_LO16);
16809 }
f21f8242 16810
584892a6 16811 macro_end ();
6478892d 16812
8a75745d 16813 mips_assembling_insn = FALSE;
6478892d
TS
16814 demand_empty_rest_of_line ();
16815}
16816
16817static void
17a2f251 16818s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d 16819{
919731af 16820 file_mips_check_options ();
16821
6478892d 16822 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
54f4ddb3 16823 .cplocal is ignored. */
6478892d
TS
16824 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16825 {
16826 s_ignore (0);
16827 return;
16828 }
16829
a276b80c
MR
16830 if (mips_opts.mips16)
16831 {
16832 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
16833 ignore_rest_of_line ();
16834 return;
16835 }
16836
6478892d 16837 mips_gp_register = tc_get_register (0);
85b51719 16838 demand_empty_rest_of_line ();
6478892d
TS
16839}
16840
252b5132
RH
16841/* Handle the .cprestore pseudo-op. This stores $gp into a given
16842 offset from $sp. The offset is remembered, and after making a PIC
16843 call $gp is restored from that location. */
16844
16845static void
17a2f251 16846s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16847{
16848 expressionS ex;
252b5132 16849
919731af 16850 file_mips_check_options ();
16851
6478892d 16852 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 16853 .cprestore is ignored. */
6478892d 16854 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16855 {
16856 s_ignore (0);
16857 return;
16858 }
16859
a276b80c
MR
16860 if (mips_opts.mips16)
16861 {
16862 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
16863 ignore_rest_of_line ();
16864 return;
16865 }
16866
252b5132 16867 mips_cprestore_offset = get_absolute_expression ();
7a621144 16868 mips_cprestore_valid = 1;
252b5132
RH
16869
16870 ex.X_op = O_constant;
16871 ex.X_add_symbol = NULL;
16872 ex.X_op_symbol = NULL;
16873 ex.X_add_number = mips_cprestore_offset;
16874
8a75745d
MR
16875 mips_mark_labels ();
16876 mips_assembling_insn = TRUE;
16877
584892a6 16878 macro_start ();
67c0d1eb
RS
16879 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
16880 SP, HAVE_64BIT_ADDRESSES);
584892a6 16881 macro_end ();
252b5132 16882
8a75745d 16883 mips_assembling_insn = FALSE;
252b5132
RH
16884 demand_empty_rest_of_line ();
16885}
16886
6478892d 16887/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 16888 was given in the preceding .cpsetup, it results in:
6478892d 16889 ld $gp, offset($sp)
76b3015f 16890
6478892d 16891 If a register $reg2 was given there, it results in:
40fc1451 16892 or $gp, $reg2, $0 */
54f4ddb3 16893
6478892d 16894static void
17a2f251 16895s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16896{
16897 expressionS ex;
6478892d 16898
919731af 16899 file_mips_check_options ();
16900
6478892d
TS
16901 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
16902 We also need NewABI support. */
16903 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16904 {
16905 s_ignore (0);
16906 return;
16907 }
16908
a276b80c
MR
16909 if (mips_opts.mips16)
16910 {
16911 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
16912 ignore_rest_of_line ();
16913 return;
16914 }
16915
8a75745d
MR
16916 mips_mark_labels ();
16917 mips_assembling_insn = TRUE;
16918
584892a6 16919 macro_start ();
6478892d
TS
16920 if (mips_cpreturn_register == -1)
16921 {
16922 ex.X_op = O_constant;
16923 ex.X_add_symbol = NULL;
16924 ex.X_op_symbol = NULL;
16925 ex.X_add_number = mips_cpreturn_offset;
16926
67c0d1eb 16927 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
16928 }
16929 else
40fc1451
SD
16930 move_register (mips_gp_register, mips_cpreturn_register);
16931
584892a6 16932 macro_end ();
6478892d 16933
8a75745d 16934 mips_assembling_insn = FALSE;
6478892d
TS
16935 demand_empty_rest_of_line ();
16936}
16937
d0f13682
CLT
16938/* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
16939 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
16940 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
16941 debug information or MIPS16 TLS. */
741d6ea8
JM
16942
16943static void
d0f13682
CLT
16944s_tls_rel_directive (const size_t bytes, const char *dirstr,
16945 bfd_reloc_code_real_type rtype)
741d6ea8
JM
16946{
16947 expressionS ex;
16948 char *p;
16949
16950 expression (&ex);
16951
16952 if (ex.X_op != O_symbol)
16953 {
1661c76c 16954 as_bad (_("unsupported use of %s"), dirstr);
741d6ea8
JM
16955 ignore_rest_of_line ();
16956 }
16957
16958 p = frag_more (bytes);
16959 md_number_to_chars (p, 0, bytes);
d0f13682 16960 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
741d6ea8 16961 demand_empty_rest_of_line ();
de64cffd 16962 mips_clear_insn_labels ();
741d6ea8
JM
16963}
16964
16965/* Handle .dtprelword. */
16966
16967static void
16968s_dtprelword (int ignore ATTRIBUTE_UNUSED)
16969{
d0f13682 16970 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
741d6ea8
JM
16971}
16972
16973/* Handle .dtpreldword. */
16974
16975static void
16976s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
16977{
d0f13682
CLT
16978 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
16979}
16980
16981/* Handle .tprelword. */
16982
16983static void
16984s_tprelword (int ignore ATTRIBUTE_UNUSED)
16985{
16986 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
16987}
16988
16989/* Handle .tpreldword. */
16990
16991static void
16992s_tpreldword (int ignore ATTRIBUTE_UNUSED)
16993{
16994 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
741d6ea8
JM
16995}
16996
6478892d
TS
16997/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
16998 code. It sets the offset to use in gp_rel relocations. */
16999
17000static void
17a2f251 17001s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
17002{
17003 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
17004 We also need NewABI support. */
17005 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17006 {
17007 s_ignore (0);
17008 return;
17009 }
17010
def2e0dd 17011 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
17012
17013 demand_empty_rest_of_line ();
17014}
17015
252b5132
RH
17016/* Handle the .gpword pseudo-op. This is used when generating PIC
17017 code. It generates a 32 bit GP relative reloc. */
17018
17019static void
17a2f251 17020s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132 17021{
a8dbcb85
TS
17022 segment_info_type *si;
17023 struct insn_label_list *l;
252b5132
RH
17024 expressionS ex;
17025 char *p;
17026
17027 /* When not generating PIC code, this is treated as .word. */
17028 if (mips_pic != SVR4_PIC)
17029 {
17030 s_cons (2);
17031 return;
17032 }
17033
a8dbcb85
TS
17034 si = seg_info (now_seg);
17035 l = si->label_list;
7d10b47d 17036 mips_emit_delays ();
252b5132 17037 if (auto_align)
462427c4 17038 mips_align (2, 0, l);
252b5132
RH
17039
17040 expression (&ex);
a1facbec 17041 mips_clear_insn_labels ();
252b5132
RH
17042
17043 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17044 {
1661c76c 17045 as_bad (_("unsupported use of .gpword"));
252b5132
RH
17046 ignore_rest_of_line ();
17047 }
17048
17049 p = frag_more (4);
17a2f251 17050 md_number_to_chars (p, 0, 4);
b34976b6 17051 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 17052 BFD_RELOC_GPREL32);
252b5132
RH
17053
17054 demand_empty_rest_of_line ();
17055}
17056
10181a0d 17057static void
17a2f251 17058s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d 17059{
a8dbcb85
TS
17060 segment_info_type *si;
17061 struct insn_label_list *l;
10181a0d
AO
17062 expressionS ex;
17063 char *p;
17064
17065 /* When not generating PIC code, this is treated as .dword. */
17066 if (mips_pic != SVR4_PIC)
17067 {
17068 s_cons (3);
17069 return;
17070 }
17071
a8dbcb85
TS
17072 si = seg_info (now_seg);
17073 l = si->label_list;
7d10b47d 17074 mips_emit_delays ();
10181a0d 17075 if (auto_align)
462427c4 17076 mips_align (3, 0, l);
10181a0d
AO
17077
17078 expression (&ex);
a1facbec 17079 mips_clear_insn_labels ();
10181a0d
AO
17080
17081 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17082 {
1661c76c 17083 as_bad (_("unsupported use of .gpdword"));
10181a0d
AO
17084 ignore_rest_of_line ();
17085 }
17086
17087 p = frag_more (8);
17a2f251 17088 md_number_to_chars (p, 0, 8);
a105a300 17089 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
6e1304d8 17090 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
17091
17092 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8
RS
17093 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
17094 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
17095
17096 demand_empty_rest_of_line ();
17097}
17098
a3f278e2
CM
17099/* Handle the .ehword pseudo-op. This is used when generating unwinding
17100 tables. It generates a R_MIPS_EH reloc. */
17101
17102static void
17103s_ehword (int ignore ATTRIBUTE_UNUSED)
17104{
17105 expressionS ex;
17106 char *p;
17107
17108 mips_emit_delays ();
17109
17110 expression (&ex);
17111 mips_clear_insn_labels ();
17112
17113 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17114 {
1661c76c 17115 as_bad (_("unsupported use of .ehword"));
a3f278e2
CM
17116 ignore_rest_of_line ();
17117 }
17118
17119 p = frag_more (4);
17120 md_number_to_chars (p, 0, 4);
17121 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
2f0c68f2 17122 BFD_RELOC_32_PCREL);
a3f278e2
CM
17123
17124 demand_empty_rest_of_line ();
17125}
17126
252b5132
RH
17127/* Handle the .cpadd pseudo-op. This is used when dealing with switch
17128 tables in SVR4 PIC code. */
17129
17130static void
17a2f251 17131s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 17132{
252b5132
RH
17133 int reg;
17134
919731af 17135 file_mips_check_options ();
17136
10181a0d
AO
17137 /* This is ignored when not generating SVR4 PIC code. */
17138 if (mips_pic != SVR4_PIC)
252b5132
RH
17139 {
17140 s_ignore (0);
17141 return;
17142 }
17143
8a75745d
MR
17144 mips_mark_labels ();
17145 mips_assembling_insn = TRUE;
17146
252b5132 17147 /* Add $gp to the register named as an argument. */
584892a6 17148 macro_start ();
252b5132 17149 reg = tc_get_register (0);
67c0d1eb 17150 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 17151 macro_end ();
252b5132 17152
8a75745d 17153 mips_assembling_insn = FALSE;
bdaaa2e1 17154 demand_empty_rest_of_line ();
252b5132
RH
17155}
17156
17157/* Handle the .insn pseudo-op. This marks instruction labels in
df58fc94 17158 mips16/micromips mode. This permits the linker to handle them specially,
252b5132
RH
17159 such as generating jalx instructions when needed. We also make
17160 them odd for the duration of the assembly, in order to generate the
17161 right sort of code. We will make them even in the adjust_symtab
17162 routine, while leaving them marked. This is convenient for the
17163 debugger and the disassembler. The linker knows to make them odd
17164 again. */
17165
17166static void
17a2f251 17167s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 17168{
7bb01e2d
MR
17169 file_mips_check_options ();
17170 file_ase_mips16 |= mips_opts.mips16;
17171 file_ase_micromips |= mips_opts.micromips;
17172
df58fc94 17173 mips_mark_labels ();
252b5132
RH
17174
17175 demand_empty_rest_of_line ();
17176}
17177
ba92f887
MR
17178/* Handle the .nan pseudo-op. */
17179
17180static void
17181s_nan (int ignore ATTRIBUTE_UNUSED)
17182{
17183 static const char str_legacy[] = "legacy";
17184 static const char str_2008[] = "2008";
17185 size_t i;
17186
17187 for (i = 0; !is_end_of_line[(unsigned char) input_line_pointer[i]]; i++);
17188
17189 if (i == sizeof (str_2008) - 1
17190 && memcmp (input_line_pointer, str_2008, i) == 0)
7361da2c 17191 mips_nan2008 = 1;
ba92f887
MR
17192 else if (i == sizeof (str_legacy) - 1
17193 && memcmp (input_line_pointer, str_legacy, i) == 0)
7361da2c
AB
17194 {
17195 if (ISA_HAS_LEGACY_NAN (file_mips_opts.isa))
17196 mips_nan2008 = 0;
17197 else
17198 as_bad (_("`%s' does not support legacy NaN"),
17199 mips_cpu_info_from_isa (file_mips_opts.isa)->name);
17200 }
ba92f887 17201 else
1661c76c 17202 as_bad (_("bad .nan directive"));
ba92f887
MR
17203
17204 input_line_pointer += i;
17205 demand_empty_rest_of_line ();
17206}
17207
754e2bb9
RS
17208/* Handle a .stab[snd] directive. Ideally these directives would be
17209 implemented in a transparent way, so that removing them would not
17210 have any effect on the generated instructions. However, s_stab
17211 internally changes the section, so in practice we need to decide
17212 now whether the preceding label marks compressed code. We do not
17213 support changing the compression mode of a label after a .stab*
17214 directive, such as in:
17215
17216 foo:
134c0c8b 17217 .stabs ...
754e2bb9
RS
17218 .set mips16
17219
17220 so the current mode wins. */
252b5132
RH
17221
17222static void
17a2f251 17223s_mips_stab (int type)
252b5132 17224{
42c0794e 17225 file_mips_check_options ();
754e2bb9 17226 mips_mark_labels ();
252b5132
RH
17227 s_stab (type);
17228}
17229
54f4ddb3 17230/* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
252b5132
RH
17231
17232static void
17a2f251 17233s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
17234{
17235 char *name;
17236 int c;
17237 symbolS *symbolP;
17238 expressionS exp;
17239
d02603dc 17240 c = get_symbol_name (&name);
252b5132
RH
17241 symbolP = symbol_find_or_make (name);
17242 S_SET_WEAK (symbolP);
17243 *input_line_pointer = c;
17244
d02603dc 17245 SKIP_WHITESPACE_AFTER_NAME ();
252b5132
RH
17246
17247 if (! is_end_of_line[(unsigned char) *input_line_pointer])
17248 {
17249 if (S_IS_DEFINED (symbolP))
17250 {
20203fb9 17251 as_bad (_("ignoring attempt to redefine symbol %s"),
252b5132
RH
17252 S_GET_NAME (symbolP));
17253 ignore_rest_of_line ();
17254 return;
17255 }
bdaaa2e1 17256
252b5132
RH
17257 if (*input_line_pointer == ',')
17258 {
17259 ++input_line_pointer;
17260 SKIP_WHITESPACE ();
17261 }
bdaaa2e1 17262
252b5132
RH
17263 expression (&exp);
17264 if (exp.X_op != O_symbol)
17265 {
20203fb9 17266 as_bad (_("bad .weakext directive"));
98d3f06f 17267 ignore_rest_of_line ();
252b5132
RH
17268 return;
17269 }
49309057 17270 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
17271 }
17272
17273 demand_empty_rest_of_line ();
17274}
17275
17276/* Parse a register string into a number. Called from the ECOFF code
17277 to parse .frame. The argument is non-zero if this is the frame
17278 register, so that we can record it in mips_frame_reg. */
17279
17280int
17a2f251 17281tc_get_register (int frame)
252b5132 17282{
707bfff6 17283 unsigned int reg;
252b5132
RH
17284
17285 SKIP_WHITESPACE ();
707bfff6
TS
17286 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17287 reg = 0;
252b5132 17288 if (frame)
7a621144
DJ
17289 {
17290 mips_frame_reg = reg != 0 ? reg : SP;
17291 mips_frame_reg_valid = 1;
17292 mips_cprestore_valid = 0;
17293 }
252b5132
RH
17294 return reg;
17295}
17296
17297valueT
17a2f251 17298md_section_align (asection *seg, valueT addr)
252b5132
RH
17299{
17300 int align = bfd_get_section_alignment (stdoutput, seg);
17301
f3ded42a
RS
17302 /* We don't need to align ELF sections to the full alignment.
17303 However, Irix 5 may prefer that we align them at least to a 16
17304 byte boundary. We don't bother to align the sections if we
17305 are targeted for an embedded system. */
17306 if (strncmp (TARGET_OS, "elf", 3) == 0)
17307 return addr;
17308 if (align > 4)
17309 align = 4;
252b5132 17310
8d3842cd 17311 return ((addr + (1 << align) - 1) & -(1 << align));
252b5132
RH
17312}
17313
17314/* Utility routine, called from above as well. If called while the
17315 input file is still being read, it's only an approximation. (For
17316 example, a symbol may later become defined which appeared to be
17317 undefined earlier.) */
17318
17319static int
17a2f251 17320nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
17321{
17322 if (sym == 0)
17323 return 0;
17324
4d0d148d 17325 if (g_switch_value > 0)
252b5132
RH
17326 {
17327 const char *symname;
17328 int change;
17329
c9914766 17330 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
17331 register. It can be if it is smaller than the -G size or if
17332 it is in the .sdata or .sbss section. Certain symbols can
c9914766 17333 not be referenced off the $gp, although it appears as though
252b5132
RH
17334 they can. */
17335 symname = S_GET_NAME (sym);
17336 if (symname != (const char *) NULL
17337 && (strcmp (symname, "eprol") == 0
17338 || strcmp (symname, "etext") == 0
17339 || strcmp (symname, "_gp") == 0
17340 || strcmp (symname, "edata") == 0
17341 || strcmp (symname, "_fbss") == 0
17342 || strcmp (symname, "_fdata") == 0
17343 || strcmp (symname, "_ftext") == 0
17344 || strcmp (symname, "end") == 0
17345 || strcmp (symname, "_gp_disp") == 0))
17346 change = 1;
17347 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17348 && (0
17349#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
17350 || (symbol_get_obj (sym)->ecoff_extern_size != 0
17351 && (symbol_get_obj (sym)->ecoff_extern_size
17352 <= g_switch_value))
252b5132
RH
17353#endif
17354 /* We must defer this decision until after the whole
17355 file has been read, since there might be a .extern
17356 after the first use of this symbol. */
17357 || (before_relaxing
17358#ifndef NO_ECOFF_DEBUGGING
49309057 17359 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
17360#endif
17361 && S_GET_VALUE (sym) == 0)
17362 || (S_GET_VALUE (sym) != 0
17363 && S_GET_VALUE (sym) <= g_switch_value)))
17364 change = 0;
17365 else
17366 {
17367 const char *segname;
17368
17369 segname = segment_name (S_GET_SEGMENT (sym));
9c2799c2 17370 gas_assert (strcmp (segname, ".lit8") != 0
252b5132
RH
17371 && strcmp (segname, ".lit4") != 0);
17372 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
17373 && strcmp (segname, ".sbss") != 0
17374 && strncmp (segname, ".sdata.", 7) != 0
d4dc2f22
TS
17375 && strncmp (segname, ".sbss.", 6) != 0
17376 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
fba2b7f9 17377 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
17378 }
17379 return change;
17380 }
17381 else
c9914766 17382 /* We are not optimizing for the $gp register. */
252b5132
RH
17383 return 1;
17384}
17385
5919d012
RS
17386
17387/* Return true if the given symbol should be considered local for SVR4 PIC. */
17388
17389static bfd_boolean
9e009953 17390pic_need_relax (symbolS *sym)
5919d012
RS
17391{
17392 asection *symsec;
5919d012
RS
17393
17394 /* Handle the case of a symbol equated to another symbol. */
17395 while (symbol_equated_reloc_p (sym))
17396 {
17397 symbolS *n;
17398
5f0fe04b 17399 /* It's possible to get a loop here in a badly written program. */
5919d012
RS
17400 n = symbol_get_value_expression (sym)->X_add_symbol;
17401 if (n == sym)
17402 break;
17403 sym = n;
17404 }
17405
df1f3cda
DD
17406 if (symbol_section_p (sym))
17407 return TRUE;
17408
5919d012
RS
17409 symsec = S_GET_SEGMENT (sym);
17410
5919d012 17411 /* This must duplicate the test in adjust_reloc_syms. */
45dfa85a
AM
17412 return (!bfd_is_und_section (symsec)
17413 && !bfd_is_abs_section (symsec)
5f0fe04b 17414 && !bfd_is_com_section (symsec)
5919d012 17415 /* A global or weak symbol is treated as external. */
f3ded42a 17416 && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
5919d012 17417}
14f72d45
MR
17418\f
17419/* Given a MIPS16 variant frag FRAGP and PC-relative operand PCREL_OP
17420 convert a section-relative value VAL to the equivalent PC-relative
17421 value. */
17422
17423static offsetT
17424mips16_pcrel_val (fragS *fragp, const struct mips_pcrel_operand *pcrel_op,
17425 offsetT val, long stretch)
17426{
17427 fragS *sym_frag;
17428 addressT addr;
17429
17430 gas_assert (pcrel_op->root.root.type == OP_PCREL);
17431
17432 sym_frag = symbol_get_frag (fragp->fr_symbol);
17433
17434 /* If the relax_marker of the symbol fragment differs from the
17435 relax_marker of this fragment, we have not yet adjusted the
17436 symbol fragment fr_address. We want to add in STRETCH in
17437 order to get a better estimate of the address. This
17438 particularly matters because of the shift bits. */
17439 if (stretch != 0 && sym_frag->relax_marker != fragp->relax_marker)
17440 {
17441 fragS *f;
17442
17443 /* Adjust stretch for any alignment frag. Note that if have
17444 been expanding the earlier code, the symbol may be
17445 defined in what appears to be an earlier frag. FIXME:
17446 This doesn't handle the fr_subtype field, which specifies
17447 a maximum number of bytes to skip when doing an
17448 alignment. */
17449 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
17450 {
17451 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17452 {
17453 if (stretch < 0)
17454 stretch = -(-stretch & ~((1 << (int) f->fr_offset) - 1));
17455 else
17456 stretch &= ~((1 << (int) f->fr_offset) - 1);
17457 if (stretch == 0)
17458 break;
17459 }
17460 }
17461 if (f != NULL)
17462 val += stretch;
17463 }
17464
17465 addr = fragp->fr_address + fragp->fr_fix;
17466
17467 /* The base address rules are complicated. The base address of
17468 a branch is the following instruction. The base address of a
17469 PC relative load or add is the instruction itself, but if it
17470 is in a delay slot (in which case it can not be extended) use
17471 the address of the instruction whose delay slot it is in. */
17472 if (pcrel_op->include_isa_bit)
17473 {
17474 addr += 2;
17475
17476 /* If we are currently assuming that this frag should be
17477 extended, then the current address is two bytes higher. */
17478 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17479 addr += 2;
17480
17481 /* Ignore the low bit in the target, since it will be set
17482 for a text label. */
17483 val &= -2;
17484 }
17485 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17486 addr -= 4;
17487 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17488 addr -= 2;
5919d012 17489
14f72d45
MR
17490 val -= addr & -(1 << pcrel_op->align_log2);
17491
17492 return val;
17493}
5919d012 17494
252b5132
RH
17495/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17496 extended opcode. SEC is the section the frag is in. */
17497
17498static int
17a2f251 17499mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132 17500{
3ccad066 17501 const struct mips_int_operand *operand;
252b5132 17502 offsetT val;
252b5132 17503 segT symsec;
14f72d45 17504 int type;
252b5132
RH
17505
17506 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17507 return 0;
17508 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17509 return 1;
17510
88a7ef16 17511 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132 17512 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
3ccad066 17513 operand = mips16_immed_operand (type, FALSE);
88a7ef16
MR
17514 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
17515 || (operand->root.type == OP_PCREL
17516 ? sec != symsec
17517 : !bfd_is_abs_section (symsec)))
17518 return 1;
252b5132 17519
88a7ef16 17520 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
252b5132 17521
3ccad066 17522 if (operand->root.type == OP_PCREL)
252b5132 17523 {
3ccad066 17524 const struct mips_pcrel_operand *pcrel_op;
3ccad066 17525 offsetT maxtiny;
252b5132 17526
1425c41d 17527 if (RELAX_MIPS16_ALWAYS_EXTENDED (fragp->fr_subtype))
88a7ef16 17528 return 1;
252b5132 17529
88a7ef16 17530 pcrel_op = (const struct mips_pcrel_operand *) operand;
14f72d45 17531 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
252b5132
RH
17532
17533 /* If any of the shifted bits are set, we must use an extended
17534 opcode. If the address depends on the size of this
17535 instruction, this can lead to a loop, so we arrange to always
88a7ef16
MR
17536 use an extended opcode. */
17537 if ((val & ((1 << operand->shift) - 1)) != 0)
252b5132
RH
17538 {
17539 fragp->fr_subtype =
1425c41d 17540 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
252b5132
RH
17541 return 1;
17542 }
17543
17544 /* If we are about to mark a frag as extended because the value
3ccad066
RS
17545 is precisely the next value above maxtiny, then there is a
17546 chance of an infinite loop as in the following code:
252b5132
RH
17547 la $4,foo
17548 .skip 1020
17549 .align 2
17550 foo:
17551 In this case when the la is extended, foo is 0x3fc bytes
17552 away, so the la can be shrunk, but then foo is 0x400 away, so
17553 the la must be extended. To avoid this loop, we mark the
17554 frag as extended if it was small, and is about to become
3ccad066
RS
17555 extended with the next value above maxtiny. */
17556 maxtiny = mips_int_operand_max (operand);
17557 if (val == maxtiny + (1 << operand->shift)
88a7ef16 17558 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
252b5132
RH
17559 {
17560 fragp->fr_subtype =
1425c41d 17561 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
252b5132
RH
17562 return 1;
17563 }
17564 }
252b5132 17565
3ccad066 17566 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
252b5132
RH
17567}
17568
8507b6e7
MR
17569/* Given a MIPS16 variant frag FRAGP, return non-zero if it needs
17570 macro expansion. SEC is the section the frag is in. We only
17571 support PC-relative instructions (LA, DLA, LW, LD) here, in
17572 non-PIC code using 32-bit addressing. */
17573
17574static int
17575mips16_macro_frag (fragS *fragp, asection *sec, long stretch)
17576{
17577 const struct mips_pcrel_operand *pcrel_op;
17578 const struct mips_int_operand *operand;
17579 offsetT val;
17580 segT symsec;
17581 int type;
17582
17583 gas_assert (!RELAX_MIPS16_USER_SMALL (fragp->fr_subtype));
17584
17585 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17586 return 0;
17587 if (!RELAX_MIPS16_SYM32 (fragp->fr_subtype))
17588 return 0;
17589
17590 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17591 switch (type)
17592 {
17593 case 'A':
17594 case 'B':
17595 case 'E':
17596 symsec = S_GET_SEGMENT (fragp->fr_symbol);
17597 if (bfd_is_abs_section (symsec))
17598 return 1;
17599 if (RELAX_MIPS16_PIC (fragp->fr_subtype))
17600 return 0;
17601 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE) || sec != symsec)
17602 return 1;
17603
17604 operand = mips16_immed_operand (type, TRUE);
17605 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17606 pcrel_op = (const struct mips_pcrel_operand *) operand;
17607 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
17608
17609 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
17610
17611 default:
17612 return 0;
17613 }
17614}
17615
4a6a3df4
AO
17616/* Compute the length of a branch sequence, and adjust the
17617 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
17618 worst-case length is computed, with UPDATE being used to indicate
17619 whether an unconditional (-1), branch-likely (+1) or regular (0)
17620 branch is to be computed. */
17621static int
17a2f251 17622relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 17623{
b34976b6 17624 bfd_boolean toofar;
4a6a3df4
AO
17625 int length;
17626
17627 if (fragp
17628 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 17629 && !S_IS_WEAK (fragp->fr_symbol)
4a6a3df4
AO
17630 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17631 {
17632 addressT addr;
17633 offsetT val;
17634
17635 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17636
17637 addr = fragp->fr_address + fragp->fr_fix + 4;
17638
17639 val -= addr;
17640
17641 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17642 }
4a6a3df4 17643 else
c1f61bd2
MR
17644 /* If the symbol is not defined or it's in a different segment,
17645 we emit the long sequence. */
b34976b6 17646 toofar = TRUE;
4a6a3df4
AO
17647
17648 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17649 fragp->fr_subtype
66b3e8da 17650 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
ce8ad872 17651 RELAX_BRANCH_PIC (fragp->fr_subtype),
66b3e8da 17652 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
17653 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17654 RELAX_BRANCH_LINK (fragp->fr_subtype),
17655 toofar);
17656
17657 length = 4;
17658 if (toofar)
17659 {
17660 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17661 length += 8;
17662
ce8ad872 17663 if (!fragp || RELAX_BRANCH_PIC (fragp->fr_subtype))
4a6a3df4
AO
17664 {
17665 /* Additional space for PIC loading of target address. */
17666 length += 8;
17667 if (mips_opts.isa == ISA_MIPS1)
17668 /* Additional space for $at-stabilizing nop. */
17669 length += 4;
17670 }
17671
17672 /* If branch is conditional. */
17673 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17674 length += 8;
17675 }
b34976b6 17676
4a6a3df4
AO
17677 return length;
17678}
17679
7bd374a4
MR
17680/* Get a FRAG's branch instruction delay slot size, either from the
17681 short-delay-slot bit of a branch-and-link instruction if AL is TRUE,
17682 or SHORT_INSN_SIZE otherwise. */
17683
17684static int
17685frag_branch_delay_slot_size (fragS *fragp, bfd_boolean al, int short_insn_size)
17686{
17687 char *buf = fragp->fr_literal + fragp->fr_fix;
17688
17689 if (al)
17690 return (read_compressed_insn (buf, 4) & 0x02000000) ? 2 : 4;
17691 else
17692 return short_insn_size;
17693}
17694
df58fc94
RS
17695/* Compute the length of a branch sequence, and adjust the
17696 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
17697 worst-case length is computed, with UPDATE being used to indicate
17698 whether an unconditional (-1), or regular (0) branch is to be
17699 computed. */
17700
17701static int
17702relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
17703{
7bd374a4
MR
17704 bfd_boolean insn32 = TRUE;
17705 bfd_boolean nods = TRUE;
ce8ad872 17706 bfd_boolean pic = TRUE;
7bd374a4
MR
17707 bfd_boolean al = TRUE;
17708 int short_insn_size;
df58fc94
RS
17709 bfd_boolean toofar;
17710 int length;
17711
7bd374a4
MR
17712 if (fragp)
17713 {
17714 insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
17715 nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
ce8ad872 17716 pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
7bd374a4
MR
17717 al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
17718 }
17719 short_insn_size = insn32 ? 4 : 2;
17720
df58fc94
RS
17721 if (fragp
17722 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 17723 && !S_IS_WEAK (fragp->fr_symbol)
df58fc94
RS
17724 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17725 {
17726 addressT addr;
17727 offsetT val;
17728
17729 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17730 /* Ignore the low bit in the target, since it will be set
17731 for a text label. */
17732 if ((val & 1) != 0)
17733 --val;
17734
17735 addr = fragp->fr_address + fragp->fr_fix + 4;
17736
17737 val -= addr;
17738
17739 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
17740 }
df58fc94 17741 else
c1f61bd2
MR
17742 /* If the symbol is not defined or it's in a different segment,
17743 we emit the long sequence. */
df58fc94
RS
17744 toofar = TRUE;
17745
17746 if (fragp && update
17747 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17748 fragp->fr_subtype = (toofar
17749 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
17750 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
17751
17752 length = 4;
17753 if (toofar)
17754 {
17755 bfd_boolean compact_known = fragp != NULL;
17756 bfd_boolean compact = FALSE;
17757 bfd_boolean uncond;
17758
df58fc94 17759 if (fragp)
8484fb75
MR
17760 {
17761 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17762 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
8484fb75 17763 }
df58fc94
RS
17764 else
17765 uncond = update < 0;
17766
17767 /* If label is out of range, we turn branch <br>:
17768
17769 <br> label # 4 bytes
17770 0:
17771
17772 into:
17773
17774 j label # 4 bytes
8484fb75
MR
17775 nop # 2/4 bytes if
17776 # compact && (!PIC || insn32)
df58fc94
RS
17777 0:
17778 */
ce8ad872 17779 if ((!pic || insn32) && (!compact_known || compact))
8484fb75 17780 length += short_insn_size;
df58fc94
RS
17781
17782 /* If assembling PIC code, we further turn:
17783
17784 j label # 4 bytes
17785
17786 into:
17787
17788 lw/ld at, %got(label)(gp) # 4 bytes
17789 d/addiu at, %lo(label) # 4 bytes
8484fb75 17790 jr/c at # 2/4 bytes
df58fc94 17791 */
ce8ad872 17792 if (pic)
8484fb75 17793 length += 4 + short_insn_size;
df58fc94 17794
7bd374a4
MR
17795 /* Add an extra nop if the jump has no compact form and we need
17796 to fill the delay slot. */
ce8ad872 17797 if ((!pic || al) && nods)
7bd374a4
MR
17798 length += (fragp
17799 ? frag_branch_delay_slot_size (fragp, al, short_insn_size)
17800 : short_insn_size);
17801
df58fc94
RS
17802 /* If branch <br> is conditional, we prepend negated branch <brneg>:
17803
17804 <brneg> 0f # 4 bytes
8484fb75 17805 nop # 2/4 bytes if !compact
df58fc94
RS
17806 */
17807 if (!uncond)
8484fb75 17808 length += (compact_known && compact) ? 4 : 4 + short_insn_size;
df58fc94 17809 }
7bd374a4
MR
17810 else if (nods)
17811 {
17812 /* Add an extra nop to fill the delay slot. */
17813 gas_assert (fragp);
17814 length += frag_branch_delay_slot_size (fragp, al, short_insn_size);
17815 }
df58fc94
RS
17816
17817 return length;
17818}
17819
17820/* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
17821 bit accordingly. */
17822
17823static int
17824relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
17825{
17826 bfd_boolean toofar;
17827
df58fc94
RS
17828 if (fragp
17829 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 17830 && !S_IS_WEAK (fragp->fr_symbol)
df58fc94
RS
17831 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17832 {
17833 addressT addr;
17834 offsetT val;
17835 int type;
17836
17837 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17838 /* Ignore the low bit in the target, since it will be set
17839 for a text label. */
17840 if ((val & 1) != 0)
17841 --val;
17842
17843 /* Assume this is a 2-byte branch. */
17844 addr = fragp->fr_address + fragp->fr_fix + 2;
17845
17846 /* We try to avoid the infinite loop by not adding 2 more bytes for
17847 long branches. */
17848
17849 val -= addr;
17850
17851 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
17852 if (type == 'D')
17853 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
17854 else if (type == 'E')
17855 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
17856 else
17857 abort ();
17858 }
17859 else
17860 /* If the symbol is not defined or it's in a different segment,
17861 we emit a normal 32-bit branch. */
17862 toofar = TRUE;
17863
17864 if (fragp && update
17865 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17866 fragp->fr_subtype
17867 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
17868 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
17869
17870 if (toofar)
17871 return 4;
17872
17873 return 2;
17874}
17875
252b5132
RH
17876/* Estimate the size of a frag before relaxing. Unless this is the
17877 mips16, we are not really relaxing here, and the final size is
17878 encoded in the subtype information. For the mips16, we have to
17879 decide whether we are using an extended opcode or not. */
17880
252b5132 17881int
17a2f251 17882md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 17883{
5919d012 17884 int change;
252b5132 17885
4a6a3df4
AO
17886 if (RELAX_BRANCH_P (fragp->fr_subtype))
17887 {
17888
b34976b6
AM
17889 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
17890
4a6a3df4
AO
17891 return fragp->fr_var;
17892 }
17893
252b5132 17894 if (RELAX_MIPS16_P (fragp->fr_subtype))
8507b6e7
MR
17895 {
17896 /* We don't want to modify the EXTENDED bit here; it might get us
17897 into infinite loops. We change it only in mips_relax_frag(). */
17898 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
25499ac7 17899 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 8 : 12;
8507b6e7
MR
17900 else
17901 return RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2;
17902 }
252b5132 17903
df58fc94
RS
17904 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17905 {
17906 int length = 4;
17907
17908 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17909 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
17910 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17911 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
17912 fragp->fr_var = length;
17913
17914 return length;
17915 }
17916
ce8ad872 17917 if (mips_pic == VXWORKS_PIC)
0a44bf69
RS
17918 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
17919 change = 0;
ce8ad872
MR
17920 else if (RELAX_PIC (fragp->fr_subtype))
17921 change = pic_need_relax (fragp->fr_symbol);
252b5132 17922 else
ce8ad872 17923 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132
RH
17924
17925 if (change)
17926 {
4d7206a2 17927 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 17928 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 17929 }
4d7206a2
RS
17930 else
17931 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
17932}
17933
17934/* This is called to see whether a reloc against a defined symbol
de7e6852 17935 should be converted into a reloc against a section. */
252b5132
RH
17936
17937int
17a2f251 17938mips_fix_adjustable (fixS *fixp)
252b5132 17939{
252b5132
RH
17940 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
17941 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17942 return 0;
a161fe53 17943
252b5132
RH
17944 if (fixp->fx_addsy == NULL)
17945 return 1;
a161fe53 17946
2f0c68f2
CM
17947 /* Allow relocs used for EH tables. */
17948 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
17949 return 1;
17950
de7e6852
RS
17951 /* If symbol SYM is in a mergeable section, relocations of the form
17952 SYM + 0 can usually be made section-relative. The mergeable data
17953 is then identified by the section offset rather than by the symbol.
17954
17955 However, if we're generating REL LO16 relocations, the offset is split
33eaf5de 17956 between the LO16 and partnering high part relocation. The linker will
de7e6852
RS
17957 need to recalculate the complete offset in order to correctly identify
17958 the merge data.
17959
33eaf5de 17960 The linker has traditionally not looked for the partnering high part
de7e6852
RS
17961 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
17962 placed anywhere. Rather than break backwards compatibility by changing
17963 this, it seems better not to force the issue, and instead keep the
17964 original symbol. This will work with either linker behavior. */
738e5348 17965 if ((lo16_reloc_p (fixp->fx_r_type)
704803a9 17966 || reloc_needs_lo_p (fixp->fx_r_type))
de7e6852
RS
17967 && HAVE_IN_PLACE_ADDENDS
17968 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
17969 return 0;
17970
97f50151
MR
17971 /* There is no place to store an in-place offset for JALR relocations. */
17972 if (jalr_reloc_p (fixp->fx_r_type) && HAVE_IN_PLACE_ADDENDS)
17973 return 0;
17974
17975 /* Likewise an in-range offset of limited PC-relative relocations may
2de39019 17976 overflow the in-place relocatable field if recalculated against the
7361da2c
AB
17977 start address of the symbol's containing section.
17978
17979 Also, PC relative relocations for MIPS R6 need to be symbol rather than
17980 section relative to allow linker relaxations to be performed later on. */
97f50151 17981 if (limited_pcrel_reloc_p (fixp->fx_r_type)
912815f0 17982 && (HAVE_IN_PLACE_ADDENDS || ISA_IS_R6 (file_mips_opts.isa)))
1180b5a4
RS
17983 return 0;
17984
b314ec0e
RS
17985 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
17986 to a floating-point stub. The same is true for non-R_MIPS16_26
17987 relocations against MIPS16 functions; in this case, the stub becomes
17988 the function's canonical address.
17989
17990 Floating-point stubs are stored in unique .mips16.call.* or
17991 .mips16.fn.* sections. If a stub T for function F is in section S,
17992 the first relocation in section S must be against F; this is how the
17993 linker determines the target function. All relocations that might
17994 resolve to T must also be against F. We therefore have the following
17995 restrictions, which are given in an intentionally-redundant way:
17996
17997 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
17998 symbols.
17999
18000 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
18001 if that stub might be used.
18002
18003 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
18004 symbols.
18005
18006 4. We cannot reduce a stub's relocations against MIPS16 symbols if
18007 that stub might be used.
18008
18009 There is a further restriction:
18010
df58fc94 18011 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
0e9c5a5c 18012 R_MICROMIPS_26_S1) or branch relocations (R_MIPS_PC26_S2,
c9775dde
MR
18013 R_MIPS_PC21_S2, R_MIPS_PC16, R_MIPS16_PC16_S1,
18014 R_MICROMIPS_PC16_S1, R_MICROMIPS_PC10_S1 or R_MICROMIPS_PC7_S1)
18015 against MIPS16 or microMIPS symbols because we need to keep the
18016 MIPS16 or microMIPS symbol for the purpose of mode mismatch
a6ebf616
MR
18017 detection and JAL or BAL to JALX instruction conversion in the
18018 linker.
b314ec0e 18019
df58fc94 18020 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
507dcb32 18021 against a MIPS16 symbol. We deal with (5) by additionally leaving
0e9c5a5c 18022 alone any jump and branch relocations against a microMIPS symbol.
b314ec0e
RS
18023
18024 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
18025 relocation against some symbol R, no relocation against R may be
18026 reduced. (Note that this deals with (2) as well as (1) because
18027 relocations against global symbols will never be reduced on ELF
18028 targets.) This approach is a little simpler than trying to detect
18029 stub sections, and gives the "all or nothing" per-symbol consistency
18030 that we have for MIPS16 symbols. */
f3ded42a 18031 if (fixp->fx_subsy == NULL
30c09090 18032 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
44d3da23 18033 || (ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
0e9c5a5c
MR
18034 && (jmp_reloc_p (fixp->fx_r_type)
18035 || b_reloc_p (fixp->fx_r_type)))
44d3da23 18036 || *symbol_get_tc (fixp->fx_addsy)))
252b5132 18037 return 0;
a161fe53 18038
252b5132
RH
18039 return 1;
18040}
18041
18042/* Translate internal representation of relocation info to BFD target
18043 format. */
18044
18045arelent **
17a2f251 18046tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
18047{
18048 static arelent *retval[4];
18049 arelent *reloc;
18050 bfd_reloc_code_real_type code;
18051
4b0cff4e 18052 memset (retval, 0, sizeof(retval));
325801bd
TS
18053 reloc = retval[0] = XCNEW (arelent);
18054 reloc->sym_ptr_ptr = XNEW (asymbol *);
49309057 18055 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
18056 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
18057
bad36eac
DJ
18058 if (fixp->fx_pcrel)
18059 {
df58fc94 18060 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
c9775dde 18061 || fixp->fx_r_type == BFD_RELOC_MIPS16_16_PCREL_S1
df58fc94
RS
18062 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
18063 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
b47468a6 18064 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
7361da2c
AB
18065 || fixp->fx_r_type == BFD_RELOC_32_PCREL
18066 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
18067 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
18068 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
18069 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
18070 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
18071 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL);
bad36eac
DJ
18072
18073 /* At this point, fx_addnumber is "symbol offset - pcrel address".
18074 Relocations want only the symbol offset. */
51f6035b
MR
18075 switch (fixp->fx_r_type)
18076 {
18077 case BFD_RELOC_MIPS_18_PCREL_S3:
18078 reloc->addend = fixp->fx_addnumber + (reloc->address & ~7);
18079 break;
18080 default:
18081 reloc->addend = fixp->fx_addnumber + reloc->address;
18082 break;
18083 }
bad36eac 18084 }
17c6c9d9
MR
18085 else if (HAVE_IN_PLACE_ADDENDS
18086 && fixp->fx_r_type == BFD_RELOC_MICROMIPS_JMP
18087 && (read_compressed_insn (fixp->fx_frag->fr_literal
18088 + fixp->fx_where, 4) >> 26) == 0x3c)
18089 {
18090 /* Shift is 2, unusually, for microMIPS JALX. Adjust the in-place
18091 addend accordingly. */
18092 reloc->addend = fixp->fx_addnumber >> 1;
18093 }
bad36eac
DJ
18094 else
18095 reloc->addend = fixp->fx_addnumber;
252b5132 18096
438c16b8
TS
18097 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
18098 entry to be used in the relocation's section offset. */
18099 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
18100 {
18101 reloc->address = reloc->addend;
18102 reloc->addend = 0;
18103 }
18104
252b5132 18105 code = fixp->fx_r_type;
252b5132 18106
bad36eac 18107 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
252b5132
RH
18108 if (reloc->howto == NULL)
18109 {
18110 as_bad_where (fixp->fx_file, fixp->fx_line,
1661c76c
RS
18111 _("cannot represent %s relocation in this object file"
18112 " format"),
252b5132
RH
18113 bfd_get_reloc_code_name (code));
18114 retval[0] = NULL;
18115 }
18116
18117 return retval;
18118}
18119
18120/* Relax a machine dependent frag. This returns the amount by which
18121 the current size of the frag should change. */
18122
18123int
17a2f251 18124mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 18125{
4a6a3df4
AO
18126 if (RELAX_BRANCH_P (fragp->fr_subtype))
18127 {
18128 offsetT old_var = fragp->fr_var;
b34976b6
AM
18129
18130 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
18131
18132 return fragp->fr_var - old_var;
18133 }
18134
df58fc94
RS
18135 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18136 {
18137 offsetT old_var = fragp->fr_var;
18138 offsetT new_var = 4;
18139
18140 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
18141 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
18142 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
18143 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
18144 fragp->fr_var = new_var;
18145
18146 return new_var - old_var;
18147 }
18148
252b5132
RH
18149 if (! RELAX_MIPS16_P (fragp->fr_subtype))
18150 return 0;
18151
8507b6e7 18152 if (!mips16_extended_frag (fragp, sec, stretch))
252b5132 18153 {
8507b6e7
MR
18154 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18155 {
18156 fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
25499ac7 18157 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -6 : -10;
8507b6e7
MR
18158 }
18159 else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18160 {
18161 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18162 return -2;
18163 }
18164 else
18165 return 0;
18166 }
18167 else if (!mips16_macro_frag (fragp, sec, stretch))
18168 {
18169 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18170 {
18171 fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
18172 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
25499ac7 18173 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -4 : -8;
8507b6e7
MR
18174 }
18175 else if (!RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18176 {
18177 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
18178 return 2;
18179 }
18180 else
252b5132 18181 return 0;
252b5132
RH
18182 }
18183 else
18184 {
8507b6e7 18185 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
252b5132 18186 return 0;
8507b6e7
MR
18187 else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18188 {
18189 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18190 fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
25499ac7 18191 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 4 : 8;
8507b6e7
MR
18192 }
18193 else
18194 {
18195 fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
25499ac7 18196 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 6 : 10;
8507b6e7 18197 }
252b5132
RH
18198 }
18199
18200 return 0;
18201}
18202
18203/* Convert a machine dependent frag. */
18204
18205void
17a2f251 18206md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 18207{
4a6a3df4
AO
18208 if (RELAX_BRANCH_P (fragp->fr_subtype))
18209 {
4d68580a 18210 char *buf;
4a6a3df4 18211 unsigned long insn;
4a6a3df4 18212 fixS *fixp;
b34976b6 18213
4d68580a
RS
18214 buf = fragp->fr_literal + fragp->fr_fix;
18215 insn = read_insn (buf);
b34976b6 18216
4a6a3df4
AO
18217 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
18218 {
18219 /* We generate a fixup instead of applying it right now
18220 because, if there are linker relaxations, we're going to
18221 need the relocations. */
bbd27b76
MR
18222 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18223 fragp->fr_symbol, fragp->fr_offset,
18224 TRUE, BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
18225 fixp->fx_file = fragp->fr_file;
18226 fixp->fx_line = fragp->fr_line;
b34976b6 18227
4d68580a 18228 buf = write_insn (buf, insn);
4a6a3df4
AO
18229 }
18230 else
18231 {
18232 int i;
18233
18234 as_warn_where (fragp->fr_file, fragp->fr_line,
1661c76c 18235 _("relaxed out-of-range branch into a jump"));
4a6a3df4
AO
18236
18237 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
18238 goto uncond;
18239
18240 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18241 {
18242 /* Reverse the branch. */
18243 switch ((insn >> 28) & 0xf)
18244 {
18245 case 4:
56d438b1
CF
18246 if ((insn & 0xff000000) == 0x47000000
18247 || (insn & 0xff600000) == 0x45600000)
18248 {
18249 /* BZ.df/BNZ.df, BZ.V/BNZ.V can have the condition
18250 reversed by tweaking bit 23. */
18251 insn ^= 0x00800000;
18252 }
18253 else
18254 {
18255 /* bc[0-3][tf]l? instructions can have the condition
18256 reversed by tweaking a single TF bit, and their
18257 opcodes all have 0x4???????. */
18258 gas_assert ((insn & 0xf3e00000) == 0x41000000);
18259 insn ^= 0x00010000;
18260 }
4a6a3df4
AO
18261 break;
18262
18263 case 0:
18264 /* bltz 0x04000000 bgez 0x04010000
54f4ddb3 18265 bltzal 0x04100000 bgezal 0x04110000 */
9c2799c2 18266 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
4a6a3df4
AO
18267 insn ^= 0x00010000;
18268 break;
b34976b6 18269
4a6a3df4
AO
18270 case 1:
18271 /* beq 0x10000000 bne 0x14000000
54f4ddb3 18272 blez 0x18000000 bgtz 0x1c000000 */
4a6a3df4
AO
18273 insn ^= 0x04000000;
18274 break;
18275
18276 default:
18277 abort ();
18278 }
18279 }
18280
18281 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18282 {
18283 /* Clear the and-link bit. */
9c2799c2 18284 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
4a6a3df4 18285
54f4ddb3
TS
18286 /* bltzal 0x04100000 bgezal 0x04110000
18287 bltzall 0x04120000 bgezall 0x04130000 */
4a6a3df4
AO
18288 insn &= ~0x00100000;
18289 }
18290
18291 /* Branch over the branch (if the branch was likely) or the
18292 full jump (not likely case). Compute the offset from the
18293 current instruction to branch to. */
18294 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18295 i = 16;
18296 else
18297 {
18298 /* How many bytes in instructions we've already emitted? */
4d68580a 18299 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18300 /* How many bytes in instructions from here to the end? */
18301 i = fragp->fr_var - i;
18302 }
18303 /* Convert to instruction count. */
18304 i >>= 2;
18305 /* Branch counts from the next instruction. */
b34976b6 18306 i--;
4a6a3df4
AO
18307 insn |= i;
18308 /* Branch over the jump. */
4d68580a 18309 buf = write_insn (buf, insn);
4a6a3df4 18310
54f4ddb3 18311 /* nop */
4d68580a 18312 buf = write_insn (buf, 0);
4a6a3df4
AO
18313
18314 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18315 {
18316 /* beql $0, $0, 2f */
18317 insn = 0x50000000;
18318 /* Compute the PC offset from the current instruction to
18319 the end of the variable frag. */
18320 /* How many bytes in instructions we've already emitted? */
4d68580a 18321 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18322 /* How many bytes in instructions from here to the end? */
18323 i = fragp->fr_var - i;
18324 /* Convert to instruction count. */
18325 i >>= 2;
18326 /* Don't decrement i, because we want to branch over the
18327 delay slot. */
4a6a3df4 18328 insn |= i;
4a6a3df4 18329
4d68580a
RS
18330 buf = write_insn (buf, insn);
18331 buf = write_insn (buf, 0);
4a6a3df4
AO
18332 }
18333
18334 uncond:
ce8ad872 18335 if (!RELAX_BRANCH_PIC (fragp->fr_subtype))
4a6a3df4
AO
18336 {
18337 /* j or jal. */
18338 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18339 ? 0x0c000000 : 0x08000000);
4a6a3df4 18340
bbd27b76
MR
18341 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18342 fragp->fr_symbol, fragp->fr_offset,
18343 FALSE, BFD_RELOC_MIPS_JMP);
4a6a3df4
AO
18344 fixp->fx_file = fragp->fr_file;
18345 fixp->fx_line = fragp->fr_line;
18346
4d68580a 18347 buf = write_insn (buf, insn);
4a6a3df4
AO
18348 }
18349 else
18350 {
66b3e8da
MR
18351 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18352
4a6a3df4 18353 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
66b3e8da
MR
18354 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18355 insn |= at << OP_SH_RT;
4a6a3df4 18356
bbd27b76
MR
18357 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18358 fragp->fr_symbol, fragp->fr_offset,
18359 FALSE, BFD_RELOC_MIPS_GOT16);
4a6a3df4
AO
18360 fixp->fx_file = fragp->fr_file;
18361 fixp->fx_line = fragp->fr_line;
18362
4d68580a 18363 buf = write_insn (buf, insn);
b34976b6 18364
4a6a3df4 18365 if (mips_opts.isa == ISA_MIPS1)
4d68580a
RS
18366 /* nop */
18367 buf = write_insn (buf, 0);
4a6a3df4
AO
18368
18369 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
66b3e8da
MR
18370 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18371 insn |= at << OP_SH_RS | at << OP_SH_RT;
4a6a3df4 18372
bbd27b76
MR
18373 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18374 fragp->fr_symbol, fragp->fr_offset,
18375 FALSE, BFD_RELOC_LO16);
4a6a3df4
AO
18376 fixp->fx_file = fragp->fr_file;
18377 fixp->fx_line = fragp->fr_line;
b34976b6 18378
4d68580a 18379 buf = write_insn (buf, insn);
4a6a3df4
AO
18380
18381 /* j(al)r $at. */
18382 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
66b3e8da 18383 insn = 0x0000f809;
4a6a3df4 18384 else
66b3e8da
MR
18385 insn = 0x00000008;
18386 insn |= at << OP_SH_RS;
4a6a3df4 18387
4d68580a 18388 buf = write_insn (buf, insn);
4a6a3df4
AO
18389 }
18390 }
18391
4a6a3df4 18392 fragp->fr_fix += fragp->fr_var;
4d68580a 18393 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
4a6a3df4
AO
18394 return;
18395 }
18396
df58fc94
RS
18397 /* Relax microMIPS branches. */
18398 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18399 {
4d68580a 18400 char *buf = fragp->fr_literal + fragp->fr_fix;
df58fc94 18401 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
8484fb75 18402 bfd_boolean insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
7bd374a4 18403 bfd_boolean nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
ce8ad872 18404 bfd_boolean pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
df58fc94
RS
18405 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18406 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
2309ddf2 18407 bfd_boolean short_ds;
df58fc94 18408 unsigned long insn;
df58fc94
RS
18409 fixS *fixp;
18410
df58fc94
RS
18411 fragp->fr_fix += fragp->fr_var;
18412
18413 /* Handle 16-bit branches that fit or are forced to fit. */
18414 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18415 {
18416 /* We generate a fixup instead of applying it right now,
18417 because if there is linker relaxation, we're going to
18418 need the relocations. */
834a65aa
MR
18419 switch (type)
18420 {
18421 case 'D':
18422 fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18423 fragp->fr_symbol, fragp->fr_offset,
18424 TRUE, BFD_RELOC_MICROMIPS_10_PCREL_S1);
18425 break;
18426 case 'E':
18427 fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18428 fragp->fr_symbol, fragp->fr_offset,
18429 TRUE, BFD_RELOC_MICROMIPS_7_PCREL_S1);
18430 break;
18431 default:
18432 abort ();
18433 }
df58fc94
RS
18434
18435 fixp->fx_file = fragp->fr_file;
18436 fixp->fx_line = fragp->fr_line;
18437
18438 /* These relocations can have an addend that won't fit in
18439 2 octets. */
18440 fixp->fx_no_overflow = 1;
18441
18442 return;
18443 }
18444
2309ddf2 18445 /* Handle 32-bit branches that fit or are forced to fit. */
df58fc94
RS
18446 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18447 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18448 {
18449 /* We generate a fixup instead of applying it right now,
18450 because if there is linker relaxation, we're going to
18451 need the relocations. */
bbd27b76
MR
18452 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18453 fragp->fr_symbol, fragp->fr_offset,
18454 TRUE, BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18455 fixp->fx_file = fragp->fr_file;
18456 fixp->fx_line = fragp->fr_line;
18457
18458 if (type == 0)
7bd374a4
MR
18459 {
18460 insn = read_compressed_insn (buf, 4);
18461 buf += 4;
18462
18463 if (nods)
18464 {
18465 /* Check the short-delay-slot bit. */
18466 if (!al || (insn & 0x02000000) != 0)
18467 buf = write_compressed_insn (buf, 0x0c00, 2);
18468 else
18469 buf = write_compressed_insn (buf, 0x00000000, 4);
18470 }
18471
18472 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18473 return;
18474 }
df58fc94
RS
18475 }
18476
18477 /* Relax 16-bit branches to 32-bit branches. */
18478 if (type != 0)
18479 {
4d68580a 18480 insn = read_compressed_insn (buf, 2);
df58fc94
RS
18481
18482 if ((insn & 0xfc00) == 0xcc00) /* b16 */
18483 insn = 0x94000000; /* beq */
18484 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
18485 {
18486 unsigned long regno;
18487
18488 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18489 regno = micromips_to_32_reg_d_map [regno];
18490 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
18491 insn |= regno << MICROMIPSOP_SH_RS;
18492 }
18493 else
18494 abort ();
18495
18496 /* Nothing else to do, just write it out. */
18497 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18498 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18499 {
4d68580a 18500 buf = write_compressed_insn (buf, insn, 4);
7bd374a4
MR
18501 if (nods)
18502 buf = write_compressed_insn (buf, 0x0c00, 2);
4d68580a 18503 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18504 return;
18505 }
18506 }
18507 else
4d68580a 18508 insn = read_compressed_insn (buf, 4);
df58fc94
RS
18509
18510 /* Relax 32-bit branches to a sequence of instructions. */
18511 as_warn_where (fragp->fr_file, fragp->fr_line,
1661c76c 18512 _("relaxed out-of-range branch into a jump"));
df58fc94 18513
2309ddf2 18514 /* Set the short-delay-slot bit. */
7bd374a4 18515 short_ds = !al || (insn & 0x02000000) != 0;
df58fc94
RS
18516
18517 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18518 {
18519 symbolS *l;
18520
18521 /* Reverse the branch. */
18522 if ((insn & 0xfc000000) == 0x94000000 /* beq */
18523 || (insn & 0xfc000000) == 0xb4000000) /* bne */
18524 insn ^= 0x20000000;
18525 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
18526 || (insn & 0xffe00000) == 0x40400000 /* bgez */
18527 || (insn & 0xffe00000) == 0x40800000 /* blez */
18528 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
18529 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
18530 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
18531 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
18532 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
18533 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
18534 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
18535 insn ^= 0x00400000;
18536 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
18537 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
18538 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
18539 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
18540 insn ^= 0x00200000;
56d438b1
CF
18541 else if ((insn & 0xff000000) == 0x83000000 /* BZ.df
18542 BNZ.df */
18543 || (insn & 0xff600000) == 0x81600000) /* BZ.V
18544 BNZ.V */
18545 insn ^= 0x00800000;
df58fc94
RS
18546 else
18547 abort ();
18548
18549 if (al)
18550 {
18551 /* Clear the and-link and short-delay-slot bits. */
18552 gas_assert ((insn & 0xfda00000) == 0x40200000);
18553
18554 /* bltzal 0x40200000 bgezal 0x40600000 */
18555 /* bltzals 0x42200000 bgezals 0x42600000 */
18556 insn &= ~0x02200000;
18557 }
18558
18559 /* Make a label at the end for use with the branch. */
18560 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
18561 micromips_label_inc ();
f3ded42a 18562 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
df58fc94
RS
18563
18564 /* Refer to it. */
4d68580a
RS
18565 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
18566 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18567 fixp->fx_file = fragp->fr_file;
18568 fixp->fx_line = fragp->fr_line;
18569
18570 /* Branch over the jump. */
4d68580a 18571 buf = write_compressed_insn (buf, insn, 4);
8484fb75 18572
df58fc94 18573 if (!compact)
8484fb75
MR
18574 {
18575 /* nop */
18576 if (insn32)
18577 buf = write_compressed_insn (buf, 0x00000000, 4);
18578 else
18579 buf = write_compressed_insn (buf, 0x0c00, 2);
18580 }
df58fc94
RS
18581 }
18582
ce8ad872 18583 if (!pic)
df58fc94 18584 {
7bd374a4
MR
18585 unsigned long jal = (short_ds || nods
18586 ? 0x74000000 : 0xf4000000); /* jal/s */
2309ddf2 18587
df58fc94
RS
18588 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
18589 insn = al ? jal : 0xd4000000;
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_JMP);
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);
8484fb75 18598
7bd374a4 18599 if (compact || nods)
8484fb75
MR
18600 {
18601 /* nop */
18602 if (insn32)
18603 buf = write_compressed_insn (buf, 0x00000000, 4);
18604 else
18605 buf = write_compressed_insn (buf, 0x0c00, 2);
18606 }
df58fc94
RS
18607 }
18608 else
18609 {
18610 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
18611
18612 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
18613 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18614 insn |= at << MICROMIPSOP_SH_RT;
18615
bbd27b76
MR
18616 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18617 fragp->fr_symbol, fragp->fr_offset,
18618 FALSE, BFD_RELOC_MICROMIPS_GOT16);
df58fc94
RS
18619 fixp->fx_file = fragp->fr_file;
18620 fixp->fx_line = fragp->fr_line;
18621
4d68580a 18622 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
18623
18624 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
18625 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18626 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18627
bbd27b76
MR
18628 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18629 fragp->fr_symbol, fragp->fr_offset,
18630 FALSE, BFD_RELOC_MICROMIPS_LO16);
df58fc94
RS
18631 fixp->fx_file = fragp->fr_file;
18632 fixp->fx_line = fragp->fr_line;
18633
4d68580a 18634 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18635
8484fb75
MR
18636 if (insn32)
18637 {
18638 /* jr/jalr $at */
18639 insn = 0x00000f3c | (al ? RA : ZERO) << MICROMIPSOP_SH_RT;
18640 insn |= at << MICROMIPSOP_SH_RS;
18641
18642 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18643
7bd374a4 18644 if (compact || nods)
8484fb75
MR
18645 /* nop */
18646 buf = write_compressed_insn (buf, 0x00000000, 4);
18647 }
18648 else
18649 {
18650 /* jr/jrc/jalr/jalrs $at */
18651 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
7bd374a4 18652 unsigned long jr = compact || nods ? 0x45a0 : 0x4580; /* jr/c */
8484fb75
MR
18653
18654 insn = al ? jalr : jr;
18655 insn |= at << MICROMIPSOP_SH_MJ;
18656
18657 buf = write_compressed_insn (buf, insn, 2);
7bd374a4
MR
18658 if (al && nods)
18659 {
18660 /* nop */
18661 if (short_ds)
18662 buf = write_compressed_insn (buf, 0x0c00, 2);
18663 else
18664 buf = write_compressed_insn (buf, 0x00000000, 4);
18665 }
8484fb75 18666 }
df58fc94
RS
18667 }
18668
4d68580a 18669 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18670 return;
18671 }
18672
252b5132
RH
18673 if (RELAX_MIPS16_P (fragp->fr_subtype))
18674 {
18675 int type;
3ccad066 18676 const struct mips_int_operand *operand;
252b5132 18677 offsetT val;
5c04167a 18678 char *buf;
8507b6e7 18679 unsigned int user_length;
9d862524 18680 bfd_boolean need_reloc;
252b5132 18681 unsigned long insn;
8507b6e7 18682 bfd_boolean mac;
5c04167a 18683 bfd_boolean ext;
88a7ef16 18684 segT symsec;
252b5132
RH
18685
18686 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
3ccad066 18687 operand = mips16_immed_operand (type, FALSE);
252b5132 18688
8507b6e7 18689 mac = RELAX_MIPS16_MACRO (fragp->fr_subtype);
5c04167a 18690 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
88a7ef16 18691 val = resolve_symbol_value (fragp->fr_symbol) + fragp->fr_offset;
9d862524
MR
18692
18693 symsec = S_GET_SEGMENT (fragp->fr_symbol);
18694 need_reloc = (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
8507b6e7 18695 || (operand->root.type == OP_PCREL && !mac
9d862524
MR
18696 ? asec != symsec
18697 : !bfd_is_abs_section (symsec)));
18698
8507b6e7 18699 if (operand->root.type == OP_PCREL && !mac)
252b5132 18700 {
3ccad066 18701 const struct mips_pcrel_operand *pcrel_op;
252b5132 18702
3ccad066 18703 pcrel_op = (const struct mips_pcrel_operand *) operand;
252b5132 18704
14f72d45 18705 if (pcrel_op->include_isa_bit && !need_reloc)
252b5132 18706 {
37b2d327
MR
18707 if (!mips_ignore_branch_isa
18708 && !ELF_ST_IS_MIPS16 (S_GET_OTHER (fragp->fr_symbol)))
14f72d45
MR
18709 as_bad_where (fragp->fr_file, fragp->fr_line,
18710 _("branch to a symbol in another ISA mode"));
18711 else if ((fragp->fr_offset & 0x1) != 0)
18712 as_bad_where (fragp->fr_file, fragp->fr_line,
18713 _("branch to misaligned address (0x%lx)"),
18714 (long) val);
252b5132 18715 }
252b5132 18716
14f72d45 18717 val = mips16_pcrel_val (fragp, pcrel_op, val, 0);
252b5132
RH
18718
18719 /* Make sure the section winds up with the alignment we have
18720 assumed. */
3ccad066
RS
18721 if (operand->shift > 0)
18722 record_alignment (asec, operand->shift);
252b5132
RH
18723 }
18724
8507b6e7
MR
18725 if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
18726 || RELAX_MIPS16_DSLOT (fragp->fr_subtype))
18727 {
18728 if (mac)
18729 as_warn_where (fragp->fr_file, fragp->fr_line,
18730 _("macro instruction expanded into multiple "
18731 "instructions in a branch delay slot"));
18732 else if (ext)
18733 as_warn_where (fragp->fr_file, fragp->fr_line,
18734 _("extended instruction in a branch delay slot"));
18735 }
18736 else if (RELAX_MIPS16_NOMACRO (fragp->fr_subtype) && mac)
252b5132 18737 as_warn_where (fragp->fr_file, fragp->fr_line,
8507b6e7
MR
18738 _("macro instruction expanded into multiple "
18739 "instructions"));
252b5132 18740
5c04167a 18741 buf = fragp->fr_literal + fragp->fr_fix;
252b5132 18742
4d68580a 18743 insn = read_compressed_insn (buf, 2);
5c04167a
RS
18744 if (ext)
18745 insn |= MIPS16_EXTEND;
252b5132 18746
5c04167a
RS
18747 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
18748 user_length = 4;
18749 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
18750 user_length = 2;
18751 else
18752 user_length = 0;
18753
8507b6e7 18754 if (mac)
c9775dde 18755 {
8507b6e7
MR
18756 unsigned long reg;
18757 unsigned long new;
18758 unsigned long op;
25499ac7 18759 bfd_boolean e2;
8507b6e7
MR
18760
18761 gas_assert (type == 'A' || type == 'B' || type == 'E');
18762 gas_assert (RELAX_MIPS16_SYM32 (fragp->fr_subtype));
c9775dde 18763
25499ac7
MR
18764 e2 = RELAX_MIPS16_E2 (fragp->fr_subtype);
18765
8507b6e7 18766 if (need_reloc)
c9775dde 18767 {
8507b6e7
MR
18768 fixS *fixp;
18769
18770 gas_assert (!RELAX_MIPS16_PIC (fragp->fr_subtype));
18771
18772 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18773 fragp->fr_symbol, fragp->fr_offset,
18774 FALSE, BFD_RELOC_MIPS16_HI16_S);
18775 fixp->fx_file = fragp->fr_file;
18776 fixp->fx_line = fragp->fr_line;
18777
25499ac7 18778 fixp = fix_new (fragp, buf - fragp->fr_literal + (e2 ? 4 : 8), 4,
8507b6e7
MR
18779 fragp->fr_symbol, fragp->fr_offset,
18780 FALSE, BFD_RELOC_MIPS16_LO16);
18781 fixp->fx_file = fragp->fr_file;
18782 fixp->fx_line = fragp->fr_line;
18783
18784 val = 0;
18785 }
18786
18787 switch (insn & 0xf800)
18788 {
18789 case 0x0800: /* ADDIU */
18790 reg = (insn >> 8) & 0x7;
18791 op = 0xf0004800 | (reg << 8);
c9775dde 18792 break;
8507b6e7
MR
18793 case 0xb000: /* LW */
18794 reg = (insn >> 8) & 0x7;
18795 op = 0xf0009800 | (reg << 8) | (reg << 5);
c9775dde 18796 break;
8507b6e7
MR
18797 case 0xf800: /* I64 */
18798 reg = (insn >> 5) & 0x7;
18799 switch (insn & 0x0700)
18800 {
18801 case 0x0400: /* LD */
18802 op = 0xf0003800 | (reg << 8) | (reg << 5);
18803 break;
18804 case 0x0600: /* DADDIU */
18805 op = 0xf000fd00 | (reg << 5);
18806 break;
18807 default:
18808 abort ();
18809 }
18810 break;
18811 default:
18812 abort ();
c9775dde 18813 }
8507b6e7 18814
25499ac7 18815 new = (e2 ? 0xf0006820 : 0xf0006800) | (reg << 8); /* LUI/LI */
8507b6e7
MR
18816 new |= mips16_immed_extend ((val + 0x8000) >> 16, 16);
18817 buf = write_compressed_insn (buf, new, 4);
25499ac7
MR
18818 if (!e2)
18819 {
18820 new = 0xf4003000 | (reg << 8) | (reg << 5); /* SLL */
18821 buf = write_compressed_insn (buf, new, 4);
18822 }
8507b6e7
MR
18823 op |= mips16_immed_extend (val, 16);
18824 buf = write_compressed_insn (buf, op, 4);
18825
25499ac7 18826 fragp->fr_fix += e2 ? 8 : 12;
8507b6e7
MR
18827 }
18828 else
18829 {
18830 unsigned int length = ext ? 4 : 2;
18831
18832 if (need_reloc)
c9775dde 18833 {
8507b6e7 18834 bfd_reloc_code_real_type reloc = BFD_RELOC_NONE;
8507b6e7 18835 fixS *fixp;
c9775dde 18836
8507b6e7
MR
18837 switch (type)
18838 {
18839 case 'p':
18840 case 'q':
18841 reloc = BFD_RELOC_MIPS16_16_PCREL_S1;
18842 break;
18843 default:
18844 break;
18845 }
18846 if (mac || reloc == BFD_RELOC_NONE)
18847 as_bad_where (fragp->fr_file, fragp->fr_line,
18848 _("unsupported relocation"));
18849 else if (ext)
18850 {
bbd27b76
MR
18851 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18852 fragp->fr_symbol, fragp->fr_offset,
18853 TRUE, reloc);
8507b6e7
MR
18854 fixp->fx_file = fragp->fr_file;
18855 fixp->fx_line = fragp->fr_line;
18856 }
18857 else
18858 as_bad_where (fragp->fr_file, fragp->fr_line,
18859 _("invalid unextended operand value"));
c9775dde 18860 }
eefc3365 18861 else
8507b6e7
MR
18862 mips16_immed (fragp->fr_file, fragp->fr_line, type,
18863 BFD_RELOC_UNUSED, val, user_length, &insn);
252b5132 18864
8507b6e7
MR
18865 gas_assert (mips16_opcode_length (insn) == length);
18866 write_compressed_insn (buf, insn, length);
18867 fragp->fr_fix += length;
18868 }
252b5132
RH
18869 }
18870 else
18871 {
df58fc94
RS
18872 relax_substateT subtype = fragp->fr_subtype;
18873 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
18874 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
4d7206a2
RS
18875 int first, second;
18876 fixS *fixp;
252b5132 18877
df58fc94
RS
18878 first = RELAX_FIRST (subtype);
18879 second = RELAX_SECOND (subtype);
4d7206a2 18880 fixp = (fixS *) fragp->fr_opcode;
252b5132 18881
df58fc94
RS
18882 /* If the delay slot chosen does not match the size of the instruction,
18883 then emit a warning. */
18884 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
18885 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
18886 {
18887 relax_substateT s;
18888 const char *msg;
18889
18890 s = subtype & (RELAX_DELAY_SLOT_16BIT
18891 | RELAX_DELAY_SLOT_SIZE_FIRST
18892 | RELAX_DELAY_SLOT_SIZE_SECOND);
18893 msg = macro_warning (s);
18894 if (msg != NULL)
db9b2be4 18895 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94
RS
18896 subtype &= ~s;
18897 }
18898
584892a6 18899 /* Possibly emit a warning if we've chosen the longer option. */
df58fc94 18900 if (use_second == second_longer)
584892a6 18901 {
df58fc94
RS
18902 relax_substateT s;
18903 const char *msg;
18904
18905 s = (subtype
18906 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
18907 msg = macro_warning (s);
18908 if (msg != NULL)
db9b2be4 18909 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94 18910 subtype &= ~s;
584892a6
RS
18911 }
18912
4d7206a2
RS
18913 /* Go through all the fixups for the first sequence. Disable them
18914 (by marking them as done) if we're going to use the second
18915 sequence instead. */
18916 while (fixp
18917 && fixp->fx_frag == fragp
18918 && fixp->fx_where < fragp->fr_fix - second)
18919 {
df58fc94 18920 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18921 fixp->fx_done = 1;
18922 fixp = fixp->fx_next;
18923 }
252b5132 18924
4d7206a2
RS
18925 /* Go through the fixups for the second sequence. Disable them if
18926 we're going to use the first sequence, otherwise adjust their
18927 addresses to account for the relaxation. */
18928 while (fixp && fixp->fx_frag == fragp)
18929 {
df58fc94 18930 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18931 fixp->fx_where -= first;
18932 else
18933 fixp->fx_done = 1;
18934 fixp = fixp->fx_next;
18935 }
18936
18937 /* Now modify the frag contents. */
df58fc94 18938 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18939 {
18940 char *start;
18941
18942 start = fragp->fr_literal + fragp->fr_fix - first - second;
18943 memmove (start, start + first, second);
18944 fragp->fr_fix -= first;
18945 }
18946 else
18947 fragp->fr_fix -= second;
252b5132
RH
18948 }
18949}
18950
252b5132
RH
18951/* This function is called after the relocs have been generated.
18952 We've been storing mips16 text labels as odd. Here we convert them
18953 back to even for the convenience of the debugger. */
18954
18955void
17a2f251 18956mips_frob_file_after_relocs (void)
252b5132
RH
18957{
18958 asymbol **syms;
18959 unsigned int count, i;
18960
252b5132
RH
18961 syms = bfd_get_outsymbols (stdoutput);
18962 count = bfd_get_symcount (stdoutput);
18963 for (i = 0; i < count; i++, syms++)
df58fc94
RS
18964 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
18965 && ((*syms)->value & 1) != 0)
18966 {
18967 (*syms)->value &= ~1;
18968 /* If the symbol has an odd size, it was probably computed
18969 incorrectly, so adjust that as well. */
18970 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
18971 ++elf_symbol (*syms)->internal_elf_sym.st_size;
18972 }
252b5132
RH
18973}
18974
a1facbec
MR
18975/* This function is called whenever a label is defined, including fake
18976 labels instantiated off the dot special symbol. It is used when
18977 handling branch delays; if a branch has a label, we assume we cannot
18978 move it. This also bumps the value of the symbol by 1 in compressed
18979 code. */
252b5132 18980
e1b47bd5 18981static void
a1facbec 18982mips_record_label (symbolS *sym)
252b5132 18983{
a8dbcb85 18984 segment_info_type *si = seg_info (now_seg);
252b5132
RH
18985 struct insn_label_list *l;
18986
18987 if (free_insn_labels == NULL)
325801bd 18988 l = XNEW (struct insn_label_list);
252b5132
RH
18989 else
18990 {
18991 l = free_insn_labels;
18992 free_insn_labels = l->next;
18993 }
18994
18995 l->label = sym;
a8dbcb85
TS
18996 l->next = si->label_list;
18997 si->label_list = l;
a1facbec 18998}
07a53e5c 18999
a1facbec
MR
19000/* This function is called as tc_frob_label() whenever a label is defined
19001 and adds a DWARF-2 record we only want for true labels. */
19002
19003void
19004mips_define_label (symbolS *sym)
19005{
19006 mips_record_label (sym);
07a53e5c 19007 dwarf2_emit_label (sym);
252b5132 19008}
e1b47bd5
RS
19009
19010/* This function is called by tc_new_dot_label whenever a new dot symbol
19011 is defined. */
19012
19013void
19014mips_add_dot_label (symbolS *sym)
19015{
19016 mips_record_label (sym);
19017 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
19018 mips_compressed_mark_label (sym);
19019}
252b5132 19020\f
351cdf24
MF
19021/* Converting ASE flags from internal to .MIPS.abiflags values. */
19022static unsigned int
19023mips_convert_ase_flags (int ase)
19024{
19025 unsigned int ext_ases = 0;
19026
19027 if (ase & ASE_DSP)
19028 ext_ases |= AFL_ASE_DSP;
19029 if (ase & ASE_DSPR2)
19030 ext_ases |= AFL_ASE_DSPR2;
8f4f9071
MF
19031 if (ase & ASE_DSPR3)
19032 ext_ases |= AFL_ASE_DSPR3;
351cdf24
MF
19033 if (ase & ASE_EVA)
19034 ext_ases |= AFL_ASE_EVA;
19035 if (ase & ASE_MCU)
19036 ext_ases |= AFL_ASE_MCU;
19037 if (ase & ASE_MDMX)
19038 ext_ases |= AFL_ASE_MDMX;
19039 if (ase & ASE_MIPS3D)
19040 ext_ases |= AFL_ASE_MIPS3D;
19041 if (ase & ASE_MT)
19042 ext_ases |= AFL_ASE_MT;
19043 if (ase & ASE_SMARTMIPS)
19044 ext_ases |= AFL_ASE_SMARTMIPS;
19045 if (ase & ASE_VIRT)
19046 ext_ases |= AFL_ASE_VIRT;
19047 if (ase & ASE_MSA)
19048 ext_ases |= AFL_ASE_MSA;
19049 if (ase & ASE_XPA)
19050 ext_ases |= AFL_ASE_XPA;
25499ac7
MR
19051 if (ase & ASE_MIPS16E2)
19052 ext_ases |= file_ase_mips16 ? AFL_ASE_MIPS16E2 : 0;
730c3174
SE
19053 if (ase & ASE_CRC)
19054 ext_ases |= AFL_ASE_CRC;
6f20c942
FS
19055 if (ase & ASE_GINV)
19056 ext_ases |= AFL_ASE_GINV;
8095d2f7
CX
19057 if (ase & ASE_LOONGSON_MMI)
19058 ext_ases |= AFL_ASE_LOONGSON_MMI;
716c08de
CX
19059 if (ase & ASE_LOONGSON_CAM)
19060 ext_ases |= AFL_ASE_LOONGSON_CAM;
bdc6c06e
CX
19061 if (ase & ASE_LOONGSON_EXT)
19062 ext_ases |= AFL_ASE_LOONGSON_EXT;
a693765e
CX
19063 if (ase & ASE_LOONGSON_EXT2)
19064 ext_ases |= AFL_ASE_LOONGSON_EXT2;
351cdf24
MF
19065
19066 return ext_ases;
19067}
252b5132
RH
19068/* Some special processing for a MIPS ELF file. */
19069
19070void
17a2f251 19071mips_elf_final_processing (void)
252b5132 19072{
351cdf24
MF
19073 int fpabi;
19074 Elf_Internal_ABIFlags_v0 flags;
19075
19076 flags.version = 0;
19077 flags.isa_rev = 0;
19078 switch (file_mips_opts.isa)
19079 {
19080 case INSN_ISA1:
19081 flags.isa_level = 1;
19082 break;
19083 case INSN_ISA2:
19084 flags.isa_level = 2;
19085 break;
19086 case INSN_ISA3:
19087 flags.isa_level = 3;
19088 break;
19089 case INSN_ISA4:
19090 flags.isa_level = 4;
19091 break;
19092 case INSN_ISA5:
19093 flags.isa_level = 5;
19094 break;
19095 case INSN_ISA32:
19096 flags.isa_level = 32;
19097 flags.isa_rev = 1;
19098 break;
19099 case INSN_ISA32R2:
19100 flags.isa_level = 32;
19101 flags.isa_rev = 2;
19102 break;
19103 case INSN_ISA32R3:
19104 flags.isa_level = 32;
19105 flags.isa_rev = 3;
19106 break;
19107 case INSN_ISA32R5:
19108 flags.isa_level = 32;
19109 flags.isa_rev = 5;
19110 break;
09c14161
MF
19111 case INSN_ISA32R6:
19112 flags.isa_level = 32;
19113 flags.isa_rev = 6;
19114 break;
351cdf24
MF
19115 case INSN_ISA64:
19116 flags.isa_level = 64;
19117 flags.isa_rev = 1;
19118 break;
19119 case INSN_ISA64R2:
19120 flags.isa_level = 64;
19121 flags.isa_rev = 2;
19122 break;
19123 case INSN_ISA64R3:
19124 flags.isa_level = 64;
19125 flags.isa_rev = 3;
19126 break;
19127 case INSN_ISA64R5:
19128 flags.isa_level = 64;
19129 flags.isa_rev = 5;
19130 break;
09c14161
MF
19131 case INSN_ISA64R6:
19132 flags.isa_level = 64;
19133 flags.isa_rev = 6;
19134 break;
351cdf24
MF
19135 }
19136
19137 flags.gpr_size = file_mips_opts.gp == 32 ? AFL_REG_32 : AFL_REG_64;
19138 flags.cpr1_size = file_mips_opts.soft_float ? AFL_REG_NONE
19139 : (file_mips_opts.ase & ASE_MSA) ? AFL_REG_128
19140 : (file_mips_opts.fp == 64) ? AFL_REG_64
19141 : AFL_REG_32;
19142 flags.cpr2_size = AFL_REG_NONE;
19143 flags.fp_abi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19144 Tag_GNU_MIPS_ABI_FP);
19145 flags.isa_ext = bfd_mips_isa_ext (stdoutput);
19146 flags.ases = mips_convert_ase_flags (file_mips_opts.ase);
19147 if (file_ase_mips16)
19148 flags.ases |= AFL_ASE_MIPS16;
19149 if (file_ase_micromips)
19150 flags.ases |= AFL_ASE_MICROMIPS;
19151 flags.flags1 = 0;
19152 if ((ISA_HAS_ODD_SINGLE_FPR (file_mips_opts.isa, file_mips_opts.arch)
19153 || file_mips_opts.fp == 64)
19154 && file_mips_opts.oddspreg)
19155 flags.flags1 |= AFL_FLAGS1_ODDSPREG;
19156 flags.flags2 = 0;
19157
19158 bfd_mips_elf_swap_abiflags_v0_out (stdoutput, &flags,
19159 ((Elf_External_ABIFlags_v0 *)
19160 mips_flags_frag));
19161
252b5132 19162 /* Write out the register information. */
316f5878 19163 if (mips_abi != N64_ABI)
252b5132
RH
19164 {
19165 Elf32_RegInfo s;
19166
19167 s.ri_gprmask = mips_gprmask;
19168 s.ri_cprmask[0] = mips_cprmask[0];
19169 s.ri_cprmask[1] = mips_cprmask[1];
19170 s.ri_cprmask[2] = mips_cprmask[2];
19171 s.ri_cprmask[3] = mips_cprmask[3];
19172 /* The gp_value field is set by the MIPS ELF backend. */
19173
19174 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
19175 ((Elf32_External_RegInfo *)
19176 mips_regmask_frag));
19177 }
19178 else
19179 {
19180 Elf64_Internal_RegInfo s;
19181
19182 s.ri_gprmask = mips_gprmask;
19183 s.ri_pad = 0;
19184 s.ri_cprmask[0] = mips_cprmask[0];
19185 s.ri_cprmask[1] = mips_cprmask[1];
19186 s.ri_cprmask[2] = mips_cprmask[2];
19187 s.ri_cprmask[3] = mips_cprmask[3];
19188 /* The gp_value field is set by the MIPS ELF backend. */
19189
19190 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
19191 ((Elf64_External_RegInfo *)
19192 mips_regmask_frag));
19193 }
19194
19195 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
19196 sort of BFD interface for this. */
19197 if (mips_any_noreorder)
19198 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
19199 if (mips_pic != NO_PIC)
143d77c5 19200 {
8b828383 19201 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
19202 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
19203 }
19204 if (mips_abicalls)
19205 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 19206
b015e599
AP
19207 /* Set MIPS ELF flags for ASEs. Note that not all ASEs have flags
19208 defined at present; this might need to change in future. */
a4672219
TS
19209 if (file_ase_mips16)
19210 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
df58fc94
RS
19211 if (file_ase_micromips)
19212 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
919731af 19213 if (file_mips_opts.ase & ASE_MDMX)
deec1734 19214 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 19215
bdaaa2e1 19216 /* Set the MIPS ELF ABI flags. */
316f5878 19217 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 19218 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 19219 else if (mips_abi == O64_ABI)
252b5132 19220 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 19221 else if (mips_abi == EABI_ABI)
252b5132 19222 {
bad1aba3 19223 if (file_mips_opts.gp == 64)
252b5132
RH
19224 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
19225 else
19226 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
19227 }
be00bddd 19228
defc8e2b 19229 /* Nothing to do for N32_ABI or N64_ABI. */
252b5132
RH
19230
19231 if (mips_32bitmode)
19232 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
ad3fea08 19233
7361da2c 19234 if (mips_nan2008 == 1)
ba92f887
MR
19235 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NAN2008;
19236
ad3fea08 19237 /* 32 bit code with 64 bit FP registers. */
351cdf24
MF
19238 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19239 Tag_GNU_MIPS_ABI_FP);
19240 if (fpabi == Val_GNU_MIPS_ABI_FP_OLD_64)
f1c38003 19241 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_FP64;
252b5132 19242}
252b5132 19243\f
beae10d5 19244typedef struct proc {
9b2f1d35
EC
19245 symbolS *func_sym;
19246 symbolS *func_end_sym;
beae10d5
KH
19247 unsigned long reg_mask;
19248 unsigned long reg_offset;
19249 unsigned long fpreg_mask;
19250 unsigned long fpreg_offset;
19251 unsigned long frame_offset;
19252 unsigned long frame_reg;
19253 unsigned long pc_reg;
19254} procS;
252b5132
RH
19255
19256static procS cur_proc;
19257static procS *cur_proc_ptr;
19258static int numprocs;
19259
df58fc94
RS
19260/* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
19261 as "2", and a normal nop as "0". */
19262
19263#define NOP_OPCODE_MIPS 0
19264#define NOP_OPCODE_MIPS16 1
19265#define NOP_OPCODE_MICROMIPS 2
742a56fe
RS
19266
19267char
19268mips_nop_opcode (void)
19269{
df58fc94
RS
19270 if (seg_info (now_seg)->tc_segment_info_data.micromips)
19271 return NOP_OPCODE_MICROMIPS;
19272 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
19273 return NOP_OPCODE_MIPS16;
19274 else
19275 return NOP_OPCODE_MIPS;
742a56fe
RS
19276}
19277
df58fc94
RS
19278/* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
19279 32-bit microMIPS NOPs here (if applicable). */
a19d8eb0 19280
0a9ef439 19281void
17a2f251 19282mips_handle_align (fragS *fragp)
a19d8eb0 19283{
df58fc94 19284 char nop_opcode;
742a56fe 19285 char *p;
c67a084a
NC
19286 int bytes, size, excess;
19287 valueT opcode;
742a56fe 19288
0a9ef439
RH
19289 if (fragp->fr_type != rs_align_code)
19290 return;
19291
742a56fe 19292 p = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
19293 nop_opcode = *p;
19294 switch (nop_opcode)
a19d8eb0 19295 {
df58fc94
RS
19296 case NOP_OPCODE_MICROMIPS:
19297 opcode = micromips_nop32_insn.insn_opcode;
19298 size = 4;
19299 break;
19300 case NOP_OPCODE_MIPS16:
c67a084a
NC
19301 opcode = mips16_nop_insn.insn_opcode;
19302 size = 2;
df58fc94
RS
19303 break;
19304 case NOP_OPCODE_MIPS:
19305 default:
c67a084a
NC
19306 opcode = nop_insn.insn_opcode;
19307 size = 4;
df58fc94 19308 break;
c67a084a 19309 }
a19d8eb0 19310
c67a084a
NC
19311 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
19312 excess = bytes % size;
df58fc94
RS
19313
19314 /* Handle the leading part if we're not inserting a whole number of
19315 instructions, and make it the end of the fixed part of the frag.
19316 Try to fit in a short microMIPS NOP if applicable and possible,
19317 and use zeroes otherwise. */
19318 gas_assert (excess < 4);
19319 fragp->fr_fix += excess;
19320 switch (excess)
c67a084a 19321 {
df58fc94
RS
19322 case 3:
19323 *p++ = '\0';
19324 /* Fall through. */
19325 case 2:
833794fc 19326 if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
df58fc94 19327 {
4d68580a 19328 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
df58fc94
RS
19329 break;
19330 }
19331 *p++ = '\0';
19332 /* Fall through. */
19333 case 1:
19334 *p++ = '\0';
19335 /* Fall through. */
19336 case 0:
19337 break;
a19d8eb0 19338 }
c67a084a
NC
19339
19340 md_number_to_chars (p, opcode, size);
19341 fragp->fr_var = size;
a19d8eb0
CP
19342}
19343
252b5132 19344static long
17a2f251 19345get_number (void)
252b5132
RH
19346{
19347 int negative = 0;
19348 long val = 0;
19349
19350 if (*input_line_pointer == '-')
19351 {
19352 ++input_line_pointer;
19353 negative = 1;
19354 }
3882b010 19355 if (!ISDIGIT (*input_line_pointer))
956cd1d6 19356 as_bad (_("expected simple number"));
252b5132
RH
19357 if (input_line_pointer[0] == '0')
19358 {
19359 if (input_line_pointer[1] == 'x')
19360 {
19361 input_line_pointer += 2;
3882b010 19362 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
19363 {
19364 val <<= 4;
19365 val |= hex_value (*input_line_pointer++);
19366 }
19367 return negative ? -val : val;
19368 }
19369 else
19370 {
19371 ++input_line_pointer;
3882b010 19372 while (ISDIGIT (*input_line_pointer))
252b5132
RH
19373 {
19374 val <<= 3;
19375 val |= *input_line_pointer++ - '0';
19376 }
19377 return negative ? -val : val;
19378 }
19379 }
3882b010 19380 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
19381 {
19382 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
19383 *input_line_pointer, *input_line_pointer);
956cd1d6 19384 as_warn (_("invalid number"));
252b5132
RH
19385 return -1;
19386 }
3882b010 19387 while (ISDIGIT (*input_line_pointer))
252b5132
RH
19388 {
19389 val *= 10;
19390 val += *input_line_pointer++ - '0';
19391 }
19392 return negative ? -val : val;
19393}
19394
19395/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
19396 is an initial number which is the ECOFF file index. In the non-ECOFF
19397 case .file implies DWARF-2. */
19398
19399static void
17a2f251 19400s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 19401{
ecb4347a
DJ
19402 static int first_file_directive = 0;
19403
c5dd6aab
DJ
19404 if (ECOFF_DEBUGGING)
19405 {
19406 get_number ();
19407 s_app_file (0);
19408 }
19409 else
ecb4347a
DJ
19410 {
19411 char *filename;
19412
68d20676 19413 filename = dwarf2_directive_filename ();
ecb4347a
DJ
19414
19415 /* Versions of GCC up to 3.1 start files with a ".file"
19416 directive even for stabs output. Make sure that this
19417 ".file" is handled. Note that you need a version of GCC
19418 after 3.1 in order to support DWARF-2 on MIPS. */
19419 if (filename != NULL && ! first_file_directive)
19420 {
19421 (void) new_logical_line (filename, -1);
c04f5787 19422 s_app_file_string (filename, 0);
ecb4347a
DJ
19423 }
19424 first_file_directive = 1;
19425 }
c5dd6aab
DJ
19426}
19427
19428/* The .loc directive, implying DWARF-2. */
252b5132
RH
19429
19430static void
17a2f251 19431s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 19432{
c5dd6aab
DJ
19433 if (!ECOFF_DEBUGGING)
19434 dwarf2_directive_loc (0);
252b5132
RH
19435}
19436
252b5132
RH
19437/* The .end directive. */
19438
19439static void
17a2f251 19440s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
19441{
19442 symbolS *p;
252b5132 19443
7a621144
DJ
19444 /* Following functions need their own .frame and .cprestore directives. */
19445 mips_frame_reg_valid = 0;
19446 mips_cprestore_valid = 0;
19447
252b5132
RH
19448 if (!is_end_of_line[(unsigned char) *input_line_pointer])
19449 {
19450 p = get_symbol ();
19451 demand_empty_rest_of_line ();
19452 }
19453 else
19454 p = NULL;
19455
14949570 19456 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
19457 as_warn (_(".end not in text section"));
19458
19459 if (!cur_proc_ptr)
19460 {
1661c76c 19461 as_warn (_(".end directive without a preceding .ent directive"));
252b5132
RH
19462 demand_empty_rest_of_line ();
19463 return;
19464 }
19465
19466 if (p != NULL)
19467 {
9c2799c2 19468 gas_assert (S_GET_NAME (p));
9b2f1d35 19469 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
1661c76c 19470 as_warn (_(".end symbol does not match .ent symbol"));
ecb4347a
DJ
19471
19472 if (debug_type == DEBUG_STABS)
19473 stabs_generate_asm_endfunc (S_GET_NAME (p),
19474 S_GET_NAME (p));
252b5132
RH
19475 }
19476 else
19477 as_warn (_(".end directive missing or unknown symbol"));
19478
9b2f1d35
EC
19479 /* Create an expression to calculate the size of the function. */
19480 if (p && cur_proc_ptr)
19481 {
19482 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
325801bd 19483 expressionS *exp = XNEW (expressionS);
9b2f1d35
EC
19484
19485 obj->size = exp;
19486 exp->X_op = O_subtract;
19487 exp->X_add_symbol = symbol_temp_new_now ();
19488 exp->X_op_symbol = p;
19489 exp->X_add_number = 0;
19490
19491 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
19492 }
19493
5ff6a06c
MR
19494#ifdef md_flush_pending_output
19495 md_flush_pending_output ();
19496#endif
19497
ecb4347a 19498 /* Generate a .pdr section. */
f3ded42a 19499 if (!ECOFF_DEBUGGING && mips_flag_pdr)
ecb4347a
DJ
19500 {
19501 segT saved_seg = now_seg;
19502 subsegT saved_subseg = now_subseg;
ecb4347a
DJ
19503 expressionS exp;
19504 char *fragp;
252b5132 19505
9c2799c2 19506 gas_assert (pdr_seg);
ecb4347a 19507 subseg_set (pdr_seg, 0);
252b5132 19508
ecb4347a
DJ
19509 /* Write the symbol. */
19510 exp.X_op = O_symbol;
19511 exp.X_add_symbol = p;
19512 exp.X_add_number = 0;
19513 emit_expr (&exp, 4);
252b5132 19514
ecb4347a 19515 fragp = frag_more (7 * 4);
252b5132 19516
17a2f251
TS
19517 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19518 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19519 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19520 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19521 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19522 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19523 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 19524
ecb4347a
DJ
19525 subseg_set (saved_seg, saved_subseg);
19526 }
252b5132
RH
19527
19528 cur_proc_ptr = NULL;
19529}
19530
19531/* The .aent and .ent directives. */
19532
19533static void
17a2f251 19534s_mips_ent (int aent)
252b5132 19535{
252b5132 19536 symbolS *symbolP;
252b5132
RH
19537
19538 symbolP = get_symbol ();
19539 if (*input_line_pointer == ',')
f9419b05 19540 ++input_line_pointer;
252b5132 19541 SKIP_WHITESPACE ();
3882b010 19542 if (ISDIGIT (*input_line_pointer)
d9a62219 19543 || *input_line_pointer == '-')
874e8986 19544 get_number ();
252b5132 19545
14949570 19546 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
1661c76c 19547 as_warn (_(".ent or .aent not in text section"));
252b5132
RH
19548
19549 if (!aent && cur_proc_ptr)
9a41af64 19550 as_warn (_("missing .end"));
252b5132
RH
19551
19552 if (!aent)
19553 {
7a621144
DJ
19554 /* This function needs its own .frame and .cprestore directives. */
19555 mips_frame_reg_valid = 0;
19556 mips_cprestore_valid = 0;
19557
252b5132
RH
19558 cur_proc_ptr = &cur_proc;
19559 memset (cur_proc_ptr, '\0', sizeof (procS));
19560
9b2f1d35 19561 cur_proc_ptr->func_sym = symbolP;
252b5132 19562
f9419b05 19563 ++numprocs;
ecb4347a
DJ
19564
19565 if (debug_type == DEBUG_STABS)
19566 stabs_generate_asm_func (S_GET_NAME (symbolP),
19567 S_GET_NAME (symbolP));
252b5132
RH
19568 }
19569
7c0fc524
MR
19570 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19571
252b5132
RH
19572 demand_empty_rest_of_line ();
19573}
19574
19575/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 19576 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 19577 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 19578 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
19579 symbol table (in the mdebug section). */
19580
19581static void
17a2f251 19582s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 19583{
f3ded42a
RS
19584 if (ECOFF_DEBUGGING)
19585 s_ignore (ignore);
19586 else
ecb4347a
DJ
19587 {
19588 long val;
252b5132 19589
ecb4347a
DJ
19590 if (cur_proc_ptr == (procS *) NULL)
19591 {
19592 as_warn (_(".frame outside of .ent"));
19593 demand_empty_rest_of_line ();
19594 return;
19595 }
252b5132 19596
ecb4347a
DJ
19597 cur_proc_ptr->frame_reg = tc_get_register (1);
19598
19599 SKIP_WHITESPACE ();
19600 if (*input_line_pointer++ != ','
19601 || get_absolute_expression_and_terminator (&val) != ',')
19602 {
1661c76c 19603 as_warn (_("bad .frame directive"));
ecb4347a
DJ
19604 --input_line_pointer;
19605 demand_empty_rest_of_line ();
19606 return;
19607 }
252b5132 19608
ecb4347a
DJ
19609 cur_proc_ptr->frame_offset = val;
19610 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 19611
252b5132 19612 demand_empty_rest_of_line ();
252b5132 19613 }
252b5132
RH
19614}
19615
bdaaa2e1
KH
19616/* The .fmask and .mask directives. If the mdebug section is present
19617 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 19618 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 19619 information correctly. We can't use the ecoff routines because they
252b5132
RH
19620 make reference to the ecoff symbol table (in the mdebug section). */
19621
19622static void
17a2f251 19623s_mips_mask (int reg_type)
252b5132 19624{
f3ded42a
RS
19625 if (ECOFF_DEBUGGING)
19626 s_ignore (reg_type);
19627 else
252b5132 19628 {
ecb4347a 19629 long mask, off;
252b5132 19630
ecb4347a
DJ
19631 if (cur_proc_ptr == (procS *) NULL)
19632 {
19633 as_warn (_(".mask/.fmask outside of .ent"));
19634 demand_empty_rest_of_line ();
19635 return;
19636 }
252b5132 19637
ecb4347a
DJ
19638 if (get_absolute_expression_and_terminator (&mask) != ',')
19639 {
1661c76c 19640 as_warn (_("bad .mask/.fmask directive"));
ecb4347a
DJ
19641 --input_line_pointer;
19642 demand_empty_rest_of_line ();
19643 return;
19644 }
252b5132 19645
ecb4347a
DJ
19646 off = get_absolute_expression ();
19647
19648 if (reg_type == 'F')
19649 {
19650 cur_proc_ptr->fpreg_mask = mask;
19651 cur_proc_ptr->fpreg_offset = off;
19652 }
19653 else
19654 {
19655 cur_proc_ptr->reg_mask = mask;
19656 cur_proc_ptr->reg_offset = off;
19657 }
19658
19659 demand_empty_rest_of_line ();
252b5132 19660 }
252b5132
RH
19661}
19662
316f5878
RS
19663/* A table describing all the processors gas knows about. Names are
19664 matched in the order listed.
e7af610e 19665
316f5878
RS
19666 To ease comparison, please keep this table in the same order as
19667 gcc's mips_cpu_info_table[]. */
e972090a
NC
19668static const struct mips_cpu_info mips_cpu_info_table[] =
19669{
316f5878 19670 /* Entries for generic ISAs */
d16afab6
RS
19671 { "mips1", MIPS_CPU_IS_ISA, 0, ISA_MIPS1, CPU_R3000 },
19672 { "mips2", MIPS_CPU_IS_ISA, 0, ISA_MIPS2, CPU_R6000 },
19673 { "mips3", MIPS_CPU_IS_ISA, 0, ISA_MIPS3, CPU_R4000 },
19674 { "mips4", MIPS_CPU_IS_ISA, 0, ISA_MIPS4, CPU_R8000 },
19675 { "mips5", MIPS_CPU_IS_ISA, 0, ISA_MIPS5, CPU_MIPS5 },
19676 { "mips32", MIPS_CPU_IS_ISA, 0, ISA_MIPS32, CPU_MIPS32 },
19677 { "mips32r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
ae52f483
AB
19678 { "mips32r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R3, CPU_MIPS32R3 },
19679 { "mips32r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R5, CPU_MIPS32R5 },
7361da2c 19680 { "mips32r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R6, CPU_MIPS32R6 },
d16afab6
RS
19681 { "mips64", MIPS_CPU_IS_ISA, 0, ISA_MIPS64, CPU_MIPS64 },
19682 { "mips64r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R2, CPU_MIPS64R2 },
ae52f483
AB
19683 { "mips64r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R3, CPU_MIPS64R3 },
19684 { "mips64r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R5, CPU_MIPS64R5 },
7361da2c 19685 { "mips64r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R6, CPU_MIPS64R6 },
316f5878
RS
19686
19687 /* MIPS I */
d16afab6
RS
19688 { "r3000", 0, 0, ISA_MIPS1, CPU_R3000 },
19689 { "r2000", 0, 0, ISA_MIPS1, CPU_R3000 },
19690 { "r3900", 0, 0, ISA_MIPS1, CPU_R3900 },
316f5878
RS
19691
19692 /* MIPS II */
d16afab6 19693 { "r6000", 0, 0, ISA_MIPS2, CPU_R6000 },
316f5878
RS
19694
19695 /* MIPS III */
d16afab6
RS
19696 { "r4000", 0, 0, ISA_MIPS3, CPU_R4000 },
19697 { "r4010", 0, 0, ISA_MIPS2, CPU_R4010 },
19698 { "vr4100", 0, 0, ISA_MIPS3, CPU_VR4100 },
19699 { "vr4111", 0, 0, ISA_MIPS3, CPU_R4111 },
19700 { "vr4120", 0, 0, ISA_MIPS3, CPU_VR4120 },
19701 { "vr4130", 0, 0, ISA_MIPS3, CPU_VR4120 },
19702 { "vr4181", 0, 0, ISA_MIPS3, CPU_R4111 },
19703 { "vr4300", 0, 0, ISA_MIPS3, CPU_R4300 },
19704 { "r4400", 0, 0, ISA_MIPS3, CPU_R4400 },
19705 { "r4600", 0, 0, ISA_MIPS3, CPU_R4600 },
19706 { "orion", 0, 0, ISA_MIPS3, CPU_R4600 },
19707 { "r4650", 0, 0, ISA_MIPS3, CPU_R4650 },
19708 { "r5900", 0, 0, ISA_MIPS3, CPU_R5900 },
b15591bb 19709 /* ST Microelectronics Loongson 2E and 2F cores */
d16afab6 19710 { "loongson2e", 0, 0, ISA_MIPS3, CPU_LOONGSON_2E },
8095d2f7 19711 { "loongson2f", 0, ASE_LOONGSON_MMI, ISA_MIPS3, CPU_LOONGSON_2F },
316f5878
RS
19712
19713 /* MIPS IV */
d16afab6
RS
19714 { "r8000", 0, 0, ISA_MIPS4, CPU_R8000 },
19715 { "r10000", 0, 0, ISA_MIPS4, CPU_R10000 },
19716 { "r12000", 0, 0, ISA_MIPS4, CPU_R12000 },
19717 { "r14000", 0, 0, ISA_MIPS4, CPU_R14000 },
19718 { "r16000", 0, 0, ISA_MIPS4, CPU_R16000 },
19719 { "vr5000", 0, 0, ISA_MIPS4, CPU_R5000 },
19720 { "vr5400", 0, 0, ISA_MIPS4, CPU_VR5400 },
19721 { "vr5500", 0, 0, ISA_MIPS4, CPU_VR5500 },
19722 { "rm5200", 0, 0, ISA_MIPS4, CPU_R5000 },
19723 { "rm5230", 0, 0, ISA_MIPS4, CPU_R5000 },
19724 { "rm5231", 0, 0, ISA_MIPS4, CPU_R5000 },
19725 { "rm5261", 0, 0, ISA_MIPS4, CPU_R5000 },
19726 { "rm5721", 0, 0, ISA_MIPS4, CPU_R5000 },
19727 { "rm7000", 0, 0, ISA_MIPS4, CPU_RM7000 },
19728 { "rm9000", 0, 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
19729
19730 /* MIPS 32 */
d16afab6
RS
19731 { "4kc", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19732 { "4km", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19733 { "4kp", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19734 { "4ksc", 0, ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
ad3fea08
TS
19735
19736 /* MIPS 32 Release 2 */
d16afab6
RS
19737 { "4kec", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19738 { "4kem", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19739 { "4kep", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19740 { "4ksd", 0, ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
19741 { "m4k", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19742 { "m4kp", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19743 { "m14k", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19744 { "m14kc", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19745 { "m14ke", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19746 ISA_MIPS32R2, CPU_MIPS32R2 },
19747 { "m14kec", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19748 ISA_MIPS32R2, CPU_MIPS32R2 },
19749 { "24kc", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19750 { "24kf2_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19751 { "24kf", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19752 { "24kf1_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19753 /* Deprecated forms of the above. */
d16afab6
RS
19754 { "24kfx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19755 { "24kx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19756 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
d16afab6
RS
19757 { "24kec", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19758 { "24kef2_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19759 { "24kef", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19760 { "24kef1_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19761 /* Deprecated forms of the above. */
d16afab6
RS
19762 { "24kefx", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19763 { "24kex", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19764 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
d16afab6
RS
19765 { "34kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19766 { "34kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19767 { "34kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19768 { "34kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19769 /* Deprecated forms of the above. */
d16afab6
RS
19770 { "34kfx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19771 { "34kx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
711eefe4 19772 /* 34Kn is a 34kc without DSP. */
d16afab6 19773 { "34kn", 0, ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19774 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
d16afab6
RS
19775 { "74kc", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19776 { "74kf2_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19777 { "74kf", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19778 { "74kf1_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19779 { "74kf3_2", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19780 /* Deprecated forms of the above. */
d16afab6
RS
19781 { "74kfx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19782 { "74kx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
30f8113a 19783 /* 1004K cores are multiprocessor versions of the 34K. */
d16afab6
RS
19784 { "1004kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19785 { "1004kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19786 { "1004kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19787 { "1004kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
77403ce9
RS
19788 /* interaptiv is the new name for 1004kf */
19789 { "interaptiv", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
38bf472a
MR
19790 { "interaptiv-mr2", 0,
19791 ASE_DSP | ASE_EVA | ASE_MT | ASE_MIPS16E2 | ASE_MIPS16E2_MT,
19792 ISA_MIPS32R3, CPU_INTERAPTIV_MR2 },
c6e5c03a
RS
19793 /* M5100 family */
19794 { "m5100", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
19795 { "m5101", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
bbaa46c0 19796 /* P5600 with EVA and Virtualization ASEs, other ASEs are optional. */
134c0c8b 19797 { "p5600", 0, ASE_VIRT | ASE_EVA | ASE_XPA, ISA_MIPS32R5, CPU_MIPS32R5 },
32b26a03 19798
316f5878 19799 /* MIPS 64 */
d16afab6
RS
19800 { "5kc", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19801 { "5kf", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19802 { "20kc", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
19803 { "25kf", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
ad3fea08 19804
c7a23324 19805 /* Broadcom SB-1 CPU core */
d16afab6 19806 { "sb1", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
1e85aad8 19807 /* Broadcom SB-1A CPU core */
d16afab6 19808 { "sb1a", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
3739860c 19809
ed163775 19810 /* MIPS 64 Release 2 */
716c08de 19811 /* Loongson CPU core */
ac8cb70f 19812 /* -march=loongson3a is an alias of -march=gs464 for compatibility */
bdc6c06e 19813 { "loongson3a", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT,
ac8cb70f
CX
19814 ISA_MIPS64R2, CPU_GS464 },
19815 { "gs464", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT,
19816 ISA_MIPS64R2, CPU_GS464 },
ed163775 19817
967344c6 19818 /* Cavium Networks Octeon CPU core */
d16afab6
RS
19819 { "octeon", 0, 0, ISA_MIPS64R2, CPU_OCTEON },
19820 { "octeon+", 0, 0, ISA_MIPS64R2, CPU_OCTEONP },
19821 { "octeon2", 0, 0, ISA_MIPS64R2, CPU_OCTEON2 },
2c629856 19822 { "octeon3", 0, ASE_VIRT | ASE_VIRT64, ISA_MIPS64R5, CPU_OCTEON3 },
967344c6 19823
52b6b6b9 19824 /* RMI Xlr */
d16afab6 19825 { "xlr", 0, 0, ISA_MIPS64, CPU_XLR },
52b6b6b9 19826
55a36193
MK
19827 /* Broadcom XLP.
19828 XLP is mostly like XLR, with the prominent exception that it is
19829 MIPS64R2 rather than MIPS64. */
d16afab6 19830 { "xlp", 0, 0, ISA_MIPS64R2, CPU_XLR },
55a36193 19831
a4968f42 19832 /* MIPS 64 Release 6 */
7ef0d297 19833 { "i6400", 0, ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
a4968f42 19834 { "p6600", 0, ASE_VIRT | ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
7ef0d297 19835
316f5878 19836 /* End marker */
d16afab6 19837 { NULL, 0, 0, 0, 0 }
316f5878 19838};
e7af610e 19839
84ea6cf2 19840
316f5878
RS
19841/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
19842 with a final "000" replaced by "k". Ignore case.
e7af610e 19843
316f5878 19844 Note: this function is shared between GCC and GAS. */
c6c98b38 19845
b34976b6 19846static bfd_boolean
17a2f251 19847mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
19848{
19849 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
19850 given++, canonical++;
19851
19852 return ((*given == 0 && *canonical == 0)
19853 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
19854}
19855
19856
19857/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
19858 CPU name. We've traditionally allowed a lot of variation here.
19859
19860 Note: this function is shared between GCC and GAS. */
19861
b34976b6 19862static bfd_boolean
17a2f251 19863mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
19864{
19865 /* First see if the name matches exactly, or with a final "000"
19866 turned into "k". */
19867 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 19868 return TRUE;
316f5878
RS
19869
19870 /* If not, try comparing based on numerical designation alone.
19871 See if GIVEN is an unadorned number, or 'r' followed by a number. */
19872 if (TOLOWER (*given) == 'r')
19873 given++;
19874 if (!ISDIGIT (*given))
b34976b6 19875 return FALSE;
316f5878
RS
19876
19877 /* Skip over some well-known prefixes in the canonical name,
19878 hoping to find a number there too. */
19879 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
19880 canonical += 2;
19881 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
19882 canonical += 2;
19883 else if (TOLOWER (canonical[0]) == 'r')
19884 canonical += 1;
19885
19886 return mips_strict_matching_cpu_name_p (canonical, given);
19887}
19888
19889
19890/* Parse an option that takes the name of a processor as its argument.
19891 OPTION is the name of the option and CPU_STRING is the argument.
19892 Return the corresponding processor enumeration if the CPU_STRING is
19893 recognized, otherwise report an error and return null.
19894
19895 A similar function exists in GCC. */
e7af610e
NC
19896
19897static const struct mips_cpu_info *
17a2f251 19898mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 19899{
316f5878 19900 const struct mips_cpu_info *p;
e7af610e 19901
316f5878
RS
19902 /* 'from-abi' selects the most compatible architecture for the given
19903 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
19904 EABIs, we have to decide whether we're using the 32-bit or 64-bit
19905 version. Look first at the -mgp options, if given, otherwise base
19906 the choice on MIPS_DEFAULT_64BIT.
e7af610e 19907
316f5878
RS
19908 Treat NO_ABI like the EABIs. One reason to do this is that the
19909 plain 'mips' and 'mips64' configs have 'from-abi' as their default
19910 architecture. This code picks MIPS I for 'mips' and MIPS III for
19911 'mips64', just as we did in the days before 'from-abi'. */
19912 if (strcasecmp (cpu_string, "from-abi") == 0)
19913 {
19914 if (ABI_NEEDS_32BIT_REGS (mips_abi))
19915 return mips_cpu_info_from_isa (ISA_MIPS1);
19916
19917 if (ABI_NEEDS_64BIT_REGS (mips_abi))
19918 return mips_cpu_info_from_isa (ISA_MIPS3);
19919
bad1aba3 19920 if (file_mips_opts.gp >= 0)
19921 return mips_cpu_info_from_isa (file_mips_opts.gp == 32
0b35dfee 19922 ? ISA_MIPS1 : ISA_MIPS3);
316f5878
RS
19923
19924 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
19925 ? ISA_MIPS3
19926 : ISA_MIPS1);
19927 }
19928
19929 /* 'default' has traditionally been a no-op. Probably not very useful. */
19930 if (strcasecmp (cpu_string, "default") == 0)
19931 return 0;
19932
19933 for (p = mips_cpu_info_table; p->name != 0; p++)
19934 if (mips_matching_cpu_name_p (p->name, cpu_string))
19935 return p;
19936
1661c76c 19937 as_bad (_("bad value (%s) for %s"), cpu_string, option);
316f5878 19938 return 0;
e7af610e
NC
19939}
19940
316f5878
RS
19941/* Return the canonical processor information for ISA (a member of the
19942 ISA_MIPS* enumeration). */
19943
e7af610e 19944static const struct mips_cpu_info *
17a2f251 19945mips_cpu_info_from_isa (int isa)
e7af610e
NC
19946{
19947 int i;
19948
19949 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
ad3fea08 19950 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
316f5878 19951 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
19952 return (&mips_cpu_info_table[i]);
19953
e972090a 19954 return NULL;
e7af610e 19955}
fef14a42
TS
19956
19957static const struct mips_cpu_info *
17a2f251 19958mips_cpu_info_from_arch (int arch)
fef14a42
TS
19959{
19960 int i;
19961
19962 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19963 if (arch == mips_cpu_info_table[i].cpu)
19964 return (&mips_cpu_info_table[i]);
19965
19966 return NULL;
19967}
316f5878
RS
19968\f
19969static void
17a2f251 19970show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
19971{
19972 if (*first_p)
19973 {
19974 fprintf (stream, "%24s", "");
19975 *col_p = 24;
19976 }
19977 else
19978 {
19979 fprintf (stream, ", ");
19980 *col_p += 2;
19981 }
e7af610e 19982
316f5878
RS
19983 if (*col_p + strlen (string) > 72)
19984 {
19985 fprintf (stream, "\n%24s", "");
19986 *col_p = 24;
19987 }
19988
19989 fprintf (stream, "%s", string);
19990 *col_p += strlen (string);
19991
19992 *first_p = 0;
19993}
19994
19995void
17a2f251 19996md_show_usage (FILE *stream)
e7af610e 19997{
316f5878
RS
19998 int column, first;
19999 size_t i;
20000
20001 fprintf (stream, _("\
20002MIPS options:\n\
316f5878
RS
20003-EB generate big endian output\n\
20004-EL generate little endian output\n\
20005-g, -g2 do not remove unneeded NOPs or swap branches\n\
20006-G NUM allow referencing objects up to NUM bytes\n\
20007 implicitly with the gp register [default 8]\n"));
20008 fprintf (stream, _("\
20009-mips1 generate MIPS ISA I instructions\n\
20010-mips2 generate MIPS ISA II instructions\n\
20011-mips3 generate MIPS ISA III instructions\n\
20012-mips4 generate MIPS ISA IV instructions\n\
20013-mips5 generate MIPS ISA V instructions\n\
20014-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 20015-mips32r2 generate MIPS32 release 2 ISA instructions\n\
ae52f483
AB
20016-mips32r3 generate MIPS32 release 3 ISA instructions\n\
20017-mips32r5 generate MIPS32 release 5 ISA instructions\n\
7361da2c 20018-mips32r6 generate MIPS32 release 6 ISA instructions\n\
316f5878 20019-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 20020-mips64r2 generate MIPS64 release 2 ISA instructions\n\
ae52f483
AB
20021-mips64r3 generate MIPS64 release 3 ISA instructions\n\
20022-mips64r5 generate MIPS64 release 5 ISA instructions\n\
7361da2c 20023-mips64r6 generate MIPS64 release 6 ISA instructions\n\
316f5878
RS
20024-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
20025
20026 first = 1;
e7af610e
NC
20027
20028 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
20029 show (stream, mips_cpu_info_table[i].name, &column, &first);
20030 show (stream, "from-abi", &column, &first);
20031 fputc ('\n', stream);
e7af610e 20032
316f5878
RS
20033 fprintf (stream, _("\
20034-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
20035-no-mCPU don't generate code specific to CPU.\n\
20036 For -mCPU and -no-mCPU, CPU must be one of:\n"));
20037
20038 first = 1;
20039
20040 show (stream, "3900", &column, &first);
20041 show (stream, "4010", &column, &first);
20042 show (stream, "4100", &column, &first);
20043 show (stream, "4650", &column, &first);
20044 fputc ('\n', stream);
20045
20046 fprintf (stream, _("\
20047-mips16 generate mips16 instructions\n\
20048-no-mips16 do not generate mips16 instructions\n"));
20049 fprintf (stream, _("\
f866b262
MR
20050-mmips16e2 generate MIPS16e2 instructions\n\
20051-mno-mips16e2 do not generate MIPS16e2 instructions\n"));
20052 fprintf (stream, _("\
df58fc94
RS
20053-mmicromips generate microMIPS instructions\n\
20054-mno-micromips do not generate microMIPS instructions\n"));
20055 fprintf (stream, _("\
e16bfa71 20056-msmartmips generate smartmips instructions\n\
3739860c 20057-mno-smartmips do not generate smartmips instructions\n"));
e16bfa71 20058 fprintf (stream, _("\
74cd071d
CF
20059-mdsp generate DSP instructions\n\
20060-mno-dsp do not generate DSP instructions\n"));
20061 fprintf (stream, _("\
8b082fb1
TS
20062-mdspr2 generate DSP R2 instructions\n\
20063-mno-dspr2 do not generate DSP R2 instructions\n"));
20064 fprintf (stream, _("\
8f4f9071
MF
20065-mdspr3 generate DSP R3 instructions\n\
20066-mno-dspr3 do not generate DSP R3 instructions\n"));
20067 fprintf (stream, _("\
ef2e4d86
CF
20068-mmt generate MT instructions\n\
20069-mno-mt do not generate MT instructions\n"));
20070 fprintf (stream, _("\
dec0624d
MR
20071-mmcu generate MCU instructions\n\
20072-mno-mcu do not generate MCU instructions\n"));
20073 fprintf (stream, _("\
56d438b1
CF
20074-mmsa generate MSA instructions\n\
20075-mno-msa do not generate MSA instructions\n"));
20076 fprintf (stream, _("\
7d64c587
AB
20077-mxpa generate eXtended Physical Address (XPA) instructions\n\
20078-mno-xpa do not generate eXtended Physical Address (XPA) instructions\n"));
20079 fprintf (stream, _("\
b015e599
AP
20080-mvirt generate Virtualization instructions\n\
20081-mno-virt do not generate Virtualization instructions\n"));
20082 fprintf (stream, _("\
730c3174
SE
20083-mcrc generate CRC instructions\n\
20084-mno-crc do not generate CRC instructions\n"));
20085 fprintf (stream, _("\
6f20c942
FS
20086-mginv generate Global INValidate (GINV) instructions\n\
20087-mno-ginv do not generate Global INValidate instructions\n"));
20088 fprintf (stream, _("\
8095d2f7
CX
20089-mloongson-mmi generate Loongson MultiMedia extensions Instructions (MMI) instructions\n\
20090-mno-loongson-mmi do not generate Loongson MultiMedia extensions Instructions\n"));
20091 fprintf (stream, _("\
716c08de
CX
20092-mloongson-cam generate Loongson Content Address Memory (CAM) instructions\n\
20093-mno-loongson-cam do not generate Loongson Content Address Memory Instructions\n"));
20094 fprintf (stream, _("\
bdc6c06e
CX
20095-mloongson-ext generate Loongson EXTensions (EXT) instructions\n\
20096-mno-loongson-ext do not generate Loongson EXTensions Instructions\n"));
20097 fprintf (stream, _("\
a693765e
CX
20098-mloongson-ext2 generate Loongson EXTensions R2 (EXT2) instructions\n\
20099-mno-loongson-ext2 do not generate Loongson EXTensions R2 Instructions\n"));
20100 fprintf (stream, _("\
833794fc
MR
20101-minsn32 only generate 32-bit microMIPS instructions\n\
20102-mno-insn32 generate all microMIPS instructions\n"));
20103 fprintf (stream, _("\
c67a084a
NC
20104-mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
20105-mfix-loongson2f-nop work around Loongson2F NOP errata\n\
d766e8ec 20106-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 20107-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
6a32d874 20108-mfix-24k insert a nop after ERET and DERET instructions\n\
d954098f 20109-mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
316f5878
RS
20110-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
20111-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261 20112-msym32 assume all symbols have 32-bit values\n\
092a534f
MR
20113-O0 do not remove unneeded NOPs, do not swap branches\n\
20114-O, -O1 remove unneeded NOPs, do not swap branches\n\
20115-O2 remove unneeded NOPs and swap branches\n\
316f5878
RS
20116--trap, --no-break trap exception on div by 0 and mult overflow\n\
20117--break, --no-trap break exception on div by 0 and mult overflow\n"));
037b32b9
AN
20118 fprintf (stream, _("\
20119-mhard-float allow floating-point instructions\n\
20120-msoft-float do not allow floating-point instructions\n\
20121-msingle-float only allow 32-bit floating-point operations\n\
20122-mdouble-float allow 32-bit and 64-bit floating-point operations\n\
3bf0dbfb 20123--[no-]construct-floats [dis]allow floating point values to be constructed\n\
ba92f887 20124--[no-]relax-branch [dis]allow out-of-range branches to be relaxed\n\
8b10b0b3
MR
20125-mignore-branch-isa accept invalid branches requiring an ISA mode switch\n\
20126-mno-ignore-branch-isa reject invalid branches requiring an ISA mode switch\n\
ba92f887
MR
20127-mnan=ENCODING select an IEEE 754 NaN encoding convention, either of:\n"));
20128
20129 first = 1;
20130
20131 show (stream, "legacy", &column, &first);
20132 show (stream, "2008", &column, &first);
20133
20134 fputc ('\n', stream);
20135
316f5878
RS
20136 fprintf (stream, _("\
20137-KPIC, -call_shared generate SVR4 position independent code\n\
861fb55a 20138-call_nonpic generate non-PIC code that can operate with DSOs\n\
0c000745 20139-mvxworks-pic generate VxWorks position independent code\n\
861fb55a 20140-non_shared do not generate code that can operate with DSOs\n\
316f5878 20141-xgot assume a 32 bit GOT\n\
dcd410fe 20142-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8 20143-mshared, -mno-shared disable/enable .cpload optimization for\n\
d821e36b 20144 position dependent (non shared) code\n\
316f5878
RS
20145-mabi=ABI create ABI conformant object file for:\n"));
20146
20147 first = 1;
20148
20149 show (stream, "32", &column, &first);
20150 show (stream, "o64", &column, &first);
20151 show (stream, "n32", &column, &first);
20152 show (stream, "64", &column, &first);
20153 show (stream, "eabi", &column, &first);
20154
20155 fputc ('\n', stream);
20156
20157 fprintf (stream, _("\
b4f6242e
MR
20158-32 create o32 ABI object file%s\n"),
20159 MIPS_DEFAULT_ABI == O32_ABI ? _(" (default)") : "");
20160 fprintf (stream, _("\
20161-n32 create n32 ABI object file%s\n"),
20162 MIPS_DEFAULT_ABI == N32_ABI ? _(" (default)") : "");
20163 fprintf (stream, _("\
20164-64 create 64 ABI object file%s\n"),
20165 MIPS_DEFAULT_ABI == N64_ABI ? _(" (default)") : "");
e7af610e 20166}
14e777e0 20167
1575952e 20168#ifdef TE_IRIX
14e777e0 20169enum dwarf2_format
413a266c 20170mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
14e777e0 20171{
369943fe 20172 if (HAVE_64BIT_SYMBOLS)
1575952e 20173 return dwarf2_format_64bit_irix;
14e777e0
KB
20174 else
20175 return dwarf2_format_32bit;
20176}
1575952e 20177#endif
73369e65
EC
20178
20179int
20180mips_dwarf2_addr_size (void)
20181{
6b6b3450 20182 if (HAVE_64BIT_OBJECTS)
73369e65 20183 return 8;
73369e65
EC
20184 else
20185 return 4;
20186}
5862107c
EC
20187
20188/* Standard calling conventions leave the CFA at SP on entry. */
20189void
20190mips_cfi_frame_initial_instructions (void)
20191{
20192 cfi_add_CFA_def_cfa_register (SP);
20193}
20194
707bfff6
TS
20195int
20196tc_mips_regname_to_dw2regnum (char *regname)
20197{
20198 unsigned int regnum = -1;
20199 unsigned int reg;
20200
20201 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
20202 regnum = reg;
20203
20204 return regnum;
20205}
263b2574 20206
20207/* Implement CONVERT_SYMBOLIC_ATTRIBUTE.
20208 Given a symbolic attribute NAME, return the proper integer value.
20209 Returns -1 if the attribute is not known. */
20210
20211int
20212mips_convert_symbolic_attribute (const char *name)
20213{
20214 static const struct
20215 {
20216 const char * name;
20217 const int tag;
20218 }
20219 attribute_table[] =
20220 {
20221#define T(tag) {#tag, tag}
20222 T (Tag_GNU_MIPS_ABI_FP),
20223 T (Tag_GNU_MIPS_ABI_MSA),
20224#undef T
20225 };
20226 unsigned int i;
20227
20228 if (name == NULL)
20229 return -1;
20230
20231 for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
20232 if (streq (name, attribute_table[i].name))
20233 return attribute_table[i].tag;
20234
20235 return -1;
20236}
fd5c94ab
RS
20237
20238void
20239md_mips_end (void)
20240{
351cdf24
MF
20241 int fpabi = Val_GNU_MIPS_ABI_FP_ANY;
20242
fd5c94ab
RS
20243 mips_emit_delays ();
20244 if (cur_proc_ptr)
20245 as_warn (_("missing .end at end of assembly"));
919731af 20246
20247 /* Just in case no code was emitted, do the consistency check. */
20248 file_mips_check_options ();
351cdf24
MF
20249
20250 /* Set a floating-point ABI if the user did not. */
20251 if (obj_elf_seen_attribute (OBJ_ATTR_GNU, Tag_GNU_MIPS_ABI_FP))
20252 {
20253 /* Perform consistency checks on the floating-point ABI. */
20254 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20255 Tag_GNU_MIPS_ABI_FP);
20256 if (fpabi != Val_GNU_MIPS_ABI_FP_ANY)
20257 check_fpabi (fpabi);
20258 }
20259 else
20260 {
20261 /* Soft-float gets precedence over single-float, the two options should
20262 not be used together so this should not matter. */
20263 if (file_mips_opts.soft_float == 1)
20264 fpabi = Val_GNU_MIPS_ABI_FP_SOFT;
20265 /* Single-float gets precedence over all double_float cases. */
20266 else if (file_mips_opts.single_float == 1)
20267 fpabi = Val_GNU_MIPS_ABI_FP_SINGLE;
20268 else
20269 {
20270 switch (file_mips_opts.fp)
20271 {
20272 case 32:
20273 if (file_mips_opts.gp == 32)
20274 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20275 break;
20276 case 0:
20277 fpabi = Val_GNU_MIPS_ABI_FP_XX;
20278 break;
20279 case 64:
20280 if (file_mips_opts.gp == 32 && !file_mips_opts.oddspreg)
20281 fpabi = Val_GNU_MIPS_ABI_FP_64A;
20282 else if (file_mips_opts.gp == 32)
20283 fpabi = Val_GNU_MIPS_ABI_FP_64;
20284 else
20285 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20286 break;
20287 }
20288 }
20289
20290 bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20291 Tag_GNU_MIPS_ABI_FP, fpabi);
20292 }
fd5c94ab 20293}
2f0c68f2
CM
20294
20295/* Returns the relocation type required for a particular CFI encoding. */
20296
20297bfd_reloc_code_real_type
20298mips_cfi_reloc_for_encoding (int encoding)
20299{
20300 if (encoding == (DW_EH_PE_sdata4 | DW_EH_PE_pcrel))
20301 return BFD_RELOC_32_PCREL;
20302 else return BFD_RELOC_NONE;
20303}
This page took 3.702913 seconds and 4 git commands to generate.