Consolidate file_mips_xxx variables.
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
CommitLineData
252b5132 1/* tc-mips.c -- assemble code for a MIPS chip.
4b95cf5c 2 Copyright (C) 1993-2014 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;
252b5132 92
85b51719 93#define ZERO 0
741fe287 94#define ATREG 1
df58fc94
RS
95#define S0 16
96#define S7 23
252b5132
RH
97#define TREG 24
98#define PIC_CALL_REG 25
99#define KT0 26
100#define KT1 27
101#define GP 28
102#define SP 29
103#define FP 30
104#define RA 31
105
106#define ILLEGAL_REG (32)
107
741fe287
MR
108#define AT mips_opts.at
109
252b5132
RH
110extern int target_big_endian;
111
252b5132 112/* The name of the readonly data section. */
e8044f35 113#define RDATA_SECTION_NAME ".rodata"
252b5132 114
a4e06468
RS
115/* Ways in which an instruction can be "appended" to the output. */
116enum append_method {
117 /* Just add it normally. */
118 APPEND_ADD,
119
120 /* Add it normally and then add a nop. */
121 APPEND_ADD_WITH_NOP,
122
123 /* Turn an instruction with a delay slot into a "compact" version. */
124 APPEND_ADD_COMPACT,
125
126 /* Insert the instruction before the last one. */
127 APPEND_SWAP
128};
129
47e39b9d
RS
130/* Information about an instruction, including its format, operands
131 and fixups. */
132struct mips_cl_insn
133{
134 /* The opcode's entry in mips_opcodes or mips16_opcodes. */
135 const struct mips_opcode *insn_mo;
136
47e39b9d 137 /* The 16-bit or 32-bit bitstring of the instruction itself. This is
5c04167a
RS
138 a copy of INSN_MO->match with the operands filled in. If we have
139 decided to use an extended MIPS16 instruction, this includes the
140 extension. */
47e39b9d
RS
141 unsigned long insn_opcode;
142
143 /* The frag that contains the instruction. */
144 struct frag *frag;
145
146 /* The offset into FRAG of the first instruction byte. */
147 long where;
148
149 /* The relocs associated with the instruction, if any. */
150 fixS *fixp[3];
151
a38419a5
RS
152 /* True if this entry cannot be moved from its current position. */
153 unsigned int fixed_p : 1;
47e39b9d 154
708587a4 155 /* True if this instruction occurred in a .set noreorder block. */
47e39b9d
RS
156 unsigned int noreorder_p : 1;
157
2fa15973
RS
158 /* True for mips16 instructions that jump to an absolute address. */
159 unsigned int mips16_absolute_jump_p : 1;
15be625d
CM
160
161 /* True if this instruction is complete. */
162 unsigned int complete_p : 1;
e407c74b
NC
163
164 /* True if this instruction is cleared from history by unconditional
165 branch. */
166 unsigned int cleared_p : 1;
47e39b9d
RS
167};
168
a325df1d
TS
169/* The ABI to use. */
170enum mips_abi_level
171{
172 NO_ABI = 0,
173 O32_ABI,
174 O64_ABI,
175 N32_ABI,
176 N64_ABI,
177 EABI_ABI
178};
179
180/* MIPS ABI we are using for this output file. */
316f5878 181static enum mips_abi_level mips_abi = NO_ABI;
a325df1d 182
143d77c5
EC
183/* Whether or not we have code that can call pic code. */
184int mips_abicalls = FALSE;
185
aa6975fb
ILT
186/* Whether or not we have code which can be put into a shared
187 library. */
188static bfd_boolean mips_in_shared = TRUE;
189
252b5132
RH
190/* This is the set of options which may be modified by the .set
191 pseudo-op. We use a struct so that .set push and .set pop are more
192 reliable. */
193
e972090a
NC
194struct mips_set_options
195{
252b5132
RH
196 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
197 if it has not been initialized. Changed by `.set mipsN', and the
198 -mipsN command line option, and the default CPU. */
199 int isa;
846ef2d0
RS
200 /* Enabled Application Specific Extensions (ASEs). Changed by `.set
201 <asename>', by command line options, and based on the default
202 architecture. */
203 int ase;
252b5132
RH
204 /* Whether we are assembling for the mips16 processor. 0 if we are
205 not, 1 if we are, and -1 if the value has not been initialized.
206 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
207 -nomips16 command line options, and the default CPU. */
208 int mips16;
df58fc94
RS
209 /* Whether we are assembling for the mipsMIPS ASE. 0 if we are not,
210 1 if we are, and -1 if the value has not been initialized. Changed
211 by `.set micromips' and `.set nomicromips', and the -mmicromips
212 and -mno-micromips command line options, and the default CPU. */
213 int micromips;
252b5132
RH
214 /* Non-zero if we should not reorder instructions. Changed by `.set
215 reorder' and `.set noreorder'. */
216 int noreorder;
741fe287
MR
217 /* Non-zero if we should not permit the register designated "assembler
218 temporary" to be used in instructions. The value is the register
219 number, normally $at ($1). Changed by `.set at=REG', `.set noat'
220 (same as `.set at=$0') and `.set at' (same as `.set at=$1'). */
221 unsigned int at;
252b5132
RH
222 /* Non-zero if we should warn when a macro instruction expands into
223 more than one machine instruction. Changed by `.set nomacro' and
224 `.set macro'. */
225 int warn_about_macros;
226 /* Non-zero if we should not move instructions. Changed by `.set
227 move', `.set volatile', `.set nomove', and `.set novolatile'. */
228 int nomove;
229 /* Non-zero if we should not optimize branches by moving the target
230 of the branch into the delay slot. Actually, we don't perform
231 this optimization anyhow. Changed by `.set bopt' and `.set
232 nobopt'. */
233 int nobopt;
234 /* Non-zero if we should not autoextend mips16 instructions.
235 Changed by `.set autoextend' and `.set noautoextend'. */
236 int noautoextend;
833794fc
MR
237 /* True if we should only emit 32-bit microMIPS instructions.
238 Changed by `.set insn32' and `.set noinsn32', and the -minsn32
239 and -mno-insn32 command line options. */
240 bfd_boolean insn32;
a325df1d
TS
241 /* Restrict general purpose registers and floating point registers
242 to 32 bit. This is initially determined when -mgp32 or -mfp32
243 is passed but can changed if the assembler code uses .set mipsN. */
244 int gp32;
0b35dfee 245 int fp;
fef14a42
TS
246 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
247 command line option, and the default CPU. */
248 int arch;
aed1a261
RS
249 /* True if ".set sym32" is in effect. */
250 bfd_boolean sym32;
037b32b9
AN
251 /* True if floating-point operations are not allowed. Changed by .set
252 softfloat or .set hardfloat, by command line options -msoft-float or
253 -mhard-float. The default is false. */
254 bfd_boolean soft_float;
255
256 /* True if only single-precision floating-point operations are allowed.
257 Changed by .set singlefloat or .set doublefloat, command-line options
258 -msingle-float or -mdouble-float. The default is false. */
259 bfd_boolean single_float;
252b5132
RH
260};
261
0b35dfee 262/* True if -mnan=2008, false if -mnan=legacy. */
263static bfd_boolean mips_flag_nan2008 = FALSE;
a325df1d 264
0b35dfee 265/* This is the struct we use to hold the module level set of options.
266 Note that we must set the isa field to ISA_UNKNOWN and the ASE, gp32 and
267 fp fields to -1 to indicate that they have not been initialized. */
037b32b9 268
0b35dfee 269static struct mips_set_options file_mips_opts =
270{
271 /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
272 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
273 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
274 /* gp32 */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
275 /* soft_float */ FALSE, /* single_float */ FALSE
276};
252b5132 277
0b35dfee 278/* This is similar to file_mips_opts, but for the current set of options. */
ba92f887 279
e972090a
NC
280static struct mips_set_options mips_opts =
281{
846ef2d0 282 /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
b015e599 283 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
833794fc 284 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
0b35dfee 285 /* gp32 */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
b015e599 286 /* soft_float */ FALSE, /* single_float */ FALSE
e7af610e 287};
252b5132 288
846ef2d0
RS
289/* The set of ASEs that were selected on the command line, either
290 explicitly via ASE options or implicitly through things like -march. */
291static unsigned int file_ase;
292
293/* Which bits of file_ase were explicitly set or cleared by ASE options. */
294static unsigned int file_ase_explicit;
295
252b5132
RH
296/* These variables are filled in with the masks of registers used.
297 The object format code reads them and puts them in the appropriate
298 place. */
299unsigned long mips_gprmask;
300unsigned long mips_cprmask[4];
301
738f4d98 302/* True if any MIPS16 code was produced. */
a4672219
TS
303static int file_ase_mips16;
304
3994f87e
TS
305#define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32 \
306 || mips_opts.isa == ISA_MIPS32R2 \
ae52f483
AB
307 || mips_opts.isa == ISA_MIPS32R3 \
308 || mips_opts.isa == ISA_MIPS32R5 \
3994f87e 309 || mips_opts.isa == ISA_MIPS64 \
ae52f483
AB
310 || mips_opts.isa == ISA_MIPS64R2 \
311 || mips_opts.isa == ISA_MIPS64R3 \
312 || mips_opts.isa == ISA_MIPS64R5)
3994f87e 313
df58fc94
RS
314/* True if any microMIPS code was produced. */
315static int file_ase_micromips;
316
b12dd2e4
CF
317/* True if we want to create R_MIPS_JALR for jalr $25. */
318#ifdef TE_IRIX
1180b5a4 319#define MIPS_JALR_HINT_P(EXPR) HAVE_NEWABI
b12dd2e4 320#else
1180b5a4
RS
321/* As a GNU extension, we use R_MIPS_JALR for o32 too. However,
322 because there's no place for any addend, the only acceptable
323 expression is a bare symbol. */
324#define MIPS_JALR_HINT_P(EXPR) \
325 (!HAVE_IN_PLACE_ADDENDS \
326 || ((EXPR)->X_op == O_symbol && (EXPR)->X_add_number == 0))
b12dd2e4
CF
327#endif
328
ec68c924 329/* The argument of the -march= flag. The architecture we are assembling. */
316f5878 330static const char *mips_arch_string;
ec68c924
EC
331
332/* The argument of the -mtune= flag. The architecture for which we
333 are optimizing. */
334static int mips_tune = CPU_UNKNOWN;
316f5878 335static const char *mips_tune_string;
ec68c924 336
316f5878 337/* True when generating 32-bit code for a 64-bit processor. */
252b5132
RH
338static int mips_32bitmode = 0;
339
316f5878
RS
340/* True if the given ABI requires 32-bit registers. */
341#define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
342
343/* Likewise 64-bit registers. */
707bfff6
TS
344#define ABI_NEEDS_64BIT_REGS(ABI) \
345 ((ABI) == N32_ABI \
346 || (ABI) == N64_ABI \
316f5878
RS
347 || (ABI) == O64_ABI)
348
ad3fea08 349/* Return true if ISA supports 64 bit wide gp registers. */
707bfff6
TS
350#define ISA_HAS_64BIT_REGS(ISA) \
351 ((ISA) == ISA_MIPS3 \
352 || (ISA) == ISA_MIPS4 \
353 || (ISA) == ISA_MIPS5 \
354 || (ISA) == ISA_MIPS64 \
ae52f483
AB
355 || (ISA) == ISA_MIPS64R2 \
356 || (ISA) == ISA_MIPS64R3 \
357 || (ISA) == ISA_MIPS64R5)
9ce8a5dd 358
ad3fea08
TS
359/* Return true if ISA supports 64 bit wide float registers. */
360#define ISA_HAS_64BIT_FPRS(ISA) \
361 ((ISA) == ISA_MIPS3 \
362 || (ISA) == ISA_MIPS4 \
363 || (ISA) == ISA_MIPS5 \
364 || (ISA) == ISA_MIPS32R2 \
ae52f483
AB
365 || (ISA) == ISA_MIPS32R3 \
366 || (ISA) == ISA_MIPS32R5 \
ad3fea08 367 || (ISA) == ISA_MIPS64 \
ae52f483
AB
368 || (ISA) == ISA_MIPS64R2 \
369 || (ISA) == ISA_MIPS64R3 \
370 || (ISA) == ISA_MIPS64R5 )
ad3fea08 371
af7ee8bf
CD
372/* Return true if ISA supports 64-bit right rotate (dror et al.)
373 instructions. */
707bfff6 374#define ISA_HAS_DROR(ISA) \
df58fc94 375 ((ISA) == ISA_MIPS64R2 \
ae52f483
AB
376 || (ISA) == ISA_MIPS64R3 \
377 || (ISA) == ISA_MIPS64R5 \
df58fc94
RS
378 || (mips_opts.micromips \
379 && ISA_HAS_64BIT_REGS (ISA)) \
380 )
af7ee8bf
CD
381
382/* Return true if ISA supports 32-bit right rotate (ror et al.)
383 instructions. */
707bfff6
TS
384#define ISA_HAS_ROR(ISA) \
385 ((ISA) == ISA_MIPS32R2 \
ae52f483
AB
386 || (ISA) == ISA_MIPS32R3 \
387 || (ISA) == ISA_MIPS32R5 \
707bfff6 388 || (ISA) == ISA_MIPS64R2 \
ae52f483
AB
389 || (ISA) == ISA_MIPS64R3 \
390 || (ISA) == ISA_MIPS64R5 \
846ef2d0 391 || (mips_opts.ase & ASE_SMARTMIPS) \
df58fc94
RS
392 || mips_opts.micromips \
393 )
707bfff6 394
7455baf8
TS
395/* Return true if ISA supports single-precision floats in odd registers. */
396#define ISA_HAS_ODD_SINGLE_FPR(ISA) \
397 ((ISA) == ISA_MIPS32 \
398 || (ISA) == ISA_MIPS32R2 \
ae52f483
AB
399 || (ISA) == ISA_MIPS32R3 \
400 || (ISA) == ISA_MIPS32R5 \
7455baf8 401 || (ISA) == ISA_MIPS64 \
ae52f483
AB
402 || (ISA) == ISA_MIPS64R2 \
403 || (ISA) == ISA_MIPS64R3 \
404 || (ISA) == ISA_MIPS64R5)
af7ee8bf 405
ad3fea08
TS
406/* Return true if ISA supports move to/from high part of a 64-bit
407 floating-point register. */
408#define ISA_HAS_MXHC1(ISA) \
409 ((ISA) == ISA_MIPS32R2 \
ae52f483
AB
410 || (ISA) == ISA_MIPS32R3 \
411 || (ISA) == ISA_MIPS32R5 \
412 || (ISA) == ISA_MIPS64R2 \
413 || (ISA) == ISA_MIPS64R3 \
414 || (ISA) == ISA_MIPS64R5)
ad3fea08 415
e013f690 416#define HAVE_32BIT_GPRS \
ad3fea08 417 (mips_opts.gp32 || !ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257 418
e013f690 419#define HAVE_32BIT_FPRS \
0b35dfee 420 (mips_opts.fp != 64 || !ISA_HAS_64BIT_FPRS (mips_opts.isa))
ca4e0257 421
ad3fea08
TS
422#define HAVE_64BIT_GPRS (!HAVE_32BIT_GPRS)
423#define HAVE_64BIT_FPRS (!HAVE_32BIT_FPRS)
ca4e0257 424
316f5878 425#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
e013f690 426
316f5878 427#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
e013f690 428
3b91255e
RS
429/* True if relocations are stored in-place. */
430#define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
431
aed1a261
RS
432/* The ABI-derived address size. */
433#define HAVE_64BIT_ADDRESSES \
434 (HAVE_64BIT_GPRS && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
435#define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
e013f690 436
aed1a261
RS
437/* The size of symbolic constants (i.e., expressions of the form
438 "SYMBOL" or "SYMBOL + OFFSET"). */
439#define HAVE_32BIT_SYMBOLS \
440 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
441#define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
ca4e0257 442
b7c7d6c1
TS
443/* Addresses are loaded in different ways, depending on the address size
444 in use. The n32 ABI Documentation also mandates the use of additions
445 with overflow checking, but existing implementations don't follow it. */
f899b4b8 446#define ADDRESS_ADD_INSN \
b7c7d6c1 447 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
f899b4b8
TS
448
449#define ADDRESS_ADDI_INSN \
b7c7d6c1 450 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
f899b4b8
TS
451
452#define ADDRESS_LOAD_INSN \
453 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
454
455#define ADDRESS_STORE_INSN \
456 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
457
a4672219 458/* Return true if the given CPU supports the MIPS16 ASE. */
3396de36
TS
459#define CPU_HAS_MIPS16(cpu) \
460 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
461 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
a4672219 462
2309ddf2 463/* Return true if the given CPU supports the microMIPS ASE. */
df58fc94
RS
464#define CPU_HAS_MICROMIPS(cpu) 0
465
60b63b72
RS
466/* True if CPU has a dror instruction. */
467#define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
468
469/* True if CPU has a ror instruction. */
470#define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
471
dd6a37e7 472/* True if CPU is in the Octeon family */
432233b3 473#define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP || (CPU) == CPU_OCTEON2)
dd6a37e7 474
dd3cbb7e 475/* True if CPU has seq/sne and seqi/snei instructions. */
dd6a37e7 476#define CPU_HAS_SEQ(CPU) (CPU_IS_OCTEON (CPU))
dd3cbb7e 477
0aa27725
RS
478/* True, if CPU has support for ldc1 and sdc1. */
479#define CPU_HAS_LDC1_SDC1(CPU) \
480 ((mips_opts.isa != ISA_MIPS1) && ((CPU) != CPU_R5900))
481
c8978940
CD
482/* True if mflo and mfhi can be immediately followed by instructions
483 which write to the HI and LO registers.
484
485 According to MIPS specifications, MIPS ISAs I, II, and III need
486 (at least) two instructions between the reads of HI/LO and
487 instructions which write them, and later ISAs do not. Contradicting
488 the MIPS specifications, some MIPS IV processor user manuals (e.g.
489 the UM for the NEC Vr5000) document needing the instructions between
490 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
491 MIPS64 and later ISAs to have the interlocks, plus any specific
492 earlier-ISA CPUs for which CPU documentation declares that the
493 instructions are really interlocked. */
494#define hilo_interlocks \
495 (mips_opts.isa == ISA_MIPS32 \
496 || mips_opts.isa == ISA_MIPS32R2 \
ae52f483
AB
497 || mips_opts.isa == ISA_MIPS32R3 \
498 || mips_opts.isa == ISA_MIPS32R5 \
c8978940
CD
499 || mips_opts.isa == ISA_MIPS64 \
500 || mips_opts.isa == ISA_MIPS64R2 \
ae52f483
AB
501 || mips_opts.isa == ISA_MIPS64R3 \
502 || mips_opts.isa == ISA_MIPS64R5 \
c8978940 503 || mips_opts.arch == CPU_R4010 \
e407c74b 504 || mips_opts.arch == CPU_R5900 \
c8978940
CD
505 || mips_opts.arch == CPU_R10000 \
506 || mips_opts.arch == CPU_R12000 \
3aa3176b
TS
507 || mips_opts.arch == CPU_R14000 \
508 || mips_opts.arch == CPU_R16000 \
c8978940 509 || mips_opts.arch == CPU_RM7000 \
c8978940 510 || mips_opts.arch == CPU_VR5500 \
df58fc94 511 || mips_opts.micromips \
c8978940 512 )
252b5132
RH
513
514/* Whether the processor uses hardware interlocks to protect reads
81912461
ILT
515 from the GPRs after they are loaded from memory, and thus does not
516 require nops to be inserted. This applies to instructions marked
67dc82bc 517 INSN_LOAD_MEMORY. These nops are only required at MIPS ISA
df58fc94
RS
518 level I and microMIPS mode instructions are always interlocked. */
519#define gpr_interlocks \
520 (mips_opts.isa != ISA_MIPS1 \
521 || mips_opts.arch == CPU_R3900 \
e407c74b 522 || mips_opts.arch == CPU_R5900 \
df58fc94
RS
523 || mips_opts.micromips \
524 )
252b5132 525
81912461
ILT
526/* Whether the processor uses hardware interlocks to avoid delays
527 required by coprocessor instructions, and thus does not require
528 nops to be inserted. This applies to instructions marked
529 INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
530 between instructions marked INSN_WRITE_COND_CODE and ones marked
531 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
df58fc94
RS
532 levels I, II, and III and microMIPS mode instructions are always
533 interlocked. */
bdaaa2e1 534/* Itbl support may require additional care here. */
81912461
ILT
535#define cop_interlocks \
536 ((mips_opts.isa != ISA_MIPS1 \
537 && mips_opts.isa != ISA_MIPS2 \
538 && mips_opts.isa != ISA_MIPS3) \
539 || mips_opts.arch == CPU_R4300 \
df58fc94 540 || mips_opts.micromips \
81912461
ILT
541 )
542
543/* Whether the processor uses hardware interlocks to protect reads
544 from coprocessor registers after they are loaded from memory, and
545 thus does not require nops to be inserted. This applies to
546 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
df58fc94
RS
547 requires at MIPS ISA level I and microMIPS mode instructions are
548 always interlocked. */
549#define cop_mem_interlocks \
550 (mips_opts.isa != ISA_MIPS1 \
551 || mips_opts.micromips \
552 )
252b5132 553
6b76fefe
CM
554/* Is this a mfhi or mflo instruction? */
555#define MF_HILO_INSN(PINFO) \
b19e8a9b
AN
556 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
557
df58fc94
RS
558/* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
559 has been selected. This implies, in particular, that addresses of text
560 labels have their LSB set. */
561#define HAVE_CODE_COMPRESSION \
562 ((mips_opts.mips16 | mips_opts.micromips) != 0)
563
42429eac
RS
564/* The minimum and maximum signed values that can be stored in a GPR. */
565#define GPR_SMAX ((offsetT) (((valueT) 1 << (HAVE_64BIT_GPRS ? 63 : 31)) - 1))
566#define GPR_SMIN (-GPR_SMAX - 1)
567
252b5132
RH
568/* MIPS PIC level. */
569
a161fe53 570enum mips_pic_level mips_pic;
252b5132 571
c9914766 572/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 573 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 574static int mips_big_got = 0;
252b5132
RH
575
576/* 1 if trap instructions should used for overflow rather than break
577 instructions. */
c9914766 578static int mips_trap = 0;
252b5132 579
119d663a 580/* 1 if double width floating point constants should not be constructed
b6ff326e 581 by assembling two single width halves into two single width floating
119d663a
NC
582 point registers which just happen to alias the double width destination
583 register. On some architectures this aliasing can be disabled by a bit
d547a75e 584 in the status register, and the setting of this bit cannot be determined
119d663a
NC
585 automatically at assemble time. */
586static int mips_disable_float_construction;
587
252b5132
RH
588/* Non-zero if any .set noreorder directives were used. */
589
590static int mips_any_noreorder;
591
6b76fefe
CM
592/* Non-zero if nops should be inserted when the register referenced in
593 an mfhi/mflo instruction is read in the next two instructions. */
594static int mips_7000_hilo_fix;
595
02ffd3e4 596/* The size of objects in the small data section. */
156c2f8b 597static unsigned int g_switch_value = 8;
252b5132
RH
598/* Whether the -G option was used. */
599static int g_switch_seen = 0;
600
601#define N_RMASK 0xc4
602#define N_VFP 0xd4
603
604/* If we can determine in advance that GP optimization won't be
605 possible, we can skip the relaxation stuff that tries to produce
606 GP-relative references. This makes delay slot optimization work
607 better.
608
609 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
610 gcc output. It needs to guess right for gcc, otherwise gcc
611 will put what it thinks is a GP-relative instruction in a branch
612 delay slot.
252b5132
RH
613
614 I don't know if a fix is needed for the SVR4_PIC mode. I've only
615 fixed it for the non-PIC mode. KR 95/04/07 */
17a2f251 616static int nopic_need_relax (symbolS *, int);
252b5132
RH
617
618/* handle of the OPCODE hash table */
619static struct hash_control *op_hash = NULL;
620
621/* The opcode hash table we use for the mips16. */
622static struct hash_control *mips16_op_hash = NULL;
623
df58fc94
RS
624/* The opcode hash table we use for the microMIPS ASE. */
625static struct hash_control *micromips_op_hash = NULL;
626
252b5132
RH
627/* This array holds the chars that always start a comment. If the
628 pre-processor is disabled, these aren't very useful */
629const char comment_chars[] = "#";
630
631/* This array holds the chars that only start a comment at the beginning of
632 a line. If the line seems to have the form '# 123 filename'
633 .line and .file directives will appear in the pre-processed output */
634/* Note that input_file.c hand checks for '#' at the beginning of the
635 first line of the input file. This is because the compiler outputs
bdaaa2e1 636 #NO_APP at the beginning of its output. */
252b5132
RH
637/* Also note that C style comments are always supported. */
638const char line_comment_chars[] = "#";
639
bdaaa2e1 640/* This array holds machine specific line separator characters. */
63a0b638 641const char line_separator_chars[] = ";";
252b5132
RH
642
643/* Chars that can be used to separate mant from exp in floating point nums */
644const char EXP_CHARS[] = "eE";
645
646/* Chars that mean this number is a floating point constant */
647/* As in 0f12.456 */
648/* or 0d1.2345e12 */
649const char FLT_CHARS[] = "rRsSfFdDxXpP";
650
651/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
652 changed in read.c . Ideally it shouldn't have to know about it at all,
653 but nothing is ideal around here.
654 */
655
e3de51ce
RS
656/* Types of printf format used for instruction-related error messages.
657 "I" means int ("%d") and "S" means string ("%s"). */
658enum mips_insn_error_format {
659 ERR_FMT_PLAIN,
660 ERR_FMT_I,
661 ERR_FMT_SS,
662};
663
664/* Information about an error that was found while assembling the current
665 instruction. */
666struct mips_insn_error {
667 /* We sometimes need to match an instruction against more than one
668 opcode table entry. Errors found during this matching are reported
669 against a particular syntactic argument rather than against the
670 instruction as a whole. We grade these messages so that errors
671 against argument N have a greater priority than an error against
672 any argument < N, since the former implies that arguments up to N
673 were acceptable and that the opcode entry was therefore a closer match.
674 If several matches report an error against the same argument,
675 we only use that error if it is the same in all cases.
676
677 min_argnum is the minimum argument number for which an error message
678 should be accepted. It is 0 if MSG is against the instruction as
679 a whole. */
680 int min_argnum;
681
682 /* The printf()-style message, including its format and arguments. */
683 enum mips_insn_error_format format;
684 const char *msg;
685 union {
686 int i;
687 const char *ss[2];
688 } u;
689};
690
691/* The error that should be reported for the current instruction. */
692static struct mips_insn_error insn_error;
252b5132
RH
693
694static int auto_align = 1;
695
696/* When outputting SVR4 PIC code, the assembler needs to know the
697 offset in the stack frame from which to restore the $gp register.
698 This is set by the .cprestore pseudo-op, and saved in this
699 variable. */
700static offsetT mips_cprestore_offset = -1;
701
67c1ffbe 702/* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
6478892d 703 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 704 offset and even an other register than $gp as global pointer. */
6478892d
TS
705static offsetT mips_cpreturn_offset = -1;
706static int mips_cpreturn_register = -1;
707static int mips_gp_register = GP;
def2e0dd 708static int mips_gprel_offset = 0;
6478892d 709
7a621144
DJ
710/* Whether mips_cprestore_offset has been set in the current function
711 (or whether it has already been warned about, if not). */
712static int mips_cprestore_valid = 0;
713
252b5132
RH
714/* This is the register which holds the stack frame, as set by the
715 .frame pseudo-op. This is needed to implement .cprestore. */
716static int mips_frame_reg = SP;
717
7a621144
DJ
718/* Whether mips_frame_reg has been set in the current function
719 (or whether it has already been warned about, if not). */
720static int mips_frame_reg_valid = 0;
721
252b5132
RH
722/* To output NOP instructions correctly, we need to keep information
723 about the previous two instructions. */
724
725/* Whether we are optimizing. The default value of 2 means to remove
726 unneeded NOPs and swap branch instructions when possible. A value
727 of 1 means to not swap branches. A value of 0 means to always
728 insert NOPs. */
729static int mips_optimize = 2;
730
731/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
732 equivalent to seeing no -g option at all. */
733static int mips_debug = 0;
734
7d8e00cf
RS
735/* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
736#define MAX_VR4130_NOPS 4
737
738/* The maximum number of NOPs needed to fill delay slots. */
739#define MAX_DELAY_NOPS 2
740
741/* The maximum number of NOPs needed for any purpose. */
742#define MAX_NOPS 4
71400594
RS
743
744/* A list of previous instructions, with index 0 being the most recent.
745 We need to look back MAX_NOPS instructions when filling delay slots
746 or working around processor errata. We need to look back one
747 instruction further if we're thinking about using history[0] to
748 fill a branch delay slot. */
749static struct mips_cl_insn history[1 + MAX_NOPS];
252b5132 750
fc76e730 751/* Arrays of operands for each instruction. */
14daeee3 752#define MAX_OPERANDS 6
fc76e730
RS
753struct mips_operand_array {
754 const struct mips_operand *operand[MAX_OPERANDS];
755};
756static struct mips_operand_array *mips_operands;
757static struct mips_operand_array *mips16_operands;
758static struct mips_operand_array *micromips_operands;
759
1e915849 760/* Nop instructions used by emit_nop. */
df58fc94
RS
761static struct mips_cl_insn nop_insn;
762static struct mips_cl_insn mips16_nop_insn;
763static struct mips_cl_insn micromips_nop16_insn;
764static struct mips_cl_insn micromips_nop32_insn;
1e915849
RS
765
766/* The appropriate nop for the current mode. */
833794fc
MR
767#define NOP_INSN (mips_opts.mips16 \
768 ? &mips16_nop_insn \
769 : (mips_opts.micromips \
770 ? (mips_opts.insn32 \
771 ? &micromips_nop32_insn \
772 : &micromips_nop16_insn) \
773 : &nop_insn))
df58fc94
RS
774
775/* The size of NOP_INSN in bytes. */
833794fc
MR
776#define NOP_INSN_SIZE ((mips_opts.mips16 \
777 || (mips_opts.micromips && !mips_opts.insn32)) \
778 ? 2 : 4)
252b5132 779
252b5132
RH
780/* If this is set, it points to a frag holding nop instructions which
781 were inserted before the start of a noreorder section. If those
782 nops turn out to be unnecessary, the size of the frag can be
783 decreased. */
784static fragS *prev_nop_frag;
785
786/* The number of nop instructions we created in prev_nop_frag. */
787static int prev_nop_frag_holds;
788
789/* The number of nop instructions that we know we need in
bdaaa2e1 790 prev_nop_frag. */
252b5132
RH
791static int prev_nop_frag_required;
792
793/* The number of instructions we've seen since prev_nop_frag. */
794static int prev_nop_frag_since;
795
e8044f35
RS
796/* Relocations against symbols are sometimes done in two parts, with a HI
797 relocation and a LO relocation. Each relocation has only 16 bits of
798 space to store an addend. This means that in order for the linker to
799 handle carries correctly, it must be able to locate both the HI and
800 the LO relocation. This means that the relocations must appear in
801 order in the relocation table.
252b5132
RH
802
803 In order to implement this, we keep track of each unmatched HI
804 relocation. We then sort them so that they immediately precede the
bdaaa2e1 805 corresponding LO relocation. */
252b5132 806
e972090a
NC
807struct mips_hi_fixup
808{
252b5132
RH
809 /* Next HI fixup. */
810 struct mips_hi_fixup *next;
811 /* This fixup. */
812 fixS *fixp;
813 /* The section this fixup is in. */
814 segT seg;
815};
816
817/* The list of unmatched HI relocs. */
818
819static struct mips_hi_fixup *mips_hi_fixup_list;
820
64bdfcaf
RS
821/* The frag containing the last explicit relocation operator.
822 Null if explicit relocations have not been used. */
823
824static fragS *prev_reloc_op_frag;
825
252b5132
RH
826/* Map mips16 register numbers to normal MIPS register numbers. */
827
e972090a
NC
828static const unsigned int mips16_to_32_reg_map[] =
829{
252b5132
RH
830 16, 17, 2, 3, 4, 5, 6, 7
831};
60b63b72 832
df58fc94
RS
833/* Map microMIPS register numbers to normal MIPS register numbers. */
834
df58fc94 835#define micromips_to_32_reg_d_map mips16_to_32_reg_map
df58fc94
RS
836
837/* The microMIPS registers with type h. */
e76ff5ab 838static const unsigned int micromips_to_32_reg_h_map1[] =
df58fc94
RS
839{
840 5, 5, 6, 4, 4, 4, 4, 4
841};
e76ff5ab 842static const unsigned int micromips_to_32_reg_h_map2[] =
df58fc94
RS
843{
844 6, 7, 7, 21, 22, 5, 6, 7
845};
846
df58fc94
RS
847/* The microMIPS registers with type m. */
848static const unsigned int micromips_to_32_reg_m_map[] =
849{
850 0, 17, 2, 3, 16, 18, 19, 20
851};
852
853#define micromips_to_32_reg_n_map micromips_to_32_reg_m_map
854
71400594
RS
855/* Classifies the kind of instructions we're interested in when
856 implementing -mfix-vr4120. */
c67a084a
NC
857enum fix_vr4120_class
858{
71400594
RS
859 FIX_VR4120_MACC,
860 FIX_VR4120_DMACC,
861 FIX_VR4120_MULT,
862 FIX_VR4120_DMULT,
863 FIX_VR4120_DIV,
864 FIX_VR4120_MTHILO,
865 NUM_FIX_VR4120_CLASSES
866};
867
c67a084a
NC
868/* ...likewise -mfix-loongson2f-jump. */
869static bfd_boolean mips_fix_loongson2f_jump;
870
871/* ...likewise -mfix-loongson2f-nop. */
872static bfd_boolean mips_fix_loongson2f_nop;
873
874/* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed. */
875static bfd_boolean mips_fix_loongson2f;
876
71400594
RS
877/* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
878 there must be at least one other instruction between an instruction
879 of type X and an instruction of type Y. */
880static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
881
882/* True if -mfix-vr4120 is in force. */
d766e8ec 883static int mips_fix_vr4120;
4a6a3df4 884
7d8e00cf
RS
885/* ...likewise -mfix-vr4130. */
886static int mips_fix_vr4130;
887
6a32d874
CM
888/* ...likewise -mfix-24k. */
889static int mips_fix_24k;
890
a8d14a88
CM
891/* ...likewise -mfix-rm7000 */
892static int mips_fix_rm7000;
893
d954098f
DD
894/* ...likewise -mfix-cn63xxp1 */
895static bfd_boolean mips_fix_cn63xxp1;
896
4a6a3df4
AO
897/* We don't relax branches by default, since this causes us to expand
898 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
899 fail to compute the offset before expanding the macro to the most
900 efficient expansion. */
901
902static int mips_relax_branch;
252b5132 903\f
4d7206a2
RS
904/* The expansion of many macros depends on the type of symbol that
905 they refer to. For example, when generating position-dependent code,
906 a macro that refers to a symbol may have two different expansions,
907 one which uses GP-relative addresses and one which uses absolute
908 addresses. When generating SVR4-style PIC, a macro may have
909 different expansions for local and global symbols.
910
911 We handle these situations by generating both sequences and putting
912 them in variant frags. In position-dependent code, the first sequence
913 will be the GP-relative one and the second sequence will be the
914 absolute one. In SVR4 PIC, the first sequence will be for global
915 symbols and the second will be for local symbols.
916
584892a6
RS
917 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
918 SECOND are the lengths of the two sequences in bytes. These fields
919 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
920 the subtype has the following flags:
4d7206a2 921
584892a6
RS
922 RELAX_USE_SECOND
923 Set if it has been decided that we should use the second
924 sequence instead of the first.
925
926 RELAX_SECOND_LONGER
927 Set in the first variant frag if the macro's second implementation
928 is longer than its first. This refers to the macro as a whole,
929 not an individual relaxation.
930
931 RELAX_NOMACRO
932 Set in the first variant frag if the macro appeared in a .set nomacro
933 block and if one alternative requires a warning but the other does not.
934
935 RELAX_DELAY_SLOT
936 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
937 delay slot.
4d7206a2 938
df58fc94
RS
939 RELAX_DELAY_SLOT_16BIT
940 Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
941 16-bit instruction.
942
943 RELAX_DELAY_SLOT_SIZE_FIRST
944 Like RELAX_DELAY_SLOT, but indicates that the first implementation of
945 the macro is of the wrong size for the branch delay slot.
946
947 RELAX_DELAY_SLOT_SIZE_SECOND
948 Like RELAX_DELAY_SLOT, but indicates that the second implementation of
949 the macro is of the wrong size for the branch delay slot.
950
4d7206a2
RS
951 The frag's "opcode" points to the first fixup for relaxable code.
952
953 Relaxable macros are generated using a sequence such as:
954
955 relax_start (SYMBOL);
956 ... generate first expansion ...
957 relax_switch ();
958 ... generate second expansion ...
959 relax_end ();
960
961 The code and fixups for the unwanted alternative are discarded
962 by md_convert_frag. */
584892a6 963#define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
4d7206a2 964
584892a6
RS
965#define RELAX_FIRST(X) (((X) >> 8) & 0xff)
966#define RELAX_SECOND(X) ((X) & 0xff)
967#define RELAX_USE_SECOND 0x10000
968#define RELAX_SECOND_LONGER 0x20000
969#define RELAX_NOMACRO 0x40000
970#define RELAX_DELAY_SLOT 0x80000
df58fc94
RS
971#define RELAX_DELAY_SLOT_16BIT 0x100000
972#define RELAX_DELAY_SLOT_SIZE_FIRST 0x200000
973#define RELAX_DELAY_SLOT_SIZE_SECOND 0x400000
252b5132 974
4a6a3df4
AO
975/* Branch without likely bit. If label is out of range, we turn:
976
977 beq reg1, reg2, label
978 delay slot
979
980 into
981
982 bne reg1, reg2, 0f
983 nop
984 j label
985 0: delay slot
986
987 with the following opcode replacements:
988
989 beq <-> bne
990 blez <-> bgtz
991 bltz <-> bgez
992 bc1f <-> bc1t
993
994 bltzal <-> bgezal (with jal label instead of j label)
995
996 Even though keeping the delay slot instruction in the delay slot of
997 the branch would be more efficient, it would be very tricky to do
998 correctly, because we'd have to introduce a variable frag *after*
999 the delay slot instruction, and expand that instead. Let's do it
1000 the easy way for now, even if the branch-not-taken case now costs
1001 one additional instruction. Out-of-range branches are not supposed
1002 to be common, anyway.
1003
1004 Branch likely. If label is out of range, we turn:
1005
1006 beql reg1, reg2, label
1007 delay slot (annulled if branch not taken)
1008
1009 into
1010
1011 beql reg1, reg2, 1f
1012 nop
1013 beql $0, $0, 2f
1014 nop
1015 1: j[al] label
1016 delay slot (executed only if branch taken)
1017 2:
1018
1019 It would be possible to generate a shorter sequence by losing the
1020 likely bit, generating something like:
b34976b6 1021
4a6a3df4
AO
1022 bne reg1, reg2, 0f
1023 nop
1024 j[al] label
1025 delay slot (executed only if branch taken)
1026 0:
1027
1028 beql -> bne
1029 bnel -> beq
1030 blezl -> bgtz
1031 bgtzl -> blez
1032 bltzl -> bgez
1033 bgezl -> bltz
1034 bc1fl -> bc1t
1035 bc1tl -> bc1f
1036
1037 bltzall -> bgezal (with jal label instead of j label)
1038 bgezall -> bltzal (ditto)
1039
1040
1041 but it's not clear that it would actually improve performance. */
66b3e8da
MR
1042#define RELAX_BRANCH_ENCODE(at, uncond, likely, link, toofar) \
1043 ((relax_substateT) \
1044 (0xc0000000 \
1045 | ((at) & 0x1f) \
1046 | ((toofar) ? 0x20 : 0) \
1047 | ((link) ? 0x40 : 0) \
1048 | ((likely) ? 0x80 : 0) \
1049 | ((uncond) ? 0x100 : 0)))
4a6a3df4 1050#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
66b3e8da
MR
1051#define RELAX_BRANCH_UNCOND(i) (((i) & 0x100) != 0)
1052#define RELAX_BRANCH_LIKELY(i) (((i) & 0x80) != 0)
1053#define RELAX_BRANCH_LINK(i) (((i) & 0x40) != 0)
1054#define RELAX_BRANCH_TOOFAR(i) (((i) & 0x20) != 0)
1055#define RELAX_BRANCH_AT(i) ((i) & 0x1f)
4a6a3df4 1056
252b5132
RH
1057/* For mips16 code, we use an entirely different form of relaxation.
1058 mips16 supports two versions of most instructions which take
1059 immediate values: a small one which takes some small value, and a
1060 larger one which takes a 16 bit value. Since branches also follow
1061 this pattern, relaxing these values is required.
1062
1063 We can assemble both mips16 and normal MIPS code in a single
1064 object. Therefore, we need to support this type of relaxation at
1065 the same time that we support the relaxation described above. We
1066 use the high bit of the subtype field to distinguish these cases.
1067
1068 The information we store for this type of relaxation is the
1069 argument code found in the opcode file for this relocation, whether
1070 the user explicitly requested a small or extended form, and whether
1071 the relocation is in a jump or jal delay slot. That tells us the
1072 size of the value, and how it should be stored. We also store
1073 whether the fragment is considered to be extended or not. We also
1074 store whether this is known to be a branch to a different section,
1075 whether we have tried to relax this frag yet, and whether we have
1076 ever extended a PC relative fragment because of a shift count. */
1077#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
1078 (0x80000000 \
1079 | ((type) & 0xff) \
1080 | ((small) ? 0x100 : 0) \
1081 | ((ext) ? 0x200 : 0) \
1082 | ((dslot) ? 0x400 : 0) \
1083 | ((jal_dslot) ? 0x800 : 0))
4a6a3df4 1084#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132
RH
1085#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
1086#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
1087#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
1088#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
1089#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
1090#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
1091#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
1092#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
1093#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
1094#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
1095#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
885add95 1096
df58fc94
RS
1097/* For microMIPS code, we use relaxation similar to one we use for
1098 MIPS16 code. Some instructions that take immediate values support
1099 two encodings: a small one which takes some small value, and a
1100 larger one which takes a 16 bit value. As some branches also follow
1101 this pattern, relaxing these values is required.
1102
1103 We can assemble both microMIPS and normal MIPS code in a single
1104 object. Therefore, we need to support this type of relaxation at
1105 the same time that we support the relaxation described above. We
1106 use one of the high bits of the subtype field to distinguish these
1107 cases.
1108
1109 The information we store for this type of relaxation is the argument
1110 code found in the opcode file for this relocation, the register
40209cad
MR
1111 selected as the assembler temporary, whether the branch is
1112 unconditional, whether it is compact, whether it stores the link
1113 address implicitly in $ra, whether relaxation of out-of-range 32-bit
1114 branches to a sequence of instructions is enabled, and whether the
1115 displacement of a branch is too large to fit as an immediate argument
1116 of a 16-bit and a 32-bit branch, respectively. */
1117#define RELAX_MICROMIPS_ENCODE(type, at, uncond, compact, link, \
1118 relax32, toofar16, toofar32) \
1119 (0x40000000 \
1120 | ((type) & 0xff) \
1121 | (((at) & 0x1f) << 8) \
1122 | ((uncond) ? 0x2000 : 0) \
1123 | ((compact) ? 0x4000 : 0) \
1124 | ((link) ? 0x8000 : 0) \
1125 | ((relax32) ? 0x10000 : 0) \
1126 | ((toofar16) ? 0x20000 : 0) \
1127 | ((toofar32) ? 0x40000 : 0))
df58fc94
RS
1128#define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1129#define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1130#define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
40209cad
MR
1131#define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x2000) != 0)
1132#define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x4000) != 0)
1133#define RELAX_MICROMIPS_LINK(i) (((i) & 0x8000) != 0)
1134#define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x10000) != 0)
1135
1136#define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x20000) != 0)
1137#define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x20000)
1138#define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x20000)
1139#define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x40000) != 0)
1140#define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x40000)
1141#define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x40000)
df58fc94 1142
43c0598f
RS
1143/* Sign-extend 16-bit value X. */
1144#define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1145
885add95
CD
1146/* Is the given value a sign-extended 32-bit value? */
1147#define IS_SEXT_32BIT_NUM(x) \
1148 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1149 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1150
1151/* Is the given value a sign-extended 16-bit value? */
1152#define IS_SEXT_16BIT_NUM(x) \
1153 (((x) &~ (offsetT) 0x7fff) == 0 \
1154 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1155
df58fc94
RS
1156/* Is the given value a sign-extended 12-bit value? */
1157#define IS_SEXT_12BIT_NUM(x) \
1158 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1159
7f3c4072
CM
1160/* Is the given value a sign-extended 9-bit value? */
1161#define IS_SEXT_9BIT_NUM(x) \
1162 (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1163
2051e8c4
MR
1164/* Is the given value a zero-extended 32-bit value? Or a negated one? */
1165#define IS_ZEXT_32BIT_NUM(x) \
1166 (((x) &~ (offsetT) 0xffffffff) == 0 \
1167 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1168
bf12938e
RS
1169/* Extract bits MASK << SHIFT from STRUCT and shift them right
1170 SHIFT places. */
1171#define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1172 (((STRUCT) >> (SHIFT)) & (MASK))
1173
bf12938e 1174/* Extract the operand given by FIELD from mips_cl_insn INSN. */
df58fc94
RS
1175#define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1176 (!(MICROMIPS) \
1177 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1178 : EXTRACT_BITS ((INSN).insn_opcode, \
1179 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
bf12938e
RS
1180#define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1181 EXTRACT_BITS ((INSN).insn_opcode, \
1182 MIPS16OP_MASK_##FIELD, \
1183 MIPS16OP_SH_##FIELD)
5c04167a
RS
1184
1185/* The MIPS16 EXTEND opcode, shifted left 16 places. */
1186#define MIPS16_EXTEND (0xf000U << 16)
4d7206a2 1187\f
df58fc94
RS
1188/* Whether or not we are emitting a branch-likely macro. */
1189static bfd_boolean emit_branch_likely_macro = FALSE;
1190
4d7206a2
RS
1191/* Global variables used when generating relaxable macros. See the
1192 comment above RELAX_ENCODE for more details about how relaxation
1193 is used. */
1194static struct {
1195 /* 0 if we're not emitting a relaxable macro.
1196 1 if we're emitting the first of the two relaxation alternatives.
1197 2 if we're emitting the second alternative. */
1198 int sequence;
1199
1200 /* The first relaxable fixup in the current frag. (In other words,
1201 the first fixup that refers to relaxable code.) */
1202 fixS *first_fixup;
1203
1204 /* sizes[0] says how many bytes of the first alternative are stored in
1205 the current frag. Likewise sizes[1] for the second alternative. */
1206 unsigned int sizes[2];
1207
1208 /* The symbol on which the choice of sequence depends. */
1209 symbolS *symbol;
1210} mips_relax;
252b5132 1211\f
584892a6
RS
1212/* Global variables used to decide whether a macro needs a warning. */
1213static struct {
1214 /* True if the macro is in a branch delay slot. */
1215 bfd_boolean delay_slot_p;
1216
df58fc94
RS
1217 /* Set to the length in bytes required if the macro is in a delay slot
1218 that requires a specific length of instruction, otherwise zero. */
1219 unsigned int delay_slot_length;
1220
584892a6
RS
1221 /* For relaxable macros, sizes[0] is the length of the first alternative
1222 in bytes and sizes[1] is the length of the second alternative.
1223 For non-relaxable macros, both elements give the length of the
1224 macro in bytes. */
1225 unsigned int sizes[2];
1226
df58fc94
RS
1227 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1228 instruction of the first alternative in bytes and first_insn_sizes[1]
1229 is the length of the first instruction of the second alternative.
1230 For non-relaxable macros, both elements give the length of the first
1231 instruction in bytes.
1232
1233 Set to zero if we haven't yet seen the first instruction. */
1234 unsigned int first_insn_sizes[2];
1235
1236 /* For relaxable macros, insns[0] is the number of instructions for the
1237 first alternative and insns[1] is the number of instructions for the
1238 second alternative.
1239
1240 For non-relaxable macros, both elements give the number of
1241 instructions for the macro. */
1242 unsigned int insns[2];
1243
584892a6
RS
1244 /* The first variant frag for this macro. */
1245 fragS *first_frag;
1246} mips_macro_warning;
1247\f
252b5132
RH
1248/* Prototypes for static functions. */
1249
252b5132
RH
1250enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1251
b34976b6 1252static void append_insn
df58fc94
RS
1253 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1254 bfd_boolean expansionp);
7d10b47d 1255static void mips_no_prev_insn (void);
c67a084a 1256static void macro_build (expressionS *, const char *, const char *, ...);
b34976b6 1257static void mips16_macro_build
03ea81db 1258 (expressionS *, const char *, const char *, va_list *);
67c0d1eb 1259static void load_register (int, expressionS *, int);
584892a6
RS
1260static void macro_start (void);
1261static void macro_end (void);
833794fc 1262static void macro (struct mips_cl_insn *ip, char *str);
17a2f251 1263static void mips16_macro (struct mips_cl_insn * ip);
17a2f251
TS
1264static void mips_ip (char *str, struct mips_cl_insn * ip);
1265static void mips16_ip (char *str, struct mips_cl_insn * ip);
b34976b6 1266static void mips16_immed
43c0598f
RS
1267 (char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
1268 unsigned int, unsigned long *);
5e0116d5 1269static size_t my_getSmallExpression
17a2f251
TS
1270 (expressionS *, bfd_reloc_code_real_type *, char *);
1271static void my_getExpression (expressionS *, char *);
1272static void s_align (int);
1273static void s_change_sec (int);
1274static void s_change_section (int);
1275static void s_cons (int);
1276static void s_float_cons (int);
1277static void s_mips_globl (int);
1278static void s_option (int);
1279static void s_mipsset (int);
1280static void s_abicalls (int);
1281static void s_cpload (int);
1282static void s_cpsetup (int);
1283static void s_cplocal (int);
1284static void s_cprestore (int);
1285static void s_cpreturn (int);
741d6ea8
JM
1286static void s_dtprelword (int);
1287static void s_dtpreldword (int);
d0f13682
CLT
1288static void s_tprelword (int);
1289static void s_tpreldword (int);
17a2f251
TS
1290static void s_gpvalue (int);
1291static void s_gpword (int);
1292static void s_gpdword (int);
a3f278e2 1293static void s_ehword (int);
17a2f251
TS
1294static void s_cpadd (int);
1295static void s_insn (int);
ba92f887 1296static void s_nan (int);
17a2f251
TS
1297static void md_obj_begin (void);
1298static void md_obj_end (void);
1299static void s_mips_ent (int);
1300static void s_mips_end (int);
1301static void s_mips_frame (int);
1302static void s_mips_mask (int reg_type);
1303static void s_mips_stab (int);
1304static void s_mips_weakext (int);
1305static void s_mips_file (int);
1306static void s_mips_loc (int);
1307static bfd_boolean pic_need_relax (symbolS *, asection *);
4a6a3df4 1308static int relaxed_branch_length (fragS *, asection *, int);
df58fc94
RS
1309static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1310static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
e7af610e
NC
1311
1312/* Table and functions used to map between CPU/ISA names, and
1313 ISA levels, and CPU numbers. */
1314
e972090a
NC
1315struct mips_cpu_info
1316{
e7af610e 1317 const char *name; /* CPU or ISA name. */
d16afab6
RS
1318 int flags; /* MIPS_CPU_* flags. */
1319 int ase; /* Set of ASEs implemented by the CPU. */
e7af610e
NC
1320 int isa; /* ISA level. */
1321 int cpu; /* CPU number (default CPU if ISA). */
1322};
1323
ad3fea08 1324#define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
ad3fea08 1325
17a2f251
TS
1326static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1327static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1328static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132 1329\f
c31f3936
RS
1330/* Command-line options. */
1331const char *md_shortopts = "O::g::G:";
1332
1333enum options
1334 {
1335 OPTION_MARCH = OPTION_MD_BASE,
1336 OPTION_MTUNE,
1337 OPTION_MIPS1,
1338 OPTION_MIPS2,
1339 OPTION_MIPS3,
1340 OPTION_MIPS4,
1341 OPTION_MIPS5,
1342 OPTION_MIPS32,
1343 OPTION_MIPS64,
1344 OPTION_MIPS32R2,
ae52f483
AB
1345 OPTION_MIPS32R3,
1346 OPTION_MIPS32R5,
c31f3936 1347 OPTION_MIPS64R2,
ae52f483
AB
1348 OPTION_MIPS64R3,
1349 OPTION_MIPS64R5,
c31f3936
RS
1350 OPTION_MIPS16,
1351 OPTION_NO_MIPS16,
1352 OPTION_MIPS3D,
1353 OPTION_NO_MIPS3D,
1354 OPTION_MDMX,
1355 OPTION_NO_MDMX,
1356 OPTION_DSP,
1357 OPTION_NO_DSP,
1358 OPTION_MT,
1359 OPTION_NO_MT,
1360 OPTION_VIRT,
1361 OPTION_NO_VIRT,
56d438b1
CF
1362 OPTION_MSA,
1363 OPTION_NO_MSA,
c31f3936
RS
1364 OPTION_SMARTMIPS,
1365 OPTION_NO_SMARTMIPS,
1366 OPTION_DSPR2,
1367 OPTION_NO_DSPR2,
1368 OPTION_EVA,
1369 OPTION_NO_EVA,
7d64c587
AB
1370 OPTION_XPA,
1371 OPTION_NO_XPA,
c31f3936
RS
1372 OPTION_MICROMIPS,
1373 OPTION_NO_MICROMIPS,
1374 OPTION_MCU,
1375 OPTION_NO_MCU,
1376 OPTION_COMPAT_ARCH_BASE,
1377 OPTION_M4650,
1378 OPTION_NO_M4650,
1379 OPTION_M4010,
1380 OPTION_NO_M4010,
1381 OPTION_M4100,
1382 OPTION_NO_M4100,
1383 OPTION_M3900,
1384 OPTION_NO_M3900,
1385 OPTION_M7000_HILO_FIX,
1386 OPTION_MNO_7000_HILO_FIX,
1387 OPTION_FIX_24K,
1388 OPTION_NO_FIX_24K,
a8d14a88
CM
1389 OPTION_FIX_RM7000,
1390 OPTION_NO_FIX_RM7000,
c31f3936
RS
1391 OPTION_FIX_LOONGSON2F_JUMP,
1392 OPTION_NO_FIX_LOONGSON2F_JUMP,
1393 OPTION_FIX_LOONGSON2F_NOP,
1394 OPTION_NO_FIX_LOONGSON2F_NOP,
1395 OPTION_FIX_VR4120,
1396 OPTION_NO_FIX_VR4120,
1397 OPTION_FIX_VR4130,
1398 OPTION_NO_FIX_VR4130,
1399 OPTION_FIX_CN63XXP1,
1400 OPTION_NO_FIX_CN63XXP1,
1401 OPTION_TRAP,
1402 OPTION_BREAK,
1403 OPTION_EB,
1404 OPTION_EL,
1405 OPTION_FP32,
1406 OPTION_GP32,
1407 OPTION_CONSTRUCT_FLOATS,
1408 OPTION_NO_CONSTRUCT_FLOATS,
1409 OPTION_FP64,
1410 OPTION_GP64,
1411 OPTION_RELAX_BRANCH,
1412 OPTION_NO_RELAX_BRANCH,
833794fc
MR
1413 OPTION_INSN32,
1414 OPTION_NO_INSN32,
c31f3936
RS
1415 OPTION_MSHARED,
1416 OPTION_MNO_SHARED,
1417 OPTION_MSYM32,
1418 OPTION_MNO_SYM32,
1419 OPTION_SOFT_FLOAT,
1420 OPTION_HARD_FLOAT,
1421 OPTION_SINGLE_FLOAT,
1422 OPTION_DOUBLE_FLOAT,
1423 OPTION_32,
c31f3936
RS
1424 OPTION_CALL_SHARED,
1425 OPTION_CALL_NONPIC,
1426 OPTION_NON_SHARED,
1427 OPTION_XGOT,
1428 OPTION_MABI,
1429 OPTION_N32,
1430 OPTION_64,
1431 OPTION_MDEBUG,
1432 OPTION_NO_MDEBUG,
1433 OPTION_PDR,
1434 OPTION_NO_PDR,
1435 OPTION_MVXWORKS_PIC,
ba92f887 1436 OPTION_NAN,
c31f3936
RS
1437 OPTION_END_OF_ENUM
1438 };
1439
1440struct option md_longopts[] =
1441{
1442 /* Options which specify architecture. */
1443 {"march", required_argument, NULL, OPTION_MARCH},
1444 {"mtune", required_argument, NULL, OPTION_MTUNE},
1445 {"mips0", no_argument, NULL, OPTION_MIPS1},
1446 {"mips1", no_argument, NULL, OPTION_MIPS1},
1447 {"mips2", no_argument, NULL, OPTION_MIPS2},
1448 {"mips3", no_argument, NULL, OPTION_MIPS3},
1449 {"mips4", no_argument, NULL, OPTION_MIPS4},
1450 {"mips5", no_argument, NULL, OPTION_MIPS5},
1451 {"mips32", no_argument, NULL, OPTION_MIPS32},
1452 {"mips64", no_argument, NULL, OPTION_MIPS64},
1453 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
ae52f483
AB
1454 {"mips32r3", no_argument, NULL, OPTION_MIPS32R3},
1455 {"mips32r5", no_argument, NULL, OPTION_MIPS32R5},
c31f3936 1456 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
ae52f483
AB
1457 {"mips64r3", no_argument, NULL, OPTION_MIPS64R3},
1458 {"mips64r5", no_argument, NULL, OPTION_MIPS64R5},
c31f3936
RS
1459
1460 /* Options which specify Application Specific Extensions (ASEs). */
1461 {"mips16", no_argument, NULL, OPTION_MIPS16},
1462 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1463 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1464 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1465 {"mdmx", no_argument, NULL, OPTION_MDMX},
1466 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1467 {"mdsp", no_argument, NULL, OPTION_DSP},
1468 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1469 {"mmt", no_argument, NULL, OPTION_MT},
1470 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1471 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1472 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1473 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1474 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
1475 {"meva", no_argument, NULL, OPTION_EVA},
1476 {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1477 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1478 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1479 {"mmcu", no_argument, NULL, OPTION_MCU},
1480 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1481 {"mvirt", no_argument, NULL, OPTION_VIRT},
1482 {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
56d438b1
CF
1483 {"mmsa", no_argument, NULL, OPTION_MSA},
1484 {"mno-msa", no_argument, NULL, OPTION_NO_MSA},
7d64c587
AB
1485 {"mxpa", no_argument, NULL, OPTION_XPA},
1486 {"mno-xpa", no_argument, NULL, OPTION_NO_XPA},
c31f3936
RS
1487
1488 /* Old-style architecture options. Don't add more of these. */
1489 {"m4650", no_argument, NULL, OPTION_M4650},
1490 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1491 {"m4010", no_argument, NULL, OPTION_M4010},
1492 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1493 {"m4100", no_argument, NULL, OPTION_M4100},
1494 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1495 {"m3900", no_argument, NULL, OPTION_M3900},
1496 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1497
1498 /* Options which enable bug fixes. */
1499 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1500 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1501 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1502 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1503 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1504 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1505 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1506 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
1507 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1508 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
1509 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1510 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
1511 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
a8d14a88
CM
1512 {"mfix-rm7000", no_argument, NULL, OPTION_FIX_RM7000},
1513 {"mno-fix-rm7000", no_argument, NULL, OPTION_NO_FIX_RM7000},
c31f3936
RS
1514 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1515 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
1516
1517 /* Miscellaneous options. */
1518 {"trap", no_argument, NULL, OPTION_TRAP},
1519 {"no-break", no_argument, NULL, OPTION_TRAP},
1520 {"break", no_argument, NULL, OPTION_BREAK},
1521 {"no-trap", no_argument, NULL, OPTION_BREAK},
1522 {"EB", no_argument, NULL, OPTION_EB},
1523 {"EL", no_argument, NULL, OPTION_EL},
1524 {"mfp32", no_argument, NULL, OPTION_FP32},
1525 {"mgp32", no_argument, NULL, OPTION_GP32},
1526 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1527 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1528 {"mfp64", no_argument, NULL, OPTION_FP64},
1529 {"mgp64", no_argument, NULL, OPTION_GP64},
1530 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1531 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
833794fc
MR
1532 {"minsn32", no_argument, NULL, OPTION_INSN32},
1533 {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
c31f3936
RS
1534 {"mshared", no_argument, NULL, OPTION_MSHARED},
1535 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1536 {"msym32", no_argument, NULL, OPTION_MSYM32},
1537 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1538 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1539 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1540 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1541 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
1542
1543 /* Strictly speaking this next option is ELF specific,
1544 but we allow it for other ports as well in order to
1545 make testing easier. */
1546 {"32", no_argument, NULL, OPTION_32},
1547
1548 /* ELF-specific options. */
c31f3936
RS
1549 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1550 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1551 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1552 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
1553 {"xgot", no_argument, NULL, OPTION_XGOT},
1554 {"mabi", required_argument, NULL, OPTION_MABI},
1555 {"n32", no_argument, NULL, OPTION_N32},
1556 {"64", no_argument, NULL, OPTION_64},
1557 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1558 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1559 {"mpdr", no_argument, NULL, OPTION_PDR},
1560 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1561 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
ba92f887 1562 {"mnan", required_argument, NULL, OPTION_NAN},
c31f3936
RS
1563
1564 {NULL, no_argument, NULL, 0}
1565};
1566size_t md_longopts_size = sizeof (md_longopts);
1567\f
c6278170
RS
1568/* Information about either an Application Specific Extension or an
1569 optional architecture feature that, for simplicity, we treat in the
1570 same way as an ASE. */
1571struct mips_ase
1572{
1573 /* The name of the ASE, used in both the command-line and .set options. */
1574 const char *name;
1575
1576 /* The associated ASE_* flags. If the ASE is available on both 32-bit
1577 and 64-bit architectures, the flags here refer to the subset that
1578 is available on both. */
1579 unsigned int flags;
1580
1581 /* The ASE_* flag used for instructions that are available on 64-bit
1582 architectures but that are not included in FLAGS. */
1583 unsigned int flags64;
1584
1585 /* The command-line options that turn the ASE on and off. */
1586 int option_on;
1587 int option_off;
1588
1589 /* The minimum required architecture revisions for MIPS32, MIPS64,
1590 microMIPS32 and microMIPS64, or -1 if the extension isn't supported. */
1591 int mips32_rev;
1592 int mips64_rev;
1593 int micromips32_rev;
1594 int micromips64_rev;
1595};
1596
1597/* A table of all supported ASEs. */
1598static const struct mips_ase mips_ases[] = {
1599 { "dsp", ASE_DSP, ASE_DSP64,
1600 OPTION_DSP, OPTION_NO_DSP,
1601 2, 2, 2, 2 },
1602
1603 { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1604 OPTION_DSPR2, OPTION_NO_DSPR2,
1605 2, 2, 2, 2 },
1606
1607 { "eva", ASE_EVA, 0,
1608 OPTION_EVA, OPTION_NO_EVA,
1609 2, 2, 2, 2 },
1610
1611 { "mcu", ASE_MCU, 0,
1612 OPTION_MCU, OPTION_NO_MCU,
1613 2, 2, 2, 2 },
1614
1615 /* Deprecated in MIPS64r5, but we don't implement that yet. */
1616 { "mdmx", ASE_MDMX, 0,
1617 OPTION_MDMX, OPTION_NO_MDMX,
1618 -1, 1, -1, -1 },
1619
1620 /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2. */
1621 { "mips3d", ASE_MIPS3D, 0,
1622 OPTION_MIPS3D, OPTION_NO_MIPS3D,
1623 2, 1, -1, -1 },
1624
1625 { "mt", ASE_MT, 0,
1626 OPTION_MT, OPTION_NO_MT,
1627 2, 2, -1, -1 },
1628
1629 { "smartmips", ASE_SMARTMIPS, 0,
1630 OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
1631 1, -1, -1, -1 },
1632
1633 { "virt", ASE_VIRT, ASE_VIRT64,
1634 OPTION_VIRT, OPTION_NO_VIRT,
56d438b1
CF
1635 2, 2, 2, 2 },
1636
1637 { "msa", ASE_MSA, ASE_MSA64,
1638 OPTION_MSA, OPTION_NO_MSA,
7d64c587
AB
1639 2, 2, 2, 2 },
1640
1641 { "xpa", ASE_XPA, 0,
1642 OPTION_XPA, OPTION_NO_XPA,
1643 2, 2, -1, -1 }
c6278170
RS
1644};
1645
1646/* The set of ASEs that require -mfp64. */
1647#define FP64_ASES (ASE_MIPS3D | ASE_MDMX)
1648
1649/* Groups of ASE_* flags that represent different revisions of an ASE. */
1650static const unsigned int mips_ase_groups[] = {
1651 ASE_DSP | ASE_DSPR2
1652};
1653\f
252b5132
RH
1654/* Pseudo-op table.
1655
1656 The following pseudo-ops from the Kane and Heinrich MIPS book
1657 should be defined here, but are currently unsupported: .alias,
1658 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1659
1660 The following pseudo-ops from the Kane and Heinrich MIPS book are
1661 specific to the type of debugging information being generated, and
1662 should be defined by the object format: .aent, .begin, .bend,
1663 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1664 .vreg.
1665
1666 The following pseudo-ops from the Kane and Heinrich MIPS book are
1667 not MIPS CPU specific, but are also not specific to the object file
1668 format. This file is probably the best place to define them, but
d84bcf09 1669 they are not currently supported: .asm0, .endr, .lab, .struct. */
252b5132 1670
e972090a
NC
1671static const pseudo_typeS mips_pseudo_table[] =
1672{
beae10d5 1673 /* MIPS specific pseudo-ops. */
252b5132
RH
1674 {"option", s_option, 0},
1675 {"set", s_mipsset, 0},
1676 {"rdata", s_change_sec, 'r'},
1677 {"sdata", s_change_sec, 's'},
1678 {"livereg", s_ignore, 0},
1679 {"abicalls", s_abicalls, 0},
1680 {"cpload", s_cpload, 0},
6478892d
TS
1681 {"cpsetup", s_cpsetup, 0},
1682 {"cplocal", s_cplocal, 0},
252b5132 1683 {"cprestore", s_cprestore, 0},
6478892d 1684 {"cpreturn", s_cpreturn, 0},
741d6ea8
JM
1685 {"dtprelword", s_dtprelword, 0},
1686 {"dtpreldword", s_dtpreldword, 0},
d0f13682
CLT
1687 {"tprelword", s_tprelword, 0},
1688 {"tpreldword", s_tpreldword, 0},
6478892d 1689 {"gpvalue", s_gpvalue, 0},
252b5132 1690 {"gpword", s_gpword, 0},
10181a0d 1691 {"gpdword", s_gpdword, 0},
a3f278e2 1692 {"ehword", s_ehword, 0},
252b5132
RH
1693 {"cpadd", s_cpadd, 0},
1694 {"insn", s_insn, 0},
ba92f887 1695 {"nan", s_nan, 0},
252b5132 1696
beae10d5 1697 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132 1698 chips. */
38a57ae7 1699 {"asciiz", stringer, 8 + 1},
252b5132
RH
1700 {"bss", s_change_sec, 'b'},
1701 {"err", s_err, 0},
1702 {"half", s_cons, 1},
1703 {"dword", s_cons, 3},
1704 {"weakext", s_mips_weakext, 0},
7c752c2a
TS
1705 {"origin", s_org, 0},
1706 {"repeat", s_rept, 0},
252b5132 1707
998b3c36
MR
1708 /* For MIPS this is non-standard, but we define it for consistency. */
1709 {"sbss", s_change_sec, 'B'},
1710
beae10d5 1711 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1712 here for one reason or another. */
1713 {"align", s_align, 0},
1714 {"byte", s_cons, 0},
1715 {"data", s_change_sec, 'd'},
1716 {"double", s_float_cons, 'd'},
1717 {"float", s_float_cons, 'f'},
1718 {"globl", s_mips_globl, 0},
1719 {"global", s_mips_globl, 0},
1720 {"hword", s_cons, 1},
1721 {"int", s_cons, 2},
1722 {"long", s_cons, 2},
1723 {"octa", s_cons, 4},
1724 {"quad", s_cons, 3},
cca86cc8 1725 {"section", s_change_section, 0},
252b5132
RH
1726 {"short", s_cons, 1},
1727 {"single", s_float_cons, 'f'},
754e2bb9 1728 {"stabd", s_mips_stab, 'd'},
252b5132 1729 {"stabn", s_mips_stab, 'n'},
754e2bb9 1730 {"stabs", s_mips_stab, 's'},
252b5132
RH
1731 {"text", s_change_sec, 't'},
1732 {"word", s_cons, 2},
add56521 1733
add56521 1734 { "extern", ecoff_directive_extern, 0},
add56521 1735
43841e91 1736 { NULL, NULL, 0 },
252b5132
RH
1737};
1738
e972090a
NC
1739static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1740{
beae10d5
KH
1741 /* These pseudo-ops should be defined by the object file format.
1742 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1743 {"aent", s_mips_ent, 1},
1744 {"bgnb", s_ignore, 0},
1745 {"end", s_mips_end, 0},
1746 {"endb", s_ignore, 0},
1747 {"ent", s_mips_ent, 0},
c5dd6aab 1748 {"file", s_mips_file, 0},
252b5132
RH
1749 {"fmask", s_mips_mask, 'F'},
1750 {"frame", s_mips_frame, 0},
c5dd6aab 1751 {"loc", s_mips_loc, 0},
252b5132
RH
1752 {"mask", s_mips_mask, 'R'},
1753 {"verstamp", s_ignore, 0},
43841e91 1754 { NULL, NULL, 0 },
252b5132
RH
1755};
1756
3ae8dd8d
MR
1757/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1758 purpose of the `.dc.a' internal pseudo-op. */
1759
1760int
1761mips_address_bytes (void)
1762{
1763 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1764}
1765
17a2f251 1766extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1767
1768void
17a2f251 1769mips_pop_insert (void)
252b5132
RH
1770{
1771 pop_insert (mips_pseudo_table);
1772 if (! ECOFF_DEBUGGING)
1773 pop_insert (mips_nonecoff_pseudo_table);
1774}
1775\f
1776/* Symbols labelling the current insn. */
1777
e972090a
NC
1778struct insn_label_list
1779{
252b5132
RH
1780 struct insn_label_list *next;
1781 symbolS *label;
1782};
1783
252b5132 1784static struct insn_label_list *free_insn_labels;
742a56fe 1785#define label_list tc_segment_info_data.labels
252b5132 1786
17a2f251 1787static void mips_clear_insn_labels (void);
df58fc94
RS
1788static void mips_mark_labels (void);
1789static void mips_compressed_mark_labels (void);
252b5132
RH
1790
1791static inline void
17a2f251 1792mips_clear_insn_labels (void)
252b5132
RH
1793{
1794 register struct insn_label_list **pl;
a8dbcb85 1795 segment_info_type *si;
252b5132 1796
a8dbcb85
TS
1797 if (now_seg)
1798 {
1799 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1800 ;
1801
1802 si = seg_info (now_seg);
1803 *pl = si->label_list;
1804 si->label_list = NULL;
1805 }
252b5132 1806}
a8dbcb85 1807
df58fc94
RS
1808/* Mark instruction labels in MIPS16/microMIPS mode. */
1809
1810static inline void
1811mips_mark_labels (void)
1812{
1813 if (HAVE_CODE_COMPRESSION)
1814 mips_compressed_mark_labels ();
1815}
252b5132
RH
1816\f
1817static char *expr_end;
1818
e423441d 1819/* An expression in a macro instruction. This is set by mips_ip and
b0e6f033 1820 mips16_ip and when populated is always an O_constant. */
252b5132
RH
1821
1822static expressionS imm_expr;
252b5132 1823
77bd4346
RS
1824/* The relocatable field in an instruction and the relocs associated
1825 with it. These variables are used for instructions like LUI and
1826 JAL as well as true offsets. They are also used for address
1827 operands in macros. */
252b5132 1828
77bd4346 1829static expressionS offset_expr;
f6688943
TS
1830static bfd_reloc_code_real_type offset_reloc[3]
1831 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 1832
df58fc94
RS
1833/* This is set to the resulting size of the instruction to be produced
1834 by mips16_ip if an explicit extension is used or by mips_ip if an
1835 explicit size is supplied. */
252b5132 1836
df58fc94 1837static unsigned int forced_insn_length;
252b5132 1838
e1b47bd5
RS
1839/* True if we are assembling an instruction. All dot symbols defined during
1840 this time should be treated as code labels. */
1841
1842static bfd_boolean mips_assembling_insn;
1843
ecb4347a
DJ
1844/* The pdr segment for per procedure frame/regmask info. Not used for
1845 ECOFF debugging. */
252b5132
RH
1846
1847static segT pdr_seg;
252b5132 1848
e013f690
TS
1849/* The default target format to use. */
1850
aeffff67
RS
1851#if defined (TE_FreeBSD)
1852#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
1853#elif defined (TE_TMIPS)
1854#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
1855#else
1856#define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
1857#endif
1858
e013f690 1859const char *
17a2f251 1860mips_target_format (void)
e013f690
TS
1861{
1862 switch (OUTPUT_FLAVOR)
1863 {
e013f690 1864 case bfd_target_elf_flavour:
0a44bf69
RS
1865#ifdef TE_VXWORKS
1866 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1867 return (target_big_endian
1868 ? "elf32-bigmips-vxworks"
1869 : "elf32-littlemips-vxworks");
1870#endif
e013f690 1871 return (target_big_endian
cfe86eaa 1872 ? (HAVE_64BIT_OBJECTS
aeffff67 1873 ? ELF_TARGET ("elf64-", "big")
cfe86eaa 1874 : (HAVE_NEWABI
aeffff67
RS
1875 ? ELF_TARGET ("elf32-n", "big")
1876 : ELF_TARGET ("elf32-", "big")))
cfe86eaa 1877 : (HAVE_64BIT_OBJECTS
aeffff67 1878 ? ELF_TARGET ("elf64-", "little")
cfe86eaa 1879 : (HAVE_NEWABI
aeffff67
RS
1880 ? ELF_TARGET ("elf32-n", "little")
1881 : ELF_TARGET ("elf32-", "little"))));
e013f690
TS
1882 default:
1883 abort ();
1884 return NULL;
1885 }
1886}
1887
c6278170
RS
1888/* Return the ISA revision that is currently in use, or 0 if we are
1889 generating code for MIPS V or below. */
1890
1891static int
1892mips_isa_rev (void)
1893{
1894 if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
1895 return 2;
1896
ae52f483
AB
1897 if (mips_opts.isa == ISA_MIPS32R3 || mips_opts.isa == ISA_MIPS64R3)
1898 return 3;
1899
1900 if (mips_opts.isa == ISA_MIPS32R5 || mips_opts.isa == ISA_MIPS64R5)
1901 return 5;
1902
c6278170
RS
1903 /* microMIPS implies revision 2 or above. */
1904 if (mips_opts.micromips)
1905 return 2;
1906
1907 if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
1908 return 1;
1909
1910 return 0;
1911}
1912
1913/* Return the mask of all ASEs that are revisions of those in FLAGS. */
1914
1915static unsigned int
1916mips_ase_mask (unsigned int flags)
1917{
1918 unsigned int i;
1919
1920 for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
1921 if (flags & mips_ase_groups[i])
1922 flags |= mips_ase_groups[i];
1923 return flags;
1924}
1925
1926/* Check whether the current ISA supports ASE. Issue a warning if
1927 appropriate. */
1928
1929static void
1930mips_check_isa_supports_ase (const struct mips_ase *ase)
1931{
1932 const char *base;
1933 int min_rev, size;
1934 static unsigned int warned_isa;
1935 static unsigned int warned_fp32;
1936
1937 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
1938 min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
1939 else
1940 min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
1941 if ((min_rev < 0 || mips_isa_rev () < min_rev)
1942 && (warned_isa & ase->flags) != ase->flags)
1943 {
1944 warned_isa |= ase->flags;
1945 base = mips_opts.micromips ? "microMIPS" : "MIPS";
1946 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
1947 if (min_rev < 0)
1661c76c 1948 as_warn (_("the %d-bit %s architecture does not support the"
c6278170
RS
1949 " `%s' extension"), size, base, ase->name);
1950 else
1661c76c 1951 as_warn (_("the `%s' extension requires %s%d revision %d or greater"),
c6278170
RS
1952 ase->name, base, size, min_rev);
1953 }
1954 if ((ase->flags & FP64_ASES)
0b35dfee 1955 && mips_opts.fp != 64
c6278170
RS
1956 && (warned_fp32 & ase->flags) != ase->flags)
1957 {
1958 warned_fp32 |= ase->flags;
1661c76c 1959 as_warn (_("the `%s' extension requires 64-bit FPRs"), ase->name);
c6278170
RS
1960 }
1961}
1962
1963/* Check all enabled ASEs to see whether they are supported by the
1964 chosen architecture. */
1965
1966static void
1967mips_check_isa_supports_ases (void)
1968{
1969 unsigned int i, mask;
1970
1971 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
1972 {
1973 mask = mips_ase_mask (mips_ases[i].flags);
1974 if ((mips_opts.ase & mask) == mips_ases[i].flags)
1975 mips_check_isa_supports_ase (&mips_ases[i]);
1976 }
1977}
1978
1979/* Set the state of ASE to ENABLED_P. Return the mask of ASE_* flags
1980 that were affected. */
1981
1982static unsigned int
1983mips_set_ase (const struct mips_ase *ase, bfd_boolean enabled_p)
1984{
1985 unsigned int mask;
1986
1987 mask = mips_ase_mask (ase->flags);
1988 mips_opts.ase &= ~mask;
1989 if (enabled_p)
1990 mips_opts.ase |= ase->flags;
1991 return mask;
1992}
1993
1994/* Return the ASE called NAME, or null if none. */
1995
1996static const struct mips_ase *
1997mips_lookup_ase (const char *name)
1998{
1999 unsigned int i;
2000
2001 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2002 if (strcmp (name, mips_ases[i].name) == 0)
2003 return &mips_ases[i];
2004 return NULL;
2005}
2006
df58fc94
RS
2007/* Return the length of a microMIPS instruction in bytes. If bits of
2008 the mask beyond the low 16 are 0, then it is a 16-bit instruction.
2009 Otherwise assume a 32-bit instruction; 48-bit instructions (0x1f
2010 major opcode) will require further modifications to the opcode
2011 table. */
2012
2013static inline unsigned int
2014micromips_insn_length (const struct mips_opcode *mo)
2015{
2016 return (mo->mask >> 16) == 0 ? 2 : 4;
2017}
2018
5c04167a
RS
2019/* Return the length of MIPS16 instruction OPCODE. */
2020
2021static inline unsigned int
2022mips16_opcode_length (unsigned long opcode)
2023{
2024 return (opcode >> 16) == 0 ? 2 : 4;
2025}
2026
1e915849
RS
2027/* Return the length of instruction INSN. */
2028
2029static inline unsigned int
2030insn_length (const struct mips_cl_insn *insn)
2031{
df58fc94
RS
2032 if (mips_opts.micromips)
2033 return micromips_insn_length (insn->insn_mo);
2034 else if (mips_opts.mips16)
5c04167a 2035 return mips16_opcode_length (insn->insn_opcode);
df58fc94 2036 else
1e915849 2037 return 4;
1e915849
RS
2038}
2039
2040/* Initialise INSN from opcode entry MO. Leave its position unspecified. */
2041
2042static void
2043create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
2044{
2045 size_t i;
2046
2047 insn->insn_mo = mo;
1e915849
RS
2048 insn->insn_opcode = mo->match;
2049 insn->frag = NULL;
2050 insn->where = 0;
2051 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2052 insn->fixp[i] = NULL;
2053 insn->fixed_p = (mips_opts.noreorder > 0);
2054 insn->noreorder_p = (mips_opts.noreorder > 0);
2055 insn->mips16_absolute_jump_p = 0;
15be625d 2056 insn->complete_p = 0;
e407c74b 2057 insn->cleared_p = 0;
1e915849
RS
2058}
2059
fc76e730
RS
2060/* Get a list of all the operands in INSN. */
2061
2062static const struct mips_operand_array *
2063insn_operands (const struct mips_cl_insn *insn)
2064{
2065 if (insn->insn_mo >= &mips_opcodes[0]
2066 && insn->insn_mo < &mips_opcodes[NUMOPCODES])
2067 return &mips_operands[insn->insn_mo - &mips_opcodes[0]];
2068
2069 if (insn->insn_mo >= &mips16_opcodes[0]
2070 && insn->insn_mo < &mips16_opcodes[bfd_mips16_num_opcodes])
2071 return &mips16_operands[insn->insn_mo - &mips16_opcodes[0]];
2072
2073 if (insn->insn_mo >= &micromips_opcodes[0]
2074 && insn->insn_mo < &micromips_opcodes[bfd_micromips_num_opcodes])
2075 return &micromips_operands[insn->insn_mo - &micromips_opcodes[0]];
2076
2077 abort ();
2078}
2079
2080/* Get a description of operand OPNO of INSN. */
2081
2082static const struct mips_operand *
2083insn_opno (const struct mips_cl_insn *insn, unsigned opno)
2084{
2085 const struct mips_operand_array *operands;
2086
2087 operands = insn_operands (insn);
2088 if (opno >= MAX_OPERANDS || !operands->operand[opno])
2089 abort ();
2090 return operands->operand[opno];
2091}
2092
e077a1c8
RS
2093/* Install UVAL as the value of OPERAND in INSN. */
2094
2095static inline void
2096insn_insert_operand (struct mips_cl_insn *insn,
2097 const struct mips_operand *operand, unsigned int uval)
2098{
2099 insn->insn_opcode = mips_insert_operand (operand, insn->insn_opcode, uval);
2100}
2101
fc76e730
RS
2102/* Extract the value of OPERAND from INSN. */
2103
2104static inline unsigned
2105insn_extract_operand (const struct mips_cl_insn *insn,
2106 const struct mips_operand *operand)
2107{
2108 return mips_extract_operand (operand, insn->insn_opcode);
2109}
2110
df58fc94 2111/* Record the current MIPS16/microMIPS mode in now_seg. */
742a56fe
RS
2112
2113static void
df58fc94 2114mips_record_compressed_mode (void)
742a56fe
RS
2115{
2116 segment_info_type *si;
2117
2118 si = seg_info (now_seg);
2119 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2120 si->tc_segment_info_data.mips16 = mips_opts.mips16;
df58fc94
RS
2121 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2122 si->tc_segment_info_data.micromips = mips_opts.micromips;
742a56fe
RS
2123}
2124
4d68580a
RS
2125/* Read a standard MIPS instruction from BUF. */
2126
2127static unsigned long
2128read_insn (char *buf)
2129{
2130 if (target_big_endian)
2131 return bfd_getb32 ((bfd_byte *) buf);
2132 else
2133 return bfd_getl32 ((bfd_byte *) buf);
2134}
2135
2136/* Write standard MIPS instruction INSN to BUF. Return a pointer to
2137 the next byte. */
2138
2139static char *
2140write_insn (char *buf, unsigned int insn)
2141{
2142 md_number_to_chars (buf, insn, 4);
2143 return buf + 4;
2144}
2145
2146/* Read a microMIPS or MIPS16 opcode from BUF, given that it
2147 has length LENGTH. */
2148
2149static unsigned long
2150read_compressed_insn (char *buf, unsigned int length)
2151{
2152 unsigned long insn;
2153 unsigned int i;
2154
2155 insn = 0;
2156 for (i = 0; i < length; i += 2)
2157 {
2158 insn <<= 16;
2159 if (target_big_endian)
2160 insn |= bfd_getb16 ((char *) buf);
2161 else
2162 insn |= bfd_getl16 ((char *) buf);
2163 buf += 2;
2164 }
2165 return insn;
2166}
2167
5c04167a
RS
2168/* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2169 instruction is LENGTH bytes long. Return a pointer to the next byte. */
2170
2171static char *
2172write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2173{
2174 unsigned int i;
2175
2176 for (i = 0; i < length; i += 2)
2177 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2178 return buf + length;
2179}
2180
1e915849
RS
2181/* Install INSN at the location specified by its "frag" and "where" fields. */
2182
2183static void
2184install_insn (const struct mips_cl_insn *insn)
2185{
2186 char *f = insn->frag->fr_literal + insn->where;
5c04167a
RS
2187 if (HAVE_CODE_COMPRESSION)
2188 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
1e915849 2189 else
4d68580a 2190 write_insn (f, insn->insn_opcode);
df58fc94 2191 mips_record_compressed_mode ();
1e915849
RS
2192}
2193
2194/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
2195 and install the opcode in the new location. */
2196
2197static void
2198move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2199{
2200 size_t i;
2201
2202 insn->frag = frag;
2203 insn->where = where;
2204 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2205 if (insn->fixp[i] != NULL)
2206 {
2207 insn->fixp[i]->fx_frag = frag;
2208 insn->fixp[i]->fx_where = where;
2209 }
2210 install_insn (insn);
2211}
2212
2213/* Add INSN to the end of the output. */
2214
2215static void
2216add_fixed_insn (struct mips_cl_insn *insn)
2217{
2218 char *f = frag_more (insn_length (insn));
2219 move_insn (insn, frag_now, f - frag_now->fr_literal);
2220}
2221
2222/* Start a variant frag and move INSN to the start of the variant part,
2223 marking it as fixed. The other arguments are as for frag_var. */
2224
2225static void
2226add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2227 relax_substateT subtype, symbolS *symbol, offsetT offset)
2228{
2229 frag_grow (max_chars);
2230 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2231 insn->fixed_p = 1;
2232 frag_var (rs_machine_dependent, max_chars, var,
2233 subtype, symbol, offset, NULL);
2234}
2235
2236/* Insert N copies of INSN into the history buffer, starting at
2237 position FIRST. Neither FIRST nor N need to be clipped. */
2238
2239static void
2240insert_into_history (unsigned int first, unsigned int n,
2241 const struct mips_cl_insn *insn)
2242{
2243 if (mips_relax.sequence != 2)
2244 {
2245 unsigned int i;
2246
2247 for (i = ARRAY_SIZE (history); i-- > first;)
2248 if (i >= first + n)
2249 history[i] = history[i - n];
2250 else
2251 history[i] = *insn;
2252 }
2253}
2254
e3de51ce
RS
2255/* Clear the error in insn_error. */
2256
2257static void
2258clear_insn_error (void)
2259{
2260 memset (&insn_error, 0, sizeof (insn_error));
2261}
2262
2263/* Possibly record error message MSG for the current instruction.
2264 If the error is about a particular argument, ARGNUM is the 1-based
2265 number of that argument, otherwise it is 0. FORMAT is the format
2266 of MSG. Return true if MSG was used, false if the current message
2267 was kept. */
2268
2269static bfd_boolean
2270set_insn_error_format (int argnum, enum mips_insn_error_format format,
2271 const char *msg)
2272{
2273 if (argnum == 0)
2274 {
2275 /* Give priority to errors against specific arguments, and to
2276 the first whole-instruction message. */
2277 if (insn_error.msg)
2278 return FALSE;
2279 }
2280 else
2281 {
2282 /* Keep insn_error if it is against a later argument. */
2283 if (argnum < insn_error.min_argnum)
2284 return FALSE;
2285
2286 /* If both errors are against the same argument but are different,
2287 give up on reporting a specific error for this argument.
2288 See the comment about mips_insn_error for details. */
2289 if (argnum == insn_error.min_argnum
2290 && insn_error.msg
2291 && strcmp (insn_error.msg, msg) != 0)
2292 {
2293 insn_error.msg = 0;
2294 insn_error.min_argnum += 1;
2295 return FALSE;
2296 }
2297 }
2298 insn_error.min_argnum = argnum;
2299 insn_error.format = format;
2300 insn_error.msg = msg;
2301 return TRUE;
2302}
2303
2304/* Record an instruction error with no % format fields. ARGNUM and MSG are
2305 as for set_insn_error_format. */
2306
2307static void
2308set_insn_error (int argnum, const char *msg)
2309{
2310 set_insn_error_format (argnum, ERR_FMT_PLAIN, msg);
2311}
2312
2313/* Record an instruction error with one %d field I. ARGNUM and MSG are
2314 as for set_insn_error_format. */
2315
2316static void
2317set_insn_error_i (int argnum, const char *msg, int i)
2318{
2319 if (set_insn_error_format (argnum, ERR_FMT_I, msg))
2320 insn_error.u.i = i;
2321}
2322
2323/* Record an instruction error with two %s fields S1 and S2. ARGNUM and MSG
2324 are as for set_insn_error_format. */
2325
2326static void
2327set_insn_error_ss (int argnum, const char *msg, const char *s1, const char *s2)
2328{
2329 if (set_insn_error_format (argnum, ERR_FMT_SS, msg))
2330 {
2331 insn_error.u.ss[0] = s1;
2332 insn_error.u.ss[1] = s2;
2333 }
2334}
2335
2336/* Report the error in insn_error, which is against assembly code STR. */
2337
2338static void
2339report_insn_error (const char *str)
2340{
2341 const char *msg;
2342
2343 msg = ACONCAT ((insn_error.msg, " `%s'", NULL));
2344 switch (insn_error.format)
2345 {
2346 case ERR_FMT_PLAIN:
2347 as_bad (msg, str);
2348 break;
2349
2350 case ERR_FMT_I:
2351 as_bad (msg, insn_error.u.i, str);
2352 break;
2353
2354 case ERR_FMT_SS:
2355 as_bad (msg, insn_error.u.ss[0], insn_error.u.ss[1], str);
2356 break;
2357 }
2358}
2359
71400594
RS
2360/* Initialize vr4120_conflicts. There is a bit of duplication here:
2361 the idea is to make it obvious at a glance that each errata is
2362 included. */
2363
2364static void
2365init_vr4120_conflicts (void)
2366{
2367#define CONFLICT(FIRST, SECOND) \
2368 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2369
2370 /* Errata 21 - [D]DIV[U] after [D]MACC */
2371 CONFLICT (MACC, DIV);
2372 CONFLICT (DMACC, DIV);
2373
2374 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
2375 CONFLICT (DMULT, DMULT);
2376 CONFLICT (DMULT, DMACC);
2377 CONFLICT (DMACC, DMULT);
2378 CONFLICT (DMACC, DMACC);
2379
2380 /* Errata 24 - MT{LO,HI} after [D]MACC */
2381 CONFLICT (MACC, MTHILO);
2382 CONFLICT (DMACC, MTHILO);
2383
2384 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2385 instruction is executed immediately after a MACC or DMACC
2386 instruction, the result of [either instruction] is incorrect." */
2387 CONFLICT (MACC, MULT);
2388 CONFLICT (MACC, DMULT);
2389 CONFLICT (DMACC, MULT);
2390 CONFLICT (DMACC, DMULT);
2391
2392 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2393 executed immediately after a DMULT, DMULTU, DIV, DIVU,
2394 DDIV or DDIVU instruction, the result of the MACC or
2395 DMACC instruction is incorrect.". */
2396 CONFLICT (DMULT, MACC);
2397 CONFLICT (DMULT, DMACC);
2398 CONFLICT (DIV, MACC);
2399 CONFLICT (DIV, DMACC);
2400
2401#undef CONFLICT
2402}
2403
707bfff6
TS
2404struct regname {
2405 const char *name;
2406 unsigned int num;
2407};
2408
14daeee3 2409#define RNUM_MASK 0x00000ff
56d438b1 2410#define RTYPE_MASK 0x0ffff00
14daeee3
RS
2411#define RTYPE_NUM 0x0000100
2412#define RTYPE_FPU 0x0000200
2413#define RTYPE_FCC 0x0000400
2414#define RTYPE_VEC 0x0000800
2415#define RTYPE_GP 0x0001000
2416#define RTYPE_CP0 0x0002000
2417#define RTYPE_PC 0x0004000
2418#define RTYPE_ACC 0x0008000
2419#define RTYPE_CCC 0x0010000
2420#define RTYPE_VI 0x0020000
2421#define RTYPE_VF 0x0040000
2422#define RTYPE_R5900_I 0x0080000
2423#define RTYPE_R5900_Q 0x0100000
2424#define RTYPE_R5900_R 0x0200000
2425#define RTYPE_R5900_ACC 0x0400000
56d438b1 2426#define RTYPE_MSA 0x0800000
14daeee3 2427#define RWARN 0x8000000
707bfff6
TS
2428
2429#define GENERIC_REGISTER_NUMBERS \
2430 {"$0", RTYPE_NUM | 0}, \
2431 {"$1", RTYPE_NUM | 1}, \
2432 {"$2", RTYPE_NUM | 2}, \
2433 {"$3", RTYPE_NUM | 3}, \
2434 {"$4", RTYPE_NUM | 4}, \
2435 {"$5", RTYPE_NUM | 5}, \
2436 {"$6", RTYPE_NUM | 6}, \
2437 {"$7", RTYPE_NUM | 7}, \
2438 {"$8", RTYPE_NUM | 8}, \
2439 {"$9", RTYPE_NUM | 9}, \
2440 {"$10", RTYPE_NUM | 10}, \
2441 {"$11", RTYPE_NUM | 11}, \
2442 {"$12", RTYPE_NUM | 12}, \
2443 {"$13", RTYPE_NUM | 13}, \
2444 {"$14", RTYPE_NUM | 14}, \
2445 {"$15", RTYPE_NUM | 15}, \
2446 {"$16", RTYPE_NUM | 16}, \
2447 {"$17", RTYPE_NUM | 17}, \
2448 {"$18", RTYPE_NUM | 18}, \
2449 {"$19", RTYPE_NUM | 19}, \
2450 {"$20", RTYPE_NUM | 20}, \
2451 {"$21", RTYPE_NUM | 21}, \
2452 {"$22", RTYPE_NUM | 22}, \
2453 {"$23", RTYPE_NUM | 23}, \
2454 {"$24", RTYPE_NUM | 24}, \
2455 {"$25", RTYPE_NUM | 25}, \
2456 {"$26", RTYPE_NUM | 26}, \
2457 {"$27", RTYPE_NUM | 27}, \
2458 {"$28", RTYPE_NUM | 28}, \
2459 {"$29", RTYPE_NUM | 29}, \
2460 {"$30", RTYPE_NUM | 30}, \
2461 {"$31", RTYPE_NUM | 31}
2462
2463#define FPU_REGISTER_NAMES \
2464 {"$f0", RTYPE_FPU | 0}, \
2465 {"$f1", RTYPE_FPU | 1}, \
2466 {"$f2", RTYPE_FPU | 2}, \
2467 {"$f3", RTYPE_FPU | 3}, \
2468 {"$f4", RTYPE_FPU | 4}, \
2469 {"$f5", RTYPE_FPU | 5}, \
2470 {"$f6", RTYPE_FPU | 6}, \
2471 {"$f7", RTYPE_FPU | 7}, \
2472 {"$f8", RTYPE_FPU | 8}, \
2473 {"$f9", RTYPE_FPU | 9}, \
2474 {"$f10", RTYPE_FPU | 10}, \
2475 {"$f11", RTYPE_FPU | 11}, \
2476 {"$f12", RTYPE_FPU | 12}, \
2477 {"$f13", RTYPE_FPU | 13}, \
2478 {"$f14", RTYPE_FPU | 14}, \
2479 {"$f15", RTYPE_FPU | 15}, \
2480 {"$f16", RTYPE_FPU | 16}, \
2481 {"$f17", RTYPE_FPU | 17}, \
2482 {"$f18", RTYPE_FPU | 18}, \
2483 {"$f19", RTYPE_FPU | 19}, \
2484 {"$f20", RTYPE_FPU | 20}, \
2485 {"$f21", RTYPE_FPU | 21}, \
2486 {"$f22", RTYPE_FPU | 22}, \
2487 {"$f23", RTYPE_FPU | 23}, \
2488 {"$f24", RTYPE_FPU | 24}, \
2489 {"$f25", RTYPE_FPU | 25}, \
2490 {"$f26", RTYPE_FPU | 26}, \
2491 {"$f27", RTYPE_FPU | 27}, \
2492 {"$f28", RTYPE_FPU | 28}, \
2493 {"$f29", RTYPE_FPU | 29}, \
2494 {"$f30", RTYPE_FPU | 30}, \
2495 {"$f31", RTYPE_FPU | 31}
2496
2497#define FPU_CONDITION_CODE_NAMES \
2498 {"$fcc0", RTYPE_FCC | 0}, \
2499 {"$fcc1", RTYPE_FCC | 1}, \
2500 {"$fcc2", RTYPE_FCC | 2}, \
2501 {"$fcc3", RTYPE_FCC | 3}, \
2502 {"$fcc4", RTYPE_FCC | 4}, \
2503 {"$fcc5", RTYPE_FCC | 5}, \
2504 {"$fcc6", RTYPE_FCC | 6}, \
2505 {"$fcc7", RTYPE_FCC | 7}
2506
2507#define COPROC_CONDITION_CODE_NAMES \
2508 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
2509 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
2510 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
2511 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
2512 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
2513 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
2514 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
2515 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
2516
2517#define N32N64_SYMBOLIC_REGISTER_NAMES \
2518 {"$a4", RTYPE_GP | 8}, \
2519 {"$a5", RTYPE_GP | 9}, \
2520 {"$a6", RTYPE_GP | 10}, \
2521 {"$a7", RTYPE_GP | 11}, \
2522 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
2523 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
2524 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
2525 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
2526 {"$t0", RTYPE_GP | 12}, \
2527 {"$t1", RTYPE_GP | 13}, \
2528 {"$t2", RTYPE_GP | 14}, \
2529 {"$t3", RTYPE_GP | 15}
2530
2531#define O32_SYMBOLIC_REGISTER_NAMES \
2532 {"$t0", RTYPE_GP | 8}, \
2533 {"$t1", RTYPE_GP | 9}, \
2534 {"$t2", RTYPE_GP | 10}, \
2535 {"$t3", RTYPE_GP | 11}, \
2536 {"$t4", RTYPE_GP | 12}, \
2537 {"$t5", RTYPE_GP | 13}, \
2538 {"$t6", RTYPE_GP | 14}, \
2539 {"$t7", RTYPE_GP | 15}, \
2540 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2541 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2542 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
2543 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
2544
2545/* Remaining symbolic register names */
2546#define SYMBOLIC_REGISTER_NAMES \
2547 {"$zero", RTYPE_GP | 0}, \
2548 {"$at", RTYPE_GP | 1}, \
2549 {"$AT", RTYPE_GP | 1}, \
2550 {"$v0", RTYPE_GP | 2}, \
2551 {"$v1", RTYPE_GP | 3}, \
2552 {"$a0", RTYPE_GP | 4}, \
2553 {"$a1", RTYPE_GP | 5}, \
2554 {"$a2", RTYPE_GP | 6}, \
2555 {"$a3", RTYPE_GP | 7}, \
2556 {"$s0", RTYPE_GP | 16}, \
2557 {"$s1", RTYPE_GP | 17}, \
2558 {"$s2", RTYPE_GP | 18}, \
2559 {"$s3", RTYPE_GP | 19}, \
2560 {"$s4", RTYPE_GP | 20}, \
2561 {"$s5", RTYPE_GP | 21}, \
2562 {"$s6", RTYPE_GP | 22}, \
2563 {"$s7", RTYPE_GP | 23}, \
2564 {"$t8", RTYPE_GP | 24}, \
2565 {"$t9", RTYPE_GP | 25}, \
2566 {"$k0", RTYPE_GP | 26}, \
2567 {"$kt0", RTYPE_GP | 26}, \
2568 {"$k1", RTYPE_GP | 27}, \
2569 {"$kt1", RTYPE_GP | 27}, \
2570 {"$gp", RTYPE_GP | 28}, \
2571 {"$sp", RTYPE_GP | 29}, \
2572 {"$s8", RTYPE_GP | 30}, \
2573 {"$fp", RTYPE_GP | 30}, \
2574 {"$ra", RTYPE_GP | 31}
2575
2576#define MIPS16_SPECIAL_REGISTER_NAMES \
2577 {"$pc", RTYPE_PC | 0}
2578
2579#define MDMX_VECTOR_REGISTER_NAMES \
2580 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
2581 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
2582 {"$v2", RTYPE_VEC | 2}, \
2583 {"$v3", RTYPE_VEC | 3}, \
2584 {"$v4", RTYPE_VEC | 4}, \
2585 {"$v5", RTYPE_VEC | 5}, \
2586 {"$v6", RTYPE_VEC | 6}, \
2587 {"$v7", RTYPE_VEC | 7}, \
2588 {"$v8", RTYPE_VEC | 8}, \
2589 {"$v9", RTYPE_VEC | 9}, \
2590 {"$v10", RTYPE_VEC | 10}, \
2591 {"$v11", RTYPE_VEC | 11}, \
2592 {"$v12", RTYPE_VEC | 12}, \
2593 {"$v13", RTYPE_VEC | 13}, \
2594 {"$v14", RTYPE_VEC | 14}, \
2595 {"$v15", RTYPE_VEC | 15}, \
2596 {"$v16", RTYPE_VEC | 16}, \
2597 {"$v17", RTYPE_VEC | 17}, \
2598 {"$v18", RTYPE_VEC | 18}, \
2599 {"$v19", RTYPE_VEC | 19}, \
2600 {"$v20", RTYPE_VEC | 20}, \
2601 {"$v21", RTYPE_VEC | 21}, \
2602 {"$v22", RTYPE_VEC | 22}, \
2603 {"$v23", RTYPE_VEC | 23}, \
2604 {"$v24", RTYPE_VEC | 24}, \
2605 {"$v25", RTYPE_VEC | 25}, \
2606 {"$v26", RTYPE_VEC | 26}, \
2607 {"$v27", RTYPE_VEC | 27}, \
2608 {"$v28", RTYPE_VEC | 28}, \
2609 {"$v29", RTYPE_VEC | 29}, \
2610 {"$v30", RTYPE_VEC | 30}, \
2611 {"$v31", RTYPE_VEC | 31}
2612
14daeee3
RS
2613#define R5900_I_NAMES \
2614 {"$I", RTYPE_R5900_I | 0}
2615
2616#define R5900_Q_NAMES \
2617 {"$Q", RTYPE_R5900_Q | 0}
2618
2619#define R5900_R_NAMES \
2620 {"$R", RTYPE_R5900_R | 0}
2621
2622#define R5900_ACC_NAMES \
2623 {"$ACC", RTYPE_R5900_ACC | 0 }
2624
707bfff6
TS
2625#define MIPS_DSP_ACCUMULATOR_NAMES \
2626 {"$ac0", RTYPE_ACC | 0}, \
2627 {"$ac1", RTYPE_ACC | 1}, \
2628 {"$ac2", RTYPE_ACC | 2}, \
2629 {"$ac3", RTYPE_ACC | 3}
2630
2631static const struct regname reg_names[] = {
2632 GENERIC_REGISTER_NUMBERS,
2633 FPU_REGISTER_NAMES,
2634 FPU_CONDITION_CODE_NAMES,
2635 COPROC_CONDITION_CODE_NAMES,
2636
2637 /* The $txx registers depends on the abi,
2638 these will be added later into the symbol table from
2639 one of the tables below once mips_abi is set after
2640 parsing of arguments from the command line. */
2641 SYMBOLIC_REGISTER_NAMES,
2642
2643 MIPS16_SPECIAL_REGISTER_NAMES,
2644 MDMX_VECTOR_REGISTER_NAMES,
14daeee3
RS
2645 R5900_I_NAMES,
2646 R5900_Q_NAMES,
2647 R5900_R_NAMES,
2648 R5900_ACC_NAMES,
707bfff6
TS
2649 MIPS_DSP_ACCUMULATOR_NAMES,
2650 {0, 0}
2651};
2652
2653static const struct regname reg_names_o32[] = {
2654 O32_SYMBOLIC_REGISTER_NAMES,
2655 {0, 0}
2656};
2657
2658static const struct regname reg_names_n32n64[] = {
2659 N32N64_SYMBOLIC_REGISTER_NAMES,
2660 {0, 0}
2661};
2662
a92713e6
RS
2663/* Register symbols $v0 and $v1 map to GPRs 2 and 3, but they can also be
2664 interpreted as vector registers 0 and 1. If SYMVAL is the value of one
2665 of these register symbols, return the associated vector register,
2666 otherwise return SYMVAL itself. */
df58fc94 2667
a92713e6
RS
2668static unsigned int
2669mips_prefer_vec_regno (unsigned int symval)
707bfff6 2670{
a92713e6
RS
2671 if ((symval & -2) == (RTYPE_GP | 2))
2672 return RTYPE_VEC | (symval & 1);
2673 return symval;
2674}
2675
14daeee3
RS
2676/* Return true if string [S, E) is a valid register name, storing its
2677 symbol value in *SYMVAL_PTR if so. */
a92713e6
RS
2678
2679static bfd_boolean
14daeee3 2680mips_parse_register_1 (char *s, char *e, unsigned int *symval_ptr)
a92713e6 2681{
707bfff6 2682 char save_c;
14daeee3 2683 symbolS *symbol;
707bfff6
TS
2684
2685 /* Terminate name. */
2686 save_c = *e;
2687 *e = '\0';
2688
a92713e6
RS
2689 /* Look up the name. */
2690 symbol = symbol_find (s);
2691 *e = save_c;
2692
2693 if (!symbol || S_GET_SEGMENT (symbol) != reg_section)
2694 return FALSE;
2695
14daeee3
RS
2696 *symval_ptr = S_GET_VALUE (symbol);
2697 return TRUE;
2698}
2699
2700/* Return true if the string at *SPTR is a valid register name. Allow it
2701 to have a VU0-style channel suffix of the form x?y?z?w? if CHANNELS_PTR
2702 is nonnull.
2703
2704 When returning true, move *SPTR past the register, store the
2705 register's symbol value in *SYMVAL_PTR and the channel mask in
2706 *CHANNELS_PTR (if nonnull). The symbol value includes the register
2707 number (RNUM_MASK) and register type (RTYPE_MASK). The channel mask
2708 is a 4-bit value of the form XYZW and is 0 if no suffix was given. */
2709
2710static bfd_boolean
2711mips_parse_register (char **sptr, unsigned int *symval_ptr,
2712 unsigned int *channels_ptr)
2713{
2714 char *s, *e, *m;
2715 const char *q;
2716 unsigned int channels, symval, bit;
2717
2718 /* Find end of name. */
2719 s = e = *sptr;
2720 if (is_name_beginner (*e))
2721 ++e;
2722 while (is_part_of_name (*e))
2723 ++e;
2724
2725 channels = 0;
2726 if (!mips_parse_register_1 (s, e, &symval))
2727 {
2728 if (!channels_ptr)
2729 return FALSE;
2730
2731 /* Eat characters from the end of the string that are valid
2732 channel suffixes. The preceding register must be $ACC or
2733 end with a digit, so there is no ambiguity. */
2734 bit = 1;
2735 m = e;
2736 for (q = "wzyx"; *q; q++, bit <<= 1)
2737 if (m > s && m[-1] == *q)
2738 {
2739 --m;
2740 channels |= bit;
2741 }
2742
2743 if (channels == 0
2744 || !mips_parse_register_1 (s, m, &symval)
2745 || (symval & (RTYPE_VI | RTYPE_VF | RTYPE_R5900_ACC)) == 0)
2746 return FALSE;
2747 }
2748
a92713e6 2749 *sptr = e;
14daeee3
RS
2750 *symval_ptr = symval;
2751 if (channels_ptr)
2752 *channels_ptr = channels;
a92713e6
RS
2753 return TRUE;
2754}
2755
2756/* Check if SPTR points at a valid register specifier according to TYPES.
2757 If so, then return 1, advance S to consume the specifier and store
2758 the register's number in REGNOP, otherwise return 0. */
2759
2760static int
2761reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2762{
2763 unsigned int regno;
2764
14daeee3 2765 if (mips_parse_register (s, &regno, NULL))
707bfff6 2766 {
a92713e6
RS
2767 if (types & RTYPE_VEC)
2768 regno = mips_prefer_vec_regno (regno);
2769 if (regno & types)
2770 regno &= RNUM_MASK;
2771 else
2772 regno = ~0;
707bfff6 2773 }
a92713e6 2774 else
707bfff6 2775 {
a92713e6 2776 if (types & RWARN)
1661c76c 2777 as_warn (_("unrecognized register name `%s'"), *s);
a92713e6 2778 regno = ~0;
707bfff6 2779 }
707bfff6 2780 if (regnop)
a92713e6
RS
2781 *regnop = regno;
2782 return regno <= RNUM_MASK;
707bfff6
TS
2783}
2784
14daeee3
RS
2785/* Parse a VU0 "x?y?z?w?" channel mask at S and store the associated
2786 mask in *CHANNELS. Return a pointer to the first unconsumed character. */
2787
2788static char *
2789mips_parse_vu0_channels (char *s, unsigned int *channels)
2790{
2791 unsigned int i;
2792
2793 *channels = 0;
2794 for (i = 0; i < 4; i++)
2795 if (*s == "xyzw"[i])
2796 {
2797 *channels |= 1 << (3 - i);
2798 ++s;
2799 }
2800 return s;
2801}
2802
a92713e6
RS
2803/* Token types for parsed operand lists. */
2804enum mips_operand_token_type {
2805 /* A plain register, e.g. $f2. */
2806 OT_REG,
df58fc94 2807
14daeee3
RS
2808 /* A 4-bit XYZW channel mask. */
2809 OT_CHANNELS,
2810
56d438b1
CF
2811 /* A constant vector index, e.g. [1]. */
2812 OT_INTEGER_INDEX,
2813
2814 /* A register vector index, e.g. [$2]. */
2815 OT_REG_INDEX,
df58fc94 2816
a92713e6
RS
2817 /* A continuous range of registers, e.g. $s0-$s4. */
2818 OT_REG_RANGE,
2819
2820 /* A (possibly relocated) expression. */
2821 OT_INTEGER,
2822
2823 /* A floating-point value. */
2824 OT_FLOAT,
2825
2826 /* A single character. This can be '(', ')' or ',', but '(' only appears
2827 before OT_REGs. */
2828 OT_CHAR,
2829
14daeee3
RS
2830 /* A doubled character, either "--" or "++". */
2831 OT_DOUBLE_CHAR,
2832
a92713e6
RS
2833 /* The end of the operand list. */
2834 OT_END
2835};
2836
2837/* A parsed operand token. */
2838struct mips_operand_token
2839{
2840 /* The type of token. */
2841 enum mips_operand_token_type type;
2842 union
2843 {
56d438b1 2844 /* The register symbol value for an OT_REG or OT_REG_INDEX. */
a92713e6
RS
2845 unsigned int regno;
2846
14daeee3
RS
2847 /* The 4-bit channel mask for an OT_CHANNEL_SUFFIX. */
2848 unsigned int channels;
2849
56d438b1
CF
2850 /* The integer value of an OT_INTEGER_INDEX. */
2851 addressT index;
a92713e6
RS
2852
2853 /* The two register symbol values involved in an OT_REG_RANGE. */
2854 struct {
2855 unsigned int regno1;
2856 unsigned int regno2;
2857 } reg_range;
2858
2859 /* The value of an OT_INTEGER. The value is represented as an
2860 expression and the relocation operators that were applied to
2861 that expression. The reloc entries are BFD_RELOC_UNUSED if no
2862 relocation operators were used. */
2863 struct {
2864 expressionS value;
2865 bfd_reloc_code_real_type relocs[3];
2866 } integer;
2867
2868 /* The binary data for an OT_FLOAT constant, and the number of bytes
2869 in the constant. */
2870 struct {
2871 unsigned char data[8];
2872 int length;
2873 } flt;
2874
14daeee3 2875 /* The character represented by an OT_CHAR or OT_DOUBLE_CHAR. */
a92713e6
RS
2876 char ch;
2877 } u;
2878};
2879
2880/* An obstack used to construct lists of mips_operand_tokens. */
2881static struct obstack mips_operand_tokens;
2882
2883/* Give TOKEN type TYPE and add it to mips_operand_tokens. */
2884
2885static void
2886mips_add_token (struct mips_operand_token *token,
2887 enum mips_operand_token_type type)
2888{
2889 token->type = type;
2890 obstack_grow (&mips_operand_tokens, token, sizeof (*token));
2891}
2892
2893/* Check whether S is '(' followed by a register name. Add OT_CHAR
2894 and OT_REG tokens for them if so, and return a pointer to the first
2895 unconsumed character. Return null otherwise. */
2896
2897static char *
2898mips_parse_base_start (char *s)
2899{
2900 struct mips_operand_token token;
14daeee3
RS
2901 unsigned int regno, channels;
2902 bfd_boolean decrement_p;
df58fc94 2903
a92713e6
RS
2904 if (*s != '(')
2905 return 0;
2906
2907 ++s;
2908 SKIP_SPACE_TABS (s);
14daeee3
RS
2909
2910 /* Only match "--" as part of a base expression. In other contexts "--X"
2911 is a double negative. */
2912 decrement_p = (s[0] == '-' && s[1] == '-');
2913 if (decrement_p)
2914 {
2915 s += 2;
2916 SKIP_SPACE_TABS (s);
2917 }
2918
2919 /* Allow a channel specifier because that leads to better error messages
2920 than treating something like "$vf0x++" as an expression. */
2921 if (!mips_parse_register (&s, &regno, &channels))
a92713e6
RS
2922 return 0;
2923
2924 token.u.ch = '(';
2925 mips_add_token (&token, OT_CHAR);
2926
14daeee3
RS
2927 if (decrement_p)
2928 {
2929 token.u.ch = '-';
2930 mips_add_token (&token, OT_DOUBLE_CHAR);
2931 }
2932
a92713e6
RS
2933 token.u.regno = regno;
2934 mips_add_token (&token, OT_REG);
2935
14daeee3
RS
2936 if (channels)
2937 {
2938 token.u.channels = channels;
2939 mips_add_token (&token, OT_CHANNELS);
2940 }
2941
2942 /* For consistency, only match "++" as part of base expressions too. */
2943 SKIP_SPACE_TABS (s);
2944 if (s[0] == '+' && s[1] == '+')
2945 {
2946 s += 2;
2947 token.u.ch = '+';
2948 mips_add_token (&token, OT_DOUBLE_CHAR);
2949 }
2950
a92713e6
RS
2951 return s;
2952}
2953
2954/* Parse one or more tokens from S. Return a pointer to the first
2955 unconsumed character on success. Return null if an error was found
2956 and store the error text in insn_error. FLOAT_FORMAT is as for
2957 mips_parse_arguments. */
2958
2959static char *
2960mips_parse_argument_token (char *s, char float_format)
2961{
2962 char *end, *save_in, *err;
14daeee3 2963 unsigned int regno1, regno2, channels;
a92713e6
RS
2964 struct mips_operand_token token;
2965
2966 /* First look for "($reg", since we want to treat that as an
2967 OT_CHAR and OT_REG rather than an expression. */
2968 end = mips_parse_base_start (s);
2969 if (end)
2970 return end;
2971
2972 /* Handle other characters that end up as OT_CHARs. */
2973 if (*s == ')' || *s == ',')
2974 {
2975 token.u.ch = *s;
2976 mips_add_token (&token, OT_CHAR);
2977 ++s;
2978 return s;
2979 }
2980
2981 /* Handle tokens that start with a register. */
14daeee3 2982 if (mips_parse_register (&s, &regno1, &channels))
df58fc94 2983 {
14daeee3
RS
2984 if (channels)
2985 {
2986 /* A register and a VU0 channel suffix. */
2987 token.u.regno = regno1;
2988 mips_add_token (&token, OT_REG);
2989
2990 token.u.channels = channels;
2991 mips_add_token (&token, OT_CHANNELS);
2992 return s;
2993 }
2994
a92713e6
RS
2995 SKIP_SPACE_TABS (s);
2996 if (*s == '-')
df58fc94 2997 {
a92713e6
RS
2998 /* A register range. */
2999 ++s;
3000 SKIP_SPACE_TABS (s);
14daeee3 3001 if (!mips_parse_register (&s, &regno2, NULL))
a92713e6 3002 {
1661c76c 3003 set_insn_error (0, _("invalid register range"));
a92713e6
RS
3004 return 0;
3005 }
df58fc94 3006
a92713e6
RS
3007 token.u.reg_range.regno1 = regno1;
3008 token.u.reg_range.regno2 = regno2;
3009 mips_add_token (&token, OT_REG_RANGE);
3010 return s;
3011 }
a92713e6 3012
56d438b1
CF
3013 /* Add the register itself. */
3014 token.u.regno = regno1;
3015 mips_add_token (&token, OT_REG);
3016
3017 /* Check for a vector index. */
3018 if (*s == '[')
3019 {
a92713e6
RS
3020 ++s;
3021 SKIP_SPACE_TABS (s);
56d438b1
CF
3022 if (mips_parse_register (&s, &token.u.regno, NULL))
3023 mips_add_token (&token, OT_REG_INDEX);
3024 else
a92713e6 3025 {
56d438b1
CF
3026 expressionS element;
3027
3028 my_getExpression (&element, s);
3029 if (element.X_op != O_constant)
3030 {
3031 set_insn_error (0, _("vector element must be constant"));
3032 return 0;
3033 }
3034 s = expr_end;
3035 token.u.index = element.X_add_number;
3036 mips_add_token (&token, OT_INTEGER_INDEX);
a92713e6 3037 }
a92713e6
RS
3038 SKIP_SPACE_TABS (s);
3039 if (*s != ']')
3040 {
1661c76c 3041 set_insn_error (0, _("missing `]'"));
a92713e6
RS
3042 return 0;
3043 }
3044 ++s;
df58fc94 3045 }
a92713e6 3046 return s;
df58fc94
RS
3047 }
3048
a92713e6
RS
3049 if (float_format)
3050 {
3051 /* First try to treat expressions as floats. */
3052 save_in = input_line_pointer;
3053 input_line_pointer = s;
3054 err = md_atof (float_format, (char *) token.u.flt.data,
3055 &token.u.flt.length);
3056 end = input_line_pointer;
3057 input_line_pointer = save_in;
3058 if (err && *err)
3059 {
e3de51ce 3060 set_insn_error (0, err);
a92713e6
RS
3061 return 0;
3062 }
3063 if (s != end)
3064 {
3065 mips_add_token (&token, OT_FLOAT);
3066 return end;
3067 }
3068 }
3069
3070 /* Treat everything else as an integer expression. */
3071 token.u.integer.relocs[0] = BFD_RELOC_UNUSED;
3072 token.u.integer.relocs[1] = BFD_RELOC_UNUSED;
3073 token.u.integer.relocs[2] = BFD_RELOC_UNUSED;
3074 my_getSmallExpression (&token.u.integer.value, token.u.integer.relocs, s);
3075 s = expr_end;
3076 mips_add_token (&token, OT_INTEGER);
3077 return s;
3078}
3079
3080/* S points to the operand list for an instruction. FLOAT_FORMAT is 'f'
3081 if expressions should be treated as 32-bit floating-point constants,
3082 'd' if they should be treated as 64-bit floating-point constants,
3083 or 0 if they should be treated as integer expressions (the usual case).
3084
3085 Return a list of tokens on success, otherwise return 0. The caller
3086 must obstack_free the list after use. */
3087
3088static struct mips_operand_token *
3089mips_parse_arguments (char *s, char float_format)
3090{
3091 struct mips_operand_token token;
3092
3093 SKIP_SPACE_TABS (s);
3094 while (*s)
3095 {
3096 s = mips_parse_argument_token (s, float_format);
3097 if (!s)
3098 {
3099 obstack_free (&mips_operand_tokens,
3100 obstack_finish (&mips_operand_tokens));
3101 return 0;
3102 }
3103 SKIP_SPACE_TABS (s);
3104 }
3105 mips_add_token (&token, OT_END);
3106 return (struct mips_operand_token *) obstack_finish (&mips_operand_tokens);
df58fc94
RS
3107}
3108
d301a56b
RS
3109/* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
3110 and architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
037b32b9
AN
3111
3112static bfd_boolean
f79e2745 3113is_opcode_valid (const struct mips_opcode *mo)
037b32b9
AN
3114{
3115 int isa = mips_opts.isa;
846ef2d0 3116 int ase = mips_opts.ase;
037b32b9 3117 int fp_s, fp_d;
c6278170 3118 unsigned int i;
037b32b9 3119
c6278170
RS
3120 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
3121 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3122 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3123 ase |= mips_ases[i].flags64;
037b32b9 3124
d301a56b 3125 if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
037b32b9
AN
3126 return FALSE;
3127
3128 /* Check whether the instruction or macro requires single-precision or
3129 double-precision floating-point support. Note that this information is
3130 stored differently in the opcode table for insns and macros. */
3131 if (mo->pinfo == INSN_MACRO)
3132 {
3133 fp_s = mo->pinfo2 & INSN2_M_FP_S;
3134 fp_d = mo->pinfo2 & INSN2_M_FP_D;
3135 }
3136 else
3137 {
3138 fp_s = mo->pinfo & FP_S;
3139 fp_d = mo->pinfo & FP_D;
3140 }
3141
3142 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
3143 return FALSE;
3144
3145 if (fp_s && mips_opts.soft_float)
3146 return FALSE;
3147
3148 return TRUE;
3149}
3150
3151/* Return TRUE if the MIPS16 opcode MO is valid on the currently
3152 selected ISA and architecture. */
3153
3154static bfd_boolean
3155is_opcode_valid_16 (const struct mips_opcode *mo)
3156{
d301a56b 3157 return opcode_is_member (mo, mips_opts.isa, 0, mips_opts.arch);
037b32b9
AN
3158}
3159
df58fc94
RS
3160/* Return TRUE if the size of the microMIPS opcode MO matches one
3161 explicitly requested. Always TRUE in the standard MIPS mode. */
3162
3163static bfd_boolean
3164is_size_valid (const struct mips_opcode *mo)
3165{
3166 if (!mips_opts.micromips)
3167 return TRUE;
3168
833794fc
MR
3169 if (mips_opts.insn32)
3170 {
3171 if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
3172 return FALSE;
3173 if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
3174 return FALSE;
3175 }
df58fc94
RS
3176 if (!forced_insn_length)
3177 return TRUE;
3178 if (mo->pinfo == INSN_MACRO)
3179 return FALSE;
3180 return forced_insn_length == micromips_insn_length (mo);
3181}
3182
3183/* Return TRUE if the microMIPS opcode MO is valid for the delay slot
e64af278
MR
3184 of the preceding instruction. Always TRUE in the standard MIPS mode.
3185
3186 We don't accept macros in 16-bit delay slots to avoid a case where
3187 a macro expansion fails because it relies on a preceding 32-bit real
3188 instruction to have matched and does not handle the operands correctly.
3189 The only macros that may expand to 16-bit instructions are JAL that
3190 cannot be placed in a delay slot anyway, and corner cases of BALIGN
3191 and BGT (that likewise cannot be placed in a delay slot) that decay to
3192 a NOP. In all these cases the macros precede any corresponding real
3193 instruction definitions in the opcode table, so they will match in the
3194 second pass where the size of the delay slot is ignored and therefore
3195 produce correct code. */
df58fc94
RS
3196
3197static bfd_boolean
3198is_delay_slot_valid (const struct mips_opcode *mo)
3199{
3200 if (!mips_opts.micromips)
3201 return TRUE;
3202
3203 if (mo->pinfo == INSN_MACRO)
c06dec14 3204 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
df58fc94
RS
3205 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
3206 && micromips_insn_length (mo) != 4)
3207 return FALSE;
3208 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
3209 && micromips_insn_length (mo) != 2)
3210 return FALSE;
3211
3212 return TRUE;
3213}
3214
fc76e730
RS
3215/* For consistency checking, verify that all bits of OPCODE are specified
3216 either by the match/mask part of the instruction definition, or by the
3217 operand list. Also build up a list of operands in OPERANDS.
3218
3219 INSN_BITS says which bits of the instruction are significant.
3220 If OPCODE is a standard or microMIPS instruction, DECODE_OPERAND
3221 provides the mips_operand description of each operand. DECODE_OPERAND
3222 is null for MIPS16 instructions. */
ab902481
RS
3223
3224static int
3225validate_mips_insn (const struct mips_opcode *opcode,
3226 unsigned long insn_bits,
fc76e730
RS
3227 const struct mips_operand *(*decode_operand) (const char *),
3228 struct mips_operand_array *operands)
ab902481
RS
3229{
3230 const char *s;
fc76e730 3231 unsigned long used_bits, doubled, undefined, opno, mask;
ab902481
RS
3232 const struct mips_operand *operand;
3233
fc76e730
RS
3234 mask = (opcode->pinfo == INSN_MACRO ? 0 : opcode->mask);
3235 if ((mask & opcode->match) != opcode->match)
ab902481
RS
3236 {
3237 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
3238 opcode->name, opcode->args);
3239 return 0;
3240 }
3241 used_bits = 0;
fc76e730 3242 opno = 0;
14daeee3
RS
3243 if (opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX)
3244 used_bits = mips_insert_operand (&mips_vu0_channel_mask, used_bits, -1);
ab902481
RS
3245 for (s = opcode->args; *s; ++s)
3246 switch (*s)
3247 {
3248 case ',':
3249 case '(':
3250 case ')':
3251 break;
3252
14daeee3
RS
3253 case '#':
3254 s++;
3255 break;
3256
ab902481 3257 default:
fc76e730
RS
3258 if (!decode_operand)
3259 operand = decode_mips16_operand (*s, FALSE);
3260 else
3261 operand = decode_operand (s);
3262 if (!operand && opcode->pinfo != INSN_MACRO)
ab902481
RS
3263 {
3264 as_bad (_("internal: unknown operand type: %s %s"),
3265 opcode->name, opcode->args);
3266 return 0;
3267 }
fc76e730
RS
3268 gas_assert (opno < MAX_OPERANDS);
3269 operands->operand[opno] = operand;
14daeee3 3270 if (operand && operand->type != OP_VU0_MATCH_SUFFIX)
fc76e730 3271 {
14daeee3 3272 used_bits = mips_insert_operand (operand, used_bits, -1);
fc76e730
RS
3273 if (operand->type == OP_MDMX_IMM_REG)
3274 /* Bit 5 is the format selector (OB vs QH). The opcode table
3275 has separate entries for each format. */
3276 used_bits &= ~(1 << (operand->lsb + 5));
3277 if (operand->type == OP_ENTRY_EXIT_LIST)
3278 used_bits &= ~(mask & 0x700);
3279 }
ab902481 3280 /* Skip prefix characters. */
fc76e730 3281 if (decode_operand && (*s == '+' || *s == 'm'))
ab902481 3282 ++s;
fc76e730 3283 opno += 1;
ab902481
RS
3284 break;
3285 }
fc76e730 3286 doubled = used_bits & mask & insn_bits;
ab902481
RS
3287 if (doubled)
3288 {
3289 as_bad (_("internal: bad mips opcode (bits 0x%08lx doubly defined):"
3290 " %s %s"), doubled, opcode->name, opcode->args);
3291 return 0;
3292 }
fc76e730 3293 used_bits |= mask;
ab902481 3294 undefined = ~used_bits & insn_bits;
fc76e730 3295 if (opcode->pinfo != INSN_MACRO && undefined)
ab902481
RS
3296 {
3297 as_bad (_("internal: bad mips opcode (bits 0x%08lx undefined): %s %s"),
3298 undefined, opcode->name, opcode->args);
3299 return 0;
3300 }
3301 used_bits &= ~insn_bits;
3302 if (used_bits)
3303 {
3304 as_bad (_("internal: bad mips opcode (bits 0x%08lx defined): %s %s"),
3305 used_bits, opcode->name, opcode->args);
3306 return 0;
3307 }
3308 return 1;
3309}
3310
fc76e730
RS
3311/* The MIPS16 version of validate_mips_insn. */
3312
3313static int
3314validate_mips16_insn (const struct mips_opcode *opcode,
3315 struct mips_operand_array *operands)
3316{
3317 if (opcode->args[0] == 'a' || opcode->args[0] == 'i')
3318 {
3319 /* In this case OPCODE defines the first 16 bits in a 32-bit jump
3320 instruction. Use TMP to describe the full instruction. */
3321 struct mips_opcode tmp;
3322
3323 tmp = *opcode;
3324 tmp.match <<= 16;
3325 tmp.mask <<= 16;
3326 return validate_mips_insn (&tmp, 0xffffffff, 0, operands);
3327 }
3328 return validate_mips_insn (opcode, 0xffff, 0, operands);
3329}
3330
ab902481
RS
3331/* The microMIPS version of validate_mips_insn. */
3332
3333static int
fc76e730
RS
3334validate_micromips_insn (const struct mips_opcode *opc,
3335 struct mips_operand_array *operands)
ab902481
RS
3336{
3337 unsigned long insn_bits;
3338 unsigned long major;
3339 unsigned int length;
3340
fc76e730
RS
3341 if (opc->pinfo == INSN_MACRO)
3342 return validate_mips_insn (opc, 0xffffffff, decode_micromips_operand,
3343 operands);
3344
ab902481
RS
3345 length = micromips_insn_length (opc);
3346 if (length != 2 && length != 4)
3347 {
1661c76c 3348 as_bad (_("internal error: bad microMIPS opcode (incorrect length: %u): "
ab902481
RS
3349 "%s %s"), length, opc->name, opc->args);
3350 return 0;
3351 }
3352 major = opc->match >> (10 + 8 * (length - 2));
3353 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
3354 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
3355 {
1661c76c 3356 as_bad (_("internal error: bad microMIPS opcode "
ab902481
RS
3357 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
3358 return 0;
3359 }
3360
3361 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
3362 insn_bits = 1 << 4 * length;
3363 insn_bits <<= 4 * length;
3364 insn_bits -= 1;
fc76e730
RS
3365 return validate_mips_insn (opc, insn_bits, decode_micromips_operand,
3366 operands);
ab902481
RS
3367}
3368
707bfff6
TS
3369/* This function is called once, at assembler startup time. It should set up
3370 all the tables, etc. that the MD part of the assembler will need. */
156c2f8b 3371
252b5132 3372void
17a2f251 3373md_begin (void)
252b5132 3374{
3994f87e 3375 const char *retval = NULL;
156c2f8b 3376 int i = 0;
252b5132 3377 int broken = 0;
1f25f5d3 3378
0a44bf69
RS
3379 if (mips_pic != NO_PIC)
3380 {
3381 if (g_switch_seen && g_switch_value != 0)
3382 as_bad (_("-G may not be used in position-independent code"));
3383 g_switch_value = 0;
3384 }
3385
0b35dfee 3386 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
1661c76c 3387 as_warn (_("could not set architecture and machine"));
252b5132 3388
252b5132
RH
3389 op_hash = hash_new ();
3390
fc76e730 3391 mips_operands = XCNEWVEC (struct mips_operand_array, NUMOPCODES);
252b5132
RH
3392 for (i = 0; i < NUMOPCODES;)
3393 {
3394 const char *name = mips_opcodes[i].name;
3395
17a2f251 3396 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
3397 if (retval != NULL)
3398 {
3399 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
3400 mips_opcodes[i].name, retval);
3401 /* Probably a memory allocation problem? Give up now. */
1661c76c 3402 as_fatal (_("broken assembler, no assembly attempted"));
252b5132
RH
3403 }
3404 do
3405 {
fc76e730
RS
3406 if (!validate_mips_insn (&mips_opcodes[i], 0xffffffff,
3407 decode_mips_operand, &mips_operands[i]))
3408 broken = 1;
3409 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
252b5132 3410 {
fc76e730
RS
3411 create_insn (&nop_insn, mips_opcodes + i);
3412 if (mips_fix_loongson2f_nop)
3413 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
3414 nop_insn.fixed_p = 1;
252b5132
RH
3415 }
3416 ++i;
3417 }
3418 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
3419 }
3420
3421 mips16_op_hash = hash_new ();
fc76e730
RS
3422 mips16_operands = XCNEWVEC (struct mips_operand_array,
3423 bfd_mips16_num_opcodes);
252b5132
RH
3424
3425 i = 0;
3426 while (i < bfd_mips16_num_opcodes)
3427 {
3428 const char *name = mips16_opcodes[i].name;
3429
17a2f251 3430 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
3431 if (retval != NULL)
3432 as_fatal (_("internal: can't hash `%s': %s"),
3433 mips16_opcodes[i].name, retval);
3434 do
3435 {
fc76e730
RS
3436 if (!validate_mips16_insn (&mips16_opcodes[i], &mips16_operands[i]))
3437 broken = 1;
1e915849
RS
3438 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3439 {
3440 create_insn (&mips16_nop_insn, mips16_opcodes + i);
3441 mips16_nop_insn.fixed_p = 1;
3442 }
252b5132
RH
3443 ++i;
3444 }
3445 while (i < bfd_mips16_num_opcodes
3446 && strcmp (mips16_opcodes[i].name, name) == 0);
3447 }
3448
df58fc94 3449 micromips_op_hash = hash_new ();
fc76e730
RS
3450 micromips_operands = XCNEWVEC (struct mips_operand_array,
3451 bfd_micromips_num_opcodes);
df58fc94
RS
3452
3453 i = 0;
3454 while (i < bfd_micromips_num_opcodes)
3455 {
3456 const char *name = micromips_opcodes[i].name;
3457
3458 retval = hash_insert (micromips_op_hash, name,
3459 (void *) &micromips_opcodes[i]);
3460 if (retval != NULL)
3461 as_fatal (_("internal: can't hash `%s': %s"),
3462 micromips_opcodes[i].name, retval);
3463 do
fc76e730
RS
3464 {
3465 struct mips_cl_insn *micromips_nop_insn;
3466
3467 if (!validate_micromips_insn (&micromips_opcodes[i],
3468 &micromips_operands[i]))
3469 broken = 1;
3470
3471 if (micromips_opcodes[i].pinfo != INSN_MACRO)
3472 {
3473 if (micromips_insn_length (micromips_opcodes + i) == 2)
3474 micromips_nop_insn = &micromips_nop16_insn;
3475 else if (micromips_insn_length (micromips_opcodes + i) == 4)
3476 micromips_nop_insn = &micromips_nop32_insn;
3477 else
3478 continue;
3479
3480 if (micromips_nop_insn->insn_mo == NULL
3481 && strcmp (name, "nop") == 0)
3482 {
3483 create_insn (micromips_nop_insn, micromips_opcodes + i);
3484 micromips_nop_insn->fixed_p = 1;
3485 }
3486 }
3487 }
df58fc94
RS
3488 while (++i < bfd_micromips_num_opcodes
3489 && strcmp (micromips_opcodes[i].name, name) == 0);
3490 }
3491
252b5132 3492 if (broken)
1661c76c 3493 as_fatal (_("broken assembler, no assembly attempted"));
252b5132
RH
3494
3495 /* We add all the general register names to the symbol table. This
3496 helps us detect invalid uses of them. */
707bfff6
TS
3497 for (i = 0; reg_names[i].name; i++)
3498 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
8fc4ee9b 3499 reg_names[i].num, /* & RNUM_MASK, */
707bfff6
TS
3500 &zero_address_frag));
3501 if (HAVE_NEWABI)
3502 for (i = 0; reg_names_n32n64[i].name; i++)
3503 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
8fc4ee9b 3504 reg_names_n32n64[i].num, /* & RNUM_MASK, */
252b5132 3505 &zero_address_frag));
707bfff6
TS
3506 else
3507 for (i = 0; reg_names_o32[i].name; i++)
3508 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
8fc4ee9b 3509 reg_names_o32[i].num, /* & RNUM_MASK, */
6047c971 3510 &zero_address_frag));
6047c971 3511
14daeee3
RS
3512 for (i = 0; i < 32; i++)
3513 {
3514 char regname[7];
3515
3516 /* R5900 VU0 floating-point register. */
3517 regname[sizeof (rename) - 1] = 0;
3518 snprintf (regname, sizeof (regname) - 1, "$vf%d", i);
3519 symbol_table_insert (symbol_new (regname, reg_section,
3520 RTYPE_VF | i, &zero_address_frag));
3521
3522 /* R5900 VU0 integer register. */
3523 snprintf (regname, sizeof (regname) - 1, "$vi%d", i);
3524 symbol_table_insert (symbol_new (regname, reg_section,
3525 RTYPE_VI | i, &zero_address_frag));
3526
56d438b1
CF
3527 /* MSA register. */
3528 snprintf (regname, sizeof (regname) - 1, "$w%d", i);
3529 symbol_table_insert (symbol_new (regname, reg_section,
3530 RTYPE_MSA | i, &zero_address_frag));
14daeee3
RS
3531 }
3532
a92713e6
RS
3533 obstack_init (&mips_operand_tokens);
3534
7d10b47d 3535 mips_no_prev_insn ();
252b5132
RH
3536
3537 mips_gprmask = 0;
3538 mips_cprmask[0] = 0;
3539 mips_cprmask[1] = 0;
3540 mips_cprmask[2] = 0;
3541 mips_cprmask[3] = 0;
3542
3543 /* set the default alignment for the text section (2**2) */
3544 record_alignment (text_section, 2);
3545
4d0d148d 3546 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132 3547
f3ded42a
RS
3548 /* On a native system other than VxWorks, sections must be aligned
3549 to 16 byte boundaries. When configured for an embedded ELF
3550 target, we don't bother. */
3551 if (strncmp (TARGET_OS, "elf", 3) != 0
3552 && strncmp (TARGET_OS, "vxworks", 7) != 0)
252b5132 3553 {
f3ded42a
RS
3554 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
3555 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
3556 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
3557 }
252b5132 3558
f3ded42a
RS
3559 /* Create a .reginfo section for register masks and a .mdebug
3560 section for debugging information. */
3561 {
3562 segT seg;
3563 subsegT subseg;
3564 flagword flags;
3565 segT sec;
3566
3567 seg = now_seg;
3568 subseg = now_subseg;
3569
3570 /* The ABI says this section should be loaded so that the
3571 running program can access it. However, we don't load it
3572 if we are configured for an embedded target */
3573 flags = SEC_READONLY | SEC_DATA;
3574 if (strncmp (TARGET_OS, "elf", 3) != 0)
3575 flags |= SEC_ALLOC | SEC_LOAD;
3576
3577 if (mips_abi != N64_ABI)
252b5132 3578 {
f3ded42a 3579 sec = subseg_new (".reginfo", (subsegT) 0);
bdaaa2e1 3580
f3ded42a
RS
3581 bfd_set_section_flags (stdoutput, sec, flags);
3582 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
252b5132 3583
f3ded42a
RS
3584 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
3585 }
3586 else
3587 {
3588 /* The 64-bit ABI uses a .MIPS.options section rather than
3589 .reginfo section. */
3590 sec = subseg_new (".MIPS.options", (subsegT) 0);
3591 bfd_set_section_flags (stdoutput, sec, flags);
3592 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132 3593
f3ded42a
RS
3594 /* Set up the option header. */
3595 {
3596 Elf_Internal_Options opthdr;
3597 char *f;
3598
3599 opthdr.kind = ODK_REGINFO;
3600 opthdr.size = (sizeof (Elf_External_Options)
3601 + sizeof (Elf64_External_RegInfo));
3602 opthdr.section = 0;
3603 opthdr.info = 0;
3604 f = frag_more (sizeof (Elf_External_Options));
3605 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
3606 (Elf_External_Options *) f);
3607
3608 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
3609 }
3610 }
252b5132 3611
f3ded42a
RS
3612 if (ECOFF_DEBUGGING)
3613 {
3614 sec = subseg_new (".mdebug", (subsegT) 0);
3615 (void) bfd_set_section_flags (stdoutput, sec,
3616 SEC_HAS_CONTENTS | SEC_READONLY);
3617 (void) bfd_set_section_alignment (stdoutput, sec, 2);
252b5132 3618 }
f3ded42a
RS
3619 else if (mips_flag_pdr)
3620 {
3621 pdr_seg = subseg_new (".pdr", (subsegT) 0);
3622 (void) bfd_set_section_flags (stdoutput, pdr_seg,
3623 SEC_READONLY | SEC_RELOC
3624 | SEC_DEBUGGING);
3625 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
3626 }
3627
3628 subseg_set (seg, subseg);
3629 }
252b5132
RH
3630
3631 if (! ECOFF_DEBUGGING)
3632 md_obj_begin ();
71400594
RS
3633
3634 if (mips_fix_vr4120)
3635 init_vr4120_conflicts ();
252b5132
RH
3636}
3637
3638void
17a2f251 3639md_mips_end (void)
252b5132 3640{
02b1ab82 3641 mips_emit_delays ();
252b5132
RH
3642 if (! ECOFF_DEBUGGING)
3643 md_obj_end ();
3644}
3645
3646void
17a2f251 3647md_assemble (char *str)
252b5132
RH
3648{
3649 struct mips_cl_insn insn;
f6688943
TS
3650 bfd_reloc_code_real_type unused_reloc[3]
3651 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
3652
3653 imm_expr.X_op = O_absent;
252b5132 3654 offset_expr.X_op = O_absent;
f6688943
TS
3655 offset_reloc[0] = BFD_RELOC_UNUSED;
3656 offset_reloc[1] = BFD_RELOC_UNUSED;
3657 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132 3658
e1b47bd5
RS
3659 mips_mark_labels ();
3660 mips_assembling_insn = TRUE;
e3de51ce 3661 clear_insn_error ();
e1b47bd5 3662
252b5132
RH
3663 if (mips_opts.mips16)
3664 mips16_ip (str, &insn);
3665 else
3666 {
3667 mips_ip (str, &insn);
beae10d5
KH
3668 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
3669 str, insn.insn_opcode));
252b5132
RH
3670 }
3671
e3de51ce
RS
3672 if (insn_error.msg)
3673 report_insn_error (str);
e1b47bd5 3674 else if (insn.insn_mo->pinfo == INSN_MACRO)
252b5132 3675 {
584892a6 3676 macro_start ();
252b5132
RH
3677 if (mips_opts.mips16)
3678 mips16_macro (&insn);
3679 else
833794fc 3680 macro (&insn, str);
584892a6 3681 macro_end ();
252b5132
RH
3682 }
3683 else
3684 {
77bd4346 3685 if (offset_expr.X_op != O_absent)
df58fc94 3686 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
252b5132 3687 else
df58fc94 3688 append_insn (&insn, NULL, unused_reloc, FALSE);
252b5132 3689 }
e1b47bd5
RS
3690
3691 mips_assembling_insn = FALSE;
252b5132
RH
3692}
3693
738e5348
RS
3694/* Convenience functions for abstracting away the differences between
3695 MIPS16 and non-MIPS16 relocations. */
3696
3697static inline bfd_boolean
3698mips16_reloc_p (bfd_reloc_code_real_type reloc)
3699{
3700 switch (reloc)
3701 {
3702 case BFD_RELOC_MIPS16_JMP:
3703 case BFD_RELOC_MIPS16_GPREL:
3704 case BFD_RELOC_MIPS16_GOT16:
3705 case BFD_RELOC_MIPS16_CALL16:
3706 case BFD_RELOC_MIPS16_HI16_S:
3707 case BFD_RELOC_MIPS16_HI16:
3708 case BFD_RELOC_MIPS16_LO16:
3709 return TRUE;
3710
3711 default:
3712 return FALSE;
3713 }
3714}
3715
df58fc94
RS
3716static inline bfd_boolean
3717micromips_reloc_p (bfd_reloc_code_real_type reloc)
3718{
3719 switch (reloc)
3720 {
3721 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
3722 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
3723 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
3724 case BFD_RELOC_MICROMIPS_GPREL16:
3725 case BFD_RELOC_MICROMIPS_JMP:
3726 case BFD_RELOC_MICROMIPS_HI16:
3727 case BFD_RELOC_MICROMIPS_HI16_S:
3728 case BFD_RELOC_MICROMIPS_LO16:
3729 case BFD_RELOC_MICROMIPS_LITERAL:
3730 case BFD_RELOC_MICROMIPS_GOT16:
3731 case BFD_RELOC_MICROMIPS_CALL16:
3732 case BFD_RELOC_MICROMIPS_GOT_HI16:
3733 case BFD_RELOC_MICROMIPS_GOT_LO16:
3734 case BFD_RELOC_MICROMIPS_CALL_HI16:
3735 case BFD_RELOC_MICROMIPS_CALL_LO16:
3736 case BFD_RELOC_MICROMIPS_SUB:
3737 case BFD_RELOC_MICROMIPS_GOT_PAGE:
3738 case BFD_RELOC_MICROMIPS_GOT_OFST:
3739 case BFD_RELOC_MICROMIPS_GOT_DISP:
3740 case BFD_RELOC_MICROMIPS_HIGHEST:
3741 case BFD_RELOC_MICROMIPS_HIGHER:
3742 case BFD_RELOC_MICROMIPS_SCN_DISP:
3743 case BFD_RELOC_MICROMIPS_JALR:
3744 return TRUE;
3745
3746 default:
3747 return FALSE;
3748 }
3749}
3750
2309ddf2
MR
3751static inline bfd_boolean
3752jmp_reloc_p (bfd_reloc_code_real_type reloc)
3753{
3754 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
3755}
3756
738e5348
RS
3757static inline bfd_boolean
3758got16_reloc_p (bfd_reloc_code_real_type reloc)
3759{
2309ddf2 3760 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
df58fc94 3761 || reloc == BFD_RELOC_MICROMIPS_GOT16);
738e5348
RS
3762}
3763
3764static inline bfd_boolean
3765hi16_reloc_p (bfd_reloc_code_real_type reloc)
3766{
2309ddf2 3767 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
df58fc94 3768 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
738e5348
RS
3769}
3770
3771static inline bfd_boolean
3772lo16_reloc_p (bfd_reloc_code_real_type reloc)
3773{
2309ddf2 3774 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
df58fc94
RS
3775 || reloc == BFD_RELOC_MICROMIPS_LO16);
3776}
3777
df58fc94
RS
3778static inline bfd_boolean
3779jalr_reloc_p (bfd_reloc_code_real_type reloc)
3780{
2309ddf2 3781 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
738e5348
RS
3782}
3783
f2ae14a1
RS
3784static inline bfd_boolean
3785gprel16_reloc_p (bfd_reloc_code_real_type reloc)
3786{
3787 return (reloc == BFD_RELOC_GPREL16 || reloc == BFD_RELOC_MIPS16_GPREL
3788 || reloc == BFD_RELOC_MICROMIPS_GPREL16);
3789}
3790
2de39019
CM
3791/* Return true if RELOC is a PC-relative relocation that does not have
3792 full address range. */
3793
3794static inline bfd_boolean
3795limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
3796{
3797 switch (reloc)
3798 {
3799 case BFD_RELOC_16_PCREL_S2:
3800 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
3801 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
3802 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
3803 return TRUE;
3804
b47468a6
CM
3805 case BFD_RELOC_32_PCREL:
3806 return HAVE_64BIT_ADDRESSES;
3807
2de39019
CM
3808 default:
3809 return FALSE;
3810 }
3811}
b47468a6 3812
5919d012 3813/* Return true if the given relocation might need a matching %lo().
0a44bf69
RS
3814 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
3815 need a matching %lo() when applied to local symbols. */
5919d012
RS
3816
3817static inline bfd_boolean
17a2f251 3818reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 3819{
3b91255e 3820 return (HAVE_IN_PLACE_ADDENDS
738e5348 3821 && (hi16_reloc_p (reloc)
0a44bf69
RS
3822 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
3823 all GOT16 relocations evaluate to "G". */
738e5348
RS
3824 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
3825}
3826
3827/* Return the type of %lo() reloc needed by RELOC, given that
3828 reloc_needs_lo_p. */
3829
3830static inline bfd_reloc_code_real_type
3831matching_lo_reloc (bfd_reloc_code_real_type reloc)
3832{
df58fc94
RS
3833 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
3834 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
3835 : BFD_RELOC_LO16));
5919d012
RS
3836}
3837
3838/* Return true if the given fixup is followed by a matching R_MIPS_LO16
3839 relocation. */
3840
3841static inline bfd_boolean
17a2f251 3842fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
3843{
3844 return (fixp->fx_next != NULL
738e5348 3845 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
5919d012
RS
3846 && fixp->fx_addsy == fixp->fx_next->fx_addsy
3847 && fixp->fx_offset == fixp->fx_next->fx_offset);
3848}
3849
462427c4
RS
3850/* Move all labels in LABELS to the current insertion point. TEXT_P
3851 says whether the labels refer to text or data. */
404a8071
RS
3852
3853static void
462427c4 3854mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
404a8071
RS
3855{
3856 struct insn_label_list *l;
3857 valueT val;
3858
462427c4 3859 for (l = labels; l != NULL; l = l->next)
404a8071 3860 {
9c2799c2 3861 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
404a8071
RS
3862 symbol_set_frag (l->label, frag_now);
3863 val = (valueT) frag_now_fix ();
df58fc94 3864 /* MIPS16/microMIPS text labels are stored as odd. */
462427c4 3865 if (text_p && HAVE_CODE_COMPRESSION)
404a8071
RS
3866 ++val;
3867 S_SET_VALUE (l->label, val);
3868 }
3869}
3870
462427c4
RS
3871/* Move all labels in insn_labels to the current insertion point
3872 and treat them as text labels. */
3873
3874static void
3875mips_move_text_labels (void)
3876{
3877 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
3878}
3879
5f0fe04b
TS
3880static bfd_boolean
3881s_is_linkonce (symbolS *sym, segT from_seg)
3882{
3883 bfd_boolean linkonce = FALSE;
3884 segT symseg = S_GET_SEGMENT (sym);
3885
3886 if (symseg != from_seg && !S_IS_LOCAL (sym))
3887 {
3888 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
3889 linkonce = TRUE;
5f0fe04b
TS
3890 /* The GNU toolchain uses an extension for ELF: a section
3891 beginning with the magic string .gnu.linkonce is a
3892 linkonce section. */
3893 if (strncmp (segment_name (symseg), ".gnu.linkonce",
3894 sizeof ".gnu.linkonce" - 1) == 0)
3895 linkonce = TRUE;
5f0fe04b
TS
3896 }
3897 return linkonce;
3898}
3899
e1b47bd5 3900/* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
df58fc94
RS
3901 linker to handle them specially, such as generating jalx instructions
3902 when needed. We also make them odd for the duration of the assembly,
3903 in order to generate the right sort of code. We will make them even
252b5132
RH
3904 in the adjust_symtab routine, while leaving them marked. This is
3905 convenient for the debugger and the disassembler. The linker knows
3906 to make them odd again. */
3907
3908static void
e1b47bd5 3909mips_compressed_mark_label (symbolS *label)
252b5132 3910{
df58fc94 3911 gas_assert (HAVE_CODE_COMPRESSION);
a8dbcb85 3912
f3ded42a
RS
3913 if (mips_opts.mips16)
3914 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
3915 else
3916 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
e1b47bd5
RS
3917 if ((S_GET_VALUE (label) & 1) == 0
3918 /* Don't adjust the address if the label is global or weak, or
3919 in a link-once section, since we'll be emitting symbol reloc
3920 references to it which will be patched up by the linker, and
3921 the final value of the symbol may or may not be MIPS16/microMIPS. */
3922 && !S_IS_WEAK (label)
3923 && !S_IS_EXTERNAL (label)
3924 && !s_is_linkonce (label, now_seg))
3925 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
3926}
3927
3928/* Mark preceding MIPS16 or microMIPS instruction labels. */
3929
3930static void
3931mips_compressed_mark_labels (void)
3932{
3933 struct insn_label_list *l;
3934
3935 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
3936 mips_compressed_mark_label (l->label);
252b5132
RH
3937}
3938
4d7206a2
RS
3939/* End the current frag. Make it a variant frag and record the
3940 relaxation info. */
3941
3942static void
3943relax_close_frag (void)
3944{
584892a6 3945 mips_macro_warning.first_frag = frag_now;
4d7206a2 3946 frag_var (rs_machine_dependent, 0, 0,
584892a6 3947 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
4d7206a2
RS
3948 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
3949
3950 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
3951 mips_relax.first_fixup = 0;
3952}
3953
3954/* Start a new relaxation sequence whose expansion depends on SYMBOL.
3955 See the comment above RELAX_ENCODE for more details. */
3956
3957static void
3958relax_start (symbolS *symbol)
3959{
9c2799c2 3960 gas_assert (mips_relax.sequence == 0);
4d7206a2
RS
3961 mips_relax.sequence = 1;
3962 mips_relax.symbol = symbol;
3963}
3964
3965/* Start generating the second version of a relaxable sequence.
3966 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
3967
3968static void
4d7206a2
RS
3969relax_switch (void)
3970{
9c2799c2 3971 gas_assert (mips_relax.sequence == 1);
4d7206a2
RS
3972 mips_relax.sequence = 2;
3973}
3974
3975/* End the current relaxable sequence. */
3976
3977static void
3978relax_end (void)
3979{
9c2799c2 3980 gas_assert (mips_relax.sequence == 2);
4d7206a2
RS
3981 relax_close_frag ();
3982 mips_relax.sequence = 0;
3983}
3984
11625dd8
RS
3985/* Return true if IP is a delayed branch or jump. */
3986
3987static inline bfd_boolean
3988delayed_branch_p (const struct mips_cl_insn *ip)
3989{
3990 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
3991 | INSN_COND_BRANCH_DELAY
3992 | INSN_COND_BRANCH_LIKELY)) != 0;
3993}
3994
3995/* Return true if IP is a compact branch or jump. */
3996
3997static inline bfd_boolean
3998compact_branch_p (const struct mips_cl_insn *ip)
3999{
26545944
RS
4000 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
4001 | INSN2_COND_BRANCH)) != 0;
11625dd8
RS
4002}
4003
4004/* Return true if IP is an unconditional branch or jump. */
4005
4006static inline bfd_boolean
4007uncond_branch_p (const struct mips_cl_insn *ip)
4008{
4009 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
26545944 4010 || (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0);
11625dd8
RS
4011}
4012
4013/* Return true if IP is a branch-likely instruction. */
4014
4015static inline bfd_boolean
4016branch_likely_p (const struct mips_cl_insn *ip)
4017{
4018 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
4019}
4020
14fe068b
RS
4021/* Return the type of nop that should be used to fill the delay slot
4022 of delayed branch IP. */
4023
4024static struct mips_cl_insn *
4025get_delay_slot_nop (const struct mips_cl_insn *ip)
4026{
4027 if (mips_opts.micromips
4028 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
4029 return &micromips_nop32_insn;
4030 return NOP_INSN;
4031}
4032
fc76e730
RS
4033/* Return a mask that has bit N set if OPCODE reads the register(s)
4034 in operand N. */
df58fc94
RS
4035
4036static unsigned int
fc76e730 4037insn_read_mask (const struct mips_opcode *opcode)
df58fc94 4038{
fc76e730
RS
4039 return (opcode->pinfo & INSN_READ_ALL) >> INSN_READ_SHIFT;
4040}
df58fc94 4041
fc76e730
RS
4042/* Return a mask that has bit N set if OPCODE writes to the register(s)
4043 in operand N. */
4044
4045static unsigned int
4046insn_write_mask (const struct mips_opcode *opcode)
4047{
4048 return (opcode->pinfo & INSN_WRITE_ALL) >> INSN_WRITE_SHIFT;
4049}
4050
4051/* Return a mask of the registers specified by operand OPERAND of INSN.
4052 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4053 is set. */
4054
4055static unsigned int
4056operand_reg_mask (const struct mips_cl_insn *insn,
4057 const struct mips_operand *operand,
4058 unsigned int type_mask)
4059{
4060 unsigned int uval, vsel;
4061
4062 switch (operand->type)
df58fc94 4063 {
fc76e730
RS
4064 case OP_INT:
4065 case OP_MAPPED_INT:
4066 case OP_MSB:
4067 case OP_PCREL:
4068 case OP_PERF_REG:
4069 case OP_ADDIUSP_INT:
4070 case OP_ENTRY_EXIT_LIST:
4071 case OP_REPEAT_DEST_REG:
4072 case OP_REPEAT_PREV_REG:
4073 case OP_PC:
14daeee3
RS
4074 case OP_VU0_SUFFIX:
4075 case OP_VU0_MATCH_SUFFIX:
56d438b1 4076 case OP_IMM_INDEX:
fc76e730
RS
4077 abort ();
4078
4079 case OP_REG:
0f35dbc4 4080 case OP_OPTIONAL_REG:
fc76e730
RS
4081 {
4082 const struct mips_reg_operand *reg_op;
4083
4084 reg_op = (const struct mips_reg_operand *) operand;
4085 if (!(type_mask & (1 << reg_op->reg_type)))
4086 return 0;
4087 uval = insn_extract_operand (insn, operand);
4088 return 1 << mips_decode_reg_operand (reg_op, uval);
4089 }
4090
4091 case OP_REG_PAIR:
4092 {
4093 const struct mips_reg_pair_operand *pair_op;
4094
4095 pair_op = (const struct mips_reg_pair_operand *) operand;
4096 if (!(type_mask & (1 << pair_op->reg_type)))
4097 return 0;
4098 uval = insn_extract_operand (insn, operand);
4099 return (1 << pair_op->reg1_map[uval]) | (1 << pair_op->reg2_map[uval]);
4100 }
4101
4102 case OP_CLO_CLZ_DEST:
4103 if (!(type_mask & (1 << OP_REG_GP)))
4104 return 0;
4105 uval = insn_extract_operand (insn, operand);
4106 return (1 << (uval & 31)) | (1 << (uval >> 5));
4107
4108 case OP_LWM_SWM_LIST:
4109 abort ();
4110
4111 case OP_SAVE_RESTORE_LIST:
4112 abort ();
4113
4114 case OP_MDMX_IMM_REG:
4115 if (!(type_mask & (1 << OP_REG_VEC)))
4116 return 0;
4117 uval = insn_extract_operand (insn, operand);
4118 vsel = uval >> 5;
4119 if ((vsel & 0x18) == 0x18)
4120 return 0;
4121 return 1 << (uval & 31);
56d438b1
CF
4122
4123 case OP_REG_INDEX:
4124 if (!(type_mask & (1 << OP_REG_GP)))
4125 return 0;
4126 return 1 << insn_extract_operand (insn, operand);
df58fc94 4127 }
fc76e730
RS
4128 abort ();
4129}
4130
4131/* Return a mask of the registers specified by operands OPNO_MASK of INSN,
4132 where bit N of OPNO_MASK is set if operand N should be included.
4133 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4134 is set. */
4135
4136static unsigned int
4137insn_reg_mask (const struct mips_cl_insn *insn,
4138 unsigned int type_mask, unsigned int opno_mask)
4139{
4140 unsigned int opno, reg_mask;
4141
4142 opno = 0;
4143 reg_mask = 0;
4144 while (opno_mask != 0)
4145 {
4146 if (opno_mask & 1)
4147 reg_mask |= operand_reg_mask (insn, insn_opno (insn, opno), type_mask);
4148 opno_mask >>= 1;
4149 opno += 1;
4150 }
4151 return reg_mask;
df58fc94
RS
4152}
4153
4c260379
RS
4154/* Return the mask of core registers that IP reads. */
4155
4156static unsigned int
4157gpr_read_mask (const struct mips_cl_insn *ip)
4158{
4159 unsigned long pinfo, pinfo2;
4160 unsigned int mask;
4161
fc76e730 4162 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_read_mask (ip->insn_mo));
4c260379
RS
4163 pinfo = ip->insn_mo->pinfo;
4164 pinfo2 = ip->insn_mo->pinfo2;
fc76e730 4165 if (pinfo & INSN_UDI)
4c260379 4166 {
fc76e730
RS
4167 /* UDI instructions have traditionally been assumed to read RS
4168 and RT. */
4169 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4170 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4c260379 4171 }
fc76e730
RS
4172 if (pinfo & INSN_READ_GPR_24)
4173 mask |= 1 << 24;
4174 if (pinfo2 & INSN2_READ_GPR_16)
4175 mask |= 1 << 16;
4176 if (pinfo2 & INSN2_READ_SP)
4177 mask |= 1 << SP;
26545944 4178 if (pinfo2 & INSN2_READ_GPR_31)
fc76e730 4179 mask |= 1 << 31;
fe35f09f
RS
4180 /* Don't include register 0. */
4181 return mask & ~1;
4c260379
RS
4182}
4183
4184/* Return the mask of core registers that IP writes. */
4185
4186static unsigned int
4187gpr_write_mask (const struct mips_cl_insn *ip)
4188{
4189 unsigned long pinfo, pinfo2;
4190 unsigned int mask;
4191
fc76e730 4192 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_write_mask (ip->insn_mo));
4c260379
RS
4193 pinfo = ip->insn_mo->pinfo;
4194 pinfo2 = ip->insn_mo->pinfo2;
fc76e730
RS
4195 if (pinfo & INSN_WRITE_GPR_24)
4196 mask |= 1 << 24;
4197 if (pinfo & INSN_WRITE_GPR_31)
4198 mask |= 1 << 31;
4199 if (pinfo & INSN_UDI)
4200 /* UDI instructions have traditionally been assumed to write to RD. */
4201 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4202 if (pinfo2 & INSN2_WRITE_SP)
4203 mask |= 1 << SP;
fe35f09f
RS
4204 /* Don't include register 0. */
4205 return mask & ~1;
4c260379
RS
4206}
4207
4208/* Return the mask of floating-point registers that IP reads. */
4209
4210static unsigned int
4211fpr_read_mask (const struct mips_cl_insn *ip)
4212{
fc76e730 4213 unsigned long pinfo;
4c260379
RS
4214 unsigned int mask;
4215
9d5de888
CF
4216 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4217 | (1 << OP_REG_MSA)),
fc76e730 4218 insn_read_mask (ip->insn_mo));
4c260379 4219 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4220 /* Conservatively treat all operands to an FP_D instruction are doubles.
4221 (This is overly pessimistic for things like cvt.d.s.) */
4222 if (HAVE_32BIT_FPRS && (pinfo & FP_D))
4223 mask |= mask << 1;
4224 return mask;
4225}
4226
4227/* Return the mask of floating-point registers that IP writes. */
4228
4229static unsigned int
4230fpr_write_mask (const struct mips_cl_insn *ip)
4231{
fc76e730 4232 unsigned long pinfo;
4c260379
RS
4233 unsigned int mask;
4234
9d5de888
CF
4235 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4236 | (1 << OP_REG_MSA)),
fc76e730 4237 insn_write_mask (ip->insn_mo));
4c260379 4238 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4239 /* Conservatively treat all operands to an FP_D instruction are doubles.
4240 (This is overly pessimistic for things like cvt.s.d.) */
4241 if (HAVE_32BIT_FPRS && (pinfo & FP_D))
4242 mask |= mask << 1;
4243 return mask;
4244}
4245
a1d78564
RS
4246/* Operand OPNUM of INSN is an odd-numbered floating-point register.
4247 Check whether that is allowed. */
4248
4249static bfd_boolean
4250mips_oddfpreg_ok (const struct mips_opcode *insn, int opnum)
4251{
4252 const char *s = insn->name;
4253
4254 if (insn->pinfo == INSN_MACRO)
4255 /* Let a macro pass, we'll catch it later when it is expanded. */
4256 return TRUE;
4257
4258 if (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa) || mips_opts.arch == CPU_R5900)
4259 {
4260 /* Allow odd registers for single-precision ops. */
4261 switch (insn->pinfo & (FP_S | FP_D))
4262 {
4263 case FP_S:
4264 case 0:
4265 return TRUE;
4266 case FP_D:
4267 return FALSE;
4268 default:
4269 break;
4270 }
4271
4272 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
4273 s = strchr (insn->name, '.');
4274 if (s != NULL && opnum == 2)
4275 s = strchr (s + 1, '.');
4276 return (s != NULL && (s[1] == 'w' || s[1] == 's'));
4277 }
4278
4279 /* Single-precision coprocessor loads and moves are OK too. */
4280 if ((insn->pinfo & FP_S)
4281 && (insn->pinfo & (INSN_COPROC_MEMORY_DELAY | INSN_STORE_MEMORY
4282 | INSN_LOAD_COPROC_DELAY | INSN_COPROC_MOVE_DELAY)))
4283 return TRUE;
4284
4285 return FALSE;
4286}
4287
a1d78564
RS
4288/* Information about an instruction argument that we're trying to match. */
4289struct mips_arg_info
4290{
4291 /* The instruction so far. */
4292 struct mips_cl_insn *insn;
4293
a92713e6
RS
4294 /* The first unconsumed operand token. */
4295 struct mips_operand_token *token;
4296
a1d78564
RS
4297 /* The 1-based operand number, in terms of insn->insn_mo->args. */
4298 int opnum;
4299
4300 /* The 1-based argument number, for error reporting. This does not
4301 count elided optional registers, etc.. */
4302 int argnum;
4303
4304 /* The last OP_REG operand seen, or ILLEGAL_REG if none. */
4305 unsigned int last_regno;
4306
4307 /* If the first operand was an OP_REG, this is the register that it
4308 specified, otherwise it is ILLEGAL_REG. */
4309 unsigned int dest_regno;
4310
4311 /* The value of the last OP_INT operand. Only used for OP_MSB,
4312 where it gives the lsb position. */
4313 unsigned int last_op_int;
4314
60f20e8b
RS
4315 /* If true, match routines should assume that no later instruction
4316 alternative matches and should therefore be as accomodating as
4317 possible. Match routines should not report errors if something
4318 is only invalid for !LAX_MATCH. */
4319 bfd_boolean lax_match;
a1d78564 4320
a1d78564
RS
4321 /* True if a reference to the current AT register was seen. */
4322 bfd_boolean seen_at;
4323};
4324
1a00e612
RS
4325/* Record that the argument is out of range. */
4326
4327static void
4328match_out_of_range (struct mips_arg_info *arg)
4329{
4330 set_insn_error_i (arg->argnum, _("operand %d out of range"), arg->argnum);
4331}
4332
4333/* Record that the argument isn't constant but needs to be. */
4334
4335static void
4336match_not_constant (struct mips_arg_info *arg)
4337{
4338 set_insn_error_i (arg->argnum, _("operand %d must be constant"),
4339 arg->argnum);
4340}
4341
a92713e6
RS
4342/* Try to match an OT_CHAR token for character CH. Consume the token
4343 and return true on success, otherwise return false. */
a1d78564 4344
a92713e6
RS
4345static bfd_boolean
4346match_char (struct mips_arg_info *arg, char ch)
a1d78564 4347{
a92713e6
RS
4348 if (arg->token->type == OT_CHAR && arg->token->u.ch == ch)
4349 {
4350 ++arg->token;
4351 if (ch == ',')
4352 arg->argnum += 1;
4353 return TRUE;
4354 }
4355 return FALSE;
4356}
a1d78564 4357
a92713e6
RS
4358/* Try to get an expression from the next tokens in ARG. Consume the
4359 tokens and return true on success, storing the expression value in
4360 VALUE and relocation types in R. */
4361
4362static bfd_boolean
4363match_expression (struct mips_arg_info *arg, expressionS *value,
4364 bfd_reloc_code_real_type *r)
4365{
d436c1c2
RS
4366 /* If the next token is a '(' that was parsed as being part of a base
4367 expression, assume we have an elided offset. The later match will fail
4368 if this turns out to be wrong. */
4369 if (arg->token->type == OT_CHAR && arg->token->u.ch == '(')
a1d78564 4370 {
d436c1c2
RS
4371 value->X_op = O_constant;
4372 value->X_add_number = 0;
4373 r[0] = r[1] = r[2] = BFD_RELOC_UNUSED;
a92713e6
RS
4374 return TRUE;
4375 }
4376
d436c1c2
RS
4377 /* Reject register-based expressions such as "0+$2" and "(($2))".
4378 For plain registers the default error seems more appropriate. */
4379 if (arg->token->type == OT_INTEGER
4380 && arg->token->u.integer.value.X_op == O_register)
a92713e6 4381 {
d436c1c2
RS
4382 set_insn_error (arg->argnum, _("register value used as expression"));
4383 return FALSE;
a1d78564 4384 }
d436c1c2
RS
4385
4386 if (arg->token->type == OT_INTEGER)
a92713e6 4387 {
d436c1c2
RS
4388 *value = arg->token->u.integer.value;
4389 memcpy (r, arg->token->u.integer.relocs, 3 * sizeof (*r));
4390 ++arg->token;
4391 return TRUE;
a92713e6 4392 }
a92713e6 4393
d436c1c2
RS
4394 set_insn_error_i
4395 (arg->argnum, _("operand %d must be an immediate expression"),
4396 arg->argnum);
4397 return FALSE;
a92713e6
RS
4398}
4399
4400/* Try to get a constant expression from the next tokens in ARG. Consume
4401 the tokens and return return true on success, storing the constant value
4402 in *VALUE. Use FALLBACK as the value if the match succeeded with an
4403 error. */
4404
4405static bfd_boolean
1a00e612 4406match_const_int (struct mips_arg_info *arg, offsetT *value)
a92713e6
RS
4407{
4408 expressionS ex;
4409 bfd_reloc_code_real_type r[3];
a1d78564 4410
a92713e6
RS
4411 if (!match_expression (arg, &ex, r))
4412 return FALSE;
4413
4414 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_constant)
a1d78564
RS
4415 *value = ex.X_add_number;
4416 else
4417 {
1a00e612
RS
4418 match_not_constant (arg);
4419 return FALSE;
a1d78564 4420 }
a92713e6 4421 return TRUE;
a1d78564
RS
4422}
4423
4424/* Return the RTYPE_* flags for a register operand of type TYPE that
4425 appears in instruction OPCODE. */
4426
4427static unsigned int
4428convert_reg_type (const struct mips_opcode *opcode,
4429 enum mips_reg_operand_type type)
4430{
4431 switch (type)
4432 {
4433 case OP_REG_GP:
4434 return RTYPE_NUM | RTYPE_GP;
4435
4436 case OP_REG_FP:
4437 /* Allow vector register names for MDMX if the instruction is a 64-bit
4438 FPR load, store or move (including moves to and from GPRs). */
4439 if ((mips_opts.ase & ASE_MDMX)
4440 && (opcode->pinfo & FP_D)
4441 && (opcode->pinfo & (INSN_COPROC_MOVE_DELAY
4442 | INSN_COPROC_MEMORY_DELAY
4443 | INSN_LOAD_COPROC_DELAY
67dc82bc 4444 | INSN_LOAD_MEMORY
a1d78564
RS
4445 | INSN_STORE_MEMORY)))
4446 return RTYPE_FPU | RTYPE_VEC;
4447 return RTYPE_FPU;
4448
4449 case OP_REG_CCC:
4450 if (opcode->pinfo & (FP_D | FP_S))
4451 return RTYPE_CCC | RTYPE_FCC;
4452 return RTYPE_CCC;
4453
4454 case OP_REG_VEC:
4455 if (opcode->membership & INSN_5400)
4456 return RTYPE_FPU;
4457 return RTYPE_FPU | RTYPE_VEC;
4458
4459 case OP_REG_ACC:
4460 return RTYPE_ACC;
4461
4462 case OP_REG_COPRO:
4463 if (opcode->name[strlen (opcode->name) - 1] == '0')
4464 return RTYPE_NUM | RTYPE_CP0;
4465 return RTYPE_NUM;
4466
4467 case OP_REG_HW:
4468 return RTYPE_NUM;
14daeee3
RS
4469
4470 case OP_REG_VI:
4471 return RTYPE_NUM | RTYPE_VI;
4472
4473 case OP_REG_VF:
4474 return RTYPE_NUM | RTYPE_VF;
4475
4476 case OP_REG_R5900_I:
4477 return RTYPE_R5900_I;
4478
4479 case OP_REG_R5900_Q:
4480 return RTYPE_R5900_Q;
4481
4482 case OP_REG_R5900_R:
4483 return RTYPE_R5900_R;
4484
4485 case OP_REG_R5900_ACC:
4486 return RTYPE_R5900_ACC;
56d438b1
CF
4487
4488 case OP_REG_MSA:
4489 return RTYPE_MSA;
4490
4491 case OP_REG_MSA_CTRL:
4492 return RTYPE_NUM;
a1d78564
RS
4493 }
4494 abort ();
4495}
4496
4497/* ARG is register REGNO, of type TYPE. Warn about any dubious registers. */
4498
4499static void
4500check_regno (struct mips_arg_info *arg,
4501 enum mips_reg_operand_type type, unsigned int regno)
4502{
4503 if (AT && type == OP_REG_GP && regno == AT)
4504 arg->seen_at = TRUE;
4505
4506 if (type == OP_REG_FP
4507 && (regno & 1) != 0
4508 && HAVE_32BIT_FPRS
4509 && !mips_oddfpreg_ok (arg->insn->insn_mo, arg->opnum))
1661c76c 4510 as_warn (_("float register should be even, was %d"), regno);
a1d78564
RS
4511
4512 if (type == OP_REG_CCC)
4513 {
4514 const char *name;
4515 size_t length;
4516
4517 name = arg->insn->insn_mo->name;
4518 length = strlen (name);
4519 if ((regno & 1) != 0
4520 && ((length >= 3 && strcmp (name + length - 3, ".ps") == 0)
4521 || (length >= 5 && strncmp (name + length - 5, "any2", 4) == 0)))
1661c76c 4522 as_warn (_("condition code register should be even for %s, was %d"),
a1d78564
RS
4523 name, regno);
4524
4525 if ((regno & 3) != 0
4526 && (length >= 5 && strncmp (name + length - 5, "any4", 4) == 0))
1661c76c 4527 as_warn (_("condition code register should be 0 or 4 for %s, was %d"),
a1d78564
RS
4528 name, regno);
4529 }
4530}
4531
a92713e6
RS
4532/* ARG is a register with symbol value SYMVAL. Try to interpret it as
4533 a register of type TYPE. Return true on success, storing the register
4534 number in *REGNO and warning about any dubious uses. */
4535
4536static bfd_boolean
4537match_regno (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4538 unsigned int symval, unsigned int *regno)
4539{
4540 if (type == OP_REG_VEC)
4541 symval = mips_prefer_vec_regno (symval);
4542 if (!(symval & convert_reg_type (arg->insn->insn_mo, type)))
4543 return FALSE;
4544
4545 *regno = symval & RNUM_MASK;
4546 check_regno (arg, type, *regno);
4547 return TRUE;
4548}
4549
4550/* Try to interpret the next token in ARG as a register of type TYPE.
4551 Consume the token and return true on success, storing the register
4552 number in *REGNO. Return false on failure. */
4553
4554static bfd_boolean
4555match_reg (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4556 unsigned int *regno)
4557{
4558 if (arg->token->type == OT_REG
4559 && match_regno (arg, type, arg->token->u.regno, regno))
4560 {
4561 ++arg->token;
4562 return TRUE;
4563 }
4564 return FALSE;
4565}
4566
4567/* Try to interpret the next token in ARG as a range of registers of type TYPE.
4568 Consume the token and return true on success, storing the register numbers
4569 in *REGNO1 and *REGNO2. Return false on failure. */
4570
4571static bfd_boolean
4572match_reg_range (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4573 unsigned int *regno1, unsigned int *regno2)
4574{
4575 if (match_reg (arg, type, regno1))
4576 {
4577 *regno2 = *regno1;
4578 return TRUE;
4579 }
4580 if (arg->token->type == OT_REG_RANGE
4581 && match_regno (arg, type, arg->token->u.reg_range.regno1, regno1)
4582 && match_regno (arg, type, arg->token->u.reg_range.regno2, regno2)
4583 && *regno1 <= *regno2)
4584 {
4585 ++arg->token;
4586 return TRUE;
4587 }
4588 return FALSE;
4589}
4590
a1d78564
RS
4591/* OP_INT matcher. */
4592
a92713e6 4593static bfd_boolean
a1d78564 4594match_int_operand (struct mips_arg_info *arg,
a92713e6 4595 const struct mips_operand *operand_base)
a1d78564
RS
4596{
4597 const struct mips_int_operand *operand;
3ccad066 4598 unsigned int uval;
a1d78564
RS
4599 int min_val, max_val, factor;
4600 offsetT sval;
a1d78564
RS
4601
4602 operand = (const struct mips_int_operand *) operand_base;
4603 factor = 1 << operand->shift;
3ccad066
RS
4604 min_val = mips_int_operand_min (operand);
4605 max_val = mips_int_operand_max (operand);
a1d78564 4606
d436c1c2
RS
4607 if (operand_base->lsb == 0
4608 && operand_base->size == 16
4609 && operand->shift == 0
4610 && operand->bias == 0
4611 && (operand->max_val == 32767 || operand->max_val == 65535))
a1d78564
RS
4612 {
4613 /* The operand can be relocated. */
a92713e6
RS
4614 if (!match_expression (arg, &offset_expr, offset_reloc))
4615 return FALSE;
4616
4617 if (offset_reloc[0] != BFD_RELOC_UNUSED)
a1d78564
RS
4618 /* Relocation operators were used. Accept the arguent and
4619 leave the relocation value in offset_expr and offset_relocs
4620 for the caller to process. */
a92713e6
RS
4621 return TRUE;
4622
4623 if (offset_expr.X_op != O_constant)
a1d78564 4624 {
60f20e8b
RS
4625 /* Accept non-constant operands if no later alternative matches,
4626 leaving it for the caller to process. */
4627 if (!arg->lax_match)
4628 return FALSE;
a92713e6
RS
4629 offset_reloc[0] = BFD_RELOC_LO16;
4630 return TRUE;
a1d78564 4631 }
a92713e6 4632
a1d78564
RS
4633 /* Clear the global state; we're going to install the operand
4634 ourselves. */
a92713e6 4635 sval = offset_expr.X_add_number;
a1d78564 4636 offset_expr.X_op = O_absent;
60f20e8b
RS
4637
4638 /* For compatibility with older assemblers, we accept
4639 0x8000-0xffff as signed 16-bit numbers when only
4640 signed numbers are allowed. */
4641 if (sval > max_val)
4642 {
4643 max_val = ((1 << operand_base->size) - 1) << operand->shift;
4644 if (!arg->lax_match && sval <= max_val)
4645 return FALSE;
4646 }
a1d78564
RS
4647 }
4648 else
4649 {
1a00e612 4650 if (!match_const_int (arg, &sval))
a92713e6 4651 return FALSE;
a1d78564
RS
4652 }
4653
4654 arg->last_op_int = sval;
4655
1a00e612 4656 if (sval < min_val || sval > max_val || sval % factor)
a1d78564 4657 {
1a00e612
RS
4658 match_out_of_range (arg);
4659 return FALSE;
a1d78564
RS
4660 }
4661
4662 uval = (unsigned int) sval >> operand->shift;
4663 uval -= operand->bias;
4664
4665 /* Handle -mfix-cn63xxp1. */
4666 if (arg->opnum == 1
4667 && mips_fix_cn63xxp1
4668 && !mips_opts.micromips
4669 && strcmp ("pref", arg->insn->insn_mo->name) == 0)
4670 switch (uval)
4671 {
4672 case 5:
4673 case 25:
4674 case 26:
4675 case 27:
4676 case 28:
4677 case 29:
4678 case 30:
4679 case 31:
4680 /* These are ok. */
4681 break;
4682
4683 default:
4684 /* The rest must be changed to 28. */
4685 uval = 28;
4686 break;
4687 }
4688
4689 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 4690 return TRUE;
a1d78564
RS
4691}
4692
4693/* OP_MAPPED_INT matcher. */
4694
a92713e6 4695static bfd_boolean
a1d78564 4696match_mapped_int_operand (struct mips_arg_info *arg,
a92713e6 4697 const struct mips_operand *operand_base)
a1d78564
RS
4698{
4699 const struct mips_mapped_int_operand *operand;
4700 unsigned int uval, num_vals;
4701 offsetT sval;
4702
4703 operand = (const struct mips_mapped_int_operand *) operand_base;
1a00e612 4704 if (!match_const_int (arg, &sval))
a92713e6 4705 return FALSE;
a1d78564
RS
4706
4707 num_vals = 1 << operand_base->size;
4708 for (uval = 0; uval < num_vals; uval++)
4709 if (operand->int_map[uval] == sval)
4710 break;
4711 if (uval == num_vals)
1a00e612
RS
4712 {
4713 match_out_of_range (arg);
4714 return FALSE;
4715 }
a1d78564
RS
4716
4717 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 4718 return TRUE;
a1d78564
RS
4719}
4720
4721/* OP_MSB matcher. */
4722
a92713e6 4723static bfd_boolean
a1d78564 4724match_msb_operand (struct mips_arg_info *arg,
a92713e6 4725 const struct mips_operand *operand_base)
a1d78564
RS
4726{
4727 const struct mips_msb_operand *operand;
4728 int min_val, max_val, max_high;
4729 offsetT size, sval, high;
4730
4731 operand = (const struct mips_msb_operand *) operand_base;
4732 min_val = operand->bias;
4733 max_val = min_val + (1 << operand_base->size) - 1;
4734 max_high = operand->opsize;
4735
1a00e612 4736 if (!match_const_int (arg, &size))
a92713e6 4737 return FALSE;
a1d78564
RS
4738
4739 high = size + arg->last_op_int;
4740 sval = operand->add_lsb ? high : size;
4741
4742 if (size < 0 || high > max_high || sval < min_val || sval > max_val)
4743 {
1a00e612
RS
4744 match_out_of_range (arg);
4745 return FALSE;
a1d78564
RS
4746 }
4747 insn_insert_operand (arg->insn, operand_base, sval - min_val);
a92713e6 4748 return TRUE;
a1d78564
RS
4749}
4750
4751/* OP_REG matcher. */
4752
a92713e6 4753static bfd_boolean
a1d78564 4754match_reg_operand (struct mips_arg_info *arg,
a92713e6 4755 const struct mips_operand *operand_base)
a1d78564
RS
4756{
4757 const struct mips_reg_operand *operand;
a92713e6 4758 unsigned int regno, uval, num_vals;
a1d78564
RS
4759
4760 operand = (const struct mips_reg_operand *) operand_base;
a92713e6
RS
4761 if (!match_reg (arg, operand->reg_type, &regno))
4762 return FALSE;
a1d78564
RS
4763
4764 if (operand->reg_map)
4765 {
4766 num_vals = 1 << operand->root.size;
4767 for (uval = 0; uval < num_vals; uval++)
4768 if (operand->reg_map[uval] == regno)
4769 break;
4770 if (num_vals == uval)
a92713e6 4771 return FALSE;
a1d78564
RS
4772 }
4773 else
4774 uval = regno;
4775
a1d78564
RS
4776 arg->last_regno = regno;
4777 if (arg->opnum == 1)
4778 arg->dest_regno = regno;
4779 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 4780 return TRUE;
a1d78564
RS
4781}
4782
4783/* OP_REG_PAIR matcher. */
4784
a92713e6 4785static bfd_boolean
a1d78564 4786match_reg_pair_operand (struct mips_arg_info *arg,
a92713e6 4787 const struct mips_operand *operand_base)
a1d78564
RS
4788{
4789 const struct mips_reg_pair_operand *operand;
a92713e6 4790 unsigned int regno1, regno2, uval, num_vals;
a1d78564
RS
4791
4792 operand = (const struct mips_reg_pair_operand *) operand_base;
a92713e6
RS
4793 if (!match_reg (arg, operand->reg_type, &regno1)
4794 || !match_char (arg, ',')
4795 || !match_reg (arg, operand->reg_type, &regno2))
4796 return FALSE;
a1d78564
RS
4797
4798 num_vals = 1 << operand_base->size;
4799 for (uval = 0; uval < num_vals; uval++)
4800 if (operand->reg1_map[uval] == regno1 && operand->reg2_map[uval] == regno2)
4801 break;
4802 if (uval == num_vals)
a92713e6 4803 return FALSE;
a1d78564 4804
a1d78564 4805 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 4806 return TRUE;
a1d78564
RS
4807}
4808
4809/* OP_PCREL matcher. The caller chooses the relocation type. */
4810
a92713e6
RS
4811static bfd_boolean
4812match_pcrel_operand (struct mips_arg_info *arg)
a1d78564 4813{
a92713e6
RS
4814 bfd_reloc_code_real_type r[3];
4815
4816 return match_expression (arg, &offset_expr, r) && r[0] == BFD_RELOC_UNUSED;
a1d78564
RS
4817}
4818
4819/* OP_PERF_REG matcher. */
4820
a92713e6 4821static bfd_boolean
a1d78564 4822match_perf_reg_operand (struct mips_arg_info *arg,
a92713e6 4823 const struct mips_operand *operand)
a1d78564
RS
4824{
4825 offsetT sval;
4826
1a00e612 4827 if (!match_const_int (arg, &sval))
a92713e6 4828 return FALSE;
a1d78564
RS
4829
4830 if (sval != 0
4831 && (sval != 1
4832 || (mips_opts.arch == CPU_R5900
4833 && (strcmp (arg->insn->insn_mo->name, "mfps") == 0
4834 || strcmp (arg->insn->insn_mo->name, "mtps") == 0))))
4835 {
1a00e612
RS
4836 set_insn_error (arg->argnum, _("invalid performance register"));
4837 return FALSE;
a1d78564
RS
4838 }
4839
4840 insn_insert_operand (arg->insn, operand, sval);
a92713e6 4841 return TRUE;
a1d78564
RS
4842}
4843
4844/* OP_ADDIUSP matcher. */
4845
a92713e6 4846static bfd_boolean
a1d78564 4847match_addiusp_operand (struct mips_arg_info *arg,
a92713e6 4848 const struct mips_operand *operand)
a1d78564
RS
4849{
4850 offsetT sval;
4851 unsigned int uval;
4852
1a00e612 4853 if (!match_const_int (arg, &sval))
a92713e6 4854 return FALSE;
a1d78564
RS
4855
4856 if (sval % 4)
1a00e612
RS
4857 {
4858 match_out_of_range (arg);
4859 return FALSE;
4860 }
a1d78564
RS
4861
4862 sval /= 4;
4863 if (!(sval >= -258 && sval <= 257) || (sval >= -2 && sval <= 1))
1a00e612
RS
4864 {
4865 match_out_of_range (arg);
4866 return FALSE;
4867 }
a1d78564
RS
4868
4869 uval = (unsigned int) sval;
4870 uval = ((uval >> 1) & ~0xff) | (uval & 0xff);
4871 insn_insert_operand (arg->insn, operand, uval);
a92713e6 4872 return TRUE;
a1d78564
RS
4873}
4874
4875/* OP_CLO_CLZ_DEST matcher. */
4876
a92713e6 4877static bfd_boolean
a1d78564 4878match_clo_clz_dest_operand (struct mips_arg_info *arg,
a92713e6 4879 const struct mips_operand *operand)
a1d78564
RS
4880{
4881 unsigned int regno;
4882
a92713e6
RS
4883 if (!match_reg (arg, OP_REG_GP, &regno))
4884 return FALSE;
a1d78564 4885
a1d78564 4886 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
a92713e6 4887 return TRUE;
a1d78564
RS
4888}
4889
4890/* OP_LWM_SWM_LIST matcher. */
4891
a92713e6 4892static bfd_boolean
a1d78564 4893match_lwm_swm_list_operand (struct mips_arg_info *arg,
a92713e6 4894 const struct mips_operand *operand)
a1d78564 4895{
a92713e6
RS
4896 unsigned int reglist, sregs, ra, regno1, regno2;
4897 struct mips_arg_info reset;
a1d78564 4898
a92713e6
RS
4899 reglist = 0;
4900 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
4901 return FALSE;
4902 do
4903 {
4904 if (regno2 == FP && regno1 >= S0 && regno1 <= S7)
4905 {
4906 reglist |= 1 << FP;
4907 regno2 = S7;
4908 }
4909 reglist |= ((1U << regno2 << 1) - 1) & -(1U << regno1);
4910 reset = *arg;
4911 }
4912 while (match_char (arg, ',')
4913 && match_reg_range (arg, OP_REG_GP, &regno1, &regno2));
4914 *arg = reset;
a1d78564
RS
4915
4916 if (operand->size == 2)
4917 {
4918 /* The list must include both ra and s0-sN, for 0 <= N <= 3. E.g.:
4919
4920 s0, ra
4921 s0, s1, ra, s2, s3
4922 s0-s2, ra
4923
4924 and any permutations of these. */
4925 if ((reglist & 0xfff1ffff) != 0x80010000)
a92713e6 4926 return FALSE;
a1d78564
RS
4927
4928 sregs = (reglist >> 17) & 7;
4929 ra = 0;
4930 }
4931 else
4932 {
4933 /* The list must include at least one of ra and s0-sN,
4934 for 0 <= N <= 8. (Note that there is a gap between s7 and s8,
4935 which are $23 and $30 respectively.) E.g.:
4936
4937 ra
4938 s0
4939 ra, s0, s1, s2
4940 s0-s8
4941 s0-s5, ra
4942
4943 and any permutations of these. */
4944 if ((reglist & 0x3f00ffff) != 0)
a92713e6 4945 return FALSE;
a1d78564
RS
4946
4947 ra = (reglist >> 27) & 0x10;
4948 sregs = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
4949 }
4950 sregs += 1;
4951 if ((sregs & -sregs) != sregs)
a92713e6 4952 return FALSE;
a1d78564
RS
4953
4954 insn_insert_operand (arg->insn, operand, (ffs (sregs) - 1) | ra);
a92713e6 4955 return TRUE;
a1d78564
RS
4956}
4957
364215c8
RS
4958/* OP_ENTRY_EXIT_LIST matcher. */
4959
a92713e6 4960static unsigned int
364215c8 4961match_entry_exit_operand (struct mips_arg_info *arg,
a92713e6 4962 const struct mips_operand *operand)
364215c8
RS
4963{
4964 unsigned int mask;
4965 bfd_boolean is_exit;
4966
4967 /* The format is the same for both ENTRY and EXIT, but the constraints
4968 are different. */
4969 is_exit = strcmp (arg->insn->insn_mo->name, "exit") == 0;
4970 mask = (is_exit ? 7 << 3 : 0);
a92713e6 4971 do
364215c8
RS
4972 {
4973 unsigned int regno1, regno2;
4974 bfd_boolean is_freg;
4975
a92713e6 4976 if (match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
364215c8 4977 is_freg = FALSE;
a92713e6 4978 else if (match_reg_range (arg, OP_REG_FP, &regno1, &regno2))
364215c8
RS
4979 is_freg = TRUE;
4980 else
a92713e6 4981 return FALSE;
364215c8
RS
4982
4983 if (is_exit && is_freg && regno1 == 0 && regno2 < 2)
4984 {
4985 mask &= ~(7 << 3);
4986 mask |= (5 + regno2) << 3;
4987 }
4988 else if (!is_exit && regno1 == 4 && regno2 >= 4 && regno2 <= 7)
4989 mask |= (regno2 - 3) << 3;
4990 else if (regno1 == 16 && regno2 >= 16 && regno2 <= 17)
4991 mask |= (regno2 - 15) << 1;
4992 else if (regno1 == RA && regno2 == RA)
4993 mask |= 1;
4994 else
a92713e6 4995 return FALSE;
364215c8 4996 }
a92713e6
RS
4997 while (match_char (arg, ','));
4998
364215c8 4999 insn_insert_operand (arg->insn, operand, mask);
a92713e6 5000 return TRUE;
364215c8
RS
5001}
5002
5003/* OP_SAVE_RESTORE_LIST matcher. */
5004
a92713e6
RS
5005static bfd_boolean
5006match_save_restore_list_operand (struct mips_arg_info *arg)
364215c8
RS
5007{
5008 unsigned int opcode, args, statics, sregs;
5009 unsigned int num_frame_sizes, num_args, num_statics, num_sregs;
364215c8 5010 offsetT frame_size;
364215c8 5011
364215c8
RS
5012 opcode = arg->insn->insn_opcode;
5013 frame_size = 0;
5014 num_frame_sizes = 0;
5015 args = 0;
5016 statics = 0;
5017 sregs = 0;
a92713e6 5018 do
364215c8
RS
5019 {
5020 unsigned int regno1, regno2;
5021
a92713e6 5022 if (arg->token->type == OT_INTEGER)
364215c8
RS
5023 {
5024 /* Handle the frame size. */
1a00e612 5025 if (!match_const_int (arg, &frame_size))
a92713e6 5026 return FALSE;
364215c8 5027 num_frame_sizes += 1;
364215c8
RS
5028 }
5029 else
5030 {
a92713e6
RS
5031 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5032 return FALSE;
364215c8
RS
5033
5034 while (regno1 <= regno2)
5035 {
5036 if (regno1 >= 4 && regno1 <= 7)
5037 {
5038 if (num_frame_sizes == 0)
5039 /* args $a0-$a3 */
5040 args |= 1 << (regno1 - 4);
5041 else
5042 /* statics $a0-$a3 */
5043 statics |= 1 << (regno1 - 4);
5044 }
5045 else if (regno1 >= 16 && regno1 <= 23)
5046 /* $s0-$s7 */
5047 sregs |= 1 << (regno1 - 16);
5048 else if (regno1 == 30)
5049 /* $s8 */
5050 sregs |= 1 << 8;
5051 else if (regno1 == 31)
5052 /* Add $ra to insn. */
5053 opcode |= 0x40;
5054 else
a92713e6 5055 return FALSE;
364215c8
RS
5056 regno1 += 1;
5057 if (regno1 == 24)
5058 regno1 = 30;
5059 }
5060 }
364215c8 5061 }
a92713e6 5062 while (match_char (arg, ','));
364215c8
RS
5063
5064 /* Encode args/statics combination. */
5065 if (args & statics)
a92713e6 5066 return FALSE;
364215c8
RS
5067 else if (args == 0xf)
5068 /* All $a0-$a3 are args. */
5069 opcode |= MIPS16_ALL_ARGS << 16;
5070 else if (statics == 0xf)
5071 /* All $a0-$a3 are statics. */
5072 opcode |= MIPS16_ALL_STATICS << 16;
5073 else
5074 {
5075 /* Count arg registers. */
5076 num_args = 0;
5077 while (args & 0x1)
5078 {
5079 args >>= 1;
5080 num_args += 1;
5081 }
5082 if (args != 0)
a92713e6 5083 return FALSE;
364215c8
RS
5084
5085 /* Count static registers. */
5086 num_statics = 0;
5087 while (statics & 0x8)
5088 {
5089 statics = (statics << 1) & 0xf;
5090 num_statics += 1;
5091 }
5092 if (statics != 0)
a92713e6 5093 return FALSE;
364215c8
RS
5094
5095 /* Encode args/statics. */
5096 opcode |= ((num_args << 2) | num_statics) << 16;
5097 }
5098
5099 /* Encode $s0/$s1. */
5100 if (sregs & (1 << 0)) /* $s0 */
5101 opcode |= 0x20;
5102 if (sregs & (1 << 1)) /* $s1 */
5103 opcode |= 0x10;
5104 sregs >>= 2;
5105
5106 /* Encode $s2-$s8. */
5107 num_sregs = 0;
5108 while (sregs & 1)
5109 {
5110 sregs >>= 1;
5111 num_sregs += 1;
5112 }
5113 if (sregs != 0)
a92713e6 5114 return FALSE;
364215c8
RS
5115 opcode |= num_sregs << 24;
5116
5117 /* Encode frame size. */
5118 if (num_frame_sizes == 0)
1a00e612
RS
5119 {
5120 set_insn_error (arg->argnum, _("missing frame size"));
5121 return FALSE;
5122 }
5123 if (num_frame_sizes > 1)
5124 {
5125 set_insn_error (arg->argnum, _("frame size specified twice"));
5126 return FALSE;
5127 }
5128 if ((frame_size & 7) != 0 || frame_size < 0 || frame_size > 0xff * 8)
5129 {
5130 set_insn_error (arg->argnum, _("invalid frame size"));
5131 return FALSE;
5132 }
5133 if (frame_size != 128 || (opcode >> 16) != 0)
364215c8
RS
5134 {
5135 frame_size /= 8;
5136 opcode |= (((frame_size & 0xf0) << 16)
5137 | (frame_size & 0x0f));
5138 }
5139
364215c8
RS
5140 /* Finally build the instruction. */
5141 if ((opcode >> 16) != 0 || frame_size == 0)
5142 opcode |= MIPS16_EXTEND;
5143 arg->insn->insn_opcode = opcode;
a92713e6 5144 return TRUE;
364215c8
RS
5145}
5146
a1d78564
RS
5147/* OP_MDMX_IMM_REG matcher. */
5148
a92713e6 5149static bfd_boolean
a1d78564 5150match_mdmx_imm_reg_operand (struct mips_arg_info *arg,
a92713e6 5151 const struct mips_operand *operand)
a1d78564 5152{
a92713e6 5153 unsigned int regno, uval;
a1d78564
RS
5154 bfd_boolean is_qh;
5155 const struct mips_opcode *opcode;
5156
5157 /* The mips_opcode records whether this is an octobyte or quadhalf
5158 instruction. Start out with that bit in place. */
5159 opcode = arg->insn->insn_mo;
5160 uval = mips_extract_operand (operand, opcode->match);
5161 is_qh = (uval != 0);
5162
56d438b1 5163 if (arg->token->type == OT_REG)
a1d78564
RS
5164 {
5165 if ((opcode->membership & INSN_5400)
5166 && strcmp (opcode->name, "rzu.ob") == 0)
5167 {
1a00e612
RS
5168 set_insn_error_i (arg->argnum, _("operand %d must be an immediate"),
5169 arg->argnum);
5170 return FALSE;
a1d78564
RS
5171 }
5172
56d438b1
CF
5173 if (!match_regno (arg, OP_REG_VEC, arg->token->u.regno, &regno))
5174 return FALSE;
5175 ++arg->token;
5176
a1d78564
RS
5177 /* Check whether this is a vector register or a broadcast of
5178 a single element. */
56d438b1 5179 if (arg->token->type == OT_INTEGER_INDEX)
a1d78564 5180 {
56d438b1 5181 if (arg->token->u.index > (is_qh ? 3 : 7))
a1d78564 5182 {
1a00e612
RS
5183 set_insn_error (arg->argnum, _("invalid element selector"));
5184 return FALSE;
a1d78564 5185 }
56d438b1
CF
5186 uval |= arg->token->u.index << (is_qh ? 2 : 1) << 5;
5187 ++arg->token;
a1d78564
RS
5188 }
5189 else
5190 {
5191 /* A full vector. */
5192 if ((opcode->membership & INSN_5400)
5193 && (strcmp (opcode->name, "sll.ob") == 0
5194 || strcmp (opcode->name, "srl.ob") == 0))
5195 {
1a00e612
RS
5196 set_insn_error_i (arg->argnum, _("operand %d must be scalar"),
5197 arg->argnum);
5198 return FALSE;
a1d78564
RS
5199 }
5200
5201 if (is_qh)
5202 uval |= MDMX_FMTSEL_VEC_QH << 5;
5203 else
5204 uval |= MDMX_FMTSEL_VEC_OB << 5;
5205 }
a1d78564
RS
5206 uval |= regno;
5207 }
5208 else
5209 {
5210 offsetT sval;
5211
1a00e612 5212 if (!match_const_int (arg, &sval))
a92713e6 5213 return FALSE;
a1d78564
RS
5214 if (sval < 0 || sval > 31)
5215 {
1a00e612
RS
5216 match_out_of_range (arg);
5217 return FALSE;
a1d78564
RS
5218 }
5219 uval |= (sval & 31);
5220 if (is_qh)
5221 uval |= MDMX_FMTSEL_IMM_QH << 5;
5222 else
5223 uval |= MDMX_FMTSEL_IMM_OB << 5;
5224 }
5225 insn_insert_operand (arg->insn, operand, uval);
a92713e6 5226 return TRUE;
a1d78564
RS
5227}
5228
56d438b1
CF
5229/* OP_IMM_INDEX matcher. */
5230
5231static bfd_boolean
5232match_imm_index_operand (struct mips_arg_info *arg,
5233 const struct mips_operand *operand)
5234{
5235 unsigned int max_val;
5236
5237 if (arg->token->type != OT_INTEGER_INDEX)
5238 return FALSE;
5239
5240 max_val = (1 << operand->size) - 1;
5241 if (arg->token->u.index > max_val)
5242 {
5243 match_out_of_range (arg);
5244 return FALSE;
5245 }
5246 insn_insert_operand (arg->insn, operand, arg->token->u.index);
5247 ++arg->token;
5248 return TRUE;
5249}
5250
5251/* OP_REG_INDEX matcher. */
5252
5253static bfd_boolean
5254match_reg_index_operand (struct mips_arg_info *arg,
5255 const struct mips_operand *operand)
5256{
5257 unsigned int regno;
5258
5259 if (arg->token->type != OT_REG_INDEX)
5260 return FALSE;
5261
5262 if (!match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno))
5263 return FALSE;
5264
5265 insn_insert_operand (arg->insn, operand, regno);
5266 ++arg->token;
5267 return TRUE;
5268}
5269
a1d78564
RS
5270/* OP_PC matcher. */
5271
a92713e6
RS
5272static bfd_boolean
5273match_pc_operand (struct mips_arg_info *arg)
a1d78564 5274{
a92713e6
RS
5275 if (arg->token->type == OT_REG && (arg->token->u.regno & RTYPE_PC))
5276 {
5277 ++arg->token;
5278 return TRUE;
5279 }
5280 return FALSE;
a1d78564
RS
5281}
5282
5283/* OP_REPEAT_DEST_REG and OP_REPEAT_PREV_REG matcher. OTHER_REGNO is the
5284 register that we need to match. */
5285
a92713e6
RS
5286static bfd_boolean
5287match_tied_reg_operand (struct mips_arg_info *arg, unsigned int other_regno)
a1d78564
RS
5288{
5289 unsigned int regno;
5290
a92713e6 5291 return match_reg (arg, OP_REG_GP, &regno) && regno == other_regno;
a1d78564
RS
5292}
5293
89565f1b
RS
5294/* Read a floating-point constant from S for LI.S or LI.D. LENGTH is
5295 the length of the value in bytes (4 for float, 8 for double) and
5296 USING_GPRS says whether the destination is a GPR rather than an FPR.
5297
5298 Return the constant in IMM and OFFSET as follows:
5299
5300 - If the constant should be loaded via memory, set IMM to O_absent and
5301 OFFSET to the memory address.
5302
5303 - Otherwise, if the constant should be loaded into two 32-bit registers,
5304 set IMM to the O_constant to load into the high register and OFFSET
5305 to the corresponding value for the low register.
5306
5307 - Otherwise, set IMM to the full O_constant and set OFFSET to O_absent.
5308
5309 These constants only appear as the last operand in an instruction,
5310 and every instruction that accepts them in any variant accepts them
5311 in all variants. This means we don't have to worry about backing out
5312 any changes if the instruction does not match. We just match
5313 unconditionally and report an error if the constant is invalid. */
5314
a92713e6
RS
5315static bfd_boolean
5316match_float_constant (struct mips_arg_info *arg, expressionS *imm,
5317 expressionS *offset, int length, bfd_boolean using_gprs)
89565f1b 5318{
a92713e6 5319 char *p;
89565f1b
RS
5320 segT seg, new_seg;
5321 subsegT subseg;
5322 const char *newname;
a92713e6 5323 unsigned char *data;
89565f1b
RS
5324
5325 /* Where the constant is placed is based on how the MIPS assembler
5326 does things:
5327
5328 length == 4 && using_gprs -- immediate value only
5329 length == 8 && using_gprs -- .rdata or immediate value
5330 length == 4 && !using_gprs -- .lit4 or immediate value
5331 length == 8 && !using_gprs -- .lit8 or immediate value
5332
5333 The .lit4 and .lit8 sections are only used if permitted by the
5334 -G argument. */
a92713e6 5335 if (arg->token->type != OT_FLOAT)
1a00e612
RS
5336 {
5337 set_insn_error (arg->argnum, _("floating-point expression required"));
5338 return FALSE;
5339 }
a92713e6
RS
5340
5341 gas_assert (arg->token->u.flt.length == length);
5342 data = arg->token->u.flt.data;
5343 ++arg->token;
89565f1b
RS
5344
5345 /* Handle 32-bit constants for which an immediate value is best. */
5346 if (length == 4
5347 && (using_gprs
5348 || g_switch_value < 4
5349 || (data[0] == 0 && data[1] == 0)
5350 || (data[2] == 0 && data[3] == 0)))
5351 {
5352 imm->X_op = O_constant;
5353 if (!target_big_endian)
5354 imm->X_add_number = bfd_getl32 (data);
5355 else
5356 imm->X_add_number = bfd_getb32 (data);
5357 offset->X_op = O_absent;
a92713e6 5358 return TRUE;
89565f1b
RS
5359 }
5360
5361 /* Handle 64-bit constants for which an immediate value is best. */
5362 if (length == 8
5363 && !mips_disable_float_construction
5364 /* Constants can only be constructed in GPRs and copied
5365 to FPRs if the GPRs are at least as wide as the FPRs.
5366 Force the constant into memory if we are using 64-bit FPRs
5367 but the GPRs are only 32 bits wide. */
5368 /* ??? No longer true with the addition of MTHC1, but this
5369 is legacy code... */
5370 && (using_gprs || !(HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
5371 && ((data[0] == 0 && data[1] == 0)
5372 || (data[2] == 0 && data[3] == 0))
5373 && ((data[4] == 0 && data[5] == 0)
5374 || (data[6] == 0 && data[7] == 0)))
5375 {
5376 /* The value is simple enough to load with a couple of instructions.
5377 If using 32-bit registers, set IMM to the high order 32 bits and
5378 OFFSET to the low order 32 bits. Otherwise, set IMM to the entire
5379 64 bit constant. */
5380 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
5381 {
5382 imm->X_op = O_constant;
5383 offset->X_op = O_constant;
5384 if (!target_big_endian)
5385 {
5386 imm->X_add_number = bfd_getl32 (data + 4);
5387 offset->X_add_number = bfd_getl32 (data);
5388 }
5389 else
5390 {
5391 imm->X_add_number = bfd_getb32 (data);
5392 offset->X_add_number = bfd_getb32 (data + 4);
5393 }
5394 if (offset->X_add_number == 0)
5395 offset->X_op = O_absent;
5396 }
5397 else
5398 {
5399 imm->X_op = O_constant;
5400 if (!target_big_endian)
5401 imm->X_add_number = bfd_getl64 (data);
5402 else
5403 imm->X_add_number = bfd_getb64 (data);
5404 offset->X_op = O_absent;
5405 }
a92713e6 5406 return TRUE;
89565f1b
RS
5407 }
5408
5409 /* Switch to the right section. */
5410 seg = now_seg;
5411 subseg = now_subseg;
5412 if (length == 4)
5413 {
5414 gas_assert (!using_gprs && g_switch_value >= 4);
5415 newname = ".lit4";
5416 }
5417 else
5418 {
5419 if (using_gprs || g_switch_value < 8)
5420 newname = RDATA_SECTION_NAME;
5421 else
5422 newname = ".lit8";
5423 }
5424
5425 new_seg = subseg_new (newname, (subsegT) 0);
5426 bfd_set_section_flags (stdoutput, new_seg,
5427 SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA);
5428 frag_align (length == 4 ? 2 : 3, 0, 0);
5429 if (strncmp (TARGET_OS, "elf", 3) != 0)
5430 record_alignment (new_seg, 4);
5431 else
5432 record_alignment (new_seg, length == 4 ? 2 : 3);
5433 if (seg == now_seg)
1661c76c 5434 as_bad (_("cannot use `%s' in this section"), arg->insn->insn_mo->name);
89565f1b
RS
5435
5436 /* Set the argument to the current address in the section. */
5437 imm->X_op = O_absent;
5438 offset->X_op = O_symbol;
5439 offset->X_add_symbol = symbol_temp_new_now ();
5440 offset->X_add_number = 0;
5441
5442 /* Put the floating point number into the section. */
5443 p = frag_more (length);
5444 memcpy (p, data, length);
5445
5446 /* Switch back to the original section. */
5447 subseg_set (seg, subseg);
a92713e6 5448 return TRUE;
89565f1b
RS
5449}
5450
14daeee3
RS
5451/* OP_VU0_SUFFIX and OP_VU0_MATCH_SUFFIX matcher; MATCH_P selects between
5452 them. */
5453
5454static bfd_boolean
5455match_vu0_suffix_operand (struct mips_arg_info *arg,
5456 const struct mips_operand *operand,
5457 bfd_boolean match_p)
5458{
5459 unsigned int uval;
5460
5461 /* The operand can be an XYZW mask or a single 2-bit channel index
5462 (with X being 0). */
5463 gas_assert (operand->size == 2 || operand->size == 4);
5464
ee5734f0 5465 /* The suffix can be omitted when it is already part of the opcode. */
14daeee3 5466 if (arg->token->type != OT_CHANNELS)
ee5734f0 5467 return match_p;
14daeee3
RS
5468
5469 uval = arg->token->u.channels;
5470 if (operand->size == 2)
5471 {
5472 /* Check that a single bit is set and convert it into a 2-bit index. */
5473 if ((uval & -uval) != uval)
5474 return FALSE;
5475 uval = 4 - ffs (uval);
5476 }
5477
5478 if (match_p && insn_extract_operand (arg->insn, operand) != uval)
5479 return FALSE;
5480
5481 ++arg->token;
5482 if (!match_p)
5483 insn_insert_operand (arg->insn, operand, uval);
5484 return TRUE;
5485}
5486
a1d78564
RS
5487/* S is the text seen for ARG. Match it against OPERAND. Return the end
5488 of the argument text if the match is successful, otherwise return null. */
5489
a92713e6 5490static bfd_boolean
a1d78564 5491match_operand (struct mips_arg_info *arg,
a92713e6 5492 const struct mips_operand *operand)
a1d78564
RS
5493{
5494 switch (operand->type)
5495 {
5496 case OP_INT:
a92713e6 5497 return match_int_operand (arg, operand);
a1d78564
RS
5498
5499 case OP_MAPPED_INT:
a92713e6 5500 return match_mapped_int_operand (arg, operand);
a1d78564
RS
5501
5502 case OP_MSB:
a92713e6 5503 return match_msb_operand (arg, operand);
a1d78564
RS
5504
5505 case OP_REG:
0f35dbc4 5506 case OP_OPTIONAL_REG:
a92713e6 5507 return match_reg_operand (arg, operand);
a1d78564
RS
5508
5509 case OP_REG_PAIR:
a92713e6 5510 return match_reg_pair_operand (arg, operand);
a1d78564
RS
5511
5512 case OP_PCREL:
a92713e6 5513 return match_pcrel_operand (arg);
a1d78564
RS
5514
5515 case OP_PERF_REG:
a92713e6 5516 return match_perf_reg_operand (arg, operand);
a1d78564
RS
5517
5518 case OP_ADDIUSP_INT:
a92713e6 5519 return match_addiusp_operand (arg, operand);
a1d78564
RS
5520
5521 case OP_CLO_CLZ_DEST:
a92713e6 5522 return match_clo_clz_dest_operand (arg, operand);
a1d78564
RS
5523
5524 case OP_LWM_SWM_LIST:
a92713e6 5525 return match_lwm_swm_list_operand (arg, operand);
a1d78564
RS
5526
5527 case OP_ENTRY_EXIT_LIST:
a92713e6 5528 return match_entry_exit_operand (arg, operand);
364215c8 5529
a1d78564 5530 case OP_SAVE_RESTORE_LIST:
a92713e6 5531 return match_save_restore_list_operand (arg);
a1d78564
RS
5532
5533 case OP_MDMX_IMM_REG:
a92713e6 5534 return match_mdmx_imm_reg_operand (arg, operand);
a1d78564
RS
5535
5536 case OP_REPEAT_DEST_REG:
a92713e6 5537 return match_tied_reg_operand (arg, arg->dest_regno);
a1d78564
RS
5538
5539 case OP_REPEAT_PREV_REG:
a92713e6 5540 return match_tied_reg_operand (arg, arg->last_regno);
a1d78564
RS
5541
5542 case OP_PC:
a92713e6 5543 return match_pc_operand (arg);
14daeee3
RS
5544
5545 case OP_VU0_SUFFIX:
5546 return match_vu0_suffix_operand (arg, operand, FALSE);
5547
5548 case OP_VU0_MATCH_SUFFIX:
5549 return match_vu0_suffix_operand (arg, operand, TRUE);
56d438b1
CF
5550
5551 case OP_IMM_INDEX:
5552 return match_imm_index_operand (arg, operand);
5553
5554 case OP_REG_INDEX:
5555 return match_reg_index_operand (arg, operand);
a1d78564
RS
5556 }
5557 abort ();
5558}
5559
5560/* ARG is the state after successfully matching an instruction.
5561 Issue any queued-up warnings. */
5562
5563static void
5564check_completed_insn (struct mips_arg_info *arg)
5565{
5566 if (arg->seen_at)
5567 {
5568 if (AT == ATREG)
1661c76c 5569 as_warn (_("used $at without \".set noat\""));
a1d78564 5570 else
1661c76c 5571 as_warn (_("used $%u with \".set at=$%u\""), AT, AT);
a1d78564
RS
5572 }
5573}
a1d78564 5574
85fcb30f
RS
5575/* Return true if modifying general-purpose register REG needs a delay. */
5576
5577static bfd_boolean
5578reg_needs_delay (unsigned int reg)
5579{
5580 unsigned long prev_pinfo;
5581
5582 prev_pinfo = history[0].insn_mo->pinfo;
5583 if (!mips_opts.noreorder
67dc82bc 5584 && (((prev_pinfo & INSN_LOAD_MEMORY) && !gpr_interlocks)
85fcb30f
RS
5585 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY) && !cop_interlocks))
5586 && (gpr_write_mask (&history[0]) & (1 << reg)))
5587 return TRUE;
5588
5589 return FALSE;
5590}
5591
71400594
RS
5592/* Classify an instruction according to the FIX_VR4120_* enumeration.
5593 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
5594 by VR4120 errata. */
4d7206a2 5595
71400594
RS
5596static unsigned int
5597classify_vr4120_insn (const char *name)
252b5132 5598{
71400594
RS
5599 if (strncmp (name, "macc", 4) == 0)
5600 return FIX_VR4120_MACC;
5601 if (strncmp (name, "dmacc", 5) == 0)
5602 return FIX_VR4120_DMACC;
5603 if (strncmp (name, "mult", 4) == 0)
5604 return FIX_VR4120_MULT;
5605 if (strncmp (name, "dmult", 5) == 0)
5606 return FIX_VR4120_DMULT;
5607 if (strstr (name, "div"))
5608 return FIX_VR4120_DIV;
5609 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
5610 return FIX_VR4120_MTHILO;
5611 return NUM_FIX_VR4120_CLASSES;
5612}
252b5132 5613
a8d14a88
CM
5614#define INSN_ERET 0x42000018
5615#define INSN_DERET 0x4200001f
5616#define INSN_DMULT 0x1c
5617#define INSN_DMULTU 0x1d
ff239038 5618
71400594
RS
5619/* Return the number of instructions that must separate INSN1 and INSN2,
5620 where INSN1 is the earlier instruction. Return the worst-case value
5621 for any INSN2 if INSN2 is null. */
252b5132 5622
71400594
RS
5623static unsigned int
5624insns_between (const struct mips_cl_insn *insn1,
5625 const struct mips_cl_insn *insn2)
5626{
5627 unsigned long pinfo1, pinfo2;
4c260379 5628 unsigned int mask;
71400594 5629
85fcb30f
RS
5630 /* If INFO2 is null, pessimistically assume that all flags are set for
5631 the second instruction. */
71400594
RS
5632 pinfo1 = insn1->insn_mo->pinfo;
5633 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
252b5132 5634
71400594
RS
5635 /* For most targets, write-after-read dependencies on the HI and LO
5636 registers must be separated by at least two instructions. */
5637 if (!hilo_interlocks)
252b5132 5638 {
71400594
RS
5639 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
5640 return 2;
5641 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
5642 return 2;
5643 }
5644
5645 /* If we're working around r7000 errata, there must be two instructions
5646 between an mfhi or mflo and any instruction that uses the result. */
5647 if (mips_7000_hilo_fix
df58fc94 5648 && !mips_opts.micromips
71400594 5649 && MF_HILO_INSN (pinfo1)
85fcb30f 5650 && (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1))))
71400594
RS
5651 return 2;
5652
ff239038
CM
5653 /* If we're working around 24K errata, one instruction is required
5654 if an ERET or DERET is followed by a branch instruction. */
df58fc94 5655 if (mips_fix_24k && !mips_opts.micromips)
ff239038
CM
5656 {
5657 if (insn1->insn_opcode == INSN_ERET
5658 || insn1->insn_opcode == INSN_DERET)
5659 {
5660 if (insn2 == NULL
5661 || insn2->insn_opcode == INSN_ERET
5662 || insn2->insn_opcode == INSN_DERET
11625dd8 5663 || delayed_branch_p (insn2))
ff239038
CM
5664 return 1;
5665 }
5666 }
5667
a8d14a88
CM
5668 /* If we're working around PMC RM7000 errata, there must be three
5669 nops between a dmult and a load instruction. */
5670 if (mips_fix_rm7000 && !mips_opts.micromips)
5671 {
5672 if ((insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULT
5673 || (insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULTU)
5674 {
5675 if (pinfo2 & INSN_LOAD_MEMORY)
5676 return 3;
5677 }
5678 }
5679
71400594
RS
5680 /* If working around VR4120 errata, check for combinations that need
5681 a single intervening instruction. */
df58fc94 5682 if (mips_fix_vr4120 && !mips_opts.micromips)
71400594
RS
5683 {
5684 unsigned int class1, class2;
252b5132 5685
71400594
RS
5686 class1 = classify_vr4120_insn (insn1->insn_mo->name);
5687 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
252b5132 5688 {
71400594
RS
5689 if (insn2 == NULL)
5690 return 1;
5691 class2 = classify_vr4120_insn (insn2->insn_mo->name);
5692 if (vr4120_conflicts[class1] & (1 << class2))
5693 return 1;
252b5132 5694 }
71400594
RS
5695 }
5696
df58fc94 5697 if (!HAVE_CODE_COMPRESSION)
71400594
RS
5698 {
5699 /* Check for GPR or coprocessor load delays. All such delays
5700 are on the RT register. */
5701 /* Itbl support may require additional care here. */
67dc82bc 5702 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY))
71400594 5703 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
252b5132 5704 {
85fcb30f 5705 if (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1)))
71400594
RS
5706 return 1;
5707 }
5708
5709 /* Check for generic coprocessor hazards.
5710
5711 This case is not handled very well. There is no special
5712 knowledge of CP0 handling, and the coprocessors other than
5713 the floating point unit are not distinguished at all. */
5714 /* Itbl support may require additional care here. FIXME!
5715 Need to modify this to include knowledge about
5716 user specified delays! */
5717 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
5718 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
5719 {
5720 /* Handle cases where INSN1 writes to a known general coprocessor
5721 register. There must be a one instruction delay before INSN2
5722 if INSN2 reads that register, otherwise no delay is needed. */
4c260379
RS
5723 mask = fpr_write_mask (insn1);
5724 if (mask != 0)
252b5132 5725 {
4c260379 5726 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
71400594 5727 return 1;
252b5132
RH
5728 }
5729 else
5730 {
71400594
RS
5731 /* Read-after-write dependencies on the control registers
5732 require a two-instruction gap. */
5733 if ((pinfo1 & INSN_WRITE_COND_CODE)
5734 && (pinfo2 & INSN_READ_COND_CODE))
5735 return 2;
5736
5737 /* We don't know exactly what INSN1 does. If INSN2 is
5738 also a coprocessor instruction, assume there must be
5739 a one instruction gap. */
5740 if (pinfo2 & INSN_COP)
5741 return 1;
252b5132
RH
5742 }
5743 }
6b76fefe 5744
71400594
RS
5745 /* Check for read-after-write dependencies on the coprocessor
5746 control registers in cases where INSN1 does not need a general
5747 coprocessor delay. This means that INSN1 is a floating point
5748 comparison instruction. */
5749 /* Itbl support may require additional care here. */
5750 else if (!cop_interlocks
5751 && (pinfo1 & INSN_WRITE_COND_CODE)
5752 && (pinfo2 & INSN_READ_COND_CODE))
5753 return 1;
5754 }
6b76fefe 5755
71400594
RS
5756 return 0;
5757}
6b76fefe 5758
7d8e00cf
RS
5759/* Return the number of nops that would be needed to work around the
5760 VR4130 mflo/mfhi errata if instruction INSN immediately followed
932d1a1b
RS
5761 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
5762 that are contained within the first IGNORE instructions of HIST. */
7d8e00cf
RS
5763
5764static int
932d1a1b 5765nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
7d8e00cf
RS
5766 const struct mips_cl_insn *insn)
5767{
4c260379
RS
5768 int i, j;
5769 unsigned int mask;
7d8e00cf
RS
5770
5771 /* Check if the instruction writes to HI or LO. MTHI and MTLO
5772 are not affected by the errata. */
5773 if (insn != 0
5774 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
5775 || strcmp (insn->insn_mo->name, "mtlo") == 0
5776 || strcmp (insn->insn_mo->name, "mthi") == 0))
5777 return 0;
5778
5779 /* Search for the first MFLO or MFHI. */
5780 for (i = 0; i < MAX_VR4130_NOPS; i++)
91d6fa6a 5781 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
7d8e00cf
RS
5782 {
5783 /* Extract the destination register. */
4c260379 5784 mask = gpr_write_mask (&hist[i]);
7d8e00cf
RS
5785
5786 /* No nops are needed if INSN reads that register. */
4c260379 5787 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
7d8e00cf
RS
5788 return 0;
5789
5790 /* ...or if any of the intervening instructions do. */
5791 for (j = 0; j < i; j++)
4c260379 5792 if (gpr_read_mask (&hist[j]) & mask)
7d8e00cf
RS
5793 return 0;
5794
932d1a1b
RS
5795 if (i >= ignore)
5796 return MAX_VR4130_NOPS - i;
7d8e00cf
RS
5797 }
5798 return 0;
5799}
5800
15be625d
CM
5801#define BASE_REG_EQ(INSN1, INSN2) \
5802 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
5803 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
5804
5805/* Return the minimum alignment for this store instruction. */
5806
5807static int
5808fix_24k_align_to (const struct mips_opcode *mo)
5809{
5810 if (strcmp (mo->name, "sh") == 0)
5811 return 2;
5812
5813 if (strcmp (mo->name, "swc1") == 0
5814 || strcmp (mo->name, "swc2") == 0
5815 || strcmp (mo->name, "sw") == 0
5816 || strcmp (mo->name, "sc") == 0
5817 || strcmp (mo->name, "s.s") == 0)
5818 return 4;
5819
5820 if (strcmp (mo->name, "sdc1") == 0
5821 || strcmp (mo->name, "sdc2") == 0
5822 || strcmp (mo->name, "s.d") == 0)
5823 return 8;
5824
5825 /* sb, swl, swr */
5826 return 1;
5827}
5828
5829struct fix_24k_store_info
5830 {
5831 /* Immediate offset, if any, for this store instruction. */
5832 short off;
5833 /* Alignment required by this store instruction. */
5834 int align_to;
5835 /* True for register offsets. */
5836 int register_offset;
5837 };
5838
5839/* Comparison function used by qsort. */
5840
5841static int
5842fix_24k_sort (const void *a, const void *b)
5843{
5844 const struct fix_24k_store_info *pos1 = a;
5845 const struct fix_24k_store_info *pos2 = b;
5846
5847 return (pos1->off - pos2->off);
5848}
5849
5850/* INSN is a store instruction. Try to record the store information
5851 in STINFO. Return false if the information isn't known. */
5852
5853static bfd_boolean
5854fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
ab9794cf 5855 const struct mips_cl_insn *insn)
15be625d
CM
5856{
5857 /* The instruction must have a known offset. */
5858 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
5859 return FALSE;
5860
5861 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
5862 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
5863 return TRUE;
5864}
5865
932d1a1b
RS
5866/* Return the number of nops that would be needed to work around the 24k
5867 "lost data on stores during refill" errata if instruction INSN
5868 immediately followed the 2 instructions described by HIST.
5869 Ignore hazards that are contained within the first IGNORE
5870 instructions of HIST.
5871
5872 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
5873 for the data cache refills and store data. The following describes
5874 the scenario where the store data could be lost.
5875
5876 * A data cache miss, due to either a load or a store, causing fill
5877 data to be supplied by the memory subsystem
5878 * The first three doublewords of fill data are returned and written
5879 into the cache
5880 * A sequence of four stores occurs in consecutive cycles around the
5881 final doubleword of the fill:
5882 * Store A
5883 * Store B
5884 * Store C
5885 * Zero, One or more instructions
5886 * Store D
5887
5888 The four stores A-D must be to different doublewords of the line that
5889 is being filled. The fourth instruction in the sequence above permits
5890 the fill of the final doubleword to be transferred from the FSB into
5891 the cache. In the sequence above, the stores may be either integer
5892 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
5893 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
5894 different doublewords on the line. If the floating point unit is
5895 running in 1:2 mode, it is not possible to create the sequence above
5896 using only floating point store instructions.
15be625d
CM
5897
5898 In this case, the cache line being filled is incorrectly marked
5899 invalid, thereby losing the data from any store to the line that
5900 occurs between the original miss and the completion of the five
5901 cycle sequence shown above.
5902
932d1a1b 5903 The workarounds are:
15be625d 5904
932d1a1b
RS
5905 * Run the data cache in write-through mode.
5906 * Insert a non-store instruction between
5907 Store A and Store B or Store B and Store C. */
15be625d
CM
5908
5909static int
932d1a1b 5910nops_for_24k (int ignore, const struct mips_cl_insn *hist,
15be625d
CM
5911 const struct mips_cl_insn *insn)
5912{
5913 struct fix_24k_store_info pos[3];
5914 int align, i, base_offset;
5915
932d1a1b
RS
5916 if (ignore >= 2)
5917 return 0;
5918
ab9794cf
RS
5919 /* If the previous instruction wasn't a store, there's nothing to
5920 worry about. */
15be625d
CM
5921 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
5922 return 0;
5923
ab9794cf
RS
5924 /* If the instructions after the previous one are unknown, we have
5925 to assume the worst. */
5926 if (!insn)
15be625d
CM
5927 return 1;
5928
ab9794cf
RS
5929 /* Check whether we are dealing with three consecutive stores. */
5930 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
5931 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
15be625d
CM
5932 return 0;
5933
5934 /* If we don't know the relationship between the store addresses,
5935 assume the worst. */
ab9794cf 5936 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
15be625d
CM
5937 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
5938 return 1;
5939
5940 if (!fix_24k_record_store_info (&pos[0], insn)
5941 || !fix_24k_record_store_info (&pos[1], &hist[0])
5942 || !fix_24k_record_store_info (&pos[2], &hist[1]))
5943 return 1;
5944
5945 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
5946
5947 /* Pick a value of ALIGN and X such that all offsets are adjusted by
5948 X bytes and such that the base register + X is known to be aligned
5949 to align bytes. */
5950
5951 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
5952 align = 8;
5953 else
5954 {
5955 align = pos[0].align_to;
5956 base_offset = pos[0].off;
5957 for (i = 1; i < 3; i++)
5958 if (align < pos[i].align_to)
5959 {
5960 align = pos[i].align_to;
5961 base_offset = pos[i].off;
5962 }
5963 for (i = 0; i < 3; i++)
5964 pos[i].off -= base_offset;
5965 }
5966
5967 pos[0].off &= ~align + 1;
5968 pos[1].off &= ~align + 1;
5969 pos[2].off &= ~align + 1;
5970
5971 /* If any two stores write to the same chunk, they also write to the
5972 same doubleword. The offsets are still sorted at this point. */
5973 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
5974 return 0;
5975
5976 /* A range of at least 9 bytes is needed for the stores to be in
5977 non-overlapping doublewords. */
5978 if (pos[2].off - pos[0].off <= 8)
5979 return 0;
5980
5981 if (pos[2].off - pos[1].off >= 24
5982 || pos[1].off - pos[0].off >= 24
5983 || pos[2].off - pos[0].off >= 32)
5984 return 0;
5985
5986 return 1;
5987}
5988
71400594 5989/* Return the number of nops that would be needed if instruction INSN
91d6fa6a 5990 immediately followed the MAX_NOPS instructions given by HIST,
932d1a1b
RS
5991 where HIST[0] is the most recent instruction. Ignore hazards
5992 between INSN and the first IGNORE instructions in HIST.
5993
5994 If INSN is null, return the worse-case number of nops for any
5995 instruction. */
bdaaa2e1 5996
71400594 5997static int
932d1a1b 5998nops_for_insn (int ignore, const struct mips_cl_insn *hist,
71400594
RS
5999 const struct mips_cl_insn *insn)
6000{
6001 int i, nops, tmp_nops;
bdaaa2e1 6002
71400594 6003 nops = 0;
932d1a1b 6004 for (i = ignore; i < MAX_DELAY_NOPS; i++)
65b02341 6005 {
91d6fa6a 6006 tmp_nops = insns_between (hist + i, insn) - i;
65b02341
RS
6007 if (tmp_nops > nops)
6008 nops = tmp_nops;
6009 }
7d8e00cf 6010
df58fc94 6011 if (mips_fix_vr4130 && !mips_opts.micromips)
7d8e00cf 6012 {
932d1a1b 6013 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
7d8e00cf
RS
6014 if (tmp_nops > nops)
6015 nops = tmp_nops;
6016 }
6017
df58fc94 6018 if (mips_fix_24k && !mips_opts.micromips)
15be625d 6019 {
932d1a1b 6020 tmp_nops = nops_for_24k (ignore, hist, insn);
15be625d
CM
6021 if (tmp_nops > nops)
6022 nops = tmp_nops;
6023 }
6024
71400594
RS
6025 return nops;
6026}
252b5132 6027
71400594 6028/* The variable arguments provide NUM_INSNS extra instructions that
91d6fa6a 6029 might be added to HIST. Return the largest number of nops that
932d1a1b
RS
6030 would be needed after the extended sequence, ignoring hazards
6031 in the first IGNORE instructions. */
252b5132 6032
71400594 6033static int
932d1a1b
RS
6034nops_for_sequence (int num_insns, int ignore,
6035 const struct mips_cl_insn *hist, ...)
71400594
RS
6036{
6037 va_list args;
6038 struct mips_cl_insn buffer[MAX_NOPS];
6039 struct mips_cl_insn *cursor;
6040 int nops;
6041
91d6fa6a 6042 va_start (args, hist);
71400594 6043 cursor = buffer + num_insns;
91d6fa6a 6044 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
71400594
RS
6045 while (cursor > buffer)
6046 *--cursor = *va_arg (args, const struct mips_cl_insn *);
6047
932d1a1b 6048 nops = nops_for_insn (ignore, buffer, NULL);
71400594
RS
6049 va_end (args);
6050 return nops;
6051}
252b5132 6052
71400594
RS
6053/* Like nops_for_insn, but if INSN is a branch, take into account the
6054 worst-case delay for the branch target. */
252b5132 6055
71400594 6056static int
932d1a1b 6057nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
71400594
RS
6058 const struct mips_cl_insn *insn)
6059{
6060 int nops, tmp_nops;
60b63b72 6061
932d1a1b 6062 nops = nops_for_insn (ignore, hist, insn);
11625dd8 6063 if (delayed_branch_p (insn))
71400594 6064 {
932d1a1b 6065 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
14fe068b 6066 hist, insn, get_delay_slot_nop (insn));
71400594
RS
6067 if (tmp_nops > nops)
6068 nops = tmp_nops;
6069 }
11625dd8 6070 else if (compact_branch_p (insn))
71400594 6071 {
932d1a1b 6072 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
71400594
RS
6073 if (tmp_nops > nops)
6074 nops = tmp_nops;
6075 }
6076 return nops;
6077}
6078
c67a084a
NC
6079/* Fix NOP issue: Replace nops by "or at,at,zero". */
6080
6081static void
6082fix_loongson2f_nop (struct mips_cl_insn * ip)
6083{
df58fc94 6084 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
6085 if (strcmp (ip->insn_mo->name, "nop") == 0)
6086 ip->insn_opcode = LOONGSON2F_NOP_INSN;
6087}
6088
6089/* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
6090 jr target pc &= 'hffff_ffff_cfff_ffff. */
6091
6092static void
6093fix_loongson2f_jump (struct mips_cl_insn * ip)
6094{
df58fc94 6095 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
6096 if (strcmp (ip->insn_mo->name, "j") == 0
6097 || strcmp (ip->insn_mo->name, "jr") == 0
6098 || strcmp (ip->insn_mo->name, "jalr") == 0)
6099 {
6100 int sreg;
6101 expressionS ep;
6102
6103 if (! mips_opts.at)
6104 return;
6105
df58fc94 6106 sreg = EXTRACT_OPERAND (0, RS, *ip);
c67a084a
NC
6107 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
6108 return;
6109
6110 ep.X_op = O_constant;
6111 ep.X_add_number = 0xcfff0000;
6112 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
6113 ep.X_add_number = 0xffff;
6114 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
6115 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
6116 }
6117}
6118
6119static void
6120fix_loongson2f (struct mips_cl_insn * ip)
6121{
6122 if (mips_fix_loongson2f_nop)
6123 fix_loongson2f_nop (ip);
6124
6125 if (mips_fix_loongson2f_jump)
6126 fix_loongson2f_jump (ip);
6127}
6128
a4e06468
RS
6129/* IP is a branch that has a delay slot, and we need to fill it
6130 automatically. Return true if we can do that by swapping IP
e407c74b
NC
6131 with the previous instruction.
6132 ADDRESS_EXPR is an operand of the instruction to be used with
6133 RELOC_TYPE. */
a4e06468
RS
6134
6135static bfd_boolean
e407c74b 6136can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 6137 bfd_reloc_code_real_type *reloc_type)
a4e06468 6138{
2b0c8b40 6139 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
a4e06468 6140 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
9d5de888 6141 unsigned int fpr_read, prev_fpr_write;
a4e06468
RS
6142
6143 /* -O2 and above is required for this optimization. */
6144 if (mips_optimize < 2)
6145 return FALSE;
6146
6147 /* If we have seen .set volatile or .set nomove, don't optimize. */
6148 if (mips_opts.nomove)
6149 return FALSE;
6150
6151 /* We can't swap if the previous instruction's position is fixed. */
6152 if (history[0].fixed_p)
6153 return FALSE;
6154
6155 /* If the previous previous insn was in a .set noreorder, we can't
6156 swap. Actually, the MIPS assembler will swap in this situation.
6157 However, gcc configured -with-gnu-as will generate code like
6158
6159 .set noreorder
6160 lw $4,XXX
6161 .set reorder
6162 INSN
6163 bne $4,$0,foo
6164
6165 in which we can not swap the bne and INSN. If gcc is not configured
6166 -with-gnu-as, it does not output the .set pseudo-ops. */
6167 if (history[1].noreorder_p)
6168 return FALSE;
6169
87333bb7
MR
6170 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
6171 This means that the previous instruction was a 4-byte one anyhow. */
a4e06468
RS
6172 if (mips_opts.mips16 && history[0].fixp[0])
6173 return FALSE;
6174
6175 /* If the branch is itself the target of a branch, we can not swap.
6176 We cheat on this; all we check for is whether there is a label on
6177 this instruction. If there are any branches to anything other than
6178 a label, users must use .set noreorder. */
6179 if (seg_info (now_seg)->label_list)
6180 return FALSE;
6181
6182 /* If the previous instruction is in a variant frag other than this
2309ddf2 6183 branch's one, we cannot do the swap. This does not apply to
9301f9c3
MR
6184 MIPS16 code, which uses variant frags for different purposes. */
6185 if (!mips_opts.mips16
a4e06468
RS
6186 && history[0].frag
6187 && history[0].frag->fr_type == rs_machine_dependent)
6188 return FALSE;
6189
bcd530a7
RS
6190 /* We do not swap with instructions that cannot architecturally
6191 be placed in a branch delay slot, such as SYNC or ERET. We
6192 also refrain from swapping with a trap instruction, since it
6193 complicates trap handlers to have the trap instruction be in
6194 a delay slot. */
a4e06468 6195 prev_pinfo = history[0].insn_mo->pinfo;
bcd530a7 6196 if (prev_pinfo & INSN_NO_DELAY_SLOT)
a4e06468
RS
6197 return FALSE;
6198
6199 /* Check for conflicts between the branch and the instructions
6200 before the candidate delay slot. */
6201 if (nops_for_insn (0, history + 1, ip) > 0)
6202 return FALSE;
6203
6204 /* Check for conflicts between the swapped sequence and the
6205 target of the branch. */
6206 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
6207 return FALSE;
6208
6209 /* If the branch reads a register that the previous
6210 instruction sets, we can not swap. */
6211 gpr_read = gpr_read_mask (ip);
6212 prev_gpr_write = gpr_write_mask (&history[0]);
6213 if (gpr_read & prev_gpr_write)
6214 return FALSE;
6215
9d5de888
CF
6216 fpr_read = fpr_read_mask (ip);
6217 prev_fpr_write = fpr_write_mask (&history[0]);
6218 if (fpr_read & prev_fpr_write)
6219 return FALSE;
6220
a4e06468
RS
6221 /* If the branch writes a register that the previous
6222 instruction sets, we can not swap. */
6223 gpr_write = gpr_write_mask (ip);
6224 if (gpr_write & prev_gpr_write)
6225 return FALSE;
6226
6227 /* If the branch writes a register that the previous
6228 instruction reads, we can not swap. */
6229 prev_gpr_read = gpr_read_mask (&history[0]);
6230 if (gpr_write & prev_gpr_read)
6231 return FALSE;
6232
6233 /* If one instruction sets a condition code and the
6234 other one uses a condition code, we can not swap. */
6235 pinfo = ip->insn_mo->pinfo;
6236 if ((pinfo & INSN_READ_COND_CODE)
6237 && (prev_pinfo & INSN_WRITE_COND_CODE))
6238 return FALSE;
6239 if ((pinfo & INSN_WRITE_COND_CODE)
6240 && (prev_pinfo & INSN_READ_COND_CODE))
6241 return FALSE;
6242
6243 /* If the previous instruction uses the PC, we can not swap. */
2b0c8b40 6244 prev_pinfo2 = history[0].insn_mo->pinfo2;
26545944 6245 if (prev_pinfo2 & INSN2_READ_PC)
2b0c8b40 6246 return FALSE;
a4e06468 6247
df58fc94
RS
6248 /* If the previous instruction has an incorrect size for a fixed
6249 branch delay slot in microMIPS mode, we cannot swap. */
2309ddf2
MR
6250 pinfo2 = ip->insn_mo->pinfo2;
6251 if (mips_opts.micromips
6252 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
6253 && insn_length (history) != 2)
6254 return FALSE;
6255 if (mips_opts.micromips
6256 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
6257 && insn_length (history) != 4)
6258 return FALSE;
6259
e407c74b
NC
6260 /* On R5900 short loops need to be fixed by inserting a nop in
6261 the branch delay slots.
6262 A short loop can be terminated too early. */
6263 if (mips_opts.arch == CPU_R5900
6264 /* Check if instruction has a parameter, ignore "j $31". */
6265 && (address_expr != NULL)
6266 /* Parameter must be 16 bit. */
6267 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
6268 /* Branch to same segment. */
6269 && (S_GET_SEGMENT(address_expr->X_add_symbol) == now_seg)
6270 /* Branch to same code fragment. */
6271 && (symbol_get_frag(address_expr->X_add_symbol) == frag_now)
6272 /* Can only calculate branch offset if value is known. */
6273 && symbol_constant_p(address_expr->X_add_symbol)
6274 /* Check if branch is really conditional. */
6275 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
6276 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
6277 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
6278 {
6279 int distance;
6280 /* Check if loop is shorter than 6 instructions including
6281 branch and delay slot. */
6282 distance = frag_now_fix() - S_GET_VALUE(address_expr->X_add_symbol);
6283 if (distance <= 20)
6284 {
6285 int i;
6286 int rv;
6287
6288 rv = FALSE;
6289 /* When the loop includes branches or jumps,
6290 it is not a short loop. */
6291 for (i = 0; i < (distance / 4); i++)
6292 {
6293 if ((history[i].cleared_p)
6294 || delayed_branch_p(&history[i]))
6295 {
6296 rv = TRUE;
6297 break;
6298 }
6299 }
6300 if (rv == FALSE)
6301 {
6302 /* Insert nop after branch to fix short loop. */
6303 return FALSE;
6304 }
6305 }
6306 }
6307
a4e06468
RS
6308 return TRUE;
6309}
6310
e407c74b
NC
6311/* Decide how we should add IP to the instruction stream.
6312 ADDRESS_EXPR is an operand of the instruction to be used with
6313 RELOC_TYPE. */
a4e06468
RS
6314
6315static enum append_method
e407c74b 6316get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 6317 bfd_reloc_code_real_type *reloc_type)
a4e06468 6318{
a4e06468
RS
6319 /* The relaxed version of a macro sequence must be inherently
6320 hazard-free. */
6321 if (mips_relax.sequence == 2)
6322 return APPEND_ADD;
6323
6324 /* We must not dabble with instructions in a ".set norerorder" block. */
6325 if (mips_opts.noreorder)
6326 return APPEND_ADD;
6327
6328 /* Otherwise, it's our responsibility to fill branch delay slots. */
11625dd8 6329 if (delayed_branch_p (ip))
a4e06468 6330 {
e407c74b
NC
6331 if (!branch_likely_p (ip)
6332 && can_swap_branch_p (ip, address_expr, reloc_type))
a4e06468
RS
6333 return APPEND_SWAP;
6334
6335 if (mips_opts.mips16
6336 && ISA_SUPPORTS_MIPS16E
fc76e730 6337 && gpr_read_mask (ip) != 0)
a4e06468
RS
6338 return APPEND_ADD_COMPACT;
6339
6340 return APPEND_ADD_WITH_NOP;
6341 }
6342
a4e06468
RS
6343 return APPEND_ADD;
6344}
6345
ceb94aa5
RS
6346/* IP is a MIPS16 instruction whose opcode we have just changed.
6347 Point IP->insn_mo to the new opcode's definition. */
6348
6349static void
6350find_altered_mips16_opcode (struct mips_cl_insn *ip)
6351{
6352 const struct mips_opcode *mo, *end;
6353
6354 end = &mips16_opcodes[bfd_mips16_num_opcodes];
6355 for (mo = ip->insn_mo; mo < end; mo++)
6356 if ((ip->insn_opcode & mo->mask) == mo->match)
6357 {
6358 ip->insn_mo = mo;
6359 return;
6360 }
6361 abort ();
6362}
6363
df58fc94
RS
6364/* For microMIPS macros, we need to generate a local number label
6365 as the target of branches. */
6366#define MICROMIPS_LABEL_CHAR '\037'
6367static unsigned long micromips_target_label;
6368static char micromips_target_name[32];
6369
6370static char *
6371micromips_label_name (void)
6372{
6373 char *p = micromips_target_name;
6374 char symbol_name_temporary[24];
6375 unsigned long l;
6376 int i;
6377
6378 if (*p)
6379 return p;
6380
6381 i = 0;
6382 l = micromips_target_label;
6383#ifdef LOCAL_LABEL_PREFIX
6384 *p++ = LOCAL_LABEL_PREFIX;
6385#endif
6386 *p++ = 'L';
6387 *p++ = MICROMIPS_LABEL_CHAR;
6388 do
6389 {
6390 symbol_name_temporary[i++] = l % 10 + '0';
6391 l /= 10;
6392 }
6393 while (l != 0);
6394 while (i > 0)
6395 *p++ = symbol_name_temporary[--i];
6396 *p = '\0';
6397
6398 return micromips_target_name;
6399}
6400
6401static void
6402micromips_label_expr (expressionS *label_expr)
6403{
6404 label_expr->X_op = O_symbol;
6405 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
6406 label_expr->X_add_number = 0;
6407}
6408
6409static void
6410micromips_label_inc (void)
6411{
6412 micromips_target_label++;
6413 *micromips_target_name = '\0';
6414}
6415
6416static void
6417micromips_add_label (void)
6418{
6419 symbolS *s;
6420
6421 s = colon (micromips_label_name ());
6422 micromips_label_inc ();
f3ded42a 6423 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
df58fc94
RS
6424}
6425
6426/* If assembling microMIPS code, then return the microMIPS reloc
6427 corresponding to the requested one if any. Otherwise return
6428 the reloc unchanged. */
6429
6430static bfd_reloc_code_real_type
6431micromips_map_reloc (bfd_reloc_code_real_type reloc)
6432{
6433 static const bfd_reloc_code_real_type relocs[][2] =
6434 {
6435 /* Keep sorted incrementally by the left-hand key. */
6436 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
6437 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
6438 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
6439 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
6440 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
6441 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
6442 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
6443 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
6444 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
6445 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
6446 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
6447 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
6448 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
6449 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
6450 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
6451 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
6452 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
6453 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
6454 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
6455 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
6456 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
6457 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
6458 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
6459 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
6460 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
6461 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
6462 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
6463 };
6464 bfd_reloc_code_real_type r;
6465 size_t i;
6466
6467 if (!mips_opts.micromips)
6468 return reloc;
6469 for (i = 0; i < ARRAY_SIZE (relocs); i++)
6470 {
6471 r = relocs[i][0];
6472 if (r > reloc)
6473 return reloc;
6474 if (r == reloc)
6475 return relocs[i][1];
6476 }
6477 return reloc;
6478}
6479
b886a2ab
RS
6480/* Try to resolve relocation RELOC against constant OPERAND at assembly time.
6481 Return true on success, storing the resolved value in RESULT. */
6482
6483static bfd_boolean
6484calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
6485 offsetT *result)
6486{
6487 switch (reloc)
6488 {
6489 case BFD_RELOC_MIPS_HIGHEST:
6490 case BFD_RELOC_MICROMIPS_HIGHEST:
6491 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
6492 return TRUE;
6493
6494 case BFD_RELOC_MIPS_HIGHER:
6495 case BFD_RELOC_MICROMIPS_HIGHER:
6496 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
6497 return TRUE;
6498
6499 case BFD_RELOC_HI16_S:
6500 case BFD_RELOC_MICROMIPS_HI16_S:
6501 case BFD_RELOC_MIPS16_HI16_S:
6502 *result = ((operand + 0x8000) >> 16) & 0xffff;
6503 return TRUE;
6504
6505 case BFD_RELOC_HI16:
6506 case BFD_RELOC_MICROMIPS_HI16:
6507 case BFD_RELOC_MIPS16_HI16:
6508 *result = (operand >> 16) & 0xffff;
6509 return TRUE;
6510
6511 case BFD_RELOC_LO16:
6512 case BFD_RELOC_MICROMIPS_LO16:
6513 case BFD_RELOC_MIPS16_LO16:
6514 *result = operand & 0xffff;
6515 return TRUE;
6516
6517 case BFD_RELOC_UNUSED:
6518 *result = operand;
6519 return TRUE;
6520
6521 default:
6522 return FALSE;
6523 }
6524}
6525
71400594
RS
6526/* Output an instruction. IP is the instruction information.
6527 ADDRESS_EXPR is an operand of the instruction to be used with
df58fc94
RS
6528 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
6529 a macro expansion. */
71400594
RS
6530
6531static void
6532append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
df58fc94 6533 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
71400594 6534{
14fe068b 6535 unsigned long prev_pinfo2, pinfo;
71400594 6536 bfd_boolean relaxed_branch = FALSE;
a4e06468 6537 enum append_method method;
2309ddf2 6538 bfd_boolean relax32;
2b0c8b40 6539 int branch_disp;
71400594 6540
2309ddf2 6541 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
c67a084a
NC
6542 fix_loongson2f (ip);
6543
738f4d98 6544 file_ase_mips16 |= mips_opts.mips16;
df58fc94 6545 file_ase_micromips |= mips_opts.micromips;
738f4d98 6546
df58fc94 6547 prev_pinfo2 = history[0].insn_mo->pinfo2;
71400594 6548 pinfo = ip->insn_mo->pinfo;
df58fc94
RS
6549
6550 if (mips_opts.micromips
6551 && !expansionp
6552 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
6553 && micromips_insn_length (ip->insn_mo) != 2)
6554 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
6555 && micromips_insn_length (ip->insn_mo) != 4)))
1661c76c 6556 as_warn (_("wrong size instruction in a %u-bit branch delay slot"),
df58fc94 6557 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
71400594 6558
15be625d
CM
6559 if (address_expr == NULL)
6560 ip->complete_p = 1;
b886a2ab
RS
6561 else if (reloc_type[0] <= BFD_RELOC_UNUSED
6562 && reloc_type[1] == BFD_RELOC_UNUSED
6563 && reloc_type[2] == BFD_RELOC_UNUSED
15be625d
CM
6564 && address_expr->X_op == O_constant)
6565 {
15be625d
CM
6566 switch (*reloc_type)
6567 {
15be625d 6568 case BFD_RELOC_MIPS_JMP:
df58fc94
RS
6569 {
6570 int shift;
6571
6572 shift = mips_opts.micromips ? 1 : 2;
6573 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
6574 as_bad (_("jump to misaligned address (0x%lx)"),
6575 (unsigned long) address_expr->X_add_number);
6576 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
6577 & 0x3ffffff);
335574df 6578 ip->complete_p = 1;
df58fc94 6579 }
15be625d
CM
6580 break;
6581
6582 case BFD_RELOC_MIPS16_JMP:
6583 if ((address_expr->X_add_number & 3) != 0)
6584 as_bad (_("jump to misaligned address (0x%lx)"),
6585 (unsigned long) address_expr->X_add_number);
6586 ip->insn_opcode |=
6587 (((address_expr->X_add_number & 0x7c0000) << 3)
6588 | ((address_expr->X_add_number & 0xf800000) >> 7)
6589 | ((address_expr->X_add_number & 0x3fffc) >> 2));
335574df 6590 ip->complete_p = 1;
15be625d
CM
6591 break;
6592
6593 case BFD_RELOC_16_PCREL_S2:
df58fc94
RS
6594 {
6595 int shift;
6596
6597 shift = mips_opts.micromips ? 1 : 2;
6598 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
6599 as_bad (_("branch to misaligned address (0x%lx)"),
6600 (unsigned long) address_expr->X_add_number);
6601 if (!mips_relax_branch)
6602 {
6603 if ((address_expr->X_add_number + (1 << (shift + 15)))
6604 & ~((1 << (shift + 16)) - 1))
6605 as_bad (_("branch address range overflow (0x%lx)"),
6606 (unsigned long) address_expr->X_add_number);
6607 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
6608 & 0xffff);
6609 }
df58fc94 6610 }
15be625d
CM
6611 break;
6612
6613 default:
b886a2ab
RS
6614 {
6615 offsetT value;
6616
6617 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
6618 &value))
6619 {
6620 ip->insn_opcode |= value & 0xffff;
6621 ip->complete_p = 1;
6622 }
6623 }
6624 break;
6625 }
15be625d
CM
6626 }
6627
71400594
RS
6628 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
6629 {
6630 /* There are a lot of optimizations we could do that we don't.
6631 In particular, we do not, in general, reorder instructions.
6632 If you use gcc with optimization, it will reorder
6633 instructions and generally do much more optimization then we
6634 do here; repeating all that work in the assembler would only
6635 benefit hand written assembly code, and does not seem worth
6636 it. */
6637 int nops = (mips_optimize == 0
932d1a1b
RS
6638 ? nops_for_insn (0, history, NULL)
6639 : nops_for_insn_or_target (0, history, ip));
71400594 6640 if (nops > 0)
252b5132
RH
6641 {
6642 fragS *old_frag;
6643 unsigned long old_frag_offset;
6644 int i;
252b5132
RH
6645
6646 old_frag = frag_now;
6647 old_frag_offset = frag_now_fix ();
6648
6649 for (i = 0; i < nops; i++)
14fe068b
RS
6650 add_fixed_insn (NOP_INSN);
6651 insert_into_history (0, nops, NOP_INSN);
252b5132
RH
6652
6653 if (listing)
6654 {
6655 listing_prev_line ();
6656 /* We may be at the start of a variant frag. In case we
6657 are, make sure there is enough space for the frag
6658 after the frags created by listing_prev_line. The
6659 argument to frag_grow here must be at least as large
6660 as the argument to all other calls to frag_grow in
6661 this file. We don't have to worry about being in the
6662 middle of a variant frag, because the variants insert
6663 all needed nop instructions themselves. */
6664 frag_grow (40);
6665 }
6666
462427c4 6667 mips_move_text_labels ();
252b5132
RH
6668
6669#ifndef NO_ECOFF_DEBUGGING
6670 if (ECOFF_DEBUGGING)
6671 ecoff_fix_loc (old_frag, old_frag_offset);
6672#endif
6673 }
71400594
RS
6674 }
6675 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
6676 {
932d1a1b
RS
6677 int nops;
6678
6679 /* Work out how many nops in prev_nop_frag are needed by IP,
6680 ignoring hazards generated by the first prev_nop_frag_since
6681 instructions. */
6682 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
9c2799c2 6683 gas_assert (nops <= prev_nop_frag_holds);
252b5132 6684
71400594
RS
6685 /* Enforce NOPS as a minimum. */
6686 if (nops > prev_nop_frag_required)
6687 prev_nop_frag_required = nops;
252b5132 6688
71400594
RS
6689 if (prev_nop_frag_holds == prev_nop_frag_required)
6690 {
6691 /* Settle for the current number of nops. Update the history
6692 accordingly (for the benefit of any future .set reorder code). */
6693 prev_nop_frag = NULL;
6694 insert_into_history (prev_nop_frag_since,
6695 prev_nop_frag_holds, NOP_INSN);
6696 }
6697 else
6698 {
6699 /* Allow this instruction to replace one of the nops that was
6700 tentatively added to prev_nop_frag. */
df58fc94 6701 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
71400594
RS
6702 prev_nop_frag_holds--;
6703 prev_nop_frag_since++;
252b5132
RH
6704 }
6705 }
6706
e407c74b 6707 method = get_append_method (ip, address_expr, reloc_type);
2b0c8b40 6708 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
a4e06468 6709
e410add4
RS
6710 dwarf2_emit_insn (0);
6711 /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
6712 so "move" the instruction address accordingly.
6713
6714 Also, it doesn't seem appropriate for the assembler to reorder .loc
6715 entries. If this instruction is a branch that we are going to swap
6716 with the previous instruction, the two instructions should be
6717 treated as a unit, and the debug information for both instructions
6718 should refer to the start of the branch sequence. Using the
6719 current position is certainly wrong when swapping a 32-bit branch
6720 and a 16-bit delay slot, since the current position would then be
6721 in the middle of a branch. */
6722 dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
58e2ea4d 6723
df58fc94
RS
6724 relax32 = (mips_relax_branch
6725 /* Don't try branch relaxation within .set nomacro, or within
6726 .set noat if we use $at for PIC computations. If it turns
6727 out that the branch was out-of-range, we'll get an error. */
6728 && !mips_opts.warn_about_macros
6729 && (mips_opts.at || mips_pic == NO_PIC)
3bf0dbfb
MR
6730 /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
6731 as they have no complementing branches. */
6732 && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
df58fc94
RS
6733
6734 if (!HAVE_CODE_COMPRESSION
6735 && address_expr
6736 && relax32
0b25d3e6 6737 && *reloc_type == BFD_RELOC_16_PCREL_S2
11625dd8 6738 && delayed_branch_p (ip))
4a6a3df4 6739 {
895921c9 6740 relaxed_branch = TRUE;
1e915849
RS
6741 add_relaxed_insn (ip, (relaxed_branch_length
6742 (NULL, NULL,
11625dd8
RS
6743 uncond_branch_p (ip) ? -1
6744 : branch_likely_p (ip) ? 1
1e915849
RS
6745 : 0)), 4,
6746 RELAX_BRANCH_ENCODE
66b3e8da 6747 (AT,
11625dd8
RS
6748 uncond_branch_p (ip),
6749 branch_likely_p (ip),
1e915849
RS
6750 pinfo & INSN_WRITE_GPR_31,
6751 0),
6752 address_expr->X_add_symbol,
6753 address_expr->X_add_number);
4a6a3df4
AO
6754 *reloc_type = BFD_RELOC_UNUSED;
6755 }
df58fc94
RS
6756 else if (mips_opts.micromips
6757 && address_expr
6758 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
6759 || *reloc_type > BFD_RELOC_UNUSED)
40209cad
MR
6760 && (delayed_branch_p (ip) || compact_branch_p (ip))
6761 /* Don't try branch relaxation when users specify
6762 16-bit/32-bit instructions. */
6763 && !forced_insn_length)
df58fc94
RS
6764 {
6765 bfd_boolean relax16 = *reloc_type > BFD_RELOC_UNUSED;
6766 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
11625dd8
RS
6767 int uncond = uncond_branch_p (ip) ? -1 : 0;
6768 int compact = compact_branch_p (ip);
df58fc94
RS
6769 int al = pinfo & INSN_WRITE_GPR_31;
6770 int length32;
6771
6772 gas_assert (address_expr != NULL);
6773 gas_assert (!mips_relax.sequence);
6774
2b0c8b40 6775 relaxed_branch = TRUE;
df58fc94
RS
6776 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
6777 add_relaxed_insn (ip, relax32 ? length32 : 4, relax16 ? 2 : 4,
40209cad
MR
6778 RELAX_MICROMIPS_ENCODE (type, AT, uncond, compact, al,
6779 relax32, 0, 0),
df58fc94
RS
6780 address_expr->X_add_symbol,
6781 address_expr->X_add_number);
6782 *reloc_type = BFD_RELOC_UNUSED;
6783 }
6784 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
6785 {
6786 /* We need to set up a variant frag. */
df58fc94 6787 gas_assert (address_expr != NULL);
1e915849
RS
6788 add_relaxed_insn (ip, 4, 0,
6789 RELAX_MIPS16_ENCODE
6790 (*reloc_type - BFD_RELOC_UNUSED,
df58fc94 6791 forced_insn_length == 2, forced_insn_length == 4,
11625dd8 6792 delayed_branch_p (&history[0]),
1e915849
RS
6793 history[0].mips16_absolute_jump_p),
6794 make_expr_symbol (address_expr), 0);
252b5132 6795 }
5c04167a 6796 else if (mips_opts.mips16 && insn_length (ip) == 2)
9497f5ac 6797 {
11625dd8 6798 if (!delayed_branch_p (ip))
b8ee1a6e
DU
6799 /* Make sure there is enough room to swap this instruction with
6800 a following jump instruction. */
6801 frag_grow (6);
1e915849 6802 add_fixed_insn (ip);
252b5132
RH
6803 }
6804 else
6805 {
6806 if (mips_opts.mips16
6807 && mips_opts.noreorder
11625dd8 6808 && delayed_branch_p (&history[0]))
252b5132
RH
6809 as_warn (_("extended instruction in delay slot"));
6810
4d7206a2
RS
6811 if (mips_relax.sequence)
6812 {
6813 /* If we've reached the end of this frag, turn it into a variant
6814 frag and record the information for the instructions we've
6815 written so far. */
6816 if (frag_room () < 4)
6817 relax_close_frag ();
df58fc94 6818 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4d7206a2
RS
6819 }
6820
584892a6 6821 if (mips_relax.sequence != 2)
df58fc94
RS
6822 {
6823 if (mips_macro_warning.first_insn_sizes[0] == 0)
6824 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
6825 mips_macro_warning.sizes[0] += insn_length (ip);
6826 mips_macro_warning.insns[0]++;
6827 }
584892a6 6828 if (mips_relax.sequence != 1)
df58fc94
RS
6829 {
6830 if (mips_macro_warning.first_insn_sizes[1] == 0)
6831 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
6832 mips_macro_warning.sizes[1] += insn_length (ip);
6833 mips_macro_warning.insns[1]++;
6834 }
584892a6 6835
1e915849
RS
6836 if (mips_opts.mips16)
6837 {
6838 ip->fixed_p = 1;
6839 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
6840 }
6841 add_fixed_insn (ip);
252b5132
RH
6842 }
6843
9fe77896 6844 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
252b5132 6845 {
df58fc94 6846 bfd_reloc_code_real_type final_type[3];
2309ddf2 6847 reloc_howto_type *howto0;
9fe77896
RS
6848 reloc_howto_type *howto;
6849 int i;
34ce925e 6850
df58fc94
RS
6851 /* Perform any necessary conversion to microMIPS relocations
6852 and find out how many relocations there actually are. */
6853 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
6854 final_type[i] = micromips_map_reloc (reloc_type[i]);
6855
9fe77896
RS
6856 /* In a compound relocation, it is the final (outermost)
6857 operator that determines the relocated field. */
2309ddf2 6858 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
e8044f35
RS
6859 if (!howto)
6860 abort ();
2309ddf2
MR
6861
6862 if (i > 1)
6863 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
9fe77896
RS
6864 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
6865 bfd_get_reloc_size (howto),
6866 address_expr,
2309ddf2
MR
6867 howto0 && howto0->pc_relative,
6868 final_type[0]);
9fe77896
RS
6869
6870 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
2309ddf2 6871 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
9fe77896
RS
6872 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
6873
6874 /* These relocations can have an addend that won't fit in
6875 4 octets for 64bit assembly. */
6876 if (HAVE_64BIT_GPRS
6877 && ! howto->partial_inplace
6878 && (reloc_type[0] == BFD_RELOC_16
6879 || reloc_type[0] == BFD_RELOC_32
6880 || reloc_type[0] == BFD_RELOC_MIPS_JMP
6881 || reloc_type[0] == BFD_RELOC_GPREL16
6882 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
6883 || reloc_type[0] == BFD_RELOC_GPREL32
6884 || reloc_type[0] == BFD_RELOC_64
6885 || reloc_type[0] == BFD_RELOC_CTOR
6886 || reloc_type[0] == BFD_RELOC_MIPS_SUB
6887 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
6888 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
6889 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
6890 || reloc_type[0] == BFD_RELOC_MIPS_REL16
6891 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
6892 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
6893 || hi16_reloc_p (reloc_type[0])
6894 || lo16_reloc_p (reloc_type[0])))
6895 ip->fixp[0]->fx_no_overflow = 1;
6896
ddaf2c41
MR
6897 /* These relocations can have an addend that won't fit in 2 octets. */
6898 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
6899 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
6900 ip->fixp[0]->fx_no_overflow = 1;
6901
9fe77896
RS
6902 if (mips_relax.sequence)
6903 {
6904 if (mips_relax.first_fixup == 0)
6905 mips_relax.first_fixup = ip->fixp[0];
6906 }
6907 else if (reloc_needs_lo_p (*reloc_type))
6908 {
6909 struct mips_hi_fixup *hi_fixup;
6910
6911 /* Reuse the last entry if it already has a matching %lo. */
6912 hi_fixup = mips_hi_fixup_list;
6913 if (hi_fixup == 0
6914 || !fixup_has_matching_lo_p (hi_fixup->fixp))
4d7206a2 6915 {
9fe77896
RS
6916 hi_fixup = ((struct mips_hi_fixup *)
6917 xmalloc (sizeof (struct mips_hi_fixup)));
6918 hi_fixup->next = mips_hi_fixup_list;
6919 mips_hi_fixup_list = hi_fixup;
4d7206a2 6920 }
9fe77896
RS
6921 hi_fixup->fixp = ip->fixp[0];
6922 hi_fixup->seg = now_seg;
6923 }
252b5132 6924
9fe77896
RS
6925 /* Add fixups for the second and third relocations, if given.
6926 Note that the ABI allows the second relocation to be
6927 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
6928 moment we only use RSS_UNDEF, but we could add support
6929 for the others if it ever becomes necessary. */
6930 for (i = 1; i < 3; i++)
6931 if (reloc_type[i] != BFD_RELOC_UNUSED)
6932 {
6933 ip->fixp[i] = fix_new (ip->frag, ip->where,
6934 ip->fixp[0]->fx_size, NULL, 0,
df58fc94 6935 FALSE, final_type[i]);
f6688943 6936
9fe77896
RS
6937 /* Use fx_tcbit to mark compound relocs. */
6938 ip->fixp[0]->fx_tcbit = 1;
6939 ip->fixp[i]->fx_tcbit = 1;
6940 }
252b5132 6941 }
1e915849 6942 install_insn (ip);
252b5132
RH
6943
6944 /* Update the register mask information. */
4c260379
RS
6945 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
6946 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
252b5132 6947
a4e06468 6948 switch (method)
252b5132 6949 {
a4e06468
RS
6950 case APPEND_ADD:
6951 insert_into_history (0, 1, ip);
6952 break;
6953
6954 case APPEND_ADD_WITH_NOP:
14fe068b
RS
6955 {
6956 struct mips_cl_insn *nop;
6957
6958 insert_into_history (0, 1, ip);
6959 nop = get_delay_slot_nop (ip);
6960 add_fixed_insn (nop);
6961 insert_into_history (0, 1, nop);
6962 if (mips_relax.sequence)
6963 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
6964 }
a4e06468
RS
6965 break;
6966
6967 case APPEND_ADD_COMPACT:
6968 /* Convert MIPS16 jr/jalr into a "compact" jump. */
6969 gas_assert (mips_opts.mips16);
6970 ip->insn_opcode |= 0x0080;
6971 find_altered_mips16_opcode (ip);
6972 install_insn (ip);
6973 insert_into_history (0, 1, ip);
6974 break;
6975
6976 case APPEND_SWAP:
6977 {
6978 struct mips_cl_insn delay = history[0];
6979 if (mips_opts.mips16)
6980 {
6981 know (delay.frag == ip->frag);
6982 move_insn (ip, delay.frag, delay.where);
6983 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
6984 }
464ab0e5 6985 else if (relaxed_branch || delay.frag != ip->frag)
a4e06468
RS
6986 {
6987 /* Add the delay slot instruction to the end of the
6988 current frag and shrink the fixed part of the
6989 original frag. If the branch occupies the tail of
6990 the latter, move it backwards to cover the gap. */
2b0c8b40 6991 delay.frag->fr_fix -= branch_disp;
a4e06468 6992 if (delay.frag == ip->frag)
2b0c8b40 6993 move_insn (ip, ip->frag, ip->where - branch_disp);
a4e06468
RS
6994 add_fixed_insn (&delay);
6995 }
6996 else
6997 {
2b0c8b40
MR
6998 move_insn (&delay, ip->frag,
6999 ip->where - branch_disp + insn_length (ip));
a4e06468
RS
7000 move_insn (ip, history[0].frag, history[0].where);
7001 }
7002 history[0] = *ip;
7003 delay.fixed_p = 1;
7004 insert_into_history (0, 1, &delay);
7005 }
7006 break;
252b5132
RH
7007 }
7008
13408f1e 7009 /* If we have just completed an unconditional branch, clear the history. */
11625dd8
RS
7010 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
7011 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
e407c74b
NC
7012 {
7013 unsigned int i;
7014
79850f26 7015 mips_no_prev_insn ();
13408f1e 7016
e407c74b 7017 for (i = 0; i < ARRAY_SIZE (history); i++)
79850f26 7018 history[i].cleared_p = 1;
e407c74b
NC
7019 }
7020
df58fc94
RS
7021 /* We need to emit a label at the end of branch-likely macros. */
7022 if (emit_branch_likely_macro)
7023 {
7024 emit_branch_likely_macro = FALSE;
7025 micromips_add_label ();
7026 }
7027
252b5132
RH
7028 /* We just output an insn, so the next one doesn't have a label. */
7029 mips_clear_insn_labels ();
252b5132
RH
7030}
7031
e407c74b
NC
7032/* Forget that there was any previous instruction or label.
7033 When BRANCH is true, the branch history is also flushed. */
252b5132
RH
7034
7035static void
7d10b47d 7036mips_no_prev_insn (void)
252b5132 7037{
7d10b47d
RS
7038 prev_nop_frag = NULL;
7039 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
7040 mips_clear_insn_labels ();
7041}
7042
7d10b47d
RS
7043/* This function must be called before we emit something other than
7044 instructions. It is like mips_no_prev_insn except that it inserts
7045 any NOPS that might be needed by previous instructions. */
252b5132 7046
7d10b47d
RS
7047void
7048mips_emit_delays (void)
252b5132
RH
7049{
7050 if (! mips_opts.noreorder)
7051 {
932d1a1b 7052 int nops = nops_for_insn (0, history, NULL);
252b5132
RH
7053 if (nops > 0)
7054 {
7d10b47d
RS
7055 while (nops-- > 0)
7056 add_fixed_insn (NOP_INSN);
462427c4 7057 mips_move_text_labels ();
7d10b47d
RS
7058 }
7059 }
7060 mips_no_prev_insn ();
7061}
7062
7063/* Start a (possibly nested) noreorder block. */
7064
7065static void
7066start_noreorder (void)
7067{
7068 if (mips_opts.noreorder == 0)
7069 {
7070 unsigned int i;
7071 int nops;
7072
7073 /* None of the instructions before the .set noreorder can be moved. */
7074 for (i = 0; i < ARRAY_SIZE (history); i++)
7075 history[i].fixed_p = 1;
7076
7077 /* Insert any nops that might be needed between the .set noreorder
7078 block and the previous instructions. We will later remove any
7079 nops that turn out not to be needed. */
932d1a1b 7080 nops = nops_for_insn (0, history, NULL);
7d10b47d
RS
7081 if (nops > 0)
7082 {
7083 if (mips_optimize != 0)
252b5132
RH
7084 {
7085 /* Record the frag which holds the nop instructions, so
7086 that we can remove them if we don't need them. */
df58fc94 7087 frag_grow (nops * NOP_INSN_SIZE);
252b5132
RH
7088 prev_nop_frag = frag_now;
7089 prev_nop_frag_holds = nops;
7090 prev_nop_frag_required = 0;
7091 prev_nop_frag_since = 0;
7092 }
7093
7094 for (; nops > 0; --nops)
1e915849 7095 add_fixed_insn (NOP_INSN);
252b5132 7096
7d10b47d
RS
7097 /* Move on to a new frag, so that it is safe to simply
7098 decrease the size of prev_nop_frag. */
7099 frag_wane (frag_now);
7100 frag_new (0);
462427c4 7101 mips_move_text_labels ();
252b5132 7102 }
df58fc94 7103 mips_mark_labels ();
7d10b47d 7104 mips_clear_insn_labels ();
252b5132 7105 }
7d10b47d
RS
7106 mips_opts.noreorder++;
7107 mips_any_noreorder = 1;
7108}
252b5132 7109
7d10b47d 7110/* End a nested noreorder block. */
252b5132 7111
7d10b47d
RS
7112static void
7113end_noreorder (void)
7114{
7115 mips_opts.noreorder--;
7116 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
7117 {
7118 /* Commit to inserting prev_nop_frag_required nops and go back to
7119 handling nop insertion the .set reorder way. */
7120 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
df58fc94 7121 * NOP_INSN_SIZE);
7d10b47d
RS
7122 insert_into_history (prev_nop_frag_since,
7123 prev_nop_frag_required, NOP_INSN);
7124 prev_nop_frag = NULL;
7125 }
252b5132
RH
7126}
7127
97d87491
RS
7128/* Sign-extend 32-bit mode constants that have bit 31 set and all
7129 higher bits unset. */
7130
7131static void
7132normalize_constant_expr (expressionS *ex)
7133{
7134 if (ex->X_op == O_constant
7135 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7136 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7137 - 0x80000000);
7138}
7139
7140/* Sign-extend 32-bit mode address offsets that have bit 31 set and
7141 all higher bits unset. */
7142
7143static void
7144normalize_address_expr (expressionS *ex)
7145{
7146 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
7147 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
7148 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7149 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7150 - 0x80000000);
7151}
7152
7153/* Try to match TOKENS against OPCODE, storing the result in INSN.
7154 Return true if the match was successful.
7155
7156 OPCODE_EXTRA is a value that should be ORed into the opcode
7157 (used for VU0 channel suffixes, etc.). MORE_ALTS is true if
7158 there are more alternatives after OPCODE and SOFT_MATCH is
7159 as for mips_arg_info. */
7160
7161static bfd_boolean
7162match_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
7163 struct mips_operand_token *tokens, unsigned int opcode_extra,
60f20e8b 7164 bfd_boolean lax_match, bfd_boolean complete_p)
97d87491
RS
7165{
7166 const char *args;
7167 struct mips_arg_info arg;
7168 const struct mips_operand *operand;
7169 char c;
7170
7171 imm_expr.X_op = O_absent;
97d87491
RS
7172 offset_expr.X_op = O_absent;
7173 offset_reloc[0] = BFD_RELOC_UNUSED;
7174 offset_reloc[1] = BFD_RELOC_UNUSED;
7175 offset_reloc[2] = BFD_RELOC_UNUSED;
7176
7177 create_insn (insn, opcode);
60f20e8b
RS
7178 /* When no opcode suffix is specified, assume ".xyzw". */
7179 if ((opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0 && opcode_extra == 0)
7180 insn->insn_opcode |= 0xf << mips_vu0_channel_mask.lsb;
7181 else
7182 insn->insn_opcode |= opcode_extra;
97d87491
RS
7183 memset (&arg, 0, sizeof (arg));
7184 arg.insn = insn;
7185 arg.token = tokens;
7186 arg.argnum = 1;
7187 arg.last_regno = ILLEGAL_REG;
7188 arg.dest_regno = ILLEGAL_REG;
60f20e8b 7189 arg.lax_match = lax_match;
97d87491
RS
7190 for (args = opcode->args;; ++args)
7191 {
7192 if (arg.token->type == OT_END)
7193 {
7194 /* Handle unary instructions in which only one operand is given.
7195 The source is then the same as the destination. */
7196 if (arg.opnum == 1 && *args == ',')
7197 {
7198 operand = (mips_opts.micromips
7199 ? decode_micromips_operand (args + 1)
7200 : decode_mips_operand (args + 1));
7201 if (operand && mips_optional_operand_p (operand))
7202 {
7203 arg.token = tokens;
7204 arg.argnum = 1;
7205 continue;
7206 }
7207 }
7208
7209 /* Treat elided base registers as $0. */
7210 if (strcmp (args, "(b)") == 0)
7211 args += 3;
7212
7213 if (args[0] == '+')
7214 switch (args[1])
7215 {
7216 case 'K':
7217 case 'N':
7218 /* The register suffix is optional. */
7219 args += 2;
7220 break;
7221 }
7222
7223 /* Fail the match if there were too few operands. */
7224 if (*args)
7225 return FALSE;
7226
7227 /* Successful match. */
60f20e8b
RS
7228 if (!complete_p)
7229 return TRUE;
e3de51ce 7230 clear_insn_error ();
97d87491
RS
7231 if (arg.dest_regno == arg.last_regno
7232 && strncmp (insn->insn_mo->name, "jalr", 4) == 0)
7233 {
7234 if (arg.opnum == 2)
e3de51ce 7235 set_insn_error
1661c76c 7236 (0, _("source and destination must be different"));
97d87491 7237 else if (arg.last_regno == 31)
e3de51ce 7238 set_insn_error
1661c76c 7239 (0, _("a destination register must be supplied"));
97d87491 7240 }
173d3447
CF
7241 else if (arg.last_regno == 31
7242 && (strncmp (insn->insn_mo->name, "bltzal", 6) == 0
7243 || strncmp (insn->insn_mo->name, "bgezal", 6) == 0))
7244 set_insn_error (0, _("the source register must not be $31"));
97d87491
RS
7245 check_completed_insn (&arg);
7246 return TRUE;
7247 }
7248
7249 /* Fail the match if the line has too many operands. */
7250 if (*args == 0)
7251 return FALSE;
7252
7253 /* Handle characters that need to match exactly. */
7254 if (*args == '(' || *args == ')' || *args == ',')
7255 {
7256 if (match_char (&arg, *args))
7257 continue;
7258 return FALSE;
7259 }
7260 if (*args == '#')
7261 {
7262 ++args;
7263 if (arg.token->type == OT_DOUBLE_CHAR
7264 && arg.token->u.ch == *args)
7265 {
7266 ++arg.token;
7267 continue;
7268 }
7269 return FALSE;
7270 }
7271
7272 /* Handle special macro operands. Work out the properties of
7273 other operands. */
7274 arg.opnum += 1;
97d87491
RS
7275 switch (*args)
7276 {
7277 case '+':
7278 switch (args[1])
7279 {
97d87491
RS
7280 case 'i':
7281 *offset_reloc = BFD_RELOC_MIPS_JMP;
7282 break;
7283 }
7284 break;
7285
97d87491 7286 case 'I':
1a00e612
RS
7287 if (!match_const_int (&arg, &imm_expr.X_add_number))
7288 return FALSE;
7289 imm_expr.X_op = O_constant;
97d87491
RS
7290 if (HAVE_32BIT_GPRS)
7291 normalize_constant_expr (&imm_expr);
7292 continue;
7293
7294 case 'A':
7295 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
7296 {
7297 /* Assume that the offset has been elided and that what
7298 we saw was a base register. The match will fail later
7299 if that assumption turns out to be wrong. */
7300 offset_expr.X_op = O_constant;
7301 offset_expr.X_add_number = 0;
7302 }
97d87491 7303 else
1a00e612
RS
7304 {
7305 if (!match_expression (&arg, &offset_expr, offset_reloc))
7306 return FALSE;
7307 normalize_address_expr (&offset_expr);
7308 }
97d87491
RS
7309 continue;
7310
7311 case 'F':
7312 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
7313 8, TRUE))
1a00e612 7314 return FALSE;
97d87491
RS
7315 continue;
7316
7317 case 'L':
7318 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
7319 8, FALSE))
1a00e612 7320 return FALSE;
97d87491
RS
7321 continue;
7322
7323 case 'f':
7324 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
7325 4, TRUE))
1a00e612 7326 return FALSE;
97d87491
RS
7327 continue;
7328
7329 case 'l':
7330 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
7331 4, FALSE))
1a00e612 7332 return FALSE;
97d87491
RS
7333 continue;
7334
97d87491
RS
7335 case 'p':
7336 *offset_reloc = BFD_RELOC_16_PCREL_S2;
7337 break;
7338
7339 case 'a':
7340 *offset_reloc = BFD_RELOC_MIPS_JMP;
7341 break;
7342
7343 case 'm':
7344 gas_assert (mips_opts.micromips);
7345 c = args[1];
7346 switch (c)
7347 {
7348 case 'D':
7349 case 'E':
7350 if (!forced_insn_length)
7351 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
7352 else if (c == 'D')
7353 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
7354 else
7355 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
7356 break;
7357 }
7358 break;
7359 }
7360
7361 operand = (mips_opts.micromips
7362 ? decode_micromips_operand (args)
7363 : decode_mips_operand (args));
7364 if (!operand)
7365 abort ();
7366
7367 /* Skip prefixes. */
7368 if (*args == '+' || *args == 'm')
7369 args++;
7370
7371 if (mips_optional_operand_p (operand)
7372 && args[1] == ','
7373 && (arg.token[0].type != OT_REG
7374 || arg.token[1].type == OT_END))
7375 {
7376 /* Assume that the register has been elided and is the
7377 same as the first operand. */
7378 arg.token = tokens;
7379 arg.argnum = 1;
7380 }
7381
7382 if (!match_operand (&arg, operand))
7383 return FALSE;
7384 }
7385}
7386
7387/* Like match_insn, but for MIPS16. */
7388
7389static bfd_boolean
7390match_mips16_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
1a00e612 7391 struct mips_operand_token *tokens)
97d87491
RS
7392{
7393 const char *args;
7394 const struct mips_operand *operand;
7395 const struct mips_operand *ext_operand;
7396 struct mips_arg_info arg;
7397 int relax_char;
7398
7399 create_insn (insn, opcode);
7400 imm_expr.X_op = O_absent;
97d87491
RS
7401 offset_expr.X_op = O_absent;
7402 offset_reloc[0] = BFD_RELOC_UNUSED;
7403 offset_reloc[1] = BFD_RELOC_UNUSED;
7404 offset_reloc[2] = BFD_RELOC_UNUSED;
7405 relax_char = 0;
7406
7407 memset (&arg, 0, sizeof (arg));
7408 arg.insn = insn;
7409 arg.token = tokens;
7410 arg.argnum = 1;
7411 arg.last_regno = ILLEGAL_REG;
7412 arg.dest_regno = ILLEGAL_REG;
97d87491
RS
7413 relax_char = 0;
7414 for (args = opcode->args;; ++args)
7415 {
7416 int c;
7417
7418 if (arg.token->type == OT_END)
7419 {
7420 offsetT value;
7421
7422 /* Handle unary instructions in which only one operand is given.
7423 The source is then the same as the destination. */
7424 if (arg.opnum == 1 && *args == ',')
7425 {
7426 operand = decode_mips16_operand (args[1], FALSE);
7427 if (operand && mips_optional_operand_p (operand))
7428 {
7429 arg.token = tokens;
7430 arg.argnum = 1;
7431 continue;
7432 }
7433 }
7434
7435 /* Fail the match if there were too few operands. */
7436 if (*args)
7437 return FALSE;
7438
7439 /* Successful match. Stuff the immediate value in now, if
7440 we can. */
e3de51ce 7441 clear_insn_error ();
97d87491
RS
7442 if (opcode->pinfo == INSN_MACRO)
7443 {
7444 gas_assert (relax_char == 0 || relax_char == 'p');
7445 gas_assert (*offset_reloc == BFD_RELOC_UNUSED);
7446 }
7447 else if (relax_char
7448 && offset_expr.X_op == O_constant
7449 && calculate_reloc (*offset_reloc,
7450 offset_expr.X_add_number,
7451 &value))
7452 {
7453 mips16_immed (NULL, 0, relax_char, *offset_reloc, value,
7454 forced_insn_length, &insn->insn_opcode);
7455 offset_expr.X_op = O_absent;
7456 *offset_reloc = BFD_RELOC_UNUSED;
7457 }
7458 else if (relax_char && *offset_reloc != BFD_RELOC_UNUSED)
7459 {
7460 if (forced_insn_length == 2)
e3de51ce 7461 set_insn_error (0, _("invalid unextended operand value"));
97d87491
RS
7462 forced_insn_length = 4;
7463 insn->insn_opcode |= MIPS16_EXTEND;
7464 }
7465 else if (relax_char)
7466 *offset_reloc = (int) BFD_RELOC_UNUSED + relax_char;
7467
7468 check_completed_insn (&arg);
7469 return TRUE;
7470 }
7471
7472 /* Fail the match if the line has too many operands. */
7473 if (*args == 0)
7474 return FALSE;
7475
7476 /* Handle characters that need to match exactly. */
7477 if (*args == '(' || *args == ')' || *args == ',')
7478 {
7479 if (match_char (&arg, *args))
7480 continue;
7481 return FALSE;
7482 }
7483
7484 arg.opnum += 1;
7485 c = *args;
7486 switch (c)
7487 {
7488 case 'p':
7489 case 'q':
7490 case 'A':
7491 case 'B':
7492 case 'E':
7493 relax_char = c;
7494 break;
7495
7496 case 'I':
1a00e612
RS
7497 if (!match_const_int (&arg, &imm_expr.X_add_number))
7498 return FALSE;
7499 imm_expr.X_op = O_constant;
97d87491
RS
7500 if (HAVE_32BIT_GPRS)
7501 normalize_constant_expr (&imm_expr);
7502 continue;
7503
7504 case 'a':
7505 case 'i':
7506 *offset_reloc = BFD_RELOC_MIPS16_JMP;
7507 insn->insn_opcode <<= 16;
7508 break;
7509 }
7510
7511 operand = decode_mips16_operand (c, FALSE);
7512 if (!operand)
7513 abort ();
7514
7515 /* '6' is a special case. It is used for BREAK and SDBBP,
7516 whose operands are only meaningful to the software that decodes
7517 them. This means that there is no architectural reason why
7518 they cannot be prefixed by EXTEND, but in practice,
7519 exception handlers will only look at the instruction
7520 itself. We therefore allow '6' to be extended when
7521 disassembling but not when assembling. */
7522 if (operand->type != OP_PCREL && c != '6')
7523 {
7524 ext_operand = decode_mips16_operand (c, TRUE);
7525 if (operand != ext_operand)
7526 {
7527 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
7528 {
7529 offset_expr.X_op = O_constant;
7530 offset_expr.X_add_number = 0;
7531 relax_char = c;
7532 continue;
7533 }
7534
7535 /* We need the OT_INTEGER check because some MIPS16
7536 immediate variants are listed before the register ones. */
7537 if (arg.token->type != OT_INTEGER
7538 || !match_expression (&arg, &offset_expr, offset_reloc))
7539 return FALSE;
7540
7541 /* '8' is used for SLTI(U) and has traditionally not
7542 been allowed to take relocation operators. */
7543 if (offset_reloc[0] != BFD_RELOC_UNUSED
7544 && (ext_operand->size != 16 || c == '8'))
7545 return FALSE;
7546
7547 relax_char = c;
7548 continue;
7549 }
7550 }
7551
7552 if (mips_optional_operand_p (operand)
7553 && args[1] == ','
7554 && (arg.token[0].type != OT_REG
7555 || arg.token[1].type == OT_END))
7556 {
7557 /* Assume that the register has been elided and is the
7558 same as the first operand. */
7559 arg.token = tokens;
7560 arg.argnum = 1;
7561 }
7562
7563 if (!match_operand (&arg, operand))
7564 return FALSE;
7565 }
7566}
7567
60f20e8b
RS
7568/* Record that the current instruction is invalid for the current ISA. */
7569
7570static void
7571match_invalid_for_isa (void)
7572{
7573 set_insn_error_ss
1661c76c 7574 (0, _("opcode not supported on this processor: %s (%s)"),
60f20e8b
RS
7575 mips_cpu_info_from_arch (mips_opts.arch)->name,
7576 mips_cpu_info_from_isa (mips_opts.isa)->name);
7577}
7578
7579/* Try to match TOKENS against a series of opcode entries, starting at FIRST.
7580 Return true if a definite match or failure was found, storing any match
7581 in INSN. OPCODE_EXTRA is a value that should be ORed into the opcode
7582 (to handle things like VU0 suffixes). LAX_MATCH is true if we have already
7583 tried and failed to match under normal conditions and now want to try a
7584 more relaxed match. */
7585
7586static bfd_boolean
7587match_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
7588 const struct mips_opcode *past, struct mips_operand_token *tokens,
7589 int opcode_extra, bfd_boolean lax_match)
7590{
7591 const struct mips_opcode *opcode;
7592 const struct mips_opcode *invalid_delay_slot;
7593 bfd_boolean seen_valid_for_isa, seen_valid_for_size;
7594
7595 /* Search for a match, ignoring alternatives that don't satisfy the
7596 current ISA or forced_length. */
7597 invalid_delay_slot = 0;
7598 seen_valid_for_isa = FALSE;
7599 seen_valid_for_size = FALSE;
7600 opcode = first;
7601 do
7602 {
7603 gas_assert (strcmp (opcode->name, first->name) == 0);
7604 if (is_opcode_valid (opcode))
7605 {
7606 seen_valid_for_isa = TRUE;
7607 if (is_size_valid (opcode))
7608 {
7609 bfd_boolean delay_slot_ok;
7610
7611 seen_valid_for_size = TRUE;
7612 delay_slot_ok = is_delay_slot_valid (opcode);
7613 if (match_insn (insn, opcode, tokens, opcode_extra,
7614 lax_match, delay_slot_ok))
7615 {
7616 if (!delay_slot_ok)
7617 {
7618 if (!invalid_delay_slot)
7619 invalid_delay_slot = opcode;
7620 }
7621 else
7622 return TRUE;
7623 }
7624 }
7625 }
7626 ++opcode;
7627 }
7628 while (opcode < past && strcmp (opcode->name, first->name) == 0);
7629
7630 /* If the only matches we found had the wrong length for the delay slot,
7631 pick the first such match. We'll issue an appropriate warning later. */
7632 if (invalid_delay_slot)
7633 {
7634 if (match_insn (insn, invalid_delay_slot, tokens, opcode_extra,
7635 lax_match, TRUE))
7636 return TRUE;
7637 abort ();
7638 }
7639
7640 /* Handle the case where we didn't try to match an instruction because
7641 all the alternatives were incompatible with the current ISA. */
7642 if (!seen_valid_for_isa)
7643 {
7644 match_invalid_for_isa ();
7645 return TRUE;
7646 }
7647
7648 /* Handle the case where we didn't try to match an instruction because
7649 all the alternatives were of the wrong size. */
7650 if (!seen_valid_for_size)
7651 {
7652 if (mips_opts.insn32)
1661c76c 7653 set_insn_error (0, _("opcode not supported in the `insn32' mode"));
60f20e8b
RS
7654 else
7655 set_insn_error_i
1661c76c 7656 (0, _("unrecognized %d-bit version of microMIPS opcode"),
60f20e8b
RS
7657 8 * forced_insn_length);
7658 return TRUE;
7659 }
7660
7661 return FALSE;
7662}
7663
7664/* Like match_insns, but for MIPS16. */
7665
7666static bfd_boolean
7667match_mips16_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
7668 struct mips_operand_token *tokens)
7669{
7670 const struct mips_opcode *opcode;
7671 bfd_boolean seen_valid_for_isa;
7672
7673 /* Search for a match, ignoring alternatives that don't satisfy the
7674 current ISA. There are no separate entries for extended forms so
7675 we deal with forced_length later. */
7676 seen_valid_for_isa = FALSE;
7677 opcode = first;
7678 do
7679 {
7680 gas_assert (strcmp (opcode->name, first->name) == 0);
7681 if (is_opcode_valid_16 (opcode))
7682 {
7683 seen_valid_for_isa = TRUE;
7684 if (match_mips16_insn (insn, opcode, tokens))
7685 return TRUE;
7686 }
7687 ++opcode;
7688 }
7689 while (opcode < &mips16_opcodes[bfd_mips16_num_opcodes]
7690 && strcmp (opcode->name, first->name) == 0);
7691
7692 /* Handle the case where we didn't try to match an instruction because
7693 all the alternatives were incompatible with the current ISA. */
7694 if (!seen_valid_for_isa)
7695 {
7696 match_invalid_for_isa ();
7697 return TRUE;
7698 }
7699
7700 return FALSE;
7701}
7702
584892a6
RS
7703/* Set up global variables for the start of a new macro. */
7704
7705static void
7706macro_start (void)
7707{
7708 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
df58fc94
RS
7709 memset (&mips_macro_warning.first_insn_sizes, 0,
7710 sizeof (mips_macro_warning.first_insn_sizes));
7711 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
584892a6 7712 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
11625dd8 7713 && delayed_branch_p (&history[0]));
df58fc94
RS
7714 switch (history[0].insn_mo->pinfo2
7715 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
7716 {
7717 case INSN2_BRANCH_DELAY_32BIT:
7718 mips_macro_warning.delay_slot_length = 4;
7719 break;
7720 case INSN2_BRANCH_DELAY_16BIT:
7721 mips_macro_warning.delay_slot_length = 2;
7722 break;
7723 default:
7724 mips_macro_warning.delay_slot_length = 0;
7725 break;
7726 }
7727 mips_macro_warning.first_frag = NULL;
584892a6
RS
7728}
7729
df58fc94
RS
7730/* Given that a macro is longer than one instruction or of the wrong size,
7731 return the appropriate warning for it. Return null if no warning is
7732 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
7733 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
7734 and RELAX_NOMACRO. */
584892a6
RS
7735
7736static const char *
7737macro_warning (relax_substateT subtype)
7738{
7739 if (subtype & RELAX_DELAY_SLOT)
1661c76c 7740 return _("macro instruction expanded into multiple instructions"
584892a6
RS
7741 " in a branch delay slot");
7742 else if (subtype & RELAX_NOMACRO)
1661c76c 7743 return _("macro instruction expanded into multiple instructions");
df58fc94
RS
7744 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
7745 | RELAX_DELAY_SLOT_SIZE_SECOND))
7746 return ((subtype & RELAX_DELAY_SLOT_16BIT)
1661c76c 7747 ? _("macro instruction expanded into a wrong size instruction"
df58fc94 7748 " in a 16-bit branch delay slot")
1661c76c 7749 : _("macro instruction expanded into a wrong size instruction"
df58fc94 7750 " in a 32-bit branch delay slot"));
584892a6
RS
7751 else
7752 return 0;
7753}
7754
7755/* Finish up a macro. Emit warnings as appropriate. */
7756
7757static void
7758macro_end (void)
7759{
df58fc94
RS
7760 /* Relaxation warning flags. */
7761 relax_substateT subtype = 0;
7762
7763 /* Check delay slot size requirements. */
7764 if (mips_macro_warning.delay_slot_length == 2)
7765 subtype |= RELAX_DELAY_SLOT_16BIT;
7766 if (mips_macro_warning.delay_slot_length != 0)
584892a6 7767 {
df58fc94
RS
7768 if (mips_macro_warning.delay_slot_length
7769 != mips_macro_warning.first_insn_sizes[0])
7770 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
7771 if (mips_macro_warning.delay_slot_length
7772 != mips_macro_warning.first_insn_sizes[1])
7773 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
7774 }
584892a6 7775
df58fc94
RS
7776 /* Check instruction count requirements. */
7777 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
7778 {
7779 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
584892a6
RS
7780 subtype |= RELAX_SECOND_LONGER;
7781 if (mips_opts.warn_about_macros)
7782 subtype |= RELAX_NOMACRO;
7783 if (mips_macro_warning.delay_slot_p)
7784 subtype |= RELAX_DELAY_SLOT;
df58fc94 7785 }
584892a6 7786
df58fc94
RS
7787 /* If both alternatives fail to fill a delay slot correctly,
7788 emit the warning now. */
7789 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
7790 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
7791 {
7792 relax_substateT s;
7793 const char *msg;
7794
7795 s = subtype & (RELAX_DELAY_SLOT_16BIT
7796 | RELAX_DELAY_SLOT_SIZE_FIRST
7797 | RELAX_DELAY_SLOT_SIZE_SECOND);
7798 msg = macro_warning (s);
7799 if (msg != NULL)
7800 as_warn ("%s", msg);
7801 subtype &= ~s;
7802 }
7803
7804 /* If both implementations are longer than 1 instruction, then emit the
7805 warning now. */
7806 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
7807 {
7808 relax_substateT s;
7809 const char *msg;
7810
7811 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
7812 msg = macro_warning (s);
7813 if (msg != NULL)
7814 as_warn ("%s", msg);
7815 subtype &= ~s;
584892a6 7816 }
df58fc94
RS
7817
7818 /* If any flags still set, then one implementation might need a warning
7819 and the other either will need one of a different kind or none at all.
7820 Pass any remaining flags over to relaxation. */
7821 if (mips_macro_warning.first_frag != NULL)
7822 mips_macro_warning.first_frag->fr_subtype |= subtype;
584892a6
RS
7823}
7824
df58fc94
RS
7825/* Instruction operand formats used in macros that vary between
7826 standard MIPS and microMIPS code. */
7827
833794fc 7828static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
df58fc94
RS
7829static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
7830static const char * const jalr_fmt[2] = { "d,s", "t,s" };
7831static const char * const lui_fmt[2] = { "t,u", "s,u" };
7832static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
833794fc 7833static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
df58fc94
RS
7834static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
7835static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
7836
833794fc 7837#define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
df58fc94
RS
7838#define COP12_FMT (cop12_fmt[mips_opts.micromips])
7839#define JALR_FMT (jalr_fmt[mips_opts.micromips])
7840#define LUI_FMT (lui_fmt[mips_opts.micromips])
7841#define MEM12_FMT (mem12_fmt[mips_opts.micromips])
833794fc 7842#define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
df58fc94
RS
7843#define SHFT_FMT (shft_fmt[mips_opts.micromips])
7844#define TRAP_FMT (trap_fmt[mips_opts.micromips])
7845
6e1304d8
RS
7846/* Read a macro's relocation codes from *ARGS and store them in *R.
7847 The first argument in *ARGS will be either the code for a single
7848 relocation or -1 followed by the three codes that make up a
7849 composite relocation. */
7850
7851static void
7852macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
7853{
7854 int i, next;
7855
7856 next = va_arg (*args, int);
7857 if (next >= 0)
7858 r[0] = (bfd_reloc_code_real_type) next;
7859 else
f2ae14a1
RS
7860 {
7861 for (i = 0; i < 3; i++)
7862 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
7863 /* This function is only used for 16-bit relocation fields.
7864 To make the macro code simpler, treat an unrelocated value
7865 in the same way as BFD_RELOC_LO16. */
7866 if (r[0] == BFD_RELOC_UNUSED)
7867 r[0] = BFD_RELOC_LO16;
7868 }
6e1304d8
RS
7869}
7870
252b5132
RH
7871/* Build an instruction created by a macro expansion. This is passed
7872 a pointer to the count of instructions created so far, an
7873 expression, the name of the instruction to build, an operand format
7874 string, and corresponding arguments. */
7875
252b5132 7876static void
67c0d1eb 7877macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 7878{
df58fc94 7879 const struct mips_opcode *mo = NULL;
f6688943 7880 bfd_reloc_code_real_type r[3];
df58fc94 7881 const struct mips_opcode *amo;
e077a1c8 7882 const struct mips_operand *operand;
df58fc94
RS
7883 struct hash_control *hash;
7884 struct mips_cl_insn insn;
252b5132 7885 va_list args;
e077a1c8 7886 unsigned int uval;
252b5132 7887
252b5132 7888 va_start (args, fmt);
252b5132 7889
252b5132
RH
7890 if (mips_opts.mips16)
7891 {
03ea81db 7892 mips16_macro_build (ep, name, fmt, &args);
252b5132
RH
7893 va_end (args);
7894 return;
7895 }
7896
f6688943
TS
7897 r[0] = BFD_RELOC_UNUSED;
7898 r[1] = BFD_RELOC_UNUSED;
7899 r[2] = BFD_RELOC_UNUSED;
df58fc94
RS
7900 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
7901 amo = (struct mips_opcode *) hash_find (hash, name);
7902 gas_assert (amo);
7903 gas_assert (strcmp (name, amo->name) == 0);
1e915849 7904
df58fc94 7905 do
8b082fb1
TS
7906 {
7907 /* Search until we get a match for NAME. It is assumed here that
df58fc94
RS
7908 macros will never generate MDMX, MIPS-3D, or MT instructions.
7909 We try to match an instruction that fulfils the branch delay
7910 slot instruction length requirement (if any) of the previous
7911 instruction. While doing this we record the first instruction
7912 seen that matches all the other conditions and use it anyway
7913 if the requirement cannot be met; we will issue an appropriate
7914 warning later on. */
7915 if (strcmp (fmt, amo->args) == 0
7916 && amo->pinfo != INSN_MACRO
7917 && is_opcode_valid (amo)
7918 && is_size_valid (amo))
7919 {
7920 if (is_delay_slot_valid (amo))
7921 {
7922 mo = amo;
7923 break;
7924 }
7925 else if (!mo)
7926 mo = amo;
7927 }
8b082fb1 7928
df58fc94
RS
7929 ++amo;
7930 gas_assert (amo->name);
252b5132 7931 }
df58fc94 7932 while (strcmp (name, amo->name) == 0);
252b5132 7933
df58fc94 7934 gas_assert (mo);
1e915849 7935 create_insn (&insn, mo);
e077a1c8 7936 for (; *fmt; ++fmt)
252b5132 7937 {
e077a1c8 7938 switch (*fmt)
252b5132 7939 {
252b5132
RH
7940 case ',':
7941 case '(':
7942 case ')':
252b5132 7943 case 'z':
e077a1c8 7944 break;
252b5132
RH
7945
7946 case 'i':
7947 case 'j':
6e1304d8 7948 macro_read_relocs (&args, r);
9c2799c2 7949 gas_assert (*r == BFD_RELOC_GPREL16
e391c024
RS
7950 || *r == BFD_RELOC_MIPS_HIGHER
7951 || *r == BFD_RELOC_HI16_S
7952 || *r == BFD_RELOC_LO16
7953 || *r == BFD_RELOC_MIPS_GOT_OFST);
e077a1c8 7954 break;
e391c024
RS
7955
7956 case 'o':
7957 macro_read_relocs (&args, r);
e077a1c8 7958 break;
252b5132
RH
7959
7960 case 'u':
6e1304d8 7961 macro_read_relocs (&args, r);
9c2799c2 7962 gas_assert (ep != NULL
90ecf173
MR
7963 && (ep->X_op == O_constant
7964 || (ep->X_op == O_symbol
7965 && (*r == BFD_RELOC_MIPS_HIGHEST
7966 || *r == BFD_RELOC_HI16_S
7967 || *r == BFD_RELOC_HI16
7968 || *r == BFD_RELOC_GPREL16
7969 || *r == BFD_RELOC_MIPS_GOT_HI16
7970 || *r == BFD_RELOC_MIPS_CALL_HI16))));
e077a1c8 7971 break;
252b5132
RH
7972
7973 case 'p':
9c2799c2 7974 gas_assert (ep != NULL);
bad36eac 7975
252b5132
RH
7976 /*
7977 * This allows macro() to pass an immediate expression for
7978 * creating short branches without creating a symbol.
bad36eac
DJ
7979 *
7980 * We don't allow branch relaxation for these branches, as
7981 * they should only appear in ".set nomacro" anyway.
252b5132
RH
7982 */
7983 if (ep->X_op == O_constant)
7984 {
df58fc94
RS
7985 /* For microMIPS we always use relocations for branches.
7986 So we should not resolve immediate values. */
7987 gas_assert (!mips_opts.micromips);
7988
bad36eac
DJ
7989 if ((ep->X_add_number & 3) != 0)
7990 as_bad (_("branch to misaligned address (0x%lx)"),
7991 (unsigned long) ep->X_add_number);
7992 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
7993 as_bad (_("branch address range overflow (0x%lx)"),
7994 (unsigned long) ep->X_add_number);
252b5132
RH
7995 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
7996 ep = NULL;
7997 }
7998 else
0b25d3e6 7999 *r = BFD_RELOC_16_PCREL_S2;
e077a1c8 8000 break;
252b5132
RH
8001
8002 case 'a':
9c2799c2 8003 gas_assert (ep != NULL);
f6688943 8004 *r = BFD_RELOC_MIPS_JMP;
e077a1c8 8005 break;
d43b4baf 8006
252b5132 8007 default:
e077a1c8
RS
8008 operand = (mips_opts.micromips
8009 ? decode_micromips_operand (fmt)
8010 : decode_mips_operand (fmt));
8011 if (!operand)
8012 abort ();
8013
8014 uval = va_arg (args, int);
8015 if (operand->type == OP_CLO_CLZ_DEST)
8016 uval |= (uval << 5);
8017 insn_insert_operand (&insn, operand, uval);
8018
8019 if (*fmt == '+' || *fmt == 'm')
8020 ++fmt;
8021 break;
252b5132 8022 }
252b5132
RH
8023 }
8024 va_end (args);
9c2799c2 8025 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 8026
df58fc94 8027 append_insn (&insn, ep, r, TRUE);
252b5132
RH
8028}
8029
8030static void
67c0d1eb 8031mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
03ea81db 8032 va_list *args)
252b5132 8033{
1e915849 8034 struct mips_opcode *mo;
252b5132 8035 struct mips_cl_insn insn;
e077a1c8 8036 const struct mips_operand *operand;
f6688943
TS
8037 bfd_reloc_code_real_type r[3]
8038 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 8039
1e915849 8040 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
9c2799c2
NC
8041 gas_assert (mo);
8042 gas_assert (strcmp (name, mo->name) == 0);
252b5132 8043
1e915849 8044 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 8045 {
1e915849 8046 ++mo;
9c2799c2
NC
8047 gas_assert (mo->name);
8048 gas_assert (strcmp (name, mo->name) == 0);
252b5132
RH
8049 }
8050
1e915849 8051 create_insn (&insn, mo);
e077a1c8 8052 for (; *fmt; ++fmt)
252b5132
RH
8053 {
8054 int c;
8055
e077a1c8 8056 c = *fmt;
252b5132
RH
8057 switch (c)
8058 {
252b5132
RH
8059 case ',':
8060 case '(':
8061 case ')':
e077a1c8 8062 break;
252b5132
RH
8063
8064 case '0':
8065 case 'S':
8066 case 'P':
8067 case 'R':
e077a1c8 8068 break;
252b5132
RH
8069
8070 case '<':
8071 case '>':
8072 case '4':
8073 case '5':
8074 case 'H':
8075 case 'W':
8076 case 'D':
8077 case 'j':
8078 case '8':
8079 case 'V':
8080 case 'C':
8081 case 'U':
8082 case 'k':
8083 case 'K':
8084 case 'p':
8085 case 'q':
8086 {
b886a2ab
RS
8087 offsetT value;
8088
9c2799c2 8089 gas_assert (ep != NULL);
252b5132
RH
8090
8091 if (ep->X_op != O_constant)
874e8986 8092 *r = (int) BFD_RELOC_UNUSED + c;
b886a2ab 8093 else if (calculate_reloc (*r, ep->X_add_number, &value))
252b5132 8094 {
b886a2ab 8095 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
252b5132 8096 ep = NULL;
f6688943 8097 *r = BFD_RELOC_UNUSED;
252b5132
RH
8098 }
8099 }
e077a1c8 8100 break;
252b5132 8101
e077a1c8
RS
8102 default:
8103 operand = decode_mips16_operand (c, FALSE);
8104 if (!operand)
8105 abort ();
252b5132 8106
4a06e5a2 8107 insn_insert_operand (&insn, operand, va_arg (*args, int));
e077a1c8
RS
8108 break;
8109 }
252b5132
RH
8110 }
8111
9c2799c2 8112 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 8113
df58fc94 8114 append_insn (&insn, ep, r, TRUE);
252b5132
RH
8115}
8116
438c16b8
TS
8117/*
8118 * Generate a "jalr" instruction with a relocation hint to the called
8119 * function. This occurs in NewABI PIC code.
8120 */
8121static void
df58fc94 8122macro_build_jalr (expressionS *ep, int cprestore)
438c16b8 8123{
df58fc94
RS
8124 static const bfd_reloc_code_real_type jalr_relocs[2]
8125 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
8126 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
8127 const char *jalr;
685736be 8128 char *f = NULL;
b34976b6 8129
1180b5a4 8130 if (MIPS_JALR_HINT_P (ep))
f21f8242 8131 {
cc3d92a5 8132 frag_grow (8);
f21f8242
AO
8133 f = frag_more (0);
8134 }
2906b037 8135 if (mips_opts.micromips)
df58fc94 8136 {
833794fc
MR
8137 jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
8138 ? "jalr" : "jalrs");
e64af278 8139 if (MIPS_JALR_HINT_P (ep)
833794fc 8140 || mips_opts.insn32
e64af278 8141 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
8142 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
8143 else
8144 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
8145 }
2906b037
MR
8146 else
8147 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
1180b5a4 8148 if (MIPS_JALR_HINT_P (ep))
df58fc94 8149 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
438c16b8
TS
8150}
8151
252b5132
RH
8152/*
8153 * Generate a "lui" instruction.
8154 */
8155static void
67c0d1eb 8156macro_build_lui (expressionS *ep, int regnum)
252b5132 8157{
9c2799c2 8158 gas_assert (! mips_opts.mips16);
252b5132 8159
df58fc94 8160 if (ep->X_op != O_constant)
252b5132 8161 {
9c2799c2 8162 gas_assert (ep->X_op == O_symbol);
bbe506e8
TS
8163 /* _gp_disp is a special case, used from s_cpload.
8164 __gnu_local_gp is used if mips_no_shared. */
9c2799c2 8165 gas_assert (mips_pic == NO_PIC
78e1bb40 8166 || (! HAVE_NEWABI
aa6975fb
ILT
8167 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
8168 || (! mips_in_shared
bbe506e8
TS
8169 && strcmp (S_GET_NAME (ep->X_add_symbol),
8170 "__gnu_local_gp") == 0));
252b5132
RH
8171 }
8172
df58fc94 8173 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
252b5132
RH
8174}
8175
885add95
CD
8176/* Generate a sequence of instructions to do a load or store from a constant
8177 offset off of a base register (breg) into/from a target register (treg),
8178 using AT if necessary. */
8179static void
67c0d1eb
RS
8180macro_build_ldst_constoffset (expressionS *ep, const char *op,
8181 int treg, int breg, int dbl)
885add95 8182{
9c2799c2 8183 gas_assert (ep->X_op == O_constant);
885add95 8184
256ab948 8185 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
8186 if (!dbl)
8187 normalize_constant_expr (ep);
256ab948 8188
67c1ffbe 8189 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 8190 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
8191 as_warn (_("operand overflow"));
8192
8193 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
8194 {
8195 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 8196 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
8197 }
8198 else
8199 {
8200 /* 32-bit offset, need multiple instructions and AT, like:
8201 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
8202 addu $tempreg,$tempreg,$breg
8203 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
8204 to handle the complete offset. */
67c0d1eb
RS
8205 macro_build_lui (ep, AT);
8206 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
8207 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95 8208
741fe287 8209 if (!mips_opts.at)
1661c76c 8210 as_bad (_("macro used $at after \".set noat\""));
885add95
CD
8211 }
8212}
8213
252b5132
RH
8214/* set_at()
8215 * Generates code to set the $at register to true (one)
8216 * if reg is less than the immediate expression.
8217 */
8218static void
67c0d1eb 8219set_at (int reg, int unsignedp)
252b5132 8220{
b0e6f033 8221 if (imm_expr.X_add_number >= -0x8000
252b5132 8222 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
8223 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
8224 AT, reg, BFD_RELOC_LO16);
252b5132
RH
8225 else
8226 {
67c0d1eb
RS
8227 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
8228 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
8229 }
8230}
8231
252b5132
RH
8232/* Count the leading zeroes by performing a binary chop. This is a
8233 bulky bit of source, but performance is a LOT better for the
8234 majority of values than a simple loop to count the bits:
8235 for (lcnt = 0; (lcnt < 32); lcnt++)
8236 if ((v) & (1 << (31 - lcnt)))
8237 break;
8238 However it is not code size friendly, and the gain will drop a bit
8239 on certain cached systems.
8240*/
8241#define COUNT_TOP_ZEROES(v) \
8242 (((v) & ~0xffff) == 0 \
8243 ? ((v) & ~0xff) == 0 \
8244 ? ((v) & ~0xf) == 0 \
8245 ? ((v) & ~0x3) == 0 \
8246 ? ((v) & ~0x1) == 0 \
8247 ? !(v) \
8248 ? 32 \
8249 : 31 \
8250 : 30 \
8251 : ((v) & ~0x7) == 0 \
8252 ? 29 \
8253 : 28 \
8254 : ((v) & ~0x3f) == 0 \
8255 ? ((v) & ~0x1f) == 0 \
8256 ? 27 \
8257 : 26 \
8258 : ((v) & ~0x7f) == 0 \
8259 ? 25 \
8260 : 24 \
8261 : ((v) & ~0xfff) == 0 \
8262 ? ((v) & ~0x3ff) == 0 \
8263 ? ((v) & ~0x1ff) == 0 \
8264 ? 23 \
8265 : 22 \
8266 : ((v) & ~0x7ff) == 0 \
8267 ? 21 \
8268 : 20 \
8269 : ((v) & ~0x3fff) == 0 \
8270 ? ((v) & ~0x1fff) == 0 \
8271 ? 19 \
8272 : 18 \
8273 : ((v) & ~0x7fff) == 0 \
8274 ? 17 \
8275 : 16 \
8276 : ((v) & ~0xffffff) == 0 \
8277 ? ((v) & ~0xfffff) == 0 \
8278 ? ((v) & ~0x3ffff) == 0 \
8279 ? ((v) & ~0x1ffff) == 0 \
8280 ? 15 \
8281 : 14 \
8282 : ((v) & ~0x7ffff) == 0 \
8283 ? 13 \
8284 : 12 \
8285 : ((v) & ~0x3fffff) == 0 \
8286 ? ((v) & ~0x1fffff) == 0 \
8287 ? 11 \
8288 : 10 \
8289 : ((v) & ~0x7fffff) == 0 \
8290 ? 9 \
8291 : 8 \
8292 : ((v) & ~0xfffffff) == 0 \
8293 ? ((v) & ~0x3ffffff) == 0 \
8294 ? ((v) & ~0x1ffffff) == 0 \
8295 ? 7 \
8296 : 6 \
8297 : ((v) & ~0x7ffffff) == 0 \
8298 ? 5 \
8299 : 4 \
8300 : ((v) & ~0x3fffffff) == 0 \
8301 ? ((v) & ~0x1fffffff) == 0 \
8302 ? 3 \
8303 : 2 \
8304 : ((v) & ~0x7fffffff) == 0 \
8305 ? 1 \
8306 : 0)
8307
8308/* load_register()
67c1ffbe 8309 * This routine generates the least number of instructions necessary to load
252b5132
RH
8310 * an absolute expression value into a register.
8311 */
8312static void
67c0d1eb 8313load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
8314{
8315 int freg;
8316 expressionS hi32, lo32;
8317
8318 if (ep->X_op != O_big)
8319 {
9c2799c2 8320 gas_assert (ep->X_op == O_constant);
256ab948
TS
8321
8322 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
8323 if (!dbl)
8324 normalize_constant_expr (ep);
256ab948
TS
8325
8326 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
8327 {
8328 /* We can handle 16 bit signed values with an addiu to
8329 $zero. No need to ever use daddiu here, since $zero and
8330 the result are always correct in 32 bit mode. */
67c0d1eb 8331 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
8332 return;
8333 }
8334 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
8335 {
8336 /* We can handle 16 bit unsigned values with an ori to
8337 $zero. */
67c0d1eb 8338 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
8339 return;
8340 }
256ab948 8341 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
8342 {
8343 /* 32 bit values require an lui. */
df58fc94 8344 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 8345 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 8346 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
8347 return;
8348 }
8349 }
8350
8351 /* The value is larger than 32 bits. */
8352
2051e8c4 8353 if (!dbl || HAVE_32BIT_GPRS)
252b5132 8354 {
55e08f71
NC
8355 char value[32];
8356
8357 sprintf_vma (value, ep->X_add_number);
1661c76c 8358 as_bad (_("number (0x%s) larger than 32 bits"), value);
67c0d1eb 8359 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
8360 return;
8361 }
8362
8363 if (ep->X_op != O_big)
8364 {
8365 hi32 = *ep;
8366 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
8367 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
8368 hi32.X_add_number &= 0xffffffff;
8369 lo32 = *ep;
8370 lo32.X_add_number &= 0xffffffff;
8371 }
8372 else
8373 {
9c2799c2 8374 gas_assert (ep->X_add_number > 2);
252b5132
RH
8375 if (ep->X_add_number == 3)
8376 generic_bignum[3] = 0;
8377 else if (ep->X_add_number > 4)
1661c76c 8378 as_bad (_("number larger than 64 bits"));
252b5132
RH
8379 lo32.X_op = O_constant;
8380 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
8381 hi32.X_op = O_constant;
8382 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
8383 }
8384
8385 if (hi32.X_add_number == 0)
8386 freg = 0;
8387 else
8388 {
8389 int shift, bit;
8390 unsigned long hi, lo;
8391
956cd1d6 8392 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
8393 {
8394 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
8395 {
67c0d1eb 8396 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
8397 return;
8398 }
8399 if (lo32.X_add_number & 0x80000000)
8400 {
df58fc94 8401 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 8402 if (lo32.X_add_number & 0xffff)
67c0d1eb 8403 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
8404 return;
8405 }
8406 }
252b5132
RH
8407
8408 /* Check for 16bit shifted constant. We know that hi32 is
8409 non-zero, so start the mask on the first bit of the hi32
8410 value. */
8411 shift = 17;
8412 do
beae10d5
KH
8413 {
8414 unsigned long himask, lomask;
8415
8416 if (shift < 32)
8417 {
8418 himask = 0xffff >> (32 - shift);
8419 lomask = (0xffff << shift) & 0xffffffff;
8420 }
8421 else
8422 {
8423 himask = 0xffff << (shift - 32);
8424 lomask = 0;
8425 }
8426 if ((hi32.X_add_number & ~(offsetT) himask) == 0
8427 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
8428 {
8429 expressionS tmp;
8430
8431 tmp.X_op = O_constant;
8432 if (shift < 32)
8433 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
8434 | (lo32.X_add_number >> shift));
8435 else
8436 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb 8437 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
df58fc94 8438 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 8439 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
8440 return;
8441 }
f9419b05 8442 ++shift;
beae10d5
KH
8443 }
8444 while (shift <= (64 - 16));
252b5132
RH
8445
8446 /* Find the bit number of the lowest one bit, and store the
8447 shifted value in hi/lo. */
8448 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
8449 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
8450 if (lo != 0)
8451 {
8452 bit = 0;
8453 while ((lo & 1) == 0)
8454 {
8455 lo >>= 1;
8456 ++bit;
8457 }
8458 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
8459 hi >>= bit;
8460 }
8461 else
8462 {
8463 bit = 32;
8464 while ((hi & 1) == 0)
8465 {
8466 hi >>= 1;
8467 ++bit;
8468 }
8469 lo = hi;
8470 hi = 0;
8471 }
8472
8473 /* Optimize if the shifted value is a (power of 2) - 1. */
8474 if ((hi == 0 && ((lo + 1) & lo) == 0)
8475 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
8476 {
8477 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 8478 if (shift != 0)
beae10d5 8479 {
252b5132
RH
8480 expressionS tmp;
8481
8482 /* This instruction will set the register to be all
8483 ones. */
beae10d5
KH
8484 tmp.X_op = O_constant;
8485 tmp.X_add_number = (offsetT) -1;
67c0d1eb 8486 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
8487 if (bit != 0)
8488 {
8489 bit += shift;
df58fc94 8490 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 8491 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 8492 }
df58fc94 8493 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
67c0d1eb 8494 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
8495 return;
8496 }
8497 }
252b5132
RH
8498
8499 /* Sign extend hi32 before calling load_register, because we can
8500 generally get better code when we load a sign extended value. */
8501 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 8502 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 8503 load_register (reg, &hi32, 0);
252b5132
RH
8504 freg = reg;
8505 }
8506 if ((lo32.X_add_number & 0xffff0000) == 0)
8507 {
8508 if (freg != 0)
8509 {
df58fc94 8510 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
252b5132
RH
8511 freg = reg;
8512 }
8513 }
8514 else
8515 {
8516 expressionS mid16;
8517
956cd1d6 8518 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 8519 {
df58fc94
RS
8520 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
8521 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
beae10d5
KH
8522 return;
8523 }
252b5132
RH
8524
8525 if (freg != 0)
8526 {
df58fc94 8527 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
252b5132
RH
8528 freg = reg;
8529 }
8530 mid16 = lo32;
8531 mid16.X_add_number >>= 16;
67c0d1eb 8532 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
df58fc94 8533 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
252b5132
RH
8534 freg = reg;
8535 }
8536 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 8537 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
8538}
8539
269137b2
TS
8540static inline void
8541load_delay_nop (void)
8542{
8543 if (!gpr_interlocks)
8544 macro_build (NULL, "nop", "");
8545}
8546
252b5132
RH
8547/* Load an address into a register. */
8548
8549static void
67c0d1eb 8550load_address (int reg, expressionS *ep, int *used_at)
252b5132 8551{
252b5132
RH
8552 if (ep->X_op != O_constant
8553 && ep->X_op != O_symbol)
8554 {
8555 as_bad (_("expression too complex"));
8556 ep->X_op = O_constant;
8557 }
8558
8559 if (ep->X_op == O_constant)
8560 {
67c0d1eb 8561 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
8562 return;
8563 }
8564
8565 if (mips_pic == NO_PIC)
8566 {
8567 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 8568 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
8569 Otherwise we want
8570 lui $reg,<sym> (BFD_RELOC_HI16_S)
8571 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 8572 If we have an addend, we always use the latter form.
76b3015f 8573
d6bc6245
TS
8574 With 64bit address space and a usable $at we want
8575 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8576 lui $at,<sym> (BFD_RELOC_HI16_S)
8577 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
8578 daddiu $at,<sym> (BFD_RELOC_LO16)
8579 dsll32 $reg,0
3a482fd5 8580 daddu $reg,$reg,$at
76b3015f 8581
c03099e6 8582 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
8583 on superscalar processors.
8584 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8585 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
8586 dsll $reg,16
8587 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
8588 dsll $reg,16
8589 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
8590
8591 For GP relative symbols in 64bit address space we can use
8592 the same sequence as in 32bit address space. */
aed1a261 8593 if (HAVE_64BIT_SYMBOLS)
d6bc6245 8594 {
6caf9ef4
TS
8595 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
8596 && !nopic_need_relax (ep->X_add_symbol, 1))
8597 {
8598 relax_start (ep->X_add_symbol);
8599 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
8600 mips_gp_register, BFD_RELOC_GPREL16);
8601 relax_switch ();
8602 }
d6bc6245 8603
741fe287 8604 if (*used_at == 0 && mips_opts.at)
d6bc6245 8605 {
df58fc94
RS
8606 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
8607 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
67c0d1eb
RS
8608 macro_build (ep, "daddiu", "t,r,j", reg, reg,
8609 BFD_RELOC_MIPS_HIGHER);
8610 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
df58fc94 8611 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
67c0d1eb 8612 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
8613 *used_at = 1;
8614 }
8615 else
8616 {
df58fc94 8617 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb
RS
8618 macro_build (ep, "daddiu", "t,r,j", reg, reg,
8619 BFD_RELOC_MIPS_HIGHER);
df58fc94 8620 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 8621 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
df58fc94 8622 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 8623 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 8624 }
6caf9ef4
TS
8625
8626 if (mips_relax.sequence)
8627 relax_end ();
d6bc6245 8628 }
252b5132
RH
8629 else
8630 {
d6bc6245 8631 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 8632 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 8633 {
4d7206a2 8634 relax_start (ep->X_add_symbol);
67c0d1eb 8635 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 8636 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 8637 relax_switch ();
d6bc6245 8638 }
67c0d1eb
RS
8639 macro_build_lui (ep, reg);
8640 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
8641 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
8642 if (mips_relax.sequence)
8643 relax_end ();
d6bc6245 8644 }
252b5132 8645 }
0a44bf69 8646 else if (!mips_big_got)
252b5132
RH
8647 {
8648 expressionS ex;
8649
8650 /* If this is a reference to an external symbol, we want
8651 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8652 Otherwise we want
8653 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8654 nop
8655 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
8656 If there is a constant, it must be added in after.
8657
ed6fb7bd 8658 If we have NewABI, we want
f5040a92
AO
8659 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
8660 unless we're referencing a global symbol with a non-zero
8661 offset, in which case cst must be added separately. */
ed6fb7bd
SC
8662 if (HAVE_NEWABI)
8663 {
f5040a92
AO
8664 if (ep->X_add_number)
8665 {
4d7206a2 8666 ex.X_add_number = ep->X_add_number;
f5040a92 8667 ep->X_add_number = 0;
4d7206a2 8668 relax_start (ep->X_add_symbol);
67c0d1eb
RS
8669 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
8670 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
8671 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
8672 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
8673 ex.X_op = O_constant;
67c0d1eb 8674 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 8675 reg, reg, BFD_RELOC_LO16);
f5040a92 8676 ep->X_add_number = ex.X_add_number;
4d7206a2 8677 relax_switch ();
f5040a92 8678 }
67c0d1eb 8679 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 8680 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
8681 if (mips_relax.sequence)
8682 relax_end ();
ed6fb7bd
SC
8683 }
8684 else
8685 {
f5040a92
AO
8686 ex.X_add_number = ep->X_add_number;
8687 ep->X_add_number = 0;
67c0d1eb
RS
8688 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
8689 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 8690 load_delay_nop ();
4d7206a2
RS
8691 relax_start (ep->X_add_symbol);
8692 relax_switch ();
67c0d1eb 8693 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 8694 BFD_RELOC_LO16);
4d7206a2 8695 relax_end ();
ed6fb7bd 8696
f5040a92
AO
8697 if (ex.X_add_number != 0)
8698 {
8699 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
8700 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
8701 ex.X_op = O_constant;
67c0d1eb 8702 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 8703 reg, reg, BFD_RELOC_LO16);
f5040a92 8704 }
252b5132
RH
8705 }
8706 }
0a44bf69 8707 else if (mips_big_got)
252b5132
RH
8708 {
8709 expressionS ex;
252b5132
RH
8710
8711 /* This is the large GOT case. If this is a reference to an
8712 external symbol, we want
8713 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
8714 addu $reg,$reg,$gp
8715 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
8716
8717 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
8718 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8719 nop
8720 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 8721 If there is a constant, it must be added in after.
f5040a92
AO
8722
8723 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
8724 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
8725 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 8726 */
438c16b8
TS
8727 if (HAVE_NEWABI)
8728 {
4d7206a2 8729 ex.X_add_number = ep->X_add_number;
f5040a92 8730 ep->X_add_number = 0;
4d7206a2 8731 relax_start (ep->X_add_symbol);
df58fc94 8732 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
8733 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8734 reg, reg, mips_gp_register);
8735 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
8736 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
8737 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
8738 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
8739 else if (ex.X_add_number)
8740 {
8741 ex.X_op = O_constant;
67c0d1eb
RS
8742 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
8743 BFD_RELOC_LO16);
f5040a92
AO
8744 }
8745
8746 ep->X_add_number = ex.X_add_number;
4d7206a2 8747 relax_switch ();
67c0d1eb 8748 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 8749 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
8750 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
8751 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 8752 relax_end ();
438c16b8 8753 }
252b5132 8754 else
438c16b8 8755 {
f5040a92
AO
8756 ex.X_add_number = ep->X_add_number;
8757 ep->X_add_number = 0;
4d7206a2 8758 relax_start (ep->X_add_symbol);
df58fc94 8759 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
8760 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8761 reg, reg, mips_gp_register);
8762 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
8763 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
8764 relax_switch ();
8765 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
8766 {
8767 /* We need a nop before loading from $gp. This special
8768 check is required because the lui which starts the main
8769 instruction stream does not refer to $gp, and so will not
8770 insert the nop which may be required. */
67c0d1eb 8771 macro_build (NULL, "nop", "");
438c16b8 8772 }
67c0d1eb 8773 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 8774 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 8775 load_delay_nop ();
67c0d1eb 8776 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 8777 BFD_RELOC_LO16);
4d7206a2 8778 relax_end ();
438c16b8 8779
f5040a92
AO
8780 if (ex.X_add_number != 0)
8781 {
8782 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
8783 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
8784 ex.X_op = O_constant;
67c0d1eb
RS
8785 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
8786 BFD_RELOC_LO16);
f5040a92 8787 }
252b5132
RH
8788 }
8789 }
252b5132
RH
8790 else
8791 abort ();
8fc2e39e 8792
741fe287 8793 if (!mips_opts.at && *used_at == 1)
1661c76c 8794 as_bad (_("macro used $at after \".set noat\""));
252b5132
RH
8795}
8796
ea1fb5dc
RS
8797/* Move the contents of register SOURCE into register DEST. */
8798
8799static void
67c0d1eb 8800move_register (int dest, int source)
ea1fb5dc 8801{
df58fc94
RS
8802 /* Prefer to use a 16-bit microMIPS instruction unless the previous
8803 instruction specifically requires a 32-bit one. */
8804 if (mips_opts.micromips
833794fc 8805 && !mips_opts.insn32
df58fc94 8806 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7951ca42 8807 macro_build (NULL, "move", "mp,mj", dest, source);
df58fc94
RS
8808 else
8809 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
8810 dest, source, 0);
ea1fb5dc
RS
8811}
8812
4d7206a2 8813/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
8814 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
8815 The two alternatives are:
4d7206a2
RS
8816
8817 Global symbol Local sybmol
8818 ------------- ------------
8819 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
8820 ... ...
8821 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
8822
8823 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
8824 emits the second for a 16-bit offset or add_got_offset_hilo emits
8825 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
8826
8827static void
67c0d1eb 8828load_got_offset (int dest, expressionS *local)
4d7206a2
RS
8829{
8830 expressionS global;
8831
8832 global = *local;
8833 global.X_add_number = 0;
8834
8835 relax_start (local->X_add_symbol);
67c0d1eb
RS
8836 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
8837 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 8838 relax_switch ();
67c0d1eb
RS
8839 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
8840 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
8841 relax_end ();
8842}
8843
8844static void
67c0d1eb 8845add_got_offset (int dest, expressionS *local)
4d7206a2
RS
8846{
8847 expressionS global;
8848
8849 global.X_op = O_constant;
8850 global.X_op_symbol = NULL;
8851 global.X_add_symbol = NULL;
8852 global.X_add_number = local->X_add_number;
8853
8854 relax_start (local->X_add_symbol);
67c0d1eb 8855 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
8856 dest, dest, BFD_RELOC_LO16);
8857 relax_switch ();
67c0d1eb 8858 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
8859 relax_end ();
8860}
8861
f6a22291
MR
8862static void
8863add_got_offset_hilo (int dest, expressionS *local, int tmp)
8864{
8865 expressionS global;
8866 int hold_mips_optimize;
8867
8868 global.X_op = O_constant;
8869 global.X_op_symbol = NULL;
8870 global.X_add_symbol = NULL;
8871 global.X_add_number = local->X_add_number;
8872
8873 relax_start (local->X_add_symbol);
8874 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
8875 relax_switch ();
8876 /* Set mips_optimize around the lui instruction to avoid
8877 inserting an unnecessary nop after the lw. */
8878 hold_mips_optimize = mips_optimize;
8879 mips_optimize = 2;
8880 macro_build_lui (&global, tmp);
8881 mips_optimize = hold_mips_optimize;
8882 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
8883 relax_end ();
8884
8885 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
8886}
8887
df58fc94
RS
8888/* Emit a sequence of instructions to emulate a branch likely operation.
8889 BR is an ordinary branch corresponding to one to be emulated. BRNEG
8890 is its complementing branch with the original condition negated.
8891 CALL is set if the original branch specified the link operation.
8892 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
8893
8894 Code like this is produced in the noreorder mode:
8895
8896 BRNEG <args>, 1f
8897 nop
8898 b <sym>
8899 delay slot (executed only if branch taken)
8900 1:
8901
8902 or, if CALL is set:
8903
8904 BRNEG <args>, 1f
8905 nop
8906 bal <sym>
8907 delay slot (executed only if branch taken)
8908 1:
8909
8910 In the reorder mode the delay slot would be filled with a nop anyway,
8911 so code produced is simply:
8912
8913 BR <args>, <sym>
8914 nop
8915
8916 This function is used when producing code for the microMIPS ASE that
8917 does not implement branch likely instructions in hardware. */
8918
8919static void
8920macro_build_branch_likely (const char *br, const char *brneg,
8921 int call, expressionS *ep, const char *fmt,
8922 unsigned int sreg, unsigned int treg)
8923{
8924 int noreorder = mips_opts.noreorder;
8925 expressionS expr1;
8926
8927 gas_assert (mips_opts.micromips);
8928 start_noreorder ();
8929 if (noreorder)
8930 {
8931 micromips_label_expr (&expr1);
8932 macro_build (&expr1, brneg, fmt, sreg, treg);
8933 macro_build (NULL, "nop", "");
8934 macro_build (ep, call ? "bal" : "b", "p");
8935
8936 /* Set to true so that append_insn adds a label. */
8937 emit_branch_likely_macro = TRUE;
8938 }
8939 else
8940 {
8941 macro_build (ep, br, fmt, sreg, treg);
8942 macro_build (NULL, "nop", "");
8943 }
8944 end_noreorder ();
8945}
8946
8947/* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
8948 the condition code tested. EP specifies the branch target. */
8949
8950static void
8951macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
8952{
8953 const int call = 0;
8954 const char *brneg;
8955 const char *br;
8956
8957 switch (type)
8958 {
8959 case M_BC1FL:
8960 br = "bc1f";
8961 brneg = "bc1t";
8962 break;
8963 case M_BC1TL:
8964 br = "bc1t";
8965 brneg = "bc1f";
8966 break;
8967 case M_BC2FL:
8968 br = "bc2f";
8969 brneg = "bc2t";
8970 break;
8971 case M_BC2TL:
8972 br = "bc2t";
8973 brneg = "bc2f";
8974 break;
8975 default:
8976 abort ();
8977 }
8978 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
8979}
8980
8981/* Emit a two-argument branch macro specified by TYPE, using SREG as
8982 the register tested. EP specifies the branch target. */
8983
8984static void
8985macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
8986{
8987 const char *brneg = NULL;
8988 const char *br;
8989 int call = 0;
8990
8991 switch (type)
8992 {
8993 case M_BGEZ:
8994 br = "bgez";
8995 break;
8996 case M_BGEZL:
8997 br = mips_opts.micromips ? "bgez" : "bgezl";
8998 brneg = "bltz";
8999 break;
9000 case M_BGEZALL:
9001 gas_assert (mips_opts.micromips);
833794fc 9002 br = mips_opts.insn32 ? "bgezal" : "bgezals";
df58fc94
RS
9003 brneg = "bltz";
9004 call = 1;
9005 break;
9006 case M_BGTZ:
9007 br = "bgtz";
9008 break;
9009 case M_BGTZL:
9010 br = mips_opts.micromips ? "bgtz" : "bgtzl";
9011 brneg = "blez";
9012 break;
9013 case M_BLEZ:
9014 br = "blez";
9015 break;
9016 case M_BLEZL:
9017 br = mips_opts.micromips ? "blez" : "blezl";
9018 brneg = "bgtz";
9019 break;
9020 case M_BLTZ:
9021 br = "bltz";
9022 break;
9023 case M_BLTZL:
9024 br = mips_opts.micromips ? "bltz" : "bltzl";
9025 brneg = "bgez";
9026 break;
9027 case M_BLTZALL:
9028 gas_assert (mips_opts.micromips);
833794fc 9029 br = mips_opts.insn32 ? "bltzal" : "bltzals";
df58fc94
RS
9030 brneg = "bgez";
9031 call = 1;
9032 break;
9033 default:
9034 abort ();
9035 }
9036 if (mips_opts.micromips && brneg)
9037 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
9038 else
9039 macro_build (ep, br, "s,p", sreg);
9040}
9041
9042/* Emit a three-argument branch macro specified by TYPE, using SREG and
9043 TREG as the registers tested. EP specifies the branch target. */
9044
9045static void
9046macro_build_branch_rsrt (int type, expressionS *ep,
9047 unsigned int sreg, unsigned int treg)
9048{
9049 const char *brneg = NULL;
9050 const int call = 0;
9051 const char *br;
9052
9053 switch (type)
9054 {
9055 case M_BEQ:
9056 case M_BEQ_I:
9057 br = "beq";
9058 break;
9059 case M_BEQL:
9060 case M_BEQL_I:
9061 br = mips_opts.micromips ? "beq" : "beql";
9062 brneg = "bne";
9063 break;
9064 case M_BNE:
9065 case M_BNE_I:
9066 br = "bne";
9067 break;
9068 case M_BNEL:
9069 case M_BNEL_I:
9070 br = mips_opts.micromips ? "bne" : "bnel";
9071 brneg = "beq";
9072 break;
9073 default:
9074 abort ();
9075 }
9076 if (mips_opts.micromips && brneg)
9077 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
9078 else
9079 macro_build (ep, br, "s,t,p", sreg, treg);
9080}
9081
f2ae14a1
RS
9082/* Return the high part that should be loaded in order to make the low
9083 part of VALUE accessible using an offset of OFFBITS bits. */
9084
9085static offsetT
9086offset_high_part (offsetT value, unsigned int offbits)
9087{
9088 offsetT bias;
9089 addressT low_mask;
9090
9091 if (offbits == 0)
9092 return value;
9093 bias = 1 << (offbits - 1);
9094 low_mask = bias * 2 - 1;
9095 return (value + bias) & ~low_mask;
9096}
9097
9098/* Return true if the value stored in offset_expr and offset_reloc
9099 fits into a signed offset of OFFBITS bits. RANGE is the maximum
9100 amount that the caller wants to add without inducing overflow
9101 and ALIGN is the known alignment of the value in bytes. */
9102
9103static bfd_boolean
9104small_offset_p (unsigned int range, unsigned int align, unsigned int offbits)
9105{
9106 if (offbits == 16)
9107 {
9108 /* Accept any relocation operator if overflow isn't a concern. */
9109 if (range < align && *offset_reloc != BFD_RELOC_UNUSED)
9110 return TRUE;
9111
9112 /* These relocations are guaranteed not to overflow in correct links. */
9113 if (*offset_reloc == BFD_RELOC_MIPS_LITERAL
9114 || gprel16_reloc_p (*offset_reloc))
9115 return TRUE;
9116 }
9117 if (offset_expr.X_op == O_constant
9118 && offset_high_part (offset_expr.X_add_number, offbits) == 0
9119 && offset_high_part (offset_expr.X_add_number + range, offbits) == 0)
9120 return TRUE;
9121 return FALSE;
9122}
9123
252b5132
RH
9124/*
9125 * Build macros
9126 * This routine implements the seemingly endless macro or synthesized
9127 * instructions and addressing modes in the mips assembly language. Many
9128 * of these macros are simple and are similar to each other. These could
67c1ffbe 9129 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
9130 * this verbose method. Others are not simple macros but are more like
9131 * optimizing code generation.
9132 * One interesting optimization is when several store macros appear
67c1ffbe 9133 * consecutively that would load AT with the upper half of the same address.
252b5132
RH
9134 * The ensuing load upper instructions are ommited. This implies some kind
9135 * of global optimization. We currently only optimize within a single macro.
9136 * For many of the load and store macros if the address is specified as a
9137 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
9138 * first load register 'at' with zero and use it as the base register. The
9139 * mips assembler simply uses register $zero. Just one tiny optimization
9140 * we're missing.
9141 */
9142static void
833794fc 9143macro (struct mips_cl_insn *ip, char *str)
252b5132 9144{
c0ebe874
RS
9145 const struct mips_operand_array *operands;
9146 unsigned int breg, i;
741fe287 9147 unsigned int tempreg;
252b5132 9148 int mask;
43841e91 9149 int used_at = 0;
df58fc94 9150 expressionS label_expr;
252b5132 9151 expressionS expr1;
df58fc94 9152 expressionS *ep;
252b5132
RH
9153 const char *s;
9154 const char *s2;
9155 const char *fmt;
9156 int likely = 0;
252b5132 9157 int coproc = 0;
7f3c4072 9158 int offbits = 16;
1abe91b1 9159 int call = 0;
df58fc94
RS
9160 int jals = 0;
9161 int dbl = 0;
9162 int imm = 0;
9163 int ust = 0;
9164 int lp = 0;
f2ae14a1 9165 bfd_boolean large_offset;
252b5132 9166 int off;
252b5132 9167 int hold_mips_optimize;
f2ae14a1 9168 unsigned int align;
c0ebe874 9169 unsigned int op[MAX_OPERANDS];
252b5132 9170
9c2799c2 9171 gas_assert (! mips_opts.mips16);
252b5132 9172
c0ebe874
RS
9173 operands = insn_operands (ip);
9174 for (i = 0; i < MAX_OPERANDS; i++)
9175 if (operands->operand[i])
9176 op[i] = insn_extract_operand (ip, operands->operand[i]);
9177 else
9178 op[i] = -1;
9179
252b5132
RH
9180 mask = ip->insn_mo->mask;
9181
df58fc94
RS
9182 label_expr.X_op = O_constant;
9183 label_expr.X_op_symbol = NULL;
9184 label_expr.X_add_symbol = NULL;
9185 label_expr.X_add_number = 0;
9186
252b5132
RH
9187 expr1.X_op = O_constant;
9188 expr1.X_op_symbol = NULL;
9189 expr1.X_add_symbol = NULL;
9190 expr1.X_add_number = 1;
f2ae14a1 9191 align = 1;
252b5132
RH
9192
9193 switch (mask)
9194 {
9195 case M_DABS:
9196 dbl = 1;
9197 case M_ABS:
df58fc94
RS
9198 /* bgez $a0,1f
9199 move v0,$a0
9200 sub v0,$zero,$a0
9201 1:
9202 */
252b5132 9203
7d10b47d 9204 start_noreorder ();
252b5132 9205
df58fc94
RS
9206 if (mips_opts.micromips)
9207 micromips_label_expr (&label_expr);
9208 else
9209 label_expr.X_add_number = 8;
c0ebe874
RS
9210 macro_build (&label_expr, "bgez", "s,p", op[1]);
9211 if (op[0] == op[1])
a605d2b3 9212 macro_build (NULL, "nop", "");
252b5132 9213 else
c0ebe874
RS
9214 move_register (op[0], op[1]);
9215 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", op[0], 0, op[1]);
df58fc94
RS
9216 if (mips_opts.micromips)
9217 micromips_add_label ();
252b5132 9218
7d10b47d 9219 end_noreorder ();
8fc2e39e 9220 break;
252b5132
RH
9221
9222 case M_ADD_I:
9223 s = "addi";
9224 s2 = "add";
9225 goto do_addi;
9226 case M_ADDU_I:
9227 s = "addiu";
9228 s2 = "addu";
9229 goto do_addi;
9230 case M_DADD_I:
9231 dbl = 1;
9232 s = "daddi";
9233 s2 = "dadd";
df58fc94
RS
9234 if (!mips_opts.micromips)
9235 goto do_addi;
b0e6f033 9236 if (imm_expr.X_add_number >= -0x200
df58fc94
RS
9237 && imm_expr.X_add_number < 0x200)
9238 {
b0e6f033
RS
9239 macro_build (NULL, s, "t,r,.", op[0], op[1],
9240 (int) imm_expr.X_add_number);
df58fc94
RS
9241 break;
9242 }
9243 goto do_addi_i;
252b5132
RH
9244 case M_DADDU_I:
9245 dbl = 1;
9246 s = "daddiu";
9247 s2 = "daddu";
9248 do_addi:
b0e6f033 9249 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
9250 && imm_expr.X_add_number < 0x8000)
9251 {
c0ebe874 9252 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 9253 break;
252b5132 9254 }
df58fc94 9255 do_addi_i:
8fc2e39e 9256 used_at = 1;
67c0d1eb 9257 load_register (AT, &imm_expr, dbl);
c0ebe874 9258 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
9259 break;
9260
9261 case M_AND_I:
9262 s = "andi";
9263 s2 = "and";
9264 goto do_bit;
9265 case M_OR_I:
9266 s = "ori";
9267 s2 = "or";
9268 goto do_bit;
9269 case M_NOR_I:
9270 s = "";
9271 s2 = "nor";
9272 goto do_bit;
9273 case M_XOR_I:
9274 s = "xori";
9275 s2 = "xor";
9276 do_bit:
b0e6f033 9277 if (imm_expr.X_add_number >= 0
252b5132
RH
9278 && imm_expr.X_add_number < 0x10000)
9279 {
9280 if (mask != M_NOR_I)
c0ebe874 9281 macro_build (&imm_expr, s, "t,r,i", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
9282 else
9283 {
67c0d1eb 9284 macro_build (&imm_expr, "ori", "t,r,i",
c0ebe874
RS
9285 op[0], op[1], BFD_RELOC_LO16);
9286 macro_build (NULL, "nor", "d,v,t", op[0], op[0], 0);
252b5132 9287 }
8fc2e39e 9288 break;
252b5132
RH
9289 }
9290
8fc2e39e 9291 used_at = 1;
67c0d1eb 9292 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 9293 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
9294 break;
9295
8b082fb1
TS
9296 case M_BALIGN:
9297 switch (imm_expr.X_add_number)
9298 {
9299 case 0:
9300 macro_build (NULL, "nop", "");
9301 break;
9302 case 2:
c0ebe874 9303 macro_build (NULL, "packrl.ph", "d,s,t", op[0], op[0], op[1]);
8b082fb1 9304 break;
03f66e8a
MR
9305 case 1:
9306 case 3:
c0ebe874 9307 macro_build (NULL, "balign", "t,s,2", op[0], op[1],
90ecf173 9308 (int) imm_expr.X_add_number);
8b082fb1 9309 break;
03f66e8a
MR
9310 default:
9311 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
9312 (unsigned long) imm_expr.X_add_number);
9313 break;
8b082fb1
TS
9314 }
9315 break;
9316
df58fc94
RS
9317 case M_BC1FL:
9318 case M_BC1TL:
9319 case M_BC2FL:
9320 case M_BC2TL:
9321 gas_assert (mips_opts.micromips);
9322 macro_build_branch_ccl (mask, &offset_expr,
9323 EXTRACT_OPERAND (1, BCC, *ip));
9324 break;
9325
252b5132 9326 case M_BEQ_I:
252b5132 9327 case M_BEQL_I:
252b5132 9328 case M_BNE_I:
252b5132 9329 case M_BNEL_I:
b0e6f033 9330 if (imm_expr.X_add_number == 0)
c0ebe874 9331 op[1] = 0;
df58fc94 9332 else
252b5132 9333 {
c0ebe874 9334 op[1] = AT;
df58fc94 9335 used_at = 1;
c0ebe874 9336 load_register (op[1], &imm_expr, HAVE_64BIT_GPRS);
252b5132 9337 }
df58fc94
RS
9338 /* Fall through. */
9339 case M_BEQL:
9340 case M_BNEL:
c0ebe874 9341 macro_build_branch_rsrt (mask, &offset_expr, op[0], op[1]);
252b5132
RH
9342 break;
9343
9344 case M_BGEL:
9345 likely = 1;
9346 case M_BGE:
c0ebe874
RS
9347 if (op[1] == 0)
9348 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[0]);
9349 else if (op[0] == 0)
9350 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[1]);
df58fc94 9351 else
252b5132 9352 {
df58fc94 9353 used_at = 1;
c0ebe874 9354 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
9355 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
9356 &offset_expr, AT, ZERO);
252b5132 9357 }
df58fc94
RS
9358 break;
9359
9360 case M_BGEZL:
9361 case M_BGEZALL:
9362 case M_BGTZL:
9363 case M_BLEZL:
9364 case M_BLTZL:
9365 case M_BLTZALL:
c0ebe874 9366 macro_build_branch_rs (mask, &offset_expr, op[0]);
252b5132
RH
9367 break;
9368
9369 case M_BGTL_I:
9370 likely = 1;
9371 case M_BGT_I:
90ecf173 9372 /* Check for > max integer. */
b0e6f033 9373 if (imm_expr.X_add_number >= GPR_SMAX)
252b5132
RH
9374 {
9375 do_false:
90ecf173 9376 /* Result is always false. */
252b5132 9377 if (! likely)
a605d2b3 9378 macro_build (NULL, "nop", "");
252b5132 9379 else
df58fc94 9380 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
8fc2e39e 9381 break;
252b5132 9382 }
f9419b05 9383 ++imm_expr.X_add_number;
252b5132
RH
9384 /* FALLTHROUGH */
9385 case M_BGE_I:
9386 case M_BGEL_I:
9387 if (mask == M_BGEL_I)
9388 likely = 1;
b0e6f033 9389 if (imm_expr.X_add_number == 0)
252b5132 9390 {
df58fc94 9391 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
c0ebe874 9392 &offset_expr, op[0]);
8fc2e39e 9393 break;
252b5132 9394 }
b0e6f033 9395 if (imm_expr.X_add_number == 1)
252b5132 9396 {
df58fc94 9397 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
c0ebe874 9398 &offset_expr, op[0]);
8fc2e39e 9399 break;
252b5132 9400 }
b0e6f033 9401 if (imm_expr.X_add_number <= GPR_SMIN)
252b5132
RH
9402 {
9403 do_true:
9404 /* result is always true */
1661c76c 9405 as_warn (_("branch %s is always true"), ip->insn_mo->name);
67c0d1eb 9406 macro_build (&offset_expr, "b", "p");
8fc2e39e 9407 break;
252b5132 9408 }
8fc2e39e 9409 used_at = 1;
c0ebe874 9410 set_at (op[0], 0);
df58fc94
RS
9411 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
9412 &offset_expr, AT, ZERO);
252b5132
RH
9413 break;
9414
9415 case M_BGEUL:
9416 likely = 1;
9417 case M_BGEU:
c0ebe874 9418 if (op[1] == 0)
252b5132 9419 goto do_true;
c0ebe874 9420 else if (op[0] == 0)
df58fc94 9421 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 9422 &offset_expr, ZERO, op[1]);
df58fc94 9423 else
252b5132 9424 {
df58fc94 9425 used_at = 1;
c0ebe874 9426 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
9427 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
9428 &offset_expr, AT, ZERO);
252b5132 9429 }
252b5132
RH
9430 break;
9431
9432 case M_BGTUL_I:
9433 likely = 1;
9434 case M_BGTU_I:
c0ebe874 9435 if (op[0] == 0
ca4e0257 9436 || (HAVE_32BIT_GPRS
f01dc953 9437 && imm_expr.X_add_number == -1))
252b5132 9438 goto do_false;
f9419b05 9439 ++imm_expr.X_add_number;
252b5132
RH
9440 /* FALLTHROUGH */
9441 case M_BGEU_I:
9442 case M_BGEUL_I:
9443 if (mask == M_BGEUL_I)
9444 likely = 1;
b0e6f033 9445 if (imm_expr.X_add_number == 0)
252b5132 9446 goto do_true;
b0e6f033 9447 else if (imm_expr.X_add_number == 1)
df58fc94 9448 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 9449 &offset_expr, op[0], ZERO);
df58fc94 9450 else
252b5132 9451 {
df58fc94 9452 used_at = 1;
c0ebe874 9453 set_at (op[0], 1);
df58fc94
RS
9454 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
9455 &offset_expr, AT, ZERO);
252b5132 9456 }
252b5132
RH
9457 break;
9458
9459 case M_BGTL:
9460 likely = 1;
9461 case M_BGT:
c0ebe874
RS
9462 if (op[1] == 0)
9463 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[0]);
9464 else if (op[0] == 0)
9465 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[1]);
df58fc94 9466 else
252b5132 9467 {
df58fc94 9468 used_at = 1;
c0ebe874 9469 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
9470 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
9471 &offset_expr, AT, ZERO);
252b5132 9472 }
252b5132
RH
9473 break;
9474
9475 case M_BGTUL:
9476 likely = 1;
9477 case M_BGTU:
c0ebe874 9478 if (op[1] == 0)
df58fc94 9479 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874
RS
9480 &offset_expr, op[0], ZERO);
9481 else if (op[0] == 0)
df58fc94
RS
9482 goto do_false;
9483 else
252b5132 9484 {
df58fc94 9485 used_at = 1;
c0ebe874 9486 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
9487 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
9488 &offset_expr, AT, ZERO);
252b5132 9489 }
252b5132
RH
9490 break;
9491
9492 case M_BLEL:
9493 likely = 1;
9494 case M_BLE:
c0ebe874
RS
9495 if (op[1] == 0)
9496 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
9497 else if (op[0] == 0)
9498 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[1]);
df58fc94 9499 else
252b5132 9500 {
df58fc94 9501 used_at = 1;
c0ebe874 9502 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
9503 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
9504 &offset_expr, AT, ZERO);
252b5132 9505 }
252b5132
RH
9506 break;
9507
9508 case M_BLEL_I:
9509 likely = 1;
9510 case M_BLE_I:
b0e6f033 9511 if (imm_expr.X_add_number >= GPR_SMAX)
252b5132 9512 goto do_true;
f9419b05 9513 ++imm_expr.X_add_number;
252b5132
RH
9514 /* FALLTHROUGH */
9515 case M_BLT_I:
9516 case M_BLTL_I:
9517 if (mask == M_BLTL_I)
9518 likely = 1;
b0e6f033 9519 if (imm_expr.X_add_number == 0)
c0ebe874 9520 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
b0e6f033 9521 else if (imm_expr.X_add_number == 1)
c0ebe874 9522 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
df58fc94 9523 else
252b5132 9524 {
df58fc94 9525 used_at = 1;
c0ebe874 9526 set_at (op[0], 0);
df58fc94
RS
9527 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
9528 &offset_expr, AT, ZERO);
252b5132 9529 }
252b5132
RH
9530 break;
9531
9532 case M_BLEUL:
9533 likely = 1;
9534 case M_BLEU:
c0ebe874 9535 if (op[1] == 0)
df58fc94 9536 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874
RS
9537 &offset_expr, op[0], ZERO);
9538 else if (op[0] == 0)
df58fc94
RS
9539 goto do_true;
9540 else
252b5132 9541 {
df58fc94 9542 used_at = 1;
c0ebe874 9543 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
9544 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
9545 &offset_expr, AT, ZERO);
252b5132 9546 }
252b5132
RH
9547 break;
9548
9549 case M_BLEUL_I:
9550 likely = 1;
9551 case M_BLEU_I:
c0ebe874 9552 if (op[0] == 0
ca4e0257 9553 || (HAVE_32BIT_GPRS
f01dc953 9554 && imm_expr.X_add_number == -1))
252b5132 9555 goto do_true;
f9419b05 9556 ++imm_expr.X_add_number;
252b5132
RH
9557 /* FALLTHROUGH */
9558 case M_BLTU_I:
9559 case M_BLTUL_I:
9560 if (mask == M_BLTUL_I)
9561 likely = 1;
b0e6f033 9562 if (imm_expr.X_add_number == 0)
252b5132 9563 goto do_false;
b0e6f033 9564 else if (imm_expr.X_add_number == 1)
df58fc94 9565 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 9566 &offset_expr, op[0], ZERO);
df58fc94 9567 else
252b5132 9568 {
df58fc94 9569 used_at = 1;
c0ebe874 9570 set_at (op[0], 1);
df58fc94
RS
9571 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
9572 &offset_expr, AT, ZERO);
252b5132 9573 }
252b5132
RH
9574 break;
9575
9576 case M_BLTL:
9577 likely = 1;
9578 case M_BLT:
c0ebe874
RS
9579 if (op[1] == 0)
9580 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
9581 else if (op[0] == 0)
9582 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[1]);
df58fc94 9583 else
252b5132 9584 {
df58fc94 9585 used_at = 1;
c0ebe874 9586 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
9587 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
9588 &offset_expr, AT, ZERO);
252b5132 9589 }
252b5132
RH
9590 break;
9591
9592 case M_BLTUL:
9593 likely = 1;
9594 case M_BLTU:
c0ebe874 9595 if (op[1] == 0)
252b5132 9596 goto do_false;
c0ebe874 9597 else if (op[0] == 0)
df58fc94 9598 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 9599 &offset_expr, ZERO, op[1]);
df58fc94 9600 else
252b5132 9601 {
df58fc94 9602 used_at = 1;
c0ebe874 9603 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
9604 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
9605 &offset_expr, AT, ZERO);
252b5132 9606 }
252b5132
RH
9607 break;
9608
9609 case M_DDIV_3:
9610 dbl = 1;
9611 case M_DIV_3:
9612 s = "mflo";
9613 goto do_div3;
9614 case M_DREM_3:
9615 dbl = 1;
9616 case M_REM_3:
9617 s = "mfhi";
9618 do_div3:
c0ebe874 9619 if (op[2] == 0)
252b5132 9620 {
1661c76c 9621 as_warn (_("divide by zero"));
252b5132 9622 if (mips_trap)
df58fc94 9623 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 9624 else
df58fc94 9625 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 9626 break;
252b5132
RH
9627 }
9628
7d10b47d 9629 start_noreorder ();
252b5132
RH
9630 if (mips_trap)
9631 {
c0ebe874
RS
9632 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
9633 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
252b5132
RH
9634 }
9635 else
9636 {
df58fc94
RS
9637 if (mips_opts.micromips)
9638 micromips_label_expr (&label_expr);
9639 else
9640 label_expr.X_add_number = 8;
c0ebe874
RS
9641 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
9642 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
df58fc94
RS
9643 macro_build (NULL, "break", BRK_FMT, 7);
9644 if (mips_opts.micromips)
9645 micromips_add_label ();
252b5132
RH
9646 }
9647 expr1.X_add_number = -1;
8fc2e39e 9648 used_at = 1;
f6a22291 9649 load_register (AT, &expr1, dbl);
df58fc94
RS
9650 if (mips_opts.micromips)
9651 micromips_label_expr (&label_expr);
9652 else
9653 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
c0ebe874 9654 macro_build (&label_expr, "bne", "s,t,p", op[2], AT);
252b5132
RH
9655 if (dbl)
9656 {
9657 expr1.X_add_number = 1;
f6a22291 9658 load_register (AT, &expr1, dbl);
df58fc94 9659 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
252b5132
RH
9660 }
9661 else
9662 {
9663 expr1.X_add_number = 0x80000000;
df58fc94 9664 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
252b5132
RH
9665 }
9666 if (mips_trap)
9667 {
c0ebe874 9668 macro_build (NULL, "teq", TRAP_FMT, op[1], AT, 6);
252b5132
RH
9669 /* We want to close the noreorder block as soon as possible, so
9670 that later insns are available for delay slot filling. */
7d10b47d 9671 end_noreorder ();
252b5132
RH
9672 }
9673 else
9674 {
df58fc94
RS
9675 if (mips_opts.micromips)
9676 micromips_label_expr (&label_expr);
9677 else
9678 label_expr.X_add_number = 8;
c0ebe874 9679 macro_build (&label_expr, "bne", "s,t,p", op[1], AT);
a605d2b3 9680 macro_build (NULL, "nop", "");
252b5132
RH
9681
9682 /* We want to close the noreorder block as soon as possible, so
9683 that later insns are available for delay slot filling. */
7d10b47d 9684 end_noreorder ();
252b5132 9685
df58fc94 9686 macro_build (NULL, "break", BRK_FMT, 6);
252b5132 9687 }
df58fc94
RS
9688 if (mips_opts.micromips)
9689 micromips_add_label ();
c0ebe874 9690 macro_build (NULL, s, MFHL_FMT, op[0]);
252b5132
RH
9691 break;
9692
9693 case M_DIV_3I:
9694 s = "div";
9695 s2 = "mflo";
9696 goto do_divi;
9697 case M_DIVU_3I:
9698 s = "divu";
9699 s2 = "mflo";
9700 goto do_divi;
9701 case M_REM_3I:
9702 s = "div";
9703 s2 = "mfhi";
9704 goto do_divi;
9705 case M_REMU_3I:
9706 s = "divu";
9707 s2 = "mfhi";
9708 goto do_divi;
9709 case M_DDIV_3I:
9710 dbl = 1;
9711 s = "ddiv";
9712 s2 = "mflo";
9713 goto do_divi;
9714 case M_DDIVU_3I:
9715 dbl = 1;
9716 s = "ddivu";
9717 s2 = "mflo";
9718 goto do_divi;
9719 case M_DREM_3I:
9720 dbl = 1;
9721 s = "ddiv";
9722 s2 = "mfhi";
9723 goto do_divi;
9724 case M_DREMU_3I:
9725 dbl = 1;
9726 s = "ddivu";
9727 s2 = "mfhi";
9728 do_divi:
b0e6f033 9729 if (imm_expr.X_add_number == 0)
252b5132 9730 {
1661c76c 9731 as_warn (_("divide by zero"));
252b5132 9732 if (mips_trap)
df58fc94 9733 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 9734 else
df58fc94 9735 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 9736 break;
252b5132 9737 }
b0e6f033 9738 if (imm_expr.X_add_number == 1)
252b5132
RH
9739 {
9740 if (strcmp (s2, "mflo") == 0)
c0ebe874 9741 move_register (op[0], op[1]);
252b5132 9742 else
c0ebe874 9743 move_register (op[0], ZERO);
8fc2e39e 9744 break;
252b5132 9745 }
b0e6f033 9746 if (imm_expr.X_add_number == -1 && s[strlen (s) - 1] != 'u')
252b5132
RH
9747 {
9748 if (strcmp (s2, "mflo") == 0)
c0ebe874 9749 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", op[0], op[1]);
252b5132 9750 else
c0ebe874 9751 move_register (op[0], ZERO);
8fc2e39e 9752 break;
252b5132
RH
9753 }
9754
8fc2e39e 9755 used_at = 1;
67c0d1eb 9756 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
9757 macro_build (NULL, s, "z,s,t", op[1], AT);
9758 macro_build (NULL, s2, MFHL_FMT, op[0]);
252b5132
RH
9759 break;
9760
9761 case M_DIVU_3:
9762 s = "divu";
9763 s2 = "mflo";
9764 goto do_divu3;
9765 case M_REMU_3:
9766 s = "divu";
9767 s2 = "mfhi";
9768 goto do_divu3;
9769 case M_DDIVU_3:
9770 s = "ddivu";
9771 s2 = "mflo";
9772 goto do_divu3;
9773 case M_DREMU_3:
9774 s = "ddivu";
9775 s2 = "mfhi";
9776 do_divu3:
7d10b47d 9777 start_noreorder ();
252b5132
RH
9778 if (mips_trap)
9779 {
c0ebe874
RS
9780 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
9781 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
9782 /* We want to close the noreorder block as soon as possible, so
9783 that later insns are available for delay slot filling. */
7d10b47d 9784 end_noreorder ();
252b5132
RH
9785 }
9786 else
9787 {
df58fc94
RS
9788 if (mips_opts.micromips)
9789 micromips_label_expr (&label_expr);
9790 else
9791 label_expr.X_add_number = 8;
c0ebe874
RS
9792 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
9793 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
9794
9795 /* We want to close the noreorder block as soon as possible, so
9796 that later insns are available for delay slot filling. */
7d10b47d 9797 end_noreorder ();
df58fc94
RS
9798 macro_build (NULL, "break", BRK_FMT, 7);
9799 if (mips_opts.micromips)
9800 micromips_add_label ();
252b5132 9801 }
c0ebe874 9802 macro_build (NULL, s2, MFHL_FMT, op[0]);
8fc2e39e 9803 break;
252b5132 9804
1abe91b1
MR
9805 case M_DLCA_AB:
9806 dbl = 1;
9807 case M_LCA_AB:
9808 call = 1;
9809 goto do_la;
252b5132
RH
9810 case M_DLA_AB:
9811 dbl = 1;
9812 case M_LA_AB:
1abe91b1 9813 do_la:
252b5132
RH
9814 /* Load the address of a symbol into a register. If breg is not
9815 zero, we then add a base register to it. */
9816
c0ebe874 9817 breg = op[2];
3bec30a8
TS
9818 if (dbl && HAVE_32BIT_GPRS)
9819 as_warn (_("dla used to load 32-bit register"));
9820
90ecf173 9821 if (!dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
9822 as_warn (_("la used to load 64-bit address"));
9823
f2ae14a1 9824 if (small_offset_p (0, align, 16))
0c11417f 9825 {
c0ebe874 9826 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", op[0], breg,
f2ae14a1 9827 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8fc2e39e 9828 break;
0c11417f
MR
9829 }
9830
c0ebe874 9831 if (mips_opts.at && (op[0] == breg))
afdbd6d0
CD
9832 {
9833 tempreg = AT;
9834 used_at = 1;
9835 }
9836 else
c0ebe874 9837 tempreg = op[0];
afdbd6d0 9838
252b5132
RH
9839 if (offset_expr.X_op != O_symbol
9840 && offset_expr.X_op != O_constant)
9841 {
1661c76c 9842 as_bad (_("expression too complex"));
252b5132
RH
9843 offset_expr.X_op = O_constant;
9844 }
9845
252b5132 9846 if (offset_expr.X_op == O_constant)
aed1a261 9847 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
9848 else if (mips_pic == NO_PIC)
9849 {
d6bc6245 9850 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 9851 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
9852 Otherwise we want
9853 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
9854 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
9855 If we have a constant, we need two instructions anyhow,
d6bc6245 9856 so we may as well always use the latter form.
76b3015f 9857
6caf9ef4
TS
9858 With 64bit address space and a usable $at we want
9859 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9860 lui $at,<sym> (BFD_RELOC_HI16_S)
9861 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
9862 daddiu $at,<sym> (BFD_RELOC_LO16)
9863 dsll32 $tempreg,0
9864 daddu $tempreg,$tempreg,$at
9865
9866 If $at is already in use, we use a path which is suboptimal
9867 on superscalar processors.
9868 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9869 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
9870 dsll $tempreg,16
9871 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
9872 dsll $tempreg,16
9873 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
9874
9875 For GP relative symbols in 64bit address space we can use
9876 the same sequence as in 32bit address space. */
aed1a261 9877 if (HAVE_64BIT_SYMBOLS)
252b5132 9878 {
6caf9ef4
TS
9879 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
9880 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
9881 {
9882 relax_start (offset_expr.X_add_symbol);
9883 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
9884 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
9885 relax_switch ();
9886 }
d6bc6245 9887
741fe287 9888 if (used_at == 0 && mips_opts.at)
98d3f06f 9889 {
df58fc94 9890 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 9891 tempreg, BFD_RELOC_MIPS_HIGHEST);
df58fc94 9892 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 9893 AT, BFD_RELOC_HI16_S);
67c0d1eb 9894 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 9895 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 9896 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 9897 AT, AT, BFD_RELOC_LO16);
df58fc94 9898 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 9899 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
9900 used_at = 1;
9901 }
9902 else
9903 {
df58fc94 9904 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 9905 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 9906 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 9907 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 9908 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 9909 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 9910 tempreg, tempreg, BFD_RELOC_HI16_S);
df58fc94 9911 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 9912 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 9913 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 9914 }
6caf9ef4
TS
9915
9916 if (mips_relax.sequence)
9917 relax_end ();
98d3f06f
KH
9918 }
9919 else
9920 {
9921 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 9922 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 9923 {
4d7206a2 9924 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
9925 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
9926 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 9927 relax_switch ();
98d3f06f 9928 }
6943caf0 9929 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
1661c76c 9930 as_bad (_("offset too large"));
67c0d1eb
RS
9931 macro_build_lui (&offset_expr, tempreg);
9932 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
9933 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
9934 if (mips_relax.sequence)
9935 relax_end ();
98d3f06f 9936 }
252b5132 9937 }
0a44bf69 9938 else if (!mips_big_got && !HAVE_NEWABI)
252b5132 9939 {
9117d219
NC
9940 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
9941
252b5132
RH
9942 /* If this is a reference to an external symbol, and there
9943 is no constant, we want
9944 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 9945 or for lca or if tempreg is PIC_CALL_REG
9117d219 9946 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
9947 For a local symbol, we want
9948 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9949 nop
9950 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
9951
9952 If we have a small constant, and this is a reference to
9953 an external symbol, we want
9954 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9955 nop
9956 addiu $tempreg,$tempreg,<constant>
9957 For a local symbol, we want the same instruction
9958 sequence, but we output a BFD_RELOC_LO16 reloc on the
9959 addiu instruction.
9960
9961 If we have a large constant, and this is a reference to
9962 an external symbol, we want
9963 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9964 lui $at,<hiconstant>
9965 addiu $at,$at,<loconstant>
9966 addu $tempreg,$tempreg,$at
9967 For a local symbol, we want the same instruction
9968 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 9969 addiu instruction.
ed6fb7bd
SC
9970 */
9971
4d7206a2 9972 if (offset_expr.X_add_number == 0)
252b5132 9973 {
0a44bf69
RS
9974 if (mips_pic == SVR4_PIC
9975 && breg == 0
9976 && (call || tempreg == PIC_CALL_REG))
4d7206a2
RS
9977 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
9978
9979 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
9980 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9981 lw_reloc_type, mips_gp_register);
4d7206a2 9982 if (breg != 0)
252b5132
RH
9983 {
9984 /* We're going to put in an addu instruction using
9985 tempreg, so we may as well insert the nop right
9986 now. */
269137b2 9987 load_delay_nop ();
252b5132 9988 }
4d7206a2 9989 relax_switch ();
67c0d1eb
RS
9990 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9991 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9992 load_delay_nop ();
67c0d1eb
RS
9993 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
9994 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 9995 relax_end ();
252b5132
RH
9996 /* FIXME: If breg == 0, and the next instruction uses
9997 $tempreg, then if this variant case is used an extra
9998 nop will be generated. */
9999 }
4d7206a2
RS
10000 else if (offset_expr.X_add_number >= -0x8000
10001 && offset_expr.X_add_number < 0x8000)
252b5132 10002 {
67c0d1eb 10003 load_got_offset (tempreg, &offset_expr);
269137b2 10004 load_delay_nop ();
67c0d1eb 10005 add_got_offset (tempreg, &offset_expr);
252b5132
RH
10006 }
10007 else
10008 {
4d7206a2
RS
10009 expr1.X_add_number = offset_expr.X_add_number;
10010 offset_expr.X_add_number =
43c0598f 10011 SEXT_16BIT (offset_expr.X_add_number);
67c0d1eb 10012 load_got_offset (tempreg, &offset_expr);
f6a22291 10013 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
10014 /* If we are going to add in a base register, and the
10015 target register and the base register are the same,
10016 then we are using AT as a temporary register. Since
10017 we want to load the constant into AT, we add our
10018 current AT (from the global offset table) and the
10019 register into the register now, and pretend we were
10020 not using a base register. */
c0ebe874 10021 if (breg == op[0])
252b5132 10022 {
269137b2 10023 load_delay_nop ();
67c0d1eb 10024 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 10025 op[0], AT, breg);
252b5132 10026 breg = 0;
c0ebe874 10027 tempreg = op[0];
252b5132 10028 }
f6a22291 10029 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
10030 used_at = 1;
10031 }
10032 }
0a44bf69 10033 else if (!mips_big_got && HAVE_NEWABI)
f5040a92 10034 {
67c0d1eb 10035 int add_breg_early = 0;
f5040a92
AO
10036
10037 /* If this is a reference to an external, and there is no
10038 constant, or local symbol (*), with or without a
10039 constant, we want
10040 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 10041 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
10042 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
10043
10044 If we have a small constant, and this is a reference to
10045 an external symbol, we want
10046 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
10047 addiu $tempreg,$tempreg,<constant>
10048
10049 If we have a large constant, and this is a reference to
10050 an external symbol, we want
10051 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
10052 lui $at,<hiconstant>
10053 addiu $at,$at,<loconstant>
10054 addu $tempreg,$tempreg,$at
10055
10056 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
10057 local symbols, even though it introduces an additional
10058 instruction. */
10059
f5040a92
AO
10060 if (offset_expr.X_add_number)
10061 {
4d7206a2 10062 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
10063 offset_expr.X_add_number = 0;
10064
4d7206a2 10065 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
10066 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10067 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
10068
10069 if (expr1.X_add_number >= -0x8000
10070 && expr1.X_add_number < 0x8000)
10071 {
67c0d1eb
RS
10072 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
10073 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 10074 }
ecd13cd3 10075 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 10076 {
c0ebe874
RS
10077 unsigned int dreg;
10078
f5040a92
AO
10079 /* If we are going to add in a base register, and the
10080 target register and the base register are the same,
10081 then we are using AT as a temporary register. Since
10082 we want to load the constant into AT, we add our
10083 current AT (from the global offset table) and the
10084 register into the register now, and pretend we were
10085 not using a base register. */
c0ebe874 10086 if (breg != op[0])
f5040a92
AO
10087 dreg = tempreg;
10088 else
10089 {
9c2799c2 10090 gas_assert (tempreg == AT);
67c0d1eb 10091 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
10092 op[0], AT, breg);
10093 dreg = op[0];
67c0d1eb 10094 add_breg_early = 1;
f5040a92
AO
10095 }
10096
f6a22291 10097 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 10098 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10099 dreg, dreg, AT);
f5040a92 10100
f5040a92
AO
10101 used_at = 1;
10102 }
10103 else
10104 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
10105
4d7206a2 10106 relax_switch ();
f5040a92
AO
10107 offset_expr.X_add_number = expr1.X_add_number;
10108
67c0d1eb
RS
10109 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10110 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
10111 if (add_breg_early)
f5040a92 10112 {
67c0d1eb 10113 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 10114 op[0], tempreg, breg);
f5040a92 10115 breg = 0;
c0ebe874 10116 tempreg = op[0];
f5040a92 10117 }
4d7206a2 10118 relax_end ();
f5040a92 10119 }
4d7206a2 10120 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 10121 {
4d7206a2 10122 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
10123 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10124 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 10125 relax_switch ();
67c0d1eb
RS
10126 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10127 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 10128 relax_end ();
f5040a92 10129 }
4d7206a2 10130 else
f5040a92 10131 {
67c0d1eb
RS
10132 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10133 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
10134 }
10135 }
0a44bf69 10136 else if (mips_big_got && !HAVE_NEWABI)
252b5132 10137 {
67c0d1eb 10138 int gpdelay;
9117d219
NC
10139 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
10140 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 10141 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
10142
10143 /* This is the large GOT case. If this is a reference to an
10144 external symbol, and there is no constant, we want
10145 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10146 addu $tempreg,$tempreg,$gp
10147 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 10148 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
10149 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
10150 addu $tempreg,$tempreg,$gp
10151 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
10152 For a local symbol, we want
10153 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10154 nop
10155 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10156
10157 If we have a small constant, and this is a reference to
10158 an external symbol, we want
10159 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10160 addu $tempreg,$tempreg,$gp
10161 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10162 nop
10163 addiu $tempreg,$tempreg,<constant>
10164 For a local symbol, we want
10165 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10166 nop
10167 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
10168
10169 If we have a large constant, and this is a reference to
10170 an external symbol, we want
10171 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10172 addu $tempreg,$tempreg,$gp
10173 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10174 lui $at,<hiconstant>
10175 addiu $at,$at,<loconstant>
10176 addu $tempreg,$tempreg,$at
10177 For a local symbol, we want
10178 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10179 lui $at,<hiconstant>
10180 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
10181 addu $tempreg,$tempreg,$at
f5040a92 10182 */
438c16b8 10183
252b5132
RH
10184 expr1.X_add_number = offset_expr.X_add_number;
10185 offset_expr.X_add_number = 0;
4d7206a2 10186 relax_start (offset_expr.X_add_symbol);
67c0d1eb 10187 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
10188 if (expr1.X_add_number == 0 && breg == 0
10189 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
10190 {
10191 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
10192 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
10193 }
df58fc94 10194 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 10195 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10196 tempreg, tempreg, mips_gp_register);
67c0d1eb 10197 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 10198 tempreg, lw_reloc_type, tempreg);
252b5132
RH
10199 if (expr1.X_add_number == 0)
10200 {
67c0d1eb 10201 if (breg != 0)
252b5132
RH
10202 {
10203 /* We're going to put in an addu instruction using
10204 tempreg, so we may as well insert the nop right
10205 now. */
269137b2 10206 load_delay_nop ();
252b5132 10207 }
252b5132
RH
10208 }
10209 else if (expr1.X_add_number >= -0x8000
10210 && expr1.X_add_number < 0x8000)
10211 {
269137b2 10212 load_delay_nop ();
67c0d1eb 10213 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 10214 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
10215 }
10216 else
10217 {
c0ebe874
RS
10218 unsigned int dreg;
10219
252b5132
RH
10220 /* If we are going to add in a base register, and the
10221 target register and the base register are the same,
10222 then we are using AT as a temporary register. Since
10223 we want to load the constant into AT, we add our
10224 current AT (from the global offset table) and the
10225 register into the register now, and pretend we were
10226 not using a base register. */
c0ebe874 10227 if (breg != op[0])
67c0d1eb 10228 dreg = tempreg;
252b5132
RH
10229 else
10230 {
9c2799c2 10231 gas_assert (tempreg == AT);
269137b2 10232 load_delay_nop ();
67c0d1eb 10233 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
10234 op[0], AT, breg);
10235 dreg = op[0];
252b5132
RH
10236 }
10237
f6a22291 10238 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 10239 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 10240
252b5132
RH
10241 used_at = 1;
10242 }
43c0598f 10243 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
4d7206a2 10244 relax_switch ();
252b5132 10245
67c0d1eb 10246 if (gpdelay)
252b5132
RH
10247 {
10248 /* This is needed because this instruction uses $gp, but
f5040a92 10249 the first instruction on the main stream does not. */
67c0d1eb 10250 macro_build (NULL, "nop", "");
252b5132 10251 }
ed6fb7bd 10252
67c0d1eb
RS
10253 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10254 local_reloc_type, mips_gp_register);
f5040a92 10255 if (expr1.X_add_number >= -0x8000
252b5132
RH
10256 && expr1.X_add_number < 0x8000)
10257 {
269137b2 10258 load_delay_nop ();
67c0d1eb
RS
10259 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10260 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 10261 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
10262 register, the external symbol case ended with a load,
10263 so if the symbol turns out to not be external, and
10264 the next instruction uses tempreg, an unnecessary nop
10265 will be inserted. */
252b5132
RH
10266 }
10267 else
10268 {
c0ebe874 10269 if (breg == op[0])
252b5132
RH
10270 {
10271 /* We must add in the base register now, as in the
f5040a92 10272 external symbol case. */
9c2799c2 10273 gas_assert (tempreg == AT);
269137b2 10274 load_delay_nop ();
67c0d1eb 10275 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
10276 op[0], AT, breg);
10277 tempreg = op[0];
252b5132 10278 /* We set breg to 0 because we have arranged to add
f5040a92 10279 it in in both cases. */
252b5132
RH
10280 breg = 0;
10281 }
10282
67c0d1eb
RS
10283 macro_build_lui (&expr1, AT);
10284 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 10285 AT, AT, BFD_RELOC_LO16);
67c0d1eb 10286 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10287 tempreg, tempreg, AT);
8fc2e39e 10288 used_at = 1;
252b5132 10289 }
4d7206a2 10290 relax_end ();
252b5132 10291 }
0a44bf69 10292 else if (mips_big_got && HAVE_NEWABI)
f5040a92 10293 {
f5040a92
AO
10294 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
10295 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 10296 int add_breg_early = 0;
f5040a92
AO
10297
10298 /* This is the large GOT case. If this is a reference to an
10299 external symbol, and there is no constant, we want
10300 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10301 add $tempreg,$tempreg,$gp
10302 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 10303 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
10304 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
10305 add $tempreg,$tempreg,$gp
10306 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
10307
10308 If we have a small constant, and this is a reference to
10309 an external symbol, we want
10310 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10311 add $tempreg,$tempreg,$gp
10312 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10313 addi $tempreg,$tempreg,<constant>
10314
10315 If we have a large constant, and this is a reference to
10316 an external symbol, we want
10317 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10318 addu $tempreg,$tempreg,$gp
10319 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10320 lui $at,<hiconstant>
10321 addi $at,$at,<loconstant>
10322 add $tempreg,$tempreg,$at
10323
10324 If we have NewABI, and we know it's a local symbol, we want
10325 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
10326 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
10327 otherwise we have to resort to GOT_HI16/GOT_LO16. */
10328
4d7206a2 10329 relax_start (offset_expr.X_add_symbol);
f5040a92 10330
4d7206a2 10331 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
10332 offset_expr.X_add_number = 0;
10333
1abe91b1
MR
10334 if (expr1.X_add_number == 0 && breg == 0
10335 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
10336 {
10337 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
10338 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
10339 }
df58fc94 10340 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 10341 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10342 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
10343 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10344 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
10345
10346 if (expr1.X_add_number == 0)
4d7206a2 10347 ;
f5040a92
AO
10348 else if (expr1.X_add_number >= -0x8000
10349 && expr1.X_add_number < 0x8000)
10350 {
67c0d1eb 10351 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 10352 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 10353 }
ecd13cd3 10354 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 10355 {
c0ebe874
RS
10356 unsigned int dreg;
10357
f5040a92
AO
10358 /* If we are going to add in a base register, and the
10359 target register and the base register are the same,
10360 then we are using AT as a temporary register. Since
10361 we want to load the constant into AT, we add our
10362 current AT (from the global offset table) and the
10363 register into the register now, and pretend we were
10364 not using a base register. */
c0ebe874 10365 if (breg != op[0])
f5040a92
AO
10366 dreg = tempreg;
10367 else
10368 {
9c2799c2 10369 gas_assert (tempreg == AT);
67c0d1eb 10370 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
10371 op[0], AT, breg);
10372 dreg = op[0];
67c0d1eb 10373 add_breg_early = 1;
f5040a92
AO
10374 }
10375
f6a22291 10376 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 10377 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 10378
f5040a92
AO
10379 used_at = 1;
10380 }
10381 else
10382 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
10383
4d7206a2 10384 relax_switch ();
f5040a92 10385 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
10386 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10387 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
10388 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
10389 tempreg, BFD_RELOC_MIPS_GOT_OFST);
10390 if (add_breg_early)
f5040a92 10391 {
67c0d1eb 10392 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 10393 op[0], tempreg, breg);
f5040a92 10394 breg = 0;
c0ebe874 10395 tempreg = op[0];
f5040a92 10396 }
4d7206a2 10397 relax_end ();
f5040a92 10398 }
252b5132
RH
10399 else
10400 abort ();
10401
10402 if (breg != 0)
c0ebe874 10403 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", op[0], tempreg, breg);
252b5132
RH
10404 break;
10405
52b6b6b9 10406 case M_MSGSND:
df58fc94 10407 gas_assert (!mips_opts.micromips);
c0ebe874 10408 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x01);
c7af4273 10409 break;
52b6b6b9
JM
10410
10411 case M_MSGLD:
df58fc94 10412 gas_assert (!mips_opts.micromips);
c8276761 10413 macro_build (NULL, "c2", "C", 0x02);
c7af4273 10414 break;
52b6b6b9
JM
10415
10416 case M_MSGLD_T:
df58fc94 10417 gas_assert (!mips_opts.micromips);
c0ebe874 10418 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x02);
c7af4273 10419 break;
52b6b6b9
JM
10420
10421 case M_MSGWAIT:
df58fc94 10422 gas_assert (!mips_opts.micromips);
52b6b6b9 10423 macro_build (NULL, "c2", "C", 3);
c7af4273 10424 break;
52b6b6b9
JM
10425
10426 case M_MSGWAIT_T:
df58fc94 10427 gas_assert (!mips_opts.micromips);
c0ebe874 10428 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x03);
c7af4273 10429 break;
52b6b6b9 10430
252b5132
RH
10431 case M_J_A:
10432 /* The j instruction may not be used in PIC code, since it
10433 requires an absolute address. We convert it to a b
10434 instruction. */
10435 if (mips_pic == NO_PIC)
67c0d1eb 10436 macro_build (&offset_expr, "j", "a");
252b5132 10437 else
67c0d1eb 10438 macro_build (&offset_expr, "b", "p");
8fc2e39e 10439 break;
252b5132
RH
10440
10441 /* The jal instructions must be handled as macros because when
10442 generating PIC code they expand to multi-instruction
10443 sequences. Normally they are simple instructions. */
df58fc94 10444 case M_JALS_1:
c0ebe874
RS
10445 op[1] = op[0];
10446 op[0] = RA;
df58fc94
RS
10447 /* Fall through. */
10448 case M_JALS_2:
10449 gas_assert (mips_opts.micromips);
833794fc
MR
10450 if (mips_opts.insn32)
10451 {
1661c76c 10452 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
833794fc
MR
10453 break;
10454 }
df58fc94
RS
10455 jals = 1;
10456 goto jal;
252b5132 10457 case M_JAL_1:
c0ebe874
RS
10458 op[1] = op[0];
10459 op[0] = RA;
252b5132
RH
10460 /* Fall through. */
10461 case M_JAL_2:
df58fc94 10462 jal:
3e722fb5 10463 if (mips_pic == NO_PIC)
df58fc94
RS
10464 {
10465 s = jals ? "jalrs" : "jalr";
e64af278 10466 if (mips_opts.micromips
833794fc 10467 && !mips_opts.insn32
c0ebe874 10468 && op[0] == RA
e64af278 10469 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 10470 macro_build (NULL, s, "mj", op[1]);
df58fc94 10471 else
c0ebe874 10472 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
df58fc94 10473 }
0a44bf69 10474 else
252b5132 10475 {
df58fc94
RS
10476 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
10477 && mips_cprestore_offset >= 0);
10478
c0ebe874 10479 if (op[1] != PIC_CALL_REG)
252b5132 10480 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 10481
833794fc
MR
10482 s = ((mips_opts.micromips
10483 && !mips_opts.insn32
10484 && (!mips_opts.noreorder || cprestore))
df58fc94 10485 ? "jalrs" : "jalr");
e64af278 10486 if (mips_opts.micromips
833794fc 10487 && !mips_opts.insn32
c0ebe874 10488 && op[0] == RA
e64af278 10489 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 10490 macro_build (NULL, s, "mj", op[1]);
df58fc94 10491 else
c0ebe874 10492 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
0a44bf69 10493 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
252b5132 10494 {
6478892d 10495 if (mips_cprestore_offset < 0)
1661c76c 10496 as_warn (_("no .cprestore pseudo-op used in PIC code"));
6478892d
TS
10497 else
10498 {
90ecf173 10499 if (!mips_frame_reg_valid)
7a621144 10500 {
1661c76c 10501 as_warn (_("no .frame pseudo-op used in PIC code"));
7a621144
DJ
10502 /* Quiet this warning. */
10503 mips_frame_reg_valid = 1;
10504 }
90ecf173 10505 if (!mips_cprestore_valid)
7a621144 10506 {
1661c76c 10507 as_warn (_("no .cprestore pseudo-op used in PIC code"));
7a621144
DJ
10508 /* Quiet this warning. */
10509 mips_cprestore_valid = 1;
10510 }
d3fca0b5
MR
10511 if (mips_opts.noreorder)
10512 macro_build (NULL, "nop", "");
6478892d 10513 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 10514 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 10515 mips_gp_register,
256ab948
TS
10516 mips_frame_reg,
10517 HAVE_64BIT_ADDRESSES);
6478892d 10518 }
252b5132
RH
10519 }
10520 }
252b5132 10521
8fc2e39e 10522 break;
252b5132 10523
df58fc94
RS
10524 case M_JALS_A:
10525 gas_assert (mips_opts.micromips);
833794fc
MR
10526 if (mips_opts.insn32)
10527 {
1661c76c 10528 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
833794fc
MR
10529 break;
10530 }
df58fc94
RS
10531 jals = 1;
10532 /* Fall through. */
252b5132
RH
10533 case M_JAL_A:
10534 if (mips_pic == NO_PIC)
df58fc94 10535 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
252b5132
RH
10536 else if (mips_pic == SVR4_PIC)
10537 {
10538 /* If this is a reference to an external symbol, and we are
10539 using a small GOT, we want
10540 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
10541 nop
f9419b05 10542 jalr $ra,$25
252b5132
RH
10543 nop
10544 lw $gp,cprestore($sp)
10545 The cprestore value is set using the .cprestore
10546 pseudo-op. If we are using a big GOT, we want
10547 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
10548 addu $25,$25,$gp
10549 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
10550 nop
f9419b05 10551 jalr $ra,$25
252b5132
RH
10552 nop
10553 lw $gp,cprestore($sp)
10554 If the symbol is not external, we want
10555 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10556 nop
10557 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 10558 jalr $ra,$25
252b5132 10559 nop
438c16b8 10560 lw $gp,cprestore($sp)
f5040a92
AO
10561
10562 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
10563 sequences above, minus nops, unless the symbol is local,
10564 which enables us to use GOT_PAGE/GOT_OFST (big got) or
10565 GOT_DISP. */
438c16b8 10566 if (HAVE_NEWABI)
252b5132 10567 {
90ecf173 10568 if (!mips_big_got)
f5040a92 10569 {
4d7206a2 10570 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
10571 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10572 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 10573 mips_gp_register);
4d7206a2 10574 relax_switch ();
67c0d1eb
RS
10575 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10576 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
10577 mips_gp_register);
10578 relax_end ();
f5040a92
AO
10579 }
10580 else
10581 {
4d7206a2 10582 relax_start (offset_expr.X_add_symbol);
df58fc94 10583 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
10584 BFD_RELOC_MIPS_CALL_HI16);
10585 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
10586 PIC_CALL_REG, mips_gp_register);
10587 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10588 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
10589 PIC_CALL_REG);
4d7206a2 10590 relax_switch ();
67c0d1eb
RS
10591 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10592 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
10593 mips_gp_register);
10594 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10595 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 10596 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 10597 relax_end ();
f5040a92 10598 }
684022ea 10599
df58fc94 10600 macro_build_jalr (&offset_expr, 0);
252b5132
RH
10601 }
10602 else
10603 {
4d7206a2 10604 relax_start (offset_expr.X_add_symbol);
90ecf173 10605 if (!mips_big_got)
438c16b8 10606 {
67c0d1eb
RS
10607 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10608 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 10609 mips_gp_register);
269137b2 10610 load_delay_nop ();
4d7206a2 10611 relax_switch ();
438c16b8 10612 }
252b5132 10613 else
252b5132 10614 {
67c0d1eb
RS
10615 int gpdelay;
10616
10617 gpdelay = reg_needs_delay (mips_gp_register);
df58fc94 10618 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
10619 BFD_RELOC_MIPS_CALL_HI16);
10620 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
10621 PIC_CALL_REG, mips_gp_register);
10622 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10623 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
10624 PIC_CALL_REG);
269137b2 10625 load_delay_nop ();
4d7206a2 10626 relax_switch ();
67c0d1eb
RS
10627 if (gpdelay)
10628 macro_build (NULL, "nop", "");
252b5132 10629 }
67c0d1eb
RS
10630 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10631 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 10632 mips_gp_register);
269137b2 10633 load_delay_nop ();
67c0d1eb
RS
10634 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10635 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 10636 relax_end ();
df58fc94 10637 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
438c16b8 10638
6478892d 10639 if (mips_cprestore_offset < 0)
1661c76c 10640 as_warn (_("no .cprestore pseudo-op used in PIC code"));
6478892d
TS
10641 else
10642 {
90ecf173 10643 if (!mips_frame_reg_valid)
7a621144 10644 {
1661c76c 10645 as_warn (_("no .frame pseudo-op used in PIC code"));
7a621144
DJ
10646 /* Quiet this warning. */
10647 mips_frame_reg_valid = 1;
10648 }
90ecf173 10649 if (!mips_cprestore_valid)
7a621144 10650 {
1661c76c 10651 as_warn (_("no .cprestore pseudo-op used in PIC code"));
7a621144
DJ
10652 /* Quiet this warning. */
10653 mips_cprestore_valid = 1;
10654 }
6478892d 10655 if (mips_opts.noreorder)
67c0d1eb 10656 macro_build (NULL, "nop", "");
6478892d 10657 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 10658 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 10659 mips_gp_register,
256ab948
TS
10660 mips_frame_reg,
10661 HAVE_64BIT_ADDRESSES);
6478892d 10662 }
252b5132
RH
10663 }
10664 }
0a44bf69 10665 else if (mips_pic == VXWORKS_PIC)
1661c76c 10666 as_bad (_("non-PIC jump used in PIC library"));
252b5132
RH
10667 else
10668 abort ();
10669
8fc2e39e 10670 break;
252b5132 10671
7f3c4072 10672 case M_LBUE_AB:
7f3c4072
CM
10673 s = "lbue";
10674 fmt = "t,+j(b)";
10675 offbits = 9;
10676 goto ld_st;
10677 case M_LHUE_AB:
7f3c4072
CM
10678 s = "lhue";
10679 fmt = "t,+j(b)";
10680 offbits = 9;
10681 goto ld_st;
10682 case M_LBE_AB:
7f3c4072
CM
10683 s = "lbe";
10684 fmt = "t,+j(b)";
10685 offbits = 9;
10686 goto ld_st;
10687 case M_LHE_AB:
7f3c4072
CM
10688 s = "lhe";
10689 fmt = "t,+j(b)";
10690 offbits = 9;
10691 goto ld_st;
10692 case M_LLE_AB:
7f3c4072
CM
10693 s = "lle";
10694 fmt = "t,+j(b)";
10695 offbits = 9;
10696 goto ld_st;
10697 case M_LWE_AB:
7f3c4072
CM
10698 s = "lwe";
10699 fmt = "t,+j(b)";
10700 offbits = 9;
10701 goto ld_st;
10702 case M_LWLE_AB:
7f3c4072
CM
10703 s = "lwle";
10704 fmt = "t,+j(b)";
10705 offbits = 9;
10706 goto ld_st;
10707 case M_LWRE_AB:
7f3c4072
CM
10708 s = "lwre";
10709 fmt = "t,+j(b)";
10710 offbits = 9;
10711 goto ld_st;
10712 case M_SBE_AB:
7f3c4072
CM
10713 s = "sbe";
10714 fmt = "t,+j(b)";
10715 offbits = 9;
10716 goto ld_st;
10717 case M_SCE_AB:
7f3c4072
CM
10718 s = "sce";
10719 fmt = "t,+j(b)";
10720 offbits = 9;
10721 goto ld_st;
10722 case M_SHE_AB:
7f3c4072
CM
10723 s = "she";
10724 fmt = "t,+j(b)";
10725 offbits = 9;
10726 goto ld_st;
10727 case M_SWE_AB:
7f3c4072
CM
10728 s = "swe";
10729 fmt = "t,+j(b)";
10730 offbits = 9;
10731 goto ld_st;
10732 case M_SWLE_AB:
7f3c4072
CM
10733 s = "swle";
10734 fmt = "t,+j(b)";
10735 offbits = 9;
10736 goto ld_st;
10737 case M_SWRE_AB:
7f3c4072
CM
10738 s = "swre";
10739 fmt = "t,+j(b)";
10740 offbits = 9;
10741 goto ld_st;
dec0624d 10742 case M_ACLR_AB:
dec0624d 10743 s = "aclr";
dec0624d 10744 fmt = "\\,~(b)";
7f3c4072 10745 offbits = 12;
dec0624d
MR
10746 goto ld_st;
10747 case M_ASET_AB:
dec0624d 10748 s = "aset";
dec0624d 10749 fmt = "\\,~(b)";
7f3c4072 10750 offbits = 12;
dec0624d 10751 goto ld_st;
252b5132
RH
10752 case M_LB_AB:
10753 s = "lb";
df58fc94 10754 fmt = "t,o(b)";
252b5132
RH
10755 goto ld;
10756 case M_LBU_AB:
10757 s = "lbu";
df58fc94 10758 fmt = "t,o(b)";
252b5132
RH
10759 goto ld;
10760 case M_LH_AB:
10761 s = "lh";
df58fc94 10762 fmt = "t,o(b)";
252b5132
RH
10763 goto ld;
10764 case M_LHU_AB:
10765 s = "lhu";
df58fc94 10766 fmt = "t,o(b)";
252b5132
RH
10767 goto ld;
10768 case M_LW_AB:
10769 s = "lw";
df58fc94 10770 fmt = "t,o(b)";
252b5132
RH
10771 goto ld;
10772 case M_LWC0_AB:
df58fc94 10773 gas_assert (!mips_opts.micromips);
252b5132 10774 s = "lwc0";
df58fc94 10775 fmt = "E,o(b)";
bdaaa2e1 10776 /* Itbl support may require additional care here. */
252b5132 10777 coproc = 1;
df58fc94 10778 goto ld_st;
252b5132
RH
10779 case M_LWC1_AB:
10780 s = "lwc1";
df58fc94 10781 fmt = "T,o(b)";
bdaaa2e1 10782 /* Itbl support may require additional care here. */
252b5132 10783 coproc = 1;
df58fc94 10784 goto ld_st;
252b5132
RH
10785 case M_LWC2_AB:
10786 s = "lwc2";
df58fc94 10787 fmt = COP12_FMT;
7f3c4072 10788 offbits = (mips_opts.micromips ? 12 : 16);
bdaaa2e1 10789 /* Itbl support may require additional care here. */
252b5132 10790 coproc = 1;
df58fc94 10791 goto ld_st;
252b5132 10792 case M_LWC3_AB:
df58fc94 10793 gas_assert (!mips_opts.micromips);
252b5132 10794 s = "lwc3";
df58fc94 10795 fmt = "E,o(b)";
bdaaa2e1 10796 /* Itbl support may require additional care here. */
252b5132 10797 coproc = 1;
df58fc94 10798 goto ld_st;
252b5132
RH
10799 case M_LWL_AB:
10800 s = "lwl";
df58fc94 10801 fmt = MEM12_FMT;
7f3c4072 10802 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10803 goto ld_st;
252b5132
RH
10804 case M_LWR_AB:
10805 s = "lwr";
df58fc94 10806 fmt = MEM12_FMT;
7f3c4072 10807 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10808 goto ld_st;
252b5132 10809 case M_LDC1_AB:
252b5132 10810 s = "ldc1";
df58fc94 10811 fmt = "T,o(b)";
bdaaa2e1 10812 /* Itbl support may require additional care here. */
252b5132 10813 coproc = 1;
df58fc94 10814 goto ld_st;
252b5132
RH
10815 case M_LDC2_AB:
10816 s = "ldc2";
df58fc94 10817 fmt = COP12_FMT;
7f3c4072 10818 offbits = (mips_opts.micromips ? 12 : 16);
bdaaa2e1 10819 /* Itbl support may require additional care here. */
252b5132 10820 coproc = 1;
df58fc94 10821 goto ld_st;
c77c0862 10822 case M_LQC2_AB:
c77c0862 10823 s = "lqc2";
14daeee3 10824 fmt = "+7,o(b)";
c77c0862
RS
10825 /* Itbl support may require additional care here. */
10826 coproc = 1;
10827 goto ld_st;
252b5132
RH
10828 case M_LDC3_AB:
10829 s = "ldc3";
df58fc94 10830 fmt = "E,o(b)";
bdaaa2e1 10831 /* Itbl support may require additional care here. */
252b5132 10832 coproc = 1;
df58fc94 10833 goto ld_st;
252b5132
RH
10834 case M_LDL_AB:
10835 s = "ldl";
df58fc94 10836 fmt = MEM12_FMT;
7f3c4072 10837 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10838 goto ld_st;
252b5132
RH
10839 case M_LDR_AB:
10840 s = "ldr";
df58fc94 10841 fmt = MEM12_FMT;
7f3c4072 10842 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10843 goto ld_st;
252b5132
RH
10844 case M_LL_AB:
10845 s = "ll";
df58fc94 10846 fmt = MEM12_FMT;
7f3c4072 10847 offbits = (mips_opts.micromips ? 12 : 16);
252b5132
RH
10848 goto ld;
10849 case M_LLD_AB:
10850 s = "lld";
df58fc94 10851 fmt = MEM12_FMT;
7f3c4072 10852 offbits = (mips_opts.micromips ? 12 : 16);
252b5132
RH
10853 goto ld;
10854 case M_LWU_AB:
10855 s = "lwu";
df58fc94 10856 fmt = MEM12_FMT;
7f3c4072 10857 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
10858 goto ld;
10859 case M_LWP_AB:
df58fc94
RS
10860 gas_assert (mips_opts.micromips);
10861 s = "lwp";
10862 fmt = "t,~(b)";
7f3c4072 10863 offbits = 12;
df58fc94
RS
10864 lp = 1;
10865 goto ld;
10866 case M_LDP_AB:
df58fc94
RS
10867 gas_assert (mips_opts.micromips);
10868 s = "ldp";
10869 fmt = "t,~(b)";
7f3c4072 10870 offbits = 12;
df58fc94
RS
10871 lp = 1;
10872 goto ld;
10873 case M_LWM_AB:
df58fc94
RS
10874 gas_assert (mips_opts.micromips);
10875 s = "lwm";
10876 fmt = "n,~(b)";
7f3c4072 10877 offbits = 12;
df58fc94
RS
10878 goto ld_st;
10879 case M_LDM_AB:
df58fc94
RS
10880 gas_assert (mips_opts.micromips);
10881 s = "ldm";
10882 fmt = "n,~(b)";
7f3c4072 10883 offbits = 12;
df58fc94
RS
10884 goto ld_st;
10885
252b5132 10886 ld:
f19ccbda 10887 /* We don't want to use $0 as tempreg. */
c0ebe874 10888 if (op[2] == op[0] + lp || op[0] + lp == ZERO)
df58fc94 10889 goto ld_st;
252b5132 10890 else
c0ebe874 10891 tempreg = op[0] + lp;
df58fc94
RS
10892 goto ld_noat;
10893
252b5132
RH
10894 case M_SB_AB:
10895 s = "sb";
df58fc94
RS
10896 fmt = "t,o(b)";
10897 goto ld_st;
252b5132
RH
10898 case M_SH_AB:
10899 s = "sh";
df58fc94
RS
10900 fmt = "t,o(b)";
10901 goto ld_st;
252b5132
RH
10902 case M_SW_AB:
10903 s = "sw";
df58fc94
RS
10904 fmt = "t,o(b)";
10905 goto ld_st;
252b5132 10906 case M_SWC0_AB:
df58fc94 10907 gas_assert (!mips_opts.micromips);
252b5132 10908 s = "swc0";
df58fc94 10909 fmt = "E,o(b)";
bdaaa2e1 10910 /* Itbl support may require additional care here. */
252b5132 10911 coproc = 1;
df58fc94 10912 goto ld_st;
252b5132
RH
10913 case M_SWC1_AB:
10914 s = "swc1";
df58fc94 10915 fmt = "T,o(b)";
bdaaa2e1 10916 /* Itbl support may require additional care here. */
252b5132 10917 coproc = 1;
df58fc94 10918 goto ld_st;
252b5132
RH
10919 case M_SWC2_AB:
10920 s = "swc2";
df58fc94 10921 fmt = COP12_FMT;
7f3c4072 10922 offbits = (mips_opts.micromips ? 12 : 16);
bdaaa2e1 10923 /* Itbl support may require additional care here. */
252b5132 10924 coproc = 1;
df58fc94 10925 goto ld_st;
252b5132 10926 case M_SWC3_AB:
df58fc94 10927 gas_assert (!mips_opts.micromips);
252b5132 10928 s = "swc3";
df58fc94 10929 fmt = "E,o(b)";
bdaaa2e1 10930 /* Itbl support may require additional care here. */
252b5132 10931 coproc = 1;
df58fc94 10932 goto ld_st;
252b5132
RH
10933 case M_SWL_AB:
10934 s = "swl";
df58fc94 10935 fmt = MEM12_FMT;
7f3c4072 10936 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10937 goto ld_st;
252b5132
RH
10938 case M_SWR_AB:
10939 s = "swr";
df58fc94 10940 fmt = MEM12_FMT;
7f3c4072 10941 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10942 goto ld_st;
252b5132
RH
10943 case M_SC_AB:
10944 s = "sc";
df58fc94 10945 fmt = MEM12_FMT;
7f3c4072 10946 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10947 goto ld_st;
252b5132
RH
10948 case M_SCD_AB:
10949 s = "scd";
df58fc94 10950 fmt = MEM12_FMT;
7f3c4072 10951 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10952 goto ld_st;
d43b4baf
TS
10953 case M_CACHE_AB:
10954 s = "cache";
df58fc94 10955 fmt = mips_opts.micromips ? "k,~(b)" : "k,o(b)";
7f3c4072
CM
10956 offbits = (mips_opts.micromips ? 12 : 16);
10957 goto ld_st;
10958 case M_CACHEE_AB:
7f3c4072
CM
10959 s = "cachee";
10960 fmt = "k,+j(b)";
10961 offbits = 9;
df58fc94 10962 goto ld_st;
3eebd5eb
MR
10963 case M_PREF_AB:
10964 s = "pref";
df58fc94 10965 fmt = !mips_opts.micromips ? "k,o(b)" : "k,~(b)";
7f3c4072
CM
10966 offbits = (mips_opts.micromips ? 12 : 16);
10967 goto ld_st;
10968 case M_PREFE_AB:
7f3c4072
CM
10969 s = "prefe";
10970 fmt = "k,+j(b)";
10971 offbits = 9;
df58fc94 10972 goto ld_st;
252b5132 10973 case M_SDC1_AB:
252b5132 10974 s = "sdc1";
df58fc94 10975 fmt = "T,o(b)";
252b5132 10976 coproc = 1;
bdaaa2e1 10977 /* Itbl support may require additional care here. */
df58fc94 10978 goto ld_st;
252b5132
RH
10979 case M_SDC2_AB:
10980 s = "sdc2";
df58fc94 10981 fmt = COP12_FMT;
7f3c4072 10982 offbits = (mips_opts.micromips ? 12 : 16);
c77c0862
RS
10983 /* Itbl support may require additional care here. */
10984 coproc = 1;
10985 goto ld_st;
10986 case M_SQC2_AB:
c77c0862 10987 s = "sqc2";
14daeee3 10988 fmt = "+7,o(b)";
bdaaa2e1 10989 /* Itbl support may require additional care here. */
252b5132 10990 coproc = 1;
df58fc94 10991 goto ld_st;
252b5132 10992 case M_SDC3_AB:
df58fc94 10993 gas_assert (!mips_opts.micromips);
252b5132 10994 s = "sdc3";
df58fc94 10995 fmt = "E,o(b)";
bdaaa2e1 10996 /* Itbl support may require additional care here. */
252b5132 10997 coproc = 1;
df58fc94 10998 goto ld_st;
252b5132
RH
10999 case M_SDL_AB:
11000 s = "sdl";
df58fc94 11001 fmt = MEM12_FMT;
7f3c4072 11002 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11003 goto ld_st;
252b5132
RH
11004 case M_SDR_AB:
11005 s = "sdr";
df58fc94 11006 fmt = MEM12_FMT;
7f3c4072 11007 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
11008 goto ld_st;
11009 case M_SWP_AB:
df58fc94
RS
11010 gas_assert (mips_opts.micromips);
11011 s = "swp";
11012 fmt = "t,~(b)";
7f3c4072 11013 offbits = 12;
df58fc94
RS
11014 goto ld_st;
11015 case M_SDP_AB:
df58fc94
RS
11016 gas_assert (mips_opts.micromips);
11017 s = "sdp";
11018 fmt = "t,~(b)";
7f3c4072 11019 offbits = 12;
df58fc94
RS
11020 goto ld_st;
11021 case M_SWM_AB:
df58fc94
RS
11022 gas_assert (mips_opts.micromips);
11023 s = "swm";
11024 fmt = "n,~(b)";
7f3c4072 11025 offbits = 12;
df58fc94
RS
11026 goto ld_st;
11027 case M_SDM_AB:
df58fc94
RS
11028 gas_assert (mips_opts.micromips);
11029 s = "sdm";
11030 fmt = "n,~(b)";
7f3c4072 11031 offbits = 12;
df58fc94
RS
11032
11033 ld_st:
8fc2e39e 11034 tempreg = AT;
df58fc94 11035 ld_noat:
c0ebe874 11036 breg = op[2];
f2ae14a1
RS
11037 if (small_offset_p (0, align, 16))
11038 {
11039 /* The first case exists for M_LD_AB and M_SD_AB, which are
11040 macros for o32 but which should act like normal instructions
11041 otherwise. */
11042 if (offbits == 16)
c0ebe874 11043 macro_build (&offset_expr, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
11044 offset_reloc[1], offset_reloc[2], breg);
11045 else if (small_offset_p (0, align, offbits))
11046 {
11047 if (offbits == 0)
c0ebe874 11048 macro_build (NULL, s, fmt, op[0], breg);
f2ae14a1 11049 else
c0ebe874 11050 macro_build (NULL, s, fmt, op[0],
c8276761 11051 (int) offset_expr.X_add_number, breg);
f2ae14a1
RS
11052 }
11053 else
11054 {
11055 if (tempreg == AT)
11056 used_at = 1;
11057 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11058 tempreg, breg, -1, offset_reloc[0],
11059 offset_reloc[1], offset_reloc[2]);
11060 if (offbits == 0)
c0ebe874 11061 macro_build (NULL, s, fmt, op[0], tempreg);
f2ae14a1 11062 else
c0ebe874 11063 macro_build (NULL, s, fmt, op[0], 0, tempreg);
f2ae14a1
RS
11064 }
11065 break;
11066 }
11067
11068 if (tempreg == AT)
11069 used_at = 1;
11070
252b5132
RH
11071 if (offset_expr.X_op != O_constant
11072 && offset_expr.X_op != O_symbol)
11073 {
1661c76c 11074 as_bad (_("expression too complex"));
252b5132
RH
11075 offset_expr.X_op = O_constant;
11076 }
11077
2051e8c4
MR
11078 if (HAVE_32BIT_ADDRESSES
11079 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
11080 {
11081 char value [32];
11082
11083 sprintf_vma (value, offset_expr.X_add_number);
1661c76c 11084 as_bad (_("number (0x%s) larger than 32 bits"), value);
55e08f71 11085 }
2051e8c4 11086
252b5132
RH
11087 /* A constant expression in PIC code can be handled just as it
11088 is in non PIC code. */
aed1a261
RS
11089 if (offset_expr.X_op == O_constant)
11090 {
f2ae14a1
RS
11091 expr1.X_add_number = offset_high_part (offset_expr.X_add_number,
11092 offbits == 0 ? 16 : offbits);
11093 offset_expr.X_add_number -= expr1.X_add_number;
df58fc94 11094
f2ae14a1
RS
11095 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
11096 if (breg != 0)
11097 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11098 tempreg, tempreg, breg);
7f3c4072 11099 if (offbits == 0)
dd6a37e7 11100 {
f2ae14a1 11101 if (offset_expr.X_add_number != 0)
dd6a37e7 11102 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
f2ae14a1 11103 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
c0ebe874 11104 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 11105 }
7f3c4072 11106 else if (offbits == 16)
c0ebe874 11107 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
df58fc94 11108 else
c0ebe874 11109 macro_build (NULL, s, fmt, op[0],
c8276761 11110 (int) offset_expr.X_add_number, tempreg);
df58fc94 11111 }
7f3c4072 11112 else if (offbits != 16)
df58fc94 11113 {
7f3c4072
CM
11114 /* The offset field is too narrow to be used for a low-part
11115 relocation, so load the whole address into the auxillary
f2ae14a1
RS
11116 register. */
11117 load_address (tempreg, &offset_expr, &used_at);
11118 if (breg != 0)
11119 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11120 tempreg, tempreg, breg);
7f3c4072 11121 if (offbits == 0)
c0ebe874 11122 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 11123 else
c0ebe874 11124 macro_build (NULL, s, fmt, op[0], 0, tempreg);
aed1a261
RS
11125 }
11126 else if (mips_pic == NO_PIC)
252b5132
RH
11127 {
11128 /* If this is a reference to a GP relative symbol, and there
11129 is no base register, we want
c0ebe874 11130 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
11131 Otherwise, if there is no base register, we want
11132 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
c0ebe874 11133 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
252b5132
RH
11134 If we have a constant, we need two instructions anyhow,
11135 so we always use the latter form.
11136
11137 If we have a base register, and this is a reference to a
11138 GP relative symbol, we want
11139 addu $tempreg,$breg,$gp
c0ebe874 11140 <op> op[0],<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
11141 Otherwise we want
11142 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
11143 addu $tempreg,$tempreg,$breg
c0ebe874 11144 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 11145 With a constant we always use the latter case.
76b3015f 11146
d6bc6245
TS
11147 With 64bit address space and no base register and $at usable,
11148 we want
11149 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11150 lui $at,<sym> (BFD_RELOC_HI16_S)
11151 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11152 dsll32 $tempreg,0
11153 daddu $tempreg,$at
c0ebe874 11154 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
11155 If we have a base register, we want
11156 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11157 lui $at,<sym> (BFD_RELOC_HI16_S)
11158 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11159 daddu $at,$breg
11160 dsll32 $tempreg,0
11161 daddu $tempreg,$at
c0ebe874 11162 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
11163
11164 Without $at we can't generate the optimal path for superscalar
11165 processors here since this would require two temporary registers.
11166 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11167 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11168 dsll $tempreg,16
11169 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
11170 dsll $tempreg,16
c0ebe874 11171 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
11172 If we have a base register, we want
11173 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11174 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11175 dsll $tempreg,16
11176 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
11177 dsll $tempreg,16
11178 daddu $tempreg,$tempreg,$breg
c0ebe874 11179 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 11180
6caf9ef4 11181 For GP relative symbols in 64bit address space we can use
aed1a261
RS
11182 the same sequence as in 32bit address space. */
11183 if (HAVE_64BIT_SYMBOLS)
d6bc6245 11184 {
aed1a261 11185 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
11186 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
11187 {
11188 relax_start (offset_expr.X_add_symbol);
11189 if (breg == 0)
11190 {
c0ebe874 11191 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
11192 BFD_RELOC_GPREL16, mips_gp_register);
11193 }
11194 else
11195 {
11196 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11197 tempreg, breg, mips_gp_register);
c0ebe874 11198 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
11199 BFD_RELOC_GPREL16, tempreg);
11200 }
11201 relax_switch ();
11202 }
d6bc6245 11203
741fe287 11204 if (used_at == 0 && mips_opts.at)
d6bc6245 11205 {
df58fc94 11206 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb 11207 BFD_RELOC_MIPS_HIGHEST);
df58fc94 11208 macro_build (&offset_expr, "lui", LUI_FMT, AT,
67c0d1eb
RS
11209 BFD_RELOC_HI16_S);
11210 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
11211 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 11212 if (breg != 0)
67c0d1eb 11213 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
df58fc94 11214 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 11215 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
c0ebe874 11216 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16,
67c0d1eb 11217 tempreg);
d6bc6245
TS
11218 used_at = 1;
11219 }
11220 else
11221 {
df58fc94 11222 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb
RS
11223 BFD_RELOC_MIPS_HIGHEST);
11224 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
11225 tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 11226 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb
RS
11227 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
11228 tempreg, BFD_RELOC_HI16_S);
df58fc94 11229 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
d6bc6245 11230 if (breg != 0)
67c0d1eb 11231 macro_build (NULL, "daddu", "d,v,t",
17a2f251 11232 tempreg, tempreg, breg);
c0ebe874 11233 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 11234 BFD_RELOC_LO16, tempreg);
d6bc6245 11235 }
6caf9ef4
TS
11236
11237 if (mips_relax.sequence)
11238 relax_end ();
8fc2e39e 11239 break;
d6bc6245 11240 }
256ab948 11241
252b5132
RH
11242 if (breg == 0)
11243 {
67c0d1eb 11244 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 11245 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 11246 {
4d7206a2 11247 relax_start (offset_expr.X_add_symbol);
c0ebe874 11248 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_GPREL16,
67c0d1eb 11249 mips_gp_register);
4d7206a2 11250 relax_switch ();
252b5132 11251 }
67c0d1eb 11252 macro_build_lui (&offset_expr, tempreg);
c0ebe874 11253 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 11254 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
11255 if (mips_relax.sequence)
11256 relax_end ();
252b5132
RH
11257 }
11258 else
11259 {
67c0d1eb 11260 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 11261 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 11262 {
4d7206a2 11263 relax_start (offset_expr.X_add_symbol);
67c0d1eb 11264 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11265 tempreg, breg, mips_gp_register);
c0ebe874 11266 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 11267 BFD_RELOC_GPREL16, tempreg);
4d7206a2 11268 relax_switch ();
252b5132 11269 }
67c0d1eb
RS
11270 macro_build_lui (&offset_expr, tempreg);
11271 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11272 tempreg, tempreg, breg);
c0ebe874 11273 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 11274 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
11275 if (mips_relax.sequence)
11276 relax_end ();
252b5132
RH
11277 }
11278 }
0a44bf69 11279 else if (!mips_big_got)
252b5132 11280 {
ed6fb7bd 11281 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 11282
252b5132
RH
11283 /* If this is a reference to an external symbol, we want
11284 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11285 nop
c0ebe874 11286 <op> op[0],0($tempreg)
252b5132
RH
11287 Otherwise we want
11288 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11289 nop
11290 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 11291 <op> op[0],0($tempreg)
f5040a92
AO
11292
11293 For NewABI, we want
11294 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 11295 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 11296
252b5132
RH
11297 If there is a base register, we add it to $tempreg before
11298 the <op>. If there is a constant, we stick it in the
11299 <op> instruction. We don't handle constants larger than
11300 16 bits, because we have no way to load the upper 16 bits
11301 (actually, we could handle them for the subset of cases
11302 in which we are not using $at). */
9c2799c2 11303 gas_assert (offset_expr.X_op == O_symbol);
f5040a92
AO
11304 if (HAVE_NEWABI)
11305 {
67c0d1eb
RS
11306 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11307 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 11308 if (breg != 0)
67c0d1eb 11309 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11310 tempreg, tempreg, breg);
c0ebe874 11311 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 11312 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
11313 break;
11314 }
252b5132
RH
11315 expr1.X_add_number = offset_expr.X_add_number;
11316 offset_expr.X_add_number = 0;
11317 if (expr1.X_add_number < -0x8000
11318 || expr1.X_add_number >= 0x8000)
11319 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
11320 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11321 lw_reloc_type, mips_gp_register);
269137b2 11322 load_delay_nop ();
4d7206a2
RS
11323 relax_start (offset_expr.X_add_symbol);
11324 relax_switch ();
67c0d1eb
RS
11325 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11326 tempreg, BFD_RELOC_LO16);
4d7206a2 11327 relax_end ();
252b5132 11328 if (breg != 0)
67c0d1eb 11329 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11330 tempreg, tempreg, breg);
c0ebe874 11331 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 11332 }
0a44bf69 11333 else if (mips_big_got && !HAVE_NEWABI)
252b5132 11334 {
67c0d1eb 11335 int gpdelay;
252b5132
RH
11336
11337 /* If this is a reference to an external symbol, we want
11338 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11339 addu $tempreg,$tempreg,$gp
11340 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 11341 <op> op[0],0($tempreg)
252b5132
RH
11342 Otherwise we want
11343 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11344 nop
11345 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 11346 <op> op[0],0($tempreg)
252b5132
RH
11347 If there is a base register, we add it to $tempreg before
11348 the <op>. If there is a constant, we stick it in the
11349 <op> instruction. We don't handle constants larger than
11350 16 bits, because we have no way to load the upper 16 bits
11351 (actually, we could handle them for the subset of cases
f5040a92 11352 in which we are not using $at). */
9c2799c2 11353 gas_assert (offset_expr.X_op == O_symbol);
252b5132
RH
11354 expr1.X_add_number = offset_expr.X_add_number;
11355 offset_expr.X_add_number = 0;
11356 if (expr1.X_add_number < -0x8000
11357 || expr1.X_add_number >= 0x8000)
11358 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 11359 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 11360 relax_start (offset_expr.X_add_symbol);
df58fc94 11361 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 11362 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
11363 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
11364 mips_gp_register);
11365 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11366 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 11367 relax_switch ();
67c0d1eb
RS
11368 if (gpdelay)
11369 macro_build (NULL, "nop", "");
11370 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11371 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 11372 load_delay_nop ();
67c0d1eb
RS
11373 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11374 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
11375 relax_end ();
11376
252b5132 11377 if (breg != 0)
67c0d1eb 11378 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11379 tempreg, tempreg, breg);
c0ebe874 11380 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 11381 }
0a44bf69 11382 else if (mips_big_got && HAVE_NEWABI)
f5040a92 11383 {
f5040a92
AO
11384 /* If this is a reference to an external symbol, we want
11385 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11386 add $tempreg,$tempreg,$gp
11387 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 11388 <op> op[0],<ofst>($tempreg)
f5040a92
AO
11389 Otherwise, for local symbols, we want:
11390 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 11391 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
9c2799c2 11392 gas_assert (offset_expr.X_op == O_symbol);
4d7206a2 11393 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
11394 offset_expr.X_add_number = 0;
11395 if (expr1.X_add_number < -0x8000
11396 || expr1.X_add_number >= 0x8000)
11397 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 11398 relax_start (offset_expr.X_add_symbol);
df58fc94 11399 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 11400 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
11401 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
11402 mips_gp_register);
11403 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11404 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 11405 if (breg != 0)
67c0d1eb 11406 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11407 tempreg, tempreg, breg);
c0ebe874 11408 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
684022ea 11409
4d7206a2 11410 relax_switch ();
f5040a92 11411 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
11412 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11413 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 11414 if (breg != 0)
67c0d1eb 11415 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11416 tempreg, tempreg, breg);
c0ebe874 11417 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 11418 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 11419 relax_end ();
f5040a92 11420 }
252b5132
RH
11421 else
11422 abort ();
11423
252b5132
RH
11424 break;
11425
833794fc
MR
11426 case M_JRADDIUSP:
11427 gas_assert (mips_opts.micromips);
11428 gas_assert (mips_opts.insn32);
11429 start_noreorder ();
11430 macro_build (NULL, "jr", "s", RA);
c0ebe874 11431 expr1.X_add_number = op[0] << 2;
833794fc
MR
11432 macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
11433 end_noreorder ();
11434 break;
11435
11436 case M_JRC:
11437 gas_assert (mips_opts.micromips);
11438 gas_assert (mips_opts.insn32);
c0ebe874 11439 macro_build (NULL, "jr", "s", op[0]);
833794fc
MR
11440 if (mips_opts.noreorder)
11441 macro_build (NULL, "nop", "");
11442 break;
11443
252b5132
RH
11444 case M_LI:
11445 case M_LI_S:
c0ebe874 11446 load_register (op[0], &imm_expr, 0);
8fc2e39e 11447 break;
252b5132
RH
11448
11449 case M_DLI:
c0ebe874 11450 load_register (op[0], &imm_expr, 1);
8fc2e39e 11451 break;
252b5132
RH
11452
11453 case M_LI_SS:
11454 if (imm_expr.X_op == O_constant)
11455 {
8fc2e39e 11456 used_at = 1;
67c0d1eb 11457 load_register (AT, &imm_expr, 0);
c0ebe874 11458 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
11459 break;
11460 }
11461 else
11462 {
b0e6f033
RS
11463 gas_assert (imm_expr.X_op == O_absent
11464 && offset_expr.X_op == O_symbol
90ecf173
MR
11465 && strcmp (segment_name (S_GET_SEGMENT
11466 (offset_expr.X_add_symbol)),
11467 ".lit4") == 0
11468 && offset_expr.X_add_number == 0);
c0ebe874 11469 macro_build (&offset_expr, "lwc1", "T,o(b)", op[0],
17a2f251 11470 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 11471 break;
252b5132
RH
11472 }
11473
11474 case M_LI_D:
ca4e0257
RS
11475 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
11476 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
11477 order 32 bits of the value and the low order 32 bits are either
11478 zero or in OFFSET_EXPR. */
b0e6f033 11479 if (imm_expr.X_op == O_constant)
252b5132 11480 {
ca4e0257 11481 if (HAVE_64BIT_GPRS)
c0ebe874 11482 load_register (op[0], &imm_expr, 1);
252b5132
RH
11483 else
11484 {
11485 int hreg, lreg;
11486
11487 if (target_big_endian)
11488 {
c0ebe874
RS
11489 hreg = op[0];
11490 lreg = op[0] + 1;
252b5132
RH
11491 }
11492 else
11493 {
c0ebe874
RS
11494 hreg = op[0] + 1;
11495 lreg = op[0];
252b5132
RH
11496 }
11497
11498 if (hreg <= 31)
67c0d1eb 11499 load_register (hreg, &imm_expr, 0);
252b5132
RH
11500 if (lreg <= 31)
11501 {
11502 if (offset_expr.X_op == O_absent)
67c0d1eb 11503 move_register (lreg, 0);
252b5132
RH
11504 else
11505 {
9c2799c2 11506 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 11507 load_register (lreg, &offset_expr, 0);
252b5132
RH
11508 }
11509 }
11510 }
8fc2e39e 11511 break;
252b5132 11512 }
b0e6f033 11513 gas_assert (imm_expr.X_op == O_absent);
252b5132
RH
11514
11515 /* We know that sym is in the .rdata section. First we get the
11516 upper 16 bits of the address. */
11517 if (mips_pic == NO_PIC)
11518 {
67c0d1eb 11519 macro_build_lui (&offset_expr, AT);
8fc2e39e 11520 used_at = 1;
252b5132 11521 }
0a44bf69 11522 else
252b5132 11523 {
67c0d1eb
RS
11524 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
11525 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 11526 used_at = 1;
252b5132 11527 }
bdaaa2e1 11528
252b5132 11529 /* Now we load the register(s). */
ca4e0257 11530 if (HAVE_64BIT_GPRS)
8fc2e39e
TS
11531 {
11532 used_at = 1;
c0ebe874
RS
11533 macro_build (&offset_expr, "ld", "t,o(b)", op[0],
11534 BFD_RELOC_LO16, AT);
8fc2e39e 11535 }
252b5132
RH
11536 else
11537 {
8fc2e39e 11538 used_at = 1;
c0ebe874
RS
11539 macro_build (&offset_expr, "lw", "t,o(b)", op[0],
11540 BFD_RELOC_LO16, AT);
11541 if (op[0] != RA)
252b5132
RH
11542 {
11543 /* FIXME: How in the world do we deal with the possible
11544 overflow here? */
11545 offset_expr.X_add_number += 4;
67c0d1eb 11546 macro_build (&offset_expr, "lw", "t,o(b)",
c0ebe874 11547 op[0] + 1, BFD_RELOC_LO16, AT);
252b5132
RH
11548 }
11549 }
252b5132
RH
11550 break;
11551
11552 case M_LI_DD:
ca4e0257
RS
11553 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
11554 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
11555 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
11556 the value and the low order 32 bits are either zero or in
11557 OFFSET_EXPR. */
b0e6f033 11558 if (imm_expr.X_op == O_constant)
252b5132 11559 {
8fc2e39e 11560 used_at = 1;
67c0d1eb 11561 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
ca4e0257
RS
11562 if (HAVE_64BIT_FPRS)
11563 {
9c2799c2 11564 gas_assert (HAVE_64BIT_GPRS);
c0ebe874 11565 macro_build (NULL, "dmtc1", "t,S", AT, op[0]);
ca4e0257 11566 }
252b5132
RH
11567 else
11568 {
c0ebe874 11569 macro_build (NULL, "mtc1", "t,G", AT, op[0] + 1);
252b5132 11570 if (offset_expr.X_op == O_absent)
c0ebe874 11571 macro_build (NULL, "mtc1", "t,G", 0, op[0]);
252b5132
RH
11572 else
11573 {
9c2799c2 11574 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 11575 load_register (AT, &offset_expr, 0);
c0ebe874 11576 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
11577 }
11578 }
11579 break;
11580 }
11581
b0e6f033
RS
11582 gas_assert (imm_expr.X_op == O_absent
11583 && offset_expr.X_op == O_symbol
90ecf173 11584 && offset_expr.X_add_number == 0);
252b5132
RH
11585 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
11586 if (strcmp (s, ".lit8") == 0)
f2ae14a1 11587 {
c0ebe874 11588 op[2] = mips_gp_register;
f2ae14a1
RS
11589 offset_reloc[0] = BFD_RELOC_MIPS_LITERAL;
11590 offset_reloc[1] = BFD_RELOC_UNUSED;
11591 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
11592 }
11593 else
11594 {
9c2799c2 11595 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 11596 used_at = 1;
0a44bf69 11597 if (mips_pic != NO_PIC)
67c0d1eb
RS
11598 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
11599 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
11600 else
11601 {
11602 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 11603 macro_build_lui (&offset_expr, AT);
252b5132 11604 }
bdaaa2e1 11605
c0ebe874 11606 op[2] = AT;
f2ae14a1
RS
11607 offset_reloc[0] = BFD_RELOC_LO16;
11608 offset_reloc[1] = BFD_RELOC_UNUSED;
11609 offset_reloc[2] = BFD_RELOC_UNUSED;
11610 }
11611 align = 8;
11612 /* Fall through */
c4a68bea 11613
252b5132
RH
11614 case M_L_DAB:
11615 /*
11616 * The MIPS assembler seems to check for X_add_number not
11617 * being double aligned and generating:
11618 * lui at,%hi(foo+1)
11619 * addu at,at,v1
11620 * addiu at,at,%lo(foo+1)
11621 * lwc1 f2,0(at)
11622 * lwc1 f3,4(at)
11623 * But, the resulting address is the same after relocation so why
11624 * generate the extra instruction?
11625 */
bdaaa2e1 11626 /* Itbl support may require additional care here. */
252b5132 11627 coproc = 1;
df58fc94 11628 fmt = "T,o(b)";
0aa27725 11629 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
11630 {
11631 s = "ldc1";
df58fc94 11632 goto ld_st;
252b5132 11633 }
252b5132 11634 s = "lwc1";
252b5132
RH
11635 goto ldd_std;
11636
11637 case M_S_DAB:
df58fc94
RS
11638 gas_assert (!mips_opts.micromips);
11639 /* Itbl support may require additional care here. */
11640 coproc = 1;
11641 fmt = "T,o(b)";
0aa27725 11642 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
11643 {
11644 s = "sdc1";
df58fc94 11645 goto ld_st;
252b5132 11646 }
252b5132 11647 s = "swc1";
252b5132
RH
11648 goto ldd_std;
11649
e407c74b
NC
11650 case M_LQ_AB:
11651 fmt = "t,o(b)";
11652 s = "lq";
11653 goto ld;
11654
11655 case M_SQ_AB:
11656 fmt = "t,o(b)";
11657 s = "sq";
11658 goto ld_st;
11659
252b5132 11660 case M_LD_AB:
df58fc94 11661 fmt = "t,o(b)";
ca4e0257 11662 if (HAVE_64BIT_GPRS)
252b5132
RH
11663 {
11664 s = "ld";
11665 goto ld;
11666 }
252b5132 11667 s = "lw";
252b5132
RH
11668 goto ldd_std;
11669
11670 case M_SD_AB:
df58fc94 11671 fmt = "t,o(b)";
ca4e0257 11672 if (HAVE_64BIT_GPRS)
252b5132
RH
11673 {
11674 s = "sd";
df58fc94 11675 goto ld_st;
252b5132 11676 }
252b5132 11677 s = "sw";
252b5132
RH
11678
11679 ldd_std:
f2ae14a1
RS
11680 /* Even on a big endian machine $fn comes before $fn+1. We have
11681 to adjust when loading from memory. We set coproc if we must
11682 load $fn+1 first. */
11683 /* Itbl support may require additional care here. */
11684 if (!target_big_endian)
11685 coproc = 0;
11686
c0ebe874 11687 breg = op[2];
f2ae14a1
RS
11688 if (small_offset_p (0, align, 16))
11689 {
11690 ep = &offset_expr;
11691 if (!small_offset_p (4, align, 16))
11692 {
11693 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT, breg,
11694 -1, offset_reloc[0], offset_reloc[1],
11695 offset_reloc[2]);
11696 expr1.X_add_number = 0;
11697 ep = &expr1;
11698 breg = AT;
11699 used_at = 1;
11700 offset_reloc[0] = BFD_RELOC_LO16;
11701 offset_reloc[1] = BFD_RELOC_UNUSED;
11702 offset_reloc[2] = BFD_RELOC_UNUSED;
11703 }
c0ebe874 11704 if (strcmp (s, "lw") == 0 && op[0] == breg)
f2ae14a1
RS
11705 {
11706 ep->X_add_number += 4;
c0ebe874 11707 macro_build (ep, s, fmt, op[0] + 1, -1, offset_reloc[0],
f2ae14a1
RS
11708 offset_reloc[1], offset_reloc[2], breg);
11709 ep->X_add_number -= 4;
c0ebe874 11710 macro_build (ep, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
11711 offset_reloc[1], offset_reloc[2], breg);
11712 }
11713 else
11714 {
c0ebe874 11715 macro_build (ep, s, fmt, coproc ? op[0] + 1 : op[0], -1,
f2ae14a1
RS
11716 offset_reloc[0], offset_reloc[1], offset_reloc[2],
11717 breg);
11718 ep->X_add_number += 4;
c0ebe874 11719 macro_build (ep, s, fmt, coproc ? op[0] : op[0] + 1, -1,
f2ae14a1
RS
11720 offset_reloc[0], offset_reloc[1], offset_reloc[2],
11721 breg);
11722 }
11723 break;
11724 }
11725
252b5132
RH
11726 if (offset_expr.X_op != O_symbol
11727 && offset_expr.X_op != O_constant)
11728 {
1661c76c 11729 as_bad (_("expression too complex"));
252b5132
RH
11730 offset_expr.X_op = O_constant;
11731 }
11732
2051e8c4
MR
11733 if (HAVE_32BIT_ADDRESSES
11734 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
11735 {
11736 char value [32];
11737
11738 sprintf_vma (value, offset_expr.X_add_number);
1661c76c 11739 as_bad (_("number (0x%s) larger than 32 bits"), value);
55e08f71 11740 }
2051e8c4 11741
90ecf173 11742 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
252b5132
RH
11743 {
11744 /* If this is a reference to a GP relative symbol, we want
c0ebe874
RS
11745 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
11746 <op> op[0]+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
11747 If we have a base register, we use this
11748 addu $at,$breg,$gp
c0ebe874
RS
11749 <op> op[0],<sym>($at) (BFD_RELOC_GPREL16)
11750 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
11751 If this is not a GP relative symbol, we want
11752 lui $at,<sym> (BFD_RELOC_HI16_S)
c0ebe874
RS
11753 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
11754 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
11755 If there is a base register, we add it to $at after the
11756 lui instruction. If there is a constant, we always use
11757 the last case. */
39a59cf8
MR
11758 if (offset_expr.X_op == O_symbol
11759 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 11760 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 11761 {
4d7206a2 11762 relax_start (offset_expr.X_add_symbol);
252b5132
RH
11763 if (breg == 0)
11764 {
c9914766 11765 tempreg = mips_gp_register;
252b5132
RH
11766 }
11767 else
11768 {
67c0d1eb 11769 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11770 AT, breg, mips_gp_register);
252b5132 11771 tempreg = AT;
252b5132
RH
11772 used_at = 1;
11773 }
11774
beae10d5 11775 /* Itbl support may require additional care here. */
c0ebe874 11776 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 11777 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
11778 offset_expr.X_add_number += 4;
11779
11780 /* Set mips_optimize to 2 to avoid inserting an
11781 undesired nop. */
11782 hold_mips_optimize = mips_optimize;
11783 mips_optimize = 2;
beae10d5 11784 /* Itbl support may require additional care here. */
c0ebe874 11785 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 11786 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
11787 mips_optimize = hold_mips_optimize;
11788
4d7206a2 11789 relax_switch ();
252b5132 11790
0970e49e 11791 offset_expr.X_add_number -= 4;
252b5132 11792 }
8fc2e39e 11793 used_at = 1;
f2ae14a1
RS
11794 if (offset_high_part (offset_expr.X_add_number, 16)
11795 != offset_high_part (offset_expr.X_add_number + 4, 16))
11796 {
11797 load_address (AT, &offset_expr, &used_at);
11798 offset_expr.X_op = O_constant;
11799 offset_expr.X_add_number = 0;
11800 }
11801 else
11802 macro_build_lui (&offset_expr, AT);
252b5132 11803 if (breg != 0)
67c0d1eb 11804 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 11805 /* Itbl support may require additional care here. */
c0ebe874 11806 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 11807 BFD_RELOC_LO16, AT);
252b5132
RH
11808 /* FIXME: How do we handle overflow here? */
11809 offset_expr.X_add_number += 4;
beae10d5 11810 /* Itbl support may require additional care here. */
c0ebe874 11811 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 11812 BFD_RELOC_LO16, AT);
4d7206a2
RS
11813 if (mips_relax.sequence)
11814 relax_end ();
bdaaa2e1 11815 }
0a44bf69 11816 else if (!mips_big_got)
252b5132 11817 {
252b5132
RH
11818 /* If this is a reference to an external symbol, we want
11819 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11820 nop
c0ebe874
RS
11821 <op> op[0],0($at)
11822 <op> op[0]+1,4($at)
252b5132
RH
11823 Otherwise we want
11824 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11825 nop
c0ebe874
RS
11826 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
11827 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
11828 If there is a base register we add it to $at before the
11829 lwc1 instructions. If there is a constant we include it
11830 in the lwc1 instructions. */
11831 used_at = 1;
11832 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
11833 if (expr1.X_add_number < -0x8000
11834 || expr1.X_add_number >= 0x8000 - 4)
11835 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 11836 load_got_offset (AT, &offset_expr);
269137b2 11837 load_delay_nop ();
252b5132 11838 if (breg != 0)
67c0d1eb 11839 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
11840
11841 /* Set mips_optimize to 2 to avoid inserting an undesired
11842 nop. */
11843 hold_mips_optimize = mips_optimize;
11844 mips_optimize = 2;
4d7206a2 11845
beae10d5 11846 /* Itbl support may require additional care here. */
4d7206a2 11847 relax_start (offset_expr.X_add_symbol);
c0ebe874 11848 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 11849 BFD_RELOC_LO16, AT);
4d7206a2 11850 expr1.X_add_number += 4;
c0ebe874 11851 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 11852 BFD_RELOC_LO16, AT);
4d7206a2 11853 relax_switch ();
c0ebe874 11854 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 11855 BFD_RELOC_LO16, AT);
4d7206a2 11856 offset_expr.X_add_number += 4;
c0ebe874 11857 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 11858 BFD_RELOC_LO16, AT);
4d7206a2 11859 relax_end ();
252b5132 11860
4d7206a2 11861 mips_optimize = hold_mips_optimize;
252b5132 11862 }
0a44bf69 11863 else if (mips_big_got)
252b5132 11864 {
67c0d1eb 11865 int gpdelay;
252b5132
RH
11866
11867 /* If this is a reference to an external symbol, we want
11868 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11869 addu $at,$at,$gp
11870 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
11871 nop
c0ebe874
RS
11872 <op> op[0],0($at)
11873 <op> op[0]+1,4($at)
252b5132
RH
11874 Otherwise we want
11875 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11876 nop
c0ebe874
RS
11877 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
11878 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
11879 If there is a base register we add it to $at before the
11880 lwc1 instructions. If there is a constant we include it
11881 in the lwc1 instructions. */
11882 used_at = 1;
11883 expr1.X_add_number = offset_expr.X_add_number;
11884 offset_expr.X_add_number = 0;
11885 if (expr1.X_add_number < -0x8000
11886 || expr1.X_add_number >= 0x8000 - 4)
11887 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 11888 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 11889 relax_start (offset_expr.X_add_symbol);
df58fc94 11890 macro_build (&offset_expr, "lui", LUI_FMT,
67c0d1eb
RS
11891 AT, BFD_RELOC_MIPS_GOT_HI16);
11892 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11893 AT, AT, mips_gp_register);
67c0d1eb 11894 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 11895 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 11896 load_delay_nop ();
252b5132 11897 if (breg != 0)
67c0d1eb 11898 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 11899 /* Itbl support may require additional care here. */
c0ebe874 11900 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 11901 BFD_RELOC_LO16, AT);
252b5132
RH
11902 expr1.X_add_number += 4;
11903
11904 /* Set mips_optimize to 2 to avoid inserting an undesired
11905 nop. */
11906 hold_mips_optimize = mips_optimize;
11907 mips_optimize = 2;
beae10d5 11908 /* Itbl support may require additional care here. */
c0ebe874 11909 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 11910 BFD_RELOC_LO16, AT);
252b5132
RH
11911 mips_optimize = hold_mips_optimize;
11912 expr1.X_add_number -= 4;
11913
4d7206a2
RS
11914 relax_switch ();
11915 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
11916 if (gpdelay)
11917 macro_build (NULL, "nop", "");
11918 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
11919 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 11920 load_delay_nop ();
252b5132 11921 if (breg != 0)
67c0d1eb 11922 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 11923 /* Itbl support may require additional care here. */
c0ebe874 11924 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 11925 BFD_RELOC_LO16, AT);
4d7206a2 11926 offset_expr.X_add_number += 4;
252b5132
RH
11927
11928 /* Set mips_optimize to 2 to avoid inserting an undesired
11929 nop. */
11930 hold_mips_optimize = mips_optimize;
11931 mips_optimize = 2;
beae10d5 11932 /* Itbl support may require additional care here. */
c0ebe874 11933 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 11934 BFD_RELOC_LO16, AT);
252b5132 11935 mips_optimize = hold_mips_optimize;
4d7206a2 11936 relax_end ();
252b5132 11937 }
252b5132
RH
11938 else
11939 abort ();
11940
252b5132 11941 break;
dd6a37e7
AP
11942
11943 case M_SAA_AB:
dd6a37e7 11944 s = "saa";
7f3c4072 11945 offbits = 0;
dd6a37e7
AP
11946 fmt = "t,(b)";
11947 goto ld_st;
11948 case M_SAAD_AB:
dd6a37e7 11949 s = "saad";
7f3c4072 11950 offbits = 0;
dd6a37e7
AP
11951 fmt = "t,(b)";
11952 goto ld_st;
11953
252b5132
RH
11954 /* New code added to support COPZ instructions.
11955 This code builds table entries out of the macros in mip_opcodes.
11956 R4000 uses interlocks to handle coproc delays.
11957 Other chips (like the R3000) require nops to be inserted for delays.
11958
f72c8c98 11959 FIXME: Currently, we require that the user handle delays.
252b5132
RH
11960 In order to fill delay slots for non-interlocked chips,
11961 we must have a way to specify delays based on the coprocessor.
11962 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
11963 What are the side-effects of the cop instruction?
11964 What cache support might we have and what are its effects?
11965 Both coprocessor & memory require delays. how long???
bdaaa2e1 11966 What registers are read/set/modified?
252b5132
RH
11967
11968 If an itbl is provided to interpret cop instructions,
bdaaa2e1 11969 this knowledge can be encoded in the itbl spec. */
252b5132
RH
11970
11971 case M_COP0:
11972 s = "c0";
11973 goto copz;
11974 case M_COP1:
11975 s = "c1";
11976 goto copz;
11977 case M_COP2:
11978 s = "c2";
11979 goto copz;
11980 case M_COP3:
11981 s = "c3";
11982 copz:
df58fc94 11983 gas_assert (!mips_opts.micromips);
252b5132
RH
11984 /* For now we just do C (same as Cz). The parameter will be
11985 stored in insn_opcode by mips_ip. */
c8276761 11986 macro_build (NULL, s, "C", (int) ip->insn_opcode);
8fc2e39e 11987 break;
252b5132 11988
ea1fb5dc 11989 case M_MOVE:
c0ebe874 11990 move_register (op[0], op[1]);
8fc2e39e 11991 break;
ea1fb5dc 11992
833794fc
MR
11993 case M_MOVEP:
11994 gas_assert (mips_opts.micromips);
11995 gas_assert (mips_opts.insn32);
c0ebe874
RS
11996 move_register (micromips_to_32_reg_h_map1[op[0]],
11997 micromips_to_32_reg_m_map[op[1]]);
11998 move_register (micromips_to_32_reg_h_map2[op[0]],
11999 micromips_to_32_reg_n_map[op[2]]);
833794fc
MR
12000 break;
12001
252b5132
RH
12002 case M_DMUL:
12003 dbl = 1;
12004 case M_MUL:
e407c74b 12005 if (mips_opts.arch == CPU_R5900)
c0ebe874
RS
12006 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", op[0], op[1],
12007 op[2]);
e407c74b
NC
12008 else
12009 {
c0ebe874
RS
12010 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", op[1], op[2]);
12011 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
e407c74b 12012 }
8fc2e39e 12013 break;
252b5132
RH
12014
12015 case M_DMUL_I:
12016 dbl = 1;
12017 case M_MUL_I:
12018 /* The MIPS assembler some times generates shifts and adds. I'm
12019 not trying to be that fancy. GCC should do this for us
12020 anyway. */
8fc2e39e 12021 used_at = 1;
67c0d1eb 12022 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
12023 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", op[1], AT);
12024 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
12025 break;
12026
12027 case M_DMULO_I:
12028 dbl = 1;
12029 case M_MULO_I:
12030 imm = 1;
12031 goto do_mulo;
12032
12033 case M_DMULO:
12034 dbl = 1;
12035 case M_MULO:
12036 do_mulo:
7d10b47d 12037 start_noreorder ();
8fc2e39e 12038 used_at = 1;
252b5132 12039 if (imm)
67c0d1eb 12040 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
12041 macro_build (NULL, dbl ? "dmult" : "mult", "s,t",
12042 op[1], imm ? AT : op[2]);
12043 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
12044 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, op[0], op[0], 31);
df58fc94 12045 macro_build (NULL, "mfhi", MFHL_FMT, AT);
252b5132 12046 if (mips_trap)
c0ebe874 12047 macro_build (NULL, "tne", TRAP_FMT, op[0], AT, 6);
252b5132
RH
12048 else
12049 {
df58fc94
RS
12050 if (mips_opts.micromips)
12051 micromips_label_expr (&label_expr);
12052 else
12053 label_expr.X_add_number = 8;
c0ebe874 12054 macro_build (&label_expr, "beq", "s,t,p", op[0], AT);
a605d2b3 12055 macro_build (NULL, "nop", "");
df58fc94
RS
12056 macro_build (NULL, "break", BRK_FMT, 6);
12057 if (mips_opts.micromips)
12058 micromips_add_label ();
252b5132 12059 }
7d10b47d 12060 end_noreorder ();
c0ebe874 12061 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
12062 break;
12063
12064 case M_DMULOU_I:
12065 dbl = 1;
12066 case M_MULOU_I:
12067 imm = 1;
12068 goto do_mulou;
12069
12070 case M_DMULOU:
12071 dbl = 1;
12072 case M_MULOU:
12073 do_mulou:
7d10b47d 12074 start_noreorder ();
8fc2e39e 12075 used_at = 1;
252b5132 12076 if (imm)
67c0d1eb
RS
12077 load_register (AT, &imm_expr, dbl);
12078 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
c0ebe874 12079 op[1], imm ? AT : op[2]);
df58fc94 12080 macro_build (NULL, "mfhi", MFHL_FMT, AT);
c0ebe874 12081 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132 12082 if (mips_trap)
df58fc94 12083 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
252b5132
RH
12084 else
12085 {
df58fc94
RS
12086 if (mips_opts.micromips)
12087 micromips_label_expr (&label_expr);
12088 else
12089 label_expr.X_add_number = 8;
12090 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
a605d2b3 12091 macro_build (NULL, "nop", "");
df58fc94
RS
12092 macro_build (NULL, "break", BRK_FMT, 6);
12093 if (mips_opts.micromips)
12094 micromips_add_label ();
252b5132 12095 }
7d10b47d 12096 end_noreorder ();
252b5132
RH
12097 break;
12098
771c7ce4 12099 case M_DROL:
fef14a42 12100 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 12101 {
c0ebe874 12102 if (op[0] == op[1])
82dd0097
CD
12103 {
12104 tempreg = AT;
12105 used_at = 1;
12106 }
12107 else
c0ebe874
RS
12108 tempreg = op[0];
12109 macro_build (NULL, "dnegu", "d,w", tempreg, op[2]);
12110 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 12111 break;
82dd0097 12112 }
8fc2e39e 12113 used_at = 1;
c0ebe874
RS
12114 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
12115 macro_build (NULL, "dsrlv", "d,t,s", AT, op[1], AT);
12116 macro_build (NULL, "dsllv", "d,t,s", op[0], op[1], op[2]);
12117 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
12118 break;
12119
252b5132 12120 case M_ROL:
fef14a42 12121 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 12122 {
c0ebe874 12123 if (op[0] == op[1])
82dd0097
CD
12124 {
12125 tempreg = AT;
12126 used_at = 1;
12127 }
12128 else
c0ebe874
RS
12129 tempreg = op[0];
12130 macro_build (NULL, "negu", "d,w", tempreg, op[2]);
12131 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 12132 break;
82dd0097 12133 }
8fc2e39e 12134 used_at = 1;
c0ebe874
RS
12135 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
12136 macro_build (NULL, "srlv", "d,t,s", AT, op[1], AT);
12137 macro_build (NULL, "sllv", "d,t,s", op[0], op[1], op[2]);
12138 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
12139 break;
12140
771c7ce4
TS
12141 case M_DROL_I:
12142 {
12143 unsigned int rot;
91d6fa6a
NC
12144 char *l;
12145 char *rr;
771c7ce4 12146
771c7ce4 12147 rot = imm_expr.X_add_number & 0x3f;
fef14a42 12148 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
12149 {
12150 rot = (64 - rot) & 0x3f;
12151 if (rot >= 32)
c0ebe874 12152 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
60b63b72 12153 else
c0ebe874 12154 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 12155 break;
60b63b72 12156 }
483fc7cd 12157 if (rot == 0)
483fc7cd 12158 {
c0ebe874 12159 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 12160 break;
483fc7cd 12161 }
82dd0097 12162 l = (rot < 0x20) ? "dsll" : "dsll32";
91d6fa6a 12163 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
82dd0097 12164 rot &= 0x1f;
8fc2e39e 12165 used_at = 1;
c0ebe874
RS
12166 macro_build (NULL, l, SHFT_FMT, AT, op[1], rot);
12167 macro_build (NULL, rr, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
12168 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
12169 }
12170 break;
12171
252b5132 12172 case M_ROL_I:
771c7ce4
TS
12173 {
12174 unsigned int rot;
12175
771c7ce4 12176 rot = imm_expr.X_add_number & 0x1f;
fef14a42 12177 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 12178 {
c0ebe874
RS
12179 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1],
12180 (32 - rot) & 0x1f);
8fc2e39e 12181 break;
60b63b72 12182 }
483fc7cd 12183 if (rot == 0)
483fc7cd 12184 {
c0ebe874 12185 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 12186 break;
483fc7cd 12187 }
8fc2e39e 12188 used_at = 1;
c0ebe874
RS
12189 macro_build (NULL, "sll", SHFT_FMT, AT, op[1], rot);
12190 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
12191 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
12192 }
12193 break;
12194
12195 case M_DROR:
fef14a42 12196 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 12197 {
c0ebe874 12198 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 12199 break;
82dd0097 12200 }
8fc2e39e 12201 used_at = 1;
c0ebe874
RS
12202 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
12203 macro_build (NULL, "dsllv", "d,t,s", AT, op[1], AT);
12204 macro_build (NULL, "dsrlv", "d,t,s", op[0], op[1], op[2]);
12205 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
12206 break;
12207
12208 case M_ROR:
fef14a42 12209 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 12210 {
c0ebe874 12211 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 12212 break;
82dd0097 12213 }
8fc2e39e 12214 used_at = 1;
c0ebe874
RS
12215 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
12216 macro_build (NULL, "sllv", "d,t,s", AT, op[1], AT);
12217 macro_build (NULL, "srlv", "d,t,s", op[0], op[1], op[2]);
12218 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
12219 break;
12220
771c7ce4
TS
12221 case M_DROR_I:
12222 {
12223 unsigned int rot;
91d6fa6a
NC
12224 char *l;
12225 char *rr;
771c7ce4 12226
771c7ce4 12227 rot = imm_expr.X_add_number & 0x3f;
fef14a42 12228 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
12229 {
12230 if (rot >= 32)
c0ebe874 12231 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
82dd0097 12232 else
c0ebe874 12233 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 12234 break;
82dd0097 12235 }
483fc7cd 12236 if (rot == 0)
483fc7cd 12237 {
c0ebe874 12238 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 12239 break;
483fc7cd 12240 }
91d6fa6a 12241 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
82dd0097
CD
12242 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
12243 rot &= 0x1f;
8fc2e39e 12244 used_at = 1;
c0ebe874
RS
12245 macro_build (NULL, rr, SHFT_FMT, AT, op[1], rot);
12246 macro_build (NULL, l, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
12247 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
12248 }
12249 break;
12250
252b5132 12251 case M_ROR_I:
771c7ce4
TS
12252 {
12253 unsigned int rot;
12254
771c7ce4 12255 rot = imm_expr.X_add_number & 0x1f;
fef14a42 12256 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 12257 {
c0ebe874 12258 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 12259 break;
82dd0097 12260 }
483fc7cd 12261 if (rot == 0)
483fc7cd 12262 {
c0ebe874 12263 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 12264 break;
483fc7cd 12265 }
8fc2e39e 12266 used_at = 1;
c0ebe874
RS
12267 macro_build (NULL, "srl", SHFT_FMT, AT, op[1], rot);
12268 macro_build (NULL, "sll", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
12269 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4 12270 }
252b5132
RH
12271 break;
12272
252b5132 12273 case M_SEQ:
c0ebe874
RS
12274 if (op[1] == 0)
12275 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[2], BFD_RELOC_LO16);
12276 else if (op[2] == 0)
12277 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
12278 else
12279 {
c0ebe874
RS
12280 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
12281 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
252b5132 12282 }
8fc2e39e 12283 break;
252b5132
RH
12284
12285 case M_SEQ_I:
b0e6f033 12286 if (imm_expr.X_add_number == 0)
252b5132 12287 {
c0ebe874 12288 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 12289 break;
252b5132 12290 }
c0ebe874 12291 if (op[1] == 0)
252b5132 12292 {
1661c76c 12293 as_warn (_("instruction %s: result is always false"),
252b5132 12294 ip->insn_mo->name);
c0ebe874 12295 move_register (op[0], 0);
8fc2e39e 12296 break;
252b5132 12297 }
dd3cbb7e
NC
12298 if (CPU_HAS_SEQ (mips_opts.arch)
12299 && -512 <= imm_expr.X_add_number
12300 && imm_expr.X_add_number < 512)
12301 {
c0ebe874 12302 macro_build (NULL, "seqi", "t,r,+Q", op[0], op[1],
750bdd57 12303 (int) imm_expr.X_add_number);
dd3cbb7e
NC
12304 break;
12305 }
b0e6f033 12306 if (imm_expr.X_add_number >= 0
252b5132 12307 && imm_expr.X_add_number < 0x10000)
c0ebe874 12308 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1], BFD_RELOC_LO16);
b0e6f033 12309 else if (imm_expr.X_add_number > -0x8000
252b5132
RH
12310 && imm_expr.X_add_number < 0)
12311 {
12312 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 12313 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
c0ebe874 12314 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 12315 }
dd3cbb7e
NC
12316 else if (CPU_HAS_SEQ (mips_opts.arch))
12317 {
12318 used_at = 1;
12319 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 12320 macro_build (NULL, "seq", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
12321 break;
12322 }
252b5132
RH
12323 else
12324 {
67c0d1eb 12325 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 12326 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
12327 used_at = 1;
12328 }
c0ebe874 12329 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 12330 break;
252b5132 12331
c0ebe874 12332 case M_SGE: /* X >= Y <==> not (X < Y) */
252b5132
RH
12333 s = "slt";
12334 goto sge;
12335 case M_SGEU:
12336 s = "sltu";
12337 sge:
c0ebe874
RS
12338 macro_build (NULL, s, "d,v,t", op[0], op[1], op[2]);
12339 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 12340 break;
252b5132 12341
c0ebe874 12342 case M_SGE_I: /* X >= I <==> not (X < I) */
252b5132 12343 case M_SGEU_I:
b0e6f033 12344 if (imm_expr.X_add_number >= -0x8000
252b5132 12345 && imm_expr.X_add_number < 0x8000)
c0ebe874
RS
12346 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
12347 op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
12348 else
12349 {
67c0d1eb
RS
12350 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
12351 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
c0ebe874 12352 op[0], op[1], AT);
252b5132
RH
12353 used_at = 1;
12354 }
c0ebe874 12355 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 12356 break;
252b5132 12357
c0ebe874 12358 case M_SGT: /* X > Y <==> Y < X */
252b5132
RH
12359 s = "slt";
12360 goto sgt;
12361 case M_SGTU:
12362 s = "sltu";
12363 sgt:
c0ebe874 12364 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
8fc2e39e 12365 break;
252b5132 12366
c0ebe874 12367 case M_SGT_I: /* X > I <==> I < X */
252b5132
RH
12368 s = "slt";
12369 goto sgti;
12370 case M_SGTU_I:
12371 s = "sltu";
12372 sgti:
8fc2e39e 12373 used_at = 1;
67c0d1eb 12374 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 12375 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
252b5132
RH
12376 break;
12377
c0ebe874 12378 case M_SLE: /* X <= Y <==> Y >= X <==> not (Y < X) */
252b5132
RH
12379 s = "slt";
12380 goto sle;
12381 case M_SLEU:
12382 s = "sltu";
12383 sle:
c0ebe874
RS
12384 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
12385 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 12386 break;
252b5132 12387
c0ebe874 12388 case M_SLE_I: /* X <= I <==> I >= X <==> not (I < X) */
252b5132
RH
12389 s = "slt";
12390 goto slei;
12391 case M_SLEU_I:
12392 s = "sltu";
12393 slei:
8fc2e39e 12394 used_at = 1;
67c0d1eb 12395 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874
RS
12396 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
12397 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
252b5132
RH
12398 break;
12399
12400 case M_SLT_I:
b0e6f033 12401 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
12402 && imm_expr.X_add_number < 0x8000)
12403 {
c0ebe874
RS
12404 macro_build (&imm_expr, "slti", "t,r,j", op[0], op[1],
12405 BFD_RELOC_LO16);
8fc2e39e 12406 break;
252b5132 12407 }
8fc2e39e 12408 used_at = 1;
67c0d1eb 12409 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 12410 macro_build (NULL, "slt", "d,v,t", op[0], op[1], AT);
252b5132
RH
12411 break;
12412
12413 case M_SLTU_I:
b0e6f033 12414 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
12415 && imm_expr.X_add_number < 0x8000)
12416 {
c0ebe874 12417 macro_build (&imm_expr, "sltiu", "t,r,j", op[0], op[1],
17a2f251 12418 BFD_RELOC_LO16);
8fc2e39e 12419 break;
252b5132 12420 }
8fc2e39e 12421 used_at = 1;
67c0d1eb 12422 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 12423 macro_build (NULL, "sltu", "d,v,t", op[0], op[1], AT);
252b5132
RH
12424 break;
12425
12426 case M_SNE:
c0ebe874
RS
12427 if (op[1] == 0)
12428 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[2]);
12429 else if (op[2] == 0)
12430 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
252b5132
RH
12431 else
12432 {
c0ebe874
RS
12433 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
12434 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
252b5132 12435 }
8fc2e39e 12436 break;
252b5132
RH
12437
12438 case M_SNE_I:
b0e6f033 12439 if (imm_expr.X_add_number == 0)
252b5132 12440 {
c0ebe874 12441 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
8fc2e39e 12442 break;
252b5132 12443 }
c0ebe874 12444 if (op[1] == 0)
252b5132 12445 {
1661c76c 12446 as_warn (_("instruction %s: result is always true"),
252b5132 12447 ip->insn_mo->name);
67c0d1eb 12448 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
c0ebe874 12449 op[0], 0, BFD_RELOC_LO16);
8fc2e39e 12450 break;
252b5132 12451 }
dd3cbb7e
NC
12452 if (CPU_HAS_SEQ (mips_opts.arch)
12453 && -512 <= imm_expr.X_add_number
12454 && imm_expr.X_add_number < 512)
12455 {
c0ebe874 12456 macro_build (NULL, "snei", "t,r,+Q", op[0], op[1],
750bdd57 12457 (int) imm_expr.X_add_number);
dd3cbb7e
NC
12458 break;
12459 }
b0e6f033 12460 if (imm_expr.X_add_number >= 0
252b5132
RH
12461 && imm_expr.X_add_number < 0x10000)
12462 {
c0ebe874
RS
12463 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1],
12464 BFD_RELOC_LO16);
252b5132 12465 }
b0e6f033 12466 else if (imm_expr.X_add_number > -0x8000
252b5132
RH
12467 && imm_expr.X_add_number < 0)
12468 {
12469 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 12470 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
c0ebe874 12471 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 12472 }
dd3cbb7e
NC
12473 else if (CPU_HAS_SEQ (mips_opts.arch))
12474 {
12475 used_at = 1;
12476 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 12477 macro_build (NULL, "sne", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
12478 break;
12479 }
252b5132
RH
12480 else
12481 {
67c0d1eb 12482 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 12483 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
12484 used_at = 1;
12485 }
c0ebe874 12486 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
8fc2e39e 12487 break;
252b5132 12488
df58fc94
RS
12489 case M_SUB_I:
12490 s = "addi";
12491 s2 = "sub";
12492 goto do_subi;
12493 case M_SUBU_I:
12494 s = "addiu";
12495 s2 = "subu";
12496 goto do_subi;
252b5132
RH
12497 case M_DSUB_I:
12498 dbl = 1;
df58fc94
RS
12499 s = "daddi";
12500 s2 = "dsub";
12501 if (!mips_opts.micromips)
12502 goto do_subi;
b0e6f033 12503 if (imm_expr.X_add_number > -0x200
df58fc94 12504 && imm_expr.X_add_number <= 0x200)
252b5132 12505 {
b0e6f033
RS
12506 macro_build (NULL, s, "t,r,.", op[0], op[1],
12507 (int) -imm_expr.X_add_number);
8fc2e39e 12508 break;
252b5132 12509 }
df58fc94 12510 goto do_subi_i;
252b5132
RH
12511 case M_DSUBU_I:
12512 dbl = 1;
df58fc94
RS
12513 s = "daddiu";
12514 s2 = "dsubu";
12515 do_subi:
b0e6f033 12516 if (imm_expr.X_add_number > -0x8000
252b5132
RH
12517 && imm_expr.X_add_number <= 0x8000)
12518 {
12519 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 12520 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 12521 break;
252b5132 12522 }
df58fc94 12523 do_subi_i:
8fc2e39e 12524 used_at = 1;
67c0d1eb 12525 load_register (AT, &imm_expr, dbl);
c0ebe874 12526 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
12527 break;
12528
12529 case M_TEQ_I:
12530 s = "teq";
12531 goto trap;
12532 case M_TGE_I:
12533 s = "tge";
12534 goto trap;
12535 case M_TGEU_I:
12536 s = "tgeu";
12537 goto trap;
12538 case M_TLT_I:
12539 s = "tlt";
12540 goto trap;
12541 case M_TLTU_I:
12542 s = "tltu";
12543 goto trap;
12544 case M_TNE_I:
12545 s = "tne";
12546 trap:
8fc2e39e 12547 used_at = 1;
67c0d1eb 12548 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 12549 macro_build (NULL, s, "s,t", op[0], AT);
252b5132
RH
12550 break;
12551
252b5132 12552 case M_TRUNCWS:
43841e91 12553 case M_TRUNCWD:
df58fc94 12554 gas_assert (!mips_opts.micromips);
0aa27725 12555 gas_assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 12556 used_at = 1;
252b5132
RH
12557
12558 /*
12559 * Is the double cfc1 instruction a bug in the mips assembler;
12560 * or is there a reason for it?
12561 */
7d10b47d 12562 start_noreorder ();
c0ebe874
RS
12563 macro_build (NULL, "cfc1", "t,G", op[2], RA);
12564 macro_build (NULL, "cfc1", "t,G", op[2], RA);
67c0d1eb 12565 macro_build (NULL, "nop", "");
252b5132 12566 expr1.X_add_number = 3;
c0ebe874 12567 macro_build (&expr1, "ori", "t,r,i", AT, op[2], BFD_RELOC_LO16);
252b5132 12568 expr1.X_add_number = 2;
67c0d1eb
RS
12569 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
12570 macro_build (NULL, "ctc1", "t,G", AT, RA);
12571 macro_build (NULL, "nop", "");
12572 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
c0ebe874
RS
12573 op[0], op[1]);
12574 macro_build (NULL, "ctc1", "t,G", op[2], RA);
67c0d1eb 12575 macro_build (NULL, "nop", "");
7d10b47d 12576 end_noreorder ();
252b5132
RH
12577 break;
12578
f2ae14a1 12579 case M_ULH_AB:
252b5132 12580 s = "lb";
df58fc94
RS
12581 s2 = "lbu";
12582 off = 1;
12583 goto uld_st;
f2ae14a1 12584 case M_ULHU_AB:
252b5132 12585 s = "lbu";
df58fc94
RS
12586 s2 = "lbu";
12587 off = 1;
12588 goto uld_st;
f2ae14a1 12589 case M_ULW_AB:
df58fc94
RS
12590 s = "lwl";
12591 s2 = "lwr";
7f3c4072 12592 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
12593 off = 3;
12594 goto uld_st;
f2ae14a1 12595 case M_ULD_AB:
252b5132
RH
12596 s = "ldl";
12597 s2 = "ldr";
7f3c4072 12598 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 12599 off = 7;
df58fc94 12600 goto uld_st;
f2ae14a1 12601 case M_USH_AB:
df58fc94
RS
12602 s = "sb";
12603 s2 = "sb";
12604 off = 1;
12605 ust = 1;
12606 goto uld_st;
f2ae14a1 12607 case M_USW_AB:
df58fc94
RS
12608 s = "swl";
12609 s2 = "swr";
7f3c4072 12610 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 12611 off = 3;
df58fc94
RS
12612 ust = 1;
12613 goto uld_st;
f2ae14a1 12614 case M_USD_AB:
df58fc94
RS
12615 s = "sdl";
12616 s2 = "sdr";
7f3c4072 12617 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
12618 off = 7;
12619 ust = 1;
12620
12621 uld_st:
c0ebe874 12622 breg = op[2];
f2ae14a1 12623 large_offset = !small_offset_p (off, align, offbits);
df58fc94
RS
12624 ep = &offset_expr;
12625 expr1.X_add_number = 0;
f2ae14a1 12626 if (large_offset)
df58fc94
RS
12627 {
12628 used_at = 1;
12629 tempreg = AT;
f2ae14a1
RS
12630 if (small_offset_p (0, align, 16))
12631 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg, -1,
12632 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
12633 else
12634 {
12635 load_address (tempreg, ep, &used_at);
12636 if (breg != 0)
12637 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12638 tempreg, tempreg, breg);
12639 }
12640 offset_reloc[0] = BFD_RELOC_LO16;
12641 offset_reloc[1] = BFD_RELOC_UNUSED;
12642 offset_reloc[2] = BFD_RELOC_UNUSED;
df58fc94 12643 breg = tempreg;
c0ebe874 12644 tempreg = op[0];
df58fc94
RS
12645 ep = &expr1;
12646 }
c0ebe874 12647 else if (!ust && op[0] == breg)
8fc2e39e
TS
12648 {
12649 used_at = 1;
12650 tempreg = AT;
12651 }
252b5132 12652 else
c0ebe874 12653 tempreg = op[0];
af22f5b2 12654
df58fc94
RS
12655 if (off == 1)
12656 goto ulh_sh;
252b5132 12657
90ecf173 12658 if (!target_big_endian)
df58fc94 12659 ep->X_add_number += off;
f2ae14a1 12660 if (offbits == 12)
c8276761 12661 macro_build (NULL, s, "t,~(b)", tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
12662 else
12663 macro_build (ep, s, "t,o(b)", tempreg, -1,
12664 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94 12665
90ecf173 12666 if (!target_big_endian)
df58fc94 12667 ep->X_add_number -= off;
252b5132 12668 else
df58fc94 12669 ep->X_add_number += off;
f2ae14a1 12670 if (offbits == 12)
df58fc94 12671 macro_build (NULL, s2, "t,~(b)",
c8276761 12672 tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
12673 else
12674 macro_build (ep, s2, "t,o(b)", tempreg, -1,
12675 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 12676
df58fc94 12677 /* If necessary, move the result in tempreg to the final destination. */
c0ebe874 12678 if (!ust && op[0] != tempreg)
df58fc94
RS
12679 {
12680 /* Protect second load's delay slot. */
12681 load_delay_nop ();
c0ebe874 12682 move_register (op[0], tempreg);
df58fc94 12683 }
8fc2e39e 12684 break;
252b5132 12685
df58fc94 12686 ulh_sh:
d6bc6245 12687 used_at = 1;
df58fc94
RS
12688 if (target_big_endian == ust)
12689 ep->X_add_number += off;
c0ebe874 12690 tempreg = ust || large_offset ? op[0] : AT;
f2ae14a1
RS
12691 macro_build (ep, s, "t,o(b)", tempreg, -1,
12692 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94
RS
12693
12694 /* For halfword transfers we need a temporary register to shuffle
12695 bytes. Unfortunately for M_USH_A we have none available before
12696 the next store as AT holds the base address. We deal with this
12697 case by clobbering TREG and then restoring it as with ULH. */
c0ebe874 12698 tempreg = ust == large_offset ? op[0] : AT;
df58fc94 12699 if (ust)
c0ebe874 12700 macro_build (NULL, "srl", SHFT_FMT, tempreg, op[0], 8);
df58fc94
RS
12701
12702 if (target_big_endian == ust)
12703 ep->X_add_number -= off;
252b5132 12704 else
df58fc94 12705 ep->X_add_number += off;
f2ae14a1
RS
12706 macro_build (ep, s2, "t,o(b)", tempreg, -1,
12707 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 12708
df58fc94 12709 /* For M_USH_A re-retrieve the LSB. */
f2ae14a1 12710 if (ust && large_offset)
df58fc94
RS
12711 {
12712 if (target_big_endian)
12713 ep->X_add_number += off;
12714 else
12715 ep->X_add_number -= off;
f2ae14a1
RS
12716 macro_build (&expr1, "lbu", "t,o(b)", AT, -1,
12717 offset_reloc[0], offset_reloc[1], offset_reloc[2], AT);
df58fc94
RS
12718 }
12719 /* For ULH and M_USH_A OR the LSB in. */
f2ae14a1 12720 if (!ust || large_offset)
df58fc94 12721 {
c0ebe874 12722 tempreg = !large_offset ? AT : op[0];
df58fc94 12723 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
c0ebe874 12724 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
df58fc94 12725 }
252b5132
RH
12726 break;
12727
12728 default:
12729 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 12730 are added dynamically. */
1661c76c 12731 as_bad (_("macro %s not implemented yet"), ip->insn_mo->name);
252b5132
RH
12732 break;
12733 }
741fe287 12734 if (!mips_opts.at && used_at)
1661c76c 12735 as_bad (_("macro used $at after \".set noat\""));
252b5132
RH
12736}
12737
12738/* Implement macros in mips16 mode. */
12739
12740static void
17a2f251 12741mips16_macro (struct mips_cl_insn *ip)
252b5132 12742{
c0ebe874 12743 const struct mips_operand_array *operands;
252b5132 12744 int mask;
c0ebe874 12745 int tmp;
252b5132
RH
12746 expressionS expr1;
12747 int dbl;
12748 const char *s, *s2, *s3;
c0ebe874
RS
12749 unsigned int op[MAX_OPERANDS];
12750 unsigned int i;
252b5132
RH
12751
12752 mask = ip->insn_mo->mask;
12753
c0ebe874
RS
12754 operands = insn_operands (ip);
12755 for (i = 0; i < MAX_OPERANDS; i++)
12756 if (operands->operand[i])
12757 op[i] = insn_extract_operand (ip, operands->operand[i]);
12758 else
12759 op[i] = -1;
252b5132 12760
252b5132
RH
12761 expr1.X_op = O_constant;
12762 expr1.X_op_symbol = NULL;
12763 expr1.X_add_symbol = NULL;
12764 expr1.X_add_number = 1;
12765
12766 dbl = 0;
12767
12768 switch (mask)
12769 {
12770 default:
b37df7c4 12771 abort ();
252b5132
RH
12772
12773 case M_DDIV_3:
12774 dbl = 1;
12775 case M_DIV_3:
12776 s = "mflo";
12777 goto do_div3;
12778 case M_DREM_3:
12779 dbl = 1;
12780 case M_REM_3:
12781 s = "mfhi";
12782 do_div3:
7d10b47d 12783 start_noreorder ();
c0ebe874 12784 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", op[1], op[2]);
252b5132 12785 expr1.X_add_number = 2;
c0ebe874 12786 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 12787 macro_build (NULL, "break", "6", 7);
bdaaa2e1 12788
252b5132
RH
12789 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
12790 since that causes an overflow. We should do that as well,
12791 but I don't see how to do the comparisons without a temporary
12792 register. */
7d10b47d 12793 end_noreorder ();
c0ebe874 12794 macro_build (NULL, s, "x", op[0]);
252b5132
RH
12795 break;
12796
12797 case M_DIVU_3:
12798 s = "divu";
12799 s2 = "mflo";
12800 goto do_divu3;
12801 case M_REMU_3:
12802 s = "divu";
12803 s2 = "mfhi";
12804 goto do_divu3;
12805 case M_DDIVU_3:
12806 s = "ddivu";
12807 s2 = "mflo";
12808 goto do_divu3;
12809 case M_DREMU_3:
12810 s = "ddivu";
12811 s2 = "mfhi";
12812 do_divu3:
7d10b47d 12813 start_noreorder ();
c0ebe874 12814 macro_build (NULL, s, "0,x,y", op[1], op[2]);
252b5132 12815 expr1.X_add_number = 2;
c0ebe874 12816 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 12817 macro_build (NULL, "break", "6", 7);
7d10b47d 12818 end_noreorder ();
c0ebe874 12819 macro_build (NULL, s2, "x", op[0]);
252b5132
RH
12820 break;
12821
12822 case M_DMUL:
12823 dbl = 1;
12824 case M_MUL:
c0ebe874
RS
12825 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", op[1], op[2]);
12826 macro_build (NULL, "mflo", "x", op[0]);
8fc2e39e 12827 break;
252b5132
RH
12828
12829 case M_DSUBU_I:
12830 dbl = 1;
12831 goto do_subu;
12832 case M_SUBU_I:
12833 do_subu:
252b5132 12834 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 12835 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", op[0], op[1]);
252b5132
RH
12836 break;
12837
12838 case M_SUBU_I_2:
252b5132 12839 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 12840 macro_build (&imm_expr, "addiu", "x,k", op[0]);
252b5132
RH
12841 break;
12842
12843 case M_DSUBU_I_2:
252b5132 12844 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 12845 macro_build (&imm_expr, "daddiu", "y,j", op[0]);
252b5132
RH
12846 break;
12847
12848 case M_BEQ:
12849 s = "cmp";
12850 s2 = "bteqz";
12851 goto do_branch;
12852 case M_BNE:
12853 s = "cmp";
12854 s2 = "btnez";
12855 goto do_branch;
12856 case M_BLT:
12857 s = "slt";
12858 s2 = "btnez";
12859 goto do_branch;
12860 case M_BLTU:
12861 s = "sltu";
12862 s2 = "btnez";
12863 goto do_branch;
12864 case M_BLE:
12865 s = "slt";
12866 s2 = "bteqz";
12867 goto do_reverse_branch;
12868 case M_BLEU:
12869 s = "sltu";
12870 s2 = "bteqz";
12871 goto do_reverse_branch;
12872 case M_BGE:
12873 s = "slt";
12874 s2 = "bteqz";
12875 goto do_branch;
12876 case M_BGEU:
12877 s = "sltu";
12878 s2 = "bteqz";
12879 goto do_branch;
12880 case M_BGT:
12881 s = "slt";
12882 s2 = "btnez";
12883 goto do_reverse_branch;
12884 case M_BGTU:
12885 s = "sltu";
12886 s2 = "btnez";
12887
12888 do_reverse_branch:
c0ebe874
RS
12889 tmp = op[1];
12890 op[1] = op[0];
12891 op[0] = tmp;
252b5132
RH
12892
12893 do_branch:
c0ebe874 12894 macro_build (NULL, s, "x,y", op[0], op[1]);
67c0d1eb 12895 macro_build (&offset_expr, s2, "p");
252b5132
RH
12896 break;
12897
12898 case M_BEQ_I:
12899 s = "cmpi";
12900 s2 = "bteqz";
12901 s3 = "x,U";
12902 goto do_branch_i;
12903 case M_BNE_I:
12904 s = "cmpi";
12905 s2 = "btnez";
12906 s3 = "x,U";
12907 goto do_branch_i;
12908 case M_BLT_I:
12909 s = "slti";
12910 s2 = "btnez";
12911 s3 = "x,8";
12912 goto do_branch_i;
12913 case M_BLTU_I:
12914 s = "sltiu";
12915 s2 = "btnez";
12916 s3 = "x,8";
12917 goto do_branch_i;
12918 case M_BLE_I:
12919 s = "slti";
12920 s2 = "btnez";
12921 s3 = "x,8";
12922 goto do_addone_branch_i;
12923 case M_BLEU_I:
12924 s = "sltiu";
12925 s2 = "btnez";
12926 s3 = "x,8";
12927 goto do_addone_branch_i;
12928 case M_BGE_I:
12929 s = "slti";
12930 s2 = "bteqz";
12931 s3 = "x,8";
12932 goto do_branch_i;
12933 case M_BGEU_I:
12934 s = "sltiu";
12935 s2 = "bteqz";
12936 s3 = "x,8";
12937 goto do_branch_i;
12938 case M_BGT_I:
12939 s = "slti";
12940 s2 = "bteqz";
12941 s3 = "x,8";
12942 goto do_addone_branch_i;
12943 case M_BGTU_I:
12944 s = "sltiu";
12945 s2 = "bteqz";
12946 s3 = "x,8";
12947
12948 do_addone_branch_i:
252b5132
RH
12949 ++imm_expr.X_add_number;
12950
12951 do_branch_i:
c0ebe874 12952 macro_build (&imm_expr, s, s3, op[0]);
67c0d1eb 12953 macro_build (&offset_expr, s2, "p");
252b5132
RH
12954 break;
12955
12956 case M_ABS:
12957 expr1.X_add_number = 0;
c0ebe874
RS
12958 macro_build (&expr1, "slti", "x,8", op[1]);
12959 if (op[0] != op[1])
12960 macro_build (NULL, "move", "y,X", op[0], mips16_to_32_reg_map[op[1]]);
252b5132 12961 expr1.X_add_number = 2;
67c0d1eb 12962 macro_build (&expr1, "bteqz", "p");
c0ebe874 12963 macro_build (NULL, "neg", "x,w", op[0], op[0]);
0acfaea6 12964 break;
252b5132
RH
12965 }
12966}
12967
14daeee3
RS
12968/* Look up instruction [START, START + LENGTH) in HASH. Record any extra
12969 opcode bits in *OPCODE_EXTRA. */
12970
12971static struct mips_opcode *
12972mips_lookup_insn (struct hash_control *hash, const char *start,
da8bca91 12973 ssize_t length, unsigned int *opcode_extra)
14daeee3
RS
12974{
12975 char *name, *dot, *p;
12976 unsigned int mask, suffix;
da8bca91 12977 ssize_t opend;
14daeee3
RS
12978 struct mips_opcode *insn;
12979
12980 /* Make a copy of the instruction so that we can fiddle with it. */
12981 name = alloca (length + 1);
12982 memcpy (name, start, length);
12983 name[length] = '\0';
12984
12985 /* Look up the instruction as-is. */
12986 insn = (struct mips_opcode *) hash_find (hash, name);
ee5734f0 12987 if (insn)
14daeee3
RS
12988 return insn;
12989
12990 dot = strchr (name, '.');
12991 if (dot && dot[1])
12992 {
12993 /* Try to interpret the text after the dot as a VU0 channel suffix. */
12994 p = mips_parse_vu0_channels (dot + 1, &mask);
12995 if (*p == 0 && mask != 0)
12996 {
12997 *dot = 0;
12998 insn = (struct mips_opcode *) hash_find (hash, name);
12999 *dot = '.';
13000 if (insn && (insn->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0)
13001 {
13002 *opcode_extra |= mask << mips_vu0_channel_mask.lsb;
13003 return insn;
13004 }
13005 }
13006 }
13007
13008 if (mips_opts.micromips)
13009 {
13010 /* See if there's an instruction size override suffix,
13011 either `16' or `32', at the end of the mnemonic proper,
13012 that defines the operation, i.e. before the first `.'
13013 character if any. Strip it and retry. */
13014 opend = dot != NULL ? dot - name : length;
13015 if (opend >= 3 && name[opend - 2] == '1' && name[opend - 1] == '6')
13016 suffix = 2;
13017 else if (name[opend - 2] == '3' && name[opend - 1] == '2')
13018 suffix = 4;
13019 else
13020 suffix = 0;
13021 if (suffix)
13022 {
13023 memcpy (name + opend - 2, name + opend, length - opend + 1);
13024 insn = (struct mips_opcode *) hash_find (hash, name);
ee5734f0 13025 if (insn)
14daeee3
RS
13026 {
13027 forced_insn_length = suffix;
13028 return insn;
13029 }
13030 }
13031 }
13032
13033 return NULL;
13034}
13035
77bd4346 13036/* Assemble an instruction into its binary format. If the instruction
e423441d
RS
13037 is a macro, set imm_expr and offset_expr to the values associated
13038 with "I" and "A" operands respectively. Otherwise store the value
13039 of the relocatable field (if any) in offset_expr. In both cases
13040 set offset_reloc to the relocation operators applied to offset_expr. */
252b5132
RH
13041
13042static void
60f20e8b 13043mips_ip (char *str, struct mips_cl_insn *insn)
252b5132 13044{
60f20e8b 13045 const struct mips_opcode *first, *past;
df58fc94 13046 struct hash_control *hash;
a92713e6 13047 char format;
14daeee3 13048 size_t end;
a92713e6 13049 struct mips_operand_token *tokens;
14daeee3 13050 unsigned int opcode_extra;
252b5132 13051
df58fc94
RS
13052 if (mips_opts.micromips)
13053 {
13054 hash = micromips_op_hash;
13055 past = &micromips_opcodes[bfd_micromips_num_opcodes];
13056 }
13057 else
13058 {
13059 hash = op_hash;
13060 past = &mips_opcodes[NUMOPCODES];
13061 }
13062 forced_insn_length = 0;
14daeee3 13063 opcode_extra = 0;
252b5132 13064
df58fc94 13065 /* We first try to match an instruction up to a space or to the end. */
a40bc9dd
RS
13066 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
13067 continue;
bdaaa2e1 13068
60f20e8b
RS
13069 first = mips_lookup_insn (hash, str, end, &opcode_extra);
13070 if (first == NULL)
252b5132 13071 {
1661c76c 13072 set_insn_error (0, _("unrecognized opcode"));
a40bc9dd 13073 return;
252b5132
RH
13074 }
13075
60f20e8b 13076 if (strcmp (first->name, "li.s") == 0)
a92713e6 13077 format = 'f';
60f20e8b 13078 else if (strcmp (first->name, "li.d") == 0)
a92713e6
RS
13079 format = 'd';
13080 else
13081 format = 0;
13082 tokens = mips_parse_arguments (str + end, format);
13083 if (!tokens)
13084 return;
13085
60f20e8b
RS
13086 if (!match_insns (insn, first, past, tokens, opcode_extra, FALSE)
13087 && !match_insns (insn, first, past, tokens, opcode_extra, TRUE))
1661c76c 13088 set_insn_error (0, _("invalid operands"));
df58fc94 13089
e3de51ce 13090 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
13091}
13092
77bd4346
RS
13093/* As for mips_ip, but used when assembling MIPS16 code.
13094 Also set forced_insn_length to the resulting instruction size in
13095 bytes if the user explicitly requested a small or extended instruction. */
252b5132
RH
13096
13097static void
60f20e8b 13098mips16_ip (char *str, struct mips_cl_insn *insn)
252b5132 13099{
1a00e612 13100 char *end, *s, c;
60f20e8b 13101 struct mips_opcode *first;
a92713e6 13102 struct mips_operand_token *tokens;
252b5132 13103
df58fc94 13104 forced_insn_length = 0;
252b5132 13105
3882b010 13106 for (s = str; ISLOWER (*s); ++s)
252b5132 13107 ;
1a00e612
RS
13108 end = s;
13109 c = *end;
13110 switch (c)
252b5132
RH
13111 {
13112 case '\0':
13113 break;
13114
13115 case ' ':
1a00e612 13116 s++;
252b5132
RH
13117 break;
13118
13119 case '.':
13120 if (s[1] == 't' && s[2] == ' ')
13121 {
df58fc94 13122 forced_insn_length = 2;
252b5132
RH
13123 s += 3;
13124 break;
13125 }
13126 else if (s[1] == 'e' && s[2] == ' ')
13127 {
df58fc94 13128 forced_insn_length = 4;
252b5132
RH
13129 s += 3;
13130 break;
13131 }
13132 /* Fall through. */
13133 default:
1661c76c 13134 set_insn_error (0, _("unrecognized opcode"));
252b5132
RH
13135 return;
13136 }
13137
df58fc94
RS
13138 if (mips_opts.noautoextend && !forced_insn_length)
13139 forced_insn_length = 2;
252b5132 13140
1a00e612 13141 *end = 0;
60f20e8b 13142 first = (struct mips_opcode *) hash_find (mips16_op_hash, str);
1a00e612
RS
13143 *end = c;
13144
60f20e8b 13145 if (!first)
252b5132 13146 {
1661c76c 13147 set_insn_error (0, _("unrecognized opcode"));
252b5132
RH
13148 return;
13149 }
13150
a92713e6
RS
13151 tokens = mips_parse_arguments (s, 0);
13152 if (!tokens)
13153 return;
13154
60f20e8b 13155 if (!match_mips16_insns (insn, first, tokens))
1661c76c 13156 set_insn_error (0, _("invalid operands"));
252b5132 13157
e3de51ce 13158 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
13159}
13160
b886a2ab
RS
13161/* Marshal immediate value VAL for an extended MIPS16 instruction.
13162 NBITS is the number of significant bits in VAL. */
13163
13164static unsigned long
13165mips16_immed_extend (offsetT val, unsigned int nbits)
13166{
13167 int extval;
13168 if (nbits == 16)
13169 {
13170 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
13171 val &= 0x1f;
13172 }
13173 else if (nbits == 15)
13174 {
13175 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
13176 val &= 0xf;
13177 }
13178 else
13179 {
13180 extval = ((val & 0x1f) << 6) | (val & 0x20);
13181 val = 0;
13182 }
13183 return (extval << 16) | val;
13184}
13185
3ccad066
RS
13186/* Like decode_mips16_operand, but require the operand to be defined and
13187 require it to be an integer. */
13188
13189static const struct mips_int_operand *
13190mips16_immed_operand (int type, bfd_boolean extended_p)
13191{
13192 const struct mips_operand *operand;
13193
13194 operand = decode_mips16_operand (type, extended_p);
13195 if (!operand || (operand->type != OP_INT && operand->type != OP_PCREL))
13196 abort ();
13197 return (const struct mips_int_operand *) operand;
13198}
13199
13200/* Return true if SVAL fits OPERAND. RELOC is as for mips16_immed. */
13201
13202static bfd_boolean
13203mips16_immed_in_range_p (const struct mips_int_operand *operand,
13204 bfd_reloc_code_real_type reloc, offsetT sval)
13205{
13206 int min_val, max_val;
13207
13208 min_val = mips_int_operand_min (operand);
13209 max_val = mips_int_operand_max (operand);
13210 if (reloc != BFD_RELOC_UNUSED)
13211 {
13212 if (min_val < 0)
13213 sval = SEXT_16BIT (sval);
13214 else
13215 sval &= 0xffff;
13216 }
13217
13218 return (sval >= min_val
13219 && sval <= max_val
13220 && (sval & ((1 << operand->shift) - 1)) == 0);
13221}
13222
5c04167a
RS
13223/* Install immediate value VAL into MIPS16 instruction *INSN,
13224 extending it if necessary. The instruction in *INSN may
13225 already be extended.
13226
43c0598f
RS
13227 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
13228 if none. In the former case, VAL is a 16-bit number with no
13229 defined signedness.
13230
13231 TYPE is the type of the immediate field. USER_INSN_LENGTH
13232 is the length that the user requested, or 0 if none. */
252b5132
RH
13233
13234static void
43c0598f
RS
13235mips16_immed (char *file, unsigned int line, int type,
13236 bfd_reloc_code_real_type reloc, offsetT val,
5c04167a 13237 unsigned int user_insn_length, unsigned long *insn)
252b5132 13238{
3ccad066
RS
13239 const struct mips_int_operand *operand;
13240 unsigned int uval, length;
252b5132 13241
3ccad066
RS
13242 operand = mips16_immed_operand (type, FALSE);
13243 if (!mips16_immed_in_range_p (operand, reloc, val))
5c04167a
RS
13244 {
13245 /* We need an extended instruction. */
13246 if (user_insn_length == 2)
13247 as_bad_where (file, line, _("invalid unextended operand value"));
13248 else
13249 *insn |= MIPS16_EXTEND;
13250 }
13251 else if (user_insn_length == 4)
13252 {
13253 /* The operand doesn't force an unextended instruction to be extended.
13254 Warn if the user wanted an extended instruction anyway. */
13255 *insn |= MIPS16_EXTEND;
13256 as_warn_where (file, line,
13257 _("extended operand requested but not required"));
13258 }
252b5132 13259
3ccad066
RS
13260 length = mips16_opcode_length (*insn);
13261 if (length == 4)
252b5132 13262 {
3ccad066
RS
13263 operand = mips16_immed_operand (type, TRUE);
13264 if (!mips16_immed_in_range_p (operand, reloc, val))
13265 as_bad_where (file, line,
13266 _("operand value out of range for instruction"));
252b5132 13267 }
3ccad066
RS
13268 uval = ((unsigned int) val >> operand->shift) - operand->bias;
13269 if (length == 2)
13270 *insn = mips_insert_operand (&operand->root, *insn, uval);
252b5132 13271 else
3ccad066 13272 *insn |= mips16_immed_extend (uval, operand->root.size);
252b5132
RH
13273}
13274\f
d6f16593 13275struct percent_op_match
ad8d3bb3 13276{
5e0116d5
RS
13277 const char *str;
13278 bfd_reloc_code_real_type reloc;
d6f16593
MR
13279};
13280
13281static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 13282{
5e0116d5 13283 {"%lo", BFD_RELOC_LO16},
5e0116d5
RS
13284 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
13285 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
13286 {"%call16", BFD_RELOC_MIPS_CALL16},
13287 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
13288 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
13289 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
13290 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
13291 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
13292 {"%got", BFD_RELOC_MIPS_GOT16},
13293 {"%gp_rel", BFD_RELOC_GPREL16},
13294 {"%half", BFD_RELOC_16},
13295 {"%highest", BFD_RELOC_MIPS_HIGHEST},
13296 {"%higher", BFD_RELOC_MIPS_HIGHER},
13297 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
13298 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
13299 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
13300 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
13301 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
13302 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
13303 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
13304 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
5e0116d5 13305 {"%hi", BFD_RELOC_HI16_S}
ad8d3bb3
TS
13306};
13307
d6f16593
MR
13308static const struct percent_op_match mips16_percent_op[] =
13309{
13310 {"%lo", BFD_RELOC_MIPS16_LO16},
13311 {"%gprel", BFD_RELOC_MIPS16_GPREL},
738e5348
RS
13312 {"%got", BFD_RELOC_MIPS16_GOT16},
13313 {"%call16", BFD_RELOC_MIPS16_CALL16},
d0f13682
CLT
13314 {"%hi", BFD_RELOC_MIPS16_HI16_S},
13315 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
13316 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
13317 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
13318 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
13319 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
13320 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
13321 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
d6f16593
MR
13322};
13323
252b5132 13324
5e0116d5
RS
13325/* Return true if *STR points to a relocation operator. When returning true,
13326 move *STR over the operator and store its relocation code in *RELOC.
13327 Leave both *STR and *RELOC alone when returning false. */
13328
13329static bfd_boolean
17a2f251 13330parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 13331{
d6f16593
MR
13332 const struct percent_op_match *percent_op;
13333 size_t limit, i;
13334
13335 if (mips_opts.mips16)
13336 {
13337 percent_op = mips16_percent_op;
13338 limit = ARRAY_SIZE (mips16_percent_op);
13339 }
13340 else
13341 {
13342 percent_op = mips_percent_op;
13343 limit = ARRAY_SIZE (mips_percent_op);
13344 }
76b3015f 13345
d6f16593 13346 for (i = 0; i < limit; i++)
5e0116d5 13347 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 13348 {
3f98094e
DJ
13349 int len = strlen (percent_op[i].str);
13350
13351 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
13352 continue;
13353
5e0116d5
RS
13354 *str += strlen (percent_op[i].str);
13355 *reloc = percent_op[i].reloc;
394f9b3a 13356
5e0116d5
RS
13357 /* Check whether the output BFD supports this relocation.
13358 If not, issue an error and fall back on something safe. */
13359 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 13360 {
20203fb9 13361 as_bad (_("relocation %s isn't supported by the current ABI"),
5e0116d5 13362 percent_op[i].str);
01a3f561 13363 *reloc = BFD_RELOC_UNUSED;
394f9b3a 13364 }
5e0116d5 13365 return TRUE;
394f9b3a 13366 }
5e0116d5 13367 return FALSE;
394f9b3a 13368}
ad8d3bb3 13369
ad8d3bb3 13370
5e0116d5
RS
13371/* Parse string STR as a 16-bit relocatable operand. Store the
13372 expression in *EP and the relocations in the array starting
13373 at RELOC. Return the number of relocation operators used.
ad8d3bb3 13374
01a3f561 13375 On exit, EXPR_END points to the first character after the expression. */
ad8d3bb3 13376
5e0116d5 13377static size_t
17a2f251
TS
13378my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
13379 char *str)
ad8d3bb3 13380{
5e0116d5
RS
13381 bfd_reloc_code_real_type reversed_reloc[3];
13382 size_t reloc_index, i;
09b8f35a
RS
13383 int crux_depth, str_depth;
13384 char *crux;
5e0116d5
RS
13385
13386 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
13387 in REVERSED_RELOC. End the loop with CRUX pointing to the start
13388 of the main expression and with CRUX_DEPTH containing the number
13389 of open brackets at that point. */
13390 reloc_index = -1;
13391 str_depth = 0;
13392 do
fb1b3232 13393 {
09b8f35a
RS
13394 reloc_index++;
13395 crux = str;
13396 crux_depth = str_depth;
13397
13398 /* Skip over whitespace and brackets, keeping count of the number
13399 of brackets. */
13400 while (*str == ' ' || *str == '\t' || *str == '(')
13401 if (*str++ == '(')
13402 str_depth++;
5e0116d5 13403 }
09b8f35a
RS
13404 while (*str == '%'
13405 && reloc_index < (HAVE_NEWABI ? 3 : 1)
13406 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 13407
09b8f35a 13408 my_getExpression (ep, crux);
5e0116d5 13409 str = expr_end;
394f9b3a 13410
5e0116d5 13411 /* Match every open bracket. */
09b8f35a 13412 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 13413 if (*str++ == ')')
09b8f35a 13414 crux_depth--;
394f9b3a 13415
09b8f35a 13416 if (crux_depth > 0)
20203fb9 13417 as_bad (_("unclosed '('"));
394f9b3a 13418
5e0116d5 13419 expr_end = str;
252b5132 13420
01a3f561 13421 if (reloc_index != 0)
64bdfcaf
RS
13422 {
13423 prev_reloc_op_frag = frag_now;
13424 for (i = 0; i < reloc_index; i++)
13425 reloc[i] = reversed_reloc[reloc_index - 1 - i];
13426 }
fb1b3232 13427
5e0116d5 13428 return reloc_index;
252b5132
RH
13429}
13430
13431static void
17a2f251 13432my_getExpression (expressionS *ep, char *str)
252b5132
RH
13433{
13434 char *save_in;
13435
13436 save_in = input_line_pointer;
13437 input_line_pointer = str;
13438 expression (ep);
13439 expr_end = input_line_pointer;
13440 input_line_pointer = save_in;
252b5132
RH
13441}
13442
252b5132 13443char *
17a2f251 13444md_atof (int type, char *litP, int *sizeP)
252b5132 13445{
499ac353 13446 return ieee_md_atof (type, litP, sizeP, target_big_endian);
252b5132
RH
13447}
13448
13449void
17a2f251 13450md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
13451{
13452 if (target_big_endian)
13453 number_to_chars_bigendian (buf, val, n);
13454 else
13455 number_to_chars_littleendian (buf, val, n);
13456}
13457\f
e013f690
TS
13458static int support_64bit_objects(void)
13459{
13460 const char **list, **l;
aa3d8fdf 13461 int yes;
e013f690
TS
13462
13463 list = bfd_target_list ();
13464 for (l = list; *l != NULL; l++)
aeffff67
RS
13465 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
13466 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
e013f690 13467 break;
aa3d8fdf 13468 yes = (*l != NULL);
e013f690 13469 free (list);
aa3d8fdf 13470 return yes;
e013f690
TS
13471}
13472
316f5878
RS
13473/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
13474 NEW_VALUE. Warn if another value was already specified. Note:
13475 we have to defer parsing the -march and -mtune arguments in order
13476 to handle 'from-abi' correctly, since the ABI might be specified
13477 in a later argument. */
13478
13479static void
17a2f251 13480mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
13481{
13482 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
1661c76c 13483 as_warn (_("a different %s was already specified, is now %s"),
316f5878
RS
13484 string_ptr == &mips_arch_string ? "-march" : "-mtune",
13485 new_value);
13486
13487 *string_ptr = new_value;
13488}
13489
252b5132 13490int
17a2f251 13491md_parse_option (int c, char *arg)
252b5132 13492{
c6278170
RS
13493 unsigned int i;
13494
13495 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
13496 if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
13497 {
13498 file_ase_explicit |= mips_set_ase (&mips_ases[i],
13499 c == mips_ases[i].option_on);
13500 return 1;
13501 }
13502
252b5132
RH
13503 switch (c)
13504 {
119d663a
NC
13505 case OPTION_CONSTRUCT_FLOATS:
13506 mips_disable_float_construction = 0;
13507 break;
bdaaa2e1 13508
119d663a
NC
13509 case OPTION_NO_CONSTRUCT_FLOATS:
13510 mips_disable_float_construction = 1;
13511 break;
bdaaa2e1 13512
252b5132
RH
13513 case OPTION_TRAP:
13514 mips_trap = 1;
13515 break;
13516
13517 case OPTION_BREAK:
13518 mips_trap = 0;
13519 break;
13520
13521 case OPTION_EB:
13522 target_big_endian = 1;
13523 break;
13524
13525 case OPTION_EL:
13526 target_big_endian = 0;
13527 break;
13528
13529 case 'O':
4ffff32f
TS
13530 if (arg == NULL)
13531 mips_optimize = 1;
13532 else if (arg[0] == '0')
13533 mips_optimize = 0;
13534 else if (arg[0] == '1')
252b5132
RH
13535 mips_optimize = 1;
13536 else
13537 mips_optimize = 2;
13538 break;
13539
13540 case 'g':
13541 if (arg == NULL)
13542 mips_debug = 2;
13543 else
13544 mips_debug = atoi (arg);
252b5132
RH
13545 break;
13546
13547 case OPTION_MIPS1:
0b35dfee 13548 file_mips_opts.isa = ISA_MIPS1;
252b5132
RH
13549 break;
13550
13551 case OPTION_MIPS2:
0b35dfee 13552 file_mips_opts.isa = ISA_MIPS2;
252b5132
RH
13553 break;
13554
13555 case OPTION_MIPS3:
0b35dfee 13556 file_mips_opts.isa = ISA_MIPS3;
252b5132
RH
13557 break;
13558
13559 case OPTION_MIPS4:
0b35dfee 13560 file_mips_opts.isa = ISA_MIPS4;
e7af610e
NC
13561 break;
13562
84ea6cf2 13563 case OPTION_MIPS5:
0b35dfee 13564 file_mips_opts.isa = ISA_MIPS5;
84ea6cf2
NC
13565 break;
13566
e7af610e 13567 case OPTION_MIPS32:
0b35dfee 13568 file_mips_opts.isa = ISA_MIPS32;
252b5132
RH
13569 break;
13570
af7ee8bf 13571 case OPTION_MIPS32R2:
0b35dfee 13572 file_mips_opts.isa = ISA_MIPS32R2;
af7ee8bf
CD
13573 break;
13574
ae52f483
AB
13575 case OPTION_MIPS32R3:
13576 file_mips_isa = ISA_MIPS32R3;
13577 break;
13578
13579 case OPTION_MIPS32R5:
13580 file_mips_isa = ISA_MIPS32R5;
13581 break;
13582
5f74bc13 13583 case OPTION_MIPS64R2:
0b35dfee 13584 file_mips_opts.isa = ISA_MIPS64R2;
5f74bc13
CD
13585 break;
13586
ae52f483
AB
13587 case OPTION_MIPS64R3:
13588 file_mips_isa = ISA_MIPS64R3;
13589 break;
13590
13591 case OPTION_MIPS64R5:
13592 file_mips_isa = ISA_MIPS64R5;
13593 break;
13594
84ea6cf2 13595 case OPTION_MIPS64:
0b35dfee 13596 file_mips_opts.isa = ISA_MIPS64;
84ea6cf2
NC
13597 break;
13598
ec68c924 13599 case OPTION_MTUNE:
316f5878
RS
13600 mips_set_option_string (&mips_tune_string, arg);
13601 break;
ec68c924 13602
316f5878
RS
13603 case OPTION_MARCH:
13604 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
13605 break;
13606
13607 case OPTION_M4650:
316f5878
RS
13608 mips_set_option_string (&mips_arch_string, "4650");
13609 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
13610 break;
13611
13612 case OPTION_NO_M4650:
13613 break;
13614
13615 case OPTION_M4010:
316f5878
RS
13616 mips_set_option_string (&mips_arch_string, "4010");
13617 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
13618 break;
13619
13620 case OPTION_NO_M4010:
13621 break;
13622
13623 case OPTION_M4100:
316f5878
RS
13624 mips_set_option_string (&mips_arch_string, "4100");
13625 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
13626 break;
13627
13628 case OPTION_NO_M4100:
13629 break;
13630
252b5132 13631 case OPTION_M3900:
316f5878
RS
13632 mips_set_option_string (&mips_arch_string, "3900");
13633 mips_set_option_string (&mips_tune_string, "3900");
252b5132 13634 break;
bdaaa2e1 13635
252b5132
RH
13636 case OPTION_NO_M3900:
13637 break;
13638
df58fc94
RS
13639 case OPTION_MICROMIPS:
13640 if (mips_opts.mips16 == 1)
13641 {
13642 as_bad (_("-mmicromips cannot be used with -mips16"));
13643 return 0;
13644 }
13645 mips_opts.micromips = 1;
13646 mips_no_prev_insn ();
13647 break;
13648
13649 case OPTION_NO_MICROMIPS:
13650 mips_opts.micromips = 0;
13651 mips_no_prev_insn ();
13652 break;
13653
252b5132 13654 case OPTION_MIPS16:
df58fc94
RS
13655 if (mips_opts.micromips == 1)
13656 {
13657 as_bad (_("-mips16 cannot be used with -micromips"));
13658 return 0;
13659 }
252b5132 13660 mips_opts.mips16 = 1;
7d10b47d 13661 mips_no_prev_insn ();
252b5132
RH
13662 break;
13663
13664 case OPTION_NO_MIPS16:
13665 mips_opts.mips16 = 0;
7d10b47d 13666 mips_no_prev_insn ();
252b5132
RH
13667 break;
13668
6a32d874
CM
13669 case OPTION_FIX_24K:
13670 mips_fix_24k = 1;
13671 break;
13672
13673 case OPTION_NO_FIX_24K:
13674 mips_fix_24k = 0;
13675 break;
13676
a8d14a88
CM
13677 case OPTION_FIX_RM7000:
13678 mips_fix_rm7000 = 1;
13679 break;
13680
13681 case OPTION_NO_FIX_RM7000:
13682 mips_fix_rm7000 = 0;
13683 break;
13684
c67a084a
NC
13685 case OPTION_FIX_LOONGSON2F_JUMP:
13686 mips_fix_loongson2f_jump = TRUE;
13687 break;
13688
13689 case OPTION_NO_FIX_LOONGSON2F_JUMP:
13690 mips_fix_loongson2f_jump = FALSE;
13691 break;
13692
13693 case OPTION_FIX_LOONGSON2F_NOP:
13694 mips_fix_loongson2f_nop = TRUE;
13695 break;
13696
13697 case OPTION_NO_FIX_LOONGSON2F_NOP:
13698 mips_fix_loongson2f_nop = FALSE;
13699 break;
13700
d766e8ec
RS
13701 case OPTION_FIX_VR4120:
13702 mips_fix_vr4120 = 1;
60b63b72
RS
13703 break;
13704
d766e8ec
RS
13705 case OPTION_NO_FIX_VR4120:
13706 mips_fix_vr4120 = 0;
60b63b72
RS
13707 break;
13708
7d8e00cf
RS
13709 case OPTION_FIX_VR4130:
13710 mips_fix_vr4130 = 1;
13711 break;
13712
13713 case OPTION_NO_FIX_VR4130:
13714 mips_fix_vr4130 = 0;
13715 break;
13716
d954098f
DD
13717 case OPTION_FIX_CN63XXP1:
13718 mips_fix_cn63xxp1 = TRUE;
13719 break;
13720
13721 case OPTION_NO_FIX_CN63XXP1:
13722 mips_fix_cn63xxp1 = FALSE;
13723 break;
13724
4a6a3df4
AO
13725 case OPTION_RELAX_BRANCH:
13726 mips_relax_branch = 1;
13727 break;
13728
13729 case OPTION_NO_RELAX_BRANCH:
13730 mips_relax_branch = 0;
13731 break;
13732
833794fc
MR
13733 case OPTION_INSN32:
13734 mips_opts.insn32 = TRUE;
13735 break;
13736
13737 case OPTION_NO_INSN32:
13738 mips_opts.insn32 = FALSE;
13739 break;
13740
aa6975fb
ILT
13741 case OPTION_MSHARED:
13742 mips_in_shared = TRUE;
13743 break;
13744
13745 case OPTION_MNO_SHARED:
13746 mips_in_shared = FALSE;
13747 break;
13748
aed1a261
RS
13749 case OPTION_MSYM32:
13750 mips_opts.sym32 = TRUE;
13751 break;
13752
13753 case OPTION_MNO_SYM32:
13754 mips_opts.sym32 = FALSE;
13755 break;
13756
252b5132
RH
13757 /* When generating ELF code, we permit -KPIC and -call_shared to
13758 select SVR4_PIC, and -non_shared to select no PIC. This is
13759 intended to be compatible with Irix 5. */
13760 case OPTION_CALL_SHARED:
252b5132 13761 mips_pic = SVR4_PIC;
143d77c5 13762 mips_abicalls = TRUE;
252b5132
RH
13763 break;
13764
861fb55a 13765 case OPTION_CALL_NONPIC:
861fb55a
DJ
13766 mips_pic = NO_PIC;
13767 mips_abicalls = TRUE;
13768 break;
13769
252b5132 13770 case OPTION_NON_SHARED:
252b5132 13771 mips_pic = NO_PIC;
143d77c5 13772 mips_abicalls = FALSE;
252b5132
RH
13773 break;
13774
44075ae2
TS
13775 /* The -xgot option tells the assembler to use 32 bit offsets
13776 when accessing the got in SVR4_PIC mode. It is for Irix
252b5132
RH
13777 compatibility. */
13778 case OPTION_XGOT:
13779 mips_big_got = 1;
13780 break;
13781
13782 case 'G':
6caf9ef4
TS
13783 g_switch_value = atoi (arg);
13784 g_switch_seen = 1;
252b5132
RH
13785 break;
13786
34ba82a8
TS
13787 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
13788 and -mabi=64. */
252b5132 13789 case OPTION_32:
f3ded42a 13790 mips_abi = O32_ABI;
252b5132
RH
13791 break;
13792
e013f690 13793 case OPTION_N32:
316f5878 13794 mips_abi = N32_ABI;
e013f690 13795 break;
252b5132 13796
e013f690 13797 case OPTION_64:
316f5878 13798 mips_abi = N64_ABI;
f43abd2b 13799 if (!support_64bit_objects())
1661c76c 13800 as_fatal (_("no compiled in support for 64 bit object file format"));
252b5132
RH
13801 break;
13802
c97ef257 13803 case OPTION_GP32:
0b35dfee 13804 file_mips_opts.gp32 = 1;
c97ef257
AH
13805 break;
13806
13807 case OPTION_GP64:
0b35dfee 13808 file_mips_opts.gp32 = 0;
c97ef257 13809 break;
252b5132 13810
ca4e0257 13811 case OPTION_FP32:
0b35dfee 13812 file_mips_opts.fp = 32;
316f5878
RS
13813 break;
13814
13815 case OPTION_FP64:
0b35dfee 13816 file_mips_opts.fp = 64;
ca4e0257
RS
13817 break;
13818
037b32b9 13819 case OPTION_SINGLE_FLOAT:
0b35dfee 13820 file_mips_opts.single_float = 1;
037b32b9
AN
13821 break;
13822
13823 case OPTION_DOUBLE_FLOAT:
0b35dfee 13824 file_mips_opts.single_float = 0;
037b32b9
AN
13825 break;
13826
13827 case OPTION_SOFT_FLOAT:
0b35dfee 13828 file_mips_opts.soft_float = 1;
037b32b9
AN
13829 break;
13830
13831 case OPTION_HARD_FLOAT:
0b35dfee 13832 file_mips_opts.soft_float = 0;
037b32b9
AN
13833 break;
13834
252b5132 13835 case OPTION_MABI:
e013f690 13836 if (strcmp (arg, "32") == 0)
316f5878 13837 mips_abi = O32_ABI;
e013f690 13838 else if (strcmp (arg, "o64") == 0)
316f5878 13839 mips_abi = O64_ABI;
e013f690 13840 else if (strcmp (arg, "n32") == 0)
316f5878 13841 mips_abi = N32_ABI;
e013f690
TS
13842 else if (strcmp (arg, "64") == 0)
13843 {
316f5878 13844 mips_abi = N64_ABI;
e013f690 13845 if (! support_64bit_objects())
1661c76c 13846 as_fatal (_("no compiled in support for 64 bit object file "
e013f690
TS
13847 "format"));
13848 }
13849 else if (strcmp (arg, "eabi") == 0)
316f5878 13850 mips_abi = EABI_ABI;
e013f690 13851 else
da0e507f
TS
13852 {
13853 as_fatal (_("invalid abi -mabi=%s"), arg);
13854 return 0;
13855 }
252b5132
RH
13856 break;
13857
6b76fefe 13858 case OPTION_M7000_HILO_FIX:
b34976b6 13859 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
13860 break;
13861
9ee72ff1 13862 case OPTION_MNO_7000_HILO_FIX:
b34976b6 13863 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
13864 break;
13865
ecb4347a 13866 case OPTION_MDEBUG:
b34976b6 13867 mips_flag_mdebug = TRUE;
ecb4347a
DJ
13868 break;
13869
13870 case OPTION_NO_MDEBUG:
b34976b6 13871 mips_flag_mdebug = FALSE;
ecb4347a 13872 break;
dcd410fe
RO
13873
13874 case OPTION_PDR:
13875 mips_flag_pdr = TRUE;
13876 break;
13877
13878 case OPTION_NO_PDR:
13879 mips_flag_pdr = FALSE;
13880 break;
0a44bf69
RS
13881
13882 case OPTION_MVXWORKS_PIC:
13883 mips_pic = VXWORKS_PIC;
13884 break;
ecb4347a 13885
ba92f887
MR
13886 case OPTION_NAN:
13887 if (strcmp (arg, "2008") == 0)
13888 mips_flag_nan2008 = TRUE;
13889 else if (strcmp (arg, "legacy") == 0)
13890 mips_flag_nan2008 = FALSE;
13891 else
13892 {
1661c76c 13893 as_fatal (_("invalid NaN setting -mnan=%s"), arg);
ba92f887
MR
13894 return 0;
13895 }
13896 break;
13897
252b5132
RH
13898 default:
13899 return 0;
13900 }
13901
c67a084a
NC
13902 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
13903
252b5132
RH
13904 return 1;
13905}
316f5878
RS
13906\f
13907/* Set up globals to generate code for the ISA or processor
13908 described by INFO. */
252b5132 13909
252b5132 13910static void
17a2f251 13911mips_set_architecture (const struct mips_cpu_info *info)
252b5132 13912{
316f5878 13913 if (info != 0)
252b5132 13914 {
0b35dfee 13915 file_mips_opts.arch = info->cpu;
fef14a42 13916 mips_opts.arch = info->cpu;
316f5878 13917 mips_opts.isa = info->isa;
252b5132 13918 }
252b5132
RH
13919}
13920
252b5132 13921
316f5878 13922/* Likewise for tuning. */
252b5132 13923
316f5878 13924static void
17a2f251 13925mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
13926{
13927 if (info != 0)
fef14a42 13928 mips_tune = info->cpu;
316f5878 13929}
80cc45a5 13930
34ba82a8 13931
252b5132 13932void
17a2f251 13933mips_after_parse_args (void)
e9670677 13934{
fef14a42
TS
13935 const struct mips_cpu_info *arch_info = 0;
13936 const struct mips_cpu_info *tune_info = 0;
13937
e9670677 13938 /* GP relative stuff not working for PE */
6caf9ef4 13939 if (strncmp (TARGET_OS, "pe", 2) == 0)
e9670677 13940 {
6caf9ef4 13941 if (g_switch_seen && g_switch_value != 0)
1661c76c 13942 as_bad (_("-G not supported in this configuration"));
e9670677
MR
13943 g_switch_value = 0;
13944 }
13945
cac012d6
AO
13946 if (mips_abi == NO_ABI)
13947 mips_abi = MIPS_DEFAULT_ABI;
13948
22923709
RS
13949 /* The following code determines the architecture and register size.
13950 Similar code was added to GCC 3.3 (see override_options() in
13951 config/mips/mips.c). The GAS and GCC code should be kept in sync
13952 as much as possible. */
e9670677 13953
316f5878 13954 if (mips_arch_string != 0)
fef14a42 13955 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 13956
0b35dfee 13957 if (file_mips_opts.isa != ISA_UNKNOWN)
e9670677 13958 {
0b35dfee 13959 /* Handle -mipsN. At this point, file_mips_opts.isa contains the
fef14a42 13960 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 13961 the -march selection (if any). */
fef14a42 13962 if (arch_info != 0)
e9670677 13963 {
316f5878
RS
13964 /* -march takes precedence over -mipsN, since it is more descriptive.
13965 There's no harm in specifying both as long as the ISA levels
13966 are the same. */
0b35dfee 13967 if (file_mips_opts.isa != arch_info->isa)
1661c76c
RS
13968 as_bad (_("-%s conflicts with the other architecture options,"
13969 " which imply -%s"),
0b35dfee 13970 mips_cpu_info_from_isa (file_mips_opts.isa)->name,
fef14a42 13971 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 13972 }
316f5878 13973 else
0b35dfee 13974 arch_info = mips_cpu_info_from_isa (file_mips_opts.isa);
e9670677
MR
13975 }
13976
fef14a42 13977 if (arch_info == 0)
95bfe26e
MF
13978 {
13979 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
13980 gas_assert (arch_info);
13981 }
e9670677 13982
fef14a42 13983 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
20203fb9 13984 as_bad (_("-march=%s is not compatible with the selected ABI"),
fef14a42
TS
13985 arch_info->name);
13986
13987 mips_set_architecture (arch_info);
13988
0b35dfee 13989 /* Optimize for file_mips_opts.arch, unless -mtune selects a different
13990 processor. */
fef14a42
TS
13991 if (mips_tune_string != 0)
13992 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 13993
fef14a42
TS
13994 if (tune_info == 0)
13995 mips_set_tune (arch_info);
13996 else
13997 mips_set_tune (tune_info);
e9670677 13998
0b35dfee 13999 if (file_mips_opts.gp32 >= 0)
e9670677 14000 {
316f5878
RS
14001 /* The user specified the size of the integer registers. Make sure
14002 it agrees with the ABI and ISA. */
0b35dfee 14003 if (file_mips_opts.gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
316f5878 14004 as_bad (_("-mgp64 used with a 32-bit processor"));
0b35dfee 14005 else if (file_mips_opts.gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
316f5878 14006 as_bad (_("-mgp32 used with a 64-bit ABI"));
0b35dfee 14007 else if (file_mips_opts.gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
316f5878 14008 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
14009 }
14010 else
14011 {
316f5878
RS
14012 /* Infer the integer register size from the ABI and processor.
14013 Restrict ourselves to 32-bit registers if that's all the
14014 processor has, or if the ABI cannot handle 64-bit registers. */
0b35dfee 14015 file_mips_opts.gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
14016 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
e9670677
MR
14017 }
14018
0b35dfee 14019 switch (file_mips_opts.fp)
ad3fea08
TS
14020 {
14021 default:
14022 case -1:
14023 /* No user specified float register size.
14024 ??? GAS treats single-float processors as though they had 64-bit
14025 float registers (although it complains when double-precision
14026 instructions are used). As things stand, saying they have 32-bit
14027 registers would lead to spurious "register must be even" messages.
14028 So here we assume float registers are never smaller than the
14029 integer ones. */
0b35dfee 14030 if (file_mips_opts.gp32 == 0)
ad3fea08 14031 /* 64-bit integer registers implies 64-bit float registers. */
0b35dfee 14032 file_mips_opts.fp = 64;
c6278170 14033 else if ((mips_opts.ase & FP64_ASES)
ad3fea08
TS
14034 && ISA_HAS_64BIT_FPRS (mips_opts.isa))
14035 /* -mips3d and -mdmx imply 64-bit float registers, if possible. */
0b35dfee 14036 file_mips_opts.fp = 64;
ad3fea08
TS
14037 else
14038 /* 32-bit float registers. */
0b35dfee 14039 file_mips_opts.fp = 32;
ad3fea08
TS
14040 break;
14041
14042 /* The user specified the size of the float registers. Check if it
14043 agrees with the ABI and ISA. */
0b35dfee 14044 case 64:
ad3fea08
TS
14045 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
14046 as_bad (_("-mfp64 used with a 32-bit fpu"));
14047 else if (ABI_NEEDS_32BIT_REGS (mips_abi)
14048 && !ISA_HAS_MXHC1 (mips_opts.isa))
14049 as_warn (_("-mfp64 used with a 32-bit ABI"));
14050 break;
0b35dfee 14051 case 32:
ad3fea08
TS
14052 if (ABI_NEEDS_64BIT_REGS (mips_abi))
14053 as_warn (_("-mfp32 used with a 64-bit ABI"));
14054 break;
14055 }
e9670677 14056
316f5878 14057 /* End of GCC-shared inference code. */
e9670677 14058
17a2f251
TS
14059 /* This flag is set when we have a 64-bit capable CPU but use only
14060 32-bit wide registers. Note that EABI does not use it. */
14061 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
0b35dfee 14062 && ((mips_abi == NO_ABI && file_mips_opts.gp32 == 1)
17a2f251 14063 || mips_abi == O32_ABI))
316f5878 14064 mips_32bitmode = 1;
e9670677
MR
14065
14066 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
14067 as_bad (_("trap exception not supported at ISA 1"));
14068
e9670677
MR
14069 /* If the selected architecture includes support for ASEs, enable
14070 generation of code for them. */
a4672219 14071 if (mips_opts.mips16 == -1)
0b35dfee 14072 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_opts.arch)) ? 1 : 0;
df58fc94 14073 if (mips_opts.micromips == -1)
0b35dfee 14074 mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_opts.arch))
14075 ? 1 : 0;
846ef2d0
RS
14076
14077 /* MIPS3D and MDMX require 64-bit FPRs, so -mfp32 should stop those
14078 ASEs from being selected implicitly. */
0b35dfee 14079 if (file_mips_opts.fp != 64)
846ef2d0
RS
14080 file_ase_explicit |= ASE_MIPS3D | ASE_MDMX;
14081
14082 /* If the user didn't explicitly select or deselect a particular ASE,
14083 use the default setting for the CPU. */
14084 mips_opts.ase |= (arch_info->ase & ~file_ase_explicit);
14085
0b35dfee 14086 file_mips_opts.isa = mips_opts.isa;
14087 file_mips_opts.ase = mips_opts.ase;
14088 mips_opts.gp32 = file_mips_opts.gp32;
14089 mips_opts.fp = file_mips_opts.fp;
14090 mips_opts.soft_float = file_mips_opts.soft_float;
14091 mips_opts.single_float = file_mips_opts.single_float;
e9670677 14092
c6278170
RS
14093 mips_check_isa_supports_ases ();
14094
ecb4347a 14095 if (mips_flag_mdebug < 0)
e8044f35 14096 mips_flag_mdebug = 0;
e9670677
MR
14097}
14098\f
14099void
17a2f251 14100mips_init_after_args (void)
252b5132
RH
14101{
14102 /* initialize opcodes */
14103 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 14104 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
14105}
14106
14107long
17a2f251 14108md_pcrel_from (fixS *fixP)
252b5132 14109{
a7ebbfdf
TS
14110 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
14111 switch (fixP->fx_r_type)
14112 {
df58fc94
RS
14113 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
14114 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
14115 /* Return the address of the delay slot. */
14116 return addr + 2;
14117
14118 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
14119 case BFD_RELOC_MICROMIPS_JMP:
a7ebbfdf
TS
14120 case BFD_RELOC_16_PCREL_S2:
14121 case BFD_RELOC_MIPS_JMP:
14122 /* Return the address of the delay slot. */
14123 return addr + 4;
df58fc94 14124
a7ebbfdf
TS
14125 default:
14126 return addr;
14127 }
252b5132
RH
14128}
14129
252b5132
RH
14130/* This is called before the symbol table is processed. In order to
14131 work with gcc when using mips-tfile, we must keep all local labels.
14132 However, in other cases, we want to discard them. If we were
14133 called with -g, but we didn't see any debugging information, it may
14134 mean that gcc is smuggling debugging information through to
14135 mips-tfile, in which case we must generate all local labels. */
14136
14137void
17a2f251 14138mips_frob_file_before_adjust (void)
252b5132
RH
14139{
14140#ifndef NO_ECOFF_DEBUGGING
14141 if (ECOFF_DEBUGGING
14142 && mips_debug != 0
14143 && ! ecoff_debugging_seen)
14144 flag_keep_locals = 1;
14145#endif
14146}
14147
3b91255e 14148/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 14149 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
14150 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
14151 relocation operators.
14152
14153 For our purposes, a %lo() expression matches a %got() or %hi()
14154 expression if:
14155
14156 (a) it refers to the same symbol; and
14157 (b) the offset applied in the %lo() expression is no lower than
14158 the offset applied in the %got() or %hi().
14159
14160 (b) allows us to cope with code like:
14161
14162 lui $4,%hi(foo)
14163 lh $4,%lo(foo+2)($4)
14164
14165 ...which is legal on RELA targets, and has a well-defined behaviour
14166 if the user knows that adding 2 to "foo" will not induce a carry to
14167 the high 16 bits.
14168
14169 When several %lo()s match a particular %got() or %hi(), we use the
14170 following rules to distinguish them:
14171
14172 (1) %lo()s with smaller offsets are a better match than %lo()s with
14173 higher offsets.
14174
14175 (2) %lo()s with no matching %got() or %hi() are better than those
14176 that already have a matching %got() or %hi().
14177
14178 (3) later %lo()s are better than earlier %lo()s.
14179
14180 These rules are applied in order.
14181
14182 (1) means, among other things, that %lo()s with identical offsets are
14183 chosen if they exist.
14184
14185 (2) means that we won't associate several high-part relocations with
14186 the same low-part relocation unless there's no alternative. Having
14187 several high parts for the same low part is a GNU extension; this rule
14188 allows careful users to avoid it.
14189
14190 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
14191 with the last high-part relocation being at the front of the list.
14192 It therefore makes sense to choose the last matching low-part
14193 relocation, all other things being equal. It's also easier
14194 to code that way. */
252b5132
RH
14195
14196void
17a2f251 14197mips_frob_file (void)
252b5132
RH
14198{
14199 struct mips_hi_fixup *l;
35903be0 14200 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
252b5132
RH
14201
14202 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
14203 {
14204 segment_info_type *seginfo;
3b91255e
RS
14205 bfd_boolean matched_lo_p;
14206 fixS **hi_pos, **lo_pos, **pos;
252b5132 14207
9c2799c2 14208 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 14209
5919d012 14210 /* If a GOT16 relocation turns out to be against a global symbol,
b886a2ab
RS
14211 there isn't supposed to be a matching LO. Ignore %gots against
14212 constants; we'll report an error for those later. */
738e5348 14213 if (got16_reloc_p (l->fixp->fx_r_type)
b886a2ab
RS
14214 && !(l->fixp->fx_addsy
14215 && pic_need_relax (l->fixp->fx_addsy, l->seg)))
5919d012
RS
14216 continue;
14217
14218 /* Check quickly whether the next fixup happens to be a matching %lo. */
14219 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
14220 continue;
14221
252b5132 14222 seginfo = seg_info (l->seg);
252b5132 14223
3b91255e
RS
14224 /* Set HI_POS to the position of this relocation in the chain.
14225 Set LO_POS to the position of the chosen low-part relocation.
14226 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
14227 relocation that matches an immediately-preceding high-part
14228 relocation. */
14229 hi_pos = NULL;
14230 lo_pos = NULL;
14231 matched_lo_p = FALSE;
738e5348 14232 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
35903be0 14233
3b91255e
RS
14234 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
14235 {
14236 if (*pos == l->fixp)
14237 hi_pos = pos;
14238
35903be0 14239 if ((*pos)->fx_r_type == looking_for_rtype
30cfc97a 14240 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
3b91255e
RS
14241 && (*pos)->fx_offset >= l->fixp->fx_offset
14242 && (lo_pos == NULL
14243 || (*pos)->fx_offset < (*lo_pos)->fx_offset
14244 || (!matched_lo_p
14245 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
14246 lo_pos = pos;
14247
14248 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
14249 && fixup_has_matching_lo_p (*pos));
14250 }
14251
14252 /* If we found a match, remove the high-part relocation from its
14253 current position and insert it before the low-part relocation.
14254 Make the offsets match so that fixup_has_matching_lo_p()
14255 will return true.
14256
14257 We don't warn about unmatched high-part relocations since some
14258 versions of gcc have been known to emit dead "lui ...%hi(...)"
14259 instructions. */
14260 if (lo_pos != NULL)
14261 {
14262 l->fixp->fx_offset = (*lo_pos)->fx_offset;
14263 if (l->fixp->fx_next != *lo_pos)
252b5132 14264 {
3b91255e
RS
14265 *hi_pos = l->fixp->fx_next;
14266 l->fixp->fx_next = *lo_pos;
14267 *lo_pos = l->fixp;
252b5132 14268 }
252b5132
RH
14269 }
14270 }
14271}
14272
252b5132 14273int
17a2f251 14274mips_force_relocation (fixS *fixp)
252b5132 14275{
ae6063d4 14276 if (generic_force_reloc (fixp))
252b5132
RH
14277 return 1;
14278
df58fc94
RS
14279 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
14280 so that the linker relaxation can update targets. */
14281 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
14282 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
14283 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
14284 return 1;
14285
3e722fb5 14286 return 0;
252b5132
RH
14287}
14288
b886a2ab
RS
14289/* Read the instruction associated with RELOC from BUF. */
14290
14291static unsigned int
14292read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
14293{
14294 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
14295 return read_compressed_insn (buf, 4);
14296 else
14297 return read_insn (buf);
14298}
14299
14300/* Write instruction INSN to BUF, given that it has been relocated
14301 by RELOC. */
14302
14303static void
14304write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
14305 unsigned long insn)
14306{
14307 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
14308 write_compressed_insn (buf, insn, 4);
14309 else
14310 write_insn (buf, insn);
14311}
14312
252b5132
RH
14313/* Apply a fixup to the object file. */
14314
94f592af 14315void
55cf6793 14316md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 14317{
4d68580a 14318 char *buf;
b886a2ab 14319 unsigned long insn;
a7ebbfdf 14320 reloc_howto_type *howto;
252b5132 14321
d56a8dda
RS
14322 if (fixP->fx_pcrel)
14323 switch (fixP->fx_r_type)
14324 {
14325 case BFD_RELOC_16_PCREL_S2:
14326 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
14327 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
14328 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
14329 case BFD_RELOC_32_PCREL:
14330 break;
14331
14332 case BFD_RELOC_32:
14333 fixP->fx_r_type = BFD_RELOC_32_PCREL;
14334 break;
14335
14336 default:
14337 as_bad_where (fixP->fx_file, fixP->fx_line,
14338 _("PC-relative reference to a different section"));
14339 break;
14340 }
14341
14342 /* Handle BFD_RELOC_8, since it's easy. Punt on other bfd relocations
14343 that have no MIPS ELF equivalent. */
14344 if (fixP->fx_r_type != BFD_RELOC_8)
14345 {
14346 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
14347 if (!howto)
14348 return;
14349 }
65551fa4 14350
df58fc94
RS
14351 gas_assert (fixP->fx_size == 2
14352 || fixP->fx_size == 4
d56a8dda 14353 || fixP->fx_r_type == BFD_RELOC_8
90ecf173
MR
14354 || fixP->fx_r_type == BFD_RELOC_16
14355 || fixP->fx_r_type == BFD_RELOC_64
14356 || fixP->fx_r_type == BFD_RELOC_CTOR
14357 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
df58fc94 14358 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
90ecf173
MR
14359 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
14360 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
14361 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64);
252b5132 14362
4d68580a 14363 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132 14364
b1dca8ee
RS
14365 /* Don't treat parts of a composite relocation as done. There are two
14366 reasons for this:
14367
14368 (1) The second and third parts will be against 0 (RSS_UNDEF) but
14369 should nevertheless be emitted if the first part is.
14370
14371 (2) In normal usage, composite relocations are never assembly-time
14372 constants. The easiest way of dealing with the pathological
14373 exceptions is to generate a relocation against STN_UNDEF and
14374 leave everything up to the linker. */
3994f87e 14375 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
252b5132
RH
14376 fixP->fx_done = 1;
14377
14378 switch (fixP->fx_r_type)
14379 {
3f98094e
DJ
14380 case BFD_RELOC_MIPS_TLS_GD:
14381 case BFD_RELOC_MIPS_TLS_LDM:
741d6ea8
JM
14382 case BFD_RELOC_MIPS_TLS_DTPREL32:
14383 case BFD_RELOC_MIPS_TLS_DTPREL64:
3f98094e
DJ
14384 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
14385 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
14386 case BFD_RELOC_MIPS_TLS_GOTTPREL:
d0f13682
CLT
14387 case BFD_RELOC_MIPS_TLS_TPREL32:
14388 case BFD_RELOC_MIPS_TLS_TPREL64:
3f98094e
DJ
14389 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
14390 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
df58fc94
RS
14391 case BFD_RELOC_MICROMIPS_TLS_GD:
14392 case BFD_RELOC_MICROMIPS_TLS_LDM:
14393 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
14394 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
14395 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
14396 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
14397 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
d0f13682
CLT
14398 case BFD_RELOC_MIPS16_TLS_GD:
14399 case BFD_RELOC_MIPS16_TLS_LDM:
14400 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
14401 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
14402 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
14403 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
14404 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
b886a2ab
RS
14405 if (!fixP->fx_addsy)
14406 {
14407 as_bad_where (fixP->fx_file, fixP->fx_line,
14408 _("TLS relocation against a constant"));
14409 break;
14410 }
3f98094e
DJ
14411 S_SET_THREAD_LOCAL (fixP->fx_addsy);
14412 /* fall through */
14413
252b5132 14414 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
14415 case BFD_RELOC_MIPS_SHIFT5:
14416 case BFD_RELOC_MIPS_SHIFT6:
14417 case BFD_RELOC_MIPS_GOT_DISP:
14418 case BFD_RELOC_MIPS_GOT_PAGE:
14419 case BFD_RELOC_MIPS_GOT_OFST:
14420 case BFD_RELOC_MIPS_SUB:
14421 case BFD_RELOC_MIPS_INSERT_A:
14422 case BFD_RELOC_MIPS_INSERT_B:
14423 case BFD_RELOC_MIPS_DELETE:
14424 case BFD_RELOC_MIPS_HIGHEST:
14425 case BFD_RELOC_MIPS_HIGHER:
14426 case BFD_RELOC_MIPS_SCN_DISP:
14427 case BFD_RELOC_MIPS_REL16:
14428 case BFD_RELOC_MIPS_RELGOT:
14429 case BFD_RELOC_MIPS_JALR:
252b5132
RH
14430 case BFD_RELOC_HI16:
14431 case BFD_RELOC_HI16_S:
b886a2ab 14432 case BFD_RELOC_LO16:
cdf6fd85 14433 case BFD_RELOC_GPREL16:
252b5132
RH
14434 case BFD_RELOC_MIPS_LITERAL:
14435 case BFD_RELOC_MIPS_CALL16:
14436 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 14437 case BFD_RELOC_GPREL32:
252b5132
RH
14438 case BFD_RELOC_MIPS_GOT_HI16:
14439 case BFD_RELOC_MIPS_GOT_LO16:
14440 case BFD_RELOC_MIPS_CALL_HI16:
14441 case BFD_RELOC_MIPS_CALL_LO16:
14442 case BFD_RELOC_MIPS16_GPREL:
738e5348
RS
14443 case BFD_RELOC_MIPS16_GOT16:
14444 case BFD_RELOC_MIPS16_CALL16:
d6f16593
MR
14445 case BFD_RELOC_MIPS16_HI16:
14446 case BFD_RELOC_MIPS16_HI16_S:
b886a2ab 14447 case BFD_RELOC_MIPS16_LO16:
252b5132 14448 case BFD_RELOC_MIPS16_JMP:
df58fc94
RS
14449 case BFD_RELOC_MICROMIPS_JMP:
14450 case BFD_RELOC_MICROMIPS_GOT_DISP:
14451 case BFD_RELOC_MICROMIPS_GOT_PAGE:
14452 case BFD_RELOC_MICROMIPS_GOT_OFST:
14453 case BFD_RELOC_MICROMIPS_SUB:
14454 case BFD_RELOC_MICROMIPS_HIGHEST:
14455 case BFD_RELOC_MICROMIPS_HIGHER:
14456 case BFD_RELOC_MICROMIPS_SCN_DISP:
14457 case BFD_RELOC_MICROMIPS_JALR:
14458 case BFD_RELOC_MICROMIPS_HI16:
14459 case BFD_RELOC_MICROMIPS_HI16_S:
b886a2ab 14460 case BFD_RELOC_MICROMIPS_LO16:
df58fc94
RS
14461 case BFD_RELOC_MICROMIPS_GPREL16:
14462 case BFD_RELOC_MICROMIPS_LITERAL:
14463 case BFD_RELOC_MICROMIPS_CALL16:
14464 case BFD_RELOC_MICROMIPS_GOT16:
14465 case BFD_RELOC_MICROMIPS_GOT_HI16:
14466 case BFD_RELOC_MICROMIPS_GOT_LO16:
14467 case BFD_RELOC_MICROMIPS_CALL_HI16:
14468 case BFD_RELOC_MICROMIPS_CALL_LO16:
067ec077 14469 case BFD_RELOC_MIPS_EH:
b886a2ab
RS
14470 if (fixP->fx_done)
14471 {
14472 offsetT value;
14473
14474 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
14475 {
14476 insn = read_reloc_insn (buf, fixP->fx_r_type);
14477 if (mips16_reloc_p (fixP->fx_r_type))
14478 insn |= mips16_immed_extend (value, 16);
14479 else
14480 insn |= (value & 0xffff);
14481 write_reloc_insn (buf, fixP->fx_r_type, insn);
14482 }
14483 else
14484 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 14485 _("unsupported constant in relocation"));
b886a2ab 14486 }
252b5132
RH
14487 break;
14488
252b5132
RH
14489 case BFD_RELOC_64:
14490 /* This is handled like BFD_RELOC_32, but we output a sign
14491 extended value if we are only 32 bits. */
3e722fb5 14492 if (fixP->fx_done)
252b5132
RH
14493 {
14494 if (8 <= sizeof (valueT))
4d68580a 14495 md_number_to_chars (buf, *valP, 8);
252b5132
RH
14496 else
14497 {
a7ebbfdf 14498 valueT hiv;
252b5132 14499
a7ebbfdf 14500 if ((*valP & 0x80000000) != 0)
252b5132
RH
14501 hiv = 0xffffffff;
14502 else
14503 hiv = 0;
4d68580a
RS
14504 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
14505 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
252b5132
RH
14506 }
14507 }
14508 break;
14509
056350c6 14510 case BFD_RELOC_RVA:
252b5132 14511 case BFD_RELOC_32:
b47468a6 14512 case BFD_RELOC_32_PCREL:
252b5132 14513 case BFD_RELOC_16:
d56a8dda 14514 case BFD_RELOC_8:
252b5132 14515 /* If we are deleting this reloc entry, we must fill in the
54f4ddb3
TS
14516 value now. This can happen if we have a .word which is not
14517 resolved when it appears but is later defined. */
252b5132 14518 if (fixP->fx_done)
4d68580a 14519 md_number_to_chars (buf, *valP, fixP->fx_size);
252b5132
RH
14520 break;
14521
252b5132 14522 case BFD_RELOC_16_PCREL_S2:
a7ebbfdf 14523 if ((*valP & 0x3) != 0)
cb56d3d3 14524 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 14525 _("branch to misaligned address (%lx)"), (long) *valP);
cb56d3d3 14526
54f4ddb3
TS
14527 /* We need to save the bits in the instruction since fixup_segment()
14528 might be deleting the relocation entry (i.e., a branch within
14529 the current segment). */
a7ebbfdf 14530 if (! fixP->fx_done)
bb2d6cd7 14531 break;
252b5132 14532
54f4ddb3 14533 /* Update old instruction data. */
4d68580a 14534 insn = read_insn (buf);
252b5132 14535
a7ebbfdf
TS
14536 if (*valP + 0x20000 <= 0x3ffff)
14537 {
14538 insn |= (*valP >> 2) & 0xffff;
4d68580a 14539 write_insn (buf, insn);
a7ebbfdf
TS
14540 }
14541 else if (mips_pic == NO_PIC
14542 && fixP->fx_done
14543 && fixP->fx_frag->fr_address >= text_section->vma
14544 && (fixP->fx_frag->fr_address
587aac4e 14545 < text_section->vma + bfd_get_section_size (text_section))
a7ebbfdf
TS
14546 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
14547 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
14548 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
14549 {
14550 /* The branch offset is too large. If this is an
14551 unconditional branch, and we are not generating PIC code,
14552 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
14553 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
14554 insn = 0x0c000000; /* jal */
252b5132 14555 else
a7ebbfdf
TS
14556 insn = 0x08000000; /* j */
14557 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
14558 fixP->fx_done = 0;
14559 fixP->fx_addsy = section_symbol (text_section);
14560 *valP += md_pcrel_from (fixP);
4d68580a 14561 write_insn (buf, insn);
a7ebbfdf
TS
14562 }
14563 else
14564 {
14565 /* If we got here, we have branch-relaxation disabled,
14566 and there's nothing we can do to fix this instruction
14567 without turning it into a longer sequence. */
14568 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 14569 _("branch out of range"));
252b5132 14570 }
252b5132
RH
14571 break;
14572
df58fc94
RS
14573 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
14574 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
14575 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
14576 /* We adjust the offset back to even. */
14577 if ((*valP & 0x1) != 0)
14578 --(*valP);
14579
14580 if (! fixP->fx_done)
14581 break;
14582
14583 /* Should never visit here, because we keep the relocation. */
14584 abort ();
14585 break;
14586
252b5132
RH
14587 case BFD_RELOC_VTABLE_INHERIT:
14588 fixP->fx_done = 0;
14589 if (fixP->fx_addsy
14590 && !S_IS_DEFINED (fixP->fx_addsy)
14591 && !S_IS_WEAK (fixP->fx_addsy))
14592 S_SET_WEAK (fixP->fx_addsy);
14593 break;
14594
14595 case BFD_RELOC_VTABLE_ENTRY:
14596 fixP->fx_done = 0;
14597 break;
14598
14599 default:
b37df7c4 14600 abort ();
252b5132 14601 }
a7ebbfdf
TS
14602
14603 /* Remember value for tc_gen_reloc. */
14604 fixP->fx_addnumber = *valP;
252b5132
RH
14605}
14606
252b5132 14607static symbolS *
17a2f251 14608get_symbol (void)
252b5132
RH
14609{
14610 int c;
14611 char *name;
14612 symbolS *p;
14613
14614 name = input_line_pointer;
14615 c = get_symbol_end ();
14616 p = (symbolS *) symbol_find_or_make (name);
14617 *input_line_pointer = c;
14618 return p;
14619}
14620
742a56fe
RS
14621/* Align the current frag to a given power of two. If a particular
14622 fill byte should be used, FILL points to an integer that contains
14623 that byte, otherwise FILL is null.
14624
462427c4
RS
14625 This function used to have the comment:
14626
14627 The MIPS assembler also automatically adjusts any preceding label.
14628
14629 The implementation therefore applied the adjustment to a maximum of
14630 one label. However, other label adjustments are applied to batches
14631 of labels, and adjusting just one caused problems when new labels
14632 were added for the sake of debugging or unwind information.
14633 We therefore adjust all preceding labels (given as LABELS) instead. */
252b5132
RH
14634
14635static void
462427c4 14636mips_align (int to, int *fill, struct insn_label_list *labels)
252b5132 14637{
7d10b47d 14638 mips_emit_delays ();
df58fc94 14639 mips_record_compressed_mode ();
742a56fe
RS
14640 if (fill == NULL && subseg_text_p (now_seg))
14641 frag_align_code (to, 0);
14642 else
14643 frag_align (to, fill ? *fill : 0, 0);
252b5132 14644 record_alignment (now_seg, to);
462427c4 14645 mips_move_labels (labels, FALSE);
252b5132
RH
14646}
14647
14648/* Align to a given power of two. .align 0 turns off the automatic
14649 alignment used by the data creating pseudo-ops. */
14650
14651static void
17a2f251 14652s_align (int x ATTRIBUTE_UNUSED)
252b5132 14653{
742a56fe 14654 int temp, fill_value, *fill_ptr;
49954fb4 14655 long max_alignment = 28;
252b5132 14656
54f4ddb3 14657 /* o Note that the assembler pulls down any immediately preceding label
252b5132 14658 to the aligned address.
54f4ddb3 14659 o It's not documented but auto alignment is reinstated by
252b5132 14660 a .align pseudo instruction.
54f4ddb3 14661 o Note also that after auto alignment is turned off the mips assembler
252b5132 14662 issues an error on attempt to assemble an improperly aligned data item.
54f4ddb3 14663 We don't. */
252b5132
RH
14664
14665 temp = get_absolute_expression ();
14666 if (temp > max_alignment)
1661c76c 14667 as_bad (_("alignment too large, %d assumed"), temp = max_alignment);
252b5132
RH
14668 else if (temp < 0)
14669 {
1661c76c 14670 as_warn (_("alignment negative, 0 assumed"));
252b5132
RH
14671 temp = 0;
14672 }
14673 if (*input_line_pointer == ',')
14674 {
f9419b05 14675 ++input_line_pointer;
742a56fe
RS
14676 fill_value = get_absolute_expression ();
14677 fill_ptr = &fill_value;
252b5132
RH
14678 }
14679 else
742a56fe 14680 fill_ptr = 0;
252b5132
RH
14681 if (temp)
14682 {
a8dbcb85
TS
14683 segment_info_type *si = seg_info (now_seg);
14684 struct insn_label_list *l = si->label_list;
54f4ddb3 14685 /* Auto alignment should be switched on by next section change. */
252b5132 14686 auto_align = 1;
462427c4 14687 mips_align (temp, fill_ptr, l);
252b5132
RH
14688 }
14689 else
14690 {
14691 auto_align = 0;
14692 }
14693
14694 demand_empty_rest_of_line ();
14695}
14696
252b5132 14697static void
17a2f251 14698s_change_sec (int sec)
252b5132
RH
14699{
14700 segT seg;
14701
252b5132
RH
14702 /* The ELF backend needs to know that we are changing sections, so
14703 that .previous works correctly. We could do something like check
b6ff326e 14704 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
14705 as it would not be appropriate to use it in the section changing
14706 functions in read.c, since obj-elf.c intercepts those. FIXME:
14707 This should be cleaner, somehow. */
f3ded42a 14708 obj_elf_section_change_hook ();
252b5132 14709
7d10b47d 14710 mips_emit_delays ();
6a32d874 14711
252b5132
RH
14712 switch (sec)
14713 {
14714 case 't':
14715 s_text (0);
14716 break;
14717 case 'd':
14718 s_data (0);
14719 break;
14720 case 'b':
14721 subseg_set (bss_section, (subsegT) get_absolute_expression ());
14722 demand_empty_rest_of_line ();
14723 break;
14724
14725 case 'r':
4d0d148d
TS
14726 seg = subseg_new (RDATA_SECTION_NAME,
14727 (subsegT) get_absolute_expression ());
f3ded42a
RS
14728 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
14729 | SEC_READONLY | SEC_RELOC
14730 | SEC_DATA));
14731 if (strncmp (TARGET_OS, "elf", 3) != 0)
14732 record_alignment (seg, 4);
4d0d148d 14733 demand_empty_rest_of_line ();
252b5132
RH
14734 break;
14735
14736 case 's':
4d0d148d 14737 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
f3ded42a
RS
14738 bfd_set_section_flags (stdoutput, seg,
14739 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
14740 if (strncmp (TARGET_OS, "elf", 3) != 0)
14741 record_alignment (seg, 4);
4d0d148d
TS
14742 demand_empty_rest_of_line ();
14743 break;
998b3c36
MR
14744
14745 case 'B':
14746 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
f3ded42a
RS
14747 bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
14748 if (strncmp (TARGET_OS, "elf", 3) != 0)
14749 record_alignment (seg, 4);
998b3c36
MR
14750 demand_empty_rest_of_line ();
14751 break;
252b5132
RH
14752 }
14753
14754 auto_align = 1;
14755}
b34976b6 14756
cca86cc8 14757void
17a2f251 14758s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 14759{
cca86cc8
SC
14760 char *section_name;
14761 char c;
684022ea 14762 char next_c = 0;
cca86cc8
SC
14763 int section_type;
14764 int section_flag;
14765 int section_entry_size;
14766 int section_alignment;
b34976b6 14767
cca86cc8
SC
14768 section_name = input_line_pointer;
14769 c = get_symbol_end ();
a816d1ed
AO
14770 if (c)
14771 next_c = *(input_line_pointer + 1);
cca86cc8 14772
4cf0dd0d
TS
14773 /* Do we have .section Name<,"flags">? */
14774 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 14775 {
4cf0dd0d
TS
14776 /* just after name is now '\0'. */
14777 *input_line_pointer = c;
cca86cc8
SC
14778 input_line_pointer = section_name;
14779 obj_elf_section (ignore);
14780 return;
14781 }
14782 input_line_pointer++;
14783
14784 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
14785 if (c == ',')
14786 section_type = get_absolute_expression ();
14787 else
14788 section_type = 0;
14789 if (*input_line_pointer++ == ',')
14790 section_flag = get_absolute_expression ();
14791 else
14792 section_flag = 0;
14793 if (*input_line_pointer++ == ',')
14794 section_entry_size = get_absolute_expression ();
14795 else
14796 section_entry_size = 0;
14797 if (*input_line_pointer++ == ',')
14798 section_alignment = get_absolute_expression ();
14799 else
14800 section_alignment = 0;
87975d2a
AM
14801 /* FIXME: really ignore? */
14802 (void) section_alignment;
cca86cc8 14803
a816d1ed
AO
14804 section_name = xstrdup (section_name);
14805
8ab8a5c8
RS
14806 /* When using the generic form of .section (as implemented by obj-elf.c),
14807 there's no way to set the section type to SHT_MIPS_DWARF. Users have
14808 traditionally had to fall back on the more common @progbits instead.
14809
14810 There's nothing really harmful in this, since bfd will correct
14811 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
708587a4 14812 means that, for backwards compatibility, the special_section entries
8ab8a5c8
RS
14813 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
14814
14815 Even so, we shouldn't force users of the MIPS .section syntax to
14816 incorrectly label the sections as SHT_PROGBITS. The best compromise
14817 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
14818 generic type-checking code. */
14819 if (section_type == SHT_MIPS_DWARF)
14820 section_type = SHT_PROGBITS;
14821
cca86cc8
SC
14822 obj_elf_change_section (section_name, section_type, section_flag,
14823 section_entry_size, 0, 0, 0);
a816d1ed
AO
14824
14825 if (now_seg->name != section_name)
14826 free (section_name);
cca86cc8 14827}
252b5132
RH
14828
14829void
17a2f251 14830mips_enable_auto_align (void)
252b5132
RH
14831{
14832 auto_align = 1;
14833}
14834
14835static void
17a2f251 14836s_cons (int log_size)
252b5132 14837{
a8dbcb85
TS
14838 segment_info_type *si = seg_info (now_seg);
14839 struct insn_label_list *l = si->label_list;
252b5132 14840
7d10b47d 14841 mips_emit_delays ();
252b5132 14842 if (log_size > 0 && auto_align)
462427c4 14843 mips_align (log_size, 0, l);
252b5132 14844 cons (1 << log_size);
a1facbec 14845 mips_clear_insn_labels ();
252b5132
RH
14846}
14847
14848static void
17a2f251 14849s_float_cons (int type)
252b5132 14850{
a8dbcb85
TS
14851 segment_info_type *si = seg_info (now_seg);
14852 struct insn_label_list *l = si->label_list;
252b5132 14853
7d10b47d 14854 mips_emit_delays ();
252b5132
RH
14855
14856 if (auto_align)
49309057
ILT
14857 {
14858 if (type == 'd')
462427c4 14859 mips_align (3, 0, l);
49309057 14860 else
462427c4 14861 mips_align (2, 0, l);
49309057 14862 }
252b5132 14863
252b5132 14864 float_cons (type);
a1facbec 14865 mips_clear_insn_labels ();
252b5132
RH
14866}
14867
14868/* Handle .globl. We need to override it because on Irix 5 you are
14869 permitted to say
14870 .globl foo .text
14871 where foo is an undefined symbol, to mean that foo should be
14872 considered to be the address of a function. */
14873
14874static void
17a2f251 14875s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
14876{
14877 char *name;
14878 int c;
14879 symbolS *symbolP;
14880 flagword flag;
14881
8a06b769 14882 do
252b5132 14883 {
8a06b769 14884 name = input_line_pointer;
252b5132 14885 c = get_symbol_end ();
8a06b769
TS
14886 symbolP = symbol_find_or_make (name);
14887 S_SET_EXTERNAL (symbolP);
14888
252b5132 14889 *input_line_pointer = c;
8a06b769 14890 SKIP_WHITESPACE ();
252b5132 14891
8a06b769
TS
14892 /* On Irix 5, every global symbol that is not explicitly labelled as
14893 being a function is apparently labelled as being an object. */
14894 flag = BSF_OBJECT;
252b5132 14895
8a06b769
TS
14896 if (!is_end_of_line[(unsigned char) *input_line_pointer]
14897 && (*input_line_pointer != ','))
14898 {
14899 char *secname;
14900 asection *sec;
14901
14902 secname = input_line_pointer;
14903 c = get_symbol_end ();
14904 sec = bfd_get_section_by_name (stdoutput, secname);
14905 if (sec == NULL)
14906 as_bad (_("%s: no such section"), secname);
14907 *input_line_pointer = c;
14908
14909 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
14910 flag = BSF_FUNCTION;
14911 }
14912
14913 symbol_get_bfdsym (symbolP)->flags |= flag;
14914
14915 c = *input_line_pointer;
14916 if (c == ',')
14917 {
14918 input_line_pointer++;
14919 SKIP_WHITESPACE ();
14920 if (is_end_of_line[(unsigned char) *input_line_pointer])
14921 c = '\n';
14922 }
14923 }
14924 while (c == ',');
252b5132 14925
252b5132
RH
14926 demand_empty_rest_of_line ();
14927}
14928
14929static void
17a2f251 14930s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
14931{
14932 char *opt;
14933 char c;
14934
14935 opt = input_line_pointer;
14936 c = get_symbol_end ();
14937
14938 if (*opt == 'O')
14939 {
14940 /* FIXME: What does this mean? */
14941 }
14942 else if (strncmp (opt, "pic", 3) == 0)
14943 {
14944 int i;
14945
14946 i = atoi (opt + 3);
14947 if (i == 0)
14948 mips_pic = NO_PIC;
14949 else if (i == 2)
143d77c5 14950 {
8b828383 14951 mips_pic = SVR4_PIC;
143d77c5
EC
14952 mips_abicalls = TRUE;
14953 }
252b5132
RH
14954 else
14955 as_bad (_(".option pic%d not supported"), i);
14956
4d0d148d 14957 if (mips_pic == SVR4_PIC)
252b5132
RH
14958 {
14959 if (g_switch_seen && g_switch_value != 0)
14960 as_warn (_("-G may not be used with SVR4 PIC code"));
14961 g_switch_value = 0;
14962 bfd_set_gp_size (stdoutput, 0);
14963 }
14964 }
14965 else
1661c76c 14966 as_warn (_("unrecognized option \"%s\""), opt);
252b5132
RH
14967
14968 *input_line_pointer = c;
14969 demand_empty_rest_of_line ();
14970}
14971
14972/* This structure is used to hold a stack of .set values. */
14973
e972090a
NC
14974struct mips_option_stack
14975{
252b5132
RH
14976 struct mips_option_stack *next;
14977 struct mips_set_options options;
14978};
14979
14980static struct mips_option_stack *mips_opts_stack;
14981
14982/* Handle the .set pseudo-op. */
14983
14984static void
17a2f251 14985s_mipsset (int x ATTRIBUTE_UNUSED)
252b5132
RH
14986{
14987 char *name = input_line_pointer, ch;
c6278170 14988 const struct mips_ase *ase;
252b5132
RH
14989
14990 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 14991 ++input_line_pointer;
252b5132
RH
14992 ch = *input_line_pointer;
14993 *input_line_pointer = '\0';
14994
14995 if (strcmp (name, "reorder") == 0)
14996 {
7d10b47d
RS
14997 if (mips_opts.noreorder)
14998 end_noreorder ();
252b5132
RH
14999 }
15000 else if (strcmp (name, "noreorder") == 0)
15001 {
7d10b47d
RS
15002 if (!mips_opts.noreorder)
15003 start_noreorder ();
252b5132 15004 }
741fe287
MR
15005 else if (strncmp (name, "at=", 3) == 0)
15006 {
15007 char *s = name + 3;
15008
15009 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
1661c76c 15010 as_bad (_("unrecognized register name `%s'"), s);
741fe287 15011 }
252b5132
RH
15012 else if (strcmp (name, "at") == 0)
15013 {
741fe287 15014 mips_opts.at = ATREG;
252b5132
RH
15015 }
15016 else if (strcmp (name, "noat") == 0)
15017 {
741fe287 15018 mips_opts.at = ZERO;
252b5132
RH
15019 }
15020 else if (strcmp (name, "macro") == 0)
15021 {
15022 mips_opts.warn_about_macros = 0;
15023 }
15024 else if (strcmp (name, "nomacro") == 0)
15025 {
15026 if (mips_opts.noreorder == 0)
15027 as_bad (_("`noreorder' must be set before `nomacro'"));
15028 mips_opts.warn_about_macros = 1;
15029 }
15030 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
15031 {
15032 mips_opts.nomove = 0;
15033 }
15034 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
15035 {
15036 mips_opts.nomove = 1;
15037 }
15038 else if (strcmp (name, "bopt") == 0)
15039 {
15040 mips_opts.nobopt = 0;
15041 }
15042 else if (strcmp (name, "nobopt") == 0)
15043 {
15044 mips_opts.nobopt = 1;
15045 }
ad3fea08 15046 else if (strcmp (name, "gp=default") == 0)
0b35dfee 15047 mips_opts.gp32 = file_mips_opts.gp32;
ad3fea08
TS
15048 else if (strcmp (name, "gp=32") == 0)
15049 mips_opts.gp32 = 1;
15050 else if (strcmp (name, "gp=64") == 0)
15051 {
15052 if (!ISA_HAS_64BIT_REGS (mips_opts.isa))
20203fb9 15053 as_warn (_("%s isa does not support 64-bit registers"),
ad3fea08
TS
15054 mips_cpu_info_from_isa (mips_opts.isa)->name);
15055 mips_opts.gp32 = 0;
15056 }
15057 else if (strcmp (name, "fp=default") == 0)
0b35dfee 15058 mips_opts.fp = file_mips_opts.fp;
ad3fea08 15059 else if (strcmp (name, "fp=32") == 0)
0b35dfee 15060 mips_opts.fp = 32;
ad3fea08
TS
15061 else if (strcmp (name, "fp=64") == 0)
15062 {
15063 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
20203fb9 15064 as_warn (_("%s isa does not support 64-bit floating point registers"),
ad3fea08 15065 mips_cpu_info_from_isa (mips_opts.isa)->name);
0b35dfee 15066 mips_opts.fp = 64;
ad3fea08 15067 }
037b32b9
AN
15068 else if (strcmp (name, "softfloat") == 0)
15069 mips_opts.soft_float = 1;
15070 else if (strcmp (name, "hardfloat") == 0)
15071 mips_opts.soft_float = 0;
15072 else if (strcmp (name, "singlefloat") == 0)
15073 mips_opts.single_float = 1;
15074 else if (strcmp (name, "doublefloat") == 0)
15075 mips_opts.single_float = 0;
252b5132
RH
15076 else if (strcmp (name, "mips16") == 0
15077 || strcmp (name, "MIPS-16") == 0)
df58fc94
RS
15078 {
15079 if (mips_opts.micromips == 1)
15080 as_fatal (_("`mips16' cannot be used with `micromips'"));
15081 mips_opts.mips16 = 1;
15082 }
252b5132
RH
15083 else if (strcmp (name, "nomips16") == 0
15084 || strcmp (name, "noMIPS-16") == 0)
15085 mips_opts.mips16 = 0;
df58fc94
RS
15086 else if (strcmp (name, "micromips") == 0)
15087 {
15088 if (mips_opts.mips16 == 1)
15089 as_fatal (_("`micromips' cannot be used with `mips16'"));
15090 mips_opts.micromips = 1;
15091 }
15092 else if (strcmp (name, "nomicromips") == 0)
15093 mips_opts.micromips = 0;
c6278170
RS
15094 else if (name[0] == 'n'
15095 && name[1] == 'o'
15096 && (ase = mips_lookup_ase (name + 2)))
15097 mips_set_ase (ase, FALSE);
15098 else if ((ase = mips_lookup_ase (name)))
15099 mips_set_ase (ase, TRUE);
1a2c1fad 15100 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 15101 {
af7ee8bf 15102 int reset = 0;
252b5132 15103
1a2c1fad
CD
15104 /* Permit the user to change the ISA and architecture on the fly.
15105 Needless to say, misuse can cause serious problems. */
81a21e38 15106 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
af7ee8bf
CD
15107 {
15108 reset = 1;
0b35dfee 15109 mips_opts.isa = file_mips_opts.isa;
15110 mips_opts.arch = file_mips_opts.arch;
1a2c1fad
CD
15111 }
15112 else if (strncmp (name, "arch=", 5) == 0)
15113 {
15114 const struct mips_cpu_info *p;
15115
15116 p = mips_parse_cpu("internal use", name + 5);
15117 if (!p)
15118 as_bad (_("unknown architecture %s"), name + 5);
15119 else
15120 {
15121 mips_opts.arch = p->cpu;
15122 mips_opts.isa = p->isa;
15123 }
15124 }
81a21e38
TS
15125 else if (strncmp (name, "mips", 4) == 0)
15126 {
15127 const struct mips_cpu_info *p;
15128
15129 p = mips_parse_cpu("internal use", name);
15130 if (!p)
15131 as_bad (_("unknown ISA level %s"), name + 4);
15132 else
15133 {
15134 mips_opts.arch = p->cpu;
15135 mips_opts.isa = p->isa;
15136 }
15137 }
af7ee8bf 15138 else
81a21e38 15139 as_bad (_("unknown ISA or architecture %s"), name);
af7ee8bf
CD
15140
15141 switch (mips_opts.isa)
98d3f06f
KH
15142 {
15143 case 0:
98d3f06f 15144 break;
af7ee8bf
CD
15145 case ISA_MIPS1:
15146 case ISA_MIPS2:
15147 case ISA_MIPS32:
15148 case ISA_MIPS32R2:
ae52f483
AB
15149 case ISA_MIPS32R3:
15150 case ISA_MIPS32R5:
98d3f06f 15151 mips_opts.gp32 = 1;
0b35dfee 15152 mips_opts.fp = 32;
98d3f06f 15153 break;
af7ee8bf
CD
15154 case ISA_MIPS3:
15155 case ISA_MIPS4:
15156 case ISA_MIPS5:
15157 case ISA_MIPS64:
5f74bc13 15158 case ISA_MIPS64R2:
ae52f483
AB
15159 case ISA_MIPS64R3:
15160 case ISA_MIPS64R5:
98d3f06f 15161 mips_opts.gp32 = 0;
e407c74b
NC
15162 if (mips_opts.arch == CPU_R5900)
15163 {
0b35dfee 15164 mips_opts.fp = 32;
e407c74b
NC
15165 }
15166 else
15167 {
0b35dfee 15168 mips_opts.fp = 64;
e407c74b 15169 }
98d3f06f
KH
15170 break;
15171 default:
15172 as_bad (_("unknown ISA level %s"), name + 4);
15173 break;
15174 }
af7ee8bf 15175 if (reset)
98d3f06f 15176 {
0b35dfee 15177 mips_opts.gp32 = file_mips_opts.gp32;
15178 mips_opts.fp = file_mips_opts.fp;
98d3f06f 15179 }
252b5132
RH
15180 }
15181 else if (strcmp (name, "autoextend") == 0)
15182 mips_opts.noautoextend = 0;
15183 else if (strcmp (name, "noautoextend") == 0)
15184 mips_opts.noautoextend = 1;
833794fc
MR
15185 else if (strcmp (name, "insn32") == 0)
15186 mips_opts.insn32 = TRUE;
15187 else if (strcmp (name, "noinsn32") == 0)
15188 mips_opts.insn32 = FALSE;
252b5132
RH
15189 else if (strcmp (name, "push") == 0)
15190 {
15191 struct mips_option_stack *s;
15192
15193 s = (struct mips_option_stack *) xmalloc (sizeof *s);
15194 s->next = mips_opts_stack;
15195 s->options = mips_opts;
15196 mips_opts_stack = s;
15197 }
15198 else if (strcmp (name, "pop") == 0)
15199 {
15200 struct mips_option_stack *s;
15201
15202 s = mips_opts_stack;
15203 if (s == NULL)
15204 as_bad (_(".set pop with no .set push"));
15205 else
15206 {
15207 /* If we're changing the reorder mode we need to handle
15208 delay slots correctly. */
15209 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 15210 start_noreorder ();
252b5132 15211 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 15212 end_noreorder ();
252b5132
RH
15213
15214 mips_opts = s->options;
15215 mips_opts_stack = s->next;
15216 free (s);
15217 }
15218 }
aed1a261
RS
15219 else if (strcmp (name, "sym32") == 0)
15220 mips_opts.sym32 = TRUE;
15221 else if (strcmp (name, "nosym32") == 0)
15222 mips_opts.sym32 = FALSE;
e6559e01
JM
15223 else if (strchr (name, ','))
15224 {
15225 /* Generic ".set" directive; use the generic handler. */
15226 *input_line_pointer = ch;
15227 input_line_pointer = name;
15228 s_set (0);
15229 return;
15230 }
252b5132
RH
15231 else
15232 {
1661c76c 15233 as_warn (_("tried to set unrecognized symbol: %s\n"), name);
252b5132 15234 }
c6278170 15235 mips_check_isa_supports_ases ();
252b5132
RH
15236 *input_line_pointer = ch;
15237 demand_empty_rest_of_line ();
15238}
15239
15240/* Handle the .abicalls pseudo-op. I believe this is equivalent to
15241 .option pic2. It means to generate SVR4 PIC calls. */
15242
15243static void
17a2f251 15244s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
15245{
15246 mips_pic = SVR4_PIC;
143d77c5 15247 mips_abicalls = TRUE;
4d0d148d
TS
15248
15249 if (g_switch_seen && g_switch_value != 0)
15250 as_warn (_("-G may not be used with SVR4 PIC code"));
15251 g_switch_value = 0;
15252
252b5132
RH
15253 bfd_set_gp_size (stdoutput, 0);
15254 demand_empty_rest_of_line ();
15255}
15256
15257/* Handle the .cpload pseudo-op. This is used when generating SVR4
15258 PIC code. It sets the $gp register for the function based on the
15259 function address, which is in the register named in the argument.
15260 This uses a relocation against _gp_disp, which is handled specially
15261 by the linker. The result is:
15262 lui $gp,%hi(_gp_disp)
15263 addiu $gp,$gp,%lo(_gp_disp)
15264 addu $gp,$gp,.cpload argument
aa6975fb
ILT
15265 The .cpload argument is normally $25 == $t9.
15266
15267 The -mno-shared option changes this to:
bbe506e8
TS
15268 lui $gp,%hi(__gnu_local_gp)
15269 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
15270 and the argument is ignored. This saves an instruction, but the
15271 resulting code is not position independent; it uses an absolute
bbe506e8
TS
15272 address for __gnu_local_gp. Thus code assembled with -mno-shared
15273 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
15274
15275static void
17a2f251 15276s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
15277{
15278 expressionS ex;
aa6975fb
ILT
15279 int reg;
15280 int in_shared;
252b5132 15281
6478892d
TS
15282 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
15283 .cpload is ignored. */
15284 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
15285 {
15286 s_ignore (0);
15287 return;
15288 }
15289
a276b80c
MR
15290 if (mips_opts.mips16)
15291 {
15292 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
15293 ignore_rest_of_line ();
15294 return;
15295 }
15296
d3ecfc59 15297 /* .cpload should be in a .set noreorder section. */
252b5132
RH
15298 if (mips_opts.noreorder == 0)
15299 as_warn (_(".cpload not in noreorder section"));
15300
aa6975fb
ILT
15301 reg = tc_get_register (0);
15302
15303 /* If we need to produce a 64-bit address, we are better off using
15304 the default instruction sequence. */
aed1a261 15305 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 15306
252b5132 15307 ex.X_op = O_symbol;
bbe506e8
TS
15308 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
15309 "__gnu_local_gp");
252b5132
RH
15310 ex.X_op_symbol = NULL;
15311 ex.X_add_number = 0;
15312
15313 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 15314 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 15315
8a75745d
MR
15316 mips_mark_labels ();
15317 mips_assembling_insn = TRUE;
15318
584892a6 15319 macro_start ();
67c0d1eb
RS
15320 macro_build_lui (&ex, mips_gp_register);
15321 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 15322 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
15323 if (in_shared)
15324 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
15325 mips_gp_register, reg);
584892a6 15326 macro_end ();
252b5132 15327
8a75745d 15328 mips_assembling_insn = FALSE;
252b5132
RH
15329 demand_empty_rest_of_line ();
15330}
15331
6478892d
TS
15332/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
15333 .cpsetup $reg1, offset|$reg2, label
15334
15335 If offset is given, this results in:
15336 sd $gp, offset($sp)
956cd1d6 15337 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
15338 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
15339 daddu $gp, $gp, $reg1
6478892d
TS
15340
15341 If $reg2 is given, this results in:
15342 daddu $reg2, $gp, $0
956cd1d6 15343 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
15344 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
15345 daddu $gp, $gp, $reg1
aa6975fb
ILT
15346 $reg1 is normally $25 == $t9.
15347
15348 The -mno-shared option replaces the last three instructions with
15349 lui $gp,%hi(_gp)
54f4ddb3 15350 addiu $gp,$gp,%lo(_gp) */
aa6975fb 15351
6478892d 15352static void
17a2f251 15353s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
15354{
15355 expressionS ex_off;
15356 expressionS ex_sym;
15357 int reg1;
6478892d 15358
8586fc66 15359 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
15360 We also need NewABI support. */
15361 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
15362 {
15363 s_ignore (0);
15364 return;
15365 }
15366
a276b80c
MR
15367 if (mips_opts.mips16)
15368 {
15369 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
15370 ignore_rest_of_line ();
15371 return;
15372 }
15373
6478892d
TS
15374 reg1 = tc_get_register (0);
15375 SKIP_WHITESPACE ();
15376 if (*input_line_pointer != ',')
15377 {
15378 as_bad (_("missing argument separator ',' for .cpsetup"));
15379 return;
15380 }
15381 else
80245285 15382 ++input_line_pointer;
6478892d
TS
15383 SKIP_WHITESPACE ();
15384 if (*input_line_pointer == '$')
80245285
TS
15385 {
15386 mips_cpreturn_register = tc_get_register (0);
15387 mips_cpreturn_offset = -1;
15388 }
6478892d 15389 else
80245285
TS
15390 {
15391 mips_cpreturn_offset = get_absolute_expression ();
15392 mips_cpreturn_register = -1;
15393 }
6478892d
TS
15394 SKIP_WHITESPACE ();
15395 if (*input_line_pointer != ',')
15396 {
15397 as_bad (_("missing argument separator ',' for .cpsetup"));
15398 return;
15399 }
15400 else
f9419b05 15401 ++input_line_pointer;
6478892d 15402 SKIP_WHITESPACE ();
f21f8242 15403 expression (&ex_sym);
6478892d 15404
8a75745d
MR
15405 mips_mark_labels ();
15406 mips_assembling_insn = TRUE;
15407
584892a6 15408 macro_start ();
6478892d
TS
15409 if (mips_cpreturn_register == -1)
15410 {
15411 ex_off.X_op = O_constant;
15412 ex_off.X_add_symbol = NULL;
15413 ex_off.X_op_symbol = NULL;
15414 ex_off.X_add_number = mips_cpreturn_offset;
15415
67c0d1eb 15416 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 15417 BFD_RELOC_LO16, SP);
6478892d
TS
15418 }
15419 else
67c0d1eb 15420 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
17a2f251 15421 mips_gp_register, 0);
6478892d 15422
aed1a261 15423 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb 15424 {
df58fc94 15425 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
aa6975fb
ILT
15426 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
15427 BFD_RELOC_HI16_S);
15428
15429 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
15430 mips_gp_register, -1, BFD_RELOC_GPREL16,
15431 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
15432
15433 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
15434 mips_gp_register, reg1);
15435 }
15436 else
15437 {
15438 expressionS ex;
15439
15440 ex.X_op = O_symbol;
4184909a 15441 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
15442 ex.X_op_symbol = NULL;
15443 ex.X_add_number = 0;
6e1304d8 15444
aa6975fb
ILT
15445 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
15446 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
15447
15448 macro_build_lui (&ex, mips_gp_register);
15449 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
15450 mips_gp_register, BFD_RELOC_LO16);
15451 }
f21f8242 15452
584892a6 15453 macro_end ();
6478892d 15454
8a75745d 15455 mips_assembling_insn = FALSE;
6478892d
TS
15456 demand_empty_rest_of_line ();
15457}
15458
15459static void
17a2f251 15460s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
15461{
15462 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
54f4ddb3 15463 .cplocal is ignored. */
6478892d
TS
15464 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
15465 {
15466 s_ignore (0);
15467 return;
15468 }
15469
a276b80c
MR
15470 if (mips_opts.mips16)
15471 {
15472 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
15473 ignore_rest_of_line ();
15474 return;
15475 }
15476
6478892d 15477 mips_gp_register = tc_get_register (0);
85b51719 15478 demand_empty_rest_of_line ();
6478892d
TS
15479}
15480
252b5132
RH
15481/* Handle the .cprestore pseudo-op. This stores $gp into a given
15482 offset from $sp. The offset is remembered, and after making a PIC
15483 call $gp is restored from that location. */
15484
15485static void
17a2f251 15486s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
15487{
15488 expressionS ex;
252b5132 15489
6478892d 15490 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 15491 .cprestore is ignored. */
6478892d 15492 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
15493 {
15494 s_ignore (0);
15495 return;
15496 }
15497
a276b80c
MR
15498 if (mips_opts.mips16)
15499 {
15500 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
15501 ignore_rest_of_line ();
15502 return;
15503 }
15504
252b5132 15505 mips_cprestore_offset = get_absolute_expression ();
7a621144 15506 mips_cprestore_valid = 1;
252b5132
RH
15507
15508 ex.X_op = O_constant;
15509 ex.X_add_symbol = NULL;
15510 ex.X_op_symbol = NULL;
15511 ex.X_add_number = mips_cprestore_offset;
15512
8a75745d
MR
15513 mips_mark_labels ();
15514 mips_assembling_insn = TRUE;
15515
584892a6 15516 macro_start ();
67c0d1eb
RS
15517 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
15518 SP, HAVE_64BIT_ADDRESSES);
584892a6 15519 macro_end ();
252b5132 15520
8a75745d 15521 mips_assembling_insn = FALSE;
252b5132
RH
15522 demand_empty_rest_of_line ();
15523}
15524
6478892d 15525/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 15526 was given in the preceding .cpsetup, it results in:
6478892d 15527 ld $gp, offset($sp)
76b3015f 15528
6478892d 15529 If a register $reg2 was given there, it results in:
54f4ddb3
TS
15530 daddu $gp, $reg2, $0 */
15531
6478892d 15532static void
17a2f251 15533s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
15534{
15535 expressionS ex;
6478892d
TS
15536
15537 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
15538 We also need NewABI support. */
15539 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
15540 {
15541 s_ignore (0);
15542 return;
15543 }
15544
a276b80c
MR
15545 if (mips_opts.mips16)
15546 {
15547 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
15548 ignore_rest_of_line ();
15549 return;
15550 }
15551
8a75745d
MR
15552 mips_mark_labels ();
15553 mips_assembling_insn = TRUE;
15554
584892a6 15555 macro_start ();
6478892d
TS
15556 if (mips_cpreturn_register == -1)
15557 {
15558 ex.X_op = O_constant;
15559 ex.X_add_symbol = NULL;
15560 ex.X_op_symbol = NULL;
15561 ex.X_add_number = mips_cpreturn_offset;
15562
67c0d1eb 15563 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
15564 }
15565 else
67c0d1eb 15566 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
17a2f251 15567 mips_cpreturn_register, 0);
584892a6 15568 macro_end ();
6478892d 15569
8a75745d 15570 mips_assembling_insn = FALSE;
6478892d
TS
15571 demand_empty_rest_of_line ();
15572}
15573
d0f13682
CLT
15574/* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
15575 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
15576 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
15577 debug information or MIPS16 TLS. */
741d6ea8
JM
15578
15579static void
d0f13682
CLT
15580s_tls_rel_directive (const size_t bytes, const char *dirstr,
15581 bfd_reloc_code_real_type rtype)
741d6ea8
JM
15582{
15583 expressionS ex;
15584 char *p;
15585
15586 expression (&ex);
15587
15588 if (ex.X_op != O_symbol)
15589 {
1661c76c 15590 as_bad (_("unsupported use of %s"), dirstr);
741d6ea8
JM
15591 ignore_rest_of_line ();
15592 }
15593
15594 p = frag_more (bytes);
15595 md_number_to_chars (p, 0, bytes);
d0f13682 15596 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
741d6ea8 15597 demand_empty_rest_of_line ();
de64cffd 15598 mips_clear_insn_labels ();
741d6ea8
JM
15599}
15600
15601/* Handle .dtprelword. */
15602
15603static void
15604s_dtprelword (int ignore ATTRIBUTE_UNUSED)
15605{
d0f13682 15606 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
741d6ea8
JM
15607}
15608
15609/* Handle .dtpreldword. */
15610
15611static void
15612s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
15613{
d0f13682
CLT
15614 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
15615}
15616
15617/* Handle .tprelword. */
15618
15619static void
15620s_tprelword (int ignore ATTRIBUTE_UNUSED)
15621{
15622 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
15623}
15624
15625/* Handle .tpreldword. */
15626
15627static void
15628s_tpreldword (int ignore ATTRIBUTE_UNUSED)
15629{
15630 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
741d6ea8
JM
15631}
15632
6478892d
TS
15633/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
15634 code. It sets the offset to use in gp_rel relocations. */
15635
15636static void
17a2f251 15637s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
15638{
15639 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
15640 We also need NewABI support. */
15641 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
15642 {
15643 s_ignore (0);
15644 return;
15645 }
15646
def2e0dd 15647 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
15648
15649 demand_empty_rest_of_line ();
15650}
15651
252b5132
RH
15652/* Handle the .gpword pseudo-op. This is used when generating PIC
15653 code. It generates a 32 bit GP relative reloc. */
15654
15655static void
17a2f251 15656s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132 15657{
a8dbcb85
TS
15658 segment_info_type *si;
15659 struct insn_label_list *l;
252b5132
RH
15660 expressionS ex;
15661 char *p;
15662
15663 /* When not generating PIC code, this is treated as .word. */
15664 if (mips_pic != SVR4_PIC)
15665 {
15666 s_cons (2);
15667 return;
15668 }
15669
a8dbcb85
TS
15670 si = seg_info (now_seg);
15671 l = si->label_list;
7d10b47d 15672 mips_emit_delays ();
252b5132 15673 if (auto_align)
462427c4 15674 mips_align (2, 0, l);
252b5132
RH
15675
15676 expression (&ex);
a1facbec 15677 mips_clear_insn_labels ();
252b5132
RH
15678
15679 if (ex.X_op != O_symbol || ex.X_add_number != 0)
15680 {
1661c76c 15681 as_bad (_("unsupported use of .gpword"));
252b5132
RH
15682 ignore_rest_of_line ();
15683 }
15684
15685 p = frag_more (4);
17a2f251 15686 md_number_to_chars (p, 0, 4);
b34976b6 15687 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 15688 BFD_RELOC_GPREL32);
252b5132
RH
15689
15690 demand_empty_rest_of_line ();
15691}
15692
10181a0d 15693static void
17a2f251 15694s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d 15695{
a8dbcb85
TS
15696 segment_info_type *si;
15697 struct insn_label_list *l;
10181a0d
AO
15698 expressionS ex;
15699 char *p;
15700
15701 /* When not generating PIC code, this is treated as .dword. */
15702 if (mips_pic != SVR4_PIC)
15703 {
15704 s_cons (3);
15705 return;
15706 }
15707
a8dbcb85
TS
15708 si = seg_info (now_seg);
15709 l = si->label_list;
7d10b47d 15710 mips_emit_delays ();
10181a0d 15711 if (auto_align)
462427c4 15712 mips_align (3, 0, l);
10181a0d
AO
15713
15714 expression (&ex);
a1facbec 15715 mips_clear_insn_labels ();
10181a0d
AO
15716
15717 if (ex.X_op != O_symbol || ex.X_add_number != 0)
15718 {
1661c76c 15719 as_bad (_("unsupported use of .gpdword"));
10181a0d
AO
15720 ignore_rest_of_line ();
15721 }
15722
15723 p = frag_more (8);
17a2f251 15724 md_number_to_chars (p, 0, 8);
a105a300 15725 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
6e1304d8 15726 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
15727
15728 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8
RS
15729 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
15730 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
15731
15732 demand_empty_rest_of_line ();
15733}
15734
a3f278e2
CM
15735/* Handle the .ehword pseudo-op. This is used when generating unwinding
15736 tables. It generates a R_MIPS_EH reloc. */
15737
15738static void
15739s_ehword (int ignore ATTRIBUTE_UNUSED)
15740{
15741 expressionS ex;
15742 char *p;
15743
15744 mips_emit_delays ();
15745
15746 expression (&ex);
15747 mips_clear_insn_labels ();
15748
15749 if (ex.X_op != O_symbol || ex.X_add_number != 0)
15750 {
1661c76c 15751 as_bad (_("unsupported use of .ehword"));
a3f278e2
CM
15752 ignore_rest_of_line ();
15753 }
15754
15755 p = frag_more (4);
15756 md_number_to_chars (p, 0, 4);
15757 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
15758 BFD_RELOC_MIPS_EH);
15759
15760 demand_empty_rest_of_line ();
15761}
15762
252b5132
RH
15763/* Handle the .cpadd pseudo-op. This is used when dealing with switch
15764 tables in SVR4 PIC code. */
15765
15766static void
17a2f251 15767s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 15768{
252b5132
RH
15769 int reg;
15770
10181a0d
AO
15771 /* This is ignored when not generating SVR4 PIC code. */
15772 if (mips_pic != SVR4_PIC)
252b5132
RH
15773 {
15774 s_ignore (0);
15775 return;
15776 }
15777
8a75745d
MR
15778 mips_mark_labels ();
15779 mips_assembling_insn = TRUE;
15780
252b5132 15781 /* Add $gp to the register named as an argument. */
584892a6 15782 macro_start ();
252b5132 15783 reg = tc_get_register (0);
67c0d1eb 15784 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 15785 macro_end ();
252b5132 15786
8a75745d 15787 mips_assembling_insn = FALSE;
bdaaa2e1 15788 demand_empty_rest_of_line ();
252b5132
RH
15789}
15790
15791/* Handle the .insn pseudo-op. This marks instruction labels in
df58fc94 15792 mips16/micromips mode. This permits the linker to handle them specially,
252b5132
RH
15793 such as generating jalx instructions when needed. We also make
15794 them odd for the duration of the assembly, in order to generate the
15795 right sort of code. We will make them even in the adjust_symtab
15796 routine, while leaving them marked. This is convenient for the
15797 debugger and the disassembler. The linker knows to make them odd
15798 again. */
15799
15800static void
17a2f251 15801s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 15802{
df58fc94 15803 mips_mark_labels ();
252b5132
RH
15804
15805 demand_empty_rest_of_line ();
15806}
15807
ba92f887
MR
15808/* Handle the .nan pseudo-op. */
15809
15810static void
15811s_nan (int ignore ATTRIBUTE_UNUSED)
15812{
15813 static const char str_legacy[] = "legacy";
15814 static const char str_2008[] = "2008";
15815 size_t i;
15816
15817 for (i = 0; !is_end_of_line[(unsigned char) input_line_pointer[i]]; i++);
15818
15819 if (i == sizeof (str_2008) - 1
15820 && memcmp (input_line_pointer, str_2008, i) == 0)
15821 mips_flag_nan2008 = TRUE;
15822 else if (i == sizeof (str_legacy) - 1
15823 && memcmp (input_line_pointer, str_legacy, i) == 0)
15824 mips_flag_nan2008 = FALSE;
15825 else
1661c76c 15826 as_bad (_("bad .nan directive"));
ba92f887
MR
15827
15828 input_line_pointer += i;
15829 demand_empty_rest_of_line ();
15830}
15831
754e2bb9
RS
15832/* Handle a .stab[snd] directive. Ideally these directives would be
15833 implemented in a transparent way, so that removing them would not
15834 have any effect on the generated instructions. However, s_stab
15835 internally changes the section, so in practice we need to decide
15836 now whether the preceding label marks compressed code. We do not
15837 support changing the compression mode of a label after a .stab*
15838 directive, such as in:
15839
15840 foo:
15841 .stabs ...
15842 .set mips16
15843
15844 so the current mode wins. */
252b5132
RH
15845
15846static void
17a2f251 15847s_mips_stab (int type)
252b5132 15848{
754e2bb9 15849 mips_mark_labels ();
252b5132
RH
15850 s_stab (type);
15851}
15852
54f4ddb3 15853/* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
252b5132
RH
15854
15855static void
17a2f251 15856s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
15857{
15858 char *name;
15859 int c;
15860 symbolS *symbolP;
15861 expressionS exp;
15862
15863 name = input_line_pointer;
15864 c = get_symbol_end ();
15865 symbolP = symbol_find_or_make (name);
15866 S_SET_WEAK (symbolP);
15867 *input_line_pointer = c;
15868
15869 SKIP_WHITESPACE ();
15870
15871 if (! is_end_of_line[(unsigned char) *input_line_pointer])
15872 {
15873 if (S_IS_DEFINED (symbolP))
15874 {
20203fb9 15875 as_bad (_("ignoring attempt to redefine symbol %s"),
252b5132
RH
15876 S_GET_NAME (symbolP));
15877 ignore_rest_of_line ();
15878 return;
15879 }
bdaaa2e1 15880
252b5132
RH
15881 if (*input_line_pointer == ',')
15882 {
15883 ++input_line_pointer;
15884 SKIP_WHITESPACE ();
15885 }
bdaaa2e1 15886
252b5132
RH
15887 expression (&exp);
15888 if (exp.X_op != O_symbol)
15889 {
20203fb9 15890 as_bad (_("bad .weakext directive"));
98d3f06f 15891 ignore_rest_of_line ();
252b5132
RH
15892 return;
15893 }
49309057 15894 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
15895 }
15896
15897 demand_empty_rest_of_line ();
15898}
15899
15900/* Parse a register string into a number. Called from the ECOFF code
15901 to parse .frame. The argument is non-zero if this is the frame
15902 register, so that we can record it in mips_frame_reg. */
15903
15904int
17a2f251 15905tc_get_register (int frame)
252b5132 15906{
707bfff6 15907 unsigned int reg;
252b5132
RH
15908
15909 SKIP_WHITESPACE ();
707bfff6
TS
15910 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
15911 reg = 0;
252b5132 15912 if (frame)
7a621144
DJ
15913 {
15914 mips_frame_reg = reg != 0 ? reg : SP;
15915 mips_frame_reg_valid = 1;
15916 mips_cprestore_valid = 0;
15917 }
252b5132
RH
15918 return reg;
15919}
15920
15921valueT
17a2f251 15922md_section_align (asection *seg, valueT addr)
252b5132
RH
15923{
15924 int align = bfd_get_section_alignment (stdoutput, seg);
15925
f3ded42a
RS
15926 /* We don't need to align ELF sections to the full alignment.
15927 However, Irix 5 may prefer that we align them at least to a 16
15928 byte boundary. We don't bother to align the sections if we
15929 are targeted for an embedded system. */
15930 if (strncmp (TARGET_OS, "elf", 3) == 0)
15931 return addr;
15932 if (align > 4)
15933 align = 4;
252b5132
RH
15934
15935 return ((addr + (1 << align) - 1) & (-1 << align));
15936}
15937
15938/* Utility routine, called from above as well. If called while the
15939 input file is still being read, it's only an approximation. (For
15940 example, a symbol may later become defined which appeared to be
15941 undefined earlier.) */
15942
15943static int
17a2f251 15944nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
15945{
15946 if (sym == 0)
15947 return 0;
15948
4d0d148d 15949 if (g_switch_value > 0)
252b5132
RH
15950 {
15951 const char *symname;
15952 int change;
15953
c9914766 15954 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
15955 register. It can be if it is smaller than the -G size or if
15956 it is in the .sdata or .sbss section. Certain symbols can
c9914766 15957 not be referenced off the $gp, although it appears as though
252b5132
RH
15958 they can. */
15959 symname = S_GET_NAME (sym);
15960 if (symname != (const char *) NULL
15961 && (strcmp (symname, "eprol") == 0
15962 || strcmp (symname, "etext") == 0
15963 || strcmp (symname, "_gp") == 0
15964 || strcmp (symname, "edata") == 0
15965 || strcmp (symname, "_fbss") == 0
15966 || strcmp (symname, "_fdata") == 0
15967 || strcmp (symname, "_ftext") == 0
15968 || strcmp (symname, "end") == 0
15969 || strcmp (symname, "_gp_disp") == 0))
15970 change = 1;
15971 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
15972 && (0
15973#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
15974 || (symbol_get_obj (sym)->ecoff_extern_size != 0
15975 && (symbol_get_obj (sym)->ecoff_extern_size
15976 <= g_switch_value))
252b5132
RH
15977#endif
15978 /* We must defer this decision until after the whole
15979 file has been read, since there might be a .extern
15980 after the first use of this symbol. */
15981 || (before_relaxing
15982#ifndef NO_ECOFF_DEBUGGING
49309057 15983 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
15984#endif
15985 && S_GET_VALUE (sym) == 0)
15986 || (S_GET_VALUE (sym) != 0
15987 && S_GET_VALUE (sym) <= g_switch_value)))
15988 change = 0;
15989 else
15990 {
15991 const char *segname;
15992
15993 segname = segment_name (S_GET_SEGMENT (sym));
9c2799c2 15994 gas_assert (strcmp (segname, ".lit8") != 0
252b5132
RH
15995 && strcmp (segname, ".lit4") != 0);
15996 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
15997 && strcmp (segname, ".sbss") != 0
15998 && strncmp (segname, ".sdata.", 7) != 0
d4dc2f22
TS
15999 && strncmp (segname, ".sbss.", 6) != 0
16000 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
fba2b7f9 16001 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
16002 }
16003 return change;
16004 }
16005 else
c9914766 16006 /* We are not optimizing for the $gp register. */
252b5132
RH
16007 return 1;
16008}
16009
5919d012
RS
16010
16011/* Return true if the given symbol should be considered local for SVR4 PIC. */
16012
16013static bfd_boolean
17a2f251 16014pic_need_relax (symbolS *sym, asection *segtype)
5919d012
RS
16015{
16016 asection *symsec;
5919d012
RS
16017
16018 /* Handle the case of a symbol equated to another symbol. */
16019 while (symbol_equated_reloc_p (sym))
16020 {
16021 symbolS *n;
16022
5f0fe04b 16023 /* It's possible to get a loop here in a badly written program. */
5919d012
RS
16024 n = symbol_get_value_expression (sym)->X_add_symbol;
16025 if (n == sym)
16026 break;
16027 sym = n;
16028 }
16029
df1f3cda
DD
16030 if (symbol_section_p (sym))
16031 return TRUE;
16032
5919d012
RS
16033 symsec = S_GET_SEGMENT (sym);
16034
5919d012 16035 /* This must duplicate the test in adjust_reloc_syms. */
45dfa85a
AM
16036 return (!bfd_is_und_section (symsec)
16037 && !bfd_is_abs_section (symsec)
5f0fe04b
TS
16038 && !bfd_is_com_section (symsec)
16039 && !s_is_linkonce (sym, segtype)
5919d012 16040 /* A global or weak symbol is treated as external. */
f3ded42a 16041 && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
5919d012
RS
16042}
16043
16044
252b5132
RH
16045/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
16046 extended opcode. SEC is the section the frag is in. */
16047
16048static int
17a2f251 16049mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132
RH
16050{
16051 int type;
3ccad066 16052 const struct mips_int_operand *operand;
252b5132 16053 offsetT val;
252b5132 16054 segT symsec;
98aa84af 16055 fragS *sym_frag;
252b5132
RH
16056
16057 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
16058 return 0;
16059 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
16060 return 1;
16061
16062 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
3ccad066 16063 operand = mips16_immed_operand (type, FALSE);
252b5132 16064
98aa84af 16065 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 16066 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 16067 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132 16068
3ccad066 16069 if (operand->root.type == OP_PCREL)
252b5132 16070 {
3ccad066 16071 const struct mips_pcrel_operand *pcrel_op;
252b5132 16072 addressT addr;
3ccad066 16073 offsetT maxtiny;
252b5132
RH
16074
16075 /* We won't have the section when we are called from
16076 mips_relax_frag. However, we will always have been called
16077 from md_estimate_size_before_relax first. If this is a
16078 branch to a different section, we mark it as such. If SEC is
16079 NULL, and the frag is not marked, then it must be a branch to
16080 the same section. */
3ccad066 16081 pcrel_op = (const struct mips_pcrel_operand *) operand;
252b5132
RH
16082 if (sec == NULL)
16083 {
16084 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
16085 return 1;
16086 }
16087 else
16088 {
98aa84af 16089 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
16090 if (symsec != sec)
16091 {
16092 fragp->fr_subtype =
16093 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
16094
16095 /* FIXME: We should support this, and let the linker
16096 catch branches and loads that are out of range. */
16097 as_bad_where (fragp->fr_file, fragp->fr_line,
16098 _("unsupported PC relative reference to different section"));
16099
16100 return 1;
16101 }
98aa84af
AM
16102 if (fragp != sym_frag && sym_frag->fr_address == 0)
16103 /* Assume non-extended on the first relaxation pass.
16104 The address we have calculated will be bogus if this is
16105 a forward branch to another frag, as the forward frag
16106 will have fr_address == 0. */
16107 return 0;
252b5132
RH
16108 }
16109
16110 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
16111 the same section. If the relax_marker of the symbol fragment
16112 differs from the relax_marker of this fragment, we have not
16113 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
16114 in STRETCH in order to get a better estimate of the address.
16115 This particularly matters because of the shift bits. */
16116 if (stretch != 0
98aa84af 16117 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
16118 {
16119 fragS *f;
16120
16121 /* Adjust stretch for any alignment frag. Note that if have
16122 been expanding the earlier code, the symbol may be
16123 defined in what appears to be an earlier frag. FIXME:
16124 This doesn't handle the fr_subtype field, which specifies
16125 a maximum number of bytes to skip when doing an
16126 alignment. */
98aa84af 16127 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
16128 {
16129 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
16130 {
16131 if (stretch < 0)
16132 stretch = - ((- stretch)
16133 & ~ ((1 << (int) f->fr_offset) - 1));
16134 else
16135 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
16136 if (stretch == 0)
16137 break;
16138 }
16139 }
16140 if (f != NULL)
16141 val += stretch;
16142 }
16143
16144 addr = fragp->fr_address + fragp->fr_fix;
16145
16146 /* The base address rules are complicated. The base address of
16147 a branch is the following instruction. The base address of a
16148 PC relative load or add is the instruction itself, but if it
16149 is in a delay slot (in which case it can not be extended) use
16150 the address of the instruction whose delay slot it is in. */
3ccad066 16151 if (pcrel_op->include_isa_bit)
252b5132
RH
16152 {
16153 addr += 2;
16154
16155 /* If we are currently assuming that this frag should be
16156 extended, then, the current address is two bytes
bdaaa2e1 16157 higher. */
252b5132
RH
16158 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
16159 addr += 2;
16160
16161 /* Ignore the low bit in the target, since it will be set
16162 for a text label. */
3ccad066 16163 val &= -2;
252b5132
RH
16164 }
16165 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
16166 addr -= 4;
16167 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
16168 addr -= 2;
16169
3ccad066 16170 val -= addr & -(1 << pcrel_op->align_log2);
252b5132
RH
16171
16172 /* If any of the shifted bits are set, we must use an extended
16173 opcode. If the address depends on the size of this
16174 instruction, this can lead to a loop, so we arrange to always
16175 use an extended opcode. We only check this when we are in
16176 the main relaxation loop, when SEC is NULL. */
3ccad066 16177 if ((val & ((1 << operand->shift) - 1)) != 0 && sec == NULL)
252b5132
RH
16178 {
16179 fragp->fr_subtype =
16180 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
16181 return 1;
16182 }
16183
16184 /* If we are about to mark a frag as extended because the value
3ccad066
RS
16185 is precisely the next value above maxtiny, then there is a
16186 chance of an infinite loop as in the following code:
252b5132
RH
16187 la $4,foo
16188 .skip 1020
16189 .align 2
16190 foo:
16191 In this case when the la is extended, foo is 0x3fc bytes
16192 away, so the la can be shrunk, but then foo is 0x400 away, so
16193 the la must be extended. To avoid this loop, we mark the
16194 frag as extended if it was small, and is about to become
3ccad066
RS
16195 extended with the next value above maxtiny. */
16196 maxtiny = mips_int_operand_max (operand);
16197 if (val == maxtiny + (1 << operand->shift)
252b5132
RH
16198 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
16199 && sec == NULL)
16200 {
16201 fragp->fr_subtype =
16202 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
16203 return 1;
16204 }
16205 }
16206 else if (symsec != absolute_section && sec != NULL)
16207 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
16208
3ccad066 16209 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
252b5132
RH
16210}
16211
4a6a3df4
AO
16212/* Compute the length of a branch sequence, and adjust the
16213 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
16214 worst-case length is computed, with UPDATE being used to indicate
16215 whether an unconditional (-1), branch-likely (+1) or regular (0)
16216 branch is to be computed. */
16217static int
17a2f251 16218relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 16219{
b34976b6 16220 bfd_boolean toofar;
4a6a3df4
AO
16221 int length;
16222
16223 if (fragp
16224 && S_IS_DEFINED (fragp->fr_symbol)
16225 && sec == S_GET_SEGMENT (fragp->fr_symbol))
16226 {
16227 addressT addr;
16228 offsetT val;
16229
16230 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
16231
16232 addr = fragp->fr_address + fragp->fr_fix + 4;
16233
16234 val -= addr;
16235
16236 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
16237 }
16238 else if (fragp)
16239 /* If the symbol is not defined or it's in a different segment,
16240 assume the user knows what's going on and emit a short
16241 branch. */
b34976b6 16242 toofar = FALSE;
4a6a3df4 16243 else
b34976b6 16244 toofar = TRUE;
4a6a3df4
AO
16245
16246 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
16247 fragp->fr_subtype
66b3e8da
MR
16248 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
16249 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
16250 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
16251 RELAX_BRANCH_LINK (fragp->fr_subtype),
16252 toofar);
16253
16254 length = 4;
16255 if (toofar)
16256 {
16257 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
16258 length += 8;
16259
16260 if (mips_pic != NO_PIC)
16261 {
16262 /* Additional space for PIC loading of target address. */
16263 length += 8;
16264 if (mips_opts.isa == ISA_MIPS1)
16265 /* Additional space for $at-stabilizing nop. */
16266 length += 4;
16267 }
16268
16269 /* If branch is conditional. */
16270 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
16271 length += 8;
16272 }
b34976b6 16273
4a6a3df4
AO
16274 return length;
16275}
16276
df58fc94
RS
16277/* Compute the length of a branch sequence, and adjust the
16278 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
16279 worst-case length is computed, with UPDATE being used to indicate
16280 whether an unconditional (-1), or regular (0) branch is to be
16281 computed. */
16282
16283static int
16284relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
16285{
16286 bfd_boolean toofar;
16287 int length;
16288
16289 if (fragp
16290 && S_IS_DEFINED (fragp->fr_symbol)
16291 && sec == S_GET_SEGMENT (fragp->fr_symbol))
16292 {
16293 addressT addr;
16294 offsetT val;
16295
16296 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
16297 /* Ignore the low bit in the target, since it will be set
16298 for a text label. */
16299 if ((val & 1) != 0)
16300 --val;
16301
16302 addr = fragp->fr_address + fragp->fr_fix + 4;
16303
16304 val -= addr;
16305
16306 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
16307 }
16308 else if (fragp)
16309 /* If the symbol is not defined or it's in a different segment,
16310 assume the user knows what's going on and emit a short
16311 branch. */
16312 toofar = FALSE;
16313 else
16314 toofar = TRUE;
16315
16316 if (fragp && update
16317 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
16318 fragp->fr_subtype = (toofar
16319 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
16320 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
16321
16322 length = 4;
16323 if (toofar)
16324 {
16325 bfd_boolean compact_known = fragp != NULL;
16326 bfd_boolean compact = FALSE;
16327 bfd_boolean uncond;
16328
16329 if (compact_known)
16330 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
16331 if (fragp)
16332 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
16333 else
16334 uncond = update < 0;
16335
16336 /* If label is out of range, we turn branch <br>:
16337
16338 <br> label # 4 bytes
16339 0:
16340
16341 into:
16342
16343 j label # 4 bytes
16344 nop # 2 bytes if compact && !PIC
16345 0:
16346 */
16347 if (mips_pic == NO_PIC && (!compact_known || compact))
16348 length += 2;
16349
16350 /* If assembling PIC code, we further turn:
16351
16352 j label # 4 bytes
16353
16354 into:
16355
16356 lw/ld at, %got(label)(gp) # 4 bytes
16357 d/addiu at, %lo(label) # 4 bytes
16358 jr/c at # 2 bytes
16359 */
16360 if (mips_pic != NO_PIC)
16361 length += 6;
16362
16363 /* If branch <br> is conditional, we prepend negated branch <brneg>:
16364
16365 <brneg> 0f # 4 bytes
16366 nop # 2 bytes if !compact
16367 */
16368 if (!uncond)
16369 length += (compact_known && compact) ? 4 : 6;
16370 }
16371
16372 return length;
16373}
16374
16375/* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
16376 bit accordingly. */
16377
16378static int
16379relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
16380{
16381 bfd_boolean toofar;
16382
df58fc94
RS
16383 if (fragp
16384 && S_IS_DEFINED (fragp->fr_symbol)
16385 && sec == S_GET_SEGMENT (fragp->fr_symbol))
16386 {
16387 addressT addr;
16388 offsetT val;
16389 int type;
16390
16391 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
16392 /* Ignore the low bit in the target, since it will be set
16393 for a text label. */
16394 if ((val & 1) != 0)
16395 --val;
16396
16397 /* Assume this is a 2-byte branch. */
16398 addr = fragp->fr_address + fragp->fr_fix + 2;
16399
16400 /* We try to avoid the infinite loop by not adding 2 more bytes for
16401 long branches. */
16402
16403 val -= addr;
16404
16405 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
16406 if (type == 'D')
16407 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
16408 else if (type == 'E')
16409 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
16410 else
16411 abort ();
16412 }
16413 else
16414 /* If the symbol is not defined or it's in a different segment,
16415 we emit a normal 32-bit branch. */
16416 toofar = TRUE;
16417
16418 if (fragp && update
16419 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
16420 fragp->fr_subtype
16421 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
16422 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
16423
16424 if (toofar)
16425 return 4;
16426
16427 return 2;
16428}
16429
252b5132
RH
16430/* Estimate the size of a frag before relaxing. Unless this is the
16431 mips16, we are not really relaxing here, and the final size is
16432 encoded in the subtype information. For the mips16, we have to
16433 decide whether we are using an extended opcode or not. */
16434
252b5132 16435int
17a2f251 16436md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 16437{
5919d012 16438 int change;
252b5132 16439
4a6a3df4
AO
16440 if (RELAX_BRANCH_P (fragp->fr_subtype))
16441 {
16442
b34976b6
AM
16443 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
16444
4a6a3df4
AO
16445 return fragp->fr_var;
16446 }
16447
252b5132 16448 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
16449 /* We don't want to modify the EXTENDED bit here; it might get us
16450 into infinite loops. We change it only in mips_relax_frag(). */
16451 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132 16452
df58fc94
RS
16453 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
16454 {
16455 int length = 4;
16456
16457 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
16458 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
16459 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
16460 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
16461 fragp->fr_var = length;
16462
16463 return length;
16464 }
16465
252b5132 16466 if (mips_pic == NO_PIC)
5919d012 16467 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132 16468 else if (mips_pic == SVR4_PIC)
5919d012 16469 change = pic_need_relax (fragp->fr_symbol, segtype);
0a44bf69
RS
16470 else if (mips_pic == VXWORKS_PIC)
16471 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
16472 change = 0;
252b5132
RH
16473 else
16474 abort ();
16475
16476 if (change)
16477 {
4d7206a2 16478 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 16479 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 16480 }
4d7206a2
RS
16481 else
16482 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
16483}
16484
16485/* This is called to see whether a reloc against a defined symbol
de7e6852 16486 should be converted into a reloc against a section. */
252b5132
RH
16487
16488int
17a2f251 16489mips_fix_adjustable (fixS *fixp)
252b5132 16490{
252b5132
RH
16491 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
16492 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
16493 return 0;
a161fe53 16494
252b5132
RH
16495 if (fixp->fx_addsy == NULL)
16496 return 1;
a161fe53 16497
de7e6852
RS
16498 /* If symbol SYM is in a mergeable section, relocations of the form
16499 SYM + 0 can usually be made section-relative. The mergeable data
16500 is then identified by the section offset rather than by the symbol.
16501
16502 However, if we're generating REL LO16 relocations, the offset is split
16503 between the LO16 and parterning high part relocation. The linker will
16504 need to recalculate the complete offset in order to correctly identify
16505 the merge data.
16506
16507 The linker has traditionally not looked for the parterning high part
16508 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
16509 placed anywhere. Rather than break backwards compatibility by changing
16510 this, it seems better not to force the issue, and instead keep the
16511 original symbol. This will work with either linker behavior. */
738e5348 16512 if ((lo16_reloc_p (fixp->fx_r_type)
704803a9 16513 || reloc_needs_lo_p (fixp->fx_r_type))
de7e6852
RS
16514 && HAVE_IN_PLACE_ADDENDS
16515 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
16516 return 0;
16517
ce70d90a 16518 /* There is no place to store an in-place offset for JALR relocations.
2de39019
CM
16519 Likewise an in-range offset of limited PC-relative relocations may
16520 overflow the in-place relocatable field if recalculated against the
16521 start address of the symbol's containing section. */
ce70d90a 16522 if (HAVE_IN_PLACE_ADDENDS
2de39019
CM
16523 && (limited_pcrel_reloc_p (fixp->fx_r_type)
16524 || jalr_reloc_p (fixp->fx_r_type)))
1180b5a4
RS
16525 return 0;
16526
b314ec0e
RS
16527 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
16528 to a floating-point stub. The same is true for non-R_MIPS16_26
16529 relocations against MIPS16 functions; in this case, the stub becomes
16530 the function's canonical address.
16531
16532 Floating-point stubs are stored in unique .mips16.call.* or
16533 .mips16.fn.* sections. If a stub T for function F is in section S,
16534 the first relocation in section S must be against F; this is how the
16535 linker determines the target function. All relocations that might
16536 resolve to T must also be against F. We therefore have the following
16537 restrictions, which are given in an intentionally-redundant way:
16538
16539 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
16540 symbols.
16541
16542 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
16543 if that stub might be used.
16544
16545 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
16546 symbols.
16547
16548 4. We cannot reduce a stub's relocations against MIPS16 symbols if
16549 that stub might be used.
16550
16551 There is a further restriction:
16552
df58fc94
RS
16553 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
16554 R_MICROMIPS_26_S1) against MIPS16 or microMIPS symbols on
16555 targets with in-place addends; the relocation field cannot
b314ec0e
RS
16556 encode the low bit.
16557
df58fc94
RS
16558 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
16559 against a MIPS16 symbol. We deal with (5) by by not reducing any
16560 such relocations on REL targets.
b314ec0e
RS
16561
16562 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
16563 relocation against some symbol R, no relocation against R may be
16564 reduced. (Note that this deals with (2) as well as (1) because
16565 relocations against global symbols will never be reduced on ELF
16566 targets.) This approach is a little simpler than trying to detect
16567 stub sections, and gives the "all or nothing" per-symbol consistency
16568 that we have for MIPS16 symbols. */
f3ded42a 16569 if (fixp->fx_subsy == NULL
30c09090 16570 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
df58fc94
RS
16571 || *symbol_get_tc (fixp->fx_addsy)
16572 || (HAVE_IN_PLACE_ADDENDS
16573 && ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
16574 && jmp_reloc_p (fixp->fx_r_type))))
252b5132 16575 return 0;
a161fe53 16576
252b5132
RH
16577 return 1;
16578}
16579
16580/* Translate internal representation of relocation info to BFD target
16581 format. */
16582
16583arelent **
17a2f251 16584tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
16585{
16586 static arelent *retval[4];
16587 arelent *reloc;
16588 bfd_reloc_code_real_type code;
16589
4b0cff4e
TS
16590 memset (retval, 0, sizeof(retval));
16591 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
49309057
ILT
16592 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
16593 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
16594 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
16595
bad36eac
DJ
16596 if (fixp->fx_pcrel)
16597 {
df58fc94
RS
16598 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
16599 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
16600 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
b47468a6
CM
16601 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
16602 || fixp->fx_r_type == BFD_RELOC_32_PCREL);
bad36eac
DJ
16603
16604 /* At this point, fx_addnumber is "symbol offset - pcrel address".
16605 Relocations want only the symbol offset. */
16606 reloc->addend = fixp->fx_addnumber + reloc->address;
bad36eac
DJ
16607 }
16608 else
16609 reloc->addend = fixp->fx_addnumber;
252b5132 16610
438c16b8
TS
16611 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
16612 entry to be used in the relocation's section offset. */
16613 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
16614 {
16615 reloc->address = reloc->addend;
16616 reloc->addend = 0;
16617 }
16618
252b5132 16619 code = fixp->fx_r_type;
252b5132 16620
bad36eac 16621 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
252b5132
RH
16622 if (reloc->howto == NULL)
16623 {
16624 as_bad_where (fixp->fx_file, fixp->fx_line,
1661c76c
RS
16625 _("cannot represent %s relocation in this object file"
16626 " format"),
252b5132
RH
16627 bfd_get_reloc_code_name (code));
16628 retval[0] = NULL;
16629 }
16630
16631 return retval;
16632}
16633
16634/* Relax a machine dependent frag. This returns the amount by which
16635 the current size of the frag should change. */
16636
16637int
17a2f251 16638mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 16639{
4a6a3df4
AO
16640 if (RELAX_BRANCH_P (fragp->fr_subtype))
16641 {
16642 offsetT old_var = fragp->fr_var;
b34976b6
AM
16643
16644 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
16645
16646 return fragp->fr_var - old_var;
16647 }
16648
df58fc94
RS
16649 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
16650 {
16651 offsetT old_var = fragp->fr_var;
16652 offsetT new_var = 4;
16653
16654 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
16655 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
16656 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
16657 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
16658 fragp->fr_var = new_var;
16659
16660 return new_var - old_var;
16661 }
16662
252b5132
RH
16663 if (! RELAX_MIPS16_P (fragp->fr_subtype))
16664 return 0;
16665
c4e7957c 16666 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
16667 {
16668 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
16669 return 0;
16670 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
16671 return 2;
16672 }
16673 else
16674 {
16675 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
16676 return 0;
16677 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
16678 return -2;
16679 }
16680
16681 return 0;
16682}
16683
16684/* Convert a machine dependent frag. */
16685
16686void
17a2f251 16687md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 16688{
4a6a3df4
AO
16689 if (RELAX_BRANCH_P (fragp->fr_subtype))
16690 {
4d68580a 16691 char *buf;
4a6a3df4
AO
16692 unsigned long insn;
16693 expressionS exp;
16694 fixS *fixp;
b34976b6 16695
4d68580a
RS
16696 buf = fragp->fr_literal + fragp->fr_fix;
16697 insn = read_insn (buf);
b34976b6 16698
4a6a3df4
AO
16699 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
16700 {
16701 /* We generate a fixup instead of applying it right now
16702 because, if there are linker relaxations, we're going to
16703 need the relocations. */
16704 exp.X_op = O_symbol;
16705 exp.X_add_symbol = fragp->fr_symbol;
16706 exp.X_add_number = fragp->fr_offset;
16707
4d68580a
RS
16708 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
16709 BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
16710 fixp->fx_file = fragp->fr_file;
16711 fixp->fx_line = fragp->fr_line;
b34976b6 16712
4d68580a 16713 buf = write_insn (buf, insn);
4a6a3df4
AO
16714 }
16715 else
16716 {
16717 int i;
16718
16719 as_warn_where (fragp->fr_file, fragp->fr_line,
1661c76c 16720 _("relaxed out-of-range branch into a jump"));
4a6a3df4
AO
16721
16722 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
16723 goto uncond;
16724
16725 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
16726 {
16727 /* Reverse the branch. */
16728 switch ((insn >> 28) & 0xf)
16729 {
16730 case 4:
56d438b1
CF
16731 if ((insn & 0xff000000) == 0x47000000
16732 || (insn & 0xff600000) == 0x45600000)
16733 {
16734 /* BZ.df/BNZ.df, BZ.V/BNZ.V can have the condition
16735 reversed by tweaking bit 23. */
16736 insn ^= 0x00800000;
16737 }
16738 else
16739 {
16740 /* bc[0-3][tf]l? instructions can have the condition
16741 reversed by tweaking a single TF bit, and their
16742 opcodes all have 0x4???????. */
16743 gas_assert ((insn & 0xf3e00000) == 0x41000000);
16744 insn ^= 0x00010000;
16745 }
4a6a3df4
AO
16746 break;
16747
16748 case 0:
16749 /* bltz 0x04000000 bgez 0x04010000
54f4ddb3 16750 bltzal 0x04100000 bgezal 0x04110000 */
9c2799c2 16751 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
4a6a3df4
AO
16752 insn ^= 0x00010000;
16753 break;
b34976b6 16754
4a6a3df4
AO
16755 case 1:
16756 /* beq 0x10000000 bne 0x14000000
54f4ddb3 16757 blez 0x18000000 bgtz 0x1c000000 */
4a6a3df4
AO
16758 insn ^= 0x04000000;
16759 break;
16760
16761 default:
16762 abort ();
16763 }
16764 }
16765
16766 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
16767 {
16768 /* Clear the and-link bit. */
9c2799c2 16769 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
4a6a3df4 16770
54f4ddb3
TS
16771 /* bltzal 0x04100000 bgezal 0x04110000
16772 bltzall 0x04120000 bgezall 0x04130000 */
4a6a3df4
AO
16773 insn &= ~0x00100000;
16774 }
16775
16776 /* Branch over the branch (if the branch was likely) or the
16777 full jump (not likely case). Compute the offset from the
16778 current instruction to branch to. */
16779 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
16780 i = 16;
16781 else
16782 {
16783 /* How many bytes in instructions we've already emitted? */
4d68580a 16784 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
16785 /* How many bytes in instructions from here to the end? */
16786 i = fragp->fr_var - i;
16787 }
16788 /* Convert to instruction count. */
16789 i >>= 2;
16790 /* Branch counts from the next instruction. */
b34976b6 16791 i--;
4a6a3df4
AO
16792 insn |= i;
16793 /* Branch over the jump. */
4d68580a 16794 buf = write_insn (buf, insn);
4a6a3df4 16795
54f4ddb3 16796 /* nop */
4d68580a 16797 buf = write_insn (buf, 0);
4a6a3df4
AO
16798
16799 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
16800 {
16801 /* beql $0, $0, 2f */
16802 insn = 0x50000000;
16803 /* Compute the PC offset from the current instruction to
16804 the end of the variable frag. */
16805 /* How many bytes in instructions we've already emitted? */
4d68580a 16806 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
16807 /* How many bytes in instructions from here to the end? */
16808 i = fragp->fr_var - i;
16809 /* Convert to instruction count. */
16810 i >>= 2;
16811 /* Don't decrement i, because we want to branch over the
16812 delay slot. */
4a6a3df4 16813 insn |= i;
4a6a3df4 16814
4d68580a
RS
16815 buf = write_insn (buf, insn);
16816 buf = write_insn (buf, 0);
4a6a3df4
AO
16817 }
16818
16819 uncond:
16820 if (mips_pic == NO_PIC)
16821 {
16822 /* j or jal. */
16823 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
16824 ? 0x0c000000 : 0x08000000);
16825 exp.X_op = O_symbol;
16826 exp.X_add_symbol = fragp->fr_symbol;
16827 exp.X_add_number = fragp->fr_offset;
16828
4d68580a
RS
16829 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
16830 FALSE, BFD_RELOC_MIPS_JMP);
4a6a3df4
AO
16831 fixp->fx_file = fragp->fr_file;
16832 fixp->fx_line = fragp->fr_line;
16833
4d68580a 16834 buf = write_insn (buf, insn);
4a6a3df4
AO
16835 }
16836 else
16837 {
66b3e8da
MR
16838 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
16839
4a6a3df4 16840 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
66b3e8da
MR
16841 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
16842 insn |= at << OP_SH_RT;
4a6a3df4
AO
16843 exp.X_op = O_symbol;
16844 exp.X_add_symbol = fragp->fr_symbol;
16845 exp.X_add_number = fragp->fr_offset;
16846
16847 if (fragp->fr_offset)
16848 {
16849 exp.X_add_symbol = make_expr_symbol (&exp);
16850 exp.X_add_number = 0;
16851 }
16852
4d68580a
RS
16853 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
16854 FALSE, BFD_RELOC_MIPS_GOT16);
4a6a3df4
AO
16855 fixp->fx_file = fragp->fr_file;
16856 fixp->fx_line = fragp->fr_line;
16857
4d68580a 16858 buf = write_insn (buf, insn);
b34976b6 16859
4a6a3df4 16860 if (mips_opts.isa == ISA_MIPS1)
4d68580a
RS
16861 /* nop */
16862 buf = write_insn (buf, 0);
4a6a3df4
AO
16863
16864 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
66b3e8da
MR
16865 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
16866 insn |= at << OP_SH_RS | at << OP_SH_RT;
4a6a3df4 16867
4d68580a
RS
16868 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
16869 FALSE, BFD_RELOC_LO16);
4a6a3df4
AO
16870 fixp->fx_file = fragp->fr_file;
16871 fixp->fx_line = fragp->fr_line;
b34976b6 16872
4d68580a 16873 buf = write_insn (buf, insn);
4a6a3df4
AO
16874
16875 /* j(al)r $at. */
16876 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
66b3e8da 16877 insn = 0x0000f809;
4a6a3df4 16878 else
66b3e8da
MR
16879 insn = 0x00000008;
16880 insn |= at << OP_SH_RS;
4a6a3df4 16881
4d68580a 16882 buf = write_insn (buf, insn);
4a6a3df4
AO
16883 }
16884 }
16885
4a6a3df4 16886 fragp->fr_fix += fragp->fr_var;
4d68580a 16887 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
4a6a3df4
AO
16888 return;
16889 }
16890
df58fc94
RS
16891 /* Relax microMIPS branches. */
16892 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
16893 {
4d68580a 16894 char *buf = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
16895 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
16896 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
16897 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
2309ddf2 16898 bfd_boolean short_ds;
df58fc94
RS
16899 unsigned long insn;
16900 expressionS exp;
16901 fixS *fixp;
16902
16903 exp.X_op = O_symbol;
16904 exp.X_add_symbol = fragp->fr_symbol;
16905 exp.X_add_number = fragp->fr_offset;
16906
16907 fragp->fr_fix += fragp->fr_var;
16908
16909 /* Handle 16-bit branches that fit or are forced to fit. */
16910 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
16911 {
16912 /* We generate a fixup instead of applying it right now,
16913 because if there is linker relaxation, we're going to
16914 need the relocations. */
16915 if (type == 'D')
4d68580a 16916 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
df58fc94
RS
16917 BFD_RELOC_MICROMIPS_10_PCREL_S1);
16918 else if (type == 'E')
4d68580a 16919 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
df58fc94
RS
16920 BFD_RELOC_MICROMIPS_7_PCREL_S1);
16921 else
16922 abort ();
16923
16924 fixp->fx_file = fragp->fr_file;
16925 fixp->fx_line = fragp->fr_line;
16926
16927 /* These relocations can have an addend that won't fit in
16928 2 octets. */
16929 fixp->fx_no_overflow = 1;
16930
16931 return;
16932 }
16933
2309ddf2 16934 /* Handle 32-bit branches that fit or are forced to fit. */
df58fc94
RS
16935 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
16936 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
16937 {
16938 /* We generate a fixup instead of applying it right now,
16939 because if there is linker relaxation, we're going to
16940 need the relocations. */
4d68580a
RS
16941 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
16942 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
16943 fixp->fx_file = fragp->fr_file;
16944 fixp->fx_line = fragp->fr_line;
16945
16946 if (type == 0)
16947 return;
16948 }
16949
16950 /* Relax 16-bit branches to 32-bit branches. */
16951 if (type != 0)
16952 {
4d68580a 16953 insn = read_compressed_insn (buf, 2);
df58fc94
RS
16954
16955 if ((insn & 0xfc00) == 0xcc00) /* b16 */
16956 insn = 0x94000000; /* beq */
16957 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
16958 {
16959 unsigned long regno;
16960
16961 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
16962 regno = micromips_to_32_reg_d_map [regno];
16963 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
16964 insn |= regno << MICROMIPSOP_SH_RS;
16965 }
16966 else
16967 abort ();
16968
16969 /* Nothing else to do, just write it out. */
16970 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
16971 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
16972 {
4d68580a
RS
16973 buf = write_compressed_insn (buf, insn, 4);
16974 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
16975 return;
16976 }
16977 }
16978 else
4d68580a 16979 insn = read_compressed_insn (buf, 4);
df58fc94
RS
16980
16981 /* Relax 32-bit branches to a sequence of instructions. */
16982 as_warn_where (fragp->fr_file, fragp->fr_line,
1661c76c 16983 _("relaxed out-of-range branch into a jump"));
df58fc94 16984
2309ddf2
MR
16985 /* Set the short-delay-slot bit. */
16986 short_ds = al && (insn & 0x02000000) != 0;
df58fc94
RS
16987
16988 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
16989 {
16990 symbolS *l;
16991
16992 /* Reverse the branch. */
16993 if ((insn & 0xfc000000) == 0x94000000 /* beq */
16994 || (insn & 0xfc000000) == 0xb4000000) /* bne */
16995 insn ^= 0x20000000;
16996 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
16997 || (insn & 0xffe00000) == 0x40400000 /* bgez */
16998 || (insn & 0xffe00000) == 0x40800000 /* blez */
16999 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
17000 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
17001 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
17002 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
17003 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
17004 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
17005 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
17006 insn ^= 0x00400000;
17007 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
17008 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
17009 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
17010 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
17011 insn ^= 0x00200000;
56d438b1
CF
17012 else if ((insn & 0xff000000) == 0x83000000 /* BZ.df
17013 BNZ.df */
17014 || (insn & 0xff600000) == 0x81600000) /* BZ.V
17015 BNZ.V */
17016 insn ^= 0x00800000;
df58fc94
RS
17017 else
17018 abort ();
17019
17020 if (al)
17021 {
17022 /* Clear the and-link and short-delay-slot bits. */
17023 gas_assert ((insn & 0xfda00000) == 0x40200000);
17024
17025 /* bltzal 0x40200000 bgezal 0x40600000 */
17026 /* bltzals 0x42200000 bgezals 0x42600000 */
17027 insn &= ~0x02200000;
17028 }
17029
17030 /* Make a label at the end for use with the branch. */
17031 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
17032 micromips_label_inc ();
f3ded42a 17033 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
df58fc94
RS
17034
17035 /* Refer to it. */
4d68580a
RS
17036 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
17037 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
17038 fixp->fx_file = fragp->fr_file;
17039 fixp->fx_line = fragp->fr_line;
17040
17041 /* Branch over the jump. */
4d68580a 17042 buf = write_compressed_insn (buf, insn, 4);
df58fc94 17043 if (!compact)
4d68580a
RS
17044 /* nop */
17045 buf = write_compressed_insn (buf, 0x0c00, 2);
df58fc94
RS
17046 }
17047
17048 if (mips_pic == NO_PIC)
17049 {
2309ddf2
MR
17050 unsigned long jal = short_ds ? 0x74000000 : 0xf4000000; /* jal/s */
17051
df58fc94
RS
17052 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
17053 insn = al ? jal : 0xd4000000;
17054
4d68580a
RS
17055 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
17056 BFD_RELOC_MICROMIPS_JMP);
df58fc94
RS
17057 fixp->fx_file = fragp->fr_file;
17058 fixp->fx_line = fragp->fr_line;
17059
4d68580a 17060 buf = write_compressed_insn (buf, insn, 4);
df58fc94 17061 if (compact)
4d68580a
RS
17062 /* nop */
17063 buf = write_compressed_insn (buf, 0x0c00, 2);
df58fc94
RS
17064 }
17065 else
17066 {
17067 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
2309ddf2
MR
17068 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
17069 unsigned long jr = compact ? 0x45a0 : 0x4580; /* jr/c */
df58fc94
RS
17070
17071 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
17072 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
17073 insn |= at << MICROMIPSOP_SH_RT;
17074
17075 if (exp.X_add_number)
17076 {
17077 exp.X_add_symbol = make_expr_symbol (&exp);
17078 exp.X_add_number = 0;
17079 }
17080
4d68580a
RS
17081 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
17082 BFD_RELOC_MICROMIPS_GOT16);
df58fc94
RS
17083 fixp->fx_file = fragp->fr_file;
17084 fixp->fx_line = fragp->fr_line;
17085
4d68580a 17086 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
17087
17088 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
17089 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
17090 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
17091
4d68580a
RS
17092 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
17093 BFD_RELOC_MICROMIPS_LO16);
df58fc94
RS
17094 fixp->fx_file = fragp->fr_file;
17095 fixp->fx_line = fragp->fr_line;
17096
4d68580a 17097 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
17098
17099 /* jr/jrc/jalr/jalrs $at */
17100 insn = al ? jalr : jr;
17101 insn |= at << MICROMIPSOP_SH_MJ;
17102
4d68580a 17103 buf = write_compressed_insn (buf, insn, 2);
df58fc94
RS
17104 }
17105
4d68580a 17106 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
17107 return;
17108 }
17109
252b5132
RH
17110 if (RELAX_MIPS16_P (fragp->fr_subtype))
17111 {
17112 int type;
3ccad066 17113 const struct mips_int_operand *operand;
252b5132 17114 offsetT val;
5c04167a
RS
17115 char *buf;
17116 unsigned int user_length, length;
252b5132 17117 unsigned long insn;
5c04167a 17118 bfd_boolean ext;
252b5132
RH
17119
17120 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
3ccad066 17121 operand = mips16_immed_operand (type, FALSE);
252b5132 17122
5c04167a 17123 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
5f5f22c0 17124 val = resolve_symbol_value (fragp->fr_symbol);
3ccad066 17125 if (operand->root.type == OP_PCREL)
252b5132 17126 {
3ccad066 17127 const struct mips_pcrel_operand *pcrel_op;
252b5132
RH
17128 addressT addr;
17129
3ccad066 17130 pcrel_op = (const struct mips_pcrel_operand *) operand;
252b5132
RH
17131 addr = fragp->fr_address + fragp->fr_fix;
17132
17133 /* The rules for the base address of a PC relative reloc are
17134 complicated; see mips16_extended_frag. */
3ccad066 17135 if (pcrel_op->include_isa_bit)
252b5132
RH
17136 {
17137 addr += 2;
17138 if (ext)
17139 addr += 2;
17140 /* Ignore the low bit in the target, since it will be
17141 set for a text label. */
3ccad066 17142 val &= -2;
252b5132
RH
17143 }
17144 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17145 addr -= 4;
17146 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17147 addr -= 2;
17148
3ccad066 17149 addr &= -(1 << pcrel_op->align_log2);
252b5132
RH
17150 val -= addr;
17151
17152 /* Make sure the section winds up with the alignment we have
17153 assumed. */
3ccad066
RS
17154 if (operand->shift > 0)
17155 record_alignment (asec, operand->shift);
252b5132
RH
17156 }
17157
17158 if (ext
17159 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
17160 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
17161 as_warn_where (fragp->fr_file, fragp->fr_line,
17162 _("extended instruction in delay slot"));
17163
5c04167a 17164 buf = fragp->fr_literal + fragp->fr_fix;
252b5132 17165
4d68580a 17166 insn = read_compressed_insn (buf, 2);
5c04167a
RS
17167 if (ext)
17168 insn |= MIPS16_EXTEND;
252b5132 17169
5c04167a
RS
17170 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17171 user_length = 4;
17172 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17173 user_length = 2;
17174 else
17175 user_length = 0;
17176
43c0598f 17177 mips16_immed (fragp->fr_file, fragp->fr_line, type,
c150d1d2 17178 BFD_RELOC_UNUSED, val, user_length, &insn);
252b5132 17179
5c04167a
RS
17180 length = (ext ? 4 : 2);
17181 gas_assert (mips16_opcode_length (insn) == length);
17182 write_compressed_insn (buf, insn, length);
17183 fragp->fr_fix += length;
252b5132
RH
17184 }
17185 else
17186 {
df58fc94
RS
17187 relax_substateT subtype = fragp->fr_subtype;
17188 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
17189 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
4d7206a2
RS
17190 int first, second;
17191 fixS *fixp;
252b5132 17192
df58fc94
RS
17193 first = RELAX_FIRST (subtype);
17194 second = RELAX_SECOND (subtype);
4d7206a2 17195 fixp = (fixS *) fragp->fr_opcode;
252b5132 17196
df58fc94
RS
17197 /* If the delay slot chosen does not match the size of the instruction,
17198 then emit a warning. */
17199 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
17200 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
17201 {
17202 relax_substateT s;
17203 const char *msg;
17204
17205 s = subtype & (RELAX_DELAY_SLOT_16BIT
17206 | RELAX_DELAY_SLOT_SIZE_FIRST
17207 | RELAX_DELAY_SLOT_SIZE_SECOND);
17208 msg = macro_warning (s);
17209 if (msg != NULL)
db9b2be4 17210 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94
RS
17211 subtype &= ~s;
17212 }
17213
584892a6 17214 /* Possibly emit a warning if we've chosen the longer option. */
df58fc94 17215 if (use_second == second_longer)
584892a6 17216 {
df58fc94
RS
17217 relax_substateT s;
17218 const char *msg;
17219
17220 s = (subtype
17221 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
17222 msg = macro_warning (s);
17223 if (msg != NULL)
db9b2be4 17224 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94 17225 subtype &= ~s;
584892a6
RS
17226 }
17227
4d7206a2
RS
17228 /* Go through all the fixups for the first sequence. Disable them
17229 (by marking them as done) if we're going to use the second
17230 sequence instead. */
17231 while (fixp
17232 && fixp->fx_frag == fragp
17233 && fixp->fx_where < fragp->fr_fix - second)
17234 {
df58fc94 17235 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
17236 fixp->fx_done = 1;
17237 fixp = fixp->fx_next;
17238 }
252b5132 17239
4d7206a2
RS
17240 /* Go through the fixups for the second sequence. Disable them if
17241 we're going to use the first sequence, otherwise adjust their
17242 addresses to account for the relaxation. */
17243 while (fixp && fixp->fx_frag == fragp)
17244 {
df58fc94 17245 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
17246 fixp->fx_where -= first;
17247 else
17248 fixp->fx_done = 1;
17249 fixp = fixp->fx_next;
17250 }
17251
17252 /* Now modify the frag contents. */
df58fc94 17253 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
17254 {
17255 char *start;
17256
17257 start = fragp->fr_literal + fragp->fr_fix - first - second;
17258 memmove (start, start + first, second);
17259 fragp->fr_fix -= first;
17260 }
17261 else
17262 fragp->fr_fix -= second;
252b5132
RH
17263 }
17264}
17265
252b5132
RH
17266/* This function is called after the relocs have been generated.
17267 We've been storing mips16 text labels as odd. Here we convert them
17268 back to even for the convenience of the debugger. */
17269
17270void
17a2f251 17271mips_frob_file_after_relocs (void)
252b5132
RH
17272{
17273 asymbol **syms;
17274 unsigned int count, i;
17275
252b5132
RH
17276 syms = bfd_get_outsymbols (stdoutput);
17277 count = bfd_get_symcount (stdoutput);
17278 for (i = 0; i < count; i++, syms++)
df58fc94
RS
17279 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
17280 && ((*syms)->value & 1) != 0)
17281 {
17282 (*syms)->value &= ~1;
17283 /* If the symbol has an odd size, it was probably computed
17284 incorrectly, so adjust that as well. */
17285 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
17286 ++elf_symbol (*syms)->internal_elf_sym.st_size;
17287 }
252b5132
RH
17288}
17289
a1facbec
MR
17290/* This function is called whenever a label is defined, including fake
17291 labels instantiated off the dot special symbol. It is used when
17292 handling branch delays; if a branch has a label, we assume we cannot
17293 move it. This also bumps the value of the symbol by 1 in compressed
17294 code. */
252b5132 17295
e1b47bd5 17296static void
a1facbec 17297mips_record_label (symbolS *sym)
252b5132 17298{
a8dbcb85 17299 segment_info_type *si = seg_info (now_seg);
252b5132
RH
17300 struct insn_label_list *l;
17301
17302 if (free_insn_labels == NULL)
17303 l = (struct insn_label_list *) xmalloc (sizeof *l);
17304 else
17305 {
17306 l = free_insn_labels;
17307 free_insn_labels = l->next;
17308 }
17309
17310 l->label = sym;
a8dbcb85
TS
17311 l->next = si->label_list;
17312 si->label_list = l;
a1facbec 17313}
07a53e5c 17314
a1facbec
MR
17315/* This function is called as tc_frob_label() whenever a label is defined
17316 and adds a DWARF-2 record we only want for true labels. */
17317
17318void
17319mips_define_label (symbolS *sym)
17320{
17321 mips_record_label (sym);
07a53e5c 17322 dwarf2_emit_label (sym);
252b5132 17323}
e1b47bd5
RS
17324
17325/* This function is called by tc_new_dot_label whenever a new dot symbol
17326 is defined. */
17327
17328void
17329mips_add_dot_label (symbolS *sym)
17330{
17331 mips_record_label (sym);
17332 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
17333 mips_compressed_mark_label (sym);
17334}
252b5132 17335\f
252b5132
RH
17336/* Some special processing for a MIPS ELF file. */
17337
17338void
17a2f251 17339mips_elf_final_processing (void)
252b5132
RH
17340{
17341 /* Write out the register information. */
316f5878 17342 if (mips_abi != N64_ABI)
252b5132
RH
17343 {
17344 Elf32_RegInfo s;
17345
17346 s.ri_gprmask = mips_gprmask;
17347 s.ri_cprmask[0] = mips_cprmask[0];
17348 s.ri_cprmask[1] = mips_cprmask[1];
17349 s.ri_cprmask[2] = mips_cprmask[2];
17350 s.ri_cprmask[3] = mips_cprmask[3];
17351 /* The gp_value field is set by the MIPS ELF backend. */
17352
17353 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
17354 ((Elf32_External_RegInfo *)
17355 mips_regmask_frag));
17356 }
17357 else
17358 {
17359 Elf64_Internal_RegInfo s;
17360
17361 s.ri_gprmask = mips_gprmask;
17362 s.ri_pad = 0;
17363 s.ri_cprmask[0] = mips_cprmask[0];
17364 s.ri_cprmask[1] = mips_cprmask[1];
17365 s.ri_cprmask[2] = mips_cprmask[2];
17366 s.ri_cprmask[3] = mips_cprmask[3];
17367 /* The gp_value field is set by the MIPS ELF backend. */
17368
17369 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
17370 ((Elf64_External_RegInfo *)
17371 mips_regmask_frag));
17372 }
17373
17374 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
17375 sort of BFD interface for this. */
17376 if (mips_any_noreorder)
17377 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
17378 if (mips_pic != NO_PIC)
143d77c5 17379 {
8b828383 17380 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
17381 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
17382 }
17383 if (mips_abicalls)
17384 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 17385
b015e599
AP
17386 /* Set MIPS ELF flags for ASEs. Note that not all ASEs have flags
17387 defined at present; this might need to change in future. */
a4672219
TS
17388 if (file_ase_mips16)
17389 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
df58fc94
RS
17390 if (file_ase_micromips)
17391 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
846ef2d0 17392 if (file_ase & ASE_MDMX)
deec1734 17393 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 17394
bdaaa2e1 17395 /* Set the MIPS ELF ABI flags. */
316f5878 17396 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 17397 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 17398 else if (mips_abi == O64_ABI)
252b5132 17399 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 17400 else if (mips_abi == EABI_ABI)
252b5132 17401 {
0b35dfee 17402 if (!file_mips_opts.gp32)
252b5132
RH
17403 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
17404 else
17405 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
17406 }
316f5878 17407 else if (mips_abi == N32_ABI)
be00bddd
TS
17408 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
17409
c9914766 17410 /* Nothing to do for N64_ABI. */
252b5132
RH
17411
17412 if (mips_32bitmode)
17413 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
ad3fea08 17414
ba92f887
MR
17415 if (mips_flag_nan2008)
17416 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NAN2008;
17417
ad3fea08 17418 /* 32 bit code with 64 bit FP registers. */
0b35dfee 17419 if (file_mips_opts.fp == 64 && ABI_NEEDS_32BIT_REGS (mips_abi))
f1c38003 17420 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_FP64;
252b5132 17421}
252b5132 17422\f
beae10d5 17423typedef struct proc {
9b2f1d35
EC
17424 symbolS *func_sym;
17425 symbolS *func_end_sym;
beae10d5
KH
17426 unsigned long reg_mask;
17427 unsigned long reg_offset;
17428 unsigned long fpreg_mask;
17429 unsigned long fpreg_offset;
17430 unsigned long frame_offset;
17431 unsigned long frame_reg;
17432 unsigned long pc_reg;
17433} procS;
252b5132
RH
17434
17435static procS cur_proc;
17436static procS *cur_proc_ptr;
17437static int numprocs;
17438
df58fc94
RS
17439/* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
17440 as "2", and a normal nop as "0". */
17441
17442#define NOP_OPCODE_MIPS 0
17443#define NOP_OPCODE_MIPS16 1
17444#define NOP_OPCODE_MICROMIPS 2
742a56fe
RS
17445
17446char
17447mips_nop_opcode (void)
17448{
df58fc94
RS
17449 if (seg_info (now_seg)->tc_segment_info_data.micromips)
17450 return NOP_OPCODE_MICROMIPS;
17451 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
17452 return NOP_OPCODE_MIPS16;
17453 else
17454 return NOP_OPCODE_MIPS;
742a56fe
RS
17455}
17456
df58fc94
RS
17457/* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
17458 32-bit microMIPS NOPs here (if applicable). */
a19d8eb0 17459
0a9ef439 17460void
17a2f251 17461mips_handle_align (fragS *fragp)
a19d8eb0 17462{
df58fc94 17463 char nop_opcode;
742a56fe 17464 char *p;
c67a084a
NC
17465 int bytes, size, excess;
17466 valueT opcode;
742a56fe 17467
0a9ef439
RH
17468 if (fragp->fr_type != rs_align_code)
17469 return;
17470
742a56fe 17471 p = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
17472 nop_opcode = *p;
17473 switch (nop_opcode)
a19d8eb0 17474 {
df58fc94
RS
17475 case NOP_OPCODE_MICROMIPS:
17476 opcode = micromips_nop32_insn.insn_opcode;
17477 size = 4;
17478 break;
17479 case NOP_OPCODE_MIPS16:
c67a084a
NC
17480 opcode = mips16_nop_insn.insn_opcode;
17481 size = 2;
df58fc94
RS
17482 break;
17483 case NOP_OPCODE_MIPS:
17484 default:
c67a084a
NC
17485 opcode = nop_insn.insn_opcode;
17486 size = 4;
df58fc94 17487 break;
c67a084a 17488 }
a19d8eb0 17489
c67a084a
NC
17490 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
17491 excess = bytes % size;
df58fc94
RS
17492
17493 /* Handle the leading part if we're not inserting a whole number of
17494 instructions, and make it the end of the fixed part of the frag.
17495 Try to fit in a short microMIPS NOP if applicable and possible,
17496 and use zeroes otherwise. */
17497 gas_assert (excess < 4);
17498 fragp->fr_fix += excess;
17499 switch (excess)
c67a084a 17500 {
df58fc94
RS
17501 case 3:
17502 *p++ = '\0';
17503 /* Fall through. */
17504 case 2:
833794fc 17505 if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
df58fc94 17506 {
4d68580a 17507 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
df58fc94
RS
17508 break;
17509 }
17510 *p++ = '\0';
17511 /* Fall through. */
17512 case 1:
17513 *p++ = '\0';
17514 /* Fall through. */
17515 case 0:
17516 break;
a19d8eb0 17517 }
c67a084a
NC
17518
17519 md_number_to_chars (p, opcode, size);
17520 fragp->fr_var = size;
a19d8eb0
CP
17521}
17522
252b5132 17523static void
17a2f251 17524md_obj_begin (void)
252b5132
RH
17525{
17526}
17527
17528static void
17a2f251 17529md_obj_end (void)
252b5132 17530{
54f4ddb3 17531 /* Check for premature end, nesting errors, etc. */
252b5132 17532 if (cur_proc_ptr)
9a41af64 17533 as_warn (_("missing .end at end of assembly"));
252b5132
RH
17534}
17535
17536static long
17a2f251 17537get_number (void)
252b5132
RH
17538{
17539 int negative = 0;
17540 long val = 0;
17541
17542 if (*input_line_pointer == '-')
17543 {
17544 ++input_line_pointer;
17545 negative = 1;
17546 }
3882b010 17547 if (!ISDIGIT (*input_line_pointer))
956cd1d6 17548 as_bad (_("expected simple number"));
252b5132
RH
17549 if (input_line_pointer[0] == '0')
17550 {
17551 if (input_line_pointer[1] == 'x')
17552 {
17553 input_line_pointer += 2;
3882b010 17554 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
17555 {
17556 val <<= 4;
17557 val |= hex_value (*input_line_pointer++);
17558 }
17559 return negative ? -val : val;
17560 }
17561 else
17562 {
17563 ++input_line_pointer;
3882b010 17564 while (ISDIGIT (*input_line_pointer))
252b5132
RH
17565 {
17566 val <<= 3;
17567 val |= *input_line_pointer++ - '0';
17568 }
17569 return negative ? -val : val;
17570 }
17571 }
3882b010 17572 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
17573 {
17574 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
17575 *input_line_pointer, *input_line_pointer);
956cd1d6 17576 as_warn (_("invalid number"));
252b5132
RH
17577 return -1;
17578 }
3882b010 17579 while (ISDIGIT (*input_line_pointer))
252b5132
RH
17580 {
17581 val *= 10;
17582 val += *input_line_pointer++ - '0';
17583 }
17584 return negative ? -val : val;
17585}
17586
17587/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
17588 is an initial number which is the ECOFF file index. In the non-ECOFF
17589 case .file implies DWARF-2. */
17590
17591static void
17a2f251 17592s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 17593{
ecb4347a
DJ
17594 static int first_file_directive = 0;
17595
c5dd6aab
DJ
17596 if (ECOFF_DEBUGGING)
17597 {
17598 get_number ();
17599 s_app_file (0);
17600 }
17601 else
ecb4347a
DJ
17602 {
17603 char *filename;
17604
17605 filename = dwarf2_directive_file (0);
17606
17607 /* Versions of GCC up to 3.1 start files with a ".file"
17608 directive even for stabs output. Make sure that this
17609 ".file" is handled. Note that you need a version of GCC
17610 after 3.1 in order to support DWARF-2 on MIPS. */
17611 if (filename != NULL && ! first_file_directive)
17612 {
17613 (void) new_logical_line (filename, -1);
c04f5787 17614 s_app_file_string (filename, 0);
ecb4347a
DJ
17615 }
17616 first_file_directive = 1;
17617 }
c5dd6aab
DJ
17618}
17619
17620/* The .loc directive, implying DWARF-2. */
252b5132
RH
17621
17622static void
17a2f251 17623s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 17624{
c5dd6aab
DJ
17625 if (!ECOFF_DEBUGGING)
17626 dwarf2_directive_loc (0);
252b5132
RH
17627}
17628
252b5132
RH
17629/* The .end directive. */
17630
17631static void
17a2f251 17632s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
17633{
17634 symbolS *p;
252b5132 17635
7a621144
DJ
17636 /* Following functions need their own .frame and .cprestore directives. */
17637 mips_frame_reg_valid = 0;
17638 mips_cprestore_valid = 0;
17639
252b5132
RH
17640 if (!is_end_of_line[(unsigned char) *input_line_pointer])
17641 {
17642 p = get_symbol ();
17643 demand_empty_rest_of_line ();
17644 }
17645 else
17646 p = NULL;
17647
14949570 17648 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
17649 as_warn (_(".end not in text section"));
17650
17651 if (!cur_proc_ptr)
17652 {
1661c76c 17653 as_warn (_(".end directive without a preceding .ent directive"));
252b5132
RH
17654 demand_empty_rest_of_line ();
17655 return;
17656 }
17657
17658 if (p != NULL)
17659 {
9c2799c2 17660 gas_assert (S_GET_NAME (p));
9b2f1d35 17661 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
1661c76c 17662 as_warn (_(".end symbol does not match .ent symbol"));
ecb4347a
DJ
17663
17664 if (debug_type == DEBUG_STABS)
17665 stabs_generate_asm_endfunc (S_GET_NAME (p),
17666 S_GET_NAME (p));
252b5132
RH
17667 }
17668 else
17669 as_warn (_(".end directive missing or unknown symbol"));
17670
9b2f1d35
EC
17671 /* Create an expression to calculate the size of the function. */
17672 if (p && cur_proc_ptr)
17673 {
17674 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
17675 expressionS *exp = xmalloc (sizeof (expressionS));
17676
17677 obj->size = exp;
17678 exp->X_op = O_subtract;
17679 exp->X_add_symbol = symbol_temp_new_now ();
17680 exp->X_op_symbol = p;
17681 exp->X_add_number = 0;
17682
17683 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
17684 }
17685
ecb4347a 17686 /* Generate a .pdr section. */
f3ded42a 17687 if (!ECOFF_DEBUGGING && mips_flag_pdr)
ecb4347a
DJ
17688 {
17689 segT saved_seg = now_seg;
17690 subsegT saved_subseg = now_subseg;
ecb4347a
DJ
17691 expressionS exp;
17692 char *fragp;
252b5132 17693
252b5132 17694#ifdef md_flush_pending_output
ecb4347a 17695 md_flush_pending_output ();
252b5132
RH
17696#endif
17697
9c2799c2 17698 gas_assert (pdr_seg);
ecb4347a 17699 subseg_set (pdr_seg, 0);
252b5132 17700
ecb4347a
DJ
17701 /* Write the symbol. */
17702 exp.X_op = O_symbol;
17703 exp.X_add_symbol = p;
17704 exp.X_add_number = 0;
17705 emit_expr (&exp, 4);
252b5132 17706
ecb4347a 17707 fragp = frag_more (7 * 4);
252b5132 17708
17a2f251
TS
17709 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
17710 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
17711 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
17712 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
17713 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
17714 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
17715 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 17716
ecb4347a
DJ
17717 subseg_set (saved_seg, saved_subseg);
17718 }
252b5132
RH
17719
17720 cur_proc_ptr = NULL;
17721}
17722
17723/* The .aent and .ent directives. */
17724
17725static void
17a2f251 17726s_mips_ent (int aent)
252b5132 17727{
252b5132 17728 symbolS *symbolP;
252b5132
RH
17729
17730 symbolP = get_symbol ();
17731 if (*input_line_pointer == ',')
f9419b05 17732 ++input_line_pointer;
252b5132 17733 SKIP_WHITESPACE ();
3882b010 17734 if (ISDIGIT (*input_line_pointer)
d9a62219 17735 || *input_line_pointer == '-')
874e8986 17736 get_number ();
252b5132 17737
14949570 17738 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
1661c76c 17739 as_warn (_(".ent or .aent not in text section"));
252b5132
RH
17740
17741 if (!aent && cur_proc_ptr)
9a41af64 17742 as_warn (_("missing .end"));
252b5132
RH
17743
17744 if (!aent)
17745 {
7a621144
DJ
17746 /* This function needs its own .frame and .cprestore directives. */
17747 mips_frame_reg_valid = 0;
17748 mips_cprestore_valid = 0;
17749
252b5132
RH
17750 cur_proc_ptr = &cur_proc;
17751 memset (cur_proc_ptr, '\0', sizeof (procS));
17752
9b2f1d35 17753 cur_proc_ptr->func_sym = symbolP;
252b5132 17754
f9419b05 17755 ++numprocs;
ecb4347a
DJ
17756
17757 if (debug_type == DEBUG_STABS)
17758 stabs_generate_asm_func (S_GET_NAME (symbolP),
17759 S_GET_NAME (symbolP));
252b5132
RH
17760 }
17761
7c0fc524
MR
17762 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
17763
252b5132
RH
17764 demand_empty_rest_of_line ();
17765}
17766
17767/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 17768 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 17769 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 17770 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
17771 symbol table (in the mdebug section). */
17772
17773static void
17a2f251 17774s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 17775{
f3ded42a
RS
17776 if (ECOFF_DEBUGGING)
17777 s_ignore (ignore);
17778 else
ecb4347a
DJ
17779 {
17780 long val;
252b5132 17781
ecb4347a
DJ
17782 if (cur_proc_ptr == (procS *) NULL)
17783 {
17784 as_warn (_(".frame outside of .ent"));
17785 demand_empty_rest_of_line ();
17786 return;
17787 }
252b5132 17788
ecb4347a
DJ
17789 cur_proc_ptr->frame_reg = tc_get_register (1);
17790
17791 SKIP_WHITESPACE ();
17792 if (*input_line_pointer++ != ','
17793 || get_absolute_expression_and_terminator (&val) != ',')
17794 {
1661c76c 17795 as_warn (_("bad .frame directive"));
ecb4347a
DJ
17796 --input_line_pointer;
17797 demand_empty_rest_of_line ();
17798 return;
17799 }
252b5132 17800
ecb4347a
DJ
17801 cur_proc_ptr->frame_offset = val;
17802 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 17803
252b5132 17804 demand_empty_rest_of_line ();
252b5132 17805 }
252b5132
RH
17806}
17807
bdaaa2e1
KH
17808/* The .fmask and .mask directives. If the mdebug section is present
17809 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 17810 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 17811 information correctly. We can't use the ecoff routines because they
252b5132
RH
17812 make reference to the ecoff symbol table (in the mdebug section). */
17813
17814static void
17a2f251 17815s_mips_mask (int reg_type)
252b5132 17816{
f3ded42a
RS
17817 if (ECOFF_DEBUGGING)
17818 s_ignore (reg_type);
17819 else
252b5132 17820 {
ecb4347a 17821 long mask, off;
252b5132 17822
ecb4347a
DJ
17823 if (cur_proc_ptr == (procS *) NULL)
17824 {
17825 as_warn (_(".mask/.fmask outside of .ent"));
17826 demand_empty_rest_of_line ();
17827 return;
17828 }
252b5132 17829
ecb4347a
DJ
17830 if (get_absolute_expression_and_terminator (&mask) != ',')
17831 {
1661c76c 17832 as_warn (_("bad .mask/.fmask directive"));
ecb4347a
DJ
17833 --input_line_pointer;
17834 demand_empty_rest_of_line ();
17835 return;
17836 }
252b5132 17837
ecb4347a
DJ
17838 off = get_absolute_expression ();
17839
17840 if (reg_type == 'F')
17841 {
17842 cur_proc_ptr->fpreg_mask = mask;
17843 cur_proc_ptr->fpreg_offset = off;
17844 }
17845 else
17846 {
17847 cur_proc_ptr->reg_mask = mask;
17848 cur_proc_ptr->reg_offset = off;
17849 }
17850
17851 demand_empty_rest_of_line ();
252b5132 17852 }
252b5132
RH
17853}
17854
316f5878
RS
17855/* A table describing all the processors gas knows about. Names are
17856 matched in the order listed.
e7af610e 17857
316f5878
RS
17858 To ease comparison, please keep this table in the same order as
17859 gcc's mips_cpu_info_table[]. */
e972090a
NC
17860static const struct mips_cpu_info mips_cpu_info_table[] =
17861{
316f5878 17862 /* Entries for generic ISAs */
d16afab6
RS
17863 { "mips1", MIPS_CPU_IS_ISA, 0, ISA_MIPS1, CPU_R3000 },
17864 { "mips2", MIPS_CPU_IS_ISA, 0, ISA_MIPS2, CPU_R6000 },
17865 { "mips3", MIPS_CPU_IS_ISA, 0, ISA_MIPS3, CPU_R4000 },
17866 { "mips4", MIPS_CPU_IS_ISA, 0, ISA_MIPS4, CPU_R8000 },
17867 { "mips5", MIPS_CPU_IS_ISA, 0, ISA_MIPS5, CPU_MIPS5 },
17868 { "mips32", MIPS_CPU_IS_ISA, 0, ISA_MIPS32, CPU_MIPS32 },
17869 { "mips32r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
ae52f483
AB
17870 { "mips32r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R3, CPU_MIPS32R3 },
17871 { "mips32r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R5, CPU_MIPS32R5 },
d16afab6
RS
17872 { "mips64", MIPS_CPU_IS_ISA, 0, ISA_MIPS64, CPU_MIPS64 },
17873 { "mips64r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R2, CPU_MIPS64R2 },
ae52f483
AB
17874 { "mips64r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R3, CPU_MIPS64R3 },
17875 { "mips64r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R5, CPU_MIPS64R5 },
316f5878
RS
17876
17877 /* MIPS I */
d16afab6
RS
17878 { "r3000", 0, 0, ISA_MIPS1, CPU_R3000 },
17879 { "r2000", 0, 0, ISA_MIPS1, CPU_R3000 },
17880 { "r3900", 0, 0, ISA_MIPS1, CPU_R3900 },
316f5878
RS
17881
17882 /* MIPS II */
d16afab6 17883 { "r6000", 0, 0, ISA_MIPS2, CPU_R6000 },
316f5878
RS
17884
17885 /* MIPS III */
d16afab6
RS
17886 { "r4000", 0, 0, ISA_MIPS3, CPU_R4000 },
17887 { "r4010", 0, 0, ISA_MIPS2, CPU_R4010 },
17888 { "vr4100", 0, 0, ISA_MIPS3, CPU_VR4100 },
17889 { "vr4111", 0, 0, ISA_MIPS3, CPU_R4111 },
17890 { "vr4120", 0, 0, ISA_MIPS3, CPU_VR4120 },
17891 { "vr4130", 0, 0, ISA_MIPS3, CPU_VR4120 },
17892 { "vr4181", 0, 0, ISA_MIPS3, CPU_R4111 },
17893 { "vr4300", 0, 0, ISA_MIPS3, CPU_R4300 },
17894 { "r4400", 0, 0, ISA_MIPS3, CPU_R4400 },
17895 { "r4600", 0, 0, ISA_MIPS3, CPU_R4600 },
17896 { "orion", 0, 0, ISA_MIPS3, CPU_R4600 },
17897 { "r4650", 0, 0, ISA_MIPS3, CPU_R4650 },
17898 { "r5900", 0, 0, ISA_MIPS3, CPU_R5900 },
b15591bb 17899 /* ST Microelectronics Loongson 2E and 2F cores */
d16afab6
RS
17900 { "loongson2e", 0, 0, ISA_MIPS3, CPU_LOONGSON_2E },
17901 { "loongson2f", 0, 0, ISA_MIPS3, CPU_LOONGSON_2F },
316f5878
RS
17902
17903 /* MIPS IV */
d16afab6
RS
17904 { "r8000", 0, 0, ISA_MIPS4, CPU_R8000 },
17905 { "r10000", 0, 0, ISA_MIPS4, CPU_R10000 },
17906 { "r12000", 0, 0, ISA_MIPS4, CPU_R12000 },
17907 { "r14000", 0, 0, ISA_MIPS4, CPU_R14000 },
17908 { "r16000", 0, 0, ISA_MIPS4, CPU_R16000 },
17909 { "vr5000", 0, 0, ISA_MIPS4, CPU_R5000 },
17910 { "vr5400", 0, 0, ISA_MIPS4, CPU_VR5400 },
17911 { "vr5500", 0, 0, ISA_MIPS4, CPU_VR5500 },
17912 { "rm5200", 0, 0, ISA_MIPS4, CPU_R5000 },
17913 { "rm5230", 0, 0, ISA_MIPS4, CPU_R5000 },
17914 { "rm5231", 0, 0, ISA_MIPS4, CPU_R5000 },
17915 { "rm5261", 0, 0, ISA_MIPS4, CPU_R5000 },
17916 { "rm5721", 0, 0, ISA_MIPS4, CPU_R5000 },
17917 { "rm7000", 0, 0, ISA_MIPS4, CPU_RM7000 },
17918 { "rm9000", 0, 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
17919
17920 /* MIPS 32 */
d16afab6
RS
17921 { "4kc", 0, 0, ISA_MIPS32, CPU_MIPS32 },
17922 { "4km", 0, 0, ISA_MIPS32, CPU_MIPS32 },
17923 { "4kp", 0, 0, ISA_MIPS32, CPU_MIPS32 },
17924 { "4ksc", 0, ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
ad3fea08
TS
17925
17926 /* MIPS 32 Release 2 */
d16afab6
RS
17927 { "4kec", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17928 { "4kem", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17929 { "4kep", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17930 { "4ksd", 0, ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
17931 { "m4k", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17932 { "m4kp", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17933 { "m14k", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
17934 { "m14kc", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
17935 { "m14ke", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
17936 ISA_MIPS32R2, CPU_MIPS32R2 },
17937 { "m14kec", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
17938 ISA_MIPS32R2, CPU_MIPS32R2 },
17939 { "24kc", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17940 { "24kf2_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17941 { "24kf", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17942 { "24kf1_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 17943 /* Deprecated forms of the above. */
d16afab6
RS
17944 { "24kfx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17945 { "24kx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 17946 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
d16afab6
RS
17947 { "24kec", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
17948 { "24kef2_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
17949 { "24kef", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
17950 { "24kef1_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 17951 /* Deprecated forms of the above. */
d16afab6
RS
17952 { "24kefx", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
17953 { "24kex", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 17954 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
d16afab6
RS
17955 { "34kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17956 { "34kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17957 { "34kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17958 { "34kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 17959 /* Deprecated forms of the above. */
d16afab6
RS
17960 { "34kfx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17961 { "34kx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
711eefe4 17962 /* 34Kn is a 34kc without DSP. */
d16afab6 17963 { "34kn", 0, ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 17964 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
d16afab6
RS
17965 { "74kc", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
17966 { "74kf2_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
17967 { "74kf", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
17968 { "74kf1_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
17969 { "74kf3_2", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 17970 /* Deprecated forms of the above. */
d16afab6
RS
17971 { "74kfx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
17972 { "74kx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
30f8113a 17973 /* 1004K cores are multiprocessor versions of the 34K. */
d16afab6
RS
17974 { "1004kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17975 { "1004kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17976 { "1004kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17977 { "1004kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
bbaa46c0 17978 /* P5600 with EVA and Virtualization ASEs, other ASEs are optional. */
ae52f483 17979 { "p5600", 0, ASE_VIRT | ASE_EVA | ASE_XPA, ISA_MIPS32R5, CPU_MIPS32R5 },
32b26a03 17980
316f5878 17981 /* MIPS 64 */
d16afab6
RS
17982 { "5kc", 0, 0, ISA_MIPS64, CPU_MIPS64 },
17983 { "5kf", 0, 0, ISA_MIPS64, CPU_MIPS64 },
17984 { "20kc", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
17985 { "25kf", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
ad3fea08 17986
c7a23324 17987 /* Broadcom SB-1 CPU core */
d16afab6 17988 { "sb1", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
1e85aad8 17989 /* Broadcom SB-1A CPU core */
d16afab6 17990 { "sb1a", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
d051516a 17991
4ba154f5 17992 { "loongson3a", 0, 0, ISA_MIPS64R2, CPU_LOONGSON_3A },
e7af610e 17993
ed163775
MR
17994 /* MIPS 64 Release 2 */
17995
967344c6 17996 /* Cavium Networks Octeon CPU core */
d16afab6
RS
17997 { "octeon", 0, 0, ISA_MIPS64R2, CPU_OCTEON },
17998 { "octeon+", 0, 0, ISA_MIPS64R2, CPU_OCTEONP },
17999 { "octeon2", 0, 0, ISA_MIPS64R2, CPU_OCTEON2 },
967344c6 18000
52b6b6b9 18001 /* RMI Xlr */
d16afab6 18002 { "xlr", 0, 0, ISA_MIPS64, CPU_XLR },
52b6b6b9 18003
55a36193
MK
18004 /* Broadcom XLP.
18005 XLP is mostly like XLR, with the prominent exception that it is
18006 MIPS64R2 rather than MIPS64. */
d16afab6 18007 { "xlp", 0, 0, ISA_MIPS64R2, CPU_XLR },
55a36193 18008
316f5878 18009 /* End marker */
d16afab6 18010 { NULL, 0, 0, 0, 0 }
316f5878 18011};
e7af610e 18012
84ea6cf2 18013
316f5878
RS
18014/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
18015 with a final "000" replaced by "k". Ignore case.
e7af610e 18016
316f5878 18017 Note: this function is shared between GCC and GAS. */
c6c98b38 18018
b34976b6 18019static bfd_boolean
17a2f251 18020mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
18021{
18022 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
18023 given++, canonical++;
18024
18025 return ((*given == 0 && *canonical == 0)
18026 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
18027}
18028
18029
18030/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
18031 CPU name. We've traditionally allowed a lot of variation here.
18032
18033 Note: this function is shared between GCC and GAS. */
18034
b34976b6 18035static bfd_boolean
17a2f251 18036mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
18037{
18038 /* First see if the name matches exactly, or with a final "000"
18039 turned into "k". */
18040 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 18041 return TRUE;
316f5878
RS
18042
18043 /* If not, try comparing based on numerical designation alone.
18044 See if GIVEN is an unadorned number, or 'r' followed by a number. */
18045 if (TOLOWER (*given) == 'r')
18046 given++;
18047 if (!ISDIGIT (*given))
b34976b6 18048 return FALSE;
316f5878
RS
18049
18050 /* Skip over some well-known prefixes in the canonical name,
18051 hoping to find a number there too. */
18052 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
18053 canonical += 2;
18054 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
18055 canonical += 2;
18056 else if (TOLOWER (canonical[0]) == 'r')
18057 canonical += 1;
18058
18059 return mips_strict_matching_cpu_name_p (canonical, given);
18060}
18061
18062
18063/* Parse an option that takes the name of a processor as its argument.
18064 OPTION is the name of the option and CPU_STRING is the argument.
18065 Return the corresponding processor enumeration if the CPU_STRING is
18066 recognized, otherwise report an error and return null.
18067
18068 A similar function exists in GCC. */
e7af610e
NC
18069
18070static const struct mips_cpu_info *
17a2f251 18071mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 18072{
316f5878 18073 const struct mips_cpu_info *p;
e7af610e 18074
316f5878
RS
18075 /* 'from-abi' selects the most compatible architecture for the given
18076 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
18077 EABIs, we have to decide whether we're using the 32-bit or 64-bit
18078 version. Look first at the -mgp options, if given, otherwise base
18079 the choice on MIPS_DEFAULT_64BIT.
e7af610e 18080
316f5878
RS
18081 Treat NO_ABI like the EABIs. One reason to do this is that the
18082 plain 'mips' and 'mips64' configs have 'from-abi' as their default
18083 architecture. This code picks MIPS I for 'mips' and MIPS III for
18084 'mips64', just as we did in the days before 'from-abi'. */
18085 if (strcasecmp (cpu_string, "from-abi") == 0)
18086 {
18087 if (ABI_NEEDS_32BIT_REGS (mips_abi))
18088 return mips_cpu_info_from_isa (ISA_MIPS1);
18089
18090 if (ABI_NEEDS_64BIT_REGS (mips_abi))
18091 return mips_cpu_info_from_isa (ISA_MIPS3);
18092
0b35dfee 18093 if (file_mips_opts.gp32 >= 0)
18094 return mips_cpu_info_from_isa (file_mips_opts.gp32
18095 ? ISA_MIPS1 : ISA_MIPS3);
316f5878
RS
18096
18097 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
18098 ? ISA_MIPS3
18099 : ISA_MIPS1);
18100 }
18101
18102 /* 'default' has traditionally been a no-op. Probably not very useful. */
18103 if (strcasecmp (cpu_string, "default") == 0)
18104 return 0;
18105
18106 for (p = mips_cpu_info_table; p->name != 0; p++)
18107 if (mips_matching_cpu_name_p (p->name, cpu_string))
18108 return p;
18109
1661c76c 18110 as_bad (_("bad value (%s) for %s"), cpu_string, option);
316f5878 18111 return 0;
e7af610e
NC
18112}
18113
316f5878
RS
18114/* Return the canonical processor information for ISA (a member of the
18115 ISA_MIPS* enumeration). */
18116
e7af610e 18117static const struct mips_cpu_info *
17a2f251 18118mips_cpu_info_from_isa (int isa)
e7af610e
NC
18119{
18120 int i;
18121
18122 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
ad3fea08 18123 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
316f5878 18124 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
18125 return (&mips_cpu_info_table[i]);
18126
e972090a 18127 return NULL;
e7af610e 18128}
fef14a42
TS
18129
18130static const struct mips_cpu_info *
17a2f251 18131mips_cpu_info_from_arch (int arch)
fef14a42
TS
18132{
18133 int i;
18134
18135 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
18136 if (arch == mips_cpu_info_table[i].cpu)
18137 return (&mips_cpu_info_table[i]);
18138
18139 return NULL;
18140}
316f5878
RS
18141\f
18142static void
17a2f251 18143show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
18144{
18145 if (*first_p)
18146 {
18147 fprintf (stream, "%24s", "");
18148 *col_p = 24;
18149 }
18150 else
18151 {
18152 fprintf (stream, ", ");
18153 *col_p += 2;
18154 }
e7af610e 18155
316f5878
RS
18156 if (*col_p + strlen (string) > 72)
18157 {
18158 fprintf (stream, "\n%24s", "");
18159 *col_p = 24;
18160 }
18161
18162 fprintf (stream, "%s", string);
18163 *col_p += strlen (string);
18164
18165 *first_p = 0;
18166}
18167
18168void
17a2f251 18169md_show_usage (FILE *stream)
e7af610e 18170{
316f5878
RS
18171 int column, first;
18172 size_t i;
18173
18174 fprintf (stream, _("\
18175MIPS options:\n\
316f5878
RS
18176-EB generate big endian output\n\
18177-EL generate little endian output\n\
18178-g, -g2 do not remove unneeded NOPs or swap branches\n\
18179-G NUM allow referencing objects up to NUM bytes\n\
18180 implicitly with the gp register [default 8]\n"));
18181 fprintf (stream, _("\
18182-mips1 generate MIPS ISA I instructions\n\
18183-mips2 generate MIPS ISA II instructions\n\
18184-mips3 generate MIPS ISA III instructions\n\
18185-mips4 generate MIPS ISA IV instructions\n\
18186-mips5 generate MIPS ISA V instructions\n\
18187-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 18188-mips32r2 generate MIPS32 release 2 ISA instructions\n\
ae52f483
AB
18189-mips32r3 generate MIPS32 release 3 ISA instructions\n\
18190-mips32r5 generate MIPS32 release 5 ISA instructions\n\
316f5878 18191-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 18192-mips64r2 generate MIPS64 release 2 ISA instructions\n\
ae52f483
AB
18193-mips64r3 generate MIPS64 release 3 ISA instructions\n\
18194-mips64r5 generate MIPS64 release 5 ISA instructions\n\
316f5878
RS
18195-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
18196
18197 first = 1;
e7af610e
NC
18198
18199 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
18200 show (stream, mips_cpu_info_table[i].name, &column, &first);
18201 show (stream, "from-abi", &column, &first);
18202 fputc ('\n', stream);
e7af610e 18203
316f5878
RS
18204 fprintf (stream, _("\
18205-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
18206-no-mCPU don't generate code specific to CPU.\n\
18207 For -mCPU and -no-mCPU, CPU must be one of:\n"));
18208
18209 first = 1;
18210
18211 show (stream, "3900", &column, &first);
18212 show (stream, "4010", &column, &first);
18213 show (stream, "4100", &column, &first);
18214 show (stream, "4650", &column, &first);
18215 fputc ('\n', stream);
18216
18217 fprintf (stream, _("\
18218-mips16 generate mips16 instructions\n\
18219-no-mips16 do not generate mips16 instructions\n"));
18220 fprintf (stream, _("\
df58fc94
RS
18221-mmicromips generate microMIPS instructions\n\
18222-mno-micromips do not generate microMIPS instructions\n"));
18223 fprintf (stream, _("\
e16bfa71
TS
18224-msmartmips generate smartmips instructions\n\
18225-mno-smartmips do not generate smartmips instructions\n"));
18226 fprintf (stream, _("\
74cd071d
CF
18227-mdsp generate DSP instructions\n\
18228-mno-dsp do not generate DSP instructions\n"));
18229 fprintf (stream, _("\
8b082fb1
TS
18230-mdspr2 generate DSP R2 instructions\n\
18231-mno-dspr2 do not generate DSP R2 instructions\n"));
18232 fprintf (stream, _("\
ef2e4d86
CF
18233-mmt generate MT instructions\n\
18234-mno-mt do not generate MT instructions\n"));
18235 fprintf (stream, _("\
dec0624d
MR
18236-mmcu generate MCU instructions\n\
18237-mno-mcu do not generate MCU instructions\n"));
18238 fprintf (stream, _("\
56d438b1
CF
18239-mmsa generate MSA instructions\n\
18240-mno-msa do not generate MSA instructions\n"));
18241 fprintf (stream, _("\
7d64c587
AB
18242-mxpa generate eXtended Physical Address (XPA) instructions\n\
18243-mno-xpa do not generate eXtended Physical Address (XPA) instructions\n"));
18244 fprintf (stream, _("\
b015e599
AP
18245-mvirt generate Virtualization instructions\n\
18246-mno-virt do not generate Virtualization instructions\n"));
18247 fprintf (stream, _("\
833794fc
MR
18248-minsn32 only generate 32-bit microMIPS instructions\n\
18249-mno-insn32 generate all microMIPS instructions\n"));
18250 fprintf (stream, _("\
c67a084a
NC
18251-mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
18252-mfix-loongson2f-nop work around Loongson2F NOP errata\n\
d766e8ec 18253-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 18254-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
6a32d874 18255-mfix-24k insert a nop after ERET and DERET instructions\n\
d954098f 18256-mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
316f5878
RS
18257-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
18258-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261 18259-msym32 assume all symbols have 32-bit values\n\
316f5878
RS
18260-O0 remove unneeded NOPs, do not swap branches\n\
18261-O remove unneeded NOPs and swap branches\n\
316f5878
RS
18262--trap, --no-break trap exception on div by 0 and mult overflow\n\
18263--break, --no-trap break exception on div by 0 and mult overflow\n"));
037b32b9
AN
18264 fprintf (stream, _("\
18265-mhard-float allow floating-point instructions\n\
18266-msoft-float do not allow floating-point instructions\n\
18267-msingle-float only allow 32-bit floating-point operations\n\
18268-mdouble-float allow 32-bit and 64-bit floating-point operations\n\
3bf0dbfb 18269--[no-]construct-floats [dis]allow floating point values to be constructed\n\
ba92f887
MR
18270--[no-]relax-branch [dis]allow out-of-range branches to be relaxed\n\
18271-mnan=ENCODING select an IEEE 754 NaN encoding convention, either of:\n"));
18272
18273 first = 1;
18274
18275 show (stream, "legacy", &column, &first);
18276 show (stream, "2008", &column, &first);
18277
18278 fputc ('\n', stream);
18279
316f5878
RS
18280 fprintf (stream, _("\
18281-KPIC, -call_shared generate SVR4 position independent code\n\
861fb55a 18282-call_nonpic generate non-PIC code that can operate with DSOs\n\
0c000745 18283-mvxworks-pic generate VxWorks position independent code\n\
861fb55a 18284-non_shared do not generate code that can operate with DSOs\n\
316f5878 18285-xgot assume a 32 bit GOT\n\
dcd410fe 18286-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8 18287-mshared, -mno-shared disable/enable .cpload optimization for\n\
d821e36b 18288 position dependent (non shared) code\n\
316f5878
RS
18289-mabi=ABI create ABI conformant object file for:\n"));
18290
18291 first = 1;
18292
18293 show (stream, "32", &column, &first);
18294 show (stream, "o64", &column, &first);
18295 show (stream, "n32", &column, &first);
18296 show (stream, "64", &column, &first);
18297 show (stream, "eabi", &column, &first);
18298
18299 fputc ('\n', stream);
18300
18301 fprintf (stream, _("\
18302-32 create o32 ABI object file (default)\n\
18303-n32 create n32 ABI object file\n\
18304-64 create 64 ABI object file\n"));
e7af610e 18305}
14e777e0 18306
1575952e 18307#ifdef TE_IRIX
14e777e0 18308enum dwarf2_format
413a266c 18309mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
14e777e0 18310{
369943fe 18311 if (HAVE_64BIT_SYMBOLS)
1575952e 18312 return dwarf2_format_64bit_irix;
14e777e0
KB
18313 else
18314 return dwarf2_format_32bit;
18315}
1575952e 18316#endif
73369e65
EC
18317
18318int
18319mips_dwarf2_addr_size (void)
18320{
6b6b3450 18321 if (HAVE_64BIT_OBJECTS)
73369e65 18322 return 8;
73369e65
EC
18323 else
18324 return 4;
18325}
5862107c
EC
18326
18327/* Standard calling conventions leave the CFA at SP on entry. */
18328void
18329mips_cfi_frame_initial_instructions (void)
18330{
18331 cfi_add_CFA_def_cfa_register (SP);
18332}
18333
707bfff6
TS
18334int
18335tc_mips_regname_to_dw2regnum (char *regname)
18336{
18337 unsigned int regnum = -1;
18338 unsigned int reg;
18339
18340 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
18341 regnum = reg;
18342
18343 return regnum;
18344}
263b2574 18345
18346/* Implement CONVERT_SYMBOLIC_ATTRIBUTE.
18347 Given a symbolic attribute NAME, return the proper integer value.
18348 Returns -1 if the attribute is not known. */
18349
18350int
18351mips_convert_symbolic_attribute (const char *name)
18352{
18353 static const struct
18354 {
18355 const char * name;
18356 const int tag;
18357 }
18358 attribute_table[] =
18359 {
18360#define T(tag) {#tag, tag}
18361 T (Tag_GNU_MIPS_ABI_FP),
18362 T (Tag_GNU_MIPS_ABI_MSA),
18363#undef T
18364 };
18365 unsigned int i;
18366
18367 if (name == NULL)
18368 return -1;
18369
18370 for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
18371 if (streq (name, attribute_table[i].name))
18372 return attribute_table[i].tag;
18373
18374 return -1;
18375}
This page took 4.385175 seconds and 4 git commands to generate.