MIPS/gas: Fix misaligned address errors to disregard ISA mode bit
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
CommitLineData
252b5132 1/* tc-mips.c -- assemble code for a MIPS chip.
82704155 2 Copyright (C) 1993-2019 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
6f2117ba
PH
144 /* The name if this is an label. */
145 char label[16];
146
147 /* The target label name if this is an branch. */
148 char target[16];
149
47e39b9d
RS
150 /* The frag that contains the instruction. */
151 struct frag *frag;
152
153 /* The offset into FRAG of the first instruction byte. */
154 long where;
155
156 /* The relocs associated with the instruction, if any. */
157 fixS *fixp[3];
158
a38419a5
RS
159 /* True if this entry cannot be moved from its current position. */
160 unsigned int fixed_p : 1;
47e39b9d 161
708587a4 162 /* True if this instruction occurred in a .set noreorder block. */
47e39b9d
RS
163 unsigned int noreorder_p : 1;
164
2fa15973
RS
165 /* True for mips16 instructions that jump to an absolute address. */
166 unsigned int mips16_absolute_jump_p : 1;
15be625d
CM
167
168 /* True if this instruction is complete. */
169 unsigned int complete_p : 1;
e407c74b
NC
170
171 /* True if this instruction is cleared from history by unconditional
172 branch. */
173 unsigned int cleared_p : 1;
47e39b9d
RS
174};
175
a325df1d
TS
176/* The ABI to use. */
177enum mips_abi_level
178{
179 NO_ABI = 0,
180 O32_ABI,
181 O64_ABI,
182 N32_ABI,
183 N64_ABI,
184 EABI_ABI
185};
186
187/* MIPS ABI we are using for this output file. */
316f5878 188static enum mips_abi_level mips_abi = NO_ABI;
a325df1d 189
143d77c5
EC
190/* Whether or not we have code that can call pic code. */
191int mips_abicalls = FALSE;
192
aa6975fb
ILT
193/* Whether or not we have code which can be put into a shared
194 library. */
195static bfd_boolean mips_in_shared = TRUE;
196
252b5132
RH
197/* This is the set of options which may be modified by the .set
198 pseudo-op. We use a struct so that .set push and .set pop are more
199 reliable. */
200
e972090a
NC
201struct mips_set_options
202{
252b5132
RH
203 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
204 if it has not been initialized. Changed by `.set mipsN', and the
205 -mipsN command line option, and the default CPU. */
206 int isa;
846ef2d0
RS
207 /* Enabled Application Specific Extensions (ASEs). Changed by `.set
208 <asename>', by command line options, and based on the default
209 architecture. */
210 int ase;
252b5132
RH
211 /* Whether we are assembling for the mips16 processor. 0 if we are
212 not, 1 if we are, and -1 if the value has not been initialized.
213 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
214 -nomips16 command line options, and the default CPU. */
215 int mips16;
df58fc94
RS
216 /* Whether we are assembling for the mipsMIPS ASE. 0 if we are not,
217 1 if we are, and -1 if the value has not been initialized. Changed
218 by `.set micromips' and `.set nomicromips', and the -mmicromips
219 and -mno-micromips command line options, and the default CPU. */
220 int micromips;
252b5132
RH
221 /* Non-zero if we should not reorder instructions. Changed by `.set
222 reorder' and `.set noreorder'. */
223 int noreorder;
741fe287
MR
224 /* Non-zero if we should not permit the register designated "assembler
225 temporary" to be used in instructions. The value is the register
226 number, normally $at ($1). Changed by `.set at=REG', `.set noat'
227 (same as `.set at=$0') and `.set at' (same as `.set at=$1'). */
228 unsigned int at;
252b5132
RH
229 /* Non-zero if we should warn when a macro instruction expands into
230 more than one machine instruction. Changed by `.set nomacro' and
231 `.set macro'. */
232 int warn_about_macros;
233 /* Non-zero if we should not move instructions. Changed by `.set
234 move', `.set volatile', `.set nomove', and `.set novolatile'. */
235 int nomove;
236 /* Non-zero if we should not optimize branches by moving the target
237 of the branch into the delay slot. Actually, we don't perform
238 this optimization anyhow. Changed by `.set bopt' and `.set
239 nobopt'. */
240 int nobopt;
241 /* Non-zero if we should not autoextend mips16 instructions.
242 Changed by `.set autoextend' and `.set noautoextend'. */
243 int noautoextend;
833794fc
MR
244 /* True if we should only emit 32-bit microMIPS instructions.
245 Changed by `.set insn32' and `.set noinsn32', and the -minsn32
246 and -mno-insn32 command line options. */
247 bfd_boolean insn32;
a325df1d
TS
248 /* Restrict general purpose registers and floating point registers
249 to 32 bit. This is initially determined when -mgp32 or -mfp32
250 is passed but can changed if the assembler code uses .set mipsN. */
bad1aba3 251 int gp;
0b35dfee 252 int fp;
fef14a42
TS
253 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
254 command line option, and the default CPU. */
255 int arch;
aed1a261
RS
256 /* True if ".set sym32" is in effect. */
257 bfd_boolean sym32;
037b32b9
AN
258 /* True if floating-point operations are not allowed. Changed by .set
259 softfloat or .set hardfloat, by command line options -msoft-float or
260 -mhard-float. The default is false. */
261 bfd_boolean soft_float;
262
263 /* True if only single-precision floating-point operations are allowed.
264 Changed by .set singlefloat or .set doublefloat, command-line options
265 -msingle-float or -mdouble-float. The default is false. */
266 bfd_boolean single_float;
351cdf24
MF
267
268 /* 1 if single-precision operations on odd-numbered registers are
269 allowed. */
270 int oddspreg;
3315614d
MF
271
272 /* The set of ASEs that should be enabled for the user specified
273 architecture. This cannot be inferred from 'arch' for all cores
274 as processors only have a unique 'arch' if they add architecture
275 specific instructions (UDI). */
276 int init_ase;
252b5132
RH
277};
278
919731af 279/* Specifies whether module level options have been checked yet. */
280static bfd_boolean file_mips_opts_checked = FALSE;
281
7361da2c
AB
282/* Do we support nan2008? 0 if we don't, 1 if we do, and -1 if the
283 value has not been initialized. Changed by `.nan legacy' and
284 `.nan 2008', and the -mnan=legacy and -mnan=2008 command line
285 options, and the default CPU. */
286static int mips_nan2008 = -1;
a325df1d 287
0b35dfee 288/* This is the struct we use to hold the module level set of options.
bad1aba3 289 Note that we must set the isa field to ISA_UNKNOWN and the ASE, gp and
0b35dfee 290 fp fields to -1 to indicate that they have not been initialized. */
037b32b9 291
0b35dfee 292static struct mips_set_options file_mips_opts =
293{
294 /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
295 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
296 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
bad1aba3 297 /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
3315614d
MF
298 /* soft_float */ FALSE, /* single_float */ FALSE, /* oddspreg */ -1,
299 /* init_ase */ 0
0b35dfee 300};
252b5132 301
0b35dfee 302/* This is similar to file_mips_opts, but for the current set of options. */
ba92f887 303
e972090a
NC
304static struct mips_set_options mips_opts =
305{
846ef2d0 306 /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
b015e599 307 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
833794fc 308 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
bad1aba3 309 /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
3315614d
MF
310 /* soft_float */ FALSE, /* single_float */ FALSE, /* oddspreg */ -1,
311 /* init_ase */ 0
e7af610e 312};
252b5132 313
846ef2d0
RS
314/* Which bits of file_ase were explicitly set or cleared by ASE options. */
315static unsigned int file_ase_explicit;
316
252b5132
RH
317/* These variables are filled in with the masks of registers used.
318 The object format code reads them and puts them in the appropriate
319 place. */
320unsigned long mips_gprmask;
321unsigned long mips_cprmask[4];
322
738f4d98 323/* True if any MIPS16 code was produced. */
a4672219
TS
324static int file_ase_mips16;
325
3994f87e
TS
326#define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32 \
327 || mips_opts.isa == ISA_MIPS32R2 \
ae52f483
AB
328 || mips_opts.isa == ISA_MIPS32R3 \
329 || mips_opts.isa == ISA_MIPS32R5 \
3994f87e 330 || mips_opts.isa == ISA_MIPS64 \
ae52f483
AB
331 || mips_opts.isa == ISA_MIPS64R2 \
332 || mips_opts.isa == ISA_MIPS64R3 \
333 || mips_opts.isa == ISA_MIPS64R5)
3994f87e 334
df58fc94
RS
335/* True if any microMIPS code was produced. */
336static int file_ase_micromips;
337
b12dd2e4
CF
338/* True if we want to create R_MIPS_JALR for jalr $25. */
339#ifdef TE_IRIX
1180b5a4 340#define MIPS_JALR_HINT_P(EXPR) HAVE_NEWABI
b12dd2e4 341#else
1180b5a4
RS
342/* As a GNU extension, we use R_MIPS_JALR for o32 too. However,
343 because there's no place for any addend, the only acceptable
344 expression is a bare symbol. */
345#define MIPS_JALR_HINT_P(EXPR) \
346 (!HAVE_IN_PLACE_ADDENDS \
347 || ((EXPR)->X_op == O_symbol && (EXPR)->X_add_number == 0))
b12dd2e4
CF
348#endif
349
ec68c924 350/* The argument of the -march= flag. The architecture we are assembling. */
316f5878 351static const char *mips_arch_string;
ec68c924
EC
352
353/* The argument of the -mtune= flag. The architecture for which we
354 are optimizing. */
355static int mips_tune = CPU_UNKNOWN;
316f5878 356static const char *mips_tune_string;
ec68c924 357
316f5878 358/* True when generating 32-bit code for a 64-bit processor. */
252b5132
RH
359static int mips_32bitmode = 0;
360
316f5878
RS
361/* True if the given ABI requires 32-bit registers. */
362#define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
363
364/* Likewise 64-bit registers. */
707bfff6 365#define ABI_NEEDS_64BIT_REGS(ABI) \
134c0c8b 366 ((ABI) == N32_ABI \
707bfff6 367 || (ABI) == N64_ABI \
316f5878
RS
368 || (ABI) == O64_ABI)
369
7361da2c
AB
370#define ISA_IS_R6(ISA) \
371 ((ISA) == ISA_MIPS32R6 \
372 || (ISA) == ISA_MIPS64R6)
373
ad3fea08 374/* Return true if ISA supports 64 bit wide gp registers. */
707bfff6
TS
375#define ISA_HAS_64BIT_REGS(ISA) \
376 ((ISA) == ISA_MIPS3 \
377 || (ISA) == ISA_MIPS4 \
378 || (ISA) == ISA_MIPS5 \
379 || (ISA) == ISA_MIPS64 \
ae52f483
AB
380 || (ISA) == ISA_MIPS64R2 \
381 || (ISA) == ISA_MIPS64R3 \
7361da2c
AB
382 || (ISA) == ISA_MIPS64R5 \
383 || (ISA) == ISA_MIPS64R6)
9ce8a5dd 384
ad3fea08
TS
385/* Return true if ISA supports 64 bit wide float registers. */
386#define ISA_HAS_64BIT_FPRS(ISA) \
387 ((ISA) == ISA_MIPS3 \
388 || (ISA) == ISA_MIPS4 \
389 || (ISA) == ISA_MIPS5 \
390 || (ISA) == ISA_MIPS32R2 \
ae52f483
AB
391 || (ISA) == ISA_MIPS32R3 \
392 || (ISA) == ISA_MIPS32R5 \
7361da2c 393 || (ISA) == ISA_MIPS32R6 \
ad3fea08 394 || (ISA) == ISA_MIPS64 \
ae52f483
AB
395 || (ISA) == ISA_MIPS64R2 \
396 || (ISA) == ISA_MIPS64R3 \
7361da2c
AB
397 || (ISA) == ISA_MIPS64R5 \
398 || (ISA) == ISA_MIPS64R6)
ad3fea08 399
af7ee8bf
CD
400/* Return true if ISA supports 64-bit right rotate (dror et al.)
401 instructions. */
707bfff6 402#define ISA_HAS_DROR(ISA) \
df58fc94 403 ((ISA) == ISA_MIPS64R2 \
ae52f483
AB
404 || (ISA) == ISA_MIPS64R3 \
405 || (ISA) == ISA_MIPS64R5 \
7361da2c 406 || (ISA) == ISA_MIPS64R6 \
df58fc94
RS
407 || (mips_opts.micromips \
408 && ISA_HAS_64BIT_REGS (ISA)) \
409 )
af7ee8bf
CD
410
411/* Return true if ISA supports 32-bit right rotate (ror et al.)
412 instructions. */
707bfff6
TS
413#define ISA_HAS_ROR(ISA) \
414 ((ISA) == ISA_MIPS32R2 \
ae52f483
AB
415 || (ISA) == ISA_MIPS32R3 \
416 || (ISA) == ISA_MIPS32R5 \
7361da2c 417 || (ISA) == ISA_MIPS32R6 \
707bfff6 418 || (ISA) == ISA_MIPS64R2 \
ae52f483
AB
419 || (ISA) == ISA_MIPS64R3 \
420 || (ISA) == ISA_MIPS64R5 \
7361da2c 421 || (ISA) == ISA_MIPS64R6 \
846ef2d0 422 || (mips_opts.ase & ASE_SMARTMIPS) \
df58fc94
RS
423 || mips_opts.micromips \
424 )
707bfff6 425
7455baf8 426/* Return true if ISA supports single-precision floats in odd registers. */
351cdf24
MF
427#define ISA_HAS_ODD_SINGLE_FPR(ISA, CPU)\
428 (((ISA) == ISA_MIPS32 \
429 || (ISA) == ISA_MIPS32R2 \
430 || (ISA) == ISA_MIPS32R3 \
431 || (ISA) == ISA_MIPS32R5 \
7361da2c 432 || (ISA) == ISA_MIPS32R6 \
351cdf24
MF
433 || (ISA) == ISA_MIPS64 \
434 || (ISA) == ISA_MIPS64R2 \
435 || (ISA) == ISA_MIPS64R3 \
436 || (ISA) == ISA_MIPS64R5 \
7361da2c 437 || (ISA) == ISA_MIPS64R6 \
351cdf24 438 || (CPU) == CPU_R5900) \
bd782c07 439 && ((CPU) != CPU_GS464 \
9108bc33
CX
440 || (CPU) != CPU_GS464E \
441 || (CPU) != CPU_GS264E))
af7ee8bf 442
ad3fea08
TS
443/* Return true if ISA supports move to/from high part of a 64-bit
444 floating-point register. */
445#define ISA_HAS_MXHC1(ISA) \
446 ((ISA) == ISA_MIPS32R2 \
ae52f483
AB
447 || (ISA) == ISA_MIPS32R3 \
448 || (ISA) == ISA_MIPS32R5 \
7361da2c
AB
449 || (ISA) == ISA_MIPS32R6 \
450 || (ISA) == ISA_MIPS64R2 \
451 || (ISA) == ISA_MIPS64R3 \
452 || (ISA) == ISA_MIPS64R5 \
453 || (ISA) == ISA_MIPS64R6)
454
455/* Return true if ISA supports legacy NAN. */
456#define ISA_HAS_LEGACY_NAN(ISA) \
457 ((ISA) == ISA_MIPS1 \
458 || (ISA) == ISA_MIPS2 \
459 || (ISA) == ISA_MIPS3 \
460 || (ISA) == ISA_MIPS4 \
461 || (ISA) == ISA_MIPS5 \
462 || (ISA) == ISA_MIPS32 \
463 || (ISA) == ISA_MIPS32R2 \
464 || (ISA) == ISA_MIPS32R3 \
465 || (ISA) == ISA_MIPS32R5 \
466 || (ISA) == ISA_MIPS64 \
ae52f483
AB
467 || (ISA) == ISA_MIPS64R2 \
468 || (ISA) == ISA_MIPS64R3 \
469 || (ISA) == ISA_MIPS64R5)
ad3fea08 470
bad1aba3 471#define GPR_SIZE \
472 (mips_opts.gp == 64 && !ISA_HAS_64BIT_REGS (mips_opts.isa) \
473 ? 32 \
474 : mips_opts.gp)
ca4e0257 475
bad1aba3 476#define FPR_SIZE \
477 (mips_opts.fp == 64 && !ISA_HAS_64BIT_FPRS (mips_opts.isa) \
478 ? 32 \
479 : mips_opts.fp)
ca4e0257 480
316f5878 481#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
e013f690 482
316f5878 483#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
e013f690 484
3b91255e
RS
485/* True if relocations are stored in-place. */
486#define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
487
aed1a261
RS
488/* The ABI-derived address size. */
489#define HAVE_64BIT_ADDRESSES \
bad1aba3 490 (GPR_SIZE == 64 && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
aed1a261 491#define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
e013f690 492
aed1a261
RS
493/* The size of symbolic constants (i.e., expressions of the form
494 "SYMBOL" or "SYMBOL + OFFSET"). */
495#define HAVE_32BIT_SYMBOLS \
496 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
497#define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
ca4e0257 498
b7c7d6c1
TS
499/* Addresses are loaded in different ways, depending on the address size
500 in use. The n32 ABI Documentation also mandates the use of additions
501 with overflow checking, but existing implementations don't follow it. */
f899b4b8 502#define ADDRESS_ADD_INSN \
b7c7d6c1 503 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
f899b4b8
TS
504
505#define ADDRESS_ADDI_INSN \
b7c7d6c1 506 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
f899b4b8
TS
507
508#define ADDRESS_LOAD_INSN \
509 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
510
511#define ADDRESS_STORE_INSN \
512 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
513
a4672219 514/* Return true if the given CPU supports the MIPS16 ASE. */
3396de36
TS
515#define CPU_HAS_MIPS16(cpu) \
516 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
517 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
a4672219 518
2309ddf2 519/* Return true if the given CPU supports the microMIPS ASE. */
df58fc94
RS
520#define CPU_HAS_MICROMIPS(cpu) 0
521
60b63b72
RS
522/* True if CPU has a dror instruction. */
523#define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
524
525/* True if CPU has a ror instruction. */
526#define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
527
6f2117ba 528/* True if CPU is in the Octeon family. */
2c629856
N
529#define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP \
530 || (CPU) == CPU_OCTEON2 || (CPU) == CPU_OCTEON3)
dd6a37e7 531
dd3cbb7e 532/* True if CPU has seq/sne and seqi/snei instructions. */
dd6a37e7 533#define CPU_HAS_SEQ(CPU) (CPU_IS_OCTEON (CPU))
dd3cbb7e 534
0aa27725
RS
535/* True, if CPU has support for ldc1 and sdc1. */
536#define CPU_HAS_LDC1_SDC1(CPU) \
537 ((mips_opts.isa != ISA_MIPS1) && ((CPU) != CPU_R5900))
538
c8978940
CD
539/* True if mflo and mfhi can be immediately followed by instructions
540 which write to the HI and LO registers.
541
542 According to MIPS specifications, MIPS ISAs I, II, and III need
543 (at least) two instructions between the reads of HI/LO and
544 instructions which write them, and later ISAs do not. Contradicting
545 the MIPS specifications, some MIPS IV processor user manuals (e.g.
546 the UM for the NEC Vr5000) document needing the instructions between
547 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
548 MIPS64 and later ISAs to have the interlocks, plus any specific
549 earlier-ISA CPUs for which CPU documentation declares that the
550 instructions are really interlocked. */
551#define hilo_interlocks \
552 (mips_opts.isa == ISA_MIPS32 \
553 || mips_opts.isa == ISA_MIPS32R2 \
ae52f483
AB
554 || mips_opts.isa == ISA_MIPS32R3 \
555 || mips_opts.isa == ISA_MIPS32R5 \
7361da2c 556 || mips_opts.isa == ISA_MIPS32R6 \
c8978940
CD
557 || mips_opts.isa == ISA_MIPS64 \
558 || mips_opts.isa == ISA_MIPS64R2 \
ae52f483
AB
559 || mips_opts.isa == ISA_MIPS64R3 \
560 || mips_opts.isa == ISA_MIPS64R5 \
7361da2c 561 || mips_opts.isa == ISA_MIPS64R6 \
c8978940 562 || mips_opts.arch == CPU_R4010 \
e407c74b 563 || mips_opts.arch == CPU_R5900 \
c8978940
CD
564 || mips_opts.arch == CPU_R10000 \
565 || mips_opts.arch == CPU_R12000 \
3aa3176b
TS
566 || mips_opts.arch == CPU_R14000 \
567 || mips_opts.arch == CPU_R16000 \
c8978940 568 || mips_opts.arch == CPU_RM7000 \
c8978940 569 || mips_opts.arch == CPU_VR5500 \
df58fc94 570 || mips_opts.micromips \
c8978940 571 )
252b5132
RH
572
573/* Whether the processor uses hardware interlocks to protect reads
81912461
ILT
574 from the GPRs after they are loaded from memory, and thus does not
575 require nops to be inserted. This applies to instructions marked
67dc82bc 576 INSN_LOAD_MEMORY. These nops are only required at MIPS ISA
df58fc94
RS
577 level I and microMIPS mode instructions are always interlocked. */
578#define gpr_interlocks \
579 (mips_opts.isa != ISA_MIPS1 \
580 || mips_opts.arch == CPU_R3900 \
e407c74b 581 || mips_opts.arch == CPU_R5900 \
df58fc94
RS
582 || mips_opts.micromips \
583 )
252b5132 584
81912461
ILT
585/* Whether the processor uses hardware interlocks to avoid delays
586 required by coprocessor instructions, and thus does not require
587 nops to be inserted. This applies to instructions marked
43885403
MF
588 INSN_LOAD_COPROC, INSN_COPROC_MOVE, and to delays between
589 instructions marked INSN_WRITE_COND_CODE and ones marked
81912461 590 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
df58fc94
RS
591 levels I, II, and III and microMIPS mode instructions are always
592 interlocked. */
bdaaa2e1 593/* Itbl support may require additional care here. */
81912461
ILT
594#define cop_interlocks \
595 ((mips_opts.isa != ISA_MIPS1 \
596 && mips_opts.isa != ISA_MIPS2 \
597 && mips_opts.isa != ISA_MIPS3) \
598 || mips_opts.arch == CPU_R4300 \
df58fc94 599 || mips_opts.micromips \
81912461
ILT
600 )
601
602/* Whether the processor uses hardware interlocks to protect reads
603 from coprocessor registers after they are loaded from memory, and
604 thus does not require nops to be inserted. This applies to
605 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
df58fc94
RS
606 requires at MIPS ISA level I and microMIPS mode instructions are
607 always interlocked. */
608#define cop_mem_interlocks \
609 (mips_opts.isa != ISA_MIPS1 \
610 || mips_opts.micromips \
611 )
252b5132 612
6b76fefe
CM
613/* Is this a mfhi or mflo instruction? */
614#define MF_HILO_INSN(PINFO) \
b19e8a9b
AN
615 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
616
df58fc94
RS
617/* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
618 has been selected. This implies, in particular, that addresses of text
619 labels have their LSB set. */
620#define HAVE_CODE_COMPRESSION \
621 ((mips_opts.mips16 | mips_opts.micromips) != 0)
622
42429eac 623/* The minimum and maximum signed values that can be stored in a GPR. */
bad1aba3 624#define GPR_SMAX ((offsetT) (((valueT) 1 << (GPR_SIZE - 1)) - 1))
42429eac
RS
625#define GPR_SMIN (-GPR_SMAX - 1)
626
252b5132
RH
627/* MIPS PIC level. */
628
a161fe53 629enum mips_pic_level mips_pic;
252b5132 630
c9914766 631/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 632 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 633static int mips_big_got = 0;
252b5132
RH
634
635/* 1 if trap instructions should used for overflow rather than break
636 instructions. */
c9914766 637static int mips_trap = 0;
252b5132 638
119d663a 639/* 1 if double width floating point constants should not be constructed
b6ff326e 640 by assembling two single width halves into two single width floating
119d663a
NC
641 point registers which just happen to alias the double width destination
642 register. On some architectures this aliasing can be disabled by a bit
d547a75e 643 in the status register, and the setting of this bit cannot be determined
119d663a
NC
644 automatically at assemble time. */
645static int mips_disable_float_construction;
646
252b5132
RH
647/* Non-zero if any .set noreorder directives were used. */
648
649static int mips_any_noreorder;
650
6b76fefe
CM
651/* Non-zero if nops should be inserted when the register referenced in
652 an mfhi/mflo instruction is read in the next two instructions. */
653static int mips_7000_hilo_fix;
654
02ffd3e4 655/* The size of objects in the small data section. */
156c2f8b 656static unsigned int g_switch_value = 8;
252b5132
RH
657/* Whether the -G option was used. */
658static int g_switch_seen = 0;
659
660#define N_RMASK 0xc4
661#define N_VFP 0xd4
662
663/* If we can determine in advance that GP optimization won't be
664 possible, we can skip the relaxation stuff that tries to produce
665 GP-relative references. This makes delay slot optimization work
666 better.
667
668 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
669 gcc output. It needs to guess right for gcc, otherwise gcc
670 will put what it thinks is a GP-relative instruction in a branch
671 delay slot.
252b5132
RH
672
673 I don't know if a fix is needed for the SVR4_PIC mode. I've only
674 fixed it for the non-PIC mode. KR 95/04/07 */
17a2f251 675static int nopic_need_relax (symbolS *, int);
252b5132 676
6f2117ba 677/* Handle of the OPCODE hash table. */
252b5132
RH
678static struct hash_control *op_hash = NULL;
679
680/* The opcode hash table we use for the mips16. */
681static struct hash_control *mips16_op_hash = NULL;
682
df58fc94
RS
683/* The opcode hash table we use for the microMIPS ASE. */
684static struct hash_control *micromips_op_hash = NULL;
685
252b5132 686/* This array holds the chars that always start a comment. If the
6f2117ba 687 pre-processor is disabled, these aren't very useful. */
252b5132
RH
688const char comment_chars[] = "#";
689
690/* This array holds the chars that only start a comment at the beginning of
691 a line. If the line seems to have the form '# 123 filename'
6f2117ba 692 .line and .file directives will appear in the pre-processed output. */
252b5132
RH
693/* Note that input_file.c hand checks for '#' at the beginning of the
694 first line of the input file. This is because the compiler outputs
bdaaa2e1 695 #NO_APP at the beginning of its output. */
252b5132
RH
696/* Also note that C style comments are always supported. */
697const char line_comment_chars[] = "#";
698
bdaaa2e1 699/* This array holds machine specific line separator characters. */
63a0b638 700const char line_separator_chars[] = ";";
252b5132 701
6f2117ba 702/* Chars that can be used to separate mant from exp in floating point nums. */
252b5132
RH
703const char EXP_CHARS[] = "eE";
704
6f2117ba
PH
705/* Chars that mean this number is a floating point constant.
706 As in 0f12.456
707 or 0d1.2345e12. */
252b5132
RH
708const char FLT_CHARS[] = "rRsSfFdDxXpP";
709
710/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
711 changed in read.c . Ideally it shouldn't have to know about it at all,
6f2117ba 712 but nothing is ideal around here. */
252b5132 713
e3de51ce 714/* Types of printf format used for instruction-related error messages.
6f2117ba
PH
715 "I" means int ("%d") and "S" means string ("%s"). */
716enum mips_insn_error_format
717{
e3de51ce
RS
718 ERR_FMT_PLAIN,
719 ERR_FMT_I,
720 ERR_FMT_SS,
721};
722
723/* Information about an error that was found while assembling the current
724 instruction. */
6f2117ba
PH
725struct mips_insn_error
726{
e3de51ce
RS
727 /* We sometimes need to match an instruction against more than one
728 opcode table entry. Errors found during this matching are reported
729 against a particular syntactic argument rather than against the
730 instruction as a whole. We grade these messages so that errors
731 against argument N have a greater priority than an error against
732 any argument < N, since the former implies that arguments up to N
733 were acceptable and that the opcode entry was therefore a closer match.
734 If several matches report an error against the same argument,
735 we only use that error if it is the same in all cases.
736
737 min_argnum is the minimum argument number for which an error message
738 should be accepted. It is 0 if MSG is against the instruction as
739 a whole. */
740 int min_argnum;
741
742 /* The printf()-style message, including its format and arguments. */
743 enum mips_insn_error_format format;
744 const char *msg;
6f2117ba
PH
745 union
746 {
e3de51ce
RS
747 int i;
748 const char *ss[2];
749 } u;
750};
751
752/* The error that should be reported for the current instruction. */
753static struct mips_insn_error insn_error;
252b5132
RH
754
755static int auto_align = 1;
756
757/* When outputting SVR4 PIC code, the assembler needs to know the
758 offset in the stack frame from which to restore the $gp register.
759 This is set by the .cprestore pseudo-op, and saved in this
760 variable. */
761static offsetT mips_cprestore_offset = -1;
762
67c1ffbe 763/* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
6478892d 764 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 765 offset and even an other register than $gp as global pointer. */
6478892d
TS
766static offsetT mips_cpreturn_offset = -1;
767static int mips_cpreturn_register = -1;
768static int mips_gp_register = GP;
def2e0dd 769static int mips_gprel_offset = 0;
6478892d 770
7a621144
DJ
771/* Whether mips_cprestore_offset has been set in the current function
772 (or whether it has already been warned about, if not). */
773static int mips_cprestore_valid = 0;
774
252b5132
RH
775/* This is the register which holds the stack frame, as set by the
776 .frame pseudo-op. This is needed to implement .cprestore. */
777static int mips_frame_reg = SP;
778
7a621144
DJ
779/* Whether mips_frame_reg has been set in the current function
780 (or whether it has already been warned about, if not). */
781static int mips_frame_reg_valid = 0;
782
252b5132
RH
783/* To output NOP instructions correctly, we need to keep information
784 about the previous two instructions. */
785
786/* Whether we are optimizing. The default value of 2 means to remove
787 unneeded NOPs and swap branch instructions when possible. A value
788 of 1 means to not swap branches. A value of 0 means to always
789 insert NOPs. */
790static int mips_optimize = 2;
791
792/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
793 equivalent to seeing no -g option at all. */
794static int mips_debug = 0;
795
7d8e00cf
RS
796/* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
797#define MAX_VR4130_NOPS 4
798
799/* The maximum number of NOPs needed to fill delay slots. */
800#define MAX_DELAY_NOPS 2
801
802/* The maximum number of NOPs needed for any purpose. */
803#define MAX_NOPS 4
71400594 804
6f2117ba
PH
805/* The maximum range of context length of ll/sc. */
806#define MAX_LLSC_RANGE 20
807
71400594
RS
808/* A list of previous instructions, with index 0 being the most recent.
809 We need to look back MAX_NOPS instructions when filling delay slots
810 or working around processor errata. We need to look back one
811 instruction further if we're thinking about using history[0] to
812 fill a branch delay slot. */
6f2117ba 813static struct mips_cl_insn history[1 + MAX_NOPS + MAX_LLSC_RANGE];
252b5132 814
fc76e730 815/* Arrays of operands for each instruction. */
14daeee3 816#define MAX_OPERANDS 6
6f2117ba
PH
817struct mips_operand_array
818{
fc76e730
RS
819 const struct mips_operand *operand[MAX_OPERANDS];
820};
821static struct mips_operand_array *mips_operands;
822static struct mips_operand_array *mips16_operands;
823static struct mips_operand_array *micromips_operands;
824
1e915849 825/* Nop instructions used by emit_nop. */
df58fc94
RS
826static struct mips_cl_insn nop_insn;
827static struct mips_cl_insn mips16_nop_insn;
828static struct mips_cl_insn micromips_nop16_insn;
829static struct mips_cl_insn micromips_nop32_insn;
1e915849 830
6f2117ba
PH
831/* Sync instructions used by insert sync. */
832static struct mips_cl_insn sync_insn;
833
1e915849 834/* The appropriate nop for the current mode. */
833794fc
MR
835#define NOP_INSN (mips_opts.mips16 \
836 ? &mips16_nop_insn \
837 : (mips_opts.micromips \
838 ? (mips_opts.insn32 \
839 ? &micromips_nop32_insn \
840 : &micromips_nop16_insn) \
841 : &nop_insn))
df58fc94
RS
842
843/* The size of NOP_INSN in bytes. */
833794fc
MR
844#define NOP_INSN_SIZE ((mips_opts.mips16 \
845 || (mips_opts.micromips && !mips_opts.insn32)) \
846 ? 2 : 4)
252b5132 847
252b5132
RH
848/* If this is set, it points to a frag holding nop instructions which
849 were inserted before the start of a noreorder section. If those
850 nops turn out to be unnecessary, the size of the frag can be
851 decreased. */
852static fragS *prev_nop_frag;
853
854/* The number of nop instructions we created in prev_nop_frag. */
855static int prev_nop_frag_holds;
856
857/* The number of nop instructions that we know we need in
bdaaa2e1 858 prev_nop_frag. */
252b5132
RH
859static int prev_nop_frag_required;
860
861/* The number of instructions we've seen since prev_nop_frag. */
862static int prev_nop_frag_since;
863
e8044f35
RS
864/* Relocations against symbols are sometimes done in two parts, with a HI
865 relocation and a LO relocation. Each relocation has only 16 bits of
866 space to store an addend. This means that in order for the linker to
867 handle carries correctly, it must be able to locate both the HI and
868 the LO relocation. This means that the relocations must appear in
869 order in the relocation table.
252b5132
RH
870
871 In order to implement this, we keep track of each unmatched HI
872 relocation. We then sort them so that they immediately precede the
bdaaa2e1 873 corresponding LO relocation. */
252b5132 874
e972090a
NC
875struct mips_hi_fixup
876{
252b5132
RH
877 /* Next HI fixup. */
878 struct mips_hi_fixup *next;
879 /* This fixup. */
880 fixS *fixp;
881 /* The section this fixup is in. */
882 segT seg;
883};
884
885/* The list of unmatched HI relocs. */
886
887static struct mips_hi_fixup *mips_hi_fixup_list;
888
64bdfcaf
RS
889/* The frag containing the last explicit relocation operator.
890 Null if explicit relocations have not been used. */
891
892static fragS *prev_reloc_op_frag;
893
252b5132
RH
894/* Map mips16 register numbers to normal MIPS register numbers. */
895
e972090a
NC
896static const unsigned int mips16_to_32_reg_map[] =
897{
252b5132
RH
898 16, 17, 2, 3, 4, 5, 6, 7
899};
60b63b72 900
df58fc94
RS
901/* Map microMIPS register numbers to normal MIPS register numbers. */
902
df58fc94 903#define micromips_to_32_reg_d_map mips16_to_32_reg_map
df58fc94
RS
904
905/* The microMIPS registers with type h. */
e76ff5ab 906static const unsigned int micromips_to_32_reg_h_map1[] =
df58fc94
RS
907{
908 5, 5, 6, 4, 4, 4, 4, 4
909};
e76ff5ab 910static const unsigned int micromips_to_32_reg_h_map2[] =
df58fc94
RS
911{
912 6, 7, 7, 21, 22, 5, 6, 7
913};
914
df58fc94
RS
915/* The microMIPS registers with type m. */
916static const unsigned int micromips_to_32_reg_m_map[] =
917{
918 0, 17, 2, 3, 16, 18, 19, 20
919};
920
921#define micromips_to_32_reg_n_map micromips_to_32_reg_m_map
922
71400594
RS
923/* Classifies the kind of instructions we're interested in when
924 implementing -mfix-vr4120. */
c67a084a
NC
925enum fix_vr4120_class
926{
71400594
RS
927 FIX_VR4120_MACC,
928 FIX_VR4120_DMACC,
929 FIX_VR4120_MULT,
930 FIX_VR4120_DMULT,
931 FIX_VR4120_DIV,
932 FIX_VR4120_MTHILO,
933 NUM_FIX_VR4120_CLASSES
934};
935
c67a084a
NC
936/* ...likewise -mfix-loongson2f-jump. */
937static bfd_boolean mips_fix_loongson2f_jump;
938
939/* ...likewise -mfix-loongson2f-nop. */
940static bfd_boolean mips_fix_loongson2f_nop;
941
942/* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed. */
943static bfd_boolean mips_fix_loongson2f;
944
71400594
RS
945/* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
946 there must be at least one other instruction between an instruction
947 of type X and an instruction of type Y. */
948static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
949
950/* True if -mfix-vr4120 is in force. */
d766e8ec 951static int mips_fix_vr4120;
4a6a3df4 952
7d8e00cf
RS
953/* ...likewise -mfix-vr4130. */
954static int mips_fix_vr4130;
955
6a32d874
CM
956/* ...likewise -mfix-24k. */
957static int mips_fix_24k;
958
a8d14a88
CM
959/* ...likewise -mfix-rm7000 */
960static int mips_fix_rm7000;
961
d954098f
DD
962/* ...likewise -mfix-cn63xxp1 */
963static bfd_boolean mips_fix_cn63xxp1;
964
27c634e0
FN
965/* ...likewise -mfix-r5900 */
966static bfd_boolean mips_fix_r5900;
967static bfd_boolean mips_fix_r5900_explicit;
968
6f2117ba
PH
969/* ...likewise -mfix-loongson3-llsc. */
970static bfd_boolean mips_fix_loongson3_llsc = DEFAULT_MIPS_FIX_LOONGSON3_LLSC;
971
4a6a3df4
AO
972/* We don't relax branches by default, since this causes us to expand
973 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
974 fail to compute the offset before expanding the macro to the most
975 efficient expansion. */
976
977static int mips_relax_branch;
8b10b0b3
MR
978
979/* TRUE if checks are suppressed for invalid branches between ISA modes.
980 Needed for broken assembly produced by some GCC versions and some
981 sloppy code out there, where branches to data labels are present. */
982static bfd_boolean mips_ignore_branch_isa;
252b5132 983\f
4d7206a2
RS
984/* The expansion of many macros depends on the type of symbol that
985 they refer to. For example, when generating position-dependent code,
986 a macro that refers to a symbol may have two different expansions,
987 one which uses GP-relative addresses and one which uses absolute
988 addresses. When generating SVR4-style PIC, a macro may have
989 different expansions for local and global symbols.
990
991 We handle these situations by generating both sequences and putting
992 them in variant frags. In position-dependent code, the first sequence
993 will be the GP-relative one and the second sequence will be the
994 absolute one. In SVR4 PIC, the first sequence will be for global
995 symbols and the second will be for local symbols.
996
584892a6
RS
997 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
998 SECOND are the lengths of the two sequences in bytes. These fields
999 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
1000 the subtype has the following flags:
4d7206a2 1001
ce8ad872
MR
1002 RELAX_PIC
1003 Set if generating PIC code.
1004
584892a6
RS
1005 RELAX_USE_SECOND
1006 Set if it has been decided that we should use the second
1007 sequence instead of the first.
1008
1009 RELAX_SECOND_LONGER
1010 Set in the first variant frag if the macro's second implementation
1011 is longer than its first. This refers to the macro as a whole,
1012 not an individual relaxation.
1013
1014 RELAX_NOMACRO
1015 Set in the first variant frag if the macro appeared in a .set nomacro
1016 block and if one alternative requires a warning but the other does not.
1017
1018 RELAX_DELAY_SLOT
1019 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
1020 delay slot.
4d7206a2 1021
df58fc94
RS
1022 RELAX_DELAY_SLOT_16BIT
1023 Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
1024 16-bit instruction.
1025
1026 RELAX_DELAY_SLOT_SIZE_FIRST
1027 Like RELAX_DELAY_SLOT, but indicates that the first implementation of
1028 the macro is of the wrong size for the branch delay slot.
1029
1030 RELAX_DELAY_SLOT_SIZE_SECOND
1031 Like RELAX_DELAY_SLOT, but indicates that the second implementation of
1032 the macro is of the wrong size for the branch delay slot.
1033
4d7206a2
RS
1034 The frag's "opcode" points to the first fixup for relaxable code.
1035
1036 Relaxable macros are generated using a sequence such as:
1037
1038 relax_start (SYMBOL);
1039 ... generate first expansion ...
1040 relax_switch ();
1041 ... generate second expansion ...
1042 relax_end ();
1043
1044 The code and fixups for the unwanted alternative are discarded
1045 by md_convert_frag. */
ce8ad872
MR
1046#define RELAX_ENCODE(FIRST, SECOND, PIC) \
1047 (((FIRST) << 8) | (SECOND) | ((PIC) ? 0x10000 : 0))
4d7206a2 1048
584892a6
RS
1049#define RELAX_FIRST(X) (((X) >> 8) & 0xff)
1050#define RELAX_SECOND(X) ((X) & 0xff)
ce8ad872
MR
1051#define RELAX_PIC(X) (((X) & 0x10000) != 0)
1052#define RELAX_USE_SECOND 0x20000
1053#define RELAX_SECOND_LONGER 0x40000
1054#define RELAX_NOMACRO 0x80000
1055#define RELAX_DELAY_SLOT 0x100000
1056#define RELAX_DELAY_SLOT_16BIT 0x200000
1057#define RELAX_DELAY_SLOT_SIZE_FIRST 0x400000
1058#define RELAX_DELAY_SLOT_SIZE_SECOND 0x800000
252b5132 1059
4a6a3df4
AO
1060/* Branch without likely bit. If label is out of range, we turn:
1061
134c0c8b 1062 beq reg1, reg2, label
4a6a3df4
AO
1063 delay slot
1064
1065 into
1066
1067 bne reg1, reg2, 0f
1068 nop
1069 j label
1070 0: delay slot
1071
1072 with the following opcode replacements:
1073
1074 beq <-> bne
1075 blez <-> bgtz
1076 bltz <-> bgez
1077 bc1f <-> bc1t
1078
1079 bltzal <-> bgezal (with jal label instead of j label)
1080
1081 Even though keeping the delay slot instruction in the delay slot of
1082 the branch would be more efficient, it would be very tricky to do
1083 correctly, because we'd have to introduce a variable frag *after*
1084 the delay slot instruction, and expand that instead. Let's do it
1085 the easy way for now, even if the branch-not-taken case now costs
1086 one additional instruction. Out-of-range branches are not supposed
1087 to be common, anyway.
1088
1089 Branch likely. If label is out of range, we turn:
1090
1091 beql reg1, reg2, label
1092 delay slot (annulled if branch not taken)
1093
1094 into
1095
1096 beql reg1, reg2, 1f
1097 nop
1098 beql $0, $0, 2f
1099 nop
1100 1: j[al] label
1101 delay slot (executed only if branch taken)
1102 2:
1103
1104 It would be possible to generate a shorter sequence by losing the
1105 likely bit, generating something like:
b34976b6 1106
4a6a3df4
AO
1107 bne reg1, reg2, 0f
1108 nop
1109 j[al] label
1110 delay slot (executed only if branch taken)
1111 0:
1112
1113 beql -> bne
1114 bnel -> beq
1115 blezl -> bgtz
1116 bgtzl -> blez
1117 bltzl -> bgez
1118 bgezl -> bltz
1119 bc1fl -> bc1t
1120 bc1tl -> bc1f
1121
1122 bltzall -> bgezal (with jal label instead of j label)
1123 bgezall -> bltzal (ditto)
1124
1125
1126 but it's not clear that it would actually improve performance. */
ce8ad872
MR
1127#define RELAX_BRANCH_ENCODE(at, pic, \
1128 uncond, likely, link, toofar) \
66b3e8da
MR
1129 ((relax_substateT) \
1130 (0xc0000000 \
1131 | ((at) & 0x1f) \
ce8ad872
MR
1132 | ((pic) ? 0x20 : 0) \
1133 | ((toofar) ? 0x40 : 0) \
1134 | ((link) ? 0x80 : 0) \
1135 | ((likely) ? 0x100 : 0) \
1136 | ((uncond) ? 0x200 : 0)))
4a6a3df4 1137#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
ce8ad872
MR
1138#define RELAX_BRANCH_UNCOND(i) (((i) & 0x200) != 0)
1139#define RELAX_BRANCH_LIKELY(i) (((i) & 0x100) != 0)
1140#define RELAX_BRANCH_LINK(i) (((i) & 0x80) != 0)
1141#define RELAX_BRANCH_TOOFAR(i) (((i) & 0x40) != 0)
1142#define RELAX_BRANCH_PIC(i) (((i) & 0x20) != 0)
66b3e8da 1143#define RELAX_BRANCH_AT(i) ((i) & 0x1f)
4a6a3df4 1144
252b5132
RH
1145/* For mips16 code, we use an entirely different form of relaxation.
1146 mips16 supports two versions of most instructions which take
1147 immediate values: a small one which takes some small value, and a
1148 larger one which takes a 16 bit value. Since branches also follow
1149 this pattern, relaxing these values is required.
1150
1151 We can assemble both mips16 and normal MIPS code in a single
1152 object. Therefore, we need to support this type of relaxation at
1153 the same time that we support the relaxation described above. We
1154 use the high bit of the subtype field to distinguish these cases.
1155
1156 The information we store for this type of relaxation is the
1157 argument code found in the opcode file for this relocation, whether
1158 the user explicitly requested a small or extended form, and whether
1159 the relocation is in a jump or jal delay slot. That tells us the
1160 size of the value, and how it should be stored. We also store
1161 whether the fragment is considered to be extended or not. We also
1162 store whether this is known to be a branch to a different section,
1163 whether we have tried to relax this frag yet, and whether we have
1164 ever extended a PC relative fragment because of a shift count. */
25499ac7 1165#define RELAX_MIPS16_ENCODE(type, e2, pic, sym32, nomacro, \
8507b6e7
MR
1166 small, ext, \
1167 dslot, jal_dslot) \
252b5132
RH
1168 (0x80000000 \
1169 | ((type) & 0xff) \
25499ac7
MR
1170 | ((e2) ? 0x100 : 0) \
1171 | ((pic) ? 0x200 : 0) \
1172 | ((sym32) ? 0x400 : 0) \
1173 | ((nomacro) ? 0x800 : 0) \
1174 | ((small) ? 0x1000 : 0) \
1175 | ((ext) ? 0x2000 : 0) \
1176 | ((dslot) ? 0x4000 : 0) \
1177 | ((jal_dslot) ? 0x8000 : 0))
8507b6e7 1178
4a6a3df4 1179#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132 1180#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
25499ac7
MR
1181#define RELAX_MIPS16_E2(i) (((i) & 0x100) != 0)
1182#define RELAX_MIPS16_PIC(i) (((i) & 0x200) != 0)
1183#define RELAX_MIPS16_SYM32(i) (((i) & 0x400) != 0)
1184#define RELAX_MIPS16_NOMACRO(i) (((i) & 0x800) != 0)
1185#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x1000) != 0)
1186#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x2000) != 0)
1187#define RELAX_MIPS16_DSLOT(i) (((i) & 0x4000) != 0)
1188#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x8000) != 0)
1189
1190#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x10000) != 0)
1191#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x10000)
1192#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) & ~0x10000)
1193#define RELAX_MIPS16_ALWAYS_EXTENDED(i) (((i) & 0x20000) != 0)
1194#define RELAX_MIPS16_MARK_ALWAYS_EXTENDED(i) ((i) | 0x20000)
1195#define RELAX_MIPS16_CLEAR_ALWAYS_EXTENDED(i) ((i) & ~0x20000)
1196#define RELAX_MIPS16_MACRO(i) (((i) & 0x40000) != 0)
1197#define RELAX_MIPS16_MARK_MACRO(i) ((i) | 0x40000)
1198#define RELAX_MIPS16_CLEAR_MACRO(i) ((i) & ~0x40000)
885add95 1199
df58fc94
RS
1200/* For microMIPS code, we use relaxation similar to one we use for
1201 MIPS16 code. Some instructions that take immediate values support
1202 two encodings: a small one which takes some small value, and a
1203 larger one which takes a 16 bit value. As some branches also follow
1204 this pattern, relaxing these values is required.
1205
1206 We can assemble both microMIPS and normal MIPS code in a single
1207 object. Therefore, we need to support this type of relaxation at
1208 the same time that we support the relaxation described above. We
1209 use one of the high bits of the subtype field to distinguish these
1210 cases.
1211
1212 The information we store for this type of relaxation is the argument
1213 code found in the opcode file for this relocation, the register
8484fb75
MR
1214 selected as the assembler temporary, whether in the 32-bit
1215 instruction mode, whether the branch is unconditional, whether it is
7bd374a4
MR
1216 compact, whether there is no delay-slot instruction available to fill
1217 in, whether it stores the link address implicitly in $ra, whether
1218 relaxation of out-of-range 32-bit branches to a sequence of
8484fb75
MR
1219 instructions is enabled, and whether the displacement of a branch is
1220 too large to fit as an immediate argument of a 16-bit and a 32-bit
1221 branch, respectively. */
ce8ad872 1222#define RELAX_MICROMIPS_ENCODE(type, at, insn32, pic, \
7bd374a4 1223 uncond, compact, link, nods, \
40209cad
MR
1224 relax32, toofar16, toofar32) \
1225 (0x40000000 \
1226 | ((type) & 0xff) \
1227 | (((at) & 0x1f) << 8) \
8484fb75 1228 | ((insn32) ? 0x2000 : 0) \
ce8ad872
MR
1229 | ((pic) ? 0x4000 : 0) \
1230 | ((uncond) ? 0x8000 : 0) \
1231 | ((compact) ? 0x10000 : 0) \
1232 | ((link) ? 0x20000 : 0) \
1233 | ((nods) ? 0x40000 : 0) \
1234 | ((relax32) ? 0x80000 : 0) \
1235 | ((toofar16) ? 0x100000 : 0) \
1236 | ((toofar32) ? 0x200000 : 0))
df58fc94
RS
1237#define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1238#define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1239#define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
8484fb75 1240#define RELAX_MICROMIPS_INSN32(i) (((i) & 0x2000) != 0)
ce8ad872
MR
1241#define RELAX_MICROMIPS_PIC(i) (((i) & 0x4000) != 0)
1242#define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x8000) != 0)
1243#define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x10000) != 0)
1244#define RELAX_MICROMIPS_LINK(i) (((i) & 0x20000) != 0)
1245#define RELAX_MICROMIPS_NODS(i) (((i) & 0x40000) != 0)
1246#define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x80000) != 0)
1247
1248#define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x100000) != 0)
1249#define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x100000)
1250#define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x100000)
1251#define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x200000) != 0)
1252#define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x200000)
1253#define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x200000)
df58fc94 1254
43c0598f
RS
1255/* Sign-extend 16-bit value X. */
1256#define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1257
885add95
CD
1258/* Is the given value a sign-extended 32-bit value? */
1259#define IS_SEXT_32BIT_NUM(x) \
1260 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1261 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1262
1263/* Is the given value a sign-extended 16-bit value? */
1264#define IS_SEXT_16BIT_NUM(x) \
1265 (((x) &~ (offsetT) 0x7fff) == 0 \
1266 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1267
df58fc94
RS
1268/* Is the given value a sign-extended 12-bit value? */
1269#define IS_SEXT_12BIT_NUM(x) \
1270 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1271
7f3c4072
CM
1272/* Is the given value a sign-extended 9-bit value? */
1273#define IS_SEXT_9BIT_NUM(x) \
1274 (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1275
2051e8c4
MR
1276/* Is the given value a zero-extended 32-bit value? Or a negated one? */
1277#define IS_ZEXT_32BIT_NUM(x) \
1278 (((x) &~ (offsetT) 0xffffffff) == 0 \
1279 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1280
bf12938e
RS
1281/* Extract bits MASK << SHIFT from STRUCT and shift them right
1282 SHIFT places. */
1283#define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1284 (((STRUCT) >> (SHIFT)) & (MASK))
1285
bf12938e 1286/* Extract the operand given by FIELD from mips_cl_insn INSN. */
df58fc94
RS
1287#define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1288 (!(MICROMIPS) \
1289 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1290 : EXTRACT_BITS ((INSN).insn_opcode, \
1291 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
bf12938e
RS
1292#define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1293 EXTRACT_BITS ((INSN).insn_opcode, \
1294 MIPS16OP_MASK_##FIELD, \
1295 MIPS16OP_SH_##FIELD)
5c04167a
RS
1296
1297/* The MIPS16 EXTEND opcode, shifted left 16 places. */
1298#define MIPS16_EXTEND (0xf000U << 16)
4d7206a2 1299\f
df58fc94
RS
1300/* Whether or not we are emitting a branch-likely macro. */
1301static bfd_boolean emit_branch_likely_macro = FALSE;
1302
4d7206a2
RS
1303/* Global variables used when generating relaxable macros. See the
1304 comment above RELAX_ENCODE for more details about how relaxation
1305 is used. */
1306static struct {
1307 /* 0 if we're not emitting a relaxable macro.
1308 1 if we're emitting the first of the two relaxation alternatives.
1309 2 if we're emitting the second alternative. */
1310 int sequence;
1311
1312 /* The first relaxable fixup in the current frag. (In other words,
1313 the first fixup that refers to relaxable code.) */
1314 fixS *first_fixup;
1315
1316 /* sizes[0] says how many bytes of the first alternative are stored in
1317 the current frag. Likewise sizes[1] for the second alternative. */
1318 unsigned int sizes[2];
1319
1320 /* The symbol on which the choice of sequence depends. */
1321 symbolS *symbol;
1322} mips_relax;
252b5132 1323\f
584892a6
RS
1324/* Global variables used to decide whether a macro needs a warning. */
1325static struct {
1326 /* True if the macro is in a branch delay slot. */
1327 bfd_boolean delay_slot_p;
1328
df58fc94
RS
1329 /* Set to the length in bytes required if the macro is in a delay slot
1330 that requires a specific length of instruction, otherwise zero. */
1331 unsigned int delay_slot_length;
1332
584892a6
RS
1333 /* For relaxable macros, sizes[0] is the length of the first alternative
1334 in bytes and sizes[1] is the length of the second alternative.
1335 For non-relaxable macros, both elements give the length of the
1336 macro in bytes. */
1337 unsigned int sizes[2];
1338
df58fc94
RS
1339 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1340 instruction of the first alternative in bytes and first_insn_sizes[1]
1341 is the length of the first instruction of the second alternative.
1342 For non-relaxable macros, both elements give the length of the first
1343 instruction in bytes.
1344
1345 Set to zero if we haven't yet seen the first instruction. */
1346 unsigned int first_insn_sizes[2];
1347
1348 /* For relaxable macros, insns[0] is the number of instructions for the
1349 first alternative and insns[1] is the number of instructions for the
1350 second alternative.
1351
1352 For non-relaxable macros, both elements give the number of
1353 instructions for the macro. */
1354 unsigned int insns[2];
1355
584892a6
RS
1356 /* The first variant frag for this macro. */
1357 fragS *first_frag;
1358} mips_macro_warning;
1359\f
252b5132
RH
1360/* Prototypes for static functions. */
1361
252b5132
RH
1362enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1363
b34976b6 1364static void append_insn
df58fc94
RS
1365 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1366 bfd_boolean expansionp);
7d10b47d 1367static void mips_no_prev_insn (void);
c67a084a 1368static void macro_build (expressionS *, const char *, const char *, ...);
b34976b6 1369static void mips16_macro_build
03ea81db 1370 (expressionS *, const char *, const char *, va_list *);
67c0d1eb 1371static void load_register (int, expressionS *, int);
584892a6
RS
1372static void macro_start (void);
1373static void macro_end (void);
833794fc 1374static void macro (struct mips_cl_insn *ip, char *str);
17a2f251 1375static void mips16_macro (struct mips_cl_insn * ip);
17a2f251
TS
1376static void mips_ip (char *str, struct mips_cl_insn * ip);
1377static void mips16_ip (char *str, struct mips_cl_insn * ip);
25499ac7 1378static unsigned long mips16_immed_extend (offsetT, unsigned int);
b34976b6 1379static void mips16_immed
3b4dbbbf 1380 (const char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
43c0598f 1381 unsigned int, unsigned long *);
5e0116d5 1382static size_t my_getSmallExpression
17a2f251
TS
1383 (expressionS *, bfd_reloc_code_real_type *, char *);
1384static void my_getExpression (expressionS *, char *);
1385static void s_align (int);
1386static void s_change_sec (int);
1387static void s_change_section (int);
1388static void s_cons (int);
1389static void s_float_cons (int);
1390static void s_mips_globl (int);
1391static void s_option (int);
1392static void s_mipsset (int);
1393static void s_abicalls (int);
1394static void s_cpload (int);
1395static void s_cpsetup (int);
1396static void s_cplocal (int);
1397static void s_cprestore (int);
1398static void s_cpreturn (int);
741d6ea8
JM
1399static void s_dtprelword (int);
1400static void s_dtpreldword (int);
d0f13682
CLT
1401static void s_tprelword (int);
1402static void s_tpreldword (int);
17a2f251
TS
1403static void s_gpvalue (int);
1404static void s_gpword (int);
1405static void s_gpdword (int);
a3f278e2 1406static void s_ehword (int);
17a2f251
TS
1407static void s_cpadd (int);
1408static void s_insn (int);
ba92f887 1409static void s_nan (int);
919731af 1410static void s_module (int);
17a2f251
TS
1411static void s_mips_ent (int);
1412static void s_mips_end (int);
1413static void s_mips_frame (int);
1414static void s_mips_mask (int reg_type);
1415static void s_mips_stab (int);
1416static void s_mips_weakext (int);
1417static void s_mips_file (int);
1418static void s_mips_loc (int);
9e009953 1419static bfd_boolean pic_need_relax (symbolS *);
4a6a3df4 1420static int relaxed_branch_length (fragS *, asection *, int);
df58fc94
RS
1421static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1422static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
919731af 1423static void file_mips_check_options (void);
e7af610e
NC
1424
1425/* Table and functions used to map between CPU/ISA names, and
1426 ISA levels, and CPU numbers. */
1427
e972090a
NC
1428struct mips_cpu_info
1429{
e7af610e 1430 const char *name; /* CPU or ISA name. */
d16afab6
RS
1431 int flags; /* MIPS_CPU_* flags. */
1432 int ase; /* Set of ASEs implemented by the CPU. */
e7af610e
NC
1433 int isa; /* ISA level. */
1434 int cpu; /* CPU number (default CPU if ISA). */
1435};
1436
ad3fea08 1437#define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
ad3fea08 1438
17a2f251
TS
1439static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1440static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1441static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132 1442\f
c31f3936
RS
1443/* Command-line options. */
1444const char *md_shortopts = "O::g::G:";
1445
1446enum options
1447 {
1448 OPTION_MARCH = OPTION_MD_BASE,
1449 OPTION_MTUNE,
1450 OPTION_MIPS1,
1451 OPTION_MIPS2,
1452 OPTION_MIPS3,
1453 OPTION_MIPS4,
1454 OPTION_MIPS5,
1455 OPTION_MIPS32,
1456 OPTION_MIPS64,
1457 OPTION_MIPS32R2,
ae52f483
AB
1458 OPTION_MIPS32R3,
1459 OPTION_MIPS32R5,
7361da2c 1460 OPTION_MIPS32R6,
c31f3936 1461 OPTION_MIPS64R2,
ae52f483
AB
1462 OPTION_MIPS64R3,
1463 OPTION_MIPS64R5,
7361da2c 1464 OPTION_MIPS64R6,
c31f3936
RS
1465 OPTION_MIPS16,
1466 OPTION_NO_MIPS16,
1467 OPTION_MIPS3D,
1468 OPTION_NO_MIPS3D,
1469 OPTION_MDMX,
1470 OPTION_NO_MDMX,
1471 OPTION_DSP,
1472 OPTION_NO_DSP,
1473 OPTION_MT,
1474 OPTION_NO_MT,
1475 OPTION_VIRT,
1476 OPTION_NO_VIRT,
56d438b1
CF
1477 OPTION_MSA,
1478 OPTION_NO_MSA,
c31f3936
RS
1479 OPTION_SMARTMIPS,
1480 OPTION_NO_SMARTMIPS,
1481 OPTION_DSPR2,
1482 OPTION_NO_DSPR2,
8f4f9071
MF
1483 OPTION_DSPR3,
1484 OPTION_NO_DSPR3,
c31f3936
RS
1485 OPTION_EVA,
1486 OPTION_NO_EVA,
7d64c587
AB
1487 OPTION_XPA,
1488 OPTION_NO_XPA,
c31f3936
RS
1489 OPTION_MICROMIPS,
1490 OPTION_NO_MICROMIPS,
1491 OPTION_MCU,
1492 OPTION_NO_MCU,
25499ac7
MR
1493 OPTION_MIPS16E2,
1494 OPTION_NO_MIPS16E2,
730c3174
SE
1495 OPTION_CRC,
1496 OPTION_NO_CRC,
c31f3936
RS
1497 OPTION_M4650,
1498 OPTION_NO_M4650,
1499 OPTION_M4010,
1500 OPTION_NO_M4010,
1501 OPTION_M4100,
1502 OPTION_NO_M4100,
1503 OPTION_M3900,
1504 OPTION_NO_M3900,
1505 OPTION_M7000_HILO_FIX,
1506 OPTION_MNO_7000_HILO_FIX,
1507 OPTION_FIX_24K,
1508 OPTION_NO_FIX_24K,
a8d14a88
CM
1509 OPTION_FIX_RM7000,
1510 OPTION_NO_FIX_RM7000,
6f2117ba
PH
1511 OPTION_FIX_LOONGSON3_LLSC,
1512 OPTION_NO_FIX_LOONGSON3_LLSC,
c31f3936
RS
1513 OPTION_FIX_LOONGSON2F_JUMP,
1514 OPTION_NO_FIX_LOONGSON2F_JUMP,
1515 OPTION_FIX_LOONGSON2F_NOP,
1516 OPTION_NO_FIX_LOONGSON2F_NOP,
1517 OPTION_FIX_VR4120,
1518 OPTION_NO_FIX_VR4120,
1519 OPTION_FIX_VR4130,
1520 OPTION_NO_FIX_VR4130,
1521 OPTION_FIX_CN63XXP1,
1522 OPTION_NO_FIX_CN63XXP1,
27c634e0
FN
1523 OPTION_FIX_R5900,
1524 OPTION_NO_FIX_R5900,
c31f3936
RS
1525 OPTION_TRAP,
1526 OPTION_BREAK,
1527 OPTION_EB,
1528 OPTION_EL,
1529 OPTION_FP32,
1530 OPTION_GP32,
1531 OPTION_CONSTRUCT_FLOATS,
1532 OPTION_NO_CONSTRUCT_FLOATS,
1533 OPTION_FP64,
351cdf24 1534 OPTION_FPXX,
c31f3936
RS
1535 OPTION_GP64,
1536 OPTION_RELAX_BRANCH,
1537 OPTION_NO_RELAX_BRANCH,
8b10b0b3
MR
1538 OPTION_IGNORE_BRANCH_ISA,
1539 OPTION_NO_IGNORE_BRANCH_ISA,
833794fc
MR
1540 OPTION_INSN32,
1541 OPTION_NO_INSN32,
c31f3936
RS
1542 OPTION_MSHARED,
1543 OPTION_MNO_SHARED,
1544 OPTION_MSYM32,
1545 OPTION_MNO_SYM32,
1546 OPTION_SOFT_FLOAT,
1547 OPTION_HARD_FLOAT,
1548 OPTION_SINGLE_FLOAT,
1549 OPTION_DOUBLE_FLOAT,
1550 OPTION_32,
c31f3936
RS
1551 OPTION_CALL_SHARED,
1552 OPTION_CALL_NONPIC,
1553 OPTION_NON_SHARED,
1554 OPTION_XGOT,
1555 OPTION_MABI,
1556 OPTION_N32,
1557 OPTION_64,
1558 OPTION_MDEBUG,
1559 OPTION_NO_MDEBUG,
1560 OPTION_PDR,
1561 OPTION_NO_PDR,
1562 OPTION_MVXWORKS_PIC,
ba92f887 1563 OPTION_NAN,
351cdf24
MF
1564 OPTION_ODD_SPREG,
1565 OPTION_NO_ODD_SPREG,
6f20c942
FS
1566 OPTION_GINV,
1567 OPTION_NO_GINV,
8095d2f7
CX
1568 OPTION_LOONGSON_MMI,
1569 OPTION_NO_LOONGSON_MMI,
716c08de
CX
1570 OPTION_LOONGSON_CAM,
1571 OPTION_NO_LOONGSON_CAM,
bdc6c06e
CX
1572 OPTION_LOONGSON_EXT,
1573 OPTION_NO_LOONGSON_EXT,
a693765e
CX
1574 OPTION_LOONGSON_EXT2,
1575 OPTION_NO_LOONGSON_EXT2,
c31f3936
RS
1576 OPTION_END_OF_ENUM
1577 };
1578
1579struct option md_longopts[] =
1580{
1581 /* Options which specify architecture. */
1582 {"march", required_argument, NULL, OPTION_MARCH},
1583 {"mtune", required_argument, NULL, OPTION_MTUNE},
1584 {"mips0", no_argument, NULL, OPTION_MIPS1},
1585 {"mips1", no_argument, NULL, OPTION_MIPS1},
1586 {"mips2", no_argument, NULL, OPTION_MIPS2},
1587 {"mips3", no_argument, NULL, OPTION_MIPS3},
1588 {"mips4", no_argument, NULL, OPTION_MIPS4},
1589 {"mips5", no_argument, NULL, OPTION_MIPS5},
1590 {"mips32", no_argument, NULL, OPTION_MIPS32},
1591 {"mips64", no_argument, NULL, OPTION_MIPS64},
1592 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
ae52f483
AB
1593 {"mips32r3", no_argument, NULL, OPTION_MIPS32R3},
1594 {"mips32r5", no_argument, NULL, OPTION_MIPS32R5},
7361da2c 1595 {"mips32r6", no_argument, NULL, OPTION_MIPS32R6},
c31f3936 1596 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
ae52f483
AB
1597 {"mips64r3", no_argument, NULL, OPTION_MIPS64R3},
1598 {"mips64r5", no_argument, NULL, OPTION_MIPS64R5},
7361da2c 1599 {"mips64r6", no_argument, NULL, OPTION_MIPS64R6},
c31f3936
RS
1600
1601 /* Options which specify Application Specific Extensions (ASEs). */
1602 {"mips16", no_argument, NULL, OPTION_MIPS16},
1603 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1604 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1605 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1606 {"mdmx", no_argument, NULL, OPTION_MDMX},
1607 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1608 {"mdsp", no_argument, NULL, OPTION_DSP},
1609 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1610 {"mmt", no_argument, NULL, OPTION_MT},
1611 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1612 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1613 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1614 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1615 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
8f4f9071
MF
1616 {"mdspr3", no_argument, NULL, OPTION_DSPR3},
1617 {"mno-dspr3", no_argument, NULL, OPTION_NO_DSPR3},
c31f3936
RS
1618 {"meva", no_argument, NULL, OPTION_EVA},
1619 {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1620 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1621 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1622 {"mmcu", no_argument, NULL, OPTION_MCU},
1623 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1624 {"mvirt", no_argument, NULL, OPTION_VIRT},
1625 {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
56d438b1
CF
1626 {"mmsa", no_argument, NULL, OPTION_MSA},
1627 {"mno-msa", no_argument, NULL, OPTION_NO_MSA},
7d64c587
AB
1628 {"mxpa", no_argument, NULL, OPTION_XPA},
1629 {"mno-xpa", no_argument, NULL, OPTION_NO_XPA},
25499ac7
MR
1630 {"mmips16e2", no_argument, NULL, OPTION_MIPS16E2},
1631 {"mno-mips16e2", no_argument, NULL, OPTION_NO_MIPS16E2},
730c3174
SE
1632 {"mcrc", no_argument, NULL, OPTION_CRC},
1633 {"mno-crc", no_argument, NULL, OPTION_NO_CRC},
6f20c942
FS
1634 {"mginv", no_argument, NULL, OPTION_GINV},
1635 {"mno-ginv", no_argument, NULL, OPTION_NO_GINV},
8095d2f7
CX
1636 {"mloongson-mmi", no_argument, NULL, OPTION_LOONGSON_MMI},
1637 {"mno-loongson-mmi", no_argument, NULL, OPTION_NO_LOONGSON_MMI},
716c08de
CX
1638 {"mloongson-cam", no_argument, NULL, OPTION_LOONGSON_CAM},
1639 {"mno-loongson-cam", no_argument, NULL, OPTION_NO_LOONGSON_CAM},
bdc6c06e
CX
1640 {"mloongson-ext", no_argument, NULL, OPTION_LOONGSON_EXT},
1641 {"mno-loongson-ext", no_argument, NULL, OPTION_NO_LOONGSON_EXT},
a693765e
CX
1642 {"mloongson-ext2", no_argument, NULL, OPTION_LOONGSON_EXT2},
1643 {"mno-loongson-ext2", no_argument, NULL, OPTION_NO_LOONGSON_EXT2},
c31f3936
RS
1644
1645 /* Old-style architecture options. Don't add more of these. */
1646 {"m4650", no_argument, NULL, OPTION_M4650},
1647 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1648 {"m4010", no_argument, NULL, OPTION_M4010},
1649 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1650 {"m4100", no_argument, NULL, OPTION_M4100},
1651 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1652 {"m3900", no_argument, NULL, OPTION_M3900},
1653 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1654
1655 /* Options which enable bug fixes. */
1656 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1657 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1658 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
6f2117ba
PH
1659 {"mfix-loongson3-llsc", no_argument, NULL, OPTION_FIX_LOONGSON3_LLSC},
1660 {"mno-fix-loongson3-llsc", no_argument, NULL, OPTION_NO_FIX_LOONGSON3_LLSC},
c31f3936
RS
1661 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1662 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1663 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1664 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1665 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
1666 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1667 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
1668 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1669 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
1670 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
a8d14a88
CM
1671 {"mfix-rm7000", no_argument, NULL, OPTION_FIX_RM7000},
1672 {"mno-fix-rm7000", no_argument, NULL, OPTION_NO_FIX_RM7000},
c31f3936
RS
1673 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1674 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
27c634e0
FN
1675 {"mfix-r5900", no_argument, NULL, OPTION_FIX_R5900},
1676 {"mno-fix-r5900", no_argument, NULL, OPTION_NO_FIX_R5900},
c31f3936
RS
1677
1678 /* Miscellaneous options. */
1679 {"trap", no_argument, NULL, OPTION_TRAP},
1680 {"no-break", no_argument, NULL, OPTION_TRAP},
1681 {"break", no_argument, NULL, OPTION_BREAK},
1682 {"no-trap", no_argument, NULL, OPTION_BREAK},
1683 {"EB", no_argument, NULL, OPTION_EB},
1684 {"EL", no_argument, NULL, OPTION_EL},
1685 {"mfp32", no_argument, NULL, OPTION_FP32},
1686 {"mgp32", no_argument, NULL, OPTION_GP32},
1687 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1688 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1689 {"mfp64", no_argument, NULL, OPTION_FP64},
351cdf24 1690 {"mfpxx", no_argument, NULL, OPTION_FPXX},
c31f3936
RS
1691 {"mgp64", no_argument, NULL, OPTION_GP64},
1692 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1693 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
8b10b0b3
MR
1694 {"mignore-branch-isa", no_argument, NULL, OPTION_IGNORE_BRANCH_ISA},
1695 {"mno-ignore-branch-isa", no_argument, NULL, OPTION_NO_IGNORE_BRANCH_ISA},
833794fc
MR
1696 {"minsn32", no_argument, NULL, OPTION_INSN32},
1697 {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
c31f3936
RS
1698 {"mshared", no_argument, NULL, OPTION_MSHARED},
1699 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1700 {"msym32", no_argument, NULL, OPTION_MSYM32},
1701 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1702 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1703 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1704 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1705 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
351cdf24
MF
1706 {"modd-spreg", no_argument, NULL, OPTION_ODD_SPREG},
1707 {"mno-odd-spreg", no_argument, NULL, OPTION_NO_ODD_SPREG},
c31f3936
RS
1708
1709 /* Strictly speaking this next option is ELF specific,
1710 but we allow it for other ports as well in order to
1711 make testing easier. */
1712 {"32", no_argument, NULL, OPTION_32},
1713
1714 /* ELF-specific options. */
c31f3936
RS
1715 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1716 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1717 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1718 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
1719 {"xgot", no_argument, NULL, OPTION_XGOT},
1720 {"mabi", required_argument, NULL, OPTION_MABI},
1721 {"n32", no_argument, NULL, OPTION_N32},
1722 {"64", no_argument, NULL, OPTION_64},
1723 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1724 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1725 {"mpdr", no_argument, NULL, OPTION_PDR},
1726 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1727 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
ba92f887 1728 {"mnan", required_argument, NULL, OPTION_NAN},
c31f3936
RS
1729
1730 {NULL, no_argument, NULL, 0}
1731};
1732size_t md_longopts_size = sizeof (md_longopts);
1733\f
c6278170
RS
1734/* Information about either an Application Specific Extension or an
1735 optional architecture feature that, for simplicity, we treat in the
1736 same way as an ASE. */
1737struct mips_ase
1738{
1739 /* The name of the ASE, used in both the command-line and .set options. */
1740 const char *name;
1741
1742 /* The associated ASE_* flags. If the ASE is available on both 32-bit
1743 and 64-bit architectures, the flags here refer to the subset that
1744 is available on both. */
1745 unsigned int flags;
1746
1747 /* The ASE_* flag used for instructions that are available on 64-bit
1748 architectures but that are not included in FLAGS. */
1749 unsigned int flags64;
1750
1751 /* The command-line options that turn the ASE on and off. */
1752 int option_on;
1753 int option_off;
1754
1755 /* The minimum required architecture revisions for MIPS32, MIPS64,
1756 microMIPS32 and microMIPS64, or -1 if the extension isn't supported. */
1757 int mips32_rev;
1758 int mips64_rev;
1759 int micromips32_rev;
1760 int micromips64_rev;
7361da2c
AB
1761
1762 /* The architecture where the ASE was removed or -1 if the extension has not
1763 been removed. */
1764 int rem_rev;
c6278170
RS
1765};
1766
1767/* A table of all supported ASEs. */
1768static const struct mips_ase mips_ases[] = {
1769 { "dsp", ASE_DSP, ASE_DSP64,
1770 OPTION_DSP, OPTION_NO_DSP,
7361da2c
AB
1771 2, 2, 2, 2,
1772 -1 },
c6278170
RS
1773
1774 { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1775 OPTION_DSPR2, OPTION_NO_DSPR2,
7361da2c
AB
1776 2, 2, 2, 2,
1777 -1 },
c6278170 1778
8f4f9071
MF
1779 { "dspr3", ASE_DSP | ASE_DSPR2 | ASE_DSPR3, 0,
1780 OPTION_DSPR3, OPTION_NO_DSPR3,
1781 6, 6, -1, -1,
1782 -1 },
1783
c6278170
RS
1784 { "eva", ASE_EVA, 0,
1785 OPTION_EVA, OPTION_NO_EVA,
7361da2c
AB
1786 2, 2, 2, 2,
1787 -1 },
c6278170
RS
1788
1789 { "mcu", ASE_MCU, 0,
1790 OPTION_MCU, OPTION_NO_MCU,
7361da2c
AB
1791 2, 2, 2, 2,
1792 -1 },
c6278170
RS
1793
1794 /* Deprecated in MIPS64r5, but we don't implement that yet. */
1795 { "mdmx", ASE_MDMX, 0,
1796 OPTION_MDMX, OPTION_NO_MDMX,
7361da2c
AB
1797 -1, 1, -1, -1,
1798 6 },
c6278170
RS
1799
1800 /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2. */
1801 { "mips3d", ASE_MIPS3D, 0,
1802 OPTION_MIPS3D, OPTION_NO_MIPS3D,
7361da2c
AB
1803 2, 1, -1, -1,
1804 6 },
c6278170
RS
1805
1806 { "mt", ASE_MT, 0,
1807 OPTION_MT, OPTION_NO_MT,
7361da2c
AB
1808 2, 2, -1, -1,
1809 -1 },
c6278170
RS
1810
1811 { "smartmips", ASE_SMARTMIPS, 0,
1812 OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
7361da2c
AB
1813 1, -1, -1, -1,
1814 6 },
c6278170
RS
1815
1816 { "virt", ASE_VIRT, ASE_VIRT64,
1817 OPTION_VIRT, OPTION_NO_VIRT,
7361da2c
AB
1818 2, 2, 2, 2,
1819 -1 },
56d438b1
CF
1820
1821 { "msa", ASE_MSA, ASE_MSA64,
1822 OPTION_MSA, OPTION_NO_MSA,
7361da2c
AB
1823 2, 2, 2, 2,
1824 -1 },
7d64c587
AB
1825
1826 { "xpa", ASE_XPA, 0,
1827 OPTION_XPA, OPTION_NO_XPA,
909b4e3d 1828 2, 2, 2, 2,
7361da2c 1829 -1 },
25499ac7
MR
1830
1831 { "mips16e2", ASE_MIPS16E2, 0,
1832 OPTION_MIPS16E2, OPTION_NO_MIPS16E2,
1833 2, 2, -1, -1,
1834 6 },
730c3174
SE
1835
1836 { "crc", ASE_CRC, ASE_CRC64,
1837 OPTION_CRC, OPTION_NO_CRC,
1838 6, 6, -1, -1,
1839 -1 },
6f20c942
FS
1840
1841 { "ginv", ASE_GINV, 0,
1842 OPTION_GINV, OPTION_NO_GINV,
1843 6, 6, 6, 6,
1844 -1 },
8095d2f7
CX
1845
1846 { "loongson-mmi", ASE_LOONGSON_MMI, 0,
1847 OPTION_LOONGSON_MMI, OPTION_NO_LOONGSON_MMI,
1848 0, 0, -1, -1,
1849 -1 },
716c08de
CX
1850
1851 { "loongson-cam", ASE_LOONGSON_CAM, 0,
1852 OPTION_LOONGSON_CAM, OPTION_NO_LOONGSON_CAM,
1853 0, 0, -1, -1,
1854 -1 },
bdc6c06e
CX
1855
1856 { "loongson-ext", ASE_LOONGSON_EXT, 0,
1857 OPTION_LOONGSON_EXT, OPTION_NO_LOONGSON_EXT,
1858 0, 0, -1, -1,
1859 -1 },
a693765e
CX
1860
1861 { "loongson-ext2", ASE_LOONGSON_EXT | ASE_LOONGSON_EXT2, 0,
1862 OPTION_LOONGSON_EXT2, OPTION_NO_LOONGSON_EXT2,
1863 0, 0, -1, -1,
1864 -1 },
c6278170
RS
1865};
1866
1867/* The set of ASEs that require -mfp64. */
82bda27b 1868#define FP64_ASES (ASE_MIPS3D | ASE_MDMX | ASE_MSA)
c6278170
RS
1869
1870/* Groups of ASE_* flags that represent different revisions of an ASE. */
1871static const unsigned int mips_ase_groups[] = {
a693765e
CX
1872 ASE_DSP | ASE_DSPR2 | ASE_DSPR3,
1873 ASE_LOONGSON_EXT | ASE_LOONGSON_EXT2
c6278170
RS
1874};
1875\f
252b5132
RH
1876/* Pseudo-op table.
1877
1878 The following pseudo-ops from the Kane and Heinrich MIPS book
1879 should be defined here, but are currently unsupported: .alias,
1880 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1881
1882 The following pseudo-ops from the Kane and Heinrich MIPS book are
1883 specific to the type of debugging information being generated, and
1884 should be defined by the object format: .aent, .begin, .bend,
1885 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1886 .vreg.
1887
1888 The following pseudo-ops from the Kane and Heinrich MIPS book are
1889 not MIPS CPU specific, but are also not specific to the object file
1890 format. This file is probably the best place to define them, but
d84bcf09 1891 they are not currently supported: .asm0, .endr, .lab, .struct. */
252b5132 1892
e972090a
NC
1893static const pseudo_typeS mips_pseudo_table[] =
1894{
beae10d5 1895 /* MIPS specific pseudo-ops. */
252b5132
RH
1896 {"option", s_option, 0},
1897 {"set", s_mipsset, 0},
1898 {"rdata", s_change_sec, 'r'},
1899 {"sdata", s_change_sec, 's'},
1900 {"livereg", s_ignore, 0},
1901 {"abicalls", s_abicalls, 0},
1902 {"cpload", s_cpload, 0},
6478892d
TS
1903 {"cpsetup", s_cpsetup, 0},
1904 {"cplocal", s_cplocal, 0},
252b5132 1905 {"cprestore", s_cprestore, 0},
6478892d 1906 {"cpreturn", s_cpreturn, 0},
741d6ea8
JM
1907 {"dtprelword", s_dtprelword, 0},
1908 {"dtpreldword", s_dtpreldword, 0},
d0f13682
CLT
1909 {"tprelword", s_tprelword, 0},
1910 {"tpreldword", s_tpreldword, 0},
6478892d 1911 {"gpvalue", s_gpvalue, 0},
252b5132 1912 {"gpword", s_gpword, 0},
10181a0d 1913 {"gpdword", s_gpdword, 0},
a3f278e2 1914 {"ehword", s_ehword, 0},
252b5132
RH
1915 {"cpadd", s_cpadd, 0},
1916 {"insn", s_insn, 0},
ba92f887 1917 {"nan", s_nan, 0},
919731af 1918 {"module", s_module, 0},
252b5132 1919
beae10d5 1920 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132 1921 chips. */
38a57ae7 1922 {"asciiz", stringer, 8 + 1},
252b5132
RH
1923 {"bss", s_change_sec, 'b'},
1924 {"err", s_err, 0},
1925 {"half", s_cons, 1},
1926 {"dword", s_cons, 3},
1927 {"weakext", s_mips_weakext, 0},
7c752c2a
TS
1928 {"origin", s_org, 0},
1929 {"repeat", s_rept, 0},
252b5132 1930
998b3c36
MR
1931 /* For MIPS this is non-standard, but we define it for consistency. */
1932 {"sbss", s_change_sec, 'B'},
1933
beae10d5 1934 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1935 here for one reason or another. */
1936 {"align", s_align, 0},
1937 {"byte", s_cons, 0},
1938 {"data", s_change_sec, 'd'},
1939 {"double", s_float_cons, 'd'},
1940 {"float", s_float_cons, 'f'},
1941 {"globl", s_mips_globl, 0},
1942 {"global", s_mips_globl, 0},
1943 {"hword", s_cons, 1},
1944 {"int", s_cons, 2},
1945 {"long", s_cons, 2},
1946 {"octa", s_cons, 4},
1947 {"quad", s_cons, 3},
cca86cc8 1948 {"section", s_change_section, 0},
252b5132
RH
1949 {"short", s_cons, 1},
1950 {"single", s_float_cons, 'f'},
754e2bb9 1951 {"stabd", s_mips_stab, 'd'},
252b5132 1952 {"stabn", s_mips_stab, 'n'},
754e2bb9 1953 {"stabs", s_mips_stab, 's'},
252b5132
RH
1954 {"text", s_change_sec, 't'},
1955 {"word", s_cons, 2},
add56521 1956
add56521 1957 { "extern", ecoff_directive_extern, 0},
add56521 1958
43841e91 1959 { NULL, NULL, 0 },
252b5132
RH
1960};
1961
e972090a
NC
1962static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1963{
beae10d5
KH
1964 /* These pseudo-ops should be defined by the object file format.
1965 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1966 {"aent", s_mips_ent, 1},
1967 {"bgnb", s_ignore, 0},
1968 {"end", s_mips_end, 0},
1969 {"endb", s_ignore, 0},
1970 {"ent", s_mips_ent, 0},
c5dd6aab 1971 {"file", s_mips_file, 0},
252b5132
RH
1972 {"fmask", s_mips_mask, 'F'},
1973 {"frame", s_mips_frame, 0},
c5dd6aab 1974 {"loc", s_mips_loc, 0},
252b5132
RH
1975 {"mask", s_mips_mask, 'R'},
1976 {"verstamp", s_ignore, 0},
43841e91 1977 { NULL, NULL, 0 },
252b5132
RH
1978};
1979
3ae8dd8d
MR
1980/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1981 purpose of the `.dc.a' internal pseudo-op. */
1982
1983int
1984mips_address_bytes (void)
1985{
919731af 1986 file_mips_check_options ();
3ae8dd8d
MR
1987 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1988}
1989
17a2f251 1990extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1991
1992void
17a2f251 1993mips_pop_insert (void)
252b5132
RH
1994{
1995 pop_insert (mips_pseudo_table);
1996 if (! ECOFF_DEBUGGING)
1997 pop_insert (mips_nonecoff_pseudo_table);
1998}
1999\f
2000/* Symbols labelling the current insn. */
2001
e972090a
NC
2002struct insn_label_list
2003{
252b5132
RH
2004 struct insn_label_list *next;
2005 symbolS *label;
2006};
2007
252b5132 2008static struct insn_label_list *free_insn_labels;
742a56fe 2009#define label_list tc_segment_info_data.labels
252b5132 2010
17a2f251 2011static void mips_clear_insn_labels (void);
df58fc94
RS
2012static void mips_mark_labels (void);
2013static void mips_compressed_mark_labels (void);
252b5132
RH
2014
2015static inline void
17a2f251 2016mips_clear_insn_labels (void)
252b5132 2017{
ed9e98c2 2018 struct insn_label_list **pl;
a8dbcb85 2019 segment_info_type *si;
252b5132 2020
a8dbcb85
TS
2021 if (now_seg)
2022 {
2023 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
2024 ;
3739860c 2025
a8dbcb85
TS
2026 si = seg_info (now_seg);
2027 *pl = si->label_list;
2028 si->label_list = NULL;
2029 }
252b5132 2030}
a8dbcb85 2031
df58fc94
RS
2032/* Mark instruction labels in MIPS16/microMIPS mode. */
2033
2034static inline void
2035mips_mark_labels (void)
2036{
2037 if (HAVE_CODE_COMPRESSION)
2038 mips_compressed_mark_labels ();
2039}
252b5132
RH
2040\f
2041static char *expr_end;
2042
e423441d 2043/* An expression in a macro instruction. This is set by mips_ip and
b0e6f033 2044 mips16_ip and when populated is always an O_constant. */
252b5132
RH
2045
2046static expressionS imm_expr;
252b5132 2047
77bd4346
RS
2048/* The relocatable field in an instruction and the relocs associated
2049 with it. These variables are used for instructions like LUI and
2050 JAL as well as true offsets. They are also used for address
2051 operands in macros. */
252b5132 2052
77bd4346 2053static expressionS offset_expr;
f6688943
TS
2054static bfd_reloc_code_real_type offset_reloc[3]
2055 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 2056
df58fc94
RS
2057/* This is set to the resulting size of the instruction to be produced
2058 by mips16_ip if an explicit extension is used or by mips_ip if an
2059 explicit size is supplied. */
252b5132 2060
df58fc94 2061static unsigned int forced_insn_length;
252b5132 2062
e1b47bd5
RS
2063/* True if we are assembling an instruction. All dot symbols defined during
2064 this time should be treated as code labels. */
2065
2066static bfd_boolean mips_assembling_insn;
2067
ecb4347a
DJ
2068/* The pdr segment for per procedure frame/regmask info. Not used for
2069 ECOFF debugging. */
252b5132
RH
2070
2071static segT pdr_seg;
252b5132 2072
e013f690
TS
2073/* The default target format to use. */
2074
aeffff67
RS
2075#if defined (TE_FreeBSD)
2076#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
2077#elif defined (TE_TMIPS)
2078#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
2079#else
2080#define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
2081#endif
2082
e013f690 2083const char *
17a2f251 2084mips_target_format (void)
e013f690
TS
2085{
2086 switch (OUTPUT_FLAVOR)
2087 {
e013f690 2088 case bfd_target_elf_flavour:
0a44bf69
RS
2089#ifdef TE_VXWORKS
2090 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
2091 return (target_big_endian
2092 ? "elf32-bigmips-vxworks"
2093 : "elf32-littlemips-vxworks");
2094#endif
e013f690 2095 return (target_big_endian
cfe86eaa 2096 ? (HAVE_64BIT_OBJECTS
aeffff67 2097 ? ELF_TARGET ("elf64-", "big")
cfe86eaa 2098 : (HAVE_NEWABI
aeffff67
RS
2099 ? ELF_TARGET ("elf32-n", "big")
2100 : ELF_TARGET ("elf32-", "big")))
cfe86eaa 2101 : (HAVE_64BIT_OBJECTS
aeffff67 2102 ? ELF_TARGET ("elf64-", "little")
cfe86eaa 2103 : (HAVE_NEWABI
aeffff67
RS
2104 ? ELF_TARGET ("elf32-n", "little")
2105 : ELF_TARGET ("elf32-", "little"))));
e013f690
TS
2106 default:
2107 abort ();
2108 return NULL;
2109 }
2110}
2111
c6278170
RS
2112/* Return the ISA revision that is currently in use, or 0 if we are
2113 generating code for MIPS V or below. */
2114
2115static int
2116mips_isa_rev (void)
2117{
2118 if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
2119 return 2;
2120
ae52f483
AB
2121 if (mips_opts.isa == ISA_MIPS32R3 || mips_opts.isa == ISA_MIPS64R3)
2122 return 3;
2123
2124 if (mips_opts.isa == ISA_MIPS32R5 || mips_opts.isa == ISA_MIPS64R5)
2125 return 5;
2126
7361da2c
AB
2127 if (mips_opts.isa == ISA_MIPS32R6 || mips_opts.isa == ISA_MIPS64R6)
2128 return 6;
2129
c6278170
RS
2130 /* microMIPS implies revision 2 or above. */
2131 if (mips_opts.micromips)
2132 return 2;
2133
2134 if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
2135 return 1;
2136
2137 return 0;
2138}
2139
2140/* Return the mask of all ASEs that are revisions of those in FLAGS. */
2141
2142static unsigned int
2143mips_ase_mask (unsigned int flags)
2144{
2145 unsigned int i;
2146
2147 for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
2148 if (flags & mips_ase_groups[i])
2149 flags |= mips_ase_groups[i];
2150 return flags;
2151}
2152
2153/* Check whether the current ISA supports ASE. Issue a warning if
2154 appropriate. */
2155
2156static void
2157mips_check_isa_supports_ase (const struct mips_ase *ase)
2158{
2159 const char *base;
2160 int min_rev, size;
2161 static unsigned int warned_isa;
2162 static unsigned int warned_fp32;
2163
2164 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
2165 min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
2166 else
2167 min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
2168 if ((min_rev < 0 || mips_isa_rev () < min_rev)
2169 && (warned_isa & ase->flags) != ase->flags)
2170 {
2171 warned_isa |= ase->flags;
2172 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2173 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2174 if (min_rev < 0)
1661c76c 2175 as_warn (_("the %d-bit %s architecture does not support the"
c6278170
RS
2176 " `%s' extension"), size, base, ase->name);
2177 else
1661c76c 2178 as_warn (_("the `%s' extension requires %s%d revision %d or greater"),
c6278170
RS
2179 ase->name, base, size, min_rev);
2180 }
7361da2c
AB
2181 else if ((ase->rem_rev > 0 && mips_isa_rev () >= ase->rem_rev)
2182 && (warned_isa & ase->flags) != ase->flags)
2183 {
2184 warned_isa |= ase->flags;
2185 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2186 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2187 as_warn (_("the `%s' extension was removed in %s%d revision %d"),
2188 ase->name, base, size, ase->rem_rev);
2189 }
2190
c6278170 2191 if ((ase->flags & FP64_ASES)
0b35dfee 2192 && mips_opts.fp != 64
c6278170
RS
2193 && (warned_fp32 & ase->flags) != ase->flags)
2194 {
2195 warned_fp32 |= ase->flags;
1661c76c 2196 as_warn (_("the `%s' extension requires 64-bit FPRs"), ase->name);
c6278170
RS
2197 }
2198}
2199
2200/* Check all enabled ASEs to see whether they are supported by the
2201 chosen architecture. */
2202
2203static void
2204mips_check_isa_supports_ases (void)
2205{
2206 unsigned int i, mask;
2207
2208 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2209 {
2210 mask = mips_ase_mask (mips_ases[i].flags);
2211 if ((mips_opts.ase & mask) == mips_ases[i].flags)
2212 mips_check_isa_supports_ase (&mips_ases[i]);
2213 }
2214}
2215
2216/* Set the state of ASE to ENABLED_P. Return the mask of ASE_* flags
2217 that were affected. */
2218
2219static unsigned int
919731af 2220mips_set_ase (const struct mips_ase *ase, struct mips_set_options *opts,
2221 bfd_boolean enabled_p)
c6278170
RS
2222{
2223 unsigned int mask;
2224
2225 mask = mips_ase_mask (ase->flags);
919731af 2226 opts->ase &= ~mask;
92cebb3d
MR
2227
2228 /* Clear combination ASE flags, which need to be recalculated based on
2229 updated regular ASE settings. */
41cee089 2230 opts->ase &= ~(ASE_MIPS16E2_MT | ASE_XPA_VIRT | ASE_EVA_R6);
92cebb3d 2231
c6278170 2232 if (enabled_p)
919731af 2233 opts->ase |= ase->flags;
25499ac7 2234
9785fc2a
MR
2235 /* The Virtualization ASE has eXtended Physical Addressing (XPA)
2236 instructions which are only valid when both ASEs are enabled.
2237 This sets the ASE_XPA_VIRT flag when both ASEs are present. */
2238 if ((opts->ase & (ASE_XPA | ASE_VIRT)) == (ASE_XPA | ASE_VIRT))
2239 {
2240 opts->ase |= ASE_XPA_VIRT;
2241 mask |= ASE_XPA_VIRT;
2242 }
25499ac7
MR
2243 if ((opts->ase & (ASE_MIPS16E2 | ASE_MT)) == (ASE_MIPS16E2 | ASE_MT))
2244 {
2245 opts->ase |= ASE_MIPS16E2_MT;
2246 mask |= ASE_MIPS16E2_MT;
2247 }
2248
41cee089
FS
2249 /* The EVA Extension has instructions which are only valid when the R6 ISA
2250 is enabled. This sets the ASE_EVA_R6 flag when both EVA and R6 ISA are
2251 present. */
2252 if (((opts->ase & ASE_EVA) != 0) && ISA_IS_R6 (opts->isa))
2253 {
2254 opts->ase |= ASE_EVA_R6;
2255 mask |= ASE_EVA_R6;
2256 }
2257
c6278170
RS
2258 return mask;
2259}
2260
2261/* Return the ASE called NAME, or null if none. */
2262
2263static const struct mips_ase *
2264mips_lookup_ase (const char *name)
2265{
2266 unsigned int i;
2267
2268 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2269 if (strcmp (name, mips_ases[i].name) == 0)
2270 return &mips_ases[i];
2271 return NULL;
2272}
2273
df58fc94 2274/* Return the length of a microMIPS instruction in bytes. If bits of
100b4f2e
MR
2275 the mask beyond the low 16 are 0, then it is a 16-bit instruction,
2276 otherwise it is a 32-bit instruction. */
df58fc94
RS
2277
2278static inline unsigned int
2279micromips_insn_length (const struct mips_opcode *mo)
2280{
7fd53920 2281 return mips_opcode_32bit_p (mo) ? 4 : 2;
df58fc94
RS
2282}
2283
5c04167a
RS
2284/* Return the length of MIPS16 instruction OPCODE. */
2285
2286static inline unsigned int
2287mips16_opcode_length (unsigned long opcode)
2288{
2289 return (opcode >> 16) == 0 ? 2 : 4;
2290}
2291
1e915849
RS
2292/* Return the length of instruction INSN. */
2293
2294static inline unsigned int
2295insn_length (const struct mips_cl_insn *insn)
2296{
df58fc94
RS
2297 if (mips_opts.micromips)
2298 return micromips_insn_length (insn->insn_mo);
2299 else if (mips_opts.mips16)
5c04167a 2300 return mips16_opcode_length (insn->insn_opcode);
df58fc94 2301 else
1e915849 2302 return 4;
1e915849
RS
2303}
2304
2305/* Initialise INSN from opcode entry MO. Leave its position unspecified. */
2306
2307static void
2308create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
2309{
2310 size_t i;
2311
2312 insn->insn_mo = mo;
1e915849
RS
2313 insn->insn_opcode = mo->match;
2314 insn->frag = NULL;
2315 insn->where = 0;
2316 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2317 insn->fixp[i] = NULL;
2318 insn->fixed_p = (mips_opts.noreorder > 0);
2319 insn->noreorder_p = (mips_opts.noreorder > 0);
2320 insn->mips16_absolute_jump_p = 0;
15be625d 2321 insn->complete_p = 0;
e407c74b 2322 insn->cleared_p = 0;
1e915849
RS
2323}
2324
fc76e730
RS
2325/* Get a list of all the operands in INSN. */
2326
2327static const struct mips_operand_array *
2328insn_operands (const struct mips_cl_insn *insn)
2329{
2330 if (insn->insn_mo >= &mips_opcodes[0]
2331 && insn->insn_mo < &mips_opcodes[NUMOPCODES])
2332 return &mips_operands[insn->insn_mo - &mips_opcodes[0]];
2333
2334 if (insn->insn_mo >= &mips16_opcodes[0]
2335 && insn->insn_mo < &mips16_opcodes[bfd_mips16_num_opcodes])
2336 return &mips16_operands[insn->insn_mo - &mips16_opcodes[0]];
2337
2338 if (insn->insn_mo >= &micromips_opcodes[0]
2339 && insn->insn_mo < &micromips_opcodes[bfd_micromips_num_opcodes])
2340 return &micromips_operands[insn->insn_mo - &micromips_opcodes[0]];
2341
2342 abort ();
2343}
2344
2345/* Get a description of operand OPNO of INSN. */
2346
2347static const struct mips_operand *
2348insn_opno (const struct mips_cl_insn *insn, unsigned opno)
2349{
2350 const struct mips_operand_array *operands;
2351
2352 operands = insn_operands (insn);
2353 if (opno >= MAX_OPERANDS || !operands->operand[opno])
2354 abort ();
2355 return operands->operand[opno];
2356}
2357
e077a1c8
RS
2358/* Install UVAL as the value of OPERAND in INSN. */
2359
2360static inline void
2361insn_insert_operand (struct mips_cl_insn *insn,
2362 const struct mips_operand *operand, unsigned int uval)
2363{
25499ac7
MR
2364 if (mips_opts.mips16
2365 && operand->type == OP_INT && operand->lsb == 0
2366 && mips_opcode_32bit_p (insn->insn_mo))
2367 insn->insn_opcode |= mips16_immed_extend (uval, operand->size);
2368 else
2369 insn->insn_opcode = mips_insert_operand (operand, insn->insn_opcode, uval);
e077a1c8
RS
2370}
2371
fc76e730
RS
2372/* Extract the value of OPERAND from INSN. */
2373
2374static inline unsigned
2375insn_extract_operand (const struct mips_cl_insn *insn,
2376 const struct mips_operand *operand)
2377{
2378 return mips_extract_operand (operand, insn->insn_opcode);
2379}
2380
df58fc94 2381/* Record the current MIPS16/microMIPS mode in now_seg. */
742a56fe
RS
2382
2383static void
df58fc94 2384mips_record_compressed_mode (void)
742a56fe
RS
2385{
2386 segment_info_type *si;
2387
2388 si = seg_info (now_seg);
2389 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2390 si->tc_segment_info_data.mips16 = mips_opts.mips16;
df58fc94
RS
2391 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2392 si->tc_segment_info_data.micromips = mips_opts.micromips;
742a56fe
RS
2393}
2394
4d68580a
RS
2395/* Read a standard MIPS instruction from BUF. */
2396
2397static unsigned long
2398read_insn (char *buf)
2399{
2400 if (target_big_endian)
2401 return bfd_getb32 ((bfd_byte *) buf);
2402 else
2403 return bfd_getl32 ((bfd_byte *) buf);
2404}
2405
2406/* Write standard MIPS instruction INSN to BUF. Return a pointer to
2407 the next byte. */
2408
2409static char *
2410write_insn (char *buf, unsigned int insn)
2411{
2412 md_number_to_chars (buf, insn, 4);
2413 return buf + 4;
2414}
2415
2416/* Read a microMIPS or MIPS16 opcode from BUF, given that it
2417 has length LENGTH. */
2418
2419static unsigned long
2420read_compressed_insn (char *buf, unsigned int length)
2421{
2422 unsigned long insn;
2423 unsigned int i;
2424
2425 insn = 0;
2426 for (i = 0; i < length; i += 2)
2427 {
2428 insn <<= 16;
2429 if (target_big_endian)
2430 insn |= bfd_getb16 ((char *) buf);
2431 else
2432 insn |= bfd_getl16 ((char *) buf);
2433 buf += 2;
2434 }
2435 return insn;
2436}
2437
5c04167a
RS
2438/* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2439 instruction is LENGTH bytes long. Return a pointer to the next byte. */
2440
2441static char *
2442write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2443{
2444 unsigned int i;
2445
2446 for (i = 0; i < length; i += 2)
2447 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2448 return buf + length;
2449}
2450
1e915849
RS
2451/* Install INSN at the location specified by its "frag" and "where" fields. */
2452
2453static void
2454install_insn (const struct mips_cl_insn *insn)
2455{
2456 char *f = insn->frag->fr_literal + insn->where;
5c04167a
RS
2457 if (HAVE_CODE_COMPRESSION)
2458 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
1e915849 2459 else
4d68580a 2460 write_insn (f, insn->insn_opcode);
df58fc94 2461 mips_record_compressed_mode ();
1e915849
RS
2462}
2463
2464/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
2465 and install the opcode in the new location. */
2466
2467static void
2468move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2469{
2470 size_t i;
2471
2472 insn->frag = frag;
2473 insn->where = where;
2474 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2475 if (insn->fixp[i] != NULL)
2476 {
2477 insn->fixp[i]->fx_frag = frag;
2478 insn->fixp[i]->fx_where = where;
2479 }
2480 install_insn (insn);
2481}
2482
2483/* Add INSN to the end of the output. */
2484
2485static void
2486add_fixed_insn (struct mips_cl_insn *insn)
2487{
2488 char *f = frag_more (insn_length (insn));
2489 move_insn (insn, frag_now, f - frag_now->fr_literal);
2490}
2491
2492/* Start a variant frag and move INSN to the start of the variant part,
2493 marking it as fixed. The other arguments are as for frag_var. */
2494
2495static void
2496add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2497 relax_substateT subtype, symbolS *symbol, offsetT offset)
2498{
2499 frag_grow (max_chars);
2500 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2501 insn->fixed_p = 1;
2502 frag_var (rs_machine_dependent, max_chars, var,
2503 subtype, symbol, offset, NULL);
2504}
2505
2506/* Insert N copies of INSN into the history buffer, starting at
2507 position FIRST. Neither FIRST nor N need to be clipped. */
2508
2509static void
2510insert_into_history (unsigned int first, unsigned int n,
2511 const struct mips_cl_insn *insn)
2512{
2513 if (mips_relax.sequence != 2)
2514 {
2515 unsigned int i;
2516
2517 for (i = ARRAY_SIZE (history); i-- > first;)
2518 if (i >= first + n)
2519 history[i] = history[i - n];
2520 else
2521 history[i] = *insn;
2522 }
2523}
2524
e3de51ce
RS
2525/* Clear the error in insn_error. */
2526
2527static void
2528clear_insn_error (void)
2529{
2530 memset (&insn_error, 0, sizeof (insn_error));
2531}
2532
2533/* Possibly record error message MSG for the current instruction.
2534 If the error is about a particular argument, ARGNUM is the 1-based
2535 number of that argument, otherwise it is 0. FORMAT is the format
2536 of MSG. Return true if MSG was used, false if the current message
2537 was kept. */
2538
2539static bfd_boolean
2540set_insn_error_format (int argnum, enum mips_insn_error_format format,
2541 const char *msg)
2542{
2543 if (argnum == 0)
2544 {
2545 /* Give priority to errors against specific arguments, and to
2546 the first whole-instruction message. */
2547 if (insn_error.msg)
2548 return FALSE;
2549 }
2550 else
2551 {
2552 /* Keep insn_error if it is against a later argument. */
2553 if (argnum < insn_error.min_argnum)
2554 return FALSE;
2555
2556 /* If both errors are against the same argument but are different,
2557 give up on reporting a specific error for this argument.
2558 See the comment about mips_insn_error for details. */
2559 if (argnum == insn_error.min_argnum
2560 && insn_error.msg
2561 && strcmp (insn_error.msg, msg) != 0)
2562 {
2563 insn_error.msg = 0;
2564 insn_error.min_argnum += 1;
2565 return FALSE;
2566 }
2567 }
2568 insn_error.min_argnum = argnum;
2569 insn_error.format = format;
2570 insn_error.msg = msg;
2571 return TRUE;
2572}
2573
2574/* Record an instruction error with no % format fields. ARGNUM and MSG are
2575 as for set_insn_error_format. */
2576
2577static void
2578set_insn_error (int argnum, const char *msg)
2579{
2580 set_insn_error_format (argnum, ERR_FMT_PLAIN, msg);
2581}
2582
2583/* Record an instruction error with one %d field I. ARGNUM and MSG are
2584 as for set_insn_error_format. */
2585
2586static void
2587set_insn_error_i (int argnum, const char *msg, int i)
2588{
2589 if (set_insn_error_format (argnum, ERR_FMT_I, msg))
2590 insn_error.u.i = i;
2591}
2592
2593/* Record an instruction error with two %s fields S1 and S2. ARGNUM and MSG
2594 are as for set_insn_error_format. */
2595
2596static void
2597set_insn_error_ss (int argnum, const char *msg, const char *s1, const char *s2)
2598{
2599 if (set_insn_error_format (argnum, ERR_FMT_SS, msg))
2600 {
2601 insn_error.u.ss[0] = s1;
2602 insn_error.u.ss[1] = s2;
2603 }
2604}
2605
2606/* Report the error in insn_error, which is against assembly code STR. */
2607
2608static void
2609report_insn_error (const char *str)
2610{
e1fa0163 2611 const char *msg = concat (insn_error.msg, " `%s'", NULL);
e3de51ce 2612
e3de51ce
RS
2613 switch (insn_error.format)
2614 {
2615 case ERR_FMT_PLAIN:
2616 as_bad (msg, str);
2617 break;
2618
2619 case ERR_FMT_I:
2620 as_bad (msg, insn_error.u.i, str);
2621 break;
2622
2623 case ERR_FMT_SS:
2624 as_bad (msg, insn_error.u.ss[0], insn_error.u.ss[1], str);
2625 break;
2626 }
e1fa0163
NC
2627
2628 free ((char *) msg);
e3de51ce
RS
2629}
2630
71400594
RS
2631/* Initialize vr4120_conflicts. There is a bit of duplication here:
2632 the idea is to make it obvious at a glance that each errata is
2633 included. */
2634
2635static void
2636init_vr4120_conflicts (void)
2637{
2638#define CONFLICT(FIRST, SECOND) \
2639 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2640
2641 /* Errata 21 - [D]DIV[U] after [D]MACC */
2642 CONFLICT (MACC, DIV);
2643 CONFLICT (DMACC, DIV);
2644
2645 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
2646 CONFLICT (DMULT, DMULT);
2647 CONFLICT (DMULT, DMACC);
2648 CONFLICT (DMACC, DMULT);
2649 CONFLICT (DMACC, DMACC);
2650
2651 /* Errata 24 - MT{LO,HI} after [D]MACC */
2652 CONFLICT (MACC, MTHILO);
2653 CONFLICT (DMACC, MTHILO);
2654
2655 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2656 instruction is executed immediately after a MACC or DMACC
2657 instruction, the result of [either instruction] is incorrect." */
2658 CONFLICT (MACC, MULT);
2659 CONFLICT (MACC, DMULT);
2660 CONFLICT (DMACC, MULT);
2661 CONFLICT (DMACC, DMULT);
2662
2663 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2664 executed immediately after a DMULT, DMULTU, DIV, DIVU,
2665 DDIV or DDIVU instruction, the result of the MACC or
2666 DMACC instruction is incorrect.". */
2667 CONFLICT (DMULT, MACC);
2668 CONFLICT (DMULT, DMACC);
2669 CONFLICT (DIV, MACC);
2670 CONFLICT (DIV, DMACC);
2671
2672#undef CONFLICT
2673}
2674
707bfff6
TS
2675struct regname {
2676 const char *name;
2677 unsigned int num;
2678};
2679
14daeee3 2680#define RNUM_MASK 0x00000ff
56d438b1 2681#define RTYPE_MASK 0x0ffff00
14daeee3
RS
2682#define RTYPE_NUM 0x0000100
2683#define RTYPE_FPU 0x0000200
2684#define RTYPE_FCC 0x0000400
2685#define RTYPE_VEC 0x0000800
2686#define RTYPE_GP 0x0001000
2687#define RTYPE_CP0 0x0002000
2688#define RTYPE_PC 0x0004000
2689#define RTYPE_ACC 0x0008000
2690#define RTYPE_CCC 0x0010000
2691#define RTYPE_VI 0x0020000
2692#define RTYPE_VF 0x0040000
2693#define RTYPE_R5900_I 0x0080000
2694#define RTYPE_R5900_Q 0x0100000
2695#define RTYPE_R5900_R 0x0200000
2696#define RTYPE_R5900_ACC 0x0400000
56d438b1 2697#define RTYPE_MSA 0x0800000
14daeee3 2698#define RWARN 0x8000000
707bfff6
TS
2699
2700#define GENERIC_REGISTER_NUMBERS \
2701 {"$0", RTYPE_NUM | 0}, \
2702 {"$1", RTYPE_NUM | 1}, \
2703 {"$2", RTYPE_NUM | 2}, \
2704 {"$3", RTYPE_NUM | 3}, \
2705 {"$4", RTYPE_NUM | 4}, \
2706 {"$5", RTYPE_NUM | 5}, \
2707 {"$6", RTYPE_NUM | 6}, \
2708 {"$7", RTYPE_NUM | 7}, \
2709 {"$8", RTYPE_NUM | 8}, \
2710 {"$9", RTYPE_NUM | 9}, \
2711 {"$10", RTYPE_NUM | 10}, \
2712 {"$11", RTYPE_NUM | 11}, \
2713 {"$12", RTYPE_NUM | 12}, \
2714 {"$13", RTYPE_NUM | 13}, \
2715 {"$14", RTYPE_NUM | 14}, \
2716 {"$15", RTYPE_NUM | 15}, \
2717 {"$16", RTYPE_NUM | 16}, \
2718 {"$17", RTYPE_NUM | 17}, \
2719 {"$18", RTYPE_NUM | 18}, \
2720 {"$19", RTYPE_NUM | 19}, \
2721 {"$20", RTYPE_NUM | 20}, \
2722 {"$21", RTYPE_NUM | 21}, \
2723 {"$22", RTYPE_NUM | 22}, \
2724 {"$23", RTYPE_NUM | 23}, \
2725 {"$24", RTYPE_NUM | 24}, \
2726 {"$25", RTYPE_NUM | 25}, \
2727 {"$26", RTYPE_NUM | 26}, \
2728 {"$27", RTYPE_NUM | 27}, \
2729 {"$28", RTYPE_NUM | 28}, \
2730 {"$29", RTYPE_NUM | 29}, \
2731 {"$30", RTYPE_NUM | 30}, \
3739860c 2732 {"$31", RTYPE_NUM | 31}
707bfff6
TS
2733
2734#define FPU_REGISTER_NAMES \
2735 {"$f0", RTYPE_FPU | 0}, \
2736 {"$f1", RTYPE_FPU | 1}, \
2737 {"$f2", RTYPE_FPU | 2}, \
2738 {"$f3", RTYPE_FPU | 3}, \
2739 {"$f4", RTYPE_FPU | 4}, \
2740 {"$f5", RTYPE_FPU | 5}, \
2741 {"$f6", RTYPE_FPU | 6}, \
2742 {"$f7", RTYPE_FPU | 7}, \
2743 {"$f8", RTYPE_FPU | 8}, \
2744 {"$f9", RTYPE_FPU | 9}, \
2745 {"$f10", RTYPE_FPU | 10}, \
2746 {"$f11", RTYPE_FPU | 11}, \
2747 {"$f12", RTYPE_FPU | 12}, \
2748 {"$f13", RTYPE_FPU | 13}, \
2749 {"$f14", RTYPE_FPU | 14}, \
2750 {"$f15", RTYPE_FPU | 15}, \
2751 {"$f16", RTYPE_FPU | 16}, \
2752 {"$f17", RTYPE_FPU | 17}, \
2753 {"$f18", RTYPE_FPU | 18}, \
2754 {"$f19", RTYPE_FPU | 19}, \
2755 {"$f20", RTYPE_FPU | 20}, \
2756 {"$f21", RTYPE_FPU | 21}, \
2757 {"$f22", RTYPE_FPU | 22}, \
2758 {"$f23", RTYPE_FPU | 23}, \
2759 {"$f24", RTYPE_FPU | 24}, \
2760 {"$f25", RTYPE_FPU | 25}, \
2761 {"$f26", RTYPE_FPU | 26}, \
2762 {"$f27", RTYPE_FPU | 27}, \
2763 {"$f28", RTYPE_FPU | 28}, \
2764 {"$f29", RTYPE_FPU | 29}, \
2765 {"$f30", RTYPE_FPU | 30}, \
2766 {"$f31", RTYPE_FPU | 31}
2767
2768#define FPU_CONDITION_CODE_NAMES \
2769 {"$fcc0", RTYPE_FCC | 0}, \
2770 {"$fcc1", RTYPE_FCC | 1}, \
2771 {"$fcc2", RTYPE_FCC | 2}, \
2772 {"$fcc3", RTYPE_FCC | 3}, \
2773 {"$fcc4", RTYPE_FCC | 4}, \
2774 {"$fcc5", RTYPE_FCC | 5}, \
2775 {"$fcc6", RTYPE_FCC | 6}, \
2776 {"$fcc7", RTYPE_FCC | 7}
2777
2778#define COPROC_CONDITION_CODE_NAMES \
2779 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
2780 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
2781 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
2782 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
2783 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
2784 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
2785 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
2786 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
2787
2788#define N32N64_SYMBOLIC_REGISTER_NAMES \
2789 {"$a4", RTYPE_GP | 8}, \
2790 {"$a5", RTYPE_GP | 9}, \
2791 {"$a6", RTYPE_GP | 10}, \
2792 {"$a7", RTYPE_GP | 11}, \
2793 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
2794 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
2795 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
2796 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
2797 {"$t0", RTYPE_GP | 12}, \
2798 {"$t1", RTYPE_GP | 13}, \
2799 {"$t2", RTYPE_GP | 14}, \
2800 {"$t3", RTYPE_GP | 15}
2801
2802#define O32_SYMBOLIC_REGISTER_NAMES \
2803 {"$t0", RTYPE_GP | 8}, \
2804 {"$t1", RTYPE_GP | 9}, \
2805 {"$t2", RTYPE_GP | 10}, \
2806 {"$t3", RTYPE_GP | 11}, \
2807 {"$t4", RTYPE_GP | 12}, \
2808 {"$t5", RTYPE_GP | 13}, \
2809 {"$t6", RTYPE_GP | 14}, \
2810 {"$t7", RTYPE_GP | 15}, \
2811 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2812 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2813 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
3739860c 2814 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
707bfff6 2815
6f2117ba 2816/* Remaining symbolic register names. */
707bfff6
TS
2817#define SYMBOLIC_REGISTER_NAMES \
2818 {"$zero", RTYPE_GP | 0}, \
2819 {"$at", RTYPE_GP | 1}, \
2820 {"$AT", RTYPE_GP | 1}, \
2821 {"$v0", RTYPE_GP | 2}, \
2822 {"$v1", RTYPE_GP | 3}, \
2823 {"$a0", RTYPE_GP | 4}, \
2824 {"$a1", RTYPE_GP | 5}, \
2825 {"$a2", RTYPE_GP | 6}, \
2826 {"$a3", RTYPE_GP | 7}, \
2827 {"$s0", RTYPE_GP | 16}, \
2828 {"$s1", RTYPE_GP | 17}, \
2829 {"$s2", RTYPE_GP | 18}, \
2830 {"$s3", RTYPE_GP | 19}, \
2831 {"$s4", RTYPE_GP | 20}, \
2832 {"$s5", RTYPE_GP | 21}, \
2833 {"$s6", RTYPE_GP | 22}, \
2834 {"$s7", RTYPE_GP | 23}, \
2835 {"$t8", RTYPE_GP | 24}, \
2836 {"$t9", RTYPE_GP | 25}, \
2837 {"$k0", RTYPE_GP | 26}, \
2838 {"$kt0", RTYPE_GP | 26}, \
2839 {"$k1", RTYPE_GP | 27}, \
2840 {"$kt1", RTYPE_GP | 27}, \
2841 {"$gp", RTYPE_GP | 28}, \
2842 {"$sp", RTYPE_GP | 29}, \
2843 {"$s8", RTYPE_GP | 30}, \
2844 {"$fp", RTYPE_GP | 30}, \
2845 {"$ra", RTYPE_GP | 31}
2846
2847#define MIPS16_SPECIAL_REGISTER_NAMES \
2848 {"$pc", RTYPE_PC | 0}
2849
2850#define MDMX_VECTOR_REGISTER_NAMES \
6f2117ba
PH
2851 /* {"$v0", RTYPE_VEC | 0}, Clash with REG 2 above. */ \
2852 /* {"$v1", RTYPE_VEC | 1}, Clash with REG 3 above. */ \
707bfff6
TS
2853 {"$v2", RTYPE_VEC | 2}, \
2854 {"$v3", RTYPE_VEC | 3}, \
2855 {"$v4", RTYPE_VEC | 4}, \
2856 {"$v5", RTYPE_VEC | 5}, \
2857 {"$v6", RTYPE_VEC | 6}, \
2858 {"$v7", RTYPE_VEC | 7}, \
2859 {"$v8", RTYPE_VEC | 8}, \
2860 {"$v9", RTYPE_VEC | 9}, \
2861 {"$v10", RTYPE_VEC | 10}, \
2862 {"$v11", RTYPE_VEC | 11}, \
2863 {"$v12", RTYPE_VEC | 12}, \
2864 {"$v13", RTYPE_VEC | 13}, \
2865 {"$v14", RTYPE_VEC | 14}, \
2866 {"$v15", RTYPE_VEC | 15}, \
2867 {"$v16", RTYPE_VEC | 16}, \
2868 {"$v17", RTYPE_VEC | 17}, \
2869 {"$v18", RTYPE_VEC | 18}, \
2870 {"$v19", RTYPE_VEC | 19}, \
2871 {"$v20", RTYPE_VEC | 20}, \
2872 {"$v21", RTYPE_VEC | 21}, \
2873 {"$v22", RTYPE_VEC | 22}, \
2874 {"$v23", RTYPE_VEC | 23}, \
2875 {"$v24", RTYPE_VEC | 24}, \
2876 {"$v25", RTYPE_VEC | 25}, \
2877 {"$v26", RTYPE_VEC | 26}, \
2878 {"$v27", RTYPE_VEC | 27}, \
2879 {"$v28", RTYPE_VEC | 28}, \
2880 {"$v29", RTYPE_VEC | 29}, \
2881 {"$v30", RTYPE_VEC | 30}, \
2882 {"$v31", RTYPE_VEC | 31}
2883
14daeee3
RS
2884#define R5900_I_NAMES \
2885 {"$I", RTYPE_R5900_I | 0}
2886
2887#define R5900_Q_NAMES \
2888 {"$Q", RTYPE_R5900_Q | 0}
2889
2890#define R5900_R_NAMES \
2891 {"$R", RTYPE_R5900_R | 0}
2892
2893#define R5900_ACC_NAMES \
2894 {"$ACC", RTYPE_R5900_ACC | 0 }
2895
707bfff6
TS
2896#define MIPS_DSP_ACCUMULATOR_NAMES \
2897 {"$ac0", RTYPE_ACC | 0}, \
2898 {"$ac1", RTYPE_ACC | 1}, \
2899 {"$ac2", RTYPE_ACC | 2}, \
2900 {"$ac3", RTYPE_ACC | 3}
2901
2902static const struct regname reg_names[] = {
2903 GENERIC_REGISTER_NUMBERS,
2904 FPU_REGISTER_NAMES,
2905 FPU_CONDITION_CODE_NAMES,
2906 COPROC_CONDITION_CODE_NAMES,
2907
2908 /* The $txx registers depends on the abi,
2909 these will be added later into the symbol table from
3739860c 2910 one of the tables below once mips_abi is set after
707bfff6
TS
2911 parsing of arguments from the command line. */
2912 SYMBOLIC_REGISTER_NAMES,
2913
2914 MIPS16_SPECIAL_REGISTER_NAMES,
2915 MDMX_VECTOR_REGISTER_NAMES,
14daeee3
RS
2916 R5900_I_NAMES,
2917 R5900_Q_NAMES,
2918 R5900_R_NAMES,
2919 R5900_ACC_NAMES,
707bfff6
TS
2920 MIPS_DSP_ACCUMULATOR_NAMES,
2921 {0, 0}
2922};
2923
2924static const struct regname reg_names_o32[] = {
2925 O32_SYMBOLIC_REGISTER_NAMES,
2926 {0, 0}
2927};
2928
2929static const struct regname reg_names_n32n64[] = {
2930 N32N64_SYMBOLIC_REGISTER_NAMES,
2931 {0, 0}
2932};
2933
a92713e6
RS
2934/* Register symbols $v0 and $v1 map to GPRs 2 and 3, but they can also be
2935 interpreted as vector registers 0 and 1. If SYMVAL is the value of one
2936 of these register symbols, return the associated vector register,
2937 otherwise return SYMVAL itself. */
df58fc94 2938
a92713e6
RS
2939static unsigned int
2940mips_prefer_vec_regno (unsigned int symval)
707bfff6 2941{
a92713e6
RS
2942 if ((symval & -2) == (RTYPE_GP | 2))
2943 return RTYPE_VEC | (symval & 1);
2944 return symval;
2945}
2946
14daeee3
RS
2947/* Return true if string [S, E) is a valid register name, storing its
2948 symbol value in *SYMVAL_PTR if so. */
a92713e6
RS
2949
2950static bfd_boolean
14daeee3 2951mips_parse_register_1 (char *s, char *e, unsigned int *symval_ptr)
a92713e6 2952{
707bfff6 2953 char save_c;
14daeee3 2954 symbolS *symbol;
707bfff6
TS
2955
2956 /* Terminate name. */
2957 save_c = *e;
2958 *e = '\0';
2959
a92713e6
RS
2960 /* Look up the name. */
2961 symbol = symbol_find (s);
2962 *e = save_c;
2963
2964 if (!symbol || S_GET_SEGMENT (symbol) != reg_section)
2965 return FALSE;
2966
14daeee3
RS
2967 *symval_ptr = S_GET_VALUE (symbol);
2968 return TRUE;
2969}
2970
2971/* Return true if the string at *SPTR is a valid register name. Allow it
2972 to have a VU0-style channel suffix of the form x?y?z?w? if CHANNELS_PTR
2973 is nonnull.
2974
2975 When returning true, move *SPTR past the register, store the
2976 register's symbol value in *SYMVAL_PTR and the channel mask in
2977 *CHANNELS_PTR (if nonnull). The symbol value includes the register
2978 number (RNUM_MASK) and register type (RTYPE_MASK). The channel mask
2979 is a 4-bit value of the form XYZW and is 0 if no suffix was given. */
2980
2981static bfd_boolean
2982mips_parse_register (char **sptr, unsigned int *symval_ptr,
2983 unsigned int *channels_ptr)
2984{
2985 char *s, *e, *m;
2986 const char *q;
2987 unsigned int channels, symval, bit;
2988
2989 /* Find end of name. */
2990 s = e = *sptr;
2991 if (is_name_beginner (*e))
2992 ++e;
2993 while (is_part_of_name (*e))
2994 ++e;
2995
2996 channels = 0;
2997 if (!mips_parse_register_1 (s, e, &symval))
2998 {
2999 if (!channels_ptr)
3000 return FALSE;
3001
3002 /* Eat characters from the end of the string that are valid
3003 channel suffixes. The preceding register must be $ACC or
3004 end with a digit, so there is no ambiguity. */
3005 bit = 1;
3006 m = e;
3007 for (q = "wzyx"; *q; q++, bit <<= 1)
3008 if (m > s && m[-1] == *q)
3009 {
3010 --m;
3011 channels |= bit;
3012 }
3013
3014 if (channels == 0
3015 || !mips_parse_register_1 (s, m, &symval)
3016 || (symval & (RTYPE_VI | RTYPE_VF | RTYPE_R5900_ACC)) == 0)
3017 return FALSE;
3018 }
3019
a92713e6 3020 *sptr = e;
14daeee3
RS
3021 *symval_ptr = symval;
3022 if (channels_ptr)
3023 *channels_ptr = channels;
a92713e6
RS
3024 return TRUE;
3025}
3026
3027/* Check if SPTR points at a valid register specifier according to TYPES.
3028 If so, then return 1, advance S to consume the specifier and store
3029 the register's number in REGNOP, otherwise return 0. */
3030
3031static int
3032reg_lookup (char **s, unsigned int types, unsigned int *regnop)
3033{
3034 unsigned int regno;
3035
14daeee3 3036 if (mips_parse_register (s, &regno, NULL))
707bfff6 3037 {
a92713e6
RS
3038 if (types & RTYPE_VEC)
3039 regno = mips_prefer_vec_regno (regno);
3040 if (regno & types)
3041 regno &= RNUM_MASK;
3042 else
3043 regno = ~0;
707bfff6 3044 }
a92713e6 3045 else
707bfff6 3046 {
a92713e6 3047 if (types & RWARN)
1661c76c 3048 as_warn (_("unrecognized register name `%s'"), *s);
a92713e6 3049 regno = ~0;
707bfff6 3050 }
707bfff6 3051 if (regnop)
a92713e6
RS
3052 *regnop = regno;
3053 return regno <= RNUM_MASK;
707bfff6
TS
3054}
3055
14daeee3
RS
3056/* Parse a VU0 "x?y?z?w?" channel mask at S and store the associated
3057 mask in *CHANNELS. Return a pointer to the first unconsumed character. */
3058
3059static char *
3060mips_parse_vu0_channels (char *s, unsigned int *channels)
3061{
3062 unsigned int i;
3063
3064 *channels = 0;
3065 for (i = 0; i < 4; i++)
3066 if (*s == "xyzw"[i])
3067 {
3068 *channels |= 1 << (3 - i);
3069 ++s;
3070 }
3071 return s;
3072}
3073
a92713e6
RS
3074/* Token types for parsed operand lists. */
3075enum mips_operand_token_type {
3076 /* A plain register, e.g. $f2. */
3077 OT_REG,
df58fc94 3078
14daeee3
RS
3079 /* A 4-bit XYZW channel mask. */
3080 OT_CHANNELS,
3081
56d438b1
CF
3082 /* A constant vector index, e.g. [1]. */
3083 OT_INTEGER_INDEX,
3084
3085 /* A register vector index, e.g. [$2]. */
3086 OT_REG_INDEX,
df58fc94 3087
a92713e6
RS
3088 /* A continuous range of registers, e.g. $s0-$s4. */
3089 OT_REG_RANGE,
3090
3091 /* A (possibly relocated) expression. */
3092 OT_INTEGER,
3093
3094 /* A floating-point value. */
3095 OT_FLOAT,
3096
3097 /* A single character. This can be '(', ')' or ',', but '(' only appears
3098 before OT_REGs. */
3099 OT_CHAR,
3100
14daeee3
RS
3101 /* A doubled character, either "--" or "++". */
3102 OT_DOUBLE_CHAR,
3103
a92713e6
RS
3104 /* The end of the operand list. */
3105 OT_END
3106};
3107
3108/* A parsed operand token. */
3109struct mips_operand_token
3110{
3111 /* The type of token. */
3112 enum mips_operand_token_type type;
3113 union
3114 {
56d438b1 3115 /* The register symbol value for an OT_REG or OT_REG_INDEX. */
a92713e6
RS
3116 unsigned int regno;
3117
14daeee3
RS
3118 /* The 4-bit channel mask for an OT_CHANNEL_SUFFIX. */
3119 unsigned int channels;
3120
56d438b1
CF
3121 /* The integer value of an OT_INTEGER_INDEX. */
3122 addressT index;
a92713e6
RS
3123
3124 /* The two register symbol values involved in an OT_REG_RANGE. */
3125 struct {
3126 unsigned int regno1;
3127 unsigned int regno2;
3128 } reg_range;
3129
3130 /* The value of an OT_INTEGER. The value is represented as an
3131 expression and the relocation operators that were applied to
3132 that expression. The reloc entries are BFD_RELOC_UNUSED if no
3133 relocation operators were used. */
3134 struct {
3135 expressionS value;
3136 bfd_reloc_code_real_type relocs[3];
3137 } integer;
3138
3139 /* The binary data for an OT_FLOAT constant, and the number of bytes
3140 in the constant. */
3141 struct {
3142 unsigned char data[8];
3143 int length;
3144 } flt;
3145
14daeee3 3146 /* The character represented by an OT_CHAR or OT_DOUBLE_CHAR. */
a92713e6
RS
3147 char ch;
3148 } u;
3149};
3150
3151/* An obstack used to construct lists of mips_operand_tokens. */
3152static struct obstack mips_operand_tokens;
3153
3154/* Give TOKEN type TYPE and add it to mips_operand_tokens. */
3155
3156static void
3157mips_add_token (struct mips_operand_token *token,
3158 enum mips_operand_token_type type)
3159{
3160 token->type = type;
3161 obstack_grow (&mips_operand_tokens, token, sizeof (*token));
3162}
3163
3164/* Check whether S is '(' followed by a register name. Add OT_CHAR
3165 and OT_REG tokens for them if so, and return a pointer to the first
3166 unconsumed character. Return null otherwise. */
3167
3168static char *
3169mips_parse_base_start (char *s)
3170{
3171 struct mips_operand_token token;
14daeee3
RS
3172 unsigned int regno, channels;
3173 bfd_boolean decrement_p;
df58fc94 3174
a92713e6
RS
3175 if (*s != '(')
3176 return 0;
3177
3178 ++s;
3179 SKIP_SPACE_TABS (s);
14daeee3
RS
3180
3181 /* Only match "--" as part of a base expression. In other contexts "--X"
3182 is a double negative. */
3183 decrement_p = (s[0] == '-' && s[1] == '-');
3184 if (decrement_p)
3185 {
3186 s += 2;
3187 SKIP_SPACE_TABS (s);
3188 }
3189
3190 /* Allow a channel specifier because that leads to better error messages
3191 than treating something like "$vf0x++" as an expression. */
3192 if (!mips_parse_register (&s, &regno, &channels))
a92713e6
RS
3193 return 0;
3194
3195 token.u.ch = '(';
3196 mips_add_token (&token, OT_CHAR);
3197
14daeee3
RS
3198 if (decrement_p)
3199 {
3200 token.u.ch = '-';
3201 mips_add_token (&token, OT_DOUBLE_CHAR);
3202 }
3203
a92713e6
RS
3204 token.u.regno = regno;
3205 mips_add_token (&token, OT_REG);
3206
14daeee3
RS
3207 if (channels)
3208 {
3209 token.u.channels = channels;
3210 mips_add_token (&token, OT_CHANNELS);
3211 }
3212
3213 /* For consistency, only match "++" as part of base expressions too. */
3214 SKIP_SPACE_TABS (s);
3215 if (s[0] == '+' && s[1] == '+')
3216 {
3217 s += 2;
3218 token.u.ch = '+';
3219 mips_add_token (&token, OT_DOUBLE_CHAR);
3220 }
3221
a92713e6
RS
3222 return s;
3223}
3224
3225/* Parse one or more tokens from S. Return a pointer to the first
3226 unconsumed character on success. Return null if an error was found
3227 and store the error text in insn_error. FLOAT_FORMAT is as for
3228 mips_parse_arguments. */
3229
3230static char *
3231mips_parse_argument_token (char *s, char float_format)
3232{
6d4af3c2
AM
3233 char *end, *save_in;
3234 const char *err;
14daeee3 3235 unsigned int regno1, regno2, channels;
a92713e6
RS
3236 struct mips_operand_token token;
3237
3238 /* First look for "($reg", since we want to treat that as an
3239 OT_CHAR and OT_REG rather than an expression. */
3240 end = mips_parse_base_start (s);
3241 if (end)
3242 return end;
3243
3244 /* Handle other characters that end up as OT_CHARs. */
3245 if (*s == ')' || *s == ',')
3246 {
3247 token.u.ch = *s;
3248 mips_add_token (&token, OT_CHAR);
3249 ++s;
3250 return s;
3251 }
3252
3253 /* Handle tokens that start with a register. */
14daeee3 3254 if (mips_parse_register (&s, &regno1, &channels))
df58fc94 3255 {
14daeee3
RS
3256 if (channels)
3257 {
3258 /* A register and a VU0 channel suffix. */
3259 token.u.regno = regno1;
3260 mips_add_token (&token, OT_REG);
3261
3262 token.u.channels = channels;
3263 mips_add_token (&token, OT_CHANNELS);
3264 return s;
3265 }
3266
a92713e6
RS
3267 SKIP_SPACE_TABS (s);
3268 if (*s == '-')
df58fc94 3269 {
a92713e6
RS
3270 /* A register range. */
3271 ++s;
3272 SKIP_SPACE_TABS (s);
14daeee3 3273 if (!mips_parse_register (&s, &regno2, NULL))
a92713e6 3274 {
1661c76c 3275 set_insn_error (0, _("invalid register range"));
a92713e6
RS
3276 return 0;
3277 }
df58fc94 3278
a92713e6
RS
3279 token.u.reg_range.regno1 = regno1;
3280 token.u.reg_range.regno2 = regno2;
3281 mips_add_token (&token, OT_REG_RANGE);
3282 return s;
3283 }
a92713e6 3284
56d438b1
CF
3285 /* Add the register itself. */
3286 token.u.regno = regno1;
3287 mips_add_token (&token, OT_REG);
3288
3289 /* Check for a vector index. */
3290 if (*s == '[')
3291 {
a92713e6
RS
3292 ++s;
3293 SKIP_SPACE_TABS (s);
56d438b1
CF
3294 if (mips_parse_register (&s, &token.u.regno, NULL))
3295 mips_add_token (&token, OT_REG_INDEX);
3296 else
a92713e6 3297 {
56d438b1
CF
3298 expressionS element;
3299
3300 my_getExpression (&element, s);
3301 if (element.X_op != O_constant)
3302 {
3303 set_insn_error (0, _("vector element must be constant"));
3304 return 0;
3305 }
3306 s = expr_end;
3307 token.u.index = element.X_add_number;
3308 mips_add_token (&token, OT_INTEGER_INDEX);
a92713e6 3309 }
a92713e6
RS
3310 SKIP_SPACE_TABS (s);
3311 if (*s != ']')
3312 {
1661c76c 3313 set_insn_error (0, _("missing `]'"));
a92713e6
RS
3314 return 0;
3315 }
3316 ++s;
df58fc94 3317 }
a92713e6 3318 return s;
df58fc94
RS
3319 }
3320
a92713e6
RS
3321 if (float_format)
3322 {
3323 /* First try to treat expressions as floats. */
3324 save_in = input_line_pointer;
3325 input_line_pointer = s;
3326 err = md_atof (float_format, (char *) token.u.flt.data,
3327 &token.u.flt.length);
3328 end = input_line_pointer;
3329 input_line_pointer = save_in;
3330 if (err && *err)
3331 {
e3de51ce 3332 set_insn_error (0, err);
a92713e6
RS
3333 return 0;
3334 }
3335 if (s != end)
3336 {
3337 mips_add_token (&token, OT_FLOAT);
3338 return end;
3339 }
3340 }
3341
3342 /* Treat everything else as an integer expression. */
3343 token.u.integer.relocs[0] = BFD_RELOC_UNUSED;
3344 token.u.integer.relocs[1] = BFD_RELOC_UNUSED;
3345 token.u.integer.relocs[2] = BFD_RELOC_UNUSED;
3346 my_getSmallExpression (&token.u.integer.value, token.u.integer.relocs, s);
3347 s = expr_end;
3348 mips_add_token (&token, OT_INTEGER);
3349 return s;
3350}
3351
3352/* S points to the operand list for an instruction. FLOAT_FORMAT is 'f'
3353 if expressions should be treated as 32-bit floating-point constants,
3354 'd' if they should be treated as 64-bit floating-point constants,
3355 or 0 if they should be treated as integer expressions (the usual case).
3356
3357 Return a list of tokens on success, otherwise return 0. The caller
3358 must obstack_free the list after use. */
3359
3360static struct mips_operand_token *
3361mips_parse_arguments (char *s, char float_format)
3362{
3363 struct mips_operand_token token;
3364
3365 SKIP_SPACE_TABS (s);
3366 while (*s)
3367 {
3368 s = mips_parse_argument_token (s, float_format);
3369 if (!s)
3370 {
3371 obstack_free (&mips_operand_tokens,
3372 obstack_finish (&mips_operand_tokens));
3373 return 0;
3374 }
3375 SKIP_SPACE_TABS (s);
3376 }
3377 mips_add_token (&token, OT_END);
3378 return (struct mips_operand_token *) obstack_finish (&mips_operand_tokens);
df58fc94
RS
3379}
3380
d301a56b
RS
3381/* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
3382 and architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
037b32b9
AN
3383
3384static bfd_boolean
f79e2745 3385is_opcode_valid (const struct mips_opcode *mo)
037b32b9
AN
3386{
3387 int isa = mips_opts.isa;
846ef2d0 3388 int ase = mips_opts.ase;
037b32b9 3389 int fp_s, fp_d;
c6278170 3390 unsigned int i;
037b32b9 3391
be0fcbee 3392 if (ISA_HAS_64BIT_REGS (isa))
c6278170
RS
3393 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3394 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3395 ase |= mips_ases[i].flags64;
037b32b9 3396
d301a56b 3397 if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
037b32b9
AN
3398 return FALSE;
3399
3400 /* Check whether the instruction or macro requires single-precision or
3401 double-precision floating-point support. Note that this information is
3402 stored differently in the opcode table for insns and macros. */
3403 if (mo->pinfo == INSN_MACRO)
3404 {
3405 fp_s = mo->pinfo2 & INSN2_M_FP_S;
3406 fp_d = mo->pinfo2 & INSN2_M_FP_D;
3407 }
3408 else
3409 {
3410 fp_s = mo->pinfo & FP_S;
3411 fp_d = mo->pinfo & FP_D;
3412 }
3413
3414 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
3415 return FALSE;
3416
3417 if (fp_s && mips_opts.soft_float)
3418 return FALSE;
3419
3420 return TRUE;
3421}
3422
3423/* Return TRUE if the MIPS16 opcode MO is valid on the currently
3424 selected ISA and architecture. */
3425
3426static bfd_boolean
3427is_opcode_valid_16 (const struct mips_opcode *mo)
3428{
25499ac7
MR
3429 int isa = mips_opts.isa;
3430 int ase = mips_opts.ase;
3431 unsigned int i;
3432
3433 if (ISA_HAS_64BIT_REGS (isa))
3434 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3435 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3436 ase |= mips_ases[i].flags64;
3437
3438 return opcode_is_member (mo, isa, ase, mips_opts.arch);
037b32b9
AN
3439}
3440
df58fc94 3441/* Return TRUE if the size of the microMIPS opcode MO matches one
7fd53920
MR
3442 explicitly requested. Always TRUE in the standard MIPS mode.
3443 Use is_size_valid_16 for MIPS16 opcodes. */
df58fc94
RS
3444
3445static bfd_boolean
3446is_size_valid (const struct mips_opcode *mo)
3447{
3448 if (!mips_opts.micromips)
3449 return TRUE;
3450
833794fc
MR
3451 if (mips_opts.insn32)
3452 {
3453 if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
3454 return FALSE;
3455 if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
3456 return FALSE;
3457 }
df58fc94
RS
3458 if (!forced_insn_length)
3459 return TRUE;
3460 if (mo->pinfo == INSN_MACRO)
3461 return FALSE;
3462 return forced_insn_length == micromips_insn_length (mo);
3463}
3464
7fd53920
MR
3465/* Return TRUE if the size of the MIPS16 opcode MO matches one
3466 explicitly requested. */
3467
3468static bfd_boolean
3469is_size_valid_16 (const struct mips_opcode *mo)
3470{
3471 if (!forced_insn_length)
3472 return TRUE;
3473 if (mo->pinfo == INSN_MACRO)
3474 return FALSE;
3475 if (forced_insn_length == 2 && mips_opcode_32bit_p (mo))
3476 return FALSE;
0674ee5d
MR
3477 if (forced_insn_length == 4 && (mo->pinfo2 & INSN2_SHORT_ONLY))
3478 return FALSE;
7fd53920
MR
3479 return TRUE;
3480}
3481
df58fc94 3482/* Return TRUE if the microMIPS opcode MO is valid for the delay slot
e64af278
MR
3483 of the preceding instruction. Always TRUE in the standard MIPS mode.
3484
3485 We don't accept macros in 16-bit delay slots to avoid a case where
3486 a macro expansion fails because it relies on a preceding 32-bit real
3487 instruction to have matched and does not handle the operands correctly.
3488 The only macros that may expand to 16-bit instructions are JAL that
3489 cannot be placed in a delay slot anyway, and corner cases of BALIGN
3490 and BGT (that likewise cannot be placed in a delay slot) that decay to
3491 a NOP. In all these cases the macros precede any corresponding real
3492 instruction definitions in the opcode table, so they will match in the
3493 second pass where the size of the delay slot is ignored and therefore
3494 produce correct code. */
df58fc94
RS
3495
3496static bfd_boolean
3497is_delay_slot_valid (const struct mips_opcode *mo)
3498{
3499 if (!mips_opts.micromips)
3500 return TRUE;
3501
3502 if (mo->pinfo == INSN_MACRO)
c06dec14 3503 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
df58fc94
RS
3504 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
3505 && micromips_insn_length (mo) != 4)
3506 return FALSE;
3507 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
3508 && micromips_insn_length (mo) != 2)
3509 return FALSE;
3510
3511 return TRUE;
3512}
3513
fc76e730
RS
3514/* For consistency checking, verify that all bits of OPCODE are specified
3515 either by the match/mask part of the instruction definition, or by the
3516 operand list. Also build up a list of operands in OPERANDS.
3517
3518 INSN_BITS says which bits of the instruction are significant.
3519 If OPCODE is a standard or microMIPS instruction, DECODE_OPERAND
3520 provides the mips_operand description of each operand. DECODE_OPERAND
3521 is null for MIPS16 instructions. */
ab902481
RS
3522
3523static int
3524validate_mips_insn (const struct mips_opcode *opcode,
3525 unsigned long insn_bits,
fc76e730
RS
3526 const struct mips_operand *(*decode_operand) (const char *),
3527 struct mips_operand_array *operands)
ab902481
RS
3528{
3529 const char *s;
fc76e730 3530 unsigned long used_bits, doubled, undefined, opno, mask;
ab902481
RS
3531 const struct mips_operand *operand;
3532
fc76e730
RS
3533 mask = (opcode->pinfo == INSN_MACRO ? 0 : opcode->mask);
3534 if ((mask & opcode->match) != opcode->match)
ab902481
RS
3535 {
3536 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
3537 opcode->name, opcode->args);
3538 return 0;
3539 }
3540 used_bits = 0;
fc76e730 3541 opno = 0;
14daeee3
RS
3542 if (opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX)
3543 used_bits = mips_insert_operand (&mips_vu0_channel_mask, used_bits, -1);
ab902481
RS
3544 for (s = opcode->args; *s; ++s)
3545 switch (*s)
3546 {
3547 case ',':
3548 case '(':
3549 case ')':
3550 break;
3551
14daeee3
RS
3552 case '#':
3553 s++;
3554 break;
3555
ab902481 3556 default:
fc76e730 3557 if (!decode_operand)
7fd53920 3558 operand = decode_mips16_operand (*s, mips_opcode_32bit_p (opcode));
fc76e730
RS
3559 else
3560 operand = decode_operand (s);
3561 if (!operand && opcode->pinfo != INSN_MACRO)
ab902481
RS
3562 {
3563 as_bad (_("internal: unknown operand type: %s %s"),
3564 opcode->name, opcode->args);
3565 return 0;
3566 }
fc76e730
RS
3567 gas_assert (opno < MAX_OPERANDS);
3568 operands->operand[opno] = operand;
25499ac7
MR
3569 if (!decode_operand && operand
3570 && operand->type == OP_INT && operand->lsb == 0
3571 && mips_opcode_32bit_p (opcode))
3572 used_bits |= mips16_immed_extend (-1, operand->size);
3573 else if (operand && operand->type != OP_VU0_MATCH_SUFFIX)
fc76e730 3574 {
14daeee3 3575 used_bits = mips_insert_operand (operand, used_bits, -1);
fc76e730
RS
3576 if (operand->type == OP_MDMX_IMM_REG)
3577 /* Bit 5 is the format selector (OB vs QH). The opcode table
3578 has separate entries for each format. */
3579 used_bits &= ~(1 << (operand->lsb + 5));
3580 if (operand->type == OP_ENTRY_EXIT_LIST)
3581 used_bits &= ~(mask & 0x700);
38bf472a
MR
3582 /* interAptiv MR2 SAVE/RESTORE instructions have a discontiguous
3583 operand field that cannot be fully described with LSB/SIZE. */
3584 if (operand->type == OP_SAVE_RESTORE_LIST && operand->lsb == 6)
3585 used_bits &= ~0x6000;
fc76e730 3586 }
ab902481 3587 /* Skip prefix characters. */
7361da2c 3588 if (decode_operand && (*s == '+' || *s == 'm' || *s == '-'))
ab902481 3589 ++s;
fc76e730 3590 opno += 1;
ab902481
RS
3591 break;
3592 }
fc76e730 3593 doubled = used_bits & mask & insn_bits;
ab902481
RS
3594 if (doubled)
3595 {
3596 as_bad (_("internal: bad mips opcode (bits 0x%08lx doubly defined):"
3597 " %s %s"), doubled, opcode->name, opcode->args);
3598 return 0;
3599 }
fc76e730 3600 used_bits |= mask;
ab902481 3601 undefined = ~used_bits & insn_bits;
fc76e730 3602 if (opcode->pinfo != INSN_MACRO && undefined)
ab902481
RS
3603 {
3604 as_bad (_("internal: bad mips opcode (bits 0x%08lx undefined): %s %s"),
3605 undefined, opcode->name, opcode->args);
3606 return 0;
3607 }
3608 used_bits &= ~insn_bits;
3609 if (used_bits)
3610 {
3611 as_bad (_("internal: bad mips opcode (bits 0x%08lx defined): %s %s"),
3612 used_bits, opcode->name, opcode->args);
3613 return 0;
3614 }
3615 return 1;
3616}
3617
fc76e730
RS
3618/* The MIPS16 version of validate_mips_insn. */
3619
3620static int
3621validate_mips16_insn (const struct mips_opcode *opcode,
3622 struct mips_operand_array *operands)
3623{
7fd53920 3624 unsigned long insn_bits = mips_opcode_32bit_p (opcode) ? 0xffffffff : 0xffff;
fc76e730 3625
7fd53920 3626 return validate_mips_insn (opcode, insn_bits, 0, operands);
fc76e730
RS
3627}
3628
ab902481
RS
3629/* The microMIPS version of validate_mips_insn. */
3630
3631static int
fc76e730
RS
3632validate_micromips_insn (const struct mips_opcode *opc,
3633 struct mips_operand_array *operands)
ab902481
RS
3634{
3635 unsigned long insn_bits;
3636 unsigned long major;
3637 unsigned int length;
3638
fc76e730
RS
3639 if (opc->pinfo == INSN_MACRO)
3640 return validate_mips_insn (opc, 0xffffffff, decode_micromips_operand,
3641 operands);
3642
ab902481
RS
3643 length = micromips_insn_length (opc);
3644 if (length != 2 && length != 4)
3645 {
1661c76c 3646 as_bad (_("internal error: bad microMIPS opcode (incorrect length: %u): "
ab902481
RS
3647 "%s %s"), length, opc->name, opc->args);
3648 return 0;
3649 }
3650 major = opc->match >> (10 + 8 * (length - 2));
3651 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
3652 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
3653 {
1661c76c 3654 as_bad (_("internal error: bad microMIPS opcode "
ab902481
RS
3655 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
3656 return 0;
3657 }
3658
3659 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
3660 insn_bits = 1 << 4 * length;
3661 insn_bits <<= 4 * length;
3662 insn_bits -= 1;
fc76e730
RS
3663 return validate_mips_insn (opc, insn_bits, decode_micromips_operand,
3664 operands);
ab902481
RS
3665}
3666
707bfff6
TS
3667/* This function is called once, at assembler startup time. It should set up
3668 all the tables, etc. that the MD part of the assembler will need. */
156c2f8b 3669
252b5132 3670void
17a2f251 3671md_begin (void)
252b5132 3672{
3994f87e 3673 const char *retval = NULL;
156c2f8b 3674 int i = 0;
252b5132 3675 int broken = 0;
1f25f5d3 3676
0a44bf69
RS
3677 if (mips_pic != NO_PIC)
3678 {
3679 if (g_switch_seen && g_switch_value != 0)
3680 as_bad (_("-G may not be used in position-independent code"));
3681 g_switch_value = 0;
3682 }
00acd688
CM
3683 else if (mips_abicalls)
3684 {
3685 if (g_switch_seen && g_switch_value != 0)
3686 as_bad (_("-G may not be used with abicalls"));
3687 g_switch_value = 0;
3688 }
0a44bf69 3689
0b35dfee 3690 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
1661c76c 3691 as_warn (_("could not set architecture and machine"));
252b5132 3692
252b5132
RH
3693 op_hash = hash_new ();
3694
fc76e730 3695 mips_operands = XCNEWVEC (struct mips_operand_array, NUMOPCODES);
252b5132
RH
3696 for (i = 0; i < NUMOPCODES;)
3697 {
3698 const char *name = mips_opcodes[i].name;
3699
17a2f251 3700 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
3701 if (retval != NULL)
3702 {
3703 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
3704 mips_opcodes[i].name, retval);
3705 /* Probably a memory allocation problem? Give up now. */
1661c76c 3706 as_fatal (_("broken assembler, no assembly attempted"));
252b5132
RH
3707 }
3708 do
3709 {
fc76e730
RS
3710 if (!validate_mips_insn (&mips_opcodes[i], 0xffffffff,
3711 decode_mips_operand, &mips_operands[i]))
3712 broken = 1;
6f2117ba 3713
fc76e730 3714 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
252b5132 3715 {
fc76e730
RS
3716 create_insn (&nop_insn, mips_opcodes + i);
3717 if (mips_fix_loongson2f_nop)
3718 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
3719 nop_insn.fixed_p = 1;
252b5132 3720 }
6f2117ba
PH
3721
3722 if (sync_insn.insn_mo == NULL && strcmp (name, "sync") == 0)
3723 create_insn (&sync_insn, mips_opcodes + i);
3724
252b5132
RH
3725 ++i;
3726 }
3727 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
3728 }
3729
3730 mips16_op_hash = hash_new ();
fc76e730
RS
3731 mips16_operands = XCNEWVEC (struct mips_operand_array,
3732 bfd_mips16_num_opcodes);
252b5132
RH
3733
3734 i = 0;
3735 while (i < bfd_mips16_num_opcodes)
3736 {
3737 const char *name = mips16_opcodes[i].name;
3738
17a2f251 3739 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
3740 if (retval != NULL)
3741 as_fatal (_("internal: can't hash `%s': %s"),
3742 mips16_opcodes[i].name, retval);
3743 do
3744 {
fc76e730
RS
3745 if (!validate_mips16_insn (&mips16_opcodes[i], &mips16_operands[i]))
3746 broken = 1;
1e915849
RS
3747 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3748 {
3749 create_insn (&mips16_nop_insn, mips16_opcodes + i);
3750 mips16_nop_insn.fixed_p = 1;
3751 }
252b5132
RH
3752 ++i;
3753 }
3754 while (i < bfd_mips16_num_opcodes
3755 && strcmp (mips16_opcodes[i].name, name) == 0);
3756 }
3757
df58fc94 3758 micromips_op_hash = hash_new ();
fc76e730
RS
3759 micromips_operands = XCNEWVEC (struct mips_operand_array,
3760 bfd_micromips_num_opcodes);
df58fc94
RS
3761
3762 i = 0;
3763 while (i < bfd_micromips_num_opcodes)
3764 {
3765 const char *name = micromips_opcodes[i].name;
3766
3767 retval = hash_insert (micromips_op_hash, name,
3768 (void *) &micromips_opcodes[i]);
3769 if (retval != NULL)
3770 as_fatal (_("internal: can't hash `%s': %s"),
3771 micromips_opcodes[i].name, retval);
3772 do
fc76e730
RS
3773 {
3774 struct mips_cl_insn *micromips_nop_insn;
3775
3776 if (!validate_micromips_insn (&micromips_opcodes[i],
3777 &micromips_operands[i]))
3778 broken = 1;
3779
3780 if (micromips_opcodes[i].pinfo != INSN_MACRO)
3781 {
3782 if (micromips_insn_length (micromips_opcodes + i) == 2)
3783 micromips_nop_insn = &micromips_nop16_insn;
3784 else if (micromips_insn_length (micromips_opcodes + i) == 4)
3785 micromips_nop_insn = &micromips_nop32_insn;
3786 else
3787 continue;
3788
3789 if (micromips_nop_insn->insn_mo == NULL
3790 && strcmp (name, "nop") == 0)
3791 {
3792 create_insn (micromips_nop_insn, micromips_opcodes + i);
3793 micromips_nop_insn->fixed_p = 1;
3794 }
3795 }
3796 }
df58fc94
RS
3797 while (++i < bfd_micromips_num_opcodes
3798 && strcmp (micromips_opcodes[i].name, name) == 0);
3799 }
3800
252b5132 3801 if (broken)
1661c76c 3802 as_fatal (_("broken assembler, no assembly attempted"));
252b5132
RH
3803
3804 /* We add all the general register names to the symbol table. This
3805 helps us detect invalid uses of them. */
3739860c 3806 for (i = 0; reg_names[i].name; i++)
707bfff6 3807 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
8fc4ee9b 3808 reg_names[i].num, /* & RNUM_MASK, */
707bfff6
TS
3809 &zero_address_frag));
3810 if (HAVE_NEWABI)
3739860c 3811 for (i = 0; reg_names_n32n64[i].name; i++)
707bfff6 3812 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
8fc4ee9b 3813 reg_names_n32n64[i].num, /* & RNUM_MASK, */
252b5132 3814 &zero_address_frag));
707bfff6 3815 else
3739860c 3816 for (i = 0; reg_names_o32[i].name; i++)
707bfff6 3817 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
8fc4ee9b 3818 reg_names_o32[i].num, /* & RNUM_MASK, */
6047c971 3819 &zero_address_frag));
6047c971 3820
14daeee3
RS
3821 for (i = 0; i < 32; i++)
3822 {
92fce9bd 3823 char regname[6];
14daeee3
RS
3824
3825 /* R5900 VU0 floating-point register. */
92fce9bd 3826 sprintf (regname, "$vf%d", i);
14daeee3
RS
3827 symbol_table_insert (symbol_new (regname, reg_section,
3828 RTYPE_VF | i, &zero_address_frag));
3829
3830 /* R5900 VU0 integer register. */
92fce9bd 3831 sprintf (regname, "$vi%d", i);
14daeee3
RS
3832 symbol_table_insert (symbol_new (regname, reg_section,
3833 RTYPE_VI | i, &zero_address_frag));
3834
56d438b1 3835 /* MSA register. */
92fce9bd 3836 sprintf (regname, "$w%d", i);
56d438b1
CF
3837 symbol_table_insert (symbol_new (regname, reg_section,
3838 RTYPE_MSA | i, &zero_address_frag));
14daeee3
RS
3839 }
3840
a92713e6
RS
3841 obstack_init (&mips_operand_tokens);
3842
7d10b47d 3843 mips_no_prev_insn ();
252b5132
RH
3844
3845 mips_gprmask = 0;
3846 mips_cprmask[0] = 0;
3847 mips_cprmask[1] = 0;
3848 mips_cprmask[2] = 0;
3849 mips_cprmask[3] = 0;
3850
3851 /* set the default alignment for the text section (2**2) */
3852 record_alignment (text_section, 2);
3853
4d0d148d 3854 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132 3855
f3ded42a
RS
3856 /* On a native system other than VxWorks, sections must be aligned
3857 to 16 byte boundaries. When configured for an embedded ELF
3858 target, we don't bother. */
3859 if (strncmp (TARGET_OS, "elf", 3) != 0
3860 && strncmp (TARGET_OS, "vxworks", 7) != 0)
252b5132 3861 {
f3ded42a
RS
3862 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
3863 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
3864 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
3865 }
252b5132 3866
f3ded42a
RS
3867 /* Create a .reginfo section for register masks and a .mdebug
3868 section for debugging information. */
3869 {
3870 segT seg;
3871 subsegT subseg;
3872 flagword flags;
3873 segT sec;
3874
3875 seg = now_seg;
3876 subseg = now_subseg;
3877
3878 /* The ABI says this section should be loaded so that the
3879 running program can access it. However, we don't load it
6f2117ba 3880 if we are configured for an embedded target. */
f3ded42a
RS
3881 flags = SEC_READONLY | SEC_DATA;
3882 if (strncmp (TARGET_OS, "elf", 3) != 0)
3883 flags |= SEC_ALLOC | SEC_LOAD;
3884
3885 if (mips_abi != N64_ABI)
252b5132 3886 {
f3ded42a 3887 sec = subseg_new (".reginfo", (subsegT) 0);
bdaaa2e1 3888
f3ded42a
RS
3889 bfd_set_section_flags (stdoutput, sec, flags);
3890 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
252b5132 3891
f3ded42a
RS
3892 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
3893 }
3894 else
3895 {
3896 /* The 64-bit ABI uses a .MIPS.options section rather than
3897 .reginfo section. */
3898 sec = subseg_new (".MIPS.options", (subsegT) 0);
3899 bfd_set_section_flags (stdoutput, sec, flags);
3900 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132 3901
f3ded42a
RS
3902 /* Set up the option header. */
3903 {
3904 Elf_Internal_Options opthdr;
3905 char *f;
3906
3907 opthdr.kind = ODK_REGINFO;
3908 opthdr.size = (sizeof (Elf_External_Options)
3909 + sizeof (Elf64_External_RegInfo));
3910 opthdr.section = 0;
3911 opthdr.info = 0;
3912 f = frag_more (sizeof (Elf_External_Options));
3913 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
3914 (Elf_External_Options *) f);
3915
3916 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
3917 }
3918 }
252b5132 3919
351cdf24
MF
3920 sec = subseg_new (".MIPS.abiflags", (subsegT) 0);
3921 bfd_set_section_flags (stdoutput, sec,
3922 SEC_READONLY | SEC_DATA | SEC_ALLOC | SEC_LOAD);
3923 bfd_set_section_alignment (stdoutput, sec, 3);
3924 mips_flags_frag = frag_more (sizeof (Elf_External_ABIFlags_v0));
3925
f3ded42a
RS
3926 if (ECOFF_DEBUGGING)
3927 {
3928 sec = subseg_new (".mdebug", (subsegT) 0);
3929 (void) bfd_set_section_flags (stdoutput, sec,
3930 SEC_HAS_CONTENTS | SEC_READONLY);
3931 (void) bfd_set_section_alignment (stdoutput, sec, 2);
252b5132 3932 }
f3ded42a
RS
3933 else if (mips_flag_pdr)
3934 {
3935 pdr_seg = subseg_new (".pdr", (subsegT) 0);
3936 (void) bfd_set_section_flags (stdoutput, pdr_seg,
3937 SEC_READONLY | SEC_RELOC
3938 | SEC_DEBUGGING);
3939 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
3940 }
3941
3942 subseg_set (seg, subseg);
3943 }
252b5132 3944
71400594
RS
3945 if (mips_fix_vr4120)
3946 init_vr4120_conflicts ();
252b5132
RH
3947}
3948
351cdf24
MF
3949static inline void
3950fpabi_incompatible_with (int fpabi, const char *what)
3951{
3952 as_warn (_(".gnu_attribute %d,%d is incompatible with `%s'"),
3953 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3954}
3955
3956static inline void
3957fpabi_requires (int fpabi, const char *what)
3958{
3959 as_warn (_(".gnu_attribute %d,%d requires `%s'"),
3960 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3961}
3962
3963/* Check -mabi and register sizes against the specified FP ABI. */
3964static void
3965check_fpabi (int fpabi)
3966{
351cdf24
MF
3967 switch (fpabi)
3968 {
3969 case Val_GNU_MIPS_ABI_FP_DOUBLE:
ea79f94a
MF
3970 if (file_mips_opts.soft_float)
3971 fpabi_incompatible_with (fpabi, "softfloat");
3972 else if (file_mips_opts.single_float)
3973 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3974 if (file_mips_opts.gp == 64 && file_mips_opts.fp == 32)
3975 fpabi_incompatible_with (fpabi, "gp=64 fp=32");
3976 else if (file_mips_opts.gp == 32 && file_mips_opts.fp == 64)
3977 fpabi_incompatible_with (fpabi, "gp=32 fp=64");
351cdf24
MF
3978 break;
3979
3980 case Val_GNU_MIPS_ABI_FP_XX:
3981 if (mips_abi != O32_ABI)
3982 fpabi_requires (fpabi, "-mabi=32");
ea79f94a
MF
3983 else if (file_mips_opts.soft_float)
3984 fpabi_incompatible_with (fpabi, "softfloat");
3985 else if (file_mips_opts.single_float)
3986 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3987 else if (file_mips_opts.fp != 0)
3988 fpabi_requires (fpabi, "fp=xx");
351cdf24
MF
3989 break;
3990
3991 case Val_GNU_MIPS_ABI_FP_64A:
3992 case Val_GNU_MIPS_ABI_FP_64:
3993 if (mips_abi != O32_ABI)
3994 fpabi_requires (fpabi, "-mabi=32");
ea79f94a
MF
3995 else if (file_mips_opts.soft_float)
3996 fpabi_incompatible_with (fpabi, "softfloat");
3997 else if (file_mips_opts.single_float)
3998 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3999 else if (file_mips_opts.fp != 64)
4000 fpabi_requires (fpabi, "fp=64");
4001 else if (fpabi == Val_GNU_MIPS_ABI_FP_64 && !file_mips_opts.oddspreg)
4002 fpabi_incompatible_with (fpabi, "nooddspreg");
4003 else if (fpabi == Val_GNU_MIPS_ABI_FP_64A && file_mips_opts.oddspreg)
4004 fpabi_requires (fpabi, "nooddspreg");
351cdf24
MF
4005 break;
4006
4007 case Val_GNU_MIPS_ABI_FP_SINGLE:
4008 if (file_mips_opts.soft_float)
4009 fpabi_incompatible_with (fpabi, "softfloat");
4010 else if (!file_mips_opts.single_float)
4011 fpabi_requires (fpabi, "singlefloat");
4012 break;
4013
4014 case Val_GNU_MIPS_ABI_FP_SOFT:
4015 if (!file_mips_opts.soft_float)
4016 fpabi_requires (fpabi, "softfloat");
4017 break;
4018
4019 case Val_GNU_MIPS_ABI_FP_OLD_64:
4020 as_warn (_(".gnu_attribute %d,%d is no longer supported"),
4021 Tag_GNU_MIPS_ABI_FP, fpabi);
4022 break;
4023
3350cc01
CM
4024 case Val_GNU_MIPS_ABI_FP_NAN2008:
4025 /* Silently ignore compatibility value. */
4026 break;
4027
351cdf24
MF
4028 default:
4029 as_warn (_(".gnu_attribute %d,%d is not a recognized"
4030 " floating-point ABI"), Tag_GNU_MIPS_ABI_FP, fpabi);
4031 break;
4032 }
351cdf24
MF
4033}
4034
919731af 4035/* Perform consistency checks on the current options. */
4036
4037static void
4038mips_check_options (struct mips_set_options *opts, bfd_boolean abi_checks)
4039{
4040 /* Check the size of integer registers agrees with the ABI and ISA. */
4041 if (opts->gp == 64 && !ISA_HAS_64BIT_REGS (opts->isa))
4042 as_bad (_("`gp=64' used with a 32-bit processor"));
4043 else if (abi_checks
4044 && opts->gp == 32 && ABI_NEEDS_64BIT_REGS (mips_abi))
4045 as_bad (_("`gp=32' used with a 64-bit ABI"));
4046 else if (abi_checks
4047 && opts->gp == 64 && ABI_NEEDS_32BIT_REGS (mips_abi))
4048 as_bad (_("`gp=64' used with a 32-bit ABI"));
4049
4050 /* Check the size of the float registers agrees with the ABI and ISA. */
4051 switch (opts->fp)
4052 {
351cdf24
MF
4053 case 0:
4054 if (!CPU_HAS_LDC1_SDC1 (opts->arch))
4055 as_bad (_("`fp=xx' used with a cpu lacking ldc1/sdc1 instructions"));
4056 else if (opts->single_float == 1)
4057 as_bad (_("`fp=xx' cannot be used with `singlefloat'"));
4058 break;
919731af 4059 case 64:
4060 if (!ISA_HAS_64BIT_FPRS (opts->isa))
4061 as_bad (_("`fp=64' used with a 32-bit fpu"));
4062 else if (abi_checks
4063 && ABI_NEEDS_32BIT_REGS (mips_abi)
4064 && !ISA_HAS_MXHC1 (opts->isa))
4065 as_warn (_("`fp=64' used with a 32-bit ABI"));
4066 break;
4067 case 32:
4068 if (abi_checks
4069 && ABI_NEEDS_64BIT_REGS (mips_abi))
4070 as_warn (_("`fp=32' used with a 64-bit ABI"));
5f4678bb 4071 if (ISA_IS_R6 (opts->isa) && opts->single_float == 0)
7361da2c 4072 as_bad (_("`fp=32' used with a MIPS R6 cpu"));
919731af 4073 break;
4074 default:
4075 as_bad (_("Unknown size of floating point registers"));
4076 break;
4077 }
4078
351cdf24
MF
4079 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !opts->oddspreg)
4080 as_bad (_("`nooddspreg` cannot be used with a 64-bit ABI"));
4081
919731af 4082 if (opts->micromips == 1 && opts->mips16 == 1)
1357373c 4083 as_bad (_("`%s' cannot be used with `%s'"), "mips16", "micromips");
5f4678bb 4084 else if (ISA_IS_R6 (opts->isa)
7361da2c
AB
4085 && (opts->micromips == 1
4086 || opts->mips16 == 1))
1357373c 4087 as_fatal (_("`%s' cannot be used with `%s'"),
7361da2c 4088 opts->micromips ? "micromips" : "mips16",
5f4678bb 4089 mips_cpu_info_from_isa (opts->isa)->name);
7361da2c
AB
4090
4091 if (ISA_IS_R6 (opts->isa) && mips_relax_branch)
4092 as_fatal (_("branch relaxation is not supported in `%s'"),
4093 mips_cpu_info_from_isa (opts->isa)->name);
919731af 4094}
4095
4096/* Perform consistency checks on the module level options exactly once.
4097 This is a deferred check that happens:
4098 at the first .set directive
4099 or, at the first pseudo op that generates code (inc .dc.a)
4100 or, at the first instruction
4101 or, at the end. */
4102
4103static void
4104file_mips_check_options (void)
4105{
919731af 4106 if (file_mips_opts_checked)
4107 return;
4108
4109 /* The following code determines the register size.
4110 Similar code was added to GCC 3.3 (see override_options() in
4111 config/mips/mips.c). The GAS and GCC code should be kept in sync
4112 as much as possible. */
4113
4114 if (file_mips_opts.gp < 0)
4115 {
4116 /* Infer the integer register size from the ABI and processor.
4117 Restrict ourselves to 32-bit registers if that's all the
4118 processor has, or if the ABI cannot handle 64-bit registers. */
4119 file_mips_opts.gp = (ABI_NEEDS_32BIT_REGS (mips_abi)
4120 || !ISA_HAS_64BIT_REGS (file_mips_opts.isa))
4121 ? 32 : 64;
4122 }
4123
4124 if (file_mips_opts.fp < 0)
4125 {
4126 /* No user specified float register size.
4127 ??? GAS treats single-float processors as though they had 64-bit
4128 float registers (although it complains when double-precision
4129 instructions are used). As things stand, saying they have 32-bit
4130 registers would lead to spurious "register must be even" messages.
4131 So here we assume float registers are never smaller than the
4132 integer ones. */
4133 if (file_mips_opts.gp == 64)
4134 /* 64-bit integer registers implies 64-bit float registers. */
4135 file_mips_opts.fp = 64;
4136 else if ((file_mips_opts.ase & FP64_ASES)
4137 && ISA_HAS_64BIT_FPRS (file_mips_opts.isa))
4138 /* Handle ASEs that require 64-bit float registers, if possible. */
4139 file_mips_opts.fp = 64;
7361da2c
AB
4140 else if (ISA_IS_R6 (mips_opts.isa))
4141 /* R6 implies 64-bit float registers. */
4142 file_mips_opts.fp = 64;
919731af 4143 else
4144 /* 32-bit float registers. */
4145 file_mips_opts.fp = 32;
4146 }
4147
351cdf24
MF
4148 /* Disable operations on odd-numbered floating-point registers by default
4149 when using the FPXX ABI. */
4150 if (file_mips_opts.oddspreg < 0)
4151 {
4152 if (file_mips_opts.fp == 0)
4153 file_mips_opts.oddspreg = 0;
4154 else
4155 file_mips_opts.oddspreg = 1;
4156 }
4157
919731af 4158 /* End of GCC-shared inference code. */
4159
4160 /* This flag is set when we have a 64-bit capable CPU but use only
4161 32-bit wide registers. Note that EABI does not use it. */
4162 if (ISA_HAS_64BIT_REGS (file_mips_opts.isa)
4163 && ((mips_abi == NO_ABI && file_mips_opts.gp == 32)
4164 || mips_abi == O32_ABI))
4165 mips_32bitmode = 1;
4166
4167 if (file_mips_opts.isa == ISA_MIPS1 && mips_trap)
4168 as_bad (_("trap exception not supported at ISA 1"));
4169
4170 /* If the selected architecture includes support for ASEs, enable
4171 generation of code for them. */
4172 if (file_mips_opts.mips16 == -1)
4173 file_mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_opts.arch)) ? 1 : 0;
4174 if (file_mips_opts.micromips == -1)
4175 file_mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_opts.arch))
4176 ? 1 : 0;
4177
7361da2c
AB
4178 if (mips_nan2008 == -1)
4179 mips_nan2008 = (ISA_HAS_LEGACY_NAN (file_mips_opts.isa)) ? 0 : 1;
4180 else if (!ISA_HAS_LEGACY_NAN (file_mips_opts.isa) && mips_nan2008 == 0)
4181 as_fatal (_("`%s' does not support legacy NaN"),
4182 mips_cpu_info_from_arch (file_mips_opts.arch)->name);
4183
919731af 4184 /* Some ASEs require 64-bit FPRs, so -mfp32 should stop those ASEs from
4185 being selected implicitly. */
4186 if (file_mips_opts.fp != 64)
4187 file_ase_explicit |= ASE_MIPS3D | ASE_MDMX | ASE_MSA;
4188
4189 /* If the user didn't explicitly select or deselect a particular ASE,
4190 use the default setting for the CPU. */
3315614d 4191 file_mips_opts.ase |= (file_mips_opts.init_ase & ~file_ase_explicit);
919731af 4192
4193 /* Set up the current options. These may change throughout assembly. */
4194 mips_opts = file_mips_opts;
4195
4196 mips_check_isa_supports_ases ();
4197 mips_check_options (&file_mips_opts, TRUE);
4198 file_mips_opts_checked = TRUE;
4199
4200 if (!bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
4201 as_warn (_("could not set architecture and machine"));
4202}
4203
252b5132 4204void
17a2f251 4205md_assemble (char *str)
252b5132
RH
4206{
4207 struct mips_cl_insn insn;
f6688943
TS
4208 bfd_reloc_code_real_type unused_reloc[3]
4209 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 4210
919731af 4211 file_mips_check_options ();
4212
252b5132 4213 imm_expr.X_op = O_absent;
252b5132 4214 offset_expr.X_op = O_absent;
f6688943
TS
4215 offset_reloc[0] = BFD_RELOC_UNUSED;
4216 offset_reloc[1] = BFD_RELOC_UNUSED;
4217 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132 4218
e1b47bd5
RS
4219 mips_mark_labels ();
4220 mips_assembling_insn = TRUE;
e3de51ce 4221 clear_insn_error ();
e1b47bd5 4222
252b5132
RH
4223 if (mips_opts.mips16)
4224 mips16_ip (str, &insn);
4225 else
4226 {
4227 mips_ip (str, &insn);
beae10d5
KH
4228 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
4229 str, insn.insn_opcode));
252b5132
RH
4230 }
4231
e3de51ce
RS
4232 if (insn_error.msg)
4233 report_insn_error (str);
e1b47bd5 4234 else if (insn.insn_mo->pinfo == INSN_MACRO)
252b5132 4235 {
584892a6 4236 macro_start ();
252b5132
RH
4237 if (mips_opts.mips16)
4238 mips16_macro (&insn);
4239 else
833794fc 4240 macro (&insn, str);
584892a6 4241 macro_end ();
252b5132
RH
4242 }
4243 else
4244 {
77bd4346 4245 if (offset_expr.X_op != O_absent)
df58fc94 4246 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
252b5132 4247 else
df58fc94 4248 append_insn (&insn, NULL, unused_reloc, FALSE);
252b5132 4249 }
e1b47bd5
RS
4250
4251 mips_assembling_insn = FALSE;
252b5132
RH
4252}
4253
738e5348
RS
4254/* Convenience functions for abstracting away the differences between
4255 MIPS16 and non-MIPS16 relocations. */
4256
4257static inline bfd_boolean
4258mips16_reloc_p (bfd_reloc_code_real_type reloc)
4259{
4260 switch (reloc)
4261 {
4262 case BFD_RELOC_MIPS16_JMP:
4263 case BFD_RELOC_MIPS16_GPREL:
4264 case BFD_RELOC_MIPS16_GOT16:
4265 case BFD_RELOC_MIPS16_CALL16:
4266 case BFD_RELOC_MIPS16_HI16_S:
4267 case BFD_RELOC_MIPS16_HI16:
4268 case BFD_RELOC_MIPS16_LO16:
c9775dde 4269 case BFD_RELOC_MIPS16_16_PCREL_S1:
738e5348
RS
4270 return TRUE;
4271
4272 default:
4273 return FALSE;
4274 }
4275}
4276
df58fc94
RS
4277static inline bfd_boolean
4278micromips_reloc_p (bfd_reloc_code_real_type reloc)
4279{
4280 switch (reloc)
4281 {
4282 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4283 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4284 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4285 case BFD_RELOC_MICROMIPS_GPREL16:
4286 case BFD_RELOC_MICROMIPS_JMP:
4287 case BFD_RELOC_MICROMIPS_HI16:
4288 case BFD_RELOC_MICROMIPS_HI16_S:
4289 case BFD_RELOC_MICROMIPS_LO16:
4290 case BFD_RELOC_MICROMIPS_LITERAL:
4291 case BFD_RELOC_MICROMIPS_GOT16:
4292 case BFD_RELOC_MICROMIPS_CALL16:
4293 case BFD_RELOC_MICROMIPS_GOT_HI16:
4294 case BFD_RELOC_MICROMIPS_GOT_LO16:
4295 case BFD_RELOC_MICROMIPS_CALL_HI16:
4296 case BFD_RELOC_MICROMIPS_CALL_LO16:
4297 case BFD_RELOC_MICROMIPS_SUB:
4298 case BFD_RELOC_MICROMIPS_GOT_PAGE:
4299 case BFD_RELOC_MICROMIPS_GOT_OFST:
4300 case BFD_RELOC_MICROMIPS_GOT_DISP:
4301 case BFD_RELOC_MICROMIPS_HIGHEST:
4302 case BFD_RELOC_MICROMIPS_HIGHER:
4303 case BFD_RELOC_MICROMIPS_SCN_DISP:
4304 case BFD_RELOC_MICROMIPS_JALR:
4305 return TRUE;
4306
4307 default:
4308 return FALSE;
4309 }
4310}
4311
2309ddf2
MR
4312static inline bfd_boolean
4313jmp_reloc_p (bfd_reloc_code_real_type reloc)
4314{
4315 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
4316}
4317
0e9c5a5c
MR
4318static inline bfd_boolean
4319b_reloc_p (bfd_reloc_code_real_type reloc)
4320{
4321 return (reloc == BFD_RELOC_MIPS_26_PCREL_S2
4322 || reloc == BFD_RELOC_MIPS_21_PCREL_S2
4323 || reloc == BFD_RELOC_16_PCREL_S2
c9775dde 4324 || reloc == BFD_RELOC_MIPS16_16_PCREL_S1
0e9c5a5c
MR
4325 || reloc == BFD_RELOC_MICROMIPS_16_PCREL_S1
4326 || reloc == BFD_RELOC_MICROMIPS_10_PCREL_S1
4327 || reloc == BFD_RELOC_MICROMIPS_7_PCREL_S1);
4328}
4329
738e5348
RS
4330static inline bfd_boolean
4331got16_reloc_p (bfd_reloc_code_real_type reloc)
4332{
2309ddf2 4333 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
df58fc94 4334 || reloc == BFD_RELOC_MICROMIPS_GOT16);
738e5348
RS
4335}
4336
4337static inline bfd_boolean
4338hi16_reloc_p (bfd_reloc_code_real_type reloc)
4339{
2309ddf2 4340 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
df58fc94 4341 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
738e5348
RS
4342}
4343
4344static inline bfd_boolean
4345lo16_reloc_p (bfd_reloc_code_real_type reloc)
4346{
2309ddf2 4347 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
df58fc94
RS
4348 || reloc == BFD_RELOC_MICROMIPS_LO16);
4349}
4350
df58fc94
RS
4351static inline bfd_boolean
4352jalr_reloc_p (bfd_reloc_code_real_type reloc)
4353{
2309ddf2 4354 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
738e5348
RS
4355}
4356
f2ae14a1
RS
4357static inline bfd_boolean
4358gprel16_reloc_p (bfd_reloc_code_real_type reloc)
4359{
4360 return (reloc == BFD_RELOC_GPREL16 || reloc == BFD_RELOC_MIPS16_GPREL
4361 || reloc == BFD_RELOC_MICROMIPS_GPREL16);
4362}
4363
2de39019
CM
4364/* Return true if RELOC is a PC-relative relocation that does not have
4365 full address range. */
4366
4367static inline bfd_boolean
4368limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
4369{
4370 switch (reloc)
4371 {
4372 case BFD_RELOC_16_PCREL_S2:
c9775dde 4373 case BFD_RELOC_MIPS16_16_PCREL_S1:
2de39019
CM
4374 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4375 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4376 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
7361da2c
AB
4377 case BFD_RELOC_MIPS_21_PCREL_S2:
4378 case BFD_RELOC_MIPS_26_PCREL_S2:
4379 case BFD_RELOC_MIPS_18_PCREL_S3:
4380 case BFD_RELOC_MIPS_19_PCREL_S2:
2de39019
CM
4381 return TRUE;
4382
b47468a6 4383 case BFD_RELOC_32_PCREL:
7361da2c
AB
4384 case BFD_RELOC_HI16_S_PCREL:
4385 case BFD_RELOC_LO16_PCREL:
b47468a6
CM
4386 return HAVE_64BIT_ADDRESSES;
4387
2de39019
CM
4388 default:
4389 return FALSE;
4390 }
4391}
b47468a6 4392
5919d012 4393/* Return true if the given relocation might need a matching %lo().
0a44bf69
RS
4394 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
4395 need a matching %lo() when applied to local symbols. */
5919d012
RS
4396
4397static inline bfd_boolean
17a2f251 4398reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 4399{
3b91255e 4400 return (HAVE_IN_PLACE_ADDENDS
738e5348 4401 && (hi16_reloc_p (reloc)
0a44bf69
RS
4402 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
4403 all GOT16 relocations evaluate to "G". */
738e5348
RS
4404 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
4405}
4406
4407/* Return the type of %lo() reloc needed by RELOC, given that
4408 reloc_needs_lo_p. */
4409
4410static inline bfd_reloc_code_real_type
4411matching_lo_reloc (bfd_reloc_code_real_type reloc)
4412{
df58fc94
RS
4413 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
4414 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
4415 : BFD_RELOC_LO16));
5919d012
RS
4416}
4417
4418/* Return true if the given fixup is followed by a matching R_MIPS_LO16
4419 relocation. */
4420
4421static inline bfd_boolean
17a2f251 4422fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
4423{
4424 return (fixp->fx_next != NULL
738e5348 4425 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
5919d012
RS
4426 && fixp->fx_addsy == fixp->fx_next->fx_addsy
4427 && fixp->fx_offset == fixp->fx_next->fx_offset);
4428}
4429
462427c4
RS
4430/* Move all labels in LABELS to the current insertion point. TEXT_P
4431 says whether the labels refer to text or data. */
404a8071
RS
4432
4433static void
462427c4 4434mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
404a8071
RS
4435{
4436 struct insn_label_list *l;
4437 valueT val;
4438
462427c4 4439 for (l = labels; l != NULL; l = l->next)
404a8071 4440 {
9c2799c2 4441 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
404a8071
RS
4442 symbol_set_frag (l->label, frag_now);
4443 val = (valueT) frag_now_fix ();
770c0151
FS
4444 /* MIPS16/microMIPS text labels are stored as odd.
4445 We just carry the ISA mode bit forward. */
462427c4 4446 if (text_p && HAVE_CODE_COMPRESSION)
770c0151 4447 val |= (S_GET_VALUE (l->label) & 0x1);
404a8071
RS
4448 S_SET_VALUE (l->label, val);
4449 }
4450}
4451
462427c4
RS
4452/* Move all labels in insn_labels to the current insertion point
4453 and treat them as text labels. */
4454
4455static void
4456mips_move_text_labels (void)
4457{
4458 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
4459}
4460
9e009953
MR
4461/* Duplicate the test for LINK_ONCE sections as in `adjust_reloc_syms'. */
4462
5f0fe04b
TS
4463static bfd_boolean
4464s_is_linkonce (symbolS *sym, segT from_seg)
4465{
4466 bfd_boolean linkonce = FALSE;
4467 segT symseg = S_GET_SEGMENT (sym);
4468
4469 if (symseg != from_seg && !S_IS_LOCAL (sym))
4470 {
4471 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
4472 linkonce = TRUE;
5f0fe04b
TS
4473 /* The GNU toolchain uses an extension for ELF: a section
4474 beginning with the magic string .gnu.linkonce is a
4475 linkonce section. */
4476 if (strncmp (segment_name (symseg), ".gnu.linkonce",
4477 sizeof ".gnu.linkonce" - 1) == 0)
4478 linkonce = TRUE;
5f0fe04b
TS
4479 }
4480 return linkonce;
4481}
4482
e1b47bd5 4483/* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
df58fc94
RS
4484 linker to handle them specially, such as generating jalx instructions
4485 when needed. We also make them odd for the duration of the assembly,
4486 in order to generate the right sort of code. We will make them even
252b5132
RH
4487 in the adjust_symtab routine, while leaving them marked. This is
4488 convenient for the debugger and the disassembler. The linker knows
4489 to make them odd again. */
4490
4491static void
e1b47bd5 4492mips_compressed_mark_label (symbolS *label)
252b5132 4493{
df58fc94 4494 gas_assert (HAVE_CODE_COMPRESSION);
a8dbcb85 4495
f3ded42a
RS
4496 if (mips_opts.mips16)
4497 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
4498 else
4499 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
e1b47bd5
RS
4500 if ((S_GET_VALUE (label) & 1) == 0
4501 /* Don't adjust the address if the label is global or weak, or
4502 in a link-once section, since we'll be emitting symbol reloc
4503 references to it which will be patched up by the linker, and
4504 the final value of the symbol may or may not be MIPS16/microMIPS. */
4505 && !S_IS_WEAK (label)
4506 && !S_IS_EXTERNAL (label)
4507 && !s_is_linkonce (label, now_seg))
4508 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
4509}
4510
4511/* Mark preceding MIPS16 or microMIPS instruction labels. */
4512
4513static void
4514mips_compressed_mark_labels (void)
4515{
4516 struct insn_label_list *l;
4517
4518 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
4519 mips_compressed_mark_label (l->label);
252b5132
RH
4520}
4521
4d7206a2
RS
4522/* End the current frag. Make it a variant frag and record the
4523 relaxation info. */
4524
4525static void
4526relax_close_frag (void)
4527{
584892a6 4528 mips_macro_warning.first_frag = frag_now;
4d7206a2 4529 frag_var (rs_machine_dependent, 0, 0,
ce8ad872
MR
4530 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1],
4531 mips_pic != NO_PIC),
4d7206a2
RS
4532 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
4533
4534 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
4535 mips_relax.first_fixup = 0;
4536}
4537
4538/* Start a new relaxation sequence whose expansion depends on SYMBOL.
4539 See the comment above RELAX_ENCODE for more details. */
4540
4541static void
4542relax_start (symbolS *symbol)
4543{
9c2799c2 4544 gas_assert (mips_relax.sequence == 0);
4d7206a2
RS
4545 mips_relax.sequence = 1;
4546 mips_relax.symbol = symbol;
4547}
4548
4549/* Start generating the second version of a relaxable sequence.
4550 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
4551
4552static void
4d7206a2
RS
4553relax_switch (void)
4554{
9c2799c2 4555 gas_assert (mips_relax.sequence == 1);
4d7206a2
RS
4556 mips_relax.sequence = 2;
4557}
4558
4559/* End the current relaxable sequence. */
4560
4561static void
4562relax_end (void)
4563{
9c2799c2 4564 gas_assert (mips_relax.sequence == 2);
4d7206a2
RS
4565 relax_close_frag ();
4566 mips_relax.sequence = 0;
4567}
4568
11625dd8
RS
4569/* Return true if IP is a delayed branch or jump. */
4570
4571static inline bfd_boolean
4572delayed_branch_p (const struct mips_cl_insn *ip)
4573{
4574 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
4575 | INSN_COND_BRANCH_DELAY
4576 | INSN_COND_BRANCH_LIKELY)) != 0;
4577}
4578
4579/* Return true if IP is a compact branch or jump. */
4580
4581static inline bfd_boolean
4582compact_branch_p (const struct mips_cl_insn *ip)
4583{
26545944
RS
4584 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
4585 | INSN2_COND_BRANCH)) != 0;
11625dd8
RS
4586}
4587
4588/* Return true if IP is an unconditional branch or jump. */
4589
4590static inline bfd_boolean
4591uncond_branch_p (const struct mips_cl_insn *ip)
4592{
4593 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
26545944 4594 || (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0);
11625dd8
RS
4595}
4596
4597/* Return true if IP is a branch-likely instruction. */
4598
4599static inline bfd_boolean
4600branch_likely_p (const struct mips_cl_insn *ip)
4601{
4602 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
4603}
4604
14fe068b
RS
4605/* Return the type of nop that should be used to fill the delay slot
4606 of delayed branch IP. */
4607
4608static struct mips_cl_insn *
4609get_delay_slot_nop (const struct mips_cl_insn *ip)
4610{
4611 if (mips_opts.micromips
4612 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
4613 return &micromips_nop32_insn;
4614 return NOP_INSN;
4615}
4616
fc76e730
RS
4617/* Return a mask that has bit N set if OPCODE reads the register(s)
4618 in operand N. */
df58fc94
RS
4619
4620static unsigned int
fc76e730 4621insn_read_mask (const struct mips_opcode *opcode)
df58fc94 4622{
fc76e730
RS
4623 return (opcode->pinfo & INSN_READ_ALL) >> INSN_READ_SHIFT;
4624}
df58fc94 4625
fc76e730
RS
4626/* Return a mask that has bit N set if OPCODE writes to the register(s)
4627 in operand N. */
4628
4629static unsigned int
4630insn_write_mask (const struct mips_opcode *opcode)
4631{
4632 return (opcode->pinfo & INSN_WRITE_ALL) >> INSN_WRITE_SHIFT;
4633}
4634
4635/* Return a mask of the registers specified by operand OPERAND of INSN.
4636 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4637 is set. */
4638
4639static unsigned int
4640operand_reg_mask (const struct mips_cl_insn *insn,
4641 const struct mips_operand *operand,
4642 unsigned int type_mask)
4643{
4644 unsigned int uval, vsel;
4645
4646 switch (operand->type)
df58fc94 4647 {
fc76e730
RS
4648 case OP_INT:
4649 case OP_MAPPED_INT:
4650 case OP_MSB:
4651 case OP_PCREL:
4652 case OP_PERF_REG:
4653 case OP_ADDIUSP_INT:
4654 case OP_ENTRY_EXIT_LIST:
4655 case OP_REPEAT_DEST_REG:
4656 case OP_REPEAT_PREV_REG:
4657 case OP_PC:
14daeee3
RS
4658 case OP_VU0_SUFFIX:
4659 case OP_VU0_MATCH_SUFFIX:
56d438b1 4660 case OP_IMM_INDEX:
fc76e730
RS
4661 abort ();
4662
25499ac7
MR
4663 case OP_REG28:
4664 return 1 << 28;
4665
fc76e730 4666 case OP_REG:
0f35dbc4 4667 case OP_OPTIONAL_REG:
fc76e730
RS
4668 {
4669 const struct mips_reg_operand *reg_op;
4670
4671 reg_op = (const struct mips_reg_operand *) operand;
4672 if (!(type_mask & (1 << reg_op->reg_type)))
4673 return 0;
4674 uval = insn_extract_operand (insn, operand);
4675 return 1 << mips_decode_reg_operand (reg_op, uval);
4676 }
4677
4678 case OP_REG_PAIR:
4679 {
4680 const struct mips_reg_pair_operand *pair_op;
4681
4682 pair_op = (const struct mips_reg_pair_operand *) operand;
4683 if (!(type_mask & (1 << pair_op->reg_type)))
4684 return 0;
4685 uval = insn_extract_operand (insn, operand);
4686 return (1 << pair_op->reg1_map[uval]) | (1 << pair_op->reg2_map[uval]);
4687 }
4688
4689 case OP_CLO_CLZ_DEST:
4690 if (!(type_mask & (1 << OP_REG_GP)))
4691 return 0;
4692 uval = insn_extract_operand (insn, operand);
4693 return (1 << (uval & 31)) | (1 << (uval >> 5));
4694
7361da2c
AB
4695 case OP_SAME_RS_RT:
4696 if (!(type_mask & (1 << OP_REG_GP)))
4697 return 0;
4698 uval = insn_extract_operand (insn, operand);
4699 gas_assert ((uval & 31) == (uval >> 5));
4700 return 1 << (uval & 31);
4701
4702 case OP_CHECK_PREV:
4703 case OP_NON_ZERO_REG:
4704 if (!(type_mask & (1 << OP_REG_GP)))
4705 return 0;
4706 uval = insn_extract_operand (insn, operand);
4707 return 1 << (uval & 31);
4708
fc76e730
RS
4709 case OP_LWM_SWM_LIST:
4710 abort ();
4711
4712 case OP_SAVE_RESTORE_LIST:
4713 abort ();
4714
4715 case OP_MDMX_IMM_REG:
4716 if (!(type_mask & (1 << OP_REG_VEC)))
4717 return 0;
4718 uval = insn_extract_operand (insn, operand);
4719 vsel = uval >> 5;
4720 if ((vsel & 0x18) == 0x18)
4721 return 0;
4722 return 1 << (uval & 31);
56d438b1
CF
4723
4724 case OP_REG_INDEX:
4725 if (!(type_mask & (1 << OP_REG_GP)))
4726 return 0;
4727 return 1 << insn_extract_operand (insn, operand);
df58fc94 4728 }
fc76e730
RS
4729 abort ();
4730}
4731
4732/* Return a mask of the registers specified by operands OPNO_MASK of INSN,
4733 where bit N of OPNO_MASK is set if operand N should be included.
4734 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4735 is set. */
4736
4737static unsigned int
4738insn_reg_mask (const struct mips_cl_insn *insn,
4739 unsigned int type_mask, unsigned int opno_mask)
4740{
4741 unsigned int opno, reg_mask;
4742
4743 opno = 0;
4744 reg_mask = 0;
4745 while (opno_mask != 0)
4746 {
4747 if (opno_mask & 1)
4748 reg_mask |= operand_reg_mask (insn, insn_opno (insn, opno), type_mask);
4749 opno_mask >>= 1;
4750 opno += 1;
4751 }
4752 return reg_mask;
df58fc94
RS
4753}
4754
4c260379
RS
4755/* Return the mask of core registers that IP reads. */
4756
4757static unsigned int
4758gpr_read_mask (const struct mips_cl_insn *ip)
4759{
4760 unsigned long pinfo, pinfo2;
4761 unsigned int mask;
4762
fc76e730 4763 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_read_mask (ip->insn_mo));
4c260379
RS
4764 pinfo = ip->insn_mo->pinfo;
4765 pinfo2 = ip->insn_mo->pinfo2;
fc76e730 4766 if (pinfo & INSN_UDI)
4c260379 4767 {
fc76e730
RS
4768 /* UDI instructions have traditionally been assumed to read RS
4769 and RT. */
4770 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4771 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4c260379 4772 }
fc76e730
RS
4773 if (pinfo & INSN_READ_GPR_24)
4774 mask |= 1 << 24;
4775 if (pinfo2 & INSN2_READ_GPR_16)
4776 mask |= 1 << 16;
4777 if (pinfo2 & INSN2_READ_SP)
4778 mask |= 1 << SP;
26545944 4779 if (pinfo2 & INSN2_READ_GPR_31)
fc76e730 4780 mask |= 1 << 31;
fe35f09f
RS
4781 /* Don't include register 0. */
4782 return mask & ~1;
4c260379
RS
4783}
4784
4785/* Return the mask of core registers that IP writes. */
4786
4787static unsigned int
4788gpr_write_mask (const struct mips_cl_insn *ip)
4789{
4790 unsigned long pinfo, pinfo2;
4791 unsigned int mask;
4792
fc76e730 4793 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_write_mask (ip->insn_mo));
4c260379
RS
4794 pinfo = ip->insn_mo->pinfo;
4795 pinfo2 = ip->insn_mo->pinfo2;
fc76e730
RS
4796 if (pinfo & INSN_WRITE_GPR_24)
4797 mask |= 1 << 24;
4798 if (pinfo & INSN_WRITE_GPR_31)
4799 mask |= 1 << 31;
4800 if (pinfo & INSN_UDI)
4801 /* UDI instructions have traditionally been assumed to write to RD. */
4802 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4803 if (pinfo2 & INSN2_WRITE_SP)
4804 mask |= 1 << SP;
fe35f09f
RS
4805 /* Don't include register 0. */
4806 return mask & ~1;
4c260379
RS
4807}
4808
4809/* Return the mask of floating-point registers that IP reads. */
4810
4811static unsigned int
4812fpr_read_mask (const struct mips_cl_insn *ip)
4813{
fc76e730 4814 unsigned long pinfo;
4c260379
RS
4815 unsigned int mask;
4816
9d5de888
CF
4817 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4818 | (1 << OP_REG_MSA)),
fc76e730 4819 insn_read_mask (ip->insn_mo));
4c260379 4820 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4821 /* Conservatively treat all operands to an FP_D instruction are doubles.
4822 (This is overly pessimistic for things like cvt.d.s.) */
bad1aba3 4823 if (FPR_SIZE != 64 && (pinfo & FP_D))
4c260379
RS
4824 mask |= mask << 1;
4825 return mask;
4826}
4827
4828/* Return the mask of floating-point registers that IP writes. */
4829
4830static unsigned int
4831fpr_write_mask (const struct mips_cl_insn *ip)
4832{
fc76e730 4833 unsigned long pinfo;
4c260379
RS
4834 unsigned int mask;
4835
9d5de888
CF
4836 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4837 | (1 << OP_REG_MSA)),
fc76e730 4838 insn_write_mask (ip->insn_mo));
4c260379 4839 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4840 /* Conservatively treat all operands to an FP_D instruction are doubles.
4841 (This is overly pessimistic for things like cvt.s.d.) */
bad1aba3 4842 if (FPR_SIZE != 64 && (pinfo & FP_D))
4c260379
RS
4843 mask |= mask << 1;
4844 return mask;
4845}
4846
a1d78564
RS
4847/* Operand OPNUM of INSN is an odd-numbered floating-point register.
4848 Check whether that is allowed. */
4849
4850static bfd_boolean
4851mips_oddfpreg_ok (const struct mips_opcode *insn, int opnum)
4852{
4853 const char *s = insn->name;
351cdf24
MF
4854 bfd_boolean oddspreg = (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa, mips_opts.arch)
4855 || FPR_SIZE == 64)
4856 && mips_opts.oddspreg;
a1d78564
RS
4857
4858 if (insn->pinfo == INSN_MACRO)
4859 /* Let a macro pass, we'll catch it later when it is expanded. */
4860 return TRUE;
4861
351cdf24
MF
4862 /* Single-precision coprocessor loads and moves are OK for 32-bit registers,
4863 otherwise it depends on oddspreg. */
4864 if ((insn->pinfo & FP_S)
4865 && (insn->pinfo & (INSN_LOAD_MEMORY | INSN_STORE_MEMORY
43885403 4866 | INSN_LOAD_COPROC | INSN_COPROC_MOVE)))
351cdf24 4867 return FPR_SIZE == 32 || oddspreg;
a1d78564 4868
351cdf24
MF
4869 /* Allow odd registers for single-precision ops and double-precision if the
4870 floating-point registers are 64-bit wide. */
4871 switch (insn->pinfo & (FP_S | FP_D))
4872 {
4873 case FP_S:
4874 case 0:
4875 return oddspreg;
4876 case FP_D:
4877 return FPR_SIZE == 64;
4878 default:
4879 break;
a1d78564
RS
4880 }
4881
351cdf24
MF
4882 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
4883 s = strchr (insn->name, '.');
4884 if (s != NULL && opnum == 2)
4885 s = strchr (s + 1, '.');
4886 if (s != NULL && (s[1] == 'w' || s[1] == 's'))
4887 return oddspreg;
a1d78564 4888
351cdf24 4889 return FPR_SIZE == 64;
a1d78564
RS
4890}
4891
a1d78564
RS
4892/* Information about an instruction argument that we're trying to match. */
4893struct mips_arg_info
4894{
4895 /* The instruction so far. */
4896 struct mips_cl_insn *insn;
4897
a92713e6
RS
4898 /* The first unconsumed operand token. */
4899 struct mips_operand_token *token;
4900
a1d78564
RS
4901 /* The 1-based operand number, in terms of insn->insn_mo->args. */
4902 int opnum;
4903
4904 /* The 1-based argument number, for error reporting. This does not
4905 count elided optional registers, etc.. */
4906 int argnum;
4907
4908 /* The last OP_REG operand seen, or ILLEGAL_REG if none. */
4909 unsigned int last_regno;
4910
4911 /* If the first operand was an OP_REG, this is the register that it
4912 specified, otherwise it is ILLEGAL_REG. */
4913 unsigned int dest_regno;
4914
4915 /* The value of the last OP_INT operand. Only used for OP_MSB,
4916 where it gives the lsb position. */
4917 unsigned int last_op_int;
4918
60f20e8b 4919 /* If true, match routines should assume that no later instruction
2b0f3761 4920 alternative matches and should therefore be as accommodating as
60f20e8b
RS
4921 possible. Match routines should not report errors if something
4922 is only invalid for !LAX_MATCH. */
4923 bfd_boolean lax_match;
a1d78564 4924
a1d78564
RS
4925 /* True if a reference to the current AT register was seen. */
4926 bfd_boolean seen_at;
4927};
4928
1a00e612
RS
4929/* Record that the argument is out of range. */
4930
4931static void
4932match_out_of_range (struct mips_arg_info *arg)
4933{
4934 set_insn_error_i (arg->argnum, _("operand %d out of range"), arg->argnum);
4935}
4936
4937/* Record that the argument isn't constant but needs to be. */
4938
4939static void
4940match_not_constant (struct mips_arg_info *arg)
4941{
4942 set_insn_error_i (arg->argnum, _("operand %d must be constant"),
4943 arg->argnum);
4944}
4945
a92713e6
RS
4946/* Try to match an OT_CHAR token for character CH. Consume the token
4947 and return true on success, otherwise return false. */
a1d78564 4948
a92713e6
RS
4949static bfd_boolean
4950match_char (struct mips_arg_info *arg, char ch)
a1d78564 4951{
a92713e6
RS
4952 if (arg->token->type == OT_CHAR && arg->token->u.ch == ch)
4953 {
4954 ++arg->token;
4955 if (ch == ',')
4956 arg->argnum += 1;
4957 return TRUE;
4958 }
4959 return FALSE;
4960}
a1d78564 4961
a92713e6
RS
4962/* Try to get an expression from the next tokens in ARG. Consume the
4963 tokens and return true on success, storing the expression value in
4964 VALUE and relocation types in R. */
4965
4966static bfd_boolean
4967match_expression (struct mips_arg_info *arg, expressionS *value,
4968 bfd_reloc_code_real_type *r)
4969{
d436c1c2
RS
4970 /* If the next token is a '(' that was parsed as being part of a base
4971 expression, assume we have an elided offset. The later match will fail
4972 if this turns out to be wrong. */
4973 if (arg->token->type == OT_CHAR && arg->token->u.ch == '(')
a1d78564 4974 {
d436c1c2
RS
4975 value->X_op = O_constant;
4976 value->X_add_number = 0;
4977 r[0] = r[1] = r[2] = BFD_RELOC_UNUSED;
a92713e6
RS
4978 return TRUE;
4979 }
4980
d436c1c2
RS
4981 /* Reject register-based expressions such as "0+$2" and "(($2))".
4982 For plain registers the default error seems more appropriate. */
4983 if (arg->token->type == OT_INTEGER
4984 && arg->token->u.integer.value.X_op == O_register)
a92713e6 4985 {
d436c1c2
RS
4986 set_insn_error (arg->argnum, _("register value used as expression"));
4987 return FALSE;
a1d78564 4988 }
d436c1c2
RS
4989
4990 if (arg->token->type == OT_INTEGER)
a92713e6 4991 {
d436c1c2
RS
4992 *value = arg->token->u.integer.value;
4993 memcpy (r, arg->token->u.integer.relocs, 3 * sizeof (*r));
4994 ++arg->token;
4995 return TRUE;
a92713e6 4996 }
a92713e6 4997
d436c1c2
RS
4998 set_insn_error_i
4999 (arg->argnum, _("operand %d must be an immediate expression"),
5000 arg->argnum);
5001 return FALSE;
a92713e6
RS
5002}
5003
5004/* Try to get a constant expression from the next tokens in ARG. Consume
de194d85 5005 the tokens and return true on success, storing the constant value
a54d5f8b 5006 in *VALUE. */
a92713e6
RS
5007
5008static bfd_boolean
1a00e612 5009match_const_int (struct mips_arg_info *arg, offsetT *value)
a92713e6
RS
5010{
5011 expressionS ex;
5012 bfd_reloc_code_real_type r[3];
a1d78564 5013
a92713e6
RS
5014 if (!match_expression (arg, &ex, r))
5015 return FALSE;
5016
5017 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_constant)
a1d78564
RS
5018 *value = ex.X_add_number;
5019 else
5020 {
c96425c5
MR
5021 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_big)
5022 match_out_of_range (arg);
5023 else
5024 match_not_constant (arg);
1a00e612 5025 return FALSE;
a1d78564 5026 }
a92713e6 5027 return TRUE;
a1d78564
RS
5028}
5029
5030/* Return the RTYPE_* flags for a register operand of type TYPE that
5031 appears in instruction OPCODE. */
5032
5033static unsigned int
5034convert_reg_type (const struct mips_opcode *opcode,
5035 enum mips_reg_operand_type type)
5036{
5037 switch (type)
5038 {
5039 case OP_REG_GP:
5040 return RTYPE_NUM | RTYPE_GP;
5041
5042 case OP_REG_FP:
5043 /* Allow vector register names for MDMX if the instruction is a 64-bit
5044 FPR load, store or move (including moves to and from GPRs). */
5045 if ((mips_opts.ase & ASE_MDMX)
5046 && (opcode->pinfo & FP_D)
43885403 5047 && (opcode->pinfo & (INSN_COPROC_MOVE
a1d78564 5048 | INSN_COPROC_MEMORY_DELAY
43885403 5049 | INSN_LOAD_COPROC
67dc82bc 5050 | INSN_LOAD_MEMORY
a1d78564
RS
5051 | INSN_STORE_MEMORY)))
5052 return RTYPE_FPU | RTYPE_VEC;
5053 return RTYPE_FPU;
5054
5055 case OP_REG_CCC:
5056 if (opcode->pinfo & (FP_D | FP_S))
5057 return RTYPE_CCC | RTYPE_FCC;
5058 return RTYPE_CCC;
5059
5060 case OP_REG_VEC:
5061 if (opcode->membership & INSN_5400)
5062 return RTYPE_FPU;
5063 return RTYPE_FPU | RTYPE_VEC;
5064
5065 case OP_REG_ACC:
5066 return RTYPE_ACC;
5067
5068 case OP_REG_COPRO:
5069 if (opcode->name[strlen (opcode->name) - 1] == '0')
5070 return RTYPE_NUM | RTYPE_CP0;
5071 return RTYPE_NUM;
5072
5073 case OP_REG_HW:
5074 return RTYPE_NUM;
14daeee3
RS
5075
5076 case OP_REG_VI:
5077 return RTYPE_NUM | RTYPE_VI;
5078
5079 case OP_REG_VF:
5080 return RTYPE_NUM | RTYPE_VF;
5081
5082 case OP_REG_R5900_I:
5083 return RTYPE_R5900_I;
5084
5085 case OP_REG_R5900_Q:
5086 return RTYPE_R5900_Q;
5087
5088 case OP_REG_R5900_R:
5089 return RTYPE_R5900_R;
5090
5091 case OP_REG_R5900_ACC:
5092 return RTYPE_R5900_ACC;
56d438b1
CF
5093
5094 case OP_REG_MSA:
5095 return RTYPE_MSA;
5096
5097 case OP_REG_MSA_CTRL:
5098 return RTYPE_NUM;
a1d78564
RS
5099 }
5100 abort ();
5101}
5102
5103/* ARG is register REGNO, of type TYPE. Warn about any dubious registers. */
5104
5105static void
5106check_regno (struct mips_arg_info *arg,
5107 enum mips_reg_operand_type type, unsigned int regno)
5108{
5109 if (AT && type == OP_REG_GP && regno == AT)
5110 arg->seen_at = TRUE;
5111
5112 if (type == OP_REG_FP
5113 && (regno & 1) != 0
a1d78564 5114 && !mips_oddfpreg_ok (arg->insn->insn_mo, arg->opnum))
351cdf24
MF
5115 {
5116 /* This was a warning prior to introducing O32 FPXX and FP64 support
5117 so maintain a warning for FP32 but raise an error for the new
5118 cases. */
5119 if (FPR_SIZE == 32)
5120 as_warn (_("float register should be even, was %d"), regno);
5121 else
5122 as_bad (_("float register should be even, was %d"), regno);
5123 }
a1d78564
RS
5124
5125 if (type == OP_REG_CCC)
5126 {
5127 const char *name;
5128 size_t length;
5129
5130 name = arg->insn->insn_mo->name;
5131 length = strlen (name);
5132 if ((regno & 1) != 0
5133 && ((length >= 3 && strcmp (name + length - 3, ".ps") == 0)
5134 || (length >= 5 && strncmp (name + length - 5, "any2", 4) == 0)))
1661c76c 5135 as_warn (_("condition code register should be even for %s, was %d"),
a1d78564
RS
5136 name, regno);
5137
5138 if ((regno & 3) != 0
5139 && (length >= 5 && strncmp (name + length - 5, "any4", 4) == 0))
1661c76c 5140 as_warn (_("condition code register should be 0 or 4 for %s, was %d"),
a1d78564
RS
5141 name, regno);
5142 }
5143}
5144
a92713e6
RS
5145/* ARG is a register with symbol value SYMVAL. Try to interpret it as
5146 a register of type TYPE. Return true on success, storing the register
5147 number in *REGNO and warning about any dubious uses. */
5148
5149static bfd_boolean
5150match_regno (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5151 unsigned int symval, unsigned int *regno)
5152{
5153 if (type == OP_REG_VEC)
5154 symval = mips_prefer_vec_regno (symval);
5155 if (!(symval & convert_reg_type (arg->insn->insn_mo, type)))
5156 return FALSE;
5157
5158 *regno = symval & RNUM_MASK;
5159 check_regno (arg, type, *regno);
5160 return TRUE;
5161}
5162
5163/* Try to interpret the next token in ARG as a register of type TYPE.
5164 Consume the token and return true on success, storing the register
5165 number in *REGNO. Return false on failure. */
5166
5167static bfd_boolean
5168match_reg (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5169 unsigned int *regno)
5170{
5171 if (arg->token->type == OT_REG
5172 && match_regno (arg, type, arg->token->u.regno, regno))
5173 {
5174 ++arg->token;
5175 return TRUE;
5176 }
5177 return FALSE;
5178}
5179
5180/* Try to interpret the next token in ARG as a range of registers of type TYPE.
5181 Consume the token and return true on success, storing the register numbers
5182 in *REGNO1 and *REGNO2. Return false on failure. */
5183
5184static bfd_boolean
5185match_reg_range (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5186 unsigned int *regno1, unsigned int *regno2)
5187{
5188 if (match_reg (arg, type, regno1))
5189 {
5190 *regno2 = *regno1;
5191 return TRUE;
5192 }
5193 if (arg->token->type == OT_REG_RANGE
5194 && match_regno (arg, type, arg->token->u.reg_range.regno1, regno1)
5195 && match_regno (arg, type, arg->token->u.reg_range.regno2, regno2)
5196 && *regno1 <= *regno2)
5197 {
5198 ++arg->token;
5199 return TRUE;
5200 }
5201 return FALSE;
5202}
5203
a1d78564
RS
5204/* OP_INT matcher. */
5205
a92713e6 5206static bfd_boolean
a1d78564 5207match_int_operand (struct mips_arg_info *arg,
a92713e6 5208 const struct mips_operand *operand_base)
a1d78564
RS
5209{
5210 const struct mips_int_operand *operand;
3ccad066 5211 unsigned int uval;
a1d78564
RS
5212 int min_val, max_val, factor;
5213 offsetT sval;
a1d78564
RS
5214
5215 operand = (const struct mips_int_operand *) operand_base;
5216 factor = 1 << operand->shift;
3ccad066
RS
5217 min_val = mips_int_operand_min (operand);
5218 max_val = mips_int_operand_max (operand);
a1d78564 5219
d436c1c2
RS
5220 if (operand_base->lsb == 0
5221 && operand_base->size == 16
5222 && operand->shift == 0
5223 && operand->bias == 0
5224 && (operand->max_val == 32767 || operand->max_val == 65535))
a1d78564
RS
5225 {
5226 /* The operand can be relocated. */
a92713e6
RS
5227 if (!match_expression (arg, &offset_expr, offset_reloc))
5228 return FALSE;
5229
c96425c5
MR
5230 if (offset_expr.X_op == O_big)
5231 {
5232 match_out_of_range (arg);
5233 return FALSE;
5234 }
5235
a92713e6 5236 if (offset_reloc[0] != BFD_RELOC_UNUSED)
33eaf5de 5237 /* Relocation operators were used. Accept the argument and
a1d78564
RS
5238 leave the relocation value in offset_expr and offset_relocs
5239 for the caller to process. */
a92713e6
RS
5240 return TRUE;
5241
5242 if (offset_expr.X_op != O_constant)
a1d78564 5243 {
60f20e8b
RS
5244 /* Accept non-constant operands if no later alternative matches,
5245 leaving it for the caller to process. */
5246 if (!arg->lax_match)
602b88e3
MR
5247 {
5248 match_not_constant (arg);
5249 return FALSE;
5250 }
a92713e6
RS
5251 offset_reloc[0] = BFD_RELOC_LO16;
5252 return TRUE;
a1d78564 5253 }
a92713e6 5254
a1d78564
RS
5255 /* Clear the global state; we're going to install the operand
5256 ourselves. */
a92713e6 5257 sval = offset_expr.X_add_number;
a1d78564 5258 offset_expr.X_op = O_absent;
60f20e8b
RS
5259
5260 /* For compatibility with older assemblers, we accept
5261 0x8000-0xffff as signed 16-bit numbers when only
5262 signed numbers are allowed. */
5263 if (sval > max_val)
5264 {
5265 max_val = ((1 << operand_base->size) - 1) << operand->shift;
5266 if (!arg->lax_match && sval <= max_val)
20c59b84
MR
5267 {
5268 match_out_of_range (arg);
5269 return FALSE;
5270 }
60f20e8b 5271 }
a1d78564
RS
5272 }
5273 else
5274 {
1a00e612 5275 if (!match_const_int (arg, &sval))
a92713e6 5276 return FALSE;
a1d78564
RS
5277 }
5278
5279 arg->last_op_int = sval;
5280
1a00e612 5281 if (sval < min_val || sval > max_val || sval % factor)
a1d78564 5282 {
1a00e612
RS
5283 match_out_of_range (arg);
5284 return FALSE;
a1d78564
RS
5285 }
5286
5287 uval = (unsigned int) sval >> operand->shift;
5288 uval -= operand->bias;
5289
5290 /* Handle -mfix-cn63xxp1. */
5291 if (arg->opnum == 1
5292 && mips_fix_cn63xxp1
5293 && !mips_opts.micromips
5294 && strcmp ("pref", arg->insn->insn_mo->name) == 0)
5295 switch (uval)
5296 {
5297 case 5:
5298 case 25:
5299 case 26:
5300 case 27:
5301 case 28:
5302 case 29:
5303 case 30:
5304 case 31:
5305 /* These are ok. */
5306 break;
5307
5308 default:
5309 /* The rest must be changed to 28. */
5310 uval = 28;
5311 break;
5312 }
5313
5314 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5315 return TRUE;
a1d78564
RS
5316}
5317
5318/* OP_MAPPED_INT matcher. */
5319
a92713e6 5320static bfd_boolean
a1d78564 5321match_mapped_int_operand (struct mips_arg_info *arg,
a92713e6 5322 const struct mips_operand *operand_base)
a1d78564
RS
5323{
5324 const struct mips_mapped_int_operand *operand;
5325 unsigned int uval, num_vals;
5326 offsetT sval;
5327
5328 operand = (const struct mips_mapped_int_operand *) operand_base;
1a00e612 5329 if (!match_const_int (arg, &sval))
a92713e6 5330 return FALSE;
a1d78564
RS
5331
5332 num_vals = 1 << operand_base->size;
5333 for (uval = 0; uval < num_vals; uval++)
5334 if (operand->int_map[uval] == sval)
5335 break;
5336 if (uval == num_vals)
1a00e612
RS
5337 {
5338 match_out_of_range (arg);
5339 return FALSE;
5340 }
a1d78564
RS
5341
5342 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5343 return TRUE;
a1d78564
RS
5344}
5345
5346/* OP_MSB matcher. */
5347
a92713e6 5348static bfd_boolean
a1d78564 5349match_msb_operand (struct mips_arg_info *arg,
a92713e6 5350 const struct mips_operand *operand_base)
a1d78564
RS
5351{
5352 const struct mips_msb_operand *operand;
5353 int min_val, max_val, max_high;
5354 offsetT size, sval, high;
5355
5356 operand = (const struct mips_msb_operand *) operand_base;
5357 min_val = operand->bias;
5358 max_val = min_val + (1 << operand_base->size) - 1;
5359 max_high = operand->opsize;
5360
1a00e612 5361 if (!match_const_int (arg, &size))
a92713e6 5362 return FALSE;
a1d78564
RS
5363
5364 high = size + arg->last_op_int;
5365 sval = operand->add_lsb ? high : size;
5366
5367 if (size < 0 || high > max_high || sval < min_val || sval > max_val)
5368 {
1a00e612
RS
5369 match_out_of_range (arg);
5370 return FALSE;
a1d78564
RS
5371 }
5372 insn_insert_operand (arg->insn, operand_base, sval - min_val);
a92713e6 5373 return TRUE;
a1d78564
RS
5374}
5375
5376/* OP_REG matcher. */
5377
a92713e6 5378static bfd_boolean
a1d78564 5379match_reg_operand (struct mips_arg_info *arg,
a92713e6 5380 const struct mips_operand *operand_base)
a1d78564
RS
5381{
5382 const struct mips_reg_operand *operand;
a92713e6 5383 unsigned int regno, uval, num_vals;
a1d78564
RS
5384
5385 operand = (const struct mips_reg_operand *) operand_base;
a92713e6
RS
5386 if (!match_reg (arg, operand->reg_type, &regno))
5387 return FALSE;
a1d78564
RS
5388
5389 if (operand->reg_map)
5390 {
5391 num_vals = 1 << operand->root.size;
5392 for (uval = 0; uval < num_vals; uval++)
5393 if (operand->reg_map[uval] == regno)
5394 break;
5395 if (num_vals == uval)
a92713e6 5396 return FALSE;
a1d78564
RS
5397 }
5398 else
5399 uval = regno;
5400
a1d78564
RS
5401 arg->last_regno = regno;
5402 if (arg->opnum == 1)
5403 arg->dest_regno = regno;
5404 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5405 return TRUE;
a1d78564
RS
5406}
5407
5408/* OP_REG_PAIR matcher. */
5409
a92713e6 5410static bfd_boolean
a1d78564 5411match_reg_pair_operand (struct mips_arg_info *arg,
a92713e6 5412 const struct mips_operand *operand_base)
a1d78564
RS
5413{
5414 const struct mips_reg_pair_operand *operand;
a92713e6 5415 unsigned int regno1, regno2, uval, num_vals;
a1d78564
RS
5416
5417 operand = (const struct mips_reg_pair_operand *) operand_base;
a92713e6
RS
5418 if (!match_reg (arg, operand->reg_type, &regno1)
5419 || !match_char (arg, ',')
5420 || !match_reg (arg, operand->reg_type, &regno2))
5421 return FALSE;
a1d78564
RS
5422
5423 num_vals = 1 << operand_base->size;
5424 for (uval = 0; uval < num_vals; uval++)
5425 if (operand->reg1_map[uval] == regno1 && operand->reg2_map[uval] == regno2)
5426 break;
5427 if (uval == num_vals)
a92713e6 5428 return FALSE;
a1d78564 5429
a1d78564 5430 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5431 return TRUE;
a1d78564
RS
5432}
5433
5434/* OP_PCREL matcher. The caller chooses the relocation type. */
5435
a92713e6
RS
5436static bfd_boolean
5437match_pcrel_operand (struct mips_arg_info *arg)
a1d78564 5438{
a92713e6
RS
5439 bfd_reloc_code_real_type r[3];
5440
5441 return match_expression (arg, &offset_expr, r) && r[0] == BFD_RELOC_UNUSED;
a1d78564
RS
5442}
5443
5444/* OP_PERF_REG matcher. */
5445
a92713e6 5446static bfd_boolean
a1d78564 5447match_perf_reg_operand (struct mips_arg_info *arg,
a92713e6 5448 const struct mips_operand *operand)
a1d78564
RS
5449{
5450 offsetT sval;
5451
1a00e612 5452 if (!match_const_int (arg, &sval))
a92713e6 5453 return FALSE;
a1d78564
RS
5454
5455 if (sval != 0
5456 && (sval != 1
5457 || (mips_opts.arch == CPU_R5900
5458 && (strcmp (arg->insn->insn_mo->name, "mfps") == 0
5459 || strcmp (arg->insn->insn_mo->name, "mtps") == 0))))
5460 {
1a00e612
RS
5461 set_insn_error (arg->argnum, _("invalid performance register"));
5462 return FALSE;
a1d78564
RS
5463 }
5464
5465 insn_insert_operand (arg->insn, operand, sval);
a92713e6 5466 return TRUE;
a1d78564
RS
5467}
5468
5469/* OP_ADDIUSP matcher. */
5470
a92713e6 5471static bfd_boolean
a1d78564 5472match_addiusp_operand (struct mips_arg_info *arg,
a92713e6 5473 const struct mips_operand *operand)
a1d78564
RS
5474{
5475 offsetT sval;
5476 unsigned int uval;
5477
1a00e612 5478 if (!match_const_int (arg, &sval))
a92713e6 5479 return FALSE;
a1d78564
RS
5480
5481 if (sval % 4)
1a00e612
RS
5482 {
5483 match_out_of_range (arg);
5484 return FALSE;
5485 }
a1d78564
RS
5486
5487 sval /= 4;
5488 if (!(sval >= -258 && sval <= 257) || (sval >= -2 && sval <= 1))
1a00e612
RS
5489 {
5490 match_out_of_range (arg);
5491 return FALSE;
5492 }
a1d78564
RS
5493
5494 uval = (unsigned int) sval;
5495 uval = ((uval >> 1) & ~0xff) | (uval & 0xff);
5496 insn_insert_operand (arg->insn, operand, uval);
a92713e6 5497 return TRUE;
a1d78564
RS
5498}
5499
5500/* OP_CLO_CLZ_DEST matcher. */
5501
a92713e6 5502static bfd_boolean
a1d78564 5503match_clo_clz_dest_operand (struct mips_arg_info *arg,
a92713e6 5504 const struct mips_operand *operand)
a1d78564
RS
5505{
5506 unsigned int regno;
5507
a92713e6
RS
5508 if (!match_reg (arg, OP_REG_GP, &regno))
5509 return FALSE;
a1d78564 5510
a1d78564 5511 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
a92713e6 5512 return TRUE;
a1d78564
RS
5513}
5514
7361da2c
AB
5515/* OP_CHECK_PREV matcher. */
5516
5517static bfd_boolean
5518match_check_prev_operand (struct mips_arg_info *arg,
5519 const struct mips_operand *operand_base)
5520{
5521 const struct mips_check_prev_operand *operand;
5522 unsigned int regno;
5523
5524 operand = (const struct mips_check_prev_operand *) operand_base;
5525
5526 if (!match_reg (arg, OP_REG_GP, &regno))
5527 return FALSE;
5528
5529 if (!operand->zero_ok && regno == 0)
5530 return FALSE;
5531
5532 if ((operand->less_than_ok && regno < arg->last_regno)
5533 || (operand->greater_than_ok && regno > arg->last_regno)
5534 || (operand->equal_ok && regno == arg->last_regno))
5535 {
5536 arg->last_regno = regno;
5537 insn_insert_operand (arg->insn, operand_base, regno);
5538 return TRUE;
5539 }
5540
5541 return FALSE;
5542}
5543
5544/* OP_SAME_RS_RT matcher. */
5545
5546static bfd_boolean
5547match_same_rs_rt_operand (struct mips_arg_info *arg,
5548 const struct mips_operand *operand)
5549{
5550 unsigned int regno;
5551
5552 if (!match_reg (arg, OP_REG_GP, &regno))
5553 return FALSE;
5554
5555 if (regno == 0)
5556 {
5557 set_insn_error (arg->argnum, _("the source register must not be $0"));
5558 return FALSE;
5559 }
5560
5561 arg->last_regno = regno;
5562
5563 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5564 return TRUE;
5565}
5566
a1d78564
RS
5567/* OP_LWM_SWM_LIST matcher. */
5568
a92713e6 5569static bfd_boolean
a1d78564 5570match_lwm_swm_list_operand (struct mips_arg_info *arg,
a92713e6 5571 const struct mips_operand *operand)
a1d78564 5572{
a92713e6
RS
5573 unsigned int reglist, sregs, ra, regno1, regno2;
5574 struct mips_arg_info reset;
a1d78564 5575
a92713e6
RS
5576 reglist = 0;
5577 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5578 return FALSE;
5579 do
5580 {
5581 if (regno2 == FP && regno1 >= S0 && regno1 <= S7)
5582 {
5583 reglist |= 1 << FP;
5584 regno2 = S7;
5585 }
5586 reglist |= ((1U << regno2 << 1) - 1) & -(1U << regno1);
5587 reset = *arg;
5588 }
5589 while (match_char (arg, ',')
5590 && match_reg_range (arg, OP_REG_GP, &regno1, &regno2));
5591 *arg = reset;
a1d78564
RS
5592
5593 if (operand->size == 2)
5594 {
5595 /* The list must include both ra and s0-sN, for 0 <= N <= 3. E.g.:
5596
5597 s0, ra
5598 s0, s1, ra, s2, s3
5599 s0-s2, ra
5600
5601 and any permutations of these. */
5602 if ((reglist & 0xfff1ffff) != 0x80010000)
a92713e6 5603 return FALSE;
a1d78564
RS
5604
5605 sregs = (reglist >> 17) & 7;
5606 ra = 0;
5607 }
5608 else
5609 {
5610 /* The list must include at least one of ra and s0-sN,
5611 for 0 <= N <= 8. (Note that there is a gap between s7 and s8,
5612 which are $23 and $30 respectively.) E.g.:
5613
5614 ra
5615 s0
5616 ra, s0, s1, s2
5617 s0-s8
5618 s0-s5, ra
5619
5620 and any permutations of these. */
5621 if ((reglist & 0x3f00ffff) != 0)
a92713e6 5622 return FALSE;
a1d78564
RS
5623
5624 ra = (reglist >> 27) & 0x10;
5625 sregs = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
5626 }
5627 sregs += 1;
5628 if ((sregs & -sregs) != sregs)
a92713e6 5629 return FALSE;
a1d78564
RS
5630
5631 insn_insert_operand (arg->insn, operand, (ffs (sregs) - 1) | ra);
a92713e6 5632 return TRUE;
a1d78564
RS
5633}
5634
364215c8
RS
5635/* OP_ENTRY_EXIT_LIST matcher. */
5636
a92713e6 5637static unsigned int
364215c8 5638match_entry_exit_operand (struct mips_arg_info *arg,
a92713e6 5639 const struct mips_operand *operand)
364215c8
RS
5640{
5641 unsigned int mask;
5642 bfd_boolean is_exit;
5643
5644 /* The format is the same for both ENTRY and EXIT, but the constraints
5645 are different. */
5646 is_exit = strcmp (arg->insn->insn_mo->name, "exit") == 0;
5647 mask = (is_exit ? 7 << 3 : 0);
a92713e6 5648 do
364215c8
RS
5649 {
5650 unsigned int regno1, regno2;
5651 bfd_boolean is_freg;
5652
a92713e6 5653 if (match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
364215c8 5654 is_freg = FALSE;
a92713e6 5655 else if (match_reg_range (arg, OP_REG_FP, &regno1, &regno2))
364215c8
RS
5656 is_freg = TRUE;
5657 else
a92713e6 5658 return FALSE;
364215c8
RS
5659
5660 if (is_exit && is_freg && regno1 == 0 && regno2 < 2)
5661 {
5662 mask &= ~(7 << 3);
5663 mask |= (5 + regno2) << 3;
5664 }
5665 else if (!is_exit && regno1 == 4 && regno2 >= 4 && regno2 <= 7)
5666 mask |= (regno2 - 3) << 3;
5667 else if (regno1 == 16 && regno2 >= 16 && regno2 <= 17)
5668 mask |= (regno2 - 15) << 1;
5669 else if (regno1 == RA && regno2 == RA)
5670 mask |= 1;
5671 else
a92713e6 5672 return FALSE;
364215c8 5673 }
a92713e6
RS
5674 while (match_char (arg, ','));
5675
364215c8 5676 insn_insert_operand (arg->insn, operand, mask);
a92713e6 5677 return TRUE;
364215c8
RS
5678}
5679
38bf472a
MR
5680/* Encode regular MIPS SAVE/RESTORE instruction operands according to
5681 the argument register mask AMASK, the number of static registers
5682 saved NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5683 respectively, and the frame size FRAME_SIZE. */
5684
5685static unsigned int
5686mips_encode_save_restore (unsigned int amask, unsigned int nsreg,
5687 unsigned int ra, unsigned int s0, unsigned int s1,
5688 unsigned int frame_size)
5689{
5690 return ((nsreg << 23) | ((frame_size & 0xf0) << 15) | (amask << 15)
5691 | (ra << 12) | (s0 << 11) | (s1 << 10) | ((frame_size & 0xf) << 6));
5692}
5693
5694/* Encode MIPS16 SAVE/RESTORE instruction operands according to the
5695 argument register mask AMASK, the number of static registers saved
5696 NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5697 respectively, and the frame size FRAME_SIZE. */
5698
5699static unsigned int
5700mips16_encode_save_restore (unsigned int amask, unsigned int nsreg,
5701 unsigned int ra, unsigned int s0, unsigned int s1,
5702 unsigned int frame_size)
5703{
5704 unsigned int args;
5705
5706 args = (ra << 6) | (s0 << 5) | (s1 << 4) | (frame_size & 0xf);
5707 if (nsreg || amask || frame_size == 0 || frame_size > 16)
5708 args |= (MIPS16_EXTEND | (nsreg << 24) | (amask << 16)
5709 | ((frame_size & 0xf0) << 16));
5710 return args;
5711}
5712
364215c8
RS
5713/* OP_SAVE_RESTORE_LIST matcher. */
5714
a92713e6
RS
5715static bfd_boolean
5716match_save_restore_list_operand (struct mips_arg_info *arg)
364215c8
RS
5717{
5718 unsigned int opcode, args, statics, sregs;
5719 unsigned int num_frame_sizes, num_args, num_statics, num_sregs;
38bf472a 5720 unsigned int arg_mask, ra, s0, s1;
364215c8 5721 offsetT frame_size;
364215c8 5722
364215c8
RS
5723 opcode = arg->insn->insn_opcode;
5724 frame_size = 0;
5725 num_frame_sizes = 0;
5726 args = 0;
5727 statics = 0;
5728 sregs = 0;
38bf472a
MR
5729 ra = 0;
5730 s0 = 0;
5731 s1 = 0;
a92713e6 5732 do
364215c8
RS
5733 {
5734 unsigned int regno1, regno2;
5735
a92713e6 5736 if (arg->token->type == OT_INTEGER)
364215c8
RS
5737 {
5738 /* Handle the frame size. */
1a00e612 5739 if (!match_const_int (arg, &frame_size))
a92713e6 5740 return FALSE;
364215c8 5741 num_frame_sizes += 1;
364215c8
RS
5742 }
5743 else
5744 {
a92713e6
RS
5745 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5746 return FALSE;
364215c8
RS
5747
5748 while (regno1 <= regno2)
5749 {
5750 if (regno1 >= 4 && regno1 <= 7)
5751 {
5752 if (num_frame_sizes == 0)
5753 /* args $a0-$a3 */
5754 args |= 1 << (regno1 - 4);
5755 else
5756 /* statics $a0-$a3 */
5757 statics |= 1 << (regno1 - 4);
5758 }
5759 else if (regno1 >= 16 && regno1 <= 23)
5760 /* $s0-$s7 */
5761 sregs |= 1 << (regno1 - 16);
5762 else if (regno1 == 30)
5763 /* $s8 */
5764 sregs |= 1 << 8;
5765 else if (regno1 == 31)
5766 /* Add $ra to insn. */
38bf472a 5767 ra = 1;
364215c8 5768 else
a92713e6 5769 return FALSE;
364215c8
RS
5770 regno1 += 1;
5771 if (regno1 == 24)
5772 regno1 = 30;
5773 }
5774 }
364215c8 5775 }
a92713e6 5776 while (match_char (arg, ','));
364215c8
RS
5777
5778 /* Encode args/statics combination. */
5779 if (args & statics)
a92713e6 5780 return FALSE;
364215c8
RS
5781 else if (args == 0xf)
5782 /* All $a0-$a3 are args. */
38bf472a 5783 arg_mask = MIPS_SVRS_ALL_ARGS;
364215c8
RS
5784 else if (statics == 0xf)
5785 /* All $a0-$a3 are statics. */
38bf472a 5786 arg_mask = MIPS_SVRS_ALL_STATICS;
364215c8
RS
5787 else
5788 {
5789 /* Count arg registers. */
5790 num_args = 0;
5791 while (args & 0x1)
5792 {
5793 args >>= 1;
5794 num_args += 1;
5795 }
5796 if (args != 0)
a92713e6 5797 return FALSE;
364215c8
RS
5798
5799 /* Count static registers. */
5800 num_statics = 0;
5801 while (statics & 0x8)
5802 {
5803 statics = (statics << 1) & 0xf;
5804 num_statics += 1;
5805 }
5806 if (statics != 0)
a92713e6 5807 return FALSE;
364215c8
RS
5808
5809 /* Encode args/statics. */
38bf472a 5810 arg_mask = (num_args << 2) | num_statics;
364215c8
RS
5811 }
5812
5813 /* Encode $s0/$s1. */
5814 if (sregs & (1 << 0)) /* $s0 */
38bf472a 5815 s0 = 1;
364215c8 5816 if (sregs & (1 << 1)) /* $s1 */
38bf472a 5817 s1 = 1;
364215c8
RS
5818 sregs >>= 2;
5819
5820 /* Encode $s2-$s8. */
5821 num_sregs = 0;
5822 while (sregs & 1)
5823 {
5824 sregs >>= 1;
5825 num_sregs += 1;
5826 }
5827 if (sregs != 0)
a92713e6 5828 return FALSE;
364215c8
RS
5829
5830 /* Encode frame size. */
5831 if (num_frame_sizes == 0)
1a00e612
RS
5832 {
5833 set_insn_error (arg->argnum, _("missing frame size"));
5834 return FALSE;
5835 }
5836 if (num_frame_sizes > 1)
5837 {
5838 set_insn_error (arg->argnum, _("frame size specified twice"));
5839 return FALSE;
5840 }
5841 if ((frame_size & 7) != 0 || frame_size < 0 || frame_size > 0xff * 8)
5842 {
5843 set_insn_error (arg->argnum, _("invalid frame size"));
5844 return FALSE;
5845 }
38bf472a 5846 frame_size /= 8;
364215c8 5847
364215c8 5848 /* Finally build the instruction. */
38bf472a
MR
5849 if (mips_opts.mips16)
5850 opcode |= mips16_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5851 frame_size);
5852 else if (!mips_opts.micromips)
5853 opcode |= mips_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5854 frame_size);
5855 else
5856 abort ();
5857
364215c8 5858 arg->insn->insn_opcode = opcode;
a92713e6 5859 return TRUE;
364215c8
RS
5860}
5861
a1d78564
RS
5862/* OP_MDMX_IMM_REG matcher. */
5863
a92713e6 5864static bfd_boolean
a1d78564 5865match_mdmx_imm_reg_operand (struct mips_arg_info *arg,
a92713e6 5866 const struct mips_operand *operand)
a1d78564 5867{
a92713e6 5868 unsigned int regno, uval;
a1d78564
RS
5869 bfd_boolean is_qh;
5870 const struct mips_opcode *opcode;
5871
5872 /* The mips_opcode records whether this is an octobyte or quadhalf
5873 instruction. Start out with that bit in place. */
5874 opcode = arg->insn->insn_mo;
5875 uval = mips_extract_operand (operand, opcode->match);
5876 is_qh = (uval != 0);
5877
56d438b1 5878 if (arg->token->type == OT_REG)
a1d78564
RS
5879 {
5880 if ((opcode->membership & INSN_5400)
5881 && strcmp (opcode->name, "rzu.ob") == 0)
5882 {
1a00e612
RS
5883 set_insn_error_i (arg->argnum, _("operand %d must be an immediate"),
5884 arg->argnum);
5885 return FALSE;
a1d78564
RS
5886 }
5887
56d438b1
CF
5888 if (!match_regno (arg, OP_REG_VEC, arg->token->u.regno, &regno))
5889 return FALSE;
5890 ++arg->token;
5891
a1d78564
RS
5892 /* Check whether this is a vector register or a broadcast of
5893 a single element. */
56d438b1 5894 if (arg->token->type == OT_INTEGER_INDEX)
a1d78564 5895 {
56d438b1 5896 if (arg->token->u.index > (is_qh ? 3 : 7))
a1d78564 5897 {
1a00e612
RS
5898 set_insn_error (arg->argnum, _("invalid element selector"));
5899 return FALSE;
a1d78564 5900 }
56d438b1
CF
5901 uval |= arg->token->u.index << (is_qh ? 2 : 1) << 5;
5902 ++arg->token;
a1d78564
RS
5903 }
5904 else
5905 {
5906 /* A full vector. */
5907 if ((opcode->membership & INSN_5400)
5908 && (strcmp (opcode->name, "sll.ob") == 0
5909 || strcmp (opcode->name, "srl.ob") == 0))
5910 {
1a00e612
RS
5911 set_insn_error_i (arg->argnum, _("operand %d must be scalar"),
5912 arg->argnum);
5913 return FALSE;
a1d78564
RS
5914 }
5915
5916 if (is_qh)
5917 uval |= MDMX_FMTSEL_VEC_QH << 5;
5918 else
5919 uval |= MDMX_FMTSEL_VEC_OB << 5;
5920 }
a1d78564
RS
5921 uval |= regno;
5922 }
5923 else
5924 {
5925 offsetT sval;
5926
1a00e612 5927 if (!match_const_int (arg, &sval))
a92713e6 5928 return FALSE;
a1d78564
RS
5929 if (sval < 0 || sval > 31)
5930 {
1a00e612
RS
5931 match_out_of_range (arg);
5932 return FALSE;
a1d78564
RS
5933 }
5934 uval |= (sval & 31);
5935 if (is_qh)
5936 uval |= MDMX_FMTSEL_IMM_QH << 5;
5937 else
5938 uval |= MDMX_FMTSEL_IMM_OB << 5;
5939 }
5940 insn_insert_operand (arg->insn, operand, uval);
a92713e6 5941 return TRUE;
a1d78564
RS
5942}
5943
56d438b1
CF
5944/* OP_IMM_INDEX matcher. */
5945
5946static bfd_boolean
5947match_imm_index_operand (struct mips_arg_info *arg,
5948 const struct mips_operand *operand)
5949{
5950 unsigned int max_val;
5951
5952 if (arg->token->type != OT_INTEGER_INDEX)
5953 return FALSE;
5954
5955 max_val = (1 << operand->size) - 1;
5956 if (arg->token->u.index > max_val)
5957 {
5958 match_out_of_range (arg);
5959 return FALSE;
5960 }
5961 insn_insert_operand (arg->insn, operand, arg->token->u.index);
5962 ++arg->token;
5963 return TRUE;
5964}
5965
5966/* OP_REG_INDEX matcher. */
5967
5968static bfd_boolean
5969match_reg_index_operand (struct mips_arg_info *arg,
5970 const struct mips_operand *operand)
5971{
5972 unsigned int regno;
5973
5974 if (arg->token->type != OT_REG_INDEX)
5975 return FALSE;
5976
5977 if (!match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno))
5978 return FALSE;
5979
5980 insn_insert_operand (arg->insn, operand, regno);
5981 ++arg->token;
5982 return TRUE;
5983}
5984
a1d78564
RS
5985/* OP_PC matcher. */
5986
a92713e6
RS
5987static bfd_boolean
5988match_pc_operand (struct mips_arg_info *arg)
a1d78564 5989{
a92713e6
RS
5990 if (arg->token->type == OT_REG && (arg->token->u.regno & RTYPE_PC))
5991 {
5992 ++arg->token;
5993 return TRUE;
5994 }
5995 return FALSE;
a1d78564
RS
5996}
5997
25499ac7
MR
5998/* OP_REG28 matcher. */
5999
6000static bfd_boolean
6001match_reg28_operand (struct mips_arg_info *arg)
6002{
6003 unsigned int regno;
6004
6005 if (arg->token->type == OT_REG
6006 && match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno)
6007 && regno == GP)
6008 {
6009 ++arg->token;
6010 return TRUE;
6011 }
6012 return FALSE;
6013}
6014
7361da2c
AB
6015/* OP_NON_ZERO_REG matcher. */
6016
6017static bfd_boolean
6018match_non_zero_reg_operand (struct mips_arg_info *arg,
6019 const struct mips_operand *operand)
6020{
6021 unsigned int regno;
6022
6023 if (!match_reg (arg, OP_REG_GP, &regno))
6024 return FALSE;
6025
6026 if (regno == 0)
85bec12d
MF
6027 {
6028 set_insn_error (arg->argnum, _("the source register must not be $0"));
6029 return FALSE;
6030 }
7361da2c
AB
6031
6032 arg->last_regno = regno;
6033 insn_insert_operand (arg->insn, operand, regno);
6034 return TRUE;
6035}
6036
a1d78564
RS
6037/* OP_REPEAT_DEST_REG and OP_REPEAT_PREV_REG matcher. OTHER_REGNO is the
6038 register that we need to match. */
6039
a92713e6
RS
6040static bfd_boolean
6041match_tied_reg_operand (struct mips_arg_info *arg, unsigned int other_regno)
a1d78564
RS
6042{
6043 unsigned int regno;
6044
a92713e6 6045 return match_reg (arg, OP_REG_GP, &regno) && regno == other_regno;
a1d78564
RS
6046}
6047
33f46696
MR
6048/* Try to match a floating-point constant from ARG for LI.S or LI.D.
6049 LENGTH is the length of the value in bytes (4 for float, 8 for double)
6050 and USING_GPRS says whether the destination is a GPR rather than an FPR.
89565f1b
RS
6051
6052 Return the constant in IMM and OFFSET as follows:
6053
6054 - If the constant should be loaded via memory, set IMM to O_absent and
6055 OFFSET to the memory address.
6056
6057 - Otherwise, if the constant should be loaded into two 32-bit registers,
6058 set IMM to the O_constant to load into the high register and OFFSET
6059 to the corresponding value for the low register.
6060
6061 - Otherwise, set IMM to the full O_constant and set OFFSET to O_absent.
6062
6063 These constants only appear as the last operand in an instruction,
6064 and every instruction that accepts them in any variant accepts them
6065 in all variants. This means we don't have to worry about backing out
6066 any changes if the instruction does not match. We just match
6067 unconditionally and report an error if the constant is invalid. */
6068
a92713e6
RS
6069static bfd_boolean
6070match_float_constant (struct mips_arg_info *arg, expressionS *imm,
6071 expressionS *offset, int length, bfd_boolean using_gprs)
89565f1b 6072{
a92713e6 6073 char *p;
89565f1b
RS
6074 segT seg, new_seg;
6075 subsegT subseg;
6076 const char *newname;
a92713e6 6077 unsigned char *data;
89565f1b
RS
6078
6079 /* Where the constant is placed is based on how the MIPS assembler
6080 does things:
6081
6082 length == 4 && using_gprs -- immediate value only
6083 length == 8 && using_gprs -- .rdata or immediate value
6084 length == 4 && !using_gprs -- .lit4 or immediate value
6085 length == 8 && !using_gprs -- .lit8 or immediate value
6086
6087 The .lit4 and .lit8 sections are only used if permitted by the
6088 -G argument. */
a92713e6 6089 if (arg->token->type != OT_FLOAT)
1a00e612
RS
6090 {
6091 set_insn_error (arg->argnum, _("floating-point expression required"));
6092 return FALSE;
6093 }
a92713e6
RS
6094
6095 gas_assert (arg->token->u.flt.length == length);
6096 data = arg->token->u.flt.data;
6097 ++arg->token;
89565f1b
RS
6098
6099 /* Handle 32-bit constants for which an immediate value is best. */
6100 if (length == 4
6101 && (using_gprs
6102 || g_switch_value < 4
6103 || (data[0] == 0 && data[1] == 0)
6104 || (data[2] == 0 && data[3] == 0)))
6105 {
6106 imm->X_op = O_constant;
6107 if (!target_big_endian)
6108 imm->X_add_number = bfd_getl32 (data);
6109 else
6110 imm->X_add_number = bfd_getb32 (data);
6111 offset->X_op = O_absent;
a92713e6 6112 return TRUE;
89565f1b
RS
6113 }
6114
6115 /* Handle 64-bit constants for which an immediate value is best. */
6116 if (length == 8
6117 && !mips_disable_float_construction
351cdf24
MF
6118 /* Constants can only be constructed in GPRs and copied to FPRs if the
6119 GPRs are at least as wide as the FPRs or MTHC1 is available.
6120 Unlike most tests for 32-bit floating-point registers this check
6121 specifically looks for GPR_SIZE == 32 as the FPXX ABI does not
6122 permit 64-bit moves without MXHC1.
6123 Force the constant into memory otherwise. */
6124 && (using_gprs
6125 || GPR_SIZE == 64
6126 || ISA_HAS_MXHC1 (mips_opts.isa)
6127 || FPR_SIZE == 32)
89565f1b
RS
6128 && ((data[0] == 0 && data[1] == 0)
6129 || (data[2] == 0 && data[3] == 0))
6130 && ((data[4] == 0 && data[5] == 0)
6131 || (data[6] == 0 && data[7] == 0)))
6132 {
6133 /* The value is simple enough to load with a couple of instructions.
6134 If using 32-bit registers, set IMM to the high order 32 bits and
6135 OFFSET to the low order 32 bits. Otherwise, set IMM to the entire
6136 64 bit constant. */
351cdf24 6137 if (GPR_SIZE == 32 || (!using_gprs && FPR_SIZE != 64))
89565f1b
RS
6138 {
6139 imm->X_op = O_constant;
6140 offset->X_op = O_constant;
6141 if (!target_big_endian)
6142 {
6143 imm->X_add_number = bfd_getl32 (data + 4);
6144 offset->X_add_number = bfd_getl32 (data);
6145 }
6146 else
6147 {
6148 imm->X_add_number = bfd_getb32 (data);
6149 offset->X_add_number = bfd_getb32 (data + 4);
6150 }
6151 if (offset->X_add_number == 0)
6152 offset->X_op = O_absent;
6153 }
6154 else
6155 {
6156 imm->X_op = O_constant;
6157 if (!target_big_endian)
6158 imm->X_add_number = bfd_getl64 (data);
6159 else
6160 imm->X_add_number = bfd_getb64 (data);
6161 offset->X_op = O_absent;
6162 }
a92713e6 6163 return TRUE;
89565f1b
RS
6164 }
6165
6166 /* Switch to the right section. */
6167 seg = now_seg;
6168 subseg = now_subseg;
6169 if (length == 4)
6170 {
6171 gas_assert (!using_gprs && g_switch_value >= 4);
6172 newname = ".lit4";
6173 }
6174 else
6175 {
6176 if (using_gprs || g_switch_value < 8)
6177 newname = RDATA_SECTION_NAME;
6178 else
6179 newname = ".lit8";
6180 }
6181
6182 new_seg = subseg_new (newname, (subsegT) 0);
6183 bfd_set_section_flags (stdoutput, new_seg,
6184 SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA);
6185 frag_align (length == 4 ? 2 : 3, 0, 0);
6186 if (strncmp (TARGET_OS, "elf", 3) != 0)
6187 record_alignment (new_seg, 4);
6188 else
6189 record_alignment (new_seg, length == 4 ? 2 : 3);
6190 if (seg == now_seg)
1661c76c 6191 as_bad (_("cannot use `%s' in this section"), arg->insn->insn_mo->name);
89565f1b
RS
6192
6193 /* Set the argument to the current address in the section. */
6194 imm->X_op = O_absent;
6195 offset->X_op = O_symbol;
6196 offset->X_add_symbol = symbol_temp_new_now ();
6197 offset->X_add_number = 0;
6198
6199 /* Put the floating point number into the section. */
6200 p = frag_more (length);
6201 memcpy (p, data, length);
6202
6203 /* Switch back to the original section. */
6204 subseg_set (seg, subseg);
a92713e6 6205 return TRUE;
89565f1b
RS
6206}
6207
14daeee3
RS
6208/* OP_VU0_SUFFIX and OP_VU0_MATCH_SUFFIX matcher; MATCH_P selects between
6209 them. */
6210
6211static bfd_boolean
6212match_vu0_suffix_operand (struct mips_arg_info *arg,
6213 const struct mips_operand *operand,
6214 bfd_boolean match_p)
6215{
6216 unsigned int uval;
6217
6218 /* The operand can be an XYZW mask or a single 2-bit channel index
6219 (with X being 0). */
6220 gas_assert (operand->size == 2 || operand->size == 4);
6221
ee5734f0 6222 /* The suffix can be omitted when it is already part of the opcode. */
14daeee3 6223 if (arg->token->type != OT_CHANNELS)
ee5734f0 6224 return match_p;
14daeee3
RS
6225
6226 uval = arg->token->u.channels;
6227 if (operand->size == 2)
6228 {
6229 /* Check that a single bit is set and convert it into a 2-bit index. */
6230 if ((uval & -uval) != uval)
6231 return FALSE;
6232 uval = 4 - ffs (uval);
6233 }
6234
6235 if (match_p && insn_extract_operand (arg->insn, operand) != uval)
6236 return FALSE;
6237
6238 ++arg->token;
6239 if (!match_p)
6240 insn_insert_operand (arg->insn, operand, uval);
6241 return TRUE;
6242}
6243
33f46696
MR
6244/* Try to match a token from ARG against OPERAND. Consume the token
6245 and return true on success, otherwise return false. */
a1d78564 6246
a92713e6 6247static bfd_boolean
a1d78564 6248match_operand (struct mips_arg_info *arg,
a92713e6 6249 const struct mips_operand *operand)
a1d78564
RS
6250{
6251 switch (operand->type)
6252 {
6253 case OP_INT:
a92713e6 6254 return match_int_operand (arg, operand);
a1d78564
RS
6255
6256 case OP_MAPPED_INT:
a92713e6 6257 return match_mapped_int_operand (arg, operand);
a1d78564
RS
6258
6259 case OP_MSB:
a92713e6 6260 return match_msb_operand (arg, operand);
a1d78564
RS
6261
6262 case OP_REG:
0f35dbc4 6263 case OP_OPTIONAL_REG:
a92713e6 6264 return match_reg_operand (arg, operand);
a1d78564
RS
6265
6266 case OP_REG_PAIR:
a92713e6 6267 return match_reg_pair_operand (arg, operand);
a1d78564
RS
6268
6269 case OP_PCREL:
a92713e6 6270 return match_pcrel_operand (arg);
a1d78564
RS
6271
6272 case OP_PERF_REG:
a92713e6 6273 return match_perf_reg_operand (arg, operand);
a1d78564
RS
6274
6275 case OP_ADDIUSP_INT:
a92713e6 6276 return match_addiusp_operand (arg, operand);
a1d78564
RS
6277
6278 case OP_CLO_CLZ_DEST:
a92713e6 6279 return match_clo_clz_dest_operand (arg, operand);
a1d78564
RS
6280
6281 case OP_LWM_SWM_LIST:
a92713e6 6282 return match_lwm_swm_list_operand (arg, operand);
a1d78564
RS
6283
6284 case OP_ENTRY_EXIT_LIST:
a92713e6 6285 return match_entry_exit_operand (arg, operand);
364215c8 6286
a1d78564 6287 case OP_SAVE_RESTORE_LIST:
a92713e6 6288 return match_save_restore_list_operand (arg);
a1d78564
RS
6289
6290 case OP_MDMX_IMM_REG:
a92713e6 6291 return match_mdmx_imm_reg_operand (arg, operand);
a1d78564
RS
6292
6293 case OP_REPEAT_DEST_REG:
a92713e6 6294 return match_tied_reg_operand (arg, arg->dest_regno);
a1d78564
RS
6295
6296 case OP_REPEAT_PREV_REG:
a92713e6 6297 return match_tied_reg_operand (arg, arg->last_regno);
a1d78564
RS
6298
6299 case OP_PC:
a92713e6 6300 return match_pc_operand (arg);
14daeee3 6301
25499ac7
MR
6302 case OP_REG28:
6303 return match_reg28_operand (arg);
6304
14daeee3
RS
6305 case OP_VU0_SUFFIX:
6306 return match_vu0_suffix_operand (arg, operand, FALSE);
6307
6308 case OP_VU0_MATCH_SUFFIX:
6309 return match_vu0_suffix_operand (arg, operand, TRUE);
56d438b1
CF
6310
6311 case OP_IMM_INDEX:
6312 return match_imm_index_operand (arg, operand);
6313
6314 case OP_REG_INDEX:
6315 return match_reg_index_operand (arg, operand);
7361da2c
AB
6316
6317 case OP_SAME_RS_RT:
6318 return match_same_rs_rt_operand (arg, operand);
6319
6320 case OP_CHECK_PREV:
6321 return match_check_prev_operand (arg, operand);
6322
6323 case OP_NON_ZERO_REG:
6324 return match_non_zero_reg_operand (arg, operand);
a1d78564
RS
6325 }
6326 abort ();
6327}
6328
6329/* ARG is the state after successfully matching an instruction.
6330 Issue any queued-up warnings. */
6331
6332static void
6333check_completed_insn (struct mips_arg_info *arg)
6334{
6335 if (arg->seen_at)
6336 {
6337 if (AT == ATREG)
1661c76c 6338 as_warn (_("used $at without \".set noat\""));
a1d78564 6339 else
1661c76c 6340 as_warn (_("used $%u with \".set at=$%u\""), AT, AT);
a1d78564
RS
6341 }
6342}
a1d78564 6343
85fcb30f
RS
6344/* Return true if modifying general-purpose register REG needs a delay. */
6345
6346static bfd_boolean
6347reg_needs_delay (unsigned int reg)
6348{
6349 unsigned long prev_pinfo;
6350
6351 prev_pinfo = history[0].insn_mo->pinfo;
6352 if (!mips_opts.noreorder
67dc82bc 6353 && (((prev_pinfo & INSN_LOAD_MEMORY) && !gpr_interlocks)
43885403 6354 || ((prev_pinfo & INSN_LOAD_COPROC) && !cop_interlocks))
85fcb30f
RS
6355 && (gpr_write_mask (&history[0]) & (1 << reg)))
6356 return TRUE;
6357
6358 return FALSE;
6359}
6360
71400594
RS
6361/* Classify an instruction according to the FIX_VR4120_* enumeration.
6362 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
6363 by VR4120 errata. */
4d7206a2 6364
71400594
RS
6365static unsigned int
6366classify_vr4120_insn (const char *name)
252b5132 6367{
71400594
RS
6368 if (strncmp (name, "macc", 4) == 0)
6369 return FIX_VR4120_MACC;
6370 if (strncmp (name, "dmacc", 5) == 0)
6371 return FIX_VR4120_DMACC;
6372 if (strncmp (name, "mult", 4) == 0)
6373 return FIX_VR4120_MULT;
6374 if (strncmp (name, "dmult", 5) == 0)
6375 return FIX_VR4120_DMULT;
6376 if (strstr (name, "div"))
6377 return FIX_VR4120_DIV;
6378 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
6379 return FIX_VR4120_MTHILO;
6380 return NUM_FIX_VR4120_CLASSES;
6381}
252b5132 6382
a8d14a88
CM
6383#define INSN_ERET 0x42000018
6384#define INSN_DERET 0x4200001f
6385#define INSN_DMULT 0x1c
6386#define INSN_DMULTU 0x1d
ff239038 6387
71400594
RS
6388/* Return the number of instructions that must separate INSN1 and INSN2,
6389 where INSN1 is the earlier instruction. Return the worst-case value
6390 for any INSN2 if INSN2 is null. */
252b5132 6391
71400594
RS
6392static unsigned int
6393insns_between (const struct mips_cl_insn *insn1,
6394 const struct mips_cl_insn *insn2)
6395{
6396 unsigned long pinfo1, pinfo2;
4c260379 6397 unsigned int mask;
71400594 6398
85fcb30f
RS
6399 /* If INFO2 is null, pessimistically assume that all flags are set for
6400 the second instruction. */
71400594
RS
6401 pinfo1 = insn1->insn_mo->pinfo;
6402 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
252b5132 6403
71400594
RS
6404 /* For most targets, write-after-read dependencies on the HI and LO
6405 registers must be separated by at least two instructions. */
6406 if (!hilo_interlocks)
252b5132 6407 {
71400594
RS
6408 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
6409 return 2;
6410 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
6411 return 2;
6412 }
6413
6414 /* If we're working around r7000 errata, there must be two instructions
6415 between an mfhi or mflo and any instruction that uses the result. */
6416 if (mips_7000_hilo_fix
df58fc94 6417 && !mips_opts.micromips
71400594 6418 && MF_HILO_INSN (pinfo1)
85fcb30f 6419 && (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1))))
71400594
RS
6420 return 2;
6421
ff239038
CM
6422 /* If we're working around 24K errata, one instruction is required
6423 if an ERET or DERET is followed by a branch instruction. */
df58fc94 6424 if (mips_fix_24k && !mips_opts.micromips)
ff239038
CM
6425 {
6426 if (insn1->insn_opcode == INSN_ERET
6427 || insn1->insn_opcode == INSN_DERET)
6428 {
6429 if (insn2 == NULL
6430 || insn2->insn_opcode == INSN_ERET
6431 || insn2->insn_opcode == INSN_DERET
11625dd8 6432 || delayed_branch_p (insn2))
ff239038
CM
6433 return 1;
6434 }
6435 }
6436
a8d14a88
CM
6437 /* If we're working around PMC RM7000 errata, there must be three
6438 nops between a dmult and a load instruction. */
6439 if (mips_fix_rm7000 && !mips_opts.micromips)
6440 {
6441 if ((insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULT
6442 || (insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULTU)
6443 {
6444 if (pinfo2 & INSN_LOAD_MEMORY)
6445 return 3;
6446 }
6447 }
6448
71400594
RS
6449 /* If working around VR4120 errata, check for combinations that need
6450 a single intervening instruction. */
df58fc94 6451 if (mips_fix_vr4120 && !mips_opts.micromips)
71400594
RS
6452 {
6453 unsigned int class1, class2;
252b5132 6454
71400594
RS
6455 class1 = classify_vr4120_insn (insn1->insn_mo->name);
6456 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
252b5132 6457 {
71400594
RS
6458 if (insn2 == NULL)
6459 return 1;
6460 class2 = classify_vr4120_insn (insn2->insn_mo->name);
6461 if (vr4120_conflicts[class1] & (1 << class2))
6462 return 1;
252b5132 6463 }
71400594
RS
6464 }
6465
df58fc94 6466 if (!HAVE_CODE_COMPRESSION)
71400594
RS
6467 {
6468 /* Check for GPR or coprocessor load delays. All such delays
6469 are on the RT register. */
6470 /* Itbl support may require additional care here. */
67dc82bc 6471 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY))
43885403 6472 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC)))
252b5132 6473 {
85fcb30f 6474 if (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1)))
71400594
RS
6475 return 1;
6476 }
6477
6478 /* Check for generic coprocessor hazards.
6479
6480 This case is not handled very well. There is no special
6481 knowledge of CP0 handling, and the coprocessors other than
6482 the floating point unit are not distinguished at all. */
6483 /* Itbl support may require additional care here. FIXME!
6484 Need to modify this to include knowledge about
6485 user specified delays! */
43885403 6486 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE))
71400594
RS
6487 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
6488 {
6489 /* Handle cases where INSN1 writes to a known general coprocessor
6490 register. There must be a one instruction delay before INSN2
6491 if INSN2 reads that register, otherwise no delay is needed. */
4c260379
RS
6492 mask = fpr_write_mask (insn1);
6493 if (mask != 0)
252b5132 6494 {
4c260379 6495 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
71400594 6496 return 1;
252b5132
RH
6497 }
6498 else
6499 {
71400594
RS
6500 /* Read-after-write dependencies on the control registers
6501 require a two-instruction gap. */
6502 if ((pinfo1 & INSN_WRITE_COND_CODE)
6503 && (pinfo2 & INSN_READ_COND_CODE))
6504 return 2;
6505
6506 /* We don't know exactly what INSN1 does. If INSN2 is
6507 also a coprocessor instruction, assume there must be
6508 a one instruction gap. */
6509 if (pinfo2 & INSN_COP)
6510 return 1;
252b5132
RH
6511 }
6512 }
6b76fefe 6513
71400594
RS
6514 /* Check for read-after-write dependencies on the coprocessor
6515 control registers in cases where INSN1 does not need a general
6516 coprocessor delay. This means that INSN1 is a floating point
6517 comparison instruction. */
6518 /* Itbl support may require additional care here. */
6519 else if (!cop_interlocks
6520 && (pinfo1 & INSN_WRITE_COND_CODE)
6521 && (pinfo2 & INSN_READ_COND_CODE))
6522 return 1;
6523 }
6b76fefe 6524
7361da2c
AB
6525 /* Forbidden slots can not contain Control Transfer Instructions (CTIs)
6526 CTIs include all branches and jumps, nal, eret, eretnc, deret, wait
6527 and pause. */
6528 if ((insn1->insn_mo->pinfo2 & INSN2_FORBIDDEN_SLOT)
6529 && ((pinfo2 & INSN_NO_DELAY_SLOT)
6530 || (insn2 && delayed_branch_p (insn2))))
6531 return 1;
6532
71400594
RS
6533 return 0;
6534}
6b76fefe 6535
7d8e00cf
RS
6536/* Return the number of nops that would be needed to work around the
6537 VR4130 mflo/mfhi errata if instruction INSN immediately followed
932d1a1b
RS
6538 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
6539 that are contained within the first IGNORE instructions of HIST. */
7d8e00cf
RS
6540
6541static int
932d1a1b 6542nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
7d8e00cf
RS
6543 const struct mips_cl_insn *insn)
6544{
4c260379
RS
6545 int i, j;
6546 unsigned int mask;
7d8e00cf
RS
6547
6548 /* Check if the instruction writes to HI or LO. MTHI and MTLO
6549 are not affected by the errata. */
6550 if (insn != 0
6551 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
6552 || strcmp (insn->insn_mo->name, "mtlo") == 0
6553 || strcmp (insn->insn_mo->name, "mthi") == 0))
6554 return 0;
6555
6556 /* Search for the first MFLO or MFHI. */
6557 for (i = 0; i < MAX_VR4130_NOPS; i++)
91d6fa6a 6558 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
7d8e00cf
RS
6559 {
6560 /* Extract the destination register. */
4c260379 6561 mask = gpr_write_mask (&hist[i]);
7d8e00cf
RS
6562
6563 /* No nops are needed if INSN reads that register. */
4c260379 6564 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
7d8e00cf
RS
6565 return 0;
6566
6567 /* ...or if any of the intervening instructions do. */
6568 for (j = 0; j < i; j++)
4c260379 6569 if (gpr_read_mask (&hist[j]) & mask)
7d8e00cf
RS
6570 return 0;
6571
932d1a1b
RS
6572 if (i >= ignore)
6573 return MAX_VR4130_NOPS - i;
7d8e00cf
RS
6574 }
6575 return 0;
6576}
6577
134c0c8b
MR
6578#define BASE_REG_EQ(INSN1, INSN2) \
6579 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
15be625d
CM
6580 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
6581
6582/* Return the minimum alignment for this store instruction. */
6583
6584static int
6585fix_24k_align_to (const struct mips_opcode *mo)
6586{
6587 if (strcmp (mo->name, "sh") == 0)
6588 return 2;
6589
6590 if (strcmp (mo->name, "swc1") == 0
6591 || strcmp (mo->name, "swc2") == 0
6592 || strcmp (mo->name, "sw") == 0
6593 || strcmp (mo->name, "sc") == 0
6594 || strcmp (mo->name, "s.s") == 0)
6595 return 4;
6596
6597 if (strcmp (mo->name, "sdc1") == 0
6598 || strcmp (mo->name, "sdc2") == 0
6599 || strcmp (mo->name, "s.d") == 0)
6600 return 8;
6601
6602 /* sb, swl, swr */
6603 return 1;
6604}
6605
6606struct fix_24k_store_info
6607 {
6608 /* Immediate offset, if any, for this store instruction. */
6609 short off;
6610 /* Alignment required by this store instruction. */
6611 int align_to;
6612 /* True for register offsets. */
6613 int register_offset;
6614 };
6615
6616/* Comparison function used by qsort. */
6617
6618static int
6619fix_24k_sort (const void *a, const void *b)
6620{
6621 const struct fix_24k_store_info *pos1 = a;
6622 const struct fix_24k_store_info *pos2 = b;
6623
6624 return (pos1->off - pos2->off);
6625}
6626
6627/* INSN is a store instruction. Try to record the store information
6628 in STINFO. Return false if the information isn't known. */
6629
6630static bfd_boolean
6631fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
ab9794cf 6632 const struct mips_cl_insn *insn)
15be625d
CM
6633{
6634 /* The instruction must have a known offset. */
6635 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
6636 return FALSE;
6637
6638 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
6639 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
6640 return TRUE;
6641}
6642
932d1a1b
RS
6643/* Return the number of nops that would be needed to work around the 24k
6644 "lost data on stores during refill" errata if instruction INSN
6645 immediately followed the 2 instructions described by HIST.
6646 Ignore hazards that are contained within the first IGNORE
6647 instructions of HIST.
6648
6649 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
6650 for the data cache refills and store data. The following describes
6651 the scenario where the store data could be lost.
6652
6653 * A data cache miss, due to either a load or a store, causing fill
6654 data to be supplied by the memory subsystem
6655 * The first three doublewords of fill data are returned and written
6656 into the cache
6657 * A sequence of four stores occurs in consecutive cycles around the
6658 final doubleword of the fill:
6659 * Store A
6660 * Store B
6661 * Store C
6662 * Zero, One or more instructions
6663 * Store D
6664
6665 The four stores A-D must be to different doublewords of the line that
6666 is being filled. The fourth instruction in the sequence above permits
6667 the fill of the final doubleword to be transferred from the FSB into
6668 the cache. In the sequence above, the stores may be either integer
6669 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
6670 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
6671 different doublewords on the line. If the floating point unit is
6672 running in 1:2 mode, it is not possible to create the sequence above
6673 using only floating point store instructions.
15be625d
CM
6674
6675 In this case, the cache line being filled is incorrectly marked
6676 invalid, thereby losing the data from any store to the line that
6677 occurs between the original miss and the completion of the five
6678 cycle sequence shown above.
6679
932d1a1b 6680 The workarounds are:
15be625d 6681
932d1a1b
RS
6682 * Run the data cache in write-through mode.
6683 * Insert a non-store instruction between
6684 Store A and Store B or Store B and Store C. */
3739860c 6685
15be625d 6686static int
932d1a1b 6687nops_for_24k (int ignore, const struct mips_cl_insn *hist,
15be625d
CM
6688 const struct mips_cl_insn *insn)
6689{
6690 struct fix_24k_store_info pos[3];
6691 int align, i, base_offset;
6692
932d1a1b
RS
6693 if (ignore >= 2)
6694 return 0;
6695
ab9794cf
RS
6696 /* If the previous instruction wasn't a store, there's nothing to
6697 worry about. */
15be625d
CM
6698 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6699 return 0;
6700
ab9794cf
RS
6701 /* If the instructions after the previous one are unknown, we have
6702 to assume the worst. */
6703 if (!insn)
15be625d
CM
6704 return 1;
6705
ab9794cf
RS
6706 /* Check whether we are dealing with three consecutive stores. */
6707 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
6708 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
15be625d
CM
6709 return 0;
6710
6711 /* If we don't know the relationship between the store addresses,
6712 assume the worst. */
ab9794cf 6713 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
15be625d
CM
6714 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
6715 return 1;
6716
6717 if (!fix_24k_record_store_info (&pos[0], insn)
6718 || !fix_24k_record_store_info (&pos[1], &hist[0])
6719 || !fix_24k_record_store_info (&pos[2], &hist[1]))
6720 return 1;
6721
6722 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
6723
6724 /* Pick a value of ALIGN and X such that all offsets are adjusted by
6725 X bytes and such that the base register + X is known to be aligned
6726 to align bytes. */
6727
6728 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
6729 align = 8;
6730 else
6731 {
6732 align = pos[0].align_to;
6733 base_offset = pos[0].off;
6734 for (i = 1; i < 3; i++)
6735 if (align < pos[i].align_to)
6736 {
6737 align = pos[i].align_to;
6738 base_offset = pos[i].off;
6739 }
6740 for (i = 0; i < 3; i++)
6741 pos[i].off -= base_offset;
6742 }
6743
6744 pos[0].off &= ~align + 1;
6745 pos[1].off &= ~align + 1;
6746 pos[2].off &= ~align + 1;
6747
6748 /* If any two stores write to the same chunk, they also write to the
6749 same doubleword. The offsets are still sorted at this point. */
6750 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
6751 return 0;
6752
6753 /* A range of at least 9 bytes is needed for the stores to be in
6754 non-overlapping doublewords. */
6755 if (pos[2].off - pos[0].off <= 8)
6756 return 0;
6757
6758 if (pos[2].off - pos[1].off >= 24
6759 || pos[1].off - pos[0].off >= 24
6760 || pos[2].off - pos[0].off >= 32)
6761 return 0;
6762
6763 return 1;
6764}
6765
71400594 6766/* Return the number of nops that would be needed if instruction INSN
91d6fa6a 6767 immediately followed the MAX_NOPS instructions given by HIST,
932d1a1b
RS
6768 where HIST[0] is the most recent instruction. Ignore hazards
6769 between INSN and the first IGNORE instructions in HIST.
6770
6771 If INSN is null, return the worse-case number of nops for any
6772 instruction. */
bdaaa2e1 6773
71400594 6774static int
932d1a1b 6775nops_for_insn (int ignore, const struct mips_cl_insn *hist,
71400594
RS
6776 const struct mips_cl_insn *insn)
6777{
6778 int i, nops, tmp_nops;
bdaaa2e1 6779
71400594 6780 nops = 0;
932d1a1b 6781 for (i = ignore; i < MAX_DELAY_NOPS; i++)
65b02341 6782 {
91d6fa6a 6783 tmp_nops = insns_between (hist + i, insn) - i;
65b02341
RS
6784 if (tmp_nops > nops)
6785 nops = tmp_nops;
6786 }
7d8e00cf 6787
df58fc94 6788 if (mips_fix_vr4130 && !mips_opts.micromips)
7d8e00cf 6789 {
932d1a1b 6790 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
7d8e00cf
RS
6791 if (tmp_nops > nops)
6792 nops = tmp_nops;
6793 }
6794
df58fc94 6795 if (mips_fix_24k && !mips_opts.micromips)
15be625d 6796 {
932d1a1b 6797 tmp_nops = nops_for_24k (ignore, hist, insn);
15be625d
CM
6798 if (tmp_nops > nops)
6799 nops = tmp_nops;
6800 }
6801
71400594
RS
6802 return nops;
6803}
252b5132 6804
71400594 6805/* The variable arguments provide NUM_INSNS extra instructions that
91d6fa6a 6806 might be added to HIST. Return the largest number of nops that
932d1a1b
RS
6807 would be needed after the extended sequence, ignoring hazards
6808 in the first IGNORE instructions. */
252b5132 6809
71400594 6810static int
932d1a1b
RS
6811nops_for_sequence (int num_insns, int ignore,
6812 const struct mips_cl_insn *hist, ...)
71400594
RS
6813{
6814 va_list args;
6815 struct mips_cl_insn buffer[MAX_NOPS];
6816 struct mips_cl_insn *cursor;
6817 int nops;
6818
91d6fa6a 6819 va_start (args, hist);
71400594 6820 cursor = buffer + num_insns;
91d6fa6a 6821 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
71400594
RS
6822 while (cursor > buffer)
6823 *--cursor = *va_arg (args, const struct mips_cl_insn *);
6824
932d1a1b 6825 nops = nops_for_insn (ignore, buffer, NULL);
71400594
RS
6826 va_end (args);
6827 return nops;
6828}
252b5132 6829
71400594
RS
6830/* Like nops_for_insn, but if INSN is a branch, take into account the
6831 worst-case delay for the branch target. */
252b5132 6832
71400594 6833static int
932d1a1b 6834nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
71400594
RS
6835 const struct mips_cl_insn *insn)
6836{
6837 int nops, tmp_nops;
60b63b72 6838
932d1a1b 6839 nops = nops_for_insn (ignore, hist, insn);
11625dd8 6840 if (delayed_branch_p (insn))
71400594 6841 {
932d1a1b 6842 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
14fe068b 6843 hist, insn, get_delay_slot_nop (insn));
71400594
RS
6844 if (tmp_nops > nops)
6845 nops = tmp_nops;
6846 }
11625dd8 6847 else if (compact_branch_p (insn))
71400594 6848 {
932d1a1b 6849 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
71400594
RS
6850 if (tmp_nops > nops)
6851 nops = tmp_nops;
6852 }
6853 return nops;
6854}
6855
c67a084a
NC
6856/* Fix NOP issue: Replace nops by "or at,at,zero". */
6857
6858static void
6859fix_loongson2f_nop (struct mips_cl_insn * ip)
6860{
df58fc94 6861 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
6862 if (strcmp (ip->insn_mo->name, "nop") == 0)
6863 ip->insn_opcode = LOONGSON2F_NOP_INSN;
6864}
6865
6866/* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
6867 jr target pc &= 'hffff_ffff_cfff_ffff. */
6868
6869static void
6870fix_loongson2f_jump (struct mips_cl_insn * ip)
6871{
df58fc94 6872 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
6873 if (strcmp (ip->insn_mo->name, "j") == 0
6874 || strcmp (ip->insn_mo->name, "jr") == 0
6875 || strcmp (ip->insn_mo->name, "jalr") == 0)
6876 {
6877 int sreg;
6878 expressionS ep;
6879
6880 if (! mips_opts.at)
6881 return;
6882
df58fc94 6883 sreg = EXTRACT_OPERAND (0, RS, *ip);
c67a084a
NC
6884 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
6885 return;
6886
6887 ep.X_op = O_constant;
6888 ep.X_add_number = 0xcfff0000;
6889 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
6890 ep.X_add_number = 0xffff;
6891 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
6892 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
6893 }
6894}
6895
6896static void
6897fix_loongson2f (struct mips_cl_insn * ip)
6898{
6899 if (mips_fix_loongson2f_nop)
6900 fix_loongson2f_nop (ip);
6901
6902 if (mips_fix_loongson2f_jump)
6903 fix_loongson2f_jump (ip);
6904}
6905
6f2117ba
PH
6906/* Fix loongson3 llsc errata: Insert sync before ll/lld. */
6907
6908static void
6909fix_loongson3_llsc (struct mips_cl_insn * ip)
6910{
6911 gas_assert (!HAVE_CODE_COMPRESSION);
6912
6913 /* If is an local label and the insn is not sync,
6914 look forward that whether an branch between ll/sc jump to here
6915 if so, insert a sync. */
6916 if (seg_info (now_seg)->label_list
6917 && S_IS_LOCAL (seg_info (now_seg)->label_list->label)
6918 && (strcmp (ip->insn_mo->name, "sync") != 0))
6919 {
6920 const char *label_name = S_GET_NAME (seg_info (now_seg)->label_list->label);
6921 unsigned long lookback = ARRAY_SIZE (history);
6922 unsigned long i;
6923
6924 for (i = 0; i < lookback; i++)
6925 {
6926 if (streq (history[i].insn_mo->name, "ll")
6927 || streq (history[i].insn_mo->name, "lld"))
6928 break;
6929
6930 if (streq (history[i].insn_mo->name, "sc")
6931 || streq (history[i].insn_mo->name, "scd"))
6932 {
6933 unsigned long j;
6934
6935 for (j = i + 1; j < lookback; j++)
6936 {
6937 if (streq (history[i].insn_mo->name, "ll")
6938 || streq (history[i].insn_mo->name, "lld"))
6939 break;
6940
6941 if (delayed_branch_p (&history[j]))
6942 {
6943 if (streq (history[j].target, label_name))
6944 {
6945 add_fixed_insn (&sync_insn);
6946 insert_into_history (0, 1, &sync_insn);
6947 i = lookback;
6948 break;
6949 }
6950 }
6951 }
6952 }
6953 }
6954 }
6955 /* If we find a sc, we look forward to look for an branch insn,
6956 and see whether it jump back and out of ll/sc. */
6957 else if (streq(ip->insn_mo->name, "sc") || streq(ip->insn_mo->name, "scd"))
6958 {
6959 unsigned long lookback = ARRAY_SIZE (history) - 1;
6960 unsigned long i;
6961
6962 for (i = 0; i < lookback; i++)
6963 {
6964 if (streq (history[i].insn_mo->name, "ll")
6965 || streq (history[i].insn_mo->name, "lld"))
6966 break;
6967
6968 if (delayed_branch_p (&history[i]))
6969 {
6970 unsigned long j;
6971
6972 for (j = i + 1; j < lookback; j++)
6973 {
6974 if (streq (history[j].insn_mo->name, "ll")
6975 || streq (history[i].insn_mo->name, "lld"))
6976 break;
6977 }
6978
6979 for (; j < lookback; j++)
6980 {
6981 if (history[j].label[0] != '\0'
6982 && streq (history[j].label, history[i].target)
6983 && strcmp (history[j+1].insn_mo->name, "sync") != 0)
6984 {
6985 add_fixed_insn (&sync_insn);
6986 insert_into_history (++j, 1, &sync_insn);
6987 }
6988 }
6989 }
6990 }
6991 }
6992
6993 /* Skip if there is a sync before ll/lld. */
6994 if ((strcmp (ip->insn_mo->name, "ll") == 0
6995 || strcmp (ip->insn_mo->name, "lld") == 0)
6996 && (strcmp (history[0].insn_mo->name, "sync") != 0))
6997 {
6998 add_fixed_insn (&sync_insn);
6999 insert_into_history (0, 1, &sync_insn);
7000 }
7001}
7002
a4e06468
RS
7003/* IP is a branch that has a delay slot, and we need to fill it
7004 automatically. Return true if we can do that by swapping IP
e407c74b
NC
7005 with the previous instruction.
7006 ADDRESS_EXPR is an operand of the instruction to be used with
7007 RELOC_TYPE. */
a4e06468
RS
7008
7009static bfd_boolean
e407c74b 7010can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 7011 bfd_reloc_code_real_type *reloc_type)
a4e06468 7012{
2b0c8b40 7013 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
a4e06468 7014 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
9d5de888 7015 unsigned int fpr_read, prev_fpr_write;
a4e06468
RS
7016
7017 /* -O2 and above is required for this optimization. */
7018 if (mips_optimize < 2)
7019 return FALSE;
7020
7021 /* If we have seen .set volatile or .set nomove, don't optimize. */
7022 if (mips_opts.nomove)
7023 return FALSE;
7024
7025 /* We can't swap if the previous instruction's position is fixed. */
7026 if (history[0].fixed_p)
7027 return FALSE;
7028
7029 /* If the previous previous insn was in a .set noreorder, we can't
7030 swap. Actually, the MIPS assembler will swap in this situation.
7031 However, gcc configured -with-gnu-as will generate code like
7032
7033 .set noreorder
7034 lw $4,XXX
7035 .set reorder
7036 INSN
7037 bne $4,$0,foo
7038
7039 in which we can not swap the bne and INSN. If gcc is not configured
7040 -with-gnu-as, it does not output the .set pseudo-ops. */
7041 if (history[1].noreorder_p)
7042 return FALSE;
7043
87333bb7
MR
7044 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
7045 This means that the previous instruction was a 4-byte one anyhow. */
a4e06468
RS
7046 if (mips_opts.mips16 && history[0].fixp[0])
7047 return FALSE;
7048
7049 /* If the branch is itself the target of a branch, we can not swap.
7050 We cheat on this; all we check for is whether there is a label on
7051 this instruction. If there are any branches to anything other than
7052 a label, users must use .set noreorder. */
7053 if (seg_info (now_seg)->label_list)
7054 return FALSE;
7055
7056 /* If the previous instruction is in a variant frag other than this
2309ddf2 7057 branch's one, we cannot do the swap. This does not apply to
9301f9c3
MR
7058 MIPS16 code, which uses variant frags for different purposes. */
7059 if (!mips_opts.mips16
a4e06468
RS
7060 && history[0].frag
7061 && history[0].frag->fr_type == rs_machine_dependent)
7062 return FALSE;
7063
bcd530a7
RS
7064 /* We do not swap with instructions that cannot architecturally
7065 be placed in a branch delay slot, such as SYNC or ERET. We
7066 also refrain from swapping with a trap instruction, since it
7067 complicates trap handlers to have the trap instruction be in
7068 a delay slot. */
a4e06468 7069 prev_pinfo = history[0].insn_mo->pinfo;
bcd530a7 7070 if (prev_pinfo & INSN_NO_DELAY_SLOT)
a4e06468
RS
7071 return FALSE;
7072
7073 /* Check for conflicts between the branch and the instructions
7074 before the candidate delay slot. */
7075 if (nops_for_insn (0, history + 1, ip) > 0)
7076 return FALSE;
7077
7078 /* Check for conflicts between the swapped sequence and the
7079 target of the branch. */
7080 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
7081 return FALSE;
7082
7083 /* If the branch reads a register that the previous
7084 instruction sets, we can not swap. */
7085 gpr_read = gpr_read_mask (ip);
7086 prev_gpr_write = gpr_write_mask (&history[0]);
7087 if (gpr_read & prev_gpr_write)
7088 return FALSE;
7089
9d5de888
CF
7090 fpr_read = fpr_read_mask (ip);
7091 prev_fpr_write = fpr_write_mask (&history[0]);
7092 if (fpr_read & prev_fpr_write)
7093 return FALSE;
7094
a4e06468
RS
7095 /* If the branch writes a register that the previous
7096 instruction sets, we can not swap. */
7097 gpr_write = gpr_write_mask (ip);
7098 if (gpr_write & prev_gpr_write)
7099 return FALSE;
7100
7101 /* If the branch writes a register that the previous
7102 instruction reads, we can not swap. */
7103 prev_gpr_read = gpr_read_mask (&history[0]);
7104 if (gpr_write & prev_gpr_read)
7105 return FALSE;
7106
7107 /* If one instruction sets a condition code and the
7108 other one uses a condition code, we can not swap. */
7109 pinfo = ip->insn_mo->pinfo;
7110 if ((pinfo & INSN_READ_COND_CODE)
7111 && (prev_pinfo & INSN_WRITE_COND_CODE))
7112 return FALSE;
7113 if ((pinfo & INSN_WRITE_COND_CODE)
7114 && (prev_pinfo & INSN_READ_COND_CODE))
7115 return FALSE;
7116
7117 /* If the previous instruction uses the PC, we can not swap. */
2b0c8b40 7118 prev_pinfo2 = history[0].insn_mo->pinfo2;
26545944 7119 if (prev_pinfo2 & INSN2_READ_PC)
2b0c8b40 7120 return FALSE;
a4e06468 7121
df58fc94
RS
7122 /* If the previous instruction has an incorrect size for a fixed
7123 branch delay slot in microMIPS mode, we cannot swap. */
2309ddf2
MR
7124 pinfo2 = ip->insn_mo->pinfo2;
7125 if (mips_opts.micromips
7126 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
7127 && insn_length (history) != 2)
7128 return FALSE;
7129 if (mips_opts.micromips
7130 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
7131 && insn_length (history) != 4)
7132 return FALSE;
7133
33d64ca5
FN
7134 /* On the R5900 short loops need to be fixed by inserting a NOP in the
7135 branch delay slot.
7136
7137 The short loop bug under certain conditions causes loops to execute
7138 only once or twice. We must ensure that the assembler never
7139 generates loops that satisfy all of the following conditions:
7140
7141 - a loop consists of less than or equal to six instructions
7142 (including the branch delay slot);
7143 - a loop contains only one conditional branch instruction at the end
7144 of the loop;
7145 - a loop does not contain any other branch or jump instructions;
7146 - a branch delay slot of the loop is not NOP (EE 2.9 or later).
7147
7148 We need to do this because of a hardware bug in the R5900 chip. */
27c634e0 7149 if (mips_fix_r5900
e407c74b
NC
7150 /* Check if instruction has a parameter, ignore "j $31". */
7151 && (address_expr != NULL)
7152 /* Parameter must be 16 bit. */
7153 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
7154 /* Branch to same segment. */
41065f5e 7155 && (S_GET_SEGMENT (address_expr->X_add_symbol) == now_seg)
e407c74b 7156 /* Branch to same code fragment. */
41065f5e 7157 && (symbol_get_frag (address_expr->X_add_symbol) == frag_now)
e407c74b 7158 /* Can only calculate branch offset if value is known. */
41065f5e 7159 && symbol_constant_p (address_expr->X_add_symbol)
e407c74b
NC
7160 /* Check if branch is really conditional. */
7161 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
7162 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
7163 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
7164 {
7165 int distance;
33d64ca5
FN
7166 /* Check if loop is shorter than or equal to 6 instructions
7167 including branch and delay slot. */
41065f5e 7168 distance = frag_now_fix () - S_GET_VALUE (address_expr->X_add_symbol);
e407c74b
NC
7169 if (distance <= 20)
7170 {
7171 int i;
7172 int rv;
7173
7174 rv = FALSE;
7175 /* When the loop includes branches or jumps,
7176 it is not a short loop. */
7177 for (i = 0; i < (distance / 4); i++)
7178 {
7179 if ((history[i].cleared_p)
41065f5e 7180 || delayed_branch_p (&history[i]))
e407c74b
NC
7181 {
7182 rv = TRUE;
7183 break;
7184 }
7185 }
535b785f 7186 if (!rv)
e407c74b
NC
7187 {
7188 /* Insert nop after branch to fix short loop. */
7189 return FALSE;
7190 }
7191 }
7192 }
7193
a4e06468
RS
7194 return TRUE;
7195}
7196
e407c74b
NC
7197/* Decide how we should add IP to the instruction stream.
7198 ADDRESS_EXPR is an operand of the instruction to be used with
7199 RELOC_TYPE. */
a4e06468
RS
7200
7201static enum append_method
e407c74b 7202get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 7203 bfd_reloc_code_real_type *reloc_type)
a4e06468 7204{
a4e06468
RS
7205 /* The relaxed version of a macro sequence must be inherently
7206 hazard-free. */
7207 if (mips_relax.sequence == 2)
7208 return APPEND_ADD;
7209
3b821a28 7210 /* We must not dabble with instructions in a ".set noreorder" block. */
a4e06468
RS
7211 if (mips_opts.noreorder)
7212 return APPEND_ADD;
7213
7214 /* Otherwise, it's our responsibility to fill branch delay slots. */
11625dd8 7215 if (delayed_branch_p (ip))
a4e06468 7216 {
e407c74b
NC
7217 if (!branch_likely_p (ip)
7218 && can_swap_branch_p (ip, address_expr, reloc_type))
a4e06468
RS
7219 return APPEND_SWAP;
7220
7221 if (mips_opts.mips16
7222 && ISA_SUPPORTS_MIPS16E
fc76e730 7223 && gpr_read_mask (ip) != 0)
a4e06468
RS
7224 return APPEND_ADD_COMPACT;
7225
7bd374a4
MR
7226 if (mips_opts.micromips
7227 && ((ip->insn_opcode & 0xffe0) == 0x4580
7228 || (!forced_insn_length
7229 && ((ip->insn_opcode & 0xfc00) == 0xcc00
7230 || (ip->insn_opcode & 0xdc00) == 0x8c00))
7231 || (ip->insn_opcode & 0xdfe00000) == 0x94000000
7232 || (ip->insn_opcode & 0xdc1f0000) == 0x94000000))
7233 return APPEND_ADD_COMPACT;
7234
a4e06468
RS
7235 return APPEND_ADD_WITH_NOP;
7236 }
7237
a4e06468
RS
7238 return APPEND_ADD;
7239}
7240
7bd374a4
MR
7241/* IP is an instruction whose opcode we have just changed, END points
7242 to the end of the opcode table processed. Point IP->insn_mo to the
7243 new opcode's definition. */
ceb94aa5
RS
7244
7245static void
7bd374a4 7246find_altered_opcode (struct mips_cl_insn *ip, const struct mips_opcode *end)
ceb94aa5 7247{
7bd374a4 7248 const struct mips_opcode *mo;
ceb94aa5 7249
ceb94aa5 7250 for (mo = ip->insn_mo; mo < end; mo++)
7bd374a4
MR
7251 if (mo->pinfo != INSN_MACRO
7252 && (ip->insn_opcode & mo->mask) == mo->match)
ceb94aa5
RS
7253 {
7254 ip->insn_mo = mo;
7255 return;
7256 }
7257 abort ();
7258}
7259
7bd374a4
MR
7260/* IP is a MIPS16 instruction whose opcode we have just changed.
7261 Point IP->insn_mo to the new opcode's definition. */
7262
7263static void
7264find_altered_mips16_opcode (struct mips_cl_insn *ip)
7265{
7266 find_altered_opcode (ip, &mips16_opcodes[bfd_mips16_num_opcodes]);
7267}
7268
7269/* IP is a microMIPS instruction whose opcode we have just changed.
7270 Point IP->insn_mo to the new opcode's definition. */
7271
7272static void
7273find_altered_micromips_opcode (struct mips_cl_insn *ip)
7274{
7275 find_altered_opcode (ip, &micromips_opcodes[bfd_micromips_num_opcodes]);
7276}
7277
df58fc94
RS
7278/* For microMIPS macros, we need to generate a local number label
7279 as the target of branches. */
7280#define MICROMIPS_LABEL_CHAR '\037'
7281static unsigned long micromips_target_label;
7282static char micromips_target_name[32];
7283
7284static char *
7285micromips_label_name (void)
7286{
7287 char *p = micromips_target_name;
7288 char symbol_name_temporary[24];
7289 unsigned long l;
7290 int i;
7291
7292 if (*p)
7293 return p;
7294
7295 i = 0;
7296 l = micromips_target_label;
7297#ifdef LOCAL_LABEL_PREFIX
7298 *p++ = LOCAL_LABEL_PREFIX;
7299#endif
7300 *p++ = 'L';
7301 *p++ = MICROMIPS_LABEL_CHAR;
7302 do
7303 {
7304 symbol_name_temporary[i++] = l % 10 + '0';
7305 l /= 10;
7306 }
7307 while (l != 0);
7308 while (i > 0)
7309 *p++ = symbol_name_temporary[--i];
7310 *p = '\0';
7311
7312 return micromips_target_name;
7313}
7314
7315static void
7316micromips_label_expr (expressionS *label_expr)
7317{
7318 label_expr->X_op = O_symbol;
7319 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
7320 label_expr->X_add_number = 0;
7321}
7322
7323static void
7324micromips_label_inc (void)
7325{
7326 micromips_target_label++;
7327 *micromips_target_name = '\0';
7328}
7329
7330static void
7331micromips_add_label (void)
7332{
7333 symbolS *s;
7334
7335 s = colon (micromips_label_name ());
7336 micromips_label_inc ();
f3ded42a 7337 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
df58fc94
RS
7338}
7339
7340/* If assembling microMIPS code, then return the microMIPS reloc
7341 corresponding to the requested one if any. Otherwise return
7342 the reloc unchanged. */
7343
7344static bfd_reloc_code_real_type
7345micromips_map_reloc (bfd_reloc_code_real_type reloc)
7346{
7347 static const bfd_reloc_code_real_type relocs[][2] =
7348 {
7349 /* Keep sorted incrementally by the left-hand key. */
7350 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
7351 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
7352 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
7353 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
7354 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
7355 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
7356 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
7357 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
7358 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
7359 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
7360 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
7361 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
7362 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
7363 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
7364 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
7365 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
7366 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
7367 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
7368 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
7369 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
7370 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
7371 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
7372 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
7373 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
7374 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
7375 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
7376 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
7377 };
7378 bfd_reloc_code_real_type r;
7379 size_t i;
7380
7381 if (!mips_opts.micromips)
7382 return reloc;
7383 for (i = 0; i < ARRAY_SIZE (relocs); i++)
7384 {
7385 r = relocs[i][0];
7386 if (r > reloc)
7387 return reloc;
7388 if (r == reloc)
7389 return relocs[i][1];
7390 }
7391 return reloc;
7392}
7393
b886a2ab
RS
7394/* Try to resolve relocation RELOC against constant OPERAND at assembly time.
7395 Return true on success, storing the resolved value in RESULT. */
7396
7397static bfd_boolean
7398calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
7399 offsetT *result)
7400{
7401 switch (reloc)
7402 {
7403 case BFD_RELOC_MIPS_HIGHEST:
7404 case BFD_RELOC_MICROMIPS_HIGHEST:
7405 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
7406 return TRUE;
7407
7408 case BFD_RELOC_MIPS_HIGHER:
7409 case BFD_RELOC_MICROMIPS_HIGHER:
7410 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
7411 return TRUE;
7412
7413 case BFD_RELOC_HI16_S:
41947d9e 7414 case BFD_RELOC_HI16_S_PCREL:
b886a2ab
RS
7415 case BFD_RELOC_MICROMIPS_HI16_S:
7416 case BFD_RELOC_MIPS16_HI16_S:
7417 *result = ((operand + 0x8000) >> 16) & 0xffff;
7418 return TRUE;
7419
7420 case BFD_RELOC_HI16:
7421 case BFD_RELOC_MICROMIPS_HI16:
7422 case BFD_RELOC_MIPS16_HI16:
7423 *result = (operand >> 16) & 0xffff;
7424 return TRUE;
7425
7426 case BFD_RELOC_LO16:
41947d9e 7427 case BFD_RELOC_LO16_PCREL:
b886a2ab
RS
7428 case BFD_RELOC_MICROMIPS_LO16:
7429 case BFD_RELOC_MIPS16_LO16:
7430 *result = operand & 0xffff;
7431 return TRUE;
7432
7433 case BFD_RELOC_UNUSED:
7434 *result = operand;
7435 return TRUE;
7436
7437 default:
7438 return FALSE;
7439 }
7440}
7441
71400594
RS
7442/* Output an instruction. IP is the instruction information.
7443 ADDRESS_EXPR is an operand of the instruction to be used with
df58fc94
RS
7444 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
7445 a macro expansion. */
71400594
RS
7446
7447static void
7448append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
df58fc94 7449 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
71400594 7450{
14fe068b 7451 unsigned long prev_pinfo2, pinfo;
71400594 7452 bfd_boolean relaxed_branch = FALSE;
a4e06468 7453 enum append_method method;
2309ddf2 7454 bfd_boolean relax32;
2b0c8b40 7455 int branch_disp;
71400594 7456
2309ddf2 7457 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
c67a084a
NC
7458 fix_loongson2f (ip);
7459
6f2117ba
PH
7460 ip->target[0] = '\0';
7461 if (offset_expr.X_op == O_symbol)
7462 strncpy (ip->target, S_GET_NAME (offset_expr.X_add_symbol), 15);
7463 ip->label[0] = '\0';
7464 if (seg_info (now_seg)->label_list)
7465 strncpy (ip->label, S_GET_NAME (seg_info (now_seg)->label_list->label), 15);
7466 if (mips_fix_loongson3_llsc && !HAVE_CODE_COMPRESSION)
7467 fix_loongson3_llsc (ip);
7468
738f4d98 7469 file_ase_mips16 |= mips_opts.mips16;
df58fc94 7470 file_ase_micromips |= mips_opts.micromips;
738f4d98 7471
df58fc94 7472 prev_pinfo2 = history[0].insn_mo->pinfo2;
71400594 7473 pinfo = ip->insn_mo->pinfo;
df58fc94 7474
7bd374a4
MR
7475 /* Don't raise alarm about `nods' frags as they'll fill in the right
7476 kind of nop in relaxation if required. */
df58fc94
RS
7477 if (mips_opts.micromips
7478 && !expansionp
7bd374a4
MR
7479 && !(history[0].frag
7480 && history[0].frag->fr_type == rs_machine_dependent
7481 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
7482 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
df58fc94
RS
7483 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
7484 && micromips_insn_length (ip->insn_mo) != 2)
7485 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
7486 && micromips_insn_length (ip->insn_mo) != 4)))
1661c76c 7487 as_warn (_("wrong size instruction in a %u-bit branch delay slot"),
df58fc94 7488 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
71400594 7489
15be625d
CM
7490 if (address_expr == NULL)
7491 ip->complete_p = 1;
b886a2ab
RS
7492 else if (reloc_type[0] <= BFD_RELOC_UNUSED
7493 && reloc_type[1] == BFD_RELOC_UNUSED
7494 && reloc_type[2] == BFD_RELOC_UNUSED
15be625d
CM
7495 && address_expr->X_op == O_constant)
7496 {
15be625d
CM
7497 switch (*reloc_type)
7498 {
15be625d 7499 case BFD_RELOC_MIPS_JMP:
df58fc94
RS
7500 {
7501 int shift;
7502
17c6c9d9
MR
7503 /* Shift is 2, unusually, for microMIPS JALX. */
7504 shift = (mips_opts.micromips
7505 && strcmp (ip->insn_mo->name, "jalx") != 0) ? 1 : 2;
df58fc94
RS
7506 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7507 as_bad (_("jump to misaligned address (0x%lx)"),
7508 (unsigned long) address_expr->X_add_number);
7509 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7510 & 0x3ffffff);
335574df 7511 ip->complete_p = 1;
df58fc94 7512 }
15be625d
CM
7513 break;
7514
7515 case BFD_RELOC_MIPS16_JMP:
7516 if ((address_expr->X_add_number & 3) != 0)
7517 as_bad (_("jump to misaligned address (0x%lx)"),
7518 (unsigned long) address_expr->X_add_number);
7519 ip->insn_opcode |=
7520 (((address_expr->X_add_number & 0x7c0000) << 3)
7521 | ((address_expr->X_add_number & 0xf800000) >> 7)
7522 | ((address_expr->X_add_number & 0x3fffc) >> 2));
335574df 7523 ip->complete_p = 1;
15be625d
CM
7524 break;
7525
7526 case BFD_RELOC_16_PCREL_S2:
df58fc94
RS
7527 {
7528 int shift;
7529
7530 shift = mips_opts.micromips ? 1 : 2;
7531 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7532 as_bad (_("branch to misaligned address (0x%lx)"),
7533 (unsigned long) address_expr->X_add_number);
7534 if (!mips_relax_branch)
7535 {
7536 if ((address_expr->X_add_number + (1 << (shift + 15)))
7537 & ~((1 << (shift + 16)) - 1))
7538 as_bad (_("branch address range overflow (0x%lx)"),
7539 (unsigned long) address_expr->X_add_number);
7540 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7541 & 0xffff);
7542 }
df58fc94 7543 }
15be625d
CM
7544 break;
7545
7361da2c
AB
7546 case BFD_RELOC_MIPS_21_PCREL_S2:
7547 {
7548 int shift;
7549
7550 shift = 2;
7551 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7552 as_bad (_("branch to misaligned address (0x%lx)"),
7553 (unsigned long) address_expr->X_add_number);
7554 if ((address_expr->X_add_number + (1 << (shift + 20)))
7555 & ~((1 << (shift + 21)) - 1))
7556 as_bad (_("branch address range overflow (0x%lx)"),
7557 (unsigned long) address_expr->X_add_number);
7558 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7559 & 0x1fffff);
7560 }
7561 break;
7562
7563 case BFD_RELOC_MIPS_26_PCREL_S2:
7564 {
7565 int shift;
7566
7567 shift = 2;
7568 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7569 as_bad (_("branch to misaligned address (0x%lx)"),
7570 (unsigned long) address_expr->X_add_number);
7571 if ((address_expr->X_add_number + (1 << (shift + 25)))
7572 & ~((1 << (shift + 26)) - 1))
7573 as_bad (_("branch address range overflow (0x%lx)"),
7574 (unsigned long) address_expr->X_add_number);
7575 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7576 & 0x3ffffff);
7577 }
7578 break;
7579
15be625d 7580 default:
b886a2ab
RS
7581 {
7582 offsetT value;
7583
7584 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
7585 &value))
7586 {
7587 ip->insn_opcode |= value & 0xffff;
7588 ip->complete_p = 1;
7589 }
7590 }
7591 break;
7592 }
15be625d
CM
7593 }
7594
71400594
RS
7595 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
7596 {
7597 /* There are a lot of optimizations we could do that we don't.
7598 In particular, we do not, in general, reorder instructions.
7599 If you use gcc with optimization, it will reorder
7600 instructions and generally do much more optimization then we
7601 do here; repeating all that work in the assembler would only
7602 benefit hand written assembly code, and does not seem worth
7603 it. */
7604 int nops = (mips_optimize == 0
932d1a1b
RS
7605 ? nops_for_insn (0, history, NULL)
7606 : nops_for_insn_or_target (0, history, ip));
71400594 7607 if (nops > 0)
252b5132
RH
7608 {
7609 fragS *old_frag;
7610 unsigned long old_frag_offset;
7611 int i;
252b5132
RH
7612
7613 old_frag = frag_now;
7614 old_frag_offset = frag_now_fix ();
7615
7616 for (i = 0; i < nops; i++)
14fe068b
RS
7617 add_fixed_insn (NOP_INSN);
7618 insert_into_history (0, nops, NOP_INSN);
252b5132
RH
7619
7620 if (listing)
7621 {
7622 listing_prev_line ();
7623 /* We may be at the start of a variant frag. In case we
7624 are, make sure there is enough space for the frag
7625 after the frags created by listing_prev_line. The
7626 argument to frag_grow here must be at least as large
7627 as the argument to all other calls to frag_grow in
7628 this file. We don't have to worry about being in the
7629 middle of a variant frag, because the variants insert
7630 all needed nop instructions themselves. */
7631 frag_grow (40);
7632 }
7633
462427c4 7634 mips_move_text_labels ();
252b5132
RH
7635
7636#ifndef NO_ECOFF_DEBUGGING
7637 if (ECOFF_DEBUGGING)
7638 ecoff_fix_loc (old_frag, old_frag_offset);
7639#endif
7640 }
71400594
RS
7641 }
7642 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
7643 {
932d1a1b
RS
7644 int nops;
7645
7646 /* Work out how many nops in prev_nop_frag are needed by IP,
7647 ignoring hazards generated by the first prev_nop_frag_since
7648 instructions. */
7649 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
9c2799c2 7650 gas_assert (nops <= prev_nop_frag_holds);
252b5132 7651
71400594
RS
7652 /* Enforce NOPS as a minimum. */
7653 if (nops > prev_nop_frag_required)
7654 prev_nop_frag_required = nops;
252b5132 7655
71400594
RS
7656 if (prev_nop_frag_holds == prev_nop_frag_required)
7657 {
7658 /* Settle for the current number of nops. Update the history
7659 accordingly (for the benefit of any future .set reorder code). */
7660 prev_nop_frag = NULL;
7661 insert_into_history (prev_nop_frag_since,
7662 prev_nop_frag_holds, NOP_INSN);
7663 }
7664 else
7665 {
7666 /* Allow this instruction to replace one of the nops that was
7667 tentatively added to prev_nop_frag. */
df58fc94 7668 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
71400594
RS
7669 prev_nop_frag_holds--;
7670 prev_nop_frag_since++;
252b5132
RH
7671 }
7672 }
7673
e407c74b 7674 method = get_append_method (ip, address_expr, reloc_type);
2b0c8b40 7675 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
a4e06468 7676
e410add4
RS
7677 dwarf2_emit_insn (0);
7678 /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
7679 so "move" the instruction address accordingly.
7680
7681 Also, it doesn't seem appropriate for the assembler to reorder .loc
7682 entries. If this instruction is a branch that we are going to swap
7683 with the previous instruction, the two instructions should be
7684 treated as a unit, and the debug information for both instructions
7685 should refer to the start of the branch sequence. Using the
7686 current position is certainly wrong when swapping a 32-bit branch
7687 and a 16-bit delay slot, since the current position would then be
7688 in the middle of a branch. */
7689 dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
58e2ea4d 7690
df58fc94
RS
7691 relax32 = (mips_relax_branch
7692 /* Don't try branch relaxation within .set nomacro, or within
7693 .set noat if we use $at for PIC computations. If it turns
7694 out that the branch was out-of-range, we'll get an error. */
7695 && !mips_opts.warn_about_macros
7696 && (mips_opts.at || mips_pic == NO_PIC)
3bf0dbfb
MR
7697 /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
7698 as they have no complementing branches. */
7699 && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
df58fc94
RS
7700
7701 if (!HAVE_CODE_COMPRESSION
7702 && address_expr
7703 && relax32
0b25d3e6 7704 && *reloc_type == BFD_RELOC_16_PCREL_S2
11625dd8 7705 && delayed_branch_p (ip))
4a6a3df4 7706 {
895921c9 7707 relaxed_branch = TRUE;
1e915849
RS
7708 add_relaxed_insn (ip, (relaxed_branch_length
7709 (NULL, NULL,
11625dd8
RS
7710 uncond_branch_p (ip) ? -1
7711 : branch_likely_p (ip) ? 1
1e915849
RS
7712 : 0)), 4,
7713 RELAX_BRANCH_ENCODE
ce8ad872 7714 (AT, mips_pic != NO_PIC,
11625dd8
RS
7715 uncond_branch_p (ip),
7716 branch_likely_p (ip),
1e915849
RS
7717 pinfo & INSN_WRITE_GPR_31,
7718 0),
7719 address_expr->X_add_symbol,
7720 address_expr->X_add_number);
4a6a3df4
AO
7721 *reloc_type = BFD_RELOC_UNUSED;
7722 }
df58fc94
RS
7723 else if (mips_opts.micromips
7724 && address_expr
7725 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
7726 || *reloc_type > BFD_RELOC_UNUSED)
40209cad
MR
7727 && (delayed_branch_p (ip) || compact_branch_p (ip))
7728 /* Don't try branch relaxation when users specify
7729 16-bit/32-bit instructions. */
7730 && !forced_insn_length)
df58fc94 7731 {
7bd374a4
MR
7732 bfd_boolean relax16 = (method != APPEND_ADD_COMPACT
7733 && *reloc_type > BFD_RELOC_UNUSED);
df58fc94 7734 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
11625dd8 7735 int uncond = uncond_branch_p (ip) ? -1 : 0;
7bd374a4
MR
7736 int compact = compact_branch_p (ip) || method == APPEND_ADD_COMPACT;
7737 int nods = method == APPEND_ADD_WITH_NOP;
df58fc94 7738 int al = pinfo & INSN_WRITE_GPR_31;
7bd374a4 7739 int length32 = nods ? 8 : 4;
df58fc94
RS
7740
7741 gas_assert (address_expr != NULL);
7742 gas_assert (!mips_relax.sequence);
7743
2b0c8b40 7744 relaxed_branch = TRUE;
7bd374a4
MR
7745 if (nods)
7746 method = APPEND_ADD;
7747 if (relax32)
7748 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
7749 add_relaxed_insn (ip, length32, relax16 ? 2 : 4,
8484fb75 7750 RELAX_MICROMIPS_ENCODE (type, AT, mips_opts.insn32,
ce8ad872 7751 mips_pic != NO_PIC,
7bd374a4 7752 uncond, compact, al, nods,
40209cad 7753 relax32, 0, 0),
df58fc94
RS
7754 address_expr->X_add_symbol,
7755 address_expr->X_add_number);
7756 *reloc_type = BFD_RELOC_UNUSED;
7757 }
7758 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
252b5132 7759 {
7fd53920
MR
7760 bfd_boolean require_unextended;
7761 bfd_boolean require_extended;
88a7ef16
MR
7762 symbolS *symbol;
7763 offsetT offset;
7764
7fd53920
MR
7765 if (forced_insn_length != 0)
7766 {
7767 require_unextended = forced_insn_length == 2;
7768 require_extended = forced_insn_length == 4;
7769 }
7770 else
7771 {
7772 require_unextended = (mips_opts.noautoextend
7773 && !mips_opcode_32bit_p (ip->insn_mo));
7774 require_extended = 0;
7775 }
7776
252b5132 7777 /* We need to set up a variant frag. */
df58fc94 7778 gas_assert (address_expr != NULL);
88a7ef16
MR
7779 /* Pass any `O_symbol' expression unchanged as an `expr_section'
7780 symbol created by `make_expr_symbol' may not get a necessary
7781 external relocation produced. */
7782 if (address_expr->X_op == O_symbol)
7783 {
7784 symbol = address_expr->X_add_symbol;
7785 offset = address_expr->X_add_number;
7786 }
7787 else
7788 {
7789 symbol = make_expr_symbol (address_expr);
82d808ed 7790 symbol_append (symbol, symbol_lastP, &symbol_rootP, &symbol_lastP);
88a7ef16
MR
7791 offset = 0;
7792 }
8507b6e7 7793 add_relaxed_insn (ip, 12, 0,
1e915849
RS
7794 RELAX_MIPS16_ENCODE
7795 (*reloc_type - BFD_RELOC_UNUSED,
25499ac7 7796 mips_opts.ase & ASE_MIPS16E2,
8507b6e7
MR
7797 mips_pic != NO_PIC,
7798 HAVE_32BIT_SYMBOLS,
7799 mips_opts.warn_about_macros,
7fd53920 7800 require_unextended, require_extended,
11625dd8 7801 delayed_branch_p (&history[0]),
1e915849 7802 history[0].mips16_absolute_jump_p),
88a7ef16 7803 symbol, offset);
252b5132 7804 }
5c04167a 7805 else if (mips_opts.mips16 && insn_length (ip) == 2)
9497f5ac 7806 {
11625dd8 7807 if (!delayed_branch_p (ip))
b8ee1a6e
DU
7808 /* Make sure there is enough room to swap this instruction with
7809 a following jump instruction. */
7810 frag_grow (6);
1e915849 7811 add_fixed_insn (ip);
252b5132
RH
7812 }
7813 else
7814 {
7815 if (mips_opts.mips16
7816 && mips_opts.noreorder
11625dd8 7817 && delayed_branch_p (&history[0]))
252b5132
RH
7818 as_warn (_("extended instruction in delay slot"));
7819
4d7206a2
RS
7820 if (mips_relax.sequence)
7821 {
7822 /* If we've reached the end of this frag, turn it into a variant
7823 frag and record the information for the instructions we've
7824 written so far. */
7825 if (frag_room () < 4)
7826 relax_close_frag ();
df58fc94 7827 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4d7206a2
RS
7828 }
7829
584892a6 7830 if (mips_relax.sequence != 2)
df58fc94
RS
7831 {
7832 if (mips_macro_warning.first_insn_sizes[0] == 0)
7833 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
7834 mips_macro_warning.sizes[0] += insn_length (ip);
7835 mips_macro_warning.insns[0]++;
7836 }
584892a6 7837 if (mips_relax.sequence != 1)
df58fc94
RS
7838 {
7839 if (mips_macro_warning.first_insn_sizes[1] == 0)
7840 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
7841 mips_macro_warning.sizes[1] += insn_length (ip);
7842 mips_macro_warning.insns[1]++;
7843 }
584892a6 7844
1e915849
RS
7845 if (mips_opts.mips16)
7846 {
7847 ip->fixed_p = 1;
7848 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
7849 }
7850 add_fixed_insn (ip);
252b5132
RH
7851 }
7852
9fe77896 7853 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
252b5132 7854 {
df58fc94 7855 bfd_reloc_code_real_type final_type[3];
2309ddf2 7856 reloc_howto_type *howto0;
9fe77896
RS
7857 reloc_howto_type *howto;
7858 int i;
34ce925e 7859
df58fc94
RS
7860 /* Perform any necessary conversion to microMIPS relocations
7861 and find out how many relocations there actually are. */
7862 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
7863 final_type[i] = micromips_map_reloc (reloc_type[i]);
7864
9fe77896
RS
7865 /* In a compound relocation, it is the final (outermost)
7866 operator that determines the relocated field. */
2309ddf2 7867 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
e8044f35
RS
7868 if (!howto)
7869 abort ();
2309ddf2
MR
7870
7871 if (i > 1)
7872 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
9fe77896
RS
7873 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
7874 bfd_get_reloc_size (howto),
7875 address_expr,
2309ddf2
MR
7876 howto0 && howto0->pc_relative,
7877 final_type[0]);
ce8ad872
MR
7878 /* Record non-PIC mode in `fx_tcbit2' for `md_apply_fix'. */
7879 ip->fixp[0]->fx_tcbit2 = mips_pic == NO_PIC;
9fe77896
RS
7880
7881 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
2309ddf2 7882 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
9fe77896
RS
7883 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
7884
7885 /* These relocations can have an addend that won't fit in
7886 4 octets for 64bit assembly. */
bad1aba3 7887 if (GPR_SIZE == 64
9fe77896
RS
7888 && ! howto->partial_inplace
7889 && (reloc_type[0] == BFD_RELOC_16
7890 || reloc_type[0] == BFD_RELOC_32
7891 || reloc_type[0] == BFD_RELOC_MIPS_JMP
7892 || reloc_type[0] == BFD_RELOC_GPREL16
7893 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
7894 || reloc_type[0] == BFD_RELOC_GPREL32
7895 || reloc_type[0] == BFD_RELOC_64
7896 || reloc_type[0] == BFD_RELOC_CTOR
7897 || reloc_type[0] == BFD_RELOC_MIPS_SUB
7898 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
7899 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
7900 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
7901 || reloc_type[0] == BFD_RELOC_MIPS_REL16
7902 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
7903 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
7904 || hi16_reloc_p (reloc_type[0])
7905 || lo16_reloc_p (reloc_type[0])))
7906 ip->fixp[0]->fx_no_overflow = 1;
7907
ddaf2c41
MR
7908 /* These relocations can have an addend that won't fit in 2 octets. */
7909 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
7910 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
7911 ip->fixp[0]->fx_no_overflow = 1;
7912
9fe77896
RS
7913 if (mips_relax.sequence)
7914 {
7915 if (mips_relax.first_fixup == 0)
7916 mips_relax.first_fixup = ip->fixp[0];
7917 }
7918 else if (reloc_needs_lo_p (*reloc_type))
7919 {
7920 struct mips_hi_fixup *hi_fixup;
7921
7922 /* Reuse the last entry if it already has a matching %lo. */
7923 hi_fixup = mips_hi_fixup_list;
7924 if (hi_fixup == 0
7925 || !fixup_has_matching_lo_p (hi_fixup->fixp))
4d7206a2 7926 {
325801bd 7927 hi_fixup = XNEW (struct mips_hi_fixup);
9fe77896
RS
7928 hi_fixup->next = mips_hi_fixup_list;
7929 mips_hi_fixup_list = hi_fixup;
4d7206a2 7930 }
9fe77896
RS
7931 hi_fixup->fixp = ip->fixp[0];
7932 hi_fixup->seg = now_seg;
7933 }
252b5132 7934
9fe77896
RS
7935 /* Add fixups for the second and third relocations, if given.
7936 Note that the ABI allows the second relocation to be
7937 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
7938 moment we only use RSS_UNDEF, but we could add support
7939 for the others if it ever becomes necessary. */
7940 for (i = 1; i < 3; i++)
7941 if (reloc_type[i] != BFD_RELOC_UNUSED)
7942 {
7943 ip->fixp[i] = fix_new (ip->frag, ip->where,
7944 ip->fixp[0]->fx_size, NULL, 0,
df58fc94 7945 FALSE, final_type[i]);
f6688943 7946
9fe77896
RS
7947 /* Use fx_tcbit to mark compound relocs. */
7948 ip->fixp[0]->fx_tcbit = 1;
7949 ip->fixp[i]->fx_tcbit = 1;
7950 }
252b5132 7951 }
252b5132
RH
7952
7953 /* Update the register mask information. */
4c260379
RS
7954 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
7955 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
252b5132 7956
a4e06468 7957 switch (method)
252b5132 7958 {
a4e06468
RS
7959 case APPEND_ADD:
7960 insert_into_history (0, 1, ip);
7961 break;
7962
7963 case APPEND_ADD_WITH_NOP:
14fe068b
RS
7964 {
7965 struct mips_cl_insn *nop;
7966
7967 insert_into_history (0, 1, ip);
7968 nop = get_delay_slot_nop (ip);
7969 add_fixed_insn (nop);
7970 insert_into_history (0, 1, nop);
7971 if (mips_relax.sequence)
7972 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
7973 }
a4e06468
RS
7974 break;
7975
7976 case APPEND_ADD_COMPACT:
7977 /* Convert MIPS16 jr/jalr into a "compact" jump. */
7bd374a4
MR
7978 if (mips_opts.mips16)
7979 {
7980 ip->insn_opcode |= 0x0080;
7981 find_altered_mips16_opcode (ip);
7982 }
7983 /* Convert microMIPS instructions. */
7984 else if (mips_opts.micromips)
7985 {
7986 /* jr16->jrc */
7987 if ((ip->insn_opcode & 0xffe0) == 0x4580)
7988 ip->insn_opcode |= 0x0020;
7989 /* b16->bc */
7990 else if ((ip->insn_opcode & 0xfc00) == 0xcc00)
7991 ip->insn_opcode = 0x40e00000;
7992 /* beqz16->beqzc, bnez16->bnezc */
7993 else if ((ip->insn_opcode & 0xdc00) == 0x8c00)
7994 {
7995 unsigned long regno;
7996
7997 regno = ip->insn_opcode >> MICROMIPSOP_SH_MD;
7998 regno &= MICROMIPSOP_MASK_MD;
7999 regno = micromips_to_32_reg_d_map[regno];
8000 ip->insn_opcode = (((ip->insn_opcode << 9) & 0x00400000)
8001 | (regno << MICROMIPSOP_SH_RS)
8002 | 0x40a00000) ^ 0x00400000;
8003 }
8004 /* beqz->beqzc, bnez->bnezc */
8005 else if ((ip->insn_opcode & 0xdfe00000) == 0x94000000)
8006 ip->insn_opcode = ((ip->insn_opcode & 0x001f0000)
8007 | ((ip->insn_opcode >> 7) & 0x00400000)
8008 | 0x40a00000) ^ 0x00400000;
8009 /* beq $0->beqzc, bne $0->bnezc */
8010 else if ((ip->insn_opcode & 0xdc1f0000) == 0x94000000)
8011 ip->insn_opcode = (((ip->insn_opcode >>
8012 (MICROMIPSOP_SH_RT - MICROMIPSOP_SH_RS))
8013 & (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS))
8014 | ((ip->insn_opcode >> 7) & 0x00400000)
8015 | 0x40a00000) ^ 0x00400000;
8016 else
8017 abort ();
8018 find_altered_micromips_opcode (ip);
8019 }
8020 else
8021 abort ();
a4e06468
RS
8022 install_insn (ip);
8023 insert_into_history (0, 1, ip);
8024 break;
8025
8026 case APPEND_SWAP:
8027 {
8028 struct mips_cl_insn delay = history[0];
99e7978b
MF
8029
8030 if (relaxed_branch || delay.frag != ip->frag)
a4e06468
RS
8031 {
8032 /* Add the delay slot instruction to the end of the
8033 current frag and shrink the fixed part of the
8034 original frag. If the branch occupies the tail of
8035 the latter, move it backwards to cover the gap. */
2b0c8b40 8036 delay.frag->fr_fix -= branch_disp;
a4e06468 8037 if (delay.frag == ip->frag)
2b0c8b40 8038 move_insn (ip, ip->frag, ip->where - branch_disp);
a4e06468
RS
8039 add_fixed_insn (&delay);
8040 }
8041 else
8042 {
5e35670b
MR
8043 /* If this is not a relaxed branch and we are in the
8044 same frag, then just swap the instructions. */
8045 move_insn (ip, delay.frag, delay.where);
8046 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
a4e06468
RS
8047 }
8048 history[0] = *ip;
8049 delay.fixed_p = 1;
8050 insert_into_history (0, 1, &delay);
8051 }
8052 break;
252b5132
RH
8053 }
8054
13408f1e 8055 /* If we have just completed an unconditional branch, clear the history. */
11625dd8
RS
8056 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
8057 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
e407c74b
NC
8058 {
8059 unsigned int i;
8060
79850f26 8061 mips_no_prev_insn ();
13408f1e 8062
e407c74b 8063 for (i = 0; i < ARRAY_SIZE (history); i++)
79850f26 8064 history[i].cleared_p = 1;
e407c74b
NC
8065 }
8066
df58fc94
RS
8067 /* We need to emit a label at the end of branch-likely macros. */
8068 if (emit_branch_likely_macro)
8069 {
8070 emit_branch_likely_macro = FALSE;
8071 micromips_add_label ();
8072 }
8073
252b5132
RH
8074 /* We just output an insn, so the next one doesn't have a label. */
8075 mips_clear_insn_labels ();
252b5132
RH
8076}
8077
e407c74b
NC
8078/* Forget that there was any previous instruction or label.
8079 When BRANCH is true, the branch history is also flushed. */
252b5132
RH
8080
8081static void
7d10b47d 8082mips_no_prev_insn (void)
252b5132 8083{
7d10b47d
RS
8084 prev_nop_frag = NULL;
8085 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
8086 mips_clear_insn_labels ();
8087}
8088
7d10b47d
RS
8089/* This function must be called before we emit something other than
8090 instructions. It is like mips_no_prev_insn except that it inserts
8091 any NOPS that might be needed by previous instructions. */
252b5132 8092
7d10b47d
RS
8093void
8094mips_emit_delays (void)
252b5132
RH
8095{
8096 if (! mips_opts.noreorder)
8097 {
932d1a1b 8098 int nops = nops_for_insn (0, history, NULL);
252b5132
RH
8099 if (nops > 0)
8100 {
7d10b47d
RS
8101 while (nops-- > 0)
8102 add_fixed_insn (NOP_INSN);
462427c4 8103 mips_move_text_labels ();
7d10b47d
RS
8104 }
8105 }
8106 mips_no_prev_insn ();
8107}
8108
8109/* Start a (possibly nested) noreorder block. */
8110
8111static void
8112start_noreorder (void)
8113{
8114 if (mips_opts.noreorder == 0)
8115 {
8116 unsigned int i;
8117 int nops;
8118
8119 /* None of the instructions before the .set noreorder can be moved. */
8120 for (i = 0; i < ARRAY_SIZE (history); i++)
8121 history[i].fixed_p = 1;
8122
8123 /* Insert any nops that might be needed between the .set noreorder
8124 block and the previous instructions. We will later remove any
8125 nops that turn out not to be needed. */
932d1a1b 8126 nops = nops_for_insn (0, history, NULL);
7d10b47d
RS
8127 if (nops > 0)
8128 {
8129 if (mips_optimize != 0)
252b5132
RH
8130 {
8131 /* Record the frag which holds the nop instructions, so
8132 that we can remove them if we don't need them. */
df58fc94 8133 frag_grow (nops * NOP_INSN_SIZE);
252b5132
RH
8134 prev_nop_frag = frag_now;
8135 prev_nop_frag_holds = nops;
8136 prev_nop_frag_required = 0;
8137 prev_nop_frag_since = 0;
8138 }
8139
8140 for (; nops > 0; --nops)
1e915849 8141 add_fixed_insn (NOP_INSN);
252b5132 8142
7d10b47d
RS
8143 /* Move on to a new frag, so that it is safe to simply
8144 decrease the size of prev_nop_frag. */
8145 frag_wane (frag_now);
8146 frag_new (0);
462427c4 8147 mips_move_text_labels ();
252b5132 8148 }
df58fc94 8149 mips_mark_labels ();
7d10b47d 8150 mips_clear_insn_labels ();
252b5132 8151 }
7d10b47d
RS
8152 mips_opts.noreorder++;
8153 mips_any_noreorder = 1;
8154}
252b5132 8155
7d10b47d 8156/* End a nested noreorder block. */
252b5132 8157
7d10b47d
RS
8158static void
8159end_noreorder (void)
8160{
8161 mips_opts.noreorder--;
8162 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
8163 {
8164 /* Commit to inserting prev_nop_frag_required nops and go back to
8165 handling nop insertion the .set reorder way. */
8166 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
df58fc94 8167 * NOP_INSN_SIZE);
7d10b47d
RS
8168 insert_into_history (prev_nop_frag_since,
8169 prev_nop_frag_required, NOP_INSN);
8170 prev_nop_frag = NULL;
8171 }
252b5132
RH
8172}
8173
97d87491
RS
8174/* Sign-extend 32-bit mode constants that have bit 31 set and all
8175 higher bits unset. */
8176
8177static void
8178normalize_constant_expr (expressionS *ex)
8179{
8180 if (ex->X_op == O_constant
8181 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
8182 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
8183 - 0x80000000);
8184}
8185
8186/* Sign-extend 32-bit mode address offsets that have bit 31 set and
8187 all higher bits unset. */
8188
8189static void
8190normalize_address_expr (expressionS *ex)
8191{
8192 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
8193 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
8194 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
8195 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
8196 - 0x80000000);
8197}
8198
8199/* Try to match TOKENS against OPCODE, storing the result in INSN.
8200 Return true if the match was successful.
8201
8202 OPCODE_EXTRA is a value that should be ORed into the opcode
8203 (used for VU0 channel suffixes, etc.). MORE_ALTS is true if
8204 there are more alternatives after OPCODE and SOFT_MATCH is
8205 as for mips_arg_info. */
8206
8207static bfd_boolean
8208match_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
8209 struct mips_operand_token *tokens, unsigned int opcode_extra,
60f20e8b 8210 bfd_boolean lax_match, bfd_boolean complete_p)
97d87491
RS
8211{
8212 const char *args;
8213 struct mips_arg_info arg;
8214 const struct mips_operand *operand;
8215 char c;
8216
8217 imm_expr.X_op = O_absent;
97d87491
RS
8218 offset_expr.X_op = O_absent;
8219 offset_reloc[0] = BFD_RELOC_UNUSED;
8220 offset_reloc[1] = BFD_RELOC_UNUSED;
8221 offset_reloc[2] = BFD_RELOC_UNUSED;
8222
8223 create_insn (insn, opcode);
60f20e8b
RS
8224 /* When no opcode suffix is specified, assume ".xyzw". */
8225 if ((opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0 && opcode_extra == 0)
8226 insn->insn_opcode |= 0xf << mips_vu0_channel_mask.lsb;
8227 else
8228 insn->insn_opcode |= opcode_extra;
97d87491
RS
8229 memset (&arg, 0, sizeof (arg));
8230 arg.insn = insn;
8231 arg.token = tokens;
8232 arg.argnum = 1;
8233 arg.last_regno = ILLEGAL_REG;
8234 arg.dest_regno = ILLEGAL_REG;
60f20e8b 8235 arg.lax_match = lax_match;
97d87491
RS
8236 for (args = opcode->args;; ++args)
8237 {
8238 if (arg.token->type == OT_END)
8239 {
8240 /* Handle unary instructions in which only one operand is given.
8241 The source is then the same as the destination. */
8242 if (arg.opnum == 1 && *args == ',')
8243 {
8244 operand = (mips_opts.micromips
8245 ? decode_micromips_operand (args + 1)
8246 : decode_mips_operand (args + 1));
8247 if (operand && mips_optional_operand_p (operand))
8248 {
8249 arg.token = tokens;
8250 arg.argnum = 1;
8251 continue;
8252 }
8253 }
8254
8255 /* Treat elided base registers as $0. */
8256 if (strcmp (args, "(b)") == 0)
8257 args += 3;
8258
8259 if (args[0] == '+')
8260 switch (args[1])
8261 {
8262 case 'K':
8263 case 'N':
8264 /* The register suffix is optional. */
8265 args += 2;
8266 break;
8267 }
8268
8269 /* Fail the match if there were too few operands. */
8270 if (*args)
8271 return FALSE;
8272
8273 /* Successful match. */
60f20e8b
RS
8274 if (!complete_p)
8275 return TRUE;
e3de51ce 8276 clear_insn_error ();
97d87491
RS
8277 if (arg.dest_regno == arg.last_regno
8278 && strncmp (insn->insn_mo->name, "jalr", 4) == 0)
8279 {
8280 if (arg.opnum == 2)
e3de51ce 8281 set_insn_error
1661c76c 8282 (0, _("source and destination must be different"));
97d87491 8283 else if (arg.last_regno == 31)
e3de51ce 8284 set_insn_error
1661c76c 8285 (0, _("a destination register must be supplied"));
97d87491 8286 }
173d3447
CF
8287 else if (arg.last_regno == 31
8288 && (strncmp (insn->insn_mo->name, "bltzal", 6) == 0
8289 || strncmp (insn->insn_mo->name, "bgezal", 6) == 0))
8290 set_insn_error (0, _("the source register must not be $31"));
97d87491
RS
8291 check_completed_insn (&arg);
8292 return TRUE;
8293 }
8294
8295 /* Fail the match if the line has too many operands. */
8296 if (*args == 0)
8297 return FALSE;
8298
8299 /* Handle characters that need to match exactly. */
8300 if (*args == '(' || *args == ')' || *args == ',')
8301 {
8302 if (match_char (&arg, *args))
8303 continue;
8304 return FALSE;
8305 }
8306 if (*args == '#')
8307 {
8308 ++args;
8309 if (arg.token->type == OT_DOUBLE_CHAR
8310 && arg.token->u.ch == *args)
8311 {
8312 ++arg.token;
8313 continue;
8314 }
8315 return FALSE;
8316 }
8317
8318 /* Handle special macro operands. Work out the properties of
8319 other operands. */
8320 arg.opnum += 1;
97d87491
RS
8321 switch (*args)
8322 {
7361da2c
AB
8323 case '-':
8324 switch (args[1])
8325 {
8326 case 'A':
8327 *offset_reloc = BFD_RELOC_MIPS_19_PCREL_S2;
8328 break;
8329
8330 case 'B':
8331 *offset_reloc = BFD_RELOC_MIPS_18_PCREL_S3;
8332 break;
8333 }
8334 break;
8335
97d87491
RS
8336 case '+':
8337 switch (args[1])
8338 {
97d87491
RS
8339 case 'i':
8340 *offset_reloc = BFD_RELOC_MIPS_JMP;
8341 break;
7361da2c
AB
8342
8343 case '\'':
8344 *offset_reloc = BFD_RELOC_MIPS_26_PCREL_S2;
8345 break;
8346
8347 case '\"':
8348 *offset_reloc = BFD_RELOC_MIPS_21_PCREL_S2;
8349 break;
97d87491
RS
8350 }
8351 break;
8352
97d87491 8353 case 'I':
1a00e612
RS
8354 if (!match_const_int (&arg, &imm_expr.X_add_number))
8355 return FALSE;
8356 imm_expr.X_op = O_constant;
bad1aba3 8357 if (GPR_SIZE == 32)
97d87491
RS
8358 normalize_constant_expr (&imm_expr);
8359 continue;
8360
8361 case 'A':
8362 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8363 {
8364 /* Assume that the offset has been elided and that what
8365 we saw was a base register. The match will fail later
8366 if that assumption turns out to be wrong. */
8367 offset_expr.X_op = O_constant;
8368 offset_expr.X_add_number = 0;
8369 }
97d87491 8370 else
1a00e612
RS
8371 {
8372 if (!match_expression (&arg, &offset_expr, offset_reloc))
8373 return FALSE;
8374 normalize_address_expr (&offset_expr);
8375 }
97d87491
RS
8376 continue;
8377
8378 case 'F':
8379 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8380 8, TRUE))
1a00e612 8381 return FALSE;
97d87491
RS
8382 continue;
8383
8384 case 'L':
8385 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8386 8, FALSE))
1a00e612 8387 return FALSE;
97d87491
RS
8388 continue;
8389
8390 case 'f':
8391 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8392 4, TRUE))
1a00e612 8393 return FALSE;
97d87491
RS
8394 continue;
8395
8396 case 'l':
8397 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8398 4, FALSE))
1a00e612 8399 return FALSE;
97d87491
RS
8400 continue;
8401
97d87491
RS
8402 case 'p':
8403 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8404 break;
8405
8406 case 'a':
8407 *offset_reloc = BFD_RELOC_MIPS_JMP;
8408 break;
8409
8410 case 'm':
8411 gas_assert (mips_opts.micromips);
8412 c = args[1];
8413 switch (c)
8414 {
8415 case 'D':
8416 case 'E':
8417 if (!forced_insn_length)
8418 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
8419 else if (c == 'D')
8420 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
8421 else
8422 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
8423 break;
8424 }
8425 break;
8426 }
8427
8428 operand = (mips_opts.micromips
8429 ? decode_micromips_operand (args)
8430 : decode_mips_operand (args));
8431 if (!operand)
8432 abort ();
8433
8434 /* Skip prefixes. */
7361da2c 8435 if (*args == '+' || *args == 'm' || *args == '-')
97d87491
RS
8436 args++;
8437
8438 if (mips_optional_operand_p (operand)
8439 && args[1] == ','
8440 && (arg.token[0].type != OT_REG
8441 || arg.token[1].type == OT_END))
8442 {
8443 /* Assume that the register has been elided and is the
8444 same as the first operand. */
8445 arg.token = tokens;
8446 arg.argnum = 1;
8447 }
8448
8449 if (!match_operand (&arg, operand))
8450 return FALSE;
8451 }
8452}
8453
8454/* Like match_insn, but for MIPS16. */
8455
8456static bfd_boolean
8457match_mips16_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
1a00e612 8458 struct mips_operand_token *tokens)
97d87491
RS
8459{
8460 const char *args;
8461 const struct mips_operand *operand;
8462 const struct mips_operand *ext_operand;
82d808ed 8463 bfd_boolean pcrel = FALSE;
7fd53920 8464 int required_insn_length;
97d87491
RS
8465 struct mips_arg_info arg;
8466 int relax_char;
8467
7fd53920
MR
8468 if (forced_insn_length)
8469 required_insn_length = forced_insn_length;
8470 else if (mips_opts.noautoextend && !mips_opcode_32bit_p (opcode))
8471 required_insn_length = 2;
8472 else
8473 required_insn_length = 0;
8474
97d87491
RS
8475 create_insn (insn, opcode);
8476 imm_expr.X_op = O_absent;
97d87491
RS
8477 offset_expr.X_op = O_absent;
8478 offset_reloc[0] = BFD_RELOC_UNUSED;
8479 offset_reloc[1] = BFD_RELOC_UNUSED;
8480 offset_reloc[2] = BFD_RELOC_UNUSED;
8481 relax_char = 0;
8482
8483 memset (&arg, 0, sizeof (arg));
8484 arg.insn = insn;
8485 arg.token = tokens;
8486 arg.argnum = 1;
8487 arg.last_regno = ILLEGAL_REG;
8488 arg.dest_regno = ILLEGAL_REG;
97d87491
RS
8489 relax_char = 0;
8490 for (args = opcode->args;; ++args)
8491 {
8492 int c;
8493
8494 if (arg.token->type == OT_END)
8495 {
8496 offsetT value;
8497
8498 /* Handle unary instructions in which only one operand is given.
8499 The source is then the same as the destination. */
8500 if (arg.opnum == 1 && *args == ',')
8501 {
8502 operand = decode_mips16_operand (args[1], FALSE);
8503 if (operand && mips_optional_operand_p (operand))
8504 {
8505 arg.token = tokens;
8506 arg.argnum = 1;
8507 continue;
8508 }
8509 }
8510
8511 /* Fail the match if there were too few operands. */
8512 if (*args)
8513 return FALSE;
8514
8515 /* Successful match. Stuff the immediate value in now, if
8516 we can. */
e3de51ce 8517 clear_insn_error ();
97d87491
RS
8518 if (opcode->pinfo == INSN_MACRO)
8519 {
8520 gas_assert (relax_char == 0 || relax_char == 'p');
8521 gas_assert (*offset_reloc == BFD_RELOC_UNUSED);
8522 }
8523 else if (relax_char
8524 && offset_expr.X_op == O_constant
82d808ed 8525 && !pcrel
97d87491
RS
8526 && calculate_reloc (*offset_reloc,
8527 offset_expr.X_add_number,
8528 &value))
8529 {
8530 mips16_immed (NULL, 0, relax_char, *offset_reloc, value,
7fd53920 8531 required_insn_length, &insn->insn_opcode);
97d87491
RS
8532 offset_expr.X_op = O_absent;
8533 *offset_reloc = BFD_RELOC_UNUSED;
8534 }
8535 else if (relax_char && *offset_reloc != BFD_RELOC_UNUSED)
8536 {
7fd53920 8537 if (required_insn_length == 2)
e3de51ce 8538 set_insn_error (0, _("invalid unextended operand value"));
25499ac7 8539 else if (!mips_opcode_32bit_p (opcode))
1da43acc
MR
8540 {
8541 forced_insn_length = 4;
8542 insn->insn_opcode |= MIPS16_EXTEND;
8543 }
97d87491
RS
8544 }
8545 else if (relax_char)
8546 *offset_reloc = (int) BFD_RELOC_UNUSED + relax_char;
8547
8548 check_completed_insn (&arg);
8549 return TRUE;
8550 }
8551
8552 /* Fail the match if the line has too many operands. */
8553 if (*args == 0)
8554 return FALSE;
8555
8556 /* Handle characters that need to match exactly. */
8557 if (*args == '(' || *args == ')' || *args == ',')
8558 {
8559 if (match_char (&arg, *args))
8560 continue;
8561 return FALSE;
8562 }
8563
8564 arg.opnum += 1;
8565 c = *args;
8566 switch (c)
8567 {
8568 case 'p':
8569 case 'q':
8570 case 'A':
8571 case 'B':
8572 case 'E':
25499ac7
MR
8573 case 'V':
8574 case 'u':
97d87491
RS
8575 relax_char = c;
8576 break;
8577
8578 case 'I':
1a00e612
RS
8579 if (!match_const_int (&arg, &imm_expr.X_add_number))
8580 return FALSE;
8581 imm_expr.X_op = O_constant;
bad1aba3 8582 if (GPR_SIZE == 32)
97d87491
RS
8583 normalize_constant_expr (&imm_expr);
8584 continue;
8585
8586 case 'a':
8587 case 'i':
8588 *offset_reloc = BFD_RELOC_MIPS16_JMP;
97d87491
RS
8589 break;
8590 }
8591
7fd53920 8592 operand = decode_mips16_operand (c, mips_opcode_32bit_p (opcode));
97d87491
RS
8593 if (!operand)
8594 abort ();
8595
82d808ed
MR
8596 if (operand->type == OP_PCREL)
8597 pcrel = TRUE;
8598 else
97d87491
RS
8599 {
8600 ext_operand = decode_mips16_operand (c, TRUE);
8601 if (operand != ext_operand)
8602 {
8603 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8604 {
8605 offset_expr.X_op = O_constant;
8606 offset_expr.X_add_number = 0;
8607 relax_char = c;
8608 continue;
8609 }
8610
1a7bf198 8611 if (!match_expression (&arg, &offset_expr, offset_reloc))
97d87491
RS
8612 return FALSE;
8613
8614 /* '8' is used for SLTI(U) and has traditionally not
8615 been allowed to take relocation operators. */
8616 if (offset_reloc[0] != BFD_RELOC_UNUSED
8617 && (ext_operand->size != 16 || c == '8'))
e295202f
MR
8618 {
8619 match_not_constant (&arg);
8620 return FALSE;
8621 }
97d87491 8622
c96425c5
MR
8623 if (offset_expr.X_op == O_big)
8624 {
8625 match_out_of_range (&arg);
8626 return FALSE;
8627 }
8628
97d87491
RS
8629 relax_char = c;
8630 continue;
8631 }
8632 }
8633
8634 if (mips_optional_operand_p (operand)
8635 && args[1] == ','
8636 && (arg.token[0].type != OT_REG
8637 || arg.token[1].type == OT_END))
8638 {
8639 /* Assume that the register has been elided and is the
8640 same as the first operand. */
8641 arg.token = tokens;
8642 arg.argnum = 1;
8643 }
8644
8645 if (!match_operand (&arg, operand))
8646 return FALSE;
8647 }
8648}
8649
60f20e8b
RS
8650/* Record that the current instruction is invalid for the current ISA. */
8651
8652static void
8653match_invalid_for_isa (void)
8654{
8655 set_insn_error_ss
1661c76c 8656 (0, _("opcode not supported on this processor: %s (%s)"),
60f20e8b
RS
8657 mips_cpu_info_from_arch (mips_opts.arch)->name,
8658 mips_cpu_info_from_isa (mips_opts.isa)->name);
8659}
8660
8661/* Try to match TOKENS against a series of opcode entries, starting at FIRST.
8662 Return true if a definite match or failure was found, storing any match
8663 in INSN. OPCODE_EXTRA is a value that should be ORed into the opcode
8664 (to handle things like VU0 suffixes). LAX_MATCH is true if we have already
8665 tried and failed to match under normal conditions and now want to try a
8666 more relaxed match. */
8667
8668static bfd_boolean
8669match_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8670 const struct mips_opcode *past, struct mips_operand_token *tokens,
8671 int opcode_extra, bfd_boolean lax_match)
8672{
8673 const struct mips_opcode *opcode;
8674 const struct mips_opcode *invalid_delay_slot;
8675 bfd_boolean seen_valid_for_isa, seen_valid_for_size;
8676
8677 /* Search for a match, ignoring alternatives that don't satisfy the
8678 current ISA or forced_length. */
8679 invalid_delay_slot = 0;
8680 seen_valid_for_isa = FALSE;
8681 seen_valid_for_size = FALSE;
8682 opcode = first;
8683 do
8684 {
8685 gas_assert (strcmp (opcode->name, first->name) == 0);
8686 if (is_opcode_valid (opcode))
8687 {
8688 seen_valid_for_isa = TRUE;
8689 if (is_size_valid (opcode))
8690 {
8691 bfd_boolean delay_slot_ok;
8692
8693 seen_valid_for_size = TRUE;
8694 delay_slot_ok = is_delay_slot_valid (opcode);
8695 if (match_insn (insn, opcode, tokens, opcode_extra,
8696 lax_match, delay_slot_ok))
8697 {
8698 if (!delay_slot_ok)
8699 {
8700 if (!invalid_delay_slot)
8701 invalid_delay_slot = opcode;
8702 }
8703 else
8704 return TRUE;
8705 }
8706 }
8707 }
8708 ++opcode;
8709 }
8710 while (opcode < past && strcmp (opcode->name, first->name) == 0);
8711
8712 /* If the only matches we found had the wrong length for the delay slot,
8713 pick the first such match. We'll issue an appropriate warning later. */
8714 if (invalid_delay_slot)
8715 {
8716 if (match_insn (insn, invalid_delay_slot, tokens, opcode_extra,
8717 lax_match, TRUE))
8718 return TRUE;
8719 abort ();
8720 }
8721
8722 /* Handle the case where we didn't try to match an instruction because
8723 all the alternatives were incompatible with the current ISA. */
8724 if (!seen_valid_for_isa)
8725 {
8726 match_invalid_for_isa ();
8727 return TRUE;
8728 }
8729
8730 /* Handle the case where we didn't try to match an instruction because
8731 all the alternatives were of the wrong size. */
8732 if (!seen_valid_for_size)
8733 {
8734 if (mips_opts.insn32)
1661c76c 8735 set_insn_error (0, _("opcode not supported in the `insn32' mode"));
60f20e8b
RS
8736 else
8737 set_insn_error_i
1661c76c 8738 (0, _("unrecognized %d-bit version of microMIPS opcode"),
60f20e8b
RS
8739 8 * forced_insn_length);
8740 return TRUE;
8741 }
8742
8743 return FALSE;
8744}
8745
8746/* Like match_insns, but for MIPS16. */
8747
8748static bfd_boolean
8749match_mips16_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8750 struct mips_operand_token *tokens)
8751{
8752 const struct mips_opcode *opcode;
8753 bfd_boolean seen_valid_for_isa;
7fd53920 8754 bfd_boolean seen_valid_for_size;
60f20e8b
RS
8755
8756 /* Search for a match, ignoring alternatives that don't satisfy the
8757 current ISA. There are no separate entries for extended forms so
8758 we deal with forced_length later. */
8759 seen_valid_for_isa = FALSE;
7fd53920 8760 seen_valid_for_size = FALSE;
60f20e8b
RS
8761 opcode = first;
8762 do
8763 {
8764 gas_assert (strcmp (opcode->name, first->name) == 0);
8765 if (is_opcode_valid_16 (opcode))
8766 {
8767 seen_valid_for_isa = TRUE;
7fd53920
MR
8768 if (is_size_valid_16 (opcode))
8769 {
8770 seen_valid_for_size = TRUE;
8771 if (match_mips16_insn (insn, opcode, tokens))
8772 return TRUE;
8773 }
60f20e8b
RS
8774 }
8775 ++opcode;
8776 }
8777 while (opcode < &mips16_opcodes[bfd_mips16_num_opcodes]
8778 && strcmp (opcode->name, first->name) == 0);
8779
8780 /* Handle the case where we didn't try to match an instruction because
8781 all the alternatives were incompatible with the current ISA. */
8782 if (!seen_valid_for_isa)
8783 {
8784 match_invalid_for_isa ();
8785 return TRUE;
8786 }
8787
7fd53920
MR
8788 /* Handle the case where we didn't try to match an instruction because
8789 all the alternatives were of the wrong size. */
8790 if (!seen_valid_for_size)
8791 {
8792 if (forced_insn_length == 2)
8793 set_insn_error
8794 (0, _("unrecognized unextended version of MIPS16 opcode"));
8795 else
8796 set_insn_error
8797 (0, _("unrecognized extended version of MIPS16 opcode"));
8798 return TRUE;
8799 }
8800
60f20e8b
RS
8801 return FALSE;
8802}
8803
584892a6
RS
8804/* Set up global variables for the start of a new macro. */
8805
8806static void
8807macro_start (void)
8808{
8809 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
df58fc94
RS
8810 memset (&mips_macro_warning.first_insn_sizes, 0,
8811 sizeof (mips_macro_warning.first_insn_sizes));
8812 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
584892a6 8813 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
11625dd8 8814 && delayed_branch_p (&history[0]));
7bd374a4
MR
8815 if (history[0].frag
8816 && history[0].frag->fr_type == rs_machine_dependent
8817 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
8818 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
8819 mips_macro_warning.delay_slot_length = 0;
8820 else
8821 switch (history[0].insn_mo->pinfo2
8822 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
8823 {
8824 case INSN2_BRANCH_DELAY_32BIT:
8825 mips_macro_warning.delay_slot_length = 4;
8826 break;
8827 case INSN2_BRANCH_DELAY_16BIT:
8828 mips_macro_warning.delay_slot_length = 2;
8829 break;
8830 default:
8831 mips_macro_warning.delay_slot_length = 0;
8832 break;
8833 }
df58fc94 8834 mips_macro_warning.first_frag = NULL;
584892a6
RS
8835}
8836
df58fc94
RS
8837/* Given that a macro is longer than one instruction or of the wrong size,
8838 return the appropriate warning for it. Return null if no warning is
8839 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
8840 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
8841 and RELAX_NOMACRO. */
584892a6
RS
8842
8843static const char *
8844macro_warning (relax_substateT subtype)
8845{
8846 if (subtype & RELAX_DELAY_SLOT)
1661c76c 8847 return _("macro instruction expanded into multiple instructions"
584892a6
RS
8848 " in a branch delay slot");
8849 else if (subtype & RELAX_NOMACRO)
1661c76c 8850 return _("macro instruction expanded into multiple instructions");
df58fc94
RS
8851 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
8852 | RELAX_DELAY_SLOT_SIZE_SECOND))
8853 return ((subtype & RELAX_DELAY_SLOT_16BIT)
1661c76c 8854 ? _("macro instruction expanded into a wrong size instruction"
df58fc94 8855 " in a 16-bit branch delay slot")
1661c76c 8856 : _("macro instruction expanded into a wrong size instruction"
df58fc94 8857 " in a 32-bit branch delay slot"));
584892a6
RS
8858 else
8859 return 0;
8860}
8861
8862/* Finish up a macro. Emit warnings as appropriate. */
8863
8864static void
8865macro_end (void)
8866{
df58fc94
RS
8867 /* Relaxation warning flags. */
8868 relax_substateT subtype = 0;
8869
8870 /* Check delay slot size requirements. */
8871 if (mips_macro_warning.delay_slot_length == 2)
8872 subtype |= RELAX_DELAY_SLOT_16BIT;
8873 if (mips_macro_warning.delay_slot_length != 0)
584892a6 8874 {
df58fc94
RS
8875 if (mips_macro_warning.delay_slot_length
8876 != mips_macro_warning.first_insn_sizes[0])
8877 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
8878 if (mips_macro_warning.delay_slot_length
8879 != mips_macro_warning.first_insn_sizes[1])
8880 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
8881 }
584892a6 8882
df58fc94
RS
8883 /* Check instruction count requirements. */
8884 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
8885 {
8886 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
584892a6
RS
8887 subtype |= RELAX_SECOND_LONGER;
8888 if (mips_opts.warn_about_macros)
8889 subtype |= RELAX_NOMACRO;
8890 if (mips_macro_warning.delay_slot_p)
8891 subtype |= RELAX_DELAY_SLOT;
df58fc94 8892 }
584892a6 8893
df58fc94
RS
8894 /* If both alternatives fail to fill a delay slot correctly,
8895 emit the warning now. */
8896 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
8897 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
8898 {
8899 relax_substateT s;
8900 const char *msg;
8901
8902 s = subtype & (RELAX_DELAY_SLOT_16BIT
8903 | RELAX_DELAY_SLOT_SIZE_FIRST
8904 | RELAX_DELAY_SLOT_SIZE_SECOND);
8905 msg = macro_warning (s);
8906 if (msg != NULL)
8907 as_warn ("%s", msg);
8908 subtype &= ~s;
8909 }
8910
8911 /* If both implementations are longer than 1 instruction, then emit the
8912 warning now. */
8913 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
8914 {
8915 relax_substateT s;
8916 const char *msg;
8917
8918 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
8919 msg = macro_warning (s);
8920 if (msg != NULL)
8921 as_warn ("%s", msg);
8922 subtype &= ~s;
584892a6 8923 }
df58fc94
RS
8924
8925 /* If any flags still set, then one implementation might need a warning
8926 and the other either will need one of a different kind or none at all.
8927 Pass any remaining flags over to relaxation. */
8928 if (mips_macro_warning.first_frag != NULL)
8929 mips_macro_warning.first_frag->fr_subtype |= subtype;
584892a6
RS
8930}
8931
df58fc94
RS
8932/* Instruction operand formats used in macros that vary between
8933 standard MIPS and microMIPS code. */
8934
833794fc 8935static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
df58fc94
RS
8936static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
8937static const char * const jalr_fmt[2] = { "d,s", "t,s" };
8938static const char * const lui_fmt[2] = { "t,u", "s,u" };
8939static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
833794fc 8940static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
df58fc94
RS
8941static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
8942static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
8943
833794fc 8944#define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
7361da2c
AB
8945#define COP12_FMT (ISA_IS_R6 (mips_opts.isa) ? "E,+:(d)" \
8946 : cop12_fmt[mips_opts.micromips])
df58fc94
RS
8947#define JALR_FMT (jalr_fmt[mips_opts.micromips])
8948#define LUI_FMT (lui_fmt[mips_opts.micromips])
8949#define MEM12_FMT (mem12_fmt[mips_opts.micromips])
7361da2c
AB
8950#define LL_SC_FMT (ISA_IS_R6 (mips_opts.isa) ? "t,+j(b)" \
8951 : mem12_fmt[mips_opts.micromips])
833794fc 8952#define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
df58fc94
RS
8953#define SHFT_FMT (shft_fmt[mips_opts.micromips])
8954#define TRAP_FMT (trap_fmt[mips_opts.micromips])
8955
6e1304d8
RS
8956/* Read a macro's relocation codes from *ARGS and store them in *R.
8957 The first argument in *ARGS will be either the code for a single
8958 relocation or -1 followed by the three codes that make up a
8959 composite relocation. */
8960
8961static void
8962macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
8963{
8964 int i, next;
8965
8966 next = va_arg (*args, int);
8967 if (next >= 0)
8968 r[0] = (bfd_reloc_code_real_type) next;
8969 else
f2ae14a1
RS
8970 {
8971 for (i = 0; i < 3; i++)
8972 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
8973 /* This function is only used for 16-bit relocation fields.
8974 To make the macro code simpler, treat an unrelocated value
8975 in the same way as BFD_RELOC_LO16. */
8976 if (r[0] == BFD_RELOC_UNUSED)
8977 r[0] = BFD_RELOC_LO16;
8978 }
6e1304d8
RS
8979}
8980
252b5132
RH
8981/* Build an instruction created by a macro expansion. This is passed
8982 a pointer to the count of instructions created so far, an
8983 expression, the name of the instruction to build, an operand format
8984 string, and corresponding arguments. */
8985
252b5132 8986static void
67c0d1eb 8987macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 8988{
df58fc94 8989 const struct mips_opcode *mo = NULL;
f6688943 8990 bfd_reloc_code_real_type r[3];
df58fc94 8991 const struct mips_opcode *amo;
e077a1c8 8992 const struct mips_operand *operand;
df58fc94
RS
8993 struct hash_control *hash;
8994 struct mips_cl_insn insn;
252b5132 8995 va_list args;
e077a1c8 8996 unsigned int uval;
252b5132 8997
252b5132 8998 va_start (args, fmt);
252b5132 8999
252b5132
RH
9000 if (mips_opts.mips16)
9001 {
03ea81db 9002 mips16_macro_build (ep, name, fmt, &args);
252b5132
RH
9003 va_end (args);
9004 return;
9005 }
9006
f6688943
TS
9007 r[0] = BFD_RELOC_UNUSED;
9008 r[1] = BFD_RELOC_UNUSED;
9009 r[2] = BFD_RELOC_UNUSED;
df58fc94
RS
9010 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
9011 amo = (struct mips_opcode *) hash_find (hash, name);
9012 gas_assert (amo);
9013 gas_assert (strcmp (name, amo->name) == 0);
1e915849 9014
df58fc94 9015 do
8b082fb1
TS
9016 {
9017 /* Search until we get a match for NAME. It is assumed here that
df58fc94 9018 macros will never generate MDMX, MIPS-3D, or MT instructions.
33eaf5de 9019 We try to match an instruction that fulfills the branch delay
df58fc94
RS
9020 slot instruction length requirement (if any) of the previous
9021 instruction. While doing this we record the first instruction
9022 seen that matches all the other conditions and use it anyway
9023 if the requirement cannot be met; we will issue an appropriate
9024 warning later on. */
9025 if (strcmp (fmt, amo->args) == 0
9026 && amo->pinfo != INSN_MACRO
9027 && is_opcode_valid (amo)
9028 && is_size_valid (amo))
9029 {
9030 if (is_delay_slot_valid (amo))
9031 {
9032 mo = amo;
9033 break;
9034 }
9035 else if (!mo)
9036 mo = amo;
9037 }
8b082fb1 9038
df58fc94
RS
9039 ++amo;
9040 gas_assert (amo->name);
252b5132 9041 }
df58fc94 9042 while (strcmp (name, amo->name) == 0);
252b5132 9043
df58fc94 9044 gas_assert (mo);
1e915849 9045 create_insn (&insn, mo);
e077a1c8 9046 for (; *fmt; ++fmt)
252b5132 9047 {
e077a1c8 9048 switch (*fmt)
252b5132 9049 {
252b5132
RH
9050 case ',':
9051 case '(':
9052 case ')':
252b5132 9053 case 'z':
e077a1c8 9054 break;
252b5132
RH
9055
9056 case 'i':
9057 case 'j':
6e1304d8 9058 macro_read_relocs (&args, r);
9c2799c2 9059 gas_assert (*r == BFD_RELOC_GPREL16
e391c024
RS
9060 || *r == BFD_RELOC_MIPS_HIGHER
9061 || *r == BFD_RELOC_HI16_S
9062 || *r == BFD_RELOC_LO16
14c80123
MR
9063 || *r == BFD_RELOC_MIPS_GOT_OFST
9064 || (mips_opts.micromips
9065 && (*r == BFD_RELOC_16
9066 || *r == BFD_RELOC_MIPS_GOT16
9067 || *r == BFD_RELOC_MIPS_CALL16
9068 || *r == BFD_RELOC_MIPS_GOT_HI16
9069 || *r == BFD_RELOC_MIPS_GOT_LO16
9070 || *r == BFD_RELOC_MIPS_CALL_HI16
9071 || *r == BFD_RELOC_MIPS_CALL_LO16
9072 || *r == BFD_RELOC_MIPS_SUB
9073 || *r == BFD_RELOC_MIPS_GOT_PAGE
9074 || *r == BFD_RELOC_MIPS_HIGHEST
9075 || *r == BFD_RELOC_MIPS_GOT_DISP
9076 || *r == BFD_RELOC_MIPS_TLS_GD
9077 || *r == BFD_RELOC_MIPS_TLS_LDM
9078 || *r == BFD_RELOC_MIPS_TLS_DTPREL_HI16
9079 || *r == BFD_RELOC_MIPS_TLS_DTPREL_LO16
9080 || *r == BFD_RELOC_MIPS_TLS_GOTTPREL
9081 || *r == BFD_RELOC_MIPS_TLS_TPREL_HI16
9082 || *r == BFD_RELOC_MIPS_TLS_TPREL_LO16)));
e077a1c8 9083 break;
e391c024
RS
9084
9085 case 'o':
9086 macro_read_relocs (&args, r);
e077a1c8 9087 break;
252b5132
RH
9088
9089 case 'u':
6e1304d8 9090 macro_read_relocs (&args, r);
9c2799c2 9091 gas_assert (ep != NULL
90ecf173
MR
9092 && (ep->X_op == O_constant
9093 || (ep->X_op == O_symbol
9094 && (*r == BFD_RELOC_MIPS_HIGHEST
9095 || *r == BFD_RELOC_HI16_S
9096 || *r == BFD_RELOC_HI16
9097 || *r == BFD_RELOC_GPREL16
9098 || *r == BFD_RELOC_MIPS_GOT_HI16
9099 || *r == BFD_RELOC_MIPS_CALL_HI16))));
e077a1c8 9100 break;
252b5132
RH
9101
9102 case 'p':
9c2799c2 9103 gas_assert (ep != NULL);
bad36eac 9104
252b5132
RH
9105 /*
9106 * This allows macro() to pass an immediate expression for
9107 * creating short branches without creating a symbol.
bad36eac
DJ
9108 *
9109 * We don't allow branch relaxation for these branches, as
9110 * they should only appear in ".set nomacro" anyway.
252b5132
RH
9111 */
9112 if (ep->X_op == O_constant)
9113 {
df58fc94
RS
9114 /* For microMIPS we always use relocations for branches.
9115 So we should not resolve immediate values. */
9116 gas_assert (!mips_opts.micromips);
9117
bad36eac
DJ
9118 if ((ep->X_add_number & 3) != 0)
9119 as_bad (_("branch to misaligned address (0x%lx)"),
9120 (unsigned long) ep->X_add_number);
9121 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
9122 as_bad (_("branch address range overflow (0x%lx)"),
9123 (unsigned long) ep->X_add_number);
252b5132
RH
9124 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
9125 ep = NULL;
9126 }
9127 else
0b25d3e6 9128 *r = BFD_RELOC_16_PCREL_S2;
e077a1c8 9129 break;
252b5132
RH
9130
9131 case 'a':
9c2799c2 9132 gas_assert (ep != NULL);
f6688943 9133 *r = BFD_RELOC_MIPS_JMP;
e077a1c8 9134 break;
d43b4baf 9135
252b5132 9136 default:
e077a1c8
RS
9137 operand = (mips_opts.micromips
9138 ? decode_micromips_operand (fmt)
9139 : decode_mips_operand (fmt));
9140 if (!operand)
9141 abort ();
9142
9143 uval = va_arg (args, int);
9144 if (operand->type == OP_CLO_CLZ_DEST)
9145 uval |= (uval << 5);
9146 insn_insert_operand (&insn, operand, uval);
9147
7361da2c 9148 if (*fmt == '+' || *fmt == 'm' || *fmt == '-')
e077a1c8
RS
9149 ++fmt;
9150 break;
252b5132 9151 }
252b5132
RH
9152 }
9153 va_end (args);
9c2799c2 9154 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 9155
df58fc94 9156 append_insn (&insn, ep, r, TRUE);
252b5132
RH
9157}
9158
9159static void
67c0d1eb 9160mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
03ea81db 9161 va_list *args)
252b5132 9162{
1e915849 9163 struct mips_opcode *mo;
252b5132 9164 struct mips_cl_insn insn;
e077a1c8 9165 const struct mips_operand *operand;
f6688943
TS
9166 bfd_reloc_code_real_type r[3]
9167 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 9168
1e915849 9169 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
9c2799c2
NC
9170 gas_assert (mo);
9171 gas_assert (strcmp (name, mo->name) == 0);
252b5132 9172
1e915849 9173 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 9174 {
1e915849 9175 ++mo;
9c2799c2
NC
9176 gas_assert (mo->name);
9177 gas_assert (strcmp (name, mo->name) == 0);
252b5132
RH
9178 }
9179
1e915849 9180 create_insn (&insn, mo);
e077a1c8 9181 for (; *fmt; ++fmt)
252b5132
RH
9182 {
9183 int c;
9184
e077a1c8 9185 c = *fmt;
252b5132
RH
9186 switch (c)
9187 {
252b5132
RH
9188 case ',':
9189 case '(':
9190 case ')':
e077a1c8 9191 break;
252b5132 9192
d8722d76 9193 case '.':
252b5132
RH
9194 case 'S':
9195 case 'P':
9196 case 'R':
e077a1c8 9197 break;
252b5132
RH
9198
9199 case '<':
252b5132 9200 case '5':
d8722d76 9201 case 'F':
252b5132
RH
9202 case 'H':
9203 case 'W':
9204 case 'D':
9205 case 'j':
9206 case '8':
9207 case 'V':
9208 case 'C':
9209 case 'U':
9210 case 'k':
9211 case 'K':
9212 case 'p':
9213 case 'q':
9214 {
b886a2ab
RS
9215 offsetT value;
9216
9c2799c2 9217 gas_assert (ep != NULL);
252b5132
RH
9218
9219 if (ep->X_op != O_constant)
874e8986 9220 *r = (int) BFD_RELOC_UNUSED + c;
b886a2ab 9221 else if (calculate_reloc (*r, ep->X_add_number, &value))
252b5132 9222 {
b886a2ab 9223 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
252b5132 9224 ep = NULL;
f6688943 9225 *r = BFD_RELOC_UNUSED;
252b5132
RH
9226 }
9227 }
e077a1c8 9228 break;
252b5132 9229
e077a1c8
RS
9230 default:
9231 operand = decode_mips16_operand (c, FALSE);
9232 if (!operand)
9233 abort ();
252b5132 9234
4a06e5a2 9235 insn_insert_operand (&insn, operand, va_arg (*args, int));
e077a1c8
RS
9236 break;
9237 }
252b5132
RH
9238 }
9239
9c2799c2 9240 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 9241
df58fc94 9242 append_insn (&insn, ep, r, TRUE);
252b5132
RH
9243}
9244
438c16b8
TS
9245/*
9246 * Generate a "jalr" instruction with a relocation hint to the called
9247 * function. This occurs in NewABI PIC code.
9248 */
9249static void
df58fc94 9250macro_build_jalr (expressionS *ep, int cprestore)
438c16b8 9251{
df58fc94
RS
9252 static const bfd_reloc_code_real_type jalr_relocs[2]
9253 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
9254 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
9255 const char *jalr;
685736be 9256 char *f = NULL;
b34976b6 9257
1180b5a4 9258 if (MIPS_JALR_HINT_P (ep))
f21f8242 9259 {
cc3d92a5 9260 frag_grow (8);
f21f8242
AO
9261 f = frag_more (0);
9262 }
2906b037 9263 if (mips_opts.micromips)
df58fc94 9264 {
833794fc
MR
9265 jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
9266 ? "jalr" : "jalrs");
e64af278 9267 if (MIPS_JALR_HINT_P (ep)
833794fc 9268 || mips_opts.insn32
e64af278 9269 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
9270 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
9271 else
9272 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
9273 }
2906b037
MR
9274 else
9275 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
1180b5a4 9276 if (MIPS_JALR_HINT_P (ep))
df58fc94 9277 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
438c16b8
TS
9278}
9279
252b5132
RH
9280/*
9281 * Generate a "lui" instruction.
9282 */
9283static void
67c0d1eb 9284macro_build_lui (expressionS *ep, int regnum)
252b5132 9285{
9c2799c2 9286 gas_assert (! mips_opts.mips16);
252b5132 9287
df58fc94 9288 if (ep->X_op != O_constant)
252b5132 9289 {
9c2799c2 9290 gas_assert (ep->X_op == O_symbol);
bbe506e8
TS
9291 /* _gp_disp is a special case, used from s_cpload.
9292 __gnu_local_gp is used if mips_no_shared. */
9c2799c2 9293 gas_assert (mips_pic == NO_PIC
78e1bb40 9294 || (! HAVE_NEWABI
aa6975fb
ILT
9295 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
9296 || (! mips_in_shared
bbe506e8
TS
9297 && strcmp (S_GET_NAME (ep->X_add_symbol),
9298 "__gnu_local_gp") == 0));
252b5132
RH
9299 }
9300
df58fc94 9301 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
252b5132
RH
9302}
9303
885add95
CD
9304/* Generate a sequence of instructions to do a load or store from a constant
9305 offset off of a base register (breg) into/from a target register (treg),
9306 using AT if necessary. */
9307static void
67c0d1eb
RS
9308macro_build_ldst_constoffset (expressionS *ep, const char *op,
9309 int treg, int breg, int dbl)
885add95 9310{
9c2799c2 9311 gas_assert (ep->X_op == O_constant);
885add95 9312
256ab948 9313 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
9314 if (!dbl)
9315 normalize_constant_expr (ep);
256ab948 9316
67c1ffbe 9317 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 9318 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
9319 as_warn (_("operand overflow"));
9320
9321 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
9322 {
9323 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 9324 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
9325 }
9326 else
9327 {
9328 /* 32-bit offset, need multiple instructions and AT, like:
9329 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
9330 addu $tempreg,$tempreg,$breg
9331 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
9332 to handle the complete offset. */
67c0d1eb
RS
9333 macro_build_lui (ep, AT);
9334 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
9335 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95 9336
741fe287 9337 if (!mips_opts.at)
1661c76c 9338 as_bad (_("macro used $at after \".set noat\""));
885add95
CD
9339 }
9340}
9341
252b5132
RH
9342/* set_at()
9343 * Generates code to set the $at register to true (one)
9344 * if reg is less than the immediate expression.
9345 */
9346static void
67c0d1eb 9347set_at (int reg, int unsignedp)
252b5132 9348{
b0e6f033 9349 if (imm_expr.X_add_number >= -0x8000
252b5132 9350 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
9351 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
9352 AT, reg, BFD_RELOC_LO16);
252b5132
RH
9353 else
9354 {
bad1aba3 9355 load_register (AT, &imm_expr, GPR_SIZE == 64);
67c0d1eb 9356 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
9357 }
9358}
9359
252b5132
RH
9360/* Count the leading zeroes by performing a binary chop. This is a
9361 bulky bit of source, but performance is a LOT better for the
9362 majority of values than a simple loop to count the bits:
9363 for (lcnt = 0; (lcnt < 32); lcnt++)
9364 if ((v) & (1 << (31 - lcnt)))
9365 break;
9366 However it is not code size friendly, and the gain will drop a bit
9367 on certain cached systems.
9368*/
9369#define COUNT_TOP_ZEROES(v) \
9370 (((v) & ~0xffff) == 0 \
9371 ? ((v) & ~0xff) == 0 \
9372 ? ((v) & ~0xf) == 0 \
9373 ? ((v) & ~0x3) == 0 \
9374 ? ((v) & ~0x1) == 0 \
9375 ? !(v) \
9376 ? 32 \
9377 : 31 \
9378 : 30 \
9379 : ((v) & ~0x7) == 0 \
9380 ? 29 \
9381 : 28 \
9382 : ((v) & ~0x3f) == 0 \
9383 ? ((v) & ~0x1f) == 0 \
9384 ? 27 \
9385 : 26 \
9386 : ((v) & ~0x7f) == 0 \
9387 ? 25 \
9388 : 24 \
9389 : ((v) & ~0xfff) == 0 \
9390 ? ((v) & ~0x3ff) == 0 \
9391 ? ((v) & ~0x1ff) == 0 \
9392 ? 23 \
9393 : 22 \
9394 : ((v) & ~0x7ff) == 0 \
9395 ? 21 \
9396 : 20 \
9397 : ((v) & ~0x3fff) == 0 \
9398 ? ((v) & ~0x1fff) == 0 \
9399 ? 19 \
9400 : 18 \
9401 : ((v) & ~0x7fff) == 0 \
9402 ? 17 \
9403 : 16 \
9404 : ((v) & ~0xffffff) == 0 \
9405 ? ((v) & ~0xfffff) == 0 \
9406 ? ((v) & ~0x3ffff) == 0 \
9407 ? ((v) & ~0x1ffff) == 0 \
9408 ? 15 \
9409 : 14 \
9410 : ((v) & ~0x7ffff) == 0 \
9411 ? 13 \
9412 : 12 \
9413 : ((v) & ~0x3fffff) == 0 \
9414 ? ((v) & ~0x1fffff) == 0 \
9415 ? 11 \
9416 : 10 \
9417 : ((v) & ~0x7fffff) == 0 \
9418 ? 9 \
9419 : 8 \
9420 : ((v) & ~0xfffffff) == 0 \
9421 ? ((v) & ~0x3ffffff) == 0 \
9422 ? ((v) & ~0x1ffffff) == 0 \
9423 ? 7 \
9424 : 6 \
9425 : ((v) & ~0x7ffffff) == 0 \
9426 ? 5 \
9427 : 4 \
9428 : ((v) & ~0x3fffffff) == 0 \
9429 ? ((v) & ~0x1fffffff) == 0 \
9430 ? 3 \
9431 : 2 \
9432 : ((v) & ~0x7fffffff) == 0 \
9433 ? 1 \
9434 : 0)
9435
9436/* load_register()
67c1ffbe 9437 * This routine generates the least number of instructions necessary to load
252b5132
RH
9438 * an absolute expression value into a register.
9439 */
9440static void
67c0d1eb 9441load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
9442{
9443 int freg;
9444 expressionS hi32, lo32;
9445
9446 if (ep->X_op != O_big)
9447 {
9c2799c2 9448 gas_assert (ep->X_op == O_constant);
256ab948
TS
9449
9450 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
9451 if (!dbl)
9452 normalize_constant_expr (ep);
256ab948
TS
9453
9454 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
9455 {
9456 /* We can handle 16 bit signed values with an addiu to
9457 $zero. No need to ever use daddiu here, since $zero and
9458 the result are always correct in 32 bit mode. */
67c0d1eb 9459 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9460 return;
9461 }
9462 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
9463 {
9464 /* We can handle 16 bit unsigned values with an ori to
9465 $zero. */
67c0d1eb 9466 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9467 return;
9468 }
256ab948 9469 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
9470 {
9471 /* 32 bit values require an lui. */
df58fc94 9472 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 9473 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 9474 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
9475 return;
9476 }
9477 }
9478
9479 /* The value is larger than 32 bits. */
9480
bad1aba3 9481 if (!dbl || GPR_SIZE == 32)
252b5132 9482 {
55e08f71
NC
9483 char value[32];
9484
9485 sprintf_vma (value, ep->X_add_number);
1661c76c 9486 as_bad (_("number (0x%s) larger than 32 bits"), value);
67c0d1eb 9487 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9488 return;
9489 }
9490
9491 if (ep->X_op != O_big)
9492 {
9493 hi32 = *ep;
9494 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9495 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9496 hi32.X_add_number &= 0xffffffff;
9497 lo32 = *ep;
9498 lo32.X_add_number &= 0xffffffff;
9499 }
9500 else
9501 {
9c2799c2 9502 gas_assert (ep->X_add_number > 2);
252b5132
RH
9503 if (ep->X_add_number == 3)
9504 generic_bignum[3] = 0;
9505 else if (ep->X_add_number > 4)
1661c76c 9506 as_bad (_("number larger than 64 bits"));
252b5132
RH
9507 lo32.X_op = O_constant;
9508 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
9509 hi32.X_op = O_constant;
9510 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
9511 }
9512
9513 if (hi32.X_add_number == 0)
9514 freg = 0;
9515 else
9516 {
9517 int shift, bit;
9518 unsigned long hi, lo;
9519
956cd1d6 9520 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
9521 {
9522 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
9523 {
67c0d1eb 9524 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
9525 return;
9526 }
9527 if (lo32.X_add_number & 0x80000000)
9528 {
df58fc94 9529 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 9530 if (lo32.X_add_number & 0xffff)
67c0d1eb 9531 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
9532 return;
9533 }
9534 }
252b5132
RH
9535
9536 /* Check for 16bit shifted constant. We know that hi32 is
9537 non-zero, so start the mask on the first bit of the hi32
9538 value. */
9539 shift = 17;
9540 do
beae10d5
KH
9541 {
9542 unsigned long himask, lomask;
9543
9544 if (shift < 32)
9545 {
9546 himask = 0xffff >> (32 - shift);
9547 lomask = (0xffff << shift) & 0xffffffff;
9548 }
9549 else
9550 {
9551 himask = 0xffff << (shift - 32);
9552 lomask = 0;
9553 }
9554 if ((hi32.X_add_number & ~(offsetT) himask) == 0
9555 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
9556 {
9557 expressionS tmp;
9558
9559 tmp.X_op = O_constant;
9560 if (shift < 32)
9561 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
9562 | (lo32.X_add_number >> shift));
9563 else
9564 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb 9565 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
df58fc94 9566 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 9567 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
9568 return;
9569 }
f9419b05 9570 ++shift;
beae10d5
KH
9571 }
9572 while (shift <= (64 - 16));
252b5132
RH
9573
9574 /* Find the bit number of the lowest one bit, and store the
9575 shifted value in hi/lo. */
9576 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
9577 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
9578 if (lo != 0)
9579 {
9580 bit = 0;
9581 while ((lo & 1) == 0)
9582 {
9583 lo >>= 1;
9584 ++bit;
9585 }
9586 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
9587 hi >>= bit;
9588 }
9589 else
9590 {
9591 bit = 32;
9592 while ((hi & 1) == 0)
9593 {
9594 hi >>= 1;
9595 ++bit;
9596 }
9597 lo = hi;
9598 hi = 0;
9599 }
9600
9601 /* Optimize if the shifted value is a (power of 2) - 1. */
9602 if ((hi == 0 && ((lo + 1) & lo) == 0)
9603 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
9604 {
9605 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 9606 if (shift != 0)
beae10d5 9607 {
252b5132
RH
9608 expressionS tmp;
9609
9610 /* This instruction will set the register to be all
9611 ones. */
beae10d5
KH
9612 tmp.X_op = O_constant;
9613 tmp.X_add_number = (offsetT) -1;
67c0d1eb 9614 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
9615 if (bit != 0)
9616 {
9617 bit += shift;
df58fc94 9618 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 9619 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 9620 }
df58fc94 9621 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
67c0d1eb 9622 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
9623 return;
9624 }
9625 }
252b5132
RH
9626
9627 /* Sign extend hi32 before calling load_register, because we can
9628 generally get better code when we load a sign extended value. */
9629 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 9630 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 9631 load_register (reg, &hi32, 0);
252b5132
RH
9632 freg = reg;
9633 }
9634 if ((lo32.X_add_number & 0xffff0000) == 0)
9635 {
9636 if (freg != 0)
9637 {
df58fc94 9638 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
252b5132
RH
9639 freg = reg;
9640 }
9641 }
9642 else
9643 {
9644 expressionS mid16;
9645
956cd1d6 9646 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 9647 {
df58fc94
RS
9648 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9649 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
beae10d5
KH
9650 return;
9651 }
252b5132
RH
9652
9653 if (freg != 0)
9654 {
df58fc94 9655 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
252b5132
RH
9656 freg = reg;
9657 }
9658 mid16 = lo32;
9659 mid16.X_add_number >>= 16;
67c0d1eb 9660 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
df58fc94 9661 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
252b5132
RH
9662 freg = reg;
9663 }
9664 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 9665 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
9666}
9667
269137b2
TS
9668static inline void
9669load_delay_nop (void)
9670{
9671 if (!gpr_interlocks)
9672 macro_build (NULL, "nop", "");
9673}
9674
252b5132
RH
9675/* Load an address into a register. */
9676
9677static void
67c0d1eb 9678load_address (int reg, expressionS *ep, int *used_at)
252b5132 9679{
252b5132
RH
9680 if (ep->X_op != O_constant
9681 && ep->X_op != O_symbol)
9682 {
9683 as_bad (_("expression too complex"));
9684 ep->X_op = O_constant;
9685 }
9686
9687 if (ep->X_op == O_constant)
9688 {
67c0d1eb 9689 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
9690 return;
9691 }
9692
9693 if (mips_pic == NO_PIC)
9694 {
9695 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 9696 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
9697 Otherwise we want
9698 lui $reg,<sym> (BFD_RELOC_HI16_S)
9699 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 9700 If we have an addend, we always use the latter form.
76b3015f 9701
d6bc6245
TS
9702 With 64bit address space and a usable $at we want
9703 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9704 lui $at,<sym> (BFD_RELOC_HI16_S)
9705 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9706 daddiu $at,<sym> (BFD_RELOC_LO16)
9707 dsll32 $reg,0
3a482fd5 9708 daddu $reg,$reg,$at
76b3015f 9709
c03099e6 9710 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
9711 on superscalar processors.
9712 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9713 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9714 dsll $reg,16
9715 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
9716 dsll $reg,16
9717 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
9718
9719 For GP relative symbols in 64bit address space we can use
9720 the same sequence as in 32bit address space. */
aed1a261 9721 if (HAVE_64BIT_SYMBOLS)
d6bc6245 9722 {
6caf9ef4
TS
9723 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9724 && !nopic_need_relax (ep->X_add_symbol, 1))
9725 {
9726 relax_start (ep->X_add_symbol);
9727 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9728 mips_gp_register, BFD_RELOC_GPREL16);
9729 relax_switch ();
9730 }
d6bc6245 9731
741fe287 9732 if (*used_at == 0 && mips_opts.at)
d6bc6245 9733 {
df58fc94
RS
9734 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9735 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
67c0d1eb
RS
9736 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9737 BFD_RELOC_MIPS_HIGHER);
9738 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
df58fc94 9739 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
67c0d1eb 9740 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
9741 *used_at = 1;
9742 }
9743 else
9744 {
df58fc94 9745 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb
RS
9746 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9747 BFD_RELOC_MIPS_HIGHER);
df58fc94 9748 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 9749 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
df58fc94 9750 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 9751 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 9752 }
6caf9ef4
TS
9753
9754 if (mips_relax.sequence)
9755 relax_end ();
d6bc6245 9756 }
252b5132
RH
9757 else
9758 {
d6bc6245 9759 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 9760 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 9761 {
4d7206a2 9762 relax_start (ep->X_add_symbol);
67c0d1eb 9763 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 9764 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 9765 relax_switch ();
d6bc6245 9766 }
67c0d1eb
RS
9767 macro_build_lui (ep, reg);
9768 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
9769 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
9770 if (mips_relax.sequence)
9771 relax_end ();
d6bc6245 9772 }
252b5132 9773 }
0a44bf69 9774 else if (!mips_big_got)
252b5132
RH
9775 {
9776 expressionS ex;
9777
9778 /* If this is a reference to an external symbol, we want
9779 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9780 Otherwise we want
9781 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9782 nop
9783 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
9784 If there is a constant, it must be added in after.
9785
ed6fb7bd 9786 If we have NewABI, we want
f5040a92
AO
9787 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
9788 unless we're referencing a global symbol with a non-zero
9789 offset, in which case cst must be added separately. */
ed6fb7bd
SC
9790 if (HAVE_NEWABI)
9791 {
f5040a92
AO
9792 if (ep->X_add_number)
9793 {
4d7206a2 9794 ex.X_add_number = ep->X_add_number;
f5040a92 9795 ep->X_add_number = 0;
4d7206a2 9796 relax_start (ep->X_add_symbol);
67c0d1eb
RS
9797 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9798 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
9799 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9800 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9801 ex.X_op = O_constant;
67c0d1eb 9802 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 9803 reg, reg, BFD_RELOC_LO16);
f5040a92 9804 ep->X_add_number = ex.X_add_number;
4d7206a2 9805 relax_switch ();
f5040a92 9806 }
67c0d1eb 9807 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9808 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
9809 if (mips_relax.sequence)
9810 relax_end ();
ed6fb7bd
SC
9811 }
9812 else
9813 {
f5040a92
AO
9814 ex.X_add_number = ep->X_add_number;
9815 ep->X_add_number = 0;
67c0d1eb
RS
9816 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9817 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9818 load_delay_nop ();
4d7206a2
RS
9819 relax_start (ep->X_add_symbol);
9820 relax_switch ();
67c0d1eb 9821 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 9822 BFD_RELOC_LO16);
4d7206a2 9823 relax_end ();
ed6fb7bd 9824
f5040a92
AO
9825 if (ex.X_add_number != 0)
9826 {
9827 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9828 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9829 ex.X_op = O_constant;
67c0d1eb 9830 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 9831 reg, reg, BFD_RELOC_LO16);
f5040a92 9832 }
252b5132
RH
9833 }
9834 }
0a44bf69 9835 else if (mips_big_got)
252b5132
RH
9836 {
9837 expressionS ex;
252b5132
RH
9838
9839 /* This is the large GOT case. If this is a reference to an
9840 external symbol, we want
9841 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9842 addu $reg,$reg,$gp
9843 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
9844
9845 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
9846 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9847 nop
9848 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 9849 If there is a constant, it must be added in after.
f5040a92
AO
9850
9851 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
9852 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
9853 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 9854 */
438c16b8
TS
9855 if (HAVE_NEWABI)
9856 {
4d7206a2 9857 ex.X_add_number = ep->X_add_number;
f5040a92 9858 ep->X_add_number = 0;
4d7206a2 9859 relax_start (ep->X_add_symbol);
df58fc94 9860 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
9861 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9862 reg, reg, mips_gp_register);
9863 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9864 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
9865 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9866 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9867 else if (ex.X_add_number)
9868 {
9869 ex.X_op = O_constant;
67c0d1eb
RS
9870 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9871 BFD_RELOC_LO16);
f5040a92
AO
9872 }
9873
9874 ep->X_add_number = ex.X_add_number;
4d7206a2 9875 relax_switch ();
67c0d1eb 9876 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9877 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
9878 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9879 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 9880 relax_end ();
438c16b8 9881 }
252b5132 9882 else
438c16b8 9883 {
f5040a92
AO
9884 ex.X_add_number = ep->X_add_number;
9885 ep->X_add_number = 0;
4d7206a2 9886 relax_start (ep->X_add_symbol);
df58fc94 9887 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
9888 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9889 reg, reg, mips_gp_register);
9890 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9891 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
9892 relax_switch ();
9893 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
9894 {
9895 /* We need a nop before loading from $gp. This special
9896 check is required because the lui which starts the main
9897 instruction stream does not refer to $gp, and so will not
9898 insert the nop which may be required. */
67c0d1eb 9899 macro_build (NULL, "nop", "");
438c16b8 9900 }
67c0d1eb 9901 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9902 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9903 load_delay_nop ();
67c0d1eb 9904 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 9905 BFD_RELOC_LO16);
4d7206a2 9906 relax_end ();
438c16b8 9907
f5040a92
AO
9908 if (ex.X_add_number != 0)
9909 {
9910 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9911 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9912 ex.X_op = O_constant;
67c0d1eb
RS
9913 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9914 BFD_RELOC_LO16);
f5040a92 9915 }
252b5132
RH
9916 }
9917 }
252b5132
RH
9918 else
9919 abort ();
8fc2e39e 9920
741fe287 9921 if (!mips_opts.at && *used_at == 1)
1661c76c 9922 as_bad (_("macro used $at after \".set noat\""));
252b5132
RH
9923}
9924
ea1fb5dc
RS
9925/* Move the contents of register SOURCE into register DEST. */
9926
9927static void
67c0d1eb 9928move_register (int dest, int source)
ea1fb5dc 9929{
df58fc94
RS
9930 /* Prefer to use a 16-bit microMIPS instruction unless the previous
9931 instruction specifically requires a 32-bit one. */
9932 if (mips_opts.micromips
833794fc 9933 && !mips_opts.insn32
df58fc94 9934 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7951ca42 9935 macro_build (NULL, "move", "mp,mj", dest, source);
df58fc94 9936 else
40fc1451 9937 macro_build (NULL, "or", "d,v,t", dest, source, 0);
ea1fb5dc
RS
9938}
9939
4d7206a2 9940/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
9941 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
9942 The two alternatives are:
4d7206a2 9943
33eaf5de 9944 Global symbol Local symbol
4d7206a2
RS
9945 ------------- ------------
9946 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
9947 ... ...
9948 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
9949
9950 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
9951 emits the second for a 16-bit offset or add_got_offset_hilo emits
9952 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
9953
9954static void
67c0d1eb 9955load_got_offset (int dest, expressionS *local)
4d7206a2
RS
9956{
9957 expressionS global;
9958
9959 global = *local;
9960 global.X_add_number = 0;
9961
9962 relax_start (local->X_add_symbol);
67c0d1eb
RS
9963 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9964 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 9965 relax_switch ();
67c0d1eb
RS
9966 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9967 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
9968 relax_end ();
9969}
9970
9971static void
67c0d1eb 9972add_got_offset (int dest, expressionS *local)
4d7206a2
RS
9973{
9974 expressionS global;
9975
9976 global.X_op = O_constant;
9977 global.X_op_symbol = NULL;
9978 global.X_add_symbol = NULL;
9979 global.X_add_number = local->X_add_number;
9980
9981 relax_start (local->X_add_symbol);
67c0d1eb 9982 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
9983 dest, dest, BFD_RELOC_LO16);
9984 relax_switch ();
67c0d1eb 9985 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
9986 relax_end ();
9987}
9988
f6a22291
MR
9989static void
9990add_got_offset_hilo (int dest, expressionS *local, int tmp)
9991{
9992 expressionS global;
9993 int hold_mips_optimize;
9994
9995 global.X_op = O_constant;
9996 global.X_op_symbol = NULL;
9997 global.X_add_symbol = NULL;
9998 global.X_add_number = local->X_add_number;
9999
10000 relax_start (local->X_add_symbol);
10001 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
10002 relax_switch ();
10003 /* Set mips_optimize around the lui instruction to avoid
10004 inserting an unnecessary nop after the lw. */
10005 hold_mips_optimize = mips_optimize;
10006 mips_optimize = 2;
10007 macro_build_lui (&global, tmp);
10008 mips_optimize = hold_mips_optimize;
10009 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
10010 relax_end ();
10011
10012 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
10013}
10014
df58fc94
RS
10015/* Emit a sequence of instructions to emulate a branch likely operation.
10016 BR is an ordinary branch corresponding to one to be emulated. BRNEG
10017 is its complementing branch with the original condition negated.
10018 CALL is set if the original branch specified the link operation.
10019 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
10020
10021 Code like this is produced in the noreorder mode:
10022
10023 BRNEG <args>, 1f
10024 nop
10025 b <sym>
10026 delay slot (executed only if branch taken)
10027 1:
10028
10029 or, if CALL is set:
10030
10031 BRNEG <args>, 1f
10032 nop
10033 bal <sym>
10034 delay slot (executed only if branch taken)
10035 1:
10036
10037 In the reorder mode the delay slot would be filled with a nop anyway,
10038 so code produced is simply:
10039
10040 BR <args>, <sym>
10041 nop
10042
10043 This function is used when producing code for the microMIPS ASE that
10044 does not implement branch likely instructions in hardware. */
10045
10046static void
10047macro_build_branch_likely (const char *br, const char *brneg,
10048 int call, expressionS *ep, const char *fmt,
10049 unsigned int sreg, unsigned int treg)
10050{
10051 int noreorder = mips_opts.noreorder;
10052 expressionS expr1;
10053
10054 gas_assert (mips_opts.micromips);
10055 start_noreorder ();
10056 if (noreorder)
10057 {
10058 micromips_label_expr (&expr1);
10059 macro_build (&expr1, brneg, fmt, sreg, treg);
10060 macro_build (NULL, "nop", "");
10061 macro_build (ep, call ? "bal" : "b", "p");
10062
10063 /* Set to true so that append_insn adds a label. */
10064 emit_branch_likely_macro = TRUE;
10065 }
10066 else
10067 {
10068 macro_build (ep, br, fmt, sreg, treg);
10069 macro_build (NULL, "nop", "");
10070 }
10071 end_noreorder ();
10072}
10073
10074/* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
10075 the condition code tested. EP specifies the branch target. */
10076
10077static void
10078macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
10079{
10080 const int call = 0;
10081 const char *brneg;
10082 const char *br;
10083
10084 switch (type)
10085 {
10086 case M_BC1FL:
10087 br = "bc1f";
10088 brneg = "bc1t";
10089 break;
10090 case M_BC1TL:
10091 br = "bc1t";
10092 brneg = "bc1f";
10093 break;
10094 case M_BC2FL:
10095 br = "bc2f";
10096 brneg = "bc2t";
10097 break;
10098 case M_BC2TL:
10099 br = "bc2t";
10100 brneg = "bc2f";
10101 break;
10102 default:
10103 abort ();
10104 }
10105 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
10106}
10107
10108/* Emit a two-argument branch macro specified by TYPE, using SREG as
10109 the register tested. EP specifies the branch target. */
10110
10111static void
10112macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
10113{
10114 const char *brneg = NULL;
10115 const char *br;
10116 int call = 0;
10117
10118 switch (type)
10119 {
10120 case M_BGEZ:
10121 br = "bgez";
10122 break;
10123 case M_BGEZL:
10124 br = mips_opts.micromips ? "bgez" : "bgezl";
10125 brneg = "bltz";
10126 break;
10127 case M_BGEZALL:
10128 gas_assert (mips_opts.micromips);
833794fc 10129 br = mips_opts.insn32 ? "bgezal" : "bgezals";
df58fc94
RS
10130 brneg = "bltz";
10131 call = 1;
10132 break;
10133 case M_BGTZ:
10134 br = "bgtz";
10135 break;
10136 case M_BGTZL:
10137 br = mips_opts.micromips ? "bgtz" : "bgtzl";
10138 brneg = "blez";
10139 break;
10140 case M_BLEZ:
10141 br = "blez";
10142 break;
10143 case M_BLEZL:
10144 br = mips_opts.micromips ? "blez" : "blezl";
10145 brneg = "bgtz";
10146 break;
10147 case M_BLTZ:
10148 br = "bltz";
10149 break;
10150 case M_BLTZL:
10151 br = mips_opts.micromips ? "bltz" : "bltzl";
10152 brneg = "bgez";
10153 break;
10154 case M_BLTZALL:
10155 gas_assert (mips_opts.micromips);
833794fc 10156 br = mips_opts.insn32 ? "bltzal" : "bltzals";
df58fc94
RS
10157 brneg = "bgez";
10158 call = 1;
10159 break;
10160 default:
10161 abort ();
10162 }
10163 if (mips_opts.micromips && brneg)
10164 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
10165 else
10166 macro_build (ep, br, "s,p", sreg);
10167}
10168
10169/* Emit a three-argument branch macro specified by TYPE, using SREG and
10170 TREG as the registers tested. EP specifies the branch target. */
10171
10172static void
10173macro_build_branch_rsrt (int type, expressionS *ep,
10174 unsigned int sreg, unsigned int treg)
10175{
10176 const char *brneg = NULL;
10177 const int call = 0;
10178 const char *br;
10179
10180 switch (type)
10181 {
10182 case M_BEQ:
10183 case M_BEQ_I:
10184 br = "beq";
10185 break;
10186 case M_BEQL:
10187 case M_BEQL_I:
10188 br = mips_opts.micromips ? "beq" : "beql";
10189 brneg = "bne";
10190 break;
10191 case M_BNE:
10192 case M_BNE_I:
10193 br = "bne";
10194 break;
10195 case M_BNEL:
10196 case M_BNEL_I:
10197 br = mips_opts.micromips ? "bne" : "bnel";
10198 brneg = "beq";
10199 break;
10200 default:
10201 abort ();
10202 }
10203 if (mips_opts.micromips && brneg)
10204 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
10205 else
10206 macro_build (ep, br, "s,t,p", sreg, treg);
10207}
10208
f2ae14a1
RS
10209/* Return the high part that should be loaded in order to make the low
10210 part of VALUE accessible using an offset of OFFBITS bits. */
10211
10212static offsetT
10213offset_high_part (offsetT value, unsigned int offbits)
10214{
10215 offsetT bias;
10216 addressT low_mask;
10217
10218 if (offbits == 0)
10219 return value;
10220 bias = 1 << (offbits - 1);
10221 low_mask = bias * 2 - 1;
10222 return (value + bias) & ~low_mask;
10223}
10224
10225/* Return true if the value stored in offset_expr and offset_reloc
10226 fits into a signed offset of OFFBITS bits. RANGE is the maximum
10227 amount that the caller wants to add without inducing overflow
10228 and ALIGN is the known alignment of the value in bytes. */
10229
10230static bfd_boolean
10231small_offset_p (unsigned int range, unsigned int align, unsigned int offbits)
10232{
10233 if (offbits == 16)
10234 {
10235 /* Accept any relocation operator if overflow isn't a concern. */
10236 if (range < align && *offset_reloc != BFD_RELOC_UNUSED)
10237 return TRUE;
10238
10239 /* These relocations are guaranteed not to overflow in correct links. */
10240 if (*offset_reloc == BFD_RELOC_MIPS_LITERAL
10241 || gprel16_reloc_p (*offset_reloc))
10242 return TRUE;
10243 }
10244 if (offset_expr.X_op == O_constant
10245 && offset_high_part (offset_expr.X_add_number, offbits) == 0
10246 && offset_high_part (offset_expr.X_add_number + range, offbits) == 0)
10247 return TRUE;
10248 return FALSE;
10249}
10250
252b5132
RH
10251/*
10252 * Build macros
10253 * This routine implements the seemingly endless macro or synthesized
10254 * instructions and addressing modes in the mips assembly language. Many
10255 * of these macros are simple and are similar to each other. These could
67c1ffbe 10256 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
10257 * this verbose method. Others are not simple macros but are more like
10258 * optimizing code generation.
10259 * One interesting optimization is when several store macros appear
67c1ffbe 10260 * consecutively that would load AT with the upper half of the same address.
2b0f3761 10261 * The ensuing load upper instructions are omitted. This implies some kind
252b5132
RH
10262 * of global optimization. We currently only optimize within a single macro.
10263 * For many of the load and store macros if the address is specified as a
10264 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
10265 * first load register 'at' with zero and use it as the base register. The
10266 * mips assembler simply uses register $zero. Just one tiny optimization
10267 * we're missing.
10268 */
10269static void
833794fc 10270macro (struct mips_cl_insn *ip, char *str)
252b5132 10271{
c0ebe874
RS
10272 const struct mips_operand_array *operands;
10273 unsigned int breg, i;
741fe287 10274 unsigned int tempreg;
252b5132 10275 int mask;
43841e91 10276 int used_at = 0;
df58fc94 10277 expressionS label_expr;
252b5132 10278 expressionS expr1;
df58fc94 10279 expressionS *ep;
252b5132
RH
10280 const char *s;
10281 const char *s2;
10282 const char *fmt;
10283 int likely = 0;
252b5132 10284 int coproc = 0;
7f3c4072 10285 int offbits = 16;
1abe91b1 10286 int call = 0;
df58fc94
RS
10287 int jals = 0;
10288 int dbl = 0;
10289 int imm = 0;
10290 int ust = 0;
10291 int lp = 0;
a45328b9 10292 int ll_sc_paired = 0;
f2ae14a1 10293 bfd_boolean large_offset;
252b5132 10294 int off;
252b5132 10295 int hold_mips_optimize;
f2ae14a1 10296 unsigned int align;
c0ebe874 10297 unsigned int op[MAX_OPERANDS];
252b5132 10298
9c2799c2 10299 gas_assert (! mips_opts.mips16);
252b5132 10300
c0ebe874
RS
10301 operands = insn_operands (ip);
10302 for (i = 0; i < MAX_OPERANDS; i++)
10303 if (operands->operand[i])
10304 op[i] = insn_extract_operand (ip, operands->operand[i]);
10305 else
10306 op[i] = -1;
10307
252b5132
RH
10308 mask = ip->insn_mo->mask;
10309
df58fc94
RS
10310 label_expr.X_op = O_constant;
10311 label_expr.X_op_symbol = NULL;
10312 label_expr.X_add_symbol = NULL;
10313 label_expr.X_add_number = 0;
10314
252b5132
RH
10315 expr1.X_op = O_constant;
10316 expr1.X_op_symbol = NULL;
10317 expr1.X_add_symbol = NULL;
10318 expr1.X_add_number = 1;
f2ae14a1 10319 align = 1;
252b5132
RH
10320
10321 switch (mask)
10322 {
10323 case M_DABS:
10324 dbl = 1;
1a0670f3 10325 /* Fall through. */
252b5132 10326 case M_ABS:
df58fc94
RS
10327 /* bgez $a0,1f
10328 move v0,$a0
10329 sub v0,$zero,$a0
10330 1:
10331 */
252b5132 10332
7d10b47d 10333 start_noreorder ();
252b5132 10334
df58fc94
RS
10335 if (mips_opts.micromips)
10336 micromips_label_expr (&label_expr);
10337 else
10338 label_expr.X_add_number = 8;
c0ebe874
RS
10339 macro_build (&label_expr, "bgez", "s,p", op[1]);
10340 if (op[0] == op[1])
a605d2b3 10341 macro_build (NULL, "nop", "");
252b5132 10342 else
c0ebe874
RS
10343 move_register (op[0], op[1]);
10344 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", op[0], 0, op[1]);
df58fc94
RS
10345 if (mips_opts.micromips)
10346 micromips_add_label ();
252b5132 10347
7d10b47d 10348 end_noreorder ();
8fc2e39e 10349 break;
252b5132
RH
10350
10351 case M_ADD_I:
10352 s = "addi";
10353 s2 = "add";
387e7624
FS
10354 if (ISA_IS_R6 (mips_opts.isa))
10355 goto do_addi_i;
10356 else
10357 goto do_addi;
252b5132
RH
10358 case M_ADDU_I:
10359 s = "addiu";
10360 s2 = "addu";
10361 goto do_addi;
10362 case M_DADD_I:
10363 dbl = 1;
10364 s = "daddi";
10365 s2 = "dadd";
387e7624 10366 if (!mips_opts.micromips && !ISA_IS_R6 (mips_opts.isa))
df58fc94 10367 goto do_addi;
b0e6f033 10368 if (imm_expr.X_add_number >= -0x200
387e7624
FS
10369 && imm_expr.X_add_number < 0x200
10370 && !ISA_IS_R6 (mips_opts.isa))
df58fc94 10371 {
b0e6f033
RS
10372 macro_build (NULL, s, "t,r,.", op[0], op[1],
10373 (int) imm_expr.X_add_number);
df58fc94
RS
10374 break;
10375 }
10376 goto do_addi_i;
252b5132
RH
10377 case M_DADDU_I:
10378 dbl = 1;
10379 s = "daddiu";
10380 s2 = "daddu";
10381 do_addi:
b0e6f033 10382 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
10383 && imm_expr.X_add_number < 0x8000)
10384 {
c0ebe874 10385 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 10386 break;
252b5132 10387 }
df58fc94 10388 do_addi_i:
8fc2e39e 10389 used_at = 1;
67c0d1eb 10390 load_register (AT, &imm_expr, dbl);
c0ebe874 10391 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
10392 break;
10393
10394 case M_AND_I:
10395 s = "andi";
10396 s2 = "and";
10397 goto do_bit;
10398 case M_OR_I:
10399 s = "ori";
10400 s2 = "or";
10401 goto do_bit;
10402 case M_NOR_I:
10403 s = "";
10404 s2 = "nor";
10405 goto do_bit;
10406 case M_XOR_I:
10407 s = "xori";
10408 s2 = "xor";
10409 do_bit:
b0e6f033 10410 if (imm_expr.X_add_number >= 0
252b5132
RH
10411 && imm_expr.X_add_number < 0x10000)
10412 {
10413 if (mask != M_NOR_I)
c0ebe874 10414 macro_build (&imm_expr, s, "t,r,i", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
10415 else
10416 {
67c0d1eb 10417 macro_build (&imm_expr, "ori", "t,r,i",
c0ebe874
RS
10418 op[0], op[1], BFD_RELOC_LO16);
10419 macro_build (NULL, "nor", "d,v,t", op[0], op[0], 0);
252b5132 10420 }
8fc2e39e 10421 break;
252b5132
RH
10422 }
10423
8fc2e39e 10424 used_at = 1;
bad1aba3 10425 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 10426 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
10427 break;
10428
8b082fb1
TS
10429 case M_BALIGN:
10430 switch (imm_expr.X_add_number)
10431 {
10432 case 0:
10433 macro_build (NULL, "nop", "");
10434 break;
10435 case 2:
c0ebe874 10436 macro_build (NULL, "packrl.ph", "d,s,t", op[0], op[0], op[1]);
8b082fb1 10437 break;
03f66e8a
MR
10438 case 1:
10439 case 3:
c0ebe874 10440 macro_build (NULL, "balign", "t,s,2", op[0], op[1],
90ecf173 10441 (int) imm_expr.X_add_number);
8b082fb1 10442 break;
03f66e8a
MR
10443 default:
10444 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
10445 (unsigned long) imm_expr.X_add_number);
10446 break;
8b082fb1
TS
10447 }
10448 break;
10449
df58fc94
RS
10450 case M_BC1FL:
10451 case M_BC1TL:
10452 case M_BC2FL:
10453 case M_BC2TL:
10454 gas_assert (mips_opts.micromips);
10455 macro_build_branch_ccl (mask, &offset_expr,
10456 EXTRACT_OPERAND (1, BCC, *ip));
10457 break;
10458
252b5132 10459 case M_BEQ_I:
252b5132 10460 case M_BEQL_I:
252b5132 10461 case M_BNE_I:
252b5132 10462 case M_BNEL_I:
b0e6f033 10463 if (imm_expr.X_add_number == 0)
c0ebe874 10464 op[1] = 0;
df58fc94 10465 else
252b5132 10466 {
c0ebe874 10467 op[1] = AT;
df58fc94 10468 used_at = 1;
bad1aba3 10469 load_register (op[1], &imm_expr, GPR_SIZE == 64);
252b5132 10470 }
df58fc94
RS
10471 /* Fall through. */
10472 case M_BEQL:
10473 case M_BNEL:
c0ebe874 10474 macro_build_branch_rsrt (mask, &offset_expr, op[0], op[1]);
252b5132
RH
10475 break;
10476
10477 case M_BGEL:
10478 likely = 1;
1a0670f3 10479 /* Fall through. */
252b5132 10480 case M_BGE:
c0ebe874
RS
10481 if (op[1] == 0)
10482 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[0]);
10483 else if (op[0] == 0)
10484 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[1]);
df58fc94 10485 else
252b5132 10486 {
df58fc94 10487 used_at = 1;
c0ebe874 10488 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10489 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10490 &offset_expr, AT, ZERO);
252b5132 10491 }
df58fc94
RS
10492 break;
10493
10494 case M_BGEZL:
10495 case M_BGEZALL:
10496 case M_BGTZL:
10497 case M_BLEZL:
10498 case M_BLTZL:
10499 case M_BLTZALL:
c0ebe874 10500 macro_build_branch_rs (mask, &offset_expr, op[0]);
252b5132
RH
10501 break;
10502
10503 case M_BGTL_I:
10504 likely = 1;
1a0670f3 10505 /* Fall through. */
252b5132 10506 case M_BGT_I:
90ecf173 10507 /* Check for > max integer. */
b0e6f033 10508 if (imm_expr.X_add_number >= GPR_SMAX)
252b5132
RH
10509 {
10510 do_false:
90ecf173 10511 /* Result is always false. */
252b5132 10512 if (! likely)
a605d2b3 10513 macro_build (NULL, "nop", "");
252b5132 10514 else
df58fc94 10515 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
8fc2e39e 10516 break;
252b5132 10517 }
f9419b05 10518 ++imm_expr.X_add_number;
6f2117ba 10519 /* Fall through. */
252b5132
RH
10520 case M_BGE_I:
10521 case M_BGEL_I:
10522 if (mask == M_BGEL_I)
10523 likely = 1;
b0e6f033 10524 if (imm_expr.X_add_number == 0)
252b5132 10525 {
df58fc94 10526 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
c0ebe874 10527 &offset_expr, op[0]);
8fc2e39e 10528 break;
252b5132 10529 }
b0e6f033 10530 if (imm_expr.X_add_number == 1)
252b5132 10531 {
df58fc94 10532 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
c0ebe874 10533 &offset_expr, op[0]);
8fc2e39e 10534 break;
252b5132 10535 }
b0e6f033 10536 if (imm_expr.X_add_number <= GPR_SMIN)
252b5132
RH
10537 {
10538 do_true:
6f2117ba 10539 /* Result is always true. */
1661c76c 10540 as_warn (_("branch %s is always true"), ip->insn_mo->name);
67c0d1eb 10541 macro_build (&offset_expr, "b", "p");
8fc2e39e 10542 break;
252b5132 10543 }
8fc2e39e 10544 used_at = 1;
c0ebe874 10545 set_at (op[0], 0);
df58fc94
RS
10546 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10547 &offset_expr, AT, ZERO);
252b5132
RH
10548 break;
10549
10550 case M_BGEUL:
10551 likely = 1;
1a0670f3 10552 /* Fall through. */
252b5132 10553 case M_BGEU:
c0ebe874 10554 if (op[1] == 0)
252b5132 10555 goto do_true;
c0ebe874 10556 else if (op[0] == 0)
df58fc94 10557 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 10558 &offset_expr, ZERO, op[1]);
df58fc94 10559 else
252b5132 10560 {
df58fc94 10561 used_at = 1;
c0ebe874 10562 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10563 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10564 &offset_expr, AT, ZERO);
252b5132 10565 }
252b5132
RH
10566 break;
10567
10568 case M_BGTUL_I:
10569 likely = 1;
1a0670f3 10570 /* Fall through. */
252b5132 10571 case M_BGTU_I:
c0ebe874 10572 if (op[0] == 0
bad1aba3 10573 || (GPR_SIZE == 32
f01dc953 10574 && imm_expr.X_add_number == -1))
252b5132 10575 goto do_false;
f9419b05 10576 ++imm_expr.X_add_number;
6f2117ba 10577 /* Fall through. */
252b5132
RH
10578 case M_BGEU_I:
10579 case M_BGEUL_I:
10580 if (mask == M_BGEUL_I)
10581 likely = 1;
b0e6f033 10582 if (imm_expr.X_add_number == 0)
252b5132 10583 goto do_true;
b0e6f033 10584 else if (imm_expr.X_add_number == 1)
df58fc94 10585 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 10586 &offset_expr, op[0], ZERO);
df58fc94 10587 else
252b5132 10588 {
df58fc94 10589 used_at = 1;
c0ebe874 10590 set_at (op[0], 1);
df58fc94
RS
10591 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10592 &offset_expr, AT, ZERO);
252b5132 10593 }
252b5132
RH
10594 break;
10595
10596 case M_BGTL:
10597 likely = 1;
1a0670f3 10598 /* Fall through. */
252b5132 10599 case M_BGT:
c0ebe874
RS
10600 if (op[1] == 0)
10601 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[0]);
10602 else if (op[0] == 0)
10603 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[1]);
df58fc94 10604 else
252b5132 10605 {
df58fc94 10606 used_at = 1;
c0ebe874 10607 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10608 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10609 &offset_expr, AT, ZERO);
252b5132 10610 }
252b5132
RH
10611 break;
10612
10613 case M_BGTUL:
10614 likely = 1;
1a0670f3 10615 /* Fall through. */
252b5132 10616 case M_BGTU:
c0ebe874 10617 if (op[1] == 0)
df58fc94 10618 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874
RS
10619 &offset_expr, op[0], ZERO);
10620 else if (op[0] == 0)
df58fc94
RS
10621 goto do_false;
10622 else
252b5132 10623 {
df58fc94 10624 used_at = 1;
c0ebe874 10625 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10626 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10627 &offset_expr, AT, ZERO);
252b5132 10628 }
252b5132
RH
10629 break;
10630
10631 case M_BLEL:
10632 likely = 1;
1a0670f3 10633 /* Fall through. */
252b5132 10634 case M_BLE:
c0ebe874
RS
10635 if (op[1] == 0)
10636 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10637 else if (op[0] == 0)
10638 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[1]);
df58fc94 10639 else
252b5132 10640 {
df58fc94 10641 used_at = 1;
c0ebe874 10642 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10643 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10644 &offset_expr, AT, ZERO);
252b5132 10645 }
252b5132
RH
10646 break;
10647
10648 case M_BLEL_I:
10649 likely = 1;
1a0670f3 10650 /* Fall through. */
252b5132 10651 case M_BLE_I:
b0e6f033 10652 if (imm_expr.X_add_number >= GPR_SMAX)
252b5132 10653 goto do_true;
f9419b05 10654 ++imm_expr.X_add_number;
6f2117ba 10655 /* Fall through. */
252b5132
RH
10656 case M_BLT_I:
10657 case M_BLTL_I:
10658 if (mask == M_BLTL_I)
10659 likely = 1;
b0e6f033 10660 if (imm_expr.X_add_number == 0)
c0ebe874 10661 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
b0e6f033 10662 else if (imm_expr.X_add_number == 1)
c0ebe874 10663 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
df58fc94 10664 else
252b5132 10665 {
df58fc94 10666 used_at = 1;
c0ebe874 10667 set_at (op[0], 0);
df58fc94
RS
10668 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10669 &offset_expr, AT, ZERO);
252b5132 10670 }
252b5132
RH
10671 break;
10672
10673 case M_BLEUL:
10674 likely = 1;
1a0670f3 10675 /* Fall through. */
252b5132 10676 case M_BLEU:
c0ebe874 10677 if (op[1] == 0)
df58fc94 10678 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874
RS
10679 &offset_expr, op[0], ZERO);
10680 else if (op[0] == 0)
df58fc94
RS
10681 goto do_true;
10682 else
252b5132 10683 {
df58fc94 10684 used_at = 1;
c0ebe874 10685 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10686 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10687 &offset_expr, AT, ZERO);
252b5132 10688 }
252b5132
RH
10689 break;
10690
10691 case M_BLEUL_I:
10692 likely = 1;
1a0670f3 10693 /* Fall through. */
252b5132 10694 case M_BLEU_I:
c0ebe874 10695 if (op[0] == 0
bad1aba3 10696 || (GPR_SIZE == 32
f01dc953 10697 && imm_expr.X_add_number == -1))
252b5132 10698 goto do_true;
f9419b05 10699 ++imm_expr.X_add_number;
6f2117ba 10700 /* Fall through. */
252b5132
RH
10701 case M_BLTU_I:
10702 case M_BLTUL_I:
10703 if (mask == M_BLTUL_I)
10704 likely = 1;
b0e6f033 10705 if (imm_expr.X_add_number == 0)
252b5132 10706 goto do_false;
b0e6f033 10707 else if (imm_expr.X_add_number == 1)
df58fc94 10708 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 10709 &offset_expr, op[0], ZERO);
df58fc94 10710 else
252b5132 10711 {
df58fc94 10712 used_at = 1;
c0ebe874 10713 set_at (op[0], 1);
df58fc94
RS
10714 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10715 &offset_expr, AT, ZERO);
252b5132 10716 }
252b5132
RH
10717 break;
10718
10719 case M_BLTL:
10720 likely = 1;
1a0670f3 10721 /* Fall through. */
252b5132 10722 case M_BLT:
c0ebe874
RS
10723 if (op[1] == 0)
10724 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10725 else if (op[0] == 0)
10726 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[1]);
df58fc94 10727 else
252b5132 10728 {
df58fc94 10729 used_at = 1;
c0ebe874 10730 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10731 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10732 &offset_expr, AT, ZERO);
252b5132 10733 }
252b5132
RH
10734 break;
10735
10736 case M_BLTUL:
10737 likely = 1;
1a0670f3 10738 /* Fall through. */
252b5132 10739 case M_BLTU:
c0ebe874 10740 if (op[1] == 0)
252b5132 10741 goto do_false;
c0ebe874 10742 else if (op[0] == 0)
df58fc94 10743 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 10744 &offset_expr, ZERO, op[1]);
df58fc94 10745 else
252b5132 10746 {
df58fc94 10747 used_at = 1;
c0ebe874 10748 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10749 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10750 &offset_expr, AT, ZERO);
252b5132 10751 }
252b5132
RH
10752 break;
10753
10754 case M_DDIV_3:
10755 dbl = 1;
1a0670f3 10756 /* Fall through. */
252b5132
RH
10757 case M_DIV_3:
10758 s = "mflo";
10759 goto do_div3;
10760 case M_DREM_3:
10761 dbl = 1;
1a0670f3 10762 /* Fall through. */
252b5132
RH
10763 case M_REM_3:
10764 s = "mfhi";
10765 do_div3:
c0ebe874 10766 if (op[2] == 0)
252b5132 10767 {
1661c76c 10768 as_warn (_("divide by zero"));
252b5132 10769 if (mips_trap)
df58fc94 10770 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 10771 else
df58fc94 10772 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 10773 break;
252b5132
RH
10774 }
10775
7d10b47d 10776 start_noreorder ();
252b5132
RH
10777 if (mips_trap)
10778 {
c0ebe874
RS
10779 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10780 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
252b5132
RH
10781 }
10782 else
10783 {
df58fc94
RS
10784 if (mips_opts.micromips)
10785 micromips_label_expr (&label_expr);
10786 else
10787 label_expr.X_add_number = 8;
c0ebe874
RS
10788 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10789 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
df58fc94
RS
10790 macro_build (NULL, "break", BRK_FMT, 7);
10791 if (mips_opts.micromips)
10792 micromips_add_label ();
252b5132
RH
10793 }
10794 expr1.X_add_number = -1;
8fc2e39e 10795 used_at = 1;
f6a22291 10796 load_register (AT, &expr1, dbl);
df58fc94
RS
10797 if (mips_opts.micromips)
10798 micromips_label_expr (&label_expr);
10799 else
10800 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
c0ebe874 10801 macro_build (&label_expr, "bne", "s,t,p", op[2], AT);
252b5132
RH
10802 if (dbl)
10803 {
10804 expr1.X_add_number = 1;
f6a22291 10805 load_register (AT, &expr1, dbl);
df58fc94 10806 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
252b5132
RH
10807 }
10808 else
10809 {
10810 expr1.X_add_number = 0x80000000;
df58fc94 10811 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
252b5132
RH
10812 }
10813 if (mips_trap)
10814 {
c0ebe874 10815 macro_build (NULL, "teq", TRAP_FMT, op[1], AT, 6);
252b5132
RH
10816 /* We want to close the noreorder block as soon as possible, so
10817 that later insns are available for delay slot filling. */
7d10b47d 10818 end_noreorder ();
252b5132
RH
10819 }
10820 else
10821 {
df58fc94
RS
10822 if (mips_opts.micromips)
10823 micromips_label_expr (&label_expr);
10824 else
10825 label_expr.X_add_number = 8;
c0ebe874 10826 macro_build (&label_expr, "bne", "s,t,p", op[1], AT);
a605d2b3 10827 macro_build (NULL, "nop", "");
252b5132
RH
10828
10829 /* We want to close the noreorder block as soon as possible, so
10830 that later insns are available for delay slot filling. */
7d10b47d 10831 end_noreorder ();
252b5132 10832
df58fc94 10833 macro_build (NULL, "break", BRK_FMT, 6);
252b5132 10834 }
df58fc94
RS
10835 if (mips_opts.micromips)
10836 micromips_add_label ();
c0ebe874 10837 macro_build (NULL, s, MFHL_FMT, op[0]);
252b5132
RH
10838 break;
10839
10840 case M_DIV_3I:
10841 s = "div";
10842 s2 = "mflo";
10843 goto do_divi;
10844 case M_DIVU_3I:
10845 s = "divu";
10846 s2 = "mflo";
10847 goto do_divi;
10848 case M_REM_3I:
10849 s = "div";
10850 s2 = "mfhi";
10851 goto do_divi;
10852 case M_REMU_3I:
10853 s = "divu";
10854 s2 = "mfhi";
10855 goto do_divi;
10856 case M_DDIV_3I:
10857 dbl = 1;
10858 s = "ddiv";
10859 s2 = "mflo";
10860 goto do_divi;
10861 case M_DDIVU_3I:
10862 dbl = 1;
10863 s = "ddivu";
10864 s2 = "mflo";
10865 goto do_divi;
10866 case M_DREM_3I:
10867 dbl = 1;
10868 s = "ddiv";
10869 s2 = "mfhi";
10870 goto do_divi;
10871 case M_DREMU_3I:
10872 dbl = 1;
10873 s = "ddivu";
10874 s2 = "mfhi";
10875 do_divi:
b0e6f033 10876 if (imm_expr.X_add_number == 0)
252b5132 10877 {
1661c76c 10878 as_warn (_("divide by zero"));
252b5132 10879 if (mips_trap)
df58fc94 10880 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 10881 else
df58fc94 10882 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 10883 break;
252b5132 10884 }
b0e6f033 10885 if (imm_expr.X_add_number == 1)
252b5132
RH
10886 {
10887 if (strcmp (s2, "mflo") == 0)
c0ebe874 10888 move_register (op[0], op[1]);
252b5132 10889 else
c0ebe874 10890 move_register (op[0], ZERO);
8fc2e39e 10891 break;
252b5132 10892 }
b0e6f033 10893 if (imm_expr.X_add_number == -1 && s[strlen (s) - 1] != 'u')
252b5132
RH
10894 {
10895 if (strcmp (s2, "mflo") == 0)
c0ebe874 10896 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", op[0], op[1]);
252b5132 10897 else
c0ebe874 10898 move_register (op[0], ZERO);
8fc2e39e 10899 break;
252b5132
RH
10900 }
10901
8fc2e39e 10902 used_at = 1;
67c0d1eb 10903 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
10904 macro_build (NULL, s, "z,s,t", op[1], AT);
10905 macro_build (NULL, s2, MFHL_FMT, op[0]);
252b5132
RH
10906 break;
10907
10908 case M_DIVU_3:
10909 s = "divu";
10910 s2 = "mflo";
10911 goto do_divu3;
10912 case M_REMU_3:
10913 s = "divu";
10914 s2 = "mfhi";
10915 goto do_divu3;
10916 case M_DDIVU_3:
10917 s = "ddivu";
10918 s2 = "mflo";
10919 goto do_divu3;
10920 case M_DREMU_3:
10921 s = "ddivu";
10922 s2 = "mfhi";
10923 do_divu3:
7d10b47d 10924 start_noreorder ();
252b5132
RH
10925 if (mips_trap)
10926 {
c0ebe874
RS
10927 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10928 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
10929 /* We want to close the noreorder block as soon as possible, so
10930 that later insns are available for delay slot filling. */
7d10b47d 10931 end_noreorder ();
252b5132
RH
10932 }
10933 else
10934 {
df58fc94
RS
10935 if (mips_opts.micromips)
10936 micromips_label_expr (&label_expr);
10937 else
10938 label_expr.X_add_number = 8;
c0ebe874
RS
10939 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10940 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
10941
10942 /* We want to close the noreorder block as soon as possible, so
10943 that later insns are available for delay slot filling. */
7d10b47d 10944 end_noreorder ();
df58fc94
RS
10945 macro_build (NULL, "break", BRK_FMT, 7);
10946 if (mips_opts.micromips)
10947 micromips_add_label ();
252b5132 10948 }
c0ebe874 10949 macro_build (NULL, s2, MFHL_FMT, op[0]);
8fc2e39e 10950 break;
252b5132 10951
1abe91b1
MR
10952 case M_DLCA_AB:
10953 dbl = 1;
1a0670f3 10954 /* Fall through. */
1abe91b1
MR
10955 case M_LCA_AB:
10956 call = 1;
10957 goto do_la;
252b5132
RH
10958 case M_DLA_AB:
10959 dbl = 1;
1a0670f3 10960 /* Fall through. */
252b5132 10961 case M_LA_AB:
1abe91b1 10962 do_la:
252b5132
RH
10963 /* Load the address of a symbol into a register. If breg is not
10964 zero, we then add a base register to it. */
10965
c0ebe874 10966 breg = op[2];
bad1aba3 10967 if (dbl && GPR_SIZE == 32)
ece794d9
MF
10968 as_warn (_("dla used to load 32-bit register; recommend using la "
10969 "instead"));
3bec30a8 10970
90ecf173 10971 if (!dbl && HAVE_64BIT_OBJECTS)
ece794d9
MF
10972 as_warn (_("la used to load 64-bit address; recommend using dla "
10973 "instead"));
3bec30a8 10974
f2ae14a1 10975 if (small_offset_p (0, align, 16))
0c11417f 10976 {
c0ebe874 10977 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", op[0], breg,
f2ae14a1 10978 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8fc2e39e 10979 break;
0c11417f
MR
10980 }
10981
c0ebe874 10982 if (mips_opts.at && (op[0] == breg))
afdbd6d0
CD
10983 {
10984 tempreg = AT;
10985 used_at = 1;
10986 }
10987 else
c0ebe874 10988 tempreg = op[0];
afdbd6d0 10989
252b5132
RH
10990 if (offset_expr.X_op != O_symbol
10991 && offset_expr.X_op != O_constant)
10992 {
1661c76c 10993 as_bad (_("expression too complex"));
252b5132
RH
10994 offset_expr.X_op = O_constant;
10995 }
10996
252b5132 10997 if (offset_expr.X_op == O_constant)
aed1a261 10998 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
10999 else if (mips_pic == NO_PIC)
11000 {
d6bc6245 11001 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 11002 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
11003 Otherwise we want
11004 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
11005 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
11006 If we have a constant, we need two instructions anyhow,
d6bc6245 11007 so we may as well always use the latter form.
76b3015f 11008
6caf9ef4
TS
11009 With 64bit address space and a usable $at we want
11010 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11011 lui $at,<sym> (BFD_RELOC_HI16_S)
11012 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11013 daddiu $at,<sym> (BFD_RELOC_LO16)
11014 dsll32 $tempreg,0
11015 daddu $tempreg,$tempreg,$at
11016
11017 If $at is already in use, we use a path which is suboptimal
11018 on superscalar processors.
11019 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11020 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11021 dsll $tempreg,16
11022 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
11023 dsll $tempreg,16
11024 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
11025
11026 For GP relative symbols in 64bit address space we can use
11027 the same sequence as in 32bit address space. */
aed1a261 11028 if (HAVE_64BIT_SYMBOLS)
252b5132 11029 {
6caf9ef4
TS
11030 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
11031 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
11032 {
11033 relax_start (offset_expr.X_add_symbol);
11034 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11035 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
11036 relax_switch ();
11037 }
d6bc6245 11038
741fe287 11039 if (used_at == 0 && mips_opts.at)
98d3f06f 11040 {
df58fc94 11041 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 11042 tempreg, BFD_RELOC_MIPS_HIGHEST);
df58fc94 11043 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 11044 AT, BFD_RELOC_HI16_S);
67c0d1eb 11045 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 11046 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 11047 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 11048 AT, AT, BFD_RELOC_LO16);
df58fc94 11049 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 11050 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
11051 used_at = 1;
11052 }
11053 else
11054 {
df58fc94 11055 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 11056 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 11057 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 11058 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 11059 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 11060 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 11061 tempreg, tempreg, BFD_RELOC_HI16_S);
df58fc94 11062 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 11063 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 11064 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 11065 }
6caf9ef4
TS
11066
11067 if (mips_relax.sequence)
11068 relax_end ();
98d3f06f
KH
11069 }
11070 else
11071 {
11072 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 11073 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 11074 {
4d7206a2 11075 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11076 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11077 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 11078 relax_switch ();
98d3f06f 11079 }
6943caf0 11080 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
1661c76c 11081 as_bad (_("offset too large"));
67c0d1eb
RS
11082 macro_build_lui (&offset_expr, tempreg);
11083 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11084 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
11085 if (mips_relax.sequence)
11086 relax_end ();
98d3f06f 11087 }
252b5132 11088 }
0a44bf69 11089 else if (!mips_big_got && !HAVE_NEWABI)
252b5132 11090 {
9117d219
NC
11091 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
11092
252b5132
RH
11093 /* If this is a reference to an external symbol, and there
11094 is no constant, we want
11095 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 11096 or for lca or if tempreg is PIC_CALL_REG
9117d219 11097 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
11098 For a local symbol, we want
11099 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11100 nop
11101 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
11102
11103 If we have a small constant, and this is a reference to
11104 an external symbol, we want
11105 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11106 nop
11107 addiu $tempreg,$tempreg,<constant>
11108 For a local symbol, we want the same instruction
11109 sequence, but we output a BFD_RELOC_LO16 reloc on the
11110 addiu instruction.
11111
11112 If we have a large constant, and this is a reference to
11113 an external symbol, we want
11114 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11115 lui $at,<hiconstant>
11116 addiu $at,$at,<loconstant>
11117 addu $tempreg,$tempreg,$at
11118 For a local symbol, we want the same instruction
11119 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 11120 addiu instruction.
ed6fb7bd
SC
11121 */
11122
4d7206a2 11123 if (offset_expr.X_add_number == 0)
252b5132 11124 {
0a44bf69
RS
11125 if (mips_pic == SVR4_PIC
11126 && breg == 0
11127 && (call || tempreg == PIC_CALL_REG))
4d7206a2
RS
11128 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
11129
11130 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11131 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11132 lw_reloc_type, mips_gp_register);
4d7206a2 11133 if (breg != 0)
252b5132
RH
11134 {
11135 /* We're going to put in an addu instruction using
11136 tempreg, so we may as well insert the nop right
11137 now. */
269137b2 11138 load_delay_nop ();
252b5132 11139 }
4d7206a2 11140 relax_switch ();
67c0d1eb
RS
11141 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11142 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 11143 load_delay_nop ();
67c0d1eb
RS
11144 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11145 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 11146 relax_end ();
252b5132
RH
11147 /* FIXME: If breg == 0, and the next instruction uses
11148 $tempreg, then if this variant case is used an extra
11149 nop will be generated. */
11150 }
4d7206a2
RS
11151 else if (offset_expr.X_add_number >= -0x8000
11152 && offset_expr.X_add_number < 0x8000)
252b5132 11153 {
67c0d1eb 11154 load_got_offset (tempreg, &offset_expr);
269137b2 11155 load_delay_nop ();
67c0d1eb 11156 add_got_offset (tempreg, &offset_expr);
252b5132
RH
11157 }
11158 else
11159 {
4d7206a2
RS
11160 expr1.X_add_number = offset_expr.X_add_number;
11161 offset_expr.X_add_number =
43c0598f 11162 SEXT_16BIT (offset_expr.X_add_number);
67c0d1eb 11163 load_got_offset (tempreg, &offset_expr);
f6a22291 11164 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
11165 /* If we are going to add in a base register, and the
11166 target register and the base register are the same,
11167 then we are using AT as a temporary register. Since
11168 we want to load the constant into AT, we add our
11169 current AT (from the global offset table) and the
11170 register into the register now, and pretend we were
11171 not using a base register. */
c0ebe874 11172 if (breg == op[0])
252b5132 11173 {
269137b2 11174 load_delay_nop ();
67c0d1eb 11175 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 11176 op[0], AT, breg);
252b5132 11177 breg = 0;
c0ebe874 11178 tempreg = op[0];
252b5132 11179 }
f6a22291 11180 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
11181 used_at = 1;
11182 }
11183 }
0a44bf69 11184 else if (!mips_big_got && HAVE_NEWABI)
f5040a92 11185 {
67c0d1eb 11186 int add_breg_early = 0;
f5040a92
AO
11187
11188 /* If this is a reference to an external, and there is no
11189 constant, or local symbol (*), with or without a
11190 constant, we want
11191 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 11192 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
11193 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11194
11195 If we have a small constant, and this is a reference to
11196 an external symbol, we want
11197 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
11198 addiu $tempreg,$tempreg,<constant>
11199
11200 If we have a large constant, and this is a reference to
11201 an external symbol, we want
11202 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
11203 lui $at,<hiconstant>
11204 addiu $at,$at,<loconstant>
11205 addu $tempreg,$tempreg,$at
11206
11207 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
11208 local symbols, even though it introduces an additional
11209 instruction. */
11210
f5040a92
AO
11211 if (offset_expr.X_add_number)
11212 {
4d7206a2 11213 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
11214 offset_expr.X_add_number = 0;
11215
4d7206a2 11216 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11217 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11218 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
11219
11220 if (expr1.X_add_number >= -0x8000
11221 && expr1.X_add_number < 0x8000)
11222 {
67c0d1eb
RS
11223 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11224 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 11225 }
ecd13cd3 11226 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 11227 {
c0ebe874
RS
11228 unsigned int dreg;
11229
f5040a92
AO
11230 /* If we are going to add in a base register, and the
11231 target register and the base register are the same,
11232 then we are using AT as a temporary register. Since
11233 we want to load the constant into AT, we add our
11234 current AT (from the global offset table) and the
11235 register into the register now, and pretend we were
11236 not using a base register. */
c0ebe874 11237 if (breg != op[0])
f5040a92
AO
11238 dreg = tempreg;
11239 else
11240 {
9c2799c2 11241 gas_assert (tempreg == AT);
67c0d1eb 11242 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11243 op[0], AT, breg);
11244 dreg = op[0];
67c0d1eb 11245 add_breg_early = 1;
f5040a92
AO
11246 }
11247
f6a22291 11248 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 11249 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11250 dreg, dreg, AT);
f5040a92 11251
f5040a92
AO
11252 used_at = 1;
11253 }
11254 else
11255 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11256
4d7206a2 11257 relax_switch ();
f5040a92
AO
11258 offset_expr.X_add_number = expr1.X_add_number;
11259
67c0d1eb
RS
11260 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11261 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11262 if (add_breg_early)
f5040a92 11263 {
67c0d1eb 11264 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 11265 op[0], tempreg, breg);
f5040a92 11266 breg = 0;
c0ebe874 11267 tempreg = op[0];
f5040a92 11268 }
4d7206a2 11269 relax_end ();
f5040a92 11270 }
4d7206a2 11271 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 11272 {
4d7206a2 11273 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11274 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11275 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 11276 relax_switch ();
67c0d1eb
RS
11277 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11278 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 11279 relax_end ();
f5040a92 11280 }
4d7206a2 11281 else
f5040a92 11282 {
67c0d1eb
RS
11283 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11284 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
11285 }
11286 }
0a44bf69 11287 else if (mips_big_got && !HAVE_NEWABI)
252b5132 11288 {
67c0d1eb 11289 int gpdelay;
9117d219
NC
11290 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11291 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 11292 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
11293
11294 /* This is the large GOT case. If this is a reference to an
11295 external symbol, and there is no constant, we want
11296 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11297 addu $tempreg,$tempreg,$gp
11298 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 11299 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
11300 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11301 addu $tempreg,$tempreg,$gp
11302 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
11303 For a local symbol, we want
11304 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11305 nop
11306 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
11307
11308 If we have a small constant, and this is a reference to
11309 an external symbol, we want
11310 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11311 addu $tempreg,$tempreg,$gp
11312 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11313 nop
11314 addiu $tempreg,$tempreg,<constant>
11315 For a local symbol, we want
11316 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11317 nop
11318 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
11319
11320 If we have a large constant, and this is a reference to
11321 an external symbol, we want
11322 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11323 addu $tempreg,$tempreg,$gp
11324 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11325 lui $at,<hiconstant>
11326 addiu $at,$at,<loconstant>
11327 addu $tempreg,$tempreg,$at
11328 For a local symbol, we want
11329 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11330 lui $at,<hiconstant>
11331 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
11332 addu $tempreg,$tempreg,$at
f5040a92 11333 */
438c16b8 11334
252b5132
RH
11335 expr1.X_add_number = offset_expr.X_add_number;
11336 offset_expr.X_add_number = 0;
4d7206a2 11337 relax_start (offset_expr.X_add_symbol);
67c0d1eb 11338 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
11339 if (expr1.X_add_number == 0 && breg == 0
11340 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
11341 {
11342 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11343 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11344 }
df58fc94 11345 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 11346 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11347 tempreg, tempreg, mips_gp_register);
67c0d1eb 11348 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 11349 tempreg, lw_reloc_type, tempreg);
252b5132
RH
11350 if (expr1.X_add_number == 0)
11351 {
67c0d1eb 11352 if (breg != 0)
252b5132
RH
11353 {
11354 /* We're going to put in an addu instruction using
11355 tempreg, so we may as well insert the nop right
11356 now. */
269137b2 11357 load_delay_nop ();
252b5132 11358 }
252b5132
RH
11359 }
11360 else if (expr1.X_add_number >= -0x8000
11361 && expr1.X_add_number < 0x8000)
11362 {
269137b2 11363 load_delay_nop ();
67c0d1eb 11364 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11365 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
11366 }
11367 else
11368 {
c0ebe874
RS
11369 unsigned int dreg;
11370
252b5132
RH
11371 /* If we are going to add in a base register, and the
11372 target register and the base register are the same,
11373 then we are using AT as a temporary register. Since
11374 we want to load the constant into AT, we add our
11375 current AT (from the global offset table) and the
11376 register into the register now, and pretend we were
11377 not using a base register. */
c0ebe874 11378 if (breg != op[0])
67c0d1eb 11379 dreg = tempreg;
252b5132
RH
11380 else
11381 {
9c2799c2 11382 gas_assert (tempreg == AT);
269137b2 11383 load_delay_nop ();
67c0d1eb 11384 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11385 op[0], AT, breg);
11386 dreg = op[0];
252b5132
RH
11387 }
11388
f6a22291 11389 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 11390 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 11391
252b5132
RH
11392 used_at = 1;
11393 }
43c0598f 11394 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
4d7206a2 11395 relax_switch ();
252b5132 11396
67c0d1eb 11397 if (gpdelay)
252b5132
RH
11398 {
11399 /* This is needed because this instruction uses $gp, but
f5040a92 11400 the first instruction on the main stream does not. */
67c0d1eb 11401 macro_build (NULL, "nop", "");
252b5132 11402 }
ed6fb7bd 11403
67c0d1eb
RS
11404 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11405 local_reloc_type, mips_gp_register);
f5040a92 11406 if (expr1.X_add_number >= -0x8000
252b5132
RH
11407 && expr1.X_add_number < 0x8000)
11408 {
269137b2 11409 load_delay_nop ();
67c0d1eb
RS
11410 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11411 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 11412 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
11413 register, the external symbol case ended with a load,
11414 so if the symbol turns out to not be external, and
11415 the next instruction uses tempreg, an unnecessary nop
11416 will be inserted. */
252b5132
RH
11417 }
11418 else
11419 {
c0ebe874 11420 if (breg == op[0])
252b5132
RH
11421 {
11422 /* We must add in the base register now, as in the
f5040a92 11423 external symbol case. */
9c2799c2 11424 gas_assert (tempreg == AT);
269137b2 11425 load_delay_nop ();
67c0d1eb 11426 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11427 op[0], AT, breg);
11428 tempreg = op[0];
252b5132 11429 /* We set breg to 0 because we have arranged to add
f5040a92 11430 it in in both cases. */
252b5132
RH
11431 breg = 0;
11432 }
11433
67c0d1eb
RS
11434 macro_build_lui (&expr1, AT);
11435 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11436 AT, AT, BFD_RELOC_LO16);
67c0d1eb 11437 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11438 tempreg, tempreg, AT);
8fc2e39e 11439 used_at = 1;
252b5132 11440 }
4d7206a2 11441 relax_end ();
252b5132 11442 }
0a44bf69 11443 else if (mips_big_got && HAVE_NEWABI)
f5040a92 11444 {
f5040a92
AO
11445 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11446 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 11447 int add_breg_early = 0;
f5040a92
AO
11448
11449 /* This is the large GOT case. If this is a reference to an
11450 external symbol, and there is no constant, we want
11451 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11452 add $tempreg,$tempreg,$gp
11453 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 11454 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
11455 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11456 add $tempreg,$tempreg,$gp
11457 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
11458
11459 If we have a small constant, and this is a reference to
11460 an external symbol, we want
11461 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11462 add $tempreg,$tempreg,$gp
11463 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11464 addi $tempreg,$tempreg,<constant>
11465
11466 If we have a large constant, and this is a reference to
11467 an external symbol, we want
11468 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11469 addu $tempreg,$tempreg,$gp
11470 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11471 lui $at,<hiconstant>
11472 addi $at,$at,<loconstant>
11473 add $tempreg,$tempreg,$at
11474
11475 If we have NewABI, and we know it's a local symbol, we want
11476 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
11477 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
11478 otherwise we have to resort to GOT_HI16/GOT_LO16. */
11479
4d7206a2 11480 relax_start (offset_expr.X_add_symbol);
f5040a92 11481
4d7206a2 11482 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
11483 offset_expr.X_add_number = 0;
11484
1abe91b1
MR
11485 if (expr1.X_add_number == 0 && breg == 0
11486 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
11487 {
11488 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11489 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11490 }
df58fc94 11491 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 11492 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11493 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
11494 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11495 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
11496
11497 if (expr1.X_add_number == 0)
4d7206a2 11498 ;
f5040a92
AO
11499 else if (expr1.X_add_number >= -0x8000
11500 && expr1.X_add_number < 0x8000)
11501 {
67c0d1eb 11502 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11503 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 11504 }
ecd13cd3 11505 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 11506 {
c0ebe874
RS
11507 unsigned int dreg;
11508
f5040a92
AO
11509 /* If we are going to add in a base register, and the
11510 target register and the base register are the same,
11511 then we are using AT as a temporary register. Since
11512 we want to load the constant into AT, we add our
11513 current AT (from the global offset table) and the
11514 register into the register now, and pretend we were
11515 not using a base register. */
c0ebe874 11516 if (breg != op[0])
f5040a92
AO
11517 dreg = tempreg;
11518 else
11519 {
9c2799c2 11520 gas_assert (tempreg == AT);
67c0d1eb 11521 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11522 op[0], AT, breg);
11523 dreg = op[0];
67c0d1eb 11524 add_breg_early = 1;
f5040a92
AO
11525 }
11526
f6a22291 11527 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 11528 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 11529
f5040a92
AO
11530 used_at = 1;
11531 }
11532 else
11533 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11534
4d7206a2 11535 relax_switch ();
f5040a92 11536 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
11537 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11538 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
11539 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11540 tempreg, BFD_RELOC_MIPS_GOT_OFST);
11541 if (add_breg_early)
f5040a92 11542 {
67c0d1eb 11543 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 11544 op[0], tempreg, breg);
f5040a92 11545 breg = 0;
c0ebe874 11546 tempreg = op[0];
f5040a92 11547 }
4d7206a2 11548 relax_end ();
f5040a92 11549 }
252b5132
RH
11550 else
11551 abort ();
11552
11553 if (breg != 0)
c0ebe874 11554 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", op[0], tempreg, breg);
252b5132
RH
11555 break;
11556
52b6b6b9 11557 case M_MSGSND:
df58fc94 11558 gas_assert (!mips_opts.micromips);
c0ebe874 11559 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x01);
c7af4273 11560 break;
52b6b6b9
JM
11561
11562 case M_MSGLD:
df58fc94 11563 gas_assert (!mips_opts.micromips);
c8276761 11564 macro_build (NULL, "c2", "C", 0x02);
c7af4273 11565 break;
52b6b6b9
JM
11566
11567 case M_MSGLD_T:
df58fc94 11568 gas_assert (!mips_opts.micromips);
c0ebe874 11569 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x02);
c7af4273 11570 break;
52b6b6b9
JM
11571
11572 case M_MSGWAIT:
df58fc94 11573 gas_assert (!mips_opts.micromips);
52b6b6b9 11574 macro_build (NULL, "c2", "C", 3);
c7af4273 11575 break;
52b6b6b9
JM
11576
11577 case M_MSGWAIT_T:
df58fc94 11578 gas_assert (!mips_opts.micromips);
c0ebe874 11579 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x03);
c7af4273 11580 break;
52b6b6b9 11581
252b5132
RH
11582 case M_J_A:
11583 /* The j instruction may not be used in PIC code, since it
11584 requires an absolute address. We convert it to a b
11585 instruction. */
11586 if (mips_pic == NO_PIC)
67c0d1eb 11587 macro_build (&offset_expr, "j", "a");
252b5132 11588 else
67c0d1eb 11589 macro_build (&offset_expr, "b", "p");
8fc2e39e 11590 break;
252b5132
RH
11591
11592 /* The jal instructions must be handled as macros because when
11593 generating PIC code they expand to multi-instruction
11594 sequences. Normally they are simple instructions. */
df58fc94 11595 case M_JALS_1:
c0ebe874
RS
11596 op[1] = op[0];
11597 op[0] = RA;
df58fc94
RS
11598 /* Fall through. */
11599 case M_JALS_2:
11600 gas_assert (mips_opts.micromips);
833794fc
MR
11601 if (mips_opts.insn32)
11602 {
1661c76c 11603 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
833794fc
MR
11604 break;
11605 }
df58fc94
RS
11606 jals = 1;
11607 goto jal;
252b5132 11608 case M_JAL_1:
c0ebe874
RS
11609 op[1] = op[0];
11610 op[0] = RA;
252b5132
RH
11611 /* Fall through. */
11612 case M_JAL_2:
df58fc94 11613 jal:
3e722fb5 11614 if (mips_pic == NO_PIC)
df58fc94
RS
11615 {
11616 s = jals ? "jalrs" : "jalr";
e64af278 11617 if (mips_opts.micromips
833794fc 11618 && !mips_opts.insn32
c0ebe874 11619 && op[0] == RA
e64af278 11620 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 11621 macro_build (NULL, s, "mj", op[1]);
df58fc94 11622 else
c0ebe874 11623 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
df58fc94 11624 }
0a44bf69 11625 else
252b5132 11626 {
df58fc94
RS
11627 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
11628 && mips_cprestore_offset >= 0);
11629
c0ebe874 11630 if (op[1] != PIC_CALL_REG)
252b5132 11631 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 11632
833794fc
MR
11633 s = ((mips_opts.micromips
11634 && !mips_opts.insn32
11635 && (!mips_opts.noreorder || cprestore))
df58fc94 11636 ? "jalrs" : "jalr");
e64af278 11637 if (mips_opts.micromips
833794fc 11638 && !mips_opts.insn32
c0ebe874 11639 && op[0] == RA
e64af278 11640 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 11641 macro_build (NULL, s, "mj", op[1]);
df58fc94 11642 else
c0ebe874 11643 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
0a44bf69 11644 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
252b5132 11645 {
6478892d 11646 if (mips_cprestore_offset < 0)
1661c76c 11647 as_warn (_("no .cprestore pseudo-op used in PIC code"));
6478892d
TS
11648 else
11649 {
90ecf173 11650 if (!mips_frame_reg_valid)
7a621144 11651 {
1661c76c 11652 as_warn (_("no .frame pseudo-op used in PIC code"));
7a621144
DJ
11653 /* Quiet this warning. */
11654 mips_frame_reg_valid = 1;
11655 }
90ecf173 11656 if (!mips_cprestore_valid)
7a621144 11657 {
1661c76c 11658 as_warn (_("no .cprestore pseudo-op used in PIC code"));
7a621144
DJ
11659 /* Quiet this warning. */
11660 mips_cprestore_valid = 1;
11661 }
d3fca0b5
MR
11662 if (mips_opts.noreorder)
11663 macro_build (NULL, "nop", "");
6478892d 11664 expr1.X_add_number = mips_cprestore_offset;
134c0c8b 11665 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 11666 mips_gp_register,
256ab948
TS
11667 mips_frame_reg,
11668 HAVE_64BIT_ADDRESSES);
6478892d 11669 }
252b5132
RH
11670 }
11671 }
252b5132 11672
8fc2e39e 11673 break;
252b5132 11674
df58fc94
RS
11675 case M_JALS_A:
11676 gas_assert (mips_opts.micromips);
833794fc
MR
11677 if (mips_opts.insn32)
11678 {
1661c76c 11679 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
833794fc
MR
11680 break;
11681 }
df58fc94
RS
11682 jals = 1;
11683 /* Fall through. */
252b5132
RH
11684 case M_JAL_A:
11685 if (mips_pic == NO_PIC)
df58fc94 11686 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
252b5132
RH
11687 else if (mips_pic == SVR4_PIC)
11688 {
11689 /* If this is a reference to an external symbol, and we are
11690 using a small GOT, we want
11691 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11692 nop
f9419b05 11693 jalr $ra,$25
252b5132
RH
11694 nop
11695 lw $gp,cprestore($sp)
11696 The cprestore value is set using the .cprestore
11697 pseudo-op. If we are using a big GOT, we want
11698 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11699 addu $25,$25,$gp
11700 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
11701 nop
f9419b05 11702 jalr $ra,$25
252b5132
RH
11703 nop
11704 lw $gp,cprestore($sp)
11705 If the symbol is not external, we want
11706 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11707 nop
11708 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 11709 jalr $ra,$25
252b5132 11710 nop
438c16b8 11711 lw $gp,cprestore($sp)
f5040a92
AO
11712
11713 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
11714 sequences above, minus nops, unless the symbol is local,
11715 which enables us to use GOT_PAGE/GOT_OFST (big got) or
11716 GOT_DISP. */
438c16b8 11717 if (HAVE_NEWABI)
252b5132 11718 {
90ecf173 11719 if (!mips_big_got)
f5040a92 11720 {
4d7206a2 11721 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11722 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11723 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 11724 mips_gp_register);
4d7206a2 11725 relax_switch ();
67c0d1eb
RS
11726 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11727 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
11728 mips_gp_register);
11729 relax_end ();
f5040a92
AO
11730 }
11731 else
11732 {
4d7206a2 11733 relax_start (offset_expr.X_add_symbol);
df58fc94 11734 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
11735 BFD_RELOC_MIPS_CALL_HI16);
11736 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11737 PIC_CALL_REG, mips_gp_register);
11738 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11739 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11740 PIC_CALL_REG);
4d7206a2 11741 relax_switch ();
67c0d1eb
RS
11742 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11743 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
11744 mips_gp_register);
11745 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11746 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 11747 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 11748 relax_end ();
f5040a92 11749 }
684022ea 11750
df58fc94 11751 macro_build_jalr (&offset_expr, 0);
252b5132
RH
11752 }
11753 else
11754 {
4d7206a2 11755 relax_start (offset_expr.X_add_symbol);
90ecf173 11756 if (!mips_big_got)
438c16b8 11757 {
67c0d1eb
RS
11758 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11759 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 11760 mips_gp_register);
269137b2 11761 load_delay_nop ();
4d7206a2 11762 relax_switch ();
438c16b8 11763 }
252b5132 11764 else
252b5132 11765 {
67c0d1eb
RS
11766 int gpdelay;
11767
11768 gpdelay = reg_needs_delay (mips_gp_register);
df58fc94 11769 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
11770 BFD_RELOC_MIPS_CALL_HI16);
11771 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11772 PIC_CALL_REG, mips_gp_register);
11773 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11774 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11775 PIC_CALL_REG);
269137b2 11776 load_delay_nop ();
4d7206a2 11777 relax_switch ();
67c0d1eb
RS
11778 if (gpdelay)
11779 macro_build (NULL, "nop", "");
252b5132 11780 }
67c0d1eb
RS
11781 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11782 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 11783 mips_gp_register);
269137b2 11784 load_delay_nop ();
67c0d1eb
RS
11785 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11786 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 11787 relax_end ();
df58fc94 11788 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
438c16b8 11789
6478892d 11790 if (mips_cprestore_offset < 0)
1661c76c 11791 as_warn (_("no .cprestore pseudo-op used in PIC code"));
6478892d
TS
11792 else
11793 {
90ecf173 11794 if (!mips_frame_reg_valid)
7a621144 11795 {
1661c76c 11796 as_warn (_("no .frame pseudo-op used in PIC code"));
7a621144
DJ
11797 /* Quiet this warning. */
11798 mips_frame_reg_valid = 1;
11799 }
90ecf173 11800 if (!mips_cprestore_valid)
7a621144 11801 {
1661c76c 11802 as_warn (_("no .cprestore pseudo-op used in PIC code"));
7a621144
DJ
11803 /* Quiet this warning. */
11804 mips_cprestore_valid = 1;
11805 }
6478892d 11806 if (mips_opts.noreorder)
67c0d1eb 11807 macro_build (NULL, "nop", "");
6478892d 11808 expr1.X_add_number = mips_cprestore_offset;
134c0c8b 11809 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 11810 mips_gp_register,
256ab948
TS
11811 mips_frame_reg,
11812 HAVE_64BIT_ADDRESSES);
6478892d 11813 }
252b5132
RH
11814 }
11815 }
0a44bf69 11816 else if (mips_pic == VXWORKS_PIC)
1661c76c 11817 as_bad (_("non-PIC jump used in PIC library"));
252b5132
RH
11818 else
11819 abort ();
11820
8fc2e39e 11821 break;
252b5132 11822
7f3c4072 11823 case M_LBUE_AB:
7f3c4072
CM
11824 s = "lbue";
11825 fmt = "t,+j(b)";
11826 offbits = 9;
11827 goto ld_st;
11828 case M_LHUE_AB:
7f3c4072
CM
11829 s = "lhue";
11830 fmt = "t,+j(b)";
11831 offbits = 9;
11832 goto ld_st;
11833 case M_LBE_AB:
7f3c4072
CM
11834 s = "lbe";
11835 fmt = "t,+j(b)";
11836 offbits = 9;
11837 goto ld_st;
11838 case M_LHE_AB:
7f3c4072
CM
11839 s = "lhe";
11840 fmt = "t,+j(b)";
11841 offbits = 9;
11842 goto ld_st;
11843 case M_LLE_AB:
7f3c4072
CM
11844 s = "lle";
11845 fmt = "t,+j(b)";
11846 offbits = 9;
11847 goto ld_st;
11848 case M_LWE_AB:
7f3c4072
CM
11849 s = "lwe";
11850 fmt = "t,+j(b)";
11851 offbits = 9;
11852 goto ld_st;
11853 case M_LWLE_AB:
7f3c4072
CM
11854 s = "lwle";
11855 fmt = "t,+j(b)";
11856 offbits = 9;
11857 goto ld_st;
11858 case M_LWRE_AB:
7f3c4072
CM
11859 s = "lwre";
11860 fmt = "t,+j(b)";
11861 offbits = 9;
11862 goto ld_st;
11863 case M_SBE_AB:
7f3c4072
CM
11864 s = "sbe";
11865 fmt = "t,+j(b)";
11866 offbits = 9;
11867 goto ld_st;
11868 case M_SCE_AB:
7f3c4072
CM
11869 s = "sce";
11870 fmt = "t,+j(b)";
11871 offbits = 9;
11872 goto ld_st;
11873 case M_SHE_AB:
7f3c4072
CM
11874 s = "she";
11875 fmt = "t,+j(b)";
11876 offbits = 9;
11877 goto ld_st;
11878 case M_SWE_AB:
7f3c4072
CM
11879 s = "swe";
11880 fmt = "t,+j(b)";
11881 offbits = 9;
11882 goto ld_st;
11883 case M_SWLE_AB:
7f3c4072
CM
11884 s = "swle";
11885 fmt = "t,+j(b)";
11886 offbits = 9;
11887 goto ld_st;
11888 case M_SWRE_AB:
7f3c4072
CM
11889 s = "swre";
11890 fmt = "t,+j(b)";
11891 offbits = 9;
11892 goto ld_st;
dec0624d 11893 case M_ACLR_AB:
dec0624d 11894 s = "aclr";
dec0624d 11895 fmt = "\\,~(b)";
7f3c4072 11896 offbits = 12;
dec0624d
MR
11897 goto ld_st;
11898 case M_ASET_AB:
dec0624d 11899 s = "aset";
dec0624d 11900 fmt = "\\,~(b)";
7f3c4072 11901 offbits = 12;
dec0624d 11902 goto ld_st;
252b5132
RH
11903 case M_LB_AB:
11904 s = "lb";
df58fc94 11905 fmt = "t,o(b)";
252b5132
RH
11906 goto ld;
11907 case M_LBU_AB:
11908 s = "lbu";
df58fc94 11909 fmt = "t,o(b)";
252b5132
RH
11910 goto ld;
11911 case M_LH_AB:
11912 s = "lh";
df58fc94 11913 fmt = "t,o(b)";
252b5132
RH
11914 goto ld;
11915 case M_LHU_AB:
11916 s = "lhu";
df58fc94 11917 fmt = "t,o(b)";
252b5132
RH
11918 goto ld;
11919 case M_LW_AB:
11920 s = "lw";
df58fc94 11921 fmt = "t,o(b)";
252b5132
RH
11922 goto ld;
11923 case M_LWC0_AB:
df58fc94 11924 gas_assert (!mips_opts.micromips);
252b5132 11925 s = "lwc0";
df58fc94 11926 fmt = "E,o(b)";
bdaaa2e1 11927 /* Itbl support may require additional care here. */
252b5132 11928 coproc = 1;
df58fc94 11929 goto ld_st;
252b5132
RH
11930 case M_LWC1_AB:
11931 s = "lwc1";
df58fc94 11932 fmt = "T,o(b)";
bdaaa2e1 11933 /* Itbl support may require additional care here. */
252b5132 11934 coproc = 1;
df58fc94 11935 goto ld_st;
252b5132
RH
11936 case M_LWC2_AB:
11937 s = "lwc2";
df58fc94 11938 fmt = COP12_FMT;
7361da2c
AB
11939 offbits = (mips_opts.micromips ? 12
11940 : ISA_IS_R6 (mips_opts.isa) ? 11
11941 : 16);
bdaaa2e1 11942 /* Itbl support may require additional care here. */
252b5132 11943 coproc = 1;
df58fc94 11944 goto ld_st;
252b5132 11945 case M_LWC3_AB:
df58fc94 11946 gas_assert (!mips_opts.micromips);
252b5132 11947 s = "lwc3";
df58fc94 11948 fmt = "E,o(b)";
bdaaa2e1 11949 /* Itbl support may require additional care here. */
252b5132 11950 coproc = 1;
df58fc94 11951 goto ld_st;
252b5132
RH
11952 case M_LWL_AB:
11953 s = "lwl";
df58fc94 11954 fmt = MEM12_FMT;
7f3c4072 11955 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11956 goto ld_st;
252b5132
RH
11957 case M_LWR_AB:
11958 s = "lwr";
df58fc94 11959 fmt = MEM12_FMT;
7f3c4072 11960 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11961 goto ld_st;
252b5132 11962 case M_LDC1_AB:
252b5132 11963 s = "ldc1";
df58fc94 11964 fmt = "T,o(b)";
bdaaa2e1 11965 /* Itbl support may require additional care here. */
252b5132 11966 coproc = 1;
df58fc94 11967 goto ld_st;
252b5132
RH
11968 case M_LDC2_AB:
11969 s = "ldc2";
df58fc94 11970 fmt = COP12_FMT;
7361da2c
AB
11971 offbits = (mips_opts.micromips ? 12
11972 : ISA_IS_R6 (mips_opts.isa) ? 11
11973 : 16);
bdaaa2e1 11974 /* Itbl support may require additional care here. */
252b5132 11975 coproc = 1;
df58fc94 11976 goto ld_st;
c77c0862 11977 case M_LQC2_AB:
c77c0862 11978 s = "lqc2";
14daeee3 11979 fmt = "+7,o(b)";
c77c0862
RS
11980 /* Itbl support may require additional care here. */
11981 coproc = 1;
11982 goto ld_st;
252b5132
RH
11983 case M_LDC3_AB:
11984 s = "ldc3";
df58fc94 11985 fmt = "E,o(b)";
bdaaa2e1 11986 /* Itbl support may require additional care here. */
252b5132 11987 coproc = 1;
df58fc94 11988 goto ld_st;
252b5132
RH
11989 case M_LDL_AB:
11990 s = "ldl";
df58fc94 11991 fmt = MEM12_FMT;
7f3c4072 11992 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11993 goto ld_st;
252b5132
RH
11994 case M_LDR_AB:
11995 s = "ldr";
df58fc94 11996 fmt = MEM12_FMT;
7f3c4072 11997 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11998 goto ld_st;
252b5132
RH
11999 case M_LL_AB:
12000 s = "ll";
7361da2c
AB
12001 fmt = LL_SC_FMT;
12002 offbits = (mips_opts.micromips ? 12
12003 : ISA_IS_R6 (mips_opts.isa) ? 9
12004 : 16);
252b5132
RH
12005 goto ld;
12006 case M_LLD_AB:
12007 s = "lld";
7361da2c
AB
12008 fmt = LL_SC_FMT;
12009 offbits = (mips_opts.micromips ? 12
12010 : ISA_IS_R6 (mips_opts.isa) ? 9
12011 : 16);
252b5132
RH
12012 goto ld;
12013 case M_LWU_AB:
12014 s = "lwu";
df58fc94 12015 fmt = MEM12_FMT;
7f3c4072 12016 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
12017 goto ld;
12018 case M_LWP_AB:
df58fc94
RS
12019 gas_assert (mips_opts.micromips);
12020 s = "lwp";
12021 fmt = "t,~(b)";
7f3c4072 12022 offbits = 12;
df58fc94
RS
12023 lp = 1;
12024 goto ld;
12025 case M_LDP_AB:
df58fc94
RS
12026 gas_assert (mips_opts.micromips);
12027 s = "ldp";
12028 fmt = "t,~(b)";
7f3c4072 12029 offbits = 12;
df58fc94
RS
12030 lp = 1;
12031 goto ld;
a45328b9
AB
12032 case M_LLDP_AB:
12033 case M_LLWP_AB:
41cee089 12034 case M_LLWPE_AB:
a45328b9
AB
12035 s = ip->insn_mo->name;
12036 fmt = "t,d,s";
12037 ll_sc_paired = 1;
12038 offbits = 0;
12039 goto ld;
df58fc94 12040 case M_LWM_AB:
df58fc94
RS
12041 gas_assert (mips_opts.micromips);
12042 s = "lwm";
12043 fmt = "n,~(b)";
7f3c4072 12044 offbits = 12;
df58fc94
RS
12045 goto ld_st;
12046 case M_LDM_AB:
df58fc94
RS
12047 gas_assert (mips_opts.micromips);
12048 s = "ldm";
12049 fmt = "n,~(b)";
7f3c4072 12050 offbits = 12;
df58fc94
RS
12051 goto ld_st;
12052
252b5132 12053 ld:
a45328b9
AB
12054 /* Try to use one the the load registers to compute the base address.
12055 We don't want to use $0 as tempreg. */
12056 if (ll_sc_paired)
12057 {
12058 if ((op[0] == ZERO && op[3] == op[1])
12059 || (op[1] == ZERO && op[3] == op[0])
12060 || (op[0] == ZERO && op[1] == ZERO))
12061 goto ld_st;
12062 else if (op[0] != op[3] && op[0] != ZERO)
12063 tempreg = op[0];
12064 else
12065 tempreg = op[1];
12066 }
252b5132 12067 else
a45328b9
AB
12068 {
12069 if (op[2] == op[0] + lp || op[0] + lp == ZERO)
12070 goto ld_st;
12071 else
12072 tempreg = op[0] + lp;
12073 }
df58fc94
RS
12074 goto ld_noat;
12075
252b5132
RH
12076 case M_SB_AB:
12077 s = "sb";
df58fc94
RS
12078 fmt = "t,o(b)";
12079 goto ld_st;
252b5132
RH
12080 case M_SH_AB:
12081 s = "sh";
df58fc94
RS
12082 fmt = "t,o(b)";
12083 goto ld_st;
252b5132
RH
12084 case M_SW_AB:
12085 s = "sw";
df58fc94
RS
12086 fmt = "t,o(b)";
12087 goto ld_st;
252b5132 12088 case M_SWC0_AB:
df58fc94 12089 gas_assert (!mips_opts.micromips);
252b5132 12090 s = "swc0";
df58fc94 12091 fmt = "E,o(b)";
bdaaa2e1 12092 /* Itbl support may require additional care here. */
252b5132 12093 coproc = 1;
df58fc94 12094 goto ld_st;
252b5132
RH
12095 case M_SWC1_AB:
12096 s = "swc1";
df58fc94 12097 fmt = "T,o(b)";
bdaaa2e1 12098 /* Itbl support may require additional care here. */
252b5132 12099 coproc = 1;
df58fc94 12100 goto ld_st;
252b5132
RH
12101 case M_SWC2_AB:
12102 s = "swc2";
df58fc94 12103 fmt = COP12_FMT;
7361da2c
AB
12104 offbits = (mips_opts.micromips ? 12
12105 : ISA_IS_R6 (mips_opts.isa) ? 11
12106 : 16);
bdaaa2e1 12107 /* Itbl support may require additional care here. */
252b5132 12108 coproc = 1;
df58fc94 12109 goto ld_st;
252b5132 12110 case M_SWC3_AB:
df58fc94 12111 gas_assert (!mips_opts.micromips);
252b5132 12112 s = "swc3";
df58fc94 12113 fmt = "E,o(b)";
bdaaa2e1 12114 /* Itbl support may require additional care here. */
252b5132 12115 coproc = 1;
df58fc94 12116 goto ld_st;
252b5132
RH
12117 case M_SWL_AB:
12118 s = "swl";
df58fc94 12119 fmt = MEM12_FMT;
7f3c4072 12120 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 12121 goto ld_st;
252b5132
RH
12122 case M_SWR_AB:
12123 s = "swr";
df58fc94 12124 fmt = MEM12_FMT;
7f3c4072 12125 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 12126 goto ld_st;
252b5132
RH
12127 case M_SC_AB:
12128 s = "sc";
7361da2c
AB
12129 fmt = LL_SC_FMT;
12130 offbits = (mips_opts.micromips ? 12
12131 : ISA_IS_R6 (mips_opts.isa) ? 9
12132 : 16);
df58fc94 12133 goto ld_st;
252b5132
RH
12134 case M_SCD_AB:
12135 s = "scd";
7361da2c
AB
12136 fmt = LL_SC_FMT;
12137 offbits = (mips_opts.micromips ? 12
12138 : ISA_IS_R6 (mips_opts.isa) ? 9
12139 : 16);
df58fc94 12140 goto ld_st;
a45328b9
AB
12141 case M_SCDP_AB:
12142 case M_SCWP_AB:
41cee089 12143 case M_SCWPE_AB:
a45328b9
AB
12144 s = ip->insn_mo->name;
12145 fmt = "t,d,s";
12146 ll_sc_paired = 1;
12147 offbits = 0;
12148 goto ld_st;
d43b4baf
TS
12149 case M_CACHE_AB:
12150 s = "cache";
7361da2c
AB
12151 fmt = (mips_opts.micromips ? "k,~(b)"
12152 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
12153 : "k,o(b)");
12154 offbits = (mips_opts.micromips ? 12
12155 : ISA_IS_R6 (mips_opts.isa) ? 9
12156 : 16);
7f3c4072
CM
12157 goto ld_st;
12158 case M_CACHEE_AB:
7f3c4072
CM
12159 s = "cachee";
12160 fmt = "k,+j(b)";
12161 offbits = 9;
df58fc94 12162 goto ld_st;
3eebd5eb
MR
12163 case M_PREF_AB:
12164 s = "pref";
7361da2c
AB
12165 fmt = (mips_opts.micromips ? "k,~(b)"
12166 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
12167 : "k,o(b)");
12168 offbits = (mips_opts.micromips ? 12
12169 : ISA_IS_R6 (mips_opts.isa) ? 9
12170 : 16);
7f3c4072
CM
12171 goto ld_st;
12172 case M_PREFE_AB:
7f3c4072
CM
12173 s = "prefe";
12174 fmt = "k,+j(b)";
12175 offbits = 9;
df58fc94 12176 goto ld_st;
252b5132 12177 case M_SDC1_AB:
252b5132 12178 s = "sdc1";
df58fc94 12179 fmt = "T,o(b)";
252b5132 12180 coproc = 1;
bdaaa2e1 12181 /* Itbl support may require additional care here. */
df58fc94 12182 goto ld_st;
252b5132
RH
12183 case M_SDC2_AB:
12184 s = "sdc2";
df58fc94 12185 fmt = COP12_FMT;
7361da2c
AB
12186 offbits = (mips_opts.micromips ? 12
12187 : ISA_IS_R6 (mips_opts.isa) ? 11
12188 : 16);
c77c0862
RS
12189 /* Itbl support may require additional care here. */
12190 coproc = 1;
12191 goto ld_st;
12192 case M_SQC2_AB:
c77c0862 12193 s = "sqc2";
14daeee3 12194 fmt = "+7,o(b)";
bdaaa2e1 12195 /* Itbl support may require additional care here. */
252b5132 12196 coproc = 1;
df58fc94 12197 goto ld_st;
252b5132 12198 case M_SDC3_AB:
df58fc94 12199 gas_assert (!mips_opts.micromips);
252b5132 12200 s = "sdc3";
df58fc94 12201 fmt = "E,o(b)";
bdaaa2e1 12202 /* Itbl support may require additional care here. */
252b5132 12203 coproc = 1;
df58fc94 12204 goto ld_st;
252b5132
RH
12205 case M_SDL_AB:
12206 s = "sdl";
df58fc94 12207 fmt = MEM12_FMT;
7f3c4072 12208 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 12209 goto ld_st;
252b5132
RH
12210 case M_SDR_AB:
12211 s = "sdr";
df58fc94 12212 fmt = MEM12_FMT;
7f3c4072 12213 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
12214 goto ld_st;
12215 case M_SWP_AB:
df58fc94
RS
12216 gas_assert (mips_opts.micromips);
12217 s = "swp";
12218 fmt = "t,~(b)";
7f3c4072 12219 offbits = 12;
df58fc94
RS
12220 goto ld_st;
12221 case M_SDP_AB:
df58fc94
RS
12222 gas_assert (mips_opts.micromips);
12223 s = "sdp";
12224 fmt = "t,~(b)";
7f3c4072 12225 offbits = 12;
df58fc94
RS
12226 goto ld_st;
12227 case M_SWM_AB:
df58fc94
RS
12228 gas_assert (mips_opts.micromips);
12229 s = "swm";
12230 fmt = "n,~(b)";
7f3c4072 12231 offbits = 12;
df58fc94
RS
12232 goto ld_st;
12233 case M_SDM_AB:
df58fc94
RS
12234 gas_assert (mips_opts.micromips);
12235 s = "sdm";
12236 fmt = "n,~(b)";
7f3c4072 12237 offbits = 12;
df58fc94
RS
12238
12239 ld_st:
8fc2e39e 12240 tempreg = AT;
df58fc94 12241 ld_noat:
a45328b9 12242 breg = ll_sc_paired ? op[3] : op[2];
f2ae14a1
RS
12243 if (small_offset_p (0, align, 16))
12244 {
12245 /* The first case exists for M_LD_AB and M_SD_AB, which are
12246 macros for o32 but which should act like normal instructions
12247 otherwise. */
12248 if (offbits == 16)
c0ebe874 12249 macro_build (&offset_expr, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
12250 offset_reloc[1], offset_reloc[2], breg);
12251 else if (small_offset_p (0, align, offbits))
12252 {
12253 if (offbits == 0)
a45328b9
AB
12254 {
12255 if (ll_sc_paired)
12256 macro_build (NULL, s, fmt, op[0], op[1], breg);
12257 else
12258 macro_build (NULL, s, fmt, op[0], breg);
12259 }
f2ae14a1 12260 else
c0ebe874 12261 macro_build (NULL, s, fmt, op[0],
c8276761 12262 (int) offset_expr.X_add_number, breg);
f2ae14a1
RS
12263 }
12264 else
12265 {
12266 if (tempreg == AT)
12267 used_at = 1;
12268 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
12269 tempreg, breg, -1, offset_reloc[0],
12270 offset_reloc[1], offset_reloc[2]);
12271 if (offbits == 0)
a45328b9
AB
12272 {
12273 if (ll_sc_paired)
12274 macro_build (NULL, s, fmt, op[0], op[1], tempreg);
12275 else
12276 macro_build (NULL, s, fmt, op[0], tempreg);
12277 }
f2ae14a1 12278 else
c0ebe874 12279 macro_build (NULL, s, fmt, op[0], 0, tempreg);
f2ae14a1
RS
12280 }
12281 break;
12282 }
12283
12284 if (tempreg == AT)
12285 used_at = 1;
12286
252b5132
RH
12287 if (offset_expr.X_op != O_constant
12288 && offset_expr.X_op != O_symbol)
12289 {
1661c76c 12290 as_bad (_("expression too complex"));
252b5132
RH
12291 offset_expr.X_op = O_constant;
12292 }
12293
2051e8c4
MR
12294 if (HAVE_32BIT_ADDRESSES
12295 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
12296 {
12297 char value [32];
12298
12299 sprintf_vma (value, offset_expr.X_add_number);
1661c76c 12300 as_bad (_("number (0x%s) larger than 32 bits"), value);
55e08f71 12301 }
2051e8c4 12302
252b5132
RH
12303 /* A constant expression in PIC code can be handled just as it
12304 is in non PIC code. */
aed1a261
RS
12305 if (offset_expr.X_op == O_constant)
12306 {
f2ae14a1
RS
12307 expr1.X_add_number = offset_high_part (offset_expr.X_add_number,
12308 offbits == 0 ? 16 : offbits);
12309 offset_expr.X_add_number -= expr1.X_add_number;
df58fc94 12310
f2ae14a1
RS
12311 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
12312 if (breg != 0)
12313 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12314 tempreg, tempreg, breg);
7f3c4072 12315 if (offbits == 0)
dd6a37e7 12316 {
f2ae14a1 12317 if (offset_expr.X_add_number != 0)
dd6a37e7 12318 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
f2ae14a1 12319 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
a45328b9
AB
12320 if (ll_sc_paired)
12321 macro_build (NULL, s, fmt, op[0], op[1], tempreg);
12322 else
12323 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 12324 }
7f3c4072 12325 else if (offbits == 16)
c0ebe874 12326 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
df58fc94 12327 else
c0ebe874 12328 macro_build (NULL, s, fmt, op[0],
c8276761 12329 (int) offset_expr.X_add_number, tempreg);
df58fc94 12330 }
7f3c4072 12331 else if (offbits != 16)
df58fc94 12332 {
7f3c4072 12333 /* The offset field is too narrow to be used for a low-part
2b0f3761 12334 relocation, so load the whole address into the auxiliary
f2ae14a1
RS
12335 register. */
12336 load_address (tempreg, &offset_expr, &used_at);
12337 if (breg != 0)
12338 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12339 tempreg, tempreg, breg);
7f3c4072 12340 if (offbits == 0)
a45328b9
AB
12341 {
12342 if (ll_sc_paired)
12343 macro_build (NULL, s, fmt, op[0], op[1], tempreg);
12344 else
12345 macro_build (NULL, s, fmt, op[0], tempreg);
12346 }
dd6a37e7 12347 else
c0ebe874 12348 macro_build (NULL, s, fmt, op[0], 0, tempreg);
aed1a261
RS
12349 }
12350 else if (mips_pic == NO_PIC)
252b5132
RH
12351 {
12352 /* If this is a reference to a GP relative symbol, and there
12353 is no base register, we want
c0ebe874 12354 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
12355 Otherwise, if there is no base register, we want
12356 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
c0ebe874 12357 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
252b5132
RH
12358 If we have a constant, we need two instructions anyhow,
12359 so we always use the latter form.
12360
12361 If we have a base register, and this is a reference to a
12362 GP relative symbol, we want
12363 addu $tempreg,$breg,$gp
c0ebe874 12364 <op> op[0],<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
12365 Otherwise we want
12366 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
12367 addu $tempreg,$tempreg,$breg
c0ebe874 12368 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 12369 With a constant we always use the latter case.
76b3015f 12370
d6bc6245
TS
12371 With 64bit address space and no base register and $at usable,
12372 we want
12373 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12374 lui $at,<sym> (BFD_RELOC_HI16_S)
12375 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12376 dsll32 $tempreg,0
12377 daddu $tempreg,$at
c0ebe874 12378 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
12379 If we have a base register, we want
12380 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12381 lui $at,<sym> (BFD_RELOC_HI16_S)
12382 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12383 daddu $at,$breg
12384 dsll32 $tempreg,0
12385 daddu $tempreg,$at
c0ebe874 12386 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
12387
12388 Without $at we can't generate the optimal path for superscalar
12389 processors here since this would require two temporary registers.
12390 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12391 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12392 dsll $tempreg,16
12393 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
12394 dsll $tempreg,16
c0ebe874 12395 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
12396 If we have a base register, we want
12397 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12398 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12399 dsll $tempreg,16
12400 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
12401 dsll $tempreg,16
12402 daddu $tempreg,$tempreg,$breg
c0ebe874 12403 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 12404
6caf9ef4 12405 For GP relative symbols in 64bit address space we can use
aed1a261
RS
12406 the same sequence as in 32bit address space. */
12407 if (HAVE_64BIT_SYMBOLS)
d6bc6245 12408 {
aed1a261 12409 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
12410 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12411 {
12412 relax_start (offset_expr.X_add_symbol);
12413 if (breg == 0)
12414 {
c0ebe874 12415 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
12416 BFD_RELOC_GPREL16, mips_gp_register);
12417 }
12418 else
12419 {
12420 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12421 tempreg, breg, mips_gp_register);
c0ebe874 12422 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
12423 BFD_RELOC_GPREL16, tempreg);
12424 }
12425 relax_switch ();
12426 }
d6bc6245 12427
741fe287 12428 if (used_at == 0 && mips_opts.at)
d6bc6245 12429 {
df58fc94 12430 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb 12431 BFD_RELOC_MIPS_HIGHEST);
df58fc94 12432 macro_build (&offset_expr, "lui", LUI_FMT, AT,
67c0d1eb
RS
12433 BFD_RELOC_HI16_S);
12434 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12435 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 12436 if (breg != 0)
67c0d1eb 12437 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
df58fc94 12438 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 12439 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
c0ebe874 12440 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16,
67c0d1eb 12441 tempreg);
d6bc6245
TS
12442 used_at = 1;
12443 }
12444 else
12445 {
df58fc94 12446 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb
RS
12447 BFD_RELOC_MIPS_HIGHEST);
12448 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12449 tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 12450 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb
RS
12451 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12452 tempreg, BFD_RELOC_HI16_S);
df58fc94 12453 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
d6bc6245 12454 if (breg != 0)
67c0d1eb 12455 macro_build (NULL, "daddu", "d,v,t",
17a2f251 12456 tempreg, tempreg, breg);
c0ebe874 12457 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12458 BFD_RELOC_LO16, tempreg);
d6bc6245 12459 }
6caf9ef4
TS
12460
12461 if (mips_relax.sequence)
12462 relax_end ();
8fc2e39e 12463 break;
d6bc6245 12464 }
256ab948 12465
252b5132
RH
12466 if (breg == 0)
12467 {
67c0d1eb 12468 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12469 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12470 {
4d7206a2 12471 relax_start (offset_expr.X_add_symbol);
c0ebe874 12472 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_GPREL16,
67c0d1eb 12473 mips_gp_register);
4d7206a2 12474 relax_switch ();
252b5132 12475 }
67c0d1eb 12476 macro_build_lui (&offset_expr, tempreg);
c0ebe874 12477 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12478 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
12479 if (mips_relax.sequence)
12480 relax_end ();
252b5132
RH
12481 }
12482 else
12483 {
67c0d1eb 12484 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12485 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12486 {
4d7206a2 12487 relax_start (offset_expr.X_add_symbol);
67c0d1eb 12488 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12489 tempreg, breg, mips_gp_register);
c0ebe874 12490 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12491 BFD_RELOC_GPREL16, tempreg);
4d7206a2 12492 relax_switch ();
252b5132 12493 }
67c0d1eb
RS
12494 macro_build_lui (&offset_expr, tempreg);
12495 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12496 tempreg, tempreg, breg);
c0ebe874 12497 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12498 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
12499 if (mips_relax.sequence)
12500 relax_end ();
252b5132
RH
12501 }
12502 }
0a44bf69 12503 else if (!mips_big_got)
252b5132 12504 {
ed6fb7bd 12505 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 12506
252b5132
RH
12507 /* If this is a reference to an external symbol, we want
12508 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12509 nop
c0ebe874 12510 <op> op[0],0($tempreg)
252b5132
RH
12511 Otherwise we want
12512 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12513 nop
12514 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 12515 <op> op[0],0($tempreg)
f5040a92
AO
12516
12517 For NewABI, we want
12518 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 12519 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 12520
252b5132
RH
12521 If there is a base register, we add it to $tempreg before
12522 the <op>. If there is a constant, we stick it in the
12523 <op> instruction. We don't handle constants larger than
12524 16 bits, because we have no way to load the upper 16 bits
12525 (actually, we could handle them for the subset of cases
12526 in which we are not using $at). */
9c2799c2 12527 gas_assert (offset_expr.X_op == O_symbol);
f5040a92
AO
12528 if (HAVE_NEWABI)
12529 {
67c0d1eb
RS
12530 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12531 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 12532 if (breg != 0)
67c0d1eb 12533 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12534 tempreg, tempreg, breg);
c0ebe874 12535 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12536 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
12537 break;
12538 }
252b5132
RH
12539 expr1.X_add_number = offset_expr.X_add_number;
12540 offset_expr.X_add_number = 0;
12541 if (expr1.X_add_number < -0x8000
12542 || expr1.X_add_number >= 0x8000)
12543 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
12544 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12545 lw_reloc_type, mips_gp_register);
269137b2 12546 load_delay_nop ();
4d7206a2
RS
12547 relax_start (offset_expr.X_add_symbol);
12548 relax_switch ();
67c0d1eb
RS
12549 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12550 tempreg, BFD_RELOC_LO16);
4d7206a2 12551 relax_end ();
252b5132 12552 if (breg != 0)
67c0d1eb 12553 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12554 tempreg, tempreg, breg);
c0ebe874 12555 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 12556 }
0a44bf69 12557 else if (mips_big_got && !HAVE_NEWABI)
252b5132 12558 {
67c0d1eb 12559 int gpdelay;
252b5132
RH
12560
12561 /* If this is a reference to an external symbol, we want
12562 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12563 addu $tempreg,$tempreg,$gp
12564 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 12565 <op> op[0],0($tempreg)
252b5132
RH
12566 Otherwise we want
12567 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12568 nop
12569 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 12570 <op> op[0],0($tempreg)
252b5132
RH
12571 If there is a base register, we add it to $tempreg before
12572 the <op>. If there is a constant, we stick it in the
12573 <op> instruction. We don't handle constants larger than
12574 16 bits, because we have no way to load the upper 16 bits
12575 (actually, we could handle them for the subset of cases
f5040a92 12576 in which we are not using $at). */
9c2799c2 12577 gas_assert (offset_expr.X_op == O_symbol);
252b5132
RH
12578 expr1.X_add_number = offset_expr.X_add_number;
12579 offset_expr.X_add_number = 0;
12580 if (expr1.X_add_number < -0x8000
12581 || expr1.X_add_number >= 0x8000)
12582 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 12583 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 12584 relax_start (offset_expr.X_add_symbol);
df58fc94 12585 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 12586 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
12587 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12588 mips_gp_register);
12589 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12590 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 12591 relax_switch ();
67c0d1eb
RS
12592 if (gpdelay)
12593 macro_build (NULL, "nop", "");
12594 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12595 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 12596 load_delay_nop ();
67c0d1eb
RS
12597 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12598 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
12599 relax_end ();
12600
252b5132 12601 if (breg != 0)
67c0d1eb 12602 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12603 tempreg, tempreg, breg);
c0ebe874 12604 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 12605 }
0a44bf69 12606 else if (mips_big_got && HAVE_NEWABI)
f5040a92 12607 {
f5040a92
AO
12608 /* If this is a reference to an external symbol, we want
12609 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12610 add $tempreg,$tempreg,$gp
12611 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 12612 <op> op[0],<ofst>($tempreg)
f5040a92
AO
12613 Otherwise, for local symbols, we want:
12614 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 12615 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
9c2799c2 12616 gas_assert (offset_expr.X_op == O_symbol);
4d7206a2 12617 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
12618 offset_expr.X_add_number = 0;
12619 if (expr1.X_add_number < -0x8000
12620 || expr1.X_add_number >= 0x8000)
12621 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 12622 relax_start (offset_expr.X_add_symbol);
df58fc94 12623 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 12624 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
12625 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12626 mips_gp_register);
12627 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12628 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 12629 if (breg != 0)
67c0d1eb 12630 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12631 tempreg, tempreg, breg);
c0ebe874 12632 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
684022ea 12633
4d7206a2 12634 relax_switch ();
f5040a92 12635 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
12636 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12637 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 12638 if (breg != 0)
67c0d1eb 12639 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12640 tempreg, tempreg, breg);
c0ebe874 12641 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12642 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 12643 relax_end ();
f5040a92 12644 }
252b5132
RH
12645 else
12646 abort ();
12647
252b5132
RH
12648 break;
12649
833794fc
MR
12650 case M_JRADDIUSP:
12651 gas_assert (mips_opts.micromips);
12652 gas_assert (mips_opts.insn32);
12653 start_noreorder ();
12654 macro_build (NULL, "jr", "s", RA);
c0ebe874 12655 expr1.X_add_number = op[0] << 2;
833794fc
MR
12656 macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
12657 end_noreorder ();
12658 break;
12659
12660 case M_JRC:
12661 gas_assert (mips_opts.micromips);
12662 gas_assert (mips_opts.insn32);
c0ebe874 12663 macro_build (NULL, "jr", "s", op[0]);
833794fc
MR
12664 if (mips_opts.noreorder)
12665 macro_build (NULL, "nop", "");
12666 break;
12667
252b5132
RH
12668 case M_LI:
12669 case M_LI_S:
c0ebe874 12670 load_register (op[0], &imm_expr, 0);
8fc2e39e 12671 break;
252b5132
RH
12672
12673 case M_DLI:
c0ebe874 12674 load_register (op[0], &imm_expr, 1);
8fc2e39e 12675 break;
252b5132
RH
12676
12677 case M_LI_SS:
12678 if (imm_expr.X_op == O_constant)
12679 {
8fc2e39e 12680 used_at = 1;
67c0d1eb 12681 load_register (AT, &imm_expr, 0);
c0ebe874 12682 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
12683 break;
12684 }
12685 else
12686 {
b0e6f033
RS
12687 gas_assert (imm_expr.X_op == O_absent
12688 && offset_expr.X_op == O_symbol
90ecf173
MR
12689 && strcmp (segment_name (S_GET_SEGMENT
12690 (offset_expr.X_add_symbol)),
12691 ".lit4") == 0
12692 && offset_expr.X_add_number == 0);
c0ebe874 12693 macro_build (&offset_expr, "lwc1", "T,o(b)", op[0],
17a2f251 12694 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 12695 break;
252b5132
RH
12696 }
12697
12698 case M_LI_D:
ca4e0257
RS
12699 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
12700 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
12701 order 32 bits of the value and the low order 32 bits are either
12702 zero or in OFFSET_EXPR. */
b0e6f033 12703 if (imm_expr.X_op == O_constant)
252b5132 12704 {
bad1aba3 12705 if (GPR_SIZE == 64)
c0ebe874 12706 load_register (op[0], &imm_expr, 1);
252b5132
RH
12707 else
12708 {
12709 int hreg, lreg;
12710
12711 if (target_big_endian)
12712 {
c0ebe874
RS
12713 hreg = op[0];
12714 lreg = op[0] + 1;
252b5132
RH
12715 }
12716 else
12717 {
c0ebe874
RS
12718 hreg = op[0] + 1;
12719 lreg = op[0];
252b5132
RH
12720 }
12721
12722 if (hreg <= 31)
67c0d1eb 12723 load_register (hreg, &imm_expr, 0);
252b5132
RH
12724 if (lreg <= 31)
12725 {
12726 if (offset_expr.X_op == O_absent)
67c0d1eb 12727 move_register (lreg, 0);
252b5132
RH
12728 else
12729 {
9c2799c2 12730 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 12731 load_register (lreg, &offset_expr, 0);
252b5132
RH
12732 }
12733 }
12734 }
8fc2e39e 12735 break;
252b5132 12736 }
b0e6f033 12737 gas_assert (imm_expr.X_op == O_absent);
252b5132
RH
12738
12739 /* We know that sym is in the .rdata section. First we get the
12740 upper 16 bits of the address. */
12741 if (mips_pic == NO_PIC)
12742 {
67c0d1eb 12743 macro_build_lui (&offset_expr, AT);
8fc2e39e 12744 used_at = 1;
252b5132 12745 }
0a44bf69 12746 else
252b5132 12747 {
67c0d1eb
RS
12748 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12749 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 12750 used_at = 1;
252b5132 12751 }
bdaaa2e1 12752
252b5132 12753 /* Now we load the register(s). */
bad1aba3 12754 if (GPR_SIZE == 64)
8fc2e39e
TS
12755 {
12756 used_at = 1;
c0ebe874
RS
12757 macro_build (&offset_expr, "ld", "t,o(b)", op[0],
12758 BFD_RELOC_LO16, AT);
8fc2e39e 12759 }
252b5132
RH
12760 else
12761 {
8fc2e39e 12762 used_at = 1;
c0ebe874
RS
12763 macro_build (&offset_expr, "lw", "t,o(b)", op[0],
12764 BFD_RELOC_LO16, AT);
12765 if (op[0] != RA)
252b5132
RH
12766 {
12767 /* FIXME: How in the world do we deal with the possible
12768 overflow here? */
12769 offset_expr.X_add_number += 4;
67c0d1eb 12770 macro_build (&offset_expr, "lw", "t,o(b)",
c0ebe874 12771 op[0] + 1, BFD_RELOC_LO16, AT);
252b5132
RH
12772 }
12773 }
252b5132
RH
12774 break;
12775
12776 case M_LI_DD:
ca4e0257
RS
12777 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
12778 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
12779 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
12780 the value and the low order 32 bits are either zero or in
12781 OFFSET_EXPR. */
b0e6f033 12782 if (imm_expr.X_op == O_constant)
252b5132 12783 {
9b444f95
FS
12784 tempreg = ZERO;
12785 if (((FPR_SIZE == 64 && GPR_SIZE == 64)
12786 || !ISA_HAS_MXHC1 (mips_opts.isa))
12787 && imm_expr.X_add_number != 0)
12788 {
12789 used_at = 1;
12790 tempreg = AT;
12791 load_register (AT, &imm_expr, FPR_SIZE == 64);
12792 }
351cdf24 12793 if (FPR_SIZE == 64 && GPR_SIZE == 64)
9b444f95 12794 macro_build (NULL, "dmtc1", "t,S", tempreg, op[0]);
252b5132
RH
12795 else
12796 {
9b444f95
FS
12797 if (!ISA_HAS_MXHC1 (mips_opts.isa))
12798 {
12799 if (FPR_SIZE != 32)
12800 as_bad (_("Unable to generate `%s' compliant code "
12801 "without mthc1"),
12802 (FPR_SIZE == 64) ? "fp64" : "fpxx");
12803 else
12804 macro_build (NULL, "mtc1", "t,G", tempreg, op[0] + 1);
12805 }
252b5132 12806 if (offset_expr.X_op == O_absent)
c0ebe874 12807 macro_build (NULL, "mtc1", "t,G", 0, op[0]);
252b5132
RH
12808 else
12809 {
9c2799c2 12810 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 12811 load_register (AT, &offset_expr, 0);
c0ebe874 12812 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132 12813 }
9b444f95
FS
12814 if (ISA_HAS_MXHC1 (mips_opts.isa))
12815 {
12816 if (imm_expr.X_add_number != 0)
12817 {
12818 used_at = 1;
12819 tempreg = AT;
12820 load_register (AT, &imm_expr, 0);
12821 }
12822 macro_build (NULL, "mthc1", "t,G", tempreg, op[0]);
12823 }
252b5132
RH
12824 }
12825 break;
12826 }
12827
b0e6f033
RS
12828 gas_assert (imm_expr.X_op == O_absent
12829 && offset_expr.X_op == O_symbol
90ecf173 12830 && offset_expr.X_add_number == 0);
252b5132
RH
12831 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
12832 if (strcmp (s, ".lit8") == 0)
134c0c8b
MR
12833 {
12834 op[2] = mips_gp_register;
f2ae14a1
RS
12835 offset_reloc[0] = BFD_RELOC_MIPS_LITERAL;
12836 offset_reloc[1] = BFD_RELOC_UNUSED;
12837 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
12838 }
12839 else
12840 {
9c2799c2 12841 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 12842 used_at = 1;
0a44bf69 12843 if (mips_pic != NO_PIC)
67c0d1eb
RS
12844 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12845 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
12846 else
12847 {
12848 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 12849 macro_build_lui (&offset_expr, AT);
252b5132 12850 }
bdaaa2e1 12851
c0ebe874 12852 op[2] = AT;
f2ae14a1
RS
12853 offset_reloc[0] = BFD_RELOC_LO16;
12854 offset_reloc[1] = BFD_RELOC_UNUSED;
12855 offset_reloc[2] = BFD_RELOC_UNUSED;
134c0c8b 12856 }
f2ae14a1 12857 align = 8;
6f2117ba 12858 /* Fall through. */
c4a68bea 12859
252b5132 12860 case M_L_DAB:
6f2117ba
PH
12861 /* The MIPS assembler seems to check for X_add_number not
12862 being double aligned and generating:
12863 lui at,%hi(foo+1)
12864 addu at,at,v1
12865 addiu at,at,%lo(foo+1)
12866 lwc1 f2,0(at)
12867 lwc1 f3,4(at)
12868 But, the resulting address is the same after relocation so why
12869 generate the extra instruction? */
bdaaa2e1 12870 /* Itbl support may require additional care here. */
252b5132 12871 coproc = 1;
df58fc94 12872 fmt = "T,o(b)";
0aa27725 12873 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
12874 {
12875 s = "ldc1";
df58fc94 12876 goto ld_st;
252b5132 12877 }
252b5132 12878 s = "lwc1";
252b5132
RH
12879 goto ldd_std;
12880
12881 case M_S_DAB:
df58fc94
RS
12882 gas_assert (!mips_opts.micromips);
12883 /* Itbl support may require additional care here. */
12884 coproc = 1;
12885 fmt = "T,o(b)";
0aa27725 12886 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
12887 {
12888 s = "sdc1";
df58fc94 12889 goto ld_st;
252b5132 12890 }
252b5132 12891 s = "swc1";
252b5132
RH
12892 goto ldd_std;
12893
e407c74b
NC
12894 case M_LQ_AB:
12895 fmt = "t,o(b)";
12896 s = "lq";
12897 goto ld;
12898
12899 case M_SQ_AB:
12900 fmt = "t,o(b)";
12901 s = "sq";
12902 goto ld_st;
12903
252b5132 12904 case M_LD_AB:
df58fc94 12905 fmt = "t,o(b)";
bad1aba3 12906 if (GPR_SIZE == 64)
252b5132
RH
12907 {
12908 s = "ld";
12909 goto ld;
12910 }
252b5132 12911 s = "lw";
252b5132
RH
12912 goto ldd_std;
12913
12914 case M_SD_AB:
df58fc94 12915 fmt = "t,o(b)";
bad1aba3 12916 if (GPR_SIZE == 64)
252b5132
RH
12917 {
12918 s = "sd";
df58fc94 12919 goto ld_st;
252b5132 12920 }
252b5132 12921 s = "sw";
252b5132
RH
12922
12923 ldd_std:
f2ae14a1
RS
12924 /* Even on a big endian machine $fn comes before $fn+1. We have
12925 to adjust when loading from memory. We set coproc if we must
12926 load $fn+1 first. */
12927 /* Itbl support may require additional care here. */
12928 if (!target_big_endian)
12929 coproc = 0;
12930
c0ebe874 12931 breg = op[2];
f2ae14a1
RS
12932 if (small_offset_p (0, align, 16))
12933 {
12934 ep = &offset_expr;
12935 if (!small_offset_p (4, align, 16))
12936 {
12937 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT, breg,
12938 -1, offset_reloc[0], offset_reloc[1],
12939 offset_reloc[2]);
12940 expr1.X_add_number = 0;
12941 ep = &expr1;
12942 breg = AT;
12943 used_at = 1;
12944 offset_reloc[0] = BFD_RELOC_LO16;
12945 offset_reloc[1] = BFD_RELOC_UNUSED;
12946 offset_reloc[2] = BFD_RELOC_UNUSED;
12947 }
c0ebe874 12948 if (strcmp (s, "lw") == 0 && op[0] == breg)
f2ae14a1
RS
12949 {
12950 ep->X_add_number += 4;
c0ebe874 12951 macro_build (ep, s, fmt, op[0] + 1, -1, offset_reloc[0],
f2ae14a1
RS
12952 offset_reloc[1], offset_reloc[2], breg);
12953 ep->X_add_number -= 4;
c0ebe874 12954 macro_build (ep, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
12955 offset_reloc[1], offset_reloc[2], breg);
12956 }
12957 else
12958 {
c0ebe874 12959 macro_build (ep, s, fmt, coproc ? op[0] + 1 : op[0], -1,
f2ae14a1
RS
12960 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12961 breg);
12962 ep->X_add_number += 4;
c0ebe874 12963 macro_build (ep, s, fmt, coproc ? op[0] : op[0] + 1, -1,
f2ae14a1
RS
12964 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12965 breg);
12966 }
12967 break;
12968 }
12969
252b5132
RH
12970 if (offset_expr.X_op != O_symbol
12971 && offset_expr.X_op != O_constant)
12972 {
1661c76c 12973 as_bad (_("expression too complex"));
252b5132
RH
12974 offset_expr.X_op = O_constant;
12975 }
12976
2051e8c4
MR
12977 if (HAVE_32BIT_ADDRESSES
12978 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
12979 {
12980 char value [32];
12981
12982 sprintf_vma (value, offset_expr.X_add_number);
1661c76c 12983 as_bad (_("number (0x%s) larger than 32 bits"), value);
55e08f71 12984 }
2051e8c4 12985
90ecf173 12986 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
252b5132
RH
12987 {
12988 /* If this is a reference to a GP relative symbol, we want
c0ebe874
RS
12989 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
12990 <op> op[0]+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
12991 If we have a base register, we use this
12992 addu $at,$breg,$gp
c0ebe874
RS
12993 <op> op[0],<sym>($at) (BFD_RELOC_GPREL16)
12994 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
12995 If this is not a GP relative symbol, we want
12996 lui $at,<sym> (BFD_RELOC_HI16_S)
c0ebe874
RS
12997 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12998 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
12999 If there is a base register, we add it to $at after the
13000 lui instruction. If there is a constant, we always use
13001 the last case. */
39a59cf8
MR
13002 if (offset_expr.X_op == O_symbol
13003 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 13004 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 13005 {
4d7206a2 13006 relax_start (offset_expr.X_add_symbol);
252b5132
RH
13007 if (breg == 0)
13008 {
c9914766 13009 tempreg = mips_gp_register;
252b5132
RH
13010 }
13011 else
13012 {
67c0d1eb 13013 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 13014 AT, breg, mips_gp_register);
252b5132 13015 tempreg = AT;
252b5132
RH
13016 used_at = 1;
13017 }
13018
beae10d5 13019 /* Itbl support may require additional care here. */
c0ebe874 13020 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 13021 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
13022 offset_expr.X_add_number += 4;
13023
13024 /* Set mips_optimize to 2 to avoid inserting an
13025 undesired nop. */
13026 hold_mips_optimize = mips_optimize;
13027 mips_optimize = 2;
beae10d5 13028 /* Itbl support may require additional care here. */
c0ebe874 13029 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 13030 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
13031 mips_optimize = hold_mips_optimize;
13032
4d7206a2 13033 relax_switch ();
252b5132 13034
0970e49e 13035 offset_expr.X_add_number -= 4;
252b5132 13036 }
8fc2e39e 13037 used_at = 1;
f2ae14a1
RS
13038 if (offset_high_part (offset_expr.X_add_number, 16)
13039 != offset_high_part (offset_expr.X_add_number + 4, 16))
13040 {
13041 load_address (AT, &offset_expr, &used_at);
13042 offset_expr.X_op = O_constant;
13043 offset_expr.X_add_number = 0;
13044 }
13045 else
13046 macro_build_lui (&offset_expr, AT);
252b5132 13047 if (breg != 0)
67c0d1eb 13048 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 13049 /* Itbl support may require additional care here. */
c0ebe874 13050 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 13051 BFD_RELOC_LO16, AT);
252b5132
RH
13052 /* FIXME: How do we handle overflow here? */
13053 offset_expr.X_add_number += 4;
beae10d5 13054 /* Itbl support may require additional care here. */
c0ebe874 13055 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 13056 BFD_RELOC_LO16, AT);
4d7206a2
RS
13057 if (mips_relax.sequence)
13058 relax_end ();
bdaaa2e1 13059 }
0a44bf69 13060 else if (!mips_big_got)
252b5132 13061 {
252b5132
RH
13062 /* If this is a reference to an external symbol, we want
13063 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
13064 nop
c0ebe874
RS
13065 <op> op[0],0($at)
13066 <op> op[0]+1,4($at)
252b5132
RH
13067 Otherwise we want
13068 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
13069 nop
c0ebe874
RS
13070 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
13071 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
13072 If there is a base register we add it to $at before the
13073 lwc1 instructions. If there is a constant we include it
13074 in the lwc1 instructions. */
13075 used_at = 1;
13076 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
13077 if (expr1.X_add_number < -0x8000
13078 || expr1.X_add_number >= 0x8000 - 4)
13079 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 13080 load_got_offset (AT, &offset_expr);
269137b2 13081 load_delay_nop ();
252b5132 13082 if (breg != 0)
67c0d1eb 13083 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
13084
13085 /* Set mips_optimize to 2 to avoid inserting an undesired
13086 nop. */
13087 hold_mips_optimize = mips_optimize;
13088 mips_optimize = 2;
4d7206a2 13089
beae10d5 13090 /* Itbl support may require additional care here. */
4d7206a2 13091 relax_start (offset_expr.X_add_symbol);
c0ebe874 13092 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 13093 BFD_RELOC_LO16, AT);
4d7206a2 13094 expr1.X_add_number += 4;
c0ebe874 13095 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 13096 BFD_RELOC_LO16, AT);
4d7206a2 13097 relax_switch ();
c0ebe874 13098 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 13099 BFD_RELOC_LO16, AT);
4d7206a2 13100 offset_expr.X_add_number += 4;
c0ebe874 13101 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 13102 BFD_RELOC_LO16, AT);
4d7206a2 13103 relax_end ();
252b5132 13104
4d7206a2 13105 mips_optimize = hold_mips_optimize;
252b5132 13106 }
0a44bf69 13107 else if (mips_big_got)
252b5132 13108 {
67c0d1eb 13109 int gpdelay;
252b5132
RH
13110
13111 /* If this is a reference to an external symbol, we want
13112 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
13113 addu $at,$at,$gp
13114 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
13115 nop
c0ebe874
RS
13116 <op> op[0],0($at)
13117 <op> op[0]+1,4($at)
252b5132
RH
13118 Otherwise we want
13119 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
13120 nop
c0ebe874
RS
13121 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
13122 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
13123 If there is a base register we add it to $at before the
13124 lwc1 instructions. If there is a constant we include it
13125 in the lwc1 instructions. */
13126 used_at = 1;
13127 expr1.X_add_number = offset_expr.X_add_number;
13128 offset_expr.X_add_number = 0;
13129 if (expr1.X_add_number < -0x8000
13130 || expr1.X_add_number >= 0x8000 - 4)
13131 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 13132 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 13133 relax_start (offset_expr.X_add_symbol);
df58fc94 13134 macro_build (&offset_expr, "lui", LUI_FMT,
67c0d1eb
RS
13135 AT, BFD_RELOC_MIPS_GOT_HI16);
13136 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 13137 AT, AT, mips_gp_register);
67c0d1eb 13138 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 13139 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 13140 load_delay_nop ();
252b5132 13141 if (breg != 0)
67c0d1eb 13142 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 13143 /* Itbl support may require additional care here. */
c0ebe874 13144 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 13145 BFD_RELOC_LO16, AT);
252b5132
RH
13146 expr1.X_add_number += 4;
13147
13148 /* Set mips_optimize to 2 to avoid inserting an undesired
13149 nop. */
13150 hold_mips_optimize = mips_optimize;
13151 mips_optimize = 2;
beae10d5 13152 /* Itbl support may require additional care here. */
c0ebe874 13153 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 13154 BFD_RELOC_LO16, AT);
252b5132
RH
13155 mips_optimize = hold_mips_optimize;
13156 expr1.X_add_number -= 4;
13157
4d7206a2
RS
13158 relax_switch ();
13159 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
13160 if (gpdelay)
13161 macro_build (NULL, "nop", "");
13162 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
13163 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 13164 load_delay_nop ();
252b5132 13165 if (breg != 0)
67c0d1eb 13166 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 13167 /* Itbl support may require additional care here. */
c0ebe874 13168 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 13169 BFD_RELOC_LO16, AT);
4d7206a2 13170 offset_expr.X_add_number += 4;
252b5132
RH
13171
13172 /* Set mips_optimize to 2 to avoid inserting an undesired
13173 nop. */
13174 hold_mips_optimize = mips_optimize;
13175 mips_optimize = 2;
beae10d5 13176 /* Itbl support may require additional care here. */
c0ebe874 13177 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 13178 BFD_RELOC_LO16, AT);
252b5132 13179 mips_optimize = hold_mips_optimize;
4d7206a2 13180 relax_end ();
252b5132 13181 }
252b5132
RH
13182 else
13183 abort ();
13184
252b5132 13185 break;
3739860c 13186
dd6a37e7 13187 case M_SAA_AB:
dd6a37e7 13188 s = "saa";
0db377d0 13189 goto saa_saad;
dd6a37e7 13190 case M_SAAD_AB:
dd6a37e7 13191 s = "saad";
0db377d0
MR
13192 saa_saad:
13193 gas_assert (!mips_opts.micromips);
7f3c4072 13194 offbits = 0;
dd6a37e7
AP
13195 fmt = "t,(b)";
13196 goto ld_st;
13197
252b5132
RH
13198 /* New code added to support COPZ instructions.
13199 This code builds table entries out of the macros in mip_opcodes.
13200 R4000 uses interlocks to handle coproc delays.
13201 Other chips (like the R3000) require nops to be inserted for delays.
13202
f72c8c98 13203 FIXME: Currently, we require that the user handle delays.
252b5132
RH
13204 In order to fill delay slots for non-interlocked chips,
13205 we must have a way to specify delays based on the coprocessor.
13206 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
13207 What are the side-effects of the cop instruction?
13208 What cache support might we have and what are its effects?
13209 Both coprocessor & memory require delays. how long???
bdaaa2e1 13210 What registers are read/set/modified?
252b5132
RH
13211
13212 If an itbl is provided to interpret cop instructions,
bdaaa2e1 13213 this knowledge can be encoded in the itbl spec. */
252b5132
RH
13214
13215 case M_COP0:
13216 s = "c0";
13217 goto copz;
13218 case M_COP1:
13219 s = "c1";
13220 goto copz;
13221 case M_COP2:
13222 s = "c2";
13223 goto copz;
13224 case M_COP3:
13225 s = "c3";
13226 copz:
df58fc94 13227 gas_assert (!mips_opts.micromips);
252b5132
RH
13228 /* For now we just do C (same as Cz). The parameter will be
13229 stored in insn_opcode by mips_ip. */
c8276761 13230 macro_build (NULL, s, "C", (int) ip->insn_opcode);
8fc2e39e 13231 break;
252b5132 13232
ea1fb5dc 13233 case M_MOVE:
c0ebe874 13234 move_register (op[0], op[1]);
8fc2e39e 13235 break;
ea1fb5dc 13236
833794fc
MR
13237 case M_MOVEP:
13238 gas_assert (mips_opts.micromips);
13239 gas_assert (mips_opts.insn32);
c0ebe874
RS
13240 move_register (micromips_to_32_reg_h_map1[op[0]],
13241 micromips_to_32_reg_m_map[op[1]]);
13242 move_register (micromips_to_32_reg_h_map2[op[0]],
13243 micromips_to_32_reg_n_map[op[2]]);
833794fc
MR
13244 break;
13245
252b5132
RH
13246 case M_DMUL:
13247 dbl = 1;
1a0670f3 13248 /* Fall through. */
252b5132 13249 case M_MUL:
e407c74b 13250 if (mips_opts.arch == CPU_R5900)
c0ebe874
RS
13251 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", op[0], op[1],
13252 op[2]);
e407c74b
NC
13253 else
13254 {
c0ebe874
RS
13255 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", op[1], op[2]);
13256 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
e407c74b 13257 }
8fc2e39e 13258 break;
252b5132
RH
13259
13260 case M_DMUL_I:
13261 dbl = 1;
1a0670f3 13262 /* Fall through. */
252b5132
RH
13263 case M_MUL_I:
13264 /* The MIPS assembler some times generates shifts and adds. I'm
13265 not trying to be that fancy. GCC should do this for us
13266 anyway. */
8fc2e39e 13267 used_at = 1;
67c0d1eb 13268 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
13269 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", op[1], AT);
13270 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
13271 break;
13272
13273 case M_DMULO_I:
13274 dbl = 1;
1a0670f3 13275 /* Fall through. */
252b5132
RH
13276 case M_MULO_I:
13277 imm = 1;
13278 goto do_mulo;
13279
13280 case M_DMULO:
13281 dbl = 1;
1a0670f3 13282 /* Fall through. */
252b5132
RH
13283 case M_MULO:
13284 do_mulo:
7d10b47d 13285 start_noreorder ();
8fc2e39e 13286 used_at = 1;
252b5132 13287 if (imm)
67c0d1eb 13288 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
13289 macro_build (NULL, dbl ? "dmult" : "mult", "s,t",
13290 op[1], imm ? AT : op[2]);
13291 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13292 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, op[0], op[0], 31);
df58fc94 13293 macro_build (NULL, "mfhi", MFHL_FMT, AT);
252b5132 13294 if (mips_trap)
c0ebe874 13295 macro_build (NULL, "tne", TRAP_FMT, op[0], AT, 6);
252b5132
RH
13296 else
13297 {
df58fc94
RS
13298 if (mips_opts.micromips)
13299 micromips_label_expr (&label_expr);
13300 else
13301 label_expr.X_add_number = 8;
c0ebe874 13302 macro_build (&label_expr, "beq", "s,t,p", op[0], AT);
a605d2b3 13303 macro_build (NULL, "nop", "");
df58fc94
RS
13304 macro_build (NULL, "break", BRK_FMT, 6);
13305 if (mips_opts.micromips)
13306 micromips_add_label ();
252b5132 13307 }
7d10b47d 13308 end_noreorder ();
c0ebe874 13309 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
13310 break;
13311
13312 case M_DMULOU_I:
13313 dbl = 1;
1a0670f3 13314 /* Fall through. */
252b5132
RH
13315 case M_MULOU_I:
13316 imm = 1;
13317 goto do_mulou;
13318
13319 case M_DMULOU:
13320 dbl = 1;
1a0670f3 13321 /* Fall through. */
252b5132
RH
13322 case M_MULOU:
13323 do_mulou:
7d10b47d 13324 start_noreorder ();
8fc2e39e 13325 used_at = 1;
252b5132 13326 if (imm)
67c0d1eb
RS
13327 load_register (AT, &imm_expr, dbl);
13328 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
c0ebe874 13329 op[1], imm ? AT : op[2]);
df58fc94 13330 macro_build (NULL, "mfhi", MFHL_FMT, AT);
c0ebe874 13331 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132 13332 if (mips_trap)
df58fc94 13333 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
252b5132
RH
13334 else
13335 {
df58fc94
RS
13336 if (mips_opts.micromips)
13337 micromips_label_expr (&label_expr);
13338 else
13339 label_expr.X_add_number = 8;
13340 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
a605d2b3 13341 macro_build (NULL, "nop", "");
df58fc94
RS
13342 macro_build (NULL, "break", BRK_FMT, 6);
13343 if (mips_opts.micromips)
13344 micromips_add_label ();
252b5132 13345 }
7d10b47d 13346 end_noreorder ();
252b5132
RH
13347 break;
13348
771c7ce4 13349 case M_DROL:
fef14a42 13350 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 13351 {
c0ebe874 13352 if (op[0] == op[1])
82dd0097
CD
13353 {
13354 tempreg = AT;
13355 used_at = 1;
13356 }
13357 else
c0ebe874
RS
13358 tempreg = op[0];
13359 macro_build (NULL, "dnegu", "d,w", tempreg, op[2]);
13360 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 13361 break;
82dd0097 13362 }
8fc2e39e 13363 used_at = 1;
c0ebe874
RS
13364 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13365 macro_build (NULL, "dsrlv", "d,t,s", AT, op[1], AT);
13366 macro_build (NULL, "dsllv", "d,t,s", op[0], op[1], op[2]);
13367 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13368 break;
13369
252b5132 13370 case M_ROL:
fef14a42 13371 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13372 {
c0ebe874 13373 if (op[0] == op[1])
82dd0097
CD
13374 {
13375 tempreg = AT;
13376 used_at = 1;
13377 }
13378 else
c0ebe874
RS
13379 tempreg = op[0];
13380 macro_build (NULL, "negu", "d,w", tempreg, op[2]);
13381 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 13382 break;
82dd0097 13383 }
8fc2e39e 13384 used_at = 1;
c0ebe874
RS
13385 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13386 macro_build (NULL, "srlv", "d,t,s", AT, op[1], AT);
13387 macro_build (NULL, "sllv", "d,t,s", op[0], op[1], op[2]);
13388 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13389 break;
13390
771c7ce4
TS
13391 case M_DROL_I:
13392 {
13393 unsigned int rot;
e0471c16
TS
13394 const char *l;
13395 const char *rr;
771c7ce4 13396
771c7ce4 13397 rot = imm_expr.X_add_number & 0x3f;
fef14a42 13398 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
13399 {
13400 rot = (64 - rot) & 0x3f;
13401 if (rot >= 32)
c0ebe874 13402 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
60b63b72 13403 else
c0ebe874 13404 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13405 break;
60b63b72 13406 }
483fc7cd 13407 if (rot == 0)
483fc7cd 13408 {
c0ebe874 13409 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13410 break;
483fc7cd 13411 }
82dd0097 13412 l = (rot < 0x20) ? "dsll" : "dsll32";
91d6fa6a 13413 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
82dd0097 13414 rot &= 0x1f;
8fc2e39e 13415 used_at = 1;
c0ebe874
RS
13416 macro_build (NULL, l, SHFT_FMT, AT, op[1], rot);
13417 macro_build (NULL, rr, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13418 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13419 }
13420 break;
13421
252b5132 13422 case M_ROL_I:
771c7ce4
TS
13423 {
13424 unsigned int rot;
13425
771c7ce4 13426 rot = imm_expr.X_add_number & 0x1f;
fef14a42 13427 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 13428 {
c0ebe874
RS
13429 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1],
13430 (32 - rot) & 0x1f);
8fc2e39e 13431 break;
60b63b72 13432 }
483fc7cd 13433 if (rot == 0)
483fc7cd 13434 {
c0ebe874 13435 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13436 break;
483fc7cd 13437 }
8fc2e39e 13438 used_at = 1;
c0ebe874
RS
13439 macro_build (NULL, "sll", SHFT_FMT, AT, op[1], rot);
13440 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13441 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13442 }
13443 break;
13444
13445 case M_DROR:
fef14a42 13446 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 13447 {
c0ebe874 13448 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 13449 break;
82dd0097 13450 }
8fc2e39e 13451 used_at = 1;
c0ebe874
RS
13452 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13453 macro_build (NULL, "dsllv", "d,t,s", AT, op[1], AT);
13454 macro_build (NULL, "dsrlv", "d,t,s", op[0], op[1], op[2]);
13455 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13456 break;
13457
13458 case M_ROR:
fef14a42 13459 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13460 {
c0ebe874 13461 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 13462 break;
82dd0097 13463 }
8fc2e39e 13464 used_at = 1;
c0ebe874
RS
13465 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13466 macro_build (NULL, "sllv", "d,t,s", AT, op[1], AT);
13467 macro_build (NULL, "srlv", "d,t,s", op[0], op[1], op[2]);
13468 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13469 break;
13470
771c7ce4
TS
13471 case M_DROR_I:
13472 {
13473 unsigned int rot;
e0471c16
TS
13474 const char *l;
13475 const char *rr;
771c7ce4 13476
771c7ce4 13477 rot = imm_expr.X_add_number & 0x3f;
fef14a42 13478 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
13479 {
13480 if (rot >= 32)
c0ebe874 13481 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
82dd0097 13482 else
c0ebe874 13483 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13484 break;
82dd0097 13485 }
483fc7cd 13486 if (rot == 0)
483fc7cd 13487 {
c0ebe874 13488 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13489 break;
483fc7cd 13490 }
91d6fa6a 13491 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
82dd0097
CD
13492 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
13493 rot &= 0x1f;
8fc2e39e 13494 used_at = 1;
c0ebe874
RS
13495 macro_build (NULL, rr, SHFT_FMT, AT, op[1], rot);
13496 macro_build (NULL, l, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13497 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13498 }
13499 break;
13500
252b5132 13501 case M_ROR_I:
771c7ce4
TS
13502 {
13503 unsigned int rot;
13504
771c7ce4 13505 rot = imm_expr.X_add_number & 0x1f;
fef14a42 13506 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13507 {
c0ebe874 13508 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13509 break;
82dd0097 13510 }
483fc7cd 13511 if (rot == 0)
483fc7cd 13512 {
c0ebe874 13513 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13514 break;
483fc7cd 13515 }
8fc2e39e 13516 used_at = 1;
c0ebe874
RS
13517 macro_build (NULL, "srl", SHFT_FMT, AT, op[1], rot);
13518 macro_build (NULL, "sll", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13519 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4 13520 }
252b5132
RH
13521 break;
13522
252b5132 13523 case M_SEQ:
c0ebe874
RS
13524 if (op[1] == 0)
13525 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[2], BFD_RELOC_LO16);
13526 else if (op[2] == 0)
13527 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
13528 else
13529 {
c0ebe874
RS
13530 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13531 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
252b5132 13532 }
8fc2e39e 13533 break;
252b5132
RH
13534
13535 case M_SEQ_I:
b0e6f033 13536 if (imm_expr.X_add_number == 0)
252b5132 13537 {
c0ebe874 13538 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 13539 break;
252b5132 13540 }
c0ebe874 13541 if (op[1] == 0)
252b5132 13542 {
1661c76c 13543 as_warn (_("instruction %s: result is always false"),
252b5132 13544 ip->insn_mo->name);
c0ebe874 13545 move_register (op[0], 0);
8fc2e39e 13546 break;
252b5132 13547 }
dd3cbb7e
NC
13548 if (CPU_HAS_SEQ (mips_opts.arch)
13549 && -512 <= imm_expr.X_add_number
13550 && imm_expr.X_add_number < 512)
13551 {
c0ebe874 13552 macro_build (NULL, "seqi", "t,r,+Q", op[0], op[1],
750bdd57 13553 (int) imm_expr.X_add_number);
dd3cbb7e
NC
13554 break;
13555 }
b0e6f033 13556 if (imm_expr.X_add_number >= 0
252b5132 13557 && imm_expr.X_add_number < 0x10000)
c0ebe874 13558 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1], BFD_RELOC_LO16);
b0e6f033 13559 else if (imm_expr.X_add_number > -0x8000
252b5132
RH
13560 && imm_expr.X_add_number < 0)
13561 {
13562 imm_expr.X_add_number = -imm_expr.X_add_number;
bad1aba3 13563 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
c0ebe874 13564 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 13565 }
dd3cbb7e
NC
13566 else if (CPU_HAS_SEQ (mips_opts.arch))
13567 {
13568 used_at = 1;
bad1aba3 13569 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13570 macro_build (NULL, "seq", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
13571 break;
13572 }
252b5132
RH
13573 else
13574 {
bad1aba3 13575 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13576 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
13577 used_at = 1;
13578 }
c0ebe874 13579 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13580 break;
252b5132 13581
c0ebe874 13582 case M_SGE: /* X >= Y <==> not (X < Y) */
252b5132
RH
13583 s = "slt";
13584 goto sge;
13585 case M_SGEU:
13586 s = "sltu";
13587 sge:
c0ebe874
RS
13588 macro_build (NULL, s, "d,v,t", op[0], op[1], op[2]);
13589 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13590 break;
252b5132 13591
6f2117ba 13592 case M_SGE_I: /* X >= I <==> not (X < I). */
252b5132 13593 case M_SGEU_I:
b0e6f033 13594 if (imm_expr.X_add_number >= -0x8000
252b5132 13595 && imm_expr.X_add_number < 0x8000)
c0ebe874
RS
13596 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
13597 op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
13598 else
13599 {
bad1aba3 13600 load_register (AT, &imm_expr, GPR_SIZE == 64);
67c0d1eb 13601 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
c0ebe874 13602 op[0], op[1], AT);
252b5132
RH
13603 used_at = 1;
13604 }
c0ebe874 13605 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13606 break;
252b5132 13607
6f2117ba 13608 case M_SGT: /* X > Y <==> Y < X. */
252b5132
RH
13609 s = "slt";
13610 goto sgt;
13611 case M_SGTU:
13612 s = "sltu";
13613 sgt:
c0ebe874 13614 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
8fc2e39e 13615 break;
252b5132 13616
6f2117ba 13617 case M_SGT_I: /* X > I <==> I < X. */
252b5132
RH
13618 s = "slt";
13619 goto sgti;
13620 case M_SGTU_I:
13621 s = "sltu";
13622 sgti:
8fc2e39e 13623 used_at = 1;
bad1aba3 13624 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13625 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
252b5132
RH
13626 break;
13627
6f2117ba 13628 case M_SLE: /* X <= Y <==> Y >= X <==> not (Y < X). */
252b5132
RH
13629 s = "slt";
13630 goto sle;
13631 case M_SLEU:
13632 s = "sltu";
13633 sle:
c0ebe874
RS
13634 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13635 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13636 break;
252b5132 13637
c0ebe874 13638 case M_SLE_I: /* X <= I <==> I >= X <==> not (I < X) */
252b5132
RH
13639 s = "slt";
13640 goto slei;
13641 case M_SLEU_I:
13642 s = "sltu";
13643 slei:
8fc2e39e 13644 used_at = 1;
bad1aba3 13645 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874
RS
13646 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13647 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
252b5132
RH
13648 break;
13649
13650 case M_SLT_I:
b0e6f033 13651 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
13652 && imm_expr.X_add_number < 0x8000)
13653 {
c0ebe874
RS
13654 macro_build (&imm_expr, "slti", "t,r,j", op[0], op[1],
13655 BFD_RELOC_LO16);
8fc2e39e 13656 break;
252b5132 13657 }
8fc2e39e 13658 used_at = 1;
bad1aba3 13659 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13660 macro_build (NULL, "slt", "d,v,t", op[0], op[1], AT);
252b5132
RH
13661 break;
13662
13663 case M_SLTU_I:
b0e6f033 13664 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
13665 && imm_expr.X_add_number < 0x8000)
13666 {
c0ebe874 13667 macro_build (&imm_expr, "sltiu", "t,r,j", op[0], op[1],
17a2f251 13668 BFD_RELOC_LO16);
8fc2e39e 13669 break;
252b5132 13670 }
8fc2e39e 13671 used_at = 1;
bad1aba3 13672 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13673 macro_build (NULL, "sltu", "d,v,t", op[0], op[1], AT);
252b5132
RH
13674 break;
13675
13676 case M_SNE:
c0ebe874
RS
13677 if (op[1] == 0)
13678 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[2]);
13679 else if (op[2] == 0)
13680 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
252b5132
RH
13681 else
13682 {
c0ebe874
RS
13683 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13684 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
252b5132 13685 }
8fc2e39e 13686 break;
252b5132
RH
13687
13688 case M_SNE_I:
b0e6f033 13689 if (imm_expr.X_add_number == 0)
252b5132 13690 {
c0ebe874 13691 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
8fc2e39e 13692 break;
252b5132 13693 }
c0ebe874 13694 if (op[1] == 0)
252b5132 13695 {
1661c76c 13696 as_warn (_("instruction %s: result is always true"),
252b5132 13697 ip->insn_mo->name);
bad1aba3 13698 macro_build (&expr1, GPR_SIZE == 32 ? "addiu" : "daddiu", "t,r,j",
c0ebe874 13699 op[0], 0, BFD_RELOC_LO16);
8fc2e39e 13700 break;
252b5132 13701 }
dd3cbb7e
NC
13702 if (CPU_HAS_SEQ (mips_opts.arch)
13703 && -512 <= imm_expr.X_add_number
13704 && imm_expr.X_add_number < 512)
13705 {
c0ebe874 13706 macro_build (NULL, "snei", "t,r,+Q", op[0], op[1],
750bdd57 13707 (int) imm_expr.X_add_number);
dd3cbb7e
NC
13708 break;
13709 }
b0e6f033 13710 if (imm_expr.X_add_number >= 0
252b5132
RH
13711 && imm_expr.X_add_number < 0x10000)
13712 {
c0ebe874
RS
13713 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1],
13714 BFD_RELOC_LO16);
252b5132 13715 }
b0e6f033 13716 else if (imm_expr.X_add_number > -0x8000
252b5132
RH
13717 && imm_expr.X_add_number < 0)
13718 {
13719 imm_expr.X_add_number = -imm_expr.X_add_number;
bad1aba3 13720 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
c0ebe874 13721 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 13722 }
dd3cbb7e
NC
13723 else if (CPU_HAS_SEQ (mips_opts.arch))
13724 {
13725 used_at = 1;
bad1aba3 13726 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13727 macro_build (NULL, "sne", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
13728 break;
13729 }
252b5132
RH
13730 else
13731 {
bad1aba3 13732 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13733 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
13734 used_at = 1;
13735 }
c0ebe874 13736 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
8fc2e39e 13737 break;
252b5132 13738
df58fc94
RS
13739 case M_SUB_I:
13740 s = "addi";
13741 s2 = "sub";
387e7624
FS
13742 if (ISA_IS_R6 (mips_opts.isa))
13743 goto do_subi_i;
13744 else
13745 goto do_subi;
df58fc94
RS
13746 case M_SUBU_I:
13747 s = "addiu";
13748 s2 = "subu";
13749 goto do_subi;
252b5132
RH
13750 case M_DSUB_I:
13751 dbl = 1;
df58fc94
RS
13752 s = "daddi";
13753 s2 = "dsub";
387e7624 13754 if (!mips_opts.micromips && !ISA_IS_R6 (mips_opts.isa))
df58fc94 13755 goto do_subi;
b0e6f033 13756 if (imm_expr.X_add_number > -0x200
387e7624
FS
13757 && imm_expr.X_add_number <= 0x200
13758 && !ISA_IS_R6 (mips_opts.isa))
252b5132 13759 {
b0e6f033
RS
13760 macro_build (NULL, s, "t,r,.", op[0], op[1],
13761 (int) -imm_expr.X_add_number);
8fc2e39e 13762 break;
252b5132 13763 }
df58fc94 13764 goto do_subi_i;
252b5132
RH
13765 case M_DSUBU_I:
13766 dbl = 1;
df58fc94
RS
13767 s = "daddiu";
13768 s2 = "dsubu";
13769 do_subi:
b0e6f033 13770 if (imm_expr.X_add_number > -0x8000
252b5132
RH
13771 && imm_expr.X_add_number <= 0x8000)
13772 {
13773 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 13774 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 13775 break;
252b5132 13776 }
df58fc94 13777 do_subi_i:
8fc2e39e 13778 used_at = 1;
67c0d1eb 13779 load_register (AT, &imm_expr, dbl);
c0ebe874 13780 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
13781 break;
13782
13783 case M_TEQ_I:
13784 s = "teq";
13785 goto trap;
13786 case M_TGE_I:
13787 s = "tge";
13788 goto trap;
13789 case M_TGEU_I:
13790 s = "tgeu";
13791 goto trap;
13792 case M_TLT_I:
13793 s = "tlt";
13794 goto trap;
13795 case M_TLTU_I:
13796 s = "tltu";
13797 goto trap;
13798 case M_TNE_I:
13799 s = "tne";
13800 trap:
8fc2e39e 13801 used_at = 1;
bad1aba3 13802 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13803 macro_build (NULL, s, "s,t", op[0], AT);
252b5132
RH
13804 break;
13805
252b5132 13806 case M_TRUNCWS:
43841e91 13807 case M_TRUNCWD:
df58fc94 13808 gas_assert (!mips_opts.micromips);
0aa27725 13809 gas_assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 13810 used_at = 1;
252b5132
RH
13811
13812 /*
13813 * Is the double cfc1 instruction a bug in the mips assembler;
13814 * or is there a reason for it?
13815 */
7d10b47d 13816 start_noreorder ();
c0ebe874
RS
13817 macro_build (NULL, "cfc1", "t,G", op[2], RA);
13818 macro_build (NULL, "cfc1", "t,G", op[2], RA);
67c0d1eb 13819 macro_build (NULL, "nop", "");
252b5132 13820 expr1.X_add_number = 3;
c0ebe874 13821 macro_build (&expr1, "ori", "t,r,i", AT, op[2], BFD_RELOC_LO16);
252b5132 13822 expr1.X_add_number = 2;
67c0d1eb
RS
13823 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
13824 macro_build (NULL, "ctc1", "t,G", AT, RA);
13825 macro_build (NULL, "nop", "");
13826 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
c0ebe874
RS
13827 op[0], op[1]);
13828 macro_build (NULL, "ctc1", "t,G", op[2], RA);
67c0d1eb 13829 macro_build (NULL, "nop", "");
7d10b47d 13830 end_noreorder ();
252b5132
RH
13831 break;
13832
f2ae14a1 13833 case M_ULH_AB:
252b5132 13834 s = "lb";
df58fc94
RS
13835 s2 = "lbu";
13836 off = 1;
13837 goto uld_st;
f2ae14a1 13838 case M_ULHU_AB:
252b5132 13839 s = "lbu";
df58fc94
RS
13840 s2 = "lbu";
13841 off = 1;
13842 goto uld_st;
f2ae14a1 13843 case M_ULW_AB:
df58fc94
RS
13844 s = "lwl";
13845 s2 = "lwr";
7f3c4072 13846 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
13847 off = 3;
13848 goto uld_st;
f2ae14a1 13849 case M_ULD_AB:
252b5132
RH
13850 s = "ldl";
13851 s2 = "ldr";
7f3c4072 13852 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 13853 off = 7;
df58fc94 13854 goto uld_st;
f2ae14a1 13855 case M_USH_AB:
df58fc94
RS
13856 s = "sb";
13857 s2 = "sb";
13858 off = 1;
13859 ust = 1;
13860 goto uld_st;
f2ae14a1 13861 case M_USW_AB:
df58fc94
RS
13862 s = "swl";
13863 s2 = "swr";
7f3c4072 13864 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 13865 off = 3;
df58fc94
RS
13866 ust = 1;
13867 goto uld_st;
f2ae14a1 13868 case M_USD_AB:
df58fc94
RS
13869 s = "sdl";
13870 s2 = "sdr";
7f3c4072 13871 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
13872 off = 7;
13873 ust = 1;
13874
13875 uld_st:
c0ebe874 13876 breg = op[2];
f2ae14a1 13877 large_offset = !small_offset_p (off, align, offbits);
df58fc94
RS
13878 ep = &offset_expr;
13879 expr1.X_add_number = 0;
f2ae14a1 13880 if (large_offset)
df58fc94
RS
13881 {
13882 used_at = 1;
13883 tempreg = AT;
f2ae14a1
RS
13884 if (small_offset_p (0, align, 16))
13885 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg, -1,
13886 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
13887 else
13888 {
13889 load_address (tempreg, ep, &used_at);
13890 if (breg != 0)
13891 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
13892 tempreg, tempreg, breg);
13893 }
13894 offset_reloc[0] = BFD_RELOC_LO16;
13895 offset_reloc[1] = BFD_RELOC_UNUSED;
13896 offset_reloc[2] = BFD_RELOC_UNUSED;
df58fc94 13897 breg = tempreg;
c0ebe874 13898 tempreg = op[0];
df58fc94
RS
13899 ep = &expr1;
13900 }
c0ebe874 13901 else if (!ust && op[0] == breg)
8fc2e39e
TS
13902 {
13903 used_at = 1;
13904 tempreg = AT;
13905 }
252b5132 13906 else
c0ebe874 13907 tempreg = op[0];
af22f5b2 13908
df58fc94
RS
13909 if (off == 1)
13910 goto ulh_sh;
252b5132 13911
90ecf173 13912 if (!target_big_endian)
df58fc94 13913 ep->X_add_number += off;
f2ae14a1 13914 if (offbits == 12)
c8276761 13915 macro_build (NULL, s, "t,~(b)", tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
13916 else
13917 macro_build (ep, s, "t,o(b)", tempreg, -1,
13918 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94 13919
90ecf173 13920 if (!target_big_endian)
df58fc94 13921 ep->X_add_number -= off;
252b5132 13922 else
df58fc94 13923 ep->X_add_number += off;
f2ae14a1 13924 if (offbits == 12)
df58fc94 13925 macro_build (NULL, s2, "t,~(b)",
c8276761 13926 tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
13927 else
13928 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13929 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 13930
df58fc94 13931 /* If necessary, move the result in tempreg to the final destination. */
c0ebe874 13932 if (!ust && op[0] != tempreg)
df58fc94
RS
13933 {
13934 /* Protect second load's delay slot. */
13935 load_delay_nop ();
c0ebe874 13936 move_register (op[0], tempreg);
df58fc94 13937 }
8fc2e39e 13938 break;
252b5132 13939
df58fc94 13940 ulh_sh:
d6bc6245 13941 used_at = 1;
df58fc94
RS
13942 if (target_big_endian == ust)
13943 ep->X_add_number += off;
c0ebe874 13944 tempreg = ust || large_offset ? op[0] : AT;
f2ae14a1
RS
13945 macro_build (ep, s, "t,o(b)", tempreg, -1,
13946 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94
RS
13947
13948 /* For halfword transfers we need a temporary register to shuffle
13949 bytes. Unfortunately for M_USH_A we have none available before
13950 the next store as AT holds the base address. We deal with this
13951 case by clobbering TREG and then restoring it as with ULH. */
c0ebe874 13952 tempreg = ust == large_offset ? op[0] : AT;
df58fc94 13953 if (ust)
c0ebe874 13954 macro_build (NULL, "srl", SHFT_FMT, tempreg, op[0], 8);
df58fc94
RS
13955
13956 if (target_big_endian == ust)
13957 ep->X_add_number -= off;
252b5132 13958 else
df58fc94 13959 ep->X_add_number += off;
f2ae14a1
RS
13960 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13961 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 13962
df58fc94 13963 /* For M_USH_A re-retrieve the LSB. */
f2ae14a1 13964 if (ust && large_offset)
df58fc94
RS
13965 {
13966 if (target_big_endian)
13967 ep->X_add_number += off;
13968 else
13969 ep->X_add_number -= off;
f2ae14a1
RS
13970 macro_build (&expr1, "lbu", "t,o(b)", AT, -1,
13971 offset_reloc[0], offset_reloc[1], offset_reloc[2], AT);
df58fc94
RS
13972 }
13973 /* For ULH and M_USH_A OR the LSB in. */
f2ae14a1 13974 if (!ust || large_offset)
df58fc94 13975 {
c0ebe874 13976 tempreg = !large_offset ? AT : op[0];
df58fc94 13977 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
c0ebe874 13978 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
df58fc94 13979 }
252b5132
RH
13980 break;
13981
13982 default:
13983 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 13984 are added dynamically. */
1661c76c 13985 as_bad (_("macro %s not implemented yet"), ip->insn_mo->name);
252b5132
RH
13986 break;
13987 }
741fe287 13988 if (!mips_opts.at && used_at)
1661c76c 13989 as_bad (_("macro used $at after \".set noat\""));
252b5132
RH
13990}
13991
13992/* Implement macros in mips16 mode. */
13993
13994static void
17a2f251 13995mips16_macro (struct mips_cl_insn *ip)
252b5132 13996{
c0ebe874 13997 const struct mips_operand_array *operands;
252b5132 13998 int mask;
c0ebe874 13999 int tmp;
252b5132
RH
14000 expressionS expr1;
14001 int dbl;
14002 const char *s, *s2, *s3;
c0ebe874
RS
14003 unsigned int op[MAX_OPERANDS];
14004 unsigned int i;
252b5132
RH
14005
14006 mask = ip->insn_mo->mask;
14007
c0ebe874
RS
14008 operands = insn_operands (ip);
14009 for (i = 0; i < MAX_OPERANDS; i++)
14010 if (operands->operand[i])
14011 op[i] = insn_extract_operand (ip, operands->operand[i]);
14012 else
14013 op[i] = -1;
252b5132 14014
252b5132
RH
14015 expr1.X_op = O_constant;
14016 expr1.X_op_symbol = NULL;
14017 expr1.X_add_symbol = NULL;
14018 expr1.X_add_number = 1;
14019
14020 dbl = 0;
14021
14022 switch (mask)
14023 {
14024 default:
b37df7c4 14025 abort ();
252b5132
RH
14026
14027 case M_DDIV_3:
14028 dbl = 1;
1a0670f3 14029 /* Fall through. */
252b5132
RH
14030 case M_DIV_3:
14031 s = "mflo";
14032 goto do_div3;
14033 case M_DREM_3:
14034 dbl = 1;
1a0670f3 14035 /* Fall through. */
252b5132
RH
14036 case M_REM_3:
14037 s = "mfhi";
14038 do_div3:
7d10b47d 14039 start_noreorder ();
d8722d76 14040 macro_build (NULL, dbl ? "ddiv" : "div", ".,x,y", op[1], op[2]);
252b5132 14041 expr1.X_add_number = 2;
c0ebe874 14042 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 14043 macro_build (NULL, "break", "6", 7);
bdaaa2e1 14044
252b5132
RH
14045 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
14046 since that causes an overflow. We should do that as well,
14047 but I don't see how to do the comparisons without a temporary
14048 register. */
7d10b47d 14049 end_noreorder ();
c0ebe874 14050 macro_build (NULL, s, "x", op[0]);
252b5132
RH
14051 break;
14052
14053 case M_DIVU_3:
14054 s = "divu";
14055 s2 = "mflo";
14056 goto do_divu3;
14057 case M_REMU_3:
14058 s = "divu";
14059 s2 = "mfhi";
14060 goto do_divu3;
14061 case M_DDIVU_3:
14062 s = "ddivu";
14063 s2 = "mflo";
14064 goto do_divu3;
14065 case M_DREMU_3:
14066 s = "ddivu";
14067 s2 = "mfhi";
14068 do_divu3:
7d10b47d 14069 start_noreorder ();
d8722d76 14070 macro_build (NULL, s, ".,x,y", op[1], op[2]);
252b5132 14071 expr1.X_add_number = 2;
c0ebe874 14072 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 14073 macro_build (NULL, "break", "6", 7);
7d10b47d 14074 end_noreorder ();
c0ebe874 14075 macro_build (NULL, s2, "x", op[0]);
252b5132
RH
14076 break;
14077
14078 case M_DMUL:
14079 dbl = 1;
1a0670f3 14080 /* Fall through. */
252b5132 14081 case M_MUL:
c0ebe874
RS
14082 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", op[1], op[2]);
14083 macro_build (NULL, "mflo", "x", op[0]);
8fc2e39e 14084 break;
252b5132
RH
14085
14086 case M_DSUBU_I:
14087 dbl = 1;
14088 goto do_subu;
14089 case M_SUBU_I:
14090 do_subu:
252b5132 14091 imm_expr.X_add_number = -imm_expr.X_add_number;
d8722d76 14092 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,F", op[0], op[1]);
252b5132
RH
14093 break;
14094
14095 case M_SUBU_I_2:
252b5132 14096 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 14097 macro_build (&imm_expr, "addiu", "x,k", op[0]);
252b5132
RH
14098 break;
14099
14100 case M_DSUBU_I_2:
252b5132 14101 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 14102 macro_build (&imm_expr, "daddiu", "y,j", op[0]);
252b5132
RH
14103 break;
14104
14105 case M_BEQ:
14106 s = "cmp";
14107 s2 = "bteqz";
14108 goto do_branch;
14109 case M_BNE:
14110 s = "cmp";
14111 s2 = "btnez";
14112 goto do_branch;
14113 case M_BLT:
14114 s = "slt";
14115 s2 = "btnez";
14116 goto do_branch;
14117 case M_BLTU:
14118 s = "sltu";
14119 s2 = "btnez";
14120 goto do_branch;
14121 case M_BLE:
14122 s = "slt";
14123 s2 = "bteqz";
14124 goto do_reverse_branch;
14125 case M_BLEU:
14126 s = "sltu";
14127 s2 = "bteqz";
14128 goto do_reverse_branch;
14129 case M_BGE:
14130 s = "slt";
14131 s2 = "bteqz";
14132 goto do_branch;
14133 case M_BGEU:
14134 s = "sltu";
14135 s2 = "bteqz";
14136 goto do_branch;
14137 case M_BGT:
14138 s = "slt";
14139 s2 = "btnez";
14140 goto do_reverse_branch;
14141 case M_BGTU:
14142 s = "sltu";
14143 s2 = "btnez";
14144
14145 do_reverse_branch:
c0ebe874
RS
14146 tmp = op[1];
14147 op[1] = op[0];
14148 op[0] = tmp;
252b5132
RH
14149
14150 do_branch:
c0ebe874 14151 macro_build (NULL, s, "x,y", op[0], op[1]);
67c0d1eb 14152 macro_build (&offset_expr, s2, "p");
252b5132
RH
14153 break;
14154
14155 case M_BEQ_I:
14156 s = "cmpi";
14157 s2 = "bteqz";
14158 s3 = "x,U";
14159 goto do_branch_i;
14160 case M_BNE_I:
14161 s = "cmpi";
14162 s2 = "btnez";
14163 s3 = "x,U";
14164 goto do_branch_i;
14165 case M_BLT_I:
14166 s = "slti";
14167 s2 = "btnez";
14168 s3 = "x,8";
14169 goto do_branch_i;
14170 case M_BLTU_I:
14171 s = "sltiu";
14172 s2 = "btnez";
14173 s3 = "x,8";
14174 goto do_branch_i;
14175 case M_BLE_I:
14176 s = "slti";
14177 s2 = "btnez";
14178 s3 = "x,8";
14179 goto do_addone_branch_i;
14180 case M_BLEU_I:
14181 s = "sltiu";
14182 s2 = "btnez";
14183 s3 = "x,8";
14184 goto do_addone_branch_i;
14185 case M_BGE_I:
14186 s = "slti";
14187 s2 = "bteqz";
14188 s3 = "x,8";
14189 goto do_branch_i;
14190 case M_BGEU_I:
14191 s = "sltiu";
14192 s2 = "bteqz";
14193 s3 = "x,8";
14194 goto do_branch_i;
14195 case M_BGT_I:
14196 s = "slti";
14197 s2 = "bteqz";
14198 s3 = "x,8";
14199 goto do_addone_branch_i;
14200 case M_BGTU_I:
14201 s = "sltiu";
14202 s2 = "bteqz";
14203 s3 = "x,8";
14204
14205 do_addone_branch_i:
252b5132
RH
14206 ++imm_expr.X_add_number;
14207
14208 do_branch_i:
c0ebe874 14209 macro_build (&imm_expr, s, s3, op[0]);
67c0d1eb 14210 macro_build (&offset_expr, s2, "p");
252b5132
RH
14211 break;
14212
14213 case M_ABS:
14214 expr1.X_add_number = 0;
c0ebe874
RS
14215 macro_build (&expr1, "slti", "x,8", op[1]);
14216 if (op[0] != op[1])
14217 macro_build (NULL, "move", "y,X", op[0], mips16_to_32_reg_map[op[1]]);
252b5132 14218 expr1.X_add_number = 2;
67c0d1eb 14219 macro_build (&expr1, "bteqz", "p");
c0ebe874 14220 macro_build (NULL, "neg", "x,w", op[0], op[0]);
0acfaea6 14221 break;
252b5132
RH
14222 }
14223}
14224
14daeee3
RS
14225/* Look up instruction [START, START + LENGTH) in HASH. Record any extra
14226 opcode bits in *OPCODE_EXTRA. */
14227
14228static struct mips_opcode *
14229mips_lookup_insn (struct hash_control *hash, const char *start,
da8bca91 14230 ssize_t length, unsigned int *opcode_extra)
14daeee3
RS
14231{
14232 char *name, *dot, *p;
14233 unsigned int mask, suffix;
da8bca91 14234 ssize_t opend;
14daeee3
RS
14235 struct mips_opcode *insn;
14236
14237 /* Make a copy of the instruction so that we can fiddle with it. */
4ec9d7d5 14238 name = xstrndup (start, length);
14daeee3
RS
14239
14240 /* Look up the instruction as-is. */
14241 insn = (struct mips_opcode *) hash_find (hash, name);
ee5734f0 14242 if (insn)
e1fa0163 14243 goto end;
14daeee3
RS
14244
14245 dot = strchr (name, '.');
14246 if (dot && dot[1])
14247 {
14248 /* Try to interpret the text after the dot as a VU0 channel suffix. */
14249 p = mips_parse_vu0_channels (dot + 1, &mask);
14250 if (*p == 0 && mask != 0)
14251 {
14252 *dot = 0;
14253 insn = (struct mips_opcode *) hash_find (hash, name);
14254 *dot = '.';
14255 if (insn && (insn->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0)
14256 {
14257 *opcode_extra |= mask << mips_vu0_channel_mask.lsb;
e1fa0163 14258 goto end;
14daeee3
RS
14259 }
14260 }
14261 }
14262
14263 if (mips_opts.micromips)
14264 {
14265 /* See if there's an instruction size override suffix,
14266 either `16' or `32', at the end of the mnemonic proper,
14267 that defines the operation, i.e. before the first `.'
14268 character if any. Strip it and retry. */
14269 opend = dot != NULL ? dot - name : length;
14270 if (opend >= 3 && name[opend - 2] == '1' && name[opend - 1] == '6')
14271 suffix = 2;
3076e594 14272 else if (opend >= 2 && name[opend - 2] == '3' && name[opend - 1] == '2')
14daeee3
RS
14273 suffix = 4;
14274 else
14275 suffix = 0;
14276 if (suffix)
14277 {
39334a61 14278 memmove (name + opend - 2, name + opend, length - opend + 1);
14daeee3 14279 insn = (struct mips_opcode *) hash_find (hash, name);
ee5734f0 14280 if (insn)
14daeee3
RS
14281 {
14282 forced_insn_length = suffix;
e1fa0163 14283 goto end;
14daeee3
RS
14284 }
14285 }
14286 }
14287
e1fa0163
NC
14288 insn = NULL;
14289 end:
14290 free (name);
14291 return insn;
14daeee3
RS
14292}
14293
77bd4346 14294/* Assemble an instruction into its binary format. If the instruction
e423441d
RS
14295 is a macro, set imm_expr and offset_expr to the values associated
14296 with "I" and "A" operands respectively. Otherwise store the value
14297 of the relocatable field (if any) in offset_expr. In both cases
14298 set offset_reloc to the relocation operators applied to offset_expr. */
252b5132
RH
14299
14300static void
60f20e8b 14301mips_ip (char *str, struct mips_cl_insn *insn)
252b5132 14302{
60f20e8b 14303 const struct mips_opcode *first, *past;
df58fc94 14304 struct hash_control *hash;
a92713e6 14305 char format;
14daeee3 14306 size_t end;
a92713e6 14307 struct mips_operand_token *tokens;
14daeee3 14308 unsigned int opcode_extra;
252b5132 14309
df58fc94
RS
14310 if (mips_opts.micromips)
14311 {
14312 hash = micromips_op_hash;
14313 past = &micromips_opcodes[bfd_micromips_num_opcodes];
14314 }
14315 else
14316 {
14317 hash = op_hash;
14318 past = &mips_opcodes[NUMOPCODES];
14319 }
14320 forced_insn_length = 0;
14daeee3 14321 opcode_extra = 0;
252b5132 14322
df58fc94 14323 /* We first try to match an instruction up to a space or to the end. */
a40bc9dd
RS
14324 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
14325 continue;
bdaaa2e1 14326
60f20e8b
RS
14327 first = mips_lookup_insn (hash, str, end, &opcode_extra);
14328 if (first == NULL)
252b5132 14329 {
1661c76c 14330 set_insn_error (0, _("unrecognized opcode"));
a40bc9dd 14331 return;
252b5132
RH
14332 }
14333
60f20e8b 14334 if (strcmp (first->name, "li.s") == 0)
a92713e6 14335 format = 'f';
60f20e8b 14336 else if (strcmp (first->name, "li.d") == 0)
a92713e6
RS
14337 format = 'd';
14338 else
14339 format = 0;
14340 tokens = mips_parse_arguments (str + end, format);
14341 if (!tokens)
14342 return;
14343
60f20e8b
RS
14344 if (!match_insns (insn, first, past, tokens, opcode_extra, FALSE)
14345 && !match_insns (insn, first, past, tokens, opcode_extra, TRUE))
1661c76c 14346 set_insn_error (0, _("invalid operands"));
df58fc94 14347
e3de51ce 14348 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
14349}
14350
77bd4346
RS
14351/* As for mips_ip, but used when assembling MIPS16 code.
14352 Also set forced_insn_length to the resulting instruction size in
14353 bytes if the user explicitly requested a small or extended instruction. */
252b5132
RH
14354
14355static void
60f20e8b 14356mips16_ip (char *str, struct mips_cl_insn *insn)
252b5132 14357{
1a00e612 14358 char *end, *s, c;
60f20e8b 14359 struct mips_opcode *first;
a92713e6 14360 struct mips_operand_token *tokens;
3fb49709 14361 unsigned int l;
252b5132 14362
25499ac7 14363 for (s = str; *s != '\0' && *s != '.' && *s != ' '; ++s)
252b5132 14364 ;
1a00e612
RS
14365 end = s;
14366 c = *end;
3fb49709
MR
14367
14368 l = 0;
1a00e612 14369 switch (c)
252b5132
RH
14370 {
14371 case '\0':
14372 break;
14373
14374 case ' ':
1a00e612 14375 s++;
252b5132
RH
14376 break;
14377
14378 case '.':
3fb49709
MR
14379 s++;
14380 if (*s == 't')
252b5132 14381 {
3fb49709
MR
14382 l = 2;
14383 s++;
252b5132 14384 }
3fb49709 14385 else if (*s == 'e')
252b5132 14386 {
3fb49709
MR
14387 l = 4;
14388 s++;
252b5132 14389 }
3fb49709
MR
14390 if (*s == '\0')
14391 break;
14392 else if (*s++ == ' ')
14393 break;
1661c76c 14394 set_insn_error (0, _("unrecognized opcode"));
252b5132
RH
14395 return;
14396 }
3fb49709 14397 forced_insn_length = l;
252b5132 14398
1a00e612 14399 *end = 0;
60f20e8b 14400 first = (struct mips_opcode *) hash_find (mips16_op_hash, str);
1a00e612
RS
14401 *end = c;
14402
60f20e8b 14403 if (!first)
252b5132 14404 {
1661c76c 14405 set_insn_error (0, _("unrecognized opcode"));
252b5132
RH
14406 return;
14407 }
14408
a92713e6
RS
14409 tokens = mips_parse_arguments (s, 0);
14410 if (!tokens)
14411 return;
14412
60f20e8b 14413 if (!match_mips16_insns (insn, first, tokens))
1661c76c 14414 set_insn_error (0, _("invalid operands"));
252b5132 14415
e3de51ce 14416 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
14417}
14418
b886a2ab
RS
14419/* Marshal immediate value VAL for an extended MIPS16 instruction.
14420 NBITS is the number of significant bits in VAL. */
14421
14422static unsigned long
14423mips16_immed_extend (offsetT val, unsigned int nbits)
14424{
14425 int extval;
25499ac7
MR
14426
14427 extval = 0;
14428 val &= (1U << nbits) - 1;
14429 if (nbits == 16 || nbits == 9)
b886a2ab
RS
14430 {
14431 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14432 val &= 0x1f;
14433 }
14434 else if (nbits == 15)
14435 {
14436 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14437 val &= 0xf;
14438 }
25499ac7 14439 else if (nbits == 6)
b886a2ab
RS
14440 {
14441 extval = ((val & 0x1f) << 6) | (val & 0x20);
14442 val = 0;
14443 }
14444 return (extval << 16) | val;
14445}
14446
3ccad066
RS
14447/* Like decode_mips16_operand, but require the operand to be defined and
14448 require it to be an integer. */
14449
14450static const struct mips_int_operand *
14451mips16_immed_operand (int type, bfd_boolean extended_p)
14452{
14453 const struct mips_operand *operand;
14454
14455 operand = decode_mips16_operand (type, extended_p);
14456 if (!operand || (operand->type != OP_INT && operand->type != OP_PCREL))
14457 abort ();
14458 return (const struct mips_int_operand *) operand;
14459}
14460
14461/* Return true if SVAL fits OPERAND. RELOC is as for mips16_immed. */
14462
14463static bfd_boolean
14464mips16_immed_in_range_p (const struct mips_int_operand *operand,
14465 bfd_reloc_code_real_type reloc, offsetT sval)
14466{
14467 int min_val, max_val;
14468
14469 min_val = mips_int_operand_min (operand);
14470 max_val = mips_int_operand_max (operand);
14471 if (reloc != BFD_RELOC_UNUSED)
14472 {
14473 if (min_val < 0)
14474 sval = SEXT_16BIT (sval);
14475 else
14476 sval &= 0xffff;
14477 }
14478
14479 return (sval >= min_val
14480 && sval <= max_val
14481 && (sval & ((1 << operand->shift) - 1)) == 0);
14482}
14483
5c04167a
RS
14484/* Install immediate value VAL into MIPS16 instruction *INSN,
14485 extending it if necessary. The instruction in *INSN may
14486 already be extended.
14487
43c0598f
RS
14488 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14489 if none. In the former case, VAL is a 16-bit number with no
14490 defined signedness.
14491
14492 TYPE is the type of the immediate field. USER_INSN_LENGTH
14493 is the length that the user requested, or 0 if none. */
252b5132
RH
14494
14495static void
3b4dbbbf 14496mips16_immed (const char *file, unsigned int line, int type,
43c0598f 14497 bfd_reloc_code_real_type reloc, offsetT val,
5c04167a 14498 unsigned int user_insn_length, unsigned long *insn)
252b5132 14499{
3ccad066
RS
14500 const struct mips_int_operand *operand;
14501 unsigned int uval, length;
252b5132 14502
3ccad066
RS
14503 operand = mips16_immed_operand (type, FALSE);
14504 if (!mips16_immed_in_range_p (operand, reloc, val))
5c04167a
RS
14505 {
14506 /* We need an extended instruction. */
14507 if (user_insn_length == 2)
14508 as_bad_where (file, line, _("invalid unextended operand value"));
14509 else
14510 *insn |= MIPS16_EXTEND;
14511 }
14512 else if (user_insn_length == 4)
14513 {
14514 /* The operand doesn't force an unextended instruction to be extended.
14515 Warn if the user wanted an extended instruction anyway. */
14516 *insn |= MIPS16_EXTEND;
14517 as_warn_where (file, line,
14518 _("extended operand requested but not required"));
14519 }
252b5132 14520
3ccad066
RS
14521 length = mips16_opcode_length (*insn);
14522 if (length == 4)
252b5132 14523 {
3ccad066
RS
14524 operand = mips16_immed_operand (type, TRUE);
14525 if (!mips16_immed_in_range_p (operand, reloc, val))
14526 as_bad_where (file, line,
14527 _("operand value out of range for instruction"));
252b5132 14528 }
3ccad066 14529 uval = ((unsigned int) val >> operand->shift) - operand->bias;
bdd15286 14530 if (length == 2 || operand->root.lsb != 0)
3ccad066 14531 *insn = mips_insert_operand (&operand->root, *insn, uval);
252b5132 14532 else
3ccad066 14533 *insn |= mips16_immed_extend (uval, operand->root.size);
252b5132
RH
14534}
14535\f
d6f16593 14536struct percent_op_match
ad8d3bb3 14537{
5e0116d5
RS
14538 const char *str;
14539 bfd_reloc_code_real_type reloc;
d6f16593
MR
14540};
14541
14542static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 14543{
5e0116d5 14544 {"%lo", BFD_RELOC_LO16},
5e0116d5
RS
14545 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14546 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14547 {"%call16", BFD_RELOC_MIPS_CALL16},
14548 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14549 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14550 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14551 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14552 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14553 {"%got", BFD_RELOC_MIPS_GOT16},
14554 {"%gp_rel", BFD_RELOC_GPREL16},
be3f1006 14555 {"%gprel", BFD_RELOC_GPREL16},
5e0116d5
RS
14556 {"%half", BFD_RELOC_16},
14557 {"%highest", BFD_RELOC_MIPS_HIGHEST},
14558 {"%higher", BFD_RELOC_MIPS_HIGHER},
14559 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
14560 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14561 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14562 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14563 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14564 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14565 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14566 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
7361da2c
AB
14567 {"%hi", BFD_RELOC_HI16_S},
14568 {"%pcrel_hi", BFD_RELOC_HI16_S_PCREL},
14569 {"%pcrel_lo", BFD_RELOC_LO16_PCREL}
ad8d3bb3
TS
14570};
14571
d6f16593
MR
14572static const struct percent_op_match mips16_percent_op[] =
14573{
14574 {"%lo", BFD_RELOC_MIPS16_LO16},
be3f1006 14575 {"%gp_rel", BFD_RELOC_MIPS16_GPREL},
d6f16593 14576 {"%gprel", BFD_RELOC_MIPS16_GPREL},
738e5348
RS
14577 {"%got", BFD_RELOC_MIPS16_GOT16},
14578 {"%call16", BFD_RELOC_MIPS16_CALL16},
d0f13682
CLT
14579 {"%hi", BFD_RELOC_MIPS16_HI16_S},
14580 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14581 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14582 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14583 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14584 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14585 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14586 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
d6f16593
MR
14587};
14588
252b5132 14589
5e0116d5
RS
14590/* Return true if *STR points to a relocation operator. When returning true,
14591 move *STR over the operator and store its relocation code in *RELOC.
14592 Leave both *STR and *RELOC alone when returning false. */
14593
14594static bfd_boolean
17a2f251 14595parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 14596{
d6f16593
MR
14597 const struct percent_op_match *percent_op;
14598 size_t limit, i;
14599
14600 if (mips_opts.mips16)
14601 {
14602 percent_op = mips16_percent_op;
14603 limit = ARRAY_SIZE (mips16_percent_op);
14604 }
14605 else
14606 {
14607 percent_op = mips_percent_op;
14608 limit = ARRAY_SIZE (mips_percent_op);
14609 }
76b3015f 14610
d6f16593 14611 for (i = 0; i < limit; i++)
5e0116d5 14612 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 14613 {
3f98094e
DJ
14614 int len = strlen (percent_op[i].str);
14615
14616 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14617 continue;
14618
5e0116d5
RS
14619 *str += strlen (percent_op[i].str);
14620 *reloc = percent_op[i].reloc;
394f9b3a 14621
5e0116d5
RS
14622 /* Check whether the output BFD supports this relocation.
14623 If not, issue an error and fall back on something safe. */
14624 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 14625 {
20203fb9 14626 as_bad (_("relocation %s isn't supported by the current ABI"),
5e0116d5 14627 percent_op[i].str);
01a3f561 14628 *reloc = BFD_RELOC_UNUSED;
394f9b3a 14629 }
5e0116d5 14630 return TRUE;
394f9b3a 14631 }
5e0116d5 14632 return FALSE;
394f9b3a 14633}
ad8d3bb3 14634
ad8d3bb3 14635
5e0116d5
RS
14636/* Parse string STR as a 16-bit relocatable operand. Store the
14637 expression in *EP and the relocations in the array starting
14638 at RELOC. Return the number of relocation operators used.
ad8d3bb3 14639
01a3f561 14640 On exit, EXPR_END points to the first character after the expression. */
ad8d3bb3 14641
5e0116d5 14642static size_t
17a2f251
TS
14643my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14644 char *str)
ad8d3bb3 14645{
5e0116d5
RS
14646 bfd_reloc_code_real_type reversed_reloc[3];
14647 size_t reloc_index, i;
09b8f35a
RS
14648 int crux_depth, str_depth;
14649 char *crux;
5e0116d5
RS
14650
14651 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
14652 in REVERSED_RELOC. End the loop with CRUX pointing to the start
14653 of the main expression and with CRUX_DEPTH containing the number
14654 of open brackets at that point. */
14655 reloc_index = -1;
14656 str_depth = 0;
14657 do
fb1b3232 14658 {
09b8f35a
RS
14659 reloc_index++;
14660 crux = str;
14661 crux_depth = str_depth;
14662
14663 /* Skip over whitespace and brackets, keeping count of the number
14664 of brackets. */
14665 while (*str == ' ' || *str == '\t' || *str == '(')
14666 if (*str++ == '(')
14667 str_depth++;
5e0116d5 14668 }
09b8f35a
RS
14669 while (*str == '%'
14670 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14671 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 14672
09b8f35a 14673 my_getExpression (ep, crux);
5e0116d5 14674 str = expr_end;
394f9b3a 14675
5e0116d5 14676 /* Match every open bracket. */
09b8f35a 14677 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 14678 if (*str++ == ')')
09b8f35a 14679 crux_depth--;
394f9b3a 14680
09b8f35a 14681 if (crux_depth > 0)
20203fb9 14682 as_bad (_("unclosed '('"));
394f9b3a 14683
5e0116d5 14684 expr_end = str;
252b5132 14685
01a3f561 14686 if (reloc_index != 0)
64bdfcaf
RS
14687 {
14688 prev_reloc_op_frag = frag_now;
14689 for (i = 0; i < reloc_index; i++)
14690 reloc[i] = reversed_reloc[reloc_index - 1 - i];
14691 }
fb1b3232 14692
5e0116d5 14693 return reloc_index;
252b5132
RH
14694}
14695
14696static void
17a2f251 14697my_getExpression (expressionS *ep, char *str)
252b5132
RH
14698{
14699 char *save_in;
14700
14701 save_in = input_line_pointer;
14702 input_line_pointer = str;
14703 expression (ep);
14704 expr_end = input_line_pointer;
14705 input_line_pointer = save_in;
252b5132
RH
14706}
14707
6d4af3c2 14708const char *
17a2f251 14709md_atof (int type, char *litP, int *sizeP)
252b5132 14710{
499ac353 14711 return ieee_md_atof (type, litP, sizeP, target_big_endian);
252b5132
RH
14712}
14713
14714void
17a2f251 14715md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
14716{
14717 if (target_big_endian)
14718 number_to_chars_bigendian (buf, val, n);
14719 else
14720 number_to_chars_littleendian (buf, val, n);
14721}
14722\f
e013f690
TS
14723static int support_64bit_objects(void)
14724{
14725 const char **list, **l;
aa3d8fdf 14726 int yes;
e013f690
TS
14727
14728 list = bfd_target_list ();
14729 for (l = list; *l != NULL; l++)
aeffff67
RS
14730 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14731 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
e013f690 14732 break;
aa3d8fdf 14733 yes = (*l != NULL);
e013f690 14734 free (list);
aa3d8fdf 14735 return yes;
e013f690
TS
14736}
14737
316f5878
RS
14738/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14739 NEW_VALUE. Warn if another value was already specified. Note:
14740 we have to defer parsing the -march and -mtune arguments in order
14741 to handle 'from-abi' correctly, since the ABI might be specified
14742 in a later argument. */
14743
14744static void
17a2f251 14745mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
14746{
14747 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
1661c76c 14748 as_warn (_("a different %s was already specified, is now %s"),
316f5878
RS
14749 string_ptr == &mips_arch_string ? "-march" : "-mtune",
14750 new_value);
14751
14752 *string_ptr = new_value;
14753}
14754
252b5132 14755int
17b9d67d 14756md_parse_option (int c, const char *arg)
252b5132 14757{
c6278170
RS
14758 unsigned int i;
14759
14760 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
14761 if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
14762 {
919731af 14763 file_ase_explicit |= mips_set_ase (&mips_ases[i], &file_mips_opts,
c6278170
RS
14764 c == mips_ases[i].option_on);
14765 return 1;
14766 }
14767
252b5132
RH
14768 switch (c)
14769 {
119d663a
NC
14770 case OPTION_CONSTRUCT_FLOATS:
14771 mips_disable_float_construction = 0;
14772 break;
bdaaa2e1 14773
119d663a
NC
14774 case OPTION_NO_CONSTRUCT_FLOATS:
14775 mips_disable_float_construction = 1;
14776 break;
bdaaa2e1 14777
252b5132
RH
14778 case OPTION_TRAP:
14779 mips_trap = 1;
14780 break;
14781
14782 case OPTION_BREAK:
14783 mips_trap = 0;
14784 break;
14785
14786 case OPTION_EB:
14787 target_big_endian = 1;
14788 break;
14789
14790 case OPTION_EL:
14791 target_big_endian = 0;
14792 break;
14793
14794 case 'O':
4ffff32f
TS
14795 if (arg == NULL)
14796 mips_optimize = 1;
14797 else if (arg[0] == '0')
14798 mips_optimize = 0;
14799 else if (arg[0] == '1')
252b5132
RH
14800 mips_optimize = 1;
14801 else
14802 mips_optimize = 2;
14803 break;
14804
14805 case 'g':
14806 if (arg == NULL)
14807 mips_debug = 2;
14808 else
14809 mips_debug = atoi (arg);
252b5132
RH
14810 break;
14811
14812 case OPTION_MIPS1:
0b35dfee 14813 file_mips_opts.isa = ISA_MIPS1;
252b5132
RH
14814 break;
14815
14816 case OPTION_MIPS2:
0b35dfee 14817 file_mips_opts.isa = ISA_MIPS2;
252b5132
RH
14818 break;
14819
14820 case OPTION_MIPS3:
0b35dfee 14821 file_mips_opts.isa = ISA_MIPS3;
252b5132
RH
14822 break;
14823
14824 case OPTION_MIPS4:
0b35dfee 14825 file_mips_opts.isa = ISA_MIPS4;
e7af610e
NC
14826 break;
14827
84ea6cf2 14828 case OPTION_MIPS5:
0b35dfee 14829 file_mips_opts.isa = ISA_MIPS5;
84ea6cf2
NC
14830 break;
14831
e7af610e 14832 case OPTION_MIPS32:
0b35dfee 14833 file_mips_opts.isa = ISA_MIPS32;
252b5132
RH
14834 break;
14835
af7ee8bf 14836 case OPTION_MIPS32R2:
0b35dfee 14837 file_mips_opts.isa = ISA_MIPS32R2;
af7ee8bf
CD
14838 break;
14839
ae52f483 14840 case OPTION_MIPS32R3:
0ae19f05 14841 file_mips_opts.isa = ISA_MIPS32R3;
ae52f483
AB
14842 break;
14843
14844 case OPTION_MIPS32R5:
0ae19f05 14845 file_mips_opts.isa = ISA_MIPS32R5;
ae52f483
AB
14846 break;
14847
7361da2c
AB
14848 case OPTION_MIPS32R6:
14849 file_mips_opts.isa = ISA_MIPS32R6;
14850 break;
14851
5f74bc13 14852 case OPTION_MIPS64R2:
0b35dfee 14853 file_mips_opts.isa = ISA_MIPS64R2;
5f74bc13
CD
14854 break;
14855
ae52f483 14856 case OPTION_MIPS64R3:
0ae19f05 14857 file_mips_opts.isa = ISA_MIPS64R3;
ae52f483
AB
14858 break;
14859
14860 case OPTION_MIPS64R5:
0ae19f05 14861 file_mips_opts.isa = ISA_MIPS64R5;
ae52f483
AB
14862 break;
14863
7361da2c
AB
14864 case OPTION_MIPS64R6:
14865 file_mips_opts.isa = ISA_MIPS64R6;
14866 break;
14867
84ea6cf2 14868 case OPTION_MIPS64:
0b35dfee 14869 file_mips_opts.isa = ISA_MIPS64;
84ea6cf2
NC
14870 break;
14871
ec68c924 14872 case OPTION_MTUNE:
316f5878
RS
14873 mips_set_option_string (&mips_tune_string, arg);
14874 break;
ec68c924 14875
316f5878
RS
14876 case OPTION_MARCH:
14877 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
14878 break;
14879
14880 case OPTION_M4650:
316f5878
RS
14881 mips_set_option_string (&mips_arch_string, "4650");
14882 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
14883 break;
14884
14885 case OPTION_NO_M4650:
14886 break;
14887
14888 case OPTION_M4010:
316f5878
RS
14889 mips_set_option_string (&mips_arch_string, "4010");
14890 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
14891 break;
14892
14893 case OPTION_NO_M4010:
14894 break;
14895
14896 case OPTION_M4100:
316f5878
RS
14897 mips_set_option_string (&mips_arch_string, "4100");
14898 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
14899 break;
14900
14901 case OPTION_NO_M4100:
14902 break;
14903
252b5132 14904 case OPTION_M3900:
316f5878
RS
14905 mips_set_option_string (&mips_arch_string, "3900");
14906 mips_set_option_string (&mips_tune_string, "3900");
252b5132 14907 break;
bdaaa2e1 14908
252b5132
RH
14909 case OPTION_NO_M3900:
14910 break;
14911
df58fc94 14912 case OPTION_MICROMIPS:
919731af 14913 if (file_mips_opts.mips16 == 1)
df58fc94
RS
14914 {
14915 as_bad (_("-mmicromips cannot be used with -mips16"));
14916 return 0;
14917 }
919731af 14918 file_mips_opts.micromips = 1;
df58fc94
RS
14919 mips_no_prev_insn ();
14920 break;
14921
14922 case OPTION_NO_MICROMIPS:
919731af 14923 file_mips_opts.micromips = 0;
df58fc94
RS
14924 mips_no_prev_insn ();
14925 break;
14926
252b5132 14927 case OPTION_MIPS16:
919731af 14928 if (file_mips_opts.micromips == 1)
df58fc94
RS
14929 {
14930 as_bad (_("-mips16 cannot be used with -micromips"));
14931 return 0;
14932 }
919731af 14933 file_mips_opts.mips16 = 1;
7d10b47d 14934 mips_no_prev_insn ();
252b5132
RH
14935 break;
14936
14937 case OPTION_NO_MIPS16:
919731af 14938 file_mips_opts.mips16 = 0;
7d10b47d 14939 mips_no_prev_insn ();
252b5132
RH
14940 break;
14941
6a32d874
CM
14942 case OPTION_FIX_24K:
14943 mips_fix_24k = 1;
14944 break;
14945
14946 case OPTION_NO_FIX_24K:
14947 mips_fix_24k = 0;
14948 break;
14949
a8d14a88
CM
14950 case OPTION_FIX_RM7000:
14951 mips_fix_rm7000 = 1;
14952 break;
14953
14954 case OPTION_NO_FIX_RM7000:
14955 mips_fix_rm7000 = 0;
14956 break;
14957
6f2117ba
PH
14958 case OPTION_FIX_LOONGSON3_LLSC:
14959 mips_fix_loongson3_llsc = TRUE;
14960 break;
14961
14962 case OPTION_NO_FIX_LOONGSON3_LLSC:
14963 mips_fix_loongson3_llsc = FALSE;
14964 break;
14965
c67a084a
NC
14966 case OPTION_FIX_LOONGSON2F_JUMP:
14967 mips_fix_loongson2f_jump = TRUE;
14968 break;
14969
14970 case OPTION_NO_FIX_LOONGSON2F_JUMP:
14971 mips_fix_loongson2f_jump = FALSE;
14972 break;
14973
14974 case OPTION_FIX_LOONGSON2F_NOP:
14975 mips_fix_loongson2f_nop = TRUE;
14976 break;
14977
14978 case OPTION_NO_FIX_LOONGSON2F_NOP:
14979 mips_fix_loongson2f_nop = FALSE;
14980 break;
14981
d766e8ec
RS
14982 case OPTION_FIX_VR4120:
14983 mips_fix_vr4120 = 1;
60b63b72
RS
14984 break;
14985
d766e8ec
RS
14986 case OPTION_NO_FIX_VR4120:
14987 mips_fix_vr4120 = 0;
60b63b72
RS
14988 break;
14989
7d8e00cf
RS
14990 case OPTION_FIX_VR4130:
14991 mips_fix_vr4130 = 1;
14992 break;
14993
14994 case OPTION_NO_FIX_VR4130:
14995 mips_fix_vr4130 = 0;
14996 break;
14997
d954098f
DD
14998 case OPTION_FIX_CN63XXP1:
14999 mips_fix_cn63xxp1 = TRUE;
15000 break;
15001
15002 case OPTION_NO_FIX_CN63XXP1:
15003 mips_fix_cn63xxp1 = FALSE;
15004 break;
15005
27c634e0
FN
15006 case OPTION_FIX_R5900:
15007 mips_fix_r5900 = TRUE;
15008 mips_fix_r5900_explicit = TRUE;
15009 break;
15010
15011 case OPTION_NO_FIX_R5900:
15012 mips_fix_r5900 = FALSE;
15013 mips_fix_r5900_explicit = TRUE;
15014 break;
15015
4a6a3df4
AO
15016 case OPTION_RELAX_BRANCH:
15017 mips_relax_branch = 1;
15018 break;
15019
15020 case OPTION_NO_RELAX_BRANCH:
15021 mips_relax_branch = 0;
15022 break;
15023
8b10b0b3
MR
15024 case OPTION_IGNORE_BRANCH_ISA:
15025 mips_ignore_branch_isa = TRUE;
15026 break;
15027
15028 case OPTION_NO_IGNORE_BRANCH_ISA:
15029 mips_ignore_branch_isa = FALSE;
15030 break;
15031
833794fc 15032 case OPTION_INSN32:
919731af 15033 file_mips_opts.insn32 = TRUE;
833794fc
MR
15034 break;
15035
15036 case OPTION_NO_INSN32:
919731af 15037 file_mips_opts.insn32 = FALSE;
833794fc
MR
15038 break;
15039
aa6975fb
ILT
15040 case OPTION_MSHARED:
15041 mips_in_shared = TRUE;
15042 break;
15043
15044 case OPTION_MNO_SHARED:
15045 mips_in_shared = FALSE;
15046 break;
15047
aed1a261 15048 case OPTION_MSYM32:
919731af 15049 file_mips_opts.sym32 = TRUE;
aed1a261
RS
15050 break;
15051
15052 case OPTION_MNO_SYM32:
919731af 15053 file_mips_opts.sym32 = FALSE;
aed1a261
RS
15054 break;
15055
252b5132
RH
15056 /* When generating ELF code, we permit -KPIC and -call_shared to
15057 select SVR4_PIC, and -non_shared to select no PIC. This is
15058 intended to be compatible with Irix 5. */
15059 case OPTION_CALL_SHARED:
252b5132 15060 mips_pic = SVR4_PIC;
143d77c5 15061 mips_abicalls = TRUE;
252b5132
RH
15062 break;
15063
861fb55a 15064 case OPTION_CALL_NONPIC:
861fb55a
DJ
15065 mips_pic = NO_PIC;
15066 mips_abicalls = TRUE;
15067 break;
15068
252b5132 15069 case OPTION_NON_SHARED:
252b5132 15070 mips_pic = NO_PIC;
143d77c5 15071 mips_abicalls = FALSE;
252b5132
RH
15072 break;
15073
44075ae2
TS
15074 /* The -xgot option tells the assembler to use 32 bit offsets
15075 when accessing the got in SVR4_PIC mode. It is for Irix
252b5132
RH
15076 compatibility. */
15077 case OPTION_XGOT:
15078 mips_big_got = 1;
15079 break;
15080
15081 case 'G':
6caf9ef4
TS
15082 g_switch_value = atoi (arg);
15083 g_switch_seen = 1;
252b5132
RH
15084 break;
15085
34ba82a8
TS
15086 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
15087 and -mabi=64. */
252b5132 15088 case OPTION_32:
f3ded42a 15089 mips_abi = O32_ABI;
252b5132
RH
15090 break;
15091
e013f690 15092 case OPTION_N32:
316f5878 15093 mips_abi = N32_ABI;
e013f690 15094 break;
252b5132 15095
e013f690 15096 case OPTION_64:
316f5878 15097 mips_abi = N64_ABI;
f43abd2b 15098 if (!support_64bit_objects())
1661c76c 15099 as_fatal (_("no compiled in support for 64 bit object file format"));
252b5132
RH
15100 break;
15101
c97ef257 15102 case OPTION_GP32:
bad1aba3 15103 file_mips_opts.gp = 32;
c97ef257
AH
15104 break;
15105
15106 case OPTION_GP64:
bad1aba3 15107 file_mips_opts.gp = 64;
c97ef257 15108 break;
252b5132 15109
ca4e0257 15110 case OPTION_FP32:
0b35dfee 15111 file_mips_opts.fp = 32;
316f5878
RS
15112 break;
15113
351cdf24
MF
15114 case OPTION_FPXX:
15115 file_mips_opts.fp = 0;
15116 break;
15117
316f5878 15118 case OPTION_FP64:
0b35dfee 15119 file_mips_opts.fp = 64;
ca4e0257
RS
15120 break;
15121
351cdf24
MF
15122 case OPTION_ODD_SPREG:
15123 file_mips_opts.oddspreg = 1;
15124 break;
15125
15126 case OPTION_NO_ODD_SPREG:
15127 file_mips_opts.oddspreg = 0;
15128 break;
15129
037b32b9 15130 case OPTION_SINGLE_FLOAT:
0b35dfee 15131 file_mips_opts.single_float = 1;
037b32b9
AN
15132 break;
15133
15134 case OPTION_DOUBLE_FLOAT:
0b35dfee 15135 file_mips_opts.single_float = 0;
037b32b9
AN
15136 break;
15137
15138 case OPTION_SOFT_FLOAT:
0b35dfee 15139 file_mips_opts.soft_float = 1;
037b32b9
AN
15140 break;
15141
15142 case OPTION_HARD_FLOAT:
0b35dfee 15143 file_mips_opts.soft_float = 0;
037b32b9
AN
15144 break;
15145
252b5132 15146 case OPTION_MABI:
e013f690 15147 if (strcmp (arg, "32") == 0)
316f5878 15148 mips_abi = O32_ABI;
e013f690 15149 else if (strcmp (arg, "o64") == 0)
316f5878 15150 mips_abi = O64_ABI;
e013f690 15151 else if (strcmp (arg, "n32") == 0)
316f5878 15152 mips_abi = N32_ABI;
e013f690
TS
15153 else if (strcmp (arg, "64") == 0)
15154 {
316f5878 15155 mips_abi = N64_ABI;
e013f690 15156 if (! support_64bit_objects())
1661c76c 15157 as_fatal (_("no compiled in support for 64 bit object file "
e013f690
TS
15158 "format"));
15159 }
15160 else if (strcmp (arg, "eabi") == 0)
316f5878 15161 mips_abi = EABI_ABI;
e013f690 15162 else
da0e507f
TS
15163 {
15164 as_fatal (_("invalid abi -mabi=%s"), arg);
15165 return 0;
15166 }
252b5132
RH
15167 break;
15168
6b76fefe 15169 case OPTION_M7000_HILO_FIX:
b34976b6 15170 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
15171 break;
15172
9ee72ff1 15173 case OPTION_MNO_7000_HILO_FIX:
b34976b6 15174 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
15175 break;
15176
ecb4347a 15177 case OPTION_MDEBUG:
b34976b6 15178 mips_flag_mdebug = TRUE;
ecb4347a
DJ
15179 break;
15180
15181 case OPTION_NO_MDEBUG:
b34976b6 15182 mips_flag_mdebug = FALSE;
ecb4347a 15183 break;
dcd410fe
RO
15184
15185 case OPTION_PDR:
15186 mips_flag_pdr = TRUE;
15187 break;
15188
15189 case OPTION_NO_PDR:
15190 mips_flag_pdr = FALSE;
15191 break;
0a44bf69
RS
15192
15193 case OPTION_MVXWORKS_PIC:
15194 mips_pic = VXWORKS_PIC;
15195 break;
ecb4347a 15196
ba92f887
MR
15197 case OPTION_NAN:
15198 if (strcmp (arg, "2008") == 0)
7361da2c 15199 mips_nan2008 = 1;
ba92f887 15200 else if (strcmp (arg, "legacy") == 0)
7361da2c 15201 mips_nan2008 = 0;
ba92f887
MR
15202 else
15203 {
1661c76c 15204 as_fatal (_("invalid NaN setting -mnan=%s"), arg);
ba92f887
MR
15205 return 0;
15206 }
15207 break;
15208
252b5132
RH
15209 default:
15210 return 0;
15211 }
15212
c67a084a
NC
15213 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
15214
252b5132
RH
15215 return 1;
15216}
316f5878 15217\f
919731af 15218/* Set up globals to tune for the ISA or processor described by INFO. */
252b5132 15219
316f5878 15220static void
17a2f251 15221mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
15222{
15223 if (info != 0)
fef14a42 15224 mips_tune = info->cpu;
316f5878 15225}
80cc45a5 15226
34ba82a8 15227
252b5132 15228void
17a2f251 15229mips_after_parse_args (void)
e9670677 15230{
fef14a42
TS
15231 const struct mips_cpu_info *arch_info = 0;
15232 const struct mips_cpu_info *tune_info = 0;
15233
6f2117ba 15234 /* GP relative stuff not working for PE. */
6caf9ef4 15235 if (strncmp (TARGET_OS, "pe", 2) == 0)
e9670677 15236 {
6caf9ef4 15237 if (g_switch_seen && g_switch_value != 0)
1661c76c 15238 as_bad (_("-G not supported in this configuration"));
e9670677
MR
15239 g_switch_value = 0;
15240 }
15241
cac012d6
AO
15242 if (mips_abi == NO_ABI)
15243 mips_abi = MIPS_DEFAULT_ABI;
15244
919731af 15245 /* The following code determines the architecture.
22923709
RS
15246 Similar code was added to GCC 3.3 (see override_options() in
15247 config/mips/mips.c). The GAS and GCC code should be kept in sync
15248 as much as possible. */
e9670677 15249
316f5878 15250 if (mips_arch_string != 0)
fef14a42 15251 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 15252
0b35dfee 15253 if (file_mips_opts.isa != ISA_UNKNOWN)
e9670677 15254 {
0b35dfee 15255 /* Handle -mipsN. At this point, file_mips_opts.isa contains the
fef14a42 15256 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 15257 the -march selection (if any). */
fef14a42 15258 if (arch_info != 0)
e9670677 15259 {
316f5878
RS
15260 /* -march takes precedence over -mipsN, since it is more descriptive.
15261 There's no harm in specifying both as long as the ISA levels
15262 are the same. */
0b35dfee 15263 if (file_mips_opts.isa != arch_info->isa)
1661c76c
RS
15264 as_bad (_("-%s conflicts with the other architecture options,"
15265 " which imply -%s"),
0b35dfee 15266 mips_cpu_info_from_isa (file_mips_opts.isa)->name,
fef14a42 15267 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 15268 }
316f5878 15269 else
0b35dfee 15270 arch_info = mips_cpu_info_from_isa (file_mips_opts.isa);
e9670677
MR
15271 }
15272
fef14a42 15273 if (arch_info == 0)
95bfe26e
MF
15274 {
15275 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
15276 gas_assert (arch_info);
15277 }
e9670677 15278
fef14a42 15279 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
20203fb9 15280 as_bad (_("-march=%s is not compatible with the selected ABI"),
fef14a42
TS
15281 arch_info->name);
15282
919731af 15283 file_mips_opts.arch = arch_info->cpu;
15284 file_mips_opts.isa = arch_info->isa;
3315614d 15285 file_mips_opts.init_ase = arch_info->ase;
919731af 15286
41cee089
FS
15287 /* The EVA Extension has instructions which are only valid when the R6 ISA
15288 is enabled. This sets the ASE_EVA_R6 flag when both EVA and R6 ISA are
15289 present. */
15290 if (((file_mips_opts.ase & ASE_EVA) != 0) && ISA_IS_R6 (file_mips_opts.isa))
15291 file_mips_opts.ase |= ASE_EVA_R6;
15292
919731af 15293 /* Set up initial mips_opts state. */
15294 mips_opts = file_mips_opts;
15295
27c634e0
FN
15296 /* For the R5900 default to `-mfix-r5900' unless the user told otherwise. */
15297 if (!mips_fix_r5900_explicit)
15298 mips_fix_r5900 = file_mips_opts.arch == CPU_R5900;
15299
919731af 15300 /* The register size inference code is now placed in
15301 file_mips_check_options. */
fef14a42 15302
0b35dfee 15303 /* Optimize for file_mips_opts.arch, unless -mtune selects a different
15304 processor. */
fef14a42
TS
15305 if (mips_tune_string != 0)
15306 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 15307
fef14a42
TS
15308 if (tune_info == 0)
15309 mips_set_tune (arch_info);
15310 else
15311 mips_set_tune (tune_info);
e9670677 15312
ecb4347a 15313 if (mips_flag_mdebug < 0)
e8044f35 15314 mips_flag_mdebug = 0;
e9670677
MR
15315}
15316\f
15317void
17a2f251 15318mips_init_after_args (void)
252b5132 15319{
6f2117ba 15320 /* Initialize opcodes. */
252b5132 15321 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 15322 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
15323}
15324
15325long
17a2f251 15326md_pcrel_from (fixS *fixP)
252b5132 15327{
a7ebbfdf 15328 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
6f2117ba 15329
a7ebbfdf
TS
15330 switch (fixP->fx_r_type)
15331 {
df58fc94
RS
15332 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15333 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15334 /* Return the address of the delay slot. */
15335 return addr + 2;
15336
15337 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15338 case BFD_RELOC_MICROMIPS_JMP:
c9775dde 15339 case BFD_RELOC_MIPS16_16_PCREL_S1:
a7ebbfdf 15340 case BFD_RELOC_16_PCREL_S2:
7361da2c
AB
15341 case BFD_RELOC_MIPS_21_PCREL_S2:
15342 case BFD_RELOC_MIPS_26_PCREL_S2:
a7ebbfdf
TS
15343 case BFD_RELOC_MIPS_JMP:
15344 /* Return the address of the delay slot. */
15345 return addr + 4;
df58fc94 15346
51f6035b
MR
15347 case BFD_RELOC_MIPS_18_PCREL_S3:
15348 /* Return the aligned address of the doubleword containing
15349 the instruction. */
15350 return addr & ~7;
15351
a7ebbfdf
TS
15352 default:
15353 return addr;
15354 }
252b5132
RH
15355}
15356
252b5132
RH
15357/* This is called before the symbol table is processed. In order to
15358 work with gcc when using mips-tfile, we must keep all local labels.
15359 However, in other cases, we want to discard them. If we were
15360 called with -g, but we didn't see any debugging information, it may
15361 mean that gcc is smuggling debugging information through to
15362 mips-tfile, in which case we must generate all local labels. */
15363
15364void
17a2f251 15365mips_frob_file_before_adjust (void)
252b5132
RH
15366{
15367#ifndef NO_ECOFF_DEBUGGING
15368 if (ECOFF_DEBUGGING
15369 && mips_debug != 0
15370 && ! ecoff_debugging_seen)
15371 flag_keep_locals = 1;
15372#endif
15373}
15374
3b91255e 15375/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 15376 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
15377 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
15378 relocation operators.
15379
15380 For our purposes, a %lo() expression matches a %got() or %hi()
15381 expression if:
15382
15383 (a) it refers to the same symbol; and
15384 (b) the offset applied in the %lo() expression is no lower than
15385 the offset applied in the %got() or %hi().
15386
15387 (b) allows us to cope with code like:
15388
15389 lui $4,%hi(foo)
15390 lh $4,%lo(foo+2)($4)
15391
15392 ...which is legal on RELA targets, and has a well-defined behaviour
15393 if the user knows that adding 2 to "foo" will not induce a carry to
15394 the high 16 bits.
15395
15396 When several %lo()s match a particular %got() or %hi(), we use the
15397 following rules to distinguish them:
15398
15399 (1) %lo()s with smaller offsets are a better match than %lo()s with
15400 higher offsets.
15401
15402 (2) %lo()s with no matching %got() or %hi() are better than those
15403 that already have a matching %got() or %hi().
15404
15405 (3) later %lo()s are better than earlier %lo()s.
15406
15407 These rules are applied in order.
15408
15409 (1) means, among other things, that %lo()s with identical offsets are
15410 chosen if they exist.
15411
15412 (2) means that we won't associate several high-part relocations with
15413 the same low-part relocation unless there's no alternative. Having
15414 several high parts for the same low part is a GNU extension; this rule
15415 allows careful users to avoid it.
15416
15417 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
15418 with the last high-part relocation being at the front of the list.
15419 It therefore makes sense to choose the last matching low-part
15420 relocation, all other things being equal. It's also easier
15421 to code that way. */
252b5132
RH
15422
15423void
17a2f251 15424mips_frob_file (void)
252b5132
RH
15425{
15426 struct mips_hi_fixup *l;
35903be0 15427 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
252b5132
RH
15428
15429 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15430 {
15431 segment_info_type *seginfo;
3b91255e
RS
15432 bfd_boolean matched_lo_p;
15433 fixS **hi_pos, **lo_pos, **pos;
252b5132 15434
9c2799c2 15435 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 15436
5919d012 15437 /* If a GOT16 relocation turns out to be against a global symbol,
b886a2ab
RS
15438 there isn't supposed to be a matching LO. Ignore %gots against
15439 constants; we'll report an error for those later. */
738e5348 15440 if (got16_reloc_p (l->fixp->fx_r_type)
b886a2ab 15441 && !(l->fixp->fx_addsy
9e009953 15442 && pic_need_relax (l->fixp->fx_addsy)))
5919d012
RS
15443 continue;
15444
15445 /* Check quickly whether the next fixup happens to be a matching %lo. */
15446 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
15447 continue;
15448
252b5132 15449 seginfo = seg_info (l->seg);
252b5132 15450
3b91255e
RS
15451 /* Set HI_POS to the position of this relocation in the chain.
15452 Set LO_POS to the position of the chosen low-part relocation.
15453 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15454 relocation that matches an immediately-preceding high-part
15455 relocation. */
15456 hi_pos = NULL;
15457 lo_pos = NULL;
15458 matched_lo_p = FALSE;
738e5348 15459 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
35903be0 15460
3b91255e
RS
15461 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15462 {
15463 if (*pos == l->fixp)
15464 hi_pos = pos;
15465
35903be0 15466 if ((*pos)->fx_r_type == looking_for_rtype
30cfc97a 15467 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
3b91255e
RS
15468 && (*pos)->fx_offset >= l->fixp->fx_offset
15469 && (lo_pos == NULL
15470 || (*pos)->fx_offset < (*lo_pos)->fx_offset
15471 || (!matched_lo_p
15472 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15473 lo_pos = pos;
15474
15475 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15476 && fixup_has_matching_lo_p (*pos));
15477 }
15478
15479 /* If we found a match, remove the high-part relocation from its
15480 current position and insert it before the low-part relocation.
15481 Make the offsets match so that fixup_has_matching_lo_p()
15482 will return true.
15483
15484 We don't warn about unmatched high-part relocations since some
15485 versions of gcc have been known to emit dead "lui ...%hi(...)"
15486 instructions. */
15487 if (lo_pos != NULL)
15488 {
15489 l->fixp->fx_offset = (*lo_pos)->fx_offset;
15490 if (l->fixp->fx_next != *lo_pos)
252b5132 15491 {
3b91255e
RS
15492 *hi_pos = l->fixp->fx_next;
15493 l->fixp->fx_next = *lo_pos;
15494 *lo_pos = l->fixp;
252b5132 15495 }
252b5132
RH
15496 }
15497 }
15498}
15499
252b5132 15500int
17a2f251 15501mips_force_relocation (fixS *fixp)
252b5132 15502{
ae6063d4 15503 if (generic_force_reloc (fixp))
252b5132
RH
15504 return 1;
15505
df58fc94
RS
15506 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15507 so that the linker relaxation can update targets. */
15508 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15509 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15510 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15511 return 1;
15512
5caa2b07
MR
15513 /* We want to keep BFD_RELOC_16_PCREL_S2 BFD_RELOC_MIPS_21_PCREL_S2
15514 and BFD_RELOC_MIPS_26_PCREL_S2 relocations against MIPS16 and
15515 microMIPS symbols so that we can do cross-mode branch diagnostics
15516 and BAL to JALX conversion by the linker. */
15517 if ((fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
9d862524
MR
15518 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15519 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2)
15520 && fixp->fx_addsy
15521 && ELF_ST_IS_COMPRESSED (S_GET_OTHER (fixp->fx_addsy)))
15522 return 1;
15523
7361da2c 15524 /* We want all PC-relative relocations to be kept for R6 relaxation. */
912815f0 15525 if (ISA_IS_R6 (file_mips_opts.isa)
7361da2c
AB
15526 && (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
15527 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15528 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
15529 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
15530 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
15531 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
15532 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL))
15533 return 1;
15534
3e722fb5 15535 return 0;
252b5132
RH
15536}
15537
b416ba9b
MR
15538/* Implement TC_FORCE_RELOCATION_ABS. */
15539
15540bfd_boolean
15541mips_force_relocation_abs (fixS *fixp)
15542{
15543 if (generic_force_reloc (fixp))
15544 return TRUE;
15545
15546 /* These relocations do not have enough bits in the in-place addend
15547 to hold an arbitrary absolute section's offset. */
15548 if (HAVE_IN_PLACE_ADDENDS && limited_pcrel_reloc_p (fixp->fx_r_type))
15549 return TRUE;
15550
15551 return FALSE;
15552}
15553
b886a2ab
RS
15554/* Read the instruction associated with RELOC from BUF. */
15555
15556static unsigned int
15557read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15558{
15559 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15560 return read_compressed_insn (buf, 4);
15561 else
15562 return read_insn (buf);
15563}
15564
15565/* Write instruction INSN to BUF, given that it has been relocated
15566 by RELOC. */
15567
15568static void
15569write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15570 unsigned long insn)
15571{
15572 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15573 write_compressed_insn (buf, insn, 4);
15574 else
15575 write_insn (buf, insn);
15576}
15577
9d862524
MR
15578/* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15579 to a symbol in another ISA mode, which cannot be converted to JALX. */
15580
15581static bfd_boolean
15582fix_bad_cross_mode_jump_p (fixS *fixP)
15583{
15584 unsigned long opcode;
15585 int other;
15586 char *buf;
15587
15588 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15589 return FALSE;
15590
15591 other = S_GET_OTHER (fixP->fx_addsy);
15592 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15593 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15594 switch (fixP->fx_r_type)
15595 {
15596 case BFD_RELOC_MIPS_JMP:
15597 return opcode != 0x1d && opcode != 0x03 && ELF_ST_IS_COMPRESSED (other);
15598 case BFD_RELOC_MICROMIPS_JMP:
15599 return opcode != 0x3c && opcode != 0x3d && !ELF_ST_IS_MICROMIPS (other);
15600 default:
15601 return FALSE;
15602 }
15603}
15604
15605/* Return TRUE if the instruction pointed to by FIXP is an invalid JALX
15606 jump to a symbol in the same ISA mode. */
15607
15608static bfd_boolean
15609fix_bad_same_mode_jalx_p (fixS *fixP)
15610{
15611 unsigned long opcode;
15612 int other;
15613 char *buf;
15614
15615 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15616 return FALSE;
15617
15618 other = S_GET_OTHER (fixP->fx_addsy);
15619 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15620 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15621 switch (fixP->fx_r_type)
15622 {
15623 case BFD_RELOC_MIPS_JMP:
15624 return opcode == 0x1d && !ELF_ST_IS_COMPRESSED (other);
15625 case BFD_RELOC_MIPS16_JMP:
15626 return opcode == 0x07 && ELF_ST_IS_COMPRESSED (other);
15627 case BFD_RELOC_MICROMIPS_JMP:
15628 return opcode == 0x3c && ELF_ST_IS_COMPRESSED (other);
15629 default:
15630 return FALSE;
15631 }
15632}
15633
15634/* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15635 to a symbol whose value plus addend is not aligned according to the
15636 ultimate (after linker relaxation) jump instruction's immediate field
15637 requirement, either to (1 << SHIFT), or, for jumps from microMIPS to
15638 regular MIPS code, to (1 << 2). */
15639
15640static bfd_boolean
15641fix_bad_misaligned_jump_p (fixS *fixP, int shift)
15642{
15643 bfd_boolean micro_to_mips_p;
15644 valueT val;
15645 int other;
15646
15647 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15648 return FALSE;
15649
15650 other = S_GET_OTHER (fixP->fx_addsy);
15651 val = S_GET_VALUE (fixP->fx_addsy) | ELF_ST_IS_COMPRESSED (other);
15652 val += fixP->fx_offset;
15653 micro_to_mips_p = (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15654 && !ELF_ST_IS_MICROMIPS (other));
15655 return ((val & ((1 << (micro_to_mips_p ? 2 : shift)) - 1))
15656 != ELF_ST_IS_COMPRESSED (other));
15657}
15658
15659/* Return TRUE if the instruction pointed to by FIXP is an invalid branch
15660 to a symbol whose annotation indicates another ISA mode. For absolute
a6ebf616
MR
15661 symbols check the ISA bit instead.
15662
15663 We accept BFD_RELOC_16_PCREL_S2 relocations against MIPS16 and microMIPS
15664 symbols or BFD_RELOC_MICROMIPS_16_PCREL_S1 relocations against regular
15665 MIPS symbols and associated with BAL instructions as these instructions
de194d85 15666 may be converted to JALX by the linker. */
9d862524
MR
15667
15668static bfd_boolean
15669fix_bad_cross_mode_branch_p (fixS *fixP)
15670{
15671 bfd_boolean absolute_p;
15672 unsigned long opcode;
15673 asection *symsec;
15674 valueT val;
15675 int other;
15676 char *buf;
15677
8b10b0b3
MR
15678 if (mips_ignore_branch_isa)
15679 return FALSE;
15680
9d862524
MR
15681 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15682 return FALSE;
15683
15684 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15685 absolute_p = bfd_is_abs_section (symsec);
15686
15687 val = S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset;
15688 other = S_GET_OTHER (fixP->fx_addsy);
15689
15690 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15691 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 16;
15692 switch (fixP->fx_r_type)
15693 {
15694 case BFD_RELOC_16_PCREL_S2:
a6ebf616
MR
15695 return ((absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other))
15696 && opcode != 0x0411);
15697 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15698 return ((absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other))
15699 && opcode != 0x4060);
9d862524
MR
15700 case BFD_RELOC_MIPS_21_PCREL_S2:
15701 case BFD_RELOC_MIPS_26_PCREL_S2:
15702 return absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other);
15703 case BFD_RELOC_MIPS16_16_PCREL_S1:
15704 return absolute_p ? !(val & 1) : !ELF_ST_IS_MIPS16 (other);
15705 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15706 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
9d862524
MR
15707 return absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other);
15708 default:
15709 abort ();
15710 }
15711}
15712
15713/* Return TRUE if the symbol plus addend associated with a regular MIPS
15714 branch instruction pointed to by FIXP is not aligned according to the
15715 branch instruction's immediate field requirement. We need the addend
15716 to preserve the ISA bit and also the sum must not have bit 2 set. We
15717 must explicitly OR in the ISA bit from symbol annotation as the bit
15718 won't be set in the symbol's value then. */
15719
15720static bfd_boolean
15721fix_bad_misaligned_branch_p (fixS *fixP)
15722{
15723 bfd_boolean absolute_p;
15724 asection *symsec;
15725 valueT isa_bit;
15726 valueT val;
15727 valueT off;
15728 int other;
15729
15730 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15731 return FALSE;
15732
15733 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15734 absolute_p = bfd_is_abs_section (symsec);
15735
15736 val = S_GET_VALUE (fixP->fx_addsy);
15737 other = S_GET_OTHER (fixP->fx_addsy);
15738 off = fixP->fx_offset;
15739
15740 isa_bit = absolute_p ? (val + off) & 1 : ELF_ST_IS_COMPRESSED (other);
15741 val |= ELF_ST_IS_COMPRESSED (other);
15742 val += off;
15743 return (val & 0x3) != isa_bit;
15744}
15745
52031738
FS
15746/* Calculate the relocation target by masking off ISA mode bit before
15747 combining symbol and addend. */
15748
15749static valueT
15750fix_bad_misaligned_address (fixS *fixP)
15751{
15752 valueT val;
15753 valueT off;
15754 unsigned isa_mode;
15755 gas_assert (fixP != NULL && fixP->fx_addsy != NULL);
15756 val = S_GET_VALUE (fixP->fx_addsy);
15757 off = fixP->fx_offset;
15758 isa_mode = (ELF_ST_IS_COMPRESSED (S_GET_OTHER (fixP->fx_addsy))
15759 ? 1 : 0);
15760
15761 return ((val & ~isa_mode) + off);
15762}
15763
9d862524
MR
15764/* Make the necessary checks on a regular MIPS branch pointed to by FIXP
15765 and its calculated value VAL. */
15766
15767static void
15768fix_validate_branch (fixS *fixP, valueT val)
15769{
15770 if (fixP->fx_done && (val & 0x3) != 0)
15771 as_bad_where (fixP->fx_file, fixP->fx_line,
15772 _("branch to misaligned address (0x%lx)"),
15773 (long) (val + md_pcrel_from (fixP)));
15774 else if (fix_bad_cross_mode_branch_p (fixP))
15775 as_bad_where (fixP->fx_file, fixP->fx_line,
15776 _("branch to a symbol in another ISA mode"));
15777 else if (fix_bad_misaligned_branch_p (fixP))
15778 as_bad_where (fixP->fx_file, fixP->fx_line,
15779 _("branch to misaligned address (0x%lx)"),
52031738 15780 (long) fix_bad_misaligned_address (fixP));
9d862524
MR
15781 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x3) != 0)
15782 as_bad_where (fixP->fx_file, fixP->fx_line,
15783 _("cannot encode misaligned addend "
15784 "in the relocatable field (0x%lx)"),
15785 (long) fixP->fx_offset);
15786}
15787
252b5132
RH
15788/* Apply a fixup to the object file. */
15789
94f592af 15790void
55cf6793 15791md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 15792{
4d68580a 15793 char *buf;
b886a2ab 15794 unsigned long insn;
a7ebbfdf 15795 reloc_howto_type *howto;
252b5132 15796
d56a8dda
RS
15797 if (fixP->fx_pcrel)
15798 switch (fixP->fx_r_type)
15799 {
15800 case BFD_RELOC_16_PCREL_S2:
c9775dde 15801 case BFD_RELOC_MIPS16_16_PCREL_S1:
d56a8dda
RS
15802 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15803 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15804 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15805 case BFD_RELOC_32_PCREL:
7361da2c
AB
15806 case BFD_RELOC_MIPS_21_PCREL_S2:
15807 case BFD_RELOC_MIPS_26_PCREL_S2:
15808 case BFD_RELOC_MIPS_18_PCREL_S3:
15809 case BFD_RELOC_MIPS_19_PCREL_S2:
15810 case BFD_RELOC_HI16_S_PCREL:
15811 case BFD_RELOC_LO16_PCREL:
d56a8dda
RS
15812 break;
15813
15814 case BFD_RELOC_32:
15815 fixP->fx_r_type = BFD_RELOC_32_PCREL;
15816 break;
15817
15818 default:
15819 as_bad_where (fixP->fx_file, fixP->fx_line,
15820 _("PC-relative reference to a different section"));
15821 break;
15822 }
15823
15824 /* Handle BFD_RELOC_8, since it's easy. Punt on other bfd relocations
15825 that have no MIPS ELF equivalent. */
15826 if (fixP->fx_r_type != BFD_RELOC_8)
15827 {
15828 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15829 if (!howto)
15830 return;
15831 }
65551fa4 15832
df58fc94
RS
15833 gas_assert (fixP->fx_size == 2
15834 || fixP->fx_size == 4
d56a8dda 15835 || fixP->fx_r_type == BFD_RELOC_8
90ecf173
MR
15836 || fixP->fx_r_type == BFD_RELOC_16
15837 || fixP->fx_r_type == BFD_RELOC_64
15838 || fixP->fx_r_type == BFD_RELOC_CTOR
15839 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
df58fc94 15840 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
90ecf173
MR
15841 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15842 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
2f0c68f2
CM
15843 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64
15844 || fixP->fx_r_type == BFD_RELOC_NONE);
252b5132 15845
4d68580a 15846 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132 15847
b1dca8ee
RS
15848 /* Don't treat parts of a composite relocation as done. There are two
15849 reasons for this:
15850
15851 (1) The second and third parts will be against 0 (RSS_UNDEF) but
15852 should nevertheless be emitted if the first part is.
15853
15854 (2) In normal usage, composite relocations are never assembly-time
15855 constants. The easiest way of dealing with the pathological
15856 exceptions is to generate a relocation against STN_UNDEF and
15857 leave everything up to the linker. */
3994f87e 15858 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
252b5132
RH
15859 fixP->fx_done = 1;
15860
15861 switch (fixP->fx_r_type)
15862 {
3f98094e
DJ
15863 case BFD_RELOC_MIPS_TLS_GD:
15864 case BFD_RELOC_MIPS_TLS_LDM:
741d6ea8
JM
15865 case BFD_RELOC_MIPS_TLS_DTPREL32:
15866 case BFD_RELOC_MIPS_TLS_DTPREL64:
3f98094e
DJ
15867 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15868 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15869 case BFD_RELOC_MIPS_TLS_GOTTPREL:
d0f13682
CLT
15870 case BFD_RELOC_MIPS_TLS_TPREL32:
15871 case BFD_RELOC_MIPS_TLS_TPREL64:
3f98094e
DJ
15872 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15873 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
df58fc94
RS
15874 case BFD_RELOC_MICROMIPS_TLS_GD:
15875 case BFD_RELOC_MICROMIPS_TLS_LDM:
15876 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15877 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15878 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15879 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15880 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
d0f13682
CLT
15881 case BFD_RELOC_MIPS16_TLS_GD:
15882 case BFD_RELOC_MIPS16_TLS_LDM:
15883 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15884 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15885 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15886 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15887 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
4512dafa
MR
15888 if (fixP->fx_addsy)
15889 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15890 else
15891 as_bad_where (fixP->fx_file, fixP->fx_line,
15892 _("TLS relocation against a constant"));
15893 break;
3f98094e 15894
252b5132 15895 case BFD_RELOC_MIPS_JMP:
9d862524
MR
15896 case BFD_RELOC_MIPS16_JMP:
15897 case BFD_RELOC_MICROMIPS_JMP:
15898 {
15899 int shift;
15900
15901 gas_assert (!fixP->fx_done);
15902
15903 /* Shift is 2, unusually, for microMIPS JALX. */
15904 if (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15905 && (read_compressed_insn (buf, 4) >> 26) != 0x3c)
15906 shift = 1;
15907 else
15908 shift = 2;
15909
15910 if (fix_bad_cross_mode_jump_p (fixP))
15911 as_bad_where (fixP->fx_file, fixP->fx_line,
15912 _("jump to a symbol in another ISA mode"));
15913 else if (fix_bad_same_mode_jalx_p (fixP))
15914 as_bad_where (fixP->fx_file, fixP->fx_line,
15915 _("JALX to a symbol in the same ISA mode"));
15916 else if (fix_bad_misaligned_jump_p (fixP, shift))
15917 as_bad_where (fixP->fx_file, fixP->fx_line,
15918 _("jump to misaligned address (0x%lx)"),
52031738 15919 (long) fix_bad_misaligned_address (fixP));
9d862524
MR
15920 else if (HAVE_IN_PLACE_ADDENDS
15921 && (fixP->fx_offset & ((1 << shift) - 1)) != 0)
15922 as_bad_where (fixP->fx_file, fixP->fx_line,
15923 _("cannot encode misaligned addend "
15924 "in the relocatable field (0x%lx)"),
15925 (long) fixP->fx_offset);
15926 }
15927 /* Fall through. */
15928
e369bcce
TS
15929 case BFD_RELOC_MIPS_SHIFT5:
15930 case BFD_RELOC_MIPS_SHIFT6:
15931 case BFD_RELOC_MIPS_GOT_DISP:
15932 case BFD_RELOC_MIPS_GOT_PAGE:
15933 case BFD_RELOC_MIPS_GOT_OFST:
15934 case BFD_RELOC_MIPS_SUB:
15935 case BFD_RELOC_MIPS_INSERT_A:
15936 case BFD_RELOC_MIPS_INSERT_B:
15937 case BFD_RELOC_MIPS_DELETE:
15938 case BFD_RELOC_MIPS_HIGHEST:
15939 case BFD_RELOC_MIPS_HIGHER:
15940 case BFD_RELOC_MIPS_SCN_DISP:
15941 case BFD_RELOC_MIPS_REL16:
15942 case BFD_RELOC_MIPS_RELGOT:
15943 case BFD_RELOC_MIPS_JALR:
252b5132
RH
15944 case BFD_RELOC_HI16:
15945 case BFD_RELOC_HI16_S:
b886a2ab 15946 case BFD_RELOC_LO16:
cdf6fd85 15947 case BFD_RELOC_GPREL16:
252b5132
RH
15948 case BFD_RELOC_MIPS_LITERAL:
15949 case BFD_RELOC_MIPS_CALL16:
15950 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 15951 case BFD_RELOC_GPREL32:
252b5132
RH
15952 case BFD_RELOC_MIPS_GOT_HI16:
15953 case BFD_RELOC_MIPS_GOT_LO16:
15954 case BFD_RELOC_MIPS_CALL_HI16:
15955 case BFD_RELOC_MIPS_CALL_LO16:
41947d9e
MR
15956 case BFD_RELOC_HI16_S_PCREL:
15957 case BFD_RELOC_LO16_PCREL:
252b5132 15958 case BFD_RELOC_MIPS16_GPREL:
738e5348
RS
15959 case BFD_RELOC_MIPS16_GOT16:
15960 case BFD_RELOC_MIPS16_CALL16:
d6f16593
MR
15961 case BFD_RELOC_MIPS16_HI16:
15962 case BFD_RELOC_MIPS16_HI16_S:
b886a2ab 15963 case BFD_RELOC_MIPS16_LO16:
df58fc94
RS
15964 case BFD_RELOC_MICROMIPS_GOT_DISP:
15965 case BFD_RELOC_MICROMIPS_GOT_PAGE:
15966 case BFD_RELOC_MICROMIPS_GOT_OFST:
15967 case BFD_RELOC_MICROMIPS_SUB:
15968 case BFD_RELOC_MICROMIPS_HIGHEST:
15969 case BFD_RELOC_MICROMIPS_HIGHER:
15970 case BFD_RELOC_MICROMIPS_SCN_DISP:
15971 case BFD_RELOC_MICROMIPS_JALR:
15972 case BFD_RELOC_MICROMIPS_HI16:
15973 case BFD_RELOC_MICROMIPS_HI16_S:
b886a2ab 15974 case BFD_RELOC_MICROMIPS_LO16:
df58fc94
RS
15975 case BFD_RELOC_MICROMIPS_GPREL16:
15976 case BFD_RELOC_MICROMIPS_LITERAL:
15977 case BFD_RELOC_MICROMIPS_CALL16:
15978 case BFD_RELOC_MICROMIPS_GOT16:
15979 case BFD_RELOC_MICROMIPS_GOT_HI16:
15980 case BFD_RELOC_MICROMIPS_GOT_LO16:
15981 case BFD_RELOC_MICROMIPS_CALL_HI16:
15982 case BFD_RELOC_MICROMIPS_CALL_LO16:
067ec077 15983 case BFD_RELOC_MIPS_EH:
b886a2ab
RS
15984 if (fixP->fx_done)
15985 {
15986 offsetT value;
15987
15988 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
15989 {
15990 insn = read_reloc_insn (buf, fixP->fx_r_type);
15991 if (mips16_reloc_p (fixP->fx_r_type))
15992 insn |= mips16_immed_extend (value, 16);
15993 else
15994 insn |= (value & 0xffff);
15995 write_reloc_insn (buf, fixP->fx_r_type, insn);
15996 }
15997 else
15998 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 15999 _("unsupported constant in relocation"));
b886a2ab 16000 }
252b5132
RH
16001 break;
16002
252b5132
RH
16003 case BFD_RELOC_64:
16004 /* This is handled like BFD_RELOC_32, but we output a sign
16005 extended value if we are only 32 bits. */
3e722fb5 16006 if (fixP->fx_done)
252b5132
RH
16007 {
16008 if (8 <= sizeof (valueT))
4d68580a 16009 md_number_to_chars (buf, *valP, 8);
252b5132
RH
16010 else
16011 {
a7ebbfdf 16012 valueT hiv;
252b5132 16013
a7ebbfdf 16014 if ((*valP & 0x80000000) != 0)
252b5132
RH
16015 hiv = 0xffffffff;
16016 else
16017 hiv = 0;
4d68580a
RS
16018 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
16019 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
252b5132
RH
16020 }
16021 }
16022 break;
16023
056350c6 16024 case BFD_RELOC_RVA:
252b5132 16025 case BFD_RELOC_32:
b47468a6 16026 case BFD_RELOC_32_PCREL:
252b5132 16027 case BFD_RELOC_16:
d56a8dda 16028 case BFD_RELOC_8:
252b5132 16029 /* If we are deleting this reloc entry, we must fill in the
54f4ddb3
TS
16030 value now. This can happen if we have a .word which is not
16031 resolved when it appears but is later defined. */
252b5132 16032 if (fixP->fx_done)
4d68580a 16033 md_number_to_chars (buf, *valP, fixP->fx_size);
252b5132
RH
16034 break;
16035
7361da2c 16036 case BFD_RELOC_MIPS_21_PCREL_S2:
9d862524 16037 fix_validate_branch (fixP, *valP);
41947d9e
MR
16038 if (!fixP->fx_done)
16039 break;
16040
16041 if (*valP + 0x400000 <= 0x7fffff)
16042 {
16043 insn = read_insn (buf);
16044 insn |= (*valP >> 2) & 0x1fffff;
16045 write_insn (buf, insn);
16046 }
16047 else
16048 as_bad_where (fixP->fx_file, fixP->fx_line,
16049 _("branch out of range"));
16050 break;
16051
7361da2c 16052 case BFD_RELOC_MIPS_26_PCREL_S2:
9d862524 16053 fix_validate_branch (fixP, *valP);
41947d9e
MR
16054 if (!fixP->fx_done)
16055 break;
7361da2c 16056
41947d9e
MR
16057 if (*valP + 0x8000000 <= 0xfffffff)
16058 {
16059 insn = read_insn (buf);
16060 insn |= (*valP >> 2) & 0x3ffffff;
16061 write_insn (buf, insn);
16062 }
16063 else
16064 as_bad_where (fixP->fx_file, fixP->fx_line,
16065 _("branch out of range"));
7361da2c
AB
16066 break;
16067
16068 case BFD_RELOC_MIPS_18_PCREL_S3:
717ba204 16069 if (fixP->fx_addsy && (S_GET_VALUE (fixP->fx_addsy) & 0x7) != 0)
7361da2c 16070 as_bad_where (fixP->fx_file, fixP->fx_line,
0866e94c
MF
16071 _("PC-relative access using misaligned symbol (%lx)"),
16072 (long) S_GET_VALUE (fixP->fx_addsy));
16073 if ((fixP->fx_offset & 0x7) != 0)
16074 as_bad_where (fixP->fx_file, fixP->fx_line,
16075 _("PC-relative access using misaligned offset (%lx)"),
16076 (long) fixP->fx_offset);
41947d9e
MR
16077 if (!fixP->fx_done)
16078 break;
7361da2c 16079
41947d9e
MR
16080 if (*valP + 0x100000 <= 0x1fffff)
16081 {
16082 insn = read_insn (buf);
16083 insn |= (*valP >> 3) & 0x3ffff;
16084 write_insn (buf, insn);
16085 }
16086 else
16087 as_bad_where (fixP->fx_file, fixP->fx_line,
16088 _("PC-relative access out of range"));
7361da2c
AB
16089 break;
16090
16091 case BFD_RELOC_MIPS_19_PCREL_S2:
16092 if ((*valP & 0x3) != 0)
16093 as_bad_where (fixP->fx_file, fixP->fx_line,
16094 _("PC-relative access to misaligned address (%lx)"),
717ba204 16095 (long) *valP);
41947d9e
MR
16096 if (!fixP->fx_done)
16097 break;
7361da2c 16098
41947d9e
MR
16099 if (*valP + 0x100000 <= 0x1fffff)
16100 {
16101 insn = read_insn (buf);
16102 insn |= (*valP >> 2) & 0x7ffff;
16103 write_insn (buf, insn);
16104 }
16105 else
16106 as_bad_where (fixP->fx_file, fixP->fx_line,
16107 _("PC-relative access out of range"));
7361da2c
AB
16108 break;
16109
252b5132 16110 case BFD_RELOC_16_PCREL_S2:
9d862524 16111 fix_validate_branch (fixP, *valP);
cb56d3d3 16112
54f4ddb3
TS
16113 /* We need to save the bits in the instruction since fixup_segment()
16114 might be deleting the relocation entry (i.e., a branch within
16115 the current segment). */
a7ebbfdf 16116 if (! fixP->fx_done)
bb2d6cd7 16117 break;
252b5132 16118
54f4ddb3 16119 /* Update old instruction data. */
4d68580a 16120 insn = read_insn (buf);
252b5132 16121
a7ebbfdf
TS
16122 if (*valP + 0x20000 <= 0x3ffff)
16123 {
16124 insn |= (*valP >> 2) & 0xffff;
4d68580a 16125 write_insn (buf, insn);
a7ebbfdf 16126 }
ce8ad872 16127 else if (fixP->fx_tcbit2
a7ebbfdf
TS
16128 && fixP->fx_done
16129 && fixP->fx_frag->fr_address >= text_section->vma
16130 && (fixP->fx_frag->fr_address
587aac4e 16131 < text_section->vma + bfd_get_section_size (text_section))
a7ebbfdf
TS
16132 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
16133 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
16134 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
16135 {
16136 /* The branch offset is too large. If this is an
16137 unconditional branch, and we are not generating PIC code,
16138 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
16139 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
16140 insn = 0x0c000000; /* jal */
252b5132 16141 else
a7ebbfdf
TS
16142 insn = 0x08000000; /* j */
16143 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
16144 fixP->fx_done = 0;
16145 fixP->fx_addsy = section_symbol (text_section);
16146 *valP += md_pcrel_from (fixP);
4d68580a 16147 write_insn (buf, insn);
a7ebbfdf
TS
16148 }
16149 else
16150 {
16151 /* If we got here, we have branch-relaxation disabled,
16152 and there's nothing we can do to fix this instruction
16153 without turning it into a longer sequence. */
16154 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 16155 _("branch out of range"));
252b5132 16156 }
252b5132
RH
16157 break;
16158
c9775dde 16159 case BFD_RELOC_MIPS16_16_PCREL_S1:
df58fc94
RS
16160 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
16161 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
16162 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
96e9ba5f 16163 gas_assert (!fixP->fx_done);
9d862524
MR
16164 if (fix_bad_cross_mode_branch_p (fixP))
16165 as_bad_where (fixP->fx_file, fixP->fx_line,
16166 _("branch to a symbol in another ISA mode"));
16167 else if (fixP->fx_addsy
16168 && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
16169 && !bfd_is_abs_section (S_GET_SEGMENT (fixP->fx_addsy))
16170 && (fixP->fx_offset & 0x1) != 0)
16171 as_bad_where (fixP->fx_file, fixP->fx_line,
16172 _("branch to misaligned address (0x%lx)"),
52031738 16173 (long) fix_bad_misaligned_address (fixP));
9d862524
MR
16174 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x1) != 0)
16175 as_bad_where (fixP->fx_file, fixP->fx_line,
16176 _("cannot encode misaligned addend "
16177 "in the relocatable field (0x%lx)"),
16178 (long) fixP->fx_offset);
df58fc94
RS
16179 break;
16180
252b5132
RH
16181 case BFD_RELOC_VTABLE_INHERIT:
16182 fixP->fx_done = 0;
16183 if (fixP->fx_addsy
16184 && !S_IS_DEFINED (fixP->fx_addsy)
16185 && !S_IS_WEAK (fixP->fx_addsy))
16186 S_SET_WEAK (fixP->fx_addsy);
16187 break;
16188
2f0c68f2 16189 case BFD_RELOC_NONE:
252b5132
RH
16190 case BFD_RELOC_VTABLE_ENTRY:
16191 fixP->fx_done = 0;
16192 break;
16193
16194 default:
b37df7c4 16195 abort ();
252b5132 16196 }
a7ebbfdf
TS
16197
16198 /* Remember value for tc_gen_reloc. */
16199 fixP->fx_addnumber = *valP;
252b5132
RH
16200}
16201
252b5132 16202static symbolS *
17a2f251 16203get_symbol (void)
252b5132
RH
16204{
16205 int c;
16206 char *name;
16207 symbolS *p;
16208
d02603dc 16209 c = get_symbol_name (&name);
252b5132 16210 p = (symbolS *) symbol_find_or_make (name);
d02603dc 16211 (void) restore_line_pointer (c);
252b5132
RH
16212 return p;
16213}
16214
742a56fe
RS
16215/* Align the current frag to a given power of two. If a particular
16216 fill byte should be used, FILL points to an integer that contains
16217 that byte, otherwise FILL is null.
16218
462427c4
RS
16219 This function used to have the comment:
16220
16221 The MIPS assembler also automatically adjusts any preceding label.
16222
16223 The implementation therefore applied the adjustment to a maximum of
16224 one label. However, other label adjustments are applied to batches
16225 of labels, and adjusting just one caused problems when new labels
16226 were added for the sake of debugging or unwind information.
16227 We therefore adjust all preceding labels (given as LABELS) instead. */
252b5132
RH
16228
16229static void
462427c4 16230mips_align (int to, int *fill, struct insn_label_list *labels)
252b5132 16231{
7d10b47d 16232 mips_emit_delays ();
df58fc94 16233 mips_record_compressed_mode ();
742a56fe
RS
16234 if (fill == NULL && subseg_text_p (now_seg))
16235 frag_align_code (to, 0);
16236 else
16237 frag_align (to, fill ? *fill : 0, 0);
252b5132 16238 record_alignment (now_seg, to);
770c0151 16239 mips_move_labels (labels, subseg_text_p (now_seg));
252b5132
RH
16240}
16241
16242/* Align to a given power of two. .align 0 turns off the automatic
16243 alignment used by the data creating pseudo-ops. */
16244
16245static void
17a2f251 16246s_align (int x ATTRIBUTE_UNUSED)
252b5132 16247{
742a56fe 16248 int temp, fill_value, *fill_ptr;
49954fb4 16249 long max_alignment = 28;
252b5132 16250
54f4ddb3 16251 /* o Note that the assembler pulls down any immediately preceding label
252b5132 16252 to the aligned address.
54f4ddb3 16253 o It's not documented but auto alignment is reinstated by
252b5132 16254 a .align pseudo instruction.
54f4ddb3 16255 o Note also that after auto alignment is turned off the mips assembler
252b5132 16256 issues an error on attempt to assemble an improperly aligned data item.
54f4ddb3 16257 We don't. */
252b5132
RH
16258
16259 temp = get_absolute_expression ();
16260 if (temp > max_alignment)
1661c76c 16261 as_bad (_("alignment too large, %d assumed"), temp = max_alignment);
252b5132
RH
16262 else if (temp < 0)
16263 {
1661c76c 16264 as_warn (_("alignment negative, 0 assumed"));
252b5132
RH
16265 temp = 0;
16266 }
16267 if (*input_line_pointer == ',')
16268 {
f9419b05 16269 ++input_line_pointer;
742a56fe
RS
16270 fill_value = get_absolute_expression ();
16271 fill_ptr = &fill_value;
252b5132
RH
16272 }
16273 else
742a56fe 16274 fill_ptr = 0;
252b5132
RH
16275 if (temp)
16276 {
a8dbcb85
TS
16277 segment_info_type *si = seg_info (now_seg);
16278 struct insn_label_list *l = si->label_list;
54f4ddb3 16279 /* Auto alignment should be switched on by next section change. */
252b5132 16280 auto_align = 1;
462427c4 16281 mips_align (temp, fill_ptr, l);
252b5132
RH
16282 }
16283 else
16284 {
16285 auto_align = 0;
16286 }
16287
16288 demand_empty_rest_of_line ();
16289}
16290
252b5132 16291static void
17a2f251 16292s_change_sec (int sec)
252b5132
RH
16293{
16294 segT seg;
16295
252b5132
RH
16296 /* The ELF backend needs to know that we are changing sections, so
16297 that .previous works correctly. We could do something like check
b6ff326e 16298 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
16299 as it would not be appropriate to use it in the section changing
16300 functions in read.c, since obj-elf.c intercepts those. FIXME:
16301 This should be cleaner, somehow. */
f3ded42a 16302 obj_elf_section_change_hook ();
252b5132 16303
7d10b47d 16304 mips_emit_delays ();
6a32d874 16305
252b5132
RH
16306 switch (sec)
16307 {
16308 case 't':
16309 s_text (0);
16310 break;
16311 case 'd':
16312 s_data (0);
16313 break;
16314 case 'b':
16315 subseg_set (bss_section, (subsegT) get_absolute_expression ());
16316 demand_empty_rest_of_line ();
16317 break;
16318
16319 case 'r':
4d0d148d
TS
16320 seg = subseg_new (RDATA_SECTION_NAME,
16321 (subsegT) get_absolute_expression ());
f3ded42a
RS
16322 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
16323 | SEC_READONLY | SEC_RELOC
16324 | SEC_DATA));
16325 if (strncmp (TARGET_OS, "elf", 3) != 0)
16326 record_alignment (seg, 4);
4d0d148d 16327 demand_empty_rest_of_line ();
252b5132
RH
16328 break;
16329
16330 case 's':
4d0d148d 16331 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
f3ded42a
RS
16332 bfd_set_section_flags (stdoutput, seg,
16333 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
16334 if (strncmp (TARGET_OS, "elf", 3) != 0)
16335 record_alignment (seg, 4);
4d0d148d
TS
16336 demand_empty_rest_of_line ();
16337 break;
998b3c36
MR
16338
16339 case 'B':
16340 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
f3ded42a
RS
16341 bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
16342 if (strncmp (TARGET_OS, "elf", 3) != 0)
16343 record_alignment (seg, 4);
998b3c36
MR
16344 demand_empty_rest_of_line ();
16345 break;
252b5132
RH
16346 }
16347
16348 auto_align = 1;
16349}
b34976b6 16350
cca86cc8 16351void
17a2f251 16352s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 16353{
d02603dc 16354 char *saved_ilp;
cca86cc8 16355 char *section_name;
d02603dc 16356 char c, endc;
684022ea 16357 char next_c = 0;
cca86cc8
SC
16358 int section_type;
16359 int section_flag;
16360 int section_entry_size;
16361 int section_alignment;
b34976b6 16362
d02603dc
NC
16363 saved_ilp = input_line_pointer;
16364 endc = get_symbol_name (&section_name);
16365 c = (endc == '"' ? input_line_pointer[1] : endc);
a816d1ed 16366 if (c)
d02603dc 16367 next_c = input_line_pointer [(endc == '"' ? 2 : 1)];
cca86cc8 16368
4cf0dd0d
TS
16369 /* Do we have .section Name<,"flags">? */
16370 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 16371 {
d02603dc
NC
16372 /* Just after name is now '\0'. */
16373 (void) restore_line_pointer (endc);
16374 input_line_pointer = saved_ilp;
cca86cc8
SC
16375 obj_elf_section (ignore);
16376 return;
16377 }
d02603dc
NC
16378
16379 section_name = xstrdup (section_name);
16380 c = restore_line_pointer (endc);
16381
cca86cc8
SC
16382 input_line_pointer++;
16383
16384 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
16385 if (c == ',')
16386 section_type = get_absolute_expression ();
16387 else
16388 section_type = 0;
d02603dc 16389
cca86cc8
SC
16390 if (*input_line_pointer++ == ',')
16391 section_flag = get_absolute_expression ();
16392 else
16393 section_flag = 0;
d02603dc 16394
cca86cc8
SC
16395 if (*input_line_pointer++ == ',')
16396 section_entry_size = get_absolute_expression ();
16397 else
16398 section_entry_size = 0;
d02603dc 16399
cca86cc8
SC
16400 if (*input_line_pointer++ == ',')
16401 section_alignment = get_absolute_expression ();
16402 else
16403 section_alignment = 0;
d02603dc 16404
87975d2a
AM
16405 /* FIXME: really ignore? */
16406 (void) section_alignment;
cca86cc8 16407
8ab8a5c8
RS
16408 /* When using the generic form of .section (as implemented by obj-elf.c),
16409 there's no way to set the section type to SHT_MIPS_DWARF. Users have
16410 traditionally had to fall back on the more common @progbits instead.
16411
16412 There's nothing really harmful in this, since bfd will correct
16413 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
708587a4 16414 means that, for backwards compatibility, the special_section entries
8ab8a5c8
RS
16415 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
16416
16417 Even so, we shouldn't force users of the MIPS .section syntax to
16418 incorrectly label the sections as SHT_PROGBITS. The best compromise
16419 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
16420 generic type-checking code. */
16421 if (section_type == SHT_MIPS_DWARF)
16422 section_type = SHT_PROGBITS;
16423
a91e1603 16424 obj_elf_change_section (section_name, section_type, 0, section_flag,
cca86cc8 16425 section_entry_size, 0, 0, 0);
a816d1ed
AO
16426
16427 if (now_seg->name != section_name)
16428 free (section_name);
cca86cc8 16429}
252b5132
RH
16430
16431void
17a2f251 16432mips_enable_auto_align (void)
252b5132
RH
16433{
16434 auto_align = 1;
16435}
16436
16437static void
17a2f251 16438s_cons (int log_size)
252b5132 16439{
a8dbcb85
TS
16440 segment_info_type *si = seg_info (now_seg);
16441 struct insn_label_list *l = si->label_list;
252b5132 16442
7d10b47d 16443 mips_emit_delays ();
252b5132 16444 if (log_size > 0 && auto_align)
462427c4 16445 mips_align (log_size, 0, l);
252b5132 16446 cons (1 << log_size);
a1facbec 16447 mips_clear_insn_labels ();
252b5132
RH
16448}
16449
16450static void
17a2f251 16451s_float_cons (int type)
252b5132 16452{
a8dbcb85
TS
16453 segment_info_type *si = seg_info (now_seg);
16454 struct insn_label_list *l = si->label_list;
252b5132 16455
7d10b47d 16456 mips_emit_delays ();
252b5132
RH
16457
16458 if (auto_align)
49309057
ILT
16459 {
16460 if (type == 'd')
462427c4 16461 mips_align (3, 0, l);
49309057 16462 else
462427c4 16463 mips_align (2, 0, l);
49309057 16464 }
252b5132 16465
252b5132 16466 float_cons (type);
a1facbec 16467 mips_clear_insn_labels ();
252b5132
RH
16468}
16469
16470/* Handle .globl. We need to override it because on Irix 5 you are
16471 permitted to say
16472 .globl foo .text
16473 where foo is an undefined symbol, to mean that foo should be
16474 considered to be the address of a function. */
16475
16476static void
17a2f251 16477s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
16478{
16479 char *name;
16480 int c;
16481 symbolS *symbolP;
252b5132 16482
8a06b769 16483 do
252b5132 16484 {
d02603dc 16485 c = get_symbol_name (&name);
8a06b769
TS
16486 symbolP = symbol_find_or_make (name);
16487 S_SET_EXTERNAL (symbolP);
16488
252b5132 16489 *input_line_pointer = c;
d02603dc 16490 SKIP_WHITESPACE_AFTER_NAME ();
252b5132 16491
8a06b769
TS
16492 if (!is_end_of_line[(unsigned char) *input_line_pointer]
16493 && (*input_line_pointer != ','))
16494 {
16495 char *secname;
16496 asection *sec;
16497
d02603dc 16498 c = get_symbol_name (&secname);
8a06b769
TS
16499 sec = bfd_get_section_by_name (stdoutput, secname);
16500 if (sec == NULL)
16501 as_bad (_("%s: no such section"), secname);
d02603dc 16502 (void) restore_line_pointer (c);
8a06b769
TS
16503
16504 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
d69cd47e 16505 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
8a06b769
TS
16506 }
16507
8a06b769
TS
16508 c = *input_line_pointer;
16509 if (c == ',')
16510 {
16511 input_line_pointer++;
16512 SKIP_WHITESPACE ();
16513 if (is_end_of_line[(unsigned char) *input_line_pointer])
16514 c = '\n';
16515 }
16516 }
16517 while (c == ',');
252b5132 16518
252b5132
RH
16519 demand_empty_rest_of_line ();
16520}
16521
d69cd47e
AM
16522#ifdef TE_IRIX
16523/* The Irix 5 and 6 assemblers set the type of any common symbol and
16524 any undefined non-function symbol to STT_OBJECT. We try to be
16525 compatible, since newer Irix 5 and 6 linkers care. */
16526
16527void
16528mips_frob_symbol (symbolS *symp ATTRIBUTE_UNUSED)
16529{
16530 /* This late in assembly we can set BSF_OBJECT indiscriminately
16531 and let elf.c:swap_out_syms sort out the symbol type. */
16532 flagword *flags = &symbol_get_bfdsym (symp)->flags;
16533 if ((*flags & (BSF_GLOBAL | BSF_WEAK)) != 0
16534 || !S_IS_DEFINED (symp))
16535 *flags |= BSF_OBJECT;
16536}
16537#endif
16538
252b5132 16539static void
17a2f251 16540s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
16541{
16542 char *opt;
16543 char c;
16544
d02603dc 16545 c = get_symbol_name (&opt);
252b5132
RH
16546
16547 if (*opt == 'O')
16548 {
16549 /* FIXME: What does this mean? */
16550 }
41a1578e 16551 else if (strncmp (opt, "pic", 3) == 0 && ISDIGIT (opt[3]) && opt[4] == '\0')
252b5132
RH
16552 {
16553 int i;
16554
16555 i = atoi (opt + 3);
668c5ebc
MR
16556 if (i != 0 && i != 2)
16557 as_bad (_(".option pic%d not supported"), i);
16558 else if (mips_pic == VXWORKS_PIC)
16559 as_bad (_(".option pic%d not supported in VxWorks PIC mode"), i);
16560 else if (i == 0)
252b5132
RH
16561 mips_pic = NO_PIC;
16562 else if (i == 2)
143d77c5 16563 {
8b828383 16564 mips_pic = SVR4_PIC;
143d77c5
EC
16565 mips_abicalls = TRUE;
16566 }
252b5132 16567
4d0d148d 16568 if (mips_pic == SVR4_PIC)
252b5132
RH
16569 {
16570 if (g_switch_seen && g_switch_value != 0)
16571 as_warn (_("-G may not be used with SVR4 PIC code"));
16572 g_switch_value = 0;
16573 bfd_set_gp_size (stdoutput, 0);
16574 }
16575 }
16576 else
1661c76c 16577 as_warn (_("unrecognized option \"%s\""), opt);
252b5132 16578
d02603dc 16579 (void) restore_line_pointer (c);
252b5132
RH
16580 demand_empty_rest_of_line ();
16581}
16582
16583/* This structure is used to hold a stack of .set values. */
16584
e972090a
NC
16585struct mips_option_stack
16586{
252b5132
RH
16587 struct mips_option_stack *next;
16588 struct mips_set_options options;
16589};
16590
16591static struct mips_option_stack *mips_opts_stack;
16592
22522f88
MR
16593/* Return status for .set/.module option handling. */
16594
16595enum code_option_type
16596{
16597 /* Unrecognized option. */
16598 OPTION_TYPE_BAD = -1,
16599
16600 /* Ordinary option. */
16601 OPTION_TYPE_NORMAL,
16602
16603 /* ISA changing option. */
16604 OPTION_TYPE_ISA
16605};
16606
16607/* Handle common .set/.module options. Return status indicating option
16608 type. */
16609
16610static enum code_option_type
919731af 16611parse_code_option (char * name)
252b5132 16612{
22522f88 16613 bfd_boolean isa_set = FALSE;
c6278170 16614 const struct mips_ase *ase;
22522f88 16615
919731af 16616 if (strncmp (name, "at=", 3) == 0)
741fe287
MR
16617 {
16618 char *s = name + 3;
16619
16620 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
1661c76c 16621 as_bad (_("unrecognized register name `%s'"), s);
741fe287 16622 }
252b5132 16623 else if (strcmp (name, "at") == 0)
919731af 16624 mips_opts.at = ATREG;
252b5132 16625 else if (strcmp (name, "noat") == 0)
919731af 16626 mips_opts.at = ZERO;
252b5132 16627 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
919731af 16628 mips_opts.nomove = 0;
252b5132 16629 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
919731af 16630 mips_opts.nomove = 1;
252b5132 16631 else if (strcmp (name, "bopt") == 0)
919731af 16632 mips_opts.nobopt = 0;
252b5132 16633 else if (strcmp (name, "nobopt") == 0)
919731af 16634 mips_opts.nobopt = 1;
ad3fea08 16635 else if (strcmp (name, "gp=32") == 0)
bad1aba3 16636 mips_opts.gp = 32;
ad3fea08 16637 else if (strcmp (name, "gp=64") == 0)
919731af 16638 mips_opts.gp = 64;
ad3fea08 16639 else if (strcmp (name, "fp=32") == 0)
0b35dfee 16640 mips_opts.fp = 32;
351cdf24
MF
16641 else if (strcmp (name, "fp=xx") == 0)
16642 mips_opts.fp = 0;
ad3fea08 16643 else if (strcmp (name, "fp=64") == 0)
919731af 16644 mips_opts.fp = 64;
037b32b9
AN
16645 else if (strcmp (name, "softfloat") == 0)
16646 mips_opts.soft_float = 1;
16647 else if (strcmp (name, "hardfloat") == 0)
16648 mips_opts.soft_float = 0;
16649 else if (strcmp (name, "singlefloat") == 0)
16650 mips_opts.single_float = 1;
16651 else if (strcmp (name, "doublefloat") == 0)
16652 mips_opts.single_float = 0;
351cdf24
MF
16653 else if (strcmp (name, "nooddspreg") == 0)
16654 mips_opts.oddspreg = 0;
16655 else if (strcmp (name, "oddspreg") == 0)
16656 mips_opts.oddspreg = 1;
252b5132
RH
16657 else if (strcmp (name, "mips16") == 0
16658 || strcmp (name, "MIPS-16") == 0)
919731af 16659 mips_opts.mips16 = 1;
252b5132
RH
16660 else if (strcmp (name, "nomips16") == 0
16661 || strcmp (name, "noMIPS-16") == 0)
16662 mips_opts.mips16 = 0;
df58fc94 16663 else if (strcmp (name, "micromips") == 0)
919731af 16664 mips_opts.micromips = 1;
df58fc94
RS
16665 else if (strcmp (name, "nomicromips") == 0)
16666 mips_opts.micromips = 0;
c6278170
RS
16667 else if (name[0] == 'n'
16668 && name[1] == 'o'
16669 && (ase = mips_lookup_ase (name + 2)))
919731af 16670 mips_set_ase (ase, &mips_opts, FALSE);
c6278170 16671 else if ((ase = mips_lookup_ase (name)))
919731af 16672 mips_set_ase (ase, &mips_opts, TRUE);
1a2c1fad 16673 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 16674 {
1a2c1fad
CD
16675 /* Permit the user to change the ISA and architecture on the fly.
16676 Needless to say, misuse can cause serious problems. */
919731af 16677 if (strncmp (name, "arch=", 5) == 0)
1a2c1fad
CD
16678 {
16679 const struct mips_cpu_info *p;
16680
919731af 16681 p = mips_parse_cpu ("internal use", name + 5);
1a2c1fad
CD
16682 if (!p)
16683 as_bad (_("unknown architecture %s"), name + 5);
16684 else
16685 {
16686 mips_opts.arch = p->cpu;
16687 mips_opts.isa = p->isa;
22522f88 16688 isa_set = TRUE;
3315614d 16689 mips_opts.init_ase = p->ase;
1a2c1fad
CD
16690 }
16691 }
81a21e38
TS
16692 else if (strncmp (name, "mips", 4) == 0)
16693 {
16694 const struct mips_cpu_info *p;
16695
919731af 16696 p = mips_parse_cpu ("internal use", name);
81a21e38
TS
16697 if (!p)
16698 as_bad (_("unknown ISA level %s"), name + 4);
16699 else
16700 {
16701 mips_opts.arch = p->cpu;
16702 mips_opts.isa = p->isa;
22522f88 16703 isa_set = TRUE;
3315614d 16704 mips_opts.init_ase = p->ase;
81a21e38
TS
16705 }
16706 }
af7ee8bf 16707 else
81a21e38 16708 as_bad (_("unknown ISA or architecture %s"), name);
252b5132
RH
16709 }
16710 else if (strcmp (name, "autoextend") == 0)
16711 mips_opts.noautoextend = 0;
16712 else if (strcmp (name, "noautoextend") == 0)
16713 mips_opts.noautoextend = 1;
833794fc
MR
16714 else if (strcmp (name, "insn32") == 0)
16715 mips_opts.insn32 = TRUE;
16716 else if (strcmp (name, "noinsn32") == 0)
16717 mips_opts.insn32 = FALSE;
919731af 16718 else if (strcmp (name, "sym32") == 0)
16719 mips_opts.sym32 = TRUE;
16720 else if (strcmp (name, "nosym32") == 0)
16721 mips_opts.sym32 = FALSE;
16722 else
22522f88
MR
16723 return OPTION_TYPE_BAD;
16724
16725 return isa_set ? OPTION_TYPE_ISA : OPTION_TYPE_NORMAL;
919731af 16726}
16727
16728/* Handle the .set pseudo-op. */
16729
16730static void
16731s_mipsset (int x ATTRIBUTE_UNUSED)
16732{
22522f88 16733 enum code_option_type type = OPTION_TYPE_NORMAL;
919731af 16734 char *name = input_line_pointer, ch;
919731af 16735
16736 file_mips_check_options ();
16737
16738 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16739 ++input_line_pointer;
16740 ch = *input_line_pointer;
16741 *input_line_pointer = '\0';
16742
16743 if (strchr (name, ','))
16744 {
16745 /* Generic ".set" directive; use the generic handler. */
16746 *input_line_pointer = ch;
16747 input_line_pointer = name;
16748 s_set (0);
16749 return;
16750 }
16751
16752 if (strcmp (name, "reorder") == 0)
16753 {
16754 if (mips_opts.noreorder)
16755 end_noreorder ();
16756 }
16757 else if (strcmp (name, "noreorder") == 0)
16758 {
16759 if (!mips_opts.noreorder)
16760 start_noreorder ();
16761 }
16762 else if (strcmp (name, "macro") == 0)
16763 mips_opts.warn_about_macros = 0;
16764 else if (strcmp (name, "nomacro") == 0)
16765 {
16766 if (mips_opts.noreorder == 0)
16767 as_bad (_("`noreorder' must be set before `nomacro'"));
16768 mips_opts.warn_about_macros = 1;
16769 }
16770 else if (strcmp (name, "gp=default") == 0)
16771 mips_opts.gp = file_mips_opts.gp;
16772 else if (strcmp (name, "fp=default") == 0)
16773 mips_opts.fp = file_mips_opts.fp;
16774 else if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
16775 {
16776 mips_opts.isa = file_mips_opts.isa;
16777 mips_opts.arch = file_mips_opts.arch;
3315614d 16778 mips_opts.init_ase = file_mips_opts.init_ase;
919731af 16779 mips_opts.gp = file_mips_opts.gp;
16780 mips_opts.fp = file_mips_opts.fp;
16781 }
252b5132
RH
16782 else if (strcmp (name, "push") == 0)
16783 {
16784 struct mips_option_stack *s;
16785
325801bd 16786 s = XNEW (struct mips_option_stack);
252b5132
RH
16787 s->next = mips_opts_stack;
16788 s->options = mips_opts;
16789 mips_opts_stack = s;
16790 }
16791 else if (strcmp (name, "pop") == 0)
16792 {
16793 struct mips_option_stack *s;
16794
16795 s = mips_opts_stack;
16796 if (s == NULL)
16797 as_bad (_(".set pop with no .set push"));
16798 else
16799 {
16800 /* If we're changing the reorder mode we need to handle
16801 delay slots correctly. */
16802 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 16803 start_noreorder ();
252b5132 16804 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 16805 end_noreorder ();
252b5132
RH
16806
16807 mips_opts = s->options;
16808 mips_opts_stack = s->next;
16809 free (s);
16810 }
16811 }
22522f88
MR
16812 else
16813 {
16814 type = parse_code_option (name);
16815 if (type == OPTION_TYPE_BAD)
16816 as_warn (_("tried to set unrecognized symbol: %s\n"), name);
16817 }
919731af 16818
16819 /* The use of .set [arch|cpu]= historically 'fixes' the width of gp and fp
16820 registers based on what is supported by the arch/cpu. */
22522f88 16821 if (type == OPTION_TYPE_ISA)
e6559e01 16822 {
919731af 16823 switch (mips_opts.isa)
16824 {
16825 case 0:
16826 break;
16827 case ISA_MIPS1:
351cdf24
MF
16828 /* MIPS I cannot support FPXX. */
16829 mips_opts.fp = 32;
16830 /* fall-through. */
919731af 16831 case ISA_MIPS2:
16832 case ISA_MIPS32:
16833 case ISA_MIPS32R2:
16834 case ISA_MIPS32R3:
16835 case ISA_MIPS32R5:
16836 mips_opts.gp = 32;
351cdf24
MF
16837 if (mips_opts.fp != 0)
16838 mips_opts.fp = 32;
919731af 16839 break;
7361da2c
AB
16840 case ISA_MIPS32R6:
16841 mips_opts.gp = 32;
16842 mips_opts.fp = 64;
16843 break;
919731af 16844 case ISA_MIPS3:
16845 case ISA_MIPS4:
16846 case ISA_MIPS5:
16847 case ISA_MIPS64:
16848 case ISA_MIPS64R2:
16849 case ISA_MIPS64R3:
16850 case ISA_MIPS64R5:
7361da2c 16851 case ISA_MIPS64R6:
919731af 16852 mips_opts.gp = 64;
351cdf24
MF
16853 if (mips_opts.fp != 0)
16854 {
16855 if (mips_opts.arch == CPU_R5900)
16856 mips_opts.fp = 32;
16857 else
16858 mips_opts.fp = 64;
16859 }
919731af 16860 break;
16861 default:
16862 as_bad (_("unknown ISA level %s"), name + 4);
16863 break;
16864 }
e6559e01 16865 }
919731af 16866
16867 mips_check_options (&mips_opts, FALSE);
16868
16869 mips_check_isa_supports_ases ();
16870 *input_line_pointer = ch;
16871 demand_empty_rest_of_line ();
16872}
16873
16874/* Handle the .module pseudo-op. */
16875
16876static void
16877s_module (int ignore ATTRIBUTE_UNUSED)
16878{
16879 char *name = input_line_pointer, ch;
16880
16881 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16882 ++input_line_pointer;
16883 ch = *input_line_pointer;
16884 *input_line_pointer = '\0';
16885
16886 if (!file_mips_opts_checked)
252b5132 16887 {
22522f88 16888 if (parse_code_option (name) == OPTION_TYPE_BAD)
919731af 16889 as_bad (_(".module used with unrecognized symbol: %s\n"), name);
16890
16891 /* Update module level settings from mips_opts. */
16892 file_mips_opts = mips_opts;
252b5132 16893 }
919731af 16894 else
16895 as_bad (_(".module is not permitted after generating code"));
16896
252b5132
RH
16897 *input_line_pointer = ch;
16898 demand_empty_rest_of_line ();
16899}
16900
16901/* Handle the .abicalls pseudo-op. I believe this is equivalent to
16902 .option pic2. It means to generate SVR4 PIC calls. */
16903
16904static void
17a2f251 16905s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16906{
16907 mips_pic = SVR4_PIC;
143d77c5 16908 mips_abicalls = TRUE;
4d0d148d
TS
16909
16910 if (g_switch_seen && g_switch_value != 0)
16911 as_warn (_("-G may not be used with SVR4 PIC code"));
16912 g_switch_value = 0;
16913
252b5132
RH
16914 bfd_set_gp_size (stdoutput, 0);
16915 demand_empty_rest_of_line ();
16916}
16917
16918/* Handle the .cpload pseudo-op. This is used when generating SVR4
16919 PIC code. It sets the $gp register for the function based on the
16920 function address, which is in the register named in the argument.
16921 This uses a relocation against _gp_disp, which is handled specially
16922 by the linker. The result is:
16923 lui $gp,%hi(_gp_disp)
16924 addiu $gp,$gp,%lo(_gp_disp)
16925 addu $gp,$gp,.cpload argument
aa6975fb
ILT
16926 The .cpload argument is normally $25 == $t9.
16927
16928 The -mno-shared option changes this to:
bbe506e8
TS
16929 lui $gp,%hi(__gnu_local_gp)
16930 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
16931 and the argument is ignored. This saves an instruction, but the
16932 resulting code is not position independent; it uses an absolute
bbe506e8
TS
16933 address for __gnu_local_gp. Thus code assembled with -mno-shared
16934 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
16935
16936static void
17a2f251 16937s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16938{
16939 expressionS ex;
aa6975fb
ILT
16940 int reg;
16941 int in_shared;
252b5132 16942
919731af 16943 file_mips_check_options ();
16944
6478892d
TS
16945 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16946 .cpload is ignored. */
16947 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16948 {
16949 s_ignore (0);
16950 return;
16951 }
16952
a276b80c
MR
16953 if (mips_opts.mips16)
16954 {
16955 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16956 ignore_rest_of_line ();
16957 return;
16958 }
16959
d3ecfc59 16960 /* .cpload should be in a .set noreorder section. */
252b5132
RH
16961 if (mips_opts.noreorder == 0)
16962 as_warn (_(".cpload not in noreorder section"));
16963
aa6975fb
ILT
16964 reg = tc_get_register (0);
16965
16966 /* If we need to produce a 64-bit address, we are better off using
16967 the default instruction sequence. */
aed1a261 16968 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 16969
252b5132 16970 ex.X_op = O_symbol;
bbe506e8
TS
16971 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16972 "__gnu_local_gp");
252b5132
RH
16973 ex.X_op_symbol = NULL;
16974 ex.X_add_number = 0;
16975
16976 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 16977 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 16978
8a75745d
MR
16979 mips_mark_labels ();
16980 mips_assembling_insn = TRUE;
16981
584892a6 16982 macro_start ();
67c0d1eb
RS
16983 macro_build_lui (&ex, mips_gp_register);
16984 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 16985 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
16986 if (in_shared)
16987 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16988 mips_gp_register, reg);
584892a6 16989 macro_end ();
252b5132 16990
8a75745d 16991 mips_assembling_insn = FALSE;
252b5132
RH
16992 demand_empty_rest_of_line ();
16993}
16994
6478892d
TS
16995/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
16996 .cpsetup $reg1, offset|$reg2, label
16997
16998 If offset is given, this results in:
16999 sd $gp, offset($sp)
956cd1d6 17000 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
17001 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
17002 daddu $gp, $gp, $reg1
6478892d
TS
17003
17004 If $reg2 is given, this results in:
40fc1451 17005 or $reg2, $gp, $0
956cd1d6 17006 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
17007 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
17008 daddu $gp, $gp, $reg1
aa6975fb
ILT
17009 $reg1 is normally $25 == $t9.
17010
17011 The -mno-shared option replaces the last three instructions with
17012 lui $gp,%hi(_gp)
54f4ddb3 17013 addiu $gp,$gp,%lo(_gp) */
aa6975fb 17014
6478892d 17015static void
17a2f251 17016s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
17017{
17018 expressionS ex_off;
17019 expressionS ex_sym;
17020 int reg1;
6478892d 17021
919731af 17022 file_mips_check_options ();
17023
8586fc66 17024 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
17025 We also need NewABI support. */
17026 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17027 {
17028 s_ignore (0);
17029 return;
17030 }
17031
a276b80c
MR
17032 if (mips_opts.mips16)
17033 {
17034 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
17035 ignore_rest_of_line ();
17036 return;
17037 }
17038
6478892d
TS
17039 reg1 = tc_get_register (0);
17040 SKIP_WHITESPACE ();
17041 if (*input_line_pointer != ',')
17042 {
17043 as_bad (_("missing argument separator ',' for .cpsetup"));
17044 return;
17045 }
17046 else
80245285 17047 ++input_line_pointer;
6478892d
TS
17048 SKIP_WHITESPACE ();
17049 if (*input_line_pointer == '$')
80245285
TS
17050 {
17051 mips_cpreturn_register = tc_get_register (0);
17052 mips_cpreturn_offset = -1;
17053 }
6478892d 17054 else
80245285
TS
17055 {
17056 mips_cpreturn_offset = get_absolute_expression ();
17057 mips_cpreturn_register = -1;
17058 }
6478892d
TS
17059 SKIP_WHITESPACE ();
17060 if (*input_line_pointer != ',')
17061 {
17062 as_bad (_("missing argument separator ',' for .cpsetup"));
17063 return;
17064 }
17065 else
f9419b05 17066 ++input_line_pointer;
6478892d 17067 SKIP_WHITESPACE ();
f21f8242 17068 expression (&ex_sym);
6478892d 17069
8a75745d
MR
17070 mips_mark_labels ();
17071 mips_assembling_insn = TRUE;
17072
584892a6 17073 macro_start ();
6478892d
TS
17074 if (mips_cpreturn_register == -1)
17075 {
17076 ex_off.X_op = O_constant;
17077 ex_off.X_add_symbol = NULL;
17078 ex_off.X_op_symbol = NULL;
17079 ex_off.X_add_number = mips_cpreturn_offset;
17080
67c0d1eb 17081 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 17082 BFD_RELOC_LO16, SP);
6478892d
TS
17083 }
17084 else
40fc1451 17085 move_register (mips_cpreturn_register, mips_gp_register);
6478892d 17086
aed1a261 17087 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb 17088 {
df58fc94 17089 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
aa6975fb
ILT
17090 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
17091 BFD_RELOC_HI16_S);
17092
17093 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
17094 mips_gp_register, -1, BFD_RELOC_GPREL16,
17095 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
17096
17097 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
17098 mips_gp_register, reg1);
17099 }
17100 else
17101 {
17102 expressionS ex;
17103
17104 ex.X_op = O_symbol;
4184909a 17105 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
17106 ex.X_op_symbol = NULL;
17107 ex.X_add_number = 0;
6e1304d8 17108
aa6975fb
ILT
17109 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
17110 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
17111
17112 macro_build_lui (&ex, mips_gp_register);
17113 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17114 mips_gp_register, BFD_RELOC_LO16);
17115 }
f21f8242 17116
584892a6 17117 macro_end ();
6478892d 17118
8a75745d 17119 mips_assembling_insn = FALSE;
6478892d
TS
17120 demand_empty_rest_of_line ();
17121}
17122
17123static void
17a2f251 17124s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d 17125{
919731af 17126 file_mips_check_options ();
17127
6478892d 17128 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
54f4ddb3 17129 .cplocal is ignored. */
6478892d
TS
17130 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17131 {
17132 s_ignore (0);
17133 return;
17134 }
17135
a276b80c
MR
17136 if (mips_opts.mips16)
17137 {
17138 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
17139 ignore_rest_of_line ();
17140 return;
17141 }
17142
6478892d 17143 mips_gp_register = tc_get_register (0);
85b51719 17144 demand_empty_rest_of_line ();
6478892d
TS
17145}
17146
252b5132
RH
17147/* Handle the .cprestore pseudo-op. This stores $gp into a given
17148 offset from $sp. The offset is remembered, and after making a PIC
17149 call $gp is restored from that location. */
17150
17151static void
17a2f251 17152s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
17153{
17154 expressionS ex;
252b5132 17155
919731af 17156 file_mips_check_options ();
17157
6478892d 17158 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 17159 .cprestore is ignored. */
6478892d 17160 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
17161 {
17162 s_ignore (0);
17163 return;
17164 }
17165
a276b80c
MR
17166 if (mips_opts.mips16)
17167 {
17168 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
17169 ignore_rest_of_line ();
17170 return;
17171 }
17172
252b5132 17173 mips_cprestore_offset = get_absolute_expression ();
7a621144 17174 mips_cprestore_valid = 1;
252b5132
RH
17175
17176 ex.X_op = O_constant;
17177 ex.X_add_symbol = NULL;
17178 ex.X_op_symbol = NULL;
17179 ex.X_add_number = mips_cprestore_offset;
17180
8a75745d
MR
17181 mips_mark_labels ();
17182 mips_assembling_insn = TRUE;
17183
584892a6 17184 macro_start ();
67c0d1eb
RS
17185 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
17186 SP, HAVE_64BIT_ADDRESSES);
584892a6 17187 macro_end ();
252b5132 17188
8a75745d 17189 mips_assembling_insn = FALSE;
252b5132
RH
17190 demand_empty_rest_of_line ();
17191}
17192
6478892d 17193/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 17194 was given in the preceding .cpsetup, it results in:
6478892d 17195 ld $gp, offset($sp)
76b3015f 17196
6478892d 17197 If a register $reg2 was given there, it results in:
40fc1451 17198 or $gp, $reg2, $0 */
54f4ddb3 17199
6478892d 17200static void
17a2f251 17201s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
17202{
17203 expressionS ex;
6478892d 17204
919731af 17205 file_mips_check_options ();
17206
6478892d
TS
17207 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
17208 We also need NewABI support. */
17209 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17210 {
17211 s_ignore (0);
17212 return;
17213 }
17214
a276b80c
MR
17215 if (mips_opts.mips16)
17216 {
17217 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
17218 ignore_rest_of_line ();
17219 return;
17220 }
17221
8a75745d
MR
17222 mips_mark_labels ();
17223 mips_assembling_insn = TRUE;
17224
584892a6 17225 macro_start ();
6478892d
TS
17226 if (mips_cpreturn_register == -1)
17227 {
17228 ex.X_op = O_constant;
17229 ex.X_add_symbol = NULL;
17230 ex.X_op_symbol = NULL;
17231 ex.X_add_number = mips_cpreturn_offset;
17232
67c0d1eb 17233 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
17234 }
17235 else
40fc1451
SD
17236 move_register (mips_gp_register, mips_cpreturn_register);
17237
584892a6 17238 macro_end ();
6478892d 17239
8a75745d 17240 mips_assembling_insn = FALSE;
6478892d
TS
17241 demand_empty_rest_of_line ();
17242}
17243
d0f13682
CLT
17244/* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
17245 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
17246 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
17247 debug information or MIPS16 TLS. */
741d6ea8
JM
17248
17249static void
d0f13682
CLT
17250s_tls_rel_directive (const size_t bytes, const char *dirstr,
17251 bfd_reloc_code_real_type rtype)
741d6ea8
JM
17252{
17253 expressionS ex;
17254 char *p;
17255
17256 expression (&ex);
17257
17258 if (ex.X_op != O_symbol)
17259 {
1661c76c 17260 as_bad (_("unsupported use of %s"), dirstr);
741d6ea8
JM
17261 ignore_rest_of_line ();
17262 }
17263
17264 p = frag_more (bytes);
17265 md_number_to_chars (p, 0, bytes);
d0f13682 17266 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
741d6ea8 17267 demand_empty_rest_of_line ();
de64cffd 17268 mips_clear_insn_labels ();
741d6ea8
JM
17269}
17270
17271/* Handle .dtprelword. */
17272
17273static void
17274s_dtprelword (int ignore ATTRIBUTE_UNUSED)
17275{
d0f13682 17276 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
741d6ea8
JM
17277}
17278
17279/* Handle .dtpreldword. */
17280
17281static void
17282s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
17283{
d0f13682
CLT
17284 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
17285}
17286
17287/* Handle .tprelword. */
17288
17289static void
17290s_tprelword (int ignore ATTRIBUTE_UNUSED)
17291{
17292 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
17293}
17294
17295/* Handle .tpreldword. */
17296
17297static void
17298s_tpreldword (int ignore ATTRIBUTE_UNUSED)
17299{
17300 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
741d6ea8
JM
17301}
17302
6478892d
TS
17303/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
17304 code. It sets the offset to use in gp_rel relocations. */
17305
17306static void
17a2f251 17307s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
17308{
17309 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
17310 We also need NewABI support. */
17311 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17312 {
17313 s_ignore (0);
17314 return;
17315 }
17316
def2e0dd 17317 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
17318
17319 demand_empty_rest_of_line ();
17320}
17321
252b5132
RH
17322/* Handle the .gpword pseudo-op. This is used when generating PIC
17323 code. It generates a 32 bit GP relative reloc. */
17324
17325static void
17a2f251 17326s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132 17327{
a8dbcb85
TS
17328 segment_info_type *si;
17329 struct insn_label_list *l;
252b5132
RH
17330 expressionS ex;
17331 char *p;
17332
17333 /* When not generating PIC code, this is treated as .word. */
17334 if (mips_pic != SVR4_PIC)
17335 {
17336 s_cons (2);
17337 return;
17338 }
17339
a8dbcb85
TS
17340 si = seg_info (now_seg);
17341 l = si->label_list;
7d10b47d 17342 mips_emit_delays ();
252b5132 17343 if (auto_align)
462427c4 17344 mips_align (2, 0, l);
252b5132
RH
17345
17346 expression (&ex);
a1facbec 17347 mips_clear_insn_labels ();
252b5132
RH
17348
17349 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17350 {
1661c76c 17351 as_bad (_("unsupported use of .gpword"));
252b5132
RH
17352 ignore_rest_of_line ();
17353 }
17354
17355 p = frag_more (4);
17a2f251 17356 md_number_to_chars (p, 0, 4);
b34976b6 17357 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 17358 BFD_RELOC_GPREL32);
252b5132
RH
17359
17360 demand_empty_rest_of_line ();
17361}
17362
10181a0d 17363static void
17a2f251 17364s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d 17365{
a8dbcb85
TS
17366 segment_info_type *si;
17367 struct insn_label_list *l;
10181a0d
AO
17368 expressionS ex;
17369 char *p;
17370
17371 /* When not generating PIC code, this is treated as .dword. */
17372 if (mips_pic != SVR4_PIC)
17373 {
17374 s_cons (3);
17375 return;
17376 }
17377
a8dbcb85
TS
17378 si = seg_info (now_seg);
17379 l = si->label_list;
7d10b47d 17380 mips_emit_delays ();
10181a0d 17381 if (auto_align)
462427c4 17382 mips_align (3, 0, l);
10181a0d
AO
17383
17384 expression (&ex);
a1facbec 17385 mips_clear_insn_labels ();
10181a0d
AO
17386
17387 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17388 {
1661c76c 17389 as_bad (_("unsupported use of .gpdword"));
10181a0d
AO
17390 ignore_rest_of_line ();
17391 }
17392
17393 p = frag_more (8);
17a2f251 17394 md_number_to_chars (p, 0, 8);
a105a300 17395 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
6e1304d8 17396 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
17397
17398 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8
RS
17399 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
17400 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
17401
17402 demand_empty_rest_of_line ();
17403}
17404
a3f278e2
CM
17405/* Handle the .ehword pseudo-op. This is used when generating unwinding
17406 tables. It generates a R_MIPS_EH reloc. */
17407
17408static void
17409s_ehword (int ignore ATTRIBUTE_UNUSED)
17410{
17411 expressionS ex;
17412 char *p;
17413
17414 mips_emit_delays ();
17415
17416 expression (&ex);
17417 mips_clear_insn_labels ();
17418
17419 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17420 {
1661c76c 17421 as_bad (_("unsupported use of .ehword"));
a3f278e2
CM
17422 ignore_rest_of_line ();
17423 }
17424
17425 p = frag_more (4);
17426 md_number_to_chars (p, 0, 4);
17427 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
2f0c68f2 17428 BFD_RELOC_32_PCREL);
a3f278e2
CM
17429
17430 demand_empty_rest_of_line ();
17431}
17432
252b5132
RH
17433/* Handle the .cpadd pseudo-op. This is used when dealing with switch
17434 tables in SVR4 PIC code. */
17435
17436static void
17a2f251 17437s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 17438{
252b5132
RH
17439 int reg;
17440
919731af 17441 file_mips_check_options ();
17442
10181a0d
AO
17443 /* This is ignored when not generating SVR4 PIC code. */
17444 if (mips_pic != SVR4_PIC)
252b5132
RH
17445 {
17446 s_ignore (0);
17447 return;
17448 }
17449
8a75745d
MR
17450 mips_mark_labels ();
17451 mips_assembling_insn = TRUE;
17452
252b5132 17453 /* Add $gp to the register named as an argument. */
584892a6 17454 macro_start ();
252b5132 17455 reg = tc_get_register (0);
67c0d1eb 17456 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 17457 macro_end ();
252b5132 17458
8a75745d 17459 mips_assembling_insn = FALSE;
bdaaa2e1 17460 demand_empty_rest_of_line ();
252b5132
RH
17461}
17462
17463/* Handle the .insn pseudo-op. This marks instruction labels in
df58fc94 17464 mips16/micromips mode. This permits the linker to handle them specially,
252b5132
RH
17465 such as generating jalx instructions when needed. We also make
17466 them odd for the duration of the assembly, in order to generate the
17467 right sort of code. We will make them even in the adjust_symtab
17468 routine, while leaving them marked. This is convenient for the
17469 debugger and the disassembler. The linker knows to make them odd
17470 again. */
17471
17472static void
17a2f251 17473s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 17474{
7bb01e2d
MR
17475 file_mips_check_options ();
17476 file_ase_mips16 |= mips_opts.mips16;
17477 file_ase_micromips |= mips_opts.micromips;
17478
df58fc94 17479 mips_mark_labels ();
252b5132
RH
17480
17481 demand_empty_rest_of_line ();
17482}
17483
ba92f887
MR
17484/* Handle the .nan pseudo-op. */
17485
17486static void
17487s_nan (int ignore ATTRIBUTE_UNUSED)
17488{
17489 static const char str_legacy[] = "legacy";
17490 static const char str_2008[] = "2008";
17491 size_t i;
17492
17493 for (i = 0; !is_end_of_line[(unsigned char) input_line_pointer[i]]; i++);
17494
17495 if (i == sizeof (str_2008) - 1
17496 && memcmp (input_line_pointer, str_2008, i) == 0)
7361da2c 17497 mips_nan2008 = 1;
ba92f887
MR
17498 else if (i == sizeof (str_legacy) - 1
17499 && memcmp (input_line_pointer, str_legacy, i) == 0)
7361da2c
AB
17500 {
17501 if (ISA_HAS_LEGACY_NAN (file_mips_opts.isa))
17502 mips_nan2008 = 0;
17503 else
17504 as_bad (_("`%s' does not support legacy NaN"),
17505 mips_cpu_info_from_isa (file_mips_opts.isa)->name);
17506 }
ba92f887 17507 else
1661c76c 17508 as_bad (_("bad .nan directive"));
ba92f887
MR
17509
17510 input_line_pointer += i;
17511 demand_empty_rest_of_line ();
17512}
17513
754e2bb9
RS
17514/* Handle a .stab[snd] directive. Ideally these directives would be
17515 implemented in a transparent way, so that removing them would not
17516 have any effect on the generated instructions. However, s_stab
17517 internally changes the section, so in practice we need to decide
17518 now whether the preceding label marks compressed code. We do not
17519 support changing the compression mode of a label after a .stab*
17520 directive, such as in:
17521
17522 foo:
134c0c8b 17523 .stabs ...
754e2bb9
RS
17524 .set mips16
17525
17526 so the current mode wins. */
252b5132
RH
17527
17528static void
17a2f251 17529s_mips_stab (int type)
252b5132 17530{
42c0794e 17531 file_mips_check_options ();
754e2bb9 17532 mips_mark_labels ();
252b5132
RH
17533 s_stab (type);
17534}
17535
54f4ddb3 17536/* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
252b5132
RH
17537
17538static void
17a2f251 17539s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
17540{
17541 char *name;
17542 int c;
17543 symbolS *symbolP;
17544 expressionS exp;
17545
d02603dc 17546 c = get_symbol_name (&name);
252b5132
RH
17547 symbolP = symbol_find_or_make (name);
17548 S_SET_WEAK (symbolP);
17549 *input_line_pointer = c;
17550
d02603dc 17551 SKIP_WHITESPACE_AFTER_NAME ();
252b5132
RH
17552
17553 if (! is_end_of_line[(unsigned char) *input_line_pointer])
17554 {
17555 if (S_IS_DEFINED (symbolP))
17556 {
20203fb9 17557 as_bad (_("ignoring attempt to redefine symbol %s"),
252b5132
RH
17558 S_GET_NAME (symbolP));
17559 ignore_rest_of_line ();
17560 return;
17561 }
bdaaa2e1 17562
252b5132
RH
17563 if (*input_line_pointer == ',')
17564 {
17565 ++input_line_pointer;
17566 SKIP_WHITESPACE ();
17567 }
bdaaa2e1 17568
252b5132
RH
17569 expression (&exp);
17570 if (exp.X_op != O_symbol)
17571 {
20203fb9 17572 as_bad (_("bad .weakext directive"));
98d3f06f 17573 ignore_rest_of_line ();
252b5132
RH
17574 return;
17575 }
49309057 17576 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
17577 }
17578
17579 demand_empty_rest_of_line ();
17580}
17581
17582/* Parse a register string into a number. Called from the ECOFF code
17583 to parse .frame. The argument is non-zero if this is the frame
17584 register, so that we can record it in mips_frame_reg. */
17585
17586int
17a2f251 17587tc_get_register (int frame)
252b5132 17588{
707bfff6 17589 unsigned int reg;
252b5132
RH
17590
17591 SKIP_WHITESPACE ();
707bfff6
TS
17592 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17593 reg = 0;
252b5132 17594 if (frame)
7a621144
DJ
17595 {
17596 mips_frame_reg = reg != 0 ? reg : SP;
17597 mips_frame_reg_valid = 1;
17598 mips_cprestore_valid = 0;
17599 }
252b5132
RH
17600 return reg;
17601}
17602
17603valueT
17a2f251 17604md_section_align (asection *seg, valueT addr)
252b5132
RH
17605{
17606 int align = bfd_get_section_alignment (stdoutput, seg);
17607
f3ded42a
RS
17608 /* We don't need to align ELF sections to the full alignment.
17609 However, Irix 5 may prefer that we align them at least to a 16
17610 byte boundary. We don't bother to align the sections if we
17611 are targeted for an embedded system. */
17612 if (strncmp (TARGET_OS, "elf", 3) == 0)
17613 return addr;
17614 if (align > 4)
17615 align = 4;
252b5132 17616
8d3842cd 17617 return ((addr + (1 << align) - 1) & -(1 << align));
252b5132
RH
17618}
17619
17620/* Utility routine, called from above as well. If called while the
17621 input file is still being read, it's only an approximation. (For
17622 example, a symbol may later become defined which appeared to be
17623 undefined earlier.) */
17624
17625static int
17a2f251 17626nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
17627{
17628 if (sym == 0)
17629 return 0;
17630
4d0d148d 17631 if (g_switch_value > 0)
252b5132
RH
17632 {
17633 const char *symname;
17634 int change;
17635
c9914766 17636 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
17637 register. It can be if it is smaller than the -G size or if
17638 it is in the .sdata or .sbss section. Certain symbols can
c9914766 17639 not be referenced off the $gp, although it appears as though
252b5132
RH
17640 they can. */
17641 symname = S_GET_NAME (sym);
17642 if (symname != (const char *) NULL
17643 && (strcmp (symname, "eprol") == 0
17644 || strcmp (symname, "etext") == 0
17645 || strcmp (symname, "_gp") == 0
17646 || strcmp (symname, "edata") == 0
17647 || strcmp (symname, "_fbss") == 0
17648 || strcmp (symname, "_fdata") == 0
17649 || strcmp (symname, "_ftext") == 0
17650 || strcmp (symname, "end") == 0
17651 || strcmp (symname, "_gp_disp") == 0))
17652 change = 1;
17653 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17654 && (0
17655#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
17656 || (symbol_get_obj (sym)->ecoff_extern_size != 0
17657 && (symbol_get_obj (sym)->ecoff_extern_size
17658 <= g_switch_value))
252b5132
RH
17659#endif
17660 /* We must defer this decision until after the whole
17661 file has been read, since there might be a .extern
17662 after the first use of this symbol. */
17663 || (before_relaxing
17664#ifndef NO_ECOFF_DEBUGGING
49309057 17665 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
17666#endif
17667 && S_GET_VALUE (sym) == 0)
17668 || (S_GET_VALUE (sym) != 0
17669 && S_GET_VALUE (sym) <= g_switch_value)))
17670 change = 0;
17671 else
17672 {
17673 const char *segname;
17674
17675 segname = segment_name (S_GET_SEGMENT (sym));
9c2799c2 17676 gas_assert (strcmp (segname, ".lit8") != 0
252b5132
RH
17677 && strcmp (segname, ".lit4") != 0);
17678 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
17679 && strcmp (segname, ".sbss") != 0
17680 && strncmp (segname, ".sdata.", 7) != 0
d4dc2f22
TS
17681 && strncmp (segname, ".sbss.", 6) != 0
17682 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
fba2b7f9 17683 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
17684 }
17685 return change;
17686 }
17687 else
c9914766 17688 /* We are not optimizing for the $gp register. */
252b5132
RH
17689 return 1;
17690}
17691
5919d012
RS
17692
17693/* Return true if the given symbol should be considered local for SVR4 PIC. */
17694
17695static bfd_boolean
9e009953 17696pic_need_relax (symbolS *sym)
5919d012
RS
17697{
17698 asection *symsec;
5919d012
RS
17699
17700 /* Handle the case of a symbol equated to another symbol. */
17701 while (symbol_equated_reloc_p (sym))
17702 {
17703 symbolS *n;
17704
5f0fe04b 17705 /* It's possible to get a loop here in a badly written program. */
5919d012
RS
17706 n = symbol_get_value_expression (sym)->X_add_symbol;
17707 if (n == sym)
17708 break;
17709 sym = n;
17710 }
17711
df1f3cda
DD
17712 if (symbol_section_p (sym))
17713 return TRUE;
17714
5919d012
RS
17715 symsec = S_GET_SEGMENT (sym);
17716
5919d012 17717 /* This must duplicate the test in adjust_reloc_syms. */
45dfa85a
AM
17718 return (!bfd_is_und_section (symsec)
17719 && !bfd_is_abs_section (symsec)
5f0fe04b 17720 && !bfd_is_com_section (symsec)
5919d012 17721 /* A global or weak symbol is treated as external. */
f3ded42a 17722 && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
5919d012 17723}
14f72d45
MR
17724\f
17725/* Given a MIPS16 variant frag FRAGP and PC-relative operand PCREL_OP
17726 convert a section-relative value VAL to the equivalent PC-relative
17727 value. */
17728
17729static offsetT
17730mips16_pcrel_val (fragS *fragp, const struct mips_pcrel_operand *pcrel_op,
17731 offsetT val, long stretch)
17732{
17733 fragS *sym_frag;
17734 addressT addr;
17735
17736 gas_assert (pcrel_op->root.root.type == OP_PCREL);
17737
17738 sym_frag = symbol_get_frag (fragp->fr_symbol);
17739
17740 /* If the relax_marker of the symbol fragment differs from the
17741 relax_marker of this fragment, we have not yet adjusted the
17742 symbol fragment fr_address. We want to add in STRETCH in
17743 order to get a better estimate of the address. This
17744 particularly matters because of the shift bits. */
17745 if (stretch != 0 && sym_frag->relax_marker != fragp->relax_marker)
17746 {
17747 fragS *f;
17748
17749 /* Adjust stretch for any alignment frag. Note that if have
17750 been expanding the earlier code, the symbol may be
17751 defined in what appears to be an earlier frag. FIXME:
17752 This doesn't handle the fr_subtype field, which specifies
17753 a maximum number of bytes to skip when doing an
17754 alignment. */
17755 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
17756 {
17757 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17758 {
17759 if (stretch < 0)
17760 stretch = -(-stretch & ~((1 << (int) f->fr_offset) - 1));
17761 else
17762 stretch &= ~((1 << (int) f->fr_offset) - 1);
17763 if (stretch == 0)
17764 break;
17765 }
17766 }
17767 if (f != NULL)
17768 val += stretch;
17769 }
17770
17771 addr = fragp->fr_address + fragp->fr_fix;
17772
17773 /* The base address rules are complicated. The base address of
17774 a branch is the following instruction. The base address of a
17775 PC relative load or add is the instruction itself, but if it
17776 is in a delay slot (in which case it can not be extended) use
17777 the address of the instruction whose delay slot it is in. */
17778 if (pcrel_op->include_isa_bit)
17779 {
17780 addr += 2;
17781
17782 /* If we are currently assuming that this frag should be
17783 extended, then the current address is two bytes higher. */
17784 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17785 addr += 2;
17786
17787 /* Ignore the low bit in the target, since it will be set
17788 for a text label. */
17789 val &= -2;
17790 }
17791 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17792 addr -= 4;
17793 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17794 addr -= 2;
5919d012 17795
14f72d45
MR
17796 val -= addr & -(1 << pcrel_op->align_log2);
17797
17798 return val;
17799}
5919d012 17800
252b5132
RH
17801/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17802 extended opcode. SEC is the section the frag is in. */
17803
17804static int
17a2f251 17805mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132 17806{
3ccad066 17807 const struct mips_int_operand *operand;
252b5132 17808 offsetT val;
252b5132 17809 segT symsec;
14f72d45 17810 int type;
252b5132
RH
17811
17812 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17813 return 0;
17814 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17815 return 1;
17816
88a7ef16 17817 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132 17818 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
3ccad066 17819 operand = mips16_immed_operand (type, FALSE);
88a7ef16
MR
17820 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
17821 || (operand->root.type == OP_PCREL
17822 ? sec != symsec
17823 : !bfd_is_abs_section (symsec)))
17824 return 1;
252b5132 17825
88a7ef16 17826 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
252b5132 17827
3ccad066 17828 if (operand->root.type == OP_PCREL)
252b5132 17829 {
3ccad066 17830 const struct mips_pcrel_operand *pcrel_op;
3ccad066 17831 offsetT maxtiny;
252b5132 17832
1425c41d 17833 if (RELAX_MIPS16_ALWAYS_EXTENDED (fragp->fr_subtype))
88a7ef16 17834 return 1;
252b5132 17835
88a7ef16 17836 pcrel_op = (const struct mips_pcrel_operand *) operand;
14f72d45 17837 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
252b5132
RH
17838
17839 /* If any of the shifted bits are set, we must use an extended
17840 opcode. If the address depends on the size of this
17841 instruction, this can lead to a loop, so we arrange to always
88a7ef16
MR
17842 use an extended opcode. */
17843 if ((val & ((1 << operand->shift) - 1)) != 0)
252b5132
RH
17844 {
17845 fragp->fr_subtype =
1425c41d 17846 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
252b5132
RH
17847 return 1;
17848 }
17849
17850 /* If we are about to mark a frag as extended because the value
3ccad066
RS
17851 is precisely the next value above maxtiny, then there is a
17852 chance of an infinite loop as in the following code:
252b5132
RH
17853 la $4,foo
17854 .skip 1020
17855 .align 2
17856 foo:
17857 In this case when the la is extended, foo is 0x3fc bytes
17858 away, so the la can be shrunk, but then foo is 0x400 away, so
17859 the la must be extended. To avoid this loop, we mark the
17860 frag as extended if it was small, and is about to become
3ccad066
RS
17861 extended with the next value above maxtiny. */
17862 maxtiny = mips_int_operand_max (operand);
17863 if (val == maxtiny + (1 << operand->shift)
88a7ef16 17864 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
252b5132
RH
17865 {
17866 fragp->fr_subtype =
1425c41d 17867 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
252b5132
RH
17868 return 1;
17869 }
17870 }
252b5132 17871
3ccad066 17872 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
252b5132
RH
17873}
17874
8507b6e7
MR
17875/* Given a MIPS16 variant frag FRAGP, return non-zero if it needs
17876 macro expansion. SEC is the section the frag is in. We only
17877 support PC-relative instructions (LA, DLA, LW, LD) here, in
17878 non-PIC code using 32-bit addressing. */
17879
17880static int
17881mips16_macro_frag (fragS *fragp, asection *sec, long stretch)
17882{
17883 const struct mips_pcrel_operand *pcrel_op;
17884 const struct mips_int_operand *operand;
17885 offsetT val;
17886 segT symsec;
17887 int type;
17888
17889 gas_assert (!RELAX_MIPS16_USER_SMALL (fragp->fr_subtype));
17890
17891 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17892 return 0;
17893 if (!RELAX_MIPS16_SYM32 (fragp->fr_subtype))
17894 return 0;
17895
17896 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17897 switch (type)
17898 {
17899 case 'A':
17900 case 'B':
17901 case 'E':
17902 symsec = S_GET_SEGMENT (fragp->fr_symbol);
17903 if (bfd_is_abs_section (symsec))
17904 return 1;
17905 if (RELAX_MIPS16_PIC (fragp->fr_subtype))
17906 return 0;
17907 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE) || sec != symsec)
17908 return 1;
17909
17910 operand = mips16_immed_operand (type, TRUE);
17911 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17912 pcrel_op = (const struct mips_pcrel_operand *) operand;
17913 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
17914
17915 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
17916
17917 default:
17918 return 0;
17919 }
17920}
17921
4a6a3df4
AO
17922/* Compute the length of a branch sequence, and adjust the
17923 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
17924 worst-case length is computed, with UPDATE being used to indicate
17925 whether an unconditional (-1), branch-likely (+1) or regular (0)
17926 branch is to be computed. */
17927static int
17a2f251 17928relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 17929{
b34976b6 17930 bfd_boolean toofar;
4a6a3df4
AO
17931 int length;
17932
17933 if (fragp
17934 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 17935 && !S_IS_WEAK (fragp->fr_symbol)
4a6a3df4
AO
17936 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17937 {
17938 addressT addr;
17939 offsetT val;
17940
17941 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17942
17943 addr = fragp->fr_address + fragp->fr_fix + 4;
17944
17945 val -= addr;
17946
17947 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17948 }
4a6a3df4 17949 else
c1f61bd2
MR
17950 /* If the symbol is not defined or it's in a different segment,
17951 we emit the long sequence. */
b34976b6 17952 toofar = TRUE;
4a6a3df4
AO
17953
17954 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17955 fragp->fr_subtype
66b3e8da 17956 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
ce8ad872 17957 RELAX_BRANCH_PIC (fragp->fr_subtype),
66b3e8da 17958 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
17959 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17960 RELAX_BRANCH_LINK (fragp->fr_subtype),
17961 toofar);
17962
17963 length = 4;
17964 if (toofar)
17965 {
17966 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17967 length += 8;
17968
ce8ad872 17969 if (!fragp || RELAX_BRANCH_PIC (fragp->fr_subtype))
4a6a3df4
AO
17970 {
17971 /* Additional space for PIC loading of target address. */
17972 length += 8;
17973 if (mips_opts.isa == ISA_MIPS1)
17974 /* Additional space for $at-stabilizing nop. */
17975 length += 4;
17976 }
17977
17978 /* If branch is conditional. */
17979 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17980 length += 8;
17981 }
b34976b6 17982
4a6a3df4
AO
17983 return length;
17984}
17985
7bd374a4
MR
17986/* Get a FRAG's branch instruction delay slot size, either from the
17987 short-delay-slot bit of a branch-and-link instruction if AL is TRUE,
17988 or SHORT_INSN_SIZE otherwise. */
17989
17990static int
17991frag_branch_delay_slot_size (fragS *fragp, bfd_boolean al, int short_insn_size)
17992{
17993 char *buf = fragp->fr_literal + fragp->fr_fix;
17994
17995 if (al)
17996 return (read_compressed_insn (buf, 4) & 0x02000000) ? 2 : 4;
17997 else
17998 return short_insn_size;
17999}
18000
df58fc94
RS
18001/* Compute the length of a branch sequence, and adjust the
18002 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
18003 worst-case length is computed, with UPDATE being used to indicate
18004 whether an unconditional (-1), or regular (0) branch is to be
18005 computed. */
18006
18007static int
18008relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
18009{
7bd374a4
MR
18010 bfd_boolean insn32 = TRUE;
18011 bfd_boolean nods = TRUE;
ce8ad872 18012 bfd_boolean pic = TRUE;
7bd374a4
MR
18013 bfd_boolean al = TRUE;
18014 int short_insn_size;
df58fc94
RS
18015 bfd_boolean toofar;
18016 int length;
18017
7bd374a4
MR
18018 if (fragp)
18019 {
18020 insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
18021 nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
ce8ad872 18022 pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
7bd374a4
MR
18023 al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18024 }
18025 short_insn_size = insn32 ? 4 : 2;
18026
df58fc94
RS
18027 if (fragp
18028 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 18029 && !S_IS_WEAK (fragp->fr_symbol)
df58fc94
RS
18030 && sec == S_GET_SEGMENT (fragp->fr_symbol))
18031 {
18032 addressT addr;
18033 offsetT val;
18034
18035 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
18036 /* Ignore the low bit in the target, since it will be set
18037 for a text label. */
18038 if ((val & 1) != 0)
18039 --val;
18040
18041 addr = fragp->fr_address + fragp->fr_fix + 4;
18042
18043 val -= addr;
18044
18045 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
18046 }
df58fc94 18047 else
c1f61bd2
MR
18048 /* If the symbol is not defined or it's in a different segment,
18049 we emit the long sequence. */
df58fc94
RS
18050 toofar = TRUE;
18051
18052 if (fragp && update
18053 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18054 fragp->fr_subtype = (toofar
18055 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
18056 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
18057
18058 length = 4;
18059 if (toofar)
18060 {
18061 bfd_boolean compact_known = fragp != NULL;
18062 bfd_boolean compact = FALSE;
18063 bfd_boolean uncond;
18064
df58fc94 18065 if (fragp)
8484fb75
MR
18066 {
18067 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18068 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
8484fb75 18069 }
df58fc94
RS
18070 else
18071 uncond = update < 0;
18072
18073 /* If label is out of range, we turn branch <br>:
18074
18075 <br> label # 4 bytes
18076 0:
18077
18078 into:
18079
18080 j label # 4 bytes
8484fb75
MR
18081 nop # 2/4 bytes if
18082 # compact && (!PIC || insn32)
df58fc94
RS
18083 0:
18084 */
ce8ad872 18085 if ((!pic || insn32) && (!compact_known || compact))
8484fb75 18086 length += short_insn_size;
df58fc94
RS
18087
18088 /* If assembling PIC code, we further turn:
18089
18090 j label # 4 bytes
18091
18092 into:
18093
18094 lw/ld at, %got(label)(gp) # 4 bytes
18095 d/addiu at, %lo(label) # 4 bytes
8484fb75 18096 jr/c at # 2/4 bytes
df58fc94 18097 */
ce8ad872 18098 if (pic)
8484fb75 18099 length += 4 + short_insn_size;
df58fc94 18100
7bd374a4
MR
18101 /* Add an extra nop if the jump has no compact form and we need
18102 to fill the delay slot. */
ce8ad872 18103 if ((!pic || al) && nods)
7bd374a4
MR
18104 length += (fragp
18105 ? frag_branch_delay_slot_size (fragp, al, short_insn_size)
18106 : short_insn_size);
18107
df58fc94
RS
18108 /* If branch <br> is conditional, we prepend negated branch <brneg>:
18109
18110 <brneg> 0f # 4 bytes
8484fb75 18111 nop # 2/4 bytes if !compact
df58fc94
RS
18112 */
18113 if (!uncond)
8484fb75 18114 length += (compact_known && compact) ? 4 : 4 + short_insn_size;
df58fc94 18115 }
7bd374a4
MR
18116 else if (nods)
18117 {
18118 /* Add an extra nop to fill the delay slot. */
18119 gas_assert (fragp);
18120 length += frag_branch_delay_slot_size (fragp, al, short_insn_size);
18121 }
df58fc94
RS
18122
18123 return length;
18124}
18125
18126/* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
18127 bit accordingly. */
18128
18129static int
18130relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
18131{
18132 bfd_boolean toofar;
18133
df58fc94
RS
18134 if (fragp
18135 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 18136 && !S_IS_WEAK (fragp->fr_symbol)
df58fc94
RS
18137 && sec == S_GET_SEGMENT (fragp->fr_symbol))
18138 {
18139 addressT addr;
18140 offsetT val;
18141 int type;
18142
18143 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
18144 /* Ignore the low bit in the target, since it will be set
18145 for a text label. */
18146 if ((val & 1) != 0)
18147 --val;
18148
18149 /* Assume this is a 2-byte branch. */
18150 addr = fragp->fr_address + fragp->fr_fix + 2;
18151
18152 /* We try to avoid the infinite loop by not adding 2 more bytes for
18153 long branches. */
18154
18155 val -= addr;
18156
18157 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
18158 if (type == 'D')
18159 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
18160 else if (type == 'E')
18161 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
18162 else
18163 abort ();
18164 }
18165 else
18166 /* If the symbol is not defined or it's in a different segment,
18167 we emit a normal 32-bit branch. */
18168 toofar = TRUE;
18169
18170 if (fragp && update
18171 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18172 fragp->fr_subtype
18173 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
18174 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
18175
18176 if (toofar)
18177 return 4;
18178
18179 return 2;
18180}
18181
252b5132
RH
18182/* Estimate the size of a frag before relaxing. Unless this is the
18183 mips16, we are not really relaxing here, and the final size is
18184 encoded in the subtype information. For the mips16, we have to
18185 decide whether we are using an extended opcode or not. */
18186
252b5132 18187int
17a2f251 18188md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 18189{
5919d012 18190 int change;
252b5132 18191
4a6a3df4
AO
18192 if (RELAX_BRANCH_P (fragp->fr_subtype))
18193 {
18194
b34976b6
AM
18195 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
18196
4a6a3df4
AO
18197 return fragp->fr_var;
18198 }
18199
252b5132 18200 if (RELAX_MIPS16_P (fragp->fr_subtype))
8507b6e7
MR
18201 {
18202 /* We don't want to modify the EXTENDED bit here; it might get us
18203 into infinite loops. We change it only in mips_relax_frag(). */
18204 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
25499ac7 18205 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 8 : 12;
8507b6e7
MR
18206 else
18207 return RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2;
18208 }
252b5132 18209
df58fc94
RS
18210 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18211 {
18212 int length = 4;
18213
18214 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
18215 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
18216 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
18217 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
18218 fragp->fr_var = length;
18219
18220 return length;
18221 }
18222
ce8ad872 18223 if (mips_pic == VXWORKS_PIC)
0a44bf69
RS
18224 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
18225 change = 0;
ce8ad872
MR
18226 else if (RELAX_PIC (fragp->fr_subtype))
18227 change = pic_need_relax (fragp->fr_symbol);
252b5132 18228 else
ce8ad872 18229 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132
RH
18230
18231 if (change)
18232 {
4d7206a2 18233 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 18234 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 18235 }
4d7206a2
RS
18236 else
18237 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
18238}
18239
18240/* This is called to see whether a reloc against a defined symbol
de7e6852 18241 should be converted into a reloc against a section. */
252b5132
RH
18242
18243int
17a2f251 18244mips_fix_adjustable (fixS *fixp)
252b5132 18245{
252b5132
RH
18246 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
18247 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
18248 return 0;
a161fe53 18249
252b5132
RH
18250 if (fixp->fx_addsy == NULL)
18251 return 1;
a161fe53 18252
2f0c68f2
CM
18253 /* Allow relocs used for EH tables. */
18254 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
18255 return 1;
18256
de7e6852
RS
18257 /* If symbol SYM is in a mergeable section, relocations of the form
18258 SYM + 0 can usually be made section-relative. The mergeable data
18259 is then identified by the section offset rather than by the symbol.
18260
18261 However, if we're generating REL LO16 relocations, the offset is split
33eaf5de 18262 between the LO16 and partnering high part relocation. The linker will
de7e6852
RS
18263 need to recalculate the complete offset in order to correctly identify
18264 the merge data.
18265
33eaf5de 18266 The linker has traditionally not looked for the partnering high part
de7e6852
RS
18267 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
18268 placed anywhere. Rather than break backwards compatibility by changing
18269 this, it seems better not to force the issue, and instead keep the
18270 original symbol. This will work with either linker behavior. */
738e5348 18271 if ((lo16_reloc_p (fixp->fx_r_type)
704803a9 18272 || reloc_needs_lo_p (fixp->fx_r_type))
de7e6852
RS
18273 && HAVE_IN_PLACE_ADDENDS
18274 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
18275 return 0;
18276
97f50151
MR
18277 /* There is no place to store an in-place offset for JALR relocations. */
18278 if (jalr_reloc_p (fixp->fx_r_type) && HAVE_IN_PLACE_ADDENDS)
18279 return 0;
18280
18281 /* Likewise an in-range offset of limited PC-relative relocations may
2de39019 18282 overflow the in-place relocatable field if recalculated against the
7361da2c
AB
18283 start address of the symbol's containing section.
18284
18285 Also, PC relative relocations for MIPS R6 need to be symbol rather than
18286 section relative to allow linker relaxations to be performed later on. */
97f50151 18287 if (limited_pcrel_reloc_p (fixp->fx_r_type)
912815f0 18288 && (HAVE_IN_PLACE_ADDENDS || ISA_IS_R6 (file_mips_opts.isa)))
1180b5a4
RS
18289 return 0;
18290
b314ec0e
RS
18291 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
18292 to a floating-point stub. The same is true for non-R_MIPS16_26
18293 relocations against MIPS16 functions; in this case, the stub becomes
18294 the function's canonical address.
18295
18296 Floating-point stubs are stored in unique .mips16.call.* or
18297 .mips16.fn.* sections. If a stub T for function F is in section S,
18298 the first relocation in section S must be against F; this is how the
18299 linker determines the target function. All relocations that might
18300 resolve to T must also be against F. We therefore have the following
18301 restrictions, which are given in an intentionally-redundant way:
18302
18303 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
18304 symbols.
18305
18306 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
18307 if that stub might be used.
18308
18309 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
18310 symbols.
18311
18312 4. We cannot reduce a stub's relocations against MIPS16 symbols if
18313 that stub might be used.
18314
18315 There is a further restriction:
18316
df58fc94 18317 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
0e9c5a5c 18318 R_MICROMIPS_26_S1) or branch relocations (R_MIPS_PC26_S2,
c9775dde
MR
18319 R_MIPS_PC21_S2, R_MIPS_PC16, R_MIPS16_PC16_S1,
18320 R_MICROMIPS_PC16_S1, R_MICROMIPS_PC10_S1 or R_MICROMIPS_PC7_S1)
18321 against MIPS16 or microMIPS symbols because we need to keep the
18322 MIPS16 or microMIPS symbol for the purpose of mode mismatch
a6ebf616
MR
18323 detection and JAL or BAL to JALX instruction conversion in the
18324 linker.
b314ec0e 18325
df58fc94 18326 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
507dcb32 18327 against a MIPS16 symbol. We deal with (5) by additionally leaving
0e9c5a5c 18328 alone any jump and branch relocations against a microMIPS symbol.
b314ec0e
RS
18329
18330 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
18331 relocation against some symbol R, no relocation against R may be
18332 reduced. (Note that this deals with (2) as well as (1) because
18333 relocations against global symbols will never be reduced on ELF
18334 targets.) This approach is a little simpler than trying to detect
18335 stub sections, and gives the "all or nothing" per-symbol consistency
18336 that we have for MIPS16 symbols. */
f3ded42a 18337 if (fixp->fx_subsy == NULL
30c09090 18338 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
44d3da23 18339 || (ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
0e9c5a5c
MR
18340 && (jmp_reloc_p (fixp->fx_r_type)
18341 || b_reloc_p (fixp->fx_r_type)))
44d3da23 18342 || *symbol_get_tc (fixp->fx_addsy)))
252b5132 18343 return 0;
a161fe53 18344
252b5132
RH
18345 return 1;
18346}
18347
18348/* Translate internal representation of relocation info to BFD target
18349 format. */
18350
18351arelent **
17a2f251 18352tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
18353{
18354 static arelent *retval[4];
18355 arelent *reloc;
18356 bfd_reloc_code_real_type code;
18357
4b0cff4e 18358 memset (retval, 0, sizeof(retval));
325801bd
TS
18359 reloc = retval[0] = XCNEW (arelent);
18360 reloc->sym_ptr_ptr = XNEW (asymbol *);
49309057 18361 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
18362 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
18363
bad36eac
DJ
18364 if (fixp->fx_pcrel)
18365 {
df58fc94 18366 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
c9775dde 18367 || fixp->fx_r_type == BFD_RELOC_MIPS16_16_PCREL_S1
df58fc94
RS
18368 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
18369 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
b47468a6 18370 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
7361da2c
AB
18371 || fixp->fx_r_type == BFD_RELOC_32_PCREL
18372 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
18373 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
18374 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
18375 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
18376 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
18377 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL);
bad36eac
DJ
18378
18379 /* At this point, fx_addnumber is "symbol offset - pcrel address".
18380 Relocations want only the symbol offset. */
51f6035b
MR
18381 switch (fixp->fx_r_type)
18382 {
18383 case BFD_RELOC_MIPS_18_PCREL_S3:
18384 reloc->addend = fixp->fx_addnumber + (reloc->address & ~7);
18385 break;
18386 default:
18387 reloc->addend = fixp->fx_addnumber + reloc->address;
18388 break;
18389 }
bad36eac 18390 }
17c6c9d9
MR
18391 else if (HAVE_IN_PLACE_ADDENDS
18392 && fixp->fx_r_type == BFD_RELOC_MICROMIPS_JMP
18393 && (read_compressed_insn (fixp->fx_frag->fr_literal
18394 + fixp->fx_where, 4) >> 26) == 0x3c)
18395 {
18396 /* Shift is 2, unusually, for microMIPS JALX. Adjust the in-place
18397 addend accordingly. */
18398 reloc->addend = fixp->fx_addnumber >> 1;
18399 }
bad36eac
DJ
18400 else
18401 reloc->addend = fixp->fx_addnumber;
252b5132 18402
438c16b8
TS
18403 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
18404 entry to be used in the relocation's section offset. */
18405 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
18406 {
18407 reloc->address = reloc->addend;
18408 reloc->addend = 0;
18409 }
18410
252b5132 18411 code = fixp->fx_r_type;
252b5132 18412
bad36eac 18413 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
252b5132
RH
18414 if (reloc->howto == NULL)
18415 {
18416 as_bad_where (fixp->fx_file, fixp->fx_line,
1661c76c
RS
18417 _("cannot represent %s relocation in this object file"
18418 " format"),
252b5132
RH
18419 bfd_get_reloc_code_name (code));
18420 retval[0] = NULL;
18421 }
18422
18423 return retval;
18424}
18425
18426/* Relax a machine dependent frag. This returns the amount by which
18427 the current size of the frag should change. */
18428
18429int
17a2f251 18430mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 18431{
4a6a3df4
AO
18432 if (RELAX_BRANCH_P (fragp->fr_subtype))
18433 {
18434 offsetT old_var = fragp->fr_var;
b34976b6
AM
18435
18436 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
18437
18438 return fragp->fr_var - old_var;
18439 }
18440
df58fc94
RS
18441 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18442 {
18443 offsetT old_var = fragp->fr_var;
18444 offsetT new_var = 4;
18445
18446 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
18447 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
18448 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
18449 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
18450 fragp->fr_var = new_var;
18451
18452 return new_var - old_var;
18453 }
18454
252b5132
RH
18455 if (! RELAX_MIPS16_P (fragp->fr_subtype))
18456 return 0;
18457
8507b6e7 18458 if (!mips16_extended_frag (fragp, sec, stretch))
252b5132 18459 {
8507b6e7
MR
18460 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18461 {
18462 fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
25499ac7 18463 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -6 : -10;
8507b6e7
MR
18464 }
18465 else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18466 {
18467 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18468 return -2;
18469 }
18470 else
18471 return 0;
18472 }
18473 else if (!mips16_macro_frag (fragp, sec, stretch))
18474 {
18475 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18476 {
18477 fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
18478 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
25499ac7 18479 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -4 : -8;
8507b6e7
MR
18480 }
18481 else if (!RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18482 {
18483 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
18484 return 2;
18485 }
18486 else
252b5132 18487 return 0;
252b5132
RH
18488 }
18489 else
18490 {
8507b6e7 18491 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
252b5132 18492 return 0;
8507b6e7
MR
18493 else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18494 {
18495 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18496 fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
25499ac7 18497 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 4 : 8;
8507b6e7
MR
18498 }
18499 else
18500 {
18501 fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
25499ac7 18502 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 6 : 10;
8507b6e7 18503 }
252b5132
RH
18504 }
18505
18506 return 0;
18507}
18508
18509/* Convert a machine dependent frag. */
18510
18511void
17a2f251 18512md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 18513{
4a6a3df4
AO
18514 if (RELAX_BRANCH_P (fragp->fr_subtype))
18515 {
4d68580a 18516 char *buf;
4a6a3df4 18517 unsigned long insn;
4a6a3df4 18518 fixS *fixp;
b34976b6 18519
4d68580a
RS
18520 buf = fragp->fr_literal + fragp->fr_fix;
18521 insn = read_insn (buf);
b34976b6 18522
4a6a3df4
AO
18523 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
18524 {
18525 /* We generate a fixup instead of applying it right now
18526 because, if there are linker relaxations, we're going to
18527 need the relocations. */
bbd27b76
MR
18528 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18529 fragp->fr_symbol, fragp->fr_offset,
18530 TRUE, BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
18531 fixp->fx_file = fragp->fr_file;
18532 fixp->fx_line = fragp->fr_line;
b34976b6 18533
4d68580a 18534 buf = write_insn (buf, insn);
4a6a3df4
AO
18535 }
18536 else
18537 {
18538 int i;
18539
18540 as_warn_where (fragp->fr_file, fragp->fr_line,
1661c76c 18541 _("relaxed out-of-range branch into a jump"));
4a6a3df4
AO
18542
18543 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
18544 goto uncond;
18545
18546 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18547 {
18548 /* Reverse the branch. */
18549 switch ((insn >> 28) & 0xf)
18550 {
18551 case 4:
56d438b1
CF
18552 if ((insn & 0xff000000) == 0x47000000
18553 || (insn & 0xff600000) == 0x45600000)
18554 {
18555 /* BZ.df/BNZ.df, BZ.V/BNZ.V can have the condition
18556 reversed by tweaking bit 23. */
18557 insn ^= 0x00800000;
18558 }
18559 else
18560 {
18561 /* bc[0-3][tf]l? instructions can have the condition
18562 reversed by tweaking a single TF bit, and their
18563 opcodes all have 0x4???????. */
18564 gas_assert ((insn & 0xf3e00000) == 0x41000000);
18565 insn ^= 0x00010000;
18566 }
4a6a3df4
AO
18567 break;
18568
18569 case 0:
18570 /* bltz 0x04000000 bgez 0x04010000
54f4ddb3 18571 bltzal 0x04100000 bgezal 0x04110000 */
9c2799c2 18572 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
4a6a3df4
AO
18573 insn ^= 0x00010000;
18574 break;
b34976b6 18575
4a6a3df4
AO
18576 case 1:
18577 /* beq 0x10000000 bne 0x14000000
54f4ddb3 18578 blez 0x18000000 bgtz 0x1c000000 */
4a6a3df4
AO
18579 insn ^= 0x04000000;
18580 break;
18581
18582 default:
18583 abort ();
18584 }
18585 }
18586
18587 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18588 {
18589 /* Clear the and-link bit. */
9c2799c2 18590 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
4a6a3df4 18591
54f4ddb3
TS
18592 /* bltzal 0x04100000 bgezal 0x04110000
18593 bltzall 0x04120000 bgezall 0x04130000 */
4a6a3df4
AO
18594 insn &= ~0x00100000;
18595 }
18596
18597 /* Branch over the branch (if the branch was likely) or the
18598 full jump (not likely case). Compute the offset from the
18599 current instruction to branch to. */
18600 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18601 i = 16;
18602 else
18603 {
18604 /* How many bytes in instructions we've already emitted? */
4d68580a 18605 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18606 /* How many bytes in instructions from here to the end? */
18607 i = fragp->fr_var - i;
18608 }
18609 /* Convert to instruction count. */
18610 i >>= 2;
18611 /* Branch counts from the next instruction. */
b34976b6 18612 i--;
4a6a3df4
AO
18613 insn |= i;
18614 /* Branch over the jump. */
4d68580a 18615 buf = write_insn (buf, insn);
4a6a3df4 18616
54f4ddb3 18617 /* nop */
4d68580a 18618 buf = write_insn (buf, 0);
4a6a3df4
AO
18619
18620 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18621 {
18622 /* beql $0, $0, 2f */
18623 insn = 0x50000000;
18624 /* Compute the PC offset from the current instruction to
18625 the end of the variable frag. */
18626 /* How many bytes in instructions we've already emitted? */
4d68580a 18627 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18628 /* How many bytes in instructions from here to the end? */
18629 i = fragp->fr_var - i;
18630 /* Convert to instruction count. */
18631 i >>= 2;
18632 /* Don't decrement i, because we want to branch over the
18633 delay slot. */
4a6a3df4 18634 insn |= i;
4a6a3df4 18635
4d68580a
RS
18636 buf = write_insn (buf, insn);
18637 buf = write_insn (buf, 0);
4a6a3df4
AO
18638 }
18639
18640 uncond:
ce8ad872 18641 if (!RELAX_BRANCH_PIC (fragp->fr_subtype))
4a6a3df4
AO
18642 {
18643 /* j or jal. */
18644 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18645 ? 0x0c000000 : 0x08000000);
4a6a3df4 18646
bbd27b76
MR
18647 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18648 fragp->fr_symbol, fragp->fr_offset,
18649 FALSE, BFD_RELOC_MIPS_JMP);
4a6a3df4
AO
18650 fixp->fx_file = fragp->fr_file;
18651 fixp->fx_line = fragp->fr_line;
18652
4d68580a 18653 buf = write_insn (buf, insn);
4a6a3df4
AO
18654 }
18655 else
18656 {
66b3e8da
MR
18657 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18658
4a6a3df4 18659 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
66b3e8da
MR
18660 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18661 insn |= at << OP_SH_RT;
4a6a3df4 18662
bbd27b76
MR
18663 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18664 fragp->fr_symbol, fragp->fr_offset,
18665 FALSE, BFD_RELOC_MIPS_GOT16);
4a6a3df4
AO
18666 fixp->fx_file = fragp->fr_file;
18667 fixp->fx_line = fragp->fr_line;
18668
4d68580a 18669 buf = write_insn (buf, insn);
b34976b6 18670
4a6a3df4 18671 if (mips_opts.isa == ISA_MIPS1)
4d68580a
RS
18672 /* nop */
18673 buf = write_insn (buf, 0);
4a6a3df4
AO
18674
18675 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
66b3e8da
MR
18676 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18677 insn |= at << OP_SH_RS | at << OP_SH_RT;
4a6a3df4 18678
bbd27b76
MR
18679 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18680 fragp->fr_symbol, fragp->fr_offset,
18681 FALSE, BFD_RELOC_LO16);
4a6a3df4
AO
18682 fixp->fx_file = fragp->fr_file;
18683 fixp->fx_line = fragp->fr_line;
b34976b6 18684
4d68580a 18685 buf = write_insn (buf, insn);
4a6a3df4
AO
18686
18687 /* j(al)r $at. */
18688 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
66b3e8da 18689 insn = 0x0000f809;
4a6a3df4 18690 else
66b3e8da
MR
18691 insn = 0x00000008;
18692 insn |= at << OP_SH_RS;
4a6a3df4 18693
4d68580a 18694 buf = write_insn (buf, insn);
4a6a3df4
AO
18695 }
18696 }
18697
4a6a3df4 18698 fragp->fr_fix += fragp->fr_var;
4d68580a 18699 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
4a6a3df4
AO
18700 return;
18701 }
18702
df58fc94
RS
18703 /* Relax microMIPS branches. */
18704 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18705 {
4d68580a 18706 char *buf = fragp->fr_literal + fragp->fr_fix;
df58fc94 18707 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
8484fb75 18708 bfd_boolean insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
7bd374a4 18709 bfd_boolean nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
ce8ad872 18710 bfd_boolean pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
df58fc94
RS
18711 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18712 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
2309ddf2 18713 bfd_boolean short_ds;
df58fc94 18714 unsigned long insn;
df58fc94
RS
18715 fixS *fixp;
18716
df58fc94
RS
18717 fragp->fr_fix += fragp->fr_var;
18718
18719 /* Handle 16-bit branches that fit or are forced to fit. */
18720 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18721 {
18722 /* We generate a fixup instead of applying it right now,
18723 because if there is linker relaxation, we're going to
18724 need the relocations. */
834a65aa
MR
18725 switch (type)
18726 {
18727 case 'D':
18728 fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18729 fragp->fr_symbol, fragp->fr_offset,
18730 TRUE, BFD_RELOC_MICROMIPS_10_PCREL_S1);
18731 break;
18732 case 'E':
18733 fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18734 fragp->fr_symbol, fragp->fr_offset,
18735 TRUE, BFD_RELOC_MICROMIPS_7_PCREL_S1);
18736 break;
18737 default:
18738 abort ();
18739 }
df58fc94
RS
18740
18741 fixp->fx_file = fragp->fr_file;
18742 fixp->fx_line = fragp->fr_line;
18743
18744 /* These relocations can have an addend that won't fit in
18745 2 octets. */
18746 fixp->fx_no_overflow = 1;
18747
18748 return;
18749 }
18750
2309ddf2 18751 /* Handle 32-bit branches that fit or are forced to fit. */
df58fc94
RS
18752 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18753 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18754 {
18755 /* We generate a fixup instead of applying it right now,
18756 because if there is linker relaxation, we're going to
18757 need the relocations. */
bbd27b76
MR
18758 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18759 fragp->fr_symbol, fragp->fr_offset,
18760 TRUE, BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18761 fixp->fx_file = fragp->fr_file;
18762 fixp->fx_line = fragp->fr_line;
18763
18764 if (type == 0)
7bd374a4
MR
18765 {
18766 insn = read_compressed_insn (buf, 4);
18767 buf += 4;
18768
18769 if (nods)
18770 {
18771 /* Check the short-delay-slot bit. */
18772 if (!al || (insn & 0x02000000) != 0)
18773 buf = write_compressed_insn (buf, 0x0c00, 2);
18774 else
18775 buf = write_compressed_insn (buf, 0x00000000, 4);
18776 }
18777
18778 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18779 return;
18780 }
df58fc94
RS
18781 }
18782
18783 /* Relax 16-bit branches to 32-bit branches. */
18784 if (type != 0)
18785 {
4d68580a 18786 insn = read_compressed_insn (buf, 2);
df58fc94
RS
18787
18788 if ((insn & 0xfc00) == 0xcc00) /* b16 */
18789 insn = 0x94000000; /* beq */
18790 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
18791 {
18792 unsigned long regno;
18793
18794 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18795 regno = micromips_to_32_reg_d_map [regno];
18796 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
18797 insn |= regno << MICROMIPSOP_SH_RS;
18798 }
18799 else
18800 abort ();
18801
18802 /* Nothing else to do, just write it out. */
18803 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18804 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18805 {
4d68580a 18806 buf = write_compressed_insn (buf, insn, 4);
7bd374a4
MR
18807 if (nods)
18808 buf = write_compressed_insn (buf, 0x0c00, 2);
4d68580a 18809 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18810 return;
18811 }
18812 }
18813 else
4d68580a 18814 insn = read_compressed_insn (buf, 4);
df58fc94
RS
18815
18816 /* Relax 32-bit branches to a sequence of instructions. */
18817 as_warn_where (fragp->fr_file, fragp->fr_line,
1661c76c 18818 _("relaxed out-of-range branch into a jump"));
df58fc94 18819
2309ddf2 18820 /* Set the short-delay-slot bit. */
7bd374a4 18821 short_ds = !al || (insn & 0x02000000) != 0;
df58fc94
RS
18822
18823 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18824 {
18825 symbolS *l;
18826
18827 /* Reverse the branch. */
18828 if ((insn & 0xfc000000) == 0x94000000 /* beq */
18829 || (insn & 0xfc000000) == 0xb4000000) /* bne */
18830 insn ^= 0x20000000;
18831 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
18832 || (insn & 0xffe00000) == 0x40400000 /* bgez */
18833 || (insn & 0xffe00000) == 0x40800000 /* blez */
18834 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
18835 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
18836 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
18837 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
18838 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
18839 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
18840 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
18841 insn ^= 0x00400000;
18842 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
18843 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
18844 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
18845 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
18846 insn ^= 0x00200000;
56d438b1
CF
18847 else if ((insn & 0xff000000) == 0x83000000 /* BZ.df
18848 BNZ.df */
18849 || (insn & 0xff600000) == 0x81600000) /* BZ.V
18850 BNZ.V */
18851 insn ^= 0x00800000;
df58fc94
RS
18852 else
18853 abort ();
18854
18855 if (al)
18856 {
18857 /* Clear the and-link and short-delay-slot bits. */
18858 gas_assert ((insn & 0xfda00000) == 0x40200000);
18859
18860 /* bltzal 0x40200000 bgezal 0x40600000 */
18861 /* bltzals 0x42200000 bgezals 0x42600000 */
18862 insn &= ~0x02200000;
18863 }
18864
18865 /* Make a label at the end for use with the branch. */
18866 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
18867 micromips_label_inc ();
f3ded42a 18868 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
df58fc94
RS
18869
18870 /* Refer to it. */
4d68580a
RS
18871 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
18872 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18873 fixp->fx_file = fragp->fr_file;
18874 fixp->fx_line = fragp->fr_line;
18875
18876 /* Branch over the jump. */
4d68580a 18877 buf = write_compressed_insn (buf, insn, 4);
8484fb75 18878
df58fc94 18879 if (!compact)
8484fb75
MR
18880 {
18881 /* nop */
18882 if (insn32)
18883 buf = write_compressed_insn (buf, 0x00000000, 4);
18884 else
18885 buf = write_compressed_insn (buf, 0x0c00, 2);
18886 }
df58fc94
RS
18887 }
18888
ce8ad872 18889 if (!pic)
df58fc94 18890 {
7bd374a4
MR
18891 unsigned long jal = (short_ds || nods
18892 ? 0x74000000 : 0xf4000000); /* jal/s */
2309ddf2 18893
df58fc94
RS
18894 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
18895 insn = al ? jal : 0xd4000000;
18896
bbd27b76
MR
18897 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18898 fragp->fr_symbol, fragp->fr_offset,
18899 FALSE, BFD_RELOC_MICROMIPS_JMP);
df58fc94
RS
18900 fixp->fx_file = fragp->fr_file;
18901 fixp->fx_line = fragp->fr_line;
18902
4d68580a 18903 buf = write_compressed_insn (buf, insn, 4);
8484fb75 18904
7bd374a4 18905 if (compact || nods)
8484fb75
MR
18906 {
18907 /* nop */
18908 if (insn32)
18909 buf = write_compressed_insn (buf, 0x00000000, 4);
18910 else
18911 buf = write_compressed_insn (buf, 0x0c00, 2);
18912 }
df58fc94
RS
18913 }
18914 else
18915 {
18916 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
18917
18918 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
18919 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18920 insn |= at << MICROMIPSOP_SH_RT;
18921
bbd27b76
MR
18922 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18923 fragp->fr_symbol, fragp->fr_offset,
18924 FALSE, BFD_RELOC_MICROMIPS_GOT16);
df58fc94
RS
18925 fixp->fx_file = fragp->fr_file;
18926 fixp->fx_line = fragp->fr_line;
18927
4d68580a 18928 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
18929
18930 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
18931 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18932 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18933
bbd27b76
MR
18934 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18935 fragp->fr_symbol, fragp->fr_offset,
18936 FALSE, BFD_RELOC_MICROMIPS_LO16);
df58fc94
RS
18937 fixp->fx_file = fragp->fr_file;
18938 fixp->fx_line = fragp->fr_line;
18939
4d68580a 18940 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18941
8484fb75
MR
18942 if (insn32)
18943 {
18944 /* jr/jalr $at */
18945 insn = 0x00000f3c | (al ? RA : ZERO) << MICROMIPSOP_SH_RT;
18946 insn |= at << MICROMIPSOP_SH_RS;
18947
18948 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18949
7bd374a4 18950 if (compact || nods)
8484fb75
MR
18951 /* nop */
18952 buf = write_compressed_insn (buf, 0x00000000, 4);
18953 }
18954 else
18955 {
18956 /* jr/jrc/jalr/jalrs $at */
18957 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
7bd374a4 18958 unsigned long jr = compact || nods ? 0x45a0 : 0x4580; /* jr/c */
8484fb75
MR
18959
18960 insn = al ? jalr : jr;
18961 insn |= at << MICROMIPSOP_SH_MJ;
18962
18963 buf = write_compressed_insn (buf, insn, 2);
7bd374a4
MR
18964 if (al && nods)
18965 {
18966 /* nop */
18967 if (short_ds)
18968 buf = write_compressed_insn (buf, 0x0c00, 2);
18969 else
18970 buf = write_compressed_insn (buf, 0x00000000, 4);
18971 }
8484fb75 18972 }
df58fc94
RS
18973 }
18974
4d68580a 18975 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18976 return;
18977 }
18978
252b5132
RH
18979 if (RELAX_MIPS16_P (fragp->fr_subtype))
18980 {
18981 int type;
3ccad066 18982 const struct mips_int_operand *operand;
252b5132 18983 offsetT val;
5c04167a 18984 char *buf;
8507b6e7 18985 unsigned int user_length;
9d862524 18986 bfd_boolean need_reloc;
252b5132 18987 unsigned long insn;
8507b6e7 18988 bfd_boolean mac;
5c04167a 18989 bfd_boolean ext;
88a7ef16 18990 segT symsec;
252b5132
RH
18991
18992 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
3ccad066 18993 operand = mips16_immed_operand (type, FALSE);
252b5132 18994
8507b6e7 18995 mac = RELAX_MIPS16_MACRO (fragp->fr_subtype);
5c04167a 18996 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
88a7ef16 18997 val = resolve_symbol_value (fragp->fr_symbol) + fragp->fr_offset;
9d862524
MR
18998
18999 symsec = S_GET_SEGMENT (fragp->fr_symbol);
19000 need_reloc = (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
8507b6e7 19001 || (operand->root.type == OP_PCREL && !mac
9d862524
MR
19002 ? asec != symsec
19003 : !bfd_is_abs_section (symsec)));
19004
8507b6e7 19005 if (operand->root.type == OP_PCREL && !mac)
252b5132 19006 {
3ccad066 19007 const struct mips_pcrel_operand *pcrel_op;
252b5132 19008
3ccad066 19009 pcrel_op = (const struct mips_pcrel_operand *) operand;
252b5132 19010
14f72d45 19011 if (pcrel_op->include_isa_bit && !need_reloc)
252b5132 19012 {
37b2d327
MR
19013 if (!mips_ignore_branch_isa
19014 && !ELF_ST_IS_MIPS16 (S_GET_OTHER (fragp->fr_symbol)))
14f72d45
MR
19015 as_bad_where (fragp->fr_file, fragp->fr_line,
19016 _("branch to a symbol in another ISA mode"));
19017 else if ((fragp->fr_offset & 0x1) != 0)
19018 as_bad_where (fragp->fr_file, fragp->fr_line,
19019 _("branch to misaligned address (0x%lx)"),
52031738
FS
19020 (long) (resolve_symbol_value (fragp->fr_symbol)
19021 + (fragp->fr_offset & ~1)));
252b5132 19022 }
252b5132 19023
14f72d45 19024 val = mips16_pcrel_val (fragp, pcrel_op, val, 0);
252b5132
RH
19025
19026 /* Make sure the section winds up with the alignment we have
19027 assumed. */
3ccad066
RS
19028 if (operand->shift > 0)
19029 record_alignment (asec, operand->shift);
252b5132
RH
19030 }
19031
8507b6e7
MR
19032 if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
19033 || RELAX_MIPS16_DSLOT (fragp->fr_subtype))
19034 {
19035 if (mac)
19036 as_warn_where (fragp->fr_file, fragp->fr_line,
19037 _("macro instruction expanded into multiple "
19038 "instructions in a branch delay slot"));
19039 else if (ext)
19040 as_warn_where (fragp->fr_file, fragp->fr_line,
19041 _("extended instruction in a branch delay slot"));
19042 }
19043 else if (RELAX_MIPS16_NOMACRO (fragp->fr_subtype) && mac)
252b5132 19044 as_warn_where (fragp->fr_file, fragp->fr_line,
8507b6e7
MR
19045 _("macro instruction expanded into multiple "
19046 "instructions"));
252b5132 19047
5c04167a 19048 buf = fragp->fr_literal + fragp->fr_fix;
252b5132 19049
4d68580a 19050 insn = read_compressed_insn (buf, 2);
5c04167a
RS
19051 if (ext)
19052 insn |= MIPS16_EXTEND;
252b5132 19053
5c04167a
RS
19054 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
19055 user_length = 4;
19056 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
19057 user_length = 2;
19058 else
19059 user_length = 0;
19060
8507b6e7 19061 if (mac)
c9775dde 19062 {
8507b6e7
MR
19063 unsigned long reg;
19064 unsigned long new;
19065 unsigned long op;
25499ac7 19066 bfd_boolean e2;
8507b6e7
MR
19067
19068 gas_assert (type == 'A' || type == 'B' || type == 'E');
19069 gas_assert (RELAX_MIPS16_SYM32 (fragp->fr_subtype));
c9775dde 19070
25499ac7
MR
19071 e2 = RELAX_MIPS16_E2 (fragp->fr_subtype);
19072
8507b6e7 19073 if (need_reloc)
c9775dde 19074 {
8507b6e7
MR
19075 fixS *fixp;
19076
19077 gas_assert (!RELAX_MIPS16_PIC (fragp->fr_subtype));
19078
19079 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
19080 fragp->fr_symbol, fragp->fr_offset,
19081 FALSE, BFD_RELOC_MIPS16_HI16_S);
19082 fixp->fx_file = fragp->fr_file;
19083 fixp->fx_line = fragp->fr_line;
19084
25499ac7 19085 fixp = fix_new (fragp, buf - fragp->fr_literal + (e2 ? 4 : 8), 4,
8507b6e7
MR
19086 fragp->fr_symbol, fragp->fr_offset,
19087 FALSE, BFD_RELOC_MIPS16_LO16);
19088 fixp->fx_file = fragp->fr_file;
19089 fixp->fx_line = fragp->fr_line;
19090
19091 val = 0;
19092 }
19093
19094 switch (insn & 0xf800)
19095 {
19096 case 0x0800: /* ADDIU */
19097 reg = (insn >> 8) & 0x7;
19098 op = 0xf0004800 | (reg << 8);
c9775dde 19099 break;
8507b6e7
MR
19100 case 0xb000: /* LW */
19101 reg = (insn >> 8) & 0x7;
19102 op = 0xf0009800 | (reg << 8) | (reg << 5);
c9775dde 19103 break;
8507b6e7
MR
19104 case 0xf800: /* I64 */
19105 reg = (insn >> 5) & 0x7;
19106 switch (insn & 0x0700)
19107 {
19108 case 0x0400: /* LD */
19109 op = 0xf0003800 | (reg << 8) | (reg << 5);
19110 break;
19111 case 0x0600: /* DADDIU */
19112 op = 0xf000fd00 | (reg << 5);
19113 break;
19114 default:
19115 abort ();
19116 }
19117 break;
19118 default:
19119 abort ();
c9775dde 19120 }
8507b6e7 19121
25499ac7 19122 new = (e2 ? 0xf0006820 : 0xf0006800) | (reg << 8); /* LUI/LI */
8507b6e7
MR
19123 new |= mips16_immed_extend ((val + 0x8000) >> 16, 16);
19124 buf = write_compressed_insn (buf, new, 4);
25499ac7
MR
19125 if (!e2)
19126 {
19127 new = 0xf4003000 | (reg << 8) | (reg << 5); /* SLL */
19128 buf = write_compressed_insn (buf, new, 4);
19129 }
8507b6e7
MR
19130 op |= mips16_immed_extend (val, 16);
19131 buf = write_compressed_insn (buf, op, 4);
19132
25499ac7 19133 fragp->fr_fix += e2 ? 8 : 12;
8507b6e7
MR
19134 }
19135 else
19136 {
19137 unsigned int length = ext ? 4 : 2;
19138
19139 if (need_reloc)
c9775dde 19140 {
8507b6e7 19141 bfd_reloc_code_real_type reloc = BFD_RELOC_NONE;
8507b6e7 19142 fixS *fixp;
c9775dde 19143
8507b6e7
MR
19144 switch (type)
19145 {
19146 case 'p':
19147 case 'q':
19148 reloc = BFD_RELOC_MIPS16_16_PCREL_S1;
19149 break;
19150 default:
19151 break;
19152 }
19153 if (mac || reloc == BFD_RELOC_NONE)
19154 as_bad_where (fragp->fr_file, fragp->fr_line,
19155 _("unsupported relocation"));
19156 else if (ext)
19157 {
bbd27b76
MR
19158 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
19159 fragp->fr_symbol, fragp->fr_offset,
19160 TRUE, reloc);
8507b6e7
MR
19161 fixp->fx_file = fragp->fr_file;
19162 fixp->fx_line = fragp->fr_line;
19163 }
19164 else
19165 as_bad_where (fragp->fr_file, fragp->fr_line,
19166 _("invalid unextended operand value"));
c9775dde 19167 }
eefc3365 19168 else
8507b6e7
MR
19169 mips16_immed (fragp->fr_file, fragp->fr_line, type,
19170 BFD_RELOC_UNUSED, val, user_length, &insn);
252b5132 19171
8507b6e7
MR
19172 gas_assert (mips16_opcode_length (insn) == length);
19173 write_compressed_insn (buf, insn, length);
19174 fragp->fr_fix += length;
19175 }
252b5132
RH
19176 }
19177 else
19178 {
df58fc94
RS
19179 relax_substateT subtype = fragp->fr_subtype;
19180 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
19181 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
871a6bd2 19182 unsigned int first, second;
4d7206a2 19183 fixS *fixp;
252b5132 19184
df58fc94
RS
19185 first = RELAX_FIRST (subtype);
19186 second = RELAX_SECOND (subtype);
4d7206a2 19187 fixp = (fixS *) fragp->fr_opcode;
252b5132 19188
df58fc94
RS
19189 /* If the delay slot chosen does not match the size of the instruction,
19190 then emit a warning. */
19191 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
19192 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
19193 {
19194 relax_substateT s;
19195 const char *msg;
19196
19197 s = subtype & (RELAX_DELAY_SLOT_16BIT
19198 | RELAX_DELAY_SLOT_SIZE_FIRST
19199 | RELAX_DELAY_SLOT_SIZE_SECOND);
19200 msg = macro_warning (s);
19201 if (msg != NULL)
db9b2be4 19202 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94
RS
19203 subtype &= ~s;
19204 }
19205
584892a6 19206 /* Possibly emit a warning if we've chosen the longer option. */
df58fc94 19207 if (use_second == second_longer)
584892a6 19208 {
df58fc94
RS
19209 relax_substateT s;
19210 const char *msg;
19211
19212 s = (subtype
19213 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
19214 msg = macro_warning (s);
19215 if (msg != NULL)
db9b2be4 19216 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94 19217 subtype &= ~s;
584892a6
RS
19218 }
19219
4d7206a2
RS
19220 /* Go through all the fixups for the first sequence. Disable them
19221 (by marking them as done) if we're going to use the second
19222 sequence instead. */
19223 while (fixp
19224 && fixp->fx_frag == fragp
90bd3c90 19225 && fixp->fx_where + second < fragp->fr_fix)
4d7206a2 19226 {
df58fc94 19227 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
19228 fixp->fx_done = 1;
19229 fixp = fixp->fx_next;
19230 }
252b5132 19231
4d7206a2
RS
19232 /* Go through the fixups for the second sequence. Disable them if
19233 we're going to use the first sequence, otherwise adjust their
19234 addresses to account for the relaxation. */
19235 while (fixp && fixp->fx_frag == fragp)
19236 {
df58fc94 19237 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
19238 fixp->fx_where -= first;
19239 else
19240 fixp->fx_done = 1;
19241 fixp = fixp->fx_next;
19242 }
19243
19244 /* Now modify the frag contents. */
df58fc94 19245 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
19246 {
19247 char *start;
19248
19249 start = fragp->fr_literal + fragp->fr_fix - first - second;
19250 memmove (start, start + first, second);
19251 fragp->fr_fix -= first;
19252 }
19253 else
19254 fragp->fr_fix -= second;
252b5132
RH
19255 }
19256}
19257
252b5132
RH
19258/* This function is called after the relocs have been generated.
19259 We've been storing mips16 text labels as odd. Here we convert them
19260 back to even for the convenience of the debugger. */
19261
19262void
17a2f251 19263mips_frob_file_after_relocs (void)
252b5132
RH
19264{
19265 asymbol **syms;
19266 unsigned int count, i;
19267
252b5132
RH
19268 syms = bfd_get_outsymbols (stdoutput);
19269 count = bfd_get_symcount (stdoutput);
19270 for (i = 0; i < count; i++, syms++)
df58fc94
RS
19271 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
19272 && ((*syms)->value & 1) != 0)
19273 {
19274 (*syms)->value &= ~1;
19275 /* If the symbol has an odd size, it was probably computed
19276 incorrectly, so adjust that as well. */
19277 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
19278 ++elf_symbol (*syms)->internal_elf_sym.st_size;
19279 }
252b5132
RH
19280}
19281
a1facbec
MR
19282/* This function is called whenever a label is defined, including fake
19283 labels instantiated off the dot special symbol. It is used when
19284 handling branch delays; if a branch has a label, we assume we cannot
19285 move it. This also bumps the value of the symbol by 1 in compressed
19286 code. */
252b5132 19287
e1b47bd5 19288static void
a1facbec 19289mips_record_label (symbolS *sym)
252b5132 19290{
a8dbcb85 19291 segment_info_type *si = seg_info (now_seg);
252b5132
RH
19292 struct insn_label_list *l;
19293
19294 if (free_insn_labels == NULL)
325801bd 19295 l = XNEW (struct insn_label_list);
252b5132
RH
19296 else
19297 {
19298 l = free_insn_labels;
19299 free_insn_labels = l->next;
19300 }
19301
19302 l->label = sym;
a8dbcb85
TS
19303 l->next = si->label_list;
19304 si->label_list = l;
a1facbec 19305}
07a53e5c 19306
a1facbec
MR
19307/* This function is called as tc_frob_label() whenever a label is defined
19308 and adds a DWARF-2 record we only want for true labels. */
19309
19310void
19311mips_define_label (symbolS *sym)
19312{
19313 mips_record_label (sym);
07a53e5c 19314 dwarf2_emit_label (sym);
252b5132 19315}
e1b47bd5
RS
19316
19317/* This function is called by tc_new_dot_label whenever a new dot symbol
19318 is defined. */
19319
19320void
19321mips_add_dot_label (symbolS *sym)
19322{
19323 mips_record_label (sym);
19324 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
19325 mips_compressed_mark_label (sym);
19326}
252b5132 19327\f
351cdf24
MF
19328/* Converting ASE flags from internal to .MIPS.abiflags values. */
19329static unsigned int
19330mips_convert_ase_flags (int ase)
19331{
19332 unsigned int ext_ases = 0;
19333
19334 if (ase & ASE_DSP)
19335 ext_ases |= AFL_ASE_DSP;
19336 if (ase & ASE_DSPR2)
19337 ext_ases |= AFL_ASE_DSPR2;
8f4f9071
MF
19338 if (ase & ASE_DSPR3)
19339 ext_ases |= AFL_ASE_DSPR3;
351cdf24
MF
19340 if (ase & ASE_EVA)
19341 ext_ases |= AFL_ASE_EVA;
19342 if (ase & ASE_MCU)
19343 ext_ases |= AFL_ASE_MCU;
19344 if (ase & ASE_MDMX)
19345 ext_ases |= AFL_ASE_MDMX;
19346 if (ase & ASE_MIPS3D)
19347 ext_ases |= AFL_ASE_MIPS3D;
19348 if (ase & ASE_MT)
19349 ext_ases |= AFL_ASE_MT;
19350 if (ase & ASE_SMARTMIPS)
19351 ext_ases |= AFL_ASE_SMARTMIPS;
19352 if (ase & ASE_VIRT)
19353 ext_ases |= AFL_ASE_VIRT;
19354 if (ase & ASE_MSA)
19355 ext_ases |= AFL_ASE_MSA;
19356 if (ase & ASE_XPA)
19357 ext_ases |= AFL_ASE_XPA;
25499ac7
MR
19358 if (ase & ASE_MIPS16E2)
19359 ext_ases |= file_ase_mips16 ? AFL_ASE_MIPS16E2 : 0;
730c3174
SE
19360 if (ase & ASE_CRC)
19361 ext_ases |= AFL_ASE_CRC;
6f20c942
FS
19362 if (ase & ASE_GINV)
19363 ext_ases |= AFL_ASE_GINV;
8095d2f7
CX
19364 if (ase & ASE_LOONGSON_MMI)
19365 ext_ases |= AFL_ASE_LOONGSON_MMI;
716c08de
CX
19366 if (ase & ASE_LOONGSON_CAM)
19367 ext_ases |= AFL_ASE_LOONGSON_CAM;
bdc6c06e
CX
19368 if (ase & ASE_LOONGSON_EXT)
19369 ext_ases |= AFL_ASE_LOONGSON_EXT;
a693765e
CX
19370 if (ase & ASE_LOONGSON_EXT2)
19371 ext_ases |= AFL_ASE_LOONGSON_EXT2;
351cdf24
MF
19372
19373 return ext_ases;
19374}
252b5132
RH
19375/* Some special processing for a MIPS ELF file. */
19376
19377void
17a2f251 19378mips_elf_final_processing (void)
252b5132 19379{
351cdf24
MF
19380 int fpabi;
19381 Elf_Internal_ABIFlags_v0 flags;
19382
19383 flags.version = 0;
19384 flags.isa_rev = 0;
19385 switch (file_mips_opts.isa)
19386 {
19387 case INSN_ISA1:
19388 flags.isa_level = 1;
19389 break;
19390 case INSN_ISA2:
19391 flags.isa_level = 2;
19392 break;
19393 case INSN_ISA3:
19394 flags.isa_level = 3;
19395 break;
19396 case INSN_ISA4:
19397 flags.isa_level = 4;
19398 break;
19399 case INSN_ISA5:
19400 flags.isa_level = 5;
19401 break;
19402 case INSN_ISA32:
19403 flags.isa_level = 32;
19404 flags.isa_rev = 1;
19405 break;
19406 case INSN_ISA32R2:
19407 flags.isa_level = 32;
19408 flags.isa_rev = 2;
19409 break;
19410 case INSN_ISA32R3:
19411 flags.isa_level = 32;
19412 flags.isa_rev = 3;
19413 break;
19414 case INSN_ISA32R5:
19415 flags.isa_level = 32;
19416 flags.isa_rev = 5;
19417 break;
09c14161
MF
19418 case INSN_ISA32R6:
19419 flags.isa_level = 32;
19420 flags.isa_rev = 6;
19421 break;
351cdf24
MF
19422 case INSN_ISA64:
19423 flags.isa_level = 64;
19424 flags.isa_rev = 1;
19425 break;
19426 case INSN_ISA64R2:
19427 flags.isa_level = 64;
19428 flags.isa_rev = 2;
19429 break;
19430 case INSN_ISA64R3:
19431 flags.isa_level = 64;
19432 flags.isa_rev = 3;
19433 break;
19434 case INSN_ISA64R5:
19435 flags.isa_level = 64;
19436 flags.isa_rev = 5;
19437 break;
09c14161
MF
19438 case INSN_ISA64R6:
19439 flags.isa_level = 64;
19440 flags.isa_rev = 6;
19441 break;
351cdf24
MF
19442 }
19443
19444 flags.gpr_size = file_mips_opts.gp == 32 ? AFL_REG_32 : AFL_REG_64;
19445 flags.cpr1_size = file_mips_opts.soft_float ? AFL_REG_NONE
19446 : (file_mips_opts.ase & ASE_MSA) ? AFL_REG_128
19447 : (file_mips_opts.fp == 64) ? AFL_REG_64
19448 : AFL_REG_32;
19449 flags.cpr2_size = AFL_REG_NONE;
19450 flags.fp_abi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19451 Tag_GNU_MIPS_ABI_FP);
19452 flags.isa_ext = bfd_mips_isa_ext (stdoutput);
19453 flags.ases = mips_convert_ase_flags (file_mips_opts.ase);
19454 if (file_ase_mips16)
19455 flags.ases |= AFL_ASE_MIPS16;
19456 if (file_ase_micromips)
19457 flags.ases |= AFL_ASE_MICROMIPS;
19458 flags.flags1 = 0;
19459 if ((ISA_HAS_ODD_SINGLE_FPR (file_mips_opts.isa, file_mips_opts.arch)
19460 || file_mips_opts.fp == 64)
19461 && file_mips_opts.oddspreg)
19462 flags.flags1 |= AFL_FLAGS1_ODDSPREG;
19463 flags.flags2 = 0;
19464
19465 bfd_mips_elf_swap_abiflags_v0_out (stdoutput, &flags,
19466 ((Elf_External_ABIFlags_v0 *)
19467 mips_flags_frag));
19468
252b5132 19469 /* Write out the register information. */
316f5878 19470 if (mips_abi != N64_ABI)
252b5132
RH
19471 {
19472 Elf32_RegInfo s;
19473
19474 s.ri_gprmask = mips_gprmask;
19475 s.ri_cprmask[0] = mips_cprmask[0];
19476 s.ri_cprmask[1] = mips_cprmask[1];
19477 s.ri_cprmask[2] = mips_cprmask[2];
19478 s.ri_cprmask[3] = mips_cprmask[3];
19479 /* The gp_value field is set by the MIPS ELF backend. */
19480
19481 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
19482 ((Elf32_External_RegInfo *)
19483 mips_regmask_frag));
19484 }
19485 else
19486 {
19487 Elf64_Internal_RegInfo s;
19488
19489 s.ri_gprmask = mips_gprmask;
19490 s.ri_pad = 0;
19491 s.ri_cprmask[0] = mips_cprmask[0];
19492 s.ri_cprmask[1] = mips_cprmask[1];
19493 s.ri_cprmask[2] = mips_cprmask[2];
19494 s.ri_cprmask[3] = mips_cprmask[3];
19495 /* The gp_value field is set by the MIPS ELF backend. */
19496
19497 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
19498 ((Elf64_External_RegInfo *)
19499 mips_regmask_frag));
19500 }
19501
19502 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
19503 sort of BFD interface for this. */
19504 if (mips_any_noreorder)
19505 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
19506 if (mips_pic != NO_PIC)
143d77c5 19507 {
8b828383 19508 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
19509 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
19510 }
19511 if (mips_abicalls)
19512 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 19513
b015e599
AP
19514 /* Set MIPS ELF flags for ASEs. Note that not all ASEs have flags
19515 defined at present; this might need to change in future. */
a4672219
TS
19516 if (file_ase_mips16)
19517 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
df58fc94
RS
19518 if (file_ase_micromips)
19519 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
919731af 19520 if (file_mips_opts.ase & ASE_MDMX)
deec1734 19521 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 19522
bdaaa2e1 19523 /* Set the MIPS ELF ABI flags. */
316f5878 19524 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 19525 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 19526 else if (mips_abi == O64_ABI)
252b5132 19527 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 19528 else if (mips_abi == EABI_ABI)
252b5132 19529 {
bad1aba3 19530 if (file_mips_opts.gp == 64)
252b5132
RH
19531 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
19532 else
19533 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
19534 }
be00bddd 19535
defc8e2b 19536 /* Nothing to do for N32_ABI or N64_ABI. */
252b5132
RH
19537
19538 if (mips_32bitmode)
19539 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
ad3fea08 19540
7361da2c 19541 if (mips_nan2008 == 1)
ba92f887
MR
19542 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NAN2008;
19543
ad3fea08 19544 /* 32 bit code with 64 bit FP registers. */
351cdf24
MF
19545 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19546 Tag_GNU_MIPS_ABI_FP);
19547 if (fpabi == Val_GNU_MIPS_ABI_FP_OLD_64)
f1c38003 19548 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_FP64;
252b5132 19549}
252b5132 19550\f
beae10d5 19551typedef struct proc {
9b2f1d35
EC
19552 symbolS *func_sym;
19553 symbolS *func_end_sym;
beae10d5
KH
19554 unsigned long reg_mask;
19555 unsigned long reg_offset;
19556 unsigned long fpreg_mask;
19557 unsigned long fpreg_offset;
19558 unsigned long frame_offset;
19559 unsigned long frame_reg;
19560 unsigned long pc_reg;
19561} procS;
252b5132
RH
19562
19563static procS cur_proc;
19564static procS *cur_proc_ptr;
19565static int numprocs;
19566
df58fc94
RS
19567/* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
19568 as "2", and a normal nop as "0". */
19569
19570#define NOP_OPCODE_MIPS 0
19571#define NOP_OPCODE_MIPS16 1
19572#define NOP_OPCODE_MICROMIPS 2
742a56fe
RS
19573
19574char
19575mips_nop_opcode (void)
19576{
df58fc94
RS
19577 if (seg_info (now_seg)->tc_segment_info_data.micromips)
19578 return NOP_OPCODE_MICROMIPS;
19579 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
19580 return NOP_OPCODE_MIPS16;
19581 else
19582 return NOP_OPCODE_MIPS;
742a56fe
RS
19583}
19584
df58fc94
RS
19585/* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
19586 32-bit microMIPS NOPs here (if applicable). */
a19d8eb0 19587
0a9ef439 19588void
17a2f251 19589mips_handle_align (fragS *fragp)
a19d8eb0 19590{
df58fc94 19591 char nop_opcode;
742a56fe 19592 char *p;
c67a084a
NC
19593 int bytes, size, excess;
19594 valueT opcode;
742a56fe 19595
0a9ef439
RH
19596 if (fragp->fr_type != rs_align_code)
19597 return;
19598
742a56fe 19599 p = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
19600 nop_opcode = *p;
19601 switch (nop_opcode)
a19d8eb0 19602 {
df58fc94
RS
19603 case NOP_OPCODE_MICROMIPS:
19604 opcode = micromips_nop32_insn.insn_opcode;
19605 size = 4;
19606 break;
19607 case NOP_OPCODE_MIPS16:
c67a084a
NC
19608 opcode = mips16_nop_insn.insn_opcode;
19609 size = 2;
df58fc94
RS
19610 break;
19611 case NOP_OPCODE_MIPS:
19612 default:
c67a084a
NC
19613 opcode = nop_insn.insn_opcode;
19614 size = 4;
df58fc94 19615 break;
c67a084a 19616 }
a19d8eb0 19617
c67a084a
NC
19618 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
19619 excess = bytes % size;
df58fc94
RS
19620
19621 /* Handle the leading part if we're not inserting a whole number of
19622 instructions, and make it the end of the fixed part of the frag.
19623 Try to fit in a short microMIPS NOP if applicable and possible,
19624 and use zeroes otherwise. */
19625 gas_assert (excess < 4);
19626 fragp->fr_fix += excess;
19627 switch (excess)
c67a084a 19628 {
df58fc94
RS
19629 case 3:
19630 *p++ = '\0';
19631 /* Fall through. */
19632 case 2:
833794fc 19633 if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
df58fc94 19634 {
4d68580a 19635 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
df58fc94
RS
19636 break;
19637 }
19638 *p++ = '\0';
19639 /* Fall through. */
19640 case 1:
19641 *p++ = '\0';
19642 /* Fall through. */
19643 case 0:
19644 break;
a19d8eb0 19645 }
c67a084a
NC
19646
19647 md_number_to_chars (p, opcode, size);
19648 fragp->fr_var = size;
a19d8eb0
CP
19649}
19650
252b5132 19651static long
17a2f251 19652get_number (void)
252b5132
RH
19653{
19654 int negative = 0;
19655 long val = 0;
19656
19657 if (*input_line_pointer == '-')
19658 {
19659 ++input_line_pointer;
19660 negative = 1;
19661 }
3882b010 19662 if (!ISDIGIT (*input_line_pointer))
956cd1d6 19663 as_bad (_("expected simple number"));
252b5132
RH
19664 if (input_line_pointer[0] == '0')
19665 {
19666 if (input_line_pointer[1] == 'x')
19667 {
19668 input_line_pointer += 2;
3882b010 19669 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
19670 {
19671 val <<= 4;
19672 val |= hex_value (*input_line_pointer++);
19673 }
19674 return negative ? -val : val;
19675 }
19676 else
19677 {
19678 ++input_line_pointer;
3882b010 19679 while (ISDIGIT (*input_line_pointer))
252b5132
RH
19680 {
19681 val <<= 3;
19682 val |= *input_line_pointer++ - '0';
19683 }
19684 return negative ? -val : val;
19685 }
19686 }
3882b010 19687 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
19688 {
19689 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
19690 *input_line_pointer, *input_line_pointer);
956cd1d6 19691 as_warn (_("invalid number"));
252b5132
RH
19692 return -1;
19693 }
3882b010 19694 while (ISDIGIT (*input_line_pointer))
252b5132
RH
19695 {
19696 val *= 10;
19697 val += *input_line_pointer++ - '0';
19698 }
19699 return negative ? -val : val;
19700}
19701
19702/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
19703 is an initial number which is the ECOFF file index. In the non-ECOFF
19704 case .file implies DWARF-2. */
19705
19706static void
17a2f251 19707s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 19708{
ecb4347a
DJ
19709 static int first_file_directive = 0;
19710
c5dd6aab
DJ
19711 if (ECOFF_DEBUGGING)
19712 {
19713 get_number ();
19714 s_app_file (0);
19715 }
19716 else
ecb4347a
DJ
19717 {
19718 char *filename;
19719
68d20676 19720 filename = dwarf2_directive_filename ();
ecb4347a
DJ
19721
19722 /* Versions of GCC up to 3.1 start files with a ".file"
19723 directive even for stabs output. Make sure that this
19724 ".file" is handled. Note that you need a version of GCC
19725 after 3.1 in order to support DWARF-2 on MIPS. */
19726 if (filename != NULL && ! first_file_directive)
19727 {
19728 (void) new_logical_line (filename, -1);
c04f5787 19729 s_app_file_string (filename, 0);
ecb4347a
DJ
19730 }
19731 first_file_directive = 1;
19732 }
c5dd6aab
DJ
19733}
19734
19735/* The .loc directive, implying DWARF-2. */
252b5132
RH
19736
19737static void
17a2f251 19738s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 19739{
c5dd6aab
DJ
19740 if (!ECOFF_DEBUGGING)
19741 dwarf2_directive_loc (0);
252b5132
RH
19742}
19743
252b5132
RH
19744/* The .end directive. */
19745
19746static void
17a2f251 19747s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
19748{
19749 symbolS *p;
252b5132 19750
7a621144
DJ
19751 /* Following functions need their own .frame and .cprestore directives. */
19752 mips_frame_reg_valid = 0;
19753 mips_cprestore_valid = 0;
19754
252b5132
RH
19755 if (!is_end_of_line[(unsigned char) *input_line_pointer])
19756 {
19757 p = get_symbol ();
19758 demand_empty_rest_of_line ();
19759 }
19760 else
19761 p = NULL;
19762
14949570 19763 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
19764 as_warn (_(".end not in text section"));
19765
19766 if (!cur_proc_ptr)
19767 {
1661c76c 19768 as_warn (_(".end directive without a preceding .ent directive"));
252b5132
RH
19769 demand_empty_rest_of_line ();
19770 return;
19771 }
19772
19773 if (p != NULL)
19774 {
9c2799c2 19775 gas_assert (S_GET_NAME (p));
9b2f1d35 19776 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
1661c76c 19777 as_warn (_(".end symbol does not match .ent symbol"));
ecb4347a
DJ
19778
19779 if (debug_type == DEBUG_STABS)
19780 stabs_generate_asm_endfunc (S_GET_NAME (p),
19781 S_GET_NAME (p));
252b5132
RH
19782 }
19783 else
19784 as_warn (_(".end directive missing or unknown symbol"));
19785
9b2f1d35
EC
19786 /* Create an expression to calculate the size of the function. */
19787 if (p && cur_proc_ptr)
19788 {
19789 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
325801bd 19790 expressionS *exp = XNEW (expressionS);
9b2f1d35
EC
19791
19792 obj->size = exp;
19793 exp->X_op = O_subtract;
19794 exp->X_add_symbol = symbol_temp_new_now ();
19795 exp->X_op_symbol = p;
19796 exp->X_add_number = 0;
19797
19798 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
19799 }
19800
5ff6a06c
MR
19801#ifdef md_flush_pending_output
19802 md_flush_pending_output ();
19803#endif
19804
ecb4347a 19805 /* Generate a .pdr section. */
f3ded42a 19806 if (!ECOFF_DEBUGGING && mips_flag_pdr)
ecb4347a
DJ
19807 {
19808 segT saved_seg = now_seg;
19809 subsegT saved_subseg = now_subseg;
ecb4347a
DJ
19810 expressionS exp;
19811 char *fragp;
252b5132 19812
9c2799c2 19813 gas_assert (pdr_seg);
ecb4347a 19814 subseg_set (pdr_seg, 0);
252b5132 19815
ecb4347a
DJ
19816 /* Write the symbol. */
19817 exp.X_op = O_symbol;
19818 exp.X_add_symbol = p;
19819 exp.X_add_number = 0;
19820 emit_expr (&exp, 4);
252b5132 19821
ecb4347a 19822 fragp = frag_more (7 * 4);
252b5132 19823
17a2f251
TS
19824 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19825 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19826 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19827 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19828 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19829 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19830 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 19831
ecb4347a
DJ
19832 subseg_set (saved_seg, saved_subseg);
19833 }
252b5132
RH
19834
19835 cur_proc_ptr = NULL;
19836}
19837
19838/* The .aent and .ent directives. */
19839
19840static void
17a2f251 19841s_mips_ent (int aent)
252b5132 19842{
252b5132 19843 symbolS *symbolP;
252b5132
RH
19844
19845 symbolP = get_symbol ();
19846 if (*input_line_pointer == ',')
f9419b05 19847 ++input_line_pointer;
252b5132 19848 SKIP_WHITESPACE ();
3882b010 19849 if (ISDIGIT (*input_line_pointer)
d9a62219 19850 || *input_line_pointer == '-')
874e8986 19851 get_number ();
252b5132 19852
14949570 19853 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
1661c76c 19854 as_warn (_(".ent or .aent not in text section"));
252b5132
RH
19855
19856 if (!aent && cur_proc_ptr)
9a41af64 19857 as_warn (_("missing .end"));
252b5132
RH
19858
19859 if (!aent)
19860 {
7a621144
DJ
19861 /* This function needs its own .frame and .cprestore directives. */
19862 mips_frame_reg_valid = 0;
19863 mips_cprestore_valid = 0;
19864
252b5132
RH
19865 cur_proc_ptr = &cur_proc;
19866 memset (cur_proc_ptr, '\0', sizeof (procS));
19867
9b2f1d35 19868 cur_proc_ptr->func_sym = symbolP;
252b5132 19869
f9419b05 19870 ++numprocs;
ecb4347a
DJ
19871
19872 if (debug_type == DEBUG_STABS)
19873 stabs_generate_asm_func (S_GET_NAME (symbolP),
19874 S_GET_NAME (symbolP));
252b5132
RH
19875 }
19876
7c0fc524
MR
19877 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19878
252b5132
RH
19879 demand_empty_rest_of_line ();
19880}
19881
19882/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 19883 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 19884 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 19885 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
19886 symbol table (in the mdebug section). */
19887
19888static void
17a2f251 19889s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 19890{
f3ded42a
RS
19891 if (ECOFF_DEBUGGING)
19892 s_ignore (ignore);
19893 else
ecb4347a
DJ
19894 {
19895 long val;
252b5132 19896
ecb4347a
DJ
19897 if (cur_proc_ptr == (procS *) NULL)
19898 {
19899 as_warn (_(".frame outside of .ent"));
19900 demand_empty_rest_of_line ();
19901 return;
19902 }
252b5132 19903
ecb4347a
DJ
19904 cur_proc_ptr->frame_reg = tc_get_register (1);
19905
19906 SKIP_WHITESPACE ();
19907 if (*input_line_pointer++ != ','
19908 || get_absolute_expression_and_terminator (&val) != ',')
19909 {
1661c76c 19910 as_warn (_("bad .frame directive"));
ecb4347a
DJ
19911 --input_line_pointer;
19912 demand_empty_rest_of_line ();
19913 return;
19914 }
252b5132 19915
ecb4347a
DJ
19916 cur_proc_ptr->frame_offset = val;
19917 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 19918
252b5132 19919 demand_empty_rest_of_line ();
252b5132 19920 }
252b5132
RH
19921}
19922
bdaaa2e1
KH
19923/* The .fmask and .mask directives. If the mdebug section is present
19924 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 19925 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 19926 information correctly. We can't use the ecoff routines because they
252b5132
RH
19927 make reference to the ecoff symbol table (in the mdebug section). */
19928
19929static void
17a2f251 19930s_mips_mask (int reg_type)
252b5132 19931{
f3ded42a
RS
19932 if (ECOFF_DEBUGGING)
19933 s_ignore (reg_type);
19934 else
252b5132 19935 {
ecb4347a 19936 long mask, off;
252b5132 19937
ecb4347a
DJ
19938 if (cur_proc_ptr == (procS *) NULL)
19939 {
19940 as_warn (_(".mask/.fmask outside of .ent"));
19941 demand_empty_rest_of_line ();
19942 return;
19943 }
252b5132 19944
ecb4347a
DJ
19945 if (get_absolute_expression_and_terminator (&mask) != ',')
19946 {
1661c76c 19947 as_warn (_("bad .mask/.fmask directive"));
ecb4347a
DJ
19948 --input_line_pointer;
19949 demand_empty_rest_of_line ();
19950 return;
19951 }
252b5132 19952
ecb4347a
DJ
19953 off = get_absolute_expression ();
19954
19955 if (reg_type == 'F')
19956 {
19957 cur_proc_ptr->fpreg_mask = mask;
19958 cur_proc_ptr->fpreg_offset = off;
19959 }
19960 else
19961 {
19962 cur_proc_ptr->reg_mask = mask;
19963 cur_proc_ptr->reg_offset = off;
19964 }
19965
19966 demand_empty_rest_of_line ();
252b5132 19967 }
252b5132
RH
19968}
19969
316f5878
RS
19970/* A table describing all the processors gas knows about. Names are
19971 matched in the order listed.
e7af610e 19972
316f5878
RS
19973 To ease comparison, please keep this table in the same order as
19974 gcc's mips_cpu_info_table[]. */
e972090a
NC
19975static const struct mips_cpu_info mips_cpu_info_table[] =
19976{
6f2117ba 19977 /* Entries for generic ISAs. */
d16afab6
RS
19978 { "mips1", MIPS_CPU_IS_ISA, 0, ISA_MIPS1, CPU_R3000 },
19979 { "mips2", MIPS_CPU_IS_ISA, 0, ISA_MIPS2, CPU_R6000 },
19980 { "mips3", MIPS_CPU_IS_ISA, 0, ISA_MIPS3, CPU_R4000 },
19981 { "mips4", MIPS_CPU_IS_ISA, 0, ISA_MIPS4, CPU_R8000 },
19982 { "mips5", MIPS_CPU_IS_ISA, 0, ISA_MIPS5, CPU_MIPS5 },
19983 { "mips32", MIPS_CPU_IS_ISA, 0, ISA_MIPS32, CPU_MIPS32 },
19984 { "mips32r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
ae52f483
AB
19985 { "mips32r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R3, CPU_MIPS32R3 },
19986 { "mips32r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R5, CPU_MIPS32R5 },
7361da2c 19987 { "mips32r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R6, CPU_MIPS32R6 },
d16afab6
RS
19988 { "mips64", MIPS_CPU_IS_ISA, 0, ISA_MIPS64, CPU_MIPS64 },
19989 { "mips64r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R2, CPU_MIPS64R2 },
ae52f483
AB
19990 { "mips64r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R3, CPU_MIPS64R3 },
19991 { "mips64r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R5, CPU_MIPS64R5 },
7361da2c 19992 { "mips64r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R6, CPU_MIPS64R6 },
316f5878
RS
19993
19994 /* MIPS I */
d16afab6
RS
19995 { "r3000", 0, 0, ISA_MIPS1, CPU_R3000 },
19996 { "r2000", 0, 0, ISA_MIPS1, CPU_R3000 },
19997 { "r3900", 0, 0, ISA_MIPS1, CPU_R3900 },
316f5878
RS
19998
19999 /* MIPS II */
d16afab6 20000 { "r6000", 0, 0, ISA_MIPS2, CPU_R6000 },
316f5878
RS
20001
20002 /* MIPS III */
d16afab6
RS
20003 { "r4000", 0, 0, ISA_MIPS3, CPU_R4000 },
20004 { "r4010", 0, 0, ISA_MIPS2, CPU_R4010 },
20005 { "vr4100", 0, 0, ISA_MIPS3, CPU_VR4100 },
20006 { "vr4111", 0, 0, ISA_MIPS3, CPU_R4111 },
20007 { "vr4120", 0, 0, ISA_MIPS3, CPU_VR4120 },
20008 { "vr4130", 0, 0, ISA_MIPS3, CPU_VR4120 },
20009 { "vr4181", 0, 0, ISA_MIPS3, CPU_R4111 },
20010 { "vr4300", 0, 0, ISA_MIPS3, CPU_R4300 },
20011 { "r4400", 0, 0, ISA_MIPS3, CPU_R4400 },
20012 { "r4600", 0, 0, ISA_MIPS3, CPU_R4600 },
20013 { "orion", 0, 0, ISA_MIPS3, CPU_R4600 },
20014 { "r4650", 0, 0, ISA_MIPS3, CPU_R4650 },
20015 { "r5900", 0, 0, ISA_MIPS3, CPU_R5900 },
6f2117ba 20016 /* ST Microelectronics Loongson 2E and 2F cores. */
d16afab6 20017 { "loongson2e", 0, 0, ISA_MIPS3, CPU_LOONGSON_2E },
8095d2f7 20018 { "loongson2f", 0, ASE_LOONGSON_MMI, ISA_MIPS3, CPU_LOONGSON_2F },
316f5878
RS
20019
20020 /* MIPS IV */
d16afab6
RS
20021 { "r8000", 0, 0, ISA_MIPS4, CPU_R8000 },
20022 { "r10000", 0, 0, ISA_MIPS4, CPU_R10000 },
20023 { "r12000", 0, 0, ISA_MIPS4, CPU_R12000 },
20024 { "r14000", 0, 0, ISA_MIPS4, CPU_R14000 },
20025 { "r16000", 0, 0, ISA_MIPS4, CPU_R16000 },
20026 { "vr5000", 0, 0, ISA_MIPS4, CPU_R5000 },
20027 { "vr5400", 0, 0, ISA_MIPS4, CPU_VR5400 },
20028 { "vr5500", 0, 0, ISA_MIPS4, CPU_VR5500 },
20029 { "rm5200", 0, 0, ISA_MIPS4, CPU_R5000 },
20030 { "rm5230", 0, 0, ISA_MIPS4, CPU_R5000 },
20031 { "rm5231", 0, 0, ISA_MIPS4, CPU_R5000 },
20032 { "rm5261", 0, 0, ISA_MIPS4, CPU_R5000 },
20033 { "rm5721", 0, 0, ISA_MIPS4, CPU_R5000 },
20034 { "rm7000", 0, 0, ISA_MIPS4, CPU_RM7000 },
20035 { "rm9000", 0, 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
20036
20037 /* MIPS 32 */
d16afab6
RS
20038 { "4kc", 0, 0, ISA_MIPS32, CPU_MIPS32 },
20039 { "4km", 0, 0, ISA_MIPS32, CPU_MIPS32 },
20040 { "4kp", 0, 0, ISA_MIPS32, CPU_MIPS32 },
20041 { "4ksc", 0, ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
ad3fea08
TS
20042
20043 /* MIPS 32 Release 2 */
d16afab6
RS
20044 { "4kec", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20045 { "4kem", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20046 { "4kep", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20047 { "4ksd", 0, ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
20048 { "m4k", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20049 { "m4kp", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20050 { "m14k", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
20051 { "m14kc", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
20052 { "m14ke", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
20053 ISA_MIPS32R2, CPU_MIPS32R2 },
20054 { "m14kec", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
20055 ISA_MIPS32R2, CPU_MIPS32R2 },
20056 { "24kc", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20057 { "24kf2_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20058 { "24kf", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20059 { "24kf1_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 20060 /* Deprecated forms of the above. */
d16afab6
RS
20061 { "24kfx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20062 { "24kx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 20063 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
d16afab6
RS
20064 { "24kec", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
20065 { "24kef2_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
20066 { "24kef", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
20067 { "24kef1_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 20068 /* Deprecated forms of the above. */
d16afab6
RS
20069 { "24kefx", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
20070 { "24kex", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 20071 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
d16afab6
RS
20072 { "34kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20073 { "34kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20074 { "34kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20075 { "34kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 20076 /* Deprecated forms of the above. */
d16afab6
RS
20077 { "34kfx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20078 { "34kx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
711eefe4 20079 /* 34Kn is a 34kc without DSP. */
d16afab6 20080 { "34kn", 0, ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 20081 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
d16afab6
RS
20082 { "74kc", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20083 { "74kf2_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20084 { "74kf", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20085 { "74kf1_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20086 { "74kf3_2", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 20087 /* Deprecated forms of the above. */
d16afab6
RS
20088 { "74kfx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20089 { "74kx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
30f8113a 20090 /* 1004K cores are multiprocessor versions of the 34K. */
d16afab6
RS
20091 { "1004kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20092 { "1004kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20093 { "1004kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20094 { "1004kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
6f2117ba 20095 /* interaptiv is the new name for 1004kf. */
77403ce9 20096 { "interaptiv", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
38bf472a
MR
20097 { "interaptiv-mr2", 0,
20098 ASE_DSP | ASE_EVA | ASE_MT | ASE_MIPS16E2 | ASE_MIPS16E2_MT,
20099 ISA_MIPS32R3, CPU_INTERAPTIV_MR2 },
6f2117ba 20100 /* M5100 family. */
c6e5c03a
RS
20101 { "m5100", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
20102 { "m5101", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
bbaa46c0 20103 /* P5600 with EVA and Virtualization ASEs, other ASEs are optional. */
134c0c8b 20104 { "p5600", 0, ASE_VIRT | ASE_EVA | ASE_XPA, ISA_MIPS32R5, CPU_MIPS32R5 },
32b26a03 20105
316f5878 20106 /* MIPS 64 */
d16afab6
RS
20107 { "5kc", 0, 0, ISA_MIPS64, CPU_MIPS64 },
20108 { "5kf", 0, 0, ISA_MIPS64, CPU_MIPS64 },
20109 { "20kc", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
20110 { "25kf", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
ad3fea08 20111
6f2117ba 20112 /* Broadcom SB-1 CPU core. */
d16afab6 20113 { "sb1", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
6f2117ba 20114 /* Broadcom SB-1A CPU core. */
d16afab6 20115 { "sb1a", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
3739860c 20116
6f2117ba
PH
20117 /* MIPS 64 Release 2. */
20118 /* Loongson CPU core. */
20119 /* -march=loongson3a is an alias of -march=gs464 for compatibility. */
bdc6c06e 20120 { "loongson3a", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT,
ac8cb70f
CX
20121 ISA_MIPS64R2, CPU_GS464 },
20122 { "gs464", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT,
20123 ISA_MIPS64R2, CPU_GS464 },
bd782c07
CX
20124 { "gs464e", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT
20125 | ASE_LOONGSON_EXT2, ISA_MIPS64R2, CPU_GS464E },
9108bc33
CX
20126 { "gs264e", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT
20127 | ASE_LOONGSON_EXT2 | ASE_MSA | ASE_MSA64, ISA_MIPS64R2, CPU_GS264E },
ed163775 20128
6f2117ba 20129 /* Cavium Networks Octeon CPU core. */
d16afab6
RS
20130 { "octeon", 0, 0, ISA_MIPS64R2, CPU_OCTEON },
20131 { "octeon+", 0, 0, ISA_MIPS64R2, CPU_OCTEONP },
20132 { "octeon2", 0, 0, ISA_MIPS64R2, CPU_OCTEON2 },
2c629856 20133 { "octeon3", 0, ASE_VIRT | ASE_VIRT64, ISA_MIPS64R5, CPU_OCTEON3 },
967344c6 20134
52b6b6b9 20135 /* RMI Xlr */
d16afab6 20136 { "xlr", 0, 0, ISA_MIPS64, CPU_XLR },
52b6b6b9 20137
55a36193
MK
20138 /* Broadcom XLP.
20139 XLP is mostly like XLR, with the prominent exception that it is
20140 MIPS64R2 rather than MIPS64. */
d16afab6 20141 { "xlp", 0, 0, ISA_MIPS64R2, CPU_XLR },
55a36193 20142
6f2117ba 20143 /* MIPS 64 Release 6. */
bdc8beb4
MF
20144 { "i6400", 0, ASE_VIRT | ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
20145 { "i6500", 0, ASE_VIRT | ASE_MSA | ASE_CRC | ASE_GINV,
20146 ISA_MIPS64R6, CPU_MIPS64R6},
a4968f42 20147 { "p6600", 0, ASE_VIRT | ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
7ef0d297 20148
6f2117ba 20149 /* End marker. */
d16afab6 20150 { NULL, 0, 0, 0, 0 }
316f5878 20151};
e7af610e 20152
84ea6cf2 20153
316f5878
RS
20154/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
20155 with a final "000" replaced by "k". Ignore case.
e7af610e 20156
316f5878 20157 Note: this function is shared between GCC and GAS. */
c6c98b38 20158
b34976b6 20159static bfd_boolean
17a2f251 20160mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
20161{
20162 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
20163 given++, canonical++;
20164
20165 return ((*given == 0 && *canonical == 0)
20166 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
20167}
20168
20169
20170/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
20171 CPU name. We've traditionally allowed a lot of variation here.
20172
20173 Note: this function is shared between GCC and GAS. */
20174
b34976b6 20175static bfd_boolean
17a2f251 20176mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
20177{
20178 /* First see if the name matches exactly, or with a final "000"
20179 turned into "k". */
20180 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 20181 return TRUE;
316f5878
RS
20182
20183 /* If not, try comparing based on numerical designation alone.
20184 See if GIVEN is an unadorned number, or 'r' followed by a number. */
20185 if (TOLOWER (*given) == 'r')
20186 given++;
20187 if (!ISDIGIT (*given))
b34976b6 20188 return FALSE;
316f5878
RS
20189
20190 /* Skip over some well-known prefixes in the canonical name,
20191 hoping to find a number there too. */
20192 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
20193 canonical += 2;
20194 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
20195 canonical += 2;
20196 else if (TOLOWER (canonical[0]) == 'r')
20197 canonical += 1;
20198
20199 return mips_strict_matching_cpu_name_p (canonical, given);
20200}
20201
20202
20203/* Parse an option that takes the name of a processor as its argument.
20204 OPTION is the name of the option and CPU_STRING is the argument.
20205 Return the corresponding processor enumeration if the CPU_STRING is
20206 recognized, otherwise report an error and return null.
20207
20208 A similar function exists in GCC. */
e7af610e
NC
20209
20210static const struct mips_cpu_info *
17a2f251 20211mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 20212{
316f5878 20213 const struct mips_cpu_info *p;
e7af610e 20214
316f5878
RS
20215 /* 'from-abi' selects the most compatible architecture for the given
20216 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
20217 EABIs, we have to decide whether we're using the 32-bit or 64-bit
20218 version. Look first at the -mgp options, if given, otherwise base
20219 the choice on MIPS_DEFAULT_64BIT.
e7af610e 20220
316f5878
RS
20221 Treat NO_ABI like the EABIs. One reason to do this is that the
20222 plain 'mips' and 'mips64' configs have 'from-abi' as their default
20223 architecture. This code picks MIPS I for 'mips' and MIPS III for
20224 'mips64', just as we did in the days before 'from-abi'. */
20225 if (strcasecmp (cpu_string, "from-abi") == 0)
20226 {
20227 if (ABI_NEEDS_32BIT_REGS (mips_abi))
20228 return mips_cpu_info_from_isa (ISA_MIPS1);
20229
20230 if (ABI_NEEDS_64BIT_REGS (mips_abi))
20231 return mips_cpu_info_from_isa (ISA_MIPS3);
20232
bad1aba3 20233 if (file_mips_opts.gp >= 0)
20234 return mips_cpu_info_from_isa (file_mips_opts.gp == 32
0b35dfee 20235 ? ISA_MIPS1 : ISA_MIPS3);
316f5878
RS
20236
20237 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
20238 ? ISA_MIPS3
20239 : ISA_MIPS1);
20240 }
20241
20242 /* 'default' has traditionally been a no-op. Probably not very useful. */
20243 if (strcasecmp (cpu_string, "default") == 0)
20244 return 0;
20245
20246 for (p = mips_cpu_info_table; p->name != 0; p++)
20247 if (mips_matching_cpu_name_p (p->name, cpu_string))
20248 return p;
20249
1661c76c 20250 as_bad (_("bad value (%s) for %s"), cpu_string, option);
316f5878 20251 return 0;
e7af610e
NC
20252}
20253
316f5878
RS
20254/* Return the canonical processor information for ISA (a member of the
20255 ISA_MIPS* enumeration). */
20256
e7af610e 20257static const struct mips_cpu_info *
17a2f251 20258mips_cpu_info_from_isa (int isa)
e7af610e
NC
20259{
20260 int i;
20261
20262 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
ad3fea08 20263 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
316f5878 20264 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
20265 return (&mips_cpu_info_table[i]);
20266
e972090a 20267 return NULL;
e7af610e 20268}
fef14a42
TS
20269
20270static const struct mips_cpu_info *
17a2f251 20271mips_cpu_info_from_arch (int arch)
fef14a42
TS
20272{
20273 int i;
20274
20275 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
20276 if (arch == mips_cpu_info_table[i].cpu)
20277 return (&mips_cpu_info_table[i]);
20278
20279 return NULL;
20280}
316f5878
RS
20281\f
20282static void
17a2f251 20283show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
20284{
20285 if (*first_p)
20286 {
20287 fprintf (stream, "%24s", "");
20288 *col_p = 24;
20289 }
20290 else
20291 {
20292 fprintf (stream, ", ");
20293 *col_p += 2;
20294 }
e7af610e 20295
316f5878
RS
20296 if (*col_p + strlen (string) > 72)
20297 {
20298 fprintf (stream, "\n%24s", "");
20299 *col_p = 24;
20300 }
20301
20302 fprintf (stream, "%s", string);
20303 *col_p += strlen (string);
20304
20305 *first_p = 0;
20306}
20307
20308void
17a2f251 20309md_show_usage (FILE *stream)
e7af610e 20310{
316f5878
RS
20311 int column, first;
20312 size_t i;
20313
20314 fprintf (stream, _("\
20315MIPS options:\n\
316f5878
RS
20316-EB generate big endian output\n\
20317-EL generate little endian output\n\
20318-g, -g2 do not remove unneeded NOPs or swap branches\n\
20319-G NUM allow referencing objects up to NUM bytes\n\
20320 implicitly with the gp register [default 8]\n"));
20321 fprintf (stream, _("\
20322-mips1 generate MIPS ISA I instructions\n\
20323-mips2 generate MIPS ISA II instructions\n\
20324-mips3 generate MIPS ISA III instructions\n\
20325-mips4 generate MIPS ISA IV instructions\n\
20326-mips5 generate MIPS ISA V instructions\n\
20327-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 20328-mips32r2 generate MIPS32 release 2 ISA instructions\n\
ae52f483
AB
20329-mips32r3 generate MIPS32 release 3 ISA instructions\n\
20330-mips32r5 generate MIPS32 release 5 ISA instructions\n\
7361da2c 20331-mips32r6 generate MIPS32 release 6 ISA instructions\n\
316f5878 20332-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 20333-mips64r2 generate MIPS64 release 2 ISA instructions\n\
ae52f483
AB
20334-mips64r3 generate MIPS64 release 3 ISA instructions\n\
20335-mips64r5 generate MIPS64 release 5 ISA instructions\n\
7361da2c 20336-mips64r6 generate MIPS64 release 6 ISA instructions\n\
316f5878
RS
20337-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
20338
20339 first = 1;
e7af610e
NC
20340
20341 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
20342 show (stream, mips_cpu_info_table[i].name, &column, &first);
20343 show (stream, "from-abi", &column, &first);
20344 fputc ('\n', stream);
e7af610e 20345
316f5878
RS
20346 fprintf (stream, _("\
20347-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
20348-no-mCPU don't generate code specific to CPU.\n\
20349 For -mCPU and -no-mCPU, CPU must be one of:\n"));
20350
20351 first = 1;
20352
20353 show (stream, "3900", &column, &first);
20354 show (stream, "4010", &column, &first);
20355 show (stream, "4100", &column, &first);
20356 show (stream, "4650", &column, &first);
20357 fputc ('\n', stream);
20358
20359 fprintf (stream, _("\
20360-mips16 generate mips16 instructions\n\
20361-no-mips16 do not generate mips16 instructions\n"));
20362 fprintf (stream, _("\
f866b262
MR
20363-mmips16e2 generate MIPS16e2 instructions\n\
20364-mno-mips16e2 do not generate MIPS16e2 instructions\n"));
20365 fprintf (stream, _("\
df58fc94
RS
20366-mmicromips generate microMIPS instructions\n\
20367-mno-micromips do not generate microMIPS instructions\n"));
20368 fprintf (stream, _("\
e16bfa71 20369-msmartmips generate smartmips instructions\n\
3739860c 20370-mno-smartmips do not generate smartmips instructions\n"));
e16bfa71 20371 fprintf (stream, _("\
74cd071d
CF
20372-mdsp generate DSP instructions\n\
20373-mno-dsp do not generate DSP instructions\n"));
20374 fprintf (stream, _("\
8b082fb1
TS
20375-mdspr2 generate DSP R2 instructions\n\
20376-mno-dspr2 do not generate DSP R2 instructions\n"));
20377 fprintf (stream, _("\
8f4f9071
MF
20378-mdspr3 generate DSP R3 instructions\n\
20379-mno-dspr3 do not generate DSP R3 instructions\n"));
20380 fprintf (stream, _("\
ef2e4d86
CF
20381-mmt generate MT instructions\n\
20382-mno-mt do not generate MT instructions\n"));
20383 fprintf (stream, _("\
dec0624d
MR
20384-mmcu generate MCU instructions\n\
20385-mno-mcu do not generate MCU instructions\n"));
20386 fprintf (stream, _("\
56d438b1
CF
20387-mmsa generate MSA instructions\n\
20388-mno-msa do not generate MSA instructions\n"));
20389 fprintf (stream, _("\
7d64c587
AB
20390-mxpa generate eXtended Physical Address (XPA) instructions\n\
20391-mno-xpa do not generate eXtended Physical Address (XPA) instructions\n"));
20392 fprintf (stream, _("\
b015e599
AP
20393-mvirt generate Virtualization instructions\n\
20394-mno-virt do not generate Virtualization instructions\n"));
20395 fprintf (stream, _("\
730c3174
SE
20396-mcrc generate CRC instructions\n\
20397-mno-crc do not generate CRC instructions\n"));
20398 fprintf (stream, _("\
6f20c942
FS
20399-mginv generate Global INValidate (GINV) instructions\n\
20400-mno-ginv do not generate Global INValidate instructions\n"));
20401 fprintf (stream, _("\
8095d2f7
CX
20402-mloongson-mmi generate Loongson MultiMedia extensions Instructions (MMI) instructions\n\
20403-mno-loongson-mmi do not generate Loongson MultiMedia extensions Instructions\n"));
20404 fprintf (stream, _("\
716c08de
CX
20405-mloongson-cam generate Loongson Content Address Memory (CAM) instructions\n\
20406-mno-loongson-cam do not generate Loongson Content Address Memory Instructions\n"));
20407 fprintf (stream, _("\
bdc6c06e
CX
20408-mloongson-ext generate Loongson EXTensions (EXT) instructions\n\
20409-mno-loongson-ext do not generate Loongson EXTensions Instructions\n"));
20410 fprintf (stream, _("\
a693765e
CX
20411-mloongson-ext2 generate Loongson EXTensions R2 (EXT2) instructions\n\
20412-mno-loongson-ext2 do not generate Loongson EXTensions R2 Instructions\n"));
20413 fprintf (stream, _("\
833794fc
MR
20414-minsn32 only generate 32-bit microMIPS instructions\n\
20415-mno-insn32 generate all microMIPS instructions\n"));
6f2117ba
PH
20416#if DEFAULT_MIPS_FIX_LOONGSON3_LLSC
20417 fprintf (stream, _("\
20418-mfix-loongson3-llsc work around Loongson3 LL/SC errata, default\n\
20419-mno-fix-loongson3-llsc disable work around Loongson3 LL/SC errata\n"));
20420#else
20421 fprintf (stream, _("\
20422-mfix-loongson3-llsc work around Loongson3 LL/SC errata\n\
20423-mno-fix-loongson3-llsc disable work around Loongson3 LL/SC errata, default\n"));
20424#endif
833794fc 20425 fprintf (stream, _("\
c67a084a
NC
20426-mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
20427-mfix-loongson2f-nop work around Loongson2F NOP errata\n\
6f2117ba
PH
20428-mfix-loongson3-llsc work around Loongson3 LL/SC errata\n\
20429-mno-fix-loongson3-llsc disable work around Loongson3 LL/SC errata\n\
d766e8ec 20430-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 20431-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
6a32d874 20432-mfix-24k insert a nop after ERET and DERET instructions\n\
d954098f 20433-mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
27c634e0 20434-mfix-r5900 work around R5900 short loop errata\n\
316f5878
RS
20435-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
20436-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261 20437-msym32 assume all symbols have 32-bit values\n\
092a534f
MR
20438-O0 do not remove unneeded NOPs, do not swap branches\n\
20439-O, -O1 remove unneeded NOPs, do not swap branches\n\
20440-O2 remove unneeded NOPs and swap branches\n\
316f5878
RS
20441--trap, --no-break trap exception on div by 0 and mult overflow\n\
20442--break, --no-trap break exception on div by 0 and mult overflow\n"));
037b32b9
AN
20443 fprintf (stream, _("\
20444-mhard-float allow floating-point instructions\n\
20445-msoft-float do not allow floating-point instructions\n\
20446-msingle-float only allow 32-bit floating-point operations\n\
20447-mdouble-float allow 32-bit and 64-bit floating-point operations\n\
3bf0dbfb 20448--[no-]construct-floats [dis]allow floating point values to be constructed\n\
ba92f887 20449--[no-]relax-branch [dis]allow out-of-range branches to be relaxed\n\
8b10b0b3
MR
20450-mignore-branch-isa accept invalid branches requiring an ISA mode switch\n\
20451-mno-ignore-branch-isa reject invalid branches requiring an ISA mode switch\n\
ba92f887
MR
20452-mnan=ENCODING select an IEEE 754 NaN encoding convention, either of:\n"));
20453
20454 first = 1;
20455
20456 show (stream, "legacy", &column, &first);
20457 show (stream, "2008", &column, &first);
20458
20459 fputc ('\n', stream);
20460
316f5878
RS
20461 fprintf (stream, _("\
20462-KPIC, -call_shared generate SVR4 position independent code\n\
861fb55a 20463-call_nonpic generate non-PIC code that can operate with DSOs\n\
0c000745 20464-mvxworks-pic generate VxWorks position independent code\n\
861fb55a 20465-non_shared do not generate code that can operate with DSOs\n\
316f5878 20466-xgot assume a 32 bit GOT\n\
dcd410fe 20467-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8 20468-mshared, -mno-shared disable/enable .cpload optimization for\n\
d821e36b 20469 position dependent (non shared) code\n\
316f5878
RS
20470-mabi=ABI create ABI conformant object file for:\n"));
20471
20472 first = 1;
20473
20474 show (stream, "32", &column, &first);
20475 show (stream, "o64", &column, &first);
20476 show (stream, "n32", &column, &first);
20477 show (stream, "64", &column, &first);
20478 show (stream, "eabi", &column, &first);
20479
20480 fputc ('\n', stream);
20481
20482 fprintf (stream, _("\
b4f6242e
MR
20483-32 create o32 ABI object file%s\n"),
20484 MIPS_DEFAULT_ABI == O32_ABI ? _(" (default)") : "");
20485 fprintf (stream, _("\
20486-n32 create n32 ABI object file%s\n"),
20487 MIPS_DEFAULT_ABI == N32_ABI ? _(" (default)") : "");
20488 fprintf (stream, _("\
20489-64 create 64 ABI object file%s\n"),
20490 MIPS_DEFAULT_ABI == N64_ABI ? _(" (default)") : "");
e7af610e 20491}
14e777e0 20492
1575952e 20493#ifdef TE_IRIX
14e777e0 20494enum dwarf2_format
413a266c 20495mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
14e777e0 20496{
369943fe 20497 if (HAVE_64BIT_SYMBOLS)
1575952e 20498 return dwarf2_format_64bit_irix;
14e777e0
KB
20499 else
20500 return dwarf2_format_32bit;
20501}
1575952e 20502#endif
73369e65
EC
20503
20504int
20505mips_dwarf2_addr_size (void)
20506{
6b6b3450 20507 if (HAVE_64BIT_OBJECTS)
73369e65 20508 return 8;
73369e65
EC
20509 else
20510 return 4;
20511}
5862107c
EC
20512
20513/* Standard calling conventions leave the CFA at SP on entry. */
20514void
20515mips_cfi_frame_initial_instructions (void)
20516{
20517 cfi_add_CFA_def_cfa_register (SP);
20518}
20519
707bfff6
TS
20520int
20521tc_mips_regname_to_dw2regnum (char *regname)
20522{
20523 unsigned int regnum = -1;
20524 unsigned int reg;
20525
20526 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
20527 regnum = reg;
20528
20529 return regnum;
20530}
263b2574 20531
20532/* Implement CONVERT_SYMBOLIC_ATTRIBUTE.
20533 Given a symbolic attribute NAME, return the proper integer value.
20534 Returns -1 if the attribute is not known. */
20535
20536int
20537mips_convert_symbolic_attribute (const char *name)
20538{
20539 static const struct
20540 {
20541 const char * name;
20542 const int tag;
20543 }
20544 attribute_table[] =
20545 {
20546#define T(tag) {#tag, tag}
20547 T (Tag_GNU_MIPS_ABI_FP),
20548 T (Tag_GNU_MIPS_ABI_MSA),
20549#undef T
20550 };
20551 unsigned int i;
20552
20553 if (name == NULL)
20554 return -1;
20555
20556 for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
20557 if (streq (name, attribute_table[i].name))
20558 return attribute_table[i].tag;
20559
20560 return -1;
20561}
fd5c94ab
RS
20562
20563void
20564md_mips_end (void)
20565{
351cdf24
MF
20566 int fpabi = Val_GNU_MIPS_ABI_FP_ANY;
20567
fd5c94ab
RS
20568 mips_emit_delays ();
20569 if (cur_proc_ptr)
20570 as_warn (_("missing .end at end of assembly"));
919731af 20571
20572 /* Just in case no code was emitted, do the consistency check. */
20573 file_mips_check_options ();
351cdf24
MF
20574
20575 /* Set a floating-point ABI if the user did not. */
20576 if (obj_elf_seen_attribute (OBJ_ATTR_GNU, Tag_GNU_MIPS_ABI_FP))
20577 {
20578 /* Perform consistency checks on the floating-point ABI. */
20579 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20580 Tag_GNU_MIPS_ABI_FP);
20581 if (fpabi != Val_GNU_MIPS_ABI_FP_ANY)
20582 check_fpabi (fpabi);
20583 }
20584 else
20585 {
20586 /* Soft-float gets precedence over single-float, the two options should
20587 not be used together so this should not matter. */
20588 if (file_mips_opts.soft_float == 1)
20589 fpabi = Val_GNU_MIPS_ABI_FP_SOFT;
20590 /* Single-float gets precedence over all double_float cases. */
20591 else if (file_mips_opts.single_float == 1)
20592 fpabi = Val_GNU_MIPS_ABI_FP_SINGLE;
20593 else
20594 {
20595 switch (file_mips_opts.fp)
20596 {
20597 case 32:
20598 if (file_mips_opts.gp == 32)
20599 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20600 break;
20601 case 0:
20602 fpabi = Val_GNU_MIPS_ABI_FP_XX;
20603 break;
20604 case 64:
20605 if (file_mips_opts.gp == 32 && !file_mips_opts.oddspreg)
20606 fpabi = Val_GNU_MIPS_ABI_FP_64A;
20607 else if (file_mips_opts.gp == 32)
20608 fpabi = Val_GNU_MIPS_ABI_FP_64;
20609 else
20610 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20611 break;
20612 }
20613 }
20614
20615 bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20616 Tag_GNU_MIPS_ABI_FP, fpabi);
20617 }
fd5c94ab 20618}
2f0c68f2
CM
20619
20620/* Returns the relocation type required for a particular CFI encoding. */
20621
20622bfd_reloc_code_real_type
20623mips_cfi_reloc_for_encoding (int encoding)
20624{
20625 if (encoding == (DW_EH_PE_sdata4 | DW_EH_PE_pcrel))
20626 return BFD_RELOC_32_PCREL;
20627 else return BFD_RELOC_NONE;
20628}
This page took 3.453449 seconds and 4 git commands to generate.