The patch fixed invalid compilation of instruction LD IY,(HL) and disassemble of...
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
CommitLineData
252b5132 1/* tc-mips.c -- assemble code for a MIPS chip.
b3adc24a 2 Copyright (C) 1993-2020 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
dec7b24b
YS
815/* The maximum number of LABELS detect for the same address. */
816#define MAX_LABELS_SAME 10
817
fc76e730 818/* Arrays of operands for each instruction. */
14daeee3 819#define MAX_OPERANDS 6
6f2117ba
PH
820struct mips_operand_array
821{
fc76e730
RS
822 const struct mips_operand *operand[MAX_OPERANDS];
823};
824static struct mips_operand_array *mips_operands;
825static struct mips_operand_array *mips16_operands;
826static struct mips_operand_array *micromips_operands;
827
1e915849 828/* Nop instructions used by emit_nop. */
df58fc94
RS
829static struct mips_cl_insn nop_insn;
830static struct mips_cl_insn mips16_nop_insn;
831static struct mips_cl_insn micromips_nop16_insn;
832static struct mips_cl_insn micromips_nop32_insn;
1e915849 833
6f2117ba
PH
834/* Sync instructions used by insert sync. */
835static struct mips_cl_insn sync_insn;
836
1e915849 837/* The appropriate nop for the current mode. */
833794fc
MR
838#define NOP_INSN (mips_opts.mips16 \
839 ? &mips16_nop_insn \
840 : (mips_opts.micromips \
841 ? (mips_opts.insn32 \
842 ? &micromips_nop32_insn \
843 : &micromips_nop16_insn) \
844 : &nop_insn))
df58fc94
RS
845
846/* The size of NOP_INSN in bytes. */
833794fc
MR
847#define NOP_INSN_SIZE ((mips_opts.mips16 \
848 || (mips_opts.micromips && !mips_opts.insn32)) \
849 ? 2 : 4)
252b5132 850
252b5132
RH
851/* If this is set, it points to a frag holding nop instructions which
852 were inserted before the start of a noreorder section. If those
853 nops turn out to be unnecessary, the size of the frag can be
854 decreased. */
855static fragS *prev_nop_frag;
856
857/* The number of nop instructions we created in prev_nop_frag. */
858static int prev_nop_frag_holds;
859
860/* The number of nop instructions that we know we need in
bdaaa2e1 861 prev_nop_frag. */
252b5132
RH
862static int prev_nop_frag_required;
863
864/* The number of instructions we've seen since prev_nop_frag. */
865static int prev_nop_frag_since;
866
e8044f35
RS
867/* Relocations against symbols are sometimes done in two parts, with a HI
868 relocation and a LO relocation. Each relocation has only 16 bits of
869 space to store an addend. This means that in order for the linker to
870 handle carries correctly, it must be able to locate both the HI and
871 the LO relocation. This means that the relocations must appear in
872 order in the relocation table.
252b5132
RH
873
874 In order to implement this, we keep track of each unmatched HI
875 relocation. We then sort them so that they immediately precede the
bdaaa2e1 876 corresponding LO relocation. */
252b5132 877
e972090a
NC
878struct mips_hi_fixup
879{
252b5132
RH
880 /* Next HI fixup. */
881 struct mips_hi_fixup *next;
882 /* This fixup. */
883 fixS *fixp;
884 /* The section this fixup is in. */
885 segT seg;
886};
887
888/* The list of unmatched HI relocs. */
889
890static struct mips_hi_fixup *mips_hi_fixup_list;
891
64bdfcaf
RS
892/* The frag containing the last explicit relocation operator.
893 Null if explicit relocations have not been used. */
894
895static fragS *prev_reloc_op_frag;
896
252b5132
RH
897/* Map mips16 register numbers to normal MIPS register numbers. */
898
e972090a
NC
899static const unsigned int mips16_to_32_reg_map[] =
900{
252b5132
RH
901 16, 17, 2, 3, 4, 5, 6, 7
902};
60b63b72 903
df58fc94
RS
904/* Map microMIPS register numbers to normal MIPS register numbers. */
905
df58fc94 906#define micromips_to_32_reg_d_map mips16_to_32_reg_map
df58fc94
RS
907
908/* The microMIPS registers with type h. */
e76ff5ab 909static const unsigned int micromips_to_32_reg_h_map1[] =
df58fc94
RS
910{
911 5, 5, 6, 4, 4, 4, 4, 4
912};
e76ff5ab 913static const unsigned int micromips_to_32_reg_h_map2[] =
df58fc94
RS
914{
915 6, 7, 7, 21, 22, 5, 6, 7
916};
917
df58fc94
RS
918/* The microMIPS registers with type m. */
919static const unsigned int micromips_to_32_reg_m_map[] =
920{
921 0, 17, 2, 3, 16, 18, 19, 20
922};
923
924#define micromips_to_32_reg_n_map micromips_to_32_reg_m_map
925
71400594
RS
926/* Classifies the kind of instructions we're interested in when
927 implementing -mfix-vr4120. */
c67a084a
NC
928enum fix_vr4120_class
929{
71400594
RS
930 FIX_VR4120_MACC,
931 FIX_VR4120_DMACC,
932 FIX_VR4120_MULT,
933 FIX_VR4120_DMULT,
934 FIX_VR4120_DIV,
935 FIX_VR4120_MTHILO,
936 NUM_FIX_VR4120_CLASSES
937};
938
c67a084a
NC
939/* ...likewise -mfix-loongson2f-jump. */
940static bfd_boolean mips_fix_loongson2f_jump;
941
942/* ...likewise -mfix-loongson2f-nop. */
943static bfd_boolean mips_fix_loongson2f_nop;
944
945/* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed. */
946static bfd_boolean mips_fix_loongson2f;
947
71400594
RS
948/* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
949 there must be at least one other instruction between an instruction
950 of type X and an instruction of type Y. */
951static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
952
953/* True if -mfix-vr4120 is in force. */
d766e8ec 954static int mips_fix_vr4120;
4a6a3df4 955
7d8e00cf
RS
956/* ...likewise -mfix-vr4130. */
957static int mips_fix_vr4130;
958
6a32d874
CM
959/* ...likewise -mfix-24k. */
960static int mips_fix_24k;
961
a8d14a88
CM
962/* ...likewise -mfix-rm7000 */
963static int mips_fix_rm7000;
964
d954098f
DD
965/* ...likewise -mfix-cn63xxp1 */
966static bfd_boolean mips_fix_cn63xxp1;
967
27c634e0
FN
968/* ...likewise -mfix-r5900 */
969static bfd_boolean mips_fix_r5900;
970static bfd_boolean mips_fix_r5900_explicit;
971
6f2117ba
PH
972/* ...likewise -mfix-loongson3-llsc. */
973static bfd_boolean mips_fix_loongson3_llsc = DEFAULT_MIPS_FIX_LOONGSON3_LLSC;
974
4a6a3df4
AO
975/* We don't relax branches by default, since this causes us to expand
976 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
977 fail to compute the offset before expanding the macro to the most
978 efficient expansion. */
979
980static int mips_relax_branch;
8b10b0b3
MR
981
982/* TRUE if checks are suppressed for invalid branches between ISA modes.
983 Needed for broken assembly produced by some GCC versions and some
984 sloppy code out there, where branches to data labels are present. */
985static bfd_boolean mips_ignore_branch_isa;
252b5132 986\f
4d7206a2
RS
987/* The expansion of many macros depends on the type of symbol that
988 they refer to. For example, when generating position-dependent code,
989 a macro that refers to a symbol may have two different expansions,
990 one which uses GP-relative addresses and one which uses absolute
991 addresses. When generating SVR4-style PIC, a macro may have
992 different expansions for local and global symbols.
993
994 We handle these situations by generating both sequences and putting
995 them in variant frags. In position-dependent code, the first sequence
996 will be the GP-relative one and the second sequence will be the
997 absolute one. In SVR4 PIC, the first sequence will be for global
998 symbols and the second will be for local symbols.
999
584892a6
RS
1000 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
1001 SECOND are the lengths of the two sequences in bytes. These fields
1002 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
1003 the subtype has the following flags:
4d7206a2 1004
ce8ad872
MR
1005 RELAX_PIC
1006 Set if generating PIC code.
1007
584892a6
RS
1008 RELAX_USE_SECOND
1009 Set if it has been decided that we should use the second
1010 sequence instead of the first.
1011
1012 RELAX_SECOND_LONGER
1013 Set in the first variant frag if the macro's second implementation
1014 is longer than its first. This refers to the macro as a whole,
1015 not an individual relaxation.
1016
1017 RELAX_NOMACRO
1018 Set in the first variant frag if the macro appeared in a .set nomacro
1019 block and if one alternative requires a warning but the other does not.
1020
1021 RELAX_DELAY_SLOT
1022 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
1023 delay slot.
4d7206a2 1024
df58fc94
RS
1025 RELAX_DELAY_SLOT_16BIT
1026 Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
1027 16-bit instruction.
1028
1029 RELAX_DELAY_SLOT_SIZE_FIRST
1030 Like RELAX_DELAY_SLOT, but indicates that the first implementation of
1031 the macro is of the wrong size for the branch delay slot.
1032
1033 RELAX_DELAY_SLOT_SIZE_SECOND
1034 Like RELAX_DELAY_SLOT, but indicates that the second implementation of
1035 the macro is of the wrong size for the branch delay slot.
1036
4d7206a2
RS
1037 The frag's "opcode" points to the first fixup for relaxable code.
1038
1039 Relaxable macros are generated using a sequence such as:
1040
1041 relax_start (SYMBOL);
1042 ... generate first expansion ...
1043 relax_switch ();
1044 ... generate second expansion ...
1045 relax_end ();
1046
1047 The code and fixups for the unwanted alternative are discarded
1048 by md_convert_frag. */
ce8ad872
MR
1049#define RELAX_ENCODE(FIRST, SECOND, PIC) \
1050 (((FIRST) << 8) | (SECOND) | ((PIC) ? 0x10000 : 0))
4d7206a2 1051
584892a6
RS
1052#define RELAX_FIRST(X) (((X) >> 8) & 0xff)
1053#define RELAX_SECOND(X) ((X) & 0xff)
ce8ad872
MR
1054#define RELAX_PIC(X) (((X) & 0x10000) != 0)
1055#define RELAX_USE_SECOND 0x20000
1056#define RELAX_SECOND_LONGER 0x40000
1057#define RELAX_NOMACRO 0x80000
1058#define RELAX_DELAY_SLOT 0x100000
1059#define RELAX_DELAY_SLOT_16BIT 0x200000
1060#define RELAX_DELAY_SLOT_SIZE_FIRST 0x400000
1061#define RELAX_DELAY_SLOT_SIZE_SECOND 0x800000
252b5132 1062
4a6a3df4
AO
1063/* Branch without likely bit. If label is out of range, we turn:
1064
134c0c8b 1065 beq reg1, reg2, label
4a6a3df4
AO
1066 delay slot
1067
1068 into
1069
1070 bne reg1, reg2, 0f
1071 nop
1072 j label
1073 0: delay slot
1074
1075 with the following opcode replacements:
1076
1077 beq <-> bne
1078 blez <-> bgtz
1079 bltz <-> bgez
1080 bc1f <-> bc1t
1081
1082 bltzal <-> bgezal (with jal label instead of j label)
1083
1084 Even though keeping the delay slot instruction in the delay slot of
1085 the branch would be more efficient, it would be very tricky to do
1086 correctly, because we'd have to introduce a variable frag *after*
1087 the delay slot instruction, and expand that instead. Let's do it
1088 the easy way for now, even if the branch-not-taken case now costs
1089 one additional instruction. Out-of-range branches are not supposed
1090 to be common, anyway.
1091
1092 Branch likely. If label is out of range, we turn:
1093
1094 beql reg1, reg2, label
1095 delay slot (annulled if branch not taken)
1096
1097 into
1098
1099 beql reg1, reg2, 1f
1100 nop
1101 beql $0, $0, 2f
1102 nop
1103 1: j[al] label
1104 delay slot (executed only if branch taken)
1105 2:
1106
1107 It would be possible to generate a shorter sequence by losing the
1108 likely bit, generating something like:
b34976b6 1109
4a6a3df4
AO
1110 bne reg1, reg2, 0f
1111 nop
1112 j[al] label
1113 delay slot (executed only if branch taken)
1114 0:
1115
1116 beql -> bne
1117 bnel -> beq
1118 blezl -> bgtz
1119 bgtzl -> blez
1120 bltzl -> bgez
1121 bgezl -> bltz
1122 bc1fl -> bc1t
1123 bc1tl -> bc1f
1124
1125 bltzall -> bgezal (with jal label instead of j label)
1126 bgezall -> bltzal (ditto)
1127
1128
1129 but it's not clear that it would actually improve performance. */
ce8ad872
MR
1130#define RELAX_BRANCH_ENCODE(at, pic, \
1131 uncond, likely, link, toofar) \
66b3e8da
MR
1132 ((relax_substateT) \
1133 (0xc0000000 \
1134 | ((at) & 0x1f) \
ce8ad872
MR
1135 | ((pic) ? 0x20 : 0) \
1136 | ((toofar) ? 0x40 : 0) \
1137 | ((link) ? 0x80 : 0) \
1138 | ((likely) ? 0x100 : 0) \
1139 | ((uncond) ? 0x200 : 0)))
4a6a3df4 1140#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
ce8ad872
MR
1141#define RELAX_BRANCH_UNCOND(i) (((i) & 0x200) != 0)
1142#define RELAX_BRANCH_LIKELY(i) (((i) & 0x100) != 0)
1143#define RELAX_BRANCH_LINK(i) (((i) & 0x80) != 0)
1144#define RELAX_BRANCH_TOOFAR(i) (((i) & 0x40) != 0)
1145#define RELAX_BRANCH_PIC(i) (((i) & 0x20) != 0)
66b3e8da 1146#define RELAX_BRANCH_AT(i) ((i) & 0x1f)
4a6a3df4 1147
252b5132
RH
1148/* For mips16 code, we use an entirely different form of relaxation.
1149 mips16 supports two versions of most instructions which take
1150 immediate values: a small one which takes some small value, and a
1151 larger one which takes a 16 bit value. Since branches also follow
1152 this pattern, relaxing these values is required.
1153
1154 We can assemble both mips16 and normal MIPS code in a single
1155 object. Therefore, we need to support this type of relaxation at
1156 the same time that we support the relaxation described above. We
1157 use the high bit of the subtype field to distinguish these cases.
1158
1159 The information we store for this type of relaxation is the
1160 argument code found in the opcode file for this relocation, whether
1161 the user explicitly requested a small or extended form, and whether
1162 the relocation is in a jump or jal delay slot. That tells us the
1163 size of the value, and how it should be stored. We also store
1164 whether the fragment is considered to be extended or not. We also
1165 store whether this is known to be a branch to a different section,
1166 whether we have tried to relax this frag yet, and whether we have
1167 ever extended a PC relative fragment because of a shift count. */
25499ac7 1168#define RELAX_MIPS16_ENCODE(type, e2, pic, sym32, nomacro, \
8507b6e7
MR
1169 small, ext, \
1170 dslot, jal_dslot) \
252b5132
RH
1171 (0x80000000 \
1172 | ((type) & 0xff) \
25499ac7
MR
1173 | ((e2) ? 0x100 : 0) \
1174 | ((pic) ? 0x200 : 0) \
1175 | ((sym32) ? 0x400 : 0) \
1176 | ((nomacro) ? 0x800 : 0) \
1177 | ((small) ? 0x1000 : 0) \
1178 | ((ext) ? 0x2000 : 0) \
1179 | ((dslot) ? 0x4000 : 0) \
1180 | ((jal_dslot) ? 0x8000 : 0))
8507b6e7 1181
4a6a3df4 1182#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132 1183#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
25499ac7
MR
1184#define RELAX_MIPS16_E2(i) (((i) & 0x100) != 0)
1185#define RELAX_MIPS16_PIC(i) (((i) & 0x200) != 0)
1186#define RELAX_MIPS16_SYM32(i) (((i) & 0x400) != 0)
1187#define RELAX_MIPS16_NOMACRO(i) (((i) & 0x800) != 0)
1188#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x1000) != 0)
1189#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x2000) != 0)
1190#define RELAX_MIPS16_DSLOT(i) (((i) & 0x4000) != 0)
1191#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x8000) != 0)
1192
1193#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x10000) != 0)
1194#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x10000)
1195#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) & ~0x10000)
1196#define RELAX_MIPS16_ALWAYS_EXTENDED(i) (((i) & 0x20000) != 0)
1197#define RELAX_MIPS16_MARK_ALWAYS_EXTENDED(i) ((i) | 0x20000)
1198#define RELAX_MIPS16_CLEAR_ALWAYS_EXTENDED(i) ((i) & ~0x20000)
1199#define RELAX_MIPS16_MACRO(i) (((i) & 0x40000) != 0)
1200#define RELAX_MIPS16_MARK_MACRO(i) ((i) | 0x40000)
1201#define RELAX_MIPS16_CLEAR_MACRO(i) ((i) & ~0x40000)
885add95 1202
df58fc94
RS
1203/* For microMIPS code, we use relaxation similar to one we use for
1204 MIPS16 code. Some instructions that take immediate values support
1205 two encodings: a small one which takes some small value, and a
1206 larger one which takes a 16 bit value. As some branches also follow
1207 this pattern, relaxing these values is required.
1208
1209 We can assemble both microMIPS and normal MIPS code in a single
1210 object. Therefore, we need to support this type of relaxation at
1211 the same time that we support the relaxation described above. We
1212 use one of the high bits of the subtype field to distinguish these
1213 cases.
1214
1215 The information we store for this type of relaxation is the argument
1216 code found in the opcode file for this relocation, the register
8484fb75
MR
1217 selected as the assembler temporary, whether in the 32-bit
1218 instruction mode, whether the branch is unconditional, whether it is
7bd374a4
MR
1219 compact, whether there is no delay-slot instruction available to fill
1220 in, whether it stores the link address implicitly in $ra, whether
1221 relaxation of out-of-range 32-bit branches to a sequence of
8484fb75
MR
1222 instructions is enabled, and whether the displacement of a branch is
1223 too large to fit as an immediate argument of a 16-bit and a 32-bit
1224 branch, respectively. */
ce8ad872 1225#define RELAX_MICROMIPS_ENCODE(type, at, insn32, pic, \
7bd374a4 1226 uncond, compact, link, nods, \
40209cad
MR
1227 relax32, toofar16, toofar32) \
1228 (0x40000000 \
1229 | ((type) & 0xff) \
1230 | (((at) & 0x1f) << 8) \
8484fb75 1231 | ((insn32) ? 0x2000 : 0) \
ce8ad872
MR
1232 | ((pic) ? 0x4000 : 0) \
1233 | ((uncond) ? 0x8000 : 0) \
1234 | ((compact) ? 0x10000 : 0) \
1235 | ((link) ? 0x20000 : 0) \
1236 | ((nods) ? 0x40000 : 0) \
1237 | ((relax32) ? 0x80000 : 0) \
1238 | ((toofar16) ? 0x100000 : 0) \
1239 | ((toofar32) ? 0x200000 : 0))
df58fc94
RS
1240#define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1241#define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1242#define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
8484fb75 1243#define RELAX_MICROMIPS_INSN32(i) (((i) & 0x2000) != 0)
ce8ad872
MR
1244#define RELAX_MICROMIPS_PIC(i) (((i) & 0x4000) != 0)
1245#define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x8000) != 0)
1246#define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x10000) != 0)
1247#define RELAX_MICROMIPS_LINK(i) (((i) & 0x20000) != 0)
1248#define RELAX_MICROMIPS_NODS(i) (((i) & 0x40000) != 0)
1249#define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x80000) != 0)
1250
1251#define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x100000) != 0)
1252#define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x100000)
1253#define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x100000)
1254#define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x200000) != 0)
1255#define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x200000)
1256#define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x200000)
df58fc94 1257
43c0598f
RS
1258/* Sign-extend 16-bit value X. */
1259#define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1260
885add95
CD
1261/* Is the given value a sign-extended 32-bit value? */
1262#define IS_SEXT_32BIT_NUM(x) \
1263 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1264 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1265
1266/* Is the given value a sign-extended 16-bit value? */
1267#define IS_SEXT_16BIT_NUM(x) \
1268 (((x) &~ (offsetT) 0x7fff) == 0 \
1269 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1270
df58fc94
RS
1271/* Is the given value a sign-extended 12-bit value? */
1272#define IS_SEXT_12BIT_NUM(x) \
1273 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1274
7f3c4072
CM
1275/* Is the given value a sign-extended 9-bit value? */
1276#define IS_SEXT_9BIT_NUM(x) \
1277 (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1278
2051e8c4
MR
1279/* Is the given value a zero-extended 32-bit value? Or a negated one? */
1280#define IS_ZEXT_32BIT_NUM(x) \
1281 (((x) &~ (offsetT) 0xffffffff) == 0 \
1282 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1283
bf12938e
RS
1284/* Extract bits MASK << SHIFT from STRUCT and shift them right
1285 SHIFT places. */
1286#define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1287 (((STRUCT) >> (SHIFT)) & (MASK))
1288
bf12938e 1289/* Extract the operand given by FIELD from mips_cl_insn INSN. */
df58fc94
RS
1290#define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1291 (!(MICROMIPS) \
1292 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1293 : EXTRACT_BITS ((INSN).insn_opcode, \
1294 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
bf12938e
RS
1295#define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1296 EXTRACT_BITS ((INSN).insn_opcode, \
1297 MIPS16OP_MASK_##FIELD, \
1298 MIPS16OP_SH_##FIELD)
5c04167a
RS
1299
1300/* The MIPS16 EXTEND opcode, shifted left 16 places. */
1301#define MIPS16_EXTEND (0xf000U << 16)
4d7206a2 1302\f
df58fc94
RS
1303/* Whether or not we are emitting a branch-likely macro. */
1304static bfd_boolean emit_branch_likely_macro = FALSE;
1305
4d7206a2
RS
1306/* Global variables used when generating relaxable macros. See the
1307 comment above RELAX_ENCODE for more details about how relaxation
1308 is used. */
1309static struct {
1310 /* 0 if we're not emitting a relaxable macro.
1311 1 if we're emitting the first of the two relaxation alternatives.
1312 2 if we're emitting the second alternative. */
1313 int sequence;
1314
1315 /* The first relaxable fixup in the current frag. (In other words,
1316 the first fixup that refers to relaxable code.) */
1317 fixS *first_fixup;
1318
1319 /* sizes[0] says how many bytes of the first alternative are stored in
1320 the current frag. Likewise sizes[1] for the second alternative. */
1321 unsigned int sizes[2];
1322
1323 /* The symbol on which the choice of sequence depends. */
1324 symbolS *symbol;
1325} mips_relax;
252b5132 1326\f
584892a6
RS
1327/* Global variables used to decide whether a macro needs a warning. */
1328static struct {
1329 /* True if the macro is in a branch delay slot. */
1330 bfd_boolean delay_slot_p;
1331
df58fc94
RS
1332 /* Set to the length in bytes required if the macro is in a delay slot
1333 that requires a specific length of instruction, otherwise zero. */
1334 unsigned int delay_slot_length;
1335
584892a6
RS
1336 /* For relaxable macros, sizes[0] is the length of the first alternative
1337 in bytes and sizes[1] is the length of the second alternative.
1338 For non-relaxable macros, both elements give the length of the
1339 macro in bytes. */
1340 unsigned int sizes[2];
1341
df58fc94
RS
1342 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1343 instruction of the first alternative in bytes and first_insn_sizes[1]
1344 is the length of the first instruction of the second alternative.
1345 For non-relaxable macros, both elements give the length of the first
1346 instruction in bytes.
1347
1348 Set to zero if we haven't yet seen the first instruction. */
1349 unsigned int first_insn_sizes[2];
1350
1351 /* For relaxable macros, insns[0] is the number of instructions for the
1352 first alternative and insns[1] is the number of instructions for the
1353 second alternative.
1354
1355 For non-relaxable macros, both elements give the number of
1356 instructions for the macro. */
1357 unsigned int insns[2];
1358
584892a6
RS
1359 /* The first variant frag for this macro. */
1360 fragS *first_frag;
1361} mips_macro_warning;
1362\f
252b5132
RH
1363/* Prototypes for static functions. */
1364
252b5132
RH
1365enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1366
b34976b6 1367static void append_insn
df58fc94
RS
1368 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1369 bfd_boolean expansionp);
7d10b47d 1370static void mips_no_prev_insn (void);
c67a084a 1371static void macro_build (expressionS *, const char *, const char *, ...);
b34976b6 1372static void mips16_macro_build
03ea81db 1373 (expressionS *, const char *, const char *, va_list *);
67c0d1eb 1374static void load_register (int, expressionS *, int);
584892a6
RS
1375static void macro_start (void);
1376static void macro_end (void);
833794fc 1377static void macro (struct mips_cl_insn *ip, char *str);
17a2f251 1378static void mips16_macro (struct mips_cl_insn * ip);
17a2f251
TS
1379static void mips_ip (char *str, struct mips_cl_insn * ip);
1380static void mips16_ip (char *str, struct mips_cl_insn * ip);
25499ac7 1381static unsigned long mips16_immed_extend (offsetT, unsigned int);
b34976b6 1382static void mips16_immed
3b4dbbbf 1383 (const char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
43c0598f 1384 unsigned int, unsigned long *);
5e0116d5 1385static size_t my_getSmallExpression
17a2f251
TS
1386 (expressionS *, bfd_reloc_code_real_type *, char *);
1387static void my_getExpression (expressionS *, char *);
1388static void s_align (int);
1389static void s_change_sec (int);
1390static void s_change_section (int);
1391static void s_cons (int);
1392static void s_float_cons (int);
1393static void s_mips_globl (int);
1394static void s_option (int);
1395static void s_mipsset (int);
1396static void s_abicalls (int);
1397static void s_cpload (int);
1398static void s_cpsetup (int);
1399static void s_cplocal (int);
1400static void s_cprestore (int);
1401static void s_cpreturn (int);
741d6ea8
JM
1402static void s_dtprelword (int);
1403static void s_dtpreldword (int);
d0f13682
CLT
1404static void s_tprelword (int);
1405static void s_tpreldword (int);
17a2f251
TS
1406static void s_gpvalue (int);
1407static void s_gpword (int);
1408static void s_gpdword (int);
a3f278e2 1409static void s_ehword (int);
17a2f251
TS
1410static void s_cpadd (int);
1411static void s_insn (int);
ba92f887 1412static void s_nan (int);
919731af 1413static void s_module (int);
17a2f251
TS
1414static void s_mips_ent (int);
1415static void s_mips_end (int);
1416static void s_mips_frame (int);
1417static void s_mips_mask (int reg_type);
1418static void s_mips_stab (int);
1419static void s_mips_weakext (int);
1420static void s_mips_file (int);
1421static void s_mips_loc (int);
9e009953 1422static bfd_boolean pic_need_relax (symbolS *);
4a6a3df4 1423static int relaxed_branch_length (fragS *, asection *, int);
df58fc94
RS
1424static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1425static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
919731af 1426static void file_mips_check_options (void);
e7af610e
NC
1427
1428/* Table and functions used to map between CPU/ISA names, and
1429 ISA levels, and CPU numbers. */
1430
e972090a
NC
1431struct mips_cpu_info
1432{
e7af610e 1433 const char *name; /* CPU or ISA name. */
d16afab6
RS
1434 int flags; /* MIPS_CPU_* flags. */
1435 int ase; /* Set of ASEs implemented by the CPU. */
e7af610e
NC
1436 int isa; /* ISA level. */
1437 int cpu; /* CPU number (default CPU if ISA). */
1438};
1439
ad3fea08 1440#define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
ad3fea08 1441
17a2f251
TS
1442static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1443static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1444static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132 1445\f
c31f3936
RS
1446/* Command-line options. */
1447const char *md_shortopts = "O::g::G:";
1448
1449enum options
1450 {
1451 OPTION_MARCH = OPTION_MD_BASE,
1452 OPTION_MTUNE,
1453 OPTION_MIPS1,
1454 OPTION_MIPS2,
1455 OPTION_MIPS3,
1456 OPTION_MIPS4,
1457 OPTION_MIPS5,
1458 OPTION_MIPS32,
1459 OPTION_MIPS64,
1460 OPTION_MIPS32R2,
ae52f483
AB
1461 OPTION_MIPS32R3,
1462 OPTION_MIPS32R5,
7361da2c 1463 OPTION_MIPS32R6,
c31f3936 1464 OPTION_MIPS64R2,
ae52f483
AB
1465 OPTION_MIPS64R3,
1466 OPTION_MIPS64R5,
7361da2c 1467 OPTION_MIPS64R6,
c31f3936
RS
1468 OPTION_MIPS16,
1469 OPTION_NO_MIPS16,
1470 OPTION_MIPS3D,
1471 OPTION_NO_MIPS3D,
1472 OPTION_MDMX,
1473 OPTION_NO_MDMX,
1474 OPTION_DSP,
1475 OPTION_NO_DSP,
1476 OPTION_MT,
1477 OPTION_NO_MT,
1478 OPTION_VIRT,
1479 OPTION_NO_VIRT,
56d438b1
CF
1480 OPTION_MSA,
1481 OPTION_NO_MSA,
c31f3936
RS
1482 OPTION_SMARTMIPS,
1483 OPTION_NO_SMARTMIPS,
1484 OPTION_DSPR2,
1485 OPTION_NO_DSPR2,
8f4f9071
MF
1486 OPTION_DSPR3,
1487 OPTION_NO_DSPR3,
c31f3936
RS
1488 OPTION_EVA,
1489 OPTION_NO_EVA,
7d64c587
AB
1490 OPTION_XPA,
1491 OPTION_NO_XPA,
c31f3936
RS
1492 OPTION_MICROMIPS,
1493 OPTION_NO_MICROMIPS,
1494 OPTION_MCU,
1495 OPTION_NO_MCU,
25499ac7
MR
1496 OPTION_MIPS16E2,
1497 OPTION_NO_MIPS16E2,
730c3174
SE
1498 OPTION_CRC,
1499 OPTION_NO_CRC,
c31f3936
RS
1500 OPTION_M4650,
1501 OPTION_NO_M4650,
1502 OPTION_M4010,
1503 OPTION_NO_M4010,
1504 OPTION_M4100,
1505 OPTION_NO_M4100,
1506 OPTION_M3900,
1507 OPTION_NO_M3900,
1508 OPTION_M7000_HILO_FIX,
1509 OPTION_MNO_7000_HILO_FIX,
1510 OPTION_FIX_24K,
1511 OPTION_NO_FIX_24K,
a8d14a88
CM
1512 OPTION_FIX_RM7000,
1513 OPTION_NO_FIX_RM7000,
6f2117ba
PH
1514 OPTION_FIX_LOONGSON3_LLSC,
1515 OPTION_NO_FIX_LOONGSON3_LLSC,
c31f3936
RS
1516 OPTION_FIX_LOONGSON2F_JUMP,
1517 OPTION_NO_FIX_LOONGSON2F_JUMP,
1518 OPTION_FIX_LOONGSON2F_NOP,
1519 OPTION_NO_FIX_LOONGSON2F_NOP,
1520 OPTION_FIX_VR4120,
1521 OPTION_NO_FIX_VR4120,
1522 OPTION_FIX_VR4130,
1523 OPTION_NO_FIX_VR4130,
1524 OPTION_FIX_CN63XXP1,
1525 OPTION_NO_FIX_CN63XXP1,
27c634e0
FN
1526 OPTION_FIX_R5900,
1527 OPTION_NO_FIX_R5900,
c31f3936
RS
1528 OPTION_TRAP,
1529 OPTION_BREAK,
1530 OPTION_EB,
1531 OPTION_EL,
1532 OPTION_FP32,
1533 OPTION_GP32,
1534 OPTION_CONSTRUCT_FLOATS,
1535 OPTION_NO_CONSTRUCT_FLOATS,
1536 OPTION_FP64,
351cdf24 1537 OPTION_FPXX,
c31f3936
RS
1538 OPTION_GP64,
1539 OPTION_RELAX_BRANCH,
1540 OPTION_NO_RELAX_BRANCH,
8b10b0b3
MR
1541 OPTION_IGNORE_BRANCH_ISA,
1542 OPTION_NO_IGNORE_BRANCH_ISA,
833794fc
MR
1543 OPTION_INSN32,
1544 OPTION_NO_INSN32,
c31f3936
RS
1545 OPTION_MSHARED,
1546 OPTION_MNO_SHARED,
1547 OPTION_MSYM32,
1548 OPTION_MNO_SYM32,
1549 OPTION_SOFT_FLOAT,
1550 OPTION_HARD_FLOAT,
1551 OPTION_SINGLE_FLOAT,
1552 OPTION_DOUBLE_FLOAT,
1553 OPTION_32,
c31f3936
RS
1554 OPTION_CALL_SHARED,
1555 OPTION_CALL_NONPIC,
1556 OPTION_NON_SHARED,
1557 OPTION_XGOT,
1558 OPTION_MABI,
1559 OPTION_N32,
1560 OPTION_64,
1561 OPTION_MDEBUG,
1562 OPTION_NO_MDEBUG,
1563 OPTION_PDR,
1564 OPTION_NO_PDR,
1565 OPTION_MVXWORKS_PIC,
ba92f887 1566 OPTION_NAN,
351cdf24
MF
1567 OPTION_ODD_SPREG,
1568 OPTION_NO_ODD_SPREG,
6f20c942
FS
1569 OPTION_GINV,
1570 OPTION_NO_GINV,
8095d2f7
CX
1571 OPTION_LOONGSON_MMI,
1572 OPTION_NO_LOONGSON_MMI,
716c08de
CX
1573 OPTION_LOONGSON_CAM,
1574 OPTION_NO_LOONGSON_CAM,
bdc6c06e
CX
1575 OPTION_LOONGSON_EXT,
1576 OPTION_NO_LOONGSON_EXT,
a693765e
CX
1577 OPTION_LOONGSON_EXT2,
1578 OPTION_NO_LOONGSON_EXT2,
c31f3936
RS
1579 OPTION_END_OF_ENUM
1580 };
1581
1582struct option md_longopts[] =
1583{
1584 /* Options which specify architecture. */
1585 {"march", required_argument, NULL, OPTION_MARCH},
1586 {"mtune", required_argument, NULL, OPTION_MTUNE},
1587 {"mips0", no_argument, NULL, OPTION_MIPS1},
1588 {"mips1", no_argument, NULL, OPTION_MIPS1},
1589 {"mips2", no_argument, NULL, OPTION_MIPS2},
1590 {"mips3", no_argument, NULL, OPTION_MIPS3},
1591 {"mips4", no_argument, NULL, OPTION_MIPS4},
1592 {"mips5", no_argument, NULL, OPTION_MIPS5},
1593 {"mips32", no_argument, NULL, OPTION_MIPS32},
1594 {"mips64", no_argument, NULL, OPTION_MIPS64},
1595 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
ae52f483
AB
1596 {"mips32r3", no_argument, NULL, OPTION_MIPS32R3},
1597 {"mips32r5", no_argument, NULL, OPTION_MIPS32R5},
7361da2c 1598 {"mips32r6", no_argument, NULL, OPTION_MIPS32R6},
c31f3936 1599 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
ae52f483
AB
1600 {"mips64r3", no_argument, NULL, OPTION_MIPS64R3},
1601 {"mips64r5", no_argument, NULL, OPTION_MIPS64R5},
7361da2c 1602 {"mips64r6", no_argument, NULL, OPTION_MIPS64R6},
c31f3936
RS
1603
1604 /* Options which specify Application Specific Extensions (ASEs). */
1605 {"mips16", no_argument, NULL, OPTION_MIPS16},
1606 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1607 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1608 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1609 {"mdmx", no_argument, NULL, OPTION_MDMX},
1610 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1611 {"mdsp", no_argument, NULL, OPTION_DSP},
1612 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1613 {"mmt", no_argument, NULL, OPTION_MT},
1614 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1615 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1616 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1617 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1618 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
8f4f9071
MF
1619 {"mdspr3", no_argument, NULL, OPTION_DSPR3},
1620 {"mno-dspr3", no_argument, NULL, OPTION_NO_DSPR3},
c31f3936
RS
1621 {"meva", no_argument, NULL, OPTION_EVA},
1622 {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1623 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1624 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1625 {"mmcu", no_argument, NULL, OPTION_MCU},
1626 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1627 {"mvirt", no_argument, NULL, OPTION_VIRT},
1628 {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
56d438b1
CF
1629 {"mmsa", no_argument, NULL, OPTION_MSA},
1630 {"mno-msa", no_argument, NULL, OPTION_NO_MSA},
7d64c587
AB
1631 {"mxpa", no_argument, NULL, OPTION_XPA},
1632 {"mno-xpa", no_argument, NULL, OPTION_NO_XPA},
25499ac7
MR
1633 {"mmips16e2", no_argument, NULL, OPTION_MIPS16E2},
1634 {"mno-mips16e2", no_argument, NULL, OPTION_NO_MIPS16E2},
730c3174
SE
1635 {"mcrc", no_argument, NULL, OPTION_CRC},
1636 {"mno-crc", no_argument, NULL, OPTION_NO_CRC},
6f20c942
FS
1637 {"mginv", no_argument, NULL, OPTION_GINV},
1638 {"mno-ginv", no_argument, NULL, OPTION_NO_GINV},
8095d2f7
CX
1639 {"mloongson-mmi", no_argument, NULL, OPTION_LOONGSON_MMI},
1640 {"mno-loongson-mmi", no_argument, NULL, OPTION_NO_LOONGSON_MMI},
716c08de
CX
1641 {"mloongson-cam", no_argument, NULL, OPTION_LOONGSON_CAM},
1642 {"mno-loongson-cam", no_argument, NULL, OPTION_NO_LOONGSON_CAM},
bdc6c06e
CX
1643 {"mloongson-ext", no_argument, NULL, OPTION_LOONGSON_EXT},
1644 {"mno-loongson-ext", no_argument, NULL, OPTION_NO_LOONGSON_EXT},
a693765e
CX
1645 {"mloongson-ext2", no_argument, NULL, OPTION_LOONGSON_EXT2},
1646 {"mno-loongson-ext2", no_argument, NULL, OPTION_NO_LOONGSON_EXT2},
c31f3936
RS
1647
1648 /* Old-style architecture options. Don't add more of these. */
1649 {"m4650", no_argument, NULL, OPTION_M4650},
1650 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1651 {"m4010", no_argument, NULL, OPTION_M4010},
1652 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1653 {"m4100", no_argument, NULL, OPTION_M4100},
1654 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1655 {"m3900", no_argument, NULL, OPTION_M3900},
1656 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1657
1658 /* Options which enable bug fixes. */
1659 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1660 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1661 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
6f2117ba
PH
1662 {"mfix-loongson3-llsc", no_argument, NULL, OPTION_FIX_LOONGSON3_LLSC},
1663 {"mno-fix-loongson3-llsc", no_argument, NULL, OPTION_NO_FIX_LOONGSON3_LLSC},
c31f3936
RS
1664 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1665 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1666 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1667 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1668 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
1669 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1670 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
1671 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1672 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
1673 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
a8d14a88
CM
1674 {"mfix-rm7000", no_argument, NULL, OPTION_FIX_RM7000},
1675 {"mno-fix-rm7000", no_argument, NULL, OPTION_NO_FIX_RM7000},
c31f3936
RS
1676 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1677 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
27c634e0
FN
1678 {"mfix-r5900", no_argument, NULL, OPTION_FIX_R5900},
1679 {"mno-fix-r5900", no_argument, NULL, OPTION_NO_FIX_R5900},
c31f3936
RS
1680
1681 /* Miscellaneous options. */
1682 {"trap", no_argument, NULL, OPTION_TRAP},
1683 {"no-break", no_argument, NULL, OPTION_TRAP},
1684 {"break", no_argument, NULL, OPTION_BREAK},
1685 {"no-trap", no_argument, NULL, OPTION_BREAK},
1686 {"EB", no_argument, NULL, OPTION_EB},
1687 {"EL", no_argument, NULL, OPTION_EL},
1688 {"mfp32", no_argument, NULL, OPTION_FP32},
1689 {"mgp32", no_argument, NULL, OPTION_GP32},
1690 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1691 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1692 {"mfp64", no_argument, NULL, OPTION_FP64},
351cdf24 1693 {"mfpxx", no_argument, NULL, OPTION_FPXX},
c31f3936
RS
1694 {"mgp64", no_argument, NULL, OPTION_GP64},
1695 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1696 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
8b10b0b3
MR
1697 {"mignore-branch-isa", no_argument, NULL, OPTION_IGNORE_BRANCH_ISA},
1698 {"mno-ignore-branch-isa", no_argument, NULL, OPTION_NO_IGNORE_BRANCH_ISA},
833794fc
MR
1699 {"minsn32", no_argument, NULL, OPTION_INSN32},
1700 {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
c31f3936
RS
1701 {"mshared", no_argument, NULL, OPTION_MSHARED},
1702 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1703 {"msym32", no_argument, NULL, OPTION_MSYM32},
1704 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1705 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1706 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1707 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1708 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
351cdf24
MF
1709 {"modd-spreg", no_argument, NULL, OPTION_ODD_SPREG},
1710 {"mno-odd-spreg", no_argument, NULL, OPTION_NO_ODD_SPREG},
c31f3936
RS
1711
1712 /* Strictly speaking this next option is ELF specific,
1713 but we allow it for other ports as well in order to
1714 make testing easier. */
1715 {"32", no_argument, NULL, OPTION_32},
1716
1717 /* ELF-specific options. */
c31f3936
RS
1718 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1719 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1720 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1721 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
1722 {"xgot", no_argument, NULL, OPTION_XGOT},
1723 {"mabi", required_argument, NULL, OPTION_MABI},
1724 {"n32", no_argument, NULL, OPTION_N32},
1725 {"64", no_argument, NULL, OPTION_64},
1726 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1727 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1728 {"mpdr", no_argument, NULL, OPTION_PDR},
1729 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1730 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
ba92f887 1731 {"mnan", required_argument, NULL, OPTION_NAN},
c31f3936
RS
1732
1733 {NULL, no_argument, NULL, 0}
1734};
1735size_t md_longopts_size = sizeof (md_longopts);
1736\f
c6278170
RS
1737/* Information about either an Application Specific Extension or an
1738 optional architecture feature that, for simplicity, we treat in the
1739 same way as an ASE. */
1740struct mips_ase
1741{
1742 /* The name of the ASE, used in both the command-line and .set options. */
1743 const char *name;
1744
1745 /* The associated ASE_* flags. If the ASE is available on both 32-bit
1746 and 64-bit architectures, the flags here refer to the subset that
1747 is available on both. */
1748 unsigned int flags;
1749
1750 /* The ASE_* flag used for instructions that are available on 64-bit
1751 architectures but that are not included in FLAGS. */
1752 unsigned int flags64;
1753
1754 /* The command-line options that turn the ASE on and off. */
1755 int option_on;
1756 int option_off;
1757
1758 /* The minimum required architecture revisions for MIPS32, MIPS64,
1759 microMIPS32 and microMIPS64, or -1 if the extension isn't supported. */
1760 int mips32_rev;
1761 int mips64_rev;
1762 int micromips32_rev;
1763 int micromips64_rev;
7361da2c
AB
1764
1765 /* The architecture where the ASE was removed or -1 if the extension has not
1766 been removed. */
1767 int rem_rev;
c6278170
RS
1768};
1769
1770/* A table of all supported ASEs. */
1771static const struct mips_ase mips_ases[] = {
1772 { "dsp", ASE_DSP, ASE_DSP64,
1773 OPTION_DSP, OPTION_NO_DSP,
7361da2c
AB
1774 2, 2, 2, 2,
1775 -1 },
c6278170
RS
1776
1777 { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1778 OPTION_DSPR2, OPTION_NO_DSPR2,
7361da2c
AB
1779 2, 2, 2, 2,
1780 -1 },
c6278170 1781
8f4f9071
MF
1782 { "dspr3", ASE_DSP | ASE_DSPR2 | ASE_DSPR3, 0,
1783 OPTION_DSPR3, OPTION_NO_DSPR3,
1784 6, 6, -1, -1,
1785 -1 },
1786
c6278170
RS
1787 { "eva", ASE_EVA, 0,
1788 OPTION_EVA, OPTION_NO_EVA,
7361da2c
AB
1789 2, 2, 2, 2,
1790 -1 },
c6278170
RS
1791
1792 { "mcu", ASE_MCU, 0,
1793 OPTION_MCU, OPTION_NO_MCU,
7361da2c
AB
1794 2, 2, 2, 2,
1795 -1 },
c6278170
RS
1796
1797 /* Deprecated in MIPS64r5, but we don't implement that yet. */
1798 { "mdmx", ASE_MDMX, 0,
1799 OPTION_MDMX, OPTION_NO_MDMX,
7361da2c
AB
1800 -1, 1, -1, -1,
1801 6 },
c6278170
RS
1802
1803 /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2. */
1804 { "mips3d", ASE_MIPS3D, 0,
1805 OPTION_MIPS3D, OPTION_NO_MIPS3D,
7361da2c
AB
1806 2, 1, -1, -1,
1807 6 },
c6278170
RS
1808
1809 { "mt", ASE_MT, 0,
1810 OPTION_MT, OPTION_NO_MT,
7361da2c
AB
1811 2, 2, -1, -1,
1812 -1 },
c6278170
RS
1813
1814 { "smartmips", ASE_SMARTMIPS, 0,
1815 OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
7361da2c
AB
1816 1, -1, -1, -1,
1817 6 },
c6278170
RS
1818
1819 { "virt", ASE_VIRT, ASE_VIRT64,
1820 OPTION_VIRT, OPTION_NO_VIRT,
7361da2c
AB
1821 2, 2, 2, 2,
1822 -1 },
56d438b1
CF
1823
1824 { "msa", ASE_MSA, ASE_MSA64,
1825 OPTION_MSA, OPTION_NO_MSA,
7361da2c
AB
1826 2, 2, 2, 2,
1827 -1 },
7d64c587
AB
1828
1829 { "xpa", ASE_XPA, 0,
1830 OPTION_XPA, OPTION_NO_XPA,
909b4e3d 1831 2, 2, 2, 2,
7361da2c 1832 -1 },
25499ac7
MR
1833
1834 { "mips16e2", ASE_MIPS16E2, 0,
1835 OPTION_MIPS16E2, OPTION_NO_MIPS16E2,
1836 2, 2, -1, -1,
1837 6 },
730c3174
SE
1838
1839 { "crc", ASE_CRC, ASE_CRC64,
1840 OPTION_CRC, OPTION_NO_CRC,
1841 6, 6, -1, -1,
1842 -1 },
6f20c942
FS
1843
1844 { "ginv", ASE_GINV, 0,
1845 OPTION_GINV, OPTION_NO_GINV,
1846 6, 6, 6, 6,
1847 -1 },
8095d2f7
CX
1848
1849 { "loongson-mmi", ASE_LOONGSON_MMI, 0,
1850 OPTION_LOONGSON_MMI, OPTION_NO_LOONGSON_MMI,
1851 0, 0, -1, -1,
1852 -1 },
716c08de
CX
1853
1854 { "loongson-cam", ASE_LOONGSON_CAM, 0,
1855 OPTION_LOONGSON_CAM, OPTION_NO_LOONGSON_CAM,
1856 0, 0, -1, -1,
1857 -1 },
bdc6c06e
CX
1858
1859 { "loongson-ext", ASE_LOONGSON_EXT, 0,
1860 OPTION_LOONGSON_EXT, OPTION_NO_LOONGSON_EXT,
1861 0, 0, -1, -1,
1862 -1 },
a693765e
CX
1863
1864 { "loongson-ext2", ASE_LOONGSON_EXT | ASE_LOONGSON_EXT2, 0,
1865 OPTION_LOONGSON_EXT2, OPTION_NO_LOONGSON_EXT2,
1866 0, 0, -1, -1,
1867 -1 },
c6278170
RS
1868};
1869
1870/* The set of ASEs that require -mfp64. */
82bda27b 1871#define FP64_ASES (ASE_MIPS3D | ASE_MDMX | ASE_MSA)
c6278170
RS
1872
1873/* Groups of ASE_* flags that represent different revisions of an ASE. */
1874static const unsigned int mips_ase_groups[] = {
a693765e
CX
1875 ASE_DSP | ASE_DSPR2 | ASE_DSPR3,
1876 ASE_LOONGSON_EXT | ASE_LOONGSON_EXT2
c6278170
RS
1877};
1878\f
252b5132
RH
1879/* Pseudo-op table.
1880
1881 The following pseudo-ops from the Kane and Heinrich MIPS book
1882 should be defined here, but are currently unsupported: .alias,
1883 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1884
1885 The following pseudo-ops from the Kane and Heinrich MIPS book are
1886 specific to the type of debugging information being generated, and
1887 should be defined by the object format: .aent, .begin, .bend,
1888 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1889 .vreg.
1890
1891 The following pseudo-ops from the Kane and Heinrich MIPS book are
1892 not MIPS CPU specific, but are also not specific to the object file
1893 format. This file is probably the best place to define them, but
d84bcf09 1894 they are not currently supported: .asm0, .endr, .lab, .struct. */
252b5132 1895
e972090a
NC
1896static const pseudo_typeS mips_pseudo_table[] =
1897{
beae10d5 1898 /* MIPS specific pseudo-ops. */
252b5132
RH
1899 {"option", s_option, 0},
1900 {"set", s_mipsset, 0},
1901 {"rdata", s_change_sec, 'r'},
1902 {"sdata", s_change_sec, 's'},
1903 {"livereg", s_ignore, 0},
1904 {"abicalls", s_abicalls, 0},
1905 {"cpload", s_cpload, 0},
6478892d
TS
1906 {"cpsetup", s_cpsetup, 0},
1907 {"cplocal", s_cplocal, 0},
252b5132 1908 {"cprestore", s_cprestore, 0},
6478892d 1909 {"cpreturn", s_cpreturn, 0},
741d6ea8
JM
1910 {"dtprelword", s_dtprelword, 0},
1911 {"dtpreldword", s_dtpreldword, 0},
d0f13682
CLT
1912 {"tprelword", s_tprelword, 0},
1913 {"tpreldword", s_tpreldword, 0},
6478892d 1914 {"gpvalue", s_gpvalue, 0},
252b5132 1915 {"gpword", s_gpword, 0},
10181a0d 1916 {"gpdword", s_gpdword, 0},
a3f278e2 1917 {"ehword", s_ehword, 0},
252b5132
RH
1918 {"cpadd", s_cpadd, 0},
1919 {"insn", s_insn, 0},
ba92f887 1920 {"nan", s_nan, 0},
919731af 1921 {"module", s_module, 0},
252b5132 1922
beae10d5 1923 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132 1924 chips. */
38a57ae7 1925 {"asciiz", stringer, 8 + 1},
252b5132
RH
1926 {"bss", s_change_sec, 'b'},
1927 {"err", s_err, 0},
1928 {"half", s_cons, 1},
1929 {"dword", s_cons, 3},
1930 {"weakext", s_mips_weakext, 0},
7c752c2a
TS
1931 {"origin", s_org, 0},
1932 {"repeat", s_rept, 0},
252b5132 1933
998b3c36
MR
1934 /* For MIPS this is non-standard, but we define it for consistency. */
1935 {"sbss", s_change_sec, 'B'},
1936
beae10d5 1937 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1938 here for one reason or another. */
1939 {"align", s_align, 0},
1940 {"byte", s_cons, 0},
1941 {"data", s_change_sec, 'd'},
1942 {"double", s_float_cons, 'd'},
1943 {"float", s_float_cons, 'f'},
1944 {"globl", s_mips_globl, 0},
1945 {"global", s_mips_globl, 0},
1946 {"hword", s_cons, 1},
1947 {"int", s_cons, 2},
1948 {"long", s_cons, 2},
1949 {"octa", s_cons, 4},
1950 {"quad", s_cons, 3},
cca86cc8 1951 {"section", s_change_section, 0},
252b5132
RH
1952 {"short", s_cons, 1},
1953 {"single", s_float_cons, 'f'},
754e2bb9 1954 {"stabd", s_mips_stab, 'd'},
252b5132 1955 {"stabn", s_mips_stab, 'n'},
754e2bb9 1956 {"stabs", s_mips_stab, 's'},
252b5132
RH
1957 {"text", s_change_sec, 't'},
1958 {"word", s_cons, 2},
add56521 1959
add56521 1960 { "extern", ecoff_directive_extern, 0},
add56521 1961
43841e91 1962 { NULL, NULL, 0 },
252b5132
RH
1963};
1964
e972090a
NC
1965static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1966{
beae10d5
KH
1967 /* These pseudo-ops should be defined by the object file format.
1968 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1969 {"aent", s_mips_ent, 1},
1970 {"bgnb", s_ignore, 0},
1971 {"end", s_mips_end, 0},
1972 {"endb", s_ignore, 0},
1973 {"ent", s_mips_ent, 0},
c5dd6aab 1974 {"file", s_mips_file, 0},
252b5132
RH
1975 {"fmask", s_mips_mask, 'F'},
1976 {"frame", s_mips_frame, 0},
c5dd6aab 1977 {"loc", s_mips_loc, 0},
252b5132
RH
1978 {"mask", s_mips_mask, 'R'},
1979 {"verstamp", s_ignore, 0},
43841e91 1980 { NULL, NULL, 0 },
252b5132
RH
1981};
1982
3ae8dd8d
MR
1983/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1984 purpose of the `.dc.a' internal pseudo-op. */
1985
1986int
1987mips_address_bytes (void)
1988{
919731af 1989 file_mips_check_options ();
3ae8dd8d
MR
1990 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1991}
1992
17a2f251 1993extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1994
1995void
17a2f251 1996mips_pop_insert (void)
252b5132
RH
1997{
1998 pop_insert (mips_pseudo_table);
1999 if (! ECOFF_DEBUGGING)
2000 pop_insert (mips_nonecoff_pseudo_table);
2001}
2002\f
2003/* Symbols labelling the current insn. */
2004
e972090a
NC
2005struct insn_label_list
2006{
252b5132
RH
2007 struct insn_label_list *next;
2008 symbolS *label;
2009};
2010
252b5132 2011static struct insn_label_list *free_insn_labels;
742a56fe 2012#define label_list tc_segment_info_data.labels
252b5132 2013
17a2f251 2014static void mips_clear_insn_labels (void);
df58fc94
RS
2015static void mips_mark_labels (void);
2016static void mips_compressed_mark_labels (void);
252b5132
RH
2017
2018static inline void
17a2f251 2019mips_clear_insn_labels (void)
252b5132 2020{
ed9e98c2 2021 struct insn_label_list **pl;
a8dbcb85 2022 segment_info_type *si;
252b5132 2023
a8dbcb85
TS
2024 if (now_seg)
2025 {
2026 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
2027 ;
3739860c 2028
a8dbcb85
TS
2029 si = seg_info (now_seg);
2030 *pl = si->label_list;
2031 si->label_list = NULL;
2032 }
252b5132 2033}
a8dbcb85 2034
df58fc94
RS
2035/* Mark instruction labels in MIPS16/microMIPS mode. */
2036
2037static inline void
2038mips_mark_labels (void)
2039{
2040 if (HAVE_CODE_COMPRESSION)
2041 mips_compressed_mark_labels ();
2042}
252b5132
RH
2043\f
2044static char *expr_end;
2045
e423441d 2046/* An expression in a macro instruction. This is set by mips_ip and
b0e6f033 2047 mips16_ip and when populated is always an O_constant. */
252b5132
RH
2048
2049static expressionS imm_expr;
252b5132 2050
77bd4346
RS
2051/* The relocatable field in an instruction and the relocs associated
2052 with it. These variables are used for instructions like LUI and
2053 JAL as well as true offsets. They are also used for address
2054 operands in macros. */
252b5132 2055
77bd4346 2056static expressionS offset_expr;
f6688943
TS
2057static bfd_reloc_code_real_type offset_reloc[3]
2058 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 2059
df58fc94
RS
2060/* This is set to the resulting size of the instruction to be produced
2061 by mips16_ip if an explicit extension is used or by mips_ip if an
2062 explicit size is supplied. */
252b5132 2063
df58fc94 2064static unsigned int forced_insn_length;
252b5132 2065
e1b47bd5
RS
2066/* True if we are assembling an instruction. All dot symbols defined during
2067 this time should be treated as code labels. */
2068
2069static bfd_boolean mips_assembling_insn;
2070
ecb4347a
DJ
2071/* The pdr segment for per procedure frame/regmask info. Not used for
2072 ECOFF debugging. */
252b5132
RH
2073
2074static segT pdr_seg;
252b5132 2075
e013f690
TS
2076/* The default target format to use. */
2077
aeffff67
RS
2078#if defined (TE_FreeBSD)
2079#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
2080#elif defined (TE_TMIPS)
2081#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
2082#else
2083#define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
2084#endif
2085
e013f690 2086const char *
17a2f251 2087mips_target_format (void)
e013f690
TS
2088{
2089 switch (OUTPUT_FLAVOR)
2090 {
e013f690 2091 case bfd_target_elf_flavour:
0a44bf69
RS
2092#ifdef TE_VXWORKS
2093 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
2094 return (target_big_endian
2095 ? "elf32-bigmips-vxworks"
2096 : "elf32-littlemips-vxworks");
2097#endif
e013f690 2098 return (target_big_endian
cfe86eaa 2099 ? (HAVE_64BIT_OBJECTS
aeffff67 2100 ? ELF_TARGET ("elf64-", "big")
cfe86eaa 2101 : (HAVE_NEWABI
aeffff67
RS
2102 ? ELF_TARGET ("elf32-n", "big")
2103 : ELF_TARGET ("elf32-", "big")))
cfe86eaa 2104 : (HAVE_64BIT_OBJECTS
aeffff67 2105 ? ELF_TARGET ("elf64-", "little")
cfe86eaa 2106 : (HAVE_NEWABI
aeffff67
RS
2107 ? ELF_TARGET ("elf32-n", "little")
2108 : ELF_TARGET ("elf32-", "little"))));
e013f690
TS
2109 default:
2110 abort ();
2111 return NULL;
2112 }
2113}
2114
c6278170
RS
2115/* Return the ISA revision that is currently in use, or 0 if we are
2116 generating code for MIPS V or below. */
2117
2118static int
2119mips_isa_rev (void)
2120{
2121 if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
2122 return 2;
2123
ae52f483
AB
2124 if (mips_opts.isa == ISA_MIPS32R3 || mips_opts.isa == ISA_MIPS64R3)
2125 return 3;
2126
2127 if (mips_opts.isa == ISA_MIPS32R5 || mips_opts.isa == ISA_MIPS64R5)
2128 return 5;
2129
7361da2c
AB
2130 if (mips_opts.isa == ISA_MIPS32R6 || mips_opts.isa == ISA_MIPS64R6)
2131 return 6;
2132
c6278170
RS
2133 /* microMIPS implies revision 2 or above. */
2134 if (mips_opts.micromips)
2135 return 2;
2136
2137 if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
2138 return 1;
2139
2140 return 0;
2141}
2142
2143/* Return the mask of all ASEs that are revisions of those in FLAGS. */
2144
2145static unsigned int
2146mips_ase_mask (unsigned int flags)
2147{
2148 unsigned int i;
2149
2150 for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
2151 if (flags & mips_ase_groups[i])
2152 flags |= mips_ase_groups[i];
2153 return flags;
2154}
2155
2156/* Check whether the current ISA supports ASE. Issue a warning if
2157 appropriate. */
2158
2159static void
2160mips_check_isa_supports_ase (const struct mips_ase *ase)
2161{
2162 const char *base;
2163 int min_rev, size;
2164 static unsigned int warned_isa;
2165 static unsigned int warned_fp32;
2166
2167 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
2168 min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
2169 else
2170 min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
2171 if ((min_rev < 0 || mips_isa_rev () < min_rev)
2172 && (warned_isa & ase->flags) != ase->flags)
2173 {
2174 warned_isa |= ase->flags;
2175 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2176 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2177 if (min_rev < 0)
1661c76c 2178 as_warn (_("the %d-bit %s architecture does not support the"
c6278170
RS
2179 " `%s' extension"), size, base, ase->name);
2180 else
1661c76c 2181 as_warn (_("the `%s' extension requires %s%d revision %d or greater"),
c6278170
RS
2182 ase->name, base, size, min_rev);
2183 }
7361da2c
AB
2184 else if ((ase->rem_rev > 0 && mips_isa_rev () >= ase->rem_rev)
2185 && (warned_isa & ase->flags) != ase->flags)
2186 {
2187 warned_isa |= ase->flags;
2188 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2189 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2190 as_warn (_("the `%s' extension was removed in %s%d revision %d"),
2191 ase->name, base, size, ase->rem_rev);
2192 }
2193
c6278170 2194 if ((ase->flags & FP64_ASES)
0b35dfee 2195 && mips_opts.fp != 64
c6278170
RS
2196 && (warned_fp32 & ase->flags) != ase->flags)
2197 {
2198 warned_fp32 |= ase->flags;
1661c76c 2199 as_warn (_("the `%s' extension requires 64-bit FPRs"), ase->name);
c6278170
RS
2200 }
2201}
2202
2203/* Check all enabled ASEs to see whether they are supported by the
2204 chosen architecture. */
2205
2206static void
2207mips_check_isa_supports_ases (void)
2208{
2209 unsigned int i, mask;
2210
2211 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2212 {
2213 mask = mips_ase_mask (mips_ases[i].flags);
2214 if ((mips_opts.ase & mask) == mips_ases[i].flags)
2215 mips_check_isa_supports_ase (&mips_ases[i]);
2216 }
2217}
2218
2219/* Set the state of ASE to ENABLED_P. Return the mask of ASE_* flags
2220 that were affected. */
2221
2222static unsigned int
919731af 2223mips_set_ase (const struct mips_ase *ase, struct mips_set_options *opts,
2224 bfd_boolean enabled_p)
c6278170
RS
2225{
2226 unsigned int mask;
2227
2228 mask = mips_ase_mask (ase->flags);
919731af 2229 opts->ase &= ~mask;
92cebb3d
MR
2230
2231 /* Clear combination ASE flags, which need to be recalculated based on
2232 updated regular ASE settings. */
41cee089 2233 opts->ase &= ~(ASE_MIPS16E2_MT | ASE_XPA_VIRT | ASE_EVA_R6);
92cebb3d 2234
c6278170 2235 if (enabled_p)
919731af 2236 opts->ase |= ase->flags;
25499ac7 2237
9785fc2a
MR
2238 /* The Virtualization ASE has eXtended Physical Addressing (XPA)
2239 instructions which are only valid when both ASEs are enabled.
2240 This sets the ASE_XPA_VIRT flag when both ASEs are present. */
2241 if ((opts->ase & (ASE_XPA | ASE_VIRT)) == (ASE_XPA | ASE_VIRT))
2242 {
2243 opts->ase |= ASE_XPA_VIRT;
2244 mask |= ASE_XPA_VIRT;
2245 }
25499ac7
MR
2246 if ((opts->ase & (ASE_MIPS16E2 | ASE_MT)) == (ASE_MIPS16E2 | ASE_MT))
2247 {
2248 opts->ase |= ASE_MIPS16E2_MT;
2249 mask |= ASE_MIPS16E2_MT;
2250 }
2251
41cee089
FS
2252 /* The EVA Extension has instructions which are only valid when the R6 ISA
2253 is enabled. This sets the ASE_EVA_R6 flag when both EVA and R6 ISA are
2254 present. */
2255 if (((opts->ase & ASE_EVA) != 0) && ISA_IS_R6 (opts->isa))
2256 {
2257 opts->ase |= ASE_EVA_R6;
2258 mask |= ASE_EVA_R6;
2259 }
2260
c6278170
RS
2261 return mask;
2262}
2263
2264/* Return the ASE called NAME, or null if none. */
2265
2266static const struct mips_ase *
2267mips_lookup_ase (const char *name)
2268{
2269 unsigned int i;
2270
2271 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2272 if (strcmp (name, mips_ases[i].name) == 0)
2273 return &mips_ases[i];
2274 return NULL;
2275}
2276
df58fc94 2277/* Return the length of a microMIPS instruction in bytes. If bits of
100b4f2e
MR
2278 the mask beyond the low 16 are 0, then it is a 16-bit instruction,
2279 otherwise it is a 32-bit instruction. */
df58fc94
RS
2280
2281static inline unsigned int
2282micromips_insn_length (const struct mips_opcode *mo)
2283{
7fd53920 2284 return mips_opcode_32bit_p (mo) ? 4 : 2;
df58fc94
RS
2285}
2286
5c04167a
RS
2287/* Return the length of MIPS16 instruction OPCODE. */
2288
2289static inline unsigned int
2290mips16_opcode_length (unsigned long opcode)
2291{
2292 return (opcode >> 16) == 0 ? 2 : 4;
2293}
2294
1e915849
RS
2295/* Return the length of instruction INSN. */
2296
2297static inline unsigned int
2298insn_length (const struct mips_cl_insn *insn)
2299{
df58fc94
RS
2300 if (mips_opts.micromips)
2301 return micromips_insn_length (insn->insn_mo);
2302 else if (mips_opts.mips16)
5c04167a 2303 return mips16_opcode_length (insn->insn_opcode);
df58fc94 2304 else
1e915849 2305 return 4;
1e915849
RS
2306}
2307
2308/* Initialise INSN from opcode entry MO. Leave its position unspecified. */
2309
2310static void
2311create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
2312{
2313 size_t i;
2314
2315 insn->insn_mo = mo;
1e915849
RS
2316 insn->insn_opcode = mo->match;
2317 insn->frag = NULL;
2318 insn->where = 0;
2319 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2320 insn->fixp[i] = NULL;
2321 insn->fixed_p = (mips_opts.noreorder > 0);
2322 insn->noreorder_p = (mips_opts.noreorder > 0);
2323 insn->mips16_absolute_jump_p = 0;
15be625d 2324 insn->complete_p = 0;
e407c74b 2325 insn->cleared_p = 0;
1e915849
RS
2326}
2327
fc76e730
RS
2328/* Get a list of all the operands in INSN. */
2329
2330static const struct mips_operand_array *
2331insn_operands (const struct mips_cl_insn *insn)
2332{
2333 if (insn->insn_mo >= &mips_opcodes[0]
2334 && insn->insn_mo < &mips_opcodes[NUMOPCODES])
2335 return &mips_operands[insn->insn_mo - &mips_opcodes[0]];
2336
2337 if (insn->insn_mo >= &mips16_opcodes[0]
2338 && insn->insn_mo < &mips16_opcodes[bfd_mips16_num_opcodes])
2339 return &mips16_operands[insn->insn_mo - &mips16_opcodes[0]];
2340
2341 if (insn->insn_mo >= &micromips_opcodes[0]
2342 && insn->insn_mo < &micromips_opcodes[bfd_micromips_num_opcodes])
2343 return &micromips_operands[insn->insn_mo - &micromips_opcodes[0]];
2344
2345 abort ();
2346}
2347
2348/* Get a description of operand OPNO of INSN. */
2349
2350static const struct mips_operand *
2351insn_opno (const struct mips_cl_insn *insn, unsigned opno)
2352{
2353 const struct mips_operand_array *operands;
2354
2355 operands = insn_operands (insn);
2356 if (opno >= MAX_OPERANDS || !operands->operand[opno])
2357 abort ();
2358 return operands->operand[opno];
2359}
2360
e077a1c8
RS
2361/* Install UVAL as the value of OPERAND in INSN. */
2362
2363static inline void
2364insn_insert_operand (struct mips_cl_insn *insn,
2365 const struct mips_operand *operand, unsigned int uval)
2366{
25499ac7
MR
2367 if (mips_opts.mips16
2368 && operand->type == OP_INT && operand->lsb == 0
2369 && mips_opcode_32bit_p (insn->insn_mo))
2370 insn->insn_opcode |= mips16_immed_extend (uval, operand->size);
2371 else
2372 insn->insn_opcode = mips_insert_operand (operand, insn->insn_opcode, uval);
e077a1c8
RS
2373}
2374
fc76e730
RS
2375/* Extract the value of OPERAND from INSN. */
2376
2377static inline unsigned
2378insn_extract_operand (const struct mips_cl_insn *insn,
2379 const struct mips_operand *operand)
2380{
2381 return mips_extract_operand (operand, insn->insn_opcode);
2382}
2383
df58fc94 2384/* Record the current MIPS16/microMIPS mode in now_seg. */
742a56fe
RS
2385
2386static void
df58fc94 2387mips_record_compressed_mode (void)
742a56fe
RS
2388{
2389 segment_info_type *si;
2390
2391 si = seg_info (now_seg);
2392 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2393 si->tc_segment_info_data.mips16 = mips_opts.mips16;
df58fc94
RS
2394 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2395 si->tc_segment_info_data.micromips = mips_opts.micromips;
742a56fe
RS
2396}
2397
4d68580a
RS
2398/* Read a standard MIPS instruction from BUF. */
2399
2400static unsigned long
2401read_insn (char *buf)
2402{
2403 if (target_big_endian)
2404 return bfd_getb32 ((bfd_byte *) buf);
2405 else
2406 return bfd_getl32 ((bfd_byte *) buf);
2407}
2408
2409/* Write standard MIPS instruction INSN to BUF. Return a pointer to
2410 the next byte. */
2411
2412static char *
2413write_insn (char *buf, unsigned int insn)
2414{
2415 md_number_to_chars (buf, insn, 4);
2416 return buf + 4;
2417}
2418
2419/* Read a microMIPS or MIPS16 opcode from BUF, given that it
2420 has length LENGTH. */
2421
2422static unsigned long
2423read_compressed_insn (char *buf, unsigned int length)
2424{
2425 unsigned long insn;
2426 unsigned int i;
2427
2428 insn = 0;
2429 for (i = 0; i < length; i += 2)
2430 {
2431 insn <<= 16;
2432 if (target_big_endian)
2433 insn |= bfd_getb16 ((char *) buf);
2434 else
2435 insn |= bfd_getl16 ((char *) buf);
2436 buf += 2;
2437 }
2438 return insn;
2439}
2440
5c04167a
RS
2441/* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2442 instruction is LENGTH bytes long. Return a pointer to the next byte. */
2443
2444static char *
2445write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2446{
2447 unsigned int i;
2448
2449 for (i = 0; i < length; i += 2)
2450 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2451 return buf + length;
2452}
2453
1e915849
RS
2454/* Install INSN at the location specified by its "frag" and "where" fields. */
2455
2456static void
2457install_insn (const struct mips_cl_insn *insn)
2458{
2459 char *f = insn->frag->fr_literal + insn->where;
5c04167a
RS
2460 if (HAVE_CODE_COMPRESSION)
2461 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
1e915849 2462 else
4d68580a 2463 write_insn (f, insn->insn_opcode);
df58fc94 2464 mips_record_compressed_mode ();
1e915849
RS
2465}
2466
2467/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
2468 and install the opcode in the new location. */
2469
2470static void
2471move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2472{
2473 size_t i;
2474
2475 insn->frag = frag;
2476 insn->where = where;
2477 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2478 if (insn->fixp[i] != NULL)
2479 {
2480 insn->fixp[i]->fx_frag = frag;
2481 insn->fixp[i]->fx_where = where;
2482 }
2483 install_insn (insn);
2484}
2485
2486/* Add INSN to the end of the output. */
2487
2488static void
2489add_fixed_insn (struct mips_cl_insn *insn)
2490{
2491 char *f = frag_more (insn_length (insn));
2492 move_insn (insn, frag_now, f - frag_now->fr_literal);
2493}
2494
2495/* Start a variant frag and move INSN to the start of the variant part,
2496 marking it as fixed. The other arguments are as for frag_var. */
2497
2498static void
2499add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2500 relax_substateT subtype, symbolS *symbol, offsetT offset)
2501{
2502 frag_grow (max_chars);
2503 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2504 insn->fixed_p = 1;
2505 frag_var (rs_machine_dependent, max_chars, var,
2506 subtype, symbol, offset, NULL);
2507}
2508
2509/* Insert N copies of INSN into the history buffer, starting at
2510 position FIRST. Neither FIRST nor N need to be clipped. */
2511
2512static void
2513insert_into_history (unsigned int first, unsigned int n,
2514 const struct mips_cl_insn *insn)
2515{
2516 if (mips_relax.sequence != 2)
2517 {
2518 unsigned int i;
2519
2520 for (i = ARRAY_SIZE (history); i-- > first;)
2521 if (i >= first + n)
2522 history[i] = history[i - n];
2523 else
2524 history[i] = *insn;
2525 }
2526}
2527
e3de51ce
RS
2528/* Clear the error in insn_error. */
2529
2530static void
2531clear_insn_error (void)
2532{
2533 memset (&insn_error, 0, sizeof (insn_error));
2534}
2535
2536/* Possibly record error message MSG for the current instruction.
2537 If the error is about a particular argument, ARGNUM is the 1-based
2538 number of that argument, otherwise it is 0. FORMAT is the format
2539 of MSG. Return true if MSG was used, false if the current message
2540 was kept. */
2541
2542static bfd_boolean
2543set_insn_error_format (int argnum, enum mips_insn_error_format format,
2544 const char *msg)
2545{
2546 if (argnum == 0)
2547 {
2548 /* Give priority to errors against specific arguments, and to
2549 the first whole-instruction message. */
2550 if (insn_error.msg)
2551 return FALSE;
2552 }
2553 else
2554 {
2555 /* Keep insn_error if it is against a later argument. */
2556 if (argnum < insn_error.min_argnum)
2557 return FALSE;
2558
2559 /* If both errors are against the same argument but are different,
2560 give up on reporting a specific error for this argument.
2561 See the comment about mips_insn_error for details. */
2562 if (argnum == insn_error.min_argnum
2563 && insn_error.msg
2564 && strcmp (insn_error.msg, msg) != 0)
2565 {
2566 insn_error.msg = 0;
2567 insn_error.min_argnum += 1;
2568 return FALSE;
2569 }
2570 }
2571 insn_error.min_argnum = argnum;
2572 insn_error.format = format;
2573 insn_error.msg = msg;
2574 return TRUE;
2575}
2576
2577/* Record an instruction error with no % format fields. ARGNUM and MSG are
2578 as for set_insn_error_format. */
2579
2580static void
2581set_insn_error (int argnum, const char *msg)
2582{
2583 set_insn_error_format (argnum, ERR_FMT_PLAIN, msg);
2584}
2585
2586/* Record an instruction error with one %d field I. ARGNUM and MSG are
2587 as for set_insn_error_format. */
2588
2589static void
2590set_insn_error_i (int argnum, const char *msg, int i)
2591{
2592 if (set_insn_error_format (argnum, ERR_FMT_I, msg))
2593 insn_error.u.i = i;
2594}
2595
2596/* Record an instruction error with two %s fields S1 and S2. ARGNUM and MSG
2597 are as for set_insn_error_format. */
2598
2599static void
2600set_insn_error_ss (int argnum, const char *msg, const char *s1, const char *s2)
2601{
2602 if (set_insn_error_format (argnum, ERR_FMT_SS, msg))
2603 {
2604 insn_error.u.ss[0] = s1;
2605 insn_error.u.ss[1] = s2;
2606 }
2607}
2608
2609/* Report the error in insn_error, which is against assembly code STR. */
2610
2611static void
2612report_insn_error (const char *str)
2613{
e1fa0163 2614 const char *msg = concat (insn_error.msg, " `%s'", NULL);
e3de51ce 2615
e3de51ce
RS
2616 switch (insn_error.format)
2617 {
2618 case ERR_FMT_PLAIN:
2619 as_bad (msg, str);
2620 break;
2621
2622 case ERR_FMT_I:
2623 as_bad (msg, insn_error.u.i, str);
2624 break;
2625
2626 case ERR_FMT_SS:
2627 as_bad (msg, insn_error.u.ss[0], insn_error.u.ss[1], str);
2628 break;
2629 }
e1fa0163
NC
2630
2631 free ((char *) msg);
e3de51ce
RS
2632}
2633
71400594
RS
2634/* Initialize vr4120_conflicts. There is a bit of duplication here:
2635 the idea is to make it obvious at a glance that each errata is
2636 included. */
2637
2638static void
2639init_vr4120_conflicts (void)
2640{
2641#define CONFLICT(FIRST, SECOND) \
2642 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2643
2644 /* Errata 21 - [D]DIV[U] after [D]MACC */
2645 CONFLICT (MACC, DIV);
2646 CONFLICT (DMACC, DIV);
2647
2648 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
2649 CONFLICT (DMULT, DMULT);
2650 CONFLICT (DMULT, DMACC);
2651 CONFLICT (DMACC, DMULT);
2652 CONFLICT (DMACC, DMACC);
2653
2654 /* Errata 24 - MT{LO,HI} after [D]MACC */
2655 CONFLICT (MACC, MTHILO);
2656 CONFLICT (DMACC, MTHILO);
2657
2658 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2659 instruction is executed immediately after a MACC or DMACC
2660 instruction, the result of [either instruction] is incorrect." */
2661 CONFLICT (MACC, MULT);
2662 CONFLICT (MACC, DMULT);
2663 CONFLICT (DMACC, MULT);
2664 CONFLICT (DMACC, DMULT);
2665
2666 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2667 executed immediately after a DMULT, DMULTU, DIV, DIVU,
2668 DDIV or DDIVU instruction, the result of the MACC or
2669 DMACC instruction is incorrect.". */
2670 CONFLICT (DMULT, MACC);
2671 CONFLICT (DMULT, DMACC);
2672 CONFLICT (DIV, MACC);
2673 CONFLICT (DIV, DMACC);
2674
2675#undef CONFLICT
2676}
2677
707bfff6
TS
2678struct regname {
2679 const char *name;
2680 unsigned int num;
2681};
2682
14daeee3 2683#define RNUM_MASK 0x00000ff
56d438b1 2684#define RTYPE_MASK 0x0ffff00
14daeee3
RS
2685#define RTYPE_NUM 0x0000100
2686#define RTYPE_FPU 0x0000200
2687#define RTYPE_FCC 0x0000400
2688#define RTYPE_VEC 0x0000800
2689#define RTYPE_GP 0x0001000
2690#define RTYPE_CP0 0x0002000
2691#define RTYPE_PC 0x0004000
2692#define RTYPE_ACC 0x0008000
2693#define RTYPE_CCC 0x0010000
2694#define RTYPE_VI 0x0020000
2695#define RTYPE_VF 0x0040000
2696#define RTYPE_R5900_I 0x0080000
2697#define RTYPE_R5900_Q 0x0100000
2698#define RTYPE_R5900_R 0x0200000
2699#define RTYPE_R5900_ACC 0x0400000
56d438b1 2700#define RTYPE_MSA 0x0800000
14daeee3 2701#define RWARN 0x8000000
707bfff6
TS
2702
2703#define GENERIC_REGISTER_NUMBERS \
2704 {"$0", RTYPE_NUM | 0}, \
2705 {"$1", RTYPE_NUM | 1}, \
2706 {"$2", RTYPE_NUM | 2}, \
2707 {"$3", RTYPE_NUM | 3}, \
2708 {"$4", RTYPE_NUM | 4}, \
2709 {"$5", RTYPE_NUM | 5}, \
2710 {"$6", RTYPE_NUM | 6}, \
2711 {"$7", RTYPE_NUM | 7}, \
2712 {"$8", RTYPE_NUM | 8}, \
2713 {"$9", RTYPE_NUM | 9}, \
2714 {"$10", RTYPE_NUM | 10}, \
2715 {"$11", RTYPE_NUM | 11}, \
2716 {"$12", RTYPE_NUM | 12}, \
2717 {"$13", RTYPE_NUM | 13}, \
2718 {"$14", RTYPE_NUM | 14}, \
2719 {"$15", RTYPE_NUM | 15}, \
2720 {"$16", RTYPE_NUM | 16}, \
2721 {"$17", RTYPE_NUM | 17}, \
2722 {"$18", RTYPE_NUM | 18}, \
2723 {"$19", RTYPE_NUM | 19}, \
2724 {"$20", RTYPE_NUM | 20}, \
2725 {"$21", RTYPE_NUM | 21}, \
2726 {"$22", RTYPE_NUM | 22}, \
2727 {"$23", RTYPE_NUM | 23}, \
2728 {"$24", RTYPE_NUM | 24}, \
2729 {"$25", RTYPE_NUM | 25}, \
2730 {"$26", RTYPE_NUM | 26}, \
2731 {"$27", RTYPE_NUM | 27}, \
2732 {"$28", RTYPE_NUM | 28}, \
2733 {"$29", RTYPE_NUM | 29}, \
2734 {"$30", RTYPE_NUM | 30}, \
3739860c 2735 {"$31", RTYPE_NUM | 31}
707bfff6
TS
2736
2737#define FPU_REGISTER_NAMES \
2738 {"$f0", RTYPE_FPU | 0}, \
2739 {"$f1", RTYPE_FPU | 1}, \
2740 {"$f2", RTYPE_FPU | 2}, \
2741 {"$f3", RTYPE_FPU | 3}, \
2742 {"$f4", RTYPE_FPU | 4}, \
2743 {"$f5", RTYPE_FPU | 5}, \
2744 {"$f6", RTYPE_FPU | 6}, \
2745 {"$f7", RTYPE_FPU | 7}, \
2746 {"$f8", RTYPE_FPU | 8}, \
2747 {"$f9", RTYPE_FPU | 9}, \
2748 {"$f10", RTYPE_FPU | 10}, \
2749 {"$f11", RTYPE_FPU | 11}, \
2750 {"$f12", RTYPE_FPU | 12}, \
2751 {"$f13", RTYPE_FPU | 13}, \
2752 {"$f14", RTYPE_FPU | 14}, \
2753 {"$f15", RTYPE_FPU | 15}, \
2754 {"$f16", RTYPE_FPU | 16}, \
2755 {"$f17", RTYPE_FPU | 17}, \
2756 {"$f18", RTYPE_FPU | 18}, \
2757 {"$f19", RTYPE_FPU | 19}, \
2758 {"$f20", RTYPE_FPU | 20}, \
2759 {"$f21", RTYPE_FPU | 21}, \
2760 {"$f22", RTYPE_FPU | 22}, \
2761 {"$f23", RTYPE_FPU | 23}, \
2762 {"$f24", RTYPE_FPU | 24}, \
2763 {"$f25", RTYPE_FPU | 25}, \
2764 {"$f26", RTYPE_FPU | 26}, \
2765 {"$f27", RTYPE_FPU | 27}, \
2766 {"$f28", RTYPE_FPU | 28}, \
2767 {"$f29", RTYPE_FPU | 29}, \
2768 {"$f30", RTYPE_FPU | 30}, \
2769 {"$f31", RTYPE_FPU | 31}
2770
2771#define FPU_CONDITION_CODE_NAMES \
2772 {"$fcc0", RTYPE_FCC | 0}, \
2773 {"$fcc1", RTYPE_FCC | 1}, \
2774 {"$fcc2", RTYPE_FCC | 2}, \
2775 {"$fcc3", RTYPE_FCC | 3}, \
2776 {"$fcc4", RTYPE_FCC | 4}, \
2777 {"$fcc5", RTYPE_FCC | 5}, \
2778 {"$fcc6", RTYPE_FCC | 6}, \
2779 {"$fcc7", RTYPE_FCC | 7}
2780
2781#define COPROC_CONDITION_CODE_NAMES \
2782 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
2783 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
2784 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
2785 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
2786 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
2787 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
2788 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
2789 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
2790
2791#define N32N64_SYMBOLIC_REGISTER_NAMES \
2792 {"$a4", RTYPE_GP | 8}, \
2793 {"$a5", RTYPE_GP | 9}, \
2794 {"$a6", RTYPE_GP | 10}, \
2795 {"$a7", RTYPE_GP | 11}, \
2796 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
2797 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
2798 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
2799 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
2800 {"$t0", RTYPE_GP | 12}, \
2801 {"$t1", RTYPE_GP | 13}, \
2802 {"$t2", RTYPE_GP | 14}, \
2803 {"$t3", RTYPE_GP | 15}
2804
2805#define O32_SYMBOLIC_REGISTER_NAMES \
2806 {"$t0", RTYPE_GP | 8}, \
2807 {"$t1", RTYPE_GP | 9}, \
2808 {"$t2", RTYPE_GP | 10}, \
2809 {"$t3", RTYPE_GP | 11}, \
2810 {"$t4", RTYPE_GP | 12}, \
2811 {"$t5", RTYPE_GP | 13}, \
2812 {"$t6", RTYPE_GP | 14}, \
2813 {"$t7", RTYPE_GP | 15}, \
2814 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2815 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2816 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
3739860c 2817 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
707bfff6 2818
6f2117ba 2819/* Remaining symbolic register names. */
707bfff6
TS
2820#define SYMBOLIC_REGISTER_NAMES \
2821 {"$zero", RTYPE_GP | 0}, \
2822 {"$at", RTYPE_GP | 1}, \
2823 {"$AT", RTYPE_GP | 1}, \
2824 {"$v0", RTYPE_GP | 2}, \
2825 {"$v1", RTYPE_GP | 3}, \
2826 {"$a0", RTYPE_GP | 4}, \
2827 {"$a1", RTYPE_GP | 5}, \
2828 {"$a2", RTYPE_GP | 6}, \
2829 {"$a3", RTYPE_GP | 7}, \
2830 {"$s0", RTYPE_GP | 16}, \
2831 {"$s1", RTYPE_GP | 17}, \
2832 {"$s2", RTYPE_GP | 18}, \
2833 {"$s3", RTYPE_GP | 19}, \
2834 {"$s4", RTYPE_GP | 20}, \
2835 {"$s5", RTYPE_GP | 21}, \
2836 {"$s6", RTYPE_GP | 22}, \
2837 {"$s7", RTYPE_GP | 23}, \
2838 {"$t8", RTYPE_GP | 24}, \
2839 {"$t9", RTYPE_GP | 25}, \
2840 {"$k0", RTYPE_GP | 26}, \
2841 {"$kt0", RTYPE_GP | 26}, \
2842 {"$k1", RTYPE_GP | 27}, \
2843 {"$kt1", RTYPE_GP | 27}, \
2844 {"$gp", RTYPE_GP | 28}, \
2845 {"$sp", RTYPE_GP | 29}, \
2846 {"$s8", RTYPE_GP | 30}, \
2847 {"$fp", RTYPE_GP | 30}, \
2848 {"$ra", RTYPE_GP | 31}
2849
2850#define MIPS16_SPECIAL_REGISTER_NAMES \
2851 {"$pc", RTYPE_PC | 0}
2852
2853#define MDMX_VECTOR_REGISTER_NAMES \
6f2117ba
PH
2854 /* {"$v0", RTYPE_VEC | 0}, Clash with REG 2 above. */ \
2855 /* {"$v1", RTYPE_VEC | 1}, Clash with REG 3 above. */ \
707bfff6
TS
2856 {"$v2", RTYPE_VEC | 2}, \
2857 {"$v3", RTYPE_VEC | 3}, \
2858 {"$v4", RTYPE_VEC | 4}, \
2859 {"$v5", RTYPE_VEC | 5}, \
2860 {"$v6", RTYPE_VEC | 6}, \
2861 {"$v7", RTYPE_VEC | 7}, \
2862 {"$v8", RTYPE_VEC | 8}, \
2863 {"$v9", RTYPE_VEC | 9}, \
2864 {"$v10", RTYPE_VEC | 10}, \
2865 {"$v11", RTYPE_VEC | 11}, \
2866 {"$v12", RTYPE_VEC | 12}, \
2867 {"$v13", RTYPE_VEC | 13}, \
2868 {"$v14", RTYPE_VEC | 14}, \
2869 {"$v15", RTYPE_VEC | 15}, \
2870 {"$v16", RTYPE_VEC | 16}, \
2871 {"$v17", RTYPE_VEC | 17}, \
2872 {"$v18", RTYPE_VEC | 18}, \
2873 {"$v19", RTYPE_VEC | 19}, \
2874 {"$v20", RTYPE_VEC | 20}, \
2875 {"$v21", RTYPE_VEC | 21}, \
2876 {"$v22", RTYPE_VEC | 22}, \
2877 {"$v23", RTYPE_VEC | 23}, \
2878 {"$v24", RTYPE_VEC | 24}, \
2879 {"$v25", RTYPE_VEC | 25}, \
2880 {"$v26", RTYPE_VEC | 26}, \
2881 {"$v27", RTYPE_VEC | 27}, \
2882 {"$v28", RTYPE_VEC | 28}, \
2883 {"$v29", RTYPE_VEC | 29}, \
2884 {"$v30", RTYPE_VEC | 30}, \
2885 {"$v31", RTYPE_VEC | 31}
2886
14daeee3
RS
2887#define R5900_I_NAMES \
2888 {"$I", RTYPE_R5900_I | 0}
2889
2890#define R5900_Q_NAMES \
2891 {"$Q", RTYPE_R5900_Q | 0}
2892
2893#define R5900_R_NAMES \
2894 {"$R", RTYPE_R5900_R | 0}
2895
2896#define R5900_ACC_NAMES \
2897 {"$ACC", RTYPE_R5900_ACC | 0 }
2898
707bfff6
TS
2899#define MIPS_DSP_ACCUMULATOR_NAMES \
2900 {"$ac0", RTYPE_ACC | 0}, \
2901 {"$ac1", RTYPE_ACC | 1}, \
2902 {"$ac2", RTYPE_ACC | 2}, \
2903 {"$ac3", RTYPE_ACC | 3}
2904
2905static const struct regname reg_names[] = {
2906 GENERIC_REGISTER_NUMBERS,
2907 FPU_REGISTER_NAMES,
2908 FPU_CONDITION_CODE_NAMES,
2909 COPROC_CONDITION_CODE_NAMES,
2910
2911 /* The $txx registers depends on the abi,
2912 these will be added later into the symbol table from
3739860c 2913 one of the tables below once mips_abi is set after
707bfff6
TS
2914 parsing of arguments from the command line. */
2915 SYMBOLIC_REGISTER_NAMES,
2916
2917 MIPS16_SPECIAL_REGISTER_NAMES,
2918 MDMX_VECTOR_REGISTER_NAMES,
14daeee3
RS
2919 R5900_I_NAMES,
2920 R5900_Q_NAMES,
2921 R5900_R_NAMES,
2922 R5900_ACC_NAMES,
707bfff6
TS
2923 MIPS_DSP_ACCUMULATOR_NAMES,
2924 {0, 0}
2925};
2926
2927static const struct regname reg_names_o32[] = {
2928 O32_SYMBOLIC_REGISTER_NAMES,
2929 {0, 0}
2930};
2931
2932static const struct regname reg_names_n32n64[] = {
2933 N32N64_SYMBOLIC_REGISTER_NAMES,
2934 {0, 0}
2935};
2936
a92713e6
RS
2937/* Register symbols $v0 and $v1 map to GPRs 2 and 3, but they can also be
2938 interpreted as vector registers 0 and 1. If SYMVAL is the value of one
2939 of these register symbols, return the associated vector register,
2940 otherwise return SYMVAL itself. */
df58fc94 2941
a92713e6
RS
2942static unsigned int
2943mips_prefer_vec_regno (unsigned int symval)
707bfff6 2944{
a92713e6
RS
2945 if ((symval & -2) == (RTYPE_GP | 2))
2946 return RTYPE_VEC | (symval & 1);
2947 return symval;
2948}
2949
14daeee3
RS
2950/* Return true if string [S, E) is a valid register name, storing its
2951 symbol value in *SYMVAL_PTR if so. */
a92713e6
RS
2952
2953static bfd_boolean
14daeee3 2954mips_parse_register_1 (char *s, char *e, unsigned int *symval_ptr)
a92713e6 2955{
707bfff6 2956 char save_c;
14daeee3 2957 symbolS *symbol;
707bfff6
TS
2958
2959 /* Terminate name. */
2960 save_c = *e;
2961 *e = '\0';
2962
a92713e6
RS
2963 /* Look up the name. */
2964 symbol = symbol_find (s);
2965 *e = save_c;
2966
2967 if (!symbol || S_GET_SEGMENT (symbol) != reg_section)
2968 return FALSE;
2969
14daeee3
RS
2970 *symval_ptr = S_GET_VALUE (symbol);
2971 return TRUE;
2972}
2973
2974/* Return true if the string at *SPTR is a valid register name. Allow it
2975 to have a VU0-style channel suffix of the form x?y?z?w? if CHANNELS_PTR
2976 is nonnull.
2977
2978 When returning true, move *SPTR past the register, store the
2979 register's symbol value in *SYMVAL_PTR and the channel mask in
2980 *CHANNELS_PTR (if nonnull). The symbol value includes the register
2981 number (RNUM_MASK) and register type (RTYPE_MASK). The channel mask
2982 is a 4-bit value of the form XYZW and is 0 if no suffix was given. */
2983
2984static bfd_boolean
2985mips_parse_register (char **sptr, unsigned int *symval_ptr,
2986 unsigned int *channels_ptr)
2987{
2988 char *s, *e, *m;
2989 const char *q;
2990 unsigned int channels, symval, bit;
2991
2992 /* Find end of name. */
2993 s = e = *sptr;
2994 if (is_name_beginner (*e))
2995 ++e;
2996 while (is_part_of_name (*e))
2997 ++e;
2998
2999 channels = 0;
3000 if (!mips_parse_register_1 (s, e, &symval))
3001 {
3002 if (!channels_ptr)
3003 return FALSE;
3004
3005 /* Eat characters from the end of the string that are valid
3006 channel suffixes. The preceding register must be $ACC or
3007 end with a digit, so there is no ambiguity. */
3008 bit = 1;
3009 m = e;
3010 for (q = "wzyx"; *q; q++, bit <<= 1)
3011 if (m > s && m[-1] == *q)
3012 {
3013 --m;
3014 channels |= bit;
3015 }
3016
3017 if (channels == 0
3018 || !mips_parse_register_1 (s, m, &symval)
3019 || (symval & (RTYPE_VI | RTYPE_VF | RTYPE_R5900_ACC)) == 0)
3020 return FALSE;
3021 }
3022
a92713e6 3023 *sptr = e;
14daeee3
RS
3024 *symval_ptr = symval;
3025 if (channels_ptr)
3026 *channels_ptr = channels;
a92713e6
RS
3027 return TRUE;
3028}
3029
3030/* Check if SPTR points at a valid register specifier according to TYPES.
3031 If so, then return 1, advance S to consume the specifier and store
3032 the register's number in REGNOP, otherwise return 0. */
3033
3034static int
3035reg_lookup (char **s, unsigned int types, unsigned int *regnop)
3036{
3037 unsigned int regno;
3038
14daeee3 3039 if (mips_parse_register (s, &regno, NULL))
707bfff6 3040 {
a92713e6
RS
3041 if (types & RTYPE_VEC)
3042 regno = mips_prefer_vec_regno (regno);
3043 if (regno & types)
3044 regno &= RNUM_MASK;
3045 else
3046 regno = ~0;
707bfff6 3047 }
a92713e6 3048 else
707bfff6 3049 {
a92713e6 3050 if (types & RWARN)
1661c76c 3051 as_warn (_("unrecognized register name `%s'"), *s);
a92713e6 3052 regno = ~0;
707bfff6 3053 }
707bfff6 3054 if (regnop)
a92713e6
RS
3055 *regnop = regno;
3056 return regno <= RNUM_MASK;
707bfff6
TS
3057}
3058
14daeee3
RS
3059/* Parse a VU0 "x?y?z?w?" channel mask at S and store the associated
3060 mask in *CHANNELS. Return a pointer to the first unconsumed character. */
3061
3062static char *
3063mips_parse_vu0_channels (char *s, unsigned int *channels)
3064{
3065 unsigned int i;
3066
3067 *channels = 0;
3068 for (i = 0; i < 4; i++)
3069 if (*s == "xyzw"[i])
3070 {
3071 *channels |= 1 << (3 - i);
3072 ++s;
3073 }
3074 return s;
3075}
3076
a92713e6
RS
3077/* Token types for parsed operand lists. */
3078enum mips_operand_token_type {
3079 /* A plain register, e.g. $f2. */
3080 OT_REG,
df58fc94 3081
14daeee3
RS
3082 /* A 4-bit XYZW channel mask. */
3083 OT_CHANNELS,
3084
56d438b1
CF
3085 /* A constant vector index, e.g. [1]. */
3086 OT_INTEGER_INDEX,
3087
3088 /* A register vector index, e.g. [$2]. */
3089 OT_REG_INDEX,
df58fc94 3090
a92713e6
RS
3091 /* A continuous range of registers, e.g. $s0-$s4. */
3092 OT_REG_RANGE,
3093
3094 /* A (possibly relocated) expression. */
3095 OT_INTEGER,
3096
3097 /* A floating-point value. */
3098 OT_FLOAT,
3099
3100 /* A single character. This can be '(', ')' or ',', but '(' only appears
3101 before OT_REGs. */
3102 OT_CHAR,
3103
14daeee3
RS
3104 /* A doubled character, either "--" or "++". */
3105 OT_DOUBLE_CHAR,
3106
a92713e6
RS
3107 /* The end of the operand list. */
3108 OT_END
3109};
3110
3111/* A parsed operand token. */
3112struct mips_operand_token
3113{
3114 /* The type of token. */
3115 enum mips_operand_token_type type;
3116 union
3117 {
56d438b1 3118 /* The register symbol value for an OT_REG or OT_REG_INDEX. */
a92713e6
RS
3119 unsigned int regno;
3120
14daeee3
RS
3121 /* The 4-bit channel mask for an OT_CHANNEL_SUFFIX. */
3122 unsigned int channels;
3123
56d438b1
CF
3124 /* The integer value of an OT_INTEGER_INDEX. */
3125 addressT index;
a92713e6
RS
3126
3127 /* The two register symbol values involved in an OT_REG_RANGE. */
3128 struct {
3129 unsigned int regno1;
3130 unsigned int regno2;
3131 } reg_range;
3132
3133 /* The value of an OT_INTEGER. The value is represented as an
3134 expression and the relocation operators that were applied to
3135 that expression. The reloc entries are BFD_RELOC_UNUSED if no
3136 relocation operators were used. */
3137 struct {
3138 expressionS value;
3139 bfd_reloc_code_real_type relocs[3];
3140 } integer;
3141
3142 /* The binary data for an OT_FLOAT constant, and the number of bytes
3143 in the constant. */
3144 struct {
3145 unsigned char data[8];
3146 int length;
3147 } flt;
3148
14daeee3 3149 /* The character represented by an OT_CHAR or OT_DOUBLE_CHAR. */
a92713e6
RS
3150 char ch;
3151 } u;
3152};
3153
3154/* An obstack used to construct lists of mips_operand_tokens. */
3155static struct obstack mips_operand_tokens;
3156
3157/* Give TOKEN type TYPE and add it to mips_operand_tokens. */
3158
3159static void
3160mips_add_token (struct mips_operand_token *token,
3161 enum mips_operand_token_type type)
3162{
3163 token->type = type;
3164 obstack_grow (&mips_operand_tokens, token, sizeof (*token));
3165}
3166
3167/* Check whether S is '(' followed by a register name. Add OT_CHAR
3168 and OT_REG tokens for them if so, and return a pointer to the first
3169 unconsumed character. Return null otherwise. */
3170
3171static char *
3172mips_parse_base_start (char *s)
3173{
3174 struct mips_operand_token token;
14daeee3
RS
3175 unsigned int regno, channels;
3176 bfd_boolean decrement_p;
df58fc94 3177
a92713e6
RS
3178 if (*s != '(')
3179 return 0;
3180
3181 ++s;
3182 SKIP_SPACE_TABS (s);
14daeee3
RS
3183
3184 /* Only match "--" as part of a base expression. In other contexts "--X"
3185 is a double negative. */
3186 decrement_p = (s[0] == '-' && s[1] == '-');
3187 if (decrement_p)
3188 {
3189 s += 2;
3190 SKIP_SPACE_TABS (s);
3191 }
3192
3193 /* Allow a channel specifier because that leads to better error messages
3194 than treating something like "$vf0x++" as an expression. */
3195 if (!mips_parse_register (&s, &regno, &channels))
a92713e6
RS
3196 return 0;
3197
3198 token.u.ch = '(';
3199 mips_add_token (&token, OT_CHAR);
3200
14daeee3
RS
3201 if (decrement_p)
3202 {
3203 token.u.ch = '-';
3204 mips_add_token (&token, OT_DOUBLE_CHAR);
3205 }
3206
a92713e6
RS
3207 token.u.regno = regno;
3208 mips_add_token (&token, OT_REG);
3209
14daeee3
RS
3210 if (channels)
3211 {
3212 token.u.channels = channels;
3213 mips_add_token (&token, OT_CHANNELS);
3214 }
3215
3216 /* For consistency, only match "++" as part of base expressions too. */
3217 SKIP_SPACE_TABS (s);
3218 if (s[0] == '+' && s[1] == '+')
3219 {
3220 s += 2;
3221 token.u.ch = '+';
3222 mips_add_token (&token, OT_DOUBLE_CHAR);
3223 }
3224
a92713e6
RS
3225 return s;
3226}
3227
3228/* Parse one or more tokens from S. Return a pointer to the first
3229 unconsumed character on success. Return null if an error was found
3230 and store the error text in insn_error. FLOAT_FORMAT is as for
3231 mips_parse_arguments. */
3232
3233static char *
3234mips_parse_argument_token (char *s, char float_format)
3235{
6d4af3c2
AM
3236 char *end, *save_in;
3237 const char *err;
14daeee3 3238 unsigned int regno1, regno2, channels;
a92713e6
RS
3239 struct mips_operand_token token;
3240
3241 /* First look for "($reg", since we want to treat that as an
3242 OT_CHAR and OT_REG rather than an expression. */
3243 end = mips_parse_base_start (s);
3244 if (end)
3245 return end;
3246
3247 /* Handle other characters that end up as OT_CHARs. */
3248 if (*s == ')' || *s == ',')
3249 {
3250 token.u.ch = *s;
3251 mips_add_token (&token, OT_CHAR);
3252 ++s;
3253 return s;
3254 }
3255
3256 /* Handle tokens that start with a register. */
14daeee3 3257 if (mips_parse_register (&s, &regno1, &channels))
df58fc94 3258 {
14daeee3
RS
3259 if (channels)
3260 {
3261 /* A register and a VU0 channel suffix. */
3262 token.u.regno = regno1;
3263 mips_add_token (&token, OT_REG);
3264
3265 token.u.channels = channels;
3266 mips_add_token (&token, OT_CHANNELS);
3267 return s;
3268 }
3269
a92713e6
RS
3270 SKIP_SPACE_TABS (s);
3271 if (*s == '-')
df58fc94 3272 {
a92713e6
RS
3273 /* A register range. */
3274 ++s;
3275 SKIP_SPACE_TABS (s);
14daeee3 3276 if (!mips_parse_register (&s, &regno2, NULL))
a92713e6 3277 {
1661c76c 3278 set_insn_error (0, _("invalid register range"));
a92713e6
RS
3279 return 0;
3280 }
df58fc94 3281
a92713e6
RS
3282 token.u.reg_range.regno1 = regno1;
3283 token.u.reg_range.regno2 = regno2;
3284 mips_add_token (&token, OT_REG_RANGE);
3285 return s;
3286 }
a92713e6 3287
56d438b1
CF
3288 /* Add the register itself. */
3289 token.u.regno = regno1;
3290 mips_add_token (&token, OT_REG);
3291
3292 /* Check for a vector index. */
3293 if (*s == '[')
3294 {
a92713e6
RS
3295 ++s;
3296 SKIP_SPACE_TABS (s);
56d438b1
CF
3297 if (mips_parse_register (&s, &token.u.regno, NULL))
3298 mips_add_token (&token, OT_REG_INDEX);
3299 else
a92713e6 3300 {
56d438b1
CF
3301 expressionS element;
3302
3303 my_getExpression (&element, s);
3304 if (element.X_op != O_constant)
3305 {
3306 set_insn_error (0, _("vector element must be constant"));
3307 return 0;
3308 }
3309 s = expr_end;
3310 token.u.index = element.X_add_number;
3311 mips_add_token (&token, OT_INTEGER_INDEX);
a92713e6 3312 }
a92713e6
RS
3313 SKIP_SPACE_TABS (s);
3314 if (*s != ']')
3315 {
1661c76c 3316 set_insn_error (0, _("missing `]'"));
a92713e6
RS
3317 return 0;
3318 }
3319 ++s;
df58fc94 3320 }
a92713e6 3321 return s;
df58fc94
RS
3322 }
3323
a92713e6
RS
3324 if (float_format)
3325 {
3326 /* First try to treat expressions as floats. */
3327 save_in = input_line_pointer;
3328 input_line_pointer = s;
3329 err = md_atof (float_format, (char *) token.u.flt.data,
3330 &token.u.flt.length);
3331 end = input_line_pointer;
3332 input_line_pointer = save_in;
3333 if (err && *err)
3334 {
e3de51ce 3335 set_insn_error (0, err);
a92713e6
RS
3336 return 0;
3337 }
3338 if (s != end)
3339 {
3340 mips_add_token (&token, OT_FLOAT);
3341 return end;
3342 }
3343 }
3344
3345 /* Treat everything else as an integer expression. */
3346 token.u.integer.relocs[0] = BFD_RELOC_UNUSED;
3347 token.u.integer.relocs[1] = BFD_RELOC_UNUSED;
3348 token.u.integer.relocs[2] = BFD_RELOC_UNUSED;
3349 my_getSmallExpression (&token.u.integer.value, token.u.integer.relocs, s);
3350 s = expr_end;
3351 mips_add_token (&token, OT_INTEGER);
3352 return s;
3353}
3354
3355/* S points to the operand list for an instruction. FLOAT_FORMAT is 'f'
3356 if expressions should be treated as 32-bit floating-point constants,
3357 'd' if they should be treated as 64-bit floating-point constants,
3358 or 0 if they should be treated as integer expressions (the usual case).
3359
3360 Return a list of tokens on success, otherwise return 0. The caller
3361 must obstack_free the list after use. */
3362
3363static struct mips_operand_token *
3364mips_parse_arguments (char *s, char float_format)
3365{
3366 struct mips_operand_token token;
3367
3368 SKIP_SPACE_TABS (s);
3369 while (*s)
3370 {
3371 s = mips_parse_argument_token (s, float_format);
3372 if (!s)
3373 {
3374 obstack_free (&mips_operand_tokens,
3375 obstack_finish (&mips_operand_tokens));
3376 return 0;
3377 }
3378 SKIP_SPACE_TABS (s);
3379 }
3380 mips_add_token (&token, OT_END);
3381 return (struct mips_operand_token *) obstack_finish (&mips_operand_tokens);
df58fc94
RS
3382}
3383
d301a56b
RS
3384/* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
3385 and architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
037b32b9
AN
3386
3387static bfd_boolean
f79e2745 3388is_opcode_valid (const struct mips_opcode *mo)
037b32b9
AN
3389{
3390 int isa = mips_opts.isa;
846ef2d0 3391 int ase = mips_opts.ase;
037b32b9 3392 int fp_s, fp_d;
c6278170 3393 unsigned int i;
037b32b9 3394
be0fcbee 3395 if (ISA_HAS_64BIT_REGS (isa))
c6278170
RS
3396 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3397 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3398 ase |= mips_ases[i].flags64;
037b32b9 3399
d301a56b 3400 if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
037b32b9
AN
3401 return FALSE;
3402
3403 /* Check whether the instruction or macro requires single-precision or
3404 double-precision floating-point support. Note that this information is
3405 stored differently in the opcode table for insns and macros. */
3406 if (mo->pinfo == INSN_MACRO)
3407 {
3408 fp_s = mo->pinfo2 & INSN2_M_FP_S;
3409 fp_d = mo->pinfo2 & INSN2_M_FP_D;
3410 }
3411 else
3412 {
3413 fp_s = mo->pinfo & FP_S;
3414 fp_d = mo->pinfo & FP_D;
3415 }
3416
3417 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
3418 return FALSE;
3419
3420 if (fp_s && mips_opts.soft_float)
3421 return FALSE;
3422
3423 return TRUE;
3424}
3425
3426/* Return TRUE if the MIPS16 opcode MO is valid on the currently
3427 selected ISA and architecture. */
3428
3429static bfd_boolean
3430is_opcode_valid_16 (const struct mips_opcode *mo)
3431{
25499ac7
MR
3432 int isa = mips_opts.isa;
3433 int ase = mips_opts.ase;
3434 unsigned int i;
3435
3436 if (ISA_HAS_64BIT_REGS (isa))
3437 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3438 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3439 ase |= mips_ases[i].flags64;
3440
3441 return opcode_is_member (mo, isa, ase, mips_opts.arch);
037b32b9
AN
3442}
3443
df58fc94 3444/* Return TRUE if the size of the microMIPS opcode MO matches one
7fd53920
MR
3445 explicitly requested. Always TRUE in the standard MIPS mode.
3446 Use is_size_valid_16 for MIPS16 opcodes. */
df58fc94
RS
3447
3448static bfd_boolean
3449is_size_valid (const struct mips_opcode *mo)
3450{
3451 if (!mips_opts.micromips)
3452 return TRUE;
3453
833794fc
MR
3454 if (mips_opts.insn32)
3455 {
3456 if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
3457 return FALSE;
3458 if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
3459 return FALSE;
3460 }
df58fc94
RS
3461 if (!forced_insn_length)
3462 return TRUE;
3463 if (mo->pinfo == INSN_MACRO)
3464 return FALSE;
3465 return forced_insn_length == micromips_insn_length (mo);
3466}
3467
7fd53920
MR
3468/* Return TRUE if the size of the MIPS16 opcode MO matches one
3469 explicitly requested. */
3470
3471static bfd_boolean
3472is_size_valid_16 (const struct mips_opcode *mo)
3473{
3474 if (!forced_insn_length)
3475 return TRUE;
3476 if (mo->pinfo == INSN_MACRO)
3477 return FALSE;
3478 if (forced_insn_length == 2 && mips_opcode_32bit_p (mo))
3479 return FALSE;
0674ee5d
MR
3480 if (forced_insn_length == 4 && (mo->pinfo2 & INSN2_SHORT_ONLY))
3481 return FALSE;
7fd53920
MR
3482 return TRUE;
3483}
3484
df58fc94 3485/* Return TRUE if the microMIPS opcode MO is valid for the delay slot
e64af278
MR
3486 of the preceding instruction. Always TRUE in the standard MIPS mode.
3487
3488 We don't accept macros in 16-bit delay slots to avoid a case where
3489 a macro expansion fails because it relies on a preceding 32-bit real
3490 instruction to have matched and does not handle the operands correctly.
3491 The only macros that may expand to 16-bit instructions are JAL that
3492 cannot be placed in a delay slot anyway, and corner cases of BALIGN
3493 and BGT (that likewise cannot be placed in a delay slot) that decay to
3494 a NOP. In all these cases the macros precede any corresponding real
3495 instruction definitions in the opcode table, so they will match in the
3496 second pass where the size of the delay slot is ignored and therefore
3497 produce correct code. */
df58fc94
RS
3498
3499static bfd_boolean
3500is_delay_slot_valid (const struct mips_opcode *mo)
3501{
3502 if (!mips_opts.micromips)
3503 return TRUE;
3504
3505 if (mo->pinfo == INSN_MACRO)
c06dec14 3506 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
df58fc94
RS
3507 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
3508 && micromips_insn_length (mo) != 4)
3509 return FALSE;
3510 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
3511 && micromips_insn_length (mo) != 2)
3512 return FALSE;
3513
3514 return TRUE;
3515}
3516
fc76e730
RS
3517/* For consistency checking, verify that all bits of OPCODE are specified
3518 either by the match/mask part of the instruction definition, or by the
3519 operand list. Also build up a list of operands in OPERANDS.
3520
3521 INSN_BITS says which bits of the instruction are significant.
3522 If OPCODE is a standard or microMIPS instruction, DECODE_OPERAND
3523 provides the mips_operand description of each operand. DECODE_OPERAND
3524 is null for MIPS16 instructions. */
ab902481
RS
3525
3526static int
3527validate_mips_insn (const struct mips_opcode *opcode,
3528 unsigned long insn_bits,
fc76e730
RS
3529 const struct mips_operand *(*decode_operand) (const char *),
3530 struct mips_operand_array *operands)
ab902481
RS
3531{
3532 const char *s;
fc76e730 3533 unsigned long used_bits, doubled, undefined, opno, mask;
ab902481
RS
3534 const struct mips_operand *operand;
3535
fc76e730
RS
3536 mask = (opcode->pinfo == INSN_MACRO ? 0 : opcode->mask);
3537 if ((mask & opcode->match) != opcode->match)
ab902481
RS
3538 {
3539 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
3540 opcode->name, opcode->args);
3541 return 0;
3542 }
3543 used_bits = 0;
fc76e730 3544 opno = 0;
14daeee3
RS
3545 if (opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX)
3546 used_bits = mips_insert_operand (&mips_vu0_channel_mask, used_bits, -1);
ab902481
RS
3547 for (s = opcode->args; *s; ++s)
3548 switch (*s)
3549 {
3550 case ',':
3551 case '(':
3552 case ')':
3553 break;
3554
14daeee3
RS
3555 case '#':
3556 s++;
3557 break;
3558
ab902481 3559 default:
fc76e730 3560 if (!decode_operand)
7fd53920 3561 operand = decode_mips16_operand (*s, mips_opcode_32bit_p (opcode));
fc76e730
RS
3562 else
3563 operand = decode_operand (s);
3564 if (!operand && opcode->pinfo != INSN_MACRO)
ab902481
RS
3565 {
3566 as_bad (_("internal: unknown operand type: %s %s"),
3567 opcode->name, opcode->args);
3568 return 0;
3569 }
fc76e730
RS
3570 gas_assert (opno < MAX_OPERANDS);
3571 operands->operand[opno] = operand;
25499ac7
MR
3572 if (!decode_operand && operand
3573 && operand->type == OP_INT && operand->lsb == 0
3574 && mips_opcode_32bit_p (opcode))
3575 used_bits |= mips16_immed_extend (-1, operand->size);
3576 else if (operand && operand->type != OP_VU0_MATCH_SUFFIX)
fc76e730 3577 {
14daeee3 3578 used_bits = mips_insert_operand (operand, used_bits, -1);
fc76e730
RS
3579 if (operand->type == OP_MDMX_IMM_REG)
3580 /* Bit 5 is the format selector (OB vs QH). The opcode table
3581 has separate entries for each format. */
3582 used_bits &= ~(1 << (operand->lsb + 5));
3583 if (operand->type == OP_ENTRY_EXIT_LIST)
3584 used_bits &= ~(mask & 0x700);
38bf472a
MR
3585 /* interAptiv MR2 SAVE/RESTORE instructions have a discontiguous
3586 operand field that cannot be fully described with LSB/SIZE. */
3587 if (operand->type == OP_SAVE_RESTORE_LIST && operand->lsb == 6)
3588 used_bits &= ~0x6000;
fc76e730 3589 }
ab902481 3590 /* Skip prefix characters. */
7361da2c 3591 if (decode_operand && (*s == '+' || *s == 'm' || *s == '-'))
ab902481 3592 ++s;
fc76e730 3593 opno += 1;
ab902481
RS
3594 break;
3595 }
fc76e730 3596 doubled = used_bits & mask & insn_bits;
ab902481
RS
3597 if (doubled)
3598 {
3599 as_bad (_("internal: bad mips opcode (bits 0x%08lx doubly defined):"
3600 " %s %s"), doubled, opcode->name, opcode->args);
3601 return 0;
3602 }
fc76e730 3603 used_bits |= mask;
ab902481 3604 undefined = ~used_bits & insn_bits;
fc76e730 3605 if (opcode->pinfo != INSN_MACRO && undefined)
ab902481
RS
3606 {
3607 as_bad (_("internal: bad mips opcode (bits 0x%08lx undefined): %s %s"),
3608 undefined, opcode->name, opcode->args);
3609 return 0;
3610 }
3611 used_bits &= ~insn_bits;
3612 if (used_bits)
3613 {
3614 as_bad (_("internal: bad mips opcode (bits 0x%08lx defined): %s %s"),
3615 used_bits, opcode->name, opcode->args);
3616 return 0;
3617 }
3618 return 1;
3619}
3620
fc76e730
RS
3621/* The MIPS16 version of validate_mips_insn. */
3622
3623static int
3624validate_mips16_insn (const struct mips_opcode *opcode,
3625 struct mips_operand_array *operands)
3626{
7fd53920 3627 unsigned long insn_bits = mips_opcode_32bit_p (opcode) ? 0xffffffff : 0xffff;
fc76e730 3628
7fd53920 3629 return validate_mips_insn (opcode, insn_bits, 0, operands);
fc76e730
RS
3630}
3631
ab902481
RS
3632/* The microMIPS version of validate_mips_insn. */
3633
3634static int
fc76e730
RS
3635validate_micromips_insn (const struct mips_opcode *opc,
3636 struct mips_operand_array *operands)
ab902481
RS
3637{
3638 unsigned long insn_bits;
3639 unsigned long major;
3640 unsigned int length;
3641
fc76e730
RS
3642 if (opc->pinfo == INSN_MACRO)
3643 return validate_mips_insn (opc, 0xffffffff, decode_micromips_operand,
3644 operands);
3645
ab902481
RS
3646 length = micromips_insn_length (opc);
3647 if (length != 2 && length != 4)
3648 {
1661c76c 3649 as_bad (_("internal error: bad microMIPS opcode (incorrect length: %u): "
ab902481
RS
3650 "%s %s"), length, opc->name, opc->args);
3651 return 0;
3652 }
3653 major = opc->match >> (10 + 8 * (length - 2));
3654 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
3655 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
3656 {
1661c76c 3657 as_bad (_("internal error: bad microMIPS opcode "
ab902481
RS
3658 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
3659 return 0;
3660 }
3661
3662 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
3663 insn_bits = 1 << 4 * length;
3664 insn_bits <<= 4 * length;
3665 insn_bits -= 1;
fc76e730
RS
3666 return validate_mips_insn (opc, insn_bits, decode_micromips_operand,
3667 operands);
ab902481
RS
3668}
3669
707bfff6
TS
3670/* This function is called once, at assembler startup time. It should set up
3671 all the tables, etc. that the MD part of the assembler will need. */
156c2f8b 3672
252b5132 3673void
17a2f251 3674md_begin (void)
252b5132 3675{
3994f87e 3676 const char *retval = NULL;
156c2f8b 3677 int i = 0;
252b5132 3678 int broken = 0;
1f25f5d3 3679
0a44bf69
RS
3680 if (mips_pic != NO_PIC)
3681 {
3682 if (g_switch_seen && g_switch_value != 0)
3683 as_bad (_("-G may not be used in position-independent code"));
3684 g_switch_value = 0;
3685 }
00acd688
CM
3686 else if (mips_abicalls)
3687 {
3688 if (g_switch_seen && g_switch_value != 0)
3689 as_bad (_("-G may not be used with abicalls"));
3690 g_switch_value = 0;
3691 }
0a44bf69 3692
0b35dfee 3693 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
1661c76c 3694 as_warn (_("could not set architecture and machine"));
252b5132 3695
252b5132
RH
3696 op_hash = hash_new ();
3697
fc76e730 3698 mips_operands = XCNEWVEC (struct mips_operand_array, NUMOPCODES);
252b5132
RH
3699 for (i = 0; i < NUMOPCODES;)
3700 {
3701 const char *name = mips_opcodes[i].name;
3702
17a2f251 3703 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
3704 if (retval != NULL)
3705 {
3706 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
3707 mips_opcodes[i].name, retval);
3708 /* Probably a memory allocation problem? Give up now. */
1661c76c 3709 as_fatal (_("broken assembler, no assembly attempted"));
252b5132
RH
3710 }
3711 do
3712 {
fc76e730
RS
3713 if (!validate_mips_insn (&mips_opcodes[i], 0xffffffff,
3714 decode_mips_operand, &mips_operands[i]))
3715 broken = 1;
6f2117ba 3716
fc76e730 3717 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
252b5132 3718 {
fc76e730
RS
3719 create_insn (&nop_insn, mips_opcodes + i);
3720 if (mips_fix_loongson2f_nop)
3721 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
3722 nop_insn.fixed_p = 1;
252b5132 3723 }
6f2117ba
PH
3724
3725 if (sync_insn.insn_mo == NULL && strcmp (name, "sync") == 0)
3726 create_insn (&sync_insn, mips_opcodes + i);
3727
252b5132
RH
3728 ++i;
3729 }
3730 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
3731 }
3732
3733 mips16_op_hash = hash_new ();
fc76e730
RS
3734 mips16_operands = XCNEWVEC (struct mips_operand_array,
3735 bfd_mips16_num_opcodes);
252b5132
RH
3736
3737 i = 0;
3738 while (i < bfd_mips16_num_opcodes)
3739 {
3740 const char *name = mips16_opcodes[i].name;
3741
17a2f251 3742 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
3743 if (retval != NULL)
3744 as_fatal (_("internal: can't hash `%s': %s"),
3745 mips16_opcodes[i].name, retval);
3746 do
3747 {
fc76e730
RS
3748 if (!validate_mips16_insn (&mips16_opcodes[i], &mips16_operands[i]))
3749 broken = 1;
1e915849
RS
3750 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3751 {
3752 create_insn (&mips16_nop_insn, mips16_opcodes + i);
3753 mips16_nop_insn.fixed_p = 1;
3754 }
252b5132
RH
3755 ++i;
3756 }
3757 while (i < bfd_mips16_num_opcodes
3758 && strcmp (mips16_opcodes[i].name, name) == 0);
3759 }
3760
df58fc94 3761 micromips_op_hash = hash_new ();
fc76e730
RS
3762 micromips_operands = XCNEWVEC (struct mips_operand_array,
3763 bfd_micromips_num_opcodes);
df58fc94
RS
3764
3765 i = 0;
3766 while (i < bfd_micromips_num_opcodes)
3767 {
3768 const char *name = micromips_opcodes[i].name;
3769
3770 retval = hash_insert (micromips_op_hash, name,
3771 (void *) &micromips_opcodes[i]);
3772 if (retval != NULL)
3773 as_fatal (_("internal: can't hash `%s': %s"),
3774 micromips_opcodes[i].name, retval);
3775 do
fc76e730
RS
3776 {
3777 struct mips_cl_insn *micromips_nop_insn;
3778
3779 if (!validate_micromips_insn (&micromips_opcodes[i],
3780 &micromips_operands[i]))
3781 broken = 1;
3782
3783 if (micromips_opcodes[i].pinfo != INSN_MACRO)
3784 {
3785 if (micromips_insn_length (micromips_opcodes + i) == 2)
3786 micromips_nop_insn = &micromips_nop16_insn;
3787 else if (micromips_insn_length (micromips_opcodes + i) == 4)
3788 micromips_nop_insn = &micromips_nop32_insn;
3789 else
3790 continue;
3791
3792 if (micromips_nop_insn->insn_mo == NULL
3793 && strcmp (name, "nop") == 0)
3794 {
3795 create_insn (micromips_nop_insn, micromips_opcodes + i);
3796 micromips_nop_insn->fixed_p = 1;
3797 }
3798 }
3799 }
df58fc94
RS
3800 while (++i < bfd_micromips_num_opcodes
3801 && strcmp (micromips_opcodes[i].name, name) == 0);
3802 }
3803
252b5132 3804 if (broken)
1661c76c 3805 as_fatal (_("broken assembler, no assembly attempted"));
252b5132
RH
3806
3807 /* We add all the general register names to the symbol table. This
3808 helps us detect invalid uses of them. */
3739860c 3809 for (i = 0; reg_names[i].name; i++)
707bfff6 3810 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
8fc4ee9b 3811 reg_names[i].num, /* & RNUM_MASK, */
707bfff6
TS
3812 &zero_address_frag));
3813 if (HAVE_NEWABI)
3739860c 3814 for (i = 0; reg_names_n32n64[i].name; i++)
707bfff6 3815 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
8fc4ee9b 3816 reg_names_n32n64[i].num, /* & RNUM_MASK, */
252b5132 3817 &zero_address_frag));
707bfff6 3818 else
3739860c 3819 for (i = 0; reg_names_o32[i].name; i++)
707bfff6 3820 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
8fc4ee9b 3821 reg_names_o32[i].num, /* & RNUM_MASK, */
6047c971 3822 &zero_address_frag));
6047c971 3823
14daeee3
RS
3824 for (i = 0; i < 32; i++)
3825 {
92fce9bd 3826 char regname[6];
14daeee3
RS
3827
3828 /* R5900 VU0 floating-point register. */
92fce9bd 3829 sprintf (regname, "$vf%d", i);
14daeee3
RS
3830 symbol_table_insert (symbol_new (regname, reg_section,
3831 RTYPE_VF | i, &zero_address_frag));
3832
3833 /* R5900 VU0 integer register. */
92fce9bd 3834 sprintf (regname, "$vi%d", i);
14daeee3
RS
3835 symbol_table_insert (symbol_new (regname, reg_section,
3836 RTYPE_VI | i, &zero_address_frag));
3837
56d438b1 3838 /* MSA register. */
92fce9bd 3839 sprintf (regname, "$w%d", i);
56d438b1
CF
3840 symbol_table_insert (symbol_new (regname, reg_section,
3841 RTYPE_MSA | i, &zero_address_frag));
14daeee3
RS
3842 }
3843
a92713e6
RS
3844 obstack_init (&mips_operand_tokens);
3845
7d10b47d 3846 mips_no_prev_insn ();
252b5132
RH
3847
3848 mips_gprmask = 0;
3849 mips_cprmask[0] = 0;
3850 mips_cprmask[1] = 0;
3851 mips_cprmask[2] = 0;
3852 mips_cprmask[3] = 0;
3853
3854 /* set the default alignment for the text section (2**2) */
3855 record_alignment (text_section, 2);
3856
4d0d148d 3857 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132 3858
f3ded42a
RS
3859 /* On a native system other than VxWorks, sections must be aligned
3860 to 16 byte boundaries. When configured for an embedded ELF
3861 target, we don't bother. */
3862 if (strncmp (TARGET_OS, "elf", 3) != 0
3863 && strncmp (TARGET_OS, "vxworks", 7) != 0)
252b5132 3864 {
fd361982
AM
3865 bfd_set_section_alignment (text_section, 4);
3866 bfd_set_section_alignment (data_section, 4);
3867 bfd_set_section_alignment (bss_section, 4);
f3ded42a 3868 }
252b5132 3869
f3ded42a
RS
3870 /* Create a .reginfo section for register masks and a .mdebug
3871 section for debugging information. */
3872 {
3873 segT seg;
3874 subsegT subseg;
3875 flagword flags;
3876 segT sec;
3877
3878 seg = now_seg;
3879 subseg = now_subseg;
3880
3881 /* The ABI says this section should be loaded so that the
3882 running program can access it. However, we don't load it
6f2117ba 3883 if we are configured for an embedded target. */
f3ded42a
RS
3884 flags = SEC_READONLY | SEC_DATA;
3885 if (strncmp (TARGET_OS, "elf", 3) != 0)
3886 flags |= SEC_ALLOC | SEC_LOAD;
3887
3888 if (mips_abi != N64_ABI)
252b5132 3889 {
f3ded42a 3890 sec = subseg_new (".reginfo", (subsegT) 0);
bdaaa2e1 3891
fd361982
AM
3892 bfd_set_section_flags (sec, flags);
3893 bfd_set_section_alignment (sec, HAVE_NEWABI ? 3 : 2);
252b5132 3894
f3ded42a
RS
3895 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
3896 }
3897 else
3898 {
3899 /* The 64-bit ABI uses a .MIPS.options section rather than
3900 .reginfo section. */
3901 sec = subseg_new (".MIPS.options", (subsegT) 0);
fd361982
AM
3902 bfd_set_section_flags (sec, flags);
3903 bfd_set_section_alignment (sec, 3);
252b5132 3904
f3ded42a
RS
3905 /* Set up the option header. */
3906 {
3907 Elf_Internal_Options opthdr;
3908 char *f;
3909
3910 opthdr.kind = ODK_REGINFO;
3911 opthdr.size = (sizeof (Elf_External_Options)
3912 + sizeof (Elf64_External_RegInfo));
3913 opthdr.section = 0;
3914 opthdr.info = 0;
3915 f = frag_more (sizeof (Elf_External_Options));
3916 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
3917 (Elf_External_Options *) f);
3918
3919 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
3920 }
3921 }
252b5132 3922
351cdf24 3923 sec = subseg_new (".MIPS.abiflags", (subsegT) 0);
fd361982 3924 bfd_set_section_flags (sec,
351cdf24 3925 SEC_READONLY | SEC_DATA | SEC_ALLOC | SEC_LOAD);
fd361982 3926 bfd_set_section_alignment (sec, 3);
351cdf24
MF
3927 mips_flags_frag = frag_more (sizeof (Elf_External_ABIFlags_v0));
3928
f3ded42a
RS
3929 if (ECOFF_DEBUGGING)
3930 {
3931 sec = subseg_new (".mdebug", (subsegT) 0);
fd361982
AM
3932 bfd_set_section_flags (sec, SEC_HAS_CONTENTS | SEC_READONLY);
3933 bfd_set_section_alignment (sec, 2);
252b5132 3934 }
f3ded42a
RS
3935 else if (mips_flag_pdr)
3936 {
3937 pdr_seg = subseg_new (".pdr", (subsegT) 0);
fd361982
AM
3938 bfd_set_section_flags (pdr_seg,
3939 SEC_READONLY | SEC_RELOC | SEC_DEBUGGING);
3940 bfd_set_section_alignment (pdr_seg, 2);
f3ded42a
RS
3941 }
3942
3943 subseg_set (seg, subseg);
3944 }
252b5132 3945
71400594
RS
3946 if (mips_fix_vr4120)
3947 init_vr4120_conflicts ();
252b5132
RH
3948}
3949
351cdf24
MF
3950static inline void
3951fpabi_incompatible_with (int fpabi, const char *what)
3952{
3953 as_warn (_(".gnu_attribute %d,%d is incompatible with `%s'"),
3954 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3955}
3956
3957static inline void
3958fpabi_requires (int fpabi, const char *what)
3959{
3960 as_warn (_(".gnu_attribute %d,%d requires `%s'"),
3961 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3962}
3963
3964/* Check -mabi and register sizes against the specified FP ABI. */
3965static void
3966check_fpabi (int fpabi)
3967{
351cdf24
MF
3968 switch (fpabi)
3969 {
3970 case Val_GNU_MIPS_ABI_FP_DOUBLE:
ea79f94a
MF
3971 if (file_mips_opts.soft_float)
3972 fpabi_incompatible_with (fpabi, "softfloat");
3973 else if (file_mips_opts.single_float)
3974 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3975 if (file_mips_opts.gp == 64 && file_mips_opts.fp == 32)
3976 fpabi_incompatible_with (fpabi, "gp=64 fp=32");
3977 else if (file_mips_opts.gp == 32 && file_mips_opts.fp == 64)
3978 fpabi_incompatible_with (fpabi, "gp=32 fp=64");
351cdf24
MF
3979 break;
3980
3981 case Val_GNU_MIPS_ABI_FP_XX:
3982 if (mips_abi != O32_ABI)
3983 fpabi_requires (fpabi, "-mabi=32");
ea79f94a
MF
3984 else if (file_mips_opts.soft_float)
3985 fpabi_incompatible_with (fpabi, "softfloat");
3986 else if (file_mips_opts.single_float)
3987 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3988 else if (file_mips_opts.fp != 0)
3989 fpabi_requires (fpabi, "fp=xx");
351cdf24
MF
3990 break;
3991
3992 case Val_GNU_MIPS_ABI_FP_64A:
3993 case Val_GNU_MIPS_ABI_FP_64:
3994 if (mips_abi != O32_ABI)
3995 fpabi_requires (fpabi, "-mabi=32");
ea79f94a
MF
3996 else if (file_mips_opts.soft_float)
3997 fpabi_incompatible_with (fpabi, "softfloat");
3998 else if (file_mips_opts.single_float)
3999 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
4000 else if (file_mips_opts.fp != 64)
4001 fpabi_requires (fpabi, "fp=64");
4002 else if (fpabi == Val_GNU_MIPS_ABI_FP_64 && !file_mips_opts.oddspreg)
4003 fpabi_incompatible_with (fpabi, "nooddspreg");
4004 else if (fpabi == Val_GNU_MIPS_ABI_FP_64A && file_mips_opts.oddspreg)
4005 fpabi_requires (fpabi, "nooddspreg");
351cdf24
MF
4006 break;
4007
4008 case Val_GNU_MIPS_ABI_FP_SINGLE:
4009 if (file_mips_opts.soft_float)
4010 fpabi_incompatible_with (fpabi, "softfloat");
4011 else if (!file_mips_opts.single_float)
4012 fpabi_requires (fpabi, "singlefloat");
4013 break;
4014
4015 case Val_GNU_MIPS_ABI_FP_SOFT:
4016 if (!file_mips_opts.soft_float)
4017 fpabi_requires (fpabi, "softfloat");
4018 break;
4019
4020 case Val_GNU_MIPS_ABI_FP_OLD_64:
4021 as_warn (_(".gnu_attribute %d,%d is no longer supported"),
4022 Tag_GNU_MIPS_ABI_FP, fpabi);
4023 break;
4024
3350cc01
CM
4025 case Val_GNU_MIPS_ABI_FP_NAN2008:
4026 /* Silently ignore compatibility value. */
4027 break;
4028
351cdf24
MF
4029 default:
4030 as_warn (_(".gnu_attribute %d,%d is not a recognized"
4031 " floating-point ABI"), Tag_GNU_MIPS_ABI_FP, fpabi);
4032 break;
4033 }
351cdf24
MF
4034}
4035
919731af 4036/* Perform consistency checks on the current options. */
4037
4038static void
4039mips_check_options (struct mips_set_options *opts, bfd_boolean abi_checks)
4040{
4041 /* Check the size of integer registers agrees with the ABI and ISA. */
4042 if (opts->gp == 64 && !ISA_HAS_64BIT_REGS (opts->isa))
4043 as_bad (_("`gp=64' used with a 32-bit processor"));
4044 else if (abi_checks
4045 && opts->gp == 32 && ABI_NEEDS_64BIT_REGS (mips_abi))
4046 as_bad (_("`gp=32' used with a 64-bit ABI"));
4047 else if (abi_checks
4048 && opts->gp == 64 && ABI_NEEDS_32BIT_REGS (mips_abi))
4049 as_bad (_("`gp=64' used with a 32-bit ABI"));
4050
4051 /* Check the size of the float registers agrees with the ABI and ISA. */
4052 switch (opts->fp)
4053 {
351cdf24
MF
4054 case 0:
4055 if (!CPU_HAS_LDC1_SDC1 (opts->arch))
4056 as_bad (_("`fp=xx' used with a cpu lacking ldc1/sdc1 instructions"));
4057 else if (opts->single_float == 1)
4058 as_bad (_("`fp=xx' cannot be used with `singlefloat'"));
4059 break;
919731af 4060 case 64:
4061 if (!ISA_HAS_64BIT_FPRS (opts->isa))
4062 as_bad (_("`fp=64' used with a 32-bit fpu"));
4063 else if (abi_checks
4064 && ABI_NEEDS_32BIT_REGS (mips_abi)
4065 && !ISA_HAS_MXHC1 (opts->isa))
4066 as_warn (_("`fp=64' used with a 32-bit ABI"));
4067 break;
4068 case 32:
4069 if (abi_checks
4070 && ABI_NEEDS_64BIT_REGS (mips_abi))
4071 as_warn (_("`fp=32' used with a 64-bit ABI"));
5f4678bb 4072 if (ISA_IS_R6 (opts->isa) && opts->single_float == 0)
7361da2c 4073 as_bad (_("`fp=32' used with a MIPS R6 cpu"));
919731af 4074 break;
4075 default:
4076 as_bad (_("Unknown size of floating point registers"));
4077 break;
4078 }
4079
351cdf24
MF
4080 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !opts->oddspreg)
4081 as_bad (_("`nooddspreg` cannot be used with a 64-bit ABI"));
4082
919731af 4083 if (opts->micromips == 1 && opts->mips16 == 1)
1357373c 4084 as_bad (_("`%s' cannot be used with `%s'"), "mips16", "micromips");
5f4678bb 4085 else if (ISA_IS_R6 (opts->isa)
7361da2c
AB
4086 && (opts->micromips == 1
4087 || opts->mips16 == 1))
1357373c 4088 as_fatal (_("`%s' cannot be used with `%s'"),
7361da2c 4089 opts->micromips ? "micromips" : "mips16",
5f4678bb 4090 mips_cpu_info_from_isa (opts->isa)->name);
7361da2c
AB
4091
4092 if (ISA_IS_R6 (opts->isa) && mips_relax_branch)
4093 as_fatal (_("branch relaxation is not supported in `%s'"),
4094 mips_cpu_info_from_isa (opts->isa)->name);
919731af 4095}
4096
4097/* Perform consistency checks on the module level options exactly once.
4098 This is a deferred check that happens:
4099 at the first .set directive
4100 or, at the first pseudo op that generates code (inc .dc.a)
4101 or, at the first instruction
4102 or, at the end. */
4103
4104static void
4105file_mips_check_options (void)
4106{
919731af 4107 if (file_mips_opts_checked)
4108 return;
4109
4110 /* The following code determines the register size.
4111 Similar code was added to GCC 3.3 (see override_options() in
4112 config/mips/mips.c). The GAS and GCC code should be kept in sync
4113 as much as possible. */
4114
4115 if (file_mips_opts.gp < 0)
4116 {
4117 /* Infer the integer register size from the ABI and processor.
4118 Restrict ourselves to 32-bit registers if that's all the
4119 processor has, or if the ABI cannot handle 64-bit registers. */
4120 file_mips_opts.gp = (ABI_NEEDS_32BIT_REGS (mips_abi)
4121 || !ISA_HAS_64BIT_REGS (file_mips_opts.isa))
4122 ? 32 : 64;
4123 }
4124
4125 if (file_mips_opts.fp < 0)
4126 {
4127 /* No user specified float register size.
4128 ??? GAS treats single-float processors as though they had 64-bit
4129 float registers (although it complains when double-precision
4130 instructions are used). As things stand, saying they have 32-bit
4131 registers would lead to spurious "register must be even" messages.
4132 So here we assume float registers are never smaller than the
4133 integer ones. */
4134 if (file_mips_opts.gp == 64)
4135 /* 64-bit integer registers implies 64-bit float registers. */
4136 file_mips_opts.fp = 64;
4137 else if ((file_mips_opts.ase & FP64_ASES)
4138 && ISA_HAS_64BIT_FPRS (file_mips_opts.isa))
4139 /* Handle ASEs that require 64-bit float registers, if possible. */
4140 file_mips_opts.fp = 64;
7361da2c
AB
4141 else if (ISA_IS_R6 (mips_opts.isa))
4142 /* R6 implies 64-bit float registers. */
4143 file_mips_opts.fp = 64;
919731af 4144 else
4145 /* 32-bit float registers. */
4146 file_mips_opts.fp = 32;
4147 }
4148
351cdf24
MF
4149 /* Disable operations on odd-numbered floating-point registers by default
4150 when using the FPXX ABI. */
4151 if (file_mips_opts.oddspreg < 0)
4152 {
4153 if (file_mips_opts.fp == 0)
4154 file_mips_opts.oddspreg = 0;
4155 else
4156 file_mips_opts.oddspreg = 1;
4157 }
4158
919731af 4159 /* End of GCC-shared inference code. */
4160
4161 /* This flag is set when we have a 64-bit capable CPU but use only
4162 32-bit wide registers. Note that EABI does not use it. */
4163 if (ISA_HAS_64BIT_REGS (file_mips_opts.isa)
4164 && ((mips_abi == NO_ABI && file_mips_opts.gp == 32)
4165 || mips_abi == O32_ABI))
4166 mips_32bitmode = 1;
4167
4168 if (file_mips_opts.isa == ISA_MIPS1 && mips_trap)
4169 as_bad (_("trap exception not supported at ISA 1"));
4170
4171 /* If the selected architecture includes support for ASEs, enable
4172 generation of code for them. */
4173 if (file_mips_opts.mips16 == -1)
4174 file_mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_opts.arch)) ? 1 : 0;
4175 if (file_mips_opts.micromips == -1)
4176 file_mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_opts.arch))
4177 ? 1 : 0;
4178
7361da2c
AB
4179 if (mips_nan2008 == -1)
4180 mips_nan2008 = (ISA_HAS_LEGACY_NAN (file_mips_opts.isa)) ? 0 : 1;
4181 else if (!ISA_HAS_LEGACY_NAN (file_mips_opts.isa) && mips_nan2008 == 0)
4182 as_fatal (_("`%s' does not support legacy NaN"),
4183 mips_cpu_info_from_arch (file_mips_opts.arch)->name);
4184
919731af 4185 /* Some ASEs require 64-bit FPRs, so -mfp32 should stop those ASEs from
4186 being selected implicitly. */
4187 if (file_mips_opts.fp != 64)
4188 file_ase_explicit |= ASE_MIPS3D | ASE_MDMX | ASE_MSA;
4189
4190 /* If the user didn't explicitly select or deselect a particular ASE,
4191 use the default setting for the CPU. */
3315614d 4192 file_mips_opts.ase |= (file_mips_opts.init_ase & ~file_ase_explicit);
919731af 4193
4194 /* Set up the current options. These may change throughout assembly. */
4195 mips_opts = file_mips_opts;
4196
4197 mips_check_isa_supports_ases ();
4198 mips_check_options (&file_mips_opts, TRUE);
4199 file_mips_opts_checked = TRUE;
4200
4201 if (!bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
4202 as_warn (_("could not set architecture and machine"));
4203}
4204
252b5132 4205void
17a2f251 4206md_assemble (char *str)
252b5132
RH
4207{
4208 struct mips_cl_insn insn;
f6688943
TS
4209 bfd_reloc_code_real_type unused_reloc[3]
4210 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 4211
919731af 4212 file_mips_check_options ();
4213
252b5132 4214 imm_expr.X_op = O_absent;
252b5132 4215 offset_expr.X_op = O_absent;
f6688943
TS
4216 offset_reloc[0] = BFD_RELOC_UNUSED;
4217 offset_reloc[1] = BFD_RELOC_UNUSED;
4218 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132 4219
e1b47bd5
RS
4220 mips_mark_labels ();
4221 mips_assembling_insn = TRUE;
e3de51ce 4222 clear_insn_error ();
e1b47bd5 4223
252b5132
RH
4224 if (mips_opts.mips16)
4225 mips16_ip (str, &insn);
4226 else
4227 {
4228 mips_ip (str, &insn);
beae10d5
KH
4229 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
4230 str, insn.insn_opcode));
252b5132
RH
4231 }
4232
e3de51ce
RS
4233 if (insn_error.msg)
4234 report_insn_error (str);
e1b47bd5 4235 else if (insn.insn_mo->pinfo == INSN_MACRO)
252b5132 4236 {
584892a6 4237 macro_start ();
252b5132
RH
4238 if (mips_opts.mips16)
4239 mips16_macro (&insn);
4240 else
833794fc 4241 macro (&insn, str);
584892a6 4242 macro_end ();
252b5132
RH
4243 }
4244 else
4245 {
77bd4346 4246 if (offset_expr.X_op != O_absent)
df58fc94 4247 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
252b5132 4248 else
df58fc94 4249 append_insn (&insn, NULL, unused_reloc, FALSE);
252b5132 4250 }
e1b47bd5
RS
4251
4252 mips_assembling_insn = FALSE;
252b5132
RH
4253}
4254
738e5348
RS
4255/* Convenience functions for abstracting away the differences between
4256 MIPS16 and non-MIPS16 relocations. */
4257
4258static inline bfd_boolean
4259mips16_reloc_p (bfd_reloc_code_real_type reloc)
4260{
4261 switch (reloc)
4262 {
4263 case BFD_RELOC_MIPS16_JMP:
4264 case BFD_RELOC_MIPS16_GPREL:
4265 case BFD_RELOC_MIPS16_GOT16:
4266 case BFD_RELOC_MIPS16_CALL16:
4267 case BFD_RELOC_MIPS16_HI16_S:
4268 case BFD_RELOC_MIPS16_HI16:
4269 case BFD_RELOC_MIPS16_LO16:
c9775dde 4270 case BFD_RELOC_MIPS16_16_PCREL_S1:
738e5348
RS
4271 return TRUE;
4272
4273 default:
4274 return FALSE;
4275 }
4276}
4277
df58fc94
RS
4278static inline bfd_boolean
4279micromips_reloc_p (bfd_reloc_code_real_type reloc)
4280{
4281 switch (reloc)
4282 {
4283 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4284 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4285 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4286 case BFD_RELOC_MICROMIPS_GPREL16:
4287 case BFD_RELOC_MICROMIPS_JMP:
4288 case BFD_RELOC_MICROMIPS_HI16:
4289 case BFD_RELOC_MICROMIPS_HI16_S:
4290 case BFD_RELOC_MICROMIPS_LO16:
4291 case BFD_RELOC_MICROMIPS_LITERAL:
4292 case BFD_RELOC_MICROMIPS_GOT16:
4293 case BFD_RELOC_MICROMIPS_CALL16:
4294 case BFD_RELOC_MICROMIPS_GOT_HI16:
4295 case BFD_RELOC_MICROMIPS_GOT_LO16:
4296 case BFD_RELOC_MICROMIPS_CALL_HI16:
4297 case BFD_RELOC_MICROMIPS_CALL_LO16:
4298 case BFD_RELOC_MICROMIPS_SUB:
4299 case BFD_RELOC_MICROMIPS_GOT_PAGE:
4300 case BFD_RELOC_MICROMIPS_GOT_OFST:
4301 case BFD_RELOC_MICROMIPS_GOT_DISP:
4302 case BFD_RELOC_MICROMIPS_HIGHEST:
4303 case BFD_RELOC_MICROMIPS_HIGHER:
4304 case BFD_RELOC_MICROMIPS_SCN_DISP:
4305 case BFD_RELOC_MICROMIPS_JALR:
4306 return TRUE;
4307
4308 default:
4309 return FALSE;
4310 }
4311}
4312
2309ddf2
MR
4313static inline bfd_boolean
4314jmp_reloc_p (bfd_reloc_code_real_type reloc)
4315{
4316 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
4317}
4318
0e9c5a5c
MR
4319static inline bfd_boolean
4320b_reloc_p (bfd_reloc_code_real_type reloc)
4321{
4322 return (reloc == BFD_RELOC_MIPS_26_PCREL_S2
4323 || reloc == BFD_RELOC_MIPS_21_PCREL_S2
4324 || reloc == BFD_RELOC_16_PCREL_S2
c9775dde 4325 || reloc == BFD_RELOC_MIPS16_16_PCREL_S1
0e9c5a5c
MR
4326 || reloc == BFD_RELOC_MICROMIPS_16_PCREL_S1
4327 || reloc == BFD_RELOC_MICROMIPS_10_PCREL_S1
4328 || reloc == BFD_RELOC_MICROMIPS_7_PCREL_S1);
4329}
4330
738e5348
RS
4331static inline bfd_boolean
4332got16_reloc_p (bfd_reloc_code_real_type reloc)
4333{
2309ddf2 4334 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
df58fc94 4335 || reloc == BFD_RELOC_MICROMIPS_GOT16);
738e5348
RS
4336}
4337
4338static inline bfd_boolean
4339hi16_reloc_p (bfd_reloc_code_real_type reloc)
4340{
2309ddf2 4341 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
df58fc94 4342 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
738e5348
RS
4343}
4344
4345static inline bfd_boolean
4346lo16_reloc_p (bfd_reloc_code_real_type reloc)
4347{
2309ddf2 4348 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
df58fc94
RS
4349 || reloc == BFD_RELOC_MICROMIPS_LO16);
4350}
4351
df58fc94
RS
4352static inline bfd_boolean
4353jalr_reloc_p (bfd_reloc_code_real_type reloc)
4354{
2309ddf2 4355 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
738e5348
RS
4356}
4357
f2ae14a1
RS
4358static inline bfd_boolean
4359gprel16_reloc_p (bfd_reloc_code_real_type reloc)
4360{
4361 return (reloc == BFD_RELOC_GPREL16 || reloc == BFD_RELOC_MIPS16_GPREL
4362 || reloc == BFD_RELOC_MICROMIPS_GPREL16);
4363}
4364
2de39019
CM
4365/* Return true if RELOC is a PC-relative relocation that does not have
4366 full address range. */
4367
4368static inline bfd_boolean
4369limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
4370{
4371 switch (reloc)
4372 {
4373 case BFD_RELOC_16_PCREL_S2:
c9775dde 4374 case BFD_RELOC_MIPS16_16_PCREL_S1:
2de39019
CM
4375 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4376 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4377 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
7361da2c
AB
4378 case BFD_RELOC_MIPS_21_PCREL_S2:
4379 case BFD_RELOC_MIPS_26_PCREL_S2:
4380 case BFD_RELOC_MIPS_18_PCREL_S3:
4381 case BFD_RELOC_MIPS_19_PCREL_S2:
2de39019
CM
4382 return TRUE;
4383
b47468a6 4384 case BFD_RELOC_32_PCREL:
7361da2c
AB
4385 case BFD_RELOC_HI16_S_PCREL:
4386 case BFD_RELOC_LO16_PCREL:
b47468a6
CM
4387 return HAVE_64BIT_ADDRESSES;
4388
2de39019
CM
4389 default:
4390 return FALSE;
4391 }
4392}
b47468a6 4393
5919d012 4394/* Return true if the given relocation might need a matching %lo().
0a44bf69
RS
4395 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
4396 need a matching %lo() when applied to local symbols. */
5919d012
RS
4397
4398static inline bfd_boolean
17a2f251 4399reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 4400{
3b91255e 4401 return (HAVE_IN_PLACE_ADDENDS
738e5348 4402 && (hi16_reloc_p (reloc)
0a44bf69
RS
4403 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
4404 all GOT16 relocations evaluate to "G". */
738e5348
RS
4405 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
4406}
4407
4408/* Return the type of %lo() reloc needed by RELOC, given that
4409 reloc_needs_lo_p. */
4410
4411static inline bfd_reloc_code_real_type
4412matching_lo_reloc (bfd_reloc_code_real_type reloc)
4413{
df58fc94
RS
4414 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
4415 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
4416 : BFD_RELOC_LO16));
5919d012
RS
4417}
4418
4419/* Return true if the given fixup is followed by a matching R_MIPS_LO16
4420 relocation. */
4421
4422static inline bfd_boolean
17a2f251 4423fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
4424{
4425 return (fixp->fx_next != NULL
738e5348 4426 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
5919d012
RS
4427 && fixp->fx_addsy == fixp->fx_next->fx_addsy
4428 && fixp->fx_offset == fixp->fx_next->fx_offset);
4429}
4430
462427c4
RS
4431/* Move all labels in LABELS to the current insertion point. TEXT_P
4432 says whether the labels refer to text or data. */
404a8071
RS
4433
4434static void
462427c4 4435mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
404a8071
RS
4436{
4437 struct insn_label_list *l;
4438 valueT val;
4439
462427c4 4440 for (l = labels; l != NULL; l = l->next)
404a8071 4441 {
9c2799c2 4442 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
404a8071
RS
4443 symbol_set_frag (l->label, frag_now);
4444 val = (valueT) frag_now_fix ();
770c0151
FS
4445 /* MIPS16/microMIPS text labels are stored as odd.
4446 We just carry the ISA mode bit forward. */
462427c4 4447 if (text_p && HAVE_CODE_COMPRESSION)
770c0151 4448 val |= (S_GET_VALUE (l->label) & 0x1);
404a8071
RS
4449 S_SET_VALUE (l->label, val);
4450 }
4451}
4452
462427c4
RS
4453/* Move all labels in insn_labels to the current insertion point
4454 and treat them as text labels. */
4455
4456static void
4457mips_move_text_labels (void)
4458{
4459 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
4460}
4461
9e009953
MR
4462/* Duplicate the test for LINK_ONCE sections as in `adjust_reloc_syms'. */
4463
5f0fe04b
TS
4464static bfd_boolean
4465s_is_linkonce (symbolS *sym, segT from_seg)
4466{
4467 bfd_boolean linkonce = FALSE;
4468 segT symseg = S_GET_SEGMENT (sym);
4469
4470 if (symseg != from_seg && !S_IS_LOCAL (sym))
4471 {
fd361982 4472 if ((bfd_section_flags (symseg) & SEC_LINK_ONCE))
5f0fe04b 4473 linkonce = TRUE;
5f0fe04b
TS
4474 /* The GNU toolchain uses an extension for ELF: a section
4475 beginning with the magic string .gnu.linkonce is a
4476 linkonce section. */
4477 if (strncmp (segment_name (symseg), ".gnu.linkonce",
4478 sizeof ".gnu.linkonce" - 1) == 0)
4479 linkonce = TRUE;
5f0fe04b
TS
4480 }
4481 return linkonce;
4482}
4483
e1b47bd5 4484/* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
df58fc94
RS
4485 linker to handle them specially, such as generating jalx instructions
4486 when needed. We also make them odd for the duration of the assembly,
4487 in order to generate the right sort of code. We will make them even
252b5132
RH
4488 in the adjust_symtab routine, while leaving them marked. This is
4489 convenient for the debugger and the disassembler. The linker knows
4490 to make them odd again. */
4491
4492static void
e1b47bd5 4493mips_compressed_mark_label (symbolS *label)
252b5132 4494{
df58fc94 4495 gas_assert (HAVE_CODE_COMPRESSION);
a8dbcb85 4496
f3ded42a
RS
4497 if (mips_opts.mips16)
4498 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
4499 else
4500 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
e1b47bd5
RS
4501 if ((S_GET_VALUE (label) & 1) == 0
4502 /* Don't adjust the address if the label is global or weak, or
4503 in a link-once section, since we'll be emitting symbol reloc
4504 references to it which will be patched up by the linker, and
4505 the final value of the symbol may or may not be MIPS16/microMIPS. */
4506 && !S_IS_WEAK (label)
4507 && !S_IS_EXTERNAL (label)
4508 && !s_is_linkonce (label, now_seg))
4509 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
4510}
4511
4512/* Mark preceding MIPS16 or microMIPS instruction labels. */
4513
4514static void
4515mips_compressed_mark_labels (void)
4516{
4517 struct insn_label_list *l;
4518
4519 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
4520 mips_compressed_mark_label (l->label);
252b5132
RH
4521}
4522
4d7206a2
RS
4523/* End the current frag. Make it a variant frag and record the
4524 relaxation info. */
4525
4526static void
4527relax_close_frag (void)
4528{
584892a6 4529 mips_macro_warning.first_frag = frag_now;
4d7206a2 4530 frag_var (rs_machine_dependent, 0, 0,
ce8ad872
MR
4531 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1],
4532 mips_pic != NO_PIC),
4d7206a2
RS
4533 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
4534
4535 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
4536 mips_relax.first_fixup = 0;
4537}
4538
4539/* Start a new relaxation sequence whose expansion depends on SYMBOL.
4540 See the comment above RELAX_ENCODE for more details. */
4541
4542static void
4543relax_start (symbolS *symbol)
4544{
9c2799c2 4545 gas_assert (mips_relax.sequence == 0);
4d7206a2
RS
4546 mips_relax.sequence = 1;
4547 mips_relax.symbol = symbol;
4548}
4549
4550/* Start generating the second version of a relaxable sequence.
4551 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
4552
4553static void
4d7206a2
RS
4554relax_switch (void)
4555{
9c2799c2 4556 gas_assert (mips_relax.sequence == 1);
4d7206a2
RS
4557 mips_relax.sequence = 2;
4558}
4559
4560/* End the current relaxable sequence. */
4561
4562static void
4563relax_end (void)
4564{
9c2799c2 4565 gas_assert (mips_relax.sequence == 2);
4d7206a2
RS
4566 relax_close_frag ();
4567 mips_relax.sequence = 0;
4568}
4569
11625dd8
RS
4570/* Return true if IP is a delayed branch or jump. */
4571
4572static inline bfd_boolean
4573delayed_branch_p (const struct mips_cl_insn *ip)
4574{
4575 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
4576 | INSN_COND_BRANCH_DELAY
4577 | INSN_COND_BRANCH_LIKELY)) != 0;
4578}
4579
4580/* Return true if IP is a compact branch or jump. */
4581
4582static inline bfd_boolean
4583compact_branch_p (const struct mips_cl_insn *ip)
4584{
26545944
RS
4585 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
4586 | INSN2_COND_BRANCH)) != 0;
11625dd8
RS
4587}
4588
4589/* Return true if IP is an unconditional branch or jump. */
4590
4591static inline bfd_boolean
4592uncond_branch_p (const struct mips_cl_insn *ip)
4593{
4594 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
26545944 4595 || (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0);
11625dd8
RS
4596}
4597
4598/* Return true if IP is a branch-likely instruction. */
4599
4600static inline bfd_boolean
4601branch_likely_p (const struct mips_cl_insn *ip)
4602{
4603 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
4604}
4605
14fe068b
RS
4606/* Return the type of nop that should be used to fill the delay slot
4607 of delayed branch IP. */
4608
4609static struct mips_cl_insn *
4610get_delay_slot_nop (const struct mips_cl_insn *ip)
4611{
4612 if (mips_opts.micromips
4613 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
4614 return &micromips_nop32_insn;
4615 return NOP_INSN;
4616}
4617
fc76e730
RS
4618/* Return a mask that has bit N set if OPCODE reads the register(s)
4619 in operand N. */
df58fc94
RS
4620
4621static unsigned int
fc76e730 4622insn_read_mask (const struct mips_opcode *opcode)
df58fc94 4623{
fc76e730
RS
4624 return (opcode->pinfo & INSN_READ_ALL) >> INSN_READ_SHIFT;
4625}
df58fc94 4626
fc76e730
RS
4627/* Return a mask that has bit N set if OPCODE writes to the register(s)
4628 in operand N. */
4629
4630static unsigned int
4631insn_write_mask (const struct mips_opcode *opcode)
4632{
4633 return (opcode->pinfo & INSN_WRITE_ALL) >> INSN_WRITE_SHIFT;
4634}
4635
4636/* Return a mask of the registers specified by operand OPERAND of INSN.
4637 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4638 is set. */
4639
4640static unsigned int
4641operand_reg_mask (const struct mips_cl_insn *insn,
4642 const struct mips_operand *operand,
4643 unsigned int type_mask)
4644{
4645 unsigned int uval, vsel;
4646
4647 switch (operand->type)
df58fc94 4648 {
fc76e730
RS
4649 case OP_INT:
4650 case OP_MAPPED_INT:
4651 case OP_MSB:
4652 case OP_PCREL:
4653 case OP_PERF_REG:
4654 case OP_ADDIUSP_INT:
4655 case OP_ENTRY_EXIT_LIST:
4656 case OP_REPEAT_DEST_REG:
4657 case OP_REPEAT_PREV_REG:
4658 case OP_PC:
14daeee3
RS
4659 case OP_VU0_SUFFIX:
4660 case OP_VU0_MATCH_SUFFIX:
56d438b1 4661 case OP_IMM_INDEX:
fc76e730
RS
4662 abort ();
4663
25499ac7
MR
4664 case OP_REG28:
4665 return 1 << 28;
4666
fc76e730 4667 case OP_REG:
0f35dbc4 4668 case OP_OPTIONAL_REG:
fc76e730
RS
4669 {
4670 const struct mips_reg_operand *reg_op;
4671
4672 reg_op = (const struct mips_reg_operand *) operand;
4673 if (!(type_mask & (1 << reg_op->reg_type)))
4674 return 0;
4675 uval = insn_extract_operand (insn, operand);
4676 return 1 << mips_decode_reg_operand (reg_op, uval);
4677 }
4678
4679 case OP_REG_PAIR:
4680 {
4681 const struct mips_reg_pair_operand *pair_op;
4682
4683 pair_op = (const struct mips_reg_pair_operand *) operand;
4684 if (!(type_mask & (1 << pair_op->reg_type)))
4685 return 0;
4686 uval = insn_extract_operand (insn, operand);
4687 return (1 << pair_op->reg1_map[uval]) | (1 << pair_op->reg2_map[uval]);
4688 }
4689
4690 case OP_CLO_CLZ_DEST:
4691 if (!(type_mask & (1 << OP_REG_GP)))
4692 return 0;
4693 uval = insn_extract_operand (insn, operand);
4694 return (1 << (uval & 31)) | (1 << (uval >> 5));
4695
7361da2c
AB
4696 case OP_SAME_RS_RT:
4697 if (!(type_mask & (1 << OP_REG_GP)))
4698 return 0;
4699 uval = insn_extract_operand (insn, operand);
4700 gas_assert ((uval & 31) == (uval >> 5));
4701 return 1 << (uval & 31);
4702
4703 case OP_CHECK_PREV:
4704 case OP_NON_ZERO_REG:
4705 if (!(type_mask & (1 << OP_REG_GP)))
4706 return 0;
4707 uval = insn_extract_operand (insn, operand);
4708 return 1 << (uval & 31);
4709
fc76e730
RS
4710 case OP_LWM_SWM_LIST:
4711 abort ();
4712
4713 case OP_SAVE_RESTORE_LIST:
4714 abort ();
4715
4716 case OP_MDMX_IMM_REG:
4717 if (!(type_mask & (1 << OP_REG_VEC)))
4718 return 0;
4719 uval = insn_extract_operand (insn, operand);
4720 vsel = uval >> 5;
4721 if ((vsel & 0x18) == 0x18)
4722 return 0;
4723 return 1 << (uval & 31);
56d438b1
CF
4724
4725 case OP_REG_INDEX:
4726 if (!(type_mask & (1 << OP_REG_GP)))
4727 return 0;
4728 return 1 << insn_extract_operand (insn, operand);
df58fc94 4729 }
fc76e730
RS
4730 abort ();
4731}
4732
4733/* Return a mask of the registers specified by operands OPNO_MASK of INSN,
4734 where bit N of OPNO_MASK is set if operand N should be included.
4735 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4736 is set. */
4737
4738static unsigned int
4739insn_reg_mask (const struct mips_cl_insn *insn,
4740 unsigned int type_mask, unsigned int opno_mask)
4741{
4742 unsigned int opno, reg_mask;
4743
4744 opno = 0;
4745 reg_mask = 0;
4746 while (opno_mask != 0)
4747 {
4748 if (opno_mask & 1)
4749 reg_mask |= operand_reg_mask (insn, insn_opno (insn, opno), type_mask);
4750 opno_mask >>= 1;
4751 opno += 1;
4752 }
4753 return reg_mask;
df58fc94
RS
4754}
4755
4c260379
RS
4756/* Return the mask of core registers that IP reads. */
4757
4758static unsigned int
4759gpr_read_mask (const struct mips_cl_insn *ip)
4760{
4761 unsigned long pinfo, pinfo2;
4762 unsigned int mask;
4763
fc76e730 4764 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_read_mask (ip->insn_mo));
4c260379
RS
4765 pinfo = ip->insn_mo->pinfo;
4766 pinfo2 = ip->insn_mo->pinfo2;
fc76e730 4767 if (pinfo & INSN_UDI)
4c260379 4768 {
fc76e730
RS
4769 /* UDI instructions have traditionally been assumed to read RS
4770 and RT. */
4771 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4772 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4c260379 4773 }
fc76e730
RS
4774 if (pinfo & INSN_READ_GPR_24)
4775 mask |= 1 << 24;
4776 if (pinfo2 & INSN2_READ_GPR_16)
4777 mask |= 1 << 16;
4778 if (pinfo2 & INSN2_READ_SP)
4779 mask |= 1 << SP;
26545944 4780 if (pinfo2 & INSN2_READ_GPR_31)
fc76e730 4781 mask |= 1 << 31;
fe35f09f
RS
4782 /* Don't include register 0. */
4783 return mask & ~1;
4c260379
RS
4784}
4785
4786/* Return the mask of core registers that IP writes. */
4787
4788static unsigned int
4789gpr_write_mask (const struct mips_cl_insn *ip)
4790{
4791 unsigned long pinfo, pinfo2;
4792 unsigned int mask;
4793
fc76e730 4794 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_write_mask (ip->insn_mo));
4c260379
RS
4795 pinfo = ip->insn_mo->pinfo;
4796 pinfo2 = ip->insn_mo->pinfo2;
fc76e730
RS
4797 if (pinfo & INSN_WRITE_GPR_24)
4798 mask |= 1 << 24;
4799 if (pinfo & INSN_WRITE_GPR_31)
4800 mask |= 1 << 31;
4801 if (pinfo & INSN_UDI)
4802 /* UDI instructions have traditionally been assumed to write to RD. */
4803 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4804 if (pinfo2 & INSN2_WRITE_SP)
4805 mask |= 1 << SP;
fe35f09f
RS
4806 /* Don't include register 0. */
4807 return mask & ~1;
4c260379
RS
4808}
4809
4810/* Return the mask of floating-point registers that IP reads. */
4811
4812static unsigned int
4813fpr_read_mask (const struct mips_cl_insn *ip)
4814{
fc76e730 4815 unsigned long pinfo;
4c260379
RS
4816 unsigned int mask;
4817
9d5de888
CF
4818 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4819 | (1 << OP_REG_MSA)),
fc76e730 4820 insn_read_mask (ip->insn_mo));
4c260379 4821 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4822 /* Conservatively treat all operands to an FP_D instruction are doubles.
4823 (This is overly pessimistic for things like cvt.d.s.) */
bad1aba3 4824 if (FPR_SIZE != 64 && (pinfo & FP_D))
4c260379
RS
4825 mask |= mask << 1;
4826 return mask;
4827}
4828
4829/* Return the mask of floating-point registers that IP writes. */
4830
4831static unsigned int
4832fpr_write_mask (const struct mips_cl_insn *ip)
4833{
fc76e730 4834 unsigned long pinfo;
4c260379
RS
4835 unsigned int mask;
4836
9d5de888
CF
4837 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4838 | (1 << OP_REG_MSA)),
fc76e730 4839 insn_write_mask (ip->insn_mo));
4c260379 4840 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4841 /* Conservatively treat all operands to an FP_D instruction are doubles.
4842 (This is overly pessimistic for things like cvt.s.d.) */
bad1aba3 4843 if (FPR_SIZE != 64 && (pinfo & FP_D))
4c260379
RS
4844 mask |= mask << 1;
4845 return mask;
4846}
4847
a1d78564
RS
4848/* Operand OPNUM of INSN is an odd-numbered floating-point register.
4849 Check whether that is allowed. */
4850
4851static bfd_boolean
4852mips_oddfpreg_ok (const struct mips_opcode *insn, int opnum)
4853{
4854 const char *s = insn->name;
351cdf24
MF
4855 bfd_boolean oddspreg = (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa, mips_opts.arch)
4856 || FPR_SIZE == 64)
4857 && mips_opts.oddspreg;
a1d78564
RS
4858
4859 if (insn->pinfo == INSN_MACRO)
4860 /* Let a macro pass, we'll catch it later when it is expanded. */
4861 return TRUE;
4862
351cdf24
MF
4863 /* Single-precision coprocessor loads and moves are OK for 32-bit registers,
4864 otherwise it depends on oddspreg. */
4865 if ((insn->pinfo & FP_S)
4866 && (insn->pinfo & (INSN_LOAD_MEMORY | INSN_STORE_MEMORY
43885403 4867 | INSN_LOAD_COPROC | INSN_COPROC_MOVE)))
351cdf24 4868 return FPR_SIZE == 32 || oddspreg;
a1d78564 4869
351cdf24
MF
4870 /* Allow odd registers for single-precision ops and double-precision if the
4871 floating-point registers are 64-bit wide. */
4872 switch (insn->pinfo & (FP_S | FP_D))
4873 {
4874 case FP_S:
4875 case 0:
4876 return oddspreg;
4877 case FP_D:
4878 return FPR_SIZE == 64;
4879 default:
4880 break;
a1d78564
RS
4881 }
4882
351cdf24
MF
4883 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
4884 s = strchr (insn->name, '.');
4885 if (s != NULL && opnum == 2)
4886 s = strchr (s + 1, '.');
4887 if (s != NULL && (s[1] == 'w' || s[1] == 's'))
4888 return oddspreg;
a1d78564 4889
351cdf24 4890 return FPR_SIZE == 64;
a1d78564
RS
4891}
4892
a1d78564
RS
4893/* Information about an instruction argument that we're trying to match. */
4894struct mips_arg_info
4895{
4896 /* The instruction so far. */
4897 struct mips_cl_insn *insn;
4898
a92713e6
RS
4899 /* The first unconsumed operand token. */
4900 struct mips_operand_token *token;
4901
a1d78564
RS
4902 /* The 1-based operand number, in terms of insn->insn_mo->args. */
4903 int opnum;
4904
4905 /* The 1-based argument number, for error reporting. This does not
4906 count elided optional registers, etc.. */
4907 int argnum;
4908
4909 /* The last OP_REG operand seen, or ILLEGAL_REG if none. */
4910 unsigned int last_regno;
4911
4912 /* If the first operand was an OP_REG, this is the register that it
4913 specified, otherwise it is ILLEGAL_REG. */
4914 unsigned int dest_regno;
4915
4916 /* The value of the last OP_INT operand. Only used for OP_MSB,
4917 where it gives the lsb position. */
4918 unsigned int last_op_int;
4919
60f20e8b 4920 /* If true, match routines should assume that no later instruction
2b0f3761 4921 alternative matches and should therefore be as accommodating as
60f20e8b
RS
4922 possible. Match routines should not report errors if something
4923 is only invalid for !LAX_MATCH. */
4924 bfd_boolean lax_match;
a1d78564 4925
a1d78564
RS
4926 /* True if a reference to the current AT register was seen. */
4927 bfd_boolean seen_at;
4928};
4929
1a00e612
RS
4930/* Record that the argument is out of range. */
4931
4932static void
4933match_out_of_range (struct mips_arg_info *arg)
4934{
4935 set_insn_error_i (arg->argnum, _("operand %d out of range"), arg->argnum);
4936}
4937
4938/* Record that the argument isn't constant but needs to be. */
4939
4940static void
4941match_not_constant (struct mips_arg_info *arg)
4942{
4943 set_insn_error_i (arg->argnum, _("operand %d must be constant"),
4944 arg->argnum);
4945}
4946
a92713e6
RS
4947/* Try to match an OT_CHAR token for character CH. Consume the token
4948 and return true on success, otherwise return false. */
a1d78564 4949
a92713e6
RS
4950static bfd_boolean
4951match_char (struct mips_arg_info *arg, char ch)
a1d78564 4952{
a92713e6
RS
4953 if (arg->token->type == OT_CHAR && arg->token->u.ch == ch)
4954 {
4955 ++arg->token;
4956 if (ch == ',')
4957 arg->argnum += 1;
4958 return TRUE;
4959 }
4960 return FALSE;
4961}
a1d78564 4962
a92713e6
RS
4963/* Try to get an expression from the next tokens in ARG. Consume the
4964 tokens and return true on success, storing the expression value in
4965 VALUE and relocation types in R. */
4966
4967static bfd_boolean
4968match_expression (struct mips_arg_info *arg, expressionS *value,
4969 bfd_reloc_code_real_type *r)
4970{
d436c1c2
RS
4971 /* If the next token is a '(' that was parsed as being part of a base
4972 expression, assume we have an elided offset. The later match will fail
4973 if this turns out to be wrong. */
4974 if (arg->token->type == OT_CHAR && arg->token->u.ch == '(')
a1d78564 4975 {
d436c1c2
RS
4976 value->X_op = O_constant;
4977 value->X_add_number = 0;
4978 r[0] = r[1] = r[2] = BFD_RELOC_UNUSED;
a92713e6
RS
4979 return TRUE;
4980 }
4981
d436c1c2
RS
4982 /* Reject register-based expressions such as "0+$2" and "(($2))".
4983 For plain registers the default error seems more appropriate. */
4984 if (arg->token->type == OT_INTEGER
4985 && arg->token->u.integer.value.X_op == O_register)
a92713e6 4986 {
d436c1c2
RS
4987 set_insn_error (arg->argnum, _("register value used as expression"));
4988 return FALSE;
a1d78564 4989 }
d436c1c2
RS
4990
4991 if (arg->token->type == OT_INTEGER)
a92713e6 4992 {
d436c1c2
RS
4993 *value = arg->token->u.integer.value;
4994 memcpy (r, arg->token->u.integer.relocs, 3 * sizeof (*r));
4995 ++arg->token;
4996 return TRUE;
a92713e6 4997 }
a92713e6 4998
d436c1c2
RS
4999 set_insn_error_i
5000 (arg->argnum, _("operand %d must be an immediate expression"),
5001 arg->argnum);
5002 return FALSE;
a92713e6
RS
5003}
5004
5005/* Try to get a constant expression from the next tokens in ARG. Consume
de194d85 5006 the tokens and return true on success, storing the constant value
a54d5f8b 5007 in *VALUE. */
a92713e6
RS
5008
5009static bfd_boolean
1a00e612 5010match_const_int (struct mips_arg_info *arg, offsetT *value)
a92713e6
RS
5011{
5012 expressionS ex;
5013 bfd_reloc_code_real_type r[3];
a1d78564 5014
a92713e6
RS
5015 if (!match_expression (arg, &ex, r))
5016 return FALSE;
5017
5018 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_constant)
a1d78564
RS
5019 *value = ex.X_add_number;
5020 else
5021 {
c96425c5
MR
5022 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_big)
5023 match_out_of_range (arg);
5024 else
5025 match_not_constant (arg);
1a00e612 5026 return FALSE;
a1d78564 5027 }
a92713e6 5028 return TRUE;
a1d78564
RS
5029}
5030
5031/* Return the RTYPE_* flags for a register operand of type TYPE that
5032 appears in instruction OPCODE. */
5033
5034static unsigned int
5035convert_reg_type (const struct mips_opcode *opcode,
5036 enum mips_reg_operand_type type)
5037{
5038 switch (type)
5039 {
5040 case OP_REG_GP:
5041 return RTYPE_NUM | RTYPE_GP;
5042
5043 case OP_REG_FP:
5044 /* Allow vector register names for MDMX if the instruction is a 64-bit
5045 FPR load, store or move (including moves to and from GPRs). */
5046 if ((mips_opts.ase & ASE_MDMX)
5047 && (opcode->pinfo & FP_D)
43885403 5048 && (opcode->pinfo & (INSN_COPROC_MOVE
a1d78564 5049 | INSN_COPROC_MEMORY_DELAY
43885403 5050 | INSN_LOAD_COPROC
67dc82bc 5051 | INSN_LOAD_MEMORY
a1d78564
RS
5052 | INSN_STORE_MEMORY)))
5053 return RTYPE_FPU | RTYPE_VEC;
5054 return RTYPE_FPU;
5055
5056 case OP_REG_CCC:
5057 if (opcode->pinfo & (FP_D | FP_S))
5058 return RTYPE_CCC | RTYPE_FCC;
5059 return RTYPE_CCC;
5060
5061 case OP_REG_VEC:
5062 if (opcode->membership & INSN_5400)
5063 return RTYPE_FPU;
5064 return RTYPE_FPU | RTYPE_VEC;
5065
5066 case OP_REG_ACC:
5067 return RTYPE_ACC;
5068
5069 case OP_REG_COPRO:
5070 if (opcode->name[strlen (opcode->name) - 1] == '0')
5071 return RTYPE_NUM | RTYPE_CP0;
5072 return RTYPE_NUM;
5073
5074 case OP_REG_HW:
5075 return RTYPE_NUM;
14daeee3
RS
5076
5077 case OP_REG_VI:
5078 return RTYPE_NUM | RTYPE_VI;
5079
5080 case OP_REG_VF:
5081 return RTYPE_NUM | RTYPE_VF;
5082
5083 case OP_REG_R5900_I:
5084 return RTYPE_R5900_I;
5085
5086 case OP_REG_R5900_Q:
5087 return RTYPE_R5900_Q;
5088
5089 case OP_REG_R5900_R:
5090 return RTYPE_R5900_R;
5091
5092 case OP_REG_R5900_ACC:
5093 return RTYPE_R5900_ACC;
56d438b1
CF
5094
5095 case OP_REG_MSA:
5096 return RTYPE_MSA;
5097
5098 case OP_REG_MSA_CTRL:
5099 return RTYPE_NUM;
a1d78564
RS
5100 }
5101 abort ();
5102}
5103
5104/* ARG is register REGNO, of type TYPE. Warn about any dubious registers. */
5105
5106static void
5107check_regno (struct mips_arg_info *arg,
5108 enum mips_reg_operand_type type, unsigned int regno)
5109{
5110 if (AT && type == OP_REG_GP && regno == AT)
5111 arg->seen_at = TRUE;
5112
5113 if (type == OP_REG_FP
5114 && (regno & 1) != 0
a1d78564 5115 && !mips_oddfpreg_ok (arg->insn->insn_mo, arg->opnum))
351cdf24
MF
5116 {
5117 /* This was a warning prior to introducing O32 FPXX and FP64 support
5118 so maintain a warning for FP32 but raise an error for the new
5119 cases. */
5120 if (FPR_SIZE == 32)
5121 as_warn (_("float register should be even, was %d"), regno);
5122 else
5123 as_bad (_("float register should be even, was %d"), regno);
5124 }
a1d78564
RS
5125
5126 if (type == OP_REG_CCC)
5127 {
5128 const char *name;
5129 size_t length;
5130
5131 name = arg->insn->insn_mo->name;
5132 length = strlen (name);
5133 if ((regno & 1) != 0
5134 && ((length >= 3 && strcmp (name + length - 3, ".ps") == 0)
5135 || (length >= 5 && strncmp (name + length - 5, "any2", 4) == 0)))
1661c76c 5136 as_warn (_("condition code register should be even for %s, was %d"),
a1d78564
RS
5137 name, regno);
5138
5139 if ((regno & 3) != 0
5140 && (length >= 5 && strncmp (name + length - 5, "any4", 4) == 0))
1661c76c 5141 as_warn (_("condition code register should be 0 or 4 for %s, was %d"),
a1d78564
RS
5142 name, regno);
5143 }
5144}
5145
a92713e6
RS
5146/* ARG is a register with symbol value SYMVAL. Try to interpret it as
5147 a register of type TYPE. Return true on success, storing the register
5148 number in *REGNO and warning about any dubious uses. */
5149
5150static bfd_boolean
5151match_regno (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5152 unsigned int symval, unsigned int *regno)
5153{
5154 if (type == OP_REG_VEC)
5155 symval = mips_prefer_vec_regno (symval);
5156 if (!(symval & convert_reg_type (arg->insn->insn_mo, type)))
5157 return FALSE;
5158
5159 *regno = symval & RNUM_MASK;
5160 check_regno (arg, type, *regno);
5161 return TRUE;
5162}
5163
5164/* Try to interpret the next token in ARG as a register of type TYPE.
5165 Consume the token and return true on success, storing the register
5166 number in *REGNO. Return false on failure. */
5167
5168static bfd_boolean
5169match_reg (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5170 unsigned int *regno)
5171{
5172 if (arg->token->type == OT_REG
5173 && match_regno (arg, type, arg->token->u.regno, regno))
5174 {
5175 ++arg->token;
5176 return TRUE;
5177 }
5178 return FALSE;
5179}
5180
5181/* Try to interpret the next token in ARG as a range of registers of type TYPE.
5182 Consume the token and return true on success, storing the register numbers
5183 in *REGNO1 and *REGNO2. Return false on failure. */
5184
5185static bfd_boolean
5186match_reg_range (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5187 unsigned int *regno1, unsigned int *regno2)
5188{
5189 if (match_reg (arg, type, regno1))
5190 {
5191 *regno2 = *regno1;
5192 return TRUE;
5193 }
5194 if (arg->token->type == OT_REG_RANGE
5195 && match_regno (arg, type, arg->token->u.reg_range.regno1, regno1)
5196 && match_regno (arg, type, arg->token->u.reg_range.regno2, regno2)
5197 && *regno1 <= *regno2)
5198 {
5199 ++arg->token;
5200 return TRUE;
5201 }
5202 return FALSE;
5203}
5204
a1d78564
RS
5205/* OP_INT matcher. */
5206
a92713e6 5207static bfd_boolean
a1d78564 5208match_int_operand (struct mips_arg_info *arg,
a92713e6 5209 const struct mips_operand *operand_base)
a1d78564
RS
5210{
5211 const struct mips_int_operand *operand;
3ccad066 5212 unsigned int uval;
a1d78564
RS
5213 int min_val, max_val, factor;
5214 offsetT sval;
a1d78564
RS
5215
5216 operand = (const struct mips_int_operand *) operand_base;
5217 factor = 1 << operand->shift;
3ccad066
RS
5218 min_val = mips_int_operand_min (operand);
5219 max_val = mips_int_operand_max (operand);
a1d78564 5220
d436c1c2
RS
5221 if (operand_base->lsb == 0
5222 && operand_base->size == 16
5223 && operand->shift == 0
5224 && operand->bias == 0
5225 && (operand->max_val == 32767 || operand->max_val == 65535))
a1d78564
RS
5226 {
5227 /* The operand can be relocated. */
a92713e6
RS
5228 if (!match_expression (arg, &offset_expr, offset_reloc))
5229 return FALSE;
5230
c96425c5
MR
5231 if (offset_expr.X_op == O_big)
5232 {
5233 match_out_of_range (arg);
5234 return FALSE;
5235 }
5236
a92713e6 5237 if (offset_reloc[0] != BFD_RELOC_UNUSED)
33eaf5de 5238 /* Relocation operators were used. Accept the argument and
a1d78564
RS
5239 leave the relocation value in offset_expr and offset_relocs
5240 for the caller to process. */
a92713e6
RS
5241 return TRUE;
5242
5243 if (offset_expr.X_op != O_constant)
a1d78564 5244 {
60f20e8b
RS
5245 /* Accept non-constant operands if no later alternative matches,
5246 leaving it for the caller to process. */
5247 if (!arg->lax_match)
602b88e3
MR
5248 {
5249 match_not_constant (arg);
5250 return FALSE;
5251 }
a92713e6
RS
5252 offset_reloc[0] = BFD_RELOC_LO16;
5253 return TRUE;
a1d78564 5254 }
a92713e6 5255
a1d78564
RS
5256 /* Clear the global state; we're going to install the operand
5257 ourselves. */
a92713e6 5258 sval = offset_expr.X_add_number;
a1d78564 5259 offset_expr.X_op = O_absent;
60f20e8b
RS
5260
5261 /* For compatibility with older assemblers, we accept
5262 0x8000-0xffff as signed 16-bit numbers when only
5263 signed numbers are allowed. */
5264 if (sval > max_val)
5265 {
5266 max_val = ((1 << operand_base->size) - 1) << operand->shift;
5267 if (!arg->lax_match && sval <= max_val)
20c59b84
MR
5268 {
5269 match_out_of_range (arg);
5270 return FALSE;
5271 }
60f20e8b 5272 }
a1d78564
RS
5273 }
5274 else
5275 {
1a00e612 5276 if (!match_const_int (arg, &sval))
a92713e6 5277 return FALSE;
a1d78564
RS
5278 }
5279
5280 arg->last_op_int = sval;
5281
1a00e612 5282 if (sval < min_val || sval > max_val || sval % factor)
a1d78564 5283 {
1a00e612
RS
5284 match_out_of_range (arg);
5285 return FALSE;
a1d78564
RS
5286 }
5287
5288 uval = (unsigned int) sval >> operand->shift;
5289 uval -= operand->bias;
5290
5291 /* Handle -mfix-cn63xxp1. */
5292 if (arg->opnum == 1
5293 && mips_fix_cn63xxp1
5294 && !mips_opts.micromips
5295 && strcmp ("pref", arg->insn->insn_mo->name) == 0)
5296 switch (uval)
5297 {
5298 case 5:
5299 case 25:
5300 case 26:
5301 case 27:
5302 case 28:
5303 case 29:
5304 case 30:
5305 case 31:
5306 /* These are ok. */
5307 break;
5308
5309 default:
5310 /* The rest must be changed to 28. */
5311 uval = 28;
5312 break;
5313 }
5314
5315 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5316 return TRUE;
a1d78564
RS
5317}
5318
5319/* OP_MAPPED_INT matcher. */
5320
a92713e6 5321static bfd_boolean
a1d78564 5322match_mapped_int_operand (struct mips_arg_info *arg,
a92713e6 5323 const struct mips_operand *operand_base)
a1d78564
RS
5324{
5325 const struct mips_mapped_int_operand *operand;
5326 unsigned int uval, num_vals;
5327 offsetT sval;
5328
5329 operand = (const struct mips_mapped_int_operand *) operand_base;
1a00e612 5330 if (!match_const_int (arg, &sval))
a92713e6 5331 return FALSE;
a1d78564
RS
5332
5333 num_vals = 1 << operand_base->size;
5334 for (uval = 0; uval < num_vals; uval++)
5335 if (operand->int_map[uval] == sval)
5336 break;
5337 if (uval == num_vals)
1a00e612
RS
5338 {
5339 match_out_of_range (arg);
5340 return FALSE;
5341 }
a1d78564
RS
5342
5343 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5344 return TRUE;
a1d78564
RS
5345}
5346
5347/* OP_MSB matcher. */
5348
a92713e6 5349static bfd_boolean
a1d78564 5350match_msb_operand (struct mips_arg_info *arg,
a92713e6 5351 const struct mips_operand *operand_base)
a1d78564
RS
5352{
5353 const struct mips_msb_operand *operand;
5354 int min_val, max_val, max_high;
5355 offsetT size, sval, high;
5356
5357 operand = (const struct mips_msb_operand *) operand_base;
5358 min_val = operand->bias;
5359 max_val = min_val + (1 << operand_base->size) - 1;
5360 max_high = operand->opsize;
5361
1a00e612 5362 if (!match_const_int (arg, &size))
a92713e6 5363 return FALSE;
a1d78564
RS
5364
5365 high = size + arg->last_op_int;
5366 sval = operand->add_lsb ? high : size;
5367
5368 if (size < 0 || high > max_high || sval < min_val || sval > max_val)
5369 {
1a00e612
RS
5370 match_out_of_range (arg);
5371 return FALSE;
a1d78564
RS
5372 }
5373 insn_insert_operand (arg->insn, operand_base, sval - min_val);
a92713e6 5374 return TRUE;
a1d78564
RS
5375}
5376
5377/* OP_REG matcher. */
5378
a92713e6 5379static bfd_boolean
a1d78564 5380match_reg_operand (struct mips_arg_info *arg,
a92713e6 5381 const struct mips_operand *operand_base)
a1d78564
RS
5382{
5383 const struct mips_reg_operand *operand;
a92713e6 5384 unsigned int regno, uval, num_vals;
a1d78564
RS
5385
5386 operand = (const struct mips_reg_operand *) operand_base;
a92713e6
RS
5387 if (!match_reg (arg, operand->reg_type, &regno))
5388 return FALSE;
a1d78564
RS
5389
5390 if (operand->reg_map)
5391 {
5392 num_vals = 1 << operand->root.size;
5393 for (uval = 0; uval < num_vals; uval++)
5394 if (operand->reg_map[uval] == regno)
5395 break;
5396 if (num_vals == uval)
a92713e6 5397 return FALSE;
a1d78564
RS
5398 }
5399 else
5400 uval = regno;
5401
a1d78564
RS
5402 arg->last_regno = regno;
5403 if (arg->opnum == 1)
5404 arg->dest_regno = regno;
5405 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5406 return TRUE;
a1d78564
RS
5407}
5408
5409/* OP_REG_PAIR matcher. */
5410
a92713e6 5411static bfd_boolean
a1d78564 5412match_reg_pair_operand (struct mips_arg_info *arg,
a92713e6 5413 const struct mips_operand *operand_base)
a1d78564
RS
5414{
5415 const struct mips_reg_pair_operand *operand;
a92713e6 5416 unsigned int regno1, regno2, uval, num_vals;
a1d78564
RS
5417
5418 operand = (const struct mips_reg_pair_operand *) operand_base;
a92713e6
RS
5419 if (!match_reg (arg, operand->reg_type, &regno1)
5420 || !match_char (arg, ',')
5421 || !match_reg (arg, operand->reg_type, &regno2))
5422 return FALSE;
a1d78564
RS
5423
5424 num_vals = 1 << operand_base->size;
5425 for (uval = 0; uval < num_vals; uval++)
5426 if (operand->reg1_map[uval] == regno1 && operand->reg2_map[uval] == regno2)
5427 break;
5428 if (uval == num_vals)
a92713e6 5429 return FALSE;
a1d78564 5430
a1d78564 5431 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5432 return TRUE;
a1d78564
RS
5433}
5434
5435/* OP_PCREL matcher. The caller chooses the relocation type. */
5436
a92713e6
RS
5437static bfd_boolean
5438match_pcrel_operand (struct mips_arg_info *arg)
a1d78564 5439{
a92713e6
RS
5440 bfd_reloc_code_real_type r[3];
5441
5442 return match_expression (arg, &offset_expr, r) && r[0] == BFD_RELOC_UNUSED;
a1d78564
RS
5443}
5444
5445/* OP_PERF_REG matcher. */
5446
a92713e6 5447static bfd_boolean
a1d78564 5448match_perf_reg_operand (struct mips_arg_info *arg,
a92713e6 5449 const struct mips_operand *operand)
a1d78564
RS
5450{
5451 offsetT sval;
5452
1a00e612 5453 if (!match_const_int (arg, &sval))
a92713e6 5454 return FALSE;
a1d78564
RS
5455
5456 if (sval != 0
5457 && (sval != 1
5458 || (mips_opts.arch == CPU_R5900
5459 && (strcmp (arg->insn->insn_mo->name, "mfps") == 0
5460 || strcmp (arg->insn->insn_mo->name, "mtps") == 0))))
5461 {
1a00e612
RS
5462 set_insn_error (arg->argnum, _("invalid performance register"));
5463 return FALSE;
a1d78564
RS
5464 }
5465
5466 insn_insert_operand (arg->insn, operand, sval);
a92713e6 5467 return TRUE;
a1d78564
RS
5468}
5469
5470/* OP_ADDIUSP matcher. */
5471
a92713e6 5472static bfd_boolean
a1d78564 5473match_addiusp_operand (struct mips_arg_info *arg,
a92713e6 5474 const struct mips_operand *operand)
a1d78564
RS
5475{
5476 offsetT sval;
5477 unsigned int uval;
5478
1a00e612 5479 if (!match_const_int (arg, &sval))
a92713e6 5480 return FALSE;
a1d78564
RS
5481
5482 if (sval % 4)
1a00e612
RS
5483 {
5484 match_out_of_range (arg);
5485 return FALSE;
5486 }
a1d78564
RS
5487
5488 sval /= 4;
5489 if (!(sval >= -258 && sval <= 257) || (sval >= -2 && sval <= 1))
1a00e612
RS
5490 {
5491 match_out_of_range (arg);
5492 return FALSE;
5493 }
a1d78564
RS
5494
5495 uval = (unsigned int) sval;
5496 uval = ((uval >> 1) & ~0xff) | (uval & 0xff);
5497 insn_insert_operand (arg->insn, operand, uval);
a92713e6 5498 return TRUE;
a1d78564
RS
5499}
5500
5501/* OP_CLO_CLZ_DEST matcher. */
5502
a92713e6 5503static bfd_boolean
a1d78564 5504match_clo_clz_dest_operand (struct mips_arg_info *arg,
a92713e6 5505 const struct mips_operand *operand)
a1d78564
RS
5506{
5507 unsigned int regno;
5508
a92713e6
RS
5509 if (!match_reg (arg, OP_REG_GP, &regno))
5510 return FALSE;
a1d78564 5511
a1d78564 5512 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
a92713e6 5513 return TRUE;
a1d78564
RS
5514}
5515
7361da2c
AB
5516/* OP_CHECK_PREV matcher. */
5517
5518static bfd_boolean
5519match_check_prev_operand (struct mips_arg_info *arg,
5520 const struct mips_operand *operand_base)
5521{
5522 const struct mips_check_prev_operand *operand;
5523 unsigned int regno;
5524
5525 operand = (const struct mips_check_prev_operand *) operand_base;
5526
5527 if (!match_reg (arg, OP_REG_GP, &regno))
5528 return FALSE;
5529
5530 if (!operand->zero_ok && regno == 0)
5531 return FALSE;
5532
5533 if ((operand->less_than_ok && regno < arg->last_regno)
5534 || (operand->greater_than_ok && regno > arg->last_regno)
5535 || (operand->equal_ok && regno == arg->last_regno))
5536 {
5537 arg->last_regno = regno;
5538 insn_insert_operand (arg->insn, operand_base, regno);
5539 return TRUE;
5540 }
5541
5542 return FALSE;
5543}
5544
5545/* OP_SAME_RS_RT matcher. */
5546
5547static bfd_boolean
5548match_same_rs_rt_operand (struct mips_arg_info *arg,
5549 const struct mips_operand *operand)
5550{
5551 unsigned int regno;
5552
5553 if (!match_reg (arg, OP_REG_GP, &regno))
5554 return FALSE;
5555
5556 if (regno == 0)
5557 {
5558 set_insn_error (arg->argnum, _("the source register must not be $0"));
5559 return FALSE;
5560 }
5561
5562 arg->last_regno = regno;
5563
5564 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5565 return TRUE;
5566}
5567
a1d78564
RS
5568/* OP_LWM_SWM_LIST matcher. */
5569
a92713e6 5570static bfd_boolean
a1d78564 5571match_lwm_swm_list_operand (struct mips_arg_info *arg,
a92713e6 5572 const struct mips_operand *operand)
a1d78564 5573{
a92713e6
RS
5574 unsigned int reglist, sregs, ra, regno1, regno2;
5575 struct mips_arg_info reset;
a1d78564 5576
a92713e6
RS
5577 reglist = 0;
5578 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5579 return FALSE;
5580 do
5581 {
5582 if (regno2 == FP && regno1 >= S0 && regno1 <= S7)
5583 {
5584 reglist |= 1 << FP;
5585 regno2 = S7;
5586 }
5587 reglist |= ((1U << regno2 << 1) - 1) & -(1U << regno1);
5588 reset = *arg;
5589 }
5590 while (match_char (arg, ',')
5591 && match_reg_range (arg, OP_REG_GP, &regno1, &regno2));
5592 *arg = reset;
a1d78564
RS
5593
5594 if (operand->size == 2)
5595 {
5596 /* The list must include both ra and s0-sN, for 0 <= N <= 3. E.g.:
5597
5598 s0, ra
5599 s0, s1, ra, s2, s3
5600 s0-s2, ra
5601
5602 and any permutations of these. */
5603 if ((reglist & 0xfff1ffff) != 0x80010000)
a92713e6 5604 return FALSE;
a1d78564
RS
5605
5606 sregs = (reglist >> 17) & 7;
5607 ra = 0;
5608 }
5609 else
5610 {
5611 /* The list must include at least one of ra and s0-sN,
5612 for 0 <= N <= 8. (Note that there is a gap between s7 and s8,
5613 which are $23 and $30 respectively.) E.g.:
5614
5615 ra
5616 s0
5617 ra, s0, s1, s2
5618 s0-s8
5619 s0-s5, ra
5620
5621 and any permutations of these. */
5622 if ((reglist & 0x3f00ffff) != 0)
a92713e6 5623 return FALSE;
a1d78564
RS
5624
5625 ra = (reglist >> 27) & 0x10;
5626 sregs = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
5627 }
5628 sregs += 1;
5629 if ((sregs & -sregs) != sregs)
a92713e6 5630 return FALSE;
a1d78564
RS
5631
5632 insn_insert_operand (arg->insn, operand, (ffs (sregs) - 1) | ra);
a92713e6 5633 return TRUE;
a1d78564
RS
5634}
5635
364215c8
RS
5636/* OP_ENTRY_EXIT_LIST matcher. */
5637
a92713e6 5638static unsigned int
364215c8 5639match_entry_exit_operand (struct mips_arg_info *arg,
a92713e6 5640 const struct mips_operand *operand)
364215c8
RS
5641{
5642 unsigned int mask;
5643 bfd_boolean is_exit;
5644
5645 /* The format is the same for both ENTRY and EXIT, but the constraints
5646 are different. */
5647 is_exit = strcmp (arg->insn->insn_mo->name, "exit") == 0;
5648 mask = (is_exit ? 7 << 3 : 0);
a92713e6 5649 do
364215c8
RS
5650 {
5651 unsigned int regno1, regno2;
5652 bfd_boolean is_freg;
5653
a92713e6 5654 if (match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
364215c8 5655 is_freg = FALSE;
a92713e6 5656 else if (match_reg_range (arg, OP_REG_FP, &regno1, &regno2))
364215c8
RS
5657 is_freg = TRUE;
5658 else
a92713e6 5659 return FALSE;
364215c8
RS
5660
5661 if (is_exit && is_freg && regno1 == 0 && regno2 < 2)
5662 {
5663 mask &= ~(7 << 3);
5664 mask |= (5 + regno2) << 3;
5665 }
5666 else if (!is_exit && regno1 == 4 && regno2 >= 4 && regno2 <= 7)
5667 mask |= (regno2 - 3) << 3;
5668 else if (regno1 == 16 && regno2 >= 16 && regno2 <= 17)
5669 mask |= (regno2 - 15) << 1;
5670 else if (regno1 == RA && regno2 == RA)
5671 mask |= 1;
5672 else
a92713e6 5673 return FALSE;
364215c8 5674 }
a92713e6
RS
5675 while (match_char (arg, ','));
5676
364215c8 5677 insn_insert_operand (arg->insn, operand, mask);
a92713e6 5678 return TRUE;
364215c8
RS
5679}
5680
38bf472a
MR
5681/* Encode regular MIPS SAVE/RESTORE instruction operands according to
5682 the argument register mask AMASK, the number of static registers
5683 saved NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5684 respectively, and the frame size FRAME_SIZE. */
5685
5686static unsigned int
5687mips_encode_save_restore (unsigned int amask, unsigned int nsreg,
5688 unsigned int ra, unsigned int s0, unsigned int s1,
5689 unsigned int frame_size)
5690{
5691 return ((nsreg << 23) | ((frame_size & 0xf0) << 15) | (amask << 15)
5692 | (ra << 12) | (s0 << 11) | (s1 << 10) | ((frame_size & 0xf) << 6));
5693}
5694
5695/* Encode MIPS16 SAVE/RESTORE instruction operands according to the
5696 argument register mask AMASK, the number of static registers saved
5697 NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5698 respectively, and the frame size FRAME_SIZE. */
5699
5700static unsigned int
5701mips16_encode_save_restore (unsigned int amask, unsigned int nsreg,
5702 unsigned int ra, unsigned int s0, unsigned int s1,
5703 unsigned int frame_size)
5704{
5705 unsigned int args;
5706
5707 args = (ra << 6) | (s0 << 5) | (s1 << 4) | (frame_size & 0xf);
5708 if (nsreg || amask || frame_size == 0 || frame_size > 16)
5709 args |= (MIPS16_EXTEND | (nsreg << 24) | (amask << 16)
5710 | ((frame_size & 0xf0) << 16));
5711 return args;
5712}
5713
364215c8
RS
5714/* OP_SAVE_RESTORE_LIST matcher. */
5715
a92713e6
RS
5716static bfd_boolean
5717match_save_restore_list_operand (struct mips_arg_info *arg)
364215c8
RS
5718{
5719 unsigned int opcode, args, statics, sregs;
5720 unsigned int num_frame_sizes, num_args, num_statics, num_sregs;
38bf472a 5721 unsigned int arg_mask, ra, s0, s1;
364215c8 5722 offsetT frame_size;
364215c8 5723
364215c8
RS
5724 opcode = arg->insn->insn_opcode;
5725 frame_size = 0;
5726 num_frame_sizes = 0;
5727 args = 0;
5728 statics = 0;
5729 sregs = 0;
38bf472a
MR
5730 ra = 0;
5731 s0 = 0;
5732 s1 = 0;
a92713e6 5733 do
364215c8
RS
5734 {
5735 unsigned int regno1, regno2;
5736
a92713e6 5737 if (arg->token->type == OT_INTEGER)
364215c8
RS
5738 {
5739 /* Handle the frame size. */
1a00e612 5740 if (!match_const_int (arg, &frame_size))
a92713e6 5741 return FALSE;
364215c8 5742 num_frame_sizes += 1;
364215c8
RS
5743 }
5744 else
5745 {
a92713e6
RS
5746 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5747 return FALSE;
364215c8
RS
5748
5749 while (regno1 <= regno2)
5750 {
5751 if (regno1 >= 4 && regno1 <= 7)
5752 {
5753 if (num_frame_sizes == 0)
5754 /* args $a0-$a3 */
5755 args |= 1 << (regno1 - 4);
5756 else
5757 /* statics $a0-$a3 */
5758 statics |= 1 << (regno1 - 4);
5759 }
5760 else if (regno1 >= 16 && regno1 <= 23)
5761 /* $s0-$s7 */
5762 sregs |= 1 << (regno1 - 16);
5763 else if (regno1 == 30)
5764 /* $s8 */
5765 sregs |= 1 << 8;
5766 else if (regno1 == 31)
5767 /* Add $ra to insn. */
38bf472a 5768 ra = 1;
364215c8 5769 else
a92713e6 5770 return FALSE;
364215c8
RS
5771 regno1 += 1;
5772 if (regno1 == 24)
5773 regno1 = 30;
5774 }
5775 }
364215c8 5776 }
a92713e6 5777 while (match_char (arg, ','));
364215c8
RS
5778
5779 /* Encode args/statics combination. */
5780 if (args & statics)
a92713e6 5781 return FALSE;
364215c8
RS
5782 else if (args == 0xf)
5783 /* All $a0-$a3 are args. */
38bf472a 5784 arg_mask = MIPS_SVRS_ALL_ARGS;
364215c8
RS
5785 else if (statics == 0xf)
5786 /* All $a0-$a3 are statics. */
38bf472a 5787 arg_mask = MIPS_SVRS_ALL_STATICS;
364215c8
RS
5788 else
5789 {
5790 /* Count arg registers. */
5791 num_args = 0;
5792 while (args & 0x1)
5793 {
5794 args >>= 1;
5795 num_args += 1;
5796 }
5797 if (args != 0)
a92713e6 5798 return FALSE;
364215c8
RS
5799
5800 /* Count static registers. */
5801 num_statics = 0;
5802 while (statics & 0x8)
5803 {
5804 statics = (statics << 1) & 0xf;
5805 num_statics += 1;
5806 }
5807 if (statics != 0)
a92713e6 5808 return FALSE;
364215c8
RS
5809
5810 /* Encode args/statics. */
38bf472a 5811 arg_mask = (num_args << 2) | num_statics;
364215c8
RS
5812 }
5813
5814 /* Encode $s0/$s1. */
5815 if (sregs & (1 << 0)) /* $s0 */
38bf472a 5816 s0 = 1;
364215c8 5817 if (sregs & (1 << 1)) /* $s1 */
38bf472a 5818 s1 = 1;
364215c8
RS
5819 sregs >>= 2;
5820
5821 /* Encode $s2-$s8. */
5822 num_sregs = 0;
5823 while (sregs & 1)
5824 {
5825 sregs >>= 1;
5826 num_sregs += 1;
5827 }
5828 if (sregs != 0)
a92713e6 5829 return FALSE;
364215c8
RS
5830
5831 /* Encode frame size. */
5832 if (num_frame_sizes == 0)
1a00e612
RS
5833 {
5834 set_insn_error (arg->argnum, _("missing frame size"));
5835 return FALSE;
5836 }
5837 if (num_frame_sizes > 1)
5838 {
5839 set_insn_error (arg->argnum, _("frame size specified twice"));
5840 return FALSE;
5841 }
5842 if ((frame_size & 7) != 0 || frame_size < 0 || frame_size > 0xff * 8)
5843 {
5844 set_insn_error (arg->argnum, _("invalid frame size"));
5845 return FALSE;
5846 }
38bf472a 5847 frame_size /= 8;
364215c8 5848
364215c8 5849 /* Finally build the instruction. */
38bf472a
MR
5850 if (mips_opts.mips16)
5851 opcode |= mips16_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5852 frame_size);
5853 else if (!mips_opts.micromips)
5854 opcode |= mips_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5855 frame_size);
5856 else
5857 abort ();
5858
364215c8 5859 arg->insn->insn_opcode = opcode;
a92713e6 5860 return TRUE;
364215c8
RS
5861}
5862
a1d78564
RS
5863/* OP_MDMX_IMM_REG matcher. */
5864
a92713e6 5865static bfd_boolean
a1d78564 5866match_mdmx_imm_reg_operand (struct mips_arg_info *arg,
a92713e6 5867 const struct mips_operand *operand)
a1d78564 5868{
a92713e6 5869 unsigned int regno, uval;
a1d78564
RS
5870 bfd_boolean is_qh;
5871 const struct mips_opcode *opcode;
5872
5873 /* The mips_opcode records whether this is an octobyte or quadhalf
5874 instruction. Start out with that bit in place. */
5875 opcode = arg->insn->insn_mo;
5876 uval = mips_extract_operand (operand, opcode->match);
5877 is_qh = (uval != 0);
5878
56d438b1 5879 if (arg->token->type == OT_REG)
a1d78564
RS
5880 {
5881 if ((opcode->membership & INSN_5400)
5882 && strcmp (opcode->name, "rzu.ob") == 0)
5883 {
1a00e612
RS
5884 set_insn_error_i (arg->argnum, _("operand %d must be an immediate"),
5885 arg->argnum);
5886 return FALSE;
a1d78564
RS
5887 }
5888
56d438b1
CF
5889 if (!match_regno (arg, OP_REG_VEC, arg->token->u.regno, &regno))
5890 return FALSE;
5891 ++arg->token;
5892
a1d78564
RS
5893 /* Check whether this is a vector register or a broadcast of
5894 a single element. */
56d438b1 5895 if (arg->token->type == OT_INTEGER_INDEX)
a1d78564 5896 {
56d438b1 5897 if (arg->token->u.index > (is_qh ? 3 : 7))
a1d78564 5898 {
1a00e612
RS
5899 set_insn_error (arg->argnum, _("invalid element selector"));
5900 return FALSE;
a1d78564 5901 }
56d438b1
CF
5902 uval |= arg->token->u.index << (is_qh ? 2 : 1) << 5;
5903 ++arg->token;
a1d78564
RS
5904 }
5905 else
5906 {
5907 /* A full vector. */
5908 if ((opcode->membership & INSN_5400)
5909 && (strcmp (opcode->name, "sll.ob") == 0
5910 || strcmp (opcode->name, "srl.ob") == 0))
5911 {
1a00e612
RS
5912 set_insn_error_i (arg->argnum, _("operand %d must be scalar"),
5913 arg->argnum);
5914 return FALSE;
a1d78564
RS
5915 }
5916
5917 if (is_qh)
5918 uval |= MDMX_FMTSEL_VEC_QH << 5;
5919 else
5920 uval |= MDMX_FMTSEL_VEC_OB << 5;
5921 }
a1d78564
RS
5922 uval |= regno;
5923 }
5924 else
5925 {
5926 offsetT sval;
5927
1a00e612 5928 if (!match_const_int (arg, &sval))
a92713e6 5929 return FALSE;
a1d78564
RS
5930 if (sval < 0 || sval > 31)
5931 {
1a00e612
RS
5932 match_out_of_range (arg);
5933 return FALSE;
a1d78564
RS
5934 }
5935 uval |= (sval & 31);
5936 if (is_qh)
5937 uval |= MDMX_FMTSEL_IMM_QH << 5;
5938 else
5939 uval |= MDMX_FMTSEL_IMM_OB << 5;
5940 }
5941 insn_insert_operand (arg->insn, operand, uval);
a92713e6 5942 return TRUE;
a1d78564
RS
5943}
5944
56d438b1
CF
5945/* OP_IMM_INDEX matcher. */
5946
5947static bfd_boolean
5948match_imm_index_operand (struct mips_arg_info *arg,
5949 const struct mips_operand *operand)
5950{
5951 unsigned int max_val;
5952
5953 if (arg->token->type != OT_INTEGER_INDEX)
5954 return FALSE;
5955
5956 max_val = (1 << operand->size) - 1;
5957 if (arg->token->u.index > max_val)
5958 {
5959 match_out_of_range (arg);
5960 return FALSE;
5961 }
5962 insn_insert_operand (arg->insn, operand, arg->token->u.index);
5963 ++arg->token;
5964 return TRUE;
5965}
5966
5967/* OP_REG_INDEX matcher. */
5968
5969static bfd_boolean
5970match_reg_index_operand (struct mips_arg_info *arg,
5971 const struct mips_operand *operand)
5972{
5973 unsigned int regno;
5974
5975 if (arg->token->type != OT_REG_INDEX)
5976 return FALSE;
5977
5978 if (!match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno))
5979 return FALSE;
5980
5981 insn_insert_operand (arg->insn, operand, regno);
5982 ++arg->token;
5983 return TRUE;
5984}
5985
a1d78564
RS
5986/* OP_PC matcher. */
5987
a92713e6
RS
5988static bfd_boolean
5989match_pc_operand (struct mips_arg_info *arg)
a1d78564 5990{
a92713e6
RS
5991 if (arg->token->type == OT_REG && (arg->token->u.regno & RTYPE_PC))
5992 {
5993 ++arg->token;
5994 return TRUE;
5995 }
5996 return FALSE;
a1d78564
RS
5997}
5998
25499ac7
MR
5999/* OP_REG28 matcher. */
6000
6001static bfd_boolean
6002match_reg28_operand (struct mips_arg_info *arg)
6003{
6004 unsigned int regno;
6005
6006 if (arg->token->type == OT_REG
6007 && match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno)
6008 && regno == GP)
6009 {
6010 ++arg->token;
6011 return TRUE;
6012 }
6013 return FALSE;
6014}
6015
7361da2c
AB
6016/* OP_NON_ZERO_REG matcher. */
6017
6018static bfd_boolean
6019match_non_zero_reg_operand (struct mips_arg_info *arg,
6020 const struct mips_operand *operand)
6021{
6022 unsigned int regno;
6023
6024 if (!match_reg (arg, OP_REG_GP, &regno))
6025 return FALSE;
6026
6027 if (regno == 0)
85bec12d
MF
6028 {
6029 set_insn_error (arg->argnum, _("the source register must not be $0"));
6030 return FALSE;
6031 }
7361da2c
AB
6032
6033 arg->last_regno = regno;
6034 insn_insert_operand (arg->insn, operand, regno);
6035 return TRUE;
6036}
6037
a1d78564
RS
6038/* OP_REPEAT_DEST_REG and OP_REPEAT_PREV_REG matcher. OTHER_REGNO is the
6039 register that we need to match. */
6040
a92713e6
RS
6041static bfd_boolean
6042match_tied_reg_operand (struct mips_arg_info *arg, unsigned int other_regno)
a1d78564
RS
6043{
6044 unsigned int regno;
6045
a92713e6 6046 return match_reg (arg, OP_REG_GP, &regno) && regno == other_regno;
a1d78564
RS
6047}
6048
33f46696
MR
6049/* Try to match a floating-point constant from ARG for LI.S or LI.D.
6050 LENGTH is the length of the value in bytes (4 for float, 8 for double)
6051 and USING_GPRS says whether the destination is a GPR rather than an FPR.
89565f1b
RS
6052
6053 Return the constant in IMM and OFFSET as follows:
6054
6055 - If the constant should be loaded via memory, set IMM to O_absent and
6056 OFFSET to the memory address.
6057
6058 - Otherwise, if the constant should be loaded into two 32-bit registers,
6059 set IMM to the O_constant to load into the high register and OFFSET
6060 to the corresponding value for the low register.
6061
6062 - Otherwise, set IMM to the full O_constant and set OFFSET to O_absent.
6063
6064 These constants only appear as the last operand in an instruction,
6065 and every instruction that accepts them in any variant accepts them
6066 in all variants. This means we don't have to worry about backing out
6067 any changes if the instruction does not match. We just match
6068 unconditionally and report an error if the constant is invalid. */
6069
a92713e6
RS
6070static bfd_boolean
6071match_float_constant (struct mips_arg_info *arg, expressionS *imm,
6072 expressionS *offset, int length, bfd_boolean using_gprs)
89565f1b 6073{
a92713e6 6074 char *p;
89565f1b
RS
6075 segT seg, new_seg;
6076 subsegT subseg;
6077 const char *newname;
a92713e6 6078 unsigned char *data;
89565f1b
RS
6079
6080 /* Where the constant is placed is based on how the MIPS assembler
6081 does things:
6082
6083 length == 4 && using_gprs -- immediate value only
6084 length == 8 && using_gprs -- .rdata or immediate value
6085 length == 4 && !using_gprs -- .lit4 or immediate value
6086 length == 8 && !using_gprs -- .lit8 or immediate value
6087
6088 The .lit4 and .lit8 sections are only used if permitted by the
6089 -G argument. */
a92713e6 6090 if (arg->token->type != OT_FLOAT)
1a00e612
RS
6091 {
6092 set_insn_error (arg->argnum, _("floating-point expression required"));
6093 return FALSE;
6094 }
a92713e6
RS
6095
6096 gas_assert (arg->token->u.flt.length == length);
6097 data = arg->token->u.flt.data;
6098 ++arg->token;
89565f1b
RS
6099
6100 /* Handle 32-bit constants for which an immediate value is best. */
6101 if (length == 4
6102 && (using_gprs
6103 || g_switch_value < 4
6104 || (data[0] == 0 && data[1] == 0)
6105 || (data[2] == 0 && data[3] == 0)))
6106 {
6107 imm->X_op = O_constant;
6108 if (!target_big_endian)
6109 imm->X_add_number = bfd_getl32 (data);
6110 else
6111 imm->X_add_number = bfd_getb32 (data);
6112 offset->X_op = O_absent;
a92713e6 6113 return TRUE;
89565f1b
RS
6114 }
6115
6116 /* Handle 64-bit constants for which an immediate value is best. */
6117 if (length == 8
6118 && !mips_disable_float_construction
351cdf24
MF
6119 /* Constants can only be constructed in GPRs and copied to FPRs if the
6120 GPRs are at least as wide as the FPRs or MTHC1 is available.
6121 Unlike most tests for 32-bit floating-point registers this check
6122 specifically looks for GPR_SIZE == 32 as the FPXX ABI does not
6123 permit 64-bit moves without MXHC1.
6124 Force the constant into memory otherwise. */
6125 && (using_gprs
6126 || GPR_SIZE == 64
6127 || ISA_HAS_MXHC1 (mips_opts.isa)
6128 || FPR_SIZE == 32)
89565f1b
RS
6129 && ((data[0] == 0 && data[1] == 0)
6130 || (data[2] == 0 && data[3] == 0))
6131 && ((data[4] == 0 && data[5] == 0)
6132 || (data[6] == 0 && data[7] == 0)))
6133 {
6134 /* The value is simple enough to load with a couple of instructions.
6135 If using 32-bit registers, set IMM to the high order 32 bits and
6136 OFFSET to the low order 32 bits. Otherwise, set IMM to the entire
6137 64 bit constant. */
351cdf24 6138 if (GPR_SIZE == 32 || (!using_gprs && FPR_SIZE != 64))
89565f1b
RS
6139 {
6140 imm->X_op = O_constant;
6141 offset->X_op = O_constant;
6142 if (!target_big_endian)
6143 {
6144 imm->X_add_number = bfd_getl32 (data + 4);
6145 offset->X_add_number = bfd_getl32 (data);
6146 }
6147 else
6148 {
6149 imm->X_add_number = bfd_getb32 (data);
6150 offset->X_add_number = bfd_getb32 (data + 4);
6151 }
6152 if (offset->X_add_number == 0)
6153 offset->X_op = O_absent;
6154 }
6155 else
6156 {
6157 imm->X_op = O_constant;
6158 if (!target_big_endian)
6159 imm->X_add_number = bfd_getl64 (data);
6160 else
6161 imm->X_add_number = bfd_getb64 (data);
6162 offset->X_op = O_absent;
6163 }
a92713e6 6164 return TRUE;
89565f1b
RS
6165 }
6166
6167 /* Switch to the right section. */
6168 seg = now_seg;
6169 subseg = now_subseg;
6170 if (length == 4)
6171 {
6172 gas_assert (!using_gprs && g_switch_value >= 4);
6173 newname = ".lit4";
6174 }
6175 else
6176 {
6177 if (using_gprs || g_switch_value < 8)
6178 newname = RDATA_SECTION_NAME;
6179 else
6180 newname = ".lit8";
6181 }
6182
6183 new_seg = subseg_new (newname, (subsegT) 0);
fd361982 6184 bfd_set_section_flags (new_seg,
89565f1b
RS
6185 SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA);
6186 frag_align (length == 4 ? 2 : 3, 0, 0);
6187 if (strncmp (TARGET_OS, "elf", 3) != 0)
6188 record_alignment (new_seg, 4);
6189 else
6190 record_alignment (new_seg, length == 4 ? 2 : 3);
6191 if (seg == now_seg)
1661c76c 6192 as_bad (_("cannot use `%s' in this section"), arg->insn->insn_mo->name);
89565f1b
RS
6193
6194 /* Set the argument to the current address in the section. */
6195 imm->X_op = O_absent;
6196 offset->X_op = O_symbol;
6197 offset->X_add_symbol = symbol_temp_new_now ();
6198 offset->X_add_number = 0;
6199
6200 /* Put the floating point number into the section. */
6201 p = frag_more (length);
6202 memcpy (p, data, length);
6203
6204 /* Switch back to the original section. */
6205 subseg_set (seg, subseg);
a92713e6 6206 return TRUE;
89565f1b
RS
6207}
6208
14daeee3
RS
6209/* OP_VU0_SUFFIX and OP_VU0_MATCH_SUFFIX matcher; MATCH_P selects between
6210 them. */
6211
6212static bfd_boolean
6213match_vu0_suffix_operand (struct mips_arg_info *arg,
6214 const struct mips_operand *operand,
6215 bfd_boolean match_p)
6216{
6217 unsigned int uval;
6218
6219 /* The operand can be an XYZW mask or a single 2-bit channel index
6220 (with X being 0). */
6221 gas_assert (operand->size == 2 || operand->size == 4);
6222
ee5734f0 6223 /* The suffix can be omitted when it is already part of the opcode. */
14daeee3 6224 if (arg->token->type != OT_CHANNELS)
ee5734f0 6225 return match_p;
14daeee3
RS
6226
6227 uval = arg->token->u.channels;
6228 if (operand->size == 2)
6229 {
6230 /* Check that a single bit is set and convert it into a 2-bit index. */
6231 if ((uval & -uval) != uval)
6232 return FALSE;
6233 uval = 4 - ffs (uval);
6234 }
6235
6236 if (match_p && insn_extract_operand (arg->insn, operand) != uval)
6237 return FALSE;
6238
6239 ++arg->token;
6240 if (!match_p)
6241 insn_insert_operand (arg->insn, operand, uval);
6242 return TRUE;
6243}
6244
33f46696
MR
6245/* Try to match a token from ARG against OPERAND. Consume the token
6246 and return true on success, otherwise return false. */
a1d78564 6247
a92713e6 6248static bfd_boolean
a1d78564 6249match_operand (struct mips_arg_info *arg,
a92713e6 6250 const struct mips_operand *operand)
a1d78564
RS
6251{
6252 switch (operand->type)
6253 {
6254 case OP_INT:
a92713e6 6255 return match_int_operand (arg, operand);
a1d78564
RS
6256
6257 case OP_MAPPED_INT:
a92713e6 6258 return match_mapped_int_operand (arg, operand);
a1d78564
RS
6259
6260 case OP_MSB:
a92713e6 6261 return match_msb_operand (arg, operand);
a1d78564
RS
6262
6263 case OP_REG:
0f35dbc4 6264 case OP_OPTIONAL_REG:
a92713e6 6265 return match_reg_operand (arg, operand);
a1d78564
RS
6266
6267 case OP_REG_PAIR:
a92713e6 6268 return match_reg_pair_operand (arg, operand);
a1d78564
RS
6269
6270 case OP_PCREL:
a92713e6 6271 return match_pcrel_operand (arg);
a1d78564
RS
6272
6273 case OP_PERF_REG:
a92713e6 6274 return match_perf_reg_operand (arg, operand);
a1d78564
RS
6275
6276 case OP_ADDIUSP_INT:
a92713e6 6277 return match_addiusp_operand (arg, operand);
a1d78564
RS
6278
6279 case OP_CLO_CLZ_DEST:
a92713e6 6280 return match_clo_clz_dest_operand (arg, operand);
a1d78564
RS
6281
6282 case OP_LWM_SWM_LIST:
a92713e6 6283 return match_lwm_swm_list_operand (arg, operand);
a1d78564
RS
6284
6285 case OP_ENTRY_EXIT_LIST:
a92713e6 6286 return match_entry_exit_operand (arg, operand);
364215c8 6287
a1d78564 6288 case OP_SAVE_RESTORE_LIST:
a92713e6 6289 return match_save_restore_list_operand (arg);
a1d78564
RS
6290
6291 case OP_MDMX_IMM_REG:
a92713e6 6292 return match_mdmx_imm_reg_operand (arg, operand);
a1d78564
RS
6293
6294 case OP_REPEAT_DEST_REG:
a92713e6 6295 return match_tied_reg_operand (arg, arg->dest_regno);
a1d78564
RS
6296
6297 case OP_REPEAT_PREV_REG:
a92713e6 6298 return match_tied_reg_operand (arg, arg->last_regno);
a1d78564
RS
6299
6300 case OP_PC:
a92713e6 6301 return match_pc_operand (arg);
14daeee3 6302
25499ac7
MR
6303 case OP_REG28:
6304 return match_reg28_operand (arg);
6305
14daeee3
RS
6306 case OP_VU0_SUFFIX:
6307 return match_vu0_suffix_operand (arg, operand, FALSE);
6308
6309 case OP_VU0_MATCH_SUFFIX:
6310 return match_vu0_suffix_operand (arg, operand, TRUE);
56d438b1
CF
6311
6312 case OP_IMM_INDEX:
6313 return match_imm_index_operand (arg, operand);
6314
6315 case OP_REG_INDEX:
6316 return match_reg_index_operand (arg, operand);
7361da2c
AB
6317
6318 case OP_SAME_RS_RT:
6319 return match_same_rs_rt_operand (arg, operand);
6320
6321 case OP_CHECK_PREV:
6322 return match_check_prev_operand (arg, operand);
6323
6324 case OP_NON_ZERO_REG:
6325 return match_non_zero_reg_operand (arg, operand);
a1d78564
RS
6326 }
6327 abort ();
6328}
6329
6330/* ARG is the state after successfully matching an instruction.
6331 Issue any queued-up warnings. */
6332
6333static void
6334check_completed_insn (struct mips_arg_info *arg)
6335{
6336 if (arg->seen_at)
6337 {
6338 if (AT == ATREG)
1661c76c 6339 as_warn (_("used $at without \".set noat\""));
a1d78564 6340 else
1661c76c 6341 as_warn (_("used $%u with \".set at=$%u\""), AT, AT);
a1d78564
RS
6342 }
6343}
a1d78564 6344
85fcb30f
RS
6345/* Return true if modifying general-purpose register REG needs a delay. */
6346
6347static bfd_boolean
6348reg_needs_delay (unsigned int reg)
6349{
6350 unsigned long prev_pinfo;
6351
6352 prev_pinfo = history[0].insn_mo->pinfo;
6353 if (!mips_opts.noreorder
67dc82bc 6354 && (((prev_pinfo & INSN_LOAD_MEMORY) && !gpr_interlocks)
43885403 6355 || ((prev_pinfo & INSN_LOAD_COPROC) && !cop_interlocks))
85fcb30f
RS
6356 && (gpr_write_mask (&history[0]) & (1 << reg)))
6357 return TRUE;
6358
6359 return FALSE;
6360}
6361
71400594
RS
6362/* Classify an instruction according to the FIX_VR4120_* enumeration.
6363 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
6364 by VR4120 errata. */
4d7206a2 6365
71400594
RS
6366static unsigned int
6367classify_vr4120_insn (const char *name)
252b5132 6368{
71400594
RS
6369 if (strncmp (name, "macc", 4) == 0)
6370 return FIX_VR4120_MACC;
6371 if (strncmp (name, "dmacc", 5) == 0)
6372 return FIX_VR4120_DMACC;
6373 if (strncmp (name, "mult", 4) == 0)
6374 return FIX_VR4120_MULT;
6375 if (strncmp (name, "dmult", 5) == 0)
6376 return FIX_VR4120_DMULT;
6377 if (strstr (name, "div"))
6378 return FIX_VR4120_DIV;
6379 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
6380 return FIX_VR4120_MTHILO;
6381 return NUM_FIX_VR4120_CLASSES;
6382}
252b5132 6383
a8d14a88
CM
6384#define INSN_ERET 0x42000018
6385#define INSN_DERET 0x4200001f
6386#define INSN_DMULT 0x1c
6387#define INSN_DMULTU 0x1d
ff239038 6388
71400594
RS
6389/* Return the number of instructions that must separate INSN1 and INSN2,
6390 where INSN1 is the earlier instruction. Return the worst-case value
6391 for any INSN2 if INSN2 is null. */
252b5132 6392
71400594
RS
6393static unsigned int
6394insns_between (const struct mips_cl_insn *insn1,
6395 const struct mips_cl_insn *insn2)
6396{
6397 unsigned long pinfo1, pinfo2;
4c260379 6398 unsigned int mask;
71400594 6399
85fcb30f
RS
6400 /* If INFO2 is null, pessimistically assume that all flags are set for
6401 the second instruction. */
71400594
RS
6402 pinfo1 = insn1->insn_mo->pinfo;
6403 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
252b5132 6404
71400594
RS
6405 /* For most targets, write-after-read dependencies on the HI and LO
6406 registers must be separated by at least two instructions. */
6407 if (!hilo_interlocks)
252b5132 6408 {
71400594
RS
6409 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
6410 return 2;
6411 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
6412 return 2;
6413 }
6414
6415 /* If we're working around r7000 errata, there must be two instructions
6416 between an mfhi or mflo and any instruction that uses the result. */
6417 if (mips_7000_hilo_fix
df58fc94 6418 && !mips_opts.micromips
71400594 6419 && MF_HILO_INSN (pinfo1)
85fcb30f 6420 && (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1))))
71400594
RS
6421 return 2;
6422
ff239038
CM
6423 /* If we're working around 24K errata, one instruction is required
6424 if an ERET or DERET is followed by a branch instruction. */
df58fc94 6425 if (mips_fix_24k && !mips_opts.micromips)
ff239038
CM
6426 {
6427 if (insn1->insn_opcode == INSN_ERET
6428 || insn1->insn_opcode == INSN_DERET)
6429 {
6430 if (insn2 == NULL
6431 || insn2->insn_opcode == INSN_ERET
6432 || insn2->insn_opcode == INSN_DERET
11625dd8 6433 || delayed_branch_p (insn2))
ff239038
CM
6434 return 1;
6435 }
6436 }
6437
a8d14a88
CM
6438 /* If we're working around PMC RM7000 errata, there must be three
6439 nops between a dmult and a load instruction. */
6440 if (mips_fix_rm7000 && !mips_opts.micromips)
6441 {
6442 if ((insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULT
6443 || (insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULTU)
6444 {
6445 if (pinfo2 & INSN_LOAD_MEMORY)
6446 return 3;
6447 }
6448 }
6449
71400594
RS
6450 /* If working around VR4120 errata, check for combinations that need
6451 a single intervening instruction. */
df58fc94 6452 if (mips_fix_vr4120 && !mips_opts.micromips)
71400594
RS
6453 {
6454 unsigned int class1, class2;
252b5132 6455
71400594
RS
6456 class1 = classify_vr4120_insn (insn1->insn_mo->name);
6457 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
252b5132 6458 {
71400594
RS
6459 if (insn2 == NULL)
6460 return 1;
6461 class2 = classify_vr4120_insn (insn2->insn_mo->name);
6462 if (vr4120_conflicts[class1] & (1 << class2))
6463 return 1;
252b5132 6464 }
71400594
RS
6465 }
6466
df58fc94 6467 if (!HAVE_CODE_COMPRESSION)
71400594
RS
6468 {
6469 /* Check for GPR or coprocessor load delays. All such delays
6470 are on the RT register. */
6471 /* Itbl support may require additional care here. */
67dc82bc 6472 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY))
43885403 6473 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC)))
252b5132 6474 {
85fcb30f 6475 if (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1)))
71400594
RS
6476 return 1;
6477 }
6478
6479 /* Check for generic coprocessor hazards.
6480
6481 This case is not handled very well. There is no special
6482 knowledge of CP0 handling, and the coprocessors other than
6483 the floating point unit are not distinguished at all. */
6484 /* Itbl support may require additional care here. FIXME!
6485 Need to modify this to include knowledge about
6486 user specified delays! */
43885403 6487 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE))
71400594
RS
6488 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
6489 {
6490 /* Handle cases where INSN1 writes to a known general coprocessor
6491 register. There must be a one instruction delay before INSN2
6492 if INSN2 reads that register, otherwise no delay is needed. */
4c260379
RS
6493 mask = fpr_write_mask (insn1);
6494 if (mask != 0)
252b5132 6495 {
4c260379 6496 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
71400594 6497 return 1;
252b5132
RH
6498 }
6499 else
6500 {
71400594
RS
6501 /* Read-after-write dependencies on the control registers
6502 require a two-instruction gap. */
6503 if ((pinfo1 & INSN_WRITE_COND_CODE)
6504 && (pinfo2 & INSN_READ_COND_CODE))
6505 return 2;
6506
6507 /* We don't know exactly what INSN1 does. If INSN2 is
6508 also a coprocessor instruction, assume there must be
6509 a one instruction gap. */
6510 if (pinfo2 & INSN_COP)
6511 return 1;
252b5132
RH
6512 }
6513 }
6b76fefe 6514
71400594
RS
6515 /* Check for read-after-write dependencies on the coprocessor
6516 control registers in cases where INSN1 does not need a general
6517 coprocessor delay. This means that INSN1 is a floating point
6518 comparison instruction. */
6519 /* Itbl support may require additional care here. */
6520 else if (!cop_interlocks
6521 && (pinfo1 & INSN_WRITE_COND_CODE)
6522 && (pinfo2 & INSN_READ_COND_CODE))
6523 return 1;
6524 }
6b76fefe 6525
7361da2c
AB
6526 /* Forbidden slots can not contain Control Transfer Instructions (CTIs)
6527 CTIs include all branches and jumps, nal, eret, eretnc, deret, wait
6528 and pause. */
6529 if ((insn1->insn_mo->pinfo2 & INSN2_FORBIDDEN_SLOT)
6530 && ((pinfo2 & INSN_NO_DELAY_SLOT)
6531 || (insn2 && delayed_branch_p (insn2))))
6532 return 1;
6533
71400594
RS
6534 return 0;
6535}
6b76fefe 6536
7d8e00cf
RS
6537/* Return the number of nops that would be needed to work around the
6538 VR4130 mflo/mfhi errata if instruction INSN immediately followed
932d1a1b
RS
6539 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
6540 that are contained within the first IGNORE instructions of HIST. */
7d8e00cf
RS
6541
6542static int
932d1a1b 6543nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
7d8e00cf
RS
6544 const struct mips_cl_insn *insn)
6545{
4c260379
RS
6546 int i, j;
6547 unsigned int mask;
7d8e00cf
RS
6548
6549 /* Check if the instruction writes to HI or LO. MTHI and MTLO
6550 are not affected by the errata. */
6551 if (insn != 0
6552 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
6553 || strcmp (insn->insn_mo->name, "mtlo") == 0
6554 || strcmp (insn->insn_mo->name, "mthi") == 0))
6555 return 0;
6556
6557 /* Search for the first MFLO or MFHI. */
6558 for (i = 0; i < MAX_VR4130_NOPS; i++)
91d6fa6a 6559 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
7d8e00cf
RS
6560 {
6561 /* Extract the destination register. */
4c260379 6562 mask = gpr_write_mask (&hist[i]);
7d8e00cf
RS
6563
6564 /* No nops are needed if INSN reads that register. */
4c260379 6565 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
7d8e00cf
RS
6566 return 0;
6567
6568 /* ...or if any of the intervening instructions do. */
6569 for (j = 0; j < i; j++)
4c260379 6570 if (gpr_read_mask (&hist[j]) & mask)
7d8e00cf
RS
6571 return 0;
6572
932d1a1b
RS
6573 if (i >= ignore)
6574 return MAX_VR4130_NOPS - i;
7d8e00cf
RS
6575 }
6576 return 0;
6577}
6578
134c0c8b
MR
6579#define BASE_REG_EQ(INSN1, INSN2) \
6580 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
15be625d
CM
6581 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
6582
6583/* Return the minimum alignment for this store instruction. */
6584
6585static int
6586fix_24k_align_to (const struct mips_opcode *mo)
6587{
6588 if (strcmp (mo->name, "sh") == 0)
6589 return 2;
6590
6591 if (strcmp (mo->name, "swc1") == 0
6592 || strcmp (mo->name, "swc2") == 0
6593 || strcmp (mo->name, "sw") == 0
6594 || strcmp (mo->name, "sc") == 0
6595 || strcmp (mo->name, "s.s") == 0)
6596 return 4;
6597
6598 if (strcmp (mo->name, "sdc1") == 0
6599 || strcmp (mo->name, "sdc2") == 0
6600 || strcmp (mo->name, "s.d") == 0)
6601 return 8;
6602
6603 /* sb, swl, swr */
6604 return 1;
6605}
6606
6607struct fix_24k_store_info
6608 {
6609 /* Immediate offset, if any, for this store instruction. */
6610 short off;
6611 /* Alignment required by this store instruction. */
6612 int align_to;
6613 /* True for register offsets. */
6614 int register_offset;
6615 };
6616
6617/* Comparison function used by qsort. */
6618
6619static int
6620fix_24k_sort (const void *a, const void *b)
6621{
6622 const struct fix_24k_store_info *pos1 = a;
6623 const struct fix_24k_store_info *pos2 = b;
6624
6625 return (pos1->off - pos2->off);
6626}
6627
6628/* INSN is a store instruction. Try to record the store information
6629 in STINFO. Return false if the information isn't known. */
6630
6631static bfd_boolean
6632fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
ab9794cf 6633 const struct mips_cl_insn *insn)
15be625d
CM
6634{
6635 /* The instruction must have a known offset. */
6636 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
6637 return FALSE;
6638
6639 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
6640 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
6641 return TRUE;
6642}
6643
932d1a1b
RS
6644/* Return the number of nops that would be needed to work around the 24k
6645 "lost data on stores during refill" errata if instruction INSN
6646 immediately followed the 2 instructions described by HIST.
6647 Ignore hazards that are contained within the first IGNORE
6648 instructions of HIST.
6649
6650 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
6651 for the data cache refills and store data. The following describes
6652 the scenario where the store data could be lost.
6653
6654 * A data cache miss, due to either a load or a store, causing fill
6655 data to be supplied by the memory subsystem
6656 * The first three doublewords of fill data are returned and written
6657 into the cache
6658 * A sequence of four stores occurs in consecutive cycles around the
6659 final doubleword of the fill:
6660 * Store A
6661 * Store B
6662 * Store C
6663 * Zero, One or more instructions
6664 * Store D
6665
6666 The four stores A-D must be to different doublewords of the line that
6667 is being filled. The fourth instruction in the sequence above permits
6668 the fill of the final doubleword to be transferred from the FSB into
6669 the cache. In the sequence above, the stores may be either integer
6670 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
6671 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
6672 different doublewords on the line. If the floating point unit is
6673 running in 1:2 mode, it is not possible to create the sequence above
6674 using only floating point store instructions.
15be625d
CM
6675
6676 In this case, the cache line being filled is incorrectly marked
6677 invalid, thereby losing the data from any store to the line that
6678 occurs between the original miss and the completion of the five
6679 cycle sequence shown above.
6680
932d1a1b 6681 The workarounds are:
15be625d 6682
932d1a1b
RS
6683 * Run the data cache in write-through mode.
6684 * Insert a non-store instruction between
6685 Store A and Store B or Store B and Store C. */
3739860c 6686
15be625d 6687static int
932d1a1b 6688nops_for_24k (int ignore, const struct mips_cl_insn *hist,
15be625d
CM
6689 const struct mips_cl_insn *insn)
6690{
6691 struct fix_24k_store_info pos[3];
6692 int align, i, base_offset;
6693
932d1a1b
RS
6694 if (ignore >= 2)
6695 return 0;
6696
ab9794cf
RS
6697 /* If the previous instruction wasn't a store, there's nothing to
6698 worry about. */
15be625d
CM
6699 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6700 return 0;
6701
ab9794cf
RS
6702 /* If the instructions after the previous one are unknown, we have
6703 to assume the worst. */
6704 if (!insn)
15be625d
CM
6705 return 1;
6706
ab9794cf
RS
6707 /* Check whether we are dealing with three consecutive stores. */
6708 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
6709 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
15be625d
CM
6710 return 0;
6711
6712 /* If we don't know the relationship between the store addresses,
6713 assume the worst. */
ab9794cf 6714 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
15be625d
CM
6715 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
6716 return 1;
6717
6718 if (!fix_24k_record_store_info (&pos[0], insn)
6719 || !fix_24k_record_store_info (&pos[1], &hist[0])
6720 || !fix_24k_record_store_info (&pos[2], &hist[1]))
6721 return 1;
6722
6723 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
6724
6725 /* Pick a value of ALIGN and X such that all offsets are adjusted by
6726 X bytes and such that the base register + X is known to be aligned
6727 to align bytes. */
6728
6729 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
6730 align = 8;
6731 else
6732 {
6733 align = pos[0].align_to;
6734 base_offset = pos[0].off;
6735 for (i = 1; i < 3; i++)
6736 if (align < pos[i].align_to)
6737 {
6738 align = pos[i].align_to;
6739 base_offset = pos[i].off;
6740 }
6741 for (i = 0; i < 3; i++)
6742 pos[i].off -= base_offset;
6743 }
6744
6745 pos[0].off &= ~align + 1;
6746 pos[1].off &= ~align + 1;
6747 pos[2].off &= ~align + 1;
6748
6749 /* If any two stores write to the same chunk, they also write to the
6750 same doubleword. The offsets are still sorted at this point. */
6751 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
6752 return 0;
6753
6754 /* A range of at least 9 bytes is needed for the stores to be in
6755 non-overlapping doublewords. */
6756 if (pos[2].off - pos[0].off <= 8)
6757 return 0;
6758
6759 if (pos[2].off - pos[1].off >= 24
6760 || pos[1].off - pos[0].off >= 24
6761 || pos[2].off - pos[0].off >= 32)
6762 return 0;
6763
6764 return 1;
6765}
6766
71400594 6767/* Return the number of nops that would be needed if instruction INSN
91d6fa6a 6768 immediately followed the MAX_NOPS instructions given by HIST,
932d1a1b
RS
6769 where HIST[0] is the most recent instruction. Ignore hazards
6770 between INSN and the first IGNORE instructions in HIST.
6771
6772 If INSN is null, return the worse-case number of nops for any
6773 instruction. */
bdaaa2e1 6774
71400594 6775static int
932d1a1b 6776nops_for_insn (int ignore, const struct mips_cl_insn *hist,
71400594
RS
6777 const struct mips_cl_insn *insn)
6778{
6779 int i, nops, tmp_nops;
bdaaa2e1 6780
71400594 6781 nops = 0;
932d1a1b 6782 for (i = ignore; i < MAX_DELAY_NOPS; i++)
65b02341 6783 {
91d6fa6a 6784 tmp_nops = insns_between (hist + i, insn) - i;
65b02341
RS
6785 if (tmp_nops > nops)
6786 nops = tmp_nops;
6787 }
7d8e00cf 6788
df58fc94 6789 if (mips_fix_vr4130 && !mips_opts.micromips)
7d8e00cf 6790 {
932d1a1b 6791 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
7d8e00cf
RS
6792 if (tmp_nops > nops)
6793 nops = tmp_nops;
6794 }
6795
df58fc94 6796 if (mips_fix_24k && !mips_opts.micromips)
15be625d 6797 {
932d1a1b 6798 tmp_nops = nops_for_24k (ignore, hist, insn);
15be625d
CM
6799 if (tmp_nops > nops)
6800 nops = tmp_nops;
6801 }
6802
71400594
RS
6803 return nops;
6804}
252b5132 6805
71400594 6806/* The variable arguments provide NUM_INSNS extra instructions that
91d6fa6a 6807 might be added to HIST. Return the largest number of nops that
932d1a1b
RS
6808 would be needed after the extended sequence, ignoring hazards
6809 in the first IGNORE instructions. */
252b5132 6810
71400594 6811static int
932d1a1b
RS
6812nops_for_sequence (int num_insns, int ignore,
6813 const struct mips_cl_insn *hist, ...)
71400594
RS
6814{
6815 va_list args;
6816 struct mips_cl_insn buffer[MAX_NOPS];
6817 struct mips_cl_insn *cursor;
6818 int nops;
6819
91d6fa6a 6820 va_start (args, hist);
71400594 6821 cursor = buffer + num_insns;
91d6fa6a 6822 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
71400594
RS
6823 while (cursor > buffer)
6824 *--cursor = *va_arg (args, const struct mips_cl_insn *);
6825
932d1a1b 6826 nops = nops_for_insn (ignore, buffer, NULL);
71400594
RS
6827 va_end (args);
6828 return nops;
6829}
252b5132 6830
71400594
RS
6831/* Like nops_for_insn, but if INSN is a branch, take into account the
6832 worst-case delay for the branch target. */
252b5132 6833
71400594 6834static int
932d1a1b 6835nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
71400594
RS
6836 const struct mips_cl_insn *insn)
6837{
6838 int nops, tmp_nops;
60b63b72 6839
932d1a1b 6840 nops = nops_for_insn (ignore, hist, insn);
11625dd8 6841 if (delayed_branch_p (insn))
71400594 6842 {
932d1a1b 6843 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
14fe068b 6844 hist, insn, get_delay_slot_nop (insn));
71400594
RS
6845 if (tmp_nops > nops)
6846 nops = tmp_nops;
6847 }
11625dd8 6848 else if (compact_branch_p (insn))
71400594 6849 {
932d1a1b 6850 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
71400594
RS
6851 if (tmp_nops > nops)
6852 nops = tmp_nops;
6853 }
6854 return nops;
6855}
6856
c67a084a
NC
6857/* Fix NOP issue: Replace nops by "or at,at,zero". */
6858
6859static void
6860fix_loongson2f_nop (struct mips_cl_insn * ip)
6861{
df58fc94 6862 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
6863 if (strcmp (ip->insn_mo->name, "nop") == 0)
6864 ip->insn_opcode = LOONGSON2F_NOP_INSN;
6865}
6866
6867/* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
6868 jr target pc &= 'hffff_ffff_cfff_ffff. */
6869
6870static void
6871fix_loongson2f_jump (struct mips_cl_insn * ip)
6872{
df58fc94 6873 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
6874 if (strcmp (ip->insn_mo->name, "j") == 0
6875 || strcmp (ip->insn_mo->name, "jr") == 0
6876 || strcmp (ip->insn_mo->name, "jalr") == 0)
6877 {
6878 int sreg;
6879 expressionS ep;
6880
6881 if (! mips_opts.at)
6882 return;
6883
df58fc94 6884 sreg = EXTRACT_OPERAND (0, RS, *ip);
c67a084a
NC
6885 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
6886 return;
6887
6888 ep.X_op = O_constant;
6889 ep.X_add_number = 0xcfff0000;
6890 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
6891 ep.X_add_number = 0xffff;
6892 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
6893 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
6894 }
6895}
6896
6897static void
6898fix_loongson2f (struct mips_cl_insn * ip)
6899{
6900 if (mips_fix_loongson2f_nop)
6901 fix_loongson2f_nop (ip);
6902
6903 if (mips_fix_loongson2f_jump)
6904 fix_loongson2f_jump (ip);
6905}
6906
dec7b24b
YS
6907static bfd_boolean
6908has_label_name (const char *arr[], size_t len ,const char *s)
6909{
6910 unsigned long i;
6911 for (i = 0; i < len; i++)
6912 {
6913 if (!arr[i])
6914 return FALSE;
6915 if (streq (arr[i], s))
6916 return TRUE;
6917 }
6918 return FALSE;
6919}
6920
6921/* Fix loongson3 llsc errata: Insert sync before ll/lld. */
6f2117ba
PH
6922
6923static void
6924fix_loongson3_llsc (struct mips_cl_insn * ip)
6925{
6926 gas_assert (!HAVE_CODE_COMPRESSION);
6927
6928 /* If is an local label and the insn is not sync,
6929 look forward that whether an branch between ll/sc jump to here
6930 if so, insert a sync. */
6931 if (seg_info (now_seg)->label_list
6932 && S_IS_LOCAL (seg_info (now_seg)->label_list->label)
6933 && (strcmp (ip->insn_mo->name, "sync") != 0))
6934 {
6f2117ba 6935 unsigned long i;
dec7b24b
YS
6936 valueT label_value;
6937 const char *label_names[MAX_LABELS_SAME];
6938 const char *label_name;
6939
6940 label_name = S_GET_NAME (seg_info (now_seg)->label_list->label);
6941 label_names[0] = label_name;
6942 struct insn_label_list *llist = seg_info (now_seg)->label_list;
6943 label_value = S_GET_VALUE (llist->label);
6f2117ba 6944
dec7b24b
YS
6945 for (i = 1; i < MAX_LABELS_SAME; i++)
6946 {
6947 llist = llist->next;
6948 if (!llist)
6949 break;
6950 if (S_GET_VALUE (llist->label) == label_value)
6951 label_names[i] = S_GET_NAME (llist->label);
6952 else
6953 break;
6954 }
6955 for (; i < MAX_LABELS_SAME; i++)
6956 label_names[i] = NULL;
6957
6958 unsigned long lookback = ARRAY_SIZE (history);
6f2117ba
PH
6959 for (i = 0; i < lookback; i++)
6960 {
6961 if (streq (history[i].insn_mo->name, "ll")
6962 || streq (history[i].insn_mo->name, "lld"))
6963 break;
6964
6965 if (streq (history[i].insn_mo->name, "sc")
6966 || streq (history[i].insn_mo->name, "scd"))
6967 {
6968 unsigned long j;
6969
6970 for (j = i + 1; j < lookback; j++)
6971 {
6972 if (streq (history[i].insn_mo->name, "ll")
6973 || streq (history[i].insn_mo->name, "lld"))
6974 break;
6975
6976 if (delayed_branch_p (&history[j]))
6977 {
dec7b24b
YS
6978 if (has_label_name (label_names,
6979 MAX_LABELS_SAME,
6980 history[j].target))
6f2117ba
PH
6981 {
6982 add_fixed_insn (&sync_insn);
6983 insert_into_history (0, 1, &sync_insn);
6984 i = lookback;
6985 break;
6986 }
6987 }
6988 }
6989 }
6990 }
6991 }
6992 /* If we find a sc, we look forward to look for an branch insn,
6993 and see whether it jump back and out of ll/sc. */
dec7b24b 6994 else if (streq (ip->insn_mo->name, "sc") || streq (ip->insn_mo->name, "scd"))
6f2117ba
PH
6995 {
6996 unsigned long lookback = ARRAY_SIZE (history) - 1;
6997 unsigned long i;
6998
6999 for (i = 0; i < lookback; i++)
7000 {
7001 if (streq (history[i].insn_mo->name, "ll")
7002 || streq (history[i].insn_mo->name, "lld"))
7003 break;
7004
7005 if (delayed_branch_p (&history[i]))
7006 {
7007 unsigned long j;
7008
7009 for (j = i + 1; j < lookback; j++)
7010 {
7011 if (streq (history[j].insn_mo->name, "ll")
7012 || streq (history[i].insn_mo->name, "lld"))
7013 break;
7014 }
7015
7016 for (; j < lookback; j++)
7017 {
7018 if (history[j].label[0] != '\0'
7019 && streq (history[j].label, history[i].target)
7020 && strcmp (history[j+1].insn_mo->name, "sync") != 0)
7021 {
7022 add_fixed_insn (&sync_insn);
7023 insert_into_history (++j, 1, &sync_insn);
7024 }
7025 }
7026 }
7027 }
7028 }
7029
7030 /* Skip if there is a sync before ll/lld. */
7031 if ((strcmp (ip->insn_mo->name, "ll") == 0
7032 || strcmp (ip->insn_mo->name, "lld") == 0)
7033 && (strcmp (history[0].insn_mo->name, "sync") != 0))
7034 {
7035 add_fixed_insn (&sync_insn);
7036 insert_into_history (0, 1, &sync_insn);
7037 }
7038}
7039
a4e06468
RS
7040/* IP is a branch that has a delay slot, and we need to fill it
7041 automatically. Return true if we can do that by swapping IP
e407c74b
NC
7042 with the previous instruction.
7043 ADDRESS_EXPR is an operand of the instruction to be used with
7044 RELOC_TYPE. */
a4e06468
RS
7045
7046static bfd_boolean
e407c74b 7047can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 7048 bfd_reloc_code_real_type *reloc_type)
a4e06468 7049{
2b0c8b40 7050 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
a4e06468 7051 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
9d5de888 7052 unsigned int fpr_read, prev_fpr_write;
a4e06468
RS
7053
7054 /* -O2 and above is required for this optimization. */
7055 if (mips_optimize < 2)
7056 return FALSE;
7057
7058 /* If we have seen .set volatile or .set nomove, don't optimize. */
7059 if (mips_opts.nomove)
7060 return FALSE;
7061
7062 /* We can't swap if the previous instruction's position is fixed. */
7063 if (history[0].fixed_p)
7064 return FALSE;
7065
7066 /* If the previous previous insn was in a .set noreorder, we can't
7067 swap. Actually, the MIPS assembler will swap in this situation.
7068 However, gcc configured -with-gnu-as will generate code like
7069
7070 .set noreorder
7071 lw $4,XXX
7072 .set reorder
7073 INSN
7074 bne $4,$0,foo
7075
7076 in which we can not swap the bne and INSN. If gcc is not configured
7077 -with-gnu-as, it does not output the .set pseudo-ops. */
7078 if (history[1].noreorder_p)
7079 return FALSE;
7080
87333bb7
MR
7081 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
7082 This means that the previous instruction was a 4-byte one anyhow. */
a4e06468
RS
7083 if (mips_opts.mips16 && history[0].fixp[0])
7084 return FALSE;
7085
7086 /* If the branch is itself the target of a branch, we can not swap.
7087 We cheat on this; all we check for is whether there is a label on
7088 this instruction. If there are any branches to anything other than
7089 a label, users must use .set noreorder. */
7090 if (seg_info (now_seg)->label_list)
7091 return FALSE;
7092
7093 /* If the previous instruction is in a variant frag other than this
2309ddf2 7094 branch's one, we cannot do the swap. This does not apply to
9301f9c3
MR
7095 MIPS16 code, which uses variant frags for different purposes. */
7096 if (!mips_opts.mips16
a4e06468
RS
7097 && history[0].frag
7098 && history[0].frag->fr_type == rs_machine_dependent)
7099 return FALSE;
7100
bcd530a7
RS
7101 /* We do not swap with instructions that cannot architecturally
7102 be placed in a branch delay slot, such as SYNC or ERET. We
7103 also refrain from swapping with a trap instruction, since it
7104 complicates trap handlers to have the trap instruction be in
7105 a delay slot. */
a4e06468 7106 prev_pinfo = history[0].insn_mo->pinfo;
bcd530a7 7107 if (prev_pinfo & INSN_NO_DELAY_SLOT)
a4e06468
RS
7108 return FALSE;
7109
7110 /* Check for conflicts between the branch and the instructions
7111 before the candidate delay slot. */
7112 if (nops_for_insn (0, history + 1, ip) > 0)
7113 return FALSE;
7114
7115 /* Check for conflicts between the swapped sequence and the
7116 target of the branch. */
7117 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
7118 return FALSE;
7119
7120 /* If the branch reads a register that the previous
7121 instruction sets, we can not swap. */
7122 gpr_read = gpr_read_mask (ip);
7123 prev_gpr_write = gpr_write_mask (&history[0]);
7124 if (gpr_read & prev_gpr_write)
7125 return FALSE;
7126
9d5de888
CF
7127 fpr_read = fpr_read_mask (ip);
7128 prev_fpr_write = fpr_write_mask (&history[0]);
7129 if (fpr_read & prev_fpr_write)
7130 return FALSE;
7131
a4e06468
RS
7132 /* If the branch writes a register that the previous
7133 instruction sets, we can not swap. */
7134 gpr_write = gpr_write_mask (ip);
7135 if (gpr_write & prev_gpr_write)
7136 return FALSE;
7137
7138 /* If the branch writes a register that the previous
7139 instruction reads, we can not swap. */
7140 prev_gpr_read = gpr_read_mask (&history[0]);
7141 if (gpr_write & prev_gpr_read)
7142 return FALSE;
7143
7144 /* If one instruction sets a condition code and the
7145 other one uses a condition code, we can not swap. */
7146 pinfo = ip->insn_mo->pinfo;
7147 if ((pinfo & INSN_READ_COND_CODE)
7148 && (prev_pinfo & INSN_WRITE_COND_CODE))
7149 return FALSE;
7150 if ((pinfo & INSN_WRITE_COND_CODE)
7151 && (prev_pinfo & INSN_READ_COND_CODE))
7152 return FALSE;
7153
7154 /* If the previous instruction uses the PC, we can not swap. */
2b0c8b40 7155 prev_pinfo2 = history[0].insn_mo->pinfo2;
26545944 7156 if (prev_pinfo2 & INSN2_READ_PC)
2b0c8b40 7157 return FALSE;
a4e06468 7158
df58fc94
RS
7159 /* If the previous instruction has an incorrect size for a fixed
7160 branch delay slot in microMIPS mode, we cannot swap. */
2309ddf2
MR
7161 pinfo2 = ip->insn_mo->pinfo2;
7162 if (mips_opts.micromips
7163 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
7164 && insn_length (history) != 2)
7165 return FALSE;
7166 if (mips_opts.micromips
7167 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
7168 && insn_length (history) != 4)
7169 return FALSE;
7170
33d64ca5
FN
7171 /* On the R5900 short loops need to be fixed by inserting a NOP in the
7172 branch delay slot.
7173
7174 The short loop bug under certain conditions causes loops to execute
7175 only once or twice. We must ensure that the assembler never
7176 generates loops that satisfy all of the following conditions:
7177
7178 - a loop consists of less than or equal to six instructions
7179 (including the branch delay slot);
7180 - a loop contains only one conditional branch instruction at the end
7181 of the loop;
7182 - a loop does not contain any other branch or jump instructions;
7183 - a branch delay slot of the loop is not NOP (EE 2.9 or later).
7184
7185 We need to do this because of a hardware bug in the R5900 chip. */
27c634e0 7186 if (mips_fix_r5900
e407c74b
NC
7187 /* Check if instruction has a parameter, ignore "j $31". */
7188 && (address_expr != NULL)
7189 /* Parameter must be 16 bit. */
7190 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
7191 /* Branch to same segment. */
41065f5e 7192 && (S_GET_SEGMENT (address_expr->X_add_symbol) == now_seg)
e407c74b 7193 /* Branch to same code fragment. */
41065f5e 7194 && (symbol_get_frag (address_expr->X_add_symbol) == frag_now)
e407c74b 7195 /* Can only calculate branch offset if value is known. */
41065f5e 7196 && symbol_constant_p (address_expr->X_add_symbol)
e407c74b
NC
7197 /* Check if branch is really conditional. */
7198 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
7199 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
7200 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
7201 {
7202 int distance;
33d64ca5
FN
7203 /* Check if loop is shorter than or equal to 6 instructions
7204 including branch and delay slot. */
41065f5e 7205 distance = frag_now_fix () - S_GET_VALUE (address_expr->X_add_symbol);
e407c74b
NC
7206 if (distance <= 20)
7207 {
7208 int i;
7209 int rv;
7210
7211 rv = FALSE;
7212 /* When the loop includes branches or jumps,
7213 it is not a short loop. */
7214 for (i = 0; i < (distance / 4); i++)
7215 {
7216 if ((history[i].cleared_p)
41065f5e 7217 || delayed_branch_p (&history[i]))
e407c74b
NC
7218 {
7219 rv = TRUE;
7220 break;
7221 }
7222 }
535b785f 7223 if (!rv)
e407c74b
NC
7224 {
7225 /* Insert nop after branch to fix short loop. */
7226 return FALSE;
7227 }
7228 }
7229 }
7230
a4e06468
RS
7231 return TRUE;
7232}
7233
e407c74b
NC
7234/* Decide how we should add IP to the instruction stream.
7235 ADDRESS_EXPR is an operand of the instruction to be used with
7236 RELOC_TYPE. */
a4e06468
RS
7237
7238static enum append_method
e407c74b 7239get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 7240 bfd_reloc_code_real_type *reloc_type)
a4e06468 7241{
a4e06468
RS
7242 /* The relaxed version of a macro sequence must be inherently
7243 hazard-free. */
7244 if (mips_relax.sequence == 2)
7245 return APPEND_ADD;
7246
3b821a28 7247 /* We must not dabble with instructions in a ".set noreorder" block. */
a4e06468
RS
7248 if (mips_opts.noreorder)
7249 return APPEND_ADD;
7250
7251 /* Otherwise, it's our responsibility to fill branch delay slots. */
11625dd8 7252 if (delayed_branch_p (ip))
a4e06468 7253 {
e407c74b
NC
7254 if (!branch_likely_p (ip)
7255 && can_swap_branch_p (ip, address_expr, reloc_type))
a4e06468
RS
7256 return APPEND_SWAP;
7257
7258 if (mips_opts.mips16
7259 && ISA_SUPPORTS_MIPS16E
fc76e730 7260 && gpr_read_mask (ip) != 0)
a4e06468
RS
7261 return APPEND_ADD_COMPACT;
7262
7bd374a4
MR
7263 if (mips_opts.micromips
7264 && ((ip->insn_opcode & 0xffe0) == 0x4580
7265 || (!forced_insn_length
7266 && ((ip->insn_opcode & 0xfc00) == 0xcc00
7267 || (ip->insn_opcode & 0xdc00) == 0x8c00))
7268 || (ip->insn_opcode & 0xdfe00000) == 0x94000000
7269 || (ip->insn_opcode & 0xdc1f0000) == 0x94000000))
7270 return APPEND_ADD_COMPACT;
7271
a4e06468
RS
7272 return APPEND_ADD_WITH_NOP;
7273 }
7274
a4e06468
RS
7275 return APPEND_ADD;
7276}
7277
7bd374a4
MR
7278/* IP is an instruction whose opcode we have just changed, END points
7279 to the end of the opcode table processed. Point IP->insn_mo to the
7280 new opcode's definition. */
ceb94aa5
RS
7281
7282static void
7bd374a4 7283find_altered_opcode (struct mips_cl_insn *ip, const struct mips_opcode *end)
ceb94aa5 7284{
7bd374a4 7285 const struct mips_opcode *mo;
ceb94aa5 7286
ceb94aa5 7287 for (mo = ip->insn_mo; mo < end; mo++)
7bd374a4
MR
7288 if (mo->pinfo != INSN_MACRO
7289 && (ip->insn_opcode & mo->mask) == mo->match)
ceb94aa5
RS
7290 {
7291 ip->insn_mo = mo;
7292 return;
7293 }
7294 abort ();
7295}
7296
7bd374a4
MR
7297/* IP is a MIPS16 instruction whose opcode we have just changed.
7298 Point IP->insn_mo to the new opcode's definition. */
7299
7300static void
7301find_altered_mips16_opcode (struct mips_cl_insn *ip)
7302{
7303 find_altered_opcode (ip, &mips16_opcodes[bfd_mips16_num_opcodes]);
7304}
7305
7306/* IP is a microMIPS instruction whose opcode we have just changed.
7307 Point IP->insn_mo to the new opcode's definition. */
7308
7309static void
7310find_altered_micromips_opcode (struct mips_cl_insn *ip)
7311{
7312 find_altered_opcode (ip, &micromips_opcodes[bfd_micromips_num_opcodes]);
7313}
7314
df58fc94
RS
7315/* For microMIPS macros, we need to generate a local number label
7316 as the target of branches. */
7317#define MICROMIPS_LABEL_CHAR '\037'
7318static unsigned long micromips_target_label;
7319static char micromips_target_name[32];
7320
7321static char *
7322micromips_label_name (void)
7323{
7324 char *p = micromips_target_name;
7325 char symbol_name_temporary[24];
7326 unsigned long l;
7327 int i;
7328
7329 if (*p)
7330 return p;
7331
7332 i = 0;
7333 l = micromips_target_label;
7334#ifdef LOCAL_LABEL_PREFIX
7335 *p++ = LOCAL_LABEL_PREFIX;
7336#endif
7337 *p++ = 'L';
7338 *p++ = MICROMIPS_LABEL_CHAR;
7339 do
7340 {
7341 symbol_name_temporary[i++] = l % 10 + '0';
7342 l /= 10;
7343 }
7344 while (l != 0);
7345 while (i > 0)
7346 *p++ = symbol_name_temporary[--i];
7347 *p = '\0';
7348
7349 return micromips_target_name;
7350}
7351
7352static void
7353micromips_label_expr (expressionS *label_expr)
7354{
7355 label_expr->X_op = O_symbol;
7356 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
7357 label_expr->X_add_number = 0;
7358}
7359
7360static void
7361micromips_label_inc (void)
7362{
7363 micromips_target_label++;
7364 *micromips_target_name = '\0';
7365}
7366
7367static void
7368micromips_add_label (void)
7369{
7370 symbolS *s;
7371
7372 s = colon (micromips_label_name ());
7373 micromips_label_inc ();
f3ded42a 7374 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
df58fc94
RS
7375}
7376
7377/* If assembling microMIPS code, then return the microMIPS reloc
7378 corresponding to the requested one if any. Otherwise return
7379 the reloc unchanged. */
7380
7381static bfd_reloc_code_real_type
7382micromips_map_reloc (bfd_reloc_code_real_type reloc)
7383{
7384 static const bfd_reloc_code_real_type relocs[][2] =
7385 {
7386 /* Keep sorted incrementally by the left-hand key. */
7387 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
7388 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
7389 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
7390 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
7391 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
7392 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
7393 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
7394 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
7395 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
7396 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
7397 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
7398 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
7399 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
7400 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
7401 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
7402 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
7403 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
7404 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
7405 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
7406 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
7407 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
7408 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
7409 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
7410 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
7411 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
7412 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
7413 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
7414 };
7415 bfd_reloc_code_real_type r;
7416 size_t i;
7417
7418 if (!mips_opts.micromips)
7419 return reloc;
7420 for (i = 0; i < ARRAY_SIZE (relocs); i++)
7421 {
7422 r = relocs[i][0];
7423 if (r > reloc)
7424 return reloc;
7425 if (r == reloc)
7426 return relocs[i][1];
7427 }
7428 return reloc;
7429}
7430
b886a2ab
RS
7431/* Try to resolve relocation RELOC against constant OPERAND at assembly time.
7432 Return true on success, storing the resolved value in RESULT. */
7433
7434static bfd_boolean
7435calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
7436 offsetT *result)
7437{
7438 switch (reloc)
7439 {
7440 case BFD_RELOC_MIPS_HIGHEST:
7441 case BFD_RELOC_MICROMIPS_HIGHEST:
7442 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
7443 return TRUE;
7444
7445 case BFD_RELOC_MIPS_HIGHER:
7446 case BFD_RELOC_MICROMIPS_HIGHER:
7447 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
7448 return TRUE;
7449
7450 case BFD_RELOC_HI16_S:
41947d9e 7451 case BFD_RELOC_HI16_S_PCREL:
b886a2ab
RS
7452 case BFD_RELOC_MICROMIPS_HI16_S:
7453 case BFD_RELOC_MIPS16_HI16_S:
7454 *result = ((operand + 0x8000) >> 16) & 0xffff;
7455 return TRUE;
7456
7457 case BFD_RELOC_HI16:
7458 case BFD_RELOC_MICROMIPS_HI16:
7459 case BFD_RELOC_MIPS16_HI16:
7460 *result = (operand >> 16) & 0xffff;
7461 return TRUE;
7462
7463 case BFD_RELOC_LO16:
41947d9e 7464 case BFD_RELOC_LO16_PCREL:
b886a2ab
RS
7465 case BFD_RELOC_MICROMIPS_LO16:
7466 case BFD_RELOC_MIPS16_LO16:
7467 *result = operand & 0xffff;
7468 return TRUE;
7469
7470 case BFD_RELOC_UNUSED:
7471 *result = operand;
7472 return TRUE;
7473
7474 default:
7475 return FALSE;
7476 }
7477}
7478
71400594
RS
7479/* Output an instruction. IP is the instruction information.
7480 ADDRESS_EXPR is an operand of the instruction to be used with
df58fc94
RS
7481 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
7482 a macro expansion. */
71400594
RS
7483
7484static void
7485append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
df58fc94 7486 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
71400594 7487{
14fe068b 7488 unsigned long prev_pinfo2, pinfo;
71400594 7489 bfd_boolean relaxed_branch = FALSE;
a4e06468 7490 enum append_method method;
2309ddf2 7491 bfd_boolean relax32;
2b0c8b40 7492 int branch_disp;
71400594 7493
2309ddf2 7494 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
c67a084a
NC
7495 fix_loongson2f (ip);
7496
6f2117ba
PH
7497 ip->target[0] = '\0';
7498 if (offset_expr.X_op == O_symbol)
7499 strncpy (ip->target, S_GET_NAME (offset_expr.X_add_symbol), 15);
7500 ip->label[0] = '\0';
7501 if (seg_info (now_seg)->label_list)
7502 strncpy (ip->label, S_GET_NAME (seg_info (now_seg)->label_list->label), 15);
7503 if (mips_fix_loongson3_llsc && !HAVE_CODE_COMPRESSION)
7504 fix_loongson3_llsc (ip);
7505
738f4d98 7506 file_ase_mips16 |= mips_opts.mips16;
df58fc94 7507 file_ase_micromips |= mips_opts.micromips;
738f4d98 7508
df58fc94 7509 prev_pinfo2 = history[0].insn_mo->pinfo2;
71400594 7510 pinfo = ip->insn_mo->pinfo;
df58fc94 7511
7bd374a4
MR
7512 /* Don't raise alarm about `nods' frags as they'll fill in the right
7513 kind of nop in relaxation if required. */
df58fc94
RS
7514 if (mips_opts.micromips
7515 && !expansionp
7bd374a4
MR
7516 && !(history[0].frag
7517 && history[0].frag->fr_type == rs_machine_dependent
7518 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
7519 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
df58fc94
RS
7520 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
7521 && micromips_insn_length (ip->insn_mo) != 2)
7522 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
7523 && micromips_insn_length (ip->insn_mo) != 4)))
1661c76c 7524 as_warn (_("wrong size instruction in a %u-bit branch delay slot"),
df58fc94 7525 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
71400594 7526
15be625d
CM
7527 if (address_expr == NULL)
7528 ip->complete_p = 1;
b886a2ab
RS
7529 else if (reloc_type[0] <= BFD_RELOC_UNUSED
7530 && reloc_type[1] == BFD_RELOC_UNUSED
7531 && reloc_type[2] == BFD_RELOC_UNUSED
15be625d
CM
7532 && address_expr->X_op == O_constant)
7533 {
15be625d
CM
7534 switch (*reloc_type)
7535 {
15be625d 7536 case BFD_RELOC_MIPS_JMP:
df58fc94
RS
7537 {
7538 int shift;
7539
17c6c9d9
MR
7540 /* Shift is 2, unusually, for microMIPS JALX. */
7541 shift = (mips_opts.micromips
7542 && strcmp (ip->insn_mo->name, "jalx") != 0) ? 1 : 2;
df58fc94
RS
7543 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7544 as_bad (_("jump to misaligned address (0x%lx)"),
7545 (unsigned long) address_expr->X_add_number);
7546 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7547 & 0x3ffffff);
335574df 7548 ip->complete_p = 1;
df58fc94 7549 }
15be625d
CM
7550 break;
7551
7552 case BFD_RELOC_MIPS16_JMP:
7553 if ((address_expr->X_add_number & 3) != 0)
7554 as_bad (_("jump to misaligned address (0x%lx)"),
7555 (unsigned long) address_expr->X_add_number);
7556 ip->insn_opcode |=
7557 (((address_expr->X_add_number & 0x7c0000) << 3)
7558 | ((address_expr->X_add_number & 0xf800000) >> 7)
7559 | ((address_expr->X_add_number & 0x3fffc) >> 2));
335574df 7560 ip->complete_p = 1;
15be625d
CM
7561 break;
7562
7563 case BFD_RELOC_16_PCREL_S2:
df58fc94
RS
7564 {
7565 int shift;
7566
7567 shift = mips_opts.micromips ? 1 : 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 (!mips_relax_branch)
7572 {
7573 if ((address_expr->X_add_number + (1 << (shift + 15)))
7574 & ~((1 << (shift + 16)) - 1))
7575 as_bad (_("branch address range overflow (0x%lx)"),
7576 (unsigned long) address_expr->X_add_number);
7577 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7578 & 0xffff);
7579 }
df58fc94 7580 }
15be625d
CM
7581 break;
7582
7361da2c
AB
7583 case BFD_RELOC_MIPS_21_PCREL_S2:
7584 {
7585 int shift;
7586
7587 shift = 2;
7588 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7589 as_bad (_("branch to misaligned address (0x%lx)"),
7590 (unsigned long) address_expr->X_add_number);
7591 if ((address_expr->X_add_number + (1 << (shift + 20)))
7592 & ~((1 << (shift + 21)) - 1))
7593 as_bad (_("branch address range overflow (0x%lx)"),
7594 (unsigned long) address_expr->X_add_number);
7595 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7596 & 0x1fffff);
7597 }
7598 break;
7599
7600 case BFD_RELOC_MIPS_26_PCREL_S2:
7601 {
7602 int shift;
7603
7604 shift = 2;
7605 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7606 as_bad (_("branch to misaligned address (0x%lx)"),
7607 (unsigned long) address_expr->X_add_number);
7608 if ((address_expr->X_add_number + (1 << (shift + 25)))
7609 & ~((1 << (shift + 26)) - 1))
7610 as_bad (_("branch address range overflow (0x%lx)"),
7611 (unsigned long) address_expr->X_add_number);
7612 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7613 & 0x3ffffff);
7614 }
7615 break;
7616
15be625d 7617 default:
b886a2ab
RS
7618 {
7619 offsetT value;
7620
7621 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
7622 &value))
7623 {
7624 ip->insn_opcode |= value & 0xffff;
7625 ip->complete_p = 1;
7626 }
7627 }
7628 break;
7629 }
15be625d
CM
7630 }
7631
71400594
RS
7632 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
7633 {
7634 /* There are a lot of optimizations we could do that we don't.
7635 In particular, we do not, in general, reorder instructions.
7636 If you use gcc with optimization, it will reorder
7637 instructions and generally do much more optimization then we
7638 do here; repeating all that work in the assembler would only
7639 benefit hand written assembly code, and does not seem worth
7640 it. */
7641 int nops = (mips_optimize == 0
932d1a1b
RS
7642 ? nops_for_insn (0, history, NULL)
7643 : nops_for_insn_or_target (0, history, ip));
71400594 7644 if (nops > 0)
252b5132
RH
7645 {
7646 fragS *old_frag;
7647 unsigned long old_frag_offset;
7648 int i;
252b5132
RH
7649
7650 old_frag = frag_now;
7651 old_frag_offset = frag_now_fix ();
7652
7653 for (i = 0; i < nops; i++)
14fe068b
RS
7654 add_fixed_insn (NOP_INSN);
7655 insert_into_history (0, nops, NOP_INSN);
252b5132
RH
7656
7657 if (listing)
7658 {
7659 listing_prev_line ();
7660 /* We may be at the start of a variant frag. In case we
7661 are, make sure there is enough space for the frag
7662 after the frags created by listing_prev_line. The
7663 argument to frag_grow here must be at least as large
7664 as the argument to all other calls to frag_grow in
7665 this file. We don't have to worry about being in the
7666 middle of a variant frag, because the variants insert
7667 all needed nop instructions themselves. */
7668 frag_grow (40);
7669 }
7670
462427c4 7671 mips_move_text_labels ();
252b5132
RH
7672
7673#ifndef NO_ECOFF_DEBUGGING
7674 if (ECOFF_DEBUGGING)
7675 ecoff_fix_loc (old_frag, old_frag_offset);
7676#endif
7677 }
71400594
RS
7678 }
7679 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
7680 {
932d1a1b
RS
7681 int nops;
7682
7683 /* Work out how many nops in prev_nop_frag are needed by IP,
7684 ignoring hazards generated by the first prev_nop_frag_since
7685 instructions. */
7686 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
9c2799c2 7687 gas_assert (nops <= prev_nop_frag_holds);
252b5132 7688
71400594
RS
7689 /* Enforce NOPS as a minimum. */
7690 if (nops > prev_nop_frag_required)
7691 prev_nop_frag_required = nops;
252b5132 7692
71400594
RS
7693 if (prev_nop_frag_holds == prev_nop_frag_required)
7694 {
7695 /* Settle for the current number of nops. Update the history
7696 accordingly (for the benefit of any future .set reorder code). */
7697 prev_nop_frag = NULL;
7698 insert_into_history (prev_nop_frag_since,
7699 prev_nop_frag_holds, NOP_INSN);
7700 }
7701 else
7702 {
7703 /* Allow this instruction to replace one of the nops that was
7704 tentatively added to prev_nop_frag. */
df58fc94 7705 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
71400594
RS
7706 prev_nop_frag_holds--;
7707 prev_nop_frag_since++;
252b5132
RH
7708 }
7709 }
7710
e407c74b 7711 method = get_append_method (ip, address_expr, reloc_type);
2b0c8b40 7712 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
a4e06468 7713
e410add4
RS
7714 dwarf2_emit_insn (0);
7715 /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
7716 so "move" the instruction address accordingly.
7717
7718 Also, it doesn't seem appropriate for the assembler to reorder .loc
7719 entries. If this instruction is a branch that we are going to swap
7720 with the previous instruction, the two instructions should be
7721 treated as a unit, and the debug information for both instructions
7722 should refer to the start of the branch sequence. Using the
7723 current position is certainly wrong when swapping a 32-bit branch
7724 and a 16-bit delay slot, since the current position would then be
7725 in the middle of a branch. */
7726 dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
58e2ea4d 7727
df58fc94
RS
7728 relax32 = (mips_relax_branch
7729 /* Don't try branch relaxation within .set nomacro, or within
7730 .set noat if we use $at for PIC computations. If it turns
7731 out that the branch was out-of-range, we'll get an error. */
7732 && !mips_opts.warn_about_macros
7733 && (mips_opts.at || mips_pic == NO_PIC)
3bf0dbfb
MR
7734 /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
7735 as they have no complementing branches. */
7736 && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
df58fc94
RS
7737
7738 if (!HAVE_CODE_COMPRESSION
7739 && address_expr
7740 && relax32
0b25d3e6 7741 && *reloc_type == BFD_RELOC_16_PCREL_S2
11625dd8 7742 && delayed_branch_p (ip))
4a6a3df4 7743 {
895921c9 7744 relaxed_branch = TRUE;
1e915849
RS
7745 add_relaxed_insn (ip, (relaxed_branch_length
7746 (NULL, NULL,
11625dd8
RS
7747 uncond_branch_p (ip) ? -1
7748 : branch_likely_p (ip) ? 1
1e915849
RS
7749 : 0)), 4,
7750 RELAX_BRANCH_ENCODE
ce8ad872 7751 (AT, mips_pic != NO_PIC,
11625dd8
RS
7752 uncond_branch_p (ip),
7753 branch_likely_p (ip),
1e915849
RS
7754 pinfo & INSN_WRITE_GPR_31,
7755 0),
7756 address_expr->X_add_symbol,
7757 address_expr->X_add_number);
4a6a3df4
AO
7758 *reloc_type = BFD_RELOC_UNUSED;
7759 }
df58fc94
RS
7760 else if (mips_opts.micromips
7761 && address_expr
7762 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
7763 || *reloc_type > BFD_RELOC_UNUSED)
40209cad
MR
7764 && (delayed_branch_p (ip) || compact_branch_p (ip))
7765 /* Don't try branch relaxation when users specify
7766 16-bit/32-bit instructions. */
7767 && !forced_insn_length)
df58fc94 7768 {
7bd374a4
MR
7769 bfd_boolean relax16 = (method != APPEND_ADD_COMPACT
7770 && *reloc_type > BFD_RELOC_UNUSED);
df58fc94 7771 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
11625dd8 7772 int uncond = uncond_branch_p (ip) ? -1 : 0;
7bd374a4
MR
7773 int compact = compact_branch_p (ip) || method == APPEND_ADD_COMPACT;
7774 int nods = method == APPEND_ADD_WITH_NOP;
df58fc94 7775 int al = pinfo & INSN_WRITE_GPR_31;
7bd374a4 7776 int length32 = nods ? 8 : 4;
df58fc94
RS
7777
7778 gas_assert (address_expr != NULL);
7779 gas_assert (!mips_relax.sequence);
7780
2b0c8b40 7781 relaxed_branch = TRUE;
7bd374a4
MR
7782 if (nods)
7783 method = APPEND_ADD;
7784 if (relax32)
7785 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
7786 add_relaxed_insn (ip, length32, relax16 ? 2 : 4,
8484fb75 7787 RELAX_MICROMIPS_ENCODE (type, AT, mips_opts.insn32,
ce8ad872 7788 mips_pic != NO_PIC,
7bd374a4 7789 uncond, compact, al, nods,
40209cad 7790 relax32, 0, 0),
df58fc94
RS
7791 address_expr->X_add_symbol,
7792 address_expr->X_add_number);
7793 *reloc_type = BFD_RELOC_UNUSED;
7794 }
7795 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
252b5132 7796 {
7fd53920
MR
7797 bfd_boolean require_unextended;
7798 bfd_boolean require_extended;
88a7ef16
MR
7799 symbolS *symbol;
7800 offsetT offset;
7801
7fd53920
MR
7802 if (forced_insn_length != 0)
7803 {
7804 require_unextended = forced_insn_length == 2;
7805 require_extended = forced_insn_length == 4;
7806 }
7807 else
7808 {
7809 require_unextended = (mips_opts.noautoextend
7810 && !mips_opcode_32bit_p (ip->insn_mo));
7811 require_extended = 0;
7812 }
7813
252b5132 7814 /* We need to set up a variant frag. */
df58fc94 7815 gas_assert (address_expr != NULL);
88a7ef16
MR
7816 /* Pass any `O_symbol' expression unchanged as an `expr_section'
7817 symbol created by `make_expr_symbol' may not get a necessary
7818 external relocation produced. */
7819 if (address_expr->X_op == O_symbol)
7820 {
7821 symbol = address_expr->X_add_symbol;
7822 offset = address_expr->X_add_number;
7823 }
7824 else
7825 {
7826 symbol = make_expr_symbol (address_expr);
82d808ed 7827 symbol_append (symbol, symbol_lastP, &symbol_rootP, &symbol_lastP);
88a7ef16
MR
7828 offset = 0;
7829 }
8507b6e7 7830 add_relaxed_insn (ip, 12, 0,
1e915849
RS
7831 RELAX_MIPS16_ENCODE
7832 (*reloc_type - BFD_RELOC_UNUSED,
25499ac7 7833 mips_opts.ase & ASE_MIPS16E2,
8507b6e7
MR
7834 mips_pic != NO_PIC,
7835 HAVE_32BIT_SYMBOLS,
7836 mips_opts.warn_about_macros,
7fd53920 7837 require_unextended, require_extended,
11625dd8 7838 delayed_branch_p (&history[0]),
1e915849 7839 history[0].mips16_absolute_jump_p),
88a7ef16 7840 symbol, offset);
252b5132 7841 }
5c04167a 7842 else if (mips_opts.mips16 && insn_length (ip) == 2)
9497f5ac 7843 {
11625dd8 7844 if (!delayed_branch_p (ip))
b8ee1a6e
DU
7845 /* Make sure there is enough room to swap this instruction with
7846 a following jump instruction. */
7847 frag_grow (6);
1e915849 7848 add_fixed_insn (ip);
252b5132
RH
7849 }
7850 else
7851 {
7852 if (mips_opts.mips16
7853 && mips_opts.noreorder
11625dd8 7854 && delayed_branch_p (&history[0]))
252b5132
RH
7855 as_warn (_("extended instruction in delay slot"));
7856
4d7206a2
RS
7857 if (mips_relax.sequence)
7858 {
7859 /* If we've reached the end of this frag, turn it into a variant
7860 frag and record the information for the instructions we've
7861 written so far. */
7862 if (frag_room () < 4)
7863 relax_close_frag ();
df58fc94 7864 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4d7206a2
RS
7865 }
7866
584892a6 7867 if (mips_relax.sequence != 2)
df58fc94
RS
7868 {
7869 if (mips_macro_warning.first_insn_sizes[0] == 0)
7870 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
7871 mips_macro_warning.sizes[0] += insn_length (ip);
7872 mips_macro_warning.insns[0]++;
7873 }
584892a6 7874 if (mips_relax.sequence != 1)
df58fc94
RS
7875 {
7876 if (mips_macro_warning.first_insn_sizes[1] == 0)
7877 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
7878 mips_macro_warning.sizes[1] += insn_length (ip);
7879 mips_macro_warning.insns[1]++;
7880 }
584892a6 7881
1e915849
RS
7882 if (mips_opts.mips16)
7883 {
7884 ip->fixed_p = 1;
7885 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
7886 }
7887 add_fixed_insn (ip);
252b5132
RH
7888 }
7889
9fe77896 7890 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
252b5132 7891 {
df58fc94 7892 bfd_reloc_code_real_type final_type[3];
2309ddf2 7893 reloc_howto_type *howto0;
9fe77896
RS
7894 reloc_howto_type *howto;
7895 int i;
34ce925e 7896
df58fc94
RS
7897 /* Perform any necessary conversion to microMIPS relocations
7898 and find out how many relocations there actually are. */
7899 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
7900 final_type[i] = micromips_map_reloc (reloc_type[i]);
7901
9fe77896
RS
7902 /* In a compound relocation, it is the final (outermost)
7903 operator that determines the relocated field. */
2309ddf2 7904 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
e8044f35
RS
7905 if (!howto)
7906 abort ();
2309ddf2
MR
7907
7908 if (i > 1)
7909 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
9fe77896
RS
7910 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
7911 bfd_get_reloc_size (howto),
7912 address_expr,
2309ddf2
MR
7913 howto0 && howto0->pc_relative,
7914 final_type[0]);
ce8ad872
MR
7915 /* Record non-PIC mode in `fx_tcbit2' for `md_apply_fix'. */
7916 ip->fixp[0]->fx_tcbit2 = mips_pic == NO_PIC;
9fe77896
RS
7917
7918 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
2309ddf2 7919 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
9fe77896
RS
7920 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
7921
7922 /* These relocations can have an addend that won't fit in
7923 4 octets for 64bit assembly. */
bad1aba3 7924 if (GPR_SIZE == 64
9fe77896
RS
7925 && ! howto->partial_inplace
7926 && (reloc_type[0] == BFD_RELOC_16
7927 || reloc_type[0] == BFD_RELOC_32
7928 || reloc_type[0] == BFD_RELOC_MIPS_JMP
7929 || reloc_type[0] == BFD_RELOC_GPREL16
7930 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
7931 || reloc_type[0] == BFD_RELOC_GPREL32
7932 || reloc_type[0] == BFD_RELOC_64
7933 || reloc_type[0] == BFD_RELOC_CTOR
7934 || reloc_type[0] == BFD_RELOC_MIPS_SUB
7935 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
7936 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
7937 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
7938 || reloc_type[0] == BFD_RELOC_MIPS_REL16
7939 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
7940 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
7941 || hi16_reloc_p (reloc_type[0])
7942 || lo16_reloc_p (reloc_type[0])))
7943 ip->fixp[0]->fx_no_overflow = 1;
7944
ddaf2c41
MR
7945 /* These relocations can have an addend that won't fit in 2 octets. */
7946 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
7947 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
7948 ip->fixp[0]->fx_no_overflow = 1;
7949
9fe77896
RS
7950 if (mips_relax.sequence)
7951 {
7952 if (mips_relax.first_fixup == 0)
7953 mips_relax.first_fixup = ip->fixp[0];
7954 }
7955 else if (reloc_needs_lo_p (*reloc_type))
7956 {
7957 struct mips_hi_fixup *hi_fixup;
7958
7959 /* Reuse the last entry if it already has a matching %lo. */
7960 hi_fixup = mips_hi_fixup_list;
7961 if (hi_fixup == 0
7962 || !fixup_has_matching_lo_p (hi_fixup->fixp))
4d7206a2 7963 {
325801bd 7964 hi_fixup = XNEW (struct mips_hi_fixup);
9fe77896
RS
7965 hi_fixup->next = mips_hi_fixup_list;
7966 mips_hi_fixup_list = hi_fixup;
4d7206a2 7967 }
9fe77896
RS
7968 hi_fixup->fixp = ip->fixp[0];
7969 hi_fixup->seg = now_seg;
7970 }
252b5132 7971
9fe77896
RS
7972 /* Add fixups for the second and third relocations, if given.
7973 Note that the ABI allows the second relocation to be
7974 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
7975 moment we only use RSS_UNDEF, but we could add support
7976 for the others if it ever becomes necessary. */
7977 for (i = 1; i < 3; i++)
7978 if (reloc_type[i] != BFD_RELOC_UNUSED)
7979 {
7980 ip->fixp[i] = fix_new (ip->frag, ip->where,
7981 ip->fixp[0]->fx_size, NULL, 0,
df58fc94 7982 FALSE, final_type[i]);
f6688943 7983
9fe77896
RS
7984 /* Use fx_tcbit to mark compound relocs. */
7985 ip->fixp[0]->fx_tcbit = 1;
7986 ip->fixp[i]->fx_tcbit = 1;
7987 }
252b5132 7988 }
252b5132
RH
7989
7990 /* Update the register mask information. */
4c260379
RS
7991 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
7992 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
252b5132 7993
a4e06468 7994 switch (method)
252b5132 7995 {
a4e06468
RS
7996 case APPEND_ADD:
7997 insert_into_history (0, 1, ip);
7998 break;
7999
8000 case APPEND_ADD_WITH_NOP:
14fe068b
RS
8001 {
8002 struct mips_cl_insn *nop;
8003
8004 insert_into_history (0, 1, ip);
8005 nop = get_delay_slot_nop (ip);
8006 add_fixed_insn (nop);
8007 insert_into_history (0, 1, nop);
8008 if (mips_relax.sequence)
8009 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
8010 }
a4e06468
RS
8011 break;
8012
8013 case APPEND_ADD_COMPACT:
8014 /* Convert MIPS16 jr/jalr into a "compact" jump. */
7bd374a4
MR
8015 if (mips_opts.mips16)
8016 {
8017 ip->insn_opcode |= 0x0080;
8018 find_altered_mips16_opcode (ip);
8019 }
8020 /* Convert microMIPS instructions. */
8021 else if (mips_opts.micromips)
8022 {
8023 /* jr16->jrc */
8024 if ((ip->insn_opcode & 0xffe0) == 0x4580)
8025 ip->insn_opcode |= 0x0020;
8026 /* b16->bc */
8027 else if ((ip->insn_opcode & 0xfc00) == 0xcc00)
8028 ip->insn_opcode = 0x40e00000;
8029 /* beqz16->beqzc, bnez16->bnezc */
8030 else if ((ip->insn_opcode & 0xdc00) == 0x8c00)
8031 {
8032 unsigned long regno;
8033
8034 regno = ip->insn_opcode >> MICROMIPSOP_SH_MD;
8035 regno &= MICROMIPSOP_MASK_MD;
8036 regno = micromips_to_32_reg_d_map[regno];
8037 ip->insn_opcode = (((ip->insn_opcode << 9) & 0x00400000)
8038 | (regno << MICROMIPSOP_SH_RS)
8039 | 0x40a00000) ^ 0x00400000;
8040 }
8041 /* beqz->beqzc, bnez->bnezc */
8042 else if ((ip->insn_opcode & 0xdfe00000) == 0x94000000)
8043 ip->insn_opcode = ((ip->insn_opcode & 0x001f0000)
8044 | ((ip->insn_opcode >> 7) & 0x00400000)
8045 | 0x40a00000) ^ 0x00400000;
8046 /* beq $0->beqzc, bne $0->bnezc */
8047 else if ((ip->insn_opcode & 0xdc1f0000) == 0x94000000)
8048 ip->insn_opcode = (((ip->insn_opcode >>
8049 (MICROMIPSOP_SH_RT - MICROMIPSOP_SH_RS))
8050 & (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS))
8051 | ((ip->insn_opcode >> 7) & 0x00400000)
8052 | 0x40a00000) ^ 0x00400000;
8053 else
8054 abort ();
8055 find_altered_micromips_opcode (ip);
8056 }
8057 else
8058 abort ();
a4e06468
RS
8059 install_insn (ip);
8060 insert_into_history (0, 1, ip);
8061 break;
8062
8063 case APPEND_SWAP:
8064 {
8065 struct mips_cl_insn delay = history[0];
99e7978b
MF
8066
8067 if (relaxed_branch || delay.frag != ip->frag)
a4e06468
RS
8068 {
8069 /* Add the delay slot instruction to the end of the
8070 current frag and shrink the fixed part of the
8071 original frag. If the branch occupies the tail of
8072 the latter, move it backwards to cover the gap. */
2b0c8b40 8073 delay.frag->fr_fix -= branch_disp;
a4e06468 8074 if (delay.frag == ip->frag)
2b0c8b40 8075 move_insn (ip, ip->frag, ip->where - branch_disp);
a4e06468
RS
8076 add_fixed_insn (&delay);
8077 }
8078 else
8079 {
5e35670b
MR
8080 /* If this is not a relaxed branch and we are in the
8081 same frag, then just swap the instructions. */
8082 move_insn (ip, delay.frag, delay.where);
8083 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
a4e06468
RS
8084 }
8085 history[0] = *ip;
8086 delay.fixed_p = 1;
8087 insert_into_history (0, 1, &delay);
8088 }
8089 break;
252b5132
RH
8090 }
8091
13408f1e 8092 /* If we have just completed an unconditional branch, clear the history. */
11625dd8
RS
8093 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
8094 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
e407c74b
NC
8095 {
8096 unsigned int i;
8097
79850f26 8098 mips_no_prev_insn ();
13408f1e 8099
e407c74b 8100 for (i = 0; i < ARRAY_SIZE (history); i++)
79850f26 8101 history[i].cleared_p = 1;
e407c74b
NC
8102 }
8103
df58fc94
RS
8104 /* We need to emit a label at the end of branch-likely macros. */
8105 if (emit_branch_likely_macro)
8106 {
8107 emit_branch_likely_macro = FALSE;
8108 micromips_add_label ();
8109 }
8110
252b5132
RH
8111 /* We just output an insn, so the next one doesn't have a label. */
8112 mips_clear_insn_labels ();
252b5132
RH
8113}
8114
e407c74b
NC
8115/* Forget that there was any previous instruction or label.
8116 When BRANCH is true, the branch history is also flushed. */
252b5132
RH
8117
8118static void
7d10b47d 8119mips_no_prev_insn (void)
252b5132 8120{
7d10b47d
RS
8121 prev_nop_frag = NULL;
8122 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
8123 mips_clear_insn_labels ();
8124}
8125
7d10b47d
RS
8126/* This function must be called before we emit something other than
8127 instructions. It is like mips_no_prev_insn except that it inserts
8128 any NOPS that might be needed by previous instructions. */
252b5132 8129
7d10b47d
RS
8130void
8131mips_emit_delays (void)
252b5132
RH
8132{
8133 if (! mips_opts.noreorder)
8134 {
932d1a1b 8135 int nops = nops_for_insn (0, history, NULL);
252b5132
RH
8136 if (nops > 0)
8137 {
7d10b47d
RS
8138 while (nops-- > 0)
8139 add_fixed_insn (NOP_INSN);
462427c4 8140 mips_move_text_labels ();
7d10b47d
RS
8141 }
8142 }
8143 mips_no_prev_insn ();
8144}
8145
8146/* Start a (possibly nested) noreorder block. */
8147
8148static void
8149start_noreorder (void)
8150{
8151 if (mips_opts.noreorder == 0)
8152 {
8153 unsigned int i;
8154 int nops;
8155
8156 /* None of the instructions before the .set noreorder can be moved. */
8157 for (i = 0; i < ARRAY_SIZE (history); i++)
8158 history[i].fixed_p = 1;
8159
8160 /* Insert any nops that might be needed between the .set noreorder
8161 block and the previous instructions. We will later remove any
8162 nops that turn out not to be needed. */
932d1a1b 8163 nops = nops_for_insn (0, history, NULL);
7d10b47d
RS
8164 if (nops > 0)
8165 {
8166 if (mips_optimize != 0)
252b5132
RH
8167 {
8168 /* Record the frag which holds the nop instructions, so
8169 that we can remove them if we don't need them. */
df58fc94 8170 frag_grow (nops * NOP_INSN_SIZE);
252b5132
RH
8171 prev_nop_frag = frag_now;
8172 prev_nop_frag_holds = nops;
8173 prev_nop_frag_required = 0;
8174 prev_nop_frag_since = 0;
8175 }
8176
8177 for (; nops > 0; --nops)
1e915849 8178 add_fixed_insn (NOP_INSN);
252b5132 8179
7d10b47d
RS
8180 /* Move on to a new frag, so that it is safe to simply
8181 decrease the size of prev_nop_frag. */
8182 frag_wane (frag_now);
8183 frag_new (0);
462427c4 8184 mips_move_text_labels ();
252b5132 8185 }
df58fc94 8186 mips_mark_labels ();
7d10b47d 8187 mips_clear_insn_labels ();
252b5132 8188 }
7d10b47d
RS
8189 mips_opts.noreorder++;
8190 mips_any_noreorder = 1;
8191}
252b5132 8192
7d10b47d 8193/* End a nested noreorder block. */
252b5132 8194
7d10b47d
RS
8195static void
8196end_noreorder (void)
8197{
8198 mips_opts.noreorder--;
8199 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
8200 {
8201 /* Commit to inserting prev_nop_frag_required nops and go back to
8202 handling nop insertion the .set reorder way. */
8203 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
df58fc94 8204 * NOP_INSN_SIZE);
7d10b47d
RS
8205 insert_into_history (prev_nop_frag_since,
8206 prev_nop_frag_required, NOP_INSN);
8207 prev_nop_frag = NULL;
8208 }
252b5132
RH
8209}
8210
97d87491
RS
8211/* Sign-extend 32-bit mode constants that have bit 31 set and all
8212 higher bits unset. */
8213
8214static void
8215normalize_constant_expr (expressionS *ex)
8216{
8217 if (ex->X_op == O_constant
8218 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
8219 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
8220 - 0x80000000);
8221}
8222
8223/* Sign-extend 32-bit mode address offsets that have bit 31 set and
8224 all higher bits unset. */
8225
8226static void
8227normalize_address_expr (expressionS *ex)
8228{
8229 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
8230 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
8231 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
8232 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
8233 - 0x80000000);
8234}
8235
8236/* Try to match TOKENS against OPCODE, storing the result in INSN.
8237 Return true if the match was successful.
8238
8239 OPCODE_EXTRA is a value that should be ORed into the opcode
8240 (used for VU0 channel suffixes, etc.). MORE_ALTS is true if
8241 there are more alternatives after OPCODE and SOFT_MATCH is
8242 as for mips_arg_info. */
8243
8244static bfd_boolean
8245match_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
8246 struct mips_operand_token *tokens, unsigned int opcode_extra,
60f20e8b 8247 bfd_boolean lax_match, bfd_boolean complete_p)
97d87491
RS
8248{
8249 const char *args;
8250 struct mips_arg_info arg;
8251 const struct mips_operand *operand;
8252 char c;
8253
8254 imm_expr.X_op = O_absent;
97d87491
RS
8255 offset_expr.X_op = O_absent;
8256 offset_reloc[0] = BFD_RELOC_UNUSED;
8257 offset_reloc[1] = BFD_RELOC_UNUSED;
8258 offset_reloc[2] = BFD_RELOC_UNUSED;
8259
8260 create_insn (insn, opcode);
60f20e8b
RS
8261 /* When no opcode suffix is specified, assume ".xyzw". */
8262 if ((opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0 && opcode_extra == 0)
8263 insn->insn_opcode |= 0xf << mips_vu0_channel_mask.lsb;
8264 else
8265 insn->insn_opcode |= opcode_extra;
97d87491
RS
8266 memset (&arg, 0, sizeof (arg));
8267 arg.insn = insn;
8268 arg.token = tokens;
8269 arg.argnum = 1;
8270 arg.last_regno = ILLEGAL_REG;
8271 arg.dest_regno = ILLEGAL_REG;
60f20e8b 8272 arg.lax_match = lax_match;
97d87491
RS
8273 for (args = opcode->args;; ++args)
8274 {
8275 if (arg.token->type == OT_END)
8276 {
8277 /* Handle unary instructions in which only one operand is given.
8278 The source is then the same as the destination. */
8279 if (arg.opnum == 1 && *args == ',')
8280 {
8281 operand = (mips_opts.micromips
8282 ? decode_micromips_operand (args + 1)
8283 : decode_mips_operand (args + 1));
8284 if (operand && mips_optional_operand_p (operand))
8285 {
8286 arg.token = tokens;
8287 arg.argnum = 1;
8288 continue;
8289 }
8290 }
8291
8292 /* Treat elided base registers as $0. */
8293 if (strcmp (args, "(b)") == 0)
8294 args += 3;
8295
8296 if (args[0] == '+')
8297 switch (args[1])
8298 {
8299 case 'K':
8300 case 'N':
8301 /* The register suffix is optional. */
8302 args += 2;
8303 break;
8304 }
8305
8306 /* Fail the match if there were too few operands. */
8307 if (*args)
8308 return FALSE;
8309
8310 /* Successful match. */
60f20e8b
RS
8311 if (!complete_p)
8312 return TRUE;
e3de51ce 8313 clear_insn_error ();
97d87491
RS
8314 if (arg.dest_regno == arg.last_regno
8315 && strncmp (insn->insn_mo->name, "jalr", 4) == 0)
8316 {
8317 if (arg.opnum == 2)
e3de51ce 8318 set_insn_error
1661c76c 8319 (0, _("source and destination must be different"));
97d87491 8320 else if (arg.last_regno == 31)
e3de51ce 8321 set_insn_error
1661c76c 8322 (0, _("a destination register must be supplied"));
97d87491 8323 }
173d3447
CF
8324 else if (arg.last_regno == 31
8325 && (strncmp (insn->insn_mo->name, "bltzal", 6) == 0
8326 || strncmp (insn->insn_mo->name, "bgezal", 6) == 0))
8327 set_insn_error (0, _("the source register must not be $31"));
97d87491
RS
8328 check_completed_insn (&arg);
8329 return TRUE;
8330 }
8331
8332 /* Fail the match if the line has too many operands. */
8333 if (*args == 0)
8334 return FALSE;
8335
8336 /* Handle characters that need to match exactly. */
8337 if (*args == '(' || *args == ')' || *args == ',')
8338 {
8339 if (match_char (&arg, *args))
8340 continue;
8341 return FALSE;
8342 }
8343 if (*args == '#')
8344 {
8345 ++args;
8346 if (arg.token->type == OT_DOUBLE_CHAR
8347 && arg.token->u.ch == *args)
8348 {
8349 ++arg.token;
8350 continue;
8351 }
8352 return FALSE;
8353 }
8354
8355 /* Handle special macro operands. Work out the properties of
8356 other operands. */
8357 arg.opnum += 1;
97d87491
RS
8358 switch (*args)
8359 {
7361da2c
AB
8360 case '-':
8361 switch (args[1])
8362 {
8363 case 'A':
8364 *offset_reloc = BFD_RELOC_MIPS_19_PCREL_S2;
8365 break;
8366
8367 case 'B':
8368 *offset_reloc = BFD_RELOC_MIPS_18_PCREL_S3;
8369 break;
8370 }
8371 break;
8372
97d87491
RS
8373 case '+':
8374 switch (args[1])
8375 {
97d87491
RS
8376 case 'i':
8377 *offset_reloc = BFD_RELOC_MIPS_JMP;
8378 break;
7361da2c
AB
8379
8380 case '\'':
8381 *offset_reloc = BFD_RELOC_MIPS_26_PCREL_S2;
8382 break;
8383
8384 case '\"':
8385 *offset_reloc = BFD_RELOC_MIPS_21_PCREL_S2;
8386 break;
97d87491
RS
8387 }
8388 break;
8389
97d87491 8390 case 'I':
1a00e612
RS
8391 if (!match_const_int (&arg, &imm_expr.X_add_number))
8392 return FALSE;
8393 imm_expr.X_op = O_constant;
bad1aba3 8394 if (GPR_SIZE == 32)
97d87491
RS
8395 normalize_constant_expr (&imm_expr);
8396 continue;
8397
8398 case 'A':
8399 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8400 {
8401 /* Assume that the offset has been elided and that what
8402 we saw was a base register. The match will fail later
8403 if that assumption turns out to be wrong. */
8404 offset_expr.X_op = O_constant;
8405 offset_expr.X_add_number = 0;
8406 }
97d87491 8407 else
1a00e612
RS
8408 {
8409 if (!match_expression (&arg, &offset_expr, offset_reloc))
8410 return FALSE;
8411 normalize_address_expr (&offset_expr);
8412 }
97d87491
RS
8413 continue;
8414
8415 case 'F':
8416 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8417 8, TRUE))
1a00e612 8418 return FALSE;
97d87491
RS
8419 continue;
8420
8421 case 'L':
8422 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8423 8, FALSE))
1a00e612 8424 return FALSE;
97d87491
RS
8425 continue;
8426
8427 case 'f':
8428 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8429 4, TRUE))
1a00e612 8430 return FALSE;
97d87491
RS
8431 continue;
8432
8433 case 'l':
8434 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8435 4, FALSE))
1a00e612 8436 return FALSE;
97d87491
RS
8437 continue;
8438
97d87491
RS
8439 case 'p':
8440 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8441 break;
8442
8443 case 'a':
8444 *offset_reloc = BFD_RELOC_MIPS_JMP;
8445 break;
8446
8447 case 'm':
8448 gas_assert (mips_opts.micromips);
8449 c = args[1];
8450 switch (c)
8451 {
8452 case 'D':
8453 case 'E':
8454 if (!forced_insn_length)
8455 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
8456 else if (c == 'D')
8457 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
8458 else
8459 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
8460 break;
8461 }
8462 break;
8463 }
8464
8465 operand = (mips_opts.micromips
8466 ? decode_micromips_operand (args)
8467 : decode_mips_operand (args));
8468 if (!operand)
8469 abort ();
8470
8471 /* Skip prefixes. */
7361da2c 8472 if (*args == '+' || *args == 'm' || *args == '-')
97d87491
RS
8473 args++;
8474
8475 if (mips_optional_operand_p (operand)
8476 && args[1] == ','
8477 && (arg.token[0].type != OT_REG
8478 || arg.token[1].type == OT_END))
8479 {
8480 /* Assume that the register has been elided and is the
8481 same as the first operand. */
8482 arg.token = tokens;
8483 arg.argnum = 1;
8484 }
8485
8486 if (!match_operand (&arg, operand))
8487 return FALSE;
8488 }
8489}
8490
8491/* Like match_insn, but for MIPS16. */
8492
8493static bfd_boolean
8494match_mips16_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
1a00e612 8495 struct mips_operand_token *tokens)
97d87491
RS
8496{
8497 const char *args;
8498 const struct mips_operand *operand;
8499 const struct mips_operand *ext_operand;
82d808ed 8500 bfd_boolean pcrel = FALSE;
7fd53920 8501 int required_insn_length;
97d87491
RS
8502 struct mips_arg_info arg;
8503 int relax_char;
8504
7fd53920
MR
8505 if (forced_insn_length)
8506 required_insn_length = forced_insn_length;
8507 else if (mips_opts.noautoextend && !mips_opcode_32bit_p (opcode))
8508 required_insn_length = 2;
8509 else
8510 required_insn_length = 0;
8511
97d87491
RS
8512 create_insn (insn, opcode);
8513 imm_expr.X_op = O_absent;
97d87491
RS
8514 offset_expr.X_op = O_absent;
8515 offset_reloc[0] = BFD_RELOC_UNUSED;
8516 offset_reloc[1] = BFD_RELOC_UNUSED;
8517 offset_reloc[2] = BFD_RELOC_UNUSED;
8518 relax_char = 0;
8519
8520 memset (&arg, 0, sizeof (arg));
8521 arg.insn = insn;
8522 arg.token = tokens;
8523 arg.argnum = 1;
8524 arg.last_regno = ILLEGAL_REG;
8525 arg.dest_regno = ILLEGAL_REG;
97d87491
RS
8526 relax_char = 0;
8527 for (args = opcode->args;; ++args)
8528 {
8529 int c;
8530
8531 if (arg.token->type == OT_END)
8532 {
8533 offsetT value;
8534
8535 /* Handle unary instructions in which only one operand is given.
8536 The source is then the same as the destination. */
8537 if (arg.opnum == 1 && *args == ',')
8538 {
8539 operand = decode_mips16_operand (args[1], FALSE);
8540 if (operand && mips_optional_operand_p (operand))
8541 {
8542 arg.token = tokens;
8543 arg.argnum = 1;
8544 continue;
8545 }
8546 }
8547
8548 /* Fail the match if there were too few operands. */
8549 if (*args)
8550 return FALSE;
8551
8552 /* Successful match. Stuff the immediate value in now, if
8553 we can. */
e3de51ce 8554 clear_insn_error ();
97d87491
RS
8555 if (opcode->pinfo == INSN_MACRO)
8556 {
8557 gas_assert (relax_char == 0 || relax_char == 'p');
8558 gas_assert (*offset_reloc == BFD_RELOC_UNUSED);
8559 }
8560 else if (relax_char
8561 && offset_expr.X_op == O_constant
82d808ed 8562 && !pcrel
97d87491
RS
8563 && calculate_reloc (*offset_reloc,
8564 offset_expr.X_add_number,
8565 &value))
8566 {
8567 mips16_immed (NULL, 0, relax_char, *offset_reloc, value,
7fd53920 8568 required_insn_length, &insn->insn_opcode);
97d87491
RS
8569 offset_expr.X_op = O_absent;
8570 *offset_reloc = BFD_RELOC_UNUSED;
8571 }
8572 else if (relax_char && *offset_reloc != BFD_RELOC_UNUSED)
8573 {
7fd53920 8574 if (required_insn_length == 2)
e3de51ce 8575 set_insn_error (0, _("invalid unextended operand value"));
25499ac7 8576 else if (!mips_opcode_32bit_p (opcode))
1da43acc
MR
8577 {
8578 forced_insn_length = 4;
8579 insn->insn_opcode |= MIPS16_EXTEND;
8580 }
97d87491
RS
8581 }
8582 else if (relax_char)
8583 *offset_reloc = (int) BFD_RELOC_UNUSED + relax_char;
8584
8585 check_completed_insn (&arg);
8586 return TRUE;
8587 }
8588
8589 /* Fail the match if the line has too many operands. */
8590 if (*args == 0)
8591 return FALSE;
8592
8593 /* Handle characters that need to match exactly. */
8594 if (*args == '(' || *args == ')' || *args == ',')
8595 {
8596 if (match_char (&arg, *args))
8597 continue;
8598 return FALSE;
8599 }
8600
8601 arg.opnum += 1;
8602 c = *args;
8603 switch (c)
8604 {
8605 case 'p':
8606 case 'q':
8607 case 'A':
8608 case 'B':
8609 case 'E':
25499ac7
MR
8610 case 'V':
8611 case 'u':
97d87491
RS
8612 relax_char = c;
8613 break;
8614
8615 case 'I':
1a00e612
RS
8616 if (!match_const_int (&arg, &imm_expr.X_add_number))
8617 return FALSE;
8618 imm_expr.X_op = O_constant;
bad1aba3 8619 if (GPR_SIZE == 32)
97d87491
RS
8620 normalize_constant_expr (&imm_expr);
8621 continue;
8622
8623 case 'a':
8624 case 'i':
8625 *offset_reloc = BFD_RELOC_MIPS16_JMP;
97d87491
RS
8626 break;
8627 }
8628
7fd53920 8629 operand = decode_mips16_operand (c, mips_opcode_32bit_p (opcode));
97d87491
RS
8630 if (!operand)
8631 abort ();
8632
82d808ed
MR
8633 if (operand->type == OP_PCREL)
8634 pcrel = TRUE;
8635 else
97d87491
RS
8636 {
8637 ext_operand = decode_mips16_operand (c, TRUE);
8638 if (operand != ext_operand)
8639 {
8640 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8641 {
8642 offset_expr.X_op = O_constant;
8643 offset_expr.X_add_number = 0;
8644 relax_char = c;
8645 continue;
8646 }
8647
1a7bf198 8648 if (!match_expression (&arg, &offset_expr, offset_reloc))
97d87491
RS
8649 return FALSE;
8650
8651 /* '8' is used for SLTI(U) and has traditionally not
8652 been allowed to take relocation operators. */
8653 if (offset_reloc[0] != BFD_RELOC_UNUSED
8654 && (ext_operand->size != 16 || c == '8'))
e295202f
MR
8655 {
8656 match_not_constant (&arg);
8657 return FALSE;
8658 }
97d87491 8659
c96425c5
MR
8660 if (offset_expr.X_op == O_big)
8661 {
8662 match_out_of_range (&arg);
8663 return FALSE;
8664 }
8665
97d87491
RS
8666 relax_char = c;
8667 continue;
8668 }
8669 }
8670
8671 if (mips_optional_operand_p (operand)
8672 && args[1] == ','
8673 && (arg.token[0].type != OT_REG
8674 || arg.token[1].type == OT_END))
8675 {
8676 /* Assume that the register has been elided and is the
8677 same as the first operand. */
8678 arg.token = tokens;
8679 arg.argnum = 1;
8680 }
8681
8682 if (!match_operand (&arg, operand))
8683 return FALSE;
8684 }
8685}
8686
60f20e8b
RS
8687/* Record that the current instruction is invalid for the current ISA. */
8688
8689static void
8690match_invalid_for_isa (void)
8691{
8692 set_insn_error_ss
1661c76c 8693 (0, _("opcode not supported on this processor: %s (%s)"),
60f20e8b
RS
8694 mips_cpu_info_from_arch (mips_opts.arch)->name,
8695 mips_cpu_info_from_isa (mips_opts.isa)->name);
8696}
8697
8698/* Try to match TOKENS against a series of opcode entries, starting at FIRST.
8699 Return true if a definite match or failure was found, storing any match
8700 in INSN. OPCODE_EXTRA is a value that should be ORed into the opcode
8701 (to handle things like VU0 suffixes). LAX_MATCH is true if we have already
8702 tried and failed to match under normal conditions and now want to try a
8703 more relaxed match. */
8704
8705static bfd_boolean
8706match_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8707 const struct mips_opcode *past, struct mips_operand_token *tokens,
8708 int opcode_extra, bfd_boolean lax_match)
8709{
8710 const struct mips_opcode *opcode;
8711 const struct mips_opcode *invalid_delay_slot;
8712 bfd_boolean seen_valid_for_isa, seen_valid_for_size;
8713
8714 /* Search for a match, ignoring alternatives that don't satisfy the
8715 current ISA or forced_length. */
8716 invalid_delay_slot = 0;
8717 seen_valid_for_isa = FALSE;
8718 seen_valid_for_size = FALSE;
8719 opcode = first;
8720 do
8721 {
8722 gas_assert (strcmp (opcode->name, first->name) == 0);
8723 if (is_opcode_valid (opcode))
8724 {
8725 seen_valid_for_isa = TRUE;
8726 if (is_size_valid (opcode))
8727 {
8728 bfd_boolean delay_slot_ok;
8729
8730 seen_valid_for_size = TRUE;
8731 delay_slot_ok = is_delay_slot_valid (opcode);
8732 if (match_insn (insn, opcode, tokens, opcode_extra,
8733 lax_match, delay_slot_ok))
8734 {
8735 if (!delay_slot_ok)
8736 {
8737 if (!invalid_delay_slot)
8738 invalid_delay_slot = opcode;
8739 }
8740 else
8741 return TRUE;
8742 }
8743 }
8744 }
8745 ++opcode;
8746 }
8747 while (opcode < past && strcmp (opcode->name, first->name) == 0);
8748
8749 /* If the only matches we found had the wrong length for the delay slot,
8750 pick the first such match. We'll issue an appropriate warning later. */
8751 if (invalid_delay_slot)
8752 {
8753 if (match_insn (insn, invalid_delay_slot, tokens, opcode_extra,
8754 lax_match, TRUE))
8755 return TRUE;
8756 abort ();
8757 }
8758
8759 /* Handle the case where we didn't try to match an instruction because
8760 all the alternatives were incompatible with the current ISA. */
8761 if (!seen_valid_for_isa)
8762 {
8763 match_invalid_for_isa ();
8764 return TRUE;
8765 }
8766
8767 /* Handle the case where we didn't try to match an instruction because
8768 all the alternatives were of the wrong size. */
8769 if (!seen_valid_for_size)
8770 {
8771 if (mips_opts.insn32)
1661c76c 8772 set_insn_error (0, _("opcode not supported in the `insn32' mode"));
60f20e8b
RS
8773 else
8774 set_insn_error_i
1661c76c 8775 (0, _("unrecognized %d-bit version of microMIPS opcode"),
60f20e8b
RS
8776 8 * forced_insn_length);
8777 return TRUE;
8778 }
8779
8780 return FALSE;
8781}
8782
8783/* Like match_insns, but for MIPS16. */
8784
8785static bfd_boolean
8786match_mips16_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8787 struct mips_operand_token *tokens)
8788{
8789 const struct mips_opcode *opcode;
8790 bfd_boolean seen_valid_for_isa;
7fd53920 8791 bfd_boolean seen_valid_for_size;
60f20e8b
RS
8792
8793 /* Search for a match, ignoring alternatives that don't satisfy the
8794 current ISA. There are no separate entries for extended forms so
8795 we deal with forced_length later. */
8796 seen_valid_for_isa = FALSE;
7fd53920 8797 seen_valid_for_size = FALSE;
60f20e8b
RS
8798 opcode = first;
8799 do
8800 {
8801 gas_assert (strcmp (opcode->name, first->name) == 0);
8802 if (is_opcode_valid_16 (opcode))
8803 {
8804 seen_valid_for_isa = TRUE;
7fd53920
MR
8805 if (is_size_valid_16 (opcode))
8806 {
8807 seen_valid_for_size = TRUE;
8808 if (match_mips16_insn (insn, opcode, tokens))
8809 return TRUE;
8810 }
60f20e8b
RS
8811 }
8812 ++opcode;
8813 }
8814 while (opcode < &mips16_opcodes[bfd_mips16_num_opcodes]
8815 && strcmp (opcode->name, first->name) == 0);
8816
8817 /* Handle the case where we didn't try to match an instruction because
8818 all the alternatives were incompatible with the current ISA. */
8819 if (!seen_valid_for_isa)
8820 {
8821 match_invalid_for_isa ();
8822 return TRUE;
8823 }
8824
7fd53920
MR
8825 /* Handle the case where we didn't try to match an instruction because
8826 all the alternatives were of the wrong size. */
8827 if (!seen_valid_for_size)
8828 {
8829 if (forced_insn_length == 2)
8830 set_insn_error
8831 (0, _("unrecognized unextended version of MIPS16 opcode"));
8832 else
8833 set_insn_error
8834 (0, _("unrecognized extended version of MIPS16 opcode"));
8835 return TRUE;
8836 }
8837
60f20e8b
RS
8838 return FALSE;
8839}
8840
584892a6
RS
8841/* Set up global variables for the start of a new macro. */
8842
8843static void
8844macro_start (void)
8845{
8846 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
df58fc94
RS
8847 memset (&mips_macro_warning.first_insn_sizes, 0,
8848 sizeof (mips_macro_warning.first_insn_sizes));
8849 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
584892a6 8850 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
11625dd8 8851 && delayed_branch_p (&history[0]));
7bd374a4
MR
8852 if (history[0].frag
8853 && history[0].frag->fr_type == rs_machine_dependent
8854 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
8855 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
8856 mips_macro_warning.delay_slot_length = 0;
8857 else
8858 switch (history[0].insn_mo->pinfo2
8859 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
8860 {
8861 case INSN2_BRANCH_DELAY_32BIT:
8862 mips_macro_warning.delay_slot_length = 4;
8863 break;
8864 case INSN2_BRANCH_DELAY_16BIT:
8865 mips_macro_warning.delay_slot_length = 2;
8866 break;
8867 default:
8868 mips_macro_warning.delay_slot_length = 0;
8869 break;
8870 }
df58fc94 8871 mips_macro_warning.first_frag = NULL;
584892a6
RS
8872}
8873
df58fc94
RS
8874/* Given that a macro is longer than one instruction or of the wrong size,
8875 return the appropriate warning for it. Return null if no warning is
8876 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
8877 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
8878 and RELAX_NOMACRO. */
584892a6
RS
8879
8880static const char *
8881macro_warning (relax_substateT subtype)
8882{
8883 if (subtype & RELAX_DELAY_SLOT)
1661c76c 8884 return _("macro instruction expanded into multiple instructions"
584892a6
RS
8885 " in a branch delay slot");
8886 else if (subtype & RELAX_NOMACRO)
1661c76c 8887 return _("macro instruction expanded into multiple instructions");
df58fc94
RS
8888 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
8889 | RELAX_DELAY_SLOT_SIZE_SECOND))
8890 return ((subtype & RELAX_DELAY_SLOT_16BIT)
1661c76c 8891 ? _("macro instruction expanded into a wrong size instruction"
df58fc94 8892 " in a 16-bit branch delay slot")
1661c76c 8893 : _("macro instruction expanded into a wrong size instruction"
df58fc94 8894 " in a 32-bit branch delay slot"));
584892a6
RS
8895 else
8896 return 0;
8897}
8898
8899/* Finish up a macro. Emit warnings as appropriate. */
8900
8901static void
8902macro_end (void)
8903{
df58fc94
RS
8904 /* Relaxation warning flags. */
8905 relax_substateT subtype = 0;
8906
8907 /* Check delay slot size requirements. */
8908 if (mips_macro_warning.delay_slot_length == 2)
8909 subtype |= RELAX_DELAY_SLOT_16BIT;
8910 if (mips_macro_warning.delay_slot_length != 0)
584892a6 8911 {
df58fc94
RS
8912 if (mips_macro_warning.delay_slot_length
8913 != mips_macro_warning.first_insn_sizes[0])
8914 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
8915 if (mips_macro_warning.delay_slot_length
8916 != mips_macro_warning.first_insn_sizes[1])
8917 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
8918 }
584892a6 8919
df58fc94
RS
8920 /* Check instruction count requirements. */
8921 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
8922 {
8923 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
584892a6
RS
8924 subtype |= RELAX_SECOND_LONGER;
8925 if (mips_opts.warn_about_macros)
8926 subtype |= RELAX_NOMACRO;
8927 if (mips_macro_warning.delay_slot_p)
8928 subtype |= RELAX_DELAY_SLOT;
df58fc94 8929 }
584892a6 8930
df58fc94
RS
8931 /* If both alternatives fail to fill a delay slot correctly,
8932 emit the warning now. */
8933 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
8934 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
8935 {
8936 relax_substateT s;
8937 const char *msg;
8938
8939 s = subtype & (RELAX_DELAY_SLOT_16BIT
8940 | RELAX_DELAY_SLOT_SIZE_FIRST
8941 | RELAX_DELAY_SLOT_SIZE_SECOND);
8942 msg = macro_warning (s);
8943 if (msg != NULL)
8944 as_warn ("%s", msg);
8945 subtype &= ~s;
8946 }
8947
8948 /* If both implementations are longer than 1 instruction, then emit the
8949 warning now. */
8950 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
8951 {
8952 relax_substateT s;
8953 const char *msg;
8954
8955 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
8956 msg = macro_warning (s);
8957 if (msg != NULL)
8958 as_warn ("%s", msg);
8959 subtype &= ~s;
584892a6 8960 }
df58fc94
RS
8961
8962 /* If any flags still set, then one implementation might need a warning
8963 and the other either will need one of a different kind or none at all.
8964 Pass any remaining flags over to relaxation. */
8965 if (mips_macro_warning.first_frag != NULL)
8966 mips_macro_warning.first_frag->fr_subtype |= subtype;
584892a6
RS
8967}
8968
df58fc94
RS
8969/* Instruction operand formats used in macros that vary between
8970 standard MIPS and microMIPS code. */
8971
833794fc 8972static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
df58fc94
RS
8973static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
8974static const char * const jalr_fmt[2] = { "d,s", "t,s" };
8975static const char * const lui_fmt[2] = { "t,u", "s,u" };
8976static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
833794fc 8977static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
df58fc94
RS
8978static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
8979static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
8980
833794fc 8981#define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
7361da2c
AB
8982#define COP12_FMT (ISA_IS_R6 (mips_opts.isa) ? "E,+:(d)" \
8983 : cop12_fmt[mips_opts.micromips])
df58fc94
RS
8984#define JALR_FMT (jalr_fmt[mips_opts.micromips])
8985#define LUI_FMT (lui_fmt[mips_opts.micromips])
8986#define MEM12_FMT (mem12_fmt[mips_opts.micromips])
7361da2c
AB
8987#define LL_SC_FMT (ISA_IS_R6 (mips_opts.isa) ? "t,+j(b)" \
8988 : mem12_fmt[mips_opts.micromips])
833794fc 8989#define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
df58fc94
RS
8990#define SHFT_FMT (shft_fmt[mips_opts.micromips])
8991#define TRAP_FMT (trap_fmt[mips_opts.micromips])
8992
6e1304d8
RS
8993/* Read a macro's relocation codes from *ARGS and store them in *R.
8994 The first argument in *ARGS will be either the code for a single
8995 relocation or -1 followed by the three codes that make up a
8996 composite relocation. */
8997
8998static void
8999macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
9000{
9001 int i, next;
9002
9003 next = va_arg (*args, int);
9004 if (next >= 0)
9005 r[0] = (bfd_reloc_code_real_type) next;
9006 else
f2ae14a1
RS
9007 {
9008 for (i = 0; i < 3; i++)
9009 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
9010 /* This function is only used for 16-bit relocation fields.
9011 To make the macro code simpler, treat an unrelocated value
9012 in the same way as BFD_RELOC_LO16. */
9013 if (r[0] == BFD_RELOC_UNUSED)
9014 r[0] = BFD_RELOC_LO16;
9015 }
6e1304d8
RS
9016}
9017
252b5132
RH
9018/* Build an instruction created by a macro expansion. This is passed
9019 a pointer to the count of instructions created so far, an
9020 expression, the name of the instruction to build, an operand format
9021 string, and corresponding arguments. */
9022
252b5132 9023static void
67c0d1eb 9024macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 9025{
df58fc94 9026 const struct mips_opcode *mo = NULL;
f6688943 9027 bfd_reloc_code_real_type r[3];
df58fc94 9028 const struct mips_opcode *amo;
e077a1c8 9029 const struct mips_operand *operand;
df58fc94
RS
9030 struct hash_control *hash;
9031 struct mips_cl_insn insn;
252b5132 9032 va_list args;
e077a1c8 9033 unsigned int uval;
252b5132 9034
252b5132 9035 va_start (args, fmt);
252b5132 9036
252b5132
RH
9037 if (mips_opts.mips16)
9038 {
03ea81db 9039 mips16_macro_build (ep, name, fmt, &args);
252b5132
RH
9040 va_end (args);
9041 return;
9042 }
9043
f6688943
TS
9044 r[0] = BFD_RELOC_UNUSED;
9045 r[1] = BFD_RELOC_UNUSED;
9046 r[2] = BFD_RELOC_UNUSED;
df58fc94
RS
9047 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
9048 amo = (struct mips_opcode *) hash_find (hash, name);
9049 gas_assert (amo);
9050 gas_assert (strcmp (name, amo->name) == 0);
1e915849 9051
df58fc94 9052 do
8b082fb1
TS
9053 {
9054 /* Search until we get a match for NAME. It is assumed here that
df58fc94 9055 macros will never generate MDMX, MIPS-3D, or MT instructions.
33eaf5de 9056 We try to match an instruction that fulfills the branch delay
df58fc94
RS
9057 slot instruction length requirement (if any) of the previous
9058 instruction. While doing this we record the first instruction
9059 seen that matches all the other conditions and use it anyway
9060 if the requirement cannot be met; we will issue an appropriate
9061 warning later on. */
9062 if (strcmp (fmt, amo->args) == 0
9063 && amo->pinfo != INSN_MACRO
9064 && is_opcode_valid (amo)
9065 && is_size_valid (amo))
9066 {
9067 if (is_delay_slot_valid (amo))
9068 {
9069 mo = amo;
9070 break;
9071 }
9072 else if (!mo)
9073 mo = amo;
9074 }
8b082fb1 9075
df58fc94
RS
9076 ++amo;
9077 gas_assert (amo->name);
252b5132 9078 }
df58fc94 9079 while (strcmp (name, amo->name) == 0);
252b5132 9080
df58fc94 9081 gas_assert (mo);
1e915849 9082 create_insn (&insn, mo);
e077a1c8 9083 for (; *fmt; ++fmt)
252b5132 9084 {
e077a1c8 9085 switch (*fmt)
252b5132 9086 {
252b5132
RH
9087 case ',':
9088 case '(':
9089 case ')':
252b5132 9090 case 'z':
e077a1c8 9091 break;
252b5132
RH
9092
9093 case 'i':
9094 case 'j':
6e1304d8 9095 macro_read_relocs (&args, r);
9c2799c2 9096 gas_assert (*r == BFD_RELOC_GPREL16
e391c024
RS
9097 || *r == BFD_RELOC_MIPS_HIGHER
9098 || *r == BFD_RELOC_HI16_S
9099 || *r == BFD_RELOC_LO16
14c80123
MR
9100 || *r == BFD_RELOC_MIPS_GOT_OFST
9101 || (mips_opts.micromips
9102 && (*r == BFD_RELOC_16
9103 || *r == BFD_RELOC_MIPS_GOT16
9104 || *r == BFD_RELOC_MIPS_CALL16
9105 || *r == BFD_RELOC_MIPS_GOT_HI16
9106 || *r == BFD_RELOC_MIPS_GOT_LO16
9107 || *r == BFD_RELOC_MIPS_CALL_HI16
9108 || *r == BFD_RELOC_MIPS_CALL_LO16
9109 || *r == BFD_RELOC_MIPS_SUB
9110 || *r == BFD_RELOC_MIPS_GOT_PAGE
9111 || *r == BFD_RELOC_MIPS_HIGHEST
9112 || *r == BFD_RELOC_MIPS_GOT_DISP
9113 || *r == BFD_RELOC_MIPS_TLS_GD
9114 || *r == BFD_RELOC_MIPS_TLS_LDM
9115 || *r == BFD_RELOC_MIPS_TLS_DTPREL_HI16
9116 || *r == BFD_RELOC_MIPS_TLS_DTPREL_LO16
9117 || *r == BFD_RELOC_MIPS_TLS_GOTTPREL
9118 || *r == BFD_RELOC_MIPS_TLS_TPREL_HI16
9119 || *r == BFD_RELOC_MIPS_TLS_TPREL_LO16)));
e077a1c8 9120 break;
e391c024
RS
9121
9122 case 'o':
9123 macro_read_relocs (&args, r);
e077a1c8 9124 break;
252b5132
RH
9125
9126 case 'u':
6e1304d8 9127 macro_read_relocs (&args, r);
9c2799c2 9128 gas_assert (ep != NULL
90ecf173
MR
9129 && (ep->X_op == O_constant
9130 || (ep->X_op == O_symbol
9131 && (*r == BFD_RELOC_MIPS_HIGHEST
9132 || *r == BFD_RELOC_HI16_S
9133 || *r == BFD_RELOC_HI16
9134 || *r == BFD_RELOC_GPREL16
9135 || *r == BFD_RELOC_MIPS_GOT_HI16
9136 || *r == BFD_RELOC_MIPS_CALL_HI16))));
e077a1c8 9137 break;
252b5132
RH
9138
9139 case 'p':
9c2799c2 9140 gas_assert (ep != NULL);
bad36eac 9141
252b5132
RH
9142 /*
9143 * This allows macro() to pass an immediate expression for
9144 * creating short branches without creating a symbol.
bad36eac
DJ
9145 *
9146 * We don't allow branch relaxation for these branches, as
9147 * they should only appear in ".set nomacro" anyway.
252b5132
RH
9148 */
9149 if (ep->X_op == O_constant)
9150 {
df58fc94
RS
9151 /* For microMIPS we always use relocations for branches.
9152 So we should not resolve immediate values. */
9153 gas_assert (!mips_opts.micromips);
9154
bad36eac
DJ
9155 if ((ep->X_add_number & 3) != 0)
9156 as_bad (_("branch to misaligned address (0x%lx)"),
9157 (unsigned long) ep->X_add_number);
9158 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
9159 as_bad (_("branch address range overflow (0x%lx)"),
9160 (unsigned long) ep->X_add_number);
252b5132
RH
9161 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
9162 ep = NULL;
9163 }
9164 else
0b25d3e6 9165 *r = BFD_RELOC_16_PCREL_S2;
e077a1c8 9166 break;
252b5132
RH
9167
9168 case 'a':
9c2799c2 9169 gas_assert (ep != NULL);
f6688943 9170 *r = BFD_RELOC_MIPS_JMP;
e077a1c8 9171 break;
d43b4baf 9172
252b5132 9173 default:
e077a1c8
RS
9174 operand = (mips_opts.micromips
9175 ? decode_micromips_operand (fmt)
9176 : decode_mips_operand (fmt));
9177 if (!operand)
9178 abort ();
9179
9180 uval = va_arg (args, int);
9181 if (operand->type == OP_CLO_CLZ_DEST)
9182 uval |= (uval << 5);
9183 insn_insert_operand (&insn, operand, uval);
9184
7361da2c 9185 if (*fmt == '+' || *fmt == 'm' || *fmt == '-')
e077a1c8
RS
9186 ++fmt;
9187 break;
252b5132 9188 }
252b5132
RH
9189 }
9190 va_end (args);
9c2799c2 9191 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 9192
df58fc94 9193 append_insn (&insn, ep, r, TRUE);
252b5132
RH
9194}
9195
9196static void
67c0d1eb 9197mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
03ea81db 9198 va_list *args)
252b5132 9199{
1e915849 9200 struct mips_opcode *mo;
252b5132 9201 struct mips_cl_insn insn;
e077a1c8 9202 const struct mips_operand *operand;
f6688943
TS
9203 bfd_reloc_code_real_type r[3]
9204 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 9205
1e915849 9206 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
9c2799c2
NC
9207 gas_assert (mo);
9208 gas_assert (strcmp (name, mo->name) == 0);
252b5132 9209
1e915849 9210 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 9211 {
1e915849 9212 ++mo;
9c2799c2
NC
9213 gas_assert (mo->name);
9214 gas_assert (strcmp (name, mo->name) == 0);
252b5132
RH
9215 }
9216
1e915849 9217 create_insn (&insn, mo);
e077a1c8 9218 for (; *fmt; ++fmt)
252b5132
RH
9219 {
9220 int c;
9221
e077a1c8 9222 c = *fmt;
252b5132
RH
9223 switch (c)
9224 {
252b5132
RH
9225 case ',':
9226 case '(':
9227 case ')':
e077a1c8 9228 break;
252b5132 9229
d8722d76 9230 case '.':
252b5132
RH
9231 case 'S':
9232 case 'P':
9233 case 'R':
e077a1c8 9234 break;
252b5132
RH
9235
9236 case '<':
252b5132 9237 case '5':
d8722d76 9238 case 'F':
252b5132
RH
9239 case 'H':
9240 case 'W':
9241 case 'D':
9242 case 'j':
9243 case '8':
9244 case 'V':
9245 case 'C':
9246 case 'U':
9247 case 'k':
9248 case 'K':
9249 case 'p':
9250 case 'q':
9251 {
b886a2ab
RS
9252 offsetT value;
9253
9c2799c2 9254 gas_assert (ep != NULL);
252b5132
RH
9255
9256 if (ep->X_op != O_constant)
874e8986 9257 *r = (int) BFD_RELOC_UNUSED + c;
b886a2ab 9258 else if (calculate_reloc (*r, ep->X_add_number, &value))
252b5132 9259 {
b886a2ab 9260 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
252b5132 9261 ep = NULL;
f6688943 9262 *r = BFD_RELOC_UNUSED;
252b5132
RH
9263 }
9264 }
e077a1c8 9265 break;
252b5132 9266
e077a1c8
RS
9267 default:
9268 operand = decode_mips16_operand (c, FALSE);
9269 if (!operand)
9270 abort ();
252b5132 9271
4a06e5a2 9272 insn_insert_operand (&insn, operand, va_arg (*args, int));
e077a1c8
RS
9273 break;
9274 }
252b5132
RH
9275 }
9276
9c2799c2 9277 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 9278
df58fc94 9279 append_insn (&insn, ep, r, TRUE);
252b5132
RH
9280}
9281
438c16b8
TS
9282/*
9283 * Generate a "jalr" instruction with a relocation hint to the called
9284 * function. This occurs in NewABI PIC code.
9285 */
9286static void
df58fc94 9287macro_build_jalr (expressionS *ep, int cprestore)
438c16b8 9288{
df58fc94
RS
9289 static const bfd_reloc_code_real_type jalr_relocs[2]
9290 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
9291 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
9292 const char *jalr;
685736be 9293 char *f = NULL;
b34976b6 9294
1180b5a4 9295 if (MIPS_JALR_HINT_P (ep))
f21f8242 9296 {
cc3d92a5 9297 frag_grow (8);
f21f8242
AO
9298 f = frag_more (0);
9299 }
2906b037 9300 if (mips_opts.micromips)
df58fc94 9301 {
833794fc
MR
9302 jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
9303 ? "jalr" : "jalrs");
e64af278 9304 if (MIPS_JALR_HINT_P (ep)
833794fc 9305 || mips_opts.insn32
e64af278 9306 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
9307 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
9308 else
9309 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
9310 }
2906b037
MR
9311 else
9312 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
1180b5a4 9313 if (MIPS_JALR_HINT_P (ep))
df58fc94 9314 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
438c16b8
TS
9315}
9316
252b5132
RH
9317/*
9318 * Generate a "lui" instruction.
9319 */
9320static void
67c0d1eb 9321macro_build_lui (expressionS *ep, int regnum)
252b5132 9322{
9c2799c2 9323 gas_assert (! mips_opts.mips16);
252b5132 9324
df58fc94 9325 if (ep->X_op != O_constant)
252b5132 9326 {
9c2799c2 9327 gas_assert (ep->X_op == O_symbol);
bbe506e8
TS
9328 /* _gp_disp is a special case, used from s_cpload.
9329 __gnu_local_gp is used if mips_no_shared. */
9c2799c2 9330 gas_assert (mips_pic == NO_PIC
78e1bb40 9331 || (! HAVE_NEWABI
aa6975fb
ILT
9332 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
9333 || (! mips_in_shared
bbe506e8
TS
9334 && strcmp (S_GET_NAME (ep->X_add_symbol),
9335 "__gnu_local_gp") == 0));
252b5132
RH
9336 }
9337
df58fc94 9338 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
252b5132
RH
9339}
9340
885add95
CD
9341/* Generate a sequence of instructions to do a load or store from a constant
9342 offset off of a base register (breg) into/from a target register (treg),
9343 using AT if necessary. */
9344static void
67c0d1eb
RS
9345macro_build_ldst_constoffset (expressionS *ep, const char *op,
9346 int treg, int breg, int dbl)
885add95 9347{
9c2799c2 9348 gas_assert (ep->X_op == O_constant);
885add95 9349
256ab948 9350 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
9351 if (!dbl)
9352 normalize_constant_expr (ep);
256ab948 9353
67c1ffbe 9354 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 9355 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
9356 as_warn (_("operand overflow"));
9357
9358 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
9359 {
9360 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 9361 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
9362 }
9363 else
9364 {
9365 /* 32-bit offset, need multiple instructions and AT, like:
9366 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
9367 addu $tempreg,$tempreg,$breg
9368 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
9369 to handle the complete offset. */
67c0d1eb
RS
9370 macro_build_lui (ep, AT);
9371 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
9372 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95 9373
741fe287 9374 if (!mips_opts.at)
1661c76c 9375 as_bad (_("macro used $at after \".set noat\""));
885add95
CD
9376 }
9377}
9378
252b5132
RH
9379/* set_at()
9380 * Generates code to set the $at register to true (one)
9381 * if reg is less than the immediate expression.
9382 */
9383static void
67c0d1eb 9384set_at (int reg, int unsignedp)
252b5132 9385{
b0e6f033 9386 if (imm_expr.X_add_number >= -0x8000
252b5132 9387 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
9388 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
9389 AT, reg, BFD_RELOC_LO16);
252b5132
RH
9390 else
9391 {
bad1aba3 9392 load_register (AT, &imm_expr, GPR_SIZE == 64);
67c0d1eb 9393 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
9394 }
9395}
9396
252b5132
RH
9397/* Count the leading zeroes by performing a binary chop. This is a
9398 bulky bit of source, but performance is a LOT better for the
9399 majority of values than a simple loop to count the bits:
9400 for (lcnt = 0; (lcnt < 32); lcnt++)
9401 if ((v) & (1 << (31 - lcnt)))
9402 break;
9403 However it is not code size friendly, and the gain will drop a bit
9404 on certain cached systems.
9405*/
9406#define COUNT_TOP_ZEROES(v) \
9407 (((v) & ~0xffff) == 0 \
9408 ? ((v) & ~0xff) == 0 \
9409 ? ((v) & ~0xf) == 0 \
9410 ? ((v) & ~0x3) == 0 \
9411 ? ((v) & ~0x1) == 0 \
9412 ? !(v) \
9413 ? 32 \
9414 : 31 \
9415 : 30 \
9416 : ((v) & ~0x7) == 0 \
9417 ? 29 \
9418 : 28 \
9419 : ((v) & ~0x3f) == 0 \
9420 ? ((v) & ~0x1f) == 0 \
9421 ? 27 \
9422 : 26 \
9423 : ((v) & ~0x7f) == 0 \
9424 ? 25 \
9425 : 24 \
9426 : ((v) & ~0xfff) == 0 \
9427 ? ((v) & ~0x3ff) == 0 \
9428 ? ((v) & ~0x1ff) == 0 \
9429 ? 23 \
9430 : 22 \
9431 : ((v) & ~0x7ff) == 0 \
9432 ? 21 \
9433 : 20 \
9434 : ((v) & ~0x3fff) == 0 \
9435 ? ((v) & ~0x1fff) == 0 \
9436 ? 19 \
9437 : 18 \
9438 : ((v) & ~0x7fff) == 0 \
9439 ? 17 \
9440 : 16 \
9441 : ((v) & ~0xffffff) == 0 \
9442 ? ((v) & ~0xfffff) == 0 \
9443 ? ((v) & ~0x3ffff) == 0 \
9444 ? ((v) & ~0x1ffff) == 0 \
9445 ? 15 \
9446 : 14 \
9447 : ((v) & ~0x7ffff) == 0 \
9448 ? 13 \
9449 : 12 \
9450 : ((v) & ~0x3fffff) == 0 \
9451 ? ((v) & ~0x1fffff) == 0 \
9452 ? 11 \
9453 : 10 \
9454 : ((v) & ~0x7fffff) == 0 \
9455 ? 9 \
9456 : 8 \
9457 : ((v) & ~0xfffffff) == 0 \
9458 ? ((v) & ~0x3ffffff) == 0 \
9459 ? ((v) & ~0x1ffffff) == 0 \
9460 ? 7 \
9461 : 6 \
9462 : ((v) & ~0x7ffffff) == 0 \
9463 ? 5 \
9464 : 4 \
9465 : ((v) & ~0x3fffffff) == 0 \
9466 ? ((v) & ~0x1fffffff) == 0 \
9467 ? 3 \
9468 : 2 \
9469 : ((v) & ~0x7fffffff) == 0 \
9470 ? 1 \
9471 : 0)
9472
9473/* load_register()
67c1ffbe 9474 * This routine generates the least number of instructions necessary to load
252b5132
RH
9475 * an absolute expression value into a register.
9476 */
9477static void
67c0d1eb 9478load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
9479{
9480 int freg;
9481 expressionS hi32, lo32;
9482
9483 if (ep->X_op != O_big)
9484 {
9c2799c2 9485 gas_assert (ep->X_op == O_constant);
256ab948
TS
9486
9487 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
9488 if (!dbl)
9489 normalize_constant_expr (ep);
256ab948
TS
9490
9491 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
9492 {
9493 /* We can handle 16 bit signed values with an addiu to
9494 $zero. No need to ever use daddiu here, since $zero and
9495 the result are always correct in 32 bit mode. */
67c0d1eb 9496 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9497 return;
9498 }
9499 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
9500 {
9501 /* We can handle 16 bit unsigned values with an ori to
9502 $zero. */
67c0d1eb 9503 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9504 return;
9505 }
256ab948 9506 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
9507 {
9508 /* 32 bit values require an lui. */
df58fc94 9509 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 9510 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 9511 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
9512 return;
9513 }
9514 }
9515
9516 /* The value is larger than 32 bits. */
9517
bad1aba3 9518 if (!dbl || GPR_SIZE == 32)
252b5132 9519 {
55e08f71
NC
9520 char value[32];
9521
9522 sprintf_vma (value, ep->X_add_number);
1661c76c 9523 as_bad (_("number (0x%s) larger than 32 bits"), value);
67c0d1eb 9524 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9525 return;
9526 }
9527
9528 if (ep->X_op != O_big)
9529 {
9530 hi32 = *ep;
9531 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9532 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9533 hi32.X_add_number &= 0xffffffff;
9534 lo32 = *ep;
9535 lo32.X_add_number &= 0xffffffff;
9536 }
9537 else
9538 {
9c2799c2 9539 gas_assert (ep->X_add_number > 2);
252b5132
RH
9540 if (ep->X_add_number == 3)
9541 generic_bignum[3] = 0;
9542 else if (ep->X_add_number > 4)
1661c76c 9543 as_bad (_("number larger than 64 bits"));
252b5132
RH
9544 lo32.X_op = O_constant;
9545 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
9546 hi32.X_op = O_constant;
9547 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
9548 }
9549
9550 if (hi32.X_add_number == 0)
9551 freg = 0;
9552 else
9553 {
9554 int shift, bit;
9555 unsigned long hi, lo;
9556
956cd1d6 9557 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
9558 {
9559 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
9560 {
67c0d1eb 9561 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
9562 return;
9563 }
9564 if (lo32.X_add_number & 0x80000000)
9565 {
df58fc94 9566 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 9567 if (lo32.X_add_number & 0xffff)
67c0d1eb 9568 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
9569 return;
9570 }
9571 }
252b5132
RH
9572
9573 /* Check for 16bit shifted constant. We know that hi32 is
9574 non-zero, so start the mask on the first bit of the hi32
9575 value. */
9576 shift = 17;
9577 do
beae10d5
KH
9578 {
9579 unsigned long himask, lomask;
9580
9581 if (shift < 32)
9582 {
9583 himask = 0xffff >> (32 - shift);
9584 lomask = (0xffff << shift) & 0xffffffff;
9585 }
9586 else
9587 {
9588 himask = 0xffff << (shift - 32);
9589 lomask = 0;
9590 }
9591 if ((hi32.X_add_number & ~(offsetT) himask) == 0
9592 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
9593 {
9594 expressionS tmp;
9595
9596 tmp.X_op = O_constant;
9597 if (shift < 32)
9598 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
9599 | (lo32.X_add_number >> shift));
9600 else
9601 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb 9602 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
df58fc94 9603 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 9604 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
9605 return;
9606 }
f9419b05 9607 ++shift;
beae10d5
KH
9608 }
9609 while (shift <= (64 - 16));
252b5132
RH
9610
9611 /* Find the bit number of the lowest one bit, and store the
9612 shifted value in hi/lo. */
9613 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
9614 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
9615 if (lo != 0)
9616 {
9617 bit = 0;
9618 while ((lo & 1) == 0)
9619 {
9620 lo >>= 1;
9621 ++bit;
9622 }
9623 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
9624 hi >>= bit;
9625 }
9626 else
9627 {
9628 bit = 32;
9629 while ((hi & 1) == 0)
9630 {
9631 hi >>= 1;
9632 ++bit;
9633 }
9634 lo = hi;
9635 hi = 0;
9636 }
9637
9638 /* Optimize if the shifted value is a (power of 2) - 1. */
9639 if ((hi == 0 && ((lo + 1) & lo) == 0)
9640 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
9641 {
9642 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 9643 if (shift != 0)
beae10d5 9644 {
252b5132
RH
9645 expressionS tmp;
9646
9647 /* This instruction will set the register to be all
9648 ones. */
beae10d5
KH
9649 tmp.X_op = O_constant;
9650 tmp.X_add_number = (offsetT) -1;
67c0d1eb 9651 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
9652 if (bit != 0)
9653 {
9654 bit += shift;
df58fc94 9655 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 9656 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 9657 }
df58fc94 9658 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
67c0d1eb 9659 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
9660 return;
9661 }
9662 }
252b5132
RH
9663
9664 /* Sign extend hi32 before calling load_register, because we can
9665 generally get better code when we load a sign extended value. */
9666 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 9667 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 9668 load_register (reg, &hi32, 0);
252b5132
RH
9669 freg = reg;
9670 }
9671 if ((lo32.X_add_number & 0xffff0000) == 0)
9672 {
9673 if (freg != 0)
9674 {
df58fc94 9675 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
252b5132
RH
9676 freg = reg;
9677 }
9678 }
9679 else
9680 {
9681 expressionS mid16;
9682
956cd1d6 9683 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 9684 {
df58fc94
RS
9685 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9686 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
beae10d5
KH
9687 return;
9688 }
252b5132
RH
9689
9690 if (freg != 0)
9691 {
df58fc94 9692 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
252b5132
RH
9693 freg = reg;
9694 }
9695 mid16 = lo32;
9696 mid16.X_add_number >>= 16;
67c0d1eb 9697 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
df58fc94 9698 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
252b5132
RH
9699 freg = reg;
9700 }
9701 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 9702 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
9703}
9704
269137b2
TS
9705static inline void
9706load_delay_nop (void)
9707{
9708 if (!gpr_interlocks)
9709 macro_build (NULL, "nop", "");
9710}
9711
252b5132
RH
9712/* Load an address into a register. */
9713
9714static void
67c0d1eb 9715load_address (int reg, expressionS *ep, int *used_at)
252b5132 9716{
252b5132
RH
9717 if (ep->X_op != O_constant
9718 && ep->X_op != O_symbol)
9719 {
9720 as_bad (_("expression too complex"));
9721 ep->X_op = O_constant;
9722 }
9723
9724 if (ep->X_op == O_constant)
9725 {
67c0d1eb 9726 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
9727 return;
9728 }
9729
9730 if (mips_pic == NO_PIC)
9731 {
9732 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 9733 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
9734 Otherwise we want
9735 lui $reg,<sym> (BFD_RELOC_HI16_S)
9736 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 9737 If we have an addend, we always use the latter form.
76b3015f 9738
d6bc6245
TS
9739 With 64bit address space and a usable $at we want
9740 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9741 lui $at,<sym> (BFD_RELOC_HI16_S)
9742 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9743 daddiu $at,<sym> (BFD_RELOC_LO16)
9744 dsll32 $reg,0
3a482fd5 9745 daddu $reg,$reg,$at
76b3015f 9746
c03099e6 9747 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
9748 on superscalar processors.
9749 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9750 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9751 dsll $reg,16
9752 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
9753 dsll $reg,16
9754 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
9755
9756 For GP relative symbols in 64bit address space we can use
9757 the same sequence as in 32bit address space. */
aed1a261 9758 if (HAVE_64BIT_SYMBOLS)
d6bc6245 9759 {
6caf9ef4
TS
9760 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9761 && !nopic_need_relax (ep->X_add_symbol, 1))
9762 {
9763 relax_start (ep->X_add_symbol);
9764 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9765 mips_gp_register, BFD_RELOC_GPREL16);
9766 relax_switch ();
9767 }
d6bc6245 9768
741fe287 9769 if (*used_at == 0 && mips_opts.at)
d6bc6245 9770 {
df58fc94
RS
9771 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9772 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
67c0d1eb
RS
9773 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9774 BFD_RELOC_MIPS_HIGHER);
9775 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
df58fc94 9776 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
67c0d1eb 9777 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
9778 *used_at = 1;
9779 }
9780 else
9781 {
df58fc94 9782 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb
RS
9783 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9784 BFD_RELOC_MIPS_HIGHER);
df58fc94 9785 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 9786 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
df58fc94 9787 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 9788 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 9789 }
6caf9ef4
TS
9790
9791 if (mips_relax.sequence)
9792 relax_end ();
d6bc6245 9793 }
252b5132
RH
9794 else
9795 {
d6bc6245 9796 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 9797 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 9798 {
4d7206a2 9799 relax_start (ep->X_add_symbol);
67c0d1eb 9800 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 9801 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 9802 relax_switch ();
d6bc6245 9803 }
67c0d1eb
RS
9804 macro_build_lui (ep, reg);
9805 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
9806 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
9807 if (mips_relax.sequence)
9808 relax_end ();
d6bc6245 9809 }
252b5132 9810 }
0a44bf69 9811 else if (!mips_big_got)
252b5132
RH
9812 {
9813 expressionS ex;
9814
9815 /* If this is a reference to an external symbol, we want
9816 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9817 Otherwise we want
9818 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9819 nop
9820 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
9821 If there is a constant, it must be added in after.
9822
ed6fb7bd 9823 If we have NewABI, we want
f5040a92
AO
9824 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
9825 unless we're referencing a global symbol with a non-zero
9826 offset, in which case cst must be added separately. */
ed6fb7bd
SC
9827 if (HAVE_NEWABI)
9828 {
f5040a92
AO
9829 if (ep->X_add_number)
9830 {
4d7206a2 9831 ex.X_add_number = ep->X_add_number;
f5040a92 9832 ep->X_add_number = 0;
4d7206a2 9833 relax_start (ep->X_add_symbol);
67c0d1eb
RS
9834 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9835 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
9836 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9837 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9838 ex.X_op = O_constant;
67c0d1eb 9839 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 9840 reg, reg, BFD_RELOC_LO16);
f5040a92 9841 ep->X_add_number = ex.X_add_number;
4d7206a2 9842 relax_switch ();
f5040a92 9843 }
67c0d1eb 9844 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9845 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
9846 if (mips_relax.sequence)
9847 relax_end ();
ed6fb7bd
SC
9848 }
9849 else
9850 {
f5040a92
AO
9851 ex.X_add_number = ep->X_add_number;
9852 ep->X_add_number = 0;
67c0d1eb
RS
9853 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9854 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9855 load_delay_nop ();
4d7206a2
RS
9856 relax_start (ep->X_add_symbol);
9857 relax_switch ();
67c0d1eb 9858 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 9859 BFD_RELOC_LO16);
4d7206a2 9860 relax_end ();
ed6fb7bd 9861
f5040a92
AO
9862 if (ex.X_add_number != 0)
9863 {
9864 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9865 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9866 ex.X_op = O_constant;
67c0d1eb 9867 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 9868 reg, reg, BFD_RELOC_LO16);
f5040a92 9869 }
252b5132
RH
9870 }
9871 }
0a44bf69 9872 else if (mips_big_got)
252b5132
RH
9873 {
9874 expressionS ex;
252b5132
RH
9875
9876 /* This is the large GOT case. If this is a reference to an
9877 external symbol, we want
9878 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9879 addu $reg,$reg,$gp
9880 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
9881
9882 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
9883 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9884 nop
9885 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 9886 If there is a constant, it must be added in after.
f5040a92
AO
9887
9888 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
9889 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
9890 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 9891 */
438c16b8
TS
9892 if (HAVE_NEWABI)
9893 {
4d7206a2 9894 ex.X_add_number = ep->X_add_number;
f5040a92 9895 ep->X_add_number = 0;
4d7206a2 9896 relax_start (ep->X_add_symbol);
df58fc94 9897 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
9898 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9899 reg, reg, mips_gp_register);
9900 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9901 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
9902 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9903 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9904 else if (ex.X_add_number)
9905 {
9906 ex.X_op = O_constant;
67c0d1eb
RS
9907 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9908 BFD_RELOC_LO16);
f5040a92
AO
9909 }
9910
9911 ep->X_add_number = ex.X_add_number;
4d7206a2 9912 relax_switch ();
67c0d1eb 9913 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9914 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
9915 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9916 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 9917 relax_end ();
438c16b8 9918 }
252b5132 9919 else
438c16b8 9920 {
f5040a92
AO
9921 ex.X_add_number = ep->X_add_number;
9922 ep->X_add_number = 0;
4d7206a2 9923 relax_start (ep->X_add_symbol);
df58fc94 9924 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
9925 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9926 reg, reg, mips_gp_register);
9927 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9928 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
9929 relax_switch ();
9930 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
9931 {
9932 /* We need a nop before loading from $gp. This special
9933 check is required because the lui which starts the main
9934 instruction stream does not refer to $gp, and so will not
9935 insert the nop which may be required. */
67c0d1eb 9936 macro_build (NULL, "nop", "");
438c16b8 9937 }
67c0d1eb 9938 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9939 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9940 load_delay_nop ();
67c0d1eb 9941 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 9942 BFD_RELOC_LO16);
4d7206a2 9943 relax_end ();
438c16b8 9944
f5040a92
AO
9945 if (ex.X_add_number != 0)
9946 {
9947 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9948 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9949 ex.X_op = O_constant;
67c0d1eb
RS
9950 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9951 BFD_RELOC_LO16);
f5040a92 9952 }
252b5132
RH
9953 }
9954 }
252b5132
RH
9955 else
9956 abort ();
8fc2e39e 9957
741fe287 9958 if (!mips_opts.at && *used_at == 1)
1661c76c 9959 as_bad (_("macro used $at after \".set noat\""));
252b5132
RH
9960}
9961
ea1fb5dc
RS
9962/* Move the contents of register SOURCE into register DEST. */
9963
9964static void
67c0d1eb 9965move_register (int dest, int source)
ea1fb5dc 9966{
df58fc94
RS
9967 /* Prefer to use a 16-bit microMIPS instruction unless the previous
9968 instruction specifically requires a 32-bit one. */
9969 if (mips_opts.micromips
833794fc 9970 && !mips_opts.insn32
df58fc94 9971 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7951ca42 9972 macro_build (NULL, "move", "mp,mj", dest, source);
df58fc94 9973 else
40fc1451 9974 macro_build (NULL, "or", "d,v,t", dest, source, 0);
ea1fb5dc
RS
9975}
9976
4d7206a2 9977/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
9978 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
9979 The two alternatives are:
4d7206a2 9980
33eaf5de 9981 Global symbol Local symbol
4d7206a2
RS
9982 ------------- ------------
9983 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
9984 ... ...
9985 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
9986
9987 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
9988 emits the second for a 16-bit offset or add_got_offset_hilo emits
9989 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
9990
9991static void
67c0d1eb 9992load_got_offset (int dest, expressionS *local)
4d7206a2
RS
9993{
9994 expressionS global;
9995
9996 global = *local;
9997 global.X_add_number = 0;
9998
9999 relax_start (local->X_add_symbol);
67c0d1eb
RS
10000 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
10001 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 10002 relax_switch ();
67c0d1eb
RS
10003 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
10004 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
10005 relax_end ();
10006}
10007
10008static void
67c0d1eb 10009add_got_offset (int dest, expressionS *local)
4d7206a2
RS
10010{
10011 expressionS global;
10012
10013 global.X_op = O_constant;
10014 global.X_op_symbol = NULL;
10015 global.X_add_symbol = NULL;
10016 global.X_add_number = local->X_add_number;
10017
10018 relax_start (local->X_add_symbol);
67c0d1eb 10019 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
10020 dest, dest, BFD_RELOC_LO16);
10021 relax_switch ();
67c0d1eb 10022 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
10023 relax_end ();
10024}
10025
f6a22291
MR
10026static void
10027add_got_offset_hilo (int dest, expressionS *local, int tmp)
10028{
10029 expressionS global;
10030 int hold_mips_optimize;
10031
10032 global.X_op = O_constant;
10033 global.X_op_symbol = NULL;
10034 global.X_add_symbol = NULL;
10035 global.X_add_number = local->X_add_number;
10036
10037 relax_start (local->X_add_symbol);
10038 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
10039 relax_switch ();
10040 /* Set mips_optimize around the lui instruction to avoid
10041 inserting an unnecessary nop after the lw. */
10042 hold_mips_optimize = mips_optimize;
10043 mips_optimize = 2;
10044 macro_build_lui (&global, tmp);
10045 mips_optimize = hold_mips_optimize;
10046 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
10047 relax_end ();
10048
10049 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
10050}
10051
df58fc94
RS
10052/* Emit a sequence of instructions to emulate a branch likely operation.
10053 BR is an ordinary branch corresponding to one to be emulated. BRNEG
10054 is its complementing branch with the original condition negated.
10055 CALL is set if the original branch specified the link operation.
10056 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
10057
10058 Code like this is produced in the noreorder mode:
10059
10060 BRNEG <args>, 1f
10061 nop
10062 b <sym>
10063 delay slot (executed only if branch taken)
10064 1:
10065
10066 or, if CALL is set:
10067
10068 BRNEG <args>, 1f
10069 nop
10070 bal <sym>
10071 delay slot (executed only if branch taken)
10072 1:
10073
10074 In the reorder mode the delay slot would be filled with a nop anyway,
10075 so code produced is simply:
10076
10077 BR <args>, <sym>
10078 nop
10079
10080 This function is used when producing code for the microMIPS ASE that
10081 does not implement branch likely instructions in hardware. */
10082
10083static void
10084macro_build_branch_likely (const char *br, const char *brneg,
10085 int call, expressionS *ep, const char *fmt,
10086 unsigned int sreg, unsigned int treg)
10087{
10088 int noreorder = mips_opts.noreorder;
10089 expressionS expr1;
10090
10091 gas_assert (mips_opts.micromips);
10092 start_noreorder ();
10093 if (noreorder)
10094 {
10095 micromips_label_expr (&expr1);
10096 macro_build (&expr1, brneg, fmt, sreg, treg);
10097 macro_build (NULL, "nop", "");
10098 macro_build (ep, call ? "bal" : "b", "p");
10099
10100 /* Set to true so that append_insn adds a label. */
10101 emit_branch_likely_macro = TRUE;
10102 }
10103 else
10104 {
10105 macro_build (ep, br, fmt, sreg, treg);
10106 macro_build (NULL, "nop", "");
10107 }
10108 end_noreorder ();
10109}
10110
10111/* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
10112 the condition code tested. EP specifies the branch target. */
10113
10114static void
10115macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
10116{
10117 const int call = 0;
10118 const char *brneg;
10119 const char *br;
10120
10121 switch (type)
10122 {
10123 case M_BC1FL:
10124 br = "bc1f";
10125 brneg = "bc1t";
10126 break;
10127 case M_BC1TL:
10128 br = "bc1t";
10129 brneg = "bc1f";
10130 break;
10131 case M_BC2FL:
10132 br = "bc2f";
10133 brneg = "bc2t";
10134 break;
10135 case M_BC2TL:
10136 br = "bc2t";
10137 brneg = "bc2f";
10138 break;
10139 default:
10140 abort ();
10141 }
10142 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
10143}
10144
10145/* Emit a two-argument branch macro specified by TYPE, using SREG as
10146 the register tested. EP specifies the branch target. */
10147
10148static void
10149macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
10150{
10151 const char *brneg = NULL;
10152 const char *br;
10153 int call = 0;
10154
10155 switch (type)
10156 {
10157 case M_BGEZ:
10158 br = "bgez";
10159 break;
10160 case M_BGEZL:
10161 br = mips_opts.micromips ? "bgez" : "bgezl";
10162 brneg = "bltz";
10163 break;
10164 case M_BGEZALL:
10165 gas_assert (mips_opts.micromips);
833794fc 10166 br = mips_opts.insn32 ? "bgezal" : "bgezals";
df58fc94
RS
10167 brneg = "bltz";
10168 call = 1;
10169 break;
10170 case M_BGTZ:
10171 br = "bgtz";
10172 break;
10173 case M_BGTZL:
10174 br = mips_opts.micromips ? "bgtz" : "bgtzl";
10175 brneg = "blez";
10176 break;
10177 case M_BLEZ:
10178 br = "blez";
10179 break;
10180 case M_BLEZL:
10181 br = mips_opts.micromips ? "blez" : "blezl";
10182 brneg = "bgtz";
10183 break;
10184 case M_BLTZ:
10185 br = "bltz";
10186 break;
10187 case M_BLTZL:
10188 br = mips_opts.micromips ? "bltz" : "bltzl";
10189 brneg = "bgez";
10190 break;
10191 case M_BLTZALL:
10192 gas_assert (mips_opts.micromips);
833794fc 10193 br = mips_opts.insn32 ? "bltzal" : "bltzals";
df58fc94
RS
10194 brneg = "bgez";
10195 call = 1;
10196 break;
10197 default:
10198 abort ();
10199 }
10200 if (mips_opts.micromips && brneg)
10201 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
10202 else
10203 macro_build (ep, br, "s,p", sreg);
10204}
10205
10206/* Emit a three-argument branch macro specified by TYPE, using SREG and
10207 TREG as the registers tested. EP specifies the branch target. */
10208
10209static void
10210macro_build_branch_rsrt (int type, expressionS *ep,
10211 unsigned int sreg, unsigned int treg)
10212{
10213 const char *brneg = NULL;
10214 const int call = 0;
10215 const char *br;
10216
10217 switch (type)
10218 {
10219 case M_BEQ:
10220 case M_BEQ_I:
10221 br = "beq";
10222 break;
10223 case M_BEQL:
10224 case M_BEQL_I:
10225 br = mips_opts.micromips ? "beq" : "beql";
10226 brneg = "bne";
10227 break;
10228 case M_BNE:
10229 case M_BNE_I:
10230 br = "bne";
10231 break;
10232 case M_BNEL:
10233 case M_BNEL_I:
10234 br = mips_opts.micromips ? "bne" : "bnel";
10235 brneg = "beq";
10236 break;
10237 default:
10238 abort ();
10239 }
10240 if (mips_opts.micromips && brneg)
10241 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
10242 else
10243 macro_build (ep, br, "s,t,p", sreg, treg);
10244}
10245
f2ae14a1
RS
10246/* Return the high part that should be loaded in order to make the low
10247 part of VALUE accessible using an offset of OFFBITS bits. */
10248
10249static offsetT
10250offset_high_part (offsetT value, unsigned int offbits)
10251{
10252 offsetT bias;
10253 addressT low_mask;
10254
10255 if (offbits == 0)
10256 return value;
10257 bias = 1 << (offbits - 1);
10258 low_mask = bias * 2 - 1;
10259 return (value + bias) & ~low_mask;
10260}
10261
10262/* Return true if the value stored in offset_expr and offset_reloc
10263 fits into a signed offset of OFFBITS bits. RANGE is the maximum
10264 amount that the caller wants to add without inducing overflow
10265 and ALIGN is the known alignment of the value in bytes. */
10266
10267static bfd_boolean
10268small_offset_p (unsigned int range, unsigned int align, unsigned int offbits)
10269{
10270 if (offbits == 16)
10271 {
10272 /* Accept any relocation operator if overflow isn't a concern. */
10273 if (range < align && *offset_reloc != BFD_RELOC_UNUSED)
10274 return TRUE;
10275
10276 /* These relocations are guaranteed not to overflow in correct links. */
10277 if (*offset_reloc == BFD_RELOC_MIPS_LITERAL
10278 || gprel16_reloc_p (*offset_reloc))
10279 return TRUE;
10280 }
10281 if (offset_expr.X_op == O_constant
10282 && offset_high_part (offset_expr.X_add_number, offbits) == 0
10283 && offset_high_part (offset_expr.X_add_number + range, offbits) == 0)
10284 return TRUE;
10285 return FALSE;
10286}
10287
252b5132
RH
10288/*
10289 * Build macros
10290 * This routine implements the seemingly endless macro or synthesized
10291 * instructions and addressing modes in the mips assembly language. Many
10292 * of these macros are simple and are similar to each other. These could
67c1ffbe 10293 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
10294 * this verbose method. Others are not simple macros but are more like
10295 * optimizing code generation.
10296 * One interesting optimization is when several store macros appear
67c1ffbe 10297 * consecutively that would load AT with the upper half of the same address.
2b0f3761 10298 * The ensuing load upper instructions are omitted. This implies some kind
252b5132
RH
10299 * of global optimization. We currently only optimize within a single macro.
10300 * For many of the load and store macros if the address is specified as a
10301 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
10302 * first load register 'at' with zero and use it as the base register. The
10303 * mips assembler simply uses register $zero. Just one tiny optimization
10304 * we're missing.
10305 */
10306static void
833794fc 10307macro (struct mips_cl_insn *ip, char *str)
252b5132 10308{
c0ebe874
RS
10309 const struct mips_operand_array *operands;
10310 unsigned int breg, i;
741fe287 10311 unsigned int tempreg;
252b5132 10312 int mask;
43841e91 10313 int used_at = 0;
df58fc94 10314 expressionS label_expr;
252b5132 10315 expressionS expr1;
df58fc94 10316 expressionS *ep;
252b5132
RH
10317 const char *s;
10318 const char *s2;
10319 const char *fmt;
10320 int likely = 0;
252b5132 10321 int coproc = 0;
7f3c4072 10322 int offbits = 16;
1abe91b1 10323 int call = 0;
df58fc94
RS
10324 int jals = 0;
10325 int dbl = 0;
10326 int imm = 0;
10327 int ust = 0;
10328 int lp = 0;
a45328b9 10329 int ll_sc_paired = 0;
f2ae14a1 10330 bfd_boolean large_offset;
252b5132 10331 int off;
252b5132 10332 int hold_mips_optimize;
f2ae14a1 10333 unsigned int align;
c0ebe874 10334 unsigned int op[MAX_OPERANDS];
252b5132 10335
9c2799c2 10336 gas_assert (! mips_opts.mips16);
252b5132 10337
c0ebe874
RS
10338 operands = insn_operands (ip);
10339 for (i = 0; i < MAX_OPERANDS; i++)
10340 if (operands->operand[i])
10341 op[i] = insn_extract_operand (ip, operands->operand[i]);
10342 else
10343 op[i] = -1;
10344
252b5132
RH
10345 mask = ip->insn_mo->mask;
10346
df58fc94
RS
10347 label_expr.X_op = O_constant;
10348 label_expr.X_op_symbol = NULL;
10349 label_expr.X_add_symbol = NULL;
10350 label_expr.X_add_number = 0;
10351
252b5132
RH
10352 expr1.X_op = O_constant;
10353 expr1.X_op_symbol = NULL;
10354 expr1.X_add_symbol = NULL;
10355 expr1.X_add_number = 1;
f2ae14a1 10356 align = 1;
252b5132
RH
10357
10358 switch (mask)
10359 {
10360 case M_DABS:
10361 dbl = 1;
1a0670f3 10362 /* Fall through. */
252b5132 10363 case M_ABS:
df58fc94
RS
10364 /* bgez $a0,1f
10365 move v0,$a0
10366 sub v0,$zero,$a0
10367 1:
10368 */
252b5132 10369
7d10b47d 10370 start_noreorder ();
252b5132 10371
df58fc94
RS
10372 if (mips_opts.micromips)
10373 micromips_label_expr (&label_expr);
10374 else
10375 label_expr.X_add_number = 8;
c0ebe874
RS
10376 macro_build (&label_expr, "bgez", "s,p", op[1]);
10377 if (op[0] == op[1])
a605d2b3 10378 macro_build (NULL, "nop", "");
252b5132 10379 else
c0ebe874
RS
10380 move_register (op[0], op[1]);
10381 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", op[0], 0, op[1]);
df58fc94
RS
10382 if (mips_opts.micromips)
10383 micromips_add_label ();
252b5132 10384
7d10b47d 10385 end_noreorder ();
8fc2e39e 10386 break;
252b5132
RH
10387
10388 case M_ADD_I:
10389 s = "addi";
10390 s2 = "add";
387e7624
FS
10391 if (ISA_IS_R6 (mips_opts.isa))
10392 goto do_addi_i;
10393 else
10394 goto do_addi;
252b5132
RH
10395 case M_ADDU_I:
10396 s = "addiu";
10397 s2 = "addu";
10398 goto do_addi;
10399 case M_DADD_I:
10400 dbl = 1;
10401 s = "daddi";
10402 s2 = "dadd";
387e7624 10403 if (!mips_opts.micromips && !ISA_IS_R6 (mips_opts.isa))
df58fc94 10404 goto do_addi;
b0e6f033 10405 if (imm_expr.X_add_number >= -0x200
387e7624
FS
10406 && imm_expr.X_add_number < 0x200
10407 && !ISA_IS_R6 (mips_opts.isa))
df58fc94 10408 {
b0e6f033
RS
10409 macro_build (NULL, s, "t,r,.", op[0], op[1],
10410 (int) imm_expr.X_add_number);
df58fc94
RS
10411 break;
10412 }
10413 goto do_addi_i;
252b5132
RH
10414 case M_DADDU_I:
10415 dbl = 1;
10416 s = "daddiu";
10417 s2 = "daddu";
10418 do_addi:
b0e6f033 10419 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
10420 && imm_expr.X_add_number < 0x8000)
10421 {
c0ebe874 10422 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 10423 break;
252b5132 10424 }
df58fc94 10425 do_addi_i:
8fc2e39e 10426 used_at = 1;
67c0d1eb 10427 load_register (AT, &imm_expr, dbl);
c0ebe874 10428 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
10429 break;
10430
10431 case M_AND_I:
10432 s = "andi";
10433 s2 = "and";
10434 goto do_bit;
10435 case M_OR_I:
10436 s = "ori";
10437 s2 = "or";
10438 goto do_bit;
10439 case M_NOR_I:
10440 s = "";
10441 s2 = "nor";
10442 goto do_bit;
10443 case M_XOR_I:
10444 s = "xori";
10445 s2 = "xor";
10446 do_bit:
b0e6f033 10447 if (imm_expr.X_add_number >= 0
252b5132
RH
10448 && imm_expr.X_add_number < 0x10000)
10449 {
10450 if (mask != M_NOR_I)
c0ebe874 10451 macro_build (&imm_expr, s, "t,r,i", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
10452 else
10453 {
67c0d1eb 10454 macro_build (&imm_expr, "ori", "t,r,i",
c0ebe874
RS
10455 op[0], op[1], BFD_RELOC_LO16);
10456 macro_build (NULL, "nor", "d,v,t", op[0], op[0], 0);
252b5132 10457 }
8fc2e39e 10458 break;
252b5132
RH
10459 }
10460
8fc2e39e 10461 used_at = 1;
bad1aba3 10462 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 10463 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
10464 break;
10465
8b082fb1
TS
10466 case M_BALIGN:
10467 switch (imm_expr.X_add_number)
10468 {
10469 case 0:
10470 macro_build (NULL, "nop", "");
10471 break;
10472 case 2:
c0ebe874 10473 macro_build (NULL, "packrl.ph", "d,s,t", op[0], op[0], op[1]);
8b082fb1 10474 break;
03f66e8a
MR
10475 case 1:
10476 case 3:
c0ebe874 10477 macro_build (NULL, "balign", "t,s,2", op[0], op[1],
90ecf173 10478 (int) imm_expr.X_add_number);
8b082fb1 10479 break;
03f66e8a
MR
10480 default:
10481 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
10482 (unsigned long) imm_expr.X_add_number);
10483 break;
8b082fb1
TS
10484 }
10485 break;
10486
df58fc94
RS
10487 case M_BC1FL:
10488 case M_BC1TL:
10489 case M_BC2FL:
10490 case M_BC2TL:
10491 gas_assert (mips_opts.micromips);
10492 macro_build_branch_ccl (mask, &offset_expr,
10493 EXTRACT_OPERAND (1, BCC, *ip));
10494 break;
10495
252b5132 10496 case M_BEQ_I:
252b5132 10497 case M_BEQL_I:
252b5132 10498 case M_BNE_I:
252b5132 10499 case M_BNEL_I:
b0e6f033 10500 if (imm_expr.X_add_number == 0)
c0ebe874 10501 op[1] = 0;
df58fc94 10502 else
252b5132 10503 {
c0ebe874 10504 op[1] = AT;
df58fc94 10505 used_at = 1;
bad1aba3 10506 load_register (op[1], &imm_expr, GPR_SIZE == 64);
252b5132 10507 }
df58fc94
RS
10508 /* Fall through. */
10509 case M_BEQL:
10510 case M_BNEL:
c0ebe874 10511 macro_build_branch_rsrt (mask, &offset_expr, op[0], op[1]);
252b5132
RH
10512 break;
10513
10514 case M_BGEL:
10515 likely = 1;
1a0670f3 10516 /* Fall through. */
252b5132 10517 case M_BGE:
c0ebe874
RS
10518 if (op[1] == 0)
10519 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[0]);
10520 else if (op[0] == 0)
10521 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[1]);
df58fc94 10522 else
252b5132 10523 {
df58fc94 10524 used_at = 1;
c0ebe874 10525 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10526 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10527 &offset_expr, AT, ZERO);
252b5132 10528 }
df58fc94
RS
10529 break;
10530
10531 case M_BGEZL:
10532 case M_BGEZALL:
10533 case M_BGTZL:
10534 case M_BLEZL:
10535 case M_BLTZL:
10536 case M_BLTZALL:
c0ebe874 10537 macro_build_branch_rs (mask, &offset_expr, op[0]);
252b5132
RH
10538 break;
10539
10540 case M_BGTL_I:
10541 likely = 1;
1a0670f3 10542 /* Fall through. */
252b5132 10543 case M_BGT_I:
90ecf173 10544 /* Check for > max integer. */
b0e6f033 10545 if (imm_expr.X_add_number >= GPR_SMAX)
252b5132
RH
10546 {
10547 do_false:
90ecf173 10548 /* Result is always false. */
252b5132 10549 if (! likely)
a605d2b3 10550 macro_build (NULL, "nop", "");
252b5132 10551 else
df58fc94 10552 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
8fc2e39e 10553 break;
252b5132 10554 }
f9419b05 10555 ++imm_expr.X_add_number;
6f2117ba 10556 /* Fall through. */
252b5132
RH
10557 case M_BGE_I:
10558 case M_BGEL_I:
10559 if (mask == M_BGEL_I)
10560 likely = 1;
b0e6f033 10561 if (imm_expr.X_add_number == 0)
252b5132 10562 {
df58fc94 10563 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
c0ebe874 10564 &offset_expr, op[0]);
8fc2e39e 10565 break;
252b5132 10566 }
b0e6f033 10567 if (imm_expr.X_add_number == 1)
252b5132 10568 {
df58fc94 10569 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
c0ebe874 10570 &offset_expr, op[0]);
8fc2e39e 10571 break;
252b5132 10572 }
b0e6f033 10573 if (imm_expr.X_add_number <= GPR_SMIN)
252b5132
RH
10574 {
10575 do_true:
6f2117ba 10576 /* Result is always true. */
1661c76c 10577 as_warn (_("branch %s is always true"), ip->insn_mo->name);
67c0d1eb 10578 macro_build (&offset_expr, "b", "p");
8fc2e39e 10579 break;
252b5132 10580 }
8fc2e39e 10581 used_at = 1;
c0ebe874 10582 set_at (op[0], 0);
df58fc94
RS
10583 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10584 &offset_expr, AT, ZERO);
252b5132
RH
10585 break;
10586
10587 case M_BGEUL:
10588 likely = 1;
1a0670f3 10589 /* Fall through. */
252b5132 10590 case M_BGEU:
c0ebe874 10591 if (op[1] == 0)
252b5132 10592 goto do_true;
c0ebe874 10593 else if (op[0] == 0)
df58fc94 10594 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 10595 &offset_expr, ZERO, op[1]);
df58fc94 10596 else
252b5132 10597 {
df58fc94 10598 used_at = 1;
c0ebe874 10599 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10600 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10601 &offset_expr, AT, ZERO);
252b5132 10602 }
252b5132
RH
10603 break;
10604
10605 case M_BGTUL_I:
10606 likely = 1;
1a0670f3 10607 /* Fall through. */
252b5132 10608 case M_BGTU_I:
c0ebe874 10609 if (op[0] == 0
bad1aba3 10610 || (GPR_SIZE == 32
f01dc953 10611 && imm_expr.X_add_number == -1))
252b5132 10612 goto do_false;
f9419b05 10613 ++imm_expr.X_add_number;
6f2117ba 10614 /* Fall through. */
252b5132
RH
10615 case M_BGEU_I:
10616 case M_BGEUL_I:
10617 if (mask == M_BGEUL_I)
10618 likely = 1;
b0e6f033 10619 if (imm_expr.X_add_number == 0)
252b5132 10620 goto do_true;
b0e6f033 10621 else if (imm_expr.X_add_number == 1)
df58fc94 10622 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 10623 &offset_expr, op[0], ZERO);
df58fc94 10624 else
252b5132 10625 {
df58fc94 10626 used_at = 1;
c0ebe874 10627 set_at (op[0], 1);
df58fc94
RS
10628 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10629 &offset_expr, AT, ZERO);
252b5132 10630 }
252b5132
RH
10631 break;
10632
10633 case M_BGTL:
10634 likely = 1;
1a0670f3 10635 /* Fall through. */
252b5132 10636 case M_BGT:
c0ebe874
RS
10637 if (op[1] == 0)
10638 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[0]);
10639 else if (op[0] == 0)
10640 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[1]);
df58fc94 10641 else
252b5132 10642 {
df58fc94 10643 used_at = 1;
c0ebe874 10644 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10645 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10646 &offset_expr, AT, ZERO);
252b5132 10647 }
252b5132
RH
10648 break;
10649
10650 case M_BGTUL:
10651 likely = 1;
1a0670f3 10652 /* Fall through. */
252b5132 10653 case M_BGTU:
c0ebe874 10654 if (op[1] == 0)
df58fc94 10655 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874
RS
10656 &offset_expr, op[0], ZERO);
10657 else if (op[0] == 0)
df58fc94
RS
10658 goto do_false;
10659 else
252b5132 10660 {
df58fc94 10661 used_at = 1;
c0ebe874 10662 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10663 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10664 &offset_expr, AT, ZERO);
252b5132 10665 }
252b5132
RH
10666 break;
10667
10668 case M_BLEL:
10669 likely = 1;
1a0670f3 10670 /* Fall through. */
252b5132 10671 case M_BLE:
c0ebe874
RS
10672 if (op[1] == 0)
10673 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10674 else if (op[0] == 0)
10675 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[1]);
df58fc94 10676 else
252b5132 10677 {
df58fc94 10678 used_at = 1;
c0ebe874 10679 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10680 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10681 &offset_expr, AT, ZERO);
252b5132 10682 }
252b5132
RH
10683 break;
10684
10685 case M_BLEL_I:
10686 likely = 1;
1a0670f3 10687 /* Fall through. */
252b5132 10688 case M_BLE_I:
b0e6f033 10689 if (imm_expr.X_add_number >= GPR_SMAX)
252b5132 10690 goto do_true;
f9419b05 10691 ++imm_expr.X_add_number;
6f2117ba 10692 /* Fall through. */
252b5132
RH
10693 case M_BLT_I:
10694 case M_BLTL_I:
10695 if (mask == M_BLTL_I)
10696 likely = 1;
b0e6f033 10697 if (imm_expr.X_add_number == 0)
c0ebe874 10698 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
b0e6f033 10699 else if (imm_expr.X_add_number == 1)
c0ebe874 10700 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
df58fc94 10701 else
252b5132 10702 {
df58fc94 10703 used_at = 1;
c0ebe874 10704 set_at (op[0], 0);
df58fc94
RS
10705 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10706 &offset_expr, AT, ZERO);
252b5132 10707 }
252b5132
RH
10708 break;
10709
10710 case M_BLEUL:
10711 likely = 1;
1a0670f3 10712 /* Fall through. */
252b5132 10713 case M_BLEU:
c0ebe874 10714 if (op[1] == 0)
df58fc94 10715 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874
RS
10716 &offset_expr, op[0], ZERO);
10717 else if (op[0] == 0)
df58fc94
RS
10718 goto do_true;
10719 else
252b5132 10720 {
df58fc94 10721 used_at = 1;
c0ebe874 10722 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10723 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10724 &offset_expr, AT, ZERO);
252b5132 10725 }
252b5132
RH
10726 break;
10727
10728 case M_BLEUL_I:
10729 likely = 1;
1a0670f3 10730 /* Fall through. */
252b5132 10731 case M_BLEU_I:
c0ebe874 10732 if (op[0] == 0
bad1aba3 10733 || (GPR_SIZE == 32
f01dc953 10734 && imm_expr.X_add_number == -1))
252b5132 10735 goto do_true;
f9419b05 10736 ++imm_expr.X_add_number;
6f2117ba 10737 /* Fall through. */
252b5132
RH
10738 case M_BLTU_I:
10739 case M_BLTUL_I:
10740 if (mask == M_BLTUL_I)
10741 likely = 1;
b0e6f033 10742 if (imm_expr.X_add_number == 0)
252b5132 10743 goto do_false;
b0e6f033 10744 else if (imm_expr.X_add_number == 1)
df58fc94 10745 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 10746 &offset_expr, op[0], ZERO);
df58fc94 10747 else
252b5132 10748 {
df58fc94 10749 used_at = 1;
c0ebe874 10750 set_at (op[0], 1);
df58fc94
RS
10751 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10752 &offset_expr, AT, ZERO);
252b5132 10753 }
252b5132
RH
10754 break;
10755
10756 case M_BLTL:
10757 likely = 1;
1a0670f3 10758 /* Fall through. */
252b5132 10759 case M_BLT:
c0ebe874
RS
10760 if (op[1] == 0)
10761 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10762 else if (op[0] == 0)
10763 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[1]);
df58fc94 10764 else
252b5132 10765 {
df58fc94 10766 used_at = 1;
c0ebe874 10767 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10768 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10769 &offset_expr, AT, ZERO);
252b5132 10770 }
252b5132
RH
10771 break;
10772
10773 case M_BLTUL:
10774 likely = 1;
1a0670f3 10775 /* Fall through. */
252b5132 10776 case M_BLTU:
c0ebe874 10777 if (op[1] == 0)
252b5132 10778 goto do_false;
c0ebe874 10779 else if (op[0] == 0)
df58fc94 10780 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 10781 &offset_expr, ZERO, op[1]);
df58fc94 10782 else
252b5132 10783 {
df58fc94 10784 used_at = 1;
c0ebe874 10785 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10786 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10787 &offset_expr, AT, ZERO);
252b5132 10788 }
252b5132
RH
10789 break;
10790
10791 case M_DDIV_3:
10792 dbl = 1;
1a0670f3 10793 /* Fall through. */
252b5132
RH
10794 case M_DIV_3:
10795 s = "mflo";
10796 goto do_div3;
10797 case M_DREM_3:
10798 dbl = 1;
1a0670f3 10799 /* Fall through. */
252b5132
RH
10800 case M_REM_3:
10801 s = "mfhi";
10802 do_div3:
c0ebe874 10803 if (op[2] == 0)
252b5132 10804 {
1661c76c 10805 as_warn (_("divide by zero"));
252b5132 10806 if (mips_trap)
df58fc94 10807 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 10808 else
df58fc94 10809 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 10810 break;
252b5132
RH
10811 }
10812
7d10b47d 10813 start_noreorder ();
252b5132
RH
10814 if (mips_trap)
10815 {
c0ebe874
RS
10816 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10817 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
252b5132
RH
10818 }
10819 else
10820 {
df58fc94
RS
10821 if (mips_opts.micromips)
10822 micromips_label_expr (&label_expr);
10823 else
10824 label_expr.X_add_number = 8;
c0ebe874
RS
10825 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10826 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
df58fc94
RS
10827 macro_build (NULL, "break", BRK_FMT, 7);
10828 if (mips_opts.micromips)
10829 micromips_add_label ();
252b5132
RH
10830 }
10831 expr1.X_add_number = -1;
8fc2e39e 10832 used_at = 1;
f6a22291 10833 load_register (AT, &expr1, dbl);
df58fc94
RS
10834 if (mips_opts.micromips)
10835 micromips_label_expr (&label_expr);
10836 else
10837 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
c0ebe874 10838 macro_build (&label_expr, "bne", "s,t,p", op[2], AT);
252b5132
RH
10839 if (dbl)
10840 {
10841 expr1.X_add_number = 1;
f6a22291 10842 load_register (AT, &expr1, dbl);
df58fc94 10843 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
252b5132
RH
10844 }
10845 else
10846 {
10847 expr1.X_add_number = 0x80000000;
df58fc94 10848 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
252b5132
RH
10849 }
10850 if (mips_trap)
10851 {
c0ebe874 10852 macro_build (NULL, "teq", TRAP_FMT, op[1], AT, 6);
252b5132
RH
10853 /* We want to close the noreorder block as soon as possible, so
10854 that later insns are available for delay slot filling. */
7d10b47d 10855 end_noreorder ();
252b5132
RH
10856 }
10857 else
10858 {
df58fc94
RS
10859 if (mips_opts.micromips)
10860 micromips_label_expr (&label_expr);
10861 else
10862 label_expr.X_add_number = 8;
c0ebe874 10863 macro_build (&label_expr, "bne", "s,t,p", op[1], AT);
a605d2b3 10864 macro_build (NULL, "nop", "");
252b5132
RH
10865
10866 /* We want to close the noreorder block as soon as possible, so
10867 that later insns are available for delay slot filling. */
7d10b47d 10868 end_noreorder ();
252b5132 10869
df58fc94 10870 macro_build (NULL, "break", BRK_FMT, 6);
252b5132 10871 }
df58fc94
RS
10872 if (mips_opts.micromips)
10873 micromips_add_label ();
c0ebe874 10874 macro_build (NULL, s, MFHL_FMT, op[0]);
252b5132
RH
10875 break;
10876
10877 case M_DIV_3I:
10878 s = "div";
10879 s2 = "mflo";
10880 goto do_divi;
10881 case M_DIVU_3I:
10882 s = "divu";
10883 s2 = "mflo";
10884 goto do_divi;
10885 case M_REM_3I:
10886 s = "div";
10887 s2 = "mfhi";
10888 goto do_divi;
10889 case M_REMU_3I:
10890 s = "divu";
10891 s2 = "mfhi";
10892 goto do_divi;
10893 case M_DDIV_3I:
10894 dbl = 1;
10895 s = "ddiv";
10896 s2 = "mflo";
10897 goto do_divi;
10898 case M_DDIVU_3I:
10899 dbl = 1;
10900 s = "ddivu";
10901 s2 = "mflo";
10902 goto do_divi;
10903 case M_DREM_3I:
10904 dbl = 1;
10905 s = "ddiv";
10906 s2 = "mfhi";
10907 goto do_divi;
10908 case M_DREMU_3I:
10909 dbl = 1;
10910 s = "ddivu";
10911 s2 = "mfhi";
10912 do_divi:
b0e6f033 10913 if (imm_expr.X_add_number == 0)
252b5132 10914 {
1661c76c 10915 as_warn (_("divide by zero"));
252b5132 10916 if (mips_trap)
df58fc94 10917 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 10918 else
df58fc94 10919 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 10920 break;
252b5132 10921 }
b0e6f033 10922 if (imm_expr.X_add_number == 1)
252b5132
RH
10923 {
10924 if (strcmp (s2, "mflo") == 0)
c0ebe874 10925 move_register (op[0], op[1]);
252b5132 10926 else
c0ebe874 10927 move_register (op[0], ZERO);
8fc2e39e 10928 break;
252b5132 10929 }
b0e6f033 10930 if (imm_expr.X_add_number == -1 && s[strlen (s) - 1] != 'u')
252b5132
RH
10931 {
10932 if (strcmp (s2, "mflo") == 0)
c0ebe874 10933 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", op[0], op[1]);
252b5132 10934 else
c0ebe874 10935 move_register (op[0], ZERO);
8fc2e39e 10936 break;
252b5132
RH
10937 }
10938
8fc2e39e 10939 used_at = 1;
67c0d1eb 10940 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
10941 macro_build (NULL, s, "z,s,t", op[1], AT);
10942 macro_build (NULL, s2, MFHL_FMT, op[0]);
252b5132
RH
10943 break;
10944
10945 case M_DIVU_3:
10946 s = "divu";
10947 s2 = "mflo";
10948 goto do_divu3;
10949 case M_REMU_3:
10950 s = "divu";
10951 s2 = "mfhi";
10952 goto do_divu3;
10953 case M_DDIVU_3:
10954 s = "ddivu";
10955 s2 = "mflo";
10956 goto do_divu3;
10957 case M_DREMU_3:
10958 s = "ddivu";
10959 s2 = "mfhi";
10960 do_divu3:
7d10b47d 10961 start_noreorder ();
252b5132
RH
10962 if (mips_trap)
10963 {
c0ebe874
RS
10964 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10965 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
10966 /* We want to close the noreorder block as soon as possible, so
10967 that later insns are available for delay slot filling. */
7d10b47d 10968 end_noreorder ();
252b5132
RH
10969 }
10970 else
10971 {
df58fc94
RS
10972 if (mips_opts.micromips)
10973 micromips_label_expr (&label_expr);
10974 else
10975 label_expr.X_add_number = 8;
c0ebe874
RS
10976 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10977 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
10978
10979 /* We want to close the noreorder block as soon as possible, so
10980 that later insns are available for delay slot filling. */
7d10b47d 10981 end_noreorder ();
df58fc94
RS
10982 macro_build (NULL, "break", BRK_FMT, 7);
10983 if (mips_opts.micromips)
10984 micromips_add_label ();
252b5132 10985 }
c0ebe874 10986 macro_build (NULL, s2, MFHL_FMT, op[0]);
8fc2e39e 10987 break;
252b5132 10988
1abe91b1
MR
10989 case M_DLCA_AB:
10990 dbl = 1;
1a0670f3 10991 /* Fall through. */
1abe91b1
MR
10992 case M_LCA_AB:
10993 call = 1;
10994 goto do_la;
252b5132
RH
10995 case M_DLA_AB:
10996 dbl = 1;
1a0670f3 10997 /* Fall through. */
252b5132 10998 case M_LA_AB:
1abe91b1 10999 do_la:
252b5132
RH
11000 /* Load the address of a symbol into a register. If breg is not
11001 zero, we then add a base register to it. */
11002
c0ebe874 11003 breg = op[2];
bad1aba3 11004 if (dbl && GPR_SIZE == 32)
ece794d9
MF
11005 as_warn (_("dla used to load 32-bit register; recommend using la "
11006 "instead"));
3bec30a8 11007
90ecf173 11008 if (!dbl && HAVE_64BIT_OBJECTS)
ece794d9
MF
11009 as_warn (_("la used to load 64-bit address; recommend using dla "
11010 "instead"));
3bec30a8 11011
f2ae14a1 11012 if (small_offset_p (0, align, 16))
0c11417f 11013 {
c0ebe874 11014 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", op[0], breg,
f2ae14a1 11015 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8fc2e39e 11016 break;
0c11417f
MR
11017 }
11018
c0ebe874 11019 if (mips_opts.at && (op[0] == breg))
afdbd6d0
CD
11020 {
11021 tempreg = AT;
11022 used_at = 1;
11023 }
11024 else
c0ebe874 11025 tempreg = op[0];
afdbd6d0 11026
252b5132
RH
11027 if (offset_expr.X_op != O_symbol
11028 && offset_expr.X_op != O_constant)
11029 {
1661c76c 11030 as_bad (_("expression too complex"));
252b5132
RH
11031 offset_expr.X_op = O_constant;
11032 }
11033
252b5132 11034 if (offset_expr.X_op == O_constant)
aed1a261 11035 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
11036 else if (mips_pic == NO_PIC)
11037 {
d6bc6245 11038 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 11039 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
11040 Otherwise we want
11041 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
11042 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
11043 If we have a constant, we need two instructions anyhow,
d6bc6245 11044 so we may as well always use the latter form.
76b3015f 11045
6caf9ef4
TS
11046 With 64bit address space and a usable $at we want
11047 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11048 lui $at,<sym> (BFD_RELOC_HI16_S)
11049 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11050 daddiu $at,<sym> (BFD_RELOC_LO16)
11051 dsll32 $tempreg,0
11052 daddu $tempreg,$tempreg,$at
11053
11054 If $at is already in use, we use a path which is suboptimal
11055 on superscalar processors.
11056 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11057 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11058 dsll $tempreg,16
11059 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
11060 dsll $tempreg,16
11061 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
11062
11063 For GP relative symbols in 64bit address space we can use
11064 the same sequence as in 32bit address space. */
aed1a261 11065 if (HAVE_64BIT_SYMBOLS)
252b5132 11066 {
6caf9ef4
TS
11067 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
11068 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
11069 {
11070 relax_start (offset_expr.X_add_symbol);
11071 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11072 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
11073 relax_switch ();
11074 }
d6bc6245 11075
741fe287 11076 if (used_at == 0 && mips_opts.at)
98d3f06f 11077 {
df58fc94 11078 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 11079 tempreg, BFD_RELOC_MIPS_HIGHEST);
df58fc94 11080 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 11081 AT, BFD_RELOC_HI16_S);
67c0d1eb 11082 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 11083 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 11084 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 11085 AT, AT, BFD_RELOC_LO16);
df58fc94 11086 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 11087 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
11088 used_at = 1;
11089 }
11090 else
11091 {
df58fc94 11092 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 11093 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 11094 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 11095 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 11096 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 11097 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 11098 tempreg, tempreg, BFD_RELOC_HI16_S);
df58fc94 11099 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 11100 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 11101 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 11102 }
6caf9ef4
TS
11103
11104 if (mips_relax.sequence)
11105 relax_end ();
98d3f06f
KH
11106 }
11107 else
11108 {
11109 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 11110 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 11111 {
4d7206a2 11112 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11113 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11114 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 11115 relax_switch ();
98d3f06f 11116 }
6943caf0 11117 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
1661c76c 11118 as_bad (_("offset too large"));
67c0d1eb
RS
11119 macro_build_lui (&offset_expr, tempreg);
11120 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11121 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
11122 if (mips_relax.sequence)
11123 relax_end ();
98d3f06f 11124 }
252b5132 11125 }
0a44bf69 11126 else if (!mips_big_got && !HAVE_NEWABI)
252b5132 11127 {
9117d219
NC
11128 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
11129
252b5132
RH
11130 /* If this is a reference to an external symbol, and there
11131 is no constant, we want
11132 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 11133 or for lca or if tempreg is PIC_CALL_REG
9117d219 11134 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
11135 For a local symbol, we want
11136 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11137 nop
11138 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
11139
11140 If we have a small constant, and this is a reference to
11141 an external symbol, we want
11142 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11143 nop
11144 addiu $tempreg,$tempreg,<constant>
11145 For a local symbol, we want the same instruction
11146 sequence, but we output a BFD_RELOC_LO16 reloc on the
11147 addiu instruction.
11148
11149 If we have a large constant, and this is a reference to
11150 an external symbol, we want
11151 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11152 lui $at,<hiconstant>
11153 addiu $at,$at,<loconstant>
11154 addu $tempreg,$tempreg,$at
11155 For a local symbol, we want the same instruction
11156 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 11157 addiu instruction.
ed6fb7bd
SC
11158 */
11159
4d7206a2 11160 if (offset_expr.X_add_number == 0)
252b5132 11161 {
0a44bf69
RS
11162 if (mips_pic == SVR4_PIC
11163 && breg == 0
11164 && (call || tempreg == PIC_CALL_REG))
4d7206a2
RS
11165 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
11166
11167 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11168 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11169 lw_reloc_type, mips_gp_register);
4d7206a2 11170 if (breg != 0)
252b5132
RH
11171 {
11172 /* We're going to put in an addu instruction using
11173 tempreg, so we may as well insert the nop right
11174 now. */
269137b2 11175 load_delay_nop ();
252b5132 11176 }
4d7206a2 11177 relax_switch ();
67c0d1eb
RS
11178 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11179 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 11180 load_delay_nop ();
67c0d1eb
RS
11181 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11182 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 11183 relax_end ();
252b5132
RH
11184 /* FIXME: If breg == 0, and the next instruction uses
11185 $tempreg, then if this variant case is used an extra
11186 nop will be generated. */
11187 }
4d7206a2
RS
11188 else if (offset_expr.X_add_number >= -0x8000
11189 && offset_expr.X_add_number < 0x8000)
252b5132 11190 {
67c0d1eb 11191 load_got_offset (tempreg, &offset_expr);
269137b2 11192 load_delay_nop ();
67c0d1eb 11193 add_got_offset (tempreg, &offset_expr);
252b5132
RH
11194 }
11195 else
11196 {
4d7206a2
RS
11197 expr1.X_add_number = offset_expr.X_add_number;
11198 offset_expr.X_add_number =
43c0598f 11199 SEXT_16BIT (offset_expr.X_add_number);
67c0d1eb 11200 load_got_offset (tempreg, &offset_expr);
f6a22291 11201 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
11202 /* If we are going to add in a base register, and the
11203 target register and the base register are the same,
11204 then we are using AT as a temporary register. Since
11205 we want to load the constant into AT, we add our
11206 current AT (from the global offset table) and the
11207 register into the register now, and pretend we were
11208 not using a base register. */
c0ebe874 11209 if (breg == op[0])
252b5132 11210 {
269137b2 11211 load_delay_nop ();
67c0d1eb 11212 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 11213 op[0], AT, breg);
252b5132 11214 breg = 0;
c0ebe874 11215 tempreg = op[0];
252b5132 11216 }
f6a22291 11217 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
11218 used_at = 1;
11219 }
11220 }
0a44bf69 11221 else if (!mips_big_got && HAVE_NEWABI)
f5040a92 11222 {
67c0d1eb 11223 int add_breg_early = 0;
f5040a92
AO
11224
11225 /* If this is a reference to an external, and there is no
11226 constant, or local symbol (*), with or without a
11227 constant, we want
11228 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 11229 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
11230 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11231
11232 If we have a small constant, and this is a reference to
11233 an external symbol, we want
11234 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
11235 addiu $tempreg,$tempreg,<constant>
11236
11237 If we have a large constant, and this is a reference to
11238 an external symbol, we want
11239 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
11240 lui $at,<hiconstant>
11241 addiu $at,$at,<loconstant>
11242 addu $tempreg,$tempreg,$at
11243
11244 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
11245 local symbols, even though it introduces an additional
11246 instruction. */
11247
f5040a92
AO
11248 if (offset_expr.X_add_number)
11249 {
4d7206a2 11250 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
11251 offset_expr.X_add_number = 0;
11252
4d7206a2 11253 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11254 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11255 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
11256
11257 if (expr1.X_add_number >= -0x8000
11258 && expr1.X_add_number < 0x8000)
11259 {
67c0d1eb
RS
11260 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11261 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 11262 }
ecd13cd3 11263 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 11264 {
c0ebe874
RS
11265 unsigned int dreg;
11266
f5040a92
AO
11267 /* If we are going to add in a base register, and the
11268 target register and the base register are the same,
11269 then we are using AT as a temporary register. Since
11270 we want to load the constant into AT, we add our
11271 current AT (from the global offset table) and the
11272 register into the register now, and pretend we were
11273 not using a base register. */
c0ebe874 11274 if (breg != op[0])
f5040a92
AO
11275 dreg = tempreg;
11276 else
11277 {
9c2799c2 11278 gas_assert (tempreg == AT);
67c0d1eb 11279 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11280 op[0], AT, breg);
11281 dreg = op[0];
67c0d1eb 11282 add_breg_early = 1;
f5040a92
AO
11283 }
11284
f6a22291 11285 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 11286 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11287 dreg, dreg, AT);
f5040a92 11288
f5040a92
AO
11289 used_at = 1;
11290 }
11291 else
11292 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11293
4d7206a2 11294 relax_switch ();
f5040a92
AO
11295 offset_expr.X_add_number = expr1.X_add_number;
11296
67c0d1eb
RS
11297 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11298 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11299 if (add_breg_early)
f5040a92 11300 {
67c0d1eb 11301 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 11302 op[0], tempreg, breg);
f5040a92 11303 breg = 0;
c0ebe874 11304 tempreg = op[0];
f5040a92 11305 }
4d7206a2 11306 relax_end ();
f5040a92 11307 }
4d7206a2 11308 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 11309 {
4d7206a2 11310 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11311 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11312 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 11313 relax_switch ();
67c0d1eb
RS
11314 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11315 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 11316 relax_end ();
f5040a92 11317 }
4d7206a2 11318 else
f5040a92 11319 {
67c0d1eb
RS
11320 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11321 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
11322 }
11323 }
0a44bf69 11324 else if (mips_big_got && !HAVE_NEWABI)
252b5132 11325 {
67c0d1eb 11326 int gpdelay;
9117d219
NC
11327 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11328 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 11329 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
11330
11331 /* This is the large GOT case. If this is a reference to an
11332 external symbol, and there is no constant, we want
11333 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11334 addu $tempreg,$tempreg,$gp
11335 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 11336 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
11337 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11338 addu $tempreg,$tempreg,$gp
11339 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
11340 For a local symbol, we want
11341 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11342 nop
11343 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
11344
11345 If we have a small constant, and this is a reference to
11346 an external symbol, we want
11347 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11348 addu $tempreg,$tempreg,$gp
11349 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11350 nop
11351 addiu $tempreg,$tempreg,<constant>
11352 For a local symbol, we want
11353 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11354 nop
11355 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
11356
11357 If we have a large constant, and this is a reference to
11358 an external symbol, we want
11359 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11360 addu $tempreg,$tempreg,$gp
11361 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11362 lui $at,<hiconstant>
11363 addiu $at,$at,<loconstant>
11364 addu $tempreg,$tempreg,$at
11365 For a local symbol, we want
11366 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11367 lui $at,<hiconstant>
11368 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
11369 addu $tempreg,$tempreg,$at
f5040a92 11370 */
438c16b8 11371
252b5132
RH
11372 expr1.X_add_number = offset_expr.X_add_number;
11373 offset_expr.X_add_number = 0;
4d7206a2 11374 relax_start (offset_expr.X_add_symbol);
67c0d1eb 11375 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
11376 if (expr1.X_add_number == 0 && breg == 0
11377 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
11378 {
11379 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11380 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11381 }
df58fc94 11382 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 11383 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11384 tempreg, tempreg, mips_gp_register);
67c0d1eb 11385 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 11386 tempreg, lw_reloc_type, tempreg);
252b5132
RH
11387 if (expr1.X_add_number == 0)
11388 {
67c0d1eb 11389 if (breg != 0)
252b5132
RH
11390 {
11391 /* We're going to put in an addu instruction using
11392 tempreg, so we may as well insert the nop right
11393 now. */
269137b2 11394 load_delay_nop ();
252b5132 11395 }
252b5132
RH
11396 }
11397 else if (expr1.X_add_number >= -0x8000
11398 && expr1.X_add_number < 0x8000)
11399 {
269137b2 11400 load_delay_nop ();
67c0d1eb 11401 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11402 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
11403 }
11404 else
11405 {
c0ebe874
RS
11406 unsigned int dreg;
11407
252b5132
RH
11408 /* If we are going to add in a base register, and the
11409 target register and the base register are the same,
11410 then we are using AT as a temporary register. Since
11411 we want to load the constant into AT, we add our
11412 current AT (from the global offset table) and the
11413 register into the register now, and pretend we were
11414 not using a base register. */
c0ebe874 11415 if (breg != op[0])
67c0d1eb 11416 dreg = tempreg;
252b5132
RH
11417 else
11418 {
9c2799c2 11419 gas_assert (tempreg == AT);
269137b2 11420 load_delay_nop ();
67c0d1eb 11421 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11422 op[0], AT, breg);
11423 dreg = op[0];
252b5132
RH
11424 }
11425
f6a22291 11426 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 11427 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 11428
252b5132
RH
11429 used_at = 1;
11430 }
43c0598f 11431 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
4d7206a2 11432 relax_switch ();
252b5132 11433
67c0d1eb 11434 if (gpdelay)
252b5132
RH
11435 {
11436 /* This is needed because this instruction uses $gp, but
f5040a92 11437 the first instruction on the main stream does not. */
67c0d1eb 11438 macro_build (NULL, "nop", "");
252b5132 11439 }
ed6fb7bd 11440
67c0d1eb
RS
11441 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11442 local_reloc_type, mips_gp_register);
f5040a92 11443 if (expr1.X_add_number >= -0x8000
252b5132
RH
11444 && expr1.X_add_number < 0x8000)
11445 {
269137b2 11446 load_delay_nop ();
67c0d1eb
RS
11447 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11448 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 11449 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
11450 register, the external symbol case ended with a load,
11451 so if the symbol turns out to not be external, and
11452 the next instruction uses tempreg, an unnecessary nop
11453 will be inserted. */
252b5132
RH
11454 }
11455 else
11456 {
c0ebe874 11457 if (breg == op[0])
252b5132
RH
11458 {
11459 /* We must add in the base register now, as in the
f5040a92 11460 external symbol case. */
9c2799c2 11461 gas_assert (tempreg == AT);
269137b2 11462 load_delay_nop ();
67c0d1eb 11463 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11464 op[0], AT, breg);
11465 tempreg = op[0];
252b5132 11466 /* We set breg to 0 because we have arranged to add
f5040a92 11467 it in in both cases. */
252b5132
RH
11468 breg = 0;
11469 }
11470
67c0d1eb
RS
11471 macro_build_lui (&expr1, AT);
11472 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11473 AT, AT, BFD_RELOC_LO16);
67c0d1eb 11474 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11475 tempreg, tempreg, AT);
8fc2e39e 11476 used_at = 1;
252b5132 11477 }
4d7206a2 11478 relax_end ();
252b5132 11479 }
0a44bf69 11480 else if (mips_big_got && HAVE_NEWABI)
f5040a92 11481 {
f5040a92
AO
11482 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11483 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 11484 int add_breg_early = 0;
f5040a92
AO
11485
11486 /* This is the large GOT case. If this is a reference to an
11487 external symbol, and there is no constant, we want
11488 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11489 add $tempreg,$tempreg,$gp
11490 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 11491 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
11492 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11493 add $tempreg,$tempreg,$gp
11494 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
11495
11496 If we have a small constant, and this is a reference to
11497 an external symbol, we want
11498 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11499 add $tempreg,$tempreg,$gp
11500 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11501 addi $tempreg,$tempreg,<constant>
11502
11503 If we have a large constant, and this is a reference to
11504 an external symbol, we want
11505 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11506 addu $tempreg,$tempreg,$gp
11507 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11508 lui $at,<hiconstant>
11509 addi $at,$at,<loconstant>
11510 add $tempreg,$tempreg,$at
11511
11512 If we have NewABI, and we know it's a local symbol, we want
11513 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
11514 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
11515 otherwise we have to resort to GOT_HI16/GOT_LO16. */
11516
4d7206a2 11517 relax_start (offset_expr.X_add_symbol);
f5040a92 11518
4d7206a2 11519 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
11520 offset_expr.X_add_number = 0;
11521
1abe91b1
MR
11522 if (expr1.X_add_number == 0 && breg == 0
11523 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
11524 {
11525 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11526 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11527 }
df58fc94 11528 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 11529 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11530 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
11531 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11532 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
11533
11534 if (expr1.X_add_number == 0)
4d7206a2 11535 ;
f5040a92
AO
11536 else if (expr1.X_add_number >= -0x8000
11537 && expr1.X_add_number < 0x8000)
11538 {
67c0d1eb 11539 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11540 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 11541 }
ecd13cd3 11542 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 11543 {
c0ebe874
RS
11544 unsigned int dreg;
11545
f5040a92
AO
11546 /* If we are going to add in a base register, and the
11547 target register and the base register are the same,
11548 then we are using AT as a temporary register. Since
11549 we want to load the constant into AT, we add our
11550 current AT (from the global offset table) and the
11551 register into the register now, and pretend we were
11552 not using a base register. */
c0ebe874 11553 if (breg != op[0])
f5040a92
AO
11554 dreg = tempreg;
11555 else
11556 {
9c2799c2 11557 gas_assert (tempreg == AT);
67c0d1eb 11558 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11559 op[0], AT, breg);
11560 dreg = op[0];
67c0d1eb 11561 add_breg_early = 1;
f5040a92
AO
11562 }
11563
f6a22291 11564 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 11565 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 11566
f5040a92
AO
11567 used_at = 1;
11568 }
11569 else
11570 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11571
4d7206a2 11572 relax_switch ();
f5040a92 11573 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
11574 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11575 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
11576 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11577 tempreg, BFD_RELOC_MIPS_GOT_OFST);
11578 if (add_breg_early)
f5040a92 11579 {
67c0d1eb 11580 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 11581 op[0], tempreg, breg);
f5040a92 11582 breg = 0;
c0ebe874 11583 tempreg = op[0];
f5040a92 11584 }
4d7206a2 11585 relax_end ();
f5040a92 11586 }
252b5132
RH
11587 else
11588 abort ();
11589
11590 if (breg != 0)
c0ebe874 11591 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", op[0], tempreg, breg);
252b5132
RH
11592 break;
11593
52b6b6b9 11594 case M_MSGSND:
df58fc94 11595 gas_assert (!mips_opts.micromips);
c0ebe874 11596 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x01);
c7af4273 11597 break;
52b6b6b9
JM
11598
11599 case M_MSGLD:
df58fc94 11600 gas_assert (!mips_opts.micromips);
c8276761 11601 macro_build (NULL, "c2", "C", 0x02);
c7af4273 11602 break;
52b6b6b9
JM
11603
11604 case M_MSGLD_T:
df58fc94 11605 gas_assert (!mips_opts.micromips);
c0ebe874 11606 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x02);
c7af4273 11607 break;
52b6b6b9
JM
11608
11609 case M_MSGWAIT:
df58fc94 11610 gas_assert (!mips_opts.micromips);
52b6b6b9 11611 macro_build (NULL, "c2", "C", 3);
c7af4273 11612 break;
52b6b6b9
JM
11613
11614 case M_MSGWAIT_T:
df58fc94 11615 gas_assert (!mips_opts.micromips);
c0ebe874 11616 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x03);
c7af4273 11617 break;
52b6b6b9 11618
252b5132
RH
11619 case M_J_A:
11620 /* The j instruction may not be used in PIC code, since it
11621 requires an absolute address. We convert it to a b
11622 instruction. */
11623 if (mips_pic == NO_PIC)
67c0d1eb 11624 macro_build (&offset_expr, "j", "a");
252b5132 11625 else
67c0d1eb 11626 macro_build (&offset_expr, "b", "p");
8fc2e39e 11627 break;
252b5132
RH
11628
11629 /* The jal instructions must be handled as macros because when
11630 generating PIC code they expand to multi-instruction
11631 sequences. Normally they are simple instructions. */
df58fc94 11632 case M_JALS_1:
c0ebe874
RS
11633 op[1] = op[0];
11634 op[0] = RA;
df58fc94
RS
11635 /* Fall through. */
11636 case M_JALS_2:
11637 gas_assert (mips_opts.micromips);
833794fc
MR
11638 if (mips_opts.insn32)
11639 {
1661c76c 11640 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
833794fc
MR
11641 break;
11642 }
df58fc94
RS
11643 jals = 1;
11644 goto jal;
252b5132 11645 case M_JAL_1:
c0ebe874
RS
11646 op[1] = op[0];
11647 op[0] = RA;
252b5132
RH
11648 /* Fall through. */
11649 case M_JAL_2:
df58fc94 11650 jal:
3e722fb5 11651 if (mips_pic == NO_PIC)
df58fc94
RS
11652 {
11653 s = jals ? "jalrs" : "jalr";
e64af278 11654 if (mips_opts.micromips
833794fc 11655 && !mips_opts.insn32
c0ebe874 11656 && op[0] == RA
e64af278 11657 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 11658 macro_build (NULL, s, "mj", op[1]);
df58fc94 11659 else
c0ebe874 11660 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
df58fc94 11661 }
0a44bf69 11662 else
252b5132 11663 {
df58fc94
RS
11664 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
11665 && mips_cprestore_offset >= 0);
11666
c0ebe874 11667 if (op[1] != PIC_CALL_REG)
252b5132 11668 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 11669
833794fc
MR
11670 s = ((mips_opts.micromips
11671 && !mips_opts.insn32
11672 && (!mips_opts.noreorder || cprestore))
df58fc94 11673 ? "jalrs" : "jalr");
e64af278 11674 if (mips_opts.micromips
833794fc 11675 && !mips_opts.insn32
c0ebe874 11676 && op[0] == RA
e64af278 11677 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 11678 macro_build (NULL, s, "mj", op[1]);
df58fc94 11679 else
c0ebe874 11680 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
0a44bf69 11681 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
252b5132 11682 {
6478892d 11683 if (mips_cprestore_offset < 0)
1661c76c 11684 as_warn (_("no .cprestore pseudo-op used in PIC code"));
6478892d
TS
11685 else
11686 {
90ecf173 11687 if (!mips_frame_reg_valid)
7a621144 11688 {
1661c76c 11689 as_warn (_("no .frame pseudo-op used in PIC code"));
7a621144
DJ
11690 /* Quiet this warning. */
11691 mips_frame_reg_valid = 1;
11692 }
90ecf173 11693 if (!mips_cprestore_valid)
7a621144 11694 {
1661c76c 11695 as_warn (_("no .cprestore pseudo-op used in PIC code"));
7a621144
DJ
11696 /* Quiet this warning. */
11697 mips_cprestore_valid = 1;
11698 }
d3fca0b5
MR
11699 if (mips_opts.noreorder)
11700 macro_build (NULL, "nop", "");
6478892d 11701 expr1.X_add_number = mips_cprestore_offset;
134c0c8b 11702 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 11703 mips_gp_register,
256ab948
TS
11704 mips_frame_reg,
11705 HAVE_64BIT_ADDRESSES);
6478892d 11706 }
252b5132
RH
11707 }
11708 }
252b5132 11709
8fc2e39e 11710 break;
252b5132 11711
df58fc94
RS
11712 case M_JALS_A:
11713 gas_assert (mips_opts.micromips);
833794fc
MR
11714 if (mips_opts.insn32)
11715 {
1661c76c 11716 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
833794fc
MR
11717 break;
11718 }
df58fc94
RS
11719 jals = 1;
11720 /* Fall through. */
252b5132
RH
11721 case M_JAL_A:
11722 if (mips_pic == NO_PIC)
df58fc94 11723 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
252b5132
RH
11724 else if (mips_pic == SVR4_PIC)
11725 {
11726 /* If this is a reference to an external symbol, and we are
11727 using a small GOT, we want
11728 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11729 nop
f9419b05 11730 jalr $ra,$25
252b5132
RH
11731 nop
11732 lw $gp,cprestore($sp)
11733 The cprestore value is set using the .cprestore
11734 pseudo-op. If we are using a big GOT, we want
11735 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11736 addu $25,$25,$gp
11737 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
11738 nop
f9419b05 11739 jalr $ra,$25
252b5132
RH
11740 nop
11741 lw $gp,cprestore($sp)
11742 If the symbol is not external, we want
11743 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11744 nop
11745 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 11746 jalr $ra,$25
252b5132 11747 nop
438c16b8 11748 lw $gp,cprestore($sp)
f5040a92
AO
11749
11750 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
11751 sequences above, minus nops, unless the symbol is local,
11752 which enables us to use GOT_PAGE/GOT_OFST (big got) or
11753 GOT_DISP. */
438c16b8 11754 if (HAVE_NEWABI)
252b5132 11755 {
90ecf173 11756 if (!mips_big_got)
f5040a92 11757 {
4d7206a2 11758 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11759 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11760 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 11761 mips_gp_register);
4d7206a2 11762 relax_switch ();
67c0d1eb
RS
11763 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11764 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
11765 mips_gp_register);
11766 relax_end ();
f5040a92
AO
11767 }
11768 else
11769 {
4d7206a2 11770 relax_start (offset_expr.X_add_symbol);
df58fc94 11771 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
11772 BFD_RELOC_MIPS_CALL_HI16);
11773 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11774 PIC_CALL_REG, mips_gp_register);
11775 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11776 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11777 PIC_CALL_REG);
4d7206a2 11778 relax_switch ();
67c0d1eb
RS
11779 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11780 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
11781 mips_gp_register);
11782 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11783 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 11784 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 11785 relax_end ();
f5040a92 11786 }
684022ea 11787
df58fc94 11788 macro_build_jalr (&offset_expr, 0);
252b5132
RH
11789 }
11790 else
11791 {
4d7206a2 11792 relax_start (offset_expr.X_add_symbol);
90ecf173 11793 if (!mips_big_got)
438c16b8 11794 {
67c0d1eb
RS
11795 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11796 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 11797 mips_gp_register);
269137b2 11798 load_delay_nop ();
4d7206a2 11799 relax_switch ();
438c16b8 11800 }
252b5132 11801 else
252b5132 11802 {
67c0d1eb
RS
11803 int gpdelay;
11804
11805 gpdelay = reg_needs_delay (mips_gp_register);
df58fc94 11806 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
11807 BFD_RELOC_MIPS_CALL_HI16);
11808 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11809 PIC_CALL_REG, mips_gp_register);
11810 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11811 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11812 PIC_CALL_REG);
269137b2 11813 load_delay_nop ();
4d7206a2 11814 relax_switch ();
67c0d1eb
RS
11815 if (gpdelay)
11816 macro_build (NULL, "nop", "");
252b5132 11817 }
67c0d1eb
RS
11818 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11819 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 11820 mips_gp_register);
269137b2 11821 load_delay_nop ();
67c0d1eb
RS
11822 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11823 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 11824 relax_end ();
df58fc94 11825 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
438c16b8 11826
6478892d 11827 if (mips_cprestore_offset < 0)
1661c76c 11828 as_warn (_("no .cprestore pseudo-op used in PIC code"));
6478892d
TS
11829 else
11830 {
90ecf173 11831 if (!mips_frame_reg_valid)
7a621144 11832 {
1661c76c 11833 as_warn (_("no .frame pseudo-op used in PIC code"));
7a621144
DJ
11834 /* Quiet this warning. */
11835 mips_frame_reg_valid = 1;
11836 }
90ecf173 11837 if (!mips_cprestore_valid)
7a621144 11838 {
1661c76c 11839 as_warn (_("no .cprestore pseudo-op used in PIC code"));
7a621144
DJ
11840 /* Quiet this warning. */
11841 mips_cprestore_valid = 1;
11842 }
6478892d 11843 if (mips_opts.noreorder)
67c0d1eb 11844 macro_build (NULL, "nop", "");
6478892d 11845 expr1.X_add_number = mips_cprestore_offset;
134c0c8b 11846 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 11847 mips_gp_register,
256ab948
TS
11848 mips_frame_reg,
11849 HAVE_64BIT_ADDRESSES);
6478892d 11850 }
252b5132
RH
11851 }
11852 }
0a44bf69 11853 else if (mips_pic == VXWORKS_PIC)
1661c76c 11854 as_bad (_("non-PIC jump used in PIC library"));
252b5132
RH
11855 else
11856 abort ();
11857
8fc2e39e 11858 break;
252b5132 11859
7f3c4072 11860 case M_LBUE_AB:
7f3c4072
CM
11861 s = "lbue";
11862 fmt = "t,+j(b)";
11863 offbits = 9;
11864 goto ld_st;
11865 case M_LHUE_AB:
7f3c4072
CM
11866 s = "lhue";
11867 fmt = "t,+j(b)";
11868 offbits = 9;
11869 goto ld_st;
11870 case M_LBE_AB:
7f3c4072
CM
11871 s = "lbe";
11872 fmt = "t,+j(b)";
11873 offbits = 9;
11874 goto ld_st;
11875 case M_LHE_AB:
7f3c4072
CM
11876 s = "lhe";
11877 fmt = "t,+j(b)";
11878 offbits = 9;
11879 goto ld_st;
11880 case M_LLE_AB:
7f3c4072
CM
11881 s = "lle";
11882 fmt = "t,+j(b)";
11883 offbits = 9;
11884 goto ld_st;
11885 case M_LWE_AB:
7f3c4072
CM
11886 s = "lwe";
11887 fmt = "t,+j(b)";
11888 offbits = 9;
11889 goto ld_st;
11890 case M_LWLE_AB:
7f3c4072
CM
11891 s = "lwle";
11892 fmt = "t,+j(b)";
11893 offbits = 9;
11894 goto ld_st;
11895 case M_LWRE_AB:
7f3c4072
CM
11896 s = "lwre";
11897 fmt = "t,+j(b)";
11898 offbits = 9;
11899 goto ld_st;
11900 case M_SBE_AB:
7f3c4072
CM
11901 s = "sbe";
11902 fmt = "t,+j(b)";
11903 offbits = 9;
11904 goto ld_st;
11905 case M_SCE_AB:
7f3c4072
CM
11906 s = "sce";
11907 fmt = "t,+j(b)";
11908 offbits = 9;
11909 goto ld_st;
11910 case M_SHE_AB:
7f3c4072
CM
11911 s = "she";
11912 fmt = "t,+j(b)";
11913 offbits = 9;
11914 goto ld_st;
11915 case M_SWE_AB:
7f3c4072
CM
11916 s = "swe";
11917 fmt = "t,+j(b)";
11918 offbits = 9;
11919 goto ld_st;
11920 case M_SWLE_AB:
7f3c4072
CM
11921 s = "swle";
11922 fmt = "t,+j(b)";
11923 offbits = 9;
11924 goto ld_st;
11925 case M_SWRE_AB:
7f3c4072
CM
11926 s = "swre";
11927 fmt = "t,+j(b)";
11928 offbits = 9;
11929 goto ld_st;
dec0624d 11930 case M_ACLR_AB:
dec0624d 11931 s = "aclr";
dec0624d 11932 fmt = "\\,~(b)";
7f3c4072 11933 offbits = 12;
dec0624d
MR
11934 goto ld_st;
11935 case M_ASET_AB:
dec0624d 11936 s = "aset";
dec0624d 11937 fmt = "\\,~(b)";
7f3c4072 11938 offbits = 12;
dec0624d 11939 goto ld_st;
252b5132
RH
11940 case M_LB_AB:
11941 s = "lb";
df58fc94 11942 fmt = "t,o(b)";
252b5132
RH
11943 goto ld;
11944 case M_LBU_AB:
11945 s = "lbu";
df58fc94 11946 fmt = "t,o(b)";
252b5132
RH
11947 goto ld;
11948 case M_LH_AB:
11949 s = "lh";
df58fc94 11950 fmt = "t,o(b)";
252b5132
RH
11951 goto ld;
11952 case M_LHU_AB:
11953 s = "lhu";
df58fc94 11954 fmt = "t,o(b)";
252b5132
RH
11955 goto ld;
11956 case M_LW_AB:
11957 s = "lw";
df58fc94 11958 fmt = "t,o(b)";
252b5132
RH
11959 goto ld;
11960 case M_LWC0_AB:
df58fc94 11961 gas_assert (!mips_opts.micromips);
252b5132 11962 s = "lwc0";
df58fc94 11963 fmt = "E,o(b)";
bdaaa2e1 11964 /* Itbl support may require additional care here. */
252b5132 11965 coproc = 1;
df58fc94 11966 goto ld_st;
252b5132
RH
11967 case M_LWC1_AB:
11968 s = "lwc1";
df58fc94 11969 fmt = "T,o(b)";
bdaaa2e1 11970 /* Itbl support may require additional care here. */
252b5132 11971 coproc = 1;
df58fc94 11972 goto ld_st;
252b5132
RH
11973 case M_LWC2_AB:
11974 s = "lwc2";
df58fc94 11975 fmt = COP12_FMT;
7361da2c
AB
11976 offbits = (mips_opts.micromips ? 12
11977 : ISA_IS_R6 (mips_opts.isa) ? 11
11978 : 16);
bdaaa2e1 11979 /* Itbl support may require additional care here. */
252b5132 11980 coproc = 1;
df58fc94 11981 goto ld_st;
252b5132 11982 case M_LWC3_AB:
df58fc94 11983 gas_assert (!mips_opts.micromips);
252b5132 11984 s = "lwc3";
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_LWL_AB:
11990 s = "lwl";
df58fc94 11991 fmt = MEM12_FMT;
7f3c4072 11992 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11993 goto ld_st;
252b5132
RH
11994 case M_LWR_AB:
11995 s = "lwr";
df58fc94 11996 fmt = MEM12_FMT;
7f3c4072 11997 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11998 goto ld_st;
252b5132 11999 case M_LDC1_AB:
252b5132 12000 s = "ldc1";
df58fc94 12001 fmt = "T,o(b)";
bdaaa2e1 12002 /* Itbl support may require additional care here. */
252b5132 12003 coproc = 1;
df58fc94 12004 goto ld_st;
252b5132
RH
12005 case M_LDC2_AB:
12006 s = "ldc2";
df58fc94 12007 fmt = COP12_FMT;
7361da2c
AB
12008 offbits = (mips_opts.micromips ? 12
12009 : ISA_IS_R6 (mips_opts.isa) ? 11
12010 : 16);
bdaaa2e1 12011 /* Itbl support may require additional care here. */
252b5132 12012 coproc = 1;
df58fc94 12013 goto ld_st;
c77c0862 12014 case M_LQC2_AB:
c77c0862 12015 s = "lqc2";
14daeee3 12016 fmt = "+7,o(b)";
c77c0862
RS
12017 /* Itbl support may require additional care here. */
12018 coproc = 1;
12019 goto ld_st;
252b5132
RH
12020 case M_LDC3_AB:
12021 s = "ldc3";
df58fc94 12022 fmt = "E,o(b)";
bdaaa2e1 12023 /* Itbl support may require additional care here. */
252b5132 12024 coproc = 1;
df58fc94 12025 goto ld_st;
252b5132
RH
12026 case M_LDL_AB:
12027 s = "ldl";
df58fc94 12028 fmt = MEM12_FMT;
7f3c4072 12029 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 12030 goto ld_st;
252b5132
RH
12031 case M_LDR_AB:
12032 s = "ldr";
df58fc94 12033 fmt = MEM12_FMT;
7f3c4072 12034 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 12035 goto ld_st;
252b5132
RH
12036 case M_LL_AB:
12037 s = "ll";
7361da2c
AB
12038 fmt = LL_SC_FMT;
12039 offbits = (mips_opts.micromips ? 12
12040 : ISA_IS_R6 (mips_opts.isa) ? 9
12041 : 16);
252b5132
RH
12042 goto ld;
12043 case M_LLD_AB:
12044 s = "lld";
7361da2c
AB
12045 fmt = LL_SC_FMT;
12046 offbits = (mips_opts.micromips ? 12
12047 : ISA_IS_R6 (mips_opts.isa) ? 9
12048 : 16);
252b5132
RH
12049 goto ld;
12050 case M_LWU_AB:
12051 s = "lwu";
df58fc94 12052 fmt = MEM12_FMT;
7f3c4072 12053 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
12054 goto ld;
12055 case M_LWP_AB:
df58fc94
RS
12056 gas_assert (mips_opts.micromips);
12057 s = "lwp";
12058 fmt = "t,~(b)";
7f3c4072 12059 offbits = 12;
df58fc94
RS
12060 lp = 1;
12061 goto ld;
12062 case M_LDP_AB:
df58fc94
RS
12063 gas_assert (mips_opts.micromips);
12064 s = "ldp";
12065 fmt = "t,~(b)";
7f3c4072 12066 offbits = 12;
df58fc94
RS
12067 lp = 1;
12068 goto ld;
a45328b9
AB
12069 case M_LLDP_AB:
12070 case M_LLWP_AB:
41cee089 12071 case M_LLWPE_AB:
a45328b9
AB
12072 s = ip->insn_mo->name;
12073 fmt = "t,d,s";
12074 ll_sc_paired = 1;
12075 offbits = 0;
12076 goto ld;
df58fc94 12077 case M_LWM_AB:
df58fc94
RS
12078 gas_assert (mips_opts.micromips);
12079 s = "lwm";
12080 fmt = "n,~(b)";
7f3c4072 12081 offbits = 12;
df58fc94
RS
12082 goto ld_st;
12083 case M_LDM_AB:
df58fc94
RS
12084 gas_assert (mips_opts.micromips);
12085 s = "ldm";
12086 fmt = "n,~(b)";
7f3c4072 12087 offbits = 12;
df58fc94
RS
12088 goto ld_st;
12089
252b5132 12090 ld:
a45328b9
AB
12091 /* Try to use one the the load registers to compute the base address.
12092 We don't want to use $0 as tempreg. */
12093 if (ll_sc_paired)
12094 {
12095 if ((op[0] == ZERO && op[3] == op[1])
12096 || (op[1] == ZERO && op[3] == op[0])
12097 || (op[0] == ZERO && op[1] == ZERO))
12098 goto ld_st;
12099 else if (op[0] != op[3] && op[0] != ZERO)
12100 tempreg = op[0];
12101 else
12102 tempreg = op[1];
12103 }
252b5132 12104 else
a45328b9
AB
12105 {
12106 if (op[2] == op[0] + lp || op[0] + lp == ZERO)
12107 goto ld_st;
12108 else
12109 tempreg = op[0] + lp;
12110 }
df58fc94
RS
12111 goto ld_noat;
12112
252b5132
RH
12113 case M_SB_AB:
12114 s = "sb";
df58fc94
RS
12115 fmt = "t,o(b)";
12116 goto ld_st;
252b5132
RH
12117 case M_SH_AB:
12118 s = "sh";
df58fc94
RS
12119 fmt = "t,o(b)";
12120 goto ld_st;
252b5132
RH
12121 case M_SW_AB:
12122 s = "sw";
df58fc94
RS
12123 fmt = "t,o(b)";
12124 goto ld_st;
252b5132 12125 case M_SWC0_AB:
df58fc94 12126 gas_assert (!mips_opts.micromips);
252b5132 12127 s = "swc0";
df58fc94 12128 fmt = "E,o(b)";
bdaaa2e1 12129 /* Itbl support may require additional care here. */
252b5132 12130 coproc = 1;
df58fc94 12131 goto ld_st;
252b5132
RH
12132 case M_SWC1_AB:
12133 s = "swc1";
df58fc94 12134 fmt = "T,o(b)";
bdaaa2e1 12135 /* Itbl support may require additional care here. */
252b5132 12136 coproc = 1;
df58fc94 12137 goto ld_st;
252b5132
RH
12138 case M_SWC2_AB:
12139 s = "swc2";
df58fc94 12140 fmt = COP12_FMT;
7361da2c
AB
12141 offbits = (mips_opts.micromips ? 12
12142 : ISA_IS_R6 (mips_opts.isa) ? 11
12143 : 16);
bdaaa2e1 12144 /* Itbl support may require additional care here. */
252b5132 12145 coproc = 1;
df58fc94 12146 goto ld_st;
252b5132 12147 case M_SWC3_AB:
df58fc94 12148 gas_assert (!mips_opts.micromips);
252b5132 12149 s = "swc3";
df58fc94 12150 fmt = "E,o(b)";
bdaaa2e1 12151 /* Itbl support may require additional care here. */
252b5132 12152 coproc = 1;
df58fc94 12153 goto ld_st;
252b5132
RH
12154 case M_SWL_AB:
12155 s = "swl";
df58fc94 12156 fmt = MEM12_FMT;
7f3c4072 12157 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 12158 goto ld_st;
252b5132
RH
12159 case M_SWR_AB:
12160 s = "swr";
df58fc94 12161 fmt = MEM12_FMT;
7f3c4072 12162 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 12163 goto ld_st;
252b5132
RH
12164 case M_SC_AB:
12165 s = "sc";
7361da2c
AB
12166 fmt = LL_SC_FMT;
12167 offbits = (mips_opts.micromips ? 12
12168 : ISA_IS_R6 (mips_opts.isa) ? 9
12169 : 16);
df58fc94 12170 goto ld_st;
252b5132
RH
12171 case M_SCD_AB:
12172 s = "scd";
7361da2c
AB
12173 fmt = LL_SC_FMT;
12174 offbits = (mips_opts.micromips ? 12
12175 : ISA_IS_R6 (mips_opts.isa) ? 9
12176 : 16);
df58fc94 12177 goto ld_st;
a45328b9
AB
12178 case M_SCDP_AB:
12179 case M_SCWP_AB:
41cee089 12180 case M_SCWPE_AB:
a45328b9
AB
12181 s = ip->insn_mo->name;
12182 fmt = "t,d,s";
12183 ll_sc_paired = 1;
12184 offbits = 0;
12185 goto ld_st;
d43b4baf
TS
12186 case M_CACHE_AB:
12187 s = "cache";
7361da2c
AB
12188 fmt = (mips_opts.micromips ? "k,~(b)"
12189 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
12190 : "k,o(b)");
12191 offbits = (mips_opts.micromips ? 12
12192 : ISA_IS_R6 (mips_opts.isa) ? 9
12193 : 16);
7f3c4072
CM
12194 goto ld_st;
12195 case M_CACHEE_AB:
7f3c4072
CM
12196 s = "cachee";
12197 fmt = "k,+j(b)";
12198 offbits = 9;
df58fc94 12199 goto ld_st;
3eebd5eb
MR
12200 case M_PREF_AB:
12201 s = "pref";
7361da2c
AB
12202 fmt = (mips_opts.micromips ? "k,~(b)"
12203 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
12204 : "k,o(b)");
12205 offbits = (mips_opts.micromips ? 12
12206 : ISA_IS_R6 (mips_opts.isa) ? 9
12207 : 16);
7f3c4072
CM
12208 goto ld_st;
12209 case M_PREFE_AB:
7f3c4072
CM
12210 s = "prefe";
12211 fmt = "k,+j(b)";
12212 offbits = 9;
df58fc94 12213 goto ld_st;
252b5132 12214 case M_SDC1_AB:
252b5132 12215 s = "sdc1";
df58fc94 12216 fmt = "T,o(b)";
252b5132 12217 coproc = 1;
bdaaa2e1 12218 /* Itbl support may require additional care here. */
df58fc94 12219 goto ld_st;
252b5132
RH
12220 case M_SDC2_AB:
12221 s = "sdc2";
df58fc94 12222 fmt = COP12_FMT;
7361da2c
AB
12223 offbits = (mips_opts.micromips ? 12
12224 : ISA_IS_R6 (mips_opts.isa) ? 11
12225 : 16);
c77c0862
RS
12226 /* Itbl support may require additional care here. */
12227 coproc = 1;
12228 goto ld_st;
12229 case M_SQC2_AB:
c77c0862 12230 s = "sqc2";
14daeee3 12231 fmt = "+7,o(b)";
bdaaa2e1 12232 /* Itbl support may require additional care here. */
252b5132 12233 coproc = 1;
df58fc94 12234 goto ld_st;
252b5132 12235 case M_SDC3_AB:
df58fc94 12236 gas_assert (!mips_opts.micromips);
252b5132 12237 s = "sdc3";
df58fc94 12238 fmt = "E,o(b)";
bdaaa2e1 12239 /* Itbl support may require additional care here. */
252b5132 12240 coproc = 1;
df58fc94 12241 goto ld_st;
252b5132
RH
12242 case M_SDL_AB:
12243 s = "sdl";
df58fc94 12244 fmt = MEM12_FMT;
7f3c4072 12245 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 12246 goto ld_st;
252b5132
RH
12247 case M_SDR_AB:
12248 s = "sdr";
df58fc94 12249 fmt = MEM12_FMT;
7f3c4072 12250 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
12251 goto ld_st;
12252 case M_SWP_AB:
df58fc94
RS
12253 gas_assert (mips_opts.micromips);
12254 s = "swp";
12255 fmt = "t,~(b)";
7f3c4072 12256 offbits = 12;
df58fc94
RS
12257 goto ld_st;
12258 case M_SDP_AB:
df58fc94
RS
12259 gas_assert (mips_opts.micromips);
12260 s = "sdp";
12261 fmt = "t,~(b)";
7f3c4072 12262 offbits = 12;
df58fc94
RS
12263 goto ld_st;
12264 case M_SWM_AB:
df58fc94
RS
12265 gas_assert (mips_opts.micromips);
12266 s = "swm";
12267 fmt = "n,~(b)";
7f3c4072 12268 offbits = 12;
df58fc94
RS
12269 goto ld_st;
12270 case M_SDM_AB:
df58fc94
RS
12271 gas_assert (mips_opts.micromips);
12272 s = "sdm";
12273 fmt = "n,~(b)";
7f3c4072 12274 offbits = 12;
df58fc94
RS
12275
12276 ld_st:
8fc2e39e 12277 tempreg = AT;
df58fc94 12278 ld_noat:
a45328b9 12279 breg = ll_sc_paired ? op[3] : op[2];
f2ae14a1
RS
12280 if (small_offset_p (0, align, 16))
12281 {
12282 /* The first case exists for M_LD_AB and M_SD_AB, which are
12283 macros for o32 but which should act like normal instructions
12284 otherwise. */
12285 if (offbits == 16)
c0ebe874 12286 macro_build (&offset_expr, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
12287 offset_reloc[1], offset_reloc[2], breg);
12288 else if (small_offset_p (0, align, offbits))
12289 {
12290 if (offbits == 0)
a45328b9
AB
12291 {
12292 if (ll_sc_paired)
12293 macro_build (NULL, s, fmt, op[0], op[1], breg);
12294 else
12295 macro_build (NULL, s, fmt, op[0], breg);
12296 }
f2ae14a1 12297 else
c0ebe874 12298 macro_build (NULL, s, fmt, op[0],
c8276761 12299 (int) offset_expr.X_add_number, breg);
f2ae14a1
RS
12300 }
12301 else
12302 {
12303 if (tempreg == AT)
12304 used_at = 1;
12305 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
12306 tempreg, breg, -1, offset_reloc[0],
12307 offset_reloc[1], offset_reloc[2]);
12308 if (offbits == 0)
a45328b9
AB
12309 {
12310 if (ll_sc_paired)
12311 macro_build (NULL, s, fmt, op[0], op[1], tempreg);
12312 else
12313 macro_build (NULL, s, fmt, op[0], tempreg);
12314 }
f2ae14a1 12315 else
c0ebe874 12316 macro_build (NULL, s, fmt, op[0], 0, tempreg);
f2ae14a1
RS
12317 }
12318 break;
12319 }
12320
12321 if (tempreg == AT)
12322 used_at = 1;
12323
252b5132
RH
12324 if (offset_expr.X_op != O_constant
12325 && offset_expr.X_op != O_symbol)
12326 {
1661c76c 12327 as_bad (_("expression too complex"));
252b5132
RH
12328 offset_expr.X_op = O_constant;
12329 }
12330
2051e8c4
MR
12331 if (HAVE_32BIT_ADDRESSES
12332 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
12333 {
12334 char value [32];
12335
12336 sprintf_vma (value, offset_expr.X_add_number);
1661c76c 12337 as_bad (_("number (0x%s) larger than 32 bits"), value);
55e08f71 12338 }
2051e8c4 12339
252b5132
RH
12340 /* A constant expression in PIC code can be handled just as it
12341 is in non PIC code. */
aed1a261
RS
12342 if (offset_expr.X_op == O_constant)
12343 {
f2ae14a1
RS
12344 expr1.X_add_number = offset_high_part (offset_expr.X_add_number,
12345 offbits == 0 ? 16 : offbits);
12346 offset_expr.X_add_number -= expr1.X_add_number;
df58fc94 12347
f2ae14a1
RS
12348 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
12349 if (breg != 0)
12350 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12351 tempreg, tempreg, breg);
7f3c4072 12352 if (offbits == 0)
dd6a37e7 12353 {
f2ae14a1 12354 if (offset_expr.X_add_number != 0)
dd6a37e7 12355 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
f2ae14a1 12356 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
a45328b9
AB
12357 if (ll_sc_paired)
12358 macro_build (NULL, s, fmt, op[0], op[1], tempreg);
12359 else
12360 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 12361 }
7f3c4072 12362 else if (offbits == 16)
c0ebe874 12363 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
df58fc94 12364 else
c0ebe874 12365 macro_build (NULL, s, fmt, op[0],
c8276761 12366 (int) offset_expr.X_add_number, tempreg);
df58fc94 12367 }
7f3c4072 12368 else if (offbits != 16)
df58fc94 12369 {
7f3c4072 12370 /* The offset field is too narrow to be used for a low-part
2b0f3761 12371 relocation, so load the whole address into the auxiliary
f2ae14a1
RS
12372 register. */
12373 load_address (tempreg, &offset_expr, &used_at);
12374 if (breg != 0)
12375 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12376 tempreg, tempreg, breg);
7f3c4072 12377 if (offbits == 0)
a45328b9
AB
12378 {
12379 if (ll_sc_paired)
12380 macro_build (NULL, s, fmt, op[0], op[1], tempreg);
12381 else
12382 macro_build (NULL, s, fmt, op[0], tempreg);
12383 }
dd6a37e7 12384 else
c0ebe874 12385 macro_build (NULL, s, fmt, op[0], 0, tempreg);
aed1a261
RS
12386 }
12387 else if (mips_pic == NO_PIC)
252b5132
RH
12388 {
12389 /* If this is a reference to a GP relative symbol, and there
12390 is no base register, we want
c0ebe874 12391 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
12392 Otherwise, if there is no base register, we want
12393 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
c0ebe874 12394 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
252b5132
RH
12395 If we have a constant, we need two instructions anyhow,
12396 so we always use the latter form.
12397
12398 If we have a base register, and this is a reference to a
12399 GP relative symbol, we want
12400 addu $tempreg,$breg,$gp
c0ebe874 12401 <op> op[0],<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
12402 Otherwise we want
12403 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
12404 addu $tempreg,$tempreg,$breg
c0ebe874 12405 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 12406 With a constant we always use the latter case.
76b3015f 12407
d6bc6245
TS
12408 With 64bit address space and no base register and $at usable,
12409 we want
12410 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12411 lui $at,<sym> (BFD_RELOC_HI16_S)
12412 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12413 dsll32 $tempreg,0
12414 daddu $tempreg,$at
c0ebe874 12415 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
12416 If we have a base register, we want
12417 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12418 lui $at,<sym> (BFD_RELOC_HI16_S)
12419 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12420 daddu $at,$breg
12421 dsll32 $tempreg,0
12422 daddu $tempreg,$at
c0ebe874 12423 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
12424
12425 Without $at we can't generate the optimal path for superscalar
12426 processors here since this would require two temporary registers.
12427 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12428 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12429 dsll $tempreg,16
12430 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
12431 dsll $tempreg,16
c0ebe874 12432 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
12433 If we have a base register, we want
12434 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12435 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12436 dsll $tempreg,16
12437 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
12438 dsll $tempreg,16
12439 daddu $tempreg,$tempreg,$breg
c0ebe874 12440 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 12441
6caf9ef4 12442 For GP relative symbols in 64bit address space we can use
aed1a261
RS
12443 the same sequence as in 32bit address space. */
12444 if (HAVE_64BIT_SYMBOLS)
d6bc6245 12445 {
aed1a261 12446 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
12447 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12448 {
12449 relax_start (offset_expr.X_add_symbol);
12450 if (breg == 0)
12451 {
c0ebe874 12452 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
12453 BFD_RELOC_GPREL16, mips_gp_register);
12454 }
12455 else
12456 {
12457 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12458 tempreg, breg, mips_gp_register);
c0ebe874 12459 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
12460 BFD_RELOC_GPREL16, tempreg);
12461 }
12462 relax_switch ();
12463 }
d6bc6245 12464
741fe287 12465 if (used_at == 0 && mips_opts.at)
d6bc6245 12466 {
df58fc94 12467 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb 12468 BFD_RELOC_MIPS_HIGHEST);
df58fc94 12469 macro_build (&offset_expr, "lui", LUI_FMT, AT,
67c0d1eb
RS
12470 BFD_RELOC_HI16_S);
12471 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12472 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 12473 if (breg != 0)
67c0d1eb 12474 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
df58fc94 12475 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 12476 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
c0ebe874 12477 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16,
67c0d1eb 12478 tempreg);
d6bc6245
TS
12479 used_at = 1;
12480 }
12481 else
12482 {
df58fc94 12483 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb
RS
12484 BFD_RELOC_MIPS_HIGHEST);
12485 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12486 tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 12487 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb
RS
12488 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12489 tempreg, BFD_RELOC_HI16_S);
df58fc94 12490 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
d6bc6245 12491 if (breg != 0)
67c0d1eb 12492 macro_build (NULL, "daddu", "d,v,t",
17a2f251 12493 tempreg, tempreg, breg);
c0ebe874 12494 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12495 BFD_RELOC_LO16, tempreg);
d6bc6245 12496 }
6caf9ef4
TS
12497
12498 if (mips_relax.sequence)
12499 relax_end ();
8fc2e39e 12500 break;
d6bc6245 12501 }
256ab948 12502
252b5132
RH
12503 if (breg == 0)
12504 {
67c0d1eb 12505 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12506 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12507 {
4d7206a2 12508 relax_start (offset_expr.X_add_symbol);
c0ebe874 12509 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_GPREL16,
67c0d1eb 12510 mips_gp_register);
4d7206a2 12511 relax_switch ();
252b5132 12512 }
67c0d1eb 12513 macro_build_lui (&offset_expr, tempreg);
c0ebe874 12514 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12515 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
12516 if (mips_relax.sequence)
12517 relax_end ();
252b5132
RH
12518 }
12519 else
12520 {
67c0d1eb 12521 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12522 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12523 {
4d7206a2 12524 relax_start (offset_expr.X_add_symbol);
67c0d1eb 12525 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12526 tempreg, breg, mips_gp_register);
c0ebe874 12527 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12528 BFD_RELOC_GPREL16, tempreg);
4d7206a2 12529 relax_switch ();
252b5132 12530 }
67c0d1eb
RS
12531 macro_build_lui (&offset_expr, tempreg);
12532 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12533 tempreg, tempreg, breg);
c0ebe874 12534 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12535 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
12536 if (mips_relax.sequence)
12537 relax_end ();
252b5132
RH
12538 }
12539 }
0a44bf69 12540 else if (!mips_big_got)
252b5132 12541 {
ed6fb7bd 12542 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 12543
252b5132
RH
12544 /* If this is a reference to an external symbol, we want
12545 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12546 nop
c0ebe874 12547 <op> op[0],0($tempreg)
252b5132
RH
12548 Otherwise we want
12549 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12550 nop
12551 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 12552 <op> op[0],0($tempreg)
f5040a92
AO
12553
12554 For NewABI, we want
12555 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 12556 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 12557
252b5132
RH
12558 If there is a base register, we add it to $tempreg before
12559 the <op>. If there is a constant, we stick it in the
12560 <op> instruction. We don't handle constants larger than
12561 16 bits, because we have no way to load the upper 16 bits
12562 (actually, we could handle them for the subset of cases
12563 in which we are not using $at). */
9c2799c2 12564 gas_assert (offset_expr.X_op == O_symbol);
f5040a92
AO
12565 if (HAVE_NEWABI)
12566 {
67c0d1eb
RS
12567 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12568 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 12569 if (breg != 0)
67c0d1eb 12570 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12571 tempreg, tempreg, breg);
c0ebe874 12572 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12573 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
12574 break;
12575 }
252b5132
RH
12576 expr1.X_add_number = offset_expr.X_add_number;
12577 offset_expr.X_add_number = 0;
12578 if (expr1.X_add_number < -0x8000
12579 || expr1.X_add_number >= 0x8000)
12580 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
12581 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12582 lw_reloc_type, mips_gp_register);
269137b2 12583 load_delay_nop ();
4d7206a2
RS
12584 relax_start (offset_expr.X_add_symbol);
12585 relax_switch ();
67c0d1eb
RS
12586 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12587 tempreg, BFD_RELOC_LO16);
4d7206a2 12588 relax_end ();
252b5132 12589 if (breg != 0)
67c0d1eb 12590 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12591 tempreg, tempreg, breg);
c0ebe874 12592 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 12593 }
0a44bf69 12594 else if (mips_big_got && !HAVE_NEWABI)
252b5132 12595 {
67c0d1eb 12596 int gpdelay;
252b5132
RH
12597
12598 /* If this is a reference to an external symbol, we want
12599 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12600 addu $tempreg,$tempreg,$gp
12601 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 12602 <op> op[0],0($tempreg)
252b5132
RH
12603 Otherwise we want
12604 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12605 nop
12606 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 12607 <op> op[0],0($tempreg)
252b5132
RH
12608 If there is a base register, we add it to $tempreg before
12609 the <op>. If there is a constant, we stick it in the
12610 <op> instruction. We don't handle constants larger than
12611 16 bits, because we have no way to load the upper 16 bits
12612 (actually, we could handle them for the subset of cases
f5040a92 12613 in which we are not using $at). */
9c2799c2 12614 gas_assert (offset_expr.X_op == O_symbol);
252b5132
RH
12615 expr1.X_add_number = offset_expr.X_add_number;
12616 offset_expr.X_add_number = 0;
12617 if (expr1.X_add_number < -0x8000
12618 || expr1.X_add_number >= 0x8000)
12619 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 12620 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 12621 relax_start (offset_expr.X_add_symbol);
df58fc94 12622 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 12623 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
12624 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12625 mips_gp_register);
12626 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12627 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 12628 relax_switch ();
67c0d1eb
RS
12629 if (gpdelay)
12630 macro_build (NULL, "nop", "");
12631 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12632 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 12633 load_delay_nop ();
67c0d1eb
RS
12634 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12635 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
12636 relax_end ();
12637
252b5132 12638 if (breg != 0)
67c0d1eb 12639 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12640 tempreg, tempreg, breg);
c0ebe874 12641 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 12642 }
0a44bf69 12643 else if (mips_big_got && HAVE_NEWABI)
f5040a92 12644 {
f5040a92
AO
12645 /* If this is a reference to an external symbol, we want
12646 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12647 add $tempreg,$tempreg,$gp
12648 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 12649 <op> op[0],<ofst>($tempreg)
f5040a92
AO
12650 Otherwise, for local symbols, we want:
12651 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 12652 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
9c2799c2 12653 gas_assert (offset_expr.X_op == O_symbol);
4d7206a2 12654 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
12655 offset_expr.X_add_number = 0;
12656 if (expr1.X_add_number < -0x8000
12657 || expr1.X_add_number >= 0x8000)
12658 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 12659 relax_start (offset_expr.X_add_symbol);
df58fc94 12660 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 12661 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
12662 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12663 mips_gp_register);
12664 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12665 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 12666 if (breg != 0)
67c0d1eb 12667 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12668 tempreg, tempreg, breg);
c0ebe874 12669 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
684022ea 12670
4d7206a2 12671 relax_switch ();
f5040a92 12672 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
12673 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12674 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 12675 if (breg != 0)
67c0d1eb 12676 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12677 tempreg, tempreg, breg);
c0ebe874 12678 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12679 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 12680 relax_end ();
f5040a92 12681 }
252b5132
RH
12682 else
12683 abort ();
12684
252b5132
RH
12685 break;
12686
833794fc
MR
12687 case M_JRADDIUSP:
12688 gas_assert (mips_opts.micromips);
12689 gas_assert (mips_opts.insn32);
12690 start_noreorder ();
12691 macro_build (NULL, "jr", "s", RA);
c0ebe874 12692 expr1.X_add_number = op[0] << 2;
833794fc
MR
12693 macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
12694 end_noreorder ();
12695 break;
12696
12697 case M_JRC:
12698 gas_assert (mips_opts.micromips);
12699 gas_assert (mips_opts.insn32);
c0ebe874 12700 macro_build (NULL, "jr", "s", op[0]);
833794fc
MR
12701 if (mips_opts.noreorder)
12702 macro_build (NULL, "nop", "");
12703 break;
12704
252b5132
RH
12705 case M_LI:
12706 case M_LI_S:
c0ebe874 12707 load_register (op[0], &imm_expr, 0);
8fc2e39e 12708 break;
252b5132
RH
12709
12710 case M_DLI:
c0ebe874 12711 load_register (op[0], &imm_expr, 1);
8fc2e39e 12712 break;
252b5132
RH
12713
12714 case M_LI_SS:
12715 if (imm_expr.X_op == O_constant)
12716 {
8fc2e39e 12717 used_at = 1;
67c0d1eb 12718 load_register (AT, &imm_expr, 0);
c0ebe874 12719 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
12720 break;
12721 }
12722 else
12723 {
b0e6f033
RS
12724 gas_assert (imm_expr.X_op == O_absent
12725 && offset_expr.X_op == O_symbol
90ecf173
MR
12726 && strcmp (segment_name (S_GET_SEGMENT
12727 (offset_expr.X_add_symbol)),
12728 ".lit4") == 0
12729 && offset_expr.X_add_number == 0);
c0ebe874 12730 macro_build (&offset_expr, "lwc1", "T,o(b)", op[0],
17a2f251 12731 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 12732 break;
252b5132
RH
12733 }
12734
12735 case M_LI_D:
ca4e0257
RS
12736 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
12737 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
12738 order 32 bits of the value and the low order 32 bits are either
12739 zero or in OFFSET_EXPR. */
b0e6f033 12740 if (imm_expr.X_op == O_constant)
252b5132 12741 {
bad1aba3 12742 if (GPR_SIZE == 64)
c0ebe874 12743 load_register (op[0], &imm_expr, 1);
252b5132
RH
12744 else
12745 {
12746 int hreg, lreg;
12747
12748 if (target_big_endian)
12749 {
c0ebe874
RS
12750 hreg = op[0];
12751 lreg = op[0] + 1;
252b5132
RH
12752 }
12753 else
12754 {
c0ebe874
RS
12755 hreg = op[0] + 1;
12756 lreg = op[0];
252b5132
RH
12757 }
12758
12759 if (hreg <= 31)
67c0d1eb 12760 load_register (hreg, &imm_expr, 0);
252b5132
RH
12761 if (lreg <= 31)
12762 {
12763 if (offset_expr.X_op == O_absent)
67c0d1eb 12764 move_register (lreg, 0);
252b5132
RH
12765 else
12766 {
9c2799c2 12767 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 12768 load_register (lreg, &offset_expr, 0);
252b5132
RH
12769 }
12770 }
12771 }
8fc2e39e 12772 break;
252b5132 12773 }
b0e6f033 12774 gas_assert (imm_expr.X_op == O_absent);
252b5132
RH
12775
12776 /* We know that sym is in the .rdata section. First we get the
12777 upper 16 bits of the address. */
12778 if (mips_pic == NO_PIC)
12779 {
67c0d1eb 12780 macro_build_lui (&offset_expr, AT);
8fc2e39e 12781 used_at = 1;
252b5132 12782 }
0a44bf69 12783 else
252b5132 12784 {
67c0d1eb
RS
12785 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12786 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 12787 used_at = 1;
252b5132 12788 }
bdaaa2e1 12789
252b5132 12790 /* Now we load the register(s). */
bad1aba3 12791 if (GPR_SIZE == 64)
8fc2e39e
TS
12792 {
12793 used_at = 1;
c0ebe874
RS
12794 macro_build (&offset_expr, "ld", "t,o(b)", op[0],
12795 BFD_RELOC_LO16, AT);
8fc2e39e 12796 }
252b5132
RH
12797 else
12798 {
8fc2e39e 12799 used_at = 1;
c0ebe874
RS
12800 macro_build (&offset_expr, "lw", "t,o(b)", op[0],
12801 BFD_RELOC_LO16, AT);
12802 if (op[0] != RA)
252b5132
RH
12803 {
12804 /* FIXME: How in the world do we deal with the possible
12805 overflow here? */
12806 offset_expr.X_add_number += 4;
67c0d1eb 12807 macro_build (&offset_expr, "lw", "t,o(b)",
c0ebe874 12808 op[0] + 1, BFD_RELOC_LO16, AT);
252b5132
RH
12809 }
12810 }
252b5132
RH
12811 break;
12812
12813 case M_LI_DD:
ca4e0257
RS
12814 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
12815 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
12816 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
12817 the value and the low order 32 bits are either zero or in
12818 OFFSET_EXPR. */
b0e6f033 12819 if (imm_expr.X_op == O_constant)
252b5132 12820 {
9b444f95
FS
12821 tempreg = ZERO;
12822 if (((FPR_SIZE == 64 && GPR_SIZE == 64)
12823 || !ISA_HAS_MXHC1 (mips_opts.isa))
12824 && imm_expr.X_add_number != 0)
12825 {
12826 used_at = 1;
12827 tempreg = AT;
12828 load_register (AT, &imm_expr, FPR_SIZE == 64);
12829 }
351cdf24 12830 if (FPR_SIZE == 64 && GPR_SIZE == 64)
9b444f95 12831 macro_build (NULL, "dmtc1", "t,S", tempreg, op[0]);
252b5132
RH
12832 else
12833 {
9b444f95
FS
12834 if (!ISA_HAS_MXHC1 (mips_opts.isa))
12835 {
12836 if (FPR_SIZE != 32)
12837 as_bad (_("Unable to generate `%s' compliant code "
12838 "without mthc1"),
12839 (FPR_SIZE == 64) ? "fp64" : "fpxx");
12840 else
12841 macro_build (NULL, "mtc1", "t,G", tempreg, op[0] + 1);
12842 }
252b5132 12843 if (offset_expr.X_op == O_absent)
c0ebe874 12844 macro_build (NULL, "mtc1", "t,G", 0, op[0]);
252b5132
RH
12845 else
12846 {
9c2799c2 12847 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 12848 load_register (AT, &offset_expr, 0);
c0ebe874 12849 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132 12850 }
9b444f95
FS
12851 if (ISA_HAS_MXHC1 (mips_opts.isa))
12852 {
12853 if (imm_expr.X_add_number != 0)
12854 {
12855 used_at = 1;
12856 tempreg = AT;
12857 load_register (AT, &imm_expr, 0);
12858 }
12859 macro_build (NULL, "mthc1", "t,G", tempreg, op[0]);
12860 }
252b5132
RH
12861 }
12862 break;
12863 }
12864
b0e6f033
RS
12865 gas_assert (imm_expr.X_op == O_absent
12866 && offset_expr.X_op == O_symbol
90ecf173 12867 && offset_expr.X_add_number == 0);
252b5132
RH
12868 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
12869 if (strcmp (s, ".lit8") == 0)
134c0c8b
MR
12870 {
12871 op[2] = mips_gp_register;
f2ae14a1
RS
12872 offset_reloc[0] = BFD_RELOC_MIPS_LITERAL;
12873 offset_reloc[1] = BFD_RELOC_UNUSED;
12874 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
12875 }
12876 else
12877 {
9c2799c2 12878 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 12879 used_at = 1;
0a44bf69 12880 if (mips_pic != NO_PIC)
67c0d1eb
RS
12881 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12882 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
12883 else
12884 {
12885 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 12886 macro_build_lui (&offset_expr, AT);
252b5132 12887 }
bdaaa2e1 12888
c0ebe874 12889 op[2] = AT;
f2ae14a1
RS
12890 offset_reloc[0] = BFD_RELOC_LO16;
12891 offset_reloc[1] = BFD_RELOC_UNUSED;
12892 offset_reloc[2] = BFD_RELOC_UNUSED;
134c0c8b 12893 }
f2ae14a1 12894 align = 8;
6f2117ba 12895 /* Fall through. */
c4a68bea 12896
252b5132 12897 case M_L_DAB:
6f2117ba
PH
12898 /* The MIPS assembler seems to check for X_add_number not
12899 being double aligned and generating:
12900 lui at,%hi(foo+1)
12901 addu at,at,v1
12902 addiu at,at,%lo(foo+1)
12903 lwc1 f2,0(at)
12904 lwc1 f3,4(at)
12905 But, the resulting address is the same after relocation so why
12906 generate the extra instruction? */
bdaaa2e1 12907 /* Itbl support may require additional care here. */
252b5132 12908 coproc = 1;
df58fc94 12909 fmt = "T,o(b)";
0aa27725 12910 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
12911 {
12912 s = "ldc1";
df58fc94 12913 goto ld_st;
252b5132 12914 }
252b5132 12915 s = "lwc1";
252b5132
RH
12916 goto ldd_std;
12917
12918 case M_S_DAB:
df58fc94
RS
12919 gas_assert (!mips_opts.micromips);
12920 /* Itbl support may require additional care here. */
12921 coproc = 1;
12922 fmt = "T,o(b)";
0aa27725 12923 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
12924 {
12925 s = "sdc1";
df58fc94 12926 goto ld_st;
252b5132 12927 }
252b5132 12928 s = "swc1";
252b5132
RH
12929 goto ldd_std;
12930
e407c74b
NC
12931 case M_LQ_AB:
12932 fmt = "t,o(b)";
12933 s = "lq";
12934 goto ld;
12935
12936 case M_SQ_AB:
12937 fmt = "t,o(b)";
12938 s = "sq";
12939 goto ld_st;
12940
252b5132 12941 case M_LD_AB:
df58fc94 12942 fmt = "t,o(b)";
bad1aba3 12943 if (GPR_SIZE == 64)
252b5132
RH
12944 {
12945 s = "ld";
12946 goto ld;
12947 }
252b5132 12948 s = "lw";
252b5132
RH
12949 goto ldd_std;
12950
12951 case M_SD_AB:
df58fc94 12952 fmt = "t,o(b)";
bad1aba3 12953 if (GPR_SIZE == 64)
252b5132
RH
12954 {
12955 s = "sd";
df58fc94 12956 goto ld_st;
252b5132 12957 }
252b5132 12958 s = "sw";
252b5132
RH
12959
12960 ldd_std:
f2ae14a1
RS
12961 /* Even on a big endian machine $fn comes before $fn+1. We have
12962 to adjust when loading from memory. We set coproc if we must
12963 load $fn+1 first. */
12964 /* Itbl support may require additional care here. */
12965 if (!target_big_endian)
12966 coproc = 0;
12967
c0ebe874 12968 breg = op[2];
f2ae14a1
RS
12969 if (small_offset_p (0, align, 16))
12970 {
12971 ep = &offset_expr;
12972 if (!small_offset_p (4, align, 16))
12973 {
12974 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT, breg,
12975 -1, offset_reloc[0], offset_reloc[1],
12976 offset_reloc[2]);
12977 expr1.X_add_number = 0;
12978 ep = &expr1;
12979 breg = AT;
12980 used_at = 1;
12981 offset_reloc[0] = BFD_RELOC_LO16;
12982 offset_reloc[1] = BFD_RELOC_UNUSED;
12983 offset_reloc[2] = BFD_RELOC_UNUSED;
12984 }
c0ebe874 12985 if (strcmp (s, "lw") == 0 && op[0] == breg)
f2ae14a1
RS
12986 {
12987 ep->X_add_number += 4;
c0ebe874 12988 macro_build (ep, s, fmt, op[0] + 1, -1, offset_reloc[0],
f2ae14a1
RS
12989 offset_reloc[1], offset_reloc[2], breg);
12990 ep->X_add_number -= 4;
c0ebe874 12991 macro_build (ep, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
12992 offset_reloc[1], offset_reloc[2], breg);
12993 }
12994 else
12995 {
c0ebe874 12996 macro_build (ep, s, fmt, coproc ? op[0] + 1 : op[0], -1,
f2ae14a1
RS
12997 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12998 breg);
12999 ep->X_add_number += 4;
c0ebe874 13000 macro_build (ep, s, fmt, coproc ? op[0] : op[0] + 1, -1,
f2ae14a1
RS
13001 offset_reloc[0], offset_reloc[1], offset_reloc[2],
13002 breg);
13003 }
13004 break;
13005 }
13006
252b5132
RH
13007 if (offset_expr.X_op != O_symbol
13008 && offset_expr.X_op != O_constant)
13009 {
1661c76c 13010 as_bad (_("expression too complex"));
252b5132
RH
13011 offset_expr.X_op = O_constant;
13012 }
13013
2051e8c4
MR
13014 if (HAVE_32BIT_ADDRESSES
13015 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
13016 {
13017 char value [32];
13018
13019 sprintf_vma (value, offset_expr.X_add_number);
1661c76c 13020 as_bad (_("number (0x%s) larger than 32 bits"), value);
55e08f71 13021 }
2051e8c4 13022
90ecf173 13023 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
252b5132
RH
13024 {
13025 /* If this is a reference to a GP relative symbol, we want
c0ebe874
RS
13026 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
13027 <op> op[0]+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
13028 If we have a base register, we use this
13029 addu $at,$breg,$gp
c0ebe874
RS
13030 <op> op[0],<sym>($at) (BFD_RELOC_GPREL16)
13031 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
13032 If this is not a GP relative symbol, we want
13033 lui $at,<sym> (BFD_RELOC_HI16_S)
c0ebe874
RS
13034 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
13035 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
13036 If there is a base register, we add it to $at after the
13037 lui instruction. If there is a constant, we always use
13038 the last case. */
39a59cf8
MR
13039 if (offset_expr.X_op == O_symbol
13040 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 13041 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 13042 {
4d7206a2 13043 relax_start (offset_expr.X_add_symbol);
252b5132
RH
13044 if (breg == 0)
13045 {
c9914766 13046 tempreg = mips_gp_register;
252b5132
RH
13047 }
13048 else
13049 {
67c0d1eb 13050 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 13051 AT, breg, mips_gp_register);
252b5132 13052 tempreg = AT;
252b5132
RH
13053 used_at = 1;
13054 }
13055
beae10d5 13056 /* Itbl support may require additional care here. */
c0ebe874 13057 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 13058 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
13059 offset_expr.X_add_number += 4;
13060
13061 /* Set mips_optimize to 2 to avoid inserting an
13062 undesired nop. */
13063 hold_mips_optimize = mips_optimize;
13064 mips_optimize = 2;
beae10d5 13065 /* Itbl support may require additional care here. */
c0ebe874 13066 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 13067 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
13068 mips_optimize = hold_mips_optimize;
13069
4d7206a2 13070 relax_switch ();
252b5132 13071
0970e49e 13072 offset_expr.X_add_number -= 4;
252b5132 13073 }
8fc2e39e 13074 used_at = 1;
f2ae14a1
RS
13075 if (offset_high_part (offset_expr.X_add_number, 16)
13076 != offset_high_part (offset_expr.X_add_number + 4, 16))
13077 {
13078 load_address (AT, &offset_expr, &used_at);
13079 offset_expr.X_op = O_constant;
13080 offset_expr.X_add_number = 0;
13081 }
13082 else
13083 macro_build_lui (&offset_expr, AT);
252b5132 13084 if (breg != 0)
67c0d1eb 13085 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 13086 /* Itbl support may require additional care here. */
c0ebe874 13087 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 13088 BFD_RELOC_LO16, AT);
252b5132
RH
13089 /* FIXME: How do we handle overflow here? */
13090 offset_expr.X_add_number += 4;
beae10d5 13091 /* Itbl support may require additional care here. */
c0ebe874 13092 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 13093 BFD_RELOC_LO16, AT);
4d7206a2
RS
13094 if (mips_relax.sequence)
13095 relax_end ();
bdaaa2e1 13096 }
0a44bf69 13097 else if (!mips_big_got)
252b5132 13098 {
252b5132
RH
13099 /* If this is a reference to an external symbol, we want
13100 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
13101 nop
c0ebe874
RS
13102 <op> op[0],0($at)
13103 <op> op[0]+1,4($at)
252b5132
RH
13104 Otherwise we want
13105 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
13106 nop
c0ebe874
RS
13107 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
13108 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
13109 If there is a base register we add it to $at before the
13110 lwc1 instructions. If there is a constant we include it
13111 in the lwc1 instructions. */
13112 used_at = 1;
13113 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
13114 if (expr1.X_add_number < -0x8000
13115 || expr1.X_add_number >= 0x8000 - 4)
13116 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 13117 load_got_offset (AT, &offset_expr);
269137b2 13118 load_delay_nop ();
252b5132 13119 if (breg != 0)
67c0d1eb 13120 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
13121
13122 /* Set mips_optimize to 2 to avoid inserting an undesired
13123 nop. */
13124 hold_mips_optimize = mips_optimize;
13125 mips_optimize = 2;
4d7206a2 13126
beae10d5 13127 /* Itbl support may require additional care here. */
4d7206a2 13128 relax_start (offset_expr.X_add_symbol);
c0ebe874 13129 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 13130 BFD_RELOC_LO16, AT);
4d7206a2 13131 expr1.X_add_number += 4;
c0ebe874 13132 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 13133 BFD_RELOC_LO16, AT);
4d7206a2 13134 relax_switch ();
c0ebe874 13135 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 13136 BFD_RELOC_LO16, AT);
4d7206a2 13137 offset_expr.X_add_number += 4;
c0ebe874 13138 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 13139 BFD_RELOC_LO16, AT);
4d7206a2 13140 relax_end ();
252b5132 13141
4d7206a2 13142 mips_optimize = hold_mips_optimize;
252b5132 13143 }
0a44bf69 13144 else if (mips_big_got)
252b5132 13145 {
67c0d1eb 13146 int gpdelay;
252b5132
RH
13147
13148 /* If this is a reference to an external symbol, we want
13149 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
13150 addu $at,$at,$gp
13151 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
13152 nop
c0ebe874
RS
13153 <op> op[0],0($at)
13154 <op> op[0]+1,4($at)
252b5132
RH
13155 Otherwise we want
13156 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
13157 nop
c0ebe874
RS
13158 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
13159 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
13160 If there is a base register we add it to $at before the
13161 lwc1 instructions. If there is a constant we include it
13162 in the lwc1 instructions. */
13163 used_at = 1;
13164 expr1.X_add_number = offset_expr.X_add_number;
13165 offset_expr.X_add_number = 0;
13166 if (expr1.X_add_number < -0x8000
13167 || expr1.X_add_number >= 0x8000 - 4)
13168 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 13169 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 13170 relax_start (offset_expr.X_add_symbol);
df58fc94 13171 macro_build (&offset_expr, "lui", LUI_FMT,
67c0d1eb
RS
13172 AT, BFD_RELOC_MIPS_GOT_HI16);
13173 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 13174 AT, AT, mips_gp_register);
67c0d1eb 13175 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 13176 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 13177 load_delay_nop ();
252b5132 13178 if (breg != 0)
67c0d1eb 13179 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 13180 /* Itbl support may require additional care here. */
c0ebe874 13181 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 13182 BFD_RELOC_LO16, AT);
252b5132
RH
13183 expr1.X_add_number += 4;
13184
13185 /* Set mips_optimize to 2 to avoid inserting an undesired
13186 nop. */
13187 hold_mips_optimize = mips_optimize;
13188 mips_optimize = 2;
beae10d5 13189 /* Itbl support may require additional care here. */
c0ebe874 13190 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 13191 BFD_RELOC_LO16, AT);
252b5132
RH
13192 mips_optimize = hold_mips_optimize;
13193 expr1.X_add_number -= 4;
13194
4d7206a2
RS
13195 relax_switch ();
13196 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
13197 if (gpdelay)
13198 macro_build (NULL, "nop", "");
13199 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
13200 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 13201 load_delay_nop ();
252b5132 13202 if (breg != 0)
67c0d1eb 13203 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 13204 /* Itbl support may require additional care here. */
c0ebe874 13205 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 13206 BFD_RELOC_LO16, AT);
4d7206a2 13207 offset_expr.X_add_number += 4;
252b5132
RH
13208
13209 /* Set mips_optimize to 2 to avoid inserting an undesired
13210 nop. */
13211 hold_mips_optimize = mips_optimize;
13212 mips_optimize = 2;
beae10d5 13213 /* Itbl support may require additional care here. */
c0ebe874 13214 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 13215 BFD_RELOC_LO16, AT);
252b5132 13216 mips_optimize = hold_mips_optimize;
4d7206a2 13217 relax_end ();
252b5132 13218 }
252b5132
RH
13219 else
13220 abort ();
13221
252b5132 13222 break;
3739860c 13223
dd6a37e7 13224 case M_SAA_AB:
dd6a37e7 13225 s = "saa";
0db377d0 13226 goto saa_saad;
dd6a37e7 13227 case M_SAAD_AB:
dd6a37e7 13228 s = "saad";
0db377d0
MR
13229 saa_saad:
13230 gas_assert (!mips_opts.micromips);
7f3c4072 13231 offbits = 0;
dd6a37e7
AP
13232 fmt = "t,(b)";
13233 goto ld_st;
13234
252b5132
RH
13235 /* New code added to support COPZ instructions.
13236 This code builds table entries out of the macros in mip_opcodes.
13237 R4000 uses interlocks to handle coproc delays.
13238 Other chips (like the R3000) require nops to be inserted for delays.
13239
f72c8c98 13240 FIXME: Currently, we require that the user handle delays.
252b5132
RH
13241 In order to fill delay slots for non-interlocked chips,
13242 we must have a way to specify delays based on the coprocessor.
13243 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
13244 What are the side-effects of the cop instruction?
13245 What cache support might we have and what are its effects?
13246 Both coprocessor & memory require delays. how long???
bdaaa2e1 13247 What registers are read/set/modified?
252b5132
RH
13248
13249 If an itbl is provided to interpret cop instructions,
bdaaa2e1 13250 this knowledge can be encoded in the itbl spec. */
252b5132
RH
13251
13252 case M_COP0:
13253 s = "c0";
13254 goto copz;
13255 case M_COP1:
13256 s = "c1";
13257 goto copz;
13258 case M_COP2:
13259 s = "c2";
13260 goto copz;
13261 case M_COP3:
13262 s = "c3";
13263 copz:
df58fc94 13264 gas_assert (!mips_opts.micromips);
252b5132
RH
13265 /* For now we just do C (same as Cz). The parameter will be
13266 stored in insn_opcode by mips_ip. */
c8276761 13267 macro_build (NULL, s, "C", (int) ip->insn_opcode);
8fc2e39e 13268 break;
252b5132 13269
ea1fb5dc 13270 case M_MOVE:
c0ebe874 13271 move_register (op[0], op[1]);
8fc2e39e 13272 break;
ea1fb5dc 13273
833794fc
MR
13274 case M_MOVEP:
13275 gas_assert (mips_opts.micromips);
13276 gas_assert (mips_opts.insn32);
c0ebe874
RS
13277 move_register (micromips_to_32_reg_h_map1[op[0]],
13278 micromips_to_32_reg_m_map[op[1]]);
13279 move_register (micromips_to_32_reg_h_map2[op[0]],
13280 micromips_to_32_reg_n_map[op[2]]);
833794fc
MR
13281 break;
13282
252b5132
RH
13283 case M_DMUL:
13284 dbl = 1;
1a0670f3 13285 /* Fall through. */
252b5132 13286 case M_MUL:
e407c74b 13287 if (mips_opts.arch == CPU_R5900)
c0ebe874
RS
13288 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", op[0], op[1],
13289 op[2]);
e407c74b
NC
13290 else
13291 {
c0ebe874
RS
13292 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", op[1], op[2]);
13293 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
e407c74b 13294 }
8fc2e39e 13295 break;
252b5132
RH
13296
13297 case M_DMUL_I:
13298 dbl = 1;
1a0670f3 13299 /* Fall through. */
252b5132
RH
13300 case M_MUL_I:
13301 /* The MIPS assembler some times generates shifts and adds. I'm
13302 not trying to be that fancy. GCC should do this for us
13303 anyway. */
8fc2e39e 13304 used_at = 1;
67c0d1eb 13305 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
13306 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", op[1], AT);
13307 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
13308 break;
13309
13310 case M_DMULO_I:
13311 dbl = 1;
1a0670f3 13312 /* Fall through. */
252b5132
RH
13313 case M_MULO_I:
13314 imm = 1;
13315 goto do_mulo;
13316
13317 case M_DMULO:
13318 dbl = 1;
1a0670f3 13319 /* Fall through. */
252b5132
RH
13320 case M_MULO:
13321 do_mulo:
7d10b47d 13322 start_noreorder ();
8fc2e39e 13323 used_at = 1;
252b5132 13324 if (imm)
67c0d1eb 13325 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
13326 macro_build (NULL, dbl ? "dmult" : "mult", "s,t",
13327 op[1], imm ? AT : op[2]);
13328 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13329 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, op[0], op[0], 31);
df58fc94 13330 macro_build (NULL, "mfhi", MFHL_FMT, AT);
252b5132 13331 if (mips_trap)
c0ebe874 13332 macro_build (NULL, "tne", TRAP_FMT, op[0], AT, 6);
252b5132
RH
13333 else
13334 {
df58fc94
RS
13335 if (mips_opts.micromips)
13336 micromips_label_expr (&label_expr);
13337 else
13338 label_expr.X_add_number = 8;
c0ebe874 13339 macro_build (&label_expr, "beq", "s,t,p", op[0], AT);
a605d2b3 13340 macro_build (NULL, "nop", "");
df58fc94
RS
13341 macro_build (NULL, "break", BRK_FMT, 6);
13342 if (mips_opts.micromips)
13343 micromips_add_label ();
252b5132 13344 }
7d10b47d 13345 end_noreorder ();
c0ebe874 13346 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
13347 break;
13348
13349 case M_DMULOU_I:
13350 dbl = 1;
1a0670f3 13351 /* Fall through. */
252b5132
RH
13352 case M_MULOU_I:
13353 imm = 1;
13354 goto do_mulou;
13355
13356 case M_DMULOU:
13357 dbl = 1;
1a0670f3 13358 /* Fall through. */
252b5132
RH
13359 case M_MULOU:
13360 do_mulou:
7d10b47d 13361 start_noreorder ();
8fc2e39e 13362 used_at = 1;
252b5132 13363 if (imm)
67c0d1eb
RS
13364 load_register (AT, &imm_expr, dbl);
13365 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
c0ebe874 13366 op[1], imm ? AT : op[2]);
df58fc94 13367 macro_build (NULL, "mfhi", MFHL_FMT, AT);
c0ebe874 13368 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132 13369 if (mips_trap)
df58fc94 13370 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
252b5132
RH
13371 else
13372 {
df58fc94
RS
13373 if (mips_opts.micromips)
13374 micromips_label_expr (&label_expr);
13375 else
13376 label_expr.X_add_number = 8;
13377 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
a605d2b3 13378 macro_build (NULL, "nop", "");
df58fc94
RS
13379 macro_build (NULL, "break", BRK_FMT, 6);
13380 if (mips_opts.micromips)
13381 micromips_add_label ();
252b5132 13382 }
7d10b47d 13383 end_noreorder ();
252b5132
RH
13384 break;
13385
771c7ce4 13386 case M_DROL:
fef14a42 13387 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 13388 {
c0ebe874 13389 if (op[0] == op[1])
82dd0097
CD
13390 {
13391 tempreg = AT;
13392 used_at = 1;
13393 }
13394 else
c0ebe874
RS
13395 tempreg = op[0];
13396 macro_build (NULL, "dnegu", "d,w", tempreg, op[2]);
13397 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 13398 break;
82dd0097 13399 }
8fc2e39e 13400 used_at = 1;
c0ebe874
RS
13401 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13402 macro_build (NULL, "dsrlv", "d,t,s", AT, op[1], AT);
13403 macro_build (NULL, "dsllv", "d,t,s", op[0], op[1], op[2]);
13404 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13405 break;
13406
252b5132 13407 case M_ROL:
fef14a42 13408 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13409 {
c0ebe874 13410 if (op[0] == op[1])
82dd0097
CD
13411 {
13412 tempreg = AT;
13413 used_at = 1;
13414 }
13415 else
c0ebe874
RS
13416 tempreg = op[0];
13417 macro_build (NULL, "negu", "d,w", tempreg, op[2]);
13418 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 13419 break;
82dd0097 13420 }
8fc2e39e 13421 used_at = 1;
c0ebe874
RS
13422 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13423 macro_build (NULL, "srlv", "d,t,s", AT, op[1], AT);
13424 macro_build (NULL, "sllv", "d,t,s", op[0], op[1], op[2]);
13425 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13426 break;
13427
771c7ce4
TS
13428 case M_DROL_I:
13429 {
13430 unsigned int rot;
e0471c16
TS
13431 const char *l;
13432 const char *rr;
771c7ce4 13433
771c7ce4 13434 rot = imm_expr.X_add_number & 0x3f;
fef14a42 13435 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
13436 {
13437 rot = (64 - rot) & 0x3f;
13438 if (rot >= 32)
c0ebe874 13439 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
60b63b72 13440 else
c0ebe874 13441 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13442 break;
60b63b72 13443 }
483fc7cd 13444 if (rot == 0)
483fc7cd 13445 {
c0ebe874 13446 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13447 break;
483fc7cd 13448 }
82dd0097 13449 l = (rot < 0x20) ? "dsll" : "dsll32";
91d6fa6a 13450 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
82dd0097 13451 rot &= 0x1f;
8fc2e39e 13452 used_at = 1;
c0ebe874
RS
13453 macro_build (NULL, l, SHFT_FMT, AT, op[1], rot);
13454 macro_build (NULL, rr, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13455 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13456 }
13457 break;
13458
252b5132 13459 case M_ROL_I:
771c7ce4
TS
13460 {
13461 unsigned int rot;
13462
771c7ce4 13463 rot = imm_expr.X_add_number & 0x1f;
fef14a42 13464 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 13465 {
c0ebe874
RS
13466 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1],
13467 (32 - rot) & 0x1f);
8fc2e39e 13468 break;
60b63b72 13469 }
483fc7cd 13470 if (rot == 0)
483fc7cd 13471 {
c0ebe874 13472 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13473 break;
483fc7cd 13474 }
8fc2e39e 13475 used_at = 1;
c0ebe874
RS
13476 macro_build (NULL, "sll", SHFT_FMT, AT, op[1], rot);
13477 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13478 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13479 }
13480 break;
13481
13482 case M_DROR:
fef14a42 13483 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 13484 {
c0ebe874 13485 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 13486 break;
82dd0097 13487 }
8fc2e39e 13488 used_at = 1;
c0ebe874
RS
13489 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13490 macro_build (NULL, "dsllv", "d,t,s", AT, op[1], AT);
13491 macro_build (NULL, "dsrlv", "d,t,s", op[0], op[1], op[2]);
13492 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13493 break;
13494
13495 case M_ROR:
fef14a42 13496 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13497 {
c0ebe874 13498 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 13499 break;
82dd0097 13500 }
8fc2e39e 13501 used_at = 1;
c0ebe874
RS
13502 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13503 macro_build (NULL, "sllv", "d,t,s", AT, op[1], AT);
13504 macro_build (NULL, "srlv", "d,t,s", op[0], op[1], op[2]);
13505 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13506 break;
13507
771c7ce4
TS
13508 case M_DROR_I:
13509 {
13510 unsigned int rot;
e0471c16
TS
13511 const char *l;
13512 const char *rr;
771c7ce4 13513
771c7ce4 13514 rot = imm_expr.X_add_number & 0x3f;
fef14a42 13515 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
13516 {
13517 if (rot >= 32)
c0ebe874 13518 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
82dd0097 13519 else
c0ebe874 13520 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13521 break;
82dd0097 13522 }
483fc7cd 13523 if (rot == 0)
483fc7cd 13524 {
c0ebe874 13525 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13526 break;
483fc7cd 13527 }
91d6fa6a 13528 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
82dd0097
CD
13529 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
13530 rot &= 0x1f;
8fc2e39e 13531 used_at = 1;
c0ebe874
RS
13532 macro_build (NULL, rr, SHFT_FMT, AT, op[1], rot);
13533 macro_build (NULL, l, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13534 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13535 }
13536 break;
13537
252b5132 13538 case M_ROR_I:
771c7ce4
TS
13539 {
13540 unsigned int rot;
13541
771c7ce4 13542 rot = imm_expr.X_add_number & 0x1f;
fef14a42 13543 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13544 {
c0ebe874 13545 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13546 break;
82dd0097 13547 }
483fc7cd 13548 if (rot == 0)
483fc7cd 13549 {
c0ebe874 13550 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13551 break;
483fc7cd 13552 }
8fc2e39e 13553 used_at = 1;
c0ebe874
RS
13554 macro_build (NULL, "srl", SHFT_FMT, AT, op[1], rot);
13555 macro_build (NULL, "sll", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13556 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4 13557 }
252b5132
RH
13558 break;
13559
252b5132 13560 case M_SEQ:
c0ebe874
RS
13561 if (op[1] == 0)
13562 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[2], BFD_RELOC_LO16);
13563 else if (op[2] == 0)
13564 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
13565 else
13566 {
c0ebe874
RS
13567 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13568 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
252b5132 13569 }
8fc2e39e 13570 break;
252b5132
RH
13571
13572 case M_SEQ_I:
b0e6f033 13573 if (imm_expr.X_add_number == 0)
252b5132 13574 {
c0ebe874 13575 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 13576 break;
252b5132 13577 }
c0ebe874 13578 if (op[1] == 0)
252b5132 13579 {
1661c76c 13580 as_warn (_("instruction %s: result is always false"),
252b5132 13581 ip->insn_mo->name);
c0ebe874 13582 move_register (op[0], 0);
8fc2e39e 13583 break;
252b5132 13584 }
dd3cbb7e
NC
13585 if (CPU_HAS_SEQ (mips_opts.arch)
13586 && -512 <= imm_expr.X_add_number
13587 && imm_expr.X_add_number < 512)
13588 {
c0ebe874 13589 macro_build (NULL, "seqi", "t,r,+Q", op[0], op[1],
750bdd57 13590 (int) imm_expr.X_add_number);
dd3cbb7e
NC
13591 break;
13592 }
b0e6f033 13593 if (imm_expr.X_add_number >= 0
252b5132 13594 && imm_expr.X_add_number < 0x10000)
c0ebe874 13595 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1], BFD_RELOC_LO16);
b0e6f033 13596 else if (imm_expr.X_add_number > -0x8000
252b5132
RH
13597 && imm_expr.X_add_number < 0)
13598 {
13599 imm_expr.X_add_number = -imm_expr.X_add_number;
bad1aba3 13600 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
c0ebe874 13601 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 13602 }
dd3cbb7e
NC
13603 else if (CPU_HAS_SEQ (mips_opts.arch))
13604 {
13605 used_at = 1;
bad1aba3 13606 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13607 macro_build (NULL, "seq", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
13608 break;
13609 }
252b5132
RH
13610 else
13611 {
bad1aba3 13612 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13613 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
13614 used_at = 1;
13615 }
c0ebe874 13616 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13617 break;
252b5132 13618
c0ebe874 13619 case M_SGE: /* X >= Y <==> not (X < Y) */
252b5132
RH
13620 s = "slt";
13621 goto sge;
13622 case M_SGEU:
13623 s = "sltu";
13624 sge:
c0ebe874
RS
13625 macro_build (NULL, s, "d,v,t", op[0], op[1], op[2]);
13626 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13627 break;
252b5132 13628
6f2117ba 13629 case M_SGE_I: /* X >= I <==> not (X < I). */
252b5132 13630 case M_SGEU_I:
b0e6f033 13631 if (imm_expr.X_add_number >= -0x8000
252b5132 13632 && imm_expr.X_add_number < 0x8000)
c0ebe874
RS
13633 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
13634 op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
13635 else
13636 {
bad1aba3 13637 load_register (AT, &imm_expr, GPR_SIZE == 64);
67c0d1eb 13638 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
c0ebe874 13639 op[0], op[1], AT);
252b5132
RH
13640 used_at = 1;
13641 }
c0ebe874 13642 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13643 break;
252b5132 13644
6f2117ba 13645 case M_SGT: /* X > Y <==> Y < X. */
252b5132
RH
13646 s = "slt";
13647 goto sgt;
13648 case M_SGTU:
13649 s = "sltu";
13650 sgt:
c0ebe874 13651 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
8fc2e39e 13652 break;
252b5132 13653
6f2117ba 13654 case M_SGT_I: /* X > I <==> I < X. */
252b5132
RH
13655 s = "slt";
13656 goto sgti;
13657 case M_SGTU_I:
13658 s = "sltu";
13659 sgti:
8fc2e39e 13660 used_at = 1;
bad1aba3 13661 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13662 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
252b5132
RH
13663 break;
13664
6f2117ba 13665 case M_SLE: /* X <= Y <==> Y >= X <==> not (Y < X). */
252b5132
RH
13666 s = "slt";
13667 goto sle;
13668 case M_SLEU:
13669 s = "sltu";
13670 sle:
c0ebe874
RS
13671 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13672 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13673 break;
252b5132 13674
c0ebe874 13675 case M_SLE_I: /* X <= I <==> I >= X <==> not (I < X) */
252b5132
RH
13676 s = "slt";
13677 goto slei;
13678 case M_SLEU_I:
13679 s = "sltu";
13680 slei:
8fc2e39e 13681 used_at = 1;
bad1aba3 13682 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874
RS
13683 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13684 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
252b5132
RH
13685 break;
13686
13687 case M_SLT_I:
b0e6f033 13688 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
13689 && imm_expr.X_add_number < 0x8000)
13690 {
c0ebe874
RS
13691 macro_build (&imm_expr, "slti", "t,r,j", op[0], op[1],
13692 BFD_RELOC_LO16);
8fc2e39e 13693 break;
252b5132 13694 }
8fc2e39e 13695 used_at = 1;
bad1aba3 13696 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13697 macro_build (NULL, "slt", "d,v,t", op[0], op[1], AT);
252b5132
RH
13698 break;
13699
13700 case M_SLTU_I:
b0e6f033 13701 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
13702 && imm_expr.X_add_number < 0x8000)
13703 {
c0ebe874 13704 macro_build (&imm_expr, "sltiu", "t,r,j", op[0], op[1],
17a2f251 13705 BFD_RELOC_LO16);
8fc2e39e 13706 break;
252b5132 13707 }
8fc2e39e 13708 used_at = 1;
bad1aba3 13709 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13710 macro_build (NULL, "sltu", "d,v,t", op[0], op[1], AT);
252b5132
RH
13711 break;
13712
13713 case M_SNE:
c0ebe874
RS
13714 if (op[1] == 0)
13715 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[2]);
13716 else if (op[2] == 0)
13717 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
252b5132
RH
13718 else
13719 {
c0ebe874
RS
13720 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13721 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
252b5132 13722 }
8fc2e39e 13723 break;
252b5132
RH
13724
13725 case M_SNE_I:
b0e6f033 13726 if (imm_expr.X_add_number == 0)
252b5132 13727 {
c0ebe874 13728 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
8fc2e39e 13729 break;
252b5132 13730 }
c0ebe874 13731 if (op[1] == 0)
252b5132 13732 {
1661c76c 13733 as_warn (_("instruction %s: result is always true"),
252b5132 13734 ip->insn_mo->name);
bad1aba3 13735 macro_build (&expr1, GPR_SIZE == 32 ? "addiu" : "daddiu", "t,r,j",
c0ebe874 13736 op[0], 0, BFD_RELOC_LO16);
8fc2e39e 13737 break;
252b5132 13738 }
dd3cbb7e
NC
13739 if (CPU_HAS_SEQ (mips_opts.arch)
13740 && -512 <= imm_expr.X_add_number
13741 && imm_expr.X_add_number < 512)
13742 {
c0ebe874 13743 macro_build (NULL, "snei", "t,r,+Q", op[0], op[1],
750bdd57 13744 (int) imm_expr.X_add_number);
dd3cbb7e
NC
13745 break;
13746 }
b0e6f033 13747 if (imm_expr.X_add_number >= 0
252b5132
RH
13748 && imm_expr.X_add_number < 0x10000)
13749 {
c0ebe874
RS
13750 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1],
13751 BFD_RELOC_LO16);
252b5132 13752 }
b0e6f033 13753 else if (imm_expr.X_add_number > -0x8000
252b5132
RH
13754 && imm_expr.X_add_number < 0)
13755 {
13756 imm_expr.X_add_number = -imm_expr.X_add_number;
bad1aba3 13757 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
c0ebe874 13758 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 13759 }
dd3cbb7e
NC
13760 else if (CPU_HAS_SEQ (mips_opts.arch))
13761 {
13762 used_at = 1;
bad1aba3 13763 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13764 macro_build (NULL, "sne", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
13765 break;
13766 }
252b5132
RH
13767 else
13768 {
bad1aba3 13769 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13770 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
13771 used_at = 1;
13772 }
c0ebe874 13773 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
8fc2e39e 13774 break;
252b5132 13775
df58fc94
RS
13776 case M_SUB_I:
13777 s = "addi";
13778 s2 = "sub";
387e7624
FS
13779 if (ISA_IS_R6 (mips_opts.isa))
13780 goto do_subi_i;
13781 else
13782 goto do_subi;
df58fc94
RS
13783 case M_SUBU_I:
13784 s = "addiu";
13785 s2 = "subu";
13786 goto do_subi;
252b5132
RH
13787 case M_DSUB_I:
13788 dbl = 1;
df58fc94
RS
13789 s = "daddi";
13790 s2 = "dsub";
387e7624 13791 if (!mips_opts.micromips && !ISA_IS_R6 (mips_opts.isa))
df58fc94 13792 goto do_subi;
b0e6f033 13793 if (imm_expr.X_add_number > -0x200
387e7624
FS
13794 && imm_expr.X_add_number <= 0x200
13795 && !ISA_IS_R6 (mips_opts.isa))
252b5132 13796 {
b0e6f033
RS
13797 macro_build (NULL, s, "t,r,.", op[0], op[1],
13798 (int) -imm_expr.X_add_number);
8fc2e39e 13799 break;
252b5132 13800 }
df58fc94 13801 goto do_subi_i;
252b5132
RH
13802 case M_DSUBU_I:
13803 dbl = 1;
df58fc94
RS
13804 s = "daddiu";
13805 s2 = "dsubu";
13806 do_subi:
b0e6f033 13807 if (imm_expr.X_add_number > -0x8000
252b5132
RH
13808 && imm_expr.X_add_number <= 0x8000)
13809 {
13810 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 13811 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 13812 break;
252b5132 13813 }
df58fc94 13814 do_subi_i:
8fc2e39e 13815 used_at = 1;
67c0d1eb 13816 load_register (AT, &imm_expr, dbl);
c0ebe874 13817 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
13818 break;
13819
13820 case M_TEQ_I:
13821 s = "teq";
13822 goto trap;
13823 case M_TGE_I:
13824 s = "tge";
13825 goto trap;
13826 case M_TGEU_I:
13827 s = "tgeu";
13828 goto trap;
13829 case M_TLT_I:
13830 s = "tlt";
13831 goto trap;
13832 case M_TLTU_I:
13833 s = "tltu";
13834 goto trap;
13835 case M_TNE_I:
13836 s = "tne";
13837 trap:
8fc2e39e 13838 used_at = 1;
bad1aba3 13839 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13840 macro_build (NULL, s, "s,t", op[0], AT);
252b5132
RH
13841 break;
13842
252b5132 13843 case M_TRUNCWS:
43841e91 13844 case M_TRUNCWD:
df58fc94 13845 gas_assert (!mips_opts.micromips);
0aa27725 13846 gas_assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 13847 used_at = 1;
252b5132
RH
13848
13849 /*
13850 * Is the double cfc1 instruction a bug in the mips assembler;
13851 * or is there a reason for it?
13852 */
7d10b47d 13853 start_noreorder ();
c0ebe874
RS
13854 macro_build (NULL, "cfc1", "t,G", op[2], RA);
13855 macro_build (NULL, "cfc1", "t,G", op[2], RA);
67c0d1eb 13856 macro_build (NULL, "nop", "");
252b5132 13857 expr1.X_add_number = 3;
c0ebe874 13858 macro_build (&expr1, "ori", "t,r,i", AT, op[2], BFD_RELOC_LO16);
252b5132 13859 expr1.X_add_number = 2;
67c0d1eb
RS
13860 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
13861 macro_build (NULL, "ctc1", "t,G", AT, RA);
13862 macro_build (NULL, "nop", "");
13863 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
c0ebe874
RS
13864 op[0], op[1]);
13865 macro_build (NULL, "ctc1", "t,G", op[2], RA);
67c0d1eb 13866 macro_build (NULL, "nop", "");
7d10b47d 13867 end_noreorder ();
252b5132
RH
13868 break;
13869
f2ae14a1 13870 case M_ULH_AB:
252b5132 13871 s = "lb";
df58fc94
RS
13872 s2 = "lbu";
13873 off = 1;
13874 goto uld_st;
f2ae14a1 13875 case M_ULHU_AB:
252b5132 13876 s = "lbu";
df58fc94
RS
13877 s2 = "lbu";
13878 off = 1;
13879 goto uld_st;
f2ae14a1 13880 case M_ULW_AB:
df58fc94
RS
13881 s = "lwl";
13882 s2 = "lwr";
7f3c4072 13883 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
13884 off = 3;
13885 goto uld_st;
f2ae14a1 13886 case M_ULD_AB:
252b5132
RH
13887 s = "ldl";
13888 s2 = "ldr";
7f3c4072 13889 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 13890 off = 7;
df58fc94 13891 goto uld_st;
f2ae14a1 13892 case M_USH_AB:
df58fc94
RS
13893 s = "sb";
13894 s2 = "sb";
13895 off = 1;
13896 ust = 1;
13897 goto uld_st;
f2ae14a1 13898 case M_USW_AB:
df58fc94
RS
13899 s = "swl";
13900 s2 = "swr";
7f3c4072 13901 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 13902 off = 3;
df58fc94
RS
13903 ust = 1;
13904 goto uld_st;
f2ae14a1 13905 case M_USD_AB:
df58fc94
RS
13906 s = "sdl";
13907 s2 = "sdr";
7f3c4072 13908 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
13909 off = 7;
13910 ust = 1;
13911
13912 uld_st:
c0ebe874 13913 breg = op[2];
f2ae14a1 13914 large_offset = !small_offset_p (off, align, offbits);
df58fc94
RS
13915 ep = &offset_expr;
13916 expr1.X_add_number = 0;
f2ae14a1 13917 if (large_offset)
df58fc94
RS
13918 {
13919 used_at = 1;
13920 tempreg = AT;
f2ae14a1
RS
13921 if (small_offset_p (0, align, 16))
13922 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg, -1,
13923 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
13924 else
13925 {
13926 load_address (tempreg, ep, &used_at);
13927 if (breg != 0)
13928 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
13929 tempreg, tempreg, breg);
13930 }
13931 offset_reloc[0] = BFD_RELOC_LO16;
13932 offset_reloc[1] = BFD_RELOC_UNUSED;
13933 offset_reloc[2] = BFD_RELOC_UNUSED;
df58fc94 13934 breg = tempreg;
c0ebe874 13935 tempreg = op[0];
df58fc94
RS
13936 ep = &expr1;
13937 }
c0ebe874 13938 else if (!ust && op[0] == breg)
8fc2e39e
TS
13939 {
13940 used_at = 1;
13941 tempreg = AT;
13942 }
252b5132 13943 else
c0ebe874 13944 tempreg = op[0];
af22f5b2 13945
df58fc94
RS
13946 if (off == 1)
13947 goto ulh_sh;
252b5132 13948
90ecf173 13949 if (!target_big_endian)
df58fc94 13950 ep->X_add_number += off;
f2ae14a1 13951 if (offbits == 12)
c8276761 13952 macro_build (NULL, s, "t,~(b)", tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
13953 else
13954 macro_build (ep, s, "t,o(b)", tempreg, -1,
13955 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94 13956
90ecf173 13957 if (!target_big_endian)
df58fc94 13958 ep->X_add_number -= off;
252b5132 13959 else
df58fc94 13960 ep->X_add_number += off;
f2ae14a1 13961 if (offbits == 12)
df58fc94 13962 macro_build (NULL, s2, "t,~(b)",
c8276761 13963 tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
13964 else
13965 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13966 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 13967
df58fc94 13968 /* If necessary, move the result in tempreg to the final destination. */
c0ebe874 13969 if (!ust && op[0] != tempreg)
df58fc94
RS
13970 {
13971 /* Protect second load's delay slot. */
13972 load_delay_nop ();
c0ebe874 13973 move_register (op[0], tempreg);
df58fc94 13974 }
8fc2e39e 13975 break;
252b5132 13976
df58fc94 13977 ulh_sh:
d6bc6245 13978 used_at = 1;
df58fc94
RS
13979 if (target_big_endian == ust)
13980 ep->X_add_number += off;
c0ebe874 13981 tempreg = ust || large_offset ? op[0] : AT;
f2ae14a1
RS
13982 macro_build (ep, s, "t,o(b)", tempreg, -1,
13983 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94
RS
13984
13985 /* For halfword transfers we need a temporary register to shuffle
13986 bytes. Unfortunately for M_USH_A we have none available before
13987 the next store as AT holds the base address. We deal with this
13988 case by clobbering TREG and then restoring it as with ULH. */
c0ebe874 13989 tempreg = ust == large_offset ? op[0] : AT;
df58fc94 13990 if (ust)
c0ebe874 13991 macro_build (NULL, "srl", SHFT_FMT, tempreg, op[0], 8);
df58fc94
RS
13992
13993 if (target_big_endian == ust)
13994 ep->X_add_number -= off;
252b5132 13995 else
df58fc94 13996 ep->X_add_number += off;
f2ae14a1
RS
13997 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13998 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 13999
df58fc94 14000 /* For M_USH_A re-retrieve the LSB. */
f2ae14a1 14001 if (ust && large_offset)
df58fc94
RS
14002 {
14003 if (target_big_endian)
14004 ep->X_add_number += off;
14005 else
14006 ep->X_add_number -= off;
f2ae14a1
RS
14007 macro_build (&expr1, "lbu", "t,o(b)", AT, -1,
14008 offset_reloc[0], offset_reloc[1], offset_reloc[2], AT);
df58fc94
RS
14009 }
14010 /* For ULH and M_USH_A OR the LSB in. */
f2ae14a1 14011 if (!ust || large_offset)
df58fc94 14012 {
c0ebe874 14013 tempreg = !large_offset ? AT : op[0];
df58fc94 14014 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
c0ebe874 14015 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
df58fc94 14016 }
252b5132
RH
14017 break;
14018
14019 default:
14020 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 14021 are added dynamically. */
1661c76c 14022 as_bad (_("macro %s not implemented yet"), ip->insn_mo->name);
252b5132
RH
14023 break;
14024 }
741fe287 14025 if (!mips_opts.at && used_at)
1661c76c 14026 as_bad (_("macro used $at after \".set noat\""));
252b5132
RH
14027}
14028
14029/* Implement macros in mips16 mode. */
14030
14031static void
17a2f251 14032mips16_macro (struct mips_cl_insn *ip)
252b5132 14033{
c0ebe874 14034 const struct mips_operand_array *operands;
252b5132 14035 int mask;
c0ebe874 14036 int tmp;
252b5132
RH
14037 expressionS expr1;
14038 int dbl;
14039 const char *s, *s2, *s3;
c0ebe874
RS
14040 unsigned int op[MAX_OPERANDS];
14041 unsigned int i;
252b5132
RH
14042
14043 mask = ip->insn_mo->mask;
14044
c0ebe874
RS
14045 operands = insn_operands (ip);
14046 for (i = 0; i < MAX_OPERANDS; i++)
14047 if (operands->operand[i])
14048 op[i] = insn_extract_operand (ip, operands->operand[i]);
14049 else
14050 op[i] = -1;
252b5132 14051
252b5132
RH
14052 expr1.X_op = O_constant;
14053 expr1.X_op_symbol = NULL;
14054 expr1.X_add_symbol = NULL;
14055 expr1.X_add_number = 1;
14056
14057 dbl = 0;
14058
14059 switch (mask)
14060 {
14061 default:
b37df7c4 14062 abort ();
252b5132
RH
14063
14064 case M_DDIV_3:
14065 dbl = 1;
1a0670f3 14066 /* Fall through. */
252b5132
RH
14067 case M_DIV_3:
14068 s = "mflo";
14069 goto do_div3;
14070 case M_DREM_3:
14071 dbl = 1;
1a0670f3 14072 /* Fall through. */
252b5132
RH
14073 case M_REM_3:
14074 s = "mfhi";
14075 do_div3:
7d10b47d 14076 start_noreorder ();
d8722d76 14077 macro_build (NULL, dbl ? "ddiv" : "div", ".,x,y", op[1], op[2]);
252b5132 14078 expr1.X_add_number = 2;
c0ebe874 14079 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 14080 macro_build (NULL, "break", "6", 7);
bdaaa2e1 14081
252b5132
RH
14082 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
14083 since that causes an overflow. We should do that as well,
14084 but I don't see how to do the comparisons without a temporary
14085 register. */
7d10b47d 14086 end_noreorder ();
c0ebe874 14087 macro_build (NULL, s, "x", op[0]);
252b5132
RH
14088 break;
14089
14090 case M_DIVU_3:
14091 s = "divu";
14092 s2 = "mflo";
14093 goto do_divu3;
14094 case M_REMU_3:
14095 s = "divu";
14096 s2 = "mfhi";
14097 goto do_divu3;
14098 case M_DDIVU_3:
14099 s = "ddivu";
14100 s2 = "mflo";
14101 goto do_divu3;
14102 case M_DREMU_3:
14103 s = "ddivu";
14104 s2 = "mfhi";
14105 do_divu3:
7d10b47d 14106 start_noreorder ();
d8722d76 14107 macro_build (NULL, s, ".,x,y", op[1], op[2]);
252b5132 14108 expr1.X_add_number = 2;
c0ebe874 14109 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 14110 macro_build (NULL, "break", "6", 7);
7d10b47d 14111 end_noreorder ();
c0ebe874 14112 macro_build (NULL, s2, "x", op[0]);
252b5132
RH
14113 break;
14114
14115 case M_DMUL:
14116 dbl = 1;
1a0670f3 14117 /* Fall through. */
252b5132 14118 case M_MUL:
c0ebe874
RS
14119 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", op[1], op[2]);
14120 macro_build (NULL, "mflo", "x", op[0]);
8fc2e39e 14121 break;
252b5132
RH
14122
14123 case M_DSUBU_I:
14124 dbl = 1;
14125 goto do_subu;
14126 case M_SUBU_I:
14127 do_subu:
252b5132 14128 imm_expr.X_add_number = -imm_expr.X_add_number;
d8722d76 14129 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,F", op[0], op[1]);
252b5132
RH
14130 break;
14131
14132 case M_SUBU_I_2:
252b5132 14133 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 14134 macro_build (&imm_expr, "addiu", "x,k", op[0]);
252b5132
RH
14135 break;
14136
14137 case M_DSUBU_I_2:
252b5132 14138 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 14139 macro_build (&imm_expr, "daddiu", "y,j", op[0]);
252b5132
RH
14140 break;
14141
14142 case M_BEQ:
14143 s = "cmp";
14144 s2 = "bteqz";
14145 goto do_branch;
14146 case M_BNE:
14147 s = "cmp";
14148 s2 = "btnez";
14149 goto do_branch;
14150 case M_BLT:
14151 s = "slt";
14152 s2 = "btnez";
14153 goto do_branch;
14154 case M_BLTU:
14155 s = "sltu";
14156 s2 = "btnez";
14157 goto do_branch;
14158 case M_BLE:
14159 s = "slt";
14160 s2 = "bteqz";
14161 goto do_reverse_branch;
14162 case M_BLEU:
14163 s = "sltu";
14164 s2 = "bteqz";
14165 goto do_reverse_branch;
14166 case M_BGE:
14167 s = "slt";
14168 s2 = "bteqz";
14169 goto do_branch;
14170 case M_BGEU:
14171 s = "sltu";
14172 s2 = "bteqz";
14173 goto do_branch;
14174 case M_BGT:
14175 s = "slt";
14176 s2 = "btnez";
14177 goto do_reverse_branch;
14178 case M_BGTU:
14179 s = "sltu";
14180 s2 = "btnez";
14181
14182 do_reverse_branch:
c0ebe874
RS
14183 tmp = op[1];
14184 op[1] = op[0];
14185 op[0] = tmp;
252b5132
RH
14186
14187 do_branch:
c0ebe874 14188 macro_build (NULL, s, "x,y", op[0], op[1]);
67c0d1eb 14189 macro_build (&offset_expr, s2, "p");
252b5132
RH
14190 break;
14191
14192 case M_BEQ_I:
14193 s = "cmpi";
14194 s2 = "bteqz";
14195 s3 = "x,U";
14196 goto do_branch_i;
14197 case M_BNE_I:
14198 s = "cmpi";
14199 s2 = "btnez";
14200 s3 = "x,U";
14201 goto do_branch_i;
14202 case M_BLT_I:
14203 s = "slti";
14204 s2 = "btnez";
14205 s3 = "x,8";
14206 goto do_branch_i;
14207 case M_BLTU_I:
14208 s = "sltiu";
14209 s2 = "btnez";
14210 s3 = "x,8";
14211 goto do_branch_i;
14212 case M_BLE_I:
14213 s = "slti";
14214 s2 = "btnez";
14215 s3 = "x,8";
14216 goto do_addone_branch_i;
14217 case M_BLEU_I:
14218 s = "sltiu";
14219 s2 = "btnez";
14220 s3 = "x,8";
14221 goto do_addone_branch_i;
14222 case M_BGE_I:
14223 s = "slti";
14224 s2 = "bteqz";
14225 s3 = "x,8";
14226 goto do_branch_i;
14227 case M_BGEU_I:
14228 s = "sltiu";
14229 s2 = "bteqz";
14230 s3 = "x,8";
14231 goto do_branch_i;
14232 case M_BGT_I:
14233 s = "slti";
14234 s2 = "bteqz";
14235 s3 = "x,8";
14236 goto do_addone_branch_i;
14237 case M_BGTU_I:
14238 s = "sltiu";
14239 s2 = "bteqz";
14240 s3 = "x,8";
14241
14242 do_addone_branch_i:
252b5132
RH
14243 ++imm_expr.X_add_number;
14244
14245 do_branch_i:
c0ebe874 14246 macro_build (&imm_expr, s, s3, op[0]);
67c0d1eb 14247 macro_build (&offset_expr, s2, "p");
252b5132
RH
14248 break;
14249
14250 case M_ABS:
14251 expr1.X_add_number = 0;
c0ebe874
RS
14252 macro_build (&expr1, "slti", "x,8", op[1]);
14253 if (op[0] != op[1])
14254 macro_build (NULL, "move", "y,X", op[0], mips16_to_32_reg_map[op[1]]);
252b5132 14255 expr1.X_add_number = 2;
67c0d1eb 14256 macro_build (&expr1, "bteqz", "p");
c0ebe874 14257 macro_build (NULL, "neg", "x,w", op[0], op[0]);
0acfaea6 14258 break;
252b5132
RH
14259 }
14260}
14261
14daeee3
RS
14262/* Look up instruction [START, START + LENGTH) in HASH. Record any extra
14263 opcode bits in *OPCODE_EXTRA. */
14264
14265static struct mips_opcode *
14266mips_lookup_insn (struct hash_control *hash, const char *start,
da8bca91 14267 ssize_t length, unsigned int *opcode_extra)
14daeee3
RS
14268{
14269 char *name, *dot, *p;
14270 unsigned int mask, suffix;
da8bca91 14271 ssize_t opend;
14daeee3
RS
14272 struct mips_opcode *insn;
14273
14274 /* Make a copy of the instruction so that we can fiddle with it. */
4ec9d7d5 14275 name = xstrndup (start, length);
14daeee3
RS
14276
14277 /* Look up the instruction as-is. */
14278 insn = (struct mips_opcode *) hash_find (hash, name);
ee5734f0 14279 if (insn)
e1fa0163 14280 goto end;
14daeee3
RS
14281
14282 dot = strchr (name, '.');
14283 if (dot && dot[1])
14284 {
14285 /* Try to interpret the text after the dot as a VU0 channel suffix. */
14286 p = mips_parse_vu0_channels (dot + 1, &mask);
14287 if (*p == 0 && mask != 0)
14288 {
14289 *dot = 0;
14290 insn = (struct mips_opcode *) hash_find (hash, name);
14291 *dot = '.';
14292 if (insn && (insn->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0)
14293 {
14294 *opcode_extra |= mask << mips_vu0_channel_mask.lsb;
e1fa0163 14295 goto end;
14daeee3
RS
14296 }
14297 }
14298 }
14299
14300 if (mips_opts.micromips)
14301 {
14302 /* See if there's an instruction size override suffix,
14303 either `16' or `32', at the end of the mnemonic proper,
14304 that defines the operation, i.e. before the first `.'
14305 character if any. Strip it and retry. */
14306 opend = dot != NULL ? dot - name : length;
14307 if (opend >= 3 && name[opend - 2] == '1' && name[opend - 1] == '6')
14308 suffix = 2;
3076e594 14309 else if (opend >= 2 && name[opend - 2] == '3' && name[opend - 1] == '2')
14daeee3
RS
14310 suffix = 4;
14311 else
14312 suffix = 0;
14313 if (suffix)
14314 {
39334a61 14315 memmove (name + opend - 2, name + opend, length - opend + 1);
14daeee3 14316 insn = (struct mips_opcode *) hash_find (hash, name);
ee5734f0 14317 if (insn)
14daeee3
RS
14318 {
14319 forced_insn_length = suffix;
e1fa0163 14320 goto end;
14daeee3
RS
14321 }
14322 }
14323 }
14324
e1fa0163
NC
14325 insn = NULL;
14326 end:
14327 free (name);
14328 return insn;
14daeee3
RS
14329}
14330
77bd4346 14331/* Assemble an instruction into its binary format. If the instruction
e423441d
RS
14332 is a macro, set imm_expr and offset_expr to the values associated
14333 with "I" and "A" operands respectively. Otherwise store the value
14334 of the relocatable field (if any) in offset_expr. In both cases
14335 set offset_reloc to the relocation operators applied to offset_expr. */
252b5132
RH
14336
14337static void
60f20e8b 14338mips_ip (char *str, struct mips_cl_insn *insn)
252b5132 14339{
60f20e8b 14340 const struct mips_opcode *first, *past;
df58fc94 14341 struct hash_control *hash;
a92713e6 14342 char format;
14daeee3 14343 size_t end;
a92713e6 14344 struct mips_operand_token *tokens;
14daeee3 14345 unsigned int opcode_extra;
252b5132 14346
df58fc94
RS
14347 if (mips_opts.micromips)
14348 {
14349 hash = micromips_op_hash;
14350 past = &micromips_opcodes[bfd_micromips_num_opcodes];
14351 }
14352 else
14353 {
14354 hash = op_hash;
14355 past = &mips_opcodes[NUMOPCODES];
14356 }
14357 forced_insn_length = 0;
14daeee3 14358 opcode_extra = 0;
252b5132 14359
df58fc94 14360 /* We first try to match an instruction up to a space or to the end. */
a40bc9dd
RS
14361 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
14362 continue;
bdaaa2e1 14363
60f20e8b
RS
14364 first = mips_lookup_insn (hash, str, end, &opcode_extra);
14365 if (first == NULL)
252b5132 14366 {
1661c76c 14367 set_insn_error (0, _("unrecognized opcode"));
a40bc9dd 14368 return;
252b5132
RH
14369 }
14370
60f20e8b 14371 if (strcmp (first->name, "li.s") == 0)
a92713e6 14372 format = 'f';
60f20e8b 14373 else if (strcmp (first->name, "li.d") == 0)
a92713e6
RS
14374 format = 'd';
14375 else
14376 format = 0;
14377 tokens = mips_parse_arguments (str + end, format);
14378 if (!tokens)
14379 return;
14380
60f20e8b
RS
14381 if (!match_insns (insn, first, past, tokens, opcode_extra, FALSE)
14382 && !match_insns (insn, first, past, tokens, opcode_extra, TRUE))
1661c76c 14383 set_insn_error (0, _("invalid operands"));
df58fc94 14384
e3de51ce 14385 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
14386}
14387
77bd4346
RS
14388/* As for mips_ip, but used when assembling MIPS16 code.
14389 Also set forced_insn_length to the resulting instruction size in
14390 bytes if the user explicitly requested a small or extended instruction. */
252b5132
RH
14391
14392static void
60f20e8b 14393mips16_ip (char *str, struct mips_cl_insn *insn)
252b5132 14394{
1a00e612 14395 char *end, *s, c;
60f20e8b 14396 struct mips_opcode *first;
a92713e6 14397 struct mips_operand_token *tokens;
3fb49709 14398 unsigned int l;
252b5132 14399
25499ac7 14400 for (s = str; *s != '\0' && *s != '.' && *s != ' '; ++s)
252b5132 14401 ;
1a00e612
RS
14402 end = s;
14403 c = *end;
3fb49709
MR
14404
14405 l = 0;
1a00e612 14406 switch (c)
252b5132
RH
14407 {
14408 case '\0':
14409 break;
14410
14411 case ' ':
1a00e612 14412 s++;
252b5132
RH
14413 break;
14414
14415 case '.':
3fb49709
MR
14416 s++;
14417 if (*s == 't')
252b5132 14418 {
3fb49709
MR
14419 l = 2;
14420 s++;
252b5132 14421 }
3fb49709 14422 else if (*s == 'e')
252b5132 14423 {
3fb49709
MR
14424 l = 4;
14425 s++;
252b5132 14426 }
3fb49709
MR
14427 if (*s == '\0')
14428 break;
14429 else if (*s++ == ' ')
14430 break;
1661c76c 14431 set_insn_error (0, _("unrecognized opcode"));
252b5132
RH
14432 return;
14433 }
3fb49709 14434 forced_insn_length = l;
252b5132 14435
1a00e612 14436 *end = 0;
60f20e8b 14437 first = (struct mips_opcode *) hash_find (mips16_op_hash, str);
1a00e612
RS
14438 *end = c;
14439
60f20e8b 14440 if (!first)
252b5132 14441 {
1661c76c 14442 set_insn_error (0, _("unrecognized opcode"));
252b5132
RH
14443 return;
14444 }
14445
a92713e6
RS
14446 tokens = mips_parse_arguments (s, 0);
14447 if (!tokens)
14448 return;
14449
60f20e8b 14450 if (!match_mips16_insns (insn, first, tokens))
1661c76c 14451 set_insn_error (0, _("invalid operands"));
252b5132 14452
e3de51ce 14453 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
14454}
14455
b886a2ab
RS
14456/* Marshal immediate value VAL for an extended MIPS16 instruction.
14457 NBITS is the number of significant bits in VAL. */
14458
14459static unsigned long
14460mips16_immed_extend (offsetT val, unsigned int nbits)
14461{
14462 int extval;
25499ac7
MR
14463
14464 extval = 0;
14465 val &= (1U << nbits) - 1;
14466 if (nbits == 16 || nbits == 9)
b886a2ab
RS
14467 {
14468 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14469 val &= 0x1f;
14470 }
14471 else if (nbits == 15)
14472 {
14473 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14474 val &= 0xf;
14475 }
25499ac7 14476 else if (nbits == 6)
b886a2ab
RS
14477 {
14478 extval = ((val & 0x1f) << 6) | (val & 0x20);
14479 val = 0;
14480 }
14481 return (extval << 16) | val;
14482}
14483
3ccad066
RS
14484/* Like decode_mips16_operand, but require the operand to be defined and
14485 require it to be an integer. */
14486
14487static const struct mips_int_operand *
14488mips16_immed_operand (int type, bfd_boolean extended_p)
14489{
14490 const struct mips_operand *operand;
14491
14492 operand = decode_mips16_operand (type, extended_p);
14493 if (!operand || (operand->type != OP_INT && operand->type != OP_PCREL))
14494 abort ();
14495 return (const struct mips_int_operand *) operand;
14496}
14497
14498/* Return true if SVAL fits OPERAND. RELOC is as for mips16_immed. */
14499
14500static bfd_boolean
14501mips16_immed_in_range_p (const struct mips_int_operand *operand,
14502 bfd_reloc_code_real_type reloc, offsetT sval)
14503{
14504 int min_val, max_val;
14505
14506 min_val = mips_int_operand_min (operand);
14507 max_val = mips_int_operand_max (operand);
14508 if (reloc != BFD_RELOC_UNUSED)
14509 {
14510 if (min_val < 0)
14511 sval = SEXT_16BIT (sval);
14512 else
14513 sval &= 0xffff;
14514 }
14515
14516 return (sval >= min_val
14517 && sval <= max_val
14518 && (sval & ((1 << operand->shift) - 1)) == 0);
14519}
14520
5c04167a
RS
14521/* Install immediate value VAL into MIPS16 instruction *INSN,
14522 extending it if necessary. The instruction in *INSN may
14523 already be extended.
14524
43c0598f
RS
14525 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14526 if none. In the former case, VAL is a 16-bit number with no
14527 defined signedness.
14528
14529 TYPE is the type of the immediate field. USER_INSN_LENGTH
14530 is the length that the user requested, or 0 if none. */
252b5132
RH
14531
14532static void
3b4dbbbf 14533mips16_immed (const char *file, unsigned int line, int type,
43c0598f 14534 bfd_reloc_code_real_type reloc, offsetT val,
5c04167a 14535 unsigned int user_insn_length, unsigned long *insn)
252b5132 14536{
3ccad066
RS
14537 const struct mips_int_operand *operand;
14538 unsigned int uval, length;
252b5132 14539
3ccad066
RS
14540 operand = mips16_immed_operand (type, FALSE);
14541 if (!mips16_immed_in_range_p (operand, reloc, val))
5c04167a
RS
14542 {
14543 /* We need an extended instruction. */
14544 if (user_insn_length == 2)
14545 as_bad_where (file, line, _("invalid unextended operand value"));
14546 else
14547 *insn |= MIPS16_EXTEND;
14548 }
14549 else if (user_insn_length == 4)
14550 {
14551 /* The operand doesn't force an unextended instruction to be extended.
14552 Warn if the user wanted an extended instruction anyway. */
14553 *insn |= MIPS16_EXTEND;
14554 as_warn_where (file, line,
14555 _("extended operand requested but not required"));
14556 }
252b5132 14557
3ccad066
RS
14558 length = mips16_opcode_length (*insn);
14559 if (length == 4)
252b5132 14560 {
3ccad066
RS
14561 operand = mips16_immed_operand (type, TRUE);
14562 if (!mips16_immed_in_range_p (operand, reloc, val))
14563 as_bad_where (file, line,
14564 _("operand value out of range for instruction"));
252b5132 14565 }
3ccad066 14566 uval = ((unsigned int) val >> operand->shift) - operand->bias;
bdd15286 14567 if (length == 2 || operand->root.lsb != 0)
3ccad066 14568 *insn = mips_insert_operand (&operand->root, *insn, uval);
252b5132 14569 else
3ccad066 14570 *insn |= mips16_immed_extend (uval, operand->root.size);
252b5132
RH
14571}
14572\f
d6f16593 14573struct percent_op_match
ad8d3bb3 14574{
5e0116d5
RS
14575 const char *str;
14576 bfd_reloc_code_real_type reloc;
d6f16593
MR
14577};
14578
14579static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 14580{
5e0116d5 14581 {"%lo", BFD_RELOC_LO16},
5e0116d5
RS
14582 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14583 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14584 {"%call16", BFD_RELOC_MIPS_CALL16},
14585 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14586 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14587 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14588 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14589 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14590 {"%got", BFD_RELOC_MIPS_GOT16},
14591 {"%gp_rel", BFD_RELOC_GPREL16},
be3f1006 14592 {"%gprel", BFD_RELOC_GPREL16},
5e0116d5
RS
14593 {"%half", BFD_RELOC_16},
14594 {"%highest", BFD_RELOC_MIPS_HIGHEST},
14595 {"%higher", BFD_RELOC_MIPS_HIGHER},
14596 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
14597 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14598 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14599 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14600 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14601 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14602 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14603 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
7361da2c
AB
14604 {"%hi", BFD_RELOC_HI16_S},
14605 {"%pcrel_hi", BFD_RELOC_HI16_S_PCREL},
14606 {"%pcrel_lo", BFD_RELOC_LO16_PCREL}
ad8d3bb3
TS
14607};
14608
d6f16593
MR
14609static const struct percent_op_match mips16_percent_op[] =
14610{
14611 {"%lo", BFD_RELOC_MIPS16_LO16},
be3f1006 14612 {"%gp_rel", BFD_RELOC_MIPS16_GPREL},
d6f16593 14613 {"%gprel", BFD_RELOC_MIPS16_GPREL},
738e5348
RS
14614 {"%got", BFD_RELOC_MIPS16_GOT16},
14615 {"%call16", BFD_RELOC_MIPS16_CALL16},
d0f13682
CLT
14616 {"%hi", BFD_RELOC_MIPS16_HI16_S},
14617 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14618 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14619 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14620 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14621 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14622 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14623 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
d6f16593
MR
14624};
14625
252b5132 14626
5e0116d5
RS
14627/* Return true if *STR points to a relocation operator. When returning true,
14628 move *STR over the operator and store its relocation code in *RELOC.
14629 Leave both *STR and *RELOC alone when returning false. */
14630
14631static bfd_boolean
17a2f251 14632parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 14633{
d6f16593
MR
14634 const struct percent_op_match *percent_op;
14635 size_t limit, i;
14636
14637 if (mips_opts.mips16)
14638 {
14639 percent_op = mips16_percent_op;
14640 limit = ARRAY_SIZE (mips16_percent_op);
14641 }
14642 else
14643 {
14644 percent_op = mips_percent_op;
14645 limit = ARRAY_SIZE (mips_percent_op);
14646 }
76b3015f 14647
d6f16593 14648 for (i = 0; i < limit; i++)
5e0116d5 14649 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 14650 {
3f98094e
DJ
14651 int len = strlen (percent_op[i].str);
14652
14653 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14654 continue;
14655
5e0116d5
RS
14656 *str += strlen (percent_op[i].str);
14657 *reloc = percent_op[i].reloc;
394f9b3a 14658
5e0116d5
RS
14659 /* Check whether the output BFD supports this relocation.
14660 If not, issue an error and fall back on something safe. */
14661 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 14662 {
20203fb9 14663 as_bad (_("relocation %s isn't supported by the current ABI"),
5e0116d5 14664 percent_op[i].str);
01a3f561 14665 *reloc = BFD_RELOC_UNUSED;
394f9b3a 14666 }
5e0116d5 14667 return TRUE;
394f9b3a 14668 }
5e0116d5 14669 return FALSE;
394f9b3a 14670}
ad8d3bb3 14671
ad8d3bb3 14672
5e0116d5
RS
14673/* Parse string STR as a 16-bit relocatable operand. Store the
14674 expression in *EP and the relocations in the array starting
14675 at RELOC. Return the number of relocation operators used.
ad8d3bb3 14676
01a3f561 14677 On exit, EXPR_END points to the first character after the expression. */
ad8d3bb3 14678
5e0116d5 14679static size_t
17a2f251
TS
14680my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14681 char *str)
ad8d3bb3 14682{
5e0116d5
RS
14683 bfd_reloc_code_real_type reversed_reloc[3];
14684 size_t reloc_index, i;
09b8f35a
RS
14685 int crux_depth, str_depth;
14686 char *crux;
5e0116d5
RS
14687
14688 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
14689 in REVERSED_RELOC. End the loop with CRUX pointing to the start
14690 of the main expression and with CRUX_DEPTH containing the number
14691 of open brackets at that point. */
14692 reloc_index = -1;
14693 str_depth = 0;
14694 do
fb1b3232 14695 {
09b8f35a
RS
14696 reloc_index++;
14697 crux = str;
14698 crux_depth = str_depth;
14699
14700 /* Skip over whitespace and brackets, keeping count of the number
14701 of brackets. */
14702 while (*str == ' ' || *str == '\t' || *str == '(')
14703 if (*str++ == '(')
14704 str_depth++;
5e0116d5 14705 }
09b8f35a
RS
14706 while (*str == '%'
14707 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14708 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 14709
09b8f35a 14710 my_getExpression (ep, crux);
5e0116d5 14711 str = expr_end;
394f9b3a 14712
5e0116d5 14713 /* Match every open bracket. */
09b8f35a 14714 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 14715 if (*str++ == ')')
09b8f35a 14716 crux_depth--;
394f9b3a 14717
09b8f35a 14718 if (crux_depth > 0)
20203fb9 14719 as_bad (_("unclosed '('"));
394f9b3a 14720
5e0116d5 14721 expr_end = str;
252b5132 14722
01a3f561 14723 if (reloc_index != 0)
64bdfcaf
RS
14724 {
14725 prev_reloc_op_frag = frag_now;
14726 for (i = 0; i < reloc_index; i++)
14727 reloc[i] = reversed_reloc[reloc_index - 1 - i];
14728 }
fb1b3232 14729
5e0116d5 14730 return reloc_index;
252b5132
RH
14731}
14732
14733static void
17a2f251 14734my_getExpression (expressionS *ep, char *str)
252b5132
RH
14735{
14736 char *save_in;
14737
14738 save_in = input_line_pointer;
14739 input_line_pointer = str;
14740 expression (ep);
14741 expr_end = input_line_pointer;
14742 input_line_pointer = save_in;
252b5132
RH
14743}
14744
6d4af3c2 14745const char *
17a2f251 14746md_atof (int type, char *litP, int *sizeP)
252b5132 14747{
499ac353 14748 return ieee_md_atof (type, litP, sizeP, target_big_endian);
252b5132
RH
14749}
14750
14751void
17a2f251 14752md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
14753{
14754 if (target_big_endian)
14755 number_to_chars_bigendian (buf, val, n);
14756 else
14757 number_to_chars_littleendian (buf, val, n);
14758}
14759\f
e013f690
TS
14760static int support_64bit_objects(void)
14761{
14762 const char **list, **l;
aa3d8fdf 14763 int yes;
e013f690
TS
14764
14765 list = bfd_target_list ();
14766 for (l = list; *l != NULL; l++)
aeffff67
RS
14767 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14768 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
e013f690 14769 break;
aa3d8fdf 14770 yes = (*l != NULL);
e013f690 14771 free (list);
aa3d8fdf 14772 return yes;
e013f690
TS
14773}
14774
316f5878
RS
14775/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14776 NEW_VALUE. Warn if another value was already specified. Note:
14777 we have to defer parsing the -march and -mtune arguments in order
14778 to handle 'from-abi' correctly, since the ABI might be specified
14779 in a later argument. */
14780
14781static void
17a2f251 14782mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
14783{
14784 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
1661c76c 14785 as_warn (_("a different %s was already specified, is now %s"),
316f5878
RS
14786 string_ptr == &mips_arch_string ? "-march" : "-mtune",
14787 new_value);
14788
14789 *string_ptr = new_value;
14790}
14791
252b5132 14792int
17b9d67d 14793md_parse_option (int c, const char *arg)
252b5132 14794{
c6278170
RS
14795 unsigned int i;
14796
14797 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
14798 if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
14799 {
919731af 14800 file_ase_explicit |= mips_set_ase (&mips_ases[i], &file_mips_opts,
c6278170
RS
14801 c == mips_ases[i].option_on);
14802 return 1;
14803 }
14804
252b5132
RH
14805 switch (c)
14806 {
119d663a
NC
14807 case OPTION_CONSTRUCT_FLOATS:
14808 mips_disable_float_construction = 0;
14809 break;
bdaaa2e1 14810
119d663a
NC
14811 case OPTION_NO_CONSTRUCT_FLOATS:
14812 mips_disable_float_construction = 1;
14813 break;
bdaaa2e1 14814
252b5132
RH
14815 case OPTION_TRAP:
14816 mips_trap = 1;
14817 break;
14818
14819 case OPTION_BREAK:
14820 mips_trap = 0;
14821 break;
14822
14823 case OPTION_EB:
14824 target_big_endian = 1;
14825 break;
14826
14827 case OPTION_EL:
14828 target_big_endian = 0;
14829 break;
14830
14831 case 'O':
4ffff32f
TS
14832 if (arg == NULL)
14833 mips_optimize = 1;
14834 else if (arg[0] == '0')
14835 mips_optimize = 0;
14836 else if (arg[0] == '1')
252b5132
RH
14837 mips_optimize = 1;
14838 else
14839 mips_optimize = 2;
14840 break;
14841
14842 case 'g':
14843 if (arg == NULL)
14844 mips_debug = 2;
14845 else
14846 mips_debug = atoi (arg);
252b5132
RH
14847 break;
14848
14849 case OPTION_MIPS1:
0b35dfee 14850 file_mips_opts.isa = ISA_MIPS1;
252b5132
RH
14851 break;
14852
14853 case OPTION_MIPS2:
0b35dfee 14854 file_mips_opts.isa = ISA_MIPS2;
252b5132
RH
14855 break;
14856
14857 case OPTION_MIPS3:
0b35dfee 14858 file_mips_opts.isa = ISA_MIPS3;
252b5132
RH
14859 break;
14860
14861 case OPTION_MIPS4:
0b35dfee 14862 file_mips_opts.isa = ISA_MIPS4;
e7af610e
NC
14863 break;
14864
84ea6cf2 14865 case OPTION_MIPS5:
0b35dfee 14866 file_mips_opts.isa = ISA_MIPS5;
84ea6cf2
NC
14867 break;
14868
e7af610e 14869 case OPTION_MIPS32:
0b35dfee 14870 file_mips_opts.isa = ISA_MIPS32;
252b5132
RH
14871 break;
14872
af7ee8bf 14873 case OPTION_MIPS32R2:
0b35dfee 14874 file_mips_opts.isa = ISA_MIPS32R2;
af7ee8bf
CD
14875 break;
14876
ae52f483 14877 case OPTION_MIPS32R3:
0ae19f05 14878 file_mips_opts.isa = ISA_MIPS32R3;
ae52f483
AB
14879 break;
14880
14881 case OPTION_MIPS32R5:
0ae19f05 14882 file_mips_opts.isa = ISA_MIPS32R5;
ae52f483
AB
14883 break;
14884
7361da2c
AB
14885 case OPTION_MIPS32R6:
14886 file_mips_opts.isa = ISA_MIPS32R6;
14887 break;
14888
5f74bc13 14889 case OPTION_MIPS64R2:
0b35dfee 14890 file_mips_opts.isa = ISA_MIPS64R2;
5f74bc13
CD
14891 break;
14892
ae52f483 14893 case OPTION_MIPS64R3:
0ae19f05 14894 file_mips_opts.isa = ISA_MIPS64R3;
ae52f483
AB
14895 break;
14896
14897 case OPTION_MIPS64R5:
0ae19f05 14898 file_mips_opts.isa = ISA_MIPS64R5;
ae52f483
AB
14899 break;
14900
7361da2c
AB
14901 case OPTION_MIPS64R6:
14902 file_mips_opts.isa = ISA_MIPS64R6;
14903 break;
14904
84ea6cf2 14905 case OPTION_MIPS64:
0b35dfee 14906 file_mips_opts.isa = ISA_MIPS64;
84ea6cf2
NC
14907 break;
14908
ec68c924 14909 case OPTION_MTUNE:
316f5878
RS
14910 mips_set_option_string (&mips_tune_string, arg);
14911 break;
ec68c924 14912
316f5878
RS
14913 case OPTION_MARCH:
14914 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
14915 break;
14916
14917 case OPTION_M4650:
316f5878
RS
14918 mips_set_option_string (&mips_arch_string, "4650");
14919 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
14920 break;
14921
14922 case OPTION_NO_M4650:
14923 break;
14924
14925 case OPTION_M4010:
316f5878
RS
14926 mips_set_option_string (&mips_arch_string, "4010");
14927 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
14928 break;
14929
14930 case OPTION_NO_M4010:
14931 break;
14932
14933 case OPTION_M4100:
316f5878
RS
14934 mips_set_option_string (&mips_arch_string, "4100");
14935 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
14936 break;
14937
14938 case OPTION_NO_M4100:
14939 break;
14940
252b5132 14941 case OPTION_M3900:
316f5878
RS
14942 mips_set_option_string (&mips_arch_string, "3900");
14943 mips_set_option_string (&mips_tune_string, "3900");
252b5132 14944 break;
bdaaa2e1 14945
252b5132
RH
14946 case OPTION_NO_M3900:
14947 break;
14948
df58fc94 14949 case OPTION_MICROMIPS:
919731af 14950 if (file_mips_opts.mips16 == 1)
df58fc94
RS
14951 {
14952 as_bad (_("-mmicromips cannot be used with -mips16"));
14953 return 0;
14954 }
919731af 14955 file_mips_opts.micromips = 1;
df58fc94
RS
14956 mips_no_prev_insn ();
14957 break;
14958
14959 case OPTION_NO_MICROMIPS:
919731af 14960 file_mips_opts.micromips = 0;
df58fc94
RS
14961 mips_no_prev_insn ();
14962 break;
14963
252b5132 14964 case OPTION_MIPS16:
919731af 14965 if (file_mips_opts.micromips == 1)
df58fc94
RS
14966 {
14967 as_bad (_("-mips16 cannot be used with -micromips"));
14968 return 0;
14969 }
919731af 14970 file_mips_opts.mips16 = 1;
7d10b47d 14971 mips_no_prev_insn ();
252b5132
RH
14972 break;
14973
14974 case OPTION_NO_MIPS16:
919731af 14975 file_mips_opts.mips16 = 0;
7d10b47d 14976 mips_no_prev_insn ();
252b5132
RH
14977 break;
14978
6a32d874
CM
14979 case OPTION_FIX_24K:
14980 mips_fix_24k = 1;
14981 break;
14982
14983 case OPTION_NO_FIX_24K:
14984 mips_fix_24k = 0;
14985 break;
14986
a8d14a88
CM
14987 case OPTION_FIX_RM7000:
14988 mips_fix_rm7000 = 1;
14989 break;
14990
14991 case OPTION_NO_FIX_RM7000:
14992 mips_fix_rm7000 = 0;
14993 break;
14994
6f2117ba
PH
14995 case OPTION_FIX_LOONGSON3_LLSC:
14996 mips_fix_loongson3_llsc = TRUE;
14997 break;
14998
14999 case OPTION_NO_FIX_LOONGSON3_LLSC:
15000 mips_fix_loongson3_llsc = FALSE;
15001 break;
15002
c67a084a
NC
15003 case OPTION_FIX_LOONGSON2F_JUMP:
15004 mips_fix_loongson2f_jump = TRUE;
15005 break;
15006
15007 case OPTION_NO_FIX_LOONGSON2F_JUMP:
15008 mips_fix_loongson2f_jump = FALSE;
15009 break;
15010
15011 case OPTION_FIX_LOONGSON2F_NOP:
15012 mips_fix_loongson2f_nop = TRUE;
15013 break;
15014
15015 case OPTION_NO_FIX_LOONGSON2F_NOP:
15016 mips_fix_loongson2f_nop = FALSE;
15017 break;
15018
d766e8ec
RS
15019 case OPTION_FIX_VR4120:
15020 mips_fix_vr4120 = 1;
60b63b72
RS
15021 break;
15022
d766e8ec
RS
15023 case OPTION_NO_FIX_VR4120:
15024 mips_fix_vr4120 = 0;
60b63b72
RS
15025 break;
15026
7d8e00cf
RS
15027 case OPTION_FIX_VR4130:
15028 mips_fix_vr4130 = 1;
15029 break;
15030
15031 case OPTION_NO_FIX_VR4130:
15032 mips_fix_vr4130 = 0;
15033 break;
15034
d954098f
DD
15035 case OPTION_FIX_CN63XXP1:
15036 mips_fix_cn63xxp1 = TRUE;
15037 break;
15038
15039 case OPTION_NO_FIX_CN63XXP1:
15040 mips_fix_cn63xxp1 = FALSE;
15041 break;
15042
27c634e0
FN
15043 case OPTION_FIX_R5900:
15044 mips_fix_r5900 = TRUE;
15045 mips_fix_r5900_explicit = TRUE;
15046 break;
15047
15048 case OPTION_NO_FIX_R5900:
15049 mips_fix_r5900 = FALSE;
15050 mips_fix_r5900_explicit = TRUE;
15051 break;
15052
4a6a3df4
AO
15053 case OPTION_RELAX_BRANCH:
15054 mips_relax_branch = 1;
15055 break;
15056
15057 case OPTION_NO_RELAX_BRANCH:
15058 mips_relax_branch = 0;
15059 break;
15060
8b10b0b3
MR
15061 case OPTION_IGNORE_BRANCH_ISA:
15062 mips_ignore_branch_isa = TRUE;
15063 break;
15064
15065 case OPTION_NO_IGNORE_BRANCH_ISA:
15066 mips_ignore_branch_isa = FALSE;
15067 break;
15068
833794fc 15069 case OPTION_INSN32:
919731af 15070 file_mips_opts.insn32 = TRUE;
833794fc
MR
15071 break;
15072
15073 case OPTION_NO_INSN32:
919731af 15074 file_mips_opts.insn32 = FALSE;
833794fc
MR
15075 break;
15076
aa6975fb
ILT
15077 case OPTION_MSHARED:
15078 mips_in_shared = TRUE;
15079 break;
15080
15081 case OPTION_MNO_SHARED:
15082 mips_in_shared = FALSE;
15083 break;
15084
aed1a261 15085 case OPTION_MSYM32:
919731af 15086 file_mips_opts.sym32 = TRUE;
aed1a261
RS
15087 break;
15088
15089 case OPTION_MNO_SYM32:
919731af 15090 file_mips_opts.sym32 = FALSE;
aed1a261
RS
15091 break;
15092
252b5132
RH
15093 /* When generating ELF code, we permit -KPIC and -call_shared to
15094 select SVR4_PIC, and -non_shared to select no PIC. This is
15095 intended to be compatible with Irix 5. */
15096 case OPTION_CALL_SHARED:
252b5132 15097 mips_pic = SVR4_PIC;
143d77c5 15098 mips_abicalls = TRUE;
252b5132
RH
15099 break;
15100
861fb55a 15101 case OPTION_CALL_NONPIC:
861fb55a
DJ
15102 mips_pic = NO_PIC;
15103 mips_abicalls = TRUE;
15104 break;
15105
252b5132 15106 case OPTION_NON_SHARED:
252b5132 15107 mips_pic = NO_PIC;
143d77c5 15108 mips_abicalls = FALSE;
252b5132
RH
15109 break;
15110
44075ae2
TS
15111 /* The -xgot option tells the assembler to use 32 bit offsets
15112 when accessing the got in SVR4_PIC mode. It is for Irix
252b5132
RH
15113 compatibility. */
15114 case OPTION_XGOT:
15115 mips_big_got = 1;
15116 break;
15117
15118 case 'G':
6caf9ef4
TS
15119 g_switch_value = atoi (arg);
15120 g_switch_seen = 1;
252b5132
RH
15121 break;
15122
34ba82a8
TS
15123 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
15124 and -mabi=64. */
252b5132 15125 case OPTION_32:
f3ded42a 15126 mips_abi = O32_ABI;
252b5132
RH
15127 break;
15128
e013f690 15129 case OPTION_N32:
316f5878 15130 mips_abi = N32_ABI;
e013f690 15131 break;
252b5132 15132
e013f690 15133 case OPTION_64:
316f5878 15134 mips_abi = N64_ABI;
f43abd2b 15135 if (!support_64bit_objects())
1661c76c 15136 as_fatal (_("no compiled in support for 64 bit object file format"));
252b5132
RH
15137 break;
15138
c97ef257 15139 case OPTION_GP32:
bad1aba3 15140 file_mips_opts.gp = 32;
c97ef257
AH
15141 break;
15142
15143 case OPTION_GP64:
bad1aba3 15144 file_mips_opts.gp = 64;
c97ef257 15145 break;
252b5132 15146
ca4e0257 15147 case OPTION_FP32:
0b35dfee 15148 file_mips_opts.fp = 32;
316f5878
RS
15149 break;
15150
351cdf24
MF
15151 case OPTION_FPXX:
15152 file_mips_opts.fp = 0;
15153 break;
15154
316f5878 15155 case OPTION_FP64:
0b35dfee 15156 file_mips_opts.fp = 64;
ca4e0257
RS
15157 break;
15158
351cdf24
MF
15159 case OPTION_ODD_SPREG:
15160 file_mips_opts.oddspreg = 1;
15161 break;
15162
15163 case OPTION_NO_ODD_SPREG:
15164 file_mips_opts.oddspreg = 0;
15165 break;
15166
037b32b9 15167 case OPTION_SINGLE_FLOAT:
0b35dfee 15168 file_mips_opts.single_float = 1;
037b32b9
AN
15169 break;
15170
15171 case OPTION_DOUBLE_FLOAT:
0b35dfee 15172 file_mips_opts.single_float = 0;
037b32b9
AN
15173 break;
15174
15175 case OPTION_SOFT_FLOAT:
0b35dfee 15176 file_mips_opts.soft_float = 1;
037b32b9
AN
15177 break;
15178
15179 case OPTION_HARD_FLOAT:
0b35dfee 15180 file_mips_opts.soft_float = 0;
037b32b9
AN
15181 break;
15182
252b5132 15183 case OPTION_MABI:
e013f690 15184 if (strcmp (arg, "32") == 0)
316f5878 15185 mips_abi = O32_ABI;
e013f690 15186 else if (strcmp (arg, "o64") == 0)
316f5878 15187 mips_abi = O64_ABI;
e013f690 15188 else if (strcmp (arg, "n32") == 0)
316f5878 15189 mips_abi = N32_ABI;
e013f690
TS
15190 else if (strcmp (arg, "64") == 0)
15191 {
316f5878 15192 mips_abi = N64_ABI;
e013f690 15193 if (! support_64bit_objects())
1661c76c 15194 as_fatal (_("no compiled in support for 64 bit object file "
e013f690
TS
15195 "format"));
15196 }
15197 else if (strcmp (arg, "eabi") == 0)
316f5878 15198 mips_abi = EABI_ABI;
e013f690 15199 else
da0e507f
TS
15200 {
15201 as_fatal (_("invalid abi -mabi=%s"), arg);
15202 return 0;
15203 }
252b5132
RH
15204 break;
15205
6b76fefe 15206 case OPTION_M7000_HILO_FIX:
b34976b6 15207 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
15208 break;
15209
9ee72ff1 15210 case OPTION_MNO_7000_HILO_FIX:
b34976b6 15211 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
15212 break;
15213
ecb4347a 15214 case OPTION_MDEBUG:
b34976b6 15215 mips_flag_mdebug = TRUE;
ecb4347a
DJ
15216 break;
15217
15218 case OPTION_NO_MDEBUG:
b34976b6 15219 mips_flag_mdebug = FALSE;
ecb4347a 15220 break;
dcd410fe
RO
15221
15222 case OPTION_PDR:
15223 mips_flag_pdr = TRUE;
15224 break;
15225
15226 case OPTION_NO_PDR:
15227 mips_flag_pdr = FALSE;
15228 break;
0a44bf69
RS
15229
15230 case OPTION_MVXWORKS_PIC:
15231 mips_pic = VXWORKS_PIC;
15232 break;
ecb4347a 15233
ba92f887
MR
15234 case OPTION_NAN:
15235 if (strcmp (arg, "2008") == 0)
7361da2c 15236 mips_nan2008 = 1;
ba92f887 15237 else if (strcmp (arg, "legacy") == 0)
7361da2c 15238 mips_nan2008 = 0;
ba92f887
MR
15239 else
15240 {
1661c76c 15241 as_fatal (_("invalid NaN setting -mnan=%s"), arg);
ba92f887
MR
15242 return 0;
15243 }
15244 break;
15245
252b5132
RH
15246 default:
15247 return 0;
15248 }
15249
c67a084a
NC
15250 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
15251
252b5132
RH
15252 return 1;
15253}
316f5878 15254\f
919731af 15255/* Set up globals to tune for the ISA or processor described by INFO. */
252b5132 15256
316f5878 15257static void
17a2f251 15258mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
15259{
15260 if (info != 0)
fef14a42 15261 mips_tune = info->cpu;
316f5878 15262}
80cc45a5 15263
34ba82a8 15264
252b5132 15265void
17a2f251 15266mips_after_parse_args (void)
e9670677 15267{
fef14a42
TS
15268 const struct mips_cpu_info *arch_info = 0;
15269 const struct mips_cpu_info *tune_info = 0;
15270
6f2117ba 15271 /* GP relative stuff not working for PE. */
6caf9ef4 15272 if (strncmp (TARGET_OS, "pe", 2) == 0)
e9670677 15273 {
6caf9ef4 15274 if (g_switch_seen && g_switch_value != 0)
1661c76c 15275 as_bad (_("-G not supported in this configuration"));
e9670677
MR
15276 g_switch_value = 0;
15277 }
15278
cac012d6
AO
15279 if (mips_abi == NO_ABI)
15280 mips_abi = MIPS_DEFAULT_ABI;
15281
919731af 15282 /* The following code determines the architecture.
22923709
RS
15283 Similar code was added to GCC 3.3 (see override_options() in
15284 config/mips/mips.c). The GAS and GCC code should be kept in sync
15285 as much as possible. */
e9670677 15286
316f5878 15287 if (mips_arch_string != 0)
fef14a42 15288 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 15289
0b35dfee 15290 if (file_mips_opts.isa != ISA_UNKNOWN)
e9670677 15291 {
0b35dfee 15292 /* Handle -mipsN. At this point, file_mips_opts.isa contains the
fef14a42 15293 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 15294 the -march selection (if any). */
fef14a42 15295 if (arch_info != 0)
e9670677 15296 {
316f5878
RS
15297 /* -march takes precedence over -mipsN, since it is more descriptive.
15298 There's no harm in specifying both as long as the ISA levels
15299 are the same. */
0b35dfee 15300 if (file_mips_opts.isa != arch_info->isa)
1661c76c
RS
15301 as_bad (_("-%s conflicts with the other architecture options,"
15302 " which imply -%s"),
0b35dfee 15303 mips_cpu_info_from_isa (file_mips_opts.isa)->name,
fef14a42 15304 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 15305 }
316f5878 15306 else
0b35dfee 15307 arch_info = mips_cpu_info_from_isa (file_mips_opts.isa);
e9670677
MR
15308 }
15309
fef14a42 15310 if (arch_info == 0)
95bfe26e
MF
15311 {
15312 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
15313 gas_assert (arch_info);
15314 }
e9670677 15315
fef14a42 15316 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
20203fb9 15317 as_bad (_("-march=%s is not compatible with the selected ABI"),
fef14a42
TS
15318 arch_info->name);
15319
919731af 15320 file_mips_opts.arch = arch_info->cpu;
15321 file_mips_opts.isa = arch_info->isa;
3315614d 15322 file_mips_opts.init_ase = arch_info->ase;
919731af 15323
41cee089
FS
15324 /* The EVA Extension has instructions which are only valid when the R6 ISA
15325 is enabled. This sets the ASE_EVA_R6 flag when both EVA and R6 ISA are
15326 present. */
15327 if (((file_mips_opts.ase & ASE_EVA) != 0) && ISA_IS_R6 (file_mips_opts.isa))
15328 file_mips_opts.ase |= ASE_EVA_R6;
15329
919731af 15330 /* Set up initial mips_opts state. */
15331 mips_opts = file_mips_opts;
15332
27c634e0
FN
15333 /* For the R5900 default to `-mfix-r5900' unless the user told otherwise. */
15334 if (!mips_fix_r5900_explicit)
15335 mips_fix_r5900 = file_mips_opts.arch == CPU_R5900;
15336
919731af 15337 /* The register size inference code is now placed in
15338 file_mips_check_options. */
fef14a42 15339
0b35dfee 15340 /* Optimize for file_mips_opts.arch, unless -mtune selects a different
15341 processor. */
fef14a42
TS
15342 if (mips_tune_string != 0)
15343 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 15344
fef14a42
TS
15345 if (tune_info == 0)
15346 mips_set_tune (arch_info);
15347 else
15348 mips_set_tune (tune_info);
e9670677 15349
ecb4347a 15350 if (mips_flag_mdebug < 0)
e8044f35 15351 mips_flag_mdebug = 0;
e9670677
MR
15352}
15353\f
15354void
17a2f251 15355mips_init_after_args (void)
252b5132 15356{
6f2117ba 15357 /* Initialize opcodes. */
252b5132 15358 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 15359 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
15360}
15361
15362long
17a2f251 15363md_pcrel_from (fixS *fixP)
252b5132 15364{
a7ebbfdf 15365 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
6f2117ba 15366
a7ebbfdf
TS
15367 switch (fixP->fx_r_type)
15368 {
df58fc94
RS
15369 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15370 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15371 /* Return the address of the delay slot. */
15372 return addr + 2;
15373
15374 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15375 case BFD_RELOC_MICROMIPS_JMP:
c9775dde 15376 case BFD_RELOC_MIPS16_16_PCREL_S1:
a7ebbfdf 15377 case BFD_RELOC_16_PCREL_S2:
7361da2c
AB
15378 case BFD_RELOC_MIPS_21_PCREL_S2:
15379 case BFD_RELOC_MIPS_26_PCREL_S2:
a7ebbfdf
TS
15380 case BFD_RELOC_MIPS_JMP:
15381 /* Return the address of the delay slot. */
15382 return addr + 4;
df58fc94 15383
51f6035b
MR
15384 case BFD_RELOC_MIPS_18_PCREL_S3:
15385 /* Return the aligned address of the doubleword containing
15386 the instruction. */
15387 return addr & ~7;
15388
a7ebbfdf
TS
15389 default:
15390 return addr;
15391 }
252b5132
RH
15392}
15393
252b5132
RH
15394/* This is called before the symbol table is processed. In order to
15395 work with gcc when using mips-tfile, we must keep all local labels.
15396 However, in other cases, we want to discard them. If we were
15397 called with -g, but we didn't see any debugging information, it may
15398 mean that gcc is smuggling debugging information through to
15399 mips-tfile, in which case we must generate all local labels. */
15400
15401void
17a2f251 15402mips_frob_file_before_adjust (void)
252b5132
RH
15403{
15404#ifndef NO_ECOFF_DEBUGGING
15405 if (ECOFF_DEBUGGING
15406 && mips_debug != 0
15407 && ! ecoff_debugging_seen)
15408 flag_keep_locals = 1;
15409#endif
15410}
15411
3b91255e 15412/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 15413 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
15414 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
15415 relocation operators.
15416
15417 For our purposes, a %lo() expression matches a %got() or %hi()
15418 expression if:
15419
15420 (a) it refers to the same symbol; and
15421 (b) the offset applied in the %lo() expression is no lower than
15422 the offset applied in the %got() or %hi().
15423
15424 (b) allows us to cope with code like:
15425
15426 lui $4,%hi(foo)
15427 lh $4,%lo(foo+2)($4)
15428
15429 ...which is legal on RELA targets, and has a well-defined behaviour
15430 if the user knows that adding 2 to "foo" will not induce a carry to
15431 the high 16 bits.
15432
15433 When several %lo()s match a particular %got() or %hi(), we use the
15434 following rules to distinguish them:
15435
15436 (1) %lo()s with smaller offsets are a better match than %lo()s with
15437 higher offsets.
15438
15439 (2) %lo()s with no matching %got() or %hi() are better than those
15440 that already have a matching %got() or %hi().
15441
15442 (3) later %lo()s are better than earlier %lo()s.
15443
15444 These rules are applied in order.
15445
15446 (1) means, among other things, that %lo()s with identical offsets are
15447 chosen if they exist.
15448
15449 (2) means that we won't associate several high-part relocations with
15450 the same low-part relocation unless there's no alternative. Having
15451 several high parts for the same low part is a GNU extension; this rule
15452 allows careful users to avoid it.
15453
15454 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
15455 with the last high-part relocation being at the front of the list.
15456 It therefore makes sense to choose the last matching low-part
15457 relocation, all other things being equal. It's also easier
15458 to code that way. */
252b5132
RH
15459
15460void
17a2f251 15461mips_frob_file (void)
252b5132
RH
15462{
15463 struct mips_hi_fixup *l;
35903be0 15464 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
252b5132
RH
15465
15466 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15467 {
15468 segment_info_type *seginfo;
3b91255e
RS
15469 bfd_boolean matched_lo_p;
15470 fixS **hi_pos, **lo_pos, **pos;
252b5132 15471
9c2799c2 15472 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 15473
5919d012 15474 /* If a GOT16 relocation turns out to be against a global symbol,
b886a2ab
RS
15475 there isn't supposed to be a matching LO. Ignore %gots against
15476 constants; we'll report an error for those later. */
738e5348 15477 if (got16_reloc_p (l->fixp->fx_r_type)
b886a2ab 15478 && !(l->fixp->fx_addsy
9e009953 15479 && pic_need_relax (l->fixp->fx_addsy)))
5919d012
RS
15480 continue;
15481
15482 /* Check quickly whether the next fixup happens to be a matching %lo. */
15483 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
15484 continue;
15485
252b5132 15486 seginfo = seg_info (l->seg);
252b5132 15487
3b91255e
RS
15488 /* Set HI_POS to the position of this relocation in the chain.
15489 Set LO_POS to the position of the chosen low-part relocation.
15490 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15491 relocation that matches an immediately-preceding high-part
15492 relocation. */
15493 hi_pos = NULL;
15494 lo_pos = NULL;
15495 matched_lo_p = FALSE;
738e5348 15496 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
35903be0 15497
3b91255e
RS
15498 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15499 {
15500 if (*pos == l->fixp)
15501 hi_pos = pos;
15502
35903be0 15503 if ((*pos)->fx_r_type == looking_for_rtype
30cfc97a 15504 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
3b91255e
RS
15505 && (*pos)->fx_offset >= l->fixp->fx_offset
15506 && (lo_pos == NULL
15507 || (*pos)->fx_offset < (*lo_pos)->fx_offset
15508 || (!matched_lo_p
15509 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15510 lo_pos = pos;
15511
15512 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15513 && fixup_has_matching_lo_p (*pos));
15514 }
15515
15516 /* If we found a match, remove the high-part relocation from its
15517 current position and insert it before the low-part relocation.
15518 Make the offsets match so that fixup_has_matching_lo_p()
15519 will return true.
15520
15521 We don't warn about unmatched high-part relocations since some
15522 versions of gcc have been known to emit dead "lui ...%hi(...)"
15523 instructions. */
15524 if (lo_pos != NULL)
15525 {
15526 l->fixp->fx_offset = (*lo_pos)->fx_offset;
15527 if (l->fixp->fx_next != *lo_pos)
252b5132 15528 {
3b91255e
RS
15529 *hi_pos = l->fixp->fx_next;
15530 l->fixp->fx_next = *lo_pos;
15531 *lo_pos = l->fixp;
252b5132 15532 }
252b5132
RH
15533 }
15534 }
15535}
15536
252b5132 15537int
17a2f251 15538mips_force_relocation (fixS *fixp)
252b5132 15539{
ae6063d4 15540 if (generic_force_reloc (fixp))
252b5132
RH
15541 return 1;
15542
df58fc94
RS
15543 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15544 so that the linker relaxation can update targets. */
15545 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15546 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15547 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15548 return 1;
15549
5caa2b07
MR
15550 /* We want to keep BFD_RELOC_16_PCREL_S2 BFD_RELOC_MIPS_21_PCREL_S2
15551 and BFD_RELOC_MIPS_26_PCREL_S2 relocations against MIPS16 and
15552 microMIPS symbols so that we can do cross-mode branch diagnostics
15553 and BAL to JALX conversion by the linker. */
15554 if ((fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
9d862524
MR
15555 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15556 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2)
15557 && fixp->fx_addsy
15558 && ELF_ST_IS_COMPRESSED (S_GET_OTHER (fixp->fx_addsy)))
15559 return 1;
15560
7361da2c 15561 /* We want all PC-relative relocations to be kept for R6 relaxation. */
912815f0 15562 if (ISA_IS_R6 (file_mips_opts.isa)
7361da2c
AB
15563 && (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
15564 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15565 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
15566 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
15567 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
15568 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
15569 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL))
15570 return 1;
15571
3e722fb5 15572 return 0;
252b5132
RH
15573}
15574
b416ba9b
MR
15575/* Implement TC_FORCE_RELOCATION_ABS. */
15576
15577bfd_boolean
15578mips_force_relocation_abs (fixS *fixp)
15579{
15580 if (generic_force_reloc (fixp))
15581 return TRUE;
15582
15583 /* These relocations do not have enough bits in the in-place addend
15584 to hold an arbitrary absolute section's offset. */
15585 if (HAVE_IN_PLACE_ADDENDS && limited_pcrel_reloc_p (fixp->fx_r_type))
15586 return TRUE;
15587
15588 return FALSE;
15589}
15590
b886a2ab
RS
15591/* Read the instruction associated with RELOC from BUF. */
15592
15593static unsigned int
15594read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15595{
15596 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15597 return read_compressed_insn (buf, 4);
15598 else
15599 return read_insn (buf);
15600}
15601
15602/* Write instruction INSN to BUF, given that it has been relocated
15603 by RELOC. */
15604
15605static void
15606write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15607 unsigned long insn)
15608{
15609 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15610 write_compressed_insn (buf, insn, 4);
15611 else
15612 write_insn (buf, insn);
15613}
15614
9d862524
MR
15615/* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15616 to a symbol in another ISA mode, which cannot be converted to JALX. */
15617
15618static bfd_boolean
15619fix_bad_cross_mode_jump_p (fixS *fixP)
15620{
15621 unsigned long opcode;
15622 int other;
15623 char *buf;
15624
15625 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15626 return FALSE;
15627
15628 other = S_GET_OTHER (fixP->fx_addsy);
15629 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15630 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15631 switch (fixP->fx_r_type)
15632 {
15633 case BFD_RELOC_MIPS_JMP:
15634 return opcode != 0x1d && opcode != 0x03 && ELF_ST_IS_COMPRESSED (other);
15635 case BFD_RELOC_MICROMIPS_JMP:
15636 return opcode != 0x3c && opcode != 0x3d && !ELF_ST_IS_MICROMIPS (other);
15637 default:
15638 return FALSE;
15639 }
15640}
15641
15642/* Return TRUE if the instruction pointed to by FIXP is an invalid JALX
15643 jump to a symbol in the same ISA mode. */
15644
15645static bfd_boolean
15646fix_bad_same_mode_jalx_p (fixS *fixP)
15647{
15648 unsigned long opcode;
15649 int other;
15650 char *buf;
15651
15652 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15653 return FALSE;
15654
15655 other = S_GET_OTHER (fixP->fx_addsy);
15656 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15657 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15658 switch (fixP->fx_r_type)
15659 {
15660 case BFD_RELOC_MIPS_JMP:
15661 return opcode == 0x1d && !ELF_ST_IS_COMPRESSED (other);
15662 case BFD_RELOC_MIPS16_JMP:
15663 return opcode == 0x07 && ELF_ST_IS_COMPRESSED (other);
15664 case BFD_RELOC_MICROMIPS_JMP:
15665 return opcode == 0x3c && ELF_ST_IS_COMPRESSED (other);
15666 default:
15667 return FALSE;
15668 }
15669}
15670
15671/* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15672 to a symbol whose value plus addend is not aligned according to the
15673 ultimate (after linker relaxation) jump instruction's immediate field
15674 requirement, either to (1 << SHIFT), or, for jumps from microMIPS to
15675 regular MIPS code, to (1 << 2). */
15676
15677static bfd_boolean
15678fix_bad_misaligned_jump_p (fixS *fixP, int shift)
15679{
15680 bfd_boolean micro_to_mips_p;
15681 valueT val;
15682 int other;
15683
15684 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15685 return FALSE;
15686
15687 other = S_GET_OTHER (fixP->fx_addsy);
15688 val = S_GET_VALUE (fixP->fx_addsy) | ELF_ST_IS_COMPRESSED (other);
15689 val += fixP->fx_offset;
15690 micro_to_mips_p = (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15691 && !ELF_ST_IS_MICROMIPS (other));
15692 return ((val & ((1 << (micro_to_mips_p ? 2 : shift)) - 1))
15693 != ELF_ST_IS_COMPRESSED (other));
15694}
15695
15696/* Return TRUE if the instruction pointed to by FIXP is an invalid branch
15697 to a symbol whose annotation indicates another ISA mode. For absolute
a6ebf616
MR
15698 symbols check the ISA bit instead.
15699
15700 We accept BFD_RELOC_16_PCREL_S2 relocations against MIPS16 and microMIPS
15701 symbols or BFD_RELOC_MICROMIPS_16_PCREL_S1 relocations against regular
15702 MIPS symbols and associated with BAL instructions as these instructions
de194d85 15703 may be converted to JALX by the linker. */
9d862524
MR
15704
15705static bfd_boolean
15706fix_bad_cross_mode_branch_p (fixS *fixP)
15707{
15708 bfd_boolean absolute_p;
15709 unsigned long opcode;
15710 asection *symsec;
15711 valueT val;
15712 int other;
15713 char *buf;
15714
8b10b0b3
MR
15715 if (mips_ignore_branch_isa)
15716 return FALSE;
15717
9d862524
MR
15718 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15719 return FALSE;
15720
15721 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15722 absolute_p = bfd_is_abs_section (symsec);
15723
15724 val = S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset;
15725 other = S_GET_OTHER (fixP->fx_addsy);
15726
15727 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15728 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 16;
15729 switch (fixP->fx_r_type)
15730 {
15731 case BFD_RELOC_16_PCREL_S2:
a6ebf616
MR
15732 return ((absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other))
15733 && opcode != 0x0411);
15734 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15735 return ((absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other))
15736 && opcode != 0x4060);
9d862524
MR
15737 case BFD_RELOC_MIPS_21_PCREL_S2:
15738 case BFD_RELOC_MIPS_26_PCREL_S2:
15739 return absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other);
15740 case BFD_RELOC_MIPS16_16_PCREL_S1:
15741 return absolute_p ? !(val & 1) : !ELF_ST_IS_MIPS16 (other);
15742 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15743 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
9d862524
MR
15744 return absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other);
15745 default:
15746 abort ();
15747 }
15748}
15749
15750/* Return TRUE if the symbol plus addend associated with a regular MIPS
15751 branch instruction pointed to by FIXP is not aligned according to the
15752 branch instruction's immediate field requirement. We need the addend
15753 to preserve the ISA bit and also the sum must not have bit 2 set. We
15754 must explicitly OR in the ISA bit from symbol annotation as the bit
15755 won't be set in the symbol's value then. */
15756
15757static bfd_boolean
15758fix_bad_misaligned_branch_p (fixS *fixP)
15759{
15760 bfd_boolean absolute_p;
15761 asection *symsec;
15762 valueT isa_bit;
15763 valueT val;
15764 valueT off;
15765 int other;
15766
15767 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15768 return FALSE;
15769
15770 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15771 absolute_p = bfd_is_abs_section (symsec);
15772
15773 val = S_GET_VALUE (fixP->fx_addsy);
15774 other = S_GET_OTHER (fixP->fx_addsy);
15775 off = fixP->fx_offset;
15776
15777 isa_bit = absolute_p ? (val + off) & 1 : ELF_ST_IS_COMPRESSED (other);
15778 val |= ELF_ST_IS_COMPRESSED (other);
15779 val += off;
15780 return (val & 0x3) != isa_bit;
15781}
15782
52031738
FS
15783/* Calculate the relocation target by masking off ISA mode bit before
15784 combining symbol and addend. */
15785
15786static valueT
15787fix_bad_misaligned_address (fixS *fixP)
15788{
15789 valueT val;
15790 valueT off;
15791 unsigned isa_mode;
15792 gas_assert (fixP != NULL && fixP->fx_addsy != NULL);
15793 val = S_GET_VALUE (fixP->fx_addsy);
15794 off = fixP->fx_offset;
15795 isa_mode = (ELF_ST_IS_COMPRESSED (S_GET_OTHER (fixP->fx_addsy))
15796 ? 1 : 0);
15797
15798 return ((val & ~isa_mode) + off);
15799}
15800
9d862524
MR
15801/* Make the necessary checks on a regular MIPS branch pointed to by FIXP
15802 and its calculated value VAL. */
15803
15804static void
15805fix_validate_branch (fixS *fixP, valueT val)
15806{
15807 if (fixP->fx_done && (val & 0x3) != 0)
15808 as_bad_where (fixP->fx_file, fixP->fx_line,
15809 _("branch to misaligned address (0x%lx)"),
15810 (long) (val + md_pcrel_from (fixP)));
15811 else if (fix_bad_cross_mode_branch_p (fixP))
15812 as_bad_where (fixP->fx_file, fixP->fx_line,
15813 _("branch to a symbol in another ISA mode"));
15814 else if (fix_bad_misaligned_branch_p (fixP))
15815 as_bad_where (fixP->fx_file, fixP->fx_line,
15816 _("branch to misaligned address (0x%lx)"),
52031738 15817 (long) fix_bad_misaligned_address (fixP));
9d862524
MR
15818 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x3) != 0)
15819 as_bad_where (fixP->fx_file, fixP->fx_line,
15820 _("cannot encode misaligned addend "
15821 "in the relocatable field (0x%lx)"),
15822 (long) fixP->fx_offset);
15823}
15824
252b5132
RH
15825/* Apply a fixup to the object file. */
15826
94f592af 15827void
55cf6793 15828md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 15829{
4d68580a 15830 char *buf;
b886a2ab 15831 unsigned long insn;
a7ebbfdf 15832 reloc_howto_type *howto;
252b5132 15833
d56a8dda
RS
15834 if (fixP->fx_pcrel)
15835 switch (fixP->fx_r_type)
15836 {
15837 case BFD_RELOC_16_PCREL_S2:
c9775dde 15838 case BFD_RELOC_MIPS16_16_PCREL_S1:
d56a8dda
RS
15839 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15840 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15841 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15842 case BFD_RELOC_32_PCREL:
7361da2c
AB
15843 case BFD_RELOC_MIPS_21_PCREL_S2:
15844 case BFD_RELOC_MIPS_26_PCREL_S2:
15845 case BFD_RELOC_MIPS_18_PCREL_S3:
15846 case BFD_RELOC_MIPS_19_PCREL_S2:
15847 case BFD_RELOC_HI16_S_PCREL:
15848 case BFD_RELOC_LO16_PCREL:
d56a8dda
RS
15849 break;
15850
15851 case BFD_RELOC_32:
15852 fixP->fx_r_type = BFD_RELOC_32_PCREL;
15853 break;
15854
15855 default:
15856 as_bad_where (fixP->fx_file, fixP->fx_line,
15857 _("PC-relative reference to a different section"));
15858 break;
15859 }
15860
15861 /* Handle BFD_RELOC_8, since it's easy. Punt on other bfd relocations
15862 that have no MIPS ELF equivalent. */
15863 if (fixP->fx_r_type != BFD_RELOC_8)
15864 {
15865 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15866 if (!howto)
15867 return;
15868 }
65551fa4 15869
df58fc94
RS
15870 gas_assert (fixP->fx_size == 2
15871 || fixP->fx_size == 4
d56a8dda 15872 || fixP->fx_r_type == BFD_RELOC_8
90ecf173
MR
15873 || fixP->fx_r_type == BFD_RELOC_16
15874 || fixP->fx_r_type == BFD_RELOC_64
15875 || fixP->fx_r_type == BFD_RELOC_CTOR
15876 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
df58fc94 15877 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
90ecf173
MR
15878 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15879 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
2f0c68f2
CM
15880 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64
15881 || fixP->fx_r_type == BFD_RELOC_NONE);
252b5132 15882
4d68580a 15883 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132 15884
b1dca8ee
RS
15885 /* Don't treat parts of a composite relocation as done. There are two
15886 reasons for this:
15887
15888 (1) The second and third parts will be against 0 (RSS_UNDEF) but
15889 should nevertheless be emitted if the first part is.
15890
15891 (2) In normal usage, composite relocations are never assembly-time
15892 constants. The easiest way of dealing with the pathological
15893 exceptions is to generate a relocation against STN_UNDEF and
15894 leave everything up to the linker. */
3994f87e 15895 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
252b5132
RH
15896 fixP->fx_done = 1;
15897
15898 switch (fixP->fx_r_type)
15899 {
3f98094e
DJ
15900 case BFD_RELOC_MIPS_TLS_GD:
15901 case BFD_RELOC_MIPS_TLS_LDM:
741d6ea8
JM
15902 case BFD_RELOC_MIPS_TLS_DTPREL32:
15903 case BFD_RELOC_MIPS_TLS_DTPREL64:
3f98094e
DJ
15904 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15905 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15906 case BFD_RELOC_MIPS_TLS_GOTTPREL:
d0f13682
CLT
15907 case BFD_RELOC_MIPS_TLS_TPREL32:
15908 case BFD_RELOC_MIPS_TLS_TPREL64:
3f98094e
DJ
15909 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15910 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
df58fc94
RS
15911 case BFD_RELOC_MICROMIPS_TLS_GD:
15912 case BFD_RELOC_MICROMIPS_TLS_LDM:
15913 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15914 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15915 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15916 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15917 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
d0f13682
CLT
15918 case BFD_RELOC_MIPS16_TLS_GD:
15919 case BFD_RELOC_MIPS16_TLS_LDM:
15920 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15921 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15922 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15923 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15924 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
4512dafa
MR
15925 if (fixP->fx_addsy)
15926 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15927 else
15928 as_bad_where (fixP->fx_file, fixP->fx_line,
15929 _("TLS relocation against a constant"));
15930 break;
3f98094e 15931
252b5132 15932 case BFD_RELOC_MIPS_JMP:
9d862524
MR
15933 case BFD_RELOC_MIPS16_JMP:
15934 case BFD_RELOC_MICROMIPS_JMP:
15935 {
15936 int shift;
15937
15938 gas_assert (!fixP->fx_done);
15939
15940 /* Shift is 2, unusually, for microMIPS JALX. */
15941 if (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15942 && (read_compressed_insn (buf, 4) >> 26) != 0x3c)
15943 shift = 1;
15944 else
15945 shift = 2;
15946
15947 if (fix_bad_cross_mode_jump_p (fixP))
15948 as_bad_where (fixP->fx_file, fixP->fx_line,
15949 _("jump to a symbol in another ISA mode"));
15950 else if (fix_bad_same_mode_jalx_p (fixP))
15951 as_bad_where (fixP->fx_file, fixP->fx_line,
15952 _("JALX to a symbol in the same ISA mode"));
15953 else if (fix_bad_misaligned_jump_p (fixP, shift))
15954 as_bad_where (fixP->fx_file, fixP->fx_line,
15955 _("jump to misaligned address (0x%lx)"),
52031738 15956 (long) fix_bad_misaligned_address (fixP));
9d862524
MR
15957 else if (HAVE_IN_PLACE_ADDENDS
15958 && (fixP->fx_offset & ((1 << shift) - 1)) != 0)
15959 as_bad_where (fixP->fx_file, fixP->fx_line,
15960 _("cannot encode misaligned addend "
15961 "in the relocatable field (0x%lx)"),
15962 (long) fixP->fx_offset);
15963 }
15964 /* Fall through. */
15965
e369bcce
TS
15966 case BFD_RELOC_MIPS_SHIFT5:
15967 case BFD_RELOC_MIPS_SHIFT6:
15968 case BFD_RELOC_MIPS_GOT_DISP:
15969 case BFD_RELOC_MIPS_GOT_PAGE:
15970 case BFD_RELOC_MIPS_GOT_OFST:
15971 case BFD_RELOC_MIPS_SUB:
15972 case BFD_RELOC_MIPS_INSERT_A:
15973 case BFD_RELOC_MIPS_INSERT_B:
15974 case BFD_RELOC_MIPS_DELETE:
15975 case BFD_RELOC_MIPS_HIGHEST:
15976 case BFD_RELOC_MIPS_HIGHER:
15977 case BFD_RELOC_MIPS_SCN_DISP:
15978 case BFD_RELOC_MIPS_REL16:
15979 case BFD_RELOC_MIPS_RELGOT:
15980 case BFD_RELOC_MIPS_JALR:
252b5132
RH
15981 case BFD_RELOC_HI16:
15982 case BFD_RELOC_HI16_S:
b886a2ab 15983 case BFD_RELOC_LO16:
cdf6fd85 15984 case BFD_RELOC_GPREL16:
252b5132
RH
15985 case BFD_RELOC_MIPS_LITERAL:
15986 case BFD_RELOC_MIPS_CALL16:
15987 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 15988 case BFD_RELOC_GPREL32:
252b5132
RH
15989 case BFD_RELOC_MIPS_GOT_HI16:
15990 case BFD_RELOC_MIPS_GOT_LO16:
15991 case BFD_RELOC_MIPS_CALL_HI16:
15992 case BFD_RELOC_MIPS_CALL_LO16:
41947d9e
MR
15993 case BFD_RELOC_HI16_S_PCREL:
15994 case BFD_RELOC_LO16_PCREL:
252b5132 15995 case BFD_RELOC_MIPS16_GPREL:
738e5348
RS
15996 case BFD_RELOC_MIPS16_GOT16:
15997 case BFD_RELOC_MIPS16_CALL16:
d6f16593
MR
15998 case BFD_RELOC_MIPS16_HI16:
15999 case BFD_RELOC_MIPS16_HI16_S:
b886a2ab 16000 case BFD_RELOC_MIPS16_LO16:
df58fc94
RS
16001 case BFD_RELOC_MICROMIPS_GOT_DISP:
16002 case BFD_RELOC_MICROMIPS_GOT_PAGE:
16003 case BFD_RELOC_MICROMIPS_GOT_OFST:
16004 case BFD_RELOC_MICROMIPS_SUB:
16005 case BFD_RELOC_MICROMIPS_HIGHEST:
16006 case BFD_RELOC_MICROMIPS_HIGHER:
16007 case BFD_RELOC_MICROMIPS_SCN_DISP:
16008 case BFD_RELOC_MICROMIPS_JALR:
16009 case BFD_RELOC_MICROMIPS_HI16:
16010 case BFD_RELOC_MICROMIPS_HI16_S:
b886a2ab 16011 case BFD_RELOC_MICROMIPS_LO16:
df58fc94
RS
16012 case BFD_RELOC_MICROMIPS_GPREL16:
16013 case BFD_RELOC_MICROMIPS_LITERAL:
16014 case BFD_RELOC_MICROMIPS_CALL16:
16015 case BFD_RELOC_MICROMIPS_GOT16:
16016 case BFD_RELOC_MICROMIPS_GOT_HI16:
16017 case BFD_RELOC_MICROMIPS_GOT_LO16:
16018 case BFD_RELOC_MICROMIPS_CALL_HI16:
16019 case BFD_RELOC_MICROMIPS_CALL_LO16:
067ec077 16020 case BFD_RELOC_MIPS_EH:
b886a2ab
RS
16021 if (fixP->fx_done)
16022 {
16023 offsetT value;
16024
16025 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
16026 {
16027 insn = read_reloc_insn (buf, fixP->fx_r_type);
16028 if (mips16_reloc_p (fixP->fx_r_type))
16029 insn |= mips16_immed_extend (value, 16);
16030 else
16031 insn |= (value & 0xffff);
16032 write_reloc_insn (buf, fixP->fx_r_type, insn);
16033 }
16034 else
16035 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 16036 _("unsupported constant in relocation"));
b886a2ab 16037 }
252b5132
RH
16038 break;
16039
252b5132
RH
16040 case BFD_RELOC_64:
16041 /* This is handled like BFD_RELOC_32, but we output a sign
16042 extended value if we are only 32 bits. */
3e722fb5 16043 if (fixP->fx_done)
252b5132
RH
16044 {
16045 if (8 <= sizeof (valueT))
4d68580a 16046 md_number_to_chars (buf, *valP, 8);
252b5132
RH
16047 else
16048 {
a7ebbfdf 16049 valueT hiv;
252b5132 16050
a7ebbfdf 16051 if ((*valP & 0x80000000) != 0)
252b5132
RH
16052 hiv = 0xffffffff;
16053 else
16054 hiv = 0;
4d68580a
RS
16055 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
16056 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
252b5132
RH
16057 }
16058 }
16059 break;
16060
056350c6 16061 case BFD_RELOC_RVA:
252b5132 16062 case BFD_RELOC_32:
b47468a6 16063 case BFD_RELOC_32_PCREL:
252b5132 16064 case BFD_RELOC_16:
d56a8dda 16065 case BFD_RELOC_8:
252b5132 16066 /* If we are deleting this reloc entry, we must fill in the
54f4ddb3
TS
16067 value now. This can happen if we have a .word which is not
16068 resolved when it appears but is later defined. */
252b5132 16069 if (fixP->fx_done)
4d68580a 16070 md_number_to_chars (buf, *valP, fixP->fx_size);
252b5132
RH
16071 break;
16072
7361da2c 16073 case BFD_RELOC_MIPS_21_PCREL_S2:
9d862524 16074 fix_validate_branch (fixP, *valP);
41947d9e
MR
16075 if (!fixP->fx_done)
16076 break;
16077
16078 if (*valP + 0x400000 <= 0x7fffff)
16079 {
16080 insn = read_insn (buf);
16081 insn |= (*valP >> 2) & 0x1fffff;
16082 write_insn (buf, insn);
16083 }
16084 else
16085 as_bad_where (fixP->fx_file, fixP->fx_line,
16086 _("branch out of range"));
16087 break;
16088
7361da2c 16089 case BFD_RELOC_MIPS_26_PCREL_S2:
9d862524 16090 fix_validate_branch (fixP, *valP);
41947d9e
MR
16091 if (!fixP->fx_done)
16092 break;
7361da2c 16093
41947d9e
MR
16094 if (*valP + 0x8000000 <= 0xfffffff)
16095 {
16096 insn = read_insn (buf);
16097 insn |= (*valP >> 2) & 0x3ffffff;
16098 write_insn (buf, insn);
16099 }
16100 else
16101 as_bad_where (fixP->fx_file, fixP->fx_line,
16102 _("branch out of range"));
7361da2c
AB
16103 break;
16104
16105 case BFD_RELOC_MIPS_18_PCREL_S3:
717ba204 16106 if (fixP->fx_addsy && (S_GET_VALUE (fixP->fx_addsy) & 0x7) != 0)
7361da2c 16107 as_bad_where (fixP->fx_file, fixP->fx_line,
0866e94c
MF
16108 _("PC-relative access using misaligned symbol (%lx)"),
16109 (long) S_GET_VALUE (fixP->fx_addsy));
16110 if ((fixP->fx_offset & 0x7) != 0)
16111 as_bad_where (fixP->fx_file, fixP->fx_line,
16112 _("PC-relative access using misaligned offset (%lx)"),
16113 (long) fixP->fx_offset);
41947d9e
MR
16114 if (!fixP->fx_done)
16115 break;
7361da2c 16116
41947d9e
MR
16117 if (*valP + 0x100000 <= 0x1fffff)
16118 {
16119 insn = read_insn (buf);
16120 insn |= (*valP >> 3) & 0x3ffff;
16121 write_insn (buf, insn);
16122 }
16123 else
16124 as_bad_where (fixP->fx_file, fixP->fx_line,
16125 _("PC-relative access out of range"));
7361da2c
AB
16126 break;
16127
16128 case BFD_RELOC_MIPS_19_PCREL_S2:
16129 if ((*valP & 0x3) != 0)
16130 as_bad_where (fixP->fx_file, fixP->fx_line,
16131 _("PC-relative access to misaligned address (%lx)"),
717ba204 16132 (long) *valP);
41947d9e
MR
16133 if (!fixP->fx_done)
16134 break;
7361da2c 16135
41947d9e
MR
16136 if (*valP + 0x100000 <= 0x1fffff)
16137 {
16138 insn = read_insn (buf);
16139 insn |= (*valP >> 2) & 0x7ffff;
16140 write_insn (buf, insn);
16141 }
16142 else
16143 as_bad_where (fixP->fx_file, fixP->fx_line,
16144 _("PC-relative access out of range"));
7361da2c
AB
16145 break;
16146
252b5132 16147 case BFD_RELOC_16_PCREL_S2:
9d862524 16148 fix_validate_branch (fixP, *valP);
cb56d3d3 16149
54f4ddb3
TS
16150 /* We need to save the bits in the instruction since fixup_segment()
16151 might be deleting the relocation entry (i.e., a branch within
16152 the current segment). */
a7ebbfdf 16153 if (! fixP->fx_done)
bb2d6cd7 16154 break;
252b5132 16155
54f4ddb3 16156 /* Update old instruction data. */
4d68580a 16157 insn = read_insn (buf);
252b5132 16158
a7ebbfdf
TS
16159 if (*valP + 0x20000 <= 0x3ffff)
16160 {
16161 insn |= (*valP >> 2) & 0xffff;
4d68580a 16162 write_insn (buf, insn);
a7ebbfdf 16163 }
ce8ad872 16164 else if (fixP->fx_tcbit2
a7ebbfdf
TS
16165 && fixP->fx_done
16166 && fixP->fx_frag->fr_address >= text_section->vma
16167 && (fixP->fx_frag->fr_address
fd361982 16168 < text_section->vma + bfd_section_size (text_section))
a7ebbfdf
TS
16169 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
16170 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
16171 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
16172 {
16173 /* The branch offset is too large. If this is an
16174 unconditional branch, and we are not generating PIC code,
16175 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
16176 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
16177 insn = 0x0c000000; /* jal */
252b5132 16178 else
a7ebbfdf
TS
16179 insn = 0x08000000; /* j */
16180 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
16181 fixP->fx_done = 0;
16182 fixP->fx_addsy = section_symbol (text_section);
16183 *valP += md_pcrel_from (fixP);
4d68580a 16184 write_insn (buf, insn);
a7ebbfdf
TS
16185 }
16186 else
16187 {
16188 /* If we got here, we have branch-relaxation disabled,
16189 and there's nothing we can do to fix this instruction
16190 without turning it into a longer sequence. */
16191 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 16192 _("branch out of range"));
252b5132 16193 }
252b5132
RH
16194 break;
16195
c9775dde 16196 case BFD_RELOC_MIPS16_16_PCREL_S1:
df58fc94
RS
16197 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
16198 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
16199 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
96e9ba5f 16200 gas_assert (!fixP->fx_done);
9d862524
MR
16201 if (fix_bad_cross_mode_branch_p (fixP))
16202 as_bad_where (fixP->fx_file, fixP->fx_line,
16203 _("branch to a symbol in another ISA mode"));
16204 else if (fixP->fx_addsy
16205 && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
16206 && !bfd_is_abs_section (S_GET_SEGMENT (fixP->fx_addsy))
16207 && (fixP->fx_offset & 0x1) != 0)
16208 as_bad_where (fixP->fx_file, fixP->fx_line,
16209 _("branch to misaligned address (0x%lx)"),
52031738 16210 (long) fix_bad_misaligned_address (fixP));
9d862524
MR
16211 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x1) != 0)
16212 as_bad_where (fixP->fx_file, fixP->fx_line,
16213 _("cannot encode misaligned addend "
16214 "in the relocatable field (0x%lx)"),
16215 (long) fixP->fx_offset);
df58fc94
RS
16216 break;
16217
252b5132
RH
16218 case BFD_RELOC_VTABLE_INHERIT:
16219 fixP->fx_done = 0;
16220 if (fixP->fx_addsy
16221 && !S_IS_DEFINED (fixP->fx_addsy)
16222 && !S_IS_WEAK (fixP->fx_addsy))
16223 S_SET_WEAK (fixP->fx_addsy);
16224 break;
16225
2f0c68f2 16226 case BFD_RELOC_NONE:
252b5132
RH
16227 case BFD_RELOC_VTABLE_ENTRY:
16228 fixP->fx_done = 0;
16229 break;
16230
16231 default:
b37df7c4 16232 abort ();
252b5132 16233 }
a7ebbfdf
TS
16234
16235 /* Remember value for tc_gen_reloc. */
16236 fixP->fx_addnumber = *valP;
252b5132
RH
16237}
16238
252b5132 16239static symbolS *
17a2f251 16240get_symbol (void)
252b5132
RH
16241{
16242 int c;
16243 char *name;
16244 symbolS *p;
16245
d02603dc 16246 c = get_symbol_name (&name);
252b5132 16247 p = (symbolS *) symbol_find_or_make (name);
d02603dc 16248 (void) restore_line_pointer (c);
252b5132
RH
16249 return p;
16250}
16251
742a56fe
RS
16252/* Align the current frag to a given power of two. If a particular
16253 fill byte should be used, FILL points to an integer that contains
16254 that byte, otherwise FILL is null.
16255
462427c4
RS
16256 This function used to have the comment:
16257
16258 The MIPS assembler also automatically adjusts any preceding label.
16259
16260 The implementation therefore applied the adjustment to a maximum of
16261 one label. However, other label adjustments are applied to batches
16262 of labels, and adjusting just one caused problems when new labels
16263 were added for the sake of debugging or unwind information.
16264 We therefore adjust all preceding labels (given as LABELS) instead. */
252b5132
RH
16265
16266static void
462427c4 16267mips_align (int to, int *fill, struct insn_label_list *labels)
252b5132 16268{
7d10b47d 16269 mips_emit_delays ();
df58fc94 16270 mips_record_compressed_mode ();
742a56fe
RS
16271 if (fill == NULL && subseg_text_p (now_seg))
16272 frag_align_code (to, 0);
16273 else
16274 frag_align (to, fill ? *fill : 0, 0);
252b5132 16275 record_alignment (now_seg, to);
770c0151 16276 mips_move_labels (labels, subseg_text_p (now_seg));
252b5132
RH
16277}
16278
16279/* Align to a given power of two. .align 0 turns off the automatic
16280 alignment used by the data creating pseudo-ops. */
16281
16282static void
17a2f251 16283s_align (int x ATTRIBUTE_UNUSED)
252b5132 16284{
742a56fe 16285 int temp, fill_value, *fill_ptr;
49954fb4 16286 long max_alignment = 28;
252b5132 16287
54f4ddb3 16288 /* o Note that the assembler pulls down any immediately preceding label
252b5132 16289 to the aligned address.
54f4ddb3 16290 o It's not documented but auto alignment is reinstated by
252b5132 16291 a .align pseudo instruction.
54f4ddb3 16292 o Note also that after auto alignment is turned off the mips assembler
252b5132 16293 issues an error on attempt to assemble an improperly aligned data item.
54f4ddb3 16294 We don't. */
252b5132
RH
16295
16296 temp = get_absolute_expression ();
16297 if (temp > max_alignment)
1661c76c 16298 as_bad (_("alignment too large, %d assumed"), temp = max_alignment);
252b5132
RH
16299 else if (temp < 0)
16300 {
1661c76c 16301 as_warn (_("alignment negative, 0 assumed"));
252b5132
RH
16302 temp = 0;
16303 }
16304 if (*input_line_pointer == ',')
16305 {
f9419b05 16306 ++input_line_pointer;
742a56fe
RS
16307 fill_value = get_absolute_expression ();
16308 fill_ptr = &fill_value;
252b5132
RH
16309 }
16310 else
742a56fe 16311 fill_ptr = 0;
252b5132
RH
16312 if (temp)
16313 {
a8dbcb85
TS
16314 segment_info_type *si = seg_info (now_seg);
16315 struct insn_label_list *l = si->label_list;
54f4ddb3 16316 /* Auto alignment should be switched on by next section change. */
252b5132 16317 auto_align = 1;
462427c4 16318 mips_align (temp, fill_ptr, l);
252b5132
RH
16319 }
16320 else
16321 {
16322 auto_align = 0;
16323 }
16324
16325 demand_empty_rest_of_line ();
16326}
16327
252b5132 16328static void
17a2f251 16329s_change_sec (int sec)
252b5132
RH
16330{
16331 segT seg;
16332
252b5132
RH
16333 /* The ELF backend needs to know that we are changing sections, so
16334 that .previous works correctly. We could do something like check
b6ff326e 16335 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
16336 as it would not be appropriate to use it in the section changing
16337 functions in read.c, since obj-elf.c intercepts those. FIXME:
16338 This should be cleaner, somehow. */
f3ded42a 16339 obj_elf_section_change_hook ();
252b5132 16340
7d10b47d 16341 mips_emit_delays ();
6a32d874 16342
252b5132
RH
16343 switch (sec)
16344 {
16345 case 't':
16346 s_text (0);
16347 break;
16348 case 'd':
16349 s_data (0);
16350 break;
16351 case 'b':
16352 subseg_set (bss_section, (subsegT) get_absolute_expression ());
16353 demand_empty_rest_of_line ();
16354 break;
16355
16356 case 'r':
4d0d148d
TS
16357 seg = subseg_new (RDATA_SECTION_NAME,
16358 (subsegT) get_absolute_expression ());
fd361982
AM
16359 bfd_set_section_flags (seg, (SEC_ALLOC | SEC_LOAD | SEC_READONLY
16360 | SEC_RELOC | SEC_DATA));
f3ded42a
RS
16361 if (strncmp (TARGET_OS, "elf", 3) != 0)
16362 record_alignment (seg, 4);
4d0d148d 16363 demand_empty_rest_of_line ();
252b5132
RH
16364 break;
16365
16366 case 's':
4d0d148d 16367 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
a4dd6c97
AM
16368 bfd_set_section_flags (seg, (SEC_ALLOC | SEC_LOAD | SEC_RELOC
16369 | SEC_DATA | SEC_SMALL_DATA));
f3ded42a
RS
16370 if (strncmp (TARGET_OS, "elf", 3) != 0)
16371 record_alignment (seg, 4);
4d0d148d
TS
16372 demand_empty_rest_of_line ();
16373 break;
998b3c36
MR
16374
16375 case 'B':
16376 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
a4dd6c97 16377 bfd_set_section_flags (seg, SEC_ALLOC | SEC_SMALL_DATA);
f3ded42a
RS
16378 if (strncmp (TARGET_OS, "elf", 3) != 0)
16379 record_alignment (seg, 4);
998b3c36
MR
16380 demand_empty_rest_of_line ();
16381 break;
252b5132
RH
16382 }
16383
16384 auto_align = 1;
16385}
b34976b6 16386
cca86cc8 16387void
17a2f251 16388s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 16389{
d02603dc 16390 char *saved_ilp;
cca86cc8 16391 char *section_name;
d02603dc 16392 char c, endc;
684022ea 16393 char next_c = 0;
cca86cc8
SC
16394 int section_type;
16395 int section_flag;
16396 int section_entry_size;
16397 int section_alignment;
b34976b6 16398
d02603dc
NC
16399 saved_ilp = input_line_pointer;
16400 endc = get_symbol_name (&section_name);
16401 c = (endc == '"' ? input_line_pointer[1] : endc);
a816d1ed 16402 if (c)
d02603dc 16403 next_c = input_line_pointer [(endc == '"' ? 2 : 1)];
cca86cc8 16404
4cf0dd0d
TS
16405 /* Do we have .section Name<,"flags">? */
16406 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 16407 {
d02603dc
NC
16408 /* Just after name is now '\0'. */
16409 (void) restore_line_pointer (endc);
16410 input_line_pointer = saved_ilp;
cca86cc8
SC
16411 obj_elf_section (ignore);
16412 return;
16413 }
d02603dc
NC
16414
16415 section_name = xstrdup (section_name);
16416 c = restore_line_pointer (endc);
16417
cca86cc8
SC
16418 input_line_pointer++;
16419
16420 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
16421 if (c == ',')
16422 section_type = get_absolute_expression ();
16423 else
16424 section_type = 0;
d02603dc 16425
cca86cc8
SC
16426 if (*input_line_pointer++ == ',')
16427 section_flag = get_absolute_expression ();
16428 else
16429 section_flag = 0;
d02603dc 16430
cca86cc8
SC
16431 if (*input_line_pointer++ == ',')
16432 section_entry_size = get_absolute_expression ();
16433 else
16434 section_entry_size = 0;
d02603dc 16435
cca86cc8
SC
16436 if (*input_line_pointer++ == ',')
16437 section_alignment = get_absolute_expression ();
16438 else
16439 section_alignment = 0;
d02603dc 16440
87975d2a
AM
16441 /* FIXME: really ignore? */
16442 (void) section_alignment;
cca86cc8 16443
8ab8a5c8
RS
16444 /* When using the generic form of .section (as implemented by obj-elf.c),
16445 there's no way to set the section type to SHT_MIPS_DWARF. Users have
16446 traditionally had to fall back on the more common @progbits instead.
16447
16448 There's nothing really harmful in this, since bfd will correct
16449 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
708587a4 16450 means that, for backwards compatibility, the special_section entries
8ab8a5c8
RS
16451 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
16452
16453 Even so, we shouldn't force users of the MIPS .section syntax to
16454 incorrectly label the sections as SHT_PROGBITS. The best compromise
16455 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
16456 generic type-checking code. */
16457 if (section_type == SHT_MIPS_DWARF)
16458 section_type = SHT_PROGBITS;
16459
a8c4d40b 16460 obj_elf_change_section (section_name, section_type, section_flag,
cca86cc8 16461 section_entry_size, 0, 0, 0);
a816d1ed
AO
16462
16463 if (now_seg->name != section_name)
16464 free (section_name);
cca86cc8 16465}
252b5132
RH
16466
16467void
17a2f251 16468mips_enable_auto_align (void)
252b5132
RH
16469{
16470 auto_align = 1;
16471}
16472
16473static void
17a2f251 16474s_cons (int log_size)
252b5132 16475{
a8dbcb85
TS
16476 segment_info_type *si = seg_info (now_seg);
16477 struct insn_label_list *l = si->label_list;
252b5132 16478
7d10b47d 16479 mips_emit_delays ();
252b5132 16480 if (log_size > 0 && auto_align)
462427c4 16481 mips_align (log_size, 0, l);
252b5132 16482 cons (1 << log_size);
a1facbec 16483 mips_clear_insn_labels ();
252b5132
RH
16484}
16485
16486static void
17a2f251 16487s_float_cons (int type)
252b5132 16488{
a8dbcb85
TS
16489 segment_info_type *si = seg_info (now_seg);
16490 struct insn_label_list *l = si->label_list;
252b5132 16491
7d10b47d 16492 mips_emit_delays ();
252b5132
RH
16493
16494 if (auto_align)
49309057
ILT
16495 {
16496 if (type == 'd')
462427c4 16497 mips_align (3, 0, l);
49309057 16498 else
462427c4 16499 mips_align (2, 0, l);
49309057 16500 }
252b5132 16501
252b5132 16502 float_cons (type);
a1facbec 16503 mips_clear_insn_labels ();
252b5132
RH
16504}
16505
16506/* Handle .globl. We need to override it because on Irix 5 you are
16507 permitted to say
16508 .globl foo .text
16509 where foo is an undefined symbol, to mean that foo should be
16510 considered to be the address of a function. */
16511
16512static void
17a2f251 16513s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
16514{
16515 char *name;
16516 int c;
16517 symbolS *symbolP;
252b5132 16518
8a06b769 16519 do
252b5132 16520 {
d02603dc 16521 c = get_symbol_name (&name);
8a06b769
TS
16522 symbolP = symbol_find_or_make (name);
16523 S_SET_EXTERNAL (symbolP);
16524
252b5132 16525 *input_line_pointer = c;
d02603dc 16526 SKIP_WHITESPACE_AFTER_NAME ();
252b5132 16527
8a06b769
TS
16528 if (!is_end_of_line[(unsigned char) *input_line_pointer]
16529 && (*input_line_pointer != ','))
16530 {
16531 char *secname;
16532 asection *sec;
16533
d02603dc 16534 c = get_symbol_name (&secname);
8a06b769
TS
16535 sec = bfd_get_section_by_name (stdoutput, secname);
16536 if (sec == NULL)
16537 as_bad (_("%s: no such section"), secname);
d02603dc 16538 (void) restore_line_pointer (c);
8a06b769
TS
16539
16540 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
d69cd47e 16541 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
8a06b769
TS
16542 }
16543
8a06b769
TS
16544 c = *input_line_pointer;
16545 if (c == ',')
16546 {
16547 input_line_pointer++;
16548 SKIP_WHITESPACE ();
16549 if (is_end_of_line[(unsigned char) *input_line_pointer])
16550 c = '\n';
16551 }
16552 }
16553 while (c == ',');
252b5132 16554
252b5132
RH
16555 demand_empty_rest_of_line ();
16556}
16557
d69cd47e
AM
16558#ifdef TE_IRIX
16559/* The Irix 5 and 6 assemblers set the type of any common symbol and
16560 any undefined non-function symbol to STT_OBJECT. We try to be
16561 compatible, since newer Irix 5 and 6 linkers care. */
16562
16563void
16564mips_frob_symbol (symbolS *symp ATTRIBUTE_UNUSED)
16565{
16566 /* This late in assembly we can set BSF_OBJECT indiscriminately
16567 and let elf.c:swap_out_syms sort out the symbol type. */
16568 flagword *flags = &symbol_get_bfdsym (symp)->flags;
16569 if ((*flags & (BSF_GLOBAL | BSF_WEAK)) != 0
16570 || !S_IS_DEFINED (symp))
16571 *flags |= BSF_OBJECT;
16572}
16573#endif
16574
252b5132 16575static void
17a2f251 16576s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
16577{
16578 char *opt;
16579 char c;
16580
d02603dc 16581 c = get_symbol_name (&opt);
252b5132
RH
16582
16583 if (*opt == 'O')
16584 {
16585 /* FIXME: What does this mean? */
16586 }
41a1578e 16587 else if (strncmp (opt, "pic", 3) == 0 && ISDIGIT (opt[3]) && opt[4] == '\0')
252b5132
RH
16588 {
16589 int i;
16590
16591 i = atoi (opt + 3);
668c5ebc
MR
16592 if (i != 0 && i != 2)
16593 as_bad (_(".option pic%d not supported"), i);
16594 else if (mips_pic == VXWORKS_PIC)
16595 as_bad (_(".option pic%d not supported in VxWorks PIC mode"), i);
16596 else if (i == 0)
252b5132
RH
16597 mips_pic = NO_PIC;
16598 else if (i == 2)
143d77c5 16599 {
8b828383 16600 mips_pic = SVR4_PIC;
143d77c5
EC
16601 mips_abicalls = TRUE;
16602 }
252b5132 16603
4d0d148d 16604 if (mips_pic == SVR4_PIC)
252b5132
RH
16605 {
16606 if (g_switch_seen && g_switch_value != 0)
16607 as_warn (_("-G may not be used with SVR4 PIC code"));
16608 g_switch_value = 0;
16609 bfd_set_gp_size (stdoutput, 0);
16610 }
16611 }
16612 else
1661c76c 16613 as_warn (_("unrecognized option \"%s\""), opt);
252b5132 16614
d02603dc 16615 (void) restore_line_pointer (c);
252b5132
RH
16616 demand_empty_rest_of_line ();
16617}
16618
16619/* This structure is used to hold a stack of .set values. */
16620
e972090a
NC
16621struct mips_option_stack
16622{
252b5132
RH
16623 struct mips_option_stack *next;
16624 struct mips_set_options options;
16625};
16626
16627static struct mips_option_stack *mips_opts_stack;
16628
22522f88
MR
16629/* Return status for .set/.module option handling. */
16630
16631enum code_option_type
16632{
16633 /* Unrecognized option. */
16634 OPTION_TYPE_BAD = -1,
16635
16636 /* Ordinary option. */
16637 OPTION_TYPE_NORMAL,
16638
16639 /* ISA changing option. */
16640 OPTION_TYPE_ISA
16641};
16642
16643/* Handle common .set/.module options. Return status indicating option
16644 type. */
16645
16646static enum code_option_type
919731af 16647parse_code_option (char * name)
252b5132 16648{
22522f88 16649 bfd_boolean isa_set = FALSE;
c6278170 16650 const struct mips_ase *ase;
22522f88 16651
919731af 16652 if (strncmp (name, "at=", 3) == 0)
741fe287
MR
16653 {
16654 char *s = name + 3;
16655
16656 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
1661c76c 16657 as_bad (_("unrecognized register name `%s'"), s);
741fe287 16658 }
252b5132 16659 else if (strcmp (name, "at") == 0)
919731af 16660 mips_opts.at = ATREG;
252b5132 16661 else if (strcmp (name, "noat") == 0)
919731af 16662 mips_opts.at = ZERO;
252b5132 16663 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
919731af 16664 mips_opts.nomove = 0;
252b5132 16665 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
919731af 16666 mips_opts.nomove = 1;
252b5132 16667 else if (strcmp (name, "bopt") == 0)
919731af 16668 mips_opts.nobopt = 0;
252b5132 16669 else if (strcmp (name, "nobopt") == 0)
919731af 16670 mips_opts.nobopt = 1;
ad3fea08 16671 else if (strcmp (name, "gp=32") == 0)
bad1aba3 16672 mips_opts.gp = 32;
ad3fea08 16673 else if (strcmp (name, "gp=64") == 0)
919731af 16674 mips_opts.gp = 64;
ad3fea08 16675 else if (strcmp (name, "fp=32") == 0)
0b35dfee 16676 mips_opts.fp = 32;
351cdf24
MF
16677 else if (strcmp (name, "fp=xx") == 0)
16678 mips_opts.fp = 0;
ad3fea08 16679 else if (strcmp (name, "fp=64") == 0)
919731af 16680 mips_opts.fp = 64;
037b32b9
AN
16681 else if (strcmp (name, "softfloat") == 0)
16682 mips_opts.soft_float = 1;
16683 else if (strcmp (name, "hardfloat") == 0)
16684 mips_opts.soft_float = 0;
16685 else if (strcmp (name, "singlefloat") == 0)
16686 mips_opts.single_float = 1;
16687 else if (strcmp (name, "doublefloat") == 0)
16688 mips_opts.single_float = 0;
351cdf24
MF
16689 else if (strcmp (name, "nooddspreg") == 0)
16690 mips_opts.oddspreg = 0;
16691 else if (strcmp (name, "oddspreg") == 0)
16692 mips_opts.oddspreg = 1;
252b5132
RH
16693 else if (strcmp (name, "mips16") == 0
16694 || strcmp (name, "MIPS-16") == 0)
919731af 16695 mips_opts.mips16 = 1;
252b5132
RH
16696 else if (strcmp (name, "nomips16") == 0
16697 || strcmp (name, "noMIPS-16") == 0)
16698 mips_opts.mips16 = 0;
df58fc94 16699 else if (strcmp (name, "micromips") == 0)
919731af 16700 mips_opts.micromips = 1;
df58fc94
RS
16701 else if (strcmp (name, "nomicromips") == 0)
16702 mips_opts.micromips = 0;
c6278170
RS
16703 else if (name[0] == 'n'
16704 && name[1] == 'o'
16705 && (ase = mips_lookup_ase (name + 2)))
919731af 16706 mips_set_ase (ase, &mips_opts, FALSE);
c6278170 16707 else if ((ase = mips_lookup_ase (name)))
919731af 16708 mips_set_ase (ase, &mips_opts, TRUE);
1a2c1fad 16709 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 16710 {
1a2c1fad
CD
16711 /* Permit the user to change the ISA and architecture on the fly.
16712 Needless to say, misuse can cause serious problems. */
919731af 16713 if (strncmp (name, "arch=", 5) == 0)
1a2c1fad
CD
16714 {
16715 const struct mips_cpu_info *p;
16716
919731af 16717 p = mips_parse_cpu ("internal use", name + 5);
1a2c1fad
CD
16718 if (!p)
16719 as_bad (_("unknown architecture %s"), name + 5);
16720 else
16721 {
16722 mips_opts.arch = p->cpu;
16723 mips_opts.isa = p->isa;
22522f88 16724 isa_set = TRUE;
3315614d 16725 mips_opts.init_ase = p->ase;
1a2c1fad
CD
16726 }
16727 }
81a21e38
TS
16728 else if (strncmp (name, "mips", 4) == 0)
16729 {
16730 const struct mips_cpu_info *p;
16731
919731af 16732 p = mips_parse_cpu ("internal use", name);
81a21e38
TS
16733 if (!p)
16734 as_bad (_("unknown ISA level %s"), name + 4);
16735 else
16736 {
16737 mips_opts.arch = p->cpu;
16738 mips_opts.isa = p->isa;
22522f88 16739 isa_set = TRUE;
3315614d 16740 mips_opts.init_ase = p->ase;
81a21e38
TS
16741 }
16742 }
af7ee8bf 16743 else
81a21e38 16744 as_bad (_("unknown ISA or architecture %s"), name);
252b5132
RH
16745 }
16746 else if (strcmp (name, "autoextend") == 0)
16747 mips_opts.noautoextend = 0;
16748 else if (strcmp (name, "noautoextend") == 0)
16749 mips_opts.noautoextend = 1;
833794fc
MR
16750 else if (strcmp (name, "insn32") == 0)
16751 mips_opts.insn32 = TRUE;
16752 else if (strcmp (name, "noinsn32") == 0)
16753 mips_opts.insn32 = FALSE;
919731af 16754 else if (strcmp (name, "sym32") == 0)
16755 mips_opts.sym32 = TRUE;
16756 else if (strcmp (name, "nosym32") == 0)
16757 mips_opts.sym32 = FALSE;
16758 else
22522f88
MR
16759 return OPTION_TYPE_BAD;
16760
16761 return isa_set ? OPTION_TYPE_ISA : OPTION_TYPE_NORMAL;
919731af 16762}
16763
16764/* Handle the .set pseudo-op. */
16765
16766static void
16767s_mipsset (int x ATTRIBUTE_UNUSED)
16768{
22522f88 16769 enum code_option_type type = OPTION_TYPE_NORMAL;
919731af 16770 char *name = input_line_pointer, ch;
919731af 16771
16772 file_mips_check_options ();
16773
16774 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16775 ++input_line_pointer;
16776 ch = *input_line_pointer;
16777 *input_line_pointer = '\0';
16778
16779 if (strchr (name, ','))
16780 {
16781 /* Generic ".set" directive; use the generic handler. */
16782 *input_line_pointer = ch;
16783 input_line_pointer = name;
16784 s_set (0);
16785 return;
16786 }
16787
16788 if (strcmp (name, "reorder") == 0)
16789 {
16790 if (mips_opts.noreorder)
16791 end_noreorder ();
16792 }
16793 else if (strcmp (name, "noreorder") == 0)
16794 {
16795 if (!mips_opts.noreorder)
16796 start_noreorder ();
16797 }
16798 else if (strcmp (name, "macro") == 0)
16799 mips_opts.warn_about_macros = 0;
16800 else if (strcmp (name, "nomacro") == 0)
16801 {
16802 if (mips_opts.noreorder == 0)
16803 as_bad (_("`noreorder' must be set before `nomacro'"));
16804 mips_opts.warn_about_macros = 1;
16805 }
16806 else if (strcmp (name, "gp=default") == 0)
16807 mips_opts.gp = file_mips_opts.gp;
16808 else if (strcmp (name, "fp=default") == 0)
16809 mips_opts.fp = file_mips_opts.fp;
16810 else if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
16811 {
16812 mips_opts.isa = file_mips_opts.isa;
16813 mips_opts.arch = file_mips_opts.arch;
3315614d 16814 mips_opts.init_ase = file_mips_opts.init_ase;
919731af 16815 mips_opts.gp = file_mips_opts.gp;
16816 mips_opts.fp = file_mips_opts.fp;
16817 }
252b5132
RH
16818 else if (strcmp (name, "push") == 0)
16819 {
16820 struct mips_option_stack *s;
16821
325801bd 16822 s = XNEW (struct mips_option_stack);
252b5132
RH
16823 s->next = mips_opts_stack;
16824 s->options = mips_opts;
16825 mips_opts_stack = s;
16826 }
16827 else if (strcmp (name, "pop") == 0)
16828 {
16829 struct mips_option_stack *s;
16830
16831 s = mips_opts_stack;
16832 if (s == NULL)
16833 as_bad (_(".set pop with no .set push"));
16834 else
16835 {
16836 /* If we're changing the reorder mode we need to handle
16837 delay slots correctly. */
16838 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 16839 start_noreorder ();
252b5132 16840 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 16841 end_noreorder ();
252b5132
RH
16842
16843 mips_opts = s->options;
16844 mips_opts_stack = s->next;
16845 free (s);
16846 }
16847 }
22522f88
MR
16848 else
16849 {
16850 type = parse_code_option (name);
16851 if (type == OPTION_TYPE_BAD)
16852 as_warn (_("tried to set unrecognized symbol: %s\n"), name);
16853 }
919731af 16854
16855 /* The use of .set [arch|cpu]= historically 'fixes' the width of gp and fp
16856 registers based on what is supported by the arch/cpu. */
22522f88 16857 if (type == OPTION_TYPE_ISA)
e6559e01 16858 {
919731af 16859 switch (mips_opts.isa)
16860 {
16861 case 0:
16862 break;
16863 case ISA_MIPS1:
351cdf24
MF
16864 /* MIPS I cannot support FPXX. */
16865 mips_opts.fp = 32;
16866 /* fall-through. */
919731af 16867 case ISA_MIPS2:
16868 case ISA_MIPS32:
16869 case ISA_MIPS32R2:
16870 case ISA_MIPS32R3:
16871 case ISA_MIPS32R5:
16872 mips_opts.gp = 32;
351cdf24
MF
16873 if (mips_opts.fp != 0)
16874 mips_opts.fp = 32;
919731af 16875 break;
7361da2c
AB
16876 case ISA_MIPS32R6:
16877 mips_opts.gp = 32;
16878 mips_opts.fp = 64;
16879 break;
919731af 16880 case ISA_MIPS3:
16881 case ISA_MIPS4:
16882 case ISA_MIPS5:
16883 case ISA_MIPS64:
16884 case ISA_MIPS64R2:
16885 case ISA_MIPS64R3:
16886 case ISA_MIPS64R5:
7361da2c 16887 case ISA_MIPS64R6:
919731af 16888 mips_opts.gp = 64;
351cdf24
MF
16889 if (mips_opts.fp != 0)
16890 {
16891 if (mips_opts.arch == CPU_R5900)
16892 mips_opts.fp = 32;
16893 else
16894 mips_opts.fp = 64;
16895 }
919731af 16896 break;
16897 default:
16898 as_bad (_("unknown ISA level %s"), name + 4);
16899 break;
16900 }
e6559e01 16901 }
919731af 16902
16903 mips_check_options (&mips_opts, FALSE);
16904
16905 mips_check_isa_supports_ases ();
16906 *input_line_pointer = ch;
16907 demand_empty_rest_of_line ();
16908}
16909
16910/* Handle the .module pseudo-op. */
16911
16912static void
16913s_module (int ignore ATTRIBUTE_UNUSED)
16914{
16915 char *name = input_line_pointer, ch;
16916
16917 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16918 ++input_line_pointer;
16919 ch = *input_line_pointer;
16920 *input_line_pointer = '\0';
16921
16922 if (!file_mips_opts_checked)
252b5132 16923 {
22522f88 16924 if (parse_code_option (name) == OPTION_TYPE_BAD)
919731af 16925 as_bad (_(".module used with unrecognized symbol: %s\n"), name);
16926
16927 /* Update module level settings from mips_opts. */
16928 file_mips_opts = mips_opts;
252b5132 16929 }
919731af 16930 else
16931 as_bad (_(".module is not permitted after generating code"));
16932
252b5132
RH
16933 *input_line_pointer = ch;
16934 demand_empty_rest_of_line ();
16935}
16936
16937/* Handle the .abicalls pseudo-op. I believe this is equivalent to
16938 .option pic2. It means to generate SVR4 PIC calls. */
16939
16940static void
17a2f251 16941s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16942{
16943 mips_pic = SVR4_PIC;
143d77c5 16944 mips_abicalls = TRUE;
4d0d148d
TS
16945
16946 if (g_switch_seen && g_switch_value != 0)
16947 as_warn (_("-G may not be used with SVR4 PIC code"));
16948 g_switch_value = 0;
16949
252b5132
RH
16950 bfd_set_gp_size (stdoutput, 0);
16951 demand_empty_rest_of_line ();
16952}
16953
16954/* Handle the .cpload pseudo-op. This is used when generating SVR4
16955 PIC code. It sets the $gp register for the function based on the
16956 function address, which is in the register named in the argument.
16957 This uses a relocation against _gp_disp, which is handled specially
16958 by the linker. The result is:
16959 lui $gp,%hi(_gp_disp)
16960 addiu $gp,$gp,%lo(_gp_disp)
16961 addu $gp,$gp,.cpload argument
aa6975fb
ILT
16962 The .cpload argument is normally $25 == $t9.
16963
16964 The -mno-shared option changes this to:
bbe506e8
TS
16965 lui $gp,%hi(__gnu_local_gp)
16966 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
16967 and the argument is ignored. This saves an instruction, but the
16968 resulting code is not position independent; it uses an absolute
bbe506e8
TS
16969 address for __gnu_local_gp. Thus code assembled with -mno-shared
16970 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
16971
16972static void
17a2f251 16973s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16974{
16975 expressionS ex;
aa6975fb
ILT
16976 int reg;
16977 int in_shared;
252b5132 16978
919731af 16979 file_mips_check_options ();
16980
6478892d
TS
16981 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16982 .cpload is ignored. */
16983 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16984 {
16985 s_ignore (0);
16986 return;
16987 }
16988
a276b80c
MR
16989 if (mips_opts.mips16)
16990 {
16991 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16992 ignore_rest_of_line ();
16993 return;
16994 }
16995
d3ecfc59 16996 /* .cpload should be in a .set noreorder section. */
252b5132
RH
16997 if (mips_opts.noreorder == 0)
16998 as_warn (_(".cpload not in noreorder section"));
16999
aa6975fb
ILT
17000 reg = tc_get_register (0);
17001
17002 /* If we need to produce a 64-bit address, we are better off using
17003 the default instruction sequence. */
aed1a261 17004 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 17005
252b5132 17006 ex.X_op = O_symbol;
bbe506e8
TS
17007 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
17008 "__gnu_local_gp");
252b5132
RH
17009 ex.X_op_symbol = NULL;
17010 ex.X_add_number = 0;
17011
17012 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 17013 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 17014
8a75745d
MR
17015 mips_mark_labels ();
17016 mips_assembling_insn = TRUE;
17017
584892a6 17018 macro_start ();
67c0d1eb
RS
17019 macro_build_lui (&ex, mips_gp_register);
17020 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 17021 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
17022 if (in_shared)
17023 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
17024 mips_gp_register, reg);
584892a6 17025 macro_end ();
252b5132 17026
8a75745d 17027 mips_assembling_insn = FALSE;
252b5132
RH
17028 demand_empty_rest_of_line ();
17029}
17030
6478892d
TS
17031/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
17032 .cpsetup $reg1, offset|$reg2, label
17033
17034 If offset is given, this results in:
17035 sd $gp, offset($sp)
956cd1d6 17036 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
17037 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
17038 daddu $gp, $gp, $reg1
6478892d
TS
17039
17040 If $reg2 is given, this results in:
40fc1451 17041 or $reg2, $gp, $0
956cd1d6 17042 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
17043 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
17044 daddu $gp, $gp, $reg1
aa6975fb
ILT
17045 $reg1 is normally $25 == $t9.
17046
17047 The -mno-shared option replaces the last three instructions with
17048 lui $gp,%hi(_gp)
54f4ddb3 17049 addiu $gp,$gp,%lo(_gp) */
aa6975fb 17050
6478892d 17051static void
17a2f251 17052s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
17053{
17054 expressionS ex_off;
17055 expressionS ex_sym;
17056 int reg1;
6478892d 17057
919731af 17058 file_mips_check_options ();
17059
8586fc66 17060 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
17061 We also need NewABI support. */
17062 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17063 {
17064 s_ignore (0);
17065 return;
17066 }
17067
a276b80c
MR
17068 if (mips_opts.mips16)
17069 {
17070 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
17071 ignore_rest_of_line ();
17072 return;
17073 }
17074
6478892d
TS
17075 reg1 = tc_get_register (0);
17076 SKIP_WHITESPACE ();
17077 if (*input_line_pointer != ',')
17078 {
17079 as_bad (_("missing argument separator ',' for .cpsetup"));
17080 return;
17081 }
17082 else
80245285 17083 ++input_line_pointer;
6478892d
TS
17084 SKIP_WHITESPACE ();
17085 if (*input_line_pointer == '$')
80245285
TS
17086 {
17087 mips_cpreturn_register = tc_get_register (0);
17088 mips_cpreturn_offset = -1;
17089 }
6478892d 17090 else
80245285
TS
17091 {
17092 mips_cpreturn_offset = get_absolute_expression ();
17093 mips_cpreturn_register = -1;
17094 }
6478892d
TS
17095 SKIP_WHITESPACE ();
17096 if (*input_line_pointer != ',')
17097 {
17098 as_bad (_("missing argument separator ',' for .cpsetup"));
17099 return;
17100 }
17101 else
f9419b05 17102 ++input_line_pointer;
6478892d 17103 SKIP_WHITESPACE ();
f21f8242 17104 expression (&ex_sym);
6478892d 17105
8a75745d
MR
17106 mips_mark_labels ();
17107 mips_assembling_insn = TRUE;
17108
584892a6 17109 macro_start ();
6478892d
TS
17110 if (mips_cpreturn_register == -1)
17111 {
17112 ex_off.X_op = O_constant;
17113 ex_off.X_add_symbol = NULL;
17114 ex_off.X_op_symbol = NULL;
17115 ex_off.X_add_number = mips_cpreturn_offset;
17116
67c0d1eb 17117 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 17118 BFD_RELOC_LO16, SP);
6478892d
TS
17119 }
17120 else
40fc1451 17121 move_register (mips_cpreturn_register, mips_gp_register);
6478892d 17122
aed1a261 17123 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb 17124 {
df58fc94 17125 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
aa6975fb
ILT
17126 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
17127 BFD_RELOC_HI16_S);
17128
17129 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
17130 mips_gp_register, -1, BFD_RELOC_GPREL16,
17131 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
17132
17133 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
17134 mips_gp_register, reg1);
17135 }
17136 else
17137 {
17138 expressionS ex;
17139
17140 ex.X_op = O_symbol;
4184909a 17141 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
17142 ex.X_op_symbol = NULL;
17143 ex.X_add_number = 0;
6e1304d8 17144
aa6975fb
ILT
17145 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
17146 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
17147
17148 macro_build_lui (&ex, mips_gp_register);
17149 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17150 mips_gp_register, BFD_RELOC_LO16);
17151 }
f21f8242 17152
584892a6 17153 macro_end ();
6478892d 17154
8a75745d 17155 mips_assembling_insn = FALSE;
6478892d
TS
17156 demand_empty_rest_of_line ();
17157}
17158
17159static void
17a2f251 17160s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d 17161{
919731af 17162 file_mips_check_options ();
17163
6478892d 17164 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
54f4ddb3 17165 .cplocal is ignored. */
6478892d
TS
17166 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17167 {
17168 s_ignore (0);
17169 return;
17170 }
17171
a276b80c
MR
17172 if (mips_opts.mips16)
17173 {
17174 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
17175 ignore_rest_of_line ();
17176 return;
17177 }
17178
6478892d 17179 mips_gp_register = tc_get_register (0);
85b51719 17180 demand_empty_rest_of_line ();
6478892d
TS
17181}
17182
252b5132
RH
17183/* Handle the .cprestore pseudo-op. This stores $gp into a given
17184 offset from $sp. The offset is remembered, and after making a PIC
17185 call $gp is restored from that location. */
17186
17187static void
17a2f251 17188s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
17189{
17190 expressionS ex;
252b5132 17191
919731af 17192 file_mips_check_options ();
17193
6478892d 17194 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 17195 .cprestore is ignored. */
6478892d 17196 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
17197 {
17198 s_ignore (0);
17199 return;
17200 }
17201
a276b80c
MR
17202 if (mips_opts.mips16)
17203 {
17204 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
17205 ignore_rest_of_line ();
17206 return;
17207 }
17208
252b5132 17209 mips_cprestore_offset = get_absolute_expression ();
7a621144 17210 mips_cprestore_valid = 1;
252b5132
RH
17211
17212 ex.X_op = O_constant;
17213 ex.X_add_symbol = NULL;
17214 ex.X_op_symbol = NULL;
17215 ex.X_add_number = mips_cprestore_offset;
17216
8a75745d
MR
17217 mips_mark_labels ();
17218 mips_assembling_insn = TRUE;
17219
584892a6 17220 macro_start ();
67c0d1eb
RS
17221 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
17222 SP, HAVE_64BIT_ADDRESSES);
584892a6 17223 macro_end ();
252b5132 17224
8a75745d 17225 mips_assembling_insn = FALSE;
252b5132
RH
17226 demand_empty_rest_of_line ();
17227}
17228
6478892d 17229/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 17230 was given in the preceding .cpsetup, it results in:
6478892d 17231 ld $gp, offset($sp)
76b3015f 17232
6478892d 17233 If a register $reg2 was given there, it results in:
40fc1451 17234 or $gp, $reg2, $0 */
54f4ddb3 17235
6478892d 17236static void
17a2f251 17237s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
17238{
17239 expressionS ex;
6478892d 17240
919731af 17241 file_mips_check_options ();
17242
6478892d
TS
17243 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
17244 We also need NewABI support. */
17245 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17246 {
17247 s_ignore (0);
17248 return;
17249 }
17250
a276b80c
MR
17251 if (mips_opts.mips16)
17252 {
17253 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
17254 ignore_rest_of_line ();
17255 return;
17256 }
17257
8a75745d
MR
17258 mips_mark_labels ();
17259 mips_assembling_insn = TRUE;
17260
584892a6 17261 macro_start ();
6478892d
TS
17262 if (mips_cpreturn_register == -1)
17263 {
17264 ex.X_op = O_constant;
17265 ex.X_add_symbol = NULL;
17266 ex.X_op_symbol = NULL;
17267 ex.X_add_number = mips_cpreturn_offset;
17268
67c0d1eb 17269 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
17270 }
17271 else
40fc1451
SD
17272 move_register (mips_gp_register, mips_cpreturn_register);
17273
584892a6 17274 macro_end ();
6478892d 17275
8a75745d 17276 mips_assembling_insn = FALSE;
6478892d
TS
17277 demand_empty_rest_of_line ();
17278}
17279
d0f13682
CLT
17280/* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
17281 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
17282 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
17283 debug information or MIPS16 TLS. */
741d6ea8
JM
17284
17285static void
d0f13682
CLT
17286s_tls_rel_directive (const size_t bytes, const char *dirstr,
17287 bfd_reloc_code_real_type rtype)
741d6ea8
JM
17288{
17289 expressionS ex;
17290 char *p;
17291
17292 expression (&ex);
17293
17294 if (ex.X_op != O_symbol)
17295 {
1661c76c 17296 as_bad (_("unsupported use of %s"), dirstr);
741d6ea8
JM
17297 ignore_rest_of_line ();
17298 }
17299
17300 p = frag_more (bytes);
17301 md_number_to_chars (p, 0, bytes);
d0f13682 17302 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
741d6ea8 17303 demand_empty_rest_of_line ();
de64cffd 17304 mips_clear_insn_labels ();
741d6ea8
JM
17305}
17306
17307/* Handle .dtprelword. */
17308
17309static void
17310s_dtprelword (int ignore ATTRIBUTE_UNUSED)
17311{
d0f13682 17312 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
741d6ea8
JM
17313}
17314
17315/* Handle .dtpreldword. */
17316
17317static void
17318s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
17319{
d0f13682
CLT
17320 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
17321}
17322
17323/* Handle .tprelword. */
17324
17325static void
17326s_tprelword (int ignore ATTRIBUTE_UNUSED)
17327{
17328 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
17329}
17330
17331/* Handle .tpreldword. */
17332
17333static void
17334s_tpreldword (int ignore ATTRIBUTE_UNUSED)
17335{
17336 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
741d6ea8
JM
17337}
17338
6478892d
TS
17339/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
17340 code. It sets the offset to use in gp_rel relocations. */
17341
17342static void
17a2f251 17343s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
17344{
17345 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
17346 We also need NewABI support. */
17347 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17348 {
17349 s_ignore (0);
17350 return;
17351 }
17352
def2e0dd 17353 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
17354
17355 demand_empty_rest_of_line ();
17356}
17357
252b5132
RH
17358/* Handle the .gpword pseudo-op. This is used when generating PIC
17359 code. It generates a 32 bit GP relative reloc. */
17360
17361static void
17a2f251 17362s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132 17363{
a8dbcb85
TS
17364 segment_info_type *si;
17365 struct insn_label_list *l;
252b5132
RH
17366 expressionS ex;
17367 char *p;
17368
17369 /* When not generating PIC code, this is treated as .word. */
17370 if (mips_pic != SVR4_PIC)
17371 {
17372 s_cons (2);
17373 return;
17374 }
17375
a8dbcb85
TS
17376 si = seg_info (now_seg);
17377 l = si->label_list;
7d10b47d 17378 mips_emit_delays ();
252b5132 17379 if (auto_align)
462427c4 17380 mips_align (2, 0, l);
252b5132
RH
17381
17382 expression (&ex);
a1facbec 17383 mips_clear_insn_labels ();
252b5132
RH
17384
17385 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17386 {
1661c76c 17387 as_bad (_("unsupported use of .gpword"));
252b5132
RH
17388 ignore_rest_of_line ();
17389 }
17390
17391 p = frag_more (4);
17a2f251 17392 md_number_to_chars (p, 0, 4);
b34976b6 17393 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 17394 BFD_RELOC_GPREL32);
252b5132
RH
17395
17396 demand_empty_rest_of_line ();
17397}
17398
10181a0d 17399static void
17a2f251 17400s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d 17401{
a8dbcb85
TS
17402 segment_info_type *si;
17403 struct insn_label_list *l;
10181a0d
AO
17404 expressionS ex;
17405 char *p;
17406
17407 /* When not generating PIC code, this is treated as .dword. */
17408 if (mips_pic != SVR4_PIC)
17409 {
17410 s_cons (3);
17411 return;
17412 }
17413
a8dbcb85
TS
17414 si = seg_info (now_seg);
17415 l = si->label_list;
7d10b47d 17416 mips_emit_delays ();
10181a0d 17417 if (auto_align)
462427c4 17418 mips_align (3, 0, l);
10181a0d
AO
17419
17420 expression (&ex);
a1facbec 17421 mips_clear_insn_labels ();
10181a0d
AO
17422
17423 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17424 {
1661c76c 17425 as_bad (_("unsupported use of .gpdword"));
10181a0d
AO
17426 ignore_rest_of_line ();
17427 }
17428
17429 p = frag_more (8);
17a2f251 17430 md_number_to_chars (p, 0, 8);
a105a300 17431 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
6e1304d8 17432 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
17433
17434 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8
RS
17435 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
17436 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
17437
17438 demand_empty_rest_of_line ();
17439}
17440
a3f278e2
CM
17441/* Handle the .ehword pseudo-op. This is used when generating unwinding
17442 tables. It generates a R_MIPS_EH reloc. */
17443
17444static void
17445s_ehword (int ignore ATTRIBUTE_UNUSED)
17446{
17447 expressionS ex;
17448 char *p;
17449
17450 mips_emit_delays ();
17451
17452 expression (&ex);
17453 mips_clear_insn_labels ();
17454
17455 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17456 {
1661c76c 17457 as_bad (_("unsupported use of .ehword"));
a3f278e2
CM
17458 ignore_rest_of_line ();
17459 }
17460
17461 p = frag_more (4);
17462 md_number_to_chars (p, 0, 4);
17463 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
2f0c68f2 17464 BFD_RELOC_32_PCREL);
a3f278e2
CM
17465
17466 demand_empty_rest_of_line ();
17467}
17468
252b5132
RH
17469/* Handle the .cpadd pseudo-op. This is used when dealing with switch
17470 tables in SVR4 PIC code. */
17471
17472static void
17a2f251 17473s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 17474{
252b5132
RH
17475 int reg;
17476
919731af 17477 file_mips_check_options ();
17478
10181a0d
AO
17479 /* This is ignored when not generating SVR4 PIC code. */
17480 if (mips_pic != SVR4_PIC)
252b5132
RH
17481 {
17482 s_ignore (0);
17483 return;
17484 }
17485
8a75745d
MR
17486 mips_mark_labels ();
17487 mips_assembling_insn = TRUE;
17488
252b5132 17489 /* Add $gp to the register named as an argument. */
584892a6 17490 macro_start ();
252b5132 17491 reg = tc_get_register (0);
67c0d1eb 17492 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 17493 macro_end ();
252b5132 17494
8a75745d 17495 mips_assembling_insn = FALSE;
bdaaa2e1 17496 demand_empty_rest_of_line ();
252b5132
RH
17497}
17498
17499/* Handle the .insn pseudo-op. This marks instruction labels in
df58fc94 17500 mips16/micromips mode. This permits the linker to handle them specially,
252b5132
RH
17501 such as generating jalx instructions when needed. We also make
17502 them odd for the duration of the assembly, in order to generate the
17503 right sort of code. We will make them even in the adjust_symtab
17504 routine, while leaving them marked. This is convenient for the
17505 debugger and the disassembler. The linker knows to make them odd
17506 again. */
17507
17508static void
17a2f251 17509s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 17510{
7bb01e2d
MR
17511 file_mips_check_options ();
17512 file_ase_mips16 |= mips_opts.mips16;
17513 file_ase_micromips |= mips_opts.micromips;
17514
df58fc94 17515 mips_mark_labels ();
252b5132
RH
17516
17517 demand_empty_rest_of_line ();
17518}
17519
ba92f887
MR
17520/* Handle the .nan pseudo-op. */
17521
17522static void
17523s_nan (int ignore ATTRIBUTE_UNUSED)
17524{
17525 static const char str_legacy[] = "legacy";
17526 static const char str_2008[] = "2008";
17527 size_t i;
17528
17529 for (i = 0; !is_end_of_line[(unsigned char) input_line_pointer[i]]; i++);
17530
17531 if (i == sizeof (str_2008) - 1
17532 && memcmp (input_line_pointer, str_2008, i) == 0)
7361da2c 17533 mips_nan2008 = 1;
ba92f887
MR
17534 else if (i == sizeof (str_legacy) - 1
17535 && memcmp (input_line_pointer, str_legacy, i) == 0)
7361da2c
AB
17536 {
17537 if (ISA_HAS_LEGACY_NAN (file_mips_opts.isa))
17538 mips_nan2008 = 0;
17539 else
17540 as_bad (_("`%s' does not support legacy NaN"),
17541 mips_cpu_info_from_isa (file_mips_opts.isa)->name);
17542 }
ba92f887 17543 else
1661c76c 17544 as_bad (_("bad .nan directive"));
ba92f887
MR
17545
17546 input_line_pointer += i;
17547 demand_empty_rest_of_line ();
17548}
17549
754e2bb9
RS
17550/* Handle a .stab[snd] directive. Ideally these directives would be
17551 implemented in a transparent way, so that removing them would not
17552 have any effect on the generated instructions. However, s_stab
17553 internally changes the section, so in practice we need to decide
17554 now whether the preceding label marks compressed code. We do not
17555 support changing the compression mode of a label after a .stab*
17556 directive, such as in:
17557
17558 foo:
134c0c8b 17559 .stabs ...
754e2bb9
RS
17560 .set mips16
17561
17562 so the current mode wins. */
252b5132
RH
17563
17564static void
17a2f251 17565s_mips_stab (int type)
252b5132 17566{
42c0794e 17567 file_mips_check_options ();
754e2bb9 17568 mips_mark_labels ();
252b5132
RH
17569 s_stab (type);
17570}
17571
54f4ddb3 17572/* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
252b5132
RH
17573
17574static void
17a2f251 17575s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
17576{
17577 char *name;
17578 int c;
17579 symbolS *symbolP;
17580 expressionS exp;
17581
d02603dc 17582 c = get_symbol_name (&name);
252b5132
RH
17583 symbolP = symbol_find_or_make (name);
17584 S_SET_WEAK (symbolP);
17585 *input_line_pointer = c;
17586
d02603dc 17587 SKIP_WHITESPACE_AFTER_NAME ();
252b5132
RH
17588
17589 if (! is_end_of_line[(unsigned char) *input_line_pointer])
17590 {
17591 if (S_IS_DEFINED (symbolP))
17592 {
20203fb9 17593 as_bad (_("ignoring attempt to redefine symbol %s"),
252b5132
RH
17594 S_GET_NAME (symbolP));
17595 ignore_rest_of_line ();
17596 return;
17597 }
bdaaa2e1 17598
252b5132
RH
17599 if (*input_line_pointer == ',')
17600 {
17601 ++input_line_pointer;
17602 SKIP_WHITESPACE ();
17603 }
bdaaa2e1 17604
252b5132
RH
17605 expression (&exp);
17606 if (exp.X_op != O_symbol)
17607 {
20203fb9 17608 as_bad (_("bad .weakext directive"));
98d3f06f 17609 ignore_rest_of_line ();
252b5132
RH
17610 return;
17611 }
49309057 17612 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
17613 }
17614
17615 demand_empty_rest_of_line ();
17616}
17617
17618/* Parse a register string into a number. Called from the ECOFF code
17619 to parse .frame. The argument is non-zero if this is the frame
17620 register, so that we can record it in mips_frame_reg. */
17621
17622int
17a2f251 17623tc_get_register (int frame)
252b5132 17624{
707bfff6 17625 unsigned int reg;
252b5132
RH
17626
17627 SKIP_WHITESPACE ();
707bfff6
TS
17628 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17629 reg = 0;
252b5132 17630 if (frame)
7a621144
DJ
17631 {
17632 mips_frame_reg = reg != 0 ? reg : SP;
17633 mips_frame_reg_valid = 1;
17634 mips_cprestore_valid = 0;
17635 }
252b5132
RH
17636 return reg;
17637}
17638
17639valueT
17a2f251 17640md_section_align (asection *seg, valueT addr)
252b5132 17641{
fd361982 17642 int align = bfd_section_alignment (seg);
252b5132 17643
f3ded42a
RS
17644 /* We don't need to align ELF sections to the full alignment.
17645 However, Irix 5 may prefer that we align them at least to a 16
17646 byte boundary. We don't bother to align the sections if we
17647 are targeted for an embedded system. */
17648 if (strncmp (TARGET_OS, "elf", 3) == 0)
17649 return addr;
17650 if (align > 4)
17651 align = 4;
252b5132 17652
8d3842cd 17653 return ((addr + (1 << align) - 1) & -(1 << align));
252b5132
RH
17654}
17655
17656/* Utility routine, called from above as well. If called while the
17657 input file is still being read, it's only an approximation. (For
17658 example, a symbol may later become defined which appeared to be
17659 undefined earlier.) */
17660
17661static int
17a2f251 17662nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
17663{
17664 if (sym == 0)
17665 return 0;
17666
4d0d148d 17667 if (g_switch_value > 0)
252b5132
RH
17668 {
17669 const char *symname;
17670 int change;
17671
c9914766 17672 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
17673 register. It can be if it is smaller than the -G size or if
17674 it is in the .sdata or .sbss section. Certain symbols can
c9914766 17675 not be referenced off the $gp, although it appears as though
252b5132
RH
17676 they can. */
17677 symname = S_GET_NAME (sym);
17678 if (symname != (const char *) NULL
17679 && (strcmp (symname, "eprol") == 0
17680 || strcmp (symname, "etext") == 0
17681 || strcmp (symname, "_gp") == 0
17682 || strcmp (symname, "edata") == 0
17683 || strcmp (symname, "_fbss") == 0
17684 || strcmp (symname, "_fdata") == 0
17685 || strcmp (symname, "_ftext") == 0
17686 || strcmp (symname, "end") == 0
17687 || strcmp (symname, "_gp_disp") == 0))
17688 change = 1;
17689 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17690 && (0
17691#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
17692 || (symbol_get_obj (sym)->ecoff_extern_size != 0
17693 && (symbol_get_obj (sym)->ecoff_extern_size
17694 <= g_switch_value))
252b5132
RH
17695#endif
17696 /* We must defer this decision until after the whole
17697 file has been read, since there might be a .extern
17698 after the first use of this symbol. */
17699 || (before_relaxing
17700#ifndef NO_ECOFF_DEBUGGING
49309057 17701 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
17702#endif
17703 && S_GET_VALUE (sym) == 0)
17704 || (S_GET_VALUE (sym) != 0
17705 && S_GET_VALUE (sym) <= g_switch_value)))
17706 change = 0;
17707 else
17708 {
17709 const char *segname;
17710
17711 segname = segment_name (S_GET_SEGMENT (sym));
9c2799c2 17712 gas_assert (strcmp (segname, ".lit8") != 0
252b5132
RH
17713 && strcmp (segname, ".lit4") != 0);
17714 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
17715 && strcmp (segname, ".sbss") != 0
17716 && strncmp (segname, ".sdata.", 7) != 0
d4dc2f22
TS
17717 && strncmp (segname, ".sbss.", 6) != 0
17718 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
fba2b7f9 17719 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
17720 }
17721 return change;
17722 }
17723 else
c9914766 17724 /* We are not optimizing for the $gp register. */
252b5132
RH
17725 return 1;
17726}
17727
5919d012
RS
17728
17729/* Return true if the given symbol should be considered local for SVR4 PIC. */
17730
17731static bfd_boolean
9e009953 17732pic_need_relax (symbolS *sym)
5919d012
RS
17733{
17734 asection *symsec;
5919d012
RS
17735
17736 /* Handle the case of a symbol equated to another symbol. */
17737 while (symbol_equated_reloc_p (sym))
17738 {
17739 symbolS *n;
17740
5f0fe04b 17741 /* It's possible to get a loop here in a badly written program. */
5919d012
RS
17742 n = symbol_get_value_expression (sym)->X_add_symbol;
17743 if (n == sym)
17744 break;
17745 sym = n;
17746 }
17747
df1f3cda
DD
17748 if (symbol_section_p (sym))
17749 return TRUE;
17750
5919d012
RS
17751 symsec = S_GET_SEGMENT (sym);
17752
5919d012 17753 /* This must duplicate the test in adjust_reloc_syms. */
45dfa85a
AM
17754 return (!bfd_is_und_section (symsec)
17755 && !bfd_is_abs_section (symsec)
5f0fe04b 17756 && !bfd_is_com_section (symsec)
5919d012 17757 /* A global or weak symbol is treated as external. */
f3ded42a 17758 && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
5919d012 17759}
14f72d45
MR
17760\f
17761/* Given a MIPS16 variant frag FRAGP and PC-relative operand PCREL_OP
17762 convert a section-relative value VAL to the equivalent PC-relative
17763 value. */
17764
17765static offsetT
17766mips16_pcrel_val (fragS *fragp, const struct mips_pcrel_operand *pcrel_op,
17767 offsetT val, long stretch)
17768{
17769 fragS *sym_frag;
17770 addressT addr;
17771
17772 gas_assert (pcrel_op->root.root.type == OP_PCREL);
17773
17774 sym_frag = symbol_get_frag (fragp->fr_symbol);
17775
17776 /* If the relax_marker of the symbol fragment differs from the
17777 relax_marker of this fragment, we have not yet adjusted the
17778 symbol fragment fr_address. We want to add in STRETCH in
17779 order to get a better estimate of the address. This
17780 particularly matters because of the shift bits. */
17781 if (stretch != 0 && sym_frag->relax_marker != fragp->relax_marker)
17782 {
17783 fragS *f;
17784
17785 /* Adjust stretch for any alignment frag. Note that if have
17786 been expanding the earlier code, the symbol may be
17787 defined in what appears to be an earlier frag. FIXME:
17788 This doesn't handle the fr_subtype field, which specifies
17789 a maximum number of bytes to skip when doing an
17790 alignment. */
17791 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
17792 {
17793 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17794 {
17795 if (stretch < 0)
17796 stretch = -(-stretch & ~((1 << (int) f->fr_offset) - 1));
17797 else
17798 stretch &= ~((1 << (int) f->fr_offset) - 1);
17799 if (stretch == 0)
17800 break;
17801 }
17802 }
17803 if (f != NULL)
17804 val += stretch;
17805 }
17806
17807 addr = fragp->fr_address + fragp->fr_fix;
17808
17809 /* The base address rules are complicated. The base address of
17810 a branch is the following instruction. The base address of a
17811 PC relative load or add is the instruction itself, but if it
17812 is in a delay slot (in which case it can not be extended) use
17813 the address of the instruction whose delay slot it is in. */
17814 if (pcrel_op->include_isa_bit)
17815 {
17816 addr += 2;
17817
17818 /* If we are currently assuming that this frag should be
17819 extended, then the current address is two bytes higher. */
17820 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17821 addr += 2;
17822
17823 /* Ignore the low bit in the target, since it will be set
17824 for a text label. */
17825 val &= -2;
17826 }
17827 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17828 addr -= 4;
17829 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17830 addr -= 2;
5919d012 17831
14f72d45
MR
17832 val -= addr & -(1 << pcrel_op->align_log2);
17833
17834 return val;
17835}
5919d012 17836
252b5132
RH
17837/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17838 extended opcode. SEC is the section the frag is in. */
17839
17840static int
17a2f251 17841mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132 17842{
3ccad066 17843 const struct mips_int_operand *operand;
252b5132 17844 offsetT val;
252b5132 17845 segT symsec;
14f72d45 17846 int type;
252b5132
RH
17847
17848 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17849 return 0;
17850 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17851 return 1;
17852
88a7ef16 17853 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132 17854 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
3ccad066 17855 operand = mips16_immed_operand (type, FALSE);
88a7ef16
MR
17856 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
17857 || (operand->root.type == OP_PCREL
17858 ? sec != symsec
17859 : !bfd_is_abs_section (symsec)))
17860 return 1;
252b5132 17861
88a7ef16 17862 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
252b5132 17863
3ccad066 17864 if (operand->root.type == OP_PCREL)
252b5132 17865 {
3ccad066 17866 const struct mips_pcrel_operand *pcrel_op;
3ccad066 17867 offsetT maxtiny;
252b5132 17868
1425c41d 17869 if (RELAX_MIPS16_ALWAYS_EXTENDED (fragp->fr_subtype))
88a7ef16 17870 return 1;
252b5132 17871
88a7ef16 17872 pcrel_op = (const struct mips_pcrel_operand *) operand;
14f72d45 17873 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
252b5132
RH
17874
17875 /* If any of the shifted bits are set, we must use an extended
17876 opcode. If the address depends on the size of this
17877 instruction, this can lead to a loop, so we arrange to always
88a7ef16
MR
17878 use an extended opcode. */
17879 if ((val & ((1 << operand->shift) - 1)) != 0)
252b5132
RH
17880 {
17881 fragp->fr_subtype =
1425c41d 17882 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
252b5132
RH
17883 return 1;
17884 }
17885
17886 /* If we are about to mark a frag as extended because the value
3ccad066
RS
17887 is precisely the next value above maxtiny, then there is a
17888 chance of an infinite loop as in the following code:
252b5132
RH
17889 la $4,foo
17890 .skip 1020
17891 .align 2
17892 foo:
17893 In this case when the la is extended, foo is 0x3fc bytes
17894 away, so the la can be shrunk, but then foo is 0x400 away, so
17895 the la must be extended. To avoid this loop, we mark the
17896 frag as extended if it was small, and is about to become
3ccad066
RS
17897 extended with the next value above maxtiny. */
17898 maxtiny = mips_int_operand_max (operand);
17899 if (val == maxtiny + (1 << operand->shift)
88a7ef16 17900 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
252b5132
RH
17901 {
17902 fragp->fr_subtype =
1425c41d 17903 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
252b5132
RH
17904 return 1;
17905 }
17906 }
252b5132 17907
3ccad066 17908 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
252b5132
RH
17909}
17910
8507b6e7
MR
17911/* Given a MIPS16 variant frag FRAGP, return non-zero if it needs
17912 macro expansion. SEC is the section the frag is in. We only
17913 support PC-relative instructions (LA, DLA, LW, LD) here, in
17914 non-PIC code using 32-bit addressing. */
17915
17916static int
17917mips16_macro_frag (fragS *fragp, asection *sec, long stretch)
17918{
17919 const struct mips_pcrel_operand *pcrel_op;
17920 const struct mips_int_operand *operand;
17921 offsetT val;
17922 segT symsec;
17923 int type;
17924
17925 gas_assert (!RELAX_MIPS16_USER_SMALL (fragp->fr_subtype));
17926
17927 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17928 return 0;
17929 if (!RELAX_MIPS16_SYM32 (fragp->fr_subtype))
17930 return 0;
17931
17932 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17933 switch (type)
17934 {
17935 case 'A':
17936 case 'B':
17937 case 'E':
17938 symsec = S_GET_SEGMENT (fragp->fr_symbol);
17939 if (bfd_is_abs_section (symsec))
17940 return 1;
17941 if (RELAX_MIPS16_PIC (fragp->fr_subtype))
17942 return 0;
17943 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE) || sec != symsec)
17944 return 1;
17945
17946 operand = mips16_immed_operand (type, TRUE);
17947 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17948 pcrel_op = (const struct mips_pcrel_operand *) operand;
17949 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
17950
17951 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
17952
17953 default:
17954 return 0;
17955 }
17956}
17957
4a6a3df4
AO
17958/* Compute the length of a branch sequence, and adjust the
17959 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
17960 worst-case length is computed, with UPDATE being used to indicate
17961 whether an unconditional (-1), branch-likely (+1) or regular (0)
17962 branch is to be computed. */
17963static int
17a2f251 17964relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 17965{
b34976b6 17966 bfd_boolean toofar;
4a6a3df4
AO
17967 int length;
17968
17969 if (fragp
17970 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 17971 && !S_IS_WEAK (fragp->fr_symbol)
4a6a3df4
AO
17972 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17973 {
17974 addressT addr;
17975 offsetT val;
17976
17977 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17978
17979 addr = fragp->fr_address + fragp->fr_fix + 4;
17980
17981 val -= addr;
17982
17983 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17984 }
4a6a3df4 17985 else
c1f61bd2
MR
17986 /* If the symbol is not defined or it's in a different segment,
17987 we emit the long sequence. */
b34976b6 17988 toofar = TRUE;
4a6a3df4
AO
17989
17990 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17991 fragp->fr_subtype
66b3e8da 17992 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
ce8ad872 17993 RELAX_BRANCH_PIC (fragp->fr_subtype),
66b3e8da 17994 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
17995 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17996 RELAX_BRANCH_LINK (fragp->fr_subtype),
17997 toofar);
17998
17999 length = 4;
18000 if (toofar)
18001 {
18002 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
18003 length += 8;
18004
ce8ad872 18005 if (!fragp || RELAX_BRANCH_PIC (fragp->fr_subtype))
4a6a3df4
AO
18006 {
18007 /* Additional space for PIC loading of target address. */
18008 length += 8;
18009 if (mips_opts.isa == ISA_MIPS1)
18010 /* Additional space for $at-stabilizing nop. */
18011 length += 4;
18012 }
18013
18014 /* If branch is conditional. */
18015 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
18016 length += 8;
18017 }
b34976b6 18018
4a6a3df4
AO
18019 return length;
18020}
18021
7bd374a4
MR
18022/* Get a FRAG's branch instruction delay slot size, either from the
18023 short-delay-slot bit of a branch-and-link instruction if AL is TRUE,
18024 or SHORT_INSN_SIZE otherwise. */
18025
18026static int
18027frag_branch_delay_slot_size (fragS *fragp, bfd_boolean al, int short_insn_size)
18028{
18029 char *buf = fragp->fr_literal + fragp->fr_fix;
18030
18031 if (al)
18032 return (read_compressed_insn (buf, 4) & 0x02000000) ? 2 : 4;
18033 else
18034 return short_insn_size;
18035}
18036
df58fc94
RS
18037/* Compute the length of a branch sequence, and adjust the
18038 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
18039 worst-case length is computed, with UPDATE being used to indicate
18040 whether an unconditional (-1), or regular (0) branch is to be
18041 computed. */
18042
18043static int
18044relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
18045{
7bd374a4
MR
18046 bfd_boolean insn32 = TRUE;
18047 bfd_boolean nods = TRUE;
ce8ad872 18048 bfd_boolean pic = TRUE;
7bd374a4
MR
18049 bfd_boolean al = TRUE;
18050 int short_insn_size;
df58fc94
RS
18051 bfd_boolean toofar;
18052 int length;
18053
7bd374a4
MR
18054 if (fragp)
18055 {
18056 insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
18057 nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
ce8ad872 18058 pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
7bd374a4
MR
18059 al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18060 }
18061 short_insn_size = insn32 ? 4 : 2;
18062
df58fc94
RS
18063 if (fragp
18064 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 18065 && !S_IS_WEAK (fragp->fr_symbol)
df58fc94
RS
18066 && sec == S_GET_SEGMENT (fragp->fr_symbol))
18067 {
18068 addressT addr;
18069 offsetT val;
18070
18071 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
18072 /* Ignore the low bit in the target, since it will be set
18073 for a text label. */
18074 if ((val & 1) != 0)
18075 --val;
18076
18077 addr = fragp->fr_address + fragp->fr_fix + 4;
18078
18079 val -= addr;
18080
18081 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
18082 }
df58fc94 18083 else
c1f61bd2
MR
18084 /* If the symbol is not defined or it's in a different segment,
18085 we emit the long sequence. */
df58fc94
RS
18086 toofar = TRUE;
18087
18088 if (fragp && update
18089 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18090 fragp->fr_subtype = (toofar
18091 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
18092 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
18093
18094 length = 4;
18095 if (toofar)
18096 {
18097 bfd_boolean compact_known = fragp != NULL;
18098 bfd_boolean compact = FALSE;
18099 bfd_boolean uncond;
18100
df58fc94 18101 if (fragp)
8484fb75
MR
18102 {
18103 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18104 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
8484fb75 18105 }
df58fc94
RS
18106 else
18107 uncond = update < 0;
18108
18109 /* If label is out of range, we turn branch <br>:
18110
18111 <br> label # 4 bytes
18112 0:
18113
18114 into:
18115
18116 j label # 4 bytes
8484fb75
MR
18117 nop # 2/4 bytes if
18118 # compact && (!PIC || insn32)
df58fc94
RS
18119 0:
18120 */
ce8ad872 18121 if ((!pic || insn32) && (!compact_known || compact))
8484fb75 18122 length += short_insn_size;
df58fc94
RS
18123
18124 /* If assembling PIC code, we further turn:
18125
18126 j label # 4 bytes
18127
18128 into:
18129
18130 lw/ld at, %got(label)(gp) # 4 bytes
18131 d/addiu at, %lo(label) # 4 bytes
8484fb75 18132 jr/c at # 2/4 bytes
df58fc94 18133 */
ce8ad872 18134 if (pic)
8484fb75 18135 length += 4 + short_insn_size;
df58fc94 18136
7bd374a4
MR
18137 /* Add an extra nop if the jump has no compact form and we need
18138 to fill the delay slot. */
ce8ad872 18139 if ((!pic || al) && nods)
7bd374a4
MR
18140 length += (fragp
18141 ? frag_branch_delay_slot_size (fragp, al, short_insn_size)
18142 : short_insn_size);
18143
df58fc94
RS
18144 /* If branch <br> is conditional, we prepend negated branch <brneg>:
18145
18146 <brneg> 0f # 4 bytes
8484fb75 18147 nop # 2/4 bytes if !compact
df58fc94
RS
18148 */
18149 if (!uncond)
8484fb75 18150 length += (compact_known && compact) ? 4 : 4 + short_insn_size;
df58fc94 18151 }
7bd374a4
MR
18152 else if (nods)
18153 {
18154 /* Add an extra nop to fill the delay slot. */
18155 gas_assert (fragp);
18156 length += frag_branch_delay_slot_size (fragp, al, short_insn_size);
18157 }
df58fc94
RS
18158
18159 return length;
18160}
18161
18162/* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
18163 bit accordingly. */
18164
18165static int
18166relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
18167{
18168 bfd_boolean toofar;
18169
df58fc94
RS
18170 if (fragp
18171 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 18172 && !S_IS_WEAK (fragp->fr_symbol)
df58fc94
RS
18173 && sec == S_GET_SEGMENT (fragp->fr_symbol))
18174 {
18175 addressT addr;
18176 offsetT val;
18177 int type;
18178
18179 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
18180 /* Ignore the low bit in the target, since it will be set
18181 for a text label. */
18182 if ((val & 1) != 0)
18183 --val;
18184
18185 /* Assume this is a 2-byte branch. */
18186 addr = fragp->fr_address + fragp->fr_fix + 2;
18187
18188 /* We try to avoid the infinite loop by not adding 2 more bytes for
18189 long branches. */
18190
18191 val -= addr;
18192
18193 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
18194 if (type == 'D')
18195 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
18196 else if (type == 'E')
18197 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
18198 else
18199 abort ();
18200 }
18201 else
18202 /* If the symbol is not defined or it's in a different segment,
18203 we emit a normal 32-bit branch. */
18204 toofar = TRUE;
18205
18206 if (fragp && update
18207 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18208 fragp->fr_subtype
18209 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
18210 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
18211
18212 if (toofar)
18213 return 4;
18214
18215 return 2;
18216}
18217
252b5132
RH
18218/* Estimate the size of a frag before relaxing. Unless this is the
18219 mips16, we are not really relaxing here, and the final size is
18220 encoded in the subtype information. For the mips16, we have to
18221 decide whether we are using an extended opcode or not. */
18222
252b5132 18223int
17a2f251 18224md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 18225{
5919d012 18226 int change;
252b5132 18227
4a6a3df4
AO
18228 if (RELAX_BRANCH_P (fragp->fr_subtype))
18229 {
18230
b34976b6
AM
18231 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
18232
4a6a3df4
AO
18233 return fragp->fr_var;
18234 }
18235
252b5132 18236 if (RELAX_MIPS16_P (fragp->fr_subtype))
8507b6e7
MR
18237 {
18238 /* We don't want to modify the EXTENDED bit here; it might get us
18239 into infinite loops. We change it only in mips_relax_frag(). */
18240 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
25499ac7 18241 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 8 : 12;
8507b6e7
MR
18242 else
18243 return RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2;
18244 }
252b5132 18245
df58fc94
RS
18246 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18247 {
18248 int length = 4;
18249
18250 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
18251 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
18252 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
18253 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
18254 fragp->fr_var = length;
18255
18256 return length;
18257 }
18258
ce8ad872 18259 if (mips_pic == VXWORKS_PIC)
0a44bf69
RS
18260 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
18261 change = 0;
ce8ad872
MR
18262 else if (RELAX_PIC (fragp->fr_subtype))
18263 change = pic_need_relax (fragp->fr_symbol);
252b5132 18264 else
ce8ad872 18265 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132
RH
18266
18267 if (change)
18268 {
4d7206a2 18269 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 18270 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 18271 }
4d7206a2
RS
18272 else
18273 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
18274}
18275
18276/* This is called to see whether a reloc against a defined symbol
de7e6852 18277 should be converted into a reloc against a section. */
252b5132
RH
18278
18279int
17a2f251 18280mips_fix_adjustable (fixS *fixp)
252b5132 18281{
252b5132
RH
18282 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
18283 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
18284 return 0;
a161fe53 18285
252b5132
RH
18286 if (fixp->fx_addsy == NULL)
18287 return 1;
a161fe53 18288
2f0c68f2
CM
18289 /* Allow relocs used for EH tables. */
18290 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
18291 return 1;
18292
de7e6852
RS
18293 /* If symbol SYM is in a mergeable section, relocations of the form
18294 SYM + 0 can usually be made section-relative. The mergeable data
18295 is then identified by the section offset rather than by the symbol.
18296
18297 However, if we're generating REL LO16 relocations, the offset is split
33eaf5de 18298 between the LO16 and partnering high part relocation. The linker will
de7e6852
RS
18299 need to recalculate the complete offset in order to correctly identify
18300 the merge data.
18301
33eaf5de 18302 The linker has traditionally not looked for the partnering high part
de7e6852
RS
18303 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
18304 placed anywhere. Rather than break backwards compatibility by changing
18305 this, it seems better not to force the issue, and instead keep the
18306 original symbol. This will work with either linker behavior. */
738e5348 18307 if ((lo16_reloc_p (fixp->fx_r_type)
704803a9 18308 || reloc_needs_lo_p (fixp->fx_r_type))
de7e6852
RS
18309 && HAVE_IN_PLACE_ADDENDS
18310 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
18311 return 0;
18312
97f50151
MR
18313 /* There is no place to store an in-place offset for JALR relocations. */
18314 if (jalr_reloc_p (fixp->fx_r_type) && HAVE_IN_PLACE_ADDENDS)
18315 return 0;
18316
18317 /* Likewise an in-range offset of limited PC-relative relocations may
2de39019 18318 overflow the in-place relocatable field if recalculated against the
7361da2c
AB
18319 start address of the symbol's containing section.
18320
18321 Also, PC relative relocations for MIPS R6 need to be symbol rather than
18322 section relative to allow linker relaxations to be performed later on. */
97f50151 18323 if (limited_pcrel_reloc_p (fixp->fx_r_type)
912815f0 18324 && (HAVE_IN_PLACE_ADDENDS || ISA_IS_R6 (file_mips_opts.isa)))
1180b5a4
RS
18325 return 0;
18326
b314ec0e
RS
18327 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
18328 to a floating-point stub. The same is true for non-R_MIPS16_26
18329 relocations against MIPS16 functions; in this case, the stub becomes
18330 the function's canonical address.
18331
18332 Floating-point stubs are stored in unique .mips16.call.* or
18333 .mips16.fn.* sections. If a stub T for function F is in section S,
18334 the first relocation in section S must be against F; this is how the
18335 linker determines the target function. All relocations that might
18336 resolve to T must also be against F. We therefore have the following
18337 restrictions, which are given in an intentionally-redundant way:
18338
18339 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
18340 symbols.
18341
18342 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
18343 if that stub might be used.
18344
18345 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
18346 symbols.
18347
18348 4. We cannot reduce a stub's relocations against MIPS16 symbols if
18349 that stub might be used.
18350
18351 There is a further restriction:
18352
df58fc94 18353 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
0e9c5a5c 18354 R_MICROMIPS_26_S1) or branch relocations (R_MIPS_PC26_S2,
c9775dde
MR
18355 R_MIPS_PC21_S2, R_MIPS_PC16, R_MIPS16_PC16_S1,
18356 R_MICROMIPS_PC16_S1, R_MICROMIPS_PC10_S1 or R_MICROMIPS_PC7_S1)
18357 against MIPS16 or microMIPS symbols because we need to keep the
18358 MIPS16 or microMIPS symbol for the purpose of mode mismatch
a6ebf616
MR
18359 detection and JAL or BAL to JALX instruction conversion in the
18360 linker.
b314ec0e 18361
df58fc94 18362 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
507dcb32 18363 against a MIPS16 symbol. We deal with (5) by additionally leaving
0e9c5a5c 18364 alone any jump and branch relocations against a microMIPS symbol.
b314ec0e
RS
18365
18366 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
18367 relocation against some symbol R, no relocation against R may be
18368 reduced. (Note that this deals with (2) as well as (1) because
18369 relocations against global symbols will never be reduced on ELF
18370 targets.) This approach is a little simpler than trying to detect
18371 stub sections, and gives the "all or nothing" per-symbol consistency
18372 that we have for MIPS16 symbols. */
f3ded42a 18373 if (fixp->fx_subsy == NULL
30c09090 18374 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
44d3da23 18375 || (ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
0e9c5a5c
MR
18376 && (jmp_reloc_p (fixp->fx_r_type)
18377 || b_reloc_p (fixp->fx_r_type)))
44d3da23 18378 || *symbol_get_tc (fixp->fx_addsy)))
252b5132 18379 return 0;
a161fe53 18380
252b5132
RH
18381 return 1;
18382}
18383
18384/* Translate internal representation of relocation info to BFD target
18385 format. */
18386
18387arelent **
17a2f251 18388tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
18389{
18390 static arelent *retval[4];
18391 arelent *reloc;
18392 bfd_reloc_code_real_type code;
18393
4b0cff4e 18394 memset (retval, 0, sizeof(retval));
325801bd
TS
18395 reloc = retval[0] = XCNEW (arelent);
18396 reloc->sym_ptr_ptr = XNEW (asymbol *);
49309057 18397 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
18398 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
18399
bad36eac
DJ
18400 if (fixp->fx_pcrel)
18401 {
df58fc94 18402 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
c9775dde 18403 || fixp->fx_r_type == BFD_RELOC_MIPS16_16_PCREL_S1
df58fc94
RS
18404 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
18405 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
b47468a6 18406 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
7361da2c
AB
18407 || fixp->fx_r_type == BFD_RELOC_32_PCREL
18408 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
18409 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
18410 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
18411 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
18412 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
18413 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL);
bad36eac
DJ
18414
18415 /* At this point, fx_addnumber is "symbol offset - pcrel address".
18416 Relocations want only the symbol offset. */
51f6035b
MR
18417 switch (fixp->fx_r_type)
18418 {
18419 case BFD_RELOC_MIPS_18_PCREL_S3:
18420 reloc->addend = fixp->fx_addnumber + (reloc->address & ~7);
18421 break;
18422 default:
18423 reloc->addend = fixp->fx_addnumber + reloc->address;
18424 break;
18425 }
bad36eac 18426 }
17c6c9d9
MR
18427 else if (HAVE_IN_PLACE_ADDENDS
18428 && fixp->fx_r_type == BFD_RELOC_MICROMIPS_JMP
18429 && (read_compressed_insn (fixp->fx_frag->fr_literal
18430 + fixp->fx_where, 4) >> 26) == 0x3c)
18431 {
18432 /* Shift is 2, unusually, for microMIPS JALX. Adjust the in-place
18433 addend accordingly. */
18434 reloc->addend = fixp->fx_addnumber >> 1;
18435 }
bad36eac
DJ
18436 else
18437 reloc->addend = fixp->fx_addnumber;
252b5132 18438
438c16b8
TS
18439 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
18440 entry to be used in the relocation's section offset. */
18441 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
18442 {
18443 reloc->address = reloc->addend;
18444 reloc->addend = 0;
18445 }
18446
252b5132 18447 code = fixp->fx_r_type;
252b5132 18448
bad36eac 18449 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
252b5132
RH
18450 if (reloc->howto == NULL)
18451 {
18452 as_bad_where (fixp->fx_file, fixp->fx_line,
1661c76c
RS
18453 _("cannot represent %s relocation in this object file"
18454 " format"),
252b5132
RH
18455 bfd_get_reloc_code_name (code));
18456 retval[0] = NULL;
18457 }
18458
18459 return retval;
18460}
18461
18462/* Relax a machine dependent frag. This returns the amount by which
18463 the current size of the frag should change. */
18464
18465int
17a2f251 18466mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 18467{
4a6a3df4
AO
18468 if (RELAX_BRANCH_P (fragp->fr_subtype))
18469 {
18470 offsetT old_var = fragp->fr_var;
b34976b6
AM
18471
18472 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
18473
18474 return fragp->fr_var - old_var;
18475 }
18476
df58fc94
RS
18477 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18478 {
18479 offsetT old_var = fragp->fr_var;
18480 offsetT new_var = 4;
18481
18482 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
18483 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
18484 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
18485 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
18486 fragp->fr_var = new_var;
18487
18488 return new_var - old_var;
18489 }
18490
252b5132
RH
18491 if (! RELAX_MIPS16_P (fragp->fr_subtype))
18492 return 0;
18493
8507b6e7 18494 if (!mips16_extended_frag (fragp, sec, stretch))
252b5132 18495 {
8507b6e7
MR
18496 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18497 {
18498 fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
25499ac7 18499 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -6 : -10;
8507b6e7
MR
18500 }
18501 else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18502 {
18503 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18504 return -2;
18505 }
18506 else
18507 return 0;
18508 }
18509 else if (!mips16_macro_frag (fragp, sec, stretch))
18510 {
18511 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18512 {
18513 fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
18514 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
25499ac7 18515 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -4 : -8;
8507b6e7
MR
18516 }
18517 else if (!RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18518 {
18519 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
18520 return 2;
18521 }
18522 else
252b5132 18523 return 0;
252b5132
RH
18524 }
18525 else
18526 {
8507b6e7 18527 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
252b5132 18528 return 0;
8507b6e7
MR
18529 else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18530 {
18531 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18532 fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
25499ac7 18533 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 4 : 8;
8507b6e7
MR
18534 }
18535 else
18536 {
18537 fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
25499ac7 18538 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 6 : 10;
8507b6e7 18539 }
252b5132
RH
18540 }
18541
18542 return 0;
18543}
18544
18545/* Convert a machine dependent frag. */
18546
18547void
17a2f251 18548md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 18549{
4a6a3df4
AO
18550 if (RELAX_BRANCH_P (fragp->fr_subtype))
18551 {
4d68580a 18552 char *buf;
4a6a3df4 18553 unsigned long insn;
4a6a3df4 18554 fixS *fixp;
b34976b6 18555
4d68580a
RS
18556 buf = fragp->fr_literal + fragp->fr_fix;
18557 insn = read_insn (buf);
b34976b6 18558
4a6a3df4
AO
18559 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
18560 {
18561 /* We generate a fixup instead of applying it right now
18562 because, if there are linker relaxations, we're going to
18563 need the relocations. */
bbd27b76
MR
18564 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18565 fragp->fr_symbol, fragp->fr_offset,
18566 TRUE, BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
18567 fixp->fx_file = fragp->fr_file;
18568 fixp->fx_line = fragp->fr_line;
b34976b6 18569
4d68580a 18570 buf = write_insn (buf, insn);
4a6a3df4
AO
18571 }
18572 else
18573 {
18574 int i;
18575
18576 as_warn_where (fragp->fr_file, fragp->fr_line,
1661c76c 18577 _("relaxed out-of-range branch into a jump"));
4a6a3df4
AO
18578
18579 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
18580 goto uncond;
18581
18582 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18583 {
18584 /* Reverse the branch. */
18585 switch ((insn >> 28) & 0xf)
18586 {
18587 case 4:
56d438b1
CF
18588 if ((insn & 0xff000000) == 0x47000000
18589 || (insn & 0xff600000) == 0x45600000)
18590 {
18591 /* BZ.df/BNZ.df, BZ.V/BNZ.V can have the condition
18592 reversed by tweaking bit 23. */
18593 insn ^= 0x00800000;
18594 }
18595 else
18596 {
18597 /* bc[0-3][tf]l? instructions can have the condition
18598 reversed by tweaking a single TF bit, and their
18599 opcodes all have 0x4???????. */
18600 gas_assert ((insn & 0xf3e00000) == 0x41000000);
18601 insn ^= 0x00010000;
18602 }
4a6a3df4
AO
18603 break;
18604
18605 case 0:
18606 /* bltz 0x04000000 bgez 0x04010000
54f4ddb3 18607 bltzal 0x04100000 bgezal 0x04110000 */
9c2799c2 18608 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
4a6a3df4
AO
18609 insn ^= 0x00010000;
18610 break;
b34976b6 18611
4a6a3df4
AO
18612 case 1:
18613 /* beq 0x10000000 bne 0x14000000
54f4ddb3 18614 blez 0x18000000 bgtz 0x1c000000 */
4a6a3df4
AO
18615 insn ^= 0x04000000;
18616 break;
18617
18618 default:
18619 abort ();
18620 }
18621 }
18622
18623 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18624 {
18625 /* Clear the and-link bit. */
9c2799c2 18626 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
4a6a3df4 18627
54f4ddb3
TS
18628 /* bltzal 0x04100000 bgezal 0x04110000
18629 bltzall 0x04120000 bgezall 0x04130000 */
4a6a3df4
AO
18630 insn &= ~0x00100000;
18631 }
18632
18633 /* Branch over the branch (if the branch was likely) or the
18634 full jump (not likely case). Compute the offset from the
18635 current instruction to branch to. */
18636 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18637 i = 16;
18638 else
18639 {
18640 /* How many bytes in instructions we've already emitted? */
4d68580a 18641 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18642 /* How many bytes in instructions from here to the end? */
18643 i = fragp->fr_var - i;
18644 }
18645 /* Convert to instruction count. */
18646 i >>= 2;
18647 /* Branch counts from the next instruction. */
b34976b6 18648 i--;
4a6a3df4
AO
18649 insn |= i;
18650 /* Branch over the jump. */
4d68580a 18651 buf = write_insn (buf, insn);
4a6a3df4 18652
54f4ddb3 18653 /* nop */
4d68580a 18654 buf = write_insn (buf, 0);
4a6a3df4
AO
18655
18656 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18657 {
18658 /* beql $0, $0, 2f */
18659 insn = 0x50000000;
18660 /* Compute the PC offset from the current instruction to
18661 the end of the variable frag. */
18662 /* How many bytes in instructions we've already emitted? */
4d68580a 18663 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18664 /* How many bytes in instructions from here to the end? */
18665 i = fragp->fr_var - i;
18666 /* Convert to instruction count. */
18667 i >>= 2;
18668 /* Don't decrement i, because we want to branch over the
18669 delay slot. */
4a6a3df4 18670 insn |= i;
4a6a3df4 18671
4d68580a
RS
18672 buf = write_insn (buf, insn);
18673 buf = write_insn (buf, 0);
4a6a3df4
AO
18674 }
18675
18676 uncond:
ce8ad872 18677 if (!RELAX_BRANCH_PIC (fragp->fr_subtype))
4a6a3df4
AO
18678 {
18679 /* j or jal. */
18680 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18681 ? 0x0c000000 : 0x08000000);
4a6a3df4 18682
bbd27b76
MR
18683 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18684 fragp->fr_symbol, fragp->fr_offset,
18685 FALSE, BFD_RELOC_MIPS_JMP);
4a6a3df4
AO
18686 fixp->fx_file = fragp->fr_file;
18687 fixp->fx_line = fragp->fr_line;
18688
4d68580a 18689 buf = write_insn (buf, insn);
4a6a3df4
AO
18690 }
18691 else
18692 {
66b3e8da
MR
18693 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18694
4a6a3df4 18695 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
66b3e8da
MR
18696 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18697 insn |= at << OP_SH_RT;
4a6a3df4 18698
bbd27b76
MR
18699 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18700 fragp->fr_symbol, fragp->fr_offset,
18701 FALSE, BFD_RELOC_MIPS_GOT16);
4a6a3df4
AO
18702 fixp->fx_file = fragp->fr_file;
18703 fixp->fx_line = fragp->fr_line;
18704
4d68580a 18705 buf = write_insn (buf, insn);
b34976b6 18706
4a6a3df4 18707 if (mips_opts.isa == ISA_MIPS1)
4d68580a
RS
18708 /* nop */
18709 buf = write_insn (buf, 0);
4a6a3df4
AO
18710
18711 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
66b3e8da
MR
18712 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18713 insn |= at << OP_SH_RS | at << OP_SH_RT;
4a6a3df4 18714
bbd27b76
MR
18715 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18716 fragp->fr_symbol, fragp->fr_offset,
18717 FALSE, BFD_RELOC_LO16);
4a6a3df4
AO
18718 fixp->fx_file = fragp->fr_file;
18719 fixp->fx_line = fragp->fr_line;
b34976b6 18720
4d68580a 18721 buf = write_insn (buf, insn);
4a6a3df4
AO
18722
18723 /* j(al)r $at. */
18724 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
66b3e8da 18725 insn = 0x0000f809;
4a6a3df4 18726 else
66b3e8da
MR
18727 insn = 0x00000008;
18728 insn |= at << OP_SH_RS;
4a6a3df4 18729
4d68580a 18730 buf = write_insn (buf, insn);
4a6a3df4
AO
18731 }
18732 }
18733
4a6a3df4 18734 fragp->fr_fix += fragp->fr_var;
4d68580a 18735 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
4a6a3df4
AO
18736 return;
18737 }
18738
df58fc94
RS
18739 /* Relax microMIPS branches. */
18740 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18741 {
4d68580a 18742 char *buf = fragp->fr_literal + fragp->fr_fix;
df58fc94 18743 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
8484fb75 18744 bfd_boolean insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
7bd374a4 18745 bfd_boolean nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
ce8ad872 18746 bfd_boolean pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
df58fc94
RS
18747 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18748 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
2309ddf2 18749 bfd_boolean short_ds;
df58fc94 18750 unsigned long insn;
df58fc94
RS
18751 fixS *fixp;
18752
df58fc94
RS
18753 fragp->fr_fix += fragp->fr_var;
18754
18755 /* Handle 16-bit branches that fit or are forced to fit. */
18756 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18757 {
18758 /* We generate a fixup instead of applying it right now,
18759 because if there is linker relaxation, we're going to
18760 need the relocations. */
834a65aa
MR
18761 switch (type)
18762 {
18763 case 'D':
18764 fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18765 fragp->fr_symbol, fragp->fr_offset,
18766 TRUE, BFD_RELOC_MICROMIPS_10_PCREL_S1);
18767 break;
18768 case 'E':
18769 fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18770 fragp->fr_symbol, fragp->fr_offset,
18771 TRUE, BFD_RELOC_MICROMIPS_7_PCREL_S1);
18772 break;
18773 default:
18774 abort ();
18775 }
df58fc94
RS
18776
18777 fixp->fx_file = fragp->fr_file;
18778 fixp->fx_line = fragp->fr_line;
18779
18780 /* These relocations can have an addend that won't fit in
18781 2 octets. */
18782 fixp->fx_no_overflow = 1;
18783
18784 return;
18785 }
18786
2309ddf2 18787 /* Handle 32-bit branches that fit or are forced to fit. */
df58fc94
RS
18788 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18789 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18790 {
18791 /* We generate a fixup instead of applying it right now,
18792 because if there is linker relaxation, we're going to
18793 need the relocations. */
bbd27b76
MR
18794 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18795 fragp->fr_symbol, fragp->fr_offset,
18796 TRUE, BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18797 fixp->fx_file = fragp->fr_file;
18798 fixp->fx_line = fragp->fr_line;
18799
18800 if (type == 0)
7bd374a4
MR
18801 {
18802 insn = read_compressed_insn (buf, 4);
18803 buf += 4;
18804
18805 if (nods)
18806 {
18807 /* Check the short-delay-slot bit. */
18808 if (!al || (insn & 0x02000000) != 0)
18809 buf = write_compressed_insn (buf, 0x0c00, 2);
18810 else
18811 buf = write_compressed_insn (buf, 0x00000000, 4);
18812 }
18813
18814 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18815 return;
18816 }
df58fc94
RS
18817 }
18818
18819 /* Relax 16-bit branches to 32-bit branches. */
18820 if (type != 0)
18821 {
4d68580a 18822 insn = read_compressed_insn (buf, 2);
df58fc94
RS
18823
18824 if ((insn & 0xfc00) == 0xcc00) /* b16 */
18825 insn = 0x94000000; /* beq */
18826 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
18827 {
18828 unsigned long regno;
18829
18830 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18831 regno = micromips_to_32_reg_d_map [regno];
18832 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
18833 insn |= regno << MICROMIPSOP_SH_RS;
18834 }
18835 else
18836 abort ();
18837
18838 /* Nothing else to do, just write it out. */
18839 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18840 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18841 {
4d68580a 18842 buf = write_compressed_insn (buf, insn, 4);
7bd374a4
MR
18843 if (nods)
18844 buf = write_compressed_insn (buf, 0x0c00, 2);
4d68580a 18845 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18846 return;
18847 }
18848 }
18849 else
4d68580a 18850 insn = read_compressed_insn (buf, 4);
df58fc94
RS
18851
18852 /* Relax 32-bit branches to a sequence of instructions. */
18853 as_warn_where (fragp->fr_file, fragp->fr_line,
1661c76c 18854 _("relaxed out-of-range branch into a jump"));
df58fc94 18855
2309ddf2 18856 /* Set the short-delay-slot bit. */
7bd374a4 18857 short_ds = !al || (insn & 0x02000000) != 0;
df58fc94
RS
18858
18859 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18860 {
18861 symbolS *l;
18862
18863 /* Reverse the branch. */
18864 if ((insn & 0xfc000000) == 0x94000000 /* beq */
18865 || (insn & 0xfc000000) == 0xb4000000) /* bne */
18866 insn ^= 0x20000000;
18867 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
18868 || (insn & 0xffe00000) == 0x40400000 /* bgez */
18869 || (insn & 0xffe00000) == 0x40800000 /* blez */
18870 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
18871 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
18872 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
18873 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
18874 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
18875 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
18876 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
18877 insn ^= 0x00400000;
18878 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
18879 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
18880 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
18881 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
18882 insn ^= 0x00200000;
56d438b1
CF
18883 else if ((insn & 0xff000000) == 0x83000000 /* BZ.df
18884 BNZ.df */
18885 || (insn & 0xff600000) == 0x81600000) /* BZ.V
18886 BNZ.V */
18887 insn ^= 0x00800000;
df58fc94
RS
18888 else
18889 abort ();
18890
18891 if (al)
18892 {
18893 /* Clear the and-link and short-delay-slot bits. */
18894 gas_assert ((insn & 0xfda00000) == 0x40200000);
18895
18896 /* bltzal 0x40200000 bgezal 0x40600000 */
18897 /* bltzals 0x42200000 bgezals 0x42600000 */
18898 insn &= ~0x02200000;
18899 }
18900
18901 /* Make a label at the end for use with the branch. */
18902 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
18903 micromips_label_inc ();
f3ded42a 18904 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
df58fc94
RS
18905
18906 /* Refer to it. */
4d68580a
RS
18907 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
18908 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18909 fixp->fx_file = fragp->fr_file;
18910 fixp->fx_line = fragp->fr_line;
18911
18912 /* Branch over the jump. */
4d68580a 18913 buf = write_compressed_insn (buf, insn, 4);
8484fb75 18914
df58fc94 18915 if (!compact)
8484fb75
MR
18916 {
18917 /* nop */
18918 if (insn32)
18919 buf = write_compressed_insn (buf, 0x00000000, 4);
18920 else
18921 buf = write_compressed_insn (buf, 0x0c00, 2);
18922 }
df58fc94
RS
18923 }
18924
ce8ad872 18925 if (!pic)
df58fc94 18926 {
7bd374a4
MR
18927 unsigned long jal = (short_ds || nods
18928 ? 0x74000000 : 0xf4000000); /* jal/s */
2309ddf2 18929
df58fc94
RS
18930 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
18931 insn = al ? jal : 0xd4000000;
18932
bbd27b76
MR
18933 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18934 fragp->fr_symbol, fragp->fr_offset,
18935 FALSE, BFD_RELOC_MICROMIPS_JMP);
df58fc94
RS
18936 fixp->fx_file = fragp->fr_file;
18937 fixp->fx_line = fragp->fr_line;
18938
4d68580a 18939 buf = write_compressed_insn (buf, insn, 4);
8484fb75 18940
7bd374a4 18941 if (compact || nods)
8484fb75
MR
18942 {
18943 /* nop */
18944 if (insn32)
18945 buf = write_compressed_insn (buf, 0x00000000, 4);
18946 else
18947 buf = write_compressed_insn (buf, 0x0c00, 2);
18948 }
df58fc94
RS
18949 }
18950 else
18951 {
18952 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
18953
18954 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
18955 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18956 insn |= at << MICROMIPSOP_SH_RT;
18957
bbd27b76
MR
18958 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18959 fragp->fr_symbol, fragp->fr_offset,
18960 FALSE, BFD_RELOC_MICROMIPS_GOT16);
df58fc94
RS
18961 fixp->fx_file = fragp->fr_file;
18962 fixp->fx_line = fragp->fr_line;
18963
4d68580a 18964 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
18965
18966 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
18967 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18968 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18969
bbd27b76
MR
18970 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18971 fragp->fr_symbol, fragp->fr_offset,
18972 FALSE, BFD_RELOC_MICROMIPS_LO16);
df58fc94
RS
18973 fixp->fx_file = fragp->fr_file;
18974 fixp->fx_line = fragp->fr_line;
18975
4d68580a 18976 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18977
8484fb75
MR
18978 if (insn32)
18979 {
18980 /* jr/jalr $at */
18981 insn = 0x00000f3c | (al ? RA : ZERO) << MICROMIPSOP_SH_RT;
18982 insn |= at << MICROMIPSOP_SH_RS;
18983
18984 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18985
7bd374a4 18986 if (compact || nods)
8484fb75
MR
18987 /* nop */
18988 buf = write_compressed_insn (buf, 0x00000000, 4);
18989 }
18990 else
18991 {
18992 /* jr/jrc/jalr/jalrs $at */
18993 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
7bd374a4 18994 unsigned long jr = compact || nods ? 0x45a0 : 0x4580; /* jr/c */
8484fb75
MR
18995
18996 insn = al ? jalr : jr;
18997 insn |= at << MICROMIPSOP_SH_MJ;
18998
18999 buf = write_compressed_insn (buf, insn, 2);
7bd374a4
MR
19000 if (al && nods)
19001 {
19002 /* nop */
19003 if (short_ds)
19004 buf = write_compressed_insn (buf, 0x0c00, 2);
19005 else
19006 buf = write_compressed_insn (buf, 0x00000000, 4);
19007 }
8484fb75 19008 }
df58fc94
RS
19009 }
19010
4d68580a 19011 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
19012 return;
19013 }
19014
252b5132
RH
19015 if (RELAX_MIPS16_P (fragp->fr_subtype))
19016 {
19017 int type;
3ccad066 19018 const struct mips_int_operand *operand;
252b5132 19019 offsetT val;
5c04167a 19020 char *buf;
8507b6e7 19021 unsigned int user_length;
9d862524 19022 bfd_boolean need_reloc;
252b5132 19023 unsigned long insn;
8507b6e7 19024 bfd_boolean mac;
5c04167a 19025 bfd_boolean ext;
88a7ef16 19026 segT symsec;
252b5132
RH
19027
19028 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
3ccad066 19029 operand = mips16_immed_operand (type, FALSE);
252b5132 19030
8507b6e7 19031 mac = RELAX_MIPS16_MACRO (fragp->fr_subtype);
5c04167a 19032 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
88a7ef16 19033 val = resolve_symbol_value (fragp->fr_symbol) + fragp->fr_offset;
9d862524
MR
19034
19035 symsec = S_GET_SEGMENT (fragp->fr_symbol);
19036 need_reloc = (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
8507b6e7 19037 || (operand->root.type == OP_PCREL && !mac
9d862524
MR
19038 ? asec != symsec
19039 : !bfd_is_abs_section (symsec)));
19040
8507b6e7 19041 if (operand->root.type == OP_PCREL && !mac)
252b5132 19042 {
3ccad066 19043 const struct mips_pcrel_operand *pcrel_op;
252b5132 19044
3ccad066 19045 pcrel_op = (const struct mips_pcrel_operand *) operand;
252b5132 19046
14f72d45 19047 if (pcrel_op->include_isa_bit && !need_reloc)
252b5132 19048 {
37b2d327
MR
19049 if (!mips_ignore_branch_isa
19050 && !ELF_ST_IS_MIPS16 (S_GET_OTHER (fragp->fr_symbol)))
14f72d45
MR
19051 as_bad_where (fragp->fr_file, fragp->fr_line,
19052 _("branch to a symbol in another ISA mode"));
19053 else if ((fragp->fr_offset & 0x1) != 0)
19054 as_bad_where (fragp->fr_file, fragp->fr_line,
19055 _("branch to misaligned address (0x%lx)"),
52031738
FS
19056 (long) (resolve_symbol_value (fragp->fr_symbol)
19057 + (fragp->fr_offset & ~1)));
252b5132 19058 }
252b5132 19059
14f72d45 19060 val = mips16_pcrel_val (fragp, pcrel_op, val, 0);
252b5132
RH
19061
19062 /* Make sure the section winds up with the alignment we have
19063 assumed. */
3ccad066
RS
19064 if (operand->shift > 0)
19065 record_alignment (asec, operand->shift);
252b5132
RH
19066 }
19067
8507b6e7
MR
19068 if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
19069 || RELAX_MIPS16_DSLOT (fragp->fr_subtype))
19070 {
19071 if (mac)
19072 as_warn_where (fragp->fr_file, fragp->fr_line,
19073 _("macro instruction expanded into multiple "
19074 "instructions in a branch delay slot"));
19075 else if (ext)
19076 as_warn_where (fragp->fr_file, fragp->fr_line,
19077 _("extended instruction in a branch delay slot"));
19078 }
19079 else if (RELAX_MIPS16_NOMACRO (fragp->fr_subtype) && mac)
252b5132 19080 as_warn_where (fragp->fr_file, fragp->fr_line,
8507b6e7
MR
19081 _("macro instruction expanded into multiple "
19082 "instructions"));
252b5132 19083
5c04167a 19084 buf = fragp->fr_literal + fragp->fr_fix;
252b5132 19085
4d68580a 19086 insn = read_compressed_insn (buf, 2);
5c04167a
RS
19087 if (ext)
19088 insn |= MIPS16_EXTEND;
252b5132 19089
5c04167a
RS
19090 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
19091 user_length = 4;
19092 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
19093 user_length = 2;
19094 else
19095 user_length = 0;
19096
8507b6e7 19097 if (mac)
c9775dde 19098 {
8507b6e7
MR
19099 unsigned long reg;
19100 unsigned long new;
19101 unsigned long op;
25499ac7 19102 bfd_boolean e2;
8507b6e7
MR
19103
19104 gas_assert (type == 'A' || type == 'B' || type == 'E');
19105 gas_assert (RELAX_MIPS16_SYM32 (fragp->fr_subtype));
c9775dde 19106
25499ac7
MR
19107 e2 = RELAX_MIPS16_E2 (fragp->fr_subtype);
19108
8507b6e7 19109 if (need_reloc)
c9775dde 19110 {
8507b6e7
MR
19111 fixS *fixp;
19112
19113 gas_assert (!RELAX_MIPS16_PIC (fragp->fr_subtype));
19114
19115 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
19116 fragp->fr_symbol, fragp->fr_offset,
19117 FALSE, BFD_RELOC_MIPS16_HI16_S);
19118 fixp->fx_file = fragp->fr_file;
19119 fixp->fx_line = fragp->fr_line;
19120
25499ac7 19121 fixp = fix_new (fragp, buf - fragp->fr_literal + (e2 ? 4 : 8), 4,
8507b6e7
MR
19122 fragp->fr_symbol, fragp->fr_offset,
19123 FALSE, BFD_RELOC_MIPS16_LO16);
19124 fixp->fx_file = fragp->fr_file;
19125 fixp->fx_line = fragp->fr_line;
19126
19127 val = 0;
19128 }
19129
19130 switch (insn & 0xf800)
19131 {
19132 case 0x0800: /* ADDIU */
19133 reg = (insn >> 8) & 0x7;
19134 op = 0xf0004800 | (reg << 8);
c9775dde 19135 break;
8507b6e7
MR
19136 case 0xb000: /* LW */
19137 reg = (insn >> 8) & 0x7;
19138 op = 0xf0009800 | (reg << 8) | (reg << 5);
c9775dde 19139 break;
8507b6e7
MR
19140 case 0xf800: /* I64 */
19141 reg = (insn >> 5) & 0x7;
19142 switch (insn & 0x0700)
19143 {
19144 case 0x0400: /* LD */
19145 op = 0xf0003800 | (reg << 8) | (reg << 5);
19146 break;
19147 case 0x0600: /* DADDIU */
19148 op = 0xf000fd00 | (reg << 5);
19149 break;
19150 default:
19151 abort ();
19152 }
19153 break;
19154 default:
19155 abort ();
c9775dde 19156 }
8507b6e7 19157
25499ac7 19158 new = (e2 ? 0xf0006820 : 0xf0006800) | (reg << 8); /* LUI/LI */
8507b6e7
MR
19159 new |= mips16_immed_extend ((val + 0x8000) >> 16, 16);
19160 buf = write_compressed_insn (buf, new, 4);
25499ac7
MR
19161 if (!e2)
19162 {
19163 new = 0xf4003000 | (reg << 8) | (reg << 5); /* SLL */
19164 buf = write_compressed_insn (buf, new, 4);
19165 }
8507b6e7
MR
19166 op |= mips16_immed_extend (val, 16);
19167 buf = write_compressed_insn (buf, op, 4);
19168
25499ac7 19169 fragp->fr_fix += e2 ? 8 : 12;
8507b6e7
MR
19170 }
19171 else
19172 {
19173 unsigned int length = ext ? 4 : 2;
19174
19175 if (need_reloc)
c9775dde 19176 {
8507b6e7 19177 bfd_reloc_code_real_type reloc = BFD_RELOC_NONE;
8507b6e7 19178 fixS *fixp;
c9775dde 19179
8507b6e7
MR
19180 switch (type)
19181 {
19182 case 'p':
19183 case 'q':
19184 reloc = BFD_RELOC_MIPS16_16_PCREL_S1;
19185 break;
19186 default:
19187 break;
19188 }
19189 if (mac || reloc == BFD_RELOC_NONE)
19190 as_bad_where (fragp->fr_file, fragp->fr_line,
19191 _("unsupported relocation"));
19192 else if (ext)
19193 {
bbd27b76
MR
19194 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
19195 fragp->fr_symbol, fragp->fr_offset,
19196 TRUE, reloc);
8507b6e7
MR
19197 fixp->fx_file = fragp->fr_file;
19198 fixp->fx_line = fragp->fr_line;
19199 }
19200 else
19201 as_bad_where (fragp->fr_file, fragp->fr_line,
19202 _("invalid unextended operand value"));
c9775dde 19203 }
eefc3365 19204 else
8507b6e7
MR
19205 mips16_immed (fragp->fr_file, fragp->fr_line, type,
19206 BFD_RELOC_UNUSED, val, user_length, &insn);
252b5132 19207
8507b6e7
MR
19208 gas_assert (mips16_opcode_length (insn) == length);
19209 write_compressed_insn (buf, insn, length);
19210 fragp->fr_fix += length;
19211 }
252b5132
RH
19212 }
19213 else
19214 {
df58fc94
RS
19215 relax_substateT subtype = fragp->fr_subtype;
19216 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
19217 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
871a6bd2 19218 unsigned int first, second;
4d7206a2 19219 fixS *fixp;
252b5132 19220
df58fc94
RS
19221 first = RELAX_FIRST (subtype);
19222 second = RELAX_SECOND (subtype);
4d7206a2 19223 fixp = (fixS *) fragp->fr_opcode;
252b5132 19224
df58fc94
RS
19225 /* If the delay slot chosen does not match the size of the instruction,
19226 then emit a warning. */
19227 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
19228 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
19229 {
19230 relax_substateT s;
19231 const char *msg;
19232
19233 s = subtype & (RELAX_DELAY_SLOT_16BIT
19234 | RELAX_DELAY_SLOT_SIZE_FIRST
19235 | RELAX_DELAY_SLOT_SIZE_SECOND);
19236 msg = macro_warning (s);
19237 if (msg != NULL)
db9b2be4 19238 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94
RS
19239 subtype &= ~s;
19240 }
19241
584892a6 19242 /* Possibly emit a warning if we've chosen the longer option. */
df58fc94 19243 if (use_second == second_longer)
584892a6 19244 {
df58fc94
RS
19245 relax_substateT s;
19246 const char *msg;
19247
19248 s = (subtype
19249 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
19250 msg = macro_warning (s);
19251 if (msg != NULL)
db9b2be4 19252 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94 19253 subtype &= ~s;
584892a6
RS
19254 }
19255
4d7206a2
RS
19256 /* Go through all the fixups for the first sequence. Disable them
19257 (by marking them as done) if we're going to use the second
19258 sequence instead. */
19259 while (fixp
19260 && fixp->fx_frag == fragp
90bd3c90 19261 && fixp->fx_where + second < fragp->fr_fix)
4d7206a2 19262 {
df58fc94 19263 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
19264 fixp->fx_done = 1;
19265 fixp = fixp->fx_next;
19266 }
252b5132 19267
4d7206a2
RS
19268 /* Go through the fixups for the second sequence. Disable them if
19269 we're going to use the first sequence, otherwise adjust their
19270 addresses to account for the relaxation. */
19271 while (fixp && fixp->fx_frag == fragp)
19272 {
df58fc94 19273 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
19274 fixp->fx_where -= first;
19275 else
19276 fixp->fx_done = 1;
19277 fixp = fixp->fx_next;
19278 }
19279
19280 /* Now modify the frag contents. */
df58fc94 19281 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
19282 {
19283 char *start;
19284
19285 start = fragp->fr_literal + fragp->fr_fix - first - second;
19286 memmove (start, start + first, second);
19287 fragp->fr_fix -= first;
19288 }
19289 else
19290 fragp->fr_fix -= second;
252b5132
RH
19291 }
19292}
19293
252b5132
RH
19294/* This function is called after the relocs have been generated.
19295 We've been storing mips16 text labels as odd. Here we convert them
19296 back to even for the convenience of the debugger. */
19297
19298void
17a2f251 19299mips_frob_file_after_relocs (void)
252b5132
RH
19300{
19301 asymbol **syms;
19302 unsigned int count, i;
19303
252b5132
RH
19304 syms = bfd_get_outsymbols (stdoutput);
19305 count = bfd_get_symcount (stdoutput);
19306 for (i = 0; i < count; i++, syms++)
df58fc94
RS
19307 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
19308 && ((*syms)->value & 1) != 0)
19309 {
19310 (*syms)->value &= ~1;
19311 /* If the symbol has an odd size, it was probably computed
19312 incorrectly, so adjust that as well. */
19313 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
19314 ++elf_symbol (*syms)->internal_elf_sym.st_size;
19315 }
252b5132
RH
19316}
19317
a1facbec
MR
19318/* This function is called whenever a label is defined, including fake
19319 labels instantiated off the dot special symbol. It is used when
19320 handling branch delays; if a branch has a label, we assume we cannot
19321 move it. This also bumps the value of the symbol by 1 in compressed
19322 code. */
252b5132 19323
e1b47bd5 19324static void
a1facbec 19325mips_record_label (symbolS *sym)
252b5132 19326{
a8dbcb85 19327 segment_info_type *si = seg_info (now_seg);
252b5132
RH
19328 struct insn_label_list *l;
19329
19330 if (free_insn_labels == NULL)
325801bd 19331 l = XNEW (struct insn_label_list);
252b5132
RH
19332 else
19333 {
19334 l = free_insn_labels;
19335 free_insn_labels = l->next;
19336 }
19337
19338 l->label = sym;
a8dbcb85
TS
19339 l->next = si->label_list;
19340 si->label_list = l;
a1facbec 19341}
07a53e5c 19342
a1facbec
MR
19343/* This function is called as tc_frob_label() whenever a label is defined
19344 and adds a DWARF-2 record we only want for true labels. */
19345
19346void
19347mips_define_label (symbolS *sym)
19348{
19349 mips_record_label (sym);
07a53e5c 19350 dwarf2_emit_label (sym);
252b5132 19351}
e1b47bd5
RS
19352
19353/* This function is called by tc_new_dot_label whenever a new dot symbol
19354 is defined. */
19355
19356void
19357mips_add_dot_label (symbolS *sym)
19358{
19359 mips_record_label (sym);
19360 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
19361 mips_compressed_mark_label (sym);
19362}
252b5132 19363\f
351cdf24
MF
19364/* Converting ASE flags from internal to .MIPS.abiflags values. */
19365static unsigned int
19366mips_convert_ase_flags (int ase)
19367{
19368 unsigned int ext_ases = 0;
19369
19370 if (ase & ASE_DSP)
19371 ext_ases |= AFL_ASE_DSP;
19372 if (ase & ASE_DSPR2)
19373 ext_ases |= AFL_ASE_DSPR2;
8f4f9071
MF
19374 if (ase & ASE_DSPR3)
19375 ext_ases |= AFL_ASE_DSPR3;
351cdf24
MF
19376 if (ase & ASE_EVA)
19377 ext_ases |= AFL_ASE_EVA;
19378 if (ase & ASE_MCU)
19379 ext_ases |= AFL_ASE_MCU;
19380 if (ase & ASE_MDMX)
19381 ext_ases |= AFL_ASE_MDMX;
19382 if (ase & ASE_MIPS3D)
19383 ext_ases |= AFL_ASE_MIPS3D;
19384 if (ase & ASE_MT)
19385 ext_ases |= AFL_ASE_MT;
19386 if (ase & ASE_SMARTMIPS)
19387 ext_ases |= AFL_ASE_SMARTMIPS;
19388 if (ase & ASE_VIRT)
19389 ext_ases |= AFL_ASE_VIRT;
19390 if (ase & ASE_MSA)
19391 ext_ases |= AFL_ASE_MSA;
19392 if (ase & ASE_XPA)
19393 ext_ases |= AFL_ASE_XPA;
25499ac7
MR
19394 if (ase & ASE_MIPS16E2)
19395 ext_ases |= file_ase_mips16 ? AFL_ASE_MIPS16E2 : 0;
730c3174
SE
19396 if (ase & ASE_CRC)
19397 ext_ases |= AFL_ASE_CRC;
6f20c942
FS
19398 if (ase & ASE_GINV)
19399 ext_ases |= AFL_ASE_GINV;
8095d2f7
CX
19400 if (ase & ASE_LOONGSON_MMI)
19401 ext_ases |= AFL_ASE_LOONGSON_MMI;
716c08de
CX
19402 if (ase & ASE_LOONGSON_CAM)
19403 ext_ases |= AFL_ASE_LOONGSON_CAM;
bdc6c06e
CX
19404 if (ase & ASE_LOONGSON_EXT)
19405 ext_ases |= AFL_ASE_LOONGSON_EXT;
a693765e
CX
19406 if (ase & ASE_LOONGSON_EXT2)
19407 ext_ases |= AFL_ASE_LOONGSON_EXT2;
351cdf24
MF
19408
19409 return ext_ases;
19410}
252b5132
RH
19411/* Some special processing for a MIPS ELF file. */
19412
19413void
17a2f251 19414mips_elf_final_processing (void)
252b5132 19415{
351cdf24
MF
19416 int fpabi;
19417 Elf_Internal_ABIFlags_v0 flags;
19418
19419 flags.version = 0;
19420 flags.isa_rev = 0;
19421 switch (file_mips_opts.isa)
19422 {
19423 case INSN_ISA1:
19424 flags.isa_level = 1;
19425 break;
19426 case INSN_ISA2:
19427 flags.isa_level = 2;
19428 break;
19429 case INSN_ISA3:
19430 flags.isa_level = 3;
19431 break;
19432 case INSN_ISA4:
19433 flags.isa_level = 4;
19434 break;
19435 case INSN_ISA5:
19436 flags.isa_level = 5;
19437 break;
19438 case INSN_ISA32:
19439 flags.isa_level = 32;
19440 flags.isa_rev = 1;
19441 break;
19442 case INSN_ISA32R2:
19443 flags.isa_level = 32;
19444 flags.isa_rev = 2;
19445 break;
19446 case INSN_ISA32R3:
19447 flags.isa_level = 32;
19448 flags.isa_rev = 3;
19449 break;
19450 case INSN_ISA32R5:
19451 flags.isa_level = 32;
19452 flags.isa_rev = 5;
19453 break;
09c14161
MF
19454 case INSN_ISA32R6:
19455 flags.isa_level = 32;
19456 flags.isa_rev = 6;
19457 break;
351cdf24
MF
19458 case INSN_ISA64:
19459 flags.isa_level = 64;
19460 flags.isa_rev = 1;
19461 break;
19462 case INSN_ISA64R2:
19463 flags.isa_level = 64;
19464 flags.isa_rev = 2;
19465 break;
19466 case INSN_ISA64R3:
19467 flags.isa_level = 64;
19468 flags.isa_rev = 3;
19469 break;
19470 case INSN_ISA64R5:
19471 flags.isa_level = 64;
19472 flags.isa_rev = 5;
19473 break;
09c14161
MF
19474 case INSN_ISA64R6:
19475 flags.isa_level = 64;
19476 flags.isa_rev = 6;
19477 break;
351cdf24
MF
19478 }
19479
19480 flags.gpr_size = file_mips_opts.gp == 32 ? AFL_REG_32 : AFL_REG_64;
19481 flags.cpr1_size = file_mips_opts.soft_float ? AFL_REG_NONE
19482 : (file_mips_opts.ase & ASE_MSA) ? AFL_REG_128
19483 : (file_mips_opts.fp == 64) ? AFL_REG_64
19484 : AFL_REG_32;
19485 flags.cpr2_size = AFL_REG_NONE;
19486 flags.fp_abi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19487 Tag_GNU_MIPS_ABI_FP);
19488 flags.isa_ext = bfd_mips_isa_ext (stdoutput);
19489 flags.ases = mips_convert_ase_flags (file_mips_opts.ase);
19490 if (file_ase_mips16)
19491 flags.ases |= AFL_ASE_MIPS16;
19492 if (file_ase_micromips)
19493 flags.ases |= AFL_ASE_MICROMIPS;
19494 flags.flags1 = 0;
19495 if ((ISA_HAS_ODD_SINGLE_FPR (file_mips_opts.isa, file_mips_opts.arch)
19496 || file_mips_opts.fp == 64)
19497 && file_mips_opts.oddspreg)
19498 flags.flags1 |= AFL_FLAGS1_ODDSPREG;
19499 flags.flags2 = 0;
19500
19501 bfd_mips_elf_swap_abiflags_v0_out (stdoutput, &flags,
19502 ((Elf_External_ABIFlags_v0 *)
19503 mips_flags_frag));
19504
252b5132 19505 /* Write out the register information. */
316f5878 19506 if (mips_abi != N64_ABI)
252b5132
RH
19507 {
19508 Elf32_RegInfo s;
19509
19510 s.ri_gprmask = mips_gprmask;
19511 s.ri_cprmask[0] = mips_cprmask[0];
19512 s.ri_cprmask[1] = mips_cprmask[1];
19513 s.ri_cprmask[2] = mips_cprmask[2];
19514 s.ri_cprmask[3] = mips_cprmask[3];
19515 /* The gp_value field is set by the MIPS ELF backend. */
19516
19517 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
19518 ((Elf32_External_RegInfo *)
19519 mips_regmask_frag));
19520 }
19521 else
19522 {
19523 Elf64_Internal_RegInfo s;
19524
19525 s.ri_gprmask = mips_gprmask;
19526 s.ri_pad = 0;
19527 s.ri_cprmask[0] = mips_cprmask[0];
19528 s.ri_cprmask[1] = mips_cprmask[1];
19529 s.ri_cprmask[2] = mips_cprmask[2];
19530 s.ri_cprmask[3] = mips_cprmask[3];
19531 /* The gp_value field is set by the MIPS ELF backend. */
19532
19533 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
19534 ((Elf64_External_RegInfo *)
19535 mips_regmask_frag));
19536 }
19537
19538 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
19539 sort of BFD interface for this. */
19540 if (mips_any_noreorder)
19541 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
19542 if (mips_pic != NO_PIC)
143d77c5 19543 {
8b828383 19544 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
19545 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
19546 }
19547 if (mips_abicalls)
19548 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 19549
b015e599
AP
19550 /* Set MIPS ELF flags for ASEs. Note that not all ASEs have flags
19551 defined at present; this might need to change in future. */
a4672219
TS
19552 if (file_ase_mips16)
19553 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
df58fc94
RS
19554 if (file_ase_micromips)
19555 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
919731af 19556 if (file_mips_opts.ase & ASE_MDMX)
deec1734 19557 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 19558
bdaaa2e1 19559 /* Set the MIPS ELF ABI flags. */
316f5878 19560 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 19561 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 19562 else if (mips_abi == O64_ABI)
252b5132 19563 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 19564 else if (mips_abi == EABI_ABI)
252b5132 19565 {
bad1aba3 19566 if (file_mips_opts.gp == 64)
252b5132
RH
19567 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
19568 else
19569 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
19570 }
be00bddd 19571
defc8e2b 19572 /* Nothing to do for N32_ABI or N64_ABI. */
252b5132
RH
19573
19574 if (mips_32bitmode)
19575 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
ad3fea08 19576
7361da2c 19577 if (mips_nan2008 == 1)
ba92f887
MR
19578 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NAN2008;
19579
ad3fea08 19580 /* 32 bit code with 64 bit FP registers. */
351cdf24
MF
19581 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19582 Tag_GNU_MIPS_ABI_FP);
19583 if (fpabi == Val_GNU_MIPS_ABI_FP_OLD_64)
f1c38003 19584 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_FP64;
252b5132 19585}
252b5132 19586\f
beae10d5 19587typedef struct proc {
9b2f1d35
EC
19588 symbolS *func_sym;
19589 symbolS *func_end_sym;
beae10d5
KH
19590 unsigned long reg_mask;
19591 unsigned long reg_offset;
19592 unsigned long fpreg_mask;
19593 unsigned long fpreg_offset;
19594 unsigned long frame_offset;
19595 unsigned long frame_reg;
19596 unsigned long pc_reg;
19597} procS;
252b5132
RH
19598
19599static procS cur_proc;
19600static procS *cur_proc_ptr;
19601static int numprocs;
19602
df58fc94
RS
19603/* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
19604 as "2", and a normal nop as "0". */
19605
19606#define NOP_OPCODE_MIPS 0
19607#define NOP_OPCODE_MIPS16 1
19608#define NOP_OPCODE_MICROMIPS 2
742a56fe
RS
19609
19610char
19611mips_nop_opcode (void)
19612{
df58fc94
RS
19613 if (seg_info (now_seg)->tc_segment_info_data.micromips)
19614 return NOP_OPCODE_MICROMIPS;
19615 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
19616 return NOP_OPCODE_MIPS16;
19617 else
19618 return NOP_OPCODE_MIPS;
742a56fe
RS
19619}
19620
df58fc94
RS
19621/* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
19622 32-bit microMIPS NOPs here (if applicable). */
a19d8eb0 19623
0a9ef439 19624void
17a2f251 19625mips_handle_align (fragS *fragp)
a19d8eb0 19626{
df58fc94 19627 char nop_opcode;
742a56fe 19628 char *p;
c67a084a
NC
19629 int bytes, size, excess;
19630 valueT opcode;
742a56fe 19631
0a9ef439
RH
19632 if (fragp->fr_type != rs_align_code)
19633 return;
19634
742a56fe 19635 p = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
19636 nop_opcode = *p;
19637 switch (nop_opcode)
a19d8eb0 19638 {
df58fc94
RS
19639 case NOP_OPCODE_MICROMIPS:
19640 opcode = micromips_nop32_insn.insn_opcode;
19641 size = 4;
19642 break;
19643 case NOP_OPCODE_MIPS16:
c67a084a
NC
19644 opcode = mips16_nop_insn.insn_opcode;
19645 size = 2;
df58fc94
RS
19646 break;
19647 case NOP_OPCODE_MIPS:
19648 default:
c67a084a
NC
19649 opcode = nop_insn.insn_opcode;
19650 size = 4;
df58fc94 19651 break;
c67a084a 19652 }
a19d8eb0 19653
c67a084a
NC
19654 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
19655 excess = bytes % size;
df58fc94
RS
19656
19657 /* Handle the leading part if we're not inserting a whole number of
19658 instructions, and make it the end of the fixed part of the frag.
19659 Try to fit in a short microMIPS NOP if applicable and possible,
19660 and use zeroes otherwise. */
19661 gas_assert (excess < 4);
19662 fragp->fr_fix += excess;
19663 switch (excess)
c67a084a 19664 {
df58fc94
RS
19665 case 3:
19666 *p++ = '\0';
19667 /* Fall through. */
19668 case 2:
833794fc 19669 if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
df58fc94 19670 {
4d68580a 19671 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
df58fc94
RS
19672 break;
19673 }
19674 *p++ = '\0';
19675 /* Fall through. */
19676 case 1:
19677 *p++ = '\0';
19678 /* Fall through. */
19679 case 0:
19680 break;
a19d8eb0 19681 }
c67a084a
NC
19682
19683 md_number_to_chars (p, opcode, size);
19684 fragp->fr_var = size;
a19d8eb0
CP
19685}
19686
252b5132 19687static long
17a2f251 19688get_number (void)
252b5132
RH
19689{
19690 int negative = 0;
19691 long val = 0;
19692
19693 if (*input_line_pointer == '-')
19694 {
19695 ++input_line_pointer;
19696 negative = 1;
19697 }
3882b010 19698 if (!ISDIGIT (*input_line_pointer))
956cd1d6 19699 as_bad (_("expected simple number"));
252b5132
RH
19700 if (input_line_pointer[0] == '0')
19701 {
19702 if (input_line_pointer[1] == 'x')
19703 {
19704 input_line_pointer += 2;
3882b010 19705 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
19706 {
19707 val <<= 4;
19708 val |= hex_value (*input_line_pointer++);
19709 }
19710 return negative ? -val : val;
19711 }
19712 else
19713 {
19714 ++input_line_pointer;
3882b010 19715 while (ISDIGIT (*input_line_pointer))
252b5132
RH
19716 {
19717 val <<= 3;
19718 val |= *input_line_pointer++ - '0';
19719 }
19720 return negative ? -val : val;
19721 }
19722 }
3882b010 19723 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
19724 {
19725 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
19726 *input_line_pointer, *input_line_pointer);
956cd1d6 19727 as_warn (_("invalid number"));
252b5132
RH
19728 return -1;
19729 }
3882b010 19730 while (ISDIGIT (*input_line_pointer))
252b5132
RH
19731 {
19732 val *= 10;
19733 val += *input_line_pointer++ - '0';
19734 }
19735 return negative ? -val : val;
19736}
19737
19738/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
19739 is an initial number which is the ECOFF file index. In the non-ECOFF
19740 case .file implies DWARF-2. */
19741
19742static void
17a2f251 19743s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 19744{
ecb4347a
DJ
19745 static int first_file_directive = 0;
19746
c5dd6aab
DJ
19747 if (ECOFF_DEBUGGING)
19748 {
19749 get_number ();
19750 s_app_file (0);
19751 }
19752 else
ecb4347a
DJ
19753 {
19754 char *filename;
19755
68d20676 19756 filename = dwarf2_directive_filename ();
ecb4347a
DJ
19757
19758 /* Versions of GCC up to 3.1 start files with a ".file"
19759 directive even for stabs output. Make sure that this
19760 ".file" is handled. Note that you need a version of GCC
19761 after 3.1 in order to support DWARF-2 on MIPS. */
19762 if (filename != NULL && ! first_file_directive)
19763 {
19764 (void) new_logical_line (filename, -1);
c04f5787 19765 s_app_file_string (filename, 0);
ecb4347a
DJ
19766 }
19767 first_file_directive = 1;
19768 }
c5dd6aab
DJ
19769}
19770
19771/* The .loc directive, implying DWARF-2. */
252b5132
RH
19772
19773static void
17a2f251 19774s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 19775{
c5dd6aab
DJ
19776 if (!ECOFF_DEBUGGING)
19777 dwarf2_directive_loc (0);
252b5132
RH
19778}
19779
252b5132
RH
19780/* The .end directive. */
19781
19782static void
17a2f251 19783s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
19784{
19785 symbolS *p;
252b5132 19786
7a621144
DJ
19787 /* Following functions need their own .frame and .cprestore directives. */
19788 mips_frame_reg_valid = 0;
19789 mips_cprestore_valid = 0;
19790
252b5132
RH
19791 if (!is_end_of_line[(unsigned char) *input_line_pointer])
19792 {
19793 p = get_symbol ();
19794 demand_empty_rest_of_line ();
19795 }
19796 else
19797 p = NULL;
19798
fd361982 19799 if ((bfd_section_flags (now_seg) & SEC_CODE) == 0)
252b5132
RH
19800 as_warn (_(".end not in text section"));
19801
19802 if (!cur_proc_ptr)
19803 {
1661c76c 19804 as_warn (_(".end directive without a preceding .ent directive"));
252b5132
RH
19805 demand_empty_rest_of_line ();
19806 return;
19807 }
19808
19809 if (p != NULL)
19810 {
9c2799c2 19811 gas_assert (S_GET_NAME (p));
9b2f1d35 19812 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
1661c76c 19813 as_warn (_(".end symbol does not match .ent symbol"));
ecb4347a
DJ
19814
19815 if (debug_type == DEBUG_STABS)
19816 stabs_generate_asm_endfunc (S_GET_NAME (p),
19817 S_GET_NAME (p));
252b5132
RH
19818 }
19819 else
19820 as_warn (_(".end directive missing or unknown symbol"));
19821
9b2f1d35
EC
19822 /* Create an expression to calculate the size of the function. */
19823 if (p && cur_proc_ptr)
19824 {
19825 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
325801bd 19826 expressionS *exp = XNEW (expressionS);
9b2f1d35
EC
19827
19828 obj->size = exp;
19829 exp->X_op = O_subtract;
19830 exp->X_add_symbol = symbol_temp_new_now ();
19831 exp->X_op_symbol = p;
19832 exp->X_add_number = 0;
19833
19834 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
19835 }
19836
5ff6a06c
MR
19837#ifdef md_flush_pending_output
19838 md_flush_pending_output ();
19839#endif
19840
ecb4347a 19841 /* Generate a .pdr section. */
f3ded42a 19842 if (!ECOFF_DEBUGGING && mips_flag_pdr)
ecb4347a
DJ
19843 {
19844 segT saved_seg = now_seg;
19845 subsegT saved_subseg = now_subseg;
ecb4347a
DJ
19846 expressionS exp;
19847 char *fragp;
252b5132 19848
9c2799c2 19849 gas_assert (pdr_seg);
ecb4347a 19850 subseg_set (pdr_seg, 0);
252b5132 19851
ecb4347a
DJ
19852 /* Write the symbol. */
19853 exp.X_op = O_symbol;
19854 exp.X_add_symbol = p;
19855 exp.X_add_number = 0;
19856 emit_expr (&exp, 4);
252b5132 19857
ecb4347a 19858 fragp = frag_more (7 * 4);
252b5132 19859
17a2f251
TS
19860 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19861 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19862 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19863 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19864 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19865 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19866 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 19867
ecb4347a
DJ
19868 subseg_set (saved_seg, saved_subseg);
19869 }
252b5132
RH
19870
19871 cur_proc_ptr = NULL;
19872}
19873
19874/* The .aent and .ent directives. */
19875
19876static void
17a2f251 19877s_mips_ent (int aent)
252b5132 19878{
252b5132 19879 symbolS *symbolP;
252b5132
RH
19880
19881 symbolP = get_symbol ();
19882 if (*input_line_pointer == ',')
f9419b05 19883 ++input_line_pointer;
252b5132 19884 SKIP_WHITESPACE ();
3882b010 19885 if (ISDIGIT (*input_line_pointer)
d9a62219 19886 || *input_line_pointer == '-')
874e8986 19887 get_number ();
252b5132 19888
fd361982 19889 if ((bfd_section_flags (now_seg) & SEC_CODE) == 0)
1661c76c 19890 as_warn (_(".ent or .aent not in text section"));
252b5132
RH
19891
19892 if (!aent && cur_proc_ptr)
9a41af64 19893 as_warn (_("missing .end"));
252b5132
RH
19894
19895 if (!aent)
19896 {
7a621144
DJ
19897 /* This function needs its own .frame and .cprestore directives. */
19898 mips_frame_reg_valid = 0;
19899 mips_cprestore_valid = 0;
19900
252b5132
RH
19901 cur_proc_ptr = &cur_proc;
19902 memset (cur_proc_ptr, '\0', sizeof (procS));
19903
9b2f1d35 19904 cur_proc_ptr->func_sym = symbolP;
252b5132 19905
f9419b05 19906 ++numprocs;
ecb4347a
DJ
19907
19908 if (debug_type == DEBUG_STABS)
19909 stabs_generate_asm_func (S_GET_NAME (symbolP),
19910 S_GET_NAME (symbolP));
252b5132
RH
19911 }
19912
7c0fc524
MR
19913 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19914
252b5132
RH
19915 demand_empty_rest_of_line ();
19916}
19917
19918/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 19919 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 19920 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 19921 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
19922 symbol table (in the mdebug section). */
19923
19924static void
17a2f251 19925s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 19926{
f3ded42a
RS
19927 if (ECOFF_DEBUGGING)
19928 s_ignore (ignore);
19929 else
ecb4347a
DJ
19930 {
19931 long val;
252b5132 19932
ecb4347a
DJ
19933 if (cur_proc_ptr == (procS *) NULL)
19934 {
19935 as_warn (_(".frame outside of .ent"));
19936 demand_empty_rest_of_line ();
19937 return;
19938 }
252b5132 19939
ecb4347a
DJ
19940 cur_proc_ptr->frame_reg = tc_get_register (1);
19941
19942 SKIP_WHITESPACE ();
19943 if (*input_line_pointer++ != ','
19944 || get_absolute_expression_and_terminator (&val) != ',')
19945 {
1661c76c 19946 as_warn (_("bad .frame directive"));
ecb4347a
DJ
19947 --input_line_pointer;
19948 demand_empty_rest_of_line ();
19949 return;
19950 }
252b5132 19951
ecb4347a
DJ
19952 cur_proc_ptr->frame_offset = val;
19953 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 19954
252b5132 19955 demand_empty_rest_of_line ();
252b5132 19956 }
252b5132
RH
19957}
19958
bdaaa2e1
KH
19959/* The .fmask and .mask directives. If the mdebug section is present
19960 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 19961 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 19962 information correctly. We can't use the ecoff routines because they
252b5132
RH
19963 make reference to the ecoff symbol table (in the mdebug section). */
19964
19965static void
17a2f251 19966s_mips_mask (int reg_type)
252b5132 19967{
f3ded42a
RS
19968 if (ECOFF_DEBUGGING)
19969 s_ignore (reg_type);
19970 else
252b5132 19971 {
ecb4347a 19972 long mask, off;
252b5132 19973
ecb4347a
DJ
19974 if (cur_proc_ptr == (procS *) NULL)
19975 {
19976 as_warn (_(".mask/.fmask outside of .ent"));
19977 demand_empty_rest_of_line ();
19978 return;
19979 }
252b5132 19980
ecb4347a
DJ
19981 if (get_absolute_expression_and_terminator (&mask) != ',')
19982 {
1661c76c 19983 as_warn (_("bad .mask/.fmask directive"));
ecb4347a
DJ
19984 --input_line_pointer;
19985 demand_empty_rest_of_line ();
19986 return;
19987 }
252b5132 19988
ecb4347a
DJ
19989 off = get_absolute_expression ();
19990
19991 if (reg_type == 'F')
19992 {
19993 cur_proc_ptr->fpreg_mask = mask;
19994 cur_proc_ptr->fpreg_offset = off;
19995 }
19996 else
19997 {
19998 cur_proc_ptr->reg_mask = mask;
19999 cur_proc_ptr->reg_offset = off;
20000 }
20001
20002 demand_empty_rest_of_line ();
252b5132 20003 }
252b5132
RH
20004}
20005
316f5878
RS
20006/* A table describing all the processors gas knows about. Names are
20007 matched in the order listed.
e7af610e 20008
316f5878
RS
20009 To ease comparison, please keep this table in the same order as
20010 gcc's mips_cpu_info_table[]. */
e972090a
NC
20011static const struct mips_cpu_info mips_cpu_info_table[] =
20012{
6f2117ba 20013 /* Entries for generic ISAs. */
d16afab6
RS
20014 { "mips1", MIPS_CPU_IS_ISA, 0, ISA_MIPS1, CPU_R3000 },
20015 { "mips2", MIPS_CPU_IS_ISA, 0, ISA_MIPS2, CPU_R6000 },
20016 { "mips3", MIPS_CPU_IS_ISA, 0, ISA_MIPS3, CPU_R4000 },
20017 { "mips4", MIPS_CPU_IS_ISA, 0, ISA_MIPS4, CPU_R8000 },
20018 { "mips5", MIPS_CPU_IS_ISA, 0, ISA_MIPS5, CPU_MIPS5 },
20019 { "mips32", MIPS_CPU_IS_ISA, 0, ISA_MIPS32, CPU_MIPS32 },
20020 { "mips32r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
ae52f483
AB
20021 { "mips32r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R3, CPU_MIPS32R3 },
20022 { "mips32r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R5, CPU_MIPS32R5 },
7361da2c 20023 { "mips32r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R6, CPU_MIPS32R6 },
d16afab6
RS
20024 { "mips64", MIPS_CPU_IS_ISA, 0, ISA_MIPS64, CPU_MIPS64 },
20025 { "mips64r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R2, CPU_MIPS64R2 },
ae52f483
AB
20026 { "mips64r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R3, CPU_MIPS64R3 },
20027 { "mips64r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R5, CPU_MIPS64R5 },
7361da2c 20028 { "mips64r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R6, CPU_MIPS64R6 },
316f5878
RS
20029
20030 /* MIPS I */
d16afab6
RS
20031 { "r3000", 0, 0, ISA_MIPS1, CPU_R3000 },
20032 { "r2000", 0, 0, ISA_MIPS1, CPU_R3000 },
20033 { "r3900", 0, 0, ISA_MIPS1, CPU_R3900 },
316f5878
RS
20034
20035 /* MIPS II */
d16afab6 20036 { "r6000", 0, 0, ISA_MIPS2, CPU_R6000 },
316f5878
RS
20037
20038 /* MIPS III */
d16afab6
RS
20039 { "r4000", 0, 0, ISA_MIPS3, CPU_R4000 },
20040 { "r4010", 0, 0, ISA_MIPS2, CPU_R4010 },
20041 { "vr4100", 0, 0, ISA_MIPS3, CPU_VR4100 },
20042 { "vr4111", 0, 0, ISA_MIPS3, CPU_R4111 },
20043 { "vr4120", 0, 0, ISA_MIPS3, CPU_VR4120 },
20044 { "vr4130", 0, 0, ISA_MIPS3, CPU_VR4120 },
20045 { "vr4181", 0, 0, ISA_MIPS3, CPU_R4111 },
20046 { "vr4300", 0, 0, ISA_MIPS3, CPU_R4300 },
20047 { "r4400", 0, 0, ISA_MIPS3, CPU_R4400 },
20048 { "r4600", 0, 0, ISA_MIPS3, CPU_R4600 },
20049 { "orion", 0, 0, ISA_MIPS3, CPU_R4600 },
20050 { "r4650", 0, 0, ISA_MIPS3, CPU_R4650 },
20051 { "r5900", 0, 0, ISA_MIPS3, CPU_R5900 },
6f2117ba 20052 /* ST Microelectronics Loongson 2E and 2F cores. */
d16afab6 20053 { "loongson2e", 0, 0, ISA_MIPS3, CPU_LOONGSON_2E },
8095d2f7 20054 { "loongson2f", 0, ASE_LOONGSON_MMI, ISA_MIPS3, CPU_LOONGSON_2F },
316f5878
RS
20055
20056 /* MIPS IV */
d16afab6
RS
20057 { "r8000", 0, 0, ISA_MIPS4, CPU_R8000 },
20058 { "r10000", 0, 0, ISA_MIPS4, CPU_R10000 },
20059 { "r12000", 0, 0, ISA_MIPS4, CPU_R12000 },
20060 { "r14000", 0, 0, ISA_MIPS4, CPU_R14000 },
20061 { "r16000", 0, 0, ISA_MIPS4, CPU_R16000 },
20062 { "vr5000", 0, 0, ISA_MIPS4, CPU_R5000 },
20063 { "vr5400", 0, 0, ISA_MIPS4, CPU_VR5400 },
20064 { "vr5500", 0, 0, ISA_MIPS4, CPU_VR5500 },
20065 { "rm5200", 0, 0, ISA_MIPS4, CPU_R5000 },
20066 { "rm5230", 0, 0, ISA_MIPS4, CPU_R5000 },
20067 { "rm5231", 0, 0, ISA_MIPS4, CPU_R5000 },
20068 { "rm5261", 0, 0, ISA_MIPS4, CPU_R5000 },
20069 { "rm5721", 0, 0, ISA_MIPS4, CPU_R5000 },
20070 { "rm7000", 0, 0, ISA_MIPS4, CPU_RM7000 },
20071 { "rm9000", 0, 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
20072
20073 /* MIPS 32 */
d16afab6
RS
20074 { "4kc", 0, 0, ISA_MIPS32, CPU_MIPS32 },
20075 { "4km", 0, 0, ISA_MIPS32, CPU_MIPS32 },
20076 { "4kp", 0, 0, ISA_MIPS32, CPU_MIPS32 },
20077 { "4ksc", 0, ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
ad3fea08
TS
20078
20079 /* MIPS 32 Release 2 */
d16afab6
RS
20080 { "4kec", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20081 { "4kem", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20082 { "4kep", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20083 { "4ksd", 0, ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
20084 { "m4k", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20085 { "m4kp", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20086 { "m14k", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
20087 { "m14kc", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
20088 { "m14ke", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
20089 ISA_MIPS32R2, CPU_MIPS32R2 },
20090 { "m14kec", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
20091 ISA_MIPS32R2, CPU_MIPS32R2 },
20092 { "24kc", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20093 { "24kf2_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20094 { "24kf", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20095 { "24kf1_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 20096 /* Deprecated forms of the above. */
d16afab6
RS
20097 { "24kfx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20098 { "24kx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 20099 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
d16afab6
RS
20100 { "24kec", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
20101 { "24kef2_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
20102 { "24kef", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
20103 { "24kef1_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 20104 /* Deprecated forms of the above. */
d16afab6
RS
20105 { "24kefx", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
20106 { "24kex", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 20107 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
d16afab6
RS
20108 { "34kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20109 { "34kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20110 { "34kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20111 { "34kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 20112 /* Deprecated forms of the above. */
d16afab6
RS
20113 { "34kfx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20114 { "34kx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
711eefe4 20115 /* 34Kn is a 34kc without DSP. */
d16afab6 20116 { "34kn", 0, ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 20117 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
d16afab6
RS
20118 { "74kc", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20119 { "74kf2_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20120 { "74kf", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20121 { "74kf1_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20122 { "74kf3_2", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 20123 /* Deprecated forms of the above. */
d16afab6
RS
20124 { "74kfx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20125 { "74kx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
30f8113a 20126 /* 1004K cores are multiprocessor versions of the 34K. */
d16afab6
RS
20127 { "1004kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20128 { "1004kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20129 { "1004kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20130 { "1004kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
6f2117ba 20131 /* interaptiv is the new name for 1004kf. */
77403ce9 20132 { "interaptiv", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
38bf472a
MR
20133 { "interaptiv-mr2", 0,
20134 ASE_DSP | ASE_EVA | ASE_MT | ASE_MIPS16E2 | ASE_MIPS16E2_MT,
20135 ISA_MIPS32R3, CPU_INTERAPTIV_MR2 },
6f2117ba 20136 /* M5100 family. */
c6e5c03a
RS
20137 { "m5100", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
20138 { "m5101", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
bbaa46c0 20139 /* P5600 with EVA and Virtualization ASEs, other ASEs are optional. */
134c0c8b 20140 { "p5600", 0, ASE_VIRT | ASE_EVA | ASE_XPA, ISA_MIPS32R5, CPU_MIPS32R5 },
32b26a03 20141
316f5878 20142 /* MIPS 64 */
d16afab6
RS
20143 { "5kc", 0, 0, ISA_MIPS64, CPU_MIPS64 },
20144 { "5kf", 0, 0, ISA_MIPS64, CPU_MIPS64 },
20145 { "20kc", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
20146 { "25kf", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
ad3fea08 20147
6f2117ba 20148 /* Broadcom SB-1 CPU core. */
d16afab6 20149 { "sb1", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
6f2117ba 20150 /* Broadcom SB-1A CPU core. */
d16afab6 20151 { "sb1a", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
3739860c 20152
6f2117ba
PH
20153 /* MIPS 64 Release 2. */
20154 /* Loongson CPU core. */
20155 /* -march=loongson3a is an alias of -march=gs464 for compatibility. */
bdc6c06e 20156 { "loongson3a", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT,
ac8cb70f
CX
20157 ISA_MIPS64R2, CPU_GS464 },
20158 { "gs464", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT,
20159 ISA_MIPS64R2, CPU_GS464 },
bd782c07
CX
20160 { "gs464e", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT
20161 | ASE_LOONGSON_EXT2, ISA_MIPS64R2, CPU_GS464E },
9108bc33
CX
20162 { "gs264e", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT
20163 | ASE_LOONGSON_EXT2 | ASE_MSA | ASE_MSA64, ISA_MIPS64R2, CPU_GS264E },
ed163775 20164
6f2117ba 20165 /* Cavium Networks Octeon CPU core. */
d16afab6
RS
20166 { "octeon", 0, 0, ISA_MIPS64R2, CPU_OCTEON },
20167 { "octeon+", 0, 0, ISA_MIPS64R2, CPU_OCTEONP },
20168 { "octeon2", 0, 0, ISA_MIPS64R2, CPU_OCTEON2 },
2c629856 20169 { "octeon3", 0, ASE_VIRT | ASE_VIRT64, ISA_MIPS64R5, CPU_OCTEON3 },
967344c6 20170
52b6b6b9 20171 /* RMI Xlr */
d16afab6 20172 { "xlr", 0, 0, ISA_MIPS64, CPU_XLR },
52b6b6b9 20173
55a36193
MK
20174 /* Broadcom XLP.
20175 XLP is mostly like XLR, with the prominent exception that it is
20176 MIPS64R2 rather than MIPS64. */
d16afab6 20177 { "xlp", 0, 0, ISA_MIPS64R2, CPU_XLR },
55a36193 20178
6f2117ba 20179 /* MIPS 64 Release 6. */
bdc8beb4
MF
20180 { "i6400", 0, ASE_VIRT | ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
20181 { "i6500", 0, ASE_VIRT | ASE_MSA | ASE_CRC | ASE_GINV,
20182 ISA_MIPS64R6, CPU_MIPS64R6},
a4968f42 20183 { "p6600", 0, ASE_VIRT | ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
7ef0d297 20184
6f2117ba 20185 /* End marker. */
d16afab6 20186 { NULL, 0, 0, 0, 0 }
316f5878 20187};
e7af610e 20188
84ea6cf2 20189
316f5878
RS
20190/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
20191 with a final "000" replaced by "k". Ignore case.
e7af610e 20192
316f5878 20193 Note: this function is shared between GCC and GAS. */
c6c98b38 20194
b34976b6 20195static bfd_boolean
17a2f251 20196mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
20197{
20198 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
20199 given++, canonical++;
20200
20201 return ((*given == 0 && *canonical == 0)
20202 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
20203}
20204
20205
20206/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
20207 CPU name. We've traditionally allowed a lot of variation here.
20208
20209 Note: this function is shared between GCC and GAS. */
20210
b34976b6 20211static bfd_boolean
17a2f251 20212mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
20213{
20214 /* First see if the name matches exactly, or with a final "000"
20215 turned into "k". */
20216 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 20217 return TRUE;
316f5878
RS
20218
20219 /* If not, try comparing based on numerical designation alone.
20220 See if GIVEN is an unadorned number, or 'r' followed by a number. */
20221 if (TOLOWER (*given) == 'r')
20222 given++;
20223 if (!ISDIGIT (*given))
b34976b6 20224 return FALSE;
316f5878
RS
20225
20226 /* Skip over some well-known prefixes in the canonical name,
20227 hoping to find a number there too. */
20228 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
20229 canonical += 2;
20230 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
20231 canonical += 2;
20232 else if (TOLOWER (canonical[0]) == 'r')
20233 canonical += 1;
20234
20235 return mips_strict_matching_cpu_name_p (canonical, given);
20236}
20237
20238
20239/* Parse an option that takes the name of a processor as its argument.
20240 OPTION is the name of the option and CPU_STRING is the argument.
20241 Return the corresponding processor enumeration if the CPU_STRING is
20242 recognized, otherwise report an error and return null.
20243
20244 A similar function exists in GCC. */
e7af610e
NC
20245
20246static const struct mips_cpu_info *
17a2f251 20247mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 20248{
316f5878 20249 const struct mips_cpu_info *p;
e7af610e 20250
316f5878
RS
20251 /* 'from-abi' selects the most compatible architecture for the given
20252 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
20253 EABIs, we have to decide whether we're using the 32-bit or 64-bit
20254 version. Look first at the -mgp options, if given, otherwise base
20255 the choice on MIPS_DEFAULT_64BIT.
e7af610e 20256
316f5878
RS
20257 Treat NO_ABI like the EABIs. One reason to do this is that the
20258 plain 'mips' and 'mips64' configs have 'from-abi' as their default
20259 architecture. This code picks MIPS I for 'mips' and MIPS III for
20260 'mips64', just as we did in the days before 'from-abi'. */
20261 if (strcasecmp (cpu_string, "from-abi") == 0)
20262 {
20263 if (ABI_NEEDS_32BIT_REGS (mips_abi))
20264 return mips_cpu_info_from_isa (ISA_MIPS1);
20265
20266 if (ABI_NEEDS_64BIT_REGS (mips_abi))
20267 return mips_cpu_info_from_isa (ISA_MIPS3);
20268
bad1aba3 20269 if (file_mips_opts.gp >= 0)
20270 return mips_cpu_info_from_isa (file_mips_opts.gp == 32
0b35dfee 20271 ? ISA_MIPS1 : ISA_MIPS3);
316f5878
RS
20272
20273 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
20274 ? ISA_MIPS3
20275 : ISA_MIPS1);
20276 }
20277
20278 /* 'default' has traditionally been a no-op. Probably not very useful. */
20279 if (strcasecmp (cpu_string, "default") == 0)
20280 return 0;
20281
20282 for (p = mips_cpu_info_table; p->name != 0; p++)
20283 if (mips_matching_cpu_name_p (p->name, cpu_string))
20284 return p;
20285
1661c76c 20286 as_bad (_("bad value (%s) for %s"), cpu_string, option);
316f5878 20287 return 0;
e7af610e
NC
20288}
20289
316f5878
RS
20290/* Return the canonical processor information for ISA (a member of the
20291 ISA_MIPS* enumeration). */
20292
e7af610e 20293static const struct mips_cpu_info *
17a2f251 20294mips_cpu_info_from_isa (int isa)
e7af610e
NC
20295{
20296 int i;
20297
20298 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
ad3fea08 20299 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
316f5878 20300 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
20301 return (&mips_cpu_info_table[i]);
20302
e972090a 20303 return NULL;
e7af610e 20304}
fef14a42
TS
20305
20306static const struct mips_cpu_info *
17a2f251 20307mips_cpu_info_from_arch (int arch)
fef14a42
TS
20308{
20309 int i;
20310
20311 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
20312 if (arch == mips_cpu_info_table[i].cpu)
20313 return (&mips_cpu_info_table[i]);
20314
20315 return NULL;
20316}
316f5878
RS
20317\f
20318static void
17a2f251 20319show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
20320{
20321 if (*first_p)
20322 {
20323 fprintf (stream, "%24s", "");
20324 *col_p = 24;
20325 }
20326 else
20327 {
20328 fprintf (stream, ", ");
20329 *col_p += 2;
20330 }
e7af610e 20331
316f5878
RS
20332 if (*col_p + strlen (string) > 72)
20333 {
20334 fprintf (stream, "\n%24s", "");
20335 *col_p = 24;
20336 }
20337
20338 fprintf (stream, "%s", string);
20339 *col_p += strlen (string);
20340
20341 *first_p = 0;
20342}
20343
20344void
17a2f251 20345md_show_usage (FILE *stream)
e7af610e 20346{
316f5878
RS
20347 int column, first;
20348 size_t i;
20349
20350 fprintf (stream, _("\
20351MIPS options:\n\
316f5878
RS
20352-EB generate big endian output\n\
20353-EL generate little endian output\n\
20354-g, -g2 do not remove unneeded NOPs or swap branches\n\
20355-G NUM allow referencing objects up to NUM bytes\n\
20356 implicitly with the gp register [default 8]\n"));
20357 fprintf (stream, _("\
20358-mips1 generate MIPS ISA I instructions\n\
20359-mips2 generate MIPS ISA II instructions\n\
20360-mips3 generate MIPS ISA III instructions\n\
20361-mips4 generate MIPS ISA IV instructions\n\
20362-mips5 generate MIPS ISA V instructions\n\
20363-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 20364-mips32r2 generate MIPS32 release 2 ISA instructions\n\
ae52f483
AB
20365-mips32r3 generate MIPS32 release 3 ISA instructions\n\
20366-mips32r5 generate MIPS32 release 5 ISA instructions\n\
7361da2c 20367-mips32r6 generate MIPS32 release 6 ISA instructions\n\
316f5878 20368-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 20369-mips64r2 generate MIPS64 release 2 ISA instructions\n\
ae52f483
AB
20370-mips64r3 generate MIPS64 release 3 ISA instructions\n\
20371-mips64r5 generate MIPS64 release 5 ISA instructions\n\
7361da2c 20372-mips64r6 generate MIPS64 release 6 ISA instructions\n\
316f5878
RS
20373-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
20374
20375 first = 1;
e7af610e
NC
20376
20377 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
20378 show (stream, mips_cpu_info_table[i].name, &column, &first);
20379 show (stream, "from-abi", &column, &first);
20380 fputc ('\n', stream);
e7af610e 20381
316f5878
RS
20382 fprintf (stream, _("\
20383-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
20384-no-mCPU don't generate code specific to CPU.\n\
20385 For -mCPU and -no-mCPU, CPU must be one of:\n"));
20386
20387 first = 1;
20388
20389 show (stream, "3900", &column, &first);
20390 show (stream, "4010", &column, &first);
20391 show (stream, "4100", &column, &first);
20392 show (stream, "4650", &column, &first);
20393 fputc ('\n', stream);
20394
20395 fprintf (stream, _("\
20396-mips16 generate mips16 instructions\n\
20397-no-mips16 do not generate mips16 instructions\n"));
20398 fprintf (stream, _("\
f866b262
MR
20399-mmips16e2 generate MIPS16e2 instructions\n\
20400-mno-mips16e2 do not generate MIPS16e2 instructions\n"));
20401 fprintf (stream, _("\
df58fc94
RS
20402-mmicromips generate microMIPS instructions\n\
20403-mno-micromips do not generate microMIPS instructions\n"));
20404 fprintf (stream, _("\
e16bfa71 20405-msmartmips generate smartmips instructions\n\
3739860c 20406-mno-smartmips do not generate smartmips instructions\n"));
e16bfa71 20407 fprintf (stream, _("\
74cd071d
CF
20408-mdsp generate DSP instructions\n\
20409-mno-dsp do not generate DSP instructions\n"));
20410 fprintf (stream, _("\
8b082fb1
TS
20411-mdspr2 generate DSP R2 instructions\n\
20412-mno-dspr2 do not generate DSP R2 instructions\n"));
20413 fprintf (stream, _("\
8f4f9071
MF
20414-mdspr3 generate DSP R3 instructions\n\
20415-mno-dspr3 do not generate DSP R3 instructions\n"));
20416 fprintf (stream, _("\
ef2e4d86
CF
20417-mmt generate MT instructions\n\
20418-mno-mt do not generate MT instructions\n"));
20419 fprintf (stream, _("\
dec0624d
MR
20420-mmcu generate MCU instructions\n\
20421-mno-mcu do not generate MCU instructions\n"));
20422 fprintf (stream, _("\
56d438b1
CF
20423-mmsa generate MSA instructions\n\
20424-mno-msa do not generate MSA instructions\n"));
20425 fprintf (stream, _("\
7d64c587
AB
20426-mxpa generate eXtended Physical Address (XPA) instructions\n\
20427-mno-xpa do not generate eXtended Physical Address (XPA) instructions\n"));
20428 fprintf (stream, _("\
b015e599
AP
20429-mvirt generate Virtualization instructions\n\
20430-mno-virt do not generate Virtualization instructions\n"));
20431 fprintf (stream, _("\
730c3174
SE
20432-mcrc generate CRC instructions\n\
20433-mno-crc do not generate CRC instructions\n"));
20434 fprintf (stream, _("\
6f20c942
FS
20435-mginv generate Global INValidate (GINV) instructions\n\
20436-mno-ginv do not generate Global INValidate instructions\n"));
20437 fprintf (stream, _("\
8095d2f7
CX
20438-mloongson-mmi generate Loongson MultiMedia extensions Instructions (MMI) instructions\n\
20439-mno-loongson-mmi do not generate Loongson MultiMedia extensions Instructions\n"));
20440 fprintf (stream, _("\
716c08de
CX
20441-mloongson-cam generate Loongson Content Address Memory (CAM) instructions\n\
20442-mno-loongson-cam do not generate Loongson Content Address Memory Instructions\n"));
20443 fprintf (stream, _("\
bdc6c06e
CX
20444-mloongson-ext generate Loongson EXTensions (EXT) instructions\n\
20445-mno-loongson-ext do not generate Loongson EXTensions Instructions\n"));
20446 fprintf (stream, _("\
a693765e
CX
20447-mloongson-ext2 generate Loongson EXTensions R2 (EXT2) instructions\n\
20448-mno-loongson-ext2 do not generate Loongson EXTensions R2 Instructions\n"));
20449 fprintf (stream, _("\
833794fc
MR
20450-minsn32 only generate 32-bit microMIPS instructions\n\
20451-mno-insn32 generate all microMIPS instructions\n"));
6f2117ba
PH
20452#if DEFAULT_MIPS_FIX_LOONGSON3_LLSC
20453 fprintf (stream, _("\
20454-mfix-loongson3-llsc work around Loongson3 LL/SC errata, default\n\
20455-mno-fix-loongson3-llsc disable work around Loongson3 LL/SC errata\n"));
20456#else
20457 fprintf (stream, _("\
20458-mfix-loongson3-llsc work around Loongson3 LL/SC errata\n\
20459-mno-fix-loongson3-llsc disable work around Loongson3 LL/SC errata, default\n"));
20460#endif
833794fc 20461 fprintf (stream, _("\
c67a084a
NC
20462-mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
20463-mfix-loongson2f-nop work around Loongson2F NOP errata\n\
6f2117ba
PH
20464-mfix-loongson3-llsc work around Loongson3 LL/SC errata\n\
20465-mno-fix-loongson3-llsc disable work around Loongson3 LL/SC errata\n\
d766e8ec 20466-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 20467-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
6a32d874 20468-mfix-24k insert a nop after ERET and DERET instructions\n\
d954098f 20469-mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
27c634e0 20470-mfix-r5900 work around R5900 short loop errata\n\
316f5878
RS
20471-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
20472-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261 20473-msym32 assume all symbols have 32-bit values\n\
092a534f
MR
20474-O0 do not remove unneeded NOPs, do not swap branches\n\
20475-O, -O1 remove unneeded NOPs, do not swap branches\n\
20476-O2 remove unneeded NOPs and swap branches\n\
316f5878
RS
20477--trap, --no-break trap exception on div by 0 and mult overflow\n\
20478--break, --no-trap break exception on div by 0 and mult overflow\n"));
037b32b9
AN
20479 fprintf (stream, _("\
20480-mhard-float allow floating-point instructions\n\
20481-msoft-float do not allow floating-point instructions\n\
20482-msingle-float only allow 32-bit floating-point operations\n\
20483-mdouble-float allow 32-bit and 64-bit floating-point operations\n\
3bf0dbfb 20484--[no-]construct-floats [dis]allow floating point values to be constructed\n\
ba92f887 20485--[no-]relax-branch [dis]allow out-of-range branches to be relaxed\n\
8b10b0b3
MR
20486-mignore-branch-isa accept invalid branches requiring an ISA mode switch\n\
20487-mno-ignore-branch-isa reject invalid branches requiring an ISA mode switch\n\
ba92f887
MR
20488-mnan=ENCODING select an IEEE 754 NaN encoding convention, either of:\n"));
20489
20490 first = 1;
20491
20492 show (stream, "legacy", &column, &first);
20493 show (stream, "2008", &column, &first);
20494
20495 fputc ('\n', stream);
20496
316f5878
RS
20497 fprintf (stream, _("\
20498-KPIC, -call_shared generate SVR4 position independent code\n\
861fb55a 20499-call_nonpic generate non-PIC code that can operate with DSOs\n\
0c000745 20500-mvxworks-pic generate VxWorks position independent code\n\
861fb55a 20501-non_shared do not generate code that can operate with DSOs\n\
316f5878 20502-xgot assume a 32 bit GOT\n\
dcd410fe 20503-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8 20504-mshared, -mno-shared disable/enable .cpload optimization for\n\
d821e36b 20505 position dependent (non shared) code\n\
316f5878
RS
20506-mabi=ABI create ABI conformant object file for:\n"));
20507
20508 first = 1;
20509
20510 show (stream, "32", &column, &first);
20511 show (stream, "o64", &column, &first);
20512 show (stream, "n32", &column, &first);
20513 show (stream, "64", &column, &first);
20514 show (stream, "eabi", &column, &first);
20515
20516 fputc ('\n', stream);
20517
20518 fprintf (stream, _("\
b4f6242e
MR
20519-32 create o32 ABI object file%s\n"),
20520 MIPS_DEFAULT_ABI == O32_ABI ? _(" (default)") : "");
20521 fprintf (stream, _("\
20522-n32 create n32 ABI object file%s\n"),
20523 MIPS_DEFAULT_ABI == N32_ABI ? _(" (default)") : "");
20524 fprintf (stream, _("\
20525-64 create 64 ABI object file%s\n"),
20526 MIPS_DEFAULT_ABI == N64_ABI ? _(" (default)") : "");
e7af610e 20527}
14e777e0 20528
1575952e 20529#ifdef TE_IRIX
14e777e0 20530enum dwarf2_format
413a266c 20531mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
14e777e0 20532{
369943fe 20533 if (HAVE_64BIT_SYMBOLS)
1575952e 20534 return dwarf2_format_64bit_irix;
14e777e0
KB
20535 else
20536 return dwarf2_format_32bit;
20537}
1575952e 20538#endif
73369e65
EC
20539
20540int
20541mips_dwarf2_addr_size (void)
20542{
6b6b3450 20543 if (HAVE_64BIT_OBJECTS)
73369e65 20544 return 8;
73369e65
EC
20545 else
20546 return 4;
20547}
5862107c
EC
20548
20549/* Standard calling conventions leave the CFA at SP on entry. */
20550void
20551mips_cfi_frame_initial_instructions (void)
20552{
20553 cfi_add_CFA_def_cfa_register (SP);
20554}
20555
707bfff6
TS
20556int
20557tc_mips_regname_to_dw2regnum (char *regname)
20558{
20559 unsigned int regnum = -1;
20560 unsigned int reg;
20561
20562 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
20563 regnum = reg;
20564
20565 return regnum;
20566}
263b2574 20567
20568/* Implement CONVERT_SYMBOLIC_ATTRIBUTE.
20569 Given a symbolic attribute NAME, return the proper integer value.
20570 Returns -1 if the attribute is not known. */
20571
20572int
20573mips_convert_symbolic_attribute (const char *name)
20574{
20575 static const struct
20576 {
20577 const char * name;
20578 const int tag;
20579 }
20580 attribute_table[] =
20581 {
20582#define T(tag) {#tag, tag}
20583 T (Tag_GNU_MIPS_ABI_FP),
20584 T (Tag_GNU_MIPS_ABI_MSA),
20585#undef T
20586 };
20587 unsigned int i;
20588
20589 if (name == NULL)
20590 return -1;
20591
20592 for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
20593 if (streq (name, attribute_table[i].name))
20594 return attribute_table[i].tag;
20595
20596 return -1;
20597}
fd5c94ab
RS
20598
20599void
20600md_mips_end (void)
20601{
351cdf24
MF
20602 int fpabi = Val_GNU_MIPS_ABI_FP_ANY;
20603
fd5c94ab
RS
20604 mips_emit_delays ();
20605 if (cur_proc_ptr)
20606 as_warn (_("missing .end at end of assembly"));
919731af 20607
20608 /* Just in case no code was emitted, do the consistency check. */
20609 file_mips_check_options ();
351cdf24
MF
20610
20611 /* Set a floating-point ABI if the user did not. */
20612 if (obj_elf_seen_attribute (OBJ_ATTR_GNU, Tag_GNU_MIPS_ABI_FP))
20613 {
20614 /* Perform consistency checks on the floating-point ABI. */
20615 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20616 Tag_GNU_MIPS_ABI_FP);
20617 if (fpabi != Val_GNU_MIPS_ABI_FP_ANY)
20618 check_fpabi (fpabi);
20619 }
20620 else
20621 {
20622 /* Soft-float gets precedence over single-float, the two options should
20623 not be used together so this should not matter. */
20624 if (file_mips_opts.soft_float == 1)
20625 fpabi = Val_GNU_MIPS_ABI_FP_SOFT;
20626 /* Single-float gets precedence over all double_float cases. */
20627 else if (file_mips_opts.single_float == 1)
20628 fpabi = Val_GNU_MIPS_ABI_FP_SINGLE;
20629 else
20630 {
20631 switch (file_mips_opts.fp)
20632 {
20633 case 32:
20634 if (file_mips_opts.gp == 32)
20635 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20636 break;
20637 case 0:
20638 fpabi = Val_GNU_MIPS_ABI_FP_XX;
20639 break;
20640 case 64:
20641 if (file_mips_opts.gp == 32 && !file_mips_opts.oddspreg)
20642 fpabi = Val_GNU_MIPS_ABI_FP_64A;
20643 else if (file_mips_opts.gp == 32)
20644 fpabi = Val_GNU_MIPS_ABI_FP_64;
20645 else
20646 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20647 break;
20648 }
20649 }
20650
20651 bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20652 Tag_GNU_MIPS_ABI_FP, fpabi);
20653 }
fd5c94ab 20654}
2f0c68f2
CM
20655
20656/* Returns the relocation type required for a particular CFI encoding. */
20657
20658bfd_reloc_code_real_type
20659mips_cfi_reloc_for_encoding (int encoding)
20660{
20661 if (encoding == (DW_EH_PE_sdata4 | DW_EH_PE_pcrel))
20662 return BFD_RELOC_32_PCREL;
20663 else return BFD_RELOC_NONE;
20664}
This page took 3.396861 seconds and 4 git commands to generate.