Rearrange symbol_create parameters
[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. */
629310ab 678static htab_t op_hash = NULL;
252b5132
RH
679
680/* The opcode hash table we use for the mips16. */
629310ab 681static htab_t mips16_op_hash = NULL;
252b5132 682
df58fc94 683/* The opcode hash table we use for the microMIPS ASE. */
629310ab 684static htab_t micromips_op_hash = NULL;
df58fc94 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
252b5132
RH
892/* Map mips16 register numbers to normal MIPS register numbers. */
893
e972090a
NC
894static const unsigned int mips16_to_32_reg_map[] =
895{
252b5132
RH
896 16, 17, 2, 3, 4, 5, 6, 7
897};
60b63b72 898
df58fc94
RS
899/* Map microMIPS register numbers to normal MIPS register numbers. */
900
df58fc94 901#define micromips_to_32_reg_d_map mips16_to_32_reg_map
df58fc94
RS
902
903/* The microMIPS registers with type h. */
e76ff5ab 904static const unsigned int micromips_to_32_reg_h_map1[] =
df58fc94
RS
905{
906 5, 5, 6, 4, 4, 4, 4, 4
907};
e76ff5ab 908static const unsigned int micromips_to_32_reg_h_map2[] =
df58fc94
RS
909{
910 6, 7, 7, 21, 22, 5, 6, 7
911};
912
df58fc94
RS
913/* The microMIPS registers with type m. */
914static const unsigned int micromips_to_32_reg_m_map[] =
915{
916 0, 17, 2, 3, 16, 18, 19, 20
917};
918
919#define micromips_to_32_reg_n_map micromips_to_32_reg_m_map
920
71400594
RS
921/* Classifies the kind of instructions we're interested in when
922 implementing -mfix-vr4120. */
c67a084a
NC
923enum fix_vr4120_class
924{
71400594
RS
925 FIX_VR4120_MACC,
926 FIX_VR4120_DMACC,
927 FIX_VR4120_MULT,
928 FIX_VR4120_DMULT,
929 FIX_VR4120_DIV,
930 FIX_VR4120_MTHILO,
931 NUM_FIX_VR4120_CLASSES
932};
933
c67a084a
NC
934/* ...likewise -mfix-loongson2f-jump. */
935static bfd_boolean mips_fix_loongson2f_jump;
936
937/* ...likewise -mfix-loongson2f-nop. */
938static bfd_boolean mips_fix_loongson2f_nop;
939
940/* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed. */
941static bfd_boolean mips_fix_loongson2f;
942
71400594
RS
943/* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
944 there must be at least one other instruction between an instruction
945 of type X and an instruction of type Y. */
946static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
947
948/* True if -mfix-vr4120 is in force. */
d766e8ec 949static int mips_fix_vr4120;
4a6a3df4 950
7d8e00cf
RS
951/* ...likewise -mfix-vr4130. */
952static int mips_fix_vr4130;
953
6a32d874
CM
954/* ...likewise -mfix-24k. */
955static int mips_fix_24k;
956
a8d14a88
CM
957/* ...likewise -mfix-rm7000 */
958static int mips_fix_rm7000;
959
d954098f
DD
960/* ...likewise -mfix-cn63xxp1 */
961static bfd_boolean mips_fix_cn63xxp1;
962
27c634e0
FN
963/* ...likewise -mfix-r5900 */
964static bfd_boolean mips_fix_r5900;
965static bfd_boolean mips_fix_r5900_explicit;
966
6f2117ba
PH
967/* ...likewise -mfix-loongson3-llsc. */
968static bfd_boolean mips_fix_loongson3_llsc = DEFAULT_MIPS_FIX_LOONGSON3_LLSC;
969
4a6a3df4
AO
970/* We don't relax branches by default, since this causes us to expand
971 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
972 fail to compute the offset before expanding the macro to the most
973 efficient expansion. */
974
975static int mips_relax_branch;
8b10b0b3
MR
976
977/* TRUE if checks are suppressed for invalid branches between ISA modes.
978 Needed for broken assembly produced by some GCC versions and some
979 sloppy code out there, where branches to data labels are present. */
980static bfd_boolean mips_ignore_branch_isa;
252b5132 981\f
4d7206a2
RS
982/* The expansion of many macros depends on the type of symbol that
983 they refer to. For example, when generating position-dependent code,
984 a macro that refers to a symbol may have two different expansions,
985 one which uses GP-relative addresses and one which uses absolute
986 addresses. When generating SVR4-style PIC, a macro may have
987 different expansions for local and global symbols.
988
989 We handle these situations by generating both sequences and putting
990 them in variant frags. In position-dependent code, the first sequence
991 will be the GP-relative one and the second sequence will be the
992 absolute one. In SVR4 PIC, the first sequence will be for global
993 symbols and the second will be for local symbols.
994
584892a6
RS
995 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
996 SECOND are the lengths of the two sequences in bytes. These fields
997 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
998 the subtype has the following flags:
4d7206a2 999
ce8ad872
MR
1000 RELAX_PIC
1001 Set if generating PIC code.
1002
584892a6
RS
1003 RELAX_USE_SECOND
1004 Set if it has been decided that we should use the second
1005 sequence instead of the first.
1006
1007 RELAX_SECOND_LONGER
1008 Set in the first variant frag if the macro's second implementation
1009 is longer than its first. This refers to the macro as a whole,
1010 not an individual relaxation.
1011
1012 RELAX_NOMACRO
1013 Set in the first variant frag if the macro appeared in a .set nomacro
1014 block and if one alternative requires a warning but the other does not.
1015
1016 RELAX_DELAY_SLOT
1017 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
1018 delay slot.
4d7206a2 1019
df58fc94
RS
1020 RELAX_DELAY_SLOT_16BIT
1021 Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
1022 16-bit instruction.
1023
1024 RELAX_DELAY_SLOT_SIZE_FIRST
1025 Like RELAX_DELAY_SLOT, but indicates that the first implementation of
1026 the macro is of the wrong size for the branch delay slot.
1027
1028 RELAX_DELAY_SLOT_SIZE_SECOND
1029 Like RELAX_DELAY_SLOT, but indicates that the second implementation of
1030 the macro is of the wrong size for the branch delay slot.
1031
4d7206a2
RS
1032 The frag's "opcode" points to the first fixup for relaxable code.
1033
1034 Relaxable macros are generated using a sequence such as:
1035
1036 relax_start (SYMBOL);
1037 ... generate first expansion ...
1038 relax_switch ();
1039 ... generate second expansion ...
1040 relax_end ();
1041
1042 The code and fixups for the unwanted alternative are discarded
1043 by md_convert_frag. */
ce8ad872
MR
1044#define RELAX_ENCODE(FIRST, SECOND, PIC) \
1045 (((FIRST) << 8) | (SECOND) | ((PIC) ? 0x10000 : 0))
4d7206a2 1046
584892a6
RS
1047#define RELAX_FIRST(X) (((X) >> 8) & 0xff)
1048#define RELAX_SECOND(X) ((X) & 0xff)
ce8ad872
MR
1049#define RELAX_PIC(X) (((X) & 0x10000) != 0)
1050#define RELAX_USE_SECOND 0x20000
1051#define RELAX_SECOND_LONGER 0x40000
1052#define RELAX_NOMACRO 0x80000
1053#define RELAX_DELAY_SLOT 0x100000
1054#define RELAX_DELAY_SLOT_16BIT 0x200000
1055#define RELAX_DELAY_SLOT_SIZE_FIRST 0x400000
1056#define RELAX_DELAY_SLOT_SIZE_SECOND 0x800000
252b5132 1057
4a6a3df4
AO
1058/* Branch without likely bit. If label is out of range, we turn:
1059
134c0c8b 1060 beq reg1, reg2, label
4a6a3df4
AO
1061 delay slot
1062
1063 into
1064
1065 bne reg1, reg2, 0f
1066 nop
1067 j label
1068 0: delay slot
1069
1070 with the following opcode replacements:
1071
1072 beq <-> bne
1073 blez <-> bgtz
1074 bltz <-> bgez
1075 bc1f <-> bc1t
1076
1077 bltzal <-> bgezal (with jal label instead of j label)
1078
1079 Even though keeping the delay slot instruction in the delay slot of
1080 the branch would be more efficient, it would be very tricky to do
1081 correctly, because we'd have to introduce a variable frag *after*
1082 the delay slot instruction, and expand that instead. Let's do it
1083 the easy way for now, even if the branch-not-taken case now costs
1084 one additional instruction. Out-of-range branches are not supposed
1085 to be common, anyway.
1086
1087 Branch likely. If label is out of range, we turn:
1088
1089 beql reg1, reg2, label
1090 delay slot (annulled if branch not taken)
1091
1092 into
1093
1094 beql reg1, reg2, 1f
1095 nop
1096 beql $0, $0, 2f
1097 nop
1098 1: j[al] label
1099 delay slot (executed only if branch taken)
1100 2:
1101
1102 It would be possible to generate a shorter sequence by losing the
1103 likely bit, generating something like:
b34976b6 1104
4a6a3df4
AO
1105 bne reg1, reg2, 0f
1106 nop
1107 j[al] label
1108 delay slot (executed only if branch taken)
1109 0:
1110
1111 beql -> bne
1112 bnel -> beq
1113 blezl -> bgtz
1114 bgtzl -> blez
1115 bltzl -> bgez
1116 bgezl -> bltz
1117 bc1fl -> bc1t
1118 bc1tl -> bc1f
1119
1120 bltzall -> bgezal (with jal label instead of j label)
1121 bgezall -> bltzal (ditto)
1122
1123
1124 but it's not clear that it would actually improve performance. */
ce8ad872
MR
1125#define RELAX_BRANCH_ENCODE(at, pic, \
1126 uncond, likely, link, toofar) \
66b3e8da
MR
1127 ((relax_substateT) \
1128 (0xc0000000 \
1129 | ((at) & 0x1f) \
ce8ad872
MR
1130 | ((pic) ? 0x20 : 0) \
1131 | ((toofar) ? 0x40 : 0) \
1132 | ((link) ? 0x80 : 0) \
1133 | ((likely) ? 0x100 : 0) \
1134 | ((uncond) ? 0x200 : 0)))
4a6a3df4 1135#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
ce8ad872
MR
1136#define RELAX_BRANCH_UNCOND(i) (((i) & 0x200) != 0)
1137#define RELAX_BRANCH_LIKELY(i) (((i) & 0x100) != 0)
1138#define RELAX_BRANCH_LINK(i) (((i) & 0x80) != 0)
1139#define RELAX_BRANCH_TOOFAR(i) (((i) & 0x40) != 0)
1140#define RELAX_BRANCH_PIC(i) (((i) & 0x20) != 0)
66b3e8da 1141#define RELAX_BRANCH_AT(i) ((i) & 0x1f)
4a6a3df4 1142
252b5132
RH
1143/* For mips16 code, we use an entirely different form of relaxation.
1144 mips16 supports two versions of most instructions which take
1145 immediate values: a small one which takes some small value, and a
1146 larger one which takes a 16 bit value. Since branches also follow
1147 this pattern, relaxing these values is required.
1148
1149 We can assemble both mips16 and normal MIPS code in a single
1150 object. Therefore, we need to support this type of relaxation at
1151 the same time that we support the relaxation described above. We
1152 use the high bit of the subtype field to distinguish these cases.
1153
1154 The information we store for this type of relaxation is the
1155 argument code found in the opcode file for this relocation, whether
1156 the user explicitly requested a small or extended form, and whether
1157 the relocation is in a jump or jal delay slot. That tells us the
1158 size of the value, and how it should be stored. We also store
1159 whether the fragment is considered to be extended or not. We also
1160 store whether this is known to be a branch to a different section,
1161 whether we have tried to relax this frag yet, and whether we have
1162 ever extended a PC relative fragment because of a shift count. */
25499ac7 1163#define RELAX_MIPS16_ENCODE(type, e2, pic, sym32, nomacro, \
8507b6e7
MR
1164 small, ext, \
1165 dslot, jal_dslot) \
252b5132
RH
1166 (0x80000000 \
1167 | ((type) & 0xff) \
25499ac7
MR
1168 | ((e2) ? 0x100 : 0) \
1169 | ((pic) ? 0x200 : 0) \
1170 | ((sym32) ? 0x400 : 0) \
1171 | ((nomacro) ? 0x800 : 0) \
1172 | ((small) ? 0x1000 : 0) \
1173 | ((ext) ? 0x2000 : 0) \
1174 | ((dslot) ? 0x4000 : 0) \
1175 | ((jal_dslot) ? 0x8000 : 0))
8507b6e7 1176
4a6a3df4 1177#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132 1178#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
25499ac7
MR
1179#define RELAX_MIPS16_E2(i) (((i) & 0x100) != 0)
1180#define RELAX_MIPS16_PIC(i) (((i) & 0x200) != 0)
1181#define RELAX_MIPS16_SYM32(i) (((i) & 0x400) != 0)
1182#define RELAX_MIPS16_NOMACRO(i) (((i) & 0x800) != 0)
1183#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x1000) != 0)
1184#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x2000) != 0)
1185#define RELAX_MIPS16_DSLOT(i) (((i) & 0x4000) != 0)
1186#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x8000) != 0)
1187
1188#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x10000) != 0)
1189#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x10000)
1190#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) & ~0x10000)
1191#define RELAX_MIPS16_ALWAYS_EXTENDED(i) (((i) & 0x20000) != 0)
1192#define RELAX_MIPS16_MARK_ALWAYS_EXTENDED(i) ((i) | 0x20000)
1193#define RELAX_MIPS16_CLEAR_ALWAYS_EXTENDED(i) ((i) & ~0x20000)
1194#define RELAX_MIPS16_MACRO(i) (((i) & 0x40000) != 0)
1195#define RELAX_MIPS16_MARK_MACRO(i) ((i) | 0x40000)
1196#define RELAX_MIPS16_CLEAR_MACRO(i) ((i) & ~0x40000)
885add95 1197
df58fc94
RS
1198/* For microMIPS code, we use relaxation similar to one we use for
1199 MIPS16 code. Some instructions that take immediate values support
1200 two encodings: a small one which takes some small value, and a
1201 larger one which takes a 16 bit value. As some branches also follow
1202 this pattern, relaxing these values is required.
1203
1204 We can assemble both microMIPS and normal MIPS code in a single
1205 object. Therefore, we need to support this type of relaxation at
1206 the same time that we support the relaxation described above. We
1207 use one of the high bits of the subtype field to distinguish these
1208 cases.
1209
1210 The information we store for this type of relaxation is the argument
1211 code found in the opcode file for this relocation, the register
8484fb75
MR
1212 selected as the assembler temporary, whether in the 32-bit
1213 instruction mode, whether the branch is unconditional, whether it is
7bd374a4
MR
1214 compact, whether there is no delay-slot instruction available to fill
1215 in, whether it stores the link address implicitly in $ra, whether
1216 relaxation of out-of-range 32-bit branches to a sequence of
8484fb75
MR
1217 instructions is enabled, and whether the displacement of a branch is
1218 too large to fit as an immediate argument of a 16-bit and a 32-bit
1219 branch, respectively. */
ce8ad872 1220#define RELAX_MICROMIPS_ENCODE(type, at, insn32, pic, \
7bd374a4 1221 uncond, compact, link, nods, \
40209cad
MR
1222 relax32, toofar16, toofar32) \
1223 (0x40000000 \
1224 | ((type) & 0xff) \
1225 | (((at) & 0x1f) << 8) \
8484fb75 1226 | ((insn32) ? 0x2000 : 0) \
ce8ad872
MR
1227 | ((pic) ? 0x4000 : 0) \
1228 | ((uncond) ? 0x8000 : 0) \
1229 | ((compact) ? 0x10000 : 0) \
1230 | ((link) ? 0x20000 : 0) \
1231 | ((nods) ? 0x40000 : 0) \
1232 | ((relax32) ? 0x80000 : 0) \
1233 | ((toofar16) ? 0x100000 : 0) \
1234 | ((toofar32) ? 0x200000 : 0))
df58fc94
RS
1235#define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1236#define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1237#define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
8484fb75 1238#define RELAX_MICROMIPS_INSN32(i) (((i) & 0x2000) != 0)
ce8ad872
MR
1239#define RELAX_MICROMIPS_PIC(i) (((i) & 0x4000) != 0)
1240#define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x8000) != 0)
1241#define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x10000) != 0)
1242#define RELAX_MICROMIPS_LINK(i) (((i) & 0x20000) != 0)
1243#define RELAX_MICROMIPS_NODS(i) (((i) & 0x40000) != 0)
1244#define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x80000) != 0)
1245
1246#define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x100000) != 0)
1247#define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x100000)
1248#define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x100000)
1249#define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x200000) != 0)
1250#define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x200000)
1251#define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x200000)
df58fc94 1252
43c0598f
RS
1253/* Sign-extend 16-bit value X. */
1254#define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1255
885add95
CD
1256/* Is the given value a sign-extended 32-bit value? */
1257#define IS_SEXT_32BIT_NUM(x) \
1258 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1259 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1260
1261/* Is the given value a sign-extended 16-bit value? */
1262#define IS_SEXT_16BIT_NUM(x) \
1263 (((x) &~ (offsetT) 0x7fff) == 0 \
1264 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1265
df58fc94
RS
1266/* Is the given value a sign-extended 12-bit value? */
1267#define IS_SEXT_12BIT_NUM(x) \
1268 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1269
7f3c4072
CM
1270/* Is the given value a sign-extended 9-bit value? */
1271#define IS_SEXT_9BIT_NUM(x) \
1272 (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1273
2051e8c4
MR
1274/* Is the given value a zero-extended 32-bit value? Or a negated one? */
1275#define IS_ZEXT_32BIT_NUM(x) \
1276 (((x) &~ (offsetT) 0xffffffff) == 0 \
1277 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1278
bf12938e
RS
1279/* Extract bits MASK << SHIFT from STRUCT and shift them right
1280 SHIFT places. */
1281#define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1282 (((STRUCT) >> (SHIFT)) & (MASK))
1283
bf12938e 1284/* Extract the operand given by FIELD from mips_cl_insn INSN. */
df58fc94
RS
1285#define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1286 (!(MICROMIPS) \
1287 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1288 : EXTRACT_BITS ((INSN).insn_opcode, \
1289 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
bf12938e
RS
1290#define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1291 EXTRACT_BITS ((INSN).insn_opcode, \
1292 MIPS16OP_MASK_##FIELD, \
1293 MIPS16OP_SH_##FIELD)
5c04167a
RS
1294
1295/* The MIPS16 EXTEND opcode, shifted left 16 places. */
1296#define MIPS16_EXTEND (0xf000U << 16)
4d7206a2 1297\f
df58fc94
RS
1298/* Whether or not we are emitting a branch-likely macro. */
1299static bfd_boolean emit_branch_likely_macro = FALSE;
1300
4d7206a2
RS
1301/* Global variables used when generating relaxable macros. See the
1302 comment above RELAX_ENCODE for more details about how relaxation
1303 is used. */
1304static struct {
1305 /* 0 if we're not emitting a relaxable macro.
1306 1 if we're emitting the first of the two relaxation alternatives.
1307 2 if we're emitting the second alternative. */
1308 int sequence;
1309
1310 /* The first relaxable fixup in the current frag. (In other words,
1311 the first fixup that refers to relaxable code.) */
1312 fixS *first_fixup;
1313
1314 /* sizes[0] says how many bytes of the first alternative are stored in
1315 the current frag. Likewise sizes[1] for the second alternative. */
1316 unsigned int sizes[2];
1317
1318 /* The symbol on which the choice of sequence depends. */
1319 symbolS *symbol;
1320} mips_relax;
252b5132 1321\f
584892a6
RS
1322/* Global variables used to decide whether a macro needs a warning. */
1323static struct {
1324 /* True if the macro is in a branch delay slot. */
1325 bfd_boolean delay_slot_p;
1326
df58fc94
RS
1327 /* Set to the length in bytes required if the macro is in a delay slot
1328 that requires a specific length of instruction, otherwise zero. */
1329 unsigned int delay_slot_length;
1330
584892a6
RS
1331 /* For relaxable macros, sizes[0] is the length of the first alternative
1332 in bytes and sizes[1] is the length of the second alternative.
1333 For non-relaxable macros, both elements give the length of the
1334 macro in bytes. */
1335 unsigned int sizes[2];
1336
df58fc94
RS
1337 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1338 instruction of the first alternative in bytes and first_insn_sizes[1]
1339 is the length of the first instruction of the second alternative.
1340 For non-relaxable macros, both elements give the length of the first
1341 instruction in bytes.
1342
1343 Set to zero if we haven't yet seen the first instruction. */
1344 unsigned int first_insn_sizes[2];
1345
1346 /* For relaxable macros, insns[0] is the number of instructions for the
1347 first alternative and insns[1] is the number of instructions for the
1348 second alternative.
1349
1350 For non-relaxable macros, both elements give the number of
1351 instructions for the macro. */
1352 unsigned int insns[2];
1353
584892a6
RS
1354 /* The first variant frag for this macro. */
1355 fragS *first_frag;
1356} mips_macro_warning;
1357\f
252b5132
RH
1358/* Prototypes for static functions. */
1359
252b5132
RH
1360enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1361
b34976b6 1362static void append_insn
df58fc94
RS
1363 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1364 bfd_boolean expansionp);
7d10b47d 1365static void mips_no_prev_insn (void);
c67a084a 1366static void macro_build (expressionS *, const char *, const char *, ...);
b34976b6 1367static void mips16_macro_build
03ea81db 1368 (expressionS *, const char *, const char *, va_list *);
67c0d1eb 1369static void load_register (int, expressionS *, int);
584892a6
RS
1370static void macro_start (void);
1371static void macro_end (void);
833794fc 1372static void macro (struct mips_cl_insn *ip, char *str);
17a2f251 1373static void mips16_macro (struct mips_cl_insn * ip);
17a2f251
TS
1374static void mips_ip (char *str, struct mips_cl_insn * ip);
1375static void mips16_ip (char *str, struct mips_cl_insn * ip);
25499ac7 1376static unsigned long mips16_immed_extend (offsetT, unsigned int);
b34976b6 1377static void mips16_immed
3b4dbbbf 1378 (const char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
43c0598f 1379 unsigned int, unsigned long *);
5e0116d5 1380static size_t my_getSmallExpression
17a2f251
TS
1381 (expressionS *, bfd_reloc_code_real_type *, char *);
1382static void my_getExpression (expressionS *, char *);
1383static void s_align (int);
1384static void s_change_sec (int);
1385static void s_change_section (int);
1386static void s_cons (int);
1387static void s_float_cons (int);
1388static void s_mips_globl (int);
1389static void s_option (int);
1390static void s_mipsset (int);
1391static void s_abicalls (int);
1392static void s_cpload (int);
1393static void s_cpsetup (int);
1394static void s_cplocal (int);
1395static void s_cprestore (int);
1396static void s_cpreturn (int);
741d6ea8
JM
1397static void s_dtprelword (int);
1398static void s_dtpreldword (int);
d0f13682
CLT
1399static void s_tprelword (int);
1400static void s_tpreldword (int);
17a2f251
TS
1401static void s_gpvalue (int);
1402static void s_gpword (int);
1403static void s_gpdword (int);
a3f278e2 1404static void s_ehword (int);
17a2f251
TS
1405static void s_cpadd (int);
1406static void s_insn (int);
ba92f887 1407static void s_nan (int);
919731af 1408static void s_module (int);
17a2f251
TS
1409static void s_mips_ent (int);
1410static void s_mips_end (int);
1411static void s_mips_frame (int);
1412static void s_mips_mask (int reg_type);
1413static void s_mips_stab (int);
1414static void s_mips_weakext (int);
1415static void s_mips_file (int);
1416static void s_mips_loc (int);
9e009953 1417static bfd_boolean pic_need_relax (symbolS *);
4a6a3df4 1418static int relaxed_branch_length (fragS *, asection *, int);
df58fc94
RS
1419static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1420static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
919731af 1421static void file_mips_check_options (void);
e7af610e
NC
1422
1423/* Table and functions used to map between CPU/ISA names, and
1424 ISA levels, and CPU numbers. */
1425
e972090a
NC
1426struct mips_cpu_info
1427{
e7af610e 1428 const char *name; /* CPU or ISA name. */
d16afab6
RS
1429 int flags; /* MIPS_CPU_* flags. */
1430 int ase; /* Set of ASEs implemented by the CPU. */
e7af610e
NC
1431 int isa; /* ISA level. */
1432 int cpu; /* CPU number (default CPU if ISA). */
1433};
1434
ad3fea08 1435#define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
ad3fea08 1436
17a2f251
TS
1437static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1438static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1439static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132 1440\f
c31f3936
RS
1441/* Command-line options. */
1442const char *md_shortopts = "O::g::G:";
1443
1444enum options
1445 {
1446 OPTION_MARCH = OPTION_MD_BASE,
1447 OPTION_MTUNE,
1448 OPTION_MIPS1,
1449 OPTION_MIPS2,
1450 OPTION_MIPS3,
1451 OPTION_MIPS4,
1452 OPTION_MIPS5,
1453 OPTION_MIPS32,
1454 OPTION_MIPS64,
1455 OPTION_MIPS32R2,
ae52f483
AB
1456 OPTION_MIPS32R3,
1457 OPTION_MIPS32R5,
7361da2c 1458 OPTION_MIPS32R6,
c31f3936 1459 OPTION_MIPS64R2,
ae52f483
AB
1460 OPTION_MIPS64R3,
1461 OPTION_MIPS64R5,
7361da2c 1462 OPTION_MIPS64R6,
c31f3936
RS
1463 OPTION_MIPS16,
1464 OPTION_NO_MIPS16,
1465 OPTION_MIPS3D,
1466 OPTION_NO_MIPS3D,
1467 OPTION_MDMX,
1468 OPTION_NO_MDMX,
1469 OPTION_DSP,
1470 OPTION_NO_DSP,
1471 OPTION_MT,
1472 OPTION_NO_MT,
1473 OPTION_VIRT,
1474 OPTION_NO_VIRT,
56d438b1
CF
1475 OPTION_MSA,
1476 OPTION_NO_MSA,
c31f3936
RS
1477 OPTION_SMARTMIPS,
1478 OPTION_NO_SMARTMIPS,
1479 OPTION_DSPR2,
1480 OPTION_NO_DSPR2,
8f4f9071
MF
1481 OPTION_DSPR3,
1482 OPTION_NO_DSPR3,
c31f3936
RS
1483 OPTION_EVA,
1484 OPTION_NO_EVA,
7d64c587
AB
1485 OPTION_XPA,
1486 OPTION_NO_XPA,
c31f3936
RS
1487 OPTION_MICROMIPS,
1488 OPTION_NO_MICROMIPS,
1489 OPTION_MCU,
1490 OPTION_NO_MCU,
25499ac7
MR
1491 OPTION_MIPS16E2,
1492 OPTION_NO_MIPS16E2,
730c3174
SE
1493 OPTION_CRC,
1494 OPTION_NO_CRC,
c31f3936
RS
1495 OPTION_M4650,
1496 OPTION_NO_M4650,
1497 OPTION_M4010,
1498 OPTION_NO_M4010,
1499 OPTION_M4100,
1500 OPTION_NO_M4100,
1501 OPTION_M3900,
1502 OPTION_NO_M3900,
1503 OPTION_M7000_HILO_FIX,
1504 OPTION_MNO_7000_HILO_FIX,
1505 OPTION_FIX_24K,
1506 OPTION_NO_FIX_24K,
a8d14a88
CM
1507 OPTION_FIX_RM7000,
1508 OPTION_NO_FIX_RM7000,
6f2117ba
PH
1509 OPTION_FIX_LOONGSON3_LLSC,
1510 OPTION_NO_FIX_LOONGSON3_LLSC,
c31f3936
RS
1511 OPTION_FIX_LOONGSON2F_JUMP,
1512 OPTION_NO_FIX_LOONGSON2F_JUMP,
1513 OPTION_FIX_LOONGSON2F_NOP,
1514 OPTION_NO_FIX_LOONGSON2F_NOP,
1515 OPTION_FIX_VR4120,
1516 OPTION_NO_FIX_VR4120,
1517 OPTION_FIX_VR4130,
1518 OPTION_NO_FIX_VR4130,
1519 OPTION_FIX_CN63XXP1,
1520 OPTION_NO_FIX_CN63XXP1,
27c634e0
FN
1521 OPTION_FIX_R5900,
1522 OPTION_NO_FIX_R5900,
c31f3936
RS
1523 OPTION_TRAP,
1524 OPTION_BREAK,
1525 OPTION_EB,
1526 OPTION_EL,
1527 OPTION_FP32,
1528 OPTION_GP32,
1529 OPTION_CONSTRUCT_FLOATS,
1530 OPTION_NO_CONSTRUCT_FLOATS,
1531 OPTION_FP64,
351cdf24 1532 OPTION_FPXX,
c31f3936
RS
1533 OPTION_GP64,
1534 OPTION_RELAX_BRANCH,
1535 OPTION_NO_RELAX_BRANCH,
8b10b0b3
MR
1536 OPTION_IGNORE_BRANCH_ISA,
1537 OPTION_NO_IGNORE_BRANCH_ISA,
833794fc
MR
1538 OPTION_INSN32,
1539 OPTION_NO_INSN32,
c31f3936
RS
1540 OPTION_MSHARED,
1541 OPTION_MNO_SHARED,
1542 OPTION_MSYM32,
1543 OPTION_MNO_SYM32,
1544 OPTION_SOFT_FLOAT,
1545 OPTION_HARD_FLOAT,
1546 OPTION_SINGLE_FLOAT,
1547 OPTION_DOUBLE_FLOAT,
1548 OPTION_32,
c31f3936
RS
1549 OPTION_CALL_SHARED,
1550 OPTION_CALL_NONPIC,
1551 OPTION_NON_SHARED,
1552 OPTION_XGOT,
1553 OPTION_MABI,
1554 OPTION_N32,
1555 OPTION_64,
1556 OPTION_MDEBUG,
1557 OPTION_NO_MDEBUG,
1558 OPTION_PDR,
1559 OPTION_NO_PDR,
1560 OPTION_MVXWORKS_PIC,
ba92f887 1561 OPTION_NAN,
351cdf24
MF
1562 OPTION_ODD_SPREG,
1563 OPTION_NO_ODD_SPREG,
6f20c942
FS
1564 OPTION_GINV,
1565 OPTION_NO_GINV,
8095d2f7
CX
1566 OPTION_LOONGSON_MMI,
1567 OPTION_NO_LOONGSON_MMI,
716c08de
CX
1568 OPTION_LOONGSON_CAM,
1569 OPTION_NO_LOONGSON_CAM,
bdc6c06e
CX
1570 OPTION_LOONGSON_EXT,
1571 OPTION_NO_LOONGSON_EXT,
a693765e
CX
1572 OPTION_LOONGSON_EXT2,
1573 OPTION_NO_LOONGSON_EXT2,
c31f3936
RS
1574 OPTION_END_OF_ENUM
1575 };
1576
1577struct option md_longopts[] =
1578{
1579 /* Options which specify architecture. */
1580 {"march", required_argument, NULL, OPTION_MARCH},
1581 {"mtune", required_argument, NULL, OPTION_MTUNE},
1582 {"mips0", no_argument, NULL, OPTION_MIPS1},
1583 {"mips1", no_argument, NULL, OPTION_MIPS1},
1584 {"mips2", no_argument, NULL, OPTION_MIPS2},
1585 {"mips3", no_argument, NULL, OPTION_MIPS3},
1586 {"mips4", no_argument, NULL, OPTION_MIPS4},
1587 {"mips5", no_argument, NULL, OPTION_MIPS5},
1588 {"mips32", no_argument, NULL, OPTION_MIPS32},
1589 {"mips64", no_argument, NULL, OPTION_MIPS64},
1590 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
ae52f483
AB
1591 {"mips32r3", no_argument, NULL, OPTION_MIPS32R3},
1592 {"mips32r5", no_argument, NULL, OPTION_MIPS32R5},
7361da2c 1593 {"mips32r6", no_argument, NULL, OPTION_MIPS32R6},
c31f3936 1594 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
ae52f483
AB
1595 {"mips64r3", no_argument, NULL, OPTION_MIPS64R3},
1596 {"mips64r5", no_argument, NULL, OPTION_MIPS64R5},
7361da2c 1597 {"mips64r6", no_argument, NULL, OPTION_MIPS64R6},
c31f3936
RS
1598
1599 /* Options which specify Application Specific Extensions (ASEs). */
1600 {"mips16", no_argument, NULL, OPTION_MIPS16},
1601 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1602 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1603 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1604 {"mdmx", no_argument, NULL, OPTION_MDMX},
1605 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1606 {"mdsp", no_argument, NULL, OPTION_DSP},
1607 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1608 {"mmt", no_argument, NULL, OPTION_MT},
1609 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1610 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1611 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1612 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1613 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
8f4f9071
MF
1614 {"mdspr3", no_argument, NULL, OPTION_DSPR3},
1615 {"mno-dspr3", no_argument, NULL, OPTION_NO_DSPR3},
c31f3936
RS
1616 {"meva", no_argument, NULL, OPTION_EVA},
1617 {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1618 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1619 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1620 {"mmcu", no_argument, NULL, OPTION_MCU},
1621 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1622 {"mvirt", no_argument, NULL, OPTION_VIRT},
1623 {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
56d438b1
CF
1624 {"mmsa", no_argument, NULL, OPTION_MSA},
1625 {"mno-msa", no_argument, NULL, OPTION_NO_MSA},
7d64c587
AB
1626 {"mxpa", no_argument, NULL, OPTION_XPA},
1627 {"mno-xpa", no_argument, NULL, OPTION_NO_XPA},
25499ac7
MR
1628 {"mmips16e2", no_argument, NULL, OPTION_MIPS16E2},
1629 {"mno-mips16e2", no_argument, NULL, OPTION_NO_MIPS16E2},
730c3174
SE
1630 {"mcrc", no_argument, NULL, OPTION_CRC},
1631 {"mno-crc", no_argument, NULL, OPTION_NO_CRC},
6f20c942
FS
1632 {"mginv", no_argument, NULL, OPTION_GINV},
1633 {"mno-ginv", no_argument, NULL, OPTION_NO_GINV},
8095d2f7
CX
1634 {"mloongson-mmi", no_argument, NULL, OPTION_LOONGSON_MMI},
1635 {"mno-loongson-mmi", no_argument, NULL, OPTION_NO_LOONGSON_MMI},
716c08de
CX
1636 {"mloongson-cam", no_argument, NULL, OPTION_LOONGSON_CAM},
1637 {"mno-loongson-cam", no_argument, NULL, OPTION_NO_LOONGSON_CAM},
bdc6c06e
CX
1638 {"mloongson-ext", no_argument, NULL, OPTION_LOONGSON_EXT},
1639 {"mno-loongson-ext", no_argument, NULL, OPTION_NO_LOONGSON_EXT},
a693765e
CX
1640 {"mloongson-ext2", no_argument, NULL, OPTION_LOONGSON_EXT2},
1641 {"mno-loongson-ext2", no_argument, NULL, OPTION_NO_LOONGSON_EXT2},
c31f3936
RS
1642
1643 /* Old-style architecture options. Don't add more of these. */
1644 {"m4650", no_argument, NULL, OPTION_M4650},
1645 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1646 {"m4010", no_argument, NULL, OPTION_M4010},
1647 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1648 {"m4100", no_argument, NULL, OPTION_M4100},
1649 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1650 {"m3900", no_argument, NULL, OPTION_M3900},
1651 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1652
1653 /* Options which enable bug fixes. */
1654 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1655 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1656 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
6f2117ba
PH
1657 {"mfix-loongson3-llsc", no_argument, NULL, OPTION_FIX_LOONGSON3_LLSC},
1658 {"mno-fix-loongson3-llsc", no_argument, NULL, OPTION_NO_FIX_LOONGSON3_LLSC},
c31f3936
RS
1659 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1660 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1661 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1662 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1663 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
1664 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1665 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
1666 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1667 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
1668 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
a8d14a88
CM
1669 {"mfix-rm7000", no_argument, NULL, OPTION_FIX_RM7000},
1670 {"mno-fix-rm7000", no_argument, NULL, OPTION_NO_FIX_RM7000},
c31f3936
RS
1671 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1672 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
27c634e0
FN
1673 {"mfix-r5900", no_argument, NULL, OPTION_FIX_R5900},
1674 {"mno-fix-r5900", no_argument, NULL, OPTION_NO_FIX_R5900},
c31f3936
RS
1675
1676 /* Miscellaneous options. */
1677 {"trap", no_argument, NULL, OPTION_TRAP},
1678 {"no-break", no_argument, NULL, OPTION_TRAP},
1679 {"break", no_argument, NULL, OPTION_BREAK},
1680 {"no-trap", no_argument, NULL, OPTION_BREAK},
1681 {"EB", no_argument, NULL, OPTION_EB},
1682 {"EL", no_argument, NULL, OPTION_EL},
1683 {"mfp32", no_argument, NULL, OPTION_FP32},
1684 {"mgp32", no_argument, NULL, OPTION_GP32},
1685 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1686 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1687 {"mfp64", no_argument, NULL, OPTION_FP64},
351cdf24 1688 {"mfpxx", no_argument, NULL, OPTION_FPXX},
c31f3936
RS
1689 {"mgp64", no_argument, NULL, OPTION_GP64},
1690 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1691 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
8b10b0b3
MR
1692 {"mignore-branch-isa", no_argument, NULL, OPTION_IGNORE_BRANCH_ISA},
1693 {"mno-ignore-branch-isa", no_argument, NULL, OPTION_NO_IGNORE_BRANCH_ISA},
833794fc
MR
1694 {"minsn32", no_argument, NULL, OPTION_INSN32},
1695 {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
c31f3936
RS
1696 {"mshared", no_argument, NULL, OPTION_MSHARED},
1697 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1698 {"msym32", no_argument, NULL, OPTION_MSYM32},
1699 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1700 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1701 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1702 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1703 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
351cdf24
MF
1704 {"modd-spreg", no_argument, NULL, OPTION_ODD_SPREG},
1705 {"mno-odd-spreg", no_argument, NULL, OPTION_NO_ODD_SPREG},
c31f3936
RS
1706
1707 /* Strictly speaking this next option is ELF specific,
1708 but we allow it for other ports as well in order to
1709 make testing easier. */
1710 {"32", no_argument, NULL, OPTION_32},
1711
1712 /* ELF-specific options. */
c31f3936
RS
1713 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1714 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1715 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1716 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
1717 {"xgot", no_argument, NULL, OPTION_XGOT},
1718 {"mabi", required_argument, NULL, OPTION_MABI},
1719 {"n32", no_argument, NULL, OPTION_N32},
1720 {"64", no_argument, NULL, OPTION_64},
1721 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1722 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1723 {"mpdr", no_argument, NULL, OPTION_PDR},
1724 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1725 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
ba92f887 1726 {"mnan", required_argument, NULL, OPTION_NAN},
c31f3936
RS
1727
1728 {NULL, no_argument, NULL, 0}
1729};
1730size_t md_longopts_size = sizeof (md_longopts);
1731\f
c6278170
RS
1732/* Information about either an Application Specific Extension or an
1733 optional architecture feature that, for simplicity, we treat in the
1734 same way as an ASE. */
1735struct mips_ase
1736{
1737 /* The name of the ASE, used in both the command-line and .set options. */
1738 const char *name;
1739
1740 /* The associated ASE_* flags. If the ASE is available on both 32-bit
1741 and 64-bit architectures, the flags here refer to the subset that
1742 is available on both. */
1743 unsigned int flags;
1744
1745 /* The ASE_* flag used for instructions that are available on 64-bit
1746 architectures but that are not included in FLAGS. */
1747 unsigned int flags64;
1748
1749 /* The command-line options that turn the ASE on and off. */
1750 int option_on;
1751 int option_off;
1752
1753 /* The minimum required architecture revisions for MIPS32, MIPS64,
1754 microMIPS32 and microMIPS64, or -1 if the extension isn't supported. */
1755 int mips32_rev;
1756 int mips64_rev;
1757 int micromips32_rev;
1758 int micromips64_rev;
7361da2c
AB
1759
1760 /* The architecture where the ASE was removed or -1 if the extension has not
1761 been removed. */
1762 int rem_rev;
c6278170
RS
1763};
1764
1765/* A table of all supported ASEs. */
1766static const struct mips_ase mips_ases[] = {
1767 { "dsp", ASE_DSP, ASE_DSP64,
1768 OPTION_DSP, OPTION_NO_DSP,
7361da2c
AB
1769 2, 2, 2, 2,
1770 -1 },
c6278170
RS
1771
1772 { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1773 OPTION_DSPR2, OPTION_NO_DSPR2,
7361da2c
AB
1774 2, 2, 2, 2,
1775 -1 },
c6278170 1776
8f4f9071
MF
1777 { "dspr3", ASE_DSP | ASE_DSPR2 | ASE_DSPR3, 0,
1778 OPTION_DSPR3, OPTION_NO_DSPR3,
1779 6, 6, -1, -1,
1780 -1 },
1781
c6278170
RS
1782 { "eva", ASE_EVA, 0,
1783 OPTION_EVA, OPTION_NO_EVA,
7361da2c
AB
1784 2, 2, 2, 2,
1785 -1 },
c6278170
RS
1786
1787 { "mcu", ASE_MCU, 0,
1788 OPTION_MCU, OPTION_NO_MCU,
7361da2c
AB
1789 2, 2, 2, 2,
1790 -1 },
c6278170
RS
1791
1792 /* Deprecated in MIPS64r5, but we don't implement that yet. */
1793 { "mdmx", ASE_MDMX, 0,
1794 OPTION_MDMX, OPTION_NO_MDMX,
7361da2c
AB
1795 -1, 1, -1, -1,
1796 6 },
c6278170
RS
1797
1798 /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2. */
1799 { "mips3d", ASE_MIPS3D, 0,
1800 OPTION_MIPS3D, OPTION_NO_MIPS3D,
7361da2c
AB
1801 2, 1, -1, -1,
1802 6 },
c6278170
RS
1803
1804 { "mt", ASE_MT, 0,
1805 OPTION_MT, OPTION_NO_MT,
7361da2c
AB
1806 2, 2, -1, -1,
1807 -1 },
c6278170
RS
1808
1809 { "smartmips", ASE_SMARTMIPS, 0,
1810 OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
7361da2c
AB
1811 1, -1, -1, -1,
1812 6 },
c6278170
RS
1813
1814 { "virt", ASE_VIRT, ASE_VIRT64,
1815 OPTION_VIRT, OPTION_NO_VIRT,
7361da2c
AB
1816 2, 2, 2, 2,
1817 -1 },
56d438b1
CF
1818
1819 { "msa", ASE_MSA, ASE_MSA64,
1820 OPTION_MSA, OPTION_NO_MSA,
7361da2c
AB
1821 2, 2, 2, 2,
1822 -1 },
7d64c587
AB
1823
1824 { "xpa", ASE_XPA, 0,
1825 OPTION_XPA, OPTION_NO_XPA,
909b4e3d 1826 2, 2, 2, 2,
7361da2c 1827 -1 },
25499ac7
MR
1828
1829 { "mips16e2", ASE_MIPS16E2, 0,
1830 OPTION_MIPS16E2, OPTION_NO_MIPS16E2,
1831 2, 2, -1, -1,
1832 6 },
730c3174
SE
1833
1834 { "crc", ASE_CRC, ASE_CRC64,
1835 OPTION_CRC, OPTION_NO_CRC,
1836 6, 6, -1, -1,
1837 -1 },
6f20c942
FS
1838
1839 { "ginv", ASE_GINV, 0,
1840 OPTION_GINV, OPTION_NO_GINV,
1841 6, 6, 6, 6,
1842 -1 },
8095d2f7
CX
1843
1844 { "loongson-mmi", ASE_LOONGSON_MMI, 0,
1845 OPTION_LOONGSON_MMI, OPTION_NO_LOONGSON_MMI,
1846 0, 0, -1, -1,
1847 -1 },
716c08de
CX
1848
1849 { "loongson-cam", ASE_LOONGSON_CAM, 0,
1850 OPTION_LOONGSON_CAM, OPTION_NO_LOONGSON_CAM,
1851 0, 0, -1, -1,
1852 -1 },
bdc6c06e
CX
1853
1854 { "loongson-ext", ASE_LOONGSON_EXT, 0,
1855 OPTION_LOONGSON_EXT, OPTION_NO_LOONGSON_EXT,
1856 0, 0, -1, -1,
1857 -1 },
a693765e
CX
1858
1859 { "loongson-ext2", ASE_LOONGSON_EXT | ASE_LOONGSON_EXT2, 0,
1860 OPTION_LOONGSON_EXT2, OPTION_NO_LOONGSON_EXT2,
1861 0, 0, -1, -1,
1862 -1 },
c6278170
RS
1863};
1864
1865/* The set of ASEs that require -mfp64. */
82bda27b 1866#define FP64_ASES (ASE_MIPS3D | ASE_MDMX | ASE_MSA)
c6278170
RS
1867
1868/* Groups of ASE_* flags that represent different revisions of an ASE. */
1869static const unsigned int mips_ase_groups[] = {
a693765e
CX
1870 ASE_DSP | ASE_DSPR2 | ASE_DSPR3,
1871 ASE_LOONGSON_EXT | ASE_LOONGSON_EXT2
c6278170
RS
1872};
1873\f
252b5132
RH
1874/* Pseudo-op table.
1875
1876 The following pseudo-ops from the Kane and Heinrich MIPS book
1877 should be defined here, but are currently unsupported: .alias,
1878 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1879
1880 The following pseudo-ops from the Kane and Heinrich MIPS book are
1881 specific to the type of debugging information being generated, and
1882 should be defined by the object format: .aent, .begin, .bend,
1883 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1884 .vreg.
1885
1886 The following pseudo-ops from the Kane and Heinrich MIPS book are
1887 not MIPS CPU specific, but are also not specific to the object file
1888 format. This file is probably the best place to define them, but
d84bcf09 1889 they are not currently supported: .asm0, .endr, .lab, .struct. */
252b5132 1890
e972090a
NC
1891static const pseudo_typeS mips_pseudo_table[] =
1892{
beae10d5 1893 /* MIPS specific pseudo-ops. */
252b5132
RH
1894 {"option", s_option, 0},
1895 {"set", s_mipsset, 0},
1896 {"rdata", s_change_sec, 'r'},
1897 {"sdata", s_change_sec, 's'},
1898 {"livereg", s_ignore, 0},
1899 {"abicalls", s_abicalls, 0},
1900 {"cpload", s_cpload, 0},
6478892d
TS
1901 {"cpsetup", s_cpsetup, 0},
1902 {"cplocal", s_cplocal, 0},
252b5132 1903 {"cprestore", s_cprestore, 0},
6478892d 1904 {"cpreturn", s_cpreturn, 0},
741d6ea8
JM
1905 {"dtprelword", s_dtprelword, 0},
1906 {"dtpreldword", s_dtpreldword, 0},
d0f13682
CLT
1907 {"tprelword", s_tprelword, 0},
1908 {"tpreldword", s_tpreldword, 0},
6478892d 1909 {"gpvalue", s_gpvalue, 0},
252b5132 1910 {"gpword", s_gpword, 0},
10181a0d 1911 {"gpdword", s_gpdword, 0},
a3f278e2 1912 {"ehword", s_ehword, 0},
252b5132
RH
1913 {"cpadd", s_cpadd, 0},
1914 {"insn", s_insn, 0},
ba92f887 1915 {"nan", s_nan, 0},
919731af 1916 {"module", s_module, 0},
252b5132 1917
beae10d5 1918 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132 1919 chips. */
38a57ae7 1920 {"asciiz", stringer, 8 + 1},
252b5132
RH
1921 {"bss", s_change_sec, 'b'},
1922 {"err", s_err, 0},
1923 {"half", s_cons, 1},
1924 {"dword", s_cons, 3},
1925 {"weakext", s_mips_weakext, 0},
7c752c2a
TS
1926 {"origin", s_org, 0},
1927 {"repeat", s_rept, 0},
252b5132 1928
998b3c36
MR
1929 /* For MIPS this is non-standard, but we define it for consistency. */
1930 {"sbss", s_change_sec, 'B'},
1931
beae10d5 1932 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1933 here for one reason or another. */
1934 {"align", s_align, 0},
1935 {"byte", s_cons, 0},
1936 {"data", s_change_sec, 'd'},
1937 {"double", s_float_cons, 'd'},
1938 {"float", s_float_cons, 'f'},
1939 {"globl", s_mips_globl, 0},
1940 {"global", s_mips_globl, 0},
1941 {"hword", s_cons, 1},
1942 {"int", s_cons, 2},
1943 {"long", s_cons, 2},
1944 {"octa", s_cons, 4},
1945 {"quad", s_cons, 3},
cca86cc8 1946 {"section", s_change_section, 0},
252b5132
RH
1947 {"short", s_cons, 1},
1948 {"single", s_float_cons, 'f'},
754e2bb9 1949 {"stabd", s_mips_stab, 'd'},
252b5132 1950 {"stabn", s_mips_stab, 'n'},
754e2bb9 1951 {"stabs", s_mips_stab, 's'},
252b5132
RH
1952 {"text", s_change_sec, 't'},
1953 {"word", s_cons, 2},
add56521 1954
add56521 1955 { "extern", ecoff_directive_extern, 0},
add56521 1956
43841e91 1957 { NULL, NULL, 0 },
252b5132
RH
1958};
1959
e972090a
NC
1960static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1961{
beae10d5
KH
1962 /* These pseudo-ops should be defined by the object file format.
1963 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1964 {"aent", s_mips_ent, 1},
1965 {"bgnb", s_ignore, 0},
1966 {"end", s_mips_end, 0},
1967 {"endb", s_ignore, 0},
1968 {"ent", s_mips_ent, 0},
c5dd6aab 1969 {"file", s_mips_file, 0},
252b5132
RH
1970 {"fmask", s_mips_mask, 'F'},
1971 {"frame", s_mips_frame, 0},
c5dd6aab 1972 {"loc", s_mips_loc, 0},
252b5132
RH
1973 {"mask", s_mips_mask, 'R'},
1974 {"verstamp", s_ignore, 0},
43841e91 1975 { NULL, NULL, 0 },
252b5132
RH
1976};
1977
3ae8dd8d
MR
1978/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1979 purpose of the `.dc.a' internal pseudo-op. */
1980
1981int
1982mips_address_bytes (void)
1983{
919731af 1984 file_mips_check_options ();
3ae8dd8d
MR
1985 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1986}
1987
17a2f251 1988extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1989
1990void
17a2f251 1991mips_pop_insert (void)
252b5132
RH
1992{
1993 pop_insert (mips_pseudo_table);
1994 if (! ECOFF_DEBUGGING)
1995 pop_insert (mips_nonecoff_pseudo_table);
1996}
1997\f
1998/* Symbols labelling the current insn. */
1999
e972090a
NC
2000struct insn_label_list
2001{
252b5132
RH
2002 struct insn_label_list *next;
2003 symbolS *label;
2004};
2005
252b5132 2006static struct insn_label_list *free_insn_labels;
742a56fe 2007#define label_list tc_segment_info_data.labels
252b5132 2008
17a2f251 2009static void mips_clear_insn_labels (void);
df58fc94
RS
2010static void mips_mark_labels (void);
2011static void mips_compressed_mark_labels (void);
252b5132
RH
2012
2013static inline void
17a2f251 2014mips_clear_insn_labels (void)
252b5132 2015{
ed9e98c2 2016 struct insn_label_list **pl;
a8dbcb85 2017 segment_info_type *si;
252b5132 2018
a8dbcb85
TS
2019 if (now_seg)
2020 {
2021 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
2022 ;
3739860c 2023
a8dbcb85
TS
2024 si = seg_info (now_seg);
2025 *pl = si->label_list;
2026 si->label_list = NULL;
2027 }
252b5132 2028}
a8dbcb85 2029
df58fc94
RS
2030/* Mark instruction labels in MIPS16/microMIPS mode. */
2031
2032static inline void
2033mips_mark_labels (void)
2034{
2035 if (HAVE_CODE_COMPRESSION)
2036 mips_compressed_mark_labels ();
2037}
252b5132
RH
2038\f
2039static char *expr_end;
2040
e423441d 2041/* An expression in a macro instruction. This is set by mips_ip and
b0e6f033 2042 mips16_ip and when populated is always an O_constant. */
252b5132
RH
2043
2044static expressionS imm_expr;
252b5132 2045
77bd4346
RS
2046/* The relocatable field in an instruction and the relocs associated
2047 with it. These variables are used for instructions like LUI and
2048 JAL as well as true offsets. They are also used for address
2049 operands in macros. */
252b5132 2050
77bd4346 2051static expressionS offset_expr;
f6688943
TS
2052static bfd_reloc_code_real_type offset_reloc[3]
2053 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 2054
df58fc94
RS
2055/* This is set to the resulting size of the instruction to be produced
2056 by mips16_ip if an explicit extension is used or by mips_ip if an
2057 explicit size is supplied. */
252b5132 2058
df58fc94 2059static unsigned int forced_insn_length;
252b5132 2060
e1b47bd5
RS
2061/* True if we are assembling an instruction. All dot symbols defined during
2062 this time should be treated as code labels. */
2063
2064static bfd_boolean mips_assembling_insn;
2065
ecb4347a
DJ
2066/* The pdr segment for per procedure frame/regmask info. Not used for
2067 ECOFF debugging. */
252b5132
RH
2068
2069static segT pdr_seg;
252b5132 2070
e013f690
TS
2071/* The default target format to use. */
2072
aeffff67
RS
2073#if defined (TE_FreeBSD)
2074#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
2075#elif defined (TE_TMIPS)
2076#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
2077#else
2078#define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
2079#endif
2080
e013f690 2081const char *
17a2f251 2082mips_target_format (void)
e013f690
TS
2083{
2084 switch (OUTPUT_FLAVOR)
2085 {
e013f690 2086 case bfd_target_elf_flavour:
0a44bf69
RS
2087#ifdef TE_VXWORKS
2088 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
2089 return (target_big_endian
2090 ? "elf32-bigmips-vxworks"
2091 : "elf32-littlemips-vxworks");
2092#endif
e013f690 2093 return (target_big_endian
cfe86eaa 2094 ? (HAVE_64BIT_OBJECTS
aeffff67 2095 ? ELF_TARGET ("elf64-", "big")
cfe86eaa 2096 : (HAVE_NEWABI
aeffff67
RS
2097 ? ELF_TARGET ("elf32-n", "big")
2098 : ELF_TARGET ("elf32-", "big")))
cfe86eaa 2099 : (HAVE_64BIT_OBJECTS
aeffff67 2100 ? ELF_TARGET ("elf64-", "little")
cfe86eaa 2101 : (HAVE_NEWABI
aeffff67
RS
2102 ? ELF_TARGET ("elf32-n", "little")
2103 : ELF_TARGET ("elf32-", "little"))));
e013f690
TS
2104 default:
2105 abort ();
2106 return NULL;
2107 }
2108}
2109
c6278170
RS
2110/* Return the ISA revision that is currently in use, or 0 if we are
2111 generating code for MIPS V or below. */
2112
2113static int
2114mips_isa_rev (void)
2115{
2116 if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
2117 return 2;
2118
ae52f483
AB
2119 if (mips_opts.isa == ISA_MIPS32R3 || mips_opts.isa == ISA_MIPS64R3)
2120 return 3;
2121
2122 if (mips_opts.isa == ISA_MIPS32R5 || mips_opts.isa == ISA_MIPS64R5)
2123 return 5;
2124
7361da2c
AB
2125 if (mips_opts.isa == ISA_MIPS32R6 || mips_opts.isa == ISA_MIPS64R6)
2126 return 6;
2127
c6278170
RS
2128 /* microMIPS implies revision 2 or above. */
2129 if (mips_opts.micromips)
2130 return 2;
2131
2132 if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
2133 return 1;
2134
2135 return 0;
2136}
2137
2138/* Return the mask of all ASEs that are revisions of those in FLAGS. */
2139
2140static unsigned int
2141mips_ase_mask (unsigned int flags)
2142{
2143 unsigned int i;
2144
2145 for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
2146 if (flags & mips_ase_groups[i])
2147 flags |= mips_ase_groups[i];
2148 return flags;
2149}
2150
2151/* Check whether the current ISA supports ASE. Issue a warning if
2152 appropriate. */
2153
2154static void
2155mips_check_isa_supports_ase (const struct mips_ase *ase)
2156{
2157 const char *base;
2158 int min_rev, size;
2159 static unsigned int warned_isa;
2160 static unsigned int warned_fp32;
2161
2162 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
2163 min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
2164 else
2165 min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
2166 if ((min_rev < 0 || mips_isa_rev () < min_rev)
2167 && (warned_isa & ase->flags) != ase->flags)
2168 {
2169 warned_isa |= ase->flags;
2170 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2171 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2172 if (min_rev < 0)
1661c76c 2173 as_warn (_("the %d-bit %s architecture does not support the"
c6278170
RS
2174 " `%s' extension"), size, base, ase->name);
2175 else
1661c76c 2176 as_warn (_("the `%s' extension requires %s%d revision %d or greater"),
c6278170
RS
2177 ase->name, base, size, min_rev);
2178 }
7361da2c
AB
2179 else if ((ase->rem_rev > 0 && mips_isa_rev () >= ase->rem_rev)
2180 && (warned_isa & ase->flags) != ase->flags)
2181 {
2182 warned_isa |= ase->flags;
2183 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2184 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2185 as_warn (_("the `%s' extension was removed in %s%d revision %d"),
2186 ase->name, base, size, ase->rem_rev);
2187 }
2188
c6278170 2189 if ((ase->flags & FP64_ASES)
0b35dfee 2190 && mips_opts.fp != 64
c6278170
RS
2191 && (warned_fp32 & ase->flags) != ase->flags)
2192 {
2193 warned_fp32 |= ase->flags;
1661c76c 2194 as_warn (_("the `%s' extension requires 64-bit FPRs"), ase->name);
c6278170
RS
2195 }
2196}
2197
2198/* Check all enabled ASEs to see whether they are supported by the
2199 chosen architecture. */
2200
2201static void
2202mips_check_isa_supports_ases (void)
2203{
2204 unsigned int i, mask;
2205
2206 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2207 {
2208 mask = mips_ase_mask (mips_ases[i].flags);
2209 if ((mips_opts.ase & mask) == mips_ases[i].flags)
2210 mips_check_isa_supports_ase (&mips_ases[i]);
2211 }
2212}
2213
2214/* Set the state of ASE to ENABLED_P. Return the mask of ASE_* flags
2215 that were affected. */
2216
2217static unsigned int
919731af 2218mips_set_ase (const struct mips_ase *ase, struct mips_set_options *opts,
2219 bfd_boolean enabled_p)
c6278170
RS
2220{
2221 unsigned int mask;
2222
2223 mask = mips_ase_mask (ase->flags);
919731af 2224 opts->ase &= ~mask;
92cebb3d
MR
2225
2226 /* Clear combination ASE flags, which need to be recalculated based on
2227 updated regular ASE settings. */
41cee089 2228 opts->ase &= ~(ASE_MIPS16E2_MT | ASE_XPA_VIRT | ASE_EVA_R6);
92cebb3d 2229
c6278170 2230 if (enabled_p)
919731af 2231 opts->ase |= ase->flags;
25499ac7 2232
9785fc2a
MR
2233 /* The Virtualization ASE has eXtended Physical Addressing (XPA)
2234 instructions which are only valid when both ASEs are enabled.
2235 This sets the ASE_XPA_VIRT flag when both ASEs are present. */
2236 if ((opts->ase & (ASE_XPA | ASE_VIRT)) == (ASE_XPA | ASE_VIRT))
2237 {
2238 opts->ase |= ASE_XPA_VIRT;
2239 mask |= ASE_XPA_VIRT;
2240 }
25499ac7
MR
2241 if ((opts->ase & (ASE_MIPS16E2 | ASE_MT)) == (ASE_MIPS16E2 | ASE_MT))
2242 {
2243 opts->ase |= ASE_MIPS16E2_MT;
2244 mask |= ASE_MIPS16E2_MT;
2245 }
2246
41cee089
FS
2247 /* The EVA Extension has instructions which are only valid when the R6 ISA
2248 is enabled. This sets the ASE_EVA_R6 flag when both EVA and R6 ISA are
2249 present. */
2250 if (((opts->ase & ASE_EVA) != 0) && ISA_IS_R6 (opts->isa))
2251 {
2252 opts->ase |= ASE_EVA_R6;
2253 mask |= ASE_EVA_R6;
2254 }
2255
c6278170
RS
2256 return mask;
2257}
2258
2259/* Return the ASE called NAME, or null if none. */
2260
2261static const struct mips_ase *
2262mips_lookup_ase (const char *name)
2263{
2264 unsigned int i;
2265
2266 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2267 if (strcmp (name, mips_ases[i].name) == 0)
2268 return &mips_ases[i];
2269 return NULL;
2270}
2271
df58fc94 2272/* Return the length of a microMIPS instruction in bytes. If bits of
100b4f2e
MR
2273 the mask beyond the low 16 are 0, then it is a 16-bit instruction,
2274 otherwise it is a 32-bit instruction. */
df58fc94
RS
2275
2276static inline unsigned int
2277micromips_insn_length (const struct mips_opcode *mo)
2278{
7fd53920 2279 return mips_opcode_32bit_p (mo) ? 4 : 2;
df58fc94
RS
2280}
2281
5c04167a
RS
2282/* Return the length of MIPS16 instruction OPCODE. */
2283
2284static inline unsigned int
2285mips16_opcode_length (unsigned long opcode)
2286{
2287 return (opcode >> 16) == 0 ? 2 : 4;
2288}
2289
1e915849
RS
2290/* Return the length of instruction INSN. */
2291
2292static inline unsigned int
2293insn_length (const struct mips_cl_insn *insn)
2294{
df58fc94
RS
2295 if (mips_opts.micromips)
2296 return micromips_insn_length (insn->insn_mo);
2297 else if (mips_opts.mips16)
5c04167a 2298 return mips16_opcode_length (insn->insn_opcode);
df58fc94 2299 else
1e915849 2300 return 4;
1e915849
RS
2301}
2302
2303/* Initialise INSN from opcode entry MO. Leave its position unspecified. */
2304
2305static void
2306create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
2307{
2308 size_t i;
2309
2310 insn->insn_mo = mo;
1e915849
RS
2311 insn->insn_opcode = mo->match;
2312 insn->frag = NULL;
2313 insn->where = 0;
2314 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2315 insn->fixp[i] = NULL;
2316 insn->fixed_p = (mips_opts.noreorder > 0);
2317 insn->noreorder_p = (mips_opts.noreorder > 0);
2318 insn->mips16_absolute_jump_p = 0;
15be625d 2319 insn->complete_p = 0;
e407c74b 2320 insn->cleared_p = 0;
1e915849
RS
2321}
2322
fc76e730
RS
2323/* Get a list of all the operands in INSN. */
2324
2325static const struct mips_operand_array *
2326insn_operands (const struct mips_cl_insn *insn)
2327{
2328 if (insn->insn_mo >= &mips_opcodes[0]
2329 && insn->insn_mo < &mips_opcodes[NUMOPCODES])
2330 return &mips_operands[insn->insn_mo - &mips_opcodes[0]];
2331
2332 if (insn->insn_mo >= &mips16_opcodes[0]
2333 && insn->insn_mo < &mips16_opcodes[bfd_mips16_num_opcodes])
2334 return &mips16_operands[insn->insn_mo - &mips16_opcodes[0]];
2335
2336 if (insn->insn_mo >= &micromips_opcodes[0]
2337 && insn->insn_mo < &micromips_opcodes[bfd_micromips_num_opcodes])
2338 return &micromips_operands[insn->insn_mo - &micromips_opcodes[0]];
2339
2340 abort ();
2341}
2342
2343/* Get a description of operand OPNO of INSN. */
2344
2345static const struct mips_operand *
2346insn_opno (const struct mips_cl_insn *insn, unsigned opno)
2347{
2348 const struct mips_operand_array *operands;
2349
2350 operands = insn_operands (insn);
2351 if (opno >= MAX_OPERANDS || !operands->operand[opno])
2352 abort ();
2353 return operands->operand[opno];
2354}
2355
e077a1c8
RS
2356/* Install UVAL as the value of OPERAND in INSN. */
2357
2358static inline void
2359insn_insert_operand (struct mips_cl_insn *insn,
2360 const struct mips_operand *operand, unsigned int uval)
2361{
25499ac7
MR
2362 if (mips_opts.mips16
2363 && operand->type == OP_INT && operand->lsb == 0
2364 && mips_opcode_32bit_p (insn->insn_mo))
2365 insn->insn_opcode |= mips16_immed_extend (uval, operand->size);
2366 else
2367 insn->insn_opcode = mips_insert_operand (operand, insn->insn_opcode, uval);
e077a1c8
RS
2368}
2369
fc76e730
RS
2370/* Extract the value of OPERAND from INSN. */
2371
2372static inline unsigned
2373insn_extract_operand (const struct mips_cl_insn *insn,
2374 const struct mips_operand *operand)
2375{
2376 return mips_extract_operand (operand, insn->insn_opcode);
2377}
2378
df58fc94 2379/* Record the current MIPS16/microMIPS mode in now_seg. */
742a56fe
RS
2380
2381static void
df58fc94 2382mips_record_compressed_mode (void)
742a56fe
RS
2383{
2384 segment_info_type *si;
2385
2386 si = seg_info (now_seg);
2387 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2388 si->tc_segment_info_data.mips16 = mips_opts.mips16;
df58fc94
RS
2389 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2390 si->tc_segment_info_data.micromips = mips_opts.micromips;
742a56fe
RS
2391}
2392
4d68580a
RS
2393/* Read a standard MIPS instruction from BUF. */
2394
2395static unsigned long
2396read_insn (char *buf)
2397{
2398 if (target_big_endian)
2399 return bfd_getb32 ((bfd_byte *) buf);
2400 else
2401 return bfd_getl32 ((bfd_byte *) buf);
2402}
2403
2404/* Write standard MIPS instruction INSN to BUF. Return a pointer to
2405 the next byte. */
2406
2407static char *
2408write_insn (char *buf, unsigned int insn)
2409{
2410 md_number_to_chars (buf, insn, 4);
2411 return buf + 4;
2412}
2413
2414/* Read a microMIPS or MIPS16 opcode from BUF, given that it
2415 has length LENGTH. */
2416
2417static unsigned long
2418read_compressed_insn (char *buf, unsigned int length)
2419{
2420 unsigned long insn;
2421 unsigned int i;
2422
2423 insn = 0;
2424 for (i = 0; i < length; i += 2)
2425 {
2426 insn <<= 16;
2427 if (target_big_endian)
2428 insn |= bfd_getb16 ((char *) buf);
2429 else
2430 insn |= bfd_getl16 ((char *) buf);
2431 buf += 2;
2432 }
2433 return insn;
2434}
2435
5c04167a
RS
2436/* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2437 instruction is LENGTH bytes long. Return a pointer to the next byte. */
2438
2439static char *
2440write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2441{
2442 unsigned int i;
2443
2444 for (i = 0; i < length; i += 2)
2445 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2446 return buf + length;
2447}
2448
1e915849
RS
2449/* Install INSN at the location specified by its "frag" and "where" fields. */
2450
2451static void
2452install_insn (const struct mips_cl_insn *insn)
2453{
2454 char *f = insn->frag->fr_literal + insn->where;
5c04167a
RS
2455 if (HAVE_CODE_COMPRESSION)
2456 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
1e915849 2457 else
4d68580a 2458 write_insn (f, insn->insn_opcode);
df58fc94 2459 mips_record_compressed_mode ();
1e915849
RS
2460}
2461
2462/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
2463 and install the opcode in the new location. */
2464
2465static void
2466move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2467{
2468 size_t i;
2469
2470 insn->frag = frag;
2471 insn->where = where;
2472 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2473 if (insn->fixp[i] != NULL)
2474 {
2475 insn->fixp[i]->fx_frag = frag;
2476 insn->fixp[i]->fx_where = where;
2477 }
2478 install_insn (insn);
2479}
2480
2481/* Add INSN to the end of the output. */
2482
2483static void
2484add_fixed_insn (struct mips_cl_insn *insn)
2485{
2486 char *f = frag_more (insn_length (insn));
2487 move_insn (insn, frag_now, f - frag_now->fr_literal);
2488}
2489
2490/* Start a variant frag and move INSN to the start of the variant part,
2491 marking it as fixed. The other arguments are as for frag_var. */
2492
2493static void
2494add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2495 relax_substateT subtype, symbolS *symbol, offsetT offset)
2496{
2497 frag_grow (max_chars);
2498 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2499 insn->fixed_p = 1;
2500 frag_var (rs_machine_dependent, max_chars, var,
2501 subtype, symbol, offset, NULL);
2502}
2503
2504/* Insert N copies of INSN into the history buffer, starting at
2505 position FIRST. Neither FIRST nor N need to be clipped. */
2506
2507static void
2508insert_into_history (unsigned int first, unsigned int n,
2509 const struct mips_cl_insn *insn)
2510{
2511 if (mips_relax.sequence != 2)
2512 {
2513 unsigned int i;
2514
2515 for (i = ARRAY_SIZE (history); i-- > first;)
2516 if (i >= first + n)
2517 history[i] = history[i - n];
2518 else
2519 history[i] = *insn;
2520 }
2521}
2522
e3de51ce
RS
2523/* Clear the error in insn_error. */
2524
2525static void
2526clear_insn_error (void)
2527{
2528 memset (&insn_error, 0, sizeof (insn_error));
2529}
2530
2531/* Possibly record error message MSG for the current instruction.
2532 If the error is about a particular argument, ARGNUM is the 1-based
2533 number of that argument, otherwise it is 0. FORMAT is the format
2534 of MSG. Return true if MSG was used, false if the current message
2535 was kept. */
2536
2537static bfd_boolean
2538set_insn_error_format (int argnum, enum mips_insn_error_format format,
2539 const char *msg)
2540{
2541 if (argnum == 0)
2542 {
2543 /* Give priority to errors against specific arguments, and to
2544 the first whole-instruction message. */
2545 if (insn_error.msg)
2546 return FALSE;
2547 }
2548 else
2549 {
2550 /* Keep insn_error if it is against a later argument. */
2551 if (argnum < insn_error.min_argnum)
2552 return FALSE;
2553
2554 /* If both errors are against the same argument but are different,
2555 give up on reporting a specific error for this argument.
2556 See the comment about mips_insn_error for details. */
2557 if (argnum == insn_error.min_argnum
2558 && insn_error.msg
2559 && strcmp (insn_error.msg, msg) != 0)
2560 {
2561 insn_error.msg = 0;
2562 insn_error.min_argnum += 1;
2563 return FALSE;
2564 }
2565 }
2566 insn_error.min_argnum = argnum;
2567 insn_error.format = format;
2568 insn_error.msg = msg;
2569 return TRUE;
2570}
2571
2572/* Record an instruction error with no % format fields. ARGNUM and MSG are
2573 as for set_insn_error_format. */
2574
2575static void
2576set_insn_error (int argnum, const char *msg)
2577{
2578 set_insn_error_format (argnum, ERR_FMT_PLAIN, msg);
2579}
2580
2581/* Record an instruction error with one %d field I. ARGNUM and MSG are
2582 as for set_insn_error_format. */
2583
2584static void
2585set_insn_error_i (int argnum, const char *msg, int i)
2586{
2587 if (set_insn_error_format (argnum, ERR_FMT_I, msg))
2588 insn_error.u.i = i;
2589}
2590
2591/* Record an instruction error with two %s fields S1 and S2. ARGNUM and MSG
2592 are as for set_insn_error_format. */
2593
2594static void
2595set_insn_error_ss (int argnum, const char *msg, const char *s1, const char *s2)
2596{
2597 if (set_insn_error_format (argnum, ERR_FMT_SS, msg))
2598 {
2599 insn_error.u.ss[0] = s1;
2600 insn_error.u.ss[1] = s2;
2601 }
2602}
2603
2604/* Report the error in insn_error, which is against assembly code STR. */
2605
2606static void
2607report_insn_error (const char *str)
2608{
e1fa0163 2609 const char *msg = concat (insn_error.msg, " `%s'", NULL);
e3de51ce 2610
e3de51ce
RS
2611 switch (insn_error.format)
2612 {
2613 case ERR_FMT_PLAIN:
2614 as_bad (msg, str);
2615 break;
2616
2617 case ERR_FMT_I:
2618 as_bad (msg, insn_error.u.i, str);
2619 break;
2620
2621 case ERR_FMT_SS:
2622 as_bad (msg, insn_error.u.ss[0], insn_error.u.ss[1], str);
2623 break;
2624 }
e1fa0163
NC
2625
2626 free ((char *) msg);
e3de51ce
RS
2627}
2628
71400594
RS
2629/* Initialize vr4120_conflicts. There is a bit of duplication here:
2630 the idea is to make it obvious at a glance that each errata is
2631 included. */
2632
2633static void
2634init_vr4120_conflicts (void)
2635{
2636#define CONFLICT(FIRST, SECOND) \
2637 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2638
2639 /* Errata 21 - [D]DIV[U] after [D]MACC */
2640 CONFLICT (MACC, DIV);
2641 CONFLICT (DMACC, DIV);
2642
2643 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
2644 CONFLICT (DMULT, DMULT);
2645 CONFLICT (DMULT, DMACC);
2646 CONFLICT (DMACC, DMULT);
2647 CONFLICT (DMACC, DMACC);
2648
2649 /* Errata 24 - MT{LO,HI} after [D]MACC */
2650 CONFLICT (MACC, MTHILO);
2651 CONFLICT (DMACC, MTHILO);
2652
2653 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2654 instruction is executed immediately after a MACC or DMACC
2655 instruction, the result of [either instruction] is incorrect." */
2656 CONFLICT (MACC, MULT);
2657 CONFLICT (MACC, DMULT);
2658 CONFLICT (DMACC, MULT);
2659 CONFLICT (DMACC, DMULT);
2660
2661 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2662 executed immediately after a DMULT, DMULTU, DIV, DIVU,
2663 DDIV or DDIVU instruction, the result of the MACC or
2664 DMACC instruction is incorrect.". */
2665 CONFLICT (DMULT, MACC);
2666 CONFLICT (DMULT, DMACC);
2667 CONFLICT (DIV, MACC);
2668 CONFLICT (DIV, DMACC);
2669
2670#undef CONFLICT
2671}
2672
707bfff6
TS
2673struct regname {
2674 const char *name;
2675 unsigned int num;
2676};
2677
14daeee3 2678#define RNUM_MASK 0x00000ff
56d438b1 2679#define RTYPE_MASK 0x0ffff00
14daeee3
RS
2680#define RTYPE_NUM 0x0000100
2681#define RTYPE_FPU 0x0000200
2682#define RTYPE_FCC 0x0000400
2683#define RTYPE_VEC 0x0000800
2684#define RTYPE_GP 0x0001000
2685#define RTYPE_CP0 0x0002000
2686#define RTYPE_PC 0x0004000
2687#define RTYPE_ACC 0x0008000
2688#define RTYPE_CCC 0x0010000
2689#define RTYPE_VI 0x0020000
2690#define RTYPE_VF 0x0040000
2691#define RTYPE_R5900_I 0x0080000
2692#define RTYPE_R5900_Q 0x0100000
2693#define RTYPE_R5900_R 0x0200000
2694#define RTYPE_R5900_ACC 0x0400000
56d438b1 2695#define RTYPE_MSA 0x0800000
14daeee3 2696#define RWARN 0x8000000
707bfff6
TS
2697
2698#define GENERIC_REGISTER_NUMBERS \
2699 {"$0", RTYPE_NUM | 0}, \
2700 {"$1", RTYPE_NUM | 1}, \
2701 {"$2", RTYPE_NUM | 2}, \
2702 {"$3", RTYPE_NUM | 3}, \
2703 {"$4", RTYPE_NUM | 4}, \
2704 {"$5", RTYPE_NUM | 5}, \
2705 {"$6", RTYPE_NUM | 6}, \
2706 {"$7", RTYPE_NUM | 7}, \
2707 {"$8", RTYPE_NUM | 8}, \
2708 {"$9", RTYPE_NUM | 9}, \
2709 {"$10", RTYPE_NUM | 10}, \
2710 {"$11", RTYPE_NUM | 11}, \
2711 {"$12", RTYPE_NUM | 12}, \
2712 {"$13", RTYPE_NUM | 13}, \
2713 {"$14", RTYPE_NUM | 14}, \
2714 {"$15", RTYPE_NUM | 15}, \
2715 {"$16", RTYPE_NUM | 16}, \
2716 {"$17", RTYPE_NUM | 17}, \
2717 {"$18", RTYPE_NUM | 18}, \
2718 {"$19", RTYPE_NUM | 19}, \
2719 {"$20", RTYPE_NUM | 20}, \
2720 {"$21", RTYPE_NUM | 21}, \
2721 {"$22", RTYPE_NUM | 22}, \
2722 {"$23", RTYPE_NUM | 23}, \
2723 {"$24", RTYPE_NUM | 24}, \
2724 {"$25", RTYPE_NUM | 25}, \
2725 {"$26", RTYPE_NUM | 26}, \
2726 {"$27", RTYPE_NUM | 27}, \
2727 {"$28", RTYPE_NUM | 28}, \
2728 {"$29", RTYPE_NUM | 29}, \
2729 {"$30", RTYPE_NUM | 30}, \
3739860c 2730 {"$31", RTYPE_NUM | 31}
707bfff6
TS
2731
2732#define FPU_REGISTER_NAMES \
2733 {"$f0", RTYPE_FPU | 0}, \
2734 {"$f1", RTYPE_FPU | 1}, \
2735 {"$f2", RTYPE_FPU | 2}, \
2736 {"$f3", RTYPE_FPU | 3}, \
2737 {"$f4", RTYPE_FPU | 4}, \
2738 {"$f5", RTYPE_FPU | 5}, \
2739 {"$f6", RTYPE_FPU | 6}, \
2740 {"$f7", RTYPE_FPU | 7}, \
2741 {"$f8", RTYPE_FPU | 8}, \
2742 {"$f9", RTYPE_FPU | 9}, \
2743 {"$f10", RTYPE_FPU | 10}, \
2744 {"$f11", RTYPE_FPU | 11}, \
2745 {"$f12", RTYPE_FPU | 12}, \
2746 {"$f13", RTYPE_FPU | 13}, \
2747 {"$f14", RTYPE_FPU | 14}, \
2748 {"$f15", RTYPE_FPU | 15}, \
2749 {"$f16", RTYPE_FPU | 16}, \
2750 {"$f17", RTYPE_FPU | 17}, \
2751 {"$f18", RTYPE_FPU | 18}, \
2752 {"$f19", RTYPE_FPU | 19}, \
2753 {"$f20", RTYPE_FPU | 20}, \
2754 {"$f21", RTYPE_FPU | 21}, \
2755 {"$f22", RTYPE_FPU | 22}, \
2756 {"$f23", RTYPE_FPU | 23}, \
2757 {"$f24", RTYPE_FPU | 24}, \
2758 {"$f25", RTYPE_FPU | 25}, \
2759 {"$f26", RTYPE_FPU | 26}, \
2760 {"$f27", RTYPE_FPU | 27}, \
2761 {"$f28", RTYPE_FPU | 28}, \
2762 {"$f29", RTYPE_FPU | 29}, \
2763 {"$f30", RTYPE_FPU | 30}, \
2764 {"$f31", RTYPE_FPU | 31}
2765
2766#define FPU_CONDITION_CODE_NAMES \
2767 {"$fcc0", RTYPE_FCC | 0}, \
2768 {"$fcc1", RTYPE_FCC | 1}, \
2769 {"$fcc2", RTYPE_FCC | 2}, \
2770 {"$fcc3", RTYPE_FCC | 3}, \
2771 {"$fcc4", RTYPE_FCC | 4}, \
2772 {"$fcc5", RTYPE_FCC | 5}, \
2773 {"$fcc6", RTYPE_FCC | 6}, \
2774 {"$fcc7", RTYPE_FCC | 7}
2775
2776#define COPROC_CONDITION_CODE_NAMES \
2777 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
2778 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
2779 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
2780 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
2781 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
2782 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
2783 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
2784 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
2785
2786#define N32N64_SYMBOLIC_REGISTER_NAMES \
2787 {"$a4", RTYPE_GP | 8}, \
2788 {"$a5", RTYPE_GP | 9}, \
2789 {"$a6", RTYPE_GP | 10}, \
2790 {"$a7", RTYPE_GP | 11}, \
2791 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
2792 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
2793 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
2794 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
2795 {"$t0", RTYPE_GP | 12}, \
2796 {"$t1", RTYPE_GP | 13}, \
2797 {"$t2", RTYPE_GP | 14}, \
2798 {"$t3", RTYPE_GP | 15}
2799
2800#define O32_SYMBOLIC_REGISTER_NAMES \
2801 {"$t0", RTYPE_GP | 8}, \
2802 {"$t1", RTYPE_GP | 9}, \
2803 {"$t2", RTYPE_GP | 10}, \
2804 {"$t3", RTYPE_GP | 11}, \
2805 {"$t4", RTYPE_GP | 12}, \
2806 {"$t5", RTYPE_GP | 13}, \
2807 {"$t6", RTYPE_GP | 14}, \
2808 {"$t7", RTYPE_GP | 15}, \
2809 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2810 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2811 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
3739860c 2812 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
707bfff6 2813
6f2117ba 2814/* Remaining symbolic register names. */
707bfff6
TS
2815#define SYMBOLIC_REGISTER_NAMES \
2816 {"$zero", RTYPE_GP | 0}, \
2817 {"$at", RTYPE_GP | 1}, \
2818 {"$AT", RTYPE_GP | 1}, \
2819 {"$v0", RTYPE_GP | 2}, \
2820 {"$v1", RTYPE_GP | 3}, \
2821 {"$a0", RTYPE_GP | 4}, \
2822 {"$a1", RTYPE_GP | 5}, \
2823 {"$a2", RTYPE_GP | 6}, \
2824 {"$a3", RTYPE_GP | 7}, \
2825 {"$s0", RTYPE_GP | 16}, \
2826 {"$s1", RTYPE_GP | 17}, \
2827 {"$s2", RTYPE_GP | 18}, \
2828 {"$s3", RTYPE_GP | 19}, \
2829 {"$s4", RTYPE_GP | 20}, \
2830 {"$s5", RTYPE_GP | 21}, \
2831 {"$s6", RTYPE_GP | 22}, \
2832 {"$s7", RTYPE_GP | 23}, \
2833 {"$t8", RTYPE_GP | 24}, \
2834 {"$t9", RTYPE_GP | 25}, \
2835 {"$k0", RTYPE_GP | 26}, \
2836 {"$kt0", RTYPE_GP | 26}, \
2837 {"$k1", RTYPE_GP | 27}, \
2838 {"$kt1", RTYPE_GP | 27}, \
2839 {"$gp", RTYPE_GP | 28}, \
2840 {"$sp", RTYPE_GP | 29}, \
2841 {"$s8", RTYPE_GP | 30}, \
2842 {"$fp", RTYPE_GP | 30}, \
2843 {"$ra", RTYPE_GP | 31}
2844
2845#define MIPS16_SPECIAL_REGISTER_NAMES \
2846 {"$pc", RTYPE_PC | 0}
2847
2848#define MDMX_VECTOR_REGISTER_NAMES \
6f2117ba
PH
2849 /* {"$v0", RTYPE_VEC | 0}, Clash with REG 2 above. */ \
2850 /* {"$v1", RTYPE_VEC | 1}, Clash with REG 3 above. */ \
707bfff6
TS
2851 {"$v2", RTYPE_VEC | 2}, \
2852 {"$v3", RTYPE_VEC | 3}, \
2853 {"$v4", RTYPE_VEC | 4}, \
2854 {"$v5", RTYPE_VEC | 5}, \
2855 {"$v6", RTYPE_VEC | 6}, \
2856 {"$v7", RTYPE_VEC | 7}, \
2857 {"$v8", RTYPE_VEC | 8}, \
2858 {"$v9", RTYPE_VEC | 9}, \
2859 {"$v10", RTYPE_VEC | 10}, \
2860 {"$v11", RTYPE_VEC | 11}, \
2861 {"$v12", RTYPE_VEC | 12}, \
2862 {"$v13", RTYPE_VEC | 13}, \
2863 {"$v14", RTYPE_VEC | 14}, \
2864 {"$v15", RTYPE_VEC | 15}, \
2865 {"$v16", RTYPE_VEC | 16}, \
2866 {"$v17", RTYPE_VEC | 17}, \
2867 {"$v18", RTYPE_VEC | 18}, \
2868 {"$v19", RTYPE_VEC | 19}, \
2869 {"$v20", RTYPE_VEC | 20}, \
2870 {"$v21", RTYPE_VEC | 21}, \
2871 {"$v22", RTYPE_VEC | 22}, \
2872 {"$v23", RTYPE_VEC | 23}, \
2873 {"$v24", RTYPE_VEC | 24}, \
2874 {"$v25", RTYPE_VEC | 25}, \
2875 {"$v26", RTYPE_VEC | 26}, \
2876 {"$v27", RTYPE_VEC | 27}, \
2877 {"$v28", RTYPE_VEC | 28}, \
2878 {"$v29", RTYPE_VEC | 29}, \
2879 {"$v30", RTYPE_VEC | 30}, \
2880 {"$v31", RTYPE_VEC | 31}
2881
14daeee3
RS
2882#define R5900_I_NAMES \
2883 {"$I", RTYPE_R5900_I | 0}
2884
2885#define R5900_Q_NAMES \
2886 {"$Q", RTYPE_R5900_Q | 0}
2887
2888#define R5900_R_NAMES \
2889 {"$R", RTYPE_R5900_R | 0}
2890
2891#define R5900_ACC_NAMES \
2892 {"$ACC", RTYPE_R5900_ACC | 0 }
2893
707bfff6
TS
2894#define MIPS_DSP_ACCUMULATOR_NAMES \
2895 {"$ac0", RTYPE_ACC | 0}, \
2896 {"$ac1", RTYPE_ACC | 1}, \
2897 {"$ac2", RTYPE_ACC | 2}, \
2898 {"$ac3", RTYPE_ACC | 3}
2899
2900static const struct regname reg_names[] = {
2901 GENERIC_REGISTER_NUMBERS,
2902 FPU_REGISTER_NAMES,
2903 FPU_CONDITION_CODE_NAMES,
2904 COPROC_CONDITION_CODE_NAMES,
2905
2906 /* The $txx registers depends on the abi,
2907 these will be added later into the symbol table from
3739860c 2908 one of the tables below once mips_abi is set after
707bfff6
TS
2909 parsing of arguments from the command line. */
2910 SYMBOLIC_REGISTER_NAMES,
2911
2912 MIPS16_SPECIAL_REGISTER_NAMES,
2913 MDMX_VECTOR_REGISTER_NAMES,
14daeee3
RS
2914 R5900_I_NAMES,
2915 R5900_Q_NAMES,
2916 R5900_R_NAMES,
2917 R5900_ACC_NAMES,
707bfff6
TS
2918 MIPS_DSP_ACCUMULATOR_NAMES,
2919 {0, 0}
2920};
2921
2922static const struct regname reg_names_o32[] = {
2923 O32_SYMBOLIC_REGISTER_NAMES,
2924 {0, 0}
2925};
2926
2927static const struct regname reg_names_n32n64[] = {
2928 N32N64_SYMBOLIC_REGISTER_NAMES,
2929 {0, 0}
2930};
2931
a92713e6
RS
2932/* Register symbols $v0 and $v1 map to GPRs 2 and 3, but they can also be
2933 interpreted as vector registers 0 and 1. If SYMVAL is the value of one
2934 of these register symbols, return the associated vector register,
2935 otherwise return SYMVAL itself. */
df58fc94 2936
a92713e6
RS
2937static unsigned int
2938mips_prefer_vec_regno (unsigned int symval)
707bfff6 2939{
a92713e6
RS
2940 if ((symval & -2) == (RTYPE_GP | 2))
2941 return RTYPE_VEC | (symval & 1);
2942 return symval;
2943}
2944
14daeee3
RS
2945/* Return true if string [S, E) is a valid register name, storing its
2946 symbol value in *SYMVAL_PTR if so. */
a92713e6
RS
2947
2948static bfd_boolean
14daeee3 2949mips_parse_register_1 (char *s, char *e, unsigned int *symval_ptr)
a92713e6 2950{
707bfff6 2951 char save_c;
14daeee3 2952 symbolS *symbol;
707bfff6
TS
2953
2954 /* Terminate name. */
2955 save_c = *e;
2956 *e = '\0';
2957
a92713e6
RS
2958 /* Look up the name. */
2959 symbol = symbol_find (s);
2960 *e = save_c;
2961
2962 if (!symbol || S_GET_SEGMENT (symbol) != reg_section)
2963 return FALSE;
2964
14daeee3
RS
2965 *symval_ptr = S_GET_VALUE (symbol);
2966 return TRUE;
2967}
2968
2969/* Return true if the string at *SPTR is a valid register name. Allow it
2970 to have a VU0-style channel suffix of the form x?y?z?w? if CHANNELS_PTR
2971 is nonnull.
2972
2973 When returning true, move *SPTR past the register, store the
2974 register's symbol value in *SYMVAL_PTR and the channel mask in
2975 *CHANNELS_PTR (if nonnull). The symbol value includes the register
2976 number (RNUM_MASK) and register type (RTYPE_MASK). The channel mask
2977 is a 4-bit value of the form XYZW and is 0 if no suffix was given. */
2978
2979static bfd_boolean
2980mips_parse_register (char **sptr, unsigned int *symval_ptr,
2981 unsigned int *channels_ptr)
2982{
2983 char *s, *e, *m;
2984 const char *q;
2985 unsigned int channels, symval, bit;
2986
2987 /* Find end of name. */
2988 s = e = *sptr;
2989 if (is_name_beginner (*e))
2990 ++e;
2991 while (is_part_of_name (*e))
2992 ++e;
2993
2994 channels = 0;
2995 if (!mips_parse_register_1 (s, e, &symval))
2996 {
2997 if (!channels_ptr)
2998 return FALSE;
2999
3000 /* Eat characters from the end of the string that are valid
3001 channel suffixes. The preceding register must be $ACC or
3002 end with a digit, so there is no ambiguity. */
3003 bit = 1;
3004 m = e;
3005 for (q = "wzyx"; *q; q++, bit <<= 1)
3006 if (m > s && m[-1] == *q)
3007 {
3008 --m;
3009 channels |= bit;
3010 }
3011
3012 if (channels == 0
3013 || !mips_parse_register_1 (s, m, &symval)
3014 || (symval & (RTYPE_VI | RTYPE_VF | RTYPE_R5900_ACC)) == 0)
3015 return FALSE;
3016 }
3017
a92713e6 3018 *sptr = e;
14daeee3
RS
3019 *symval_ptr = symval;
3020 if (channels_ptr)
3021 *channels_ptr = channels;
a92713e6
RS
3022 return TRUE;
3023}
3024
3025/* Check if SPTR points at a valid register specifier according to TYPES.
3026 If so, then return 1, advance S to consume the specifier and store
3027 the register's number in REGNOP, otherwise return 0. */
3028
3029static int
3030reg_lookup (char **s, unsigned int types, unsigned int *regnop)
3031{
3032 unsigned int regno;
3033
14daeee3 3034 if (mips_parse_register (s, &regno, NULL))
707bfff6 3035 {
a92713e6
RS
3036 if (types & RTYPE_VEC)
3037 regno = mips_prefer_vec_regno (regno);
3038 if (regno & types)
3039 regno &= RNUM_MASK;
3040 else
3041 regno = ~0;
707bfff6 3042 }
a92713e6 3043 else
707bfff6 3044 {
a92713e6 3045 if (types & RWARN)
1661c76c 3046 as_warn (_("unrecognized register name `%s'"), *s);
a92713e6 3047 regno = ~0;
707bfff6 3048 }
707bfff6 3049 if (regnop)
a92713e6
RS
3050 *regnop = regno;
3051 return regno <= RNUM_MASK;
707bfff6
TS
3052}
3053
14daeee3
RS
3054/* Parse a VU0 "x?y?z?w?" channel mask at S and store the associated
3055 mask in *CHANNELS. Return a pointer to the first unconsumed character. */
3056
3057static char *
3058mips_parse_vu0_channels (char *s, unsigned int *channels)
3059{
3060 unsigned int i;
3061
3062 *channels = 0;
3063 for (i = 0; i < 4; i++)
3064 if (*s == "xyzw"[i])
3065 {
3066 *channels |= 1 << (3 - i);
3067 ++s;
3068 }
3069 return s;
3070}
3071
a92713e6
RS
3072/* Token types for parsed operand lists. */
3073enum mips_operand_token_type {
3074 /* A plain register, e.g. $f2. */
3075 OT_REG,
df58fc94 3076
14daeee3
RS
3077 /* A 4-bit XYZW channel mask. */
3078 OT_CHANNELS,
3079
56d438b1
CF
3080 /* A constant vector index, e.g. [1]. */
3081 OT_INTEGER_INDEX,
3082
3083 /* A register vector index, e.g. [$2]. */
3084 OT_REG_INDEX,
df58fc94 3085
a92713e6
RS
3086 /* A continuous range of registers, e.g. $s0-$s4. */
3087 OT_REG_RANGE,
3088
3089 /* A (possibly relocated) expression. */
3090 OT_INTEGER,
3091
3092 /* A floating-point value. */
3093 OT_FLOAT,
3094
3095 /* A single character. This can be '(', ')' or ',', but '(' only appears
3096 before OT_REGs. */
3097 OT_CHAR,
3098
14daeee3
RS
3099 /* A doubled character, either "--" or "++". */
3100 OT_DOUBLE_CHAR,
3101
a92713e6
RS
3102 /* The end of the operand list. */
3103 OT_END
3104};
3105
3106/* A parsed operand token. */
3107struct mips_operand_token
3108{
3109 /* The type of token. */
3110 enum mips_operand_token_type type;
3111 union
3112 {
56d438b1 3113 /* The register symbol value for an OT_REG or OT_REG_INDEX. */
a92713e6
RS
3114 unsigned int regno;
3115
14daeee3
RS
3116 /* The 4-bit channel mask for an OT_CHANNEL_SUFFIX. */
3117 unsigned int channels;
3118
56d438b1
CF
3119 /* The integer value of an OT_INTEGER_INDEX. */
3120 addressT index;
a92713e6
RS
3121
3122 /* The two register symbol values involved in an OT_REG_RANGE. */
3123 struct {
3124 unsigned int regno1;
3125 unsigned int regno2;
3126 } reg_range;
3127
3128 /* The value of an OT_INTEGER. The value is represented as an
3129 expression and the relocation operators that were applied to
3130 that expression. The reloc entries are BFD_RELOC_UNUSED if no
3131 relocation operators were used. */
3132 struct {
3133 expressionS value;
3134 bfd_reloc_code_real_type relocs[3];
3135 } integer;
3136
3137 /* The binary data for an OT_FLOAT constant, and the number of bytes
3138 in the constant. */
3139 struct {
3140 unsigned char data[8];
3141 int length;
3142 } flt;
3143
14daeee3 3144 /* The character represented by an OT_CHAR or OT_DOUBLE_CHAR. */
a92713e6
RS
3145 char ch;
3146 } u;
3147};
3148
3149/* An obstack used to construct lists of mips_operand_tokens. */
3150static struct obstack mips_operand_tokens;
3151
3152/* Give TOKEN type TYPE and add it to mips_operand_tokens. */
3153
3154static void
3155mips_add_token (struct mips_operand_token *token,
3156 enum mips_operand_token_type type)
3157{
3158 token->type = type;
3159 obstack_grow (&mips_operand_tokens, token, sizeof (*token));
3160}
3161
3162/* Check whether S is '(' followed by a register name. Add OT_CHAR
3163 and OT_REG tokens for them if so, and return a pointer to the first
3164 unconsumed character. Return null otherwise. */
3165
3166static char *
3167mips_parse_base_start (char *s)
3168{
3169 struct mips_operand_token token;
14daeee3
RS
3170 unsigned int regno, channels;
3171 bfd_boolean decrement_p;
df58fc94 3172
a92713e6
RS
3173 if (*s != '(')
3174 return 0;
3175
3176 ++s;
3177 SKIP_SPACE_TABS (s);
14daeee3
RS
3178
3179 /* Only match "--" as part of a base expression. In other contexts "--X"
3180 is a double negative. */
3181 decrement_p = (s[0] == '-' && s[1] == '-');
3182 if (decrement_p)
3183 {
3184 s += 2;
3185 SKIP_SPACE_TABS (s);
3186 }
3187
3188 /* Allow a channel specifier because that leads to better error messages
3189 than treating something like "$vf0x++" as an expression. */
3190 if (!mips_parse_register (&s, &regno, &channels))
a92713e6
RS
3191 return 0;
3192
3193 token.u.ch = '(';
3194 mips_add_token (&token, OT_CHAR);
3195
14daeee3
RS
3196 if (decrement_p)
3197 {
3198 token.u.ch = '-';
3199 mips_add_token (&token, OT_DOUBLE_CHAR);
3200 }
3201
a92713e6
RS
3202 token.u.regno = regno;
3203 mips_add_token (&token, OT_REG);
3204
14daeee3
RS
3205 if (channels)
3206 {
3207 token.u.channels = channels;
3208 mips_add_token (&token, OT_CHANNELS);
3209 }
3210
3211 /* For consistency, only match "++" as part of base expressions too. */
3212 SKIP_SPACE_TABS (s);
3213 if (s[0] == '+' && s[1] == '+')
3214 {
3215 s += 2;
3216 token.u.ch = '+';
3217 mips_add_token (&token, OT_DOUBLE_CHAR);
3218 }
3219
a92713e6
RS
3220 return s;
3221}
3222
3223/* Parse one or more tokens from S. Return a pointer to the first
3224 unconsumed character on success. Return null if an error was found
3225 and store the error text in insn_error. FLOAT_FORMAT is as for
3226 mips_parse_arguments. */
3227
3228static char *
3229mips_parse_argument_token (char *s, char float_format)
3230{
6d4af3c2
AM
3231 char *end, *save_in;
3232 const char *err;
14daeee3 3233 unsigned int regno1, regno2, channels;
a92713e6
RS
3234 struct mips_operand_token token;
3235
3236 /* First look for "($reg", since we want to treat that as an
3237 OT_CHAR and OT_REG rather than an expression. */
3238 end = mips_parse_base_start (s);
3239 if (end)
3240 return end;
3241
3242 /* Handle other characters that end up as OT_CHARs. */
3243 if (*s == ')' || *s == ',')
3244 {
3245 token.u.ch = *s;
3246 mips_add_token (&token, OT_CHAR);
3247 ++s;
3248 return s;
3249 }
3250
3251 /* Handle tokens that start with a register. */
14daeee3 3252 if (mips_parse_register (&s, &regno1, &channels))
df58fc94 3253 {
14daeee3
RS
3254 if (channels)
3255 {
3256 /* A register and a VU0 channel suffix. */
3257 token.u.regno = regno1;
3258 mips_add_token (&token, OT_REG);
3259
3260 token.u.channels = channels;
3261 mips_add_token (&token, OT_CHANNELS);
3262 return s;
3263 }
3264
a92713e6
RS
3265 SKIP_SPACE_TABS (s);
3266 if (*s == '-')
df58fc94 3267 {
a92713e6
RS
3268 /* A register range. */
3269 ++s;
3270 SKIP_SPACE_TABS (s);
14daeee3 3271 if (!mips_parse_register (&s, &regno2, NULL))
a92713e6 3272 {
1661c76c 3273 set_insn_error (0, _("invalid register range"));
a92713e6
RS
3274 return 0;
3275 }
df58fc94 3276
a92713e6
RS
3277 token.u.reg_range.regno1 = regno1;
3278 token.u.reg_range.regno2 = regno2;
3279 mips_add_token (&token, OT_REG_RANGE);
3280 return s;
3281 }
a92713e6 3282
56d438b1
CF
3283 /* Add the register itself. */
3284 token.u.regno = regno1;
3285 mips_add_token (&token, OT_REG);
3286
3287 /* Check for a vector index. */
3288 if (*s == '[')
3289 {
a92713e6
RS
3290 ++s;
3291 SKIP_SPACE_TABS (s);
56d438b1
CF
3292 if (mips_parse_register (&s, &token.u.regno, NULL))
3293 mips_add_token (&token, OT_REG_INDEX);
3294 else
a92713e6 3295 {
56d438b1
CF
3296 expressionS element;
3297
3298 my_getExpression (&element, s);
3299 if (element.X_op != O_constant)
3300 {
3301 set_insn_error (0, _("vector element must be constant"));
3302 return 0;
3303 }
3304 s = expr_end;
3305 token.u.index = element.X_add_number;
3306 mips_add_token (&token, OT_INTEGER_INDEX);
a92713e6 3307 }
a92713e6
RS
3308 SKIP_SPACE_TABS (s);
3309 if (*s != ']')
3310 {
1661c76c 3311 set_insn_error (0, _("missing `]'"));
a92713e6
RS
3312 return 0;
3313 }
3314 ++s;
df58fc94 3315 }
a92713e6 3316 return s;
df58fc94
RS
3317 }
3318
a92713e6
RS
3319 if (float_format)
3320 {
3321 /* First try to treat expressions as floats. */
3322 save_in = input_line_pointer;
3323 input_line_pointer = s;
3324 err = md_atof (float_format, (char *) token.u.flt.data,
3325 &token.u.flt.length);
3326 end = input_line_pointer;
3327 input_line_pointer = save_in;
3328 if (err && *err)
3329 {
e3de51ce 3330 set_insn_error (0, err);
a92713e6
RS
3331 return 0;
3332 }
3333 if (s != end)
3334 {
3335 mips_add_token (&token, OT_FLOAT);
3336 return end;
3337 }
3338 }
3339
3340 /* Treat everything else as an integer expression. */
3341 token.u.integer.relocs[0] = BFD_RELOC_UNUSED;
3342 token.u.integer.relocs[1] = BFD_RELOC_UNUSED;
3343 token.u.integer.relocs[2] = BFD_RELOC_UNUSED;
3344 my_getSmallExpression (&token.u.integer.value, token.u.integer.relocs, s);
3345 s = expr_end;
3346 mips_add_token (&token, OT_INTEGER);
3347 return s;
3348}
3349
3350/* S points to the operand list for an instruction. FLOAT_FORMAT is 'f'
3351 if expressions should be treated as 32-bit floating-point constants,
3352 'd' if they should be treated as 64-bit floating-point constants,
3353 or 0 if they should be treated as integer expressions (the usual case).
3354
3355 Return a list of tokens on success, otherwise return 0. The caller
3356 must obstack_free the list after use. */
3357
3358static struct mips_operand_token *
3359mips_parse_arguments (char *s, char float_format)
3360{
3361 struct mips_operand_token token;
3362
3363 SKIP_SPACE_TABS (s);
3364 while (*s)
3365 {
3366 s = mips_parse_argument_token (s, float_format);
3367 if (!s)
3368 {
3369 obstack_free (&mips_operand_tokens,
3370 obstack_finish (&mips_operand_tokens));
3371 return 0;
3372 }
3373 SKIP_SPACE_TABS (s);
3374 }
3375 mips_add_token (&token, OT_END);
3376 return (struct mips_operand_token *) obstack_finish (&mips_operand_tokens);
df58fc94
RS
3377}
3378
d301a56b
RS
3379/* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
3380 and architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
037b32b9
AN
3381
3382static bfd_boolean
f79e2745 3383is_opcode_valid (const struct mips_opcode *mo)
037b32b9
AN
3384{
3385 int isa = mips_opts.isa;
846ef2d0 3386 int ase = mips_opts.ase;
037b32b9 3387 int fp_s, fp_d;
c6278170 3388 unsigned int i;
037b32b9 3389
be0fcbee 3390 if (ISA_HAS_64BIT_REGS (isa))
c6278170
RS
3391 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3392 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3393 ase |= mips_ases[i].flags64;
037b32b9 3394
d301a56b 3395 if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
037b32b9
AN
3396 return FALSE;
3397
3398 /* Check whether the instruction or macro requires single-precision or
3399 double-precision floating-point support. Note that this information is
3400 stored differently in the opcode table for insns and macros. */
3401 if (mo->pinfo == INSN_MACRO)
3402 {
3403 fp_s = mo->pinfo2 & INSN2_M_FP_S;
3404 fp_d = mo->pinfo2 & INSN2_M_FP_D;
3405 }
3406 else
3407 {
3408 fp_s = mo->pinfo & FP_S;
3409 fp_d = mo->pinfo & FP_D;
3410 }
3411
3412 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
3413 return FALSE;
3414
3415 if (fp_s && mips_opts.soft_float)
3416 return FALSE;
3417
3418 return TRUE;
3419}
3420
3421/* Return TRUE if the MIPS16 opcode MO is valid on the currently
3422 selected ISA and architecture. */
3423
3424static bfd_boolean
3425is_opcode_valid_16 (const struct mips_opcode *mo)
3426{
25499ac7
MR
3427 int isa = mips_opts.isa;
3428 int ase = mips_opts.ase;
3429 unsigned int i;
3430
3431 if (ISA_HAS_64BIT_REGS (isa))
3432 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3433 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3434 ase |= mips_ases[i].flags64;
3435
3436 return opcode_is_member (mo, isa, ase, mips_opts.arch);
037b32b9
AN
3437}
3438
df58fc94 3439/* Return TRUE if the size of the microMIPS opcode MO matches one
7fd53920
MR
3440 explicitly requested. Always TRUE in the standard MIPS mode.
3441 Use is_size_valid_16 for MIPS16 opcodes. */
df58fc94
RS
3442
3443static bfd_boolean
3444is_size_valid (const struct mips_opcode *mo)
3445{
3446 if (!mips_opts.micromips)
3447 return TRUE;
3448
833794fc
MR
3449 if (mips_opts.insn32)
3450 {
3451 if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
3452 return FALSE;
3453 if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
3454 return FALSE;
3455 }
df58fc94
RS
3456 if (!forced_insn_length)
3457 return TRUE;
3458 if (mo->pinfo == INSN_MACRO)
3459 return FALSE;
3460 return forced_insn_length == micromips_insn_length (mo);
3461}
3462
7fd53920
MR
3463/* Return TRUE if the size of the MIPS16 opcode MO matches one
3464 explicitly requested. */
3465
3466static bfd_boolean
3467is_size_valid_16 (const struct mips_opcode *mo)
3468{
3469 if (!forced_insn_length)
3470 return TRUE;
3471 if (mo->pinfo == INSN_MACRO)
3472 return FALSE;
3473 if (forced_insn_length == 2 && mips_opcode_32bit_p (mo))
3474 return FALSE;
0674ee5d
MR
3475 if (forced_insn_length == 4 && (mo->pinfo2 & INSN2_SHORT_ONLY))
3476 return FALSE;
7fd53920
MR
3477 return TRUE;
3478}
3479
df58fc94 3480/* Return TRUE if the microMIPS opcode MO is valid for the delay slot
e64af278
MR
3481 of the preceding instruction. Always TRUE in the standard MIPS mode.
3482
3483 We don't accept macros in 16-bit delay slots to avoid a case where
3484 a macro expansion fails because it relies on a preceding 32-bit real
3485 instruction to have matched and does not handle the operands correctly.
3486 The only macros that may expand to 16-bit instructions are JAL that
3487 cannot be placed in a delay slot anyway, and corner cases of BALIGN
3488 and BGT (that likewise cannot be placed in a delay slot) that decay to
3489 a NOP. In all these cases the macros precede any corresponding real
3490 instruction definitions in the opcode table, so they will match in the
3491 second pass where the size of the delay slot is ignored and therefore
3492 produce correct code. */
df58fc94
RS
3493
3494static bfd_boolean
3495is_delay_slot_valid (const struct mips_opcode *mo)
3496{
3497 if (!mips_opts.micromips)
3498 return TRUE;
3499
3500 if (mo->pinfo == INSN_MACRO)
c06dec14 3501 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
df58fc94
RS
3502 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
3503 && micromips_insn_length (mo) != 4)
3504 return FALSE;
3505 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
3506 && micromips_insn_length (mo) != 2)
3507 return FALSE;
3508
3509 return TRUE;
3510}
3511
fc76e730
RS
3512/* For consistency checking, verify that all bits of OPCODE are specified
3513 either by the match/mask part of the instruction definition, or by the
3514 operand list. Also build up a list of operands in OPERANDS.
3515
3516 INSN_BITS says which bits of the instruction are significant.
3517 If OPCODE is a standard or microMIPS instruction, DECODE_OPERAND
3518 provides the mips_operand description of each operand. DECODE_OPERAND
3519 is null for MIPS16 instructions. */
ab902481
RS
3520
3521static int
3522validate_mips_insn (const struct mips_opcode *opcode,
3523 unsigned long insn_bits,
fc76e730
RS
3524 const struct mips_operand *(*decode_operand) (const char *),
3525 struct mips_operand_array *operands)
ab902481
RS
3526{
3527 const char *s;
fc76e730 3528 unsigned long used_bits, doubled, undefined, opno, mask;
ab902481
RS
3529 const struct mips_operand *operand;
3530
fc76e730
RS
3531 mask = (opcode->pinfo == INSN_MACRO ? 0 : opcode->mask);
3532 if ((mask & opcode->match) != opcode->match)
ab902481
RS
3533 {
3534 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
3535 opcode->name, opcode->args);
3536 return 0;
3537 }
3538 used_bits = 0;
fc76e730 3539 opno = 0;
14daeee3
RS
3540 if (opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX)
3541 used_bits = mips_insert_operand (&mips_vu0_channel_mask, used_bits, -1);
ab902481
RS
3542 for (s = opcode->args; *s; ++s)
3543 switch (*s)
3544 {
3545 case ',':
3546 case '(':
3547 case ')':
3548 break;
3549
14daeee3
RS
3550 case '#':
3551 s++;
3552 break;
3553
ab902481 3554 default:
fc76e730 3555 if (!decode_operand)
7fd53920 3556 operand = decode_mips16_operand (*s, mips_opcode_32bit_p (opcode));
fc76e730
RS
3557 else
3558 operand = decode_operand (s);
3559 if (!operand && opcode->pinfo != INSN_MACRO)
ab902481
RS
3560 {
3561 as_bad (_("internal: unknown operand type: %s %s"),
3562 opcode->name, opcode->args);
3563 return 0;
3564 }
fc76e730
RS
3565 gas_assert (opno < MAX_OPERANDS);
3566 operands->operand[opno] = operand;
25499ac7
MR
3567 if (!decode_operand && operand
3568 && operand->type == OP_INT && operand->lsb == 0
3569 && mips_opcode_32bit_p (opcode))
3570 used_bits |= mips16_immed_extend (-1, operand->size);
3571 else if (operand && operand->type != OP_VU0_MATCH_SUFFIX)
fc76e730 3572 {
14daeee3 3573 used_bits = mips_insert_operand (operand, used_bits, -1);
fc76e730
RS
3574 if (operand->type == OP_MDMX_IMM_REG)
3575 /* Bit 5 is the format selector (OB vs QH). The opcode table
3576 has separate entries for each format. */
3577 used_bits &= ~(1 << (operand->lsb + 5));
3578 if (operand->type == OP_ENTRY_EXIT_LIST)
3579 used_bits &= ~(mask & 0x700);
38bf472a
MR
3580 /* interAptiv MR2 SAVE/RESTORE instructions have a discontiguous
3581 operand field that cannot be fully described with LSB/SIZE. */
3582 if (operand->type == OP_SAVE_RESTORE_LIST && operand->lsb == 6)
3583 used_bits &= ~0x6000;
fc76e730 3584 }
ab902481 3585 /* Skip prefix characters. */
7361da2c 3586 if (decode_operand && (*s == '+' || *s == 'm' || *s == '-'))
ab902481 3587 ++s;
fc76e730 3588 opno += 1;
ab902481
RS
3589 break;
3590 }
fc76e730 3591 doubled = used_bits & mask & insn_bits;
ab902481
RS
3592 if (doubled)
3593 {
3594 as_bad (_("internal: bad mips opcode (bits 0x%08lx doubly defined):"
3595 " %s %s"), doubled, opcode->name, opcode->args);
3596 return 0;
3597 }
fc76e730 3598 used_bits |= mask;
ab902481 3599 undefined = ~used_bits & insn_bits;
fc76e730 3600 if (opcode->pinfo != INSN_MACRO && undefined)
ab902481
RS
3601 {
3602 as_bad (_("internal: bad mips opcode (bits 0x%08lx undefined): %s %s"),
3603 undefined, opcode->name, opcode->args);
3604 return 0;
3605 }
3606 used_bits &= ~insn_bits;
3607 if (used_bits)
3608 {
3609 as_bad (_("internal: bad mips opcode (bits 0x%08lx defined): %s %s"),
3610 used_bits, opcode->name, opcode->args);
3611 return 0;
3612 }
3613 return 1;
3614}
3615
fc76e730
RS
3616/* The MIPS16 version of validate_mips_insn. */
3617
3618static int
3619validate_mips16_insn (const struct mips_opcode *opcode,
3620 struct mips_operand_array *operands)
3621{
7fd53920 3622 unsigned long insn_bits = mips_opcode_32bit_p (opcode) ? 0xffffffff : 0xffff;
fc76e730 3623
7fd53920 3624 return validate_mips_insn (opcode, insn_bits, 0, operands);
fc76e730
RS
3625}
3626
ab902481
RS
3627/* The microMIPS version of validate_mips_insn. */
3628
3629static int
fc76e730
RS
3630validate_micromips_insn (const struct mips_opcode *opc,
3631 struct mips_operand_array *operands)
ab902481
RS
3632{
3633 unsigned long insn_bits;
3634 unsigned long major;
3635 unsigned int length;
3636
fc76e730
RS
3637 if (opc->pinfo == INSN_MACRO)
3638 return validate_mips_insn (opc, 0xffffffff, decode_micromips_operand,
3639 operands);
3640
ab902481
RS
3641 length = micromips_insn_length (opc);
3642 if (length != 2 && length != 4)
3643 {
1661c76c 3644 as_bad (_("internal error: bad microMIPS opcode (incorrect length: %u): "
ab902481
RS
3645 "%s %s"), length, opc->name, opc->args);
3646 return 0;
3647 }
3648 major = opc->match >> (10 + 8 * (length - 2));
3649 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
3650 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
3651 {
1661c76c 3652 as_bad (_("internal error: bad microMIPS opcode "
ab902481
RS
3653 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
3654 return 0;
3655 }
3656
3657 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
3658 insn_bits = 1 << 4 * length;
3659 insn_bits <<= 4 * length;
3660 insn_bits -= 1;
fc76e730
RS
3661 return validate_mips_insn (opc, insn_bits, decode_micromips_operand,
3662 operands);
ab902481
RS
3663}
3664
707bfff6
TS
3665/* This function is called once, at assembler startup time. It should set up
3666 all the tables, etc. that the MD part of the assembler will need. */
156c2f8b 3667
252b5132 3668void
17a2f251 3669md_begin (void)
252b5132 3670{
156c2f8b 3671 int i = 0;
252b5132 3672 int broken = 0;
1f25f5d3 3673
0a44bf69
RS
3674 if (mips_pic != NO_PIC)
3675 {
3676 if (g_switch_seen && g_switch_value != 0)
3677 as_bad (_("-G may not be used in position-independent code"));
3678 g_switch_value = 0;
3679 }
00acd688
CM
3680 else if (mips_abicalls)
3681 {
3682 if (g_switch_seen && g_switch_value != 0)
3683 as_bad (_("-G may not be used with abicalls"));
3684 g_switch_value = 0;
3685 }
0a44bf69 3686
0b35dfee 3687 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
1661c76c 3688 as_warn (_("could not set architecture and machine"));
252b5132 3689
629310ab 3690 op_hash = str_htab_create ();
252b5132 3691
fc76e730 3692 mips_operands = XCNEWVEC (struct mips_operand_array, NUMOPCODES);
252b5132
RH
3693 for (i = 0; i < NUMOPCODES;)
3694 {
3695 const char *name = mips_opcodes[i].name;
3696
629310ab 3697 str_hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
3698 do
3699 {
fc76e730
RS
3700 if (!validate_mips_insn (&mips_opcodes[i], 0xffffffff,
3701 decode_mips_operand, &mips_operands[i]))
3702 broken = 1;
6f2117ba 3703
fc76e730 3704 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
252b5132 3705 {
fc76e730
RS
3706 create_insn (&nop_insn, mips_opcodes + i);
3707 if (mips_fix_loongson2f_nop)
3708 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
3709 nop_insn.fixed_p = 1;
252b5132 3710 }
6f2117ba
PH
3711
3712 if (sync_insn.insn_mo == NULL && strcmp (name, "sync") == 0)
3713 create_insn (&sync_insn, mips_opcodes + i);
3714
252b5132
RH
3715 ++i;
3716 }
3717 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
3718 }
3719
629310ab 3720 mips16_op_hash = str_htab_create ();
fc76e730
RS
3721 mips16_operands = XCNEWVEC (struct mips_operand_array,
3722 bfd_mips16_num_opcodes);
252b5132
RH
3723
3724 i = 0;
3725 while (i < bfd_mips16_num_opcodes)
3726 {
3727 const char *name = mips16_opcodes[i].name;
3728
629310ab 3729 str_hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
3730 do
3731 {
fc76e730
RS
3732 if (!validate_mips16_insn (&mips16_opcodes[i], &mips16_operands[i]))
3733 broken = 1;
1e915849
RS
3734 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3735 {
3736 create_insn (&mips16_nop_insn, mips16_opcodes + i);
3737 mips16_nop_insn.fixed_p = 1;
3738 }
252b5132
RH
3739 ++i;
3740 }
3741 while (i < bfd_mips16_num_opcodes
3742 && strcmp (mips16_opcodes[i].name, name) == 0);
3743 }
3744
629310ab 3745 micromips_op_hash = str_htab_create ();
fc76e730
RS
3746 micromips_operands = XCNEWVEC (struct mips_operand_array,
3747 bfd_micromips_num_opcodes);
df58fc94
RS
3748
3749 i = 0;
3750 while (i < bfd_micromips_num_opcodes)
3751 {
3752 const char *name = micromips_opcodes[i].name;
3753
629310ab 3754 str_hash_insert (micromips_op_hash, name,
df58fc94 3755 (void *) &micromips_opcodes[i]);
df58fc94 3756 do
fc76e730
RS
3757 {
3758 struct mips_cl_insn *micromips_nop_insn;
3759
3760 if (!validate_micromips_insn (&micromips_opcodes[i],
3761 &micromips_operands[i]))
3762 broken = 1;
3763
3764 if (micromips_opcodes[i].pinfo != INSN_MACRO)
3765 {
3766 if (micromips_insn_length (micromips_opcodes + i) == 2)
3767 micromips_nop_insn = &micromips_nop16_insn;
3768 else if (micromips_insn_length (micromips_opcodes + i) == 4)
3769 micromips_nop_insn = &micromips_nop32_insn;
3770 else
3771 continue;
3772
3773 if (micromips_nop_insn->insn_mo == NULL
3774 && strcmp (name, "nop") == 0)
3775 {
3776 create_insn (micromips_nop_insn, micromips_opcodes + i);
3777 micromips_nop_insn->fixed_p = 1;
3778 }
3779 }
3780 }
df58fc94
RS
3781 while (++i < bfd_micromips_num_opcodes
3782 && strcmp (micromips_opcodes[i].name, name) == 0);
3783 }
3784
252b5132 3785 if (broken)
1661c76c 3786 as_fatal (_("broken assembler, no assembly attempted"));
252b5132
RH
3787
3788 /* We add all the general register names to the symbol table. This
3789 helps us detect invalid uses of them. */
3739860c 3790 for (i = 0; reg_names[i].name; i++)
707bfff6 3791 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
e01e1cee
AM
3792 &zero_address_frag,
3793 reg_names[i].num));
707bfff6 3794 if (HAVE_NEWABI)
3739860c 3795 for (i = 0; reg_names_n32n64[i].name; i++)
707bfff6 3796 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
e01e1cee
AM
3797 &zero_address_frag,
3798 reg_names_n32n64[i].num));
707bfff6 3799 else
3739860c 3800 for (i = 0; reg_names_o32[i].name; i++)
707bfff6 3801 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
e01e1cee
AM
3802 &zero_address_frag,
3803 reg_names_o32[i].num));
6047c971 3804
14daeee3
RS
3805 for (i = 0; i < 32; i++)
3806 {
92fce9bd 3807 char regname[6];
14daeee3
RS
3808
3809 /* R5900 VU0 floating-point register. */
92fce9bd 3810 sprintf (regname, "$vf%d", i);
14daeee3 3811 symbol_table_insert (symbol_new (regname, reg_section,
e01e1cee 3812 &zero_address_frag, RTYPE_VF | i));
14daeee3
RS
3813
3814 /* R5900 VU0 integer register. */
92fce9bd 3815 sprintf (regname, "$vi%d", i);
14daeee3 3816 symbol_table_insert (symbol_new (regname, reg_section,
e01e1cee 3817 &zero_address_frag, RTYPE_VI | i));
14daeee3 3818
56d438b1 3819 /* MSA register. */
92fce9bd 3820 sprintf (regname, "$w%d", i);
56d438b1 3821 symbol_table_insert (symbol_new (regname, reg_section,
e01e1cee 3822 &zero_address_frag, RTYPE_MSA | i));
14daeee3
RS
3823 }
3824
a92713e6
RS
3825 obstack_init (&mips_operand_tokens);
3826
7d10b47d 3827 mips_no_prev_insn ();
252b5132
RH
3828
3829 mips_gprmask = 0;
3830 mips_cprmask[0] = 0;
3831 mips_cprmask[1] = 0;
3832 mips_cprmask[2] = 0;
3833 mips_cprmask[3] = 0;
3834
3835 /* set the default alignment for the text section (2**2) */
3836 record_alignment (text_section, 2);
3837
4d0d148d 3838 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132 3839
f3ded42a
RS
3840 /* On a native system other than VxWorks, sections must be aligned
3841 to 16 byte boundaries. When configured for an embedded ELF
3842 target, we don't bother. */
3843 if (strncmp (TARGET_OS, "elf", 3) != 0
3844 && strncmp (TARGET_OS, "vxworks", 7) != 0)
252b5132 3845 {
fd361982
AM
3846 bfd_set_section_alignment (text_section, 4);
3847 bfd_set_section_alignment (data_section, 4);
3848 bfd_set_section_alignment (bss_section, 4);
f3ded42a 3849 }
252b5132 3850
f3ded42a
RS
3851 /* Create a .reginfo section for register masks and a .mdebug
3852 section for debugging information. */
3853 {
3854 segT seg;
3855 subsegT subseg;
3856 flagword flags;
3857 segT sec;
3858
3859 seg = now_seg;
3860 subseg = now_subseg;
3861
3862 /* The ABI says this section should be loaded so that the
3863 running program can access it. However, we don't load it
6f2117ba 3864 if we are configured for an embedded target. */
f3ded42a
RS
3865 flags = SEC_READONLY | SEC_DATA;
3866 if (strncmp (TARGET_OS, "elf", 3) != 0)
3867 flags |= SEC_ALLOC | SEC_LOAD;
3868
3869 if (mips_abi != N64_ABI)
252b5132 3870 {
f3ded42a 3871 sec = subseg_new (".reginfo", (subsegT) 0);
bdaaa2e1 3872
fd361982
AM
3873 bfd_set_section_flags (sec, flags);
3874 bfd_set_section_alignment (sec, HAVE_NEWABI ? 3 : 2);
252b5132 3875
f3ded42a
RS
3876 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
3877 }
3878 else
3879 {
3880 /* The 64-bit ABI uses a .MIPS.options section rather than
3881 .reginfo section. */
3882 sec = subseg_new (".MIPS.options", (subsegT) 0);
fd361982
AM
3883 bfd_set_section_flags (sec, flags);
3884 bfd_set_section_alignment (sec, 3);
252b5132 3885
f3ded42a
RS
3886 /* Set up the option header. */
3887 {
3888 Elf_Internal_Options opthdr;
3889 char *f;
3890
3891 opthdr.kind = ODK_REGINFO;
3892 opthdr.size = (sizeof (Elf_External_Options)
3893 + sizeof (Elf64_External_RegInfo));
3894 opthdr.section = 0;
3895 opthdr.info = 0;
3896 f = frag_more (sizeof (Elf_External_Options));
3897 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
3898 (Elf_External_Options *) f);
3899
3900 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
3901 }
3902 }
252b5132 3903
351cdf24 3904 sec = subseg_new (".MIPS.abiflags", (subsegT) 0);
fd361982 3905 bfd_set_section_flags (sec,
351cdf24 3906 SEC_READONLY | SEC_DATA | SEC_ALLOC | SEC_LOAD);
fd361982 3907 bfd_set_section_alignment (sec, 3);
351cdf24
MF
3908 mips_flags_frag = frag_more (sizeof (Elf_External_ABIFlags_v0));
3909
f3ded42a
RS
3910 if (ECOFF_DEBUGGING)
3911 {
3912 sec = subseg_new (".mdebug", (subsegT) 0);
fd361982
AM
3913 bfd_set_section_flags (sec, SEC_HAS_CONTENTS | SEC_READONLY);
3914 bfd_set_section_alignment (sec, 2);
252b5132 3915 }
f3ded42a
RS
3916 else if (mips_flag_pdr)
3917 {
3918 pdr_seg = subseg_new (".pdr", (subsegT) 0);
fd361982
AM
3919 bfd_set_section_flags (pdr_seg,
3920 SEC_READONLY | SEC_RELOC | SEC_DEBUGGING);
3921 bfd_set_section_alignment (pdr_seg, 2);
f3ded42a
RS
3922 }
3923
3924 subseg_set (seg, subseg);
3925 }
252b5132 3926
71400594
RS
3927 if (mips_fix_vr4120)
3928 init_vr4120_conflicts ();
252b5132
RH
3929}
3930
351cdf24
MF
3931static inline void
3932fpabi_incompatible_with (int fpabi, const char *what)
3933{
3934 as_warn (_(".gnu_attribute %d,%d is incompatible with `%s'"),
3935 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3936}
3937
3938static inline void
3939fpabi_requires (int fpabi, const char *what)
3940{
3941 as_warn (_(".gnu_attribute %d,%d requires `%s'"),
3942 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3943}
3944
3945/* Check -mabi and register sizes against the specified FP ABI. */
3946static void
3947check_fpabi (int fpabi)
3948{
351cdf24
MF
3949 switch (fpabi)
3950 {
3951 case Val_GNU_MIPS_ABI_FP_DOUBLE:
ea79f94a
MF
3952 if (file_mips_opts.soft_float)
3953 fpabi_incompatible_with (fpabi, "softfloat");
3954 else if (file_mips_opts.single_float)
3955 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3956 if (file_mips_opts.gp == 64 && file_mips_opts.fp == 32)
3957 fpabi_incompatible_with (fpabi, "gp=64 fp=32");
3958 else if (file_mips_opts.gp == 32 && file_mips_opts.fp == 64)
3959 fpabi_incompatible_with (fpabi, "gp=32 fp=64");
351cdf24
MF
3960 break;
3961
3962 case Val_GNU_MIPS_ABI_FP_XX:
3963 if (mips_abi != O32_ABI)
3964 fpabi_requires (fpabi, "-mabi=32");
ea79f94a
MF
3965 else if (file_mips_opts.soft_float)
3966 fpabi_incompatible_with (fpabi, "softfloat");
3967 else if (file_mips_opts.single_float)
3968 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3969 else if (file_mips_opts.fp != 0)
3970 fpabi_requires (fpabi, "fp=xx");
351cdf24
MF
3971 break;
3972
3973 case Val_GNU_MIPS_ABI_FP_64A:
3974 case Val_GNU_MIPS_ABI_FP_64:
3975 if (mips_abi != O32_ABI)
3976 fpabi_requires (fpabi, "-mabi=32");
ea79f94a
MF
3977 else if (file_mips_opts.soft_float)
3978 fpabi_incompatible_with (fpabi, "softfloat");
3979 else if (file_mips_opts.single_float)
3980 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3981 else if (file_mips_opts.fp != 64)
3982 fpabi_requires (fpabi, "fp=64");
3983 else if (fpabi == Val_GNU_MIPS_ABI_FP_64 && !file_mips_opts.oddspreg)
3984 fpabi_incompatible_with (fpabi, "nooddspreg");
3985 else if (fpabi == Val_GNU_MIPS_ABI_FP_64A && file_mips_opts.oddspreg)
3986 fpabi_requires (fpabi, "nooddspreg");
351cdf24
MF
3987 break;
3988
3989 case Val_GNU_MIPS_ABI_FP_SINGLE:
3990 if (file_mips_opts.soft_float)
3991 fpabi_incompatible_with (fpabi, "softfloat");
3992 else if (!file_mips_opts.single_float)
3993 fpabi_requires (fpabi, "singlefloat");
3994 break;
3995
3996 case Val_GNU_MIPS_ABI_FP_SOFT:
3997 if (!file_mips_opts.soft_float)
3998 fpabi_requires (fpabi, "softfloat");
3999 break;
4000
4001 case Val_GNU_MIPS_ABI_FP_OLD_64:
4002 as_warn (_(".gnu_attribute %d,%d is no longer supported"),
4003 Tag_GNU_MIPS_ABI_FP, fpabi);
4004 break;
4005
3350cc01
CM
4006 case Val_GNU_MIPS_ABI_FP_NAN2008:
4007 /* Silently ignore compatibility value. */
4008 break;
4009
351cdf24
MF
4010 default:
4011 as_warn (_(".gnu_attribute %d,%d is not a recognized"
4012 " floating-point ABI"), Tag_GNU_MIPS_ABI_FP, fpabi);
4013 break;
4014 }
351cdf24
MF
4015}
4016
919731af 4017/* Perform consistency checks on the current options. */
4018
4019static void
4020mips_check_options (struct mips_set_options *opts, bfd_boolean abi_checks)
4021{
4022 /* Check the size of integer registers agrees with the ABI and ISA. */
4023 if (opts->gp == 64 && !ISA_HAS_64BIT_REGS (opts->isa))
4024 as_bad (_("`gp=64' used with a 32-bit processor"));
4025 else if (abi_checks
4026 && opts->gp == 32 && ABI_NEEDS_64BIT_REGS (mips_abi))
4027 as_bad (_("`gp=32' used with a 64-bit ABI"));
4028 else if (abi_checks
4029 && opts->gp == 64 && ABI_NEEDS_32BIT_REGS (mips_abi))
4030 as_bad (_("`gp=64' used with a 32-bit ABI"));
4031
4032 /* Check the size of the float registers agrees with the ABI and ISA. */
4033 switch (opts->fp)
4034 {
351cdf24
MF
4035 case 0:
4036 if (!CPU_HAS_LDC1_SDC1 (opts->arch))
4037 as_bad (_("`fp=xx' used with a cpu lacking ldc1/sdc1 instructions"));
4038 else if (opts->single_float == 1)
4039 as_bad (_("`fp=xx' cannot be used with `singlefloat'"));
4040 break;
919731af 4041 case 64:
4042 if (!ISA_HAS_64BIT_FPRS (opts->isa))
4043 as_bad (_("`fp=64' used with a 32-bit fpu"));
4044 else if (abi_checks
4045 && ABI_NEEDS_32BIT_REGS (mips_abi)
4046 && !ISA_HAS_MXHC1 (opts->isa))
4047 as_warn (_("`fp=64' used with a 32-bit ABI"));
4048 break;
4049 case 32:
4050 if (abi_checks
4051 && ABI_NEEDS_64BIT_REGS (mips_abi))
4052 as_warn (_("`fp=32' used with a 64-bit ABI"));
5f4678bb 4053 if (ISA_IS_R6 (opts->isa) && opts->single_float == 0)
7361da2c 4054 as_bad (_("`fp=32' used with a MIPS R6 cpu"));
919731af 4055 break;
4056 default:
4057 as_bad (_("Unknown size of floating point registers"));
4058 break;
4059 }
4060
351cdf24
MF
4061 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !opts->oddspreg)
4062 as_bad (_("`nooddspreg` cannot be used with a 64-bit ABI"));
4063
919731af 4064 if (opts->micromips == 1 && opts->mips16 == 1)
1357373c 4065 as_bad (_("`%s' cannot be used with `%s'"), "mips16", "micromips");
5f4678bb 4066 else if (ISA_IS_R6 (opts->isa)
7361da2c
AB
4067 && (opts->micromips == 1
4068 || opts->mips16 == 1))
1357373c 4069 as_fatal (_("`%s' cannot be used with `%s'"),
7361da2c 4070 opts->micromips ? "micromips" : "mips16",
5f4678bb 4071 mips_cpu_info_from_isa (opts->isa)->name);
7361da2c
AB
4072
4073 if (ISA_IS_R6 (opts->isa) && mips_relax_branch)
4074 as_fatal (_("branch relaxation is not supported in `%s'"),
4075 mips_cpu_info_from_isa (opts->isa)->name);
919731af 4076}
4077
4078/* Perform consistency checks on the module level options exactly once.
4079 This is a deferred check that happens:
4080 at the first .set directive
4081 or, at the first pseudo op that generates code (inc .dc.a)
4082 or, at the first instruction
4083 or, at the end. */
4084
4085static void
4086file_mips_check_options (void)
4087{
919731af 4088 if (file_mips_opts_checked)
4089 return;
4090
4091 /* The following code determines the register size.
4092 Similar code was added to GCC 3.3 (see override_options() in
4093 config/mips/mips.c). The GAS and GCC code should be kept in sync
4094 as much as possible. */
4095
4096 if (file_mips_opts.gp < 0)
4097 {
4098 /* Infer the integer register size from the ABI and processor.
4099 Restrict ourselves to 32-bit registers if that's all the
4100 processor has, or if the ABI cannot handle 64-bit registers. */
4101 file_mips_opts.gp = (ABI_NEEDS_32BIT_REGS (mips_abi)
4102 || !ISA_HAS_64BIT_REGS (file_mips_opts.isa))
4103 ? 32 : 64;
4104 }
4105
4106 if (file_mips_opts.fp < 0)
4107 {
4108 /* No user specified float register size.
4109 ??? GAS treats single-float processors as though they had 64-bit
4110 float registers (although it complains when double-precision
4111 instructions are used). As things stand, saying they have 32-bit
4112 registers would lead to spurious "register must be even" messages.
4113 So here we assume float registers are never smaller than the
4114 integer ones. */
4115 if (file_mips_opts.gp == 64)
4116 /* 64-bit integer registers implies 64-bit float registers. */
4117 file_mips_opts.fp = 64;
4118 else if ((file_mips_opts.ase & FP64_ASES)
4119 && ISA_HAS_64BIT_FPRS (file_mips_opts.isa))
4120 /* Handle ASEs that require 64-bit float registers, if possible. */
4121 file_mips_opts.fp = 64;
7361da2c
AB
4122 else if (ISA_IS_R6 (mips_opts.isa))
4123 /* R6 implies 64-bit float registers. */
4124 file_mips_opts.fp = 64;
919731af 4125 else
4126 /* 32-bit float registers. */
4127 file_mips_opts.fp = 32;
4128 }
4129
351cdf24
MF
4130 /* Disable operations on odd-numbered floating-point registers by default
4131 when using the FPXX ABI. */
4132 if (file_mips_opts.oddspreg < 0)
4133 {
4134 if (file_mips_opts.fp == 0)
4135 file_mips_opts.oddspreg = 0;
4136 else
4137 file_mips_opts.oddspreg = 1;
4138 }
4139
919731af 4140 /* End of GCC-shared inference code. */
4141
4142 /* This flag is set when we have a 64-bit capable CPU but use only
4143 32-bit wide registers. Note that EABI does not use it. */
4144 if (ISA_HAS_64BIT_REGS (file_mips_opts.isa)
4145 && ((mips_abi == NO_ABI && file_mips_opts.gp == 32)
4146 || mips_abi == O32_ABI))
4147 mips_32bitmode = 1;
4148
4149 if (file_mips_opts.isa == ISA_MIPS1 && mips_trap)
4150 as_bad (_("trap exception not supported at ISA 1"));
4151
4152 /* If the selected architecture includes support for ASEs, enable
4153 generation of code for them. */
4154 if (file_mips_opts.mips16 == -1)
4155 file_mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_opts.arch)) ? 1 : 0;
4156 if (file_mips_opts.micromips == -1)
4157 file_mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_opts.arch))
4158 ? 1 : 0;
4159
7361da2c
AB
4160 if (mips_nan2008 == -1)
4161 mips_nan2008 = (ISA_HAS_LEGACY_NAN (file_mips_opts.isa)) ? 0 : 1;
4162 else if (!ISA_HAS_LEGACY_NAN (file_mips_opts.isa) && mips_nan2008 == 0)
4163 as_fatal (_("`%s' does not support legacy NaN"),
4164 mips_cpu_info_from_arch (file_mips_opts.arch)->name);
4165
919731af 4166 /* Some ASEs require 64-bit FPRs, so -mfp32 should stop those ASEs from
4167 being selected implicitly. */
4168 if (file_mips_opts.fp != 64)
4169 file_ase_explicit |= ASE_MIPS3D | ASE_MDMX | ASE_MSA;
4170
4171 /* If the user didn't explicitly select or deselect a particular ASE,
4172 use the default setting for the CPU. */
3315614d 4173 file_mips_opts.ase |= (file_mips_opts.init_ase & ~file_ase_explicit);
919731af 4174
4175 /* Set up the current options. These may change throughout assembly. */
4176 mips_opts = file_mips_opts;
4177
4178 mips_check_isa_supports_ases ();
4179 mips_check_options (&file_mips_opts, TRUE);
4180 file_mips_opts_checked = TRUE;
4181
4182 if (!bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
4183 as_warn (_("could not set architecture and machine"));
4184}
4185
252b5132 4186void
17a2f251 4187md_assemble (char *str)
252b5132
RH
4188{
4189 struct mips_cl_insn insn;
f6688943
TS
4190 bfd_reloc_code_real_type unused_reloc[3]
4191 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 4192
919731af 4193 file_mips_check_options ();
4194
252b5132 4195 imm_expr.X_op = O_absent;
252b5132 4196 offset_expr.X_op = O_absent;
f6688943
TS
4197 offset_reloc[0] = BFD_RELOC_UNUSED;
4198 offset_reloc[1] = BFD_RELOC_UNUSED;
4199 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132 4200
e1b47bd5
RS
4201 mips_mark_labels ();
4202 mips_assembling_insn = TRUE;
e3de51ce 4203 clear_insn_error ();
e1b47bd5 4204
252b5132
RH
4205 if (mips_opts.mips16)
4206 mips16_ip (str, &insn);
4207 else
4208 {
4209 mips_ip (str, &insn);
beae10d5
KH
4210 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
4211 str, insn.insn_opcode));
252b5132
RH
4212 }
4213
e3de51ce
RS
4214 if (insn_error.msg)
4215 report_insn_error (str);
e1b47bd5 4216 else if (insn.insn_mo->pinfo == INSN_MACRO)
252b5132 4217 {
584892a6 4218 macro_start ();
252b5132
RH
4219 if (mips_opts.mips16)
4220 mips16_macro (&insn);
4221 else
833794fc 4222 macro (&insn, str);
584892a6 4223 macro_end ();
252b5132
RH
4224 }
4225 else
4226 {
77bd4346 4227 if (offset_expr.X_op != O_absent)
df58fc94 4228 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
252b5132 4229 else
df58fc94 4230 append_insn (&insn, NULL, unused_reloc, FALSE);
252b5132 4231 }
e1b47bd5
RS
4232
4233 mips_assembling_insn = FALSE;
252b5132
RH
4234}
4235
738e5348
RS
4236/* Convenience functions for abstracting away the differences between
4237 MIPS16 and non-MIPS16 relocations. */
4238
4239static inline bfd_boolean
4240mips16_reloc_p (bfd_reloc_code_real_type reloc)
4241{
4242 switch (reloc)
4243 {
4244 case BFD_RELOC_MIPS16_JMP:
4245 case BFD_RELOC_MIPS16_GPREL:
4246 case BFD_RELOC_MIPS16_GOT16:
4247 case BFD_RELOC_MIPS16_CALL16:
4248 case BFD_RELOC_MIPS16_HI16_S:
4249 case BFD_RELOC_MIPS16_HI16:
4250 case BFD_RELOC_MIPS16_LO16:
c9775dde 4251 case BFD_RELOC_MIPS16_16_PCREL_S1:
738e5348
RS
4252 return TRUE;
4253
4254 default:
4255 return FALSE;
4256 }
4257}
4258
df58fc94
RS
4259static inline bfd_boolean
4260micromips_reloc_p (bfd_reloc_code_real_type reloc)
4261{
4262 switch (reloc)
4263 {
4264 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4265 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4266 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4267 case BFD_RELOC_MICROMIPS_GPREL16:
4268 case BFD_RELOC_MICROMIPS_JMP:
4269 case BFD_RELOC_MICROMIPS_HI16:
4270 case BFD_RELOC_MICROMIPS_HI16_S:
4271 case BFD_RELOC_MICROMIPS_LO16:
4272 case BFD_RELOC_MICROMIPS_LITERAL:
4273 case BFD_RELOC_MICROMIPS_GOT16:
4274 case BFD_RELOC_MICROMIPS_CALL16:
4275 case BFD_RELOC_MICROMIPS_GOT_HI16:
4276 case BFD_RELOC_MICROMIPS_GOT_LO16:
4277 case BFD_RELOC_MICROMIPS_CALL_HI16:
4278 case BFD_RELOC_MICROMIPS_CALL_LO16:
4279 case BFD_RELOC_MICROMIPS_SUB:
4280 case BFD_RELOC_MICROMIPS_GOT_PAGE:
4281 case BFD_RELOC_MICROMIPS_GOT_OFST:
4282 case BFD_RELOC_MICROMIPS_GOT_DISP:
4283 case BFD_RELOC_MICROMIPS_HIGHEST:
4284 case BFD_RELOC_MICROMIPS_HIGHER:
4285 case BFD_RELOC_MICROMIPS_SCN_DISP:
4286 case BFD_RELOC_MICROMIPS_JALR:
4287 return TRUE;
4288
4289 default:
4290 return FALSE;
4291 }
4292}
4293
2309ddf2
MR
4294static inline bfd_boolean
4295jmp_reloc_p (bfd_reloc_code_real_type reloc)
4296{
4297 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
4298}
4299
0e9c5a5c
MR
4300static inline bfd_boolean
4301b_reloc_p (bfd_reloc_code_real_type reloc)
4302{
4303 return (reloc == BFD_RELOC_MIPS_26_PCREL_S2
4304 || reloc == BFD_RELOC_MIPS_21_PCREL_S2
4305 || reloc == BFD_RELOC_16_PCREL_S2
c9775dde 4306 || reloc == BFD_RELOC_MIPS16_16_PCREL_S1
0e9c5a5c
MR
4307 || reloc == BFD_RELOC_MICROMIPS_16_PCREL_S1
4308 || reloc == BFD_RELOC_MICROMIPS_10_PCREL_S1
4309 || reloc == BFD_RELOC_MICROMIPS_7_PCREL_S1);
4310}
4311
738e5348
RS
4312static inline bfd_boolean
4313got16_reloc_p (bfd_reloc_code_real_type reloc)
4314{
2309ddf2 4315 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
df58fc94 4316 || reloc == BFD_RELOC_MICROMIPS_GOT16);
738e5348
RS
4317}
4318
4319static inline bfd_boolean
4320hi16_reloc_p (bfd_reloc_code_real_type reloc)
4321{
2309ddf2 4322 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
df58fc94 4323 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
738e5348
RS
4324}
4325
4326static inline bfd_boolean
4327lo16_reloc_p (bfd_reloc_code_real_type reloc)
4328{
2309ddf2 4329 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
df58fc94
RS
4330 || reloc == BFD_RELOC_MICROMIPS_LO16);
4331}
4332
df58fc94
RS
4333static inline bfd_boolean
4334jalr_reloc_p (bfd_reloc_code_real_type reloc)
4335{
2309ddf2 4336 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
738e5348
RS
4337}
4338
f2ae14a1
RS
4339static inline bfd_boolean
4340gprel16_reloc_p (bfd_reloc_code_real_type reloc)
4341{
4342 return (reloc == BFD_RELOC_GPREL16 || reloc == BFD_RELOC_MIPS16_GPREL
4343 || reloc == BFD_RELOC_MICROMIPS_GPREL16);
4344}
4345
2de39019
CM
4346/* Return true if RELOC is a PC-relative relocation that does not have
4347 full address range. */
4348
4349static inline bfd_boolean
4350limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
4351{
4352 switch (reloc)
4353 {
4354 case BFD_RELOC_16_PCREL_S2:
c9775dde 4355 case BFD_RELOC_MIPS16_16_PCREL_S1:
2de39019
CM
4356 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4357 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4358 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
7361da2c
AB
4359 case BFD_RELOC_MIPS_21_PCREL_S2:
4360 case BFD_RELOC_MIPS_26_PCREL_S2:
4361 case BFD_RELOC_MIPS_18_PCREL_S3:
4362 case BFD_RELOC_MIPS_19_PCREL_S2:
2de39019
CM
4363 return TRUE;
4364
b47468a6 4365 case BFD_RELOC_32_PCREL:
7361da2c
AB
4366 case BFD_RELOC_HI16_S_PCREL:
4367 case BFD_RELOC_LO16_PCREL:
b47468a6
CM
4368 return HAVE_64BIT_ADDRESSES;
4369
2de39019
CM
4370 default:
4371 return FALSE;
4372 }
4373}
b47468a6 4374
5919d012 4375/* Return true if the given relocation might need a matching %lo().
0a44bf69
RS
4376 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
4377 need a matching %lo() when applied to local symbols. */
5919d012
RS
4378
4379static inline bfd_boolean
17a2f251 4380reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 4381{
3b91255e 4382 return (HAVE_IN_PLACE_ADDENDS
738e5348 4383 && (hi16_reloc_p (reloc)
0a44bf69
RS
4384 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
4385 all GOT16 relocations evaluate to "G". */
738e5348
RS
4386 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
4387}
4388
4389/* Return the type of %lo() reloc needed by RELOC, given that
4390 reloc_needs_lo_p. */
4391
4392static inline bfd_reloc_code_real_type
4393matching_lo_reloc (bfd_reloc_code_real_type reloc)
4394{
df58fc94
RS
4395 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
4396 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
4397 : BFD_RELOC_LO16));
5919d012
RS
4398}
4399
4400/* Return true if the given fixup is followed by a matching R_MIPS_LO16
4401 relocation. */
4402
4403static inline bfd_boolean
17a2f251 4404fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
4405{
4406 return (fixp->fx_next != NULL
738e5348 4407 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
5919d012
RS
4408 && fixp->fx_addsy == fixp->fx_next->fx_addsy
4409 && fixp->fx_offset == fixp->fx_next->fx_offset);
4410}
4411
462427c4
RS
4412/* Move all labels in LABELS to the current insertion point. TEXT_P
4413 says whether the labels refer to text or data. */
404a8071
RS
4414
4415static void
462427c4 4416mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
404a8071
RS
4417{
4418 struct insn_label_list *l;
4419 valueT val;
4420
462427c4 4421 for (l = labels; l != NULL; l = l->next)
404a8071 4422 {
9c2799c2 4423 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
404a8071
RS
4424 symbol_set_frag (l->label, frag_now);
4425 val = (valueT) frag_now_fix ();
770c0151
FS
4426 /* MIPS16/microMIPS text labels are stored as odd.
4427 We just carry the ISA mode bit forward. */
462427c4 4428 if (text_p && HAVE_CODE_COMPRESSION)
770c0151 4429 val |= (S_GET_VALUE (l->label) & 0x1);
404a8071
RS
4430 S_SET_VALUE (l->label, val);
4431 }
4432}
4433
462427c4
RS
4434/* Move all labels in insn_labels to the current insertion point
4435 and treat them as text labels. */
4436
4437static void
4438mips_move_text_labels (void)
4439{
4440 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
4441}
4442
9e009953
MR
4443/* Duplicate the test for LINK_ONCE sections as in `adjust_reloc_syms'. */
4444
5f0fe04b
TS
4445static bfd_boolean
4446s_is_linkonce (symbolS *sym, segT from_seg)
4447{
4448 bfd_boolean linkonce = FALSE;
4449 segT symseg = S_GET_SEGMENT (sym);
4450
4451 if (symseg != from_seg && !S_IS_LOCAL (sym))
4452 {
fd361982 4453 if ((bfd_section_flags (symseg) & SEC_LINK_ONCE))
5f0fe04b 4454 linkonce = TRUE;
5f0fe04b
TS
4455 /* The GNU toolchain uses an extension for ELF: a section
4456 beginning with the magic string .gnu.linkonce is a
4457 linkonce section. */
4458 if (strncmp (segment_name (symseg), ".gnu.linkonce",
4459 sizeof ".gnu.linkonce" - 1) == 0)
4460 linkonce = TRUE;
5f0fe04b
TS
4461 }
4462 return linkonce;
4463}
4464
e1b47bd5 4465/* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
df58fc94
RS
4466 linker to handle them specially, such as generating jalx instructions
4467 when needed. We also make them odd for the duration of the assembly,
4468 in order to generate the right sort of code. We will make them even
252b5132
RH
4469 in the adjust_symtab routine, while leaving them marked. This is
4470 convenient for the debugger and the disassembler. The linker knows
4471 to make them odd again. */
4472
4473static void
e1b47bd5 4474mips_compressed_mark_label (symbolS *label)
252b5132 4475{
df58fc94 4476 gas_assert (HAVE_CODE_COMPRESSION);
a8dbcb85 4477
f3ded42a
RS
4478 if (mips_opts.mips16)
4479 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
4480 else
4481 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
e1b47bd5
RS
4482 if ((S_GET_VALUE (label) & 1) == 0
4483 /* Don't adjust the address if the label is global or weak, or
4484 in a link-once section, since we'll be emitting symbol reloc
4485 references to it which will be patched up by the linker, and
4486 the final value of the symbol may or may not be MIPS16/microMIPS. */
4487 && !S_IS_WEAK (label)
4488 && !S_IS_EXTERNAL (label)
4489 && !s_is_linkonce (label, now_seg))
4490 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
4491}
4492
4493/* Mark preceding MIPS16 or microMIPS instruction labels. */
4494
4495static void
4496mips_compressed_mark_labels (void)
4497{
4498 struct insn_label_list *l;
4499
4500 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
4501 mips_compressed_mark_label (l->label);
252b5132
RH
4502}
4503
4d7206a2
RS
4504/* End the current frag. Make it a variant frag and record the
4505 relaxation info. */
4506
4507static void
4508relax_close_frag (void)
4509{
584892a6 4510 mips_macro_warning.first_frag = frag_now;
4d7206a2 4511 frag_var (rs_machine_dependent, 0, 0,
ce8ad872
MR
4512 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1],
4513 mips_pic != NO_PIC),
4d7206a2
RS
4514 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
4515
4516 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
4517 mips_relax.first_fixup = 0;
4518}
4519
4520/* Start a new relaxation sequence whose expansion depends on SYMBOL.
4521 See the comment above RELAX_ENCODE for more details. */
4522
4523static void
4524relax_start (symbolS *symbol)
4525{
9c2799c2 4526 gas_assert (mips_relax.sequence == 0);
4d7206a2
RS
4527 mips_relax.sequence = 1;
4528 mips_relax.symbol = symbol;
4529}
4530
4531/* Start generating the second version of a relaxable sequence.
4532 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
4533
4534static void
4d7206a2
RS
4535relax_switch (void)
4536{
9c2799c2 4537 gas_assert (mips_relax.sequence == 1);
4d7206a2
RS
4538 mips_relax.sequence = 2;
4539}
4540
4541/* End the current relaxable sequence. */
4542
4543static void
4544relax_end (void)
4545{
9c2799c2 4546 gas_assert (mips_relax.sequence == 2);
4d7206a2
RS
4547 relax_close_frag ();
4548 mips_relax.sequence = 0;
4549}
4550
11625dd8
RS
4551/* Return true if IP is a delayed branch or jump. */
4552
4553static inline bfd_boolean
4554delayed_branch_p (const struct mips_cl_insn *ip)
4555{
4556 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
4557 | INSN_COND_BRANCH_DELAY
4558 | INSN_COND_BRANCH_LIKELY)) != 0;
4559}
4560
4561/* Return true if IP is a compact branch or jump. */
4562
4563static inline bfd_boolean
4564compact_branch_p (const struct mips_cl_insn *ip)
4565{
26545944
RS
4566 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
4567 | INSN2_COND_BRANCH)) != 0;
11625dd8
RS
4568}
4569
4570/* Return true if IP is an unconditional branch or jump. */
4571
4572static inline bfd_boolean
4573uncond_branch_p (const struct mips_cl_insn *ip)
4574{
4575 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
26545944 4576 || (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0);
11625dd8
RS
4577}
4578
4579/* Return true if IP is a branch-likely instruction. */
4580
4581static inline bfd_boolean
4582branch_likely_p (const struct mips_cl_insn *ip)
4583{
4584 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
4585}
4586
14fe068b
RS
4587/* Return the type of nop that should be used to fill the delay slot
4588 of delayed branch IP. */
4589
4590static struct mips_cl_insn *
4591get_delay_slot_nop (const struct mips_cl_insn *ip)
4592{
4593 if (mips_opts.micromips
4594 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
4595 return &micromips_nop32_insn;
4596 return NOP_INSN;
4597}
4598
fc76e730
RS
4599/* Return a mask that has bit N set if OPCODE reads the register(s)
4600 in operand N. */
df58fc94
RS
4601
4602static unsigned int
fc76e730 4603insn_read_mask (const struct mips_opcode *opcode)
df58fc94 4604{
fc76e730
RS
4605 return (opcode->pinfo & INSN_READ_ALL) >> INSN_READ_SHIFT;
4606}
df58fc94 4607
fc76e730
RS
4608/* Return a mask that has bit N set if OPCODE writes to the register(s)
4609 in operand N. */
4610
4611static unsigned int
4612insn_write_mask (const struct mips_opcode *opcode)
4613{
4614 return (opcode->pinfo & INSN_WRITE_ALL) >> INSN_WRITE_SHIFT;
4615}
4616
4617/* Return a mask of the registers specified by operand OPERAND of INSN.
4618 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4619 is set. */
4620
4621static unsigned int
4622operand_reg_mask (const struct mips_cl_insn *insn,
4623 const struct mips_operand *operand,
4624 unsigned int type_mask)
4625{
4626 unsigned int uval, vsel;
4627
4628 switch (operand->type)
df58fc94 4629 {
fc76e730
RS
4630 case OP_INT:
4631 case OP_MAPPED_INT:
4632 case OP_MSB:
4633 case OP_PCREL:
4634 case OP_PERF_REG:
4635 case OP_ADDIUSP_INT:
4636 case OP_ENTRY_EXIT_LIST:
4637 case OP_REPEAT_DEST_REG:
4638 case OP_REPEAT_PREV_REG:
4639 case OP_PC:
14daeee3
RS
4640 case OP_VU0_SUFFIX:
4641 case OP_VU0_MATCH_SUFFIX:
56d438b1 4642 case OP_IMM_INDEX:
fc76e730
RS
4643 abort ();
4644
25499ac7
MR
4645 case OP_REG28:
4646 return 1 << 28;
4647
fc76e730 4648 case OP_REG:
0f35dbc4 4649 case OP_OPTIONAL_REG:
fc76e730
RS
4650 {
4651 const struct mips_reg_operand *reg_op;
4652
4653 reg_op = (const struct mips_reg_operand *) operand;
4654 if (!(type_mask & (1 << reg_op->reg_type)))
4655 return 0;
4656 uval = insn_extract_operand (insn, operand);
4657 return 1 << mips_decode_reg_operand (reg_op, uval);
4658 }
4659
4660 case OP_REG_PAIR:
4661 {
4662 const struct mips_reg_pair_operand *pair_op;
4663
4664 pair_op = (const struct mips_reg_pair_operand *) operand;
4665 if (!(type_mask & (1 << pair_op->reg_type)))
4666 return 0;
4667 uval = insn_extract_operand (insn, operand);
4668 return (1 << pair_op->reg1_map[uval]) | (1 << pair_op->reg2_map[uval]);
4669 }
4670
4671 case OP_CLO_CLZ_DEST:
4672 if (!(type_mask & (1 << OP_REG_GP)))
4673 return 0;
4674 uval = insn_extract_operand (insn, operand);
4675 return (1 << (uval & 31)) | (1 << (uval >> 5));
4676
7361da2c
AB
4677 case OP_SAME_RS_RT:
4678 if (!(type_mask & (1 << OP_REG_GP)))
4679 return 0;
4680 uval = insn_extract_operand (insn, operand);
4681 gas_assert ((uval & 31) == (uval >> 5));
4682 return 1 << (uval & 31);
4683
4684 case OP_CHECK_PREV:
4685 case OP_NON_ZERO_REG:
4686 if (!(type_mask & (1 << OP_REG_GP)))
4687 return 0;
4688 uval = insn_extract_operand (insn, operand);
4689 return 1 << (uval & 31);
4690
fc76e730
RS
4691 case OP_LWM_SWM_LIST:
4692 abort ();
4693
4694 case OP_SAVE_RESTORE_LIST:
4695 abort ();
4696
4697 case OP_MDMX_IMM_REG:
4698 if (!(type_mask & (1 << OP_REG_VEC)))
4699 return 0;
4700 uval = insn_extract_operand (insn, operand);
4701 vsel = uval >> 5;
4702 if ((vsel & 0x18) == 0x18)
4703 return 0;
4704 return 1 << (uval & 31);
56d438b1
CF
4705
4706 case OP_REG_INDEX:
4707 if (!(type_mask & (1 << OP_REG_GP)))
4708 return 0;
4709 return 1 << insn_extract_operand (insn, operand);
df58fc94 4710 }
fc76e730
RS
4711 abort ();
4712}
4713
4714/* Return a mask of the registers specified by operands OPNO_MASK of INSN,
4715 where bit N of OPNO_MASK is set if operand N should be included.
4716 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4717 is set. */
4718
4719static unsigned int
4720insn_reg_mask (const struct mips_cl_insn *insn,
4721 unsigned int type_mask, unsigned int opno_mask)
4722{
4723 unsigned int opno, reg_mask;
4724
4725 opno = 0;
4726 reg_mask = 0;
4727 while (opno_mask != 0)
4728 {
4729 if (opno_mask & 1)
4730 reg_mask |= operand_reg_mask (insn, insn_opno (insn, opno), type_mask);
4731 opno_mask >>= 1;
4732 opno += 1;
4733 }
4734 return reg_mask;
df58fc94
RS
4735}
4736
4c260379
RS
4737/* Return the mask of core registers that IP reads. */
4738
4739static unsigned int
4740gpr_read_mask (const struct mips_cl_insn *ip)
4741{
4742 unsigned long pinfo, pinfo2;
4743 unsigned int mask;
4744
fc76e730 4745 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_read_mask (ip->insn_mo));
4c260379
RS
4746 pinfo = ip->insn_mo->pinfo;
4747 pinfo2 = ip->insn_mo->pinfo2;
fc76e730 4748 if (pinfo & INSN_UDI)
4c260379 4749 {
fc76e730
RS
4750 /* UDI instructions have traditionally been assumed to read RS
4751 and RT. */
4752 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4753 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4c260379 4754 }
fc76e730
RS
4755 if (pinfo & INSN_READ_GPR_24)
4756 mask |= 1 << 24;
4757 if (pinfo2 & INSN2_READ_GPR_16)
4758 mask |= 1 << 16;
4759 if (pinfo2 & INSN2_READ_SP)
4760 mask |= 1 << SP;
26545944 4761 if (pinfo2 & INSN2_READ_GPR_31)
a6a1f5e0 4762 mask |= 1u << 31;
fe35f09f
RS
4763 /* Don't include register 0. */
4764 return mask & ~1;
4c260379
RS
4765}
4766
4767/* Return the mask of core registers that IP writes. */
4768
4769static unsigned int
4770gpr_write_mask (const struct mips_cl_insn *ip)
4771{
4772 unsigned long pinfo, pinfo2;
4773 unsigned int mask;
4774
fc76e730 4775 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_write_mask (ip->insn_mo));
4c260379
RS
4776 pinfo = ip->insn_mo->pinfo;
4777 pinfo2 = ip->insn_mo->pinfo2;
fc76e730
RS
4778 if (pinfo & INSN_WRITE_GPR_24)
4779 mask |= 1 << 24;
4780 if (pinfo & INSN_WRITE_GPR_31)
a6a1f5e0 4781 mask |= 1u << 31;
fc76e730
RS
4782 if (pinfo & INSN_UDI)
4783 /* UDI instructions have traditionally been assumed to write to RD. */
4784 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4785 if (pinfo2 & INSN2_WRITE_SP)
4786 mask |= 1 << SP;
fe35f09f
RS
4787 /* Don't include register 0. */
4788 return mask & ~1;
4c260379
RS
4789}
4790
4791/* Return the mask of floating-point registers that IP reads. */
4792
4793static unsigned int
4794fpr_read_mask (const struct mips_cl_insn *ip)
4795{
fc76e730 4796 unsigned long pinfo;
4c260379
RS
4797 unsigned int mask;
4798
9d5de888
CF
4799 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4800 | (1 << OP_REG_MSA)),
fc76e730 4801 insn_read_mask (ip->insn_mo));
4c260379 4802 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4803 /* Conservatively treat all operands to an FP_D instruction are doubles.
4804 (This is overly pessimistic for things like cvt.d.s.) */
bad1aba3 4805 if (FPR_SIZE != 64 && (pinfo & FP_D))
4c260379
RS
4806 mask |= mask << 1;
4807 return mask;
4808}
4809
4810/* Return the mask of floating-point registers that IP writes. */
4811
4812static unsigned int
4813fpr_write_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_write_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.s.d.) */
bad1aba3 4824 if (FPR_SIZE != 64 && (pinfo & FP_D))
4c260379
RS
4825 mask |= mask << 1;
4826 return mask;
4827}
4828
a1d78564
RS
4829/* Operand OPNUM of INSN is an odd-numbered floating-point register.
4830 Check whether that is allowed. */
4831
4832static bfd_boolean
4833mips_oddfpreg_ok (const struct mips_opcode *insn, int opnum)
4834{
4835 const char *s = insn->name;
351cdf24
MF
4836 bfd_boolean oddspreg = (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa, mips_opts.arch)
4837 || FPR_SIZE == 64)
4838 && mips_opts.oddspreg;
a1d78564
RS
4839
4840 if (insn->pinfo == INSN_MACRO)
4841 /* Let a macro pass, we'll catch it later when it is expanded. */
4842 return TRUE;
4843
351cdf24
MF
4844 /* Single-precision coprocessor loads and moves are OK for 32-bit registers,
4845 otherwise it depends on oddspreg. */
4846 if ((insn->pinfo & FP_S)
4847 && (insn->pinfo & (INSN_LOAD_MEMORY | INSN_STORE_MEMORY
43885403 4848 | INSN_LOAD_COPROC | INSN_COPROC_MOVE)))
351cdf24 4849 return FPR_SIZE == 32 || oddspreg;
a1d78564 4850
351cdf24
MF
4851 /* Allow odd registers for single-precision ops and double-precision if the
4852 floating-point registers are 64-bit wide. */
4853 switch (insn->pinfo & (FP_S | FP_D))
4854 {
4855 case FP_S:
4856 case 0:
4857 return oddspreg;
4858 case FP_D:
4859 return FPR_SIZE == 64;
4860 default:
4861 break;
a1d78564
RS
4862 }
4863
351cdf24
MF
4864 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
4865 s = strchr (insn->name, '.');
4866 if (s != NULL && opnum == 2)
4867 s = strchr (s + 1, '.');
4868 if (s != NULL && (s[1] == 'w' || s[1] == 's'))
4869 return oddspreg;
a1d78564 4870
351cdf24 4871 return FPR_SIZE == 64;
a1d78564
RS
4872}
4873
a1d78564
RS
4874/* Information about an instruction argument that we're trying to match. */
4875struct mips_arg_info
4876{
4877 /* The instruction so far. */
4878 struct mips_cl_insn *insn;
4879
a92713e6
RS
4880 /* The first unconsumed operand token. */
4881 struct mips_operand_token *token;
4882
a1d78564
RS
4883 /* The 1-based operand number, in terms of insn->insn_mo->args. */
4884 int opnum;
4885
4886 /* The 1-based argument number, for error reporting. This does not
4887 count elided optional registers, etc.. */
4888 int argnum;
4889
4890 /* The last OP_REG operand seen, or ILLEGAL_REG if none. */
4891 unsigned int last_regno;
4892
4893 /* If the first operand was an OP_REG, this is the register that it
4894 specified, otherwise it is ILLEGAL_REG. */
4895 unsigned int dest_regno;
4896
4897 /* The value of the last OP_INT operand. Only used for OP_MSB,
4898 where it gives the lsb position. */
4899 unsigned int last_op_int;
4900
60f20e8b 4901 /* If true, match routines should assume that no later instruction
2b0f3761 4902 alternative matches and should therefore be as accommodating as
60f20e8b
RS
4903 possible. Match routines should not report errors if something
4904 is only invalid for !LAX_MATCH. */
4905 bfd_boolean lax_match;
a1d78564 4906
a1d78564
RS
4907 /* True if a reference to the current AT register was seen. */
4908 bfd_boolean seen_at;
4909};
4910
1a00e612
RS
4911/* Record that the argument is out of range. */
4912
4913static void
4914match_out_of_range (struct mips_arg_info *arg)
4915{
4916 set_insn_error_i (arg->argnum, _("operand %d out of range"), arg->argnum);
4917}
4918
4919/* Record that the argument isn't constant but needs to be. */
4920
4921static void
4922match_not_constant (struct mips_arg_info *arg)
4923{
4924 set_insn_error_i (arg->argnum, _("operand %d must be constant"),
4925 arg->argnum);
4926}
4927
a92713e6
RS
4928/* Try to match an OT_CHAR token for character CH. Consume the token
4929 and return true on success, otherwise return false. */
a1d78564 4930
a92713e6
RS
4931static bfd_boolean
4932match_char (struct mips_arg_info *arg, char ch)
a1d78564 4933{
a92713e6
RS
4934 if (arg->token->type == OT_CHAR && arg->token->u.ch == ch)
4935 {
4936 ++arg->token;
4937 if (ch == ',')
4938 arg->argnum += 1;
4939 return TRUE;
4940 }
4941 return FALSE;
4942}
a1d78564 4943
a92713e6
RS
4944/* Try to get an expression from the next tokens in ARG. Consume the
4945 tokens and return true on success, storing the expression value in
4946 VALUE and relocation types in R. */
4947
4948static bfd_boolean
4949match_expression (struct mips_arg_info *arg, expressionS *value,
4950 bfd_reloc_code_real_type *r)
4951{
d436c1c2
RS
4952 /* If the next token is a '(' that was parsed as being part of a base
4953 expression, assume we have an elided offset. The later match will fail
4954 if this turns out to be wrong. */
4955 if (arg->token->type == OT_CHAR && arg->token->u.ch == '(')
a1d78564 4956 {
d436c1c2
RS
4957 value->X_op = O_constant;
4958 value->X_add_number = 0;
4959 r[0] = r[1] = r[2] = BFD_RELOC_UNUSED;
a92713e6
RS
4960 return TRUE;
4961 }
4962
d436c1c2
RS
4963 /* Reject register-based expressions such as "0+$2" and "(($2))".
4964 For plain registers the default error seems more appropriate. */
4965 if (arg->token->type == OT_INTEGER
4966 && arg->token->u.integer.value.X_op == O_register)
a92713e6 4967 {
d436c1c2
RS
4968 set_insn_error (arg->argnum, _("register value used as expression"));
4969 return FALSE;
a1d78564 4970 }
d436c1c2
RS
4971
4972 if (arg->token->type == OT_INTEGER)
a92713e6 4973 {
d436c1c2
RS
4974 *value = arg->token->u.integer.value;
4975 memcpy (r, arg->token->u.integer.relocs, 3 * sizeof (*r));
4976 ++arg->token;
4977 return TRUE;
a92713e6 4978 }
a92713e6 4979
d436c1c2
RS
4980 set_insn_error_i
4981 (arg->argnum, _("operand %d must be an immediate expression"),
4982 arg->argnum);
4983 return FALSE;
a92713e6
RS
4984}
4985
4986/* Try to get a constant expression from the next tokens in ARG. Consume
de194d85 4987 the tokens and return true on success, storing the constant value
a54d5f8b 4988 in *VALUE. */
a92713e6
RS
4989
4990static bfd_boolean
1a00e612 4991match_const_int (struct mips_arg_info *arg, offsetT *value)
a92713e6
RS
4992{
4993 expressionS ex;
4994 bfd_reloc_code_real_type r[3];
a1d78564 4995
a92713e6
RS
4996 if (!match_expression (arg, &ex, r))
4997 return FALSE;
4998
4999 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_constant)
a1d78564
RS
5000 *value = ex.X_add_number;
5001 else
5002 {
c96425c5
MR
5003 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_big)
5004 match_out_of_range (arg);
5005 else
5006 match_not_constant (arg);
1a00e612 5007 return FALSE;
a1d78564 5008 }
a92713e6 5009 return TRUE;
a1d78564
RS
5010}
5011
5012/* Return the RTYPE_* flags for a register operand of type TYPE that
5013 appears in instruction OPCODE. */
5014
5015static unsigned int
5016convert_reg_type (const struct mips_opcode *opcode,
5017 enum mips_reg_operand_type type)
5018{
5019 switch (type)
5020 {
5021 case OP_REG_GP:
5022 return RTYPE_NUM | RTYPE_GP;
5023
5024 case OP_REG_FP:
5025 /* Allow vector register names for MDMX if the instruction is a 64-bit
5026 FPR load, store or move (including moves to and from GPRs). */
5027 if ((mips_opts.ase & ASE_MDMX)
5028 && (opcode->pinfo & FP_D)
43885403 5029 && (opcode->pinfo & (INSN_COPROC_MOVE
a1d78564 5030 | INSN_COPROC_MEMORY_DELAY
43885403 5031 | INSN_LOAD_COPROC
67dc82bc 5032 | INSN_LOAD_MEMORY
a1d78564
RS
5033 | INSN_STORE_MEMORY)))
5034 return RTYPE_FPU | RTYPE_VEC;
5035 return RTYPE_FPU;
5036
5037 case OP_REG_CCC:
5038 if (opcode->pinfo & (FP_D | FP_S))
5039 return RTYPE_CCC | RTYPE_FCC;
5040 return RTYPE_CCC;
5041
5042 case OP_REG_VEC:
5043 if (opcode->membership & INSN_5400)
5044 return RTYPE_FPU;
5045 return RTYPE_FPU | RTYPE_VEC;
5046
5047 case OP_REG_ACC:
5048 return RTYPE_ACC;
5049
5050 case OP_REG_COPRO:
5051 if (opcode->name[strlen (opcode->name) - 1] == '0')
5052 return RTYPE_NUM | RTYPE_CP0;
5053 return RTYPE_NUM;
5054
5055 case OP_REG_HW:
5056 return RTYPE_NUM;
14daeee3
RS
5057
5058 case OP_REG_VI:
5059 return RTYPE_NUM | RTYPE_VI;
5060
5061 case OP_REG_VF:
5062 return RTYPE_NUM | RTYPE_VF;
5063
5064 case OP_REG_R5900_I:
5065 return RTYPE_R5900_I;
5066
5067 case OP_REG_R5900_Q:
5068 return RTYPE_R5900_Q;
5069
5070 case OP_REG_R5900_R:
5071 return RTYPE_R5900_R;
5072
5073 case OP_REG_R5900_ACC:
5074 return RTYPE_R5900_ACC;
56d438b1
CF
5075
5076 case OP_REG_MSA:
5077 return RTYPE_MSA;
5078
5079 case OP_REG_MSA_CTRL:
5080 return RTYPE_NUM;
a1d78564
RS
5081 }
5082 abort ();
5083}
5084
5085/* ARG is register REGNO, of type TYPE. Warn about any dubious registers. */
5086
5087static void
5088check_regno (struct mips_arg_info *arg,
5089 enum mips_reg_operand_type type, unsigned int regno)
5090{
5091 if (AT && type == OP_REG_GP && regno == AT)
5092 arg->seen_at = TRUE;
5093
5094 if (type == OP_REG_FP
5095 && (regno & 1) != 0
a1d78564 5096 && !mips_oddfpreg_ok (arg->insn->insn_mo, arg->opnum))
351cdf24
MF
5097 {
5098 /* This was a warning prior to introducing O32 FPXX and FP64 support
5099 so maintain a warning for FP32 but raise an error for the new
5100 cases. */
5101 if (FPR_SIZE == 32)
5102 as_warn (_("float register should be even, was %d"), regno);
5103 else
5104 as_bad (_("float register should be even, was %d"), regno);
5105 }
a1d78564
RS
5106
5107 if (type == OP_REG_CCC)
5108 {
5109 const char *name;
5110 size_t length;
5111
5112 name = arg->insn->insn_mo->name;
5113 length = strlen (name);
5114 if ((regno & 1) != 0
5115 && ((length >= 3 && strcmp (name + length - 3, ".ps") == 0)
5116 || (length >= 5 && strncmp (name + length - 5, "any2", 4) == 0)))
1661c76c 5117 as_warn (_("condition code register should be even for %s, was %d"),
a1d78564
RS
5118 name, regno);
5119
5120 if ((regno & 3) != 0
5121 && (length >= 5 && strncmp (name + length - 5, "any4", 4) == 0))
1661c76c 5122 as_warn (_("condition code register should be 0 or 4 for %s, was %d"),
a1d78564
RS
5123 name, regno);
5124 }
5125}
5126
a92713e6
RS
5127/* ARG is a register with symbol value SYMVAL. Try to interpret it as
5128 a register of type TYPE. Return true on success, storing the register
5129 number in *REGNO and warning about any dubious uses. */
5130
5131static bfd_boolean
5132match_regno (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5133 unsigned int symval, unsigned int *regno)
5134{
5135 if (type == OP_REG_VEC)
5136 symval = mips_prefer_vec_regno (symval);
5137 if (!(symval & convert_reg_type (arg->insn->insn_mo, type)))
5138 return FALSE;
5139
5140 *regno = symval & RNUM_MASK;
5141 check_regno (arg, type, *regno);
5142 return TRUE;
5143}
5144
5145/* Try to interpret the next token in ARG as a register of type TYPE.
5146 Consume the token and return true on success, storing the register
5147 number in *REGNO. Return false on failure. */
5148
5149static bfd_boolean
5150match_reg (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5151 unsigned int *regno)
5152{
5153 if (arg->token->type == OT_REG
5154 && match_regno (arg, type, arg->token->u.regno, regno))
5155 {
5156 ++arg->token;
5157 return TRUE;
5158 }
5159 return FALSE;
5160}
5161
5162/* Try to interpret the next token in ARG as a range of registers of type TYPE.
5163 Consume the token and return true on success, storing the register numbers
5164 in *REGNO1 and *REGNO2. Return false on failure. */
5165
5166static bfd_boolean
5167match_reg_range (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5168 unsigned int *regno1, unsigned int *regno2)
5169{
5170 if (match_reg (arg, type, regno1))
5171 {
5172 *regno2 = *regno1;
5173 return TRUE;
5174 }
5175 if (arg->token->type == OT_REG_RANGE
5176 && match_regno (arg, type, arg->token->u.reg_range.regno1, regno1)
5177 && match_regno (arg, type, arg->token->u.reg_range.regno2, regno2)
5178 && *regno1 <= *regno2)
5179 {
5180 ++arg->token;
5181 return TRUE;
5182 }
5183 return FALSE;
5184}
5185
a1d78564
RS
5186/* OP_INT matcher. */
5187
a92713e6 5188static bfd_boolean
a1d78564 5189match_int_operand (struct mips_arg_info *arg,
a92713e6 5190 const struct mips_operand *operand_base)
a1d78564
RS
5191{
5192 const struct mips_int_operand *operand;
3ccad066 5193 unsigned int uval;
a1d78564
RS
5194 int min_val, max_val, factor;
5195 offsetT sval;
a1d78564
RS
5196
5197 operand = (const struct mips_int_operand *) operand_base;
5198 factor = 1 << operand->shift;
3ccad066
RS
5199 min_val = mips_int_operand_min (operand);
5200 max_val = mips_int_operand_max (operand);
a1d78564 5201
d436c1c2
RS
5202 if (operand_base->lsb == 0
5203 && operand_base->size == 16
5204 && operand->shift == 0
5205 && operand->bias == 0
5206 && (operand->max_val == 32767 || operand->max_val == 65535))
a1d78564
RS
5207 {
5208 /* The operand can be relocated. */
a92713e6
RS
5209 if (!match_expression (arg, &offset_expr, offset_reloc))
5210 return FALSE;
5211
c96425c5
MR
5212 if (offset_expr.X_op == O_big)
5213 {
5214 match_out_of_range (arg);
5215 return FALSE;
5216 }
5217
a92713e6 5218 if (offset_reloc[0] != BFD_RELOC_UNUSED)
33eaf5de 5219 /* Relocation operators were used. Accept the argument and
a1d78564
RS
5220 leave the relocation value in offset_expr and offset_relocs
5221 for the caller to process. */
a92713e6
RS
5222 return TRUE;
5223
5224 if (offset_expr.X_op != O_constant)
a1d78564 5225 {
60f20e8b
RS
5226 /* Accept non-constant operands if no later alternative matches,
5227 leaving it for the caller to process. */
5228 if (!arg->lax_match)
602b88e3
MR
5229 {
5230 match_not_constant (arg);
5231 return FALSE;
5232 }
a92713e6
RS
5233 offset_reloc[0] = BFD_RELOC_LO16;
5234 return TRUE;
a1d78564 5235 }
a92713e6 5236
a1d78564
RS
5237 /* Clear the global state; we're going to install the operand
5238 ourselves. */
a92713e6 5239 sval = offset_expr.X_add_number;
a1d78564 5240 offset_expr.X_op = O_absent;
60f20e8b
RS
5241
5242 /* For compatibility with older assemblers, we accept
5243 0x8000-0xffff as signed 16-bit numbers when only
5244 signed numbers are allowed. */
5245 if (sval > max_val)
5246 {
5247 max_val = ((1 << operand_base->size) - 1) << operand->shift;
5248 if (!arg->lax_match && sval <= max_val)
20c59b84
MR
5249 {
5250 match_out_of_range (arg);
5251 return FALSE;
5252 }
60f20e8b 5253 }
a1d78564
RS
5254 }
5255 else
5256 {
1a00e612 5257 if (!match_const_int (arg, &sval))
a92713e6 5258 return FALSE;
a1d78564
RS
5259 }
5260
5261 arg->last_op_int = sval;
5262
1a00e612 5263 if (sval < min_val || sval > max_val || sval % factor)
a1d78564 5264 {
1a00e612
RS
5265 match_out_of_range (arg);
5266 return FALSE;
a1d78564
RS
5267 }
5268
5269 uval = (unsigned int) sval >> operand->shift;
5270 uval -= operand->bias;
5271
5272 /* Handle -mfix-cn63xxp1. */
5273 if (arg->opnum == 1
5274 && mips_fix_cn63xxp1
5275 && !mips_opts.micromips
5276 && strcmp ("pref", arg->insn->insn_mo->name) == 0)
5277 switch (uval)
5278 {
5279 case 5:
5280 case 25:
5281 case 26:
5282 case 27:
5283 case 28:
5284 case 29:
5285 case 30:
5286 case 31:
5287 /* These are ok. */
5288 break;
5289
5290 default:
5291 /* The rest must be changed to 28. */
5292 uval = 28;
5293 break;
5294 }
5295
5296 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5297 return TRUE;
a1d78564
RS
5298}
5299
5300/* OP_MAPPED_INT matcher. */
5301
a92713e6 5302static bfd_boolean
a1d78564 5303match_mapped_int_operand (struct mips_arg_info *arg,
a92713e6 5304 const struct mips_operand *operand_base)
a1d78564
RS
5305{
5306 const struct mips_mapped_int_operand *operand;
5307 unsigned int uval, num_vals;
5308 offsetT sval;
5309
5310 operand = (const struct mips_mapped_int_operand *) operand_base;
1a00e612 5311 if (!match_const_int (arg, &sval))
a92713e6 5312 return FALSE;
a1d78564
RS
5313
5314 num_vals = 1 << operand_base->size;
5315 for (uval = 0; uval < num_vals; uval++)
5316 if (operand->int_map[uval] == sval)
5317 break;
5318 if (uval == num_vals)
1a00e612
RS
5319 {
5320 match_out_of_range (arg);
5321 return FALSE;
5322 }
a1d78564
RS
5323
5324 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5325 return TRUE;
a1d78564
RS
5326}
5327
5328/* OP_MSB matcher. */
5329
a92713e6 5330static bfd_boolean
a1d78564 5331match_msb_operand (struct mips_arg_info *arg,
a92713e6 5332 const struct mips_operand *operand_base)
a1d78564
RS
5333{
5334 const struct mips_msb_operand *operand;
5335 int min_val, max_val, max_high;
5336 offsetT size, sval, high;
5337
5338 operand = (const struct mips_msb_operand *) operand_base;
5339 min_val = operand->bias;
5340 max_val = min_val + (1 << operand_base->size) - 1;
5341 max_high = operand->opsize;
5342
1a00e612 5343 if (!match_const_int (arg, &size))
a92713e6 5344 return FALSE;
a1d78564
RS
5345
5346 high = size + arg->last_op_int;
5347 sval = operand->add_lsb ? high : size;
5348
5349 if (size < 0 || high > max_high || sval < min_val || sval > max_val)
5350 {
1a00e612
RS
5351 match_out_of_range (arg);
5352 return FALSE;
a1d78564
RS
5353 }
5354 insn_insert_operand (arg->insn, operand_base, sval - min_val);
a92713e6 5355 return TRUE;
a1d78564
RS
5356}
5357
5358/* OP_REG matcher. */
5359
a92713e6 5360static bfd_boolean
a1d78564 5361match_reg_operand (struct mips_arg_info *arg,
a92713e6 5362 const struct mips_operand *operand_base)
a1d78564
RS
5363{
5364 const struct mips_reg_operand *operand;
a92713e6 5365 unsigned int regno, uval, num_vals;
a1d78564
RS
5366
5367 operand = (const struct mips_reg_operand *) operand_base;
a92713e6
RS
5368 if (!match_reg (arg, operand->reg_type, &regno))
5369 return FALSE;
a1d78564
RS
5370
5371 if (operand->reg_map)
5372 {
5373 num_vals = 1 << operand->root.size;
5374 for (uval = 0; uval < num_vals; uval++)
5375 if (operand->reg_map[uval] == regno)
5376 break;
5377 if (num_vals == uval)
a92713e6 5378 return FALSE;
a1d78564
RS
5379 }
5380 else
5381 uval = regno;
5382
a1d78564
RS
5383 arg->last_regno = regno;
5384 if (arg->opnum == 1)
5385 arg->dest_regno = regno;
5386 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5387 return TRUE;
a1d78564
RS
5388}
5389
5390/* OP_REG_PAIR matcher. */
5391
a92713e6 5392static bfd_boolean
a1d78564 5393match_reg_pair_operand (struct mips_arg_info *arg,
a92713e6 5394 const struct mips_operand *operand_base)
a1d78564
RS
5395{
5396 const struct mips_reg_pair_operand *operand;
a92713e6 5397 unsigned int regno1, regno2, uval, num_vals;
a1d78564
RS
5398
5399 operand = (const struct mips_reg_pair_operand *) operand_base;
a92713e6
RS
5400 if (!match_reg (arg, operand->reg_type, &regno1)
5401 || !match_char (arg, ',')
5402 || !match_reg (arg, operand->reg_type, &regno2))
5403 return FALSE;
a1d78564
RS
5404
5405 num_vals = 1 << operand_base->size;
5406 for (uval = 0; uval < num_vals; uval++)
5407 if (operand->reg1_map[uval] == regno1 && operand->reg2_map[uval] == regno2)
5408 break;
5409 if (uval == num_vals)
a92713e6 5410 return FALSE;
a1d78564 5411
a1d78564 5412 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5413 return TRUE;
a1d78564
RS
5414}
5415
5416/* OP_PCREL matcher. The caller chooses the relocation type. */
5417
a92713e6
RS
5418static bfd_boolean
5419match_pcrel_operand (struct mips_arg_info *arg)
a1d78564 5420{
a92713e6
RS
5421 bfd_reloc_code_real_type r[3];
5422
5423 return match_expression (arg, &offset_expr, r) && r[0] == BFD_RELOC_UNUSED;
a1d78564
RS
5424}
5425
5426/* OP_PERF_REG matcher. */
5427
a92713e6 5428static bfd_boolean
a1d78564 5429match_perf_reg_operand (struct mips_arg_info *arg,
a92713e6 5430 const struct mips_operand *operand)
a1d78564
RS
5431{
5432 offsetT sval;
5433
1a00e612 5434 if (!match_const_int (arg, &sval))
a92713e6 5435 return FALSE;
a1d78564
RS
5436
5437 if (sval != 0
5438 && (sval != 1
5439 || (mips_opts.arch == CPU_R5900
5440 && (strcmp (arg->insn->insn_mo->name, "mfps") == 0
5441 || strcmp (arg->insn->insn_mo->name, "mtps") == 0))))
5442 {
1a00e612
RS
5443 set_insn_error (arg->argnum, _("invalid performance register"));
5444 return FALSE;
a1d78564
RS
5445 }
5446
5447 insn_insert_operand (arg->insn, operand, sval);
a92713e6 5448 return TRUE;
a1d78564
RS
5449}
5450
5451/* OP_ADDIUSP matcher. */
5452
a92713e6 5453static bfd_boolean
a1d78564 5454match_addiusp_operand (struct mips_arg_info *arg,
a92713e6 5455 const struct mips_operand *operand)
a1d78564
RS
5456{
5457 offsetT sval;
5458 unsigned int uval;
5459
1a00e612 5460 if (!match_const_int (arg, &sval))
a92713e6 5461 return FALSE;
a1d78564
RS
5462
5463 if (sval % 4)
1a00e612
RS
5464 {
5465 match_out_of_range (arg);
5466 return FALSE;
5467 }
a1d78564
RS
5468
5469 sval /= 4;
5470 if (!(sval >= -258 && sval <= 257) || (sval >= -2 && sval <= 1))
1a00e612
RS
5471 {
5472 match_out_of_range (arg);
5473 return FALSE;
5474 }
a1d78564
RS
5475
5476 uval = (unsigned int) sval;
5477 uval = ((uval >> 1) & ~0xff) | (uval & 0xff);
5478 insn_insert_operand (arg->insn, operand, uval);
a92713e6 5479 return TRUE;
a1d78564
RS
5480}
5481
5482/* OP_CLO_CLZ_DEST matcher. */
5483
a92713e6 5484static bfd_boolean
a1d78564 5485match_clo_clz_dest_operand (struct mips_arg_info *arg,
a92713e6 5486 const struct mips_operand *operand)
a1d78564
RS
5487{
5488 unsigned int regno;
5489
a92713e6
RS
5490 if (!match_reg (arg, OP_REG_GP, &regno))
5491 return FALSE;
a1d78564 5492
a1d78564 5493 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
a92713e6 5494 return TRUE;
a1d78564
RS
5495}
5496
7361da2c
AB
5497/* OP_CHECK_PREV matcher. */
5498
5499static bfd_boolean
5500match_check_prev_operand (struct mips_arg_info *arg,
5501 const struct mips_operand *operand_base)
5502{
5503 const struct mips_check_prev_operand *operand;
5504 unsigned int regno;
5505
5506 operand = (const struct mips_check_prev_operand *) operand_base;
5507
5508 if (!match_reg (arg, OP_REG_GP, &regno))
5509 return FALSE;
5510
5511 if (!operand->zero_ok && regno == 0)
5512 return FALSE;
5513
5514 if ((operand->less_than_ok && regno < arg->last_regno)
5515 || (operand->greater_than_ok && regno > arg->last_regno)
5516 || (operand->equal_ok && regno == arg->last_regno))
5517 {
5518 arg->last_regno = regno;
5519 insn_insert_operand (arg->insn, operand_base, regno);
5520 return TRUE;
5521 }
5522
5523 return FALSE;
5524}
5525
5526/* OP_SAME_RS_RT matcher. */
5527
5528static bfd_boolean
5529match_same_rs_rt_operand (struct mips_arg_info *arg,
5530 const struct mips_operand *operand)
5531{
5532 unsigned int regno;
5533
5534 if (!match_reg (arg, OP_REG_GP, &regno))
5535 return FALSE;
5536
5537 if (regno == 0)
5538 {
5539 set_insn_error (arg->argnum, _("the source register must not be $0"));
5540 return FALSE;
5541 }
5542
5543 arg->last_regno = regno;
5544
5545 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5546 return TRUE;
5547}
5548
a1d78564
RS
5549/* OP_LWM_SWM_LIST matcher. */
5550
a92713e6 5551static bfd_boolean
a1d78564 5552match_lwm_swm_list_operand (struct mips_arg_info *arg,
a92713e6 5553 const struct mips_operand *operand)
a1d78564 5554{
a92713e6
RS
5555 unsigned int reglist, sregs, ra, regno1, regno2;
5556 struct mips_arg_info reset;
a1d78564 5557
a92713e6
RS
5558 reglist = 0;
5559 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5560 return FALSE;
5561 do
5562 {
5563 if (regno2 == FP && regno1 >= S0 && regno1 <= S7)
5564 {
5565 reglist |= 1 << FP;
5566 regno2 = S7;
5567 }
5568 reglist |= ((1U << regno2 << 1) - 1) & -(1U << regno1);
5569 reset = *arg;
5570 }
5571 while (match_char (arg, ',')
5572 && match_reg_range (arg, OP_REG_GP, &regno1, &regno2));
5573 *arg = reset;
a1d78564
RS
5574
5575 if (operand->size == 2)
5576 {
5577 /* The list must include both ra and s0-sN, for 0 <= N <= 3. E.g.:
5578
5579 s0, ra
5580 s0, s1, ra, s2, s3
5581 s0-s2, ra
5582
5583 and any permutations of these. */
5584 if ((reglist & 0xfff1ffff) != 0x80010000)
a92713e6 5585 return FALSE;
a1d78564
RS
5586
5587 sregs = (reglist >> 17) & 7;
5588 ra = 0;
5589 }
5590 else
5591 {
5592 /* The list must include at least one of ra and s0-sN,
5593 for 0 <= N <= 8. (Note that there is a gap between s7 and s8,
5594 which are $23 and $30 respectively.) E.g.:
5595
5596 ra
5597 s0
5598 ra, s0, s1, s2
5599 s0-s8
5600 s0-s5, ra
5601
5602 and any permutations of these. */
5603 if ((reglist & 0x3f00ffff) != 0)
a92713e6 5604 return FALSE;
a1d78564
RS
5605
5606 ra = (reglist >> 27) & 0x10;
5607 sregs = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
5608 }
5609 sregs += 1;
5610 if ((sregs & -sregs) != sregs)
a92713e6 5611 return FALSE;
a1d78564
RS
5612
5613 insn_insert_operand (arg->insn, operand, (ffs (sregs) - 1) | ra);
a92713e6 5614 return TRUE;
a1d78564
RS
5615}
5616
364215c8
RS
5617/* OP_ENTRY_EXIT_LIST matcher. */
5618
a92713e6 5619static unsigned int
364215c8 5620match_entry_exit_operand (struct mips_arg_info *arg,
a92713e6 5621 const struct mips_operand *operand)
364215c8
RS
5622{
5623 unsigned int mask;
5624 bfd_boolean is_exit;
5625
5626 /* The format is the same for both ENTRY and EXIT, but the constraints
5627 are different. */
5628 is_exit = strcmp (arg->insn->insn_mo->name, "exit") == 0;
5629 mask = (is_exit ? 7 << 3 : 0);
a92713e6 5630 do
364215c8
RS
5631 {
5632 unsigned int regno1, regno2;
5633 bfd_boolean is_freg;
5634
a92713e6 5635 if (match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
364215c8 5636 is_freg = FALSE;
a92713e6 5637 else if (match_reg_range (arg, OP_REG_FP, &regno1, &regno2))
364215c8
RS
5638 is_freg = TRUE;
5639 else
a92713e6 5640 return FALSE;
364215c8
RS
5641
5642 if (is_exit && is_freg && regno1 == 0 && regno2 < 2)
5643 {
5644 mask &= ~(7 << 3);
5645 mask |= (5 + regno2) << 3;
5646 }
5647 else if (!is_exit && regno1 == 4 && regno2 >= 4 && regno2 <= 7)
5648 mask |= (regno2 - 3) << 3;
5649 else if (regno1 == 16 && regno2 >= 16 && regno2 <= 17)
5650 mask |= (regno2 - 15) << 1;
5651 else if (regno1 == RA && regno2 == RA)
5652 mask |= 1;
5653 else
a92713e6 5654 return FALSE;
364215c8 5655 }
a92713e6
RS
5656 while (match_char (arg, ','));
5657
364215c8 5658 insn_insert_operand (arg->insn, operand, mask);
a92713e6 5659 return TRUE;
364215c8
RS
5660}
5661
38bf472a
MR
5662/* Encode regular MIPS SAVE/RESTORE instruction operands according to
5663 the argument register mask AMASK, the number of static registers
5664 saved NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5665 respectively, and the frame size FRAME_SIZE. */
5666
5667static unsigned int
5668mips_encode_save_restore (unsigned int amask, unsigned int nsreg,
5669 unsigned int ra, unsigned int s0, unsigned int s1,
5670 unsigned int frame_size)
5671{
5672 return ((nsreg << 23) | ((frame_size & 0xf0) << 15) | (amask << 15)
5673 | (ra << 12) | (s0 << 11) | (s1 << 10) | ((frame_size & 0xf) << 6));
5674}
5675
5676/* Encode MIPS16 SAVE/RESTORE instruction operands according to the
5677 argument register mask AMASK, the number of static registers saved
5678 NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5679 respectively, and the frame size FRAME_SIZE. */
5680
5681static unsigned int
5682mips16_encode_save_restore (unsigned int amask, unsigned int nsreg,
5683 unsigned int ra, unsigned int s0, unsigned int s1,
5684 unsigned int frame_size)
5685{
5686 unsigned int args;
5687
5688 args = (ra << 6) | (s0 << 5) | (s1 << 4) | (frame_size & 0xf);
5689 if (nsreg || amask || frame_size == 0 || frame_size > 16)
5690 args |= (MIPS16_EXTEND | (nsreg << 24) | (amask << 16)
5691 | ((frame_size & 0xf0) << 16));
5692 return args;
5693}
5694
364215c8
RS
5695/* OP_SAVE_RESTORE_LIST matcher. */
5696
a92713e6
RS
5697static bfd_boolean
5698match_save_restore_list_operand (struct mips_arg_info *arg)
364215c8
RS
5699{
5700 unsigned int opcode, args, statics, sregs;
5701 unsigned int num_frame_sizes, num_args, num_statics, num_sregs;
38bf472a 5702 unsigned int arg_mask, ra, s0, s1;
364215c8 5703 offsetT frame_size;
364215c8 5704
364215c8
RS
5705 opcode = arg->insn->insn_opcode;
5706 frame_size = 0;
5707 num_frame_sizes = 0;
5708 args = 0;
5709 statics = 0;
5710 sregs = 0;
38bf472a
MR
5711 ra = 0;
5712 s0 = 0;
5713 s1 = 0;
a92713e6 5714 do
364215c8
RS
5715 {
5716 unsigned int regno1, regno2;
5717
a92713e6 5718 if (arg->token->type == OT_INTEGER)
364215c8
RS
5719 {
5720 /* Handle the frame size. */
1a00e612 5721 if (!match_const_int (arg, &frame_size))
a92713e6 5722 return FALSE;
364215c8 5723 num_frame_sizes += 1;
364215c8
RS
5724 }
5725 else
5726 {
a92713e6
RS
5727 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5728 return FALSE;
364215c8
RS
5729
5730 while (regno1 <= regno2)
5731 {
5732 if (regno1 >= 4 && regno1 <= 7)
5733 {
5734 if (num_frame_sizes == 0)
5735 /* args $a0-$a3 */
5736 args |= 1 << (regno1 - 4);
5737 else
5738 /* statics $a0-$a3 */
5739 statics |= 1 << (regno1 - 4);
5740 }
5741 else if (regno1 >= 16 && regno1 <= 23)
5742 /* $s0-$s7 */
5743 sregs |= 1 << (regno1 - 16);
5744 else if (regno1 == 30)
5745 /* $s8 */
5746 sregs |= 1 << 8;
5747 else if (regno1 == 31)
5748 /* Add $ra to insn. */
38bf472a 5749 ra = 1;
364215c8 5750 else
a92713e6 5751 return FALSE;
364215c8
RS
5752 regno1 += 1;
5753 if (regno1 == 24)
5754 regno1 = 30;
5755 }
5756 }
364215c8 5757 }
a92713e6 5758 while (match_char (arg, ','));
364215c8
RS
5759
5760 /* Encode args/statics combination. */
5761 if (args & statics)
a92713e6 5762 return FALSE;
364215c8
RS
5763 else if (args == 0xf)
5764 /* All $a0-$a3 are args. */
38bf472a 5765 arg_mask = MIPS_SVRS_ALL_ARGS;
364215c8
RS
5766 else if (statics == 0xf)
5767 /* All $a0-$a3 are statics. */
38bf472a 5768 arg_mask = MIPS_SVRS_ALL_STATICS;
364215c8
RS
5769 else
5770 {
5771 /* Count arg registers. */
5772 num_args = 0;
5773 while (args & 0x1)
5774 {
5775 args >>= 1;
5776 num_args += 1;
5777 }
5778 if (args != 0)
a92713e6 5779 return FALSE;
364215c8
RS
5780
5781 /* Count static registers. */
5782 num_statics = 0;
5783 while (statics & 0x8)
5784 {
5785 statics = (statics << 1) & 0xf;
5786 num_statics += 1;
5787 }
5788 if (statics != 0)
a92713e6 5789 return FALSE;
364215c8
RS
5790
5791 /* Encode args/statics. */
38bf472a 5792 arg_mask = (num_args << 2) | num_statics;
364215c8
RS
5793 }
5794
5795 /* Encode $s0/$s1. */
5796 if (sregs & (1 << 0)) /* $s0 */
38bf472a 5797 s0 = 1;
364215c8 5798 if (sregs & (1 << 1)) /* $s1 */
38bf472a 5799 s1 = 1;
364215c8
RS
5800 sregs >>= 2;
5801
5802 /* Encode $s2-$s8. */
5803 num_sregs = 0;
5804 while (sregs & 1)
5805 {
5806 sregs >>= 1;
5807 num_sregs += 1;
5808 }
5809 if (sregs != 0)
a92713e6 5810 return FALSE;
364215c8
RS
5811
5812 /* Encode frame size. */
5813 if (num_frame_sizes == 0)
1a00e612
RS
5814 {
5815 set_insn_error (arg->argnum, _("missing frame size"));
5816 return FALSE;
5817 }
5818 if (num_frame_sizes > 1)
5819 {
5820 set_insn_error (arg->argnum, _("frame size specified twice"));
5821 return FALSE;
5822 }
5823 if ((frame_size & 7) != 0 || frame_size < 0 || frame_size > 0xff * 8)
5824 {
5825 set_insn_error (arg->argnum, _("invalid frame size"));
5826 return FALSE;
5827 }
38bf472a 5828 frame_size /= 8;
364215c8 5829
364215c8 5830 /* Finally build the instruction. */
38bf472a
MR
5831 if (mips_opts.mips16)
5832 opcode |= mips16_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5833 frame_size);
5834 else if (!mips_opts.micromips)
5835 opcode |= mips_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5836 frame_size);
5837 else
5838 abort ();
5839
364215c8 5840 arg->insn->insn_opcode = opcode;
a92713e6 5841 return TRUE;
364215c8
RS
5842}
5843
a1d78564
RS
5844/* OP_MDMX_IMM_REG matcher. */
5845
a92713e6 5846static bfd_boolean
a1d78564 5847match_mdmx_imm_reg_operand (struct mips_arg_info *arg,
a92713e6 5848 const struct mips_operand *operand)
a1d78564 5849{
a92713e6 5850 unsigned int regno, uval;
a1d78564
RS
5851 bfd_boolean is_qh;
5852 const struct mips_opcode *opcode;
5853
5854 /* The mips_opcode records whether this is an octobyte or quadhalf
5855 instruction. Start out with that bit in place. */
5856 opcode = arg->insn->insn_mo;
5857 uval = mips_extract_operand (operand, opcode->match);
5858 is_qh = (uval != 0);
5859
56d438b1 5860 if (arg->token->type == OT_REG)
a1d78564
RS
5861 {
5862 if ((opcode->membership & INSN_5400)
5863 && strcmp (opcode->name, "rzu.ob") == 0)
5864 {
1a00e612
RS
5865 set_insn_error_i (arg->argnum, _("operand %d must be an immediate"),
5866 arg->argnum);
5867 return FALSE;
a1d78564
RS
5868 }
5869
56d438b1
CF
5870 if (!match_regno (arg, OP_REG_VEC, arg->token->u.regno, &regno))
5871 return FALSE;
5872 ++arg->token;
5873
a1d78564
RS
5874 /* Check whether this is a vector register or a broadcast of
5875 a single element. */
56d438b1 5876 if (arg->token->type == OT_INTEGER_INDEX)
a1d78564 5877 {
56d438b1 5878 if (arg->token->u.index > (is_qh ? 3 : 7))
a1d78564 5879 {
1a00e612
RS
5880 set_insn_error (arg->argnum, _("invalid element selector"));
5881 return FALSE;
a1d78564 5882 }
56d438b1
CF
5883 uval |= arg->token->u.index << (is_qh ? 2 : 1) << 5;
5884 ++arg->token;
a1d78564
RS
5885 }
5886 else
5887 {
5888 /* A full vector. */
5889 if ((opcode->membership & INSN_5400)
5890 && (strcmp (opcode->name, "sll.ob") == 0
5891 || strcmp (opcode->name, "srl.ob") == 0))
5892 {
1a00e612
RS
5893 set_insn_error_i (arg->argnum, _("operand %d must be scalar"),
5894 arg->argnum);
5895 return FALSE;
a1d78564
RS
5896 }
5897
5898 if (is_qh)
5899 uval |= MDMX_FMTSEL_VEC_QH << 5;
5900 else
5901 uval |= MDMX_FMTSEL_VEC_OB << 5;
5902 }
a1d78564
RS
5903 uval |= regno;
5904 }
5905 else
5906 {
5907 offsetT sval;
5908
1a00e612 5909 if (!match_const_int (arg, &sval))
a92713e6 5910 return FALSE;
a1d78564
RS
5911 if (sval < 0 || sval > 31)
5912 {
1a00e612
RS
5913 match_out_of_range (arg);
5914 return FALSE;
a1d78564
RS
5915 }
5916 uval |= (sval & 31);
5917 if (is_qh)
5918 uval |= MDMX_FMTSEL_IMM_QH << 5;
5919 else
5920 uval |= MDMX_FMTSEL_IMM_OB << 5;
5921 }
5922 insn_insert_operand (arg->insn, operand, uval);
a92713e6 5923 return TRUE;
a1d78564
RS
5924}
5925
56d438b1
CF
5926/* OP_IMM_INDEX matcher. */
5927
5928static bfd_boolean
5929match_imm_index_operand (struct mips_arg_info *arg,
5930 const struct mips_operand *operand)
5931{
5932 unsigned int max_val;
5933
5934 if (arg->token->type != OT_INTEGER_INDEX)
5935 return FALSE;
5936
5937 max_val = (1 << operand->size) - 1;
5938 if (arg->token->u.index > max_val)
5939 {
5940 match_out_of_range (arg);
5941 return FALSE;
5942 }
5943 insn_insert_operand (arg->insn, operand, arg->token->u.index);
5944 ++arg->token;
5945 return TRUE;
5946}
5947
5948/* OP_REG_INDEX matcher. */
5949
5950static bfd_boolean
5951match_reg_index_operand (struct mips_arg_info *arg,
5952 const struct mips_operand *operand)
5953{
5954 unsigned int regno;
5955
5956 if (arg->token->type != OT_REG_INDEX)
5957 return FALSE;
5958
5959 if (!match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno))
5960 return FALSE;
5961
5962 insn_insert_operand (arg->insn, operand, regno);
5963 ++arg->token;
5964 return TRUE;
5965}
5966
a1d78564
RS
5967/* OP_PC matcher. */
5968
a92713e6
RS
5969static bfd_boolean
5970match_pc_operand (struct mips_arg_info *arg)
a1d78564 5971{
a92713e6
RS
5972 if (arg->token->type == OT_REG && (arg->token->u.regno & RTYPE_PC))
5973 {
5974 ++arg->token;
5975 return TRUE;
5976 }
5977 return FALSE;
a1d78564
RS
5978}
5979
25499ac7
MR
5980/* OP_REG28 matcher. */
5981
5982static bfd_boolean
5983match_reg28_operand (struct mips_arg_info *arg)
5984{
5985 unsigned int regno;
5986
5987 if (arg->token->type == OT_REG
5988 && match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno)
5989 && regno == GP)
5990 {
5991 ++arg->token;
5992 return TRUE;
5993 }
5994 return FALSE;
5995}
5996
7361da2c
AB
5997/* OP_NON_ZERO_REG matcher. */
5998
5999static bfd_boolean
6000match_non_zero_reg_operand (struct mips_arg_info *arg,
6001 const struct mips_operand *operand)
6002{
6003 unsigned int regno;
6004
6005 if (!match_reg (arg, OP_REG_GP, &regno))
6006 return FALSE;
6007
6008 if (regno == 0)
85bec12d
MF
6009 {
6010 set_insn_error (arg->argnum, _("the source register must not be $0"));
6011 return FALSE;
6012 }
7361da2c
AB
6013
6014 arg->last_regno = regno;
6015 insn_insert_operand (arg->insn, operand, regno);
6016 return TRUE;
6017}
6018
a1d78564
RS
6019/* OP_REPEAT_DEST_REG and OP_REPEAT_PREV_REG matcher. OTHER_REGNO is the
6020 register that we need to match. */
6021
a92713e6
RS
6022static bfd_boolean
6023match_tied_reg_operand (struct mips_arg_info *arg, unsigned int other_regno)
a1d78564
RS
6024{
6025 unsigned int regno;
6026
a92713e6 6027 return match_reg (arg, OP_REG_GP, &regno) && regno == other_regno;
a1d78564
RS
6028}
6029
33f46696
MR
6030/* Try to match a floating-point constant from ARG for LI.S or LI.D.
6031 LENGTH is the length of the value in bytes (4 for float, 8 for double)
6032 and USING_GPRS says whether the destination is a GPR rather than an FPR.
89565f1b
RS
6033
6034 Return the constant in IMM and OFFSET as follows:
6035
6036 - If the constant should be loaded via memory, set IMM to O_absent and
6037 OFFSET to the memory address.
6038
6039 - Otherwise, if the constant should be loaded into two 32-bit registers,
6040 set IMM to the O_constant to load into the high register and OFFSET
6041 to the corresponding value for the low register.
6042
6043 - Otherwise, set IMM to the full O_constant and set OFFSET to O_absent.
6044
6045 These constants only appear as the last operand in an instruction,
6046 and every instruction that accepts them in any variant accepts them
6047 in all variants. This means we don't have to worry about backing out
6048 any changes if the instruction does not match. We just match
6049 unconditionally and report an error if the constant is invalid. */
6050
a92713e6
RS
6051static bfd_boolean
6052match_float_constant (struct mips_arg_info *arg, expressionS *imm,
6053 expressionS *offset, int length, bfd_boolean using_gprs)
89565f1b 6054{
a92713e6 6055 char *p;
89565f1b
RS
6056 segT seg, new_seg;
6057 subsegT subseg;
6058 const char *newname;
a92713e6 6059 unsigned char *data;
89565f1b
RS
6060
6061 /* Where the constant is placed is based on how the MIPS assembler
6062 does things:
6063
6064 length == 4 && using_gprs -- immediate value only
6065 length == 8 && using_gprs -- .rdata or immediate value
6066 length == 4 && !using_gprs -- .lit4 or immediate value
6067 length == 8 && !using_gprs -- .lit8 or immediate value
6068
6069 The .lit4 and .lit8 sections are only used if permitted by the
6070 -G argument. */
a92713e6 6071 if (arg->token->type != OT_FLOAT)
1a00e612
RS
6072 {
6073 set_insn_error (arg->argnum, _("floating-point expression required"));
6074 return FALSE;
6075 }
a92713e6
RS
6076
6077 gas_assert (arg->token->u.flt.length == length);
6078 data = arg->token->u.flt.data;
6079 ++arg->token;
89565f1b
RS
6080
6081 /* Handle 32-bit constants for which an immediate value is best. */
6082 if (length == 4
6083 && (using_gprs
6084 || g_switch_value < 4
6085 || (data[0] == 0 && data[1] == 0)
6086 || (data[2] == 0 && data[3] == 0)))
6087 {
6088 imm->X_op = O_constant;
6089 if (!target_big_endian)
6090 imm->X_add_number = bfd_getl32 (data);
6091 else
6092 imm->X_add_number = bfd_getb32 (data);
6093 offset->X_op = O_absent;
a92713e6 6094 return TRUE;
89565f1b
RS
6095 }
6096
6097 /* Handle 64-bit constants for which an immediate value is best. */
6098 if (length == 8
6099 && !mips_disable_float_construction
351cdf24
MF
6100 /* Constants can only be constructed in GPRs and copied to FPRs if the
6101 GPRs are at least as wide as the FPRs or MTHC1 is available.
6102 Unlike most tests for 32-bit floating-point registers this check
6103 specifically looks for GPR_SIZE == 32 as the FPXX ABI does not
6104 permit 64-bit moves without MXHC1.
6105 Force the constant into memory otherwise. */
6106 && (using_gprs
6107 || GPR_SIZE == 64
6108 || ISA_HAS_MXHC1 (mips_opts.isa)
6109 || FPR_SIZE == 32)
89565f1b
RS
6110 && ((data[0] == 0 && data[1] == 0)
6111 || (data[2] == 0 && data[3] == 0))
6112 && ((data[4] == 0 && data[5] == 0)
6113 || (data[6] == 0 && data[7] == 0)))
6114 {
6115 /* The value is simple enough to load with a couple of instructions.
6116 If using 32-bit registers, set IMM to the high order 32 bits and
6117 OFFSET to the low order 32 bits. Otherwise, set IMM to the entire
6118 64 bit constant. */
351cdf24 6119 if (GPR_SIZE == 32 || (!using_gprs && FPR_SIZE != 64))
89565f1b
RS
6120 {
6121 imm->X_op = O_constant;
6122 offset->X_op = O_constant;
6123 if (!target_big_endian)
6124 {
6125 imm->X_add_number = bfd_getl32 (data + 4);
6126 offset->X_add_number = bfd_getl32 (data);
6127 }
6128 else
6129 {
6130 imm->X_add_number = bfd_getb32 (data);
6131 offset->X_add_number = bfd_getb32 (data + 4);
6132 }
6133 if (offset->X_add_number == 0)
6134 offset->X_op = O_absent;
6135 }
6136 else
6137 {
6138 imm->X_op = O_constant;
6139 if (!target_big_endian)
6140 imm->X_add_number = bfd_getl64 (data);
6141 else
6142 imm->X_add_number = bfd_getb64 (data);
6143 offset->X_op = O_absent;
6144 }
a92713e6 6145 return TRUE;
89565f1b
RS
6146 }
6147
6148 /* Switch to the right section. */
6149 seg = now_seg;
6150 subseg = now_subseg;
6151 if (length == 4)
6152 {
6153 gas_assert (!using_gprs && g_switch_value >= 4);
6154 newname = ".lit4";
6155 }
6156 else
6157 {
6158 if (using_gprs || g_switch_value < 8)
6159 newname = RDATA_SECTION_NAME;
6160 else
6161 newname = ".lit8";
6162 }
6163
6164 new_seg = subseg_new (newname, (subsegT) 0);
fd361982 6165 bfd_set_section_flags (new_seg,
89565f1b
RS
6166 SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA);
6167 frag_align (length == 4 ? 2 : 3, 0, 0);
6168 if (strncmp (TARGET_OS, "elf", 3) != 0)
6169 record_alignment (new_seg, 4);
6170 else
6171 record_alignment (new_seg, length == 4 ? 2 : 3);
6172 if (seg == now_seg)
1661c76c 6173 as_bad (_("cannot use `%s' in this section"), arg->insn->insn_mo->name);
89565f1b
RS
6174
6175 /* Set the argument to the current address in the section. */
6176 imm->X_op = O_absent;
6177 offset->X_op = O_symbol;
6178 offset->X_add_symbol = symbol_temp_new_now ();
6179 offset->X_add_number = 0;
6180
6181 /* Put the floating point number into the section. */
6182 p = frag_more (length);
6183 memcpy (p, data, length);
6184
6185 /* Switch back to the original section. */
6186 subseg_set (seg, subseg);
a92713e6 6187 return TRUE;
89565f1b
RS
6188}
6189
14daeee3
RS
6190/* OP_VU0_SUFFIX and OP_VU0_MATCH_SUFFIX matcher; MATCH_P selects between
6191 them. */
6192
6193static bfd_boolean
6194match_vu0_suffix_operand (struct mips_arg_info *arg,
6195 const struct mips_operand *operand,
6196 bfd_boolean match_p)
6197{
6198 unsigned int uval;
6199
6200 /* The operand can be an XYZW mask or a single 2-bit channel index
6201 (with X being 0). */
6202 gas_assert (operand->size == 2 || operand->size == 4);
6203
ee5734f0 6204 /* The suffix can be omitted when it is already part of the opcode. */
14daeee3 6205 if (arg->token->type != OT_CHANNELS)
ee5734f0 6206 return match_p;
14daeee3
RS
6207
6208 uval = arg->token->u.channels;
6209 if (operand->size == 2)
6210 {
6211 /* Check that a single bit is set and convert it into a 2-bit index. */
6212 if ((uval & -uval) != uval)
6213 return FALSE;
6214 uval = 4 - ffs (uval);
6215 }
6216
6217 if (match_p && insn_extract_operand (arg->insn, operand) != uval)
6218 return FALSE;
6219
6220 ++arg->token;
6221 if (!match_p)
6222 insn_insert_operand (arg->insn, operand, uval);
6223 return TRUE;
6224}
6225
33f46696
MR
6226/* Try to match a token from ARG against OPERAND. Consume the token
6227 and return true on success, otherwise return false. */
a1d78564 6228
a92713e6 6229static bfd_boolean
a1d78564 6230match_operand (struct mips_arg_info *arg,
a92713e6 6231 const struct mips_operand *operand)
a1d78564
RS
6232{
6233 switch (operand->type)
6234 {
6235 case OP_INT:
a92713e6 6236 return match_int_operand (arg, operand);
a1d78564
RS
6237
6238 case OP_MAPPED_INT:
a92713e6 6239 return match_mapped_int_operand (arg, operand);
a1d78564
RS
6240
6241 case OP_MSB:
a92713e6 6242 return match_msb_operand (arg, operand);
a1d78564
RS
6243
6244 case OP_REG:
0f35dbc4 6245 case OP_OPTIONAL_REG:
a92713e6 6246 return match_reg_operand (arg, operand);
a1d78564
RS
6247
6248 case OP_REG_PAIR:
a92713e6 6249 return match_reg_pair_operand (arg, operand);
a1d78564
RS
6250
6251 case OP_PCREL:
a92713e6 6252 return match_pcrel_operand (arg);
a1d78564
RS
6253
6254 case OP_PERF_REG:
a92713e6 6255 return match_perf_reg_operand (arg, operand);
a1d78564
RS
6256
6257 case OP_ADDIUSP_INT:
a92713e6 6258 return match_addiusp_operand (arg, operand);
a1d78564
RS
6259
6260 case OP_CLO_CLZ_DEST:
a92713e6 6261 return match_clo_clz_dest_operand (arg, operand);
a1d78564
RS
6262
6263 case OP_LWM_SWM_LIST:
a92713e6 6264 return match_lwm_swm_list_operand (arg, operand);
a1d78564
RS
6265
6266 case OP_ENTRY_EXIT_LIST:
a92713e6 6267 return match_entry_exit_operand (arg, operand);
364215c8 6268
a1d78564 6269 case OP_SAVE_RESTORE_LIST:
a92713e6 6270 return match_save_restore_list_operand (arg);
a1d78564
RS
6271
6272 case OP_MDMX_IMM_REG:
a92713e6 6273 return match_mdmx_imm_reg_operand (arg, operand);
a1d78564
RS
6274
6275 case OP_REPEAT_DEST_REG:
a92713e6 6276 return match_tied_reg_operand (arg, arg->dest_regno);
a1d78564
RS
6277
6278 case OP_REPEAT_PREV_REG:
a92713e6 6279 return match_tied_reg_operand (arg, arg->last_regno);
a1d78564
RS
6280
6281 case OP_PC:
a92713e6 6282 return match_pc_operand (arg);
14daeee3 6283
25499ac7
MR
6284 case OP_REG28:
6285 return match_reg28_operand (arg);
6286
14daeee3
RS
6287 case OP_VU0_SUFFIX:
6288 return match_vu0_suffix_operand (arg, operand, FALSE);
6289
6290 case OP_VU0_MATCH_SUFFIX:
6291 return match_vu0_suffix_operand (arg, operand, TRUE);
56d438b1
CF
6292
6293 case OP_IMM_INDEX:
6294 return match_imm_index_operand (arg, operand);
6295
6296 case OP_REG_INDEX:
6297 return match_reg_index_operand (arg, operand);
7361da2c
AB
6298
6299 case OP_SAME_RS_RT:
6300 return match_same_rs_rt_operand (arg, operand);
6301
6302 case OP_CHECK_PREV:
6303 return match_check_prev_operand (arg, operand);
6304
6305 case OP_NON_ZERO_REG:
6306 return match_non_zero_reg_operand (arg, operand);
a1d78564
RS
6307 }
6308 abort ();
6309}
6310
6311/* ARG is the state after successfully matching an instruction.
6312 Issue any queued-up warnings. */
6313
6314static void
6315check_completed_insn (struct mips_arg_info *arg)
6316{
6317 if (arg->seen_at)
6318 {
6319 if (AT == ATREG)
1661c76c 6320 as_warn (_("used $at without \".set noat\""));
a1d78564 6321 else
1661c76c 6322 as_warn (_("used $%u with \".set at=$%u\""), AT, AT);
a1d78564
RS
6323 }
6324}
a1d78564 6325
85fcb30f
RS
6326/* Return true if modifying general-purpose register REG needs a delay. */
6327
6328static bfd_boolean
6329reg_needs_delay (unsigned int reg)
6330{
6331 unsigned long prev_pinfo;
6332
6333 prev_pinfo = history[0].insn_mo->pinfo;
6334 if (!mips_opts.noreorder
67dc82bc 6335 && (((prev_pinfo & INSN_LOAD_MEMORY) && !gpr_interlocks)
43885403 6336 || ((prev_pinfo & INSN_LOAD_COPROC) && !cop_interlocks))
85fcb30f
RS
6337 && (gpr_write_mask (&history[0]) & (1 << reg)))
6338 return TRUE;
6339
6340 return FALSE;
6341}
6342
71400594
RS
6343/* Classify an instruction according to the FIX_VR4120_* enumeration.
6344 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
6345 by VR4120 errata. */
4d7206a2 6346
71400594
RS
6347static unsigned int
6348classify_vr4120_insn (const char *name)
252b5132 6349{
71400594
RS
6350 if (strncmp (name, "macc", 4) == 0)
6351 return FIX_VR4120_MACC;
6352 if (strncmp (name, "dmacc", 5) == 0)
6353 return FIX_VR4120_DMACC;
6354 if (strncmp (name, "mult", 4) == 0)
6355 return FIX_VR4120_MULT;
6356 if (strncmp (name, "dmult", 5) == 0)
6357 return FIX_VR4120_DMULT;
6358 if (strstr (name, "div"))
6359 return FIX_VR4120_DIV;
6360 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
6361 return FIX_VR4120_MTHILO;
6362 return NUM_FIX_VR4120_CLASSES;
6363}
252b5132 6364
a8d14a88
CM
6365#define INSN_ERET 0x42000018
6366#define INSN_DERET 0x4200001f
6367#define INSN_DMULT 0x1c
6368#define INSN_DMULTU 0x1d
ff239038 6369
71400594
RS
6370/* Return the number of instructions that must separate INSN1 and INSN2,
6371 where INSN1 is the earlier instruction. Return the worst-case value
6372 for any INSN2 if INSN2 is null. */
252b5132 6373
71400594
RS
6374static unsigned int
6375insns_between (const struct mips_cl_insn *insn1,
6376 const struct mips_cl_insn *insn2)
6377{
6378 unsigned long pinfo1, pinfo2;
4c260379 6379 unsigned int mask;
71400594 6380
85fcb30f
RS
6381 /* If INFO2 is null, pessimistically assume that all flags are set for
6382 the second instruction. */
71400594
RS
6383 pinfo1 = insn1->insn_mo->pinfo;
6384 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
252b5132 6385
71400594
RS
6386 /* For most targets, write-after-read dependencies on the HI and LO
6387 registers must be separated by at least two instructions. */
6388 if (!hilo_interlocks)
252b5132 6389 {
71400594
RS
6390 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
6391 return 2;
6392 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
6393 return 2;
6394 }
6395
6396 /* If we're working around r7000 errata, there must be two instructions
6397 between an mfhi or mflo and any instruction that uses the result. */
6398 if (mips_7000_hilo_fix
df58fc94 6399 && !mips_opts.micromips
71400594 6400 && MF_HILO_INSN (pinfo1)
85fcb30f 6401 && (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1))))
71400594
RS
6402 return 2;
6403
ff239038
CM
6404 /* If we're working around 24K errata, one instruction is required
6405 if an ERET or DERET is followed by a branch instruction. */
df58fc94 6406 if (mips_fix_24k && !mips_opts.micromips)
ff239038
CM
6407 {
6408 if (insn1->insn_opcode == INSN_ERET
6409 || insn1->insn_opcode == INSN_DERET)
6410 {
6411 if (insn2 == NULL
6412 || insn2->insn_opcode == INSN_ERET
6413 || insn2->insn_opcode == INSN_DERET
11625dd8 6414 || delayed_branch_p (insn2))
ff239038
CM
6415 return 1;
6416 }
6417 }
6418
a8d14a88
CM
6419 /* If we're working around PMC RM7000 errata, there must be three
6420 nops between a dmult and a load instruction. */
6421 if (mips_fix_rm7000 && !mips_opts.micromips)
6422 {
6423 if ((insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULT
6424 || (insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULTU)
6425 {
6426 if (pinfo2 & INSN_LOAD_MEMORY)
6427 return 3;
6428 }
6429 }
6430
71400594
RS
6431 /* If working around VR4120 errata, check for combinations that need
6432 a single intervening instruction. */
df58fc94 6433 if (mips_fix_vr4120 && !mips_opts.micromips)
71400594
RS
6434 {
6435 unsigned int class1, class2;
252b5132 6436
71400594
RS
6437 class1 = classify_vr4120_insn (insn1->insn_mo->name);
6438 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
252b5132 6439 {
71400594
RS
6440 if (insn2 == NULL)
6441 return 1;
6442 class2 = classify_vr4120_insn (insn2->insn_mo->name);
6443 if (vr4120_conflicts[class1] & (1 << class2))
6444 return 1;
252b5132 6445 }
71400594
RS
6446 }
6447
df58fc94 6448 if (!HAVE_CODE_COMPRESSION)
71400594
RS
6449 {
6450 /* Check for GPR or coprocessor load delays. All such delays
6451 are on the RT register. */
6452 /* Itbl support may require additional care here. */
67dc82bc 6453 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY))
43885403 6454 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC)))
252b5132 6455 {
85fcb30f 6456 if (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1)))
71400594
RS
6457 return 1;
6458 }
6459
6460 /* Check for generic coprocessor hazards.
6461
6462 This case is not handled very well. There is no special
6463 knowledge of CP0 handling, and the coprocessors other than
6464 the floating point unit are not distinguished at all. */
6465 /* Itbl support may require additional care here. FIXME!
6466 Need to modify this to include knowledge about
6467 user specified delays! */
43885403 6468 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE))
71400594
RS
6469 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
6470 {
6471 /* Handle cases where INSN1 writes to a known general coprocessor
6472 register. There must be a one instruction delay before INSN2
6473 if INSN2 reads that register, otherwise no delay is needed. */
4c260379
RS
6474 mask = fpr_write_mask (insn1);
6475 if (mask != 0)
252b5132 6476 {
4c260379 6477 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
71400594 6478 return 1;
252b5132
RH
6479 }
6480 else
6481 {
71400594
RS
6482 /* Read-after-write dependencies on the control registers
6483 require a two-instruction gap. */
6484 if ((pinfo1 & INSN_WRITE_COND_CODE)
6485 && (pinfo2 & INSN_READ_COND_CODE))
6486 return 2;
6487
6488 /* We don't know exactly what INSN1 does. If INSN2 is
6489 also a coprocessor instruction, assume there must be
6490 a one instruction gap. */
6491 if (pinfo2 & INSN_COP)
6492 return 1;
252b5132
RH
6493 }
6494 }
6b76fefe 6495
71400594
RS
6496 /* Check for read-after-write dependencies on the coprocessor
6497 control registers in cases where INSN1 does not need a general
6498 coprocessor delay. This means that INSN1 is a floating point
6499 comparison instruction. */
6500 /* Itbl support may require additional care here. */
6501 else if (!cop_interlocks
6502 && (pinfo1 & INSN_WRITE_COND_CODE)
6503 && (pinfo2 & INSN_READ_COND_CODE))
6504 return 1;
6505 }
6b76fefe 6506
7361da2c
AB
6507 /* Forbidden slots can not contain Control Transfer Instructions (CTIs)
6508 CTIs include all branches and jumps, nal, eret, eretnc, deret, wait
6509 and pause. */
6510 if ((insn1->insn_mo->pinfo2 & INSN2_FORBIDDEN_SLOT)
6511 && ((pinfo2 & INSN_NO_DELAY_SLOT)
6512 || (insn2 && delayed_branch_p (insn2))))
6513 return 1;
6514
71400594
RS
6515 return 0;
6516}
6b76fefe 6517
7d8e00cf
RS
6518/* Return the number of nops that would be needed to work around the
6519 VR4130 mflo/mfhi errata if instruction INSN immediately followed
932d1a1b
RS
6520 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
6521 that are contained within the first IGNORE instructions of HIST. */
7d8e00cf
RS
6522
6523static int
932d1a1b 6524nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
7d8e00cf
RS
6525 const struct mips_cl_insn *insn)
6526{
4c260379
RS
6527 int i, j;
6528 unsigned int mask;
7d8e00cf
RS
6529
6530 /* Check if the instruction writes to HI or LO. MTHI and MTLO
6531 are not affected by the errata. */
6532 if (insn != 0
6533 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
6534 || strcmp (insn->insn_mo->name, "mtlo") == 0
6535 || strcmp (insn->insn_mo->name, "mthi") == 0))
6536 return 0;
6537
6538 /* Search for the first MFLO or MFHI. */
6539 for (i = 0; i < MAX_VR4130_NOPS; i++)
91d6fa6a 6540 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
7d8e00cf
RS
6541 {
6542 /* Extract the destination register. */
4c260379 6543 mask = gpr_write_mask (&hist[i]);
7d8e00cf
RS
6544
6545 /* No nops are needed if INSN reads that register. */
4c260379 6546 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
7d8e00cf
RS
6547 return 0;
6548
6549 /* ...or if any of the intervening instructions do. */
6550 for (j = 0; j < i; j++)
4c260379 6551 if (gpr_read_mask (&hist[j]) & mask)
7d8e00cf
RS
6552 return 0;
6553
932d1a1b
RS
6554 if (i >= ignore)
6555 return MAX_VR4130_NOPS - i;
7d8e00cf
RS
6556 }
6557 return 0;
6558}
6559
134c0c8b
MR
6560#define BASE_REG_EQ(INSN1, INSN2) \
6561 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
15be625d
CM
6562 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
6563
6564/* Return the minimum alignment for this store instruction. */
6565
6566static int
6567fix_24k_align_to (const struct mips_opcode *mo)
6568{
6569 if (strcmp (mo->name, "sh") == 0)
6570 return 2;
6571
6572 if (strcmp (mo->name, "swc1") == 0
6573 || strcmp (mo->name, "swc2") == 0
6574 || strcmp (mo->name, "sw") == 0
6575 || strcmp (mo->name, "sc") == 0
6576 || strcmp (mo->name, "s.s") == 0)
6577 return 4;
6578
6579 if (strcmp (mo->name, "sdc1") == 0
6580 || strcmp (mo->name, "sdc2") == 0
6581 || strcmp (mo->name, "s.d") == 0)
6582 return 8;
6583
6584 /* sb, swl, swr */
6585 return 1;
6586}
6587
6588struct fix_24k_store_info
6589 {
6590 /* Immediate offset, if any, for this store instruction. */
6591 short off;
6592 /* Alignment required by this store instruction. */
6593 int align_to;
6594 /* True for register offsets. */
6595 int register_offset;
6596 };
6597
6598/* Comparison function used by qsort. */
6599
6600static int
6601fix_24k_sort (const void *a, const void *b)
6602{
6603 const struct fix_24k_store_info *pos1 = a;
6604 const struct fix_24k_store_info *pos2 = b;
6605
6606 return (pos1->off - pos2->off);
6607}
6608
6609/* INSN is a store instruction. Try to record the store information
6610 in STINFO. Return false if the information isn't known. */
6611
6612static bfd_boolean
6613fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
ab9794cf 6614 const struct mips_cl_insn *insn)
15be625d
CM
6615{
6616 /* The instruction must have a known offset. */
6617 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
6618 return FALSE;
6619
6620 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
6621 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
6622 return TRUE;
6623}
6624
932d1a1b
RS
6625/* Return the number of nops that would be needed to work around the 24k
6626 "lost data on stores during refill" errata if instruction INSN
6627 immediately followed the 2 instructions described by HIST.
6628 Ignore hazards that are contained within the first IGNORE
6629 instructions of HIST.
6630
6631 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
6632 for the data cache refills and store data. The following describes
6633 the scenario where the store data could be lost.
6634
6635 * A data cache miss, due to either a load or a store, causing fill
6636 data to be supplied by the memory subsystem
6637 * The first three doublewords of fill data are returned and written
6638 into the cache
6639 * A sequence of four stores occurs in consecutive cycles around the
6640 final doubleword of the fill:
6641 * Store A
6642 * Store B
6643 * Store C
6644 * Zero, One or more instructions
6645 * Store D
6646
6647 The four stores A-D must be to different doublewords of the line that
6648 is being filled. The fourth instruction in the sequence above permits
6649 the fill of the final doubleword to be transferred from the FSB into
6650 the cache. In the sequence above, the stores may be either integer
6651 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
6652 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
6653 different doublewords on the line. If the floating point unit is
6654 running in 1:2 mode, it is not possible to create the sequence above
6655 using only floating point store instructions.
15be625d
CM
6656
6657 In this case, the cache line being filled is incorrectly marked
6658 invalid, thereby losing the data from any store to the line that
6659 occurs between the original miss and the completion of the five
6660 cycle sequence shown above.
6661
932d1a1b 6662 The workarounds are:
15be625d 6663
932d1a1b
RS
6664 * Run the data cache in write-through mode.
6665 * Insert a non-store instruction between
6666 Store A and Store B or Store B and Store C. */
3739860c 6667
15be625d 6668static int
932d1a1b 6669nops_for_24k (int ignore, const struct mips_cl_insn *hist,
15be625d
CM
6670 const struct mips_cl_insn *insn)
6671{
6672 struct fix_24k_store_info pos[3];
6673 int align, i, base_offset;
6674
932d1a1b
RS
6675 if (ignore >= 2)
6676 return 0;
6677
ab9794cf
RS
6678 /* If the previous instruction wasn't a store, there's nothing to
6679 worry about. */
15be625d
CM
6680 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6681 return 0;
6682
ab9794cf
RS
6683 /* If the instructions after the previous one are unknown, we have
6684 to assume the worst. */
6685 if (!insn)
15be625d
CM
6686 return 1;
6687
ab9794cf
RS
6688 /* Check whether we are dealing with three consecutive stores. */
6689 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
6690 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
15be625d
CM
6691 return 0;
6692
6693 /* If we don't know the relationship between the store addresses,
6694 assume the worst. */
ab9794cf 6695 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
15be625d
CM
6696 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
6697 return 1;
6698
6699 if (!fix_24k_record_store_info (&pos[0], insn)
6700 || !fix_24k_record_store_info (&pos[1], &hist[0])
6701 || !fix_24k_record_store_info (&pos[2], &hist[1]))
6702 return 1;
6703
6704 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
6705
6706 /* Pick a value of ALIGN and X such that all offsets are adjusted by
6707 X bytes and such that the base register + X is known to be aligned
6708 to align bytes. */
6709
6710 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
6711 align = 8;
6712 else
6713 {
6714 align = pos[0].align_to;
6715 base_offset = pos[0].off;
6716 for (i = 1; i < 3; i++)
6717 if (align < pos[i].align_to)
6718 {
6719 align = pos[i].align_to;
6720 base_offset = pos[i].off;
6721 }
6722 for (i = 0; i < 3; i++)
6723 pos[i].off -= base_offset;
6724 }
6725
6726 pos[0].off &= ~align + 1;
6727 pos[1].off &= ~align + 1;
6728 pos[2].off &= ~align + 1;
6729
6730 /* If any two stores write to the same chunk, they also write to the
6731 same doubleword. The offsets are still sorted at this point. */
6732 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
6733 return 0;
6734
6735 /* A range of at least 9 bytes is needed for the stores to be in
6736 non-overlapping doublewords. */
6737 if (pos[2].off - pos[0].off <= 8)
6738 return 0;
6739
6740 if (pos[2].off - pos[1].off >= 24
6741 || pos[1].off - pos[0].off >= 24
6742 || pos[2].off - pos[0].off >= 32)
6743 return 0;
6744
6745 return 1;
6746}
6747
71400594 6748/* Return the number of nops that would be needed if instruction INSN
91d6fa6a 6749 immediately followed the MAX_NOPS instructions given by HIST,
932d1a1b
RS
6750 where HIST[0] is the most recent instruction. Ignore hazards
6751 between INSN and the first IGNORE instructions in HIST.
6752
6753 If INSN is null, return the worse-case number of nops for any
6754 instruction. */
bdaaa2e1 6755
71400594 6756static int
932d1a1b 6757nops_for_insn (int ignore, const struct mips_cl_insn *hist,
71400594
RS
6758 const struct mips_cl_insn *insn)
6759{
6760 int i, nops, tmp_nops;
bdaaa2e1 6761
71400594 6762 nops = 0;
932d1a1b 6763 for (i = ignore; i < MAX_DELAY_NOPS; i++)
65b02341 6764 {
91d6fa6a 6765 tmp_nops = insns_between (hist + i, insn) - i;
65b02341
RS
6766 if (tmp_nops > nops)
6767 nops = tmp_nops;
6768 }
7d8e00cf 6769
df58fc94 6770 if (mips_fix_vr4130 && !mips_opts.micromips)
7d8e00cf 6771 {
932d1a1b 6772 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
7d8e00cf
RS
6773 if (tmp_nops > nops)
6774 nops = tmp_nops;
6775 }
6776
df58fc94 6777 if (mips_fix_24k && !mips_opts.micromips)
15be625d 6778 {
932d1a1b 6779 tmp_nops = nops_for_24k (ignore, hist, insn);
15be625d
CM
6780 if (tmp_nops > nops)
6781 nops = tmp_nops;
6782 }
6783
71400594
RS
6784 return nops;
6785}
252b5132 6786
71400594 6787/* The variable arguments provide NUM_INSNS extra instructions that
91d6fa6a 6788 might be added to HIST. Return the largest number of nops that
932d1a1b
RS
6789 would be needed after the extended sequence, ignoring hazards
6790 in the first IGNORE instructions. */
252b5132 6791
71400594 6792static int
932d1a1b
RS
6793nops_for_sequence (int num_insns, int ignore,
6794 const struct mips_cl_insn *hist, ...)
71400594
RS
6795{
6796 va_list args;
6797 struct mips_cl_insn buffer[MAX_NOPS];
6798 struct mips_cl_insn *cursor;
6799 int nops;
6800
91d6fa6a 6801 va_start (args, hist);
71400594 6802 cursor = buffer + num_insns;
91d6fa6a 6803 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
71400594
RS
6804 while (cursor > buffer)
6805 *--cursor = *va_arg (args, const struct mips_cl_insn *);
6806
932d1a1b 6807 nops = nops_for_insn (ignore, buffer, NULL);
71400594
RS
6808 va_end (args);
6809 return nops;
6810}
252b5132 6811
71400594
RS
6812/* Like nops_for_insn, but if INSN is a branch, take into account the
6813 worst-case delay for the branch target. */
252b5132 6814
71400594 6815static int
932d1a1b 6816nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
71400594
RS
6817 const struct mips_cl_insn *insn)
6818{
6819 int nops, tmp_nops;
60b63b72 6820
932d1a1b 6821 nops = nops_for_insn (ignore, hist, insn);
11625dd8 6822 if (delayed_branch_p (insn))
71400594 6823 {
932d1a1b 6824 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
14fe068b 6825 hist, insn, get_delay_slot_nop (insn));
71400594
RS
6826 if (tmp_nops > nops)
6827 nops = tmp_nops;
6828 }
11625dd8 6829 else if (compact_branch_p (insn))
71400594 6830 {
932d1a1b 6831 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
71400594
RS
6832 if (tmp_nops > nops)
6833 nops = tmp_nops;
6834 }
6835 return nops;
6836}
6837
c67a084a
NC
6838/* Fix NOP issue: Replace nops by "or at,at,zero". */
6839
6840static void
6841fix_loongson2f_nop (struct mips_cl_insn * ip)
6842{
df58fc94 6843 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
6844 if (strcmp (ip->insn_mo->name, "nop") == 0)
6845 ip->insn_opcode = LOONGSON2F_NOP_INSN;
6846}
6847
6848/* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
6849 jr target pc &= 'hffff_ffff_cfff_ffff. */
6850
6851static void
6852fix_loongson2f_jump (struct mips_cl_insn * ip)
6853{
df58fc94 6854 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
6855 if (strcmp (ip->insn_mo->name, "j") == 0
6856 || strcmp (ip->insn_mo->name, "jr") == 0
6857 || strcmp (ip->insn_mo->name, "jalr") == 0)
6858 {
6859 int sreg;
6860 expressionS ep;
6861
6862 if (! mips_opts.at)
6863 return;
6864
df58fc94 6865 sreg = EXTRACT_OPERAND (0, RS, *ip);
c67a084a
NC
6866 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
6867 return;
6868
6869 ep.X_op = O_constant;
6870 ep.X_add_number = 0xcfff0000;
6871 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
6872 ep.X_add_number = 0xffff;
6873 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
6874 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
6875 }
6876}
6877
6878static void
6879fix_loongson2f (struct mips_cl_insn * ip)
6880{
6881 if (mips_fix_loongson2f_nop)
6882 fix_loongson2f_nop (ip);
6883
6884 if (mips_fix_loongson2f_jump)
6885 fix_loongson2f_jump (ip);
6886}
6887
dec7b24b
YS
6888static bfd_boolean
6889has_label_name (const char *arr[], size_t len ,const char *s)
6890{
6891 unsigned long i;
6892 for (i = 0; i < len; i++)
6893 {
6894 if (!arr[i])
6895 return FALSE;
6896 if (streq (arr[i], s))
6897 return TRUE;
6898 }
6899 return FALSE;
6900}
6901
6902/* Fix loongson3 llsc errata: Insert sync before ll/lld. */
6f2117ba
PH
6903
6904static void
6905fix_loongson3_llsc (struct mips_cl_insn * ip)
6906{
6907 gas_assert (!HAVE_CODE_COMPRESSION);
6908
6909 /* If is an local label and the insn is not sync,
6910 look forward that whether an branch between ll/sc jump to here
6911 if so, insert a sync. */
6912 if (seg_info (now_seg)->label_list
6913 && S_IS_LOCAL (seg_info (now_seg)->label_list->label)
6914 && (strcmp (ip->insn_mo->name, "sync") != 0))
6915 {
6f2117ba 6916 unsigned long i;
dec7b24b
YS
6917 valueT label_value;
6918 const char *label_names[MAX_LABELS_SAME];
6919 const char *label_name;
6920
6921 label_name = S_GET_NAME (seg_info (now_seg)->label_list->label);
6922 label_names[0] = label_name;
6923 struct insn_label_list *llist = seg_info (now_seg)->label_list;
6924 label_value = S_GET_VALUE (llist->label);
6f2117ba 6925
dec7b24b
YS
6926 for (i = 1; i < MAX_LABELS_SAME; i++)
6927 {
6928 llist = llist->next;
6929 if (!llist)
6930 break;
6931 if (S_GET_VALUE (llist->label) == label_value)
6932 label_names[i] = S_GET_NAME (llist->label);
6933 else
6934 break;
6935 }
6936 for (; i < MAX_LABELS_SAME; i++)
6937 label_names[i] = NULL;
6938
6939 unsigned long lookback = ARRAY_SIZE (history);
6f2117ba
PH
6940 for (i = 0; i < lookback; i++)
6941 {
6942 if (streq (history[i].insn_mo->name, "ll")
6943 || streq (history[i].insn_mo->name, "lld"))
6944 break;
6945
6946 if (streq (history[i].insn_mo->name, "sc")
6947 || streq (history[i].insn_mo->name, "scd"))
6948 {
6949 unsigned long j;
6950
6951 for (j = i + 1; j < lookback; j++)
6952 {
6953 if (streq (history[i].insn_mo->name, "ll")
6954 || streq (history[i].insn_mo->name, "lld"))
6955 break;
6956
6957 if (delayed_branch_p (&history[j]))
6958 {
dec7b24b
YS
6959 if (has_label_name (label_names,
6960 MAX_LABELS_SAME,
6961 history[j].target))
6f2117ba
PH
6962 {
6963 add_fixed_insn (&sync_insn);
6964 insert_into_history (0, 1, &sync_insn);
6965 i = lookback;
6966 break;
6967 }
6968 }
6969 }
6970 }
6971 }
6972 }
6973 /* If we find a sc, we look forward to look for an branch insn,
6974 and see whether it jump back and out of ll/sc. */
dec7b24b 6975 else if (streq (ip->insn_mo->name, "sc") || streq (ip->insn_mo->name, "scd"))
6f2117ba
PH
6976 {
6977 unsigned long lookback = ARRAY_SIZE (history) - 1;
6978 unsigned long i;
6979
6980 for (i = 0; i < lookback; i++)
6981 {
6982 if (streq (history[i].insn_mo->name, "ll")
6983 || streq (history[i].insn_mo->name, "lld"))
6984 break;
6985
6986 if (delayed_branch_p (&history[i]))
6987 {
6988 unsigned long j;
6989
6990 for (j = i + 1; j < lookback; j++)
6991 {
6992 if (streq (history[j].insn_mo->name, "ll")
6993 || streq (history[i].insn_mo->name, "lld"))
6994 break;
6995 }
6996
6997 for (; j < lookback; j++)
6998 {
6999 if (history[j].label[0] != '\0'
7000 && streq (history[j].label, history[i].target)
7001 && strcmp (history[j+1].insn_mo->name, "sync") != 0)
7002 {
7003 add_fixed_insn (&sync_insn);
7004 insert_into_history (++j, 1, &sync_insn);
7005 }
7006 }
7007 }
7008 }
7009 }
7010
7011 /* Skip if there is a sync before ll/lld. */
7012 if ((strcmp (ip->insn_mo->name, "ll") == 0
7013 || strcmp (ip->insn_mo->name, "lld") == 0)
7014 && (strcmp (history[0].insn_mo->name, "sync") != 0))
7015 {
7016 add_fixed_insn (&sync_insn);
7017 insert_into_history (0, 1, &sync_insn);
7018 }
7019}
7020
a4e06468
RS
7021/* IP is a branch that has a delay slot, and we need to fill it
7022 automatically. Return true if we can do that by swapping IP
e407c74b
NC
7023 with the previous instruction.
7024 ADDRESS_EXPR is an operand of the instruction to be used with
7025 RELOC_TYPE. */
a4e06468
RS
7026
7027static bfd_boolean
e407c74b 7028can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 7029 bfd_reloc_code_real_type *reloc_type)
a4e06468 7030{
2b0c8b40 7031 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
a4e06468 7032 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
9d5de888 7033 unsigned int fpr_read, prev_fpr_write;
a4e06468
RS
7034
7035 /* -O2 and above is required for this optimization. */
7036 if (mips_optimize < 2)
7037 return FALSE;
7038
7039 /* If we have seen .set volatile or .set nomove, don't optimize. */
7040 if (mips_opts.nomove)
7041 return FALSE;
7042
7043 /* We can't swap if the previous instruction's position is fixed. */
7044 if (history[0].fixed_p)
7045 return FALSE;
7046
7047 /* If the previous previous insn was in a .set noreorder, we can't
7048 swap. Actually, the MIPS assembler will swap in this situation.
7049 However, gcc configured -with-gnu-as will generate code like
7050
7051 .set noreorder
7052 lw $4,XXX
7053 .set reorder
7054 INSN
7055 bne $4,$0,foo
7056
7057 in which we can not swap the bne and INSN. If gcc is not configured
7058 -with-gnu-as, it does not output the .set pseudo-ops. */
7059 if (history[1].noreorder_p)
7060 return FALSE;
7061
87333bb7
MR
7062 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
7063 This means that the previous instruction was a 4-byte one anyhow. */
a4e06468
RS
7064 if (mips_opts.mips16 && history[0].fixp[0])
7065 return FALSE;
7066
7067 /* If the branch is itself the target of a branch, we can not swap.
7068 We cheat on this; all we check for is whether there is a label on
7069 this instruction. If there are any branches to anything other than
7070 a label, users must use .set noreorder. */
7071 if (seg_info (now_seg)->label_list)
7072 return FALSE;
7073
7074 /* If the previous instruction is in a variant frag other than this
2309ddf2 7075 branch's one, we cannot do the swap. This does not apply to
9301f9c3
MR
7076 MIPS16 code, which uses variant frags for different purposes. */
7077 if (!mips_opts.mips16
a4e06468
RS
7078 && history[0].frag
7079 && history[0].frag->fr_type == rs_machine_dependent)
7080 return FALSE;
7081
bcd530a7
RS
7082 /* We do not swap with instructions that cannot architecturally
7083 be placed in a branch delay slot, such as SYNC or ERET. We
7084 also refrain from swapping with a trap instruction, since it
7085 complicates trap handlers to have the trap instruction be in
7086 a delay slot. */
a4e06468 7087 prev_pinfo = history[0].insn_mo->pinfo;
bcd530a7 7088 if (prev_pinfo & INSN_NO_DELAY_SLOT)
a4e06468
RS
7089 return FALSE;
7090
7091 /* Check for conflicts between the branch and the instructions
7092 before the candidate delay slot. */
7093 if (nops_for_insn (0, history + 1, ip) > 0)
7094 return FALSE;
7095
7096 /* Check for conflicts between the swapped sequence and the
7097 target of the branch. */
7098 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
7099 return FALSE;
7100
7101 /* If the branch reads a register that the previous
7102 instruction sets, we can not swap. */
7103 gpr_read = gpr_read_mask (ip);
7104 prev_gpr_write = gpr_write_mask (&history[0]);
7105 if (gpr_read & prev_gpr_write)
7106 return FALSE;
7107
9d5de888
CF
7108 fpr_read = fpr_read_mask (ip);
7109 prev_fpr_write = fpr_write_mask (&history[0]);
7110 if (fpr_read & prev_fpr_write)
7111 return FALSE;
7112
a4e06468
RS
7113 /* If the branch writes a register that the previous
7114 instruction sets, we can not swap. */
7115 gpr_write = gpr_write_mask (ip);
7116 if (gpr_write & prev_gpr_write)
7117 return FALSE;
7118
7119 /* If the branch writes a register that the previous
7120 instruction reads, we can not swap. */
7121 prev_gpr_read = gpr_read_mask (&history[0]);
7122 if (gpr_write & prev_gpr_read)
7123 return FALSE;
7124
7125 /* If one instruction sets a condition code and the
7126 other one uses a condition code, we can not swap. */
7127 pinfo = ip->insn_mo->pinfo;
7128 if ((pinfo & INSN_READ_COND_CODE)
7129 && (prev_pinfo & INSN_WRITE_COND_CODE))
7130 return FALSE;
7131 if ((pinfo & INSN_WRITE_COND_CODE)
7132 && (prev_pinfo & INSN_READ_COND_CODE))
7133 return FALSE;
7134
7135 /* If the previous instruction uses the PC, we can not swap. */
2b0c8b40 7136 prev_pinfo2 = history[0].insn_mo->pinfo2;
26545944 7137 if (prev_pinfo2 & INSN2_READ_PC)
2b0c8b40 7138 return FALSE;
a4e06468 7139
df58fc94
RS
7140 /* If the previous instruction has an incorrect size for a fixed
7141 branch delay slot in microMIPS mode, we cannot swap. */
2309ddf2
MR
7142 pinfo2 = ip->insn_mo->pinfo2;
7143 if (mips_opts.micromips
7144 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
7145 && insn_length (history) != 2)
7146 return FALSE;
7147 if (mips_opts.micromips
7148 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
7149 && insn_length (history) != 4)
7150 return FALSE;
7151
33d64ca5
FN
7152 /* On the R5900 short loops need to be fixed by inserting a NOP in the
7153 branch delay slot.
7154
7155 The short loop bug under certain conditions causes loops to execute
7156 only once or twice. We must ensure that the assembler never
7157 generates loops that satisfy all of the following conditions:
7158
7159 - a loop consists of less than or equal to six instructions
7160 (including the branch delay slot);
7161 - a loop contains only one conditional branch instruction at the end
7162 of the loop;
7163 - a loop does not contain any other branch or jump instructions;
7164 - a branch delay slot of the loop is not NOP (EE 2.9 or later).
7165
7166 We need to do this because of a hardware bug in the R5900 chip. */
27c634e0 7167 if (mips_fix_r5900
e407c74b
NC
7168 /* Check if instruction has a parameter, ignore "j $31". */
7169 && (address_expr != NULL)
7170 /* Parameter must be 16 bit. */
7171 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
7172 /* Branch to same segment. */
41065f5e 7173 && (S_GET_SEGMENT (address_expr->X_add_symbol) == now_seg)
e407c74b 7174 /* Branch to same code fragment. */
41065f5e 7175 && (symbol_get_frag (address_expr->X_add_symbol) == frag_now)
e407c74b 7176 /* Can only calculate branch offset if value is known. */
41065f5e 7177 && symbol_constant_p (address_expr->X_add_symbol)
e407c74b
NC
7178 /* Check if branch is really conditional. */
7179 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
7180 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
7181 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
7182 {
7183 int distance;
33d64ca5
FN
7184 /* Check if loop is shorter than or equal to 6 instructions
7185 including branch and delay slot. */
41065f5e 7186 distance = frag_now_fix () - S_GET_VALUE (address_expr->X_add_symbol);
e407c74b
NC
7187 if (distance <= 20)
7188 {
7189 int i;
7190 int rv;
7191
7192 rv = FALSE;
7193 /* When the loop includes branches or jumps,
7194 it is not a short loop. */
7195 for (i = 0; i < (distance / 4); i++)
7196 {
7197 if ((history[i].cleared_p)
41065f5e 7198 || delayed_branch_p (&history[i]))
e407c74b
NC
7199 {
7200 rv = TRUE;
7201 break;
7202 }
7203 }
535b785f 7204 if (!rv)
e407c74b
NC
7205 {
7206 /* Insert nop after branch to fix short loop. */
7207 return FALSE;
7208 }
7209 }
7210 }
7211
a4e06468
RS
7212 return TRUE;
7213}
7214
e407c74b
NC
7215/* Decide how we should add IP to the instruction stream.
7216 ADDRESS_EXPR is an operand of the instruction to be used with
7217 RELOC_TYPE. */
a4e06468
RS
7218
7219static enum append_method
e407c74b 7220get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 7221 bfd_reloc_code_real_type *reloc_type)
a4e06468 7222{
a4e06468
RS
7223 /* The relaxed version of a macro sequence must be inherently
7224 hazard-free. */
7225 if (mips_relax.sequence == 2)
7226 return APPEND_ADD;
7227
3b821a28 7228 /* We must not dabble with instructions in a ".set noreorder" block. */
a4e06468
RS
7229 if (mips_opts.noreorder)
7230 return APPEND_ADD;
7231
7232 /* Otherwise, it's our responsibility to fill branch delay slots. */
11625dd8 7233 if (delayed_branch_p (ip))
a4e06468 7234 {
e407c74b
NC
7235 if (!branch_likely_p (ip)
7236 && can_swap_branch_p (ip, address_expr, reloc_type))
a4e06468
RS
7237 return APPEND_SWAP;
7238
7239 if (mips_opts.mips16
7240 && ISA_SUPPORTS_MIPS16E
fc76e730 7241 && gpr_read_mask (ip) != 0)
a4e06468
RS
7242 return APPEND_ADD_COMPACT;
7243
7bd374a4
MR
7244 if (mips_opts.micromips
7245 && ((ip->insn_opcode & 0xffe0) == 0x4580
7246 || (!forced_insn_length
7247 && ((ip->insn_opcode & 0xfc00) == 0xcc00
7248 || (ip->insn_opcode & 0xdc00) == 0x8c00))
7249 || (ip->insn_opcode & 0xdfe00000) == 0x94000000
7250 || (ip->insn_opcode & 0xdc1f0000) == 0x94000000))
7251 return APPEND_ADD_COMPACT;
7252
a4e06468
RS
7253 return APPEND_ADD_WITH_NOP;
7254 }
7255
a4e06468
RS
7256 return APPEND_ADD;
7257}
7258
7bd374a4
MR
7259/* IP is an instruction whose opcode we have just changed, END points
7260 to the end of the opcode table processed. Point IP->insn_mo to the
7261 new opcode's definition. */
ceb94aa5
RS
7262
7263static void
7bd374a4 7264find_altered_opcode (struct mips_cl_insn *ip, const struct mips_opcode *end)
ceb94aa5 7265{
7bd374a4 7266 const struct mips_opcode *mo;
ceb94aa5 7267
ceb94aa5 7268 for (mo = ip->insn_mo; mo < end; mo++)
7bd374a4
MR
7269 if (mo->pinfo != INSN_MACRO
7270 && (ip->insn_opcode & mo->mask) == mo->match)
ceb94aa5
RS
7271 {
7272 ip->insn_mo = mo;
7273 return;
7274 }
7275 abort ();
7276}
7277
7bd374a4
MR
7278/* IP is a MIPS16 instruction whose opcode we have just changed.
7279 Point IP->insn_mo to the new opcode's definition. */
7280
7281static void
7282find_altered_mips16_opcode (struct mips_cl_insn *ip)
7283{
7284 find_altered_opcode (ip, &mips16_opcodes[bfd_mips16_num_opcodes]);
7285}
7286
7287/* IP is a microMIPS instruction whose opcode we have just changed.
7288 Point IP->insn_mo to the new opcode's definition. */
7289
7290static void
7291find_altered_micromips_opcode (struct mips_cl_insn *ip)
7292{
7293 find_altered_opcode (ip, &micromips_opcodes[bfd_micromips_num_opcodes]);
7294}
7295
df58fc94
RS
7296/* For microMIPS macros, we need to generate a local number label
7297 as the target of branches. */
7298#define MICROMIPS_LABEL_CHAR '\037'
7299static unsigned long micromips_target_label;
7300static char micromips_target_name[32];
7301
7302static char *
7303micromips_label_name (void)
7304{
7305 char *p = micromips_target_name;
7306 char symbol_name_temporary[24];
7307 unsigned long l;
7308 int i;
7309
7310 if (*p)
7311 return p;
7312
7313 i = 0;
7314 l = micromips_target_label;
7315#ifdef LOCAL_LABEL_PREFIX
7316 *p++ = LOCAL_LABEL_PREFIX;
7317#endif
7318 *p++ = 'L';
7319 *p++ = MICROMIPS_LABEL_CHAR;
7320 do
7321 {
7322 symbol_name_temporary[i++] = l % 10 + '0';
7323 l /= 10;
7324 }
7325 while (l != 0);
7326 while (i > 0)
7327 *p++ = symbol_name_temporary[--i];
7328 *p = '\0';
7329
7330 return micromips_target_name;
7331}
7332
7333static void
7334micromips_label_expr (expressionS *label_expr)
7335{
7336 label_expr->X_op = O_symbol;
7337 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
7338 label_expr->X_add_number = 0;
7339}
7340
7341static void
7342micromips_label_inc (void)
7343{
7344 micromips_target_label++;
7345 *micromips_target_name = '\0';
7346}
7347
7348static void
7349micromips_add_label (void)
7350{
7351 symbolS *s;
7352
7353 s = colon (micromips_label_name ());
7354 micromips_label_inc ();
f3ded42a 7355 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
df58fc94
RS
7356}
7357
7358/* If assembling microMIPS code, then return the microMIPS reloc
7359 corresponding to the requested one if any. Otherwise return
7360 the reloc unchanged. */
7361
7362static bfd_reloc_code_real_type
7363micromips_map_reloc (bfd_reloc_code_real_type reloc)
7364{
7365 static const bfd_reloc_code_real_type relocs[][2] =
7366 {
7367 /* Keep sorted incrementally by the left-hand key. */
7368 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
7369 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
7370 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
7371 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
7372 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
7373 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
7374 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
7375 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
7376 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
7377 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
7378 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
7379 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
7380 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
7381 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
7382 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
7383 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
7384 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
7385 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
7386 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
7387 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
7388 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
7389 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
7390 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
7391 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
7392 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
7393 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
7394 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
7395 };
7396 bfd_reloc_code_real_type r;
7397 size_t i;
7398
7399 if (!mips_opts.micromips)
7400 return reloc;
7401 for (i = 0; i < ARRAY_SIZE (relocs); i++)
7402 {
7403 r = relocs[i][0];
7404 if (r > reloc)
7405 return reloc;
7406 if (r == reloc)
7407 return relocs[i][1];
7408 }
7409 return reloc;
7410}
7411
b886a2ab
RS
7412/* Try to resolve relocation RELOC against constant OPERAND at assembly time.
7413 Return true on success, storing the resolved value in RESULT. */
7414
7415static bfd_boolean
7416calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
7417 offsetT *result)
7418{
7419 switch (reloc)
7420 {
7421 case BFD_RELOC_MIPS_HIGHEST:
7422 case BFD_RELOC_MICROMIPS_HIGHEST:
7423 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
7424 return TRUE;
7425
7426 case BFD_RELOC_MIPS_HIGHER:
7427 case BFD_RELOC_MICROMIPS_HIGHER:
7428 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
7429 return TRUE;
7430
7431 case BFD_RELOC_HI16_S:
41947d9e 7432 case BFD_RELOC_HI16_S_PCREL:
b886a2ab
RS
7433 case BFD_RELOC_MICROMIPS_HI16_S:
7434 case BFD_RELOC_MIPS16_HI16_S:
7435 *result = ((operand + 0x8000) >> 16) & 0xffff;
7436 return TRUE;
7437
7438 case BFD_RELOC_HI16:
7439 case BFD_RELOC_MICROMIPS_HI16:
7440 case BFD_RELOC_MIPS16_HI16:
7441 *result = (operand >> 16) & 0xffff;
7442 return TRUE;
7443
7444 case BFD_RELOC_LO16:
41947d9e 7445 case BFD_RELOC_LO16_PCREL:
b886a2ab
RS
7446 case BFD_RELOC_MICROMIPS_LO16:
7447 case BFD_RELOC_MIPS16_LO16:
7448 *result = operand & 0xffff;
7449 return TRUE;
7450
7451 case BFD_RELOC_UNUSED:
7452 *result = operand;
7453 return TRUE;
7454
7455 default:
7456 return FALSE;
7457 }
7458}
7459
71400594
RS
7460/* Output an instruction. IP is the instruction information.
7461 ADDRESS_EXPR is an operand of the instruction to be used with
df58fc94
RS
7462 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
7463 a macro expansion. */
71400594
RS
7464
7465static void
7466append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
df58fc94 7467 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
71400594 7468{
14fe068b 7469 unsigned long prev_pinfo2, pinfo;
71400594 7470 bfd_boolean relaxed_branch = FALSE;
a4e06468 7471 enum append_method method;
2309ddf2 7472 bfd_boolean relax32;
2b0c8b40 7473 int branch_disp;
71400594 7474
2309ddf2 7475 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
c67a084a
NC
7476 fix_loongson2f (ip);
7477
6f2117ba
PH
7478 ip->target[0] = '\0';
7479 if (offset_expr.X_op == O_symbol)
7480 strncpy (ip->target, S_GET_NAME (offset_expr.X_add_symbol), 15);
7481 ip->label[0] = '\0';
7482 if (seg_info (now_seg)->label_list)
7483 strncpy (ip->label, S_GET_NAME (seg_info (now_seg)->label_list->label), 15);
7484 if (mips_fix_loongson3_llsc && !HAVE_CODE_COMPRESSION)
7485 fix_loongson3_llsc (ip);
7486
738f4d98 7487 file_ase_mips16 |= mips_opts.mips16;
df58fc94 7488 file_ase_micromips |= mips_opts.micromips;
738f4d98 7489
df58fc94 7490 prev_pinfo2 = history[0].insn_mo->pinfo2;
71400594 7491 pinfo = ip->insn_mo->pinfo;
df58fc94 7492
7bd374a4
MR
7493 /* Don't raise alarm about `nods' frags as they'll fill in the right
7494 kind of nop in relaxation if required. */
df58fc94
RS
7495 if (mips_opts.micromips
7496 && !expansionp
7bd374a4
MR
7497 && !(history[0].frag
7498 && history[0].frag->fr_type == rs_machine_dependent
7499 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
7500 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
df58fc94
RS
7501 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
7502 && micromips_insn_length (ip->insn_mo) != 2)
7503 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
7504 && micromips_insn_length (ip->insn_mo) != 4)))
1661c76c 7505 as_warn (_("wrong size instruction in a %u-bit branch delay slot"),
df58fc94 7506 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
71400594 7507
15be625d
CM
7508 if (address_expr == NULL)
7509 ip->complete_p = 1;
b886a2ab
RS
7510 else if (reloc_type[0] <= BFD_RELOC_UNUSED
7511 && reloc_type[1] == BFD_RELOC_UNUSED
7512 && reloc_type[2] == BFD_RELOC_UNUSED
15be625d
CM
7513 && address_expr->X_op == O_constant)
7514 {
15be625d
CM
7515 switch (*reloc_type)
7516 {
15be625d 7517 case BFD_RELOC_MIPS_JMP:
df58fc94
RS
7518 {
7519 int shift;
7520
17c6c9d9
MR
7521 /* Shift is 2, unusually, for microMIPS JALX. */
7522 shift = (mips_opts.micromips
7523 && strcmp (ip->insn_mo->name, "jalx") != 0) ? 1 : 2;
df58fc94
RS
7524 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7525 as_bad (_("jump to misaligned address (0x%lx)"),
7526 (unsigned long) address_expr->X_add_number);
7527 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7528 & 0x3ffffff);
335574df 7529 ip->complete_p = 1;
df58fc94 7530 }
15be625d
CM
7531 break;
7532
7533 case BFD_RELOC_MIPS16_JMP:
7534 if ((address_expr->X_add_number & 3) != 0)
7535 as_bad (_("jump to misaligned address (0x%lx)"),
7536 (unsigned long) address_expr->X_add_number);
7537 ip->insn_opcode |=
7538 (((address_expr->X_add_number & 0x7c0000) << 3)
7539 | ((address_expr->X_add_number & 0xf800000) >> 7)
7540 | ((address_expr->X_add_number & 0x3fffc) >> 2));
335574df 7541 ip->complete_p = 1;
15be625d
CM
7542 break;
7543
7544 case BFD_RELOC_16_PCREL_S2:
df58fc94
RS
7545 {
7546 int shift;
7547
7548 shift = mips_opts.micromips ? 1 : 2;
7549 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7550 as_bad (_("branch to misaligned address (0x%lx)"),
7551 (unsigned long) address_expr->X_add_number);
7552 if (!mips_relax_branch)
7553 {
7554 if ((address_expr->X_add_number + (1 << (shift + 15)))
7555 & ~((1 << (shift + 16)) - 1))
7556 as_bad (_("branch address range overflow (0x%lx)"),
7557 (unsigned long) address_expr->X_add_number);
7558 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7559 & 0xffff);
7560 }
df58fc94 7561 }
15be625d
CM
7562 break;
7563
7361da2c
AB
7564 case BFD_RELOC_MIPS_21_PCREL_S2:
7565 {
7566 int shift;
7567
7568 shift = 2;
7569 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7570 as_bad (_("branch to misaligned address (0x%lx)"),
7571 (unsigned long) address_expr->X_add_number);
7572 if ((address_expr->X_add_number + (1 << (shift + 20)))
7573 & ~((1 << (shift + 21)) - 1))
7574 as_bad (_("branch address range overflow (0x%lx)"),
7575 (unsigned long) address_expr->X_add_number);
7576 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7577 & 0x1fffff);
7578 }
7579 break;
7580
7581 case BFD_RELOC_MIPS_26_PCREL_S2:
7582 {
7583 int shift;
7584
7585 shift = 2;
7586 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7587 as_bad (_("branch to misaligned address (0x%lx)"),
7588 (unsigned long) address_expr->X_add_number);
7589 if ((address_expr->X_add_number + (1 << (shift + 25)))
7590 & ~((1 << (shift + 26)) - 1))
7591 as_bad (_("branch address range overflow (0x%lx)"),
7592 (unsigned long) address_expr->X_add_number);
7593 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7594 & 0x3ffffff);
7595 }
7596 break;
7597
15be625d 7598 default:
b886a2ab
RS
7599 {
7600 offsetT value;
7601
7602 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
7603 &value))
7604 {
7605 ip->insn_opcode |= value & 0xffff;
7606 ip->complete_p = 1;
7607 }
7608 }
7609 break;
7610 }
15be625d
CM
7611 }
7612
71400594
RS
7613 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
7614 {
7615 /* There are a lot of optimizations we could do that we don't.
7616 In particular, we do not, in general, reorder instructions.
7617 If you use gcc with optimization, it will reorder
7618 instructions and generally do much more optimization then we
7619 do here; repeating all that work in the assembler would only
7620 benefit hand written assembly code, and does not seem worth
7621 it. */
7622 int nops = (mips_optimize == 0
932d1a1b
RS
7623 ? nops_for_insn (0, history, NULL)
7624 : nops_for_insn_or_target (0, history, ip));
71400594 7625 if (nops > 0)
252b5132
RH
7626 {
7627 fragS *old_frag;
7628 unsigned long old_frag_offset;
7629 int i;
252b5132
RH
7630
7631 old_frag = frag_now;
7632 old_frag_offset = frag_now_fix ();
7633
7634 for (i = 0; i < nops; i++)
14fe068b
RS
7635 add_fixed_insn (NOP_INSN);
7636 insert_into_history (0, nops, NOP_INSN);
252b5132
RH
7637
7638 if (listing)
7639 {
7640 listing_prev_line ();
7641 /* We may be at the start of a variant frag. In case we
7642 are, make sure there is enough space for the frag
7643 after the frags created by listing_prev_line. The
7644 argument to frag_grow here must be at least as large
7645 as the argument to all other calls to frag_grow in
7646 this file. We don't have to worry about being in the
7647 middle of a variant frag, because the variants insert
7648 all needed nop instructions themselves. */
7649 frag_grow (40);
7650 }
7651
462427c4 7652 mips_move_text_labels ();
252b5132
RH
7653
7654#ifndef NO_ECOFF_DEBUGGING
7655 if (ECOFF_DEBUGGING)
7656 ecoff_fix_loc (old_frag, old_frag_offset);
7657#endif
7658 }
71400594
RS
7659 }
7660 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
7661 {
932d1a1b
RS
7662 int nops;
7663
7664 /* Work out how many nops in prev_nop_frag are needed by IP,
7665 ignoring hazards generated by the first prev_nop_frag_since
7666 instructions. */
7667 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
9c2799c2 7668 gas_assert (nops <= prev_nop_frag_holds);
252b5132 7669
71400594
RS
7670 /* Enforce NOPS as a minimum. */
7671 if (nops > prev_nop_frag_required)
7672 prev_nop_frag_required = nops;
252b5132 7673
71400594
RS
7674 if (prev_nop_frag_holds == prev_nop_frag_required)
7675 {
7676 /* Settle for the current number of nops. Update the history
7677 accordingly (for the benefit of any future .set reorder code). */
7678 prev_nop_frag = NULL;
7679 insert_into_history (prev_nop_frag_since,
7680 prev_nop_frag_holds, NOP_INSN);
7681 }
7682 else
7683 {
7684 /* Allow this instruction to replace one of the nops that was
7685 tentatively added to prev_nop_frag. */
df58fc94 7686 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
71400594
RS
7687 prev_nop_frag_holds--;
7688 prev_nop_frag_since++;
252b5132
RH
7689 }
7690 }
7691
e407c74b 7692 method = get_append_method (ip, address_expr, reloc_type);
2b0c8b40 7693 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
a4e06468 7694
e410add4
RS
7695 dwarf2_emit_insn (0);
7696 /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
7697 so "move" the instruction address accordingly.
7698
7699 Also, it doesn't seem appropriate for the assembler to reorder .loc
7700 entries. If this instruction is a branch that we are going to swap
7701 with the previous instruction, the two instructions should be
7702 treated as a unit, and the debug information for both instructions
7703 should refer to the start of the branch sequence. Using the
7704 current position is certainly wrong when swapping a 32-bit branch
7705 and a 16-bit delay slot, since the current position would then be
7706 in the middle of a branch. */
7707 dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
58e2ea4d 7708
df58fc94
RS
7709 relax32 = (mips_relax_branch
7710 /* Don't try branch relaxation within .set nomacro, or within
7711 .set noat if we use $at for PIC computations. If it turns
7712 out that the branch was out-of-range, we'll get an error. */
7713 && !mips_opts.warn_about_macros
7714 && (mips_opts.at || mips_pic == NO_PIC)
3bf0dbfb
MR
7715 /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
7716 as they have no complementing branches. */
7717 && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
df58fc94
RS
7718
7719 if (!HAVE_CODE_COMPRESSION
7720 && address_expr
7721 && relax32
0b25d3e6 7722 && *reloc_type == BFD_RELOC_16_PCREL_S2
11625dd8 7723 && delayed_branch_p (ip))
4a6a3df4 7724 {
895921c9 7725 relaxed_branch = TRUE;
1e915849
RS
7726 add_relaxed_insn (ip, (relaxed_branch_length
7727 (NULL, NULL,
11625dd8
RS
7728 uncond_branch_p (ip) ? -1
7729 : branch_likely_p (ip) ? 1
1e915849
RS
7730 : 0)), 4,
7731 RELAX_BRANCH_ENCODE
ce8ad872 7732 (AT, mips_pic != NO_PIC,
11625dd8
RS
7733 uncond_branch_p (ip),
7734 branch_likely_p (ip),
1e915849
RS
7735 pinfo & INSN_WRITE_GPR_31,
7736 0),
7737 address_expr->X_add_symbol,
7738 address_expr->X_add_number);
4a6a3df4
AO
7739 *reloc_type = BFD_RELOC_UNUSED;
7740 }
df58fc94
RS
7741 else if (mips_opts.micromips
7742 && address_expr
7743 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
7744 || *reloc_type > BFD_RELOC_UNUSED)
40209cad
MR
7745 && (delayed_branch_p (ip) || compact_branch_p (ip))
7746 /* Don't try branch relaxation when users specify
7747 16-bit/32-bit instructions. */
7748 && !forced_insn_length)
df58fc94 7749 {
7bd374a4
MR
7750 bfd_boolean relax16 = (method != APPEND_ADD_COMPACT
7751 && *reloc_type > BFD_RELOC_UNUSED);
df58fc94 7752 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
11625dd8 7753 int uncond = uncond_branch_p (ip) ? -1 : 0;
7bd374a4
MR
7754 int compact = compact_branch_p (ip) || method == APPEND_ADD_COMPACT;
7755 int nods = method == APPEND_ADD_WITH_NOP;
df58fc94 7756 int al = pinfo & INSN_WRITE_GPR_31;
7bd374a4 7757 int length32 = nods ? 8 : 4;
df58fc94
RS
7758
7759 gas_assert (address_expr != NULL);
7760 gas_assert (!mips_relax.sequence);
7761
2b0c8b40 7762 relaxed_branch = TRUE;
7bd374a4
MR
7763 if (nods)
7764 method = APPEND_ADD;
7765 if (relax32)
7766 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
7767 add_relaxed_insn (ip, length32, relax16 ? 2 : 4,
8484fb75 7768 RELAX_MICROMIPS_ENCODE (type, AT, mips_opts.insn32,
ce8ad872 7769 mips_pic != NO_PIC,
7bd374a4 7770 uncond, compact, al, nods,
40209cad 7771 relax32, 0, 0),
df58fc94
RS
7772 address_expr->X_add_symbol,
7773 address_expr->X_add_number);
7774 *reloc_type = BFD_RELOC_UNUSED;
7775 }
7776 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
252b5132 7777 {
7fd53920
MR
7778 bfd_boolean require_unextended;
7779 bfd_boolean require_extended;
88a7ef16
MR
7780 symbolS *symbol;
7781 offsetT offset;
7782
7fd53920
MR
7783 if (forced_insn_length != 0)
7784 {
7785 require_unextended = forced_insn_length == 2;
7786 require_extended = forced_insn_length == 4;
7787 }
7788 else
7789 {
7790 require_unextended = (mips_opts.noautoextend
7791 && !mips_opcode_32bit_p (ip->insn_mo));
7792 require_extended = 0;
7793 }
7794
252b5132 7795 /* We need to set up a variant frag. */
df58fc94 7796 gas_assert (address_expr != NULL);
88a7ef16
MR
7797 /* Pass any `O_symbol' expression unchanged as an `expr_section'
7798 symbol created by `make_expr_symbol' may not get a necessary
7799 external relocation produced. */
7800 if (address_expr->X_op == O_symbol)
7801 {
7802 symbol = address_expr->X_add_symbol;
7803 offset = address_expr->X_add_number;
7804 }
7805 else
7806 {
7807 symbol = make_expr_symbol (address_expr);
82d808ed 7808 symbol_append (symbol, symbol_lastP, &symbol_rootP, &symbol_lastP);
88a7ef16
MR
7809 offset = 0;
7810 }
8507b6e7 7811 add_relaxed_insn (ip, 12, 0,
1e915849
RS
7812 RELAX_MIPS16_ENCODE
7813 (*reloc_type - BFD_RELOC_UNUSED,
25499ac7 7814 mips_opts.ase & ASE_MIPS16E2,
8507b6e7
MR
7815 mips_pic != NO_PIC,
7816 HAVE_32BIT_SYMBOLS,
7817 mips_opts.warn_about_macros,
7fd53920 7818 require_unextended, require_extended,
11625dd8 7819 delayed_branch_p (&history[0]),
1e915849 7820 history[0].mips16_absolute_jump_p),
88a7ef16 7821 symbol, offset);
252b5132 7822 }
5c04167a 7823 else if (mips_opts.mips16 && insn_length (ip) == 2)
9497f5ac 7824 {
11625dd8 7825 if (!delayed_branch_p (ip))
b8ee1a6e
DU
7826 /* Make sure there is enough room to swap this instruction with
7827 a following jump instruction. */
7828 frag_grow (6);
1e915849 7829 add_fixed_insn (ip);
252b5132
RH
7830 }
7831 else
7832 {
7833 if (mips_opts.mips16
7834 && mips_opts.noreorder
11625dd8 7835 && delayed_branch_p (&history[0]))
252b5132
RH
7836 as_warn (_("extended instruction in delay slot"));
7837
4d7206a2
RS
7838 if (mips_relax.sequence)
7839 {
7840 /* If we've reached the end of this frag, turn it into a variant
7841 frag and record the information for the instructions we've
7842 written so far. */
7843 if (frag_room () < 4)
7844 relax_close_frag ();
df58fc94 7845 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4d7206a2
RS
7846 }
7847
584892a6 7848 if (mips_relax.sequence != 2)
df58fc94
RS
7849 {
7850 if (mips_macro_warning.first_insn_sizes[0] == 0)
7851 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
7852 mips_macro_warning.sizes[0] += insn_length (ip);
7853 mips_macro_warning.insns[0]++;
7854 }
584892a6 7855 if (mips_relax.sequence != 1)
df58fc94
RS
7856 {
7857 if (mips_macro_warning.first_insn_sizes[1] == 0)
7858 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
7859 mips_macro_warning.sizes[1] += insn_length (ip);
7860 mips_macro_warning.insns[1]++;
7861 }
584892a6 7862
1e915849
RS
7863 if (mips_opts.mips16)
7864 {
7865 ip->fixed_p = 1;
7866 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
7867 }
7868 add_fixed_insn (ip);
252b5132
RH
7869 }
7870
9fe77896 7871 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
252b5132 7872 {
df58fc94 7873 bfd_reloc_code_real_type final_type[3];
2309ddf2 7874 reloc_howto_type *howto0;
9fe77896
RS
7875 reloc_howto_type *howto;
7876 int i;
34ce925e 7877
df58fc94
RS
7878 /* Perform any necessary conversion to microMIPS relocations
7879 and find out how many relocations there actually are. */
7880 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
7881 final_type[i] = micromips_map_reloc (reloc_type[i]);
7882
9fe77896
RS
7883 /* In a compound relocation, it is the final (outermost)
7884 operator that determines the relocated field. */
2309ddf2 7885 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
e8044f35
RS
7886 if (!howto)
7887 abort ();
2309ddf2
MR
7888
7889 if (i > 1)
7890 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
9fe77896
RS
7891 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
7892 bfd_get_reloc_size (howto),
7893 address_expr,
2309ddf2
MR
7894 howto0 && howto0->pc_relative,
7895 final_type[0]);
ce8ad872
MR
7896 /* Record non-PIC mode in `fx_tcbit2' for `md_apply_fix'. */
7897 ip->fixp[0]->fx_tcbit2 = mips_pic == NO_PIC;
9fe77896
RS
7898
7899 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
2309ddf2 7900 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
9fe77896
RS
7901 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
7902
7903 /* These relocations can have an addend that won't fit in
7904 4 octets for 64bit assembly. */
bad1aba3 7905 if (GPR_SIZE == 64
9fe77896
RS
7906 && ! howto->partial_inplace
7907 && (reloc_type[0] == BFD_RELOC_16
7908 || reloc_type[0] == BFD_RELOC_32
7909 || reloc_type[0] == BFD_RELOC_MIPS_JMP
7910 || reloc_type[0] == BFD_RELOC_GPREL16
7911 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
7912 || reloc_type[0] == BFD_RELOC_GPREL32
7913 || reloc_type[0] == BFD_RELOC_64
7914 || reloc_type[0] == BFD_RELOC_CTOR
7915 || reloc_type[0] == BFD_RELOC_MIPS_SUB
7916 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
7917 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
7918 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
7919 || reloc_type[0] == BFD_RELOC_MIPS_REL16
7920 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
7921 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
7922 || hi16_reloc_p (reloc_type[0])
7923 || lo16_reloc_p (reloc_type[0])))
7924 ip->fixp[0]->fx_no_overflow = 1;
7925
ddaf2c41
MR
7926 /* These relocations can have an addend that won't fit in 2 octets. */
7927 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
7928 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
7929 ip->fixp[0]->fx_no_overflow = 1;
7930
9fe77896
RS
7931 if (mips_relax.sequence)
7932 {
7933 if (mips_relax.first_fixup == 0)
7934 mips_relax.first_fixup = ip->fixp[0];
7935 }
7936 else if (reloc_needs_lo_p (*reloc_type))
7937 {
7938 struct mips_hi_fixup *hi_fixup;
7939
7940 /* Reuse the last entry if it already has a matching %lo. */
7941 hi_fixup = mips_hi_fixup_list;
7942 if (hi_fixup == 0
7943 || !fixup_has_matching_lo_p (hi_fixup->fixp))
4d7206a2 7944 {
325801bd 7945 hi_fixup = XNEW (struct mips_hi_fixup);
9fe77896
RS
7946 hi_fixup->next = mips_hi_fixup_list;
7947 mips_hi_fixup_list = hi_fixup;
4d7206a2 7948 }
9fe77896
RS
7949 hi_fixup->fixp = ip->fixp[0];
7950 hi_fixup->seg = now_seg;
7951 }
252b5132 7952
9fe77896
RS
7953 /* Add fixups for the second and third relocations, if given.
7954 Note that the ABI allows the second relocation to be
7955 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
7956 moment we only use RSS_UNDEF, but we could add support
7957 for the others if it ever becomes necessary. */
7958 for (i = 1; i < 3; i++)
7959 if (reloc_type[i] != BFD_RELOC_UNUSED)
7960 {
7961 ip->fixp[i] = fix_new (ip->frag, ip->where,
7962 ip->fixp[0]->fx_size, NULL, 0,
df58fc94 7963 FALSE, final_type[i]);
f6688943 7964
9fe77896
RS
7965 /* Use fx_tcbit to mark compound relocs. */
7966 ip->fixp[0]->fx_tcbit = 1;
7967 ip->fixp[i]->fx_tcbit = 1;
7968 }
252b5132 7969 }
252b5132
RH
7970
7971 /* Update the register mask information. */
4c260379
RS
7972 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
7973 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
252b5132 7974
a4e06468 7975 switch (method)
252b5132 7976 {
a4e06468
RS
7977 case APPEND_ADD:
7978 insert_into_history (0, 1, ip);
7979 break;
7980
7981 case APPEND_ADD_WITH_NOP:
14fe068b
RS
7982 {
7983 struct mips_cl_insn *nop;
7984
7985 insert_into_history (0, 1, ip);
7986 nop = get_delay_slot_nop (ip);
7987 add_fixed_insn (nop);
7988 insert_into_history (0, 1, nop);
7989 if (mips_relax.sequence)
7990 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
7991 }
a4e06468
RS
7992 break;
7993
7994 case APPEND_ADD_COMPACT:
7995 /* Convert MIPS16 jr/jalr into a "compact" jump. */
7bd374a4
MR
7996 if (mips_opts.mips16)
7997 {
7998 ip->insn_opcode |= 0x0080;
7999 find_altered_mips16_opcode (ip);
8000 }
8001 /* Convert microMIPS instructions. */
8002 else if (mips_opts.micromips)
8003 {
8004 /* jr16->jrc */
8005 if ((ip->insn_opcode & 0xffe0) == 0x4580)
8006 ip->insn_opcode |= 0x0020;
8007 /* b16->bc */
8008 else if ((ip->insn_opcode & 0xfc00) == 0xcc00)
8009 ip->insn_opcode = 0x40e00000;
8010 /* beqz16->beqzc, bnez16->bnezc */
8011 else if ((ip->insn_opcode & 0xdc00) == 0x8c00)
8012 {
8013 unsigned long regno;
8014
8015 regno = ip->insn_opcode >> MICROMIPSOP_SH_MD;
8016 regno &= MICROMIPSOP_MASK_MD;
8017 regno = micromips_to_32_reg_d_map[regno];
8018 ip->insn_opcode = (((ip->insn_opcode << 9) & 0x00400000)
8019 | (regno << MICROMIPSOP_SH_RS)
8020 | 0x40a00000) ^ 0x00400000;
8021 }
8022 /* beqz->beqzc, bnez->bnezc */
8023 else if ((ip->insn_opcode & 0xdfe00000) == 0x94000000)
8024 ip->insn_opcode = ((ip->insn_opcode & 0x001f0000)
8025 | ((ip->insn_opcode >> 7) & 0x00400000)
8026 | 0x40a00000) ^ 0x00400000;
8027 /* beq $0->beqzc, bne $0->bnezc */
8028 else if ((ip->insn_opcode & 0xdc1f0000) == 0x94000000)
8029 ip->insn_opcode = (((ip->insn_opcode >>
8030 (MICROMIPSOP_SH_RT - MICROMIPSOP_SH_RS))
8031 & (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS))
8032 | ((ip->insn_opcode >> 7) & 0x00400000)
8033 | 0x40a00000) ^ 0x00400000;
8034 else
8035 abort ();
8036 find_altered_micromips_opcode (ip);
8037 }
8038 else
8039 abort ();
a4e06468
RS
8040 install_insn (ip);
8041 insert_into_history (0, 1, ip);
8042 break;
8043
8044 case APPEND_SWAP:
8045 {
8046 struct mips_cl_insn delay = history[0];
99e7978b
MF
8047
8048 if (relaxed_branch || delay.frag != ip->frag)
a4e06468
RS
8049 {
8050 /* Add the delay slot instruction to the end of the
8051 current frag and shrink the fixed part of the
8052 original frag. If the branch occupies the tail of
8053 the latter, move it backwards to cover the gap. */
2b0c8b40 8054 delay.frag->fr_fix -= branch_disp;
a4e06468 8055 if (delay.frag == ip->frag)
2b0c8b40 8056 move_insn (ip, ip->frag, ip->where - branch_disp);
a4e06468
RS
8057 add_fixed_insn (&delay);
8058 }
8059 else
8060 {
5e35670b
MR
8061 /* If this is not a relaxed branch and we are in the
8062 same frag, then just swap the instructions. */
8063 move_insn (ip, delay.frag, delay.where);
8064 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
a4e06468
RS
8065 }
8066 history[0] = *ip;
8067 delay.fixed_p = 1;
8068 insert_into_history (0, 1, &delay);
8069 }
8070 break;
252b5132
RH
8071 }
8072
13408f1e 8073 /* If we have just completed an unconditional branch, clear the history. */
11625dd8
RS
8074 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
8075 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
e407c74b
NC
8076 {
8077 unsigned int i;
8078
79850f26 8079 mips_no_prev_insn ();
13408f1e 8080
e407c74b 8081 for (i = 0; i < ARRAY_SIZE (history); i++)
79850f26 8082 history[i].cleared_p = 1;
e407c74b
NC
8083 }
8084
df58fc94
RS
8085 /* We need to emit a label at the end of branch-likely macros. */
8086 if (emit_branch_likely_macro)
8087 {
8088 emit_branch_likely_macro = FALSE;
8089 micromips_add_label ();
8090 }
8091
252b5132
RH
8092 /* We just output an insn, so the next one doesn't have a label. */
8093 mips_clear_insn_labels ();
252b5132
RH
8094}
8095
e407c74b
NC
8096/* Forget that there was any previous instruction or label.
8097 When BRANCH is true, the branch history is also flushed. */
252b5132
RH
8098
8099static void
7d10b47d 8100mips_no_prev_insn (void)
252b5132 8101{
7d10b47d
RS
8102 prev_nop_frag = NULL;
8103 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
8104 mips_clear_insn_labels ();
8105}
8106
7d10b47d
RS
8107/* This function must be called before we emit something other than
8108 instructions. It is like mips_no_prev_insn except that it inserts
8109 any NOPS that might be needed by previous instructions. */
252b5132 8110
7d10b47d
RS
8111void
8112mips_emit_delays (void)
252b5132
RH
8113{
8114 if (! mips_opts.noreorder)
8115 {
932d1a1b 8116 int nops = nops_for_insn (0, history, NULL);
252b5132
RH
8117 if (nops > 0)
8118 {
7d10b47d
RS
8119 while (nops-- > 0)
8120 add_fixed_insn (NOP_INSN);
462427c4 8121 mips_move_text_labels ();
7d10b47d
RS
8122 }
8123 }
8124 mips_no_prev_insn ();
8125}
8126
8127/* Start a (possibly nested) noreorder block. */
8128
8129static void
8130start_noreorder (void)
8131{
8132 if (mips_opts.noreorder == 0)
8133 {
8134 unsigned int i;
8135 int nops;
8136
8137 /* None of the instructions before the .set noreorder can be moved. */
8138 for (i = 0; i < ARRAY_SIZE (history); i++)
8139 history[i].fixed_p = 1;
8140
8141 /* Insert any nops that might be needed between the .set noreorder
8142 block and the previous instructions. We will later remove any
8143 nops that turn out not to be needed. */
932d1a1b 8144 nops = nops_for_insn (0, history, NULL);
7d10b47d
RS
8145 if (nops > 0)
8146 {
8147 if (mips_optimize != 0)
252b5132
RH
8148 {
8149 /* Record the frag which holds the nop instructions, so
8150 that we can remove them if we don't need them. */
df58fc94 8151 frag_grow (nops * NOP_INSN_SIZE);
252b5132
RH
8152 prev_nop_frag = frag_now;
8153 prev_nop_frag_holds = nops;
8154 prev_nop_frag_required = 0;
8155 prev_nop_frag_since = 0;
8156 }
8157
8158 for (; nops > 0; --nops)
1e915849 8159 add_fixed_insn (NOP_INSN);
252b5132 8160
7d10b47d
RS
8161 /* Move on to a new frag, so that it is safe to simply
8162 decrease the size of prev_nop_frag. */
8163 frag_wane (frag_now);
8164 frag_new (0);
462427c4 8165 mips_move_text_labels ();
252b5132 8166 }
df58fc94 8167 mips_mark_labels ();
7d10b47d 8168 mips_clear_insn_labels ();
252b5132 8169 }
7d10b47d
RS
8170 mips_opts.noreorder++;
8171 mips_any_noreorder = 1;
8172}
252b5132 8173
7d10b47d 8174/* End a nested noreorder block. */
252b5132 8175
7d10b47d
RS
8176static void
8177end_noreorder (void)
8178{
8179 mips_opts.noreorder--;
8180 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
8181 {
8182 /* Commit to inserting prev_nop_frag_required nops and go back to
8183 handling nop insertion the .set reorder way. */
8184 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
df58fc94 8185 * NOP_INSN_SIZE);
7d10b47d
RS
8186 insert_into_history (prev_nop_frag_since,
8187 prev_nop_frag_required, NOP_INSN);
8188 prev_nop_frag = NULL;
8189 }
252b5132
RH
8190}
8191
97d87491
RS
8192/* Sign-extend 32-bit mode constants that have bit 31 set and all
8193 higher bits unset. */
8194
8195static void
8196normalize_constant_expr (expressionS *ex)
8197{
8198 if (ex->X_op == O_constant
8199 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
8200 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
8201 - 0x80000000);
8202}
8203
8204/* Sign-extend 32-bit mode address offsets that have bit 31 set and
8205 all higher bits unset. */
8206
8207static void
8208normalize_address_expr (expressionS *ex)
8209{
8210 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
8211 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
8212 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
8213 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
8214 - 0x80000000);
8215}
8216
8217/* Try to match TOKENS against OPCODE, storing the result in INSN.
8218 Return true if the match was successful.
8219
8220 OPCODE_EXTRA is a value that should be ORed into the opcode
8221 (used for VU0 channel suffixes, etc.). MORE_ALTS is true if
8222 there are more alternatives after OPCODE and SOFT_MATCH is
8223 as for mips_arg_info. */
8224
8225static bfd_boolean
8226match_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
8227 struct mips_operand_token *tokens, unsigned int opcode_extra,
60f20e8b 8228 bfd_boolean lax_match, bfd_boolean complete_p)
97d87491
RS
8229{
8230 const char *args;
8231 struct mips_arg_info arg;
8232 const struct mips_operand *operand;
8233 char c;
8234
8235 imm_expr.X_op = O_absent;
97d87491
RS
8236 offset_expr.X_op = O_absent;
8237 offset_reloc[0] = BFD_RELOC_UNUSED;
8238 offset_reloc[1] = BFD_RELOC_UNUSED;
8239 offset_reloc[2] = BFD_RELOC_UNUSED;
8240
8241 create_insn (insn, opcode);
60f20e8b
RS
8242 /* When no opcode suffix is specified, assume ".xyzw". */
8243 if ((opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0 && opcode_extra == 0)
8244 insn->insn_opcode |= 0xf << mips_vu0_channel_mask.lsb;
8245 else
8246 insn->insn_opcode |= opcode_extra;
97d87491
RS
8247 memset (&arg, 0, sizeof (arg));
8248 arg.insn = insn;
8249 arg.token = tokens;
8250 arg.argnum = 1;
8251 arg.last_regno = ILLEGAL_REG;
8252 arg.dest_regno = ILLEGAL_REG;
60f20e8b 8253 arg.lax_match = lax_match;
97d87491
RS
8254 for (args = opcode->args;; ++args)
8255 {
8256 if (arg.token->type == OT_END)
8257 {
8258 /* Handle unary instructions in which only one operand is given.
8259 The source is then the same as the destination. */
8260 if (arg.opnum == 1 && *args == ',')
8261 {
8262 operand = (mips_opts.micromips
8263 ? decode_micromips_operand (args + 1)
8264 : decode_mips_operand (args + 1));
8265 if (operand && mips_optional_operand_p (operand))
8266 {
8267 arg.token = tokens;
8268 arg.argnum = 1;
8269 continue;
8270 }
8271 }
8272
8273 /* Treat elided base registers as $0. */
8274 if (strcmp (args, "(b)") == 0)
8275 args += 3;
8276
8277 if (args[0] == '+')
8278 switch (args[1])
8279 {
8280 case 'K':
8281 case 'N':
8282 /* The register suffix is optional. */
8283 args += 2;
8284 break;
8285 }
8286
8287 /* Fail the match if there were too few operands. */
8288 if (*args)
8289 return FALSE;
8290
8291 /* Successful match. */
60f20e8b
RS
8292 if (!complete_p)
8293 return TRUE;
e3de51ce 8294 clear_insn_error ();
97d87491
RS
8295 if (arg.dest_regno == arg.last_regno
8296 && strncmp (insn->insn_mo->name, "jalr", 4) == 0)
8297 {
8298 if (arg.opnum == 2)
e3de51ce 8299 set_insn_error
1661c76c 8300 (0, _("source and destination must be different"));
97d87491 8301 else if (arg.last_regno == 31)
e3de51ce 8302 set_insn_error
1661c76c 8303 (0, _("a destination register must be supplied"));
97d87491 8304 }
173d3447
CF
8305 else if (arg.last_regno == 31
8306 && (strncmp (insn->insn_mo->name, "bltzal", 6) == 0
8307 || strncmp (insn->insn_mo->name, "bgezal", 6) == 0))
8308 set_insn_error (0, _("the source register must not be $31"));
97d87491
RS
8309 check_completed_insn (&arg);
8310 return TRUE;
8311 }
8312
8313 /* Fail the match if the line has too many operands. */
8314 if (*args == 0)
8315 return FALSE;
8316
8317 /* Handle characters that need to match exactly. */
8318 if (*args == '(' || *args == ')' || *args == ',')
8319 {
8320 if (match_char (&arg, *args))
8321 continue;
8322 return FALSE;
8323 }
8324 if (*args == '#')
8325 {
8326 ++args;
8327 if (arg.token->type == OT_DOUBLE_CHAR
8328 && arg.token->u.ch == *args)
8329 {
8330 ++arg.token;
8331 continue;
8332 }
8333 return FALSE;
8334 }
8335
8336 /* Handle special macro operands. Work out the properties of
8337 other operands. */
8338 arg.opnum += 1;
97d87491
RS
8339 switch (*args)
8340 {
7361da2c
AB
8341 case '-':
8342 switch (args[1])
8343 {
8344 case 'A':
8345 *offset_reloc = BFD_RELOC_MIPS_19_PCREL_S2;
8346 break;
8347
8348 case 'B':
8349 *offset_reloc = BFD_RELOC_MIPS_18_PCREL_S3;
8350 break;
8351 }
8352 break;
8353
97d87491
RS
8354 case '+':
8355 switch (args[1])
8356 {
97d87491
RS
8357 case 'i':
8358 *offset_reloc = BFD_RELOC_MIPS_JMP;
8359 break;
7361da2c
AB
8360
8361 case '\'':
8362 *offset_reloc = BFD_RELOC_MIPS_26_PCREL_S2;
8363 break;
8364
8365 case '\"':
8366 *offset_reloc = BFD_RELOC_MIPS_21_PCREL_S2;
8367 break;
97d87491
RS
8368 }
8369 break;
8370
97d87491 8371 case 'I':
1a00e612
RS
8372 if (!match_const_int (&arg, &imm_expr.X_add_number))
8373 return FALSE;
8374 imm_expr.X_op = O_constant;
bad1aba3 8375 if (GPR_SIZE == 32)
97d87491
RS
8376 normalize_constant_expr (&imm_expr);
8377 continue;
8378
8379 case 'A':
8380 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8381 {
8382 /* Assume that the offset has been elided and that what
8383 we saw was a base register. The match will fail later
8384 if that assumption turns out to be wrong. */
8385 offset_expr.X_op = O_constant;
8386 offset_expr.X_add_number = 0;
8387 }
97d87491 8388 else
1a00e612
RS
8389 {
8390 if (!match_expression (&arg, &offset_expr, offset_reloc))
8391 return FALSE;
8392 normalize_address_expr (&offset_expr);
8393 }
97d87491
RS
8394 continue;
8395
8396 case 'F':
8397 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8398 8, TRUE))
1a00e612 8399 return FALSE;
97d87491
RS
8400 continue;
8401
8402 case 'L':
8403 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8404 8, FALSE))
1a00e612 8405 return FALSE;
97d87491
RS
8406 continue;
8407
8408 case 'f':
8409 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8410 4, TRUE))
1a00e612 8411 return FALSE;
97d87491
RS
8412 continue;
8413
8414 case 'l':
8415 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8416 4, FALSE))
1a00e612 8417 return FALSE;
97d87491
RS
8418 continue;
8419
97d87491
RS
8420 case 'p':
8421 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8422 break;
8423
8424 case 'a':
8425 *offset_reloc = BFD_RELOC_MIPS_JMP;
8426 break;
8427
8428 case 'm':
8429 gas_assert (mips_opts.micromips);
8430 c = args[1];
8431 switch (c)
8432 {
8433 case 'D':
8434 case 'E':
8435 if (!forced_insn_length)
8436 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
8437 else if (c == 'D')
8438 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
8439 else
8440 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
8441 break;
8442 }
8443 break;
8444 }
8445
8446 operand = (mips_opts.micromips
8447 ? decode_micromips_operand (args)
8448 : decode_mips_operand (args));
8449 if (!operand)
8450 abort ();
8451
8452 /* Skip prefixes. */
7361da2c 8453 if (*args == '+' || *args == 'm' || *args == '-')
97d87491
RS
8454 args++;
8455
8456 if (mips_optional_operand_p (operand)
8457 && args[1] == ','
8458 && (arg.token[0].type != OT_REG
8459 || arg.token[1].type == OT_END))
8460 {
8461 /* Assume that the register has been elided and is the
8462 same as the first operand. */
8463 arg.token = tokens;
8464 arg.argnum = 1;
8465 }
8466
8467 if (!match_operand (&arg, operand))
8468 return FALSE;
8469 }
8470}
8471
8472/* Like match_insn, but for MIPS16. */
8473
8474static bfd_boolean
8475match_mips16_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
1a00e612 8476 struct mips_operand_token *tokens)
97d87491
RS
8477{
8478 const char *args;
8479 const struct mips_operand *operand;
8480 const struct mips_operand *ext_operand;
82d808ed 8481 bfd_boolean pcrel = FALSE;
7fd53920 8482 int required_insn_length;
97d87491
RS
8483 struct mips_arg_info arg;
8484 int relax_char;
8485
7fd53920
MR
8486 if (forced_insn_length)
8487 required_insn_length = forced_insn_length;
8488 else if (mips_opts.noautoextend && !mips_opcode_32bit_p (opcode))
8489 required_insn_length = 2;
8490 else
8491 required_insn_length = 0;
8492
97d87491
RS
8493 create_insn (insn, opcode);
8494 imm_expr.X_op = O_absent;
97d87491
RS
8495 offset_expr.X_op = O_absent;
8496 offset_reloc[0] = BFD_RELOC_UNUSED;
8497 offset_reloc[1] = BFD_RELOC_UNUSED;
8498 offset_reloc[2] = BFD_RELOC_UNUSED;
8499 relax_char = 0;
8500
8501 memset (&arg, 0, sizeof (arg));
8502 arg.insn = insn;
8503 arg.token = tokens;
8504 arg.argnum = 1;
8505 arg.last_regno = ILLEGAL_REG;
8506 arg.dest_regno = ILLEGAL_REG;
97d87491
RS
8507 relax_char = 0;
8508 for (args = opcode->args;; ++args)
8509 {
8510 int c;
8511
8512 if (arg.token->type == OT_END)
8513 {
8514 offsetT value;
8515
8516 /* Handle unary instructions in which only one operand is given.
8517 The source is then the same as the destination. */
8518 if (arg.opnum == 1 && *args == ',')
8519 {
8520 operand = decode_mips16_operand (args[1], FALSE);
8521 if (operand && mips_optional_operand_p (operand))
8522 {
8523 arg.token = tokens;
8524 arg.argnum = 1;
8525 continue;
8526 }
8527 }
8528
8529 /* Fail the match if there were too few operands. */
8530 if (*args)
8531 return FALSE;
8532
8533 /* Successful match. Stuff the immediate value in now, if
8534 we can. */
e3de51ce 8535 clear_insn_error ();
97d87491
RS
8536 if (opcode->pinfo == INSN_MACRO)
8537 {
8538 gas_assert (relax_char == 0 || relax_char == 'p');
8539 gas_assert (*offset_reloc == BFD_RELOC_UNUSED);
8540 }
8541 else if (relax_char
8542 && offset_expr.X_op == O_constant
82d808ed 8543 && !pcrel
97d87491
RS
8544 && calculate_reloc (*offset_reloc,
8545 offset_expr.X_add_number,
8546 &value))
8547 {
8548 mips16_immed (NULL, 0, relax_char, *offset_reloc, value,
7fd53920 8549 required_insn_length, &insn->insn_opcode);
97d87491
RS
8550 offset_expr.X_op = O_absent;
8551 *offset_reloc = BFD_RELOC_UNUSED;
8552 }
8553 else if (relax_char && *offset_reloc != BFD_RELOC_UNUSED)
8554 {
7fd53920 8555 if (required_insn_length == 2)
e3de51ce 8556 set_insn_error (0, _("invalid unextended operand value"));
25499ac7 8557 else if (!mips_opcode_32bit_p (opcode))
1da43acc
MR
8558 {
8559 forced_insn_length = 4;
8560 insn->insn_opcode |= MIPS16_EXTEND;
8561 }
97d87491
RS
8562 }
8563 else if (relax_char)
8564 *offset_reloc = (int) BFD_RELOC_UNUSED + relax_char;
8565
8566 check_completed_insn (&arg);
8567 return TRUE;
8568 }
8569
8570 /* Fail the match if the line has too many operands. */
8571 if (*args == 0)
8572 return FALSE;
8573
8574 /* Handle characters that need to match exactly. */
8575 if (*args == '(' || *args == ')' || *args == ',')
8576 {
8577 if (match_char (&arg, *args))
8578 continue;
8579 return FALSE;
8580 }
8581
8582 arg.opnum += 1;
8583 c = *args;
8584 switch (c)
8585 {
8586 case 'p':
8587 case 'q':
8588 case 'A':
8589 case 'B':
8590 case 'E':
25499ac7
MR
8591 case 'V':
8592 case 'u':
97d87491
RS
8593 relax_char = c;
8594 break;
8595
8596 case 'I':
1a00e612
RS
8597 if (!match_const_int (&arg, &imm_expr.X_add_number))
8598 return FALSE;
8599 imm_expr.X_op = O_constant;
bad1aba3 8600 if (GPR_SIZE == 32)
97d87491
RS
8601 normalize_constant_expr (&imm_expr);
8602 continue;
8603
8604 case 'a':
8605 case 'i':
8606 *offset_reloc = BFD_RELOC_MIPS16_JMP;
97d87491
RS
8607 break;
8608 }
8609
7fd53920 8610 operand = decode_mips16_operand (c, mips_opcode_32bit_p (opcode));
97d87491
RS
8611 if (!operand)
8612 abort ();
8613
82d808ed
MR
8614 if (operand->type == OP_PCREL)
8615 pcrel = TRUE;
8616 else
97d87491
RS
8617 {
8618 ext_operand = decode_mips16_operand (c, TRUE);
8619 if (operand != ext_operand)
8620 {
8621 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8622 {
8623 offset_expr.X_op = O_constant;
8624 offset_expr.X_add_number = 0;
8625 relax_char = c;
8626 continue;
8627 }
8628
1a7bf198 8629 if (!match_expression (&arg, &offset_expr, offset_reloc))
97d87491
RS
8630 return FALSE;
8631
8632 /* '8' is used for SLTI(U) and has traditionally not
8633 been allowed to take relocation operators. */
8634 if (offset_reloc[0] != BFD_RELOC_UNUSED
8635 && (ext_operand->size != 16 || c == '8'))
e295202f
MR
8636 {
8637 match_not_constant (&arg);
8638 return FALSE;
8639 }
97d87491 8640
c96425c5
MR
8641 if (offset_expr.X_op == O_big)
8642 {
8643 match_out_of_range (&arg);
8644 return FALSE;
8645 }
8646
97d87491
RS
8647 relax_char = c;
8648 continue;
8649 }
8650 }
8651
8652 if (mips_optional_operand_p (operand)
8653 && args[1] == ','
8654 && (arg.token[0].type != OT_REG
8655 || arg.token[1].type == OT_END))
8656 {
8657 /* Assume that the register has been elided and is the
8658 same as the first operand. */
8659 arg.token = tokens;
8660 arg.argnum = 1;
8661 }
8662
8663 if (!match_operand (&arg, operand))
8664 return FALSE;
8665 }
8666}
8667
60f20e8b
RS
8668/* Record that the current instruction is invalid for the current ISA. */
8669
8670static void
8671match_invalid_for_isa (void)
8672{
8673 set_insn_error_ss
1661c76c 8674 (0, _("opcode not supported on this processor: %s (%s)"),
60f20e8b
RS
8675 mips_cpu_info_from_arch (mips_opts.arch)->name,
8676 mips_cpu_info_from_isa (mips_opts.isa)->name);
8677}
8678
8679/* Try to match TOKENS against a series of opcode entries, starting at FIRST.
8680 Return true if a definite match or failure was found, storing any match
8681 in INSN. OPCODE_EXTRA is a value that should be ORed into the opcode
8682 (to handle things like VU0 suffixes). LAX_MATCH is true if we have already
8683 tried and failed to match under normal conditions and now want to try a
8684 more relaxed match. */
8685
8686static bfd_boolean
8687match_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8688 const struct mips_opcode *past, struct mips_operand_token *tokens,
8689 int opcode_extra, bfd_boolean lax_match)
8690{
8691 const struct mips_opcode *opcode;
8692 const struct mips_opcode *invalid_delay_slot;
8693 bfd_boolean seen_valid_for_isa, seen_valid_for_size;
8694
8695 /* Search for a match, ignoring alternatives that don't satisfy the
8696 current ISA or forced_length. */
8697 invalid_delay_slot = 0;
8698 seen_valid_for_isa = FALSE;
8699 seen_valid_for_size = FALSE;
8700 opcode = first;
8701 do
8702 {
8703 gas_assert (strcmp (opcode->name, first->name) == 0);
8704 if (is_opcode_valid (opcode))
8705 {
8706 seen_valid_for_isa = TRUE;
8707 if (is_size_valid (opcode))
8708 {
8709 bfd_boolean delay_slot_ok;
8710
8711 seen_valid_for_size = TRUE;
8712 delay_slot_ok = is_delay_slot_valid (opcode);
8713 if (match_insn (insn, opcode, tokens, opcode_extra,
8714 lax_match, delay_slot_ok))
8715 {
8716 if (!delay_slot_ok)
8717 {
8718 if (!invalid_delay_slot)
8719 invalid_delay_slot = opcode;
8720 }
8721 else
8722 return TRUE;
8723 }
8724 }
8725 }
8726 ++opcode;
8727 }
8728 while (opcode < past && strcmp (opcode->name, first->name) == 0);
8729
8730 /* If the only matches we found had the wrong length for the delay slot,
8731 pick the first such match. We'll issue an appropriate warning later. */
8732 if (invalid_delay_slot)
8733 {
8734 if (match_insn (insn, invalid_delay_slot, tokens, opcode_extra,
8735 lax_match, TRUE))
8736 return TRUE;
8737 abort ();
8738 }
8739
8740 /* Handle the case where we didn't try to match an instruction because
8741 all the alternatives were incompatible with the current ISA. */
8742 if (!seen_valid_for_isa)
8743 {
8744 match_invalid_for_isa ();
8745 return TRUE;
8746 }
8747
8748 /* Handle the case where we didn't try to match an instruction because
8749 all the alternatives were of the wrong size. */
8750 if (!seen_valid_for_size)
8751 {
8752 if (mips_opts.insn32)
1661c76c 8753 set_insn_error (0, _("opcode not supported in the `insn32' mode"));
60f20e8b
RS
8754 else
8755 set_insn_error_i
1661c76c 8756 (0, _("unrecognized %d-bit version of microMIPS opcode"),
60f20e8b
RS
8757 8 * forced_insn_length);
8758 return TRUE;
8759 }
8760
8761 return FALSE;
8762}
8763
8764/* Like match_insns, but for MIPS16. */
8765
8766static bfd_boolean
8767match_mips16_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8768 struct mips_operand_token *tokens)
8769{
8770 const struct mips_opcode *opcode;
8771 bfd_boolean seen_valid_for_isa;
7fd53920 8772 bfd_boolean seen_valid_for_size;
60f20e8b
RS
8773
8774 /* Search for a match, ignoring alternatives that don't satisfy the
8775 current ISA. There are no separate entries for extended forms so
8776 we deal with forced_length later. */
8777 seen_valid_for_isa = FALSE;
7fd53920 8778 seen_valid_for_size = FALSE;
60f20e8b
RS
8779 opcode = first;
8780 do
8781 {
8782 gas_assert (strcmp (opcode->name, first->name) == 0);
8783 if (is_opcode_valid_16 (opcode))
8784 {
8785 seen_valid_for_isa = TRUE;
7fd53920
MR
8786 if (is_size_valid_16 (opcode))
8787 {
8788 seen_valid_for_size = TRUE;
8789 if (match_mips16_insn (insn, opcode, tokens))
8790 return TRUE;
8791 }
60f20e8b
RS
8792 }
8793 ++opcode;
8794 }
8795 while (opcode < &mips16_opcodes[bfd_mips16_num_opcodes]
8796 && strcmp (opcode->name, first->name) == 0);
8797
8798 /* Handle the case where we didn't try to match an instruction because
8799 all the alternatives were incompatible with the current ISA. */
8800 if (!seen_valid_for_isa)
8801 {
8802 match_invalid_for_isa ();
8803 return TRUE;
8804 }
8805
7fd53920
MR
8806 /* Handle the case where we didn't try to match an instruction because
8807 all the alternatives were of the wrong size. */
8808 if (!seen_valid_for_size)
8809 {
8810 if (forced_insn_length == 2)
8811 set_insn_error
8812 (0, _("unrecognized unextended version of MIPS16 opcode"));
8813 else
8814 set_insn_error
8815 (0, _("unrecognized extended version of MIPS16 opcode"));
8816 return TRUE;
8817 }
8818
60f20e8b
RS
8819 return FALSE;
8820}
8821
584892a6
RS
8822/* Set up global variables for the start of a new macro. */
8823
8824static void
8825macro_start (void)
8826{
8827 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
df58fc94
RS
8828 memset (&mips_macro_warning.first_insn_sizes, 0,
8829 sizeof (mips_macro_warning.first_insn_sizes));
8830 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
584892a6 8831 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
11625dd8 8832 && delayed_branch_p (&history[0]));
7bd374a4
MR
8833 if (history[0].frag
8834 && history[0].frag->fr_type == rs_machine_dependent
8835 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
8836 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
8837 mips_macro_warning.delay_slot_length = 0;
8838 else
8839 switch (history[0].insn_mo->pinfo2
8840 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
8841 {
8842 case INSN2_BRANCH_DELAY_32BIT:
8843 mips_macro_warning.delay_slot_length = 4;
8844 break;
8845 case INSN2_BRANCH_DELAY_16BIT:
8846 mips_macro_warning.delay_slot_length = 2;
8847 break;
8848 default:
8849 mips_macro_warning.delay_slot_length = 0;
8850 break;
8851 }
df58fc94 8852 mips_macro_warning.first_frag = NULL;
584892a6
RS
8853}
8854
df58fc94
RS
8855/* Given that a macro is longer than one instruction or of the wrong size,
8856 return the appropriate warning for it. Return null if no warning is
8857 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
8858 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
8859 and RELAX_NOMACRO. */
584892a6
RS
8860
8861static const char *
8862macro_warning (relax_substateT subtype)
8863{
8864 if (subtype & RELAX_DELAY_SLOT)
1661c76c 8865 return _("macro instruction expanded into multiple instructions"
584892a6
RS
8866 " in a branch delay slot");
8867 else if (subtype & RELAX_NOMACRO)
1661c76c 8868 return _("macro instruction expanded into multiple instructions");
df58fc94
RS
8869 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
8870 | RELAX_DELAY_SLOT_SIZE_SECOND))
8871 return ((subtype & RELAX_DELAY_SLOT_16BIT)
1661c76c 8872 ? _("macro instruction expanded into a wrong size instruction"
df58fc94 8873 " in a 16-bit branch delay slot")
1661c76c 8874 : _("macro instruction expanded into a wrong size instruction"
df58fc94 8875 " in a 32-bit branch delay slot"));
584892a6
RS
8876 else
8877 return 0;
8878}
8879
8880/* Finish up a macro. Emit warnings as appropriate. */
8881
8882static void
8883macro_end (void)
8884{
df58fc94
RS
8885 /* Relaxation warning flags. */
8886 relax_substateT subtype = 0;
8887
8888 /* Check delay slot size requirements. */
8889 if (mips_macro_warning.delay_slot_length == 2)
8890 subtype |= RELAX_DELAY_SLOT_16BIT;
8891 if (mips_macro_warning.delay_slot_length != 0)
584892a6 8892 {
df58fc94
RS
8893 if (mips_macro_warning.delay_slot_length
8894 != mips_macro_warning.first_insn_sizes[0])
8895 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
8896 if (mips_macro_warning.delay_slot_length
8897 != mips_macro_warning.first_insn_sizes[1])
8898 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
8899 }
584892a6 8900
df58fc94
RS
8901 /* Check instruction count requirements. */
8902 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
8903 {
8904 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
584892a6
RS
8905 subtype |= RELAX_SECOND_LONGER;
8906 if (mips_opts.warn_about_macros)
8907 subtype |= RELAX_NOMACRO;
8908 if (mips_macro_warning.delay_slot_p)
8909 subtype |= RELAX_DELAY_SLOT;
df58fc94 8910 }
584892a6 8911
df58fc94
RS
8912 /* If both alternatives fail to fill a delay slot correctly,
8913 emit the warning now. */
8914 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
8915 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
8916 {
8917 relax_substateT s;
8918 const char *msg;
8919
8920 s = subtype & (RELAX_DELAY_SLOT_16BIT
8921 | RELAX_DELAY_SLOT_SIZE_FIRST
8922 | RELAX_DELAY_SLOT_SIZE_SECOND);
8923 msg = macro_warning (s);
8924 if (msg != NULL)
8925 as_warn ("%s", msg);
8926 subtype &= ~s;
8927 }
8928
8929 /* If both implementations are longer than 1 instruction, then emit the
8930 warning now. */
8931 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
8932 {
8933 relax_substateT s;
8934 const char *msg;
8935
8936 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
8937 msg = macro_warning (s);
8938 if (msg != NULL)
8939 as_warn ("%s", msg);
8940 subtype &= ~s;
584892a6 8941 }
df58fc94
RS
8942
8943 /* If any flags still set, then one implementation might need a warning
8944 and the other either will need one of a different kind or none at all.
8945 Pass any remaining flags over to relaxation. */
8946 if (mips_macro_warning.first_frag != NULL)
8947 mips_macro_warning.first_frag->fr_subtype |= subtype;
584892a6
RS
8948}
8949
df58fc94
RS
8950/* Instruction operand formats used in macros that vary between
8951 standard MIPS and microMIPS code. */
8952
833794fc 8953static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
df58fc94
RS
8954static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
8955static const char * const jalr_fmt[2] = { "d,s", "t,s" };
8956static const char * const lui_fmt[2] = { "t,u", "s,u" };
8957static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
833794fc 8958static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
df58fc94
RS
8959static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
8960static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
8961
833794fc 8962#define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
7361da2c
AB
8963#define COP12_FMT (ISA_IS_R6 (mips_opts.isa) ? "E,+:(d)" \
8964 : cop12_fmt[mips_opts.micromips])
df58fc94
RS
8965#define JALR_FMT (jalr_fmt[mips_opts.micromips])
8966#define LUI_FMT (lui_fmt[mips_opts.micromips])
8967#define MEM12_FMT (mem12_fmt[mips_opts.micromips])
7361da2c
AB
8968#define LL_SC_FMT (ISA_IS_R6 (mips_opts.isa) ? "t,+j(b)" \
8969 : mem12_fmt[mips_opts.micromips])
833794fc 8970#define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
df58fc94
RS
8971#define SHFT_FMT (shft_fmt[mips_opts.micromips])
8972#define TRAP_FMT (trap_fmt[mips_opts.micromips])
8973
6e1304d8
RS
8974/* Read a macro's relocation codes from *ARGS and store them in *R.
8975 The first argument in *ARGS will be either the code for a single
8976 relocation or -1 followed by the three codes that make up a
8977 composite relocation. */
8978
8979static void
8980macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
8981{
8982 int i, next;
8983
8984 next = va_arg (*args, int);
8985 if (next >= 0)
8986 r[0] = (bfd_reloc_code_real_type) next;
8987 else
f2ae14a1
RS
8988 {
8989 for (i = 0; i < 3; i++)
8990 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
8991 /* This function is only used for 16-bit relocation fields.
8992 To make the macro code simpler, treat an unrelocated value
8993 in the same way as BFD_RELOC_LO16. */
8994 if (r[0] == BFD_RELOC_UNUSED)
8995 r[0] = BFD_RELOC_LO16;
8996 }
6e1304d8
RS
8997}
8998
252b5132
RH
8999/* Build an instruction created by a macro expansion. This is passed
9000 a pointer to the count of instructions created so far, an
9001 expression, the name of the instruction to build, an operand format
9002 string, and corresponding arguments. */
9003
252b5132 9004static void
67c0d1eb 9005macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 9006{
df58fc94 9007 const struct mips_opcode *mo = NULL;
f6688943 9008 bfd_reloc_code_real_type r[3];
df58fc94 9009 const struct mips_opcode *amo;
e077a1c8 9010 const struct mips_operand *operand;
629310ab 9011 htab_t hash;
df58fc94 9012 struct mips_cl_insn insn;
252b5132 9013 va_list args;
e077a1c8 9014 unsigned int uval;
252b5132 9015
252b5132 9016 va_start (args, fmt);
252b5132 9017
252b5132
RH
9018 if (mips_opts.mips16)
9019 {
03ea81db 9020 mips16_macro_build (ep, name, fmt, &args);
252b5132
RH
9021 va_end (args);
9022 return;
9023 }
9024
f6688943
TS
9025 r[0] = BFD_RELOC_UNUSED;
9026 r[1] = BFD_RELOC_UNUSED;
9027 r[2] = BFD_RELOC_UNUSED;
df58fc94 9028 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
629310ab 9029 amo = (struct mips_opcode *) str_hash_find (hash, name);
df58fc94
RS
9030 gas_assert (amo);
9031 gas_assert (strcmp (name, amo->name) == 0);
1e915849 9032
df58fc94 9033 do
8b082fb1
TS
9034 {
9035 /* Search until we get a match for NAME. It is assumed here that
df58fc94 9036 macros will never generate MDMX, MIPS-3D, or MT instructions.
33eaf5de 9037 We try to match an instruction that fulfills the branch delay
df58fc94
RS
9038 slot instruction length requirement (if any) of the previous
9039 instruction. While doing this we record the first instruction
9040 seen that matches all the other conditions and use it anyway
9041 if the requirement cannot be met; we will issue an appropriate
9042 warning later on. */
9043 if (strcmp (fmt, amo->args) == 0
9044 && amo->pinfo != INSN_MACRO
9045 && is_opcode_valid (amo)
9046 && is_size_valid (amo))
9047 {
9048 if (is_delay_slot_valid (amo))
9049 {
9050 mo = amo;
9051 break;
9052 }
9053 else if (!mo)
9054 mo = amo;
9055 }
8b082fb1 9056
df58fc94
RS
9057 ++amo;
9058 gas_assert (amo->name);
252b5132 9059 }
df58fc94 9060 while (strcmp (name, amo->name) == 0);
252b5132 9061
df58fc94 9062 gas_assert (mo);
1e915849 9063 create_insn (&insn, mo);
e077a1c8 9064 for (; *fmt; ++fmt)
252b5132 9065 {
e077a1c8 9066 switch (*fmt)
252b5132 9067 {
252b5132
RH
9068 case ',':
9069 case '(':
9070 case ')':
252b5132 9071 case 'z':
e077a1c8 9072 break;
252b5132
RH
9073
9074 case 'i':
9075 case 'j':
6e1304d8 9076 macro_read_relocs (&args, r);
9c2799c2 9077 gas_assert (*r == BFD_RELOC_GPREL16
e391c024
RS
9078 || *r == BFD_RELOC_MIPS_HIGHER
9079 || *r == BFD_RELOC_HI16_S
9080 || *r == BFD_RELOC_LO16
14c80123
MR
9081 || *r == BFD_RELOC_MIPS_GOT_OFST
9082 || (mips_opts.micromips
9083 && (*r == BFD_RELOC_16
9084 || *r == BFD_RELOC_MIPS_GOT16
9085 || *r == BFD_RELOC_MIPS_CALL16
9086 || *r == BFD_RELOC_MIPS_GOT_HI16
9087 || *r == BFD_RELOC_MIPS_GOT_LO16
9088 || *r == BFD_RELOC_MIPS_CALL_HI16
9089 || *r == BFD_RELOC_MIPS_CALL_LO16
9090 || *r == BFD_RELOC_MIPS_SUB
9091 || *r == BFD_RELOC_MIPS_GOT_PAGE
9092 || *r == BFD_RELOC_MIPS_HIGHEST
9093 || *r == BFD_RELOC_MIPS_GOT_DISP
9094 || *r == BFD_RELOC_MIPS_TLS_GD
9095 || *r == BFD_RELOC_MIPS_TLS_LDM
9096 || *r == BFD_RELOC_MIPS_TLS_DTPREL_HI16
9097 || *r == BFD_RELOC_MIPS_TLS_DTPREL_LO16
9098 || *r == BFD_RELOC_MIPS_TLS_GOTTPREL
9099 || *r == BFD_RELOC_MIPS_TLS_TPREL_HI16
9100 || *r == BFD_RELOC_MIPS_TLS_TPREL_LO16)));
e077a1c8 9101 break;
e391c024
RS
9102
9103 case 'o':
9104 macro_read_relocs (&args, r);
e077a1c8 9105 break;
252b5132
RH
9106
9107 case 'u':
6e1304d8 9108 macro_read_relocs (&args, r);
9c2799c2 9109 gas_assert (ep != NULL
90ecf173
MR
9110 && (ep->X_op == O_constant
9111 || (ep->X_op == O_symbol
9112 && (*r == BFD_RELOC_MIPS_HIGHEST
9113 || *r == BFD_RELOC_HI16_S
9114 || *r == BFD_RELOC_HI16
9115 || *r == BFD_RELOC_GPREL16
9116 || *r == BFD_RELOC_MIPS_GOT_HI16
9117 || *r == BFD_RELOC_MIPS_CALL_HI16))));
e077a1c8 9118 break;
252b5132
RH
9119
9120 case 'p':
9c2799c2 9121 gas_assert (ep != NULL);
bad36eac 9122
252b5132
RH
9123 /*
9124 * This allows macro() to pass an immediate expression for
9125 * creating short branches without creating a symbol.
bad36eac
DJ
9126 *
9127 * We don't allow branch relaxation for these branches, as
9128 * they should only appear in ".set nomacro" anyway.
252b5132
RH
9129 */
9130 if (ep->X_op == O_constant)
9131 {
df58fc94
RS
9132 /* For microMIPS we always use relocations for branches.
9133 So we should not resolve immediate values. */
9134 gas_assert (!mips_opts.micromips);
9135
bad36eac
DJ
9136 if ((ep->X_add_number & 3) != 0)
9137 as_bad (_("branch to misaligned address (0x%lx)"),
9138 (unsigned long) ep->X_add_number);
9139 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
9140 as_bad (_("branch address range overflow (0x%lx)"),
9141 (unsigned long) ep->X_add_number);
252b5132
RH
9142 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
9143 ep = NULL;
9144 }
9145 else
0b25d3e6 9146 *r = BFD_RELOC_16_PCREL_S2;
e077a1c8 9147 break;
252b5132
RH
9148
9149 case 'a':
9c2799c2 9150 gas_assert (ep != NULL);
f6688943 9151 *r = BFD_RELOC_MIPS_JMP;
e077a1c8 9152 break;
d43b4baf 9153
252b5132 9154 default:
e077a1c8
RS
9155 operand = (mips_opts.micromips
9156 ? decode_micromips_operand (fmt)
9157 : decode_mips_operand (fmt));
9158 if (!operand)
9159 abort ();
9160
9161 uval = va_arg (args, int);
9162 if (operand->type == OP_CLO_CLZ_DEST)
9163 uval |= (uval << 5);
9164 insn_insert_operand (&insn, operand, uval);
9165
7361da2c 9166 if (*fmt == '+' || *fmt == 'm' || *fmt == '-')
e077a1c8
RS
9167 ++fmt;
9168 break;
252b5132 9169 }
252b5132
RH
9170 }
9171 va_end (args);
9c2799c2 9172 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 9173
df58fc94 9174 append_insn (&insn, ep, r, TRUE);
252b5132
RH
9175}
9176
9177static void
67c0d1eb 9178mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
03ea81db 9179 va_list *args)
252b5132 9180{
1e915849 9181 struct mips_opcode *mo;
252b5132 9182 struct mips_cl_insn insn;
e077a1c8 9183 const struct mips_operand *operand;
f6688943
TS
9184 bfd_reloc_code_real_type r[3]
9185 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 9186
629310ab 9187 mo = (struct mips_opcode *) str_hash_find (mips16_op_hash, name);
9c2799c2
NC
9188 gas_assert (mo);
9189 gas_assert (strcmp (name, mo->name) == 0);
252b5132 9190
1e915849 9191 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 9192 {
1e915849 9193 ++mo;
9c2799c2
NC
9194 gas_assert (mo->name);
9195 gas_assert (strcmp (name, mo->name) == 0);
252b5132
RH
9196 }
9197
1e915849 9198 create_insn (&insn, mo);
e077a1c8 9199 for (; *fmt; ++fmt)
252b5132
RH
9200 {
9201 int c;
9202
e077a1c8 9203 c = *fmt;
252b5132
RH
9204 switch (c)
9205 {
252b5132
RH
9206 case ',':
9207 case '(':
9208 case ')':
e077a1c8 9209 break;
252b5132 9210
d8722d76 9211 case '.':
252b5132
RH
9212 case 'S':
9213 case 'P':
9214 case 'R':
e077a1c8 9215 break;
252b5132
RH
9216
9217 case '<':
252b5132 9218 case '5':
d8722d76 9219 case 'F':
252b5132
RH
9220 case 'H':
9221 case 'W':
9222 case 'D':
9223 case 'j':
9224 case '8':
9225 case 'V':
9226 case 'C':
9227 case 'U':
9228 case 'k':
9229 case 'K':
9230 case 'p':
9231 case 'q':
9232 {
b886a2ab
RS
9233 offsetT value;
9234
9c2799c2 9235 gas_assert (ep != NULL);
252b5132
RH
9236
9237 if (ep->X_op != O_constant)
874e8986 9238 *r = (int) BFD_RELOC_UNUSED + c;
b886a2ab 9239 else if (calculate_reloc (*r, ep->X_add_number, &value))
252b5132 9240 {
b886a2ab 9241 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
252b5132 9242 ep = NULL;
f6688943 9243 *r = BFD_RELOC_UNUSED;
252b5132
RH
9244 }
9245 }
e077a1c8 9246 break;
252b5132 9247
e077a1c8
RS
9248 default:
9249 operand = decode_mips16_operand (c, FALSE);
9250 if (!operand)
9251 abort ();
252b5132 9252
4a06e5a2 9253 insn_insert_operand (&insn, operand, va_arg (*args, int));
e077a1c8
RS
9254 break;
9255 }
252b5132
RH
9256 }
9257
9c2799c2 9258 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 9259
df58fc94 9260 append_insn (&insn, ep, r, TRUE);
252b5132
RH
9261}
9262
438c16b8
TS
9263/*
9264 * Generate a "jalr" instruction with a relocation hint to the called
9265 * function. This occurs in NewABI PIC code.
9266 */
9267static void
df58fc94 9268macro_build_jalr (expressionS *ep, int cprestore)
438c16b8 9269{
df58fc94
RS
9270 static const bfd_reloc_code_real_type jalr_relocs[2]
9271 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
9272 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
9273 const char *jalr;
685736be 9274 char *f = NULL;
b34976b6 9275
1180b5a4 9276 if (MIPS_JALR_HINT_P (ep))
f21f8242 9277 {
cc3d92a5 9278 frag_grow (8);
f21f8242
AO
9279 f = frag_more (0);
9280 }
2906b037 9281 if (mips_opts.micromips)
df58fc94 9282 {
833794fc
MR
9283 jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
9284 ? "jalr" : "jalrs");
e64af278 9285 if (MIPS_JALR_HINT_P (ep)
833794fc 9286 || mips_opts.insn32
e64af278 9287 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
9288 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
9289 else
9290 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
9291 }
2906b037
MR
9292 else
9293 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
1180b5a4 9294 if (MIPS_JALR_HINT_P (ep))
df58fc94 9295 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
438c16b8
TS
9296}
9297
252b5132
RH
9298/*
9299 * Generate a "lui" instruction.
9300 */
9301static void
67c0d1eb 9302macro_build_lui (expressionS *ep, int regnum)
252b5132 9303{
9c2799c2 9304 gas_assert (! mips_opts.mips16);
252b5132 9305
df58fc94 9306 if (ep->X_op != O_constant)
252b5132 9307 {
9c2799c2 9308 gas_assert (ep->X_op == O_symbol);
bbe506e8
TS
9309 /* _gp_disp is a special case, used from s_cpload.
9310 __gnu_local_gp is used if mips_no_shared. */
9c2799c2 9311 gas_assert (mips_pic == NO_PIC
78e1bb40 9312 || (! HAVE_NEWABI
aa6975fb
ILT
9313 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
9314 || (! mips_in_shared
bbe506e8
TS
9315 && strcmp (S_GET_NAME (ep->X_add_symbol),
9316 "__gnu_local_gp") == 0));
252b5132
RH
9317 }
9318
df58fc94 9319 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
252b5132
RH
9320}
9321
885add95
CD
9322/* Generate a sequence of instructions to do a load or store from a constant
9323 offset off of a base register (breg) into/from a target register (treg),
9324 using AT if necessary. */
9325static void
67c0d1eb
RS
9326macro_build_ldst_constoffset (expressionS *ep, const char *op,
9327 int treg, int breg, int dbl)
885add95 9328{
9c2799c2 9329 gas_assert (ep->X_op == O_constant);
885add95 9330
256ab948 9331 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
9332 if (!dbl)
9333 normalize_constant_expr (ep);
256ab948 9334
67c1ffbe 9335 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 9336 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
9337 as_warn (_("operand overflow"));
9338
9339 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
9340 {
9341 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 9342 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
9343 }
9344 else
9345 {
9346 /* 32-bit offset, need multiple instructions and AT, like:
9347 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
9348 addu $tempreg,$tempreg,$breg
9349 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
9350 to handle the complete offset. */
67c0d1eb
RS
9351 macro_build_lui (ep, AT);
9352 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
9353 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95 9354
741fe287 9355 if (!mips_opts.at)
1661c76c 9356 as_bad (_("macro used $at after \".set noat\""));
885add95
CD
9357 }
9358}
9359
252b5132
RH
9360/* set_at()
9361 * Generates code to set the $at register to true (one)
9362 * if reg is less than the immediate expression.
9363 */
9364static void
67c0d1eb 9365set_at (int reg, int unsignedp)
252b5132 9366{
b0e6f033 9367 if (imm_expr.X_add_number >= -0x8000
252b5132 9368 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
9369 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
9370 AT, reg, BFD_RELOC_LO16);
252b5132
RH
9371 else
9372 {
bad1aba3 9373 load_register (AT, &imm_expr, GPR_SIZE == 64);
67c0d1eb 9374 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
9375 }
9376}
9377
252b5132
RH
9378/* Count the leading zeroes by performing a binary chop. This is a
9379 bulky bit of source, but performance is a LOT better for the
9380 majority of values than a simple loop to count the bits:
9381 for (lcnt = 0; (lcnt < 32); lcnt++)
9382 if ((v) & (1 << (31 - lcnt)))
9383 break;
9384 However it is not code size friendly, and the gain will drop a bit
9385 on certain cached systems.
9386*/
9387#define COUNT_TOP_ZEROES(v) \
9388 (((v) & ~0xffff) == 0 \
9389 ? ((v) & ~0xff) == 0 \
9390 ? ((v) & ~0xf) == 0 \
9391 ? ((v) & ~0x3) == 0 \
9392 ? ((v) & ~0x1) == 0 \
9393 ? !(v) \
9394 ? 32 \
9395 : 31 \
9396 : 30 \
9397 : ((v) & ~0x7) == 0 \
9398 ? 29 \
9399 : 28 \
9400 : ((v) & ~0x3f) == 0 \
9401 ? ((v) & ~0x1f) == 0 \
9402 ? 27 \
9403 : 26 \
9404 : ((v) & ~0x7f) == 0 \
9405 ? 25 \
9406 : 24 \
9407 : ((v) & ~0xfff) == 0 \
9408 ? ((v) & ~0x3ff) == 0 \
9409 ? ((v) & ~0x1ff) == 0 \
9410 ? 23 \
9411 : 22 \
9412 : ((v) & ~0x7ff) == 0 \
9413 ? 21 \
9414 : 20 \
9415 : ((v) & ~0x3fff) == 0 \
9416 ? ((v) & ~0x1fff) == 0 \
9417 ? 19 \
9418 : 18 \
9419 : ((v) & ~0x7fff) == 0 \
9420 ? 17 \
9421 : 16 \
9422 : ((v) & ~0xffffff) == 0 \
9423 ? ((v) & ~0xfffff) == 0 \
9424 ? ((v) & ~0x3ffff) == 0 \
9425 ? ((v) & ~0x1ffff) == 0 \
9426 ? 15 \
9427 : 14 \
9428 : ((v) & ~0x7ffff) == 0 \
9429 ? 13 \
9430 : 12 \
9431 : ((v) & ~0x3fffff) == 0 \
9432 ? ((v) & ~0x1fffff) == 0 \
9433 ? 11 \
9434 : 10 \
9435 : ((v) & ~0x7fffff) == 0 \
9436 ? 9 \
9437 : 8 \
9438 : ((v) & ~0xfffffff) == 0 \
9439 ? ((v) & ~0x3ffffff) == 0 \
9440 ? ((v) & ~0x1ffffff) == 0 \
9441 ? 7 \
9442 : 6 \
9443 : ((v) & ~0x7ffffff) == 0 \
9444 ? 5 \
9445 : 4 \
9446 : ((v) & ~0x3fffffff) == 0 \
9447 ? ((v) & ~0x1fffffff) == 0 \
9448 ? 3 \
9449 : 2 \
9450 : ((v) & ~0x7fffffff) == 0 \
9451 ? 1 \
9452 : 0)
9453
9454/* load_register()
67c1ffbe 9455 * This routine generates the least number of instructions necessary to load
252b5132
RH
9456 * an absolute expression value into a register.
9457 */
9458static void
67c0d1eb 9459load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
9460{
9461 int freg;
9462 expressionS hi32, lo32;
9463
9464 if (ep->X_op != O_big)
9465 {
9c2799c2 9466 gas_assert (ep->X_op == O_constant);
256ab948
TS
9467
9468 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
9469 if (!dbl)
9470 normalize_constant_expr (ep);
256ab948
TS
9471
9472 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
9473 {
9474 /* We can handle 16 bit signed values with an addiu to
9475 $zero. No need to ever use daddiu here, since $zero and
9476 the result are always correct in 32 bit mode. */
67c0d1eb 9477 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9478 return;
9479 }
9480 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
9481 {
9482 /* We can handle 16 bit unsigned values with an ori to
9483 $zero. */
67c0d1eb 9484 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9485 return;
9486 }
256ab948 9487 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
9488 {
9489 /* 32 bit values require an lui. */
df58fc94 9490 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 9491 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 9492 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
9493 return;
9494 }
9495 }
9496
9497 /* The value is larger than 32 bits. */
9498
bad1aba3 9499 if (!dbl || GPR_SIZE == 32)
252b5132 9500 {
55e08f71
NC
9501 char value[32];
9502
9503 sprintf_vma (value, ep->X_add_number);
1661c76c 9504 as_bad (_("number (0x%s) larger than 32 bits"), value);
67c0d1eb 9505 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9506 return;
9507 }
9508
9509 if (ep->X_op != O_big)
9510 {
9511 hi32 = *ep;
9512 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9513 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9514 hi32.X_add_number &= 0xffffffff;
9515 lo32 = *ep;
9516 lo32.X_add_number &= 0xffffffff;
9517 }
9518 else
9519 {
9c2799c2 9520 gas_assert (ep->X_add_number > 2);
252b5132
RH
9521 if (ep->X_add_number == 3)
9522 generic_bignum[3] = 0;
9523 else if (ep->X_add_number > 4)
1661c76c 9524 as_bad (_("number larger than 64 bits"));
252b5132
RH
9525 lo32.X_op = O_constant;
9526 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
9527 hi32.X_op = O_constant;
9528 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
9529 }
9530
9531 if (hi32.X_add_number == 0)
9532 freg = 0;
9533 else
9534 {
9535 int shift, bit;
9536 unsigned long hi, lo;
9537
956cd1d6 9538 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
9539 {
9540 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
9541 {
67c0d1eb 9542 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
9543 return;
9544 }
9545 if (lo32.X_add_number & 0x80000000)
9546 {
df58fc94 9547 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 9548 if (lo32.X_add_number & 0xffff)
67c0d1eb 9549 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
9550 return;
9551 }
9552 }
252b5132
RH
9553
9554 /* Check for 16bit shifted constant. We know that hi32 is
9555 non-zero, so start the mask on the first bit of the hi32
9556 value. */
9557 shift = 17;
9558 do
beae10d5
KH
9559 {
9560 unsigned long himask, lomask;
9561
9562 if (shift < 32)
9563 {
9564 himask = 0xffff >> (32 - shift);
9565 lomask = (0xffff << shift) & 0xffffffff;
9566 }
9567 else
9568 {
9569 himask = 0xffff << (shift - 32);
9570 lomask = 0;
9571 }
9572 if ((hi32.X_add_number & ~(offsetT) himask) == 0
9573 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
9574 {
9575 expressionS tmp;
9576
9577 tmp.X_op = O_constant;
9578 if (shift < 32)
9579 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
9580 | (lo32.X_add_number >> shift));
9581 else
9582 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb 9583 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
df58fc94 9584 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 9585 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
9586 return;
9587 }
f9419b05 9588 ++shift;
beae10d5
KH
9589 }
9590 while (shift <= (64 - 16));
252b5132
RH
9591
9592 /* Find the bit number of the lowest one bit, and store the
9593 shifted value in hi/lo. */
9594 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
9595 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
9596 if (lo != 0)
9597 {
9598 bit = 0;
9599 while ((lo & 1) == 0)
9600 {
9601 lo >>= 1;
9602 ++bit;
9603 }
9604 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
9605 hi >>= bit;
9606 }
9607 else
9608 {
9609 bit = 32;
9610 while ((hi & 1) == 0)
9611 {
9612 hi >>= 1;
9613 ++bit;
9614 }
9615 lo = hi;
9616 hi = 0;
9617 }
9618
9619 /* Optimize if the shifted value is a (power of 2) - 1. */
9620 if ((hi == 0 && ((lo + 1) & lo) == 0)
9621 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
9622 {
9623 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 9624 if (shift != 0)
beae10d5 9625 {
252b5132
RH
9626 expressionS tmp;
9627
9628 /* This instruction will set the register to be all
9629 ones. */
beae10d5
KH
9630 tmp.X_op = O_constant;
9631 tmp.X_add_number = (offsetT) -1;
67c0d1eb 9632 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
9633 if (bit != 0)
9634 {
9635 bit += shift;
df58fc94 9636 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 9637 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 9638 }
df58fc94 9639 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
67c0d1eb 9640 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
9641 return;
9642 }
9643 }
252b5132
RH
9644
9645 /* Sign extend hi32 before calling load_register, because we can
9646 generally get better code when we load a sign extended value. */
9647 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 9648 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 9649 load_register (reg, &hi32, 0);
252b5132
RH
9650 freg = reg;
9651 }
9652 if ((lo32.X_add_number & 0xffff0000) == 0)
9653 {
9654 if (freg != 0)
9655 {
df58fc94 9656 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
252b5132
RH
9657 freg = reg;
9658 }
9659 }
9660 else
9661 {
9662 expressionS mid16;
9663
956cd1d6 9664 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 9665 {
df58fc94
RS
9666 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9667 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
beae10d5
KH
9668 return;
9669 }
252b5132
RH
9670
9671 if (freg != 0)
9672 {
df58fc94 9673 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
252b5132
RH
9674 freg = reg;
9675 }
9676 mid16 = lo32;
9677 mid16.X_add_number >>= 16;
67c0d1eb 9678 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
df58fc94 9679 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
252b5132
RH
9680 freg = reg;
9681 }
9682 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 9683 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
9684}
9685
269137b2
TS
9686static inline void
9687load_delay_nop (void)
9688{
9689 if (!gpr_interlocks)
9690 macro_build (NULL, "nop", "");
9691}
9692
252b5132
RH
9693/* Load an address into a register. */
9694
9695static void
67c0d1eb 9696load_address (int reg, expressionS *ep, int *used_at)
252b5132 9697{
252b5132
RH
9698 if (ep->X_op != O_constant
9699 && ep->X_op != O_symbol)
9700 {
9701 as_bad (_("expression too complex"));
9702 ep->X_op = O_constant;
9703 }
9704
9705 if (ep->X_op == O_constant)
9706 {
67c0d1eb 9707 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
9708 return;
9709 }
9710
9711 if (mips_pic == NO_PIC)
9712 {
9713 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 9714 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
9715 Otherwise we want
9716 lui $reg,<sym> (BFD_RELOC_HI16_S)
9717 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 9718 If we have an addend, we always use the latter form.
76b3015f 9719
d6bc6245
TS
9720 With 64bit address space and a usable $at we want
9721 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9722 lui $at,<sym> (BFD_RELOC_HI16_S)
9723 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9724 daddiu $at,<sym> (BFD_RELOC_LO16)
9725 dsll32 $reg,0
3a482fd5 9726 daddu $reg,$reg,$at
76b3015f 9727
c03099e6 9728 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
9729 on superscalar processors.
9730 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9731 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9732 dsll $reg,16
9733 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
9734 dsll $reg,16
9735 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
9736
9737 For GP relative symbols in 64bit address space we can use
9738 the same sequence as in 32bit address space. */
aed1a261 9739 if (HAVE_64BIT_SYMBOLS)
d6bc6245 9740 {
6caf9ef4
TS
9741 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9742 && !nopic_need_relax (ep->X_add_symbol, 1))
9743 {
9744 relax_start (ep->X_add_symbol);
9745 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9746 mips_gp_register, BFD_RELOC_GPREL16);
9747 relax_switch ();
9748 }
d6bc6245 9749
741fe287 9750 if (*used_at == 0 && mips_opts.at)
d6bc6245 9751 {
df58fc94
RS
9752 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9753 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
67c0d1eb
RS
9754 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9755 BFD_RELOC_MIPS_HIGHER);
9756 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
df58fc94 9757 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
67c0d1eb 9758 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
9759 *used_at = 1;
9760 }
9761 else
9762 {
df58fc94 9763 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb
RS
9764 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9765 BFD_RELOC_MIPS_HIGHER);
df58fc94 9766 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 9767 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
df58fc94 9768 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 9769 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 9770 }
6caf9ef4
TS
9771
9772 if (mips_relax.sequence)
9773 relax_end ();
d6bc6245 9774 }
252b5132
RH
9775 else
9776 {
d6bc6245 9777 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 9778 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 9779 {
4d7206a2 9780 relax_start (ep->X_add_symbol);
67c0d1eb 9781 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 9782 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 9783 relax_switch ();
d6bc6245 9784 }
67c0d1eb
RS
9785 macro_build_lui (ep, reg);
9786 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
9787 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
9788 if (mips_relax.sequence)
9789 relax_end ();
d6bc6245 9790 }
252b5132 9791 }
0a44bf69 9792 else if (!mips_big_got)
252b5132
RH
9793 {
9794 expressionS ex;
9795
9796 /* If this is a reference to an external symbol, we want
9797 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9798 Otherwise we want
9799 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9800 nop
9801 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
9802 If there is a constant, it must be added in after.
9803
ed6fb7bd 9804 If we have NewABI, we want
f5040a92
AO
9805 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
9806 unless we're referencing a global symbol with a non-zero
9807 offset, in which case cst must be added separately. */
ed6fb7bd
SC
9808 if (HAVE_NEWABI)
9809 {
f5040a92
AO
9810 if (ep->X_add_number)
9811 {
4d7206a2 9812 ex.X_add_number = ep->X_add_number;
f5040a92 9813 ep->X_add_number = 0;
4d7206a2 9814 relax_start (ep->X_add_symbol);
67c0d1eb
RS
9815 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9816 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
9817 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9818 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9819 ex.X_op = O_constant;
67c0d1eb 9820 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 9821 reg, reg, BFD_RELOC_LO16);
f5040a92 9822 ep->X_add_number = ex.X_add_number;
4d7206a2 9823 relax_switch ();
f5040a92 9824 }
67c0d1eb 9825 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9826 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
9827 if (mips_relax.sequence)
9828 relax_end ();
ed6fb7bd
SC
9829 }
9830 else
9831 {
f5040a92
AO
9832 ex.X_add_number = ep->X_add_number;
9833 ep->X_add_number = 0;
67c0d1eb
RS
9834 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9835 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9836 load_delay_nop ();
4d7206a2
RS
9837 relax_start (ep->X_add_symbol);
9838 relax_switch ();
67c0d1eb 9839 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 9840 BFD_RELOC_LO16);
4d7206a2 9841 relax_end ();
ed6fb7bd 9842
f5040a92
AO
9843 if (ex.X_add_number != 0)
9844 {
9845 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9846 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9847 ex.X_op = O_constant;
67c0d1eb 9848 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 9849 reg, reg, BFD_RELOC_LO16);
f5040a92 9850 }
252b5132
RH
9851 }
9852 }
0a44bf69 9853 else if (mips_big_got)
252b5132
RH
9854 {
9855 expressionS ex;
252b5132
RH
9856
9857 /* This is the large GOT case. If this is a reference to an
9858 external symbol, we want
9859 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9860 addu $reg,$reg,$gp
9861 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
9862
9863 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
9864 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9865 nop
9866 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 9867 If there is a constant, it must be added in after.
f5040a92
AO
9868
9869 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
9870 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
9871 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 9872 */
438c16b8
TS
9873 if (HAVE_NEWABI)
9874 {
4d7206a2 9875 ex.X_add_number = ep->X_add_number;
f5040a92 9876 ep->X_add_number = 0;
4d7206a2 9877 relax_start (ep->X_add_symbol);
df58fc94 9878 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
9879 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9880 reg, reg, mips_gp_register);
9881 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9882 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
9883 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9884 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9885 else if (ex.X_add_number)
9886 {
9887 ex.X_op = O_constant;
67c0d1eb
RS
9888 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9889 BFD_RELOC_LO16);
f5040a92
AO
9890 }
9891
9892 ep->X_add_number = ex.X_add_number;
4d7206a2 9893 relax_switch ();
67c0d1eb 9894 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9895 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
9896 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9897 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 9898 relax_end ();
438c16b8 9899 }
252b5132 9900 else
438c16b8 9901 {
f5040a92
AO
9902 ex.X_add_number = ep->X_add_number;
9903 ep->X_add_number = 0;
4d7206a2 9904 relax_start (ep->X_add_symbol);
df58fc94 9905 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
9906 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9907 reg, reg, mips_gp_register);
9908 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9909 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
9910 relax_switch ();
9911 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
9912 {
9913 /* We need a nop before loading from $gp. This special
9914 check is required because the lui which starts the main
9915 instruction stream does not refer to $gp, and so will not
9916 insert the nop which may be required. */
67c0d1eb 9917 macro_build (NULL, "nop", "");
438c16b8 9918 }
67c0d1eb 9919 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9920 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9921 load_delay_nop ();
67c0d1eb 9922 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 9923 BFD_RELOC_LO16);
4d7206a2 9924 relax_end ();
438c16b8 9925
f5040a92
AO
9926 if (ex.X_add_number != 0)
9927 {
9928 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9929 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9930 ex.X_op = O_constant;
67c0d1eb
RS
9931 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9932 BFD_RELOC_LO16);
f5040a92 9933 }
252b5132
RH
9934 }
9935 }
252b5132
RH
9936 else
9937 abort ();
8fc2e39e 9938
741fe287 9939 if (!mips_opts.at && *used_at == 1)
1661c76c 9940 as_bad (_("macro used $at after \".set noat\""));
252b5132
RH
9941}
9942
ea1fb5dc
RS
9943/* Move the contents of register SOURCE into register DEST. */
9944
9945static void
67c0d1eb 9946move_register (int dest, int source)
ea1fb5dc 9947{
df58fc94
RS
9948 /* Prefer to use a 16-bit microMIPS instruction unless the previous
9949 instruction specifically requires a 32-bit one. */
9950 if (mips_opts.micromips
833794fc 9951 && !mips_opts.insn32
df58fc94 9952 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7951ca42 9953 macro_build (NULL, "move", "mp,mj", dest, source);
df58fc94 9954 else
40fc1451 9955 macro_build (NULL, "or", "d,v,t", dest, source, 0);
ea1fb5dc
RS
9956}
9957
4d7206a2 9958/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
9959 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
9960 The two alternatives are:
4d7206a2 9961
33eaf5de 9962 Global symbol Local symbol
4d7206a2
RS
9963 ------------- ------------
9964 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
9965 ... ...
9966 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
9967
9968 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
9969 emits the second for a 16-bit offset or add_got_offset_hilo emits
9970 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
9971
9972static void
67c0d1eb 9973load_got_offset (int dest, expressionS *local)
4d7206a2
RS
9974{
9975 expressionS global;
9976
9977 global = *local;
9978 global.X_add_number = 0;
9979
9980 relax_start (local->X_add_symbol);
67c0d1eb
RS
9981 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9982 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 9983 relax_switch ();
67c0d1eb
RS
9984 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9985 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
9986 relax_end ();
9987}
9988
9989static void
67c0d1eb 9990add_got_offset (int dest, expressionS *local)
4d7206a2
RS
9991{
9992 expressionS global;
9993
9994 global.X_op = O_constant;
9995 global.X_op_symbol = NULL;
9996 global.X_add_symbol = NULL;
9997 global.X_add_number = local->X_add_number;
9998
9999 relax_start (local->X_add_symbol);
67c0d1eb 10000 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
10001 dest, dest, BFD_RELOC_LO16);
10002 relax_switch ();
67c0d1eb 10003 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
10004 relax_end ();
10005}
10006
f6a22291
MR
10007static void
10008add_got_offset_hilo (int dest, expressionS *local, int tmp)
10009{
10010 expressionS global;
10011 int hold_mips_optimize;
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);
10019 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
10020 relax_switch ();
10021 /* Set mips_optimize around the lui instruction to avoid
10022 inserting an unnecessary nop after the lw. */
10023 hold_mips_optimize = mips_optimize;
10024 mips_optimize = 2;
10025 macro_build_lui (&global, tmp);
10026 mips_optimize = hold_mips_optimize;
10027 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
10028 relax_end ();
10029
10030 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
10031}
10032
df58fc94
RS
10033/* Emit a sequence of instructions to emulate a branch likely operation.
10034 BR is an ordinary branch corresponding to one to be emulated. BRNEG
10035 is its complementing branch with the original condition negated.
10036 CALL is set if the original branch specified the link operation.
10037 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
10038
10039 Code like this is produced in the noreorder mode:
10040
10041 BRNEG <args>, 1f
10042 nop
10043 b <sym>
10044 delay slot (executed only if branch taken)
10045 1:
10046
10047 or, if CALL is set:
10048
10049 BRNEG <args>, 1f
10050 nop
10051 bal <sym>
10052 delay slot (executed only if branch taken)
10053 1:
10054
10055 In the reorder mode the delay slot would be filled with a nop anyway,
10056 so code produced is simply:
10057
10058 BR <args>, <sym>
10059 nop
10060
10061 This function is used when producing code for the microMIPS ASE that
10062 does not implement branch likely instructions in hardware. */
10063
10064static void
10065macro_build_branch_likely (const char *br, const char *brneg,
10066 int call, expressionS *ep, const char *fmt,
10067 unsigned int sreg, unsigned int treg)
10068{
10069 int noreorder = mips_opts.noreorder;
10070 expressionS expr1;
10071
10072 gas_assert (mips_opts.micromips);
10073 start_noreorder ();
10074 if (noreorder)
10075 {
10076 micromips_label_expr (&expr1);
10077 macro_build (&expr1, brneg, fmt, sreg, treg);
10078 macro_build (NULL, "nop", "");
10079 macro_build (ep, call ? "bal" : "b", "p");
10080
10081 /* Set to true so that append_insn adds a label. */
10082 emit_branch_likely_macro = TRUE;
10083 }
10084 else
10085 {
10086 macro_build (ep, br, fmt, sreg, treg);
10087 macro_build (NULL, "nop", "");
10088 }
10089 end_noreorder ();
10090}
10091
10092/* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
10093 the condition code tested. EP specifies the branch target. */
10094
10095static void
10096macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
10097{
10098 const int call = 0;
10099 const char *brneg;
10100 const char *br;
10101
10102 switch (type)
10103 {
10104 case M_BC1FL:
10105 br = "bc1f";
10106 brneg = "bc1t";
10107 break;
10108 case M_BC1TL:
10109 br = "bc1t";
10110 brneg = "bc1f";
10111 break;
10112 case M_BC2FL:
10113 br = "bc2f";
10114 brneg = "bc2t";
10115 break;
10116 case M_BC2TL:
10117 br = "bc2t";
10118 brneg = "bc2f";
10119 break;
10120 default:
10121 abort ();
10122 }
10123 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
10124}
10125
10126/* Emit a two-argument branch macro specified by TYPE, using SREG as
10127 the register tested. EP specifies the branch target. */
10128
10129static void
10130macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
10131{
10132 const char *brneg = NULL;
10133 const char *br;
10134 int call = 0;
10135
10136 switch (type)
10137 {
10138 case M_BGEZ:
10139 br = "bgez";
10140 break;
10141 case M_BGEZL:
10142 br = mips_opts.micromips ? "bgez" : "bgezl";
10143 brneg = "bltz";
10144 break;
10145 case M_BGEZALL:
10146 gas_assert (mips_opts.micromips);
833794fc 10147 br = mips_opts.insn32 ? "bgezal" : "bgezals";
df58fc94
RS
10148 brneg = "bltz";
10149 call = 1;
10150 break;
10151 case M_BGTZ:
10152 br = "bgtz";
10153 break;
10154 case M_BGTZL:
10155 br = mips_opts.micromips ? "bgtz" : "bgtzl";
10156 brneg = "blez";
10157 break;
10158 case M_BLEZ:
10159 br = "blez";
10160 break;
10161 case M_BLEZL:
10162 br = mips_opts.micromips ? "blez" : "blezl";
10163 brneg = "bgtz";
10164 break;
10165 case M_BLTZ:
10166 br = "bltz";
10167 break;
10168 case M_BLTZL:
10169 br = mips_opts.micromips ? "bltz" : "bltzl";
10170 brneg = "bgez";
10171 break;
10172 case M_BLTZALL:
10173 gas_assert (mips_opts.micromips);
833794fc 10174 br = mips_opts.insn32 ? "bltzal" : "bltzals";
df58fc94
RS
10175 brneg = "bgez";
10176 call = 1;
10177 break;
10178 default:
10179 abort ();
10180 }
10181 if (mips_opts.micromips && brneg)
10182 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
10183 else
10184 macro_build (ep, br, "s,p", sreg);
10185}
10186
10187/* Emit a three-argument branch macro specified by TYPE, using SREG and
10188 TREG as the registers tested. EP specifies the branch target. */
10189
10190static void
10191macro_build_branch_rsrt (int type, expressionS *ep,
10192 unsigned int sreg, unsigned int treg)
10193{
10194 const char *brneg = NULL;
10195 const int call = 0;
10196 const char *br;
10197
10198 switch (type)
10199 {
10200 case M_BEQ:
10201 case M_BEQ_I:
10202 br = "beq";
10203 break;
10204 case M_BEQL:
10205 case M_BEQL_I:
10206 br = mips_opts.micromips ? "beq" : "beql";
10207 brneg = "bne";
10208 break;
10209 case M_BNE:
10210 case M_BNE_I:
10211 br = "bne";
10212 break;
10213 case M_BNEL:
10214 case M_BNEL_I:
10215 br = mips_opts.micromips ? "bne" : "bnel";
10216 brneg = "beq";
10217 break;
10218 default:
10219 abort ();
10220 }
10221 if (mips_opts.micromips && brneg)
10222 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
10223 else
10224 macro_build (ep, br, "s,t,p", sreg, treg);
10225}
10226
f2ae14a1
RS
10227/* Return the high part that should be loaded in order to make the low
10228 part of VALUE accessible using an offset of OFFBITS bits. */
10229
10230static offsetT
10231offset_high_part (offsetT value, unsigned int offbits)
10232{
10233 offsetT bias;
10234 addressT low_mask;
10235
10236 if (offbits == 0)
10237 return value;
10238 bias = 1 << (offbits - 1);
10239 low_mask = bias * 2 - 1;
10240 return (value + bias) & ~low_mask;
10241}
10242
10243/* Return true if the value stored in offset_expr and offset_reloc
10244 fits into a signed offset of OFFBITS bits. RANGE is the maximum
10245 amount that the caller wants to add without inducing overflow
10246 and ALIGN is the known alignment of the value in bytes. */
10247
10248static bfd_boolean
10249small_offset_p (unsigned int range, unsigned int align, unsigned int offbits)
10250{
10251 if (offbits == 16)
10252 {
10253 /* Accept any relocation operator if overflow isn't a concern. */
10254 if (range < align && *offset_reloc != BFD_RELOC_UNUSED)
10255 return TRUE;
10256
10257 /* These relocations are guaranteed not to overflow in correct links. */
10258 if (*offset_reloc == BFD_RELOC_MIPS_LITERAL
10259 || gprel16_reloc_p (*offset_reloc))
10260 return TRUE;
10261 }
10262 if (offset_expr.X_op == O_constant
10263 && offset_high_part (offset_expr.X_add_number, offbits) == 0
10264 && offset_high_part (offset_expr.X_add_number + range, offbits) == 0)
10265 return TRUE;
10266 return FALSE;
10267}
10268
252b5132
RH
10269/*
10270 * Build macros
10271 * This routine implements the seemingly endless macro or synthesized
10272 * instructions and addressing modes in the mips assembly language. Many
10273 * of these macros are simple and are similar to each other. These could
67c1ffbe 10274 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
10275 * this verbose method. Others are not simple macros but are more like
10276 * optimizing code generation.
10277 * One interesting optimization is when several store macros appear
67c1ffbe 10278 * consecutively that would load AT with the upper half of the same address.
2b0f3761 10279 * The ensuing load upper instructions are omitted. This implies some kind
252b5132
RH
10280 * of global optimization. We currently only optimize within a single macro.
10281 * For many of the load and store macros if the address is specified as a
10282 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
10283 * first load register 'at' with zero and use it as the base register. The
10284 * mips assembler simply uses register $zero. Just one tiny optimization
10285 * we're missing.
10286 */
10287static void
833794fc 10288macro (struct mips_cl_insn *ip, char *str)
252b5132 10289{
c0ebe874
RS
10290 const struct mips_operand_array *operands;
10291 unsigned int breg, i;
741fe287 10292 unsigned int tempreg;
252b5132 10293 int mask;
43841e91 10294 int used_at = 0;
df58fc94 10295 expressionS label_expr;
252b5132 10296 expressionS expr1;
df58fc94 10297 expressionS *ep;
252b5132
RH
10298 const char *s;
10299 const char *s2;
10300 const char *fmt;
10301 int likely = 0;
252b5132 10302 int coproc = 0;
7f3c4072 10303 int offbits = 16;
1abe91b1 10304 int call = 0;
df58fc94
RS
10305 int jals = 0;
10306 int dbl = 0;
10307 int imm = 0;
10308 int ust = 0;
10309 int lp = 0;
a45328b9 10310 int ll_sc_paired = 0;
f2ae14a1 10311 bfd_boolean large_offset;
252b5132 10312 int off;
252b5132 10313 int hold_mips_optimize;
f2ae14a1 10314 unsigned int align;
c0ebe874 10315 unsigned int op[MAX_OPERANDS];
252b5132 10316
9c2799c2 10317 gas_assert (! mips_opts.mips16);
252b5132 10318
c0ebe874
RS
10319 operands = insn_operands (ip);
10320 for (i = 0; i < MAX_OPERANDS; i++)
10321 if (operands->operand[i])
10322 op[i] = insn_extract_operand (ip, operands->operand[i]);
10323 else
10324 op[i] = -1;
10325
252b5132
RH
10326 mask = ip->insn_mo->mask;
10327
df58fc94
RS
10328 label_expr.X_op = O_constant;
10329 label_expr.X_op_symbol = NULL;
10330 label_expr.X_add_symbol = NULL;
10331 label_expr.X_add_number = 0;
10332
252b5132
RH
10333 expr1.X_op = O_constant;
10334 expr1.X_op_symbol = NULL;
10335 expr1.X_add_symbol = NULL;
10336 expr1.X_add_number = 1;
f2ae14a1 10337 align = 1;
252b5132
RH
10338
10339 switch (mask)
10340 {
10341 case M_DABS:
10342 dbl = 1;
1a0670f3 10343 /* Fall through. */
252b5132 10344 case M_ABS:
df58fc94
RS
10345 /* bgez $a0,1f
10346 move v0,$a0
10347 sub v0,$zero,$a0
10348 1:
10349 */
252b5132 10350
7d10b47d 10351 start_noreorder ();
252b5132 10352
df58fc94
RS
10353 if (mips_opts.micromips)
10354 micromips_label_expr (&label_expr);
10355 else
10356 label_expr.X_add_number = 8;
c0ebe874
RS
10357 macro_build (&label_expr, "bgez", "s,p", op[1]);
10358 if (op[0] == op[1])
a605d2b3 10359 macro_build (NULL, "nop", "");
252b5132 10360 else
c0ebe874
RS
10361 move_register (op[0], op[1]);
10362 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", op[0], 0, op[1]);
df58fc94
RS
10363 if (mips_opts.micromips)
10364 micromips_add_label ();
252b5132 10365
7d10b47d 10366 end_noreorder ();
8fc2e39e 10367 break;
252b5132
RH
10368
10369 case M_ADD_I:
10370 s = "addi";
10371 s2 = "add";
387e7624
FS
10372 if (ISA_IS_R6 (mips_opts.isa))
10373 goto do_addi_i;
10374 else
10375 goto do_addi;
252b5132
RH
10376 case M_ADDU_I:
10377 s = "addiu";
10378 s2 = "addu";
10379 goto do_addi;
10380 case M_DADD_I:
10381 dbl = 1;
10382 s = "daddi";
10383 s2 = "dadd";
387e7624 10384 if (!mips_opts.micromips && !ISA_IS_R6 (mips_opts.isa))
df58fc94 10385 goto do_addi;
b0e6f033 10386 if (imm_expr.X_add_number >= -0x200
387e7624
FS
10387 && imm_expr.X_add_number < 0x200
10388 && !ISA_IS_R6 (mips_opts.isa))
df58fc94 10389 {
b0e6f033
RS
10390 macro_build (NULL, s, "t,r,.", op[0], op[1],
10391 (int) imm_expr.X_add_number);
df58fc94
RS
10392 break;
10393 }
10394 goto do_addi_i;
252b5132
RH
10395 case M_DADDU_I:
10396 dbl = 1;
10397 s = "daddiu";
10398 s2 = "daddu";
10399 do_addi:
b0e6f033 10400 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
10401 && imm_expr.X_add_number < 0x8000)
10402 {
c0ebe874 10403 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 10404 break;
252b5132 10405 }
df58fc94 10406 do_addi_i:
8fc2e39e 10407 used_at = 1;
67c0d1eb 10408 load_register (AT, &imm_expr, dbl);
c0ebe874 10409 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
10410 break;
10411
10412 case M_AND_I:
10413 s = "andi";
10414 s2 = "and";
10415 goto do_bit;
10416 case M_OR_I:
10417 s = "ori";
10418 s2 = "or";
10419 goto do_bit;
10420 case M_NOR_I:
10421 s = "";
10422 s2 = "nor";
10423 goto do_bit;
10424 case M_XOR_I:
10425 s = "xori";
10426 s2 = "xor";
10427 do_bit:
b0e6f033 10428 if (imm_expr.X_add_number >= 0
252b5132
RH
10429 && imm_expr.X_add_number < 0x10000)
10430 {
10431 if (mask != M_NOR_I)
c0ebe874 10432 macro_build (&imm_expr, s, "t,r,i", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
10433 else
10434 {
67c0d1eb 10435 macro_build (&imm_expr, "ori", "t,r,i",
c0ebe874
RS
10436 op[0], op[1], BFD_RELOC_LO16);
10437 macro_build (NULL, "nor", "d,v,t", op[0], op[0], 0);
252b5132 10438 }
8fc2e39e 10439 break;
252b5132
RH
10440 }
10441
8fc2e39e 10442 used_at = 1;
bad1aba3 10443 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 10444 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
10445 break;
10446
8b082fb1
TS
10447 case M_BALIGN:
10448 switch (imm_expr.X_add_number)
10449 {
10450 case 0:
10451 macro_build (NULL, "nop", "");
10452 break;
10453 case 2:
c0ebe874 10454 macro_build (NULL, "packrl.ph", "d,s,t", op[0], op[0], op[1]);
8b082fb1 10455 break;
03f66e8a
MR
10456 case 1:
10457 case 3:
c0ebe874 10458 macro_build (NULL, "balign", "t,s,2", op[0], op[1],
90ecf173 10459 (int) imm_expr.X_add_number);
8b082fb1 10460 break;
03f66e8a
MR
10461 default:
10462 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
10463 (unsigned long) imm_expr.X_add_number);
10464 break;
8b082fb1
TS
10465 }
10466 break;
10467
df58fc94
RS
10468 case M_BC1FL:
10469 case M_BC1TL:
10470 case M_BC2FL:
10471 case M_BC2TL:
10472 gas_assert (mips_opts.micromips);
10473 macro_build_branch_ccl (mask, &offset_expr,
10474 EXTRACT_OPERAND (1, BCC, *ip));
10475 break;
10476
252b5132 10477 case M_BEQ_I:
252b5132 10478 case M_BEQL_I:
252b5132 10479 case M_BNE_I:
252b5132 10480 case M_BNEL_I:
b0e6f033 10481 if (imm_expr.X_add_number == 0)
c0ebe874 10482 op[1] = 0;
df58fc94 10483 else
252b5132 10484 {
c0ebe874 10485 op[1] = AT;
df58fc94 10486 used_at = 1;
bad1aba3 10487 load_register (op[1], &imm_expr, GPR_SIZE == 64);
252b5132 10488 }
df58fc94
RS
10489 /* Fall through. */
10490 case M_BEQL:
10491 case M_BNEL:
c0ebe874 10492 macro_build_branch_rsrt (mask, &offset_expr, op[0], op[1]);
252b5132
RH
10493 break;
10494
10495 case M_BGEL:
10496 likely = 1;
1a0670f3 10497 /* Fall through. */
252b5132 10498 case M_BGE:
c0ebe874
RS
10499 if (op[1] == 0)
10500 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[0]);
10501 else if (op[0] == 0)
10502 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[1]);
df58fc94 10503 else
252b5132 10504 {
df58fc94 10505 used_at = 1;
c0ebe874 10506 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10507 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10508 &offset_expr, AT, ZERO);
252b5132 10509 }
df58fc94
RS
10510 break;
10511
10512 case M_BGEZL:
10513 case M_BGEZALL:
10514 case M_BGTZL:
10515 case M_BLEZL:
10516 case M_BLTZL:
10517 case M_BLTZALL:
c0ebe874 10518 macro_build_branch_rs (mask, &offset_expr, op[0]);
252b5132
RH
10519 break;
10520
10521 case M_BGTL_I:
10522 likely = 1;
1a0670f3 10523 /* Fall through. */
252b5132 10524 case M_BGT_I:
90ecf173 10525 /* Check for > max integer. */
b0e6f033 10526 if (imm_expr.X_add_number >= GPR_SMAX)
252b5132
RH
10527 {
10528 do_false:
90ecf173 10529 /* Result is always false. */
252b5132 10530 if (! likely)
a605d2b3 10531 macro_build (NULL, "nop", "");
252b5132 10532 else
df58fc94 10533 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
8fc2e39e 10534 break;
252b5132 10535 }
f9419b05 10536 ++imm_expr.X_add_number;
6f2117ba 10537 /* Fall through. */
252b5132
RH
10538 case M_BGE_I:
10539 case M_BGEL_I:
10540 if (mask == M_BGEL_I)
10541 likely = 1;
b0e6f033 10542 if (imm_expr.X_add_number == 0)
252b5132 10543 {
df58fc94 10544 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
c0ebe874 10545 &offset_expr, op[0]);
8fc2e39e 10546 break;
252b5132 10547 }
b0e6f033 10548 if (imm_expr.X_add_number == 1)
252b5132 10549 {
df58fc94 10550 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
c0ebe874 10551 &offset_expr, op[0]);
8fc2e39e 10552 break;
252b5132 10553 }
b0e6f033 10554 if (imm_expr.X_add_number <= GPR_SMIN)
252b5132
RH
10555 {
10556 do_true:
6f2117ba 10557 /* Result is always true. */
1661c76c 10558 as_warn (_("branch %s is always true"), ip->insn_mo->name);
67c0d1eb 10559 macro_build (&offset_expr, "b", "p");
8fc2e39e 10560 break;
252b5132 10561 }
8fc2e39e 10562 used_at = 1;
c0ebe874 10563 set_at (op[0], 0);
df58fc94
RS
10564 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10565 &offset_expr, AT, ZERO);
252b5132
RH
10566 break;
10567
10568 case M_BGEUL:
10569 likely = 1;
1a0670f3 10570 /* Fall through. */
252b5132 10571 case M_BGEU:
c0ebe874 10572 if (op[1] == 0)
252b5132 10573 goto do_true;
c0ebe874 10574 else if (op[0] == 0)
df58fc94 10575 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 10576 &offset_expr, ZERO, op[1]);
df58fc94 10577 else
252b5132 10578 {
df58fc94 10579 used_at = 1;
c0ebe874 10580 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10581 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10582 &offset_expr, AT, ZERO);
252b5132 10583 }
252b5132
RH
10584 break;
10585
10586 case M_BGTUL_I:
10587 likely = 1;
1a0670f3 10588 /* Fall through. */
252b5132 10589 case M_BGTU_I:
c0ebe874 10590 if (op[0] == 0
bad1aba3 10591 || (GPR_SIZE == 32
f01dc953 10592 && imm_expr.X_add_number == -1))
252b5132 10593 goto do_false;
f9419b05 10594 ++imm_expr.X_add_number;
6f2117ba 10595 /* Fall through. */
252b5132
RH
10596 case M_BGEU_I:
10597 case M_BGEUL_I:
10598 if (mask == M_BGEUL_I)
10599 likely = 1;
b0e6f033 10600 if (imm_expr.X_add_number == 0)
252b5132 10601 goto do_true;
b0e6f033 10602 else if (imm_expr.X_add_number == 1)
df58fc94 10603 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 10604 &offset_expr, op[0], ZERO);
df58fc94 10605 else
252b5132 10606 {
df58fc94 10607 used_at = 1;
c0ebe874 10608 set_at (op[0], 1);
df58fc94
RS
10609 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10610 &offset_expr, AT, ZERO);
252b5132 10611 }
252b5132
RH
10612 break;
10613
10614 case M_BGTL:
10615 likely = 1;
1a0670f3 10616 /* Fall through. */
252b5132 10617 case M_BGT:
c0ebe874
RS
10618 if (op[1] == 0)
10619 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[0]);
10620 else if (op[0] == 0)
10621 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[1]);
df58fc94 10622 else
252b5132 10623 {
df58fc94 10624 used_at = 1;
c0ebe874 10625 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10626 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10627 &offset_expr, AT, ZERO);
252b5132 10628 }
252b5132
RH
10629 break;
10630
10631 case M_BGTUL:
10632 likely = 1;
1a0670f3 10633 /* Fall through. */
252b5132 10634 case M_BGTU:
c0ebe874 10635 if (op[1] == 0)
df58fc94 10636 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874
RS
10637 &offset_expr, op[0], ZERO);
10638 else if (op[0] == 0)
df58fc94
RS
10639 goto do_false;
10640 else
252b5132 10641 {
df58fc94 10642 used_at = 1;
c0ebe874 10643 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10644 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10645 &offset_expr, AT, ZERO);
252b5132 10646 }
252b5132
RH
10647 break;
10648
10649 case M_BLEL:
10650 likely = 1;
1a0670f3 10651 /* Fall through. */
252b5132 10652 case M_BLE:
c0ebe874
RS
10653 if (op[1] == 0)
10654 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10655 else if (op[0] == 0)
10656 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[1]);
df58fc94 10657 else
252b5132 10658 {
df58fc94 10659 used_at = 1;
c0ebe874 10660 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10661 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10662 &offset_expr, AT, ZERO);
252b5132 10663 }
252b5132
RH
10664 break;
10665
10666 case M_BLEL_I:
10667 likely = 1;
1a0670f3 10668 /* Fall through. */
252b5132 10669 case M_BLE_I:
b0e6f033 10670 if (imm_expr.X_add_number >= GPR_SMAX)
252b5132 10671 goto do_true;
f9419b05 10672 ++imm_expr.X_add_number;
6f2117ba 10673 /* Fall through. */
252b5132
RH
10674 case M_BLT_I:
10675 case M_BLTL_I:
10676 if (mask == M_BLTL_I)
10677 likely = 1;
b0e6f033 10678 if (imm_expr.X_add_number == 0)
c0ebe874 10679 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
b0e6f033 10680 else if (imm_expr.X_add_number == 1)
c0ebe874 10681 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
df58fc94 10682 else
252b5132 10683 {
df58fc94 10684 used_at = 1;
c0ebe874 10685 set_at (op[0], 0);
df58fc94
RS
10686 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10687 &offset_expr, AT, ZERO);
252b5132 10688 }
252b5132
RH
10689 break;
10690
10691 case M_BLEUL:
10692 likely = 1;
1a0670f3 10693 /* Fall through. */
252b5132 10694 case M_BLEU:
c0ebe874 10695 if (op[1] == 0)
df58fc94 10696 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874
RS
10697 &offset_expr, op[0], ZERO);
10698 else if (op[0] == 0)
df58fc94
RS
10699 goto do_true;
10700 else
252b5132 10701 {
df58fc94 10702 used_at = 1;
c0ebe874 10703 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10704 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10705 &offset_expr, AT, ZERO);
252b5132 10706 }
252b5132
RH
10707 break;
10708
10709 case M_BLEUL_I:
10710 likely = 1;
1a0670f3 10711 /* Fall through. */
252b5132 10712 case M_BLEU_I:
c0ebe874 10713 if (op[0] == 0
bad1aba3 10714 || (GPR_SIZE == 32
f01dc953 10715 && imm_expr.X_add_number == -1))
252b5132 10716 goto do_true;
f9419b05 10717 ++imm_expr.X_add_number;
6f2117ba 10718 /* Fall through. */
252b5132
RH
10719 case M_BLTU_I:
10720 case M_BLTUL_I:
10721 if (mask == M_BLTUL_I)
10722 likely = 1;
b0e6f033 10723 if (imm_expr.X_add_number == 0)
252b5132 10724 goto do_false;
b0e6f033 10725 else if (imm_expr.X_add_number == 1)
df58fc94 10726 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 10727 &offset_expr, op[0], ZERO);
df58fc94 10728 else
252b5132 10729 {
df58fc94 10730 used_at = 1;
c0ebe874 10731 set_at (op[0], 1);
df58fc94
RS
10732 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10733 &offset_expr, AT, ZERO);
252b5132 10734 }
252b5132
RH
10735 break;
10736
10737 case M_BLTL:
10738 likely = 1;
1a0670f3 10739 /* Fall through. */
252b5132 10740 case M_BLT:
c0ebe874
RS
10741 if (op[1] == 0)
10742 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10743 else if (op[0] == 0)
10744 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[1]);
df58fc94 10745 else
252b5132 10746 {
df58fc94 10747 used_at = 1;
c0ebe874 10748 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10749 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10750 &offset_expr, AT, ZERO);
252b5132 10751 }
252b5132
RH
10752 break;
10753
10754 case M_BLTUL:
10755 likely = 1;
1a0670f3 10756 /* Fall through. */
252b5132 10757 case M_BLTU:
c0ebe874 10758 if (op[1] == 0)
252b5132 10759 goto do_false;
c0ebe874 10760 else if (op[0] == 0)
df58fc94 10761 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 10762 &offset_expr, ZERO, op[1]);
df58fc94 10763 else
252b5132 10764 {
df58fc94 10765 used_at = 1;
c0ebe874 10766 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10767 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10768 &offset_expr, AT, ZERO);
252b5132 10769 }
252b5132
RH
10770 break;
10771
10772 case M_DDIV_3:
10773 dbl = 1;
1a0670f3 10774 /* Fall through. */
252b5132
RH
10775 case M_DIV_3:
10776 s = "mflo";
10777 goto do_div3;
10778 case M_DREM_3:
10779 dbl = 1;
1a0670f3 10780 /* Fall through. */
252b5132
RH
10781 case M_REM_3:
10782 s = "mfhi";
10783 do_div3:
c0ebe874 10784 if (op[2] == 0)
252b5132 10785 {
1661c76c 10786 as_warn (_("divide by zero"));
252b5132 10787 if (mips_trap)
df58fc94 10788 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 10789 else
df58fc94 10790 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 10791 break;
252b5132
RH
10792 }
10793
7d10b47d 10794 start_noreorder ();
252b5132
RH
10795 if (mips_trap)
10796 {
c0ebe874
RS
10797 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10798 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
252b5132
RH
10799 }
10800 else
10801 {
df58fc94
RS
10802 if (mips_opts.micromips)
10803 micromips_label_expr (&label_expr);
10804 else
10805 label_expr.X_add_number = 8;
c0ebe874
RS
10806 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10807 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
df58fc94
RS
10808 macro_build (NULL, "break", BRK_FMT, 7);
10809 if (mips_opts.micromips)
10810 micromips_add_label ();
252b5132
RH
10811 }
10812 expr1.X_add_number = -1;
8fc2e39e 10813 used_at = 1;
f6a22291 10814 load_register (AT, &expr1, dbl);
df58fc94
RS
10815 if (mips_opts.micromips)
10816 micromips_label_expr (&label_expr);
10817 else
10818 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
c0ebe874 10819 macro_build (&label_expr, "bne", "s,t,p", op[2], AT);
252b5132
RH
10820 if (dbl)
10821 {
10822 expr1.X_add_number = 1;
f6a22291 10823 load_register (AT, &expr1, dbl);
df58fc94 10824 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
252b5132
RH
10825 }
10826 else
10827 {
10828 expr1.X_add_number = 0x80000000;
df58fc94 10829 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
252b5132
RH
10830 }
10831 if (mips_trap)
10832 {
c0ebe874 10833 macro_build (NULL, "teq", TRAP_FMT, op[1], AT, 6);
252b5132
RH
10834 /* We want to close the noreorder block as soon as possible, so
10835 that later insns are available for delay slot filling. */
7d10b47d 10836 end_noreorder ();
252b5132
RH
10837 }
10838 else
10839 {
df58fc94
RS
10840 if (mips_opts.micromips)
10841 micromips_label_expr (&label_expr);
10842 else
10843 label_expr.X_add_number = 8;
c0ebe874 10844 macro_build (&label_expr, "bne", "s,t,p", op[1], AT);
a605d2b3 10845 macro_build (NULL, "nop", "");
252b5132
RH
10846
10847 /* We want to close the noreorder block as soon as possible, so
10848 that later insns are available for delay slot filling. */
7d10b47d 10849 end_noreorder ();
252b5132 10850
df58fc94 10851 macro_build (NULL, "break", BRK_FMT, 6);
252b5132 10852 }
df58fc94
RS
10853 if (mips_opts.micromips)
10854 micromips_add_label ();
c0ebe874 10855 macro_build (NULL, s, MFHL_FMT, op[0]);
252b5132
RH
10856 break;
10857
10858 case M_DIV_3I:
10859 s = "div";
10860 s2 = "mflo";
10861 goto do_divi;
10862 case M_DIVU_3I:
10863 s = "divu";
10864 s2 = "mflo";
10865 goto do_divi;
10866 case M_REM_3I:
10867 s = "div";
10868 s2 = "mfhi";
10869 goto do_divi;
10870 case M_REMU_3I:
10871 s = "divu";
10872 s2 = "mfhi";
10873 goto do_divi;
10874 case M_DDIV_3I:
10875 dbl = 1;
10876 s = "ddiv";
10877 s2 = "mflo";
10878 goto do_divi;
10879 case M_DDIVU_3I:
10880 dbl = 1;
10881 s = "ddivu";
10882 s2 = "mflo";
10883 goto do_divi;
10884 case M_DREM_3I:
10885 dbl = 1;
10886 s = "ddiv";
10887 s2 = "mfhi";
10888 goto do_divi;
10889 case M_DREMU_3I:
10890 dbl = 1;
10891 s = "ddivu";
10892 s2 = "mfhi";
10893 do_divi:
b0e6f033 10894 if (imm_expr.X_add_number == 0)
252b5132 10895 {
1661c76c 10896 as_warn (_("divide by zero"));
252b5132 10897 if (mips_trap)
df58fc94 10898 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 10899 else
df58fc94 10900 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 10901 break;
252b5132 10902 }
b0e6f033 10903 if (imm_expr.X_add_number == 1)
252b5132
RH
10904 {
10905 if (strcmp (s2, "mflo") == 0)
c0ebe874 10906 move_register (op[0], op[1]);
252b5132 10907 else
c0ebe874 10908 move_register (op[0], ZERO);
8fc2e39e 10909 break;
252b5132 10910 }
b0e6f033 10911 if (imm_expr.X_add_number == -1 && s[strlen (s) - 1] != 'u')
252b5132
RH
10912 {
10913 if (strcmp (s2, "mflo") == 0)
c0ebe874 10914 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", op[0], op[1]);
252b5132 10915 else
c0ebe874 10916 move_register (op[0], ZERO);
8fc2e39e 10917 break;
252b5132
RH
10918 }
10919
8fc2e39e 10920 used_at = 1;
67c0d1eb 10921 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
10922 macro_build (NULL, s, "z,s,t", op[1], AT);
10923 macro_build (NULL, s2, MFHL_FMT, op[0]);
252b5132
RH
10924 break;
10925
10926 case M_DIVU_3:
10927 s = "divu";
10928 s2 = "mflo";
10929 goto do_divu3;
10930 case M_REMU_3:
10931 s = "divu";
10932 s2 = "mfhi";
10933 goto do_divu3;
10934 case M_DDIVU_3:
10935 s = "ddivu";
10936 s2 = "mflo";
10937 goto do_divu3;
10938 case M_DREMU_3:
10939 s = "ddivu";
10940 s2 = "mfhi";
10941 do_divu3:
7d10b47d 10942 start_noreorder ();
252b5132
RH
10943 if (mips_trap)
10944 {
c0ebe874
RS
10945 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10946 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
10947 /* We want to close the noreorder block as soon as possible, so
10948 that later insns are available for delay slot filling. */
7d10b47d 10949 end_noreorder ();
252b5132
RH
10950 }
10951 else
10952 {
df58fc94
RS
10953 if (mips_opts.micromips)
10954 micromips_label_expr (&label_expr);
10955 else
10956 label_expr.X_add_number = 8;
c0ebe874
RS
10957 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10958 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
10959
10960 /* We want to close the noreorder block as soon as possible, so
10961 that later insns are available for delay slot filling. */
7d10b47d 10962 end_noreorder ();
df58fc94
RS
10963 macro_build (NULL, "break", BRK_FMT, 7);
10964 if (mips_opts.micromips)
10965 micromips_add_label ();
252b5132 10966 }
c0ebe874 10967 macro_build (NULL, s2, MFHL_FMT, op[0]);
8fc2e39e 10968 break;
252b5132 10969
1abe91b1
MR
10970 case M_DLCA_AB:
10971 dbl = 1;
1a0670f3 10972 /* Fall through. */
1abe91b1
MR
10973 case M_LCA_AB:
10974 call = 1;
10975 goto do_la;
252b5132
RH
10976 case M_DLA_AB:
10977 dbl = 1;
1a0670f3 10978 /* Fall through. */
252b5132 10979 case M_LA_AB:
1abe91b1 10980 do_la:
252b5132
RH
10981 /* Load the address of a symbol into a register. If breg is not
10982 zero, we then add a base register to it. */
10983
c0ebe874 10984 breg = op[2];
bad1aba3 10985 if (dbl && GPR_SIZE == 32)
ece794d9
MF
10986 as_warn (_("dla used to load 32-bit register; recommend using la "
10987 "instead"));
3bec30a8 10988
90ecf173 10989 if (!dbl && HAVE_64BIT_OBJECTS)
ece794d9
MF
10990 as_warn (_("la used to load 64-bit address; recommend using dla "
10991 "instead"));
3bec30a8 10992
f2ae14a1 10993 if (small_offset_p (0, align, 16))
0c11417f 10994 {
c0ebe874 10995 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", op[0], breg,
f2ae14a1 10996 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8fc2e39e 10997 break;
0c11417f
MR
10998 }
10999
c0ebe874 11000 if (mips_opts.at && (op[0] == breg))
afdbd6d0
CD
11001 {
11002 tempreg = AT;
11003 used_at = 1;
11004 }
11005 else
c0ebe874 11006 tempreg = op[0];
afdbd6d0 11007
252b5132
RH
11008 if (offset_expr.X_op != O_symbol
11009 && offset_expr.X_op != O_constant)
11010 {
1661c76c 11011 as_bad (_("expression too complex"));
252b5132
RH
11012 offset_expr.X_op = O_constant;
11013 }
11014
252b5132 11015 if (offset_expr.X_op == O_constant)
aed1a261 11016 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
11017 else if (mips_pic == NO_PIC)
11018 {
d6bc6245 11019 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 11020 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
11021 Otherwise we want
11022 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
11023 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
11024 If we have a constant, we need two instructions anyhow,
d6bc6245 11025 so we may as well always use the latter form.
76b3015f 11026
6caf9ef4
TS
11027 With 64bit address space and a usable $at we want
11028 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11029 lui $at,<sym> (BFD_RELOC_HI16_S)
11030 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11031 daddiu $at,<sym> (BFD_RELOC_LO16)
11032 dsll32 $tempreg,0
11033 daddu $tempreg,$tempreg,$at
11034
11035 If $at is already in use, we use a path which is suboptimal
11036 on superscalar processors.
11037 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11038 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11039 dsll $tempreg,16
11040 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
11041 dsll $tempreg,16
11042 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
11043
11044 For GP relative symbols in 64bit address space we can use
11045 the same sequence as in 32bit address space. */
aed1a261 11046 if (HAVE_64BIT_SYMBOLS)
252b5132 11047 {
6caf9ef4
TS
11048 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
11049 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
11050 {
11051 relax_start (offset_expr.X_add_symbol);
11052 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11053 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
11054 relax_switch ();
11055 }
d6bc6245 11056
741fe287 11057 if (used_at == 0 && mips_opts.at)
98d3f06f 11058 {
df58fc94 11059 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 11060 tempreg, BFD_RELOC_MIPS_HIGHEST);
df58fc94 11061 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 11062 AT, BFD_RELOC_HI16_S);
67c0d1eb 11063 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 11064 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 11065 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 11066 AT, AT, BFD_RELOC_LO16);
df58fc94 11067 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 11068 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
11069 used_at = 1;
11070 }
11071 else
11072 {
df58fc94 11073 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 11074 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 11075 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 11076 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 11077 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 11078 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 11079 tempreg, tempreg, BFD_RELOC_HI16_S);
df58fc94 11080 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 11081 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 11082 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 11083 }
6caf9ef4
TS
11084
11085 if (mips_relax.sequence)
11086 relax_end ();
98d3f06f
KH
11087 }
11088 else
11089 {
11090 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 11091 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 11092 {
4d7206a2 11093 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11094 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11095 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 11096 relax_switch ();
98d3f06f 11097 }
6943caf0 11098 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
1661c76c 11099 as_bad (_("offset too large"));
67c0d1eb
RS
11100 macro_build_lui (&offset_expr, tempreg);
11101 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11102 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
11103 if (mips_relax.sequence)
11104 relax_end ();
98d3f06f 11105 }
252b5132 11106 }
0a44bf69 11107 else if (!mips_big_got && !HAVE_NEWABI)
252b5132 11108 {
9117d219
NC
11109 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
11110
252b5132
RH
11111 /* If this is a reference to an external symbol, and there
11112 is no constant, we want
11113 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 11114 or for lca or if tempreg is PIC_CALL_REG
9117d219 11115 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
11116 For a local symbol, we want
11117 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11118 nop
11119 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
11120
11121 If we have a small constant, and this is a reference to
11122 an external symbol, we want
11123 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11124 nop
11125 addiu $tempreg,$tempreg,<constant>
11126 For a local symbol, we want the same instruction
11127 sequence, but we output a BFD_RELOC_LO16 reloc on the
11128 addiu instruction.
11129
11130 If we have a large constant, and this is a reference to
11131 an external symbol, we want
11132 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11133 lui $at,<hiconstant>
11134 addiu $at,$at,<loconstant>
11135 addu $tempreg,$tempreg,$at
11136 For a local symbol, we want the same instruction
11137 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 11138 addiu instruction.
ed6fb7bd
SC
11139 */
11140
4d7206a2 11141 if (offset_expr.X_add_number == 0)
252b5132 11142 {
0a44bf69
RS
11143 if (mips_pic == SVR4_PIC
11144 && breg == 0
11145 && (call || tempreg == PIC_CALL_REG))
4d7206a2
RS
11146 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
11147
11148 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11149 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11150 lw_reloc_type, mips_gp_register);
4d7206a2 11151 if (breg != 0)
252b5132
RH
11152 {
11153 /* We're going to put in an addu instruction using
11154 tempreg, so we may as well insert the nop right
11155 now. */
269137b2 11156 load_delay_nop ();
252b5132 11157 }
4d7206a2 11158 relax_switch ();
67c0d1eb
RS
11159 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11160 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 11161 load_delay_nop ();
67c0d1eb
RS
11162 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11163 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 11164 relax_end ();
252b5132
RH
11165 /* FIXME: If breg == 0, and the next instruction uses
11166 $tempreg, then if this variant case is used an extra
11167 nop will be generated. */
11168 }
4d7206a2
RS
11169 else if (offset_expr.X_add_number >= -0x8000
11170 && offset_expr.X_add_number < 0x8000)
252b5132 11171 {
67c0d1eb 11172 load_got_offset (tempreg, &offset_expr);
269137b2 11173 load_delay_nop ();
67c0d1eb 11174 add_got_offset (tempreg, &offset_expr);
252b5132
RH
11175 }
11176 else
11177 {
4d7206a2
RS
11178 expr1.X_add_number = offset_expr.X_add_number;
11179 offset_expr.X_add_number =
43c0598f 11180 SEXT_16BIT (offset_expr.X_add_number);
67c0d1eb 11181 load_got_offset (tempreg, &offset_expr);
f6a22291 11182 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
11183 /* If we are going to add in a base register, and the
11184 target register and the base register are the same,
11185 then we are using AT as a temporary register. Since
11186 we want to load the constant into AT, we add our
11187 current AT (from the global offset table) and the
11188 register into the register now, and pretend we were
11189 not using a base register. */
c0ebe874 11190 if (breg == op[0])
252b5132 11191 {
269137b2 11192 load_delay_nop ();
67c0d1eb 11193 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 11194 op[0], AT, breg);
252b5132 11195 breg = 0;
c0ebe874 11196 tempreg = op[0];
252b5132 11197 }
f6a22291 11198 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
11199 used_at = 1;
11200 }
11201 }
0a44bf69 11202 else if (!mips_big_got && HAVE_NEWABI)
f5040a92 11203 {
67c0d1eb 11204 int add_breg_early = 0;
f5040a92
AO
11205
11206 /* If this is a reference to an external, and there is no
11207 constant, or local symbol (*), with or without a
11208 constant, we want
11209 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 11210 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
11211 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11212
11213 If we have a small constant, and this is a reference to
11214 an external symbol, we want
11215 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
11216 addiu $tempreg,$tempreg,<constant>
11217
11218 If we have a large constant, and this is a reference to
11219 an external symbol, we want
11220 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
11221 lui $at,<hiconstant>
11222 addiu $at,$at,<loconstant>
11223 addu $tempreg,$tempreg,$at
11224
11225 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
11226 local symbols, even though it introduces an additional
11227 instruction. */
11228
f5040a92
AO
11229 if (offset_expr.X_add_number)
11230 {
4d7206a2 11231 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
11232 offset_expr.X_add_number = 0;
11233
4d7206a2 11234 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11235 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11236 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
11237
11238 if (expr1.X_add_number >= -0x8000
11239 && expr1.X_add_number < 0x8000)
11240 {
67c0d1eb
RS
11241 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11242 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 11243 }
ecd13cd3 11244 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 11245 {
c0ebe874
RS
11246 unsigned int dreg;
11247
f5040a92
AO
11248 /* If we are going to add in a base register, and the
11249 target register and the base register are the same,
11250 then we are using AT as a temporary register. Since
11251 we want to load the constant into AT, we add our
11252 current AT (from the global offset table) and the
11253 register into the register now, and pretend we were
11254 not using a base register. */
c0ebe874 11255 if (breg != op[0])
f5040a92
AO
11256 dreg = tempreg;
11257 else
11258 {
9c2799c2 11259 gas_assert (tempreg == AT);
67c0d1eb 11260 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11261 op[0], AT, breg);
11262 dreg = op[0];
67c0d1eb 11263 add_breg_early = 1;
f5040a92
AO
11264 }
11265
f6a22291 11266 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 11267 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11268 dreg, dreg, AT);
f5040a92 11269
f5040a92
AO
11270 used_at = 1;
11271 }
11272 else
11273 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11274
4d7206a2 11275 relax_switch ();
f5040a92
AO
11276 offset_expr.X_add_number = expr1.X_add_number;
11277
67c0d1eb
RS
11278 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11279 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11280 if (add_breg_early)
f5040a92 11281 {
67c0d1eb 11282 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 11283 op[0], tempreg, breg);
f5040a92 11284 breg = 0;
c0ebe874 11285 tempreg = op[0];
f5040a92 11286 }
4d7206a2 11287 relax_end ();
f5040a92 11288 }
4d7206a2 11289 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 11290 {
4d7206a2 11291 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11292 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11293 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 11294 relax_switch ();
67c0d1eb
RS
11295 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11296 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 11297 relax_end ();
f5040a92 11298 }
4d7206a2 11299 else
f5040a92 11300 {
67c0d1eb
RS
11301 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11302 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
11303 }
11304 }
0a44bf69 11305 else if (mips_big_got && !HAVE_NEWABI)
252b5132 11306 {
67c0d1eb 11307 int gpdelay;
9117d219
NC
11308 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11309 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 11310 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
11311
11312 /* This is the large GOT case. If this is a reference to an
11313 external symbol, and there is no constant, we want
11314 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11315 addu $tempreg,$tempreg,$gp
11316 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 11317 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
11318 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11319 addu $tempreg,$tempreg,$gp
11320 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
11321 For a local symbol, we want
11322 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11323 nop
11324 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
11325
11326 If we have a small constant, and this is a reference to
11327 an external symbol, we want
11328 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11329 addu $tempreg,$tempreg,$gp
11330 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11331 nop
11332 addiu $tempreg,$tempreg,<constant>
11333 For a local symbol, we want
11334 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11335 nop
11336 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
11337
11338 If we have a large constant, and this is a reference to
11339 an external symbol, we want
11340 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11341 addu $tempreg,$tempreg,$gp
11342 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11343 lui $at,<hiconstant>
11344 addiu $at,$at,<loconstant>
11345 addu $tempreg,$tempreg,$at
11346 For a local symbol, we want
11347 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11348 lui $at,<hiconstant>
11349 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
11350 addu $tempreg,$tempreg,$at
f5040a92 11351 */
438c16b8 11352
252b5132
RH
11353 expr1.X_add_number = offset_expr.X_add_number;
11354 offset_expr.X_add_number = 0;
4d7206a2 11355 relax_start (offset_expr.X_add_symbol);
67c0d1eb 11356 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
11357 if (expr1.X_add_number == 0 && breg == 0
11358 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
11359 {
11360 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11361 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11362 }
df58fc94 11363 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 11364 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11365 tempreg, tempreg, mips_gp_register);
67c0d1eb 11366 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 11367 tempreg, lw_reloc_type, tempreg);
252b5132
RH
11368 if (expr1.X_add_number == 0)
11369 {
67c0d1eb 11370 if (breg != 0)
252b5132
RH
11371 {
11372 /* We're going to put in an addu instruction using
11373 tempreg, so we may as well insert the nop right
11374 now. */
269137b2 11375 load_delay_nop ();
252b5132 11376 }
252b5132
RH
11377 }
11378 else if (expr1.X_add_number >= -0x8000
11379 && expr1.X_add_number < 0x8000)
11380 {
269137b2 11381 load_delay_nop ();
67c0d1eb 11382 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11383 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
11384 }
11385 else
11386 {
c0ebe874
RS
11387 unsigned int dreg;
11388
252b5132
RH
11389 /* If we are going to add in a base register, and the
11390 target register and the base register are the same,
11391 then we are using AT as a temporary register. Since
11392 we want to load the constant into AT, we add our
11393 current AT (from the global offset table) and the
11394 register into the register now, and pretend we were
11395 not using a base register. */
c0ebe874 11396 if (breg != op[0])
67c0d1eb 11397 dreg = tempreg;
252b5132
RH
11398 else
11399 {
9c2799c2 11400 gas_assert (tempreg == AT);
269137b2 11401 load_delay_nop ();
67c0d1eb 11402 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11403 op[0], AT, breg);
11404 dreg = op[0];
252b5132
RH
11405 }
11406
f6a22291 11407 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 11408 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 11409
252b5132
RH
11410 used_at = 1;
11411 }
43c0598f 11412 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
4d7206a2 11413 relax_switch ();
252b5132 11414
67c0d1eb 11415 if (gpdelay)
252b5132
RH
11416 {
11417 /* This is needed because this instruction uses $gp, but
f5040a92 11418 the first instruction on the main stream does not. */
67c0d1eb 11419 macro_build (NULL, "nop", "");
252b5132 11420 }
ed6fb7bd 11421
67c0d1eb
RS
11422 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11423 local_reloc_type, mips_gp_register);
f5040a92 11424 if (expr1.X_add_number >= -0x8000
252b5132
RH
11425 && expr1.X_add_number < 0x8000)
11426 {
269137b2 11427 load_delay_nop ();
67c0d1eb
RS
11428 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11429 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 11430 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
11431 register, the external symbol case ended with a load,
11432 so if the symbol turns out to not be external, and
11433 the next instruction uses tempreg, an unnecessary nop
11434 will be inserted. */
252b5132
RH
11435 }
11436 else
11437 {
c0ebe874 11438 if (breg == op[0])
252b5132
RH
11439 {
11440 /* We must add in the base register now, as in the
f5040a92 11441 external symbol case. */
9c2799c2 11442 gas_assert (tempreg == AT);
269137b2 11443 load_delay_nop ();
67c0d1eb 11444 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11445 op[0], AT, breg);
11446 tempreg = op[0];
252b5132 11447 /* We set breg to 0 because we have arranged to add
f5040a92 11448 it in in both cases. */
252b5132
RH
11449 breg = 0;
11450 }
11451
67c0d1eb
RS
11452 macro_build_lui (&expr1, AT);
11453 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11454 AT, AT, BFD_RELOC_LO16);
67c0d1eb 11455 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11456 tempreg, tempreg, AT);
8fc2e39e 11457 used_at = 1;
252b5132 11458 }
4d7206a2 11459 relax_end ();
252b5132 11460 }
0a44bf69 11461 else if (mips_big_got && HAVE_NEWABI)
f5040a92 11462 {
f5040a92
AO
11463 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11464 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 11465 int add_breg_early = 0;
f5040a92
AO
11466
11467 /* This is the large GOT case. If this is a reference to an
11468 external symbol, and there is no constant, we want
11469 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11470 add $tempreg,$tempreg,$gp
11471 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 11472 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
11473 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11474 add $tempreg,$tempreg,$gp
11475 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
11476
11477 If we have a small constant, and this is a reference to
11478 an external symbol, we want
11479 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11480 add $tempreg,$tempreg,$gp
11481 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11482 addi $tempreg,$tempreg,<constant>
11483
11484 If we have a large constant, and this is a reference to
11485 an external symbol, we want
11486 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11487 addu $tempreg,$tempreg,$gp
11488 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11489 lui $at,<hiconstant>
11490 addi $at,$at,<loconstant>
11491 add $tempreg,$tempreg,$at
11492
11493 If we have NewABI, and we know it's a local symbol, we want
11494 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
11495 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
11496 otherwise we have to resort to GOT_HI16/GOT_LO16. */
11497
4d7206a2 11498 relax_start (offset_expr.X_add_symbol);
f5040a92 11499
4d7206a2 11500 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
11501 offset_expr.X_add_number = 0;
11502
1abe91b1
MR
11503 if (expr1.X_add_number == 0 && breg == 0
11504 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
11505 {
11506 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11507 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11508 }
df58fc94 11509 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 11510 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11511 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
11512 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11513 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
11514
11515 if (expr1.X_add_number == 0)
4d7206a2 11516 ;
f5040a92
AO
11517 else if (expr1.X_add_number >= -0x8000
11518 && expr1.X_add_number < 0x8000)
11519 {
67c0d1eb 11520 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11521 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 11522 }
ecd13cd3 11523 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 11524 {
c0ebe874
RS
11525 unsigned int dreg;
11526
f5040a92
AO
11527 /* If we are going to add in a base register, and the
11528 target register and the base register are the same,
11529 then we are using AT as a temporary register. Since
11530 we want to load the constant into AT, we add our
11531 current AT (from the global offset table) and the
11532 register into the register now, and pretend we were
11533 not using a base register. */
c0ebe874 11534 if (breg != op[0])
f5040a92
AO
11535 dreg = tempreg;
11536 else
11537 {
9c2799c2 11538 gas_assert (tempreg == AT);
67c0d1eb 11539 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11540 op[0], AT, breg);
11541 dreg = op[0];
67c0d1eb 11542 add_breg_early = 1;
f5040a92
AO
11543 }
11544
f6a22291 11545 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 11546 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 11547
f5040a92
AO
11548 used_at = 1;
11549 }
11550 else
11551 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11552
4d7206a2 11553 relax_switch ();
f5040a92 11554 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
11555 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11556 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
11557 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11558 tempreg, BFD_RELOC_MIPS_GOT_OFST);
11559 if (add_breg_early)
f5040a92 11560 {
67c0d1eb 11561 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 11562 op[0], tempreg, breg);
f5040a92 11563 breg = 0;
c0ebe874 11564 tempreg = op[0];
f5040a92 11565 }
4d7206a2 11566 relax_end ();
f5040a92 11567 }
252b5132
RH
11568 else
11569 abort ();
11570
11571 if (breg != 0)
c0ebe874 11572 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", op[0], tempreg, breg);
252b5132
RH
11573 break;
11574
52b6b6b9 11575 case M_MSGSND:
df58fc94 11576 gas_assert (!mips_opts.micromips);
c0ebe874 11577 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x01);
c7af4273 11578 break;
52b6b6b9
JM
11579
11580 case M_MSGLD:
df58fc94 11581 gas_assert (!mips_opts.micromips);
c8276761 11582 macro_build (NULL, "c2", "C", 0x02);
c7af4273 11583 break;
52b6b6b9
JM
11584
11585 case M_MSGLD_T:
df58fc94 11586 gas_assert (!mips_opts.micromips);
c0ebe874 11587 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x02);
c7af4273 11588 break;
52b6b6b9
JM
11589
11590 case M_MSGWAIT:
df58fc94 11591 gas_assert (!mips_opts.micromips);
52b6b6b9 11592 macro_build (NULL, "c2", "C", 3);
c7af4273 11593 break;
52b6b6b9
JM
11594
11595 case M_MSGWAIT_T:
df58fc94 11596 gas_assert (!mips_opts.micromips);
c0ebe874 11597 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x03);
c7af4273 11598 break;
52b6b6b9 11599
252b5132
RH
11600 case M_J_A:
11601 /* The j instruction may not be used in PIC code, since it
11602 requires an absolute address. We convert it to a b
11603 instruction. */
11604 if (mips_pic == NO_PIC)
67c0d1eb 11605 macro_build (&offset_expr, "j", "a");
252b5132 11606 else
67c0d1eb 11607 macro_build (&offset_expr, "b", "p");
8fc2e39e 11608 break;
252b5132
RH
11609
11610 /* The jal instructions must be handled as macros because when
11611 generating PIC code they expand to multi-instruction
11612 sequences. Normally they are simple instructions. */
df58fc94 11613 case M_JALS_1:
c0ebe874
RS
11614 op[1] = op[0];
11615 op[0] = RA;
df58fc94
RS
11616 /* Fall through. */
11617 case M_JALS_2:
11618 gas_assert (mips_opts.micromips);
833794fc
MR
11619 if (mips_opts.insn32)
11620 {
1661c76c 11621 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
833794fc
MR
11622 break;
11623 }
df58fc94
RS
11624 jals = 1;
11625 goto jal;
252b5132 11626 case M_JAL_1:
c0ebe874
RS
11627 op[1] = op[0];
11628 op[0] = RA;
252b5132
RH
11629 /* Fall through. */
11630 case M_JAL_2:
df58fc94 11631 jal:
3e722fb5 11632 if (mips_pic == NO_PIC)
df58fc94
RS
11633 {
11634 s = jals ? "jalrs" : "jalr";
e64af278 11635 if (mips_opts.micromips
833794fc 11636 && !mips_opts.insn32
c0ebe874 11637 && op[0] == RA
e64af278 11638 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 11639 macro_build (NULL, s, "mj", op[1]);
df58fc94 11640 else
c0ebe874 11641 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
df58fc94 11642 }
0a44bf69 11643 else
252b5132 11644 {
df58fc94
RS
11645 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
11646 && mips_cprestore_offset >= 0);
11647
c0ebe874 11648 if (op[1] != PIC_CALL_REG)
252b5132 11649 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 11650
833794fc
MR
11651 s = ((mips_opts.micromips
11652 && !mips_opts.insn32
11653 && (!mips_opts.noreorder || cprestore))
df58fc94 11654 ? "jalrs" : "jalr");
e64af278 11655 if (mips_opts.micromips
833794fc 11656 && !mips_opts.insn32
c0ebe874 11657 && op[0] == RA
e64af278 11658 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 11659 macro_build (NULL, s, "mj", op[1]);
df58fc94 11660 else
c0ebe874 11661 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
0a44bf69 11662 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
252b5132 11663 {
6478892d 11664 if (mips_cprestore_offset < 0)
1661c76c 11665 as_warn (_("no .cprestore pseudo-op used in PIC code"));
6478892d
TS
11666 else
11667 {
90ecf173 11668 if (!mips_frame_reg_valid)
7a621144 11669 {
1661c76c 11670 as_warn (_("no .frame pseudo-op used in PIC code"));
7a621144
DJ
11671 /* Quiet this warning. */
11672 mips_frame_reg_valid = 1;
11673 }
90ecf173 11674 if (!mips_cprestore_valid)
7a621144 11675 {
1661c76c 11676 as_warn (_("no .cprestore pseudo-op used in PIC code"));
7a621144
DJ
11677 /* Quiet this warning. */
11678 mips_cprestore_valid = 1;
11679 }
d3fca0b5
MR
11680 if (mips_opts.noreorder)
11681 macro_build (NULL, "nop", "");
6478892d 11682 expr1.X_add_number = mips_cprestore_offset;
134c0c8b 11683 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 11684 mips_gp_register,
256ab948
TS
11685 mips_frame_reg,
11686 HAVE_64BIT_ADDRESSES);
6478892d 11687 }
252b5132
RH
11688 }
11689 }
252b5132 11690
8fc2e39e 11691 break;
252b5132 11692
df58fc94
RS
11693 case M_JALS_A:
11694 gas_assert (mips_opts.micromips);
833794fc
MR
11695 if (mips_opts.insn32)
11696 {
1661c76c 11697 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
833794fc
MR
11698 break;
11699 }
df58fc94
RS
11700 jals = 1;
11701 /* Fall through. */
252b5132
RH
11702 case M_JAL_A:
11703 if (mips_pic == NO_PIC)
df58fc94 11704 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
252b5132
RH
11705 else if (mips_pic == SVR4_PIC)
11706 {
11707 /* If this is a reference to an external symbol, and we are
11708 using a small GOT, we want
11709 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11710 nop
f9419b05 11711 jalr $ra,$25
252b5132
RH
11712 nop
11713 lw $gp,cprestore($sp)
11714 The cprestore value is set using the .cprestore
11715 pseudo-op. If we are using a big GOT, we want
11716 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11717 addu $25,$25,$gp
11718 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
11719 nop
f9419b05 11720 jalr $ra,$25
252b5132
RH
11721 nop
11722 lw $gp,cprestore($sp)
11723 If the symbol is not external, we want
11724 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11725 nop
11726 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 11727 jalr $ra,$25
252b5132 11728 nop
438c16b8 11729 lw $gp,cprestore($sp)
f5040a92
AO
11730
11731 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
11732 sequences above, minus nops, unless the symbol is local,
11733 which enables us to use GOT_PAGE/GOT_OFST (big got) or
11734 GOT_DISP. */
438c16b8 11735 if (HAVE_NEWABI)
252b5132 11736 {
90ecf173 11737 if (!mips_big_got)
f5040a92 11738 {
4d7206a2 11739 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11740 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11741 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 11742 mips_gp_register);
4d7206a2 11743 relax_switch ();
67c0d1eb
RS
11744 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11745 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
11746 mips_gp_register);
11747 relax_end ();
f5040a92
AO
11748 }
11749 else
11750 {
4d7206a2 11751 relax_start (offset_expr.X_add_symbol);
df58fc94 11752 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
11753 BFD_RELOC_MIPS_CALL_HI16);
11754 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11755 PIC_CALL_REG, mips_gp_register);
11756 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11757 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11758 PIC_CALL_REG);
4d7206a2 11759 relax_switch ();
67c0d1eb
RS
11760 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11761 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
11762 mips_gp_register);
11763 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11764 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 11765 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 11766 relax_end ();
f5040a92 11767 }
684022ea 11768
df58fc94 11769 macro_build_jalr (&offset_expr, 0);
252b5132
RH
11770 }
11771 else
11772 {
4d7206a2 11773 relax_start (offset_expr.X_add_symbol);
90ecf173 11774 if (!mips_big_got)
438c16b8 11775 {
67c0d1eb
RS
11776 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11777 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 11778 mips_gp_register);
269137b2 11779 load_delay_nop ();
4d7206a2 11780 relax_switch ();
438c16b8 11781 }
252b5132 11782 else
252b5132 11783 {
67c0d1eb
RS
11784 int gpdelay;
11785
11786 gpdelay = reg_needs_delay (mips_gp_register);
df58fc94 11787 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
11788 BFD_RELOC_MIPS_CALL_HI16);
11789 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11790 PIC_CALL_REG, mips_gp_register);
11791 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11792 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11793 PIC_CALL_REG);
269137b2 11794 load_delay_nop ();
4d7206a2 11795 relax_switch ();
67c0d1eb
RS
11796 if (gpdelay)
11797 macro_build (NULL, "nop", "");
252b5132 11798 }
67c0d1eb
RS
11799 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11800 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 11801 mips_gp_register);
269137b2 11802 load_delay_nop ();
67c0d1eb
RS
11803 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11804 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 11805 relax_end ();
df58fc94 11806 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
438c16b8 11807
6478892d 11808 if (mips_cprestore_offset < 0)
1661c76c 11809 as_warn (_("no .cprestore pseudo-op used in PIC code"));
6478892d
TS
11810 else
11811 {
90ecf173 11812 if (!mips_frame_reg_valid)
7a621144 11813 {
1661c76c 11814 as_warn (_("no .frame pseudo-op used in PIC code"));
7a621144
DJ
11815 /* Quiet this warning. */
11816 mips_frame_reg_valid = 1;
11817 }
90ecf173 11818 if (!mips_cprestore_valid)
7a621144 11819 {
1661c76c 11820 as_warn (_("no .cprestore pseudo-op used in PIC code"));
7a621144
DJ
11821 /* Quiet this warning. */
11822 mips_cprestore_valid = 1;
11823 }
6478892d 11824 if (mips_opts.noreorder)
67c0d1eb 11825 macro_build (NULL, "nop", "");
6478892d 11826 expr1.X_add_number = mips_cprestore_offset;
134c0c8b 11827 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 11828 mips_gp_register,
256ab948
TS
11829 mips_frame_reg,
11830 HAVE_64BIT_ADDRESSES);
6478892d 11831 }
252b5132
RH
11832 }
11833 }
0a44bf69 11834 else if (mips_pic == VXWORKS_PIC)
1661c76c 11835 as_bad (_("non-PIC jump used in PIC library"));
252b5132
RH
11836 else
11837 abort ();
11838
8fc2e39e 11839 break;
252b5132 11840
7f3c4072 11841 case M_LBUE_AB:
7f3c4072
CM
11842 s = "lbue";
11843 fmt = "t,+j(b)";
11844 offbits = 9;
11845 goto ld_st;
11846 case M_LHUE_AB:
7f3c4072
CM
11847 s = "lhue";
11848 fmt = "t,+j(b)";
11849 offbits = 9;
11850 goto ld_st;
11851 case M_LBE_AB:
7f3c4072
CM
11852 s = "lbe";
11853 fmt = "t,+j(b)";
11854 offbits = 9;
11855 goto ld_st;
11856 case M_LHE_AB:
7f3c4072
CM
11857 s = "lhe";
11858 fmt = "t,+j(b)";
11859 offbits = 9;
11860 goto ld_st;
11861 case M_LLE_AB:
7f3c4072
CM
11862 s = "lle";
11863 fmt = "t,+j(b)";
11864 offbits = 9;
11865 goto ld_st;
11866 case M_LWE_AB:
7f3c4072
CM
11867 s = "lwe";
11868 fmt = "t,+j(b)";
11869 offbits = 9;
11870 goto ld_st;
11871 case M_LWLE_AB:
7f3c4072
CM
11872 s = "lwle";
11873 fmt = "t,+j(b)";
11874 offbits = 9;
11875 goto ld_st;
11876 case M_LWRE_AB:
7f3c4072
CM
11877 s = "lwre";
11878 fmt = "t,+j(b)";
11879 offbits = 9;
11880 goto ld_st;
11881 case M_SBE_AB:
7f3c4072
CM
11882 s = "sbe";
11883 fmt = "t,+j(b)";
11884 offbits = 9;
11885 goto ld_st;
11886 case M_SCE_AB:
7f3c4072
CM
11887 s = "sce";
11888 fmt = "t,+j(b)";
11889 offbits = 9;
11890 goto ld_st;
11891 case M_SHE_AB:
7f3c4072
CM
11892 s = "she";
11893 fmt = "t,+j(b)";
11894 offbits = 9;
11895 goto ld_st;
11896 case M_SWE_AB:
7f3c4072
CM
11897 s = "swe";
11898 fmt = "t,+j(b)";
11899 offbits = 9;
11900 goto ld_st;
11901 case M_SWLE_AB:
7f3c4072
CM
11902 s = "swle";
11903 fmt = "t,+j(b)";
11904 offbits = 9;
11905 goto ld_st;
11906 case M_SWRE_AB:
7f3c4072
CM
11907 s = "swre";
11908 fmt = "t,+j(b)";
11909 offbits = 9;
11910 goto ld_st;
dec0624d 11911 case M_ACLR_AB:
dec0624d 11912 s = "aclr";
dec0624d 11913 fmt = "\\,~(b)";
7f3c4072 11914 offbits = 12;
dec0624d
MR
11915 goto ld_st;
11916 case M_ASET_AB:
dec0624d 11917 s = "aset";
dec0624d 11918 fmt = "\\,~(b)";
7f3c4072 11919 offbits = 12;
dec0624d 11920 goto ld_st;
252b5132
RH
11921 case M_LB_AB:
11922 s = "lb";
df58fc94 11923 fmt = "t,o(b)";
252b5132
RH
11924 goto ld;
11925 case M_LBU_AB:
11926 s = "lbu";
df58fc94 11927 fmt = "t,o(b)";
252b5132
RH
11928 goto ld;
11929 case M_LH_AB:
11930 s = "lh";
df58fc94 11931 fmt = "t,o(b)";
252b5132
RH
11932 goto ld;
11933 case M_LHU_AB:
11934 s = "lhu";
df58fc94 11935 fmt = "t,o(b)";
252b5132
RH
11936 goto ld;
11937 case M_LW_AB:
11938 s = "lw";
df58fc94 11939 fmt = "t,o(b)";
252b5132
RH
11940 goto ld;
11941 case M_LWC0_AB:
df58fc94 11942 gas_assert (!mips_opts.micromips);
252b5132 11943 s = "lwc0";
df58fc94 11944 fmt = "E,o(b)";
bdaaa2e1 11945 /* Itbl support may require additional care here. */
252b5132 11946 coproc = 1;
df58fc94 11947 goto ld_st;
252b5132
RH
11948 case M_LWC1_AB:
11949 s = "lwc1";
df58fc94 11950 fmt = "T,o(b)";
bdaaa2e1 11951 /* Itbl support may require additional care here. */
252b5132 11952 coproc = 1;
df58fc94 11953 goto ld_st;
252b5132
RH
11954 case M_LWC2_AB:
11955 s = "lwc2";
df58fc94 11956 fmt = COP12_FMT;
7361da2c
AB
11957 offbits = (mips_opts.micromips ? 12
11958 : ISA_IS_R6 (mips_opts.isa) ? 11
11959 : 16);
bdaaa2e1 11960 /* Itbl support may require additional care here. */
252b5132 11961 coproc = 1;
df58fc94 11962 goto ld_st;
252b5132 11963 case M_LWC3_AB:
df58fc94 11964 gas_assert (!mips_opts.micromips);
252b5132 11965 s = "lwc3";
df58fc94 11966 fmt = "E,o(b)";
bdaaa2e1 11967 /* Itbl support may require additional care here. */
252b5132 11968 coproc = 1;
df58fc94 11969 goto ld_st;
252b5132
RH
11970 case M_LWL_AB:
11971 s = "lwl";
df58fc94 11972 fmt = MEM12_FMT;
7f3c4072 11973 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11974 goto ld_st;
252b5132
RH
11975 case M_LWR_AB:
11976 s = "lwr";
df58fc94 11977 fmt = MEM12_FMT;
7f3c4072 11978 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11979 goto ld_st;
252b5132 11980 case M_LDC1_AB:
252b5132 11981 s = "ldc1";
df58fc94 11982 fmt = "T,o(b)";
bdaaa2e1 11983 /* Itbl support may require additional care here. */
252b5132 11984 coproc = 1;
df58fc94 11985 goto ld_st;
252b5132
RH
11986 case M_LDC2_AB:
11987 s = "ldc2";
df58fc94 11988 fmt = COP12_FMT;
7361da2c
AB
11989 offbits = (mips_opts.micromips ? 12
11990 : ISA_IS_R6 (mips_opts.isa) ? 11
11991 : 16);
bdaaa2e1 11992 /* Itbl support may require additional care here. */
252b5132 11993 coproc = 1;
df58fc94 11994 goto ld_st;
c77c0862 11995 case M_LQC2_AB:
c77c0862 11996 s = "lqc2";
14daeee3 11997 fmt = "+7,o(b)";
c77c0862
RS
11998 /* Itbl support may require additional care here. */
11999 coproc = 1;
12000 goto ld_st;
252b5132
RH
12001 case M_LDC3_AB:
12002 s = "ldc3";
df58fc94 12003 fmt = "E,o(b)";
bdaaa2e1 12004 /* Itbl support may require additional care here. */
252b5132 12005 coproc = 1;
df58fc94 12006 goto ld_st;
252b5132
RH
12007 case M_LDL_AB:
12008 s = "ldl";
df58fc94 12009 fmt = MEM12_FMT;
7f3c4072 12010 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 12011 goto ld_st;
252b5132
RH
12012 case M_LDR_AB:
12013 s = "ldr";
df58fc94 12014 fmt = MEM12_FMT;
7f3c4072 12015 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 12016 goto ld_st;
252b5132
RH
12017 case M_LL_AB:
12018 s = "ll";
7361da2c
AB
12019 fmt = LL_SC_FMT;
12020 offbits = (mips_opts.micromips ? 12
12021 : ISA_IS_R6 (mips_opts.isa) ? 9
12022 : 16);
252b5132
RH
12023 goto ld;
12024 case M_LLD_AB:
12025 s = "lld";
7361da2c
AB
12026 fmt = LL_SC_FMT;
12027 offbits = (mips_opts.micromips ? 12
12028 : ISA_IS_R6 (mips_opts.isa) ? 9
12029 : 16);
252b5132
RH
12030 goto ld;
12031 case M_LWU_AB:
12032 s = "lwu";
df58fc94 12033 fmt = MEM12_FMT;
7f3c4072 12034 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
12035 goto ld;
12036 case M_LWP_AB:
df58fc94
RS
12037 gas_assert (mips_opts.micromips);
12038 s = "lwp";
12039 fmt = "t,~(b)";
7f3c4072 12040 offbits = 12;
df58fc94
RS
12041 lp = 1;
12042 goto ld;
12043 case M_LDP_AB:
df58fc94
RS
12044 gas_assert (mips_opts.micromips);
12045 s = "ldp";
12046 fmt = "t,~(b)";
7f3c4072 12047 offbits = 12;
df58fc94
RS
12048 lp = 1;
12049 goto ld;
a45328b9
AB
12050 case M_LLDP_AB:
12051 case M_LLWP_AB:
41cee089 12052 case M_LLWPE_AB:
a45328b9
AB
12053 s = ip->insn_mo->name;
12054 fmt = "t,d,s";
12055 ll_sc_paired = 1;
12056 offbits = 0;
12057 goto ld;
df58fc94 12058 case M_LWM_AB:
df58fc94
RS
12059 gas_assert (mips_opts.micromips);
12060 s = "lwm";
12061 fmt = "n,~(b)";
7f3c4072 12062 offbits = 12;
df58fc94
RS
12063 goto ld_st;
12064 case M_LDM_AB:
df58fc94
RS
12065 gas_assert (mips_opts.micromips);
12066 s = "ldm";
12067 fmt = "n,~(b)";
7f3c4072 12068 offbits = 12;
df58fc94
RS
12069 goto ld_st;
12070
252b5132 12071 ld:
a45328b9
AB
12072 /* Try to use one the the load registers to compute the base address.
12073 We don't want to use $0 as tempreg. */
12074 if (ll_sc_paired)
12075 {
12076 if ((op[0] == ZERO && op[3] == op[1])
12077 || (op[1] == ZERO && op[3] == op[0])
12078 || (op[0] == ZERO && op[1] == ZERO))
12079 goto ld_st;
12080 else if (op[0] != op[3] && op[0] != ZERO)
12081 tempreg = op[0];
12082 else
12083 tempreg = op[1];
12084 }
252b5132 12085 else
a45328b9
AB
12086 {
12087 if (op[2] == op[0] + lp || op[0] + lp == ZERO)
12088 goto ld_st;
12089 else
12090 tempreg = op[0] + lp;
12091 }
df58fc94
RS
12092 goto ld_noat;
12093
252b5132
RH
12094 case M_SB_AB:
12095 s = "sb";
df58fc94
RS
12096 fmt = "t,o(b)";
12097 goto ld_st;
252b5132
RH
12098 case M_SH_AB:
12099 s = "sh";
df58fc94
RS
12100 fmt = "t,o(b)";
12101 goto ld_st;
252b5132
RH
12102 case M_SW_AB:
12103 s = "sw";
df58fc94
RS
12104 fmt = "t,o(b)";
12105 goto ld_st;
252b5132 12106 case M_SWC0_AB:
df58fc94 12107 gas_assert (!mips_opts.micromips);
252b5132 12108 s = "swc0";
df58fc94 12109 fmt = "E,o(b)";
bdaaa2e1 12110 /* Itbl support may require additional care here. */
252b5132 12111 coproc = 1;
df58fc94 12112 goto ld_st;
252b5132
RH
12113 case M_SWC1_AB:
12114 s = "swc1";
df58fc94 12115 fmt = "T,o(b)";
bdaaa2e1 12116 /* Itbl support may require additional care here. */
252b5132 12117 coproc = 1;
df58fc94 12118 goto ld_st;
252b5132
RH
12119 case M_SWC2_AB:
12120 s = "swc2";
df58fc94 12121 fmt = COP12_FMT;
7361da2c
AB
12122 offbits = (mips_opts.micromips ? 12
12123 : ISA_IS_R6 (mips_opts.isa) ? 11
12124 : 16);
bdaaa2e1 12125 /* Itbl support may require additional care here. */
252b5132 12126 coproc = 1;
df58fc94 12127 goto ld_st;
252b5132 12128 case M_SWC3_AB:
df58fc94 12129 gas_assert (!mips_opts.micromips);
252b5132 12130 s = "swc3";
df58fc94 12131 fmt = "E,o(b)";
bdaaa2e1 12132 /* Itbl support may require additional care here. */
252b5132 12133 coproc = 1;
df58fc94 12134 goto ld_st;
252b5132
RH
12135 case M_SWL_AB:
12136 s = "swl";
df58fc94 12137 fmt = MEM12_FMT;
7f3c4072 12138 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 12139 goto ld_st;
252b5132
RH
12140 case M_SWR_AB:
12141 s = "swr";
df58fc94 12142 fmt = MEM12_FMT;
7f3c4072 12143 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 12144 goto ld_st;
252b5132
RH
12145 case M_SC_AB:
12146 s = "sc";
7361da2c
AB
12147 fmt = LL_SC_FMT;
12148 offbits = (mips_opts.micromips ? 12
12149 : ISA_IS_R6 (mips_opts.isa) ? 9
12150 : 16);
df58fc94 12151 goto ld_st;
252b5132
RH
12152 case M_SCD_AB:
12153 s = "scd";
7361da2c
AB
12154 fmt = LL_SC_FMT;
12155 offbits = (mips_opts.micromips ? 12
12156 : ISA_IS_R6 (mips_opts.isa) ? 9
12157 : 16);
df58fc94 12158 goto ld_st;
a45328b9
AB
12159 case M_SCDP_AB:
12160 case M_SCWP_AB:
41cee089 12161 case M_SCWPE_AB:
a45328b9
AB
12162 s = ip->insn_mo->name;
12163 fmt = "t,d,s";
12164 ll_sc_paired = 1;
12165 offbits = 0;
12166 goto ld_st;
d43b4baf
TS
12167 case M_CACHE_AB:
12168 s = "cache";
7361da2c
AB
12169 fmt = (mips_opts.micromips ? "k,~(b)"
12170 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
12171 : "k,o(b)");
12172 offbits = (mips_opts.micromips ? 12
12173 : ISA_IS_R6 (mips_opts.isa) ? 9
12174 : 16);
7f3c4072
CM
12175 goto ld_st;
12176 case M_CACHEE_AB:
7f3c4072
CM
12177 s = "cachee";
12178 fmt = "k,+j(b)";
12179 offbits = 9;
df58fc94 12180 goto ld_st;
3eebd5eb
MR
12181 case M_PREF_AB:
12182 s = "pref";
7361da2c
AB
12183 fmt = (mips_opts.micromips ? "k,~(b)"
12184 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
12185 : "k,o(b)");
12186 offbits = (mips_opts.micromips ? 12
12187 : ISA_IS_R6 (mips_opts.isa) ? 9
12188 : 16);
7f3c4072
CM
12189 goto ld_st;
12190 case M_PREFE_AB:
7f3c4072
CM
12191 s = "prefe";
12192 fmt = "k,+j(b)";
12193 offbits = 9;
df58fc94 12194 goto ld_st;
252b5132 12195 case M_SDC1_AB:
252b5132 12196 s = "sdc1";
df58fc94 12197 fmt = "T,o(b)";
252b5132 12198 coproc = 1;
bdaaa2e1 12199 /* Itbl support may require additional care here. */
df58fc94 12200 goto ld_st;
252b5132
RH
12201 case M_SDC2_AB:
12202 s = "sdc2";
df58fc94 12203 fmt = COP12_FMT;
7361da2c
AB
12204 offbits = (mips_opts.micromips ? 12
12205 : ISA_IS_R6 (mips_opts.isa) ? 11
12206 : 16);
c77c0862
RS
12207 /* Itbl support may require additional care here. */
12208 coproc = 1;
12209 goto ld_st;
12210 case M_SQC2_AB:
c77c0862 12211 s = "sqc2";
14daeee3 12212 fmt = "+7,o(b)";
bdaaa2e1 12213 /* Itbl support may require additional care here. */
252b5132 12214 coproc = 1;
df58fc94 12215 goto ld_st;
252b5132 12216 case M_SDC3_AB:
df58fc94 12217 gas_assert (!mips_opts.micromips);
252b5132 12218 s = "sdc3";
df58fc94 12219 fmt = "E,o(b)";
bdaaa2e1 12220 /* Itbl support may require additional care here. */
252b5132 12221 coproc = 1;
df58fc94 12222 goto ld_st;
252b5132
RH
12223 case M_SDL_AB:
12224 s = "sdl";
df58fc94 12225 fmt = MEM12_FMT;
7f3c4072 12226 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 12227 goto ld_st;
252b5132
RH
12228 case M_SDR_AB:
12229 s = "sdr";
df58fc94 12230 fmt = MEM12_FMT;
7f3c4072 12231 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
12232 goto ld_st;
12233 case M_SWP_AB:
df58fc94
RS
12234 gas_assert (mips_opts.micromips);
12235 s = "swp";
12236 fmt = "t,~(b)";
7f3c4072 12237 offbits = 12;
df58fc94
RS
12238 goto ld_st;
12239 case M_SDP_AB:
df58fc94
RS
12240 gas_assert (mips_opts.micromips);
12241 s = "sdp";
12242 fmt = "t,~(b)";
7f3c4072 12243 offbits = 12;
df58fc94
RS
12244 goto ld_st;
12245 case M_SWM_AB:
df58fc94
RS
12246 gas_assert (mips_opts.micromips);
12247 s = "swm";
12248 fmt = "n,~(b)";
7f3c4072 12249 offbits = 12;
df58fc94
RS
12250 goto ld_st;
12251 case M_SDM_AB:
df58fc94
RS
12252 gas_assert (mips_opts.micromips);
12253 s = "sdm";
12254 fmt = "n,~(b)";
7f3c4072 12255 offbits = 12;
df58fc94
RS
12256
12257 ld_st:
8fc2e39e 12258 tempreg = AT;
df58fc94 12259 ld_noat:
a45328b9 12260 breg = ll_sc_paired ? op[3] : op[2];
f2ae14a1
RS
12261 if (small_offset_p (0, align, 16))
12262 {
12263 /* The first case exists for M_LD_AB and M_SD_AB, which are
12264 macros for o32 but which should act like normal instructions
12265 otherwise. */
12266 if (offbits == 16)
c0ebe874 12267 macro_build (&offset_expr, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
12268 offset_reloc[1], offset_reloc[2], breg);
12269 else if (small_offset_p (0, align, offbits))
12270 {
12271 if (offbits == 0)
a45328b9
AB
12272 {
12273 if (ll_sc_paired)
12274 macro_build (NULL, s, fmt, op[0], op[1], breg);
12275 else
12276 macro_build (NULL, s, fmt, op[0], breg);
12277 }
f2ae14a1 12278 else
c0ebe874 12279 macro_build (NULL, s, fmt, op[0],
c8276761 12280 (int) offset_expr.X_add_number, breg);
f2ae14a1
RS
12281 }
12282 else
12283 {
12284 if (tempreg == AT)
12285 used_at = 1;
12286 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
12287 tempreg, breg, -1, offset_reloc[0],
12288 offset_reloc[1], offset_reloc[2]);
12289 if (offbits == 0)
a45328b9
AB
12290 {
12291 if (ll_sc_paired)
12292 macro_build (NULL, s, fmt, op[0], op[1], tempreg);
12293 else
12294 macro_build (NULL, s, fmt, op[0], tempreg);
12295 }
f2ae14a1 12296 else
c0ebe874 12297 macro_build (NULL, s, fmt, op[0], 0, tempreg);
f2ae14a1
RS
12298 }
12299 break;
12300 }
12301
12302 if (tempreg == AT)
12303 used_at = 1;
12304
252b5132
RH
12305 if (offset_expr.X_op != O_constant
12306 && offset_expr.X_op != O_symbol)
12307 {
1661c76c 12308 as_bad (_("expression too complex"));
252b5132
RH
12309 offset_expr.X_op = O_constant;
12310 }
12311
2051e8c4
MR
12312 if (HAVE_32BIT_ADDRESSES
12313 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
12314 {
12315 char value [32];
12316
12317 sprintf_vma (value, offset_expr.X_add_number);
1661c76c 12318 as_bad (_("number (0x%s) larger than 32 bits"), value);
55e08f71 12319 }
2051e8c4 12320
252b5132
RH
12321 /* A constant expression in PIC code can be handled just as it
12322 is in non PIC code. */
aed1a261
RS
12323 if (offset_expr.X_op == O_constant)
12324 {
f2ae14a1
RS
12325 expr1.X_add_number = offset_high_part (offset_expr.X_add_number,
12326 offbits == 0 ? 16 : offbits);
12327 offset_expr.X_add_number -= expr1.X_add_number;
df58fc94 12328
f2ae14a1
RS
12329 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
12330 if (breg != 0)
12331 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12332 tempreg, tempreg, breg);
7f3c4072 12333 if (offbits == 0)
dd6a37e7 12334 {
f2ae14a1 12335 if (offset_expr.X_add_number != 0)
dd6a37e7 12336 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
f2ae14a1 12337 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
a45328b9
AB
12338 if (ll_sc_paired)
12339 macro_build (NULL, s, fmt, op[0], op[1], tempreg);
12340 else
12341 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 12342 }
7f3c4072 12343 else if (offbits == 16)
c0ebe874 12344 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
df58fc94 12345 else
c0ebe874 12346 macro_build (NULL, s, fmt, op[0],
c8276761 12347 (int) offset_expr.X_add_number, tempreg);
df58fc94 12348 }
7f3c4072 12349 else if (offbits != 16)
df58fc94 12350 {
7f3c4072 12351 /* The offset field is too narrow to be used for a low-part
2b0f3761 12352 relocation, so load the whole address into the auxiliary
f2ae14a1
RS
12353 register. */
12354 load_address (tempreg, &offset_expr, &used_at);
12355 if (breg != 0)
12356 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12357 tempreg, tempreg, breg);
7f3c4072 12358 if (offbits == 0)
a45328b9
AB
12359 {
12360 if (ll_sc_paired)
12361 macro_build (NULL, s, fmt, op[0], op[1], tempreg);
12362 else
12363 macro_build (NULL, s, fmt, op[0], tempreg);
12364 }
dd6a37e7 12365 else
c0ebe874 12366 macro_build (NULL, s, fmt, op[0], 0, tempreg);
aed1a261
RS
12367 }
12368 else if (mips_pic == NO_PIC)
252b5132
RH
12369 {
12370 /* If this is a reference to a GP relative symbol, and there
12371 is no base register, we want
c0ebe874 12372 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
12373 Otherwise, if there is no base register, we want
12374 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
c0ebe874 12375 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
252b5132
RH
12376 If we have a constant, we need two instructions anyhow,
12377 so we always use the latter form.
12378
12379 If we have a base register, and this is a reference to a
12380 GP relative symbol, we want
12381 addu $tempreg,$breg,$gp
c0ebe874 12382 <op> op[0],<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
12383 Otherwise we want
12384 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
12385 addu $tempreg,$tempreg,$breg
c0ebe874 12386 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 12387 With a constant we always use the latter case.
76b3015f 12388
d6bc6245
TS
12389 With 64bit address space and no base register and $at usable,
12390 we want
12391 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12392 lui $at,<sym> (BFD_RELOC_HI16_S)
12393 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12394 dsll32 $tempreg,0
12395 daddu $tempreg,$at
c0ebe874 12396 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
12397 If we have a base register, we want
12398 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12399 lui $at,<sym> (BFD_RELOC_HI16_S)
12400 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12401 daddu $at,$breg
12402 dsll32 $tempreg,0
12403 daddu $tempreg,$at
c0ebe874 12404 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
12405
12406 Without $at we can't generate the optimal path for superscalar
12407 processors here since this would require two temporary registers.
12408 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12409 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12410 dsll $tempreg,16
12411 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
12412 dsll $tempreg,16
c0ebe874 12413 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
12414 If we have a base register, we want
12415 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12416 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12417 dsll $tempreg,16
12418 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
12419 dsll $tempreg,16
12420 daddu $tempreg,$tempreg,$breg
c0ebe874 12421 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 12422
6caf9ef4 12423 For GP relative symbols in 64bit address space we can use
aed1a261
RS
12424 the same sequence as in 32bit address space. */
12425 if (HAVE_64BIT_SYMBOLS)
d6bc6245 12426 {
aed1a261 12427 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
12428 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12429 {
12430 relax_start (offset_expr.X_add_symbol);
12431 if (breg == 0)
12432 {
c0ebe874 12433 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
12434 BFD_RELOC_GPREL16, mips_gp_register);
12435 }
12436 else
12437 {
12438 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12439 tempreg, breg, mips_gp_register);
c0ebe874 12440 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
12441 BFD_RELOC_GPREL16, tempreg);
12442 }
12443 relax_switch ();
12444 }
d6bc6245 12445
741fe287 12446 if (used_at == 0 && mips_opts.at)
d6bc6245 12447 {
df58fc94 12448 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb 12449 BFD_RELOC_MIPS_HIGHEST);
df58fc94 12450 macro_build (&offset_expr, "lui", LUI_FMT, AT,
67c0d1eb
RS
12451 BFD_RELOC_HI16_S);
12452 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12453 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 12454 if (breg != 0)
67c0d1eb 12455 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
df58fc94 12456 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 12457 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
c0ebe874 12458 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16,
67c0d1eb 12459 tempreg);
d6bc6245
TS
12460 used_at = 1;
12461 }
12462 else
12463 {
df58fc94 12464 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb
RS
12465 BFD_RELOC_MIPS_HIGHEST);
12466 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12467 tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 12468 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb
RS
12469 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12470 tempreg, BFD_RELOC_HI16_S);
df58fc94 12471 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
d6bc6245 12472 if (breg != 0)
67c0d1eb 12473 macro_build (NULL, "daddu", "d,v,t",
17a2f251 12474 tempreg, tempreg, breg);
c0ebe874 12475 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12476 BFD_RELOC_LO16, tempreg);
d6bc6245 12477 }
6caf9ef4
TS
12478
12479 if (mips_relax.sequence)
12480 relax_end ();
8fc2e39e 12481 break;
d6bc6245 12482 }
256ab948 12483
252b5132
RH
12484 if (breg == 0)
12485 {
67c0d1eb 12486 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12487 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12488 {
4d7206a2 12489 relax_start (offset_expr.X_add_symbol);
c0ebe874 12490 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_GPREL16,
67c0d1eb 12491 mips_gp_register);
4d7206a2 12492 relax_switch ();
252b5132 12493 }
67c0d1eb 12494 macro_build_lui (&offset_expr, tempreg);
c0ebe874 12495 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12496 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
12497 if (mips_relax.sequence)
12498 relax_end ();
252b5132
RH
12499 }
12500 else
12501 {
67c0d1eb 12502 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12503 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12504 {
4d7206a2 12505 relax_start (offset_expr.X_add_symbol);
67c0d1eb 12506 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12507 tempreg, breg, mips_gp_register);
c0ebe874 12508 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12509 BFD_RELOC_GPREL16, tempreg);
4d7206a2 12510 relax_switch ();
252b5132 12511 }
67c0d1eb
RS
12512 macro_build_lui (&offset_expr, tempreg);
12513 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12514 tempreg, tempreg, breg);
c0ebe874 12515 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12516 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
12517 if (mips_relax.sequence)
12518 relax_end ();
252b5132
RH
12519 }
12520 }
0a44bf69 12521 else if (!mips_big_got)
252b5132 12522 {
ed6fb7bd 12523 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 12524
252b5132
RH
12525 /* If this is a reference to an external symbol, we want
12526 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12527 nop
c0ebe874 12528 <op> op[0],0($tempreg)
252b5132
RH
12529 Otherwise we want
12530 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12531 nop
12532 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 12533 <op> op[0],0($tempreg)
f5040a92
AO
12534
12535 For NewABI, we want
12536 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 12537 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 12538
252b5132
RH
12539 If there is a base register, we add it to $tempreg before
12540 the <op>. If there is a constant, we stick it in the
12541 <op> instruction. We don't handle constants larger than
12542 16 bits, because we have no way to load the upper 16 bits
12543 (actually, we could handle them for the subset of cases
12544 in which we are not using $at). */
9c2799c2 12545 gas_assert (offset_expr.X_op == O_symbol);
f5040a92
AO
12546 if (HAVE_NEWABI)
12547 {
67c0d1eb
RS
12548 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12549 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 12550 if (breg != 0)
67c0d1eb 12551 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12552 tempreg, tempreg, breg);
c0ebe874 12553 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12554 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
12555 break;
12556 }
252b5132
RH
12557 expr1.X_add_number = offset_expr.X_add_number;
12558 offset_expr.X_add_number = 0;
12559 if (expr1.X_add_number < -0x8000
12560 || expr1.X_add_number >= 0x8000)
12561 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
12562 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12563 lw_reloc_type, mips_gp_register);
269137b2 12564 load_delay_nop ();
4d7206a2
RS
12565 relax_start (offset_expr.X_add_symbol);
12566 relax_switch ();
67c0d1eb
RS
12567 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12568 tempreg, BFD_RELOC_LO16);
4d7206a2 12569 relax_end ();
252b5132 12570 if (breg != 0)
67c0d1eb 12571 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12572 tempreg, tempreg, breg);
c0ebe874 12573 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 12574 }
0a44bf69 12575 else if (mips_big_got && !HAVE_NEWABI)
252b5132 12576 {
67c0d1eb 12577 int gpdelay;
252b5132
RH
12578
12579 /* If this is a reference to an external symbol, we want
12580 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12581 addu $tempreg,$tempreg,$gp
12582 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 12583 <op> op[0],0($tempreg)
252b5132
RH
12584 Otherwise we want
12585 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12586 nop
12587 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 12588 <op> op[0],0($tempreg)
252b5132
RH
12589 If there is a base register, we add it to $tempreg before
12590 the <op>. If there is a constant, we stick it in the
12591 <op> instruction. We don't handle constants larger than
12592 16 bits, because we have no way to load the upper 16 bits
12593 (actually, we could handle them for the subset of cases
f5040a92 12594 in which we are not using $at). */
9c2799c2 12595 gas_assert (offset_expr.X_op == O_symbol);
252b5132
RH
12596 expr1.X_add_number = offset_expr.X_add_number;
12597 offset_expr.X_add_number = 0;
12598 if (expr1.X_add_number < -0x8000
12599 || expr1.X_add_number >= 0x8000)
12600 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 12601 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 12602 relax_start (offset_expr.X_add_symbol);
df58fc94 12603 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 12604 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
12605 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12606 mips_gp_register);
12607 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12608 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 12609 relax_switch ();
67c0d1eb
RS
12610 if (gpdelay)
12611 macro_build (NULL, "nop", "");
12612 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12613 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 12614 load_delay_nop ();
67c0d1eb
RS
12615 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12616 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
12617 relax_end ();
12618
252b5132 12619 if (breg != 0)
67c0d1eb 12620 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12621 tempreg, tempreg, breg);
c0ebe874 12622 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 12623 }
0a44bf69 12624 else if (mips_big_got && HAVE_NEWABI)
f5040a92 12625 {
f5040a92
AO
12626 /* If this is a reference to an external symbol, we want
12627 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12628 add $tempreg,$tempreg,$gp
12629 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 12630 <op> op[0],<ofst>($tempreg)
f5040a92
AO
12631 Otherwise, for local symbols, we want:
12632 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 12633 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
9c2799c2 12634 gas_assert (offset_expr.X_op == O_symbol);
4d7206a2 12635 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
12636 offset_expr.X_add_number = 0;
12637 if (expr1.X_add_number < -0x8000
12638 || expr1.X_add_number >= 0x8000)
12639 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 12640 relax_start (offset_expr.X_add_symbol);
df58fc94 12641 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 12642 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
12643 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12644 mips_gp_register);
12645 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12646 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 12647 if (breg != 0)
67c0d1eb 12648 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12649 tempreg, tempreg, breg);
c0ebe874 12650 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
684022ea 12651
4d7206a2 12652 relax_switch ();
f5040a92 12653 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
12654 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12655 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 12656 if (breg != 0)
67c0d1eb 12657 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12658 tempreg, tempreg, breg);
c0ebe874 12659 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12660 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 12661 relax_end ();
f5040a92 12662 }
252b5132
RH
12663 else
12664 abort ();
12665
252b5132
RH
12666 break;
12667
833794fc
MR
12668 case M_JRADDIUSP:
12669 gas_assert (mips_opts.micromips);
12670 gas_assert (mips_opts.insn32);
12671 start_noreorder ();
12672 macro_build (NULL, "jr", "s", RA);
c0ebe874 12673 expr1.X_add_number = op[0] << 2;
833794fc
MR
12674 macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
12675 end_noreorder ();
12676 break;
12677
12678 case M_JRC:
12679 gas_assert (mips_opts.micromips);
12680 gas_assert (mips_opts.insn32);
c0ebe874 12681 macro_build (NULL, "jr", "s", op[0]);
833794fc
MR
12682 if (mips_opts.noreorder)
12683 macro_build (NULL, "nop", "");
12684 break;
12685
252b5132
RH
12686 case M_LI:
12687 case M_LI_S:
c0ebe874 12688 load_register (op[0], &imm_expr, 0);
8fc2e39e 12689 break;
252b5132
RH
12690
12691 case M_DLI:
c0ebe874 12692 load_register (op[0], &imm_expr, 1);
8fc2e39e 12693 break;
252b5132
RH
12694
12695 case M_LI_SS:
12696 if (imm_expr.X_op == O_constant)
12697 {
8fc2e39e 12698 used_at = 1;
67c0d1eb 12699 load_register (AT, &imm_expr, 0);
c0ebe874 12700 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
12701 break;
12702 }
12703 else
12704 {
b0e6f033
RS
12705 gas_assert (imm_expr.X_op == O_absent
12706 && offset_expr.X_op == O_symbol
90ecf173
MR
12707 && strcmp (segment_name (S_GET_SEGMENT
12708 (offset_expr.X_add_symbol)),
12709 ".lit4") == 0
12710 && offset_expr.X_add_number == 0);
c0ebe874 12711 macro_build (&offset_expr, "lwc1", "T,o(b)", op[0],
17a2f251 12712 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 12713 break;
252b5132
RH
12714 }
12715
12716 case M_LI_D:
ca4e0257
RS
12717 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
12718 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
12719 order 32 bits of the value and the low order 32 bits are either
12720 zero or in OFFSET_EXPR. */
b0e6f033 12721 if (imm_expr.X_op == O_constant)
252b5132 12722 {
bad1aba3 12723 if (GPR_SIZE == 64)
c0ebe874 12724 load_register (op[0], &imm_expr, 1);
252b5132
RH
12725 else
12726 {
12727 int hreg, lreg;
12728
12729 if (target_big_endian)
12730 {
c0ebe874
RS
12731 hreg = op[0];
12732 lreg = op[0] + 1;
252b5132
RH
12733 }
12734 else
12735 {
c0ebe874
RS
12736 hreg = op[0] + 1;
12737 lreg = op[0];
252b5132
RH
12738 }
12739
12740 if (hreg <= 31)
67c0d1eb 12741 load_register (hreg, &imm_expr, 0);
252b5132
RH
12742 if (lreg <= 31)
12743 {
12744 if (offset_expr.X_op == O_absent)
67c0d1eb 12745 move_register (lreg, 0);
252b5132
RH
12746 else
12747 {
9c2799c2 12748 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 12749 load_register (lreg, &offset_expr, 0);
252b5132
RH
12750 }
12751 }
12752 }
8fc2e39e 12753 break;
252b5132 12754 }
b0e6f033 12755 gas_assert (imm_expr.X_op == O_absent);
252b5132
RH
12756
12757 /* We know that sym is in the .rdata section. First we get the
12758 upper 16 bits of the address. */
12759 if (mips_pic == NO_PIC)
12760 {
67c0d1eb 12761 macro_build_lui (&offset_expr, AT);
8fc2e39e 12762 used_at = 1;
252b5132 12763 }
0a44bf69 12764 else
252b5132 12765 {
67c0d1eb
RS
12766 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12767 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 12768 used_at = 1;
252b5132 12769 }
bdaaa2e1 12770
252b5132 12771 /* Now we load the register(s). */
bad1aba3 12772 if (GPR_SIZE == 64)
8fc2e39e
TS
12773 {
12774 used_at = 1;
c0ebe874
RS
12775 macro_build (&offset_expr, "ld", "t,o(b)", op[0],
12776 BFD_RELOC_LO16, AT);
8fc2e39e 12777 }
252b5132
RH
12778 else
12779 {
8fc2e39e 12780 used_at = 1;
c0ebe874
RS
12781 macro_build (&offset_expr, "lw", "t,o(b)", op[0],
12782 BFD_RELOC_LO16, AT);
12783 if (op[0] != RA)
252b5132
RH
12784 {
12785 /* FIXME: How in the world do we deal with the possible
12786 overflow here? */
12787 offset_expr.X_add_number += 4;
67c0d1eb 12788 macro_build (&offset_expr, "lw", "t,o(b)",
c0ebe874 12789 op[0] + 1, BFD_RELOC_LO16, AT);
252b5132
RH
12790 }
12791 }
252b5132
RH
12792 break;
12793
12794 case M_LI_DD:
ca4e0257
RS
12795 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
12796 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
12797 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
12798 the value and the low order 32 bits are either zero or in
12799 OFFSET_EXPR. */
b0e6f033 12800 if (imm_expr.X_op == O_constant)
252b5132 12801 {
9b444f95
FS
12802 tempreg = ZERO;
12803 if (((FPR_SIZE == 64 && GPR_SIZE == 64)
12804 || !ISA_HAS_MXHC1 (mips_opts.isa))
12805 && imm_expr.X_add_number != 0)
12806 {
12807 used_at = 1;
12808 tempreg = AT;
12809 load_register (AT, &imm_expr, FPR_SIZE == 64);
12810 }
351cdf24 12811 if (FPR_SIZE == 64 && GPR_SIZE == 64)
9b444f95 12812 macro_build (NULL, "dmtc1", "t,S", tempreg, op[0]);
252b5132
RH
12813 else
12814 {
9b444f95
FS
12815 if (!ISA_HAS_MXHC1 (mips_opts.isa))
12816 {
12817 if (FPR_SIZE != 32)
12818 as_bad (_("Unable to generate `%s' compliant code "
12819 "without mthc1"),
12820 (FPR_SIZE == 64) ? "fp64" : "fpxx");
12821 else
12822 macro_build (NULL, "mtc1", "t,G", tempreg, op[0] + 1);
12823 }
252b5132 12824 if (offset_expr.X_op == O_absent)
c0ebe874 12825 macro_build (NULL, "mtc1", "t,G", 0, op[0]);
252b5132
RH
12826 else
12827 {
9c2799c2 12828 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 12829 load_register (AT, &offset_expr, 0);
c0ebe874 12830 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132 12831 }
9b444f95
FS
12832 if (ISA_HAS_MXHC1 (mips_opts.isa))
12833 {
12834 if (imm_expr.X_add_number != 0)
12835 {
12836 used_at = 1;
12837 tempreg = AT;
12838 load_register (AT, &imm_expr, 0);
12839 }
12840 macro_build (NULL, "mthc1", "t,G", tempreg, op[0]);
12841 }
252b5132
RH
12842 }
12843 break;
12844 }
12845
b0e6f033
RS
12846 gas_assert (imm_expr.X_op == O_absent
12847 && offset_expr.X_op == O_symbol
90ecf173 12848 && offset_expr.X_add_number == 0);
252b5132
RH
12849 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
12850 if (strcmp (s, ".lit8") == 0)
134c0c8b
MR
12851 {
12852 op[2] = mips_gp_register;
f2ae14a1
RS
12853 offset_reloc[0] = BFD_RELOC_MIPS_LITERAL;
12854 offset_reloc[1] = BFD_RELOC_UNUSED;
12855 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
12856 }
12857 else
12858 {
9c2799c2 12859 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 12860 used_at = 1;
0a44bf69 12861 if (mips_pic != NO_PIC)
67c0d1eb
RS
12862 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12863 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
12864 else
12865 {
12866 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 12867 macro_build_lui (&offset_expr, AT);
252b5132 12868 }
bdaaa2e1 12869
c0ebe874 12870 op[2] = AT;
f2ae14a1
RS
12871 offset_reloc[0] = BFD_RELOC_LO16;
12872 offset_reloc[1] = BFD_RELOC_UNUSED;
12873 offset_reloc[2] = BFD_RELOC_UNUSED;
134c0c8b 12874 }
f2ae14a1 12875 align = 8;
6f2117ba 12876 /* Fall through. */
c4a68bea 12877
252b5132 12878 case M_L_DAB:
6f2117ba
PH
12879 /* The MIPS assembler seems to check for X_add_number not
12880 being double aligned and generating:
12881 lui at,%hi(foo+1)
12882 addu at,at,v1
12883 addiu at,at,%lo(foo+1)
12884 lwc1 f2,0(at)
12885 lwc1 f3,4(at)
12886 But, the resulting address is the same after relocation so why
12887 generate the extra instruction? */
bdaaa2e1 12888 /* Itbl support may require additional care here. */
252b5132 12889 coproc = 1;
df58fc94 12890 fmt = "T,o(b)";
0aa27725 12891 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
12892 {
12893 s = "ldc1";
df58fc94 12894 goto ld_st;
252b5132 12895 }
252b5132 12896 s = "lwc1";
252b5132
RH
12897 goto ldd_std;
12898
12899 case M_S_DAB:
df58fc94
RS
12900 gas_assert (!mips_opts.micromips);
12901 /* Itbl support may require additional care here. */
12902 coproc = 1;
12903 fmt = "T,o(b)";
0aa27725 12904 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
12905 {
12906 s = "sdc1";
df58fc94 12907 goto ld_st;
252b5132 12908 }
252b5132 12909 s = "swc1";
252b5132
RH
12910 goto ldd_std;
12911
e407c74b
NC
12912 case M_LQ_AB:
12913 fmt = "t,o(b)";
12914 s = "lq";
12915 goto ld;
12916
12917 case M_SQ_AB:
12918 fmt = "t,o(b)";
12919 s = "sq";
12920 goto ld_st;
12921
252b5132 12922 case M_LD_AB:
df58fc94 12923 fmt = "t,o(b)";
bad1aba3 12924 if (GPR_SIZE == 64)
252b5132
RH
12925 {
12926 s = "ld";
12927 goto ld;
12928 }
252b5132 12929 s = "lw";
252b5132
RH
12930 goto ldd_std;
12931
12932 case M_SD_AB:
df58fc94 12933 fmt = "t,o(b)";
bad1aba3 12934 if (GPR_SIZE == 64)
252b5132
RH
12935 {
12936 s = "sd";
df58fc94 12937 goto ld_st;
252b5132 12938 }
252b5132 12939 s = "sw";
252b5132
RH
12940
12941 ldd_std:
f2ae14a1
RS
12942 /* Even on a big endian machine $fn comes before $fn+1. We have
12943 to adjust when loading from memory. We set coproc if we must
12944 load $fn+1 first. */
12945 /* Itbl support may require additional care here. */
12946 if (!target_big_endian)
12947 coproc = 0;
12948
c0ebe874 12949 breg = op[2];
f2ae14a1
RS
12950 if (small_offset_p (0, align, 16))
12951 {
12952 ep = &offset_expr;
12953 if (!small_offset_p (4, align, 16))
12954 {
12955 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT, breg,
12956 -1, offset_reloc[0], offset_reloc[1],
12957 offset_reloc[2]);
12958 expr1.X_add_number = 0;
12959 ep = &expr1;
12960 breg = AT;
12961 used_at = 1;
12962 offset_reloc[0] = BFD_RELOC_LO16;
12963 offset_reloc[1] = BFD_RELOC_UNUSED;
12964 offset_reloc[2] = BFD_RELOC_UNUSED;
12965 }
c0ebe874 12966 if (strcmp (s, "lw") == 0 && op[0] == breg)
f2ae14a1
RS
12967 {
12968 ep->X_add_number += 4;
c0ebe874 12969 macro_build (ep, s, fmt, op[0] + 1, -1, offset_reloc[0],
f2ae14a1
RS
12970 offset_reloc[1], offset_reloc[2], breg);
12971 ep->X_add_number -= 4;
c0ebe874 12972 macro_build (ep, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
12973 offset_reloc[1], offset_reloc[2], breg);
12974 }
12975 else
12976 {
c0ebe874 12977 macro_build (ep, s, fmt, coproc ? op[0] + 1 : op[0], -1,
f2ae14a1
RS
12978 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12979 breg);
12980 ep->X_add_number += 4;
c0ebe874 12981 macro_build (ep, s, fmt, coproc ? op[0] : op[0] + 1, -1,
f2ae14a1
RS
12982 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12983 breg);
12984 }
12985 break;
12986 }
12987
252b5132
RH
12988 if (offset_expr.X_op != O_symbol
12989 && offset_expr.X_op != O_constant)
12990 {
1661c76c 12991 as_bad (_("expression too complex"));
252b5132
RH
12992 offset_expr.X_op = O_constant;
12993 }
12994
2051e8c4
MR
12995 if (HAVE_32BIT_ADDRESSES
12996 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
12997 {
12998 char value [32];
12999
13000 sprintf_vma (value, offset_expr.X_add_number);
1661c76c 13001 as_bad (_("number (0x%s) larger than 32 bits"), value);
55e08f71 13002 }
2051e8c4 13003
90ecf173 13004 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
252b5132
RH
13005 {
13006 /* If this is a reference to a GP relative symbol, we want
c0ebe874
RS
13007 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
13008 <op> op[0]+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
13009 If we have a base register, we use this
13010 addu $at,$breg,$gp
c0ebe874
RS
13011 <op> op[0],<sym>($at) (BFD_RELOC_GPREL16)
13012 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
13013 If this is not a GP relative symbol, we want
13014 lui $at,<sym> (BFD_RELOC_HI16_S)
c0ebe874
RS
13015 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
13016 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
13017 If there is a base register, we add it to $at after the
13018 lui instruction. If there is a constant, we always use
13019 the last case. */
39a59cf8
MR
13020 if (offset_expr.X_op == O_symbol
13021 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 13022 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 13023 {
4d7206a2 13024 relax_start (offset_expr.X_add_symbol);
252b5132
RH
13025 if (breg == 0)
13026 {
c9914766 13027 tempreg = mips_gp_register;
252b5132
RH
13028 }
13029 else
13030 {
67c0d1eb 13031 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 13032 AT, breg, mips_gp_register);
252b5132 13033 tempreg = AT;
252b5132
RH
13034 used_at = 1;
13035 }
13036
beae10d5 13037 /* Itbl support may require additional care here. */
c0ebe874 13038 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 13039 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
13040 offset_expr.X_add_number += 4;
13041
13042 /* Set mips_optimize to 2 to avoid inserting an
13043 undesired nop. */
13044 hold_mips_optimize = mips_optimize;
13045 mips_optimize = 2;
beae10d5 13046 /* Itbl support may require additional care here. */
c0ebe874 13047 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 13048 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
13049 mips_optimize = hold_mips_optimize;
13050
4d7206a2 13051 relax_switch ();
252b5132 13052
0970e49e 13053 offset_expr.X_add_number -= 4;
252b5132 13054 }
8fc2e39e 13055 used_at = 1;
f2ae14a1
RS
13056 if (offset_high_part (offset_expr.X_add_number, 16)
13057 != offset_high_part (offset_expr.X_add_number + 4, 16))
13058 {
13059 load_address (AT, &offset_expr, &used_at);
13060 offset_expr.X_op = O_constant;
13061 offset_expr.X_add_number = 0;
13062 }
13063 else
13064 macro_build_lui (&offset_expr, AT);
252b5132 13065 if (breg != 0)
67c0d1eb 13066 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 13067 /* Itbl support may require additional care here. */
c0ebe874 13068 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 13069 BFD_RELOC_LO16, AT);
252b5132
RH
13070 /* FIXME: How do we handle overflow here? */
13071 offset_expr.X_add_number += 4;
beae10d5 13072 /* Itbl support may require additional care here. */
c0ebe874 13073 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 13074 BFD_RELOC_LO16, AT);
4d7206a2
RS
13075 if (mips_relax.sequence)
13076 relax_end ();
bdaaa2e1 13077 }
0a44bf69 13078 else if (!mips_big_got)
252b5132 13079 {
252b5132
RH
13080 /* If this is a reference to an external symbol, we want
13081 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
13082 nop
c0ebe874
RS
13083 <op> op[0],0($at)
13084 <op> op[0]+1,4($at)
252b5132
RH
13085 Otherwise we want
13086 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
13087 nop
c0ebe874
RS
13088 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
13089 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
13090 If there is a base register we add it to $at before the
13091 lwc1 instructions. If there is a constant we include it
13092 in the lwc1 instructions. */
13093 used_at = 1;
13094 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
13095 if (expr1.X_add_number < -0x8000
13096 || expr1.X_add_number >= 0x8000 - 4)
13097 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 13098 load_got_offset (AT, &offset_expr);
269137b2 13099 load_delay_nop ();
252b5132 13100 if (breg != 0)
67c0d1eb 13101 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
13102
13103 /* Set mips_optimize to 2 to avoid inserting an undesired
13104 nop. */
13105 hold_mips_optimize = mips_optimize;
13106 mips_optimize = 2;
4d7206a2 13107
beae10d5 13108 /* Itbl support may require additional care here. */
4d7206a2 13109 relax_start (offset_expr.X_add_symbol);
c0ebe874 13110 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 13111 BFD_RELOC_LO16, AT);
4d7206a2 13112 expr1.X_add_number += 4;
c0ebe874 13113 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 13114 BFD_RELOC_LO16, AT);
4d7206a2 13115 relax_switch ();
c0ebe874 13116 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 13117 BFD_RELOC_LO16, AT);
4d7206a2 13118 offset_expr.X_add_number += 4;
c0ebe874 13119 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 13120 BFD_RELOC_LO16, AT);
4d7206a2 13121 relax_end ();
252b5132 13122
4d7206a2 13123 mips_optimize = hold_mips_optimize;
252b5132 13124 }
0a44bf69 13125 else if (mips_big_got)
252b5132 13126 {
67c0d1eb 13127 int gpdelay;
252b5132
RH
13128
13129 /* If this is a reference to an external symbol, we want
13130 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
13131 addu $at,$at,$gp
13132 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
13133 nop
c0ebe874
RS
13134 <op> op[0],0($at)
13135 <op> op[0]+1,4($at)
252b5132
RH
13136 Otherwise we want
13137 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
13138 nop
c0ebe874
RS
13139 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
13140 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
13141 If there is a base register we add it to $at before the
13142 lwc1 instructions. If there is a constant we include it
13143 in the lwc1 instructions. */
13144 used_at = 1;
13145 expr1.X_add_number = offset_expr.X_add_number;
13146 offset_expr.X_add_number = 0;
13147 if (expr1.X_add_number < -0x8000
13148 || expr1.X_add_number >= 0x8000 - 4)
13149 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 13150 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 13151 relax_start (offset_expr.X_add_symbol);
df58fc94 13152 macro_build (&offset_expr, "lui", LUI_FMT,
67c0d1eb
RS
13153 AT, BFD_RELOC_MIPS_GOT_HI16);
13154 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 13155 AT, AT, mips_gp_register);
67c0d1eb 13156 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 13157 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 13158 load_delay_nop ();
252b5132 13159 if (breg != 0)
67c0d1eb 13160 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 13161 /* Itbl support may require additional care here. */
c0ebe874 13162 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 13163 BFD_RELOC_LO16, AT);
252b5132
RH
13164 expr1.X_add_number += 4;
13165
13166 /* Set mips_optimize to 2 to avoid inserting an undesired
13167 nop. */
13168 hold_mips_optimize = mips_optimize;
13169 mips_optimize = 2;
beae10d5 13170 /* Itbl support may require additional care here. */
c0ebe874 13171 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 13172 BFD_RELOC_LO16, AT);
252b5132
RH
13173 mips_optimize = hold_mips_optimize;
13174 expr1.X_add_number -= 4;
13175
4d7206a2
RS
13176 relax_switch ();
13177 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
13178 if (gpdelay)
13179 macro_build (NULL, "nop", "");
13180 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
13181 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 13182 load_delay_nop ();
252b5132 13183 if (breg != 0)
67c0d1eb 13184 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 13185 /* Itbl support may require additional care here. */
c0ebe874 13186 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 13187 BFD_RELOC_LO16, AT);
4d7206a2 13188 offset_expr.X_add_number += 4;
252b5132
RH
13189
13190 /* Set mips_optimize to 2 to avoid inserting an undesired
13191 nop. */
13192 hold_mips_optimize = mips_optimize;
13193 mips_optimize = 2;
beae10d5 13194 /* Itbl support may require additional care here. */
c0ebe874 13195 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 13196 BFD_RELOC_LO16, AT);
252b5132 13197 mips_optimize = hold_mips_optimize;
4d7206a2 13198 relax_end ();
252b5132 13199 }
252b5132
RH
13200 else
13201 abort ();
13202
252b5132 13203 break;
3739860c 13204
dd6a37e7 13205 case M_SAA_AB:
dd6a37e7 13206 s = "saa";
0db377d0 13207 goto saa_saad;
dd6a37e7 13208 case M_SAAD_AB:
dd6a37e7 13209 s = "saad";
0db377d0
MR
13210 saa_saad:
13211 gas_assert (!mips_opts.micromips);
7f3c4072 13212 offbits = 0;
dd6a37e7
AP
13213 fmt = "t,(b)";
13214 goto ld_st;
13215
252b5132
RH
13216 /* New code added to support COPZ instructions.
13217 This code builds table entries out of the macros in mip_opcodes.
13218 R4000 uses interlocks to handle coproc delays.
13219 Other chips (like the R3000) require nops to be inserted for delays.
13220
f72c8c98 13221 FIXME: Currently, we require that the user handle delays.
252b5132
RH
13222 In order to fill delay slots for non-interlocked chips,
13223 we must have a way to specify delays based on the coprocessor.
13224 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
13225 What are the side-effects of the cop instruction?
13226 What cache support might we have and what are its effects?
13227 Both coprocessor & memory require delays. how long???
bdaaa2e1 13228 What registers are read/set/modified?
252b5132
RH
13229
13230 If an itbl is provided to interpret cop instructions,
bdaaa2e1 13231 this knowledge can be encoded in the itbl spec. */
252b5132
RH
13232
13233 case M_COP0:
13234 s = "c0";
13235 goto copz;
13236 case M_COP1:
13237 s = "c1";
13238 goto copz;
13239 case M_COP2:
13240 s = "c2";
13241 goto copz;
13242 case M_COP3:
13243 s = "c3";
13244 copz:
df58fc94 13245 gas_assert (!mips_opts.micromips);
252b5132
RH
13246 /* For now we just do C (same as Cz). The parameter will be
13247 stored in insn_opcode by mips_ip. */
c8276761 13248 macro_build (NULL, s, "C", (int) ip->insn_opcode);
8fc2e39e 13249 break;
252b5132 13250
ea1fb5dc 13251 case M_MOVE:
c0ebe874 13252 move_register (op[0], op[1]);
8fc2e39e 13253 break;
ea1fb5dc 13254
833794fc
MR
13255 case M_MOVEP:
13256 gas_assert (mips_opts.micromips);
13257 gas_assert (mips_opts.insn32);
c0ebe874
RS
13258 move_register (micromips_to_32_reg_h_map1[op[0]],
13259 micromips_to_32_reg_m_map[op[1]]);
13260 move_register (micromips_to_32_reg_h_map2[op[0]],
13261 micromips_to_32_reg_n_map[op[2]]);
833794fc
MR
13262 break;
13263
252b5132
RH
13264 case M_DMUL:
13265 dbl = 1;
1a0670f3 13266 /* Fall through. */
252b5132 13267 case M_MUL:
e407c74b 13268 if (mips_opts.arch == CPU_R5900)
c0ebe874
RS
13269 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", op[0], op[1],
13270 op[2]);
e407c74b
NC
13271 else
13272 {
c0ebe874
RS
13273 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", op[1], op[2]);
13274 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
e407c74b 13275 }
8fc2e39e 13276 break;
252b5132
RH
13277
13278 case M_DMUL_I:
13279 dbl = 1;
1a0670f3 13280 /* Fall through. */
252b5132
RH
13281 case M_MUL_I:
13282 /* The MIPS assembler some times generates shifts and adds. I'm
13283 not trying to be that fancy. GCC should do this for us
13284 anyway. */
8fc2e39e 13285 used_at = 1;
67c0d1eb 13286 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
13287 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", op[1], AT);
13288 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
13289 break;
13290
13291 case M_DMULO_I:
13292 dbl = 1;
1a0670f3 13293 /* Fall through. */
252b5132
RH
13294 case M_MULO_I:
13295 imm = 1;
13296 goto do_mulo;
13297
13298 case M_DMULO:
13299 dbl = 1;
1a0670f3 13300 /* Fall through. */
252b5132
RH
13301 case M_MULO:
13302 do_mulo:
7d10b47d 13303 start_noreorder ();
8fc2e39e 13304 used_at = 1;
252b5132 13305 if (imm)
67c0d1eb 13306 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
13307 macro_build (NULL, dbl ? "dmult" : "mult", "s,t",
13308 op[1], imm ? AT : op[2]);
13309 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13310 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, op[0], op[0], 31);
df58fc94 13311 macro_build (NULL, "mfhi", MFHL_FMT, AT);
252b5132 13312 if (mips_trap)
c0ebe874 13313 macro_build (NULL, "tne", TRAP_FMT, op[0], AT, 6);
252b5132
RH
13314 else
13315 {
df58fc94
RS
13316 if (mips_opts.micromips)
13317 micromips_label_expr (&label_expr);
13318 else
13319 label_expr.X_add_number = 8;
c0ebe874 13320 macro_build (&label_expr, "beq", "s,t,p", op[0], AT);
a605d2b3 13321 macro_build (NULL, "nop", "");
df58fc94
RS
13322 macro_build (NULL, "break", BRK_FMT, 6);
13323 if (mips_opts.micromips)
13324 micromips_add_label ();
252b5132 13325 }
7d10b47d 13326 end_noreorder ();
c0ebe874 13327 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
13328 break;
13329
13330 case M_DMULOU_I:
13331 dbl = 1;
1a0670f3 13332 /* Fall through. */
252b5132
RH
13333 case M_MULOU_I:
13334 imm = 1;
13335 goto do_mulou;
13336
13337 case M_DMULOU:
13338 dbl = 1;
1a0670f3 13339 /* Fall through. */
252b5132
RH
13340 case M_MULOU:
13341 do_mulou:
7d10b47d 13342 start_noreorder ();
8fc2e39e 13343 used_at = 1;
252b5132 13344 if (imm)
67c0d1eb
RS
13345 load_register (AT, &imm_expr, dbl);
13346 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
c0ebe874 13347 op[1], imm ? AT : op[2]);
df58fc94 13348 macro_build (NULL, "mfhi", MFHL_FMT, AT);
c0ebe874 13349 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132 13350 if (mips_trap)
df58fc94 13351 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
252b5132
RH
13352 else
13353 {
df58fc94
RS
13354 if (mips_opts.micromips)
13355 micromips_label_expr (&label_expr);
13356 else
13357 label_expr.X_add_number = 8;
13358 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
a605d2b3 13359 macro_build (NULL, "nop", "");
df58fc94
RS
13360 macro_build (NULL, "break", BRK_FMT, 6);
13361 if (mips_opts.micromips)
13362 micromips_add_label ();
252b5132 13363 }
7d10b47d 13364 end_noreorder ();
252b5132
RH
13365 break;
13366
771c7ce4 13367 case M_DROL:
fef14a42 13368 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 13369 {
c0ebe874 13370 if (op[0] == op[1])
82dd0097
CD
13371 {
13372 tempreg = AT;
13373 used_at = 1;
13374 }
13375 else
c0ebe874
RS
13376 tempreg = op[0];
13377 macro_build (NULL, "dnegu", "d,w", tempreg, op[2]);
13378 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 13379 break;
82dd0097 13380 }
8fc2e39e 13381 used_at = 1;
c0ebe874
RS
13382 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13383 macro_build (NULL, "dsrlv", "d,t,s", AT, op[1], AT);
13384 macro_build (NULL, "dsllv", "d,t,s", op[0], op[1], op[2]);
13385 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13386 break;
13387
252b5132 13388 case M_ROL:
fef14a42 13389 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13390 {
c0ebe874 13391 if (op[0] == op[1])
82dd0097
CD
13392 {
13393 tempreg = AT;
13394 used_at = 1;
13395 }
13396 else
c0ebe874
RS
13397 tempreg = op[0];
13398 macro_build (NULL, "negu", "d,w", tempreg, op[2]);
13399 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 13400 break;
82dd0097 13401 }
8fc2e39e 13402 used_at = 1;
c0ebe874
RS
13403 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13404 macro_build (NULL, "srlv", "d,t,s", AT, op[1], AT);
13405 macro_build (NULL, "sllv", "d,t,s", op[0], op[1], op[2]);
13406 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13407 break;
13408
771c7ce4
TS
13409 case M_DROL_I:
13410 {
13411 unsigned int rot;
e0471c16
TS
13412 const char *l;
13413 const char *rr;
771c7ce4 13414
771c7ce4 13415 rot = imm_expr.X_add_number & 0x3f;
fef14a42 13416 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
13417 {
13418 rot = (64 - rot) & 0x3f;
13419 if (rot >= 32)
c0ebe874 13420 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
60b63b72 13421 else
c0ebe874 13422 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13423 break;
60b63b72 13424 }
483fc7cd 13425 if (rot == 0)
483fc7cd 13426 {
c0ebe874 13427 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13428 break;
483fc7cd 13429 }
82dd0097 13430 l = (rot < 0x20) ? "dsll" : "dsll32";
91d6fa6a 13431 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
82dd0097 13432 rot &= 0x1f;
8fc2e39e 13433 used_at = 1;
c0ebe874
RS
13434 macro_build (NULL, l, SHFT_FMT, AT, op[1], rot);
13435 macro_build (NULL, rr, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13436 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13437 }
13438 break;
13439
252b5132 13440 case M_ROL_I:
771c7ce4
TS
13441 {
13442 unsigned int rot;
13443
771c7ce4 13444 rot = imm_expr.X_add_number & 0x1f;
fef14a42 13445 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 13446 {
c0ebe874
RS
13447 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1],
13448 (32 - rot) & 0x1f);
8fc2e39e 13449 break;
60b63b72 13450 }
483fc7cd 13451 if (rot == 0)
483fc7cd 13452 {
c0ebe874 13453 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13454 break;
483fc7cd 13455 }
8fc2e39e 13456 used_at = 1;
c0ebe874
RS
13457 macro_build (NULL, "sll", SHFT_FMT, AT, op[1], rot);
13458 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13459 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13460 }
13461 break;
13462
13463 case M_DROR:
fef14a42 13464 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 13465 {
c0ebe874 13466 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 13467 break;
82dd0097 13468 }
8fc2e39e 13469 used_at = 1;
c0ebe874
RS
13470 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13471 macro_build (NULL, "dsllv", "d,t,s", AT, op[1], AT);
13472 macro_build (NULL, "dsrlv", "d,t,s", op[0], op[1], op[2]);
13473 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13474 break;
13475
13476 case M_ROR:
fef14a42 13477 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13478 {
c0ebe874 13479 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 13480 break;
82dd0097 13481 }
8fc2e39e 13482 used_at = 1;
c0ebe874
RS
13483 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13484 macro_build (NULL, "sllv", "d,t,s", AT, op[1], AT);
13485 macro_build (NULL, "srlv", "d,t,s", op[0], op[1], op[2]);
13486 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13487 break;
13488
771c7ce4
TS
13489 case M_DROR_I:
13490 {
13491 unsigned int rot;
e0471c16
TS
13492 const char *l;
13493 const char *rr;
771c7ce4 13494
771c7ce4 13495 rot = imm_expr.X_add_number & 0x3f;
fef14a42 13496 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
13497 {
13498 if (rot >= 32)
c0ebe874 13499 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
82dd0097 13500 else
c0ebe874 13501 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13502 break;
82dd0097 13503 }
483fc7cd 13504 if (rot == 0)
483fc7cd 13505 {
c0ebe874 13506 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13507 break;
483fc7cd 13508 }
91d6fa6a 13509 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
82dd0097
CD
13510 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
13511 rot &= 0x1f;
8fc2e39e 13512 used_at = 1;
c0ebe874
RS
13513 macro_build (NULL, rr, SHFT_FMT, AT, op[1], rot);
13514 macro_build (NULL, l, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13515 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13516 }
13517 break;
13518
252b5132 13519 case M_ROR_I:
771c7ce4
TS
13520 {
13521 unsigned int rot;
13522
771c7ce4 13523 rot = imm_expr.X_add_number & 0x1f;
fef14a42 13524 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13525 {
c0ebe874 13526 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13527 break;
82dd0097 13528 }
483fc7cd 13529 if (rot == 0)
483fc7cd 13530 {
c0ebe874 13531 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13532 break;
483fc7cd 13533 }
8fc2e39e 13534 used_at = 1;
c0ebe874
RS
13535 macro_build (NULL, "srl", SHFT_FMT, AT, op[1], rot);
13536 macro_build (NULL, "sll", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13537 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4 13538 }
252b5132
RH
13539 break;
13540
252b5132 13541 case M_SEQ:
c0ebe874
RS
13542 if (op[1] == 0)
13543 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[2], BFD_RELOC_LO16);
13544 else if (op[2] == 0)
13545 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
13546 else
13547 {
c0ebe874
RS
13548 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13549 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
252b5132 13550 }
8fc2e39e 13551 break;
252b5132
RH
13552
13553 case M_SEQ_I:
b0e6f033 13554 if (imm_expr.X_add_number == 0)
252b5132 13555 {
c0ebe874 13556 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 13557 break;
252b5132 13558 }
c0ebe874 13559 if (op[1] == 0)
252b5132 13560 {
1661c76c 13561 as_warn (_("instruction %s: result is always false"),
252b5132 13562 ip->insn_mo->name);
c0ebe874 13563 move_register (op[0], 0);
8fc2e39e 13564 break;
252b5132 13565 }
dd3cbb7e
NC
13566 if (CPU_HAS_SEQ (mips_opts.arch)
13567 && -512 <= imm_expr.X_add_number
13568 && imm_expr.X_add_number < 512)
13569 {
c0ebe874 13570 macro_build (NULL, "seqi", "t,r,+Q", op[0], op[1],
750bdd57 13571 (int) imm_expr.X_add_number);
dd3cbb7e
NC
13572 break;
13573 }
b0e6f033 13574 if (imm_expr.X_add_number >= 0
252b5132 13575 && imm_expr.X_add_number < 0x10000)
c0ebe874 13576 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1], BFD_RELOC_LO16);
b0e6f033 13577 else if (imm_expr.X_add_number > -0x8000
252b5132
RH
13578 && imm_expr.X_add_number < 0)
13579 {
13580 imm_expr.X_add_number = -imm_expr.X_add_number;
bad1aba3 13581 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
c0ebe874 13582 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 13583 }
dd3cbb7e
NC
13584 else if (CPU_HAS_SEQ (mips_opts.arch))
13585 {
13586 used_at = 1;
bad1aba3 13587 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13588 macro_build (NULL, "seq", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
13589 break;
13590 }
252b5132
RH
13591 else
13592 {
bad1aba3 13593 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13594 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
13595 used_at = 1;
13596 }
c0ebe874 13597 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13598 break;
252b5132 13599
c0ebe874 13600 case M_SGE: /* X >= Y <==> not (X < Y) */
252b5132
RH
13601 s = "slt";
13602 goto sge;
13603 case M_SGEU:
13604 s = "sltu";
13605 sge:
c0ebe874
RS
13606 macro_build (NULL, s, "d,v,t", op[0], op[1], op[2]);
13607 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13608 break;
252b5132 13609
6f2117ba 13610 case M_SGE_I: /* X >= I <==> not (X < I). */
252b5132 13611 case M_SGEU_I:
b0e6f033 13612 if (imm_expr.X_add_number >= -0x8000
252b5132 13613 && imm_expr.X_add_number < 0x8000)
c0ebe874
RS
13614 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
13615 op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
13616 else
13617 {
bad1aba3 13618 load_register (AT, &imm_expr, GPR_SIZE == 64);
67c0d1eb 13619 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
c0ebe874 13620 op[0], op[1], AT);
252b5132
RH
13621 used_at = 1;
13622 }
c0ebe874 13623 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13624 break;
252b5132 13625
6f2117ba 13626 case M_SGT: /* X > Y <==> Y < X. */
252b5132
RH
13627 s = "slt";
13628 goto sgt;
13629 case M_SGTU:
13630 s = "sltu";
13631 sgt:
c0ebe874 13632 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
8fc2e39e 13633 break;
252b5132 13634
6f2117ba 13635 case M_SGT_I: /* X > I <==> I < X. */
252b5132
RH
13636 s = "slt";
13637 goto sgti;
13638 case M_SGTU_I:
13639 s = "sltu";
13640 sgti:
8fc2e39e 13641 used_at = 1;
bad1aba3 13642 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13643 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
252b5132
RH
13644 break;
13645
6f2117ba 13646 case M_SLE: /* X <= Y <==> Y >= X <==> not (Y < X). */
252b5132
RH
13647 s = "slt";
13648 goto sle;
13649 case M_SLEU:
13650 s = "sltu";
13651 sle:
c0ebe874
RS
13652 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13653 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13654 break;
252b5132 13655
c0ebe874 13656 case M_SLE_I: /* X <= I <==> I >= X <==> not (I < X) */
252b5132
RH
13657 s = "slt";
13658 goto slei;
13659 case M_SLEU_I:
13660 s = "sltu";
13661 slei:
8fc2e39e 13662 used_at = 1;
bad1aba3 13663 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874
RS
13664 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13665 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
252b5132
RH
13666 break;
13667
13668 case M_SLT_I:
b0e6f033 13669 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
13670 && imm_expr.X_add_number < 0x8000)
13671 {
c0ebe874
RS
13672 macro_build (&imm_expr, "slti", "t,r,j", op[0], op[1],
13673 BFD_RELOC_LO16);
8fc2e39e 13674 break;
252b5132 13675 }
8fc2e39e 13676 used_at = 1;
bad1aba3 13677 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13678 macro_build (NULL, "slt", "d,v,t", op[0], op[1], AT);
252b5132
RH
13679 break;
13680
13681 case M_SLTU_I:
b0e6f033 13682 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
13683 && imm_expr.X_add_number < 0x8000)
13684 {
c0ebe874 13685 macro_build (&imm_expr, "sltiu", "t,r,j", op[0], op[1],
17a2f251 13686 BFD_RELOC_LO16);
8fc2e39e 13687 break;
252b5132 13688 }
8fc2e39e 13689 used_at = 1;
bad1aba3 13690 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13691 macro_build (NULL, "sltu", "d,v,t", op[0], op[1], AT);
252b5132
RH
13692 break;
13693
13694 case M_SNE:
c0ebe874
RS
13695 if (op[1] == 0)
13696 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[2]);
13697 else if (op[2] == 0)
13698 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
252b5132
RH
13699 else
13700 {
c0ebe874
RS
13701 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13702 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
252b5132 13703 }
8fc2e39e 13704 break;
252b5132
RH
13705
13706 case M_SNE_I:
b0e6f033 13707 if (imm_expr.X_add_number == 0)
252b5132 13708 {
c0ebe874 13709 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
8fc2e39e 13710 break;
252b5132 13711 }
c0ebe874 13712 if (op[1] == 0)
252b5132 13713 {
1661c76c 13714 as_warn (_("instruction %s: result is always true"),
252b5132 13715 ip->insn_mo->name);
bad1aba3 13716 macro_build (&expr1, GPR_SIZE == 32 ? "addiu" : "daddiu", "t,r,j",
c0ebe874 13717 op[0], 0, BFD_RELOC_LO16);
8fc2e39e 13718 break;
252b5132 13719 }
dd3cbb7e
NC
13720 if (CPU_HAS_SEQ (mips_opts.arch)
13721 && -512 <= imm_expr.X_add_number
13722 && imm_expr.X_add_number < 512)
13723 {
c0ebe874 13724 macro_build (NULL, "snei", "t,r,+Q", op[0], op[1],
750bdd57 13725 (int) imm_expr.X_add_number);
dd3cbb7e
NC
13726 break;
13727 }
b0e6f033 13728 if (imm_expr.X_add_number >= 0
252b5132
RH
13729 && imm_expr.X_add_number < 0x10000)
13730 {
c0ebe874
RS
13731 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1],
13732 BFD_RELOC_LO16);
252b5132 13733 }
b0e6f033 13734 else if (imm_expr.X_add_number > -0x8000
252b5132
RH
13735 && imm_expr.X_add_number < 0)
13736 {
13737 imm_expr.X_add_number = -imm_expr.X_add_number;
bad1aba3 13738 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
c0ebe874 13739 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 13740 }
dd3cbb7e
NC
13741 else if (CPU_HAS_SEQ (mips_opts.arch))
13742 {
13743 used_at = 1;
bad1aba3 13744 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13745 macro_build (NULL, "sne", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
13746 break;
13747 }
252b5132
RH
13748 else
13749 {
bad1aba3 13750 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13751 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
13752 used_at = 1;
13753 }
c0ebe874 13754 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
8fc2e39e 13755 break;
252b5132 13756
df58fc94
RS
13757 case M_SUB_I:
13758 s = "addi";
13759 s2 = "sub";
387e7624
FS
13760 if (ISA_IS_R6 (mips_opts.isa))
13761 goto do_subi_i;
13762 else
13763 goto do_subi;
df58fc94
RS
13764 case M_SUBU_I:
13765 s = "addiu";
13766 s2 = "subu";
13767 goto do_subi;
252b5132
RH
13768 case M_DSUB_I:
13769 dbl = 1;
df58fc94
RS
13770 s = "daddi";
13771 s2 = "dsub";
387e7624 13772 if (!mips_opts.micromips && !ISA_IS_R6 (mips_opts.isa))
df58fc94 13773 goto do_subi;
b0e6f033 13774 if (imm_expr.X_add_number > -0x200
387e7624
FS
13775 && imm_expr.X_add_number <= 0x200
13776 && !ISA_IS_R6 (mips_opts.isa))
252b5132 13777 {
b0e6f033
RS
13778 macro_build (NULL, s, "t,r,.", op[0], op[1],
13779 (int) -imm_expr.X_add_number);
8fc2e39e 13780 break;
252b5132 13781 }
df58fc94 13782 goto do_subi_i;
252b5132
RH
13783 case M_DSUBU_I:
13784 dbl = 1;
df58fc94
RS
13785 s = "daddiu";
13786 s2 = "dsubu";
13787 do_subi:
b0e6f033 13788 if (imm_expr.X_add_number > -0x8000
252b5132
RH
13789 && imm_expr.X_add_number <= 0x8000)
13790 {
13791 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 13792 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 13793 break;
252b5132 13794 }
df58fc94 13795 do_subi_i:
8fc2e39e 13796 used_at = 1;
67c0d1eb 13797 load_register (AT, &imm_expr, dbl);
c0ebe874 13798 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
13799 break;
13800
13801 case M_TEQ_I:
13802 s = "teq";
13803 goto trap;
13804 case M_TGE_I:
13805 s = "tge";
13806 goto trap;
13807 case M_TGEU_I:
13808 s = "tgeu";
13809 goto trap;
13810 case M_TLT_I:
13811 s = "tlt";
13812 goto trap;
13813 case M_TLTU_I:
13814 s = "tltu";
13815 goto trap;
13816 case M_TNE_I:
13817 s = "tne";
13818 trap:
8fc2e39e 13819 used_at = 1;
bad1aba3 13820 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13821 macro_build (NULL, s, "s,t", op[0], AT);
252b5132
RH
13822 break;
13823
252b5132 13824 case M_TRUNCWS:
43841e91 13825 case M_TRUNCWD:
df58fc94 13826 gas_assert (!mips_opts.micromips);
0aa27725 13827 gas_assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 13828 used_at = 1;
252b5132
RH
13829
13830 /*
13831 * Is the double cfc1 instruction a bug in the mips assembler;
13832 * or is there a reason for it?
13833 */
7d10b47d 13834 start_noreorder ();
c0ebe874
RS
13835 macro_build (NULL, "cfc1", "t,G", op[2], RA);
13836 macro_build (NULL, "cfc1", "t,G", op[2], RA);
67c0d1eb 13837 macro_build (NULL, "nop", "");
252b5132 13838 expr1.X_add_number = 3;
c0ebe874 13839 macro_build (&expr1, "ori", "t,r,i", AT, op[2], BFD_RELOC_LO16);
252b5132 13840 expr1.X_add_number = 2;
67c0d1eb
RS
13841 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
13842 macro_build (NULL, "ctc1", "t,G", AT, RA);
13843 macro_build (NULL, "nop", "");
13844 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
c0ebe874
RS
13845 op[0], op[1]);
13846 macro_build (NULL, "ctc1", "t,G", op[2], RA);
67c0d1eb 13847 macro_build (NULL, "nop", "");
7d10b47d 13848 end_noreorder ();
252b5132
RH
13849 break;
13850
f2ae14a1 13851 case M_ULH_AB:
252b5132 13852 s = "lb";
df58fc94
RS
13853 s2 = "lbu";
13854 off = 1;
13855 goto uld_st;
f2ae14a1 13856 case M_ULHU_AB:
252b5132 13857 s = "lbu";
df58fc94
RS
13858 s2 = "lbu";
13859 off = 1;
13860 goto uld_st;
f2ae14a1 13861 case M_ULW_AB:
df58fc94
RS
13862 s = "lwl";
13863 s2 = "lwr";
7f3c4072 13864 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
13865 off = 3;
13866 goto uld_st;
f2ae14a1 13867 case M_ULD_AB:
252b5132
RH
13868 s = "ldl";
13869 s2 = "ldr";
7f3c4072 13870 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 13871 off = 7;
df58fc94 13872 goto uld_st;
f2ae14a1 13873 case M_USH_AB:
df58fc94
RS
13874 s = "sb";
13875 s2 = "sb";
13876 off = 1;
13877 ust = 1;
13878 goto uld_st;
f2ae14a1 13879 case M_USW_AB:
df58fc94
RS
13880 s = "swl";
13881 s2 = "swr";
7f3c4072 13882 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 13883 off = 3;
df58fc94
RS
13884 ust = 1;
13885 goto uld_st;
f2ae14a1 13886 case M_USD_AB:
df58fc94
RS
13887 s = "sdl";
13888 s2 = "sdr";
7f3c4072 13889 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
13890 off = 7;
13891 ust = 1;
13892
13893 uld_st:
c0ebe874 13894 breg = op[2];
f2ae14a1 13895 large_offset = !small_offset_p (off, align, offbits);
df58fc94
RS
13896 ep = &offset_expr;
13897 expr1.X_add_number = 0;
f2ae14a1 13898 if (large_offset)
df58fc94
RS
13899 {
13900 used_at = 1;
13901 tempreg = AT;
f2ae14a1
RS
13902 if (small_offset_p (0, align, 16))
13903 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg, -1,
13904 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
13905 else
13906 {
13907 load_address (tempreg, ep, &used_at);
13908 if (breg != 0)
13909 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
13910 tempreg, tempreg, breg);
13911 }
13912 offset_reloc[0] = BFD_RELOC_LO16;
13913 offset_reloc[1] = BFD_RELOC_UNUSED;
13914 offset_reloc[2] = BFD_RELOC_UNUSED;
df58fc94 13915 breg = tempreg;
c0ebe874 13916 tempreg = op[0];
df58fc94
RS
13917 ep = &expr1;
13918 }
c0ebe874 13919 else if (!ust && op[0] == breg)
8fc2e39e
TS
13920 {
13921 used_at = 1;
13922 tempreg = AT;
13923 }
252b5132 13924 else
c0ebe874 13925 tempreg = op[0];
af22f5b2 13926
df58fc94
RS
13927 if (off == 1)
13928 goto ulh_sh;
252b5132 13929
90ecf173 13930 if (!target_big_endian)
df58fc94 13931 ep->X_add_number += off;
f2ae14a1 13932 if (offbits == 12)
c8276761 13933 macro_build (NULL, s, "t,~(b)", tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
13934 else
13935 macro_build (ep, s, "t,o(b)", tempreg, -1,
13936 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94 13937
90ecf173 13938 if (!target_big_endian)
df58fc94 13939 ep->X_add_number -= off;
252b5132 13940 else
df58fc94 13941 ep->X_add_number += off;
f2ae14a1 13942 if (offbits == 12)
df58fc94 13943 macro_build (NULL, s2, "t,~(b)",
c8276761 13944 tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
13945 else
13946 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13947 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 13948
df58fc94 13949 /* If necessary, move the result in tempreg to the final destination. */
c0ebe874 13950 if (!ust && op[0] != tempreg)
df58fc94
RS
13951 {
13952 /* Protect second load's delay slot. */
13953 load_delay_nop ();
c0ebe874 13954 move_register (op[0], tempreg);
df58fc94 13955 }
8fc2e39e 13956 break;
252b5132 13957
df58fc94 13958 ulh_sh:
d6bc6245 13959 used_at = 1;
df58fc94
RS
13960 if (target_big_endian == ust)
13961 ep->X_add_number += off;
c0ebe874 13962 tempreg = ust || large_offset ? op[0] : AT;
f2ae14a1
RS
13963 macro_build (ep, s, "t,o(b)", tempreg, -1,
13964 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94
RS
13965
13966 /* For halfword transfers we need a temporary register to shuffle
13967 bytes. Unfortunately for M_USH_A we have none available before
13968 the next store as AT holds the base address. We deal with this
13969 case by clobbering TREG and then restoring it as with ULH. */
c0ebe874 13970 tempreg = ust == large_offset ? op[0] : AT;
df58fc94 13971 if (ust)
c0ebe874 13972 macro_build (NULL, "srl", SHFT_FMT, tempreg, op[0], 8);
df58fc94
RS
13973
13974 if (target_big_endian == ust)
13975 ep->X_add_number -= off;
252b5132 13976 else
df58fc94 13977 ep->X_add_number += off;
f2ae14a1
RS
13978 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13979 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 13980
df58fc94 13981 /* For M_USH_A re-retrieve the LSB. */
f2ae14a1 13982 if (ust && large_offset)
df58fc94
RS
13983 {
13984 if (target_big_endian)
13985 ep->X_add_number += off;
13986 else
13987 ep->X_add_number -= off;
f2ae14a1
RS
13988 macro_build (&expr1, "lbu", "t,o(b)", AT, -1,
13989 offset_reloc[0], offset_reloc[1], offset_reloc[2], AT);
df58fc94
RS
13990 }
13991 /* For ULH and M_USH_A OR the LSB in. */
f2ae14a1 13992 if (!ust || large_offset)
df58fc94 13993 {
c0ebe874 13994 tempreg = !large_offset ? AT : op[0];
df58fc94 13995 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
c0ebe874 13996 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
df58fc94 13997 }
252b5132
RH
13998 break;
13999
14000 default:
14001 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 14002 are added dynamically. */
1661c76c 14003 as_bad (_("macro %s not implemented yet"), ip->insn_mo->name);
252b5132
RH
14004 break;
14005 }
741fe287 14006 if (!mips_opts.at && used_at)
1661c76c 14007 as_bad (_("macro used $at after \".set noat\""));
252b5132
RH
14008}
14009
14010/* Implement macros in mips16 mode. */
14011
14012static void
17a2f251 14013mips16_macro (struct mips_cl_insn *ip)
252b5132 14014{
c0ebe874 14015 const struct mips_operand_array *operands;
252b5132 14016 int mask;
c0ebe874 14017 int tmp;
252b5132
RH
14018 expressionS expr1;
14019 int dbl;
14020 const char *s, *s2, *s3;
c0ebe874
RS
14021 unsigned int op[MAX_OPERANDS];
14022 unsigned int i;
252b5132
RH
14023
14024 mask = ip->insn_mo->mask;
14025
c0ebe874
RS
14026 operands = insn_operands (ip);
14027 for (i = 0; i < MAX_OPERANDS; i++)
14028 if (operands->operand[i])
14029 op[i] = insn_extract_operand (ip, operands->operand[i]);
14030 else
14031 op[i] = -1;
252b5132 14032
252b5132
RH
14033 expr1.X_op = O_constant;
14034 expr1.X_op_symbol = NULL;
14035 expr1.X_add_symbol = NULL;
14036 expr1.X_add_number = 1;
14037
14038 dbl = 0;
14039
14040 switch (mask)
14041 {
14042 default:
b37df7c4 14043 abort ();
252b5132
RH
14044
14045 case M_DDIV_3:
14046 dbl = 1;
1a0670f3 14047 /* Fall through. */
252b5132
RH
14048 case M_DIV_3:
14049 s = "mflo";
14050 goto do_div3;
14051 case M_DREM_3:
14052 dbl = 1;
1a0670f3 14053 /* Fall through. */
252b5132
RH
14054 case M_REM_3:
14055 s = "mfhi";
14056 do_div3:
7d10b47d 14057 start_noreorder ();
d8722d76 14058 macro_build (NULL, dbl ? "ddiv" : "div", ".,x,y", op[1], op[2]);
252b5132 14059 expr1.X_add_number = 2;
c0ebe874 14060 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 14061 macro_build (NULL, "break", "6", 7);
bdaaa2e1 14062
252b5132
RH
14063 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
14064 since that causes an overflow. We should do that as well,
14065 but I don't see how to do the comparisons without a temporary
14066 register. */
7d10b47d 14067 end_noreorder ();
c0ebe874 14068 macro_build (NULL, s, "x", op[0]);
252b5132
RH
14069 break;
14070
14071 case M_DIVU_3:
14072 s = "divu";
14073 s2 = "mflo";
14074 goto do_divu3;
14075 case M_REMU_3:
14076 s = "divu";
14077 s2 = "mfhi";
14078 goto do_divu3;
14079 case M_DDIVU_3:
14080 s = "ddivu";
14081 s2 = "mflo";
14082 goto do_divu3;
14083 case M_DREMU_3:
14084 s = "ddivu";
14085 s2 = "mfhi";
14086 do_divu3:
7d10b47d 14087 start_noreorder ();
d8722d76 14088 macro_build (NULL, s, ".,x,y", op[1], op[2]);
252b5132 14089 expr1.X_add_number = 2;
c0ebe874 14090 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 14091 macro_build (NULL, "break", "6", 7);
7d10b47d 14092 end_noreorder ();
c0ebe874 14093 macro_build (NULL, s2, "x", op[0]);
252b5132
RH
14094 break;
14095
14096 case M_DMUL:
14097 dbl = 1;
1a0670f3 14098 /* Fall through. */
252b5132 14099 case M_MUL:
c0ebe874
RS
14100 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", op[1], op[2]);
14101 macro_build (NULL, "mflo", "x", op[0]);
8fc2e39e 14102 break;
252b5132
RH
14103
14104 case M_DSUBU_I:
14105 dbl = 1;
14106 goto do_subu;
14107 case M_SUBU_I:
14108 do_subu:
252b5132 14109 imm_expr.X_add_number = -imm_expr.X_add_number;
d8722d76 14110 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,F", op[0], op[1]);
252b5132
RH
14111 break;
14112
14113 case M_SUBU_I_2:
252b5132 14114 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 14115 macro_build (&imm_expr, "addiu", "x,k", op[0]);
252b5132
RH
14116 break;
14117
14118 case M_DSUBU_I_2:
252b5132 14119 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 14120 macro_build (&imm_expr, "daddiu", "y,j", op[0]);
252b5132
RH
14121 break;
14122
14123 case M_BEQ:
14124 s = "cmp";
14125 s2 = "bteqz";
14126 goto do_branch;
14127 case M_BNE:
14128 s = "cmp";
14129 s2 = "btnez";
14130 goto do_branch;
14131 case M_BLT:
14132 s = "slt";
14133 s2 = "btnez";
14134 goto do_branch;
14135 case M_BLTU:
14136 s = "sltu";
14137 s2 = "btnez";
14138 goto do_branch;
14139 case M_BLE:
14140 s = "slt";
14141 s2 = "bteqz";
14142 goto do_reverse_branch;
14143 case M_BLEU:
14144 s = "sltu";
14145 s2 = "bteqz";
14146 goto do_reverse_branch;
14147 case M_BGE:
14148 s = "slt";
14149 s2 = "bteqz";
14150 goto do_branch;
14151 case M_BGEU:
14152 s = "sltu";
14153 s2 = "bteqz";
14154 goto do_branch;
14155 case M_BGT:
14156 s = "slt";
14157 s2 = "btnez";
14158 goto do_reverse_branch;
14159 case M_BGTU:
14160 s = "sltu";
14161 s2 = "btnez";
14162
14163 do_reverse_branch:
c0ebe874
RS
14164 tmp = op[1];
14165 op[1] = op[0];
14166 op[0] = tmp;
252b5132
RH
14167
14168 do_branch:
c0ebe874 14169 macro_build (NULL, s, "x,y", op[0], op[1]);
67c0d1eb 14170 macro_build (&offset_expr, s2, "p");
252b5132
RH
14171 break;
14172
14173 case M_BEQ_I:
14174 s = "cmpi";
14175 s2 = "bteqz";
14176 s3 = "x,U";
14177 goto do_branch_i;
14178 case M_BNE_I:
14179 s = "cmpi";
14180 s2 = "btnez";
14181 s3 = "x,U";
14182 goto do_branch_i;
14183 case M_BLT_I:
14184 s = "slti";
14185 s2 = "btnez";
14186 s3 = "x,8";
14187 goto do_branch_i;
14188 case M_BLTU_I:
14189 s = "sltiu";
14190 s2 = "btnez";
14191 s3 = "x,8";
14192 goto do_branch_i;
14193 case M_BLE_I:
14194 s = "slti";
14195 s2 = "btnez";
14196 s3 = "x,8";
14197 goto do_addone_branch_i;
14198 case M_BLEU_I:
14199 s = "sltiu";
14200 s2 = "btnez";
14201 s3 = "x,8";
14202 goto do_addone_branch_i;
14203 case M_BGE_I:
14204 s = "slti";
14205 s2 = "bteqz";
14206 s3 = "x,8";
14207 goto do_branch_i;
14208 case M_BGEU_I:
14209 s = "sltiu";
14210 s2 = "bteqz";
14211 s3 = "x,8";
14212 goto do_branch_i;
14213 case M_BGT_I:
14214 s = "slti";
14215 s2 = "bteqz";
14216 s3 = "x,8";
14217 goto do_addone_branch_i;
14218 case M_BGTU_I:
14219 s = "sltiu";
14220 s2 = "bteqz";
14221 s3 = "x,8";
14222
14223 do_addone_branch_i:
252b5132
RH
14224 ++imm_expr.X_add_number;
14225
14226 do_branch_i:
c0ebe874 14227 macro_build (&imm_expr, s, s3, op[0]);
67c0d1eb 14228 macro_build (&offset_expr, s2, "p");
252b5132
RH
14229 break;
14230
14231 case M_ABS:
14232 expr1.X_add_number = 0;
c0ebe874
RS
14233 macro_build (&expr1, "slti", "x,8", op[1]);
14234 if (op[0] != op[1])
14235 macro_build (NULL, "move", "y,X", op[0], mips16_to_32_reg_map[op[1]]);
252b5132 14236 expr1.X_add_number = 2;
67c0d1eb 14237 macro_build (&expr1, "bteqz", "p");
c0ebe874 14238 macro_build (NULL, "neg", "x,w", op[0], op[0]);
0acfaea6 14239 break;
252b5132
RH
14240 }
14241}
14242
14daeee3
RS
14243/* Look up instruction [START, START + LENGTH) in HASH. Record any extra
14244 opcode bits in *OPCODE_EXTRA. */
14245
14246static struct mips_opcode *
629310ab 14247mips_lookup_insn (htab_t hash, const char *start,
da8bca91 14248 ssize_t length, unsigned int *opcode_extra)
14daeee3
RS
14249{
14250 char *name, *dot, *p;
14251 unsigned int mask, suffix;
da8bca91 14252 ssize_t opend;
14daeee3
RS
14253 struct mips_opcode *insn;
14254
14255 /* Make a copy of the instruction so that we can fiddle with it. */
4ec9d7d5 14256 name = xstrndup (start, length);
14daeee3
RS
14257
14258 /* Look up the instruction as-is. */
629310ab 14259 insn = (struct mips_opcode *) str_hash_find (hash, name);
ee5734f0 14260 if (insn)
e1fa0163 14261 goto end;
14daeee3
RS
14262
14263 dot = strchr (name, '.');
14264 if (dot && dot[1])
14265 {
14266 /* Try to interpret the text after the dot as a VU0 channel suffix. */
14267 p = mips_parse_vu0_channels (dot + 1, &mask);
14268 if (*p == 0 && mask != 0)
14269 {
14270 *dot = 0;
629310ab 14271 insn = (struct mips_opcode *) str_hash_find (hash, name);
14daeee3
RS
14272 *dot = '.';
14273 if (insn && (insn->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0)
14274 {
14275 *opcode_extra |= mask << mips_vu0_channel_mask.lsb;
e1fa0163 14276 goto end;
14daeee3
RS
14277 }
14278 }
14279 }
14280
14281 if (mips_opts.micromips)
14282 {
14283 /* See if there's an instruction size override suffix,
14284 either `16' or `32', at the end of the mnemonic proper,
14285 that defines the operation, i.e. before the first `.'
14286 character if any. Strip it and retry. */
14287 opend = dot != NULL ? dot - name : length;
14288 if (opend >= 3 && name[opend - 2] == '1' && name[opend - 1] == '6')
14289 suffix = 2;
3076e594 14290 else if (opend >= 2 && name[opend - 2] == '3' && name[opend - 1] == '2')
14daeee3
RS
14291 suffix = 4;
14292 else
14293 suffix = 0;
14294 if (suffix)
14295 {
39334a61 14296 memmove (name + opend - 2, name + opend, length - opend + 1);
629310ab 14297 insn = (struct mips_opcode *) str_hash_find (hash, name);
ee5734f0 14298 if (insn)
14daeee3
RS
14299 {
14300 forced_insn_length = suffix;
e1fa0163 14301 goto end;
14daeee3
RS
14302 }
14303 }
14304 }
14305
e1fa0163
NC
14306 insn = NULL;
14307 end:
14308 free (name);
14309 return insn;
14daeee3
RS
14310}
14311
77bd4346 14312/* Assemble an instruction into its binary format. If the instruction
e423441d
RS
14313 is a macro, set imm_expr and offset_expr to the values associated
14314 with "I" and "A" operands respectively. Otherwise store the value
14315 of the relocatable field (if any) in offset_expr. In both cases
14316 set offset_reloc to the relocation operators applied to offset_expr. */
252b5132
RH
14317
14318static void
60f20e8b 14319mips_ip (char *str, struct mips_cl_insn *insn)
252b5132 14320{
60f20e8b 14321 const struct mips_opcode *first, *past;
629310ab 14322 htab_t hash;
a92713e6 14323 char format;
14daeee3 14324 size_t end;
a92713e6 14325 struct mips_operand_token *tokens;
14daeee3 14326 unsigned int opcode_extra;
252b5132 14327
df58fc94
RS
14328 if (mips_opts.micromips)
14329 {
14330 hash = micromips_op_hash;
14331 past = &micromips_opcodes[bfd_micromips_num_opcodes];
14332 }
14333 else
14334 {
14335 hash = op_hash;
14336 past = &mips_opcodes[NUMOPCODES];
14337 }
14338 forced_insn_length = 0;
14daeee3 14339 opcode_extra = 0;
252b5132 14340
df58fc94 14341 /* We first try to match an instruction up to a space or to the end. */
a40bc9dd
RS
14342 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
14343 continue;
bdaaa2e1 14344
60f20e8b
RS
14345 first = mips_lookup_insn (hash, str, end, &opcode_extra);
14346 if (first == NULL)
252b5132 14347 {
1661c76c 14348 set_insn_error (0, _("unrecognized opcode"));
a40bc9dd 14349 return;
252b5132
RH
14350 }
14351
60f20e8b 14352 if (strcmp (first->name, "li.s") == 0)
a92713e6 14353 format = 'f';
60f20e8b 14354 else if (strcmp (first->name, "li.d") == 0)
a92713e6
RS
14355 format = 'd';
14356 else
14357 format = 0;
14358 tokens = mips_parse_arguments (str + end, format);
14359 if (!tokens)
14360 return;
14361
60f20e8b
RS
14362 if (!match_insns (insn, first, past, tokens, opcode_extra, FALSE)
14363 && !match_insns (insn, first, past, tokens, opcode_extra, TRUE))
1661c76c 14364 set_insn_error (0, _("invalid operands"));
df58fc94 14365
e3de51ce 14366 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
14367}
14368
77bd4346
RS
14369/* As for mips_ip, but used when assembling MIPS16 code.
14370 Also set forced_insn_length to the resulting instruction size in
14371 bytes if the user explicitly requested a small or extended instruction. */
252b5132
RH
14372
14373static void
60f20e8b 14374mips16_ip (char *str, struct mips_cl_insn *insn)
252b5132 14375{
1a00e612 14376 char *end, *s, c;
60f20e8b 14377 struct mips_opcode *first;
a92713e6 14378 struct mips_operand_token *tokens;
3fb49709 14379 unsigned int l;
252b5132 14380
25499ac7 14381 for (s = str; *s != '\0' && *s != '.' && *s != ' '; ++s)
252b5132 14382 ;
1a00e612
RS
14383 end = s;
14384 c = *end;
3fb49709
MR
14385
14386 l = 0;
1a00e612 14387 switch (c)
252b5132
RH
14388 {
14389 case '\0':
14390 break;
14391
14392 case ' ':
1a00e612 14393 s++;
252b5132
RH
14394 break;
14395
14396 case '.':
3fb49709
MR
14397 s++;
14398 if (*s == 't')
252b5132 14399 {
3fb49709
MR
14400 l = 2;
14401 s++;
252b5132 14402 }
3fb49709 14403 else if (*s == 'e')
252b5132 14404 {
3fb49709
MR
14405 l = 4;
14406 s++;
252b5132 14407 }
3fb49709
MR
14408 if (*s == '\0')
14409 break;
14410 else if (*s++ == ' ')
14411 break;
1661c76c 14412 set_insn_error (0, _("unrecognized opcode"));
252b5132
RH
14413 return;
14414 }
3fb49709 14415 forced_insn_length = l;
252b5132 14416
1a00e612 14417 *end = 0;
629310ab 14418 first = (struct mips_opcode *) str_hash_find (mips16_op_hash, str);
1a00e612
RS
14419 *end = c;
14420
60f20e8b 14421 if (!first)
252b5132 14422 {
1661c76c 14423 set_insn_error (0, _("unrecognized opcode"));
252b5132
RH
14424 return;
14425 }
14426
a92713e6
RS
14427 tokens = mips_parse_arguments (s, 0);
14428 if (!tokens)
14429 return;
14430
60f20e8b 14431 if (!match_mips16_insns (insn, first, tokens))
1661c76c 14432 set_insn_error (0, _("invalid operands"));
252b5132 14433
e3de51ce 14434 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
14435}
14436
b886a2ab
RS
14437/* Marshal immediate value VAL for an extended MIPS16 instruction.
14438 NBITS is the number of significant bits in VAL. */
14439
14440static unsigned long
14441mips16_immed_extend (offsetT val, unsigned int nbits)
14442{
14443 int extval;
25499ac7
MR
14444
14445 extval = 0;
14446 val &= (1U << nbits) - 1;
14447 if (nbits == 16 || nbits == 9)
b886a2ab
RS
14448 {
14449 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14450 val &= 0x1f;
14451 }
14452 else if (nbits == 15)
14453 {
14454 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14455 val &= 0xf;
14456 }
25499ac7 14457 else if (nbits == 6)
b886a2ab
RS
14458 {
14459 extval = ((val & 0x1f) << 6) | (val & 0x20);
14460 val = 0;
14461 }
14462 return (extval << 16) | val;
14463}
14464
3ccad066
RS
14465/* Like decode_mips16_operand, but require the operand to be defined and
14466 require it to be an integer. */
14467
14468static const struct mips_int_operand *
14469mips16_immed_operand (int type, bfd_boolean extended_p)
14470{
14471 const struct mips_operand *operand;
14472
14473 operand = decode_mips16_operand (type, extended_p);
14474 if (!operand || (operand->type != OP_INT && operand->type != OP_PCREL))
14475 abort ();
14476 return (const struct mips_int_operand *) operand;
14477}
14478
14479/* Return true if SVAL fits OPERAND. RELOC is as for mips16_immed. */
14480
14481static bfd_boolean
14482mips16_immed_in_range_p (const struct mips_int_operand *operand,
14483 bfd_reloc_code_real_type reloc, offsetT sval)
14484{
14485 int min_val, max_val;
14486
14487 min_val = mips_int_operand_min (operand);
14488 max_val = mips_int_operand_max (operand);
14489 if (reloc != BFD_RELOC_UNUSED)
14490 {
14491 if (min_val < 0)
14492 sval = SEXT_16BIT (sval);
14493 else
14494 sval &= 0xffff;
14495 }
14496
14497 return (sval >= min_val
14498 && sval <= max_val
14499 && (sval & ((1 << operand->shift) - 1)) == 0);
14500}
14501
5c04167a
RS
14502/* Install immediate value VAL into MIPS16 instruction *INSN,
14503 extending it if necessary. The instruction in *INSN may
14504 already be extended.
14505
43c0598f
RS
14506 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14507 if none. In the former case, VAL is a 16-bit number with no
14508 defined signedness.
14509
14510 TYPE is the type of the immediate field. USER_INSN_LENGTH
14511 is the length that the user requested, or 0 if none. */
252b5132
RH
14512
14513static void
3b4dbbbf 14514mips16_immed (const char *file, unsigned int line, int type,
43c0598f 14515 bfd_reloc_code_real_type reloc, offsetT val,
5c04167a 14516 unsigned int user_insn_length, unsigned long *insn)
252b5132 14517{
3ccad066
RS
14518 const struct mips_int_operand *operand;
14519 unsigned int uval, length;
252b5132 14520
3ccad066
RS
14521 operand = mips16_immed_operand (type, FALSE);
14522 if (!mips16_immed_in_range_p (operand, reloc, val))
5c04167a
RS
14523 {
14524 /* We need an extended instruction. */
14525 if (user_insn_length == 2)
14526 as_bad_where (file, line, _("invalid unextended operand value"));
14527 else
14528 *insn |= MIPS16_EXTEND;
14529 }
14530 else if (user_insn_length == 4)
14531 {
14532 /* The operand doesn't force an unextended instruction to be extended.
14533 Warn if the user wanted an extended instruction anyway. */
14534 *insn |= MIPS16_EXTEND;
14535 as_warn_where (file, line,
14536 _("extended operand requested but not required"));
14537 }
252b5132 14538
3ccad066
RS
14539 length = mips16_opcode_length (*insn);
14540 if (length == 4)
252b5132 14541 {
3ccad066
RS
14542 operand = mips16_immed_operand (type, TRUE);
14543 if (!mips16_immed_in_range_p (operand, reloc, val))
14544 as_bad_where (file, line,
14545 _("operand value out of range for instruction"));
252b5132 14546 }
3ccad066 14547 uval = ((unsigned int) val >> operand->shift) - operand->bias;
bdd15286 14548 if (length == 2 || operand->root.lsb != 0)
3ccad066 14549 *insn = mips_insert_operand (&operand->root, *insn, uval);
252b5132 14550 else
3ccad066 14551 *insn |= mips16_immed_extend (uval, operand->root.size);
252b5132
RH
14552}
14553\f
d6f16593 14554struct percent_op_match
ad8d3bb3 14555{
5e0116d5
RS
14556 const char *str;
14557 bfd_reloc_code_real_type reloc;
d6f16593
MR
14558};
14559
14560static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 14561{
5e0116d5 14562 {"%lo", BFD_RELOC_LO16},
5e0116d5
RS
14563 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14564 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14565 {"%call16", BFD_RELOC_MIPS_CALL16},
14566 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14567 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14568 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14569 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14570 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14571 {"%got", BFD_RELOC_MIPS_GOT16},
14572 {"%gp_rel", BFD_RELOC_GPREL16},
be3f1006 14573 {"%gprel", BFD_RELOC_GPREL16},
5e0116d5
RS
14574 {"%half", BFD_RELOC_16},
14575 {"%highest", BFD_RELOC_MIPS_HIGHEST},
14576 {"%higher", BFD_RELOC_MIPS_HIGHER},
14577 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
14578 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14579 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14580 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14581 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14582 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14583 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14584 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
7361da2c
AB
14585 {"%hi", BFD_RELOC_HI16_S},
14586 {"%pcrel_hi", BFD_RELOC_HI16_S_PCREL},
14587 {"%pcrel_lo", BFD_RELOC_LO16_PCREL}
ad8d3bb3
TS
14588};
14589
d6f16593
MR
14590static const struct percent_op_match mips16_percent_op[] =
14591{
14592 {"%lo", BFD_RELOC_MIPS16_LO16},
be3f1006 14593 {"%gp_rel", BFD_RELOC_MIPS16_GPREL},
d6f16593 14594 {"%gprel", BFD_RELOC_MIPS16_GPREL},
738e5348
RS
14595 {"%got", BFD_RELOC_MIPS16_GOT16},
14596 {"%call16", BFD_RELOC_MIPS16_CALL16},
d0f13682
CLT
14597 {"%hi", BFD_RELOC_MIPS16_HI16_S},
14598 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14599 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14600 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14601 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14602 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14603 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14604 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
d6f16593
MR
14605};
14606
252b5132 14607
5e0116d5
RS
14608/* Return true if *STR points to a relocation operator. When returning true,
14609 move *STR over the operator and store its relocation code in *RELOC.
14610 Leave both *STR and *RELOC alone when returning false. */
14611
14612static bfd_boolean
17a2f251 14613parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 14614{
d6f16593
MR
14615 const struct percent_op_match *percent_op;
14616 size_t limit, i;
14617
14618 if (mips_opts.mips16)
14619 {
14620 percent_op = mips16_percent_op;
14621 limit = ARRAY_SIZE (mips16_percent_op);
14622 }
14623 else
14624 {
14625 percent_op = mips_percent_op;
14626 limit = ARRAY_SIZE (mips_percent_op);
14627 }
76b3015f 14628
d6f16593 14629 for (i = 0; i < limit; i++)
5e0116d5 14630 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 14631 {
3f98094e
DJ
14632 int len = strlen (percent_op[i].str);
14633
14634 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14635 continue;
14636
5e0116d5
RS
14637 *str += strlen (percent_op[i].str);
14638 *reloc = percent_op[i].reloc;
394f9b3a 14639
5e0116d5
RS
14640 /* Check whether the output BFD supports this relocation.
14641 If not, issue an error and fall back on something safe. */
14642 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 14643 {
20203fb9 14644 as_bad (_("relocation %s isn't supported by the current ABI"),
5e0116d5 14645 percent_op[i].str);
01a3f561 14646 *reloc = BFD_RELOC_UNUSED;
394f9b3a 14647 }
5e0116d5 14648 return TRUE;
394f9b3a 14649 }
5e0116d5 14650 return FALSE;
394f9b3a 14651}
ad8d3bb3 14652
ad8d3bb3 14653
5e0116d5
RS
14654/* Parse string STR as a 16-bit relocatable operand. Store the
14655 expression in *EP and the relocations in the array starting
14656 at RELOC. Return the number of relocation operators used.
ad8d3bb3 14657
01a3f561 14658 On exit, EXPR_END points to the first character after the expression. */
ad8d3bb3 14659
5e0116d5 14660static size_t
17a2f251
TS
14661my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14662 char *str)
ad8d3bb3 14663{
5e0116d5
RS
14664 bfd_reloc_code_real_type reversed_reloc[3];
14665 size_t reloc_index, i;
09b8f35a
RS
14666 int crux_depth, str_depth;
14667 char *crux;
5e0116d5
RS
14668
14669 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
14670 in REVERSED_RELOC. End the loop with CRUX pointing to the start
14671 of the main expression and with CRUX_DEPTH containing the number
14672 of open brackets at that point. */
14673 reloc_index = -1;
14674 str_depth = 0;
14675 do
fb1b3232 14676 {
09b8f35a
RS
14677 reloc_index++;
14678 crux = str;
14679 crux_depth = str_depth;
14680
14681 /* Skip over whitespace and brackets, keeping count of the number
14682 of brackets. */
14683 while (*str == ' ' || *str == '\t' || *str == '(')
14684 if (*str++ == '(')
14685 str_depth++;
5e0116d5 14686 }
09b8f35a
RS
14687 while (*str == '%'
14688 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14689 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 14690
09b8f35a 14691 my_getExpression (ep, crux);
5e0116d5 14692 str = expr_end;
394f9b3a 14693
5e0116d5 14694 /* Match every open bracket. */
09b8f35a 14695 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 14696 if (*str++ == ')')
09b8f35a 14697 crux_depth--;
394f9b3a 14698
09b8f35a 14699 if (crux_depth > 0)
20203fb9 14700 as_bad (_("unclosed '('"));
394f9b3a 14701
5e0116d5 14702 expr_end = str;
252b5132 14703
ec4fcab0
MR
14704 for (i = 0; i < reloc_index; i++)
14705 reloc[i] = reversed_reloc[reloc_index - 1 - i];
fb1b3232 14706
5e0116d5 14707 return reloc_index;
252b5132
RH
14708}
14709
14710static void
17a2f251 14711my_getExpression (expressionS *ep, char *str)
252b5132
RH
14712{
14713 char *save_in;
14714
14715 save_in = input_line_pointer;
14716 input_line_pointer = str;
14717 expression (ep);
14718 expr_end = input_line_pointer;
14719 input_line_pointer = save_in;
252b5132
RH
14720}
14721
6d4af3c2 14722const char *
17a2f251 14723md_atof (int type, char *litP, int *sizeP)
252b5132 14724{
499ac353 14725 return ieee_md_atof (type, litP, sizeP, target_big_endian);
252b5132
RH
14726}
14727
14728void
17a2f251 14729md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
14730{
14731 if (target_big_endian)
14732 number_to_chars_bigendian (buf, val, n);
14733 else
14734 number_to_chars_littleendian (buf, val, n);
14735}
14736\f
e013f690
TS
14737static int support_64bit_objects(void)
14738{
14739 const char **list, **l;
aa3d8fdf 14740 int yes;
e013f690
TS
14741
14742 list = bfd_target_list ();
14743 for (l = list; *l != NULL; l++)
aeffff67
RS
14744 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14745 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
e013f690 14746 break;
aa3d8fdf 14747 yes = (*l != NULL);
e013f690 14748 free (list);
aa3d8fdf 14749 return yes;
e013f690
TS
14750}
14751
316f5878
RS
14752/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14753 NEW_VALUE. Warn if another value was already specified. Note:
14754 we have to defer parsing the -march and -mtune arguments in order
14755 to handle 'from-abi' correctly, since the ABI might be specified
14756 in a later argument. */
14757
14758static void
17a2f251 14759mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
14760{
14761 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
1661c76c 14762 as_warn (_("a different %s was already specified, is now %s"),
316f5878
RS
14763 string_ptr == &mips_arch_string ? "-march" : "-mtune",
14764 new_value);
14765
14766 *string_ptr = new_value;
14767}
14768
252b5132 14769int
17b9d67d 14770md_parse_option (int c, const char *arg)
252b5132 14771{
c6278170
RS
14772 unsigned int i;
14773
14774 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
14775 if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
14776 {
919731af 14777 file_ase_explicit |= mips_set_ase (&mips_ases[i], &file_mips_opts,
c6278170
RS
14778 c == mips_ases[i].option_on);
14779 return 1;
14780 }
14781
252b5132
RH
14782 switch (c)
14783 {
119d663a
NC
14784 case OPTION_CONSTRUCT_FLOATS:
14785 mips_disable_float_construction = 0;
14786 break;
bdaaa2e1 14787
119d663a
NC
14788 case OPTION_NO_CONSTRUCT_FLOATS:
14789 mips_disable_float_construction = 1;
14790 break;
bdaaa2e1 14791
252b5132
RH
14792 case OPTION_TRAP:
14793 mips_trap = 1;
14794 break;
14795
14796 case OPTION_BREAK:
14797 mips_trap = 0;
14798 break;
14799
14800 case OPTION_EB:
14801 target_big_endian = 1;
14802 break;
14803
14804 case OPTION_EL:
14805 target_big_endian = 0;
14806 break;
14807
14808 case 'O':
4ffff32f
TS
14809 if (arg == NULL)
14810 mips_optimize = 1;
14811 else if (arg[0] == '0')
14812 mips_optimize = 0;
14813 else if (arg[0] == '1')
252b5132
RH
14814 mips_optimize = 1;
14815 else
14816 mips_optimize = 2;
14817 break;
14818
14819 case 'g':
14820 if (arg == NULL)
14821 mips_debug = 2;
14822 else
14823 mips_debug = atoi (arg);
252b5132
RH
14824 break;
14825
14826 case OPTION_MIPS1:
0b35dfee 14827 file_mips_opts.isa = ISA_MIPS1;
252b5132
RH
14828 break;
14829
14830 case OPTION_MIPS2:
0b35dfee 14831 file_mips_opts.isa = ISA_MIPS2;
252b5132
RH
14832 break;
14833
14834 case OPTION_MIPS3:
0b35dfee 14835 file_mips_opts.isa = ISA_MIPS3;
252b5132
RH
14836 break;
14837
14838 case OPTION_MIPS4:
0b35dfee 14839 file_mips_opts.isa = ISA_MIPS4;
e7af610e
NC
14840 break;
14841
84ea6cf2 14842 case OPTION_MIPS5:
0b35dfee 14843 file_mips_opts.isa = ISA_MIPS5;
84ea6cf2
NC
14844 break;
14845
e7af610e 14846 case OPTION_MIPS32:
0b35dfee 14847 file_mips_opts.isa = ISA_MIPS32;
252b5132
RH
14848 break;
14849
af7ee8bf 14850 case OPTION_MIPS32R2:
0b35dfee 14851 file_mips_opts.isa = ISA_MIPS32R2;
af7ee8bf
CD
14852 break;
14853
ae52f483 14854 case OPTION_MIPS32R3:
0ae19f05 14855 file_mips_opts.isa = ISA_MIPS32R3;
ae52f483
AB
14856 break;
14857
14858 case OPTION_MIPS32R5:
0ae19f05 14859 file_mips_opts.isa = ISA_MIPS32R5;
ae52f483
AB
14860 break;
14861
7361da2c
AB
14862 case OPTION_MIPS32R6:
14863 file_mips_opts.isa = ISA_MIPS32R6;
14864 break;
14865
5f74bc13 14866 case OPTION_MIPS64R2:
0b35dfee 14867 file_mips_opts.isa = ISA_MIPS64R2;
5f74bc13
CD
14868 break;
14869
ae52f483 14870 case OPTION_MIPS64R3:
0ae19f05 14871 file_mips_opts.isa = ISA_MIPS64R3;
ae52f483
AB
14872 break;
14873
14874 case OPTION_MIPS64R5:
0ae19f05 14875 file_mips_opts.isa = ISA_MIPS64R5;
ae52f483
AB
14876 break;
14877
7361da2c
AB
14878 case OPTION_MIPS64R6:
14879 file_mips_opts.isa = ISA_MIPS64R6;
14880 break;
14881
84ea6cf2 14882 case OPTION_MIPS64:
0b35dfee 14883 file_mips_opts.isa = ISA_MIPS64;
84ea6cf2
NC
14884 break;
14885
ec68c924 14886 case OPTION_MTUNE:
316f5878
RS
14887 mips_set_option_string (&mips_tune_string, arg);
14888 break;
ec68c924 14889
316f5878
RS
14890 case OPTION_MARCH:
14891 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
14892 break;
14893
14894 case OPTION_M4650:
316f5878
RS
14895 mips_set_option_string (&mips_arch_string, "4650");
14896 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
14897 break;
14898
14899 case OPTION_NO_M4650:
14900 break;
14901
14902 case OPTION_M4010:
316f5878
RS
14903 mips_set_option_string (&mips_arch_string, "4010");
14904 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
14905 break;
14906
14907 case OPTION_NO_M4010:
14908 break;
14909
14910 case OPTION_M4100:
316f5878
RS
14911 mips_set_option_string (&mips_arch_string, "4100");
14912 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
14913 break;
14914
14915 case OPTION_NO_M4100:
14916 break;
14917
252b5132 14918 case OPTION_M3900:
316f5878
RS
14919 mips_set_option_string (&mips_arch_string, "3900");
14920 mips_set_option_string (&mips_tune_string, "3900");
252b5132 14921 break;
bdaaa2e1 14922
252b5132
RH
14923 case OPTION_NO_M3900:
14924 break;
14925
df58fc94 14926 case OPTION_MICROMIPS:
919731af 14927 if (file_mips_opts.mips16 == 1)
df58fc94
RS
14928 {
14929 as_bad (_("-mmicromips cannot be used with -mips16"));
14930 return 0;
14931 }
919731af 14932 file_mips_opts.micromips = 1;
df58fc94
RS
14933 mips_no_prev_insn ();
14934 break;
14935
14936 case OPTION_NO_MICROMIPS:
919731af 14937 file_mips_opts.micromips = 0;
df58fc94
RS
14938 mips_no_prev_insn ();
14939 break;
14940
252b5132 14941 case OPTION_MIPS16:
919731af 14942 if (file_mips_opts.micromips == 1)
df58fc94
RS
14943 {
14944 as_bad (_("-mips16 cannot be used with -micromips"));
14945 return 0;
14946 }
919731af 14947 file_mips_opts.mips16 = 1;
7d10b47d 14948 mips_no_prev_insn ();
252b5132
RH
14949 break;
14950
14951 case OPTION_NO_MIPS16:
919731af 14952 file_mips_opts.mips16 = 0;
7d10b47d 14953 mips_no_prev_insn ();
252b5132
RH
14954 break;
14955
6a32d874
CM
14956 case OPTION_FIX_24K:
14957 mips_fix_24k = 1;
14958 break;
14959
14960 case OPTION_NO_FIX_24K:
14961 mips_fix_24k = 0;
14962 break;
14963
a8d14a88
CM
14964 case OPTION_FIX_RM7000:
14965 mips_fix_rm7000 = 1;
14966 break;
14967
14968 case OPTION_NO_FIX_RM7000:
14969 mips_fix_rm7000 = 0;
14970 break;
14971
6f2117ba
PH
14972 case OPTION_FIX_LOONGSON3_LLSC:
14973 mips_fix_loongson3_llsc = TRUE;
14974 break;
14975
14976 case OPTION_NO_FIX_LOONGSON3_LLSC:
14977 mips_fix_loongson3_llsc = FALSE;
14978 break;
14979
c67a084a
NC
14980 case OPTION_FIX_LOONGSON2F_JUMP:
14981 mips_fix_loongson2f_jump = TRUE;
14982 break;
14983
14984 case OPTION_NO_FIX_LOONGSON2F_JUMP:
14985 mips_fix_loongson2f_jump = FALSE;
14986 break;
14987
14988 case OPTION_FIX_LOONGSON2F_NOP:
14989 mips_fix_loongson2f_nop = TRUE;
14990 break;
14991
14992 case OPTION_NO_FIX_LOONGSON2F_NOP:
14993 mips_fix_loongson2f_nop = FALSE;
14994 break;
14995
d766e8ec
RS
14996 case OPTION_FIX_VR4120:
14997 mips_fix_vr4120 = 1;
60b63b72
RS
14998 break;
14999
d766e8ec
RS
15000 case OPTION_NO_FIX_VR4120:
15001 mips_fix_vr4120 = 0;
60b63b72
RS
15002 break;
15003
7d8e00cf
RS
15004 case OPTION_FIX_VR4130:
15005 mips_fix_vr4130 = 1;
15006 break;
15007
15008 case OPTION_NO_FIX_VR4130:
15009 mips_fix_vr4130 = 0;
15010 break;
15011
d954098f
DD
15012 case OPTION_FIX_CN63XXP1:
15013 mips_fix_cn63xxp1 = TRUE;
15014 break;
15015
15016 case OPTION_NO_FIX_CN63XXP1:
15017 mips_fix_cn63xxp1 = FALSE;
15018 break;
15019
27c634e0
FN
15020 case OPTION_FIX_R5900:
15021 mips_fix_r5900 = TRUE;
15022 mips_fix_r5900_explicit = TRUE;
15023 break;
15024
15025 case OPTION_NO_FIX_R5900:
15026 mips_fix_r5900 = FALSE;
15027 mips_fix_r5900_explicit = TRUE;
15028 break;
15029
4a6a3df4
AO
15030 case OPTION_RELAX_BRANCH:
15031 mips_relax_branch = 1;
15032 break;
15033
15034 case OPTION_NO_RELAX_BRANCH:
15035 mips_relax_branch = 0;
15036 break;
15037
8b10b0b3
MR
15038 case OPTION_IGNORE_BRANCH_ISA:
15039 mips_ignore_branch_isa = TRUE;
15040 break;
15041
15042 case OPTION_NO_IGNORE_BRANCH_ISA:
15043 mips_ignore_branch_isa = FALSE;
15044 break;
15045
833794fc 15046 case OPTION_INSN32:
919731af 15047 file_mips_opts.insn32 = TRUE;
833794fc
MR
15048 break;
15049
15050 case OPTION_NO_INSN32:
919731af 15051 file_mips_opts.insn32 = FALSE;
833794fc
MR
15052 break;
15053
aa6975fb
ILT
15054 case OPTION_MSHARED:
15055 mips_in_shared = TRUE;
15056 break;
15057
15058 case OPTION_MNO_SHARED:
15059 mips_in_shared = FALSE;
15060 break;
15061
aed1a261 15062 case OPTION_MSYM32:
919731af 15063 file_mips_opts.sym32 = TRUE;
aed1a261
RS
15064 break;
15065
15066 case OPTION_MNO_SYM32:
919731af 15067 file_mips_opts.sym32 = FALSE;
aed1a261
RS
15068 break;
15069
252b5132
RH
15070 /* When generating ELF code, we permit -KPIC and -call_shared to
15071 select SVR4_PIC, and -non_shared to select no PIC. This is
15072 intended to be compatible with Irix 5. */
15073 case OPTION_CALL_SHARED:
252b5132 15074 mips_pic = SVR4_PIC;
143d77c5 15075 mips_abicalls = TRUE;
252b5132
RH
15076 break;
15077
861fb55a 15078 case OPTION_CALL_NONPIC:
861fb55a
DJ
15079 mips_pic = NO_PIC;
15080 mips_abicalls = TRUE;
15081 break;
15082
252b5132 15083 case OPTION_NON_SHARED:
252b5132 15084 mips_pic = NO_PIC;
143d77c5 15085 mips_abicalls = FALSE;
252b5132
RH
15086 break;
15087
44075ae2
TS
15088 /* The -xgot option tells the assembler to use 32 bit offsets
15089 when accessing the got in SVR4_PIC mode. It is for Irix
252b5132
RH
15090 compatibility. */
15091 case OPTION_XGOT:
15092 mips_big_got = 1;
15093 break;
15094
15095 case 'G':
6caf9ef4
TS
15096 g_switch_value = atoi (arg);
15097 g_switch_seen = 1;
252b5132
RH
15098 break;
15099
34ba82a8
TS
15100 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
15101 and -mabi=64. */
252b5132 15102 case OPTION_32:
f3ded42a 15103 mips_abi = O32_ABI;
252b5132
RH
15104 break;
15105
e013f690 15106 case OPTION_N32:
316f5878 15107 mips_abi = N32_ABI;
e013f690 15108 break;
252b5132 15109
e013f690 15110 case OPTION_64:
316f5878 15111 mips_abi = N64_ABI;
f43abd2b 15112 if (!support_64bit_objects())
1661c76c 15113 as_fatal (_("no compiled in support for 64 bit object file format"));
252b5132
RH
15114 break;
15115
c97ef257 15116 case OPTION_GP32:
bad1aba3 15117 file_mips_opts.gp = 32;
c97ef257
AH
15118 break;
15119
15120 case OPTION_GP64:
bad1aba3 15121 file_mips_opts.gp = 64;
c97ef257 15122 break;
252b5132 15123
ca4e0257 15124 case OPTION_FP32:
0b35dfee 15125 file_mips_opts.fp = 32;
316f5878
RS
15126 break;
15127
351cdf24
MF
15128 case OPTION_FPXX:
15129 file_mips_opts.fp = 0;
15130 break;
15131
316f5878 15132 case OPTION_FP64:
0b35dfee 15133 file_mips_opts.fp = 64;
ca4e0257
RS
15134 break;
15135
351cdf24
MF
15136 case OPTION_ODD_SPREG:
15137 file_mips_opts.oddspreg = 1;
15138 break;
15139
15140 case OPTION_NO_ODD_SPREG:
15141 file_mips_opts.oddspreg = 0;
15142 break;
15143
037b32b9 15144 case OPTION_SINGLE_FLOAT:
0b35dfee 15145 file_mips_opts.single_float = 1;
037b32b9
AN
15146 break;
15147
15148 case OPTION_DOUBLE_FLOAT:
0b35dfee 15149 file_mips_opts.single_float = 0;
037b32b9
AN
15150 break;
15151
15152 case OPTION_SOFT_FLOAT:
0b35dfee 15153 file_mips_opts.soft_float = 1;
037b32b9
AN
15154 break;
15155
15156 case OPTION_HARD_FLOAT:
0b35dfee 15157 file_mips_opts.soft_float = 0;
037b32b9
AN
15158 break;
15159
252b5132 15160 case OPTION_MABI:
e013f690 15161 if (strcmp (arg, "32") == 0)
316f5878 15162 mips_abi = O32_ABI;
e013f690 15163 else if (strcmp (arg, "o64") == 0)
316f5878 15164 mips_abi = O64_ABI;
e013f690 15165 else if (strcmp (arg, "n32") == 0)
316f5878 15166 mips_abi = N32_ABI;
e013f690
TS
15167 else if (strcmp (arg, "64") == 0)
15168 {
316f5878 15169 mips_abi = N64_ABI;
e013f690 15170 if (! support_64bit_objects())
1661c76c 15171 as_fatal (_("no compiled in support for 64 bit object file "
e013f690
TS
15172 "format"));
15173 }
15174 else if (strcmp (arg, "eabi") == 0)
316f5878 15175 mips_abi = EABI_ABI;
e013f690 15176 else
da0e507f
TS
15177 {
15178 as_fatal (_("invalid abi -mabi=%s"), arg);
15179 return 0;
15180 }
252b5132
RH
15181 break;
15182
6b76fefe 15183 case OPTION_M7000_HILO_FIX:
b34976b6 15184 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
15185 break;
15186
9ee72ff1 15187 case OPTION_MNO_7000_HILO_FIX:
b34976b6 15188 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
15189 break;
15190
ecb4347a 15191 case OPTION_MDEBUG:
b34976b6 15192 mips_flag_mdebug = TRUE;
ecb4347a
DJ
15193 break;
15194
15195 case OPTION_NO_MDEBUG:
b34976b6 15196 mips_flag_mdebug = FALSE;
ecb4347a 15197 break;
dcd410fe
RO
15198
15199 case OPTION_PDR:
15200 mips_flag_pdr = TRUE;
15201 break;
15202
15203 case OPTION_NO_PDR:
15204 mips_flag_pdr = FALSE;
15205 break;
0a44bf69
RS
15206
15207 case OPTION_MVXWORKS_PIC:
15208 mips_pic = VXWORKS_PIC;
15209 break;
ecb4347a 15210
ba92f887
MR
15211 case OPTION_NAN:
15212 if (strcmp (arg, "2008") == 0)
7361da2c 15213 mips_nan2008 = 1;
ba92f887 15214 else if (strcmp (arg, "legacy") == 0)
7361da2c 15215 mips_nan2008 = 0;
ba92f887
MR
15216 else
15217 {
1661c76c 15218 as_fatal (_("invalid NaN setting -mnan=%s"), arg);
ba92f887
MR
15219 return 0;
15220 }
15221 break;
15222
252b5132
RH
15223 default:
15224 return 0;
15225 }
15226
c67a084a
NC
15227 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
15228
252b5132
RH
15229 return 1;
15230}
316f5878 15231\f
919731af 15232/* Set up globals to tune for the ISA or processor described by INFO. */
252b5132 15233
316f5878 15234static void
17a2f251 15235mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
15236{
15237 if (info != 0)
fef14a42 15238 mips_tune = info->cpu;
316f5878 15239}
80cc45a5 15240
34ba82a8 15241
252b5132 15242void
17a2f251 15243mips_after_parse_args (void)
e9670677 15244{
fef14a42
TS
15245 const struct mips_cpu_info *arch_info = 0;
15246 const struct mips_cpu_info *tune_info = 0;
15247
6f2117ba 15248 /* GP relative stuff not working for PE. */
6caf9ef4 15249 if (strncmp (TARGET_OS, "pe", 2) == 0)
e9670677 15250 {
6caf9ef4 15251 if (g_switch_seen && g_switch_value != 0)
1661c76c 15252 as_bad (_("-G not supported in this configuration"));
e9670677
MR
15253 g_switch_value = 0;
15254 }
15255
cac012d6
AO
15256 if (mips_abi == NO_ABI)
15257 mips_abi = MIPS_DEFAULT_ABI;
15258
919731af 15259 /* The following code determines the architecture.
22923709
RS
15260 Similar code was added to GCC 3.3 (see override_options() in
15261 config/mips/mips.c). The GAS and GCC code should be kept in sync
15262 as much as possible. */
e9670677 15263
316f5878 15264 if (mips_arch_string != 0)
fef14a42 15265 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 15266
0b35dfee 15267 if (file_mips_opts.isa != ISA_UNKNOWN)
e9670677 15268 {
0b35dfee 15269 /* Handle -mipsN. At this point, file_mips_opts.isa contains the
fef14a42 15270 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 15271 the -march selection (if any). */
fef14a42 15272 if (arch_info != 0)
e9670677 15273 {
316f5878
RS
15274 /* -march takes precedence over -mipsN, since it is more descriptive.
15275 There's no harm in specifying both as long as the ISA levels
15276 are the same. */
0b35dfee 15277 if (file_mips_opts.isa != arch_info->isa)
1661c76c
RS
15278 as_bad (_("-%s conflicts with the other architecture options,"
15279 " which imply -%s"),
0b35dfee 15280 mips_cpu_info_from_isa (file_mips_opts.isa)->name,
fef14a42 15281 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 15282 }
316f5878 15283 else
0b35dfee 15284 arch_info = mips_cpu_info_from_isa (file_mips_opts.isa);
e9670677
MR
15285 }
15286
fef14a42 15287 if (arch_info == 0)
95bfe26e
MF
15288 {
15289 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
15290 gas_assert (arch_info);
15291 }
e9670677 15292
fef14a42 15293 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
20203fb9 15294 as_bad (_("-march=%s is not compatible with the selected ABI"),
fef14a42
TS
15295 arch_info->name);
15296
919731af 15297 file_mips_opts.arch = arch_info->cpu;
15298 file_mips_opts.isa = arch_info->isa;
3315614d 15299 file_mips_opts.init_ase = arch_info->ase;
919731af 15300
41cee089
FS
15301 /* The EVA Extension has instructions which are only valid when the R6 ISA
15302 is enabled. This sets the ASE_EVA_R6 flag when both EVA and R6 ISA are
15303 present. */
15304 if (((file_mips_opts.ase & ASE_EVA) != 0) && ISA_IS_R6 (file_mips_opts.isa))
15305 file_mips_opts.ase |= ASE_EVA_R6;
15306
919731af 15307 /* Set up initial mips_opts state. */
15308 mips_opts = file_mips_opts;
15309
27c634e0
FN
15310 /* For the R5900 default to `-mfix-r5900' unless the user told otherwise. */
15311 if (!mips_fix_r5900_explicit)
15312 mips_fix_r5900 = file_mips_opts.arch == CPU_R5900;
15313
919731af 15314 /* The register size inference code is now placed in
15315 file_mips_check_options. */
fef14a42 15316
0b35dfee 15317 /* Optimize for file_mips_opts.arch, unless -mtune selects a different
15318 processor. */
fef14a42
TS
15319 if (mips_tune_string != 0)
15320 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 15321
fef14a42
TS
15322 if (tune_info == 0)
15323 mips_set_tune (arch_info);
15324 else
15325 mips_set_tune (tune_info);
e9670677 15326
ecb4347a 15327 if (mips_flag_mdebug < 0)
e8044f35 15328 mips_flag_mdebug = 0;
e9670677
MR
15329}
15330\f
15331void
17a2f251 15332mips_init_after_args (void)
252b5132 15333{
6f2117ba 15334 /* Initialize opcodes. */
252b5132 15335 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 15336 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
15337}
15338
15339long
17a2f251 15340md_pcrel_from (fixS *fixP)
252b5132 15341{
a7ebbfdf 15342 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
6f2117ba 15343
a7ebbfdf
TS
15344 switch (fixP->fx_r_type)
15345 {
df58fc94
RS
15346 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15347 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15348 /* Return the address of the delay slot. */
15349 return addr + 2;
15350
15351 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15352 case BFD_RELOC_MICROMIPS_JMP:
c9775dde 15353 case BFD_RELOC_MIPS16_16_PCREL_S1:
a7ebbfdf 15354 case BFD_RELOC_16_PCREL_S2:
7361da2c
AB
15355 case BFD_RELOC_MIPS_21_PCREL_S2:
15356 case BFD_RELOC_MIPS_26_PCREL_S2:
a7ebbfdf
TS
15357 case BFD_RELOC_MIPS_JMP:
15358 /* Return the address of the delay slot. */
15359 return addr + 4;
df58fc94 15360
51f6035b
MR
15361 case BFD_RELOC_MIPS_18_PCREL_S3:
15362 /* Return the aligned address of the doubleword containing
15363 the instruction. */
15364 return addr & ~7;
15365
a7ebbfdf
TS
15366 default:
15367 return addr;
15368 }
252b5132
RH
15369}
15370
252b5132
RH
15371/* This is called before the symbol table is processed. In order to
15372 work with gcc when using mips-tfile, we must keep all local labels.
15373 However, in other cases, we want to discard them. If we were
15374 called with -g, but we didn't see any debugging information, it may
15375 mean that gcc is smuggling debugging information through to
15376 mips-tfile, in which case we must generate all local labels. */
15377
15378void
17a2f251 15379mips_frob_file_before_adjust (void)
252b5132
RH
15380{
15381#ifndef NO_ECOFF_DEBUGGING
15382 if (ECOFF_DEBUGGING
15383 && mips_debug != 0
15384 && ! ecoff_debugging_seen)
15385 flag_keep_locals = 1;
15386#endif
15387}
15388
3b91255e 15389/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 15390 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
15391 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
15392 relocation operators.
15393
15394 For our purposes, a %lo() expression matches a %got() or %hi()
15395 expression if:
15396
15397 (a) it refers to the same symbol; and
15398 (b) the offset applied in the %lo() expression is no lower than
15399 the offset applied in the %got() or %hi().
15400
15401 (b) allows us to cope with code like:
15402
15403 lui $4,%hi(foo)
15404 lh $4,%lo(foo+2)($4)
15405
15406 ...which is legal on RELA targets, and has a well-defined behaviour
15407 if the user knows that adding 2 to "foo" will not induce a carry to
15408 the high 16 bits.
15409
15410 When several %lo()s match a particular %got() or %hi(), we use the
15411 following rules to distinguish them:
15412
15413 (1) %lo()s with smaller offsets are a better match than %lo()s with
15414 higher offsets.
15415
15416 (2) %lo()s with no matching %got() or %hi() are better than those
15417 that already have a matching %got() or %hi().
15418
15419 (3) later %lo()s are better than earlier %lo()s.
15420
15421 These rules are applied in order.
15422
15423 (1) means, among other things, that %lo()s with identical offsets are
15424 chosen if they exist.
15425
15426 (2) means that we won't associate several high-part relocations with
15427 the same low-part relocation unless there's no alternative. Having
15428 several high parts for the same low part is a GNU extension; this rule
15429 allows careful users to avoid it.
15430
15431 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
15432 with the last high-part relocation being at the front of the list.
15433 It therefore makes sense to choose the last matching low-part
15434 relocation, all other things being equal. It's also easier
15435 to code that way. */
252b5132
RH
15436
15437void
17a2f251 15438mips_frob_file (void)
252b5132
RH
15439{
15440 struct mips_hi_fixup *l;
35903be0 15441 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
252b5132
RH
15442
15443 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15444 {
15445 segment_info_type *seginfo;
3b91255e
RS
15446 bfd_boolean matched_lo_p;
15447 fixS **hi_pos, **lo_pos, **pos;
252b5132 15448
9c2799c2 15449 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 15450
5919d012 15451 /* If a GOT16 relocation turns out to be against a global symbol,
b886a2ab
RS
15452 there isn't supposed to be a matching LO. Ignore %gots against
15453 constants; we'll report an error for those later. */
738e5348 15454 if (got16_reloc_p (l->fixp->fx_r_type)
b886a2ab 15455 && !(l->fixp->fx_addsy
9e009953 15456 && pic_need_relax (l->fixp->fx_addsy)))
5919d012
RS
15457 continue;
15458
15459 /* Check quickly whether the next fixup happens to be a matching %lo. */
15460 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
15461 continue;
15462
252b5132 15463 seginfo = seg_info (l->seg);
252b5132 15464
3b91255e
RS
15465 /* Set HI_POS to the position of this relocation in the chain.
15466 Set LO_POS to the position of the chosen low-part relocation.
15467 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15468 relocation that matches an immediately-preceding high-part
15469 relocation. */
15470 hi_pos = NULL;
15471 lo_pos = NULL;
15472 matched_lo_p = FALSE;
738e5348 15473 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
35903be0 15474
3b91255e
RS
15475 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15476 {
15477 if (*pos == l->fixp)
15478 hi_pos = pos;
15479
35903be0 15480 if ((*pos)->fx_r_type == looking_for_rtype
30cfc97a 15481 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
3b91255e
RS
15482 && (*pos)->fx_offset >= l->fixp->fx_offset
15483 && (lo_pos == NULL
15484 || (*pos)->fx_offset < (*lo_pos)->fx_offset
15485 || (!matched_lo_p
15486 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15487 lo_pos = pos;
15488
15489 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15490 && fixup_has_matching_lo_p (*pos));
15491 }
15492
15493 /* If we found a match, remove the high-part relocation from its
15494 current position and insert it before the low-part relocation.
15495 Make the offsets match so that fixup_has_matching_lo_p()
15496 will return true.
15497
15498 We don't warn about unmatched high-part relocations since some
15499 versions of gcc have been known to emit dead "lui ...%hi(...)"
15500 instructions. */
15501 if (lo_pos != NULL)
15502 {
15503 l->fixp->fx_offset = (*lo_pos)->fx_offset;
15504 if (l->fixp->fx_next != *lo_pos)
252b5132 15505 {
3b91255e
RS
15506 *hi_pos = l->fixp->fx_next;
15507 l->fixp->fx_next = *lo_pos;
15508 *lo_pos = l->fixp;
252b5132 15509 }
252b5132
RH
15510 }
15511 }
15512}
15513
252b5132 15514int
17a2f251 15515mips_force_relocation (fixS *fixp)
252b5132 15516{
ae6063d4 15517 if (generic_force_reloc (fixp))
252b5132
RH
15518 return 1;
15519
df58fc94
RS
15520 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15521 so that the linker relaxation can update targets. */
15522 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15523 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15524 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15525 return 1;
15526
5caa2b07
MR
15527 /* We want to keep BFD_RELOC_16_PCREL_S2 BFD_RELOC_MIPS_21_PCREL_S2
15528 and BFD_RELOC_MIPS_26_PCREL_S2 relocations against MIPS16 and
15529 microMIPS symbols so that we can do cross-mode branch diagnostics
15530 and BAL to JALX conversion by the linker. */
15531 if ((fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
9d862524
MR
15532 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15533 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2)
15534 && fixp->fx_addsy
15535 && ELF_ST_IS_COMPRESSED (S_GET_OTHER (fixp->fx_addsy)))
15536 return 1;
15537
7361da2c 15538 /* We want all PC-relative relocations to be kept for R6 relaxation. */
912815f0 15539 if (ISA_IS_R6 (file_mips_opts.isa)
7361da2c
AB
15540 && (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
15541 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15542 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
15543 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
15544 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
15545 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
15546 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL))
15547 return 1;
15548
3e722fb5 15549 return 0;
252b5132
RH
15550}
15551
b416ba9b
MR
15552/* Implement TC_FORCE_RELOCATION_ABS. */
15553
15554bfd_boolean
15555mips_force_relocation_abs (fixS *fixp)
15556{
15557 if (generic_force_reloc (fixp))
15558 return TRUE;
15559
15560 /* These relocations do not have enough bits in the in-place addend
15561 to hold an arbitrary absolute section's offset. */
15562 if (HAVE_IN_PLACE_ADDENDS && limited_pcrel_reloc_p (fixp->fx_r_type))
15563 return TRUE;
15564
15565 return FALSE;
15566}
15567
b886a2ab
RS
15568/* Read the instruction associated with RELOC from BUF. */
15569
15570static unsigned int
15571read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15572{
15573 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15574 return read_compressed_insn (buf, 4);
15575 else
15576 return read_insn (buf);
15577}
15578
15579/* Write instruction INSN to BUF, given that it has been relocated
15580 by RELOC. */
15581
15582static void
15583write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15584 unsigned long insn)
15585{
15586 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15587 write_compressed_insn (buf, insn, 4);
15588 else
15589 write_insn (buf, insn);
15590}
15591
9d862524
MR
15592/* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15593 to a symbol in another ISA mode, which cannot be converted to JALX. */
15594
15595static bfd_boolean
15596fix_bad_cross_mode_jump_p (fixS *fixP)
15597{
15598 unsigned long opcode;
15599 int other;
15600 char *buf;
15601
15602 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15603 return FALSE;
15604
15605 other = S_GET_OTHER (fixP->fx_addsy);
15606 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15607 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15608 switch (fixP->fx_r_type)
15609 {
15610 case BFD_RELOC_MIPS_JMP:
15611 return opcode != 0x1d && opcode != 0x03 && ELF_ST_IS_COMPRESSED (other);
15612 case BFD_RELOC_MICROMIPS_JMP:
15613 return opcode != 0x3c && opcode != 0x3d && !ELF_ST_IS_MICROMIPS (other);
15614 default:
15615 return FALSE;
15616 }
15617}
15618
15619/* Return TRUE if the instruction pointed to by FIXP is an invalid JALX
15620 jump to a symbol in the same ISA mode. */
15621
15622static bfd_boolean
15623fix_bad_same_mode_jalx_p (fixS *fixP)
15624{
15625 unsigned long opcode;
15626 int other;
15627 char *buf;
15628
15629 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15630 return FALSE;
15631
15632 other = S_GET_OTHER (fixP->fx_addsy);
15633 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15634 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15635 switch (fixP->fx_r_type)
15636 {
15637 case BFD_RELOC_MIPS_JMP:
15638 return opcode == 0x1d && !ELF_ST_IS_COMPRESSED (other);
15639 case BFD_RELOC_MIPS16_JMP:
15640 return opcode == 0x07 && ELF_ST_IS_COMPRESSED (other);
15641 case BFD_RELOC_MICROMIPS_JMP:
15642 return opcode == 0x3c && ELF_ST_IS_COMPRESSED (other);
15643 default:
15644 return FALSE;
15645 }
15646}
15647
15648/* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15649 to a symbol whose value plus addend is not aligned according to the
15650 ultimate (after linker relaxation) jump instruction's immediate field
15651 requirement, either to (1 << SHIFT), or, for jumps from microMIPS to
15652 regular MIPS code, to (1 << 2). */
15653
15654static bfd_boolean
15655fix_bad_misaligned_jump_p (fixS *fixP, int shift)
15656{
15657 bfd_boolean micro_to_mips_p;
15658 valueT val;
15659 int other;
15660
15661 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15662 return FALSE;
15663
15664 other = S_GET_OTHER (fixP->fx_addsy);
15665 val = S_GET_VALUE (fixP->fx_addsy) | ELF_ST_IS_COMPRESSED (other);
15666 val += fixP->fx_offset;
15667 micro_to_mips_p = (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15668 && !ELF_ST_IS_MICROMIPS (other));
15669 return ((val & ((1 << (micro_to_mips_p ? 2 : shift)) - 1))
15670 != ELF_ST_IS_COMPRESSED (other));
15671}
15672
15673/* Return TRUE if the instruction pointed to by FIXP is an invalid branch
15674 to a symbol whose annotation indicates another ISA mode. For absolute
a6ebf616
MR
15675 symbols check the ISA bit instead.
15676
15677 We accept BFD_RELOC_16_PCREL_S2 relocations against MIPS16 and microMIPS
15678 symbols or BFD_RELOC_MICROMIPS_16_PCREL_S1 relocations against regular
15679 MIPS symbols and associated with BAL instructions as these instructions
de194d85 15680 may be converted to JALX by the linker. */
9d862524
MR
15681
15682static bfd_boolean
15683fix_bad_cross_mode_branch_p (fixS *fixP)
15684{
15685 bfd_boolean absolute_p;
15686 unsigned long opcode;
15687 asection *symsec;
15688 valueT val;
15689 int other;
15690 char *buf;
15691
8b10b0b3
MR
15692 if (mips_ignore_branch_isa)
15693 return FALSE;
15694
9d862524
MR
15695 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15696 return FALSE;
15697
15698 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15699 absolute_p = bfd_is_abs_section (symsec);
15700
15701 val = S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset;
15702 other = S_GET_OTHER (fixP->fx_addsy);
15703
15704 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15705 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 16;
15706 switch (fixP->fx_r_type)
15707 {
15708 case BFD_RELOC_16_PCREL_S2:
a6ebf616
MR
15709 return ((absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other))
15710 && opcode != 0x0411);
15711 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15712 return ((absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other))
15713 && opcode != 0x4060);
9d862524
MR
15714 case BFD_RELOC_MIPS_21_PCREL_S2:
15715 case BFD_RELOC_MIPS_26_PCREL_S2:
15716 return absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other);
15717 case BFD_RELOC_MIPS16_16_PCREL_S1:
15718 return absolute_p ? !(val & 1) : !ELF_ST_IS_MIPS16 (other);
15719 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15720 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
9d862524
MR
15721 return absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other);
15722 default:
15723 abort ();
15724 }
15725}
15726
15727/* Return TRUE if the symbol plus addend associated with a regular MIPS
15728 branch instruction pointed to by FIXP is not aligned according to the
15729 branch instruction's immediate field requirement. We need the addend
15730 to preserve the ISA bit and also the sum must not have bit 2 set. We
15731 must explicitly OR in the ISA bit from symbol annotation as the bit
15732 won't be set in the symbol's value then. */
15733
15734static bfd_boolean
15735fix_bad_misaligned_branch_p (fixS *fixP)
15736{
15737 bfd_boolean absolute_p;
15738 asection *symsec;
15739 valueT isa_bit;
15740 valueT val;
15741 valueT off;
15742 int other;
15743
15744 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15745 return FALSE;
15746
15747 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15748 absolute_p = bfd_is_abs_section (symsec);
15749
15750 val = S_GET_VALUE (fixP->fx_addsy);
15751 other = S_GET_OTHER (fixP->fx_addsy);
15752 off = fixP->fx_offset;
15753
15754 isa_bit = absolute_p ? (val + off) & 1 : ELF_ST_IS_COMPRESSED (other);
15755 val |= ELF_ST_IS_COMPRESSED (other);
15756 val += off;
15757 return (val & 0x3) != isa_bit;
15758}
15759
52031738
FS
15760/* Calculate the relocation target by masking off ISA mode bit before
15761 combining symbol and addend. */
15762
15763static valueT
15764fix_bad_misaligned_address (fixS *fixP)
15765{
15766 valueT val;
15767 valueT off;
15768 unsigned isa_mode;
15769 gas_assert (fixP != NULL && fixP->fx_addsy != NULL);
15770 val = S_GET_VALUE (fixP->fx_addsy);
15771 off = fixP->fx_offset;
15772 isa_mode = (ELF_ST_IS_COMPRESSED (S_GET_OTHER (fixP->fx_addsy))
15773 ? 1 : 0);
15774
15775 return ((val & ~isa_mode) + off);
15776}
15777
9d862524
MR
15778/* Make the necessary checks on a regular MIPS branch pointed to by FIXP
15779 and its calculated value VAL. */
15780
15781static void
15782fix_validate_branch (fixS *fixP, valueT val)
15783{
15784 if (fixP->fx_done && (val & 0x3) != 0)
15785 as_bad_where (fixP->fx_file, fixP->fx_line,
15786 _("branch to misaligned address (0x%lx)"),
15787 (long) (val + md_pcrel_from (fixP)));
15788 else if (fix_bad_cross_mode_branch_p (fixP))
15789 as_bad_where (fixP->fx_file, fixP->fx_line,
15790 _("branch to a symbol in another ISA mode"));
15791 else if (fix_bad_misaligned_branch_p (fixP))
15792 as_bad_where (fixP->fx_file, fixP->fx_line,
15793 _("branch to misaligned address (0x%lx)"),
52031738 15794 (long) fix_bad_misaligned_address (fixP));
9d862524
MR
15795 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x3) != 0)
15796 as_bad_where (fixP->fx_file, fixP->fx_line,
15797 _("cannot encode misaligned addend "
15798 "in the relocatable field (0x%lx)"),
15799 (long) fixP->fx_offset);
15800}
15801
252b5132
RH
15802/* Apply a fixup to the object file. */
15803
94f592af 15804void
55cf6793 15805md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 15806{
4d68580a 15807 char *buf;
b886a2ab 15808 unsigned long insn;
a7ebbfdf 15809 reloc_howto_type *howto;
252b5132 15810
d56a8dda
RS
15811 if (fixP->fx_pcrel)
15812 switch (fixP->fx_r_type)
15813 {
15814 case BFD_RELOC_16_PCREL_S2:
c9775dde 15815 case BFD_RELOC_MIPS16_16_PCREL_S1:
d56a8dda
RS
15816 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15817 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15818 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15819 case BFD_RELOC_32_PCREL:
7361da2c
AB
15820 case BFD_RELOC_MIPS_21_PCREL_S2:
15821 case BFD_RELOC_MIPS_26_PCREL_S2:
15822 case BFD_RELOC_MIPS_18_PCREL_S3:
15823 case BFD_RELOC_MIPS_19_PCREL_S2:
15824 case BFD_RELOC_HI16_S_PCREL:
15825 case BFD_RELOC_LO16_PCREL:
d56a8dda
RS
15826 break;
15827
15828 case BFD_RELOC_32:
15829 fixP->fx_r_type = BFD_RELOC_32_PCREL;
15830 break;
15831
15832 default:
15833 as_bad_where (fixP->fx_file, fixP->fx_line,
15834 _("PC-relative reference to a different section"));
15835 break;
15836 }
15837
15838 /* Handle BFD_RELOC_8, since it's easy. Punt on other bfd relocations
15839 that have no MIPS ELF equivalent. */
15840 if (fixP->fx_r_type != BFD_RELOC_8)
15841 {
15842 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15843 if (!howto)
15844 return;
15845 }
65551fa4 15846
df58fc94
RS
15847 gas_assert (fixP->fx_size == 2
15848 || fixP->fx_size == 4
d56a8dda 15849 || fixP->fx_r_type == BFD_RELOC_8
90ecf173
MR
15850 || fixP->fx_r_type == BFD_RELOC_16
15851 || fixP->fx_r_type == BFD_RELOC_64
15852 || fixP->fx_r_type == BFD_RELOC_CTOR
15853 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
df58fc94 15854 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
90ecf173
MR
15855 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15856 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
2f0c68f2
CM
15857 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64
15858 || fixP->fx_r_type == BFD_RELOC_NONE);
252b5132 15859
4d68580a 15860 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132 15861
b1dca8ee
RS
15862 /* Don't treat parts of a composite relocation as done. There are two
15863 reasons for this:
15864
15865 (1) The second and third parts will be against 0 (RSS_UNDEF) but
15866 should nevertheless be emitted if the first part is.
15867
15868 (2) In normal usage, composite relocations are never assembly-time
15869 constants. The easiest way of dealing with the pathological
15870 exceptions is to generate a relocation against STN_UNDEF and
15871 leave everything up to the linker. */
3994f87e 15872 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
252b5132
RH
15873 fixP->fx_done = 1;
15874
15875 switch (fixP->fx_r_type)
15876 {
3f98094e
DJ
15877 case BFD_RELOC_MIPS_TLS_GD:
15878 case BFD_RELOC_MIPS_TLS_LDM:
741d6ea8
JM
15879 case BFD_RELOC_MIPS_TLS_DTPREL32:
15880 case BFD_RELOC_MIPS_TLS_DTPREL64:
3f98094e
DJ
15881 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15882 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15883 case BFD_RELOC_MIPS_TLS_GOTTPREL:
d0f13682
CLT
15884 case BFD_RELOC_MIPS_TLS_TPREL32:
15885 case BFD_RELOC_MIPS_TLS_TPREL64:
3f98094e
DJ
15886 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15887 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
df58fc94
RS
15888 case BFD_RELOC_MICROMIPS_TLS_GD:
15889 case BFD_RELOC_MICROMIPS_TLS_LDM:
15890 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15891 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15892 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15893 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15894 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
d0f13682
CLT
15895 case BFD_RELOC_MIPS16_TLS_GD:
15896 case BFD_RELOC_MIPS16_TLS_LDM:
15897 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15898 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15899 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15900 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15901 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
4512dafa
MR
15902 if (fixP->fx_addsy)
15903 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15904 else
15905 as_bad_where (fixP->fx_file, fixP->fx_line,
15906 _("TLS relocation against a constant"));
15907 break;
3f98094e 15908
252b5132 15909 case BFD_RELOC_MIPS_JMP:
9d862524
MR
15910 case BFD_RELOC_MIPS16_JMP:
15911 case BFD_RELOC_MICROMIPS_JMP:
15912 {
15913 int shift;
15914
15915 gas_assert (!fixP->fx_done);
15916
15917 /* Shift is 2, unusually, for microMIPS JALX. */
15918 if (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15919 && (read_compressed_insn (buf, 4) >> 26) != 0x3c)
15920 shift = 1;
15921 else
15922 shift = 2;
15923
15924 if (fix_bad_cross_mode_jump_p (fixP))
15925 as_bad_where (fixP->fx_file, fixP->fx_line,
15926 _("jump to a symbol in another ISA mode"));
15927 else if (fix_bad_same_mode_jalx_p (fixP))
15928 as_bad_where (fixP->fx_file, fixP->fx_line,
15929 _("JALX to a symbol in the same ISA mode"));
15930 else if (fix_bad_misaligned_jump_p (fixP, shift))
15931 as_bad_where (fixP->fx_file, fixP->fx_line,
15932 _("jump to misaligned address (0x%lx)"),
52031738 15933 (long) fix_bad_misaligned_address (fixP));
9d862524
MR
15934 else if (HAVE_IN_PLACE_ADDENDS
15935 && (fixP->fx_offset & ((1 << shift) - 1)) != 0)
15936 as_bad_where (fixP->fx_file, fixP->fx_line,
15937 _("cannot encode misaligned addend "
15938 "in the relocatable field (0x%lx)"),
15939 (long) fixP->fx_offset);
15940 }
15941 /* Fall through. */
15942
e369bcce
TS
15943 case BFD_RELOC_MIPS_SHIFT5:
15944 case BFD_RELOC_MIPS_SHIFT6:
15945 case BFD_RELOC_MIPS_GOT_DISP:
15946 case BFD_RELOC_MIPS_GOT_PAGE:
15947 case BFD_RELOC_MIPS_GOT_OFST:
15948 case BFD_RELOC_MIPS_SUB:
15949 case BFD_RELOC_MIPS_INSERT_A:
15950 case BFD_RELOC_MIPS_INSERT_B:
15951 case BFD_RELOC_MIPS_DELETE:
15952 case BFD_RELOC_MIPS_HIGHEST:
15953 case BFD_RELOC_MIPS_HIGHER:
15954 case BFD_RELOC_MIPS_SCN_DISP:
15955 case BFD_RELOC_MIPS_REL16:
15956 case BFD_RELOC_MIPS_RELGOT:
15957 case BFD_RELOC_MIPS_JALR:
252b5132
RH
15958 case BFD_RELOC_HI16:
15959 case BFD_RELOC_HI16_S:
b886a2ab 15960 case BFD_RELOC_LO16:
cdf6fd85 15961 case BFD_RELOC_GPREL16:
252b5132
RH
15962 case BFD_RELOC_MIPS_LITERAL:
15963 case BFD_RELOC_MIPS_CALL16:
15964 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 15965 case BFD_RELOC_GPREL32:
252b5132
RH
15966 case BFD_RELOC_MIPS_GOT_HI16:
15967 case BFD_RELOC_MIPS_GOT_LO16:
15968 case BFD_RELOC_MIPS_CALL_HI16:
15969 case BFD_RELOC_MIPS_CALL_LO16:
41947d9e
MR
15970 case BFD_RELOC_HI16_S_PCREL:
15971 case BFD_RELOC_LO16_PCREL:
252b5132 15972 case BFD_RELOC_MIPS16_GPREL:
738e5348
RS
15973 case BFD_RELOC_MIPS16_GOT16:
15974 case BFD_RELOC_MIPS16_CALL16:
d6f16593
MR
15975 case BFD_RELOC_MIPS16_HI16:
15976 case BFD_RELOC_MIPS16_HI16_S:
b886a2ab 15977 case BFD_RELOC_MIPS16_LO16:
df58fc94
RS
15978 case BFD_RELOC_MICROMIPS_GOT_DISP:
15979 case BFD_RELOC_MICROMIPS_GOT_PAGE:
15980 case BFD_RELOC_MICROMIPS_GOT_OFST:
15981 case BFD_RELOC_MICROMIPS_SUB:
15982 case BFD_RELOC_MICROMIPS_HIGHEST:
15983 case BFD_RELOC_MICROMIPS_HIGHER:
15984 case BFD_RELOC_MICROMIPS_SCN_DISP:
15985 case BFD_RELOC_MICROMIPS_JALR:
15986 case BFD_RELOC_MICROMIPS_HI16:
15987 case BFD_RELOC_MICROMIPS_HI16_S:
b886a2ab 15988 case BFD_RELOC_MICROMIPS_LO16:
df58fc94
RS
15989 case BFD_RELOC_MICROMIPS_GPREL16:
15990 case BFD_RELOC_MICROMIPS_LITERAL:
15991 case BFD_RELOC_MICROMIPS_CALL16:
15992 case BFD_RELOC_MICROMIPS_GOT16:
15993 case BFD_RELOC_MICROMIPS_GOT_HI16:
15994 case BFD_RELOC_MICROMIPS_GOT_LO16:
15995 case BFD_RELOC_MICROMIPS_CALL_HI16:
15996 case BFD_RELOC_MICROMIPS_CALL_LO16:
067ec077 15997 case BFD_RELOC_MIPS_EH:
b886a2ab
RS
15998 if (fixP->fx_done)
15999 {
16000 offsetT value;
16001
16002 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
16003 {
16004 insn = read_reloc_insn (buf, fixP->fx_r_type);
16005 if (mips16_reloc_p (fixP->fx_r_type))
16006 insn |= mips16_immed_extend (value, 16);
16007 else
16008 insn |= (value & 0xffff);
16009 write_reloc_insn (buf, fixP->fx_r_type, insn);
16010 }
16011 else
16012 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 16013 _("unsupported constant in relocation"));
b886a2ab 16014 }
252b5132
RH
16015 break;
16016
252b5132
RH
16017 case BFD_RELOC_64:
16018 /* This is handled like BFD_RELOC_32, but we output a sign
16019 extended value if we are only 32 bits. */
3e722fb5 16020 if (fixP->fx_done)
252b5132
RH
16021 {
16022 if (8 <= sizeof (valueT))
4d68580a 16023 md_number_to_chars (buf, *valP, 8);
252b5132
RH
16024 else
16025 {
a7ebbfdf 16026 valueT hiv;
252b5132 16027
a7ebbfdf 16028 if ((*valP & 0x80000000) != 0)
252b5132
RH
16029 hiv = 0xffffffff;
16030 else
16031 hiv = 0;
4d68580a
RS
16032 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
16033 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
252b5132
RH
16034 }
16035 }
16036 break;
16037
056350c6 16038 case BFD_RELOC_RVA:
252b5132 16039 case BFD_RELOC_32:
b47468a6 16040 case BFD_RELOC_32_PCREL:
252b5132 16041 case BFD_RELOC_16:
d56a8dda 16042 case BFD_RELOC_8:
252b5132 16043 /* If we are deleting this reloc entry, we must fill in the
54f4ddb3
TS
16044 value now. This can happen if we have a .word which is not
16045 resolved when it appears but is later defined. */
252b5132 16046 if (fixP->fx_done)
4d68580a 16047 md_number_to_chars (buf, *valP, fixP->fx_size);
252b5132
RH
16048 break;
16049
7361da2c 16050 case BFD_RELOC_MIPS_21_PCREL_S2:
9d862524 16051 fix_validate_branch (fixP, *valP);
41947d9e
MR
16052 if (!fixP->fx_done)
16053 break;
16054
16055 if (*valP + 0x400000 <= 0x7fffff)
16056 {
16057 insn = read_insn (buf);
16058 insn |= (*valP >> 2) & 0x1fffff;
16059 write_insn (buf, insn);
16060 }
16061 else
16062 as_bad_where (fixP->fx_file, fixP->fx_line,
16063 _("branch out of range"));
16064 break;
16065
7361da2c 16066 case BFD_RELOC_MIPS_26_PCREL_S2:
9d862524 16067 fix_validate_branch (fixP, *valP);
41947d9e
MR
16068 if (!fixP->fx_done)
16069 break;
7361da2c 16070
41947d9e
MR
16071 if (*valP + 0x8000000 <= 0xfffffff)
16072 {
16073 insn = read_insn (buf);
16074 insn |= (*valP >> 2) & 0x3ffffff;
16075 write_insn (buf, insn);
16076 }
16077 else
16078 as_bad_where (fixP->fx_file, fixP->fx_line,
16079 _("branch out of range"));
7361da2c
AB
16080 break;
16081
16082 case BFD_RELOC_MIPS_18_PCREL_S3:
717ba204 16083 if (fixP->fx_addsy && (S_GET_VALUE (fixP->fx_addsy) & 0x7) != 0)
7361da2c 16084 as_bad_where (fixP->fx_file, fixP->fx_line,
0866e94c
MF
16085 _("PC-relative access using misaligned symbol (%lx)"),
16086 (long) S_GET_VALUE (fixP->fx_addsy));
16087 if ((fixP->fx_offset & 0x7) != 0)
16088 as_bad_where (fixP->fx_file, fixP->fx_line,
16089 _("PC-relative access using misaligned offset (%lx)"),
16090 (long) fixP->fx_offset);
41947d9e
MR
16091 if (!fixP->fx_done)
16092 break;
7361da2c 16093
41947d9e
MR
16094 if (*valP + 0x100000 <= 0x1fffff)
16095 {
16096 insn = read_insn (buf);
16097 insn |= (*valP >> 3) & 0x3ffff;
16098 write_insn (buf, insn);
16099 }
16100 else
16101 as_bad_where (fixP->fx_file, fixP->fx_line,
16102 _("PC-relative access out of range"));
7361da2c
AB
16103 break;
16104
16105 case BFD_RELOC_MIPS_19_PCREL_S2:
16106 if ((*valP & 0x3) != 0)
16107 as_bad_where (fixP->fx_file, fixP->fx_line,
16108 _("PC-relative access to misaligned address (%lx)"),
717ba204 16109 (long) *valP);
41947d9e
MR
16110 if (!fixP->fx_done)
16111 break;
7361da2c 16112
41947d9e
MR
16113 if (*valP + 0x100000 <= 0x1fffff)
16114 {
16115 insn = read_insn (buf);
16116 insn |= (*valP >> 2) & 0x7ffff;
16117 write_insn (buf, insn);
16118 }
16119 else
16120 as_bad_where (fixP->fx_file, fixP->fx_line,
16121 _("PC-relative access out of range"));
7361da2c
AB
16122 break;
16123
252b5132 16124 case BFD_RELOC_16_PCREL_S2:
9d862524 16125 fix_validate_branch (fixP, *valP);
cb56d3d3 16126
54f4ddb3
TS
16127 /* We need to save the bits in the instruction since fixup_segment()
16128 might be deleting the relocation entry (i.e., a branch within
16129 the current segment). */
a7ebbfdf 16130 if (! fixP->fx_done)
bb2d6cd7 16131 break;
252b5132 16132
54f4ddb3 16133 /* Update old instruction data. */
4d68580a 16134 insn = read_insn (buf);
252b5132 16135
a7ebbfdf
TS
16136 if (*valP + 0x20000 <= 0x3ffff)
16137 {
16138 insn |= (*valP >> 2) & 0xffff;
4d68580a 16139 write_insn (buf, insn);
a7ebbfdf 16140 }
ce8ad872 16141 else if (fixP->fx_tcbit2
a7ebbfdf
TS
16142 && fixP->fx_done
16143 && fixP->fx_frag->fr_address >= text_section->vma
16144 && (fixP->fx_frag->fr_address
fd361982 16145 < text_section->vma + bfd_section_size (text_section))
a7ebbfdf
TS
16146 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
16147 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
16148 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
16149 {
16150 /* The branch offset is too large. If this is an
16151 unconditional branch, and we are not generating PIC code,
16152 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
16153 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
16154 insn = 0x0c000000; /* jal */
252b5132 16155 else
a7ebbfdf
TS
16156 insn = 0x08000000; /* j */
16157 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
16158 fixP->fx_done = 0;
16159 fixP->fx_addsy = section_symbol (text_section);
16160 *valP += md_pcrel_from (fixP);
4d68580a 16161 write_insn (buf, insn);
a7ebbfdf
TS
16162 }
16163 else
16164 {
16165 /* If we got here, we have branch-relaxation disabled,
16166 and there's nothing we can do to fix this instruction
16167 without turning it into a longer sequence. */
16168 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 16169 _("branch out of range"));
252b5132 16170 }
252b5132
RH
16171 break;
16172
c9775dde 16173 case BFD_RELOC_MIPS16_16_PCREL_S1:
df58fc94
RS
16174 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
16175 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
16176 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
96e9ba5f 16177 gas_assert (!fixP->fx_done);
9d862524
MR
16178 if (fix_bad_cross_mode_branch_p (fixP))
16179 as_bad_where (fixP->fx_file, fixP->fx_line,
16180 _("branch to a symbol in another ISA mode"));
16181 else if (fixP->fx_addsy
16182 && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
16183 && !bfd_is_abs_section (S_GET_SEGMENT (fixP->fx_addsy))
16184 && (fixP->fx_offset & 0x1) != 0)
16185 as_bad_where (fixP->fx_file, fixP->fx_line,
16186 _("branch to misaligned address (0x%lx)"),
52031738 16187 (long) fix_bad_misaligned_address (fixP));
9d862524
MR
16188 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x1) != 0)
16189 as_bad_where (fixP->fx_file, fixP->fx_line,
16190 _("cannot encode misaligned addend "
16191 "in the relocatable field (0x%lx)"),
16192 (long) fixP->fx_offset);
df58fc94
RS
16193 break;
16194
252b5132
RH
16195 case BFD_RELOC_VTABLE_INHERIT:
16196 fixP->fx_done = 0;
16197 if (fixP->fx_addsy
16198 && !S_IS_DEFINED (fixP->fx_addsy)
16199 && !S_IS_WEAK (fixP->fx_addsy))
16200 S_SET_WEAK (fixP->fx_addsy);
16201 break;
16202
2f0c68f2 16203 case BFD_RELOC_NONE:
252b5132
RH
16204 case BFD_RELOC_VTABLE_ENTRY:
16205 fixP->fx_done = 0;
16206 break;
16207
16208 default:
b37df7c4 16209 abort ();
252b5132 16210 }
a7ebbfdf
TS
16211
16212 /* Remember value for tc_gen_reloc. */
16213 fixP->fx_addnumber = *valP;
252b5132
RH
16214}
16215
252b5132 16216static symbolS *
17a2f251 16217get_symbol (void)
252b5132
RH
16218{
16219 int c;
16220 char *name;
16221 symbolS *p;
16222
d02603dc 16223 c = get_symbol_name (&name);
252b5132 16224 p = (symbolS *) symbol_find_or_make (name);
d02603dc 16225 (void) restore_line_pointer (c);
252b5132
RH
16226 return p;
16227}
16228
742a56fe
RS
16229/* Align the current frag to a given power of two. If a particular
16230 fill byte should be used, FILL points to an integer that contains
16231 that byte, otherwise FILL is null.
16232
462427c4
RS
16233 This function used to have the comment:
16234
16235 The MIPS assembler also automatically adjusts any preceding label.
16236
16237 The implementation therefore applied the adjustment to a maximum of
16238 one label. However, other label adjustments are applied to batches
16239 of labels, and adjusting just one caused problems when new labels
16240 were added for the sake of debugging or unwind information.
16241 We therefore adjust all preceding labels (given as LABELS) instead. */
252b5132
RH
16242
16243static void
462427c4 16244mips_align (int to, int *fill, struct insn_label_list *labels)
252b5132 16245{
7d10b47d 16246 mips_emit_delays ();
df58fc94 16247 mips_record_compressed_mode ();
742a56fe
RS
16248 if (fill == NULL && subseg_text_p (now_seg))
16249 frag_align_code (to, 0);
16250 else
16251 frag_align (to, fill ? *fill : 0, 0);
252b5132 16252 record_alignment (now_seg, to);
770c0151 16253 mips_move_labels (labels, subseg_text_p (now_seg));
252b5132
RH
16254}
16255
16256/* Align to a given power of two. .align 0 turns off the automatic
16257 alignment used by the data creating pseudo-ops. */
16258
16259static void
17a2f251 16260s_align (int x ATTRIBUTE_UNUSED)
252b5132 16261{
742a56fe 16262 int temp, fill_value, *fill_ptr;
49954fb4 16263 long max_alignment = 28;
252b5132 16264
54f4ddb3 16265 /* o Note that the assembler pulls down any immediately preceding label
252b5132 16266 to the aligned address.
54f4ddb3 16267 o It's not documented but auto alignment is reinstated by
252b5132 16268 a .align pseudo instruction.
54f4ddb3 16269 o Note also that after auto alignment is turned off the mips assembler
252b5132 16270 issues an error on attempt to assemble an improperly aligned data item.
54f4ddb3 16271 We don't. */
252b5132
RH
16272
16273 temp = get_absolute_expression ();
16274 if (temp > max_alignment)
1661c76c 16275 as_bad (_("alignment too large, %d assumed"), temp = max_alignment);
252b5132
RH
16276 else if (temp < 0)
16277 {
1661c76c 16278 as_warn (_("alignment negative, 0 assumed"));
252b5132
RH
16279 temp = 0;
16280 }
16281 if (*input_line_pointer == ',')
16282 {
f9419b05 16283 ++input_line_pointer;
742a56fe
RS
16284 fill_value = get_absolute_expression ();
16285 fill_ptr = &fill_value;
252b5132
RH
16286 }
16287 else
742a56fe 16288 fill_ptr = 0;
252b5132
RH
16289 if (temp)
16290 {
a8dbcb85
TS
16291 segment_info_type *si = seg_info (now_seg);
16292 struct insn_label_list *l = si->label_list;
54f4ddb3 16293 /* Auto alignment should be switched on by next section change. */
252b5132 16294 auto_align = 1;
462427c4 16295 mips_align (temp, fill_ptr, l);
252b5132
RH
16296 }
16297 else
16298 {
16299 auto_align = 0;
16300 }
16301
16302 demand_empty_rest_of_line ();
16303}
16304
252b5132 16305static void
17a2f251 16306s_change_sec (int sec)
252b5132
RH
16307{
16308 segT seg;
16309
252b5132
RH
16310 /* The ELF backend needs to know that we are changing sections, so
16311 that .previous works correctly. We could do something like check
b6ff326e 16312 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
16313 as it would not be appropriate to use it in the section changing
16314 functions in read.c, since obj-elf.c intercepts those. FIXME:
16315 This should be cleaner, somehow. */
f3ded42a 16316 obj_elf_section_change_hook ();
252b5132 16317
7d10b47d 16318 mips_emit_delays ();
6a32d874 16319
252b5132
RH
16320 switch (sec)
16321 {
16322 case 't':
16323 s_text (0);
16324 break;
16325 case 'd':
16326 s_data (0);
16327 break;
16328 case 'b':
16329 subseg_set (bss_section, (subsegT) get_absolute_expression ());
16330 demand_empty_rest_of_line ();
16331 break;
16332
16333 case 'r':
4d0d148d
TS
16334 seg = subseg_new (RDATA_SECTION_NAME,
16335 (subsegT) get_absolute_expression ());
fd361982
AM
16336 bfd_set_section_flags (seg, (SEC_ALLOC | SEC_LOAD | SEC_READONLY
16337 | SEC_RELOC | SEC_DATA));
f3ded42a
RS
16338 if (strncmp (TARGET_OS, "elf", 3) != 0)
16339 record_alignment (seg, 4);
4d0d148d 16340 demand_empty_rest_of_line ();
252b5132
RH
16341 break;
16342
16343 case 's':
4d0d148d 16344 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
a4dd6c97
AM
16345 bfd_set_section_flags (seg, (SEC_ALLOC | SEC_LOAD | SEC_RELOC
16346 | SEC_DATA | SEC_SMALL_DATA));
f3ded42a
RS
16347 if (strncmp (TARGET_OS, "elf", 3) != 0)
16348 record_alignment (seg, 4);
4d0d148d
TS
16349 demand_empty_rest_of_line ();
16350 break;
998b3c36
MR
16351
16352 case 'B':
16353 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
a4dd6c97 16354 bfd_set_section_flags (seg, SEC_ALLOC | SEC_SMALL_DATA);
f3ded42a
RS
16355 if (strncmp (TARGET_OS, "elf", 3) != 0)
16356 record_alignment (seg, 4);
998b3c36
MR
16357 demand_empty_rest_of_line ();
16358 break;
252b5132
RH
16359 }
16360
16361 auto_align = 1;
16362}
b34976b6 16363
cca86cc8 16364void
17a2f251 16365s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 16366{
d02603dc 16367 char *saved_ilp;
cca86cc8 16368 char *section_name;
d02603dc 16369 char c, endc;
684022ea 16370 char next_c = 0;
cca86cc8
SC
16371 int section_type;
16372 int section_flag;
16373 int section_entry_size;
16374 int section_alignment;
b34976b6 16375
d02603dc
NC
16376 saved_ilp = input_line_pointer;
16377 endc = get_symbol_name (&section_name);
16378 c = (endc == '"' ? input_line_pointer[1] : endc);
a816d1ed 16379 if (c)
d02603dc 16380 next_c = input_line_pointer [(endc == '"' ? 2 : 1)];
cca86cc8 16381
4cf0dd0d
TS
16382 /* Do we have .section Name<,"flags">? */
16383 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 16384 {
d02603dc
NC
16385 /* Just after name is now '\0'. */
16386 (void) restore_line_pointer (endc);
16387 input_line_pointer = saved_ilp;
cca86cc8
SC
16388 obj_elf_section (ignore);
16389 return;
16390 }
d02603dc
NC
16391
16392 section_name = xstrdup (section_name);
16393 c = restore_line_pointer (endc);
16394
cca86cc8
SC
16395 input_line_pointer++;
16396
16397 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
16398 if (c == ',')
16399 section_type = get_absolute_expression ();
16400 else
16401 section_type = 0;
d02603dc 16402
cca86cc8
SC
16403 if (*input_line_pointer++ == ',')
16404 section_flag = get_absolute_expression ();
16405 else
16406 section_flag = 0;
d02603dc 16407
cca86cc8
SC
16408 if (*input_line_pointer++ == ',')
16409 section_entry_size = get_absolute_expression ();
16410 else
16411 section_entry_size = 0;
d02603dc 16412
cca86cc8
SC
16413 if (*input_line_pointer++ == ',')
16414 section_alignment = get_absolute_expression ();
16415 else
16416 section_alignment = 0;
d02603dc 16417
87975d2a
AM
16418 /* FIXME: really ignore? */
16419 (void) section_alignment;
cca86cc8 16420
8ab8a5c8
RS
16421 /* When using the generic form of .section (as implemented by obj-elf.c),
16422 there's no way to set the section type to SHT_MIPS_DWARF. Users have
16423 traditionally had to fall back on the more common @progbits instead.
16424
16425 There's nothing really harmful in this, since bfd will correct
16426 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
708587a4 16427 means that, for backwards compatibility, the special_section entries
8ab8a5c8
RS
16428 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
16429
16430 Even so, we shouldn't force users of the MIPS .section syntax to
16431 incorrectly label the sections as SHT_PROGBITS. The best compromise
16432 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
16433 generic type-checking code. */
16434 if (section_type == SHT_MIPS_DWARF)
16435 section_type = SHT_PROGBITS;
16436
a8c4d40b 16437 obj_elf_change_section (section_name, section_type, section_flag,
cca86cc8 16438 section_entry_size, 0, 0, 0);
a816d1ed
AO
16439
16440 if (now_seg->name != section_name)
16441 free (section_name);
cca86cc8 16442}
252b5132
RH
16443
16444void
17a2f251 16445mips_enable_auto_align (void)
252b5132
RH
16446{
16447 auto_align = 1;
16448}
16449
16450static void
17a2f251 16451s_cons (int log_size)
252b5132 16452{
a8dbcb85
TS
16453 segment_info_type *si = seg_info (now_seg);
16454 struct insn_label_list *l = si->label_list;
252b5132 16455
7d10b47d 16456 mips_emit_delays ();
252b5132 16457 if (log_size > 0 && auto_align)
462427c4 16458 mips_align (log_size, 0, l);
252b5132 16459 cons (1 << log_size);
a1facbec 16460 mips_clear_insn_labels ();
252b5132
RH
16461}
16462
16463static void
17a2f251 16464s_float_cons (int type)
252b5132 16465{
a8dbcb85
TS
16466 segment_info_type *si = seg_info (now_seg);
16467 struct insn_label_list *l = si->label_list;
252b5132 16468
7d10b47d 16469 mips_emit_delays ();
252b5132
RH
16470
16471 if (auto_align)
49309057
ILT
16472 {
16473 if (type == 'd')
462427c4 16474 mips_align (3, 0, l);
49309057 16475 else
462427c4 16476 mips_align (2, 0, l);
49309057 16477 }
252b5132 16478
252b5132 16479 float_cons (type);
a1facbec 16480 mips_clear_insn_labels ();
252b5132
RH
16481}
16482
16483/* Handle .globl. We need to override it because on Irix 5 you are
16484 permitted to say
16485 .globl foo .text
16486 where foo is an undefined symbol, to mean that foo should be
16487 considered to be the address of a function. */
16488
16489static void
17a2f251 16490s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
16491{
16492 char *name;
16493 int c;
16494 symbolS *symbolP;
252b5132 16495
8a06b769 16496 do
252b5132 16497 {
d02603dc 16498 c = get_symbol_name (&name);
8a06b769
TS
16499 symbolP = symbol_find_or_make (name);
16500 S_SET_EXTERNAL (symbolP);
16501
252b5132 16502 *input_line_pointer = c;
d02603dc 16503 SKIP_WHITESPACE_AFTER_NAME ();
252b5132 16504
8a06b769
TS
16505 if (!is_end_of_line[(unsigned char) *input_line_pointer]
16506 && (*input_line_pointer != ','))
16507 {
16508 char *secname;
16509 asection *sec;
16510
d02603dc 16511 c = get_symbol_name (&secname);
8a06b769
TS
16512 sec = bfd_get_section_by_name (stdoutput, secname);
16513 if (sec == NULL)
16514 as_bad (_("%s: no such section"), secname);
d02603dc 16515 (void) restore_line_pointer (c);
8a06b769
TS
16516
16517 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
d69cd47e 16518 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
8a06b769
TS
16519 }
16520
8a06b769
TS
16521 c = *input_line_pointer;
16522 if (c == ',')
16523 {
16524 input_line_pointer++;
16525 SKIP_WHITESPACE ();
16526 if (is_end_of_line[(unsigned char) *input_line_pointer])
16527 c = '\n';
16528 }
16529 }
16530 while (c == ',');
252b5132 16531
252b5132
RH
16532 demand_empty_rest_of_line ();
16533}
16534
d69cd47e
AM
16535#ifdef TE_IRIX
16536/* The Irix 5 and 6 assemblers set the type of any common symbol and
16537 any undefined non-function symbol to STT_OBJECT. We try to be
16538 compatible, since newer Irix 5 and 6 linkers care. */
16539
16540void
16541mips_frob_symbol (symbolS *symp ATTRIBUTE_UNUSED)
16542{
16543 /* This late in assembly we can set BSF_OBJECT indiscriminately
16544 and let elf.c:swap_out_syms sort out the symbol type. */
16545 flagword *flags = &symbol_get_bfdsym (symp)->flags;
16546 if ((*flags & (BSF_GLOBAL | BSF_WEAK)) != 0
16547 || !S_IS_DEFINED (symp))
16548 *flags |= BSF_OBJECT;
16549}
16550#endif
16551
252b5132 16552static void
17a2f251 16553s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
16554{
16555 char *opt;
16556 char c;
16557
d02603dc 16558 c = get_symbol_name (&opt);
252b5132
RH
16559
16560 if (*opt == 'O')
16561 {
16562 /* FIXME: What does this mean? */
16563 }
41a1578e 16564 else if (strncmp (opt, "pic", 3) == 0 && ISDIGIT (opt[3]) && opt[4] == '\0')
252b5132
RH
16565 {
16566 int i;
16567
16568 i = atoi (opt + 3);
668c5ebc
MR
16569 if (i != 0 && i != 2)
16570 as_bad (_(".option pic%d not supported"), i);
16571 else if (mips_pic == VXWORKS_PIC)
16572 as_bad (_(".option pic%d not supported in VxWorks PIC mode"), i);
16573 else if (i == 0)
252b5132
RH
16574 mips_pic = NO_PIC;
16575 else if (i == 2)
143d77c5 16576 {
8b828383 16577 mips_pic = SVR4_PIC;
143d77c5
EC
16578 mips_abicalls = TRUE;
16579 }
252b5132 16580
4d0d148d 16581 if (mips_pic == SVR4_PIC)
252b5132
RH
16582 {
16583 if (g_switch_seen && g_switch_value != 0)
16584 as_warn (_("-G may not be used with SVR4 PIC code"));
16585 g_switch_value = 0;
16586 bfd_set_gp_size (stdoutput, 0);
16587 }
16588 }
16589 else
1661c76c 16590 as_warn (_("unrecognized option \"%s\""), opt);
252b5132 16591
d02603dc 16592 (void) restore_line_pointer (c);
252b5132
RH
16593 demand_empty_rest_of_line ();
16594}
16595
16596/* This structure is used to hold a stack of .set values. */
16597
e972090a
NC
16598struct mips_option_stack
16599{
252b5132
RH
16600 struct mips_option_stack *next;
16601 struct mips_set_options options;
16602};
16603
16604static struct mips_option_stack *mips_opts_stack;
16605
22522f88
MR
16606/* Return status for .set/.module option handling. */
16607
16608enum code_option_type
16609{
16610 /* Unrecognized option. */
16611 OPTION_TYPE_BAD = -1,
16612
16613 /* Ordinary option. */
16614 OPTION_TYPE_NORMAL,
16615
16616 /* ISA changing option. */
16617 OPTION_TYPE_ISA
16618};
16619
16620/* Handle common .set/.module options. Return status indicating option
16621 type. */
16622
16623static enum code_option_type
919731af 16624parse_code_option (char * name)
252b5132 16625{
22522f88 16626 bfd_boolean isa_set = FALSE;
c6278170 16627 const struct mips_ase *ase;
22522f88 16628
919731af 16629 if (strncmp (name, "at=", 3) == 0)
741fe287
MR
16630 {
16631 char *s = name + 3;
16632
16633 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
1661c76c 16634 as_bad (_("unrecognized register name `%s'"), s);
741fe287 16635 }
252b5132 16636 else if (strcmp (name, "at") == 0)
919731af 16637 mips_opts.at = ATREG;
252b5132 16638 else if (strcmp (name, "noat") == 0)
919731af 16639 mips_opts.at = ZERO;
252b5132 16640 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
919731af 16641 mips_opts.nomove = 0;
252b5132 16642 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
919731af 16643 mips_opts.nomove = 1;
252b5132 16644 else if (strcmp (name, "bopt") == 0)
919731af 16645 mips_opts.nobopt = 0;
252b5132 16646 else if (strcmp (name, "nobopt") == 0)
919731af 16647 mips_opts.nobopt = 1;
ad3fea08 16648 else if (strcmp (name, "gp=32") == 0)
bad1aba3 16649 mips_opts.gp = 32;
ad3fea08 16650 else if (strcmp (name, "gp=64") == 0)
919731af 16651 mips_opts.gp = 64;
ad3fea08 16652 else if (strcmp (name, "fp=32") == 0)
0b35dfee 16653 mips_opts.fp = 32;
351cdf24
MF
16654 else if (strcmp (name, "fp=xx") == 0)
16655 mips_opts.fp = 0;
ad3fea08 16656 else if (strcmp (name, "fp=64") == 0)
919731af 16657 mips_opts.fp = 64;
037b32b9
AN
16658 else if (strcmp (name, "softfloat") == 0)
16659 mips_opts.soft_float = 1;
16660 else if (strcmp (name, "hardfloat") == 0)
16661 mips_opts.soft_float = 0;
16662 else if (strcmp (name, "singlefloat") == 0)
16663 mips_opts.single_float = 1;
16664 else if (strcmp (name, "doublefloat") == 0)
16665 mips_opts.single_float = 0;
351cdf24
MF
16666 else if (strcmp (name, "nooddspreg") == 0)
16667 mips_opts.oddspreg = 0;
16668 else if (strcmp (name, "oddspreg") == 0)
16669 mips_opts.oddspreg = 1;
252b5132
RH
16670 else if (strcmp (name, "mips16") == 0
16671 || strcmp (name, "MIPS-16") == 0)
919731af 16672 mips_opts.mips16 = 1;
252b5132
RH
16673 else if (strcmp (name, "nomips16") == 0
16674 || strcmp (name, "noMIPS-16") == 0)
16675 mips_opts.mips16 = 0;
df58fc94 16676 else if (strcmp (name, "micromips") == 0)
919731af 16677 mips_opts.micromips = 1;
df58fc94
RS
16678 else if (strcmp (name, "nomicromips") == 0)
16679 mips_opts.micromips = 0;
c6278170
RS
16680 else if (name[0] == 'n'
16681 && name[1] == 'o'
16682 && (ase = mips_lookup_ase (name + 2)))
919731af 16683 mips_set_ase (ase, &mips_opts, FALSE);
c6278170 16684 else if ((ase = mips_lookup_ase (name)))
919731af 16685 mips_set_ase (ase, &mips_opts, TRUE);
1a2c1fad 16686 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 16687 {
1a2c1fad
CD
16688 /* Permit the user to change the ISA and architecture on the fly.
16689 Needless to say, misuse can cause serious problems. */
919731af 16690 if (strncmp (name, "arch=", 5) == 0)
1a2c1fad
CD
16691 {
16692 const struct mips_cpu_info *p;
16693
919731af 16694 p = mips_parse_cpu ("internal use", name + 5);
1a2c1fad
CD
16695 if (!p)
16696 as_bad (_("unknown architecture %s"), name + 5);
16697 else
16698 {
16699 mips_opts.arch = p->cpu;
16700 mips_opts.isa = p->isa;
22522f88 16701 isa_set = TRUE;
3315614d 16702 mips_opts.init_ase = p->ase;
1a2c1fad
CD
16703 }
16704 }
81a21e38
TS
16705 else if (strncmp (name, "mips", 4) == 0)
16706 {
16707 const struct mips_cpu_info *p;
16708
919731af 16709 p = mips_parse_cpu ("internal use", name);
81a21e38
TS
16710 if (!p)
16711 as_bad (_("unknown ISA level %s"), name + 4);
16712 else
16713 {
16714 mips_opts.arch = p->cpu;
16715 mips_opts.isa = p->isa;
22522f88 16716 isa_set = TRUE;
3315614d 16717 mips_opts.init_ase = p->ase;
81a21e38
TS
16718 }
16719 }
af7ee8bf 16720 else
81a21e38 16721 as_bad (_("unknown ISA or architecture %s"), name);
252b5132
RH
16722 }
16723 else if (strcmp (name, "autoextend") == 0)
16724 mips_opts.noautoextend = 0;
16725 else if (strcmp (name, "noautoextend") == 0)
16726 mips_opts.noautoextend = 1;
833794fc
MR
16727 else if (strcmp (name, "insn32") == 0)
16728 mips_opts.insn32 = TRUE;
16729 else if (strcmp (name, "noinsn32") == 0)
16730 mips_opts.insn32 = FALSE;
919731af 16731 else if (strcmp (name, "sym32") == 0)
16732 mips_opts.sym32 = TRUE;
16733 else if (strcmp (name, "nosym32") == 0)
16734 mips_opts.sym32 = FALSE;
16735 else
22522f88
MR
16736 return OPTION_TYPE_BAD;
16737
16738 return isa_set ? OPTION_TYPE_ISA : OPTION_TYPE_NORMAL;
919731af 16739}
16740
16741/* Handle the .set pseudo-op. */
16742
16743static void
16744s_mipsset (int x ATTRIBUTE_UNUSED)
16745{
22522f88 16746 enum code_option_type type = OPTION_TYPE_NORMAL;
919731af 16747 char *name = input_line_pointer, ch;
919731af 16748
16749 file_mips_check_options ();
16750
16751 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16752 ++input_line_pointer;
16753 ch = *input_line_pointer;
16754 *input_line_pointer = '\0';
16755
16756 if (strchr (name, ','))
16757 {
16758 /* Generic ".set" directive; use the generic handler. */
16759 *input_line_pointer = ch;
16760 input_line_pointer = name;
16761 s_set (0);
16762 return;
16763 }
16764
16765 if (strcmp (name, "reorder") == 0)
16766 {
16767 if (mips_opts.noreorder)
16768 end_noreorder ();
16769 }
16770 else if (strcmp (name, "noreorder") == 0)
16771 {
16772 if (!mips_opts.noreorder)
16773 start_noreorder ();
16774 }
16775 else if (strcmp (name, "macro") == 0)
16776 mips_opts.warn_about_macros = 0;
16777 else if (strcmp (name, "nomacro") == 0)
16778 {
16779 if (mips_opts.noreorder == 0)
16780 as_bad (_("`noreorder' must be set before `nomacro'"));
16781 mips_opts.warn_about_macros = 1;
16782 }
16783 else if (strcmp (name, "gp=default") == 0)
16784 mips_opts.gp = file_mips_opts.gp;
16785 else if (strcmp (name, "fp=default") == 0)
16786 mips_opts.fp = file_mips_opts.fp;
16787 else if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
16788 {
16789 mips_opts.isa = file_mips_opts.isa;
16790 mips_opts.arch = file_mips_opts.arch;
3315614d 16791 mips_opts.init_ase = file_mips_opts.init_ase;
919731af 16792 mips_opts.gp = file_mips_opts.gp;
16793 mips_opts.fp = file_mips_opts.fp;
16794 }
252b5132
RH
16795 else if (strcmp (name, "push") == 0)
16796 {
16797 struct mips_option_stack *s;
16798
325801bd 16799 s = XNEW (struct mips_option_stack);
252b5132
RH
16800 s->next = mips_opts_stack;
16801 s->options = mips_opts;
16802 mips_opts_stack = s;
16803 }
16804 else if (strcmp (name, "pop") == 0)
16805 {
16806 struct mips_option_stack *s;
16807
16808 s = mips_opts_stack;
16809 if (s == NULL)
16810 as_bad (_(".set pop with no .set push"));
16811 else
16812 {
16813 /* If we're changing the reorder mode we need to handle
16814 delay slots correctly. */
16815 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 16816 start_noreorder ();
252b5132 16817 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 16818 end_noreorder ();
252b5132
RH
16819
16820 mips_opts = s->options;
16821 mips_opts_stack = s->next;
16822 free (s);
16823 }
16824 }
22522f88
MR
16825 else
16826 {
16827 type = parse_code_option (name);
16828 if (type == OPTION_TYPE_BAD)
16829 as_warn (_("tried to set unrecognized symbol: %s\n"), name);
16830 }
919731af 16831
16832 /* The use of .set [arch|cpu]= historically 'fixes' the width of gp and fp
16833 registers based on what is supported by the arch/cpu. */
22522f88 16834 if (type == OPTION_TYPE_ISA)
e6559e01 16835 {
919731af 16836 switch (mips_opts.isa)
16837 {
16838 case 0:
16839 break;
16840 case ISA_MIPS1:
351cdf24
MF
16841 /* MIPS I cannot support FPXX. */
16842 mips_opts.fp = 32;
16843 /* fall-through. */
919731af 16844 case ISA_MIPS2:
16845 case ISA_MIPS32:
16846 case ISA_MIPS32R2:
16847 case ISA_MIPS32R3:
16848 case ISA_MIPS32R5:
16849 mips_opts.gp = 32;
351cdf24
MF
16850 if (mips_opts.fp != 0)
16851 mips_opts.fp = 32;
919731af 16852 break;
7361da2c
AB
16853 case ISA_MIPS32R6:
16854 mips_opts.gp = 32;
16855 mips_opts.fp = 64;
16856 break;
919731af 16857 case ISA_MIPS3:
16858 case ISA_MIPS4:
16859 case ISA_MIPS5:
16860 case ISA_MIPS64:
16861 case ISA_MIPS64R2:
16862 case ISA_MIPS64R3:
16863 case ISA_MIPS64R5:
7361da2c 16864 case ISA_MIPS64R6:
919731af 16865 mips_opts.gp = 64;
351cdf24
MF
16866 if (mips_opts.fp != 0)
16867 {
16868 if (mips_opts.arch == CPU_R5900)
16869 mips_opts.fp = 32;
16870 else
16871 mips_opts.fp = 64;
16872 }
919731af 16873 break;
16874 default:
16875 as_bad (_("unknown ISA level %s"), name + 4);
16876 break;
16877 }
e6559e01 16878 }
919731af 16879
16880 mips_check_options (&mips_opts, FALSE);
16881
16882 mips_check_isa_supports_ases ();
16883 *input_line_pointer = ch;
16884 demand_empty_rest_of_line ();
16885}
16886
16887/* Handle the .module pseudo-op. */
16888
16889static void
16890s_module (int ignore ATTRIBUTE_UNUSED)
16891{
16892 char *name = input_line_pointer, ch;
16893
16894 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16895 ++input_line_pointer;
16896 ch = *input_line_pointer;
16897 *input_line_pointer = '\0';
16898
16899 if (!file_mips_opts_checked)
252b5132 16900 {
22522f88 16901 if (parse_code_option (name) == OPTION_TYPE_BAD)
919731af 16902 as_bad (_(".module used with unrecognized symbol: %s\n"), name);
16903
16904 /* Update module level settings from mips_opts. */
16905 file_mips_opts = mips_opts;
252b5132 16906 }
919731af 16907 else
16908 as_bad (_(".module is not permitted after generating code"));
16909
252b5132
RH
16910 *input_line_pointer = ch;
16911 demand_empty_rest_of_line ();
16912}
16913
16914/* Handle the .abicalls pseudo-op. I believe this is equivalent to
16915 .option pic2. It means to generate SVR4 PIC calls. */
16916
16917static void
17a2f251 16918s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16919{
16920 mips_pic = SVR4_PIC;
143d77c5 16921 mips_abicalls = TRUE;
4d0d148d
TS
16922
16923 if (g_switch_seen && g_switch_value != 0)
16924 as_warn (_("-G may not be used with SVR4 PIC code"));
16925 g_switch_value = 0;
16926
252b5132
RH
16927 bfd_set_gp_size (stdoutput, 0);
16928 demand_empty_rest_of_line ();
16929}
16930
16931/* Handle the .cpload pseudo-op. This is used when generating SVR4
16932 PIC code. It sets the $gp register for the function based on the
16933 function address, which is in the register named in the argument.
16934 This uses a relocation against _gp_disp, which is handled specially
16935 by the linker. The result is:
16936 lui $gp,%hi(_gp_disp)
16937 addiu $gp,$gp,%lo(_gp_disp)
16938 addu $gp,$gp,.cpload argument
aa6975fb
ILT
16939 The .cpload argument is normally $25 == $t9.
16940
16941 The -mno-shared option changes this to:
bbe506e8
TS
16942 lui $gp,%hi(__gnu_local_gp)
16943 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
16944 and the argument is ignored. This saves an instruction, but the
16945 resulting code is not position independent; it uses an absolute
bbe506e8
TS
16946 address for __gnu_local_gp. Thus code assembled with -mno-shared
16947 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
16948
16949static void
17a2f251 16950s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16951{
16952 expressionS ex;
aa6975fb
ILT
16953 int reg;
16954 int in_shared;
252b5132 16955
919731af 16956 file_mips_check_options ();
16957
6478892d
TS
16958 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16959 .cpload is ignored. */
16960 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16961 {
16962 s_ignore (0);
16963 return;
16964 }
16965
a276b80c
MR
16966 if (mips_opts.mips16)
16967 {
16968 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16969 ignore_rest_of_line ();
16970 return;
16971 }
16972
d3ecfc59 16973 /* .cpload should be in a .set noreorder section. */
252b5132
RH
16974 if (mips_opts.noreorder == 0)
16975 as_warn (_(".cpload not in noreorder section"));
16976
aa6975fb
ILT
16977 reg = tc_get_register (0);
16978
16979 /* If we need to produce a 64-bit address, we are better off using
16980 the default instruction sequence. */
aed1a261 16981 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 16982
252b5132 16983 ex.X_op = O_symbol;
bbe506e8
TS
16984 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16985 "__gnu_local_gp");
252b5132
RH
16986 ex.X_op_symbol = NULL;
16987 ex.X_add_number = 0;
16988
16989 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 16990 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 16991
8a75745d
MR
16992 mips_mark_labels ();
16993 mips_assembling_insn = TRUE;
16994
584892a6 16995 macro_start ();
67c0d1eb
RS
16996 macro_build_lui (&ex, mips_gp_register);
16997 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 16998 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
16999 if (in_shared)
17000 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
17001 mips_gp_register, reg);
584892a6 17002 macro_end ();
252b5132 17003
8a75745d 17004 mips_assembling_insn = FALSE;
252b5132
RH
17005 demand_empty_rest_of_line ();
17006}
17007
6478892d
TS
17008/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
17009 .cpsetup $reg1, offset|$reg2, label
17010
17011 If offset is given, this results in:
17012 sd $gp, offset($sp)
956cd1d6 17013 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
17014 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
17015 daddu $gp, $gp, $reg1
6478892d
TS
17016
17017 If $reg2 is given, this results in:
40fc1451 17018 or $reg2, $gp, $0
956cd1d6 17019 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
17020 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
17021 daddu $gp, $gp, $reg1
aa6975fb
ILT
17022 $reg1 is normally $25 == $t9.
17023
17024 The -mno-shared option replaces the last three instructions with
17025 lui $gp,%hi(_gp)
54f4ddb3 17026 addiu $gp,$gp,%lo(_gp) */
aa6975fb 17027
6478892d 17028static void
17a2f251 17029s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
17030{
17031 expressionS ex_off;
17032 expressionS ex_sym;
17033 int reg1;
6478892d 17034
919731af 17035 file_mips_check_options ();
17036
8586fc66 17037 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
17038 We also need NewABI support. */
17039 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17040 {
17041 s_ignore (0);
17042 return;
17043 }
17044
a276b80c
MR
17045 if (mips_opts.mips16)
17046 {
17047 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
17048 ignore_rest_of_line ();
17049 return;
17050 }
17051
6478892d
TS
17052 reg1 = tc_get_register (0);
17053 SKIP_WHITESPACE ();
17054 if (*input_line_pointer != ',')
17055 {
17056 as_bad (_("missing argument separator ',' for .cpsetup"));
17057 return;
17058 }
17059 else
80245285 17060 ++input_line_pointer;
6478892d
TS
17061 SKIP_WHITESPACE ();
17062 if (*input_line_pointer == '$')
80245285
TS
17063 {
17064 mips_cpreturn_register = tc_get_register (0);
17065 mips_cpreturn_offset = -1;
17066 }
6478892d 17067 else
80245285
TS
17068 {
17069 mips_cpreturn_offset = get_absolute_expression ();
17070 mips_cpreturn_register = -1;
17071 }
6478892d
TS
17072 SKIP_WHITESPACE ();
17073 if (*input_line_pointer != ',')
17074 {
17075 as_bad (_("missing argument separator ',' for .cpsetup"));
17076 return;
17077 }
17078 else
f9419b05 17079 ++input_line_pointer;
6478892d 17080 SKIP_WHITESPACE ();
f21f8242 17081 expression (&ex_sym);
6478892d 17082
8a75745d
MR
17083 mips_mark_labels ();
17084 mips_assembling_insn = TRUE;
17085
584892a6 17086 macro_start ();
6478892d
TS
17087 if (mips_cpreturn_register == -1)
17088 {
17089 ex_off.X_op = O_constant;
17090 ex_off.X_add_symbol = NULL;
17091 ex_off.X_op_symbol = NULL;
17092 ex_off.X_add_number = mips_cpreturn_offset;
17093
67c0d1eb 17094 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 17095 BFD_RELOC_LO16, SP);
6478892d
TS
17096 }
17097 else
40fc1451 17098 move_register (mips_cpreturn_register, mips_gp_register);
6478892d 17099
aed1a261 17100 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb 17101 {
df58fc94 17102 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
aa6975fb
ILT
17103 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
17104 BFD_RELOC_HI16_S);
17105
17106 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
17107 mips_gp_register, -1, BFD_RELOC_GPREL16,
17108 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
17109
17110 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
17111 mips_gp_register, reg1);
17112 }
17113 else
17114 {
17115 expressionS ex;
17116
17117 ex.X_op = O_symbol;
4184909a 17118 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
17119 ex.X_op_symbol = NULL;
17120 ex.X_add_number = 0;
6e1304d8 17121
aa6975fb
ILT
17122 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
17123 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
17124
17125 macro_build_lui (&ex, mips_gp_register);
17126 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17127 mips_gp_register, BFD_RELOC_LO16);
17128 }
f21f8242 17129
584892a6 17130 macro_end ();
6478892d 17131
8a75745d 17132 mips_assembling_insn = FALSE;
6478892d
TS
17133 demand_empty_rest_of_line ();
17134}
17135
17136static void
17a2f251 17137s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d 17138{
919731af 17139 file_mips_check_options ();
17140
6478892d 17141 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
54f4ddb3 17142 .cplocal is ignored. */
6478892d
TS
17143 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17144 {
17145 s_ignore (0);
17146 return;
17147 }
17148
a276b80c
MR
17149 if (mips_opts.mips16)
17150 {
17151 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
17152 ignore_rest_of_line ();
17153 return;
17154 }
17155
6478892d 17156 mips_gp_register = tc_get_register (0);
85b51719 17157 demand_empty_rest_of_line ();
6478892d
TS
17158}
17159
252b5132
RH
17160/* Handle the .cprestore pseudo-op. This stores $gp into a given
17161 offset from $sp. The offset is remembered, and after making a PIC
17162 call $gp is restored from that location. */
17163
17164static void
17a2f251 17165s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
17166{
17167 expressionS ex;
252b5132 17168
919731af 17169 file_mips_check_options ();
17170
6478892d 17171 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 17172 .cprestore is ignored. */
6478892d 17173 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
17174 {
17175 s_ignore (0);
17176 return;
17177 }
17178
a276b80c
MR
17179 if (mips_opts.mips16)
17180 {
17181 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
17182 ignore_rest_of_line ();
17183 return;
17184 }
17185
252b5132 17186 mips_cprestore_offset = get_absolute_expression ();
7a621144 17187 mips_cprestore_valid = 1;
252b5132
RH
17188
17189 ex.X_op = O_constant;
17190 ex.X_add_symbol = NULL;
17191 ex.X_op_symbol = NULL;
17192 ex.X_add_number = mips_cprestore_offset;
17193
8a75745d
MR
17194 mips_mark_labels ();
17195 mips_assembling_insn = TRUE;
17196
584892a6 17197 macro_start ();
67c0d1eb
RS
17198 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
17199 SP, HAVE_64BIT_ADDRESSES);
584892a6 17200 macro_end ();
252b5132 17201
8a75745d 17202 mips_assembling_insn = FALSE;
252b5132
RH
17203 demand_empty_rest_of_line ();
17204}
17205
6478892d 17206/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 17207 was given in the preceding .cpsetup, it results in:
6478892d 17208 ld $gp, offset($sp)
76b3015f 17209
6478892d 17210 If a register $reg2 was given there, it results in:
40fc1451 17211 or $gp, $reg2, $0 */
54f4ddb3 17212
6478892d 17213static void
17a2f251 17214s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
17215{
17216 expressionS ex;
6478892d 17217
919731af 17218 file_mips_check_options ();
17219
6478892d
TS
17220 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
17221 We also need NewABI support. */
17222 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17223 {
17224 s_ignore (0);
17225 return;
17226 }
17227
a276b80c
MR
17228 if (mips_opts.mips16)
17229 {
17230 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
17231 ignore_rest_of_line ();
17232 return;
17233 }
17234
8a75745d
MR
17235 mips_mark_labels ();
17236 mips_assembling_insn = TRUE;
17237
584892a6 17238 macro_start ();
6478892d
TS
17239 if (mips_cpreturn_register == -1)
17240 {
17241 ex.X_op = O_constant;
17242 ex.X_add_symbol = NULL;
17243 ex.X_op_symbol = NULL;
17244 ex.X_add_number = mips_cpreturn_offset;
17245
67c0d1eb 17246 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
17247 }
17248 else
40fc1451
SD
17249 move_register (mips_gp_register, mips_cpreturn_register);
17250
584892a6 17251 macro_end ();
6478892d 17252
8a75745d 17253 mips_assembling_insn = FALSE;
6478892d
TS
17254 demand_empty_rest_of_line ();
17255}
17256
d0f13682
CLT
17257/* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
17258 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
17259 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
17260 debug information or MIPS16 TLS. */
741d6ea8
JM
17261
17262static void
d0f13682
CLT
17263s_tls_rel_directive (const size_t bytes, const char *dirstr,
17264 bfd_reloc_code_real_type rtype)
741d6ea8
JM
17265{
17266 expressionS ex;
17267 char *p;
17268
17269 expression (&ex);
17270
17271 if (ex.X_op != O_symbol)
17272 {
1661c76c 17273 as_bad (_("unsupported use of %s"), dirstr);
741d6ea8
JM
17274 ignore_rest_of_line ();
17275 }
17276
17277 p = frag_more (bytes);
17278 md_number_to_chars (p, 0, bytes);
d0f13682 17279 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
741d6ea8 17280 demand_empty_rest_of_line ();
de64cffd 17281 mips_clear_insn_labels ();
741d6ea8
JM
17282}
17283
17284/* Handle .dtprelword. */
17285
17286static void
17287s_dtprelword (int ignore ATTRIBUTE_UNUSED)
17288{
d0f13682 17289 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
741d6ea8
JM
17290}
17291
17292/* Handle .dtpreldword. */
17293
17294static void
17295s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
17296{
d0f13682
CLT
17297 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
17298}
17299
17300/* Handle .tprelword. */
17301
17302static void
17303s_tprelword (int ignore ATTRIBUTE_UNUSED)
17304{
17305 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
17306}
17307
17308/* Handle .tpreldword. */
17309
17310static void
17311s_tpreldword (int ignore ATTRIBUTE_UNUSED)
17312{
17313 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
741d6ea8
JM
17314}
17315
6478892d
TS
17316/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
17317 code. It sets the offset to use in gp_rel relocations. */
17318
17319static void
17a2f251 17320s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
17321{
17322 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
17323 We also need NewABI support. */
17324 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17325 {
17326 s_ignore (0);
17327 return;
17328 }
17329
def2e0dd 17330 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
17331
17332 demand_empty_rest_of_line ();
17333}
17334
252b5132
RH
17335/* Handle the .gpword pseudo-op. This is used when generating PIC
17336 code. It generates a 32 bit GP relative reloc. */
17337
17338static void
17a2f251 17339s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132 17340{
a8dbcb85
TS
17341 segment_info_type *si;
17342 struct insn_label_list *l;
252b5132
RH
17343 expressionS ex;
17344 char *p;
17345
17346 /* When not generating PIC code, this is treated as .word. */
17347 if (mips_pic != SVR4_PIC)
17348 {
17349 s_cons (2);
17350 return;
17351 }
17352
a8dbcb85
TS
17353 si = seg_info (now_seg);
17354 l = si->label_list;
7d10b47d 17355 mips_emit_delays ();
252b5132 17356 if (auto_align)
462427c4 17357 mips_align (2, 0, l);
252b5132
RH
17358
17359 expression (&ex);
a1facbec 17360 mips_clear_insn_labels ();
252b5132
RH
17361
17362 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17363 {
1661c76c 17364 as_bad (_("unsupported use of .gpword"));
252b5132
RH
17365 ignore_rest_of_line ();
17366 }
17367
17368 p = frag_more (4);
17a2f251 17369 md_number_to_chars (p, 0, 4);
b34976b6 17370 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 17371 BFD_RELOC_GPREL32);
252b5132
RH
17372
17373 demand_empty_rest_of_line ();
17374}
17375
10181a0d 17376static void
17a2f251 17377s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d 17378{
a8dbcb85
TS
17379 segment_info_type *si;
17380 struct insn_label_list *l;
10181a0d
AO
17381 expressionS ex;
17382 char *p;
17383
17384 /* When not generating PIC code, this is treated as .dword. */
17385 if (mips_pic != SVR4_PIC)
17386 {
17387 s_cons (3);
17388 return;
17389 }
17390
a8dbcb85
TS
17391 si = seg_info (now_seg);
17392 l = si->label_list;
7d10b47d 17393 mips_emit_delays ();
10181a0d 17394 if (auto_align)
462427c4 17395 mips_align (3, 0, l);
10181a0d
AO
17396
17397 expression (&ex);
a1facbec 17398 mips_clear_insn_labels ();
10181a0d
AO
17399
17400 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17401 {
1661c76c 17402 as_bad (_("unsupported use of .gpdword"));
10181a0d
AO
17403 ignore_rest_of_line ();
17404 }
17405
17406 p = frag_more (8);
17a2f251 17407 md_number_to_chars (p, 0, 8);
a105a300 17408 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
6e1304d8 17409 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
17410
17411 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8
RS
17412 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
17413 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
17414
17415 demand_empty_rest_of_line ();
17416}
17417
a3f278e2
CM
17418/* Handle the .ehword pseudo-op. This is used when generating unwinding
17419 tables. It generates a R_MIPS_EH reloc. */
17420
17421static void
17422s_ehword (int ignore ATTRIBUTE_UNUSED)
17423{
17424 expressionS ex;
17425 char *p;
17426
17427 mips_emit_delays ();
17428
17429 expression (&ex);
17430 mips_clear_insn_labels ();
17431
17432 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17433 {
1661c76c 17434 as_bad (_("unsupported use of .ehword"));
a3f278e2
CM
17435 ignore_rest_of_line ();
17436 }
17437
17438 p = frag_more (4);
17439 md_number_to_chars (p, 0, 4);
17440 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
2f0c68f2 17441 BFD_RELOC_32_PCREL);
a3f278e2
CM
17442
17443 demand_empty_rest_of_line ();
17444}
17445
252b5132
RH
17446/* Handle the .cpadd pseudo-op. This is used when dealing with switch
17447 tables in SVR4 PIC code. */
17448
17449static void
17a2f251 17450s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 17451{
252b5132
RH
17452 int reg;
17453
919731af 17454 file_mips_check_options ();
17455
10181a0d
AO
17456 /* This is ignored when not generating SVR4 PIC code. */
17457 if (mips_pic != SVR4_PIC)
252b5132
RH
17458 {
17459 s_ignore (0);
17460 return;
17461 }
17462
8a75745d
MR
17463 mips_mark_labels ();
17464 mips_assembling_insn = TRUE;
17465
252b5132 17466 /* Add $gp to the register named as an argument. */
584892a6 17467 macro_start ();
252b5132 17468 reg = tc_get_register (0);
67c0d1eb 17469 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 17470 macro_end ();
252b5132 17471
8a75745d 17472 mips_assembling_insn = FALSE;
bdaaa2e1 17473 demand_empty_rest_of_line ();
252b5132
RH
17474}
17475
17476/* Handle the .insn pseudo-op. This marks instruction labels in
df58fc94 17477 mips16/micromips mode. This permits the linker to handle them specially,
252b5132
RH
17478 such as generating jalx instructions when needed. We also make
17479 them odd for the duration of the assembly, in order to generate the
17480 right sort of code. We will make them even in the adjust_symtab
17481 routine, while leaving them marked. This is convenient for the
17482 debugger and the disassembler. The linker knows to make them odd
17483 again. */
17484
17485static void
17a2f251 17486s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 17487{
7bb01e2d
MR
17488 file_mips_check_options ();
17489 file_ase_mips16 |= mips_opts.mips16;
17490 file_ase_micromips |= mips_opts.micromips;
17491
df58fc94 17492 mips_mark_labels ();
252b5132
RH
17493
17494 demand_empty_rest_of_line ();
17495}
17496
ba92f887
MR
17497/* Handle the .nan pseudo-op. */
17498
17499static void
17500s_nan (int ignore ATTRIBUTE_UNUSED)
17501{
17502 static const char str_legacy[] = "legacy";
17503 static const char str_2008[] = "2008";
17504 size_t i;
17505
17506 for (i = 0; !is_end_of_line[(unsigned char) input_line_pointer[i]]; i++);
17507
17508 if (i == sizeof (str_2008) - 1
17509 && memcmp (input_line_pointer, str_2008, i) == 0)
7361da2c 17510 mips_nan2008 = 1;
ba92f887
MR
17511 else if (i == sizeof (str_legacy) - 1
17512 && memcmp (input_line_pointer, str_legacy, i) == 0)
7361da2c
AB
17513 {
17514 if (ISA_HAS_LEGACY_NAN (file_mips_opts.isa))
17515 mips_nan2008 = 0;
17516 else
17517 as_bad (_("`%s' does not support legacy NaN"),
17518 mips_cpu_info_from_isa (file_mips_opts.isa)->name);
17519 }
ba92f887 17520 else
1661c76c 17521 as_bad (_("bad .nan directive"));
ba92f887
MR
17522
17523 input_line_pointer += i;
17524 demand_empty_rest_of_line ();
17525}
17526
754e2bb9
RS
17527/* Handle a .stab[snd] directive. Ideally these directives would be
17528 implemented in a transparent way, so that removing them would not
17529 have any effect on the generated instructions. However, s_stab
17530 internally changes the section, so in practice we need to decide
17531 now whether the preceding label marks compressed code. We do not
17532 support changing the compression mode of a label after a .stab*
17533 directive, such as in:
17534
17535 foo:
134c0c8b 17536 .stabs ...
754e2bb9
RS
17537 .set mips16
17538
17539 so the current mode wins. */
252b5132
RH
17540
17541static void
17a2f251 17542s_mips_stab (int type)
252b5132 17543{
42c0794e 17544 file_mips_check_options ();
754e2bb9 17545 mips_mark_labels ();
252b5132
RH
17546 s_stab (type);
17547}
17548
54f4ddb3 17549/* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
252b5132
RH
17550
17551static void
17a2f251 17552s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
17553{
17554 char *name;
17555 int c;
17556 symbolS *symbolP;
17557 expressionS exp;
17558
d02603dc 17559 c = get_symbol_name (&name);
252b5132
RH
17560 symbolP = symbol_find_or_make (name);
17561 S_SET_WEAK (symbolP);
17562 *input_line_pointer = c;
17563
d02603dc 17564 SKIP_WHITESPACE_AFTER_NAME ();
252b5132
RH
17565
17566 if (! is_end_of_line[(unsigned char) *input_line_pointer])
17567 {
17568 if (S_IS_DEFINED (symbolP))
17569 {
20203fb9 17570 as_bad (_("ignoring attempt to redefine symbol %s"),
252b5132
RH
17571 S_GET_NAME (symbolP));
17572 ignore_rest_of_line ();
17573 return;
17574 }
bdaaa2e1 17575
252b5132
RH
17576 if (*input_line_pointer == ',')
17577 {
17578 ++input_line_pointer;
17579 SKIP_WHITESPACE ();
17580 }
bdaaa2e1 17581
252b5132
RH
17582 expression (&exp);
17583 if (exp.X_op != O_symbol)
17584 {
20203fb9 17585 as_bad (_("bad .weakext directive"));
98d3f06f 17586 ignore_rest_of_line ();
252b5132
RH
17587 return;
17588 }
49309057 17589 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
17590 }
17591
17592 demand_empty_rest_of_line ();
17593}
17594
17595/* Parse a register string into a number. Called from the ECOFF code
17596 to parse .frame. The argument is non-zero if this is the frame
17597 register, so that we can record it in mips_frame_reg. */
17598
17599int
17a2f251 17600tc_get_register (int frame)
252b5132 17601{
707bfff6 17602 unsigned int reg;
252b5132
RH
17603
17604 SKIP_WHITESPACE ();
707bfff6
TS
17605 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17606 reg = 0;
252b5132 17607 if (frame)
7a621144
DJ
17608 {
17609 mips_frame_reg = reg != 0 ? reg : SP;
17610 mips_frame_reg_valid = 1;
17611 mips_cprestore_valid = 0;
17612 }
252b5132
RH
17613 return reg;
17614}
17615
17616valueT
17a2f251 17617md_section_align (asection *seg, valueT addr)
252b5132 17618{
fd361982 17619 int align = bfd_section_alignment (seg);
252b5132 17620
f3ded42a
RS
17621 /* We don't need to align ELF sections to the full alignment.
17622 However, Irix 5 may prefer that we align them at least to a 16
17623 byte boundary. We don't bother to align the sections if we
17624 are targeted for an embedded system. */
17625 if (strncmp (TARGET_OS, "elf", 3) == 0)
17626 return addr;
17627 if (align > 4)
17628 align = 4;
252b5132 17629
8d3842cd 17630 return ((addr + (1 << align) - 1) & -(1 << align));
252b5132
RH
17631}
17632
17633/* Utility routine, called from above as well. If called while the
17634 input file is still being read, it's only an approximation. (For
17635 example, a symbol may later become defined which appeared to be
17636 undefined earlier.) */
17637
17638static int
17a2f251 17639nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
17640{
17641 if (sym == 0)
17642 return 0;
17643
4d0d148d 17644 if (g_switch_value > 0)
252b5132
RH
17645 {
17646 const char *symname;
17647 int change;
17648
c9914766 17649 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
17650 register. It can be if it is smaller than the -G size or if
17651 it is in the .sdata or .sbss section. Certain symbols can
c9914766 17652 not be referenced off the $gp, although it appears as though
252b5132
RH
17653 they can. */
17654 symname = S_GET_NAME (sym);
17655 if (symname != (const char *) NULL
17656 && (strcmp (symname, "eprol") == 0
17657 || strcmp (symname, "etext") == 0
17658 || strcmp (symname, "_gp") == 0
17659 || strcmp (symname, "edata") == 0
17660 || strcmp (symname, "_fbss") == 0
17661 || strcmp (symname, "_fdata") == 0
17662 || strcmp (symname, "_ftext") == 0
17663 || strcmp (symname, "end") == 0
17664 || strcmp (symname, "_gp_disp") == 0))
17665 change = 1;
17666 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17667 && (0
17668#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
17669 || (symbol_get_obj (sym)->ecoff_extern_size != 0
17670 && (symbol_get_obj (sym)->ecoff_extern_size
17671 <= g_switch_value))
252b5132
RH
17672#endif
17673 /* We must defer this decision until after the whole
17674 file has been read, since there might be a .extern
17675 after the first use of this symbol. */
17676 || (before_relaxing
17677#ifndef NO_ECOFF_DEBUGGING
49309057 17678 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
17679#endif
17680 && S_GET_VALUE (sym) == 0)
17681 || (S_GET_VALUE (sym) != 0
17682 && S_GET_VALUE (sym) <= g_switch_value)))
17683 change = 0;
17684 else
17685 {
17686 const char *segname;
17687
17688 segname = segment_name (S_GET_SEGMENT (sym));
9c2799c2 17689 gas_assert (strcmp (segname, ".lit8") != 0
252b5132
RH
17690 && strcmp (segname, ".lit4") != 0);
17691 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
17692 && strcmp (segname, ".sbss") != 0
17693 && strncmp (segname, ".sdata.", 7) != 0
d4dc2f22
TS
17694 && strncmp (segname, ".sbss.", 6) != 0
17695 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
fba2b7f9 17696 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
17697 }
17698 return change;
17699 }
17700 else
c9914766 17701 /* We are not optimizing for the $gp register. */
252b5132
RH
17702 return 1;
17703}
17704
5919d012
RS
17705
17706/* Return true if the given symbol should be considered local for SVR4 PIC. */
17707
17708static bfd_boolean
9e009953 17709pic_need_relax (symbolS *sym)
5919d012
RS
17710{
17711 asection *symsec;
5919d012
RS
17712
17713 /* Handle the case of a symbol equated to another symbol. */
17714 while (symbol_equated_reloc_p (sym))
17715 {
17716 symbolS *n;
17717
5f0fe04b 17718 /* It's possible to get a loop here in a badly written program. */
5919d012
RS
17719 n = symbol_get_value_expression (sym)->X_add_symbol;
17720 if (n == sym)
17721 break;
17722 sym = n;
17723 }
17724
df1f3cda
DD
17725 if (symbol_section_p (sym))
17726 return TRUE;
17727
5919d012
RS
17728 symsec = S_GET_SEGMENT (sym);
17729
5919d012 17730 /* This must duplicate the test in adjust_reloc_syms. */
45dfa85a
AM
17731 return (!bfd_is_und_section (symsec)
17732 && !bfd_is_abs_section (symsec)
5f0fe04b 17733 && !bfd_is_com_section (symsec)
5919d012 17734 /* A global or weak symbol is treated as external. */
f3ded42a 17735 && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
5919d012 17736}
14f72d45
MR
17737\f
17738/* Given a MIPS16 variant frag FRAGP and PC-relative operand PCREL_OP
17739 convert a section-relative value VAL to the equivalent PC-relative
17740 value. */
17741
17742static offsetT
17743mips16_pcrel_val (fragS *fragp, const struct mips_pcrel_operand *pcrel_op,
17744 offsetT val, long stretch)
17745{
17746 fragS *sym_frag;
17747 addressT addr;
17748
17749 gas_assert (pcrel_op->root.root.type == OP_PCREL);
17750
17751 sym_frag = symbol_get_frag (fragp->fr_symbol);
17752
17753 /* If the relax_marker of the symbol fragment differs from the
17754 relax_marker of this fragment, we have not yet adjusted the
17755 symbol fragment fr_address. We want to add in STRETCH in
17756 order to get a better estimate of the address. This
17757 particularly matters because of the shift bits. */
17758 if (stretch != 0 && sym_frag->relax_marker != fragp->relax_marker)
17759 {
17760 fragS *f;
17761
17762 /* Adjust stretch for any alignment frag. Note that if have
17763 been expanding the earlier code, the symbol may be
17764 defined in what appears to be an earlier frag. FIXME:
17765 This doesn't handle the fr_subtype field, which specifies
17766 a maximum number of bytes to skip when doing an
17767 alignment. */
17768 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
17769 {
17770 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17771 {
17772 if (stretch < 0)
17773 stretch = -(-stretch & ~((1 << (int) f->fr_offset) - 1));
17774 else
17775 stretch &= ~((1 << (int) f->fr_offset) - 1);
17776 if (stretch == 0)
17777 break;
17778 }
17779 }
17780 if (f != NULL)
17781 val += stretch;
17782 }
17783
17784 addr = fragp->fr_address + fragp->fr_fix;
17785
17786 /* The base address rules are complicated. The base address of
17787 a branch is the following instruction. The base address of a
17788 PC relative load or add is the instruction itself, but if it
17789 is in a delay slot (in which case it can not be extended) use
17790 the address of the instruction whose delay slot it is in. */
17791 if (pcrel_op->include_isa_bit)
17792 {
17793 addr += 2;
17794
17795 /* If we are currently assuming that this frag should be
17796 extended, then the current address is two bytes higher. */
17797 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17798 addr += 2;
17799
17800 /* Ignore the low bit in the target, since it will be set
17801 for a text label. */
17802 val &= -2;
17803 }
17804 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17805 addr -= 4;
17806 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17807 addr -= 2;
5919d012 17808
14f72d45
MR
17809 val -= addr & -(1 << pcrel_op->align_log2);
17810
17811 return val;
17812}
5919d012 17813
252b5132
RH
17814/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17815 extended opcode. SEC is the section the frag is in. */
17816
17817static int
17a2f251 17818mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132 17819{
3ccad066 17820 const struct mips_int_operand *operand;
252b5132 17821 offsetT val;
252b5132 17822 segT symsec;
14f72d45 17823 int type;
252b5132
RH
17824
17825 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17826 return 0;
17827 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17828 return 1;
17829
88a7ef16 17830 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132 17831 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
3ccad066 17832 operand = mips16_immed_operand (type, FALSE);
88a7ef16
MR
17833 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
17834 || (operand->root.type == OP_PCREL
17835 ? sec != symsec
17836 : !bfd_is_abs_section (symsec)))
17837 return 1;
252b5132 17838
88a7ef16 17839 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
252b5132 17840
3ccad066 17841 if (operand->root.type == OP_PCREL)
252b5132 17842 {
3ccad066 17843 const struct mips_pcrel_operand *pcrel_op;
3ccad066 17844 offsetT maxtiny;
252b5132 17845
1425c41d 17846 if (RELAX_MIPS16_ALWAYS_EXTENDED (fragp->fr_subtype))
88a7ef16 17847 return 1;
252b5132 17848
88a7ef16 17849 pcrel_op = (const struct mips_pcrel_operand *) operand;
14f72d45 17850 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
252b5132
RH
17851
17852 /* If any of the shifted bits are set, we must use an extended
17853 opcode. If the address depends on the size of this
17854 instruction, this can lead to a loop, so we arrange to always
88a7ef16
MR
17855 use an extended opcode. */
17856 if ((val & ((1 << operand->shift) - 1)) != 0)
252b5132
RH
17857 {
17858 fragp->fr_subtype =
1425c41d 17859 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
252b5132
RH
17860 return 1;
17861 }
17862
17863 /* If we are about to mark a frag as extended because the value
3ccad066
RS
17864 is precisely the next value above maxtiny, then there is a
17865 chance of an infinite loop as in the following code:
252b5132
RH
17866 la $4,foo
17867 .skip 1020
17868 .align 2
17869 foo:
17870 In this case when the la is extended, foo is 0x3fc bytes
17871 away, so the la can be shrunk, but then foo is 0x400 away, so
17872 the la must be extended. To avoid this loop, we mark the
17873 frag as extended if it was small, and is about to become
3ccad066
RS
17874 extended with the next value above maxtiny. */
17875 maxtiny = mips_int_operand_max (operand);
17876 if (val == maxtiny + (1 << operand->shift)
88a7ef16 17877 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
252b5132
RH
17878 {
17879 fragp->fr_subtype =
1425c41d 17880 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
252b5132
RH
17881 return 1;
17882 }
17883 }
252b5132 17884
3ccad066 17885 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
252b5132
RH
17886}
17887
8507b6e7
MR
17888/* Given a MIPS16 variant frag FRAGP, return non-zero if it needs
17889 macro expansion. SEC is the section the frag is in. We only
17890 support PC-relative instructions (LA, DLA, LW, LD) here, in
17891 non-PIC code using 32-bit addressing. */
17892
17893static int
17894mips16_macro_frag (fragS *fragp, asection *sec, long stretch)
17895{
17896 const struct mips_pcrel_operand *pcrel_op;
17897 const struct mips_int_operand *operand;
17898 offsetT val;
17899 segT symsec;
17900 int type;
17901
17902 gas_assert (!RELAX_MIPS16_USER_SMALL (fragp->fr_subtype));
17903
17904 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17905 return 0;
17906 if (!RELAX_MIPS16_SYM32 (fragp->fr_subtype))
17907 return 0;
17908
17909 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17910 switch (type)
17911 {
17912 case 'A':
17913 case 'B':
17914 case 'E':
17915 symsec = S_GET_SEGMENT (fragp->fr_symbol);
17916 if (bfd_is_abs_section (symsec))
17917 return 1;
17918 if (RELAX_MIPS16_PIC (fragp->fr_subtype))
17919 return 0;
17920 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE) || sec != symsec)
17921 return 1;
17922
17923 operand = mips16_immed_operand (type, TRUE);
17924 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17925 pcrel_op = (const struct mips_pcrel_operand *) operand;
17926 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
17927
17928 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
17929
17930 default:
17931 return 0;
17932 }
17933}
17934
4a6a3df4
AO
17935/* Compute the length of a branch sequence, and adjust the
17936 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
17937 worst-case length is computed, with UPDATE being used to indicate
17938 whether an unconditional (-1), branch-likely (+1) or regular (0)
17939 branch is to be computed. */
17940static int
17a2f251 17941relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 17942{
b34976b6 17943 bfd_boolean toofar;
4a6a3df4
AO
17944 int length;
17945
17946 if (fragp
17947 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 17948 && !S_IS_WEAK (fragp->fr_symbol)
4a6a3df4
AO
17949 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17950 {
17951 addressT addr;
17952 offsetT val;
17953
17954 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17955
17956 addr = fragp->fr_address + fragp->fr_fix + 4;
17957
17958 val -= addr;
17959
17960 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17961 }
4a6a3df4 17962 else
c1f61bd2
MR
17963 /* If the symbol is not defined or it's in a different segment,
17964 we emit the long sequence. */
b34976b6 17965 toofar = TRUE;
4a6a3df4
AO
17966
17967 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17968 fragp->fr_subtype
66b3e8da 17969 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
ce8ad872 17970 RELAX_BRANCH_PIC (fragp->fr_subtype),
66b3e8da 17971 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
17972 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17973 RELAX_BRANCH_LINK (fragp->fr_subtype),
17974 toofar);
17975
17976 length = 4;
17977 if (toofar)
17978 {
17979 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17980 length += 8;
17981
ce8ad872 17982 if (!fragp || RELAX_BRANCH_PIC (fragp->fr_subtype))
4a6a3df4
AO
17983 {
17984 /* Additional space for PIC loading of target address. */
17985 length += 8;
17986 if (mips_opts.isa == ISA_MIPS1)
17987 /* Additional space for $at-stabilizing nop. */
17988 length += 4;
17989 }
17990
17991 /* If branch is conditional. */
17992 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17993 length += 8;
17994 }
b34976b6 17995
4a6a3df4
AO
17996 return length;
17997}
17998
7bd374a4
MR
17999/* Get a FRAG's branch instruction delay slot size, either from the
18000 short-delay-slot bit of a branch-and-link instruction if AL is TRUE,
18001 or SHORT_INSN_SIZE otherwise. */
18002
18003static int
18004frag_branch_delay_slot_size (fragS *fragp, bfd_boolean al, int short_insn_size)
18005{
18006 char *buf = fragp->fr_literal + fragp->fr_fix;
18007
18008 if (al)
18009 return (read_compressed_insn (buf, 4) & 0x02000000) ? 2 : 4;
18010 else
18011 return short_insn_size;
18012}
18013
df58fc94
RS
18014/* Compute the length of a branch sequence, and adjust the
18015 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
18016 worst-case length is computed, with UPDATE being used to indicate
18017 whether an unconditional (-1), or regular (0) branch is to be
18018 computed. */
18019
18020static int
18021relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
18022{
7bd374a4
MR
18023 bfd_boolean insn32 = TRUE;
18024 bfd_boolean nods = TRUE;
ce8ad872 18025 bfd_boolean pic = TRUE;
7bd374a4
MR
18026 bfd_boolean al = TRUE;
18027 int short_insn_size;
df58fc94
RS
18028 bfd_boolean toofar;
18029 int length;
18030
7bd374a4
MR
18031 if (fragp)
18032 {
18033 insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
18034 nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
ce8ad872 18035 pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
7bd374a4
MR
18036 al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18037 }
18038 short_insn_size = insn32 ? 4 : 2;
18039
df58fc94
RS
18040 if (fragp
18041 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 18042 && !S_IS_WEAK (fragp->fr_symbol)
df58fc94
RS
18043 && sec == S_GET_SEGMENT (fragp->fr_symbol))
18044 {
18045 addressT addr;
18046 offsetT val;
18047
18048 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
18049 /* Ignore the low bit in the target, since it will be set
18050 for a text label. */
18051 if ((val & 1) != 0)
18052 --val;
18053
18054 addr = fragp->fr_address + fragp->fr_fix + 4;
18055
18056 val -= addr;
18057
18058 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
18059 }
df58fc94 18060 else
c1f61bd2
MR
18061 /* If the symbol is not defined or it's in a different segment,
18062 we emit the long sequence. */
df58fc94
RS
18063 toofar = TRUE;
18064
18065 if (fragp && update
18066 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18067 fragp->fr_subtype = (toofar
18068 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
18069 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
18070
18071 length = 4;
18072 if (toofar)
18073 {
18074 bfd_boolean compact_known = fragp != NULL;
18075 bfd_boolean compact = FALSE;
18076 bfd_boolean uncond;
18077
df58fc94 18078 if (fragp)
8484fb75
MR
18079 {
18080 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18081 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
8484fb75 18082 }
df58fc94
RS
18083 else
18084 uncond = update < 0;
18085
18086 /* If label is out of range, we turn branch <br>:
18087
18088 <br> label # 4 bytes
18089 0:
18090
18091 into:
18092
18093 j label # 4 bytes
8484fb75
MR
18094 nop # 2/4 bytes if
18095 # compact && (!PIC || insn32)
df58fc94
RS
18096 0:
18097 */
ce8ad872 18098 if ((!pic || insn32) && (!compact_known || compact))
8484fb75 18099 length += short_insn_size;
df58fc94
RS
18100
18101 /* If assembling PIC code, we further turn:
18102
18103 j label # 4 bytes
18104
18105 into:
18106
18107 lw/ld at, %got(label)(gp) # 4 bytes
18108 d/addiu at, %lo(label) # 4 bytes
8484fb75 18109 jr/c at # 2/4 bytes
df58fc94 18110 */
ce8ad872 18111 if (pic)
8484fb75 18112 length += 4 + short_insn_size;
df58fc94 18113
7bd374a4
MR
18114 /* Add an extra nop if the jump has no compact form and we need
18115 to fill the delay slot. */
ce8ad872 18116 if ((!pic || al) && nods)
7bd374a4
MR
18117 length += (fragp
18118 ? frag_branch_delay_slot_size (fragp, al, short_insn_size)
18119 : short_insn_size);
18120
df58fc94
RS
18121 /* If branch <br> is conditional, we prepend negated branch <brneg>:
18122
18123 <brneg> 0f # 4 bytes
8484fb75 18124 nop # 2/4 bytes if !compact
df58fc94
RS
18125 */
18126 if (!uncond)
8484fb75 18127 length += (compact_known && compact) ? 4 : 4 + short_insn_size;
df58fc94 18128 }
7bd374a4
MR
18129 else if (nods)
18130 {
18131 /* Add an extra nop to fill the delay slot. */
18132 gas_assert (fragp);
18133 length += frag_branch_delay_slot_size (fragp, al, short_insn_size);
18134 }
df58fc94
RS
18135
18136 return length;
18137}
18138
18139/* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
18140 bit accordingly. */
18141
18142static int
18143relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
18144{
18145 bfd_boolean toofar;
18146
df58fc94
RS
18147 if (fragp
18148 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 18149 && !S_IS_WEAK (fragp->fr_symbol)
df58fc94
RS
18150 && sec == S_GET_SEGMENT (fragp->fr_symbol))
18151 {
18152 addressT addr;
18153 offsetT val;
18154 int type;
18155
18156 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
18157 /* Ignore the low bit in the target, since it will be set
18158 for a text label. */
18159 if ((val & 1) != 0)
18160 --val;
18161
18162 /* Assume this is a 2-byte branch. */
18163 addr = fragp->fr_address + fragp->fr_fix + 2;
18164
18165 /* We try to avoid the infinite loop by not adding 2 more bytes for
18166 long branches. */
18167
18168 val -= addr;
18169
18170 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
18171 if (type == 'D')
18172 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
18173 else if (type == 'E')
18174 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
18175 else
18176 abort ();
18177 }
18178 else
18179 /* If the symbol is not defined or it's in a different segment,
18180 we emit a normal 32-bit branch. */
18181 toofar = TRUE;
18182
18183 if (fragp && update
18184 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18185 fragp->fr_subtype
18186 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
18187 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
18188
18189 if (toofar)
18190 return 4;
18191
18192 return 2;
18193}
18194
252b5132
RH
18195/* Estimate the size of a frag before relaxing. Unless this is the
18196 mips16, we are not really relaxing here, and the final size is
18197 encoded in the subtype information. For the mips16, we have to
18198 decide whether we are using an extended opcode or not. */
18199
252b5132 18200int
17a2f251 18201md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 18202{
5919d012 18203 int change;
252b5132 18204
4a6a3df4
AO
18205 if (RELAX_BRANCH_P (fragp->fr_subtype))
18206 {
18207
b34976b6
AM
18208 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
18209
4a6a3df4
AO
18210 return fragp->fr_var;
18211 }
18212
252b5132 18213 if (RELAX_MIPS16_P (fragp->fr_subtype))
8507b6e7
MR
18214 {
18215 /* We don't want to modify the EXTENDED bit here; it might get us
18216 into infinite loops. We change it only in mips_relax_frag(). */
18217 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
25499ac7 18218 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 8 : 12;
8507b6e7
MR
18219 else
18220 return RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2;
18221 }
252b5132 18222
df58fc94
RS
18223 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18224 {
18225 int length = 4;
18226
18227 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
18228 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
18229 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
18230 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
18231 fragp->fr_var = length;
18232
18233 return length;
18234 }
18235
ce8ad872 18236 if (mips_pic == VXWORKS_PIC)
0a44bf69
RS
18237 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
18238 change = 0;
ce8ad872
MR
18239 else if (RELAX_PIC (fragp->fr_subtype))
18240 change = pic_need_relax (fragp->fr_symbol);
252b5132 18241 else
ce8ad872 18242 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132
RH
18243
18244 if (change)
18245 {
4d7206a2 18246 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 18247 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 18248 }
4d7206a2
RS
18249 else
18250 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
18251}
18252
18253/* This is called to see whether a reloc against a defined symbol
de7e6852 18254 should be converted into a reloc against a section. */
252b5132
RH
18255
18256int
17a2f251 18257mips_fix_adjustable (fixS *fixp)
252b5132 18258{
252b5132
RH
18259 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
18260 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
18261 return 0;
a161fe53 18262
252b5132
RH
18263 if (fixp->fx_addsy == NULL)
18264 return 1;
a161fe53 18265
2f0c68f2
CM
18266 /* Allow relocs used for EH tables. */
18267 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
18268 return 1;
18269
de7e6852
RS
18270 /* If symbol SYM is in a mergeable section, relocations of the form
18271 SYM + 0 can usually be made section-relative. The mergeable data
18272 is then identified by the section offset rather than by the symbol.
18273
18274 However, if we're generating REL LO16 relocations, the offset is split
33eaf5de 18275 between the LO16 and partnering high part relocation. The linker will
de7e6852
RS
18276 need to recalculate the complete offset in order to correctly identify
18277 the merge data.
18278
33eaf5de 18279 The linker has traditionally not looked for the partnering high part
de7e6852
RS
18280 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
18281 placed anywhere. Rather than break backwards compatibility by changing
18282 this, it seems better not to force the issue, and instead keep the
18283 original symbol. This will work with either linker behavior. */
738e5348 18284 if ((lo16_reloc_p (fixp->fx_r_type)
704803a9 18285 || reloc_needs_lo_p (fixp->fx_r_type))
de7e6852
RS
18286 && HAVE_IN_PLACE_ADDENDS
18287 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
18288 return 0;
18289
97f50151
MR
18290 /* There is no place to store an in-place offset for JALR relocations. */
18291 if (jalr_reloc_p (fixp->fx_r_type) && HAVE_IN_PLACE_ADDENDS)
18292 return 0;
18293
18294 /* Likewise an in-range offset of limited PC-relative relocations may
2de39019 18295 overflow the in-place relocatable field if recalculated against the
7361da2c
AB
18296 start address of the symbol's containing section.
18297
18298 Also, PC relative relocations for MIPS R6 need to be symbol rather than
18299 section relative to allow linker relaxations to be performed later on. */
97f50151 18300 if (limited_pcrel_reloc_p (fixp->fx_r_type)
912815f0 18301 && (HAVE_IN_PLACE_ADDENDS || ISA_IS_R6 (file_mips_opts.isa)))
1180b5a4
RS
18302 return 0;
18303
b314ec0e
RS
18304 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
18305 to a floating-point stub. The same is true for non-R_MIPS16_26
18306 relocations against MIPS16 functions; in this case, the stub becomes
18307 the function's canonical address.
18308
18309 Floating-point stubs are stored in unique .mips16.call.* or
18310 .mips16.fn.* sections. If a stub T for function F is in section S,
18311 the first relocation in section S must be against F; this is how the
18312 linker determines the target function. All relocations that might
18313 resolve to T must also be against F. We therefore have the following
18314 restrictions, which are given in an intentionally-redundant way:
18315
18316 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
18317 symbols.
18318
18319 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
18320 if that stub might be used.
18321
18322 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
18323 symbols.
18324
18325 4. We cannot reduce a stub's relocations against MIPS16 symbols if
18326 that stub might be used.
18327
18328 There is a further restriction:
18329
df58fc94 18330 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
0e9c5a5c 18331 R_MICROMIPS_26_S1) or branch relocations (R_MIPS_PC26_S2,
c9775dde
MR
18332 R_MIPS_PC21_S2, R_MIPS_PC16, R_MIPS16_PC16_S1,
18333 R_MICROMIPS_PC16_S1, R_MICROMIPS_PC10_S1 or R_MICROMIPS_PC7_S1)
18334 against MIPS16 or microMIPS symbols because we need to keep the
18335 MIPS16 or microMIPS symbol for the purpose of mode mismatch
a6ebf616
MR
18336 detection and JAL or BAL to JALX instruction conversion in the
18337 linker.
b314ec0e 18338
df58fc94 18339 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
507dcb32 18340 against a MIPS16 symbol. We deal with (5) by additionally leaving
0e9c5a5c 18341 alone any jump and branch relocations against a microMIPS symbol.
b314ec0e
RS
18342
18343 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
18344 relocation against some symbol R, no relocation against R may be
18345 reduced. (Note that this deals with (2) as well as (1) because
18346 relocations against global symbols will never be reduced on ELF
18347 targets.) This approach is a little simpler than trying to detect
18348 stub sections, and gives the "all or nothing" per-symbol consistency
18349 that we have for MIPS16 symbols. */
f3ded42a 18350 if (fixp->fx_subsy == NULL
30c09090 18351 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
44d3da23 18352 || (ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
0e9c5a5c
MR
18353 && (jmp_reloc_p (fixp->fx_r_type)
18354 || b_reloc_p (fixp->fx_r_type)))
44d3da23 18355 || *symbol_get_tc (fixp->fx_addsy)))
252b5132 18356 return 0;
a161fe53 18357
252b5132
RH
18358 return 1;
18359}
18360
18361/* Translate internal representation of relocation info to BFD target
18362 format. */
18363
18364arelent **
17a2f251 18365tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
18366{
18367 static arelent *retval[4];
18368 arelent *reloc;
18369 bfd_reloc_code_real_type code;
18370
4b0cff4e 18371 memset (retval, 0, sizeof(retval));
325801bd
TS
18372 reloc = retval[0] = XCNEW (arelent);
18373 reloc->sym_ptr_ptr = XNEW (asymbol *);
49309057 18374 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
18375 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
18376
bad36eac
DJ
18377 if (fixp->fx_pcrel)
18378 {
df58fc94 18379 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
c9775dde 18380 || fixp->fx_r_type == BFD_RELOC_MIPS16_16_PCREL_S1
df58fc94
RS
18381 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
18382 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
b47468a6 18383 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
7361da2c
AB
18384 || fixp->fx_r_type == BFD_RELOC_32_PCREL
18385 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
18386 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
18387 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
18388 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
18389 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
18390 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL);
bad36eac
DJ
18391
18392 /* At this point, fx_addnumber is "symbol offset - pcrel address".
18393 Relocations want only the symbol offset. */
51f6035b
MR
18394 switch (fixp->fx_r_type)
18395 {
18396 case BFD_RELOC_MIPS_18_PCREL_S3:
18397 reloc->addend = fixp->fx_addnumber + (reloc->address & ~7);
18398 break;
18399 default:
18400 reloc->addend = fixp->fx_addnumber + reloc->address;
18401 break;
18402 }
bad36eac 18403 }
17c6c9d9
MR
18404 else if (HAVE_IN_PLACE_ADDENDS
18405 && fixp->fx_r_type == BFD_RELOC_MICROMIPS_JMP
18406 && (read_compressed_insn (fixp->fx_frag->fr_literal
18407 + fixp->fx_where, 4) >> 26) == 0x3c)
18408 {
18409 /* Shift is 2, unusually, for microMIPS JALX. Adjust the in-place
18410 addend accordingly. */
18411 reloc->addend = fixp->fx_addnumber >> 1;
18412 }
bad36eac
DJ
18413 else
18414 reloc->addend = fixp->fx_addnumber;
252b5132 18415
438c16b8
TS
18416 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
18417 entry to be used in the relocation's section offset. */
18418 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
18419 {
18420 reloc->address = reloc->addend;
18421 reloc->addend = 0;
18422 }
18423
252b5132 18424 code = fixp->fx_r_type;
252b5132 18425
bad36eac 18426 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
252b5132
RH
18427 if (reloc->howto == NULL)
18428 {
18429 as_bad_where (fixp->fx_file, fixp->fx_line,
1661c76c
RS
18430 _("cannot represent %s relocation in this object file"
18431 " format"),
252b5132
RH
18432 bfd_get_reloc_code_name (code));
18433 retval[0] = NULL;
18434 }
18435
18436 return retval;
18437}
18438
18439/* Relax a machine dependent frag. This returns the amount by which
18440 the current size of the frag should change. */
18441
18442int
17a2f251 18443mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 18444{
4a6a3df4
AO
18445 if (RELAX_BRANCH_P (fragp->fr_subtype))
18446 {
18447 offsetT old_var = fragp->fr_var;
b34976b6
AM
18448
18449 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
18450
18451 return fragp->fr_var - old_var;
18452 }
18453
df58fc94
RS
18454 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18455 {
18456 offsetT old_var = fragp->fr_var;
18457 offsetT new_var = 4;
18458
18459 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
18460 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
18461 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
18462 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
18463 fragp->fr_var = new_var;
18464
18465 return new_var - old_var;
18466 }
18467
252b5132
RH
18468 if (! RELAX_MIPS16_P (fragp->fr_subtype))
18469 return 0;
18470
8507b6e7 18471 if (!mips16_extended_frag (fragp, sec, stretch))
252b5132 18472 {
8507b6e7
MR
18473 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18474 {
18475 fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
25499ac7 18476 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -6 : -10;
8507b6e7
MR
18477 }
18478 else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18479 {
18480 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18481 return -2;
18482 }
18483 else
18484 return 0;
18485 }
18486 else if (!mips16_macro_frag (fragp, sec, stretch))
18487 {
18488 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18489 {
18490 fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
18491 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
25499ac7 18492 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -4 : -8;
8507b6e7
MR
18493 }
18494 else if (!RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18495 {
18496 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
18497 return 2;
18498 }
18499 else
252b5132 18500 return 0;
252b5132
RH
18501 }
18502 else
18503 {
8507b6e7 18504 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
252b5132 18505 return 0;
8507b6e7
MR
18506 else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18507 {
18508 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18509 fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
25499ac7 18510 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 4 : 8;
8507b6e7
MR
18511 }
18512 else
18513 {
18514 fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
25499ac7 18515 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 6 : 10;
8507b6e7 18516 }
252b5132
RH
18517 }
18518
18519 return 0;
18520}
18521
18522/* Convert a machine dependent frag. */
18523
18524void
17a2f251 18525md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 18526{
4a6a3df4
AO
18527 if (RELAX_BRANCH_P (fragp->fr_subtype))
18528 {
4d68580a 18529 char *buf;
4a6a3df4 18530 unsigned long insn;
4a6a3df4 18531 fixS *fixp;
b34976b6 18532
4d68580a
RS
18533 buf = fragp->fr_literal + fragp->fr_fix;
18534 insn = read_insn (buf);
b34976b6 18535
4a6a3df4
AO
18536 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
18537 {
18538 /* We generate a fixup instead of applying it right now
18539 because, if there are linker relaxations, we're going to
18540 need the relocations. */
bbd27b76
MR
18541 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18542 fragp->fr_symbol, fragp->fr_offset,
18543 TRUE, BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
18544 fixp->fx_file = fragp->fr_file;
18545 fixp->fx_line = fragp->fr_line;
b34976b6 18546
4d68580a 18547 buf = write_insn (buf, insn);
4a6a3df4
AO
18548 }
18549 else
18550 {
18551 int i;
18552
18553 as_warn_where (fragp->fr_file, fragp->fr_line,
1661c76c 18554 _("relaxed out-of-range branch into a jump"));
4a6a3df4
AO
18555
18556 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
18557 goto uncond;
18558
18559 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18560 {
18561 /* Reverse the branch. */
18562 switch ((insn >> 28) & 0xf)
18563 {
18564 case 4:
56d438b1
CF
18565 if ((insn & 0xff000000) == 0x47000000
18566 || (insn & 0xff600000) == 0x45600000)
18567 {
18568 /* BZ.df/BNZ.df, BZ.V/BNZ.V can have the condition
18569 reversed by tweaking bit 23. */
18570 insn ^= 0x00800000;
18571 }
18572 else
18573 {
18574 /* bc[0-3][tf]l? instructions can have the condition
18575 reversed by tweaking a single TF bit, and their
18576 opcodes all have 0x4???????. */
18577 gas_assert ((insn & 0xf3e00000) == 0x41000000);
18578 insn ^= 0x00010000;
18579 }
4a6a3df4
AO
18580 break;
18581
18582 case 0:
18583 /* bltz 0x04000000 bgez 0x04010000
54f4ddb3 18584 bltzal 0x04100000 bgezal 0x04110000 */
9c2799c2 18585 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
4a6a3df4
AO
18586 insn ^= 0x00010000;
18587 break;
b34976b6 18588
4a6a3df4
AO
18589 case 1:
18590 /* beq 0x10000000 bne 0x14000000
54f4ddb3 18591 blez 0x18000000 bgtz 0x1c000000 */
4a6a3df4
AO
18592 insn ^= 0x04000000;
18593 break;
18594
18595 default:
18596 abort ();
18597 }
18598 }
18599
18600 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18601 {
18602 /* Clear the and-link bit. */
9c2799c2 18603 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
4a6a3df4 18604
54f4ddb3
TS
18605 /* bltzal 0x04100000 bgezal 0x04110000
18606 bltzall 0x04120000 bgezall 0x04130000 */
4a6a3df4
AO
18607 insn &= ~0x00100000;
18608 }
18609
18610 /* Branch over the branch (if the branch was likely) or the
18611 full jump (not likely case). Compute the offset from the
18612 current instruction to branch to. */
18613 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18614 i = 16;
18615 else
18616 {
18617 /* How many bytes in instructions we've already emitted? */
4d68580a 18618 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18619 /* How many bytes in instructions from here to the end? */
18620 i = fragp->fr_var - i;
18621 }
18622 /* Convert to instruction count. */
18623 i >>= 2;
18624 /* Branch counts from the next instruction. */
b34976b6 18625 i--;
4a6a3df4
AO
18626 insn |= i;
18627 /* Branch over the jump. */
4d68580a 18628 buf = write_insn (buf, insn);
4a6a3df4 18629
54f4ddb3 18630 /* nop */
4d68580a 18631 buf = write_insn (buf, 0);
4a6a3df4
AO
18632
18633 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18634 {
18635 /* beql $0, $0, 2f */
18636 insn = 0x50000000;
18637 /* Compute the PC offset from the current instruction to
18638 the end of the variable frag. */
18639 /* How many bytes in instructions we've already emitted? */
4d68580a 18640 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18641 /* How many bytes in instructions from here to the end? */
18642 i = fragp->fr_var - i;
18643 /* Convert to instruction count. */
18644 i >>= 2;
18645 /* Don't decrement i, because we want to branch over the
18646 delay slot. */
4a6a3df4 18647 insn |= i;
4a6a3df4 18648
4d68580a
RS
18649 buf = write_insn (buf, insn);
18650 buf = write_insn (buf, 0);
4a6a3df4
AO
18651 }
18652
18653 uncond:
ce8ad872 18654 if (!RELAX_BRANCH_PIC (fragp->fr_subtype))
4a6a3df4
AO
18655 {
18656 /* j or jal. */
18657 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18658 ? 0x0c000000 : 0x08000000);
4a6a3df4 18659
bbd27b76
MR
18660 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18661 fragp->fr_symbol, fragp->fr_offset,
18662 FALSE, BFD_RELOC_MIPS_JMP);
4a6a3df4
AO
18663 fixp->fx_file = fragp->fr_file;
18664 fixp->fx_line = fragp->fr_line;
18665
4d68580a 18666 buf = write_insn (buf, insn);
4a6a3df4
AO
18667 }
18668 else
18669 {
66b3e8da
MR
18670 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18671
4a6a3df4 18672 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
66b3e8da
MR
18673 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18674 insn |= at << OP_SH_RT;
4a6a3df4 18675
bbd27b76
MR
18676 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18677 fragp->fr_symbol, fragp->fr_offset,
18678 FALSE, BFD_RELOC_MIPS_GOT16);
4a6a3df4
AO
18679 fixp->fx_file = fragp->fr_file;
18680 fixp->fx_line = fragp->fr_line;
18681
4d68580a 18682 buf = write_insn (buf, insn);
b34976b6 18683
4a6a3df4 18684 if (mips_opts.isa == ISA_MIPS1)
4d68580a
RS
18685 /* nop */
18686 buf = write_insn (buf, 0);
4a6a3df4
AO
18687
18688 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
66b3e8da
MR
18689 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18690 insn |= at << OP_SH_RS | at << OP_SH_RT;
4a6a3df4 18691
bbd27b76
MR
18692 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18693 fragp->fr_symbol, fragp->fr_offset,
18694 FALSE, BFD_RELOC_LO16);
4a6a3df4
AO
18695 fixp->fx_file = fragp->fr_file;
18696 fixp->fx_line = fragp->fr_line;
b34976b6 18697
4d68580a 18698 buf = write_insn (buf, insn);
4a6a3df4
AO
18699
18700 /* j(al)r $at. */
18701 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
66b3e8da 18702 insn = 0x0000f809;
4a6a3df4 18703 else
66b3e8da
MR
18704 insn = 0x00000008;
18705 insn |= at << OP_SH_RS;
4a6a3df4 18706
4d68580a 18707 buf = write_insn (buf, insn);
4a6a3df4
AO
18708 }
18709 }
18710
4a6a3df4 18711 fragp->fr_fix += fragp->fr_var;
4d68580a 18712 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
4a6a3df4
AO
18713 return;
18714 }
18715
df58fc94
RS
18716 /* Relax microMIPS branches. */
18717 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18718 {
4d68580a 18719 char *buf = fragp->fr_literal + fragp->fr_fix;
df58fc94 18720 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
8484fb75 18721 bfd_boolean insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
7bd374a4 18722 bfd_boolean nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
ce8ad872 18723 bfd_boolean pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
df58fc94
RS
18724 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18725 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
2309ddf2 18726 bfd_boolean short_ds;
df58fc94 18727 unsigned long insn;
df58fc94
RS
18728 fixS *fixp;
18729
df58fc94
RS
18730 fragp->fr_fix += fragp->fr_var;
18731
18732 /* Handle 16-bit branches that fit or are forced to fit. */
18733 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18734 {
18735 /* We generate a fixup instead of applying it right now,
18736 because if there is linker relaxation, we're going to
18737 need the relocations. */
834a65aa
MR
18738 switch (type)
18739 {
18740 case 'D':
18741 fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18742 fragp->fr_symbol, fragp->fr_offset,
18743 TRUE, BFD_RELOC_MICROMIPS_10_PCREL_S1);
18744 break;
18745 case 'E':
18746 fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18747 fragp->fr_symbol, fragp->fr_offset,
18748 TRUE, BFD_RELOC_MICROMIPS_7_PCREL_S1);
18749 break;
18750 default:
18751 abort ();
18752 }
df58fc94
RS
18753
18754 fixp->fx_file = fragp->fr_file;
18755 fixp->fx_line = fragp->fr_line;
18756
18757 /* These relocations can have an addend that won't fit in
18758 2 octets. */
18759 fixp->fx_no_overflow = 1;
18760
18761 return;
18762 }
18763
2309ddf2 18764 /* Handle 32-bit branches that fit or are forced to fit. */
df58fc94
RS
18765 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18766 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18767 {
18768 /* We generate a fixup instead of applying it right now,
18769 because if there is linker relaxation, we're going to
18770 need the relocations. */
bbd27b76
MR
18771 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18772 fragp->fr_symbol, fragp->fr_offset,
18773 TRUE, BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18774 fixp->fx_file = fragp->fr_file;
18775 fixp->fx_line = fragp->fr_line;
18776
18777 if (type == 0)
7bd374a4
MR
18778 {
18779 insn = read_compressed_insn (buf, 4);
18780 buf += 4;
18781
18782 if (nods)
18783 {
18784 /* Check the short-delay-slot bit. */
18785 if (!al || (insn & 0x02000000) != 0)
18786 buf = write_compressed_insn (buf, 0x0c00, 2);
18787 else
18788 buf = write_compressed_insn (buf, 0x00000000, 4);
18789 }
18790
18791 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18792 return;
18793 }
df58fc94
RS
18794 }
18795
18796 /* Relax 16-bit branches to 32-bit branches. */
18797 if (type != 0)
18798 {
4d68580a 18799 insn = read_compressed_insn (buf, 2);
df58fc94
RS
18800
18801 if ((insn & 0xfc00) == 0xcc00) /* b16 */
18802 insn = 0x94000000; /* beq */
18803 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
18804 {
18805 unsigned long regno;
18806
18807 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18808 regno = micromips_to_32_reg_d_map [regno];
18809 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
18810 insn |= regno << MICROMIPSOP_SH_RS;
18811 }
18812 else
18813 abort ();
18814
18815 /* Nothing else to do, just write it out. */
18816 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18817 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18818 {
4d68580a 18819 buf = write_compressed_insn (buf, insn, 4);
7bd374a4
MR
18820 if (nods)
18821 buf = write_compressed_insn (buf, 0x0c00, 2);
4d68580a 18822 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18823 return;
18824 }
18825 }
18826 else
4d68580a 18827 insn = read_compressed_insn (buf, 4);
df58fc94
RS
18828
18829 /* Relax 32-bit branches to a sequence of instructions. */
18830 as_warn_where (fragp->fr_file, fragp->fr_line,
1661c76c 18831 _("relaxed out-of-range branch into a jump"));
df58fc94 18832
2309ddf2 18833 /* Set the short-delay-slot bit. */
7bd374a4 18834 short_ds = !al || (insn & 0x02000000) != 0;
df58fc94
RS
18835
18836 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18837 {
18838 symbolS *l;
18839
18840 /* Reverse the branch. */
18841 if ((insn & 0xfc000000) == 0x94000000 /* beq */
18842 || (insn & 0xfc000000) == 0xb4000000) /* bne */
18843 insn ^= 0x20000000;
18844 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
18845 || (insn & 0xffe00000) == 0x40400000 /* bgez */
18846 || (insn & 0xffe00000) == 0x40800000 /* blez */
18847 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
18848 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
18849 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
18850 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
18851 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
18852 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
18853 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
18854 insn ^= 0x00400000;
18855 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
18856 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
18857 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
18858 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
18859 insn ^= 0x00200000;
56d438b1
CF
18860 else if ((insn & 0xff000000) == 0x83000000 /* BZ.df
18861 BNZ.df */
18862 || (insn & 0xff600000) == 0x81600000) /* BZ.V
18863 BNZ.V */
18864 insn ^= 0x00800000;
df58fc94
RS
18865 else
18866 abort ();
18867
18868 if (al)
18869 {
18870 /* Clear the and-link and short-delay-slot bits. */
18871 gas_assert ((insn & 0xfda00000) == 0x40200000);
18872
18873 /* bltzal 0x40200000 bgezal 0x40600000 */
18874 /* bltzals 0x42200000 bgezals 0x42600000 */
18875 insn &= ~0x02200000;
18876 }
18877
18878 /* Make a label at the end for use with the branch. */
e01e1cee 18879 l = symbol_new (micromips_label_name (), asec, fragp, fragp->fr_fix);
df58fc94 18880 micromips_label_inc ();
f3ded42a 18881 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
df58fc94
RS
18882
18883 /* Refer to it. */
4d68580a
RS
18884 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
18885 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18886 fixp->fx_file = fragp->fr_file;
18887 fixp->fx_line = fragp->fr_line;
18888
18889 /* Branch over the jump. */
4d68580a 18890 buf = write_compressed_insn (buf, insn, 4);
8484fb75 18891
df58fc94 18892 if (!compact)
8484fb75
MR
18893 {
18894 /* nop */
18895 if (insn32)
18896 buf = write_compressed_insn (buf, 0x00000000, 4);
18897 else
18898 buf = write_compressed_insn (buf, 0x0c00, 2);
18899 }
df58fc94
RS
18900 }
18901
ce8ad872 18902 if (!pic)
df58fc94 18903 {
7bd374a4
MR
18904 unsigned long jal = (short_ds || nods
18905 ? 0x74000000 : 0xf4000000); /* jal/s */
2309ddf2 18906
df58fc94
RS
18907 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
18908 insn = al ? jal : 0xd4000000;
18909
bbd27b76
MR
18910 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18911 fragp->fr_symbol, fragp->fr_offset,
18912 FALSE, BFD_RELOC_MICROMIPS_JMP);
df58fc94
RS
18913 fixp->fx_file = fragp->fr_file;
18914 fixp->fx_line = fragp->fr_line;
18915
4d68580a 18916 buf = write_compressed_insn (buf, insn, 4);
8484fb75 18917
7bd374a4 18918 if (compact || nods)
8484fb75
MR
18919 {
18920 /* nop */
18921 if (insn32)
18922 buf = write_compressed_insn (buf, 0x00000000, 4);
18923 else
18924 buf = write_compressed_insn (buf, 0x0c00, 2);
18925 }
df58fc94
RS
18926 }
18927 else
18928 {
18929 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
18930
18931 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
18932 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18933 insn |= at << MICROMIPSOP_SH_RT;
18934
bbd27b76
MR
18935 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18936 fragp->fr_symbol, fragp->fr_offset,
18937 FALSE, BFD_RELOC_MICROMIPS_GOT16);
df58fc94
RS
18938 fixp->fx_file = fragp->fr_file;
18939 fixp->fx_line = fragp->fr_line;
18940
4d68580a 18941 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
18942
18943 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
18944 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18945 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18946
bbd27b76
MR
18947 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18948 fragp->fr_symbol, fragp->fr_offset,
18949 FALSE, BFD_RELOC_MICROMIPS_LO16);
df58fc94
RS
18950 fixp->fx_file = fragp->fr_file;
18951 fixp->fx_line = fragp->fr_line;
18952
4d68580a 18953 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18954
8484fb75
MR
18955 if (insn32)
18956 {
18957 /* jr/jalr $at */
18958 insn = 0x00000f3c | (al ? RA : ZERO) << MICROMIPSOP_SH_RT;
18959 insn |= at << MICROMIPSOP_SH_RS;
18960
18961 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18962
7bd374a4 18963 if (compact || nods)
8484fb75
MR
18964 /* nop */
18965 buf = write_compressed_insn (buf, 0x00000000, 4);
18966 }
18967 else
18968 {
18969 /* jr/jrc/jalr/jalrs $at */
18970 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
7bd374a4 18971 unsigned long jr = compact || nods ? 0x45a0 : 0x4580; /* jr/c */
8484fb75
MR
18972
18973 insn = al ? jalr : jr;
18974 insn |= at << MICROMIPSOP_SH_MJ;
18975
18976 buf = write_compressed_insn (buf, insn, 2);
7bd374a4
MR
18977 if (al && nods)
18978 {
18979 /* nop */
18980 if (short_ds)
18981 buf = write_compressed_insn (buf, 0x0c00, 2);
18982 else
18983 buf = write_compressed_insn (buf, 0x00000000, 4);
18984 }
8484fb75 18985 }
df58fc94
RS
18986 }
18987
4d68580a 18988 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18989 return;
18990 }
18991
252b5132
RH
18992 if (RELAX_MIPS16_P (fragp->fr_subtype))
18993 {
18994 int type;
3ccad066 18995 const struct mips_int_operand *operand;
252b5132 18996 offsetT val;
5c04167a 18997 char *buf;
8507b6e7 18998 unsigned int user_length;
9d862524 18999 bfd_boolean need_reloc;
252b5132 19000 unsigned long insn;
8507b6e7 19001 bfd_boolean mac;
5c04167a 19002 bfd_boolean ext;
88a7ef16 19003 segT symsec;
252b5132
RH
19004
19005 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
3ccad066 19006 operand = mips16_immed_operand (type, FALSE);
252b5132 19007
8507b6e7 19008 mac = RELAX_MIPS16_MACRO (fragp->fr_subtype);
5c04167a 19009 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
88a7ef16 19010 val = resolve_symbol_value (fragp->fr_symbol) + fragp->fr_offset;
9d862524
MR
19011
19012 symsec = S_GET_SEGMENT (fragp->fr_symbol);
19013 need_reloc = (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
8507b6e7 19014 || (operand->root.type == OP_PCREL && !mac
9d862524
MR
19015 ? asec != symsec
19016 : !bfd_is_abs_section (symsec)));
19017
8507b6e7 19018 if (operand->root.type == OP_PCREL && !mac)
252b5132 19019 {
3ccad066 19020 const struct mips_pcrel_operand *pcrel_op;
252b5132 19021
3ccad066 19022 pcrel_op = (const struct mips_pcrel_operand *) operand;
252b5132 19023
14f72d45 19024 if (pcrel_op->include_isa_bit && !need_reloc)
252b5132 19025 {
37b2d327
MR
19026 if (!mips_ignore_branch_isa
19027 && !ELF_ST_IS_MIPS16 (S_GET_OTHER (fragp->fr_symbol)))
14f72d45
MR
19028 as_bad_where (fragp->fr_file, fragp->fr_line,
19029 _("branch to a symbol in another ISA mode"));
19030 else if ((fragp->fr_offset & 0x1) != 0)
19031 as_bad_where (fragp->fr_file, fragp->fr_line,
19032 _("branch to misaligned address (0x%lx)"),
52031738
FS
19033 (long) (resolve_symbol_value (fragp->fr_symbol)
19034 + (fragp->fr_offset & ~1)));
252b5132 19035 }
252b5132 19036
14f72d45 19037 val = mips16_pcrel_val (fragp, pcrel_op, val, 0);
252b5132
RH
19038
19039 /* Make sure the section winds up with the alignment we have
19040 assumed. */
3ccad066
RS
19041 if (operand->shift > 0)
19042 record_alignment (asec, operand->shift);
252b5132
RH
19043 }
19044
8507b6e7
MR
19045 if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
19046 || RELAX_MIPS16_DSLOT (fragp->fr_subtype))
19047 {
19048 if (mac)
19049 as_warn_where (fragp->fr_file, fragp->fr_line,
19050 _("macro instruction expanded into multiple "
19051 "instructions in a branch delay slot"));
19052 else if (ext)
19053 as_warn_where (fragp->fr_file, fragp->fr_line,
19054 _("extended instruction in a branch delay slot"));
19055 }
19056 else if (RELAX_MIPS16_NOMACRO (fragp->fr_subtype) && mac)
252b5132 19057 as_warn_where (fragp->fr_file, fragp->fr_line,
8507b6e7
MR
19058 _("macro instruction expanded into multiple "
19059 "instructions"));
252b5132 19060
5c04167a 19061 buf = fragp->fr_literal + fragp->fr_fix;
252b5132 19062
4d68580a 19063 insn = read_compressed_insn (buf, 2);
5c04167a
RS
19064 if (ext)
19065 insn |= MIPS16_EXTEND;
252b5132 19066
5c04167a
RS
19067 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
19068 user_length = 4;
19069 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
19070 user_length = 2;
19071 else
19072 user_length = 0;
19073
8507b6e7 19074 if (mac)
c9775dde 19075 {
8507b6e7
MR
19076 unsigned long reg;
19077 unsigned long new;
19078 unsigned long op;
25499ac7 19079 bfd_boolean e2;
8507b6e7
MR
19080
19081 gas_assert (type == 'A' || type == 'B' || type == 'E');
19082 gas_assert (RELAX_MIPS16_SYM32 (fragp->fr_subtype));
c9775dde 19083
25499ac7
MR
19084 e2 = RELAX_MIPS16_E2 (fragp->fr_subtype);
19085
8507b6e7 19086 if (need_reloc)
c9775dde 19087 {
8507b6e7
MR
19088 fixS *fixp;
19089
19090 gas_assert (!RELAX_MIPS16_PIC (fragp->fr_subtype));
19091
19092 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
19093 fragp->fr_symbol, fragp->fr_offset,
19094 FALSE, BFD_RELOC_MIPS16_HI16_S);
19095 fixp->fx_file = fragp->fr_file;
19096 fixp->fx_line = fragp->fr_line;
19097
25499ac7 19098 fixp = fix_new (fragp, buf - fragp->fr_literal + (e2 ? 4 : 8), 4,
8507b6e7
MR
19099 fragp->fr_symbol, fragp->fr_offset,
19100 FALSE, BFD_RELOC_MIPS16_LO16);
19101 fixp->fx_file = fragp->fr_file;
19102 fixp->fx_line = fragp->fr_line;
19103
19104 val = 0;
19105 }
19106
19107 switch (insn & 0xf800)
19108 {
19109 case 0x0800: /* ADDIU */
19110 reg = (insn >> 8) & 0x7;
19111 op = 0xf0004800 | (reg << 8);
c9775dde 19112 break;
8507b6e7
MR
19113 case 0xb000: /* LW */
19114 reg = (insn >> 8) & 0x7;
19115 op = 0xf0009800 | (reg << 8) | (reg << 5);
c9775dde 19116 break;
8507b6e7
MR
19117 case 0xf800: /* I64 */
19118 reg = (insn >> 5) & 0x7;
19119 switch (insn & 0x0700)
19120 {
19121 case 0x0400: /* LD */
19122 op = 0xf0003800 | (reg << 8) | (reg << 5);
19123 break;
19124 case 0x0600: /* DADDIU */
19125 op = 0xf000fd00 | (reg << 5);
19126 break;
19127 default:
19128 abort ();
19129 }
19130 break;
19131 default:
19132 abort ();
c9775dde 19133 }
8507b6e7 19134
25499ac7 19135 new = (e2 ? 0xf0006820 : 0xf0006800) | (reg << 8); /* LUI/LI */
8507b6e7
MR
19136 new |= mips16_immed_extend ((val + 0x8000) >> 16, 16);
19137 buf = write_compressed_insn (buf, new, 4);
25499ac7
MR
19138 if (!e2)
19139 {
19140 new = 0xf4003000 | (reg << 8) | (reg << 5); /* SLL */
19141 buf = write_compressed_insn (buf, new, 4);
19142 }
8507b6e7
MR
19143 op |= mips16_immed_extend (val, 16);
19144 buf = write_compressed_insn (buf, op, 4);
19145
25499ac7 19146 fragp->fr_fix += e2 ? 8 : 12;
8507b6e7
MR
19147 }
19148 else
19149 {
19150 unsigned int length = ext ? 4 : 2;
19151
19152 if (need_reloc)
c9775dde 19153 {
8507b6e7 19154 bfd_reloc_code_real_type reloc = BFD_RELOC_NONE;
8507b6e7 19155 fixS *fixp;
c9775dde 19156
8507b6e7
MR
19157 switch (type)
19158 {
19159 case 'p':
19160 case 'q':
19161 reloc = BFD_RELOC_MIPS16_16_PCREL_S1;
19162 break;
19163 default:
19164 break;
19165 }
19166 if (mac || reloc == BFD_RELOC_NONE)
19167 as_bad_where (fragp->fr_file, fragp->fr_line,
19168 _("unsupported relocation"));
19169 else if (ext)
19170 {
bbd27b76
MR
19171 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
19172 fragp->fr_symbol, fragp->fr_offset,
19173 TRUE, reloc);
8507b6e7
MR
19174 fixp->fx_file = fragp->fr_file;
19175 fixp->fx_line = fragp->fr_line;
19176 }
19177 else
19178 as_bad_where (fragp->fr_file, fragp->fr_line,
19179 _("invalid unextended operand value"));
c9775dde 19180 }
eefc3365 19181 else
8507b6e7
MR
19182 mips16_immed (fragp->fr_file, fragp->fr_line, type,
19183 BFD_RELOC_UNUSED, val, user_length, &insn);
252b5132 19184
8507b6e7
MR
19185 gas_assert (mips16_opcode_length (insn) == length);
19186 write_compressed_insn (buf, insn, length);
19187 fragp->fr_fix += length;
19188 }
252b5132
RH
19189 }
19190 else
19191 {
df58fc94
RS
19192 relax_substateT subtype = fragp->fr_subtype;
19193 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
19194 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
871a6bd2 19195 unsigned int first, second;
4d7206a2 19196 fixS *fixp;
252b5132 19197
df58fc94
RS
19198 first = RELAX_FIRST (subtype);
19199 second = RELAX_SECOND (subtype);
4d7206a2 19200 fixp = (fixS *) fragp->fr_opcode;
252b5132 19201
df58fc94
RS
19202 /* If the delay slot chosen does not match the size of the instruction,
19203 then emit a warning. */
19204 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
19205 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
19206 {
19207 relax_substateT s;
19208 const char *msg;
19209
19210 s = subtype & (RELAX_DELAY_SLOT_16BIT
19211 | RELAX_DELAY_SLOT_SIZE_FIRST
19212 | RELAX_DELAY_SLOT_SIZE_SECOND);
19213 msg = macro_warning (s);
19214 if (msg != NULL)
db9b2be4 19215 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94
RS
19216 subtype &= ~s;
19217 }
19218
584892a6 19219 /* Possibly emit a warning if we've chosen the longer option. */
df58fc94 19220 if (use_second == second_longer)
584892a6 19221 {
df58fc94
RS
19222 relax_substateT s;
19223 const char *msg;
19224
19225 s = (subtype
19226 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
19227 msg = macro_warning (s);
19228 if (msg != NULL)
db9b2be4 19229 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94 19230 subtype &= ~s;
584892a6
RS
19231 }
19232
4d7206a2
RS
19233 /* Go through all the fixups for the first sequence. Disable them
19234 (by marking them as done) if we're going to use the second
19235 sequence instead. */
19236 while (fixp
19237 && fixp->fx_frag == fragp
90bd3c90 19238 && fixp->fx_where + second < fragp->fr_fix)
4d7206a2 19239 {
df58fc94 19240 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
19241 fixp->fx_done = 1;
19242 fixp = fixp->fx_next;
19243 }
252b5132 19244
4d7206a2
RS
19245 /* Go through the fixups for the second sequence. Disable them if
19246 we're going to use the first sequence, otherwise adjust their
19247 addresses to account for the relaxation. */
19248 while (fixp && fixp->fx_frag == fragp)
19249 {
df58fc94 19250 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
19251 fixp->fx_where -= first;
19252 else
19253 fixp->fx_done = 1;
19254 fixp = fixp->fx_next;
19255 }
19256
19257 /* Now modify the frag contents. */
df58fc94 19258 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
19259 {
19260 char *start;
19261
19262 start = fragp->fr_literal + fragp->fr_fix - first - second;
19263 memmove (start, start + first, second);
19264 fragp->fr_fix -= first;
19265 }
19266 else
19267 fragp->fr_fix -= second;
252b5132
RH
19268 }
19269}
19270
252b5132
RH
19271/* This function is called after the relocs have been generated.
19272 We've been storing mips16 text labels as odd. Here we convert them
19273 back to even for the convenience of the debugger. */
19274
19275void
17a2f251 19276mips_frob_file_after_relocs (void)
252b5132
RH
19277{
19278 asymbol **syms;
19279 unsigned int count, i;
19280
252b5132
RH
19281 syms = bfd_get_outsymbols (stdoutput);
19282 count = bfd_get_symcount (stdoutput);
19283 for (i = 0; i < count; i++, syms++)
df58fc94
RS
19284 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
19285 && ((*syms)->value & 1) != 0)
19286 {
19287 (*syms)->value &= ~1;
19288 /* If the symbol has an odd size, it was probably computed
19289 incorrectly, so adjust that as well. */
19290 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
19291 ++elf_symbol (*syms)->internal_elf_sym.st_size;
19292 }
252b5132
RH
19293}
19294
a1facbec
MR
19295/* This function is called whenever a label is defined, including fake
19296 labels instantiated off the dot special symbol. It is used when
19297 handling branch delays; if a branch has a label, we assume we cannot
19298 move it. This also bumps the value of the symbol by 1 in compressed
19299 code. */
252b5132 19300
e1b47bd5 19301static void
a1facbec 19302mips_record_label (symbolS *sym)
252b5132 19303{
a8dbcb85 19304 segment_info_type *si = seg_info (now_seg);
252b5132
RH
19305 struct insn_label_list *l;
19306
19307 if (free_insn_labels == NULL)
325801bd 19308 l = XNEW (struct insn_label_list);
252b5132
RH
19309 else
19310 {
19311 l = free_insn_labels;
19312 free_insn_labels = l->next;
19313 }
19314
19315 l->label = sym;
a8dbcb85
TS
19316 l->next = si->label_list;
19317 si->label_list = l;
a1facbec 19318}
07a53e5c 19319
a1facbec
MR
19320/* This function is called as tc_frob_label() whenever a label is defined
19321 and adds a DWARF-2 record we only want for true labels. */
19322
19323void
19324mips_define_label (symbolS *sym)
19325{
19326 mips_record_label (sym);
07a53e5c 19327 dwarf2_emit_label (sym);
252b5132 19328}
e1b47bd5
RS
19329
19330/* This function is called by tc_new_dot_label whenever a new dot symbol
19331 is defined. */
19332
19333void
19334mips_add_dot_label (symbolS *sym)
19335{
19336 mips_record_label (sym);
19337 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
19338 mips_compressed_mark_label (sym);
19339}
252b5132 19340\f
351cdf24
MF
19341/* Converting ASE flags from internal to .MIPS.abiflags values. */
19342static unsigned int
19343mips_convert_ase_flags (int ase)
19344{
19345 unsigned int ext_ases = 0;
19346
19347 if (ase & ASE_DSP)
19348 ext_ases |= AFL_ASE_DSP;
19349 if (ase & ASE_DSPR2)
19350 ext_ases |= AFL_ASE_DSPR2;
8f4f9071
MF
19351 if (ase & ASE_DSPR3)
19352 ext_ases |= AFL_ASE_DSPR3;
351cdf24
MF
19353 if (ase & ASE_EVA)
19354 ext_ases |= AFL_ASE_EVA;
19355 if (ase & ASE_MCU)
19356 ext_ases |= AFL_ASE_MCU;
19357 if (ase & ASE_MDMX)
19358 ext_ases |= AFL_ASE_MDMX;
19359 if (ase & ASE_MIPS3D)
19360 ext_ases |= AFL_ASE_MIPS3D;
19361 if (ase & ASE_MT)
19362 ext_ases |= AFL_ASE_MT;
19363 if (ase & ASE_SMARTMIPS)
19364 ext_ases |= AFL_ASE_SMARTMIPS;
19365 if (ase & ASE_VIRT)
19366 ext_ases |= AFL_ASE_VIRT;
19367 if (ase & ASE_MSA)
19368 ext_ases |= AFL_ASE_MSA;
19369 if (ase & ASE_XPA)
19370 ext_ases |= AFL_ASE_XPA;
25499ac7
MR
19371 if (ase & ASE_MIPS16E2)
19372 ext_ases |= file_ase_mips16 ? AFL_ASE_MIPS16E2 : 0;
730c3174
SE
19373 if (ase & ASE_CRC)
19374 ext_ases |= AFL_ASE_CRC;
6f20c942
FS
19375 if (ase & ASE_GINV)
19376 ext_ases |= AFL_ASE_GINV;
8095d2f7
CX
19377 if (ase & ASE_LOONGSON_MMI)
19378 ext_ases |= AFL_ASE_LOONGSON_MMI;
716c08de
CX
19379 if (ase & ASE_LOONGSON_CAM)
19380 ext_ases |= AFL_ASE_LOONGSON_CAM;
bdc6c06e
CX
19381 if (ase & ASE_LOONGSON_EXT)
19382 ext_ases |= AFL_ASE_LOONGSON_EXT;
a693765e
CX
19383 if (ase & ASE_LOONGSON_EXT2)
19384 ext_ases |= AFL_ASE_LOONGSON_EXT2;
351cdf24
MF
19385
19386 return ext_ases;
19387}
252b5132
RH
19388/* Some special processing for a MIPS ELF file. */
19389
19390void
17a2f251 19391mips_elf_final_processing (void)
252b5132 19392{
351cdf24
MF
19393 int fpabi;
19394 Elf_Internal_ABIFlags_v0 flags;
19395
19396 flags.version = 0;
19397 flags.isa_rev = 0;
19398 switch (file_mips_opts.isa)
19399 {
19400 case INSN_ISA1:
19401 flags.isa_level = 1;
19402 break;
19403 case INSN_ISA2:
19404 flags.isa_level = 2;
19405 break;
19406 case INSN_ISA3:
19407 flags.isa_level = 3;
19408 break;
19409 case INSN_ISA4:
19410 flags.isa_level = 4;
19411 break;
19412 case INSN_ISA5:
19413 flags.isa_level = 5;
19414 break;
19415 case INSN_ISA32:
19416 flags.isa_level = 32;
19417 flags.isa_rev = 1;
19418 break;
19419 case INSN_ISA32R2:
19420 flags.isa_level = 32;
19421 flags.isa_rev = 2;
19422 break;
19423 case INSN_ISA32R3:
19424 flags.isa_level = 32;
19425 flags.isa_rev = 3;
19426 break;
19427 case INSN_ISA32R5:
19428 flags.isa_level = 32;
19429 flags.isa_rev = 5;
19430 break;
09c14161
MF
19431 case INSN_ISA32R6:
19432 flags.isa_level = 32;
19433 flags.isa_rev = 6;
19434 break;
351cdf24
MF
19435 case INSN_ISA64:
19436 flags.isa_level = 64;
19437 flags.isa_rev = 1;
19438 break;
19439 case INSN_ISA64R2:
19440 flags.isa_level = 64;
19441 flags.isa_rev = 2;
19442 break;
19443 case INSN_ISA64R3:
19444 flags.isa_level = 64;
19445 flags.isa_rev = 3;
19446 break;
19447 case INSN_ISA64R5:
19448 flags.isa_level = 64;
19449 flags.isa_rev = 5;
19450 break;
09c14161
MF
19451 case INSN_ISA64R6:
19452 flags.isa_level = 64;
19453 flags.isa_rev = 6;
19454 break;
351cdf24
MF
19455 }
19456
19457 flags.gpr_size = file_mips_opts.gp == 32 ? AFL_REG_32 : AFL_REG_64;
19458 flags.cpr1_size = file_mips_opts.soft_float ? AFL_REG_NONE
19459 : (file_mips_opts.ase & ASE_MSA) ? AFL_REG_128
19460 : (file_mips_opts.fp == 64) ? AFL_REG_64
19461 : AFL_REG_32;
19462 flags.cpr2_size = AFL_REG_NONE;
19463 flags.fp_abi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19464 Tag_GNU_MIPS_ABI_FP);
19465 flags.isa_ext = bfd_mips_isa_ext (stdoutput);
19466 flags.ases = mips_convert_ase_flags (file_mips_opts.ase);
19467 if (file_ase_mips16)
19468 flags.ases |= AFL_ASE_MIPS16;
19469 if (file_ase_micromips)
19470 flags.ases |= AFL_ASE_MICROMIPS;
19471 flags.flags1 = 0;
19472 if ((ISA_HAS_ODD_SINGLE_FPR (file_mips_opts.isa, file_mips_opts.arch)
19473 || file_mips_opts.fp == 64)
19474 && file_mips_opts.oddspreg)
19475 flags.flags1 |= AFL_FLAGS1_ODDSPREG;
19476 flags.flags2 = 0;
19477
19478 bfd_mips_elf_swap_abiflags_v0_out (stdoutput, &flags,
19479 ((Elf_External_ABIFlags_v0 *)
19480 mips_flags_frag));
19481
252b5132 19482 /* Write out the register information. */
316f5878 19483 if (mips_abi != N64_ABI)
252b5132
RH
19484 {
19485 Elf32_RegInfo s;
19486
19487 s.ri_gprmask = mips_gprmask;
19488 s.ri_cprmask[0] = mips_cprmask[0];
19489 s.ri_cprmask[1] = mips_cprmask[1];
19490 s.ri_cprmask[2] = mips_cprmask[2];
19491 s.ri_cprmask[3] = mips_cprmask[3];
19492 /* The gp_value field is set by the MIPS ELF backend. */
19493
19494 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
19495 ((Elf32_External_RegInfo *)
19496 mips_regmask_frag));
19497 }
19498 else
19499 {
19500 Elf64_Internal_RegInfo s;
19501
19502 s.ri_gprmask = mips_gprmask;
19503 s.ri_pad = 0;
19504 s.ri_cprmask[0] = mips_cprmask[0];
19505 s.ri_cprmask[1] = mips_cprmask[1];
19506 s.ri_cprmask[2] = mips_cprmask[2];
19507 s.ri_cprmask[3] = mips_cprmask[3];
19508 /* The gp_value field is set by the MIPS ELF backend. */
19509
19510 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
19511 ((Elf64_External_RegInfo *)
19512 mips_regmask_frag));
19513 }
19514
19515 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
19516 sort of BFD interface for this. */
19517 if (mips_any_noreorder)
19518 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
19519 if (mips_pic != NO_PIC)
143d77c5 19520 {
8b828383 19521 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
19522 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
19523 }
19524 if (mips_abicalls)
19525 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 19526
b015e599
AP
19527 /* Set MIPS ELF flags for ASEs. Note that not all ASEs have flags
19528 defined at present; this might need to change in future. */
a4672219
TS
19529 if (file_ase_mips16)
19530 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
df58fc94
RS
19531 if (file_ase_micromips)
19532 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
919731af 19533 if (file_mips_opts.ase & ASE_MDMX)
deec1734 19534 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 19535
bdaaa2e1 19536 /* Set the MIPS ELF ABI flags. */
316f5878 19537 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 19538 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 19539 else if (mips_abi == O64_ABI)
252b5132 19540 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 19541 else if (mips_abi == EABI_ABI)
252b5132 19542 {
bad1aba3 19543 if (file_mips_opts.gp == 64)
252b5132
RH
19544 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
19545 else
19546 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
19547 }
be00bddd 19548
defc8e2b 19549 /* Nothing to do for N32_ABI or N64_ABI. */
252b5132
RH
19550
19551 if (mips_32bitmode)
19552 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
ad3fea08 19553
7361da2c 19554 if (mips_nan2008 == 1)
ba92f887
MR
19555 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NAN2008;
19556
ad3fea08 19557 /* 32 bit code with 64 bit FP registers. */
351cdf24
MF
19558 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19559 Tag_GNU_MIPS_ABI_FP);
19560 if (fpabi == Val_GNU_MIPS_ABI_FP_OLD_64)
f1c38003 19561 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_FP64;
252b5132 19562}
252b5132 19563\f
beae10d5 19564typedef struct proc {
9b2f1d35
EC
19565 symbolS *func_sym;
19566 symbolS *func_end_sym;
beae10d5
KH
19567 unsigned long reg_mask;
19568 unsigned long reg_offset;
19569 unsigned long fpreg_mask;
19570 unsigned long fpreg_offset;
19571 unsigned long frame_offset;
19572 unsigned long frame_reg;
19573 unsigned long pc_reg;
19574} procS;
252b5132
RH
19575
19576static procS cur_proc;
19577static procS *cur_proc_ptr;
19578static int numprocs;
19579
df58fc94
RS
19580/* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
19581 as "2", and a normal nop as "0". */
19582
19583#define NOP_OPCODE_MIPS 0
19584#define NOP_OPCODE_MIPS16 1
19585#define NOP_OPCODE_MICROMIPS 2
742a56fe
RS
19586
19587char
19588mips_nop_opcode (void)
19589{
df58fc94
RS
19590 if (seg_info (now_seg)->tc_segment_info_data.micromips)
19591 return NOP_OPCODE_MICROMIPS;
19592 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
19593 return NOP_OPCODE_MIPS16;
19594 else
19595 return NOP_OPCODE_MIPS;
742a56fe
RS
19596}
19597
df58fc94
RS
19598/* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
19599 32-bit microMIPS NOPs here (if applicable). */
a19d8eb0 19600
0a9ef439 19601void
17a2f251 19602mips_handle_align (fragS *fragp)
a19d8eb0 19603{
df58fc94 19604 char nop_opcode;
742a56fe 19605 char *p;
c67a084a
NC
19606 int bytes, size, excess;
19607 valueT opcode;
742a56fe 19608
0a9ef439
RH
19609 if (fragp->fr_type != rs_align_code)
19610 return;
19611
742a56fe 19612 p = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
19613 nop_opcode = *p;
19614 switch (nop_opcode)
a19d8eb0 19615 {
df58fc94
RS
19616 case NOP_OPCODE_MICROMIPS:
19617 opcode = micromips_nop32_insn.insn_opcode;
19618 size = 4;
19619 break;
19620 case NOP_OPCODE_MIPS16:
c67a084a
NC
19621 opcode = mips16_nop_insn.insn_opcode;
19622 size = 2;
df58fc94
RS
19623 break;
19624 case NOP_OPCODE_MIPS:
19625 default:
c67a084a
NC
19626 opcode = nop_insn.insn_opcode;
19627 size = 4;
df58fc94 19628 break;
c67a084a 19629 }
a19d8eb0 19630
c67a084a
NC
19631 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
19632 excess = bytes % size;
df58fc94
RS
19633
19634 /* Handle the leading part if we're not inserting a whole number of
19635 instructions, and make it the end of the fixed part of the frag.
19636 Try to fit in a short microMIPS NOP if applicable and possible,
19637 and use zeroes otherwise. */
19638 gas_assert (excess < 4);
19639 fragp->fr_fix += excess;
19640 switch (excess)
c67a084a 19641 {
df58fc94
RS
19642 case 3:
19643 *p++ = '\0';
19644 /* Fall through. */
19645 case 2:
833794fc 19646 if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
df58fc94 19647 {
4d68580a 19648 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
df58fc94
RS
19649 break;
19650 }
19651 *p++ = '\0';
19652 /* Fall through. */
19653 case 1:
19654 *p++ = '\0';
19655 /* Fall through. */
19656 case 0:
19657 break;
a19d8eb0 19658 }
c67a084a
NC
19659
19660 md_number_to_chars (p, opcode, size);
19661 fragp->fr_var = size;
a19d8eb0
CP
19662}
19663
252b5132 19664static long
17a2f251 19665get_number (void)
252b5132
RH
19666{
19667 int negative = 0;
19668 long val = 0;
19669
19670 if (*input_line_pointer == '-')
19671 {
19672 ++input_line_pointer;
19673 negative = 1;
19674 }
3882b010 19675 if (!ISDIGIT (*input_line_pointer))
956cd1d6 19676 as_bad (_("expected simple number"));
252b5132
RH
19677 if (input_line_pointer[0] == '0')
19678 {
19679 if (input_line_pointer[1] == 'x')
19680 {
19681 input_line_pointer += 2;
3882b010 19682 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
19683 {
19684 val <<= 4;
19685 val |= hex_value (*input_line_pointer++);
19686 }
19687 return negative ? -val : val;
19688 }
19689 else
19690 {
19691 ++input_line_pointer;
3882b010 19692 while (ISDIGIT (*input_line_pointer))
252b5132
RH
19693 {
19694 val <<= 3;
19695 val |= *input_line_pointer++ - '0';
19696 }
19697 return negative ? -val : val;
19698 }
19699 }
3882b010 19700 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
19701 {
19702 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
19703 *input_line_pointer, *input_line_pointer);
956cd1d6 19704 as_warn (_("invalid number"));
252b5132
RH
19705 return -1;
19706 }
3882b010 19707 while (ISDIGIT (*input_line_pointer))
252b5132
RH
19708 {
19709 val *= 10;
19710 val += *input_line_pointer++ - '0';
19711 }
19712 return negative ? -val : val;
19713}
19714
19715/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
19716 is an initial number which is the ECOFF file index. In the non-ECOFF
19717 case .file implies DWARF-2. */
19718
19719static void
17a2f251 19720s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 19721{
ecb4347a
DJ
19722 static int first_file_directive = 0;
19723
c5dd6aab
DJ
19724 if (ECOFF_DEBUGGING)
19725 {
19726 get_number ();
19727 s_app_file (0);
19728 }
19729 else
ecb4347a
DJ
19730 {
19731 char *filename;
19732
68d20676 19733 filename = dwarf2_directive_filename ();
ecb4347a
DJ
19734
19735 /* Versions of GCC up to 3.1 start files with a ".file"
19736 directive even for stabs output. Make sure that this
19737 ".file" is handled. Note that you need a version of GCC
19738 after 3.1 in order to support DWARF-2 on MIPS. */
19739 if (filename != NULL && ! first_file_directive)
19740 {
19741 (void) new_logical_line (filename, -1);
c04f5787 19742 s_app_file_string (filename, 0);
ecb4347a
DJ
19743 }
19744 first_file_directive = 1;
19745 }
c5dd6aab
DJ
19746}
19747
19748/* The .loc directive, implying DWARF-2. */
252b5132
RH
19749
19750static void
17a2f251 19751s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 19752{
c5dd6aab
DJ
19753 if (!ECOFF_DEBUGGING)
19754 dwarf2_directive_loc (0);
252b5132
RH
19755}
19756
252b5132
RH
19757/* The .end directive. */
19758
19759static void
17a2f251 19760s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
19761{
19762 symbolS *p;
252b5132 19763
7a621144
DJ
19764 /* Following functions need their own .frame and .cprestore directives. */
19765 mips_frame_reg_valid = 0;
19766 mips_cprestore_valid = 0;
19767
252b5132
RH
19768 if (!is_end_of_line[(unsigned char) *input_line_pointer])
19769 {
19770 p = get_symbol ();
19771 demand_empty_rest_of_line ();
19772 }
19773 else
19774 p = NULL;
19775
fd361982 19776 if ((bfd_section_flags (now_seg) & SEC_CODE) == 0)
252b5132
RH
19777 as_warn (_(".end not in text section"));
19778
19779 if (!cur_proc_ptr)
19780 {
1661c76c 19781 as_warn (_(".end directive without a preceding .ent directive"));
252b5132
RH
19782 demand_empty_rest_of_line ();
19783 return;
19784 }
19785
19786 if (p != NULL)
19787 {
9c2799c2 19788 gas_assert (S_GET_NAME (p));
9b2f1d35 19789 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
1661c76c 19790 as_warn (_(".end symbol does not match .ent symbol"));
ecb4347a
DJ
19791
19792 if (debug_type == DEBUG_STABS)
19793 stabs_generate_asm_endfunc (S_GET_NAME (p),
19794 S_GET_NAME (p));
252b5132
RH
19795 }
19796 else
19797 as_warn (_(".end directive missing or unknown symbol"));
19798
9b2f1d35
EC
19799 /* Create an expression to calculate the size of the function. */
19800 if (p && cur_proc_ptr)
19801 {
19802 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
325801bd 19803 expressionS *exp = XNEW (expressionS);
9b2f1d35
EC
19804
19805 obj->size = exp;
19806 exp->X_op = O_subtract;
19807 exp->X_add_symbol = symbol_temp_new_now ();
19808 exp->X_op_symbol = p;
19809 exp->X_add_number = 0;
19810
19811 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
19812 }
19813
5ff6a06c
MR
19814#ifdef md_flush_pending_output
19815 md_flush_pending_output ();
19816#endif
19817
ecb4347a 19818 /* Generate a .pdr section. */
f3ded42a 19819 if (!ECOFF_DEBUGGING && mips_flag_pdr)
ecb4347a
DJ
19820 {
19821 segT saved_seg = now_seg;
19822 subsegT saved_subseg = now_subseg;
ecb4347a
DJ
19823 expressionS exp;
19824 char *fragp;
252b5132 19825
9c2799c2 19826 gas_assert (pdr_seg);
ecb4347a 19827 subseg_set (pdr_seg, 0);
252b5132 19828
ecb4347a
DJ
19829 /* Write the symbol. */
19830 exp.X_op = O_symbol;
19831 exp.X_add_symbol = p;
19832 exp.X_add_number = 0;
19833 emit_expr (&exp, 4);
252b5132 19834
ecb4347a 19835 fragp = frag_more (7 * 4);
252b5132 19836
17a2f251
TS
19837 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19838 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19839 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19840 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19841 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19842 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19843 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 19844
ecb4347a
DJ
19845 subseg_set (saved_seg, saved_subseg);
19846 }
252b5132
RH
19847
19848 cur_proc_ptr = NULL;
19849}
19850
19851/* The .aent and .ent directives. */
19852
19853static void
17a2f251 19854s_mips_ent (int aent)
252b5132 19855{
252b5132 19856 symbolS *symbolP;
252b5132
RH
19857
19858 symbolP = get_symbol ();
19859 if (*input_line_pointer == ',')
f9419b05 19860 ++input_line_pointer;
252b5132 19861 SKIP_WHITESPACE ();
3882b010 19862 if (ISDIGIT (*input_line_pointer)
d9a62219 19863 || *input_line_pointer == '-')
874e8986 19864 get_number ();
252b5132 19865
fd361982 19866 if ((bfd_section_flags (now_seg) & SEC_CODE) == 0)
1661c76c 19867 as_warn (_(".ent or .aent not in text section"));
252b5132
RH
19868
19869 if (!aent && cur_proc_ptr)
9a41af64 19870 as_warn (_("missing .end"));
252b5132
RH
19871
19872 if (!aent)
19873 {
7a621144
DJ
19874 /* This function needs its own .frame and .cprestore directives. */
19875 mips_frame_reg_valid = 0;
19876 mips_cprestore_valid = 0;
19877
252b5132
RH
19878 cur_proc_ptr = &cur_proc;
19879 memset (cur_proc_ptr, '\0', sizeof (procS));
19880
9b2f1d35 19881 cur_proc_ptr->func_sym = symbolP;
252b5132 19882
f9419b05 19883 ++numprocs;
ecb4347a
DJ
19884
19885 if (debug_type == DEBUG_STABS)
19886 stabs_generate_asm_func (S_GET_NAME (symbolP),
19887 S_GET_NAME (symbolP));
252b5132
RH
19888 }
19889
7c0fc524
MR
19890 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19891
252b5132
RH
19892 demand_empty_rest_of_line ();
19893}
19894
19895/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 19896 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 19897 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 19898 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
19899 symbol table (in the mdebug section). */
19900
19901static void
17a2f251 19902s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 19903{
f3ded42a
RS
19904 if (ECOFF_DEBUGGING)
19905 s_ignore (ignore);
19906 else
ecb4347a
DJ
19907 {
19908 long val;
252b5132 19909
ecb4347a
DJ
19910 if (cur_proc_ptr == (procS *) NULL)
19911 {
19912 as_warn (_(".frame outside of .ent"));
19913 demand_empty_rest_of_line ();
19914 return;
19915 }
252b5132 19916
ecb4347a
DJ
19917 cur_proc_ptr->frame_reg = tc_get_register (1);
19918
19919 SKIP_WHITESPACE ();
19920 if (*input_line_pointer++ != ','
19921 || get_absolute_expression_and_terminator (&val) != ',')
19922 {
1661c76c 19923 as_warn (_("bad .frame directive"));
ecb4347a
DJ
19924 --input_line_pointer;
19925 demand_empty_rest_of_line ();
19926 return;
19927 }
252b5132 19928
ecb4347a
DJ
19929 cur_proc_ptr->frame_offset = val;
19930 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 19931
252b5132 19932 demand_empty_rest_of_line ();
252b5132 19933 }
252b5132
RH
19934}
19935
bdaaa2e1
KH
19936/* The .fmask and .mask directives. If the mdebug section is present
19937 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 19938 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 19939 information correctly. We can't use the ecoff routines because they
252b5132
RH
19940 make reference to the ecoff symbol table (in the mdebug section). */
19941
19942static void
17a2f251 19943s_mips_mask (int reg_type)
252b5132 19944{
f3ded42a
RS
19945 if (ECOFF_DEBUGGING)
19946 s_ignore (reg_type);
19947 else
252b5132 19948 {
ecb4347a 19949 long mask, off;
252b5132 19950
ecb4347a
DJ
19951 if (cur_proc_ptr == (procS *) NULL)
19952 {
19953 as_warn (_(".mask/.fmask outside of .ent"));
19954 demand_empty_rest_of_line ();
19955 return;
19956 }
252b5132 19957
ecb4347a
DJ
19958 if (get_absolute_expression_and_terminator (&mask) != ',')
19959 {
1661c76c 19960 as_warn (_("bad .mask/.fmask directive"));
ecb4347a
DJ
19961 --input_line_pointer;
19962 demand_empty_rest_of_line ();
19963 return;
19964 }
252b5132 19965
ecb4347a
DJ
19966 off = get_absolute_expression ();
19967
19968 if (reg_type == 'F')
19969 {
19970 cur_proc_ptr->fpreg_mask = mask;
19971 cur_proc_ptr->fpreg_offset = off;
19972 }
19973 else
19974 {
19975 cur_proc_ptr->reg_mask = mask;
19976 cur_proc_ptr->reg_offset = off;
19977 }
19978
19979 demand_empty_rest_of_line ();
252b5132 19980 }
252b5132
RH
19981}
19982
316f5878
RS
19983/* A table describing all the processors gas knows about. Names are
19984 matched in the order listed.
e7af610e 19985
316f5878
RS
19986 To ease comparison, please keep this table in the same order as
19987 gcc's mips_cpu_info_table[]. */
e972090a
NC
19988static const struct mips_cpu_info mips_cpu_info_table[] =
19989{
6f2117ba 19990 /* Entries for generic ISAs. */
d16afab6
RS
19991 { "mips1", MIPS_CPU_IS_ISA, 0, ISA_MIPS1, CPU_R3000 },
19992 { "mips2", MIPS_CPU_IS_ISA, 0, ISA_MIPS2, CPU_R6000 },
19993 { "mips3", MIPS_CPU_IS_ISA, 0, ISA_MIPS3, CPU_R4000 },
19994 { "mips4", MIPS_CPU_IS_ISA, 0, ISA_MIPS4, CPU_R8000 },
19995 { "mips5", MIPS_CPU_IS_ISA, 0, ISA_MIPS5, CPU_MIPS5 },
19996 { "mips32", MIPS_CPU_IS_ISA, 0, ISA_MIPS32, CPU_MIPS32 },
19997 { "mips32r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
ae52f483
AB
19998 { "mips32r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R3, CPU_MIPS32R3 },
19999 { "mips32r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R5, CPU_MIPS32R5 },
7361da2c 20000 { "mips32r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R6, CPU_MIPS32R6 },
d16afab6
RS
20001 { "mips64", MIPS_CPU_IS_ISA, 0, ISA_MIPS64, CPU_MIPS64 },
20002 { "mips64r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R2, CPU_MIPS64R2 },
ae52f483
AB
20003 { "mips64r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R3, CPU_MIPS64R3 },
20004 { "mips64r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R5, CPU_MIPS64R5 },
7361da2c 20005 { "mips64r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R6, CPU_MIPS64R6 },
316f5878
RS
20006
20007 /* MIPS I */
d16afab6
RS
20008 { "r3000", 0, 0, ISA_MIPS1, CPU_R3000 },
20009 { "r2000", 0, 0, ISA_MIPS1, CPU_R3000 },
20010 { "r3900", 0, 0, ISA_MIPS1, CPU_R3900 },
316f5878
RS
20011
20012 /* MIPS II */
d16afab6 20013 { "r6000", 0, 0, ISA_MIPS2, CPU_R6000 },
316f5878
RS
20014
20015 /* MIPS III */
d16afab6
RS
20016 { "r4000", 0, 0, ISA_MIPS3, CPU_R4000 },
20017 { "r4010", 0, 0, ISA_MIPS2, CPU_R4010 },
20018 { "vr4100", 0, 0, ISA_MIPS3, CPU_VR4100 },
20019 { "vr4111", 0, 0, ISA_MIPS3, CPU_R4111 },
20020 { "vr4120", 0, 0, ISA_MIPS3, CPU_VR4120 },
20021 { "vr4130", 0, 0, ISA_MIPS3, CPU_VR4120 },
20022 { "vr4181", 0, 0, ISA_MIPS3, CPU_R4111 },
20023 { "vr4300", 0, 0, ISA_MIPS3, CPU_R4300 },
20024 { "r4400", 0, 0, ISA_MIPS3, CPU_R4400 },
20025 { "r4600", 0, 0, ISA_MIPS3, CPU_R4600 },
20026 { "orion", 0, 0, ISA_MIPS3, CPU_R4600 },
20027 { "r4650", 0, 0, ISA_MIPS3, CPU_R4650 },
20028 { "r5900", 0, 0, ISA_MIPS3, CPU_R5900 },
6f2117ba 20029 /* ST Microelectronics Loongson 2E and 2F cores. */
d16afab6 20030 { "loongson2e", 0, 0, ISA_MIPS3, CPU_LOONGSON_2E },
8095d2f7 20031 { "loongson2f", 0, ASE_LOONGSON_MMI, ISA_MIPS3, CPU_LOONGSON_2F },
316f5878
RS
20032
20033 /* MIPS IV */
d16afab6
RS
20034 { "r8000", 0, 0, ISA_MIPS4, CPU_R8000 },
20035 { "r10000", 0, 0, ISA_MIPS4, CPU_R10000 },
20036 { "r12000", 0, 0, ISA_MIPS4, CPU_R12000 },
20037 { "r14000", 0, 0, ISA_MIPS4, CPU_R14000 },
20038 { "r16000", 0, 0, ISA_MIPS4, CPU_R16000 },
20039 { "vr5000", 0, 0, ISA_MIPS4, CPU_R5000 },
20040 { "vr5400", 0, 0, ISA_MIPS4, CPU_VR5400 },
20041 { "vr5500", 0, 0, ISA_MIPS4, CPU_VR5500 },
20042 { "rm5200", 0, 0, ISA_MIPS4, CPU_R5000 },
20043 { "rm5230", 0, 0, ISA_MIPS4, CPU_R5000 },
20044 { "rm5231", 0, 0, ISA_MIPS4, CPU_R5000 },
20045 { "rm5261", 0, 0, ISA_MIPS4, CPU_R5000 },
20046 { "rm5721", 0, 0, ISA_MIPS4, CPU_R5000 },
20047 { "rm7000", 0, 0, ISA_MIPS4, CPU_RM7000 },
20048 { "rm9000", 0, 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
20049
20050 /* MIPS 32 */
d16afab6
RS
20051 { "4kc", 0, 0, ISA_MIPS32, CPU_MIPS32 },
20052 { "4km", 0, 0, ISA_MIPS32, CPU_MIPS32 },
20053 { "4kp", 0, 0, ISA_MIPS32, CPU_MIPS32 },
20054 { "4ksc", 0, ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
ad3fea08
TS
20055
20056 /* MIPS 32 Release 2 */
d16afab6
RS
20057 { "4kec", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20058 { "4kem", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20059 { "4kep", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20060 { "4ksd", 0, ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
20061 { "m4k", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20062 { "m4kp", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20063 { "m14k", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
20064 { "m14kc", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
20065 { "m14ke", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
20066 ISA_MIPS32R2, CPU_MIPS32R2 },
20067 { "m14kec", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
20068 ISA_MIPS32R2, CPU_MIPS32R2 },
20069 { "24kc", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20070 { "24kf2_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20071 { "24kf", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20072 { "24kf1_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 20073 /* Deprecated forms of the above. */
d16afab6
RS
20074 { "24kfx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20075 { "24kx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 20076 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
d16afab6
RS
20077 { "24kec", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
20078 { "24kef2_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
20079 { "24kef", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
20080 { "24kef1_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 20081 /* Deprecated forms of the above. */
d16afab6
RS
20082 { "24kefx", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
20083 { "24kex", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 20084 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
d16afab6
RS
20085 { "34kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20086 { "34kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20087 { "34kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20088 { "34kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 20089 /* Deprecated forms of the above. */
d16afab6
RS
20090 { "34kfx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20091 { "34kx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
711eefe4 20092 /* 34Kn is a 34kc without DSP. */
d16afab6 20093 { "34kn", 0, ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 20094 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
d16afab6
RS
20095 { "74kc", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20096 { "74kf2_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20097 { "74kf", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20098 { "74kf1_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20099 { "74kf3_2", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 20100 /* Deprecated forms of the above. */
d16afab6
RS
20101 { "74kfx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20102 { "74kx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
30f8113a 20103 /* 1004K cores are multiprocessor versions of the 34K. */
d16afab6
RS
20104 { "1004kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20105 { "1004kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20106 { "1004kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20107 { "1004kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
6f2117ba 20108 /* interaptiv is the new name for 1004kf. */
77403ce9 20109 { "interaptiv", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
38bf472a
MR
20110 { "interaptiv-mr2", 0,
20111 ASE_DSP | ASE_EVA | ASE_MT | ASE_MIPS16E2 | ASE_MIPS16E2_MT,
20112 ISA_MIPS32R3, CPU_INTERAPTIV_MR2 },
6f2117ba 20113 /* M5100 family. */
c6e5c03a
RS
20114 { "m5100", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
20115 { "m5101", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
bbaa46c0 20116 /* P5600 with EVA and Virtualization ASEs, other ASEs are optional. */
134c0c8b 20117 { "p5600", 0, ASE_VIRT | ASE_EVA | ASE_XPA, ISA_MIPS32R5, CPU_MIPS32R5 },
32b26a03 20118
316f5878 20119 /* MIPS 64 */
d16afab6
RS
20120 { "5kc", 0, 0, ISA_MIPS64, CPU_MIPS64 },
20121 { "5kf", 0, 0, ISA_MIPS64, CPU_MIPS64 },
20122 { "20kc", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
20123 { "25kf", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
ad3fea08 20124
6f2117ba 20125 /* Broadcom SB-1 CPU core. */
d16afab6 20126 { "sb1", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
6f2117ba 20127 /* Broadcom SB-1A CPU core. */
d16afab6 20128 { "sb1a", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
3739860c 20129
6f2117ba
PH
20130 /* MIPS 64 Release 2. */
20131 /* Loongson CPU core. */
20132 /* -march=loongson3a is an alias of -march=gs464 for compatibility. */
bdc6c06e 20133 { "loongson3a", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT,
ac8cb70f
CX
20134 ISA_MIPS64R2, CPU_GS464 },
20135 { "gs464", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT,
20136 ISA_MIPS64R2, CPU_GS464 },
bd782c07
CX
20137 { "gs464e", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT
20138 | ASE_LOONGSON_EXT2, ISA_MIPS64R2, CPU_GS464E },
9108bc33
CX
20139 { "gs264e", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT
20140 | ASE_LOONGSON_EXT2 | ASE_MSA | ASE_MSA64, ISA_MIPS64R2, CPU_GS264E },
ed163775 20141
6f2117ba 20142 /* Cavium Networks Octeon CPU core. */
d16afab6
RS
20143 { "octeon", 0, 0, ISA_MIPS64R2, CPU_OCTEON },
20144 { "octeon+", 0, 0, ISA_MIPS64R2, CPU_OCTEONP },
20145 { "octeon2", 0, 0, ISA_MIPS64R2, CPU_OCTEON2 },
2c629856 20146 { "octeon3", 0, ASE_VIRT | ASE_VIRT64, ISA_MIPS64R5, CPU_OCTEON3 },
967344c6 20147
52b6b6b9 20148 /* RMI Xlr */
d16afab6 20149 { "xlr", 0, 0, ISA_MIPS64, CPU_XLR },
52b6b6b9 20150
55a36193
MK
20151 /* Broadcom XLP.
20152 XLP is mostly like XLR, with the prominent exception that it is
20153 MIPS64R2 rather than MIPS64. */
d16afab6 20154 { "xlp", 0, 0, ISA_MIPS64R2, CPU_XLR },
55a36193 20155
6f2117ba 20156 /* MIPS 64 Release 6. */
bdc8beb4
MF
20157 { "i6400", 0, ASE_VIRT | ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
20158 { "i6500", 0, ASE_VIRT | ASE_MSA | ASE_CRC | ASE_GINV,
20159 ISA_MIPS64R6, CPU_MIPS64R6},
a4968f42 20160 { "p6600", 0, ASE_VIRT | ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
7ef0d297 20161
6f2117ba 20162 /* End marker. */
d16afab6 20163 { NULL, 0, 0, 0, 0 }
316f5878 20164};
e7af610e 20165
84ea6cf2 20166
316f5878
RS
20167/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
20168 with a final "000" replaced by "k". Ignore case.
e7af610e 20169
316f5878 20170 Note: this function is shared between GCC and GAS. */
c6c98b38 20171
b34976b6 20172static bfd_boolean
17a2f251 20173mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
20174{
20175 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
20176 given++, canonical++;
20177
20178 return ((*given == 0 && *canonical == 0)
20179 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
20180}
20181
20182
20183/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
20184 CPU name. We've traditionally allowed a lot of variation here.
20185
20186 Note: this function is shared between GCC and GAS. */
20187
b34976b6 20188static bfd_boolean
17a2f251 20189mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
20190{
20191 /* First see if the name matches exactly, or with a final "000"
20192 turned into "k". */
20193 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 20194 return TRUE;
316f5878
RS
20195
20196 /* If not, try comparing based on numerical designation alone.
20197 See if GIVEN is an unadorned number, or 'r' followed by a number. */
20198 if (TOLOWER (*given) == 'r')
20199 given++;
20200 if (!ISDIGIT (*given))
b34976b6 20201 return FALSE;
316f5878
RS
20202
20203 /* Skip over some well-known prefixes in the canonical name,
20204 hoping to find a number there too. */
20205 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
20206 canonical += 2;
20207 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
20208 canonical += 2;
20209 else if (TOLOWER (canonical[0]) == 'r')
20210 canonical += 1;
20211
20212 return mips_strict_matching_cpu_name_p (canonical, given);
20213}
20214
20215
20216/* Parse an option that takes the name of a processor as its argument.
20217 OPTION is the name of the option and CPU_STRING is the argument.
20218 Return the corresponding processor enumeration if the CPU_STRING is
20219 recognized, otherwise report an error and return null.
20220
20221 A similar function exists in GCC. */
e7af610e
NC
20222
20223static const struct mips_cpu_info *
17a2f251 20224mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 20225{
316f5878 20226 const struct mips_cpu_info *p;
e7af610e 20227
316f5878
RS
20228 /* 'from-abi' selects the most compatible architecture for the given
20229 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
20230 EABIs, we have to decide whether we're using the 32-bit or 64-bit
20231 version. Look first at the -mgp options, if given, otherwise base
20232 the choice on MIPS_DEFAULT_64BIT.
e7af610e 20233
316f5878
RS
20234 Treat NO_ABI like the EABIs. One reason to do this is that the
20235 plain 'mips' and 'mips64' configs have 'from-abi' as their default
20236 architecture. This code picks MIPS I for 'mips' and MIPS III for
20237 'mips64', just as we did in the days before 'from-abi'. */
20238 if (strcasecmp (cpu_string, "from-abi") == 0)
20239 {
20240 if (ABI_NEEDS_32BIT_REGS (mips_abi))
20241 return mips_cpu_info_from_isa (ISA_MIPS1);
20242
20243 if (ABI_NEEDS_64BIT_REGS (mips_abi))
20244 return mips_cpu_info_from_isa (ISA_MIPS3);
20245
bad1aba3 20246 if (file_mips_opts.gp >= 0)
20247 return mips_cpu_info_from_isa (file_mips_opts.gp == 32
0b35dfee 20248 ? ISA_MIPS1 : ISA_MIPS3);
316f5878
RS
20249
20250 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
20251 ? ISA_MIPS3
20252 : ISA_MIPS1);
20253 }
20254
20255 /* 'default' has traditionally been a no-op. Probably not very useful. */
20256 if (strcasecmp (cpu_string, "default") == 0)
20257 return 0;
20258
20259 for (p = mips_cpu_info_table; p->name != 0; p++)
20260 if (mips_matching_cpu_name_p (p->name, cpu_string))
20261 return p;
20262
1661c76c 20263 as_bad (_("bad value (%s) for %s"), cpu_string, option);
316f5878 20264 return 0;
e7af610e
NC
20265}
20266
316f5878
RS
20267/* Return the canonical processor information for ISA (a member of the
20268 ISA_MIPS* enumeration). */
20269
e7af610e 20270static const struct mips_cpu_info *
17a2f251 20271mips_cpu_info_from_isa (int isa)
e7af610e
NC
20272{
20273 int i;
20274
20275 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
ad3fea08 20276 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
316f5878 20277 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
20278 return (&mips_cpu_info_table[i]);
20279
e972090a 20280 return NULL;
e7af610e 20281}
fef14a42
TS
20282
20283static const struct mips_cpu_info *
17a2f251 20284mips_cpu_info_from_arch (int arch)
fef14a42
TS
20285{
20286 int i;
20287
20288 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
20289 if (arch == mips_cpu_info_table[i].cpu)
20290 return (&mips_cpu_info_table[i]);
20291
20292 return NULL;
20293}
316f5878
RS
20294\f
20295static void
17a2f251 20296show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
20297{
20298 if (*first_p)
20299 {
20300 fprintf (stream, "%24s", "");
20301 *col_p = 24;
20302 }
20303 else
20304 {
20305 fprintf (stream, ", ");
20306 *col_p += 2;
20307 }
e7af610e 20308
316f5878
RS
20309 if (*col_p + strlen (string) > 72)
20310 {
20311 fprintf (stream, "\n%24s", "");
20312 *col_p = 24;
20313 }
20314
20315 fprintf (stream, "%s", string);
20316 *col_p += strlen (string);
20317
20318 *first_p = 0;
20319}
20320
20321void
17a2f251 20322md_show_usage (FILE *stream)
e7af610e 20323{
316f5878
RS
20324 int column, first;
20325 size_t i;
20326
20327 fprintf (stream, _("\
20328MIPS options:\n\
316f5878
RS
20329-EB generate big endian output\n\
20330-EL generate little endian output\n\
20331-g, -g2 do not remove unneeded NOPs or swap branches\n\
20332-G NUM allow referencing objects up to NUM bytes\n\
20333 implicitly with the gp register [default 8]\n"));
20334 fprintf (stream, _("\
20335-mips1 generate MIPS ISA I instructions\n\
20336-mips2 generate MIPS ISA II instructions\n\
20337-mips3 generate MIPS ISA III instructions\n\
20338-mips4 generate MIPS ISA IV instructions\n\
20339-mips5 generate MIPS ISA V instructions\n\
20340-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 20341-mips32r2 generate MIPS32 release 2 ISA instructions\n\
ae52f483
AB
20342-mips32r3 generate MIPS32 release 3 ISA instructions\n\
20343-mips32r5 generate MIPS32 release 5 ISA instructions\n\
7361da2c 20344-mips32r6 generate MIPS32 release 6 ISA instructions\n\
316f5878 20345-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 20346-mips64r2 generate MIPS64 release 2 ISA instructions\n\
ae52f483
AB
20347-mips64r3 generate MIPS64 release 3 ISA instructions\n\
20348-mips64r5 generate MIPS64 release 5 ISA instructions\n\
7361da2c 20349-mips64r6 generate MIPS64 release 6 ISA instructions\n\
316f5878
RS
20350-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
20351
20352 first = 1;
e7af610e
NC
20353
20354 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
20355 show (stream, mips_cpu_info_table[i].name, &column, &first);
20356 show (stream, "from-abi", &column, &first);
20357 fputc ('\n', stream);
e7af610e 20358
316f5878
RS
20359 fprintf (stream, _("\
20360-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
20361-no-mCPU don't generate code specific to CPU.\n\
20362 For -mCPU and -no-mCPU, CPU must be one of:\n"));
20363
20364 first = 1;
20365
20366 show (stream, "3900", &column, &first);
20367 show (stream, "4010", &column, &first);
20368 show (stream, "4100", &column, &first);
20369 show (stream, "4650", &column, &first);
20370 fputc ('\n', stream);
20371
20372 fprintf (stream, _("\
20373-mips16 generate mips16 instructions\n\
20374-no-mips16 do not generate mips16 instructions\n"));
20375 fprintf (stream, _("\
f866b262
MR
20376-mmips16e2 generate MIPS16e2 instructions\n\
20377-mno-mips16e2 do not generate MIPS16e2 instructions\n"));
20378 fprintf (stream, _("\
df58fc94
RS
20379-mmicromips generate microMIPS instructions\n\
20380-mno-micromips do not generate microMIPS instructions\n"));
20381 fprintf (stream, _("\
e16bfa71 20382-msmartmips generate smartmips instructions\n\
3739860c 20383-mno-smartmips do not generate smartmips instructions\n"));
e16bfa71 20384 fprintf (stream, _("\
74cd071d
CF
20385-mdsp generate DSP instructions\n\
20386-mno-dsp do not generate DSP instructions\n"));
20387 fprintf (stream, _("\
8b082fb1
TS
20388-mdspr2 generate DSP R2 instructions\n\
20389-mno-dspr2 do not generate DSP R2 instructions\n"));
20390 fprintf (stream, _("\
8f4f9071
MF
20391-mdspr3 generate DSP R3 instructions\n\
20392-mno-dspr3 do not generate DSP R3 instructions\n"));
20393 fprintf (stream, _("\
ef2e4d86
CF
20394-mmt generate MT instructions\n\
20395-mno-mt do not generate MT instructions\n"));
20396 fprintf (stream, _("\
dec0624d
MR
20397-mmcu generate MCU instructions\n\
20398-mno-mcu do not generate MCU instructions\n"));
20399 fprintf (stream, _("\
56d438b1
CF
20400-mmsa generate MSA instructions\n\
20401-mno-msa do not generate MSA instructions\n"));
20402 fprintf (stream, _("\
7d64c587
AB
20403-mxpa generate eXtended Physical Address (XPA) instructions\n\
20404-mno-xpa do not generate eXtended Physical Address (XPA) instructions\n"));
20405 fprintf (stream, _("\
b015e599
AP
20406-mvirt generate Virtualization instructions\n\
20407-mno-virt do not generate Virtualization instructions\n"));
20408 fprintf (stream, _("\
730c3174
SE
20409-mcrc generate CRC instructions\n\
20410-mno-crc do not generate CRC instructions\n"));
20411 fprintf (stream, _("\
6f20c942
FS
20412-mginv generate Global INValidate (GINV) instructions\n\
20413-mno-ginv do not generate Global INValidate instructions\n"));
20414 fprintf (stream, _("\
8095d2f7
CX
20415-mloongson-mmi generate Loongson MultiMedia extensions Instructions (MMI) instructions\n\
20416-mno-loongson-mmi do not generate Loongson MultiMedia extensions Instructions\n"));
20417 fprintf (stream, _("\
716c08de
CX
20418-mloongson-cam generate Loongson Content Address Memory (CAM) instructions\n\
20419-mno-loongson-cam do not generate Loongson Content Address Memory Instructions\n"));
20420 fprintf (stream, _("\
bdc6c06e
CX
20421-mloongson-ext generate Loongson EXTensions (EXT) instructions\n\
20422-mno-loongson-ext do not generate Loongson EXTensions Instructions\n"));
20423 fprintf (stream, _("\
a693765e
CX
20424-mloongson-ext2 generate Loongson EXTensions R2 (EXT2) instructions\n\
20425-mno-loongson-ext2 do not generate Loongson EXTensions R2 Instructions\n"));
20426 fprintf (stream, _("\
833794fc
MR
20427-minsn32 only generate 32-bit microMIPS instructions\n\
20428-mno-insn32 generate all microMIPS instructions\n"));
6f2117ba
PH
20429#if DEFAULT_MIPS_FIX_LOONGSON3_LLSC
20430 fprintf (stream, _("\
20431-mfix-loongson3-llsc work around Loongson3 LL/SC errata, default\n\
20432-mno-fix-loongson3-llsc disable work around Loongson3 LL/SC errata\n"));
20433#else
20434 fprintf (stream, _("\
20435-mfix-loongson3-llsc work around Loongson3 LL/SC errata\n\
20436-mno-fix-loongson3-llsc disable work around Loongson3 LL/SC errata, default\n"));
20437#endif
833794fc 20438 fprintf (stream, _("\
c67a084a
NC
20439-mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
20440-mfix-loongson2f-nop work around Loongson2F NOP errata\n\
6f2117ba
PH
20441-mfix-loongson3-llsc work around Loongson3 LL/SC errata\n\
20442-mno-fix-loongson3-llsc disable work around Loongson3 LL/SC errata\n\
d766e8ec 20443-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 20444-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
6a32d874 20445-mfix-24k insert a nop after ERET and DERET instructions\n\
d954098f 20446-mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
27c634e0 20447-mfix-r5900 work around R5900 short loop errata\n\
316f5878
RS
20448-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
20449-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261 20450-msym32 assume all symbols have 32-bit values\n\
092a534f
MR
20451-O0 do not remove unneeded NOPs, do not swap branches\n\
20452-O, -O1 remove unneeded NOPs, do not swap branches\n\
20453-O2 remove unneeded NOPs and swap branches\n\
316f5878
RS
20454--trap, --no-break trap exception on div by 0 and mult overflow\n\
20455--break, --no-trap break exception on div by 0 and mult overflow\n"));
037b32b9
AN
20456 fprintf (stream, _("\
20457-mhard-float allow floating-point instructions\n\
20458-msoft-float do not allow floating-point instructions\n\
20459-msingle-float only allow 32-bit floating-point operations\n\
20460-mdouble-float allow 32-bit and 64-bit floating-point operations\n\
3bf0dbfb 20461--[no-]construct-floats [dis]allow floating point values to be constructed\n\
ba92f887 20462--[no-]relax-branch [dis]allow out-of-range branches to be relaxed\n\
8b10b0b3
MR
20463-mignore-branch-isa accept invalid branches requiring an ISA mode switch\n\
20464-mno-ignore-branch-isa reject invalid branches requiring an ISA mode switch\n\
ba92f887
MR
20465-mnan=ENCODING select an IEEE 754 NaN encoding convention, either of:\n"));
20466
20467 first = 1;
20468
20469 show (stream, "legacy", &column, &first);
20470 show (stream, "2008", &column, &first);
20471
20472 fputc ('\n', stream);
20473
316f5878
RS
20474 fprintf (stream, _("\
20475-KPIC, -call_shared generate SVR4 position independent code\n\
861fb55a 20476-call_nonpic generate non-PIC code that can operate with DSOs\n\
0c000745 20477-mvxworks-pic generate VxWorks position independent code\n\
861fb55a 20478-non_shared do not generate code that can operate with DSOs\n\
316f5878 20479-xgot assume a 32 bit GOT\n\
dcd410fe 20480-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8 20481-mshared, -mno-shared disable/enable .cpload optimization for\n\
d821e36b 20482 position dependent (non shared) code\n\
316f5878
RS
20483-mabi=ABI create ABI conformant object file for:\n"));
20484
20485 first = 1;
20486
20487 show (stream, "32", &column, &first);
20488 show (stream, "o64", &column, &first);
20489 show (stream, "n32", &column, &first);
20490 show (stream, "64", &column, &first);
20491 show (stream, "eabi", &column, &first);
20492
20493 fputc ('\n', stream);
20494
20495 fprintf (stream, _("\
b4f6242e
MR
20496-32 create o32 ABI object file%s\n"),
20497 MIPS_DEFAULT_ABI == O32_ABI ? _(" (default)") : "");
20498 fprintf (stream, _("\
20499-n32 create n32 ABI object file%s\n"),
20500 MIPS_DEFAULT_ABI == N32_ABI ? _(" (default)") : "");
20501 fprintf (stream, _("\
20502-64 create 64 ABI object file%s\n"),
20503 MIPS_DEFAULT_ABI == N64_ABI ? _(" (default)") : "");
e7af610e 20504}
14e777e0 20505
1575952e 20506#ifdef TE_IRIX
14e777e0 20507enum dwarf2_format
413a266c 20508mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
14e777e0 20509{
369943fe 20510 if (HAVE_64BIT_SYMBOLS)
1575952e 20511 return dwarf2_format_64bit_irix;
14e777e0
KB
20512 else
20513 return dwarf2_format_32bit;
20514}
1575952e 20515#endif
73369e65
EC
20516
20517int
20518mips_dwarf2_addr_size (void)
20519{
6b6b3450 20520 if (HAVE_64BIT_OBJECTS)
73369e65 20521 return 8;
73369e65
EC
20522 else
20523 return 4;
20524}
5862107c
EC
20525
20526/* Standard calling conventions leave the CFA at SP on entry. */
20527void
20528mips_cfi_frame_initial_instructions (void)
20529{
20530 cfi_add_CFA_def_cfa_register (SP);
20531}
20532
707bfff6
TS
20533int
20534tc_mips_regname_to_dw2regnum (char *regname)
20535{
20536 unsigned int regnum = -1;
20537 unsigned int reg;
20538
20539 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
20540 regnum = reg;
20541
20542 return regnum;
20543}
263b2574 20544
20545/* Implement CONVERT_SYMBOLIC_ATTRIBUTE.
20546 Given a symbolic attribute NAME, return the proper integer value.
20547 Returns -1 if the attribute is not known. */
20548
20549int
20550mips_convert_symbolic_attribute (const char *name)
20551{
20552 static const struct
20553 {
20554 const char * name;
20555 const int tag;
20556 }
20557 attribute_table[] =
20558 {
20559#define T(tag) {#tag, tag}
20560 T (Tag_GNU_MIPS_ABI_FP),
20561 T (Tag_GNU_MIPS_ABI_MSA),
20562#undef T
20563 };
20564 unsigned int i;
20565
20566 if (name == NULL)
20567 return -1;
20568
20569 for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
20570 if (streq (name, attribute_table[i].name))
20571 return attribute_table[i].tag;
20572
20573 return -1;
20574}
fd5c94ab
RS
20575
20576void
20577md_mips_end (void)
20578{
351cdf24
MF
20579 int fpabi = Val_GNU_MIPS_ABI_FP_ANY;
20580
fd5c94ab
RS
20581 mips_emit_delays ();
20582 if (cur_proc_ptr)
20583 as_warn (_("missing .end at end of assembly"));
919731af 20584
20585 /* Just in case no code was emitted, do the consistency check. */
20586 file_mips_check_options ();
351cdf24
MF
20587
20588 /* Set a floating-point ABI if the user did not. */
20589 if (obj_elf_seen_attribute (OBJ_ATTR_GNU, Tag_GNU_MIPS_ABI_FP))
20590 {
20591 /* Perform consistency checks on the floating-point ABI. */
20592 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20593 Tag_GNU_MIPS_ABI_FP);
20594 if (fpabi != Val_GNU_MIPS_ABI_FP_ANY)
20595 check_fpabi (fpabi);
20596 }
20597 else
20598 {
20599 /* Soft-float gets precedence over single-float, the two options should
20600 not be used together so this should not matter. */
20601 if (file_mips_opts.soft_float == 1)
20602 fpabi = Val_GNU_MIPS_ABI_FP_SOFT;
20603 /* Single-float gets precedence over all double_float cases. */
20604 else if (file_mips_opts.single_float == 1)
20605 fpabi = Val_GNU_MIPS_ABI_FP_SINGLE;
20606 else
20607 {
20608 switch (file_mips_opts.fp)
20609 {
20610 case 32:
20611 if (file_mips_opts.gp == 32)
20612 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20613 break;
20614 case 0:
20615 fpabi = Val_GNU_MIPS_ABI_FP_XX;
20616 break;
20617 case 64:
20618 if (file_mips_opts.gp == 32 && !file_mips_opts.oddspreg)
20619 fpabi = Val_GNU_MIPS_ABI_FP_64A;
20620 else if (file_mips_opts.gp == 32)
20621 fpabi = Val_GNU_MIPS_ABI_FP_64;
20622 else
20623 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20624 break;
20625 }
20626 }
20627
20628 bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20629 Tag_GNU_MIPS_ABI_FP, fpabi);
20630 }
fd5c94ab 20631}
2f0c68f2
CM
20632
20633/* Returns the relocation type required for a particular CFI encoding. */
20634
20635bfd_reloc_code_real_type
20636mips_cfi_reloc_for_encoding (int encoding)
20637{
20638 if (encoding == (DW_EH_PE_sdata4 | DW_EH_PE_pcrel))
20639 return BFD_RELOC_32_PCREL;
20640 else return BFD_RELOC_NONE;
20641}
This page took 3.382323 seconds and 4 git commands to generate.