[MIPS] Apply ASE information for the selected processor
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
CommitLineData
252b5132 1/* tc-mips.c -- assemble code for a MIPS chip.
82704155 2 Copyright (C) 1993-2019 Free Software Foundation, Inc.
252b5132
RH
3 Contributed by the OSF and Ralph Campbell.
4 Written by Keith Knowles and Ralph Campbell, working independently.
5 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
6 Support.
7
8 This file is part of GAS.
9
10 GAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
ec2655a6 12 the Free Software Foundation; either version 3, or (at your option)
252b5132
RH
13 any later version.
14
15 GAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GAS; see the file COPYING. If not, write to the Free
4b4da160
NC
22 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
23 02110-1301, USA. */
252b5132
RH
24
25#include "as.h"
26#include "config.h"
27#include "subsegs.h"
3882b010 28#include "safe-ctype.h"
252b5132 29
252b5132
RH
30#include "opcode/mips.h"
31#include "itbl-ops.h"
c5dd6aab 32#include "dwarf2dbg.h"
5862107c 33#include "dw2gencfi.h"
252b5132 34
42429eac
RS
35/* Check assumptions made in this file. */
36typedef char static_assert1[sizeof (offsetT) < 8 ? -1 : 1];
37typedef char static_assert2[sizeof (valueT) < 8 ? -1 : 1];
38
252b5132
RH
39#ifdef DEBUG
40#define DBG(x) printf x
41#else
42#define DBG(x)
43#endif
44
263b2574 45#define streq(a, b) (strcmp (a, b) == 0)
46
9e12b7a2
RS
47#define SKIP_SPACE_TABS(S) \
48 do { while (*(S) == ' ' || *(S) == '\t') ++(S); } while (0)
49
252b5132 50/* Clean up namespace so we can include obj-elf.h too. */
17a2f251
TS
51static int mips_output_flavor (void);
52static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
252b5132
RH
53#undef OBJ_PROCESS_STAB
54#undef OUTPUT_FLAVOR
55#undef S_GET_ALIGN
56#undef S_GET_SIZE
57#undef S_SET_ALIGN
58#undef S_SET_SIZE
252b5132
RH
59#undef obj_frob_file
60#undef obj_frob_file_after_relocs
61#undef obj_frob_symbol
62#undef obj_pop_insert
63#undef obj_sec_sym_ok_for_reloc
64#undef OBJ_COPY_SYMBOL_ATTRIBUTES
65
66#include "obj-elf.h"
67/* Fix any of them that we actually care about. */
68#undef OUTPUT_FLAVOR
69#define OUTPUT_FLAVOR mips_output_flavor()
252b5132 70
252b5132 71#include "elf/mips.h"
252b5132
RH
72
73#ifndef ECOFF_DEBUGGING
74#define NO_ECOFF_DEBUGGING
75#define ECOFF_DEBUGGING 0
76#endif
77
ecb4347a
DJ
78int mips_flag_mdebug = -1;
79
dcd410fe
RO
80/* Control generation of .pdr sections. Off by default on IRIX: the native
81 linker doesn't know about and discards them, but relocations against them
82 remain, leading to rld crashes. */
83#ifdef TE_IRIX
84int mips_flag_pdr = FALSE;
85#else
86int mips_flag_pdr = TRUE;
87#endif
88
252b5132
RH
89#include "ecoff.h"
90
252b5132 91static char *mips_regmask_frag;
351cdf24 92static char *mips_flags_frag;
252b5132 93
85b51719 94#define ZERO 0
741fe287 95#define ATREG 1
df58fc94
RS
96#define S0 16
97#define S7 23
252b5132
RH
98#define TREG 24
99#define PIC_CALL_REG 25
100#define KT0 26
101#define KT1 27
102#define GP 28
103#define SP 29
104#define FP 30
105#define RA 31
106
107#define ILLEGAL_REG (32)
108
741fe287
MR
109#define AT mips_opts.at
110
252b5132
RH
111extern int target_big_endian;
112
252b5132 113/* The name of the readonly data section. */
e8044f35 114#define RDATA_SECTION_NAME ".rodata"
252b5132 115
a4e06468
RS
116/* Ways in which an instruction can be "appended" to the output. */
117enum append_method {
118 /* Just add it normally. */
119 APPEND_ADD,
120
121 /* Add it normally and then add a nop. */
122 APPEND_ADD_WITH_NOP,
123
124 /* Turn an instruction with a delay slot into a "compact" version. */
125 APPEND_ADD_COMPACT,
126
127 /* Insert the instruction before the last one. */
128 APPEND_SWAP
129};
130
47e39b9d
RS
131/* Information about an instruction, including its format, operands
132 and fixups. */
133struct mips_cl_insn
134{
135 /* The opcode's entry in mips_opcodes or mips16_opcodes. */
136 const struct mips_opcode *insn_mo;
137
47e39b9d 138 /* The 16-bit or 32-bit bitstring of the instruction itself. This is
5c04167a
RS
139 a copy of INSN_MO->match with the operands filled in. If we have
140 decided to use an extended MIPS16 instruction, this includes the
141 extension. */
47e39b9d
RS
142 unsigned long insn_opcode;
143
6f2117ba
PH
144 /* The name if this is an label. */
145 char label[16];
146
147 /* The target label name if this is an branch. */
148 char target[16];
149
47e39b9d
RS
150 /* The frag that contains the instruction. */
151 struct frag *frag;
152
153 /* The offset into FRAG of the first instruction byte. */
154 long where;
155
156 /* The relocs associated with the instruction, if any. */
157 fixS *fixp[3];
158
a38419a5
RS
159 /* True if this entry cannot be moved from its current position. */
160 unsigned int fixed_p : 1;
47e39b9d 161
708587a4 162 /* True if this instruction occurred in a .set noreorder block. */
47e39b9d
RS
163 unsigned int noreorder_p : 1;
164
2fa15973
RS
165 /* True for mips16 instructions that jump to an absolute address. */
166 unsigned int mips16_absolute_jump_p : 1;
15be625d
CM
167
168 /* True if this instruction is complete. */
169 unsigned int complete_p : 1;
e407c74b
NC
170
171 /* True if this instruction is cleared from history by unconditional
172 branch. */
173 unsigned int cleared_p : 1;
47e39b9d
RS
174};
175
a325df1d
TS
176/* The ABI to use. */
177enum mips_abi_level
178{
179 NO_ABI = 0,
180 O32_ABI,
181 O64_ABI,
182 N32_ABI,
183 N64_ABI,
184 EABI_ABI
185};
186
187/* MIPS ABI we are using for this output file. */
316f5878 188static enum mips_abi_level mips_abi = NO_ABI;
a325df1d 189
143d77c5
EC
190/* Whether or not we have code that can call pic code. */
191int mips_abicalls = FALSE;
192
aa6975fb
ILT
193/* Whether or not we have code which can be put into a shared
194 library. */
195static bfd_boolean mips_in_shared = TRUE;
196
252b5132
RH
197/* This is the set of options which may be modified by the .set
198 pseudo-op. We use a struct so that .set push and .set pop are more
199 reliable. */
200
e972090a
NC
201struct mips_set_options
202{
252b5132
RH
203 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
204 if it has not been initialized. Changed by `.set mipsN', and the
205 -mipsN command line option, and the default CPU. */
206 int isa;
846ef2d0
RS
207 /* Enabled Application Specific Extensions (ASEs). Changed by `.set
208 <asename>', by command line options, and based on the default
209 architecture. */
210 int ase;
252b5132
RH
211 /* Whether we are assembling for the mips16 processor. 0 if we are
212 not, 1 if we are, and -1 if the value has not been initialized.
213 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
214 -nomips16 command line options, and the default CPU. */
215 int mips16;
df58fc94
RS
216 /* Whether we are assembling for the mipsMIPS ASE. 0 if we are not,
217 1 if we are, and -1 if the value has not been initialized. Changed
218 by `.set micromips' and `.set nomicromips', and the -mmicromips
219 and -mno-micromips command line options, and the default CPU. */
220 int micromips;
252b5132
RH
221 /* Non-zero if we should not reorder instructions. Changed by `.set
222 reorder' and `.set noreorder'. */
223 int noreorder;
741fe287
MR
224 /* Non-zero if we should not permit the register designated "assembler
225 temporary" to be used in instructions. The value is the register
226 number, normally $at ($1). Changed by `.set at=REG', `.set noat'
227 (same as `.set at=$0') and `.set at' (same as `.set at=$1'). */
228 unsigned int at;
252b5132
RH
229 /* Non-zero if we should warn when a macro instruction expands into
230 more than one machine instruction. Changed by `.set nomacro' and
231 `.set macro'. */
232 int warn_about_macros;
233 /* Non-zero if we should not move instructions. Changed by `.set
234 move', `.set volatile', `.set nomove', and `.set novolatile'. */
235 int nomove;
236 /* Non-zero if we should not optimize branches by moving the target
237 of the branch into the delay slot. Actually, we don't perform
238 this optimization anyhow. Changed by `.set bopt' and `.set
239 nobopt'. */
240 int nobopt;
241 /* Non-zero if we should not autoextend mips16 instructions.
242 Changed by `.set autoextend' and `.set noautoextend'. */
243 int noautoextend;
833794fc
MR
244 /* True if we should only emit 32-bit microMIPS instructions.
245 Changed by `.set insn32' and `.set noinsn32', and the -minsn32
246 and -mno-insn32 command line options. */
247 bfd_boolean insn32;
a325df1d
TS
248 /* Restrict general purpose registers and floating point registers
249 to 32 bit. This is initially determined when -mgp32 or -mfp32
250 is passed but can changed if the assembler code uses .set mipsN. */
bad1aba3 251 int gp;
0b35dfee 252 int fp;
fef14a42
TS
253 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
254 command line option, and the default CPU. */
255 int arch;
aed1a261
RS
256 /* True if ".set sym32" is in effect. */
257 bfd_boolean sym32;
037b32b9
AN
258 /* True if floating-point operations are not allowed. Changed by .set
259 softfloat or .set hardfloat, by command line options -msoft-float or
260 -mhard-float. The default is false. */
261 bfd_boolean soft_float;
262
263 /* True if only single-precision floating-point operations are allowed.
264 Changed by .set singlefloat or .set doublefloat, command-line options
265 -msingle-float or -mdouble-float. The default is false. */
266 bfd_boolean single_float;
351cdf24
MF
267
268 /* 1 if single-precision operations on odd-numbered registers are
269 allowed. */
270 int oddspreg;
3315614d
MF
271
272 /* The set of ASEs that should be enabled for the user specified
273 architecture. This cannot be inferred from 'arch' for all cores
274 as processors only have a unique 'arch' if they add architecture
275 specific instructions (UDI). */
276 int init_ase;
252b5132
RH
277};
278
919731af 279/* Specifies whether module level options have been checked yet. */
280static bfd_boolean file_mips_opts_checked = FALSE;
281
7361da2c
AB
282/* Do we support nan2008? 0 if we don't, 1 if we do, and -1 if the
283 value has not been initialized. Changed by `.nan legacy' and
284 `.nan 2008', and the -mnan=legacy and -mnan=2008 command line
285 options, and the default CPU. */
286static int mips_nan2008 = -1;
a325df1d 287
0b35dfee 288/* This is the struct we use to hold the module level set of options.
bad1aba3 289 Note that we must set the isa field to ISA_UNKNOWN and the ASE, gp and
0b35dfee 290 fp fields to -1 to indicate that they have not been initialized. */
037b32b9 291
0b35dfee 292static struct mips_set_options file_mips_opts =
293{
294 /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
295 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
296 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
bad1aba3 297 /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
3315614d
MF
298 /* soft_float */ FALSE, /* single_float */ FALSE, /* oddspreg */ -1,
299 /* init_ase */ 0
0b35dfee 300};
252b5132 301
0b35dfee 302/* This is similar to file_mips_opts, but for the current set of options. */
ba92f887 303
e972090a
NC
304static struct mips_set_options mips_opts =
305{
846ef2d0 306 /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
b015e599 307 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
833794fc 308 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
bad1aba3 309 /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
3315614d
MF
310 /* soft_float */ FALSE, /* single_float */ FALSE, /* oddspreg */ -1,
311 /* init_ase */ 0
e7af610e 312};
252b5132 313
846ef2d0
RS
314/* Which bits of file_ase were explicitly set or cleared by ASE options. */
315static unsigned int file_ase_explicit;
316
252b5132
RH
317/* These variables are filled in with the masks of registers used.
318 The object format code reads them and puts them in the appropriate
319 place. */
320unsigned long mips_gprmask;
321unsigned long mips_cprmask[4];
322
738f4d98 323/* True if any MIPS16 code was produced. */
a4672219
TS
324static int file_ase_mips16;
325
3994f87e
TS
326#define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32 \
327 || mips_opts.isa == ISA_MIPS32R2 \
ae52f483
AB
328 || mips_opts.isa == ISA_MIPS32R3 \
329 || mips_opts.isa == ISA_MIPS32R5 \
3994f87e 330 || mips_opts.isa == ISA_MIPS64 \
ae52f483
AB
331 || mips_opts.isa == ISA_MIPS64R2 \
332 || mips_opts.isa == ISA_MIPS64R3 \
333 || mips_opts.isa == ISA_MIPS64R5)
3994f87e 334
df58fc94
RS
335/* True if any microMIPS code was produced. */
336static int file_ase_micromips;
337
b12dd2e4
CF
338/* True if we want to create R_MIPS_JALR for jalr $25. */
339#ifdef TE_IRIX
1180b5a4 340#define MIPS_JALR_HINT_P(EXPR) HAVE_NEWABI
b12dd2e4 341#else
1180b5a4
RS
342/* As a GNU extension, we use R_MIPS_JALR for o32 too. However,
343 because there's no place for any addend, the only acceptable
344 expression is a bare symbol. */
345#define MIPS_JALR_HINT_P(EXPR) \
346 (!HAVE_IN_PLACE_ADDENDS \
347 || ((EXPR)->X_op == O_symbol && (EXPR)->X_add_number == 0))
b12dd2e4
CF
348#endif
349
ec68c924 350/* The argument of the -march= flag. The architecture we are assembling. */
316f5878 351static const char *mips_arch_string;
ec68c924
EC
352
353/* The argument of the -mtune= flag. The architecture for which we
354 are optimizing. */
355static int mips_tune = CPU_UNKNOWN;
316f5878 356static const char *mips_tune_string;
ec68c924 357
316f5878 358/* True when generating 32-bit code for a 64-bit processor. */
252b5132
RH
359static int mips_32bitmode = 0;
360
316f5878
RS
361/* True if the given ABI requires 32-bit registers. */
362#define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
363
364/* Likewise 64-bit registers. */
707bfff6 365#define ABI_NEEDS_64BIT_REGS(ABI) \
134c0c8b 366 ((ABI) == N32_ABI \
707bfff6 367 || (ABI) == N64_ABI \
316f5878
RS
368 || (ABI) == O64_ABI)
369
7361da2c
AB
370#define ISA_IS_R6(ISA) \
371 ((ISA) == ISA_MIPS32R6 \
372 || (ISA) == ISA_MIPS64R6)
373
ad3fea08 374/* Return true if ISA supports 64 bit wide gp registers. */
707bfff6
TS
375#define ISA_HAS_64BIT_REGS(ISA) \
376 ((ISA) == ISA_MIPS3 \
377 || (ISA) == ISA_MIPS4 \
378 || (ISA) == ISA_MIPS5 \
379 || (ISA) == ISA_MIPS64 \
ae52f483
AB
380 || (ISA) == ISA_MIPS64R2 \
381 || (ISA) == ISA_MIPS64R3 \
7361da2c
AB
382 || (ISA) == ISA_MIPS64R5 \
383 || (ISA) == ISA_MIPS64R6)
9ce8a5dd 384
ad3fea08
TS
385/* Return true if ISA supports 64 bit wide float registers. */
386#define ISA_HAS_64BIT_FPRS(ISA) \
387 ((ISA) == ISA_MIPS3 \
388 || (ISA) == ISA_MIPS4 \
389 || (ISA) == ISA_MIPS5 \
390 || (ISA) == ISA_MIPS32R2 \
ae52f483
AB
391 || (ISA) == ISA_MIPS32R3 \
392 || (ISA) == ISA_MIPS32R5 \
7361da2c 393 || (ISA) == ISA_MIPS32R6 \
ad3fea08 394 || (ISA) == ISA_MIPS64 \
ae52f483
AB
395 || (ISA) == ISA_MIPS64R2 \
396 || (ISA) == ISA_MIPS64R3 \
7361da2c
AB
397 || (ISA) == ISA_MIPS64R5 \
398 || (ISA) == ISA_MIPS64R6)
ad3fea08 399
af7ee8bf
CD
400/* Return true if ISA supports 64-bit right rotate (dror et al.)
401 instructions. */
707bfff6 402#define ISA_HAS_DROR(ISA) \
df58fc94 403 ((ISA) == ISA_MIPS64R2 \
ae52f483
AB
404 || (ISA) == ISA_MIPS64R3 \
405 || (ISA) == ISA_MIPS64R5 \
7361da2c 406 || (ISA) == ISA_MIPS64R6 \
df58fc94
RS
407 || (mips_opts.micromips \
408 && ISA_HAS_64BIT_REGS (ISA)) \
409 )
af7ee8bf
CD
410
411/* Return true if ISA supports 32-bit right rotate (ror et al.)
412 instructions. */
707bfff6
TS
413#define ISA_HAS_ROR(ISA) \
414 ((ISA) == ISA_MIPS32R2 \
ae52f483
AB
415 || (ISA) == ISA_MIPS32R3 \
416 || (ISA) == ISA_MIPS32R5 \
7361da2c 417 || (ISA) == ISA_MIPS32R6 \
707bfff6 418 || (ISA) == ISA_MIPS64R2 \
ae52f483
AB
419 || (ISA) == ISA_MIPS64R3 \
420 || (ISA) == ISA_MIPS64R5 \
7361da2c 421 || (ISA) == ISA_MIPS64R6 \
846ef2d0 422 || (mips_opts.ase & ASE_SMARTMIPS) \
df58fc94
RS
423 || mips_opts.micromips \
424 )
707bfff6 425
7455baf8 426/* Return true if ISA supports single-precision floats in odd registers. */
351cdf24
MF
427#define ISA_HAS_ODD_SINGLE_FPR(ISA, CPU)\
428 (((ISA) == ISA_MIPS32 \
429 || (ISA) == ISA_MIPS32R2 \
430 || (ISA) == ISA_MIPS32R3 \
431 || (ISA) == ISA_MIPS32R5 \
7361da2c 432 || (ISA) == ISA_MIPS32R6 \
351cdf24
MF
433 || (ISA) == ISA_MIPS64 \
434 || (ISA) == ISA_MIPS64R2 \
435 || (ISA) == ISA_MIPS64R3 \
436 || (ISA) == ISA_MIPS64R5 \
7361da2c 437 || (ISA) == ISA_MIPS64R6 \
351cdf24 438 || (CPU) == CPU_R5900) \
bd782c07 439 && ((CPU) != CPU_GS464 \
9108bc33
CX
440 || (CPU) != CPU_GS464E \
441 || (CPU) != CPU_GS264E))
af7ee8bf 442
ad3fea08
TS
443/* Return true if ISA supports move to/from high part of a 64-bit
444 floating-point register. */
445#define ISA_HAS_MXHC1(ISA) \
446 ((ISA) == ISA_MIPS32R2 \
ae52f483
AB
447 || (ISA) == ISA_MIPS32R3 \
448 || (ISA) == ISA_MIPS32R5 \
7361da2c
AB
449 || (ISA) == ISA_MIPS32R6 \
450 || (ISA) == ISA_MIPS64R2 \
451 || (ISA) == ISA_MIPS64R3 \
452 || (ISA) == ISA_MIPS64R5 \
453 || (ISA) == ISA_MIPS64R6)
454
455/* Return true if ISA supports legacy NAN. */
456#define ISA_HAS_LEGACY_NAN(ISA) \
457 ((ISA) == ISA_MIPS1 \
458 || (ISA) == ISA_MIPS2 \
459 || (ISA) == ISA_MIPS3 \
460 || (ISA) == ISA_MIPS4 \
461 || (ISA) == ISA_MIPS5 \
462 || (ISA) == ISA_MIPS32 \
463 || (ISA) == ISA_MIPS32R2 \
464 || (ISA) == ISA_MIPS32R3 \
465 || (ISA) == ISA_MIPS32R5 \
466 || (ISA) == ISA_MIPS64 \
ae52f483
AB
467 || (ISA) == ISA_MIPS64R2 \
468 || (ISA) == ISA_MIPS64R3 \
469 || (ISA) == ISA_MIPS64R5)
ad3fea08 470
bad1aba3 471#define GPR_SIZE \
472 (mips_opts.gp == 64 && !ISA_HAS_64BIT_REGS (mips_opts.isa) \
473 ? 32 \
474 : mips_opts.gp)
ca4e0257 475
bad1aba3 476#define FPR_SIZE \
477 (mips_opts.fp == 64 && !ISA_HAS_64BIT_FPRS (mips_opts.isa) \
478 ? 32 \
479 : mips_opts.fp)
ca4e0257 480
316f5878 481#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
e013f690 482
316f5878 483#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
e013f690 484
3b91255e
RS
485/* True if relocations are stored in-place. */
486#define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
487
aed1a261
RS
488/* The ABI-derived address size. */
489#define HAVE_64BIT_ADDRESSES \
bad1aba3 490 (GPR_SIZE == 64 && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
aed1a261 491#define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
e013f690 492
aed1a261
RS
493/* The size of symbolic constants (i.e., expressions of the form
494 "SYMBOL" or "SYMBOL + OFFSET"). */
495#define HAVE_32BIT_SYMBOLS \
496 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
497#define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
ca4e0257 498
b7c7d6c1
TS
499/* Addresses are loaded in different ways, depending on the address size
500 in use. The n32 ABI Documentation also mandates the use of additions
501 with overflow checking, but existing implementations don't follow it. */
f899b4b8 502#define ADDRESS_ADD_INSN \
b7c7d6c1 503 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
f899b4b8
TS
504
505#define ADDRESS_ADDI_INSN \
b7c7d6c1 506 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
f899b4b8
TS
507
508#define ADDRESS_LOAD_INSN \
509 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
510
511#define ADDRESS_STORE_INSN \
512 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
513
a4672219 514/* Return true if the given CPU supports the MIPS16 ASE. */
3396de36
TS
515#define CPU_HAS_MIPS16(cpu) \
516 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
517 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
a4672219 518
2309ddf2 519/* Return true if the given CPU supports the microMIPS ASE. */
df58fc94
RS
520#define CPU_HAS_MICROMIPS(cpu) 0
521
60b63b72
RS
522/* True if CPU has a dror instruction. */
523#define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
524
525/* True if CPU has a ror instruction. */
526#define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
527
6f2117ba 528/* True if CPU is in the Octeon family. */
2c629856
N
529#define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP \
530 || (CPU) == CPU_OCTEON2 || (CPU) == CPU_OCTEON3)
dd6a37e7 531
dd3cbb7e 532/* True if CPU has seq/sne and seqi/snei instructions. */
dd6a37e7 533#define CPU_HAS_SEQ(CPU) (CPU_IS_OCTEON (CPU))
dd3cbb7e 534
0aa27725
RS
535/* True, if CPU has support for ldc1 and sdc1. */
536#define CPU_HAS_LDC1_SDC1(CPU) \
537 ((mips_opts.isa != ISA_MIPS1) && ((CPU) != CPU_R5900))
538
c8978940
CD
539/* True if mflo and mfhi can be immediately followed by instructions
540 which write to the HI and LO registers.
541
542 According to MIPS specifications, MIPS ISAs I, II, and III need
543 (at least) two instructions between the reads of HI/LO and
544 instructions which write them, and later ISAs do not. Contradicting
545 the MIPS specifications, some MIPS IV processor user manuals (e.g.
546 the UM for the NEC Vr5000) document needing the instructions between
547 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
548 MIPS64 and later ISAs to have the interlocks, plus any specific
549 earlier-ISA CPUs for which CPU documentation declares that the
550 instructions are really interlocked. */
551#define hilo_interlocks \
552 (mips_opts.isa == ISA_MIPS32 \
553 || mips_opts.isa == ISA_MIPS32R2 \
ae52f483
AB
554 || mips_opts.isa == ISA_MIPS32R3 \
555 || mips_opts.isa == ISA_MIPS32R5 \
7361da2c 556 || mips_opts.isa == ISA_MIPS32R6 \
c8978940
CD
557 || mips_opts.isa == ISA_MIPS64 \
558 || mips_opts.isa == ISA_MIPS64R2 \
ae52f483
AB
559 || mips_opts.isa == ISA_MIPS64R3 \
560 || mips_opts.isa == ISA_MIPS64R5 \
7361da2c 561 || mips_opts.isa == ISA_MIPS64R6 \
c8978940 562 || mips_opts.arch == CPU_R4010 \
e407c74b 563 || mips_opts.arch == CPU_R5900 \
c8978940
CD
564 || mips_opts.arch == CPU_R10000 \
565 || mips_opts.arch == CPU_R12000 \
3aa3176b
TS
566 || mips_opts.arch == CPU_R14000 \
567 || mips_opts.arch == CPU_R16000 \
c8978940 568 || mips_opts.arch == CPU_RM7000 \
c8978940 569 || mips_opts.arch == CPU_VR5500 \
df58fc94 570 || mips_opts.micromips \
c8978940 571 )
252b5132
RH
572
573/* Whether the processor uses hardware interlocks to protect reads
81912461
ILT
574 from the GPRs after they are loaded from memory, and thus does not
575 require nops to be inserted. This applies to instructions marked
67dc82bc 576 INSN_LOAD_MEMORY. These nops are only required at MIPS ISA
df58fc94
RS
577 level I and microMIPS mode instructions are always interlocked. */
578#define gpr_interlocks \
579 (mips_opts.isa != ISA_MIPS1 \
580 || mips_opts.arch == CPU_R3900 \
e407c74b 581 || mips_opts.arch == CPU_R5900 \
df58fc94
RS
582 || mips_opts.micromips \
583 )
252b5132 584
81912461
ILT
585/* Whether the processor uses hardware interlocks to avoid delays
586 required by coprocessor instructions, and thus does not require
587 nops to be inserted. This applies to instructions marked
43885403
MF
588 INSN_LOAD_COPROC, INSN_COPROC_MOVE, and to delays between
589 instructions marked INSN_WRITE_COND_CODE and ones marked
81912461 590 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
df58fc94
RS
591 levels I, II, and III and microMIPS mode instructions are always
592 interlocked. */
bdaaa2e1 593/* Itbl support may require additional care here. */
81912461
ILT
594#define cop_interlocks \
595 ((mips_opts.isa != ISA_MIPS1 \
596 && mips_opts.isa != ISA_MIPS2 \
597 && mips_opts.isa != ISA_MIPS3) \
598 || mips_opts.arch == CPU_R4300 \
df58fc94 599 || mips_opts.micromips \
81912461
ILT
600 )
601
602/* Whether the processor uses hardware interlocks to protect reads
603 from coprocessor registers after they are loaded from memory, and
604 thus does not require nops to be inserted. This applies to
605 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
df58fc94
RS
606 requires at MIPS ISA level I and microMIPS mode instructions are
607 always interlocked. */
608#define cop_mem_interlocks \
609 (mips_opts.isa != ISA_MIPS1 \
610 || mips_opts.micromips \
611 )
252b5132 612
6b76fefe
CM
613/* Is this a mfhi or mflo instruction? */
614#define MF_HILO_INSN(PINFO) \
b19e8a9b
AN
615 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
616
df58fc94
RS
617/* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
618 has been selected. This implies, in particular, that addresses of text
619 labels have their LSB set. */
620#define HAVE_CODE_COMPRESSION \
621 ((mips_opts.mips16 | mips_opts.micromips) != 0)
622
42429eac 623/* The minimum and maximum signed values that can be stored in a GPR. */
bad1aba3 624#define GPR_SMAX ((offsetT) (((valueT) 1 << (GPR_SIZE - 1)) - 1))
42429eac
RS
625#define GPR_SMIN (-GPR_SMAX - 1)
626
252b5132
RH
627/* MIPS PIC level. */
628
a161fe53 629enum mips_pic_level mips_pic;
252b5132 630
c9914766 631/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 632 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 633static int mips_big_got = 0;
252b5132
RH
634
635/* 1 if trap instructions should used for overflow rather than break
636 instructions. */
c9914766 637static int mips_trap = 0;
252b5132 638
119d663a 639/* 1 if double width floating point constants should not be constructed
b6ff326e 640 by assembling two single width halves into two single width floating
119d663a
NC
641 point registers which just happen to alias the double width destination
642 register. On some architectures this aliasing can be disabled by a bit
d547a75e 643 in the status register, and the setting of this bit cannot be determined
119d663a
NC
644 automatically at assemble time. */
645static int mips_disable_float_construction;
646
252b5132
RH
647/* Non-zero if any .set noreorder directives were used. */
648
649static int mips_any_noreorder;
650
6b76fefe
CM
651/* Non-zero if nops should be inserted when the register referenced in
652 an mfhi/mflo instruction is read in the next two instructions. */
653static int mips_7000_hilo_fix;
654
02ffd3e4 655/* The size of objects in the small data section. */
156c2f8b 656static unsigned int g_switch_value = 8;
252b5132
RH
657/* Whether the -G option was used. */
658static int g_switch_seen = 0;
659
660#define N_RMASK 0xc4
661#define N_VFP 0xd4
662
663/* If we can determine in advance that GP optimization won't be
664 possible, we can skip the relaxation stuff that tries to produce
665 GP-relative references. This makes delay slot optimization work
666 better.
667
668 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
669 gcc output. It needs to guess right for gcc, otherwise gcc
670 will put what it thinks is a GP-relative instruction in a branch
671 delay slot.
252b5132
RH
672
673 I don't know if a fix is needed for the SVR4_PIC mode. I've only
674 fixed it for the non-PIC mode. KR 95/04/07 */
17a2f251 675static int nopic_need_relax (symbolS *, int);
252b5132 676
6f2117ba 677/* Handle of the OPCODE hash table. */
252b5132
RH
678static struct hash_control *op_hash = NULL;
679
680/* The opcode hash table we use for the mips16. */
681static struct hash_control *mips16_op_hash = NULL;
682
df58fc94
RS
683/* The opcode hash table we use for the microMIPS ASE. */
684static struct hash_control *micromips_op_hash = NULL;
685
252b5132 686/* This array holds the chars that always start a comment. If the
6f2117ba 687 pre-processor is disabled, these aren't very useful. */
252b5132
RH
688const char comment_chars[] = "#";
689
690/* This array holds the chars that only start a comment at the beginning of
691 a line. If the line seems to have the form '# 123 filename'
6f2117ba 692 .line and .file directives will appear in the pre-processed output. */
252b5132
RH
693/* Note that input_file.c hand checks for '#' at the beginning of the
694 first line of the input file. This is because the compiler outputs
bdaaa2e1 695 #NO_APP at the beginning of its output. */
252b5132
RH
696/* Also note that C style comments are always supported. */
697const char line_comment_chars[] = "#";
698
bdaaa2e1 699/* This array holds machine specific line separator characters. */
63a0b638 700const char line_separator_chars[] = ";";
252b5132 701
6f2117ba 702/* Chars that can be used to separate mant from exp in floating point nums. */
252b5132
RH
703const char EXP_CHARS[] = "eE";
704
6f2117ba
PH
705/* Chars that mean this number is a floating point constant.
706 As in 0f12.456
707 or 0d1.2345e12. */
252b5132
RH
708const char FLT_CHARS[] = "rRsSfFdDxXpP";
709
710/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
711 changed in read.c . Ideally it shouldn't have to know about it at all,
6f2117ba 712 but nothing is ideal around here. */
252b5132 713
e3de51ce 714/* Types of printf format used for instruction-related error messages.
6f2117ba
PH
715 "I" means int ("%d") and "S" means string ("%s"). */
716enum mips_insn_error_format
717{
e3de51ce
RS
718 ERR_FMT_PLAIN,
719 ERR_FMT_I,
720 ERR_FMT_SS,
721};
722
723/* Information about an error that was found while assembling the current
724 instruction. */
6f2117ba
PH
725struct mips_insn_error
726{
e3de51ce
RS
727 /* We sometimes need to match an instruction against more than one
728 opcode table entry. Errors found during this matching are reported
729 against a particular syntactic argument rather than against the
730 instruction as a whole. We grade these messages so that errors
731 against argument N have a greater priority than an error against
732 any argument < N, since the former implies that arguments up to N
733 were acceptable and that the opcode entry was therefore a closer match.
734 If several matches report an error against the same argument,
735 we only use that error if it is the same in all cases.
736
737 min_argnum is the minimum argument number for which an error message
738 should be accepted. It is 0 if MSG is against the instruction as
739 a whole. */
740 int min_argnum;
741
742 /* The printf()-style message, including its format and arguments. */
743 enum mips_insn_error_format format;
744 const char *msg;
6f2117ba
PH
745 union
746 {
e3de51ce
RS
747 int i;
748 const char *ss[2];
749 } u;
750};
751
752/* The error that should be reported for the current instruction. */
753static struct mips_insn_error insn_error;
252b5132
RH
754
755static int auto_align = 1;
756
757/* When outputting SVR4 PIC code, the assembler needs to know the
758 offset in the stack frame from which to restore the $gp register.
759 This is set by the .cprestore pseudo-op, and saved in this
760 variable. */
761static offsetT mips_cprestore_offset = -1;
762
67c1ffbe 763/* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
6478892d 764 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 765 offset and even an other register than $gp as global pointer. */
6478892d
TS
766static offsetT mips_cpreturn_offset = -1;
767static int mips_cpreturn_register = -1;
768static int mips_gp_register = GP;
def2e0dd 769static int mips_gprel_offset = 0;
6478892d 770
7a621144
DJ
771/* Whether mips_cprestore_offset has been set in the current function
772 (or whether it has already been warned about, if not). */
773static int mips_cprestore_valid = 0;
774
252b5132
RH
775/* This is the register which holds the stack frame, as set by the
776 .frame pseudo-op. This is needed to implement .cprestore. */
777static int mips_frame_reg = SP;
778
7a621144
DJ
779/* Whether mips_frame_reg has been set in the current function
780 (or whether it has already been warned about, if not). */
781static int mips_frame_reg_valid = 0;
782
252b5132
RH
783/* To output NOP instructions correctly, we need to keep information
784 about the previous two instructions. */
785
786/* Whether we are optimizing. The default value of 2 means to remove
787 unneeded NOPs and swap branch instructions when possible. A value
788 of 1 means to not swap branches. A value of 0 means to always
789 insert NOPs. */
790static int mips_optimize = 2;
791
792/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
793 equivalent to seeing no -g option at all. */
794static int mips_debug = 0;
795
7d8e00cf
RS
796/* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
797#define MAX_VR4130_NOPS 4
798
799/* The maximum number of NOPs needed to fill delay slots. */
800#define MAX_DELAY_NOPS 2
801
802/* The maximum number of NOPs needed for any purpose. */
803#define MAX_NOPS 4
71400594 804
6f2117ba
PH
805/* The maximum range of context length of ll/sc. */
806#define MAX_LLSC_RANGE 20
807
71400594
RS
808/* A list of previous instructions, with index 0 being the most recent.
809 We need to look back MAX_NOPS instructions when filling delay slots
810 or working around processor errata. We need to look back one
811 instruction further if we're thinking about using history[0] to
812 fill a branch delay slot. */
6f2117ba 813static struct mips_cl_insn history[1 + MAX_NOPS + MAX_LLSC_RANGE];
252b5132 814
fc76e730 815/* Arrays of operands for each instruction. */
14daeee3 816#define MAX_OPERANDS 6
6f2117ba
PH
817struct mips_operand_array
818{
fc76e730
RS
819 const struct mips_operand *operand[MAX_OPERANDS];
820};
821static struct mips_operand_array *mips_operands;
822static struct mips_operand_array *mips16_operands;
823static struct mips_operand_array *micromips_operands;
824
1e915849 825/* Nop instructions used by emit_nop. */
df58fc94
RS
826static struct mips_cl_insn nop_insn;
827static struct mips_cl_insn mips16_nop_insn;
828static struct mips_cl_insn micromips_nop16_insn;
829static struct mips_cl_insn micromips_nop32_insn;
1e915849 830
6f2117ba
PH
831/* Sync instructions used by insert sync. */
832static struct mips_cl_insn sync_insn;
833
1e915849 834/* The appropriate nop for the current mode. */
833794fc
MR
835#define NOP_INSN (mips_opts.mips16 \
836 ? &mips16_nop_insn \
837 : (mips_opts.micromips \
838 ? (mips_opts.insn32 \
839 ? &micromips_nop32_insn \
840 : &micromips_nop16_insn) \
841 : &nop_insn))
df58fc94
RS
842
843/* The size of NOP_INSN in bytes. */
833794fc
MR
844#define NOP_INSN_SIZE ((mips_opts.mips16 \
845 || (mips_opts.micromips && !mips_opts.insn32)) \
846 ? 2 : 4)
252b5132 847
252b5132
RH
848/* If this is set, it points to a frag holding nop instructions which
849 were inserted before the start of a noreorder section. If those
850 nops turn out to be unnecessary, the size of the frag can be
851 decreased. */
852static fragS *prev_nop_frag;
853
854/* The number of nop instructions we created in prev_nop_frag. */
855static int prev_nop_frag_holds;
856
857/* The number of nop instructions that we know we need in
bdaaa2e1 858 prev_nop_frag. */
252b5132
RH
859static int prev_nop_frag_required;
860
861/* The number of instructions we've seen since prev_nop_frag. */
862static int prev_nop_frag_since;
863
e8044f35
RS
864/* Relocations against symbols are sometimes done in two parts, with a HI
865 relocation and a LO relocation. Each relocation has only 16 bits of
866 space to store an addend. This means that in order for the linker to
867 handle carries correctly, it must be able to locate both the HI and
868 the LO relocation. This means that the relocations must appear in
869 order in the relocation table.
252b5132
RH
870
871 In order to implement this, we keep track of each unmatched HI
872 relocation. We then sort them so that they immediately precede the
bdaaa2e1 873 corresponding LO relocation. */
252b5132 874
e972090a
NC
875struct mips_hi_fixup
876{
252b5132
RH
877 /* Next HI fixup. */
878 struct mips_hi_fixup *next;
879 /* This fixup. */
880 fixS *fixp;
881 /* The section this fixup is in. */
882 segT seg;
883};
884
885/* The list of unmatched HI relocs. */
886
887static struct mips_hi_fixup *mips_hi_fixup_list;
888
64bdfcaf
RS
889/* The frag containing the last explicit relocation operator.
890 Null if explicit relocations have not been used. */
891
892static fragS *prev_reloc_op_frag;
893
252b5132
RH
894/* Map mips16 register numbers to normal MIPS register numbers. */
895
e972090a
NC
896static const unsigned int mips16_to_32_reg_map[] =
897{
252b5132
RH
898 16, 17, 2, 3, 4, 5, 6, 7
899};
60b63b72 900
df58fc94
RS
901/* Map microMIPS register numbers to normal MIPS register numbers. */
902
df58fc94 903#define micromips_to_32_reg_d_map mips16_to_32_reg_map
df58fc94
RS
904
905/* The microMIPS registers with type h. */
e76ff5ab 906static const unsigned int micromips_to_32_reg_h_map1[] =
df58fc94
RS
907{
908 5, 5, 6, 4, 4, 4, 4, 4
909};
e76ff5ab 910static const unsigned int micromips_to_32_reg_h_map2[] =
df58fc94
RS
911{
912 6, 7, 7, 21, 22, 5, 6, 7
913};
914
df58fc94
RS
915/* The microMIPS registers with type m. */
916static const unsigned int micromips_to_32_reg_m_map[] =
917{
918 0, 17, 2, 3, 16, 18, 19, 20
919};
920
921#define micromips_to_32_reg_n_map micromips_to_32_reg_m_map
922
71400594
RS
923/* Classifies the kind of instructions we're interested in when
924 implementing -mfix-vr4120. */
c67a084a
NC
925enum fix_vr4120_class
926{
71400594
RS
927 FIX_VR4120_MACC,
928 FIX_VR4120_DMACC,
929 FIX_VR4120_MULT,
930 FIX_VR4120_DMULT,
931 FIX_VR4120_DIV,
932 FIX_VR4120_MTHILO,
933 NUM_FIX_VR4120_CLASSES
934};
935
c67a084a
NC
936/* ...likewise -mfix-loongson2f-jump. */
937static bfd_boolean mips_fix_loongson2f_jump;
938
939/* ...likewise -mfix-loongson2f-nop. */
940static bfd_boolean mips_fix_loongson2f_nop;
941
942/* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed. */
943static bfd_boolean mips_fix_loongson2f;
944
71400594
RS
945/* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
946 there must be at least one other instruction between an instruction
947 of type X and an instruction of type Y. */
948static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
949
950/* True if -mfix-vr4120 is in force. */
d766e8ec 951static int mips_fix_vr4120;
4a6a3df4 952
7d8e00cf
RS
953/* ...likewise -mfix-vr4130. */
954static int mips_fix_vr4130;
955
6a32d874
CM
956/* ...likewise -mfix-24k. */
957static int mips_fix_24k;
958
a8d14a88
CM
959/* ...likewise -mfix-rm7000 */
960static int mips_fix_rm7000;
961
d954098f
DD
962/* ...likewise -mfix-cn63xxp1 */
963static bfd_boolean mips_fix_cn63xxp1;
964
27c634e0
FN
965/* ...likewise -mfix-r5900 */
966static bfd_boolean mips_fix_r5900;
967static bfd_boolean mips_fix_r5900_explicit;
968
6f2117ba
PH
969/* ...likewise -mfix-loongson3-llsc. */
970static bfd_boolean mips_fix_loongson3_llsc = DEFAULT_MIPS_FIX_LOONGSON3_LLSC;
971
4a6a3df4
AO
972/* We don't relax branches by default, since this causes us to expand
973 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
974 fail to compute the offset before expanding the macro to the most
975 efficient expansion. */
976
977static int mips_relax_branch;
8b10b0b3
MR
978
979/* TRUE if checks are suppressed for invalid branches between ISA modes.
980 Needed for broken assembly produced by some GCC versions and some
981 sloppy code out there, where branches to data labels are present. */
982static bfd_boolean mips_ignore_branch_isa;
252b5132 983\f
4d7206a2
RS
984/* The expansion of many macros depends on the type of symbol that
985 they refer to. For example, when generating position-dependent code,
986 a macro that refers to a symbol may have two different expansions,
987 one which uses GP-relative addresses and one which uses absolute
988 addresses. When generating SVR4-style PIC, a macro may have
989 different expansions for local and global symbols.
990
991 We handle these situations by generating both sequences and putting
992 them in variant frags. In position-dependent code, the first sequence
993 will be the GP-relative one and the second sequence will be the
994 absolute one. In SVR4 PIC, the first sequence will be for global
995 symbols and the second will be for local symbols.
996
584892a6
RS
997 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
998 SECOND are the lengths of the two sequences in bytes. These fields
999 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
1000 the subtype has the following flags:
4d7206a2 1001
ce8ad872
MR
1002 RELAX_PIC
1003 Set if generating PIC code.
1004
584892a6
RS
1005 RELAX_USE_SECOND
1006 Set if it has been decided that we should use the second
1007 sequence instead of the first.
1008
1009 RELAX_SECOND_LONGER
1010 Set in the first variant frag if the macro's second implementation
1011 is longer than its first. This refers to the macro as a whole,
1012 not an individual relaxation.
1013
1014 RELAX_NOMACRO
1015 Set in the first variant frag if the macro appeared in a .set nomacro
1016 block and if one alternative requires a warning but the other does not.
1017
1018 RELAX_DELAY_SLOT
1019 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
1020 delay slot.
4d7206a2 1021
df58fc94
RS
1022 RELAX_DELAY_SLOT_16BIT
1023 Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
1024 16-bit instruction.
1025
1026 RELAX_DELAY_SLOT_SIZE_FIRST
1027 Like RELAX_DELAY_SLOT, but indicates that the first implementation of
1028 the macro is of the wrong size for the branch delay slot.
1029
1030 RELAX_DELAY_SLOT_SIZE_SECOND
1031 Like RELAX_DELAY_SLOT, but indicates that the second implementation of
1032 the macro is of the wrong size for the branch delay slot.
1033
4d7206a2
RS
1034 The frag's "opcode" points to the first fixup for relaxable code.
1035
1036 Relaxable macros are generated using a sequence such as:
1037
1038 relax_start (SYMBOL);
1039 ... generate first expansion ...
1040 relax_switch ();
1041 ... generate second expansion ...
1042 relax_end ();
1043
1044 The code and fixups for the unwanted alternative are discarded
1045 by md_convert_frag. */
ce8ad872
MR
1046#define RELAX_ENCODE(FIRST, SECOND, PIC) \
1047 (((FIRST) << 8) | (SECOND) | ((PIC) ? 0x10000 : 0))
4d7206a2 1048
584892a6
RS
1049#define RELAX_FIRST(X) (((X) >> 8) & 0xff)
1050#define RELAX_SECOND(X) ((X) & 0xff)
ce8ad872
MR
1051#define RELAX_PIC(X) (((X) & 0x10000) != 0)
1052#define RELAX_USE_SECOND 0x20000
1053#define RELAX_SECOND_LONGER 0x40000
1054#define RELAX_NOMACRO 0x80000
1055#define RELAX_DELAY_SLOT 0x100000
1056#define RELAX_DELAY_SLOT_16BIT 0x200000
1057#define RELAX_DELAY_SLOT_SIZE_FIRST 0x400000
1058#define RELAX_DELAY_SLOT_SIZE_SECOND 0x800000
252b5132 1059
4a6a3df4
AO
1060/* Branch without likely bit. If label is out of range, we turn:
1061
134c0c8b 1062 beq reg1, reg2, label
4a6a3df4
AO
1063 delay slot
1064
1065 into
1066
1067 bne reg1, reg2, 0f
1068 nop
1069 j label
1070 0: delay slot
1071
1072 with the following opcode replacements:
1073
1074 beq <-> bne
1075 blez <-> bgtz
1076 bltz <-> bgez
1077 bc1f <-> bc1t
1078
1079 bltzal <-> bgezal (with jal label instead of j label)
1080
1081 Even though keeping the delay slot instruction in the delay slot of
1082 the branch would be more efficient, it would be very tricky to do
1083 correctly, because we'd have to introduce a variable frag *after*
1084 the delay slot instruction, and expand that instead. Let's do it
1085 the easy way for now, even if the branch-not-taken case now costs
1086 one additional instruction. Out-of-range branches are not supposed
1087 to be common, anyway.
1088
1089 Branch likely. If label is out of range, we turn:
1090
1091 beql reg1, reg2, label
1092 delay slot (annulled if branch not taken)
1093
1094 into
1095
1096 beql reg1, reg2, 1f
1097 nop
1098 beql $0, $0, 2f
1099 nop
1100 1: j[al] label
1101 delay slot (executed only if branch taken)
1102 2:
1103
1104 It would be possible to generate a shorter sequence by losing the
1105 likely bit, generating something like:
b34976b6 1106
4a6a3df4
AO
1107 bne reg1, reg2, 0f
1108 nop
1109 j[al] label
1110 delay slot (executed only if branch taken)
1111 0:
1112
1113 beql -> bne
1114 bnel -> beq
1115 blezl -> bgtz
1116 bgtzl -> blez
1117 bltzl -> bgez
1118 bgezl -> bltz
1119 bc1fl -> bc1t
1120 bc1tl -> bc1f
1121
1122 bltzall -> bgezal (with jal label instead of j label)
1123 bgezall -> bltzal (ditto)
1124
1125
1126 but it's not clear that it would actually improve performance. */
ce8ad872
MR
1127#define RELAX_BRANCH_ENCODE(at, pic, \
1128 uncond, likely, link, toofar) \
66b3e8da
MR
1129 ((relax_substateT) \
1130 (0xc0000000 \
1131 | ((at) & 0x1f) \
ce8ad872
MR
1132 | ((pic) ? 0x20 : 0) \
1133 | ((toofar) ? 0x40 : 0) \
1134 | ((link) ? 0x80 : 0) \
1135 | ((likely) ? 0x100 : 0) \
1136 | ((uncond) ? 0x200 : 0)))
4a6a3df4 1137#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
ce8ad872
MR
1138#define RELAX_BRANCH_UNCOND(i) (((i) & 0x200) != 0)
1139#define RELAX_BRANCH_LIKELY(i) (((i) & 0x100) != 0)
1140#define RELAX_BRANCH_LINK(i) (((i) & 0x80) != 0)
1141#define RELAX_BRANCH_TOOFAR(i) (((i) & 0x40) != 0)
1142#define RELAX_BRANCH_PIC(i) (((i) & 0x20) != 0)
66b3e8da 1143#define RELAX_BRANCH_AT(i) ((i) & 0x1f)
4a6a3df4 1144
252b5132
RH
1145/* For mips16 code, we use an entirely different form of relaxation.
1146 mips16 supports two versions of most instructions which take
1147 immediate values: a small one which takes some small value, and a
1148 larger one which takes a 16 bit value. Since branches also follow
1149 this pattern, relaxing these values is required.
1150
1151 We can assemble both mips16 and normal MIPS code in a single
1152 object. Therefore, we need to support this type of relaxation at
1153 the same time that we support the relaxation described above. We
1154 use the high bit of the subtype field to distinguish these cases.
1155
1156 The information we store for this type of relaxation is the
1157 argument code found in the opcode file for this relocation, whether
1158 the user explicitly requested a small or extended form, and whether
1159 the relocation is in a jump or jal delay slot. That tells us the
1160 size of the value, and how it should be stored. We also store
1161 whether the fragment is considered to be extended or not. We also
1162 store whether this is known to be a branch to a different section,
1163 whether we have tried to relax this frag yet, and whether we have
1164 ever extended a PC relative fragment because of a shift count. */
25499ac7 1165#define RELAX_MIPS16_ENCODE(type, e2, pic, sym32, nomacro, \
8507b6e7
MR
1166 small, ext, \
1167 dslot, jal_dslot) \
252b5132
RH
1168 (0x80000000 \
1169 | ((type) & 0xff) \
25499ac7
MR
1170 | ((e2) ? 0x100 : 0) \
1171 | ((pic) ? 0x200 : 0) \
1172 | ((sym32) ? 0x400 : 0) \
1173 | ((nomacro) ? 0x800 : 0) \
1174 | ((small) ? 0x1000 : 0) \
1175 | ((ext) ? 0x2000 : 0) \
1176 | ((dslot) ? 0x4000 : 0) \
1177 | ((jal_dslot) ? 0x8000 : 0))
8507b6e7 1178
4a6a3df4 1179#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132 1180#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
25499ac7
MR
1181#define RELAX_MIPS16_E2(i) (((i) & 0x100) != 0)
1182#define RELAX_MIPS16_PIC(i) (((i) & 0x200) != 0)
1183#define RELAX_MIPS16_SYM32(i) (((i) & 0x400) != 0)
1184#define RELAX_MIPS16_NOMACRO(i) (((i) & 0x800) != 0)
1185#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x1000) != 0)
1186#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x2000) != 0)
1187#define RELAX_MIPS16_DSLOT(i) (((i) & 0x4000) != 0)
1188#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x8000) != 0)
1189
1190#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x10000) != 0)
1191#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x10000)
1192#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) & ~0x10000)
1193#define RELAX_MIPS16_ALWAYS_EXTENDED(i) (((i) & 0x20000) != 0)
1194#define RELAX_MIPS16_MARK_ALWAYS_EXTENDED(i) ((i) | 0x20000)
1195#define RELAX_MIPS16_CLEAR_ALWAYS_EXTENDED(i) ((i) & ~0x20000)
1196#define RELAX_MIPS16_MACRO(i) (((i) & 0x40000) != 0)
1197#define RELAX_MIPS16_MARK_MACRO(i) ((i) | 0x40000)
1198#define RELAX_MIPS16_CLEAR_MACRO(i) ((i) & ~0x40000)
885add95 1199
df58fc94
RS
1200/* For microMIPS code, we use relaxation similar to one we use for
1201 MIPS16 code. Some instructions that take immediate values support
1202 two encodings: a small one which takes some small value, and a
1203 larger one which takes a 16 bit value. As some branches also follow
1204 this pattern, relaxing these values is required.
1205
1206 We can assemble both microMIPS and normal MIPS code in a single
1207 object. Therefore, we need to support this type of relaxation at
1208 the same time that we support the relaxation described above. We
1209 use one of the high bits of the subtype field to distinguish these
1210 cases.
1211
1212 The information we store for this type of relaxation is the argument
1213 code found in the opcode file for this relocation, the register
8484fb75
MR
1214 selected as the assembler temporary, whether in the 32-bit
1215 instruction mode, whether the branch is unconditional, whether it is
7bd374a4
MR
1216 compact, whether there is no delay-slot instruction available to fill
1217 in, whether it stores the link address implicitly in $ra, whether
1218 relaxation of out-of-range 32-bit branches to a sequence of
8484fb75
MR
1219 instructions is enabled, and whether the displacement of a branch is
1220 too large to fit as an immediate argument of a 16-bit and a 32-bit
1221 branch, respectively. */
ce8ad872 1222#define RELAX_MICROMIPS_ENCODE(type, at, insn32, pic, \
7bd374a4 1223 uncond, compact, link, nods, \
40209cad
MR
1224 relax32, toofar16, toofar32) \
1225 (0x40000000 \
1226 | ((type) & 0xff) \
1227 | (((at) & 0x1f) << 8) \
8484fb75 1228 | ((insn32) ? 0x2000 : 0) \
ce8ad872
MR
1229 | ((pic) ? 0x4000 : 0) \
1230 | ((uncond) ? 0x8000 : 0) \
1231 | ((compact) ? 0x10000 : 0) \
1232 | ((link) ? 0x20000 : 0) \
1233 | ((nods) ? 0x40000 : 0) \
1234 | ((relax32) ? 0x80000 : 0) \
1235 | ((toofar16) ? 0x100000 : 0) \
1236 | ((toofar32) ? 0x200000 : 0))
df58fc94
RS
1237#define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1238#define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1239#define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
8484fb75 1240#define RELAX_MICROMIPS_INSN32(i) (((i) & 0x2000) != 0)
ce8ad872
MR
1241#define RELAX_MICROMIPS_PIC(i) (((i) & 0x4000) != 0)
1242#define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x8000) != 0)
1243#define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x10000) != 0)
1244#define RELAX_MICROMIPS_LINK(i) (((i) & 0x20000) != 0)
1245#define RELAX_MICROMIPS_NODS(i) (((i) & 0x40000) != 0)
1246#define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x80000) != 0)
1247
1248#define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x100000) != 0)
1249#define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x100000)
1250#define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x100000)
1251#define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x200000) != 0)
1252#define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x200000)
1253#define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x200000)
df58fc94 1254
43c0598f
RS
1255/* Sign-extend 16-bit value X. */
1256#define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1257
885add95
CD
1258/* Is the given value a sign-extended 32-bit value? */
1259#define IS_SEXT_32BIT_NUM(x) \
1260 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1261 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1262
1263/* Is the given value a sign-extended 16-bit value? */
1264#define IS_SEXT_16BIT_NUM(x) \
1265 (((x) &~ (offsetT) 0x7fff) == 0 \
1266 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1267
df58fc94
RS
1268/* Is the given value a sign-extended 12-bit value? */
1269#define IS_SEXT_12BIT_NUM(x) \
1270 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1271
7f3c4072
CM
1272/* Is the given value a sign-extended 9-bit value? */
1273#define IS_SEXT_9BIT_NUM(x) \
1274 (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1275
2051e8c4
MR
1276/* Is the given value a zero-extended 32-bit value? Or a negated one? */
1277#define IS_ZEXT_32BIT_NUM(x) \
1278 (((x) &~ (offsetT) 0xffffffff) == 0 \
1279 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1280
bf12938e
RS
1281/* Extract bits MASK << SHIFT from STRUCT and shift them right
1282 SHIFT places. */
1283#define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1284 (((STRUCT) >> (SHIFT)) & (MASK))
1285
bf12938e 1286/* Extract the operand given by FIELD from mips_cl_insn INSN. */
df58fc94
RS
1287#define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1288 (!(MICROMIPS) \
1289 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1290 : EXTRACT_BITS ((INSN).insn_opcode, \
1291 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
bf12938e
RS
1292#define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1293 EXTRACT_BITS ((INSN).insn_opcode, \
1294 MIPS16OP_MASK_##FIELD, \
1295 MIPS16OP_SH_##FIELD)
5c04167a
RS
1296
1297/* The MIPS16 EXTEND opcode, shifted left 16 places. */
1298#define MIPS16_EXTEND (0xf000U << 16)
4d7206a2 1299\f
df58fc94
RS
1300/* Whether or not we are emitting a branch-likely macro. */
1301static bfd_boolean emit_branch_likely_macro = FALSE;
1302
4d7206a2
RS
1303/* Global variables used when generating relaxable macros. See the
1304 comment above RELAX_ENCODE for more details about how relaxation
1305 is used. */
1306static struct {
1307 /* 0 if we're not emitting a relaxable macro.
1308 1 if we're emitting the first of the two relaxation alternatives.
1309 2 if we're emitting the second alternative. */
1310 int sequence;
1311
1312 /* The first relaxable fixup in the current frag. (In other words,
1313 the first fixup that refers to relaxable code.) */
1314 fixS *first_fixup;
1315
1316 /* sizes[0] says how many bytes of the first alternative are stored in
1317 the current frag. Likewise sizes[1] for the second alternative. */
1318 unsigned int sizes[2];
1319
1320 /* The symbol on which the choice of sequence depends. */
1321 symbolS *symbol;
1322} mips_relax;
252b5132 1323\f
584892a6
RS
1324/* Global variables used to decide whether a macro needs a warning. */
1325static struct {
1326 /* True if the macro is in a branch delay slot. */
1327 bfd_boolean delay_slot_p;
1328
df58fc94
RS
1329 /* Set to the length in bytes required if the macro is in a delay slot
1330 that requires a specific length of instruction, otherwise zero. */
1331 unsigned int delay_slot_length;
1332
584892a6
RS
1333 /* For relaxable macros, sizes[0] is the length of the first alternative
1334 in bytes and sizes[1] is the length of the second alternative.
1335 For non-relaxable macros, both elements give the length of the
1336 macro in bytes. */
1337 unsigned int sizes[2];
1338
df58fc94
RS
1339 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1340 instruction of the first alternative in bytes and first_insn_sizes[1]
1341 is the length of the first instruction of the second alternative.
1342 For non-relaxable macros, both elements give the length of the first
1343 instruction in bytes.
1344
1345 Set to zero if we haven't yet seen the first instruction. */
1346 unsigned int first_insn_sizes[2];
1347
1348 /* For relaxable macros, insns[0] is the number of instructions for the
1349 first alternative and insns[1] is the number of instructions for the
1350 second alternative.
1351
1352 For non-relaxable macros, both elements give the number of
1353 instructions for the macro. */
1354 unsigned int insns[2];
1355
584892a6
RS
1356 /* The first variant frag for this macro. */
1357 fragS *first_frag;
1358} mips_macro_warning;
1359\f
252b5132
RH
1360/* Prototypes for static functions. */
1361
252b5132
RH
1362enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1363
b34976b6 1364static void append_insn
df58fc94
RS
1365 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1366 bfd_boolean expansionp);
7d10b47d 1367static void mips_no_prev_insn (void);
c67a084a 1368static void macro_build (expressionS *, const char *, const char *, ...);
b34976b6 1369static void mips16_macro_build
03ea81db 1370 (expressionS *, const char *, const char *, va_list *);
67c0d1eb 1371static void load_register (int, expressionS *, int);
584892a6
RS
1372static void macro_start (void);
1373static void macro_end (void);
833794fc 1374static void macro (struct mips_cl_insn *ip, char *str);
17a2f251 1375static void mips16_macro (struct mips_cl_insn * ip);
17a2f251
TS
1376static void mips_ip (char *str, struct mips_cl_insn * ip);
1377static void mips16_ip (char *str, struct mips_cl_insn * ip);
25499ac7 1378static unsigned long mips16_immed_extend (offsetT, unsigned int);
b34976b6 1379static void mips16_immed
3b4dbbbf 1380 (const char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
43c0598f 1381 unsigned int, unsigned long *);
5e0116d5 1382static size_t my_getSmallExpression
17a2f251
TS
1383 (expressionS *, bfd_reloc_code_real_type *, char *);
1384static void my_getExpression (expressionS *, char *);
1385static void s_align (int);
1386static void s_change_sec (int);
1387static void s_change_section (int);
1388static void s_cons (int);
1389static void s_float_cons (int);
1390static void s_mips_globl (int);
1391static void s_option (int);
1392static void s_mipsset (int);
1393static void s_abicalls (int);
1394static void s_cpload (int);
1395static void s_cpsetup (int);
1396static void s_cplocal (int);
1397static void s_cprestore (int);
1398static void s_cpreturn (int);
741d6ea8
JM
1399static void s_dtprelword (int);
1400static void s_dtpreldword (int);
d0f13682
CLT
1401static void s_tprelword (int);
1402static void s_tpreldword (int);
17a2f251
TS
1403static void s_gpvalue (int);
1404static void s_gpword (int);
1405static void s_gpdword (int);
a3f278e2 1406static void s_ehword (int);
17a2f251
TS
1407static void s_cpadd (int);
1408static void s_insn (int);
ba92f887 1409static void s_nan (int);
919731af 1410static void s_module (int);
17a2f251
TS
1411static void s_mips_ent (int);
1412static void s_mips_end (int);
1413static void s_mips_frame (int);
1414static void s_mips_mask (int reg_type);
1415static void s_mips_stab (int);
1416static void s_mips_weakext (int);
1417static void s_mips_file (int);
1418static void s_mips_loc (int);
9e009953 1419static bfd_boolean pic_need_relax (symbolS *);
4a6a3df4 1420static int relaxed_branch_length (fragS *, asection *, int);
df58fc94
RS
1421static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1422static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
919731af 1423static void file_mips_check_options (void);
e7af610e
NC
1424
1425/* Table and functions used to map between CPU/ISA names, and
1426 ISA levels, and CPU numbers. */
1427
e972090a
NC
1428struct mips_cpu_info
1429{
e7af610e 1430 const char *name; /* CPU or ISA name. */
d16afab6
RS
1431 int flags; /* MIPS_CPU_* flags. */
1432 int ase; /* Set of ASEs implemented by the CPU. */
e7af610e
NC
1433 int isa; /* ISA level. */
1434 int cpu; /* CPU number (default CPU if ISA). */
1435};
1436
ad3fea08 1437#define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
ad3fea08 1438
17a2f251
TS
1439static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1440static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1441static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132 1442\f
c31f3936
RS
1443/* Command-line options. */
1444const char *md_shortopts = "O::g::G:";
1445
1446enum options
1447 {
1448 OPTION_MARCH = OPTION_MD_BASE,
1449 OPTION_MTUNE,
1450 OPTION_MIPS1,
1451 OPTION_MIPS2,
1452 OPTION_MIPS3,
1453 OPTION_MIPS4,
1454 OPTION_MIPS5,
1455 OPTION_MIPS32,
1456 OPTION_MIPS64,
1457 OPTION_MIPS32R2,
ae52f483
AB
1458 OPTION_MIPS32R3,
1459 OPTION_MIPS32R5,
7361da2c 1460 OPTION_MIPS32R6,
c31f3936 1461 OPTION_MIPS64R2,
ae52f483
AB
1462 OPTION_MIPS64R3,
1463 OPTION_MIPS64R5,
7361da2c 1464 OPTION_MIPS64R6,
c31f3936
RS
1465 OPTION_MIPS16,
1466 OPTION_NO_MIPS16,
1467 OPTION_MIPS3D,
1468 OPTION_NO_MIPS3D,
1469 OPTION_MDMX,
1470 OPTION_NO_MDMX,
1471 OPTION_DSP,
1472 OPTION_NO_DSP,
1473 OPTION_MT,
1474 OPTION_NO_MT,
1475 OPTION_VIRT,
1476 OPTION_NO_VIRT,
56d438b1
CF
1477 OPTION_MSA,
1478 OPTION_NO_MSA,
c31f3936
RS
1479 OPTION_SMARTMIPS,
1480 OPTION_NO_SMARTMIPS,
1481 OPTION_DSPR2,
1482 OPTION_NO_DSPR2,
8f4f9071
MF
1483 OPTION_DSPR3,
1484 OPTION_NO_DSPR3,
c31f3936
RS
1485 OPTION_EVA,
1486 OPTION_NO_EVA,
7d64c587
AB
1487 OPTION_XPA,
1488 OPTION_NO_XPA,
c31f3936
RS
1489 OPTION_MICROMIPS,
1490 OPTION_NO_MICROMIPS,
1491 OPTION_MCU,
1492 OPTION_NO_MCU,
25499ac7
MR
1493 OPTION_MIPS16E2,
1494 OPTION_NO_MIPS16E2,
730c3174
SE
1495 OPTION_CRC,
1496 OPTION_NO_CRC,
c31f3936
RS
1497 OPTION_M4650,
1498 OPTION_NO_M4650,
1499 OPTION_M4010,
1500 OPTION_NO_M4010,
1501 OPTION_M4100,
1502 OPTION_NO_M4100,
1503 OPTION_M3900,
1504 OPTION_NO_M3900,
1505 OPTION_M7000_HILO_FIX,
1506 OPTION_MNO_7000_HILO_FIX,
1507 OPTION_FIX_24K,
1508 OPTION_NO_FIX_24K,
a8d14a88
CM
1509 OPTION_FIX_RM7000,
1510 OPTION_NO_FIX_RM7000,
6f2117ba
PH
1511 OPTION_FIX_LOONGSON3_LLSC,
1512 OPTION_NO_FIX_LOONGSON3_LLSC,
c31f3936
RS
1513 OPTION_FIX_LOONGSON2F_JUMP,
1514 OPTION_NO_FIX_LOONGSON2F_JUMP,
1515 OPTION_FIX_LOONGSON2F_NOP,
1516 OPTION_NO_FIX_LOONGSON2F_NOP,
1517 OPTION_FIX_VR4120,
1518 OPTION_NO_FIX_VR4120,
1519 OPTION_FIX_VR4130,
1520 OPTION_NO_FIX_VR4130,
1521 OPTION_FIX_CN63XXP1,
1522 OPTION_NO_FIX_CN63XXP1,
27c634e0
FN
1523 OPTION_FIX_R5900,
1524 OPTION_NO_FIX_R5900,
c31f3936
RS
1525 OPTION_TRAP,
1526 OPTION_BREAK,
1527 OPTION_EB,
1528 OPTION_EL,
1529 OPTION_FP32,
1530 OPTION_GP32,
1531 OPTION_CONSTRUCT_FLOATS,
1532 OPTION_NO_CONSTRUCT_FLOATS,
1533 OPTION_FP64,
351cdf24 1534 OPTION_FPXX,
c31f3936
RS
1535 OPTION_GP64,
1536 OPTION_RELAX_BRANCH,
1537 OPTION_NO_RELAX_BRANCH,
8b10b0b3
MR
1538 OPTION_IGNORE_BRANCH_ISA,
1539 OPTION_NO_IGNORE_BRANCH_ISA,
833794fc
MR
1540 OPTION_INSN32,
1541 OPTION_NO_INSN32,
c31f3936
RS
1542 OPTION_MSHARED,
1543 OPTION_MNO_SHARED,
1544 OPTION_MSYM32,
1545 OPTION_MNO_SYM32,
1546 OPTION_SOFT_FLOAT,
1547 OPTION_HARD_FLOAT,
1548 OPTION_SINGLE_FLOAT,
1549 OPTION_DOUBLE_FLOAT,
1550 OPTION_32,
c31f3936
RS
1551 OPTION_CALL_SHARED,
1552 OPTION_CALL_NONPIC,
1553 OPTION_NON_SHARED,
1554 OPTION_XGOT,
1555 OPTION_MABI,
1556 OPTION_N32,
1557 OPTION_64,
1558 OPTION_MDEBUG,
1559 OPTION_NO_MDEBUG,
1560 OPTION_PDR,
1561 OPTION_NO_PDR,
1562 OPTION_MVXWORKS_PIC,
ba92f887 1563 OPTION_NAN,
351cdf24
MF
1564 OPTION_ODD_SPREG,
1565 OPTION_NO_ODD_SPREG,
6f20c942
FS
1566 OPTION_GINV,
1567 OPTION_NO_GINV,
8095d2f7
CX
1568 OPTION_LOONGSON_MMI,
1569 OPTION_NO_LOONGSON_MMI,
716c08de
CX
1570 OPTION_LOONGSON_CAM,
1571 OPTION_NO_LOONGSON_CAM,
bdc6c06e
CX
1572 OPTION_LOONGSON_EXT,
1573 OPTION_NO_LOONGSON_EXT,
a693765e
CX
1574 OPTION_LOONGSON_EXT2,
1575 OPTION_NO_LOONGSON_EXT2,
c31f3936
RS
1576 OPTION_END_OF_ENUM
1577 };
1578
1579struct option md_longopts[] =
1580{
1581 /* Options which specify architecture. */
1582 {"march", required_argument, NULL, OPTION_MARCH},
1583 {"mtune", required_argument, NULL, OPTION_MTUNE},
1584 {"mips0", no_argument, NULL, OPTION_MIPS1},
1585 {"mips1", no_argument, NULL, OPTION_MIPS1},
1586 {"mips2", no_argument, NULL, OPTION_MIPS2},
1587 {"mips3", no_argument, NULL, OPTION_MIPS3},
1588 {"mips4", no_argument, NULL, OPTION_MIPS4},
1589 {"mips5", no_argument, NULL, OPTION_MIPS5},
1590 {"mips32", no_argument, NULL, OPTION_MIPS32},
1591 {"mips64", no_argument, NULL, OPTION_MIPS64},
1592 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
ae52f483
AB
1593 {"mips32r3", no_argument, NULL, OPTION_MIPS32R3},
1594 {"mips32r5", no_argument, NULL, OPTION_MIPS32R5},
7361da2c 1595 {"mips32r6", no_argument, NULL, OPTION_MIPS32R6},
c31f3936 1596 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
ae52f483
AB
1597 {"mips64r3", no_argument, NULL, OPTION_MIPS64R3},
1598 {"mips64r5", no_argument, NULL, OPTION_MIPS64R5},
7361da2c 1599 {"mips64r6", no_argument, NULL, OPTION_MIPS64R6},
c31f3936
RS
1600
1601 /* Options which specify Application Specific Extensions (ASEs). */
1602 {"mips16", no_argument, NULL, OPTION_MIPS16},
1603 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1604 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1605 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1606 {"mdmx", no_argument, NULL, OPTION_MDMX},
1607 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1608 {"mdsp", no_argument, NULL, OPTION_DSP},
1609 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1610 {"mmt", no_argument, NULL, OPTION_MT},
1611 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1612 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1613 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1614 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1615 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
8f4f9071
MF
1616 {"mdspr3", no_argument, NULL, OPTION_DSPR3},
1617 {"mno-dspr3", no_argument, NULL, OPTION_NO_DSPR3},
c31f3936
RS
1618 {"meva", no_argument, NULL, OPTION_EVA},
1619 {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1620 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1621 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1622 {"mmcu", no_argument, NULL, OPTION_MCU},
1623 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1624 {"mvirt", no_argument, NULL, OPTION_VIRT},
1625 {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
56d438b1
CF
1626 {"mmsa", no_argument, NULL, OPTION_MSA},
1627 {"mno-msa", no_argument, NULL, OPTION_NO_MSA},
7d64c587
AB
1628 {"mxpa", no_argument, NULL, OPTION_XPA},
1629 {"mno-xpa", no_argument, NULL, OPTION_NO_XPA},
25499ac7
MR
1630 {"mmips16e2", no_argument, NULL, OPTION_MIPS16E2},
1631 {"mno-mips16e2", no_argument, NULL, OPTION_NO_MIPS16E2},
730c3174
SE
1632 {"mcrc", no_argument, NULL, OPTION_CRC},
1633 {"mno-crc", no_argument, NULL, OPTION_NO_CRC},
6f20c942
FS
1634 {"mginv", no_argument, NULL, OPTION_GINV},
1635 {"mno-ginv", no_argument, NULL, OPTION_NO_GINV},
8095d2f7
CX
1636 {"mloongson-mmi", no_argument, NULL, OPTION_LOONGSON_MMI},
1637 {"mno-loongson-mmi", no_argument, NULL, OPTION_NO_LOONGSON_MMI},
716c08de
CX
1638 {"mloongson-cam", no_argument, NULL, OPTION_LOONGSON_CAM},
1639 {"mno-loongson-cam", no_argument, NULL, OPTION_NO_LOONGSON_CAM},
bdc6c06e
CX
1640 {"mloongson-ext", no_argument, NULL, OPTION_LOONGSON_EXT},
1641 {"mno-loongson-ext", no_argument, NULL, OPTION_NO_LOONGSON_EXT},
a693765e
CX
1642 {"mloongson-ext2", no_argument, NULL, OPTION_LOONGSON_EXT2},
1643 {"mno-loongson-ext2", no_argument, NULL, OPTION_NO_LOONGSON_EXT2},
c31f3936
RS
1644
1645 /* Old-style architecture options. Don't add more of these. */
1646 {"m4650", no_argument, NULL, OPTION_M4650},
1647 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1648 {"m4010", no_argument, NULL, OPTION_M4010},
1649 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1650 {"m4100", no_argument, NULL, OPTION_M4100},
1651 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1652 {"m3900", no_argument, NULL, OPTION_M3900},
1653 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1654
1655 /* Options which enable bug fixes. */
1656 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1657 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1658 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
6f2117ba
PH
1659 {"mfix-loongson3-llsc", no_argument, NULL, OPTION_FIX_LOONGSON3_LLSC},
1660 {"mno-fix-loongson3-llsc", no_argument, NULL, OPTION_NO_FIX_LOONGSON3_LLSC},
c31f3936
RS
1661 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1662 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1663 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1664 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1665 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
1666 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1667 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
1668 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1669 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
1670 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
a8d14a88
CM
1671 {"mfix-rm7000", no_argument, NULL, OPTION_FIX_RM7000},
1672 {"mno-fix-rm7000", no_argument, NULL, OPTION_NO_FIX_RM7000},
c31f3936
RS
1673 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1674 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
27c634e0
FN
1675 {"mfix-r5900", no_argument, NULL, OPTION_FIX_R5900},
1676 {"mno-fix-r5900", no_argument, NULL, OPTION_NO_FIX_R5900},
c31f3936
RS
1677
1678 /* Miscellaneous options. */
1679 {"trap", no_argument, NULL, OPTION_TRAP},
1680 {"no-break", no_argument, NULL, OPTION_TRAP},
1681 {"break", no_argument, NULL, OPTION_BREAK},
1682 {"no-trap", no_argument, NULL, OPTION_BREAK},
1683 {"EB", no_argument, NULL, OPTION_EB},
1684 {"EL", no_argument, NULL, OPTION_EL},
1685 {"mfp32", no_argument, NULL, OPTION_FP32},
1686 {"mgp32", no_argument, NULL, OPTION_GP32},
1687 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1688 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1689 {"mfp64", no_argument, NULL, OPTION_FP64},
351cdf24 1690 {"mfpxx", no_argument, NULL, OPTION_FPXX},
c31f3936
RS
1691 {"mgp64", no_argument, NULL, OPTION_GP64},
1692 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1693 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
8b10b0b3
MR
1694 {"mignore-branch-isa", no_argument, NULL, OPTION_IGNORE_BRANCH_ISA},
1695 {"mno-ignore-branch-isa", no_argument, NULL, OPTION_NO_IGNORE_BRANCH_ISA},
833794fc
MR
1696 {"minsn32", no_argument, NULL, OPTION_INSN32},
1697 {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
c31f3936
RS
1698 {"mshared", no_argument, NULL, OPTION_MSHARED},
1699 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1700 {"msym32", no_argument, NULL, OPTION_MSYM32},
1701 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1702 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1703 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1704 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1705 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
351cdf24
MF
1706 {"modd-spreg", no_argument, NULL, OPTION_ODD_SPREG},
1707 {"mno-odd-spreg", no_argument, NULL, OPTION_NO_ODD_SPREG},
c31f3936
RS
1708
1709 /* Strictly speaking this next option is ELF specific,
1710 but we allow it for other ports as well in order to
1711 make testing easier. */
1712 {"32", no_argument, NULL, OPTION_32},
1713
1714 /* ELF-specific options. */
c31f3936
RS
1715 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1716 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1717 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1718 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
1719 {"xgot", no_argument, NULL, OPTION_XGOT},
1720 {"mabi", required_argument, NULL, OPTION_MABI},
1721 {"n32", no_argument, NULL, OPTION_N32},
1722 {"64", no_argument, NULL, OPTION_64},
1723 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1724 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1725 {"mpdr", no_argument, NULL, OPTION_PDR},
1726 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1727 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
ba92f887 1728 {"mnan", required_argument, NULL, OPTION_NAN},
c31f3936
RS
1729
1730 {NULL, no_argument, NULL, 0}
1731};
1732size_t md_longopts_size = sizeof (md_longopts);
1733\f
c6278170
RS
1734/* Information about either an Application Specific Extension or an
1735 optional architecture feature that, for simplicity, we treat in the
1736 same way as an ASE. */
1737struct mips_ase
1738{
1739 /* The name of the ASE, used in both the command-line and .set options. */
1740 const char *name;
1741
1742 /* The associated ASE_* flags. If the ASE is available on both 32-bit
1743 and 64-bit architectures, the flags here refer to the subset that
1744 is available on both. */
1745 unsigned int flags;
1746
1747 /* The ASE_* flag used for instructions that are available on 64-bit
1748 architectures but that are not included in FLAGS. */
1749 unsigned int flags64;
1750
1751 /* The command-line options that turn the ASE on and off. */
1752 int option_on;
1753 int option_off;
1754
1755 /* The minimum required architecture revisions for MIPS32, MIPS64,
1756 microMIPS32 and microMIPS64, or -1 if the extension isn't supported. */
1757 int mips32_rev;
1758 int mips64_rev;
1759 int micromips32_rev;
1760 int micromips64_rev;
7361da2c
AB
1761
1762 /* The architecture where the ASE was removed or -1 if the extension has not
1763 been removed. */
1764 int rem_rev;
c6278170
RS
1765};
1766
1767/* A table of all supported ASEs. */
1768static const struct mips_ase mips_ases[] = {
1769 { "dsp", ASE_DSP, ASE_DSP64,
1770 OPTION_DSP, OPTION_NO_DSP,
7361da2c
AB
1771 2, 2, 2, 2,
1772 -1 },
c6278170
RS
1773
1774 { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1775 OPTION_DSPR2, OPTION_NO_DSPR2,
7361da2c
AB
1776 2, 2, 2, 2,
1777 -1 },
c6278170 1778
8f4f9071
MF
1779 { "dspr3", ASE_DSP | ASE_DSPR2 | ASE_DSPR3, 0,
1780 OPTION_DSPR3, OPTION_NO_DSPR3,
1781 6, 6, -1, -1,
1782 -1 },
1783
c6278170
RS
1784 { "eva", ASE_EVA, 0,
1785 OPTION_EVA, OPTION_NO_EVA,
7361da2c
AB
1786 2, 2, 2, 2,
1787 -1 },
c6278170
RS
1788
1789 { "mcu", ASE_MCU, 0,
1790 OPTION_MCU, OPTION_NO_MCU,
7361da2c
AB
1791 2, 2, 2, 2,
1792 -1 },
c6278170
RS
1793
1794 /* Deprecated in MIPS64r5, but we don't implement that yet. */
1795 { "mdmx", ASE_MDMX, 0,
1796 OPTION_MDMX, OPTION_NO_MDMX,
7361da2c
AB
1797 -1, 1, -1, -1,
1798 6 },
c6278170
RS
1799
1800 /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2. */
1801 { "mips3d", ASE_MIPS3D, 0,
1802 OPTION_MIPS3D, OPTION_NO_MIPS3D,
7361da2c
AB
1803 2, 1, -1, -1,
1804 6 },
c6278170
RS
1805
1806 { "mt", ASE_MT, 0,
1807 OPTION_MT, OPTION_NO_MT,
7361da2c
AB
1808 2, 2, -1, -1,
1809 -1 },
c6278170
RS
1810
1811 { "smartmips", ASE_SMARTMIPS, 0,
1812 OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
7361da2c
AB
1813 1, -1, -1, -1,
1814 6 },
c6278170
RS
1815
1816 { "virt", ASE_VIRT, ASE_VIRT64,
1817 OPTION_VIRT, OPTION_NO_VIRT,
7361da2c
AB
1818 2, 2, 2, 2,
1819 -1 },
56d438b1
CF
1820
1821 { "msa", ASE_MSA, ASE_MSA64,
1822 OPTION_MSA, OPTION_NO_MSA,
7361da2c
AB
1823 2, 2, 2, 2,
1824 -1 },
7d64c587
AB
1825
1826 { "xpa", ASE_XPA, 0,
1827 OPTION_XPA, OPTION_NO_XPA,
909b4e3d 1828 2, 2, 2, 2,
7361da2c 1829 -1 },
25499ac7
MR
1830
1831 { "mips16e2", ASE_MIPS16E2, 0,
1832 OPTION_MIPS16E2, OPTION_NO_MIPS16E2,
1833 2, 2, -1, -1,
1834 6 },
730c3174
SE
1835
1836 { "crc", ASE_CRC, ASE_CRC64,
1837 OPTION_CRC, OPTION_NO_CRC,
1838 6, 6, -1, -1,
1839 -1 },
6f20c942
FS
1840
1841 { "ginv", ASE_GINV, 0,
1842 OPTION_GINV, OPTION_NO_GINV,
1843 6, 6, 6, 6,
1844 -1 },
8095d2f7
CX
1845
1846 { "loongson-mmi", ASE_LOONGSON_MMI, 0,
1847 OPTION_LOONGSON_MMI, OPTION_NO_LOONGSON_MMI,
1848 0, 0, -1, -1,
1849 -1 },
716c08de
CX
1850
1851 { "loongson-cam", ASE_LOONGSON_CAM, 0,
1852 OPTION_LOONGSON_CAM, OPTION_NO_LOONGSON_CAM,
1853 0, 0, -1, -1,
1854 -1 },
bdc6c06e
CX
1855
1856 { "loongson-ext", ASE_LOONGSON_EXT, 0,
1857 OPTION_LOONGSON_EXT, OPTION_NO_LOONGSON_EXT,
1858 0, 0, -1, -1,
1859 -1 },
a693765e
CX
1860
1861 { "loongson-ext2", ASE_LOONGSON_EXT | ASE_LOONGSON_EXT2, 0,
1862 OPTION_LOONGSON_EXT2, OPTION_NO_LOONGSON_EXT2,
1863 0, 0, -1, -1,
1864 -1 },
c6278170
RS
1865};
1866
1867/* The set of ASEs that require -mfp64. */
82bda27b 1868#define FP64_ASES (ASE_MIPS3D | ASE_MDMX | ASE_MSA)
c6278170
RS
1869
1870/* Groups of ASE_* flags that represent different revisions of an ASE. */
1871static const unsigned int mips_ase_groups[] = {
a693765e
CX
1872 ASE_DSP | ASE_DSPR2 | ASE_DSPR3,
1873 ASE_LOONGSON_EXT | ASE_LOONGSON_EXT2
c6278170
RS
1874};
1875\f
252b5132
RH
1876/* Pseudo-op table.
1877
1878 The following pseudo-ops from the Kane and Heinrich MIPS book
1879 should be defined here, but are currently unsupported: .alias,
1880 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1881
1882 The following pseudo-ops from the Kane and Heinrich MIPS book are
1883 specific to the type of debugging information being generated, and
1884 should be defined by the object format: .aent, .begin, .bend,
1885 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1886 .vreg.
1887
1888 The following pseudo-ops from the Kane and Heinrich MIPS book are
1889 not MIPS CPU specific, but are also not specific to the object file
1890 format. This file is probably the best place to define them, but
d84bcf09 1891 they are not currently supported: .asm0, .endr, .lab, .struct. */
252b5132 1892
e972090a
NC
1893static const pseudo_typeS mips_pseudo_table[] =
1894{
beae10d5 1895 /* MIPS specific pseudo-ops. */
252b5132
RH
1896 {"option", s_option, 0},
1897 {"set", s_mipsset, 0},
1898 {"rdata", s_change_sec, 'r'},
1899 {"sdata", s_change_sec, 's'},
1900 {"livereg", s_ignore, 0},
1901 {"abicalls", s_abicalls, 0},
1902 {"cpload", s_cpload, 0},
6478892d
TS
1903 {"cpsetup", s_cpsetup, 0},
1904 {"cplocal", s_cplocal, 0},
252b5132 1905 {"cprestore", s_cprestore, 0},
6478892d 1906 {"cpreturn", s_cpreturn, 0},
741d6ea8
JM
1907 {"dtprelword", s_dtprelword, 0},
1908 {"dtpreldword", s_dtpreldword, 0},
d0f13682
CLT
1909 {"tprelword", s_tprelword, 0},
1910 {"tpreldword", s_tpreldword, 0},
6478892d 1911 {"gpvalue", s_gpvalue, 0},
252b5132 1912 {"gpword", s_gpword, 0},
10181a0d 1913 {"gpdword", s_gpdword, 0},
a3f278e2 1914 {"ehword", s_ehword, 0},
252b5132
RH
1915 {"cpadd", s_cpadd, 0},
1916 {"insn", s_insn, 0},
ba92f887 1917 {"nan", s_nan, 0},
919731af 1918 {"module", s_module, 0},
252b5132 1919
beae10d5 1920 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132 1921 chips. */
38a57ae7 1922 {"asciiz", stringer, 8 + 1},
252b5132
RH
1923 {"bss", s_change_sec, 'b'},
1924 {"err", s_err, 0},
1925 {"half", s_cons, 1},
1926 {"dword", s_cons, 3},
1927 {"weakext", s_mips_weakext, 0},
7c752c2a
TS
1928 {"origin", s_org, 0},
1929 {"repeat", s_rept, 0},
252b5132 1930
998b3c36
MR
1931 /* For MIPS this is non-standard, but we define it for consistency. */
1932 {"sbss", s_change_sec, 'B'},
1933
beae10d5 1934 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1935 here for one reason or another. */
1936 {"align", s_align, 0},
1937 {"byte", s_cons, 0},
1938 {"data", s_change_sec, 'd'},
1939 {"double", s_float_cons, 'd'},
1940 {"float", s_float_cons, 'f'},
1941 {"globl", s_mips_globl, 0},
1942 {"global", s_mips_globl, 0},
1943 {"hword", s_cons, 1},
1944 {"int", s_cons, 2},
1945 {"long", s_cons, 2},
1946 {"octa", s_cons, 4},
1947 {"quad", s_cons, 3},
cca86cc8 1948 {"section", s_change_section, 0},
252b5132
RH
1949 {"short", s_cons, 1},
1950 {"single", s_float_cons, 'f'},
754e2bb9 1951 {"stabd", s_mips_stab, 'd'},
252b5132 1952 {"stabn", s_mips_stab, 'n'},
754e2bb9 1953 {"stabs", s_mips_stab, 's'},
252b5132
RH
1954 {"text", s_change_sec, 't'},
1955 {"word", s_cons, 2},
add56521 1956
add56521 1957 { "extern", ecoff_directive_extern, 0},
add56521 1958
43841e91 1959 { NULL, NULL, 0 },
252b5132
RH
1960};
1961
e972090a
NC
1962static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1963{
beae10d5
KH
1964 /* These pseudo-ops should be defined by the object file format.
1965 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1966 {"aent", s_mips_ent, 1},
1967 {"bgnb", s_ignore, 0},
1968 {"end", s_mips_end, 0},
1969 {"endb", s_ignore, 0},
1970 {"ent", s_mips_ent, 0},
c5dd6aab 1971 {"file", s_mips_file, 0},
252b5132
RH
1972 {"fmask", s_mips_mask, 'F'},
1973 {"frame", s_mips_frame, 0},
c5dd6aab 1974 {"loc", s_mips_loc, 0},
252b5132
RH
1975 {"mask", s_mips_mask, 'R'},
1976 {"verstamp", s_ignore, 0},
43841e91 1977 { NULL, NULL, 0 },
252b5132
RH
1978};
1979
3ae8dd8d
MR
1980/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1981 purpose of the `.dc.a' internal pseudo-op. */
1982
1983int
1984mips_address_bytes (void)
1985{
919731af 1986 file_mips_check_options ();
3ae8dd8d
MR
1987 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1988}
1989
17a2f251 1990extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1991
1992void
17a2f251 1993mips_pop_insert (void)
252b5132
RH
1994{
1995 pop_insert (mips_pseudo_table);
1996 if (! ECOFF_DEBUGGING)
1997 pop_insert (mips_nonecoff_pseudo_table);
1998}
1999\f
2000/* Symbols labelling the current insn. */
2001
e972090a
NC
2002struct insn_label_list
2003{
252b5132
RH
2004 struct insn_label_list *next;
2005 symbolS *label;
2006};
2007
252b5132 2008static struct insn_label_list *free_insn_labels;
742a56fe 2009#define label_list tc_segment_info_data.labels
252b5132 2010
17a2f251 2011static void mips_clear_insn_labels (void);
df58fc94
RS
2012static void mips_mark_labels (void);
2013static void mips_compressed_mark_labels (void);
252b5132
RH
2014
2015static inline void
17a2f251 2016mips_clear_insn_labels (void)
252b5132 2017{
ed9e98c2 2018 struct insn_label_list **pl;
a8dbcb85 2019 segment_info_type *si;
252b5132 2020
a8dbcb85
TS
2021 if (now_seg)
2022 {
2023 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
2024 ;
3739860c 2025
a8dbcb85
TS
2026 si = seg_info (now_seg);
2027 *pl = si->label_list;
2028 si->label_list = NULL;
2029 }
252b5132 2030}
a8dbcb85 2031
df58fc94
RS
2032/* Mark instruction labels in MIPS16/microMIPS mode. */
2033
2034static inline void
2035mips_mark_labels (void)
2036{
2037 if (HAVE_CODE_COMPRESSION)
2038 mips_compressed_mark_labels ();
2039}
252b5132
RH
2040\f
2041static char *expr_end;
2042
e423441d 2043/* An expression in a macro instruction. This is set by mips_ip and
b0e6f033 2044 mips16_ip and when populated is always an O_constant. */
252b5132
RH
2045
2046static expressionS imm_expr;
252b5132 2047
77bd4346
RS
2048/* The relocatable field in an instruction and the relocs associated
2049 with it. These variables are used for instructions like LUI and
2050 JAL as well as true offsets. They are also used for address
2051 operands in macros. */
252b5132 2052
77bd4346 2053static expressionS offset_expr;
f6688943
TS
2054static bfd_reloc_code_real_type offset_reloc[3]
2055 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 2056
df58fc94
RS
2057/* This is set to the resulting size of the instruction to be produced
2058 by mips16_ip if an explicit extension is used or by mips_ip if an
2059 explicit size is supplied. */
252b5132 2060
df58fc94 2061static unsigned int forced_insn_length;
252b5132 2062
e1b47bd5
RS
2063/* True if we are assembling an instruction. All dot symbols defined during
2064 this time should be treated as code labels. */
2065
2066static bfd_boolean mips_assembling_insn;
2067
ecb4347a
DJ
2068/* The pdr segment for per procedure frame/regmask info. Not used for
2069 ECOFF debugging. */
252b5132
RH
2070
2071static segT pdr_seg;
252b5132 2072
e013f690
TS
2073/* The default target format to use. */
2074
aeffff67
RS
2075#if defined (TE_FreeBSD)
2076#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
2077#elif defined (TE_TMIPS)
2078#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
2079#else
2080#define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
2081#endif
2082
e013f690 2083const char *
17a2f251 2084mips_target_format (void)
e013f690
TS
2085{
2086 switch (OUTPUT_FLAVOR)
2087 {
e013f690 2088 case bfd_target_elf_flavour:
0a44bf69
RS
2089#ifdef TE_VXWORKS
2090 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
2091 return (target_big_endian
2092 ? "elf32-bigmips-vxworks"
2093 : "elf32-littlemips-vxworks");
2094#endif
e013f690 2095 return (target_big_endian
cfe86eaa 2096 ? (HAVE_64BIT_OBJECTS
aeffff67 2097 ? ELF_TARGET ("elf64-", "big")
cfe86eaa 2098 : (HAVE_NEWABI
aeffff67
RS
2099 ? ELF_TARGET ("elf32-n", "big")
2100 : ELF_TARGET ("elf32-", "big")))
cfe86eaa 2101 : (HAVE_64BIT_OBJECTS
aeffff67 2102 ? ELF_TARGET ("elf64-", "little")
cfe86eaa 2103 : (HAVE_NEWABI
aeffff67
RS
2104 ? ELF_TARGET ("elf32-n", "little")
2105 : ELF_TARGET ("elf32-", "little"))));
e013f690
TS
2106 default:
2107 abort ();
2108 return NULL;
2109 }
2110}
2111
c6278170
RS
2112/* Return the ISA revision that is currently in use, or 0 if we are
2113 generating code for MIPS V or below. */
2114
2115static int
2116mips_isa_rev (void)
2117{
2118 if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
2119 return 2;
2120
ae52f483
AB
2121 if (mips_opts.isa == ISA_MIPS32R3 || mips_opts.isa == ISA_MIPS64R3)
2122 return 3;
2123
2124 if (mips_opts.isa == ISA_MIPS32R5 || mips_opts.isa == ISA_MIPS64R5)
2125 return 5;
2126
7361da2c
AB
2127 if (mips_opts.isa == ISA_MIPS32R6 || mips_opts.isa == ISA_MIPS64R6)
2128 return 6;
2129
c6278170
RS
2130 /* microMIPS implies revision 2 or above. */
2131 if (mips_opts.micromips)
2132 return 2;
2133
2134 if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
2135 return 1;
2136
2137 return 0;
2138}
2139
2140/* Return the mask of all ASEs that are revisions of those in FLAGS. */
2141
2142static unsigned int
2143mips_ase_mask (unsigned int flags)
2144{
2145 unsigned int i;
2146
2147 for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
2148 if (flags & mips_ase_groups[i])
2149 flags |= mips_ase_groups[i];
2150 return flags;
2151}
2152
2153/* Check whether the current ISA supports ASE. Issue a warning if
2154 appropriate. */
2155
2156static void
2157mips_check_isa_supports_ase (const struct mips_ase *ase)
2158{
2159 const char *base;
2160 int min_rev, size;
2161 static unsigned int warned_isa;
2162 static unsigned int warned_fp32;
2163
2164 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
2165 min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
2166 else
2167 min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
2168 if ((min_rev < 0 || mips_isa_rev () < min_rev)
2169 && (warned_isa & ase->flags) != ase->flags)
2170 {
2171 warned_isa |= ase->flags;
2172 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2173 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2174 if (min_rev < 0)
1661c76c 2175 as_warn (_("the %d-bit %s architecture does not support the"
c6278170
RS
2176 " `%s' extension"), size, base, ase->name);
2177 else
1661c76c 2178 as_warn (_("the `%s' extension requires %s%d revision %d or greater"),
c6278170
RS
2179 ase->name, base, size, min_rev);
2180 }
7361da2c
AB
2181 else if ((ase->rem_rev > 0 && mips_isa_rev () >= ase->rem_rev)
2182 && (warned_isa & ase->flags) != ase->flags)
2183 {
2184 warned_isa |= ase->flags;
2185 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2186 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2187 as_warn (_("the `%s' extension was removed in %s%d revision %d"),
2188 ase->name, base, size, ase->rem_rev);
2189 }
2190
c6278170 2191 if ((ase->flags & FP64_ASES)
0b35dfee 2192 && mips_opts.fp != 64
c6278170
RS
2193 && (warned_fp32 & ase->flags) != ase->flags)
2194 {
2195 warned_fp32 |= ase->flags;
1661c76c 2196 as_warn (_("the `%s' extension requires 64-bit FPRs"), ase->name);
c6278170
RS
2197 }
2198}
2199
2200/* Check all enabled ASEs to see whether they are supported by the
2201 chosen architecture. */
2202
2203static void
2204mips_check_isa_supports_ases (void)
2205{
2206 unsigned int i, mask;
2207
2208 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2209 {
2210 mask = mips_ase_mask (mips_ases[i].flags);
2211 if ((mips_opts.ase & mask) == mips_ases[i].flags)
2212 mips_check_isa_supports_ase (&mips_ases[i]);
2213 }
2214}
2215
2216/* Set the state of ASE to ENABLED_P. Return the mask of ASE_* flags
2217 that were affected. */
2218
2219static unsigned int
919731af 2220mips_set_ase (const struct mips_ase *ase, struct mips_set_options *opts,
2221 bfd_boolean enabled_p)
c6278170
RS
2222{
2223 unsigned int mask;
2224
2225 mask = mips_ase_mask (ase->flags);
919731af 2226 opts->ase &= ~mask;
92cebb3d
MR
2227
2228 /* Clear combination ASE flags, which need to be recalculated based on
2229 updated regular ASE settings. */
9785fc2a 2230 opts->ase &= ~(ASE_MIPS16E2_MT | ASE_XPA_VIRT);
92cebb3d 2231
c6278170 2232 if (enabled_p)
919731af 2233 opts->ase |= ase->flags;
25499ac7 2234
9785fc2a
MR
2235 /* The Virtualization ASE has eXtended Physical Addressing (XPA)
2236 instructions which are only valid when both ASEs are enabled.
2237 This sets the ASE_XPA_VIRT flag when both ASEs are present. */
2238 if ((opts->ase & (ASE_XPA | ASE_VIRT)) == (ASE_XPA | ASE_VIRT))
2239 {
2240 opts->ase |= ASE_XPA_VIRT;
2241 mask |= ASE_XPA_VIRT;
2242 }
25499ac7
MR
2243 if ((opts->ase & (ASE_MIPS16E2 | ASE_MT)) == (ASE_MIPS16E2 | ASE_MT))
2244 {
2245 opts->ase |= ASE_MIPS16E2_MT;
2246 mask |= ASE_MIPS16E2_MT;
2247 }
2248
c6278170
RS
2249 return mask;
2250}
2251
2252/* Return the ASE called NAME, or null if none. */
2253
2254static const struct mips_ase *
2255mips_lookup_ase (const char *name)
2256{
2257 unsigned int i;
2258
2259 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2260 if (strcmp (name, mips_ases[i].name) == 0)
2261 return &mips_ases[i];
2262 return NULL;
2263}
2264
df58fc94 2265/* Return the length of a microMIPS instruction in bytes. If bits of
100b4f2e
MR
2266 the mask beyond the low 16 are 0, then it is a 16-bit instruction,
2267 otherwise it is a 32-bit instruction. */
df58fc94
RS
2268
2269static inline unsigned int
2270micromips_insn_length (const struct mips_opcode *mo)
2271{
7fd53920 2272 return mips_opcode_32bit_p (mo) ? 4 : 2;
df58fc94
RS
2273}
2274
5c04167a
RS
2275/* Return the length of MIPS16 instruction OPCODE. */
2276
2277static inline unsigned int
2278mips16_opcode_length (unsigned long opcode)
2279{
2280 return (opcode >> 16) == 0 ? 2 : 4;
2281}
2282
1e915849
RS
2283/* Return the length of instruction INSN. */
2284
2285static inline unsigned int
2286insn_length (const struct mips_cl_insn *insn)
2287{
df58fc94
RS
2288 if (mips_opts.micromips)
2289 return micromips_insn_length (insn->insn_mo);
2290 else if (mips_opts.mips16)
5c04167a 2291 return mips16_opcode_length (insn->insn_opcode);
df58fc94 2292 else
1e915849 2293 return 4;
1e915849
RS
2294}
2295
2296/* Initialise INSN from opcode entry MO. Leave its position unspecified. */
2297
2298static void
2299create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
2300{
2301 size_t i;
2302
2303 insn->insn_mo = mo;
1e915849
RS
2304 insn->insn_opcode = mo->match;
2305 insn->frag = NULL;
2306 insn->where = 0;
2307 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2308 insn->fixp[i] = NULL;
2309 insn->fixed_p = (mips_opts.noreorder > 0);
2310 insn->noreorder_p = (mips_opts.noreorder > 0);
2311 insn->mips16_absolute_jump_p = 0;
15be625d 2312 insn->complete_p = 0;
e407c74b 2313 insn->cleared_p = 0;
1e915849
RS
2314}
2315
fc76e730
RS
2316/* Get a list of all the operands in INSN. */
2317
2318static const struct mips_operand_array *
2319insn_operands (const struct mips_cl_insn *insn)
2320{
2321 if (insn->insn_mo >= &mips_opcodes[0]
2322 && insn->insn_mo < &mips_opcodes[NUMOPCODES])
2323 return &mips_operands[insn->insn_mo - &mips_opcodes[0]];
2324
2325 if (insn->insn_mo >= &mips16_opcodes[0]
2326 && insn->insn_mo < &mips16_opcodes[bfd_mips16_num_opcodes])
2327 return &mips16_operands[insn->insn_mo - &mips16_opcodes[0]];
2328
2329 if (insn->insn_mo >= &micromips_opcodes[0]
2330 && insn->insn_mo < &micromips_opcodes[bfd_micromips_num_opcodes])
2331 return &micromips_operands[insn->insn_mo - &micromips_opcodes[0]];
2332
2333 abort ();
2334}
2335
2336/* Get a description of operand OPNO of INSN. */
2337
2338static const struct mips_operand *
2339insn_opno (const struct mips_cl_insn *insn, unsigned opno)
2340{
2341 const struct mips_operand_array *operands;
2342
2343 operands = insn_operands (insn);
2344 if (opno >= MAX_OPERANDS || !operands->operand[opno])
2345 abort ();
2346 return operands->operand[opno];
2347}
2348
e077a1c8
RS
2349/* Install UVAL as the value of OPERAND in INSN. */
2350
2351static inline void
2352insn_insert_operand (struct mips_cl_insn *insn,
2353 const struct mips_operand *operand, unsigned int uval)
2354{
25499ac7
MR
2355 if (mips_opts.mips16
2356 && operand->type == OP_INT && operand->lsb == 0
2357 && mips_opcode_32bit_p (insn->insn_mo))
2358 insn->insn_opcode |= mips16_immed_extend (uval, operand->size);
2359 else
2360 insn->insn_opcode = mips_insert_operand (operand, insn->insn_opcode, uval);
e077a1c8
RS
2361}
2362
fc76e730
RS
2363/* Extract the value of OPERAND from INSN. */
2364
2365static inline unsigned
2366insn_extract_operand (const struct mips_cl_insn *insn,
2367 const struct mips_operand *operand)
2368{
2369 return mips_extract_operand (operand, insn->insn_opcode);
2370}
2371
df58fc94 2372/* Record the current MIPS16/microMIPS mode in now_seg. */
742a56fe
RS
2373
2374static void
df58fc94 2375mips_record_compressed_mode (void)
742a56fe
RS
2376{
2377 segment_info_type *si;
2378
2379 si = seg_info (now_seg);
2380 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2381 si->tc_segment_info_data.mips16 = mips_opts.mips16;
df58fc94
RS
2382 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2383 si->tc_segment_info_data.micromips = mips_opts.micromips;
742a56fe
RS
2384}
2385
4d68580a
RS
2386/* Read a standard MIPS instruction from BUF. */
2387
2388static unsigned long
2389read_insn (char *buf)
2390{
2391 if (target_big_endian)
2392 return bfd_getb32 ((bfd_byte *) buf);
2393 else
2394 return bfd_getl32 ((bfd_byte *) buf);
2395}
2396
2397/* Write standard MIPS instruction INSN to BUF. Return a pointer to
2398 the next byte. */
2399
2400static char *
2401write_insn (char *buf, unsigned int insn)
2402{
2403 md_number_to_chars (buf, insn, 4);
2404 return buf + 4;
2405}
2406
2407/* Read a microMIPS or MIPS16 opcode from BUF, given that it
2408 has length LENGTH. */
2409
2410static unsigned long
2411read_compressed_insn (char *buf, unsigned int length)
2412{
2413 unsigned long insn;
2414 unsigned int i;
2415
2416 insn = 0;
2417 for (i = 0; i < length; i += 2)
2418 {
2419 insn <<= 16;
2420 if (target_big_endian)
2421 insn |= bfd_getb16 ((char *) buf);
2422 else
2423 insn |= bfd_getl16 ((char *) buf);
2424 buf += 2;
2425 }
2426 return insn;
2427}
2428
5c04167a
RS
2429/* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2430 instruction is LENGTH bytes long. Return a pointer to the next byte. */
2431
2432static char *
2433write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2434{
2435 unsigned int i;
2436
2437 for (i = 0; i < length; i += 2)
2438 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2439 return buf + length;
2440}
2441
1e915849
RS
2442/* Install INSN at the location specified by its "frag" and "where" fields. */
2443
2444static void
2445install_insn (const struct mips_cl_insn *insn)
2446{
2447 char *f = insn->frag->fr_literal + insn->where;
5c04167a
RS
2448 if (HAVE_CODE_COMPRESSION)
2449 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
1e915849 2450 else
4d68580a 2451 write_insn (f, insn->insn_opcode);
df58fc94 2452 mips_record_compressed_mode ();
1e915849
RS
2453}
2454
2455/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
2456 and install the opcode in the new location. */
2457
2458static void
2459move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2460{
2461 size_t i;
2462
2463 insn->frag = frag;
2464 insn->where = where;
2465 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2466 if (insn->fixp[i] != NULL)
2467 {
2468 insn->fixp[i]->fx_frag = frag;
2469 insn->fixp[i]->fx_where = where;
2470 }
2471 install_insn (insn);
2472}
2473
2474/* Add INSN to the end of the output. */
2475
2476static void
2477add_fixed_insn (struct mips_cl_insn *insn)
2478{
2479 char *f = frag_more (insn_length (insn));
2480 move_insn (insn, frag_now, f - frag_now->fr_literal);
2481}
2482
2483/* Start a variant frag and move INSN to the start of the variant part,
2484 marking it as fixed. The other arguments are as for frag_var. */
2485
2486static void
2487add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2488 relax_substateT subtype, symbolS *symbol, offsetT offset)
2489{
2490 frag_grow (max_chars);
2491 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2492 insn->fixed_p = 1;
2493 frag_var (rs_machine_dependent, max_chars, var,
2494 subtype, symbol, offset, NULL);
2495}
2496
2497/* Insert N copies of INSN into the history buffer, starting at
2498 position FIRST. Neither FIRST nor N need to be clipped. */
2499
2500static void
2501insert_into_history (unsigned int first, unsigned int n,
2502 const struct mips_cl_insn *insn)
2503{
2504 if (mips_relax.sequence != 2)
2505 {
2506 unsigned int i;
2507
2508 for (i = ARRAY_SIZE (history); i-- > first;)
2509 if (i >= first + n)
2510 history[i] = history[i - n];
2511 else
2512 history[i] = *insn;
2513 }
2514}
2515
e3de51ce
RS
2516/* Clear the error in insn_error. */
2517
2518static void
2519clear_insn_error (void)
2520{
2521 memset (&insn_error, 0, sizeof (insn_error));
2522}
2523
2524/* Possibly record error message MSG for the current instruction.
2525 If the error is about a particular argument, ARGNUM is the 1-based
2526 number of that argument, otherwise it is 0. FORMAT is the format
2527 of MSG. Return true if MSG was used, false if the current message
2528 was kept. */
2529
2530static bfd_boolean
2531set_insn_error_format (int argnum, enum mips_insn_error_format format,
2532 const char *msg)
2533{
2534 if (argnum == 0)
2535 {
2536 /* Give priority to errors against specific arguments, and to
2537 the first whole-instruction message. */
2538 if (insn_error.msg)
2539 return FALSE;
2540 }
2541 else
2542 {
2543 /* Keep insn_error if it is against a later argument. */
2544 if (argnum < insn_error.min_argnum)
2545 return FALSE;
2546
2547 /* If both errors are against the same argument but are different,
2548 give up on reporting a specific error for this argument.
2549 See the comment about mips_insn_error for details. */
2550 if (argnum == insn_error.min_argnum
2551 && insn_error.msg
2552 && strcmp (insn_error.msg, msg) != 0)
2553 {
2554 insn_error.msg = 0;
2555 insn_error.min_argnum += 1;
2556 return FALSE;
2557 }
2558 }
2559 insn_error.min_argnum = argnum;
2560 insn_error.format = format;
2561 insn_error.msg = msg;
2562 return TRUE;
2563}
2564
2565/* Record an instruction error with no % format fields. ARGNUM and MSG are
2566 as for set_insn_error_format. */
2567
2568static void
2569set_insn_error (int argnum, const char *msg)
2570{
2571 set_insn_error_format (argnum, ERR_FMT_PLAIN, msg);
2572}
2573
2574/* Record an instruction error with one %d field I. ARGNUM and MSG are
2575 as for set_insn_error_format. */
2576
2577static void
2578set_insn_error_i (int argnum, const char *msg, int i)
2579{
2580 if (set_insn_error_format (argnum, ERR_FMT_I, msg))
2581 insn_error.u.i = i;
2582}
2583
2584/* Record an instruction error with two %s fields S1 and S2. ARGNUM and MSG
2585 are as for set_insn_error_format. */
2586
2587static void
2588set_insn_error_ss (int argnum, const char *msg, const char *s1, const char *s2)
2589{
2590 if (set_insn_error_format (argnum, ERR_FMT_SS, msg))
2591 {
2592 insn_error.u.ss[0] = s1;
2593 insn_error.u.ss[1] = s2;
2594 }
2595}
2596
2597/* Report the error in insn_error, which is against assembly code STR. */
2598
2599static void
2600report_insn_error (const char *str)
2601{
e1fa0163 2602 const char *msg = concat (insn_error.msg, " `%s'", NULL);
e3de51ce 2603
e3de51ce
RS
2604 switch (insn_error.format)
2605 {
2606 case ERR_FMT_PLAIN:
2607 as_bad (msg, str);
2608 break;
2609
2610 case ERR_FMT_I:
2611 as_bad (msg, insn_error.u.i, str);
2612 break;
2613
2614 case ERR_FMT_SS:
2615 as_bad (msg, insn_error.u.ss[0], insn_error.u.ss[1], str);
2616 break;
2617 }
e1fa0163
NC
2618
2619 free ((char *) msg);
e3de51ce
RS
2620}
2621
71400594
RS
2622/* Initialize vr4120_conflicts. There is a bit of duplication here:
2623 the idea is to make it obvious at a glance that each errata is
2624 included. */
2625
2626static void
2627init_vr4120_conflicts (void)
2628{
2629#define CONFLICT(FIRST, SECOND) \
2630 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2631
2632 /* Errata 21 - [D]DIV[U] after [D]MACC */
2633 CONFLICT (MACC, DIV);
2634 CONFLICT (DMACC, DIV);
2635
2636 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
2637 CONFLICT (DMULT, DMULT);
2638 CONFLICT (DMULT, DMACC);
2639 CONFLICT (DMACC, DMULT);
2640 CONFLICT (DMACC, DMACC);
2641
2642 /* Errata 24 - MT{LO,HI} after [D]MACC */
2643 CONFLICT (MACC, MTHILO);
2644 CONFLICT (DMACC, MTHILO);
2645
2646 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2647 instruction is executed immediately after a MACC or DMACC
2648 instruction, the result of [either instruction] is incorrect." */
2649 CONFLICT (MACC, MULT);
2650 CONFLICT (MACC, DMULT);
2651 CONFLICT (DMACC, MULT);
2652 CONFLICT (DMACC, DMULT);
2653
2654 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2655 executed immediately after a DMULT, DMULTU, DIV, DIVU,
2656 DDIV or DDIVU instruction, the result of the MACC or
2657 DMACC instruction is incorrect.". */
2658 CONFLICT (DMULT, MACC);
2659 CONFLICT (DMULT, DMACC);
2660 CONFLICT (DIV, MACC);
2661 CONFLICT (DIV, DMACC);
2662
2663#undef CONFLICT
2664}
2665
707bfff6
TS
2666struct regname {
2667 const char *name;
2668 unsigned int num;
2669};
2670
14daeee3 2671#define RNUM_MASK 0x00000ff
56d438b1 2672#define RTYPE_MASK 0x0ffff00
14daeee3
RS
2673#define RTYPE_NUM 0x0000100
2674#define RTYPE_FPU 0x0000200
2675#define RTYPE_FCC 0x0000400
2676#define RTYPE_VEC 0x0000800
2677#define RTYPE_GP 0x0001000
2678#define RTYPE_CP0 0x0002000
2679#define RTYPE_PC 0x0004000
2680#define RTYPE_ACC 0x0008000
2681#define RTYPE_CCC 0x0010000
2682#define RTYPE_VI 0x0020000
2683#define RTYPE_VF 0x0040000
2684#define RTYPE_R5900_I 0x0080000
2685#define RTYPE_R5900_Q 0x0100000
2686#define RTYPE_R5900_R 0x0200000
2687#define RTYPE_R5900_ACC 0x0400000
56d438b1 2688#define RTYPE_MSA 0x0800000
14daeee3 2689#define RWARN 0x8000000
707bfff6
TS
2690
2691#define GENERIC_REGISTER_NUMBERS \
2692 {"$0", RTYPE_NUM | 0}, \
2693 {"$1", RTYPE_NUM | 1}, \
2694 {"$2", RTYPE_NUM | 2}, \
2695 {"$3", RTYPE_NUM | 3}, \
2696 {"$4", RTYPE_NUM | 4}, \
2697 {"$5", RTYPE_NUM | 5}, \
2698 {"$6", RTYPE_NUM | 6}, \
2699 {"$7", RTYPE_NUM | 7}, \
2700 {"$8", RTYPE_NUM | 8}, \
2701 {"$9", RTYPE_NUM | 9}, \
2702 {"$10", RTYPE_NUM | 10}, \
2703 {"$11", RTYPE_NUM | 11}, \
2704 {"$12", RTYPE_NUM | 12}, \
2705 {"$13", RTYPE_NUM | 13}, \
2706 {"$14", RTYPE_NUM | 14}, \
2707 {"$15", RTYPE_NUM | 15}, \
2708 {"$16", RTYPE_NUM | 16}, \
2709 {"$17", RTYPE_NUM | 17}, \
2710 {"$18", RTYPE_NUM | 18}, \
2711 {"$19", RTYPE_NUM | 19}, \
2712 {"$20", RTYPE_NUM | 20}, \
2713 {"$21", RTYPE_NUM | 21}, \
2714 {"$22", RTYPE_NUM | 22}, \
2715 {"$23", RTYPE_NUM | 23}, \
2716 {"$24", RTYPE_NUM | 24}, \
2717 {"$25", RTYPE_NUM | 25}, \
2718 {"$26", RTYPE_NUM | 26}, \
2719 {"$27", RTYPE_NUM | 27}, \
2720 {"$28", RTYPE_NUM | 28}, \
2721 {"$29", RTYPE_NUM | 29}, \
2722 {"$30", RTYPE_NUM | 30}, \
3739860c 2723 {"$31", RTYPE_NUM | 31}
707bfff6
TS
2724
2725#define FPU_REGISTER_NAMES \
2726 {"$f0", RTYPE_FPU | 0}, \
2727 {"$f1", RTYPE_FPU | 1}, \
2728 {"$f2", RTYPE_FPU | 2}, \
2729 {"$f3", RTYPE_FPU | 3}, \
2730 {"$f4", RTYPE_FPU | 4}, \
2731 {"$f5", RTYPE_FPU | 5}, \
2732 {"$f6", RTYPE_FPU | 6}, \
2733 {"$f7", RTYPE_FPU | 7}, \
2734 {"$f8", RTYPE_FPU | 8}, \
2735 {"$f9", RTYPE_FPU | 9}, \
2736 {"$f10", RTYPE_FPU | 10}, \
2737 {"$f11", RTYPE_FPU | 11}, \
2738 {"$f12", RTYPE_FPU | 12}, \
2739 {"$f13", RTYPE_FPU | 13}, \
2740 {"$f14", RTYPE_FPU | 14}, \
2741 {"$f15", RTYPE_FPU | 15}, \
2742 {"$f16", RTYPE_FPU | 16}, \
2743 {"$f17", RTYPE_FPU | 17}, \
2744 {"$f18", RTYPE_FPU | 18}, \
2745 {"$f19", RTYPE_FPU | 19}, \
2746 {"$f20", RTYPE_FPU | 20}, \
2747 {"$f21", RTYPE_FPU | 21}, \
2748 {"$f22", RTYPE_FPU | 22}, \
2749 {"$f23", RTYPE_FPU | 23}, \
2750 {"$f24", RTYPE_FPU | 24}, \
2751 {"$f25", RTYPE_FPU | 25}, \
2752 {"$f26", RTYPE_FPU | 26}, \
2753 {"$f27", RTYPE_FPU | 27}, \
2754 {"$f28", RTYPE_FPU | 28}, \
2755 {"$f29", RTYPE_FPU | 29}, \
2756 {"$f30", RTYPE_FPU | 30}, \
2757 {"$f31", RTYPE_FPU | 31}
2758
2759#define FPU_CONDITION_CODE_NAMES \
2760 {"$fcc0", RTYPE_FCC | 0}, \
2761 {"$fcc1", RTYPE_FCC | 1}, \
2762 {"$fcc2", RTYPE_FCC | 2}, \
2763 {"$fcc3", RTYPE_FCC | 3}, \
2764 {"$fcc4", RTYPE_FCC | 4}, \
2765 {"$fcc5", RTYPE_FCC | 5}, \
2766 {"$fcc6", RTYPE_FCC | 6}, \
2767 {"$fcc7", RTYPE_FCC | 7}
2768
2769#define COPROC_CONDITION_CODE_NAMES \
2770 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
2771 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
2772 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
2773 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
2774 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
2775 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
2776 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
2777 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
2778
2779#define N32N64_SYMBOLIC_REGISTER_NAMES \
2780 {"$a4", RTYPE_GP | 8}, \
2781 {"$a5", RTYPE_GP | 9}, \
2782 {"$a6", RTYPE_GP | 10}, \
2783 {"$a7", RTYPE_GP | 11}, \
2784 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
2785 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
2786 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
2787 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
2788 {"$t0", RTYPE_GP | 12}, \
2789 {"$t1", RTYPE_GP | 13}, \
2790 {"$t2", RTYPE_GP | 14}, \
2791 {"$t3", RTYPE_GP | 15}
2792
2793#define O32_SYMBOLIC_REGISTER_NAMES \
2794 {"$t0", RTYPE_GP | 8}, \
2795 {"$t1", RTYPE_GP | 9}, \
2796 {"$t2", RTYPE_GP | 10}, \
2797 {"$t3", RTYPE_GP | 11}, \
2798 {"$t4", RTYPE_GP | 12}, \
2799 {"$t5", RTYPE_GP | 13}, \
2800 {"$t6", RTYPE_GP | 14}, \
2801 {"$t7", RTYPE_GP | 15}, \
2802 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2803 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2804 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
3739860c 2805 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
707bfff6 2806
6f2117ba 2807/* Remaining symbolic register names. */
707bfff6
TS
2808#define SYMBOLIC_REGISTER_NAMES \
2809 {"$zero", RTYPE_GP | 0}, \
2810 {"$at", RTYPE_GP | 1}, \
2811 {"$AT", RTYPE_GP | 1}, \
2812 {"$v0", RTYPE_GP | 2}, \
2813 {"$v1", RTYPE_GP | 3}, \
2814 {"$a0", RTYPE_GP | 4}, \
2815 {"$a1", RTYPE_GP | 5}, \
2816 {"$a2", RTYPE_GP | 6}, \
2817 {"$a3", RTYPE_GP | 7}, \
2818 {"$s0", RTYPE_GP | 16}, \
2819 {"$s1", RTYPE_GP | 17}, \
2820 {"$s2", RTYPE_GP | 18}, \
2821 {"$s3", RTYPE_GP | 19}, \
2822 {"$s4", RTYPE_GP | 20}, \
2823 {"$s5", RTYPE_GP | 21}, \
2824 {"$s6", RTYPE_GP | 22}, \
2825 {"$s7", RTYPE_GP | 23}, \
2826 {"$t8", RTYPE_GP | 24}, \
2827 {"$t9", RTYPE_GP | 25}, \
2828 {"$k0", RTYPE_GP | 26}, \
2829 {"$kt0", RTYPE_GP | 26}, \
2830 {"$k1", RTYPE_GP | 27}, \
2831 {"$kt1", RTYPE_GP | 27}, \
2832 {"$gp", RTYPE_GP | 28}, \
2833 {"$sp", RTYPE_GP | 29}, \
2834 {"$s8", RTYPE_GP | 30}, \
2835 {"$fp", RTYPE_GP | 30}, \
2836 {"$ra", RTYPE_GP | 31}
2837
2838#define MIPS16_SPECIAL_REGISTER_NAMES \
2839 {"$pc", RTYPE_PC | 0}
2840
2841#define MDMX_VECTOR_REGISTER_NAMES \
6f2117ba
PH
2842 /* {"$v0", RTYPE_VEC | 0}, Clash with REG 2 above. */ \
2843 /* {"$v1", RTYPE_VEC | 1}, Clash with REG 3 above. */ \
707bfff6
TS
2844 {"$v2", RTYPE_VEC | 2}, \
2845 {"$v3", RTYPE_VEC | 3}, \
2846 {"$v4", RTYPE_VEC | 4}, \
2847 {"$v5", RTYPE_VEC | 5}, \
2848 {"$v6", RTYPE_VEC | 6}, \
2849 {"$v7", RTYPE_VEC | 7}, \
2850 {"$v8", RTYPE_VEC | 8}, \
2851 {"$v9", RTYPE_VEC | 9}, \
2852 {"$v10", RTYPE_VEC | 10}, \
2853 {"$v11", RTYPE_VEC | 11}, \
2854 {"$v12", RTYPE_VEC | 12}, \
2855 {"$v13", RTYPE_VEC | 13}, \
2856 {"$v14", RTYPE_VEC | 14}, \
2857 {"$v15", RTYPE_VEC | 15}, \
2858 {"$v16", RTYPE_VEC | 16}, \
2859 {"$v17", RTYPE_VEC | 17}, \
2860 {"$v18", RTYPE_VEC | 18}, \
2861 {"$v19", RTYPE_VEC | 19}, \
2862 {"$v20", RTYPE_VEC | 20}, \
2863 {"$v21", RTYPE_VEC | 21}, \
2864 {"$v22", RTYPE_VEC | 22}, \
2865 {"$v23", RTYPE_VEC | 23}, \
2866 {"$v24", RTYPE_VEC | 24}, \
2867 {"$v25", RTYPE_VEC | 25}, \
2868 {"$v26", RTYPE_VEC | 26}, \
2869 {"$v27", RTYPE_VEC | 27}, \
2870 {"$v28", RTYPE_VEC | 28}, \
2871 {"$v29", RTYPE_VEC | 29}, \
2872 {"$v30", RTYPE_VEC | 30}, \
2873 {"$v31", RTYPE_VEC | 31}
2874
14daeee3
RS
2875#define R5900_I_NAMES \
2876 {"$I", RTYPE_R5900_I | 0}
2877
2878#define R5900_Q_NAMES \
2879 {"$Q", RTYPE_R5900_Q | 0}
2880
2881#define R5900_R_NAMES \
2882 {"$R", RTYPE_R5900_R | 0}
2883
2884#define R5900_ACC_NAMES \
2885 {"$ACC", RTYPE_R5900_ACC | 0 }
2886
707bfff6
TS
2887#define MIPS_DSP_ACCUMULATOR_NAMES \
2888 {"$ac0", RTYPE_ACC | 0}, \
2889 {"$ac1", RTYPE_ACC | 1}, \
2890 {"$ac2", RTYPE_ACC | 2}, \
2891 {"$ac3", RTYPE_ACC | 3}
2892
2893static const struct regname reg_names[] = {
2894 GENERIC_REGISTER_NUMBERS,
2895 FPU_REGISTER_NAMES,
2896 FPU_CONDITION_CODE_NAMES,
2897 COPROC_CONDITION_CODE_NAMES,
2898
2899 /* The $txx registers depends on the abi,
2900 these will be added later into the symbol table from
3739860c 2901 one of the tables below once mips_abi is set after
707bfff6
TS
2902 parsing of arguments from the command line. */
2903 SYMBOLIC_REGISTER_NAMES,
2904
2905 MIPS16_SPECIAL_REGISTER_NAMES,
2906 MDMX_VECTOR_REGISTER_NAMES,
14daeee3
RS
2907 R5900_I_NAMES,
2908 R5900_Q_NAMES,
2909 R5900_R_NAMES,
2910 R5900_ACC_NAMES,
707bfff6
TS
2911 MIPS_DSP_ACCUMULATOR_NAMES,
2912 {0, 0}
2913};
2914
2915static const struct regname reg_names_o32[] = {
2916 O32_SYMBOLIC_REGISTER_NAMES,
2917 {0, 0}
2918};
2919
2920static const struct regname reg_names_n32n64[] = {
2921 N32N64_SYMBOLIC_REGISTER_NAMES,
2922 {0, 0}
2923};
2924
a92713e6
RS
2925/* Register symbols $v0 and $v1 map to GPRs 2 and 3, but they can also be
2926 interpreted as vector registers 0 and 1. If SYMVAL is the value of one
2927 of these register symbols, return the associated vector register,
2928 otherwise return SYMVAL itself. */
df58fc94 2929
a92713e6
RS
2930static unsigned int
2931mips_prefer_vec_regno (unsigned int symval)
707bfff6 2932{
a92713e6
RS
2933 if ((symval & -2) == (RTYPE_GP | 2))
2934 return RTYPE_VEC | (symval & 1);
2935 return symval;
2936}
2937
14daeee3
RS
2938/* Return true if string [S, E) is a valid register name, storing its
2939 symbol value in *SYMVAL_PTR if so. */
a92713e6
RS
2940
2941static bfd_boolean
14daeee3 2942mips_parse_register_1 (char *s, char *e, unsigned int *symval_ptr)
a92713e6 2943{
707bfff6 2944 char save_c;
14daeee3 2945 symbolS *symbol;
707bfff6
TS
2946
2947 /* Terminate name. */
2948 save_c = *e;
2949 *e = '\0';
2950
a92713e6
RS
2951 /* Look up the name. */
2952 symbol = symbol_find (s);
2953 *e = save_c;
2954
2955 if (!symbol || S_GET_SEGMENT (symbol) != reg_section)
2956 return FALSE;
2957
14daeee3
RS
2958 *symval_ptr = S_GET_VALUE (symbol);
2959 return TRUE;
2960}
2961
2962/* Return true if the string at *SPTR is a valid register name. Allow it
2963 to have a VU0-style channel suffix of the form x?y?z?w? if CHANNELS_PTR
2964 is nonnull.
2965
2966 When returning true, move *SPTR past the register, store the
2967 register's symbol value in *SYMVAL_PTR and the channel mask in
2968 *CHANNELS_PTR (if nonnull). The symbol value includes the register
2969 number (RNUM_MASK) and register type (RTYPE_MASK). The channel mask
2970 is a 4-bit value of the form XYZW and is 0 if no suffix was given. */
2971
2972static bfd_boolean
2973mips_parse_register (char **sptr, unsigned int *symval_ptr,
2974 unsigned int *channels_ptr)
2975{
2976 char *s, *e, *m;
2977 const char *q;
2978 unsigned int channels, symval, bit;
2979
2980 /* Find end of name. */
2981 s = e = *sptr;
2982 if (is_name_beginner (*e))
2983 ++e;
2984 while (is_part_of_name (*e))
2985 ++e;
2986
2987 channels = 0;
2988 if (!mips_parse_register_1 (s, e, &symval))
2989 {
2990 if (!channels_ptr)
2991 return FALSE;
2992
2993 /* Eat characters from the end of the string that are valid
2994 channel suffixes. The preceding register must be $ACC or
2995 end with a digit, so there is no ambiguity. */
2996 bit = 1;
2997 m = e;
2998 for (q = "wzyx"; *q; q++, bit <<= 1)
2999 if (m > s && m[-1] == *q)
3000 {
3001 --m;
3002 channels |= bit;
3003 }
3004
3005 if (channels == 0
3006 || !mips_parse_register_1 (s, m, &symval)
3007 || (symval & (RTYPE_VI | RTYPE_VF | RTYPE_R5900_ACC)) == 0)
3008 return FALSE;
3009 }
3010
a92713e6 3011 *sptr = e;
14daeee3
RS
3012 *symval_ptr = symval;
3013 if (channels_ptr)
3014 *channels_ptr = channels;
a92713e6
RS
3015 return TRUE;
3016}
3017
3018/* Check if SPTR points at a valid register specifier according to TYPES.
3019 If so, then return 1, advance S to consume the specifier and store
3020 the register's number in REGNOP, otherwise return 0. */
3021
3022static int
3023reg_lookup (char **s, unsigned int types, unsigned int *regnop)
3024{
3025 unsigned int regno;
3026
14daeee3 3027 if (mips_parse_register (s, &regno, NULL))
707bfff6 3028 {
a92713e6
RS
3029 if (types & RTYPE_VEC)
3030 regno = mips_prefer_vec_regno (regno);
3031 if (regno & types)
3032 regno &= RNUM_MASK;
3033 else
3034 regno = ~0;
707bfff6 3035 }
a92713e6 3036 else
707bfff6 3037 {
a92713e6 3038 if (types & RWARN)
1661c76c 3039 as_warn (_("unrecognized register name `%s'"), *s);
a92713e6 3040 regno = ~0;
707bfff6 3041 }
707bfff6 3042 if (regnop)
a92713e6
RS
3043 *regnop = regno;
3044 return regno <= RNUM_MASK;
707bfff6
TS
3045}
3046
14daeee3
RS
3047/* Parse a VU0 "x?y?z?w?" channel mask at S and store the associated
3048 mask in *CHANNELS. Return a pointer to the first unconsumed character. */
3049
3050static char *
3051mips_parse_vu0_channels (char *s, unsigned int *channels)
3052{
3053 unsigned int i;
3054
3055 *channels = 0;
3056 for (i = 0; i < 4; i++)
3057 if (*s == "xyzw"[i])
3058 {
3059 *channels |= 1 << (3 - i);
3060 ++s;
3061 }
3062 return s;
3063}
3064
a92713e6
RS
3065/* Token types for parsed operand lists. */
3066enum mips_operand_token_type {
3067 /* A plain register, e.g. $f2. */
3068 OT_REG,
df58fc94 3069
14daeee3
RS
3070 /* A 4-bit XYZW channel mask. */
3071 OT_CHANNELS,
3072
56d438b1
CF
3073 /* A constant vector index, e.g. [1]. */
3074 OT_INTEGER_INDEX,
3075
3076 /* A register vector index, e.g. [$2]. */
3077 OT_REG_INDEX,
df58fc94 3078
a92713e6
RS
3079 /* A continuous range of registers, e.g. $s0-$s4. */
3080 OT_REG_RANGE,
3081
3082 /* A (possibly relocated) expression. */
3083 OT_INTEGER,
3084
3085 /* A floating-point value. */
3086 OT_FLOAT,
3087
3088 /* A single character. This can be '(', ')' or ',', but '(' only appears
3089 before OT_REGs. */
3090 OT_CHAR,
3091
14daeee3
RS
3092 /* A doubled character, either "--" or "++". */
3093 OT_DOUBLE_CHAR,
3094
a92713e6
RS
3095 /* The end of the operand list. */
3096 OT_END
3097};
3098
3099/* A parsed operand token. */
3100struct mips_operand_token
3101{
3102 /* The type of token. */
3103 enum mips_operand_token_type type;
3104 union
3105 {
56d438b1 3106 /* The register symbol value for an OT_REG or OT_REG_INDEX. */
a92713e6
RS
3107 unsigned int regno;
3108
14daeee3
RS
3109 /* The 4-bit channel mask for an OT_CHANNEL_SUFFIX. */
3110 unsigned int channels;
3111
56d438b1
CF
3112 /* The integer value of an OT_INTEGER_INDEX. */
3113 addressT index;
a92713e6
RS
3114
3115 /* The two register symbol values involved in an OT_REG_RANGE. */
3116 struct {
3117 unsigned int regno1;
3118 unsigned int regno2;
3119 } reg_range;
3120
3121 /* The value of an OT_INTEGER. The value is represented as an
3122 expression and the relocation operators that were applied to
3123 that expression. The reloc entries are BFD_RELOC_UNUSED if no
3124 relocation operators were used. */
3125 struct {
3126 expressionS value;
3127 bfd_reloc_code_real_type relocs[3];
3128 } integer;
3129
3130 /* The binary data for an OT_FLOAT constant, and the number of bytes
3131 in the constant. */
3132 struct {
3133 unsigned char data[8];
3134 int length;
3135 } flt;
3136
14daeee3 3137 /* The character represented by an OT_CHAR or OT_DOUBLE_CHAR. */
a92713e6
RS
3138 char ch;
3139 } u;
3140};
3141
3142/* An obstack used to construct lists of mips_operand_tokens. */
3143static struct obstack mips_operand_tokens;
3144
3145/* Give TOKEN type TYPE and add it to mips_operand_tokens. */
3146
3147static void
3148mips_add_token (struct mips_operand_token *token,
3149 enum mips_operand_token_type type)
3150{
3151 token->type = type;
3152 obstack_grow (&mips_operand_tokens, token, sizeof (*token));
3153}
3154
3155/* Check whether S is '(' followed by a register name. Add OT_CHAR
3156 and OT_REG tokens for them if so, and return a pointer to the first
3157 unconsumed character. Return null otherwise. */
3158
3159static char *
3160mips_parse_base_start (char *s)
3161{
3162 struct mips_operand_token token;
14daeee3
RS
3163 unsigned int regno, channels;
3164 bfd_boolean decrement_p;
df58fc94 3165
a92713e6
RS
3166 if (*s != '(')
3167 return 0;
3168
3169 ++s;
3170 SKIP_SPACE_TABS (s);
14daeee3
RS
3171
3172 /* Only match "--" as part of a base expression. In other contexts "--X"
3173 is a double negative. */
3174 decrement_p = (s[0] == '-' && s[1] == '-');
3175 if (decrement_p)
3176 {
3177 s += 2;
3178 SKIP_SPACE_TABS (s);
3179 }
3180
3181 /* Allow a channel specifier because that leads to better error messages
3182 than treating something like "$vf0x++" as an expression. */
3183 if (!mips_parse_register (&s, &regno, &channels))
a92713e6
RS
3184 return 0;
3185
3186 token.u.ch = '(';
3187 mips_add_token (&token, OT_CHAR);
3188
14daeee3
RS
3189 if (decrement_p)
3190 {
3191 token.u.ch = '-';
3192 mips_add_token (&token, OT_DOUBLE_CHAR);
3193 }
3194
a92713e6
RS
3195 token.u.regno = regno;
3196 mips_add_token (&token, OT_REG);
3197
14daeee3
RS
3198 if (channels)
3199 {
3200 token.u.channels = channels;
3201 mips_add_token (&token, OT_CHANNELS);
3202 }
3203
3204 /* For consistency, only match "++" as part of base expressions too. */
3205 SKIP_SPACE_TABS (s);
3206 if (s[0] == '+' && s[1] == '+')
3207 {
3208 s += 2;
3209 token.u.ch = '+';
3210 mips_add_token (&token, OT_DOUBLE_CHAR);
3211 }
3212
a92713e6
RS
3213 return s;
3214}
3215
3216/* Parse one or more tokens from S. Return a pointer to the first
3217 unconsumed character on success. Return null if an error was found
3218 and store the error text in insn_error. FLOAT_FORMAT is as for
3219 mips_parse_arguments. */
3220
3221static char *
3222mips_parse_argument_token (char *s, char float_format)
3223{
6d4af3c2
AM
3224 char *end, *save_in;
3225 const char *err;
14daeee3 3226 unsigned int regno1, regno2, channels;
a92713e6
RS
3227 struct mips_operand_token token;
3228
3229 /* First look for "($reg", since we want to treat that as an
3230 OT_CHAR and OT_REG rather than an expression. */
3231 end = mips_parse_base_start (s);
3232 if (end)
3233 return end;
3234
3235 /* Handle other characters that end up as OT_CHARs. */
3236 if (*s == ')' || *s == ',')
3237 {
3238 token.u.ch = *s;
3239 mips_add_token (&token, OT_CHAR);
3240 ++s;
3241 return s;
3242 }
3243
3244 /* Handle tokens that start with a register. */
14daeee3 3245 if (mips_parse_register (&s, &regno1, &channels))
df58fc94 3246 {
14daeee3
RS
3247 if (channels)
3248 {
3249 /* A register and a VU0 channel suffix. */
3250 token.u.regno = regno1;
3251 mips_add_token (&token, OT_REG);
3252
3253 token.u.channels = channels;
3254 mips_add_token (&token, OT_CHANNELS);
3255 return s;
3256 }
3257
a92713e6
RS
3258 SKIP_SPACE_TABS (s);
3259 if (*s == '-')
df58fc94 3260 {
a92713e6
RS
3261 /* A register range. */
3262 ++s;
3263 SKIP_SPACE_TABS (s);
14daeee3 3264 if (!mips_parse_register (&s, &regno2, NULL))
a92713e6 3265 {
1661c76c 3266 set_insn_error (0, _("invalid register range"));
a92713e6
RS
3267 return 0;
3268 }
df58fc94 3269
a92713e6
RS
3270 token.u.reg_range.regno1 = regno1;
3271 token.u.reg_range.regno2 = regno2;
3272 mips_add_token (&token, OT_REG_RANGE);
3273 return s;
3274 }
a92713e6 3275
56d438b1
CF
3276 /* Add the register itself. */
3277 token.u.regno = regno1;
3278 mips_add_token (&token, OT_REG);
3279
3280 /* Check for a vector index. */
3281 if (*s == '[')
3282 {
a92713e6
RS
3283 ++s;
3284 SKIP_SPACE_TABS (s);
56d438b1
CF
3285 if (mips_parse_register (&s, &token.u.regno, NULL))
3286 mips_add_token (&token, OT_REG_INDEX);
3287 else
a92713e6 3288 {
56d438b1
CF
3289 expressionS element;
3290
3291 my_getExpression (&element, s);
3292 if (element.X_op != O_constant)
3293 {
3294 set_insn_error (0, _("vector element must be constant"));
3295 return 0;
3296 }
3297 s = expr_end;
3298 token.u.index = element.X_add_number;
3299 mips_add_token (&token, OT_INTEGER_INDEX);
a92713e6 3300 }
a92713e6
RS
3301 SKIP_SPACE_TABS (s);
3302 if (*s != ']')
3303 {
1661c76c 3304 set_insn_error (0, _("missing `]'"));
a92713e6
RS
3305 return 0;
3306 }
3307 ++s;
df58fc94 3308 }
a92713e6 3309 return s;
df58fc94
RS
3310 }
3311
a92713e6
RS
3312 if (float_format)
3313 {
3314 /* First try to treat expressions as floats. */
3315 save_in = input_line_pointer;
3316 input_line_pointer = s;
3317 err = md_atof (float_format, (char *) token.u.flt.data,
3318 &token.u.flt.length);
3319 end = input_line_pointer;
3320 input_line_pointer = save_in;
3321 if (err && *err)
3322 {
e3de51ce 3323 set_insn_error (0, err);
a92713e6
RS
3324 return 0;
3325 }
3326 if (s != end)
3327 {
3328 mips_add_token (&token, OT_FLOAT);
3329 return end;
3330 }
3331 }
3332
3333 /* Treat everything else as an integer expression. */
3334 token.u.integer.relocs[0] = BFD_RELOC_UNUSED;
3335 token.u.integer.relocs[1] = BFD_RELOC_UNUSED;
3336 token.u.integer.relocs[2] = BFD_RELOC_UNUSED;
3337 my_getSmallExpression (&token.u.integer.value, token.u.integer.relocs, s);
3338 s = expr_end;
3339 mips_add_token (&token, OT_INTEGER);
3340 return s;
3341}
3342
3343/* S points to the operand list for an instruction. FLOAT_FORMAT is 'f'
3344 if expressions should be treated as 32-bit floating-point constants,
3345 'd' if they should be treated as 64-bit floating-point constants,
3346 or 0 if they should be treated as integer expressions (the usual case).
3347
3348 Return a list of tokens on success, otherwise return 0. The caller
3349 must obstack_free the list after use. */
3350
3351static struct mips_operand_token *
3352mips_parse_arguments (char *s, char float_format)
3353{
3354 struct mips_operand_token token;
3355
3356 SKIP_SPACE_TABS (s);
3357 while (*s)
3358 {
3359 s = mips_parse_argument_token (s, float_format);
3360 if (!s)
3361 {
3362 obstack_free (&mips_operand_tokens,
3363 obstack_finish (&mips_operand_tokens));
3364 return 0;
3365 }
3366 SKIP_SPACE_TABS (s);
3367 }
3368 mips_add_token (&token, OT_END);
3369 return (struct mips_operand_token *) obstack_finish (&mips_operand_tokens);
df58fc94
RS
3370}
3371
d301a56b
RS
3372/* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
3373 and architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
037b32b9
AN
3374
3375static bfd_boolean
f79e2745 3376is_opcode_valid (const struct mips_opcode *mo)
037b32b9
AN
3377{
3378 int isa = mips_opts.isa;
846ef2d0 3379 int ase = mips_opts.ase;
037b32b9 3380 int fp_s, fp_d;
c6278170 3381 unsigned int i;
037b32b9 3382
be0fcbee 3383 if (ISA_HAS_64BIT_REGS (isa))
c6278170
RS
3384 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3385 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3386 ase |= mips_ases[i].flags64;
037b32b9 3387
d301a56b 3388 if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
037b32b9
AN
3389 return FALSE;
3390
3391 /* Check whether the instruction or macro requires single-precision or
3392 double-precision floating-point support. Note that this information is
3393 stored differently in the opcode table for insns and macros. */
3394 if (mo->pinfo == INSN_MACRO)
3395 {
3396 fp_s = mo->pinfo2 & INSN2_M_FP_S;
3397 fp_d = mo->pinfo2 & INSN2_M_FP_D;
3398 }
3399 else
3400 {
3401 fp_s = mo->pinfo & FP_S;
3402 fp_d = mo->pinfo & FP_D;
3403 }
3404
3405 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
3406 return FALSE;
3407
3408 if (fp_s && mips_opts.soft_float)
3409 return FALSE;
3410
3411 return TRUE;
3412}
3413
3414/* Return TRUE if the MIPS16 opcode MO is valid on the currently
3415 selected ISA and architecture. */
3416
3417static bfd_boolean
3418is_opcode_valid_16 (const struct mips_opcode *mo)
3419{
25499ac7
MR
3420 int isa = mips_opts.isa;
3421 int ase = mips_opts.ase;
3422 unsigned int i;
3423
3424 if (ISA_HAS_64BIT_REGS (isa))
3425 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3426 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3427 ase |= mips_ases[i].flags64;
3428
3429 return opcode_is_member (mo, isa, ase, mips_opts.arch);
037b32b9
AN
3430}
3431
df58fc94 3432/* Return TRUE if the size of the microMIPS opcode MO matches one
7fd53920
MR
3433 explicitly requested. Always TRUE in the standard MIPS mode.
3434 Use is_size_valid_16 for MIPS16 opcodes. */
df58fc94
RS
3435
3436static bfd_boolean
3437is_size_valid (const struct mips_opcode *mo)
3438{
3439 if (!mips_opts.micromips)
3440 return TRUE;
3441
833794fc
MR
3442 if (mips_opts.insn32)
3443 {
3444 if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
3445 return FALSE;
3446 if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
3447 return FALSE;
3448 }
df58fc94
RS
3449 if (!forced_insn_length)
3450 return TRUE;
3451 if (mo->pinfo == INSN_MACRO)
3452 return FALSE;
3453 return forced_insn_length == micromips_insn_length (mo);
3454}
3455
7fd53920
MR
3456/* Return TRUE if the size of the MIPS16 opcode MO matches one
3457 explicitly requested. */
3458
3459static bfd_boolean
3460is_size_valid_16 (const struct mips_opcode *mo)
3461{
3462 if (!forced_insn_length)
3463 return TRUE;
3464 if (mo->pinfo == INSN_MACRO)
3465 return FALSE;
3466 if (forced_insn_length == 2 && mips_opcode_32bit_p (mo))
3467 return FALSE;
0674ee5d
MR
3468 if (forced_insn_length == 4 && (mo->pinfo2 & INSN2_SHORT_ONLY))
3469 return FALSE;
7fd53920
MR
3470 return TRUE;
3471}
3472
df58fc94 3473/* Return TRUE if the microMIPS opcode MO is valid for the delay slot
e64af278
MR
3474 of the preceding instruction. Always TRUE in the standard MIPS mode.
3475
3476 We don't accept macros in 16-bit delay slots to avoid a case where
3477 a macro expansion fails because it relies on a preceding 32-bit real
3478 instruction to have matched and does not handle the operands correctly.
3479 The only macros that may expand to 16-bit instructions are JAL that
3480 cannot be placed in a delay slot anyway, and corner cases of BALIGN
3481 and BGT (that likewise cannot be placed in a delay slot) that decay to
3482 a NOP. In all these cases the macros precede any corresponding real
3483 instruction definitions in the opcode table, so they will match in the
3484 second pass where the size of the delay slot is ignored and therefore
3485 produce correct code. */
df58fc94
RS
3486
3487static bfd_boolean
3488is_delay_slot_valid (const struct mips_opcode *mo)
3489{
3490 if (!mips_opts.micromips)
3491 return TRUE;
3492
3493 if (mo->pinfo == INSN_MACRO)
c06dec14 3494 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
df58fc94
RS
3495 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
3496 && micromips_insn_length (mo) != 4)
3497 return FALSE;
3498 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
3499 && micromips_insn_length (mo) != 2)
3500 return FALSE;
3501
3502 return TRUE;
3503}
3504
fc76e730
RS
3505/* For consistency checking, verify that all bits of OPCODE are specified
3506 either by the match/mask part of the instruction definition, or by the
3507 operand list. Also build up a list of operands in OPERANDS.
3508
3509 INSN_BITS says which bits of the instruction are significant.
3510 If OPCODE is a standard or microMIPS instruction, DECODE_OPERAND
3511 provides the mips_operand description of each operand. DECODE_OPERAND
3512 is null for MIPS16 instructions. */
ab902481
RS
3513
3514static int
3515validate_mips_insn (const struct mips_opcode *opcode,
3516 unsigned long insn_bits,
fc76e730
RS
3517 const struct mips_operand *(*decode_operand) (const char *),
3518 struct mips_operand_array *operands)
ab902481
RS
3519{
3520 const char *s;
fc76e730 3521 unsigned long used_bits, doubled, undefined, opno, mask;
ab902481
RS
3522 const struct mips_operand *operand;
3523
fc76e730
RS
3524 mask = (opcode->pinfo == INSN_MACRO ? 0 : opcode->mask);
3525 if ((mask & opcode->match) != opcode->match)
ab902481
RS
3526 {
3527 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
3528 opcode->name, opcode->args);
3529 return 0;
3530 }
3531 used_bits = 0;
fc76e730 3532 opno = 0;
14daeee3
RS
3533 if (opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX)
3534 used_bits = mips_insert_operand (&mips_vu0_channel_mask, used_bits, -1);
ab902481
RS
3535 for (s = opcode->args; *s; ++s)
3536 switch (*s)
3537 {
3538 case ',':
3539 case '(':
3540 case ')':
3541 break;
3542
14daeee3
RS
3543 case '#':
3544 s++;
3545 break;
3546
ab902481 3547 default:
fc76e730 3548 if (!decode_operand)
7fd53920 3549 operand = decode_mips16_operand (*s, mips_opcode_32bit_p (opcode));
fc76e730
RS
3550 else
3551 operand = decode_operand (s);
3552 if (!operand && opcode->pinfo != INSN_MACRO)
ab902481
RS
3553 {
3554 as_bad (_("internal: unknown operand type: %s %s"),
3555 opcode->name, opcode->args);
3556 return 0;
3557 }
fc76e730
RS
3558 gas_assert (opno < MAX_OPERANDS);
3559 operands->operand[opno] = operand;
25499ac7
MR
3560 if (!decode_operand && operand
3561 && operand->type == OP_INT && operand->lsb == 0
3562 && mips_opcode_32bit_p (opcode))
3563 used_bits |= mips16_immed_extend (-1, operand->size);
3564 else if (operand && operand->type != OP_VU0_MATCH_SUFFIX)
fc76e730 3565 {
14daeee3 3566 used_bits = mips_insert_operand (operand, used_bits, -1);
fc76e730
RS
3567 if (operand->type == OP_MDMX_IMM_REG)
3568 /* Bit 5 is the format selector (OB vs QH). The opcode table
3569 has separate entries for each format. */
3570 used_bits &= ~(1 << (operand->lsb + 5));
3571 if (operand->type == OP_ENTRY_EXIT_LIST)
3572 used_bits &= ~(mask & 0x700);
38bf472a
MR
3573 /* interAptiv MR2 SAVE/RESTORE instructions have a discontiguous
3574 operand field that cannot be fully described with LSB/SIZE. */
3575 if (operand->type == OP_SAVE_RESTORE_LIST && operand->lsb == 6)
3576 used_bits &= ~0x6000;
fc76e730 3577 }
ab902481 3578 /* Skip prefix characters. */
7361da2c 3579 if (decode_operand && (*s == '+' || *s == 'm' || *s == '-'))
ab902481 3580 ++s;
fc76e730 3581 opno += 1;
ab902481
RS
3582 break;
3583 }
fc76e730 3584 doubled = used_bits & mask & insn_bits;
ab902481
RS
3585 if (doubled)
3586 {
3587 as_bad (_("internal: bad mips opcode (bits 0x%08lx doubly defined):"
3588 " %s %s"), doubled, opcode->name, opcode->args);
3589 return 0;
3590 }
fc76e730 3591 used_bits |= mask;
ab902481 3592 undefined = ~used_bits & insn_bits;
fc76e730 3593 if (opcode->pinfo != INSN_MACRO && undefined)
ab902481
RS
3594 {
3595 as_bad (_("internal: bad mips opcode (bits 0x%08lx undefined): %s %s"),
3596 undefined, opcode->name, opcode->args);
3597 return 0;
3598 }
3599 used_bits &= ~insn_bits;
3600 if (used_bits)
3601 {
3602 as_bad (_("internal: bad mips opcode (bits 0x%08lx defined): %s %s"),
3603 used_bits, opcode->name, opcode->args);
3604 return 0;
3605 }
3606 return 1;
3607}
3608
fc76e730
RS
3609/* The MIPS16 version of validate_mips_insn. */
3610
3611static int
3612validate_mips16_insn (const struct mips_opcode *opcode,
3613 struct mips_operand_array *operands)
3614{
7fd53920 3615 unsigned long insn_bits = mips_opcode_32bit_p (opcode) ? 0xffffffff : 0xffff;
fc76e730 3616
7fd53920 3617 return validate_mips_insn (opcode, insn_bits, 0, operands);
fc76e730
RS
3618}
3619
ab902481
RS
3620/* The microMIPS version of validate_mips_insn. */
3621
3622static int
fc76e730
RS
3623validate_micromips_insn (const struct mips_opcode *opc,
3624 struct mips_operand_array *operands)
ab902481
RS
3625{
3626 unsigned long insn_bits;
3627 unsigned long major;
3628 unsigned int length;
3629
fc76e730
RS
3630 if (opc->pinfo == INSN_MACRO)
3631 return validate_mips_insn (opc, 0xffffffff, decode_micromips_operand,
3632 operands);
3633
ab902481
RS
3634 length = micromips_insn_length (opc);
3635 if (length != 2 && length != 4)
3636 {
1661c76c 3637 as_bad (_("internal error: bad microMIPS opcode (incorrect length: %u): "
ab902481
RS
3638 "%s %s"), length, opc->name, opc->args);
3639 return 0;
3640 }
3641 major = opc->match >> (10 + 8 * (length - 2));
3642 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
3643 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
3644 {
1661c76c 3645 as_bad (_("internal error: bad microMIPS opcode "
ab902481
RS
3646 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
3647 return 0;
3648 }
3649
3650 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
3651 insn_bits = 1 << 4 * length;
3652 insn_bits <<= 4 * length;
3653 insn_bits -= 1;
fc76e730
RS
3654 return validate_mips_insn (opc, insn_bits, decode_micromips_operand,
3655 operands);
ab902481
RS
3656}
3657
707bfff6
TS
3658/* This function is called once, at assembler startup time. It should set up
3659 all the tables, etc. that the MD part of the assembler will need. */
156c2f8b 3660
252b5132 3661void
17a2f251 3662md_begin (void)
252b5132 3663{
3994f87e 3664 const char *retval = NULL;
156c2f8b 3665 int i = 0;
252b5132 3666 int broken = 0;
1f25f5d3 3667
0a44bf69
RS
3668 if (mips_pic != NO_PIC)
3669 {
3670 if (g_switch_seen && g_switch_value != 0)
3671 as_bad (_("-G may not be used in position-independent code"));
3672 g_switch_value = 0;
3673 }
00acd688
CM
3674 else if (mips_abicalls)
3675 {
3676 if (g_switch_seen && g_switch_value != 0)
3677 as_bad (_("-G may not be used with abicalls"));
3678 g_switch_value = 0;
3679 }
0a44bf69 3680
0b35dfee 3681 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
1661c76c 3682 as_warn (_("could not set architecture and machine"));
252b5132 3683
252b5132
RH
3684 op_hash = hash_new ();
3685
fc76e730 3686 mips_operands = XCNEWVEC (struct mips_operand_array, NUMOPCODES);
252b5132
RH
3687 for (i = 0; i < NUMOPCODES;)
3688 {
3689 const char *name = mips_opcodes[i].name;
3690
17a2f251 3691 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
3692 if (retval != NULL)
3693 {
3694 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
3695 mips_opcodes[i].name, retval);
3696 /* Probably a memory allocation problem? Give up now. */
1661c76c 3697 as_fatal (_("broken assembler, no assembly attempted"));
252b5132
RH
3698 }
3699 do
3700 {
fc76e730
RS
3701 if (!validate_mips_insn (&mips_opcodes[i], 0xffffffff,
3702 decode_mips_operand, &mips_operands[i]))
3703 broken = 1;
6f2117ba 3704
fc76e730 3705 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
252b5132 3706 {
fc76e730
RS
3707 create_insn (&nop_insn, mips_opcodes + i);
3708 if (mips_fix_loongson2f_nop)
3709 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
3710 nop_insn.fixed_p = 1;
252b5132 3711 }
6f2117ba
PH
3712
3713 if (sync_insn.insn_mo == NULL && strcmp (name, "sync") == 0)
3714 create_insn (&sync_insn, mips_opcodes + i);
3715
252b5132
RH
3716 ++i;
3717 }
3718 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
3719 }
3720
3721 mips16_op_hash = hash_new ();
fc76e730
RS
3722 mips16_operands = XCNEWVEC (struct mips_operand_array,
3723 bfd_mips16_num_opcodes);
252b5132
RH
3724
3725 i = 0;
3726 while (i < bfd_mips16_num_opcodes)
3727 {
3728 const char *name = mips16_opcodes[i].name;
3729
17a2f251 3730 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
3731 if (retval != NULL)
3732 as_fatal (_("internal: can't hash `%s': %s"),
3733 mips16_opcodes[i].name, retval);
3734 do
3735 {
fc76e730
RS
3736 if (!validate_mips16_insn (&mips16_opcodes[i], &mips16_operands[i]))
3737 broken = 1;
1e915849
RS
3738 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3739 {
3740 create_insn (&mips16_nop_insn, mips16_opcodes + i);
3741 mips16_nop_insn.fixed_p = 1;
3742 }
252b5132
RH
3743 ++i;
3744 }
3745 while (i < bfd_mips16_num_opcodes
3746 && strcmp (mips16_opcodes[i].name, name) == 0);
3747 }
3748
df58fc94 3749 micromips_op_hash = hash_new ();
fc76e730
RS
3750 micromips_operands = XCNEWVEC (struct mips_operand_array,
3751 bfd_micromips_num_opcodes);
df58fc94
RS
3752
3753 i = 0;
3754 while (i < bfd_micromips_num_opcodes)
3755 {
3756 const char *name = micromips_opcodes[i].name;
3757
3758 retval = hash_insert (micromips_op_hash, name,
3759 (void *) &micromips_opcodes[i]);
3760 if (retval != NULL)
3761 as_fatal (_("internal: can't hash `%s': %s"),
3762 micromips_opcodes[i].name, retval);
3763 do
fc76e730
RS
3764 {
3765 struct mips_cl_insn *micromips_nop_insn;
3766
3767 if (!validate_micromips_insn (&micromips_opcodes[i],
3768 &micromips_operands[i]))
3769 broken = 1;
3770
3771 if (micromips_opcodes[i].pinfo != INSN_MACRO)
3772 {
3773 if (micromips_insn_length (micromips_opcodes + i) == 2)
3774 micromips_nop_insn = &micromips_nop16_insn;
3775 else if (micromips_insn_length (micromips_opcodes + i) == 4)
3776 micromips_nop_insn = &micromips_nop32_insn;
3777 else
3778 continue;
3779
3780 if (micromips_nop_insn->insn_mo == NULL
3781 && strcmp (name, "nop") == 0)
3782 {
3783 create_insn (micromips_nop_insn, micromips_opcodes + i);
3784 micromips_nop_insn->fixed_p = 1;
3785 }
3786 }
3787 }
df58fc94
RS
3788 while (++i < bfd_micromips_num_opcodes
3789 && strcmp (micromips_opcodes[i].name, name) == 0);
3790 }
3791
252b5132 3792 if (broken)
1661c76c 3793 as_fatal (_("broken assembler, no assembly attempted"));
252b5132
RH
3794
3795 /* We add all the general register names to the symbol table. This
3796 helps us detect invalid uses of them. */
3739860c 3797 for (i = 0; reg_names[i].name; i++)
707bfff6 3798 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
8fc4ee9b 3799 reg_names[i].num, /* & RNUM_MASK, */
707bfff6
TS
3800 &zero_address_frag));
3801 if (HAVE_NEWABI)
3739860c 3802 for (i = 0; reg_names_n32n64[i].name; i++)
707bfff6 3803 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
8fc4ee9b 3804 reg_names_n32n64[i].num, /* & RNUM_MASK, */
252b5132 3805 &zero_address_frag));
707bfff6 3806 else
3739860c 3807 for (i = 0; reg_names_o32[i].name; i++)
707bfff6 3808 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
8fc4ee9b 3809 reg_names_o32[i].num, /* & RNUM_MASK, */
6047c971 3810 &zero_address_frag));
6047c971 3811
14daeee3
RS
3812 for (i = 0; i < 32; i++)
3813 {
92fce9bd 3814 char regname[6];
14daeee3
RS
3815
3816 /* R5900 VU0 floating-point register. */
92fce9bd 3817 sprintf (regname, "$vf%d", i);
14daeee3
RS
3818 symbol_table_insert (symbol_new (regname, reg_section,
3819 RTYPE_VF | i, &zero_address_frag));
3820
3821 /* R5900 VU0 integer register. */
92fce9bd 3822 sprintf (regname, "$vi%d", i);
14daeee3
RS
3823 symbol_table_insert (symbol_new (regname, reg_section,
3824 RTYPE_VI | i, &zero_address_frag));
3825
56d438b1 3826 /* MSA register. */
92fce9bd 3827 sprintf (regname, "$w%d", i);
56d438b1
CF
3828 symbol_table_insert (symbol_new (regname, reg_section,
3829 RTYPE_MSA | i, &zero_address_frag));
14daeee3
RS
3830 }
3831
a92713e6
RS
3832 obstack_init (&mips_operand_tokens);
3833
7d10b47d 3834 mips_no_prev_insn ();
252b5132
RH
3835
3836 mips_gprmask = 0;
3837 mips_cprmask[0] = 0;
3838 mips_cprmask[1] = 0;
3839 mips_cprmask[2] = 0;
3840 mips_cprmask[3] = 0;
3841
3842 /* set the default alignment for the text section (2**2) */
3843 record_alignment (text_section, 2);
3844
4d0d148d 3845 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132 3846
f3ded42a
RS
3847 /* On a native system other than VxWorks, sections must be aligned
3848 to 16 byte boundaries. When configured for an embedded ELF
3849 target, we don't bother. */
3850 if (strncmp (TARGET_OS, "elf", 3) != 0
3851 && strncmp (TARGET_OS, "vxworks", 7) != 0)
252b5132 3852 {
f3ded42a
RS
3853 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
3854 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
3855 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
3856 }
252b5132 3857
f3ded42a
RS
3858 /* Create a .reginfo section for register masks and a .mdebug
3859 section for debugging information. */
3860 {
3861 segT seg;
3862 subsegT subseg;
3863 flagword flags;
3864 segT sec;
3865
3866 seg = now_seg;
3867 subseg = now_subseg;
3868
3869 /* The ABI says this section should be loaded so that the
3870 running program can access it. However, we don't load it
6f2117ba 3871 if we are configured for an embedded target. */
f3ded42a
RS
3872 flags = SEC_READONLY | SEC_DATA;
3873 if (strncmp (TARGET_OS, "elf", 3) != 0)
3874 flags |= SEC_ALLOC | SEC_LOAD;
3875
3876 if (mips_abi != N64_ABI)
252b5132 3877 {
f3ded42a 3878 sec = subseg_new (".reginfo", (subsegT) 0);
bdaaa2e1 3879
f3ded42a
RS
3880 bfd_set_section_flags (stdoutput, sec, flags);
3881 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
252b5132 3882
f3ded42a
RS
3883 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
3884 }
3885 else
3886 {
3887 /* The 64-bit ABI uses a .MIPS.options section rather than
3888 .reginfo section. */
3889 sec = subseg_new (".MIPS.options", (subsegT) 0);
3890 bfd_set_section_flags (stdoutput, sec, flags);
3891 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132 3892
f3ded42a
RS
3893 /* Set up the option header. */
3894 {
3895 Elf_Internal_Options opthdr;
3896 char *f;
3897
3898 opthdr.kind = ODK_REGINFO;
3899 opthdr.size = (sizeof (Elf_External_Options)
3900 + sizeof (Elf64_External_RegInfo));
3901 opthdr.section = 0;
3902 opthdr.info = 0;
3903 f = frag_more (sizeof (Elf_External_Options));
3904 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
3905 (Elf_External_Options *) f);
3906
3907 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
3908 }
3909 }
252b5132 3910
351cdf24
MF
3911 sec = subseg_new (".MIPS.abiflags", (subsegT) 0);
3912 bfd_set_section_flags (stdoutput, sec,
3913 SEC_READONLY | SEC_DATA | SEC_ALLOC | SEC_LOAD);
3914 bfd_set_section_alignment (stdoutput, sec, 3);
3915 mips_flags_frag = frag_more (sizeof (Elf_External_ABIFlags_v0));
3916
f3ded42a
RS
3917 if (ECOFF_DEBUGGING)
3918 {
3919 sec = subseg_new (".mdebug", (subsegT) 0);
3920 (void) bfd_set_section_flags (stdoutput, sec,
3921 SEC_HAS_CONTENTS | SEC_READONLY);
3922 (void) bfd_set_section_alignment (stdoutput, sec, 2);
252b5132 3923 }
f3ded42a
RS
3924 else if (mips_flag_pdr)
3925 {
3926 pdr_seg = subseg_new (".pdr", (subsegT) 0);
3927 (void) bfd_set_section_flags (stdoutput, pdr_seg,
3928 SEC_READONLY | SEC_RELOC
3929 | SEC_DEBUGGING);
3930 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
3931 }
3932
3933 subseg_set (seg, subseg);
3934 }
252b5132 3935
71400594
RS
3936 if (mips_fix_vr4120)
3937 init_vr4120_conflicts ();
252b5132
RH
3938}
3939
351cdf24
MF
3940static inline void
3941fpabi_incompatible_with (int fpabi, const char *what)
3942{
3943 as_warn (_(".gnu_attribute %d,%d is incompatible with `%s'"),
3944 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3945}
3946
3947static inline void
3948fpabi_requires (int fpabi, const char *what)
3949{
3950 as_warn (_(".gnu_attribute %d,%d requires `%s'"),
3951 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3952}
3953
3954/* Check -mabi and register sizes against the specified FP ABI. */
3955static void
3956check_fpabi (int fpabi)
3957{
351cdf24
MF
3958 switch (fpabi)
3959 {
3960 case Val_GNU_MIPS_ABI_FP_DOUBLE:
ea79f94a
MF
3961 if (file_mips_opts.soft_float)
3962 fpabi_incompatible_with (fpabi, "softfloat");
3963 else if (file_mips_opts.single_float)
3964 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3965 if (file_mips_opts.gp == 64 && file_mips_opts.fp == 32)
3966 fpabi_incompatible_with (fpabi, "gp=64 fp=32");
3967 else if (file_mips_opts.gp == 32 && file_mips_opts.fp == 64)
3968 fpabi_incompatible_with (fpabi, "gp=32 fp=64");
351cdf24
MF
3969 break;
3970
3971 case Val_GNU_MIPS_ABI_FP_XX:
3972 if (mips_abi != O32_ABI)
3973 fpabi_requires (fpabi, "-mabi=32");
ea79f94a
MF
3974 else if (file_mips_opts.soft_float)
3975 fpabi_incompatible_with (fpabi, "softfloat");
3976 else if (file_mips_opts.single_float)
3977 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3978 else if (file_mips_opts.fp != 0)
3979 fpabi_requires (fpabi, "fp=xx");
351cdf24
MF
3980 break;
3981
3982 case Val_GNU_MIPS_ABI_FP_64A:
3983 case Val_GNU_MIPS_ABI_FP_64:
3984 if (mips_abi != O32_ABI)
3985 fpabi_requires (fpabi, "-mabi=32");
ea79f94a
MF
3986 else if (file_mips_opts.soft_float)
3987 fpabi_incompatible_with (fpabi, "softfloat");
3988 else if (file_mips_opts.single_float)
3989 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3990 else if (file_mips_opts.fp != 64)
3991 fpabi_requires (fpabi, "fp=64");
3992 else if (fpabi == Val_GNU_MIPS_ABI_FP_64 && !file_mips_opts.oddspreg)
3993 fpabi_incompatible_with (fpabi, "nooddspreg");
3994 else if (fpabi == Val_GNU_MIPS_ABI_FP_64A && file_mips_opts.oddspreg)
3995 fpabi_requires (fpabi, "nooddspreg");
351cdf24
MF
3996 break;
3997
3998 case Val_GNU_MIPS_ABI_FP_SINGLE:
3999 if (file_mips_opts.soft_float)
4000 fpabi_incompatible_with (fpabi, "softfloat");
4001 else if (!file_mips_opts.single_float)
4002 fpabi_requires (fpabi, "singlefloat");
4003 break;
4004
4005 case Val_GNU_MIPS_ABI_FP_SOFT:
4006 if (!file_mips_opts.soft_float)
4007 fpabi_requires (fpabi, "softfloat");
4008 break;
4009
4010 case Val_GNU_MIPS_ABI_FP_OLD_64:
4011 as_warn (_(".gnu_attribute %d,%d is no longer supported"),
4012 Tag_GNU_MIPS_ABI_FP, fpabi);
4013 break;
4014
3350cc01
CM
4015 case Val_GNU_MIPS_ABI_FP_NAN2008:
4016 /* Silently ignore compatibility value. */
4017 break;
4018
351cdf24
MF
4019 default:
4020 as_warn (_(".gnu_attribute %d,%d is not a recognized"
4021 " floating-point ABI"), Tag_GNU_MIPS_ABI_FP, fpabi);
4022 break;
4023 }
351cdf24
MF
4024}
4025
919731af 4026/* Perform consistency checks on the current options. */
4027
4028static void
4029mips_check_options (struct mips_set_options *opts, bfd_boolean abi_checks)
4030{
4031 /* Check the size of integer registers agrees with the ABI and ISA. */
4032 if (opts->gp == 64 && !ISA_HAS_64BIT_REGS (opts->isa))
4033 as_bad (_("`gp=64' used with a 32-bit processor"));
4034 else if (abi_checks
4035 && opts->gp == 32 && ABI_NEEDS_64BIT_REGS (mips_abi))
4036 as_bad (_("`gp=32' used with a 64-bit ABI"));
4037 else if (abi_checks
4038 && opts->gp == 64 && ABI_NEEDS_32BIT_REGS (mips_abi))
4039 as_bad (_("`gp=64' used with a 32-bit ABI"));
4040
4041 /* Check the size of the float registers agrees with the ABI and ISA. */
4042 switch (opts->fp)
4043 {
351cdf24
MF
4044 case 0:
4045 if (!CPU_HAS_LDC1_SDC1 (opts->arch))
4046 as_bad (_("`fp=xx' used with a cpu lacking ldc1/sdc1 instructions"));
4047 else if (opts->single_float == 1)
4048 as_bad (_("`fp=xx' cannot be used with `singlefloat'"));
4049 break;
919731af 4050 case 64:
4051 if (!ISA_HAS_64BIT_FPRS (opts->isa))
4052 as_bad (_("`fp=64' used with a 32-bit fpu"));
4053 else if (abi_checks
4054 && ABI_NEEDS_32BIT_REGS (mips_abi)
4055 && !ISA_HAS_MXHC1 (opts->isa))
4056 as_warn (_("`fp=64' used with a 32-bit ABI"));
4057 break;
4058 case 32:
4059 if (abi_checks
4060 && ABI_NEEDS_64BIT_REGS (mips_abi))
4061 as_warn (_("`fp=32' used with a 64-bit ABI"));
5f4678bb 4062 if (ISA_IS_R6 (opts->isa) && opts->single_float == 0)
7361da2c 4063 as_bad (_("`fp=32' used with a MIPS R6 cpu"));
919731af 4064 break;
4065 default:
4066 as_bad (_("Unknown size of floating point registers"));
4067 break;
4068 }
4069
351cdf24
MF
4070 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !opts->oddspreg)
4071 as_bad (_("`nooddspreg` cannot be used with a 64-bit ABI"));
4072
919731af 4073 if (opts->micromips == 1 && opts->mips16 == 1)
1357373c 4074 as_bad (_("`%s' cannot be used with `%s'"), "mips16", "micromips");
5f4678bb 4075 else if (ISA_IS_R6 (opts->isa)
7361da2c
AB
4076 && (opts->micromips == 1
4077 || opts->mips16 == 1))
1357373c 4078 as_fatal (_("`%s' cannot be used with `%s'"),
7361da2c 4079 opts->micromips ? "micromips" : "mips16",
5f4678bb 4080 mips_cpu_info_from_isa (opts->isa)->name);
7361da2c
AB
4081
4082 if (ISA_IS_R6 (opts->isa) && mips_relax_branch)
4083 as_fatal (_("branch relaxation is not supported in `%s'"),
4084 mips_cpu_info_from_isa (opts->isa)->name);
919731af 4085}
4086
4087/* Perform consistency checks on the module level options exactly once.
4088 This is a deferred check that happens:
4089 at the first .set directive
4090 or, at the first pseudo op that generates code (inc .dc.a)
4091 or, at the first instruction
4092 or, at the end. */
4093
4094static void
4095file_mips_check_options (void)
4096{
919731af 4097 if (file_mips_opts_checked)
4098 return;
4099
4100 /* The following code determines the register size.
4101 Similar code was added to GCC 3.3 (see override_options() in
4102 config/mips/mips.c). The GAS and GCC code should be kept in sync
4103 as much as possible. */
4104
4105 if (file_mips_opts.gp < 0)
4106 {
4107 /* Infer the integer register size from the ABI and processor.
4108 Restrict ourselves to 32-bit registers if that's all the
4109 processor has, or if the ABI cannot handle 64-bit registers. */
4110 file_mips_opts.gp = (ABI_NEEDS_32BIT_REGS (mips_abi)
4111 || !ISA_HAS_64BIT_REGS (file_mips_opts.isa))
4112 ? 32 : 64;
4113 }
4114
4115 if (file_mips_opts.fp < 0)
4116 {
4117 /* No user specified float register size.
4118 ??? GAS treats single-float processors as though they had 64-bit
4119 float registers (although it complains when double-precision
4120 instructions are used). As things stand, saying they have 32-bit
4121 registers would lead to spurious "register must be even" messages.
4122 So here we assume float registers are never smaller than the
4123 integer ones. */
4124 if (file_mips_opts.gp == 64)
4125 /* 64-bit integer registers implies 64-bit float registers. */
4126 file_mips_opts.fp = 64;
4127 else if ((file_mips_opts.ase & FP64_ASES)
4128 && ISA_HAS_64BIT_FPRS (file_mips_opts.isa))
4129 /* Handle ASEs that require 64-bit float registers, if possible. */
4130 file_mips_opts.fp = 64;
7361da2c
AB
4131 else if (ISA_IS_R6 (mips_opts.isa))
4132 /* R6 implies 64-bit float registers. */
4133 file_mips_opts.fp = 64;
919731af 4134 else
4135 /* 32-bit float registers. */
4136 file_mips_opts.fp = 32;
4137 }
4138
351cdf24
MF
4139 /* Disable operations on odd-numbered floating-point registers by default
4140 when using the FPXX ABI. */
4141 if (file_mips_opts.oddspreg < 0)
4142 {
4143 if (file_mips_opts.fp == 0)
4144 file_mips_opts.oddspreg = 0;
4145 else
4146 file_mips_opts.oddspreg = 1;
4147 }
4148
919731af 4149 /* End of GCC-shared inference code. */
4150
4151 /* This flag is set when we have a 64-bit capable CPU but use only
4152 32-bit wide registers. Note that EABI does not use it. */
4153 if (ISA_HAS_64BIT_REGS (file_mips_opts.isa)
4154 && ((mips_abi == NO_ABI && file_mips_opts.gp == 32)
4155 || mips_abi == O32_ABI))
4156 mips_32bitmode = 1;
4157
4158 if (file_mips_opts.isa == ISA_MIPS1 && mips_trap)
4159 as_bad (_("trap exception not supported at ISA 1"));
4160
4161 /* If the selected architecture includes support for ASEs, enable
4162 generation of code for them. */
4163 if (file_mips_opts.mips16 == -1)
4164 file_mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_opts.arch)) ? 1 : 0;
4165 if (file_mips_opts.micromips == -1)
4166 file_mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_opts.arch))
4167 ? 1 : 0;
4168
7361da2c
AB
4169 if (mips_nan2008 == -1)
4170 mips_nan2008 = (ISA_HAS_LEGACY_NAN (file_mips_opts.isa)) ? 0 : 1;
4171 else if (!ISA_HAS_LEGACY_NAN (file_mips_opts.isa) && mips_nan2008 == 0)
4172 as_fatal (_("`%s' does not support legacy NaN"),
4173 mips_cpu_info_from_arch (file_mips_opts.arch)->name);
4174
919731af 4175 /* Some ASEs require 64-bit FPRs, so -mfp32 should stop those ASEs from
4176 being selected implicitly. */
4177 if (file_mips_opts.fp != 64)
4178 file_ase_explicit |= ASE_MIPS3D | ASE_MDMX | ASE_MSA;
4179
4180 /* If the user didn't explicitly select or deselect a particular ASE,
4181 use the default setting for the CPU. */
3315614d 4182 file_mips_opts.ase |= (file_mips_opts.init_ase & ~file_ase_explicit);
919731af 4183
4184 /* Set up the current options. These may change throughout assembly. */
4185 mips_opts = file_mips_opts;
4186
4187 mips_check_isa_supports_ases ();
4188 mips_check_options (&file_mips_opts, TRUE);
4189 file_mips_opts_checked = TRUE;
4190
4191 if (!bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
4192 as_warn (_("could not set architecture and machine"));
4193}
4194
252b5132 4195void
17a2f251 4196md_assemble (char *str)
252b5132
RH
4197{
4198 struct mips_cl_insn insn;
f6688943
TS
4199 bfd_reloc_code_real_type unused_reloc[3]
4200 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 4201
919731af 4202 file_mips_check_options ();
4203
252b5132 4204 imm_expr.X_op = O_absent;
252b5132 4205 offset_expr.X_op = O_absent;
f6688943
TS
4206 offset_reloc[0] = BFD_RELOC_UNUSED;
4207 offset_reloc[1] = BFD_RELOC_UNUSED;
4208 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132 4209
e1b47bd5
RS
4210 mips_mark_labels ();
4211 mips_assembling_insn = TRUE;
e3de51ce 4212 clear_insn_error ();
e1b47bd5 4213
252b5132
RH
4214 if (mips_opts.mips16)
4215 mips16_ip (str, &insn);
4216 else
4217 {
4218 mips_ip (str, &insn);
beae10d5
KH
4219 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
4220 str, insn.insn_opcode));
252b5132
RH
4221 }
4222
e3de51ce
RS
4223 if (insn_error.msg)
4224 report_insn_error (str);
e1b47bd5 4225 else if (insn.insn_mo->pinfo == INSN_MACRO)
252b5132 4226 {
584892a6 4227 macro_start ();
252b5132
RH
4228 if (mips_opts.mips16)
4229 mips16_macro (&insn);
4230 else
833794fc 4231 macro (&insn, str);
584892a6 4232 macro_end ();
252b5132
RH
4233 }
4234 else
4235 {
77bd4346 4236 if (offset_expr.X_op != O_absent)
df58fc94 4237 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
252b5132 4238 else
df58fc94 4239 append_insn (&insn, NULL, unused_reloc, FALSE);
252b5132 4240 }
e1b47bd5
RS
4241
4242 mips_assembling_insn = FALSE;
252b5132
RH
4243}
4244
738e5348
RS
4245/* Convenience functions for abstracting away the differences between
4246 MIPS16 and non-MIPS16 relocations. */
4247
4248static inline bfd_boolean
4249mips16_reloc_p (bfd_reloc_code_real_type reloc)
4250{
4251 switch (reloc)
4252 {
4253 case BFD_RELOC_MIPS16_JMP:
4254 case BFD_RELOC_MIPS16_GPREL:
4255 case BFD_RELOC_MIPS16_GOT16:
4256 case BFD_RELOC_MIPS16_CALL16:
4257 case BFD_RELOC_MIPS16_HI16_S:
4258 case BFD_RELOC_MIPS16_HI16:
4259 case BFD_RELOC_MIPS16_LO16:
c9775dde 4260 case BFD_RELOC_MIPS16_16_PCREL_S1:
738e5348
RS
4261 return TRUE;
4262
4263 default:
4264 return FALSE;
4265 }
4266}
4267
df58fc94
RS
4268static inline bfd_boolean
4269micromips_reloc_p (bfd_reloc_code_real_type reloc)
4270{
4271 switch (reloc)
4272 {
4273 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4274 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4275 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4276 case BFD_RELOC_MICROMIPS_GPREL16:
4277 case BFD_RELOC_MICROMIPS_JMP:
4278 case BFD_RELOC_MICROMIPS_HI16:
4279 case BFD_RELOC_MICROMIPS_HI16_S:
4280 case BFD_RELOC_MICROMIPS_LO16:
4281 case BFD_RELOC_MICROMIPS_LITERAL:
4282 case BFD_RELOC_MICROMIPS_GOT16:
4283 case BFD_RELOC_MICROMIPS_CALL16:
4284 case BFD_RELOC_MICROMIPS_GOT_HI16:
4285 case BFD_RELOC_MICROMIPS_GOT_LO16:
4286 case BFD_RELOC_MICROMIPS_CALL_HI16:
4287 case BFD_RELOC_MICROMIPS_CALL_LO16:
4288 case BFD_RELOC_MICROMIPS_SUB:
4289 case BFD_RELOC_MICROMIPS_GOT_PAGE:
4290 case BFD_RELOC_MICROMIPS_GOT_OFST:
4291 case BFD_RELOC_MICROMIPS_GOT_DISP:
4292 case BFD_RELOC_MICROMIPS_HIGHEST:
4293 case BFD_RELOC_MICROMIPS_HIGHER:
4294 case BFD_RELOC_MICROMIPS_SCN_DISP:
4295 case BFD_RELOC_MICROMIPS_JALR:
4296 return TRUE;
4297
4298 default:
4299 return FALSE;
4300 }
4301}
4302
2309ddf2
MR
4303static inline bfd_boolean
4304jmp_reloc_p (bfd_reloc_code_real_type reloc)
4305{
4306 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
4307}
4308
0e9c5a5c
MR
4309static inline bfd_boolean
4310b_reloc_p (bfd_reloc_code_real_type reloc)
4311{
4312 return (reloc == BFD_RELOC_MIPS_26_PCREL_S2
4313 || reloc == BFD_RELOC_MIPS_21_PCREL_S2
4314 || reloc == BFD_RELOC_16_PCREL_S2
c9775dde 4315 || reloc == BFD_RELOC_MIPS16_16_PCREL_S1
0e9c5a5c
MR
4316 || reloc == BFD_RELOC_MICROMIPS_16_PCREL_S1
4317 || reloc == BFD_RELOC_MICROMIPS_10_PCREL_S1
4318 || reloc == BFD_RELOC_MICROMIPS_7_PCREL_S1);
4319}
4320
738e5348
RS
4321static inline bfd_boolean
4322got16_reloc_p (bfd_reloc_code_real_type reloc)
4323{
2309ddf2 4324 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
df58fc94 4325 || reloc == BFD_RELOC_MICROMIPS_GOT16);
738e5348
RS
4326}
4327
4328static inline bfd_boolean
4329hi16_reloc_p (bfd_reloc_code_real_type reloc)
4330{
2309ddf2 4331 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
df58fc94 4332 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
738e5348
RS
4333}
4334
4335static inline bfd_boolean
4336lo16_reloc_p (bfd_reloc_code_real_type reloc)
4337{
2309ddf2 4338 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
df58fc94
RS
4339 || reloc == BFD_RELOC_MICROMIPS_LO16);
4340}
4341
df58fc94
RS
4342static inline bfd_boolean
4343jalr_reloc_p (bfd_reloc_code_real_type reloc)
4344{
2309ddf2 4345 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
738e5348
RS
4346}
4347
f2ae14a1
RS
4348static inline bfd_boolean
4349gprel16_reloc_p (bfd_reloc_code_real_type reloc)
4350{
4351 return (reloc == BFD_RELOC_GPREL16 || reloc == BFD_RELOC_MIPS16_GPREL
4352 || reloc == BFD_RELOC_MICROMIPS_GPREL16);
4353}
4354
2de39019
CM
4355/* Return true if RELOC is a PC-relative relocation that does not have
4356 full address range. */
4357
4358static inline bfd_boolean
4359limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
4360{
4361 switch (reloc)
4362 {
4363 case BFD_RELOC_16_PCREL_S2:
c9775dde 4364 case BFD_RELOC_MIPS16_16_PCREL_S1:
2de39019
CM
4365 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4366 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4367 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
7361da2c
AB
4368 case BFD_RELOC_MIPS_21_PCREL_S2:
4369 case BFD_RELOC_MIPS_26_PCREL_S2:
4370 case BFD_RELOC_MIPS_18_PCREL_S3:
4371 case BFD_RELOC_MIPS_19_PCREL_S2:
2de39019
CM
4372 return TRUE;
4373
b47468a6 4374 case BFD_RELOC_32_PCREL:
7361da2c
AB
4375 case BFD_RELOC_HI16_S_PCREL:
4376 case BFD_RELOC_LO16_PCREL:
b47468a6
CM
4377 return HAVE_64BIT_ADDRESSES;
4378
2de39019
CM
4379 default:
4380 return FALSE;
4381 }
4382}
b47468a6 4383
5919d012 4384/* Return true if the given relocation might need a matching %lo().
0a44bf69
RS
4385 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
4386 need a matching %lo() when applied to local symbols. */
5919d012
RS
4387
4388static inline bfd_boolean
17a2f251 4389reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 4390{
3b91255e 4391 return (HAVE_IN_PLACE_ADDENDS
738e5348 4392 && (hi16_reloc_p (reloc)
0a44bf69
RS
4393 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
4394 all GOT16 relocations evaluate to "G". */
738e5348
RS
4395 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
4396}
4397
4398/* Return the type of %lo() reloc needed by RELOC, given that
4399 reloc_needs_lo_p. */
4400
4401static inline bfd_reloc_code_real_type
4402matching_lo_reloc (bfd_reloc_code_real_type reloc)
4403{
df58fc94
RS
4404 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
4405 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
4406 : BFD_RELOC_LO16));
5919d012
RS
4407}
4408
4409/* Return true if the given fixup is followed by a matching R_MIPS_LO16
4410 relocation. */
4411
4412static inline bfd_boolean
17a2f251 4413fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
4414{
4415 return (fixp->fx_next != NULL
738e5348 4416 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
5919d012
RS
4417 && fixp->fx_addsy == fixp->fx_next->fx_addsy
4418 && fixp->fx_offset == fixp->fx_next->fx_offset);
4419}
4420
462427c4
RS
4421/* Move all labels in LABELS to the current insertion point. TEXT_P
4422 says whether the labels refer to text or data. */
404a8071
RS
4423
4424static void
462427c4 4425mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
404a8071
RS
4426{
4427 struct insn_label_list *l;
4428 valueT val;
4429
462427c4 4430 for (l = labels; l != NULL; l = l->next)
404a8071 4431 {
9c2799c2 4432 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
404a8071
RS
4433 symbol_set_frag (l->label, frag_now);
4434 val = (valueT) frag_now_fix ();
df58fc94 4435 /* MIPS16/microMIPS text labels are stored as odd. */
462427c4 4436 if (text_p && HAVE_CODE_COMPRESSION)
404a8071
RS
4437 ++val;
4438 S_SET_VALUE (l->label, val);
4439 }
4440}
4441
462427c4
RS
4442/* Move all labels in insn_labels to the current insertion point
4443 and treat them as text labels. */
4444
4445static void
4446mips_move_text_labels (void)
4447{
4448 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
4449}
4450
9e009953
MR
4451/* Duplicate the test for LINK_ONCE sections as in `adjust_reloc_syms'. */
4452
5f0fe04b
TS
4453static bfd_boolean
4454s_is_linkonce (symbolS *sym, segT from_seg)
4455{
4456 bfd_boolean linkonce = FALSE;
4457 segT symseg = S_GET_SEGMENT (sym);
4458
4459 if (symseg != from_seg && !S_IS_LOCAL (sym))
4460 {
4461 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
4462 linkonce = TRUE;
5f0fe04b
TS
4463 /* The GNU toolchain uses an extension for ELF: a section
4464 beginning with the magic string .gnu.linkonce is a
4465 linkonce section. */
4466 if (strncmp (segment_name (symseg), ".gnu.linkonce",
4467 sizeof ".gnu.linkonce" - 1) == 0)
4468 linkonce = TRUE;
5f0fe04b
TS
4469 }
4470 return linkonce;
4471}
4472
e1b47bd5 4473/* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
df58fc94
RS
4474 linker to handle them specially, such as generating jalx instructions
4475 when needed. We also make them odd for the duration of the assembly,
4476 in order to generate the right sort of code. We will make them even
252b5132
RH
4477 in the adjust_symtab routine, while leaving them marked. This is
4478 convenient for the debugger and the disassembler. The linker knows
4479 to make them odd again. */
4480
4481static void
e1b47bd5 4482mips_compressed_mark_label (symbolS *label)
252b5132 4483{
df58fc94 4484 gas_assert (HAVE_CODE_COMPRESSION);
a8dbcb85 4485
f3ded42a
RS
4486 if (mips_opts.mips16)
4487 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
4488 else
4489 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
e1b47bd5
RS
4490 if ((S_GET_VALUE (label) & 1) == 0
4491 /* Don't adjust the address if the label is global or weak, or
4492 in a link-once section, since we'll be emitting symbol reloc
4493 references to it which will be patched up by the linker, and
4494 the final value of the symbol may or may not be MIPS16/microMIPS. */
4495 && !S_IS_WEAK (label)
4496 && !S_IS_EXTERNAL (label)
4497 && !s_is_linkonce (label, now_seg))
4498 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
4499}
4500
4501/* Mark preceding MIPS16 or microMIPS instruction labels. */
4502
4503static void
4504mips_compressed_mark_labels (void)
4505{
4506 struct insn_label_list *l;
4507
4508 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
4509 mips_compressed_mark_label (l->label);
252b5132
RH
4510}
4511
4d7206a2
RS
4512/* End the current frag. Make it a variant frag and record the
4513 relaxation info. */
4514
4515static void
4516relax_close_frag (void)
4517{
584892a6 4518 mips_macro_warning.first_frag = frag_now;
4d7206a2 4519 frag_var (rs_machine_dependent, 0, 0,
ce8ad872
MR
4520 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1],
4521 mips_pic != NO_PIC),
4d7206a2
RS
4522 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
4523
4524 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
4525 mips_relax.first_fixup = 0;
4526}
4527
4528/* Start a new relaxation sequence whose expansion depends on SYMBOL.
4529 See the comment above RELAX_ENCODE for more details. */
4530
4531static void
4532relax_start (symbolS *symbol)
4533{
9c2799c2 4534 gas_assert (mips_relax.sequence == 0);
4d7206a2
RS
4535 mips_relax.sequence = 1;
4536 mips_relax.symbol = symbol;
4537}
4538
4539/* Start generating the second version of a relaxable sequence.
4540 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
4541
4542static void
4d7206a2
RS
4543relax_switch (void)
4544{
9c2799c2 4545 gas_assert (mips_relax.sequence == 1);
4d7206a2
RS
4546 mips_relax.sequence = 2;
4547}
4548
4549/* End the current relaxable sequence. */
4550
4551static void
4552relax_end (void)
4553{
9c2799c2 4554 gas_assert (mips_relax.sequence == 2);
4d7206a2
RS
4555 relax_close_frag ();
4556 mips_relax.sequence = 0;
4557}
4558
11625dd8
RS
4559/* Return true if IP is a delayed branch or jump. */
4560
4561static inline bfd_boolean
4562delayed_branch_p (const struct mips_cl_insn *ip)
4563{
4564 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
4565 | INSN_COND_BRANCH_DELAY
4566 | INSN_COND_BRANCH_LIKELY)) != 0;
4567}
4568
4569/* Return true if IP is a compact branch or jump. */
4570
4571static inline bfd_boolean
4572compact_branch_p (const struct mips_cl_insn *ip)
4573{
26545944
RS
4574 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
4575 | INSN2_COND_BRANCH)) != 0;
11625dd8
RS
4576}
4577
4578/* Return true if IP is an unconditional branch or jump. */
4579
4580static inline bfd_boolean
4581uncond_branch_p (const struct mips_cl_insn *ip)
4582{
4583 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
26545944 4584 || (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0);
11625dd8
RS
4585}
4586
4587/* Return true if IP is a branch-likely instruction. */
4588
4589static inline bfd_boolean
4590branch_likely_p (const struct mips_cl_insn *ip)
4591{
4592 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
4593}
4594
14fe068b
RS
4595/* Return the type of nop that should be used to fill the delay slot
4596 of delayed branch IP. */
4597
4598static struct mips_cl_insn *
4599get_delay_slot_nop (const struct mips_cl_insn *ip)
4600{
4601 if (mips_opts.micromips
4602 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
4603 return &micromips_nop32_insn;
4604 return NOP_INSN;
4605}
4606
fc76e730
RS
4607/* Return a mask that has bit N set if OPCODE reads the register(s)
4608 in operand N. */
df58fc94
RS
4609
4610static unsigned int
fc76e730 4611insn_read_mask (const struct mips_opcode *opcode)
df58fc94 4612{
fc76e730
RS
4613 return (opcode->pinfo & INSN_READ_ALL) >> INSN_READ_SHIFT;
4614}
df58fc94 4615
fc76e730
RS
4616/* Return a mask that has bit N set if OPCODE writes to the register(s)
4617 in operand N. */
4618
4619static unsigned int
4620insn_write_mask (const struct mips_opcode *opcode)
4621{
4622 return (opcode->pinfo & INSN_WRITE_ALL) >> INSN_WRITE_SHIFT;
4623}
4624
4625/* Return a mask of the registers specified by operand OPERAND of INSN.
4626 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4627 is set. */
4628
4629static unsigned int
4630operand_reg_mask (const struct mips_cl_insn *insn,
4631 const struct mips_operand *operand,
4632 unsigned int type_mask)
4633{
4634 unsigned int uval, vsel;
4635
4636 switch (operand->type)
df58fc94 4637 {
fc76e730
RS
4638 case OP_INT:
4639 case OP_MAPPED_INT:
4640 case OP_MSB:
4641 case OP_PCREL:
4642 case OP_PERF_REG:
4643 case OP_ADDIUSP_INT:
4644 case OP_ENTRY_EXIT_LIST:
4645 case OP_REPEAT_DEST_REG:
4646 case OP_REPEAT_PREV_REG:
4647 case OP_PC:
14daeee3
RS
4648 case OP_VU0_SUFFIX:
4649 case OP_VU0_MATCH_SUFFIX:
56d438b1 4650 case OP_IMM_INDEX:
fc76e730
RS
4651 abort ();
4652
25499ac7
MR
4653 case OP_REG28:
4654 return 1 << 28;
4655
fc76e730 4656 case OP_REG:
0f35dbc4 4657 case OP_OPTIONAL_REG:
fc76e730
RS
4658 {
4659 const struct mips_reg_operand *reg_op;
4660
4661 reg_op = (const struct mips_reg_operand *) operand;
4662 if (!(type_mask & (1 << reg_op->reg_type)))
4663 return 0;
4664 uval = insn_extract_operand (insn, operand);
4665 return 1 << mips_decode_reg_operand (reg_op, uval);
4666 }
4667
4668 case OP_REG_PAIR:
4669 {
4670 const struct mips_reg_pair_operand *pair_op;
4671
4672 pair_op = (const struct mips_reg_pair_operand *) operand;
4673 if (!(type_mask & (1 << pair_op->reg_type)))
4674 return 0;
4675 uval = insn_extract_operand (insn, operand);
4676 return (1 << pair_op->reg1_map[uval]) | (1 << pair_op->reg2_map[uval]);
4677 }
4678
4679 case OP_CLO_CLZ_DEST:
4680 if (!(type_mask & (1 << OP_REG_GP)))
4681 return 0;
4682 uval = insn_extract_operand (insn, operand);
4683 return (1 << (uval & 31)) | (1 << (uval >> 5));
4684
7361da2c
AB
4685 case OP_SAME_RS_RT:
4686 if (!(type_mask & (1 << OP_REG_GP)))
4687 return 0;
4688 uval = insn_extract_operand (insn, operand);
4689 gas_assert ((uval & 31) == (uval >> 5));
4690 return 1 << (uval & 31);
4691
4692 case OP_CHECK_PREV:
4693 case OP_NON_ZERO_REG:
4694 if (!(type_mask & (1 << OP_REG_GP)))
4695 return 0;
4696 uval = insn_extract_operand (insn, operand);
4697 return 1 << (uval & 31);
4698
fc76e730
RS
4699 case OP_LWM_SWM_LIST:
4700 abort ();
4701
4702 case OP_SAVE_RESTORE_LIST:
4703 abort ();
4704
4705 case OP_MDMX_IMM_REG:
4706 if (!(type_mask & (1 << OP_REG_VEC)))
4707 return 0;
4708 uval = insn_extract_operand (insn, operand);
4709 vsel = uval >> 5;
4710 if ((vsel & 0x18) == 0x18)
4711 return 0;
4712 return 1 << (uval & 31);
56d438b1
CF
4713
4714 case OP_REG_INDEX:
4715 if (!(type_mask & (1 << OP_REG_GP)))
4716 return 0;
4717 return 1 << insn_extract_operand (insn, operand);
df58fc94 4718 }
fc76e730
RS
4719 abort ();
4720}
4721
4722/* Return a mask of the registers specified by operands OPNO_MASK of INSN,
4723 where bit N of OPNO_MASK is set if operand N should be included.
4724 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4725 is set. */
4726
4727static unsigned int
4728insn_reg_mask (const struct mips_cl_insn *insn,
4729 unsigned int type_mask, unsigned int opno_mask)
4730{
4731 unsigned int opno, reg_mask;
4732
4733 opno = 0;
4734 reg_mask = 0;
4735 while (opno_mask != 0)
4736 {
4737 if (opno_mask & 1)
4738 reg_mask |= operand_reg_mask (insn, insn_opno (insn, opno), type_mask);
4739 opno_mask >>= 1;
4740 opno += 1;
4741 }
4742 return reg_mask;
df58fc94
RS
4743}
4744
4c260379
RS
4745/* Return the mask of core registers that IP reads. */
4746
4747static unsigned int
4748gpr_read_mask (const struct mips_cl_insn *ip)
4749{
4750 unsigned long pinfo, pinfo2;
4751 unsigned int mask;
4752
fc76e730 4753 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_read_mask (ip->insn_mo));
4c260379
RS
4754 pinfo = ip->insn_mo->pinfo;
4755 pinfo2 = ip->insn_mo->pinfo2;
fc76e730 4756 if (pinfo & INSN_UDI)
4c260379 4757 {
fc76e730
RS
4758 /* UDI instructions have traditionally been assumed to read RS
4759 and RT. */
4760 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4761 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4c260379 4762 }
fc76e730
RS
4763 if (pinfo & INSN_READ_GPR_24)
4764 mask |= 1 << 24;
4765 if (pinfo2 & INSN2_READ_GPR_16)
4766 mask |= 1 << 16;
4767 if (pinfo2 & INSN2_READ_SP)
4768 mask |= 1 << SP;
26545944 4769 if (pinfo2 & INSN2_READ_GPR_31)
fc76e730 4770 mask |= 1 << 31;
fe35f09f
RS
4771 /* Don't include register 0. */
4772 return mask & ~1;
4c260379
RS
4773}
4774
4775/* Return the mask of core registers that IP writes. */
4776
4777static unsigned int
4778gpr_write_mask (const struct mips_cl_insn *ip)
4779{
4780 unsigned long pinfo, pinfo2;
4781 unsigned int mask;
4782
fc76e730 4783 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_write_mask (ip->insn_mo));
4c260379
RS
4784 pinfo = ip->insn_mo->pinfo;
4785 pinfo2 = ip->insn_mo->pinfo2;
fc76e730
RS
4786 if (pinfo & INSN_WRITE_GPR_24)
4787 mask |= 1 << 24;
4788 if (pinfo & INSN_WRITE_GPR_31)
4789 mask |= 1 << 31;
4790 if (pinfo & INSN_UDI)
4791 /* UDI instructions have traditionally been assumed to write to RD. */
4792 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4793 if (pinfo2 & INSN2_WRITE_SP)
4794 mask |= 1 << SP;
fe35f09f
RS
4795 /* Don't include register 0. */
4796 return mask & ~1;
4c260379
RS
4797}
4798
4799/* Return the mask of floating-point registers that IP reads. */
4800
4801static unsigned int
4802fpr_read_mask (const struct mips_cl_insn *ip)
4803{
fc76e730 4804 unsigned long pinfo;
4c260379
RS
4805 unsigned int mask;
4806
9d5de888
CF
4807 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4808 | (1 << OP_REG_MSA)),
fc76e730 4809 insn_read_mask (ip->insn_mo));
4c260379 4810 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4811 /* Conservatively treat all operands to an FP_D instruction are doubles.
4812 (This is overly pessimistic for things like cvt.d.s.) */
bad1aba3 4813 if (FPR_SIZE != 64 && (pinfo & FP_D))
4c260379
RS
4814 mask |= mask << 1;
4815 return mask;
4816}
4817
4818/* Return the mask of floating-point registers that IP writes. */
4819
4820static unsigned int
4821fpr_write_mask (const struct mips_cl_insn *ip)
4822{
fc76e730 4823 unsigned long pinfo;
4c260379
RS
4824 unsigned int mask;
4825
9d5de888
CF
4826 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4827 | (1 << OP_REG_MSA)),
fc76e730 4828 insn_write_mask (ip->insn_mo));
4c260379 4829 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4830 /* Conservatively treat all operands to an FP_D instruction are doubles.
4831 (This is overly pessimistic for things like cvt.s.d.) */
bad1aba3 4832 if (FPR_SIZE != 64 && (pinfo & FP_D))
4c260379
RS
4833 mask |= mask << 1;
4834 return mask;
4835}
4836
a1d78564
RS
4837/* Operand OPNUM of INSN is an odd-numbered floating-point register.
4838 Check whether that is allowed. */
4839
4840static bfd_boolean
4841mips_oddfpreg_ok (const struct mips_opcode *insn, int opnum)
4842{
4843 const char *s = insn->name;
351cdf24
MF
4844 bfd_boolean oddspreg = (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa, mips_opts.arch)
4845 || FPR_SIZE == 64)
4846 && mips_opts.oddspreg;
a1d78564
RS
4847
4848 if (insn->pinfo == INSN_MACRO)
4849 /* Let a macro pass, we'll catch it later when it is expanded. */
4850 return TRUE;
4851
351cdf24
MF
4852 /* Single-precision coprocessor loads and moves are OK for 32-bit registers,
4853 otherwise it depends on oddspreg. */
4854 if ((insn->pinfo & FP_S)
4855 && (insn->pinfo & (INSN_LOAD_MEMORY | INSN_STORE_MEMORY
43885403 4856 | INSN_LOAD_COPROC | INSN_COPROC_MOVE)))
351cdf24 4857 return FPR_SIZE == 32 || oddspreg;
a1d78564 4858
351cdf24
MF
4859 /* Allow odd registers for single-precision ops and double-precision if the
4860 floating-point registers are 64-bit wide. */
4861 switch (insn->pinfo & (FP_S | FP_D))
4862 {
4863 case FP_S:
4864 case 0:
4865 return oddspreg;
4866 case FP_D:
4867 return FPR_SIZE == 64;
4868 default:
4869 break;
a1d78564
RS
4870 }
4871
351cdf24
MF
4872 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
4873 s = strchr (insn->name, '.');
4874 if (s != NULL && opnum == 2)
4875 s = strchr (s + 1, '.');
4876 if (s != NULL && (s[1] == 'w' || s[1] == 's'))
4877 return oddspreg;
a1d78564 4878
351cdf24 4879 return FPR_SIZE == 64;
a1d78564
RS
4880}
4881
a1d78564
RS
4882/* Information about an instruction argument that we're trying to match. */
4883struct mips_arg_info
4884{
4885 /* The instruction so far. */
4886 struct mips_cl_insn *insn;
4887
a92713e6
RS
4888 /* The first unconsumed operand token. */
4889 struct mips_operand_token *token;
4890
a1d78564
RS
4891 /* The 1-based operand number, in terms of insn->insn_mo->args. */
4892 int opnum;
4893
4894 /* The 1-based argument number, for error reporting. This does not
4895 count elided optional registers, etc.. */
4896 int argnum;
4897
4898 /* The last OP_REG operand seen, or ILLEGAL_REG if none. */
4899 unsigned int last_regno;
4900
4901 /* If the first operand was an OP_REG, this is the register that it
4902 specified, otherwise it is ILLEGAL_REG. */
4903 unsigned int dest_regno;
4904
4905 /* The value of the last OP_INT operand. Only used for OP_MSB,
4906 where it gives the lsb position. */
4907 unsigned int last_op_int;
4908
60f20e8b 4909 /* If true, match routines should assume that no later instruction
2b0f3761 4910 alternative matches and should therefore be as accommodating as
60f20e8b
RS
4911 possible. Match routines should not report errors if something
4912 is only invalid for !LAX_MATCH. */
4913 bfd_boolean lax_match;
a1d78564 4914
a1d78564
RS
4915 /* True if a reference to the current AT register was seen. */
4916 bfd_boolean seen_at;
4917};
4918
1a00e612
RS
4919/* Record that the argument is out of range. */
4920
4921static void
4922match_out_of_range (struct mips_arg_info *arg)
4923{
4924 set_insn_error_i (arg->argnum, _("operand %d out of range"), arg->argnum);
4925}
4926
4927/* Record that the argument isn't constant but needs to be. */
4928
4929static void
4930match_not_constant (struct mips_arg_info *arg)
4931{
4932 set_insn_error_i (arg->argnum, _("operand %d must be constant"),
4933 arg->argnum);
4934}
4935
a92713e6
RS
4936/* Try to match an OT_CHAR token for character CH. Consume the token
4937 and return true on success, otherwise return false. */
a1d78564 4938
a92713e6
RS
4939static bfd_boolean
4940match_char (struct mips_arg_info *arg, char ch)
a1d78564 4941{
a92713e6
RS
4942 if (arg->token->type == OT_CHAR && arg->token->u.ch == ch)
4943 {
4944 ++arg->token;
4945 if (ch == ',')
4946 arg->argnum += 1;
4947 return TRUE;
4948 }
4949 return FALSE;
4950}
a1d78564 4951
a92713e6
RS
4952/* Try to get an expression from the next tokens in ARG. Consume the
4953 tokens and return true on success, storing the expression value in
4954 VALUE and relocation types in R. */
4955
4956static bfd_boolean
4957match_expression (struct mips_arg_info *arg, expressionS *value,
4958 bfd_reloc_code_real_type *r)
4959{
d436c1c2
RS
4960 /* If the next token is a '(' that was parsed as being part of a base
4961 expression, assume we have an elided offset. The later match will fail
4962 if this turns out to be wrong. */
4963 if (arg->token->type == OT_CHAR && arg->token->u.ch == '(')
a1d78564 4964 {
d436c1c2
RS
4965 value->X_op = O_constant;
4966 value->X_add_number = 0;
4967 r[0] = r[1] = r[2] = BFD_RELOC_UNUSED;
a92713e6
RS
4968 return TRUE;
4969 }
4970
d436c1c2
RS
4971 /* Reject register-based expressions such as "0+$2" and "(($2))".
4972 For plain registers the default error seems more appropriate. */
4973 if (arg->token->type == OT_INTEGER
4974 && arg->token->u.integer.value.X_op == O_register)
a92713e6 4975 {
d436c1c2
RS
4976 set_insn_error (arg->argnum, _("register value used as expression"));
4977 return FALSE;
a1d78564 4978 }
d436c1c2
RS
4979
4980 if (arg->token->type == OT_INTEGER)
a92713e6 4981 {
d436c1c2
RS
4982 *value = arg->token->u.integer.value;
4983 memcpy (r, arg->token->u.integer.relocs, 3 * sizeof (*r));
4984 ++arg->token;
4985 return TRUE;
a92713e6 4986 }
a92713e6 4987
d436c1c2
RS
4988 set_insn_error_i
4989 (arg->argnum, _("operand %d must be an immediate expression"),
4990 arg->argnum);
4991 return FALSE;
a92713e6
RS
4992}
4993
4994/* Try to get a constant expression from the next tokens in ARG. Consume
de194d85 4995 the tokens and return true on success, storing the constant value
a54d5f8b 4996 in *VALUE. */
a92713e6
RS
4997
4998static bfd_boolean
1a00e612 4999match_const_int (struct mips_arg_info *arg, offsetT *value)
a92713e6
RS
5000{
5001 expressionS ex;
5002 bfd_reloc_code_real_type r[3];
a1d78564 5003
a92713e6
RS
5004 if (!match_expression (arg, &ex, r))
5005 return FALSE;
5006
5007 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_constant)
a1d78564
RS
5008 *value = ex.X_add_number;
5009 else
5010 {
c96425c5
MR
5011 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_big)
5012 match_out_of_range (arg);
5013 else
5014 match_not_constant (arg);
1a00e612 5015 return FALSE;
a1d78564 5016 }
a92713e6 5017 return TRUE;
a1d78564
RS
5018}
5019
5020/* Return the RTYPE_* flags for a register operand of type TYPE that
5021 appears in instruction OPCODE. */
5022
5023static unsigned int
5024convert_reg_type (const struct mips_opcode *opcode,
5025 enum mips_reg_operand_type type)
5026{
5027 switch (type)
5028 {
5029 case OP_REG_GP:
5030 return RTYPE_NUM | RTYPE_GP;
5031
5032 case OP_REG_FP:
5033 /* Allow vector register names for MDMX if the instruction is a 64-bit
5034 FPR load, store or move (including moves to and from GPRs). */
5035 if ((mips_opts.ase & ASE_MDMX)
5036 && (opcode->pinfo & FP_D)
43885403 5037 && (opcode->pinfo & (INSN_COPROC_MOVE
a1d78564 5038 | INSN_COPROC_MEMORY_DELAY
43885403 5039 | INSN_LOAD_COPROC
67dc82bc 5040 | INSN_LOAD_MEMORY
a1d78564
RS
5041 | INSN_STORE_MEMORY)))
5042 return RTYPE_FPU | RTYPE_VEC;
5043 return RTYPE_FPU;
5044
5045 case OP_REG_CCC:
5046 if (opcode->pinfo & (FP_D | FP_S))
5047 return RTYPE_CCC | RTYPE_FCC;
5048 return RTYPE_CCC;
5049
5050 case OP_REG_VEC:
5051 if (opcode->membership & INSN_5400)
5052 return RTYPE_FPU;
5053 return RTYPE_FPU | RTYPE_VEC;
5054
5055 case OP_REG_ACC:
5056 return RTYPE_ACC;
5057
5058 case OP_REG_COPRO:
5059 if (opcode->name[strlen (opcode->name) - 1] == '0')
5060 return RTYPE_NUM | RTYPE_CP0;
5061 return RTYPE_NUM;
5062
5063 case OP_REG_HW:
5064 return RTYPE_NUM;
14daeee3
RS
5065
5066 case OP_REG_VI:
5067 return RTYPE_NUM | RTYPE_VI;
5068
5069 case OP_REG_VF:
5070 return RTYPE_NUM | RTYPE_VF;
5071
5072 case OP_REG_R5900_I:
5073 return RTYPE_R5900_I;
5074
5075 case OP_REG_R5900_Q:
5076 return RTYPE_R5900_Q;
5077
5078 case OP_REG_R5900_R:
5079 return RTYPE_R5900_R;
5080
5081 case OP_REG_R5900_ACC:
5082 return RTYPE_R5900_ACC;
56d438b1
CF
5083
5084 case OP_REG_MSA:
5085 return RTYPE_MSA;
5086
5087 case OP_REG_MSA_CTRL:
5088 return RTYPE_NUM;
a1d78564
RS
5089 }
5090 abort ();
5091}
5092
5093/* ARG is register REGNO, of type TYPE. Warn about any dubious registers. */
5094
5095static void
5096check_regno (struct mips_arg_info *arg,
5097 enum mips_reg_operand_type type, unsigned int regno)
5098{
5099 if (AT && type == OP_REG_GP && regno == AT)
5100 arg->seen_at = TRUE;
5101
5102 if (type == OP_REG_FP
5103 && (regno & 1) != 0
a1d78564 5104 && !mips_oddfpreg_ok (arg->insn->insn_mo, arg->opnum))
351cdf24
MF
5105 {
5106 /* This was a warning prior to introducing O32 FPXX and FP64 support
5107 so maintain a warning for FP32 but raise an error for the new
5108 cases. */
5109 if (FPR_SIZE == 32)
5110 as_warn (_("float register should be even, was %d"), regno);
5111 else
5112 as_bad (_("float register should be even, was %d"), regno);
5113 }
a1d78564
RS
5114
5115 if (type == OP_REG_CCC)
5116 {
5117 const char *name;
5118 size_t length;
5119
5120 name = arg->insn->insn_mo->name;
5121 length = strlen (name);
5122 if ((regno & 1) != 0
5123 && ((length >= 3 && strcmp (name + length - 3, ".ps") == 0)
5124 || (length >= 5 && strncmp (name + length - 5, "any2", 4) == 0)))
1661c76c 5125 as_warn (_("condition code register should be even for %s, was %d"),
a1d78564
RS
5126 name, regno);
5127
5128 if ((regno & 3) != 0
5129 && (length >= 5 && strncmp (name + length - 5, "any4", 4) == 0))
1661c76c 5130 as_warn (_("condition code register should be 0 or 4 for %s, was %d"),
a1d78564
RS
5131 name, regno);
5132 }
5133}
5134
a92713e6
RS
5135/* ARG is a register with symbol value SYMVAL. Try to interpret it as
5136 a register of type TYPE. Return true on success, storing the register
5137 number in *REGNO and warning about any dubious uses. */
5138
5139static bfd_boolean
5140match_regno (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5141 unsigned int symval, unsigned int *regno)
5142{
5143 if (type == OP_REG_VEC)
5144 symval = mips_prefer_vec_regno (symval);
5145 if (!(symval & convert_reg_type (arg->insn->insn_mo, type)))
5146 return FALSE;
5147
5148 *regno = symval & RNUM_MASK;
5149 check_regno (arg, type, *regno);
5150 return TRUE;
5151}
5152
5153/* Try to interpret the next token in ARG as a register of type TYPE.
5154 Consume the token and return true on success, storing the register
5155 number in *REGNO. Return false on failure. */
5156
5157static bfd_boolean
5158match_reg (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5159 unsigned int *regno)
5160{
5161 if (arg->token->type == OT_REG
5162 && match_regno (arg, type, arg->token->u.regno, regno))
5163 {
5164 ++arg->token;
5165 return TRUE;
5166 }
5167 return FALSE;
5168}
5169
5170/* Try to interpret the next token in ARG as a range of registers of type TYPE.
5171 Consume the token and return true on success, storing the register numbers
5172 in *REGNO1 and *REGNO2. Return false on failure. */
5173
5174static bfd_boolean
5175match_reg_range (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5176 unsigned int *regno1, unsigned int *regno2)
5177{
5178 if (match_reg (arg, type, regno1))
5179 {
5180 *regno2 = *regno1;
5181 return TRUE;
5182 }
5183 if (arg->token->type == OT_REG_RANGE
5184 && match_regno (arg, type, arg->token->u.reg_range.regno1, regno1)
5185 && match_regno (arg, type, arg->token->u.reg_range.regno2, regno2)
5186 && *regno1 <= *regno2)
5187 {
5188 ++arg->token;
5189 return TRUE;
5190 }
5191 return FALSE;
5192}
5193
a1d78564
RS
5194/* OP_INT matcher. */
5195
a92713e6 5196static bfd_boolean
a1d78564 5197match_int_operand (struct mips_arg_info *arg,
a92713e6 5198 const struct mips_operand *operand_base)
a1d78564
RS
5199{
5200 const struct mips_int_operand *operand;
3ccad066 5201 unsigned int uval;
a1d78564
RS
5202 int min_val, max_val, factor;
5203 offsetT sval;
a1d78564
RS
5204
5205 operand = (const struct mips_int_operand *) operand_base;
5206 factor = 1 << operand->shift;
3ccad066
RS
5207 min_val = mips_int_operand_min (operand);
5208 max_val = mips_int_operand_max (operand);
a1d78564 5209
d436c1c2
RS
5210 if (operand_base->lsb == 0
5211 && operand_base->size == 16
5212 && operand->shift == 0
5213 && operand->bias == 0
5214 && (operand->max_val == 32767 || operand->max_val == 65535))
a1d78564
RS
5215 {
5216 /* The operand can be relocated. */
a92713e6
RS
5217 if (!match_expression (arg, &offset_expr, offset_reloc))
5218 return FALSE;
5219
c96425c5
MR
5220 if (offset_expr.X_op == O_big)
5221 {
5222 match_out_of_range (arg);
5223 return FALSE;
5224 }
5225
a92713e6 5226 if (offset_reloc[0] != BFD_RELOC_UNUSED)
33eaf5de 5227 /* Relocation operators were used. Accept the argument and
a1d78564
RS
5228 leave the relocation value in offset_expr and offset_relocs
5229 for the caller to process. */
a92713e6
RS
5230 return TRUE;
5231
5232 if (offset_expr.X_op != O_constant)
a1d78564 5233 {
60f20e8b
RS
5234 /* Accept non-constant operands if no later alternative matches,
5235 leaving it for the caller to process. */
5236 if (!arg->lax_match)
602b88e3
MR
5237 {
5238 match_not_constant (arg);
5239 return FALSE;
5240 }
a92713e6
RS
5241 offset_reloc[0] = BFD_RELOC_LO16;
5242 return TRUE;
a1d78564 5243 }
a92713e6 5244
a1d78564
RS
5245 /* Clear the global state; we're going to install the operand
5246 ourselves. */
a92713e6 5247 sval = offset_expr.X_add_number;
a1d78564 5248 offset_expr.X_op = O_absent;
60f20e8b
RS
5249
5250 /* For compatibility with older assemblers, we accept
5251 0x8000-0xffff as signed 16-bit numbers when only
5252 signed numbers are allowed. */
5253 if (sval > max_val)
5254 {
5255 max_val = ((1 << operand_base->size) - 1) << operand->shift;
5256 if (!arg->lax_match && sval <= max_val)
20c59b84
MR
5257 {
5258 match_out_of_range (arg);
5259 return FALSE;
5260 }
60f20e8b 5261 }
a1d78564
RS
5262 }
5263 else
5264 {
1a00e612 5265 if (!match_const_int (arg, &sval))
a92713e6 5266 return FALSE;
a1d78564
RS
5267 }
5268
5269 arg->last_op_int = sval;
5270
1a00e612 5271 if (sval < min_val || sval > max_val || sval % factor)
a1d78564 5272 {
1a00e612
RS
5273 match_out_of_range (arg);
5274 return FALSE;
a1d78564
RS
5275 }
5276
5277 uval = (unsigned int) sval >> operand->shift;
5278 uval -= operand->bias;
5279
5280 /* Handle -mfix-cn63xxp1. */
5281 if (arg->opnum == 1
5282 && mips_fix_cn63xxp1
5283 && !mips_opts.micromips
5284 && strcmp ("pref", arg->insn->insn_mo->name) == 0)
5285 switch (uval)
5286 {
5287 case 5:
5288 case 25:
5289 case 26:
5290 case 27:
5291 case 28:
5292 case 29:
5293 case 30:
5294 case 31:
5295 /* These are ok. */
5296 break;
5297
5298 default:
5299 /* The rest must be changed to 28. */
5300 uval = 28;
5301 break;
5302 }
5303
5304 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5305 return TRUE;
a1d78564
RS
5306}
5307
5308/* OP_MAPPED_INT matcher. */
5309
a92713e6 5310static bfd_boolean
a1d78564 5311match_mapped_int_operand (struct mips_arg_info *arg,
a92713e6 5312 const struct mips_operand *operand_base)
a1d78564
RS
5313{
5314 const struct mips_mapped_int_operand *operand;
5315 unsigned int uval, num_vals;
5316 offsetT sval;
5317
5318 operand = (const struct mips_mapped_int_operand *) operand_base;
1a00e612 5319 if (!match_const_int (arg, &sval))
a92713e6 5320 return FALSE;
a1d78564
RS
5321
5322 num_vals = 1 << operand_base->size;
5323 for (uval = 0; uval < num_vals; uval++)
5324 if (operand->int_map[uval] == sval)
5325 break;
5326 if (uval == num_vals)
1a00e612
RS
5327 {
5328 match_out_of_range (arg);
5329 return FALSE;
5330 }
a1d78564
RS
5331
5332 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5333 return TRUE;
a1d78564
RS
5334}
5335
5336/* OP_MSB matcher. */
5337
a92713e6 5338static bfd_boolean
a1d78564 5339match_msb_operand (struct mips_arg_info *arg,
a92713e6 5340 const struct mips_operand *operand_base)
a1d78564
RS
5341{
5342 const struct mips_msb_operand *operand;
5343 int min_val, max_val, max_high;
5344 offsetT size, sval, high;
5345
5346 operand = (const struct mips_msb_operand *) operand_base;
5347 min_val = operand->bias;
5348 max_val = min_val + (1 << operand_base->size) - 1;
5349 max_high = operand->opsize;
5350
1a00e612 5351 if (!match_const_int (arg, &size))
a92713e6 5352 return FALSE;
a1d78564
RS
5353
5354 high = size + arg->last_op_int;
5355 sval = operand->add_lsb ? high : size;
5356
5357 if (size < 0 || high > max_high || sval < min_val || sval > max_val)
5358 {
1a00e612
RS
5359 match_out_of_range (arg);
5360 return FALSE;
a1d78564
RS
5361 }
5362 insn_insert_operand (arg->insn, operand_base, sval - min_val);
a92713e6 5363 return TRUE;
a1d78564
RS
5364}
5365
5366/* OP_REG matcher. */
5367
a92713e6 5368static bfd_boolean
a1d78564 5369match_reg_operand (struct mips_arg_info *arg,
a92713e6 5370 const struct mips_operand *operand_base)
a1d78564
RS
5371{
5372 const struct mips_reg_operand *operand;
a92713e6 5373 unsigned int regno, uval, num_vals;
a1d78564
RS
5374
5375 operand = (const struct mips_reg_operand *) operand_base;
a92713e6
RS
5376 if (!match_reg (arg, operand->reg_type, &regno))
5377 return FALSE;
a1d78564
RS
5378
5379 if (operand->reg_map)
5380 {
5381 num_vals = 1 << operand->root.size;
5382 for (uval = 0; uval < num_vals; uval++)
5383 if (operand->reg_map[uval] == regno)
5384 break;
5385 if (num_vals == uval)
a92713e6 5386 return FALSE;
a1d78564
RS
5387 }
5388 else
5389 uval = regno;
5390
a1d78564
RS
5391 arg->last_regno = regno;
5392 if (arg->opnum == 1)
5393 arg->dest_regno = regno;
5394 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5395 return TRUE;
a1d78564
RS
5396}
5397
5398/* OP_REG_PAIR matcher. */
5399
a92713e6 5400static bfd_boolean
a1d78564 5401match_reg_pair_operand (struct mips_arg_info *arg,
a92713e6 5402 const struct mips_operand *operand_base)
a1d78564
RS
5403{
5404 const struct mips_reg_pair_operand *operand;
a92713e6 5405 unsigned int regno1, regno2, uval, num_vals;
a1d78564
RS
5406
5407 operand = (const struct mips_reg_pair_operand *) operand_base;
a92713e6
RS
5408 if (!match_reg (arg, operand->reg_type, &regno1)
5409 || !match_char (arg, ',')
5410 || !match_reg (arg, operand->reg_type, &regno2))
5411 return FALSE;
a1d78564
RS
5412
5413 num_vals = 1 << operand_base->size;
5414 for (uval = 0; uval < num_vals; uval++)
5415 if (operand->reg1_map[uval] == regno1 && operand->reg2_map[uval] == regno2)
5416 break;
5417 if (uval == num_vals)
a92713e6 5418 return FALSE;
a1d78564 5419
a1d78564 5420 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5421 return TRUE;
a1d78564
RS
5422}
5423
5424/* OP_PCREL matcher. The caller chooses the relocation type. */
5425
a92713e6
RS
5426static bfd_boolean
5427match_pcrel_operand (struct mips_arg_info *arg)
a1d78564 5428{
a92713e6
RS
5429 bfd_reloc_code_real_type r[3];
5430
5431 return match_expression (arg, &offset_expr, r) && r[0] == BFD_RELOC_UNUSED;
a1d78564
RS
5432}
5433
5434/* OP_PERF_REG matcher. */
5435
a92713e6 5436static bfd_boolean
a1d78564 5437match_perf_reg_operand (struct mips_arg_info *arg,
a92713e6 5438 const struct mips_operand *operand)
a1d78564
RS
5439{
5440 offsetT sval;
5441
1a00e612 5442 if (!match_const_int (arg, &sval))
a92713e6 5443 return FALSE;
a1d78564
RS
5444
5445 if (sval != 0
5446 && (sval != 1
5447 || (mips_opts.arch == CPU_R5900
5448 && (strcmp (arg->insn->insn_mo->name, "mfps") == 0
5449 || strcmp (arg->insn->insn_mo->name, "mtps") == 0))))
5450 {
1a00e612
RS
5451 set_insn_error (arg->argnum, _("invalid performance register"));
5452 return FALSE;
a1d78564
RS
5453 }
5454
5455 insn_insert_operand (arg->insn, operand, sval);
a92713e6 5456 return TRUE;
a1d78564
RS
5457}
5458
5459/* OP_ADDIUSP matcher. */
5460
a92713e6 5461static bfd_boolean
a1d78564 5462match_addiusp_operand (struct mips_arg_info *arg,
a92713e6 5463 const struct mips_operand *operand)
a1d78564
RS
5464{
5465 offsetT sval;
5466 unsigned int uval;
5467
1a00e612 5468 if (!match_const_int (arg, &sval))
a92713e6 5469 return FALSE;
a1d78564
RS
5470
5471 if (sval % 4)
1a00e612
RS
5472 {
5473 match_out_of_range (arg);
5474 return FALSE;
5475 }
a1d78564
RS
5476
5477 sval /= 4;
5478 if (!(sval >= -258 && sval <= 257) || (sval >= -2 && sval <= 1))
1a00e612
RS
5479 {
5480 match_out_of_range (arg);
5481 return FALSE;
5482 }
a1d78564
RS
5483
5484 uval = (unsigned int) sval;
5485 uval = ((uval >> 1) & ~0xff) | (uval & 0xff);
5486 insn_insert_operand (arg->insn, operand, uval);
a92713e6 5487 return TRUE;
a1d78564
RS
5488}
5489
5490/* OP_CLO_CLZ_DEST matcher. */
5491
a92713e6 5492static bfd_boolean
a1d78564 5493match_clo_clz_dest_operand (struct mips_arg_info *arg,
a92713e6 5494 const struct mips_operand *operand)
a1d78564
RS
5495{
5496 unsigned int regno;
5497
a92713e6
RS
5498 if (!match_reg (arg, OP_REG_GP, &regno))
5499 return FALSE;
a1d78564 5500
a1d78564 5501 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
a92713e6 5502 return TRUE;
a1d78564
RS
5503}
5504
7361da2c
AB
5505/* OP_CHECK_PREV matcher. */
5506
5507static bfd_boolean
5508match_check_prev_operand (struct mips_arg_info *arg,
5509 const struct mips_operand *operand_base)
5510{
5511 const struct mips_check_prev_operand *operand;
5512 unsigned int regno;
5513
5514 operand = (const struct mips_check_prev_operand *) operand_base;
5515
5516 if (!match_reg (arg, OP_REG_GP, &regno))
5517 return FALSE;
5518
5519 if (!operand->zero_ok && regno == 0)
5520 return FALSE;
5521
5522 if ((operand->less_than_ok && regno < arg->last_regno)
5523 || (operand->greater_than_ok && regno > arg->last_regno)
5524 || (operand->equal_ok && regno == arg->last_regno))
5525 {
5526 arg->last_regno = regno;
5527 insn_insert_operand (arg->insn, operand_base, regno);
5528 return TRUE;
5529 }
5530
5531 return FALSE;
5532}
5533
5534/* OP_SAME_RS_RT matcher. */
5535
5536static bfd_boolean
5537match_same_rs_rt_operand (struct mips_arg_info *arg,
5538 const struct mips_operand *operand)
5539{
5540 unsigned int regno;
5541
5542 if (!match_reg (arg, OP_REG_GP, &regno))
5543 return FALSE;
5544
5545 if (regno == 0)
5546 {
5547 set_insn_error (arg->argnum, _("the source register must not be $0"));
5548 return FALSE;
5549 }
5550
5551 arg->last_regno = regno;
5552
5553 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5554 return TRUE;
5555}
5556
a1d78564
RS
5557/* OP_LWM_SWM_LIST matcher. */
5558
a92713e6 5559static bfd_boolean
a1d78564 5560match_lwm_swm_list_operand (struct mips_arg_info *arg,
a92713e6 5561 const struct mips_operand *operand)
a1d78564 5562{
a92713e6
RS
5563 unsigned int reglist, sregs, ra, regno1, regno2;
5564 struct mips_arg_info reset;
a1d78564 5565
a92713e6
RS
5566 reglist = 0;
5567 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5568 return FALSE;
5569 do
5570 {
5571 if (regno2 == FP && regno1 >= S0 && regno1 <= S7)
5572 {
5573 reglist |= 1 << FP;
5574 regno2 = S7;
5575 }
5576 reglist |= ((1U << regno2 << 1) - 1) & -(1U << regno1);
5577 reset = *arg;
5578 }
5579 while (match_char (arg, ',')
5580 && match_reg_range (arg, OP_REG_GP, &regno1, &regno2));
5581 *arg = reset;
a1d78564
RS
5582
5583 if (operand->size == 2)
5584 {
5585 /* The list must include both ra and s0-sN, for 0 <= N <= 3. E.g.:
5586
5587 s0, ra
5588 s0, s1, ra, s2, s3
5589 s0-s2, ra
5590
5591 and any permutations of these. */
5592 if ((reglist & 0xfff1ffff) != 0x80010000)
a92713e6 5593 return FALSE;
a1d78564
RS
5594
5595 sregs = (reglist >> 17) & 7;
5596 ra = 0;
5597 }
5598 else
5599 {
5600 /* The list must include at least one of ra and s0-sN,
5601 for 0 <= N <= 8. (Note that there is a gap between s7 and s8,
5602 which are $23 and $30 respectively.) E.g.:
5603
5604 ra
5605 s0
5606 ra, s0, s1, s2
5607 s0-s8
5608 s0-s5, ra
5609
5610 and any permutations of these. */
5611 if ((reglist & 0x3f00ffff) != 0)
a92713e6 5612 return FALSE;
a1d78564
RS
5613
5614 ra = (reglist >> 27) & 0x10;
5615 sregs = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
5616 }
5617 sregs += 1;
5618 if ((sregs & -sregs) != sregs)
a92713e6 5619 return FALSE;
a1d78564
RS
5620
5621 insn_insert_operand (arg->insn, operand, (ffs (sregs) - 1) | ra);
a92713e6 5622 return TRUE;
a1d78564
RS
5623}
5624
364215c8
RS
5625/* OP_ENTRY_EXIT_LIST matcher. */
5626
a92713e6 5627static unsigned int
364215c8 5628match_entry_exit_operand (struct mips_arg_info *arg,
a92713e6 5629 const struct mips_operand *operand)
364215c8
RS
5630{
5631 unsigned int mask;
5632 bfd_boolean is_exit;
5633
5634 /* The format is the same for both ENTRY and EXIT, but the constraints
5635 are different. */
5636 is_exit = strcmp (arg->insn->insn_mo->name, "exit") == 0;
5637 mask = (is_exit ? 7 << 3 : 0);
a92713e6 5638 do
364215c8
RS
5639 {
5640 unsigned int regno1, regno2;
5641 bfd_boolean is_freg;
5642
a92713e6 5643 if (match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
364215c8 5644 is_freg = FALSE;
a92713e6 5645 else if (match_reg_range (arg, OP_REG_FP, &regno1, &regno2))
364215c8
RS
5646 is_freg = TRUE;
5647 else
a92713e6 5648 return FALSE;
364215c8
RS
5649
5650 if (is_exit && is_freg && regno1 == 0 && regno2 < 2)
5651 {
5652 mask &= ~(7 << 3);
5653 mask |= (5 + regno2) << 3;
5654 }
5655 else if (!is_exit && regno1 == 4 && regno2 >= 4 && regno2 <= 7)
5656 mask |= (regno2 - 3) << 3;
5657 else if (regno1 == 16 && regno2 >= 16 && regno2 <= 17)
5658 mask |= (regno2 - 15) << 1;
5659 else if (regno1 == RA && regno2 == RA)
5660 mask |= 1;
5661 else
a92713e6 5662 return FALSE;
364215c8 5663 }
a92713e6
RS
5664 while (match_char (arg, ','));
5665
364215c8 5666 insn_insert_operand (arg->insn, operand, mask);
a92713e6 5667 return TRUE;
364215c8
RS
5668}
5669
38bf472a
MR
5670/* Encode regular MIPS SAVE/RESTORE instruction operands according to
5671 the argument register mask AMASK, the number of static registers
5672 saved NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5673 respectively, and the frame size FRAME_SIZE. */
5674
5675static unsigned int
5676mips_encode_save_restore (unsigned int amask, unsigned int nsreg,
5677 unsigned int ra, unsigned int s0, unsigned int s1,
5678 unsigned int frame_size)
5679{
5680 return ((nsreg << 23) | ((frame_size & 0xf0) << 15) | (amask << 15)
5681 | (ra << 12) | (s0 << 11) | (s1 << 10) | ((frame_size & 0xf) << 6));
5682}
5683
5684/* Encode MIPS16 SAVE/RESTORE instruction operands according to the
5685 argument register mask AMASK, the number of static registers saved
5686 NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5687 respectively, and the frame size FRAME_SIZE. */
5688
5689static unsigned int
5690mips16_encode_save_restore (unsigned int amask, unsigned int nsreg,
5691 unsigned int ra, unsigned int s0, unsigned int s1,
5692 unsigned int frame_size)
5693{
5694 unsigned int args;
5695
5696 args = (ra << 6) | (s0 << 5) | (s1 << 4) | (frame_size & 0xf);
5697 if (nsreg || amask || frame_size == 0 || frame_size > 16)
5698 args |= (MIPS16_EXTEND | (nsreg << 24) | (amask << 16)
5699 | ((frame_size & 0xf0) << 16));
5700 return args;
5701}
5702
364215c8
RS
5703/* OP_SAVE_RESTORE_LIST matcher. */
5704
a92713e6
RS
5705static bfd_boolean
5706match_save_restore_list_operand (struct mips_arg_info *arg)
364215c8
RS
5707{
5708 unsigned int opcode, args, statics, sregs;
5709 unsigned int num_frame_sizes, num_args, num_statics, num_sregs;
38bf472a 5710 unsigned int arg_mask, ra, s0, s1;
364215c8 5711 offsetT frame_size;
364215c8 5712
364215c8
RS
5713 opcode = arg->insn->insn_opcode;
5714 frame_size = 0;
5715 num_frame_sizes = 0;
5716 args = 0;
5717 statics = 0;
5718 sregs = 0;
38bf472a
MR
5719 ra = 0;
5720 s0 = 0;
5721 s1 = 0;
a92713e6 5722 do
364215c8
RS
5723 {
5724 unsigned int regno1, regno2;
5725
a92713e6 5726 if (arg->token->type == OT_INTEGER)
364215c8
RS
5727 {
5728 /* Handle the frame size. */
1a00e612 5729 if (!match_const_int (arg, &frame_size))
a92713e6 5730 return FALSE;
364215c8 5731 num_frame_sizes += 1;
364215c8
RS
5732 }
5733 else
5734 {
a92713e6
RS
5735 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5736 return FALSE;
364215c8
RS
5737
5738 while (regno1 <= regno2)
5739 {
5740 if (regno1 >= 4 && regno1 <= 7)
5741 {
5742 if (num_frame_sizes == 0)
5743 /* args $a0-$a3 */
5744 args |= 1 << (regno1 - 4);
5745 else
5746 /* statics $a0-$a3 */
5747 statics |= 1 << (regno1 - 4);
5748 }
5749 else if (regno1 >= 16 && regno1 <= 23)
5750 /* $s0-$s7 */
5751 sregs |= 1 << (regno1 - 16);
5752 else if (regno1 == 30)
5753 /* $s8 */
5754 sregs |= 1 << 8;
5755 else if (regno1 == 31)
5756 /* Add $ra to insn. */
38bf472a 5757 ra = 1;
364215c8 5758 else
a92713e6 5759 return FALSE;
364215c8
RS
5760 regno1 += 1;
5761 if (regno1 == 24)
5762 regno1 = 30;
5763 }
5764 }
364215c8 5765 }
a92713e6 5766 while (match_char (arg, ','));
364215c8
RS
5767
5768 /* Encode args/statics combination. */
5769 if (args & statics)
a92713e6 5770 return FALSE;
364215c8
RS
5771 else if (args == 0xf)
5772 /* All $a0-$a3 are args. */
38bf472a 5773 arg_mask = MIPS_SVRS_ALL_ARGS;
364215c8
RS
5774 else if (statics == 0xf)
5775 /* All $a0-$a3 are statics. */
38bf472a 5776 arg_mask = MIPS_SVRS_ALL_STATICS;
364215c8
RS
5777 else
5778 {
5779 /* Count arg registers. */
5780 num_args = 0;
5781 while (args & 0x1)
5782 {
5783 args >>= 1;
5784 num_args += 1;
5785 }
5786 if (args != 0)
a92713e6 5787 return FALSE;
364215c8
RS
5788
5789 /* Count static registers. */
5790 num_statics = 0;
5791 while (statics & 0x8)
5792 {
5793 statics = (statics << 1) & 0xf;
5794 num_statics += 1;
5795 }
5796 if (statics != 0)
a92713e6 5797 return FALSE;
364215c8
RS
5798
5799 /* Encode args/statics. */
38bf472a 5800 arg_mask = (num_args << 2) | num_statics;
364215c8
RS
5801 }
5802
5803 /* Encode $s0/$s1. */
5804 if (sregs & (1 << 0)) /* $s0 */
38bf472a 5805 s0 = 1;
364215c8 5806 if (sregs & (1 << 1)) /* $s1 */
38bf472a 5807 s1 = 1;
364215c8
RS
5808 sregs >>= 2;
5809
5810 /* Encode $s2-$s8. */
5811 num_sregs = 0;
5812 while (sregs & 1)
5813 {
5814 sregs >>= 1;
5815 num_sregs += 1;
5816 }
5817 if (sregs != 0)
a92713e6 5818 return FALSE;
364215c8
RS
5819
5820 /* Encode frame size. */
5821 if (num_frame_sizes == 0)
1a00e612
RS
5822 {
5823 set_insn_error (arg->argnum, _("missing frame size"));
5824 return FALSE;
5825 }
5826 if (num_frame_sizes > 1)
5827 {
5828 set_insn_error (arg->argnum, _("frame size specified twice"));
5829 return FALSE;
5830 }
5831 if ((frame_size & 7) != 0 || frame_size < 0 || frame_size > 0xff * 8)
5832 {
5833 set_insn_error (arg->argnum, _("invalid frame size"));
5834 return FALSE;
5835 }
38bf472a 5836 frame_size /= 8;
364215c8 5837
364215c8 5838 /* Finally build the instruction. */
38bf472a
MR
5839 if (mips_opts.mips16)
5840 opcode |= mips16_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5841 frame_size);
5842 else if (!mips_opts.micromips)
5843 opcode |= mips_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5844 frame_size);
5845 else
5846 abort ();
5847
364215c8 5848 arg->insn->insn_opcode = opcode;
a92713e6 5849 return TRUE;
364215c8
RS
5850}
5851
a1d78564
RS
5852/* OP_MDMX_IMM_REG matcher. */
5853
a92713e6 5854static bfd_boolean
a1d78564 5855match_mdmx_imm_reg_operand (struct mips_arg_info *arg,
a92713e6 5856 const struct mips_operand *operand)
a1d78564 5857{
a92713e6 5858 unsigned int regno, uval;
a1d78564
RS
5859 bfd_boolean is_qh;
5860 const struct mips_opcode *opcode;
5861
5862 /* The mips_opcode records whether this is an octobyte or quadhalf
5863 instruction. Start out with that bit in place. */
5864 opcode = arg->insn->insn_mo;
5865 uval = mips_extract_operand (operand, opcode->match);
5866 is_qh = (uval != 0);
5867
56d438b1 5868 if (arg->token->type == OT_REG)
a1d78564
RS
5869 {
5870 if ((opcode->membership & INSN_5400)
5871 && strcmp (opcode->name, "rzu.ob") == 0)
5872 {
1a00e612
RS
5873 set_insn_error_i (arg->argnum, _("operand %d must be an immediate"),
5874 arg->argnum);
5875 return FALSE;
a1d78564
RS
5876 }
5877
56d438b1
CF
5878 if (!match_regno (arg, OP_REG_VEC, arg->token->u.regno, &regno))
5879 return FALSE;
5880 ++arg->token;
5881
a1d78564
RS
5882 /* Check whether this is a vector register or a broadcast of
5883 a single element. */
56d438b1 5884 if (arg->token->type == OT_INTEGER_INDEX)
a1d78564 5885 {
56d438b1 5886 if (arg->token->u.index > (is_qh ? 3 : 7))
a1d78564 5887 {
1a00e612
RS
5888 set_insn_error (arg->argnum, _("invalid element selector"));
5889 return FALSE;
a1d78564 5890 }
56d438b1
CF
5891 uval |= arg->token->u.index << (is_qh ? 2 : 1) << 5;
5892 ++arg->token;
a1d78564
RS
5893 }
5894 else
5895 {
5896 /* A full vector. */
5897 if ((opcode->membership & INSN_5400)
5898 && (strcmp (opcode->name, "sll.ob") == 0
5899 || strcmp (opcode->name, "srl.ob") == 0))
5900 {
1a00e612
RS
5901 set_insn_error_i (arg->argnum, _("operand %d must be scalar"),
5902 arg->argnum);
5903 return FALSE;
a1d78564
RS
5904 }
5905
5906 if (is_qh)
5907 uval |= MDMX_FMTSEL_VEC_QH << 5;
5908 else
5909 uval |= MDMX_FMTSEL_VEC_OB << 5;
5910 }
a1d78564
RS
5911 uval |= regno;
5912 }
5913 else
5914 {
5915 offsetT sval;
5916
1a00e612 5917 if (!match_const_int (arg, &sval))
a92713e6 5918 return FALSE;
a1d78564
RS
5919 if (sval < 0 || sval > 31)
5920 {
1a00e612
RS
5921 match_out_of_range (arg);
5922 return FALSE;
a1d78564
RS
5923 }
5924 uval |= (sval & 31);
5925 if (is_qh)
5926 uval |= MDMX_FMTSEL_IMM_QH << 5;
5927 else
5928 uval |= MDMX_FMTSEL_IMM_OB << 5;
5929 }
5930 insn_insert_operand (arg->insn, operand, uval);
a92713e6 5931 return TRUE;
a1d78564
RS
5932}
5933
56d438b1
CF
5934/* OP_IMM_INDEX matcher. */
5935
5936static bfd_boolean
5937match_imm_index_operand (struct mips_arg_info *arg,
5938 const struct mips_operand *operand)
5939{
5940 unsigned int max_val;
5941
5942 if (arg->token->type != OT_INTEGER_INDEX)
5943 return FALSE;
5944
5945 max_val = (1 << operand->size) - 1;
5946 if (arg->token->u.index > max_val)
5947 {
5948 match_out_of_range (arg);
5949 return FALSE;
5950 }
5951 insn_insert_operand (arg->insn, operand, arg->token->u.index);
5952 ++arg->token;
5953 return TRUE;
5954}
5955
5956/* OP_REG_INDEX matcher. */
5957
5958static bfd_boolean
5959match_reg_index_operand (struct mips_arg_info *arg,
5960 const struct mips_operand *operand)
5961{
5962 unsigned int regno;
5963
5964 if (arg->token->type != OT_REG_INDEX)
5965 return FALSE;
5966
5967 if (!match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno))
5968 return FALSE;
5969
5970 insn_insert_operand (arg->insn, operand, regno);
5971 ++arg->token;
5972 return TRUE;
5973}
5974
a1d78564
RS
5975/* OP_PC matcher. */
5976
a92713e6
RS
5977static bfd_boolean
5978match_pc_operand (struct mips_arg_info *arg)
a1d78564 5979{
a92713e6
RS
5980 if (arg->token->type == OT_REG && (arg->token->u.regno & RTYPE_PC))
5981 {
5982 ++arg->token;
5983 return TRUE;
5984 }
5985 return FALSE;
a1d78564
RS
5986}
5987
25499ac7
MR
5988/* OP_REG28 matcher. */
5989
5990static bfd_boolean
5991match_reg28_operand (struct mips_arg_info *arg)
5992{
5993 unsigned int regno;
5994
5995 if (arg->token->type == OT_REG
5996 && match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno)
5997 && regno == GP)
5998 {
5999 ++arg->token;
6000 return TRUE;
6001 }
6002 return FALSE;
6003}
6004
7361da2c
AB
6005/* OP_NON_ZERO_REG matcher. */
6006
6007static bfd_boolean
6008match_non_zero_reg_operand (struct mips_arg_info *arg,
6009 const struct mips_operand *operand)
6010{
6011 unsigned int regno;
6012
6013 if (!match_reg (arg, OP_REG_GP, &regno))
6014 return FALSE;
6015
6016 if (regno == 0)
6017 return FALSE;
6018
6019 arg->last_regno = regno;
6020 insn_insert_operand (arg->insn, operand, regno);
6021 return TRUE;
6022}
6023
a1d78564
RS
6024/* OP_REPEAT_DEST_REG and OP_REPEAT_PREV_REG matcher. OTHER_REGNO is the
6025 register that we need to match. */
6026
a92713e6
RS
6027static bfd_boolean
6028match_tied_reg_operand (struct mips_arg_info *arg, unsigned int other_regno)
a1d78564
RS
6029{
6030 unsigned int regno;
6031
a92713e6 6032 return match_reg (arg, OP_REG_GP, &regno) && regno == other_regno;
a1d78564
RS
6033}
6034
33f46696
MR
6035/* Try to match a floating-point constant from ARG for LI.S or LI.D.
6036 LENGTH is the length of the value in bytes (4 for float, 8 for double)
6037 and USING_GPRS says whether the destination is a GPR rather than an FPR.
89565f1b
RS
6038
6039 Return the constant in IMM and OFFSET as follows:
6040
6041 - If the constant should be loaded via memory, set IMM to O_absent and
6042 OFFSET to the memory address.
6043
6044 - Otherwise, if the constant should be loaded into two 32-bit registers,
6045 set IMM to the O_constant to load into the high register and OFFSET
6046 to the corresponding value for the low register.
6047
6048 - Otherwise, set IMM to the full O_constant and set OFFSET to O_absent.
6049
6050 These constants only appear as the last operand in an instruction,
6051 and every instruction that accepts them in any variant accepts them
6052 in all variants. This means we don't have to worry about backing out
6053 any changes if the instruction does not match. We just match
6054 unconditionally and report an error if the constant is invalid. */
6055
a92713e6
RS
6056static bfd_boolean
6057match_float_constant (struct mips_arg_info *arg, expressionS *imm,
6058 expressionS *offset, int length, bfd_boolean using_gprs)
89565f1b 6059{
a92713e6 6060 char *p;
89565f1b
RS
6061 segT seg, new_seg;
6062 subsegT subseg;
6063 const char *newname;
a92713e6 6064 unsigned char *data;
89565f1b
RS
6065
6066 /* Where the constant is placed is based on how the MIPS assembler
6067 does things:
6068
6069 length == 4 && using_gprs -- immediate value only
6070 length == 8 && using_gprs -- .rdata or immediate value
6071 length == 4 && !using_gprs -- .lit4 or immediate value
6072 length == 8 && !using_gprs -- .lit8 or immediate value
6073
6074 The .lit4 and .lit8 sections are only used if permitted by the
6075 -G argument. */
a92713e6 6076 if (arg->token->type != OT_FLOAT)
1a00e612
RS
6077 {
6078 set_insn_error (arg->argnum, _("floating-point expression required"));
6079 return FALSE;
6080 }
a92713e6
RS
6081
6082 gas_assert (arg->token->u.flt.length == length);
6083 data = arg->token->u.flt.data;
6084 ++arg->token;
89565f1b
RS
6085
6086 /* Handle 32-bit constants for which an immediate value is best. */
6087 if (length == 4
6088 && (using_gprs
6089 || g_switch_value < 4
6090 || (data[0] == 0 && data[1] == 0)
6091 || (data[2] == 0 && data[3] == 0)))
6092 {
6093 imm->X_op = O_constant;
6094 if (!target_big_endian)
6095 imm->X_add_number = bfd_getl32 (data);
6096 else
6097 imm->X_add_number = bfd_getb32 (data);
6098 offset->X_op = O_absent;
a92713e6 6099 return TRUE;
89565f1b
RS
6100 }
6101
6102 /* Handle 64-bit constants for which an immediate value is best. */
6103 if (length == 8
6104 && !mips_disable_float_construction
351cdf24
MF
6105 /* Constants can only be constructed in GPRs and copied to FPRs if the
6106 GPRs are at least as wide as the FPRs or MTHC1 is available.
6107 Unlike most tests for 32-bit floating-point registers this check
6108 specifically looks for GPR_SIZE == 32 as the FPXX ABI does not
6109 permit 64-bit moves without MXHC1.
6110 Force the constant into memory otherwise. */
6111 && (using_gprs
6112 || GPR_SIZE == 64
6113 || ISA_HAS_MXHC1 (mips_opts.isa)
6114 || FPR_SIZE == 32)
89565f1b
RS
6115 && ((data[0] == 0 && data[1] == 0)
6116 || (data[2] == 0 && data[3] == 0))
6117 && ((data[4] == 0 && data[5] == 0)
6118 || (data[6] == 0 && data[7] == 0)))
6119 {
6120 /* The value is simple enough to load with a couple of instructions.
6121 If using 32-bit registers, set IMM to the high order 32 bits and
6122 OFFSET to the low order 32 bits. Otherwise, set IMM to the entire
6123 64 bit constant. */
351cdf24 6124 if (GPR_SIZE == 32 || (!using_gprs && FPR_SIZE != 64))
89565f1b
RS
6125 {
6126 imm->X_op = O_constant;
6127 offset->X_op = O_constant;
6128 if (!target_big_endian)
6129 {
6130 imm->X_add_number = bfd_getl32 (data + 4);
6131 offset->X_add_number = bfd_getl32 (data);
6132 }
6133 else
6134 {
6135 imm->X_add_number = bfd_getb32 (data);
6136 offset->X_add_number = bfd_getb32 (data + 4);
6137 }
6138 if (offset->X_add_number == 0)
6139 offset->X_op = O_absent;
6140 }
6141 else
6142 {
6143 imm->X_op = O_constant;
6144 if (!target_big_endian)
6145 imm->X_add_number = bfd_getl64 (data);
6146 else
6147 imm->X_add_number = bfd_getb64 (data);
6148 offset->X_op = O_absent;
6149 }
a92713e6 6150 return TRUE;
89565f1b
RS
6151 }
6152
6153 /* Switch to the right section. */
6154 seg = now_seg;
6155 subseg = now_subseg;
6156 if (length == 4)
6157 {
6158 gas_assert (!using_gprs && g_switch_value >= 4);
6159 newname = ".lit4";
6160 }
6161 else
6162 {
6163 if (using_gprs || g_switch_value < 8)
6164 newname = RDATA_SECTION_NAME;
6165 else
6166 newname = ".lit8";
6167 }
6168
6169 new_seg = subseg_new (newname, (subsegT) 0);
6170 bfd_set_section_flags (stdoutput, new_seg,
6171 SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA);
6172 frag_align (length == 4 ? 2 : 3, 0, 0);
6173 if (strncmp (TARGET_OS, "elf", 3) != 0)
6174 record_alignment (new_seg, 4);
6175 else
6176 record_alignment (new_seg, length == 4 ? 2 : 3);
6177 if (seg == now_seg)
1661c76c 6178 as_bad (_("cannot use `%s' in this section"), arg->insn->insn_mo->name);
89565f1b
RS
6179
6180 /* Set the argument to the current address in the section. */
6181 imm->X_op = O_absent;
6182 offset->X_op = O_symbol;
6183 offset->X_add_symbol = symbol_temp_new_now ();
6184 offset->X_add_number = 0;
6185
6186 /* Put the floating point number into the section. */
6187 p = frag_more (length);
6188 memcpy (p, data, length);
6189
6190 /* Switch back to the original section. */
6191 subseg_set (seg, subseg);
a92713e6 6192 return TRUE;
89565f1b
RS
6193}
6194
14daeee3
RS
6195/* OP_VU0_SUFFIX and OP_VU0_MATCH_SUFFIX matcher; MATCH_P selects between
6196 them. */
6197
6198static bfd_boolean
6199match_vu0_suffix_operand (struct mips_arg_info *arg,
6200 const struct mips_operand *operand,
6201 bfd_boolean match_p)
6202{
6203 unsigned int uval;
6204
6205 /* The operand can be an XYZW mask or a single 2-bit channel index
6206 (with X being 0). */
6207 gas_assert (operand->size == 2 || operand->size == 4);
6208
ee5734f0 6209 /* The suffix can be omitted when it is already part of the opcode. */
14daeee3 6210 if (arg->token->type != OT_CHANNELS)
ee5734f0 6211 return match_p;
14daeee3
RS
6212
6213 uval = arg->token->u.channels;
6214 if (operand->size == 2)
6215 {
6216 /* Check that a single bit is set and convert it into a 2-bit index. */
6217 if ((uval & -uval) != uval)
6218 return FALSE;
6219 uval = 4 - ffs (uval);
6220 }
6221
6222 if (match_p && insn_extract_operand (arg->insn, operand) != uval)
6223 return FALSE;
6224
6225 ++arg->token;
6226 if (!match_p)
6227 insn_insert_operand (arg->insn, operand, uval);
6228 return TRUE;
6229}
6230
33f46696
MR
6231/* Try to match a token from ARG against OPERAND. Consume the token
6232 and return true on success, otherwise return false. */
a1d78564 6233
a92713e6 6234static bfd_boolean
a1d78564 6235match_operand (struct mips_arg_info *arg,
a92713e6 6236 const struct mips_operand *operand)
a1d78564
RS
6237{
6238 switch (operand->type)
6239 {
6240 case OP_INT:
a92713e6 6241 return match_int_operand (arg, operand);
a1d78564
RS
6242
6243 case OP_MAPPED_INT:
a92713e6 6244 return match_mapped_int_operand (arg, operand);
a1d78564
RS
6245
6246 case OP_MSB:
a92713e6 6247 return match_msb_operand (arg, operand);
a1d78564
RS
6248
6249 case OP_REG:
0f35dbc4 6250 case OP_OPTIONAL_REG:
a92713e6 6251 return match_reg_operand (arg, operand);
a1d78564
RS
6252
6253 case OP_REG_PAIR:
a92713e6 6254 return match_reg_pair_operand (arg, operand);
a1d78564
RS
6255
6256 case OP_PCREL:
a92713e6 6257 return match_pcrel_operand (arg);
a1d78564
RS
6258
6259 case OP_PERF_REG:
a92713e6 6260 return match_perf_reg_operand (arg, operand);
a1d78564
RS
6261
6262 case OP_ADDIUSP_INT:
a92713e6 6263 return match_addiusp_operand (arg, operand);
a1d78564
RS
6264
6265 case OP_CLO_CLZ_DEST:
a92713e6 6266 return match_clo_clz_dest_operand (arg, operand);
a1d78564
RS
6267
6268 case OP_LWM_SWM_LIST:
a92713e6 6269 return match_lwm_swm_list_operand (arg, operand);
a1d78564
RS
6270
6271 case OP_ENTRY_EXIT_LIST:
a92713e6 6272 return match_entry_exit_operand (arg, operand);
364215c8 6273
a1d78564 6274 case OP_SAVE_RESTORE_LIST:
a92713e6 6275 return match_save_restore_list_operand (arg);
a1d78564
RS
6276
6277 case OP_MDMX_IMM_REG:
a92713e6 6278 return match_mdmx_imm_reg_operand (arg, operand);
a1d78564
RS
6279
6280 case OP_REPEAT_DEST_REG:
a92713e6 6281 return match_tied_reg_operand (arg, arg->dest_regno);
a1d78564
RS
6282
6283 case OP_REPEAT_PREV_REG:
a92713e6 6284 return match_tied_reg_operand (arg, arg->last_regno);
a1d78564
RS
6285
6286 case OP_PC:
a92713e6 6287 return match_pc_operand (arg);
14daeee3 6288
25499ac7
MR
6289 case OP_REG28:
6290 return match_reg28_operand (arg);
6291
14daeee3
RS
6292 case OP_VU0_SUFFIX:
6293 return match_vu0_suffix_operand (arg, operand, FALSE);
6294
6295 case OP_VU0_MATCH_SUFFIX:
6296 return match_vu0_suffix_operand (arg, operand, TRUE);
56d438b1
CF
6297
6298 case OP_IMM_INDEX:
6299 return match_imm_index_operand (arg, operand);
6300
6301 case OP_REG_INDEX:
6302 return match_reg_index_operand (arg, operand);
7361da2c
AB
6303
6304 case OP_SAME_RS_RT:
6305 return match_same_rs_rt_operand (arg, operand);
6306
6307 case OP_CHECK_PREV:
6308 return match_check_prev_operand (arg, operand);
6309
6310 case OP_NON_ZERO_REG:
6311 return match_non_zero_reg_operand (arg, operand);
a1d78564
RS
6312 }
6313 abort ();
6314}
6315
6316/* ARG is the state after successfully matching an instruction.
6317 Issue any queued-up warnings. */
6318
6319static void
6320check_completed_insn (struct mips_arg_info *arg)
6321{
6322 if (arg->seen_at)
6323 {
6324 if (AT == ATREG)
1661c76c 6325 as_warn (_("used $at without \".set noat\""));
a1d78564 6326 else
1661c76c 6327 as_warn (_("used $%u with \".set at=$%u\""), AT, AT);
a1d78564
RS
6328 }
6329}
a1d78564 6330
85fcb30f
RS
6331/* Return true if modifying general-purpose register REG needs a delay. */
6332
6333static bfd_boolean
6334reg_needs_delay (unsigned int reg)
6335{
6336 unsigned long prev_pinfo;
6337
6338 prev_pinfo = history[0].insn_mo->pinfo;
6339 if (!mips_opts.noreorder
67dc82bc 6340 && (((prev_pinfo & INSN_LOAD_MEMORY) && !gpr_interlocks)
43885403 6341 || ((prev_pinfo & INSN_LOAD_COPROC) && !cop_interlocks))
85fcb30f
RS
6342 && (gpr_write_mask (&history[0]) & (1 << reg)))
6343 return TRUE;
6344
6345 return FALSE;
6346}
6347
71400594
RS
6348/* Classify an instruction according to the FIX_VR4120_* enumeration.
6349 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
6350 by VR4120 errata. */
4d7206a2 6351
71400594
RS
6352static unsigned int
6353classify_vr4120_insn (const char *name)
252b5132 6354{
71400594
RS
6355 if (strncmp (name, "macc", 4) == 0)
6356 return FIX_VR4120_MACC;
6357 if (strncmp (name, "dmacc", 5) == 0)
6358 return FIX_VR4120_DMACC;
6359 if (strncmp (name, "mult", 4) == 0)
6360 return FIX_VR4120_MULT;
6361 if (strncmp (name, "dmult", 5) == 0)
6362 return FIX_VR4120_DMULT;
6363 if (strstr (name, "div"))
6364 return FIX_VR4120_DIV;
6365 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
6366 return FIX_VR4120_MTHILO;
6367 return NUM_FIX_VR4120_CLASSES;
6368}
252b5132 6369
a8d14a88
CM
6370#define INSN_ERET 0x42000018
6371#define INSN_DERET 0x4200001f
6372#define INSN_DMULT 0x1c
6373#define INSN_DMULTU 0x1d
ff239038 6374
71400594
RS
6375/* Return the number of instructions that must separate INSN1 and INSN2,
6376 where INSN1 is the earlier instruction. Return the worst-case value
6377 for any INSN2 if INSN2 is null. */
252b5132 6378
71400594
RS
6379static unsigned int
6380insns_between (const struct mips_cl_insn *insn1,
6381 const struct mips_cl_insn *insn2)
6382{
6383 unsigned long pinfo1, pinfo2;
4c260379 6384 unsigned int mask;
71400594 6385
85fcb30f
RS
6386 /* If INFO2 is null, pessimistically assume that all flags are set for
6387 the second instruction. */
71400594
RS
6388 pinfo1 = insn1->insn_mo->pinfo;
6389 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
252b5132 6390
71400594
RS
6391 /* For most targets, write-after-read dependencies on the HI and LO
6392 registers must be separated by at least two instructions. */
6393 if (!hilo_interlocks)
252b5132 6394 {
71400594
RS
6395 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
6396 return 2;
6397 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
6398 return 2;
6399 }
6400
6401 /* If we're working around r7000 errata, there must be two instructions
6402 between an mfhi or mflo and any instruction that uses the result. */
6403 if (mips_7000_hilo_fix
df58fc94 6404 && !mips_opts.micromips
71400594 6405 && MF_HILO_INSN (pinfo1)
85fcb30f 6406 && (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1))))
71400594
RS
6407 return 2;
6408
ff239038
CM
6409 /* If we're working around 24K errata, one instruction is required
6410 if an ERET or DERET is followed by a branch instruction. */
df58fc94 6411 if (mips_fix_24k && !mips_opts.micromips)
ff239038
CM
6412 {
6413 if (insn1->insn_opcode == INSN_ERET
6414 || insn1->insn_opcode == INSN_DERET)
6415 {
6416 if (insn2 == NULL
6417 || insn2->insn_opcode == INSN_ERET
6418 || insn2->insn_opcode == INSN_DERET
11625dd8 6419 || delayed_branch_p (insn2))
ff239038
CM
6420 return 1;
6421 }
6422 }
6423
a8d14a88
CM
6424 /* If we're working around PMC RM7000 errata, there must be three
6425 nops between a dmult and a load instruction. */
6426 if (mips_fix_rm7000 && !mips_opts.micromips)
6427 {
6428 if ((insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULT
6429 || (insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULTU)
6430 {
6431 if (pinfo2 & INSN_LOAD_MEMORY)
6432 return 3;
6433 }
6434 }
6435
71400594
RS
6436 /* If working around VR4120 errata, check for combinations that need
6437 a single intervening instruction. */
df58fc94 6438 if (mips_fix_vr4120 && !mips_opts.micromips)
71400594
RS
6439 {
6440 unsigned int class1, class2;
252b5132 6441
71400594
RS
6442 class1 = classify_vr4120_insn (insn1->insn_mo->name);
6443 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
252b5132 6444 {
71400594
RS
6445 if (insn2 == NULL)
6446 return 1;
6447 class2 = classify_vr4120_insn (insn2->insn_mo->name);
6448 if (vr4120_conflicts[class1] & (1 << class2))
6449 return 1;
252b5132 6450 }
71400594
RS
6451 }
6452
df58fc94 6453 if (!HAVE_CODE_COMPRESSION)
71400594
RS
6454 {
6455 /* Check for GPR or coprocessor load delays. All such delays
6456 are on the RT register. */
6457 /* Itbl support may require additional care here. */
67dc82bc 6458 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY))
43885403 6459 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC)))
252b5132 6460 {
85fcb30f 6461 if (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1)))
71400594
RS
6462 return 1;
6463 }
6464
6465 /* Check for generic coprocessor hazards.
6466
6467 This case is not handled very well. There is no special
6468 knowledge of CP0 handling, and the coprocessors other than
6469 the floating point unit are not distinguished at all. */
6470 /* Itbl support may require additional care here. FIXME!
6471 Need to modify this to include knowledge about
6472 user specified delays! */
43885403 6473 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE))
71400594
RS
6474 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
6475 {
6476 /* Handle cases where INSN1 writes to a known general coprocessor
6477 register. There must be a one instruction delay before INSN2
6478 if INSN2 reads that register, otherwise no delay is needed. */
4c260379
RS
6479 mask = fpr_write_mask (insn1);
6480 if (mask != 0)
252b5132 6481 {
4c260379 6482 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
71400594 6483 return 1;
252b5132
RH
6484 }
6485 else
6486 {
71400594
RS
6487 /* Read-after-write dependencies on the control registers
6488 require a two-instruction gap. */
6489 if ((pinfo1 & INSN_WRITE_COND_CODE)
6490 && (pinfo2 & INSN_READ_COND_CODE))
6491 return 2;
6492
6493 /* We don't know exactly what INSN1 does. If INSN2 is
6494 also a coprocessor instruction, assume there must be
6495 a one instruction gap. */
6496 if (pinfo2 & INSN_COP)
6497 return 1;
252b5132
RH
6498 }
6499 }
6b76fefe 6500
71400594
RS
6501 /* Check for read-after-write dependencies on the coprocessor
6502 control registers in cases where INSN1 does not need a general
6503 coprocessor delay. This means that INSN1 is a floating point
6504 comparison instruction. */
6505 /* Itbl support may require additional care here. */
6506 else if (!cop_interlocks
6507 && (pinfo1 & INSN_WRITE_COND_CODE)
6508 && (pinfo2 & INSN_READ_COND_CODE))
6509 return 1;
6510 }
6b76fefe 6511
7361da2c
AB
6512 /* Forbidden slots can not contain Control Transfer Instructions (CTIs)
6513 CTIs include all branches and jumps, nal, eret, eretnc, deret, wait
6514 and pause. */
6515 if ((insn1->insn_mo->pinfo2 & INSN2_FORBIDDEN_SLOT)
6516 && ((pinfo2 & INSN_NO_DELAY_SLOT)
6517 || (insn2 && delayed_branch_p (insn2))))
6518 return 1;
6519
71400594
RS
6520 return 0;
6521}
6b76fefe 6522
7d8e00cf
RS
6523/* Return the number of nops that would be needed to work around the
6524 VR4130 mflo/mfhi errata if instruction INSN immediately followed
932d1a1b
RS
6525 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
6526 that are contained within the first IGNORE instructions of HIST. */
7d8e00cf
RS
6527
6528static int
932d1a1b 6529nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
7d8e00cf
RS
6530 const struct mips_cl_insn *insn)
6531{
4c260379
RS
6532 int i, j;
6533 unsigned int mask;
7d8e00cf
RS
6534
6535 /* Check if the instruction writes to HI or LO. MTHI and MTLO
6536 are not affected by the errata. */
6537 if (insn != 0
6538 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
6539 || strcmp (insn->insn_mo->name, "mtlo") == 0
6540 || strcmp (insn->insn_mo->name, "mthi") == 0))
6541 return 0;
6542
6543 /* Search for the first MFLO or MFHI. */
6544 for (i = 0; i < MAX_VR4130_NOPS; i++)
91d6fa6a 6545 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
7d8e00cf
RS
6546 {
6547 /* Extract the destination register. */
4c260379 6548 mask = gpr_write_mask (&hist[i]);
7d8e00cf
RS
6549
6550 /* No nops are needed if INSN reads that register. */
4c260379 6551 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
7d8e00cf
RS
6552 return 0;
6553
6554 /* ...or if any of the intervening instructions do. */
6555 for (j = 0; j < i; j++)
4c260379 6556 if (gpr_read_mask (&hist[j]) & mask)
7d8e00cf
RS
6557 return 0;
6558
932d1a1b
RS
6559 if (i >= ignore)
6560 return MAX_VR4130_NOPS - i;
7d8e00cf
RS
6561 }
6562 return 0;
6563}
6564
134c0c8b
MR
6565#define BASE_REG_EQ(INSN1, INSN2) \
6566 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
15be625d
CM
6567 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
6568
6569/* Return the minimum alignment for this store instruction. */
6570
6571static int
6572fix_24k_align_to (const struct mips_opcode *mo)
6573{
6574 if (strcmp (mo->name, "sh") == 0)
6575 return 2;
6576
6577 if (strcmp (mo->name, "swc1") == 0
6578 || strcmp (mo->name, "swc2") == 0
6579 || strcmp (mo->name, "sw") == 0
6580 || strcmp (mo->name, "sc") == 0
6581 || strcmp (mo->name, "s.s") == 0)
6582 return 4;
6583
6584 if (strcmp (mo->name, "sdc1") == 0
6585 || strcmp (mo->name, "sdc2") == 0
6586 || strcmp (mo->name, "s.d") == 0)
6587 return 8;
6588
6589 /* sb, swl, swr */
6590 return 1;
6591}
6592
6593struct fix_24k_store_info
6594 {
6595 /* Immediate offset, if any, for this store instruction. */
6596 short off;
6597 /* Alignment required by this store instruction. */
6598 int align_to;
6599 /* True for register offsets. */
6600 int register_offset;
6601 };
6602
6603/* Comparison function used by qsort. */
6604
6605static int
6606fix_24k_sort (const void *a, const void *b)
6607{
6608 const struct fix_24k_store_info *pos1 = a;
6609 const struct fix_24k_store_info *pos2 = b;
6610
6611 return (pos1->off - pos2->off);
6612}
6613
6614/* INSN is a store instruction. Try to record the store information
6615 in STINFO. Return false if the information isn't known. */
6616
6617static bfd_boolean
6618fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
ab9794cf 6619 const struct mips_cl_insn *insn)
15be625d
CM
6620{
6621 /* The instruction must have a known offset. */
6622 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
6623 return FALSE;
6624
6625 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
6626 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
6627 return TRUE;
6628}
6629
932d1a1b
RS
6630/* Return the number of nops that would be needed to work around the 24k
6631 "lost data on stores during refill" errata if instruction INSN
6632 immediately followed the 2 instructions described by HIST.
6633 Ignore hazards that are contained within the first IGNORE
6634 instructions of HIST.
6635
6636 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
6637 for the data cache refills and store data. The following describes
6638 the scenario where the store data could be lost.
6639
6640 * A data cache miss, due to either a load or a store, causing fill
6641 data to be supplied by the memory subsystem
6642 * The first three doublewords of fill data are returned and written
6643 into the cache
6644 * A sequence of four stores occurs in consecutive cycles around the
6645 final doubleword of the fill:
6646 * Store A
6647 * Store B
6648 * Store C
6649 * Zero, One or more instructions
6650 * Store D
6651
6652 The four stores A-D must be to different doublewords of the line that
6653 is being filled. The fourth instruction in the sequence above permits
6654 the fill of the final doubleword to be transferred from the FSB into
6655 the cache. In the sequence above, the stores may be either integer
6656 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
6657 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
6658 different doublewords on the line. If the floating point unit is
6659 running in 1:2 mode, it is not possible to create the sequence above
6660 using only floating point store instructions.
15be625d
CM
6661
6662 In this case, the cache line being filled is incorrectly marked
6663 invalid, thereby losing the data from any store to the line that
6664 occurs between the original miss and the completion of the five
6665 cycle sequence shown above.
6666
932d1a1b 6667 The workarounds are:
15be625d 6668
932d1a1b
RS
6669 * Run the data cache in write-through mode.
6670 * Insert a non-store instruction between
6671 Store A and Store B or Store B and Store C. */
3739860c 6672
15be625d 6673static int
932d1a1b 6674nops_for_24k (int ignore, const struct mips_cl_insn *hist,
15be625d
CM
6675 const struct mips_cl_insn *insn)
6676{
6677 struct fix_24k_store_info pos[3];
6678 int align, i, base_offset;
6679
932d1a1b
RS
6680 if (ignore >= 2)
6681 return 0;
6682
ab9794cf
RS
6683 /* If the previous instruction wasn't a store, there's nothing to
6684 worry about. */
15be625d
CM
6685 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6686 return 0;
6687
ab9794cf
RS
6688 /* If the instructions after the previous one are unknown, we have
6689 to assume the worst. */
6690 if (!insn)
15be625d
CM
6691 return 1;
6692
ab9794cf
RS
6693 /* Check whether we are dealing with three consecutive stores. */
6694 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
6695 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
15be625d
CM
6696 return 0;
6697
6698 /* If we don't know the relationship between the store addresses,
6699 assume the worst. */
ab9794cf 6700 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
15be625d
CM
6701 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
6702 return 1;
6703
6704 if (!fix_24k_record_store_info (&pos[0], insn)
6705 || !fix_24k_record_store_info (&pos[1], &hist[0])
6706 || !fix_24k_record_store_info (&pos[2], &hist[1]))
6707 return 1;
6708
6709 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
6710
6711 /* Pick a value of ALIGN and X such that all offsets are adjusted by
6712 X bytes and such that the base register + X is known to be aligned
6713 to align bytes. */
6714
6715 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
6716 align = 8;
6717 else
6718 {
6719 align = pos[0].align_to;
6720 base_offset = pos[0].off;
6721 for (i = 1; i < 3; i++)
6722 if (align < pos[i].align_to)
6723 {
6724 align = pos[i].align_to;
6725 base_offset = pos[i].off;
6726 }
6727 for (i = 0; i < 3; i++)
6728 pos[i].off -= base_offset;
6729 }
6730
6731 pos[0].off &= ~align + 1;
6732 pos[1].off &= ~align + 1;
6733 pos[2].off &= ~align + 1;
6734
6735 /* If any two stores write to the same chunk, they also write to the
6736 same doubleword. The offsets are still sorted at this point. */
6737 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
6738 return 0;
6739
6740 /* A range of at least 9 bytes is needed for the stores to be in
6741 non-overlapping doublewords. */
6742 if (pos[2].off - pos[0].off <= 8)
6743 return 0;
6744
6745 if (pos[2].off - pos[1].off >= 24
6746 || pos[1].off - pos[0].off >= 24
6747 || pos[2].off - pos[0].off >= 32)
6748 return 0;
6749
6750 return 1;
6751}
6752
71400594 6753/* Return the number of nops that would be needed if instruction INSN
91d6fa6a 6754 immediately followed the MAX_NOPS instructions given by HIST,
932d1a1b
RS
6755 where HIST[0] is the most recent instruction. Ignore hazards
6756 between INSN and the first IGNORE instructions in HIST.
6757
6758 If INSN is null, return the worse-case number of nops for any
6759 instruction. */
bdaaa2e1 6760
71400594 6761static int
932d1a1b 6762nops_for_insn (int ignore, const struct mips_cl_insn *hist,
71400594
RS
6763 const struct mips_cl_insn *insn)
6764{
6765 int i, nops, tmp_nops;
bdaaa2e1 6766
71400594 6767 nops = 0;
932d1a1b 6768 for (i = ignore; i < MAX_DELAY_NOPS; i++)
65b02341 6769 {
91d6fa6a 6770 tmp_nops = insns_between (hist + i, insn) - i;
65b02341
RS
6771 if (tmp_nops > nops)
6772 nops = tmp_nops;
6773 }
7d8e00cf 6774
df58fc94 6775 if (mips_fix_vr4130 && !mips_opts.micromips)
7d8e00cf 6776 {
932d1a1b 6777 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
7d8e00cf
RS
6778 if (tmp_nops > nops)
6779 nops = tmp_nops;
6780 }
6781
df58fc94 6782 if (mips_fix_24k && !mips_opts.micromips)
15be625d 6783 {
932d1a1b 6784 tmp_nops = nops_for_24k (ignore, hist, insn);
15be625d
CM
6785 if (tmp_nops > nops)
6786 nops = tmp_nops;
6787 }
6788
71400594
RS
6789 return nops;
6790}
252b5132 6791
71400594 6792/* The variable arguments provide NUM_INSNS extra instructions that
91d6fa6a 6793 might be added to HIST. Return the largest number of nops that
932d1a1b
RS
6794 would be needed after the extended sequence, ignoring hazards
6795 in the first IGNORE instructions. */
252b5132 6796
71400594 6797static int
932d1a1b
RS
6798nops_for_sequence (int num_insns, int ignore,
6799 const struct mips_cl_insn *hist, ...)
71400594
RS
6800{
6801 va_list args;
6802 struct mips_cl_insn buffer[MAX_NOPS];
6803 struct mips_cl_insn *cursor;
6804 int nops;
6805
91d6fa6a 6806 va_start (args, hist);
71400594 6807 cursor = buffer + num_insns;
91d6fa6a 6808 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
71400594
RS
6809 while (cursor > buffer)
6810 *--cursor = *va_arg (args, const struct mips_cl_insn *);
6811
932d1a1b 6812 nops = nops_for_insn (ignore, buffer, NULL);
71400594
RS
6813 va_end (args);
6814 return nops;
6815}
252b5132 6816
71400594
RS
6817/* Like nops_for_insn, but if INSN is a branch, take into account the
6818 worst-case delay for the branch target. */
252b5132 6819
71400594 6820static int
932d1a1b 6821nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
71400594
RS
6822 const struct mips_cl_insn *insn)
6823{
6824 int nops, tmp_nops;
60b63b72 6825
932d1a1b 6826 nops = nops_for_insn (ignore, hist, insn);
11625dd8 6827 if (delayed_branch_p (insn))
71400594 6828 {
932d1a1b 6829 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
14fe068b 6830 hist, insn, get_delay_slot_nop (insn));
71400594
RS
6831 if (tmp_nops > nops)
6832 nops = tmp_nops;
6833 }
11625dd8 6834 else if (compact_branch_p (insn))
71400594 6835 {
932d1a1b 6836 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
71400594
RS
6837 if (tmp_nops > nops)
6838 nops = tmp_nops;
6839 }
6840 return nops;
6841}
6842
c67a084a
NC
6843/* Fix NOP issue: Replace nops by "or at,at,zero". */
6844
6845static void
6846fix_loongson2f_nop (struct mips_cl_insn * ip)
6847{
df58fc94 6848 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
6849 if (strcmp (ip->insn_mo->name, "nop") == 0)
6850 ip->insn_opcode = LOONGSON2F_NOP_INSN;
6851}
6852
6853/* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
6854 jr target pc &= 'hffff_ffff_cfff_ffff. */
6855
6856static void
6857fix_loongson2f_jump (struct mips_cl_insn * ip)
6858{
df58fc94 6859 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
6860 if (strcmp (ip->insn_mo->name, "j") == 0
6861 || strcmp (ip->insn_mo->name, "jr") == 0
6862 || strcmp (ip->insn_mo->name, "jalr") == 0)
6863 {
6864 int sreg;
6865 expressionS ep;
6866
6867 if (! mips_opts.at)
6868 return;
6869
df58fc94 6870 sreg = EXTRACT_OPERAND (0, RS, *ip);
c67a084a
NC
6871 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
6872 return;
6873
6874 ep.X_op = O_constant;
6875 ep.X_add_number = 0xcfff0000;
6876 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
6877 ep.X_add_number = 0xffff;
6878 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
6879 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
6880 }
6881}
6882
6883static void
6884fix_loongson2f (struct mips_cl_insn * ip)
6885{
6886 if (mips_fix_loongson2f_nop)
6887 fix_loongson2f_nop (ip);
6888
6889 if (mips_fix_loongson2f_jump)
6890 fix_loongson2f_jump (ip);
6891}
6892
6f2117ba
PH
6893/* Fix loongson3 llsc errata: Insert sync before ll/lld. */
6894
6895static void
6896fix_loongson3_llsc (struct mips_cl_insn * ip)
6897{
6898 gas_assert (!HAVE_CODE_COMPRESSION);
6899
6900 /* If is an local label and the insn is not sync,
6901 look forward that whether an branch between ll/sc jump to here
6902 if so, insert a sync. */
6903 if (seg_info (now_seg)->label_list
6904 && S_IS_LOCAL (seg_info (now_seg)->label_list->label)
6905 && (strcmp (ip->insn_mo->name, "sync") != 0))
6906 {
6907 const char *label_name = S_GET_NAME (seg_info (now_seg)->label_list->label);
6908 unsigned long lookback = ARRAY_SIZE (history);
6909 unsigned long i;
6910
6911 for (i = 0; i < lookback; i++)
6912 {
6913 if (streq (history[i].insn_mo->name, "ll")
6914 || streq (history[i].insn_mo->name, "lld"))
6915 break;
6916
6917 if (streq (history[i].insn_mo->name, "sc")
6918 || streq (history[i].insn_mo->name, "scd"))
6919 {
6920 unsigned long j;
6921
6922 for (j = i + 1; j < lookback; j++)
6923 {
6924 if (streq (history[i].insn_mo->name, "ll")
6925 || streq (history[i].insn_mo->name, "lld"))
6926 break;
6927
6928 if (delayed_branch_p (&history[j]))
6929 {
6930 if (streq (history[j].target, label_name))
6931 {
6932 add_fixed_insn (&sync_insn);
6933 insert_into_history (0, 1, &sync_insn);
6934 i = lookback;
6935 break;
6936 }
6937 }
6938 }
6939 }
6940 }
6941 }
6942 /* If we find a sc, we look forward to look for an branch insn,
6943 and see whether it jump back and out of ll/sc. */
6944 else if (streq(ip->insn_mo->name, "sc") || streq(ip->insn_mo->name, "scd"))
6945 {
6946 unsigned long lookback = ARRAY_SIZE (history) - 1;
6947 unsigned long i;
6948
6949 for (i = 0; i < lookback; i++)
6950 {
6951 if (streq (history[i].insn_mo->name, "ll")
6952 || streq (history[i].insn_mo->name, "lld"))
6953 break;
6954
6955 if (delayed_branch_p (&history[i]))
6956 {
6957 unsigned long j;
6958
6959 for (j = i + 1; j < lookback; j++)
6960 {
6961 if (streq (history[j].insn_mo->name, "ll")
6962 || streq (history[i].insn_mo->name, "lld"))
6963 break;
6964 }
6965
6966 for (; j < lookback; j++)
6967 {
6968 if (history[j].label[0] != '\0'
6969 && streq (history[j].label, history[i].target)
6970 && strcmp (history[j+1].insn_mo->name, "sync") != 0)
6971 {
6972 add_fixed_insn (&sync_insn);
6973 insert_into_history (++j, 1, &sync_insn);
6974 }
6975 }
6976 }
6977 }
6978 }
6979
6980 /* Skip if there is a sync before ll/lld. */
6981 if ((strcmp (ip->insn_mo->name, "ll") == 0
6982 || strcmp (ip->insn_mo->name, "lld") == 0)
6983 && (strcmp (history[0].insn_mo->name, "sync") != 0))
6984 {
6985 add_fixed_insn (&sync_insn);
6986 insert_into_history (0, 1, &sync_insn);
6987 }
6988}
6989
a4e06468
RS
6990/* IP is a branch that has a delay slot, and we need to fill it
6991 automatically. Return true if we can do that by swapping IP
e407c74b
NC
6992 with the previous instruction.
6993 ADDRESS_EXPR is an operand of the instruction to be used with
6994 RELOC_TYPE. */
a4e06468
RS
6995
6996static bfd_boolean
e407c74b 6997can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 6998 bfd_reloc_code_real_type *reloc_type)
a4e06468 6999{
2b0c8b40 7000 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
a4e06468 7001 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
9d5de888 7002 unsigned int fpr_read, prev_fpr_write;
a4e06468
RS
7003
7004 /* -O2 and above is required for this optimization. */
7005 if (mips_optimize < 2)
7006 return FALSE;
7007
7008 /* If we have seen .set volatile or .set nomove, don't optimize. */
7009 if (mips_opts.nomove)
7010 return FALSE;
7011
7012 /* We can't swap if the previous instruction's position is fixed. */
7013 if (history[0].fixed_p)
7014 return FALSE;
7015
7016 /* If the previous previous insn was in a .set noreorder, we can't
7017 swap. Actually, the MIPS assembler will swap in this situation.
7018 However, gcc configured -with-gnu-as will generate code like
7019
7020 .set noreorder
7021 lw $4,XXX
7022 .set reorder
7023 INSN
7024 bne $4,$0,foo
7025
7026 in which we can not swap the bne and INSN. If gcc is not configured
7027 -with-gnu-as, it does not output the .set pseudo-ops. */
7028 if (history[1].noreorder_p)
7029 return FALSE;
7030
87333bb7
MR
7031 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
7032 This means that the previous instruction was a 4-byte one anyhow. */
a4e06468
RS
7033 if (mips_opts.mips16 && history[0].fixp[0])
7034 return FALSE;
7035
7036 /* If the branch is itself the target of a branch, we can not swap.
7037 We cheat on this; all we check for is whether there is a label on
7038 this instruction. If there are any branches to anything other than
7039 a label, users must use .set noreorder. */
7040 if (seg_info (now_seg)->label_list)
7041 return FALSE;
7042
7043 /* If the previous instruction is in a variant frag other than this
2309ddf2 7044 branch's one, we cannot do the swap. This does not apply to
9301f9c3
MR
7045 MIPS16 code, which uses variant frags for different purposes. */
7046 if (!mips_opts.mips16
a4e06468
RS
7047 && history[0].frag
7048 && history[0].frag->fr_type == rs_machine_dependent)
7049 return FALSE;
7050
bcd530a7
RS
7051 /* We do not swap with instructions that cannot architecturally
7052 be placed in a branch delay slot, such as SYNC or ERET. We
7053 also refrain from swapping with a trap instruction, since it
7054 complicates trap handlers to have the trap instruction be in
7055 a delay slot. */
a4e06468 7056 prev_pinfo = history[0].insn_mo->pinfo;
bcd530a7 7057 if (prev_pinfo & INSN_NO_DELAY_SLOT)
a4e06468
RS
7058 return FALSE;
7059
7060 /* Check for conflicts between the branch and the instructions
7061 before the candidate delay slot. */
7062 if (nops_for_insn (0, history + 1, ip) > 0)
7063 return FALSE;
7064
7065 /* Check for conflicts between the swapped sequence and the
7066 target of the branch. */
7067 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
7068 return FALSE;
7069
7070 /* If the branch reads a register that the previous
7071 instruction sets, we can not swap. */
7072 gpr_read = gpr_read_mask (ip);
7073 prev_gpr_write = gpr_write_mask (&history[0]);
7074 if (gpr_read & prev_gpr_write)
7075 return FALSE;
7076
9d5de888
CF
7077 fpr_read = fpr_read_mask (ip);
7078 prev_fpr_write = fpr_write_mask (&history[0]);
7079 if (fpr_read & prev_fpr_write)
7080 return FALSE;
7081
a4e06468
RS
7082 /* If the branch writes a register that the previous
7083 instruction sets, we can not swap. */
7084 gpr_write = gpr_write_mask (ip);
7085 if (gpr_write & prev_gpr_write)
7086 return FALSE;
7087
7088 /* If the branch writes a register that the previous
7089 instruction reads, we can not swap. */
7090 prev_gpr_read = gpr_read_mask (&history[0]);
7091 if (gpr_write & prev_gpr_read)
7092 return FALSE;
7093
7094 /* If one instruction sets a condition code and the
7095 other one uses a condition code, we can not swap. */
7096 pinfo = ip->insn_mo->pinfo;
7097 if ((pinfo & INSN_READ_COND_CODE)
7098 && (prev_pinfo & INSN_WRITE_COND_CODE))
7099 return FALSE;
7100 if ((pinfo & INSN_WRITE_COND_CODE)
7101 && (prev_pinfo & INSN_READ_COND_CODE))
7102 return FALSE;
7103
7104 /* If the previous instruction uses the PC, we can not swap. */
2b0c8b40 7105 prev_pinfo2 = history[0].insn_mo->pinfo2;
26545944 7106 if (prev_pinfo2 & INSN2_READ_PC)
2b0c8b40 7107 return FALSE;
a4e06468 7108
df58fc94
RS
7109 /* If the previous instruction has an incorrect size for a fixed
7110 branch delay slot in microMIPS mode, we cannot swap. */
2309ddf2
MR
7111 pinfo2 = ip->insn_mo->pinfo2;
7112 if (mips_opts.micromips
7113 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
7114 && insn_length (history) != 2)
7115 return FALSE;
7116 if (mips_opts.micromips
7117 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
7118 && insn_length (history) != 4)
7119 return FALSE;
7120
33d64ca5
FN
7121 /* On the R5900 short loops need to be fixed by inserting a NOP in the
7122 branch delay slot.
7123
7124 The short loop bug under certain conditions causes loops to execute
7125 only once or twice. We must ensure that the assembler never
7126 generates loops that satisfy all of the following conditions:
7127
7128 - a loop consists of less than or equal to six instructions
7129 (including the branch delay slot);
7130 - a loop contains only one conditional branch instruction at the end
7131 of the loop;
7132 - a loop does not contain any other branch or jump instructions;
7133 - a branch delay slot of the loop is not NOP (EE 2.9 or later).
7134
7135 We need to do this because of a hardware bug in the R5900 chip. */
27c634e0 7136 if (mips_fix_r5900
e407c74b
NC
7137 /* Check if instruction has a parameter, ignore "j $31". */
7138 && (address_expr != NULL)
7139 /* Parameter must be 16 bit. */
7140 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
7141 /* Branch to same segment. */
41065f5e 7142 && (S_GET_SEGMENT (address_expr->X_add_symbol) == now_seg)
e407c74b 7143 /* Branch to same code fragment. */
41065f5e 7144 && (symbol_get_frag (address_expr->X_add_symbol) == frag_now)
e407c74b 7145 /* Can only calculate branch offset if value is known. */
41065f5e 7146 && symbol_constant_p (address_expr->X_add_symbol)
e407c74b
NC
7147 /* Check if branch is really conditional. */
7148 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
7149 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
7150 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
7151 {
7152 int distance;
33d64ca5
FN
7153 /* Check if loop is shorter than or equal to 6 instructions
7154 including branch and delay slot. */
41065f5e 7155 distance = frag_now_fix () - S_GET_VALUE (address_expr->X_add_symbol);
e407c74b
NC
7156 if (distance <= 20)
7157 {
7158 int i;
7159 int rv;
7160
7161 rv = FALSE;
7162 /* When the loop includes branches or jumps,
7163 it is not a short loop. */
7164 for (i = 0; i < (distance / 4); i++)
7165 {
7166 if ((history[i].cleared_p)
41065f5e 7167 || delayed_branch_p (&history[i]))
e407c74b
NC
7168 {
7169 rv = TRUE;
7170 break;
7171 }
7172 }
535b785f 7173 if (!rv)
e407c74b
NC
7174 {
7175 /* Insert nop after branch to fix short loop. */
7176 return FALSE;
7177 }
7178 }
7179 }
7180
a4e06468
RS
7181 return TRUE;
7182}
7183
e407c74b
NC
7184/* Decide how we should add IP to the instruction stream.
7185 ADDRESS_EXPR is an operand of the instruction to be used with
7186 RELOC_TYPE. */
a4e06468
RS
7187
7188static enum append_method
e407c74b 7189get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 7190 bfd_reloc_code_real_type *reloc_type)
a4e06468 7191{
a4e06468
RS
7192 /* The relaxed version of a macro sequence must be inherently
7193 hazard-free. */
7194 if (mips_relax.sequence == 2)
7195 return APPEND_ADD;
7196
3b821a28 7197 /* We must not dabble with instructions in a ".set noreorder" block. */
a4e06468
RS
7198 if (mips_opts.noreorder)
7199 return APPEND_ADD;
7200
7201 /* Otherwise, it's our responsibility to fill branch delay slots. */
11625dd8 7202 if (delayed_branch_p (ip))
a4e06468 7203 {
e407c74b
NC
7204 if (!branch_likely_p (ip)
7205 && can_swap_branch_p (ip, address_expr, reloc_type))
a4e06468
RS
7206 return APPEND_SWAP;
7207
7208 if (mips_opts.mips16
7209 && ISA_SUPPORTS_MIPS16E
fc76e730 7210 && gpr_read_mask (ip) != 0)
a4e06468
RS
7211 return APPEND_ADD_COMPACT;
7212
7bd374a4
MR
7213 if (mips_opts.micromips
7214 && ((ip->insn_opcode & 0xffe0) == 0x4580
7215 || (!forced_insn_length
7216 && ((ip->insn_opcode & 0xfc00) == 0xcc00
7217 || (ip->insn_opcode & 0xdc00) == 0x8c00))
7218 || (ip->insn_opcode & 0xdfe00000) == 0x94000000
7219 || (ip->insn_opcode & 0xdc1f0000) == 0x94000000))
7220 return APPEND_ADD_COMPACT;
7221
a4e06468
RS
7222 return APPEND_ADD_WITH_NOP;
7223 }
7224
a4e06468
RS
7225 return APPEND_ADD;
7226}
7227
7bd374a4
MR
7228/* IP is an instruction whose opcode we have just changed, END points
7229 to the end of the opcode table processed. Point IP->insn_mo to the
7230 new opcode's definition. */
ceb94aa5
RS
7231
7232static void
7bd374a4 7233find_altered_opcode (struct mips_cl_insn *ip, const struct mips_opcode *end)
ceb94aa5 7234{
7bd374a4 7235 const struct mips_opcode *mo;
ceb94aa5 7236
ceb94aa5 7237 for (mo = ip->insn_mo; mo < end; mo++)
7bd374a4
MR
7238 if (mo->pinfo != INSN_MACRO
7239 && (ip->insn_opcode & mo->mask) == mo->match)
ceb94aa5
RS
7240 {
7241 ip->insn_mo = mo;
7242 return;
7243 }
7244 abort ();
7245}
7246
7bd374a4
MR
7247/* IP is a MIPS16 instruction whose opcode we have just changed.
7248 Point IP->insn_mo to the new opcode's definition. */
7249
7250static void
7251find_altered_mips16_opcode (struct mips_cl_insn *ip)
7252{
7253 find_altered_opcode (ip, &mips16_opcodes[bfd_mips16_num_opcodes]);
7254}
7255
7256/* IP is a microMIPS instruction whose opcode we have just changed.
7257 Point IP->insn_mo to the new opcode's definition. */
7258
7259static void
7260find_altered_micromips_opcode (struct mips_cl_insn *ip)
7261{
7262 find_altered_opcode (ip, &micromips_opcodes[bfd_micromips_num_opcodes]);
7263}
7264
df58fc94
RS
7265/* For microMIPS macros, we need to generate a local number label
7266 as the target of branches. */
7267#define MICROMIPS_LABEL_CHAR '\037'
7268static unsigned long micromips_target_label;
7269static char micromips_target_name[32];
7270
7271static char *
7272micromips_label_name (void)
7273{
7274 char *p = micromips_target_name;
7275 char symbol_name_temporary[24];
7276 unsigned long l;
7277 int i;
7278
7279 if (*p)
7280 return p;
7281
7282 i = 0;
7283 l = micromips_target_label;
7284#ifdef LOCAL_LABEL_PREFIX
7285 *p++ = LOCAL_LABEL_PREFIX;
7286#endif
7287 *p++ = 'L';
7288 *p++ = MICROMIPS_LABEL_CHAR;
7289 do
7290 {
7291 symbol_name_temporary[i++] = l % 10 + '0';
7292 l /= 10;
7293 }
7294 while (l != 0);
7295 while (i > 0)
7296 *p++ = symbol_name_temporary[--i];
7297 *p = '\0';
7298
7299 return micromips_target_name;
7300}
7301
7302static void
7303micromips_label_expr (expressionS *label_expr)
7304{
7305 label_expr->X_op = O_symbol;
7306 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
7307 label_expr->X_add_number = 0;
7308}
7309
7310static void
7311micromips_label_inc (void)
7312{
7313 micromips_target_label++;
7314 *micromips_target_name = '\0';
7315}
7316
7317static void
7318micromips_add_label (void)
7319{
7320 symbolS *s;
7321
7322 s = colon (micromips_label_name ());
7323 micromips_label_inc ();
f3ded42a 7324 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
df58fc94
RS
7325}
7326
7327/* If assembling microMIPS code, then return the microMIPS reloc
7328 corresponding to the requested one if any. Otherwise return
7329 the reloc unchanged. */
7330
7331static bfd_reloc_code_real_type
7332micromips_map_reloc (bfd_reloc_code_real_type reloc)
7333{
7334 static const bfd_reloc_code_real_type relocs[][2] =
7335 {
7336 /* Keep sorted incrementally by the left-hand key. */
7337 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
7338 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
7339 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
7340 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
7341 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
7342 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
7343 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
7344 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
7345 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
7346 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
7347 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
7348 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
7349 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
7350 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
7351 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
7352 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
7353 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
7354 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
7355 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
7356 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
7357 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
7358 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
7359 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
7360 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
7361 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
7362 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
7363 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
7364 };
7365 bfd_reloc_code_real_type r;
7366 size_t i;
7367
7368 if (!mips_opts.micromips)
7369 return reloc;
7370 for (i = 0; i < ARRAY_SIZE (relocs); i++)
7371 {
7372 r = relocs[i][0];
7373 if (r > reloc)
7374 return reloc;
7375 if (r == reloc)
7376 return relocs[i][1];
7377 }
7378 return reloc;
7379}
7380
b886a2ab
RS
7381/* Try to resolve relocation RELOC against constant OPERAND at assembly time.
7382 Return true on success, storing the resolved value in RESULT. */
7383
7384static bfd_boolean
7385calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
7386 offsetT *result)
7387{
7388 switch (reloc)
7389 {
7390 case BFD_RELOC_MIPS_HIGHEST:
7391 case BFD_RELOC_MICROMIPS_HIGHEST:
7392 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
7393 return TRUE;
7394
7395 case BFD_RELOC_MIPS_HIGHER:
7396 case BFD_RELOC_MICROMIPS_HIGHER:
7397 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
7398 return TRUE;
7399
7400 case BFD_RELOC_HI16_S:
41947d9e 7401 case BFD_RELOC_HI16_S_PCREL:
b886a2ab
RS
7402 case BFD_RELOC_MICROMIPS_HI16_S:
7403 case BFD_RELOC_MIPS16_HI16_S:
7404 *result = ((operand + 0x8000) >> 16) & 0xffff;
7405 return TRUE;
7406
7407 case BFD_RELOC_HI16:
7408 case BFD_RELOC_MICROMIPS_HI16:
7409 case BFD_RELOC_MIPS16_HI16:
7410 *result = (operand >> 16) & 0xffff;
7411 return TRUE;
7412
7413 case BFD_RELOC_LO16:
41947d9e 7414 case BFD_RELOC_LO16_PCREL:
b886a2ab
RS
7415 case BFD_RELOC_MICROMIPS_LO16:
7416 case BFD_RELOC_MIPS16_LO16:
7417 *result = operand & 0xffff;
7418 return TRUE;
7419
7420 case BFD_RELOC_UNUSED:
7421 *result = operand;
7422 return TRUE;
7423
7424 default:
7425 return FALSE;
7426 }
7427}
7428
71400594
RS
7429/* Output an instruction. IP is the instruction information.
7430 ADDRESS_EXPR is an operand of the instruction to be used with
df58fc94
RS
7431 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
7432 a macro expansion. */
71400594
RS
7433
7434static void
7435append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
df58fc94 7436 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
71400594 7437{
14fe068b 7438 unsigned long prev_pinfo2, pinfo;
71400594 7439 bfd_boolean relaxed_branch = FALSE;
a4e06468 7440 enum append_method method;
2309ddf2 7441 bfd_boolean relax32;
2b0c8b40 7442 int branch_disp;
71400594 7443
2309ddf2 7444 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
c67a084a
NC
7445 fix_loongson2f (ip);
7446
6f2117ba
PH
7447 ip->target[0] = '\0';
7448 if (offset_expr.X_op == O_symbol)
7449 strncpy (ip->target, S_GET_NAME (offset_expr.X_add_symbol), 15);
7450 ip->label[0] = '\0';
7451 if (seg_info (now_seg)->label_list)
7452 strncpy (ip->label, S_GET_NAME (seg_info (now_seg)->label_list->label), 15);
7453 if (mips_fix_loongson3_llsc && !HAVE_CODE_COMPRESSION)
7454 fix_loongson3_llsc (ip);
7455
738f4d98 7456 file_ase_mips16 |= mips_opts.mips16;
df58fc94 7457 file_ase_micromips |= mips_opts.micromips;
738f4d98 7458
df58fc94 7459 prev_pinfo2 = history[0].insn_mo->pinfo2;
71400594 7460 pinfo = ip->insn_mo->pinfo;
df58fc94 7461
7bd374a4
MR
7462 /* Don't raise alarm about `nods' frags as they'll fill in the right
7463 kind of nop in relaxation if required. */
df58fc94
RS
7464 if (mips_opts.micromips
7465 && !expansionp
7bd374a4
MR
7466 && !(history[0].frag
7467 && history[0].frag->fr_type == rs_machine_dependent
7468 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
7469 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
df58fc94
RS
7470 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
7471 && micromips_insn_length (ip->insn_mo) != 2)
7472 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
7473 && micromips_insn_length (ip->insn_mo) != 4)))
1661c76c 7474 as_warn (_("wrong size instruction in a %u-bit branch delay slot"),
df58fc94 7475 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
71400594 7476
15be625d
CM
7477 if (address_expr == NULL)
7478 ip->complete_p = 1;
b886a2ab
RS
7479 else if (reloc_type[0] <= BFD_RELOC_UNUSED
7480 && reloc_type[1] == BFD_RELOC_UNUSED
7481 && reloc_type[2] == BFD_RELOC_UNUSED
15be625d
CM
7482 && address_expr->X_op == O_constant)
7483 {
15be625d
CM
7484 switch (*reloc_type)
7485 {
15be625d 7486 case BFD_RELOC_MIPS_JMP:
df58fc94
RS
7487 {
7488 int shift;
7489
17c6c9d9
MR
7490 /* Shift is 2, unusually, for microMIPS JALX. */
7491 shift = (mips_opts.micromips
7492 && strcmp (ip->insn_mo->name, "jalx") != 0) ? 1 : 2;
df58fc94
RS
7493 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7494 as_bad (_("jump to misaligned address (0x%lx)"),
7495 (unsigned long) address_expr->X_add_number);
7496 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7497 & 0x3ffffff);
335574df 7498 ip->complete_p = 1;
df58fc94 7499 }
15be625d
CM
7500 break;
7501
7502 case BFD_RELOC_MIPS16_JMP:
7503 if ((address_expr->X_add_number & 3) != 0)
7504 as_bad (_("jump to misaligned address (0x%lx)"),
7505 (unsigned long) address_expr->X_add_number);
7506 ip->insn_opcode |=
7507 (((address_expr->X_add_number & 0x7c0000) << 3)
7508 | ((address_expr->X_add_number & 0xf800000) >> 7)
7509 | ((address_expr->X_add_number & 0x3fffc) >> 2));
335574df 7510 ip->complete_p = 1;
15be625d
CM
7511 break;
7512
7513 case BFD_RELOC_16_PCREL_S2:
df58fc94
RS
7514 {
7515 int shift;
7516
7517 shift = mips_opts.micromips ? 1 : 2;
7518 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7519 as_bad (_("branch to misaligned address (0x%lx)"),
7520 (unsigned long) address_expr->X_add_number);
7521 if (!mips_relax_branch)
7522 {
7523 if ((address_expr->X_add_number + (1 << (shift + 15)))
7524 & ~((1 << (shift + 16)) - 1))
7525 as_bad (_("branch address range overflow (0x%lx)"),
7526 (unsigned long) address_expr->X_add_number);
7527 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7528 & 0xffff);
7529 }
df58fc94 7530 }
15be625d
CM
7531 break;
7532
7361da2c
AB
7533 case BFD_RELOC_MIPS_21_PCREL_S2:
7534 {
7535 int shift;
7536
7537 shift = 2;
7538 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7539 as_bad (_("branch to misaligned address (0x%lx)"),
7540 (unsigned long) address_expr->X_add_number);
7541 if ((address_expr->X_add_number + (1 << (shift + 20)))
7542 & ~((1 << (shift + 21)) - 1))
7543 as_bad (_("branch address range overflow (0x%lx)"),
7544 (unsigned long) address_expr->X_add_number);
7545 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7546 & 0x1fffff);
7547 }
7548 break;
7549
7550 case BFD_RELOC_MIPS_26_PCREL_S2:
7551 {
7552 int shift;
7553
7554 shift = 2;
7555 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7556 as_bad (_("branch to misaligned address (0x%lx)"),
7557 (unsigned long) address_expr->X_add_number);
7558 if ((address_expr->X_add_number + (1 << (shift + 25)))
7559 & ~((1 << (shift + 26)) - 1))
7560 as_bad (_("branch address range overflow (0x%lx)"),
7561 (unsigned long) address_expr->X_add_number);
7562 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7563 & 0x3ffffff);
7564 }
7565 break;
7566
15be625d 7567 default:
b886a2ab
RS
7568 {
7569 offsetT value;
7570
7571 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
7572 &value))
7573 {
7574 ip->insn_opcode |= value & 0xffff;
7575 ip->complete_p = 1;
7576 }
7577 }
7578 break;
7579 }
15be625d
CM
7580 }
7581
71400594
RS
7582 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
7583 {
7584 /* There are a lot of optimizations we could do that we don't.
7585 In particular, we do not, in general, reorder instructions.
7586 If you use gcc with optimization, it will reorder
7587 instructions and generally do much more optimization then we
7588 do here; repeating all that work in the assembler would only
7589 benefit hand written assembly code, and does not seem worth
7590 it. */
7591 int nops = (mips_optimize == 0
932d1a1b
RS
7592 ? nops_for_insn (0, history, NULL)
7593 : nops_for_insn_or_target (0, history, ip));
71400594 7594 if (nops > 0)
252b5132
RH
7595 {
7596 fragS *old_frag;
7597 unsigned long old_frag_offset;
7598 int i;
252b5132
RH
7599
7600 old_frag = frag_now;
7601 old_frag_offset = frag_now_fix ();
7602
7603 for (i = 0; i < nops; i++)
14fe068b
RS
7604 add_fixed_insn (NOP_INSN);
7605 insert_into_history (0, nops, NOP_INSN);
252b5132
RH
7606
7607 if (listing)
7608 {
7609 listing_prev_line ();
7610 /* We may be at the start of a variant frag. In case we
7611 are, make sure there is enough space for the frag
7612 after the frags created by listing_prev_line. The
7613 argument to frag_grow here must be at least as large
7614 as the argument to all other calls to frag_grow in
7615 this file. We don't have to worry about being in the
7616 middle of a variant frag, because the variants insert
7617 all needed nop instructions themselves. */
7618 frag_grow (40);
7619 }
7620
462427c4 7621 mips_move_text_labels ();
252b5132
RH
7622
7623#ifndef NO_ECOFF_DEBUGGING
7624 if (ECOFF_DEBUGGING)
7625 ecoff_fix_loc (old_frag, old_frag_offset);
7626#endif
7627 }
71400594
RS
7628 }
7629 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
7630 {
932d1a1b
RS
7631 int nops;
7632
7633 /* Work out how many nops in prev_nop_frag are needed by IP,
7634 ignoring hazards generated by the first prev_nop_frag_since
7635 instructions. */
7636 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
9c2799c2 7637 gas_assert (nops <= prev_nop_frag_holds);
252b5132 7638
71400594
RS
7639 /* Enforce NOPS as a minimum. */
7640 if (nops > prev_nop_frag_required)
7641 prev_nop_frag_required = nops;
252b5132 7642
71400594
RS
7643 if (prev_nop_frag_holds == prev_nop_frag_required)
7644 {
7645 /* Settle for the current number of nops. Update the history
7646 accordingly (for the benefit of any future .set reorder code). */
7647 prev_nop_frag = NULL;
7648 insert_into_history (prev_nop_frag_since,
7649 prev_nop_frag_holds, NOP_INSN);
7650 }
7651 else
7652 {
7653 /* Allow this instruction to replace one of the nops that was
7654 tentatively added to prev_nop_frag. */
df58fc94 7655 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
71400594
RS
7656 prev_nop_frag_holds--;
7657 prev_nop_frag_since++;
252b5132
RH
7658 }
7659 }
7660
e407c74b 7661 method = get_append_method (ip, address_expr, reloc_type);
2b0c8b40 7662 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
a4e06468 7663
e410add4
RS
7664 dwarf2_emit_insn (0);
7665 /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
7666 so "move" the instruction address accordingly.
7667
7668 Also, it doesn't seem appropriate for the assembler to reorder .loc
7669 entries. If this instruction is a branch that we are going to swap
7670 with the previous instruction, the two instructions should be
7671 treated as a unit, and the debug information for both instructions
7672 should refer to the start of the branch sequence. Using the
7673 current position is certainly wrong when swapping a 32-bit branch
7674 and a 16-bit delay slot, since the current position would then be
7675 in the middle of a branch. */
7676 dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
58e2ea4d 7677
df58fc94
RS
7678 relax32 = (mips_relax_branch
7679 /* Don't try branch relaxation within .set nomacro, or within
7680 .set noat if we use $at for PIC computations. If it turns
7681 out that the branch was out-of-range, we'll get an error. */
7682 && !mips_opts.warn_about_macros
7683 && (mips_opts.at || mips_pic == NO_PIC)
3bf0dbfb
MR
7684 /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
7685 as they have no complementing branches. */
7686 && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
df58fc94
RS
7687
7688 if (!HAVE_CODE_COMPRESSION
7689 && address_expr
7690 && relax32
0b25d3e6 7691 && *reloc_type == BFD_RELOC_16_PCREL_S2
11625dd8 7692 && delayed_branch_p (ip))
4a6a3df4 7693 {
895921c9 7694 relaxed_branch = TRUE;
1e915849
RS
7695 add_relaxed_insn (ip, (relaxed_branch_length
7696 (NULL, NULL,
11625dd8
RS
7697 uncond_branch_p (ip) ? -1
7698 : branch_likely_p (ip) ? 1
1e915849
RS
7699 : 0)), 4,
7700 RELAX_BRANCH_ENCODE
ce8ad872 7701 (AT, mips_pic != NO_PIC,
11625dd8
RS
7702 uncond_branch_p (ip),
7703 branch_likely_p (ip),
1e915849
RS
7704 pinfo & INSN_WRITE_GPR_31,
7705 0),
7706 address_expr->X_add_symbol,
7707 address_expr->X_add_number);
4a6a3df4
AO
7708 *reloc_type = BFD_RELOC_UNUSED;
7709 }
df58fc94
RS
7710 else if (mips_opts.micromips
7711 && address_expr
7712 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
7713 || *reloc_type > BFD_RELOC_UNUSED)
40209cad
MR
7714 && (delayed_branch_p (ip) || compact_branch_p (ip))
7715 /* Don't try branch relaxation when users specify
7716 16-bit/32-bit instructions. */
7717 && !forced_insn_length)
df58fc94 7718 {
7bd374a4
MR
7719 bfd_boolean relax16 = (method != APPEND_ADD_COMPACT
7720 && *reloc_type > BFD_RELOC_UNUSED);
df58fc94 7721 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
11625dd8 7722 int uncond = uncond_branch_p (ip) ? -1 : 0;
7bd374a4
MR
7723 int compact = compact_branch_p (ip) || method == APPEND_ADD_COMPACT;
7724 int nods = method == APPEND_ADD_WITH_NOP;
df58fc94 7725 int al = pinfo & INSN_WRITE_GPR_31;
7bd374a4 7726 int length32 = nods ? 8 : 4;
df58fc94
RS
7727
7728 gas_assert (address_expr != NULL);
7729 gas_assert (!mips_relax.sequence);
7730
2b0c8b40 7731 relaxed_branch = TRUE;
7bd374a4
MR
7732 if (nods)
7733 method = APPEND_ADD;
7734 if (relax32)
7735 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
7736 add_relaxed_insn (ip, length32, relax16 ? 2 : 4,
8484fb75 7737 RELAX_MICROMIPS_ENCODE (type, AT, mips_opts.insn32,
ce8ad872 7738 mips_pic != NO_PIC,
7bd374a4 7739 uncond, compact, al, nods,
40209cad 7740 relax32, 0, 0),
df58fc94
RS
7741 address_expr->X_add_symbol,
7742 address_expr->X_add_number);
7743 *reloc_type = BFD_RELOC_UNUSED;
7744 }
7745 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
252b5132 7746 {
7fd53920
MR
7747 bfd_boolean require_unextended;
7748 bfd_boolean require_extended;
88a7ef16
MR
7749 symbolS *symbol;
7750 offsetT offset;
7751
7fd53920
MR
7752 if (forced_insn_length != 0)
7753 {
7754 require_unextended = forced_insn_length == 2;
7755 require_extended = forced_insn_length == 4;
7756 }
7757 else
7758 {
7759 require_unextended = (mips_opts.noautoextend
7760 && !mips_opcode_32bit_p (ip->insn_mo));
7761 require_extended = 0;
7762 }
7763
252b5132 7764 /* We need to set up a variant frag. */
df58fc94 7765 gas_assert (address_expr != NULL);
88a7ef16
MR
7766 /* Pass any `O_symbol' expression unchanged as an `expr_section'
7767 symbol created by `make_expr_symbol' may not get a necessary
7768 external relocation produced. */
7769 if (address_expr->X_op == O_symbol)
7770 {
7771 symbol = address_expr->X_add_symbol;
7772 offset = address_expr->X_add_number;
7773 }
7774 else
7775 {
7776 symbol = make_expr_symbol (address_expr);
82d808ed 7777 symbol_append (symbol, symbol_lastP, &symbol_rootP, &symbol_lastP);
88a7ef16
MR
7778 offset = 0;
7779 }
8507b6e7 7780 add_relaxed_insn (ip, 12, 0,
1e915849
RS
7781 RELAX_MIPS16_ENCODE
7782 (*reloc_type - BFD_RELOC_UNUSED,
25499ac7 7783 mips_opts.ase & ASE_MIPS16E2,
8507b6e7
MR
7784 mips_pic != NO_PIC,
7785 HAVE_32BIT_SYMBOLS,
7786 mips_opts.warn_about_macros,
7fd53920 7787 require_unextended, require_extended,
11625dd8 7788 delayed_branch_p (&history[0]),
1e915849 7789 history[0].mips16_absolute_jump_p),
88a7ef16 7790 symbol, offset);
252b5132 7791 }
5c04167a 7792 else if (mips_opts.mips16 && insn_length (ip) == 2)
9497f5ac 7793 {
11625dd8 7794 if (!delayed_branch_p (ip))
b8ee1a6e
DU
7795 /* Make sure there is enough room to swap this instruction with
7796 a following jump instruction. */
7797 frag_grow (6);
1e915849 7798 add_fixed_insn (ip);
252b5132
RH
7799 }
7800 else
7801 {
7802 if (mips_opts.mips16
7803 && mips_opts.noreorder
11625dd8 7804 && delayed_branch_p (&history[0]))
252b5132
RH
7805 as_warn (_("extended instruction in delay slot"));
7806
4d7206a2
RS
7807 if (mips_relax.sequence)
7808 {
7809 /* If we've reached the end of this frag, turn it into a variant
7810 frag and record the information for the instructions we've
7811 written so far. */
7812 if (frag_room () < 4)
7813 relax_close_frag ();
df58fc94 7814 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4d7206a2
RS
7815 }
7816
584892a6 7817 if (mips_relax.sequence != 2)
df58fc94
RS
7818 {
7819 if (mips_macro_warning.first_insn_sizes[0] == 0)
7820 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
7821 mips_macro_warning.sizes[0] += insn_length (ip);
7822 mips_macro_warning.insns[0]++;
7823 }
584892a6 7824 if (mips_relax.sequence != 1)
df58fc94
RS
7825 {
7826 if (mips_macro_warning.first_insn_sizes[1] == 0)
7827 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
7828 mips_macro_warning.sizes[1] += insn_length (ip);
7829 mips_macro_warning.insns[1]++;
7830 }
584892a6 7831
1e915849
RS
7832 if (mips_opts.mips16)
7833 {
7834 ip->fixed_p = 1;
7835 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
7836 }
7837 add_fixed_insn (ip);
252b5132
RH
7838 }
7839
9fe77896 7840 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
252b5132 7841 {
df58fc94 7842 bfd_reloc_code_real_type final_type[3];
2309ddf2 7843 reloc_howto_type *howto0;
9fe77896
RS
7844 reloc_howto_type *howto;
7845 int i;
34ce925e 7846
df58fc94
RS
7847 /* Perform any necessary conversion to microMIPS relocations
7848 and find out how many relocations there actually are. */
7849 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
7850 final_type[i] = micromips_map_reloc (reloc_type[i]);
7851
9fe77896
RS
7852 /* In a compound relocation, it is the final (outermost)
7853 operator that determines the relocated field. */
2309ddf2 7854 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
e8044f35
RS
7855 if (!howto)
7856 abort ();
2309ddf2
MR
7857
7858 if (i > 1)
7859 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
9fe77896
RS
7860 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
7861 bfd_get_reloc_size (howto),
7862 address_expr,
2309ddf2
MR
7863 howto0 && howto0->pc_relative,
7864 final_type[0]);
ce8ad872
MR
7865 /* Record non-PIC mode in `fx_tcbit2' for `md_apply_fix'. */
7866 ip->fixp[0]->fx_tcbit2 = mips_pic == NO_PIC;
9fe77896
RS
7867
7868 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
2309ddf2 7869 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
9fe77896
RS
7870 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
7871
7872 /* These relocations can have an addend that won't fit in
7873 4 octets for 64bit assembly. */
bad1aba3 7874 if (GPR_SIZE == 64
9fe77896
RS
7875 && ! howto->partial_inplace
7876 && (reloc_type[0] == BFD_RELOC_16
7877 || reloc_type[0] == BFD_RELOC_32
7878 || reloc_type[0] == BFD_RELOC_MIPS_JMP
7879 || reloc_type[0] == BFD_RELOC_GPREL16
7880 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
7881 || reloc_type[0] == BFD_RELOC_GPREL32
7882 || reloc_type[0] == BFD_RELOC_64
7883 || reloc_type[0] == BFD_RELOC_CTOR
7884 || reloc_type[0] == BFD_RELOC_MIPS_SUB
7885 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
7886 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
7887 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
7888 || reloc_type[0] == BFD_RELOC_MIPS_REL16
7889 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
7890 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
7891 || hi16_reloc_p (reloc_type[0])
7892 || lo16_reloc_p (reloc_type[0])))
7893 ip->fixp[0]->fx_no_overflow = 1;
7894
ddaf2c41
MR
7895 /* These relocations can have an addend that won't fit in 2 octets. */
7896 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
7897 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
7898 ip->fixp[0]->fx_no_overflow = 1;
7899
9fe77896
RS
7900 if (mips_relax.sequence)
7901 {
7902 if (mips_relax.first_fixup == 0)
7903 mips_relax.first_fixup = ip->fixp[0];
7904 }
7905 else if (reloc_needs_lo_p (*reloc_type))
7906 {
7907 struct mips_hi_fixup *hi_fixup;
7908
7909 /* Reuse the last entry if it already has a matching %lo. */
7910 hi_fixup = mips_hi_fixup_list;
7911 if (hi_fixup == 0
7912 || !fixup_has_matching_lo_p (hi_fixup->fixp))
4d7206a2 7913 {
325801bd 7914 hi_fixup = XNEW (struct mips_hi_fixup);
9fe77896
RS
7915 hi_fixup->next = mips_hi_fixup_list;
7916 mips_hi_fixup_list = hi_fixup;
4d7206a2 7917 }
9fe77896
RS
7918 hi_fixup->fixp = ip->fixp[0];
7919 hi_fixup->seg = now_seg;
7920 }
252b5132 7921
9fe77896
RS
7922 /* Add fixups for the second and third relocations, if given.
7923 Note that the ABI allows the second relocation to be
7924 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
7925 moment we only use RSS_UNDEF, but we could add support
7926 for the others if it ever becomes necessary. */
7927 for (i = 1; i < 3; i++)
7928 if (reloc_type[i] != BFD_RELOC_UNUSED)
7929 {
7930 ip->fixp[i] = fix_new (ip->frag, ip->where,
7931 ip->fixp[0]->fx_size, NULL, 0,
df58fc94 7932 FALSE, final_type[i]);
f6688943 7933
9fe77896
RS
7934 /* Use fx_tcbit to mark compound relocs. */
7935 ip->fixp[0]->fx_tcbit = 1;
7936 ip->fixp[i]->fx_tcbit = 1;
7937 }
252b5132 7938 }
252b5132
RH
7939
7940 /* Update the register mask information. */
4c260379
RS
7941 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
7942 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
252b5132 7943
a4e06468 7944 switch (method)
252b5132 7945 {
a4e06468
RS
7946 case APPEND_ADD:
7947 insert_into_history (0, 1, ip);
7948 break;
7949
7950 case APPEND_ADD_WITH_NOP:
14fe068b
RS
7951 {
7952 struct mips_cl_insn *nop;
7953
7954 insert_into_history (0, 1, ip);
7955 nop = get_delay_slot_nop (ip);
7956 add_fixed_insn (nop);
7957 insert_into_history (0, 1, nop);
7958 if (mips_relax.sequence)
7959 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
7960 }
a4e06468
RS
7961 break;
7962
7963 case APPEND_ADD_COMPACT:
7964 /* Convert MIPS16 jr/jalr into a "compact" jump. */
7bd374a4
MR
7965 if (mips_opts.mips16)
7966 {
7967 ip->insn_opcode |= 0x0080;
7968 find_altered_mips16_opcode (ip);
7969 }
7970 /* Convert microMIPS instructions. */
7971 else if (mips_opts.micromips)
7972 {
7973 /* jr16->jrc */
7974 if ((ip->insn_opcode & 0xffe0) == 0x4580)
7975 ip->insn_opcode |= 0x0020;
7976 /* b16->bc */
7977 else if ((ip->insn_opcode & 0xfc00) == 0xcc00)
7978 ip->insn_opcode = 0x40e00000;
7979 /* beqz16->beqzc, bnez16->bnezc */
7980 else if ((ip->insn_opcode & 0xdc00) == 0x8c00)
7981 {
7982 unsigned long regno;
7983
7984 regno = ip->insn_opcode >> MICROMIPSOP_SH_MD;
7985 regno &= MICROMIPSOP_MASK_MD;
7986 regno = micromips_to_32_reg_d_map[regno];
7987 ip->insn_opcode = (((ip->insn_opcode << 9) & 0x00400000)
7988 | (regno << MICROMIPSOP_SH_RS)
7989 | 0x40a00000) ^ 0x00400000;
7990 }
7991 /* beqz->beqzc, bnez->bnezc */
7992 else if ((ip->insn_opcode & 0xdfe00000) == 0x94000000)
7993 ip->insn_opcode = ((ip->insn_opcode & 0x001f0000)
7994 | ((ip->insn_opcode >> 7) & 0x00400000)
7995 | 0x40a00000) ^ 0x00400000;
7996 /* beq $0->beqzc, bne $0->bnezc */
7997 else if ((ip->insn_opcode & 0xdc1f0000) == 0x94000000)
7998 ip->insn_opcode = (((ip->insn_opcode >>
7999 (MICROMIPSOP_SH_RT - MICROMIPSOP_SH_RS))
8000 & (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS))
8001 | ((ip->insn_opcode >> 7) & 0x00400000)
8002 | 0x40a00000) ^ 0x00400000;
8003 else
8004 abort ();
8005 find_altered_micromips_opcode (ip);
8006 }
8007 else
8008 abort ();
a4e06468
RS
8009 install_insn (ip);
8010 insert_into_history (0, 1, ip);
8011 break;
8012
8013 case APPEND_SWAP:
8014 {
8015 struct mips_cl_insn delay = history[0];
99e7978b
MF
8016
8017 if (relaxed_branch || delay.frag != ip->frag)
a4e06468
RS
8018 {
8019 /* Add the delay slot instruction to the end of the
8020 current frag and shrink the fixed part of the
8021 original frag. If the branch occupies the tail of
8022 the latter, move it backwards to cover the gap. */
2b0c8b40 8023 delay.frag->fr_fix -= branch_disp;
a4e06468 8024 if (delay.frag == ip->frag)
2b0c8b40 8025 move_insn (ip, ip->frag, ip->where - branch_disp);
a4e06468
RS
8026 add_fixed_insn (&delay);
8027 }
8028 else
8029 {
5e35670b
MR
8030 /* If this is not a relaxed branch and we are in the
8031 same frag, then just swap the instructions. */
8032 move_insn (ip, delay.frag, delay.where);
8033 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
a4e06468
RS
8034 }
8035 history[0] = *ip;
8036 delay.fixed_p = 1;
8037 insert_into_history (0, 1, &delay);
8038 }
8039 break;
252b5132
RH
8040 }
8041
13408f1e 8042 /* If we have just completed an unconditional branch, clear the history. */
11625dd8
RS
8043 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
8044 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
e407c74b
NC
8045 {
8046 unsigned int i;
8047
79850f26 8048 mips_no_prev_insn ();
13408f1e 8049
e407c74b 8050 for (i = 0; i < ARRAY_SIZE (history); i++)
79850f26 8051 history[i].cleared_p = 1;
e407c74b
NC
8052 }
8053
df58fc94
RS
8054 /* We need to emit a label at the end of branch-likely macros. */
8055 if (emit_branch_likely_macro)
8056 {
8057 emit_branch_likely_macro = FALSE;
8058 micromips_add_label ();
8059 }
8060
252b5132
RH
8061 /* We just output an insn, so the next one doesn't have a label. */
8062 mips_clear_insn_labels ();
252b5132
RH
8063}
8064
e407c74b
NC
8065/* Forget that there was any previous instruction or label.
8066 When BRANCH is true, the branch history is also flushed. */
252b5132
RH
8067
8068static void
7d10b47d 8069mips_no_prev_insn (void)
252b5132 8070{
7d10b47d
RS
8071 prev_nop_frag = NULL;
8072 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
8073 mips_clear_insn_labels ();
8074}
8075
7d10b47d
RS
8076/* This function must be called before we emit something other than
8077 instructions. It is like mips_no_prev_insn except that it inserts
8078 any NOPS that might be needed by previous instructions. */
252b5132 8079
7d10b47d
RS
8080void
8081mips_emit_delays (void)
252b5132
RH
8082{
8083 if (! mips_opts.noreorder)
8084 {
932d1a1b 8085 int nops = nops_for_insn (0, history, NULL);
252b5132
RH
8086 if (nops > 0)
8087 {
7d10b47d
RS
8088 while (nops-- > 0)
8089 add_fixed_insn (NOP_INSN);
462427c4 8090 mips_move_text_labels ();
7d10b47d
RS
8091 }
8092 }
8093 mips_no_prev_insn ();
8094}
8095
8096/* Start a (possibly nested) noreorder block. */
8097
8098static void
8099start_noreorder (void)
8100{
8101 if (mips_opts.noreorder == 0)
8102 {
8103 unsigned int i;
8104 int nops;
8105
8106 /* None of the instructions before the .set noreorder can be moved. */
8107 for (i = 0; i < ARRAY_SIZE (history); i++)
8108 history[i].fixed_p = 1;
8109
8110 /* Insert any nops that might be needed between the .set noreorder
8111 block and the previous instructions. We will later remove any
8112 nops that turn out not to be needed. */
932d1a1b 8113 nops = nops_for_insn (0, history, NULL);
7d10b47d
RS
8114 if (nops > 0)
8115 {
8116 if (mips_optimize != 0)
252b5132
RH
8117 {
8118 /* Record the frag which holds the nop instructions, so
8119 that we can remove them if we don't need them. */
df58fc94 8120 frag_grow (nops * NOP_INSN_SIZE);
252b5132
RH
8121 prev_nop_frag = frag_now;
8122 prev_nop_frag_holds = nops;
8123 prev_nop_frag_required = 0;
8124 prev_nop_frag_since = 0;
8125 }
8126
8127 for (; nops > 0; --nops)
1e915849 8128 add_fixed_insn (NOP_INSN);
252b5132 8129
7d10b47d
RS
8130 /* Move on to a new frag, so that it is safe to simply
8131 decrease the size of prev_nop_frag. */
8132 frag_wane (frag_now);
8133 frag_new (0);
462427c4 8134 mips_move_text_labels ();
252b5132 8135 }
df58fc94 8136 mips_mark_labels ();
7d10b47d 8137 mips_clear_insn_labels ();
252b5132 8138 }
7d10b47d
RS
8139 mips_opts.noreorder++;
8140 mips_any_noreorder = 1;
8141}
252b5132 8142
7d10b47d 8143/* End a nested noreorder block. */
252b5132 8144
7d10b47d
RS
8145static void
8146end_noreorder (void)
8147{
8148 mips_opts.noreorder--;
8149 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
8150 {
8151 /* Commit to inserting prev_nop_frag_required nops and go back to
8152 handling nop insertion the .set reorder way. */
8153 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
df58fc94 8154 * NOP_INSN_SIZE);
7d10b47d
RS
8155 insert_into_history (prev_nop_frag_since,
8156 prev_nop_frag_required, NOP_INSN);
8157 prev_nop_frag = NULL;
8158 }
252b5132
RH
8159}
8160
97d87491
RS
8161/* Sign-extend 32-bit mode constants that have bit 31 set and all
8162 higher bits unset. */
8163
8164static void
8165normalize_constant_expr (expressionS *ex)
8166{
8167 if (ex->X_op == O_constant
8168 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
8169 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
8170 - 0x80000000);
8171}
8172
8173/* Sign-extend 32-bit mode address offsets that have bit 31 set and
8174 all higher bits unset. */
8175
8176static void
8177normalize_address_expr (expressionS *ex)
8178{
8179 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
8180 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
8181 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
8182 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
8183 - 0x80000000);
8184}
8185
8186/* Try to match TOKENS against OPCODE, storing the result in INSN.
8187 Return true if the match was successful.
8188
8189 OPCODE_EXTRA is a value that should be ORed into the opcode
8190 (used for VU0 channel suffixes, etc.). MORE_ALTS is true if
8191 there are more alternatives after OPCODE and SOFT_MATCH is
8192 as for mips_arg_info. */
8193
8194static bfd_boolean
8195match_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
8196 struct mips_operand_token *tokens, unsigned int opcode_extra,
60f20e8b 8197 bfd_boolean lax_match, bfd_boolean complete_p)
97d87491
RS
8198{
8199 const char *args;
8200 struct mips_arg_info arg;
8201 const struct mips_operand *operand;
8202 char c;
8203
8204 imm_expr.X_op = O_absent;
97d87491
RS
8205 offset_expr.X_op = O_absent;
8206 offset_reloc[0] = BFD_RELOC_UNUSED;
8207 offset_reloc[1] = BFD_RELOC_UNUSED;
8208 offset_reloc[2] = BFD_RELOC_UNUSED;
8209
8210 create_insn (insn, opcode);
60f20e8b
RS
8211 /* When no opcode suffix is specified, assume ".xyzw". */
8212 if ((opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0 && opcode_extra == 0)
8213 insn->insn_opcode |= 0xf << mips_vu0_channel_mask.lsb;
8214 else
8215 insn->insn_opcode |= opcode_extra;
97d87491
RS
8216 memset (&arg, 0, sizeof (arg));
8217 arg.insn = insn;
8218 arg.token = tokens;
8219 arg.argnum = 1;
8220 arg.last_regno = ILLEGAL_REG;
8221 arg.dest_regno = ILLEGAL_REG;
60f20e8b 8222 arg.lax_match = lax_match;
97d87491
RS
8223 for (args = opcode->args;; ++args)
8224 {
8225 if (arg.token->type == OT_END)
8226 {
8227 /* Handle unary instructions in which only one operand is given.
8228 The source is then the same as the destination. */
8229 if (arg.opnum == 1 && *args == ',')
8230 {
8231 operand = (mips_opts.micromips
8232 ? decode_micromips_operand (args + 1)
8233 : decode_mips_operand (args + 1));
8234 if (operand && mips_optional_operand_p (operand))
8235 {
8236 arg.token = tokens;
8237 arg.argnum = 1;
8238 continue;
8239 }
8240 }
8241
8242 /* Treat elided base registers as $0. */
8243 if (strcmp (args, "(b)") == 0)
8244 args += 3;
8245
8246 if (args[0] == '+')
8247 switch (args[1])
8248 {
8249 case 'K':
8250 case 'N':
8251 /* The register suffix is optional. */
8252 args += 2;
8253 break;
8254 }
8255
8256 /* Fail the match if there were too few operands. */
8257 if (*args)
8258 return FALSE;
8259
8260 /* Successful match. */
60f20e8b
RS
8261 if (!complete_p)
8262 return TRUE;
e3de51ce 8263 clear_insn_error ();
97d87491
RS
8264 if (arg.dest_regno == arg.last_regno
8265 && strncmp (insn->insn_mo->name, "jalr", 4) == 0)
8266 {
8267 if (arg.opnum == 2)
e3de51ce 8268 set_insn_error
1661c76c 8269 (0, _("source and destination must be different"));
97d87491 8270 else if (arg.last_regno == 31)
e3de51ce 8271 set_insn_error
1661c76c 8272 (0, _("a destination register must be supplied"));
97d87491 8273 }
173d3447
CF
8274 else if (arg.last_regno == 31
8275 && (strncmp (insn->insn_mo->name, "bltzal", 6) == 0
8276 || strncmp (insn->insn_mo->name, "bgezal", 6) == 0))
8277 set_insn_error (0, _("the source register must not be $31"));
97d87491
RS
8278 check_completed_insn (&arg);
8279 return TRUE;
8280 }
8281
8282 /* Fail the match if the line has too many operands. */
8283 if (*args == 0)
8284 return FALSE;
8285
8286 /* Handle characters that need to match exactly. */
8287 if (*args == '(' || *args == ')' || *args == ',')
8288 {
8289 if (match_char (&arg, *args))
8290 continue;
8291 return FALSE;
8292 }
8293 if (*args == '#')
8294 {
8295 ++args;
8296 if (arg.token->type == OT_DOUBLE_CHAR
8297 && arg.token->u.ch == *args)
8298 {
8299 ++arg.token;
8300 continue;
8301 }
8302 return FALSE;
8303 }
8304
8305 /* Handle special macro operands. Work out the properties of
8306 other operands. */
8307 arg.opnum += 1;
97d87491
RS
8308 switch (*args)
8309 {
7361da2c
AB
8310 case '-':
8311 switch (args[1])
8312 {
8313 case 'A':
8314 *offset_reloc = BFD_RELOC_MIPS_19_PCREL_S2;
8315 break;
8316
8317 case 'B':
8318 *offset_reloc = BFD_RELOC_MIPS_18_PCREL_S3;
8319 break;
8320 }
8321 break;
8322
97d87491
RS
8323 case '+':
8324 switch (args[1])
8325 {
97d87491
RS
8326 case 'i':
8327 *offset_reloc = BFD_RELOC_MIPS_JMP;
8328 break;
7361da2c
AB
8329
8330 case '\'':
8331 *offset_reloc = BFD_RELOC_MIPS_26_PCREL_S2;
8332 break;
8333
8334 case '\"':
8335 *offset_reloc = BFD_RELOC_MIPS_21_PCREL_S2;
8336 break;
97d87491
RS
8337 }
8338 break;
8339
97d87491 8340 case 'I':
1a00e612
RS
8341 if (!match_const_int (&arg, &imm_expr.X_add_number))
8342 return FALSE;
8343 imm_expr.X_op = O_constant;
bad1aba3 8344 if (GPR_SIZE == 32)
97d87491
RS
8345 normalize_constant_expr (&imm_expr);
8346 continue;
8347
8348 case 'A':
8349 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8350 {
8351 /* Assume that the offset has been elided and that what
8352 we saw was a base register. The match will fail later
8353 if that assumption turns out to be wrong. */
8354 offset_expr.X_op = O_constant;
8355 offset_expr.X_add_number = 0;
8356 }
97d87491 8357 else
1a00e612
RS
8358 {
8359 if (!match_expression (&arg, &offset_expr, offset_reloc))
8360 return FALSE;
8361 normalize_address_expr (&offset_expr);
8362 }
97d87491
RS
8363 continue;
8364
8365 case 'F':
8366 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8367 8, TRUE))
1a00e612 8368 return FALSE;
97d87491
RS
8369 continue;
8370
8371 case 'L':
8372 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8373 8, FALSE))
1a00e612 8374 return FALSE;
97d87491
RS
8375 continue;
8376
8377 case 'f':
8378 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8379 4, TRUE))
1a00e612 8380 return FALSE;
97d87491
RS
8381 continue;
8382
8383 case 'l':
8384 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8385 4, FALSE))
1a00e612 8386 return FALSE;
97d87491
RS
8387 continue;
8388
97d87491
RS
8389 case 'p':
8390 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8391 break;
8392
8393 case 'a':
8394 *offset_reloc = BFD_RELOC_MIPS_JMP;
8395 break;
8396
8397 case 'm':
8398 gas_assert (mips_opts.micromips);
8399 c = args[1];
8400 switch (c)
8401 {
8402 case 'D':
8403 case 'E':
8404 if (!forced_insn_length)
8405 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
8406 else if (c == 'D')
8407 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
8408 else
8409 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
8410 break;
8411 }
8412 break;
8413 }
8414
8415 operand = (mips_opts.micromips
8416 ? decode_micromips_operand (args)
8417 : decode_mips_operand (args));
8418 if (!operand)
8419 abort ();
8420
8421 /* Skip prefixes. */
7361da2c 8422 if (*args == '+' || *args == 'm' || *args == '-')
97d87491
RS
8423 args++;
8424
8425 if (mips_optional_operand_p (operand)
8426 && args[1] == ','
8427 && (arg.token[0].type != OT_REG
8428 || arg.token[1].type == OT_END))
8429 {
8430 /* Assume that the register has been elided and is the
8431 same as the first operand. */
8432 arg.token = tokens;
8433 arg.argnum = 1;
8434 }
8435
8436 if (!match_operand (&arg, operand))
8437 return FALSE;
8438 }
8439}
8440
8441/* Like match_insn, but for MIPS16. */
8442
8443static bfd_boolean
8444match_mips16_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
1a00e612 8445 struct mips_operand_token *tokens)
97d87491
RS
8446{
8447 const char *args;
8448 const struct mips_operand *operand;
8449 const struct mips_operand *ext_operand;
82d808ed 8450 bfd_boolean pcrel = FALSE;
7fd53920 8451 int required_insn_length;
97d87491
RS
8452 struct mips_arg_info arg;
8453 int relax_char;
8454
7fd53920
MR
8455 if (forced_insn_length)
8456 required_insn_length = forced_insn_length;
8457 else if (mips_opts.noautoextend && !mips_opcode_32bit_p (opcode))
8458 required_insn_length = 2;
8459 else
8460 required_insn_length = 0;
8461
97d87491
RS
8462 create_insn (insn, opcode);
8463 imm_expr.X_op = O_absent;
97d87491
RS
8464 offset_expr.X_op = O_absent;
8465 offset_reloc[0] = BFD_RELOC_UNUSED;
8466 offset_reloc[1] = BFD_RELOC_UNUSED;
8467 offset_reloc[2] = BFD_RELOC_UNUSED;
8468 relax_char = 0;
8469
8470 memset (&arg, 0, sizeof (arg));
8471 arg.insn = insn;
8472 arg.token = tokens;
8473 arg.argnum = 1;
8474 arg.last_regno = ILLEGAL_REG;
8475 arg.dest_regno = ILLEGAL_REG;
97d87491
RS
8476 relax_char = 0;
8477 for (args = opcode->args;; ++args)
8478 {
8479 int c;
8480
8481 if (arg.token->type == OT_END)
8482 {
8483 offsetT value;
8484
8485 /* Handle unary instructions in which only one operand is given.
8486 The source is then the same as the destination. */
8487 if (arg.opnum == 1 && *args == ',')
8488 {
8489 operand = decode_mips16_operand (args[1], FALSE);
8490 if (operand && mips_optional_operand_p (operand))
8491 {
8492 arg.token = tokens;
8493 arg.argnum = 1;
8494 continue;
8495 }
8496 }
8497
8498 /* Fail the match if there were too few operands. */
8499 if (*args)
8500 return FALSE;
8501
8502 /* Successful match. Stuff the immediate value in now, if
8503 we can. */
e3de51ce 8504 clear_insn_error ();
97d87491
RS
8505 if (opcode->pinfo == INSN_MACRO)
8506 {
8507 gas_assert (relax_char == 0 || relax_char == 'p');
8508 gas_assert (*offset_reloc == BFD_RELOC_UNUSED);
8509 }
8510 else if (relax_char
8511 && offset_expr.X_op == O_constant
82d808ed 8512 && !pcrel
97d87491
RS
8513 && calculate_reloc (*offset_reloc,
8514 offset_expr.X_add_number,
8515 &value))
8516 {
8517 mips16_immed (NULL, 0, relax_char, *offset_reloc, value,
7fd53920 8518 required_insn_length, &insn->insn_opcode);
97d87491
RS
8519 offset_expr.X_op = O_absent;
8520 *offset_reloc = BFD_RELOC_UNUSED;
8521 }
8522 else if (relax_char && *offset_reloc != BFD_RELOC_UNUSED)
8523 {
7fd53920 8524 if (required_insn_length == 2)
e3de51ce 8525 set_insn_error (0, _("invalid unextended operand value"));
25499ac7 8526 else if (!mips_opcode_32bit_p (opcode))
1da43acc
MR
8527 {
8528 forced_insn_length = 4;
8529 insn->insn_opcode |= MIPS16_EXTEND;
8530 }
97d87491
RS
8531 }
8532 else if (relax_char)
8533 *offset_reloc = (int) BFD_RELOC_UNUSED + relax_char;
8534
8535 check_completed_insn (&arg);
8536 return TRUE;
8537 }
8538
8539 /* Fail the match if the line has too many operands. */
8540 if (*args == 0)
8541 return FALSE;
8542
8543 /* Handle characters that need to match exactly. */
8544 if (*args == '(' || *args == ')' || *args == ',')
8545 {
8546 if (match_char (&arg, *args))
8547 continue;
8548 return FALSE;
8549 }
8550
8551 arg.opnum += 1;
8552 c = *args;
8553 switch (c)
8554 {
8555 case 'p':
8556 case 'q':
8557 case 'A':
8558 case 'B':
8559 case 'E':
25499ac7
MR
8560 case 'V':
8561 case 'u':
97d87491
RS
8562 relax_char = c;
8563 break;
8564
8565 case 'I':
1a00e612
RS
8566 if (!match_const_int (&arg, &imm_expr.X_add_number))
8567 return FALSE;
8568 imm_expr.X_op = O_constant;
bad1aba3 8569 if (GPR_SIZE == 32)
97d87491
RS
8570 normalize_constant_expr (&imm_expr);
8571 continue;
8572
8573 case 'a':
8574 case 'i':
8575 *offset_reloc = BFD_RELOC_MIPS16_JMP;
97d87491
RS
8576 break;
8577 }
8578
7fd53920 8579 operand = decode_mips16_operand (c, mips_opcode_32bit_p (opcode));
97d87491
RS
8580 if (!operand)
8581 abort ();
8582
82d808ed
MR
8583 if (operand->type == OP_PCREL)
8584 pcrel = TRUE;
8585 else
97d87491
RS
8586 {
8587 ext_operand = decode_mips16_operand (c, TRUE);
8588 if (operand != ext_operand)
8589 {
8590 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8591 {
8592 offset_expr.X_op = O_constant;
8593 offset_expr.X_add_number = 0;
8594 relax_char = c;
8595 continue;
8596 }
8597
1a7bf198 8598 if (!match_expression (&arg, &offset_expr, offset_reloc))
97d87491
RS
8599 return FALSE;
8600
8601 /* '8' is used for SLTI(U) and has traditionally not
8602 been allowed to take relocation operators. */
8603 if (offset_reloc[0] != BFD_RELOC_UNUSED
8604 && (ext_operand->size != 16 || c == '8'))
e295202f
MR
8605 {
8606 match_not_constant (&arg);
8607 return FALSE;
8608 }
97d87491 8609
c96425c5
MR
8610 if (offset_expr.X_op == O_big)
8611 {
8612 match_out_of_range (&arg);
8613 return FALSE;
8614 }
8615
97d87491
RS
8616 relax_char = c;
8617 continue;
8618 }
8619 }
8620
8621 if (mips_optional_operand_p (operand)
8622 && args[1] == ','
8623 && (arg.token[0].type != OT_REG
8624 || arg.token[1].type == OT_END))
8625 {
8626 /* Assume that the register has been elided and is the
8627 same as the first operand. */
8628 arg.token = tokens;
8629 arg.argnum = 1;
8630 }
8631
8632 if (!match_operand (&arg, operand))
8633 return FALSE;
8634 }
8635}
8636
60f20e8b
RS
8637/* Record that the current instruction is invalid for the current ISA. */
8638
8639static void
8640match_invalid_for_isa (void)
8641{
8642 set_insn_error_ss
1661c76c 8643 (0, _("opcode not supported on this processor: %s (%s)"),
60f20e8b
RS
8644 mips_cpu_info_from_arch (mips_opts.arch)->name,
8645 mips_cpu_info_from_isa (mips_opts.isa)->name);
8646}
8647
8648/* Try to match TOKENS against a series of opcode entries, starting at FIRST.
8649 Return true if a definite match or failure was found, storing any match
8650 in INSN. OPCODE_EXTRA is a value that should be ORed into the opcode
8651 (to handle things like VU0 suffixes). LAX_MATCH is true if we have already
8652 tried and failed to match under normal conditions and now want to try a
8653 more relaxed match. */
8654
8655static bfd_boolean
8656match_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8657 const struct mips_opcode *past, struct mips_operand_token *tokens,
8658 int opcode_extra, bfd_boolean lax_match)
8659{
8660 const struct mips_opcode *opcode;
8661 const struct mips_opcode *invalid_delay_slot;
8662 bfd_boolean seen_valid_for_isa, seen_valid_for_size;
8663
8664 /* Search for a match, ignoring alternatives that don't satisfy the
8665 current ISA or forced_length. */
8666 invalid_delay_slot = 0;
8667 seen_valid_for_isa = FALSE;
8668 seen_valid_for_size = FALSE;
8669 opcode = first;
8670 do
8671 {
8672 gas_assert (strcmp (opcode->name, first->name) == 0);
8673 if (is_opcode_valid (opcode))
8674 {
8675 seen_valid_for_isa = TRUE;
8676 if (is_size_valid (opcode))
8677 {
8678 bfd_boolean delay_slot_ok;
8679
8680 seen_valid_for_size = TRUE;
8681 delay_slot_ok = is_delay_slot_valid (opcode);
8682 if (match_insn (insn, opcode, tokens, opcode_extra,
8683 lax_match, delay_slot_ok))
8684 {
8685 if (!delay_slot_ok)
8686 {
8687 if (!invalid_delay_slot)
8688 invalid_delay_slot = opcode;
8689 }
8690 else
8691 return TRUE;
8692 }
8693 }
8694 }
8695 ++opcode;
8696 }
8697 while (opcode < past && strcmp (opcode->name, first->name) == 0);
8698
8699 /* If the only matches we found had the wrong length for the delay slot,
8700 pick the first such match. We'll issue an appropriate warning later. */
8701 if (invalid_delay_slot)
8702 {
8703 if (match_insn (insn, invalid_delay_slot, tokens, opcode_extra,
8704 lax_match, TRUE))
8705 return TRUE;
8706 abort ();
8707 }
8708
8709 /* Handle the case where we didn't try to match an instruction because
8710 all the alternatives were incompatible with the current ISA. */
8711 if (!seen_valid_for_isa)
8712 {
8713 match_invalid_for_isa ();
8714 return TRUE;
8715 }
8716
8717 /* Handle the case where we didn't try to match an instruction because
8718 all the alternatives were of the wrong size. */
8719 if (!seen_valid_for_size)
8720 {
8721 if (mips_opts.insn32)
1661c76c 8722 set_insn_error (0, _("opcode not supported in the `insn32' mode"));
60f20e8b
RS
8723 else
8724 set_insn_error_i
1661c76c 8725 (0, _("unrecognized %d-bit version of microMIPS opcode"),
60f20e8b
RS
8726 8 * forced_insn_length);
8727 return TRUE;
8728 }
8729
8730 return FALSE;
8731}
8732
8733/* Like match_insns, but for MIPS16. */
8734
8735static bfd_boolean
8736match_mips16_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8737 struct mips_operand_token *tokens)
8738{
8739 const struct mips_opcode *opcode;
8740 bfd_boolean seen_valid_for_isa;
7fd53920 8741 bfd_boolean seen_valid_for_size;
60f20e8b
RS
8742
8743 /* Search for a match, ignoring alternatives that don't satisfy the
8744 current ISA. There are no separate entries for extended forms so
8745 we deal with forced_length later. */
8746 seen_valid_for_isa = FALSE;
7fd53920 8747 seen_valid_for_size = FALSE;
60f20e8b
RS
8748 opcode = first;
8749 do
8750 {
8751 gas_assert (strcmp (opcode->name, first->name) == 0);
8752 if (is_opcode_valid_16 (opcode))
8753 {
8754 seen_valid_for_isa = TRUE;
7fd53920
MR
8755 if (is_size_valid_16 (opcode))
8756 {
8757 seen_valid_for_size = TRUE;
8758 if (match_mips16_insn (insn, opcode, tokens))
8759 return TRUE;
8760 }
60f20e8b
RS
8761 }
8762 ++opcode;
8763 }
8764 while (opcode < &mips16_opcodes[bfd_mips16_num_opcodes]
8765 && strcmp (opcode->name, first->name) == 0);
8766
8767 /* Handle the case where we didn't try to match an instruction because
8768 all the alternatives were incompatible with the current ISA. */
8769 if (!seen_valid_for_isa)
8770 {
8771 match_invalid_for_isa ();
8772 return TRUE;
8773 }
8774
7fd53920
MR
8775 /* Handle the case where we didn't try to match an instruction because
8776 all the alternatives were of the wrong size. */
8777 if (!seen_valid_for_size)
8778 {
8779 if (forced_insn_length == 2)
8780 set_insn_error
8781 (0, _("unrecognized unextended version of MIPS16 opcode"));
8782 else
8783 set_insn_error
8784 (0, _("unrecognized extended version of MIPS16 opcode"));
8785 return TRUE;
8786 }
8787
60f20e8b
RS
8788 return FALSE;
8789}
8790
584892a6
RS
8791/* Set up global variables for the start of a new macro. */
8792
8793static void
8794macro_start (void)
8795{
8796 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
df58fc94
RS
8797 memset (&mips_macro_warning.first_insn_sizes, 0,
8798 sizeof (mips_macro_warning.first_insn_sizes));
8799 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
584892a6 8800 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
11625dd8 8801 && delayed_branch_p (&history[0]));
7bd374a4
MR
8802 if (history[0].frag
8803 && history[0].frag->fr_type == rs_machine_dependent
8804 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
8805 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
8806 mips_macro_warning.delay_slot_length = 0;
8807 else
8808 switch (history[0].insn_mo->pinfo2
8809 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
8810 {
8811 case INSN2_BRANCH_DELAY_32BIT:
8812 mips_macro_warning.delay_slot_length = 4;
8813 break;
8814 case INSN2_BRANCH_DELAY_16BIT:
8815 mips_macro_warning.delay_slot_length = 2;
8816 break;
8817 default:
8818 mips_macro_warning.delay_slot_length = 0;
8819 break;
8820 }
df58fc94 8821 mips_macro_warning.first_frag = NULL;
584892a6
RS
8822}
8823
df58fc94
RS
8824/* Given that a macro is longer than one instruction or of the wrong size,
8825 return the appropriate warning for it. Return null if no warning is
8826 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
8827 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
8828 and RELAX_NOMACRO. */
584892a6
RS
8829
8830static const char *
8831macro_warning (relax_substateT subtype)
8832{
8833 if (subtype & RELAX_DELAY_SLOT)
1661c76c 8834 return _("macro instruction expanded into multiple instructions"
584892a6
RS
8835 " in a branch delay slot");
8836 else if (subtype & RELAX_NOMACRO)
1661c76c 8837 return _("macro instruction expanded into multiple instructions");
df58fc94
RS
8838 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
8839 | RELAX_DELAY_SLOT_SIZE_SECOND))
8840 return ((subtype & RELAX_DELAY_SLOT_16BIT)
1661c76c 8841 ? _("macro instruction expanded into a wrong size instruction"
df58fc94 8842 " in a 16-bit branch delay slot")
1661c76c 8843 : _("macro instruction expanded into a wrong size instruction"
df58fc94 8844 " in a 32-bit branch delay slot"));
584892a6
RS
8845 else
8846 return 0;
8847}
8848
8849/* Finish up a macro. Emit warnings as appropriate. */
8850
8851static void
8852macro_end (void)
8853{
df58fc94
RS
8854 /* Relaxation warning flags. */
8855 relax_substateT subtype = 0;
8856
8857 /* Check delay slot size requirements. */
8858 if (mips_macro_warning.delay_slot_length == 2)
8859 subtype |= RELAX_DELAY_SLOT_16BIT;
8860 if (mips_macro_warning.delay_slot_length != 0)
584892a6 8861 {
df58fc94
RS
8862 if (mips_macro_warning.delay_slot_length
8863 != mips_macro_warning.first_insn_sizes[0])
8864 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
8865 if (mips_macro_warning.delay_slot_length
8866 != mips_macro_warning.first_insn_sizes[1])
8867 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
8868 }
584892a6 8869
df58fc94
RS
8870 /* Check instruction count requirements. */
8871 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
8872 {
8873 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
584892a6
RS
8874 subtype |= RELAX_SECOND_LONGER;
8875 if (mips_opts.warn_about_macros)
8876 subtype |= RELAX_NOMACRO;
8877 if (mips_macro_warning.delay_slot_p)
8878 subtype |= RELAX_DELAY_SLOT;
df58fc94 8879 }
584892a6 8880
df58fc94
RS
8881 /* If both alternatives fail to fill a delay slot correctly,
8882 emit the warning now. */
8883 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
8884 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
8885 {
8886 relax_substateT s;
8887 const char *msg;
8888
8889 s = subtype & (RELAX_DELAY_SLOT_16BIT
8890 | RELAX_DELAY_SLOT_SIZE_FIRST
8891 | RELAX_DELAY_SLOT_SIZE_SECOND);
8892 msg = macro_warning (s);
8893 if (msg != NULL)
8894 as_warn ("%s", msg);
8895 subtype &= ~s;
8896 }
8897
8898 /* If both implementations are longer than 1 instruction, then emit the
8899 warning now. */
8900 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
8901 {
8902 relax_substateT s;
8903 const char *msg;
8904
8905 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
8906 msg = macro_warning (s);
8907 if (msg != NULL)
8908 as_warn ("%s", msg);
8909 subtype &= ~s;
584892a6 8910 }
df58fc94
RS
8911
8912 /* If any flags still set, then one implementation might need a warning
8913 and the other either will need one of a different kind or none at all.
8914 Pass any remaining flags over to relaxation. */
8915 if (mips_macro_warning.first_frag != NULL)
8916 mips_macro_warning.first_frag->fr_subtype |= subtype;
584892a6
RS
8917}
8918
df58fc94
RS
8919/* Instruction operand formats used in macros that vary between
8920 standard MIPS and microMIPS code. */
8921
833794fc 8922static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
df58fc94
RS
8923static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
8924static const char * const jalr_fmt[2] = { "d,s", "t,s" };
8925static const char * const lui_fmt[2] = { "t,u", "s,u" };
8926static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
833794fc 8927static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
df58fc94
RS
8928static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
8929static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
8930
833794fc 8931#define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
7361da2c
AB
8932#define COP12_FMT (ISA_IS_R6 (mips_opts.isa) ? "E,+:(d)" \
8933 : cop12_fmt[mips_opts.micromips])
df58fc94
RS
8934#define JALR_FMT (jalr_fmt[mips_opts.micromips])
8935#define LUI_FMT (lui_fmt[mips_opts.micromips])
8936#define MEM12_FMT (mem12_fmt[mips_opts.micromips])
7361da2c
AB
8937#define LL_SC_FMT (ISA_IS_R6 (mips_opts.isa) ? "t,+j(b)" \
8938 : mem12_fmt[mips_opts.micromips])
833794fc 8939#define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
df58fc94
RS
8940#define SHFT_FMT (shft_fmt[mips_opts.micromips])
8941#define TRAP_FMT (trap_fmt[mips_opts.micromips])
8942
6e1304d8
RS
8943/* Read a macro's relocation codes from *ARGS and store them in *R.
8944 The first argument in *ARGS will be either the code for a single
8945 relocation or -1 followed by the three codes that make up a
8946 composite relocation. */
8947
8948static void
8949macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
8950{
8951 int i, next;
8952
8953 next = va_arg (*args, int);
8954 if (next >= 0)
8955 r[0] = (bfd_reloc_code_real_type) next;
8956 else
f2ae14a1
RS
8957 {
8958 for (i = 0; i < 3; i++)
8959 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
8960 /* This function is only used for 16-bit relocation fields.
8961 To make the macro code simpler, treat an unrelocated value
8962 in the same way as BFD_RELOC_LO16. */
8963 if (r[0] == BFD_RELOC_UNUSED)
8964 r[0] = BFD_RELOC_LO16;
8965 }
6e1304d8
RS
8966}
8967
252b5132
RH
8968/* Build an instruction created by a macro expansion. This is passed
8969 a pointer to the count of instructions created so far, an
8970 expression, the name of the instruction to build, an operand format
8971 string, and corresponding arguments. */
8972
252b5132 8973static void
67c0d1eb 8974macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 8975{
df58fc94 8976 const struct mips_opcode *mo = NULL;
f6688943 8977 bfd_reloc_code_real_type r[3];
df58fc94 8978 const struct mips_opcode *amo;
e077a1c8 8979 const struct mips_operand *operand;
df58fc94
RS
8980 struct hash_control *hash;
8981 struct mips_cl_insn insn;
252b5132 8982 va_list args;
e077a1c8 8983 unsigned int uval;
252b5132 8984
252b5132 8985 va_start (args, fmt);
252b5132 8986
252b5132
RH
8987 if (mips_opts.mips16)
8988 {
03ea81db 8989 mips16_macro_build (ep, name, fmt, &args);
252b5132
RH
8990 va_end (args);
8991 return;
8992 }
8993
f6688943
TS
8994 r[0] = BFD_RELOC_UNUSED;
8995 r[1] = BFD_RELOC_UNUSED;
8996 r[2] = BFD_RELOC_UNUSED;
df58fc94
RS
8997 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
8998 amo = (struct mips_opcode *) hash_find (hash, name);
8999 gas_assert (amo);
9000 gas_assert (strcmp (name, amo->name) == 0);
1e915849 9001
df58fc94 9002 do
8b082fb1
TS
9003 {
9004 /* Search until we get a match for NAME. It is assumed here that
df58fc94 9005 macros will never generate MDMX, MIPS-3D, or MT instructions.
33eaf5de 9006 We try to match an instruction that fulfills the branch delay
df58fc94
RS
9007 slot instruction length requirement (if any) of the previous
9008 instruction. While doing this we record the first instruction
9009 seen that matches all the other conditions and use it anyway
9010 if the requirement cannot be met; we will issue an appropriate
9011 warning later on. */
9012 if (strcmp (fmt, amo->args) == 0
9013 && amo->pinfo != INSN_MACRO
9014 && is_opcode_valid (amo)
9015 && is_size_valid (amo))
9016 {
9017 if (is_delay_slot_valid (amo))
9018 {
9019 mo = amo;
9020 break;
9021 }
9022 else if (!mo)
9023 mo = amo;
9024 }
8b082fb1 9025
df58fc94
RS
9026 ++amo;
9027 gas_assert (amo->name);
252b5132 9028 }
df58fc94 9029 while (strcmp (name, amo->name) == 0);
252b5132 9030
df58fc94 9031 gas_assert (mo);
1e915849 9032 create_insn (&insn, mo);
e077a1c8 9033 for (; *fmt; ++fmt)
252b5132 9034 {
e077a1c8 9035 switch (*fmt)
252b5132 9036 {
252b5132
RH
9037 case ',':
9038 case '(':
9039 case ')':
252b5132 9040 case 'z':
e077a1c8 9041 break;
252b5132
RH
9042
9043 case 'i':
9044 case 'j':
6e1304d8 9045 macro_read_relocs (&args, r);
9c2799c2 9046 gas_assert (*r == BFD_RELOC_GPREL16
e391c024
RS
9047 || *r == BFD_RELOC_MIPS_HIGHER
9048 || *r == BFD_RELOC_HI16_S
9049 || *r == BFD_RELOC_LO16
14c80123
MR
9050 || *r == BFD_RELOC_MIPS_GOT_OFST
9051 || (mips_opts.micromips
9052 && (*r == BFD_RELOC_16
9053 || *r == BFD_RELOC_MIPS_GOT16
9054 || *r == BFD_RELOC_MIPS_CALL16
9055 || *r == BFD_RELOC_MIPS_GOT_HI16
9056 || *r == BFD_RELOC_MIPS_GOT_LO16
9057 || *r == BFD_RELOC_MIPS_CALL_HI16
9058 || *r == BFD_RELOC_MIPS_CALL_LO16
9059 || *r == BFD_RELOC_MIPS_SUB
9060 || *r == BFD_RELOC_MIPS_GOT_PAGE
9061 || *r == BFD_RELOC_MIPS_HIGHEST
9062 || *r == BFD_RELOC_MIPS_GOT_DISP
9063 || *r == BFD_RELOC_MIPS_TLS_GD
9064 || *r == BFD_RELOC_MIPS_TLS_LDM
9065 || *r == BFD_RELOC_MIPS_TLS_DTPREL_HI16
9066 || *r == BFD_RELOC_MIPS_TLS_DTPREL_LO16
9067 || *r == BFD_RELOC_MIPS_TLS_GOTTPREL
9068 || *r == BFD_RELOC_MIPS_TLS_TPREL_HI16
9069 || *r == BFD_RELOC_MIPS_TLS_TPREL_LO16)));
e077a1c8 9070 break;
e391c024
RS
9071
9072 case 'o':
9073 macro_read_relocs (&args, r);
e077a1c8 9074 break;
252b5132
RH
9075
9076 case 'u':
6e1304d8 9077 macro_read_relocs (&args, r);
9c2799c2 9078 gas_assert (ep != NULL
90ecf173
MR
9079 && (ep->X_op == O_constant
9080 || (ep->X_op == O_symbol
9081 && (*r == BFD_RELOC_MIPS_HIGHEST
9082 || *r == BFD_RELOC_HI16_S
9083 || *r == BFD_RELOC_HI16
9084 || *r == BFD_RELOC_GPREL16
9085 || *r == BFD_RELOC_MIPS_GOT_HI16
9086 || *r == BFD_RELOC_MIPS_CALL_HI16))));
e077a1c8 9087 break;
252b5132
RH
9088
9089 case 'p':
9c2799c2 9090 gas_assert (ep != NULL);
bad36eac 9091
252b5132
RH
9092 /*
9093 * This allows macro() to pass an immediate expression for
9094 * creating short branches without creating a symbol.
bad36eac
DJ
9095 *
9096 * We don't allow branch relaxation for these branches, as
9097 * they should only appear in ".set nomacro" anyway.
252b5132
RH
9098 */
9099 if (ep->X_op == O_constant)
9100 {
df58fc94
RS
9101 /* For microMIPS we always use relocations for branches.
9102 So we should not resolve immediate values. */
9103 gas_assert (!mips_opts.micromips);
9104
bad36eac
DJ
9105 if ((ep->X_add_number & 3) != 0)
9106 as_bad (_("branch to misaligned address (0x%lx)"),
9107 (unsigned long) ep->X_add_number);
9108 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
9109 as_bad (_("branch address range overflow (0x%lx)"),
9110 (unsigned long) ep->X_add_number);
252b5132
RH
9111 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
9112 ep = NULL;
9113 }
9114 else
0b25d3e6 9115 *r = BFD_RELOC_16_PCREL_S2;
e077a1c8 9116 break;
252b5132
RH
9117
9118 case 'a':
9c2799c2 9119 gas_assert (ep != NULL);
f6688943 9120 *r = BFD_RELOC_MIPS_JMP;
e077a1c8 9121 break;
d43b4baf 9122
252b5132 9123 default:
e077a1c8
RS
9124 operand = (mips_opts.micromips
9125 ? decode_micromips_operand (fmt)
9126 : decode_mips_operand (fmt));
9127 if (!operand)
9128 abort ();
9129
9130 uval = va_arg (args, int);
9131 if (operand->type == OP_CLO_CLZ_DEST)
9132 uval |= (uval << 5);
9133 insn_insert_operand (&insn, operand, uval);
9134
7361da2c 9135 if (*fmt == '+' || *fmt == 'm' || *fmt == '-')
e077a1c8
RS
9136 ++fmt;
9137 break;
252b5132 9138 }
252b5132
RH
9139 }
9140 va_end (args);
9c2799c2 9141 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 9142
df58fc94 9143 append_insn (&insn, ep, r, TRUE);
252b5132
RH
9144}
9145
9146static void
67c0d1eb 9147mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
03ea81db 9148 va_list *args)
252b5132 9149{
1e915849 9150 struct mips_opcode *mo;
252b5132 9151 struct mips_cl_insn insn;
e077a1c8 9152 const struct mips_operand *operand;
f6688943
TS
9153 bfd_reloc_code_real_type r[3]
9154 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 9155
1e915849 9156 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
9c2799c2
NC
9157 gas_assert (mo);
9158 gas_assert (strcmp (name, mo->name) == 0);
252b5132 9159
1e915849 9160 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 9161 {
1e915849 9162 ++mo;
9c2799c2
NC
9163 gas_assert (mo->name);
9164 gas_assert (strcmp (name, mo->name) == 0);
252b5132
RH
9165 }
9166
1e915849 9167 create_insn (&insn, mo);
e077a1c8 9168 for (; *fmt; ++fmt)
252b5132
RH
9169 {
9170 int c;
9171
e077a1c8 9172 c = *fmt;
252b5132
RH
9173 switch (c)
9174 {
252b5132
RH
9175 case ',':
9176 case '(':
9177 case ')':
e077a1c8 9178 break;
252b5132 9179
d8722d76 9180 case '.':
252b5132
RH
9181 case 'S':
9182 case 'P':
9183 case 'R':
e077a1c8 9184 break;
252b5132
RH
9185
9186 case '<':
252b5132 9187 case '5':
d8722d76 9188 case 'F':
252b5132
RH
9189 case 'H':
9190 case 'W':
9191 case 'D':
9192 case 'j':
9193 case '8':
9194 case 'V':
9195 case 'C':
9196 case 'U':
9197 case 'k':
9198 case 'K':
9199 case 'p':
9200 case 'q':
9201 {
b886a2ab
RS
9202 offsetT value;
9203
9c2799c2 9204 gas_assert (ep != NULL);
252b5132
RH
9205
9206 if (ep->X_op != O_constant)
874e8986 9207 *r = (int) BFD_RELOC_UNUSED + c;
b886a2ab 9208 else if (calculate_reloc (*r, ep->X_add_number, &value))
252b5132 9209 {
b886a2ab 9210 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
252b5132 9211 ep = NULL;
f6688943 9212 *r = BFD_RELOC_UNUSED;
252b5132
RH
9213 }
9214 }
e077a1c8 9215 break;
252b5132 9216
e077a1c8
RS
9217 default:
9218 operand = decode_mips16_operand (c, FALSE);
9219 if (!operand)
9220 abort ();
252b5132 9221
4a06e5a2 9222 insn_insert_operand (&insn, operand, va_arg (*args, int));
e077a1c8
RS
9223 break;
9224 }
252b5132
RH
9225 }
9226
9c2799c2 9227 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 9228
df58fc94 9229 append_insn (&insn, ep, r, TRUE);
252b5132
RH
9230}
9231
438c16b8
TS
9232/*
9233 * Generate a "jalr" instruction with a relocation hint to the called
9234 * function. This occurs in NewABI PIC code.
9235 */
9236static void
df58fc94 9237macro_build_jalr (expressionS *ep, int cprestore)
438c16b8 9238{
df58fc94
RS
9239 static const bfd_reloc_code_real_type jalr_relocs[2]
9240 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
9241 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
9242 const char *jalr;
685736be 9243 char *f = NULL;
b34976b6 9244
1180b5a4 9245 if (MIPS_JALR_HINT_P (ep))
f21f8242 9246 {
cc3d92a5 9247 frag_grow (8);
f21f8242
AO
9248 f = frag_more (0);
9249 }
2906b037 9250 if (mips_opts.micromips)
df58fc94 9251 {
833794fc
MR
9252 jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
9253 ? "jalr" : "jalrs");
e64af278 9254 if (MIPS_JALR_HINT_P (ep)
833794fc 9255 || mips_opts.insn32
e64af278 9256 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
9257 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
9258 else
9259 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
9260 }
2906b037
MR
9261 else
9262 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
1180b5a4 9263 if (MIPS_JALR_HINT_P (ep))
df58fc94 9264 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
438c16b8
TS
9265}
9266
252b5132
RH
9267/*
9268 * Generate a "lui" instruction.
9269 */
9270static void
67c0d1eb 9271macro_build_lui (expressionS *ep, int regnum)
252b5132 9272{
9c2799c2 9273 gas_assert (! mips_opts.mips16);
252b5132 9274
df58fc94 9275 if (ep->X_op != O_constant)
252b5132 9276 {
9c2799c2 9277 gas_assert (ep->X_op == O_symbol);
bbe506e8
TS
9278 /* _gp_disp is a special case, used from s_cpload.
9279 __gnu_local_gp is used if mips_no_shared. */
9c2799c2 9280 gas_assert (mips_pic == NO_PIC
78e1bb40 9281 || (! HAVE_NEWABI
aa6975fb
ILT
9282 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
9283 || (! mips_in_shared
bbe506e8
TS
9284 && strcmp (S_GET_NAME (ep->X_add_symbol),
9285 "__gnu_local_gp") == 0));
252b5132
RH
9286 }
9287
df58fc94 9288 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
252b5132
RH
9289}
9290
885add95
CD
9291/* Generate a sequence of instructions to do a load or store from a constant
9292 offset off of a base register (breg) into/from a target register (treg),
9293 using AT if necessary. */
9294static void
67c0d1eb
RS
9295macro_build_ldst_constoffset (expressionS *ep, const char *op,
9296 int treg, int breg, int dbl)
885add95 9297{
9c2799c2 9298 gas_assert (ep->X_op == O_constant);
885add95 9299
256ab948 9300 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
9301 if (!dbl)
9302 normalize_constant_expr (ep);
256ab948 9303
67c1ffbe 9304 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 9305 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
9306 as_warn (_("operand overflow"));
9307
9308 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
9309 {
9310 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 9311 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
9312 }
9313 else
9314 {
9315 /* 32-bit offset, need multiple instructions and AT, like:
9316 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
9317 addu $tempreg,$tempreg,$breg
9318 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
9319 to handle the complete offset. */
67c0d1eb
RS
9320 macro_build_lui (ep, AT);
9321 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
9322 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95 9323
741fe287 9324 if (!mips_opts.at)
1661c76c 9325 as_bad (_("macro used $at after \".set noat\""));
885add95
CD
9326 }
9327}
9328
252b5132
RH
9329/* set_at()
9330 * Generates code to set the $at register to true (one)
9331 * if reg is less than the immediate expression.
9332 */
9333static void
67c0d1eb 9334set_at (int reg, int unsignedp)
252b5132 9335{
b0e6f033 9336 if (imm_expr.X_add_number >= -0x8000
252b5132 9337 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
9338 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
9339 AT, reg, BFD_RELOC_LO16);
252b5132
RH
9340 else
9341 {
bad1aba3 9342 load_register (AT, &imm_expr, GPR_SIZE == 64);
67c0d1eb 9343 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
9344 }
9345}
9346
252b5132
RH
9347/* Count the leading zeroes by performing a binary chop. This is a
9348 bulky bit of source, but performance is a LOT better for the
9349 majority of values than a simple loop to count the bits:
9350 for (lcnt = 0; (lcnt < 32); lcnt++)
9351 if ((v) & (1 << (31 - lcnt)))
9352 break;
9353 However it is not code size friendly, and the gain will drop a bit
9354 on certain cached systems.
9355*/
9356#define COUNT_TOP_ZEROES(v) \
9357 (((v) & ~0xffff) == 0 \
9358 ? ((v) & ~0xff) == 0 \
9359 ? ((v) & ~0xf) == 0 \
9360 ? ((v) & ~0x3) == 0 \
9361 ? ((v) & ~0x1) == 0 \
9362 ? !(v) \
9363 ? 32 \
9364 : 31 \
9365 : 30 \
9366 : ((v) & ~0x7) == 0 \
9367 ? 29 \
9368 : 28 \
9369 : ((v) & ~0x3f) == 0 \
9370 ? ((v) & ~0x1f) == 0 \
9371 ? 27 \
9372 : 26 \
9373 : ((v) & ~0x7f) == 0 \
9374 ? 25 \
9375 : 24 \
9376 : ((v) & ~0xfff) == 0 \
9377 ? ((v) & ~0x3ff) == 0 \
9378 ? ((v) & ~0x1ff) == 0 \
9379 ? 23 \
9380 : 22 \
9381 : ((v) & ~0x7ff) == 0 \
9382 ? 21 \
9383 : 20 \
9384 : ((v) & ~0x3fff) == 0 \
9385 ? ((v) & ~0x1fff) == 0 \
9386 ? 19 \
9387 : 18 \
9388 : ((v) & ~0x7fff) == 0 \
9389 ? 17 \
9390 : 16 \
9391 : ((v) & ~0xffffff) == 0 \
9392 ? ((v) & ~0xfffff) == 0 \
9393 ? ((v) & ~0x3ffff) == 0 \
9394 ? ((v) & ~0x1ffff) == 0 \
9395 ? 15 \
9396 : 14 \
9397 : ((v) & ~0x7ffff) == 0 \
9398 ? 13 \
9399 : 12 \
9400 : ((v) & ~0x3fffff) == 0 \
9401 ? ((v) & ~0x1fffff) == 0 \
9402 ? 11 \
9403 : 10 \
9404 : ((v) & ~0x7fffff) == 0 \
9405 ? 9 \
9406 : 8 \
9407 : ((v) & ~0xfffffff) == 0 \
9408 ? ((v) & ~0x3ffffff) == 0 \
9409 ? ((v) & ~0x1ffffff) == 0 \
9410 ? 7 \
9411 : 6 \
9412 : ((v) & ~0x7ffffff) == 0 \
9413 ? 5 \
9414 : 4 \
9415 : ((v) & ~0x3fffffff) == 0 \
9416 ? ((v) & ~0x1fffffff) == 0 \
9417 ? 3 \
9418 : 2 \
9419 : ((v) & ~0x7fffffff) == 0 \
9420 ? 1 \
9421 : 0)
9422
9423/* load_register()
67c1ffbe 9424 * This routine generates the least number of instructions necessary to load
252b5132
RH
9425 * an absolute expression value into a register.
9426 */
9427static void
67c0d1eb 9428load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
9429{
9430 int freg;
9431 expressionS hi32, lo32;
9432
9433 if (ep->X_op != O_big)
9434 {
9c2799c2 9435 gas_assert (ep->X_op == O_constant);
256ab948
TS
9436
9437 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
9438 if (!dbl)
9439 normalize_constant_expr (ep);
256ab948
TS
9440
9441 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
9442 {
9443 /* We can handle 16 bit signed values with an addiu to
9444 $zero. No need to ever use daddiu here, since $zero and
9445 the result are always correct in 32 bit mode. */
67c0d1eb 9446 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9447 return;
9448 }
9449 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
9450 {
9451 /* We can handle 16 bit unsigned values with an ori to
9452 $zero. */
67c0d1eb 9453 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9454 return;
9455 }
256ab948 9456 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
9457 {
9458 /* 32 bit values require an lui. */
df58fc94 9459 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 9460 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 9461 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
9462 return;
9463 }
9464 }
9465
9466 /* The value is larger than 32 bits. */
9467
bad1aba3 9468 if (!dbl || GPR_SIZE == 32)
252b5132 9469 {
55e08f71
NC
9470 char value[32];
9471
9472 sprintf_vma (value, ep->X_add_number);
1661c76c 9473 as_bad (_("number (0x%s) larger than 32 bits"), value);
67c0d1eb 9474 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9475 return;
9476 }
9477
9478 if (ep->X_op != O_big)
9479 {
9480 hi32 = *ep;
9481 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9482 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9483 hi32.X_add_number &= 0xffffffff;
9484 lo32 = *ep;
9485 lo32.X_add_number &= 0xffffffff;
9486 }
9487 else
9488 {
9c2799c2 9489 gas_assert (ep->X_add_number > 2);
252b5132
RH
9490 if (ep->X_add_number == 3)
9491 generic_bignum[3] = 0;
9492 else if (ep->X_add_number > 4)
1661c76c 9493 as_bad (_("number larger than 64 bits"));
252b5132
RH
9494 lo32.X_op = O_constant;
9495 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
9496 hi32.X_op = O_constant;
9497 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
9498 }
9499
9500 if (hi32.X_add_number == 0)
9501 freg = 0;
9502 else
9503 {
9504 int shift, bit;
9505 unsigned long hi, lo;
9506
956cd1d6 9507 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
9508 {
9509 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
9510 {
67c0d1eb 9511 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
9512 return;
9513 }
9514 if (lo32.X_add_number & 0x80000000)
9515 {
df58fc94 9516 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 9517 if (lo32.X_add_number & 0xffff)
67c0d1eb 9518 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
9519 return;
9520 }
9521 }
252b5132
RH
9522
9523 /* Check for 16bit shifted constant. We know that hi32 is
9524 non-zero, so start the mask on the first bit of the hi32
9525 value. */
9526 shift = 17;
9527 do
beae10d5
KH
9528 {
9529 unsigned long himask, lomask;
9530
9531 if (shift < 32)
9532 {
9533 himask = 0xffff >> (32 - shift);
9534 lomask = (0xffff << shift) & 0xffffffff;
9535 }
9536 else
9537 {
9538 himask = 0xffff << (shift - 32);
9539 lomask = 0;
9540 }
9541 if ((hi32.X_add_number & ~(offsetT) himask) == 0
9542 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
9543 {
9544 expressionS tmp;
9545
9546 tmp.X_op = O_constant;
9547 if (shift < 32)
9548 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
9549 | (lo32.X_add_number >> shift));
9550 else
9551 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb 9552 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
df58fc94 9553 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 9554 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
9555 return;
9556 }
f9419b05 9557 ++shift;
beae10d5
KH
9558 }
9559 while (shift <= (64 - 16));
252b5132
RH
9560
9561 /* Find the bit number of the lowest one bit, and store the
9562 shifted value in hi/lo. */
9563 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
9564 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
9565 if (lo != 0)
9566 {
9567 bit = 0;
9568 while ((lo & 1) == 0)
9569 {
9570 lo >>= 1;
9571 ++bit;
9572 }
9573 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
9574 hi >>= bit;
9575 }
9576 else
9577 {
9578 bit = 32;
9579 while ((hi & 1) == 0)
9580 {
9581 hi >>= 1;
9582 ++bit;
9583 }
9584 lo = hi;
9585 hi = 0;
9586 }
9587
9588 /* Optimize if the shifted value is a (power of 2) - 1. */
9589 if ((hi == 0 && ((lo + 1) & lo) == 0)
9590 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
9591 {
9592 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 9593 if (shift != 0)
beae10d5 9594 {
252b5132
RH
9595 expressionS tmp;
9596
9597 /* This instruction will set the register to be all
9598 ones. */
beae10d5
KH
9599 tmp.X_op = O_constant;
9600 tmp.X_add_number = (offsetT) -1;
67c0d1eb 9601 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
9602 if (bit != 0)
9603 {
9604 bit += shift;
df58fc94 9605 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 9606 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 9607 }
df58fc94 9608 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
67c0d1eb 9609 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
9610 return;
9611 }
9612 }
252b5132
RH
9613
9614 /* Sign extend hi32 before calling load_register, because we can
9615 generally get better code when we load a sign extended value. */
9616 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 9617 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 9618 load_register (reg, &hi32, 0);
252b5132
RH
9619 freg = reg;
9620 }
9621 if ((lo32.X_add_number & 0xffff0000) == 0)
9622 {
9623 if (freg != 0)
9624 {
df58fc94 9625 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
252b5132
RH
9626 freg = reg;
9627 }
9628 }
9629 else
9630 {
9631 expressionS mid16;
9632
956cd1d6 9633 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 9634 {
df58fc94
RS
9635 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9636 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
beae10d5
KH
9637 return;
9638 }
252b5132
RH
9639
9640 if (freg != 0)
9641 {
df58fc94 9642 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
252b5132
RH
9643 freg = reg;
9644 }
9645 mid16 = lo32;
9646 mid16.X_add_number >>= 16;
67c0d1eb 9647 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
df58fc94 9648 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
252b5132
RH
9649 freg = reg;
9650 }
9651 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 9652 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
9653}
9654
269137b2
TS
9655static inline void
9656load_delay_nop (void)
9657{
9658 if (!gpr_interlocks)
9659 macro_build (NULL, "nop", "");
9660}
9661
252b5132
RH
9662/* Load an address into a register. */
9663
9664static void
67c0d1eb 9665load_address (int reg, expressionS *ep, int *used_at)
252b5132 9666{
252b5132
RH
9667 if (ep->X_op != O_constant
9668 && ep->X_op != O_symbol)
9669 {
9670 as_bad (_("expression too complex"));
9671 ep->X_op = O_constant;
9672 }
9673
9674 if (ep->X_op == O_constant)
9675 {
67c0d1eb 9676 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
9677 return;
9678 }
9679
9680 if (mips_pic == NO_PIC)
9681 {
9682 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 9683 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
9684 Otherwise we want
9685 lui $reg,<sym> (BFD_RELOC_HI16_S)
9686 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 9687 If we have an addend, we always use the latter form.
76b3015f 9688
d6bc6245
TS
9689 With 64bit address space and a usable $at we want
9690 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9691 lui $at,<sym> (BFD_RELOC_HI16_S)
9692 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9693 daddiu $at,<sym> (BFD_RELOC_LO16)
9694 dsll32 $reg,0
3a482fd5 9695 daddu $reg,$reg,$at
76b3015f 9696
c03099e6 9697 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
9698 on superscalar processors.
9699 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9700 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9701 dsll $reg,16
9702 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
9703 dsll $reg,16
9704 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
9705
9706 For GP relative symbols in 64bit address space we can use
9707 the same sequence as in 32bit address space. */
aed1a261 9708 if (HAVE_64BIT_SYMBOLS)
d6bc6245 9709 {
6caf9ef4
TS
9710 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9711 && !nopic_need_relax (ep->X_add_symbol, 1))
9712 {
9713 relax_start (ep->X_add_symbol);
9714 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9715 mips_gp_register, BFD_RELOC_GPREL16);
9716 relax_switch ();
9717 }
d6bc6245 9718
741fe287 9719 if (*used_at == 0 && mips_opts.at)
d6bc6245 9720 {
df58fc94
RS
9721 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9722 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
67c0d1eb
RS
9723 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9724 BFD_RELOC_MIPS_HIGHER);
9725 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
df58fc94 9726 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
67c0d1eb 9727 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
9728 *used_at = 1;
9729 }
9730 else
9731 {
df58fc94 9732 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb
RS
9733 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9734 BFD_RELOC_MIPS_HIGHER);
df58fc94 9735 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 9736 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
df58fc94 9737 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 9738 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 9739 }
6caf9ef4
TS
9740
9741 if (mips_relax.sequence)
9742 relax_end ();
d6bc6245 9743 }
252b5132
RH
9744 else
9745 {
d6bc6245 9746 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 9747 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 9748 {
4d7206a2 9749 relax_start (ep->X_add_symbol);
67c0d1eb 9750 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 9751 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 9752 relax_switch ();
d6bc6245 9753 }
67c0d1eb
RS
9754 macro_build_lui (ep, reg);
9755 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
9756 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
9757 if (mips_relax.sequence)
9758 relax_end ();
d6bc6245 9759 }
252b5132 9760 }
0a44bf69 9761 else if (!mips_big_got)
252b5132
RH
9762 {
9763 expressionS ex;
9764
9765 /* If this is a reference to an external symbol, we want
9766 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9767 Otherwise we want
9768 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9769 nop
9770 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
9771 If there is a constant, it must be added in after.
9772
ed6fb7bd 9773 If we have NewABI, we want
f5040a92
AO
9774 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
9775 unless we're referencing a global symbol with a non-zero
9776 offset, in which case cst must be added separately. */
ed6fb7bd
SC
9777 if (HAVE_NEWABI)
9778 {
f5040a92
AO
9779 if (ep->X_add_number)
9780 {
4d7206a2 9781 ex.X_add_number = ep->X_add_number;
f5040a92 9782 ep->X_add_number = 0;
4d7206a2 9783 relax_start (ep->X_add_symbol);
67c0d1eb
RS
9784 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9785 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
9786 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9787 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9788 ex.X_op = O_constant;
67c0d1eb 9789 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 9790 reg, reg, BFD_RELOC_LO16);
f5040a92 9791 ep->X_add_number = ex.X_add_number;
4d7206a2 9792 relax_switch ();
f5040a92 9793 }
67c0d1eb 9794 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9795 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
9796 if (mips_relax.sequence)
9797 relax_end ();
ed6fb7bd
SC
9798 }
9799 else
9800 {
f5040a92
AO
9801 ex.X_add_number = ep->X_add_number;
9802 ep->X_add_number = 0;
67c0d1eb
RS
9803 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9804 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9805 load_delay_nop ();
4d7206a2
RS
9806 relax_start (ep->X_add_symbol);
9807 relax_switch ();
67c0d1eb 9808 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 9809 BFD_RELOC_LO16);
4d7206a2 9810 relax_end ();
ed6fb7bd 9811
f5040a92
AO
9812 if (ex.X_add_number != 0)
9813 {
9814 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9815 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9816 ex.X_op = O_constant;
67c0d1eb 9817 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 9818 reg, reg, BFD_RELOC_LO16);
f5040a92 9819 }
252b5132
RH
9820 }
9821 }
0a44bf69 9822 else if (mips_big_got)
252b5132
RH
9823 {
9824 expressionS ex;
252b5132
RH
9825
9826 /* This is the large GOT case. If this is a reference to an
9827 external symbol, we want
9828 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9829 addu $reg,$reg,$gp
9830 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
9831
9832 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
9833 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9834 nop
9835 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 9836 If there is a constant, it must be added in after.
f5040a92
AO
9837
9838 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
9839 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
9840 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 9841 */
438c16b8
TS
9842 if (HAVE_NEWABI)
9843 {
4d7206a2 9844 ex.X_add_number = ep->X_add_number;
f5040a92 9845 ep->X_add_number = 0;
4d7206a2 9846 relax_start (ep->X_add_symbol);
df58fc94 9847 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
9848 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9849 reg, reg, mips_gp_register);
9850 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9851 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
9852 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9853 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9854 else if (ex.X_add_number)
9855 {
9856 ex.X_op = O_constant;
67c0d1eb
RS
9857 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9858 BFD_RELOC_LO16);
f5040a92
AO
9859 }
9860
9861 ep->X_add_number = ex.X_add_number;
4d7206a2 9862 relax_switch ();
67c0d1eb 9863 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9864 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
9865 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9866 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 9867 relax_end ();
438c16b8 9868 }
252b5132 9869 else
438c16b8 9870 {
f5040a92
AO
9871 ex.X_add_number = ep->X_add_number;
9872 ep->X_add_number = 0;
4d7206a2 9873 relax_start (ep->X_add_symbol);
df58fc94 9874 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
9875 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9876 reg, reg, mips_gp_register);
9877 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9878 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
9879 relax_switch ();
9880 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
9881 {
9882 /* We need a nop before loading from $gp. This special
9883 check is required because the lui which starts the main
9884 instruction stream does not refer to $gp, and so will not
9885 insert the nop which may be required. */
67c0d1eb 9886 macro_build (NULL, "nop", "");
438c16b8 9887 }
67c0d1eb 9888 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9889 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9890 load_delay_nop ();
67c0d1eb 9891 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 9892 BFD_RELOC_LO16);
4d7206a2 9893 relax_end ();
438c16b8 9894
f5040a92
AO
9895 if (ex.X_add_number != 0)
9896 {
9897 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9898 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9899 ex.X_op = O_constant;
67c0d1eb
RS
9900 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9901 BFD_RELOC_LO16);
f5040a92 9902 }
252b5132
RH
9903 }
9904 }
252b5132
RH
9905 else
9906 abort ();
8fc2e39e 9907
741fe287 9908 if (!mips_opts.at && *used_at == 1)
1661c76c 9909 as_bad (_("macro used $at after \".set noat\""));
252b5132
RH
9910}
9911
ea1fb5dc
RS
9912/* Move the contents of register SOURCE into register DEST. */
9913
9914static void
67c0d1eb 9915move_register (int dest, int source)
ea1fb5dc 9916{
df58fc94
RS
9917 /* Prefer to use a 16-bit microMIPS instruction unless the previous
9918 instruction specifically requires a 32-bit one. */
9919 if (mips_opts.micromips
833794fc 9920 && !mips_opts.insn32
df58fc94 9921 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7951ca42 9922 macro_build (NULL, "move", "mp,mj", dest, source);
df58fc94 9923 else
40fc1451 9924 macro_build (NULL, "or", "d,v,t", dest, source, 0);
ea1fb5dc
RS
9925}
9926
4d7206a2 9927/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
9928 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
9929 The two alternatives are:
4d7206a2 9930
33eaf5de 9931 Global symbol Local symbol
4d7206a2
RS
9932 ------------- ------------
9933 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
9934 ... ...
9935 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
9936
9937 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
9938 emits the second for a 16-bit offset or add_got_offset_hilo emits
9939 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
9940
9941static void
67c0d1eb 9942load_got_offset (int dest, expressionS *local)
4d7206a2
RS
9943{
9944 expressionS global;
9945
9946 global = *local;
9947 global.X_add_number = 0;
9948
9949 relax_start (local->X_add_symbol);
67c0d1eb
RS
9950 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9951 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 9952 relax_switch ();
67c0d1eb
RS
9953 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9954 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
9955 relax_end ();
9956}
9957
9958static void
67c0d1eb 9959add_got_offset (int dest, expressionS *local)
4d7206a2
RS
9960{
9961 expressionS global;
9962
9963 global.X_op = O_constant;
9964 global.X_op_symbol = NULL;
9965 global.X_add_symbol = NULL;
9966 global.X_add_number = local->X_add_number;
9967
9968 relax_start (local->X_add_symbol);
67c0d1eb 9969 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
9970 dest, dest, BFD_RELOC_LO16);
9971 relax_switch ();
67c0d1eb 9972 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
9973 relax_end ();
9974}
9975
f6a22291
MR
9976static void
9977add_got_offset_hilo (int dest, expressionS *local, int tmp)
9978{
9979 expressionS global;
9980 int hold_mips_optimize;
9981
9982 global.X_op = O_constant;
9983 global.X_op_symbol = NULL;
9984 global.X_add_symbol = NULL;
9985 global.X_add_number = local->X_add_number;
9986
9987 relax_start (local->X_add_symbol);
9988 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
9989 relax_switch ();
9990 /* Set mips_optimize around the lui instruction to avoid
9991 inserting an unnecessary nop after the lw. */
9992 hold_mips_optimize = mips_optimize;
9993 mips_optimize = 2;
9994 macro_build_lui (&global, tmp);
9995 mips_optimize = hold_mips_optimize;
9996 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
9997 relax_end ();
9998
9999 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
10000}
10001
df58fc94
RS
10002/* Emit a sequence of instructions to emulate a branch likely operation.
10003 BR is an ordinary branch corresponding to one to be emulated. BRNEG
10004 is its complementing branch with the original condition negated.
10005 CALL is set if the original branch specified the link operation.
10006 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
10007
10008 Code like this is produced in the noreorder mode:
10009
10010 BRNEG <args>, 1f
10011 nop
10012 b <sym>
10013 delay slot (executed only if branch taken)
10014 1:
10015
10016 or, if CALL is set:
10017
10018 BRNEG <args>, 1f
10019 nop
10020 bal <sym>
10021 delay slot (executed only if branch taken)
10022 1:
10023
10024 In the reorder mode the delay slot would be filled with a nop anyway,
10025 so code produced is simply:
10026
10027 BR <args>, <sym>
10028 nop
10029
10030 This function is used when producing code for the microMIPS ASE that
10031 does not implement branch likely instructions in hardware. */
10032
10033static void
10034macro_build_branch_likely (const char *br, const char *brneg,
10035 int call, expressionS *ep, const char *fmt,
10036 unsigned int sreg, unsigned int treg)
10037{
10038 int noreorder = mips_opts.noreorder;
10039 expressionS expr1;
10040
10041 gas_assert (mips_opts.micromips);
10042 start_noreorder ();
10043 if (noreorder)
10044 {
10045 micromips_label_expr (&expr1);
10046 macro_build (&expr1, brneg, fmt, sreg, treg);
10047 macro_build (NULL, "nop", "");
10048 macro_build (ep, call ? "bal" : "b", "p");
10049
10050 /* Set to true so that append_insn adds a label. */
10051 emit_branch_likely_macro = TRUE;
10052 }
10053 else
10054 {
10055 macro_build (ep, br, fmt, sreg, treg);
10056 macro_build (NULL, "nop", "");
10057 }
10058 end_noreorder ();
10059}
10060
10061/* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
10062 the condition code tested. EP specifies the branch target. */
10063
10064static void
10065macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
10066{
10067 const int call = 0;
10068 const char *brneg;
10069 const char *br;
10070
10071 switch (type)
10072 {
10073 case M_BC1FL:
10074 br = "bc1f";
10075 brneg = "bc1t";
10076 break;
10077 case M_BC1TL:
10078 br = "bc1t";
10079 brneg = "bc1f";
10080 break;
10081 case M_BC2FL:
10082 br = "bc2f";
10083 brneg = "bc2t";
10084 break;
10085 case M_BC2TL:
10086 br = "bc2t";
10087 brneg = "bc2f";
10088 break;
10089 default:
10090 abort ();
10091 }
10092 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
10093}
10094
10095/* Emit a two-argument branch macro specified by TYPE, using SREG as
10096 the register tested. EP specifies the branch target. */
10097
10098static void
10099macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
10100{
10101 const char *brneg = NULL;
10102 const char *br;
10103 int call = 0;
10104
10105 switch (type)
10106 {
10107 case M_BGEZ:
10108 br = "bgez";
10109 break;
10110 case M_BGEZL:
10111 br = mips_opts.micromips ? "bgez" : "bgezl";
10112 brneg = "bltz";
10113 break;
10114 case M_BGEZALL:
10115 gas_assert (mips_opts.micromips);
833794fc 10116 br = mips_opts.insn32 ? "bgezal" : "bgezals";
df58fc94
RS
10117 brneg = "bltz";
10118 call = 1;
10119 break;
10120 case M_BGTZ:
10121 br = "bgtz";
10122 break;
10123 case M_BGTZL:
10124 br = mips_opts.micromips ? "bgtz" : "bgtzl";
10125 brneg = "blez";
10126 break;
10127 case M_BLEZ:
10128 br = "blez";
10129 break;
10130 case M_BLEZL:
10131 br = mips_opts.micromips ? "blez" : "blezl";
10132 brneg = "bgtz";
10133 break;
10134 case M_BLTZ:
10135 br = "bltz";
10136 break;
10137 case M_BLTZL:
10138 br = mips_opts.micromips ? "bltz" : "bltzl";
10139 brneg = "bgez";
10140 break;
10141 case M_BLTZALL:
10142 gas_assert (mips_opts.micromips);
833794fc 10143 br = mips_opts.insn32 ? "bltzal" : "bltzals";
df58fc94
RS
10144 brneg = "bgez";
10145 call = 1;
10146 break;
10147 default:
10148 abort ();
10149 }
10150 if (mips_opts.micromips && brneg)
10151 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
10152 else
10153 macro_build (ep, br, "s,p", sreg);
10154}
10155
10156/* Emit a three-argument branch macro specified by TYPE, using SREG and
10157 TREG as the registers tested. EP specifies the branch target. */
10158
10159static void
10160macro_build_branch_rsrt (int type, expressionS *ep,
10161 unsigned int sreg, unsigned int treg)
10162{
10163 const char *brneg = NULL;
10164 const int call = 0;
10165 const char *br;
10166
10167 switch (type)
10168 {
10169 case M_BEQ:
10170 case M_BEQ_I:
10171 br = "beq";
10172 break;
10173 case M_BEQL:
10174 case M_BEQL_I:
10175 br = mips_opts.micromips ? "beq" : "beql";
10176 brneg = "bne";
10177 break;
10178 case M_BNE:
10179 case M_BNE_I:
10180 br = "bne";
10181 break;
10182 case M_BNEL:
10183 case M_BNEL_I:
10184 br = mips_opts.micromips ? "bne" : "bnel";
10185 brneg = "beq";
10186 break;
10187 default:
10188 abort ();
10189 }
10190 if (mips_opts.micromips && brneg)
10191 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
10192 else
10193 macro_build (ep, br, "s,t,p", sreg, treg);
10194}
10195
f2ae14a1
RS
10196/* Return the high part that should be loaded in order to make the low
10197 part of VALUE accessible using an offset of OFFBITS bits. */
10198
10199static offsetT
10200offset_high_part (offsetT value, unsigned int offbits)
10201{
10202 offsetT bias;
10203 addressT low_mask;
10204
10205 if (offbits == 0)
10206 return value;
10207 bias = 1 << (offbits - 1);
10208 low_mask = bias * 2 - 1;
10209 return (value + bias) & ~low_mask;
10210}
10211
10212/* Return true if the value stored in offset_expr and offset_reloc
10213 fits into a signed offset of OFFBITS bits. RANGE is the maximum
10214 amount that the caller wants to add without inducing overflow
10215 and ALIGN is the known alignment of the value in bytes. */
10216
10217static bfd_boolean
10218small_offset_p (unsigned int range, unsigned int align, unsigned int offbits)
10219{
10220 if (offbits == 16)
10221 {
10222 /* Accept any relocation operator if overflow isn't a concern. */
10223 if (range < align && *offset_reloc != BFD_RELOC_UNUSED)
10224 return TRUE;
10225
10226 /* These relocations are guaranteed not to overflow in correct links. */
10227 if (*offset_reloc == BFD_RELOC_MIPS_LITERAL
10228 || gprel16_reloc_p (*offset_reloc))
10229 return TRUE;
10230 }
10231 if (offset_expr.X_op == O_constant
10232 && offset_high_part (offset_expr.X_add_number, offbits) == 0
10233 && offset_high_part (offset_expr.X_add_number + range, offbits) == 0)
10234 return TRUE;
10235 return FALSE;
10236}
10237
252b5132
RH
10238/*
10239 * Build macros
10240 * This routine implements the seemingly endless macro or synthesized
10241 * instructions and addressing modes in the mips assembly language. Many
10242 * of these macros are simple and are similar to each other. These could
67c1ffbe 10243 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
10244 * this verbose method. Others are not simple macros but are more like
10245 * optimizing code generation.
10246 * One interesting optimization is when several store macros appear
67c1ffbe 10247 * consecutively that would load AT with the upper half of the same address.
2b0f3761 10248 * The ensuing load upper instructions are omitted. This implies some kind
252b5132
RH
10249 * of global optimization. We currently only optimize within a single macro.
10250 * For many of the load and store macros if the address is specified as a
10251 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
10252 * first load register 'at' with zero and use it as the base register. The
10253 * mips assembler simply uses register $zero. Just one tiny optimization
10254 * we're missing.
10255 */
10256static void
833794fc 10257macro (struct mips_cl_insn *ip, char *str)
252b5132 10258{
c0ebe874
RS
10259 const struct mips_operand_array *operands;
10260 unsigned int breg, i;
741fe287 10261 unsigned int tempreg;
252b5132 10262 int mask;
43841e91 10263 int used_at = 0;
df58fc94 10264 expressionS label_expr;
252b5132 10265 expressionS expr1;
df58fc94 10266 expressionS *ep;
252b5132
RH
10267 const char *s;
10268 const char *s2;
10269 const char *fmt;
10270 int likely = 0;
252b5132 10271 int coproc = 0;
7f3c4072 10272 int offbits = 16;
1abe91b1 10273 int call = 0;
df58fc94
RS
10274 int jals = 0;
10275 int dbl = 0;
10276 int imm = 0;
10277 int ust = 0;
10278 int lp = 0;
f2ae14a1 10279 bfd_boolean large_offset;
252b5132 10280 int off;
252b5132 10281 int hold_mips_optimize;
f2ae14a1 10282 unsigned int align;
c0ebe874 10283 unsigned int op[MAX_OPERANDS];
252b5132 10284
9c2799c2 10285 gas_assert (! mips_opts.mips16);
252b5132 10286
c0ebe874
RS
10287 operands = insn_operands (ip);
10288 for (i = 0; i < MAX_OPERANDS; i++)
10289 if (operands->operand[i])
10290 op[i] = insn_extract_operand (ip, operands->operand[i]);
10291 else
10292 op[i] = -1;
10293
252b5132
RH
10294 mask = ip->insn_mo->mask;
10295
df58fc94
RS
10296 label_expr.X_op = O_constant;
10297 label_expr.X_op_symbol = NULL;
10298 label_expr.X_add_symbol = NULL;
10299 label_expr.X_add_number = 0;
10300
252b5132
RH
10301 expr1.X_op = O_constant;
10302 expr1.X_op_symbol = NULL;
10303 expr1.X_add_symbol = NULL;
10304 expr1.X_add_number = 1;
f2ae14a1 10305 align = 1;
252b5132
RH
10306
10307 switch (mask)
10308 {
10309 case M_DABS:
10310 dbl = 1;
1a0670f3 10311 /* Fall through. */
252b5132 10312 case M_ABS:
df58fc94
RS
10313 /* bgez $a0,1f
10314 move v0,$a0
10315 sub v0,$zero,$a0
10316 1:
10317 */
252b5132 10318
7d10b47d 10319 start_noreorder ();
252b5132 10320
df58fc94
RS
10321 if (mips_opts.micromips)
10322 micromips_label_expr (&label_expr);
10323 else
10324 label_expr.X_add_number = 8;
c0ebe874
RS
10325 macro_build (&label_expr, "bgez", "s,p", op[1]);
10326 if (op[0] == op[1])
a605d2b3 10327 macro_build (NULL, "nop", "");
252b5132 10328 else
c0ebe874
RS
10329 move_register (op[0], op[1]);
10330 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", op[0], 0, op[1]);
df58fc94
RS
10331 if (mips_opts.micromips)
10332 micromips_add_label ();
252b5132 10333
7d10b47d 10334 end_noreorder ();
8fc2e39e 10335 break;
252b5132
RH
10336
10337 case M_ADD_I:
10338 s = "addi";
10339 s2 = "add";
10340 goto do_addi;
10341 case M_ADDU_I:
10342 s = "addiu";
10343 s2 = "addu";
10344 goto do_addi;
10345 case M_DADD_I:
10346 dbl = 1;
10347 s = "daddi";
10348 s2 = "dadd";
df58fc94
RS
10349 if (!mips_opts.micromips)
10350 goto do_addi;
b0e6f033 10351 if (imm_expr.X_add_number >= -0x200
df58fc94
RS
10352 && imm_expr.X_add_number < 0x200)
10353 {
b0e6f033
RS
10354 macro_build (NULL, s, "t,r,.", op[0], op[1],
10355 (int) imm_expr.X_add_number);
df58fc94
RS
10356 break;
10357 }
10358 goto do_addi_i;
252b5132
RH
10359 case M_DADDU_I:
10360 dbl = 1;
10361 s = "daddiu";
10362 s2 = "daddu";
10363 do_addi:
b0e6f033 10364 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
10365 && imm_expr.X_add_number < 0x8000)
10366 {
c0ebe874 10367 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 10368 break;
252b5132 10369 }
df58fc94 10370 do_addi_i:
8fc2e39e 10371 used_at = 1;
67c0d1eb 10372 load_register (AT, &imm_expr, dbl);
c0ebe874 10373 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
10374 break;
10375
10376 case M_AND_I:
10377 s = "andi";
10378 s2 = "and";
10379 goto do_bit;
10380 case M_OR_I:
10381 s = "ori";
10382 s2 = "or";
10383 goto do_bit;
10384 case M_NOR_I:
10385 s = "";
10386 s2 = "nor";
10387 goto do_bit;
10388 case M_XOR_I:
10389 s = "xori";
10390 s2 = "xor";
10391 do_bit:
b0e6f033 10392 if (imm_expr.X_add_number >= 0
252b5132
RH
10393 && imm_expr.X_add_number < 0x10000)
10394 {
10395 if (mask != M_NOR_I)
c0ebe874 10396 macro_build (&imm_expr, s, "t,r,i", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
10397 else
10398 {
67c0d1eb 10399 macro_build (&imm_expr, "ori", "t,r,i",
c0ebe874
RS
10400 op[0], op[1], BFD_RELOC_LO16);
10401 macro_build (NULL, "nor", "d,v,t", op[0], op[0], 0);
252b5132 10402 }
8fc2e39e 10403 break;
252b5132
RH
10404 }
10405
8fc2e39e 10406 used_at = 1;
bad1aba3 10407 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 10408 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
10409 break;
10410
8b082fb1
TS
10411 case M_BALIGN:
10412 switch (imm_expr.X_add_number)
10413 {
10414 case 0:
10415 macro_build (NULL, "nop", "");
10416 break;
10417 case 2:
c0ebe874 10418 macro_build (NULL, "packrl.ph", "d,s,t", op[0], op[0], op[1]);
8b082fb1 10419 break;
03f66e8a
MR
10420 case 1:
10421 case 3:
c0ebe874 10422 macro_build (NULL, "balign", "t,s,2", op[0], op[1],
90ecf173 10423 (int) imm_expr.X_add_number);
8b082fb1 10424 break;
03f66e8a
MR
10425 default:
10426 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
10427 (unsigned long) imm_expr.X_add_number);
10428 break;
8b082fb1
TS
10429 }
10430 break;
10431
df58fc94
RS
10432 case M_BC1FL:
10433 case M_BC1TL:
10434 case M_BC2FL:
10435 case M_BC2TL:
10436 gas_assert (mips_opts.micromips);
10437 macro_build_branch_ccl (mask, &offset_expr,
10438 EXTRACT_OPERAND (1, BCC, *ip));
10439 break;
10440
252b5132 10441 case M_BEQ_I:
252b5132 10442 case M_BEQL_I:
252b5132 10443 case M_BNE_I:
252b5132 10444 case M_BNEL_I:
b0e6f033 10445 if (imm_expr.X_add_number == 0)
c0ebe874 10446 op[1] = 0;
df58fc94 10447 else
252b5132 10448 {
c0ebe874 10449 op[1] = AT;
df58fc94 10450 used_at = 1;
bad1aba3 10451 load_register (op[1], &imm_expr, GPR_SIZE == 64);
252b5132 10452 }
df58fc94
RS
10453 /* Fall through. */
10454 case M_BEQL:
10455 case M_BNEL:
c0ebe874 10456 macro_build_branch_rsrt (mask, &offset_expr, op[0], op[1]);
252b5132
RH
10457 break;
10458
10459 case M_BGEL:
10460 likely = 1;
1a0670f3 10461 /* Fall through. */
252b5132 10462 case M_BGE:
c0ebe874
RS
10463 if (op[1] == 0)
10464 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[0]);
10465 else if (op[0] == 0)
10466 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[1]);
df58fc94 10467 else
252b5132 10468 {
df58fc94 10469 used_at = 1;
c0ebe874 10470 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10471 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10472 &offset_expr, AT, ZERO);
252b5132 10473 }
df58fc94
RS
10474 break;
10475
10476 case M_BGEZL:
10477 case M_BGEZALL:
10478 case M_BGTZL:
10479 case M_BLEZL:
10480 case M_BLTZL:
10481 case M_BLTZALL:
c0ebe874 10482 macro_build_branch_rs (mask, &offset_expr, op[0]);
252b5132
RH
10483 break;
10484
10485 case M_BGTL_I:
10486 likely = 1;
1a0670f3 10487 /* Fall through. */
252b5132 10488 case M_BGT_I:
90ecf173 10489 /* Check for > max integer. */
b0e6f033 10490 if (imm_expr.X_add_number >= GPR_SMAX)
252b5132
RH
10491 {
10492 do_false:
90ecf173 10493 /* Result is always false. */
252b5132 10494 if (! likely)
a605d2b3 10495 macro_build (NULL, "nop", "");
252b5132 10496 else
df58fc94 10497 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
8fc2e39e 10498 break;
252b5132 10499 }
f9419b05 10500 ++imm_expr.X_add_number;
6f2117ba 10501 /* Fall through. */
252b5132
RH
10502 case M_BGE_I:
10503 case M_BGEL_I:
10504 if (mask == M_BGEL_I)
10505 likely = 1;
b0e6f033 10506 if (imm_expr.X_add_number == 0)
252b5132 10507 {
df58fc94 10508 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
c0ebe874 10509 &offset_expr, op[0]);
8fc2e39e 10510 break;
252b5132 10511 }
b0e6f033 10512 if (imm_expr.X_add_number == 1)
252b5132 10513 {
df58fc94 10514 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
c0ebe874 10515 &offset_expr, op[0]);
8fc2e39e 10516 break;
252b5132 10517 }
b0e6f033 10518 if (imm_expr.X_add_number <= GPR_SMIN)
252b5132
RH
10519 {
10520 do_true:
6f2117ba 10521 /* Result is always true. */
1661c76c 10522 as_warn (_("branch %s is always true"), ip->insn_mo->name);
67c0d1eb 10523 macro_build (&offset_expr, "b", "p");
8fc2e39e 10524 break;
252b5132 10525 }
8fc2e39e 10526 used_at = 1;
c0ebe874 10527 set_at (op[0], 0);
df58fc94
RS
10528 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10529 &offset_expr, AT, ZERO);
252b5132
RH
10530 break;
10531
10532 case M_BGEUL:
10533 likely = 1;
1a0670f3 10534 /* Fall through. */
252b5132 10535 case M_BGEU:
c0ebe874 10536 if (op[1] == 0)
252b5132 10537 goto do_true;
c0ebe874 10538 else if (op[0] == 0)
df58fc94 10539 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 10540 &offset_expr, ZERO, op[1]);
df58fc94 10541 else
252b5132 10542 {
df58fc94 10543 used_at = 1;
c0ebe874 10544 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10545 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10546 &offset_expr, AT, ZERO);
252b5132 10547 }
252b5132
RH
10548 break;
10549
10550 case M_BGTUL_I:
10551 likely = 1;
1a0670f3 10552 /* Fall through. */
252b5132 10553 case M_BGTU_I:
c0ebe874 10554 if (op[0] == 0
bad1aba3 10555 || (GPR_SIZE == 32
f01dc953 10556 && imm_expr.X_add_number == -1))
252b5132 10557 goto do_false;
f9419b05 10558 ++imm_expr.X_add_number;
6f2117ba 10559 /* Fall through. */
252b5132
RH
10560 case M_BGEU_I:
10561 case M_BGEUL_I:
10562 if (mask == M_BGEUL_I)
10563 likely = 1;
b0e6f033 10564 if (imm_expr.X_add_number == 0)
252b5132 10565 goto do_true;
b0e6f033 10566 else if (imm_expr.X_add_number == 1)
df58fc94 10567 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 10568 &offset_expr, op[0], ZERO);
df58fc94 10569 else
252b5132 10570 {
df58fc94 10571 used_at = 1;
c0ebe874 10572 set_at (op[0], 1);
df58fc94
RS
10573 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10574 &offset_expr, AT, ZERO);
252b5132 10575 }
252b5132
RH
10576 break;
10577
10578 case M_BGTL:
10579 likely = 1;
1a0670f3 10580 /* Fall through. */
252b5132 10581 case M_BGT:
c0ebe874
RS
10582 if (op[1] == 0)
10583 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[0]);
10584 else if (op[0] == 0)
10585 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[1]);
df58fc94 10586 else
252b5132 10587 {
df58fc94 10588 used_at = 1;
c0ebe874 10589 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10590 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10591 &offset_expr, AT, ZERO);
252b5132 10592 }
252b5132
RH
10593 break;
10594
10595 case M_BGTUL:
10596 likely = 1;
1a0670f3 10597 /* Fall through. */
252b5132 10598 case M_BGTU:
c0ebe874 10599 if (op[1] == 0)
df58fc94 10600 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874
RS
10601 &offset_expr, op[0], ZERO);
10602 else if (op[0] == 0)
df58fc94
RS
10603 goto do_false;
10604 else
252b5132 10605 {
df58fc94 10606 used_at = 1;
c0ebe874 10607 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10608 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10609 &offset_expr, AT, ZERO);
252b5132 10610 }
252b5132
RH
10611 break;
10612
10613 case M_BLEL:
10614 likely = 1;
1a0670f3 10615 /* Fall through. */
252b5132 10616 case M_BLE:
c0ebe874
RS
10617 if (op[1] == 0)
10618 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10619 else if (op[0] == 0)
10620 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[1]);
df58fc94 10621 else
252b5132 10622 {
df58fc94 10623 used_at = 1;
c0ebe874 10624 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10625 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10626 &offset_expr, AT, ZERO);
252b5132 10627 }
252b5132
RH
10628 break;
10629
10630 case M_BLEL_I:
10631 likely = 1;
1a0670f3 10632 /* Fall through. */
252b5132 10633 case M_BLE_I:
b0e6f033 10634 if (imm_expr.X_add_number >= GPR_SMAX)
252b5132 10635 goto do_true;
f9419b05 10636 ++imm_expr.X_add_number;
6f2117ba 10637 /* Fall through. */
252b5132
RH
10638 case M_BLT_I:
10639 case M_BLTL_I:
10640 if (mask == M_BLTL_I)
10641 likely = 1;
b0e6f033 10642 if (imm_expr.X_add_number == 0)
c0ebe874 10643 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
b0e6f033 10644 else if (imm_expr.X_add_number == 1)
c0ebe874 10645 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
df58fc94 10646 else
252b5132 10647 {
df58fc94 10648 used_at = 1;
c0ebe874 10649 set_at (op[0], 0);
df58fc94
RS
10650 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10651 &offset_expr, AT, ZERO);
252b5132 10652 }
252b5132
RH
10653 break;
10654
10655 case M_BLEUL:
10656 likely = 1;
1a0670f3 10657 /* Fall through. */
252b5132 10658 case M_BLEU:
c0ebe874 10659 if (op[1] == 0)
df58fc94 10660 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874
RS
10661 &offset_expr, op[0], ZERO);
10662 else if (op[0] == 0)
df58fc94
RS
10663 goto do_true;
10664 else
252b5132 10665 {
df58fc94 10666 used_at = 1;
c0ebe874 10667 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10668 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10669 &offset_expr, AT, ZERO);
252b5132 10670 }
252b5132
RH
10671 break;
10672
10673 case M_BLEUL_I:
10674 likely = 1;
1a0670f3 10675 /* Fall through. */
252b5132 10676 case M_BLEU_I:
c0ebe874 10677 if (op[0] == 0
bad1aba3 10678 || (GPR_SIZE == 32
f01dc953 10679 && imm_expr.X_add_number == -1))
252b5132 10680 goto do_true;
f9419b05 10681 ++imm_expr.X_add_number;
6f2117ba 10682 /* Fall through. */
252b5132
RH
10683 case M_BLTU_I:
10684 case M_BLTUL_I:
10685 if (mask == M_BLTUL_I)
10686 likely = 1;
b0e6f033 10687 if (imm_expr.X_add_number == 0)
252b5132 10688 goto do_false;
b0e6f033 10689 else if (imm_expr.X_add_number == 1)
df58fc94 10690 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 10691 &offset_expr, op[0], ZERO);
df58fc94 10692 else
252b5132 10693 {
df58fc94 10694 used_at = 1;
c0ebe874 10695 set_at (op[0], 1);
df58fc94
RS
10696 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10697 &offset_expr, AT, ZERO);
252b5132 10698 }
252b5132
RH
10699 break;
10700
10701 case M_BLTL:
10702 likely = 1;
1a0670f3 10703 /* Fall through. */
252b5132 10704 case M_BLT:
c0ebe874
RS
10705 if (op[1] == 0)
10706 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10707 else if (op[0] == 0)
10708 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[1]);
df58fc94 10709 else
252b5132 10710 {
df58fc94 10711 used_at = 1;
c0ebe874 10712 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10713 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10714 &offset_expr, AT, ZERO);
252b5132 10715 }
252b5132
RH
10716 break;
10717
10718 case M_BLTUL:
10719 likely = 1;
1a0670f3 10720 /* Fall through. */
252b5132 10721 case M_BLTU:
c0ebe874 10722 if (op[1] == 0)
252b5132 10723 goto do_false;
c0ebe874 10724 else if (op[0] == 0)
df58fc94 10725 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 10726 &offset_expr, ZERO, op[1]);
df58fc94 10727 else
252b5132 10728 {
df58fc94 10729 used_at = 1;
c0ebe874 10730 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10731 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10732 &offset_expr, AT, ZERO);
252b5132 10733 }
252b5132
RH
10734 break;
10735
10736 case M_DDIV_3:
10737 dbl = 1;
1a0670f3 10738 /* Fall through. */
252b5132
RH
10739 case M_DIV_3:
10740 s = "mflo";
10741 goto do_div3;
10742 case M_DREM_3:
10743 dbl = 1;
1a0670f3 10744 /* Fall through. */
252b5132
RH
10745 case M_REM_3:
10746 s = "mfhi";
10747 do_div3:
c0ebe874 10748 if (op[2] == 0)
252b5132 10749 {
1661c76c 10750 as_warn (_("divide by zero"));
252b5132 10751 if (mips_trap)
df58fc94 10752 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 10753 else
df58fc94 10754 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 10755 break;
252b5132
RH
10756 }
10757
7d10b47d 10758 start_noreorder ();
252b5132
RH
10759 if (mips_trap)
10760 {
c0ebe874
RS
10761 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10762 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
252b5132
RH
10763 }
10764 else
10765 {
df58fc94
RS
10766 if (mips_opts.micromips)
10767 micromips_label_expr (&label_expr);
10768 else
10769 label_expr.X_add_number = 8;
c0ebe874
RS
10770 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10771 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
df58fc94
RS
10772 macro_build (NULL, "break", BRK_FMT, 7);
10773 if (mips_opts.micromips)
10774 micromips_add_label ();
252b5132
RH
10775 }
10776 expr1.X_add_number = -1;
8fc2e39e 10777 used_at = 1;
f6a22291 10778 load_register (AT, &expr1, dbl);
df58fc94
RS
10779 if (mips_opts.micromips)
10780 micromips_label_expr (&label_expr);
10781 else
10782 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
c0ebe874 10783 macro_build (&label_expr, "bne", "s,t,p", op[2], AT);
252b5132
RH
10784 if (dbl)
10785 {
10786 expr1.X_add_number = 1;
f6a22291 10787 load_register (AT, &expr1, dbl);
df58fc94 10788 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
252b5132
RH
10789 }
10790 else
10791 {
10792 expr1.X_add_number = 0x80000000;
df58fc94 10793 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
252b5132
RH
10794 }
10795 if (mips_trap)
10796 {
c0ebe874 10797 macro_build (NULL, "teq", TRAP_FMT, op[1], AT, 6);
252b5132
RH
10798 /* We want to close the noreorder block as soon as possible, so
10799 that later insns are available for delay slot filling. */
7d10b47d 10800 end_noreorder ();
252b5132
RH
10801 }
10802 else
10803 {
df58fc94
RS
10804 if (mips_opts.micromips)
10805 micromips_label_expr (&label_expr);
10806 else
10807 label_expr.X_add_number = 8;
c0ebe874 10808 macro_build (&label_expr, "bne", "s,t,p", op[1], AT);
a605d2b3 10809 macro_build (NULL, "nop", "");
252b5132
RH
10810
10811 /* We want to close the noreorder block as soon as possible, so
10812 that later insns are available for delay slot filling. */
7d10b47d 10813 end_noreorder ();
252b5132 10814
df58fc94 10815 macro_build (NULL, "break", BRK_FMT, 6);
252b5132 10816 }
df58fc94
RS
10817 if (mips_opts.micromips)
10818 micromips_add_label ();
c0ebe874 10819 macro_build (NULL, s, MFHL_FMT, op[0]);
252b5132
RH
10820 break;
10821
10822 case M_DIV_3I:
10823 s = "div";
10824 s2 = "mflo";
10825 goto do_divi;
10826 case M_DIVU_3I:
10827 s = "divu";
10828 s2 = "mflo";
10829 goto do_divi;
10830 case M_REM_3I:
10831 s = "div";
10832 s2 = "mfhi";
10833 goto do_divi;
10834 case M_REMU_3I:
10835 s = "divu";
10836 s2 = "mfhi";
10837 goto do_divi;
10838 case M_DDIV_3I:
10839 dbl = 1;
10840 s = "ddiv";
10841 s2 = "mflo";
10842 goto do_divi;
10843 case M_DDIVU_3I:
10844 dbl = 1;
10845 s = "ddivu";
10846 s2 = "mflo";
10847 goto do_divi;
10848 case M_DREM_3I:
10849 dbl = 1;
10850 s = "ddiv";
10851 s2 = "mfhi";
10852 goto do_divi;
10853 case M_DREMU_3I:
10854 dbl = 1;
10855 s = "ddivu";
10856 s2 = "mfhi";
10857 do_divi:
b0e6f033 10858 if (imm_expr.X_add_number == 0)
252b5132 10859 {
1661c76c 10860 as_warn (_("divide by zero"));
252b5132 10861 if (mips_trap)
df58fc94 10862 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 10863 else
df58fc94 10864 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 10865 break;
252b5132 10866 }
b0e6f033 10867 if (imm_expr.X_add_number == 1)
252b5132
RH
10868 {
10869 if (strcmp (s2, "mflo") == 0)
c0ebe874 10870 move_register (op[0], op[1]);
252b5132 10871 else
c0ebe874 10872 move_register (op[0], ZERO);
8fc2e39e 10873 break;
252b5132 10874 }
b0e6f033 10875 if (imm_expr.X_add_number == -1 && s[strlen (s) - 1] != 'u')
252b5132
RH
10876 {
10877 if (strcmp (s2, "mflo") == 0)
c0ebe874 10878 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", op[0], op[1]);
252b5132 10879 else
c0ebe874 10880 move_register (op[0], ZERO);
8fc2e39e 10881 break;
252b5132
RH
10882 }
10883
8fc2e39e 10884 used_at = 1;
67c0d1eb 10885 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
10886 macro_build (NULL, s, "z,s,t", op[1], AT);
10887 macro_build (NULL, s2, MFHL_FMT, op[0]);
252b5132
RH
10888 break;
10889
10890 case M_DIVU_3:
10891 s = "divu";
10892 s2 = "mflo";
10893 goto do_divu3;
10894 case M_REMU_3:
10895 s = "divu";
10896 s2 = "mfhi";
10897 goto do_divu3;
10898 case M_DDIVU_3:
10899 s = "ddivu";
10900 s2 = "mflo";
10901 goto do_divu3;
10902 case M_DREMU_3:
10903 s = "ddivu";
10904 s2 = "mfhi";
10905 do_divu3:
7d10b47d 10906 start_noreorder ();
252b5132
RH
10907 if (mips_trap)
10908 {
c0ebe874
RS
10909 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10910 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
10911 /* We want to close the noreorder block as soon as possible, so
10912 that later insns are available for delay slot filling. */
7d10b47d 10913 end_noreorder ();
252b5132
RH
10914 }
10915 else
10916 {
df58fc94
RS
10917 if (mips_opts.micromips)
10918 micromips_label_expr (&label_expr);
10919 else
10920 label_expr.X_add_number = 8;
c0ebe874
RS
10921 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10922 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
10923
10924 /* We want to close the noreorder block as soon as possible, so
10925 that later insns are available for delay slot filling. */
7d10b47d 10926 end_noreorder ();
df58fc94
RS
10927 macro_build (NULL, "break", BRK_FMT, 7);
10928 if (mips_opts.micromips)
10929 micromips_add_label ();
252b5132 10930 }
c0ebe874 10931 macro_build (NULL, s2, MFHL_FMT, op[0]);
8fc2e39e 10932 break;
252b5132 10933
1abe91b1
MR
10934 case M_DLCA_AB:
10935 dbl = 1;
1a0670f3 10936 /* Fall through. */
1abe91b1
MR
10937 case M_LCA_AB:
10938 call = 1;
10939 goto do_la;
252b5132
RH
10940 case M_DLA_AB:
10941 dbl = 1;
1a0670f3 10942 /* Fall through. */
252b5132 10943 case M_LA_AB:
1abe91b1 10944 do_la:
252b5132
RH
10945 /* Load the address of a symbol into a register. If breg is not
10946 zero, we then add a base register to it. */
10947
c0ebe874 10948 breg = op[2];
bad1aba3 10949 if (dbl && GPR_SIZE == 32)
ece794d9
MF
10950 as_warn (_("dla used to load 32-bit register; recommend using la "
10951 "instead"));
3bec30a8 10952
90ecf173 10953 if (!dbl && HAVE_64BIT_OBJECTS)
ece794d9
MF
10954 as_warn (_("la used to load 64-bit address; recommend using dla "
10955 "instead"));
3bec30a8 10956
f2ae14a1 10957 if (small_offset_p (0, align, 16))
0c11417f 10958 {
c0ebe874 10959 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", op[0], breg,
f2ae14a1 10960 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8fc2e39e 10961 break;
0c11417f
MR
10962 }
10963
c0ebe874 10964 if (mips_opts.at && (op[0] == breg))
afdbd6d0
CD
10965 {
10966 tempreg = AT;
10967 used_at = 1;
10968 }
10969 else
c0ebe874 10970 tempreg = op[0];
afdbd6d0 10971
252b5132
RH
10972 if (offset_expr.X_op != O_symbol
10973 && offset_expr.X_op != O_constant)
10974 {
1661c76c 10975 as_bad (_("expression too complex"));
252b5132
RH
10976 offset_expr.X_op = O_constant;
10977 }
10978
252b5132 10979 if (offset_expr.X_op == O_constant)
aed1a261 10980 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
10981 else if (mips_pic == NO_PIC)
10982 {
d6bc6245 10983 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 10984 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
10985 Otherwise we want
10986 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
10987 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10988 If we have a constant, we need two instructions anyhow,
d6bc6245 10989 so we may as well always use the latter form.
76b3015f 10990
6caf9ef4
TS
10991 With 64bit address space and a usable $at we want
10992 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10993 lui $at,<sym> (BFD_RELOC_HI16_S)
10994 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10995 daddiu $at,<sym> (BFD_RELOC_LO16)
10996 dsll32 $tempreg,0
10997 daddu $tempreg,$tempreg,$at
10998
10999 If $at is already in use, we use a path which is suboptimal
11000 on superscalar processors.
11001 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11002 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11003 dsll $tempreg,16
11004 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
11005 dsll $tempreg,16
11006 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
11007
11008 For GP relative symbols in 64bit address space we can use
11009 the same sequence as in 32bit address space. */
aed1a261 11010 if (HAVE_64BIT_SYMBOLS)
252b5132 11011 {
6caf9ef4
TS
11012 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
11013 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
11014 {
11015 relax_start (offset_expr.X_add_symbol);
11016 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11017 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
11018 relax_switch ();
11019 }
d6bc6245 11020
741fe287 11021 if (used_at == 0 && mips_opts.at)
98d3f06f 11022 {
df58fc94 11023 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 11024 tempreg, BFD_RELOC_MIPS_HIGHEST);
df58fc94 11025 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 11026 AT, BFD_RELOC_HI16_S);
67c0d1eb 11027 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 11028 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 11029 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 11030 AT, AT, BFD_RELOC_LO16);
df58fc94 11031 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 11032 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
11033 used_at = 1;
11034 }
11035 else
11036 {
df58fc94 11037 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 11038 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 11039 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 11040 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 11041 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 11042 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 11043 tempreg, tempreg, BFD_RELOC_HI16_S);
df58fc94 11044 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 11045 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 11046 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 11047 }
6caf9ef4
TS
11048
11049 if (mips_relax.sequence)
11050 relax_end ();
98d3f06f
KH
11051 }
11052 else
11053 {
11054 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 11055 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 11056 {
4d7206a2 11057 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11058 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11059 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 11060 relax_switch ();
98d3f06f 11061 }
6943caf0 11062 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
1661c76c 11063 as_bad (_("offset too large"));
67c0d1eb
RS
11064 macro_build_lui (&offset_expr, tempreg);
11065 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11066 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
11067 if (mips_relax.sequence)
11068 relax_end ();
98d3f06f 11069 }
252b5132 11070 }
0a44bf69 11071 else if (!mips_big_got && !HAVE_NEWABI)
252b5132 11072 {
9117d219
NC
11073 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
11074
252b5132
RH
11075 /* If this is a reference to an external symbol, and there
11076 is no constant, we want
11077 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 11078 or for lca or if tempreg is PIC_CALL_REG
9117d219 11079 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
11080 For a local symbol, we want
11081 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11082 nop
11083 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
11084
11085 If we have a small constant, and this is a reference to
11086 an external symbol, we want
11087 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11088 nop
11089 addiu $tempreg,$tempreg,<constant>
11090 For a local symbol, we want the same instruction
11091 sequence, but we output a BFD_RELOC_LO16 reloc on the
11092 addiu instruction.
11093
11094 If we have a large constant, and this is a reference to
11095 an external symbol, we want
11096 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11097 lui $at,<hiconstant>
11098 addiu $at,$at,<loconstant>
11099 addu $tempreg,$tempreg,$at
11100 For a local symbol, we want the same instruction
11101 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 11102 addiu instruction.
ed6fb7bd
SC
11103 */
11104
4d7206a2 11105 if (offset_expr.X_add_number == 0)
252b5132 11106 {
0a44bf69
RS
11107 if (mips_pic == SVR4_PIC
11108 && breg == 0
11109 && (call || tempreg == PIC_CALL_REG))
4d7206a2
RS
11110 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
11111
11112 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11113 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11114 lw_reloc_type, mips_gp_register);
4d7206a2 11115 if (breg != 0)
252b5132
RH
11116 {
11117 /* We're going to put in an addu instruction using
11118 tempreg, so we may as well insert the nop right
11119 now. */
269137b2 11120 load_delay_nop ();
252b5132 11121 }
4d7206a2 11122 relax_switch ();
67c0d1eb
RS
11123 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11124 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 11125 load_delay_nop ();
67c0d1eb
RS
11126 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11127 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 11128 relax_end ();
252b5132
RH
11129 /* FIXME: If breg == 0, and the next instruction uses
11130 $tempreg, then if this variant case is used an extra
11131 nop will be generated. */
11132 }
4d7206a2
RS
11133 else if (offset_expr.X_add_number >= -0x8000
11134 && offset_expr.X_add_number < 0x8000)
252b5132 11135 {
67c0d1eb 11136 load_got_offset (tempreg, &offset_expr);
269137b2 11137 load_delay_nop ();
67c0d1eb 11138 add_got_offset (tempreg, &offset_expr);
252b5132
RH
11139 }
11140 else
11141 {
4d7206a2
RS
11142 expr1.X_add_number = offset_expr.X_add_number;
11143 offset_expr.X_add_number =
43c0598f 11144 SEXT_16BIT (offset_expr.X_add_number);
67c0d1eb 11145 load_got_offset (tempreg, &offset_expr);
f6a22291 11146 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
11147 /* If we are going to add in a base register, and the
11148 target register and the base register are the same,
11149 then we are using AT as a temporary register. Since
11150 we want to load the constant into AT, we add our
11151 current AT (from the global offset table) and the
11152 register into the register now, and pretend we were
11153 not using a base register. */
c0ebe874 11154 if (breg == op[0])
252b5132 11155 {
269137b2 11156 load_delay_nop ();
67c0d1eb 11157 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 11158 op[0], AT, breg);
252b5132 11159 breg = 0;
c0ebe874 11160 tempreg = op[0];
252b5132 11161 }
f6a22291 11162 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
11163 used_at = 1;
11164 }
11165 }
0a44bf69 11166 else if (!mips_big_got && HAVE_NEWABI)
f5040a92 11167 {
67c0d1eb 11168 int add_breg_early = 0;
f5040a92
AO
11169
11170 /* If this is a reference to an external, and there is no
11171 constant, or local symbol (*), with or without a
11172 constant, we want
11173 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 11174 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
11175 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11176
11177 If we have a small constant, and this is a reference to
11178 an external symbol, we want
11179 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
11180 addiu $tempreg,$tempreg,<constant>
11181
11182 If we have a large constant, and this is a reference to
11183 an external symbol, we want
11184 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
11185 lui $at,<hiconstant>
11186 addiu $at,$at,<loconstant>
11187 addu $tempreg,$tempreg,$at
11188
11189 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
11190 local symbols, even though it introduces an additional
11191 instruction. */
11192
f5040a92
AO
11193 if (offset_expr.X_add_number)
11194 {
4d7206a2 11195 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
11196 offset_expr.X_add_number = 0;
11197
4d7206a2 11198 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11199 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11200 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
11201
11202 if (expr1.X_add_number >= -0x8000
11203 && expr1.X_add_number < 0x8000)
11204 {
67c0d1eb
RS
11205 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11206 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 11207 }
ecd13cd3 11208 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 11209 {
c0ebe874
RS
11210 unsigned int dreg;
11211
f5040a92
AO
11212 /* If we are going to add in a base register, and the
11213 target register and the base register are the same,
11214 then we are using AT as a temporary register. Since
11215 we want to load the constant into AT, we add our
11216 current AT (from the global offset table) and the
11217 register into the register now, and pretend we were
11218 not using a base register. */
c0ebe874 11219 if (breg != op[0])
f5040a92
AO
11220 dreg = tempreg;
11221 else
11222 {
9c2799c2 11223 gas_assert (tempreg == AT);
67c0d1eb 11224 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11225 op[0], AT, breg);
11226 dreg = op[0];
67c0d1eb 11227 add_breg_early = 1;
f5040a92
AO
11228 }
11229
f6a22291 11230 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 11231 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11232 dreg, dreg, AT);
f5040a92 11233
f5040a92
AO
11234 used_at = 1;
11235 }
11236 else
11237 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11238
4d7206a2 11239 relax_switch ();
f5040a92
AO
11240 offset_expr.X_add_number = expr1.X_add_number;
11241
67c0d1eb
RS
11242 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11243 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11244 if (add_breg_early)
f5040a92 11245 {
67c0d1eb 11246 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 11247 op[0], tempreg, breg);
f5040a92 11248 breg = 0;
c0ebe874 11249 tempreg = op[0];
f5040a92 11250 }
4d7206a2 11251 relax_end ();
f5040a92 11252 }
4d7206a2 11253 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 11254 {
4d7206a2 11255 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11256 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11257 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 11258 relax_switch ();
67c0d1eb
RS
11259 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11260 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 11261 relax_end ();
f5040a92 11262 }
4d7206a2 11263 else
f5040a92 11264 {
67c0d1eb
RS
11265 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11266 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
11267 }
11268 }
0a44bf69 11269 else if (mips_big_got && !HAVE_NEWABI)
252b5132 11270 {
67c0d1eb 11271 int gpdelay;
9117d219
NC
11272 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11273 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 11274 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
11275
11276 /* This is the large GOT case. If this is a reference to an
11277 external symbol, and there is no constant, we want
11278 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11279 addu $tempreg,$tempreg,$gp
11280 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 11281 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
11282 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11283 addu $tempreg,$tempreg,$gp
11284 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
11285 For a local symbol, we want
11286 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11287 nop
11288 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
11289
11290 If we have a small constant, and this is a reference to
11291 an external symbol, we want
11292 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11293 addu $tempreg,$tempreg,$gp
11294 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11295 nop
11296 addiu $tempreg,$tempreg,<constant>
11297 For a local symbol, we want
11298 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11299 nop
11300 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
11301
11302 If we have a large constant, and this is a reference to
11303 an external symbol, we want
11304 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11305 addu $tempreg,$tempreg,$gp
11306 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11307 lui $at,<hiconstant>
11308 addiu $at,$at,<loconstant>
11309 addu $tempreg,$tempreg,$at
11310 For a local symbol, we want
11311 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11312 lui $at,<hiconstant>
11313 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
11314 addu $tempreg,$tempreg,$at
f5040a92 11315 */
438c16b8 11316
252b5132
RH
11317 expr1.X_add_number = offset_expr.X_add_number;
11318 offset_expr.X_add_number = 0;
4d7206a2 11319 relax_start (offset_expr.X_add_symbol);
67c0d1eb 11320 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
11321 if (expr1.X_add_number == 0 && breg == 0
11322 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
11323 {
11324 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11325 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11326 }
df58fc94 11327 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 11328 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11329 tempreg, tempreg, mips_gp_register);
67c0d1eb 11330 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 11331 tempreg, lw_reloc_type, tempreg);
252b5132
RH
11332 if (expr1.X_add_number == 0)
11333 {
67c0d1eb 11334 if (breg != 0)
252b5132
RH
11335 {
11336 /* We're going to put in an addu instruction using
11337 tempreg, so we may as well insert the nop right
11338 now. */
269137b2 11339 load_delay_nop ();
252b5132 11340 }
252b5132
RH
11341 }
11342 else if (expr1.X_add_number >= -0x8000
11343 && expr1.X_add_number < 0x8000)
11344 {
269137b2 11345 load_delay_nop ();
67c0d1eb 11346 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11347 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
11348 }
11349 else
11350 {
c0ebe874
RS
11351 unsigned int dreg;
11352
252b5132
RH
11353 /* If we are going to add in a base register, and the
11354 target register and the base register are the same,
11355 then we are using AT as a temporary register. Since
11356 we want to load the constant into AT, we add our
11357 current AT (from the global offset table) and the
11358 register into the register now, and pretend we were
11359 not using a base register. */
c0ebe874 11360 if (breg != op[0])
67c0d1eb 11361 dreg = tempreg;
252b5132
RH
11362 else
11363 {
9c2799c2 11364 gas_assert (tempreg == AT);
269137b2 11365 load_delay_nop ();
67c0d1eb 11366 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11367 op[0], AT, breg);
11368 dreg = op[0];
252b5132
RH
11369 }
11370
f6a22291 11371 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 11372 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 11373
252b5132
RH
11374 used_at = 1;
11375 }
43c0598f 11376 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
4d7206a2 11377 relax_switch ();
252b5132 11378
67c0d1eb 11379 if (gpdelay)
252b5132
RH
11380 {
11381 /* This is needed because this instruction uses $gp, but
f5040a92 11382 the first instruction on the main stream does not. */
67c0d1eb 11383 macro_build (NULL, "nop", "");
252b5132 11384 }
ed6fb7bd 11385
67c0d1eb
RS
11386 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11387 local_reloc_type, mips_gp_register);
f5040a92 11388 if (expr1.X_add_number >= -0x8000
252b5132
RH
11389 && expr1.X_add_number < 0x8000)
11390 {
269137b2 11391 load_delay_nop ();
67c0d1eb
RS
11392 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11393 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 11394 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
11395 register, the external symbol case ended with a load,
11396 so if the symbol turns out to not be external, and
11397 the next instruction uses tempreg, an unnecessary nop
11398 will be inserted. */
252b5132
RH
11399 }
11400 else
11401 {
c0ebe874 11402 if (breg == op[0])
252b5132
RH
11403 {
11404 /* We must add in the base register now, as in the
f5040a92 11405 external symbol case. */
9c2799c2 11406 gas_assert (tempreg == AT);
269137b2 11407 load_delay_nop ();
67c0d1eb 11408 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11409 op[0], AT, breg);
11410 tempreg = op[0];
252b5132 11411 /* We set breg to 0 because we have arranged to add
f5040a92 11412 it in in both cases. */
252b5132
RH
11413 breg = 0;
11414 }
11415
67c0d1eb
RS
11416 macro_build_lui (&expr1, AT);
11417 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11418 AT, AT, BFD_RELOC_LO16);
67c0d1eb 11419 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11420 tempreg, tempreg, AT);
8fc2e39e 11421 used_at = 1;
252b5132 11422 }
4d7206a2 11423 relax_end ();
252b5132 11424 }
0a44bf69 11425 else if (mips_big_got && HAVE_NEWABI)
f5040a92 11426 {
f5040a92
AO
11427 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11428 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 11429 int add_breg_early = 0;
f5040a92
AO
11430
11431 /* This is the large GOT case. If this is a reference to an
11432 external symbol, and there is no constant, we want
11433 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11434 add $tempreg,$tempreg,$gp
11435 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 11436 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
11437 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11438 add $tempreg,$tempreg,$gp
11439 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
11440
11441 If we have a small constant, and this is a reference to
11442 an external symbol, we want
11443 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11444 add $tempreg,$tempreg,$gp
11445 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11446 addi $tempreg,$tempreg,<constant>
11447
11448 If we have a large constant, and this is a reference to
11449 an external symbol, we want
11450 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11451 addu $tempreg,$tempreg,$gp
11452 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11453 lui $at,<hiconstant>
11454 addi $at,$at,<loconstant>
11455 add $tempreg,$tempreg,$at
11456
11457 If we have NewABI, and we know it's a local symbol, we want
11458 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
11459 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
11460 otherwise we have to resort to GOT_HI16/GOT_LO16. */
11461
4d7206a2 11462 relax_start (offset_expr.X_add_symbol);
f5040a92 11463
4d7206a2 11464 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
11465 offset_expr.X_add_number = 0;
11466
1abe91b1
MR
11467 if (expr1.X_add_number == 0 && breg == 0
11468 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
11469 {
11470 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11471 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11472 }
df58fc94 11473 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 11474 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11475 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
11476 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11477 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
11478
11479 if (expr1.X_add_number == 0)
4d7206a2 11480 ;
f5040a92
AO
11481 else if (expr1.X_add_number >= -0x8000
11482 && expr1.X_add_number < 0x8000)
11483 {
67c0d1eb 11484 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11485 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 11486 }
ecd13cd3 11487 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 11488 {
c0ebe874
RS
11489 unsigned int dreg;
11490
f5040a92
AO
11491 /* If we are going to add in a base register, and the
11492 target register and the base register are the same,
11493 then we are using AT as a temporary register. Since
11494 we want to load the constant into AT, we add our
11495 current AT (from the global offset table) and the
11496 register into the register now, and pretend we were
11497 not using a base register. */
c0ebe874 11498 if (breg != op[0])
f5040a92
AO
11499 dreg = tempreg;
11500 else
11501 {
9c2799c2 11502 gas_assert (tempreg == AT);
67c0d1eb 11503 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11504 op[0], AT, breg);
11505 dreg = op[0];
67c0d1eb 11506 add_breg_early = 1;
f5040a92
AO
11507 }
11508
f6a22291 11509 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 11510 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 11511
f5040a92
AO
11512 used_at = 1;
11513 }
11514 else
11515 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11516
4d7206a2 11517 relax_switch ();
f5040a92 11518 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
11519 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11520 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
11521 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11522 tempreg, BFD_RELOC_MIPS_GOT_OFST);
11523 if (add_breg_early)
f5040a92 11524 {
67c0d1eb 11525 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 11526 op[0], tempreg, breg);
f5040a92 11527 breg = 0;
c0ebe874 11528 tempreg = op[0];
f5040a92 11529 }
4d7206a2 11530 relax_end ();
f5040a92 11531 }
252b5132
RH
11532 else
11533 abort ();
11534
11535 if (breg != 0)
c0ebe874 11536 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", op[0], tempreg, breg);
252b5132
RH
11537 break;
11538
52b6b6b9 11539 case M_MSGSND:
df58fc94 11540 gas_assert (!mips_opts.micromips);
c0ebe874 11541 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x01);
c7af4273 11542 break;
52b6b6b9
JM
11543
11544 case M_MSGLD:
df58fc94 11545 gas_assert (!mips_opts.micromips);
c8276761 11546 macro_build (NULL, "c2", "C", 0x02);
c7af4273 11547 break;
52b6b6b9
JM
11548
11549 case M_MSGLD_T:
df58fc94 11550 gas_assert (!mips_opts.micromips);
c0ebe874 11551 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x02);
c7af4273 11552 break;
52b6b6b9
JM
11553
11554 case M_MSGWAIT:
df58fc94 11555 gas_assert (!mips_opts.micromips);
52b6b6b9 11556 macro_build (NULL, "c2", "C", 3);
c7af4273 11557 break;
52b6b6b9
JM
11558
11559 case M_MSGWAIT_T:
df58fc94 11560 gas_assert (!mips_opts.micromips);
c0ebe874 11561 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x03);
c7af4273 11562 break;
52b6b6b9 11563
252b5132
RH
11564 case M_J_A:
11565 /* The j instruction may not be used in PIC code, since it
11566 requires an absolute address. We convert it to a b
11567 instruction. */
11568 if (mips_pic == NO_PIC)
67c0d1eb 11569 macro_build (&offset_expr, "j", "a");
252b5132 11570 else
67c0d1eb 11571 macro_build (&offset_expr, "b", "p");
8fc2e39e 11572 break;
252b5132
RH
11573
11574 /* The jal instructions must be handled as macros because when
11575 generating PIC code they expand to multi-instruction
11576 sequences. Normally they are simple instructions. */
df58fc94 11577 case M_JALS_1:
c0ebe874
RS
11578 op[1] = op[0];
11579 op[0] = RA;
df58fc94
RS
11580 /* Fall through. */
11581 case M_JALS_2:
11582 gas_assert (mips_opts.micromips);
833794fc
MR
11583 if (mips_opts.insn32)
11584 {
1661c76c 11585 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
833794fc
MR
11586 break;
11587 }
df58fc94
RS
11588 jals = 1;
11589 goto jal;
252b5132 11590 case M_JAL_1:
c0ebe874
RS
11591 op[1] = op[0];
11592 op[0] = RA;
252b5132
RH
11593 /* Fall through. */
11594 case M_JAL_2:
df58fc94 11595 jal:
3e722fb5 11596 if (mips_pic == NO_PIC)
df58fc94
RS
11597 {
11598 s = jals ? "jalrs" : "jalr";
e64af278 11599 if (mips_opts.micromips
833794fc 11600 && !mips_opts.insn32
c0ebe874 11601 && op[0] == RA
e64af278 11602 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 11603 macro_build (NULL, s, "mj", op[1]);
df58fc94 11604 else
c0ebe874 11605 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
df58fc94 11606 }
0a44bf69 11607 else
252b5132 11608 {
df58fc94
RS
11609 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
11610 && mips_cprestore_offset >= 0);
11611
c0ebe874 11612 if (op[1] != PIC_CALL_REG)
252b5132 11613 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 11614
833794fc
MR
11615 s = ((mips_opts.micromips
11616 && !mips_opts.insn32
11617 && (!mips_opts.noreorder || cprestore))
df58fc94 11618 ? "jalrs" : "jalr");
e64af278 11619 if (mips_opts.micromips
833794fc 11620 && !mips_opts.insn32
c0ebe874 11621 && op[0] == RA
e64af278 11622 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 11623 macro_build (NULL, s, "mj", op[1]);
df58fc94 11624 else
c0ebe874 11625 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
0a44bf69 11626 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
252b5132 11627 {
6478892d 11628 if (mips_cprestore_offset < 0)
1661c76c 11629 as_warn (_("no .cprestore pseudo-op used in PIC code"));
6478892d
TS
11630 else
11631 {
90ecf173 11632 if (!mips_frame_reg_valid)
7a621144 11633 {
1661c76c 11634 as_warn (_("no .frame pseudo-op used in PIC code"));
7a621144
DJ
11635 /* Quiet this warning. */
11636 mips_frame_reg_valid = 1;
11637 }
90ecf173 11638 if (!mips_cprestore_valid)
7a621144 11639 {
1661c76c 11640 as_warn (_("no .cprestore pseudo-op used in PIC code"));
7a621144
DJ
11641 /* Quiet this warning. */
11642 mips_cprestore_valid = 1;
11643 }
d3fca0b5
MR
11644 if (mips_opts.noreorder)
11645 macro_build (NULL, "nop", "");
6478892d 11646 expr1.X_add_number = mips_cprestore_offset;
134c0c8b 11647 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 11648 mips_gp_register,
256ab948
TS
11649 mips_frame_reg,
11650 HAVE_64BIT_ADDRESSES);
6478892d 11651 }
252b5132
RH
11652 }
11653 }
252b5132 11654
8fc2e39e 11655 break;
252b5132 11656
df58fc94
RS
11657 case M_JALS_A:
11658 gas_assert (mips_opts.micromips);
833794fc
MR
11659 if (mips_opts.insn32)
11660 {
1661c76c 11661 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
833794fc
MR
11662 break;
11663 }
df58fc94
RS
11664 jals = 1;
11665 /* Fall through. */
252b5132
RH
11666 case M_JAL_A:
11667 if (mips_pic == NO_PIC)
df58fc94 11668 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
252b5132
RH
11669 else if (mips_pic == SVR4_PIC)
11670 {
11671 /* If this is a reference to an external symbol, and we are
11672 using a small GOT, we want
11673 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11674 nop
f9419b05 11675 jalr $ra,$25
252b5132
RH
11676 nop
11677 lw $gp,cprestore($sp)
11678 The cprestore value is set using the .cprestore
11679 pseudo-op. If we are using a big GOT, we want
11680 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11681 addu $25,$25,$gp
11682 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
11683 nop
f9419b05 11684 jalr $ra,$25
252b5132
RH
11685 nop
11686 lw $gp,cprestore($sp)
11687 If the symbol is not external, we want
11688 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11689 nop
11690 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 11691 jalr $ra,$25
252b5132 11692 nop
438c16b8 11693 lw $gp,cprestore($sp)
f5040a92
AO
11694
11695 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
11696 sequences above, minus nops, unless the symbol is local,
11697 which enables us to use GOT_PAGE/GOT_OFST (big got) or
11698 GOT_DISP. */
438c16b8 11699 if (HAVE_NEWABI)
252b5132 11700 {
90ecf173 11701 if (!mips_big_got)
f5040a92 11702 {
4d7206a2 11703 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11704 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11705 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 11706 mips_gp_register);
4d7206a2 11707 relax_switch ();
67c0d1eb
RS
11708 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11709 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
11710 mips_gp_register);
11711 relax_end ();
f5040a92
AO
11712 }
11713 else
11714 {
4d7206a2 11715 relax_start (offset_expr.X_add_symbol);
df58fc94 11716 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
11717 BFD_RELOC_MIPS_CALL_HI16);
11718 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11719 PIC_CALL_REG, mips_gp_register);
11720 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11721 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11722 PIC_CALL_REG);
4d7206a2 11723 relax_switch ();
67c0d1eb
RS
11724 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11725 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
11726 mips_gp_register);
11727 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11728 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 11729 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 11730 relax_end ();
f5040a92 11731 }
684022ea 11732
df58fc94 11733 macro_build_jalr (&offset_expr, 0);
252b5132
RH
11734 }
11735 else
11736 {
4d7206a2 11737 relax_start (offset_expr.X_add_symbol);
90ecf173 11738 if (!mips_big_got)
438c16b8 11739 {
67c0d1eb
RS
11740 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11741 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 11742 mips_gp_register);
269137b2 11743 load_delay_nop ();
4d7206a2 11744 relax_switch ();
438c16b8 11745 }
252b5132 11746 else
252b5132 11747 {
67c0d1eb
RS
11748 int gpdelay;
11749
11750 gpdelay = reg_needs_delay (mips_gp_register);
df58fc94 11751 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
11752 BFD_RELOC_MIPS_CALL_HI16);
11753 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11754 PIC_CALL_REG, mips_gp_register);
11755 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11756 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11757 PIC_CALL_REG);
269137b2 11758 load_delay_nop ();
4d7206a2 11759 relax_switch ();
67c0d1eb
RS
11760 if (gpdelay)
11761 macro_build (NULL, "nop", "");
252b5132 11762 }
67c0d1eb
RS
11763 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11764 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 11765 mips_gp_register);
269137b2 11766 load_delay_nop ();
67c0d1eb
RS
11767 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11768 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 11769 relax_end ();
df58fc94 11770 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
438c16b8 11771
6478892d 11772 if (mips_cprestore_offset < 0)
1661c76c 11773 as_warn (_("no .cprestore pseudo-op used in PIC code"));
6478892d
TS
11774 else
11775 {
90ecf173 11776 if (!mips_frame_reg_valid)
7a621144 11777 {
1661c76c 11778 as_warn (_("no .frame pseudo-op used in PIC code"));
7a621144
DJ
11779 /* Quiet this warning. */
11780 mips_frame_reg_valid = 1;
11781 }
90ecf173 11782 if (!mips_cprestore_valid)
7a621144 11783 {
1661c76c 11784 as_warn (_("no .cprestore pseudo-op used in PIC code"));
7a621144
DJ
11785 /* Quiet this warning. */
11786 mips_cprestore_valid = 1;
11787 }
6478892d 11788 if (mips_opts.noreorder)
67c0d1eb 11789 macro_build (NULL, "nop", "");
6478892d 11790 expr1.X_add_number = mips_cprestore_offset;
134c0c8b 11791 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 11792 mips_gp_register,
256ab948
TS
11793 mips_frame_reg,
11794 HAVE_64BIT_ADDRESSES);
6478892d 11795 }
252b5132
RH
11796 }
11797 }
0a44bf69 11798 else if (mips_pic == VXWORKS_PIC)
1661c76c 11799 as_bad (_("non-PIC jump used in PIC library"));
252b5132
RH
11800 else
11801 abort ();
11802
8fc2e39e 11803 break;
252b5132 11804
7f3c4072 11805 case M_LBUE_AB:
7f3c4072
CM
11806 s = "lbue";
11807 fmt = "t,+j(b)";
11808 offbits = 9;
11809 goto ld_st;
11810 case M_LHUE_AB:
7f3c4072
CM
11811 s = "lhue";
11812 fmt = "t,+j(b)";
11813 offbits = 9;
11814 goto ld_st;
11815 case M_LBE_AB:
7f3c4072
CM
11816 s = "lbe";
11817 fmt = "t,+j(b)";
11818 offbits = 9;
11819 goto ld_st;
11820 case M_LHE_AB:
7f3c4072
CM
11821 s = "lhe";
11822 fmt = "t,+j(b)";
11823 offbits = 9;
11824 goto ld_st;
11825 case M_LLE_AB:
7f3c4072
CM
11826 s = "lle";
11827 fmt = "t,+j(b)";
11828 offbits = 9;
11829 goto ld_st;
11830 case M_LWE_AB:
7f3c4072
CM
11831 s = "lwe";
11832 fmt = "t,+j(b)";
11833 offbits = 9;
11834 goto ld_st;
11835 case M_LWLE_AB:
7f3c4072
CM
11836 s = "lwle";
11837 fmt = "t,+j(b)";
11838 offbits = 9;
11839 goto ld_st;
11840 case M_LWRE_AB:
7f3c4072
CM
11841 s = "lwre";
11842 fmt = "t,+j(b)";
11843 offbits = 9;
11844 goto ld_st;
11845 case M_SBE_AB:
7f3c4072
CM
11846 s = "sbe";
11847 fmt = "t,+j(b)";
11848 offbits = 9;
11849 goto ld_st;
11850 case M_SCE_AB:
7f3c4072
CM
11851 s = "sce";
11852 fmt = "t,+j(b)";
11853 offbits = 9;
11854 goto ld_st;
11855 case M_SHE_AB:
7f3c4072
CM
11856 s = "she";
11857 fmt = "t,+j(b)";
11858 offbits = 9;
11859 goto ld_st;
11860 case M_SWE_AB:
7f3c4072
CM
11861 s = "swe";
11862 fmt = "t,+j(b)";
11863 offbits = 9;
11864 goto ld_st;
11865 case M_SWLE_AB:
7f3c4072
CM
11866 s = "swle";
11867 fmt = "t,+j(b)";
11868 offbits = 9;
11869 goto ld_st;
11870 case M_SWRE_AB:
7f3c4072
CM
11871 s = "swre";
11872 fmt = "t,+j(b)";
11873 offbits = 9;
11874 goto ld_st;
dec0624d 11875 case M_ACLR_AB:
dec0624d 11876 s = "aclr";
dec0624d 11877 fmt = "\\,~(b)";
7f3c4072 11878 offbits = 12;
dec0624d
MR
11879 goto ld_st;
11880 case M_ASET_AB:
dec0624d 11881 s = "aset";
dec0624d 11882 fmt = "\\,~(b)";
7f3c4072 11883 offbits = 12;
dec0624d 11884 goto ld_st;
252b5132
RH
11885 case M_LB_AB:
11886 s = "lb";
df58fc94 11887 fmt = "t,o(b)";
252b5132
RH
11888 goto ld;
11889 case M_LBU_AB:
11890 s = "lbu";
df58fc94 11891 fmt = "t,o(b)";
252b5132
RH
11892 goto ld;
11893 case M_LH_AB:
11894 s = "lh";
df58fc94 11895 fmt = "t,o(b)";
252b5132
RH
11896 goto ld;
11897 case M_LHU_AB:
11898 s = "lhu";
df58fc94 11899 fmt = "t,o(b)";
252b5132
RH
11900 goto ld;
11901 case M_LW_AB:
11902 s = "lw";
df58fc94 11903 fmt = "t,o(b)";
252b5132
RH
11904 goto ld;
11905 case M_LWC0_AB:
df58fc94 11906 gas_assert (!mips_opts.micromips);
252b5132 11907 s = "lwc0";
df58fc94 11908 fmt = "E,o(b)";
bdaaa2e1 11909 /* Itbl support may require additional care here. */
252b5132 11910 coproc = 1;
df58fc94 11911 goto ld_st;
252b5132
RH
11912 case M_LWC1_AB:
11913 s = "lwc1";
df58fc94 11914 fmt = "T,o(b)";
bdaaa2e1 11915 /* Itbl support may require additional care here. */
252b5132 11916 coproc = 1;
df58fc94 11917 goto ld_st;
252b5132
RH
11918 case M_LWC2_AB:
11919 s = "lwc2";
df58fc94 11920 fmt = COP12_FMT;
7361da2c
AB
11921 offbits = (mips_opts.micromips ? 12
11922 : ISA_IS_R6 (mips_opts.isa) ? 11
11923 : 16);
bdaaa2e1 11924 /* Itbl support may require additional care here. */
252b5132 11925 coproc = 1;
df58fc94 11926 goto ld_st;
252b5132 11927 case M_LWC3_AB:
df58fc94 11928 gas_assert (!mips_opts.micromips);
252b5132 11929 s = "lwc3";
df58fc94 11930 fmt = "E,o(b)";
bdaaa2e1 11931 /* Itbl support may require additional care here. */
252b5132 11932 coproc = 1;
df58fc94 11933 goto ld_st;
252b5132
RH
11934 case M_LWL_AB:
11935 s = "lwl";
df58fc94 11936 fmt = MEM12_FMT;
7f3c4072 11937 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11938 goto ld_st;
252b5132
RH
11939 case M_LWR_AB:
11940 s = "lwr";
df58fc94 11941 fmt = MEM12_FMT;
7f3c4072 11942 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11943 goto ld_st;
252b5132 11944 case M_LDC1_AB:
252b5132 11945 s = "ldc1";
df58fc94 11946 fmt = "T,o(b)";
bdaaa2e1 11947 /* Itbl support may require additional care here. */
252b5132 11948 coproc = 1;
df58fc94 11949 goto ld_st;
252b5132
RH
11950 case M_LDC2_AB:
11951 s = "ldc2";
df58fc94 11952 fmt = COP12_FMT;
7361da2c
AB
11953 offbits = (mips_opts.micromips ? 12
11954 : ISA_IS_R6 (mips_opts.isa) ? 11
11955 : 16);
bdaaa2e1 11956 /* Itbl support may require additional care here. */
252b5132 11957 coproc = 1;
df58fc94 11958 goto ld_st;
c77c0862 11959 case M_LQC2_AB:
c77c0862 11960 s = "lqc2";
14daeee3 11961 fmt = "+7,o(b)";
c77c0862
RS
11962 /* Itbl support may require additional care here. */
11963 coproc = 1;
11964 goto ld_st;
252b5132
RH
11965 case M_LDC3_AB:
11966 s = "ldc3";
df58fc94 11967 fmt = "E,o(b)";
bdaaa2e1 11968 /* Itbl support may require additional care here. */
252b5132 11969 coproc = 1;
df58fc94 11970 goto ld_st;
252b5132
RH
11971 case M_LDL_AB:
11972 s = "ldl";
df58fc94 11973 fmt = MEM12_FMT;
7f3c4072 11974 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11975 goto ld_st;
252b5132
RH
11976 case M_LDR_AB:
11977 s = "ldr";
df58fc94 11978 fmt = MEM12_FMT;
7f3c4072 11979 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11980 goto ld_st;
252b5132
RH
11981 case M_LL_AB:
11982 s = "ll";
7361da2c
AB
11983 fmt = LL_SC_FMT;
11984 offbits = (mips_opts.micromips ? 12
11985 : ISA_IS_R6 (mips_opts.isa) ? 9
11986 : 16);
252b5132
RH
11987 goto ld;
11988 case M_LLD_AB:
11989 s = "lld";
7361da2c
AB
11990 fmt = LL_SC_FMT;
11991 offbits = (mips_opts.micromips ? 12
11992 : ISA_IS_R6 (mips_opts.isa) ? 9
11993 : 16);
252b5132
RH
11994 goto ld;
11995 case M_LWU_AB:
11996 s = "lwu";
df58fc94 11997 fmt = MEM12_FMT;
7f3c4072 11998 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
11999 goto ld;
12000 case M_LWP_AB:
df58fc94
RS
12001 gas_assert (mips_opts.micromips);
12002 s = "lwp";
12003 fmt = "t,~(b)";
7f3c4072 12004 offbits = 12;
df58fc94
RS
12005 lp = 1;
12006 goto ld;
12007 case M_LDP_AB:
df58fc94
RS
12008 gas_assert (mips_opts.micromips);
12009 s = "ldp";
12010 fmt = "t,~(b)";
7f3c4072 12011 offbits = 12;
df58fc94
RS
12012 lp = 1;
12013 goto ld;
12014 case M_LWM_AB:
df58fc94
RS
12015 gas_assert (mips_opts.micromips);
12016 s = "lwm";
12017 fmt = "n,~(b)";
7f3c4072 12018 offbits = 12;
df58fc94
RS
12019 goto ld_st;
12020 case M_LDM_AB:
df58fc94
RS
12021 gas_assert (mips_opts.micromips);
12022 s = "ldm";
12023 fmt = "n,~(b)";
7f3c4072 12024 offbits = 12;
df58fc94
RS
12025 goto ld_st;
12026
252b5132 12027 ld:
f19ccbda 12028 /* We don't want to use $0 as tempreg. */
c0ebe874 12029 if (op[2] == op[0] + lp || op[0] + lp == ZERO)
df58fc94 12030 goto ld_st;
252b5132 12031 else
c0ebe874 12032 tempreg = op[0] + lp;
df58fc94
RS
12033 goto ld_noat;
12034
252b5132
RH
12035 case M_SB_AB:
12036 s = "sb";
df58fc94
RS
12037 fmt = "t,o(b)";
12038 goto ld_st;
252b5132
RH
12039 case M_SH_AB:
12040 s = "sh";
df58fc94
RS
12041 fmt = "t,o(b)";
12042 goto ld_st;
252b5132
RH
12043 case M_SW_AB:
12044 s = "sw";
df58fc94
RS
12045 fmt = "t,o(b)";
12046 goto ld_st;
252b5132 12047 case M_SWC0_AB:
df58fc94 12048 gas_assert (!mips_opts.micromips);
252b5132 12049 s = "swc0";
df58fc94 12050 fmt = "E,o(b)";
bdaaa2e1 12051 /* Itbl support may require additional care here. */
252b5132 12052 coproc = 1;
df58fc94 12053 goto ld_st;
252b5132
RH
12054 case M_SWC1_AB:
12055 s = "swc1";
df58fc94 12056 fmt = "T,o(b)";
bdaaa2e1 12057 /* Itbl support may require additional care here. */
252b5132 12058 coproc = 1;
df58fc94 12059 goto ld_st;
252b5132
RH
12060 case M_SWC2_AB:
12061 s = "swc2";
df58fc94 12062 fmt = COP12_FMT;
7361da2c
AB
12063 offbits = (mips_opts.micromips ? 12
12064 : ISA_IS_R6 (mips_opts.isa) ? 11
12065 : 16);
bdaaa2e1 12066 /* Itbl support may require additional care here. */
252b5132 12067 coproc = 1;
df58fc94 12068 goto ld_st;
252b5132 12069 case M_SWC3_AB:
df58fc94 12070 gas_assert (!mips_opts.micromips);
252b5132 12071 s = "swc3";
df58fc94 12072 fmt = "E,o(b)";
bdaaa2e1 12073 /* Itbl support may require additional care here. */
252b5132 12074 coproc = 1;
df58fc94 12075 goto ld_st;
252b5132
RH
12076 case M_SWL_AB:
12077 s = "swl";
df58fc94 12078 fmt = MEM12_FMT;
7f3c4072 12079 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 12080 goto ld_st;
252b5132
RH
12081 case M_SWR_AB:
12082 s = "swr";
df58fc94 12083 fmt = MEM12_FMT;
7f3c4072 12084 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 12085 goto ld_st;
252b5132
RH
12086 case M_SC_AB:
12087 s = "sc";
7361da2c
AB
12088 fmt = LL_SC_FMT;
12089 offbits = (mips_opts.micromips ? 12
12090 : ISA_IS_R6 (mips_opts.isa) ? 9
12091 : 16);
df58fc94 12092 goto ld_st;
252b5132
RH
12093 case M_SCD_AB:
12094 s = "scd";
7361da2c
AB
12095 fmt = LL_SC_FMT;
12096 offbits = (mips_opts.micromips ? 12
12097 : ISA_IS_R6 (mips_opts.isa) ? 9
12098 : 16);
df58fc94 12099 goto ld_st;
d43b4baf
TS
12100 case M_CACHE_AB:
12101 s = "cache";
7361da2c
AB
12102 fmt = (mips_opts.micromips ? "k,~(b)"
12103 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
12104 : "k,o(b)");
12105 offbits = (mips_opts.micromips ? 12
12106 : ISA_IS_R6 (mips_opts.isa) ? 9
12107 : 16);
7f3c4072
CM
12108 goto ld_st;
12109 case M_CACHEE_AB:
7f3c4072
CM
12110 s = "cachee";
12111 fmt = "k,+j(b)";
12112 offbits = 9;
df58fc94 12113 goto ld_st;
3eebd5eb
MR
12114 case M_PREF_AB:
12115 s = "pref";
7361da2c
AB
12116 fmt = (mips_opts.micromips ? "k,~(b)"
12117 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
12118 : "k,o(b)");
12119 offbits = (mips_opts.micromips ? 12
12120 : ISA_IS_R6 (mips_opts.isa) ? 9
12121 : 16);
7f3c4072
CM
12122 goto ld_st;
12123 case M_PREFE_AB:
7f3c4072
CM
12124 s = "prefe";
12125 fmt = "k,+j(b)";
12126 offbits = 9;
df58fc94 12127 goto ld_st;
252b5132 12128 case M_SDC1_AB:
252b5132 12129 s = "sdc1";
df58fc94 12130 fmt = "T,o(b)";
252b5132 12131 coproc = 1;
bdaaa2e1 12132 /* Itbl support may require additional care here. */
df58fc94 12133 goto ld_st;
252b5132
RH
12134 case M_SDC2_AB:
12135 s = "sdc2";
df58fc94 12136 fmt = COP12_FMT;
7361da2c
AB
12137 offbits = (mips_opts.micromips ? 12
12138 : ISA_IS_R6 (mips_opts.isa) ? 11
12139 : 16);
c77c0862
RS
12140 /* Itbl support may require additional care here. */
12141 coproc = 1;
12142 goto ld_st;
12143 case M_SQC2_AB:
c77c0862 12144 s = "sqc2";
14daeee3 12145 fmt = "+7,o(b)";
bdaaa2e1 12146 /* Itbl support may require additional care here. */
252b5132 12147 coproc = 1;
df58fc94 12148 goto ld_st;
252b5132 12149 case M_SDC3_AB:
df58fc94 12150 gas_assert (!mips_opts.micromips);
252b5132 12151 s = "sdc3";
df58fc94 12152 fmt = "E,o(b)";
bdaaa2e1 12153 /* Itbl support may require additional care here. */
252b5132 12154 coproc = 1;
df58fc94 12155 goto ld_st;
252b5132
RH
12156 case M_SDL_AB:
12157 s = "sdl";
df58fc94 12158 fmt = MEM12_FMT;
7f3c4072 12159 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 12160 goto ld_st;
252b5132
RH
12161 case M_SDR_AB:
12162 s = "sdr";
df58fc94 12163 fmt = MEM12_FMT;
7f3c4072 12164 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
12165 goto ld_st;
12166 case M_SWP_AB:
df58fc94
RS
12167 gas_assert (mips_opts.micromips);
12168 s = "swp";
12169 fmt = "t,~(b)";
7f3c4072 12170 offbits = 12;
df58fc94
RS
12171 goto ld_st;
12172 case M_SDP_AB:
df58fc94
RS
12173 gas_assert (mips_opts.micromips);
12174 s = "sdp";
12175 fmt = "t,~(b)";
7f3c4072 12176 offbits = 12;
df58fc94
RS
12177 goto ld_st;
12178 case M_SWM_AB:
df58fc94
RS
12179 gas_assert (mips_opts.micromips);
12180 s = "swm";
12181 fmt = "n,~(b)";
7f3c4072 12182 offbits = 12;
df58fc94
RS
12183 goto ld_st;
12184 case M_SDM_AB:
df58fc94
RS
12185 gas_assert (mips_opts.micromips);
12186 s = "sdm";
12187 fmt = "n,~(b)";
7f3c4072 12188 offbits = 12;
df58fc94
RS
12189
12190 ld_st:
8fc2e39e 12191 tempreg = AT;
df58fc94 12192 ld_noat:
c0ebe874 12193 breg = op[2];
f2ae14a1
RS
12194 if (small_offset_p (0, align, 16))
12195 {
12196 /* The first case exists for M_LD_AB and M_SD_AB, which are
12197 macros for o32 but which should act like normal instructions
12198 otherwise. */
12199 if (offbits == 16)
c0ebe874 12200 macro_build (&offset_expr, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
12201 offset_reloc[1], offset_reloc[2], breg);
12202 else if (small_offset_p (0, align, offbits))
12203 {
12204 if (offbits == 0)
c0ebe874 12205 macro_build (NULL, s, fmt, op[0], breg);
f2ae14a1 12206 else
c0ebe874 12207 macro_build (NULL, s, fmt, op[0],
c8276761 12208 (int) offset_expr.X_add_number, breg);
f2ae14a1
RS
12209 }
12210 else
12211 {
12212 if (tempreg == AT)
12213 used_at = 1;
12214 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
12215 tempreg, breg, -1, offset_reloc[0],
12216 offset_reloc[1], offset_reloc[2]);
12217 if (offbits == 0)
c0ebe874 12218 macro_build (NULL, s, fmt, op[0], tempreg);
f2ae14a1 12219 else
c0ebe874 12220 macro_build (NULL, s, fmt, op[0], 0, tempreg);
f2ae14a1
RS
12221 }
12222 break;
12223 }
12224
12225 if (tempreg == AT)
12226 used_at = 1;
12227
252b5132
RH
12228 if (offset_expr.X_op != O_constant
12229 && offset_expr.X_op != O_symbol)
12230 {
1661c76c 12231 as_bad (_("expression too complex"));
252b5132
RH
12232 offset_expr.X_op = O_constant;
12233 }
12234
2051e8c4
MR
12235 if (HAVE_32BIT_ADDRESSES
12236 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
12237 {
12238 char value [32];
12239
12240 sprintf_vma (value, offset_expr.X_add_number);
1661c76c 12241 as_bad (_("number (0x%s) larger than 32 bits"), value);
55e08f71 12242 }
2051e8c4 12243
252b5132
RH
12244 /* A constant expression in PIC code can be handled just as it
12245 is in non PIC code. */
aed1a261
RS
12246 if (offset_expr.X_op == O_constant)
12247 {
f2ae14a1
RS
12248 expr1.X_add_number = offset_high_part (offset_expr.X_add_number,
12249 offbits == 0 ? 16 : offbits);
12250 offset_expr.X_add_number -= expr1.X_add_number;
df58fc94 12251
f2ae14a1
RS
12252 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
12253 if (breg != 0)
12254 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12255 tempreg, tempreg, breg);
7f3c4072 12256 if (offbits == 0)
dd6a37e7 12257 {
f2ae14a1 12258 if (offset_expr.X_add_number != 0)
dd6a37e7 12259 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
f2ae14a1 12260 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
c0ebe874 12261 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 12262 }
7f3c4072 12263 else if (offbits == 16)
c0ebe874 12264 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
df58fc94 12265 else
c0ebe874 12266 macro_build (NULL, s, fmt, op[0],
c8276761 12267 (int) offset_expr.X_add_number, tempreg);
df58fc94 12268 }
7f3c4072 12269 else if (offbits != 16)
df58fc94 12270 {
7f3c4072 12271 /* The offset field is too narrow to be used for a low-part
2b0f3761 12272 relocation, so load the whole address into the auxiliary
f2ae14a1
RS
12273 register. */
12274 load_address (tempreg, &offset_expr, &used_at);
12275 if (breg != 0)
12276 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12277 tempreg, tempreg, breg);
7f3c4072 12278 if (offbits == 0)
c0ebe874 12279 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 12280 else
c0ebe874 12281 macro_build (NULL, s, fmt, op[0], 0, tempreg);
aed1a261
RS
12282 }
12283 else if (mips_pic == NO_PIC)
252b5132
RH
12284 {
12285 /* If this is a reference to a GP relative symbol, and there
12286 is no base register, we want
c0ebe874 12287 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
12288 Otherwise, if there is no base register, we want
12289 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
c0ebe874 12290 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
252b5132
RH
12291 If we have a constant, we need two instructions anyhow,
12292 so we always use the latter form.
12293
12294 If we have a base register, and this is a reference to a
12295 GP relative symbol, we want
12296 addu $tempreg,$breg,$gp
c0ebe874 12297 <op> op[0],<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
12298 Otherwise we want
12299 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
12300 addu $tempreg,$tempreg,$breg
c0ebe874 12301 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 12302 With a constant we always use the latter case.
76b3015f 12303
d6bc6245
TS
12304 With 64bit address space and no base register and $at usable,
12305 we want
12306 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12307 lui $at,<sym> (BFD_RELOC_HI16_S)
12308 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12309 dsll32 $tempreg,0
12310 daddu $tempreg,$at
c0ebe874 12311 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
12312 If we have a base register, we want
12313 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12314 lui $at,<sym> (BFD_RELOC_HI16_S)
12315 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12316 daddu $at,$breg
12317 dsll32 $tempreg,0
12318 daddu $tempreg,$at
c0ebe874 12319 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
12320
12321 Without $at we can't generate the optimal path for superscalar
12322 processors here since this would require two temporary registers.
12323 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12324 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12325 dsll $tempreg,16
12326 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
12327 dsll $tempreg,16
c0ebe874 12328 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
12329 If we have a base register, we want
12330 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12331 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12332 dsll $tempreg,16
12333 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
12334 dsll $tempreg,16
12335 daddu $tempreg,$tempreg,$breg
c0ebe874 12336 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 12337
6caf9ef4 12338 For GP relative symbols in 64bit address space we can use
aed1a261
RS
12339 the same sequence as in 32bit address space. */
12340 if (HAVE_64BIT_SYMBOLS)
d6bc6245 12341 {
aed1a261 12342 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
12343 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12344 {
12345 relax_start (offset_expr.X_add_symbol);
12346 if (breg == 0)
12347 {
c0ebe874 12348 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
12349 BFD_RELOC_GPREL16, mips_gp_register);
12350 }
12351 else
12352 {
12353 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12354 tempreg, breg, mips_gp_register);
c0ebe874 12355 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
12356 BFD_RELOC_GPREL16, tempreg);
12357 }
12358 relax_switch ();
12359 }
d6bc6245 12360
741fe287 12361 if (used_at == 0 && mips_opts.at)
d6bc6245 12362 {
df58fc94 12363 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb 12364 BFD_RELOC_MIPS_HIGHEST);
df58fc94 12365 macro_build (&offset_expr, "lui", LUI_FMT, AT,
67c0d1eb
RS
12366 BFD_RELOC_HI16_S);
12367 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12368 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 12369 if (breg != 0)
67c0d1eb 12370 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
df58fc94 12371 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 12372 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
c0ebe874 12373 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16,
67c0d1eb 12374 tempreg);
d6bc6245
TS
12375 used_at = 1;
12376 }
12377 else
12378 {
df58fc94 12379 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb
RS
12380 BFD_RELOC_MIPS_HIGHEST);
12381 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12382 tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 12383 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb
RS
12384 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12385 tempreg, BFD_RELOC_HI16_S);
df58fc94 12386 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
d6bc6245 12387 if (breg != 0)
67c0d1eb 12388 macro_build (NULL, "daddu", "d,v,t",
17a2f251 12389 tempreg, tempreg, breg);
c0ebe874 12390 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12391 BFD_RELOC_LO16, tempreg);
d6bc6245 12392 }
6caf9ef4
TS
12393
12394 if (mips_relax.sequence)
12395 relax_end ();
8fc2e39e 12396 break;
d6bc6245 12397 }
256ab948 12398
252b5132
RH
12399 if (breg == 0)
12400 {
67c0d1eb 12401 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12402 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12403 {
4d7206a2 12404 relax_start (offset_expr.X_add_symbol);
c0ebe874 12405 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_GPREL16,
67c0d1eb 12406 mips_gp_register);
4d7206a2 12407 relax_switch ();
252b5132 12408 }
67c0d1eb 12409 macro_build_lui (&offset_expr, tempreg);
c0ebe874 12410 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12411 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
12412 if (mips_relax.sequence)
12413 relax_end ();
252b5132
RH
12414 }
12415 else
12416 {
67c0d1eb 12417 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12418 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12419 {
4d7206a2 12420 relax_start (offset_expr.X_add_symbol);
67c0d1eb 12421 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12422 tempreg, breg, mips_gp_register);
c0ebe874 12423 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12424 BFD_RELOC_GPREL16, tempreg);
4d7206a2 12425 relax_switch ();
252b5132 12426 }
67c0d1eb
RS
12427 macro_build_lui (&offset_expr, tempreg);
12428 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12429 tempreg, tempreg, breg);
c0ebe874 12430 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12431 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
12432 if (mips_relax.sequence)
12433 relax_end ();
252b5132
RH
12434 }
12435 }
0a44bf69 12436 else if (!mips_big_got)
252b5132 12437 {
ed6fb7bd 12438 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 12439
252b5132
RH
12440 /* If this is a reference to an external symbol, we want
12441 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12442 nop
c0ebe874 12443 <op> op[0],0($tempreg)
252b5132
RH
12444 Otherwise we want
12445 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12446 nop
12447 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 12448 <op> op[0],0($tempreg)
f5040a92
AO
12449
12450 For NewABI, we want
12451 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 12452 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 12453
252b5132
RH
12454 If there is a base register, we add it to $tempreg before
12455 the <op>. If there is a constant, we stick it in the
12456 <op> instruction. We don't handle constants larger than
12457 16 bits, because we have no way to load the upper 16 bits
12458 (actually, we could handle them for the subset of cases
12459 in which we are not using $at). */
9c2799c2 12460 gas_assert (offset_expr.X_op == O_symbol);
f5040a92
AO
12461 if (HAVE_NEWABI)
12462 {
67c0d1eb
RS
12463 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12464 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 12465 if (breg != 0)
67c0d1eb 12466 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12467 tempreg, tempreg, breg);
c0ebe874 12468 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12469 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
12470 break;
12471 }
252b5132
RH
12472 expr1.X_add_number = offset_expr.X_add_number;
12473 offset_expr.X_add_number = 0;
12474 if (expr1.X_add_number < -0x8000
12475 || expr1.X_add_number >= 0x8000)
12476 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
12477 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12478 lw_reloc_type, mips_gp_register);
269137b2 12479 load_delay_nop ();
4d7206a2
RS
12480 relax_start (offset_expr.X_add_symbol);
12481 relax_switch ();
67c0d1eb
RS
12482 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12483 tempreg, BFD_RELOC_LO16);
4d7206a2 12484 relax_end ();
252b5132 12485 if (breg != 0)
67c0d1eb 12486 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12487 tempreg, tempreg, breg);
c0ebe874 12488 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 12489 }
0a44bf69 12490 else if (mips_big_got && !HAVE_NEWABI)
252b5132 12491 {
67c0d1eb 12492 int gpdelay;
252b5132
RH
12493
12494 /* If this is a reference to an external symbol, we want
12495 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12496 addu $tempreg,$tempreg,$gp
12497 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 12498 <op> op[0],0($tempreg)
252b5132
RH
12499 Otherwise we want
12500 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12501 nop
12502 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 12503 <op> op[0],0($tempreg)
252b5132
RH
12504 If there is a base register, we add it to $tempreg before
12505 the <op>. If there is a constant, we stick it in the
12506 <op> instruction. We don't handle constants larger than
12507 16 bits, because we have no way to load the upper 16 bits
12508 (actually, we could handle them for the subset of cases
f5040a92 12509 in which we are not using $at). */
9c2799c2 12510 gas_assert (offset_expr.X_op == O_symbol);
252b5132
RH
12511 expr1.X_add_number = offset_expr.X_add_number;
12512 offset_expr.X_add_number = 0;
12513 if (expr1.X_add_number < -0x8000
12514 || expr1.X_add_number >= 0x8000)
12515 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 12516 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 12517 relax_start (offset_expr.X_add_symbol);
df58fc94 12518 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 12519 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
12520 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12521 mips_gp_register);
12522 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12523 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 12524 relax_switch ();
67c0d1eb
RS
12525 if (gpdelay)
12526 macro_build (NULL, "nop", "");
12527 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12528 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 12529 load_delay_nop ();
67c0d1eb
RS
12530 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12531 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
12532 relax_end ();
12533
252b5132 12534 if (breg != 0)
67c0d1eb 12535 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12536 tempreg, tempreg, breg);
c0ebe874 12537 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 12538 }
0a44bf69 12539 else if (mips_big_got && HAVE_NEWABI)
f5040a92 12540 {
f5040a92
AO
12541 /* If this is a reference to an external symbol, we want
12542 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12543 add $tempreg,$tempreg,$gp
12544 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 12545 <op> op[0],<ofst>($tempreg)
f5040a92
AO
12546 Otherwise, for local symbols, we want:
12547 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 12548 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
9c2799c2 12549 gas_assert (offset_expr.X_op == O_symbol);
4d7206a2 12550 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
12551 offset_expr.X_add_number = 0;
12552 if (expr1.X_add_number < -0x8000
12553 || expr1.X_add_number >= 0x8000)
12554 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 12555 relax_start (offset_expr.X_add_symbol);
df58fc94 12556 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 12557 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
12558 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12559 mips_gp_register);
12560 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12561 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 12562 if (breg != 0)
67c0d1eb 12563 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12564 tempreg, tempreg, breg);
c0ebe874 12565 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
684022ea 12566
4d7206a2 12567 relax_switch ();
f5040a92 12568 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
12569 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12570 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 12571 if (breg != 0)
67c0d1eb 12572 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12573 tempreg, tempreg, breg);
c0ebe874 12574 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12575 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 12576 relax_end ();
f5040a92 12577 }
252b5132
RH
12578 else
12579 abort ();
12580
252b5132
RH
12581 break;
12582
833794fc
MR
12583 case M_JRADDIUSP:
12584 gas_assert (mips_opts.micromips);
12585 gas_assert (mips_opts.insn32);
12586 start_noreorder ();
12587 macro_build (NULL, "jr", "s", RA);
c0ebe874 12588 expr1.X_add_number = op[0] << 2;
833794fc
MR
12589 macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
12590 end_noreorder ();
12591 break;
12592
12593 case M_JRC:
12594 gas_assert (mips_opts.micromips);
12595 gas_assert (mips_opts.insn32);
c0ebe874 12596 macro_build (NULL, "jr", "s", op[0]);
833794fc
MR
12597 if (mips_opts.noreorder)
12598 macro_build (NULL, "nop", "");
12599 break;
12600
252b5132
RH
12601 case M_LI:
12602 case M_LI_S:
c0ebe874 12603 load_register (op[0], &imm_expr, 0);
8fc2e39e 12604 break;
252b5132
RH
12605
12606 case M_DLI:
c0ebe874 12607 load_register (op[0], &imm_expr, 1);
8fc2e39e 12608 break;
252b5132
RH
12609
12610 case M_LI_SS:
12611 if (imm_expr.X_op == O_constant)
12612 {
8fc2e39e 12613 used_at = 1;
67c0d1eb 12614 load_register (AT, &imm_expr, 0);
c0ebe874 12615 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
12616 break;
12617 }
12618 else
12619 {
b0e6f033
RS
12620 gas_assert (imm_expr.X_op == O_absent
12621 && offset_expr.X_op == O_symbol
90ecf173
MR
12622 && strcmp (segment_name (S_GET_SEGMENT
12623 (offset_expr.X_add_symbol)),
12624 ".lit4") == 0
12625 && offset_expr.X_add_number == 0);
c0ebe874 12626 macro_build (&offset_expr, "lwc1", "T,o(b)", op[0],
17a2f251 12627 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 12628 break;
252b5132
RH
12629 }
12630
12631 case M_LI_D:
ca4e0257
RS
12632 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
12633 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
12634 order 32 bits of the value and the low order 32 bits are either
12635 zero or in OFFSET_EXPR. */
b0e6f033 12636 if (imm_expr.X_op == O_constant)
252b5132 12637 {
bad1aba3 12638 if (GPR_SIZE == 64)
c0ebe874 12639 load_register (op[0], &imm_expr, 1);
252b5132
RH
12640 else
12641 {
12642 int hreg, lreg;
12643
12644 if (target_big_endian)
12645 {
c0ebe874
RS
12646 hreg = op[0];
12647 lreg = op[0] + 1;
252b5132
RH
12648 }
12649 else
12650 {
c0ebe874
RS
12651 hreg = op[0] + 1;
12652 lreg = op[0];
252b5132
RH
12653 }
12654
12655 if (hreg <= 31)
67c0d1eb 12656 load_register (hreg, &imm_expr, 0);
252b5132
RH
12657 if (lreg <= 31)
12658 {
12659 if (offset_expr.X_op == O_absent)
67c0d1eb 12660 move_register (lreg, 0);
252b5132
RH
12661 else
12662 {
9c2799c2 12663 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 12664 load_register (lreg, &offset_expr, 0);
252b5132
RH
12665 }
12666 }
12667 }
8fc2e39e 12668 break;
252b5132 12669 }
b0e6f033 12670 gas_assert (imm_expr.X_op == O_absent);
252b5132
RH
12671
12672 /* We know that sym is in the .rdata section. First we get the
12673 upper 16 bits of the address. */
12674 if (mips_pic == NO_PIC)
12675 {
67c0d1eb 12676 macro_build_lui (&offset_expr, AT);
8fc2e39e 12677 used_at = 1;
252b5132 12678 }
0a44bf69 12679 else
252b5132 12680 {
67c0d1eb
RS
12681 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12682 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 12683 used_at = 1;
252b5132 12684 }
bdaaa2e1 12685
252b5132 12686 /* Now we load the register(s). */
bad1aba3 12687 if (GPR_SIZE == 64)
8fc2e39e
TS
12688 {
12689 used_at = 1;
c0ebe874
RS
12690 macro_build (&offset_expr, "ld", "t,o(b)", op[0],
12691 BFD_RELOC_LO16, AT);
8fc2e39e 12692 }
252b5132
RH
12693 else
12694 {
8fc2e39e 12695 used_at = 1;
c0ebe874
RS
12696 macro_build (&offset_expr, "lw", "t,o(b)", op[0],
12697 BFD_RELOC_LO16, AT);
12698 if (op[0] != RA)
252b5132
RH
12699 {
12700 /* FIXME: How in the world do we deal with the possible
12701 overflow here? */
12702 offset_expr.X_add_number += 4;
67c0d1eb 12703 macro_build (&offset_expr, "lw", "t,o(b)",
c0ebe874 12704 op[0] + 1, BFD_RELOC_LO16, AT);
252b5132
RH
12705 }
12706 }
252b5132
RH
12707 break;
12708
12709 case M_LI_DD:
ca4e0257
RS
12710 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
12711 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
12712 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
12713 the value and the low order 32 bits are either zero or in
12714 OFFSET_EXPR. */
b0e6f033 12715 if (imm_expr.X_op == O_constant)
252b5132 12716 {
8fc2e39e 12717 used_at = 1;
bad1aba3 12718 load_register (AT, &imm_expr, FPR_SIZE == 64);
351cdf24
MF
12719 if (FPR_SIZE == 64 && GPR_SIZE == 64)
12720 macro_build (NULL, "dmtc1", "t,S", AT, op[0]);
252b5132
RH
12721 else
12722 {
351cdf24
MF
12723 if (ISA_HAS_MXHC1 (mips_opts.isa))
12724 macro_build (NULL, "mthc1", "t,G", AT, op[0]);
12725 else if (FPR_SIZE != 32)
12726 as_bad (_("Unable to generate `%s' compliant code "
12727 "without mthc1"),
12728 (FPR_SIZE == 64) ? "fp64" : "fpxx");
12729 else
12730 macro_build (NULL, "mtc1", "t,G", AT, op[0] + 1);
252b5132 12731 if (offset_expr.X_op == O_absent)
c0ebe874 12732 macro_build (NULL, "mtc1", "t,G", 0, op[0]);
252b5132
RH
12733 else
12734 {
9c2799c2 12735 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 12736 load_register (AT, &offset_expr, 0);
c0ebe874 12737 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
12738 }
12739 }
12740 break;
12741 }
12742
b0e6f033
RS
12743 gas_assert (imm_expr.X_op == O_absent
12744 && offset_expr.X_op == O_symbol
90ecf173 12745 && offset_expr.X_add_number == 0);
252b5132
RH
12746 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
12747 if (strcmp (s, ".lit8") == 0)
134c0c8b
MR
12748 {
12749 op[2] = mips_gp_register;
f2ae14a1
RS
12750 offset_reloc[0] = BFD_RELOC_MIPS_LITERAL;
12751 offset_reloc[1] = BFD_RELOC_UNUSED;
12752 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
12753 }
12754 else
12755 {
9c2799c2 12756 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 12757 used_at = 1;
0a44bf69 12758 if (mips_pic != NO_PIC)
67c0d1eb
RS
12759 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12760 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
12761 else
12762 {
12763 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 12764 macro_build_lui (&offset_expr, AT);
252b5132 12765 }
bdaaa2e1 12766
c0ebe874 12767 op[2] = AT;
f2ae14a1
RS
12768 offset_reloc[0] = BFD_RELOC_LO16;
12769 offset_reloc[1] = BFD_RELOC_UNUSED;
12770 offset_reloc[2] = BFD_RELOC_UNUSED;
134c0c8b 12771 }
f2ae14a1 12772 align = 8;
6f2117ba 12773 /* Fall through. */
c4a68bea 12774
252b5132 12775 case M_L_DAB:
6f2117ba
PH
12776 /* The MIPS assembler seems to check for X_add_number not
12777 being double aligned and generating:
12778 lui at,%hi(foo+1)
12779 addu at,at,v1
12780 addiu at,at,%lo(foo+1)
12781 lwc1 f2,0(at)
12782 lwc1 f3,4(at)
12783 But, the resulting address is the same after relocation so why
12784 generate the extra instruction? */
bdaaa2e1 12785 /* Itbl support may require additional care here. */
252b5132 12786 coproc = 1;
df58fc94 12787 fmt = "T,o(b)";
0aa27725 12788 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
12789 {
12790 s = "ldc1";
df58fc94 12791 goto ld_st;
252b5132 12792 }
252b5132 12793 s = "lwc1";
252b5132
RH
12794 goto ldd_std;
12795
12796 case M_S_DAB:
df58fc94
RS
12797 gas_assert (!mips_opts.micromips);
12798 /* Itbl support may require additional care here. */
12799 coproc = 1;
12800 fmt = "T,o(b)";
0aa27725 12801 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
12802 {
12803 s = "sdc1";
df58fc94 12804 goto ld_st;
252b5132 12805 }
252b5132 12806 s = "swc1";
252b5132
RH
12807 goto ldd_std;
12808
e407c74b
NC
12809 case M_LQ_AB:
12810 fmt = "t,o(b)";
12811 s = "lq";
12812 goto ld;
12813
12814 case M_SQ_AB:
12815 fmt = "t,o(b)";
12816 s = "sq";
12817 goto ld_st;
12818
252b5132 12819 case M_LD_AB:
df58fc94 12820 fmt = "t,o(b)";
bad1aba3 12821 if (GPR_SIZE == 64)
252b5132
RH
12822 {
12823 s = "ld";
12824 goto ld;
12825 }
252b5132 12826 s = "lw";
252b5132
RH
12827 goto ldd_std;
12828
12829 case M_SD_AB:
df58fc94 12830 fmt = "t,o(b)";
bad1aba3 12831 if (GPR_SIZE == 64)
252b5132
RH
12832 {
12833 s = "sd";
df58fc94 12834 goto ld_st;
252b5132 12835 }
252b5132 12836 s = "sw";
252b5132
RH
12837
12838 ldd_std:
f2ae14a1
RS
12839 /* Even on a big endian machine $fn comes before $fn+1. We have
12840 to adjust when loading from memory. We set coproc if we must
12841 load $fn+1 first. */
12842 /* Itbl support may require additional care here. */
12843 if (!target_big_endian)
12844 coproc = 0;
12845
c0ebe874 12846 breg = op[2];
f2ae14a1
RS
12847 if (small_offset_p (0, align, 16))
12848 {
12849 ep = &offset_expr;
12850 if (!small_offset_p (4, align, 16))
12851 {
12852 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT, breg,
12853 -1, offset_reloc[0], offset_reloc[1],
12854 offset_reloc[2]);
12855 expr1.X_add_number = 0;
12856 ep = &expr1;
12857 breg = AT;
12858 used_at = 1;
12859 offset_reloc[0] = BFD_RELOC_LO16;
12860 offset_reloc[1] = BFD_RELOC_UNUSED;
12861 offset_reloc[2] = BFD_RELOC_UNUSED;
12862 }
c0ebe874 12863 if (strcmp (s, "lw") == 0 && op[0] == breg)
f2ae14a1
RS
12864 {
12865 ep->X_add_number += 4;
c0ebe874 12866 macro_build (ep, s, fmt, op[0] + 1, -1, offset_reloc[0],
f2ae14a1
RS
12867 offset_reloc[1], offset_reloc[2], breg);
12868 ep->X_add_number -= 4;
c0ebe874 12869 macro_build (ep, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
12870 offset_reloc[1], offset_reloc[2], breg);
12871 }
12872 else
12873 {
c0ebe874 12874 macro_build (ep, s, fmt, coproc ? op[0] + 1 : op[0], -1,
f2ae14a1
RS
12875 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12876 breg);
12877 ep->X_add_number += 4;
c0ebe874 12878 macro_build (ep, s, fmt, coproc ? op[0] : op[0] + 1, -1,
f2ae14a1
RS
12879 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12880 breg);
12881 }
12882 break;
12883 }
12884
252b5132
RH
12885 if (offset_expr.X_op != O_symbol
12886 && offset_expr.X_op != O_constant)
12887 {
1661c76c 12888 as_bad (_("expression too complex"));
252b5132
RH
12889 offset_expr.X_op = O_constant;
12890 }
12891
2051e8c4
MR
12892 if (HAVE_32BIT_ADDRESSES
12893 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
12894 {
12895 char value [32];
12896
12897 sprintf_vma (value, offset_expr.X_add_number);
1661c76c 12898 as_bad (_("number (0x%s) larger than 32 bits"), value);
55e08f71 12899 }
2051e8c4 12900
90ecf173 12901 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
252b5132
RH
12902 {
12903 /* If this is a reference to a GP relative symbol, we want
c0ebe874
RS
12904 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
12905 <op> op[0]+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
12906 If we have a base register, we use this
12907 addu $at,$breg,$gp
c0ebe874
RS
12908 <op> op[0],<sym>($at) (BFD_RELOC_GPREL16)
12909 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
12910 If this is not a GP relative symbol, we want
12911 lui $at,<sym> (BFD_RELOC_HI16_S)
c0ebe874
RS
12912 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12913 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
12914 If there is a base register, we add it to $at after the
12915 lui instruction. If there is a constant, we always use
12916 the last case. */
39a59cf8
MR
12917 if (offset_expr.X_op == O_symbol
12918 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12919 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12920 {
4d7206a2 12921 relax_start (offset_expr.X_add_symbol);
252b5132
RH
12922 if (breg == 0)
12923 {
c9914766 12924 tempreg = mips_gp_register;
252b5132
RH
12925 }
12926 else
12927 {
67c0d1eb 12928 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12929 AT, breg, mips_gp_register);
252b5132 12930 tempreg = AT;
252b5132
RH
12931 used_at = 1;
12932 }
12933
beae10d5 12934 /* Itbl support may require additional care here. */
c0ebe874 12935 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 12936 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
12937 offset_expr.X_add_number += 4;
12938
12939 /* Set mips_optimize to 2 to avoid inserting an
12940 undesired nop. */
12941 hold_mips_optimize = mips_optimize;
12942 mips_optimize = 2;
beae10d5 12943 /* Itbl support may require additional care here. */
c0ebe874 12944 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 12945 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
12946 mips_optimize = hold_mips_optimize;
12947
4d7206a2 12948 relax_switch ();
252b5132 12949
0970e49e 12950 offset_expr.X_add_number -= 4;
252b5132 12951 }
8fc2e39e 12952 used_at = 1;
f2ae14a1
RS
12953 if (offset_high_part (offset_expr.X_add_number, 16)
12954 != offset_high_part (offset_expr.X_add_number + 4, 16))
12955 {
12956 load_address (AT, &offset_expr, &used_at);
12957 offset_expr.X_op = O_constant;
12958 offset_expr.X_add_number = 0;
12959 }
12960 else
12961 macro_build_lui (&offset_expr, AT);
252b5132 12962 if (breg != 0)
67c0d1eb 12963 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 12964 /* Itbl support may require additional care here. */
c0ebe874 12965 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 12966 BFD_RELOC_LO16, AT);
252b5132
RH
12967 /* FIXME: How do we handle overflow here? */
12968 offset_expr.X_add_number += 4;
beae10d5 12969 /* Itbl support may require additional care here. */
c0ebe874 12970 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 12971 BFD_RELOC_LO16, AT);
4d7206a2
RS
12972 if (mips_relax.sequence)
12973 relax_end ();
bdaaa2e1 12974 }
0a44bf69 12975 else if (!mips_big_got)
252b5132 12976 {
252b5132
RH
12977 /* If this is a reference to an external symbol, we want
12978 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12979 nop
c0ebe874
RS
12980 <op> op[0],0($at)
12981 <op> op[0]+1,4($at)
252b5132
RH
12982 Otherwise we want
12983 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12984 nop
c0ebe874
RS
12985 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12986 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
12987 If there is a base register we add it to $at before the
12988 lwc1 instructions. If there is a constant we include it
12989 in the lwc1 instructions. */
12990 used_at = 1;
12991 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
12992 if (expr1.X_add_number < -0x8000
12993 || expr1.X_add_number >= 0x8000 - 4)
12994 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 12995 load_got_offset (AT, &offset_expr);
269137b2 12996 load_delay_nop ();
252b5132 12997 if (breg != 0)
67c0d1eb 12998 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
12999
13000 /* Set mips_optimize to 2 to avoid inserting an undesired
13001 nop. */
13002 hold_mips_optimize = mips_optimize;
13003 mips_optimize = 2;
4d7206a2 13004
beae10d5 13005 /* Itbl support may require additional care here. */
4d7206a2 13006 relax_start (offset_expr.X_add_symbol);
c0ebe874 13007 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 13008 BFD_RELOC_LO16, AT);
4d7206a2 13009 expr1.X_add_number += 4;
c0ebe874 13010 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 13011 BFD_RELOC_LO16, AT);
4d7206a2 13012 relax_switch ();
c0ebe874 13013 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 13014 BFD_RELOC_LO16, AT);
4d7206a2 13015 offset_expr.X_add_number += 4;
c0ebe874 13016 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 13017 BFD_RELOC_LO16, AT);
4d7206a2 13018 relax_end ();
252b5132 13019
4d7206a2 13020 mips_optimize = hold_mips_optimize;
252b5132 13021 }
0a44bf69 13022 else if (mips_big_got)
252b5132 13023 {
67c0d1eb 13024 int gpdelay;
252b5132
RH
13025
13026 /* If this is a reference to an external symbol, we want
13027 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
13028 addu $at,$at,$gp
13029 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
13030 nop
c0ebe874
RS
13031 <op> op[0],0($at)
13032 <op> op[0]+1,4($at)
252b5132
RH
13033 Otherwise we want
13034 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
13035 nop
c0ebe874
RS
13036 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
13037 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
13038 If there is a base register we add it to $at before the
13039 lwc1 instructions. If there is a constant we include it
13040 in the lwc1 instructions. */
13041 used_at = 1;
13042 expr1.X_add_number = offset_expr.X_add_number;
13043 offset_expr.X_add_number = 0;
13044 if (expr1.X_add_number < -0x8000
13045 || expr1.X_add_number >= 0x8000 - 4)
13046 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 13047 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 13048 relax_start (offset_expr.X_add_symbol);
df58fc94 13049 macro_build (&offset_expr, "lui", LUI_FMT,
67c0d1eb
RS
13050 AT, BFD_RELOC_MIPS_GOT_HI16);
13051 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 13052 AT, AT, mips_gp_register);
67c0d1eb 13053 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 13054 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 13055 load_delay_nop ();
252b5132 13056 if (breg != 0)
67c0d1eb 13057 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 13058 /* Itbl support may require additional care here. */
c0ebe874 13059 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 13060 BFD_RELOC_LO16, AT);
252b5132
RH
13061 expr1.X_add_number += 4;
13062
13063 /* Set mips_optimize to 2 to avoid inserting an undesired
13064 nop. */
13065 hold_mips_optimize = mips_optimize;
13066 mips_optimize = 2;
beae10d5 13067 /* Itbl support may require additional care here. */
c0ebe874 13068 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 13069 BFD_RELOC_LO16, AT);
252b5132
RH
13070 mips_optimize = hold_mips_optimize;
13071 expr1.X_add_number -= 4;
13072
4d7206a2
RS
13073 relax_switch ();
13074 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
13075 if (gpdelay)
13076 macro_build (NULL, "nop", "");
13077 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
13078 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 13079 load_delay_nop ();
252b5132 13080 if (breg != 0)
67c0d1eb 13081 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 13082 /* Itbl support may require additional care here. */
c0ebe874 13083 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 13084 BFD_RELOC_LO16, AT);
4d7206a2 13085 offset_expr.X_add_number += 4;
252b5132
RH
13086
13087 /* Set mips_optimize to 2 to avoid inserting an undesired
13088 nop. */
13089 hold_mips_optimize = mips_optimize;
13090 mips_optimize = 2;
beae10d5 13091 /* Itbl support may require additional care here. */
c0ebe874 13092 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 13093 BFD_RELOC_LO16, AT);
252b5132 13094 mips_optimize = hold_mips_optimize;
4d7206a2 13095 relax_end ();
252b5132 13096 }
252b5132
RH
13097 else
13098 abort ();
13099
252b5132 13100 break;
3739860c 13101
dd6a37e7 13102 case M_SAA_AB:
dd6a37e7 13103 s = "saa";
0db377d0 13104 goto saa_saad;
dd6a37e7 13105 case M_SAAD_AB:
dd6a37e7 13106 s = "saad";
0db377d0
MR
13107 saa_saad:
13108 gas_assert (!mips_opts.micromips);
7f3c4072 13109 offbits = 0;
dd6a37e7
AP
13110 fmt = "t,(b)";
13111 goto ld_st;
13112
252b5132
RH
13113 /* New code added to support COPZ instructions.
13114 This code builds table entries out of the macros in mip_opcodes.
13115 R4000 uses interlocks to handle coproc delays.
13116 Other chips (like the R3000) require nops to be inserted for delays.
13117
f72c8c98 13118 FIXME: Currently, we require that the user handle delays.
252b5132
RH
13119 In order to fill delay slots for non-interlocked chips,
13120 we must have a way to specify delays based on the coprocessor.
13121 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
13122 What are the side-effects of the cop instruction?
13123 What cache support might we have and what are its effects?
13124 Both coprocessor & memory require delays. how long???
bdaaa2e1 13125 What registers are read/set/modified?
252b5132
RH
13126
13127 If an itbl is provided to interpret cop instructions,
bdaaa2e1 13128 this knowledge can be encoded in the itbl spec. */
252b5132
RH
13129
13130 case M_COP0:
13131 s = "c0";
13132 goto copz;
13133 case M_COP1:
13134 s = "c1";
13135 goto copz;
13136 case M_COP2:
13137 s = "c2";
13138 goto copz;
13139 case M_COP3:
13140 s = "c3";
13141 copz:
df58fc94 13142 gas_assert (!mips_opts.micromips);
252b5132
RH
13143 /* For now we just do C (same as Cz). The parameter will be
13144 stored in insn_opcode by mips_ip. */
c8276761 13145 macro_build (NULL, s, "C", (int) ip->insn_opcode);
8fc2e39e 13146 break;
252b5132 13147
ea1fb5dc 13148 case M_MOVE:
c0ebe874 13149 move_register (op[0], op[1]);
8fc2e39e 13150 break;
ea1fb5dc 13151
833794fc
MR
13152 case M_MOVEP:
13153 gas_assert (mips_opts.micromips);
13154 gas_assert (mips_opts.insn32);
c0ebe874
RS
13155 move_register (micromips_to_32_reg_h_map1[op[0]],
13156 micromips_to_32_reg_m_map[op[1]]);
13157 move_register (micromips_to_32_reg_h_map2[op[0]],
13158 micromips_to_32_reg_n_map[op[2]]);
833794fc
MR
13159 break;
13160
252b5132
RH
13161 case M_DMUL:
13162 dbl = 1;
1a0670f3 13163 /* Fall through. */
252b5132 13164 case M_MUL:
e407c74b 13165 if (mips_opts.arch == CPU_R5900)
c0ebe874
RS
13166 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", op[0], op[1],
13167 op[2]);
e407c74b
NC
13168 else
13169 {
c0ebe874
RS
13170 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", op[1], op[2]);
13171 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
e407c74b 13172 }
8fc2e39e 13173 break;
252b5132
RH
13174
13175 case M_DMUL_I:
13176 dbl = 1;
1a0670f3 13177 /* Fall through. */
252b5132
RH
13178 case M_MUL_I:
13179 /* The MIPS assembler some times generates shifts and adds. I'm
13180 not trying to be that fancy. GCC should do this for us
13181 anyway. */
8fc2e39e 13182 used_at = 1;
67c0d1eb 13183 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
13184 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", op[1], AT);
13185 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
13186 break;
13187
13188 case M_DMULO_I:
13189 dbl = 1;
1a0670f3 13190 /* Fall through. */
252b5132
RH
13191 case M_MULO_I:
13192 imm = 1;
13193 goto do_mulo;
13194
13195 case M_DMULO:
13196 dbl = 1;
1a0670f3 13197 /* Fall through. */
252b5132
RH
13198 case M_MULO:
13199 do_mulo:
7d10b47d 13200 start_noreorder ();
8fc2e39e 13201 used_at = 1;
252b5132 13202 if (imm)
67c0d1eb 13203 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
13204 macro_build (NULL, dbl ? "dmult" : "mult", "s,t",
13205 op[1], imm ? AT : op[2]);
13206 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13207 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, op[0], op[0], 31);
df58fc94 13208 macro_build (NULL, "mfhi", MFHL_FMT, AT);
252b5132 13209 if (mips_trap)
c0ebe874 13210 macro_build (NULL, "tne", TRAP_FMT, op[0], AT, 6);
252b5132
RH
13211 else
13212 {
df58fc94
RS
13213 if (mips_opts.micromips)
13214 micromips_label_expr (&label_expr);
13215 else
13216 label_expr.X_add_number = 8;
c0ebe874 13217 macro_build (&label_expr, "beq", "s,t,p", op[0], AT);
a605d2b3 13218 macro_build (NULL, "nop", "");
df58fc94
RS
13219 macro_build (NULL, "break", BRK_FMT, 6);
13220 if (mips_opts.micromips)
13221 micromips_add_label ();
252b5132 13222 }
7d10b47d 13223 end_noreorder ();
c0ebe874 13224 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
13225 break;
13226
13227 case M_DMULOU_I:
13228 dbl = 1;
1a0670f3 13229 /* Fall through. */
252b5132
RH
13230 case M_MULOU_I:
13231 imm = 1;
13232 goto do_mulou;
13233
13234 case M_DMULOU:
13235 dbl = 1;
1a0670f3 13236 /* Fall through. */
252b5132
RH
13237 case M_MULOU:
13238 do_mulou:
7d10b47d 13239 start_noreorder ();
8fc2e39e 13240 used_at = 1;
252b5132 13241 if (imm)
67c0d1eb
RS
13242 load_register (AT, &imm_expr, dbl);
13243 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
c0ebe874 13244 op[1], imm ? AT : op[2]);
df58fc94 13245 macro_build (NULL, "mfhi", MFHL_FMT, AT);
c0ebe874 13246 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132 13247 if (mips_trap)
df58fc94 13248 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
252b5132
RH
13249 else
13250 {
df58fc94
RS
13251 if (mips_opts.micromips)
13252 micromips_label_expr (&label_expr);
13253 else
13254 label_expr.X_add_number = 8;
13255 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
a605d2b3 13256 macro_build (NULL, "nop", "");
df58fc94
RS
13257 macro_build (NULL, "break", BRK_FMT, 6);
13258 if (mips_opts.micromips)
13259 micromips_add_label ();
252b5132 13260 }
7d10b47d 13261 end_noreorder ();
252b5132
RH
13262 break;
13263
771c7ce4 13264 case M_DROL:
fef14a42 13265 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 13266 {
c0ebe874 13267 if (op[0] == op[1])
82dd0097
CD
13268 {
13269 tempreg = AT;
13270 used_at = 1;
13271 }
13272 else
c0ebe874
RS
13273 tempreg = op[0];
13274 macro_build (NULL, "dnegu", "d,w", tempreg, op[2]);
13275 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 13276 break;
82dd0097 13277 }
8fc2e39e 13278 used_at = 1;
c0ebe874
RS
13279 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13280 macro_build (NULL, "dsrlv", "d,t,s", AT, op[1], AT);
13281 macro_build (NULL, "dsllv", "d,t,s", op[0], op[1], op[2]);
13282 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13283 break;
13284
252b5132 13285 case M_ROL:
fef14a42 13286 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13287 {
c0ebe874 13288 if (op[0] == op[1])
82dd0097
CD
13289 {
13290 tempreg = AT;
13291 used_at = 1;
13292 }
13293 else
c0ebe874
RS
13294 tempreg = op[0];
13295 macro_build (NULL, "negu", "d,w", tempreg, op[2]);
13296 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 13297 break;
82dd0097 13298 }
8fc2e39e 13299 used_at = 1;
c0ebe874
RS
13300 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13301 macro_build (NULL, "srlv", "d,t,s", AT, op[1], AT);
13302 macro_build (NULL, "sllv", "d,t,s", op[0], op[1], op[2]);
13303 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13304 break;
13305
771c7ce4
TS
13306 case M_DROL_I:
13307 {
13308 unsigned int rot;
e0471c16
TS
13309 const char *l;
13310 const char *rr;
771c7ce4 13311
771c7ce4 13312 rot = imm_expr.X_add_number & 0x3f;
fef14a42 13313 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
13314 {
13315 rot = (64 - rot) & 0x3f;
13316 if (rot >= 32)
c0ebe874 13317 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
60b63b72 13318 else
c0ebe874 13319 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13320 break;
60b63b72 13321 }
483fc7cd 13322 if (rot == 0)
483fc7cd 13323 {
c0ebe874 13324 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13325 break;
483fc7cd 13326 }
82dd0097 13327 l = (rot < 0x20) ? "dsll" : "dsll32";
91d6fa6a 13328 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
82dd0097 13329 rot &= 0x1f;
8fc2e39e 13330 used_at = 1;
c0ebe874
RS
13331 macro_build (NULL, l, SHFT_FMT, AT, op[1], rot);
13332 macro_build (NULL, rr, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13333 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13334 }
13335 break;
13336
252b5132 13337 case M_ROL_I:
771c7ce4
TS
13338 {
13339 unsigned int rot;
13340
771c7ce4 13341 rot = imm_expr.X_add_number & 0x1f;
fef14a42 13342 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 13343 {
c0ebe874
RS
13344 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1],
13345 (32 - rot) & 0x1f);
8fc2e39e 13346 break;
60b63b72 13347 }
483fc7cd 13348 if (rot == 0)
483fc7cd 13349 {
c0ebe874 13350 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13351 break;
483fc7cd 13352 }
8fc2e39e 13353 used_at = 1;
c0ebe874
RS
13354 macro_build (NULL, "sll", SHFT_FMT, AT, op[1], rot);
13355 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13356 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13357 }
13358 break;
13359
13360 case M_DROR:
fef14a42 13361 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 13362 {
c0ebe874 13363 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 13364 break;
82dd0097 13365 }
8fc2e39e 13366 used_at = 1;
c0ebe874
RS
13367 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13368 macro_build (NULL, "dsllv", "d,t,s", AT, op[1], AT);
13369 macro_build (NULL, "dsrlv", "d,t,s", op[0], op[1], op[2]);
13370 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13371 break;
13372
13373 case M_ROR:
fef14a42 13374 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13375 {
c0ebe874 13376 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 13377 break;
82dd0097 13378 }
8fc2e39e 13379 used_at = 1;
c0ebe874
RS
13380 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13381 macro_build (NULL, "sllv", "d,t,s", AT, op[1], AT);
13382 macro_build (NULL, "srlv", "d,t,s", op[0], op[1], op[2]);
13383 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13384 break;
13385
771c7ce4
TS
13386 case M_DROR_I:
13387 {
13388 unsigned int rot;
e0471c16
TS
13389 const char *l;
13390 const char *rr;
771c7ce4 13391
771c7ce4 13392 rot = imm_expr.X_add_number & 0x3f;
fef14a42 13393 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
13394 {
13395 if (rot >= 32)
c0ebe874 13396 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
82dd0097 13397 else
c0ebe874 13398 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13399 break;
82dd0097 13400 }
483fc7cd 13401 if (rot == 0)
483fc7cd 13402 {
c0ebe874 13403 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13404 break;
483fc7cd 13405 }
91d6fa6a 13406 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
82dd0097
CD
13407 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
13408 rot &= 0x1f;
8fc2e39e 13409 used_at = 1;
c0ebe874
RS
13410 macro_build (NULL, rr, SHFT_FMT, AT, op[1], rot);
13411 macro_build (NULL, l, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13412 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13413 }
13414 break;
13415
252b5132 13416 case M_ROR_I:
771c7ce4
TS
13417 {
13418 unsigned int rot;
13419
771c7ce4 13420 rot = imm_expr.X_add_number & 0x1f;
fef14a42 13421 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13422 {
c0ebe874 13423 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13424 break;
82dd0097 13425 }
483fc7cd 13426 if (rot == 0)
483fc7cd 13427 {
c0ebe874 13428 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13429 break;
483fc7cd 13430 }
8fc2e39e 13431 used_at = 1;
c0ebe874
RS
13432 macro_build (NULL, "srl", SHFT_FMT, AT, op[1], rot);
13433 macro_build (NULL, "sll", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13434 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4 13435 }
252b5132
RH
13436 break;
13437
252b5132 13438 case M_SEQ:
c0ebe874
RS
13439 if (op[1] == 0)
13440 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[2], BFD_RELOC_LO16);
13441 else if (op[2] == 0)
13442 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
13443 else
13444 {
c0ebe874
RS
13445 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13446 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
252b5132 13447 }
8fc2e39e 13448 break;
252b5132
RH
13449
13450 case M_SEQ_I:
b0e6f033 13451 if (imm_expr.X_add_number == 0)
252b5132 13452 {
c0ebe874 13453 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 13454 break;
252b5132 13455 }
c0ebe874 13456 if (op[1] == 0)
252b5132 13457 {
1661c76c 13458 as_warn (_("instruction %s: result is always false"),
252b5132 13459 ip->insn_mo->name);
c0ebe874 13460 move_register (op[0], 0);
8fc2e39e 13461 break;
252b5132 13462 }
dd3cbb7e
NC
13463 if (CPU_HAS_SEQ (mips_opts.arch)
13464 && -512 <= imm_expr.X_add_number
13465 && imm_expr.X_add_number < 512)
13466 {
c0ebe874 13467 macro_build (NULL, "seqi", "t,r,+Q", op[0], op[1],
750bdd57 13468 (int) imm_expr.X_add_number);
dd3cbb7e
NC
13469 break;
13470 }
b0e6f033 13471 if (imm_expr.X_add_number >= 0
252b5132 13472 && imm_expr.X_add_number < 0x10000)
c0ebe874 13473 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1], BFD_RELOC_LO16);
b0e6f033 13474 else if (imm_expr.X_add_number > -0x8000
252b5132
RH
13475 && imm_expr.X_add_number < 0)
13476 {
13477 imm_expr.X_add_number = -imm_expr.X_add_number;
bad1aba3 13478 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
c0ebe874 13479 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 13480 }
dd3cbb7e
NC
13481 else if (CPU_HAS_SEQ (mips_opts.arch))
13482 {
13483 used_at = 1;
bad1aba3 13484 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13485 macro_build (NULL, "seq", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
13486 break;
13487 }
252b5132
RH
13488 else
13489 {
bad1aba3 13490 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13491 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
13492 used_at = 1;
13493 }
c0ebe874 13494 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13495 break;
252b5132 13496
c0ebe874 13497 case M_SGE: /* X >= Y <==> not (X < Y) */
252b5132
RH
13498 s = "slt";
13499 goto sge;
13500 case M_SGEU:
13501 s = "sltu";
13502 sge:
c0ebe874
RS
13503 macro_build (NULL, s, "d,v,t", op[0], op[1], op[2]);
13504 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13505 break;
252b5132 13506
6f2117ba 13507 case M_SGE_I: /* X >= I <==> not (X < I). */
252b5132 13508 case M_SGEU_I:
b0e6f033 13509 if (imm_expr.X_add_number >= -0x8000
252b5132 13510 && imm_expr.X_add_number < 0x8000)
c0ebe874
RS
13511 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
13512 op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
13513 else
13514 {
bad1aba3 13515 load_register (AT, &imm_expr, GPR_SIZE == 64);
67c0d1eb 13516 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
c0ebe874 13517 op[0], op[1], AT);
252b5132
RH
13518 used_at = 1;
13519 }
c0ebe874 13520 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13521 break;
252b5132 13522
6f2117ba 13523 case M_SGT: /* X > Y <==> Y < X. */
252b5132
RH
13524 s = "slt";
13525 goto sgt;
13526 case M_SGTU:
13527 s = "sltu";
13528 sgt:
c0ebe874 13529 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
8fc2e39e 13530 break;
252b5132 13531
6f2117ba 13532 case M_SGT_I: /* X > I <==> I < X. */
252b5132
RH
13533 s = "slt";
13534 goto sgti;
13535 case M_SGTU_I:
13536 s = "sltu";
13537 sgti:
8fc2e39e 13538 used_at = 1;
bad1aba3 13539 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13540 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
252b5132
RH
13541 break;
13542
6f2117ba 13543 case M_SLE: /* X <= Y <==> Y >= X <==> not (Y < X). */
252b5132
RH
13544 s = "slt";
13545 goto sle;
13546 case M_SLEU:
13547 s = "sltu";
13548 sle:
c0ebe874
RS
13549 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13550 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13551 break;
252b5132 13552
c0ebe874 13553 case M_SLE_I: /* X <= I <==> I >= X <==> not (I < X) */
252b5132
RH
13554 s = "slt";
13555 goto slei;
13556 case M_SLEU_I:
13557 s = "sltu";
13558 slei:
8fc2e39e 13559 used_at = 1;
bad1aba3 13560 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874
RS
13561 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13562 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
252b5132
RH
13563 break;
13564
13565 case M_SLT_I:
b0e6f033 13566 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
13567 && imm_expr.X_add_number < 0x8000)
13568 {
c0ebe874
RS
13569 macro_build (&imm_expr, "slti", "t,r,j", op[0], op[1],
13570 BFD_RELOC_LO16);
8fc2e39e 13571 break;
252b5132 13572 }
8fc2e39e 13573 used_at = 1;
bad1aba3 13574 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13575 macro_build (NULL, "slt", "d,v,t", op[0], op[1], AT);
252b5132
RH
13576 break;
13577
13578 case M_SLTU_I:
b0e6f033 13579 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
13580 && imm_expr.X_add_number < 0x8000)
13581 {
c0ebe874 13582 macro_build (&imm_expr, "sltiu", "t,r,j", op[0], op[1],
17a2f251 13583 BFD_RELOC_LO16);
8fc2e39e 13584 break;
252b5132 13585 }
8fc2e39e 13586 used_at = 1;
bad1aba3 13587 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13588 macro_build (NULL, "sltu", "d,v,t", op[0], op[1], AT);
252b5132
RH
13589 break;
13590
13591 case M_SNE:
c0ebe874
RS
13592 if (op[1] == 0)
13593 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[2]);
13594 else if (op[2] == 0)
13595 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
252b5132
RH
13596 else
13597 {
c0ebe874
RS
13598 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13599 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
252b5132 13600 }
8fc2e39e 13601 break;
252b5132
RH
13602
13603 case M_SNE_I:
b0e6f033 13604 if (imm_expr.X_add_number == 0)
252b5132 13605 {
c0ebe874 13606 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
8fc2e39e 13607 break;
252b5132 13608 }
c0ebe874 13609 if (op[1] == 0)
252b5132 13610 {
1661c76c 13611 as_warn (_("instruction %s: result is always true"),
252b5132 13612 ip->insn_mo->name);
bad1aba3 13613 macro_build (&expr1, GPR_SIZE == 32 ? "addiu" : "daddiu", "t,r,j",
c0ebe874 13614 op[0], 0, BFD_RELOC_LO16);
8fc2e39e 13615 break;
252b5132 13616 }
dd3cbb7e
NC
13617 if (CPU_HAS_SEQ (mips_opts.arch)
13618 && -512 <= imm_expr.X_add_number
13619 && imm_expr.X_add_number < 512)
13620 {
c0ebe874 13621 macro_build (NULL, "snei", "t,r,+Q", op[0], op[1],
750bdd57 13622 (int) imm_expr.X_add_number);
dd3cbb7e
NC
13623 break;
13624 }
b0e6f033 13625 if (imm_expr.X_add_number >= 0
252b5132
RH
13626 && imm_expr.X_add_number < 0x10000)
13627 {
c0ebe874
RS
13628 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1],
13629 BFD_RELOC_LO16);
252b5132 13630 }
b0e6f033 13631 else if (imm_expr.X_add_number > -0x8000
252b5132
RH
13632 && imm_expr.X_add_number < 0)
13633 {
13634 imm_expr.X_add_number = -imm_expr.X_add_number;
bad1aba3 13635 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
c0ebe874 13636 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 13637 }
dd3cbb7e
NC
13638 else if (CPU_HAS_SEQ (mips_opts.arch))
13639 {
13640 used_at = 1;
bad1aba3 13641 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13642 macro_build (NULL, "sne", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
13643 break;
13644 }
252b5132
RH
13645 else
13646 {
bad1aba3 13647 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13648 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
13649 used_at = 1;
13650 }
c0ebe874 13651 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
8fc2e39e 13652 break;
252b5132 13653
df58fc94
RS
13654 case M_SUB_I:
13655 s = "addi";
13656 s2 = "sub";
13657 goto do_subi;
13658 case M_SUBU_I:
13659 s = "addiu";
13660 s2 = "subu";
13661 goto do_subi;
252b5132
RH
13662 case M_DSUB_I:
13663 dbl = 1;
df58fc94
RS
13664 s = "daddi";
13665 s2 = "dsub";
13666 if (!mips_opts.micromips)
13667 goto do_subi;
b0e6f033 13668 if (imm_expr.X_add_number > -0x200
df58fc94 13669 && imm_expr.X_add_number <= 0x200)
252b5132 13670 {
b0e6f033
RS
13671 macro_build (NULL, s, "t,r,.", op[0], op[1],
13672 (int) -imm_expr.X_add_number);
8fc2e39e 13673 break;
252b5132 13674 }
df58fc94 13675 goto do_subi_i;
252b5132
RH
13676 case M_DSUBU_I:
13677 dbl = 1;
df58fc94
RS
13678 s = "daddiu";
13679 s2 = "dsubu";
13680 do_subi:
b0e6f033 13681 if (imm_expr.X_add_number > -0x8000
252b5132
RH
13682 && imm_expr.X_add_number <= 0x8000)
13683 {
13684 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 13685 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 13686 break;
252b5132 13687 }
df58fc94 13688 do_subi_i:
8fc2e39e 13689 used_at = 1;
67c0d1eb 13690 load_register (AT, &imm_expr, dbl);
c0ebe874 13691 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
13692 break;
13693
13694 case M_TEQ_I:
13695 s = "teq";
13696 goto trap;
13697 case M_TGE_I:
13698 s = "tge";
13699 goto trap;
13700 case M_TGEU_I:
13701 s = "tgeu";
13702 goto trap;
13703 case M_TLT_I:
13704 s = "tlt";
13705 goto trap;
13706 case M_TLTU_I:
13707 s = "tltu";
13708 goto trap;
13709 case M_TNE_I:
13710 s = "tne";
13711 trap:
8fc2e39e 13712 used_at = 1;
bad1aba3 13713 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13714 macro_build (NULL, s, "s,t", op[0], AT);
252b5132
RH
13715 break;
13716
252b5132 13717 case M_TRUNCWS:
43841e91 13718 case M_TRUNCWD:
df58fc94 13719 gas_assert (!mips_opts.micromips);
0aa27725 13720 gas_assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 13721 used_at = 1;
252b5132
RH
13722
13723 /*
13724 * Is the double cfc1 instruction a bug in the mips assembler;
13725 * or is there a reason for it?
13726 */
7d10b47d 13727 start_noreorder ();
c0ebe874
RS
13728 macro_build (NULL, "cfc1", "t,G", op[2], RA);
13729 macro_build (NULL, "cfc1", "t,G", op[2], RA);
67c0d1eb 13730 macro_build (NULL, "nop", "");
252b5132 13731 expr1.X_add_number = 3;
c0ebe874 13732 macro_build (&expr1, "ori", "t,r,i", AT, op[2], BFD_RELOC_LO16);
252b5132 13733 expr1.X_add_number = 2;
67c0d1eb
RS
13734 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
13735 macro_build (NULL, "ctc1", "t,G", AT, RA);
13736 macro_build (NULL, "nop", "");
13737 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
c0ebe874
RS
13738 op[0], op[1]);
13739 macro_build (NULL, "ctc1", "t,G", op[2], RA);
67c0d1eb 13740 macro_build (NULL, "nop", "");
7d10b47d 13741 end_noreorder ();
252b5132
RH
13742 break;
13743
f2ae14a1 13744 case M_ULH_AB:
252b5132 13745 s = "lb";
df58fc94
RS
13746 s2 = "lbu";
13747 off = 1;
13748 goto uld_st;
f2ae14a1 13749 case M_ULHU_AB:
252b5132 13750 s = "lbu";
df58fc94
RS
13751 s2 = "lbu";
13752 off = 1;
13753 goto uld_st;
f2ae14a1 13754 case M_ULW_AB:
df58fc94
RS
13755 s = "lwl";
13756 s2 = "lwr";
7f3c4072 13757 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
13758 off = 3;
13759 goto uld_st;
f2ae14a1 13760 case M_ULD_AB:
252b5132
RH
13761 s = "ldl";
13762 s2 = "ldr";
7f3c4072 13763 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 13764 off = 7;
df58fc94 13765 goto uld_st;
f2ae14a1 13766 case M_USH_AB:
df58fc94
RS
13767 s = "sb";
13768 s2 = "sb";
13769 off = 1;
13770 ust = 1;
13771 goto uld_st;
f2ae14a1 13772 case M_USW_AB:
df58fc94
RS
13773 s = "swl";
13774 s2 = "swr";
7f3c4072 13775 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 13776 off = 3;
df58fc94
RS
13777 ust = 1;
13778 goto uld_st;
f2ae14a1 13779 case M_USD_AB:
df58fc94
RS
13780 s = "sdl";
13781 s2 = "sdr";
7f3c4072 13782 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
13783 off = 7;
13784 ust = 1;
13785
13786 uld_st:
c0ebe874 13787 breg = op[2];
f2ae14a1 13788 large_offset = !small_offset_p (off, align, offbits);
df58fc94
RS
13789 ep = &offset_expr;
13790 expr1.X_add_number = 0;
f2ae14a1 13791 if (large_offset)
df58fc94
RS
13792 {
13793 used_at = 1;
13794 tempreg = AT;
f2ae14a1
RS
13795 if (small_offset_p (0, align, 16))
13796 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg, -1,
13797 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
13798 else
13799 {
13800 load_address (tempreg, ep, &used_at);
13801 if (breg != 0)
13802 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
13803 tempreg, tempreg, breg);
13804 }
13805 offset_reloc[0] = BFD_RELOC_LO16;
13806 offset_reloc[1] = BFD_RELOC_UNUSED;
13807 offset_reloc[2] = BFD_RELOC_UNUSED;
df58fc94 13808 breg = tempreg;
c0ebe874 13809 tempreg = op[0];
df58fc94
RS
13810 ep = &expr1;
13811 }
c0ebe874 13812 else if (!ust && op[0] == breg)
8fc2e39e
TS
13813 {
13814 used_at = 1;
13815 tempreg = AT;
13816 }
252b5132 13817 else
c0ebe874 13818 tempreg = op[0];
af22f5b2 13819
df58fc94
RS
13820 if (off == 1)
13821 goto ulh_sh;
252b5132 13822
90ecf173 13823 if (!target_big_endian)
df58fc94 13824 ep->X_add_number += off;
f2ae14a1 13825 if (offbits == 12)
c8276761 13826 macro_build (NULL, s, "t,~(b)", tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
13827 else
13828 macro_build (ep, s, "t,o(b)", tempreg, -1,
13829 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94 13830
90ecf173 13831 if (!target_big_endian)
df58fc94 13832 ep->X_add_number -= off;
252b5132 13833 else
df58fc94 13834 ep->X_add_number += off;
f2ae14a1 13835 if (offbits == 12)
df58fc94 13836 macro_build (NULL, s2, "t,~(b)",
c8276761 13837 tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
13838 else
13839 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13840 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 13841
df58fc94 13842 /* If necessary, move the result in tempreg to the final destination. */
c0ebe874 13843 if (!ust && op[0] != tempreg)
df58fc94
RS
13844 {
13845 /* Protect second load's delay slot. */
13846 load_delay_nop ();
c0ebe874 13847 move_register (op[0], tempreg);
df58fc94 13848 }
8fc2e39e 13849 break;
252b5132 13850
df58fc94 13851 ulh_sh:
d6bc6245 13852 used_at = 1;
df58fc94
RS
13853 if (target_big_endian == ust)
13854 ep->X_add_number += off;
c0ebe874 13855 tempreg = ust || large_offset ? op[0] : AT;
f2ae14a1
RS
13856 macro_build (ep, s, "t,o(b)", tempreg, -1,
13857 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94
RS
13858
13859 /* For halfword transfers we need a temporary register to shuffle
13860 bytes. Unfortunately for M_USH_A we have none available before
13861 the next store as AT holds the base address. We deal with this
13862 case by clobbering TREG and then restoring it as with ULH. */
c0ebe874 13863 tempreg = ust == large_offset ? op[0] : AT;
df58fc94 13864 if (ust)
c0ebe874 13865 macro_build (NULL, "srl", SHFT_FMT, tempreg, op[0], 8);
df58fc94
RS
13866
13867 if (target_big_endian == ust)
13868 ep->X_add_number -= off;
252b5132 13869 else
df58fc94 13870 ep->X_add_number += off;
f2ae14a1
RS
13871 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13872 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 13873
df58fc94 13874 /* For M_USH_A re-retrieve the LSB. */
f2ae14a1 13875 if (ust && large_offset)
df58fc94
RS
13876 {
13877 if (target_big_endian)
13878 ep->X_add_number += off;
13879 else
13880 ep->X_add_number -= off;
f2ae14a1
RS
13881 macro_build (&expr1, "lbu", "t,o(b)", AT, -1,
13882 offset_reloc[0], offset_reloc[1], offset_reloc[2], AT);
df58fc94
RS
13883 }
13884 /* For ULH and M_USH_A OR the LSB in. */
f2ae14a1 13885 if (!ust || large_offset)
df58fc94 13886 {
c0ebe874 13887 tempreg = !large_offset ? AT : op[0];
df58fc94 13888 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
c0ebe874 13889 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
df58fc94 13890 }
252b5132
RH
13891 break;
13892
13893 default:
13894 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 13895 are added dynamically. */
1661c76c 13896 as_bad (_("macro %s not implemented yet"), ip->insn_mo->name);
252b5132
RH
13897 break;
13898 }
741fe287 13899 if (!mips_opts.at && used_at)
1661c76c 13900 as_bad (_("macro used $at after \".set noat\""));
252b5132
RH
13901}
13902
13903/* Implement macros in mips16 mode. */
13904
13905static void
17a2f251 13906mips16_macro (struct mips_cl_insn *ip)
252b5132 13907{
c0ebe874 13908 const struct mips_operand_array *operands;
252b5132 13909 int mask;
c0ebe874 13910 int tmp;
252b5132
RH
13911 expressionS expr1;
13912 int dbl;
13913 const char *s, *s2, *s3;
c0ebe874
RS
13914 unsigned int op[MAX_OPERANDS];
13915 unsigned int i;
252b5132
RH
13916
13917 mask = ip->insn_mo->mask;
13918
c0ebe874
RS
13919 operands = insn_operands (ip);
13920 for (i = 0; i < MAX_OPERANDS; i++)
13921 if (operands->operand[i])
13922 op[i] = insn_extract_operand (ip, operands->operand[i]);
13923 else
13924 op[i] = -1;
252b5132 13925
252b5132
RH
13926 expr1.X_op = O_constant;
13927 expr1.X_op_symbol = NULL;
13928 expr1.X_add_symbol = NULL;
13929 expr1.X_add_number = 1;
13930
13931 dbl = 0;
13932
13933 switch (mask)
13934 {
13935 default:
b37df7c4 13936 abort ();
252b5132
RH
13937
13938 case M_DDIV_3:
13939 dbl = 1;
1a0670f3 13940 /* Fall through. */
252b5132
RH
13941 case M_DIV_3:
13942 s = "mflo";
13943 goto do_div3;
13944 case M_DREM_3:
13945 dbl = 1;
1a0670f3 13946 /* Fall through. */
252b5132
RH
13947 case M_REM_3:
13948 s = "mfhi";
13949 do_div3:
7d10b47d 13950 start_noreorder ();
d8722d76 13951 macro_build (NULL, dbl ? "ddiv" : "div", ".,x,y", op[1], op[2]);
252b5132 13952 expr1.X_add_number = 2;
c0ebe874 13953 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 13954 macro_build (NULL, "break", "6", 7);
bdaaa2e1 13955
252b5132
RH
13956 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
13957 since that causes an overflow. We should do that as well,
13958 but I don't see how to do the comparisons without a temporary
13959 register. */
7d10b47d 13960 end_noreorder ();
c0ebe874 13961 macro_build (NULL, s, "x", op[0]);
252b5132
RH
13962 break;
13963
13964 case M_DIVU_3:
13965 s = "divu";
13966 s2 = "mflo";
13967 goto do_divu3;
13968 case M_REMU_3:
13969 s = "divu";
13970 s2 = "mfhi";
13971 goto do_divu3;
13972 case M_DDIVU_3:
13973 s = "ddivu";
13974 s2 = "mflo";
13975 goto do_divu3;
13976 case M_DREMU_3:
13977 s = "ddivu";
13978 s2 = "mfhi";
13979 do_divu3:
7d10b47d 13980 start_noreorder ();
d8722d76 13981 macro_build (NULL, s, ".,x,y", op[1], op[2]);
252b5132 13982 expr1.X_add_number = 2;
c0ebe874 13983 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 13984 macro_build (NULL, "break", "6", 7);
7d10b47d 13985 end_noreorder ();
c0ebe874 13986 macro_build (NULL, s2, "x", op[0]);
252b5132
RH
13987 break;
13988
13989 case M_DMUL:
13990 dbl = 1;
1a0670f3 13991 /* Fall through. */
252b5132 13992 case M_MUL:
c0ebe874
RS
13993 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", op[1], op[2]);
13994 macro_build (NULL, "mflo", "x", op[0]);
8fc2e39e 13995 break;
252b5132
RH
13996
13997 case M_DSUBU_I:
13998 dbl = 1;
13999 goto do_subu;
14000 case M_SUBU_I:
14001 do_subu:
252b5132 14002 imm_expr.X_add_number = -imm_expr.X_add_number;
d8722d76 14003 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,F", op[0], op[1]);
252b5132
RH
14004 break;
14005
14006 case M_SUBU_I_2:
252b5132 14007 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 14008 macro_build (&imm_expr, "addiu", "x,k", op[0]);
252b5132
RH
14009 break;
14010
14011 case M_DSUBU_I_2:
252b5132 14012 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 14013 macro_build (&imm_expr, "daddiu", "y,j", op[0]);
252b5132
RH
14014 break;
14015
14016 case M_BEQ:
14017 s = "cmp";
14018 s2 = "bteqz";
14019 goto do_branch;
14020 case M_BNE:
14021 s = "cmp";
14022 s2 = "btnez";
14023 goto do_branch;
14024 case M_BLT:
14025 s = "slt";
14026 s2 = "btnez";
14027 goto do_branch;
14028 case M_BLTU:
14029 s = "sltu";
14030 s2 = "btnez";
14031 goto do_branch;
14032 case M_BLE:
14033 s = "slt";
14034 s2 = "bteqz";
14035 goto do_reverse_branch;
14036 case M_BLEU:
14037 s = "sltu";
14038 s2 = "bteqz";
14039 goto do_reverse_branch;
14040 case M_BGE:
14041 s = "slt";
14042 s2 = "bteqz";
14043 goto do_branch;
14044 case M_BGEU:
14045 s = "sltu";
14046 s2 = "bteqz";
14047 goto do_branch;
14048 case M_BGT:
14049 s = "slt";
14050 s2 = "btnez";
14051 goto do_reverse_branch;
14052 case M_BGTU:
14053 s = "sltu";
14054 s2 = "btnez";
14055
14056 do_reverse_branch:
c0ebe874
RS
14057 tmp = op[1];
14058 op[1] = op[0];
14059 op[0] = tmp;
252b5132
RH
14060
14061 do_branch:
c0ebe874 14062 macro_build (NULL, s, "x,y", op[0], op[1]);
67c0d1eb 14063 macro_build (&offset_expr, s2, "p");
252b5132
RH
14064 break;
14065
14066 case M_BEQ_I:
14067 s = "cmpi";
14068 s2 = "bteqz";
14069 s3 = "x,U";
14070 goto do_branch_i;
14071 case M_BNE_I:
14072 s = "cmpi";
14073 s2 = "btnez";
14074 s3 = "x,U";
14075 goto do_branch_i;
14076 case M_BLT_I:
14077 s = "slti";
14078 s2 = "btnez";
14079 s3 = "x,8";
14080 goto do_branch_i;
14081 case M_BLTU_I:
14082 s = "sltiu";
14083 s2 = "btnez";
14084 s3 = "x,8";
14085 goto do_branch_i;
14086 case M_BLE_I:
14087 s = "slti";
14088 s2 = "btnez";
14089 s3 = "x,8";
14090 goto do_addone_branch_i;
14091 case M_BLEU_I:
14092 s = "sltiu";
14093 s2 = "btnez";
14094 s3 = "x,8";
14095 goto do_addone_branch_i;
14096 case M_BGE_I:
14097 s = "slti";
14098 s2 = "bteqz";
14099 s3 = "x,8";
14100 goto do_branch_i;
14101 case M_BGEU_I:
14102 s = "sltiu";
14103 s2 = "bteqz";
14104 s3 = "x,8";
14105 goto do_branch_i;
14106 case M_BGT_I:
14107 s = "slti";
14108 s2 = "bteqz";
14109 s3 = "x,8";
14110 goto do_addone_branch_i;
14111 case M_BGTU_I:
14112 s = "sltiu";
14113 s2 = "bteqz";
14114 s3 = "x,8";
14115
14116 do_addone_branch_i:
252b5132
RH
14117 ++imm_expr.X_add_number;
14118
14119 do_branch_i:
c0ebe874 14120 macro_build (&imm_expr, s, s3, op[0]);
67c0d1eb 14121 macro_build (&offset_expr, s2, "p");
252b5132
RH
14122 break;
14123
14124 case M_ABS:
14125 expr1.X_add_number = 0;
c0ebe874
RS
14126 macro_build (&expr1, "slti", "x,8", op[1]);
14127 if (op[0] != op[1])
14128 macro_build (NULL, "move", "y,X", op[0], mips16_to_32_reg_map[op[1]]);
252b5132 14129 expr1.X_add_number = 2;
67c0d1eb 14130 macro_build (&expr1, "bteqz", "p");
c0ebe874 14131 macro_build (NULL, "neg", "x,w", op[0], op[0]);
0acfaea6 14132 break;
252b5132
RH
14133 }
14134}
14135
14daeee3
RS
14136/* Look up instruction [START, START + LENGTH) in HASH. Record any extra
14137 opcode bits in *OPCODE_EXTRA. */
14138
14139static struct mips_opcode *
14140mips_lookup_insn (struct hash_control *hash, const char *start,
da8bca91 14141 ssize_t length, unsigned int *opcode_extra)
14daeee3
RS
14142{
14143 char *name, *dot, *p;
14144 unsigned int mask, suffix;
da8bca91 14145 ssize_t opend;
14daeee3
RS
14146 struct mips_opcode *insn;
14147
14148 /* Make a copy of the instruction so that we can fiddle with it. */
4ec9d7d5 14149 name = xstrndup (start, length);
14daeee3
RS
14150
14151 /* Look up the instruction as-is. */
14152 insn = (struct mips_opcode *) hash_find (hash, name);
ee5734f0 14153 if (insn)
e1fa0163 14154 goto end;
14daeee3
RS
14155
14156 dot = strchr (name, '.');
14157 if (dot && dot[1])
14158 {
14159 /* Try to interpret the text after the dot as a VU0 channel suffix. */
14160 p = mips_parse_vu0_channels (dot + 1, &mask);
14161 if (*p == 0 && mask != 0)
14162 {
14163 *dot = 0;
14164 insn = (struct mips_opcode *) hash_find (hash, name);
14165 *dot = '.';
14166 if (insn && (insn->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0)
14167 {
14168 *opcode_extra |= mask << mips_vu0_channel_mask.lsb;
e1fa0163 14169 goto end;
14daeee3
RS
14170 }
14171 }
14172 }
14173
14174 if (mips_opts.micromips)
14175 {
14176 /* See if there's an instruction size override suffix,
14177 either `16' or `32', at the end of the mnemonic proper,
14178 that defines the operation, i.e. before the first `.'
14179 character if any. Strip it and retry. */
14180 opend = dot != NULL ? dot - name : length;
14181 if (opend >= 3 && name[opend - 2] == '1' && name[opend - 1] == '6')
14182 suffix = 2;
14183 else if (name[opend - 2] == '3' && name[opend - 1] == '2')
14184 suffix = 4;
14185 else
14186 suffix = 0;
14187 if (suffix)
14188 {
39334a61 14189 memmove (name + opend - 2, name + opend, length - opend + 1);
14daeee3 14190 insn = (struct mips_opcode *) hash_find (hash, name);
ee5734f0 14191 if (insn)
14daeee3
RS
14192 {
14193 forced_insn_length = suffix;
e1fa0163 14194 goto end;
14daeee3
RS
14195 }
14196 }
14197 }
14198
e1fa0163
NC
14199 insn = NULL;
14200 end:
14201 free (name);
14202 return insn;
14daeee3
RS
14203}
14204
77bd4346 14205/* Assemble an instruction into its binary format. If the instruction
e423441d
RS
14206 is a macro, set imm_expr and offset_expr to the values associated
14207 with "I" and "A" operands respectively. Otherwise store the value
14208 of the relocatable field (if any) in offset_expr. In both cases
14209 set offset_reloc to the relocation operators applied to offset_expr. */
252b5132
RH
14210
14211static void
60f20e8b 14212mips_ip (char *str, struct mips_cl_insn *insn)
252b5132 14213{
60f20e8b 14214 const struct mips_opcode *first, *past;
df58fc94 14215 struct hash_control *hash;
a92713e6 14216 char format;
14daeee3 14217 size_t end;
a92713e6 14218 struct mips_operand_token *tokens;
14daeee3 14219 unsigned int opcode_extra;
252b5132 14220
df58fc94
RS
14221 if (mips_opts.micromips)
14222 {
14223 hash = micromips_op_hash;
14224 past = &micromips_opcodes[bfd_micromips_num_opcodes];
14225 }
14226 else
14227 {
14228 hash = op_hash;
14229 past = &mips_opcodes[NUMOPCODES];
14230 }
14231 forced_insn_length = 0;
14daeee3 14232 opcode_extra = 0;
252b5132 14233
df58fc94 14234 /* We first try to match an instruction up to a space or to the end. */
a40bc9dd
RS
14235 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
14236 continue;
bdaaa2e1 14237
60f20e8b
RS
14238 first = mips_lookup_insn (hash, str, end, &opcode_extra);
14239 if (first == NULL)
252b5132 14240 {
1661c76c 14241 set_insn_error (0, _("unrecognized opcode"));
a40bc9dd 14242 return;
252b5132
RH
14243 }
14244
60f20e8b 14245 if (strcmp (first->name, "li.s") == 0)
a92713e6 14246 format = 'f';
60f20e8b 14247 else if (strcmp (first->name, "li.d") == 0)
a92713e6
RS
14248 format = 'd';
14249 else
14250 format = 0;
14251 tokens = mips_parse_arguments (str + end, format);
14252 if (!tokens)
14253 return;
14254
60f20e8b
RS
14255 if (!match_insns (insn, first, past, tokens, opcode_extra, FALSE)
14256 && !match_insns (insn, first, past, tokens, opcode_extra, TRUE))
1661c76c 14257 set_insn_error (0, _("invalid operands"));
df58fc94 14258
e3de51ce 14259 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
14260}
14261
77bd4346
RS
14262/* As for mips_ip, but used when assembling MIPS16 code.
14263 Also set forced_insn_length to the resulting instruction size in
14264 bytes if the user explicitly requested a small or extended instruction. */
252b5132
RH
14265
14266static void
60f20e8b 14267mips16_ip (char *str, struct mips_cl_insn *insn)
252b5132 14268{
1a00e612 14269 char *end, *s, c;
60f20e8b 14270 struct mips_opcode *first;
a92713e6 14271 struct mips_operand_token *tokens;
3fb49709 14272 unsigned int l;
252b5132 14273
25499ac7 14274 for (s = str; *s != '\0' && *s != '.' && *s != ' '; ++s)
252b5132 14275 ;
1a00e612
RS
14276 end = s;
14277 c = *end;
3fb49709
MR
14278
14279 l = 0;
1a00e612 14280 switch (c)
252b5132
RH
14281 {
14282 case '\0':
14283 break;
14284
14285 case ' ':
1a00e612 14286 s++;
252b5132
RH
14287 break;
14288
14289 case '.':
3fb49709
MR
14290 s++;
14291 if (*s == 't')
252b5132 14292 {
3fb49709
MR
14293 l = 2;
14294 s++;
252b5132 14295 }
3fb49709 14296 else if (*s == 'e')
252b5132 14297 {
3fb49709
MR
14298 l = 4;
14299 s++;
252b5132 14300 }
3fb49709
MR
14301 if (*s == '\0')
14302 break;
14303 else if (*s++ == ' ')
14304 break;
1661c76c 14305 set_insn_error (0, _("unrecognized opcode"));
252b5132
RH
14306 return;
14307 }
3fb49709 14308 forced_insn_length = l;
252b5132 14309
1a00e612 14310 *end = 0;
60f20e8b 14311 first = (struct mips_opcode *) hash_find (mips16_op_hash, str);
1a00e612
RS
14312 *end = c;
14313
60f20e8b 14314 if (!first)
252b5132 14315 {
1661c76c 14316 set_insn_error (0, _("unrecognized opcode"));
252b5132
RH
14317 return;
14318 }
14319
a92713e6
RS
14320 tokens = mips_parse_arguments (s, 0);
14321 if (!tokens)
14322 return;
14323
60f20e8b 14324 if (!match_mips16_insns (insn, first, tokens))
1661c76c 14325 set_insn_error (0, _("invalid operands"));
252b5132 14326
e3de51ce 14327 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
14328}
14329
b886a2ab
RS
14330/* Marshal immediate value VAL for an extended MIPS16 instruction.
14331 NBITS is the number of significant bits in VAL. */
14332
14333static unsigned long
14334mips16_immed_extend (offsetT val, unsigned int nbits)
14335{
14336 int extval;
25499ac7
MR
14337
14338 extval = 0;
14339 val &= (1U << nbits) - 1;
14340 if (nbits == 16 || nbits == 9)
b886a2ab
RS
14341 {
14342 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14343 val &= 0x1f;
14344 }
14345 else if (nbits == 15)
14346 {
14347 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14348 val &= 0xf;
14349 }
25499ac7 14350 else if (nbits == 6)
b886a2ab
RS
14351 {
14352 extval = ((val & 0x1f) << 6) | (val & 0x20);
14353 val = 0;
14354 }
14355 return (extval << 16) | val;
14356}
14357
3ccad066
RS
14358/* Like decode_mips16_operand, but require the operand to be defined and
14359 require it to be an integer. */
14360
14361static const struct mips_int_operand *
14362mips16_immed_operand (int type, bfd_boolean extended_p)
14363{
14364 const struct mips_operand *operand;
14365
14366 operand = decode_mips16_operand (type, extended_p);
14367 if (!operand || (operand->type != OP_INT && operand->type != OP_PCREL))
14368 abort ();
14369 return (const struct mips_int_operand *) operand;
14370}
14371
14372/* Return true if SVAL fits OPERAND. RELOC is as for mips16_immed. */
14373
14374static bfd_boolean
14375mips16_immed_in_range_p (const struct mips_int_operand *operand,
14376 bfd_reloc_code_real_type reloc, offsetT sval)
14377{
14378 int min_val, max_val;
14379
14380 min_val = mips_int_operand_min (operand);
14381 max_val = mips_int_operand_max (operand);
14382 if (reloc != BFD_RELOC_UNUSED)
14383 {
14384 if (min_val < 0)
14385 sval = SEXT_16BIT (sval);
14386 else
14387 sval &= 0xffff;
14388 }
14389
14390 return (sval >= min_val
14391 && sval <= max_val
14392 && (sval & ((1 << operand->shift) - 1)) == 0);
14393}
14394
5c04167a
RS
14395/* Install immediate value VAL into MIPS16 instruction *INSN,
14396 extending it if necessary. The instruction in *INSN may
14397 already be extended.
14398
43c0598f
RS
14399 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14400 if none. In the former case, VAL is a 16-bit number with no
14401 defined signedness.
14402
14403 TYPE is the type of the immediate field. USER_INSN_LENGTH
14404 is the length that the user requested, or 0 if none. */
252b5132
RH
14405
14406static void
3b4dbbbf 14407mips16_immed (const char *file, unsigned int line, int type,
43c0598f 14408 bfd_reloc_code_real_type reloc, offsetT val,
5c04167a 14409 unsigned int user_insn_length, unsigned long *insn)
252b5132 14410{
3ccad066
RS
14411 const struct mips_int_operand *operand;
14412 unsigned int uval, length;
252b5132 14413
3ccad066
RS
14414 operand = mips16_immed_operand (type, FALSE);
14415 if (!mips16_immed_in_range_p (operand, reloc, val))
5c04167a
RS
14416 {
14417 /* We need an extended instruction. */
14418 if (user_insn_length == 2)
14419 as_bad_where (file, line, _("invalid unextended operand value"));
14420 else
14421 *insn |= MIPS16_EXTEND;
14422 }
14423 else if (user_insn_length == 4)
14424 {
14425 /* The operand doesn't force an unextended instruction to be extended.
14426 Warn if the user wanted an extended instruction anyway. */
14427 *insn |= MIPS16_EXTEND;
14428 as_warn_where (file, line,
14429 _("extended operand requested but not required"));
14430 }
252b5132 14431
3ccad066
RS
14432 length = mips16_opcode_length (*insn);
14433 if (length == 4)
252b5132 14434 {
3ccad066
RS
14435 operand = mips16_immed_operand (type, TRUE);
14436 if (!mips16_immed_in_range_p (operand, reloc, val))
14437 as_bad_where (file, line,
14438 _("operand value out of range for instruction"));
252b5132 14439 }
3ccad066 14440 uval = ((unsigned int) val >> operand->shift) - operand->bias;
bdd15286 14441 if (length == 2 || operand->root.lsb != 0)
3ccad066 14442 *insn = mips_insert_operand (&operand->root, *insn, uval);
252b5132 14443 else
3ccad066 14444 *insn |= mips16_immed_extend (uval, operand->root.size);
252b5132
RH
14445}
14446\f
d6f16593 14447struct percent_op_match
ad8d3bb3 14448{
5e0116d5
RS
14449 const char *str;
14450 bfd_reloc_code_real_type reloc;
d6f16593
MR
14451};
14452
14453static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 14454{
5e0116d5 14455 {"%lo", BFD_RELOC_LO16},
5e0116d5
RS
14456 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14457 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14458 {"%call16", BFD_RELOC_MIPS_CALL16},
14459 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14460 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14461 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14462 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14463 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14464 {"%got", BFD_RELOC_MIPS_GOT16},
14465 {"%gp_rel", BFD_RELOC_GPREL16},
be3f1006 14466 {"%gprel", BFD_RELOC_GPREL16},
5e0116d5
RS
14467 {"%half", BFD_RELOC_16},
14468 {"%highest", BFD_RELOC_MIPS_HIGHEST},
14469 {"%higher", BFD_RELOC_MIPS_HIGHER},
14470 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
14471 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14472 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14473 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14474 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14475 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14476 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14477 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
7361da2c
AB
14478 {"%hi", BFD_RELOC_HI16_S},
14479 {"%pcrel_hi", BFD_RELOC_HI16_S_PCREL},
14480 {"%pcrel_lo", BFD_RELOC_LO16_PCREL}
ad8d3bb3
TS
14481};
14482
d6f16593
MR
14483static const struct percent_op_match mips16_percent_op[] =
14484{
14485 {"%lo", BFD_RELOC_MIPS16_LO16},
be3f1006 14486 {"%gp_rel", BFD_RELOC_MIPS16_GPREL},
d6f16593 14487 {"%gprel", BFD_RELOC_MIPS16_GPREL},
738e5348
RS
14488 {"%got", BFD_RELOC_MIPS16_GOT16},
14489 {"%call16", BFD_RELOC_MIPS16_CALL16},
d0f13682
CLT
14490 {"%hi", BFD_RELOC_MIPS16_HI16_S},
14491 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14492 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14493 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14494 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14495 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14496 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14497 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
d6f16593
MR
14498};
14499
252b5132 14500
5e0116d5
RS
14501/* Return true if *STR points to a relocation operator. When returning true,
14502 move *STR over the operator and store its relocation code in *RELOC.
14503 Leave both *STR and *RELOC alone when returning false. */
14504
14505static bfd_boolean
17a2f251 14506parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 14507{
d6f16593
MR
14508 const struct percent_op_match *percent_op;
14509 size_t limit, i;
14510
14511 if (mips_opts.mips16)
14512 {
14513 percent_op = mips16_percent_op;
14514 limit = ARRAY_SIZE (mips16_percent_op);
14515 }
14516 else
14517 {
14518 percent_op = mips_percent_op;
14519 limit = ARRAY_SIZE (mips_percent_op);
14520 }
76b3015f 14521
d6f16593 14522 for (i = 0; i < limit; i++)
5e0116d5 14523 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 14524 {
3f98094e
DJ
14525 int len = strlen (percent_op[i].str);
14526
14527 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14528 continue;
14529
5e0116d5
RS
14530 *str += strlen (percent_op[i].str);
14531 *reloc = percent_op[i].reloc;
394f9b3a 14532
5e0116d5
RS
14533 /* Check whether the output BFD supports this relocation.
14534 If not, issue an error and fall back on something safe. */
14535 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 14536 {
20203fb9 14537 as_bad (_("relocation %s isn't supported by the current ABI"),
5e0116d5 14538 percent_op[i].str);
01a3f561 14539 *reloc = BFD_RELOC_UNUSED;
394f9b3a 14540 }
5e0116d5 14541 return TRUE;
394f9b3a 14542 }
5e0116d5 14543 return FALSE;
394f9b3a 14544}
ad8d3bb3 14545
ad8d3bb3 14546
5e0116d5
RS
14547/* Parse string STR as a 16-bit relocatable operand. Store the
14548 expression in *EP and the relocations in the array starting
14549 at RELOC. Return the number of relocation operators used.
ad8d3bb3 14550
01a3f561 14551 On exit, EXPR_END points to the first character after the expression. */
ad8d3bb3 14552
5e0116d5 14553static size_t
17a2f251
TS
14554my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14555 char *str)
ad8d3bb3 14556{
5e0116d5
RS
14557 bfd_reloc_code_real_type reversed_reloc[3];
14558 size_t reloc_index, i;
09b8f35a
RS
14559 int crux_depth, str_depth;
14560 char *crux;
5e0116d5
RS
14561
14562 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
14563 in REVERSED_RELOC. End the loop with CRUX pointing to the start
14564 of the main expression and with CRUX_DEPTH containing the number
14565 of open brackets at that point. */
14566 reloc_index = -1;
14567 str_depth = 0;
14568 do
fb1b3232 14569 {
09b8f35a
RS
14570 reloc_index++;
14571 crux = str;
14572 crux_depth = str_depth;
14573
14574 /* Skip over whitespace and brackets, keeping count of the number
14575 of brackets. */
14576 while (*str == ' ' || *str == '\t' || *str == '(')
14577 if (*str++ == '(')
14578 str_depth++;
5e0116d5 14579 }
09b8f35a
RS
14580 while (*str == '%'
14581 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14582 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 14583
09b8f35a 14584 my_getExpression (ep, crux);
5e0116d5 14585 str = expr_end;
394f9b3a 14586
5e0116d5 14587 /* Match every open bracket. */
09b8f35a 14588 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 14589 if (*str++ == ')')
09b8f35a 14590 crux_depth--;
394f9b3a 14591
09b8f35a 14592 if (crux_depth > 0)
20203fb9 14593 as_bad (_("unclosed '('"));
394f9b3a 14594
5e0116d5 14595 expr_end = str;
252b5132 14596
01a3f561 14597 if (reloc_index != 0)
64bdfcaf
RS
14598 {
14599 prev_reloc_op_frag = frag_now;
14600 for (i = 0; i < reloc_index; i++)
14601 reloc[i] = reversed_reloc[reloc_index - 1 - i];
14602 }
fb1b3232 14603
5e0116d5 14604 return reloc_index;
252b5132
RH
14605}
14606
14607static void
17a2f251 14608my_getExpression (expressionS *ep, char *str)
252b5132
RH
14609{
14610 char *save_in;
14611
14612 save_in = input_line_pointer;
14613 input_line_pointer = str;
14614 expression (ep);
14615 expr_end = input_line_pointer;
14616 input_line_pointer = save_in;
252b5132
RH
14617}
14618
6d4af3c2 14619const char *
17a2f251 14620md_atof (int type, char *litP, int *sizeP)
252b5132 14621{
499ac353 14622 return ieee_md_atof (type, litP, sizeP, target_big_endian);
252b5132
RH
14623}
14624
14625void
17a2f251 14626md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
14627{
14628 if (target_big_endian)
14629 number_to_chars_bigendian (buf, val, n);
14630 else
14631 number_to_chars_littleendian (buf, val, n);
14632}
14633\f
e013f690
TS
14634static int support_64bit_objects(void)
14635{
14636 const char **list, **l;
aa3d8fdf 14637 int yes;
e013f690
TS
14638
14639 list = bfd_target_list ();
14640 for (l = list; *l != NULL; l++)
aeffff67
RS
14641 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14642 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
e013f690 14643 break;
aa3d8fdf 14644 yes = (*l != NULL);
e013f690 14645 free (list);
aa3d8fdf 14646 return yes;
e013f690
TS
14647}
14648
316f5878
RS
14649/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14650 NEW_VALUE. Warn if another value was already specified. Note:
14651 we have to defer parsing the -march and -mtune arguments in order
14652 to handle 'from-abi' correctly, since the ABI might be specified
14653 in a later argument. */
14654
14655static void
17a2f251 14656mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
14657{
14658 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
1661c76c 14659 as_warn (_("a different %s was already specified, is now %s"),
316f5878
RS
14660 string_ptr == &mips_arch_string ? "-march" : "-mtune",
14661 new_value);
14662
14663 *string_ptr = new_value;
14664}
14665
252b5132 14666int
17b9d67d 14667md_parse_option (int c, const char *arg)
252b5132 14668{
c6278170
RS
14669 unsigned int i;
14670
14671 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
14672 if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
14673 {
919731af 14674 file_ase_explicit |= mips_set_ase (&mips_ases[i], &file_mips_opts,
c6278170
RS
14675 c == mips_ases[i].option_on);
14676 return 1;
14677 }
14678
252b5132
RH
14679 switch (c)
14680 {
119d663a
NC
14681 case OPTION_CONSTRUCT_FLOATS:
14682 mips_disable_float_construction = 0;
14683 break;
bdaaa2e1 14684
119d663a
NC
14685 case OPTION_NO_CONSTRUCT_FLOATS:
14686 mips_disable_float_construction = 1;
14687 break;
bdaaa2e1 14688
252b5132
RH
14689 case OPTION_TRAP:
14690 mips_trap = 1;
14691 break;
14692
14693 case OPTION_BREAK:
14694 mips_trap = 0;
14695 break;
14696
14697 case OPTION_EB:
14698 target_big_endian = 1;
14699 break;
14700
14701 case OPTION_EL:
14702 target_big_endian = 0;
14703 break;
14704
14705 case 'O':
4ffff32f
TS
14706 if (arg == NULL)
14707 mips_optimize = 1;
14708 else if (arg[0] == '0')
14709 mips_optimize = 0;
14710 else if (arg[0] == '1')
252b5132
RH
14711 mips_optimize = 1;
14712 else
14713 mips_optimize = 2;
14714 break;
14715
14716 case 'g':
14717 if (arg == NULL)
14718 mips_debug = 2;
14719 else
14720 mips_debug = atoi (arg);
252b5132
RH
14721 break;
14722
14723 case OPTION_MIPS1:
0b35dfee 14724 file_mips_opts.isa = ISA_MIPS1;
252b5132
RH
14725 break;
14726
14727 case OPTION_MIPS2:
0b35dfee 14728 file_mips_opts.isa = ISA_MIPS2;
252b5132
RH
14729 break;
14730
14731 case OPTION_MIPS3:
0b35dfee 14732 file_mips_opts.isa = ISA_MIPS3;
252b5132
RH
14733 break;
14734
14735 case OPTION_MIPS4:
0b35dfee 14736 file_mips_opts.isa = ISA_MIPS4;
e7af610e
NC
14737 break;
14738
84ea6cf2 14739 case OPTION_MIPS5:
0b35dfee 14740 file_mips_opts.isa = ISA_MIPS5;
84ea6cf2
NC
14741 break;
14742
e7af610e 14743 case OPTION_MIPS32:
0b35dfee 14744 file_mips_opts.isa = ISA_MIPS32;
252b5132
RH
14745 break;
14746
af7ee8bf 14747 case OPTION_MIPS32R2:
0b35dfee 14748 file_mips_opts.isa = ISA_MIPS32R2;
af7ee8bf
CD
14749 break;
14750
ae52f483 14751 case OPTION_MIPS32R3:
0ae19f05 14752 file_mips_opts.isa = ISA_MIPS32R3;
ae52f483
AB
14753 break;
14754
14755 case OPTION_MIPS32R5:
0ae19f05 14756 file_mips_opts.isa = ISA_MIPS32R5;
ae52f483
AB
14757 break;
14758
7361da2c
AB
14759 case OPTION_MIPS32R6:
14760 file_mips_opts.isa = ISA_MIPS32R6;
14761 break;
14762
5f74bc13 14763 case OPTION_MIPS64R2:
0b35dfee 14764 file_mips_opts.isa = ISA_MIPS64R2;
5f74bc13
CD
14765 break;
14766
ae52f483 14767 case OPTION_MIPS64R3:
0ae19f05 14768 file_mips_opts.isa = ISA_MIPS64R3;
ae52f483
AB
14769 break;
14770
14771 case OPTION_MIPS64R5:
0ae19f05 14772 file_mips_opts.isa = ISA_MIPS64R5;
ae52f483
AB
14773 break;
14774
7361da2c
AB
14775 case OPTION_MIPS64R6:
14776 file_mips_opts.isa = ISA_MIPS64R6;
14777 break;
14778
84ea6cf2 14779 case OPTION_MIPS64:
0b35dfee 14780 file_mips_opts.isa = ISA_MIPS64;
84ea6cf2
NC
14781 break;
14782
ec68c924 14783 case OPTION_MTUNE:
316f5878
RS
14784 mips_set_option_string (&mips_tune_string, arg);
14785 break;
ec68c924 14786
316f5878
RS
14787 case OPTION_MARCH:
14788 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
14789 break;
14790
14791 case OPTION_M4650:
316f5878
RS
14792 mips_set_option_string (&mips_arch_string, "4650");
14793 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
14794 break;
14795
14796 case OPTION_NO_M4650:
14797 break;
14798
14799 case OPTION_M4010:
316f5878
RS
14800 mips_set_option_string (&mips_arch_string, "4010");
14801 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
14802 break;
14803
14804 case OPTION_NO_M4010:
14805 break;
14806
14807 case OPTION_M4100:
316f5878
RS
14808 mips_set_option_string (&mips_arch_string, "4100");
14809 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
14810 break;
14811
14812 case OPTION_NO_M4100:
14813 break;
14814
252b5132 14815 case OPTION_M3900:
316f5878
RS
14816 mips_set_option_string (&mips_arch_string, "3900");
14817 mips_set_option_string (&mips_tune_string, "3900");
252b5132 14818 break;
bdaaa2e1 14819
252b5132
RH
14820 case OPTION_NO_M3900:
14821 break;
14822
df58fc94 14823 case OPTION_MICROMIPS:
919731af 14824 if (file_mips_opts.mips16 == 1)
df58fc94
RS
14825 {
14826 as_bad (_("-mmicromips cannot be used with -mips16"));
14827 return 0;
14828 }
919731af 14829 file_mips_opts.micromips = 1;
df58fc94
RS
14830 mips_no_prev_insn ();
14831 break;
14832
14833 case OPTION_NO_MICROMIPS:
919731af 14834 file_mips_opts.micromips = 0;
df58fc94
RS
14835 mips_no_prev_insn ();
14836 break;
14837
252b5132 14838 case OPTION_MIPS16:
919731af 14839 if (file_mips_opts.micromips == 1)
df58fc94
RS
14840 {
14841 as_bad (_("-mips16 cannot be used with -micromips"));
14842 return 0;
14843 }
919731af 14844 file_mips_opts.mips16 = 1;
7d10b47d 14845 mips_no_prev_insn ();
252b5132
RH
14846 break;
14847
14848 case OPTION_NO_MIPS16:
919731af 14849 file_mips_opts.mips16 = 0;
7d10b47d 14850 mips_no_prev_insn ();
252b5132
RH
14851 break;
14852
6a32d874
CM
14853 case OPTION_FIX_24K:
14854 mips_fix_24k = 1;
14855 break;
14856
14857 case OPTION_NO_FIX_24K:
14858 mips_fix_24k = 0;
14859 break;
14860
a8d14a88
CM
14861 case OPTION_FIX_RM7000:
14862 mips_fix_rm7000 = 1;
14863 break;
14864
14865 case OPTION_NO_FIX_RM7000:
14866 mips_fix_rm7000 = 0;
14867 break;
14868
6f2117ba
PH
14869 case OPTION_FIX_LOONGSON3_LLSC:
14870 mips_fix_loongson3_llsc = TRUE;
14871 break;
14872
14873 case OPTION_NO_FIX_LOONGSON3_LLSC:
14874 mips_fix_loongson3_llsc = FALSE;
14875 break;
14876
c67a084a
NC
14877 case OPTION_FIX_LOONGSON2F_JUMP:
14878 mips_fix_loongson2f_jump = TRUE;
14879 break;
14880
14881 case OPTION_NO_FIX_LOONGSON2F_JUMP:
14882 mips_fix_loongson2f_jump = FALSE;
14883 break;
14884
14885 case OPTION_FIX_LOONGSON2F_NOP:
14886 mips_fix_loongson2f_nop = TRUE;
14887 break;
14888
14889 case OPTION_NO_FIX_LOONGSON2F_NOP:
14890 mips_fix_loongson2f_nop = FALSE;
14891 break;
14892
d766e8ec
RS
14893 case OPTION_FIX_VR4120:
14894 mips_fix_vr4120 = 1;
60b63b72
RS
14895 break;
14896
d766e8ec
RS
14897 case OPTION_NO_FIX_VR4120:
14898 mips_fix_vr4120 = 0;
60b63b72
RS
14899 break;
14900
7d8e00cf
RS
14901 case OPTION_FIX_VR4130:
14902 mips_fix_vr4130 = 1;
14903 break;
14904
14905 case OPTION_NO_FIX_VR4130:
14906 mips_fix_vr4130 = 0;
14907 break;
14908
d954098f
DD
14909 case OPTION_FIX_CN63XXP1:
14910 mips_fix_cn63xxp1 = TRUE;
14911 break;
14912
14913 case OPTION_NO_FIX_CN63XXP1:
14914 mips_fix_cn63xxp1 = FALSE;
14915 break;
14916
27c634e0
FN
14917 case OPTION_FIX_R5900:
14918 mips_fix_r5900 = TRUE;
14919 mips_fix_r5900_explicit = TRUE;
14920 break;
14921
14922 case OPTION_NO_FIX_R5900:
14923 mips_fix_r5900 = FALSE;
14924 mips_fix_r5900_explicit = TRUE;
14925 break;
14926
4a6a3df4
AO
14927 case OPTION_RELAX_BRANCH:
14928 mips_relax_branch = 1;
14929 break;
14930
14931 case OPTION_NO_RELAX_BRANCH:
14932 mips_relax_branch = 0;
14933 break;
14934
8b10b0b3
MR
14935 case OPTION_IGNORE_BRANCH_ISA:
14936 mips_ignore_branch_isa = TRUE;
14937 break;
14938
14939 case OPTION_NO_IGNORE_BRANCH_ISA:
14940 mips_ignore_branch_isa = FALSE;
14941 break;
14942
833794fc 14943 case OPTION_INSN32:
919731af 14944 file_mips_opts.insn32 = TRUE;
833794fc
MR
14945 break;
14946
14947 case OPTION_NO_INSN32:
919731af 14948 file_mips_opts.insn32 = FALSE;
833794fc
MR
14949 break;
14950
aa6975fb
ILT
14951 case OPTION_MSHARED:
14952 mips_in_shared = TRUE;
14953 break;
14954
14955 case OPTION_MNO_SHARED:
14956 mips_in_shared = FALSE;
14957 break;
14958
aed1a261 14959 case OPTION_MSYM32:
919731af 14960 file_mips_opts.sym32 = TRUE;
aed1a261
RS
14961 break;
14962
14963 case OPTION_MNO_SYM32:
919731af 14964 file_mips_opts.sym32 = FALSE;
aed1a261
RS
14965 break;
14966
252b5132
RH
14967 /* When generating ELF code, we permit -KPIC and -call_shared to
14968 select SVR4_PIC, and -non_shared to select no PIC. This is
14969 intended to be compatible with Irix 5. */
14970 case OPTION_CALL_SHARED:
252b5132 14971 mips_pic = SVR4_PIC;
143d77c5 14972 mips_abicalls = TRUE;
252b5132
RH
14973 break;
14974
861fb55a 14975 case OPTION_CALL_NONPIC:
861fb55a
DJ
14976 mips_pic = NO_PIC;
14977 mips_abicalls = TRUE;
14978 break;
14979
252b5132 14980 case OPTION_NON_SHARED:
252b5132 14981 mips_pic = NO_PIC;
143d77c5 14982 mips_abicalls = FALSE;
252b5132
RH
14983 break;
14984
44075ae2
TS
14985 /* The -xgot option tells the assembler to use 32 bit offsets
14986 when accessing the got in SVR4_PIC mode. It is for Irix
252b5132
RH
14987 compatibility. */
14988 case OPTION_XGOT:
14989 mips_big_got = 1;
14990 break;
14991
14992 case 'G':
6caf9ef4
TS
14993 g_switch_value = atoi (arg);
14994 g_switch_seen = 1;
252b5132
RH
14995 break;
14996
34ba82a8
TS
14997 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
14998 and -mabi=64. */
252b5132 14999 case OPTION_32:
f3ded42a 15000 mips_abi = O32_ABI;
252b5132
RH
15001 break;
15002
e013f690 15003 case OPTION_N32:
316f5878 15004 mips_abi = N32_ABI;
e013f690 15005 break;
252b5132 15006
e013f690 15007 case OPTION_64:
316f5878 15008 mips_abi = N64_ABI;
f43abd2b 15009 if (!support_64bit_objects())
1661c76c 15010 as_fatal (_("no compiled in support for 64 bit object file format"));
252b5132
RH
15011 break;
15012
c97ef257 15013 case OPTION_GP32:
bad1aba3 15014 file_mips_opts.gp = 32;
c97ef257
AH
15015 break;
15016
15017 case OPTION_GP64:
bad1aba3 15018 file_mips_opts.gp = 64;
c97ef257 15019 break;
252b5132 15020
ca4e0257 15021 case OPTION_FP32:
0b35dfee 15022 file_mips_opts.fp = 32;
316f5878
RS
15023 break;
15024
351cdf24
MF
15025 case OPTION_FPXX:
15026 file_mips_opts.fp = 0;
15027 break;
15028
316f5878 15029 case OPTION_FP64:
0b35dfee 15030 file_mips_opts.fp = 64;
ca4e0257
RS
15031 break;
15032
351cdf24
MF
15033 case OPTION_ODD_SPREG:
15034 file_mips_opts.oddspreg = 1;
15035 break;
15036
15037 case OPTION_NO_ODD_SPREG:
15038 file_mips_opts.oddspreg = 0;
15039 break;
15040
037b32b9 15041 case OPTION_SINGLE_FLOAT:
0b35dfee 15042 file_mips_opts.single_float = 1;
037b32b9
AN
15043 break;
15044
15045 case OPTION_DOUBLE_FLOAT:
0b35dfee 15046 file_mips_opts.single_float = 0;
037b32b9
AN
15047 break;
15048
15049 case OPTION_SOFT_FLOAT:
0b35dfee 15050 file_mips_opts.soft_float = 1;
037b32b9
AN
15051 break;
15052
15053 case OPTION_HARD_FLOAT:
0b35dfee 15054 file_mips_opts.soft_float = 0;
037b32b9
AN
15055 break;
15056
252b5132 15057 case OPTION_MABI:
e013f690 15058 if (strcmp (arg, "32") == 0)
316f5878 15059 mips_abi = O32_ABI;
e013f690 15060 else if (strcmp (arg, "o64") == 0)
316f5878 15061 mips_abi = O64_ABI;
e013f690 15062 else if (strcmp (arg, "n32") == 0)
316f5878 15063 mips_abi = N32_ABI;
e013f690
TS
15064 else if (strcmp (arg, "64") == 0)
15065 {
316f5878 15066 mips_abi = N64_ABI;
e013f690 15067 if (! support_64bit_objects())
1661c76c 15068 as_fatal (_("no compiled in support for 64 bit object file "
e013f690
TS
15069 "format"));
15070 }
15071 else if (strcmp (arg, "eabi") == 0)
316f5878 15072 mips_abi = EABI_ABI;
e013f690 15073 else
da0e507f
TS
15074 {
15075 as_fatal (_("invalid abi -mabi=%s"), arg);
15076 return 0;
15077 }
252b5132
RH
15078 break;
15079
6b76fefe 15080 case OPTION_M7000_HILO_FIX:
b34976b6 15081 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
15082 break;
15083
9ee72ff1 15084 case OPTION_MNO_7000_HILO_FIX:
b34976b6 15085 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
15086 break;
15087
ecb4347a 15088 case OPTION_MDEBUG:
b34976b6 15089 mips_flag_mdebug = TRUE;
ecb4347a
DJ
15090 break;
15091
15092 case OPTION_NO_MDEBUG:
b34976b6 15093 mips_flag_mdebug = FALSE;
ecb4347a 15094 break;
dcd410fe
RO
15095
15096 case OPTION_PDR:
15097 mips_flag_pdr = TRUE;
15098 break;
15099
15100 case OPTION_NO_PDR:
15101 mips_flag_pdr = FALSE;
15102 break;
0a44bf69
RS
15103
15104 case OPTION_MVXWORKS_PIC:
15105 mips_pic = VXWORKS_PIC;
15106 break;
ecb4347a 15107
ba92f887
MR
15108 case OPTION_NAN:
15109 if (strcmp (arg, "2008") == 0)
7361da2c 15110 mips_nan2008 = 1;
ba92f887 15111 else if (strcmp (arg, "legacy") == 0)
7361da2c 15112 mips_nan2008 = 0;
ba92f887
MR
15113 else
15114 {
1661c76c 15115 as_fatal (_("invalid NaN setting -mnan=%s"), arg);
ba92f887
MR
15116 return 0;
15117 }
15118 break;
15119
252b5132
RH
15120 default:
15121 return 0;
15122 }
15123
c67a084a
NC
15124 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
15125
252b5132
RH
15126 return 1;
15127}
316f5878 15128\f
919731af 15129/* Set up globals to tune for the ISA or processor described by INFO. */
252b5132 15130
316f5878 15131static void
17a2f251 15132mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
15133{
15134 if (info != 0)
fef14a42 15135 mips_tune = info->cpu;
316f5878 15136}
80cc45a5 15137
34ba82a8 15138
252b5132 15139void
17a2f251 15140mips_after_parse_args (void)
e9670677 15141{
fef14a42
TS
15142 const struct mips_cpu_info *arch_info = 0;
15143 const struct mips_cpu_info *tune_info = 0;
15144
6f2117ba 15145 /* GP relative stuff not working for PE. */
6caf9ef4 15146 if (strncmp (TARGET_OS, "pe", 2) == 0)
e9670677 15147 {
6caf9ef4 15148 if (g_switch_seen && g_switch_value != 0)
1661c76c 15149 as_bad (_("-G not supported in this configuration"));
e9670677
MR
15150 g_switch_value = 0;
15151 }
15152
cac012d6
AO
15153 if (mips_abi == NO_ABI)
15154 mips_abi = MIPS_DEFAULT_ABI;
15155
919731af 15156 /* The following code determines the architecture.
22923709
RS
15157 Similar code was added to GCC 3.3 (see override_options() in
15158 config/mips/mips.c). The GAS and GCC code should be kept in sync
15159 as much as possible. */
e9670677 15160
316f5878 15161 if (mips_arch_string != 0)
fef14a42 15162 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 15163
0b35dfee 15164 if (file_mips_opts.isa != ISA_UNKNOWN)
e9670677 15165 {
0b35dfee 15166 /* Handle -mipsN. At this point, file_mips_opts.isa contains the
fef14a42 15167 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 15168 the -march selection (if any). */
fef14a42 15169 if (arch_info != 0)
e9670677 15170 {
316f5878
RS
15171 /* -march takes precedence over -mipsN, since it is more descriptive.
15172 There's no harm in specifying both as long as the ISA levels
15173 are the same. */
0b35dfee 15174 if (file_mips_opts.isa != arch_info->isa)
1661c76c
RS
15175 as_bad (_("-%s conflicts with the other architecture options,"
15176 " which imply -%s"),
0b35dfee 15177 mips_cpu_info_from_isa (file_mips_opts.isa)->name,
fef14a42 15178 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 15179 }
316f5878 15180 else
0b35dfee 15181 arch_info = mips_cpu_info_from_isa (file_mips_opts.isa);
e9670677
MR
15182 }
15183
fef14a42 15184 if (arch_info == 0)
95bfe26e
MF
15185 {
15186 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
15187 gas_assert (arch_info);
15188 }
e9670677 15189
fef14a42 15190 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
20203fb9 15191 as_bad (_("-march=%s is not compatible with the selected ABI"),
fef14a42
TS
15192 arch_info->name);
15193
919731af 15194 file_mips_opts.arch = arch_info->cpu;
15195 file_mips_opts.isa = arch_info->isa;
3315614d 15196 file_mips_opts.init_ase = arch_info->ase;
919731af 15197
15198 /* Set up initial mips_opts state. */
15199 mips_opts = file_mips_opts;
15200
27c634e0
FN
15201 /* For the R5900 default to `-mfix-r5900' unless the user told otherwise. */
15202 if (!mips_fix_r5900_explicit)
15203 mips_fix_r5900 = file_mips_opts.arch == CPU_R5900;
15204
919731af 15205 /* The register size inference code is now placed in
15206 file_mips_check_options. */
fef14a42 15207
0b35dfee 15208 /* Optimize for file_mips_opts.arch, unless -mtune selects a different
15209 processor. */
fef14a42
TS
15210 if (mips_tune_string != 0)
15211 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 15212
fef14a42
TS
15213 if (tune_info == 0)
15214 mips_set_tune (arch_info);
15215 else
15216 mips_set_tune (tune_info);
e9670677 15217
ecb4347a 15218 if (mips_flag_mdebug < 0)
e8044f35 15219 mips_flag_mdebug = 0;
e9670677
MR
15220}
15221\f
15222void
17a2f251 15223mips_init_after_args (void)
252b5132 15224{
6f2117ba 15225 /* Initialize opcodes. */
252b5132 15226 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 15227 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
15228}
15229
15230long
17a2f251 15231md_pcrel_from (fixS *fixP)
252b5132 15232{
a7ebbfdf 15233 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
6f2117ba 15234
a7ebbfdf
TS
15235 switch (fixP->fx_r_type)
15236 {
df58fc94
RS
15237 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15238 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15239 /* Return the address of the delay slot. */
15240 return addr + 2;
15241
15242 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15243 case BFD_RELOC_MICROMIPS_JMP:
c9775dde 15244 case BFD_RELOC_MIPS16_16_PCREL_S1:
a7ebbfdf 15245 case BFD_RELOC_16_PCREL_S2:
7361da2c
AB
15246 case BFD_RELOC_MIPS_21_PCREL_S2:
15247 case BFD_RELOC_MIPS_26_PCREL_S2:
a7ebbfdf
TS
15248 case BFD_RELOC_MIPS_JMP:
15249 /* Return the address of the delay slot. */
15250 return addr + 4;
df58fc94 15251
51f6035b
MR
15252 case BFD_RELOC_MIPS_18_PCREL_S3:
15253 /* Return the aligned address of the doubleword containing
15254 the instruction. */
15255 return addr & ~7;
15256
a7ebbfdf
TS
15257 default:
15258 return addr;
15259 }
252b5132
RH
15260}
15261
252b5132
RH
15262/* This is called before the symbol table is processed. In order to
15263 work with gcc when using mips-tfile, we must keep all local labels.
15264 However, in other cases, we want to discard them. If we were
15265 called with -g, but we didn't see any debugging information, it may
15266 mean that gcc is smuggling debugging information through to
15267 mips-tfile, in which case we must generate all local labels. */
15268
15269void
17a2f251 15270mips_frob_file_before_adjust (void)
252b5132
RH
15271{
15272#ifndef NO_ECOFF_DEBUGGING
15273 if (ECOFF_DEBUGGING
15274 && mips_debug != 0
15275 && ! ecoff_debugging_seen)
15276 flag_keep_locals = 1;
15277#endif
15278}
15279
3b91255e 15280/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 15281 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
15282 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
15283 relocation operators.
15284
15285 For our purposes, a %lo() expression matches a %got() or %hi()
15286 expression if:
15287
15288 (a) it refers to the same symbol; and
15289 (b) the offset applied in the %lo() expression is no lower than
15290 the offset applied in the %got() or %hi().
15291
15292 (b) allows us to cope with code like:
15293
15294 lui $4,%hi(foo)
15295 lh $4,%lo(foo+2)($4)
15296
15297 ...which is legal on RELA targets, and has a well-defined behaviour
15298 if the user knows that adding 2 to "foo" will not induce a carry to
15299 the high 16 bits.
15300
15301 When several %lo()s match a particular %got() or %hi(), we use the
15302 following rules to distinguish them:
15303
15304 (1) %lo()s with smaller offsets are a better match than %lo()s with
15305 higher offsets.
15306
15307 (2) %lo()s with no matching %got() or %hi() are better than those
15308 that already have a matching %got() or %hi().
15309
15310 (3) later %lo()s are better than earlier %lo()s.
15311
15312 These rules are applied in order.
15313
15314 (1) means, among other things, that %lo()s with identical offsets are
15315 chosen if they exist.
15316
15317 (2) means that we won't associate several high-part relocations with
15318 the same low-part relocation unless there's no alternative. Having
15319 several high parts for the same low part is a GNU extension; this rule
15320 allows careful users to avoid it.
15321
15322 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
15323 with the last high-part relocation being at the front of the list.
15324 It therefore makes sense to choose the last matching low-part
15325 relocation, all other things being equal. It's also easier
15326 to code that way. */
252b5132
RH
15327
15328void
17a2f251 15329mips_frob_file (void)
252b5132
RH
15330{
15331 struct mips_hi_fixup *l;
35903be0 15332 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
252b5132
RH
15333
15334 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15335 {
15336 segment_info_type *seginfo;
3b91255e
RS
15337 bfd_boolean matched_lo_p;
15338 fixS **hi_pos, **lo_pos, **pos;
252b5132 15339
9c2799c2 15340 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 15341
5919d012 15342 /* If a GOT16 relocation turns out to be against a global symbol,
b886a2ab
RS
15343 there isn't supposed to be a matching LO. Ignore %gots against
15344 constants; we'll report an error for those later. */
738e5348 15345 if (got16_reloc_p (l->fixp->fx_r_type)
b886a2ab 15346 && !(l->fixp->fx_addsy
9e009953 15347 && pic_need_relax (l->fixp->fx_addsy)))
5919d012
RS
15348 continue;
15349
15350 /* Check quickly whether the next fixup happens to be a matching %lo. */
15351 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
15352 continue;
15353
252b5132 15354 seginfo = seg_info (l->seg);
252b5132 15355
3b91255e
RS
15356 /* Set HI_POS to the position of this relocation in the chain.
15357 Set LO_POS to the position of the chosen low-part relocation.
15358 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15359 relocation that matches an immediately-preceding high-part
15360 relocation. */
15361 hi_pos = NULL;
15362 lo_pos = NULL;
15363 matched_lo_p = FALSE;
738e5348 15364 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
35903be0 15365
3b91255e
RS
15366 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15367 {
15368 if (*pos == l->fixp)
15369 hi_pos = pos;
15370
35903be0 15371 if ((*pos)->fx_r_type == looking_for_rtype
30cfc97a 15372 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
3b91255e
RS
15373 && (*pos)->fx_offset >= l->fixp->fx_offset
15374 && (lo_pos == NULL
15375 || (*pos)->fx_offset < (*lo_pos)->fx_offset
15376 || (!matched_lo_p
15377 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15378 lo_pos = pos;
15379
15380 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15381 && fixup_has_matching_lo_p (*pos));
15382 }
15383
15384 /* If we found a match, remove the high-part relocation from its
15385 current position and insert it before the low-part relocation.
15386 Make the offsets match so that fixup_has_matching_lo_p()
15387 will return true.
15388
15389 We don't warn about unmatched high-part relocations since some
15390 versions of gcc have been known to emit dead "lui ...%hi(...)"
15391 instructions. */
15392 if (lo_pos != NULL)
15393 {
15394 l->fixp->fx_offset = (*lo_pos)->fx_offset;
15395 if (l->fixp->fx_next != *lo_pos)
252b5132 15396 {
3b91255e
RS
15397 *hi_pos = l->fixp->fx_next;
15398 l->fixp->fx_next = *lo_pos;
15399 *lo_pos = l->fixp;
252b5132 15400 }
252b5132
RH
15401 }
15402 }
15403}
15404
252b5132 15405int
17a2f251 15406mips_force_relocation (fixS *fixp)
252b5132 15407{
ae6063d4 15408 if (generic_force_reloc (fixp))
252b5132
RH
15409 return 1;
15410
df58fc94
RS
15411 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15412 so that the linker relaxation can update targets. */
15413 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15414 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15415 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15416 return 1;
15417
5caa2b07
MR
15418 /* We want to keep BFD_RELOC_16_PCREL_S2 BFD_RELOC_MIPS_21_PCREL_S2
15419 and BFD_RELOC_MIPS_26_PCREL_S2 relocations against MIPS16 and
15420 microMIPS symbols so that we can do cross-mode branch diagnostics
15421 and BAL to JALX conversion by the linker. */
15422 if ((fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
9d862524
MR
15423 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15424 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2)
15425 && fixp->fx_addsy
15426 && ELF_ST_IS_COMPRESSED (S_GET_OTHER (fixp->fx_addsy)))
15427 return 1;
15428
7361da2c 15429 /* We want all PC-relative relocations to be kept for R6 relaxation. */
912815f0 15430 if (ISA_IS_R6 (file_mips_opts.isa)
7361da2c
AB
15431 && (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
15432 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15433 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
15434 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
15435 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
15436 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
15437 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL))
15438 return 1;
15439
3e722fb5 15440 return 0;
252b5132
RH
15441}
15442
b416ba9b
MR
15443/* Implement TC_FORCE_RELOCATION_ABS. */
15444
15445bfd_boolean
15446mips_force_relocation_abs (fixS *fixp)
15447{
15448 if (generic_force_reloc (fixp))
15449 return TRUE;
15450
15451 /* These relocations do not have enough bits in the in-place addend
15452 to hold an arbitrary absolute section's offset. */
15453 if (HAVE_IN_PLACE_ADDENDS && limited_pcrel_reloc_p (fixp->fx_r_type))
15454 return TRUE;
15455
15456 return FALSE;
15457}
15458
b886a2ab
RS
15459/* Read the instruction associated with RELOC from BUF. */
15460
15461static unsigned int
15462read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15463{
15464 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15465 return read_compressed_insn (buf, 4);
15466 else
15467 return read_insn (buf);
15468}
15469
15470/* Write instruction INSN to BUF, given that it has been relocated
15471 by RELOC. */
15472
15473static void
15474write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15475 unsigned long insn)
15476{
15477 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15478 write_compressed_insn (buf, insn, 4);
15479 else
15480 write_insn (buf, insn);
15481}
15482
9d862524
MR
15483/* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15484 to a symbol in another ISA mode, which cannot be converted to JALX. */
15485
15486static bfd_boolean
15487fix_bad_cross_mode_jump_p (fixS *fixP)
15488{
15489 unsigned long opcode;
15490 int other;
15491 char *buf;
15492
15493 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15494 return FALSE;
15495
15496 other = S_GET_OTHER (fixP->fx_addsy);
15497 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15498 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15499 switch (fixP->fx_r_type)
15500 {
15501 case BFD_RELOC_MIPS_JMP:
15502 return opcode != 0x1d && opcode != 0x03 && ELF_ST_IS_COMPRESSED (other);
15503 case BFD_RELOC_MICROMIPS_JMP:
15504 return opcode != 0x3c && opcode != 0x3d && !ELF_ST_IS_MICROMIPS (other);
15505 default:
15506 return FALSE;
15507 }
15508}
15509
15510/* Return TRUE if the instruction pointed to by FIXP is an invalid JALX
15511 jump to a symbol in the same ISA mode. */
15512
15513static bfd_boolean
15514fix_bad_same_mode_jalx_p (fixS *fixP)
15515{
15516 unsigned long opcode;
15517 int other;
15518 char *buf;
15519
15520 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15521 return FALSE;
15522
15523 other = S_GET_OTHER (fixP->fx_addsy);
15524 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15525 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15526 switch (fixP->fx_r_type)
15527 {
15528 case BFD_RELOC_MIPS_JMP:
15529 return opcode == 0x1d && !ELF_ST_IS_COMPRESSED (other);
15530 case BFD_RELOC_MIPS16_JMP:
15531 return opcode == 0x07 && ELF_ST_IS_COMPRESSED (other);
15532 case BFD_RELOC_MICROMIPS_JMP:
15533 return opcode == 0x3c && ELF_ST_IS_COMPRESSED (other);
15534 default:
15535 return FALSE;
15536 }
15537}
15538
15539/* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15540 to a symbol whose value plus addend is not aligned according to the
15541 ultimate (after linker relaxation) jump instruction's immediate field
15542 requirement, either to (1 << SHIFT), or, for jumps from microMIPS to
15543 regular MIPS code, to (1 << 2). */
15544
15545static bfd_boolean
15546fix_bad_misaligned_jump_p (fixS *fixP, int shift)
15547{
15548 bfd_boolean micro_to_mips_p;
15549 valueT val;
15550 int other;
15551
15552 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15553 return FALSE;
15554
15555 other = S_GET_OTHER (fixP->fx_addsy);
15556 val = S_GET_VALUE (fixP->fx_addsy) | ELF_ST_IS_COMPRESSED (other);
15557 val += fixP->fx_offset;
15558 micro_to_mips_p = (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15559 && !ELF_ST_IS_MICROMIPS (other));
15560 return ((val & ((1 << (micro_to_mips_p ? 2 : shift)) - 1))
15561 != ELF_ST_IS_COMPRESSED (other));
15562}
15563
15564/* Return TRUE if the instruction pointed to by FIXP is an invalid branch
15565 to a symbol whose annotation indicates another ISA mode. For absolute
a6ebf616
MR
15566 symbols check the ISA bit instead.
15567
15568 We accept BFD_RELOC_16_PCREL_S2 relocations against MIPS16 and microMIPS
15569 symbols or BFD_RELOC_MICROMIPS_16_PCREL_S1 relocations against regular
15570 MIPS symbols and associated with BAL instructions as these instructions
de194d85 15571 may be converted to JALX by the linker. */
9d862524
MR
15572
15573static bfd_boolean
15574fix_bad_cross_mode_branch_p (fixS *fixP)
15575{
15576 bfd_boolean absolute_p;
15577 unsigned long opcode;
15578 asection *symsec;
15579 valueT val;
15580 int other;
15581 char *buf;
15582
8b10b0b3
MR
15583 if (mips_ignore_branch_isa)
15584 return FALSE;
15585
9d862524
MR
15586 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15587 return FALSE;
15588
15589 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15590 absolute_p = bfd_is_abs_section (symsec);
15591
15592 val = S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset;
15593 other = S_GET_OTHER (fixP->fx_addsy);
15594
15595 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15596 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 16;
15597 switch (fixP->fx_r_type)
15598 {
15599 case BFD_RELOC_16_PCREL_S2:
a6ebf616
MR
15600 return ((absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other))
15601 && opcode != 0x0411);
15602 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15603 return ((absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other))
15604 && opcode != 0x4060);
9d862524
MR
15605 case BFD_RELOC_MIPS_21_PCREL_S2:
15606 case BFD_RELOC_MIPS_26_PCREL_S2:
15607 return absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other);
15608 case BFD_RELOC_MIPS16_16_PCREL_S1:
15609 return absolute_p ? !(val & 1) : !ELF_ST_IS_MIPS16 (other);
15610 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15611 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
9d862524
MR
15612 return absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other);
15613 default:
15614 abort ();
15615 }
15616}
15617
15618/* Return TRUE if the symbol plus addend associated with a regular MIPS
15619 branch instruction pointed to by FIXP is not aligned according to the
15620 branch instruction's immediate field requirement. We need the addend
15621 to preserve the ISA bit and also the sum must not have bit 2 set. We
15622 must explicitly OR in the ISA bit from symbol annotation as the bit
15623 won't be set in the symbol's value then. */
15624
15625static bfd_boolean
15626fix_bad_misaligned_branch_p (fixS *fixP)
15627{
15628 bfd_boolean absolute_p;
15629 asection *symsec;
15630 valueT isa_bit;
15631 valueT val;
15632 valueT off;
15633 int other;
15634
15635 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15636 return FALSE;
15637
15638 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15639 absolute_p = bfd_is_abs_section (symsec);
15640
15641 val = S_GET_VALUE (fixP->fx_addsy);
15642 other = S_GET_OTHER (fixP->fx_addsy);
15643 off = fixP->fx_offset;
15644
15645 isa_bit = absolute_p ? (val + off) & 1 : ELF_ST_IS_COMPRESSED (other);
15646 val |= ELF_ST_IS_COMPRESSED (other);
15647 val += off;
15648 return (val & 0x3) != isa_bit;
15649}
15650
15651/* Make the necessary checks on a regular MIPS branch pointed to by FIXP
15652 and its calculated value VAL. */
15653
15654static void
15655fix_validate_branch (fixS *fixP, valueT val)
15656{
15657 if (fixP->fx_done && (val & 0x3) != 0)
15658 as_bad_where (fixP->fx_file, fixP->fx_line,
15659 _("branch to misaligned address (0x%lx)"),
15660 (long) (val + md_pcrel_from (fixP)));
15661 else if (fix_bad_cross_mode_branch_p (fixP))
15662 as_bad_where (fixP->fx_file, fixP->fx_line,
15663 _("branch to a symbol in another ISA mode"));
15664 else if (fix_bad_misaligned_branch_p (fixP))
15665 as_bad_where (fixP->fx_file, fixP->fx_line,
15666 _("branch to misaligned address (0x%lx)"),
15667 (long) (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset));
15668 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x3) != 0)
15669 as_bad_where (fixP->fx_file, fixP->fx_line,
15670 _("cannot encode misaligned addend "
15671 "in the relocatable field (0x%lx)"),
15672 (long) fixP->fx_offset);
15673}
15674
252b5132
RH
15675/* Apply a fixup to the object file. */
15676
94f592af 15677void
55cf6793 15678md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 15679{
4d68580a 15680 char *buf;
b886a2ab 15681 unsigned long insn;
a7ebbfdf 15682 reloc_howto_type *howto;
252b5132 15683
d56a8dda
RS
15684 if (fixP->fx_pcrel)
15685 switch (fixP->fx_r_type)
15686 {
15687 case BFD_RELOC_16_PCREL_S2:
c9775dde 15688 case BFD_RELOC_MIPS16_16_PCREL_S1:
d56a8dda
RS
15689 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15690 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15691 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15692 case BFD_RELOC_32_PCREL:
7361da2c
AB
15693 case BFD_RELOC_MIPS_21_PCREL_S2:
15694 case BFD_RELOC_MIPS_26_PCREL_S2:
15695 case BFD_RELOC_MIPS_18_PCREL_S3:
15696 case BFD_RELOC_MIPS_19_PCREL_S2:
15697 case BFD_RELOC_HI16_S_PCREL:
15698 case BFD_RELOC_LO16_PCREL:
d56a8dda
RS
15699 break;
15700
15701 case BFD_RELOC_32:
15702 fixP->fx_r_type = BFD_RELOC_32_PCREL;
15703 break;
15704
15705 default:
15706 as_bad_where (fixP->fx_file, fixP->fx_line,
15707 _("PC-relative reference to a different section"));
15708 break;
15709 }
15710
15711 /* Handle BFD_RELOC_8, since it's easy. Punt on other bfd relocations
15712 that have no MIPS ELF equivalent. */
15713 if (fixP->fx_r_type != BFD_RELOC_8)
15714 {
15715 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15716 if (!howto)
15717 return;
15718 }
65551fa4 15719
df58fc94
RS
15720 gas_assert (fixP->fx_size == 2
15721 || fixP->fx_size == 4
d56a8dda 15722 || fixP->fx_r_type == BFD_RELOC_8
90ecf173
MR
15723 || fixP->fx_r_type == BFD_RELOC_16
15724 || fixP->fx_r_type == BFD_RELOC_64
15725 || fixP->fx_r_type == BFD_RELOC_CTOR
15726 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
df58fc94 15727 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
90ecf173
MR
15728 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15729 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
2f0c68f2
CM
15730 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64
15731 || fixP->fx_r_type == BFD_RELOC_NONE);
252b5132 15732
4d68580a 15733 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132 15734
b1dca8ee
RS
15735 /* Don't treat parts of a composite relocation as done. There are two
15736 reasons for this:
15737
15738 (1) The second and third parts will be against 0 (RSS_UNDEF) but
15739 should nevertheless be emitted if the first part is.
15740
15741 (2) In normal usage, composite relocations are never assembly-time
15742 constants. The easiest way of dealing with the pathological
15743 exceptions is to generate a relocation against STN_UNDEF and
15744 leave everything up to the linker. */
3994f87e 15745 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
252b5132
RH
15746 fixP->fx_done = 1;
15747
15748 switch (fixP->fx_r_type)
15749 {
3f98094e
DJ
15750 case BFD_RELOC_MIPS_TLS_GD:
15751 case BFD_RELOC_MIPS_TLS_LDM:
741d6ea8
JM
15752 case BFD_RELOC_MIPS_TLS_DTPREL32:
15753 case BFD_RELOC_MIPS_TLS_DTPREL64:
3f98094e
DJ
15754 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15755 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15756 case BFD_RELOC_MIPS_TLS_GOTTPREL:
d0f13682
CLT
15757 case BFD_RELOC_MIPS_TLS_TPREL32:
15758 case BFD_RELOC_MIPS_TLS_TPREL64:
3f98094e
DJ
15759 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15760 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
df58fc94
RS
15761 case BFD_RELOC_MICROMIPS_TLS_GD:
15762 case BFD_RELOC_MICROMIPS_TLS_LDM:
15763 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15764 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15765 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15766 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15767 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
d0f13682
CLT
15768 case BFD_RELOC_MIPS16_TLS_GD:
15769 case BFD_RELOC_MIPS16_TLS_LDM:
15770 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15771 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15772 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15773 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15774 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
4512dafa
MR
15775 if (fixP->fx_addsy)
15776 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15777 else
15778 as_bad_where (fixP->fx_file, fixP->fx_line,
15779 _("TLS relocation against a constant"));
15780 break;
3f98094e 15781
252b5132 15782 case BFD_RELOC_MIPS_JMP:
9d862524
MR
15783 case BFD_RELOC_MIPS16_JMP:
15784 case BFD_RELOC_MICROMIPS_JMP:
15785 {
15786 int shift;
15787
15788 gas_assert (!fixP->fx_done);
15789
15790 /* Shift is 2, unusually, for microMIPS JALX. */
15791 if (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15792 && (read_compressed_insn (buf, 4) >> 26) != 0x3c)
15793 shift = 1;
15794 else
15795 shift = 2;
15796
15797 if (fix_bad_cross_mode_jump_p (fixP))
15798 as_bad_where (fixP->fx_file, fixP->fx_line,
15799 _("jump to a symbol in another ISA mode"));
15800 else if (fix_bad_same_mode_jalx_p (fixP))
15801 as_bad_where (fixP->fx_file, fixP->fx_line,
15802 _("JALX to a symbol in the same ISA mode"));
15803 else if (fix_bad_misaligned_jump_p (fixP, shift))
15804 as_bad_where (fixP->fx_file, fixP->fx_line,
15805 _("jump to misaligned address (0x%lx)"),
15806 (long) (S_GET_VALUE (fixP->fx_addsy)
15807 + fixP->fx_offset));
15808 else if (HAVE_IN_PLACE_ADDENDS
15809 && (fixP->fx_offset & ((1 << shift) - 1)) != 0)
15810 as_bad_where (fixP->fx_file, fixP->fx_line,
15811 _("cannot encode misaligned addend "
15812 "in the relocatable field (0x%lx)"),
15813 (long) fixP->fx_offset);
15814 }
15815 /* Fall through. */
15816
e369bcce
TS
15817 case BFD_RELOC_MIPS_SHIFT5:
15818 case BFD_RELOC_MIPS_SHIFT6:
15819 case BFD_RELOC_MIPS_GOT_DISP:
15820 case BFD_RELOC_MIPS_GOT_PAGE:
15821 case BFD_RELOC_MIPS_GOT_OFST:
15822 case BFD_RELOC_MIPS_SUB:
15823 case BFD_RELOC_MIPS_INSERT_A:
15824 case BFD_RELOC_MIPS_INSERT_B:
15825 case BFD_RELOC_MIPS_DELETE:
15826 case BFD_RELOC_MIPS_HIGHEST:
15827 case BFD_RELOC_MIPS_HIGHER:
15828 case BFD_RELOC_MIPS_SCN_DISP:
15829 case BFD_RELOC_MIPS_REL16:
15830 case BFD_RELOC_MIPS_RELGOT:
15831 case BFD_RELOC_MIPS_JALR:
252b5132
RH
15832 case BFD_RELOC_HI16:
15833 case BFD_RELOC_HI16_S:
b886a2ab 15834 case BFD_RELOC_LO16:
cdf6fd85 15835 case BFD_RELOC_GPREL16:
252b5132
RH
15836 case BFD_RELOC_MIPS_LITERAL:
15837 case BFD_RELOC_MIPS_CALL16:
15838 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 15839 case BFD_RELOC_GPREL32:
252b5132
RH
15840 case BFD_RELOC_MIPS_GOT_HI16:
15841 case BFD_RELOC_MIPS_GOT_LO16:
15842 case BFD_RELOC_MIPS_CALL_HI16:
15843 case BFD_RELOC_MIPS_CALL_LO16:
41947d9e
MR
15844 case BFD_RELOC_HI16_S_PCREL:
15845 case BFD_RELOC_LO16_PCREL:
252b5132 15846 case BFD_RELOC_MIPS16_GPREL:
738e5348
RS
15847 case BFD_RELOC_MIPS16_GOT16:
15848 case BFD_RELOC_MIPS16_CALL16:
d6f16593
MR
15849 case BFD_RELOC_MIPS16_HI16:
15850 case BFD_RELOC_MIPS16_HI16_S:
b886a2ab 15851 case BFD_RELOC_MIPS16_LO16:
df58fc94
RS
15852 case BFD_RELOC_MICROMIPS_GOT_DISP:
15853 case BFD_RELOC_MICROMIPS_GOT_PAGE:
15854 case BFD_RELOC_MICROMIPS_GOT_OFST:
15855 case BFD_RELOC_MICROMIPS_SUB:
15856 case BFD_RELOC_MICROMIPS_HIGHEST:
15857 case BFD_RELOC_MICROMIPS_HIGHER:
15858 case BFD_RELOC_MICROMIPS_SCN_DISP:
15859 case BFD_RELOC_MICROMIPS_JALR:
15860 case BFD_RELOC_MICROMIPS_HI16:
15861 case BFD_RELOC_MICROMIPS_HI16_S:
b886a2ab 15862 case BFD_RELOC_MICROMIPS_LO16:
df58fc94
RS
15863 case BFD_RELOC_MICROMIPS_GPREL16:
15864 case BFD_RELOC_MICROMIPS_LITERAL:
15865 case BFD_RELOC_MICROMIPS_CALL16:
15866 case BFD_RELOC_MICROMIPS_GOT16:
15867 case BFD_RELOC_MICROMIPS_GOT_HI16:
15868 case BFD_RELOC_MICROMIPS_GOT_LO16:
15869 case BFD_RELOC_MICROMIPS_CALL_HI16:
15870 case BFD_RELOC_MICROMIPS_CALL_LO16:
067ec077 15871 case BFD_RELOC_MIPS_EH:
b886a2ab
RS
15872 if (fixP->fx_done)
15873 {
15874 offsetT value;
15875
15876 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
15877 {
15878 insn = read_reloc_insn (buf, fixP->fx_r_type);
15879 if (mips16_reloc_p (fixP->fx_r_type))
15880 insn |= mips16_immed_extend (value, 16);
15881 else
15882 insn |= (value & 0xffff);
15883 write_reloc_insn (buf, fixP->fx_r_type, insn);
15884 }
15885 else
15886 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 15887 _("unsupported constant in relocation"));
b886a2ab 15888 }
252b5132
RH
15889 break;
15890
252b5132
RH
15891 case BFD_RELOC_64:
15892 /* This is handled like BFD_RELOC_32, but we output a sign
15893 extended value if we are only 32 bits. */
3e722fb5 15894 if (fixP->fx_done)
252b5132
RH
15895 {
15896 if (8 <= sizeof (valueT))
4d68580a 15897 md_number_to_chars (buf, *valP, 8);
252b5132
RH
15898 else
15899 {
a7ebbfdf 15900 valueT hiv;
252b5132 15901
a7ebbfdf 15902 if ((*valP & 0x80000000) != 0)
252b5132
RH
15903 hiv = 0xffffffff;
15904 else
15905 hiv = 0;
4d68580a
RS
15906 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
15907 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
252b5132
RH
15908 }
15909 }
15910 break;
15911
056350c6 15912 case BFD_RELOC_RVA:
252b5132 15913 case BFD_RELOC_32:
b47468a6 15914 case BFD_RELOC_32_PCREL:
252b5132 15915 case BFD_RELOC_16:
d56a8dda 15916 case BFD_RELOC_8:
252b5132 15917 /* If we are deleting this reloc entry, we must fill in the
54f4ddb3
TS
15918 value now. This can happen if we have a .word which is not
15919 resolved when it appears but is later defined. */
252b5132 15920 if (fixP->fx_done)
4d68580a 15921 md_number_to_chars (buf, *valP, fixP->fx_size);
252b5132
RH
15922 break;
15923
7361da2c 15924 case BFD_RELOC_MIPS_21_PCREL_S2:
9d862524 15925 fix_validate_branch (fixP, *valP);
41947d9e
MR
15926 if (!fixP->fx_done)
15927 break;
15928
15929 if (*valP + 0x400000 <= 0x7fffff)
15930 {
15931 insn = read_insn (buf);
15932 insn |= (*valP >> 2) & 0x1fffff;
15933 write_insn (buf, insn);
15934 }
15935 else
15936 as_bad_where (fixP->fx_file, fixP->fx_line,
15937 _("branch out of range"));
15938 break;
15939
7361da2c 15940 case BFD_RELOC_MIPS_26_PCREL_S2:
9d862524 15941 fix_validate_branch (fixP, *valP);
41947d9e
MR
15942 if (!fixP->fx_done)
15943 break;
7361da2c 15944
41947d9e
MR
15945 if (*valP + 0x8000000 <= 0xfffffff)
15946 {
15947 insn = read_insn (buf);
15948 insn |= (*valP >> 2) & 0x3ffffff;
15949 write_insn (buf, insn);
15950 }
15951 else
15952 as_bad_where (fixP->fx_file, fixP->fx_line,
15953 _("branch out of range"));
7361da2c
AB
15954 break;
15955
15956 case BFD_RELOC_MIPS_18_PCREL_S3:
717ba204 15957 if (fixP->fx_addsy && (S_GET_VALUE (fixP->fx_addsy) & 0x7) != 0)
7361da2c 15958 as_bad_where (fixP->fx_file, fixP->fx_line,
0866e94c
MF
15959 _("PC-relative access using misaligned symbol (%lx)"),
15960 (long) S_GET_VALUE (fixP->fx_addsy));
15961 if ((fixP->fx_offset & 0x7) != 0)
15962 as_bad_where (fixP->fx_file, fixP->fx_line,
15963 _("PC-relative access using misaligned offset (%lx)"),
15964 (long) fixP->fx_offset);
41947d9e
MR
15965 if (!fixP->fx_done)
15966 break;
7361da2c 15967
41947d9e
MR
15968 if (*valP + 0x100000 <= 0x1fffff)
15969 {
15970 insn = read_insn (buf);
15971 insn |= (*valP >> 3) & 0x3ffff;
15972 write_insn (buf, insn);
15973 }
15974 else
15975 as_bad_where (fixP->fx_file, fixP->fx_line,
15976 _("PC-relative access out of range"));
7361da2c
AB
15977 break;
15978
15979 case BFD_RELOC_MIPS_19_PCREL_S2:
15980 if ((*valP & 0x3) != 0)
15981 as_bad_where (fixP->fx_file, fixP->fx_line,
15982 _("PC-relative access to misaligned address (%lx)"),
717ba204 15983 (long) *valP);
41947d9e
MR
15984 if (!fixP->fx_done)
15985 break;
7361da2c 15986
41947d9e
MR
15987 if (*valP + 0x100000 <= 0x1fffff)
15988 {
15989 insn = read_insn (buf);
15990 insn |= (*valP >> 2) & 0x7ffff;
15991 write_insn (buf, insn);
15992 }
15993 else
15994 as_bad_where (fixP->fx_file, fixP->fx_line,
15995 _("PC-relative access out of range"));
7361da2c
AB
15996 break;
15997
252b5132 15998 case BFD_RELOC_16_PCREL_S2:
9d862524 15999 fix_validate_branch (fixP, *valP);
cb56d3d3 16000
54f4ddb3
TS
16001 /* We need to save the bits in the instruction since fixup_segment()
16002 might be deleting the relocation entry (i.e., a branch within
16003 the current segment). */
a7ebbfdf 16004 if (! fixP->fx_done)
bb2d6cd7 16005 break;
252b5132 16006
54f4ddb3 16007 /* Update old instruction data. */
4d68580a 16008 insn = read_insn (buf);
252b5132 16009
a7ebbfdf
TS
16010 if (*valP + 0x20000 <= 0x3ffff)
16011 {
16012 insn |= (*valP >> 2) & 0xffff;
4d68580a 16013 write_insn (buf, insn);
a7ebbfdf 16014 }
ce8ad872 16015 else if (fixP->fx_tcbit2
a7ebbfdf
TS
16016 && fixP->fx_done
16017 && fixP->fx_frag->fr_address >= text_section->vma
16018 && (fixP->fx_frag->fr_address
587aac4e 16019 < text_section->vma + bfd_get_section_size (text_section))
a7ebbfdf
TS
16020 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
16021 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
16022 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
16023 {
16024 /* The branch offset is too large. If this is an
16025 unconditional branch, and we are not generating PIC code,
16026 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
16027 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
16028 insn = 0x0c000000; /* jal */
252b5132 16029 else
a7ebbfdf
TS
16030 insn = 0x08000000; /* j */
16031 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
16032 fixP->fx_done = 0;
16033 fixP->fx_addsy = section_symbol (text_section);
16034 *valP += md_pcrel_from (fixP);
4d68580a 16035 write_insn (buf, insn);
a7ebbfdf
TS
16036 }
16037 else
16038 {
16039 /* If we got here, we have branch-relaxation disabled,
16040 and there's nothing we can do to fix this instruction
16041 without turning it into a longer sequence. */
16042 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 16043 _("branch out of range"));
252b5132 16044 }
252b5132
RH
16045 break;
16046
c9775dde 16047 case BFD_RELOC_MIPS16_16_PCREL_S1:
df58fc94
RS
16048 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
16049 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
16050 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
96e9ba5f 16051 gas_assert (!fixP->fx_done);
9d862524
MR
16052 if (fix_bad_cross_mode_branch_p (fixP))
16053 as_bad_where (fixP->fx_file, fixP->fx_line,
16054 _("branch to a symbol in another ISA mode"));
16055 else if (fixP->fx_addsy
16056 && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
16057 && !bfd_is_abs_section (S_GET_SEGMENT (fixP->fx_addsy))
16058 && (fixP->fx_offset & 0x1) != 0)
16059 as_bad_where (fixP->fx_file, fixP->fx_line,
16060 _("branch to misaligned address (0x%lx)"),
16061 (long) (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset));
16062 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x1) != 0)
16063 as_bad_where (fixP->fx_file, fixP->fx_line,
16064 _("cannot encode misaligned addend "
16065 "in the relocatable field (0x%lx)"),
16066 (long) fixP->fx_offset);
df58fc94
RS
16067 break;
16068
252b5132
RH
16069 case BFD_RELOC_VTABLE_INHERIT:
16070 fixP->fx_done = 0;
16071 if (fixP->fx_addsy
16072 && !S_IS_DEFINED (fixP->fx_addsy)
16073 && !S_IS_WEAK (fixP->fx_addsy))
16074 S_SET_WEAK (fixP->fx_addsy);
16075 break;
16076
2f0c68f2 16077 case BFD_RELOC_NONE:
252b5132
RH
16078 case BFD_RELOC_VTABLE_ENTRY:
16079 fixP->fx_done = 0;
16080 break;
16081
16082 default:
b37df7c4 16083 abort ();
252b5132 16084 }
a7ebbfdf
TS
16085
16086 /* Remember value for tc_gen_reloc. */
16087 fixP->fx_addnumber = *valP;
252b5132
RH
16088}
16089
252b5132 16090static symbolS *
17a2f251 16091get_symbol (void)
252b5132
RH
16092{
16093 int c;
16094 char *name;
16095 symbolS *p;
16096
d02603dc 16097 c = get_symbol_name (&name);
252b5132 16098 p = (symbolS *) symbol_find_or_make (name);
d02603dc 16099 (void) restore_line_pointer (c);
252b5132
RH
16100 return p;
16101}
16102
742a56fe
RS
16103/* Align the current frag to a given power of two. If a particular
16104 fill byte should be used, FILL points to an integer that contains
16105 that byte, otherwise FILL is null.
16106
462427c4
RS
16107 This function used to have the comment:
16108
16109 The MIPS assembler also automatically adjusts any preceding label.
16110
16111 The implementation therefore applied the adjustment to a maximum of
16112 one label. However, other label adjustments are applied to batches
16113 of labels, and adjusting just one caused problems when new labels
16114 were added for the sake of debugging or unwind information.
16115 We therefore adjust all preceding labels (given as LABELS) instead. */
252b5132
RH
16116
16117static void
462427c4 16118mips_align (int to, int *fill, struct insn_label_list *labels)
252b5132 16119{
7d10b47d 16120 mips_emit_delays ();
df58fc94 16121 mips_record_compressed_mode ();
742a56fe
RS
16122 if (fill == NULL && subseg_text_p (now_seg))
16123 frag_align_code (to, 0);
16124 else
16125 frag_align (to, fill ? *fill : 0, 0);
252b5132 16126 record_alignment (now_seg, to);
462427c4 16127 mips_move_labels (labels, FALSE);
252b5132
RH
16128}
16129
16130/* Align to a given power of two. .align 0 turns off the automatic
16131 alignment used by the data creating pseudo-ops. */
16132
16133static void
17a2f251 16134s_align (int x ATTRIBUTE_UNUSED)
252b5132 16135{
742a56fe 16136 int temp, fill_value, *fill_ptr;
49954fb4 16137 long max_alignment = 28;
252b5132 16138
54f4ddb3 16139 /* o Note that the assembler pulls down any immediately preceding label
252b5132 16140 to the aligned address.
54f4ddb3 16141 o It's not documented but auto alignment is reinstated by
252b5132 16142 a .align pseudo instruction.
54f4ddb3 16143 o Note also that after auto alignment is turned off the mips assembler
252b5132 16144 issues an error on attempt to assemble an improperly aligned data item.
54f4ddb3 16145 We don't. */
252b5132
RH
16146
16147 temp = get_absolute_expression ();
16148 if (temp > max_alignment)
1661c76c 16149 as_bad (_("alignment too large, %d assumed"), temp = max_alignment);
252b5132
RH
16150 else if (temp < 0)
16151 {
1661c76c 16152 as_warn (_("alignment negative, 0 assumed"));
252b5132
RH
16153 temp = 0;
16154 }
16155 if (*input_line_pointer == ',')
16156 {
f9419b05 16157 ++input_line_pointer;
742a56fe
RS
16158 fill_value = get_absolute_expression ();
16159 fill_ptr = &fill_value;
252b5132
RH
16160 }
16161 else
742a56fe 16162 fill_ptr = 0;
252b5132
RH
16163 if (temp)
16164 {
a8dbcb85
TS
16165 segment_info_type *si = seg_info (now_seg);
16166 struct insn_label_list *l = si->label_list;
54f4ddb3 16167 /* Auto alignment should be switched on by next section change. */
252b5132 16168 auto_align = 1;
462427c4 16169 mips_align (temp, fill_ptr, l);
252b5132
RH
16170 }
16171 else
16172 {
16173 auto_align = 0;
16174 }
16175
16176 demand_empty_rest_of_line ();
16177}
16178
252b5132 16179static void
17a2f251 16180s_change_sec (int sec)
252b5132
RH
16181{
16182 segT seg;
16183
252b5132
RH
16184 /* The ELF backend needs to know that we are changing sections, so
16185 that .previous works correctly. We could do something like check
b6ff326e 16186 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
16187 as it would not be appropriate to use it in the section changing
16188 functions in read.c, since obj-elf.c intercepts those. FIXME:
16189 This should be cleaner, somehow. */
f3ded42a 16190 obj_elf_section_change_hook ();
252b5132 16191
7d10b47d 16192 mips_emit_delays ();
6a32d874 16193
252b5132
RH
16194 switch (sec)
16195 {
16196 case 't':
16197 s_text (0);
16198 break;
16199 case 'd':
16200 s_data (0);
16201 break;
16202 case 'b':
16203 subseg_set (bss_section, (subsegT) get_absolute_expression ());
16204 demand_empty_rest_of_line ();
16205 break;
16206
16207 case 'r':
4d0d148d
TS
16208 seg = subseg_new (RDATA_SECTION_NAME,
16209 (subsegT) get_absolute_expression ());
f3ded42a
RS
16210 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
16211 | SEC_READONLY | SEC_RELOC
16212 | SEC_DATA));
16213 if (strncmp (TARGET_OS, "elf", 3) != 0)
16214 record_alignment (seg, 4);
4d0d148d 16215 demand_empty_rest_of_line ();
252b5132
RH
16216 break;
16217
16218 case 's':
4d0d148d 16219 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
f3ded42a
RS
16220 bfd_set_section_flags (stdoutput, seg,
16221 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
16222 if (strncmp (TARGET_OS, "elf", 3) != 0)
16223 record_alignment (seg, 4);
4d0d148d
TS
16224 demand_empty_rest_of_line ();
16225 break;
998b3c36
MR
16226
16227 case 'B':
16228 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
f3ded42a
RS
16229 bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
16230 if (strncmp (TARGET_OS, "elf", 3) != 0)
16231 record_alignment (seg, 4);
998b3c36
MR
16232 demand_empty_rest_of_line ();
16233 break;
252b5132
RH
16234 }
16235
16236 auto_align = 1;
16237}
b34976b6 16238
cca86cc8 16239void
17a2f251 16240s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 16241{
d02603dc 16242 char *saved_ilp;
cca86cc8 16243 char *section_name;
d02603dc 16244 char c, endc;
684022ea 16245 char next_c = 0;
cca86cc8
SC
16246 int section_type;
16247 int section_flag;
16248 int section_entry_size;
16249 int section_alignment;
b34976b6 16250
d02603dc
NC
16251 saved_ilp = input_line_pointer;
16252 endc = get_symbol_name (&section_name);
16253 c = (endc == '"' ? input_line_pointer[1] : endc);
a816d1ed 16254 if (c)
d02603dc 16255 next_c = input_line_pointer [(endc == '"' ? 2 : 1)];
cca86cc8 16256
4cf0dd0d
TS
16257 /* Do we have .section Name<,"flags">? */
16258 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 16259 {
d02603dc
NC
16260 /* Just after name is now '\0'. */
16261 (void) restore_line_pointer (endc);
16262 input_line_pointer = saved_ilp;
cca86cc8
SC
16263 obj_elf_section (ignore);
16264 return;
16265 }
d02603dc
NC
16266
16267 section_name = xstrdup (section_name);
16268 c = restore_line_pointer (endc);
16269
cca86cc8
SC
16270 input_line_pointer++;
16271
16272 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
16273 if (c == ',')
16274 section_type = get_absolute_expression ();
16275 else
16276 section_type = 0;
d02603dc 16277
cca86cc8
SC
16278 if (*input_line_pointer++ == ',')
16279 section_flag = get_absolute_expression ();
16280 else
16281 section_flag = 0;
d02603dc 16282
cca86cc8
SC
16283 if (*input_line_pointer++ == ',')
16284 section_entry_size = get_absolute_expression ();
16285 else
16286 section_entry_size = 0;
d02603dc 16287
cca86cc8
SC
16288 if (*input_line_pointer++ == ',')
16289 section_alignment = get_absolute_expression ();
16290 else
16291 section_alignment = 0;
d02603dc 16292
87975d2a
AM
16293 /* FIXME: really ignore? */
16294 (void) section_alignment;
cca86cc8 16295
8ab8a5c8
RS
16296 /* When using the generic form of .section (as implemented by obj-elf.c),
16297 there's no way to set the section type to SHT_MIPS_DWARF. Users have
16298 traditionally had to fall back on the more common @progbits instead.
16299
16300 There's nothing really harmful in this, since bfd will correct
16301 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
708587a4 16302 means that, for backwards compatibility, the special_section entries
8ab8a5c8
RS
16303 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
16304
16305 Even so, we shouldn't force users of the MIPS .section syntax to
16306 incorrectly label the sections as SHT_PROGBITS. The best compromise
16307 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
16308 generic type-checking code. */
16309 if (section_type == SHT_MIPS_DWARF)
16310 section_type = SHT_PROGBITS;
16311
a91e1603 16312 obj_elf_change_section (section_name, section_type, 0, section_flag,
cca86cc8 16313 section_entry_size, 0, 0, 0);
a816d1ed
AO
16314
16315 if (now_seg->name != section_name)
16316 free (section_name);
cca86cc8 16317}
252b5132
RH
16318
16319void
17a2f251 16320mips_enable_auto_align (void)
252b5132
RH
16321{
16322 auto_align = 1;
16323}
16324
16325static void
17a2f251 16326s_cons (int log_size)
252b5132 16327{
a8dbcb85
TS
16328 segment_info_type *si = seg_info (now_seg);
16329 struct insn_label_list *l = si->label_list;
252b5132 16330
7d10b47d 16331 mips_emit_delays ();
252b5132 16332 if (log_size > 0 && auto_align)
462427c4 16333 mips_align (log_size, 0, l);
252b5132 16334 cons (1 << log_size);
a1facbec 16335 mips_clear_insn_labels ();
252b5132
RH
16336}
16337
16338static void
17a2f251 16339s_float_cons (int type)
252b5132 16340{
a8dbcb85
TS
16341 segment_info_type *si = seg_info (now_seg);
16342 struct insn_label_list *l = si->label_list;
252b5132 16343
7d10b47d 16344 mips_emit_delays ();
252b5132
RH
16345
16346 if (auto_align)
49309057
ILT
16347 {
16348 if (type == 'd')
462427c4 16349 mips_align (3, 0, l);
49309057 16350 else
462427c4 16351 mips_align (2, 0, l);
49309057 16352 }
252b5132 16353
252b5132 16354 float_cons (type);
a1facbec 16355 mips_clear_insn_labels ();
252b5132
RH
16356}
16357
16358/* Handle .globl. We need to override it because on Irix 5 you are
16359 permitted to say
16360 .globl foo .text
16361 where foo is an undefined symbol, to mean that foo should be
16362 considered to be the address of a function. */
16363
16364static void
17a2f251 16365s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
16366{
16367 char *name;
16368 int c;
16369 symbolS *symbolP;
16370 flagword flag;
16371
8a06b769 16372 do
252b5132 16373 {
d02603dc 16374 c = get_symbol_name (&name);
8a06b769
TS
16375 symbolP = symbol_find_or_make (name);
16376 S_SET_EXTERNAL (symbolP);
16377
252b5132 16378 *input_line_pointer = c;
d02603dc 16379 SKIP_WHITESPACE_AFTER_NAME ();
252b5132 16380
8a06b769
TS
16381 /* On Irix 5, every global symbol that is not explicitly labelled as
16382 being a function is apparently labelled as being an object. */
16383 flag = BSF_OBJECT;
252b5132 16384
8a06b769
TS
16385 if (!is_end_of_line[(unsigned char) *input_line_pointer]
16386 && (*input_line_pointer != ','))
16387 {
16388 char *secname;
16389 asection *sec;
16390
d02603dc 16391 c = get_symbol_name (&secname);
8a06b769
TS
16392 sec = bfd_get_section_by_name (stdoutput, secname);
16393 if (sec == NULL)
16394 as_bad (_("%s: no such section"), secname);
d02603dc 16395 (void) restore_line_pointer (c);
8a06b769
TS
16396
16397 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
16398 flag = BSF_FUNCTION;
16399 }
16400
16401 symbol_get_bfdsym (symbolP)->flags |= flag;
16402
16403 c = *input_line_pointer;
16404 if (c == ',')
16405 {
16406 input_line_pointer++;
16407 SKIP_WHITESPACE ();
16408 if (is_end_of_line[(unsigned char) *input_line_pointer])
16409 c = '\n';
16410 }
16411 }
16412 while (c == ',');
252b5132 16413
252b5132
RH
16414 demand_empty_rest_of_line ();
16415}
16416
16417static void
17a2f251 16418s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
16419{
16420 char *opt;
16421 char c;
16422
d02603dc 16423 c = get_symbol_name (&opt);
252b5132
RH
16424
16425 if (*opt == 'O')
16426 {
16427 /* FIXME: What does this mean? */
16428 }
41a1578e 16429 else if (strncmp (opt, "pic", 3) == 0 && ISDIGIT (opt[3]) && opt[4] == '\0')
252b5132
RH
16430 {
16431 int i;
16432
16433 i = atoi (opt + 3);
668c5ebc
MR
16434 if (i != 0 && i != 2)
16435 as_bad (_(".option pic%d not supported"), i);
16436 else if (mips_pic == VXWORKS_PIC)
16437 as_bad (_(".option pic%d not supported in VxWorks PIC mode"), i);
16438 else if (i == 0)
252b5132
RH
16439 mips_pic = NO_PIC;
16440 else if (i == 2)
143d77c5 16441 {
8b828383 16442 mips_pic = SVR4_PIC;
143d77c5
EC
16443 mips_abicalls = TRUE;
16444 }
252b5132 16445
4d0d148d 16446 if (mips_pic == SVR4_PIC)
252b5132
RH
16447 {
16448 if (g_switch_seen && g_switch_value != 0)
16449 as_warn (_("-G may not be used with SVR4 PIC code"));
16450 g_switch_value = 0;
16451 bfd_set_gp_size (stdoutput, 0);
16452 }
16453 }
16454 else
1661c76c 16455 as_warn (_("unrecognized option \"%s\""), opt);
252b5132 16456
d02603dc 16457 (void) restore_line_pointer (c);
252b5132
RH
16458 demand_empty_rest_of_line ();
16459}
16460
16461/* This structure is used to hold a stack of .set values. */
16462
e972090a
NC
16463struct mips_option_stack
16464{
252b5132
RH
16465 struct mips_option_stack *next;
16466 struct mips_set_options options;
16467};
16468
16469static struct mips_option_stack *mips_opts_stack;
16470
22522f88
MR
16471/* Return status for .set/.module option handling. */
16472
16473enum code_option_type
16474{
16475 /* Unrecognized option. */
16476 OPTION_TYPE_BAD = -1,
16477
16478 /* Ordinary option. */
16479 OPTION_TYPE_NORMAL,
16480
16481 /* ISA changing option. */
16482 OPTION_TYPE_ISA
16483};
16484
16485/* Handle common .set/.module options. Return status indicating option
16486 type. */
16487
16488static enum code_option_type
919731af 16489parse_code_option (char * name)
252b5132 16490{
22522f88 16491 bfd_boolean isa_set = FALSE;
c6278170 16492 const struct mips_ase *ase;
22522f88 16493
919731af 16494 if (strncmp (name, "at=", 3) == 0)
741fe287
MR
16495 {
16496 char *s = name + 3;
16497
16498 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
1661c76c 16499 as_bad (_("unrecognized register name `%s'"), s);
741fe287 16500 }
252b5132 16501 else if (strcmp (name, "at") == 0)
919731af 16502 mips_opts.at = ATREG;
252b5132 16503 else if (strcmp (name, "noat") == 0)
919731af 16504 mips_opts.at = ZERO;
252b5132 16505 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
919731af 16506 mips_opts.nomove = 0;
252b5132 16507 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
919731af 16508 mips_opts.nomove = 1;
252b5132 16509 else if (strcmp (name, "bopt") == 0)
919731af 16510 mips_opts.nobopt = 0;
252b5132 16511 else if (strcmp (name, "nobopt") == 0)
919731af 16512 mips_opts.nobopt = 1;
ad3fea08 16513 else if (strcmp (name, "gp=32") == 0)
bad1aba3 16514 mips_opts.gp = 32;
ad3fea08 16515 else if (strcmp (name, "gp=64") == 0)
919731af 16516 mips_opts.gp = 64;
ad3fea08 16517 else if (strcmp (name, "fp=32") == 0)
0b35dfee 16518 mips_opts.fp = 32;
351cdf24
MF
16519 else if (strcmp (name, "fp=xx") == 0)
16520 mips_opts.fp = 0;
ad3fea08 16521 else if (strcmp (name, "fp=64") == 0)
919731af 16522 mips_opts.fp = 64;
037b32b9
AN
16523 else if (strcmp (name, "softfloat") == 0)
16524 mips_opts.soft_float = 1;
16525 else if (strcmp (name, "hardfloat") == 0)
16526 mips_opts.soft_float = 0;
16527 else if (strcmp (name, "singlefloat") == 0)
16528 mips_opts.single_float = 1;
16529 else if (strcmp (name, "doublefloat") == 0)
16530 mips_opts.single_float = 0;
351cdf24
MF
16531 else if (strcmp (name, "nooddspreg") == 0)
16532 mips_opts.oddspreg = 0;
16533 else if (strcmp (name, "oddspreg") == 0)
16534 mips_opts.oddspreg = 1;
252b5132
RH
16535 else if (strcmp (name, "mips16") == 0
16536 || strcmp (name, "MIPS-16") == 0)
919731af 16537 mips_opts.mips16 = 1;
252b5132
RH
16538 else if (strcmp (name, "nomips16") == 0
16539 || strcmp (name, "noMIPS-16") == 0)
16540 mips_opts.mips16 = 0;
df58fc94 16541 else if (strcmp (name, "micromips") == 0)
919731af 16542 mips_opts.micromips = 1;
df58fc94
RS
16543 else if (strcmp (name, "nomicromips") == 0)
16544 mips_opts.micromips = 0;
c6278170
RS
16545 else if (name[0] == 'n'
16546 && name[1] == 'o'
16547 && (ase = mips_lookup_ase (name + 2)))
919731af 16548 mips_set_ase (ase, &mips_opts, FALSE);
c6278170 16549 else if ((ase = mips_lookup_ase (name)))
919731af 16550 mips_set_ase (ase, &mips_opts, TRUE);
1a2c1fad 16551 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 16552 {
1a2c1fad
CD
16553 /* Permit the user to change the ISA and architecture on the fly.
16554 Needless to say, misuse can cause serious problems. */
919731af 16555 if (strncmp (name, "arch=", 5) == 0)
1a2c1fad
CD
16556 {
16557 const struct mips_cpu_info *p;
16558
919731af 16559 p = mips_parse_cpu ("internal use", name + 5);
1a2c1fad
CD
16560 if (!p)
16561 as_bad (_("unknown architecture %s"), name + 5);
16562 else
16563 {
16564 mips_opts.arch = p->cpu;
16565 mips_opts.isa = p->isa;
22522f88 16566 isa_set = TRUE;
3315614d 16567 mips_opts.init_ase = p->ase;
1a2c1fad
CD
16568 }
16569 }
81a21e38
TS
16570 else if (strncmp (name, "mips", 4) == 0)
16571 {
16572 const struct mips_cpu_info *p;
16573
919731af 16574 p = mips_parse_cpu ("internal use", name);
81a21e38
TS
16575 if (!p)
16576 as_bad (_("unknown ISA level %s"), name + 4);
16577 else
16578 {
16579 mips_opts.arch = p->cpu;
16580 mips_opts.isa = p->isa;
22522f88 16581 isa_set = TRUE;
3315614d 16582 mips_opts.init_ase = p->ase;
81a21e38
TS
16583 }
16584 }
af7ee8bf 16585 else
81a21e38 16586 as_bad (_("unknown ISA or architecture %s"), name);
252b5132
RH
16587 }
16588 else if (strcmp (name, "autoextend") == 0)
16589 mips_opts.noautoextend = 0;
16590 else if (strcmp (name, "noautoextend") == 0)
16591 mips_opts.noautoextend = 1;
833794fc
MR
16592 else if (strcmp (name, "insn32") == 0)
16593 mips_opts.insn32 = TRUE;
16594 else if (strcmp (name, "noinsn32") == 0)
16595 mips_opts.insn32 = FALSE;
919731af 16596 else if (strcmp (name, "sym32") == 0)
16597 mips_opts.sym32 = TRUE;
16598 else if (strcmp (name, "nosym32") == 0)
16599 mips_opts.sym32 = FALSE;
16600 else
22522f88
MR
16601 return OPTION_TYPE_BAD;
16602
16603 return isa_set ? OPTION_TYPE_ISA : OPTION_TYPE_NORMAL;
919731af 16604}
16605
16606/* Handle the .set pseudo-op. */
16607
16608static void
16609s_mipsset (int x ATTRIBUTE_UNUSED)
16610{
22522f88 16611 enum code_option_type type = OPTION_TYPE_NORMAL;
919731af 16612 char *name = input_line_pointer, ch;
919731af 16613
16614 file_mips_check_options ();
16615
16616 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16617 ++input_line_pointer;
16618 ch = *input_line_pointer;
16619 *input_line_pointer = '\0';
16620
16621 if (strchr (name, ','))
16622 {
16623 /* Generic ".set" directive; use the generic handler. */
16624 *input_line_pointer = ch;
16625 input_line_pointer = name;
16626 s_set (0);
16627 return;
16628 }
16629
16630 if (strcmp (name, "reorder") == 0)
16631 {
16632 if (mips_opts.noreorder)
16633 end_noreorder ();
16634 }
16635 else if (strcmp (name, "noreorder") == 0)
16636 {
16637 if (!mips_opts.noreorder)
16638 start_noreorder ();
16639 }
16640 else if (strcmp (name, "macro") == 0)
16641 mips_opts.warn_about_macros = 0;
16642 else if (strcmp (name, "nomacro") == 0)
16643 {
16644 if (mips_opts.noreorder == 0)
16645 as_bad (_("`noreorder' must be set before `nomacro'"));
16646 mips_opts.warn_about_macros = 1;
16647 }
16648 else if (strcmp (name, "gp=default") == 0)
16649 mips_opts.gp = file_mips_opts.gp;
16650 else if (strcmp (name, "fp=default") == 0)
16651 mips_opts.fp = file_mips_opts.fp;
16652 else if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
16653 {
16654 mips_opts.isa = file_mips_opts.isa;
16655 mips_opts.arch = file_mips_opts.arch;
3315614d 16656 mips_opts.init_ase = file_mips_opts.init_ase;
919731af 16657 mips_opts.gp = file_mips_opts.gp;
16658 mips_opts.fp = file_mips_opts.fp;
16659 }
252b5132
RH
16660 else if (strcmp (name, "push") == 0)
16661 {
16662 struct mips_option_stack *s;
16663
325801bd 16664 s = XNEW (struct mips_option_stack);
252b5132
RH
16665 s->next = mips_opts_stack;
16666 s->options = mips_opts;
16667 mips_opts_stack = s;
16668 }
16669 else if (strcmp (name, "pop") == 0)
16670 {
16671 struct mips_option_stack *s;
16672
16673 s = mips_opts_stack;
16674 if (s == NULL)
16675 as_bad (_(".set pop with no .set push"));
16676 else
16677 {
16678 /* If we're changing the reorder mode we need to handle
16679 delay slots correctly. */
16680 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 16681 start_noreorder ();
252b5132 16682 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 16683 end_noreorder ();
252b5132
RH
16684
16685 mips_opts = s->options;
16686 mips_opts_stack = s->next;
16687 free (s);
16688 }
16689 }
22522f88
MR
16690 else
16691 {
16692 type = parse_code_option (name);
16693 if (type == OPTION_TYPE_BAD)
16694 as_warn (_("tried to set unrecognized symbol: %s\n"), name);
16695 }
919731af 16696
16697 /* The use of .set [arch|cpu]= historically 'fixes' the width of gp and fp
16698 registers based on what is supported by the arch/cpu. */
22522f88 16699 if (type == OPTION_TYPE_ISA)
e6559e01 16700 {
919731af 16701 switch (mips_opts.isa)
16702 {
16703 case 0:
16704 break;
16705 case ISA_MIPS1:
351cdf24
MF
16706 /* MIPS I cannot support FPXX. */
16707 mips_opts.fp = 32;
16708 /* fall-through. */
919731af 16709 case ISA_MIPS2:
16710 case ISA_MIPS32:
16711 case ISA_MIPS32R2:
16712 case ISA_MIPS32R3:
16713 case ISA_MIPS32R5:
16714 mips_opts.gp = 32;
351cdf24
MF
16715 if (mips_opts.fp != 0)
16716 mips_opts.fp = 32;
919731af 16717 break;
7361da2c
AB
16718 case ISA_MIPS32R6:
16719 mips_opts.gp = 32;
16720 mips_opts.fp = 64;
16721 break;
919731af 16722 case ISA_MIPS3:
16723 case ISA_MIPS4:
16724 case ISA_MIPS5:
16725 case ISA_MIPS64:
16726 case ISA_MIPS64R2:
16727 case ISA_MIPS64R3:
16728 case ISA_MIPS64R5:
7361da2c 16729 case ISA_MIPS64R6:
919731af 16730 mips_opts.gp = 64;
351cdf24
MF
16731 if (mips_opts.fp != 0)
16732 {
16733 if (mips_opts.arch == CPU_R5900)
16734 mips_opts.fp = 32;
16735 else
16736 mips_opts.fp = 64;
16737 }
919731af 16738 break;
16739 default:
16740 as_bad (_("unknown ISA level %s"), name + 4);
16741 break;
16742 }
e6559e01 16743 }
919731af 16744
16745 mips_check_options (&mips_opts, FALSE);
16746
16747 mips_check_isa_supports_ases ();
16748 *input_line_pointer = ch;
16749 demand_empty_rest_of_line ();
16750}
16751
16752/* Handle the .module pseudo-op. */
16753
16754static void
16755s_module (int ignore ATTRIBUTE_UNUSED)
16756{
16757 char *name = input_line_pointer, ch;
16758
16759 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16760 ++input_line_pointer;
16761 ch = *input_line_pointer;
16762 *input_line_pointer = '\0';
16763
16764 if (!file_mips_opts_checked)
252b5132 16765 {
22522f88 16766 if (parse_code_option (name) == OPTION_TYPE_BAD)
919731af 16767 as_bad (_(".module used with unrecognized symbol: %s\n"), name);
16768
16769 /* Update module level settings from mips_opts. */
16770 file_mips_opts = mips_opts;
252b5132 16771 }
919731af 16772 else
16773 as_bad (_(".module is not permitted after generating code"));
16774
252b5132
RH
16775 *input_line_pointer = ch;
16776 demand_empty_rest_of_line ();
16777}
16778
16779/* Handle the .abicalls pseudo-op. I believe this is equivalent to
16780 .option pic2. It means to generate SVR4 PIC calls. */
16781
16782static void
17a2f251 16783s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16784{
16785 mips_pic = SVR4_PIC;
143d77c5 16786 mips_abicalls = TRUE;
4d0d148d
TS
16787
16788 if (g_switch_seen && g_switch_value != 0)
16789 as_warn (_("-G may not be used with SVR4 PIC code"));
16790 g_switch_value = 0;
16791
252b5132
RH
16792 bfd_set_gp_size (stdoutput, 0);
16793 demand_empty_rest_of_line ();
16794}
16795
16796/* Handle the .cpload pseudo-op. This is used when generating SVR4
16797 PIC code. It sets the $gp register for the function based on the
16798 function address, which is in the register named in the argument.
16799 This uses a relocation against _gp_disp, which is handled specially
16800 by the linker. The result is:
16801 lui $gp,%hi(_gp_disp)
16802 addiu $gp,$gp,%lo(_gp_disp)
16803 addu $gp,$gp,.cpload argument
aa6975fb
ILT
16804 The .cpload argument is normally $25 == $t9.
16805
16806 The -mno-shared option changes this to:
bbe506e8
TS
16807 lui $gp,%hi(__gnu_local_gp)
16808 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
16809 and the argument is ignored. This saves an instruction, but the
16810 resulting code is not position independent; it uses an absolute
bbe506e8
TS
16811 address for __gnu_local_gp. Thus code assembled with -mno-shared
16812 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
16813
16814static void
17a2f251 16815s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16816{
16817 expressionS ex;
aa6975fb
ILT
16818 int reg;
16819 int in_shared;
252b5132 16820
919731af 16821 file_mips_check_options ();
16822
6478892d
TS
16823 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16824 .cpload is ignored. */
16825 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16826 {
16827 s_ignore (0);
16828 return;
16829 }
16830
a276b80c
MR
16831 if (mips_opts.mips16)
16832 {
16833 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16834 ignore_rest_of_line ();
16835 return;
16836 }
16837
d3ecfc59 16838 /* .cpload should be in a .set noreorder section. */
252b5132
RH
16839 if (mips_opts.noreorder == 0)
16840 as_warn (_(".cpload not in noreorder section"));
16841
aa6975fb
ILT
16842 reg = tc_get_register (0);
16843
16844 /* If we need to produce a 64-bit address, we are better off using
16845 the default instruction sequence. */
aed1a261 16846 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 16847
252b5132 16848 ex.X_op = O_symbol;
bbe506e8
TS
16849 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16850 "__gnu_local_gp");
252b5132
RH
16851 ex.X_op_symbol = NULL;
16852 ex.X_add_number = 0;
16853
16854 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 16855 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 16856
8a75745d
MR
16857 mips_mark_labels ();
16858 mips_assembling_insn = TRUE;
16859
584892a6 16860 macro_start ();
67c0d1eb
RS
16861 macro_build_lui (&ex, mips_gp_register);
16862 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 16863 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
16864 if (in_shared)
16865 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16866 mips_gp_register, reg);
584892a6 16867 macro_end ();
252b5132 16868
8a75745d 16869 mips_assembling_insn = FALSE;
252b5132
RH
16870 demand_empty_rest_of_line ();
16871}
16872
6478892d
TS
16873/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
16874 .cpsetup $reg1, offset|$reg2, label
16875
16876 If offset is given, this results in:
16877 sd $gp, offset($sp)
956cd1d6 16878 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
16879 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16880 daddu $gp, $gp, $reg1
6478892d
TS
16881
16882 If $reg2 is given, this results in:
40fc1451 16883 or $reg2, $gp, $0
956cd1d6 16884 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
16885 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16886 daddu $gp, $gp, $reg1
aa6975fb
ILT
16887 $reg1 is normally $25 == $t9.
16888
16889 The -mno-shared option replaces the last three instructions with
16890 lui $gp,%hi(_gp)
54f4ddb3 16891 addiu $gp,$gp,%lo(_gp) */
aa6975fb 16892
6478892d 16893static void
17a2f251 16894s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16895{
16896 expressionS ex_off;
16897 expressionS ex_sym;
16898 int reg1;
6478892d 16899
919731af 16900 file_mips_check_options ();
16901
8586fc66 16902 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
16903 We also need NewABI support. */
16904 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16905 {
16906 s_ignore (0);
16907 return;
16908 }
16909
a276b80c
MR
16910 if (mips_opts.mips16)
16911 {
16912 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
16913 ignore_rest_of_line ();
16914 return;
16915 }
16916
6478892d
TS
16917 reg1 = tc_get_register (0);
16918 SKIP_WHITESPACE ();
16919 if (*input_line_pointer != ',')
16920 {
16921 as_bad (_("missing argument separator ',' for .cpsetup"));
16922 return;
16923 }
16924 else
80245285 16925 ++input_line_pointer;
6478892d
TS
16926 SKIP_WHITESPACE ();
16927 if (*input_line_pointer == '$')
80245285
TS
16928 {
16929 mips_cpreturn_register = tc_get_register (0);
16930 mips_cpreturn_offset = -1;
16931 }
6478892d 16932 else
80245285
TS
16933 {
16934 mips_cpreturn_offset = get_absolute_expression ();
16935 mips_cpreturn_register = -1;
16936 }
6478892d
TS
16937 SKIP_WHITESPACE ();
16938 if (*input_line_pointer != ',')
16939 {
16940 as_bad (_("missing argument separator ',' for .cpsetup"));
16941 return;
16942 }
16943 else
f9419b05 16944 ++input_line_pointer;
6478892d 16945 SKIP_WHITESPACE ();
f21f8242 16946 expression (&ex_sym);
6478892d 16947
8a75745d
MR
16948 mips_mark_labels ();
16949 mips_assembling_insn = TRUE;
16950
584892a6 16951 macro_start ();
6478892d
TS
16952 if (mips_cpreturn_register == -1)
16953 {
16954 ex_off.X_op = O_constant;
16955 ex_off.X_add_symbol = NULL;
16956 ex_off.X_op_symbol = NULL;
16957 ex_off.X_add_number = mips_cpreturn_offset;
16958
67c0d1eb 16959 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 16960 BFD_RELOC_LO16, SP);
6478892d
TS
16961 }
16962 else
40fc1451 16963 move_register (mips_cpreturn_register, mips_gp_register);
6478892d 16964
aed1a261 16965 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb 16966 {
df58fc94 16967 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
aa6975fb
ILT
16968 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
16969 BFD_RELOC_HI16_S);
16970
16971 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
16972 mips_gp_register, -1, BFD_RELOC_GPREL16,
16973 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
16974
16975 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
16976 mips_gp_register, reg1);
16977 }
16978 else
16979 {
16980 expressionS ex;
16981
16982 ex.X_op = O_symbol;
4184909a 16983 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
16984 ex.X_op_symbol = NULL;
16985 ex.X_add_number = 0;
6e1304d8 16986
aa6975fb
ILT
16987 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16988 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16989
16990 macro_build_lui (&ex, mips_gp_register);
16991 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16992 mips_gp_register, BFD_RELOC_LO16);
16993 }
f21f8242 16994
584892a6 16995 macro_end ();
6478892d 16996
8a75745d 16997 mips_assembling_insn = FALSE;
6478892d
TS
16998 demand_empty_rest_of_line ();
16999}
17000
17001static void
17a2f251 17002s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d 17003{
919731af 17004 file_mips_check_options ();
17005
6478892d 17006 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
54f4ddb3 17007 .cplocal is ignored. */
6478892d
TS
17008 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17009 {
17010 s_ignore (0);
17011 return;
17012 }
17013
a276b80c
MR
17014 if (mips_opts.mips16)
17015 {
17016 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
17017 ignore_rest_of_line ();
17018 return;
17019 }
17020
6478892d 17021 mips_gp_register = tc_get_register (0);
85b51719 17022 demand_empty_rest_of_line ();
6478892d
TS
17023}
17024
252b5132
RH
17025/* Handle the .cprestore pseudo-op. This stores $gp into a given
17026 offset from $sp. The offset is remembered, and after making a PIC
17027 call $gp is restored from that location. */
17028
17029static void
17a2f251 17030s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
17031{
17032 expressionS ex;
252b5132 17033
919731af 17034 file_mips_check_options ();
17035
6478892d 17036 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 17037 .cprestore is ignored. */
6478892d 17038 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
17039 {
17040 s_ignore (0);
17041 return;
17042 }
17043
a276b80c
MR
17044 if (mips_opts.mips16)
17045 {
17046 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
17047 ignore_rest_of_line ();
17048 return;
17049 }
17050
252b5132 17051 mips_cprestore_offset = get_absolute_expression ();
7a621144 17052 mips_cprestore_valid = 1;
252b5132
RH
17053
17054 ex.X_op = O_constant;
17055 ex.X_add_symbol = NULL;
17056 ex.X_op_symbol = NULL;
17057 ex.X_add_number = mips_cprestore_offset;
17058
8a75745d
MR
17059 mips_mark_labels ();
17060 mips_assembling_insn = TRUE;
17061
584892a6 17062 macro_start ();
67c0d1eb
RS
17063 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
17064 SP, HAVE_64BIT_ADDRESSES);
584892a6 17065 macro_end ();
252b5132 17066
8a75745d 17067 mips_assembling_insn = FALSE;
252b5132
RH
17068 demand_empty_rest_of_line ();
17069}
17070
6478892d 17071/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 17072 was given in the preceding .cpsetup, it results in:
6478892d 17073 ld $gp, offset($sp)
76b3015f 17074
6478892d 17075 If a register $reg2 was given there, it results in:
40fc1451 17076 or $gp, $reg2, $0 */
54f4ddb3 17077
6478892d 17078static void
17a2f251 17079s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
17080{
17081 expressionS ex;
6478892d 17082
919731af 17083 file_mips_check_options ();
17084
6478892d
TS
17085 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
17086 We also need NewABI support. */
17087 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17088 {
17089 s_ignore (0);
17090 return;
17091 }
17092
a276b80c
MR
17093 if (mips_opts.mips16)
17094 {
17095 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
17096 ignore_rest_of_line ();
17097 return;
17098 }
17099
8a75745d
MR
17100 mips_mark_labels ();
17101 mips_assembling_insn = TRUE;
17102
584892a6 17103 macro_start ();
6478892d
TS
17104 if (mips_cpreturn_register == -1)
17105 {
17106 ex.X_op = O_constant;
17107 ex.X_add_symbol = NULL;
17108 ex.X_op_symbol = NULL;
17109 ex.X_add_number = mips_cpreturn_offset;
17110
67c0d1eb 17111 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
17112 }
17113 else
40fc1451
SD
17114 move_register (mips_gp_register, mips_cpreturn_register);
17115
584892a6 17116 macro_end ();
6478892d 17117
8a75745d 17118 mips_assembling_insn = FALSE;
6478892d
TS
17119 demand_empty_rest_of_line ();
17120}
17121
d0f13682
CLT
17122/* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
17123 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
17124 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
17125 debug information or MIPS16 TLS. */
741d6ea8
JM
17126
17127static void
d0f13682
CLT
17128s_tls_rel_directive (const size_t bytes, const char *dirstr,
17129 bfd_reloc_code_real_type rtype)
741d6ea8
JM
17130{
17131 expressionS ex;
17132 char *p;
17133
17134 expression (&ex);
17135
17136 if (ex.X_op != O_symbol)
17137 {
1661c76c 17138 as_bad (_("unsupported use of %s"), dirstr);
741d6ea8
JM
17139 ignore_rest_of_line ();
17140 }
17141
17142 p = frag_more (bytes);
17143 md_number_to_chars (p, 0, bytes);
d0f13682 17144 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
741d6ea8 17145 demand_empty_rest_of_line ();
de64cffd 17146 mips_clear_insn_labels ();
741d6ea8
JM
17147}
17148
17149/* Handle .dtprelword. */
17150
17151static void
17152s_dtprelword (int ignore ATTRIBUTE_UNUSED)
17153{
d0f13682 17154 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
741d6ea8
JM
17155}
17156
17157/* Handle .dtpreldword. */
17158
17159static void
17160s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
17161{
d0f13682
CLT
17162 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
17163}
17164
17165/* Handle .tprelword. */
17166
17167static void
17168s_tprelword (int ignore ATTRIBUTE_UNUSED)
17169{
17170 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
17171}
17172
17173/* Handle .tpreldword. */
17174
17175static void
17176s_tpreldword (int ignore ATTRIBUTE_UNUSED)
17177{
17178 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
741d6ea8
JM
17179}
17180
6478892d
TS
17181/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
17182 code. It sets the offset to use in gp_rel relocations. */
17183
17184static void
17a2f251 17185s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
17186{
17187 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
17188 We also need NewABI support. */
17189 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17190 {
17191 s_ignore (0);
17192 return;
17193 }
17194
def2e0dd 17195 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
17196
17197 demand_empty_rest_of_line ();
17198}
17199
252b5132
RH
17200/* Handle the .gpword pseudo-op. This is used when generating PIC
17201 code. It generates a 32 bit GP relative reloc. */
17202
17203static void
17a2f251 17204s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132 17205{
a8dbcb85
TS
17206 segment_info_type *si;
17207 struct insn_label_list *l;
252b5132
RH
17208 expressionS ex;
17209 char *p;
17210
17211 /* When not generating PIC code, this is treated as .word. */
17212 if (mips_pic != SVR4_PIC)
17213 {
17214 s_cons (2);
17215 return;
17216 }
17217
a8dbcb85
TS
17218 si = seg_info (now_seg);
17219 l = si->label_list;
7d10b47d 17220 mips_emit_delays ();
252b5132 17221 if (auto_align)
462427c4 17222 mips_align (2, 0, l);
252b5132
RH
17223
17224 expression (&ex);
a1facbec 17225 mips_clear_insn_labels ();
252b5132
RH
17226
17227 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17228 {
1661c76c 17229 as_bad (_("unsupported use of .gpword"));
252b5132
RH
17230 ignore_rest_of_line ();
17231 }
17232
17233 p = frag_more (4);
17a2f251 17234 md_number_to_chars (p, 0, 4);
b34976b6 17235 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 17236 BFD_RELOC_GPREL32);
252b5132
RH
17237
17238 demand_empty_rest_of_line ();
17239}
17240
10181a0d 17241static void
17a2f251 17242s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d 17243{
a8dbcb85
TS
17244 segment_info_type *si;
17245 struct insn_label_list *l;
10181a0d
AO
17246 expressionS ex;
17247 char *p;
17248
17249 /* When not generating PIC code, this is treated as .dword. */
17250 if (mips_pic != SVR4_PIC)
17251 {
17252 s_cons (3);
17253 return;
17254 }
17255
a8dbcb85
TS
17256 si = seg_info (now_seg);
17257 l = si->label_list;
7d10b47d 17258 mips_emit_delays ();
10181a0d 17259 if (auto_align)
462427c4 17260 mips_align (3, 0, l);
10181a0d
AO
17261
17262 expression (&ex);
a1facbec 17263 mips_clear_insn_labels ();
10181a0d
AO
17264
17265 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17266 {
1661c76c 17267 as_bad (_("unsupported use of .gpdword"));
10181a0d
AO
17268 ignore_rest_of_line ();
17269 }
17270
17271 p = frag_more (8);
17a2f251 17272 md_number_to_chars (p, 0, 8);
a105a300 17273 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
6e1304d8 17274 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
17275
17276 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8
RS
17277 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
17278 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
17279
17280 demand_empty_rest_of_line ();
17281}
17282
a3f278e2
CM
17283/* Handle the .ehword pseudo-op. This is used when generating unwinding
17284 tables. It generates a R_MIPS_EH reloc. */
17285
17286static void
17287s_ehword (int ignore ATTRIBUTE_UNUSED)
17288{
17289 expressionS ex;
17290 char *p;
17291
17292 mips_emit_delays ();
17293
17294 expression (&ex);
17295 mips_clear_insn_labels ();
17296
17297 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17298 {
1661c76c 17299 as_bad (_("unsupported use of .ehword"));
a3f278e2
CM
17300 ignore_rest_of_line ();
17301 }
17302
17303 p = frag_more (4);
17304 md_number_to_chars (p, 0, 4);
17305 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
2f0c68f2 17306 BFD_RELOC_32_PCREL);
a3f278e2
CM
17307
17308 demand_empty_rest_of_line ();
17309}
17310
252b5132
RH
17311/* Handle the .cpadd pseudo-op. This is used when dealing with switch
17312 tables in SVR4 PIC code. */
17313
17314static void
17a2f251 17315s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 17316{
252b5132
RH
17317 int reg;
17318
919731af 17319 file_mips_check_options ();
17320
10181a0d
AO
17321 /* This is ignored when not generating SVR4 PIC code. */
17322 if (mips_pic != SVR4_PIC)
252b5132
RH
17323 {
17324 s_ignore (0);
17325 return;
17326 }
17327
8a75745d
MR
17328 mips_mark_labels ();
17329 mips_assembling_insn = TRUE;
17330
252b5132 17331 /* Add $gp to the register named as an argument. */
584892a6 17332 macro_start ();
252b5132 17333 reg = tc_get_register (0);
67c0d1eb 17334 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 17335 macro_end ();
252b5132 17336
8a75745d 17337 mips_assembling_insn = FALSE;
bdaaa2e1 17338 demand_empty_rest_of_line ();
252b5132
RH
17339}
17340
17341/* Handle the .insn pseudo-op. This marks instruction labels in
df58fc94 17342 mips16/micromips mode. This permits the linker to handle them specially,
252b5132
RH
17343 such as generating jalx instructions when needed. We also make
17344 them odd for the duration of the assembly, in order to generate the
17345 right sort of code. We will make them even in the adjust_symtab
17346 routine, while leaving them marked. This is convenient for the
17347 debugger and the disassembler. The linker knows to make them odd
17348 again. */
17349
17350static void
17a2f251 17351s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 17352{
7bb01e2d
MR
17353 file_mips_check_options ();
17354 file_ase_mips16 |= mips_opts.mips16;
17355 file_ase_micromips |= mips_opts.micromips;
17356
df58fc94 17357 mips_mark_labels ();
252b5132
RH
17358
17359 demand_empty_rest_of_line ();
17360}
17361
ba92f887
MR
17362/* Handle the .nan pseudo-op. */
17363
17364static void
17365s_nan (int ignore ATTRIBUTE_UNUSED)
17366{
17367 static const char str_legacy[] = "legacy";
17368 static const char str_2008[] = "2008";
17369 size_t i;
17370
17371 for (i = 0; !is_end_of_line[(unsigned char) input_line_pointer[i]]; i++);
17372
17373 if (i == sizeof (str_2008) - 1
17374 && memcmp (input_line_pointer, str_2008, i) == 0)
7361da2c 17375 mips_nan2008 = 1;
ba92f887
MR
17376 else if (i == sizeof (str_legacy) - 1
17377 && memcmp (input_line_pointer, str_legacy, i) == 0)
7361da2c
AB
17378 {
17379 if (ISA_HAS_LEGACY_NAN (file_mips_opts.isa))
17380 mips_nan2008 = 0;
17381 else
17382 as_bad (_("`%s' does not support legacy NaN"),
17383 mips_cpu_info_from_isa (file_mips_opts.isa)->name);
17384 }
ba92f887 17385 else
1661c76c 17386 as_bad (_("bad .nan directive"));
ba92f887
MR
17387
17388 input_line_pointer += i;
17389 demand_empty_rest_of_line ();
17390}
17391
754e2bb9
RS
17392/* Handle a .stab[snd] directive. Ideally these directives would be
17393 implemented in a transparent way, so that removing them would not
17394 have any effect on the generated instructions. However, s_stab
17395 internally changes the section, so in practice we need to decide
17396 now whether the preceding label marks compressed code. We do not
17397 support changing the compression mode of a label after a .stab*
17398 directive, such as in:
17399
17400 foo:
134c0c8b 17401 .stabs ...
754e2bb9
RS
17402 .set mips16
17403
17404 so the current mode wins. */
252b5132
RH
17405
17406static void
17a2f251 17407s_mips_stab (int type)
252b5132 17408{
42c0794e 17409 file_mips_check_options ();
754e2bb9 17410 mips_mark_labels ();
252b5132
RH
17411 s_stab (type);
17412}
17413
54f4ddb3 17414/* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
252b5132
RH
17415
17416static void
17a2f251 17417s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
17418{
17419 char *name;
17420 int c;
17421 symbolS *symbolP;
17422 expressionS exp;
17423
d02603dc 17424 c = get_symbol_name (&name);
252b5132
RH
17425 symbolP = symbol_find_or_make (name);
17426 S_SET_WEAK (symbolP);
17427 *input_line_pointer = c;
17428
d02603dc 17429 SKIP_WHITESPACE_AFTER_NAME ();
252b5132
RH
17430
17431 if (! is_end_of_line[(unsigned char) *input_line_pointer])
17432 {
17433 if (S_IS_DEFINED (symbolP))
17434 {
20203fb9 17435 as_bad (_("ignoring attempt to redefine symbol %s"),
252b5132
RH
17436 S_GET_NAME (symbolP));
17437 ignore_rest_of_line ();
17438 return;
17439 }
bdaaa2e1 17440
252b5132
RH
17441 if (*input_line_pointer == ',')
17442 {
17443 ++input_line_pointer;
17444 SKIP_WHITESPACE ();
17445 }
bdaaa2e1 17446
252b5132
RH
17447 expression (&exp);
17448 if (exp.X_op != O_symbol)
17449 {
20203fb9 17450 as_bad (_("bad .weakext directive"));
98d3f06f 17451 ignore_rest_of_line ();
252b5132
RH
17452 return;
17453 }
49309057 17454 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
17455 }
17456
17457 demand_empty_rest_of_line ();
17458}
17459
17460/* Parse a register string into a number. Called from the ECOFF code
17461 to parse .frame. The argument is non-zero if this is the frame
17462 register, so that we can record it in mips_frame_reg. */
17463
17464int
17a2f251 17465tc_get_register (int frame)
252b5132 17466{
707bfff6 17467 unsigned int reg;
252b5132
RH
17468
17469 SKIP_WHITESPACE ();
707bfff6
TS
17470 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17471 reg = 0;
252b5132 17472 if (frame)
7a621144
DJ
17473 {
17474 mips_frame_reg = reg != 0 ? reg : SP;
17475 mips_frame_reg_valid = 1;
17476 mips_cprestore_valid = 0;
17477 }
252b5132
RH
17478 return reg;
17479}
17480
17481valueT
17a2f251 17482md_section_align (asection *seg, valueT addr)
252b5132
RH
17483{
17484 int align = bfd_get_section_alignment (stdoutput, seg);
17485
f3ded42a
RS
17486 /* We don't need to align ELF sections to the full alignment.
17487 However, Irix 5 may prefer that we align them at least to a 16
17488 byte boundary. We don't bother to align the sections if we
17489 are targeted for an embedded system. */
17490 if (strncmp (TARGET_OS, "elf", 3) == 0)
17491 return addr;
17492 if (align > 4)
17493 align = 4;
252b5132 17494
8d3842cd 17495 return ((addr + (1 << align) - 1) & -(1 << align));
252b5132
RH
17496}
17497
17498/* Utility routine, called from above as well. If called while the
17499 input file is still being read, it's only an approximation. (For
17500 example, a symbol may later become defined which appeared to be
17501 undefined earlier.) */
17502
17503static int
17a2f251 17504nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
17505{
17506 if (sym == 0)
17507 return 0;
17508
4d0d148d 17509 if (g_switch_value > 0)
252b5132
RH
17510 {
17511 const char *symname;
17512 int change;
17513
c9914766 17514 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
17515 register. It can be if it is smaller than the -G size or if
17516 it is in the .sdata or .sbss section. Certain symbols can
c9914766 17517 not be referenced off the $gp, although it appears as though
252b5132
RH
17518 they can. */
17519 symname = S_GET_NAME (sym);
17520 if (symname != (const char *) NULL
17521 && (strcmp (symname, "eprol") == 0
17522 || strcmp (symname, "etext") == 0
17523 || strcmp (symname, "_gp") == 0
17524 || strcmp (symname, "edata") == 0
17525 || strcmp (symname, "_fbss") == 0
17526 || strcmp (symname, "_fdata") == 0
17527 || strcmp (symname, "_ftext") == 0
17528 || strcmp (symname, "end") == 0
17529 || strcmp (symname, "_gp_disp") == 0))
17530 change = 1;
17531 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17532 && (0
17533#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
17534 || (symbol_get_obj (sym)->ecoff_extern_size != 0
17535 && (symbol_get_obj (sym)->ecoff_extern_size
17536 <= g_switch_value))
252b5132
RH
17537#endif
17538 /* We must defer this decision until after the whole
17539 file has been read, since there might be a .extern
17540 after the first use of this symbol. */
17541 || (before_relaxing
17542#ifndef NO_ECOFF_DEBUGGING
49309057 17543 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
17544#endif
17545 && S_GET_VALUE (sym) == 0)
17546 || (S_GET_VALUE (sym) != 0
17547 && S_GET_VALUE (sym) <= g_switch_value)))
17548 change = 0;
17549 else
17550 {
17551 const char *segname;
17552
17553 segname = segment_name (S_GET_SEGMENT (sym));
9c2799c2 17554 gas_assert (strcmp (segname, ".lit8") != 0
252b5132
RH
17555 && strcmp (segname, ".lit4") != 0);
17556 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
17557 && strcmp (segname, ".sbss") != 0
17558 && strncmp (segname, ".sdata.", 7) != 0
d4dc2f22
TS
17559 && strncmp (segname, ".sbss.", 6) != 0
17560 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
fba2b7f9 17561 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
17562 }
17563 return change;
17564 }
17565 else
c9914766 17566 /* We are not optimizing for the $gp register. */
252b5132
RH
17567 return 1;
17568}
17569
5919d012
RS
17570
17571/* Return true if the given symbol should be considered local for SVR4 PIC. */
17572
17573static bfd_boolean
9e009953 17574pic_need_relax (symbolS *sym)
5919d012
RS
17575{
17576 asection *symsec;
5919d012
RS
17577
17578 /* Handle the case of a symbol equated to another symbol. */
17579 while (symbol_equated_reloc_p (sym))
17580 {
17581 symbolS *n;
17582
5f0fe04b 17583 /* It's possible to get a loop here in a badly written program. */
5919d012
RS
17584 n = symbol_get_value_expression (sym)->X_add_symbol;
17585 if (n == sym)
17586 break;
17587 sym = n;
17588 }
17589
df1f3cda
DD
17590 if (symbol_section_p (sym))
17591 return TRUE;
17592
5919d012
RS
17593 symsec = S_GET_SEGMENT (sym);
17594
5919d012 17595 /* This must duplicate the test in adjust_reloc_syms. */
45dfa85a
AM
17596 return (!bfd_is_und_section (symsec)
17597 && !bfd_is_abs_section (symsec)
5f0fe04b 17598 && !bfd_is_com_section (symsec)
5919d012 17599 /* A global or weak symbol is treated as external. */
f3ded42a 17600 && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
5919d012 17601}
14f72d45
MR
17602\f
17603/* Given a MIPS16 variant frag FRAGP and PC-relative operand PCREL_OP
17604 convert a section-relative value VAL to the equivalent PC-relative
17605 value. */
17606
17607static offsetT
17608mips16_pcrel_val (fragS *fragp, const struct mips_pcrel_operand *pcrel_op,
17609 offsetT val, long stretch)
17610{
17611 fragS *sym_frag;
17612 addressT addr;
17613
17614 gas_assert (pcrel_op->root.root.type == OP_PCREL);
17615
17616 sym_frag = symbol_get_frag (fragp->fr_symbol);
17617
17618 /* If the relax_marker of the symbol fragment differs from the
17619 relax_marker of this fragment, we have not yet adjusted the
17620 symbol fragment fr_address. We want to add in STRETCH in
17621 order to get a better estimate of the address. This
17622 particularly matters because of the shift bits. */
17623 if (stretch != 0 && sym_frag->relax_marker != fragp->relax_marker)
17624 {
17625 fragS *f;
17626
17627 /* Adjust stretch for any alignment frag. Note that if have
17628 been expanding the earlier code, the symbol may be
17629 defined in what appears to be an earlier frag. FIXME:
17630 This doesn't handle the fr_subtype field, which specifies
17631 a maximum number of bytes to skip when doing an
17632 alignment. */
17633 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
17634 {
17635 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17636 {
17637 if (stretch < 0)
17638 stretch = -(-stretch & ~((1 << (int) f->fr_offset) - 1));
17639 else
17640 stretch &= ~((1 << (int) f->fr_offset) - 1);
17641 if (stretch == 0)
17642 break;
17643 }
17644 }
17645 if (f != NULL)
17646 val += stretch;
17647 }
17648
17649 addr = fragp->fr_address + fragp->fr_fix;
17650
17651 /* The base address rules are complicated. The base address of
17652 a branch is the following instruction. The base address of a
17653 PC relative load or add is the instruction itself, but if it
17654 is in a delay slot (in which case it can not be extended) use
17655 the address of the instruction whose delay slot it is in. */
17656 if (pcrel_op->include_isa_bit)
17657 {
17658 addr += 2;
17659
17660 /* If we are currently assuming that this frag should be
17661 extended, then the current address is two bytes higher. */
17662 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17663 addr += 2;
17664
17665 /* Ignore the low bit in the target, since it will be set
17666 for a text label. */
17667 val &= -2;
17668 }
17669 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17670 addr -= 4;
17671 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17672 addr -= 2;
5919d012 17673
14f72d45
MR
17674 val -= addr & -(1 << pcrel_op->align_log2);
17675
17676 return val;
17677}
5919d012 17678
252b5132
RH
17679/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17680 extended opcode. SEC is the section the frag is in. */
17681
17682static int
17a2f251 17683mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132 17684{
3ccad066 17685 const struct mips_int_operand *operand;
252b5132 17686 offsetT val;
252b5132 17687 segT symsec;
14f72d45 17688 int type;
252b5132
RH
17689
17690 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17691 return 0;
17692 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17693 return 1;
17694
88a7ef16 17695 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132 17696 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
3ccad066 17697 operand = mips16_immed_operand (type, FALSE);
88a7ef16
MR
17698 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
17699 || (operand->root.type == OP_PCREL
17700 ? sec != symsec
17701 : !bfd_is_abs_section (symsec)))
17702 return 1;
252b5132 17703
88a7ef16 17704 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
252b5132 17705
3ccad066 17706 if (operand->root.type == OP_PCREL)
252b5132 17707 {
3ccad066 17708 const struct mips_pcrel_operand *pcrel_op;
3ccad066 17709 offsetT maxtiny;
252b5132 17710
1425c41d 17711 if (RELAX_MIPS16_ALWAYS_EXTENDED (fragp->fr_subtype))
88a7ef16 17712 return 1;
252b5132 17713
88a7ef16 17714 pcrel_op = (const struct mips_pcrel_operand *) operand;
14f72d45 17715 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
252b5132
RH
17716
17717 /* If any of the shifted bits are set, we must use an extended
17718 opcode. If the address depends on the size of this
17719 instruction, this can lead to a loop, so we arrange to always
88a7ef16
MR
17720 use an extended opcode. */
17721 if ((val & ((1 << operand->shift) - 1)) != 0)
252b5132
RH
17722 {
17723 fragp->fr_subtype =
1425c41d 17724 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
252b5132
RH
17725 return 1;
17726 }
17727
17728 /* If we are about to mark a frag as extended because the value
3ccad066
RS
17729 is precisely the next value above maxtiny, then there is a
17730 chance of an infinite loop as in the following code:
252b5132
RH
17731 la $4,foo
17732 .skip 1020
17733 .align 2
17734 foo:
17735 In this case when the la is extended, foo is 0x3fc bytes
17736 away, so the la can be shrunk, but then foo is 0x400 away, so
17737 the la must be extended. To avoid this loop, we mark the
17738 frag as extended if it was small, and is about to become
3ccad066
RS
17739 extended with the next value above maxtiny. */
17740 maxtiny = mips_int_operand_max (operand);
17741 if (val == maxtiny + (1 << operand->shift)
88a7ef16 17742 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
252b5132
RH
17743 {
17744 fragp->fr_subtype =
1425c41d 17745 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
252b5132
RH
17746 return 1;
17747 }
17748 }
252b5132 17749
3ccad066 17750 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
252b5132
RH
17751}
17752
8507b6e7
MR
17753/* Given a MIPS16 variant frag FRAGP, return non-zero if it needs
17754 macro expansion. SEC is the section the frag is in. We only
17755 support PC-relative instructions (LA, DLA, LW, LD) here, in
17756 non-PIC code using 32-bit addressing. */
17757
17758static int
17759mips16_macro_frag (fragS *fragp, asection *sec, long stretch)
17760{
17761 const struct mips_pcrel_operand *pcrel_op;
17762 const struct mips_int_operand *operand;
17763 offsetT val;
17764 segT symsec;
17765 int type;
17766
17767 gas_assert (!RELAX_MIPS16_USER_SMALL (fragp->fr_subtype));
17768
17769 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17770 return 0;
17771 if (!RELAX_MIPS16_SYM32 (fragp->fr_subtype))
17772 return 0;
17773
17774 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17775 switch (type)
17776 {
17777 case 'A':
17778 case 'B':
17779 case 'E':
17780 symsec = S_GET_SEGMENT (fragp->fr_symbol);
17781 if (bfd_is_abs_section (symsec))
17782 return 1;
17783 if (RELAX_MIPS16_PIC (fragp->fr_subtype))
17784 return 0;
17785 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE) || sec != symsec)
17786 return 1;
17787
17788 operand = mips16_immed_operand (type, TRUE);
17789 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17790 pcrel_op = (const struct mips_pcrel_operand *) operand;
17791 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
17792
17793 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
17794
17795 default:
17796 return 0;
17797 }
17798}
17799
4a6a3df4
AO
17800/* Compute the length of a branch sequence, and adjust the
17801 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
17802 worst-case length is computed, with UPDATE being used to indicate
17803 whether an unconditional (-1), branch-likely (+1) or regular (0)
17804 branch is to be computed. */
17805static int
17a2f251 17806relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 17807{
b34976b6 17808 bfd_boolean toofar;
4a6a3df4
AO
17809 int length;
17810
17811 if (fragp
17812 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 17813 && !S_IS_WEAK (fragp->fr_symbol)
4a6a3df4
AO
17814 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17815 {
17816 addressT addr;
17817 offsetT val;
17818
17819 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17820
17821 addr = fragp->fr_address + fragp->fr_fix + 4;
17822
17823 val -= addr;
17824
17825 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17826 }
4a6a3df4 17827 else
c1f61bd2
MR
17828 /* If the symbol is not defined or it's in a different segment,
17829 we emit the long sequence. */
b34976b6 17830 toofar = TRUE;
4a6a3df4
AO
17831
17832 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17833 fragp->fr_subtype
66b3e8da 17834 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
ce8ad872 17835 RELAX_BRANCH_PIC (fragp->fr_subtype),
66b3e8da 17836 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
17837 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17838 RELAX_BRANCH_LINK (fragp->fr_subtype),
17839 toofar);
17840
17841 length = 4;
17842 if (toofar)
17843 {
17844 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17845 length += 8;
17846
ce8ad872 17847 if (!fragp || RELAX_BRANCH_PIC (fragp->fr_subtype))
4a6a3df4
AO
17848 {
17849 /* Additional space for PIC loading of target address. */
17850 length += 8;
17851 if (mips_opts.isa == ISA_MIPS1)
17852 /* Additional space for $at-stabilizing nop. */
17853 length += 4;
17854 }
17855
17856 /* If branch is conditional. */
17857 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17858 length += 8;
17859 }
b34976b6 17860
4a6a3df4
AO
17861 return length;
17862}
17863
7bd374a4
MR
17864/* Get a FRAG's branch instruction delay slot size, either from the
17865 short-delay-slot bit of a branch-and-link instruction if AL is TRUE,
17866 or SHORT_INSN_SIZE otherwise. */
17867
17868static int
17869frag_branch_delay_slot_size (fragS *fragp, bfd_boolean al, int short_insn_size)
17870{
17871 char *buf = fragp->fr_literal + fragp->fr_fix;
17872
17873 if (al)
17874 return (read_compressed_insn (buf, 4) & 0x02000000) ? 2 : 4;
17875 else
17876 return short_insn_size;
17877}
17878
df58fc94
RS
17879/* Compute the length of a branch sequence, and adjust the
17880 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
17881 worst-case length is computed, with UPDATE being used to indicate
17882 whether an unconditional (-1), or regular (0) branch is to be
17883 computed. */
17884
17885static int
17886relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
17887{
7bd374a4
MR
17888 bfd_boolean insn32 = TRUE;
17889 bfd_boolean nods = TRUE;
ce8ad872 17890 bfd_boolean pic = TRUE;
7bd374a4
MR
17891 bfd_boolean al = TRUE;
17892 int short_insn_size;
df58fc94
RS
17893 bfd_boolean toofar;
17894 int length;
17895
7bd374a4
MR
17896 if (fragp)
17897 {
17898 insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
17899 nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
ce8ad872 17900 pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
7bd374a4
MR
17901 al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
17902 }
17903 short_insn_size = insn32 ? 4 : 2;
17904
df58fc94
RS
17905 if (fragp
17906 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 17907 && !S_IS_WEAK (fragp->fr_symbol)
df58fc94
RS
17908 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17909 {
17910 addressT addr;
17911 offsetT val;
17912
17913 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17914 /* Ignore the low bit in the target, since it will be set
17915 for a text label. */
17916 if ((val & 1) != 0)
17917 --val;
17918
17919 addr = fragp->fr_address + fragp->fr_fix + 4;
17920
17921 val -= addr;
17922
17923 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
17924 }
df58fc94 17925 else
c1f61bd2
MR
17926 /* If the symbol is not defined or it's in a different segment,
17927 we emit the long sequence. */
df58fc94
RS
17928 toofar = TRUE;
17929
17930 if (fragp && update
17931 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17932 fragp->fr_subtype = (toofar
17933 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
17934 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
17935
17936 length = 4;
17937 if (toofar)
17938 {
17939 bfd_boolean compact_known = fragp != NULL;
17940 bfd_boolean compact = FALSE;
17941 bfd_boolean uncond;
17942
df58fc94 17943 if (fragp)
8484fb75
MR
17944 {
17945 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17946 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
8484fb75 17947 }
df58fc94
RS
17948 else
17949 uncond = update < 0;
17950
17951 /* If label is out of range, we turn branch <br>:
17952
17953 <br> label # 4 bytes
17954 0:
17955
17956 into:
17957
17958 j label # 4 bytes
8484fb75
MR
17959 nop # 2/4 bytes if
17960 # compact && (!PIC || insn32)
df58fc94
RS
17961 0:
17962 */
ce8ad872 17963 if ((!pic || insn32) && (!compact_known || compact))
8484fb75 17964 length += short_insn_size;
df58fc94
RS
17965
17966 /* If assembling PIC code, we further turn:
17967
17968 j label # 4 bytes
17969
17970 into:
17971
17972 lw/ld at, %got(label)(gp) # 4 bytes
17973 d/addiu at, %lo(label) # 4 bytes
8484fb75 17974 jr/c at # 2/4 bytes
df58fc94 17975 */
ce8ad872 17976 if (pic)
8484fb75 17977 length += 4 + short_insn_size;
df58fc94 17978
7bd374a4
MR
17979 /* Add an extra nop if the jump has no compact form and we need
17980 to fill the delay slot. */
ce8ad872 17981 if ((!pic || al) && nods)
7bd374a4
MR
17982 length += (fragp
17983 ? frag_branch_delay_slot_size (fragp, al, short_insn_size)
17984 : short_insn_size);
17985
df58fc94
RS
17986 /* If branch <br> is conditional, we prepend negated branch <brneg>:
17987
17988 <brneg> 0f # 4 bytes
8484fb75 17989 nop # 2/4 bytes if !compact
df58fc94
RS
17990 */
17991 if (!uncond)
8484fb75 17992 length += (compact_known && compact) ? 4 : 4 + short_insn_size;
df58fc94 17993 }
7bd374a4
MR
17994 else if (nods)
17995 {
17996 /* Add an extra nop to fill the delay slot. */
17997 gas_assert (fragp);
17998 length += frag_branch_delay_slot_size (fragp, al, short_insn_size);
17999 }
df58fc94
RS
18000
18001 return length;
18002}
18003
18004/* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
18005 bit accordingly. */
18006
18007static int
18008relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
18009{
18010 bfd_boolean toofar;
18011
df58fc94
RS
18012 if (fragp
18013 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 18014 && !S_IS_WEAK (fragp->fr_symbol)
df58fc94
RS
18015 && sec == S_GET_SEGMENT (fragp->fr_symbol))
18016 {
18017 addressT addr;
18018 offsetT val;
18019 int type;
18020
18021 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
18022 /* Ignore the low bit in the target, since it will be set
18023 for a text label. */
18024 if ((val & 1) != 0)
18025 --val;
18026
18027 /* Assume this is a 2-byte branch. */
18028 addr = fragp->fr_address + fragp->fr_fix + 2;
18029
18030 /* We try to avoid the infinite loop by not adding 2 more bytes for
18031 long branches. */
18032
18033 val -= addr;
18034
18035 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
18036 if (type == 'D')
18037 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
18038 else if (type == 'E')
18039 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
18040 else
18041 abort ();
18042 }
18043 else
18044 /* If the symbol is not defined or it's in a different segment,
18045 we emit a normal 32-bit branch. */
18046 toofar = TRUE;
18047
18048 if (fragp && update
18049 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18050 fragp->fr_subtype
18051 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
18052 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
18053
18054 if (toofar)
18055 return 4;
18056
18057 return 2;
18058}
18059
252b5132
RH
18060/* Estimate the size of a frag before relaxing. Unless this is the
18061 mips16, we are not really relaxing here, and the final size is
18062 encoded in the subtype information. For the mips16, we have to
18063 decide whether we are using an extended opcode or not. */
18064
252b5132 18065int
17a2f251 18066md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 18067{
5919d012 18068 int change;
252b5132 18069
4a6a3df4
AO
18070 if (RELAX_BRANCH_P (fragp->fr_subtype))
18071 {
18072
b34976b6
AM
18073 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
18074
4a6a3df4
AO
18075 return fragp->fr_var;
18076 }
18077
252b5132 18078 if (RELAX_MIPS16_P (fragp->fr_subtype))
8507b6e7
MR
18079 {
18080 /* We don't want to modify the EXTENDED bit here; it might get us
18081 into infinite loops. We change it only in mips_relax_frag(). */
18082 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
25499ac7 18083 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 8 : 12;
8507b6e7
MR
18084 else
18085 return RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2;
18086 }
252b5132 18087
df58fc94
RS
18088 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18089 {
18090 int length = 4;
18091
18092 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
18093 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
18094 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
18095 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
18096 fragp->fr_var = length;
18097
18098 return length;
18099 }
18100
ce8ad872 18101 if (mips_pic == VXWORKS_PIC)
0a44bf69
RS
18102 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
18103 change = 0;
ce8ad872
MR
18104 else if (RELAX_PIC (fragp->fr_subtype))
18105 change = pic_need_relax (fragp->fr_symbol);
252b5132 18106 else
ce8ad872 18107 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132
RH
18108
18109 if (change)
18110 {
4d7206a2 18111 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 18112 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 18113 }
4d7206a2
RS
18114 else
18115 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
18116}
18117
18118/* This is called to see whether a reloc against a defined symbol
de7e6852 18119 should be converted into a reloc against a section. */
252b5132
RH
18120
18121int
17a2f251 18122mips_fix_adjustable (fixS *fixp)
252b5132 18123{
252b5132
RH
18124 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
18125 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
18126 return 0;
a161fe53 18127
252b5132
RH
18128 if (fixp->fx_addsy == NULL)
18129 return 1;
a161fe53 18130
2f0c68f2
CM
18131 /* Allow relocs used for EH tables. */
18132 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
18133 return 1;
18134
de7e6852
RS
18135 /* If symbol SYM is in a mergeable section, relocations of the form
18136 SYM + 0 can usually be made section-relative. The mergeable data
18137 is then identified by the section offset rather than by the symbol.
18138
18139 However, if we're generating REL LO16 relocations, the offset is split
33eaf5de 18140 between the LO16 and partnering high part relocation. The linker will
de7e6852
RS
18141 need to recalculate the complete offset in order to correctly identify
18142 the merge data.
18143
33eaf5de 18144 The linker has traditionally not looked for the partnering high part
de7e6852
RS
18145 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
18146 placed anywhere. Rather than break backwards compatibility by changing
18147 this, it seems better not to force the issue, and instead keep the
18148 original symbol. This will work with either linker behavior. */
738e5348 18149 if ((lo16_reloc_p (fixp->fx_r_type)
704803a9 18150 || reloc_needs_lo_p (fixp->fx_r_type))
de7e6852
RS
18151 && HAVE_IN_PLACE_ADDENDS
18152 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
18153 return 0;
18154
97f50151
MR
18155 /* There is no place to store an in-place offset for JALR relocations. */
18156 if (jalr_reloc_p (fixp->fx_r_type) && HAVE_IN_PLACE_ADDENDS)
18157 return 0;
18158
18159 /* Likewise an in-range offset of limited PC-relative relocations may
2de39019 18160 overflow the in-place relocatable field if recalculated against the
7361da2c
AB
18161 start address of the symbol's containing section.
18162
18163 Also, PC relative relocations for MIPS R6 need to be symbol rather than
18164 section relative to allow linker relaxations to be performed later on. */
97f50151 18165 if (limited_pcrel_reloc_p (fixp->fx_r_type)
912815f0 18166 && (HAVE_IN_PLACE_ADDENDS || ISA_IS_R6 (file_mips_opts.isa)))
1180b5a4
RS
18167 return 0;
18168
b314ec0e
RS
18169 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
18170 to a floating-point stub. The same is true for non-R_MIPS16_26
18171 relocations against MIPS16 functions; in this case, the stub becomes
18172 the function's canonical address.
18173
18174 Floating-point stubs are stored in unique .mips16.call.* or
18175 .mips16.fn.* sections. If a stub T for function F is in section S,
18176 the first relocation in section S must be against F; this is how the
18177 linker determines the target function. All relocations that might
18178 resolve to T must also be against F. We therefore have the following
18179 restrictions, which are given in an intentionally-redundant way:
18180
18181 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
18182 symbols.
18183
18184 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
18185 if that stub might be used.
18186
18187 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
18188 symbols.
18189
18190 4. We cannot reduce a stub's relocations against MIPS16 symbols if
18191 that stub might be used.
18192
18193 There is a further restriction:
18194
df58fc94 18195 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
0e9c5a5c 18196 R_MICROMIPS_26_S1) or branch relocations (R_MIPS_PC26_S2,
c9775dde
MR
18197 R_MIPS_PC21_S2, R_MIPS_PC16, R_MIPS16_PC16_S1,
18198 R_MICROMIPS_PC16_S1, R_MICROMIPS_PC10_S1 or R_MICROMIPS_PC7_S1)
18199 against MIPS16 or microMIPS symbols because we need to keep the
18200 MIPS16 or microMIPS symbol for the purpose of mode mismatch
a6ebf616
MR
18201 detection and JAL or BAL to JALX instruction conversion in the
18202 linker.
b314ec0e 18203
df58fc94 18204 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
507dcb32 18205 against a MIPS16 symbol. We deal with (5) by additionally leaving
0e9c5a5c 18206 alone any jump and branch relocations against a microMIPS symbol.
b314ec0e
RS
18207
18208 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
18209 relocation against some symbol R, no relocation against R may be
18210 reduced. (Note that this deals with (2) as well as (1) because
18211 relocations against global symbols will never be reduced on ELF
18212 targets.) This approach is a little simpler than trying to detect
18213 stub sections, and gives the "all or nothing" per-symbol consistency
18214 that we have for MIPS16 symbols. */
f3ded42a 18215 if (fixp->fx_subsy == NULL
30c09090 18216 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
44d3da23 18217 || (ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
0e9c5a5c
MR
18218 && (jmp_reloc_p (fixp->fx_r_type)
18219 || b_reloc_p (fixp->fx_r_type)))
44d3da23 18220 || *symbol_get_tc (fixp->fx_addsy)))
252b5132 18221 return 0;
a161fe53 18222
252b5132
RH
18223 return 1;
18224}
18225
18226/* Translate internal representation of relocation info to BFD target
18227 format. */
18228
18229arelent **
17a2f251 18230tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
18231{
18232 static arelent *retval[4];
18233 arelent *reloc;
18234 bfd_reloc_code_real_type code;
18235
4b0cff4e 18236 memset (retval, 0, sizeof(retval));
325801bd
TS
18237 reloc = retval[0] = XCNEW (arelent);
18238 reloc->sym_ptr_ptr = XNEW (asymbol *);
49309057 18239 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
18240 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
18241
bad36eac
DJ
18242 if (fixp->fx_pcrel)
18243 {
df58fc94 18244 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
c9775dde 18245 || fixp->fx_r_type == BFD_RELOC_MIPS16_16_PCREL_S1
df58fc94
RS
18246 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
18247 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
b47468a6 18248 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
7361da2c
AB
18249 || fixp->fx_r_type == BFD_RELOC_32_PCREL
18250 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
18251 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
18252 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
18253 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
18254 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
18255 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL);
bad36eac
DJ
18256
18257 /* At this point, fx_addnumber is "symbol offset - pcrel address".
18258 Relocations want only the symbol offset. */
51f6035b
MR
18259 switch (fixp->fx_r_type)
18260 {
18261 case BFD_RELOC_MIPS_18_PCREL_S3:
18262 reloc->addend = fixp->fx_addnumber + (reloc->address & ~7);
18263 break;
18264 default:
18265 reloc->addend = fixp->fx_addnumber + reloc->address;
18266 break;
18267 }
bad36eac 18268 }
17c6c9d9
MR
18269 else if (HAVE_IN_PLACE_ADDENDS
18270 && fixp->fx_r_type == BFD_RELOC_MICROMIPS_JMP
18271 && (read_compressed_insn (fixp->fx_frag->fr_literal
18272 + fixp->fx_where, 4) >> 26) == 0x3c)
18273 {
18274 /* Shift is 2, unusually, for microMIPS JALX. Adjust the in-place
18275 addend accordingly. */
18276 reloc->addend = fixp->fx_addnumber >> 1;
18277 }
bad36eac
DJ
18278 else
18279 reloc->addend = fixp->fx_addnumber;
252b5132 18280
438c16b8
TS
18281 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
18282 entry to be used in the relocation's section offset. */
18283 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
18284 {
18285 reloc->address = reloc->addend;
18286 reloc->addend = 0;
18287 }
18288
252b5132 18289 code = fixp->fx_r_type;
252b5132 18290
bad36eac 18291 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
252b5132
RH
18292 if (reloc->howto == NULL)
18293 {
18294 as_bad_where (fixp->fx_file, fixp->fx_line,
1661c76c
RS
18295 _("cannot represent %s relocation in this object file"
18296 " format"),
252b5132
RH
18297 bfd_get_reloc_code_name (code));
18298 retval[0] = NULL;
18299 }
18300
18301 return retval;
18302}
18303
18304/* Relax a machine dependent frag. This returns the amount by which
18305 the current size of the frag should change. */
18306
18307int
17a2f251 18308mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 18309{
4a6a3df4
AO
18310 if (RELAX_BRANCH_P (fragp->fr_subtype))
18311 {
18312 offsetT old_var = fragp->fr_var;
b34976b6
AM
18313
18314 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
18315
18316 return fragp->fr_var - old_var;
18317 }
18318
df58fc94
RS
18319 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18320 {
18321 offsetT old_var = fragp->fr_var;
18322 offsetT new_var = 4;
18323
18324 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
18325 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
18326 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
18327 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
18328 fragp->fr_var = new_var;
18329
18330 return new_var - old_var;
18331 }
18332
252b5132
RH
18333 if (! RELAX_MIPS16_P (fragp->fr_subtype))
18334 return 0;
18335
8507b6e7 18336 if (!mips16_extended_frag (fragp, sec, stretch))
252b5132 18337 {
8507b6e7
MR
18338 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18339 {
18340 fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
25499ac7 18341 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -6 : -10;
8507b6e7
MR
18342 }
18343 else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18344 {
18345 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18346 return -2;
18347 }
18348 else
18349 return 0;
18350 }
18351 else if (!mips16_macro_frag (fragp, sec, stretch))
18352 {
18353 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18354 {
18355 fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
18356 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
25499ac7 18357 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -4 : -8;
8507b6e7
MR
18358 }
18359 else if (!RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18360 {
18361 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
18362 return 2;
18363 }
18364 else
252b5132 18365 return 0;
252b5132
RH
18366 }
18367 else
18368 {
8507b6e7 18369 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
252b5132 18370 return 0;
8507b6e7
MR
18371 else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18372 {
18373 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18374 fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
25499ac7 18375 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 4 : 8;
8507b6e7
MR
18376 }
18377 else
18378 {
18379 fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
25499ac7 18380 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 6 : 10;
8507b6e7 18381 }
252b5132
RH
18382 }
18383
18384 return 0;
18385}
18386
18387/* Convert a machine dependent frag. */
18388
18389void
17a2f251 18390md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 18391{
4a6a3df4
AO
18392 if (RELAX_BRANCH_P (fragp->fr_subtype))
18393 {
4d68580a 18394 char *buf;
4a6a3df4 18395 unsigned long insn;
4a6a3df4 18396 fixS *fixp;
b34976b6 18397
4d68580a
RS
18398 buf = fragp->fr_literal + fragp->fr_fix;
18399 insn = read_insn (buf);
b34976b6 18400
4a6a3df4
AO
18401 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
18402 {
18403 /* We generate a fixup instead of applying it right now
18404 because, if there are linker relaxations, we're going to
18405 need the relocations. */
bbd27b76
MR
18406 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18407 fragp->fr_symbol, fragp->fr_offset,
18408 TRUE, BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
18409 fixp->fx_file = fragp->fr_file;
18410 fixp->fx_line = fragp->fr_line;
b34976b6 18411
4d68580a 18412 buf = write_insn (buf, insn);
4a6a3df4
AO
18413 }
18414 else
18415 {
18416 int i;
18417
18418 as_warn_where (fragp->fr_file, fragp->fr_line,
1661c76c 18419 _("relaxed out-of-range branch into a jump"));
4a6a3df4
AO
18420
18421 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
18422 goto uncond;
18423
18424 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18425 {
18426 /* Reverse the branch. */
18427 switch ((insn >> 28) & 0xf)
18428 {
18429 case 4:
56d438b1
CF
18430 if ((insn & 0xff000000) == 0x47000000
18431 || (insn & 0xff600000) == 0x45600000)
18432 {
18433 /* BZ.df/BNZ.df, BZ.V/BNZ.V can have the condition
18434 reversed by tweaking bit 23. */
18435 insn ^= 0x00800000;
18436 }
18437 else
18438 {
18439 /* bc[0-3][tf]l? instructions can have the condition
18440 reversed by tweaking a single TF bit, and their
18441 opcodes all have 0x4???????. */
18442 gas_assert ((insn & 0xf3e00000) == 0x41000000);
18443 insn ^= 0x00010000;
18444 }
4a6a3df4
AO
18445 break;
18446
18447 case 0:
18448 /* bltz 0x04000000 bgez 0x04010000
54f4ddb3 18449 bltzal 0x04100000 bgezal 0x04110000 */
9c2799c2 18450 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
4a6a3df4
AO
18451 insn ^= 0x00010000;
18452 break;
b34976b6 18453
4a6a3df4
AO
18454 case 1:
18455 /* beq 0x10000000 bne 0x14000000
54f4ddb3 18456 blez 0x18000000 bgtz 0x1c000000 */
4a6a3df4
AO
18457 insn ^= 0x04000000;
18458 break;
18459
18460 default:
18461 abort ();
18462 }
18463 }
18464
18465 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18466 {
18467 /* Clear the and-link bit. */
9c2799c2 18468 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
4a6a3df4 18469
54f4ddb3
TS
18470 /* bltzal 0x04100000 bgezal 0x04110000
18471 bltzall 0x04120000 bgezall 0x04130000 */
4a6a3df4
AO
18472 insn &= ~0x00100000;
18473 }
18474
18475 /* Branch over the branch (if the branch was likely) or the
18476 full jump (not likely case). Compute the offset from the
18477 current instruction to branch to. */
18478 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18479 i = 16;
18480 else
18481 {
18482 /* How many bytes in instructions we've already emitted? */
4d68580a 18483 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18484 /* How many bytes in instructions from here to the end? */
18485 i = fragp->fr_var - i;
18486 }
18487 /* Convert to instruction count. */
18488 i >>= 2;
18489 /* Branch counts from the next instruction. */
b34976b6 18490 i--;
4a6a3df4
AO
18491 insn |= i;
18492 /* Branch over the jump. */
4d68580a 18493 buf = write_insn (buf, insn);
4a6a3df4 18494
54f4ddb3 18495 /* nop */
4d68580a 18496 buf = write_insn (buf, 0);
4a6a3df4
AO
18497
18498 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18499 {
18500 /* beql $0, $0, 2f */
18501 insn = 0x50000000;
18502 /* Compute the PC offset from the current instruction to
18503 the end of the variable frag. */
18504 /* How many bytes in instructions we've already emitted? */
4d68580a 18505 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18506 /* How many bytes in instructions from here to the end? */
18507 i = fragp->fr_var - i;
18508 /* Convert to instruction count. */
18509 i >>= 2;
18510 /* Don't decrement i, because we want to branch over the
18511 delay slot. */
4a6a3df4 18512 insn |= i;
4a6a3df4 18513
4d68580a
RS
18514 buf = write_insn (buf, insn);
18515 buf = write_insn (buf, 0);
4a6a3df4
AO
18516 }
18517
18518 uncond:
ce8ad872 18519 if (!RELAX_BRANCH_PIC (fragp->fr_subtype))
4a6a3df4
AO
18520 {
18521 /* j or jal. */
18522 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18523 ? 0x0c000000 : 0x08000000);
4a6a3df4 18524
bbd27b76
MR
18525 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18526 fragp->fr_symbol, fragp->fr_offset,
18527 FALSE, BFD_RELOC_MIPS_JMP);
4a6a3df4
AO
18528 fixp->fx_file = fragp->fr_file;
18529 fixp->fx_line = fragp->fr_line;
18530
4d68580a 18531 buf = write_insn (buf, insn);
4a6a3df4
AO
18532 }
18533 else
18534 {
66b3e8da
MR
18535 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18536
4a6a3df4 18537 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
66b3e8da
MR
18538 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18539 insn |= at << OP_SH_RT;
4a6a3df4 18540
bbd27b76
MR
18541 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18542 fragp->fr_symbol, fragp->fr_offset,
18543 FALSE, BFD_RELOC_MIPS_GOT16);
4a6a3df4
AO
18544 fixp->fx_file = fragp->fr_file;
18545 fixp->fx_line = fragp->fr_line;
18546
4d68580a 18547 buf = write_insn (buf, insn);
b34976b6 18548
4a6a3df4 18549 if (mips_opts.isa == ISA_MIPS1)
4d68580a
RS
18550 /* nop */
18551 buf = write_insn (buf, 0);
4a6a3df4
AO
18552
18553 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
66b3e8da
MR
18554 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18555 insn |= at << OP_SH_RS | at << OP_SH_RT;
4a6a3df4 18556
bbd27b76
MR
18557 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18558 fragp->fr_symbol, fragp->fr_offset,
18559 FALSE, BFD_RELOC_LO16);
4a6a3df4
AO
18560 fixp->fx_file = fragp->fr_file;
18561 fixp->fx_line = fragp->fr_line;
b34976b6 18562
4d68580a 18563 buf = write_insn (buf, insn);
4a6a3df4
AO
18564
18565 /* j(al)r $at. */
18566 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
66b3e8da 18567 insn = 0x0000f809;
4a6a3df4 18568 else
66b3e8da
MR
18569 insn = 0x00000008;
18570 insn |= at << OP_SH_RS;
4a6a3df4 18571
4d68580a 18572 buf = write_insn (buf, insn);
4a6a3df4
AO
18573 }
18574 }
18575
4a6a3df4 18576 fragp->fr_fix += fragp->fr_var;
4d68580a 18577 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
4a6a3df4
AO
18578 return;
18579 }
18580
df58fc94
RS
18581 /* Relax microMIPS branches. */
18582 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18583 {
4d68580a 18584 char *buf = fragp->fr_literal + fragp->fr_fix;
df58fc94 18585 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
8484fb75 18586 bfd_boolean insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
7bd374a4 18587 bfd_boolean nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
ce8ad872 18588 bfd_boolean pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
df58fc94
RS
18589 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18590 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
2309ddf2 18591 bfd_boolean short_ds;
df58fc94 18592 unsigned long insn;
df58fc94
RS
18593 fixS *fixp;
18594
df58fc94
RS
18595 fragp->fr_fix += fragp->fr_var;
18596
18597 /* Handle 16-bit branches that fit or are forced to fit. */
18598 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18599 {
18600 /* We generate a fixup instead of applying it right now,
18601 because if there is linker relaxation, we're going to
18602 need the relocations. */
834a65aa
MR
18603 switch (type)
18604 {
18605 case 'D':
18606 fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18607 fragp->fr_symbol, fragp->fr_offset,
18608 TRUE, BFD_RELOC_MICROMIPS_10_PCREL_S1);
18609 break;
18610 case 'E':
18611 fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18612 fragp->fr_symbol, fragp->fr_offset,
18613 TRUE, BFD_RELOC_MICROMIPS_7_PCREL_S1);
18614 break;
18615 default:
18616 abort ();
18617 }
df58fc94
RS
18618
18619 fixp->fx_file = fragp->fr_file;
18620 fixp->fx_line = fragp->fr_line;
18621
18622 /* These relocations can have an addend that won't fit in
18623 2 octets. */
18624 fixp->fx_no_overflow = 1;
18625
18626 return;
18627 }
18628
2309ddf2 18629 /* Handle 32-bit branches that fit or are forced to fit. */
df58fc94
RS
18630 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18631 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18632 {
18633 /* We generate a fixup instead of applying it right now,
18634 because if there is linker relaxation, we're going to
18635 need the relocations. */
bbd27b76
MR
18636 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18637 fragp->fr_symbol, fragp->fr_offset,
18638 TRUE, BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18639 fixp->fx_file = fragp->fr_file;
18640 fixp->fx_line = fragp->fr_line;
18641
18642 if (type == 0)
7bd374a4
MR
18643 {
18644 insn = read_compressed_insn (buf, 4);
18645 buf += 4;
18646
18647 if (nods)
18648 {
18649 /* Check the short-delay-slot bit. */
18650 if (!al || (insn & 0x02000000) != 0)
18651 buf = write_compressed_insn (buf, 0x0c00, 2);
18652 else
18653 buf = write_compressed_insn (buf, 0x00000000, 4);
18654 }
18655
18656 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18657 return;
18658 }
df58fc94
RS
18659 }
18660
18661 /* Relax 16-bit branches to 32-bit branches. */
18662 if (type != 0)
18663 {
4d68580a 18664 insn = read_compressed_insn (buf, 2);
df58fc94
RS
18665
18666 if ((insn & 0xfc00) == 0xcc00) /* b16 */
18667 insn = 0x94000000; /* beq */
18668 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
18669 {
18670 unsigned long regno;
18671
18672 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18673 regno = micromips_to_32_reg_d_map [regno];
18674 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
18675 insn |= regno << MICROMIPSOP_SH_RS;
18676 }
18677 else
18678 abort ();
18679
18680 /* Nothing else to do, just write it out. */
18681 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18682 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18683 {
4d68580a 18684 buf = write_compressed_insn (buf, insn, 4);
7bd374a4
MR
18685 if (nods)
18686 buf = write_compressed_insn (buf, 0x0c00, 2);
4d68580a 18687 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18688 return;
18689 }
18690 }
18691 else
4d68580a 18692 insn = read_compressed_insn (buf, 4);
df58fc94
RS
18693
18694 /* Relax 32-bit branches to a sequence of instructions. */
18695 as_warn_where (fragp->fr_file, fragp->fr_line,
1661c76c 18696 _("relaxed out-of-range branch into a jump"));
df58fc94 18697
2309ddf2 18698 /* Set the short-delay-slot bit. */
7bd374a4 18699 short_ds = !al || (insn & 0x02000000) != 0;
df58fc94
RS
18700
18701 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18702 {
18703 symbolS *l;
18704
18705 /* Reverse the branch. */
18706 if ((insn & 0xfc000000) == 0x94000000 /* beq */
18707 || (insn & 0xfc000000) == 0xb4000000) /* bne */
18708 insn ^= 0x20000000;
18709 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
18710 || (insn & 0xffe00000) == 0x40400000 /* bgez */
18711 || (insn & 0xffe00000) == 0x40800000 /* blez */
18712 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
18713 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
18714 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
18715 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
18716 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
18717 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
18718 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
18719 insn ^= 0x00400000;
18720 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
18721 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
18722 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
18723 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
18724 insn ^= 0x00200000;
56d438b1
CF
18725 else if ((insn & 0xff000000) == 0x83000000 /* BZ.df
18726 BNZ.df */
18727 || (insn & 0xff600000) == 0x81600000) /* BZ.V
18728 BNZ.V */
18729 insn ^= 0x00800000;
df58fc94
RS
18730 else
18731 abort ();
18732
18733 if (al)
18734 {
18735 /* Clear the and-link and short-delay-slot bits. */
18736 gas_assert ((insn & 0xfda00000) == 0x40200000);
18737
18738 /* bltzal 0x40200000 bgezal 0x40600000 */
18739 /* bltzals 0x42200000 bgezals 0x42600000 */
18740 insn &= ~0x02200000;
18741 }
18742
18743 /* Make a label at the end for use with the branch. */
18744 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
18745 micromips_label_inc ();
f3ded42a 18746 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
df58fc94
RS
18747
18748 /* Refer to it. */
4d68580a
RS
18749 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
18750 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18751 fixp->fx_file = fragp->fr_file;
18752 fixp->fx_line = fragp->fr_line;
18753
18754 /* Branch over the jump. */
4d68580a 18755 buf = write_compressed_insn (buf, insn, 4);
8484fb75 18756
df58fc94 18757 if (!compact)
8484fb75
MR
18758 {
18759 /* nop */
18760 if (insn32)
18761 buf = write_compressed_insn (buf, 0x00000000, 4);
18762 else
18763 buf = write_compressed_insn (buf, 0x0c00, 2);
18764 }
df58fc94
RS
18765 }
18766
ce8ad872 18767 if (!pic)
df58fc94 18768 {
7bd374a4
MR
18769 unsigned long jal = (short_ds || nods
18770 ? 0x74000000 : 0xf4000000); /* jal/s */
2309ddf2 18771
df58fc94
RS
18772 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
18773 insn = al ? jal : 0xd4000000;
18774
bbd27b76
MR
18775 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18776 fragp->fr_symbol, fragp->fr_offset,
18777 FALSE, BFD_RELOC_MICROMIPS_JMP);
df58fc94
RS
18778 fixp->fx_file = fragp->fr_file;
18779 fixp->fx_line = fragp->fr_line;
18780
4d68580a 18781 buf = write_compressed_insn (buf, insn, 4);
8484fb75 18782
7bd374a4 18783 if (compact || nods)
8484fb75
MR
18784 {
18785 /* nop */
18786 if (insn32)
18787 buf = write_compressed_insn (buf, 0x00000000, 4);
18788 else
18789 buf = write_compressed_insn (buf, 0x0c00, 2);
18790 }
df58fc94
RS
18791 }
18792 else
18793 {
18794 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
18795
18796 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
18797 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18798 insn |= at << MICROMIPSOP_SH_RT;
18799
bbd27b76
MR
18800 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18801 fragp->fr_symbol, fragp->fr_offset,
18802 FALSE, BFD_RELOC_MICROMIPS_GOT16);
df58fc94
RS
18803 fixp->fx_file = fragp->fr_file;
18804 fixp->fx_line = fragp->fr_line;
18805
4d68580a 18806 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
18807
18808 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
18809 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18810 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18811
bbd27b76
MR
18812 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18813 fragp->fr_symbol, fragp->fr_offset,
18814 FALSE, BFD_RELOC_MICROMIPS_LO16);
df58fc94
RS
18815 fixp->fx_file = fragp->fr_file;
18816 fixp->fx_line = fragp->fr_line;
18817
4d68580a 18818 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18819
8484fb75
MR
18820 if (insn32)
18821 {
18822 /* jr/jalr $at */
18823 insn = 0x00000f3c | (al ? RA : ZERO) << MICROMIPSOP_SH_RT;
18824 insn |= at << MICROMIPSOP_SH_RS;
18825
18826 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18827
7bd374a4 18828 if (compact || nods)
8484fb75
MR
18829 /* nop */
18830 buf = write_compressed_insn (buf, 0x00000000, 4);
18831 }
18832 else
18833 {
18834 /* jr/jrc/jalr/jalrs $at */
18835 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
7bd374a4 18836 unsigned long jr = compact || nods ? 0x45a0 : 0x4580; /* jr/c */
8484fb75
MR
18837
18838 insn = al ? jalr : jr;
18839 insn |= at << MICROMIPSOP_SH_MJ;
18840
18841 buf = write_compressed_insn (buf, insn, 2);
7bd374a4
MR
18842 if (al && nods)
18843 {
18844 /* nop */
18845 if (short_ds)
18846 buf = write_compressed_insn (buf, 0x0c00, 2);
18847 else
18848 buf = write_compressed_insn (buf, 0x00000000, 4);
18849 }
8484fb75 18850 }
df58fc94
RS
18851 }
18852
4d68580a 18853 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18854 return;
18855 }
18856
252b5132
RH
18857 if (RELAX_MIPS16_P (fragp->fr_subtype))
18858 {
18859 int type;
3ccad066 18860 const struct mips_int_operand *operand;
252b5132 18861 offsetT val;
5c04167a 18862 char *buf;
8507b6e7 18863 unsigned int user_length;
9d862524 18864 bfd_boolean need_reloc;
252b5132 18865 unsigned long insn;
8507b6e7 18866 bfd_boolean mac;
5c04167a 18867 bfd_boolean ext;
88a7ef16 18868 segT symsec;
252b5132
RH
18869
18870 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
3ccad066 18871 operand = mips16_immed_operand (type, FALSE);
252b5132 18872
8507b6e7 18873 mac = RELAX_MIPS16_MACRO (fragp->fr_subtype);
5c04167a 18874 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
88a7ef16 18875 val = resolve_symbol_value (fragp->fr_symbol) + fragp->fr_offset;
9d862524
MR
18876
18877 symsec = S_GET_SEGMENT (fragp->fr_symbol);
18878 need_reloc = (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
8507b6e7 18879 || (operand->root.type == OP_PCREL && !mac
9d862524
MR
18880 ? asec != symsec
18881 : !bfd_is_abs_section (symsec)));
18882
8507b6e7 18883 if (operand->root.type == OP_PCREL && !mac)
252b5132 18884 {
3ccad066 18885 const struct mips_pcrel_operand *pcrel_op;
252b5132 18886
3ccad066 18887 pcrel_op = (const struct mips_pcrel_operand *) operand;
252b5132 18888
14f72d45 18889 if (pcrel_op->include_isa_bit && !need_reloc)
252b5132 18890 {
37b2d327
MR
18891 if (!mips_ignore_branch_isa
18892 && !ELF_ST_IS_MIPS16 (S_GET_OTHER (fragp->fr_symbol)))
14f72d45
MR
18893 as_bad_where (fragp->fr_file, fragp->fr_line,
18894 _("branch to a symbol in another ISA mode"));
18895 else if ((fragp->fr_offset & 0x1) != 0)
18896 as_bad_where (fragp->fr_file, fragp->fr_line,
18897 _("branch to misaligned address (0x%lx)"),
18898 (long) val);
252b5132 18899 }
252b5132 18900
14f72d45 18901 val = mips16_pcrel_val (fragp, pcrel_op, val, 0);
252b5132
RH
18902
18903 /* Make sure the section winds up with the alignment we have
18904 assumed. */
3ccad066
RS
18905 if (operand->shift > 0)
18906 record_alignment (asec, operand->shift);
252b5132
RH
18907 }
18908
8507b6e7
MR
18909 if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
18910 || RELAX_MIPS16_DSLOT (fragp->fr_subtype))
18911 {
18912 if (mac)
18913 as_warn_where (fragp->fr_file, fragp->fr_line,
18914 _("macro instruction expanded into multiple "
18915 "instructions in a branch delay slot"));
18916 else if (ext)
18917 as_warn_where (fragp->fr_file, fragp->fr_line,
18918 _("extended instruction in a branch delay slot"));
18919 }
18920 else if (RELAX_MIPS16_NOMACRO (fragp->fr_subtype) && mac)
252b5132 18921 as_warn_where (fragp->fr_file, fragp->fr_line,
8507b6e7
MR
18922 _("macro instruction expanded into multiple "
18923 "instructions"));
252b5132 18924
5c04167a 18925 buf = fragp->fr_literal + fragp->fr_fix;
252b5132 18926
4d68580a 18927 insn = read_compressed_insn (buf, 2);
5c04167a
RS
18928 if (ext)
18929 insn |= MIPS16_EXTEND;
252b5132 18930
5c04167a
RS
18931 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
18932 user_length = 4;
18933 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
18934 user_length = 2;
18935 else
18936 user_length = 0;
18937
8507b6e7 18938 if (mac)
c9775dde 18939 {
8507b6e7
MR
18940 unsigned long reg;
18941 unsigned long new;
18942 unsigned long op;
25499ac7 18943 bfd_boolean e2;
8507b6e7
MR
18944
18945 gas_assert (type == 'A' || type == 'B' || type == 'E');
18946 gas_assert (RELAX_MIPS16_SYM32 (fragp->fr_subtype));
c9775dde 18947
25499ac7
MR
18948 e2 = RELAX_MIPS16_E2 (fragp->fr_subtype);
18949
8507b6e7 18950 if (need_reloc)
c9775dde 18951 {
8507b6e7
MR
18952 fixS *fixp;
18953
18954 gas_assert (!RELAX_MIPS16_PIC (fragp->fr_subtype));
18955
18956 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18957 fragp->fr_symbol, fragp->fr_offset,
18958 FALSE, BFD_RELOC_MIPS16_HI16_S);
18959 fixp->fx_file = fragp->fr_file;
18960 fixp->fx_line = fragp->fr_line;
18961
25499ac7 18962 fixp = fix_new (fragp, buf - fragp->fr_literal + (e2 ? 4 : 8), 4,
8507b6e7
MR
18963 fragp->fr_symbol, fragp->fr_offset,
18964 FALSE, BFD_RELOC_MIPS16_LO16);
18965 fixp->fx_file = fragp->fr_file;
18966 fixp->fx_line = fragp->fr_line;
18967
18968 val = 0;
18969 }
18970
18971 switch (insn & 0xf800)
18972 {
18973 case 0x0800: /* ADDIU */
18974 reg = (insn >> 8) & 0x7;
18975 op = 0xf0004800 | (reg << 8);
c9775dde 18976 break;
8507b6e7
MR
18977 case 0xb000: /* LW */
18978 reg = (insn >> 8) & 0x7;
18979 op = 0xf0009800 | (reg << 8) | (reg << 5);
c9775dde 18980 break;
8507b6e7
MR
18981 case 0xf800: /* I64 */
18982 reg = (insn >> 5) & 0x7;
18983 switch (insn & 0x0700)
18984 {
18985 case 0x0400: /* LD */
18986 op = 0xf0003800 | (reg << 8) | (reg << 5);
18987 break;
18988 case 0x0600: /* DADDIU */
18989 op = 0xf000fd00 | (reg << 5);
18990 break;
18991 default:
18992 abort ();
18993 }
18994 break;
18995 default:
18996 abort ();
c9775dde 18997 }
8507b6e7 18998
25499ac7 18999 new = (e2 ? 0xf0006820 : 0xf0006800) | (reg << 8); /* LUI/LI */
8507b6e7
MR
19000 new |= mips16_immed_extend ((val + 0x8000) >> 16, 16);
19001 buf = write_compressed_insn (buf, new, 4);
25499ac7
MR
19002 if (!e2)
19003 {
19004 new = 0xf4003000 | (reg << 8) | (reg << 5); /* SLL */
19005 buf = write_compressed_insn (buf, new, 4);
19006 }
8507b6e7
MR
19007 op |= mips16_immed_extend (val, 16);
19008 buf = write_compressed_insn (buf, op, 4);
19009
25499ac7 19010 fragp->fr_fix += e2 ? 8 : 12;
8507b6e7
MR
19011 }
19012 else
19013 {
19014 unsigned int length = ext ? 4 : 2;
19015
19016 if (need_reloc)
c9775dde 19017 {
8507b6e7 19018 bfd_reloc_code_real_type reloc = BFD_RELOC_NONE;
8507b6e7 19019 fixS *fixp;
c9775dde 19020
8507b6e7
MR
19021 switch (type)
19022 {
19023 case 'p':
19024 case 'q':
19025 reloc = BFD_RELOC_MIPS16_16_PCREL_S1;
19026 break;
19027 default:
19028 break;
19029 }
19030 if (mac || reloc == BFD_RELOC_NONE)
19031 as_bad_where (fragp->fr_file, fragp->fr_line,
19032 _("unsupported relocation"));
19033 else if (ext)
19034 {
bbd27b76
MR
19035 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
19036 fragp->fr_symbol, fragp->fr_offset,
19037 TRUE, reloc);
8507b6e7
MR
19038 fixp->fx_file = fragp->fr_file;
19039 fixp->fx_line = fragp->fr_line;
19040 }
19041 else
19042 as_bad_where (fragp->fr_file, fragp->fr_line,
19043 _("invalid unextended operand value"));
c9775dde 19044 }
eefc3365 19045 else
8507b6e7
MR
19046 mips16_immed (fragp->fr_file, fragp->fr_line, type,
19047 BFD_RELOC_UNUSED, val, user_length, &insn);
252b5132 19048
8507b6e7
MR
19049 gas_assert (mips16_opcode_length (insn) == length);
19050 write_compressed_insn (buf, insn, length);
19051 fragp->fr_fix += length;
19052 }
252b5132
RH
19053 }
19054 else
19055 {
df58fc94
RS
19056 relax_substateT subtype = fragp->fr_subtype;
19057 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
19058 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
4d7206a2
RS
19059 int first, second;
19060 fixS *fixp;
252b5132 19061
df58fc94
RS
19062 first = RELAX_FIRST (subtype);
19063 second = RELAX_SECOND (subtype);
4d7206a2 19064 fixp = (fixS *) fragp->fr_opcode;
252b5132 19065
df58fc94
RS
19066 /* If the delay slot chosen does not match the size of the instruction,
19067 then emit a warning. */
19068 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
19069 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
19070 {
19071 relax_substateT s;
19072 const char *msg;
19073
19074 s = subtype & (RELAX_DELAY_SLOT_16BIT
19075 | RELAX_DELAY_SLOT_SIZE_FIRST
19076 | RELAX_DELAY_SLOT_SIZE_SECOND);
19077 msg = macro_warning (s);
19078 if (msg != NULL)
db9b2be4 19079 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94
RS
19080 subtype &= ~s;
19081 }
19082
584892a6 19083 /* Possibly emit a warning if we've chosen the longer option. */
df58fc94 19084 if (use_second == second_longer)
584892a6 19085 {
df58fc94
RS
19086 relax_substateT s;
19087 const char *msg;
19088
19089 s = (subtype
19090 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
19091 msg = macro_warning (s);
19092 if (msg != NULL)
db9b2be4 19093 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94 19094 subtype &= ~s;
584892a6
RS
19095 }
19096
4d7206a2
RS
19097 /* Go through all the fixups for the first sequence. Disable them
19098 (by marking them as done) if we're going to use the second
19099 sequence instead. */
19100 while (fixp
19101 && fixp->fx_frag == fragp
19102 && fixp->fx_where < fragp->fr_fix - second)
19103 {
df58fc94 19104 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
19105 fixp->fx_done = 1;
19106 fixp = fixp->fx_next;
19107 }
252b5132 19108
4d7206a2
RS
19109 /* Go through the fixups for the second sequence. Disable them if
19110 we're going to use the first sequence, otherwise adjust their
19111 addresses to account for the relaxation. */
19112 while (fixp && fixp->fx_frag == fragp)
19113 {
df58fc94 19114 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
19115 fixp->fx_where -= first;
19116 else
19117 fixp->fx_done = 1;
19118 fixp = fixp->fx_next;
19119 }
19120
19121 /* Now modify the frag contents. */
df58fc94 19122 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
19123 {
19124 char *start;
19125
19126 start = fragp->fr_literal + fragp->fr_fix - first - second;
19127 memmove (start, start + first, second);
19128 fragp->fr_fix -= first;
19129 }
19130 else
19131 fragp->fr_fix -= second;
252b5132
RH
19132 }
19133}
19134
252b5132
RH
19135/* This function is called after the relocs have been generated.
19136 We've been storing mips16 text labels as odd. Here we convert them
19137 back to even for the convenience of the debugger. */
19138
19139void
17a2f251 19140mips_frob_file_after_relocs (void)
252b5132
RH
19141{
19142 asymbol **syms;
19143 unsigned int count, i;
19144
252b5132
RH
19145 syms = bfd_get_outsymbols (stdoutput);
19146 count = bfd_get_symcount (stdoutput);
19147 for (i = 0; i < count; i++, syms++)
df58fc94
RS
19148 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
19149 && ((*syms)->value & 1) != 0)
19150 {
19151 (*syms)->value &= ~1;
19152 /* If the symbol has an odd size, it was probably computed
19153 incorrectly, so adjust that as well. */
19154 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
19155 ++elf_symbol (*syms)->internal_elf_sym.st_size;
19156 }
252b5132
RH
19157}
19158
a1facbec
MR
19159/* This function is called whenever a label is defined, including fake
19160 labels instantiated off the dot special symbol. It is used when
19161 handling branch delays; if a branch has a label, we assume we cannot
19162 move it. This also bumps the value of the symbol by 1 in compressed
19163 code. */
252b5132 19164
e1b47bd5 19165static void
a1facbec 19166mips_record_label (symbolS *sym)
252b5132 19167{
a8dbcb85 19168 segment_info_type *si = seg_info (now_seg);
252b5132
RH
19169 struct insn_label_list *l;
19170
19171 if (free_insn_labels == NULL)
325801bd 19172 l = XNEW (struct insn_label_list);
252b5132
RH
19173 else
19174 {
19175 l = free_insn_labels;
19176 free_insn_labels = l->next;
19177 }
19178
19179 l->label = sym;
a8dbcb85
TS
19180 l->next = si->label_list;
19181 si->label_list = l;
a1facbec 19182}
07a53e5c 19183
a1facbec
MR
19184/* This function is called as tc_frob_label() whenever a label is defined
19185 and adds a DWARF-2 record we only want for true labels. */
19186
19187void
19188mips_define_label (symbolS *sym)
19189{
19190 mips_record_label (sym);
07a53e5c 19191 dwarf2_emit_label (sym);
252b5132 19192}
e1b47bd5
RS
19193
19194/* This function is called by tc_new_dot_label whenever a new dot symbol
19195 is defined. */
19196
19197void
19198mips_add_dot_label (symbolS *sym)
19199{
19200 mips_record_label (sym);
19201 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
19202 mips_compressed_mark_label (sym);
19203}
252b5132 19204\f
351cdf24
MF
19205/* Converting ASE flags from internal to .MIPS.abiflags values. */
19206static unsigned int
19207mips_convert_ase_flags (int ase)
19208{
19209 unsigned int ext_ases = 0;
19210
19211 if (ase & ASE_DSP)
19212 ext_ases |= AFL_ASE_DSP;
19213 if (ase & ASE_DSPR2)
19214 ext_ases |= AFL_ASE_DSPR2;
8f4f9071
MF
19215 if (ase & ASE_DSPR3)
19216 ext_ases |= AFL_ASE_DSPR3;
351cdf24
MF
19217 if (ase & ASE_EVA)
19218 ext_ases |= AFL_ASE_EVA;
19219 if (ase & ASE_MCU)
19220 ext_ases |= AFL_ASE_MCU;
19221 if (ase & ASE_MDMX)
19222 ext_ases |= AFL_ASE_MDMX;
19223 if (ase & ASE_MIPS3D)
19224 ext_ases |= AFL_ASE_MIPS3D;
19225 if (ase & ASE_MT)
19226 ext_ases |= AFL_ASE_MT;
19227 if (ase & ASE_SMARTMIPS)
19228 ext_ases |= AFL_ASE_SMARTMIPS;
19229 if (ase & ASE_VIRT)
19230 ext_ases |= AFL_ASE_VIRT;
19231 if (ase & ASE_MSA)
19232 ext_ases |= AFL_ASE_MSA;
19233 if (ase & ASE_XPA)
19234 ext_ases |= AFL_ASE_XPA;
25499ac7
MR
19235 if (ase & ASE_MIPS16E2)
19236 ext_ases |= file_ase_mips16 ? AFL_ASE_MIPS16E2 : 0;
730c3174
SE
19237 if (ase & ASE_CRC)
19238 ext_ases |= AFL_ASE_CRC;
6f20c942
FS
19239 if (ase & ASE_GINV)
19240 ext_ases |= AFL_ASE_GINV;
8095d2f7
CX
19241 if (ase & ASE_LOONGSON_MMI)
19242 ext_ases |= AFL_ASE_LOONGSON_MMI;
716c08de
CX
19243 if (ase & ASE_LOONGSON_CAM)
19244 ext_ases |= AFL_ASE_LOONGSON_CAM;
bdc6c06e
CX
19245 if (ase & ASE_LOONGSON_EXT)
19246 ext_ases |= AFL_ASE_LOONGSON_EXT;
a693765e
CX
19247 if (ase & ASE_LOONGSON_EXT2)
19248 ext_ases |= AFL_ASE_LOONGSON_EXT2;
351cdf24
MF
19249
19250 return ext_ases;
19251}
252b5132
RH
19252/* Some special processing for a MIPS ELF file. */
19253
19254void
17a2f251 19255mips_elf_final_processing (void)
252b5132 19256{
351cdf24
MF
19257 int fpabi;
19258 Elf_Internal_ABIFlags_v0 flags;
19259
19260 flags.version = 0;
19261 flags.isa_rev = 0;
19262 switch (file_mips_opts.isa)
19263 {
19264 case INSN_ISA1:
19265 flags.isa_level = 1;
19266 break;
19267 case INSN_ISA2:
19268 flags.isa_level = 2;
19269 break;
19270 case INSN_ISA3:
19271 flags.isa_level = 3;
19272 break;
19273 case INSN_ISA4:
19274 flags.isa_level = 4;
19275 break;
19276 case INSN_ISA5:
19277 flags.isa_level = 5;
19278 break;
19279 case INSN_ISA32:
19280 flags.isa_level = 32;
19281 flags.isa_rev = 1;
19282 break;
19283 case INSN_ISA32R2:
19284 flags.isa_level = 32;
19285 flags.isa_rev = 2;
19286 break;
19287 case INSN_ISA32R3:
19288 flags.isa_level = 32;
19289 flags.isa_rev = 3;
19290 break;
19291 case INSN_ISA32R5:
19292 flags.isa_level = 32;
19293 flags.isa_rev = 5;
19294 break;
09c14161
MF
19295 case INSN_ISA32R6:
19296 flags.isa_level = 32;
19297 flags.isa_rev = 6;
19298 break;
351cdf24
MF
19299 case INSN_ISA64:
19300 flags.isa_level = 64;
19301 flags.isa_rev = 1;
19302 break;
19303 case INSN_ISA64R2:
19304 flags.isa_level = 64;
19305 flags.isa_rev = 2;
19306 break;
19307 case INSN_ISA64R3:
19308 flags.isa_level = 64;
19309 flags.isa_rev = 3;
19310 break;
19311 case INSN_ISA64R5:
19312 flags.isa_level = 64;
19313 flags.isa_rev = 5;
19314 break;
09c14161
MF
19315 case INSN_ISA64R6:
19316 flags.isa_level = 64;
19317 flags.isa_rev = 6;
19318 break;
351cdf24
MF
19319 }
19320
19321 flags.gpr_size = file_mips_opts.gp == 32 ? AFL_REG_32 : AFL_REG_64;
19322 flags.cpr1_size = file_mips_opts.soft_float ? AFL_REG_NONE
19323 : (file_mips_opts.ase & ASE_MSA) ? AFL_REG_128
19324 : (file_mips_opts.fp == 64) ? AFL_REG_64
19325 : AFL_REG_32;
19326 flags.cpr2_size = AFL_REG_NONE;
19327 flags.fp_abi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19328 Tag_GNU_MIPS_ABI_FP);
19329 flags.isa_ext = bfd_mips_isa_ext (stdoutput);
19330 flags.ases = mips_convert_ase_flags (file_mips_opts.ase);
19331 if (file_ase_mips16)
19332 flags.ases |= AFL_ASE_MIPS16;
19333 if (file_ase_micromips)
19334 flags.ases |= AFL_ASE_MICROMIPS;
19335 flags.flags1 = 0;
19336 if ((ISA_HAS_ODD_SINGLE_FPR (file_mips_opts.isa, file_mips_opts.arch)
19337 || file_mips_opts.fp == 64)
19338 && file_mips_opts.oddspreg)
19339 flags.flags1 |= AFL_FLAGS1_ODDSPREG;
19340 flags.flags2 = 0;
19341
19342 bfd_mips_elf_swap_abiflags_v0_out (stdoutput, &flags,
19343 ((Elf_External_ABIFlags_v0 *)
19344 mips_flags_frag));
19345
252b5132 19346 /* Write out the register information. */
316f5878 19347 if (mips_abi != N64_ABI)
252b5132
RH
19348 {
19349 Elf32_RegInfo s;
19350
19351 s.ri_gprmask = mips_gprmask;
19352 s.ri_cprmask[0] = mips_cprmask[0];
19353 s.ri_cprmask[1] = mips_cprmask[1];
19354 s.ri_cprmask[2] = mips_cprmask[2];
19355 s.ri_cprmask[3] = mips_cprmask[3];
19356 /* The gp_value field is set by the MIPS ELF backend. */
19357
19358 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
19359 ((Elf32_External_RegInfo *)
19360 mips_regmask_frag));
19361 }
19362 else
19363 {
19364 Elf64_Internal_RegInfo s;
19365
19366 s.ri_gprmask = mips_gprmask;
19367 s.ri_pad = 0;
19368 s.ri_cprmask[0] = mips_cprmask[0];
19369 s.ri_cprmask[1] = mips_cprmask[1];
19370 s.ri_cprmask[2] = mips_cprmask[2];
19371 s.ri_cprmask[3] = mips_cprmask[3];
19372 /* The gp_value field is set by the MIPS ELF backend. */
19373
19374 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
19375 ((Elf64_External_RegInfo *)
19376 mips_regmask_frag));
19377 }
19378
19379 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
19380 sort of BFD interface for this. */
19381 if (mips_any_noreorder)
19382 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
19383 if (mips_pic != NO_PIC)
143d77c5 19384 {
8b828383 19385 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
19386 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
19387 }
19388 if (mips_abicalls)
19389 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 19390
b015e599
AP
19391 /* Set MIPS ELF flags for ASEs. Note that not all ASEs have flags
19392 defined at present; this might need to change in future. */
a4672219
TS
19393 if (file_ase_mips16)
19394 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
df58fc94
RS
19395 if (file_ase_micromips)
19396 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
919731af 19397 if (file_mips_opts.ase & ASE_MDMX)
deec1734 19398 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 19399
bdaaa2e1 19400 /* Set the MIPS ELF ABI flags. */
316f5878 19401 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 19402 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 19403 else if (mips_abi == O64_ABI)
252b5132 19404 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 19405 else if (mips_abi == EABI_ABI)
252b5132 19406 {
bad1aba3 19407 if (file_mips_opts.gp == 64)
252b5132
RH
19408 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
19409 else
19410 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
19411 }
be00bddd 19412
defc8e2b 19413 /* Nothing to do for N32_ABI or N64_ABI. */
252b5132
RH
19414
19415 if (mips_32bitmode)
19416 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
ad3fea08 19417
7361da2c 19418 if (mips_nan2008 == 1)
ba92f887
MR
19419 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NAN2008;
19420
ad3fea08 19421 /* 32 bit code with 64 bit FP registers. */
351cdf24
MF
19422 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19423 Tag_GNU_MIPS_ABI_FP);
19424 if (fpabi == Val_GNU_MIPS_ABI_FP_OLD_64)
f1c38003 19425 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_FP64;
252b5132 19426}
252b5132 19427\f
beae10d5 19428typedef struct proc {
9b2f1d35
EC
19429 symbolS *func_sym;
19430 symbolS *func_end_sym;
beae10d5
KH
19431 unsigned long reg_mask;
19432 unsigned long reg_offset;
19433 unsigned long fpreg_mask;
19434 unsigned long fpreg_offset;
19435 unsigned long frame_offset;
19436 unsigned long frame_reg;
19437 unsigned long pc_reg;
19438} procS;
252b5132
RH
19439
19440static procS cur_proc;
19441static procS *cur_proc_ptr;
19442static int numprocs;
19443
df58fc94
RS
19444/* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
19445 as "2", and a normal nop as "0". */
19446
19447#define NOP_OPCODE_MIPS 0
19448#define NOP_OPCODE_MIPS16 1
19449#define NOP_OPCODE_MICROMIPS 2
742a56fe
RS
19450
19451char
19452mips_nop_opcode (void)
19453{
df58fc94
RS
19454 if (seg_info (now_seg)->tc_segment_info_data.micromips)
19455 return NOP_OPCODE_MICROMIPS;
19456 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
19457 return NOP_OPCODE_MIPS16;
19458 else
19459 return NOP_OPCODE_MIPS;
742a56fe
RS
19460}
19461
df58fc94
RS
19462/* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
19463 32-bit microMIPS NOPs here (if applicable). */
a19d8eb0 19464
0a9ef439 19465void
17a2f251 19466mips_handle_align (fragS *fragp)
a19d8eb0 19467{
df58fc94 19468 char nop_opcode;
742a56fe 19469 char *p;
c67a084a
NC
19470 int bytes, size, excess;
19471 valueT opcode;
742a56fe 19472
0a9ef439
RH
19473 if (fragp->fr_type != rs_align_code)
19474 return;
19475
742a56fe 19476 p = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
19477 nop_opcode = *p;
19478 switch (nop_opcode)
a19d8eb0 19479 {
df58fc94
RS
19480 case NOP_OPCODE_MICROMIPS:
19481 opcode = micromips_nop32_insn.insn_opcode;
19482 size = 4;
19483 break;
19484 case NOP_OPCODE_MIPS16:
c67a084a
NC
19485 opcode = mips16_nop_insn.insn_opcode;
19486 size = 2;
df58fc94
RS
19487 break;
19488 case NOP_OPCODE_MIPS:
19489 default:
c67a084a
NC
19490 opcode = nop_insn.insn_opcode;
19491 size = 4;
df58fc94 19492 break;
c67a084a 19493 }
a19d8eb0 19494
c67a084a
NC
19495 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
19496 excess = bytes % size;
df58fc94
RS
19497
19498 /* Handle the leading part if we're not inserting a whole number of
19499 instructions, and make it the end of the fixed part of the frag.
19500 Try to fit in a short microMIPS NOP if applicable and possible,
19501 and use zeroes otherwise. */
19502 gas_assert (excess < 4);
19503 fragp->fr_fix += excess;
19504 switch (excess)
c67a084a 19505 {
df58fc94
RS
19506 case 3:
19507 *p++ = '\0';
19508 /* Fall through. */
19509 case 2:
833794fc 19510 if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
df58fc94 19511 {
4d68580a 19512 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
df58fc94
RS
19513 break;
19514 }
19515 *p++ = '\0';
19516 /* Fall through. */
19517 case 1:
19518 *p++ = '\0';
19519 /* Fall through. */
19520 case 0:
19521 break;
a19d8eb0 19522 }
c67a084a
NC
19523
19524 md_number_to_chars (p, opcode, size);
19525 fragp->fr_var = size;
a19d8eb0
CP
19526}
19527
252b5132 19528static long
17a2f251 19529get_number (void)
252b5132
RH
19530{
19531 int negative = 0;
19532 long val = 0;
19533
19534 if (*input_line_pointer == '-')
19535 {
19536 ++input_line_pointer;
19537 negative = 1;
19538 }
3882b010 19539 if (!ISDIGIT (*input_line_pointer))
956cd1d6 19540 as_bad (_("expected simple number"));
252b5132
RH
19541 if (input_line_pointer[0] == '0')
19542 {
19543 if (input_line_pointer[1] == 'x')
19544 {
19545 input_line_pointer += 2;
3882b010 19546 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
19547 {
19548 val <<= 4;
19549 val |= hex_value (*input_line_pointer++);
19550 }
19551 return negative ? -val : val;
19552 }
19553 else
19554 {
19555 ++input_line_pointer;
3882b010 19556 while (ISDIGIT (*input_line_pointer))
252b5132
RH
19557 {
19558 val <<= 3;
19559 val |= *input_line_pointer++ - '0';
19560 }
19561 return negative ? -val : val;
19562 }
19563 }
3882b010 19564 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
19565 {
19566 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
19567 *input_line_pointer, *input_line_pointer);
956cd1d6 19568 as_warn (_("invalid number"));
252b5132
RH
19569 return -1;
19570 }
3882b010 19571 while (ISDIGIT (*input_line_pointer))
252b5132
RH
19572 {
19573 val *= 10;
19574 val += *input_line_pointer++ - '0';
19575 }
19576 return negative ? -val : val;
19577}
19578
19579/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
19580 is an initial number which is the ECOFF file index. In the non-ECOFF
19581 case .file implies DWARF-2. */
19582
19583static void
17a2f251 19584s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 19585{
ecb4347a
DJ
19586 static int first_file_directive = 0;
19587
c5dd6aab
DJ
19588 if (ECOFF_DEBUGGING)
19589 {
19590 get_number ();
19591 s_app_file (0);
19592 }
19593 else
ecb4347a
DJ
19594 {
19595 char *filename;
19596
68d20676 19597 filename = dwarf2_directive_filename ();
ecb4347a
DJ
19598
19599 /* Versions of GCC up to 3.1 start files with a ".file"
19600 directive even for stabs output. Make sure that this
19601 ".file" is handled. Note that you need a version of GCC
19602 after 3.1 in order to support DWARF-2 on MIPS. */
19603 if (filename != NULL && ! first_file_directive)
19604 {
19605 (void) new_logical_line (filename, -1);
c04f5787 19606 s_app_file_string (filename, 0);
ecb4347a
DJ
19607 }
19608 first_file_directive = 1;
19609 }
c5dd6aab
DJ
19610}
19611
19612/* The .loc directive, implying DWARF-2. */
252b5132
RH
19613
19614static void
17a2f251 19615s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 19616{
c5dd6aab
DJ
19617 if (!ECOFF_DEBUGGING)
19618 dwarf2_directive_loc (0);
252b5132
RH
19619}
19620
252b5132
RH
19621/* The .end directive. */
19622
19623static void
17a2f251 19624s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
19625{
19626 symbolS *p;
252b5132 19627
7a621144
DJ
19628 /* Following functions need their own .frame and .cprestore directives. */
19629 mips_frame_reg_valid = 0;
19630 mips_cprestore_valid = 0;
19631
252b5132
RH
19632 if (!is_end_of_line[(unsigned char) *input_line_pointer])
19633 {
19634 p = get_symbol ();
19635 demand_empty_rest_of_line ();
19636 }
19637 else
19638 p = NULL;
19639
14949570 19640 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
19641 as_warn (_(".end not in text section"));
19642
19643 if (!cur_proc_ptr)
19644 {
1661c76c 19645 as_warn (_(".end directive without a preceding .ent directive"));
252b5132
RH
19646 demand_empty_rest_of_line ();
19647 return;
19648 }
19649
19650 if (p != NULL)
19651 {
9c2799c2 19652 gas_assert (S_GET_NAME (p));
9b2f1d35 19653 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
1661c76c 19654 as_warn (_(".end symbol does not match .ent symbol"));
ecb4347a
DJ
19655
19656 if (debug_type == DEBUG_STABS)
19657 stabs_generate_asm_endfunc (S_GET_NAME (p),
19658 S_GET_NAME (p));
252b5132
RH
19659 }
19660 else
19661 as_warn (_(".end directive missing or unknown symbol"));
19662
9b2f1d35
EC
19663 /* Create an expression to calculate the size of the function. */
19664 if (p && cur_proc_ptr)
19665 {
19666 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
325801bd 19667 expressionS *exp = XNEW (expressionS);
9b2f1d35
EC
19668
19669 obj->size = exp;
19670 exp->X_op = O_subtract;
19671 exp->X_add_symbol = symbol_temp_new_now ();
19672 exp->X_op_symbol = p;
19673 exp->X_add_number = 0;
19674
19675 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
19676 }
19677
5ff6a06c
MR
19678#ifdef md_flush_pending_output
19679 md_flush_pending_output ();
19680#endif
19681
ecb4347a 19682 /* Generate a .pdr section. */
f3ded42a 19683 if (!ECOFF_DEBUGGING && mips_flag_pdr)
ecb4347a
DJ
19684 {
19685 segT saved_seg = now_seg;
19686 subsegT saved_subseg = now_subseg;
ecb4347a
DJ
19687 expressionS exp;
19688 char *fragp;
252b5132 19689
9c2799c2 19690 gas_assert (pdr_seg);
ecb4347a 19691 subseg_set (pdr_seg, 0);
252b5132 19692
ecb4347a
DJ
19693 /* Write the symbol. */
19694 exp.X_op = O_symbol;
19695 exp.X_add_symbol = p;
19696 exp.X_add_number = 0;
19697 emit_expr (&exp, 4);
252b5132 19698
ecb4347a 19699 fragp = frag_more (7 * 4);
252b5132 19700
17a2f251
TS
19701 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19702 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19703 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19704 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19705 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19706 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19707 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 19708
ecb4347a
DJ
19709 subseg_set (saved_seg, saved_subseg);
19710 }
252b5132
RH
19711
19712 cur_proc_ptr = NULL;
19713}
19714
19715/* The .aent and .ent directives. */
19716
19717static void
17a2f251 19718s_mips_ent (int aent)
252b5132 19719{
252b5132 19720 symbolS *symbolP;
252b5132
RH
19721
19722 symbolP = get_symbol ();
19723 if (*input_line_pointer == ',')
f9419b05 19724 ++input_line_pointer;
252b5132 19725 SKIP_WHITESPACE ();
3882b010 19726 if (ISDIGIT (*input_line_pointer)
d9a62219 19727 || *input_line_pointer == '-')
874e8986 19728 get_number ();
252b5132 19729
14949570 19730 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
1661c76c 19731 as_warn (_(".ent or .aent not in text section"));
252b5132
RH
19732
19733 if (!aent && cur_proc_ptr)
9a41af64 19734 as_warn (_("missing .end"));
252b5132
RH
19735
19736 if (!aent)
19737 {
7a621144
DJ
19738 /* This function needs its own .frame and .cprestore directives. */
19739 mips_frame_reg_valid = 0;
19740 mips_cprestore_valid = 0;
19741
252b5132
RH
19742 cur_proc_ptr = &cur_proc;
19743 memset (cur_proc_ptr, '\0', sizeof (procS));
19744
9b2f1d35 19745 cur_proc_ptr->func_sym = symbolP;
252b5132 19746
f9419b05 19747 ++numprocs;
ecb4347a
DJ
19748
19749 if (debug_type == DEBUG_STABS)
19750 stabs_generate_asm_func (S_GET_NAME (symbolP),
19751 S_GET_NAME (symbolP));
252b5132
RH
19752 }
19753
7c0fc524
MR
19754 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19755
252b5132
RH
19756 demand_empty_rest_of_line ();
19757}
19758
19759/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 19760 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 19761 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 19762 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
19763 symbol table (in the mdebug section). */
19764
19765static void
17a2f251 19766s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 19767{
f3ded42a
RS
19768 if (ECOFF_DEBUGGING)
19769 s_ignore (ignore);
19770 else
ecb4347a
DJ
19771 {
19772 long val;
252b5132 19773
ecb4347a
DJ
19774 if (cur_proc_ptr == (procS *) NULL)
19775 {
19776 as_warn (_(".frame outside of .ent"));
19777 demand_empty_rest_of_line ();
19778 return;
19779 }
252b5132 19780
ecb4347a
DJ
19781 cur_proc_ptr->frame_reg = tc_get_register (1);
19782
19783 SKIP_WHITESPACE ();
19784 if (*input_line_pointer++ != ','
19785 || get_absolute_expression_and_terminator (&val) != ',')
19786 {
1661c76c 19787 as_warn (_("bad .frame directive"));
ecb4347a
DJ
19788 --input_line_pointer;
19789 demand_empty_rest_of_line ();
19790 return;
19791 }
252b5132 19792
ecb4347a
DJ
19793 cur_proc_ptr->frame_offset = val;
19794 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 19795
252b5132 19796 demand_empty_rest_of_line ();
252b5132 19797 }
252b5132
RH
19798}
19799
bdaaa2e1
KH
19800/* The .fmask and .mask directives. If the mdebug section is present
19801 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 19802 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 19803 information correctly. We can't use the ecoff routines because they
252b5132
RH
19804 make reference to the ecoff symbol table (in the mdebug section). */
19805
19806static void
17a2f251 19807s_mips_mask (int reg_type)
252b5132 19808{
f3ded42a
RS
19809 if (ECOFF_DEBUGGING)
19810 s_ignore (reg_type);
19811 else
252b5132 19812 {
ecb4347a 19813 long mask, off;
252b5132 19814
ecb4347a
DJ
19815 if (cur_proc_ptr == (procS *) NULL)
19816 {
19817 as_warn (_(".mask/.fmask outside of .ent"));
19818 demand_empty_rest_of_line ();
19819 return;
19820 }
252b5132 19821
ecb4347a
DJ
19822 if (get_absolute_expression_and_terminator (&mask) != ',')
19823 {
1661c76c 19824 as_warn (_("bad .mask/.fmask directive"));
ecb4347a
DJ
19825 --input_line_pointer;
19826 demand_empty_rest_of_line ();
19827 return;
19828 }
252b5132 19829
ecb4347a
DJ
19830 off = get_absolute_expression ();
19831
19832 if (reg_type == 'F')
19833 {
19834 cur_proc_ptr->fpreg_mask = mask;
19835 cur_proc_ptr->fpreg_offset = off;
19836 }
19837 else
19838 {
19839 cur_proc_ptr->reg_mask = mask;
19840 cur_proc_ptr->reg_offset = off;
19841 }
19842
19843 demand_empty_rest_of_line ();
252b5132 19844 }
252b5132
RH
19845}
19846
316f5878
RS
19847/* A table describing all the processors gas knows about. Names are
19848 matched in the order listed.
e7af610e 19849
316f5878
RS
19850 To ease comparison, please keep this table in the same order as
19851 gcc's mips_cpu_info_table[]. */
e972090a
NC
19852static const struct mips_cpu_info mips_cpu_info_table[] =
19853{
6f2117ba 19854 /* Entries for generic ISAs. */
d16afab6
RS
19855 { "mips1", MIPS_CPU_IS_ISA, 0, ISA_MIPS1, CPU_R3000 },
19856 { "mips2", MIPS_CPU_IS_ISA, 0, ISA_MIPS2, CPU_R6000 },
19857 { "mips3", MIPS_CPU_IS_ISA, 0, ISA_MIPS3, CPU_R4000 },
19858 { "mips4", MIPS_CPU_IS_ISA, 0, ISA_MIPS4, CPU_R8000 },
19859 { "mips5", MIPS_CPU_IS_ISA, 0, ISA_MIPS5, CPU_MIPS5 },
19860 { "mips32", MIPS_CPU_IS_ISA, 0, ISA_MIPS32, CPU_MIPS32 },
19861 { "mips32r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
ae52f483
AB
19862 { "mips32r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R3, CPU_MIPS32R3 },
19863 { "mips32r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R5, CPU_MIPS32R5 },
7361da2c 19864 { "mips32r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R6, CPU_MIPS32R6 },
d16afab6
RS
19865 { "mips64", MIPS_CPU_IS_ISA, 0, ISA_MIPS64, CPU_MIPS64 },
19866 { "mips64r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R2, CPU_MIPS64R2 },
ae52f483
AB
19867 { "mips64r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R3, CPU_MIPS64R3 },
19868 { "mips64r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R5, CPU_MIPS64R5 },
7361da2c 19869 { "mips64r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R6, CPU_MIPS64R6 },
316f5878
RS
19870
19871 /* MIPS I */
d16afab6
RS
19872 { "r3000", 0, 0, ISA_MIPS1, CPU_R3000 },
19873 { "r2000", 0, 0, ISA_MIPS1, CPU_R3000 },
19874 { "r3900", 0, 0, ISA_MIPS1, CPU_R3900 },
316f5878
RS
19875
19876 /* MIPS II */
d16afab6 19877 { "r6000", 0, 0, ISA_MIPS2, CPU_R6000 },
316f5878
RS
19878
19879 /* MIPS III */
d16afab6
RS
19880 { "r4000", 0, 0, ISA_MIPS3, CPU_R4000 },
19881 { "r4010", 0, 0, ISA_MIPS2, CPU_R4010 },
19882 { "vr4100", 0, 0, ISA_MIPS3, CPU_VR4100 },
19883 { "vr4111", 0, 0, ISA_MIPS3, CPU_R4111 },
19884 { "vr4120", 0, 0, ISA_MIPS3, CPU_VR4120 },
19885 { "vr4130", 0, 0, ISA_MIPS3, CPU_VR4120 },
19886 { "vr4181", 0, 0, ISA_MIPS3, CPU_R4111 },
19887 { "vr4300", 0, 0, ISA_MIPS3, CPU_R4300 },
19888 { "r4400", 0, 0, ISA_MIPS3, CPU_R4400 },
19889 { "r4600", 0, 0, ISA_MIPS3, CPU_R4600 },
19890 { "orion", 0, 0, ISA_MIPS3, CPU_R4600 },
19891 { "r4650", 0, 0, ISA_MIPS3, CPU_R4650 },
19892 { "r5900", 0, 0, ISA_MIPS3, CPU_R5900 },
6f2117ba 19893 /* ST Microelectronics Loongson 2E and 2F cores. */
d16afab6 19894 { "loongson2e", 0, 0, ISA_MIPS3, CPU_LOONGSON_2E },
8095d2f7 19895 { "loongson2f", 0, ASE_LOONGSON_MMI, ISA_MIPS3, CPU_LOONGSON_2F },
316f5878
RS
19896
19897 /* MIPS IV */
d16afab6
RS
19898 { "r8000", 0, 0, ISA_MIPS4, CPU_R8000 },
19899 { "r10000", 0, 0, ISA_MIPS4, CPU_R10000 },
19900 { "r12000", 0, 0, ISA_MIPS4, CPU_R12000 },
19901 { "r14000", 0, 0, ISA_MIPS4, CPU_R14000 },
19902 { "r16000", 0, 0, ISA_MIPS4, CPU_R16000 },
19903 { "vr5000", 0, 0, ISA_MIPS4, CPU_R5000 },
19904 { "vr5400", 0, 0, ISA_MIPS4, CPU_VR5400 },
19905 { "vr5500", 0, 0, ISA_MIPS4, CPU_VR5500 },
19906 { "rm5200", 0, 0, ISA_MIPS4, CPU_R5000 },
19907 { "rm5230", 0, 0, ISA_MIPS4, CPU_R5000 },
19908 { "rm5231", 0, 0, ISA_MIPS4, CPU_R5000 },
19909 { "rm5261", 0, 0, ISA_MIPS4, CPU_R5000 },
19910 { "rm5721", 0, 0, ISA_MIPS4, CPU_R5000 },
19911 { "rm7000", 0, 0, ISA_MIPS4, CPU_RM7000 },
19912 { "rm9000", 0, 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
19913
19914 /* MIPS 32 */
d16afab6
RS
19915 { "4kc", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19916 { "4km", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19917 { "4kp", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19918 { "4ksc", 0, ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
ad3fea08
TS
19919
19920 /* MIPS 32 Release 2 */
d16afab6
RS
19921 { "4kec", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19922 { "4kem", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19923 { "4kep", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19924 { "4ksd", 0, ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
19925 { "m4k", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19926 { "m4kp", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19927 { "m14k", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19928 { "m14kc", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19929 { "m14ke", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19930 ISA_MIPS32R2, CPU_MIPS32R2 },
19931 { "m14kec", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19932 ISA_MIPS32R2, CPU_MIPS32R2 },
19933 { "24kc", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19934 { "24kf2_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19935 { "24kf", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19936 { "24kf1_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19937 /* Deprecated forms of the above. */
d16afab6
RS
19938 { "24kfx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19939 { "24kx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19940 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
d16afab6
RS
19941 { "24kec", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19942 { "24kef2_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19943 { "24kef", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19944 { "24kef1_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19945 /* Deprecated forms of the above. */
d16afab6
RS
19946 { "24kefx", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19947 { "24kex", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19948 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
d16afab6
RS
19949 { "34kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19950 { "34kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19951 { "34kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19952 { "34kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19953 /* Deprecated forms of the above. */
d16afab6
RS
19954 { "34kfx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19955 { "34kx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
711eefe4 19956 /* 34Kn is a 34kc without DSP. */
d16afab6 19957 { "34kn", 0, ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19958 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
d16afab6
RS
19959 { "74kc", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19960 { "74kf2_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19961 { "74kf", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19962 { "74kf1_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19963 { "74kf3_2", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19964 /* Deprecated forms of the above. */
d16afab6
RS
19965 { "74kfx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19966 { "74kx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
30f8113a 19967 /* 1004K cores are multiprocessor versions of the 34K. */
d16afab6
RS
19968 { "1004kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19969 { "1004kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19970 { "1004kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19971 { "1004kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
6f2117ba 19972 /* interaptiv is the new name for 1004kf. */
77403ce9 19973 { "interaptiv", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
38bf472a
MR
19974 { "interaptiv-mr2", 0,
19975 ASE_DSP | ASE_EVA | ASE_MT | ASE_MIPS16E2 | ASE_MIPS16E2_MT,
19976 ISA_MIPS32R3, CPU_INTERAPTIV_MR2 },
6f2117ba 19977 /* M5100 family. */
c6e5c03a
RS
19978 { "m5100", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
19979 { "m5101", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
bbaa46c0 19980 /* P5600 with EVA and Virtualization ASEs, other ASEs are optional. */
134c0c8b 19981 { "p5600", 0, ASE_VIRT | ASE_EVA | ASE_XPA, ISA_MIPS32R5, CPU_MIPS32R5 },
32b26a03 19982
316f5878 19983 /* MIPS 64 */
d16afab6
RS
19984 { "5kc", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19985 { "5kf", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19986 { "20kc", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
19987 { "25kf", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
ad3fea08 19988
6f2117ba 19989 /* Broadcom SB-1 CPU core. */
d16afab6 19990 { "sb1", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
6f2117ba 19991 /* Broadcom SB-1A CPU core. */
d16afab6 19992 { "sb1a", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
3739860c 19993
6f2117ba
PH
19994 /* MIPS 64 Release 2. */
19995 /* Loongson CPU core. */
19996 /* -march=loongson3a is an alias of -march=gs464 for compatibility. */
bdc6c06e 19997 { "loongson3a", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT,
ac8cb70f
CX
19998 ISA_MIPS64R2, CPU_GS464 },
19999 { "gs464", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT,
20000 ISA_MIPS64R2, CPU_GS464 },
bd782c07
CX
20001 { "gs464e", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT
20002 | ASE_LOONGSON_EXT2, ISA_MIPS64R2, CPU_GS464E },
9108bc33
CX
20003 { "gs264e", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT
20004 | ASE_LOONGSON_EXT2 | ASE_MSA | ASE_MSA64, ISA_MIPS64R2, CPU_GS264E },
ed163775 20005
6f2117ba 20006 /* Cavium Networks Octeon CPU core. */
d16afab6
RS
20007 { "octeon", 0, 0, ISA_MIPS64R2, CPU_OCTEON },
20008 { "octeon+", 0, 0, ISA_MIPS64R2, CPU_OCTEONP },
20009 { "octeon2", 0, 0, ISA_MIPS64R2, CPU_OCTEON2 },
2c629856 20010 { "octeon3", 0, ASE_VIRT | ASE_VIRT64, ISA_MIPS64R5, CPU_OCTEON3 },
967344c6 20011
52b6b6b9 20012 /* RMI Xlr */
d16afab6 20013 { "xlr", 0, 0, ISA_MIPS64, CPU_XLR },
52b6b6b9 20014
55a36193
MK
20015 /* Broadcom XLP.
20016 XLP is mostly like XLR, with the prominent exception that it is
20017 MIPS64R2 rather than MIPS64. */
d16afab6 20018 { "xlp", 0, 0, ISA_MIPS64R2, CPU_XLR },
55a36193 20019
6f2117ba 20020 /* MIPS 64 Release 6. */
7ef0d297 20021 { "i6400", 0, ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
a4968f42 20022 { "p6600", 0, ASE_VIRT | ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
7ef0d297 20023
6f2117ba 20024 /* End marker. */
d16afab6 20025 { NULL, 0, 0, 0, 0 }
316f5878 20026};
e7af610e 20027
84ea6cf2 20028
316f5878
RS
20029/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
20030 with a final "000" replaced by "k". Ignore case.
e7af610e 20031
316f5878 20032 Note: this function is shared between GCC and GAS. */
c6c98b38 20033
b34976b6 20034static bfd_boolean
17a2f251 20035mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
20036{
20037 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
20038 given++, canonical++;
20039
20040 return ((*given == 0 && *canonical == 0)
20041 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
20042}
20043
20044
20045/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
20046 CPU name. We've traditionally allowed a lot of variation here.
20047
20048 Note: this function is shared between GCC and GAS. */
20049
b34976b6 20050static bfd_boolean
17a2f251 20051mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
20052{
20053 /* First see if the name matches exactly, or with a final "000"
20054 turned into "k". */
20055 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 20056 return TRUE;
316f5878
RS
20057
20058 /* If not, try comparing based on numerical designation alone.
20059 See if GIVEN is an unadorned number, or 'r' followed by a number. */
20060 if (TOLOWER (*given) == 'r')
20061 given++;
20062 if (!ISDIGIT (*given))
b34976b6 20063 return FALSE;
316f5878
RS
20064
20065 /* Skip over some well-known prefixes in the canonical name,
20066 hoping to find a number there too. */
20067 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
20068 canonical += 2;
20069 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
20070 canonical += 2;
20071 else if (TOLOWER (canonical[0]) == 'r')
20072 canonical += 1;
20073
20074 return mips_strict_matching_cpu_name_p (canonical, given);
20075}
20076
20077
20078/* Parse an option that takes the name of a processor as its argument.
20079 OPTION is the name of the option and CPU_STRING is the argument.
20080 Return the corresponding processor enumeration if the CPU_STRING is
20081 recognized, otherwise report an error and return null.
20082
20083 A similar function exists in GCC. */
e7af610e
NC
20084
20085static const struct mips_cpu_info *
17a2f251 20086mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 20087{
316f5878 20088 const struct mips_cpu_info *p;
e7af610e 20089
316f5878
RS
20090 /* 'from-abi' selects the most compatible architecture for the given
20091 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
20092 EABIs, we have to decide whether we're using the 32-bit or 64-bit
20093 version. Look first at the -mgp options, if given, otherwise base
20094 the choice on MIPS_DEFAULT_64BIT.
e7af610e 20095
316f5878
RS
20096 Treat NO_ABI like the EABIs. One reason to do this is that the
20097 plain 'mips' and 'mips64' configs have 'from-abi' as their default
20098 architecture. This code picks MIPS I for 'mips' and MIPS III for
20099 'mips64', just as we did in the days before 'from-abi'. */
20100 if (strcasecmp (cpu_string, "from-abi") == 0)
20101 {
20102 if (ABI_NEEDS_32BIT_REGS (mips_abi))
20103 return mips_cpu_info_from_isa (ISA_MIPS1);
20104
20105 if (ABI_NEEDS_64BIT_REGS (mips_abi))
20106 return mips_cpu_info_from_isa (ISA_MIPS3);
20107
bad1aba3 20108 if (file_mips_opts.gp >= 0)
20109 return mips_cpu_info_from_isa (file_mips_opts.gp == 32
0b35dfee 20110 ? ISA_MIPS1 : ISA_MIPS3);
316f5878
RS
20111
20112 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
20113 ? ISA_MIPS3
20114 : ISA_MIPS1);
20115 }
20116
20117 /* 'default' has traditionally been a no-op. Probably not very useful. */
20118 if (strcasecmp (cpu_string, "default") == 0)
20119 return 0;
20120
20121 for (p = mips_cpu_info_table; p->name != 0; p++)
20122 if (mips_matching_cpu_name_p (p->name, cpu_string))
20123 return p;
20124
1661c76c 20125 as_bad (_("bad value (%s) for %s"), cpu_string, option);
316f5878 20126 return 0;
e7af610e
NC
20127}
20128
316f5878
RS
20129/* Return the canonical processor information for ISA (a member of the
20130 ISA_MIPS* enumeration). */
20131
e7af610e 20132static const struct mips_cpu_info *
17a2f251 20133mips_cpu_info_from_isa (int isa)
e7af610e
NC
20134{
20135 int i;
20136
20137 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
ad3fea08 20138 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
316f5878 20139 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
20140 return (&mips_cpu_info_table[i]);
20141
e972090a 20142 return NULL;
e7af610e 20143}
fef14a42
TS
20144
20145static const struct mips_cpu_info *
17a2f251 20146mips_cpu_info_from_arch (int arch)
fef14a42
TS
20147{
20148 int i;
20149
20150 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
20151 if (arch == mips_cpu_info_table[i].cpu)
20152 return (&mips_cpu_info_table[i]);
20153
20154 return NULL;
20155}
316f5878
RS
20156\f
20157static void
17a2f251 20158show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
20159{
20160 if (*first_p)
20161 {
20162 fprintf (stream, "%24s", "");
20163 *col_p = 24;
20164 }
20165 else
20166 {
20167 fprintf (stream, ", ");
20168 *col_p += 2;
20169 }
e7af610e 20170
316f5878
RS
20171 if (*col_p + strlen (string) > 72)
20172 {
20173 fprintf (stream, "\n%24s", "");
20174 *col_p = 24;
20175 }
20176
20177 fprintf (stream, "%s", string);
20178 *col_p += strlen (string);
20179
20180 *first_p = 0;
20181}
20182
20183void
17a2f251 20184md_show_usage (FILE *stream)
e7af610e 20185{
316f5878
RS
20186 int column, first;
20187 size_t i;
20188
20189 fprintf (stream, _("\
20190MIPS options:\n\
316f5878
RS
20191-EB generate big endian output\n\
20192-EL generate little endian output\n\
20193-g, -g2 do not remove unneeded NOPs or swap branches\n\
20194-G NUM allow referencing objects up to NUM bytes\n\
20195 implicitly with the gp register [default 8]\n"));
20196 fprintf (stream, _("\
20197-mips1 generate MIPS ISA I instructions\n\
20198-mips2 generate MIPS ISA II instructions\n\
20199-mips3 generate MIPS ISA III instructions\n\
20200-mips4 generate MIPS ISA IV instructions\n\
20201-mips5 generate MIPS ISA V instructions\n\
20202-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 20203-mips32r2 generate MIPS32 release 2 ISA instructions\n\
ae52f483
AB
20204-mips32r3 generate MIPS32 release 3 ISA instructions\n\
20205-mips32r5 generate MIPS32 release 5 ISA instructions\n\
7361da2c 20206-mips32r6 generate MIPS32 release 6 ISA instructions\n\
316f5878 20207-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 20208-mips64r2 generate MIPS64 release 2 ISA instructions\n\
ae52f483
AB
20209-mips64r3 generate MIPS64 release 3 ISA instructions\n\
20210-mips64r5 generate MIPS64 release 5 ISA instructions\n\
7361da2c 20211-mips64r6 generate MIPS64 release 6 ISA instructions\n\
316f5878
RS
20212-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
20213
20214 first = 1;
e7af610e
NC
20215
20216 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
20217 show (stream, mips_cpu_info_table[i].name, &column, &first);
20218 show (stream, "from-abi", &column, &first);
20219 fputc ('\n', stream);
e7af610e 20220
316f5878
RS
20221 fprintf (stream, _("\
20222-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
20223-no-mCPU don't generate code specific to CPU.\n\
20224 For -mCPU and -no-mCPU, CPU must be one of:\n"));
20225
20226 first = 1;
20227
20228 show (stream, "3900", &column, &first);
20229 show (stream, "4010", &column, &first);
20230 show (stream, "4100", &column, &first);
20231 show (stream, "4650", &column, &first);
20232 fputc ('\n', stream);
20233
20234 fprintf (stream, _("\
20235-mips16 generate mips16 instructions\n\
20236-no-mips16 do not generate mips16 instructions\n"));
20237 fprintf (stream, _("\
f866b262
MR
20238-mmips16e2 generate MIPS16e2 instructions\n\
20239-mno-mips16e2 do not generate MIPS16e2 instructions\n"));
20240 fprintf (stream, _("\
df58fc94
RS
20241-mmicromips generate microMIPS instructions\n\
20242-mno-micromips do not generate microMIPS instructions\n"));
20243 fprintf (stream, _("\
e16bfa71 20244-msmartmips generate smartmips instructions\n\
3739860c 20245-mno-smartmips do not generate smartmips instructions\n"));
e16bfa71 20246 fprintf (stream, _("\
74cd071d
CF
20247-mdsp generate DSP instructions\n\
20248-mno-dsp do not generate DSP instructions\n"));
20249 fprintf (stream, _("\
8b082fb1
TS
20250-mdspr2 generate DSP R2 instructions\n\
20251-mno-dspr2 do not generate DSP R2 instructions\n"));
20252 fprintf (stream, _("\
8f4f9071
MF
20253-mdspr3 generate DSP R3 instructions\n\
20254-mno-dspr3 do not generate DSP R3 instructions\n"));
20255 fprintf (stream, _("\
ef2e4d86
CF
20256-mmt generate MT instructions\n\
20257-mno-mt do not generate MT instructions\n"));
20258 fprintf (stream, _("\
dec0624d
MR
20259-mmcu generate MCU instructions\n\
20260-mno-mcu do not generate MCU instructions\n"));
20261 fprintf (stream, _("\
56d438b1
CF
20262-mmsa generate MSA instructions\n\
20263-mno-msa do not generate MSA instructions\n"));
20264 fprintf (stream, _("\
7d64c587
AB
20265-mxpa generate eXtended Physical Address (XPA) instructions\n\
20266-mno-xpa do not generate eXtended Physical Address (XPA) instructions\n"));
20267 fprintf (stream, _("\
b015e599
AP
20268-mvirt generate Virtualization instructions\n\
20269-mno-virt do not generate Virtualization instructions\n"));
20270 fprintf (stream, _("\
730c3174
SE
20271-mcrc generate CRC instructions\n\
20272-mno-crc do not generate CRC instructions\n"));
20273 fprintf (stream, _("\
6f20c942
FS
20274-mginv generate Global INValidate (GINV) instructions\n\
20275-mno-ginv do not generate Global INValidate instructions\n"));
20276 fprintf (stream, _("\
8095d2f7
CX
20277-mloongson-mmi generate Loongson MultiMedia extensions Instructions (MMI) instructions\n\
20278-mno-loongson-mmi do not generate Loongson MultiMedia extensions Instructions\n"));
20279 fprintf (stream, _("\
716c08de
CX
20280-mloongson-cam generate Loongson Content Address Memory (CAM) instructions\n\
20281-mno-loongson-cam do not generate Loongson Content Address Memory Instructions\n"));
20282 fprintf (stream, _("\
bdc6c06e
CX
20283-mloongson-ext generate Loongson EXTensions (EXT) instructions\n\
20284-mno-loongson-ext do not generate Loongson EXTensions Instructions\n"));
20285 fprintf (stream, _("\
a693765e
CX
20286-mloongson-ext2 generate Loongson EXTensions R2 (EXT2) instructions\n\
20287-mno-loongson-ext2 do not generate Loongson EXTensions R2 Instructions\n"));
20288 fprintf (stream, _("\
833794fc
MR
20289-minsn32 only generate 32-bit microMIPS instructions\n\
20290-mno-insn32 generate all microMIPS instructions\n"));
6f2117ba
PH
20291#if DEFAULT_MIPS_FIX_LOONGSON3_LLSC
20292 fprintf (stream, _("\
20293-mfix-loongson3-llsc work around Loongson3 LL/SC errata, default\n\
20294-mno-fix-loongson3-llsc disable work around Loongson3 LL/SC errata\n"));
20295#else
20296 fprintf (stream, _("\
20297-mfix-loongson3-llsc work around Loongson3 LL/SC errata\n\
20298-mno-fix-loongson3-llsc disable work around Loongson3 LL/SC errata, default\n"));
20299#endif
833794fc 20300 fprintf (stream, _("\
c67a084a
NC
20301-mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
20302-mfix-loongson2f-nop work around Loongson2F NOP errata\n\
6f2117ba
PH
20303-mfix-loongson3-llsc work around Loongson3 LL/SC errata\n\
20304-mno-fix-loongson3-llsc disable work around Loongson3 LL/SC errata\n\
d766e8ec 20305-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 20306-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
6a32d874 20307-mfix-24k insert a nop after ERET and DERET instructions\n\
d954098f 20308-mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
27c634e0 20309-mfix-r5900 work around R5900 short loop errata\n\
316f5878
RS
20310-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
20311-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261 20312-msym32 assume all symbols have 32-bit values\n\
092a534f
MR
20313-O0 do not remove unneeded NOPs, do not swap branches\n\
20314-O, -O1 remove unneeded NOPs, do not swap branches\n\
20315-O2 remove unneeded NOPs and swap branches\n\
316f5878
RS
20316--trap, --no-break trap exception on div by 0 and mult overflow\n\
20317--break, --no-trap break exception on div by 0 and mult overflow\n"));
037b32b9
AN
20318 fprintf (stream, _("\
20319-mhard-float allow floating-point instructions\n\
20320-msoft-float do not allow floating-point instructions\n\
20321-msingle-float only allow 32-bit floating-point operations\n\
20322-mdouble-float allow 32-bit and 64-bit floating-point operations\n\
3bf0dbfb 20323--[no-]construct-floats [dis]allow floating point values to be constructed\n\
ba92f887 20324--[no-]relax-branch [dis]allow out-of-range branches to be relaxed\n\
8b10b0b3
MR
20325-mignore-branch-isa accept invalid branches requiring an ISA mode switch\n\
20326-mno-ignore-branch-isa reject invalid branches requiring an ISA mode switch\n\
ba92f887
MR
20327-mnan=ENCODING select an IEEE 754 NaN encoding convention, either of:\n"));
20328
20329 first = 1;
20330
20331 show (stream, "legacy", &column, &first);
20332 show (stream, "2008", &column, &first);
20333
20334 fputc ('\n', stream);
20335
316f5878
RS
20336 fprintf (stream, _("\
20337-KPIC, -call_shared generate SVR4 position independent code\n\
861fb55a 20338-call_nonpic generate non-PIC code that can operate with DSOs\n\
0c000745 20339-mvxworks-pic generate VxWorks position independent code\n\
861fb55a 20340-non_shared do not generate code that can operate with DSOs\n\
316f5878 20341-xgot assume a 32 bit GOT\n\
dcd410fe 20342-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8 20343-mshared, -mno-shared disable/enable .cpload optimization for\n\
d821e36b 20344 position dependent (non shared) code\n\
316f5878
RS
20345-mabi=ABI create ABI conformant object file for:\n"));
20346
20347 first = 1;
20348
20349 show (stream, "32", &column, &first);
20350 show (stream, "o64", &column, &first);
20351 show (stream, "n32", &column, &first);
20352 show (stream, "64", &column, &first);
20353 show (stream, "eabi", &column, &first);
20354
20355 fputc ('\n', stream);
20356
20357 fprintf (stream, _("\
b4f6242e
MR
20358-32 create o32 ABI object file%s\n"),
20359 MIPS_DEFAULT_ABI == O32_ABI ? _(" (default)") : "");
20360 fprintf (stream, _("\
20361-n32 create n32 ABI object file%s\n"),
20362 MIPS_DEFAULT_ABI == N32_ABI ? _(" (default)") : "");
20363 fprintf (stream, _("\
20364-64 create 64 ABI object file%s\n"),
20365 MIPS_DEFAULT_ABI == N64_ABI ? _(" (default)") : "");
e7af610e 20366}
14e777e0 20367
1575952e 20368#ifdef TE_IRIX
14e777e0 20369enum dwarf2_format
413a266c 20370mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
14e777e0 20371{
369943fe 20372 if (HAVE_64BIT_SYMBOLS)
1575952e 20373 return dwarf2_format_64bit_irix;
14e777e0
KB
20374 else
20375 return dwarf2_format_32bit;
20376}
1575952e 20377#endif
73369e65
EC
20378
20379int
20380mips_dwarf2_addr_size (void)
20381{
6b6b3450 20382 if (HAVE_64BIT_OBJECTS)
73369e65 20383 return 8;
73369e65
EC
20384 else
20385 return 4;
20386}
5862107c
EC
20387
20388/* Standard calling conventions leave the CFA at SP on entry. */
20389void
20390mips_cfi_frame_initial_instructions (void)
20391{
20392 cfi_add_CFA_def_cfa_register (SP);
20393}
20394
707bfff6
TS
20395int
20396tc_mips_regname_to_dw2regnum (char *regname)
20397{
20398 unsigned int regnum = -1;
20399 unsigned int reg;
20400
20401 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
20402 regnum = reg;
20403
20404 return regnum;
20405}
263b2574 20406
20407/* Implement CONVERT_SYMBOLIC_ATTRIBUTE.
20408 Given a symbolic attribute NAME, return the proper integer value.
20409 Returns -1 if the attribute is not known. */
20410
20411int
20412mips_convert_symbolic_attribute (const char *name)
20413{
20414 static const struct
20415 {
20416 const char * name;
20417 const int tag;
20418 }
20419 attribute_table[] =
20420 {
20421#define T(tag) {#tag, tag}
20422 T (Tag_GNU_MIPS_ABI_FP),
20423 T (Tag_GNU_MIPS_ABI_MSA),
20424#undef T
20425 };
20426 unsigned int i;
20427
20428 if (name == NULL)
20429 return -1;
20430
20431 for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
20432 if (streq (name, attribute_table[i].name))
20433 return attribute_table[i].tag;
20434
20435 return -1;
20436}
fd5c94ab
RS
20437
20438void
20439md_mips_end (void)
20440{
351cdf24
MF
20441 int fpabi = Val_GNU_MIPS_ABI_FP_ANY;
20442
fd5c94ab
RS
20443 mips_emit_delays ();
20444 if (cur_proc_ptr)
20445 as_warn (_("missing .end at end of assembly"));
919731af 20446
20447 /* Just in case no code was emitted, do the consistency check. */
20448 file_mips_check_options ();
351cdf24
MF
20449
20450 /* Set a floating-point ABI if the user did not. */
20451 if (obj_elf_seen_attribute (OBJ_ATTR_GNU, Tag_GNU_MIPS_ABI_FP))
20452 {
20453 /* Perform consistency checks on the floating-point ABI. */
20454 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20455 Tag_GNU_MIPS_ABI_FP);
20456 if (fpabi != Val_GNU_MIPS_ABI_FP_ANY)
20457 check_fpabi (fpabi);
20458 }
20459 else
20460 {
20461 /* Soft-float gets precedence over single-float, the two options should
20462 not be used together so this should not matter. */
20463 if (file_mips_opts.soft_float == 1)
20464 fpabi = Val_GNU_MIPS_ABI_FP_SOFT;
20465 /* Single-float gets precedence over all double_float cases. */
20466 else if (file_mips_opts.single_float == 1)
20467 fpabi = Val_GNU_MIPS_ABI_FP_SINGLE;
20468 else
20469 {
20470 switch (file_mips_opts.fp)
20471 {
20472 case 32:
20473 if (file_mips_opts.gp == 32)
20474 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20475 break;
20476 case 0:
20477 fpabi = Val_GNU_MIPS_ABI_FP_XX;
20478 break;
20479 case 64:
20480 if (file_mips_opts.gp == 32 && !file_mips_opts.oddspreg)
20481 fpabi = Val_GNU_MIPS_ABI_FP_64A;
20482 else if (file_mips_opts.gp == 32)
20483 fpabi = Val_GNU_MIPS_ABI_FP_64;
20484 else
20485 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20486 break;
20487 }
20488 }
20489
20490 bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20491 Tag_GNU_MIPS_ABI_FP, fpabi);
20492 }
fd5c94ab 20493}
2f0c68f2
CM
20494
20495/* Returns the relocation type required for a particular CFI encoding. */
20496
20497bfd_reloc_code_real_type
20498mips_cfi_reloc_for_encoding (int encoding)
20499{
20500 if (encoding == (DW_EH_PE_sdata4 | DW_EH_PE_pcrel))
20501 return BFD_RELOC_32_PCREL;
20502 else return BFD_RELOC_NONE;
20503}
This page took 3.323169 seconds and 4 git commands to generate.