Prevent the V850 assembler from generating an internal error if it is asked to
[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. */
bad1aba3 244 int gp;
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.
bad1aba3 266 Note that we must set the isa field to ISA_UNKNOWN and the ASE, gp and
0b35dfee 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,
bad1aba3 274 /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
0b35dfee 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,
bad1aba3 285 /* gp */ -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
bad1aba3 416#define GPR_SIZE \
417 (mips_opts.gp == 64 && !ISA_HAS_64BIT_REGS (mips_opts.isa) \
418 ? 32 \
419 : mips_opts.gp)
ca4e0257 420
bad1aba3 421#define FPR_SIZE \
422 (mips_opts.fp == 64 && !ISA_HAS_64BIT_FPRS (mips_opts.isa) \
423 ? 32 \
424 : mips_opts.fp)
ca4e0257 425
316f5878 426#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
e013f690 427
316f5878 428#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
e013f690 429
3b91255e
RS
430/* True if relocations are stored in-place. */
431#define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
432
aed1a261
RS
433/* The ABI-derived address size. */
434#define HAVE_64BIT_ADDRESSES \
bad1aba3 435 (GPR_SIZE == 64 && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
aed1a261 436#define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
e013f690 437
aed1a261
RS
438/* The size of symbolic constants (i.e., expressions of the form
439 "SYMBOL" or "SYMBOL + OFFSET"). */
440#define HAVE_32BIT_SYMBOLS \
441 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
442#define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
ca4e0257 443
b7c7d6c1
TS
444/* Addresses are loaded in different ways, depending on the address size
445 in use. The n32 ABI Documentation also mandates the use of additions
446 with overflow checking, but existing implementations don't follow it. */
f899b4b8 447#define ADDRESS_ADD_INSN \
b7c7d6c1 448 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
f899b4b8
TS
449
450#define ADDRESS_ADDI_INSN \
b7c7d6c1 451 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
f899b4b8
TS
452
453#define ADDRESS_LOAD_INSN \
454 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
455
456#define ADDRESS_STORE_INSN \
457 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
458
a4672219 459/* Return true if the given CPU supports the MIPS16 ASE. */
3396de36
TS
460#define CPU_HAS_MIPS16(cpu) \
461 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
462 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
a4672219 463
2309ddf2 464/* Return true if the given CPU supports the microMIPS ASE. */
df58fc94
RS
465#define CPU_HAS_MICROMIPS(cpu) 0
466
60b63b72
RS
467/* True if CPU has a dror instruction. */
468#define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
469
470/* True if CPU has a ror instruction. */
471#define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
472
dd6a37e7 473/* True if CPU is in the Octeon family */
432233b3 474#define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP || (CPU) == CPU_OCTEON2)
dd6a37e7 475
dd3cbb7e 476/* True if CPU has seq/sne and seqi/snei instructions. */
dd6a37e7 477#define CPU_HAS_SEQ(CPU) (CPU_IS_OCTEON (CPU))
dd3cbb7e 478
0aa27725
RS
479/* True, if CPU has support for ldc1 and sdc1. */
480#define CPU_HAS_LDC1_SDC1(CPU) \
481 ((mips_opts.isa != ISA_MIPS1) && ((CPU) != CPU_R5900))
482
c8978940
CD
483/* True if mflo and mfhi can be immediately followed by instructions
484 which write to the HI and LO registers.
485
486 According to MIPS specifications, MIPS ISAs I, II, and III need
487 (at least) two instructions between the reads of HI/LO and
488 instructions which write them, and later ISAs do not. Contradicting
489 the MIPS specifications, some MIPS IV processor user manuals (e.g.
490 the UM for the NEC Vr5000) document needing the instructions between
491 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
492 MIPS64 and later ISAs to have the interlocks, plus any specific
493 earlier-ISA CPUs for which CPU documentation declares that the
494 instructions are really interlocked. */
495#define hilo_interlocks \
496 (mips_opts.isa == ISA_MIPS32 \
497 || mips_opts.isa == ISA_MIPS32R2 \
ae52f483
AB
498 || mips_opts.isa == ISA_MIPS32R3 \
499 || mips_opts.isa == ISA_MIPS32R5 \
c8978940
CD
500 || mips_opts.isa == ISA_MIPS64 \
501 || mips_opts.isa == ISA_MIPS64R2 \
ae52f483
AB
502 || mips_opts.isa == ISA_MIPS64R3 \
503 || mips_opts.isa == ISA_MIPS64R5 \
c8978940 504 || mips_opts.arch == CPU_R4010 \
e407c74b 505 || mips_opts.arch == CPU_R5900 \
c8978940
CD
506 || mips_opts.arch == CPU_R10000 \
507 || mips_opts.arch == CPU_R12000 \
3aa3176b
TS
508 || mips_opts.arch == CPU_R14000 \
509 || mips_opts.arch == CPU_R16000 \
c8978940 510 || mips_opts.arch == CPU_RM7000 \
c8978940 511 || mips_opts.arch == CPU_VR5500 \
df58fc94 512 || mips_opts.micromips \
c8978940 513 )
252b5132
RH
514
515/* Whether the processor uses hardware interlocks to protect reads
81912461
ILT
516 from the GPRs after they are loaded from memory, and thus does not
517 require nops to be inserted. This applies to instructions marked
67dc82bc 518 INSN_LOAD_MEMORY. These nops are only required at MIPS ISA
df58fc94
RS
519 level I and microMIPS mode instructions are always interlocked. */
520#define gpr_interlocks \
521 (mips_opts.isa != ISA_MIPS1 \
522 || mips_opts.arch == CPU_R3900 \
e407c74b 523 || mips_opts.arch == CPU_R5900 \
df58fc94
RS
524 || mips_opts.micromips \
525 )
252b5132 526
81912461
ILT
527/* Whether the processor uses hardware interlocks to avoid delays
528 required by coprocessor instructions, and thus does not require
529 nops to be inserted. This applies to instructions marked
530 INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
531 between instructions marked INSN_WRITE_COND_CODE and ones marked
532 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
df58fc94
RS
533 levels I, II, and III and microMIPS mode instructions are always
534 interlocked. */
bdaaa2e1 535/* Itbl support may require additional care here. */
81912461
ILT
536#define cop_interlocks \
537 ((mips_opts.isa != ISA_MIPS1 \
538 && mips_opts.isa != ISA_MIPS2 \
539 && mips_opts.isa != ISA_MIPS3) \
540 || mips_opts.arch == CPU_R4300 \
df58fc94 541 || mips_opts.micromips \
81912461
ILT
542 )
543
544/* Whether the processor uses hardware interlocks to protect reads
545 from coprocessor registers after they are loaded from memory, and
546 thus does not require nops to be inserted. This applies to
547 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
df58fc94
RS
548 requires at MIPS ISA level I and microMIPS mode instructions are
549 always interlocked. */
550#define cop_mem_interlocks \
551 (mips_opts.isa != ISA_MIPS1 \
552 || mips_opts.micromips \
553 )
252b5132 554
6b76fefe
CM
555/* Is this a mfhi or mflo instruction? */
556#define MF_HILO_INSN(PINFO) \
b19e8a9b
AN
557 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
558
df58fc94
RS
559/* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
560 has been selected. This implies, in particular, that addresses of text
561 labels have their LSB set. */
562#define HAVE_CODE_COMPRESSION \
563 ((mips_opts.mips16 | mips_opts.micromips) != 0)
564
42429eac 565/* The minimum and maximum signed values that can be stored in a GPR. */
bad1aba3 566#define GPR_SMAX ((offsetT) (((valueT) 1 << (GPR_SIZE - 1)) - 1))
42429eac
RS
567#define GPR_SMIN (-GPR_SMAX - 1)
568
252b5132
RH
569/* MIPS PIC level. */
570
a161fe53 571enum mips_pic_level mips_pic;
252b5132 572
c9914766 573/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 574 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 575static int mips_big_got = 0;
252b5132
RH
576
577/* 1 if trap instructions should used for overflow rather than break
578 instructions. */
c9914766 579static int mips_trap = 0;
252b5132 580
119d663a 581/* 1 if double width floating point constants should not be constructed
b6ff326e 582 by assembling two single width halves into two single width floating
119d663a
NC
583 point registers which just happen to alias the double width destination
584 register. On some architectures this aliasing can be disabled by a bit
d547a75e 585 in the status register, and the setting of this bit cannot be determined
119d663a
NC
586 automatically at assemble time. */
587static int mips_disable_float_construction;
588
252b5132
RH
589/* Non-zero if any .set noreorder directives were used. */
590
591static int mips_any_noreorder;
592
6b76fefe
CM
593/* Non-zero if nops should be inserted when the register referenced in
594 an mfhi/mflo instruction is read in the next two instructions. */
595static int mips_7000_hilo_fix;
596
02ffd3e4 597/* The size of objects in the small data section. */
156c2f8b 598static unsigned int g_switch_value = 8;
252b5132
RH
599/* Whether the -G option was used. */
600static int g_switch_seen = 0;
601
602#define N_RMASK 0xc4
603#define N_VFP 0xd4
604
605/* If we can determine in advance that GP optimization won't be
606 possible, we can skip the relaxation stuff that tries to produce
607 GP-relative references. This makes delay slot optimization work
608 better.
609
610 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
611 gcc output. It needs to guess right for gcc, otherwise gcc
612 will put what it thinks is a GP-relative instruction in a branch
613 delay slot.
252b5132
RH
614
615 I don't know if a fix is needed for the SVR4_PIC mode. I've only
616 fixed it for the non-PIC mode. KR 95/04/07 */
17a2f251 617static int nopic_need_relax (symbolS *, int);
252b5132
RH
618
619/* handle of the OPCODE hash table */
620static struct hash_control *op_hash = NULL;
621
622/* The opcode hash table we use for the mips16. */
623static struct hash_control *mips16_op_hash = NULL;
624
df58fc94
RS
625/* The opcode hash table we use for the microMIPS ASE. */
626static struct hash_control *micromips_op_hash = NULL;
627
252b5132
RH
628/* This array holds the chars that always start a comment. If the
629 pre-processor is disabled, these aren't very useful */
630const char comment_chars[] = "#";
631
632/* This array holds the chars that only start a comment at the beginning of
633 a line. If the line seems to have the form '# 123 filename'
634 .line and .file directives will appear in the pre-processed output */
635/* Note that input_file.c hand checks for '#' at the beginning of the
636 first line of the input file. This is because the compiler outputs
bdaaa2e1 637 #NO_APP at the beginning of its output. */
252b5132
RH
638/* Also note that C style comments are always supported. */
639const char line_comment_chars[] = "#";
640
bdaaa2e1 641/* This array holds machine specific line separator characters. */
63a0b638 642const char line_separator_chars[] = ";";
252b5132
RH
643
644/* Chars that can be used to separate mant from exp in floating point nums */
645const char EXP_CHARS[] = "eE";
646
647/* Chars that mean this number is a floating point constant */
648/* As in 0f12.456 */
649/* or 0d1.2345e12 */
650const char FLT_CHARS[] = "rRsSfFdDxXpP";
651
652/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
653 changed in read.c . Ideally it shouldn't have to know about it at all,
654 but nothing is ideal around here.
655 */
656
e3de51ce
RS
657/* Types of printf format used for instruction-related error messages.
658 "I" means int ("%d") and "S" means string ("%s"). */
659enum mips_insn_error_format {
660 ERR_FMT_PLAIN,
661 ERR_FMT_I,
662 ERR_FMT_SS,
663};
664
665/* Information about an error that was found while assembling the current
666 instruction. */
667struct mips_insn_error {
668 /* We sometimes need to match an instruction against more than one
669 opcode table entry. Errors found during this matching are reported
670 against a particular syntactic argument rather than against the
671 instruction as a whole. We grade these messages so that errors
672 against argument N have a greater priority than an error against
673 any argument < N, since the former implies that arguments up to N
674 were acceptable and that the opcode entry was therefore a closer match.
675 If several matches report an error against the same argument,
676 we only use that error if it is the same in all cases.
677
678 min_argnum is the minimum argument number for which an error message
679 should be accepted. It is 0 if MSG is against the instruction as
680 a whole. */
681 int min_argnum;
682
683 /* The printf()-style message, including its format and arguments. */
684 enum mips_insn_error_format format;
685 const char *msg;
686 union {
687 int i;
688 const char *ss[2];
689 } u;
690};
691
692/* The error that should be reported for the current instruction. */
693static struct mips_insn_error insn_error;
252b5132
RH
694
695static int auto_align = 1;
696
697/* When outputting SVR4 PIC code, the assembler needs to know the
698 offset in the stack frame from which to restore the $gp register.
699 This is set by the .cprestore pseudo-op, and saved in this
700 variable. */
701static offsetT mips_cprestore_offset = -1;
702
67c1ffbe 703/* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
6478892d 704 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 705 offset and even an other register than $gp as global pointer. */
6478892d
TS
706static offsetT mips_cpreturn_offset = -1;
707static int mips_cpreturn_register = -1;
708static int mips_gp_register = GP;
def2e0dd 709static int mips_gprel_offset = 0;
6478892d 710
7a621144
DJ
711/* Whether mips_cprestore_offset has been set in the current function
712 (or whether it has already been warned about, if not). */
713static int mips_cprestore_valid = 0;
714
252b5132
RH
715/* This is the register which holds the stack frame, as set by the
716 .frame pseudo-op. This is needed to implement .cprestore. */
717static int mips_frame_reg = SP;
718
7a621144
DJ
719/* Whether mips_frame_reg has been set in the current function
720 (or whether it has already been warned about, if not). */
721static int mips_frame_reg_valid = 0;
722
252b5132
RH
723/* To output NOP instructions correctly, we need to keep information
724 about the previous two instructions. */
725
726/* Whether we are optimizing. The default value of 2 means to remove
727 unneeded NOPs and swap branch instructions when possible. A value
728 of 1 means to not swap branches. A value of 0 means to always
729 insert NOPs. */
730static int mips_optimize = 2;
731
732/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
733 equivalent to seeing no -g option at all. */
734static int mips_debug = 0;
735
7d8e00cf
RS
736/* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
737#define MAX_VR4130_NOPS 4
738
739/* The maximum number of NOPs needed to fill delay slots. */
740#define MAX_DELAY_NOPS 2
741
742/* The maximum number of NOPs needed for any purpose. */
743#define MAX_NOPS 4
71400594
RS
744
745/* A list of previous instructions, with index 0 being the most recent.
746 We need to look back MAX_NOPS instructions when filling delay slots
747 or working around processor errata. We need to look back one
748 instruction further if we're thinking about using history[0] to
749 fill a branch delay slot. */
750static struct mips_cl_insn history[1 + MAX_NOPS];
252b5132 751
fc76e730 752/* Arrays of operands for each instruction. */
14daeee3 753#define MAX_OPERANDS 6
fc76e730
RS
754struct mips_operand_array {
755 const struct mips_operand *operand[MAX_OPERANDS];
756};
757static struct mips_operand_array *mips_operands;
758static struct mips_operand_array *mips16_operands;
759static struct mips_operand_array *micromips_operands;
760
1e915849 761/* Nop instructions used by emit_nop. */
df58fc94
RS
762static struct mips_cl_insn nop_insn;
763static struct mips_cl_insn mips16_nop_insn;
764static struct mips_cl_insn micromips_nop16_insn;
765static struct mips_cl_insn micromips_nop32_insn;
1e915849
RS
766
767/* The appropriate nop for the current mode. */
833794fc
MR
768#define NOP_INSN (mips_opts.mips16 \
769 ? &mips16_nop_insn \
770 : (mips_opts.micromips \
771 ? (mips_opts.insn32 \
772 ? &micromips_nop32_insn \
773 : &micromips_nop16_insn) \
774 : &nop_insn))
df58fc94
RS
775
776/* The size of NOP_INSN in bytes. */
833794fc
MR
777#define NOP_INSN_SIZE ((mips_opts.mips16 \
778 || (mips_opts.micromips && !mips_opts.insn32)) \
779 ? 2 : 4)
252b5132 780
252b5132
RH
781/* If this is set, it points to a frag holding nop instructions which
782 were inserted before the start of a noreorder section. If those
783 nops turn out to be unnecessary, the size of the frag can be
784 decreased. */
785static fragS *prev_nop_frag;
786
787/* The number of nop instructions we created in prev_nop_frag. */
788static int prev_nop_frag_holds;
789
790/* The number of nop instructions that we know we need in
bdaaa2e1 791 prev_nop_frag. */
252b5132
RH
792static int prev_nop_frag_required;
793
794/* The number of instructions we've seen since prev_nop_frag. */
795static int prev_nop_frag_since;
796
e8044f35
RS
797/* Relocations against symbols are sometimes done in two parts, with a HI
798 relocation and a LO relocation. Each relocation has only 16 bits of
799 space to store an addend. This means that in order for the linker to
800 handle carries correctly, it must be able to locate both the HI and
801 the LO relocation. This means that the relocations must appear in
802 order in the relocation table.
252b5132
RH
803
804 In order to implement this, we keep track of each unmatched HI
805 relocation. We then sort them so that they immediately precede the
bdaaa2e1 806 corresponding LO relocation. */
252b5132 807
e972090a
NC
808struct mips_hi_fixup
809{
252b5132
RH
810 /* Next HI fixup. */
811 struct mips_hi_fixup *next;
812 /* This fixup. */
813 fixS *fixp;
814 /* The section this fixup is in. */
815 segT seg;
816};
817
818/* The list of unmatched HI relocs. */
819
820static struct mips_hi_fixup *mips_hi_fixup_list;
821
64bdfcaf
RS
822/* The frag containing the last explicit relocation operator.
823 Null if explicit relocations have not been used. */
824
825static fragS *prev_reloc_op_frag;
826
252b5132
RH
827/* Map mips16 register numbers to normal MIPS register numbers. */
828
e972090a
NC
829static const unsigned int mips16_to_32_reg_map[] =
830{
252b5132
RH
831 16, 17, 2, 3, 4, 5, 6, 7
832};
60b63b72 833
df58fc94
RS
834/* Map microMIPS register numbers to normal MIPS register numbers. */
835
df58fc94 836#define micromips_to_32_reg_d_map mips16_to_32_reg_map
df58fc94
RS
837
838/* The microMIPS registers with type h. */
e76ff5ab 839static const unsigned int micromips_to_32_reg_h_map1[] =
df58fc94
RS
840{
841 5, 5, 6, 4, 4, 4, 4, 4
842};
e76ff5ab 843static const unsigned int micromips_to_32_reg_h_map2[] =
df58fc94
RS
844{
845 6, 7, 7, 21, 22, 5, 6, 7
846};
847
df58fc94
RS
848/* The microMIPS registers with type m. */
849static const unsigned int micromips_to_32_reg_m_map[] =
850{
851 0, 17, 2, 3, 16, 18, 19, 20
852};
853
854#define micromips_to_32_reg_n_map micromips_to_32_reg_m_map
855
71400594
RS
856/* Classifies the kind of instructions we're interested in when
857 implementing -mfix-vr4120. */
c67a084a
NC
858enum fix_vr4120_class
859{
71400594
RS
860 FIX_VR4120_MACC,
861 FIX_VR4120_DMACC,
862 FIX_VR4120_MULT,
863 FIX_VR4120_DMULT,
864 FIX_VR4120_DIV,
865 FIX_VR4120_MTHILO,
866 NUM_FIX_VR4120_CLASSES
867};
868
c67a084a
NC
869/* ...likewise -mfix-loongson2f-jump. */
870static bfd_boolean mips_fix_loongson2f_jump;
871
872/* ...likewise -mfix-loongson2f-nop. */
873static bfd_boolean mips_fix_loongson2f_nop;
874
875/* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed. */
876static bfd_boolean mips_fix_loongson2f;
877
71400594
RS
878/* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
879 there must be at least one other instruction between an instruction
880 of type X and an instruction of type Y. */
881static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
882
883/* True if -mfix-vr4120 is in force. */
d766e8ec 884static int mips_fix_vr4120;
4a6a3df4 885
7d8e00cf
RS
886/* ...likewise -mfix-vr4130. */
887static int mips_fix_vr4130;
888
6a32d874
CM
889/* ...likewise -mfix-24k. */
890static int mips_fix_24k;
891
a8d14a88
CM
892/* ...likewise -mfix-rm7000 */
893static int mips_fix_rm7000;
894
d954098f
DD
895/* ...likewise -mfix-cn63xxp1 */
896static bfd_boolean mips_fix_cn63xxp1;
897
4a6a3df4
AO
898/* We don't relax branches by default, since this causes us to expand
899 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
900 fail to compute the offset before expanding the macro to the most
901 efficient expansion. */
902
903static int mips_relax_branch;
252b5132 904\f
4d7206a2
RS
905/* The expansion of many macros depends on the type of symbol that
906 they refer to. For example, when generating position-dependent code,
907 a macro that refers to a symbol may have two different expansions,
908 one which uses GP-relative addresses and one which uses absolute
909 addresses. When generating SVR4-style PIC, a macro may have
910 different expansions for local and global symbols.
911
912 We handle these situations by generating both sequences and putting
913 them in variant frags. In position-dependent code, the first sequence
914 will be the GP-relative one and the second sequence will be the
915 absolute one. In SVR4 PIC, the first sequence will be for global
916 symbols and the second will be for local symbols.
917
584892a6
RS
918 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
919 SECOND are the lengths of the two sequences in bytes. These fields
920 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
921 the subtype has the following flags:
4d7206a2 922
584892a6
RS
923 RELAX_USE_SECOND
924 Set if it has been decided that we should use the second
925 sequence instead of the first.
926
927 RELAX_SECOND_LONGER
928 Set in the first variant frag if the macro's second implementation
929 is longer than its first. This refers to the macro as a whole,
930 not an individual relaxation.
931
932 RELAX_NOMACRO
933 Set in the first variant frag if the macro appeared in a .set nomacro
934 block and if one alternative requires a warning but the other does not.
935
936 RELAX_DELAY_SLOT
937 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
938 delay slot.
4d7206a2 939
df58fc94
RS
940 RELAX_DELAY_SLOT_16BIT
941 Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
942 16-bit instruction.
943
944 RELAX_DELAY_SLOT_SIZE_FIRST
945 Like RELAX_DELAY_SLOT, but indicates that the first implementation of
946 the macro is of the wrong size for the branch delay slot.
947
948 RELAX_DELAY_SLOT_SIZE_SECOND
949 Like RELAX_DELAY_SLOT, but indicates that the second implementation of
950 the macro is of the wrong size for the branch delay slot.
951
4d7206a2
RS
952 The frag's "opcode" points to the first fixup for relaxable code.
953
954 Relaxable macros are generated using a sequence such as:
955
956 relax_start (SYMBOL);
957 ... generate first expansion ...
958 relax_switch ();
959 ... generate second expansion ...
960 relax_end ();
961
962 The code and fixups for the unwanted alternative are discarded
963 by md_convert_frag. */
584892a6 964#define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
4d7206a2 965
584892a6
RS
966#define RELAX_FIRST(X) (((X) >> 8) & 0xff)
967#define RELAX_SECOND(X) ((X) & 0xff)
968#define RELAX_USE_SECOND 0x10000
969#define RELAX_SECOND_LONGER 0x20000
970#define RELAX_NOMACRO 0x40000
971#define RELAX_DELAY_SLOT 0x80000
df58fc94
RS
972#define RELAX_DELAY_SLOT_16BIT 0x100000
973#define RELAX_DELAY_SLOT_SIZE_FIRST 0x200000
974#define RELAX_DELAY_SLOT_SIZE_SECOND 0x400000
252b5132 975
4a6a3df4
AO
976/* Branch without likely bit. If label is out of range, we turn:
977
978 beq reg1, reg2, label
979 delay slot
980
981 into
982
983 bne reg1, reg2, 0f
984 nop
985 j label
986 0: delay slot
987
988 with the following opcode replacements:
989
990 beq <-> bne
991 blez <-> bgtz
992 bltz <-> bgez
993 bc1f <-> bc1t
994
995 bltzal <-> bgezal (with jal label instead of j label)
996
997 Even though keeping the delay slot instruction in the delay slot of
998 the branch would be more efficient, it would be very tricky to do
999 correctly, because we'd have to introduce a variable frag *after*
1000 the delay slot instruction, and expand that instead. Let's do it
1001 the easy way for now, even if the branch-not-taken case now costs
1002 one additional instruction. Out-of-range branches are not supposed
1003 to be common, anyway.
1004
1005 Branch likely. If label is out of range, we turn:
1006
1007 beql reg1, reg2, label
1008 delay slot (annulled if branch not taken)
1009
1010 into
1011
1012 beql reg1, reg2, 1f
1013 nop
1014 beql $0, $0, 2f
1015 nop
1016 1: j[al] label
1017 delay slot (executed only if branch taken)
1018 2:
1019
1020 It would be possible to generate a shorter sequence by losing the
1021 likely bit, generating something like:
b34976b6 1022
4a6a3df4
AO
1023 bne reg1, reg2, 0f
1024 nop
1025 j[al] label
1026 delay slot (executed only if branch taken)
1027 0:
1028
1029 beql -> bne
1030 bnel -> beq
1031 blezl -> bgtz
1032 bgtzl -> blez
1033 bltzl -> bgez
1034 bgezl -> bltz
1035 bc1fl -> bc1t
1036 bc1tl -> bc1f
1037
1038 bltzall -> bgezal (with jal label instead of j label)
1039 bgezall -> bltzal (ditto)
1040
1041
1042 but it's not clear that it would actually improve performance. */
66b3e8da
MR
1043#define RELAX_BRANCH_ENCODE(at, uncond, likely, link, toofar) \
1044 ((relax_substateT) \
1045 (0xc0000000 \
1046 | ((at) & 0x1f) \
1047 | ((toofar) ? 0x20 : 0) \
1048 | ((link) ? 0x40 : 0) \
1049 | ((likely) ? 0x80 : 0) \
1050 | ((uncond) ? 0x100 : 0)))
4a6a3df4 1051#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
66b3e8da
MR
1052#define RELAX_BRANCH_UNCOND(i) (((i) & 0x100) != 0)
1053#define RELAX_BRANCH_LIKELY(i) (((i) & 0x80) != 0)
1054#define RELAX_BRANCH_LINK(i) (((i) & 0x40) != 0)
1055#define RELAX_BRANCH_TOOFAR(i) (((i) & 0x20) != 0)
1056#define RELAX_BRANCH_AT(i) ((i) & 0x1f)
4a6a3df4 1057
252b5132
RH
1058/* For mips16 code, we use an entirely different form of relaxation.
1059 mips16 supports two versions of most instructions which take
1060 immediate values: a small one which takes some small value, and a
1061 larger one which takes a 16 bit value. Since branches also follow
1062 this pattern, relaxing these values is required.
1063
1064 We can assemble both mips16 and normal MIPS code in a single
1065 object. Therefore, we need to support this type of relaxation at
1066 the same time that we support the relaxation described above. We
1067 use the high bit of the subtype field to distinguish these cases.
1068
1069 The information we store for this type of relaxation is the
1070 argument code found in the opcode file for this relocation, whether
1071 the user explicitly requested a small or extended form, and whether
1072 the relocation is in a jump or jal delay slot. That tells us the
1073 size of the value, and how it should be stored. We also store
1074 whether the fragment is considered to be extended or not. We also
1075 store whether this is known to be a branch to a different section,
1076 whether we have tried to relax this frag yet, and whether we have
1077 ever extended a PC relative fragment because of a shift count. */
1078#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
1079 (0x80000000 \
1080 | ((type) & 0xff) \
1081 | ((small) ? 0x100 : 0) \
1082 | ((ext) ? 0x200 : 0) \
1083 | ((dslot) ? 0x400 : 0) \
1084 | ((jal_dslot) ? 0x800 : 0))
4a6a3df4 1085#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132
RH
1086#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
1087#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
1088#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
1089#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
1090#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
1091#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
1092#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
1093#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
1094#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
1095#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
1096#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
885add95 1097
df58fc94
RS
1098/* For microMIPS code, we use relaxation similar to one we use for
1099 MIPS16 code. Some instructions that take immediate values support
1100 two encodings: a small one which takes some small value, and a
1101 larger one which takes a 16 bit value. As some branches also follow
1102 this pattern, relaxing these values is required.
1103
1104 We can assemble both microMIPS and normal MIPS code in a single
1105 object. Therefore, we need to support this type of relaxation at
1106 the same time that we support the relaxation described above. We
1107 use one of the high bits of the subtype field to distinguish these
1108 cases.
1109
1110 The information we store for this type of relaxation is the argument
1111 code found in the opcode file for this relocation, the register
40209cad
MR
1112 selected as the assembler temporary, whether the branch is
1113 unconditional, whether it is compact, whether it stores the link
1114 address implicitly in $ra, whether relaxation of out-of-range 32-bit
1115 branches to a sequence of instructions is enabled, and whether the
1116 displacement of a branch is too large to fit as an immediate argument
1117 of a 16-bit and a 32-bit branch, respectively. */
1118#define RELAX_MICROMIPS_ENCODE(type, at, uncond, compact, link, \
1119 relax32, toofar16, toofar32) \
1120 (0x40000000 \
1121 | ((type) & 0xff) \
1122 | (((at) & 0x1f) << 8) \
1123 | ((uncond) ? 0x2000 : 0) \
1124 | ((compact) ? 0x4000 : 0) \
1125 | ((link) ? 0x8000 : 0) \
1126 | ((relax32) ? 0x10000 : 0) \
1127 | ((toofar16) ? 0x20000 : 0) \
1128 | ((toofar32) ? 0x40000 : 0))
df58fc94
RS
1129#define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1130#define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1131#define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
40209cad
MR
1132#define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x2000) != 0)
1133#define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x4000) != 0)
1134#define RELAX_MICROMIPS_LINK(i) (((i) & 0x8000) != 0)
1135#define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x10000) != 0)
1136
1137#define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x20000) != 0)
1138#define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x20000)
1139#define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x20000)
1140#define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x40000) != 0)
1141#define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x40000)
1142#define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x40000)
df58fc94 1143
43c0598f
RS
1144/* Sign-extend 16-bit value X. */
1145#define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1146
885add95
CD
1147/* Is the given value a sign-extended 32-bit value? */
1148#define IS_SEXT_32BIT_NUM(x) \
1149 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1150 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1151
1152/* Is the given value a sign-extended 16-bit value? */
1153#define IS_SEXT_16BIT_NUM(x) \
1154 (((x) &~ (offsetT) 0x7fff) == 0 \
1155 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1156
df58fc94
RS
1157/* Is the given value a sign-extended 12-bit value? */
1158#define IS_SEXT_12BIT_NUM(x) \
1159 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1160
7f3c4072
CM
1161/* Is the given value a sign-extended 9-bit value? */
1162#define IS_SEXT_9BIT_NUM(x) \
1163 (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1164
2051e8c4
MR
1165/* Is the given value a zero-extended 32-bit value? Or a negated one? */
1166#define IS_ZEXT_32BIT_NUM(x) \
1167 (((x) &~ (offsetT) 0xffffffff) == 0 \
1168 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1169
bf12938e
RS
1170/* Extract bits MASK << SHIFT from STRUCT and shift them right
1171 SHIFT places. */
1172#define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1173 (((STRUCT) >> (SHIFT)) & (MASK))
1174
bf12938e 1175/* Extract the operand given by FIELD from mips_cl_insn INSN. */
df58fc94
RS
1176#define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1177 (!(MICROMIPS) \
1178 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1179 : EXTRACT_BITS ((INSN).insn_opcode, \
1180 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
bf12938e
RS
1181#define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1182 EXTRACT_BITS ((INSN).insn_opcode, \
1183 MIPS16OP_MASK_##FIELD, \
1184 MIPS16OP_SH_##FIELD)
5c04167a
RS
1185
1186/* The MIPS16 EXTEND opcode, shifted left 16 places. */
1187#define MIPS16_EXTEND (0xf000U << 16)
4d7206a2 1188\f
df58fc94
RS
1189/* Whether or not we are emitting a branch-likely macro. */
1190static bfd_boolean emit_branch_likely_macro = FALSE;
1191
4d7206a2
RS
1192/* Global variables used when generating relaxable macros. See the
1193 comment above RELAX_ENCODE for more details about how relaxation
1194 is used. */
1195static struct {
1196 /* 0 if we're not emitting a relaxable macro.
1197 1 if we're emitting the first of the two relaxation alternatives.
1198 2 if we're emitting the second alternative. */
1199 int sequence;
1200
1201 /* The first relaxable fixup in the current frag. (In other words,
1202 the first fixup that refers to relaxable code.) */
1203 fixS *first_fixup;
1204
1205 /* sizes[0] says how many bytes of the first alternative are stored in
1206 the current frag. Likewise sizes[1] for the second alternative. */
1207 unsigned int sizes[2];
1208
1209 /* The symbol on which the choice of sequence depends. */
1210 symbolS *symbol;
1211} mips_relax;
252b5132 1212\f
584892a6
RS
1213/* Global variables used to decide whether a macro needs a warning. */
1214static struct {
1215 /* True if the macro is in a branch delay slot. */
1216 bfd_boolean delay_slot_p;
1217
df58fc94
RS
1218 /* Set to the length in bytes required if the macro is in a delay slot
1219 that requires a specific length of instruction, otherwise zero. */
1220 unsigned int delay_slot_length;
1221
584892a6
RS
1222 /* For relaxable macros, sizes[0] is the length of the first alternative
1223 in bytes and sizes[1] is the length of the second alternative.
1224 For non-relaxable macros, both elements give the length of the
1225 macro in bytes. */
1226 unsigned int sizes[2];
1227
df58fc94
RS
1228 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1229 instruction of the first alternative in bytes and first_insn_sizes[1]
1230 is the length of the first instruction of the second alternative.
1231 For non-relaxable macros, both elements give the length of the first
1232 instruction in bytes.
1233
1234 Set to zero if we haven't yet seen the first instruction. */
1235 unsigned int first_insn_sizes[2];
1236
1237 /* For relaxable macros, insns[0] is the number of instructions for the
1238 first alternative and insns[1] is the number of instructions for the
1239 second alternative.
1240
1241 For non-relaxable macros, both elements give the number of
1242 instructions for the macro. */
1243 unsigned int insns[2];
1244
584892a6
RS
1245 /* The first variant frag for this macro. */
1246 fragS *first_frag;
1247} mips_macro_warning;
1248\f
252b5132
RH
1249/* Prototypes for static functions. */
1250
252b5132
RH
1251enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1252
b34976b6 1253static void append_insn
df58fc94
RS
1254 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1255 bfd_boolean expansionp);
7d10b47d 1256static void mips_no_prev_insn (void);
c67a084a 1257static void macro_build (expressionS *, const char *, const char *, ...);
b34976b6 1258static void mips16_macro_build
03ea81db 1259 (expressionS *, const char *, const char *, va_list *);
67c0d1eb 1260static void load_register (int, expressionS *, int);
584892a6
RS
1261static void macro_start (void);
1262static void macro_end (void);
833794fc 1263static void macro (struct mips_cl_insn *ip, char *str);
17a2f251 1264static void mips16_macro (struct mips_cl_insn * ip);
17a2f251
TS
1265static void mips_ip (char *str, struct mips_cl_insn * ip);
1266static void mips16_ip (char *str, struct mips_cl_insn * ip);
b34976b6 1267static void mips16_immed
43c0598f
RS
1268 (char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
1269 unsigned int, unsigned long *);
5e0116d5 1270static size_t my_getSmallExpression
17a2f251
TS
1271 (expressionS *, bfd_reloc_code_real_type *, char *);
1272static void my_getExpression (expressionS *, char *);
1273static void s_align (int);
1274static void s_change_sec (int);
1275static void s_change_section (int);
1276static void s_cons (int);
1277static void s_float_cons (int);
1278static void s_mips_globl (int);
1279static void s_option (int);
1280static void s_mipsset (int);
1281static void s_abicalls (int);
1282static void s_cpload (int);
1283static void s_cpsetup (int);
1284static void s_cplocal (int);
1285static void s_cprestore (int);
1286static void s_cpreturn (int);
741d6ea8
JM
1287static void s_dtprelword (int);
1288static void s_dtpreldword (int);
d0f13682
CLT
1289static void s_tprelword (int);
1290static void s_tpreldword (int);
17a2f251
TS
1291static void s_gpvalue (int);
1292static void s_gpword (int);
1293static void s_gpdword (int);
a3f278e2 1294static void s_ehword (int);
17a2f251
TS
1295static void s_cpadd (int);
1296static void s_insn (int);
ba92f887 1297static void s_nan (int);
17a2f251
TS
1298static void md_obj_begin (void);
1299static void md_obj_end (void);
1300static void s_mips_ent (int);
1301static void s_mips_end (int);
1302static void s_mips_frame (int);
1303static void s_mips_mask (int reg_type);
1304static void s_mips_stab (int);
1305static void s_mips_weakext (int);
1306static void s_mips_file (int);
1307static void s_mips_loc (int);
1308static bfd_boolean pic_need_relax (symbolS *, asection *);
4a6a3df4 1309static int relaxed_branch_length (fragS *, asection *, int);
df58fc94
RS
1310static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1311static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
e7af610e
NC
1312
1313/* Table and functions used to map between CPU/ISA names, and
1314 ISA levels, and CPU numbers. */
1315
e972090a
NC
1316struct mips_cpu_info
1317{
e7af610e 1318 const char *name; /* CPU or ISA name. */
d16afab6
RS
1319 int flags; /* MIPS_CPU_* flags. */
1320 int ase; /* Set of ASEs implemented by the CPU. */
e7af610e
NC
1321 int isa; /* ISA level. */
1322 int cpu; /* CPU number (default CPU if ISA). */
1323};
1324
ad3fea08 1325#define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
ad3fea08 1326
17a2f251
TS
1327static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1328static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1329static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132 1330\f
c31f3936
RS
1331/* Command-line options. */
1332const char *md_shortopts = "O::g::G:";
1333
1334enum options
1335 {
1336 OPTION_MARCH = OPTION_MD_BASE,
1337 OPTION_MTUNE,
1338 OPTION_MIPS1,
1339 OPTION_MIPS2,
1340 OPTION_MIPS3,
1341 OPTION_MIPS4,
1342 OPTION_MIPS5,
1343 OPTION_MIPS32,
1344 OPTION_MIPS64,
1345 OPTION_MIPS32R2,
ae52f483
AB
1346 OPTION_MIPS32R3,
1347 OPTION_MIPS32R5,
c31f3936 1348 OPTION_MIPS64R2,
ae52f483
AB
1349 OPTION_MIPS64R3,
1350 OPTION_MIPS64R5,
c31f3936
RS
1351 OPTION_MIPS16,
1352 OPTION_NO_MIPS16,
1353 OPTION_MIPS3D,
1354 OPTION_NO_MIPS3D,
1355 OPTION_MDMX,
1356 OPTION_NO_MDMX,
1357 OPTION_DSP,
1358 OPTION_NO_DSP,
1359 OPTION_MT,
1360 OPTION_NO_MT,
1361 OPTION_VIRT,
1362 OPTION_NO_VIRT,
56d438b1
CF
1363 OPTION_MSA,
1364 OPTION_NO_MSA,
c31f3936
RS
1365 OPTION_SMARTMIPS,
1366 OPTION_NO_SMARTMIPS,
1367 OPTION_DSPR2,
1368 OPTION_NO_DSPR2,
1369 OPTION_EVA,
1370 OPTION_NO_EVA,
7d64c587
AB
1371 OPTION_XPA,
1372 OPTION_NO_XPA,
c31f3936
RS
1373 OPTION_MICROMIPS,
1374 OPTION_NO_MICROMIPS,
1375 OPTION_MCU,
1376 OPTION_NO_MCU,
1377 OPTION_COMPAT_ARCH_BASE,
1378 OPTION_M4650,
1379 OPTION_NO_M4650,
1380 OPTION_M4010,
1381 OPTION_NO_M4010,
1382 OPTION_M4100,
1383 OPTION_NO_M4100,
1384 OPTION_M3900,
1385 OPTION_NO_M3900,
1386 OPTION_M7000_HILO_FIX,
1387 OPTION_MNO_7000_HILO_FIX,
1388 OPTION_FIX_24K,
1389 OPTION_NO_FIX_24K,
a8d14a88
CM
1390 OPTION_FIX_RM7000,
1391 OPTION_NO_FIX_RM7000,
c31f3936
RS
1392 OPTION_FIX_LOONGSON2F_JUMP,
1393 OPTION_NO_FIX_LOONGSON2F_JUMP,
1394 OPTION_FIX_LOONGSON2F_NOP,
1395 OPTION_NO_FIX_LOONGSON2F_NOP,
1396 OPTION_FIX_VR4120,
1397 OPTION_NO_FIX_VR4120,
1398 OPTION_FIX_VR4130,
1399 OPTION_NO_FIX_VR4130,
1400 OPTION_FIX_CN63XXP1,
1401 OPTION_NO_FIX_CN63XXP1,
1402 OPTION_TRAP,
1403 OPTION_BREAK,
1404 OPTION_EB,
1405 OPTION_EL,
1406 OPTION_FP32,
1407 OPTION_GP32,
1408 OPTION_CONSTRUCT_FLOATS,
1409 OPTION_NO_CONSTRUCT_FLOATS,
1410 OPTION_FP64,
1411 OPTION_GP64,
1412 OPTION_RELAX_BRANCH,
1413 OPTION_NO_RELAX_BRANCH,
833794fc
MR
1414 OPTION_INSN32,
1415 OPTION_NO_INSN32,
c31f3936
RS
1416 OPTION_MSHARED,
1417 OPTION_MNO_SHARED,
1418 OPTION_MSYM32,
1419 OPTION_MNO_SYM32,
1420 OPTION_SOFT_FLOAT,
1421 OPTION_HARD_FLOAT,
1422 OPTION_SINGLE_FLOAT,
1423 OPTION_DOUBLE_FLOAT,
1424 OPTION_32,
c31f3936
RS
1425 OPTION_CALL_SHARED,
1426 OPTION_CALL_NONPIC,
1427 OPTION_NON_SHARED,
1428 OPTION_XGOT,
1429 OPTION_MABI,
1430 OPTION_N32,
1431 OPTION_64,
1432 OPTION_MDEBUG,
1433 OPTION_NO_MDEBUG,
1434 OPTION_PDR,
1435 OPTION_NO_PDR,
1436 OPTION_MVXWORKS_PIC,
ba92f887 1437 OPTION_NAN,
c31f3936
RS
1438 OPTION_END_OF_ENUM
1439 };
1440
1441struct option md_longopts[] =
1442{
1443 /* Options which specify architecture. */
1444 {"march", required_argument, NULL, OPTION_MARCH},
1445 {"mtune", required_argument, NULL, OPTION_MTUNE},
1446 {"mips0", no_argument, NULL, OPTION_MIPS1},
1447 {"mips1", no_argument, NULL, OPTION_MIPS1},
1448 {"mips2", no_argument, NULL, OPTION_MIPS2},
1449 {"mips3", no_argument, NULL, OPTION_MIPS3},
1450 {"mips4", no_argument, NULL, OPTION_MIPS4},
1451 {"mips5", no_argument, NULL, OPTION_MIPS5},
1452 {"mips32", no_argument, NULL, OPTION_MIPS32},
1453 {"mips64", no_argument, NULL, OPTION_MIPS64},
1454 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
ae52f483
AB
1455 {"mips32r3", no_argument, NULL, OPTION_MIPS32R3},
1456 {"mips32r5", no_argument, NULL, OPTION_MIPS32R5},
c31f3936 1457 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
ae52f483
AB
1458 {"mips64r3", no_argument, NULL, OPTION_MIPS64R3},
1459 {"mips64r5", no_argument, NULL, OPTION_MIPS64R5},
c31f3936
RS
1460
1461 /* Options which specify Application Specific Extensions (ASEs). */
1462 {"mips16", no_argument, NULL, OPTION_MIPS16},
1463 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1464 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1465 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1466 {"mdmx", no_argument, NULL, OPTION_MDMX},
1467 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1468 {"mdsp", no_argument, NULL, OPTION_DSP},
1469 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1470 {"mmt", no_argument, NULL, OPTION_MT},
1471 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1472 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1473 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1474 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1475 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
1476 {"meva", no_argument, NULL, OPTION_EVA},
1477 {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1478 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1479 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1480 {"mmcu", no_argument, NULL, OPTION_MCU},
1481 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1482 {"mvirt", no_argument, NULL, OPTION_VIRT},
1483 {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
56d438b1
CF
1484 {"mmsa", no_argument, NULL, OPTION_MSA},
1485 {"mno-msa", no_argument, NULL, OPTION_NO_MSA},
7d64c587
AB
1486 {"mxpa", no_argument, NULL, OPTION_XPA},
1487 {"mno-xpa", no_argument, NULL, OPTION_NO_XPA},
c31f3936
RS
1488
1489 /* Old-style architecture options. Don't add more of these. */
1490 {"m4650", no_argument, NULL, OPTION_M4650},
1491 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1492 {"m4010", no_argument, NULL, OPTION_M4010},
1493 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1494 {"m4100", no_argument, NULL, OPTION_M4100},
1495 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1496 {"m3900", no_argument, NULL, OPTION_M3900},
1497 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1498
1499 /* Options which enable bug fixes. */
1500 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1501 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1502 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1503 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1504 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1505 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1506 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1507 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
1508 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1509 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
1510 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1511 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
1512 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
a8d14a88
CM
1513 {"mfix-rm7000", no_argument, NULL, OPTION_FIX_RM7000},
1514 {"mno-fix-rm7000", no_argument, NULL, OPTION_NO_FIX_RM7000},
c31f3936
RS
1515 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1516 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
1517
1518 /* Miscellaneous options. */
1519 {"trap", no_argument, NULL, OPTION_TRAP},
1520 {"no-break", no_argument, NULL, OPTION_TRAP},
1521 {"break", no_argument, NULL, OPTION_BREAK},
1522 {"no-trap", no_argument, NULL, OPTION_BREAK},
1523 {"EB", no_argument, NULL, OPTION_EB},
1524 {"EL", no_argument, NULL, OPTION_EL},
1525 {"mfp32", no_argument, NULL, OPTION_FP32},
1526 {"mgp32", no_argument, NULL, OPTION_GP32},
1527 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1528 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1529 {"mfp64", no_argument, NULL, OPTION_FP64},
1530 {"mgp64", no_argument, NULL, OPTION_GP64},
1531 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1532 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
833794fc
MR
1533 {"minsn32", no_argument, NULL, OPTION_INSN32},
1534 {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
c31f3936
RS
1535 {"mshared", no_argument, NULL, OPTION_MSHARED},
1536 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1537 {"msym32", no_argument, NULL, OPTION_MSYM32},
1538 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1539 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1540 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1541 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1542 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
1543
1544 /* Strictly speaking this next option is ELF specific,
1545 but we allow it for other ports as well in order to
1546 make testing easier. */
1547 {"32", no_argument, NULL, OPTION_32},
1548
1549 /* ELF-specific options. */
c31f3936
RS
1550 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1551 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1552 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1553 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
1554 {"xgot", no_argument, NULL, OPTION_XGOT},
1555 {"mabi", required_argument, NULL, OPTION_MABI},
1556 {"n32", no_argument, NULL, OPTION_N32},
1557 {"64", no_argument, NULL, OPTION_64},
1558 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1559 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1560 {"mpdr", no_argument, NULL, OPTION_PDR},
1561 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1562 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
ba92f887 1563 {"mnan", required_argument, NULL, OPTION_NAN},
c31f3936
RS
1564
1565 {NULL, no_argument, NULL, 0}
1566};
1567size_t md_longopts_size = sizeof (md_longopts);
1568\f
c6278170
RS
1569/* Information about either an Application Specific Extension or an
1570 optional architecture feature that, for simplicity, we treat in the
1571 same way as an ASE. */
1572struct mips_ase
1573{
1574 /* The name of the ASE, used in both the command-line and .set options. */
1575 const char *name;
1576
1577 /* The associated ASE_* flags. If the ASE is available on both 32-bit
1578 and 64-bit architectures, the flags here refer to the subset that
1579 is available on both. */
1580 unsigned int flags;
1581
1582 /* The ASE_* flag used for instructions that are available on 64-bit
1583 architectures but that are not included in FLAGS. */
1584 unsigned int flags64;
1585
1586 /* The command-line options that turn the ASE on and off. */
1587 int option_on;
1588 int option_off;
1589
1590 /* The minimum required architecture revisions for MIPS32, MIPS64,
1591 microMIPS32 and microMIPS64, or -1 if the extension isn't supported. */
1592 int mips32_rev;
1593 int mips64_rev;
1594 int micromips32_rev;
1595 int micromips64_rev;
1596};
1597
1598/* A table of all supported ASEs. */
1599static const struct mips_ase mips_ases[] = {
1600 { "dsp", ASE_DSP, ASE_DSP64,
1601 OPTION_DSP, OPTION_NO_DSP,
1602 2, 2, 2, 2 },
1603
1604 { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1605 OPTION_DSPR2, OPTION_NO_DSPR2,
1606 2, 2, 2, 2 },
1607
1608 { "eva", ASE_EVA, 0,
1609 OPTION_EVA, OPTION_NO_EVA,
1610 2, 2, 2, 2 },
1611
1612 { "mcu", ASE_MCU, 0,
1613 OPTION_MCU, OPTION_NO_MCU,
1614 2, 2, 2, 2 },
1615
1616 /* Deprecated in MIPS64r5, but we don't implement that yet. */
1617 { "mdmx", ASE_MDMX, 0,
1618 OPTION_MDMX, OPTION_NO_MDMX,
1619 -1, 1, -1, -1 },
1620
1621 /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2. */
1622 { "mips3d", ASE_MIPS3D, 0,
1623 OPTION_MIPS3D, OPTION_NO_MIPS3D,
1624 2, 1, -1, -1 },
1625
1626 { "mt", ASE_MT, 0,
1627 OPTION_MT, OPTION_NO_MT,
1628 2, 2, -1, -1 },
1629
1630 { "smartmips", ASE_SMARTMIPS, 0,
1631 OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
1632 1, -1, -1, -1 },
1633
1634 { "virt", ASE_VIRT, ASE_VIRT64,
1635 OPTION_VIRT, OPTION_NO_VIRT,
56d438b1
CF
1636 2, 2, 2, 2 },
1637
1638 { "msa", ASE_MSA, ASE_MSA64,
1639 OPTION_MSA, OPTION_NO_MSA,
7d64c587
AB
1640 2, 2, 2, 2 },
1641
1642 { "xpa", ASE_XPA, 0,
1643 OPTION_XPA, OPTION_NO_XPA,
1644 2, 2, -1, -1 }
c6278170
RS
1645};
1646
1647/* The set of ASEs that require -mfp64. */
1648#define FP64_ASES (ASE_MIPS3D | ASE_MDMX)
1649
1650/* Groups of ASE_* flags that represent different revisions of an ASE. */
1651static const unsigned int mips_ase_groups[] = {
1652 ASE_DSP | ASE_DSPR2
1653};
1654\f
252b5132
RH
1655/* Pseudo-op table.
1656
1657 The following pseudo-ops from the Kane and Heinrich MIPS book
1658 should be defined here, but are currently unsupported: .alias,
1659 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1660
1661 The following pseudo-ops from the Kane and Heinrich MIPS book are
1662 specific to the type of debugging information being generated, and
1663 should be defined by the object format: .aent, .begin, .bend,
1664 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1665 .vreg.
1666
1667 The following pseudo-ops from the Kane and Heinrich MIPS book are
1668 not MIPS CPU specific, but are also not specific to the object file
1669 format. This file is probably the best place to define them, but
d84bcf09 1670 they are not currently supported: .asm0, .endr, .lab, .struct. */
252b5132 1671
e972090a
NC
1672static const pseudo_typeS mips_pseudo_table[] =
1673{
beae10d5 1674 /* MIPS specific pseudo-ops. */
252b5132
RH
1675 {"option", s_option, 0},
1676 {"set", s_mipsset, 0},
1677 {"rdata", s_change_sec, 'r'},
1678 {"sdata", s_change_sec, 's'},
1679 {"livereg", s_ignore, 0},
1680 {"abicalls", s_abicalls, 0},
1681 {"cpload", s_cpload, 0},
6478892d
TS
1682 {"cpsetup", s_cpsetup, 0},
1683 {"cplocal", s_cplocal, 0},
252b5132 1684 {"cprestore", s_cprestore, 0},
6478892d 1685 {"cpreturn", s_cpreturn, 0},
741d6ea8
JM
1686 {"dtprelword", s_dtprelword, 0},
1687 {"dtpreldword", s_dtpreldword, 0},
d0f13682
CLT
1688 {"tprelword", s_tprelword, 0},
1689 {"tpreldword", s_tpreldword, 0},
6478892d 1690 {"gpvalue", s_gpvalue, 0},
252b5132 1691 {"gpword", s_gpword, 0},
10181a0d 1692 {"gpdword", s_gpdword, 0},
a3f278e2 1693 {"ehword", s_ehword, 0},
252b5132
RH
1694 {"cpadd", s_cpadd, 0},
1695 {"insn", s_insn, 0},
ba92f887 1696 {"nan", s_nan, 0},
252b5132 1697
beae10d5 1698 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132 1699 chips. */
38a57ae7 1700 {"asciiz", stringer, 8 + 1},
252b5132
RH
1701 {"bss", s_change_sec, 'b'},
1702 {"err", s_err, 0},
1703 {"half", s_cons, 1},
1704 {"dword", s_cons, 3},
1705 {"weakext", s_mips_weakext, 0},
7c752c2a
TS
1706 {"origin", s_org, 0},
1707 {"repeat", s_rept, 0},
252b5132 1708
998b3c36
MR
1709 /* For MIPS this is non-standard, but we define it for consistency. */
1710 {"sbss", s_change_sec, 'B'},
1711
beae10d5 1712 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1713 here for one reason or another. */
1714 {"align", s_align, 0},
1715 {"byte", s_cons, 0},
1716 {"data", s_change_sec, 'd'},
1717 {"double", s_float_cons, 'd'},
1718 {"float", s_float_cons, 'f'},
1719 {"globl", s_mips_globl, 0},
1720 {"global", s_mips_globl, 0},
1721 {"hword", s_cons, 1},
1722 {"int", s_cons, 2},
1723 {"long", s_cons, 2},
1724 {"octa", s_cons, 4},
1725 {"quad", s_cons, 3},
cca86cc8 1726 {"section", s_change_section, 0},
252b5132
RH
1727 {"short", s_cons, 1},
1728 {"single", s_float_cons, 'f'},
754e2bb9 1729 {"stabd", s_mips_stab, 'd'},
252b5132 1730 {"stabn", s_mips_stab, 'n'},
754e2bb9 1731 {"stabs", s_mips_stab, 's'},
252b5132
RH
1732 {"text", s_change_sec, 't'},
1733 {"word", s_cons, 2},
add56521 1734
add56521 1735 { "extern", ecoff_directive_extern, 0},
add56521 1736
43841e91 1737 { NULL, NULL, 0 },
252b5132
RH
1738};
1739
e972090a
NC
1740static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1741{
beae10d5
KH
1742 /* These pseudo-ops should be defined by the object file format.
1743 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1744 {"aent", s_mips_ent, 1},
1745 {"bgnb", s_ignore, 0},
1746 {"end", s_mips_end, 0},
1747 {"endb", s_ignore, 0},
1748 {"ent", s_mips_ent, 0},
c5dd6aab 1749 {"file", s_mips_file, 0},
252b5132
RH
1750 {"fmask", s_mips_mask, 'F'},
1751 {"frame", s_mips_frame, 0},
c5dd6aab 1752 {"loc", s_mips_loc, 0},
252b5132
RH
1753 {"mask", s_mips_mask, 'R'},
1754 {"verstamp", s_ignore, 0},
43841e91 1755 { NULL, NULL, 0 },
252b5132
RH
1756};
1757
3ae8dd8d
MR
1758/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1759 purpose of the `.dc.a' internal pseudo-op. */
1760
1761int
1762mips_address_bytes (void)
1763{
1764 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1765}
1766
17a2f251 1767extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1768
1769void
17a2f251 1770mips_pop_insert (void)
252b5132
RH
1771{
1772 pop_insert (mips_pseudo_table);
1773 if (! ECOFF_DEBUGGING)
1774 pop_insert (mips_nonecoff_pseudo_table);
1775}
1776\f
1777/* Symbols labelling the current insn. */
1778
e972090a
NC
1779struct insn_label_list
1780{
252b5132
RH
1781 struct insn_label_list *next;
1782 symbolS *label;
1783};
1784
252b5132 1785static struct insn_label_list *free_insn_labels;
742a56fe 1786#define label_list tc_segment_info_data.labels
252b5132 1787
17a2f251 1788static void mips_clear_insn_labels (void);
df58fc94
RS
1789static void mips_mark_labels (void);
1790static void mips_compressed_mark_labels (void);
252b5132
RH
1791
1792static inline void
17a2f251 1793mips_clear_insn_labels (void)
252b5132
RH
1794{
1795 register struct insn_label_list **pl;
a8dbcb85 1796 segment_info_type *si;
252b5132 1797
a8dbcb85
TS
1798 if (now_seg)
1799 {
1800 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1801 ;
1802
1803 si = seg_info (now_seg);
1804 *pl = si->label_list;
1805 si->label_list = NULL;
1806 }
252b5132 1807}
a8dbcb85 1808
df58fc94
RS
1809/* Mark instruction labels in MIPS16/microMIPS mode. */
1810
1811static inline void
1812mips_mark_labels (void)
1813{
1814 if (HAVE_CODE_COMPRESSION)
1815 mips_compressed_mark_labels ();
1816}
252b5132
RH
1817\f
1818static char *expr_end;
1819
e423441d 1820/* An expression in a macro instruction. This is set by mips_ip and
b0e6f033 1821 mips16_ip and when populated is always an O_constant. */
252b5132
RH
1822
1823static expressionS imm_expr;
252b5132 1824
77bd4346
RS
1825/* The relocatable field in an instruction and the relocs associated
1826 with it. These variables are used for instructions like LUI and
1827 JAL as well as true offsets. They are also used for address
1828 operands in macros. */
252b5132 1829
77bd4346 1830static expressionS offset_expr;
f6688943
TS
1831static bfd_reloc_code_real_type offset_reloc[3]
1832 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 1833
df58fc94
RS
1834/* This is set to the resulting size of the instruction to be produced
1835 by mips16_ip if an explicit extension is used or by mips_ip if an
1836 explicit size is supplied. */
252b5132 1837
df58fc94 1838static unsigned int forced_insn_length;
252b5132 1839
e1b47bd5
RS
1840/* True if we are assembling an instruction. All dot symbols defined during
1841 this time should be treated as code labels. */
1842
1843static bfd_boolean mips_assembling_insn;
1844
ecb4347a
DJ
1845/* The pdr segment for per procedure frame/regmask info. Not used for
1846 ECOFF debugging. */
252b5132
RH
1847
1848static segT pdr_seg;
252b5132 1849
e013f690
TS
1850/* The default target format to use. */
1851
aeffff67
RS
1852#if defined (TE_FreeBSD)
1853#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
1854#elif defined (TE_TMIPS)
1855#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
1856#else
1857#define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
1858#endif
1859
e013f690 1860const char *
17a2f251 1861mips_target_format (void)
e013f690
TS
1862{
1863 switch (OUTPUT_FLAVOR)
1864 {
e013f690 1865 case bfd_target_elf_flavour:
0a44bf69
RS
1866#ifdef TE_VXWORKS
1867 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1868 return (target_big_endian
1869 ? "elf32-bigmips-vxworks"
1870 : "elf32-littlemips-vxworks");
1871#endif
e013f690 1872 return (target_big_endian
cfe86eaa 1873 ? (HAVE_64BIT_OBJECTS
aeffff67 1874 ? ELF_TARGET ("elf64-", "big")
cfe86eaa 1875 : (HAVE_NEWABI
aeffff67
RS
1876 ? ELF_TARGET ("elf32-n", "big")
1877 : ELF_TARGET ("elf32-", "big")))
cfe86eaa 1878 : (HAVE_64BIT_OBJECTS
aeffff67 1879 ? ELF_TARGET ("elf64-", "little")
cfe86eaa 1880 : (HAVE_NEWABI
aeffff67
RS
1881 ? ELF_TARGET ("elf32-n", "little")
1882 : ELF_TARGET ("elf32-", "little"))));
e013f690
TS
1883 default:
1884 abort ();
1885 return NULL;
1886 }
1887}
1888
c6278170
RS
1889/* Return the ISA revision that is currently in use, or 0 if we are
1890 generating code for MIPS V or below. */
1891
1892static int
1893mips_isa_rev (void)
1894{
1895 if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
1896 return 2;
1897
ae52f483
AB
1898 if (mips_opts.isa == ISA_MIPS32R3 || mips_opts.isa == ISA_MIPS64R3)
1899 return 3;
1900
1901 if (mips_opts.isa == ISA_MIPS32R5 || mips_opts.isa == ISA_MIPS64R5)
1902 return 5;
1903
c6278170
RS
1904 /* microMIPS implies revision 2 or above. */
1905 if (mips_opts.micromips)
1906 return 2;
1907
1908 if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
1909 return 1;
1910
1911 return 0;
1912}
1913
1914/* Return the mask of all ASEs that are revisions of those in FLAGS. */
1915
1916static unsigned int
1917mips_ase_mask (unsigned int flags)
1918{
1919 unsigned int i;
1920
1921 for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
1922 if (flags & mips_ase_groups[i])
1923 flags |= mips_ase_groups[i];
1924 return flags;
1925}
1926
1927/* Check whether the current ISA supports ASE. Issue a warning if
1928 appropriate. */
1929
1930static void
1931mips_check_isa_supports_ase (const struct mips_ase *ase)
1932{
1933 const char *base;
1934 int min_rev, size;
1935 static unsigned int warned_isa;
1936 static unsigned int warned_fp32;
1937
1938 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
1939 min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
1940 else
1941 min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
1942 if ((min_rev < 0 || mips_isa_rev () < min_rev)
1943 && (warned_isa & ase->flags) != ase->flags)
1944 {
1945 warned_isa |= ase->flags;
1946 base = mips_opts.micromips ? "microMIPS" : "MIPS";
1947 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
1948 if (min_rev < 0)
1661c76c 1949 as_warn (_("the %d-bit %s architecture does not support the"
c6278170
RS
1950 " `%s' extension"), size, base, ase->name);
1951 else
1661c76c 1952 as_warn (_("the `%s' extension requires %s%d revision %d or greater"),
c6278170
RS
1953 ase->name, base, size, min_rev);
1954 }
1955 if ((ase->flags & FP64_ASES)
0b35dfee 1956 && mips_opts.fp != 64
c6278170
RS
1957 && (warned_fp32 & ase->flags) != ase->flags)
1958 {
1959 warned_fp32 |= ase->flags;
1661c76c 1960 as_warn (_("the `%s' extension requires 64-bit FPRs"), ase->name);
c6278170
RS
1961 }
1962}
1963
1964/* Check all enabled ASEs to see whether they are supported by the
1965 chosen architecture. */
1966
1967static void
1968mips_check_isa_supports_ases (void)
1969{
1970 unsigned int i, mask;
1971
1972 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
1973 {
1974 mask = mips_ase_mask (mips_ases[i].flags);
1975 if ((mips_opts.ase & mask) == mips_ases[i].flags)
1976 mips_check_isa_supports_ase (&mips_ases[i]);
1977 }
1978}
1979
1980/* Set the state of ASE to ENABLED_P. Return the mask of ASE_* flags
1981 that were affected. */
1982
1983static unsigned int
1984mips_set_ase (const struct mips_ase *ase, bfd_boolean enabled_p)
1985{
1986 unsigned int mask;
1987
1988 mask = mips_ase_mask (ase->flags);
1989 mips_opts.ase &= ~mask;
1990 if (enabled_p)
1991 mips_opts.ase |= ase->flags;
1992 return mask;
1993}
1994
1995/* Return the ASE called NAME, or null if none. */
1996
1997static const struct mips_ase *
1998mips_lookup_ase (const char *name)
1999{
2000 unsigned int i;
2001
2002 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2003 if (strcmp (name, mips_ases[i].name) == 0)
2004 return &mips_ases[i];
2005 return NULL;
2006}
2007
df58fc94
RS
2008/* Return the length of a microMIPS instruction in bytes. If bits of
2009 the mask beyond the low 16 are 0, then it is a 16-bit instruction.
2010 Otherwise assume a 32-bit instruction; 48-bit instructions (0x1f
2011 major opcode) will require further modifications to the opcode
2012 table. */
2013
2014static inline unsigned int
2015micromips_insn_length (const struct mips_opcode *mo)
2016{
2017 return (mo->mask >> 16) == 0 ? 2 : 4;
2018}
2019
5c04167a
RS
2020/* Return the length of MIPS16 instruction OPCODE. */
2021
2022static inline unsigned int
2023mips16_opcode_length (unsigned long opcode)
2024{
2025 return (opcode >> 16) == 0 ? 2 : 4;
2026}
2027
1e915849
RS
2028/* Return the length of instruction INSN. */
2029
2030static inline unsigned int
2031insn_length (const struct mips_cl_insn *insn)
2032{
df58fc94
RS
2033 if (mips_opts.micromips)
2034 return micromips_insn_length (insn->insn_mo);
2035 else if (mips_opts.mips16)
5c04167a 2036 return mips16_opcode_length (insn->insn_opcode);
df58fc94 2037 else
1e915849 2038 return 4;
1e915849
RS
2039}
2040
2041/* Initialise INSN from opcode entry MO. Leave its position unspecified. */
2042
2043static void
2044create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
2045{
2046 size_t i;
2047
2048 insn->insn_mo = mo;
1e915849
RS
2049 insn->insn_opcode = mo->match;
2050 insn->frag = NULL;
2051 insn->where = 0;
2052 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2053 insn->fixp[i] = NULL;
2054 insn->fixed_p = (mips_opts.noreorder > 0);
2055 insn->noreorder_p = (mips_opts.noreorder > 0);
2056 insn->mips16_absolute_jump_p = 0;
15be625d 2057 insn->complete_p = 0;
e407c74b 2058 insn->cleared_p = 0;
1e915849
RS
2059}
2060
fc76e730
RS
2061/* Get a list of all the operands in INSN. */
2062
2063static const struct mips_operand_array *
2064insn_operands (const struct mips_cl_insn *insn)
2065{
2066 if (insn->insn_mo >= &mips_opcodes[0]
2067 && insn->insn_mo < &mips_opcodes[NUMOPCODES])
2068 return &mips_operands[insn->insn_mo - &mips_opcodes[0]];
2069
2070 if (insn->insn_mo >= &mips16_opcodes[0]
2071 && insn->insn_mo < &mips16_opcodes[bfd_mips16_num_opcodes])
2072 return &mips16_operands[insn->insn_mo - &mips16_opcodes[0]];
2073
2074 if (insn->insn_mo >= &micromips_opcodes[0]
2075 && insn->insn_mo < &micromips_opcodes[bfd_micromips_num_opcodes])
2076 return &micromips_operands[insn->insn_mo - &micromips_opcodes[0]];
2077
2078 abort ();
2079}
2080
2081/* Get a description of operand OPNO of INSN. */
2082
2083static const struct mips_operand *
2084insn_opno (const struct mips_cl_insn *insn, unsigned opno)
2085{
2086 const struct mips_operand_array *operands;
2087
2088 operands = insn_operands (insn);
2089 if (opno >= MAX_OPERANDS || !operands->operand[opno])
2090 abort ();
2091 return operands->operand[opno];
2092}
2093
e077a1c8
RS
2094/* Install UVAL as the value of OPERAND in INSN. */
2095
2096static inline void
2097insn_insert_operand (struct mips_cl_insn *insn,
2098 const struct mips_operand *operand, unsigned int uval)
2099{
2100 insn->insn_opcode = mips_insert_operand (operand, insn->insn_opcode, uval);
2101}
2102
fc76e730
RS
2103/* Extract the value of OPERAND from INSN. */
2104
2105static inline unsigned
2106insn_extract_operand (const struct mips_cl_insn *insn,
2107 const struct mips_operand *operand)
2108{
2109 return mips_extract_operand (operand, insn->insn_opcode);
2110}
2111
df58fc94 2112/* Record the current MIPS16/microMIPS mode in now_seg. */
742a56fe
RS
2113
2114static void
df58fc94 2115mips_record_compressed_mode (void)
742a56fe
RS
2116{
2117 segment_info_type *si;
2118
2119 si = seg_info (now_seg);
2120 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2121 si->tc_segment_info_data.mips16 = mips_opts.mips16;
df58fc94
RS
2122 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2123 si->tc_segment_info_data.micromips = mips_opts.micromips;
742a56fe
RS
2124}
2125
4d68580a
RS
2126/* Read a standard MIPS instruction from BUF. */
2127
2128static unsigned long
2129read_insn (char *buf)
2130{
2131 if (target_big_endian)
2132 return bfd_getb32 ((bfd_byte *) buf);
2133 else
2134 return bfd_getl32 ((bfd_byte *) buf);
2135}
2136
2137/* Write standard MIPS instruction INSN to BUF. Return a pointer to
2138 the next byte. */
2139
2140static char *
2141write_insn (char *buf, unsigned int insn)
2142{
2143 md_number_to_chars (buf, insn, 4);
2144 return buf + 4;
2145}
2146
2147/* Read a microMIPS or MIPS16 opcode from BUF, given that it
2148 has length LENGTH. */
2149
2150static unsigned long
2151read_compressed_insn (char *buf, unsigned int length)
2152{
2153 unsigned long insn;
2154 unsigned int i;
2155
2156 insn = 0;
2157 for (i = 0; i < length; i += 2)
2158 {
2159 insn <<= 16;
2160 if (target_big_endian)
2161 insn |= bfd_getb16 ((char *) buf);
2162 else
2163 insn |= bfd_getl16 ((char *) buf);
2164 buf += 2;
2165 }
2166 return insn;
2167}
2168
5c04167a
RS
2169/* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2170 instruction is LENGTH bytes long. Return a pointer to the next byte. */
2171
2172static char *
2173write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2174{
2175 unsigned int i;
2176
2177 for (i = 0; i < length; i += 2)
2178 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2179 return buf + length;
2180}
2181
1e915849
RS
2182/* Install INSN at the location specified by its "frag" and "where" fields. */
2183
2184static void
2185install_insn (const struct mips_cl_insn *insn)
2186{
2187 char *f = insn->frag->fr_literal + insn->where;
5c04167a
RS
2188 if (HAVE_CODE_COMPRESSION)
2189 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
1e915849 2190 else
4d68580a 2191 write_insn (f, insn->insn_opcode);
df58fc94 2192 mips_record_compressed_mode ();
1e915849
RS
2193}
2194
2195/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
2196 and install the opcode in the new location. */
2197
2198static void
2199move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2200{
2201 size_t i;
2202
2203 insn->frag = frag;
2204 insn->where = where;
2205 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2206 if (insn->fixp[i] != NULL)
2207 {
2208 insn->fixp[i]->fx_frag = frag;
2209 insn->fixp[i]->fx_where = where;
2210 }
2211 install_insn (insn);
2212}
2213
2214/* Add INSN to the end of the output. */
2215
2216static void
2217add_fixed_insn (struct mips_cl_insn *insn)
2218{
2219 char *f = frag_more (insn_length (insn));
2220 move_insn (insn, frag_now, f - frag_now->fr_literal);
2221}
2222
2223/* Start a variant frag and move INSN to the start of the variant part,
2224 marking it as fixed. The other arguments are as for frag_var. */
2225
2226static void
2227add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2228 relax_substateT subtype, symbolS *symbol, offsetT offset)
2229{
2230 frag_grow (max_chars);
2231 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2232 insn->fixed_p = 1;
2233 frag_var (rs_machine_dependent, max_chars, var,
2234 subtype, symbol, offset, NULL);
2235}
2236
2237/* Insert N copies of INSN into the history buffer, starting at
2238 position FIRST. Neither FIRST nor N need to be clipped. */
2239
2240static void
2241insert_into_history (unsigned int first, unsigned int n,
2242 const struct mips_cl_insn *insn)
2243{
2244 if (mips_relax.sequence != 2)
2245 {
2246 unsigned int i;
2247
2248 for (i = ARRAY_SIZE (history); i-- > first;)
2249 if (i >= first + n)
2250 history[i] = history[i - n];
2251 else
2252 history[i] = *insn;
2253 }
2254}
2255
e3de51ce
RS
2256/* Clear the error in insn_error. */
2257
2258static void
2259clear_insn_error (void)
2260{
2261 memset (&insn_error, 0, sizeof (insn_error));
2262}
2263
2264/* Possibly record error message MSG for the current instruction.
2265 If the error is about a particular argument, ARGNUM is the 1-based
2266 number of that argument, otherwise it is 0. FORMAT is the format
2267 of MSG. Return true if MSG was used, false if the current message
2268 was kept. */
2269
2270static bfd_boolean
2271set_insn_error_format (int argnum, enum mips_insn_error_format format,
2272 const char *msg)
2273{
2274 if (argnum == 0)
2275 {
2276 /* Give priority to errors against specific arguments, and to
2277 the first whole-instruction message. */
2278 if (insn_error.msg)
2279 return FALSE;
2280 }
2281 else
2282 {
2283 /* Keep insn_error if it is against a later argument. */
2284 if (argnum < insn_error.min_argnum)
2285 return FALSE;
2286
2287 /* If both errors are against the same argument but are different,
2288 give up on reporting a specific error for this argument.
2289 See the comment about mips_insn_error for details. */
2290 if (argnum == insn_error.min_argnum
2291 && insn_error.msg
2292 && strcmp (insn_error.msg, msg) != 0)
2293 {
2294 insn_error.msg = 0;
2295 insn_error.min_argnum += 1;
2296 return FALSE;
2297 }
2298 }
2299 insn_error.min_argnum = argnum;
2300 insn_error.format = format;
2301 insn_error.msg = msg;
2302 return TRUE;
2303}
2304
2305/* Record an instruction error with no % format fields. ARGNUM and MSG are
2306 as for set_insn_error_format. */
2307
2308static void
2309set_insn_error (int argnum, const char *msg)
2310{
2311 set_insn_error_format (argnum, ERR_FMT_PLAIN, msg);
2312}
2313
2314/* Record an instruction error with one %d field I. ARGNUM and MSG are
2315 as for set_insn_error_format. */
2316
2317static void
2318set_insn_error_i (int argnum, const char *msg, int i)
2319{
2320 if (set_insn_error_format (argnum, ERR_FMT_I, msg))
2321 insn_error.u.i = i;
2322}
2323
2324/* Record an instruction error with two %s fields S1 and S2. ARGNUM and MSG
2325 are as for set_insn_error_format. */
2326
2327static void
2328set_insn_error_ss (int argnum, const char *msg, const char *s1, const char *s2)
2329{
2330 if (set_insn_error_format (argnum, ERR_FMT_SS, msg))
2331 {
2332 insn_error.u.ss[0] = s1;
2333 insn_error.u.ss[1] = s2;
2334 }
2335}
2336
2337/* Report the error in insn_error, which is against assembly code STR. */
2338
2339static void
2340report_insn_error (const char *str)
2341{
2342 const char *msg;
2343
2344 msg = ACONCAT ((insn_error.msg, " `%s'", NULL));
2345 switch (insn_error.format)
2346 {
2347 case ERR_FMT_PLAIN:
2348 as_bad (msg, str);
2349 break;
2350
2351 case ERR_FMT_I:
2352 as_bad (msg, insn_error.u.i, str);
2353 break;
2354
2355 case ERR_FMT_SS:
2356 as_bad (msg, insn_error.u.ss[0], insn_error.u.ss[1], str);
2357 break;
2358 }
2359}
2360
71400594
RS
2361/* Initialize vr4120_conflicts. There is a bit of duplication here:
2362 the idea is to make it obvious at a glance that each errata is
2363 included. */
2364
2365static void
2366init_vr4120_conflicts (void)
2367{
2368#define CONFLICT(FIRST, SECOND) \
2369 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2370
2371 /* Errata 21 - [D]DIV[U] after [D]MACC */
2372 CONFLICT (MACC, DIV);
2373 CONFLICT (DMACC, DIV);
2374
2375 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
2376 CONFLICT (DMULT, DMULT);
2377 CONFLICT (DMULT, DMACC);
2378 CONFLICT (DMACC, DMULT);
2379 CONFLICT (DMACC, DMACC);
2380
2381 /* Errata 24 - MT{LO,HI} after [D]MACC */
2382 CONFLICT (MACC, MTHILO);
2383 CONFLICT (DMACC, MTHILO);
2384
2385 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2386 instruction is executed immediately after a MACC or DMACC
2387 instruction, the result of [either instruction] is incorrect." */
2388 CONFLICT (MACC, MULT);
2389 CONFLICT (MACC, DMULT);
2390 CONFLICT (DMACC, MULT);
2391 CONFLICT (DMACC, DMULT);
2392
2393 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2394 executed immediately after a DMULT, DMULTU, DIV, DIVU,
2395 DDIV or DDIVU instruction, the result of the MACC or
2396 DMACC instruction is incorrect.". */
2397 CONFLICT (DMULT, MACC);
2398 CONFLICT (DMULT, DMACC);
2399 CONFLICT (DIV, MACC);
2400 CONFLICT (DIV, DMACC);
2401
2402#undef CONFLICT
2403}
2404
707bfff6
TS
2405struct regname {
2406 const char *name;
2407 unsigned int num;
2408};
2409
14daeee3 2410#define RNUM_MASK 0x00000ff
56d438b1 2411#define RTYPE_MASK 0x0ffff00
14daeee3
RS
2412#define RTYPE_NUM 0x0000100
2413#define RTYPE_FPU 0x0000200
2414#define RTYPE_FCC 0x0000400
2415#define RTYPE_VEC 0x0000800
2416#define RTYPE_GP 0x0001000
2417#define RTYPE_CP0 0x0002000
2418#define RTYPE_PC 0x0004000
2419#define RTYPE_ACC 0x0008000
2420#define RTYPE_CCC 0x0010000
2421#define RTYPE_VI 0x0020000
2422#define RTYPE_VF 0x0040000
2423#define RTYPE_R5900_I 0x0080000
2424#define RTYPE_R5900_Q 0x0100000
2425#define RTYPE_R5900_R 0x0200000
2426#define RTYPE_R5900_ACC 0x0400000
56d438b1 2427#define RTYPE_MSA 0x0800000
14daeee3 2428#define RWARN 0x8000000
707bfff6
TS
2429
2430#define GENERIC_REGISTER_NUMBERS \
2431 {"$0", RTYPE_NUM | 0}, \
2432 {"$1", RTYPE_NUM | 1}, \
2433 {"$2", RTYPE_NUM | 2}, \
2434 {"$3", RTYPE_NUM | 3}, \
2435 {"$4", RTYPE_NUM | 4}, \
2436 {"$5", RTYPE_NUM | 5}, \
2437 {"$6", RTYPE_NUM | 6}, \
2438 {"$7", RTYPE_NUM | 7}, \
2439 {"$8", RTYPE_NUM | 8}, \
2440 {"$9", RTYPE_NUM | 9}, \
2441 {"$10", RTYPE_NUM | 10}, \
2442 {"$11", RTYPE_NUM | 11}, \
2443 {"$12", RTYPE_NUM | 12}, \
2444 {"$13", RTYPE_NUM | 13}, \
2445 {"$14", RTYPE_NUM | 14}, \
2446 {"$15", RTYPE_NUM | 15}, \
2447 {"$16", RTYPE_NUM | 16}, \
2448 {"$17", RTYPE_NUM | 17}, \
2449 {"$18", RTYPE_NUM | 18}, \
2450 {"$19", RTYPE_NUM | 19}, \
2451 {"$20", RTYPE_NUM | 20}, \
2452 {"$21", RTYPE_NUM | 21}, \
2453 {"$22", RTYPE_NUM | 22}, \
2454 {"$23", RTYPE_NUM | 23}, \
2455 {"$24", RTYPE_NUM | 24}, \
2456 {"$25", RTYPE_NUM | 25}, \
2457 {"$26", RTYPE_NUM | 26}, \
2458 {"$27", RTYPE_NUM | 27}, \
2459 {"$28", RTYPE_NUM | 28}, \
2460 {"$29", RTYPE_NUM | 29}, \
2461 {"$30", RTYPE_NUM | 30}, \
2462 {"$31", RTYPE_NUM | 31}
2463
2464#define FPU_REGISTER_NAMES \
2465 {"$f0", RTYPE_FPU | 0}, \
2466 {"$f1", RTYPE_FPU | 1}, \
2467 {"$f2", RTYPE_FPU | 2}, \
2468 {"$f3", RTYPE_FPU | 3}, \
2469 {"$f4", RTYPE_FPU | 4}, \
2470 {"$f5", RTYPE_FPU | 5}, \
2471 {"$f6", RTYPE_FPU | 6}, \
2472 {"$f7", RTYPE_FPU | 7}, \
2473 {"$f8", RTYPE_FPU | 8}, \
2474 {"$f9", RTYPE_FPU | 9}, \
2475 {"$f10", RTYPE_FPU | 10}, \
2476 {"$f11", RTYPE_FPU | 11}, \
2477 {"$f12", RTYPE_FPU | 12}, \
2478 {"$f13", RTYPE_FPU | 13}, \
2479 {"$f14", RTYPE_FPU | 14}, \
2480 {"$f15", RTYPE_FPU | 15}, \
2481 {"$f16", RTYPE_FPU | 16}, \
2482 {"$f17", RTYPE_FPU | 17}, \
2483 {"$f18", RTYPE_FPU | 18}, \
2484 {"$f19", RTYPE_FPU | 19}, \
2485 {"$f20", RTYPE_FPU | 20}, \
2486 {"$f21", RTYPE_FPU | 21}, \
2487 {"$f22", RTYPE_FPU | 22}, \
2488 {"$f23", RTYPE_FPU | 23}, \
2489 {"$f24", RTYPE_FPU | 24}, \
2490 {"$f25", RTYPE_FPU | 25}, \
2491 {"$f26", RTYPE_FPU | 26}, \
2492 {"$f27", RTYPE_FPU | 27}, \
2493 {"$f28", RTYPE_FPU | 28}, \
2494 {"$f29", RTYPE_FPU | 29}, \
2495 {"$f30", RTYPE_FPU | 30}, \
2496 {"$f31", RTYPE_FPU | 31}
2497
2498#define FPU_CONDITION_CODE_NAMES \
2499 {"$fcc0", RTYPE_FCC | 0}, \
2500 {"$fcc1", RTYPE_FCC | 1}, \
2501 {"$fcc2", RTYPE_FCC | 2}, \
2502 {"$fcc3", RTYPE_FCC | 3}, \
2503 {"$fcc4", RTYPE_FCC | 4}, \
2504 {"$fcc5", RTYPE_FCC | 5}, \
2505 {"$fcc6", RTYPE_FCC | 6}, \
2506 {"$fcc7", RTYPE_FCC | 7}
2507
2508#define COPROC_CONDITION_CODE_NAMES \
2509 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
2510 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
2511 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
2512 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
2513 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
2514 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
2515 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
2516 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
2517
2518#define N32N64_SYMBOLIC_REGISTER_NAMES \
2519 {"$a4", RTYPE_GP | 8}, \
2520 {"$a5", RTYPE_GP | 9}, \
2521 {"$a6", RTYPE_GP | 10}, \
2522 {"$a7", RTYPE_GP | 11}, \
2523 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
2524 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
2525 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
2526 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
2527 {"$t0", RTYPE_GP | 12}, \
2528 {"$t1", RTYPE_GP | 13}, \
2529 {"$t2", RTYPE_GP | 14}, \
2530 {"$t3", RTYPE_GP | 15}
2531
2532#define O32_SYMBOLIC_REGISTER_NAMES \
2533 {"$t0", RTYPE_GP | 8}, \
2534 {"$t1", RTYPE_GP | 9}, \
2535 {"$t2", RTYPE_GP | 10}, \
2536 {"$t3", RTYPE_GP | 11}, \
2537 {"$t4", RTYPE_GP | 12}, \
2538 {"$t5", RTYPE_GP | 13}, \
2539 {"$t6", RTYPE_GP | 14}, \
2540 {"$t7", RTYPE_GP | 15}, \
2541 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2542 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2543 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
2544 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
2545
2546/* Remaining symbolic register names */
2547#define SYMBOLIC_REGISTER_NAMES \
2548 {"$zero", RTYPE_GP | 0}, \
2549 {"$at", RTYPE_GP | 1}, \
2550 {"$AT", RTYPE_GP | 1}, \
2551 {"$v0", RTYPE_GP | 2}, \
2552 {"$v1", RTYPE_GP | 3}, \
2553 {"$a0", RTYPE_GP | 4}, \
2554 {"$a1", RTYPE_GP | 5}, \
2555 {"$a2", RTYPE_GP | 6}, \
2556 {"$a3", RTYPE_GP | 7}, \
2557 {"$s0", RTYPE_GP | 16}, \
2558 {"$s1", RTYPE_GP | 17}, \
2559 {"$s2", RTYPE_GP | 18}, \
2560 {"$s3", RTYPE_GP | 19}, \
2561 {"$s4", RTYPE_GP | 20}, \
2562 {"$s5", RTYPE_GP | 21}, \
2563 {"$s6", RTYPE_GP | 22}, \
2564 {"$s7", RTYPE_GP | 23}, \
2565 {"$t8", RTYPE_GP | 24}, \
2566 {"$t9", RTYPE_GP | 25}, \
2567 {"$k0", RTYPE_GP | 26}, \
2568 {"$kt0", RTYPE_GP | 26}, \
2569 {"$k1", RTYPE_GP | 27}, \
2570 {"$kt1", RTYPE_GP | 27}, \
2571 {"$gp", RTYPE_GP | 28}, \
2572 {"$sp", RTYPE_GP | 29}, \
2573 {"$s8", RTYPE_GP | 30}, \
2574 {"$fp", RTYPE_GP | 30}, \
2575 {"$ra", RTYPE_GP | 31}
2576
2577#define MIPS16_SPECIAL_REGISTER_NAMES \
2578 {"$pc", RTYPE_PC | 0}
2579
2580#define MDMX_VECTOR_REGISTER_NAMES \
2581 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
2582 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
2583 {"$v2", RTYPE_VEC | 2}, \
2584 {"$v3", RTYPE_VEC | 3}, \
2585 {"$v4", RTYPE_VEC | 4}, \
2586 {"$v5", RTYPE_VEC | 5}, \
2587 {"$v6", RTYPE_VEC | 6}, \
2588 {"$v7", RTYPE_VEC | 7}, \
2589 {"$v8", RTYPE_VEC | 8}, \
2590 {"$v9", RTYPE_VEC | 9}, \
2591 {"$v10", RTYPE_VEC | 10}, \
2592 {"$v11", RTYPE_VEC | 11}, \
2593 {"$v12", RTYPE_VEC | 12}, \
2594 {"$v13", RTYPE_VEC | 13}, \
2595 {"$v14", RTYPE_VEC | 14}, \
2596 {"$v15", RTYPE_VEC | 15}, \
2597 {"$v16", RTYPE_VEC | 16}, \
2598 {"$v17", RTYPE_VEC | 17}, \
2599 {"$v18", RTYPE_VEC | 18}, \
2600 {"$v19", RTYPE_VEC | 19}, \
2601 {"$v20", RTYPE_VEC | 20}, \
2602 {"$v21", RTYPE_VEC | 21}, \
2603 {"$v22", RTYPE_VEC | 22}, \
2604 {"$v23", RTYPE_VEC | 23}, \
2605 {"$v24", RTYPE_VEC | 24}, \
2606 {"$v25", RTYPE_VEC | 25}, \
2607 {"$v26", RTYPE_VEC | 26}, \
2608 {"$v27", RTYPE_VEC | 27}, \
2609 {"$v28", RTYPE_VEC | 28}, \
2610 {"$v29", RTYPE_VEC | 29}, \
2611 {"$v30", RTYPE_VEC | 30}, \
2612 {"$v31", RTYPE_VEC | 31}
2613
14daeee3
RS
2614#define R5900_I_NAMES \
2615 {"$I", RTYPE_R5900_I | 0}
2616
2617#define R5900_Q_NAMES \
2618 {"$Q", RTYPE_R5900_Q | 0}
2619
2620#define R5900_R_NAMES \
2621 {"$R", RTYPE_R5900_R | 0}
2622
2623#define R5900_ACC_NAMES \
2624 {"$ACC", RTYPE_R5900_ACC | 0 }
2625
707bfff6
TS
2626#define MIPS_DSP_ACCUMULATOR_NAMES \
2627 {"$ac0", RTYPE_ACC | 0}, \
2628 {"$ac1", RTYPE_ACC | 1}, \
2629 {"$ac2", RTYPE_ACC | 2}, \
2630 {"$ac3", RTYPE_ACC | 3}
2631
2632static const struct regname reg_names[] = {
2633 GENERIC_REGISTER_NUMBERS,
2634 FPU_REGISTER_NAMES,
2635 FPU_CONDITION_CODE_NAMES,
2636 COPROC_CONDITION_CODE_NAMES,
2637
2638 /* The $txx registers depends on the abi,
2639 these will be added later into the symbol table from
2640 one of the tables below once mips_abi is set after
2641 parsing of arguments from the command line. */
2642 SYMBOLIC_REGISTER_NAMES,
2643
2644 MIPS16_SPECIAL_REGISTER_NAMES,
2645 MDMX_VECTOR_REGISTER_NAMES,
14daeee3
RS
2646 R5900_I_NAMES,
2647 R5900_Q_NAMES,
2648 R5900_R_NAMES,
2649 R5900_ACC_NAMES,
707bfff6
TS
2650 MIPS_DSP_ACCUMULATOR_NAMES,
2651 {0, 0}
2652};
2653
2654static const struct regname reg_names_o32[] = {
2655 O32_SYMBOLIC_REGISTER_NAMES,
2656 {0, 0}
2657};
2658
2659static const struct regname reg_names_n32n64[] = {
2660 N32N64_SYMBOLIC_REGISTER_NAMES,
2661 {0, 0}
2662};
2663
a92713e6
RS
2664/* Register symbols $v0 and $v1 map to GPRs 2 and 3, but they can also be
2665 interpreted as vector registers 0 and 1. If SYMVAL is the value of one
2666 of these register symbols, return the associated vector register,
2667 otherwise return SYMVAL itself. */
df58fc94 2668
a92713e6
RS
2669static unsigned int
2670mips_prefer_vec_regno (unsigned int symval)
707bfff6 2671{
a92713e6
RS
2672 if ((symval & -2) == (RTYPE_GP | 2))
2673 return RTYPE_VEC | (symval & 1);
2674 return symval;
2675}
2676
14daeee3
RS
2677/* Return true if string [S, E) is a valid register name, storing its
2678 symbol value in *SYMVAL_PTR if so. */
a92713e6
RS
2679
2680static bfd_boolean
14daeee3 2681mips_parse_register_1 (char *s, char *e, unsigned int *symval_ptr)
a92713e6 2682{
707bfff6 2683 char save_c;
14daeee3 2684 symbolS *symbol;
707bfff6
TS
2685
2686 /* Terminate name. */
2687 save_c = *e;
2688 *e = '\0';
2689
a92713e6
RS
2690 /* Look up the name. */
2691 symbol = symbol_find (s);
2692 *e = save_c;
2693
2694 if (!symbol || S_GET_SEGMENT (symbol) != reg_section)
2695 return FALSE;
2696
14daeee3
RS
2697 *symval_ptr = S_GET_VALUE (symbol);
2698 return TRUE;
2699}
2700
2701/* Return true if the string at *SPTR is a valid register name. Allow it
2702 to have a VU0-style channel suffix of the form x?y?z?w? if CHANNELS_PTR
2703 is nonnull.
2704
2705 When returning true, move *SPTR past the register, store the
2706 register's symbol value in *SYMVAL_PTR and the channel mask in
2707 *CHANNELS_PTR (if nonnull). The symbol value includes the register
2708 number (RNUM_MASK) and register type (RTYPE_MASK). The channel mask
2709 is a 4-bit value of the form XYZW and is 0 if no suffix was given. */
2710
2711static bfd_boolean
2712mips_parse_register (char **sptr, unsigned int *symval_ptr,
2713 unsigned int *channels_ptr)
2714{
2715 char *s, *e, *m;
2716 const char *q;
2717 unsigned int channels, symval, bit;
2718
2719 /* Find end of name. */
2720 s = e = *sptr;
2721 if (is_name_beginner (*e))
2722 ++e;
2723 while (is_part_of_name (*e))
2724 ++e;
2725
2726 channels = 0;
2727 if (!mips_parse_register_1 (s, e, &symval))
2728 {
2729 if (!channels_ptr)
2730 return FALSE;
2731
2732 /* Eat characters from the end of the string that are valid
2733 channel suffixes. The preceding register must be $ACC or
2734 end with a digit, so there is no ambiguity. */
2735 bit = 1;
2736 m = e;
2737 for (q = "wzyx"; *q; q++, bit <<= 1)
2738 if (m > s && m[-1] == *q)
2739 {
2740 --m;
2741 channels |= bit;
2742 }
2743
2744 if (channels == 0
2745 || !mips_parse_register_1 (s, m, &symval)
2746 || (symval & (RTYPE_VI | RTYPE_VF | RTYPE_R5900_ACC)) == 0)
2747 return FALSE;
2748 }
2749
a92713e6 2750 *sptr = e;
14daeee3
RS
2751 *symval_ptr = symval;
2752 if (channels_ptr)
2753 *channels_ptr = channels;
a92713e6
RS
2754 return TRUE;
2755}
2756
2757/* Check if SPTR points at a valid register specifier according to TYPES.
2758 If so, then return 1, advance S to consume the specifier and store
2759 the register's number in REGNOP, otherwise return 0. */
2760
2761static int
2762reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2763{
2764 unsigned int regno;
2765
14daeee3 2766 if (mips_parse_register (s, &regno, NULL))
707bfff6 2767 {
a92713e6
RS
2768 if (types & RTYPE_VEC)
2769 regno = mips_prefer_vec_regno (regno);
2770 if (regno & types)
2771 regno &= RNUM_MASK;
2772 else
2773 regno = ~0;
707bfff6 2774 }
a92713e6 2775 else
707bfff6 2776 {
a92713e6 2777 if (types & RWARN)
1661c76c 2778 as_warn (_("unrecognized register name `%s'"), *s);
a92713e6 2779 regno = ~0;
707bfff6 2780 }
707bfff6 2781 if (regnop)
a92713e6
RS
2782 *regnop = regno;
2783 return regno <= RNUM_MASK;
707bfff6
TS
2784}
2785
14daeee3
RS
2786/* Parse a VU0 "x?y?z?w?" channel mask at S and store the associated
2787 mask in *CHANNELS. Return a pointer to the first unconsumed character. */
2788
2789static char *
2790mips_parse_vu0_channels (char *s, unsigned int *channels)
2791{
2792 unsigned int i;
2793
2794 *channels = 0;
2795 for (i = 0; i < 4; i++)
2796 if (*s == "xyzw"[i])
2797 {
2798 *channels |= 1 << (3 - i);
2799 ++s;
2800 }
2801 return s;
2802}
2803
a92713e6
RS
2804/* Token types for parsed operand lists. */
2805enum mips_operand_token_type {
2806 /* A plain register, e.g. $f2. */
2807 OT_REG,
df58fc94 2808
14daeee3
RS
2809 /* A 4-bit XYZW channel mask. */
2810 OT_CHANNELS,
2811
56d438b1
CF
2812 /* A constant vector index, e.g. [1]. */
2813 OT_INTEGER_INDEX,
2814
2815 /* A register vector index, e.g. [$2]. */
2816 OT_REG_INDEX,
df58fc94 2817
a92713e6
RS
2818 /* A continuous range of registers, e.g. $s0-$s4. */
2819 OT_REG_RANGE,
2820
2821 /* A (possibly relocated) expression. */
2822 OT_INTEGER,
2823
2824 /* A floating-point value. */
2825 OT_FLOAT,
2826
2827 /* A single character. This can be '(', ')' or ',', but '(' only appears
2828 before OT_REGs. */
2829 OT_CHAR,
2830
14daeee3
RS
2831 /* A doubled character, either "--" or "++". */
2832 OT_DOUBLE_CHAR,
2833
a92713e6
RS
2834 /* The end of the operand list. */
2835 OT_END
2836};
2837
2838/* A parsed operand token. */
2839struct mips_operand_token
2840{
2841 /* The type of token. */
2842 enum mips_operand_token_type type;
2843 union
2844 {
56d438b1 2845 /* The register symbol value for an OT_REG or OT_REG_INDEX. */
a92713e6
RS
2846 unsigned int regno;
2847
14daeee3
RS
2848 /* The 4-bit channel mask for an OT_CHANNEL_SUFFIX. */
2849 unsigned int channels;
2850
56d438b1
CF
2851 /* The integer value of an OT_INTEGER_INDEX. */
2852 addressT index;
a92713e6
RS
2853
2854 /* The two register symbol values involved in an OT_REG_RANGE. */
2855 struct {
2856 unsigned int regno1;
2857 unsigned int regno2;
2858 } reg_range;
2859
2860 /* The value of an OT_INTEGER. The value is represented as an
2861 expression and the relocation operators that were applied to
2862 that expression. The reloc entries are BFD_RELOC_UNUSED if no
2863 relocation operators were used. */
2864 struct {
2865 expressionS value;
2866 bfd_reloc_code_real_type relocs[3];
2867 } integer;
2868
2869 /* The binary data for an OT_FLOAT constant, and the number of bytes
2870 in the constant. */
2871 struct {
2872 unsigned char data[8];
2873 int length;
2874 } flt;
2875
14daeee3 2876 /* The character represented by an OT_CHAR or OT_DOUBLE_CHAR. */
a92713e6
RS
2877 char ch;
2878 } u;
2879};
2880
2881/* An obstack used to construct lists of mips_operand_tokens. */
2882static struct obstack mips_operand_tokens;
2883
2884/* Give TOKEN type TYPE and add it to mips_operand_tokens. */
2885
2886static void
2887mips_add_token (struct mips_operand_token *token,
2888 enum mips_operand_token_type type)
2889{
2890 token->type = type;
2891 obstack_grow (&mips_operand_tokens, token, sizeof (*token));
2892}
2893
2894/* Check whether S is '(' followed by a register name. Add OT_CHAR
2895 and OT_REG tokens for them if so, and return a pointer to the first
2896 unconsumed character. Return null otherwise. */
2897
2898static char *
2899mips_parse_base_start (char *s)
2900{
2901 struct mips_operand_token token;
14daeee3
RS
2902 unsigned int regno, channels;
2903 bfd_boolean decrement_p;
df58fc94 2904
a92713e6
RS
2905 if (*s != '(')
2906 return 0;
2907
2908 ++s;
2909 SKIP_SPACE_TABS (s);
14daeee3
RS
2910
2911 /* Only match "--" as part of a base expression. In other contexts "--X"
2912 is a double negative. */
2913 decrement_p = (s[0] == '-' && s[1] == '-');
2914 if (decrement_p)
2915 {
2916 s += 2;
2917 SKIP_SPACE_TABS (s);
2918 }
2919
2920 /* Allow a channel specifier because that leads to better error messages
2921 than treating something like "$vf0x++" as an expression. */
2922 if (!mips_parse_register (&s, &regno, &channels))
a92713e6
RS
2923 return 0;
2924
2925 token.u.ch = '(';
2926 mips_add_token (&token, OT_CHAR);
2927
14daeee3
RS
2928 if (decrement_p)
2929 {
2930 token.u.ch = '-';
2931 mips_add_token (&token, OT_DOUBLE_CHAR);
2932 }
2933
a92713e6
RS
2934 token.u.regno = regno;
2935 mips_add_token (&token, OT_REG);
2936
14daeee3
RS
2937 if (channels)
2938 {
2939 token.u.channels = channels;
2940 mips_add_token (&token, OT_CHANNELS);
2941 }
2942
2943 /* For consistency, only match "++" as part of base expressions too. */
2944 SKIP_SPACE_TABS (s);
2945 if (s[0] == '+' && s[1] == '+')
2946 {
2947 s += 2;
2948 token.u.ch = '+';
2949 mips_add_token (&token, OT_DOUBLE_CHAR);
2950 }
2951
a92713e6
RS
2952 return s;
2953}
2954
2955/* Parse one or more tokens from S. Return a pointer to the first
2956 unconsumed character on success. Return null if an error was found
2957 and store the error text in insn_error. FLOAT_FORMAT is as for
2958 mips_parse_arguments. */
2959
2960static char *
2961mips_parse_argument_token (char *s, char float_format)
2962{
2963 char *end, *save_in, *err;
14daeee3 2964 unsigned int regno1, regno2, channels;
a92713e6
RS
2965 struct mips_operand_token token;
2966
2967 /* First look for "($reg", since we want to treat that as an
2968 OT_CHAR and OT_REG rather than an expression. */
2969 end = mips_parse_base_start (s);
2970 if (end)
2971 return end;
2972
2973 /* Handle other characters that end up as OT_CHARs. */
2974 if (*s == ')' || *s == ',')
2975 {
2976 token.u.ch = *s;
2977 mips_add_token (&token, OT_CHAR);
2978 ++s;
2979 return s;
2980 }
2981
2982 /* Handle tokens that start with a register. */
14daeee3 2983 if (mips_parse_register (&s, &regno1, &channels))
df58fc94 2984 {
14daeee3
RS
2985 if (channels)
2986 {
2987 /* A register and a VU0 channel suffix. */
2988 token.u.regno = regno1;
2989 mips_add_token (&token, OT_REG);
2990
2991 token.u.channels = channels;
2992 mips_add_token (&token, OT_CHANNELS);
2993 return s;
2994 }
2995
a92713e6
RS
2996 SKIP_SPACE_TABS (s);
2997 if (*s == '-')
df58fc94 2998 {
a92713e6
RS
2999 /* A register range. */
3000 ++s;
3001 SKIP_SPACE_TABS (s);
14daeee3 3002 if (!mips_parse_register (&s, &regno2, NULL))
a92713e6 3003 {
1661c76c 3004 set_insn_error (0, _("invalid register range"));
a92713e6
RS
3005 return 0;
3006 }
df58fc94 3007
a92713e6
RS
3008 token.u.reg_range.regno1 = regno1;
3009 token.u.reg_range.regno2 = regno2;
3010 mips_add_token (&token, OT_REG_RANGE);
3011 return s;
3012 }
a92713e6 3013
56d438b1
CF
3014 /* Add the register itself. */
3015 token.u.regno = regno1;
3016 mips_add_token (&token, OT_REG);
3017
3018 /* Check for a vector index. */
3019 if (*s == '[')
3020 {
a92713e6
RS
3021 ++s;
3022 SKIP_SPACE_TABS (s);
56d438b1
CF
3023 if (mips_parse_register (&s, &token.u.regno, NULL))
3024 mips_add_token (&token, OT_REG_INDEX);
3025 else
a92713e6 3026 {
56d438b1
CF
3027 expressionS element;
3028
3029 my_getExpression (&element, s);
3030 if (element.X_op != O_constant)
3031 {
3032 set_insn_error (0, _("vector element must be constant"));
3033 return 0;
3034 }
3035 s = expr_end;
3036 token.u.index = element.X_add_number;
3037 mips_add_token (&token, OT_INTEGER_INDEX);
a92713e6 3038 }
a92713e6
RS
3039 SKIP_SPACE_TABS (s);
3040 if (*s != ']')
3041 {
1661c76c 3042 set_insn_error (0, _("missing `]'"));
a92713e6
RS
3043 return 0;
3044 }
3045 ++s;
df58fc94 3046 }
a92713e6 3047 return s;
df58fc94
RS
3048 }
3049
a92713e6
RS
3050 if (float_format)
3051 {
3052 /* First try to treat expressions as floats. */
3053 save_in = input_line_pointer;
3054 input_line_pointer = s;
3055 err = md_atof (float_format, (char *) token.u.flt.data,
3056 &token.u.flt.length);
3057 end = input_line_pointer;
3058 input_line_pointer = save_in;
3059 if (err && *err)
3060 {
e3de51ce 3061 set_insn_error (0, err);
a92713e6
RS
3062 return 0;
3063 }
3064 if (s != end)
3065 {
3066 mips_add_token (&token, OT_FLOAT);
3067 return end;
3068 }
3069 }
3070
3071 /* Treat everything else as an integer expression. */
3072 token.u.integer.relocs[0] = BFD_RELOC_UNUSED;
3073 token.u.integer.relocs[1] = BFD_RELOC_UNUSED;
3074 token.u.integer.relocs[2] = BFD_RELOC_UNUSED;
3075 my_getSmallExpression (&token.u.integer.value, token.u.integer.relocs, s);
3076 s = expr_end;
3077 mips_add_token (&token, OT_INTEGER);
3078 return s;
3079}
3080
3081/* S points to the operand list for an instruction. FLOAT_FORMAT is 'f'
3082 if expressions should be treated as 32-bit floating-point constants,
3083 'd' if they should be treated as 64-bit floating-point constants,
3084 or 0 if they should be treated as integer expressions (the usual case).
3085
3086 Return a list of tokens on success, otherwise return 0. The caller
3087 must obstack_free the list after use. */
3088
3089static struct mips_operand_token *
3090mips_parse_arguments (char *s, char float_format)
3091{
3092 struct mips_operand_token token;
3093
3094 SKIP_SPACE_TABS (s);
3095 while (*s)
3096 {
3097 s = mips_parse_argument_token (s, float_format);
3098 if (!s)
3099 {
3100 obstack_free (&mips_operand_tokens,
3101 obstack_finish (&mips_operand_tokens));
3102 return 0;
3103 }
3104 SKIP_SPACE_TABS (s);
3105 }
3106 mips_add_token (&token, OT_END);
3107 return (struct mips_operand_token *) obstack_finish (&mips_operand_tokens);
df58fc94
RS
3108}
3109
d301a56b
RS
3110/* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
3111 and architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
037b32b9
AN
3112
3113static bfd_boolean
f79e2745 3114is_opcode_valid (const struct mips_opcode *mo)
037b32b9
AN
3115{
3116 int isa = mips_opts.isa;
846ef2d0 3117 int ase = mips_opts.ase;
037b32b9 3118 int fp_s, fp_d;
c6278170 3119 unsigned int i;
037b32b9 3120
c6278170
RS
3121 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
3122 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3123 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3124 ase |= mips_ases[i].flags64;
037b32b9 3125
d301a56b 3126 if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
037b32b9
AN
3127 return FALSE;
3128
3129 /* Check whether the instruction or macro requires single-precision or
3130 double-precision floating-point support. Note that this information is
3131 stored differently in the opcode table for insns and macros. */
3132 if (mo->pinfo == INSN_MACRO)
3133 {
3134 fp_s = mo->pinfo2 & INSN2_M_FP_S;
3135 fp_d = mo->pinfo2 & INSN2_M_FP_D;
3136 }
3137 else
3138 {
3139 fp_s = mo->pinfo & FP_S;
3140 fp_d = mo->pinfo & FP_D;
3141 }
3142
3143 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
3144 return FALSE;
3145
3146 if (fp_s && mips_opts.soft_float)
3147 return FALSE;
3148
3149 return TRUE;
3150}
3151
3152/* Return TRUE if the MIPS16 opcode MO is valid on the currently
3153 selected ISA and architecture. */
3154
3155static bfd_boolean
3156is_opcode_valid_16 (const struct mips_opcode *mo)
3157{
d301a56b 3158 return opcode_is_member (mo, mips_opts.isa, 0, mips_opts.arch);
037b32b9
AN
3159}
3160
df58fc94
RS
3161/* Return TRUE if the size of the microMIPS opcode MO matches one
3162 explicitly requested. Always TRUE in the standard MIPS mode. */
3163
3164static bfd_boolean
3165is_size_valid (const struct mips_opcode *mo)
3166{
3167 if (!mips_opts.micromips)
3168 return TRUE;
3169
833794fc
MR
3170 if (mips_opts.insn32)
3171 {
3172 if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
3173 return FALSE;
3174 if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
3175 return FALSE;
3176 }
df58fc94
RS
3177 if (!forced_insn_length)
3178 return TRUE;
3179 if (mo->pinfo == INSN_MACRO)
3180 return FALSE;
3181 return forced_insn_length == micromips_insn_length (mo);
3182}
3183
3184/* Return TRUE if the microMIPS opcode MO is valid for the delay slot
e64af278
MR
3185 of the preceding instruction. Always TRUE in the standard MIPS mode.
3186
3187 We don't accept macros in 16-bit delay slots to avoid a case where
3188 a macro expansion fails because it relies on a preceding 32-bit real
3189 instruction to have matched and does not handle the operands correctly.
3190 The only macros that may expand to 16-bit instructions are JAL that
3191 cannot be placed in a delay slot anyway, and corner cases of BALIGN
3192 and BGT (that likewise cannot be placed in a delay slot) that decay to
3193 a NOP. In all these cases the macros precede any corresponding real
3194 instruction definitions in the opcode table, so they will match in the
3195 second pass where the size of the delay slot is ignored and therefore
3196 produce correct code. */
df58fc94
RS
3197
3198static bfd_boolean
3199is_delay_slot_valid (const struct mips_opcode *mo)
3200{
3201 if (!mips_opts.micromips)
3202 return TRUE;
3203
3204 if (mo->pinfo == INSN_MACRO)
c06dec14 3205 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
df58fc94
RS
3206 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
3207 && micromips_insn_length (mo) != 4)
3208 return FALSE;
3209 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
3210 && micromips_insn_length (mo) != 2)
3211 return FALSE;
3212
3213 return TRUE;
3214}
3215
fc76e730
RS
3216/* For consistency checking, verify that all bits of OPCODE are specified
3217 either by the match/mask part of the instruction definition, or by the
3218 operand list. Also build up a list of operands in OPERANDS.
3219
3220 INSN_BITS says which bits of the instruction are significant.
3221 If OPCODE is a standard or microMIPS instruction, DECODE_OPERAND
3222 provides the mips_operand description of each operand. DECODE_OPERAND
3223 is null for MIPS16 instructions. */
ab902481
RS
3224
3225static int
3226validate_mips_insn (const struct mips_opcode *opcode,
3227 unsigned long insn_bits,
fc76e730
RS
3228 const struct mips_operand *(*decode_operand) (const char *),
3229 struct mips_operand_array *operands)
ab902481
RS
3230{
3231 const char *s;
fc76e730 3232 unsigned long used_bits, doubled, undefined, opno, mask;
ab902481
RS
3233 const struct mips_operand *operand;
3234
fc76e730
RS
3235 mask = (opcode->pinfo == INSN_MACRO ? 0 : opcode->mask);
3236 if ((mask & opcode->match) != opcode->match)
ab902481
RS
3237 {
3238 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
3239 opcode->name, opcode->args);
3240 return 0;
3241 }
3242 used_bits = 0;
fc76e730 3243 opno = 0;
14daeee3
RS
3244 if (opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX)
3245 used_bits = mips_insert_operand (&mips_vu0_channel_mask, used_bits, -1);
ab902481
RS
3246 for (s = opcode->args; *s; ++s)
3247 switch (*s)
3248 {
3249 case ',':
3250 case '(':
3251 case ')':
3252 break;
3253
14daeee3
RS
3254 case '#':
3255 s++;
3256 break;
3257
ab902481 3258 default:
fc76e730
RS
3259 if (!decode_operand)
3260 operand = decode_mips16_operand (*s, FALSE);
3261 else
3262 operand = decode_operand (s);
3263 if (!operand && opcode->pinfo != INSN_MACRO)
ab902481
RS
3264 {
3265 as_bad (_("internal: unknown operand type: %s %s"),
3266 opcode->name, opcode->args);
3267 return 0;
3268 }
fc76e730
RS
3269 gas_assert (opno < MAX_OPERANDS);
3270 operands->operand[opno] = operand;
14daeee3 3271 if (operand && operand->type != OP_VU0_MATCH_SUFFIX)
fc76e730 3272 {
14daeee3 3273 used_bits = mips_insert_operand (operand, used_bits, -1);
fc76e730
RS
3274 if (operand->type == OP_MDMX_IMM_REG)
3275 /* Bit 5 is the format selector (OB vs QH). The opcode table
3276 has separate entries for each format. */
3277 used_bits &= ~(1 << (operand->lsb + 5));
3278 if (operand->type == OP_ENTRY_EXIT_LIST)
3279 used_bits &= ~(mask & 0x700);
3280 }
ab902481 3281 /* Skip prefix characters. */
fc76e730 3282 if (decode_operand && (*s == '+' || *s == 'm'))
ab902481 3283 ++s;
fc76e730 3284 opno += 1;
ab902481
RS
3285 break;
3286 }
fc76e730 3287 doubled = used_bits & mask & insn_bits;
ab902481
RS
3288 if (doubled)
3289 {
3290 as_bad (_("internal: bad mips opcode (bits 0x%08lx doubly defined):"
3291 " %s %s"), doubled, opcode->name, opcode->args);
3292 return 0;
3293 }
fc76e730 3294 used_bits |= mask;
ab902481 3295 undefined = ~used_bits & insn_bits;
fc76e730 3296 if (opcode->pinfo != INSN_MACRO && undefined)
ab902481
RS
3297 {
3298 as_bad (_("internal: bad mips opcode (bits 0x%08lx undefined): %s %s"),
3299 undefined, opcode->name, opcode->args);
3300 return 0;
3301 }
3302 used_bits &= ~insn_bits;
3303 if (used_bits)
3304 {
3305 as_bad (_("internal: bad mips opcode (bits 0x%08lx defined): %s %s"),
3306 used_bits, opcode->name, opcode->args);
3307 return 0;
3308 }
3309 return 1;
3310}
3311
fc76e730
RS
3312/* The MIPS16 version of validate_mips_insn. */
3313
3314static int
3315validate_mips16_insn (const struct mips_opcode *opcode,
3316 struct mips_operand_array *operands)
3317{
3318 if (opcode->args[0] == 'a' || opcode->args[0] == 'i')
3319 {
3320 /* In this case OPCODE defines the first 16 bits in a 32-bit jump
3321 instruction. Use TMP to describe the full instruction. */
3322 struct mips_opcode tmp;
3323
3324 tmp = *opcode;
3325 tmp.match <<= 16;
3326 tmp.mask <<= 16;
3327 return validate_mips_insn (&tmp, 0xffffffff, 0, operands);
3328 }
3329 return validate_mips_insn (opcode, 0xffff, 0, operands);
3330}
3331
ab902481
RS
3332/* The microMIPS version of validate_mips_insn. */
3333
3334static int
fc76e730
RS
3335validate_micromips_insn (const struct mips_opcode *opc,
3336 struct mips_operand_array *operands)
ab902481
RS
3337{
3338 unsigned long insn_bits;
3339 unsigned long major;
3340 unsigned int length;
3341
fc76e730
RS
3342 if (opc->pinfo == INSN_MACRO)
3343 return validate_mips_insn (opc, 0xffffffff, decode_micromips_operand,
3344 operands);
3345
ab902481
RS
3346 length = micromips_insn_length (opc);
3347 if (length != 2 && length != 4)
3348 {
1661c76c 3349 as_bad (_("internal error: bad microMIPS opcode (incorrect length: %u): "
ab902481
RS
3350 "%s %s"), length, opc->name, opc->args);
3351 return 0;
3352 }
3353 major = opc->match >> (10 + 8 * (length - 2));
3354 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
3355 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
3356 {
1661c76c 3357 as_bad (_("internal error: bad microMIPS opcode "
ab902481
RS
3358 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
3359 return 0;
3360 }
3361
3362 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
3363 insn_bits = 1 << 4 * length;
3364 insn_bits <<= 4 * length;
3365 insn_bits -= 1;
fc76e730
RS
3366 return validate_mips_insn (opc, insn_bits, decode_micromips_operand,
3367 operands);
ab902481
RS
3368}
3369
707bfff6
TS
3370/* This function is called once, at assembler startup time. It should set up
3371 all the tables, etc. that the MD part of the assembler will need. */
156c2f8b 3372
252b5132 3373void
17a2f251 3374md_begin (void)
252b5132 3375{
3994f87e 3376 const char *retval = NULL;
156c2f8b 3377 int i = 0;
252b5132 3378 int broken = 0;
1f25f5d3 3379
0a44bf69
RS
3380 if (mips_pic != NO_PIC)
3381 {
3382 if (g_switch_seen && g_switch_value != 0)
3383 as_bad (_("-G may not be used in position-independent code"));
3384 g_switch_value = 0;
3385 }
3386
0b35dfee 3387 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
1661c76c 3388 as_warn (_("could not set architecture and machine"));
252b5132 3389
252b5132
RH
3390 op_hash = hash_new ();
3391
fc76e730 3392 mips_operands = XCNEWVEC (struct mips_operand_array, NUMOPCODES);
252b5132
RH
3393 for (i = 0; i < NUMOPCODES;)
3394 {
3395 const char *name = mips_opcodes[i].name;
3396
17a2f251 3397 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
3398 if (retval != NULL)
3399 {
3400 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
3401 mips_opcodes[i].name, retval);
3402 /* Probably a memory allocation problem? Give up now. */
1661c76c 3403 as_fatal (_("broken assembler, no assembly attempted"));
252b5132
RH
3404 }
3405 do
3406 {
fc76e730
RS
3407 if (!validate_mips_insn (&mips_opcodes[i], 0xffffffff,
3408 decode_mips_operand, &mips_operands[i]))
3409 broken = 1;
3410 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
252b5132 3411 {
fc76e730
RS
3412 create_insn (&nop_insn, mips_opcodes + i);
3413 if (mips_fix_loongson2f_nop)
3414 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
3415 nop_insn.fixed_p = 1;
252b5132
RH
3416 }
3417 ++i;
3418 }
3419 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
3420 }
3421
3422 mips16_op_hash = hash_new ();
fc76e730
RS
3423 mips16_operands = XCNEWVEC (struct mips_operand_array,
3424 bfd_mips16_num_opcodes);
252b5132
RH
3425
3426 i = 0;
3427 while (i < bfd_mips16_num_opcodes)
3428 {
3429 const char *name = mips16_opcodes[i].name;
3430
17a2f251 3431 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
3432 if (retval != NULL)
3433 as_fatal (_("internal: can't hash `%s': %s"),
3434 mips16_opcodes[i].name, retval);
3435 do
3436 {
fc76e730
RS
3437 if (!validate_mips16_insn (&mips16_opcodes[i], &mips16_operands[i]))
3438 broken = 1;
1e915849
RS
3439 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3440 {
3441 create_insn (&mips16_nop_insn, mips16_opcodes + i);
3442 mips16_nop_insn.fixed_p = 1;
3443 }
252b5132
RH
3444 ++i;
3445 }
3446 while (i < bfd_mips16_num_opcodes
3447 && strcmp (mips16_opcodes[i].name, name) == 0);
3448 }
3449
df58fc94 3450 micromips_op_hash = hash_new ();
fc76e730
RS
3451 micromips_operands = XCNEWVEC (struct mips_operand_array,
3452 bfd_micromips_num_opcodes);
df58fc94
RS
3453
3454 i = 0;
3455 while (i < bfd_micromips_num_opcodes)
3456 {
3457 const char *name = micromips_opcodes[i].name;
3458
3459 retval = hash_insert (micromips_op_hash, name,
3460 (void *) &micromips_opcodes[i]);
3461 if (retval != NULL)
3462 as_fatal (_("internal: can't hash `%s': %s"),
3463 micromips_opcodes[i].name, retval);
3464 do
fc76e730
RS
3465 {
3466 struct mips_cl_insn *micromips_nop_insn;
3467
3468 if (!validate_micromips_insn (&micromips_opcodes[i],
3469 &micromips_operands[i]))
3470 broken = 1;
3471
3472 if (micromips_opcodes[i].pinfo != INSN_MACRO)
3473 {
3474 if (micromips_insn_length (micromips_opcodes + i) == 2)
3475 micromips_nop_insn = &micromips_nop16_insn;
3476 else if (micromips_insn_length (micromips_opcodes + i) == 4)
3477 micromips_nop_insn = &micromips_nop32_insn;
3478 else
3479 continue;
3480
3481 if (micromips_nop_insn->insn_mo == NULL
3482 && strcmp (name, "nop") == 0)
3483 {
3484 create_insn (micromips_nop_insn, micromips_opcodes + i);
3485 micromips_nop_insn->fixed_p = 1;
3486 }
3487 }
3488 }
df58fc94
RS
3489 while (++i < bfd_micromips_num_opcodes
3490 && strcmp (micromips_opcodes[i].name, name) == 0);
3491 }
3492
252b5132 3493 if (broken)
1661c76c 3494 as_fatal (_("broken assembler, no assembly attempted"));
252b5132
RH
3495
3496 /* We add all the general register names to the symbol table. This
3497 helps us detect invalid uses of them. */
707bfff6
TS
3498 for (i = 0; reg_names[i].name; i++)
3499 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
8fc4ee9b 3500 reg_names[i].num, /* & RNUM_MASK, */
707bfff6
TS
3501 &zero_address_frag));
3502 if (HAVE_NEWABI)
3503 for (i = 0; reg_names_n32n64[i].name; i++)
3504 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
8fc4ee9b 3505 reg_names_n32n64[i].num, /* & RNUM_MASK, */
252b5132 3506 &zero_address_frag));
707bfff6
TS
3507 else
3508 for (i = 0; reg_names_o32[i].name; i++)
3509 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
8fc4ee9b 3510 reg_names_o32[i].num, /* & RNUM_MASK, */
6047c971 3511 &zero_address_frag));
6047c971 3512
14daeee3
RS
3513 for (i = 0; i < 32; i++)
3514 {
3515 char regname[7];
3516
3517 /* R5900 VU0 floating-point register. */
3518 regname[sizeof (rename) - 1] = 0;
3519 snprintf (regname, sizeof (regname) - 1, "$vf%d", i);
3520 symbol_table_insert (symbol_new (regname, reg_section,
3521 RTYPE_VF | i, &zero_address_frag));
3522
3523 /* R5900 VU0 integer register. */
3524 snprintf (regname, sizeof (regname) - 1, "$vi%d", i);
3525 symbol_table_insert (symbol_new (regname, reg_section,
3526 RTYPE_VI | i, &zero_address_frag));
3527
56d438b1
CF
3528 /* MSA register. */
3529 snprintf (regname, sizeof (regname) - 1, "$w%d", i);
3530 symbol_table_insert (symbol_new (regname, reg_section,
3531 RTYPE_MSA | i, &zero_address_frag));
14daeee3
RS
3532 }
3533
a92713e6
RS
3534 obstack_init (&mips_operand_tokens);
3535
7d10b47d 3536 mips_no_prev_insn ();
252b5132
RH
3537
3538 mips_gprmask = 0;
3539 mips_cprmask[0] = 0;
3540 mips_cprmask[1] = 0;
3541 mips_cprmask[2] = 0;
3542 mips_cprmask[3] = 0;
3543
3544 /* set the default alignment for the text section (2**2) */
3545 record_alignment (text_section, 2);
3546
4d0d148d 3547 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132 3548
f3ded42a
RS
3549 /* On a native system other than VxWorks, sections must be aligned
3550 to 16 byte boundaries. When configured for an embedded ELF
3551 target, we don't bother. */
3552 if (strncmp (TARGET_OS, "elf", 3) != 0
3553 && strncmp (TARGET_OS, "vxworks", 7) != 0)
252b5132 3554 {
f3ded42a
RS
3555 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
3556 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
3557 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
3558 }
252b5132 3559
f3ded42a
RS
3560 /* Create a .reginfo section for register masks and a .mdebug
3561 section for debugging information. */
3562 {
3563 segT seg;
3564 subsegT subseg;
3565 flagword flags;
3566 segT sec;
3567
3568 seg = now_seg;
3569 subseg = now_subseg;
3570
3571 /* The ABI says this section should be loaded so that the
3572 running program can access it. However, we don't load it
3573 if we are configured for an embedded target */
3574 flags = SEC_READONLY | SEC_DATA;
3575 if (strncmp (TARGET_OS, "elf", 3) != 0)
3576 flags |= SEC_ALLOC | SEC_LOAD;
3577
3578 if (mips_abi != N64_ABI)
252b5132 3579 {
f3ded42a 3580 sec = subseg_new (".reginfo", (subsegT) 0);
bdaaa2e1 3581
f3ded42a
RS
3582 bfd_set_section_flags (stdoutput, sec, flags);
3583 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
252b5132 3584
f3ded42a
RS
3585 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
3586 }
3587 else
3588 {
3589 /* The 64-bit ABI uses a .MIPS.options section rather than
3590 .reginfo section. */
3591 sec = subseg_new (".MIPS.options", (subsegT) 0);
3592 bfd_set_section_flags (stdoutput, sec, flags);
3593 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132 3594
f3ded42a
RS
3595 /* Set up the option header. */
3596 {
3597 Elf_Internal_Options opthdr;
3598 char *f;
3599
3600 opthdr.kind = ODK_REGINFO;
3601 opthdr.size = (sizeof (Elf_External_Options)
3602 + sizeof (Elf64_External_RegInfo));
3603 opthdr.section = 0;
3604 opthdr.info = 0;
3605 f = frag_more (sizeof (Elf_External_Options));
3606 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
3607 (Elf_External_Options *) f);
3608
3609 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
3610 }
3611 }
252b5132 3612
f3ded42a
RS
3613 if (ECOFF_DEBUGGING)
3614 {
3615 sec = subseg_new (".mdebug", (subsegT) 0);
3616 (void) bfd_set_section_flags (stdoutput, sec,
3617 SEC_HAS_CONTENTS | SEC_READONLY);
3618 (void) bfd_set_section_alignment (stdoutput, sec, 2);
252b5132 3619 }
f3ded42a
RS
3620 else if (mips_flag_pdr)
3621 {
3622 pdr_seg = subseg_new (".pdr", (subsegT) 0);
3623 (void) bfd_set_section_flags (stdoutput, pdr_seg,
3624 SEC_READONLY | SEC_RELOC
3625 | SEC_DEBUGGING);
3626 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
3627 }
3628
3629 subseg_set (seg, subseg);
3630 }
252b5132
RH
3631
3632 if (! ECOFF_DEBUGGING)
3633 md_obj_begin ();
71400594
RS
3634
3635 if (mips_fix_vr4120)
3636 init_vr4120_conflicts ();
252b5132
RH
3637}
3638
3639void
17a2f251 3640md_mips_end (void)
252b5132 3641{
02b1ab82 3642 mips_emit_delays ();
252b5132
RH
3643 if (! ECOFF_DEBUGGING)
3644 md_obj_end ();
3645}
3646
3647void
17a2f251 3648md_assemble (char *str)
252b5132
RH
3649{
3650 struct mips_cl_insn insn;
f6688943
TS
3651 bfd_reloc_code_real_type unused_reloc[3]
3652 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
3653
3654 imm_expr.X_op = O_absent;
252b5132 3655 offset_expr.X_op = O_absent;
f6688943
TS
3656 offset_reloc[0] = BFD_RELOC_UNUSED;
3657 offset_reloc[1] = BFD_RELOC_UNUSED;
3658 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132 3659
e1b47bd5
RS
3660 mips_mark_labels ();
3661 mips_assembling_insn = TRUE;
e3de51ce 3662 clear_insn_error ();
e1b47bd5 3663
252b5132
RH
3664 if (mips_opts.mips16)
3665 mips16_ip (str, &insn);
3666 else
3667 {
3668 mips_ip (str, &insn);
beae10d5
KH
3669 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
3670 str, insn.insn_opcode));
252b5132
RH
3671 }
3672
e3de51ce
RS
3673 if (insn_error.msg)
3674 report_insn_error (str);
e1b47bd5 3675 else if (insn.insn_mo->pinfo == INSN_MACRO)
252b5132 3676 {
584892a6 3677 macro_start ();
252b5132
RH
3678 if (mips_opts.mips16)
3679 mips16_macro (&insn);
3680 else
833794fc 3681 macro (&insn, str);
584892a6 3682 macro_end ();
252b5132
RH
3683 }
3684 else
3685 {
77bd4346 3686 if (offset_expr.X_op != O_absent)
df58fc94 3687 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
252b5132 3688 else
df58fc94 3689 append_insn (&insn, NULL, unused_reloc, FALSE);
252b5132 3690 }
e1b47bd5
RS
3691
3692 mips_assembling_insn = FALSE;
252b5132
RH
3693}
3694
738e5348
RS
3695/* Convenience functions for abstracting away the differences between
3696 MIPS16 and non-MIPS16 relocations. */
3697
3698static inline bfd_boolean
3699mips16_reloc_p (bfd_reloc_code_real_type reloc)
3700{
3701 switch (reloc)
3702 {
3703 case BFD_RELOC_MIPS16_JMP:
3704 case BFD_RELOC_MIPS16_GPREL:
3705 case BFD_RELOC_MIPS16_GOT16:
3706 case BFD_RELOC_MIPS16_CALL16:
3707 case BFD_RELOC_MIPS16_HI16_S:
3708 case BFD_RELOC_MIPS16_HI16:
3709 case BFD_RELOC_MIPS16_LO16:
3710 return TRUE;
3711
3712 default:
3713 return FALSE;
3714 }
3715}
3716
df58fc94
RS
3717static inline bfd_boolean
3718micromips_reloc_p (bfd_reloc_code_real_type reloc)
3719{
3720 switch (reloc)
3721 {
3722 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
3723 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
3724 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
3725 case BFD_RELOC_MICROMIPS_GPREL16:
3726 case BFD_RELOC_MICROMIPS_JMP:
3727 case BFD_RELOC_MICROMIPS_HI16:
3728 case BFD_RELOC_MICROMIPS_HI16_S:
3729 case BFD_RELOC_MICROMIPS_LO16:
3730 case BFD_RELOC_MICROMIPS_LITERAL:
3731 case BFD_RELOC_MICROMIPS_GOT16:
3732 case BFD_RELOC_MICROMIPS_CALL16:
3733 case BFD_RELOC_MICROMIPS_GOT_HI16:
3734 case BFD_RELOC_MICROMIPS_GOT_LO16:
3735 case BFD_RELOC_MICROMIPS_CALL_HI16:
3736 case BFD_RELOC_MICROMIPS_CALL_LO16:
3737 case BFD_RELOC_MICROMIPS_SUB:
3738 case BFD_RELOC_MICROMIPS_GOT_PAGE:
3739 case BFD_RELOC_MICROMIPS_GOT_OFST:
3740 case BFD_RELOC_MICROMIPS_GOT_DISP:
3741 case BFD_RELOC_MICROMIPS_HIGHEST:
3742 case BFD_RELOC_MICROMIPS_HIGHER:
3743 case BFD_RELOC_MICROMIPS_SCN_DISP:
3744 case BFD_RELOC_MICROMIPS_JALR:
3745 return TRUE;
3746
3747 default:
3748 return FALSE;
3749 }
3750}
3751
2309ddf2
MR
3752static inline bfd_boolean
3753jmp_reloc_p (bfd_reloc_code_real_type reloc)
3754{
3755 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
3756}
3757
738e5348
RS
3758static inline bfd_boolean
3759got16_reloc_p (bfd_reloc_code_real_type reloc)
3760{
2309ddf2 3761 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
df58fc94 3762 || reloc == BFD_RELOC_MICROMIPS_GOT16);
738e5348
RS
3763}
3764
3765static inline bfd_boolean
3766hi16_reloc_p (bfd_reloc_code_real_type reloc)
3767{
2309ddf2 3768 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
df58fc94 3769 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
738e5348
RS
3770}
3771
3772static inline bfd_boolean
3773lo16_reloc_p (bfd_reloc_code_real_type reloc)
3774{
2309ddf2 3775 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
df58fc94
RS
3776 || reloc == BFD_RELOC_MICROMIPS_LO16);
3777}
3778
df58fc94
RS
3779static inline bfd_boolean
3780jalr_reloc_p (bfd_reloc_code_real_type reloc)
3781{
2309ddf2 3782 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
738e5348
RS
3783}
3784
f2ae14a1
RS
3785static inline bfd_boolean
3786gprel16_reloc_p (bfd_reloc_code_real_type reloc)
3787{
3788 return (reloc == BFD_RELOC_GPREL16 || reloc == BFD_RELOC_MIPS16_GPREL
3789 || reloc == BFD_RELOC_MICROMIPS_GPREL16);
3790}
3791
2de39019
CM
3792/* Return true if RELOC is a PC-relative relocation that does not have
3793 full address range. */
3794
3795static inline bfd_boolean
3796limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
3797{
3798 switch (reloc)
3799 {
3800 case BFD_RELOC_16_PCREL_S2:
3801 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
3802 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
3803 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
3804 return TRUE;
3805
b47468a6
CM
3806 case BFD_RELOC_32_PCREL:
3807 return HAVE_64BIT_ADDRESSES;
3808
2de39019
CM
3809 default:
3810 return FALSE;
3811 }
3812}
b47468a6 3813
5919d012 3814/* Return true if the given relocation might need a matching %lo().
0a44bf69
RS
3815 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
3816 need a matching %lo() when applied to local symbols. */
5919d012
RS
3817
3818static inline bfd_boolean
17a2f251 3819reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 3820{
3b91255e 3821 return (HAVE_IN_PLACE_ADDENDS
738e5348 3822 && (hi16_reloc_p (reloc)
0a44bf69
RS
3823 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
3824 all GOT16 relocations evaluate to "G". */
738e5348
RS
3825 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
3826}
3827
3828/* Return the type of %lo() reloc needed by RELOC, given that
3829 reloc_needs_lo_p. */
3830
3831static inline bfd_reloc_code_real_type
3832matching_lo_reloc (bfd_reloc_code_real_type reloc)
3833{
df58fc94
RS
3834 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
3835 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
3836 : BFD_RELOC_LO16));
5919d012
RS
3837}
3838
3839/* Return true if the given fixup is followed by a matching R_MIPS_LO16
3840 relocation. */
3841
3842static inline bfd_boolean
17a2f251 3843fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
3844{
3845 return (fixp->fx_next != NULL
738e5348 3846 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
5919d012
RS
3847 && fixp->fx_addsy == fixp->fx_next->fx_addsy
3848 && fixp->fx_offset == fixp->fx_next->fx_offset);
3849}
3850
462427c4
RS
3851/* Move all labels in LABELS to the current insertion point. TEXT_P
3852 says whether the labels refer to text or data. */
404a8071
RS
3853
3854static void
462427c4 3855mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
404a8071
RS
3856{
3857 struct insn_label_list *l;
3858 valueT val;
3859
462427c4 3860 for (l = labels; l != NULL; l = l->next)
404a8071 3861 {
9c2799c2 3862 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
404a8071
RS
3863 symbol_set_frag (l->label, frag_now);
3864 val = (valueT) frag_now_fix ();
df58fc94 3865 /* MIPS16/microMIPS text labels are stored as odd. */
462427c4 3866 if (text_p && HAVE_CODE_COMPRESSION)
404a8071
RS
3867 ++val;
3868 S_SET_VALUE (l->label, val);
3869 }
3870}
3871
462427c4
RS
3872/* Move all labels in insn_labels to the current insertion point
3873 and treat them as text labels. */
3874
3875static void
3876mips_move_text_labels (void)
3877{
3878 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
3879}
3880
5f0fe04b
TS
3881static bfd_boolean
3882s_is_linkonce (symbolS *sym, segT from_seg)
3883{
3884 bfd_boolean linkonce = FALSE;
3885 segT symseg = S_GET_SEGMENT (sym);
3886
3887 if (symseg != from_seg && !S_IS_LOCAL (sym))
3888 {
3889 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
3890 linkonce = TRUE;
5f0fe04b
TS
3891 /* The GNU toolchain uses an extension for ELF: a section
3892 beginning with the magic string .gnu.linkonce is a
3893 linkonce section. */
3894 if (strncmp (segment_name (symseg), ".gnu.linkonce",
3895 sizeof ".gnu.linkonce" - 1) == 0)
3896 linkonce = TRUE;
5f0fe04b
TS
3897 }
3898 return linkonce;
3899}
3900
e1b47bd5 3901/* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
df58fc94
RS
3902 linker to handle them specially, such as generating jalx instructions
3903 when needed. We also make them odd for the duration of the assembly,
3904 in order to generate the right sort of code. We will make them even
252b5132
RH
3905 in the adjust_symtab routine, while leaving them marked. This is
3906 convenient for the debugger and the disassembler. The linker knows
3907 to make them odd again. */
3908
3909static void
e1b47bd5 3910mips_compressed_mark_label (symbolS *label)
252b5132 3911{
df58fc94 3912 gas_assert (HAVE_CODE_COMPRESSION);
a8dbcb85 3913
f3ded42a
RS
3914 if (mips_opts.mips16)
3915 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
3916 else
3917 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
e1b47bd5
RS
3918 if ((S_GET_VALUE (label) & 1) == 0
3919 /* Don't adjust the address if the label is global or weak, or
3920 in a link-once section, since we'll be emitting symbol reloc
3921 references to it which will be patched up by the linker, and
3922 the final value of the symbol may or may not be MIPS16/microMIPS. */
3923 && !S_IS_WEAK (label)
3924 && !S_IS_EXTERNAL (label)
3925 && !s_is_linkonce (label, now_seg))
3926 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
3927}
3928
3929/* Mark preceding MIPS16 or microMIPS instruction labels. */
3930
3931static void
3932mips_compressed_mark_labels (void)
3933{
3934 struct insn_label_list *l;
3935
3936 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
3937 mips_compressed_mark_label (l->label);
252b5132
RH
3938}
3939
4d7206a2
RS
3940/* End the current frag. Make it a variant frag and record the
3941 relaxation info. */
3942
3943static void
3944relax_close_frag (void)
3945{
584892a6 3946 mips_macro_warning.first_frag = frag_now;
4d7206a2 3947 frag_var (rs_machine_dependent, 0, 0,
584892a6 3948 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
4d7206a2
RS
3949 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
3950
3951 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
3952 mips_relax.first_fixup = 0;
3953}
3954
3955/* Start a new relaxation sequence whose expansion depends on SYMBOL.
3956 See the comment above RELAX_ENCODE for more details. */
3957
3958static void
3959relax_start (symbolS *symbol)
3960{
9c2799c2 3961 gas_assert (mips_relax.sequence == 0);
4d7206a2
RS
3962 mips_relax.sequence = 1;
3963 mips_relax.symbol = symbol;
3964}
3965
3966/* Start generating the second version of a relaxable sequence.
3967 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
3968
3969static void
4d7206a2
RS
3970relax_switch (void)
3971{
9c2799c2 3972 gas_assert (mips_relax.sequence == 1);
4d7206a2
RS
3973 mips_relax.sequence = 2;
3974}
3975
3976/* End the current relaxable sequence. */
3977
3978static void
3979relax_end (void)
3980{
9c2799c2 3981 gas_assert (mips_relax.sequence == 2);
4d7206a2
RS
3982 relax_close_frag ();
3983 mips_relax.sequence = 0;
3984}
3985
11625dd8
RS
3986/* Return true if IP is a delayed branch or jump. */
3987
3988static inline bfd_boolean
3989delayed_branch_p (const struct mips_cl_insn *ip)
3990{
3991 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
3992 | INSN_COND_BRANCH_DELAY
3993 | INSN_COND_BRANCH_LIKELY)) != 0;
3994}
3995
3996/* Return true if IP is a compact branch or jump. */
3997
3998static inline bfd_boolean
3999compact_branch_p (const struct mips_cl_insn *ip)
4000{
26545944
RS
4001 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
4002 | INSN2_COND_BRANCH)) != 0;
11625dd8
RS
4003}
4004
4005/* Return true if IP is an unconditional branch or jump. */
4006
4007static inline bfd_boolean
4008uncond_branch_p (const struct mips_cl_insn *ip)
4009{
4010 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
26545944 4011 || (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0);
11625dd8
RS
4012}
4013
4014/* Return true if IP is a branch-likely instruction. */
4015
4016static inline bfd_boolean
4017branch_likely_p (const struct mips_cl_insn *ip)
4018{
4019 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
4020}
4021
14fe068b
RS
4022/* Return the type of nop that should be used to fill the delay slot
4023 of delayed branch IP. */
4024
4025static struct mips_cl_insn *
4026get_delay_slot_nop (const struct mips_cl_insn *ip)
4027{
4028 if (mips_opts.micromips
4029 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
4030 return &micromips_nop32_insn;
4031 return NOP_INSN;
4032}
4033
fc76e730
RS
4034/* Return a mask that has bit N set if OPCODE reads the register(s)
4035 in operand N. */
df58fc94
RS
4036
4037static unsigned int
fc76e730 4038insn_read_mask (const struct mips_opcode *opcode)
df58fc94 4039{
fc76e730
RS
4040 return (opcode->pinfo & INSN_READ_ALL) >> INSN_READ_SHIFT;
4041}
df58fc94 4042
fc76e730
RS
4043/* Return a mask that has bit N set if OPCODE writes to the register(s)
4044 in operand N. */
4045
4046static unsigned int
4047insn_write_mask (const struct mips_opcode *opcode)
4048{
4049 return (opcode->pinfo & INSN_WRITE_ALL) >> INSN_WRITE_SHIFT;
4050}
4051
4052/* Return a mask of the registers specified by operand OPERAND of INSN.
4053 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4054 is set. */
4055
4056static unsigned int
4057operand_reg_mask (const struct mips_cl_insn *insn,
4058 const struct mips_operand *operand,
4059 unsigned int type_mask)
4060{
4061 unsigned int uval, vsel;
4062
4063 switch (operand->type)
df58fc94 4064 {
fc76e730
RS
4065 case OP_INT:
4066 case OP_MAPPED_INT:
4067 case OP_MSB:
4068 case OP_PCREL:
4069 case OP_PERF_REG:
4070 case OP_ADDIUSP_INT:
4071 case OP_ENTRY_EXIT_LIST:
4072 case OP_REPEAT_DEST_REG:
4073 case OP_REPEAT_PREV_REG:
4074 case OP_PC:
14daeee3
RS
4075 case OP_VU0_SUFFIX:
4076 case OP_VU0_MATCH_SUFFIX:
56d438b1 4077 case OP_IMM_INDEX:
fc76e730
RS
4078 abort ();
4079
4080 case OP_REG:
0f35dbc4 4081 case OP_OPTIONAL_REG:
fc76e730
RS
4082 {
4083 const struct mips_reg_operand *reg_op;
4084
4085 reg_op = (const struct mips_reg_operand *) operand;
4086 if (!(type_mask & (1 << reg_op->reg_type)))
4087 return 0;
4088 uval = insn_extract_operand (insn, operand);
4089 return 1 << mips_decode_reg_operand (reg_op, uval);
4090 }
4091
4092 case OP_REG_PAIR:
4093 {
4094 const struct mips_reg_pair_operand *pair_op;
4095
4096 pair_op = (const struct mips_reg_pair_operand *) operand;
4097 if (!(type_mask & (1 << pair_op->reg_type)))
4098 return 0;
4099 uval = insn_extract_operand (insn, operand);
4100 return (1 << pair_op->reg1_map[uval]) | (1 << pair_op->reg2_map[uval]);
4101 }
4102
4103 case OP_CLO_CLZ_DEST:
4104 if (!(type_mask & (1 << OP_REG_GP)))
4105 return 0;
4106 uval = insn_extract_operand (insn, operand);
4107 return (1 << (uval & 31)) | (1 << (uval >> 5));
4108
4109 case OP_LWM_SWM_LIST:
4110 abort ();
4111
4112 case OP_SAVE_RESTORE_LIST:
4113 abort ();
4114
4115 case OP_MDMX_IMM_REG:
4116 if (!(type_mask & (1 << OP_REG_VEC)))
4117 return 0;
4118 uval = insn_extract_operand (insn, operand);
4119 vsel = uval >> 5;
4120 if ((vsel & 0x18) == 0x18)
4121 return 0;
4122 return 1 << (uval & 31);
56d438b1
CF
4123
4124 case OP_REG_INDEX:
4125 if (!(type_mask & (1 << OP_REG_GP)))
4126 return 0;
4127 return 1 << insn_extract_operand (insn, operand);
df58fc94 4128 }
fc76e730
RS
4129 abort ();
4130}
4131
4132/* Return a mask of the registers specified by operands OPNO_MASK of INSN,
4133 where bit N of OPNO_MASK is set if operand N should be included.
4134 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4135 is set. */
4136
4137static unsigned int
4138insn_reg_mask (const struct mips_cl_insn *insn,
4139 unsigned int type_mask, unsigned int opno_mask)
4140{
4141 unsigned int opno, reg_mask;
4142
4143 opno = 0;
4144 reg_mask = 0;
4145 while (opno_mask != 0)
4146 {
4147 if (opno_mask & 1)
4148 reg_mask |= operand_reg_mask (insn, insn_opno (insn, opno), type_mask);
4149 opno_mask >>= 1;
4150 opno += 1;
4151 }
4152 return reg_mask;
df58fc94
RS
4153}
4154
4c260379
RS
4155/* Return the mask of core registers that IP reads. */
4156
4157static unsigned int
4158gpr_read_mask (const struct mips_cl_insn *ip)
4159{
4160 unsigned long pinfo, pinfo2;
4161 unsigned int mask;
4162
fc76e730 4163 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_read_mask (ip->insn_mo));
4c260379
RS
4164 pinfo = ip->insn_mo->pinfo;
4165 pinfo2 = ip->insn_mo->pinfo2;
fc76e730 4166 if (pinfo & INSN_UDI)
4c260379 4167 {
fc76e730
RS
4168 /* UDI instructions have traditionally been assumed to read RS
4169 and RT. */
4170 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4171 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4c260379 4172 }
fc76e730
RS
4173 if (pinfo & INSN_READ_GPR_24)
4174 mask |= 1 << 24;
4175 if (pinfo2 & INSN2_READ_GPR_16)
4176 mask |= 1 << 16;
4177 if (pinfo2 & INSN2_READ_SP)
4178 mask |= 1 << SP;
26545944 4179 if (pinfo2 & INSN2_READ_GPR_31)
fc76e730 4180 mask |= 1 << 31;
fe35f09f
RS
4181 /* Don't include register 0. */
4182 return mask & ~1;
4c260379
RS
4183}
4184
4185/* Return the mask of core registers that IP writes. */
4186
4187static unsigned int
4188gpr_write_mask (const struct mips_cl_insn *ip)
4189{
4190 unsigned long pinfo, pinfo2;
4191 unsigned int mask;
4192
fc76e730 4193 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_write_mask (ip->insn_mo));
4c260379
RS
4194 pinfo = ip->insn_mo->pinfo;
4195 pinfo2 = ip->insn_mo->pinfo2;
fc76e730
RS
4196 if (pinfo & INSN_WRITE_GPR_24)
4197 mask |= 1 << 24;
4198 if (pinfo & INSN_WRITE_GPR_31)
4199 mask |= 1 << 31;
4200 if (pinfo & INSN_UDI)
4201 /* UDI instructions have traditionally been assumed to write to RD. */
4202 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4203 if (pinfo2 & INSN2_WRITE_SP)
4204 mask |= 1 << SP;
fe35f09f
RS
4205 /* Don't include register 0. */
4206 return mask & ~1;
4c260379
RS
4207}
4208
4209/* Return the mask of floating-point registers that IP reads. */
4210
4211static unsigned int
4212fpr_read_mask (const struct mips_cl_insn *ip)
4213{
fc76e730 4214 unsigned long pinfo;
4c260379
RS
4215 unsigned int mask;
4216
9d5de888
CF
4217 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4218 | (1 << OP_REG_MSA)),
fc76e730 4219 insn_read_mask (ip->insn_mo));
4c260379 4220 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4221 /* Conservatively treat all operands to an FP_D instruction are doubles.
4222 (This is overly pessimistic for things like cvt.d.s.) */
bad1aba3 4223 if (FPR_SIZE != 64 && (pinfo & FP_D))
4c260379
RS
4224 mask |= mask << 1;
4225 return mask;
4226}
4227
4228/* Return the mask of floating-point registers that IP writes. */
4229
4230static unsigned int
4231fpr_write_mask (const struct mips_cl_insn *ip)
4232{
fc76e730 4233 unsigned long pinfo;
4c260379
RS
4234 unsigned int mask;
4235
9d5de888
CF
4236 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4237 | (1 << OP_REG_MSA)),
fc76e730 4238 insn_write_mask (ip->insn_mo));
4c260379 4239 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4240 /* Conservatively treat all operands to an FP_D instruction are doubles.
4241 (This is overly pessimistic for things like cvt.s.d.) */
bad1aba3 4242 if (FPR_SIZE != 64 && (pinfo & FP_D))
4c260379
RS
4243 mask |= mask << 1;
4244 return mask;
4245}
4246
a1d78564
RS
4247/* Operand OPNUM of INSN is an odd-numbered floating-point register.
4248 Check whether that is allowed. */
4249
4250static bfd_boolean
4251mips_oddfpreg_ok (const struct mips_opcode *insn, int opnum)
4252{
4253 const char *s = insn->name;
4254
4255 if (insn->pinfo == INSN_MACRO)
4256 /* Let a macro pass, we'll catch it later when it is expanded. */
4257 return TRUE;
4258
4259 if (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa) || mips_opts.arch == CPU_R5900)
4260 {
4261 /* Allow odd registers for single-precision ops. */
4262 switch (insn->pinfo & (FP_S | FP_D))
4263 {
4264 case FP_S:
4265 case 0:
4266 return TRUE;
4267 case FP_D:
4268 return FALSE;
4269 default:
4270 break;
4271 }
4272
4273 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
4274 s = strchr (insn->name, '.');
4275 if (s != NULL && opnum == 2)
4276 s = strchr (s + 1, '.');
4277 return (s != NULL && (s[1] == 'w' || s[1] == 's'));
4278 }
4279
4280 /* Single-precision coprocessor loads and moves are OK too. */
4281 if ((insn->pinfo & FP_S)
4282 && (insn->pinfo & (INSN_COPROC_MEMORY_DELAY | INSN_STORE_MEMORY
4283 | INSN_LOAD_COPROC_DELAY | INSN_COPROC_MOVE_DELAY)))
4284 return TRUE;
4285
4286 return FALSE;
4287}
4288
a1d78564
RS
4289/* Information about an instruction argument that we're trying to match. */
4290struct mips_arg_info
4291{
4292 /* The instruction so far. */
4293 struct mips_cl_insn *insn;
4294
a92713e6
RS
4295 /* The first unconsumed operand token. */
4296 struct mips_operand_token *token;
4297
a1d78564
RS
4298 /* The 1-based operand number, in terms of insn->insn_mo->args. */
4299 int opnum;
4300
4301 /* The 1-based argument number, for error reporting. This does not
4302 count elided optional registers, etc.. */
4303 int argnum;
4304
4305 /* The last OP_REG operand seen, or ILLEGAL_REG if none. */
4306 unsigned int last_regno;
4307
4308 /* If the first operand was an OP_REG, this is the register that it
4309 specified, otherwise it is ILLEGAL_REG. */
4310 unsigned int dest_regno;
4311
4312 /* The value of the last OP_INT operand. Only used for OP_MSB,
4313 where it gives the lsb position. */
4314 unsigned int last_op_int;
4315
60f20e8b
RS
4316 /* If true, match routines should assume that no later instruction
4317 alternative matches and should therefore be as accomodating as
4318 possible. Match routines should not report errors if something
4319 is only invalid for !LAX_MATCH. */
4320 bfd_boolean lax_match;
a1d78564 4321
a1d78564
RS
4322 /* True if a reference to the current AT register was seen. */
4323 bfd_boolean seen_at;
4324};
4325
1a00e612
RS
4326/* Record that the argument is out of range. */
4327
4328static void
4329match_out_of_range (struct mips_arg_info *arg)
4330{
4331 set_insn_error_i (arg->argnum, _("operand %d out of range"), arg->argnum);
4332}
4333
4334/* Record that the argument isn't constant but needs to be. */
4335
4336static void
4337match_not_constant (struct mips_arg_info *arg)
4338{
4339 set_insn_error_i (arg->argnum, _("operand %d must be constant"),
4340 arg->argnum);
4341}
4342
a92713e6
RS
4343/* Try to match an OT_CHAR token for character CH. Consume the token
4344 and return true on success, otherwise return false. */
a1d78564 4345
a92713e6
RS
4346static bfd_boolean
4347match_char (struct mips_arg_info *arg, char ch)
a1d78564 4348{
a92713e6
RS
4349 if (arg->token->type == OT_CHAR && arg->token->u.ch == ch)
4350 {
4351 ++arg->token;
4352 if (ch == ',')
4353 arg->argnum += 1;
4354 return TRUE;
4355 }
4356 return FALSE;
4357}
a1d78564 4358
a92713e6
RS
4359/* Try to get an expression from the next tokens in ARG. Consume the
4360 tokens and return true on success, storing the expression value in
4361 VALUE and relocation types in R. */
4362
4363static bfd_boolean
4364match_expression (struct mips_arg_info *arg, expressionS *value,
4365 bfd_reloc_code_real_type *r)
4366{
d436c1c2
RS
4367 /* If the next token is a '(' that was parsed as being part of a base
4368 expression, assume we have an elided offset. The later match will fail
4369 if this turns out to be wrong. */
4370 if (arg->token->type == OT_CHAR && arg->token->u.ch == '(')
a1d78564 4371 {
d436c1c2
RS
4372 value->X_op = O_constant;
4373 value->X_add_number = 0;
4374 r[0] = r[1] = r[2] = BFD_RELOC_UNUSED;
a92713e6
RS
4375 return TRUE;
4376 }
4377
d436c1c2
RS
4378 /* Reject register-based expressions such as "0+$2" and "(($2))".
4379 For plain registers the default error seems more appropriate. */
4380 if (arg->token->type == OT_INTEGER
4381 && arg->token->u.integer.value.X_op == O_register)
a92713e6 4382 {
d436c1c2
RS
4383 set_insn_error (arg->argnum, _("register value used as expression"));
4384 return FALSE;
a1d78564 4385 }
d436c1c2
RS
4386
4387 if (arg->token->type == OT_INTEGER)
a92713e6 4388 {
d436c1c2
RS
4389 *value = arg->token->u.integer.value;
4390 memcpy (r, arg->token->u.integer.relocs, 3 * sizeof (*r));
4391 ++arg->token;
4392 return TRUE;
a92713e6 4393 }
a92713e6 4394
d436c1c2
RS
4395 set_insn_error_i
4396 (arg->argnum, _("operand %d must be an immediate expression"),
4397 arg->argnum);
4398 return FALSE;
a92713e6
RS
4399}
4400
4401/* Try to get a constant expression from the next tokens in ARG. Consume
4402 the tokens and return return true on success, storing the constant value
4403 in *VALUE. Use FALLBACK as the value if the match succeeded with an
4404 error. */
4405
4406static bfd_boolean
1a00e612 4407match_const_int (struct mips_arg_info *arg, offsetT *value)
a92713e6
RS
4408{
4409 expressionS ex;
4410 bfd_reloc_code_real_type r[3];
a1d78564 4411
a92713e6
RS
4412 if (!match_expression (arg, &ex, r))
4413 return FALSE;
4414
4415 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_constant)
a1d78564
RS
4416 *value = ex.X_add_number;
4417 else
4418 {
1a00e612
RS
4419 match_not_constant (arg);
4420 return FALSE;
a1d78564 4421 }
a92713e6 4422 return TRUE;
a1d78564
RS
4423}
4424
4425/* Return the RTYPE_* flags for a register operand of type TYPE that
4426 appears in instruction OPCODE. */
4427
4428static unsigned int
4429convert_reg_type (const struct mips_opcode *opcode,
4430 enum mips_reg_operand_type type)
4431{
4432 switch (type)
4433 {
4434 case OP_REG_GP:
4435 return RTYPE_NUM | RTYPE_GP;
4436
4437 case OP_REG_FP:
4438 /* Allow vector register names for MDMX if the instruction is a 64-bit
4439 FPR load, store or move (including moves to and from GPRs). */
4440 if ((mips_opts.ase & ASE_MDMX)
4441 && (opcode->pinfo & FP_D)
4442 && (opcode->pinfo & (INSN_COPROC_MOVE_DELAY
4443 | INSN_COPROC_MEMORY_DELAY
4444 | INSN_LOAD_COPROC_DELAY
67dc82bc 4445 | INSN_LOAD_MEMORY
a1d78564
RS
4446 | INSN_STORE_MEMORY)))
4447 return RTYPE_FPU | RTYPE_VEC;
4448 return RTYPE_FPU;
4449
4450 case OP_REG_CCC:
4451 if (opcode->pinfo & (FP_D | FP_S))
4452 return RTYPE_CCC | RTYPE_FCC;
4453 return RTYPE_CCC;
4454
4455 case OP_REG_VEC:
4456 if (opcode->membership & INSN_5400)
4457 return RTYPE_FPU;
4458 return RTYPE_FPU | RTYPE_VEC;
4459
4460 case OP_REG_ACC:
4461 return RTYPE_ACC;
4462
4463 case OP_REG_COPRO:
4464 if (opcode->name[strlen (opcode->name) - 1] == '0')
4465 return RTYPE_NUM | RTYPE_CP0;
4466 return RTYPE_NUM;
4467
4468 case OP_REG_HW:
4469 return RTYPE_NUM;
14daeee3
RS
4470
4471 case OP_REG_VI:
4472 return RTYPE_NUM | RTYPE_VI;
4473
4474 case OP_REG_VF:
4475 return RTYPE_NUM | RTYPE_VF;
4476
4477 case OP_REG_R5900_I:
4478 return RTYPE_R5900_I;
4479
4480 case OP_REG_R5900_Q:
4481 return RTYPE_R5900_Q;
4482
4483 case OP_REG_R5900_R:
4484 return RTYPE_R5900_R;
4485
4486 case OP_REG_R5900_ACC:
4487 return RTYPE_R5900_ACC;
56d438b1
CF
4488
4489 case OP_REG_MSA:
4490 return RTYPE_MSA;
4491
4492 case OP_REG_MSA_CTRL:
4493 return RTYPE_NUM;
a1d78564
RS
4494 }
4495 abort ();
4496}
4497
4498/* ARG is register REGNO, of type TYPE. Warn about any dubious registers. */
4499
4500static void
4501check_regno (struct mips_arg_info *arg,
4502 enum mips_reg_operand_type type, unsigned int regno)
4503{
4504 if (AT && type == OP_REG_GP && regno == AT)
4505 arg->seen_at = TRUE;
4506
4507 if (type == OP_REG_FP
4508 && (regno & 1) != 0
bad1aba3 4509 && FPR_SIZE != 64
a1d78564 4510 && !mips_oddfpreg_ok (arg->insn->insn_mo, arg->opnum))
1661c76c 4511 as_warn (_("float register should be even, was %d"), regno);
a1d78564
RS
4512
4513 if (type == OP_REG_CCC)
4514 {
4515 const char *name;
4516 size_t length;
4517
4518 name = arg->insn->insn_mo->name;
4519 length = strlen (name);
4520 if ((regno & 1) != 0
4521 && ((length >= 3 && strcmp (name + length - 3, ".ps") == 0)
4522 || (length >= 5 && strncmp (name + length - 5, "any2", 4) == 0)))
1661c76c 4523 as_warn (_("condition code register should be even for %s, was %d"),
a1d78564
RS
4524 name, regno);
4525
4526 if ((regno & 3) != 0
4527 && (length >= 5 && strncmp (name + length - 5, "any4", 4) == 0))
1661c76c 4528 as_warn (_("condition code register should be 0 or 4 for %s, was %d"),
a1d78564
RS
4529 name, regno);
4530 }
4531}
4532
a92713e6
RS
4533/* ARG is a register with symbol value SYMVAL. Try to interpret it as
4534 a register of type TYPE. Return true on success, storing the register
4535 number in *REGNO and warning about any dubious uses. */
4536
4537static bfd_boolean
4538match_regno (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4539 unsigned int symval, unsigned int *regno)
4540{
4541 if (type == OP_REG_VEC)
4542 symval = mips_prefer_vec_regno (symval);
4543 if (!(symval & convert_reg_type (arg->insn->insn_mo, type)))
4544 return FALSE;
4545
4546 *regno = symval & RNUM_MASK;
4547 check_regno (arg, type, *regno);
4548 return TRUE;
4549}
4550
4551/* Try to interpret the next token in ARG as a register of type TYPE.
4552 Consume the token and return true on success, storing the register
4553 number in *REGNO. Return false on failure. */
4554
4555static bfd_boolean
4556match_reg (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4557 unsigned int *regno)
4558{
4559 if (arg->token->type == OT_REG
4560 && match_regno (arg, type, arg->token->u.regno, regno))
4561 {
4562 ++arg->token;
4563 return TRUE;
4564 }
4565 return FALSE;
4566}
4567
4568/* Try to interpret the next token in ARG as a range of registers of type TYPE.
4569 Consume the token and return true on success, storing the register numbers
4570 in *REGNO1 and *REGNO2. Return false on failure. */
4571
4572static bfd_boolean
4573match_reg_range (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4574 unsigned int *regno1, unsigned int *regno2)
4575{
4576 if (match_reg (arg, type, regno1))
4577 {
4578 *regno2 = *regno1;
4579 return TRUE;
4580 }
4581 if (arg->token->type == OT_REG_RANGE
4582 && match_regno (arg, type, arg->token->u.reg_range.regno1, regno1)
4583 && match_regno (arg, type, arg->token->u.reg_range.regno2, regno2)
4584 && *regno1 <= *regno2)
4585 {
4586 ++arg->token;
4587 return TRUE;
4588 }
4589 return FALSE;
4590}
4591
a1d78564
RS
4592/* OP_INT matcher. */
4593
a92713e6 4594static bfd_boolean
a1d78564 4595match_int_operand (struct mips_arg_info *arg,
a92713e6 4596 const struct mips_operand *operand_base)
a1d78564
RS
4597{
4598 const struct mips_int_operand *operand;
3ccad066 4599 unsigned int uval;
a1d78564
RS
4600 int min_val, max_val, factor;
4601 offsetT sval;
a1d78564
RS
4602
4603 operand = (const struct mips_int_operand *) operand_base;
4604 factor = 1 << operand->shift;
3ccad066
RS
4605 min_val = mips_int_operand_min (operand);
4606 max_val = mips_int_operand_max (operand);
a1d78564 4607
d436c1c2
RS
4608 if (operand_base->lsb == 0
4609 && operand_base->size == 16
4610 && operand->shift == 0
4611 && operand->bias == 0
4612 && (operand->max_val == 32767 || operand->max_val == 65535))
a1d78564
RS
4613 {
4614 /* The operand can be relocated. */
a92713e6
RS
4615 if (!match_expression (arg, &offset_expr, offset_reloc))
4616 return FALSE;
4617
4618 if (offset_reloc[0] != BFD_RELOC_UNUSED)
a1d78564
RS
4619 /* Relocation operators were used. Accept the arguent and
4620 leave the relocation value in offset_expr and offset_relocs
4621 for the caller to process. */
a92713e6
RS
4622 return TRUE;
4623
4624 if (offset_expr.X_op != O_constant)
a1d78564 4625 {
60f20e8b
RS
4626 /* Accept non-constant operands if no later alternative matches,
4627 leaving it for the caller to process. */
4628 if (!arg->lax_match)
4629 return FALSE;
a92713e6
RS
4630 offset_reloc[0] = BFD_RELOC_LO16;
4631 return TRUE;
a1d78564 4632 }
a92713e6 4633
a1d78564
RS
4634 /* Clear the global state; we're going to install the operand
4635 ourselves. */
a92713e6 4636 sval = offset_expr.X_add_number;
a1d78564 4637 offset_expr.X_op = O_absent;
60f20e8b
RS
4638
4639 /* For compatibility with older assemblers, we accept
4640 0x8000-0xffff as signed 16-bit numbers when only
4641 signed numbers are allowed. */
4642 if (sval > max_val)
4643 {
4644 max_val = ((1 << operand_base->size) - 1) << operand->shift;
4645 if (!arg->lax_match && sval <= max_val)
4646 return FALSE;
4647 }
a1d78564
RS
4648 }
4649 else
4650 {
1a00e612 4651 if (!match_const_int (arg, &sval))
a92713e6 4652 return FALSE;
a1d78564
RS
4653 }
4654
4655 arg->last_op_int = sval;
4656
1a00e612 4657 if (sval < min_val || sval > max_val || sval % factor)
a1d78564 4658 {
1a00e612
RS
4659 match_out_of_range (arg);
4660 return FALSE;
a1d78564
RS
4661 }
4662
4663 uval = (unsigned int) sval >> operand->shift;
4664 uval -= operand->bias;
4665
4666 /* Handle -mfix-cn63xxp1. */
4667 if (arg->opnum == 1
4668 && mips_fix_cn63xxp1
4669 && !mips_opts.micromips
4670 && strcmp ("pref", arg->insn->insn_mo->name) == 0)
4671 switch (uval)
4672 {
4673 case 5:
4674 case 25:
4675 case 26:
4676 case 27:
4677 case 28:
4678 case 29:
4679 case 30:
4680 case 31:
4681 /* These are ok. */
4682 break;
4683
4684 default:
4685 /* The rest must be changed to 28. */
4686 uval = 28;
4687 break;
4688 }
4689
4690 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 4691 return TRUE;
a1d78564
RS
4692}
4693
4694/* OP_MAPPED_INT matcher. */
4695
a92713e6 4696static bfd_boolean
a1d78564 4697match_mapped_int_operand (struct mips_arg_info *arg,
a92713e6 4698 const struct mips_operand *operand_base)
a1d78564
RS
4699{
4700 const struct mips_mapped_int_operand *operand;
4701 unsigned int uval, num_vals;
4702 offsetT sval;
4703
4704 operand = (const struct mips_mapped_int_operand *) operand_base;
1a00e612 4705 if (!match_const_int (arg, &sval))
a92713e6 4706 return FALSE;
a1d78564
RS
4707
4708 num_vals = 1 << operand_base->size;
4709 for (uval = 0; uval < num_vals; uval++)
4710 if (operand->int_map[uval] == sval)
4711 break;
4712 if (uval == num_vals)
1a00e612
RS
4713 {
4714 match_out_of_range (arg);
4715 return FALSE;
4716 }
a1d78564
RS
4717
4718 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 4719 return TRUE;
a1d78564
RS
4720}
4721
4722/* OP_MSB matcher. */
4723
a92713e6 4724static bfd_boolean
a1d78564 4725match_msb_operand (struct mips_arg_info *arg,
a92713e6 4726 const struct mips_operand *operand_base)
a1d78564
RS
4727{
4728 const struct mips_msb_operand *operand;
4729 int min_val, max_val, max_high;
4730 offsetT size, sval, high;
4731
4732 operand = (const struct mips_msb_operand *) operand_base;
4733 min_val = operand->bias;
4734 max_val = min_val + (1 << operand_base->size) - 1;
4735 max_high = operand->opsize;
4736
1a00e612 4737 if (!match_const_int (arg, &size))
a92713e6 4738 return FALSE;
a1d78564
RS
4739
4740 high = size + arg->last_op_int;
4741 sval = operand->add_lsb ? high : size;
4742
4743 if (size < 0 || high > max_high || sval < min_val || sval > max_val)
4744 {
1a00e612
RS
4745 match_out_of_range (arg);
4746 return FALSE;
a1d78564
RS
4747 }
4748 insn_insert_operand (arg->insn, operand_base, sval - min_val);
a92713e6 4749 return TRUE;
a1d78564
RS
4750}
4751
4752/* OP_REG matcher. */
4753
a92713e6 4754static bfd_boolean
a1d78564 4755match_reg_operand (struct mips_arg_info *arg,
a92713e6 4756 const struct mips_operand *operand_base)
a1d78564
RS
4757{
4758 const struct mips_reg_operand *operand;
a92713e6 4759 unsigned int regno, uval, num_vals;
a1d78564
RS
4760
4761 operand = (const struct mips_reg_operand *) operand_base;
a92713e6
RS
4762 if (!match_reg (arg, operand->reg_type, &regno))
4763 return FALSE;
a1d78564
RS
4764
4765 if (operand->reg_map)
4766 {
4767 num_vals = 1 << operand->root.size;
4768 for (uval = 0; uval < num_vals; uval++)
4769 if (operand->reg_map[uval] == regno)
4770 break;
4771 if (num_vals == uval)
a92713e6 4772 return FALSE;
a1d78564
RS
4773 }
4774 else
4775 uval = regno;
4776
a1d78564
RS
4777 arg->last_regno = regno;
4778 if (arg->opnum == 1)
4779 arg->dest_regno = regno;
4780 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 4781 return TRUE;
a1d78564
RS
4782}
4783
4784/* OP_REG_PAIR matcher. */
4785
a92713e6 4786static bfd_boolean
a1d78564 4787match_reg_pair_operand (struct mips_arg_info *arg,
a92713e6 4788 const struct mips_operand *operand_base)
a1d78564
RS
4789{
4790 const struct mips_reg_pair_operand *operand;
a92713e6 4791 unsigned int regno1, regno2, uval, num_vals;
a1d78564
RS
4792
4793 operand = (const struct mips_reg_pair_operand *) operand_base;
a92713e6
RS
4794 if (!match_reg (arg, operand->reg_type, &regno1)
4795 || !match_char (arg, ',')
4796 || !match_reg (arg, operand->reg_type, &regno2))
4797 return FALSE;
a1d78564
RS
4798
4799 num_vals = 1 << operand_base->size;
4800 for (uval = 0; uval < num_vals; uval++)
4801 if (operand->reg1_map[uval] == regno1 && operand->reg2_map[uval] == regno2)
4802 break;
4803 if (uval == num_vals)
a92713e6 4804 return FALSE;
a1d78564 4805
a1d78564 4806 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 4807 return TRUE;
a1d78564
RS
4808}
4809
4810/* OP_PCREL matcher. The caller chooses the relocation type. */
4811
a92713e6
RS
4812static bfd_boolean
4813match_pcrel_operand (struct mips_arg_info *arg)
a1d78564 4814{
a92713e6
RS
4815 bfd_reloc_code_real_type r[3];
4816
4817 return match_expression (arg, &offset_expr, r) && r[0] == BFD_RELOC_UNUSED;
a1d78564
RS
4818}
4819
4820/* OP_PERF_REG matcher. */
4821
a92713e6 4822static bfd_boolean
a1d78564 4823match_perf_reg_operand (struct mips_arg_info *arg,
a92713e6 4824 const struct mips_operand *operand)
a1d78564
RS
4825{
4826 offsetT sval;
4827
1a00e612 4828 if (!match_const_int (arg, &sval))
a92713e6 4829 return FALSE;
a1d78564
RS
4830
4831 if (sval != 0
4832 && (sval != 1
4833 || (mips_opts.arch == CPU_R5900
4834 && (strcmp (arg->insn->insn_mo->name, "mfps") == 0
4835 || strcmp (arg->insn->insn_mo->name, "mtps") == 0))))
4836 {
1a00e612
RS
4837 set_insn_error (arg->argnum, _("invalid performance register"));
4838 return FALSE;
a1d78564
RS
4839 }
4840
4841 insn_insert_operand (arg->insn, operand, sval);
a92713e6 4842 return TRUE;
a1d78564
RS
4843}
4844
4845/* OP_ADDIUSP matcher. */
4846
a92713e6 4847static bfd_boolean
a1d78564 4848match_addiusp_operand (struct mips_arg_info *arg,
a92713e6 4849 const struct mips_operand *operand)
a1d78564
RS
4850{
4851 offsetT sval;
4852 unsigned int uval;
4853
1a00e612 4854 if (!match_const_int (arg, &sval))
a92713e6 4855 return FALSE;
a1d78564
RS
4856
4857 if (sval % 4)
1a00e612
RS
4858 {
4859 match_out_of_range (arg);
4860 return FALSE;
4861 }
a1d78564
RS
4862
4863 sval /= 4;
4864 if (!(sval >= -258 && sval <= 257) || (sval >= -2 && sval <= 1))
1a00e612
RS
4865 {
4866 match_out_of_range (arg);
4867 return FALSE;
4868 }
a1d78564
RS
4869
4870 uval = (unsigned int) sval;
4871 uval = ((uval >> 1) & ~0xff) | (uval & 0xff);
4872 insn_insert_operand (arg->insn, operand, uval);
a92713e6 4873 return TRUE;
a1d78564
RS
4874}
4875
4876/* OP_CLO_CLZ_DEST matcher. */
4877
a92713e6 4878static bfd_boolean
a1d78564 4879match_clo_clz_dest_operand (struct mips_arg_info *arg,
a92713e6 4880 const struct mips_operand *operand)
a1d78564
RS
4881{
4882 unsigned int regno;
4883
a92713e6
RS
4884 if (!match_reg (arg, OP_REG_GP, &regno))
4885 return FALSE;
a1d78564 4886
a1d78564 4887 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
a92713e6 4888 return TRUE;
a1d78564
RS
4889}
4890
4891/* OP_LWM_SWM_LIST matcher. */
4892
a92713e6 4893static bfd_boolean
a1d78564 4894match_lwm_swm_list_operand (struct mips_arg_info *arg,
a92713e6 4895 const struct mips_operand *operand)
a1d78564 4896{
a92713e6
RS
4897 unsigned int reglist, sregs, ra, regno1, regno2;
4898 struct mips_arg_info reset;
a1d78564 4899
a92713e6
RS
4900 reglist = 0;
4901 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
4902 return FALSE;
4903 do
4904 {
4905 if (regno2 == FP && regno1 >= S0 && regno1 <= S7)
4906 {
4907 reglist |= 1 << FP;
4908 regno2 = S7;
4909 }
4910 reglist |= ((1U << regno2 << 1) - 1) & -(1U << regno1);
4911 reset = *arg;
4912 }
4913 while (match_char (arg, ',')
4914 && match_reg_range (arg, OP_REG_GP, &regno1, &regno2));
4915 *arg = reset;
a1d78564
RS
4916
4917 if (operand->size == 2)
4918 {
4919 /* The list must include both ra and s0-sN, for 0 <= N <= 3. E.g.:
4920
4921 s0, ra
4922 s0, s1, ra, s2, s3
4923 s0-s2, ra
4924
4925 and any permutations of these. */
4926 if ((reglist & 0xfff1ffff) != 0x80010000)
a92713e6 4927 return FALSE;
a1d78564
RS
4928
4929 sregs = (reglist >> 17) & 7;
4930 ra = 0;
4931 }
4932 else
4933 {
4934 /* The list must include at least one of ra and s0-sN,
4935 for 0 <= N <= 8. (Note that there is a gap between s7 and s8,
4936 which are $23 and $30 respectively.) E.g.:
4937
4938 ra
4939 s0
4940 ra, s0, s1, s2
4941 s0-s8
4942 s0-s5, ra
4943
4944 and any permutations of these. */
4945 if ((reglist & 0x3f00ffff) != 0)
a92713e6 4946 return FALSE;
a1d78564
RS
4947
4948 ra = (reglist >> 27) & 0x10;
4949 sregs = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
4950 }
4951 sregs += 1;
4952 if ((sregs & -sregs) != sregs)
a92713e6 4953 return FALSE;
a1d78564
RS
4954
4955 insn_insert_operand (arg->insn, operand, (ffs (sregs) - 1) | ra);
a92713e6 4956 return TRUE;
a1d78564
RS
4957}
4958
364215c8
RS
4959/* OP_ENTRY_EXIT_LIST matcher. */
4960
a92713e6 4961static unsigned int
364215c8 4962match_entry_exit_operand (struct mips_arg_info *arg,
a92713e6 4963 const struct mips_operand *operand)
364215c8
RS
4964{
4965 unsigned int mask;
4966 bfd_boolean is_exit;
4967
4968 /* The format is the same for both ENTRY and EXIT, but the constraints
4969 are different. */
4970 is_exit = strcmp (arg->insn->insn_mo->name, "exit") == 0;
4971 mask = (is_exit ? 7 << 3 : 0);
a92713e6 4972 do
364215c8
RS
4973 {
4974 unsigned int regno1, regno2;
4975 bfd_boolean is_freg;
4976
a92713e6 4977 if (match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
364215c8 4978 is_freg = FALSE;
a92713e6 4979 else if (match_reg_range (arg, OP_REG_FP, &regno1, &regno2))
364215c8
RS
4980 is_freg = TRUE;
4981 else
a92713e6 4982 return FALSE;
364215c8
RS
4983
4984 if (is_exit && is_freg && regno1 == 0 && regno2 < 2)
4985 {
4986 mask &= ~(7 << 3);
4987 mask |= (5 + regno2) << 3;
4988 }
4989 else if (!is_exit && regno1 == 4 && regno2 >= 4 && regno2 <= 7)
4990 mask |= (regno2 - 3) << 3;
4991 else if (regno1 == 16 && regno2 >= 16 && regno2 <= 17)
4992 mask |= (regno2 - 15) << 1;
4993 else if (regno1 == RA && regno2 == RA)
4994 mask |= 1;
4995 else
a92713e6 4996 return FALSE;
364215c8 4997 }
a92713e6
RS
4998 while (match_char (arg, ','));
4999
364215c8 5000 insn_insert_operand (arg->insn, operand, mask);
a92713e6 5001 return TRUE;
364215c8
RS
5002}
5003
5004/* OP_SAVE_RESTORE_LIST matcher. */
5005
a92713e6
RS
5006static bfd_boolean
5007match_save_restore_list_operand (struct mips_arg_info *arg)
364215c8
RS
5008{
5009 unsigned int opcode, args, statics, sregs;
5010 unsigned int num_frame_sizes, num_args, num_statics, num_sregs;
364215c8 5011 offsetT frame_size;
364215c8 5012
364215c8
RS
5013 opcode = arg->insn->insn_opcode;
5014 frame_size = 0;
5015 num_frame_sizes = 0;
5016 args = 0;
5017 statics = 0;
5018 sregs = 0;
a92713e6 5019 do
364215c8
RS
5020 {
5021 unsigned int regno1, regno2;
5022
a92713e6 5023 if (arg->token->type == OT_INTEGER)
364215c8
RS
5024 {
5025 /* Handle the frame size. */
1a00e612 5026 if (!match_const_int (arg, &frame_size))
a92713e6 5027 return FALSE;
364215c8 5028 num_frame_sizes += 1;
364215c8
RS
5029 }
5030 else
5031 {
a92713e6
RS
5032 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5033 return FALSE;
364215c8
RS
5034
5035 while (regno1 <= regno2)
5036 {
5037 if (regno1 >= 4 && regno1 <= 7)
5038 {
5039 if (num_frame_sizes == 0)
5040 /* args $a0-$a3 */
5041 args |= 1 << (regno1 - 4);
5042 else
5043 /* statics $a0-$a3 */
5044 statics |= 1 << (regno1 - 4);
5045 }
5046 else if (regno1 >= 16 && regno1 <= 23)
5047 /* $s0-$s7 */
5048 sregs |= 1 << (regno1 - 16);
5049 else if (regno1 == 30)
5050 /* $s8 */
5051 sregs |= 1 << 8;
5052 else if (regno1 == 31)
5053 /* Add $ra to insn. */
5054 opcode |= 0x40;
5055 else
a92713e6 5056 return FALSE;
364215c8
RS
5057 regno1 += 1;
5058 if (regno1 == 24)
5059 regno1 = 30;
5060 }
5061 }
364215c8 5062 }
a92713e6 5063 while (match_char (arg, ','));
364215c8
RS
5064
5065 /* Encode args/statics combination. */
5066 if (args & statics)
a92713e6 5067 return FALSE;
364215c8
RS
5068 else if (args == 0xf)
5069 /* All $a0-$a3 are args. */
5070 opcode |= MIPS16_ALL_ARGS << 16;
5071 else if (statics == 0xf)
5072 /* All $a0-$a3 are statics. */
5073 opcode |= MIPS16_ALL_STATICS << 16;
5074 else
5075 {
5076 /* Count arg registers. */
5077 num_args = 0;
5078 while (args & 0x1)
5079 {
5080 args >>= 1;
5081 num_args += 1;
5082 }
5083 if (args != 0)
a92713e6 5084 return FALSE;
364215c8
RS
5085
5086 /* Count static registers. */
5087 num_statics = 0;
5088 while (statics & 0x8)
5089 {
5090 statics = (statics << 1) & 0xf;
5091 num_statics += 1;
5092 }
5093 if (statics != 0)
a92713e6 5094 return FALSE;
364215c8
RS
5095
5096 /* Encode args/statics. */
5097 opcode |= ((num_args << 2) | num_statics) << 16;
5098 }
5099
5100 /* Encode $s0/$s1. */
5101 if (sregs & (1 << 0)) /* $s0 */
5102 opcode |= 0x20;
5103 if (sregs & (1 << 1)) /* $s1 */
5104 opcode |= 0x10;
5105 sregs >>= 2;
5106
5107 /* Encode $s2-$s8. */
5108 num_sregs = 0;
5109 while (sregs & 1)
5110 {
5111 sregs >>= 1;
5112 num_sregs += 1;
5113 }
5114 if (sregs != 0)
a92713e6 5115 return FALSE;
364215c8
RS
5116 opcode |= num_sregs << 24;
5117
5118 /* Encode frame size. */
5119 if (num_frame_sizes == 0)
1a00e612
RS
5120 {
5121 set_insn_error (arg->argnum, _("missing frame size"));
5122 return FALSE;
5123 }
5124 if (num_frame_sizes > 1)
5125 {
5126 set_insn_error (arg->argnum, _("frame size specified twice"));
5127 return FALSE;
5128 }
5129 if ((frame_size & 7) != 0 || frame_size < 0 || frame_size > 0xff * 8)
5130 {
5131 set_insn_error (arg->argnum, _("invalid frame size"));
5132 return FALSE;
5133 }
5134 if (frame_size != 128 || (opcode >> 16) != 0)
364215c8
RS
5135 {
5136 frame_size /= 8;
5137 opcode |= (((frame_size & 0xf0) << 16)
5138 | (frame_size & 0x0f));
5139 }
5140
364215c8
RS
5141 /* Finally build the instruction. */
5142 if ((opcode >> 16) != 0 || frame_size == 0)
5143 opcode |= MIPS16_EXTEND;
5144 arg->insn->insn_opcode = opcode;
a92713e6 5145 return TRUE;
364215c8
RS
5146}
5147
a1d78564
RS
5148/* OP_MDMX_IMM_REG matcher. */
5149
a92713e6 5150static bfd_boolean
a1d78564 5151match_mdmx_imm_reg_operand (struct mips_arg_info *arg,
a92713e6 5152 const struct mips_operand *operand)
a1d78564 5153{
a92713e6 5154 unsigned int regno, uval;
a1d78564
RS
5155 bfd_boolean is_qh;
5156 const struct mips_opcode *opcode;
5157
5158 /* The mips_opcode records whether this is an octobyte or quadhalf
5159 instruction. Start out with that bit in place. */
5160 opcode = arg->insn->insn_mo;
5161 uval = mips_extract_operand (operand, opcode->match);
5162 is_qh = (uval != 0);
5163
56d438b1 5164 if (arg->token->type == OT_REG)
a1d78564
RS
5165 {
5166 if ((opcode->membership & INSN_5400)
5167 && strcmp (opcode->name, "rzu.ob") == 0)
5168 {
1a00e612
RS
5169 set_insn_error_i (arg->argnum, _("operand %d must be an immediate"),
5170 arg->argnum);
5171 return FALSE;
a1d78564
RS
5172 }
5173
56d438b1
CF
5174 if (!match_regno (arg, OP_REG_VEC, arg->token->u.regno, &regno))
5175 return FALSE;
5176 ++arg->token;
5177
a1d78564
RS
5178 /* Check whether this is a vector register or a broadcast of
5179 a single element. */
56d438b1 5180 if (arg->token->type == OT_INTEGER_INDEX)
a1d78564 5181 {
56d438b1 5182 if (arg->token->u.index > (is_qh ? 3 : 7))
a1d78564 5183 {
1a00e612
RS
5184 set_insn_error (arg->argnum, _("invalid element selector"));
5185 return FALSE;
a1d78564 5186 }
56d438b1
CF
5187 uval |= arg->token->u.index << (is_qh ? 2 : 1) << 5;
5188 ++arg->token;
a1d78564
RS
5189 }
5190 else
5191 {
5192 /* A full vector. */
5193 if ((opcode->membership & INSN_5400)
5194 && (strcmp (opcode->name, "sll.ob") == 0
5195 || strcmp (opcode->name, "srl.ob") == 0))
5196 {
1a00e612
RS
5197 set_insn_error_i (arg->argnum, _("operand %d must be scalar"),
5198 arg->argnum);
5199 return FALSE;
a1d78564
RS
5200 }
5201
5202 if (is_qh)
5203 uval |= MDMX_FMTSEL_VEC_QH << 5;
5204 else
5205 uval |= MDMX_FMTSEL_VEC_OB << 5;
5206 }
a1d78564
RS
5207 uval |= regno;
5208 }
5209 else
5210 {
5211 offsetT sval;
5212
1a00e612 5213 if (!match_const_int (arg, &sval))
a92713e6 5214 return FALSE;
a1d78564
RS
5215 if (sval < 0 || sval > 31)
5216 {
1a00e612
RS
5217 match_out_of_range (arg);
5218 return FALSE;
a1d78564
RS
5219 }
5220 uval |= (sval & 31);
5221 if (is_qh)
5222 uval |= MDMX_FMTSEL_IMM_QH << 5;
5223 else
5224 uval |= MDMX_FMTSEL_IMM_OB << 5;
5225 }
5226 insn_insert_operand (arg->insn, operand, uval);
a92713e6 5227 return TRUE;
a1d78564
RS
5228}
5229
56d438b1
CF
5230/* OP_IMM_INDEX matcher. */
5231
5232static bfd_boolean
5233match_imm_index_operand (struct mips_arg_info *arg,
5234 const struct mips_operand *operand)
5235{
5236 unsigned int max_val;
5237
5238 if (arg->token->type != OT_INTEGER_INDEX)
5239 return FALSE;
5240
5241 max_val = (1 << operand->size) - 1;
5242 if (arg->token->u.index > max_val)
5243 {
5244 match_out_of_range (arg);
5245 return FALSE;
5246 }
5247 insn_insert_operand (arg->insn, operand, arg->token->u.index);
5248 ++arg->token;
5249 return TRUE;
5250}
5251
5252/* OP_REG_INDEX matcher. */
5253
5254static bfd_boolean
5255match_reg_index_operand (struct mips_arg_info *arg,
5256 const struct mips_operand *operand)
5257{
5258 unsigned int regno;
5259
5260 if (arg->token->type != OT_REG_INDEX)
5261 return FALSE;
5262
5263 if (!match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno))
5264 return FALSE;
5265
5266 insn_insert_operand (arg->insn, operand, regno);
5267 ++arg->token;
5268 return TRUE;
5269}
5270
a1d78564
RS
5271/* OP_PC matcher. */
5272
a92713e6
RS
5273static bfd_boolean
5274match_pc_operand (struct mips_arg_info *arg)
a1d78564 5275{
a92713e6
RS
5276 if (arg->token->type == OT_REG && (arg->token->u.regno & RTYPE_PC))
5277 {
5278 ++arg->token;
5279 return TRUE;
5280 }
5281 return FALSE;
a1d78564
RS
5282}
5283
5284/* OP_REPEAT_DEST_REG and OP_REPEAT_PREV_REG matcher. OTHER_REGNO is the
5285 register that we need to match. */
5286
a92713e6
RS
5287static bfd_boolean
5288match_tied_reg_operand (struct mips_arg_info *arg, unsigned int other_regno)
a1d78564
RS
5289{
5290 unsigned int regno;
5291
a92713e6 5292 return match_reg (arg, OP_REG_GP, &regno) && regno == other_regno;
a1d78564
RS
5293}
5294
89565f1b
RS
5295/* Read a floating-point constant from S for LI.S or LI.D. LENGTH is
5296 the length of the value in bytes (4 for float, 8 for double) and
5297 USING_GPRS says whether the destination is a GPR rather than an FPR.
5298
5299 Return the constant in IMM and OFFSET as follows:
5300
5301 - If the constant should be loaded via memory, set IMM to O_absent and
5302 OFFSET to the memory address.
5303
5304 - Otherwise, if the constant should be loaded into two 32-bit registers,
5305 set IMM to the O_constant to load into the high register and OFFSET
5306 to the corresponding value for the low register.
5307
5308 - Otherwise, set IMM to the full O_constant and set OFFSET to O_absent.
5309
5310 These constants only appear as the last operand in an instruction,
5311 and every instruction that accepts them in any variant accepts them
5312 in all variants. This means we don't have to worry about backing out
5313 any changes if the instruction does not match. We just match
5314 unconditionally and report an error if the constant is invalid. */
5315
a92713e6
RS
5316static bfd_boolean
5317match_float_constant (struct mips_arg_info *arg, expressionS *imm,
5318 expressionS *offset, int length, bfd_boolean using_gprs)
89565f1b 5319{
a92713e6 5320 char *p;
89565f1b
RS
5321 segT seg, new_seg;
5322 subsegT subseg;
5323 const char *newname;
a92713e6 5324 unsigned char *data;
89565f1b
RS
5325
5326 /* Where the constant is placed is based on how the MIPS assembler
5327 does things:
5328
5329 length == 4 && using_gprs -- immediate value only
5330 length == 8 && using_gprs -- .rdata or immediate value
5331 length == 4 && !using_gprs -- .lit4 or immediate value
5332 length == 8 && !using_gprs -- .lit8 or immediate value
5333
5334 The .lit4 and .lit8 sections are only used if permitted by the
5335 -G argument. */
a92713e6 5336 if (arg->token->type != OT_FLOAT)
1a00e612
RS
5337 {
5338 set_insn_error (arg->argnum, _("floating-point expression required"));
5339 return FALSE;
5340 }
a92713e6
RS
5341
5342 gas_assert (arg->token->u.flt.length == length);
5343 data = arg->token->u.flt.data;
5344 ++arg->token;
89565f1b
RS
5345
5346 /* Handle 32-bit constants for which an immediate value is best. */
5347 if (length == 4
5348 && (using_gprs
5349 || g_switch_value < 4
5350 || (data[0] == 0 && data[1] == 0)
5351 || (data[2] == 0 && data[3] == 0)))
5352 {
5353 imm->X_op = O_constant;
5354 if (!target_big_endian)
5355 imm->X_add_number = bfd_getl32 (data);
5356 else
5357 imm->X_add_number = bfd_getb32 (data);
5358 offset->X_op = O_absent;
a92713e6 5359 return TRUE;
89565f1b
RS
5360 }
5361
5362 /* Handle 64-bit constants for which an immediate value is best. */
5363 if (length == 8
5364 && !mips_disable_float_construction
5365 /* Constants can only be constructed in GPRs and copied
5366 to FPRs if the GPRs are at least as wide as the FPRs.
5367 Force the constant into memory if we are using 64-bit FPRs
5368 but the GPRs are only 32 bits wide. */
5369 /* ??? No longer true with the addition of MTHC1, but this
5370 is legacy code... */
bad1aba3 5371 && (using_gprs || !(FPR_SIZE == 64 && GPR_SIZE == 32))
89565f1b
RS
5372 && ((data[0] == 0 && data[1] == 0)
5373 || (data[2] == 0 && data[3] == 0))
5374 && ((data[4] == 0 && data[5] == 0)
5375 || (data[6] == 0 && data[7] == 0)))
5376 {
5377 /* The value is simple enough to load with a couple of instructions.
5378 If using 32-bit registers, set IMM to the high order 32 bits and
5379 OFFSET to the low order 32 bits. Otherwise, set IMM to the entire
5380 64 bit constant. */
bad1aba3 5381 if (using_gprs ? GPR_SIZE == 32 : FPR_SIZE != 64)
89565f1b
RS
5382 {
5383 imm->X_op = O_constant;
5384 offset->X_op = O_constant;
5385 if (!target_big_endian)
5386 {
5387 imm->X_add_number = bfd_getl32 (data + 4);
5388 offset->X_add_number = bfd_getl32 (data);
5389 }
5390 else
5391 {
5392 imm->X_add_number = bfd_getb32 (data);
5393 offset->X_add_number = bfd_getb32 (data + 4);
5394 }
5395 if (offset->X_add_number == 0)
5396 offset->X_op = O_absent;
5397 }
5398 else
5399 {
5400 imm->X_op = O_constant;
5401 if (!target_big_endian)
5402 imm->X_add_number = bfd_getl64 (data);
5403 else
5404 imm->X_add_number = bfd_getb64 (data);
5405 offset->X_op = O_absent;
5406 }
a92713e6 5407 return TRUE;
89565f1b
RS
5408 }
5409
5410 /* Switch to the right section. */
5411 seg = now_seg;
5412 subseg = now_subseg;
5413 if (length == 4)
5414 {
5415 gas_assert (!using_gprs && g_switch_value >= 4);
5416 newname = ".lit4";
5417 }
5418 else
5419 {
5420 if (using_gprs || g_switch_value < 8)
5421 newname = RDATA_SECTION_NAME;
5422 else
5423 newname = ".lit8";
5424 }
5425
5426 new_seg = subseg_new (newname, (subsegT) 0);
5427 bfd_set_section_flags (stdoutput, new_seg,
5428 SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA);
5429 frag_align (length == 4 ? 2 : 3, 0, 0);
5430 if (strncmp (TARGET_OS, "elf", 3) != 0)
5431 record_alignment (new_seg, 4);
5432 else
5433 record_alignment (new_seg, length == 4 ? 2 : 3);
5434 if (seg == now_seg)
1661c76c 5435 as_bad (_("cannot use `%s' in this section"), arg->insn->insn_mo->name);
89565f1b
RS
5436
5437 /* Set the argument to the current address in the section. */
5438 imm->X_op = O_absent;
5439 offset->X_op = O_symbol;
5440 offset->X_add_symbol = symbol_temp_new_now ();
5441 offset->X_add_number = 0;
5442
5443 /* Put the floating point number into the section. */
5444 p = frag_more (length);
5445 memcpy (p, data, length);
5446
5447 /* Switch back to the original section. */
5448 subseg_set (seg, subseg);
a92713e6 5449 return TRUE;
89565f1b
RS
5450}
5451
14daeee3
RS
5452/* OP_VU0_SUFFIX and OP_VU0_MATCH_SUFFIX matcher; MATCH_P selects between
5453 them. */
5454
5455static bfd_boolean
5456match_vu0_suffix_operand (struct mips_arg_info *arg,
5457 const struct mips_operand *operand,
5458 bfd_boolean match_p)
5459{
5460 unsigned int uval;
5461
5462 /* The operand can be an XYZW mask or a single 2-bit channel index
5463 (with X being 0). */
5464 gas_assert (operand->size == 2 || operand->size == 4);
5465
ee5734f0 5466 /* The suffix can be omitted when it is already part of the opcode. */
14daeee3 5467 if (arg->token->type != OT_CHANNELS)
ee5734f0 5468 return match_p;
14daeee3
RS
5469
5470 uval = arg->token->u.channels;
5471 if (operand->size == 2)
5472 {
5473 /* Check that a single bit is set and convert it into a 2-bit index. */
5474 if ((uval & -uval) != uval)
5475 return FALSE;
5476 uval = 4 - ffs (uval);
5477 }
5478
5479 if (match_p && insn_extract_operand (arg->insn, operand) != uval)
5480 return FALSE;
5481
5482 ++arg->token;
5483 if (!match_p)
5484 insn_insert_operand (arg->insn, operand, uval);
5485 return TRUE;
5486}
5487
a1d78564
RS
5488/* S is the text seen for ARG. Match it against OPERAND. Return the end
5489 of the argument text if the match is successful, otherwise return null. */
5490
a92713e6 5491static bfd_boolean
a1d78564 5492match_operand (struct mips_arg_info *arg,
a92713e6 5493 const struct mips_operand *operand)
a1d78564
RS
5494{
5495 switch (operand->type)
5496 {
5497 case OP_INT:
a92713e6 5498 return match_int_operand (arg, operand);
a1d78564
RS
5499
5500 case OP_MAPPED_INT:
a92713e6 5501 return match_mapped_int_operand (arg, operand);
a1d78564
RS
5502
5503 case OP_MSB:
a92713e6 5504 return match_msb_operand (arg, operand);
a1d78564
RS
5505
5506 case OP_REG:
0f35dbc4 5507 case OP_OPTIONAL_REG:
a92713e6 5508 return match_reg_operand (arg, operand);
a1d78564
RS
5509
5510 case OP_REG_PAIR:
a92713e6 5511 return match_reg_pair_operand (arg, operand);
a1d78564
RS
5512
5513 case OP_PCREL:
a92713e6 5514 return match_pcrel_operand (arg);
a1d78564
RS
5515
5516 case OP_PERF_REG:
a92713e6 5517 return match_perf_reg_operand (arg, operand);
a1d78564
RS
5518
5519 case OP_ADDIUSP_INT:
a92713e6 5520 return match_addiusp_operand (arg, operand);
a1d78564
RS
5521
5522 case OP_CLO_CLZ_DEST:
a92713e6 5523 return match_clo_clz_dest_operand (arg, operand);
a1d78564
RS
5524
5525 case OP_LWM_SWM_LIST:
a92713e6 5526 return match_lwm_swm_list_operand (arg, operand);
a1d78564
RS
5527
5528 case OP_ENTRY_EXIT_LIST:
a92713e6 5529 return match_entry_exit_operand (arg, operand);
364215c8 5530
a1d78564 5531 case OP_SAVE_RESTORE_LIST:
a92713e6 5532 return match_save_restore_list_operand (arg);
a1d78564
RS
5533
5534 case OP_MDMX_IMM_REG:
a92713e6 5535 return match_mdmx_imm_reg_operand (arg, operand);
a1d78564
RS
5536
5537 case OP_REPEAT_DEST_REG:
a92713e6 5538 return match_tied_reg_operand (arg, arg->dest_regno);
a1d78564
RS
5539
5540 case OP_REPEAT_PREV_REG:
a92713e6 5541 return match_tied_reg_operand (arg, arg->last_regno);
a1d78564
RS
5542
5543 case OP_PC:
a92713e6 5544 return match_pc_operand (arg);
14daeee3
RS
5545
5546 case OP_VU0_SUFFIX:
5547 return match_vu0_suffix_operand (arg, operand, FALSE);
5548
5549 case OP_VU0_MATCH_SUFFIX:
5550 return match_vu0_suffix_operand (arg, operand, TRUE);
56d438b1
CF
5551
5552 case OP_IMM_INDEX:
5553 return match_imm_index_operand (arg, operand);
5554
5555 case OP_REG_INDEX:
5556 return match_reg_index_operand (arg, operand);
a1d78564
RS
5557 }
5558 abort ();
5559}
5560
5561/* ARG is the state after successfully matching an instruction.
5562 Issue any queued-up warnings. */
5563
5564static void
5565check_completed_insn (struct mips_arg_info *arg)
5566{
5567 if (arg->seen_at)
5568 {
5569 if (AT == ATREG)
1661c76c 5570 as_warn (_("used $at without \".set noat\""));
a1d78564 5571 else
1661c76c 5572 as_warn (_("used $%u with \".set at=$%u\""), AT, AT);
a1d78564
RS
5573 }
5574}
a1d78564 5575
85fcb30f
RS
5576/* Return true if modifying general-purpose register REG needs a delay. */
5577
5578static bfd_boolean
5579reg_needs_delay (unsigned int reg)
5580{
5581 unsigned long prev_pinfo;
5582
5583 prev_pinfo = history[0].insn_mo->pinfo;
5584 if (!mips_opts.noreorder
67dc82bc 5585 && (((prev_pinfo & INSN_LOAD_MEMORY) && !gpr_interlocks)
85fcb30f
RS
5586 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY) && !cop_interlocks))
5587 && (gpr_write_mask (&history[0]) & (1 << reg)))
5588 return TRUE;
5589
5590 return FALSE;
5591}
5592
71400594
RS
5593/* Classify an instruction according to the FIX_VR4120_* enumeration.
5594 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
5595 by VR4120 errata. */
4d7206a2 5596
71400594
RS
5597static unsigned int
5598classify_vr4120_insn (const char *name)
252b5132 5599{
71400594
RS
5600 if (strncmp (name, "macc", 4) == 0)
5601 return FIX_VR4120_MACC;
5602 if (strncmp (name, "dmacc", 5) == 0)
5603 return FIX_VR4120_DMACC;
5604 if (strncmp (name, "mult", 4) == 0)
5605 return FIX_VR4120_MULT;
5606 if (strncmp (name, "dmult", 5) == 0)
5607 return FIX_VR4120_DMULT;
5608 if (strstr (name, "div"))
5609 return FIX_VR4120_DIV;
5610 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
5611 return FIX_VR4120_MTHILO;
5612 return NUM_FIX_VR4120_CLASSES;
5613}
252b5132 5614
a8d14a88
CM
5615#define INSN_ERET 0x42000018
5616#define INSN_DERET 0x4200001f
5617#define INSN_DMULT 0x1c
5618#define INSN_DMULTU 0x1d
ff239038 5619
71400594
RS
5620/* Return the number of instructions that must separate INSN1 and INSN2,
5621 where INSN1 is the earlier instruction. Return the worst-case value
5622 for any INSN2 if INSN2 is null. */
252b5132 5623
71400594
RS
5624static unsigned int
5625insns_between (const struct mips_cl_insn *insn1,
5626 const struct mips_cl_insn *insn2)
5627{
5628 unsigned long pinfo1, pinfo2;
4c260379 5629 unsigned int mask;
71400594 5630
85fcb30f
RS
5631 /* If INFO2 is null, pessimistically assume that all flags are set for
5632 the second instruction. */
71400594
RS
5633 pinfo1 = insn1->insn_mo->pinfo;
5634 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
252b5132 5635
71400594
RS
5636 /* For most targets, write-after-read dependencies on the HI and LO
5637 registers must be separated by at least two instructions. */
5638 if (!hilo_interlocks)
252b5132 5639 {
71400594
RS
5640 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
5641 return 2;
5642 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
5643 return 2;
5644 }
5645
5646 /* If we're working around r7000 errata, there must be two instructions
5647 between an mfhi or mflo and any instruction that uses the result. */
5648 if (mips_7000_hilo_fix
df58fc94 5649 && !mips_opts.micromips
71400594 5650 && MF_HILO_INSN (pinfo1)
85fcb30f 5651 && (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1))))
71400594
RS
5652 return 2;
5653
ff239038
CM
5654 /* If we're working around 24K errata, one instruction is required
5655 if an ERET or DERET is followed by a branch instruction. */
df58fc94 5656 if (mips_fix_24k && !mips_opts.micromips)
ff239038
CM
5657 {
5658 if (insn1->insn_opcode == INSN_ERET
5659 || insn1->insn_opcode == INSN_DERET)
5660 {
5661 if (insn2 == NULL
5662 || insn2->insn_opcode == INSN_ERET
5663 || insn2->insn_opcode == INSN_DERET
11625dd8 5664 || delayed_branch_p (insn2))
ff239038
CM
5665 return 1;
5666 }
5667 }
5668
a8d14a88
CM
5669 /* If we're working around PMC RM7000 errata, there must be three
5670 nops between a dmult and a load instruction. */
5671 if (mips_fix_rm7000 && !mips_opts.micromips)
5672 {
5673 if ((insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULT
5674 || (insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULTU)
5675 {
5676 if (pinfo2 & INSN_LOAD_MEMORY)
5677 return 3;
5678 }
5679 }
5680
71400594
RS
5681 /* If working around VR4120 errata, check for combinations that need
5682 a single intervening instruction. */
df58fc94 5683 if (mips_fix_vr4120 && !mips_opts.micromips)
71400594
RS
5684 {
5685 unsigned int class1, class2;
252b5132 5686
71400594
RS
5687 class1 = classify_vr4120_insn (insn1->insn_mo->name);
5688 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
252b5132 5689 {
71400594
RS
5690 if (insn2 == NULL)
5691 return 1;
5692 class2 = classify_vr4120_insn (insn2->insn_mo->name);
5693 if (vr4120_conflicts[class1] & (1 << class2))
5694 return 1;
252b5132 5695 }
71400594
RS
5696 }
5697
df58fc94 5698 if (!HAVE_CODE_COMPRESSION)
71400594
RS
5699 {
5700 /* Check for GPR or coprocessor load delays. All such delays
5701 are on the RT register. */
5702 /* Itbl support may require additional care here. */
67dc82bc 5703 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY))
71400594 5704 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
252b5132 5705 {
85fcb30f 5706 if (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1)))
71400594
RS
5707 return 1;
5708 }
5709
5710 /* Check for generic coprocessor hazards.
5711
5712 This case is not handled very well. There is no special
5713 knowledge of CP0 handling, and the coprocessors other than
5714 the floating point unit are not distinguished at all. */
5715 /* Itbl support may require additional care here. FIXME!
5716 Need to modify this to include knowledge about
5717 user specified delays! */
5718 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
5719 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
5720 {
5721 /* Handle cases where INSN1 writes to a known general coprocessor
5722 register. There must be a one instruction delay before INSN2
5723 if INSN2 reads that register, otherwise no delay is needed. */
4c260379
RS
5724 mask = fpr_write_mask (insn1);
5725 if (mask != 0)
252b5132 5726 {
4c260379 5727 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
71400594 5728 return 1;
252b5132
RH
5729 }
5730 else
5731 {
71400594
RS
5732 /* Read-after-write dependencies on the control registers
5733 require a two-instruction gap. */
5734 if ((pinfo1 & INSN_WRITE_COND_CODE)
5735 && (pinfo2 & INSN_READ_COND_CODE))
5736 return 2;
5737
5738 /* We don't know exactly what INSN1 does. If INSN2 is
5739 also a coprocessor instruction, assume there must be
5740 a one instruction gap. */
5741 if (pinfo2 & INSN_COP)
5742 return 1;
252b5132
RH
5743 }
5744 }
6b76fefe 5745
71400594
RS
5746 /* Check for read-after-write dependencies on the coprocessor
5747 control registers in cases where INSN1 does not need a general
5748 coprocessor delay. This means that INSN1 is a floating point
5749 comparison instruction. */
5750 /* Itbl support may require additional care here. */
5751 else if (!cop_interlocks
5752 && (pinfo1 & INSN_WRITE_COND_CODE)
5753 && (pinfo2 & INSN_READ_COND_CODE))
5754 return 1;
5755 }
6b76fefe 5756
71400594
RS
5757 return 0;
5758}
6b76fefe 5759
7d8e00cf
RS
5760/* Return the number of nops that would be needed to work around the
5761 VR4130 mflo/mfhi errata if instruction INSN immediately followed
932d1a1b
RS
5762 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
5763 that are contained within the first IGNORE instructions of HIST. */
7d8e00cf
RS
5764
5765static int
932d1a1b 5766nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
7d8e00cf
RS
5767 const struct mips_cl_insn *insn)
5768{
4c260379
RS
5769 int i, j;
5770 unsigned int mask;
7d8e00cf
RS
5771
5772 /* Check if the instruction writes to HI or LO. MTHI and MTLO
5773 are not affected by the errata. */
5774 if (insn != 0
5775 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
5776 || strcmp (insn->insn_mo->name, "mtlo") == 0
5777 || strcmp (insn->insn_mo->name, "mthi") == 0))
5778 return 0;
5779
5780 /* Search for the first MFLO or MFHI. */
5781 for (i = 0; i < MAX_VR4130_NOPS; i++)
91d6fa6a 5782 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
7d8e00cf
RS
5783 {
5784 /* Extract the destination register. */
4c260379 5785 mask = gpr_write_mask (&hist[i]);
7d8e00cf
RS
5786
5787 /* No nops are needed if INSN reads that register. */
4c260379 5788 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
7d8e00cf
RS
5789 return 0;
5790
5791 /* ...or if any of the intervening instructions do. */
5792 for (j = 0; j < i; j++)
4c260379 5793 if (gpr_read_mask (&hist[j]) & mask)
7d8e00cf
RS
5794 return 0;
5795
932d1a1b
RS
5796 if (i >= ignore)
5797 return MAX_VR4130_NOPS - i;
7d8e00cf
RS
5798 }
5799 return 0;
5800}
5801
15be625d
CM
5802#define BASE_REG_EQ(INSN1, INSN2) \
5803 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
5804 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
5805
5806/* Return the minimum alignment for this store instruction. */
5807
5808static int
5809fix_24k_align_to (const struct mips_opcode *mo)
5810{
5811 if (strcmp (mo->name, "sh") == 0)
5812 return 2;
5813
5814 if (strcmp (mo->name, "swc1") == 0
5815 || strcmp (mo->name, "swc2") == 0
5816 || strcmp (mo->name, "sw") == 0
5817 || strcmp (mo->name, "sc") == 0
5818 || strcmp (mo->name, "s.s") == 0)
5819 return 4;
5820
5821 if (strcmp (mo->name, "sdc1") == 0
5822 || strcmp (mo->name, "sdc2") == 0
5823 || strcmp (mo->name, "s.d") == 0)
5824 return 8;
5825
5826 /* sb, swl, swr */
5827 return 1;
5828}
5829
5830struct fix_24k_store_info
5831 {
5832 /* Immediate offset, if any, for this store instruction. */
5833 short off;
5834 /* Alignment required by this store instruction. */
5835 int align_to;
5836 /* True for register offsets. */
5837 int register_offset;
5838 };
5839
5840/* Comparison function used by qsort. */
5841
5842static int
5843fix_24k_sort (const void *a, const void *b)
5844{
5845 const struct fix_24k_store_info *pos1 = a;
5846 const struct fix_24k_store_info *pos2 = b;
5847
5848 return (pos1->off - pos2->off);
5849}
5850
5851/* INSN is a store instruction. Try to record the store information
5852 in STINFO. Return false if the information isn't known. */
5853
5854static bfd_boolean
5855fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
ab9794cf 5856 const struct mips_cl_insn *insn)
15be625d
CM
5857{
5858 /* The instruction must have a known offset. */
5859 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
5860 return FALSE;
5861
5862 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
5863 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
5864 return TRUE;
5865}
5866
932d1a1b
RS
5867/* Return the number of nops that would be needed to work around the 24k
5868 "lost data on stores during refill" errata if instruction INSN
5869 immediately followed the 2 instructions described by HIST.
5870 Ignore hazards that are contained within the first IGNORE
5871 instructions of HIST.
5872
5873 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
5874 for the data cache refills and store data. The following describes
5875 the scenario where the store data could be lost.
5876
5877 * A data cache miss, due to either a load or a store, causing fill
5878 data to be supplied by the memory subsystem
5879 * The first three doublewords of fill data are returned and written
5880 into the cache
5881 * A sequence of four stores occurs in consecutive cycles around the
5882 final doubleword of the fill:
5883 * Store A
5884 * Store B
5885 * Store C
5886 * Zero, One or more instructions
5887 * Store D
5888
5889 The four stores A-D must be to different doublewords of the line that
5890 is being filled. The fourth instruction in the sequence above permits
5891 the fill of the final doubleword to be transferred from the FSB into
5892 the cache. In the sequence above, the stores may be either integer
5893 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
5894 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
5895 different doublewords on the line. If the floating point unit is
5896 running in 1:2 mode, it is not possible to create the sequence above
5897 using only floating point store instructions.
15be625d
CM
5898
5899 In this case, the cache line being filled is incorrectly marked
5900 invalid, thereby losing the data from any store to the line that
5901 occurs between the original miss and the completion of the five
5902 cycle sequence shown above.
5903
932d1a1b 5904 The workarounds are:
15be625d 5905
932d1a1b
RS
5906 * Run the data cache in write-through mode.
5907 * Insert a non-store instruction between
5908 Store A and Store B or Store B and Store C. */
15be625d
CM
5909
5910static int
932d1a1b 5911nops_for_24k (int ignore, const struct mips_cl_insn *hist,
15be625d
CM
5912 const struct mips_cl_insn *insn)
5913{
5914 struct fix_24k_store_info pos[3];
5915 int align, i, base_offset;
5916
932d1a1b
RS
5917 if (ignore >= 2)
5918 return 0;
5919
ab9794cf
RS
5920 /* If the previous instruction wasn't a store, there's nothing to
5921 worry about. */
15be625d
CM
5922 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
5923 return 0;
5924
ab9794cf
RS
5925 /* If the instructions after the previous one are unknown, we have
5926 to assume the worst. */
5927 if (!insn)
15be625d
CM
5928 return 1;
5929
ab9794cf
RS
5930 /* Check whether we are dealing with three consecutive stores. */
5931 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
5932 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
15be625d
CM
5933 return 0;
5934
5935 /* If we don't know the relationship between the store addresses,
5936 assume the worst. */
ab9794cf 5937 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
15be625d
CM
5938 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
5939 return 1;
5940
5941 if (!fix_24k_record_store_info (&pos[0], insn)
5942 || !fix_24k_record_store_info (&pos[1], &hist[0])
5943 || !fix_24k_record_store_info (&pos[2], &hist[1]))
5944 return 1;
5945
5946 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
5947
5948 /* Pick a value of ALIGN and X such that all offsets are adjusted by
5949 X bytes and such that the base register + X is known to be aligned
5950 to align bytes. */
5951
5952 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
5953 align = 8;
5954 else
5955 {
5956 align = pos[0].align_to;
5957 base_offset = pos[0].off;
5958 for (i = 1; i < 3; i++)
5959 if (align < pos[i].align_to)
5960 {
5961 align = pos[i].align_to;
5962 base_offset = pos[i].off;
5963 }
5964 for (i = 0; i < 3; i++)
5965 pos[i].off -= base_offset;
5966 }
5967
5968 pos[0].off &= ~align + 1;
5969 pos[1].off &= ~align + 1;
5970 pos[2].off &= ~align + 1;
5971
5972 /* If any two stores write to the same chunk, they also write to the
5973 same doubleword. The offsets are still sorted at this point. */
5974 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
5975 return 0;
5976
5977 /* A range of at least 9 bytes is needed for the stores to be in
5978 non-overlapping doublewords. */
5979 if (pos[2].off - pos[0].off <= 8)
5980 return 0;
5981
5982 if (pos[2].off - pos[1].off >= 24
5983 || pos[1].off - pos[0].off >= 24
5984 || pos[2].off - pos[0].off >= 32)
5985 return 0;
5986
5987 return 1;
5988}
5989
71400594 5990/* Return the number of nops that would be needed if instruction INSN
91d6fa6a 5991 immediately followed the MAX_NOPS instructions given by HIST,
932d1a1b
RS
5992 where HIST[0] is the most recent instruction. Ignore hazards
5993 between INSN and the first IGNORE instructions in HIST.
5994
5995 If INSN is null, return the worse-case number of nops for any
5996 instruction. */
bdaaa2e1 5997
71400594 5998static int
932d1a1b 5999nops_for_insn (int ignore, const struct mips_cl_insn *hist,
71400594
RS
6000 const struct mips_cl_insn *insn)
6001{
6002 int i, nops, tmp_nops;
bdaaa2e1 6003
71400594 6004 nops = 0;
932d1a1b 6005 for (i = ignore; i < MAX_DELAY_NOPS; i++)
65b02341 6006 {
91d6fa6a 6007 tmp_nops = insns_between (hist + i, insn) - i;
65b02341
RS
6008 if (tmp_nops > nops)
6009 nops = tmp_nops;
6010 }
7d8e00cf 6011
df58fc94 6012 if (mips_fix_vr4130 && !mips_opts.micromips)
7d8e00cf 6013 {
932d1a1b 6014 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
7d8e00cf
RS
6015 if (tmp_nops > nops)
6016 nops = tmp_nops;
6017 }
6018
df58fc94 6019 if (mips_fix_24k && !mips_opts.micromips)
15be625d 6020 {
932d1a1b 6021 tmp_nops = nops_for_24k (ignore, hist, insn);
15be625d
CM
6022 if (tmp_nops > nops)
6023 nops = tmp_nops;
6024 }
6025
71400594
RS
6026 return nops;
6027}
252b5132 6028
71400594 6029/* The variable arguments provide NUM_INSNS extra instructions that
91d6fa6a 6030 might be added to HIST. Return the largest number of nops that
932d1a1b
RS
6031 would be needed after the extended sequence, ignoring hazards
6032 in the first IGNORE instructions. */
252b5132 6033
71400594 6034static int
932d1a1b
RS
6035nops_for_sequence (int num_insns, int ignore,
6036 const struct mips_cl_insn *hist, ...)
71400594
RS
6037{
6038 va_list args;
6039 struct mips_cl_insn buffer[MAX_NOPS];
6040 struct mips_cl_insn *cursor;
6041 int nops;
6042
91d6fa6a 6043 va_start (args, hist);
71400594 6044 cursor = buffer + num_insns;
91d6fa6a 6045 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
71400594
RS
6046 while (cursor > buffer)
6047 *--cursor = *va_arg (args, const struct mips_cl_insn *);
6048
932d1a1b 6049 nops = nops_for_insn (ignore, buffer, NULL);
71400594
RS
6050 va_end (args);
6051 return nops;
6052}
252b5132 6053
71400594
RS
6054/* Like nops_for_insn, but if INSN is a branch, take into account the
6055 worst-case delay for the branch target. */
252b5132 6056
71400594 6057static int
932d1a1b 6058nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
71400594
RS
6059 const struct mips_cl_insn *insn)
6060{
6061 int nops, tmp_nops;
60b63b72 6062
932d1a1b 6063 nops = nops_for_insn (ignore, hist, insn);
11625dd8 6064 if (delayed_branch_p (insn))
71400594 6065 {
932d1a1b 6066 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
14fe068b 6067 hist, insn, get_delay_slot_nop (insn));
71400594
RS
6068 if (tmp_nops > nops)
6069 nops = tmp_nops;
6070 }
11625dd8 6071 else if (compact_branch_p (insn))
71400594 6072 {
932d1a1b 6073 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
71400594
RS
6074 if (tmp_nops > nops)
6075 nops = tmp_nops;
6076 }
6077 return nops;
6078}
6079
c67a084a
NC
6080/* Fix NOP issue: Replace nops by "or at,at,zero". */
6081
6082static void
6083fix_loongson2f_nop (struct mips_cl_insn * ip)
6084{
df58fc94 6085 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
6086 if (strcmp (ip->insn_mo->name, "nop") == 0)
6087 ip->insn_opcode = LOONGSON2F_NOP_INSN;
6088}
6089
6090/* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
6091 jr target pc &= 'hffff_ffff_cfff_ffff. */
6092
6093static void
6094fix_loongson2f_jump (struct mips_cl_insn * ip)
6095{
df58fc94 6096 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
6097 if (strcmp (ip->insn_mo->name, "j") == 0
6098 || strcmp (ip->insn_mo->name, "jr") == 0
6099 || strcmp (ip->insn_mo->name, "jalr") == 0)
6100 {
6101 int sreg;
6102 expressionS ep;
6103
6104 if (! mips_opts.at)
6105 return;
6106
df58fc94 6107 sreg = EXTRACT_OPERAND (0, RS, *ip);
c67a084a
NC
6108 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
6109 return;
6110
6111 ep.X_op = O_constant;
6112 ep.X_add_number = 0xcfff0000;
6113 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
6114 ep.X_add_number = 0xffff;
6115 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
6116 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
6117 }
6118}
6119
6120static void
6121fix_loongson2f (struct mips_cl_insn * ip)
6122{
6123 if (mips_fix_loongson2f_nop)
6124 fix_loongson2f_nop (ip);
6125
6126 if (mips_fix_loongson2f_jump)
6127 fix_loongson2f_jump (ip);
6128}
6129
a4e06468
RS
6130/* IP is a branch that has a delay slot, and we need to fill it
6131 automatically. Return true if we can do that by swapping IP
e407c74b
NC
6132 with the previous instruction.
6133 ADDRESS_EXPR is an operand of the instruction to be used with
6134 RELOC_TYPE. */
a4e06468
RS
6135
6136static bfd_boolean
e407c74b 6137can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 6138 bfd_reloc_code_real_type *reloc_type)
a4e06468 6139{
2b0c8b40 6140 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
a4e06468 6141 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
9d5de888 6142 unsigned int fpr_read, prev_fpr_write;
a4e06468
RS
6143
6144 /* -O2 and above is required for this optimization. */
6145 if (mips_optimize < 2)
6146 return FALSE;
6147
6148 /* If we have seen .set volatile or .set nomove, don't optimize. */
6149 if (mips_opts.nomove)
6150 return FALSE;
6151
6152 /* We can't swap if the previous instruction's position is fixed. */
6153 if (history[0].fixed_p)
6154 return FALSE;
6155
6156 /* If the previous previous insn was in a .set noreorder, we can't
6157 swap. Actually, the MIPS assembler will swap in this situation.
6158 However, gcc configured -with-gnu-as will generate code like
6159
6160 .set noreorder
6161 lw $4,XXX
6162 .set reorder
6163 INSN
6164 bne $4,$0,foo
6165
6166 in which we can not swap the bne and INSN. If gcc is not configured
6167 -with-gnu-as, it does not output the .set pseudo-ops. */
6168 if (history[1].noreorder_p)
6169 return FALSE;
6170
87333bb7
MR
6171 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
6172 This means that the previous instruction was a 4-byte one anyhow. */
a4e06468
RS
6173 if (mips_opts.mips16 && history[0].fixp[0])
6174 return FALSE;
6175
6176 /* If the branch is itself the target of a branch, we can not swap.
6177 We cheat on this; all we check for is whether there is a label on
6178 this instruction. If there are any branches to anything other than
6179 a label, users must use .set noreorder. */
6180 if (seg_info (now_seg)->label_list)
6181 return FALSE;
6182
6183 /* If the previous instruction is in a variant frag other than this
2309ddf2 6184 branch's one, we cannot do the swap. This does not apply to
9301f9c3
MR
6185 MIPS16 code, which uses variant frags for different purposes. */
6186 if (!mips_opts.mips16
a4e06468
RS
6187 && history[0].frag
6188 && history[0].frag->fr_type == rs_machine_dependent)
6189 return FALSE;
6190
bcd530a7
RS
6191 /* We do not swap with instructions that cannot architecturally
6192 be placed in a branch delay slot, such as SYNC or ERET. We
6193 also refrain from swapping with a trap instruction, since it
6194 complicates trap handlers to have the trap instruction be in
6195 a delay slot. */
a4e06468 6196 prev_pinfo = history[0].insn_mo->pinfo;
bcd530a7 6197 if (prev_pinfo & INSN_NO_DELAY_SLOT)
a4e06468
RS
6198 return FALSE;
6199
6200 /* Check for conflicts between the branch and the instructions
6201 before the candidate delay slot. */
6202 if (nops_for_insn (0, history + 1, ip) > 0)
6203 return FALSE;
6204
6205 /* Check for conflicts between the swapped sequence and the
6206 target of the branch. */
6207 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
6208 return FALSE;
6209
6210 /* If the branch reads a register that the previous
6211 instruction sets, we can not swap. */
6212 gpr_read = gpr_read_mask (ip);
6213 prev_gpr_write = gpr_write_mask (&history[0]);
6214 if (gpr_read & prev_gpr_write)
6215 return FALSE;
6216
9d5de888
CF
6217 fpr_read = fpr_read_mask (ip);
6218 prev_fpr_write = fpr_write_mask (&history[0]);
6219 if (fpr_read & prev_fpr_write)
6220 return FALSE;
6221
a4e06468
RS
6222 /* If the branch writes a register that the previous
6223 instruction sets, we can not swap. */
6224 gpr_write = gpr_write_mask (ip);
6225 if (gpr_write & prev_gpr_write)
6226 return FALSE;
6227
6228 /* If the branch writes a register that the previous
6229 instruction reads, we can not swap. */
6230 prev_gpr_read = gpr_read_mask (&history[0]);
6231 if (gpr_write & prev_gpr_read)
6232 return FALSE;
6233
6234 /* If one instruction sets a condition code and the
6235 other one uses a condition code, we can not swap. */
6236 pinfo = ip->insn_mo->pinfo;
6237 if ((pinfo & INSN_READ_COND_CODE)
6238 && (prev_pinfo & INSN_WRITE_COND_CODE))
6239 return FALSE;
6240 if ((pinfo & INSN_WRITE_COND_CODE)
6241 && (prev_pinfo & INSN_READ_COND_CODE))
6242 return FALSE;
6243
6244 /* If the previous instruction uses the PC, we can not swap. */
2b0c8b40 6245 prev_pinfo2 = history[0].insn_mo->pinfo2;
26545944 6246 if (prev_pinfo2 & INSN2_READ_PC)
2b0c8b40 6247 return FALSE;
a4e06468 6248
df58fc94
RS
6249 /* If the previous instruction has an incorrect size for a fixed
6250 branch delay slot in microMIPS mode, we cannot swap. */
2309ddf2
MR
6251 pinfo2 = ip->insn_mo->pinfo2;
6252 if (mips_opts.micromips
6253 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
6254 && insn_length (history) != 2)
6255 return FALSE;
6256 if (mips_opts.micromips
6257 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
6258 && insn_length (history) != 4)
6259 return FALSE;
6260
e407c74b
NC
6261 /* On R5900 short loops need to be fixed by inserting a nop in
6262 the branch delay slots.
6263 A short loop can be terminated too early. */
6264 if (mips_opts.arch == CPU_R5900
6265 /* Check if instruction has a parameter, ignore "j $31". */
6266 && (address_expr != NULL)
6267 /* Parameter must be 16 bit. */
6268 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
6269 /* Branch to same segment. */
6270 && (S_GET_SEGMENT(address_expr->X_add_symbol) == now_seg)
6271 /* Branch to same code fragment. */
6272 && (symbol_get_frag(address_expr->X_add_symbol) == frag_now)
6273 /* Can only calculate branch offset if value is known. */
6274 && symbol_constant_p(address_expr->X_add_symbol)
6275 /* Check if branch is really conditional. */
6276 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
6277 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
6278 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
6279 {
6280 int distance;
6281 /* Check if loop is shorter than 6 instructions including
6282 branch and delay slot. */
6283 distance = frag_now_fix() - S_GET_VALUE(address_expr->X_add_symbol);
6284 if (distance <= 20)
6285 {
6286 int i;
6287 int rv;
6288
6289 rv = FALSE;
6290 /* When the loop includes branches or jumps,
6291 it is not a short loop. */
6292 for (i = 0; i < (distance / 4); i++)
6293 {
6294 if ((history[i].cleared_p)
6295 || delayed_branch_p(&history[i]))
6296 {
6297 rv = TRUE;
6298 break;
6299 }
6300 }
6301 if (rv == FALSE)
6302 {
6303 /* Insert nop after branch to fix short loop. */
6304 return FALSE;
6305 }
6306 }
6307 }
6308
a4e06468
RS
6309 return TRUE;
6310}
6311
e407c74b
NC
6312/* Decide how we should add IP to the instruction stream.
6313 ADDRESS_EXPR is an operand of the instruction to be used with
6314 RELOC_TYPE. */
a4e06468
RS
6315
6316static enum append_method
e407c74b 6317get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 6318 bfd_reloc_code_real_type *reloc_type)
a4e06468 6319{
a4e06468
RS
6320 /* The relaxed version of a macro sequence must be inherently
6321 hazard-free. */
6322 if (mips_relax.sequence == 2)
6323 return APPEND_ADD;
6324
6325 /* We must not dabble with instructions in a ".set norerorder" block. */
6326 if (mips_opts.noreorder)
6327 return APPEND_ADD;
6328
6329 /* Otherwise, it's our responsibility to fill branch delay slots. */
11625dd8 6330 if (delayed_branch_p (ip))
a4e06468 6331 {
e407c74b
NC
6332 if (!branch_likely_p (ip)
6333 && can_swap_branch_p (ip, address_expr, reloc_type))
a4e06468
RS
6334 return APPEND_SWAP;
6335
6336 if (mips_opts.mips16
6337 && ISA_SUPPORTS_MIPS16E
fc76e730 6338 && gpr_read_mask (ip) != 0)
a4e06468
RS
6339 return APPEND_ADD_COMPACT;
6340
6341 return APPEND_ADD_WITH_NOP;
6342 }
6343
a4e06468
RS
6344 return APPEND_ADD;
6345}
6346
ceb94aa5
RS
6347/* IP is a MIPS16 instruction whose opcode we have just changed.
6348 Point IP->insn_mo to the new opcode's definition. */
6349
6350static void
6351find_altered_mips16_opcode (struct mips_cl_insn *ip)
6352{
6353 const struct mips_opcode *mo, *end;
6354
6355 end = &mips16_opcodes[bfd_mips16_num_opcodes];
6356 for (mo = ip->insn_mo; mo < end; mo++)
6357 if ((ip->insn_opcode & mo->mask) == mo->match)
6358 {
6359 ip->insn_mo = mo;
6360 return;
6361 }
6362 abort ();
6363}
6364
df58fc94
RS
6365/* For microMIPS macros, we need to generate a local number label
6366 as the target of branches. */
6367#define MICROMIPS_LABEL_CHAR '\037'
6368static unsigned long micromips_target_label;
6369static char micromips_target_name[32];
6370
6371static char *
6372micromips_label_name (void)
6373{
6374 char *p = micromips_target_name;
6375 char symbol_name_temporary[24];
6376 unsigned long l;
6377 int i;
6378
6379 if (*p)
6380 return p;
6381
6382 i = 0;
6383 l = micromips_target_label;
6384#ifdef LOCAL_LABEL_PREFIX
6385 *p++ = LOCAL_LABEL_PREFIX;
6386#endif
6387 *p++ = 'L';
6388 *p++ = MICROMIPS_LABEL_CHAR;
6389 do
6390 {
6391 symbol_name_temporary[i++] = l % 10 + '0';
6392 l /= 10;
6393 }
6394 while (l != 0);
6395 while (i > 0)
6396 *p++ = symbol_name_temporary[--i];
6397 *p = '\0';
6398
6399 return micromips_target_name;
6400}
6401
6402static void
6403micromips_label_expr (expressionS *label_expr)
6404{
6405 label_expr->X_op = O_symbol;
6406 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
6407 label_expr->X_add_number = 0;
6408}
6409
6410static void
6411micromips_label_inc (void)
6412{
6413 micromips_target_label++;
6414 *micromips_target_name = '\0';
6415}
6416
6417static void
6418micromips_add_label (void)
6419{
6420 symbolS *s;
6421
6422 s = colon (micromips_label_name ());
6423 micromips_label_inc ();
f3ded42a 6424 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
df58fc94
RS
6425}
6426
6427/* If assembling microMIPS code, then return the microMIPS reloc
6428 corresponding to the requested one if any. Otherwise return
6429 the reloc unchanged. */
6430
6431static bfd_reloc_code_real_type
6432micromips_map_reloc (bfd_reloc_code_real_type reloc)
6433{
6434 static const bfd_reloc_code_real_type relocs[][2] =
6435 {
6436 /* Keep sorted incrementally by the left-hand key. */
6437 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
6438 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
6439 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
6440 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
6441 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
6442 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
6443 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
6444 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
6445 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
6446 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
6447 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
6448 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
6449 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
6450 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
6451 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
6452 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
6453 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
6454 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
6455 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
6456 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
6457 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
6458 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
6459 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
6460 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
6461 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
6462 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
6463 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
6464 };
6465 bfd_reloc_code_real_type r;
6466 size_t i;
6467
6468 if (!mips_opts.micromips)
6469 return reloc;
6470 for (i = 0; i < ARRAY_SIZE (relocs); i++)
6471 {
6472 r = relocs[i][0];
6473 if (r > reloc)
6474 return reloc;
6475 if (r == reloc)
6476 return relocs[i][1];
6477 }
6478 return reloc;
6479}
6480
b886a2ab
RS
6481/* Try to resolve relocation RELOC against constant OPERAND at assembly time.
6482 Return true on success, storing the resolved value in RESULT. */
6483
6484static bfd_boolean
6485calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
6486 offsetT *result)
6487{
6488 switch (reloc)
6489 {
6490 case BFD_RELOC_MIPS_HIGHEST:
6491 case BFD_RELOC_MICROMIPS_HIGHEST:
6492 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
6493 return TRUE;
6494
6495 case BFD_RELOC_MIPS_HIGHER:
6496 case BFD_RELOC_MICROMIPS_HIGHER:
6497 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
6498 return TRUE;
6499
6500 case BFD_RELOC_HI16_S:
6501 case BFD_RELOC_MICROMIPS_HI16_S:
6502 case BFD_RELOC_MIPS16_HI16_S:
6503 *result = ((operand + 0x8000) >> 16) & 0xffff;
6504 return TRUE;
6505
6506 case BFD_RELOC_HI16:
6507 case BFD_RELOC_MICROMIPS_HI16:
6508 case BFD_RELOC_MIPS16_HI16:
6509 *result = (operand >> 16) & 0xffff;
6510 return TRUE;
6511
6512 case BFD_RELOC_LO16:
6513 case BFD_RELOC_MICROMIPS_LO16:
6514 case BFD_RELOC_MIPS16_LO16:
6515 *result = operand & 0xffff;
6516 return TRUE;
6517
6518 case BFD_RELOC_UNUSED:
6519 *result = operand;
6520 return TRUE;
6521
6522 default:
6523 return FALSE;
6524 }
6525}
6526
71400594
RS
6527/* Output an instruction. IP is the instruction information.
6528 ADDRESS_EXPR is an operand of the instruction to be used with
df58fc94
RS
6529 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
6530 a macro expansion. */
71400594
RS
6531
6532static void
6533append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
df58fc94 6534 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
71400594 6535{
14fe068b 6536 unsigned long prev_pinfo2, pinfo;
71400594 6537 bfd_boolean relaxed_branch = FALSE;
a4e06468 6538 enum append_method method;
2309ddf2 6539 bfd_boolean relax32;
2b0c8b40 6540 int branch_disp;
71400594 6541
2309ddf2 6542 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
c67a084a
NC
6543 fix_loongson2f (ip);
6544
738f4d98 6545 file_ase_mips16 |= mips_opts.mips16;
df58fc94 6546 file_ase_micromips |= mips_opts.micromips;
738f4d98 6547
df58fc94 6548 prev_pinfo2 = history[0].insn_mo->pinfo2;
71400594 6549 pinfo = ip->insn_mo->pinfo;
df58fc94
RS
6550
6551 if (mips_opts.micromips
6552 && !expansionp
6553 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
6554 && micromips_insn_length (ip->insn_mo) != 2)
6555 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
6556 && micromips_insn_length (ip->insn_mo) != 4)))
1661c76c 6557 as_warn (_("wrong size instruction in a %u-bit branch delay slot"),
df58fc94 6558 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
71400594 6559
15be625d
CM
6560 if (address_expr == NULL)
6561 ip->complete_p = 1;
b886a2ab
RS
6562 else if (reloc_type[0] <= BFD_RELOC_UNUSED
6563 && reloc_type[1] == BFD_RELOC_UNUSED
6564 && reloc_type[2] == BFD_RELOC_UNUSED
15be625d
CM
6565 && address_expr->X_op == O_constant)
6566 {
15be625d
CM
6567 switch (*reloc_type)
6568 {
15be625d 6569 case BFD_RELOC_MIPS_JMP:
df58fc94
RS
6570 {
6571 int shift;
6572
6573 shift = mips_opts.micromips ? 1 : 2;
6574 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
6575 as_bad (_("jump to misaligned address (0x%lx)"),
6576 (unsigned long) address_expr->X_add_number);
6577 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
6578 & 0x3ffffff);
335574df 6579 ip->complete_p = 1;
df58fc94 6580 }
15be625d
CM
6581 break;
6582
6583 case BFD_RELOC_MIPS16_JMP:
6584 if ((address_expr->X_add_number & 3) != 0)
6585 as_bad (_("jump to misaligned address (0x%lx)"),
6586 (unsigned long) address_expr->X_add_number);
6587 ip->insn_opcode |=
6588 (((address_expr->X_add_number & 0x7c0000) << 3)
6589 | ((address_expr->X_add_number & 0xf800000) >> 7)
6590 | ((address_expr->X_add_number & 0x3fffc) >> 2));
335574df 6591 ip->complete_p = 1;
15be625d
CM
6592 break;
6593
6594 case BFD_RELOC_16_PCREL_S2:
df58fc94
RS
6595 {
6596 int shift;
6597
6598 shift = mips_opts.micromips ? 1 : 2;
6599 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
6600 as_bad (_("branch to misaligned address (0x%lx)"),
6601 (unsigned long) address_expr->X_add_number);
6602 if (!mips_relax_branch)
6603 {
6604 if ((address_expr->X_add_number + (1 << (shift + 15)))
6605 & ~((1 << (shift + 16)) - 1))
6606 as_bad (_("branch address range overflow (0x%lx)"),
6607 (unsigned long) address_expr->X_add_number);
6608 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
6609 & 0xffff);
6610 }
df58fc94 6611 }
15be625d
CM
6612 break;
6613
6614 default:
b886a2ab
RS
6615 {
6616 offsetT value;
6617
6618 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
6619 &value))
6620 {
6621 ip->insn_opcode |= value & 0xffff;
6622 ip->complete_p = 1;
6623 }
6624 }
6625 break;
6626 }
15be625d
CM
6627 }
6628
71400594
RS
6629 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
6630 {
6631 /* There are a lot of optimizations we could do that we don't.
6632 In particular, we do not, in general, reorder instructions.
6633 If you use gcc with optimization, it will reorder
6634 instructions and generally do much more optimization then we
6635 do here; repeating all that work in the assembler would only
6636 benefit hand written assembly code, and does not seem worth
6637 it. */
6638 int nops = (mips_optimize == 0
932d1a1b
RS
6639 ? nops_for_insn (0, history, NULL)
6640 : nops_for_insn_or_target (0, history, ip));
71400594 6641 if (nops > 0)
252b5132
RH
6642 {
6643 fragS *old_frag;
6644 unsigned long old_frag_offset;
6645 int i;
252b5132
RH
6646
6647 old_frag = frag_now;
6648 old_frag_offset = frag_now_fix ();
6649
6650 for (i = 0; i < nops; i++)
14fe068b
RS
6651 add_fixed_insn (NOP_INSN);
6652 insert_into_history (0, nops, NOP_INSN);
252b5132
RH
6653
6654 if (listing)
6655 {
6656 listing_prev_line ();
6657 /* We may be at the start of a variant frag. In case we
6658 are, make sure there is enough space for the frag
6659 after the frags created by listing_prev_line. The
6660 argument to frag_grow here must be at least as large
6661 as the argument to all other calls to frag_grow in
6662 this file. We don't have to worry about being in the
6663 middle of a variant frag, because the variants insert
6664 all needed nop instructions themselves. */
6665 frag_grow (40);
6666 }
6667
462427c4 6668 mips_move_text_labels ();
252b5132
RH
6669
6670#ifndef NO_ECOFF_DEBUGGING
6671 if (ECOFF_DEBUGGING)
6672 ecoff_fix_loc (old_frag, old_frag_offset);
6673#endif
6674 }
71400594
RS
6675 }
6676 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
6677 {
932d1a1b
RS
6678 int nops;
6679
6680 /* Work out how many nops in prev_nop_frag are needed by IP,
6681 ignoring hazards generated by the first prev_nop_frag_since
6682 instructions. */
6683 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
9c2799c2 6684 gas_assert (nops <= prev_nop_frag_holds);
252b5132 6685
71400594
RS
6686 /* Enforce NOPS as a minimum. */
6687 if (nops > prev_nop_frag_required)
6688 prev_nop_frag_required = nops;
252b5132 6689
71400594
RS
6690 if (prev_nop_frag_holds == prev_nop_frag_required)
6691 {
6692 /* Settle for the current number of nops. Update the history
6693 accordingly (for the benefit of any future .set reorder code). */
6694 prev_nop_frag = NULL;
6695 insert_into_history (prev_nop_frag_since,
6696 prev_nop_frag_holds, NOP_INSN);
6697 }
6698 else
6699 {
6700 /* Allow this instruction to replace one of the nops that was
6701 tentatively added to prev_nop_frag. */
df58fc94 6702 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
71400594
RS
6703 prev_nop_frag_holds--;
6704 prev_nop_frag_since++;
252b5132
RH
6705 }
6706 }
6707
e407c74b 6708 method = get_append_method (ip, address_expr, reloc_type);
2b0c8b40 6709 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
a4e06468 6710
e410add4
RS
6711 dwarf2_emit_insn (0);
6712 /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
6713 so "move" the instruction address accordingly.
6714
6715 Also, it doesn't seem appropriate for the assembler to reorder .loc
6716 entries. If this instruction is a branch that we are going to swap
6717 with the previous instruction, the two instructions should be
6718 treated as a unit, and the debug information for both instructions
6719 should refer to the start of the branch sequence. Using the
6720 current position is certainly wrong when swapping a 32-bit branch
6721 and a 16-bit delay slot, since the current position would then be
6722 in the middle of a branch. */
6723 dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
58e2ea4d 6724
df58fc94
RS
6725 relax32 = (mips_relax_branch
6726 /* Don't try branch relaxation within .set nomacro, or within
6727 .set noat if we use $at for PIC computations. If it turns
6728 out that the branch was out-of-range, we'll get an error. */
6729 && !mips_opts.warn_about_macros
6730 && (mips_opts.at || mips_pic == NO_PIC)
3bf0dbfb
MR
6731 /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
6732 as they have no complementing branches. */
6733 && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
df58fc94
RS
6734
6735 if (!HAVE_CODE_COMPRESSION
6736 && address_expr
6737 && relax32
0b25d3e6 6738 && *reloc_type == BFD_RELOC_16_PCREL_S2
11625dd8 6739 && delayed_branch_p (ip))
4a6a3df4 6740 {
895921c9 6741 relaxed_branch = TRUE;
1e915849
RS
6742 add_relaxed_insn (ip, (relaxed_branch_length
6743 (NULL, NULL,
11625dd8
RS
6744 uncond_branch_p (ip) ? -1
6745 : branch_likely_p (ip) ? 1
1e915849
RS
6746 : 0)), 4,
6747 RELAX_BRANCH_ENCODE
66b3e8da 6748 (AT,
11625dd8
RS
6749 uncond_branch_p (ip),
6750 branch_likely_p (ip),
1e915849
RS
6751 pinfo & INSN_WRITE_GPR_31,
6752 0),
6753 address_expr->X_add_symbol,
6754 address_expr->X_add_number);
4a6a3df4
AO
6755 *reloc_type = BFD_RELOC_UNUSED;
6756 }
df58fc94
RS
6757 else if (mips_opts.micromips
6758 && address_expr
6759 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
6760 || *reloc_type > BFD_RELOC_UNUSED)
40209cad
MR
6761 && (delayed_branch_p (ip) || compact_branch_p (ip))
6762 /* Don't try branch relaxation when users specify
6763 16-bit/32-bit instructions. */
6764 && !forced_insn_length)
df58fc94
RS
6765 {
6766 bfd_boolean relax16 = *reloc_type > BFD_RELOC_UNUSED;
6767 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
11625dd8
RS
6768 int uncond = uncond_branch_p (ip) ? -1 : 0;
6769 int compact = compact_branch_p (ip);
df58fc94
RS
6770 int al = pinfo & INSN_WRITE_GPR_31;
6771 int length32;
6772
6773 gas_assert (address_expr != NULL);
6774 gas_assert (!mips_relax.sequence);
6775
2b0c8b40 6776 relaxed_branch = TRUE;
df58fc94
RS
6777 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
6778 add_relaxed_insn (ip, relax32 ? length32 : 4, relax16 ? 2 : 4,
40209cad
MR
6779 RELAX_MICROMIPS_ENCODE (type, AT, uncond, compact, al,
6780 relax32, 0, 0),
df58fc94
RS
6781 address_expr->X_add_symbol,
6782 address_expr->X_add_number);
6783 *reloc_type = BFD_RELOC_UNUSED;
6784 }
6785 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
6786 {
6787 /* We need to set up a variant frag. */
df58fc94 6788 gas_assert (address_expr != NULL);
1e915849
RS
6789 add_relaxed_insn (ip, 4, 0,
6790 RELAX_MIPS16_ENCODE
6791 (*reloc_type - BFD_RELOC_UNUSED,
df58fc94 6792 forced_insn_length == 2, forced_insn_length == 4,
11625dd8 6793 delayed_branch_p (&history[0]),
1e915849
RS
6794 history[0].mips16_absolute_jump_p),
6795 make_expr_symbol (address_expr), 0);
252b5132 6796 }
5c04167a 6797 else if (mips_opts.mips16 && insn_length (ip) == 2)
9497f5ac 6798 {
11625dd8 6799 if (!delayed_branch_p (ip))
b8ee1a6e
DU
6800 /* Make sure there is enough room to swap this instruction with
6801 a following jump instruction. */
6802 frag_grow (6);
1e915849 6803 add_fixed_insn (ip);
252b5132
RH
6804 }
6805 else
6806 {
6807 if (mips_opts.mips16
6808 && mips_opts.noreorder
11625dd8 6809 && delayed_branch_p (&history[0]))
252b5132
RH
6810 as_warn (_("extended instruction in delay slot"));
6811
4d7206a2
RS
6812 if (mips_relax.sequence)
6813 {
6814 /* If we've reached the end of this frag, turn it into a variant
6815 frag and record the information for the instructions we've
6816 written so far. */
6817 if (frag_room () < 4)
6818 relax_close_frag ();
df58fc94 6819 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4d7206a2
RS
6820 }
6821
584892a6 6822 if (mips_relax.sequence != 2)
df58fc94
RS
6823 {
6824 if (mips_macro_warning.first_insn_sizes[0] == 0)
6825 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
6826 mips_macro_warning.sizes[0] += insn_length (ip);
6827 mips_macro_warning.insns[0]++;
6828 }
584892a6 6829 if (mips_relax.sequence != 1)
df58fc94
RS
6830 {
6831 if (mips_macro_warning.first_insn_sizes[1] == 0)
6832 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
6833 mips_macro_warning.sizes[1] += insn_length (ip);
6834 mips_macro_warning.insns[1]++;
6835 }
584892a6 6836
1e915849
RS
6837 if (mips_opts.mips16)
6838 {
6839 ip->fixed_p = 1;
6840 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
6841 }
6842 add_fixed_insn (ip);
252b5132
RH
6843 }
6844
9fe77896 6845 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
252b5132 6846 {
df58fc94 6847 bfd_reloc_code_real_type final_type[3];
2309ddf2 6848 reloc_howto_type *howto0;
9fe77896
RS
6849 reloc_howto_type *howto;
6850 int i;
34ce925e 6851
df58fc94
RS
6852 /* Perform any necessary conversion to microMIPS relocations
6853 and find out how many relocations there actually are. */
6854 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
6855 final_type[i] = micromips_map_reloc (reloc_type[i]);
6856
9fe77896
RS
6857 /* In a compound relocation, it is the final (outermost)
6858 operator that determines the relocated field. */
2309ddf2 6859 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
e8044f35
RS
6860 if (!howto)
6861 abort ();
2309ddf2
MR
6862
6863 if (i > 1)
6864 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
9fe77896
RS
6865 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
6866 bfd_get_reloc_size (howto),
6867 address_expr,
2309ddf2
MR
6868 howto0 && howto0->pc_relative,
6869 final_type[0]);
9fe77896
RS
6870
6871 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
2309ddf2 6872 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
9fe77896
RS
6873 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
6874
6875 /* These relocations can have an addend that won't fit in
6876 4 octets for 64bit assembly. */
bad1aba3 6877 if (GPR_SIZE == 64
9fe77896
RS
6878 && ! howto->partial_inplace
6879 && (reloc_type[0] == BFD_RELOC_16
6880 || reloc_type[0] == BFD_RELOC_32
6881 || reloc_type[0] == BFD_RELOC_MIPS_JMP
6882 || reloc_type[0] == BFD_RELOC_GPREL16
6883 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
6884 || reloc_type[0] == BFD_RELOC_GPREL32
6885 || reloc_type[0] == BFD_RELOC_64
6886 || reloc_type[0] == BFD_RELOC_CTOR
6887 || reloc_type[0] == BFD_RELOC_MIPS_SUB
6888 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
6889 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
6890 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
6891 || reloc_type[0] == BFD_RELOC_MIPS_REL16
6892 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
6893 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
6894 || hi16_reloc_p (reloc_type[0])
6895 || lo16_reloc_p (reloc_type[0])))
6896 ip->fixp[0]->fx_no_overflow = 1;
6897
ddaf2c41
MR
6898 /* These relocations can have an addend that won't fit in 2 octets. */
6899 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
6900 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
6901 ip->fixp[0]->fx_no_overflow = 1;
6902
9fe77896
RS
6903 if (mips_relax.sequence)
6904 {
6905 if (mips_relax.first_fixup == 0)
6906 mips_relax.first_fixup = ip->fixp[0];
6907 }
6908 else if (reloc_needs_lo_p (*reloc_type))
6909 {
6910 struct mips_hi_fixup *hi_fixup;
6911
6912 /* Reuse the last entry if it already has a matching %lo. */
6913 hi_fixup = mips_hi_fixup_list;
6914 if (hi_fixup == 0
6915 || !fixup_has_matching_lo_p (hi_fixup->fixp))
4d7206a2 6916 {
9fe77896
RS
6917 hi_fixup = ((struct mips_hi_fixup *)
6918 xmalloc (sizeof (struct mips_hi_fixup)));
6919 hi_fixup->next = mips_hi_fixup_list;
6920 mips_hi_fixup_list = hi_fixup;
4d7206a2 6921 }
9fe77896
RS
6922 hi_fixup->fixp = ip->fixp[0];
6923 hi_fixup->seg = now_seg;
6924 }
252b5132 6925
9fe77896
RS
6926 /* Add fixups for the second and third relocations, if given.
6927 Note that the ABI allows the second relocation to be
6928 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
6929 moment we only use RSS_UNDEF, but we could add support
6930 for the others if it ever becomes necessary. */
6931 for (i = 1; i < 3; i++)
6932 if (reloc_type[i] != BFD_RELOC_UNUSED)
6933 {
6934 ip->fixp[i] = fix_new (ip->frag, ip->where,
6935 ip->fixp[0]->fx_size, NULL, 0,
df58fc94 6936 FALSE, final_type[i]);
f6688943 6937
9fe77896
RS
6938 /* Use fx_tcbit to mark compound relocs. */
6939 ip->fixp[0]->fx_tcbit = 1;
6940 ip->fixp[i]->fx_tcbit = 1;
6941 }
252b5132 6942 }
1e915849 6943 install_insn (ip);
252b5132
RH
6944
6945 /* Update the register mask information. */
4c260379
RS
6946 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
6947 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
252b5132 6948
a4e06468 6949 switch (method)
252b5132 6950 {
a4e06468
RS
6951 case APPEND_ADD:
6952 insert_into_history (0, 1, ip);
6953 break;
6954
6955 case APPEND_ADD_WITH_NOP:
14fe068b
RS
6956 {
6957 struct mips_cl_insn *nop;
6958
6959 insert_into_history (0, 1, ip);
6960 nop = get_delay_slot_nop (ip);
6961 add_fixed_insn (nop);
6962 insert_into_history (0, 1, nop);
6963 if (mips_relax.sequence)
6964 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
6965 }
a4e06468
RS
6966 break;
6967
6968 case APPEND_ADD_COMPACT:
6969 /* Convert MIPS16 jr/jalr into a "compact" jump. */
6970 gas_assert (mips_opts.mips16);
6971 ip->insn_opcode |= 0x0080;
6972 find_altered_mips16_opcode (ip);
6973 install_insn (ip);
6974 insert_into_history (0, 1, ip);
6975 break;
6976
6977 case APPEND_SWAP:
6978 {
6979 struct mips_cl_insn delay = history[0];
6980 if (mips_opts.mips16)
6981 {
6982 know (delay.frag == ip->frag);
6983 move_insn (ip, delay.frag, delay.where);
6984 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
6985 }
464ab0e5 6986 else if (relaxed_branch || delay.frag != ip->frag)
a4e06468
RS
6987 {
6988 /* Add the delay slot instruction to the end of the
6989 current frag and shrink the fixed part of the
6990 original frag. If the branch occupies the tail of
6991 the latter, move it backwards to cover the gap. */
2b0c8b40 6992 delay.frag->fr_fix -= branch_disp;
a4e06468 6993 if (delay.frag == ip->frag)
2b0c8b40 6994 move_insn (ip, ip->frag, ip->where - branch_disp);
a4e06468
RS
6995 add_fixed_insn (&delay);
6996 }
6997 else
6998 {
2b0c8b40
MR
6999 move_insn (&delay, ip->frag,
7000 ip->where - branch_disp + insn_length (ip));
a4e06468
RS
7001 move_insn (ip, history[0].frag, history[0].where);
7002 }
7003 history[0] = *ip;
7004 delay.fixed_p = 1;
7005 insert_into_history (0, 1, &delay);
7006 }
7007 break;
252b5132
RH
7008 }
7009
13408f1e 7010 /* If we have just completed an unconditional branch, clear the history. */
11625dd8
RS
7011 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
7012 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
e407c74b
NC
7013 {
7014 unsigned int i;
7015
79850f26 7016 mips_no_prev_insn ();
13408f1e 7017
e407c74b 7018 for (i = 0; i < ARRAY_SIZE (history); i++)
79850f26 7019 history[i].cleared_p = 1;
e407c74b
NC
7020 }
7021
df58fc94
RS
7022 /* We need to emit a label at the end of branch-likely macros. */
7023 if (emit_branch_likely_macro)
7024 {
7025 emit_branch_likely_macro = FALSE;
7026 micromips_add_label ();
7027 }
7028
252b5132
RH
7029 /* We just output an insn, so the next one doesn't have a label. */
7030 mips_clear_insn_labels ();
252b5132
RH
7031}
7032
e407c74b
NC
7033/* Forget that there was any previous instruction or label.
7034 When BRANCH is true, the branch history is also flushed. */
252b5132
RH
7035
7036static void
7d10b47d 7037mips_no_prev_insn (void)
252b5132 7038{
7d10b47d
RS
7039 prev_nop_frag = NULL;
7040 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
7041 mips_clear_insn_labels ();
7042}
7043
7d10b47d
RS
7044/* This function must be called before we emit something other than
7045 instructions. It is like mips_no_prev_insn except that it inserts
7046 any NOPS that might be needed by previous instructions. */
252b5132 7047
7d10b47d
RS
7048void
7049mips_emit_delays (void)
252b5132
RH
7050{
7051 if (! mips_opts.noreorder)
7052 {
932d1a1b 7053 int nops = nops_for_insn (0, history, NULL);
252b5132
RH
7054 if (nops > 0)
7055 {
7d10b47d
RS
7056 while (nops-- > 0)
7057 add_fixed_insn (NOP_INSN);
462427c4 7058 mips_move_text_labels ();
7d10b47d
RS
7059 }
7060 }
7061 mips_no_prev_insn ();
7062}
7063
7064/* Start a (possibly nested) noreorder block. */
7065
7066static void
7067start_noreorder (void)
7068{
7069 if (mips_opts.noreorder == 0)
7070 {
7071 unsigned int i;
7072 int nops;
7073
7074 /* None of the instructions before the .set noreorder can be moved. */
7075 for (i = 0; i < ARRAY_SIZE (history); i++)
7076 history[i].fixed_p = 1;
7077
7078 /* Insert any nops that might be needed between the .set noreorder
7079 block and the previous instructions. We will later remove any
7080 nops that turn out not to be needed. */
932d1a1b 7081 nops = nops_for_insn (0, history, NULL);
7d10b47d
RS
7082 if (nops > 0)
7083 {
7084 if (mips_optimize != 0)
252b5132
RH
7085 {
7086 /* Record the frag which holds the nop instructions, so
7087 that we can remove them if we don't need them. */
df58fc94 7088 frag_grow (nops * NOP_INSN_SIZE);
252b5132
RH
7089 prev_nop_frag = frag_now;
7090 prev_nop_frag_holds = nops;
7091 prev_nop_frag_required = 0;
7092 prev_nop_frag_since = 0;
7093 }
7094
7095 for (; nops > 0; --nops)
1e915849 7096 add_fixed_insn (NOP_INSN);
252b5132 7097
7d10b47d
RS
7098 /* Move on to a new frag, so that it is safe to simply
7099 decrease the size of prev_nop_frag. */
7100 frag_wane (frag_now);
7101 frag_new (0);
462427c4 7102 mips_move_text_labels ();
252b5132 7103 }
df58fc94 7104 mips_mark_labels ();
7d10b47d 7105 mips_clear_insn_labels ();
252b5132 7106 }
7d10b47d
RS
7107 mips_opts.noreorder++;
7108 mips_any_noreorder = 1;
7109}
252b5132 7110
7d10b47d 7111/* End a nested noreorder block. */
252b5132 7112
7d10b47d
RS
7113static void
7114end_noreorder (void)
7115{
7116 mips_opts.noreorder--;
7117 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
7118 {
7119 /* Commit to inserting prev_nop_frag_required nops and go back to
7120 handling nop insertion the .set reorder way. */
7121 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
df58fc94 7122 * NOP_INSN_SIZE);
7d10b47d
RS
7123 insert_into_history (prev_nop_frag_since,
7124 prev_nop_frag_required, NOP_INSN);
7125 prev_nop_frag = NULL;
7126 }
252b5132
RH
7127}
7128
97d87491
RS
7129/* Sign-extend 32-bit mode constants that have bit 31 set and all
7130 higher bits unset. */
7131
7132static void
7133normalize_constant_expr (expressionS *ex)
7134{
7135 if (ex->X_op == O_constant
7136 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7137 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7138 - 0x80000000);
7139}
7140
7141/* Sign-extend 32-bit mode address offsets that have bit 31 set and
7142 all higher bits unset. */
7143
7144static void
7145normalize_address_expr (expressionS *ex)
7146{
7147 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
7148 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
7149 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7150 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7151 - 0x80000000);
7152}
7153
7154/* Try to match TOKENS against OPCODE, storing the result in INSN.
7155 Return true if the match was successful.
7156
7157 OPCODE_EXTRA is a value that should be ORed into the opcode
7158 (used for VU0 channel suffixes, etc.). MORE_ALTS is true if
7159 there are more alternatives after OPCODE and SOFT_MATCH is
7160 as for mips_arg_info. */
7161
7162static bfd_boolean
7163match_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
7164 struct mips_operand_token *tokens, unsigned int opcode_extra,
60f20e8b 7165 bfd_boolean lax_match, bfd_boolean complete_p)
97d87491
RS
7166{
7167 const char *args;
7168 struct mips_arg_info arg;
7169 const struct mips_operand *operand;
7170 char c;
7171
7172 imm_expr.X_op = O_absent;
97d87491
RS
7173 offset_expr.X_op = O_absent;
7174 offset_reloc[0] = BFD_RELOC_UNUSED;
7175 offset_reloc[1] = BFD_RELOC_UNUSED;
7176 offset_reloc[2] = BFD_RELOC_UNUSED;
7177
7178 create_insn (insn, opcode);
60f20e8b
RS
7179 /* When no opcode suffix is specified, assume ".xyzw". */
7180 if ((opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0 && opcode_extra == 0)
7181 insn->insn_opcode |= 0xf << mips_vu0_channel_mask.lsb;
7182 else
7183 insn->insn_opcode |= opcode_extra;
97d87491
RS
7184 memset (&arg, 0, sizeof (arg));
7185 arg.insn = insn;
7186 arg.token = tokens;
7187 arg.argnum = 1;
7188 arg.last_regno = ILLEGAL_REG;
7189 arg.dest_regno = ILLEGAL_REG;
60f20e8b 7190 arg.lax_match = lax_match;
97d87491
RS
7191 for (args = opcode->args;; ++args)
7192 {
7193 if (arg.token->type == OT_END)
7194 {
7195 /* Handle unary instructions in which only one operand is given.
7196 The source is then the same as the destination. */
7197 if (arg.opnum == 1 && *args == ',')
7198 {
7199 operand = (mips_opts.micromips
7200 ? decode_micromips_operand (args + 1)
7201 : decode_mips_operand (args + 1));
7202 if (operand && mips_optional_operand_p (operand))
7203 {
7204 arg.token = tokens;
7205 arg.argnum = 1;
7206 continue;
7207 }
7208 }
7209
7210 /* Treat elided base registers as $0. */
7211 if (strcmp (args, "(b)") == 0)
7212 args += 3;
7213
7214 if (args[0] == '+')
7215 switch (args[1])
7216 {
7217 case 'K':
7218 case 'N':
7219 /* The register suffix is optional. */
7220 args += 2;
7221 break;
7222 }
7223
7224 /* Fail the match if there were too few operands. */
7225 if (*args)
7226 return FALSE;
7227
7228 /* Successful match. */
60f20e8b
RS
7229 if (!complete_p)
7230 return TRUE;
e3de51ce 7231 clear_insn_error ();
97d87491
RS
7232 if (arg.dest_regno == arg.last_regno
7233 && strncmp (insn->insn_mo->name, "jalr", 4) == 0)
7234 {
7235 if (arg.opnum == 2)
e3de51ce 7236 set_insn_error
1661c76c 7237 (0, _("source and destination must be different"));
97d87491 7238 else if (arg.last_regno == 31)
e3de51ce 7239 set_insn_error
1661c76c 7240 (0, _("a destination register must be supplied"));
97d87491 7241 }
173d3447
CF
7242 else if (arg.last_regno == 31
7243 && (strncmp (insn->insn_mo->name, "bltzal", 6) == 0
7244 || strncmp (insn->insn_mo->name, "bgezal", 6) == 0))
7245 set_insn_error (0, _("the source register must not be $31"));
97d87491
RS
7246 check_completed_insn (&arg);
7247 return TRUE;
7248 }
7249
7250 /* Fail the match if the line has too many operands. */
7251 if (*args == 0)
7252 return FALSE;
7253
7254 /* Handle characters that need to match exactly. */
7255 if (*args == '(' || *args == ')' || *args == ',')
7256 {
7257 if (match_char (&arg, *args))
7258 continue;
7259 return FALSE;
7260 }
7261 if (*args == '#')
7262 {
7263 ++args;
7264 if (arg.token->type == OT_DOUBLE_CHAR
7265 && arg.token->u.ch == *args)
7266 {
7267 ++arg.token;
7268 continue;
7269 }
7270 return FALSE;
7271 }
7272
7273 /* Handle special macro operands. Work out the properties of
7274 other operands. */
7275 arg.opnum += 1;
97d87491
RS
7276 switch (*args)
7277 {
7278 case '+':
7279 switch (args[1])
7280 {
97d87491
RS
7281 case 'i':
7282 *offset_reloc = BFD_RELOC_MIPS_JMP;
7283 break;
7284 }
7285 break;
7286
97d87491 7287 case 'I':
1a00e612
RS
7288 if (!match_const_int (&arg, &imm_expr.X_add_number))
7289 return FALSE;
7290 imm_expr.X_op = O_constant;
bad1aba3 7291 if (GPR_SIZE == 32)
97d87491
RS
7292 normalize_constant_expr (&imm_expr);
7293 continue;
7294
7295 case 'A':
7296 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
7297 {
7298 /* Assume that the offset has been elided and that what
7299 we saw was a base register. The match will fail later
7300 if that assumption turns out to be wrong. */
7301 offset_expr.X_op = O_constant;
7302 offset_expr.X_add_number = 0;
7303 }
97d87491 7304 else
1a00e612
RS
7305 {
7306 if (!match_expression (&arg, &offset_expr, offset_reloc))
7307 return FALSE;
7308 normalize_address_expr (&offset_expr);
7309 }
97d87491
RS
7310 continue;
7311
7312 case 'F':
7313 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
7314 8, TRUE))
1a00e612 7315 return FALSE;
97d87491
RS
7316 continue;
7317
7318 case 'L':
7319 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
7320 8, FALSE))
1a00e612 7321 return FALSE;
97d87491
RS
7322 continue;
7323
7324 case 'f':
7325 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
7326 4, TRUE))
1a00e612 7327 return FALSE;
97d87491
RS
7328 continue;
7329
7330 case 'l':
7331 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
7332 4, FALSE))
1a00e612 7333 return FALSE;
97d87491
RS
7334 continue;
7335
97d87491
RS
7336 case 'p':
7337 *offset_reloc = BFD_RELOC_16_PCREL_S2;
7338 break;
7339
7340 case 'a':
7341 *offset_reloc = BFD_RELOC_MIPS_JMP;
7342 break;
7343
7344 case 'm':
7345 gas_assert (mips_opts.micromips);
7346 c = args[1];
7347 switch (c)
7348 {
7349 case 'D':
7350 case 'E':
7351 if (!forced_insn_length)
7352 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
7353 else if (c == 'D')
7354 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
7355 else
7356 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
7357 break;
7358 }
7359 break;
7360 }
7361
7362 operand = (mips_opts.micromips
7363 ? decode_micromips_operand (args)
7364 : decode_mips_operand (args));
7365 if (!operand)
7366 abort ();
7367
7368 /* Skip prefixes. */
7369 if (*args == '+' || *args == 'm')
7370 args++;
7371
7372 if (mips_optional_operand_p (operand)
7373 && args[1] == ','
7374 && (arg.token[0].type != OT_REG
7375 || arg.token[1].type == OT_END))
7376 {
7377 /* Assume that the register has been elided and is the
7378 same as the first operand. */
7379 arg.token = tokens;
7380 arg.argnum = 1;
7381 }
7382
7383 if (!match_operand (&arg, operand))
7384 return FALSE;
7385 }
7386}
7387
7388/* Like match_insn, but for MIPS16. */
7389
7390static bfd_boolean
7391match_mips16_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
1a00e612 7392 struct mips_operand_token *tokens)
97d87491
RS
7393{
7394 const char *args;
7395 const struct mips_operand *operand;
7396 const struct mips_operand *ext_operand;
7397 struct mips_arg_info arg;
7398 int relax_char;
7399
7400 create_insn (insn, opcode);
7401 imm_expr.X_op = O_absent;
97d87491
RS
7402 offset_expr.X_op = O_absent;
7403 offset_reloc[0] = BFD_RELOC_UNUSED;
7404 offset_reloc[1] = BFD_RELOC_UNUSED;
7405 offset_reloc[2] = BFD_RELOC_UNUSED;
7406 relax_char = 0;
7407
7408 memset (&arg, 0, sizeof (arg));
7409 arg.insn = insn;
7410 arg.token = tokens;
7411 arg.argnum = 1;
7412 arg.last_regno = ILLEGAL_REG;
7413 arg.dest_regno = ILLEGAL_REG;
97d87491
RS
7414 relax_char = 0;
7415 for (args = opcode->args;; ++args)
7416 {
7417 int c;
7418
7419 if (arg.token->type == OT_END)
7420 {
7421 offsetT value;
7422
7423 /* Handle unary instructions in which only one operand is given.
7424 The source is then the same as the destination. */
7425 if (arg.opnum == 1 && *args == ',')
7426 {
7427 operand = decode_mips16_operand (args[1], FALSE);
7428 if (operand && mips_optional_operand_p (operand))
7429 {
7430 arg.token = tokens;
7431 arg.argnum = 1;
7432 continue;
7433 }
7434 }
7435
7436 /* Fail the match if there were too few operands. */
7437 if (*args)
7438 return FALSE;
7439
7440 /* Successful match. Stuff the immediate value in now, if
7441 we can. */
e3de51ce 7442 clear_insn_error ();
97d87491
RS
7443 if (opcode->pinfo == INSN_MACRO)
7444 {
7445 gas_assert (relax_char == 0 || relax_char == 'p');
7446 gas_assert (*offset_reloc == BFD_RELOC_UNUSED);
7447 }
7448 else if (relax_char
7449 && offset_expr.X_op == O_constant
7450 && calculate_reloc (*offset_reloc,
7451 offset_expr.X_add_number,
7452 &value))
7453 {
7454 mips16_immed (NULL, 0, relax_char, *offset_reloc, value,
7455 forced_insn_length, &insn->insn_opcode);
7456 offset_expr.X_op = O_absent;
7457 *offset_reloc = BFD_RELOC_UNUSED;
7458 }
7459 else if (relax_char && *offset_reloc != BFD_RELOC_UNUSED)
7460 {
7461 if (forced_insn_length == 2)
e3de51ce 7462 set_insn_error (0, _("invalid unextended operand value"));
97d87491
RS
7463 forced_insn_length = 4;
7464 insn->insn_opcode |= MIPS16_EXTEND;
7465 }
7466 else if (relax_char)
7467 *offset_reloc = (int) BFD_RELOC_UNUSED + relax_char;
7468
7469 check_completed_insn (&arg);
7470 return TRUE;
7471 }
7472
7473 /* Fail the match if the line has too many operands. */
7474 if (*args == 0)
7475 return FALSE;
7476
7477 /* Handle characters that need to match exactly. */
7478 if (*args == '(' || *args == ')' || *args == ',')
7479 {
7480 if (match_char (&arg, *args))
7481 continue;
7482 return FALSE;
7483 }
7484
7485 arg.opnum += 1;
7486 c = *args;
7487 switch (c)
7488 {
7489 case 'p':
7490 case 'q':
7491 case 'A':
7492 case 'B':
7493 case 'E':
7494 relax_char = c;
7495 break;
7496
7497 case 'I':
1a00e612
RS
7498 if (!match_const_int (&arg, &imm_expr.X_add_number))
7499 return FALSE;
7500 imm_expr.X_op = O_constant;
bad1aba3 7501 if (GPR_SIZE == 32)
97d87491
RS
7502 normalize_constant_expr (&imm_expr);
7503 continue;
7504
7505 case 'a':
7506 case 'i':
7507 *offset_reloc = BFD_RELOC_MIPS16_JMP;
7508 insn->insn_opcode <<= 16;
7509 break;
7510 }
7511
7512 operand = decode_mips16_operand (c, FALSE);
7513 if (!operand)
7514 abort ();
7515
7516 /* '6' is a special case. It is used for BREAK and SDBBP,
7517 whose operands are only meaningful to the software that decodes
7518 them. This means that there is no architectural reason why
7519 they cannot be prefixed by EXTEND, but in practice,
7520 exception handlers will only look at the instruction
7521 itself. We therefore allow '6' to be extended when
7522 disassembling but not when assembling. */
7523 if (operand->type != OP_PCREL && c != '6')
7524 {
7525 ext_operand = decode_mips16_operand (c, TRUE);
7526 if (operand != ext_operand)
7527 {
7528 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
7529 {
7530 offset_expr.X_op = O_constant;
7531 offset_expr.X_add_number = 0;
7532 relax_char = c;
7533 continue;
7534 }
7535
7536 /* We need the OT_INTEGER check because some MIPS16
7537 immediate variants are listed before the register ones. */
7538 if (arg.token->type != OT_INTEGER
7539 || !match_expression (&arg, &offset_expr, offset_reloc))
7540 return FALSE;
7541
7542 /* '8' is used for SLTI(U) and has traditionally not
7543 been allowed to take relocation operators. */
7544 if (offset_reloc[0] != BFD_RELOC_UNUSED
7545 && (ext_operand->size != 16 || c == '8'))
7546 return FALSE;
7547
7548 relax_char = c;
7549 continue;
7550 }
7551 }
7552
7553 if (mips_optional_operand_p (operand)
7554 && args[1] == ','
7555 && (arg.token[0].type != OT_REG
7556 || arg.token[1].type == OT_END))
7557 {
7558 /* Assume that the register has been elided and is the
7559 same as the first operand. */
7560 arg.token = tokens;
7561 arg.argnum = 1;
7562 }
7563
7564 if (!match_operand (&arg, operand))
7565 return FALSE;
7566 }
7567}
7568
60f20e8b
RS
7569/* Record that the current instruction is invalid for the current ISA. */
7570
7571static void
7572match_invalid_for_isa (void)
7573{
7574 set_insn_error_ss
1661c76c 7575 (0, _("opcode not supported on this processor: %s (%s)"),
60f20e8b
RS
7576 mips_cpu_info_from_arch (mips_opts.arch)->name,
7577 mips_cpu_info_from_isa (mips_opts.isa)->name);
7578}
7579
7580/* Try to match TOKENS against a series of opcode entries, starting at FIRST.
7581 Return true if a definite match or failure was found, storing any match
7582 in INSN. OPCODE_EXTRA is a value that should be ORed into the opcode
7583 (to handle things like VU0 suffixes). LAX_MATCH is true if we have already
7584 tried and failed to match under normal conditions and now want to try a
7585 more relaxed match. */
7586
7587static bfd_boolean
7588match_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
7589 const struct mips_opcode *past, struct mips_operand_token *tokens,
7590 int opcode_extra, bfd_boolean lax_match)
7591{
7592 const struct mips_opcode *opcode;
7593 const struct mips_opcode *invalid_delay_slot;
7594 bfd_boolean seen_valid_for_isa, seen_valid_for_size;
7595
7596 /* Search for a match, ignoring alternatives that don't satisfy the
7597 current ISA or forced_length. */
7598 invalid_delay_slot = 0;
7599 seen_valid_for_isa = FALSE;
7600 seen_valid_for_size = FALSE;
7601 opcode = first;
7602 do
7603 {
7604 gas_assert (strcmp (opcode->name, first->name) == 0);
7605 if (is_opcode_valid (opcode))
7606 {
7607 seen_valid_for_isa = TRUE;
7608 if (is_size_valid (opcode))
7609 {
7610 bfd_boolean delay_slot_ok;
7611
7612 seen_valid_for_size = TRUE;
7613 delay_slot_ok = is_delay_slot_valid (opcode);
7614 if (match_insn (insn, opcode, tokens, opcode_extra,
7615 lax_match, delay_slot_ok))
7616 {
7617 if (!delay_slot_ok)
7618 {
7619 if (!invalid_delay_slot)
7620 invalid_delay_slot = opcode;
7621 }
7622 else
7623 return TRUE;
7624 }
7625 }
7626 }
7627 ++opcode;
7628 }
7629 while (opcode < past && strcmp (opcode->name, first->name) == 0);
7630
7631 /* If the only matches we found had the wrong length for the delay slot,
7632 pick the first such match. We'll issue an appropriate warning later. */
7633 if (invalid_delay_slot)
7634 {
7635 if (match_insn (insn, invalid_delay_slot, tokens, opcode_extra,
7636 lax_match, TRUE))
7637 return TRUE;
7638 abort ();
7639 }
7640
7641 /* Handle the case where we didn't try to match an instruction because
7642 all the alternatives were incompatible with the current ISA. */
7643 if (!seen_valid_for_isa)
7644 {
7645 match_invalid_for_isa ();
7646 return TRUE;
7647 }
7648
7649 /* Handle the case where we didn't try to match an instruction because
7650 all the alternatives were of the wrong size. */
7651 if (!seen_valid_for_size)
7652 {
7653 if (mips_opts.insn32)
1661c76c 7654 set_insn_error (0, _("opcode not supported in the `insn32' mode"));
60f20e8b
RS
7655 else
7656 set_insn_error_i
1661c76c 7657 (0, _("unrecognized %d-bit version of microMIPS opcode"),
60f20e8b
RS
7658 8 * forced_insn_length);
7659 return TRUE;
7660 }
7661
7662 return FALSE;
7663}
7664
7665/* Like match_insns, but for MIPS16. */
7666
7667static bfd_boolean
7668match_mips16_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
7669 struct mips_operand_token *tokens)
7670{
7671 const struct mips_opcode *opcode;
7672 bfd_boolean seen_valid_for_isa;
7673
7674 /* Search for a match, ignoring alternatives that don't satisfy the
7675 current ISA. There are no separate entries for extended forms so
7676 we deal with forced_length later. */
7677 seen_valid_for_isa = FALSE;
7678 opcode = first;
7679 do
7680 {
7681 gas_assert (strcmp (opcode->name, first->name) == 0);
7682 if (is_opcode_valid_16 (opcode))
7683 {
7684 seen_valid_for_isa = TRUE;
7685 if (match_mips16_insn (insn, opcode, tokens))
7686 return TRUE;
7687 }
7688 ++opcode;
7689 }
7690 while (opcode < &mips16_opcodes[bfd_mips16_num_opcodes]
7691 && strcmp (opcode->name, first->name) == 0);
7692
7693 /* Handle the case where we didn't try to match an instruction because
7694 all the alternatives were incompatible with the current ISA. */
7695 if (!seen_valid_for_isa)
7696 {
7697 match_invalid_for_isa ();
7698 return TRUE;
7699 }
7700
7701 return FALSE;
7702}
7703
584892a6
RS
7704/* Set up global variables for the start of a new macro. */
7705
7706static void
7707macro_start (void)
7708{
7709 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
df58fc94
RS
7710 memset (&mips_macro_warning.first_insn_sizes, 0,
7711 sizeof (mips_macro_warning.first_insn_sizes));
7712 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
584892a6 7713 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
11625dd8 7714 && delayed_branch_p (&history[0]));
df58fc94
RS
7715 switch (history[0].insn_mo->pinfo2
7716 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
7717 {
7718 case INSN2_BRANCH_DELAY_32BIT:
7719 mips_macro_warning.delay_slot_length = 4;
7720 break;
7721 case INSN2_BRANCH_DELAY_16BIT:
7722 mips_macro_warning.delay_slot_length = 2;
7723 break;
7724 default:
7725 mips_macro_warning.delay_slot_length = 0;
7726 break;
7727 }
7728 mips_macro_warning.first_frag = NULL;
584892a6
RS
7729}
7730
df58fc94
RS
7731/* Given that a macro is longer than one instruction or of the wrong size,
7732 return the appropriate warning for it. Return null if no warning is
7733 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
7734 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
7735 and RELAX_NOMACRO. */
584892a6
RS
7736
7737static const char *
7738macro_warning (relax_substateT subtype)
7739{
7740 if (subtype & RELAX_DELAY_SLOT)
1661c76c 7741 return _("macro instruction expanded into multiple instructions"
584892a6
RS
7742 " in a branch delay slot");
7743 else if (subtype & RELAX_NOMACRO)
1661c76c 7744 return _("macro instruction expanded into multiple instructions");
df58fc94
RS
7745 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
7746 | RELAX_DELAY_SLOT_SIZE_SECOND))
7747 return ((subtype & RELAX_DELAY_SLOT_16BIT)
1661c76c 7748 ? _("macro instruction expanded into a wrong size instruction"
df58fc94 7749 " in a 16-bit branch delay slot")
1661c76c 7750 : _("macro instruction expanded into a wrong size instruction"
df58fc94 7751 " in a 32-bit branch delay slot"));
584892a6
RS
7752 else
7753 return 0;
7754}
7755
7756/* Finish up a macro. Emit warnings as appropriate. */
7757
7758static void
7759macro_end (void)
7760{
df58fc94
RS
7761 /* Relaxation warning flags. */
7762 relax_substateT subtype = 0;
7763
7764 /* Check delay slot size requirements. */
7765 if (mips_macro_warning.delay_slot_length == 2)
7766 subtype |= RELAX_DELAY_SLOT_16BIT;
7767 if (mips_macro_warning.delay_slot_length != 0)
584892a6 7768 {
df58fc94
RS
7769 if (mips_macro_warning.delay_slot_length
7770 != mips_macro_warning.first_insn_sizes[0])
7771 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
7772 if (mips_macro_warning.delay_slot_length
7773 != mips_macro_warning.first_insn_sizes[1])
7774 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
7775 }
584892a6 7776
df58fc94
RS
7777 /* Check instruction count requirements. */
7778 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
7779 {
7780 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
584892a6
RS
7781 subtype |= RELAX_SECOND_LONGER;
7782 if (mips_opts.warn_about_macros)
7783 subtype |= RELAX_NOMACRO;
7784 if (mips_macro_warning.delay_slot_p)
7785 subtype |= RELAX_DELAY_SLOT;
df58fc94 7786 }
584892a6 7787
df58fc94
RS
7788 /* If both alternatives fail to fill a delay slot correctly,
7789 emit the warning now. */
7790 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
7791 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
7792 {
7793 relax_substateT s;
7794 const char *msg;
7795
7796 s = subtype & (RELAX_DELAY_SLOT_16BIT
7797 | RELAX_DELAY_SLOT_SIZE_FIRST
7798 | RELAX_DELAY_SLOT_SIZE_SECOND);
7799 msg = macro_warning (s);
7800 if (msg != NULL)
7801 as_warn ("%s", msg);
7802 subtype &= ~s;
7803 }
7804
7805 /* If both implementations are longer than 1 instruction, then emit the
7806 warning now. */
7807 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
7808 {
7809 relax_substateT s;
7810 const char *msg;
7811
7812 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
7813 msg = macro_warning (s);
7814 if (msg != NULL)
7815 as_warn ("%s", msg);
7816 subtype &= ~s;
584892a6 7817 }
df58fc94
RS
7818
7819 /* If any flags still set, then one implementation might need a warning
7820 and the other either will need one of a different kind or none at all.
7821 Pass any remaining flags over to relaxation. */
7822 if (mips_macro_warning.first_frag != NULL)
7823 mips_macro_warning.first_frag->fr_subtype |= subtype;
584892a6
RS
7824}
7825
df58fc94
RS
7826/* Instruction operand formats used in macros that vary between
7827 standard MIPS and microMIPS code. */
7828
833794fc 7829static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
df58fc94
RS
7830static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
7831static const char * const jalr_fmt[2] = { "d,s", "t,s" };
7832static const char * const lui_fmt[2] = { "t,u", "s,u" };
7833static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
833794fc 7834static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
df58fc94
RS
7835static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
7836static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
7837
833794fc 7838#define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
df58fc94
RS
7839#define COP12_FMT (cop12_fmt[mips_opts.micromips])
7840#define JALR_FMT (jalr_fmt[mips_opts.micromips])
7841#define LUI_FMT (lui_fmt[mips_opts.micromips])
7842#define MEM12_FMT (mem12_fmt[mips_opts.micromips])
833794fc 7843#define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
df58fc94
RS
7844#define SHFT_FMT (shft_fmt[mips_opts.micromips])
7845#define TRAP_FMT (trap_fmt[mips_opts.micromips])
7846
6e1304d8
RS
7847/* Read a macro's relocation codes from *ARGS and store them in *R.
7848 The first argument in *ARGS will be either the code for a single
7849 relocation or -1 followed by the three codes that make up a
7850 composite relocation. */
7851
7852static void
7853macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
7854{
7855 int i, next;
7856
7857 next = va_arg (*args, int);
7858 if (next >= 0)
7859 r[0] = (bfd_reloc_code_real_type) next;
7860 else
f2ae14a1
RS
7861 {
7862 for (i = 0; i < 3; i++)
7863 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
7864 /* This function is only used for 16-bit relocation fields.
7865 To make the macro code simpler, treat an unrelocated value
7866 in the same way as BFD_RELOC_LO16. */
7867 if (r[0] == BFD_RELOC_UNUSED)
7868 r[0] = BFD_RELOC_LO16;
7869 }
6e1304d8
RS
7870}
7871
252b5132
RH
7872/* Build an instruction created by a macro expansion. This is passed
7873 a pointer to the count of instructions created so far, an
7874 expression, the name of the instruction to build, an operand format
7875 string, and corresponding arguments. */
7876
252b5132 7877static void
67c0d1eb 7878macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 7879{
df58fc94 7880 const struct mips_opcode *mo = NULL;
f6688943 7881 bfd_reloc_code_real_type r[3];
df58fc94 7882 const struct mips_opcode *amo;
e077a1c8 7883 const struct mips_operand *operand;
df58fc94
RS
7884 struct hash_control *hash;
7885 struct mips_cl_insn insn;
252b5132 7886 va_list args;
e077a1c8 7887 unsigned int uval;
252b5132 7888
252b5132 7889 va_start (args, fmt);
252b5132 7890
252b5132
RH
7891 if (mips_opts.mips16)
7892 {
03ea81db 7893 mips16_macro_build (ep, name, fmt, &args);
252b5132
RH
7894 va_end (args);
7895 return;
7896 }
7897
f6688943
TS
7898 r[0] = BFD_RELOC_UNUSED;
7899 r[1] = BFD_RELOC_UNUSED;
7900 r[2] = BFD_RELOC_UNUSED;
df58fc94
RS
7901 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
7902 amo = (struct mips_opcode *) hash_find (hash, name);
7903 gas_assert (amo);
7904 gas_assert (strcmp (name, amo->name) == 0);
1e915849 7905
df58fc94 7906 do
8b082fb1
TS
7907 {
7908 /* Search until we get a match for NAME. It is assumed here that
df58fc94
RS
7909 macros will never generate MDMX, MIPS-3D, or MT instructions.
7910 We try to match an instruction that fulfils the branch delay
7911 slot instruction length requirement (if any) of the previous
7912 instruction. While doing this we record the first instruction
7913 seen that matches all the other conditions and use it anyway
7914 if the requirement cannot be met; we will issue an appropriate
7915 warning later on. */
7916 if (strcmp (fmt, amo->args) == 0
7917 && amo->pinfo != INSN_MACRO
7918 && is_opcode_valid (amo)
7919 && is_size_valid (amo))
7920 {
7921 if (is_delay_slot_valid (amo))
7922 {
7923 mo = amo;
7924 break;
7925 }
7926 else if (!mo)
7927 mo = amo;
7928 }
8b082fb1 7929
df58fc94
RS
7930 ++amo;
7931 gas_assert (amo->name);
252b5132 7932 }
df58fc94 7933 while (strcmp (name, amo->name) == 0);
252b5132 7934
df58fc94 7935 gas_assert (mo);
1e915849 7936 create_insn (&insn, mo);
e077a1c8 7937 for (; *fmt; ++fmt)
252b5132 7938 {
e077a1c8 7939 switch (*fmt)
252b5132 7940 {
252b5132
RH
7941 case ',':
7942 case '(':
7943 case ')':
252b5132 7944 case 'z':
e077a1c8 7945 break;
252b5132
RH
7946
7947 case 'i':
7948 case 'j':
6e1304d8 7949 macro_read_relocs (&args, r);
9c2799c2 7950 gas_assert (*r == BFD_RELOC_GPREL16
e391c024
RS
7951 || *r == BFD_RELOC_MIPS_HIGHER
7952 || *r == BFD_RELOC_HI16_S
7953 || *r == BFD_RELOC_LO16
7954 || *r == BFD_RELOC_MIPS_GOT_OFST);
e077a1c8 7955 break;
e391c024
RS
7956
7957 case 'o':
7958 macro_read_relocs (&args, r);
e077a1c8 7959 break;
252b5132
RH
7960
7961 case 'u':
6e1304d8 7962 macro_read_relocs (&args, r);
9c2799c2 7963 gas_assert (ep != NULL
90ecf173
MR
7964 && (ep->X_op == O_constant
7965 || (ep->X_op == O_symbol
7966 && (*r == BFD_RELOC_MIPS_HIGHEST
7967 || *r == BFD_RELOC_HI16_S
7968 || *r == BFD_RELOC_HI16
7969 || *r == BFD_RELOC_GPREL16
7970 || *r == BFD_RELOC_MIPS_GOT_HI16
7971 || *r == BFD_RELOC_MIPS_CALL_HI16))));
e077a1c8 7972 break;
252b5132
RH
7973
7974 case 'p':
9c2799c2 7975 gas_assert (ep != NULL);
bad36eac 7976
252b5132
RH
7977 /*
7978 * This allows macro() to pass an immediate expression for
7979 * creating short branches without creating a symbol.
bad36eac
DJ
7980 *
7981 * We don't allow branch relaxation for these branches, as
7982 * they should only appear in ".set nomacro" anyway.
252b5132
RH
7983 */
7984 if (ep->X_op == O_constant)
7985 {
df58fc94
RS
7986 /* For microMIPS we always use relocations for branches.
7987 So we should not resolve immediate values. */
7988 gas_assert (!mips_opts.micromips);
7989
bad36eac
DJ
7990 if ((ep->X_add_number & 3) != 0)
7991 as_bad (_("branch to misaligned address (0x%lx)"),
7992 (unsigned long) ep->X_add_number);
7993 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
7994 as_bad (_("branch address range overflow (0x%lx)"),
7995 (unsigned long) ep->X_add_number);
252b5132
RH
7996 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
7997 ep = NULL;
7998 }
7999 else
0b25d3e6 8000 *r = BFD_RELOC_16_PCREL_S2;
e077a1c8 8001 break;
252b5132
RH
8002
8003 case 'a':
9c2799c2 8004 gas_assert (ep != NULL);
f6688943 8005 *r = BFD_RELOC_MIPS_JMP;
e077a1c8 8006 break;
d43b4baf 8007
252b5132 8008 default:
e077a1c8
RS
8009 operand = (mips_opts.micromips
8010 ? decode_micromips_operand (fmt)
8011 : decode_mips_operand (fmt));
8012 if (!operand)
8013 abort ();
8014
8015 uval = va_arg (args, int);
8016 if (operand->type == OP_CLO_CLZ_DEST)
8017 uval |= (uval << 5);
8018 insn_insert_operand (&insn, operand, uval);
8019
8020 if (*fmt == '+' || *fmt == 'm')
8021 ++fmt;
8022 break;
252b5132 8023 }
252b5132
RH
8024 }
8025 va_end (args);
9c2799c2 8026 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 8027
df58fc94 8028 append_insn (&insn, ep, r, TRUE);
252b5132
RH
8029}
8030
8031static void
67c0d1eb 8032mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
03ea81db 8033 va_list *args)
252b5132 8034{
1e915849 8035 struct mips_opcode *mo;
252b5132 8036 struct mips_cl_insn insn;
e077a1c8 8037 const struct mips_operand *operand;
f6688943
TS
8038 bfd_reloc_code_real_type r[3]
8039 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 8040
1e915849 8041 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
9c2799c2
NC
8042 gas_assert (mo);
8043 gas_assert (strcmp (name, mo->name) == 0);
252b5132 8044
1e915849 8045 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 8046 {
1e915849 8047 ++mo;
9c2799c2
NC
8048 gas_assert (mo->name);
8049 gas_assert (strcmp (name, mo->name) == 0);
252b5132
RH
8050 }
8051
1e915849 8052 create_insn (&insn, mo);
e077a1c8 8053 for (; *fmt; ++fmt)
252b5132
RH
8054 {
8055 int c;
8056
e077a1c8 8057 c = *fmt;
252b5132
RH
8058 switch (c)
8059 {
252b5132
RH
8060 case ',':
8061 case '(':
8062 case ')':
e077a1c8 8063 break;
252b5132
RH
8064
8065 case '0':
8066 case 'S':
8067 case 'P':
8068 case 'R':
e077a1c8 8069 break;
252b5132
RH
8070
8071 case '<':
8072 case '>':
8073 case '4':
8074 case '5':
8075 case 'H':
8076 case 'W':
8077 case 'D':
8078 case 'j':
8079 case '8':
8080 case 'V':
8081 case 'C':
8082 case 'U':
8083 case 'k':
8084 case 'K':
8085 case 'p':
8086 case 'q':
8087 {
b886a2ab
RS
8088 offsetT value;
8089
9c2799c2 8090 gas_assert (ep != NULL);
252b5132
RH
8091
8092 if (ep->X_op != O_constant)
874e8986 8093 *r = (int) BFD_RELOC_UNUSED + c;
b886a2ab 8094 else if (calculate_reloc (*r, ep->X_add_number, &value))
252b5132 8095 {
b886a2ab 8096 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
252b5132 8097 ep = NULL;
f6688943 8098 *r = BFD_RELOC_UNUSED;
252b5132
RH
8099 }
8100 }
e077a1c8 8101 break;
252b5132 8102
e077a1c8
RS
8103 default:
8104 operand = decode_mips16_operand (c, FALSE);
8105 if (!operand)
8106 abort ();
252b5132 8107
4a06e5a2 8108 insn_insert_operand (&insn, operand, va_arg (*args, int));
e077a1c8
RS
8109 break;
8110 }
252b5132
RH
8111 }
8112
9c2799c2 8113 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 8114
df58fc94 8115 append_insn (&insn, ep, r, TRUE);
252b5132
RH
8116}
8117
438c16b8
TS
8118/*
8119 * Generate a "jalr" instruction with a relocation hint to the called
8120 * function. This occurs in NewABI PIC code.
8121 */
8122static void
df58fc94 8123macro_build_jalr (expressionS *ep, int cprestore)
438c16b8 8124{
df58fc94
RS
8125 static const bfd_reloc_code_real_type jalr_relocs[2]
8126 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
8127 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
8128 const char *jalr;
685736be 8129 char *f = NULL;
b34976b6 8130
1180b5a4 8131 if (MIPS_JALR_HINT_P (ep))
f21f8242 8132 {
cc3d92a5 8133 frag_grow (8);
f21f8242
AO
8134 f = frag_more (0);
8135 }
2906b037 8136 if (mips_opts.micromips)
df58fc94 8137 {
833794fc
MR
8138 jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
8139 ? "jalr" : "jalrs");
e64af278 8140 if (MIPS_JALR_HINT_P (ep)
833794fc 8141 || mips_opts.insn32
e64af278 8142 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
8143 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
8144 else
8145 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
8146 }
2906b037
MR
8147 else
8148 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
1180b5a4 8149 if (MIPS_JALR_HINT_P (ep))
df58fc94 8150 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
438c16b8
TS
8151}
8152
252b5132
RH
8153/*
8154 * Generate a "lui" instruction.
8155 */
8156static void
67c0d1eb 8157macro_build_lui (expressionS *ep, int regnum)
252b5132 8158{
9c2799c2 8159 gas_assert (! mips_opts.mips16);
252b5132 8160
df58fc94 8161 if (ep->X_op != O_constant)
252b5132 8162 {
9c2799c2 8163 gas_assert (ep->X_op == O_symbol);
bbe506e8
TS
8164 /* _gp_disp is a special case, used from s_cpload.
8165 __gnu_local_gp is used if mips_no_shared. */
9c2799c2 8166 gas_assert (mips_pic == NO_PIC
78e1bb40 8167 || (! HAVE_NEWABI
aa6975fb
ILT
8168 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
8169 || (! mips_in_shared
bbe506e8
TS
8170 && strcmp (S_GET_NAME (ep->X_add_symbol),
8171 "__gnu_local_gp") == 0));
252b5132
RH
8172 }
8173
df58fc94 8174 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
252b5132
RH
8175}
8176
885add95
CD
8177/* Generate a sequence of instructions to do a load or store from a constant
8178 offset off of a base register (breg) into/from a target register (treg),
8179 using AT if necessary. */
8180static void
67c0d1eb
RS
8181macro_build_ldst_constoffset (expressionS *ep, const char *op,
8182 int treg, int breg, int dbl)
885add95 8183{
9c2799c2 8184 gas_assert (ep->X_op == O_constant);
885add95 8185
256ab948 8186 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
8187 if (!dbl)
8188 normalize_constant_expr (ep);
256ab948 8189
67c1ffbe 8190 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 8191 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
8192 as_warn (_("operand overflow"));
8193
8194 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
8195 {
8196 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 8197 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
8198 }
8199 else
8200 {
8201 /* 32-bit offset, need multiple instructions and AT, like:
8202 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
8203 addu $tempreg,$tempreg,$breg
8204 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
8205 to handle the complete offset. */
67c0d1eb
RS
8206 macro_build_lui (ep, AT);
8207 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
8208 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95 8209
741fe287 8210 if (!mips_opts.at)
1661c76c 8211 as_bad (_("macro used $at after \".set noat\""));
885add95
CD
8212 }
8213}
8214
252b5132
RH
8215/* set_at()
8216 * Generates code to set the $at register to true (one)
8217 * if reg is less than the immediate expression.
8218 */
8219static void
67c0d1eb 8220set_at (int reg, int unsignedp)
252b5132 8221{
b0e6f033 8222 if (imm_expr.X_add_number >= -0x8000
252b5132 8223 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
8224 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
8225 AT, reg, BFD_RELOC_LO16);
252b5132
RH
8226 else
8227 {
bad1aba3 8228 load_register (AT, &imm_expr, GPR_SIZE == 64);
67c0d1eb 8229 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
8230 }
8231}
8232
252b5132
RH
8233/* Count the leading zeroes by performing a binary chop. This is a
8234 bulky bit of source, but performance is a LOT better for the
8235 majority of values than a simple loop to count the bits:
8236 for (lcnt = 0; (lcnt < 32); lcnt++)
8237 if ((v) & (1 << (31 - lcnt)))
8238 break;
8239 However it is not code size friendly, and the gain will drop a bit
8240 on certain cached systems.
8241*/
8242#define COUNT_TOP_ZEROES(v) \
8243 (((v) & ~0xffff) == 0 \
8244 ? ((v) & ~0xff) == 0 \
8245 ? ((v) & ~0xf) == 0 \
8246 ? ((v) & ~0x3) == 0 \
8247 ? ((v) & ~0x1) == 0 \
8248 ? !(v) \
8249 ? 32 \
8250 : 31 \
8251 : 30 \
8252 : ((v) & ~0x7) == 0 \
8253 ? 29 \
8254 : 28 \
8255 : ((v) & ~0x3f) == 0 \
8256 ? ((v) & ~0x1f) == 0 \
8257 ? 27 \
8258 : 26 \
8259 : ((v) & ~0x7f) == 0 \
8260 ? 25 \
8261 : 24 \
8262 : ((v) & ~0xfff) == 0 \
8263 ? ((v) & ~0x3ff) == 0 \
8264 ? ((v) & ~0x1ff) == 0 \
8265 ? 23 \
8266 : 22 \
8267 : ((v) & ~0x7ff) == 0 \
8268 ? 21 \
8269 : 20 \
8270 : ((v) & ~0x3fff) == 0 \
8271 ? ((v) & ~0x1fff) == 0 \
8272 ? 19 \
8273 : 18 \
8274 : ((v) & ~0x7fff) == 0 \
8275 ? 17 \
8276 : 16 \
8277 : ((v) & ~0xffffff) == 0 \
8278 ? ((v) & ~0xfffff) == 0 \
8279 ? ((v) & ~0x3ffff) == 0 \
8280 ? ((v) & ~0x1ffff) == 0 \
8281 ? 15 \
8282 : 14 \
8283 : ((v) & ~0x7ffff) == 0 \
8284 ? 13 \
8285 : 12 \
8286 : ((v) & ~0x3fffff) == 0 \
8287 ? ((v) & ~0x1fffff) == 0 \
8288 ? 11 \
8289 : 10 \
8290 : ((v) & ~0x7fffff) == 0 \
8291 ? 9 \
8292 : 8 \
8293 : ((v) & ~0xfffffff) == 0 \
8294 ? ((v) & ~0x3ffffff) == 0 \
8295 ? ((v) & ~0x1ffffff) == 0 \
8296 ? 7 \
8297 : 6 \
8298 : ((v) & ~0x7ffffff) == 0 \
8299 ? 5 \
8300 : 4 \
8301 : ((v) & ~0x3fffffff) == 0 \
8302 ? ((v) & ~0x1fffffff) == 0 \
8303 ? 3 \
8304 : 2 \
8305 : ((v) & ~0x7fffffff) == 0 \
8306 ? 1 \
8307 : 0)
8308
8309/* load_register()
67c1ffbe 8310 * This routine generates the least number of instructions necessary to load
252b5132
RH
8311 * an absolute expression value into a register.
8312 */
8313static void
67c0d1eb 8314load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
8315{
8316 int freg;
8317 expressionS hi32, lo32;
8318
8319 if (ep->X_op != O_big)
8320 {
9c2799c2 8321 gas_assert (ep->X_op == O_constant);
256ab948
TS
8322
8323 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
8324 if (!dbl)
8325 normalize_constant_expr (ep);
256ab948
TS
8326
8327 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
8328 {
8329 /* We can handle 16 bit signed values with an addiu to
8330 $zero. No need to ever use daddiu here, since $zero and
8331 the result are always correct in 32 bit mode. */
67c0d1eb 8332 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
8333 return;
8334 }
8335 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
8336 {
8337 /* We can handle 16 bit unsigned values with an ori to
8338 $zero. */
67c0d1eb 8339 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
8340 return;
8341 }
256ab948 8342 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
8343 {
8344 /* 32 bit values require an lui. */
df58fc94 8345 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 8346 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 8347 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
8348 return;
8349 }
8350 }
8351
8352 /* The value is larger than 32 bits. */
8353
bad1aba3 8354 if (!dbl || GPR_SIZE == 32)
252b5132 8355 {
55e08f71
NC
8356 char value[32];
8357
8358 sprintf_vma (value, ep->X_add_number);
1661c76c 8359 as_bad (_("number (0x%s) larger than 32 bits"), value);
67c0d1eb 8360 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
8361 return;
8362 }
8363
8364 if (ep->X_op != O_big)
8365 {
8366 hi32 = *ep;
8367 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
8368 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
8369 hi32.X_add_number &= 0xffffffff;
8370 lo32 = *ep;
8371 lo32.X_add_number &= 0xffffffff;
8372 }
8373 else
8374 {
9c2799c2 8375 gas_assert (ep->X_add_number > 2);
252b5132
RH
8376 if (ep->X_add_number == 3)
8377 generic_bignum[3] = 0;
8378 else if (ep->X_add_number > 4)
1661c76c 8379 as_bad (_("number larger than 64 bits"));
252b5132
RH
8380 lo32.X_op = O_constant;
8381 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
8382 hi32.X_op = O_constant;
8383 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
8384 }
8385
8386 if (hi32.X_add_number == 0)
8387 freg = 0;
8388 else
8389 {
8390 int shift, bit;
8391 unsigned long hi, lo;
8392
956cd1d6 8393 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
8394 {
8395 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
8396 {
67c0d1eb 8397 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
8398 return;
8399 }
8400 if (lo32.X_add_number & 0x80000000)
8401 {
df58fc94 8402 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 8403 if (lo32.X_add_number & 0xffff)
67c0d1eb 8404 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
8405 return;
8406 }
8407 }
252b5132
RH
8408
8409 /* Check for 16bit shifted constant. We know that hi32 is
8410 non-zero, so start the mask on the first bit of the hi32
8411 value. */
8412 shift = 17;
8413 do
beae10d5
KH
8414 {
8415 unsigned long himask, lomask;
8416
8417 if (shift < 32)
8418 {
8419 himask = 0xffff >> (32 - shift);
8420 lomask = (0xffff << shift) & 0xffffffff;
8421 }
8422 else
8423 {
8424 himask = 0xffff << (shift - 32);
8425 lomask = 0;
8426 }
8427 if ((hi32.X_add_number & ~(offsetT) himask) == 0
8428 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
8429 {
8430 expressionS tmp;
8431
8432 tmp.X_op = O_constant;
8433 if (shift < 32)
8434 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
8435 | (lo32.X_add_number >> shift));
8436 else
8437 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb 8438 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
df58fc94 8439 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 8440 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
8441 return;
8442 }
f9419b05 8443 ++shift;
beae10d5
KH
8444 }
8445 while (shift <= (64 - 16));
252b5132
RH
8446
8447 /* Find the bit number of the lowest one bit, and store the
8448 shifted value in hi/lo. */
8449 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
8450 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
8451 if (lo != 0)
8452 {
8453 bit = 0;
8454 while ((lo & 1) == 0)
8455 {
8456 lo >>= 1;
8457 ++bit;
8458 }
8459 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
8460 hi >>= bit;
8461 }
8462 else
8463 {
8464 bit = 32;
8465 while ((hi & 1) == 0)
8466 {
8467 hi >>= 1;
8468 ++bit;
8469 }
8470 lo = hi;
8471 hi = 0;
8472 }
8473
8474 /* Optimize if the shifted value is a (power of 2) - 1. */
8475 if ((hi == 0 && ((lo + 1) & lo) == 0)
8476 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
8477 {
8478 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 8479 if (shift != 0)
beae10d5 8480 {
252b5132
RH
8481 expressionS tmp;
8482
8483 /* This instruction will set the register to be all
8484 ones. */
beae10d5
KH
8485 tmp.X_op = O_constant;
8486 tmp.X_add_number = (offsetT) -1;
67c0d1eb 8487 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
8488 if (bit != 0)
8489 {
8490 bit += shift;
df58fc94 8491 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 8492 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 8493 }
df58fc94 8494 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
67c0d1eb 8495 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
8496 return;
8497 }
8498 }
252b5132
RH
8499
8500 /* Sign extend hi32 before calling load_register, because we can
8501 generally get better code when we load a sign extended value. */
8502 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 8503 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 8504 load_register (reg, &hi32, 0);
252b5132
RH
8505 freg = reg;
8506 }
8507 if ((lo32.X_add_number & 0xffff0000) == 0)
8508 {
8509 if (freg != 0)
8510 {
df58fc94 8511 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
252b5132
RH
8512 freg = reg;
8513 }
8514 }
8515 else
8516 {
8517 expressionS mid16;
8518
956cd1d6 8519 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 8520 {
df58fc94
RS
8521 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
8522 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
beae10d5
KH
8523 return;
8524 }
252b5132
RH
8525
8526 if (freg != 0)
8527 {
df58fc94 8528 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
252b5132
RH
8529 freg = reg;
8530 }
8531 mid16 = lo32;
8532 mid16.X_add_number >>= 16;
67c0d1eb 8533 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
df58fc94 8534 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
252b5132
RH
8535 freg = reg;
8536 }
8537 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 8538 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
8539}
8540
269137b2
TS
8541static inline void
8542load_delay_nop (void)
8543{
8544 if (!gpr_interlocks)
8545 macro_build (NULL, "nop", "");
8546}
8547
252b5132
RH
8548/* Load an address into a register. */
8549
8550static void
67c0d1eb 8551load_address (int reg, expressionS *ep, int *used_at)
252b5132 8552{
252b5132
RH
8553 if (ep->X_op != O_constant
8554 && ep->X_op != O_symbol)
8555 {
8556 as_bad (_("expression too complex"));
8557 ep->X_op = O_constant;
8558 }
8559
8560 if (ep->X_op == O_constant)
8561 {
67c0d1eb 8562 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
8563 return;
8564 }
8565
8566 if (mips_pic == NO_PIC)
8567 {
8568 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 8569 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
8570 Otherwise we want
8571 lui $reg,<sym> (BFD_RELOC_HI16_S)
8572 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 8573 If we have an addend, we always use the latter form.
76b3015f 8574
d6bc6245
TS
8575 With 64bit address space and a usable $at we want
8576 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8577 lui $at,<sym> (BFD_RELOC_HI16_S)
8578 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
8579 daddiu $at,<sym> (BFD_RELOC_LO16)
8580 dsll32 $reg,0
3a482fd5 8581 daddu $reg,$reg,$at
76b3015f 8582
c03099e6 8583 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
8584 on superscalar processors.
8585 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8586 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
8587 dsll $reg,16
8588 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
8589 dsll $reg,16
8590 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
8591
8592 For GP relative symbols in 64bit address space we can use
8593 the same sequence as in 32bit address space. */
aed1a261 8594 if (HAVE_64BIT_SYMBOLS)
d6bc6245 8595 {
6caf9ef4
TS
8596 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
8597 && !nopic_need_relax (ep->X_add_symbol, 1))
8598 {
8599 relax_start (ep->X_add_symbol);
8600 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
8601 mips_gp_register, BFD_RELOC_GPREL16);
8602 relax_switch ();
8603 }
d6bc6245 8604
741fe287 8605 if (*used_at == 0 && mips_opts.at)
d6bc6245 8606 {
df58fc94
RS
8607 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
8608 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
67c0d1eb
RS
8609 macro_build (ep, "daddiu", "t,r,j", reg, reg,
8610 BFD_RELOC_MIPS_HIGHER);
8611 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
df58fc94 8612 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
67c0d1eb 8613 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
8614 *used_at = 1;
8615 }
8616 else
8617 {
df58fc94 8618 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb
RS
8619 macro_build (ep, "daddiu", "t,r,j", reg, reg,
8620 BFD_RELOC_MIPS_HIGHER);
df58fc94 8621 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 8622 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
df58fc94 8623 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 8624 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 8625 }
6caf9ef4
TS
8626
8627 if (mips_relax.sequence)
8628 relax_end ();
d6bc6245 8629 }
252b5132
RH
8630 else
8631 {
d6bc6245 8632 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 8633 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 8634 {
4d7206a2 8635 relax_start (ep->X_add_symbol);
67c0d1eb 8636 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 8637 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 8638 relax_switch ();
d6bc6245 8639 }
67c0d1eb
RS
8640 macro_build_lui (ep, reg);
8641 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
8642 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
8643 if (mips_relax.sequence)
8644 relax_end ();
d6bc6245 8645 }
252b5132 8646 }
0a44bf69 8647 else if (!mips_big_got)
252b5132
RH
8648 {
8649 expressionS ex;
8650
8651 /* If this is a reference to an external symbol, we want
8652 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8653 Otherwise we want
8654 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8655 nop
8656 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
8657 If there is a constant, it must be added in after.
8658
ed6fb7bd 8659 If we have NewABI, we want
f5040a92
AO
8660 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
8661 unless we're referencing a global symbol with a non-zero
8662 offset, in which case cst must be added separately. */
ed6fb7bd
SC
8663 if (HAVE_NEWABI)
8664 {
f5040a92
AO
8665 if (ep->X_add_number)
8666 {
4d7206a2 8667 ex.X_add_number = ep->X_add_number;
f5040a92 8668 ep->X_add_number = 0;
4d7206a2 8669 relax_start (ep->X_add_symbol);
67c0d1eb
RS
8670 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
8671 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
8672 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
8673 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
8674 ex.X_op = O_constant;
67c0d1eb 8675 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 8676 reg, reg, BFD_RELOC_LO16);
f5040a92 8677 ep->X_add_number = ex.X_add_number;
4d7206a2 8678 relax_switch ();
f5040a92 8679 }
67c0d1eb 8680 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 8681 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
8682 if (mips_relax.sequence)
8683 relax_end ();
ed6fb7bd
SC
8684 }
8685 else
8686 {
f5040a92
AO
8687 ex.X_add_number = ep->X_add_number;
8688 ep->X_add_number = 0;
67c0d1eb
RS
8689 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
8690 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 8691 load_delay_nop ();
4d7206a2
RS
8692 relax_start (ep->X_add_symbol);
8693 relax_switch ();
67c0d1eb 8694 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 8695 BFD_RELOC_LO16);
4d7206a2 8696 relax_end ();
ed6fb7bd 8697
f5040a92
AO
8698 if (ex.X_add_number != 0)
8699 {
8700 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
8701 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
8702 ex.X_op = O_constant;
67c0d1eb 8703 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 8704 reg, reg, BFD_RELOC_LO16);
f5040a92 8705 }
252b5132
RH
8706 }
8707 }
0a44bf69 8708 else if (mips_big_got)
252b5132
RH
8709 {
8710 expressionS ex;
252b5132
RH
8711
8712 /* This is the large GOT case. If this is a reference to an
8713 external symbol, we want
8714 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
8715 addu $reg,$reg,$gp
8716 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
8717
8718 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
8719 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8720 nop
8721 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 8722 If there is a constant, it must be added in after.
f5040a92
AO
8723
8724 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
8725 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
8726 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 8727 */
438c16b8
TS
8728 if (HAVE_NEWABI)
8729 {
4d7206a2 8730 ex.X_add_number = ep->X_add_number;
f5040a92 8731 ep->X_add_number = 0;
4d7206a2 8732 relax_start (ep->X_add_symbol);
df58fc94 8733 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
8734 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8735 reg, reg, mips_gp_register);
8736 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
8737 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
8738 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
8739 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
8740 else if (ex.X_add_number)
8741 {
8742 ex.X_op = O_constant;
67c0d1eb
RS
8743 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
8744 BFD_RELOC_LO16);
f5040a92
AO
8745 }
8746
8747 ep->X_add_number = ex.X_add_number;
4d7206a2 8748 relax_switch ();
67c0d1eb 8749 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 8750 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
8751 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
8752 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 8753 relax_end ();
438c16b8 8754 }
252b5132 8755 else
438c16b8 8756 {
f5040a92
AO
8757 ex.X_add_number = ep->X_add_number;
8758 ep->X_add_number = 0;
4d7206a2 8759 relax_start (ep->X_add_symbol);
df58fc94 8760 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
8761 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8762 reg, reg, mips_gp_register);
8763 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
8764 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
8765 relax_switch ();
8766 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
8767 {
8768 /* We need a nop before loading from $gp. This special
8769 check is required because the lui which starts the main
8770 instruction stream does not refer to $gp, and so will not
8771 insert the nop which may be required. */
67c0d1eb 8772 macro_build (NULL, "nop", "");
438c16b8 8773 }
67c0d1eb 8774 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 8775 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 8776 load_delay_nop ();
67c0d1eb 8777 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 8778 BFD_RELOC_LO16);
4d7206a2 8779 relax_end ();
438c16b8 8780
f5040a92
AO
8781 if (ex.X_add_number != 0)
8782 {
8783 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
8784 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
8785 ex.X_op = O_constant;
67c0d1eb
RS
8786 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
8787 BFD_RELOC_LO16);
f5040a92 8788 }
252b5132
RH
8789 }
8790 }
252b5132
RH
8791 else
8792 abort ();
8fc2e39e 8793
741fe287 8794 if (!mips_opts.at && *used_at == 1)
1661c76c 8795 as_bad (_("macro used $at after \".set noat\""));
252b5132
RH
8796}
8797
ea1fb5dc
RS
8798/* Move the contents of register SOURCE into register DEST. */
8799
8800static void
67c0d1eb 8801move_register (int dest, int source)
ea1fb5dc 8802{
df58fc94
RS
8803 /* Prefer to use a 16-bit microMIPS instruction unless the previous
8804 instruction specifically requires a 32-bit one. */
8805 if (mips_opts.micromips
833794fc 8806 && !mips_opts.insn32
df58fc94 8807 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7951ca42 8808 macro_build (NULL, "move", "mp,mj", dest, source);
df58fc94 8809 else
bad1aba3 8810 macro_build (NULL, GPR_SIZE == 32 ? "addu" : "daddu", "d,v,t",
df58fc94 8811 dest, source, 0);
ea1fb5dc
RS
8812}
8813
4d7206a2 8814/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
8815 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
8816 The two alternatives are:
4d7206a2
RS
8817
8818 Global symbol Local sybmol
8819 ------------- ------------
8820 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
8821 ... ...
8822 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
8823
8824 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
8825 emits the second for a 16-bit offset or add_got_offset_hilo emits
8826 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
8827
8828static void
67c0d1eb 8829load_got_offset (int dest, expressionS *local)
4d7206a2
RS
8830{
8831 expressionS global;
8832
8833 global = *local;
8834 global.X_add_number = 0;
8835
8836 relax_start (local->X_add_symbol);
67c0d1eb
RS
8837 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
8838 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 8839 relax_switch ();
67c0d1eb
RS
8840 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
8841 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
8842 relax_end ();
8843}
8844
8845static void
67c0d1eb 8846add_got_offset (int dest, expressionS *local)
4d7206a2
RS
8847{
8848 expressionS global;
8849
8850 global.X_op = O_constant;
8851 global.X_op_symbol = NULL;
8852 global.X_add_symbol = NULL;
8853 global.X_add_number = local->X_add_number;
8854
8855 relax_start (local->X_add_symbol);
67c0d1eb 8856 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
8857 dest, dest, BFD_RELOC_LO16);
8858 relax_switch ();
67c0d1eb 8859 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
8860 relax_end ();
8861}
8862
f6a22291
MR
8863static void
8864add_got_offset_hilo (int dest, expressionS *local, int tmp)
8865{
8866 expressionS global;
8867 int hold_mips_optimize;
8868
8869 global.X_op = O_constant;
8870 global.X_op_symbol = NULL;
8871 global.X_add_symbol = NULL;
8872 global.X_add_number = local->X_add_number;
8873
8874 relax_start (local->X_add_symbol);
8875 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
8876 relax_switch ();
8877 /* Set mips_optimize around the lui instruction to avoid
8878 inserting an unnecessary nop after the lw. */
8879 hold_mips_optimize = mips_optimize;
8880 mips_optimize = 2;
8881 macro_build_lui (&global, tmp);
8882 mips_optimize = hold_mips_optimize;
8883 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
8884 relax_end ();
8885
8886 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
8887}
8888
df58fc94
RS
8889/* Emit a sequence of instructions to emulate a branch likely operation.
8890 BR is an ordinary branch corresponding to one to be emulated. BRNEG
8891 is its complementing branch with the original condition negated.
8892 CALL is set if the original branch specified the link operation.
8893 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
8894
8895 Code like this is produced in the noreorder mode:
8896
8897 BRNEG <args>, 1f
8898 nop
8899 b <sym>
8900 delay slot (executed only if branch taken)
8901 1:
8902
8903 or, if CALL is set:
8904
8905 BRNEG <args>, 1f
8906 nop
8907 bal <sym>
8908 delay slot (executed only if branch taken)
8909 1:
8910
8911 In the reorder mode the delay slot would be filled with a nop anyway,
8912 so code produced is simply:
8913
8914 BR <args>, <sym>
8915 nop
8916
8917 This function is used when producing code for the microMIPS ASE that
8918 does not implement branch likely instructions in hardware. */
8919
8920static void
8921macro_build_branch_likely (const char *br, const char *brneg,
8922 int call, expressionS *ep, const char *fmt,
8923 unsigned int sreg, unsigned int treg)
8924{
8925 int noreorder = mips_opts.noreorder;
8926 expressionS expr1;
8927
8928 gas_assert (mips_opts.micromips);
8929 start_noreorder ();
8930 if (noreorder)
8931 {
8932 micromips_label_expr (&expr1);
8933 macro_build (&expr1, brneg, fmt, sreg, treg);
8934 macro_build (NULL, "nop", "");
8935 macro_build (ep, call ? "bal" : "b", "p");
8936
8937 /* Set to true so that append_insn adds a label. */
8938 emit_branch_likely_macro = TRUE;
8939 }
8940 else
8941 {
8942 macro_build (ep, br, fmt, sreg, treg);
8943 macro_build (NULL, "nop", "");
8944 }
8945 end_noreorder ();
8946}
8947
8948/* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
8949 the condition code tested. EP specifies the branch target. */
8950
8951static void
8952macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
8953{
8954 const int call = 0;
8955 const char *brneg;
8956 const char *br;
8957
8958 switch (type)
8959 {
8960 case M_BC1FL:
8961 br = "bc1f";
8962 brneg = "bc1t";
8963 break;
8964 case M_BC1TL:
8965 br = "bc1t";
8966 brneg = "bc1f";
8967 break;
8968 case M_BC2FL:
8969 br = "bc2f";
8970 brneg = "bc2t";
8971 break;
8972 case M_BC2TL:
8973 br = "bc2t";
8974 brneg = "bc2f";
8975 break;
8976 default:
8977 abort ();
8978 }
8979 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
8980}
8981
8982/* Emit a two-argument branch macro specified by TYPE, using SREG as
8983 the register tested. EP specifies the branch target. */
8984
8985static void
8986macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
8987{
8988 const char *brneg = NULL;
8989 const char *br;
8990 int call = 0;
8991
8992 switch (type)
8993 {
8994 case M_BGEZ:
8995 br = "bgez";
8996 break;
8997 case M_BGEZL:
8998 br = mips_opts.micromips ? "bgez" : "bgezl";
8999 brneg = "bltz";
9000 break;
9001 case M_BGEZALL:
9002 gas_assert (mips_opts.micromips);
833794fc 9003 br = mips_opts.insn32 ? "bgezal" : "bgezals";
df58fc94
RS
9004 brneg = "bltz";
9005 call = 1;
9006 break;
9007 case M_BGTZ:
9008 br = "bgtz";
9009 break;
9010 case M_BGTZL:
9011 br = mips_opts.micromips ? "bgtz" : "bgtzl";
9012 brneg = "blez";
9013 break;
9014 case M_BLEZ:
9015 br = "blez";
9016 break;
9017 case M_BLEZL:
9018 br = mips_opts.micromips ? "blez" : "blezl";
9019 brneg = "bgtz";
9020 break;
9021 case M_BLTZ:
9022 br = "bltz";
9023 break;
9024 case M_BLTZL:
9025 br = mips_opts.micromips ? "bltz" : "bltzl";
9026 brneg = "bgez";
9027 break;
9028 case M_BLTZALL:
9029 gas_assert (mips_opts.micromips);
833794fc 9030 br = mips_opts.insn32 ? "bltzal" : "bltzals";
df58fc94
RS
9031 brneg = "bgez";
9032 call = 1;
9033 break;
9034 default:
9035 abort ();
9036 }
9037 if (mips_opts.micromips && brneg)
9038 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
9039 else
9040 macro_build (ep, br, "s,p", sreg);
9041}
9042
9043/* Emit a three-argument branch macro specified by TYPE, using SREG and
9044 TREG as the registers tested. EP specifies the branch target. */
9045
9046static void
9047macro_build_branch_rsrt (int type, expressionS *ep,
9048 unsigned int sreg, unsigned int treg)
9049{
9050 const char *brneg = NULL;
9051 const int call = 0;
9052 const char *br;
9053
9054 switch (type)
9055 {
9056 case M_BEQ:
9057 case M_BEQ_I:
9058 br = "beq";
9059 break;
9060 case M_BEQL:
9061 case M_BEQL_I:
9062 br = mips_opts.micromips ? "beq" : "beql";
9063 brneg = "bne";
9064 break;
9065 case M_BNE:
9066 case M_BNE_I:
9067 br = "bne";
9068 break;
9069 case M_BNEL:
9070 case M_BNEL_I:
9071 br = mips_opts.micromips ? "bne" : "bnel";
9072 brneg = "beq";
9073 break;
9074 default:
9075 abort ();
9076 }
9077 if (mips_opts.micromips && brneg)
9078 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
9079 else
9080 macro_build (ep, br, "s,t,p", sreg, treg);
9081}
9082
f2ae14a1
RS
9083/* Return the high part that should be loaded in order to make the low
9084 part of VALUE accessible using an offset of OFFBITS bits. */
9085
9086static offsetT
9087offset_high_part (offsetT value, unsigned int offbits)
9088{
9089 offsetT bias;
9090 addressT low_mask;
9091
9092 if (offbits == 0)
9093 return value;
9094 bias = 1 << (offbits - 1);
9095 low_mask = bias * 2 - 1;
9096 return (value + bias) & ~low_mask;
9097}
9098
9099/* Return true if the value stored in offset_expr and offset_reloc
9100 fits into a signed offset of OFFBITS bits. RANGE is the maximum
9101 amount that the caller wants to add without inducing overflow
9102 and ALIGN is the known alignment of the value in bytes. */
9103
9104static bfd_boolean
9105small_offset_p (unsigned int range, unsigned int align, unsigned int offbits)
9106{
9107 if (offbits == 16)
9108 {
9109 /* Accept any relocation operator if overflow isn't a concern. */
9110 if (range < align && *offset_reloc != BFD_RELOC_UNUSED)
9111 return TRUE;
9112
9113 /* These relocations are guaranteed not to overflow in correct links. */
9114 if (*offset_reloc == BFD_RELOC_MIPS_LITERAL
9115 || gprel16_reloc_p (*offset_reloc))
9116 return TRUE;
9117 }
9118 if (offset_expr.X_op == O_constant
9119 && offset_high_part (offset_expr.X_add_number, offbits) == 0
9120 && offset_high_part (offset_expr.X_add_number + range, offbits) == 0)
9121 return TRUE;
9122 return FALSE;
9123}
9124
252b5132
RH
9125/*
9126 * Build macros
9127 * This routine implements the seemingly endless macro or synthesized
9128 * instructions and addressing modes in the mips assembly language. Many
9129 * of these macros are simple and are similar to each other. These could
67c1ffbe 9130 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
9131 * this verbose method. Others are not simple macros but are more like
9132 * optimizing code generation.
9133 * One interesting optimization is when several store macros appear
67c1ffbe 9134 * consecutively that would load AT with the upper half of the same address.
252b5132
RH
9135 * The ensuing load upper instructions are ommited. This implies some kind
9136 * of global optimization. We currently only optimize within a single macro.
9137 * For many of the load and store macros if the address is specified as a
9138 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
9139 * first load register 'at' with zero and use it as the base register. The
9140 * mips assembler simply uses register $zero. Just one tiny optimization
9141 * we're missing.
9142 */
9143static void
833794fc 9144macro (struct mips_cl_insn *ip, char *str)
252b5132 9145{
c0ebe874
RS
9146 const struct mips_operand_array *operands;
9147 unsigned int breg, i;
741fe287 9148 unsigned int tempreg;
252b5132 9149 int mask;
43841e91 9150 int used_at = 0;
df58fc94 9151 expressionS label_expr;
252b5132 9152 expressionS expr1;
df58fc94 9153 expressionS *ep;
252b5132
RH
9154 const char *s;
9155 const char *s2;
9156 const char *fmt;
9157 int likely = 0;
252b5132 9158 int coproc = 0;
7f3c4072 9159 int offbits = 16;
1abe91b1 9160 int call = 0;
df58fc94
RS
9161 int jals = 0;
9162 int dbl = 0;
9163 int imm = 0;
9164 int ust = 0;
9165 int lp = 0;
f2ae14a1 9166 bfd_boolean large_offset;
252b5132 9167 int off;
252b5132 9168 int hold_mips_optimize;
f2ae14a1 9169 unsigned int align;
c0ebe874 9170 unsigned int op[MAX_OPERANDS];
252b5132 9171
9c2799c2 9172 gas_assert (! mips_opts.mips16);
252b5132 9173
c0ebe874
RS
9174 operands = insn_operands (ip);
9175 for (i = 0; i < MAX_OPERANDS; i++)
9176 if (operands->operand[i])
9177 op[i] = insn_extract_operand (ip, operands->operand[i]);
9178 else
9179 op[i] = -1;
9180
252b5132
RH
9181 mask = ip->insn_mo->mask;
9182
df58fc94
RS
9183 label_expr.X_op = O_constant;
9184 label_expr.X_op_symbol = NULL;
9185 label_expr.X_add_symbol = NULL;
9186 label_expr.X_add_number = 0;
9187
252b5132
RH
9188 expr1.X_op = O_constant;
9189 expr1.X_op_symbol = NULL;
9190 expr1.X_add_symbol = NULL;
9191 expr1.X_add_number = 1;
f2ae14a1 9192 align = 1;
252b5132
RH
9193
9194 switch (mask)
9195 {
9196 case M_DABS:
9197 dbl = 1;
9198 case M_ABS:
df58fc94
RS
9199 /* bgez $a0,1f
9200 move v0,$a0
9201 sub v0,$zero,$a0
9202 1:
9203 */
252b5132 9204
7d10b47d 9205 start_noreorder ();
252b5132 9206
df58fc94
RS
9207 if (mips_opts.micromips)
9208 micromips_label_expr (&label_expr);
9209 else
9210 label_expr.X_add_number = 8;
c0ebe874
RS
9211 macro_build (&label_expr, "bgez", "s,p", op[1]);
9212 if (op[0] == op[1])
a605d2b3 9213 macro_build (NULL, "nop", "");
252b5132 9214 else
c0ebe874
RS
9215 move_register (op[0], op[1]);
9216 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", op[0], 0, op[1]);
df58fc94
RS
9217 if (mips_opts.micromips)
9218 micromips_add_label ();
252b5132 9219
7d10b47d 9220 end_noreorder ();
8fc2e39e 9221 break;
252b5132
RH
9222
9223 case M_ADD_I:
9224 s = "addi";
9225 s2 = "add";
9226 goto do_addi;
9227 case M_ADDU_I:
9228 s = "addiu";
9229 s2 = "addu";
9230 goto do_addi;
9231 case M_DADD_I:
9232 dbl = 1;
9233 s = "daddi";
9234 s2 = "dadd";
df58fc94
RS
9235 if (!mips_opts.micromips)
9236 goto do_addi;
b0e6f033 9237 if (imm_expr.X_add_number >= -0x200
df58fc94
RS
9238 && imm_expr.X_add_number < 0x200)
9239 {
b0e6f033
RS
9240 macro_build (NULL, s, "t,r,.", op[0], op[1],
9241 (int) imm_expr.X_add_number);
df58fc94
RS
9242 break;
9243 }
9244 goto do_addi_i;
252b5132
RH
9245 case M_DADDU_I:
9246 dbl = 1;
9247 s = "daddiu";
9248 s2 = "daddu";
9249 do_addi:
b0e6f033 9250 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
9251 && imm_expr.X_add_number < 0x8000)
9252 {
c0ebe874 9253 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 9254 break;
252b5132 9255 }
df58fc94 9256 do_addi_i:
8fc2e39e 9257 used_at = 1;
67c0d1eb 9258 load_register (AT, &imm_expr, dbl);
c0ebe874 9259 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
9260 break;
9261
9262 case M_AND_I:
9263 s = "andi";
9264 s2 = "and";
9265 goto do_bit;
9266 case M_OR_I:
9267 s = "ori";
9268 s2 = "or";
9269 goto do_bit;
9270 case M_NOR_I:
9271 s = "";
9272 s2 = "nor";
9273 goto do_bit;
9274 case M_XOR_I:
9275 s = "xori";
9276 s2 = "xor";
9277 do_bit:
b0e6f033 9278 if (imm_expr.X_add_number >= 0
252b5132
RH
9279 && imm_expr.X_add_number < 0x10000)
9280 {
9281 if (mask != M_NOR_I)
c0ebe874 9282 macro_build (&imm_expr, s, "t,r,i", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
9283 else
9284 {
67c0d1eb 9285 macro_build (&imm_expr, "ori", "t,r,i",
c0ebe874
RS
9286 op[0], op[1], BFD_RELOC_LO16);
9287 macro_build (NULL, "nor", "d,v,t", op[0], op[0], 0);
252b5132 9288 }
8fc2e39e 9289 break;
252b5132
RH
9290 }
9291
8fc2e39e 9292 used_at = 1;
bad1aba3 9293 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 9294 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
9295 break;
9296
8b082fb1
TS
9297 case M_BALIGN:
9298 switch (imm_expr.X_add_number)
9299 {
9300 case 0:
9301 macro_build (NULL, "nop", "");
9302 break;
9303 case 2:
c0ebe874 9304 macro_build (NULL, "packrl.ph", "d,s,t", op[0], op[0], op[1]);
8b082fb1 9305 break;
03f66e8a
MR
9306 case 1:
9307 case 3:
c0ebe874 9308 macro_build (NULL, "balign", "t,s,2", op[0], op[1],
90ecf173 9309 (int) imm_expr.X_add_number);
8b082fb1 9310 break;
03f66e8a
MR
9311 default:
9312 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
9313 (unsigned long) imm_expr.X_add_number);
9314 break;
8b082fb1
TS
9315 }
9316 break;
9317
df58fc94
RS
9318 case M_BC1FL:
9319 case M_BC1TL:
9320 case M_BC2FL:
9321 case M_BC2TL:
9322 gas_assert (mips_opts.micromips);
9323 macro_build_branch_ccl (mask, &offset_expr,
9324 EXTRACT_OPERAND (1, BCC, *ip));
9325 break;
9326
252b5132 9327 case M_BEQ_I:
252b5132 9328 case M_BEQL_I:
252b5132 9329 case M_BNE_I:
252b5132 9330 case M_BNEL_I:
b0e6f033 9331 if (imm_expr.X_add_number == 0)
c0ebe874 9332 op[1] = 0;
df58fc94 9333 else
252b5132 9334 {
c0ebe874 9335 op[1] = AT;
df58fc94 9336 used_at = 1;
bad1aba3 9337 load_register (op[1], &imm_expr, GPR_SIZE == 64);
252b5132 9338 }
df58fc94
RS
9339 /* Fall through. */
9340 case M_BEQL:
9341 case M_BNEL:
c0ebe874 9342 macro_build_branch_rsrt (mask, &offset_expr, op[0], op[1]);
252b5132
RH
9343 break;
9344
9345 case M_BGEL:
9346 likely = 1;
9347 case M_BGE:
c0ebe874
RS
9348 if (op[1] == 0)
9349 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[0]);
9350 else if (op[0] == 0)
9351 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[1]);
df58fc94 9352 else
252b5132 9353 {
df58fc94 9354 used_at = 1;
c0ebe874 9355 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
9356 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
9357 &offset_expr, AT, ZERO);
252b5132 9358 }
df58fc94
RS
9359 break;
9360
9361 case M_BGEZL:
9362 case M_BGEZALL:
9363 case M_BGTZL:
9364 case M_BLEZL:
9365 case M_BLTZL:
9366 case M_BLTZALL:
c0ebe874 9367 macro_build_branch_rs (mask, &offset_expr, op[0]);
252b5132
RH
9368 break;
9369
9370 case M_BGTL_I:
9371 likely = 1;
9372 case M_BGT_I:
90ecf173 9373 /* Check for > max integer. */
b0e6f033 9374 if (imm_expr.X_add_number >= GPR_SMAX)
252b5132
RH
9375 {
9376 do_false:
90ecf173 9377 /* Result is always false. */
252b5132 9378 if (! likely)
a605d2b3 9379 macro_build (NULL, "nop", "");
252b5132 9380 else
df58fc94 9381 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
8fc2e39e 9382 break;
252b5132 9383 }
f9419b05 9384 ++imm_expr.X_add_number;
252b5132
RH
9385 /* FALLTHROUGH */
9386 case M_BGE_I:
9387 case M_BGEL_I:
9388 if (mask == M_BGEL_I)
9389 likely = 1;
b0e6f033 9390 if (imm_expr.X_add_number == 0)
252b5132 9391 {
df58fc94 9392 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
c0ebe874 9393 &offset_expr, op[0]);
8fc2e39e 9394 break;
252b5132 9395 }
b0e6f033 9396 if (imm_expr.X_add_number == 1)
252b5132 9397 {
df58fc94 9398 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
c0ebe874 9399 &offset_expr, op[0]);
8fc2e39e 9400 break;
252b5132 9401 }
b0e6f033 9402 if (imm_expr.X_add_number <= GPR_SMIN)
252b5132
RH
9403 {
9404 do_true:
9405 /* result is always true */
1661c76c 9406 as_warn (_("branch %s is always true"), ip->insn_mo->name);
67c0d1eb 9407 macro_build (&offset_expr, "b", "p");
8fc2e39e 9408 break;
252b5132 9409 }
8fc2e39e 9410 used_at = 1;
c0ebe874 9411 set_at (op[0], 0);
df58fc94
RS
9412 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
9413 &offset_expr, AT, ZERO);
252b5132
RH
9414 break;
9415
9416 case M_BGEUL:
9417 likely = 1;
9418 case M_BGEU:
c0ebe874 9419 if (op[1] == 0)
252b5132 9420 goto do_true;
c0ebe874 9421 else if (op[0] == 0)
df58fc94 9422 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 9423 &offset_expr, ZERO, op[1]);
df58fc94 9424 else
252b5132 9425 {
df58fc94 9426 used_at = 1;
c0ebe874 9427 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
9428 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
9429 &offset_expr, AT, ZERO);
252b5132 9430 }
252b5132
RH
9431 break;
9432
9433 case M_BGTUL_I:
9434 likely = 1;
9435 case M_BGTU_I:
c0ebe874 9436 if (op[0] == 0
bad1aba3 9437 || (GPR_SIZE == 32
f01dc953 9438 && imm_expr.X_add_number == -1))
252b5132 9439 goto do_false;
f9419b05 9440 ++imm_expr.X_add_number;
252b5132
RH
9441 /* FALLTHROUGH */
9442 case M_BGEU_I:
9443 case M_BGEUL_I:
9444 if (mask == M_BGEUL_I)
9445 likely = 1;
b0e6f033 9446 if (imm_expr.X_add_number == 0)
252b5132 9447 goto do_true;
b0e6f033 9448 else if (imm_expr.X_add_number == 1)
df58fc94 9449 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 9450 &offset_expr, op[0], ZERO);
df58fc94 9451 else
252b5132 9452 {
df58fc94 9453 used_at = 1;
c0ebe874 9454 set_at (op[0], 1);
df58fc94
RS
9455 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
9456 &offset_expr, AT, ZERO);
252b5132 9457 }
252b5132
RH
9458 break;
9459
9460 case M_BGTL:
9461 likely = 1;
9462 case M_BGT:
c0ebe874
RS
9463 if (op[1] == 0)
9464 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[0]);
9465 else if (op[0] == 0)
9466 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[1]);
df58fc94 9467 else
252b5132 9468 {
df58fc94 9469 used_at = 1;
c0ebe874 9470 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
9471 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
9472 &offset_expr, AT, ZERO);
252b5132 9473 }
252b5132
RH
9474 break;
9475
9476 case M_BGTUL:
9477 likely = 1;
9478 case M_BGTU:
c0ebe874 9479 if (op[1] == 0)
df58fc94 9480 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874
RS
9481 &offset_expr, op[0], ZERO);
9482 else if (op[0] == 0)
df58fc94
RS
9483 goto do_false;
9484 else
252b5132 9485 {
df58fc94 9486 used_at = 1;
c0ebe874 9487 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
9488 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
9489 &offset_expr, AT, ZERO);
252b5132 9490 }
252b5132
RH
9491 break;
9492
9493 case M_BLEL:
9494 likely = 1;
9495 case M_BLE:
c0ebe874
RS
9496 if (op[1] == 0)
9497 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
9498 else if (op[0] == 0)
9499 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[1]);
df58fc94 9500 else
252b5132 9501 {
df58fc94 9502 used_at = 1;
c0ebe874 9503 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
9504 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
9505 &offset_expr, AT, ZERO);
252b5132 9506 }
252b5132
RH
9507 break;
9508
9509 case M_BLEL_I:
9510 likely = 1;
9511 case M_BLE_I:
b0e6f033 9512 if (imm_expr.X_add_number >= GPR_SMAX)
252b5132 9513 goto do_true;
f9419b05 9514 ++imm_expr.X_add_number;
252b5132
RH
9515 /* FALLTHROUGH */
9516 case M_BLT_I:
9517 case M_BLTL_I:
9518 if (mask == M_BLTL_I)
9519 likely = 1;
b0e6f033 9520 if (imm_expr.X_add_number == 0)
c0ebe874 9521 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
b0e6f033 9522 else if (imm_expr.X_add_number == 1)
c0ebe874 9523 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
df58fc94 9524 else
252b5132 9525 {
df58fc94 9526 used_at = 1;
c0ebe874 9527 set_at (op[0], 0);
df58fc94
RS
9528 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
9529 &offset_expr, AT, ZERO);
252b5132 9530 }
252b5132
RH
9531 break;
9532
9533 case M_BLEUL:
9534 likely = 1;
9535 case M_BLEU:
c0ebe874 9536 if (op[1] == 0)
df58fc94 9537 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874
RS
9538 &offset_expr, op[0], ZERO);
9539 else if (op[0] == 0)
df58fc94
RS
9540 goto do_true;
9541 else
252b5132 9542 {
df58fc94 9543 used_at = 1;
c0ebe874 9544 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
9545 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
9546 &offset_expr, AT, ZERO);
252b5132 9547 }
252b5132
RH
9548 break;
9549
9550 case M_BLEUL_I:
9551 likely = 1;
9552 case M_BLEU_I:
c0ebe874 9553 if (op[0] == 0
bad1aba3 9554 || (GPR_SIZE == 32
f01dc953 9555 && imm_expr.X_add_number == -1))
252b5132 9556 goto do_true;
f9419b05 9557 ++imm_expr.X_add_number;
252b5132
RH
9558 /* FALLTHROUGH */
9559 case M_BLTU_I:
9560 case M_BLTUL_I:
9561 if (mask == M_BLTUL_I)
9562 likely = 1;
b0e6f033 9563 if (imm_expr.X_add_number == 0)
252b5132 9564 goto do_false;
b0e6f033 9565 else if (imm_expr.X_add_number == 1)
df58fc94 9566 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 9567 &offset_expr, op[0], ZERO);
df58fc94 9568 else
252b5132 9569 {
df58fc94 9570 used_at = 1;
c0ebe874 9571 set_at (op[0], 1);
df58fc94
RS
9572 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
9573 &offset_expr, AT, ZERO);
252b5132 9574 }
252b5132
RH
9575 break;
9576
9577 case M_BLTL:
9578 likely = 1;
9579 case M_BLT:
c0ebe874
RS
9580 if (op[1] == 0)
9581 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
9582 else if (op[0] == 0)
9583 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[1]);
df58fc94 9584 else
252b5132 9585 {
df58fc94 9586 used_at = 1;
c0ebe874 9587 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
9588 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
9589 &offset_expr, AT, ZERO);
252b5132 9590 }
252b5132
RH
9591 break;
9592
9593 case M_BLTUL:
9594 likely = 1;
9595 case M_BLTU:
c0ebe874 9596 if (op[1] == 0)
252b5132 9597 goto do_false;
c0ebe874 9598 else if (op[0] == 0)
df58fc94 9599 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 9600 &offset_expr, ZERO, op[1]);
df58fc94 9601 else
252b5132 9602 {
df58fc94 9603 used_at = 1;
c0ebe874 9604 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
9605 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
9606 &offset_expr, AT, ZERO);
252b5132 9607 }
252b5132
RH
9608 break;
9609
9610 case M_DDIV_3:
9611 dbl = 1;
9612 case M_DIV_3:
9613 s = "mflo";
9614 goto do_div3;
9615 case M_DREM_3:
9616 dbl = 1;
9617 case M_REM_3:
9618 s = "mfhi";
9619 do_div3:
c0ebe874 9620 if (op[2] == 0)
252b5132 9621 {
1661c76c 9622 as_warn (_("divide by zero"));
252b5132 9623 if (mips_trap)
df58fc94 9624 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 9625 else
df58fc94 9626 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 9627 break;
252b5132
RH
9628 }
9629
7d10b47d 9630 start_noreorder ();
252b5132
RH
9631 if (mips_trap)
9632 {
c0ebe874
RS
9633 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
9634 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
252b5132
RH
9635 }
9636 else
9637 {
df58fc94
RS
9638 if (mips_opts.micromips)
9639 micromips_label_expr (&label_expr);
9640 else
9641 label_expr.X_add_number = 8;
c0ebe874
RS
9642 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
9643 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
df58fc94
RS
9644 macro_build (NULL, "break", BRK_FMT, 7);
9645 if (mips_opts.micromips)
9646 micromips_add_label ();
252b5132
RH
9647 }
9648 expr1.X_add_number = -1;
8fc2e39e 9649 used_at = 1;
f6a22291 9650 load_register (AT, &expr1, dbl);
df58fc94
RS
9651 if (mips_opts.micromips)
9652 micromips_label_expr (&label_expr);
9653 else
9654 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
c0ebe874 9655 macro_build (&label_expr, "bne", "s,t,p", op[2], AT);
252b5132
RH
9656 if (dbl)
9657 {
9658 expr1.X_add_number = 1;
f6a22291 9659 load_register (AT, &expr1, dbl);
df58fc94 9660 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
252b5132
RH
9661 }
9662 else
9663 {
9664 expr1.X_add_number = 0x80000000;
df58fc94 9665 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
252b5132
RH
9666 }
9667 if (mips_trap)
9668 {
c0ebe874 9669 macro_build (NULL, "teq", TRAP_FMT, op[1], AT, 6);
252b5132
RH
9670 /* We want to close the noreorder block as soon as possible, so
9671 that later insns are available for delay slot filling. */
7d10b47d 9672 end_noreorder ();
252b5132
RH
9673 }
9674 else
9675 {
df58fc94
RS
9676 if (mips_opts.micromips)
9677 micromips_label_expr (&label_expr);
9678 else
9679 label_expr.X_add_number = 8;
c0ebe874 9680 macro_build (&label_expr, "bne", "s,t,p", op[1], AT);
a605d2b3 9681 macro_build (NULL, "nop", "");
252b5132
RH
9682
9683 /* We want to close the noreorder block as soon as possible, so
9684 that later insns are available for delay slot filling. */
7d10b47d 9685 end_noreorder ();
252b5132 9686
df58fc94 9687 macro_build (NULL, "break", BRK_FMT, 6);
252b5132 9688 }
df58fc94
RS
9689 if (mips_opts.micromips)
9690 micromips_add_label ();
c0ebe874 9691 macro_build (NULL, s, MFHL_FMT, op[0]);
252b5132
RH
9692 break;
9693
9694 case M_DIV_3I:
9695 s = "div";
9696 s2 = "mflo";
9697 goto do_divi;
9698 case M_DIVU_3I:
9699 s = "divu";
9700 s2 = "mflo";
9701 goto do_divi;
9702 case M_REM_3I:
9703 s = "div";
9704 s2 = "mfhi";
9705 goto do_divi;
9706 case M_REMU_3I:
9707 s = "divu";
9708 s2 = "mfhi";
9709 goto do_divi;
9710 case M_DDIV_3I:
9711 dbl = 1;
9712 s = "ddiv";
9713 s2 = "mflo";
9714 goto do_divi;
9715 case M_DDIVU_3I:
9716 dbl = 1;
9717 s = "ddivu";
9718 s2 = "mflo";
9719 goto do_divi;
9720 case M_DREM_3I:
9721 dbl = 1;
9722 s = "ddiv";
9723 s2 = "mfhi";
9724 goto do_divi;
9725 case M_DREMU_3I:
9726 dbl = 1;
9727 s = "ddivu";
9728 s2 = "mfhi";
9729 do_divi:
b0e6f033 9730 if (imm_expr.X_add_number == 0)
252b5132 9731 {
1661c76c 9732 as_warn (_("divide by zero"));
252b5132 9733 if (mips_trap)
df58fc94 9734 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 9735 else
df58fc94 9736 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 9737 break;
252b5132 9738 }
b0e6f033 9739 if (imm_expr.X_add_number == 1)
252b5132
RH
9740 {
9741 if (strcmp (s2, "mflo") == 0)
c0ebe874 9742 move_register (op[0], op[1]);
252b5132 9743 else
c0ebe874 9744 move_register (op[0], ZERO);
8fc2e39e 9745 break;
252b5132 9746 }
b0e6f033 9747 if (imm_expr.X_add_number == -1 && s[strlen (s) - 1] != 'u')
252b5132
RH
9748 {
9749 if (strcmp (s2, "mflo") == 0)
c0ebe874 9750 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", op[0], op[1]);
252b5132 9751 else
c0ebe874 9752 move_register (op[0], ZERO);
8fc2e39e 9753 break;
252b5132
RH
9754 }
9755
8fc2e39e 9756 used_at = 1;
67c0d1eb 9757 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
9758 macro_build (NULL, s, "z,s,t", op[1], AT);
9759 macro_build (NULL, s2, MFHL_FMT, op[0]);
252b5132
RH
9760 break;
9761
9762 case M_DIVU_3:
9763 s = "divu";
9764 s2 = "mflo";
9765 goto do_divu3;
9766 case M_REMU_3:
9767 s = "divu";
9768 s2 = "mfhi";
9769 goto do_divu3;
9770 case M_DDIVU_3:
9771 s = "ddivu";
9772 s2 = "mflo";
9773 goto do_divu3;
9774 case M_DREMU_3:
9775 s = "ddivu";
9776 s2 = "mfhi";
9777 do_divu3:
7d10b47d 9778 start_noreorder ();
252b5132
RH
9779 if (mips_trap)
9780 {
c0ebe874
RS
9781 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
9782 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
9783 /* We want to close the noreorder block as soon as possible, so
9784 that later insns are available for delay slot filling. */
7d10b47d 9785 end_noreorder ();
252b5132
RH
9786 }
9787 else
9788 {
df58fc94
RS
9789 if (mips_opts.micromips)
9790 micromips_label_expr (&label_expr);
9791 else
9792 label_expr.X_add_number = 8;
c0ebe874
RS
9793 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
9794 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
9795
9796 /* We want to close the noreorder block as soon as possible, so
9797 that later insns are available for delay slot filling. */
7d10b47d 9798 end_noreorder ();
df58fc94
RS
9799 macro_build (NULL, "break", BRK_FMT, 7);
9800 if (mips_opts.micromips)
9801 micromips_add_label ();
252b5132 9802 }
c0ebe874 9803 macro_build (NULL, s2, MFHL_FMT, op[0]);
8fc2e39e 9804 break;
252b5132 9805
1abe91b1
MR
9806 case M_DLCA_AB:
9807 dbl = 1;
9808 case M_LCA_AB:
9809 call = 1;
9810 goto do_la;
252b5132
RH
9811 case M_DLA_AB:
9812 dbl = 1;
9813 case M_LA_AB:
1abe91b1 9814 do_la:
252b5132
RH
9815 /* Load the address of a symbol into a register. If breg is not
9816 zero, we then add a base register to it. */
9817
c0ebe874 9818 breg = op[2];
bad1aba3 9819 if (dbl && GPR_SIZE == 32)
3bec30a8
TS
9820 as_warn (_("dla used to load 32-bit register"));
9821
90ecf173 9822 if (!dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
9823 as_warn (_("la used to load 64-bit address"));
9824
f2ae14a1 9825 if (small_offset_p (0, align, 16))
0c11417f 9826 {
c0ebe874 9827 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", op[0], breg,
f2ae14a1 9828 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8fc2e39e 9829 break;
0c11417f
MR
9830 }
9831
c0ebe874 9832 if (mips_opts.at && (op[0] == breg))
afdbd6d0
CD
9833 {
9834 tempreg = AT;
9835 used_at = 1;
9836 }
9837 else
c0ebe874 9838 tempreg = op[0];
afdbd6d0 9839
252b5132
RH
9840 if (offset_expr.X_op != O_symbol
9841 && offset_expr.X_op != O_constant)
9842 {
1661c76c 9843 as_bad (_("expression too complex"));
252b5132
RH
9844 offset_expr.X_op = O_constant;
9845 }
9846
252b5132 9847 if (offset_expr.X_op == O_constant)
aed1a261 9848 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
9849 else if (mips_pic == NO_PIC)
9850 {
d6bc6245 9851 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 9852 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
9853 Otherwise we want
9854 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
9855 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
9856 If we have a constant, we need two instructions anyhow,
d6bc6245 9857 so we may as well always use the latter form.
76b3015f 9858
6caf9ef4
TS
9859 With 64bit address space and a usable $at we want
9860 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9861 lui $at,<sym> (BFD_RELOC_HI16_S)
9862 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
9863 daddiu $at,<sym> (BFD_RELOC_LO16)
9864 dsll32 $tempreg,0
9865 daddu $tempreg,$tempreg,$at
9866
9867 If $at is already in use, we use a path which is suboptimal
9868 on superscalar processors.
9869 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9870 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
9871 dsll $tempreg,16
9872 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
9873 dsll $tempreg,16
9874 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
9875
9876 For GP relative symbols in 64bit address space we can use
9877 the same sequence as in 32bit address space. */
aed1a261 9878 if (HAVE_64BIT_SYMBOLS)
252b5132 9879 {
6caf9ef4
TS
9880 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
9881 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
9882 {
9883 relax_start (offset_expr.X_add_symbol);
9884 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
9885 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
9886 relax_switch ();
9887 }
d6bc6245 9888
741fe287 9889 if (used_at == 0 && mips_opts.at)
98d3f06f 9890 {
df58fc94 9891 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 9892 tempreg, BFD_RELOC_MIPS_HIGHEST);
df58fc94 9893 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 9894 AT, BFD_RELOC_HI16_S);
67c0d1eb 9895 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 9896 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 9897 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 9898 AT, AT, BFD_RELOC_LO16);
df58fc94 9899 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 9900 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
9901 used_at = 1;
9902 }
9903 else
9904 {
df58fc94 9905 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 9906 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 9907 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 9908 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 9909 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 9910 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 9911 tempreg, tempreg, BFD_RELOC_HI16_S);
df58fc94 9912 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 9913 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 9914 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 9915 }
6caf9ef4
TS
9916
9917 if (mips_relax.sequence)
9918 relax_end ();
98d3f06f
KH
9919 }
9920 else
9921 {
9922 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 9923 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 9924 {
4d7206a2 9925 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
9926 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
9927 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 9928 relax_switch ();
98d3f06f 9929 }
6943caf0 9930 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
1661c76c 9931 as_bad (_("offset too large"));
67c0d1eb
RS
9932 macro_build_lui (&offset_expr, tempreg);
9933 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
9934 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
9935 if (mips_relax.sequence)
9936 relax_end ();
98d3f06f 9937 }
252b5132 9938 }
0a44bf69 9939 else if (!mips_big_got && !HAVE_NEWABI)
252b5132 9940 {
9117d219
NC
9941 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
9942
252b5132
RH
9943 /* If this is a reference to an external symbol, and there
9944 is no constant, we want
9945 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 9946 or for lca or if tempreg is PIC_CALL_REG
9117d219 9947 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
9948 For a local symbol, we want
9949 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9950 nop
9951 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
9952
9953 If we have a small constant, and this is a reference to
9954 an external symbol, we want
9955 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9956 nop
9957 addiu $tempreg,$tempreg,<constant>
9958 For a local symbol, we want the same instruction
9959 sequence, but we output a BFD_RELOC_LO16 reloc on the
9960 addiu instruction.
9961
9962 If we have a large constant, and this is a reference to
9963 an external symbol, we want
9964 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9965 lui $at,<hiconstant>
9966 addiu $at,$at,<loconstant>
9967 addu $tempreg,$tempreg,$at
9968 For a local symbol, we want the same instruction
9969 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 9970 addiu instruction.
ed6fb7bd
SC
9971 */
9972
4d7206a2 9973 if (offset_expr.X_add_number == 0)
252b5132 9974 {
0a44bf69
RS
9975 if (mips_pic == SVR4_PIC
9976 && breg == 0
9977 && (call || tempreg == PIC_CALL_REG))
4d7206a2
RS
9978 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
9979
9980 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
9981 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9982 lw_reloc_type, mips_gp_register);
4d7206a2 9983 if (breg != 0)
252b5132
RH
9984 {
9985 /* We're going to put in an addu instruction using
9986 tempreg, so we may as well insert the nop right
9987 now. */
269137b2 9988 load_delay_nop ();
252b5132 9989 }
4d7206a2 9990 relax_switch ();
67c0d1eb
RS
9991 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9992 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9993 load_delay_nop ();
67c0d1eb
RS
9994 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
9995 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 9996 relax_end ();
252b5132
RH
9997 /* FIXME: If breg == 0, and the next instruction uses
9998 $tempreg, then if this variant case is used an extra
9999 nop will be generated. */
10000 }
4d7206a2
RS
10001 else if (offset_expr.X_add_number >= -0x8000
10002 && offset_expr.X_add_number < 0x8000)
252b5132 10003 {
67c0d1eb 10004 load_got_offset (tempreg, &offset_expr);
269137b2 10005 load_delay_nop ();
67c0d1eb 10006 add_got_offset (tempreg, &offset_expr);
252b5132
RH
10007 }
10008 else
10009 {
4d7206a2
RS
10010 expr1.X_add_number = offset_expr.X_add_number;
10011 offset_expr.X_add_number =
43c0598f 10012 SEXT_16BIT (offset_expr.X_add_number);
67c0d1eb 10013 load_got_offset (tempreg, &offset_expr);
f6a22291 10014 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
10015 /* If we are going to add in a base register, and the
10016 target register and the base register are the same,
10017 then we are using AT as a temporary register. Since
10018 we want to load the constant into AT, we add our
10019 current AT (from the global offset table) and the
10020 register into the register now, and pretend we were
10021 not using a base register. */
c0ebe874 10022 if (breg == op[0])
252b5132 10023 {
269137b2 10024 load_delay_nop ();
67c0d1eb 10025 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 10026 op[0], AT, breg);
252b5132 10027 breg = 0;
c0ebe874 10028 tempreg = op[0];
252b5132 10029 }
f6a22291 10030 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
10031 used_at = 1;
10032 }
10033 }
0a44bf69 10034 else if (!mips_big_got && HAVE_NEWABI)
f5040a92 10035 {
67c0d1eb 10036 int add_breg_early = 0;
f5040a92
AO
10037
10038 /* If this is a reference to an external, and there is no
10039 constant, or local symbol (*), with or without a
10040 constant, we want
10041 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 10042 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
10043 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
10044
10045 If we have a small constant, and this is a reference to
10046 an external symbol, we want
10047 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
10048 addiu $tempreg,$tempreg,<constant>
10049
10050 If we have a large constant, and this is a reference to
10051 an external symbol, we want
10052 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
10053 lui $at,<hiconstant>
10054 addiu $at,$at,<loconstant>
10055 addu $tempreg,$tempreg,$at
10056
10057 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
10058 local symbols, even though it introduces an additional
10059 instruction. */
10060
f5040a92
AO
10061 if (offset_expr.X_add_number)
10062 {
4d7206a2 10063 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
10064 offset_expr.X_add_number = 0;
10065
4d7206a2 10066 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
10067 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10068 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
10069
10070 if (expr1.X_add_number >= -0x8000
10071 && expr1.X_add_number < 0x8000)
10072 {
67c0d1eb
RS
10073 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
10074 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 10075 }
ecd13cd3 10076 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 10077 {
c0ebe874
RS
10078 unsigned int dreg;
10079
f5040a92
AO
10080 /* If we are going to add in a base register, and the
10081 target register and the base register are the same,
10082 then we are using AT as a temporary register. Since
10083 we want to load the constant into AT, we add our
10084 current AT (from the global offset table) and the
10085 register into the register now, and pretend we were
10086 not using a base register. */
c0ebe874 10087 if (breg != op[0])
f5040a92
AO
10088 dreg = tempreg;
10089 else
10090 {
9c2799c2 10091 gas_assert (tempreg == AT);
67c0d1eb 10092 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
10093 op[0], AT, breg);
10094 dreg = op[0];
67c0d1eb 10095 add_breg_early = 1;
f5040a92
AO
10096 }
10097
f6a22291 10098 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 10099 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10100 dreg, dreg, AT);
f5040a92 10101
f5040a92
AO
10102 used_at = 1;
10103 }
10104 else
10105 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
10106
4d7206a2 10107 relax_switch ();
f5040a92
AO
10108 offset_expr.X_add_number = expr1.X_add_number;
10109
67c0d1eb
RS
10110 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10111 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
10112 if (add_breg_early)
f5040a92 10113 {
67c0d1eb 10114 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 10115 op[0], tempreg, breg);
f5040a92 10116 breg = 0;
c0ebe874 10117 tempreg = op[0];
f5040a92 10118 }
4d7206a2 10119 relax_end ();
f5040a92 10120 }
4d7206a2 10121 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 10122 {
4d7206a2 10123 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
10124 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10125 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 10126 relax_switch ();
67c0d1eb
RS
10127 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10128 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 10129 relax_end ();
f5040a92 10130 }
4d7206a2 10131 else
f5040a92 10132 {
67c0d1eb
RS
10133 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10134 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
10135 }
10136 }
0a44bf69 10137 else if (mips_big_got && !HAVE_NEWABI)
252b5132 10138 {
67c0d1eb 10139 int gpdelay;
9117d219
NC
10140 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
10141 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 10142 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
10143
10144 /* This is the large GOT case. If this is a reference to an
10145 external symbol, and there is no constant, we want
10146 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10147 addu $tempreg,$tempreg,$gp
10148 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 10149 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
10150 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
10151 addu $tempreg,$tempreg,$gp
10152 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
10153 For a local symbol, we want
10154 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10155 nop
10156 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10157
10158 If we have a small constant, and this is a reference to
10159 an external symbol, we want
10160 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10161 addu $tempreg,$tempreg,$gp
10162 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10163 nop
10164 addiu $tempreg,$tempreg,<constant>
10165 For a local symbol, we want
10166 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10167 nop
10168 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
10169
10170 If we have a large constant, and this is a reference to
10171 an external symbol, we want
10172 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10173 addu $tempreg,$tempreg,$gp
10174 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10175 lui $at,<hiconstant>
10176 addiu $at,$at,<loconstant>
10177 addu $tempreg,$tempreg,$at
10178 For a local symbol, we want
10179 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10180 lui $at,<hiconstant>
10181 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
10182 addu $tempreg,$tempreg,$at
f5040a92 10183 */
438c16b8 10184
252b5132
RH
10185 expr1.X_add_number = offset_expr.X_add_number;
10186 offset_expr.X_add_number = 0;
4d7206a2 10187 relax_start (offset_expr.X_add_symbol);
67c0d1eb 10188 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
10189 if (expr1.X_add_number == 0 && breg == 0
10190 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
10191 {
10192 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
10193 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
10194 }
df58fc94 10195 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 10196 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10197 tempreg, tempreg, mips_gp_register);
67c0d1eb 10198 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 10199 tempreg, lw_reloc_type, tempreg);
252b5132
RH
10200 if (expr1.X_add_number == 0)
10201 {
67c0d1eb 10202 if (breg != 0)
252b5132
RH
10203 {
10204 /* We're going to put in an addu instruction using
10205 tempreg, so we may as well insert the nop right
10206 now. */
269137b2 10207 load_delay_nop ();
252b5132 10208 }
252b5132
RH
10209 }
10210 else if (expr1.X_add_number >= -0x8000
10211 && expr1.X_add_number < 0x8000)
10212 {
269137b2 10213 load_delay_nop ();
67c0d1eb 10214 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 10215 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
10216 }
10217 else
10218 {
c0ebe874
RS
10219 unsigned int dreg;
10220
252b5132
RH
10221 /* If we are going to add in a base register, and the
10222 target register and the base register are the same,
10223 then we are using AT as a temporary register. Since
10224 we want to load the constant into AT, we add our
10225 current AT (from the global offset table) and the
10226 register into the register now, and pretend we were
10227 not using a base register. */
c0ebe874 10228 if (breg != op[0])
67c0d1eb 10229 dreg = tempreg;
252b5132
RH
10230 else
10231 {
9c2799c2 10232 gas_assert (tempreg == AT);
269137b2 10233 load_delay_nop ();
67c0d1eb 10234 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
10235 op[0], AT, breg);
10236 dreg = op[0];
252b5132
RH
10237 }
10238
f6a22291 10239 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 10240 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 10241
252b5132
RH
10242 used_at = 1;
10243 }
43c0598f 10244 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
4d7206a2 10245 relax_switch ();
252b5132 10246
67c0d1eb 10247 if (gpdelay)
252b5132
RH
10248 {
10249 /* This is needed because this instruction uses $gp, but
f5040a92 10250 the first instruction on the main stream does not. */
67c0d1eb 10251 macro_build (NULL, "nop", "");
252b5132 10252 }
ed6fb7bd 10253
67c0d1eb
RS
10254 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10255 local_reloc_type, mips_gp_register);
f5040a92 10256 if (expr1.X_add_number >= -0x8000
252b5132
RH
10257 && expr1.X_add_number < 0x8000)
10258 {
269137b2 10259 load_delay_nop ();
67c0d1eb
RS
10260 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10261 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 10262 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
10263 register, the external symbol case ended with a load,
10264 so if the symbol turns out to not be external, and
10265 the next instruction uses tempreg, an unnecessary nop
10266 will be inserted. */
252b5132
RH
10267 }
10268 else
10269 {
c0ebe874 10270 if (breg == op[0])
252b5132
RH
10271 {
10272 /* We must add in the base register now, as in the
f5040a92 10273 external symbol case. */
9c2799c2 10274 gas_assert (tempreg == AT);
269137b2 10275 load_delay_nop ();
67c0d1eb 10276 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
10277 op[0], AT, breg);
10278 tempreg = op[0];
252b5132 10279 /* We set breg to 0 because we have arranged to add
f5040a92 10280 it in in both cases. */
252b5132
RH
10281 breg = 0;
10282 }
10283
67c0d1eb
RS
10284 macro_build_lui (&expr1, AT);
10285 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 10286 AT, AT, BFD_RELOC_LO16);
67c0d1eb 10287 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10288 tempreg, tempreg, AT);
8fc2e39e 10289 used_at = 1;
252b5132 10290 }
4d7206a2 10291 relax_end ();
252b5132 10292 }
0a44bf69 10293 else if (mips_big_got && HAVE_NEWABI)
f5040a92 10294 {
f5040a92
AO
10295 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
10296 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 10297 int add_breg_early = 0;
f5040a92
AO
10298
10299 /* This is the large GOT case. If this is a reference to an
10300 external symbol, and there is no constant, we want
10301 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10302 add $tempreg,$tempreg,$gp
10303 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 10304 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
10305 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
10306 add $tempreg,$tempreg,$gp
10307 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
10308
10309 If we have a small constant, and this is a reference to
10310 an external symbol, we want
10311 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10312 add $tempreg,$tempreg,$gp
10313 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10314 addi $tempreg,$tempreg,<constant>
10315
10316 If we have a large constant, and this is a reference to
10317 an external symbol, we want
10318 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10319 addu $tempreg,$tempreg,$gp
10320 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10321 lui $at,<hiconstant>
10322 addi $at,$at,<loconstant>
10323 add $tempreg,$tempreg,$at
10324
10325 If we have NewABI, and we know it's a local symbol, we want
10326 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
10327 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
10328 otherwise we have to resort to GOT_HI16/GOT_LO16. */
10329
4d7206a2 10330 relax_start (offset_expr.X_add_symbol);
f5040a92 10331
4d7206a2 10332 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
10333 offset_expr.X_add_number = 0;
10334
1abe91b1
MR
10335 if (expr1.X_add_number == 0 && breg == 0
10336 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
10337 {
10338 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
10339 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
10340 }
df58fc94 10341 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 10342 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10343 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
10344 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10345 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
10346
10347 if (expr1.X_add_number == 0)
4d7206a2 10348 ;
f5040a92
AO
10349 else if (expr1.X_add_number >= -0x8000
10350 && expr1.X_add_number < 0x8000)
10351 {
67c0d1eb 10352 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 10353 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 10354 }
ecd13cd3 10355 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 10356 {
c0ebe874
RS
10357 unsigned int dreg;
10358
f5040a92
AO
10359 /* If we are going to add in a base register, and the
10360 target register and the base register are the same,
10361 then we are using AT as a temporary register. Since
10362 we want to load the constant into AT, we add our
10363 current AT (from the global offset table) and the
10364 register into the register now, and pretend we were
10365 not using a base register. */
c0ebe874 10366 if (breg != op[0])
f5040a92
AO
10367 dreg = tempreg;
10368 else
10369 {
9c2799c2 10370 gas_assert (tempreg == AT);
67c0d1eb 10371 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
10372 op[0], AT, breg);
10373 dreg = op[0];
67c0d1eb 10374 add_breg_early = 1;
f5040a92
AO
10375 }
10376
f6a22291 10377 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 10378 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 10379
f5040a92
AO
10380 used_at = 1;
10381 }
10382 else
10383 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
10384
4d7206a2 10385 relax_switch ();
f5040a92 10386 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
10387 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10388 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
10389 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
10390 tempreg, BFD_RELOC_MIPS_GOT_OFST);
10391 if (add_breg_early)
f5040a92 10392 {
67c0d1eb 10393 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 10394 op[0], tempreg, breg);
f5040a92 10395 breg = 0;
c0ebe874 10396 tempreg = op[0];
f5040a92 10397 }
4d7206a2 10398 relax_end ();
f5040a92 10399 }
252b5132
RH
10400 else
10401 abort ();
10402
10403 if (breg != 0)
c0ebe874 10404 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", op[0], tempreg, breg);
252b5132
RH
10405 break;
10406
52b6b6b9 10407 case M_MSGSND:
df58fc94 10408 gas_assert (!mips_opts.micromips);
c0ebe874 10409 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x01);
c7af4273 10410 break;
52b6b6b9
JM
10411
10412 case M_MSGLD:
df58fc94 10413 gas_assert (!mips_opts.micromips);
c8276761 10414 macro_build (NULL, "c2", "C", 0x02);
c7af4273 10415 break;
52b6b6b9
JM
10416
10417 case M_MSGLD_T:
df58fc94 10418 gas_assert (!mips_opts.micromips);
c0ebe874 10419 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x02);
c7af4273 10420 break;
52b6b6b9
JM
10421
10422 case M_MSGWAIT:
df58fc94 10423 gas_assert (!mips_opts.micromips);
52b6b6b9 10424 macro_build (NULL, "c2", "C", 3);
c7af4273 10425 break;
52b6b6b9
JM
10426
10427 case M_MSGWAIT_T:
df58fc94 10428 gas_assert (!mips_opts.micromips);
c0ebe874 10429 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x03);
c7af4273 10430 break;
52b6b6b9 10431
252b5132
RH
10432 case M_J_A:
10433 /* The j instruction may not be used in PIC code, since it
10434 requires an absolute address. We convert it to a b
10435 instruction. */
10436 if (mips_pic == NO_PIC)
67c0d1eb 10437 macro_build (&offset_expr, "j", "a");
252b5132 10438 else
67c0d1eb 10439 macro_build (&offset_expr, "b", "p");
8fc2e39e 10440 break;
252b5132
RH
10441
10442 /* The jal instructions must be handled as macros because when
10443 generating PIC code they expand to multi-instruction
10444 sequences. Normally they are simple instructions. */
df58fc94 10445 case M_JALS_1:
c0ebe874
RS
10446 op[1] = op[0];
10447 op[0] = RA;
df58fc94
RS
10448 /* Fall through. */
10449 case M_JALS_2:
10450 gas_assert (mips_opts.micromips);
833794fc
MR
10451 if (mips_opts.insn32)
10452 {
1661c76c 10453 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
833794fc
MR
10454 break;
10455 }
df58fc94
RS
10456 jals = 1;
10457 goto jal;
252b5132 10458 case M_JAL_1:
c0ebe874
RS
10459 op[1] = op[0];
10460 op[0] = RA;
252b5132
RH
10461 /* Fall through. */
10462 case M_JAL_2:
df58fc94 10463 jal:
3e722fb5 10464 if (mips_pic == NO_PIC)
df58fc94
RS
10465 {
10466 s = jals ? "jalrs" : "jalr";
e64af278 10467 if (mips_opts.micromips
833794fc 10468 && !mips_opts.insn32
c0ebe874 10469 && op[0] == RA
e64af278 10470 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 10471 macro_build (NULL, s, "mj", op[1]);
df58fc94 10472 else
c0ebe874 10473 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
df58fc94 10474 }
0a44bf69 10475 else
252b5132 10476 {
df58fc94
RS
10477 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
10478 && mips_cprestore_offset >= 0);
10479
c0ebe874 10480 if (op[1] != PIC_CALL_REG)
252b5132 10481 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 10482
833794fc
MR
10483 s = ((mips_opts.micromips
10484 && !mips_opts.insn32
10485 && (!mips_opts.noreorder || cprestore))
df58fc94 10486 ? "jalrs" : "jalr");
e64af278 10487 if (mips_opts.micromips
833794fc 10488 && !mips_opts.insn32
c0ebe874 10489 && op[0] == RA
e64af278 10490 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 10491 macro_build (NULL, s, "mj", op[1]);
df58fc94 10492 else
c0ebe874 10493 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
0a44bf69 10494 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
252b5132 10495 {
6478892d 10496 if (mips_cprestore_offset < 0)
1661c76c 10497 as_warn (_("no .cprestore pseudo-op used in PIC code"));
6478892d
TS
10498 else
10499 {
90ecf173 10500 if (!mips_frame_reg_valid)
7a621144 10501 {
1661c76c 10502 as_warn (_("no .frame pseudo-op used in PIC code"));
7a621144
DJ
10503 /* Quiet this warning. */
10504 mips_frame_reg_valid = 1;
10505 }
90ecf173 10506 if (!mips_cprestore_valid)
7a621144 10507 {
1661c76c 10508 as_warn (_("no .cprestore pseudo-op used in PIC code"));
7a621144
DJ
10509 /* Quiet this warning. */
10510 mips_cprestore_valid = 1;
10511 }
d3fca0b5
MR
10512 if (mips_opts.noreorder)
10513 macro_build (NULL, "nop", "");
6478892d 10514 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 10515 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 10516 mips_gp_register,
256ab948
TS
10517 mips_frame_reg,
10518 HAVE_64BIT_ADDRESSES);
6478892d 10519 }
252b5132
RH
10520 }
10521 }
252b5132 10522
8fc2e39e 10523 break;
252b5132 10524
df58fc94
RS
10525 case M_JALS_A:
10526 gas_assert (mips_opts.micromips);
833794fc
MR
10527 if (mips_opts.insn32)
10528 {
1661c76c 10529 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
833794fc
MR
10530 break;
10531 }
df58fc94
RS
10532 jals = 1;
10533 /* Fall through. */
252b5132
RH
10534 case M_JAL_A:
10535 if (mips_pic == NO_PIC)
df58fc94 10536 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
252b5132
RH
10537 else if (mips_pic == SVR4_PIC)
10538 {
10539 /* If this is a reference to an external symbol, and we are
10540 using a small GOT, we want
10541 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
10542 nop
f9419b05 10543 jalr $ra,$25
252b5132
RH
10544 nop
10545 lw $gp,cprestore($sp)
10546 The cprestore value is set using the .cprestore
10547 pseudo-op. If we are using a big GOT, we want
10548 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
10549 addu $25,$25,$gp
10550 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
10551 nop
f9419b05 10552 jalr $ra,$25
252b5132
RH
10553 nop
10554 lw $gp,cprestore($sp)
10555 If the symbol is not external, we want
10556 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10557 nop
10558 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 10559 jalr $ra,$25
252b5132 10560 nop
438c16b8 10561 lw $gp,cprestore($sp)
f5040a92
AO
10562
10563 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
10564 sequences above, minus nops, unless the symbol is local,
10565 which enables us to use GOT_PAGE/GOT_OFST (big got) or
10566 GOT_DISP. */
438c16b8 10567 if (HAVE_NEWABI)
252b5132 10568 {
90ecf173 10569 if (!mips_big_got)
f5040a92 10570 {
4d7206a2 10571 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
10572 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10573 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 10574 mips_gp_register);
4d7206a2 10575 relax_switch ();
67c0d1eb
RS
10576 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10577 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
10578 mips_gp_register);
10579 relax_end ();
f5040a92
AO
10580 }
10581 else
10582 {
4d7206a2 10583 relax_start (offset_expr.X_add_symbol);
df58fc94 10584 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
10585 BFD_RELOC_MIPS_CALL_HI16);
10586 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
10587 PIC_CALL_REG, mips_gp_register);
10588 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10589 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
10590 PIC_CALL_REG);
4d7206a2 10591 relax_switch ();
67c0d1eb
RS
10592 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10593 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
10594 mips_gp_register);
10595 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10596 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 10597 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 10598 relax_end ();
f5040a92 10599 }
684022ea 10600
df58fc94 10601 macro_build_jalr (&offset_expr, 0);
252b5132
RH
10602 }
10603 else
10604 {
4d7206a2 10605 relax_start (offset_expr.X_add_symbol);
90ecf173 10606 if (!mips_big_got)
438c16b8 10607 {
67c0d1eb
RS
10608 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10609 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 10610 mips_gp_register);
269137b2 10611 load_delay_nop ();
4d7206a2 10612 relax_switch ();
438c16b8 10613 }
252b5132 10614 else
252b5132 10615 {
67c0d1eb
RS
10616 int gpdelay;
10617
10618 gpdelay = reg_needs_delay (mips_gp_register);
df58fc94 10619 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
10620 BFD_RELOC_MIPS_CALL_HI16);
10621 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
10622 PIC_CALL_REG, mips_gp_register);
10623 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10624 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
10625 PIC_CALL_REG);
269137b2 10626 load_delay_nop ();
4d7206a2 10627 relax_switch ();
67c0d1eb
RS
10628 if (gpdelay)
10629 macro_build (NULL, "nop", "");
252b5132 10630 }
67c0d1eb
RS
10631 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10632 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 10633 mips_gp_register);
269137b2 10634 load_delay_nop ();
67c0d1eb
RS
10635 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10636 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 10637 relax_end ();
df58fc94 10638 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
438c16b8 10639
6478892d 10640 if (mips_cprestore_offset < 0)
1661c76c 10641 as_warn (_("no .cprestore pseudo-op used in PIC code"));
6478892d
TS
10642 else
10643 {
90ecf173 10644 if (!mips_frame_reg_valid)
7a621144 10645 {
1661c76c 10646 as_warn (_("no .frame pseudo-op used in PIC code"));
7a621144
DJ
10647 /* Quiet this warning. */
10648 mips_frame_reg_valid = 1;
10649 }
90ecf173 10650 if (!mips_cprestore_valid)
7a621144 10651 {
1661c76c 10652 as_warn (_("no .cprestore pseudo-op used in PIC code"));
7a621144
DJ
10653 /* Quiet this warning. */
10654 mips_cprestore_valid = 1;
10655 }
6478892d 10656 if (mips_opts.noreorder)
67c0d1eb 10657 macro_build (NULL, "nop", "");
6478892d 10658 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 10659 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 10660 mips_gp_register,
256ab948
TS
10661 mips_frame_reg,
10662 HAVE_64BIT_ADDRESSES);
6478892d 10663 }
252b5132
RH
10664 }
10665 }
0a44bf69 10666 else if (mips_pic == VXWORKS_PIC)
1661c76c 10667 as_bad (_("non-PIC jump used in PIC library"));
252b5132
RH
10668 else
10669 abort ();
10670
8fc2e39e 10671 break;
252b5132 10672
7f3c4072 10673 case M_LBUE_AB:
7f3c4072
CM
10674 s = "lbue";
10675 fmt = "t,+j(b)";
10676 offbits = 9;
10677 goto ld_st;
10678 case M_LHUE_AB:
7f3c4072
CM
10679 s = "lhue";
10680 fmt = "t,+j(b)";
10681 offbits = 9;
10682 goto ld_st;
10683 case M_LBE_AB:
7f3c4072
CM
10684 s = "lbe";
10685 fmt = "t,+j(b)";
10686 offbits = 9;
10687 goto ld_st;
10688 case M_LHE_AB:
7f3c4072
CM
10689 s = "lhe";
10690 fmt = "t,+j(b)";
10691 offbits = 9;
10692 goto ld_st;
10693 case M_LLE_AB:
7f3c4072
CM
10694 s = "lle";
10695 fmt = "t,+j(b)";
10696 offbits = 9;
10697 goto ld_st;
10698 case M_LWE_AB:
7f3c4072
CM
10699 s = "lwe";
10700 fmt = "t,+j(b)";
10701 offbits = 9;
10702 goto ld_st;
10703 case M_LWLE_AB:
7f3c4072
CM
10704 s = "lwle";
10705 fmt = "t,+j(b)";
10706 offbits = 9;
10707 goto ld_st;
10708 case M_LWRE_AB:
7f3c4072
CM
10709 s = "lwre";
10710 fmt = "t,+j(b)";
10711 offbits = 9;
10712 goto ld_st;
10713 case M_SBE_AB:
7f3c4072
CM
10714 s = "sbe";
10715 fmt = "t,+j(b)";
10716 offbits = 9;
10717 goto ld_st;
10718 case M_SCE_AB:
7f3c4072
CM
10719 s = "sce";
10720 fmt = "t,+j(b)";
10721 offbits = 9;
10722 goto ld_st;
10723 case M_SHE_AB:
7f3c4072
CM
10724 s = "she";
10725 fmt = "t,+j(b)";
10726 offbits = 9;
10727 goto ld_st;
10728 case M_SWE_AB:
7f3c4072
CM
10729 s = "swe";
10730 fmt = "t,+j(b)";
10731 offbits = 9;
10732 goto ld_st;
10733 case M_SWLE_AB:
7f3c4072
CM
10734 s = "swle";
10735 fmt = "t,+j(b)";
10736 offbits = 9;
10737 goto ld_st;
10738 case M_SWRE_AB:
7f3c4072
CM
10739 s = "swre";
10740 fmt = "t,+j(b)";
10741 offbits = 9;
10742 goto ld_st;
dec0624d 10743 case M_ACLR_AB:
dec0624d 10744 s = "aclr";
dec0624d 10745 fmt = "\\,~(b)";
7f3c4072 10746 offbits = 12;
dec0624d
MR
10747 goto ld_st;
10748 case M_ASET_AB:
dec0624d 10749 s = "aset";
dec0624d 10750 fmt = "\\,~(b)";
7f3c4072 10751 offbits = 12;
dec0624d 10752 goto ld_st;
252b5132
RH
10753 case M_LB_AB:
10754 s = "lb";
df58fc94 10755 fmt = "t,o(b)";
252b5132
RH
10756 goto ld;
10757 case M_LBU_AB:
10758 s = "lbu";
df58fc94 10759 fmt = "t,o(b)";
252b5132
RH
10760 goto ld;
10761 case M_LH_AB:
10762 s = "lh";
df58fc94 10763 fmt = "t,o(b)";
252b5132
RH
10764 goto ld;
10765 case M_LHU_AB:
10766 s = "lhu";
df58fc94 10767 fmt = "t,o(b)";
252b5132
RH
10768 goto ld;
10769 case M_LW_AB:
10770 s = "lw";
df58fc94 10771 fmt = "t,o(b)";
252b5132
RH
10772 goto ld;
10773 case M_LWC0_AB:
df58fc94 10774 gas_assert (!mips_opts.micromips);
252b5132 10775 s = "lwc0";
df58fc94 10776 fmt = "E,o(b)";
bdaaa2e1 10777 /* Itbl support may require additional care here. */
252b5132 10778 coproc = 1;
df58fc94 10779 goto ld_st;
252b5132
RH
10780 case M_LWC1_AB:
10781 s = "lwc1";
df58fc94 10782 fmt = "T,o(b)";
bdaaa2e1 10783 /* Itbl support may require additional care here. */
252b5132 10784 coproc = 1;
df58fc94 10785 goto ld_st;
252b5132
RH
10786 case M_LWC2_AB:
10787 s = "lwc2";
df58fc94 10788 fmt = COP12_FMT;
7f3c4072 10789 offbits = (mips_opts.micromips ? 12 : 16);
bdaaa2e1 10790 /* Itbl support may require additional care here. */
252b5132 10791 coproc = 1;
df58fc94 10792 goto ld_st;
252b5132 10793 case M_LWC3_AB:
df58fc94 10794 gas_assert (!mips_opts.micromips);
252b5132 10795 s = "lwc3";
df58fc94 10796 fmt = "E,o(b)";
bdaaa2e1 10797 /* Itbl support may require additional care here. */
252b5132 10798 coproc = 1;
df58fc94 10799 goto ld_st;
252b5132
RH
10800 case M_LWL_AB:
10801 s = "lwl";
df58fc94 10802 fmt = MEM12_FMT;
7f3c4072 10803 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10804 goto ld_st;
252b5132
RH
10805 case M_LWR_AB:
10806 s = "lwr";
df58fc94 10807 fmt = MEM12_FMT;
7f3c4072 10808 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10809 goto ld_st;
252b5132 10810 case M_LDC1_AB:
252b5132 10811 s = "ldc1";
df58fc94 10812 fmt = "T,o(b)";
bdaaa2e1 10813 /* Itbl support may require additional care here. */
252b5132 10814 coproc = 1;
df58fc94 10815 goto ld_st;
252b5132
RH
10816 case M_LDC2_AB:
10817 s = "ldc2";
df58fc94 10818 fmt = COP12_FMT;
7f3c4072 10819 offbits = (mips_opts.micromips ? 12 : 16);
bdaaa2e1 10820 /* Itbl support may require additional care here. */
252b5132 10821 coproc = 1;
df58fc94 10822 goto ld_st;
c77c0862 10823 case M_LQC2_AB:
c77c0862 10824 s = "lqc2";
14daeee3 10825 fmt = "+7,o(b)";
c77c0862
RS
10826 /* Itbl support may require additional care here. */
10827 coproc = 1;
10828 goto ld_st;
252b5132
RH
10829 case M_LDC3_AB:
10830 s = "ldc3";
df58fc94 10831 fmt = "E,o(b)";
bdaaa2e1 10832 /* Itbl support may require additional care here. */
252b5132 10833 coproc = 1;
df58fc94 10834 goto ld_st;
252b5132
RH
10835 case M_LDL_AB:
10836 s = "ldl";
df58fc94 10837 fmt = MEM12_FMT;
7f3c4072 10838 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10839 goto ld_st;
252b5132
RH
10840 case M_LDR_AB:
10841 s = "ldr";
df58fc94 10842 fmt = MEM12_FMT;
7f3c4072 10843 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10844 goto ld_st;
252b5132
RH
10845 case M_LL_AB:
10846 s = "ll";
df58fc94 10847 fmt = MEM12_FMT;
7f3c4072 10848 offbits = (mips_opts.micromips ? 12 : 16);
252b5132
RH
10849 goto ld;
10850 case M_LLD_AB:
10851 s = "lld";
df58fc94 10852 fmt = MEM12_FMT;
7f3c4072 10853 offbits = (mips_opts.micromips ? 12 : 16);
252b5132
RH
10854 goto ld;
10855 case M_LWU_AB:
10856 s = "lwu";
df58fc94 10857 fmt = MEM12_FMT;
7f3c4072 10858 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
10859 goto ld;
10860 case M_LWP_AB:
df58fc94
RS
10861 gas_assert (mips_opts.micromips);
10862 s = "lwp";
10863 fmt = "t,~(b)";
7f3c4072 10864 offbits = 12;
df58fc94
RS
10865 lp = 1;
10866 goto ld;
10867 case M_LDP_AB:
df58fc94
RS
10868 gas_assert (mips_opts.micromips);
10869 s = "ldp";
10870 fmt = "t,~(b)";
7f3c4072 10871 offbits = 12;
df58fc94
RS
10872 lp = 1;
10873 goto ld;
10874 case M_LWM_AB:
df58fc94
RS
10875 gas_assert (mips_opts.micromips);
10876 s = "lwm";
10877 fmt = "n,~(b)";
7f3c4072 10878 offbits = 12;
df58fc94
RS
10879 goto ld_st;
10880 case M_LDM_AB:
df58fc94
RS
10881 gas_assert (mips_opts.micromips);
10882 s = "ldm";
10883 fmt = "n,~(b)";
7f3c4072 10884 offbits = 12;
df58fc94
RS
10885 goto ld_st;
10886
252b5132 10887 ld:
f19ccbda 10888 /* We don't want to use $0 as tempreg. */
c0ebe874 10889 if (op[2] == op[0] + lp || op[0] + lp == ZERO)
df58fc94 10890 goto ld_st;
252b5132 10891 else
c0ebe874 10892 tempreg = op[0] + lp;
df58fc94
RS
10893 goto ld_noat;
10894
252b5132
RH
10895 case M_SB_AB:
10896 s = "sb";
df58fc94
RS
10897 fmt = "t,o(b)";
10898 goto ld_st;
252b5132
RH
10899 case M_SH_AB:
10900 s = "sh";
df58fc94
RS
10901 fmt = "t,o(b)";
10902 goto ld_st;
252b5132
RH
10903 case M_SW_AB:
10904 s = "sw";
df58fc94
RS
10905 fmt = "t,o(b)";
10906 goto ld_st;
252b5132 10907 case M_SWC0_AB:
df58fc94 10908 gas_assert (!mips_opts.micromips);
252b5132 10909 s = "swc0";
df58fc94 10910 fmt = "E,o(b)";
bdaaa2e1 10911 /* Itbl support may require additional care here. */
252b5132 10912 coproc = 1;
df58fc94 10913 goto ld_st;
252b5132
RH
10914 case M_SWC1_AB:
10915 s = "swc1";
df58fc94 10916 fmt = "T,o(b)";
bdaaa2e1 10917 /* Itbl support may require additional care here. */
252b5132 10918 coproc = 1;
df58fc94 10919 goto ld_st;
252b5132
RH
10920 case M_SWC2_AB:
10921 s = "swc2";
df58fc94 10922 fmt = COP12_FMT;
7f3c4072 10923 offbits = (mips_opts.micromips ? 12 : 16);
bdaaa2e1 10924 /* Itbl support may require additional care here. */
252b5132 10925 coproc = 1;
df58fc94 10926 goto ld_st;
252b5132 10927 case M_SWC3_AB:
df58fc94 10928 gas_assert (!mips_opts.micromips);
252b5132 10929 s = "swc3";
df58fc94 10930 fmt = "E,o(b)";
bdaaa2e1 10931 /* Itbl support may require additional care here. */
252b5132 10932 coproc = 1;
df58fc94 10933 goto ld_st;
252b5132
RH
10934 case M_SWL_AB:
10935 s = "swl";
df58fc94 10936 fmt = MEM12_FMT;
7f3c4072 10937 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10938 goto ld_st;
252b5132
RH
10939 case M_SWR_AB:
10940 s = "swr";
df58fc94 10941 fmt = MEM12_FMT;
7f3c4072 10942 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10943 goto ld_st;
252b5132
RH
10944 case M_SC_AB:
10945 s = "sc";
df58fc94 10946 fmt = MEM12_FMT;
7f3c4072 10947 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10948 goto ld_st;
252b5132
RH
10949 case M_SCD_AB:
10950 s = "scd";
df58fc94 10951 fmt = MEM12_FMT;
7f3c4072 10952 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10953 goto ld_st;
d43b4baf
TS
10954 case M_CACHE_AB:
10955 s = "cache";
df58fc94 10956 fmt = mips_opts.micromips ? "k,~(b)" : "k,o(b)";
7f3c4072
CM
10957 offbits = (mips_opts.micromips ? 12 : 16);
10958 goto ld_st;
10959 case M_CACHEE_AB:
7f3c4072
CM
10960 s = "cachee";
10961 fmt = "k,+j(b)";
10962 offbits = 9;
df58fc94 10963 goto ld_st;
3eebd5eb
MR
10964 case M_PREF_AB:
10965 s = "pref";
df58fc94 10966 fmt = !mips_opts.micromips ? "k,o(b)" : "k,~(b)";
7f3c4072
CM
10967 offbits = (mips_opts.micromips ? 12 : 16);
10968 goto ld_st;
10969 case M_PREFE_AB:
7f3c4072
CM
10970 s = "prefe";
10971 fmt = "k,+j(b)";
10972 offbits = 9;
df58fc94 10973 goto ld_st;
252b5132 10974 case M_SDC1_AB:
252b5132 10975 s = "sdc1";
df58fc94 10976 fmt = "T,o(b)";
252b5132 10977 coproc = 1;
bdaaa2e1 10978 /* Itbl support may require additional care here. */
df58fc94 10979 goto ld_st;
252b5132
RH
10980 case M_SDC2_AB:
10981 s = "sdc2";
df58fc94 10982 fmt = COP12_FMT;
7f3c4072 10983 offbits = (mips_opts.micromips ? 12 : 16);
c77c0862
RS
10984 /* Itbl support may require additional care here. */
10985 coproc = 1;
10986 goto ld_st;
10987 case M_SQC2_AB:
c77c0862 10988 s = "sqc2";
14daeee3 10989 fmt = "+7,o(b)";
bdaaa2e1 10990 /* Itbl support may require additional care here. */
252b5132 10991 coproc = 1;
df58fc94 10992 goto ld_st;
252b5132 10993 case M_SDC3_AB:
df58fc94 10994 gas_assert (!mips_opts.micromips);
252b5132 10995 s = "sdc3";
df58fc94 10996 fmt = "E,o(b)";
bdaaa2e1 10997 /* Itbl support may require additional care here. */
252b5132 10998 coproc = 1;
df58fc94 10999 goto ld_st;
252b5132
RH
11000 case M_SDL_AB:
11001 s = "sdl";
df58fc94 11002 fmt = MEM12_FMT;
7f3c4072 11003 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11004 goto ld_st;
252b5132
RH
11005 case M_SDR_AB:
11006 s = "sdr";
df58fc94 11007 fmt = MEM12_FMT;
7f3c4072 11008 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
11009 goto ld_st;
11010 case M_SWP_AB:
df58fc94
RS
11011 gas_assert (mips_opts.micromips);
11012 s = "swp";
11013 fmt = "t,~(b)";
7f3c4072 11014 offbits = 12;
df58fc94
RS
11015 goto ld_st;
11016 case M_SDP_AB:
df58fc94
RS
11017 gas_assert (mips_opts.micromips);
11018 s = "sdp";
11019 fmt = "t,~(b)";
7f3c4072 11020 offbits = 12;
df58fc94
RS
11021 goto ld_st;
11022 case M_SWM_AB:
df58fc94
RS
11023 gas_assert (mips_opts.micromips);
11024 s = "swm";
11025 fmt = "n,~(b)";
7f3c4072 11026 offbits = 12;
df58fc94
RS
11027 goto ld_st;
11028 case M_SDM_AB:
df58fc94
RS
11029 gas_assert (mips_opts.micromips);
11030 s = "sdm";
11031 fmt = "n,~(b)";
7f3c4072 11032 offbits = 12;
df58fc94
RS
11033
11034 ld_st:
8fc2e39e 11035 tempreg = AT;
df58fc94 11036 ld_noat:
c0ebe874 11037 breg = op[2];
f2ae14a1
RS
11038 if (small_offset_p (0, align, 16))
11039 {
11040 /* The first case exists for M_LD_AB and M_SD_AB, which are
11041 macros for o32 but which should act like normal instructions
11042 otherwise. */
11043 if (offbits == 16)
c0ebe874 11044 macro_build (&offset_expr, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
11045 offset_reloc[1], offset_reloc[2], breg);
11046 else if (small_offset_p (0, align, offbits))
11047 {
11048 if (offbits == 0)
c0ebe874 11049 macro_build (NULL, s, fmt, op[0], breg);
f2ae14a1 11050 else
c0ebe874 11051 macro_build (NULL, s, fmt, op[0],
c8276761 11052 (int) offset_expr.X_add_number, breg);
f2ae14a1
RS
11053 }
11054 else
11055 {
11056 if (tempreg == AT)
11057 used_at = 1;
11058 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11059 tempreg, breg, -1, offset_reloc[0],
11060 offset_reloc[1], offset_reloc[2]);
11061 if (offbits == 0)
c0ebe874 11062 macro_build (NULL, s, fmt, op[0], tempreg);
f2ae14a1 11063 else
c0ebe874 11064 macro_build (NULL, s, fmt, op[0], 0, tempreg);
f2ae14a1
RS
11065 }
11066 break;
11067 }
11068
11069 if (tempreg == AT)
11070 used_at = 1;
11071
252b5132
RH
11072 if (offset_expr.X_op != O_constant
11073 && offset_expr.X_op != O_symbol)
11074 {
1661c76c 11075 as_bad (_("expression too complex"));
252b5132
RH
11076 offset_expr.X_op = O_constant;
11077 }
11078
2051e8c4
MR
11079 if (HAVE_32BIT_ADDRESSES
11080 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
11081 {
11082 char value [32];
11083
11084 sprintf_vma (value, offset_expr.X_add_number);
1661c76c 11085 as_bad (_("number (0x%s) larger than 32 bits"), value);
55e08f71 11086 }
2051e8c4 11087
252b5132
RH
11088 /* A constant expression in PIC code can be handled just as it
11089 is in non PIC code. */
aed1a261
RS
11090 if (offset_expr.X_op == O_constant)
11091 {
f2ae14a1
RS
11092 expr1.X_add_number = offset_high_part (offset_expr.X_add_number,
11093 offbits == 0 ? 16 : offbits);
11094 offset_expr.X_add_number -= expr1.X_add_number;
df58fc94 11095
f2ae14a1
RS
11096 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
11097 if (breg != 0)
11098 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11099 tempreg, tempreg, breg);
7f3c4072 11100 if (offbits == 0)
dd6a37e7 11101 {
f2ae14a1 11102 if (offset_expr.X_add_number != 0)
dd6a37e7 11103 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
f2ae14a1 11104 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
c0ebe874 11105 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 11106 }
7f3c4072 11107 else if (offbits == 16)
c0ebe874 11108 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
df58fc94 11109 else
c0ebe874 11110 macro_build (NULL, s, fmt, op[0],
c8276761 11111 (int) offset_expr.X_add_number, tempreg);
df58fc94 11112 }
7f3c4072 11113 else if (offbits != 16)
df58fc94 11114 {
7f3c4072
CM
11115 /* The offset field is too narrow to be used for a low-part
11116 relocation, so load the whole address into the auxillary
f2ae14a1
RS
11117 register. */
11118 load_address (tempreg, &offset_expr, &used_at);
11119 if (breg != 0)
11120 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11121 tempreg, tempreg, breg);
7f3c4072 11122 if (offbits == 0)
c0ebe874 11123 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 11124 else
c0ebe874 11125 macro_build (NULL, s, fmt, op[0], 0, tempreg);
aed1a261
RS
11126 }
11127 else if (mips_pic == NO_PIC)
252b5132
RH
11128 {
11129 /* If this is a reference to a GP relative symbol, and there
11130 is no base register, we want
c0ebe874 11131 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
11132 Otherwise, if there is no base register, we want
11133 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
c0ebe874 11134 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
252b5132
RH
11135 If we have a constant, we need two instructions anyhow,
11136 so we always use the latter form.
11137
11138 If we have a base register, and this is a reference to a
11139 GP relative symbol, we want
11140 addu $tempreg,$breg,$gp
c0ebe874 11141 <op> op[0],<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
11142 Otherwise we want
11143 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
11144 addu $tempreg,$tempreg,$breg
c0ebe874 11145 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 11146 With a constant we always use the latter case.
76b3015f 11147
d6bc6245
TS
11148 With 64bit address space and no base register and $at usable,
11149 we want
11150 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11151 lui $at,<sym> (BFD_RELOC_HI16_S)
11152 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11153 dsll32 $tempreg,0
11154 daddu $tempreg,$at
c0ebe874 11155 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
11156 If we have a base register, we want
11157 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11158 lui $at,<sym> (BFD_RELOC_HI16_S)
11159 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11160 daddu $at,$breg
11161 dsll32 $tempreg,0
11162 daddu $tempreg,$at
c0ebe874 11163 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
11164
11165 Without $at we can't generate the optimal path for superscalar
11166 processors here since this would require two temporary registers.
11167 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11168 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11169 dsll $tempreg,16
11170 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
11171 dsll $tempreg,16
c0ebe874 11172 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
11173 If we have a base register, we want
11174 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11175 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11176 dsll $tempreg,16
11177 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
11178 dsll $tempreg,16
11179 daddu $tempreg,$tempreg,$breg
c0ebe874 11180 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 11181
6caf9ef4 11182 For GP relative symbols in 64bit address space we can use
aed1a261
RS
11183 the same sequence as in 32bit address space. */
11184 if (HAVE_64BIT_SYMBOLS)
d6bc6245 11185 {
aed1a261 11186 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
11187 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
11188 {
11189 relax_start (offset_expr.X_add_symbol);
11190 if (breg == 0)
11191 {
c0ebe874 11192 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
11193 BFD_RELOC_GPREL16, mips_gp_register);
11194 }
11195 else
11196 {
11197 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11198 tempreg, breg, mips_gp_register);
c0ebe874 11199 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
11200 BFD_RELOC_GPREL16, tempreg);
11201 }
11202 relax_switch ();
11203 }
d6bc6245 11204
741fe287 11205 if (used_at == 0 && mips_opts.at)
d6bc6245 11206 {
df58fc94 11207 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb 11208 BFD_RELOC_MIPS_HIGHEST);
df58fc94 11209 macro_build (&offset_expr, "lui", LUI_FMT, AT,
67c0d1eb
RS
11210 BFD_RELOC_HI16_S);
11211 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
11212 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 11213 if (breg != 0)
67c0d1eb 11214 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
df58fc94 11215 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 11216 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
c0ebe874 11217 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16,
67c0d1eb 11218 tempreg);
d6bc6245
TS
11219 used_at = 1;
11220 }
11221 else
11222 {
df58fc94 11223 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb
RS
11224 BFD_RELOC_MIPS_HIGHEST);
11225 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
11226 tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 11227 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb
RS
11228 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
11229 tempreg, BFD_RELOC_HI16_S);
df58fc94 11230 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
d6bc6245 11231 if (breg != 0)
67c0d1eb 11232 macro_build (NULL, "daddu", "d,v,t",
17a2f251 11233 tempreg, tempreg, breg);
c0ebe874 11234 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 11235 BFD_RELOC_LO16, tempreg);
d6bc6245 11236 }
6caf9ef4
TS
11237
11238 if (mips_relax.sequence)
11239 relax_end ();
8fc2e39e 11240 break;
d6bc6245 11241 }
256ab948 11242
252b5132
RH
11243 if (breg == 0)
11244 {
67c0d1eb 11245 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 11246 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 11247 {
4d7206a2 11248 relax_start (offset_expr.X_add_symbol);
c0ebe874 11249 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_GPREL16,
67c0d1eb 11250 mips_gp_register);
4d7206a2 11251 relax_switch ();
252b5132 11252 }
67c0d1eb 11253 macro_build_lui (&offset_expr, tempreg);
c0ebe874 11254 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 11255 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
11256 if (mips_relax.sequence)
11257 relax_end ();
252b5132
RH
11258 }
11259 else
11260 {
67c0d1eb 11261 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 11262 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 11263 {
4d7206a2 11264 relax_start (offset_expr.X_add_symbol);
67c0d1eb 11265 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11266 tempreg, breg, mips_gp_register);
c0ebe874 11267 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 11268 BFD_RELOC_GPREL16, tempreg);
4d7206a2 11269 relax_switch ();
252b5132 11270 }
67c0d1eb
RS
11271 macro_build_lui (&offset_expr, tempreg);
11272 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11273 tempreg, tempreg, breg);
c0ebe874 11274 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 11275 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
11276 if (mips_relax.sequence)
11277 relax_end ();
252b5132
RH
11278 }
11279 }
0a44bf69 11280 else if (!mips_big_got)
252b5132 11281 {
ed6fb7bd 11282 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 11283
252b5132
RH
11284 /* If this is a reference to an external symbol, we want
11285 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11286 nop
c0ebe874 11287 <op> op[0],0($tempreg)
252b5132
RH
11288 Otherwise we want
11289 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11290 nop
11291 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 11292 <op> op[0],0($tempreg)
f5040a92
AO
11293
11294 For NewABI, we want
11295 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 11296 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 11297
252b5132
RH
11298 If there is a base register, we add it to $tempreg before
11299 the <op>. If there is a constant, we stick it in the
11300 <op> instruction. We don't handle constants larger than
11301 16 bits, because we have no way to load the upper 16 bits
11302 (actually, we could handle them for the subset of cases
11303 in which we are not using $at). */
9c2799c2 11304 gas_assert (offset_expr.X_op == O_symbol);
f5040a92
AO
11305 if (HAVE_NEWABI)
11306 {
67c0d1eb
RS
11307 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11308 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 11309 if (breg != 0)
67c0d1eb 11310 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11311 tempreg, tempreg, breg);
c0ebe874 11312 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 11313 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
11314 break;
11315 }
252b5132
RH
11316 expr1.X_add_number = offset_expr.X_add_number;
11317 offset_expr.X_add_number = 0;
11318 if (expr1.X_add_number < -0x8000
11319 || expr1.X_add_number >= 0x8000)
11320 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
11321 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11322 lw_reloc_type, mips_gp_register);
269137b2 11323 load_delay_nop ();
4d7206a2
RS
11324 relax_start (offset_expr.X_add_symbol);
11325 relax_switch ();
67c0d1eb
RS
11326 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11327 tempreg, BFD_RELOC_LO16);
4d7206a2 11328 relax_end ();
252b5132 11329 if (breg != 0)
67c0d1eb 11330 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11331 tempreg, tempreg, breg);
c0ebe874 11332 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 11333 }
0a44bf69 11334 else if (mips_big_got && !HAVE_NEWABI)
252b5132 11335 {
67c0d1eb 11336 int gpdelay;
252b5132
RH
11337
11338 /* If this is a reference to an external symbol, we want
11339 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11340 addu $tempreg,$tempreg,$gp
11341 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 11342 <op> op[0],0($tempreg)
252b5132
RH
11343 Otherwise we want
11344 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11345 nop
11346 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 11347 <op> op[0],0($tempreg)
252b5132
RH
11348 If there is a base register, we add it to $tempreg before
11349 the <op>. If there is a constant, we stick it in the
11350 <op> instruction. We don't handle constants larger than
11351 16 bits, because we have no way to load the upper 16 bits
11352 (actually, we could handle them for the subset of cases
f5040a92 11353 in which we are not using $at). */
9c2799c2 11354 gas_assert (offset_expr.X_op == O_symbol);
252b5132
RH
11355 expr1.X_add_number = offset_expr.X_add_number;
11356 offset_expr.X_add_number = 0;
11357 if (expr1.X_add_number < -0x8000
11358 || expr1.X_add_number >= 0x8000)
11359 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 11360 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 11361 relax_start (offset_expr.X_add_symbol);
df58fc94 11362 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 11363 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
11364 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
11365 mips_gp_register);
11366 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11367 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 11368 relax_switch ();
67c0d1eb
RS
11369 if (gpdelay)
11370 macro_build (NULL, "nop", "");
11371 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11372 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 11373 load_delay_nop ();
67c0d1eb
RS
11374 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11375 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
11376 relax_end ();
11377
252b5132 11378 if (breg != 0)
67c0d1eb 11379 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11380 tempreg, tempreg, breg);
c0ebe874 11381 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 11382 }
0a44bf69 11383 else if (mips_big_got && HAVE_NEWABI)
f5040a92 11384 {
f5040a92
AO
11385 /* If this is a reference to an external symbol, we want
11386 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11387 add $tempreg,$tempreg,$gp
11388 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 11389 <op> op[0],<ofst>($tempreg)
f5040a92
AO
11390 Otherwise, for local symbols, we want:
11391 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 11392 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
9c2799c2 11393 gas_assert (offset_expr.X_op == O_symbol);
4d7206a2 11394 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
11395 offset_expr.X_add_number = 0;
11396 if (expr1.X_add_number < -0x8000
11397 || expr1.X_add_number >= 0x8000)
11398 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 11399 relax_start (offset_expr.X_add_symbol);
df58fc94 11400 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 11401 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
11402 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
11403 mips_gp_register);
11404 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11405 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 11406 if (breg != 0)
67c0d1eb 11407 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11408 tempreg, tempreg, breg);
c0ebe874 11409 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
684022ea 11410
4d7206a2 11411 relax_switch ();
f5040a92 11412 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
11413 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11414 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 11415 if (breg != 0)
67c0d1eb 11416 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11417 tempreg, tempreg, breg);
c0ebe874 11418 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 11419 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 11420 relax_end ();
f5040a92 11421 }
252b5132
RH
11422 else
11423 abort ();
11424
252b5132
RH
11425 break;
11426
833794fc
MR
11427 case M_JRADDIUSP:
11428 gas_assert (mips_opts.micromips);
11429 gas_assert (mips_opts.insn32);
11430 start_noreorder ();
11431 macro_build (NULL, "jr", "s", RA);
c0ebe874 11432 expr1.X_add_number = op[0] << 2;
833794fc
MR
11433 macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
11434 end_noreorder ();
11435 break;
11436
11437 case M_JRC:
11438 gas_assert (mips_opts.micromips);
11439 gas_assert (mips_opts.insn32);
c0ebe874 11440 macro_build (NULL, "jr", "s", op[0]);
833794fc
MR
11441 if (mips_opts.noreorder)
11442 macro_build (NULL, "nop", "");
11443 break;
11444
252b5132
RH
11445 case M_LI:
11446 case M_LI_S:
c0ebe874 11447 load_register (op[0], &imm_expr, 0);
8fc2e39e 11448 break;
252b5132
RH
11449
11450 case M_DLI:
c0ebe874 11451 load_register (op[0], &imm_expr, 1);
8fc2e39e 11452 break;
252b5132
RH
11453
11454 case M_LI_SS:
11455 if (imm_expr.X_op == O_constant)
11456 {
8fc2e39e 11457 used_at = 1;
67c0d1eb 11458 load_register (AT, &imm_expr, 0);
c0ebe874 11459 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
11460 break;
11461 }
11462 else
11463 {
b0e6f033
RS
11464 gas_assert (imm_expr.X_op == O_absent
11465 && offset_expr.X_op == O_symbol
90ecf173
MR
11466 && strcmp (segment_name (S_GET_SEGMENT
11467 (offset_expr.X_add_symbol)),
11468 ".lit4") == 0
11469 && offset_expr.X_add_number == 0);
c0ebe874 11470 macro_build (&offset_expr, "lwc1", "T,o(b)", op[0],
17a2f251 11471 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 11472 break;
252b5132
RH
11473 }
11474
11475 case M_LI_D:
ca4e0257
RS
11476 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
11477 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
11478 order 32 bits of the value and the low order 32 bits are either
11479 zero or in OFFSET_EXPR. */
b0e6f033 11480 if (imm_expr.X_op == O_constant)
252b5132 11481 {
bad1aba3 11482 if (GPR_SIZE == 64)
c0ebe874 11483 load_register (op[0], &imm_expr, 1);
252b5132
RH
11484 else
11485 {
11486 int hreg, lreg;
11487
11488 if (target_big_endian)
11489 {
c0ebe874
RS
11490 hreg = op[0];
11491 lreg = op[0] + 1;
252b5132
RH
11492 }
11493 else
11494 {
c0ebe874
RS
11495 hreg = op[0] + 1;
11496 lreg = op[0];
252b5132
RH
11497 }
11498
11499 if (hreg <= 31)
67c0d1eb 11500 load_register (hreg, &imm_expr, 0);
252b5132
RH
11501 if (lreg <= 31)
11502 {
11503 if (offset_expr.X_op == O_absent)
67c0d1eb 11504 move_register (lreg, 0);
252b5132
RH
11505 else
11506 {
9c2799c2 11507 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 11508 load_register (lreg, &offset_expr, 0);
252b5132
RH
11509 }
11510 }
11511 }
8fc2e39e 11512 break;
252b5132 11513 }
b0e6f033 11514 gas_assert (imm_expr.X_op == O_absent);
252b5132
RH
11515
11516 /* We know that sym is in the .rdata section. First we get the
11517 upper 16 bits of the address. */
11518 if (mips_pic == NO_PIC)
11519 {
67c0d1eb 11520 macro_build_lui (&offset_expr, AT);
8fc2e39e 11521 used_at = 1;
252b5132 11522 }
0a44bf69 11523 else
252b5132 11524 {
67c0d1eb
RS
11525 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
11526 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 11527 used_at = 1;
252b5132 11528 }
bdaaa2e1 11529
252b5132 11530 /* Now we load the register(s). */
bad1aba3 11531 if (GPR_SIZE == 64)
8fc2e39e
TS
11532 {
11533 used_at = 1;
c0ebe874
RS
11534 macro_build (&offset_expr, "ld", "t,o(b)", op[0],
11535 BFD_RELOC_LO16, AT);
8fc2e39e 11536 }
252b5132
RH
11537 else
11538 {
8fc2e39e 11539 used_at = 1;
c0ebe874
RS
11540 macro_build (&offset_expr, "lw", "t,o(b)", op[0],
11541 BFD_RELOC_LO16, AT);
11542 if (op[0] != RA)
252b5132
RH
11543 {
11544 /* FIXME: How in the world do we deal with the possible
11545 overflow here? */
11546 offset_expr.X_add_number += 4;
67c0d1eb 11547 macro_build (&offset_expr, "lw", "t,o(b)",
c0ebe874 11548 op[0] + 1, BFD_RELOC_LO16, AT);
252b5132
RH
11549 }
11550 }
252b5132
RH
11551 break;
11552
11553 case M_LI_DD:
ca4e0257
RS
11554 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
11555 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
11556 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
11557 the value and the low order 32 bits are either zero or in
11558 OFFSET_EXPR. */
b0e6f033 11559 if (imm_expr.X_op == O_constant)
252b5132 11560 {
8fc2e39e 11561 used_at = 1;
bad1aba3 11562 load_register (AT, &imm_expr, FPR_SIZE == 64);
11563 if (FPR_SIZE == 64)
ca4e0257 11564 {
bad1aba3 11565 gas_assert (GPR_SIZE == 64);
c0ebe874 11566 macro_build (NULL, "dmtc1", "t,S", AT, op[0]);
ca4e0257 11567 }
252b5132
RH
11568 else
11569 {
c0ebe874 11570 macro_build (NULL, "mtc1", "t,G", AT, op[0] + 1);
252b5132 11571 if (offset_expr.X_op == O_absent)
c0ebe874 11572 macro_build (NULL, "mtc1", "t,G", 0, op[0]);
252b5132
RH
11573 else
11574 {
9c2799c2 11575 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 11576 load_register (AT, &offset_expr, 0);
c0ebe874 11577 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
11578 }
11579 }
11580 break;
11581 }
11582
b0e6f033
RS
11583 gas_assert (imm_expr.X_op == O_absent
11584 && offset_expr.X_op == O_symbol
90ecf173 11585 && offset_expr.X_add_number == 0);
252b5132
RH
11586 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
11587 if (strcmp (s, ".lit8") == 0)
f2ae14a1 11588 {
c0ebe874 11589 op[2] = mips_gp_register;
f2ae14a1
RS
11590 offset_reloc[0] = BFD_RELOC_MIPS_LITERAL;
11591 offset_reloc[1] = BFD_RELOC_UNUSED;
11592 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
11593 }
11594 else
11595 {
9c2799c2 11596 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 11597 used_at = 1;
0a44bf69 11598 if (mips_pic != NO_PIC)
67c0d1eb
RS
11599 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
11600 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
11601 else
11602 {
11603 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 11604 macro_build_lui (&offset_expr, AT);
252b5132 11605 }
bdaaa2e1 11606
c0ebe874 11607 op[2] = AT;
f2ae14a1
RS
11608 offset_reloc[0] = BFD_RELOC_LO16;
11609 offset_reloc[1] = BFD_RELOC_UNUSED;
11610 offset_reloc[2] = BFD_RELOC_UNUSED;
11611 }
11612 align = 8;
11613 /* Fall through */
c4a68bea 11614
252b5132
RH
11615 case M_L_DAB:
11616 /*
11617 * The MIPS assembler seems to check for X_add_number not
11618 * being double aligned and generating:
11619 * lui at,%hi(foo+1)
11620 * addu at,at,v1
11621 * addiu at,at,%lo(foo+1)
11622 * lwc1 f2,0(at)
11623 * lwc1 f3,4(at)
11624 * But, the resulting address is the same after relocation so why
11625 * generate the extra instruction?
11626 */
bdaaa2e1 11627 /* Itbl support may require additional care here. */
252b5132 11628 coproc = 1;
df58fc94 11629 fmt = "T,o(b)";
0aa27725 11630 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
11631 {
11632 s = "ldc1";
df58fc94 11633 goto ld_st;
252b5132 11634 }
252b5132 11635 s = "lwc1";
252b5132
RH
11636 goto ldd_std;
11637
11638 case M_S_DAB:
df58fc94
RS
11639 gas_assert (!mips_opts.micromips);
11640 /* Itbl support may require additional care here. */
11641 coproc = 1;
11642 fmt = "T,o(b)";
0aa27725 11643 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
11644 {
11645 s = "sdc1";
df58fc94 11646 goto ld_st;
252b5132 11647 }
252b5132 11648 s = "swc1";
252b5132
RH
11649 goto ldd_std;
11650
e407c74b
NC
11651 case M_LQ_AB:
11652 fmt = "t,o(b)";
11653 s = "lq";
11654 goto ld;
11655
11656 case M_SQ_AB:
11657 fmt = "t,o(b)";
11658 s = "sq";
11659 goto ld_st;
11660
252b5132 11661 case M_LD_AB:
df58fc94 11662 fmt = "t,o(b)";
bad1aba3 11663 if (GPR_SIZE == 64)
252b5132
RH
11664 {
11665 s = "ld";
11666 goto ld;
11667 }
252b5132 11668 s = "lw";
252b5132
RH
11669 goto ldd_std;
11670
11671 case M_SD_AB:
df58fc94 11672 fmt = "t,o(b)";
bad1aba3 11673 if (GPR_SIZE == 64)
252b5132
RH
11674 {
11675 s = "sd";
df58fc94 11676 goto ld_st;
252b5132 11677 }
252b5132 11678 s = "sw";
252b5132
RH
11679
11680 ldd_std:
f2ae14a1
RS
11681 /* Even on a big endian machine $fn comes before $fn+1. We have
11682 to adjust when loading from memory. We set coproc if we must
11683 load $fn+1 first. */
11684 /* Itbl support may require additional care here. */
11685 if (!target_big_endian)
11686 coproc = 0;
11687
c0ebe874 11688 breg = op[2];
f2ae14a1
RS
11689 if (small_offset_p (0, align, 16))
11690 {
11691 ep = &offset_expr;
11692 if (!small_offset_p (4, align, 16))
11693 {
11694 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT, breg,
11695 -1, offset_reloc[0], offset_reloc[1],
11696 offset_reloc[2]);
11697 expr1.X_add_number = 0;
11698 ep = &expr1;
11699 breg = AT;
11700 used_at = 1;
11701 offset_reloc[0] = BFD_RELOC_LO16;
11702 offset_reloc[1] = BFD_RELOC_UNUSED;
11703 offset_reloc[2] = BFD_RELOC_UNUSED;
11704 }
c0ebe874 11705 if (strcmp (s, "lw") == 0 && op[0] == breg)
f2ae14a1
RS
11706 {
11707 ep->X_add_number += 4;
c0ebe874 11708 macro_build (ep, s, fmt, op[0] + 1, -1, offset_reloc[0],
f2ae14a1
RS
11709 offset_reloc[1], offset_reloc[2], breg);
11710 ep->X_add_number -= 4;
c0ebe874 11711 macro_build (ep, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
11712 offset_reloc[1], offset_reloc[2], breg);
11713 }
11714 else
11715 {
c0ebe874 11716 macro_build (ep, s, fmt, coproc ? op[0] + 1 : op[0], -1,
f2ae14a1
RS
11717 offset_reloc[0], offset_reloc[1], offset_reloc[2],
11718 breg);
11719 ep->X_add_number += 4;
c0ebe874 11720 macro_build (ep, s, fmt, coproc ? op[0] : op[0] + 1, -1,
f2ae14a1
RS
11721 offset_reloc[0], offset_reloc[1], offset_reloc[2],
11722 breg);
11723 }
11724 break;
11725 }
11726
252b5132
RH
11727 if (offset_expr.X_op != O_symbol
11728 && offset_expr.X_op != O_constant)
11729 {
1661c76c 11730 as_bad (_("expression too complex"));
252b5132
RH
11731 offset_expr.X_op = O_constant;
11732 }
11733
2051e8c4
MR
11734 if (HAVE_32BIT_ADDRESSES
11735 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
11736 {
11737 char value [32];
11738
11739 sprintf_vma (value, offset_expr.X_add_number);
1661c76c 11740 as_bad (_("number (0x%s) larger than 32 bits"), value);
55e08f71 11741 }
2051e8c4 11742
90ecf173 11743 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
252b5132
RH
11744 {
11745 /* If this is a reference to a GP relative symbol, we want
c0ebe874
RS
11746 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
11747 <op> op[0]+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
11748 If we have a base register, we use this
11749 addu $at,$breg,$gp
c0ebe874
RS
11750 <op> op[0],<sym>($at) (BFD_RELOC_GPREL16)
11751 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
11752 If this is not a GP relative symbol, we want
11753 lui $at,<sym> (BFD_RELOC_HI16_S)
c0ebe874
RS
11754 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
11755 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
11756 If there is a base register, we add it to $at after the
11757 lui instruction. If there is a constant, we always use
11758 the last case. */
39a59cf8
MR
11759 if (offset_expr.X_op == O_symbol
11760 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 11761 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 11762 {
4d7206a2 11763 relax_start (offset_expr.X_add_symbol);
252b5132
RH
11764 if (breg == 0)
11765 {
c9914766 11766 tempreg = mips_gp_register;
252b5132
RH
11767 }
11768 else
11769 {
67c0d1eb 11770 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11771 AT, breg, mips_gp_register);
252b5132 11772 tempreg = AT;
252b5132
RH
11773 used_at = 1;
11774 }
11775
beae10d5 11776 /* Itbl support may require additional care here. */
c0ebe874 11777 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 11778 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
11779 offset_expr.X_add_number += 4;
11780
11781 /* Set mips_optimize to 2 to avoid inserting an
11782 undesired nop. */
11783 hold_mips_optimize = mips_optimize;
11784 mips_optimize = 2;
beae10d5 11785 /* Itbl support may require additional care here. */
c0ebe874 11786 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 11787 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
11788 mips_optimize = hold_mips_optimize;
11789
4d7206a2 11790 relax_switch ();
252b5132 11791
0970e49e 11792 offset_expr.X_add_number -= 4;
252b5132 11793 }
8fc2e39e 11794 used_at = 1;
f2ae14a1
RS
11795 if (offset_high_part (offset_expr.X_add_number, 16)
11796 != offset_high_part (offset_expr.X_add_number + 4, 16))
11797 {
11798 load_address (AT, &offset_expr, &used_at);
11799 offset_expr.X_op = O_constant;
11800 offset_expr.X_add_number = 0;
11801 }
11802 else
11803 macro_build_lui (&offset_expr, AT);
252b5132 11804 if (breg != 0)
67c0d1eb 11805 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 11806 /* Itbl support may require additional care here. */
c0ebe874 11807 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 11808 BFD_RELOC_LO16, AT);
252b5132
RH
11809 /* FIXME: How do we handle overflow here? */
11810 offset_expr.X_add_number += 4;
beae10d5 11811 /* Itbl support may require additional care here. */
c0ebe874 11812 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 11813 BFD_RELOC_LO16, AT);
4d7206a2
RS
11814 if (mips_relax.sequence)
11815 relax_end ();
bdaaa2e1 11816 }
0a44bf69 11817 else if (!mips_big_got)
252b5132 11818 {
252b5132
RH
11819 /* If this is a reference to an external symbol, we want
11820 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11821 nop
c0ebe874
RS
11822 <op> op[0],0($at)
11823 <op> op[0]+1,4($at)
252b5132
RH
11824 Otherwise we want
11825 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11826 nop
c0ebe874
RS
11827 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
11828 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
11829 If there is a base register we add it to $at before the
11830 lwc1 instructions. If there is a constant we include it
11831 in the lwc1 instructions. */
11832 used_at = 1;
11833 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
11834 if (expr1.X_add_number < -0x8000
11835 || expr1.X_add_number >= 0x8000 - 4)
11836 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 11837 load_got_offset (AT, &offset_expr);
269137b2 11838 load_delay_nop ();
252b5132 11839 if (breg != 0)
67c0d1eb 11840 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
11841
11842 /* Set mips_optimize to 2 to avoid inserting an undesired
11843 nop. */
11844 hold_mips_optimize = mips_optimize;
11845 mips_optimize = 2;
4d7206a2 11846
beae10d5 11847 /* Itbl support may require additional care here. */
4d7206a2 11848 relax_start (offset_expr.X_add_symbol);
c0ebe874 11849 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 11850 BFD_RELOC_LO16, AT);
4d7206a2 11851 expr1.X_add_number += 4;
c0ebe874 11852 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 11853 BFD_RELOC_LO16, AT);
4d7206a2 11854 relax_switch ();
c0ebe874 11855 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 11856 BFD_RELOC_LO16, AT);
4d7206a2 11857 offset_expr.X_add_number += 4;
c0ebe874 11858 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 11859 BFD_RELOC_LO16, AT);
4d7206a2 11860 relax_end ();
252b5132 11861
4d7206a2 11862 mips_optimize = hold_mips_optimize;
252b5132 11863 }
0a44bf69 11864 else if (mips_big_got)
252b5132 11865 {
67c0d1eb 11866 int gpdelay;
252b5132
RH
11867
11868 /* If this is a reference to an external symbol, we want
11869 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11870 addu $at,$at,$gp
11871 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
11872 nop
c0ebe874
RS
11873 <op> op[0],0($at)
11874 <op> op[0]+1,4($at)
252b5132
RH
11875 Otherwise we want
11876 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11877 nop
c0ebe874
RS
11878 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
11879 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
11880 If there is a base register we add it to $at before the
11881 lwc1 instructions. If there is a constant we include it
11882 in the lwc1 instructions. */
11883 used_at = 1;
11884 expr1.X_add_number = offset_expr.X_add_number;
11885 offset_expr.X_add_number = 0;
11886 if (expr1.X_add_number < -0x8000
11887 || expr1.X_add_number >= 0x8000 - 4)
11888 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 11889 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 11890 relax_start (offset_expr.X_add_symbol);
df58fc94 11891 macro_build (&offset_expr, "lui", LUI_FMT,
67c0d1eb
RS
11892 AT, BFD_RELOC_MIPS_GOT_HI16);
11893 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11894 AT, AT, mips_gp_register);
67c0d1eb 11895 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 11896 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 11897 load_delay_nop ();
252b5132 11898 if (breg != 0)
67c0d1eb 11899 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 11900 /* Itbl support may require additional care here. */
c0ebe874 11901 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 11902 BFD_RELOC_LO16, AT);
252b5132
RH
11903 expr1.X_add_number += 4;
11904
11905 /* Set mips_optimize to 2 to avoid inserting an undesired
11906 nop. */
11907 hold_mips_optimize = mips_optimize;
11908 mips_optimize = 2;
beae10d5 11909 /* Itbl support may require additional care here. */
c0ebe874 11910 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 11911 BFD_RELOC_LO16, AT);
252b5132
RH
11912 mips_optimize = hold_mips_optimize;
11913 expr1.X_add_number -= 4;
11914
4d7206a2
RS
11915 relax_switch ();
11916 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
11917 if (gpdelay)
11918 macro_build (NULL, "nop", "");
11919 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
11920 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 11921 load_delay_nop ();
252b5132 11922 if (breg != 0)
67c0d1eb 11923 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 11924 /* Itbl support may require additional care here. */
c0ebe874 11925 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 11926 BFD_RELOC_LO16, AT);
4d7206a2 11927 offset_expr.X_add_number += 4;
252b5132
RH
11928
11929 /* Set mips_optimize to 2 to avoid inserting an undesired
11930 nop. */
11931 hold_mips_optimize = mips_optimize;
11932 mips_optimize = 2;
beae10d5 11933 /* Itbl support may require additional care here. */
c0ebe874 11934 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 11935 BFD_RELOC_LO16, AT);
252b5132 11936 mips_optimize = hold_mips_optimize;
4d7206a2 11937 relax_end ();
252b5132 11938 }
252b5132
RH
11939 else
11940 abort ();
11941
252b5132 11942 break;
dd6a37e7
AP
11943
11944 case M_SAA_AB:
dd6a37e7 11945 s = "saa";
7f3c4072 11946 offbits = 0;
dd6a37e7
AP
11947 fmt = "t,(b)";
11948 goto ld_st;
11949 case M_SAAD_AB:
dd6a37e7 11950 s = "saad";
7f3c4072 11951 offbits = 0;
dd6a37e7
AP
11952 fmt = "t,(b)";
11953 goto ld_st;
11954
252b5132
RH
11955 /* New code added to support COPZ instructions.
11956 This code builds table entries out of the macros in mip_opcodes.
11957 R4000 uses interlocks to handle coproc delays.
11958 Other chips (like the R3000) require nops to be inserted for delays.
11959
f72c8c98 11960 FIXME: Currently, we require that the user handle delays.
252b5132
RH
11961 In order to fill delay slots for non-interlocked chips,
11962 we must have a way to specify delays based on the coprocessor.
11963 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
11964 What are the side-effects of the cop instruction?
11965 What cache support might we have and what are its effects?
11966 Both coprocessor & memory require delays. how long???
bdaaa2e1 11967 What registers are read/set/modified?
252b5132
RH
11968
11969 If an itbl is provided to interpret cop instructions,
bdaaa2e1 11970 this knowledge can be encoded in the itbl spec. */
252b5132
RH
11971
11972 case M_COP0:
11973 s = "c0";
11974 goto copz;
11975 case M_COP1:
11976 s = "c1";
11977 goto copz;
11978 case M_COP2:
11979 s = "c2";
11980 goto copz;
11981 case M_COP3:
11982 s = "c3";
11983 copz:
df58fc94 11984 gas_assert (!mips_opts.micromips);
252b5132
RH
11985 /* For now we just do C (same as Cz). The parameter will be
11986 stored in insn_opcode by mips_ip. */
c8276761 11987 macro_build (NULL, s, "C", (int) ip->insn_opcode);
8fc2e39e 11988 break;
252b5132 11989
ea1fb5dc 11990 case M_MOVE:
c0ebe874 11991 move_register (op[0], op[1]);
8fc2e39e 11992 break;
ea1fb5dc 11993
833794fc
MR
11994 case M_MOVEP:
11995 gas_assert (mips_opts.micromips);
11996 gas_assert (mips_opts.insn32);
c0ebe874
RS
11997 move_register (micromips_to_32_reg_h_map1[op[0]],
11998 micromips_to_32_reg_m_map[op[1]]);
11999 move_register (micromips_to_32_reg_h_map2[op[0]],
12000 micromips_to_32_reg_n_map[op[2]]);
833794fc
MR
12001 break;
12002
252b5132
RH
12003 case M_DMUL:
12004 dbl = 1;
12005 case M_MUL:
e407c74b 12006 if (mips_opts.arch == CPU_R5900)
c0ebe874
RS
12007 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", op[0], op[1],
12008 op[2]);
e407c74b
NC
12009 else
12010 {
c0ebe874
RS
12011 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", op[1], op[2]);
12012 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
e407c74b 12013 }
8fc2e39e 12014 break;
252b5132
RH
12015
12016 case M_DMUL_I:
12017 dbl = 1;
12018 case M_MUL_I:
12019 /* The MIPS assembler some times generates shifts and adds. I'm
12020 not trying to be that fancy. GCC should do this for us
12021 anyway. */
8fc2e39e 12022 used_at = 1;
67c0d1eb 12023 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
12024 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", op[1], AT);
12025 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
12026 break;
12027
12028 case M_DMULO_I:
12029 dbl = 1;
12030 case M_MULO_I:
12031 imm = 1;
12032 goto do_mulo;
12033
12034 case M_DMULO:
12035 dbl = 1;
12036 case M_MULO:
12037 do_mulo:
7d10b47d 12038 start_noreorder ();
8fc2e39e 12039 used_at = 1;
252b5132 12040 if (imm)
67c0d1eb 12041 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
12042 macro_build (NULL, dbl ? "dmult" : "mult", "s,t",
12043 op[1], imm ? AT : op[2]);
12044 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
12045 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, op[0], op[0], 31);
df58fc94 12046 macro_build (NULL, "mfhi", MFHL_FMT, AT);
252b5132 12047 if (mips_trap)
c0ebe874 12048 macro_build (NULL, "tne", TRAP_FMT, op[0], AT, 6);
252b5132
RH
12049 else
12050 {
df58fc94
RS
12051 if (mips_opts.micromips)
12052 micromips_label_expr (&label_expr);
12053 else
12054 label_expr.X_add_number = 8;
c0ebe874 12055 macro_build (&label_expr, "beq", "s,t,p", op[0], AT);
a605d2b3 12056 macro_build (NULL, "nop", "");
df58fc94
RS
12057 macro_build (NULL, "break", BRK_FMT, 6);
12058 if (mips_opts.micromips)
12059 micromips_add_label ();
252b5132 12060 }
7d10b47d 12061 end_noreorder ();
c0ebe874 12062 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
12063 break;
12064
12065 case M_DMULOU_I:
12066 dbl = 1;
12067 case M_MULOU_I:
12068 imm = 1;
12069 goto do_mulou;
12070
12071 case M_DMULOU:
12072 dbl = 1;
12073 case M_MULOU:
12074 do_mulou:
7d10b47d 12075 start_noreorder ();
8fc2e39e 12076 used_at = 1;
252b5132 12077 if (imm)
67c0d1eb
RS
12078 load_register (AT, &imm_expr, dbl);
12079 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
c0ebe874 12080 op[1], imm ? AT : op[2]);
df58fc94 12081 macro_build (NULL, "mfhi", MFHL_FMT, AT);
c0ebe874 12082 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132 12083 if (mips_trap)
df58fc94 12084 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
252b5132
RH
12085 else
12086 {
df58fc94
RS
12087 if (mips_opts.micromips)
12088 micromips_label_expr (&label_expr);
12089 else
12090 label_expr.X_add_number = 8;
12091 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
a605d2b3 12092 macro_build (NULL, "nop", "");
df58fc94
RS
12093 macro_build (NULL, "break", BRK_FMT, 6);
12094 if (mips_opts.micromips)
12095 micromips_add_label ();
252b5132 12096 }
7d10b47d 12097 end_noreorder ();
252b5132
RH
12098 break;
12099
771c7ce4 12100 case M_DROL:
fef14a42 12101 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 12102 {
c0ebe874 12103 if (op[0] == op[1])
82dd0097
CD
12104 {
12105 tempreg = AT;
12106 used_at = 1;
12107 }
12108 else
c0ebe874
RS
12109 tempreg = op[0];
12110 macro_build (NULL, "dnegu", "d,w", tempreg, op[2]);
12111 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 12112 break;
82dd0097 12113 }
8fc2e39e 12114 used_at = 1;
c0ebe874
RS
12115 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
12116 macro_build (NULL, "dsrlv", "d,t,s", AT, op[1], AT);
12117 macro_build (NULL, "dsllv", "d,t,s", op[0], op[1], op[2]);
12118 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
12119 break;
12120
252b5132 12121 case M_ROL:
fef14a42 12122 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 12123 {
c0ebe874 12124 if (op[0] == op[1])
82dd0097
CD
12125 {
12126 tempreg = AT;
12127 used_at = 1;
12128 }
12129 else
c0ebe874
RS
12130 tempreg = op[0];
12131 macro_build (NULL, "negu", "d,w", tempreg, op[2]);
12132 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 12133 break;
82dd0097 12134 }
8fc2e39e 12135 used_at = 1;
c0ebe874
RS
12136 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
12137 macro_build (NULL, "srlv", "d,t,s", AT, op[1], AT);
12138 macro_build (NULL, "sllv", "d,t,s", op[0], op[1], op[2]);
12139 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
12140 break;
12141
771c7ce4
TS
12142 case M_DROL_I:
12143 {
12144 unsigned int rot;
91d6fa6a
NC
12145 char *l;
12146 char *rr;
771c7ce4 12147
771c7ce4 12148 rot = imm_expr.X_add_number & 0x3f;
fef14a42 12149 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
12150 {
12151 rot = (64 - rot) & 0x3f;
12152 if (rot >= 32)
c0ebe874 12153 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
60b63b72 12154 else
c0ebe874 12155 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 12156 break;
60b63b72 12157 }
483fc7cd 12158 if (rot == 0)
483fc7cd 12159 {
c0ebe874 12160 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 12161 break;
483fc7cd 12162 }
82dd0097 12163 l = (rot < 0x20) ? "dsll" : "dsll32";
91d6fa6a 12164 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
82dd0097 12165 rot &= 0x1f;
8fc2e39e 12166 used_at = 1;
c0ebe874
RS
12167 macro_build (NULL, l, SHFT_FMT, AT, op[1], rot);
12168 macro_build (NULL, rr, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
12169 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
12170 }
12171 break;
12172
252b5132 12173 case M_ROL_I:
771c7ce4
TS
12174 {
12175 unsigned int rot;
12176
771c7ce4 12177 rot = imm_expr.X_add_number & 0x1f;
fef14a42 12178 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 12179 {
c0ebe874
RS
12180 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1],
12181 (32 - rot) & 0x1f);
8fc2e39e 12182 break;
60b63b72 12183 }
483fc7cd 12184 if (rot == 0)
483fc7cd 12185 {
c0ebe874 12186 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 12187 break;
483fc7cd 12188 }
8fc2e39e 12189 used_at = 1;
c0ebe874
RS
12190 macro_build (NULL, "sll", SHFT_FMT, AT, op[1], rot);
12191 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
12192 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
12193 }
12194 break;
12195
12196 case M_DROR:
fef14a42 12197 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 12198 {
c0ebe874 12199 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 12200 break;
82dd0097 12201 }
8fc2e39e 12202 used_at = 1;
c0ebe874
RS
12203 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
12204 macro_build (NULL, "dsllv", "d,t,s", AT, op[1], AT);
12205 macro_build (NULL, "dsrlv", "d,t,s", op[0], op[1], op[2]);
12206 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
12207 break;
12208
12209 case M_ROR:
fef14a42 12210 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 12211 {
c0ebe874 12212 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 12213 break;
82dd0097 12214 }
8fc2e39e 12215 used_at = 1;
c0ebe874
RS
12216 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
12217 macro_build (NULL, "sllv", "d,t,s", AT, op[1], AT);
12218 macro_build (NULL, "srlv", "d,t,s", op[0], op[1], op[2]);
12219 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
12220 break;
12221
771c7ce4
TS
12222 case M_DROR_I:
12223 {
12224 unsigned int rot;
91d6fa6a
NC
12225 char *l;
12226 char *rr;
771c7ce4 12227
771c7ce4 12228 rot = imm_expr.X_add_number & 0x3f;
fef14a42 12229 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
12230 {
12231 if (rot >= 32)
c0ebe874 12232 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
82dd0097 12233 else
c0ebe874 12234 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 12235 break;
82dd0097 12236 }
483fc7cd 12237 if (rot == 0)
483fc7cd 12238 {
c0ebe874 12239 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 12240 break;
483fc7cd 12241 }
91d6fa6a 12242 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
82dd0097
CD
12243 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
12244 rot &= 0x1f;
8fc2e39e 12245 used_at = 1;
c0ebe874
RS
12246 macro_build (NULL, rr, SHFT_FMT, AT, op[1], rot);
12247 macro_build (NULL, l, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
12248 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
12249 }
12250 break;
12251
252b5132 12252 case M_ROR_I:
771c7ce4
TS
12253 {
12254 unsigned int rot;
12255
771c7ce4 12256 rot = imm_expr.X_add_number & 0x1f;
fef14a42 12257 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 12258 {
c0ebe874 12259 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 12260 break;
82dd0097 12261 }
483fc7cd 12262 if (rot == 0)
483fc7cd 12263 {
c0ebe874 12264 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 12265 break;
483fc7cd 12266 }
8fc2e39e 12267 used_at = 1;
c0ebe874
RS
12268 macro_build (NULL, "srl", SHFT_FMT, AT, op[1], rot);
12269 macro_build (NULL, "sll", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
12270 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4 12271 }
252b5132
RH
12272 break;
12273
252b5132 12274 case M_SEQ:
c0ebe874
RS
12275 if (op[1] == 0)
12276 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[2], BFD_RELOC_LO16);
12277 else if (op[2] == 0)
12278 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
12279 else
12280 {
c0ebe874
RS
12281 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
12282 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
252b5132 12283 }
8fc2e39e 12284 break;
252b5132
RH
12285
12286 case M_SEQ_I:
b0e6f033 12287 if (imm_expr.X_add_number == 0)
252b5132 12288 {
c0ebe874 12289 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 12290 break;
252b5132 12291 }
c0ebe874 12292 if (op[1] == 0)
252b5132 12293 {
1661c76c 12294 as_warn (_("instruction %s: result is always false"),
252b5132 12295 ip->insn_mo->name);
c0ebe874 12296 move_register (op[0], 0);
8fc2e39e 12297 break;
252b5132 12298 }
dd3cbb7e
NC
12299 if (CPU_HAS_SEQ (mips_opts.arch)
12300 && -512 <= imm_expr.X_add_number
12301 && imm_expr.X_add_number < 512)
12302 {
c0ebe874 12303 macro_build (NULL, "seqi", "t,r,+Q", op[0], op[1],
750bdd57 12304 (int) imm_expr.X_add_number);
dd3cbb7e
NC
12305 break;
12306 }
b0e6f033 12307 if (imm_expr.X_add_number >= 0
252b5132 12308 && imm_expr.X_add_number < 0x10000)
c0ebe874 12309 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1], BFD_RELOC_LO16);
b0e6f033 12310 else if (imm_expr.X_add_number > -0x8000
252b5132
RH
12311 && imm_expr.X_add_number < 0)
12312 {
12313 imm_expr.X_add_number = -imm_expr.X_add_number;
bad1aba3 12314 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
c0ebe874 12315 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 12316 }
dd3cbb7e
NC
12317 else if (CPU_HAS_SEQ (mips_opts.arch))
12318 {
12319 used_at = 1;
bad1aba3 12320 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 12321 macro_build (NULL, "seq", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
12322 break;
12323 }
252b5132
RH
12324 else
12325 {
bad1aba3 12326 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 12327 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
12328 used_at = 1;
12329 }
c0ebe874 12330 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 12331 break;
252b5132 12332
c0ebe874 12333 case M_SGE: /* X >= Y <==> not (X < Y) */
252b5132
RH
12334 s = "slt";
12335 goto sge;
12336 case M_SGEU:
12337 s = "sltu";
12338 sge:
c0ebe874
RS
12339 macro_build (NULL, s, "d,v,t", op[0], op[1], op[2]);
12340 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 12341 break;
252b5132 12342
c0ebe874 12343 case M_SGE_I: /* X >= I <==> not (X < I) */
252b5132 12344 case M_SGEU_I:
b0e6f033 12345 if (imm_expr.X_add_number >= -0x8000
252b5132 12346 && imm_expr.X_add_number < 0x8000)
c0ebe874
RS
12347 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
12348 op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
12349 else
12350 {
bad1aba3 12351 load_register (AT, &imm_expr, GPR_SIZE == 64);
67c0d1eb 12352 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
c0ebe874 12353 op[0], op[1], AT);
252b5132
RH
12354 used_at = 1;
12355 }
c0ebe874 12356 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 12357 break;
252b5132 12358
c0ebe874 12359 case M_SGT: /* X > Y <==> Y < X */
252b5132
RH
12360 s = "slt";
12361 goto sgt;
12362 case M_SGTU:
12363 s = "sltu";
12364 sgt:
c0ebe874 12365 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
8fc2e39e 12366 break;
252b5132 12367
c0ebe874 12368 case M_SGT_I: /* X > I <==> I < X */
252b5132
RH
12369 s = "slt";
12370 goto sgti;
12371 case M_SGTU_I:
12372 s = "sltu";
12373 sgti:
8fc2e39e 12374 used_at = 1;
bad1aba3 12375 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 12376 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
252b5132
RH
12377 break;
12378
c0ebe874 12379 case M_SLE: /* X <= Y <==> Y >= X <==> not (Y < X) */
252b5132
RH
12380 s = "slt";
12381 goto sle;
12382 case M_SLEU:
12383 s = "sltu";
12384 sle:
c0ebe874
RS
12385 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
12386 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 12387 break;
252b5132 12388
c0ebe874 12389 case M_SLE_I: /* X <= I <==> I >= X <==> not (I < X) */
252b5132
RH
12390 s = "slt";
12391 goto slei;
12392 case M_SLEU_I:
12393 s = "sltu";
12394 slei:
8fc2e39e 12395 used_at = 1;
bad1aba3 12396 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874
RS
12397 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
12398 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
252b5132
RH
12399 break;
12400
12401 case M_SLT_I:
b0e6f033 12402 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
12403 && imm_expr.X_add_number < 0x8000)
12404 {
c0ebe874
RS
12405 macro_build (&imm_expr, "slti", "t,r,j", op[0], op[1],
12406 BFD_RELOC_LO16);
8fc2e39e 12407 break;
252b5132 12408 }
8fc2e39e 12409 used_at = 1;
bad1aba3 12410 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 12411 macro_build (NULL, "slt", "d,v,t", op[0], op[1], AT);
252b5132
RH
12412 break;
12413
12414 case M_SLTU_I:
b0e6f033 12415 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
12416 && imm_expr.X_add_number < 0x8000)
12417 {
c0ebe874 12418 macro_build (&imm_expr, "sltiu", "t,r,j", op[0], op[1],
17a2f251 12419 BFD_RELOC_LO16);
8fc2e39e 12420 break;
252b5132 12421 }
8fc2e39e 12422 used_at = 1;
bad1aba3 12423 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 12424 macro_build (NULL, "sltu", "d,v,t", op[0], op[1], AT);
252b5132
RH
12425 break;
12426
12427 case M_SNE:
c0ebe874
RS
12428 if (op[1] == 0)
12429 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[2]);
12430 else if (op[2] == 0)
12431 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
252b5132
RH
12432 else
12433 {
c0ebe874
RS
12434 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
12435 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
252b5132 12436 }
8fc2e39e 12437 break;
252b5132
RH
12438
12439 case M_SNE_I:
b0e6f033 12440 if (imm_expr.X_add_number == 0)
252b5132 12441 {
c0ebe874 12442 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
8fc2e39e 12443 break;
252b5132 12444 }
c0ebe874 12445 if (op[1] == 0)
252b5132 12446 {
1661c76c 12447 as_warn (_("instruction %s: result is always true"),
252b5132 12448 ip->insn_mo->name);
bad1aba3 12449 macro_build (&expr1, GPR_SIZE == 32 ? "addiu" : "daddiu", "t,r,j",
c0ebe874 12450 op[0], 0, BFD_RELOC_LO16);
8fc2e39e 12451 break;
252b5132 12452 }
dd3cbb7e
NC
12453 if (CPU_HAS_SEQ (mips_opts.arch)
12454 && -512 <= imm_expr.X_add_number
12455 && imm_expr.X_add_number < 512)
12456 {
c0ebe874 12457 macro_build (NULL, "snei", "t,r,+Q", op[0], op[1],
750bdd57 12458 (int) imm_expr.X_add_number);
dd3cbb7e
NC
12459 break;
12460 }
b0e6f033 12461 if (imm_expr.X_add_number >= 0
252b5132
RH
12462 && imm_expr.X_add_number < 0x10000)
12463 {
c0ebe874
RS
12464 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1],
12465 BFD_RELOC_LO16);
252b5132 12466 }
b0e6f033 12467 else if (imm_expr.X_add_number > -0x8000
252b5132
RH
12468 && imm_expr.X_add_number < 0)
12469 {
12470 imm_expr.X_add_number = -imm_expr.X_add_number;
bad1aba3 12471 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
c0ebe874 12472 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 12473 }
dd3cbb7e
NC
12474 else if (CPU_HAS_SEQ (mips_opts.arch))
12475 {
12476 used_at = 1;
bad1aba3 12477 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 12478 macro_build (NULL, "sne", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
12479 break;
12480 }
252b5132
RH
12481 else
12482 {
bad1aba3 12483 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 12484 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
12485 used_at = 1;
12486 }
c0ebe874 12487 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
8fc2e39e 12488 break;
252b5132 12489
df58fc94
RS
12490 case M_SUB_I:
12491 s = "addi";
12492 s2 = "sub";
12493 goto do_subi;
12494 case M_SUBU_I:
12495 s = "addiu";
12496 s2 = "subu";
12497 goto do_subi;
252b5132
RH
12498 case M_DSUB_I:
12499 dbl = 1;
df58fc94
RS
12500 s = "daddi";
12501 s2 = "dsub";
12502 if (!mips_opts.micromips)
12503 goto do_subi;
b0e6f033 12504 if (imm_expr.X_add_number > -0x200
df58fc94 12505 && imm_expr.X_add_number <= 0x200)
252b5132 12506 {
b0e6f033
RS
12507 macro_build (NULL, s, "t,r,.", op[0], op[1],
12508 (int) -imm_expr.X_add_number);
8fc2e39e 12509 break;
252b5132 12510 }
df58fc94 12511 goto do_subi_i;
252b5132
RH
12512 case M_DSUBU_I:
12513 dbl = 1;
df58fc94
RS
12514 s = "daddiu";
12515 s2 = "dsubu";
12516 do_subi:
b0e6f033 12517 if (imm_expr.X_add_number > -0x8000
252b5132
RH
12518 && imm_expr.X_add_number <= 0x8000)
12519 {
12520 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 12521 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 12522 break;
252b5132 12523 }
df58fc94 12524 do_subi_i:
8fc2e39e 12525 used_at = 1;
67c0d1eb 12526 load_register (AT, &imm_expr, dbl);
c0ebe874 12527 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
12528 break;
12529
12530 case M_TEQ_I:
12531 s = "teq";
12532 goto trap;
12533 case M_TGE_I:
12534 s = "tge";
12535 goto trap;
12536 case M_TGEU_I:
12537 s = "tgeu";
12538 goto trap;
12539 case M_TLT_I:
12540 s = "tlt";
12541 goto trap;
12542 case M_TLTU_I:
12543 s = "tltu";
12544 goto trap;
12545 case M_TNE_I:
12546 s = "tne";
12547 trap:
8fc2e39e 12548 used_at = 1;
bad1aba3 12549 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 12550 macro_build (NULL, s, "s,t", op[0], AT);
252b5132
RH
12551 break;
12552
252b5132 12553 case M_TRUNCWS:
43841e91 12554 case M_TRUNCWD:
df58fc94 12555 gas_assert (!mips_opts.micromips);
0aa27725 12556 gas_assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 12557 used_at = 1;
252b5132
RH
12558
12559 /*
12560 * Is the double cfc1 instruction a bug in the mips assembler;
12561 * or is there a reason for it?
12562 */
7d10b47d 12563 start_noreorder ();
c0ebe874
RS
12564 macro_build (NULL, "cfc1", "t,G", op[2], RA);
12565 macro_build (NULL, "cfc1", "t,G", op[2], RA);
67c0d1eb 12566 macro_build (NULL, "nop", "");
252b5132 12567 expr1.X_add_number = 3;
c0ebe874 12568 macro_build (&expr1, "ori", "t,r,i", AT, op[2], BFD_RELOC_LO16);
252b5132 12569 expr1.X_add_number = 2;
67c0d1eb
RS
12570 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
12571 macro_build (NULL, "ctc1", "t,G", AT, RA);
12572 macro_build (NULL, "nop", "");
12573 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
c0ebe874
RS
12574 op[0], op[1]);
12575 macro_build (NULL, "ctc1", "t,G", op[2], RA);
67c0d1eb 12576 macro_build (NULL, "nop", "");
7d10b47d 12577 end_noreorder ();
252b5132
RH
12578 break;
12579
f2ae14a1 12580 case M_ULH_AB:
252b5132 12581 s = "lb";
df58fc94
RS
12582 s2 = "lbu";
12583 off = 1;
12584 goto uld_st;
f2ae14a1 12585 case M_ULHU_AB:
252b5132 12586 s = "lbu";
df58fc94
RS
12587 s2 = "lbu";
12588 off = 1;
12589 goto uld_st;
f2ae14a1 12590 case M_ULW_AB:
df58fc94
RS
12591 s = "lwl";
12592 s2 = "lwr";
7f3c4072 12593 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
12594 off = 3;
12595 goto uld_st;
f2ae14a1 12596 case M_ULD_AB:
252b5132
RH
12597 s = "ldl";
12598 s2 = "ldr";
7f3c4072 12599 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 12600 off = 7;
df58fc94 12601 goto uld_st;
f2ae14a1 12602 case M_USH_AB:
df58fc94
RS
12603 s = "sb";
12604 s2 = "sb";
12605 off = 1;
12606 ust = 1;
12607 goto uld_st;
f2ae14a1 12608 case M_USW_AB:
df58fc94
RS
12609 s = "swl";
12610 s2 = "swr";
7f3c4072 12611 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 12612 off = 3;
df58fc94
RS
12613 ust = 1;
12614 goto uld_st;
f2ae14a1 12615 case M_USD_AB:
df58fc94
RS
12616 s = "sdl";
12617 s2 = "sdr";
7f3c4072 12618 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
12619 off = 7;
12620 ust = 1;
12621
12622 uld_st:
c0ebe874 12623 breg = op[2];
f2ae14a1 12624 large_offset = !small_offset_p (off, align, offbits);
df58fc94
RS
12625 ep = &offset_expr;
12626 expr1.X_add_number = 0;
f2ae14a1 12627 if (large_offset)
df58fc94
RS
12628 {
12629 used_at = 1;
12630 tempreg = AT;
f2ae14a1
RS
12631 if (small_offset_p (0, align, 16))
12632 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg, -1,
12633 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
12634 else
12635 {
12636 load_address (tempreg, ep, &used_at);
12637 if (breg != 0)
12638 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12639 tempreg, tempreg, breg);
12640 }
12641 offset_reloc[0] = BFD_RELOC_LO16;
12642 offset_reloc[1] = BFD_RELOC_UNUSED;
12643 offset_reloc[2] = BFD_RELOC_UNUSED;
df58fc94 12644 breg = tempreg;
c0ebe874 12645 tempreg = op[0];
df58fc94
RS
12646 ep = &expr1;
12647 }
c0ebe874 12648 else if (!ust && op[0] == breg)
8fc2e39e
TS
12649 {
12650 used_at = 1;
12651 tempreg = AT;
12652 }
252b5132 12653 else
c0ebe874 12654 tempreg = op[0];
af22f5b2 12655
df58fc94
RS
12656 if (off == 1)
12657 goto ulh_sh;
252b5132 12658
90ecf173 12659 if (!target_big_endian)
df58fc94 12660 ep->X_add_number += off;
f2ae14a1 12661 if (offbits == 12)
c8276761 12662 macro_build (NULL, s, "t,~(b)", tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
12663 else
12664 macro_build (ep, s, "t,o(b)", tempreg, -1,
12665 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94 12666
90ecf173 12667 if (!target_big_endian)
df58fc94 12668 ep->X_add_number -= off;
252b5132 12669 else
df58fc94 12670 ep->X_add_number += off;
f2ae14a1 12671 if (offbits == 12)
df58fc94 12672 macro_build (NULL, s2, "t,~(b)",
c8276761 12673 tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
12674 else
12675 macro_build (ep, s2, "t,o(b)", tempreg, -1,
12676 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 12677
df58fc94 12678 /* If necessary, move the result in tempreg to the final destination. */
c0ebe874 12679 if (!ust && op[0] != tempreg)
df58fc94
RS
12680 {
12681 /* Protect second load's delay slot. */
12682 load_delay_nop ();
c0ebe874 12683 move_register (op[0], tempreg);
df58fc94 12684 }
8fc2e39e 12685 break;
252b5132 12686
df58fc94 12687 ulh_sh:
d6bc6245 12688 used_at = 1;
df58fc94
RS
12689 if (target_big_endian == ust)
12690 ep->X_add_number += off;
c0ebe874 12691 tempreg = ust || large_offset ? op[0] : AT;
f2ae14a1
RS
12692 macro_build (ep, s, "t,o(b)", tempreg, -1,
12693 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94
RS
12694
12695 /* For halfword transfers we need a temporary register to shuffle
12696 bytes. Unfortunately for M_USH_A we have none available before
12697 the next store as AT holds the base address. We deal with this
12698 case by clobbering TREG and then restoring it as with ULH. */
c0ebe874 12699 tempreg = ust == large_offset ? op[0] : AT;
df58fc94 12700 if (ust)
c0ebe874 12701 macro_build (NULL, "srl", SHFT_FMT, tempreg, op[0], 8);
df58fc94
RS
12702
12703 if (target_big_endian == ust)
12704 ep->X_add_number -= off;
252b5132 12705 else
df58fc94 12706 ep->X_add_number += off;
f2ae14a1
RS
12707 macro_build (ep, s2, "t,o(b)", tempreg, -1,
12708 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 12709
df58fc94 12710 /* For M_USH_A re-retrieve the LSB. */
f2ae14a1 12711 if (ust && large_offset)
df58fc94
RS
12712 {
12713 if (target_big_endian)
12714 ep->X_add_number += off;
12715 else
12716 ep->X_add_number -= off;
f2ae14a1
RS
12717 macro_build (&expr1, "lbu", "t,o(b)", AT, -1,
12718 offset_reloc[0], offset_reloc[1], offset_reloc[2], AT);
df58fc94
RS
12719 }
12720 /* For ULH and M_USH_A OR the LSB in. */
f2ae14a1 12721 if (!ust || large_offset)
df58fc94 12722 {
c0ebe874 12723 tempreg = !large_offset ? AT : op[0];
df58fc94 12724 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
c0ebe874 12725 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
df58fc94 12726 }
252b5132
RH
12727 break;
12728
12729 default:
12730 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 12731 are added dynamically. */
1661c76c 12732 as_bad (_("macro %s not implemented yet"), ip->insn_mo->name);
252b5132
RH
12733 break;
12734 }
741fe287 12735 if (!mips_opts.at && used_at)
1661c76c 12736 as_bad (_("macro used $at after \".set noat\""));
252b5132
RH
12737}
12738
12739/* Implement macros in mips16 mode. */
12740
12741static void
17a2f251 12742mips16_macro (struct mips_cl_insn *ip)
252b5132 12743{
c0ebe874 12744 const struct mips_operand_array *operands;
252b5132 12745 int mask;
c0ebe874 12746 int tmp;
252b5132
RH
12747 expressionS expr1;
12748 int dbl;
12749 const char *s, *s2, *s3;
c0ebe874
RS
12750 unsigned int op[MAX_OPERANDS];
12751 unsigned int i;
252b5132
RH
12752
12753 mask = ip->insn_mo->mask;
12754
c0ebe874
RS
12755 operands = insn_operands (ip);
12756 for (i = 0; i < MAX_OPERANDS; i++)
12757 if (operands->operand[i])
12758 op[i] = insn_extract_operand (ip, operands->operand[i]);
12759 else
12760 op[i] = -1;
252b5132 12761
252b5132
RH
12762 expr1.X_op = O_constant;
12763 expr1.X_op_symbol = NULL;
12764 expr1.X_add_symbol = NULL;
12765 expr1.X_add_number = 1;
12766
12767 dbl = 0;
12768
12769 switch (mask)
12770 {
12771 default:
b37df7c4 12772 abort ();
252b5132
RH
12773
12774 case M_DDIV_3:
12775 dbl = 1;
12776 case M_DIV_3:
12777 s = "mflo";
12778 goto do_div3;
12779 case M_DREM_3:
12780 dbl = 1;
12781 case M_REM_3:
12782 s = "mfhi";
12783 do_div3:
7d10b47d 12784 start_noreorder ();
c0ebe874 12785 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", op[1], op[2]);
252b5132 12786 expr1.X_add_number = 2;
c0ebe874 12787 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 12788 macro_build (NULL, "break", "6", 7);
bdaaa2e1 12789
252b5132
RH
12790 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
12791 since that causes an overflow. We should do that as well,
12792 but I don't see how to do the comparisons without a temporary
12793 register. */
7d10b47d 12794 end_noreorder ();
c0ebe874 12795 macro_build (NULL, s, "x", op[0]);
252b5132
RH
12796 break;
12797
12798 case M_DIVU_3:
12799 s = "divu";
12800 s2 = "mflo";
12801 goto do_divu3;
12802 case M_REMU_3:
12803 s = "divu";
12804 s2 = "mfhi";
12805 goto do_divu3;
12806 case M_DDIVU_3:
12807 s = "ddivu";
12808 s2 = "mflo";
12809 goto do_divu3;
12810 case M_DREMU_3:
12811 s = "ddivu";
12812 s2 = "mfhi";
12813 do_divu3:
7d10b47d 12814 start_noreorder ();
c0ebe874 12815 macro_build (NULL, s, "0,x,y", op[1], op[2]);
252b5132 12816 expr1.X_add_number = 2;
c0ebe874 12817 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 12818 macro_build (NULL, "break", "6", 7);
7d10b47d 12819 end_noreorder ();
c0ebe874 12820 macro_build (NULL, s2, "x", op[0]);
252b5132
RH
12821 break;
12822
12823 case M_DMUL:
12824 dbl = 1;
12825 case M_MUL:
c0ebe874
RS
12826 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", op[1], op[2]);
12827 macro_build (NULL, "mflo", "x", op[0]);
8fc2e39e 12828 break;
252b5132
RH
12829
12830 case M_DSUBU_I:
12831 dbl = 1;
12832 goto do_subu;
12833 case M_SUBU_I:
12834 do_subu:
252b5132 12835 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 12836 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", op[0], op[1]);
252b5132
RH
12837 break;
12838
12839 case M_SUBU_I_2:
252b5132 12840 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 12841 macro_build (&imm_expr, "addiu", "x,k", op[0]);
252b5132
RH
12842 break;
12843
12844 case M_DSUBU_I_2:
252b5132 12845 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 12846 macro_build (&imm_expr, "daddiu", "y,j", op[0]);
252b5132
RH
12847 break;
12848
12849 case M_BEQ:
12850 s = "cmp";
12851 s2 = "bteqz";
12852 goto do_branch;
12853 case M_BNE:
12854 s = "cmp";
12855 s2 = "btnez";
12856 goto do_branch;
12857 case M_BLT:
12858 s = "slt";
12859 s2 = "btnez";
12860 goto do_branch;
12861 case M_BLTU:
12862 s = "sltu";
12863 s2 = "btnez";
12864 goto do_branch;
12865 case M_BLE:
12866 s = "slt";
12867 s2 = "bteqz";
12868 goto do_reverse_branch;
12869 case M_BLEU:
12870 s = "sltu";
12871 s2 = "bteqz";
12872 goto do_reverse_branch;
12873 case M_BGE:
12874 s = "slt";
12875 s2 = "bteqz";
12876 goto do_branch;
12877 case M_BGEU:
12878 s = "sltu";
12879 s2 = "bteqz";
12880 goto do_branch;
12881 case M_BGT:
12882 s = "slt";
12883 s2 = "btnez";
12884 goto do_reverse_branch;
12885 case M_BGTU:
12886 s = "sltu";
12887 s2 = "btnez";
12888
12889 do_reverse_branch:
c0ebe874
RS
12890 tmp = op[1];
12891 op[1] = op[0];
12892 op[0] = tmp;
252b5132
RH
12893
12894 do_branch:
c0ebe874 12895 macro_build (NULL, s, "x,y", op[0], op[1]);
67c0d1eb 12896 macro_build (&offset_expr, s2, "p");
252b5132
RH
12897 break;
12898
12899 case M_BEQ_I:
12900 s = "cmpi";
12901 s2 = "bteqz";
12902 s3 = "x,U";
12903 goto do_branch_i;
12904 case M_BNE_I:
12905 s = "cmpi";
12906 s2 = "btnez";
12907 s3 = "x,U";
12908 goto do_branch_i;
12909 case M_BLT_I:
12910 s = "slti";
12911 s2 = "btnez";
12912 s3 = "x,8";
12913 goto do_branch_i;
12914 case M_BLTU_I:
12915 s = "sltiu";
12916 s2 = "btnez";
12917 s3 = "x,8";
12918 goto do_branch_i;
12919 case M_BLE_I:
12920 s = "slti";
12921 s2 = "btnez";
12922 s3 = "x,8";
12923 goto do_addone_branch_i;
12924 case M_BLEU_I:
12925 s = "sltiu";
12926 s2 = "btnez";
12927 s3 = "x,8";
12928 goto do_addone_branch_i;
12929 case M_BGE_I:
12930 s = "slti";
12931 s2 = "bteqz";
12932 s3 = "x,8";
12933 goto do_branch_i;
12934 case M_BGEU_I:
12935 s = "sltiu";
12936 s2 = "bteqz";
12937 s3 = "x,8";
12938 goto do_branch_i;
12939 case M_BGT_I:
12940 s = "slti";
12941 s2 = "bteqz";
12942 s3 = "x,8";
12943 goto do_addone_branch_i;
12944 case M_BGTU_I:
12945 s = "sltiu";
12946 s2 = "bteqz";
12947 s3 = "x,8";
12948
12949 do_addone_branch_i:
252b5132
RH
12950 ++imm_expr.X_add_number;
12951
12952 do_branch_i:
c0ebe874 12953 macro_build (&imm_expr, s, s3, op[0]);
67c0d1eb 12954 macro_build (&offset_expr, s2, "p");
252b5132
RH
12955 break;
12956
12957 case M_ABS:
12958 expr1.X_add_number = 0;
c0ebe874
RS
12959 macro_build (&expr1, "slti", "x,8", op[1]);
12960 if (op[0] != op[1])
12961 macro_build (NULL, "move", "y,X", op[0], mips16_to_32_reg_map[op[1]]);
252b5132 12962 expr1.X_add_number = 2;
67c0d1eb 12963 macro_build (&expr1, "bteqz", "p");
c0ebe874 12964 macro_build (NULL, "neg", "x,w", op[0], op[0]);
0acfaea6 12965 break;
252b5132
RH
12966 }
12967}
12968
14daeee3
RS
12969/* Look up instruction [START, START + LENGTH) in HASH. Record any extra
12970 opcode bits in *OPCODE_EXTRA. */
12971
12972static struct mips_opcode *
12973mips_lookup_insn (struct hash_control *hash, const char *start,
da8bca91 12974 ssize_t length, unsigned int *opcode_extra)
14daeee3
RS
12975{
12976 char *name, *dot, *p;
12977 unsigned int mask, suffix;
da8bca91 12978 ssize_t opend;
14daeee3
RS
12979 struct mips_opcode *insn;
12980
12981 /* Make a copy of the instruction so that we can fiddle with it. */
12982 name = alloca (length + 1);
12983 memcpy (name, start, length);
12984 name[length] = '\0';
12985
12986 /* Look up the instruction as-is. */
12987 insn = (struct mips_opcode *) hash_find (hash, name);
ee5734f0 12988 if (insn)
14daeee3
RS
12989 return insn;
12990
12991 dot = strchr (name, '.');
12992 if (dot && dot[1])
12993 {
12994 /* Try to interpret the text after the dot as a VU0 channel suffix. */
12995 p = mips_parse_vu0_channels (dot + 1, &mask);
12996 if (*p == 0 && mask != 0)
12997 {
12998 *dot = 0;
12999 insn = (struct mips_opcode *) hash_find (hash, name);
13000 *dot = '.';
13001 if (insn && (insn->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0)
13002 {
13003 *opcode_extra |= mask << mips_vu0_channel_mask.lsb;
13004 return insn;
13005 }
13006 }
13007 }
13008
13009 if (mips_opts.micromips)
13010 {
13011 /* See if there's an instruction size override suffix,
13012 either `16' or `32', at the end of the mnemonic proper,
13013 that defines the operation, i.e. before the first `.'
13014 character if any. Strip it and retry. */
13015 opend = dot != NULL ? dot - name : length;
13016 if (opend >= 3 && name[opend - 2] == '1' && name[opend - 1] == '6')
13017 suffix = 2;
13018 else if (name[opend - 2] == '3' && name[opend - 1] == '2')
13019 suffix = 4;
13020 else
13021 suffix = 0;
13022 if (suffix)
13023 {
13024 memcpy (name + opend - 2, name + opend, length - opend + 1);
13025 insn = (struct mips_opcode *) hash_find (hash, name);
ee5734f0 13026 if (insn)
14daeee3
RS
13027 {
13028 forced_insn_length = suffix;
13029 return insn;
13030 }
13031 }
13032 }
13033
13034 return NULL;
13035}
13036
77bd4346 13037/* Assemble an instruction into its binary format. If the instruction
e423441d
RS
13038 is a macro, set imm_expr and offset_expr to the values associated
13039 with "I" and "A" operands respectively. Otherwise store the value
13040 of the relocatable field (if any) in offset_expr. In both cases
13041 set offset_reloc to the relocation operators applied to offset_expr. */
252b5132
RH
13042
13043static void
60f20e8b 13044mips_ip (char *str, struct mips_cl_insn *insn)
252b5132 13045{
60f20e8b 13046 const struct mips_opcode *first, *past;
df58fc94 13047 struct hash_control *hash;
a92713e6 13048 char format;
14daeee3 13049 size_t end;
a92713e6 13050 struct mips_operand_token *tokens;
14daeee3 13051 unsigned int opcode_extra;
252b5132 13052
df58fc94
RS
13053 if (mips_opts.micromips)
13054 {
13055 hash = micromips_op_hash;
13056 past = &micromips_opcodes[bfd_micromips_num_opcodes];
13057 }
13058 else
13059 {
13060 hash = op_hash;
13061 past = &mips_opcodes[NUMOPCODES];
13062 }
13063 forced_insn_length = 0;
14daeee3 13064 opcode_extra = 0;
252b5132 13065
df58fc94 13066 /* We first try to match an instruction up to a space or to the end. */
a40bc9dd
RS
13067 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
13068 continue;
bdaaa2e1 13069
60f20e8b
RS
13070 first = mips_lookup_insn (hash, str, end, &opcode_extra);
13071 if (first == NULL)
252b5132 13072 {
1661c76c 13073 set_insn_error (0, _("unrecognized opcode"));
a40bc9dd 13074 return;
252b5132
RH
13075 }
13076
60f20e8b 13077 if (strcmp (first->name, "li.s") == 0)
a92713e6 13078 format = 'f';
60f20e8b 13079 else if (strcmp (first->name, "li.d") == 0)
a92713e6
RS
13080 format = 'd';
13081 else
13082 format = 0;
13083 tokens = mips_parse_arguments (str + end, format);
13084 if (!tokens)
13085 return;
13086
60f20e8b
RS
13087 if (!match_insns (insn, first, past, tokens, opcode_extra, FALSE)
13088 && !match_insns (insn, first, past, tokens, opcode_extra, TRUE))
1661c76c 13089 set_insn_error (0, _("invalid operands"));
df58fc94 13090
e3de51ce 13091 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
13092}
13093
77bd4346
RS
13094/* As for mips_ip, but used when assembling MIPS16 code.
13095 Also set forced_insn_length to the resulting instruction size in
13096 bytes if the user explicitly requested a small or extended instruction. */
252b5132
RH
13097
13098static void
60f20e8b 13099mips16_ip (char *str, struct mips_cl_insn *insn)
252b5132 13100{
1a00e612 13101 char *end, *s, c;
60f20e8b 13102 struct mips_opcode *first;
a92713e6 13103 struct mips_operand_token *tokens;
252b5132 13104
df58fc94 13105 forced_insn_length = 0;
252b5132 13106
3882b010 13107 for (s = str; ISLOWER (*s); ++s)
252b5132 13108 ;
1a00e612
RS
13109 end = s;
13110 c = *end;
13111 switch (c)
252b5132
RH
13112 {
13113 case '\0':
13114 break;
13115
13116 case ' ':
1a00e612 13117 s++;
252b5132
RH
13118 break;
13119
13120 case '.':
13121 if (s[1] == 't' && s[2] == ' ')
13122 {
df58fc94 13123 forced_insn_length = 2;
252b5132
RH
13124 s += 3;
13125 break;
13126 }
13127 else if (s[1] == 'e' && s[2] == ' ')
13128 {
df58fc94 13129 forced_insn_length = 4;
252b5132
RH
13130 s += 3;
13131 break;
13132 }
13133 /* Fall through. */
13134 default:
1661c76c 13135 set_insn_error (0, _("unrecognized opcode"));
252b5132
RH
13136 return;
13137 }
13138
df58fc94
RS
13139 if (mips_opts.noautoextend && !forced_insn_length)
13140 forced_insn_length = 2;
252b5132 13141
1a00e612 13142 *end = 0;
60f20e8b 13143 first = (struct mips_opcode *) hash_find (mips16_op_hash, str);
1a00e612
RS
13144 *end = c;
13145
60f20e8b 13146 if (!first)
252b5132 13147 {
1661c76c 13148 set_insn_error (0, _("unrecognized opcode"));
252b5132
RH
13149 return;
13150 }
13151
a92713e6
RS
13152 tokens = mips_parse_arguments (s, 0);
13153 if (!tokens)
13154 return;
13155
60f20e8b 13156 if (!match_mips16_insns (insn, first, tokens))
1661c76c 13157 set_insn_error (0, _("invalid operands"));
252b5132 13158
e3de51ce 13159 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
13160}
13161
b886a2ab
RS
13162/* Marshal immediate value VAL for an extended MIPS16 instruction.
13163 NBITS is the number of significant bits in VAL. */
13164
13165static unsigned long
13166mips16_immed_extend (offsetT val, unsigned int nbits)
13167{
13168 int extval;
13169 if (nbits == 16)
13170 {
13171 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
13172 val &= 0x1f;
13173 }
13174 else if (nbits == 15)
13175 {
13176 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
13177 val &= 0xf;
13178 }
13179 else
13180 {
13181 extval = ((val & 0x1f) << 6) | (val & 0x20);
13182 val = 0;
13183 }
13184 return (extval << 16) | val;
13185}
13186
3ccad066
RS
13187/* Like decode_mips16_operand, but require the operand to be defined and
13188 require it to be an integer. */
13189
13190static const struct mips_int_operand *
13191mips16_immed_operand (int type, bfd_boolean extended_p)
13192{
13193 const struct mips_operand *operand;
13194
13195 operand = decode_mips16_operand (type, extended_p);
13196 if (!operand || (operand->type != OP_INT && operand->type != OP_PCREL))
13197 abort ();
13198 return (const struct mips_int_operand *) operand;
13199}
13200
13201/* Return true if SVAL fits OPERAND. RELOC is as for mips16_immed. */
13202
13203static bfd_boolean
13204mips16_immed_in_range_p (const struct mips_int_operand *operand,
13205 bfd_reloc_code_real_type reloc, offsetT sval)
13206{
13207 int min_val, max_val;
13208
13209 min_val = mips_int_operand_min (operand);
13210 max_val = mips_int_operand_max (operand);
13211 if (reloc != BFD_RELOC_UNUSED)
13212 {
13213 if (min_val < 0)
13214 sval = SEXT_16BIT (sval);
13215 else
13216 sval &= 0xffff;
13217 }
13218
13219 return (sval >= min_val
13220 && sval <= max_val
13221 && (sval & ((1 << operand->shift) - 1)) == 0);
13222}
13223
5c04167a
RS
13224/* Install immediate value VAL into MIPS16 instruction *INSN,
13225 extending it if necessary. The instruction in *INSN may
13226 already be extended.
13227
43c0598f
RS
13228 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
13229 if none. In the former case, VAL is a 16-bit number with no
13230 defined signedness.
13231
13232 TYPE is the type of the immediate field. USER_INSN_LENGTH
13233 is the length that the user requested, or 0 if none. */
252b5132
RH
13234
13235static void
43c0598f
RS
13236mips16_immed (char *file, unsigned int line, int type,
13237 bfd_reloc_code_real_type reloc, offsetT val,
5c04167a 13238 unsigned int user_insn_length, unsigned long *insn)
252b5132 13239{
3ccad066
RS
13240 const struct mips_int_operand *operand;
13241 unsigned int uval, length;
252b5132 13242
3ccad066
RS
13243 operand = mips16_immed_operand (type, FALSE);
13244 if (!mips16_immed_in_range_p (operand, reloc, val))
5c04167a
RS
13245 {
13246 /* We need an extended instruction. */
13247 if (user_insn_length == 2)
13248 as_bad_where (file, line, _("invalid unextended operand value"));
13249 else
13250 *insn |= MIPS16_EXTEND;
13251 }
13252 else if (user_insn_length == 4)
13253 {
13254 /* The operand doesn't force an unextended instruction to be extended.
13255 Warn if the user wanted an extended instruction anyway. */
13256 *insn |= MIPS16_EXTEND;
13257 as_warn_where (file, line,
13258 _("extended operand requested but not required"));
13259 }
252b5132 13260
3ccad066
RS
13261 length = mips16_opcode_length (*insn);
13262 if (length == 4)
252b5132 13263 {
3ccad066
RS
13264 operand = mips16_immed_operand (type, TRUE);
13265 if (!mips16_immed_in_range_p (operand, reloc, val))
13266 as_bad_where (file, line,
13267 _("operand value out of range for instruction"));
252b5132 13268 }
3ccad066
RS
13269 uval = ((unsigned int) val >> operand->shift) - operand->bias;
13270 if (length == 2)
13271 *insn = mips_insert_operand (&operand->root, *insn, uval);
252b5132 13272 else
3ccad066 13273 *insn |= mips16_immed_extend (uval, operand->root.size);
252b5132
RH
13274}
13275\f
d6f16593 13276struct percent_op_match
ad8d3bb3 13277{
5e0116d5
RS
13278 const char *str;
13279 bfd_reloc_code_real_type reloc;
d6f16593
MR
13280};
13281
13282static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 13283{
5e0116d5 13284 {"%lo", BFD_RELOC_LO16},
5e0116d5
RS
13285 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
13286 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
13287 {"%call16", BFD_RELOC_MIPS_CALL16},
13288 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
13289 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
13290 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
13291 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
13292 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
13293 {"%got", BFD_RELOC_MIPS_GOT16},
13294 {"%gp_rel", BFD_RELOC_GPREL16},
13295 {"%half", BFD_RELOC_16},
13296 {"%highest", BFD_RELOC_MIPS_HIGHEST},
13297 {"%higher", BFD_RELOC_MIPS_HIGHER},
13298 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
13299 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
13300 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
13301 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
13302 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
13303 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
13304 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
13305 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
5e0116d5 13306 {"%hi", BFD_RELOC_HI16_S}
ad8d3bb3
TS
13307};
13308
d6f16593
MR
13309static const struct percent_op_match mips16_percent_op[] =
13310{
13311 {"%lo", BFD_RELOC_MIPS16_LO16},
13312 {"%gprel", BFD_RELOC_MIPS16_GPREL},
738e5348
RS
13313 {"%got", BFD_RELOC_MIPS16_GOT16},
13314 {"%call16", BFD_RELOC_MIPS16_CALL16},
d0f13682
CLT
13315 {"%hi", BFD_RELOC_MIPS16_HI16_S},
13316 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
13317 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
13318 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
13319 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
13320 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
13321 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
13322 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
d6f16593
MR
13323};
13324
252b5132 13325
5e0116d5
RS
13326/* Return true if *STR points to a relocation operator. When returning true,
13327 move *STR over the operator and store its relocation code in *RELOC.
13328 Leave both *STR and *RELOC alone when returning false. */
13329
13330static bfd_boolean
17a2f251 13331parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 13332{
d6f16593
MR
13333 const struct percent_op_match *percent_op;
13334 size_t limit, i;
13335
13336 if (mips_opts.mips16)
13337 {
13338 percent_op = mips16_percent_op;
13339 limit = ARRAY_SIZE (mips16_percent_op);
13340 }
13341 else
13342 {
13343 percent_op = mips_percent_op;
13344 limit = ARRAY_SIZE (mips_percent_op);
13345 }
76b3015f 13346
d6f16593 13347 for (i = 0; i < limit; i++)
5e0116d5 13348 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 13349 {
3f98094e
DJ
13350 int len = strlen (percent_op[i].str);
13351
13352 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
13353 continue;
13354
5e0116d5
RS
13355 *str += strlen (percent_op[i].str);
13356 *reloc = percent_op[i].reloc;
394f9b3a 13357
5e0116d5
RS
13358 /* Check whether the output BFD supports this relocation.
13359 If not, issue an error and fall back on something safe. */
13360 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 13361 {
20203fb9 13362 as_bad (_("relocation %s isn't supported by the current ABI"),
5e0116d5 13363 percent_op[i].str);
01a3f561 13364 *reloc = BFD_RELOC_UNUSED;
394f9b3a 13365 }
5e0116d5 13366 return TRUE;
394f9b3a 13367 }
5e0116d5 13368 return FALSE;
394f9b3a 13369}
ad8d3bb3 13370
ad8d3bb3 13371
5e0116d5
RS
13372/* Parse string STR as a 16-bit relocatable operand. Store the
13373 expression in *EP and the relocations in the array starting
13374 at RELOC. Return the number of relocation operators used.
ad8d3bb3 13375
01a3f561 13376 On exit, EXPR_END points to the first character after the expression. */
ad8d3bb3 13377
5e0116d5 13378static size_t
17a2f251
TS
13379my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
13380 char *str)
ad8d3bb3 13381{
5e0116d5
RS
13382 bfd_reloc_code_real_type reversed_reloc[3];
13383 size_t reloc_index, i;
09b8f35a
RS
13384 int crux_depth, str_depth;
13385 char *crux;
5e0116d5
RS
13386
13387 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
13388 in REVERSED_RELOC. End the loop with CRUX pointing to the start
13389 of the main expression and with CRUX_DEPTH containing the number
13390 of open brackets at that point. */
13391 reloc_index = -1;
13392 str_depth = 0;
13393 do
fb1b3232 13394 {
09b8f35a
RS
13395 reloc_index++;
13396 crux = str;
13397 crux_depth = str_depth;
13398
13399 /* Skip over whitespace and brackets, keeping count of the number
13400 of brackets. */
13401 while (*str == ' ' || *str == '\t' || *str == '(')
13402 if (*str++ == '(')
13403 str_depth++;
5e0116d5 13404 }
09b8f35a
RS
13405 while (*str == '%'
13406 && reloc_index < (HAVE_NEWABI ? 3 : 1)
13407 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 13408
09b8f35a 13409 my_getExpression (ep, crux);
5e0116d5 13410 str = expr_end;
394f9b3a 13411
5e0116d5 13412 /* Match every open bracket. */
09b8f35a 13413 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 13414 if (*str++ == ')')
09b8f35a 13415 crux_depth--;
394f9b3a 13416
09b8f35a 13417 if (crux_depth > 0)
20203fb9 13418 as_bad (_("unclosed '('"));
394f9b3a 13419
5e0116d5 13420 expr_end = str;
252b5132 13421
01a3f561 13422 if (reloc_index != 0)
64bdfcaf
RS
13423 {
13424 prev_reloc_op_frag = frag_now;
13425 for (i = 0; i < reloc_index; i++)
13426 reloc[i] = reversed_reloc[reloc_index - 1 - i];
13427 }
fb1b3232 13428
5e0116d5 13429 return reloc_index;
252b5132
RH
13430}
13431
13432static void
17a2f251 13433my_getExpression (expressionS *ep, char *str)
252b5132
RH
13434{
13435 char *save_in;
13436
13437 save_in = input_line_pointer;
13438 input_line_pointer = str;
13439 expression (ep);
13440 expr_end = input_line_pointer;
13441 input_line_pointer = save_in;
252b5132
RH
13442}
13443
252b5132 13444char *
17a2f251 13445md_atof (int type, char *litP, int *sizeP)
252b5132 13446{
499ac353 13447 return ieee_md_atof (type, litP, sizeP, target_big_endian);
252b5132
RH
13448}
13449
13450void
17a2f251 13451md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
13452{
13453 if (target_big_endian)
13454 number_to_chars_bigendian (buf, val, n);
13455 else
13456 number_to_chars_littleendian (buf, val, n);
13457}
13458\f
e013f690
TS
13459static int support_64bit_objects(void)
13460{
13461 const char **list, **l;
aa3d8fdf 13462 int yes;
e013f690
TS
13463
13464 list = bfd_target_list ();
13465 for (l = list; *l != NULL; l++)
aeffff67
RS
13466 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
13467 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
e013f690 13468 break;
aa3d8fdf 13469 yes = (*l != NULL);
e013f690 13470 free (list);
aa3d8fdf 13471 return yes;
e013f690
TS
13472}
13473
316f5878
RS
13474/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
13475 NEW_VALUE. Warn if another value was already specified. Note:
13476 we have to defer parsing the -march and -mtune arguments in order
13477 to handle 'from-abi' correctly, since the ABI might be specified
13478 in a later argument. */
13479
13480static void
17a2f251 13481mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
13482{
13483 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
1661c76c 13484 as_warn (_("a different %s was already specified, is now %s"),
316f5878
RS
13485 string_ptr == &mips_arch_string ? "-march" : "-mtune",
13486 new_value);
13487
13488 *string_ptr = new_value;
13489}
13490
252b5132 13491int
17a2f251 13492md_parse_option (int c, char *arg)
252b5132 13493{
c6278170
RS
13494 unsigned int i;
13495
13496 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
13497 if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
13498 {
13499 file_ase_explicit |= mips_set_ase (&mips_ases[i],
13500 c == mips_ases[i].option_on);
13501 return 1;
13502 }
13503
252b5132
RH
13504 switch (c)
13505 {
119d663a
NC
13506 case OPTION_CONSTRUCT_FLOATS:
13507 mips_disable_float_construction = 0;
13508 break;
bdaaa2e1 13509
119d663a
NC
13510 case OPTION_NO_CONSTRUCT_FLOATS:
13511 mips_disable_float_construction = 1;
13512 break;
bdaaa2e1 13513
252b5132
RH
13514 case OPTION_TRAP:
13515 mips_trap = 1;
13516 break;
13517
13518 case OPTION_BREAK:
13519 mips_trap = 0;
13520 break;
13521
13522 case OPTION_EB:
13523 target_big_endian = 1;
13524 break;
13525
13526 case OPTION_EL:
13527 target_big_endian = 0;
13528 break;
13529
13530 case 'O':
4ffff32f
TS
13531 if (arg == NULL)
13532 mips_optimize = 1;
13533 else if (arg[0] == '0')
13534 mips_optimize = 0;
13535 else if (arg[0] == '1')
252b5132
RH
13536 mips_optimize = 1;
13537 else
13538 mips_optimize = 2;
13539 break;
13540
13541 case 'g':
13542 if (arg == NULL)
13543 mips_debug = 2;
13544 else
13545 mips_debug = atoi (arg);
252b5132
RH
13546 break;
13547
13548 case OPTION_MIPS1:
0b35dfee 13549 file_mips_opts.isa = ISA_MIPS1;
252b5132
RH
13550 break;
13551
13552 case OPTION_MIPS2:
0b35dfee 13553 file_mips_opts.isa = ISA_MIPS2;
252b5132
RH
13554 break;
13555
13556 case OPTION_MIPS3:
0b35dfee 13557 file_mips_opts.isa = ISA_MIPS3;
252b5132
RH
13558 break;
13559
13560 case OPTION_MIPS4:
0b35dfee 13561 file_mips_opts.isa = ISA_MIPS4;
e7af610e
NC
13562 break;
13563
84ea6cf2 13564 case OPTION_MIPS5:
0b35dfee 13565 file_mips_opts.isa = ISA_MIPS5;
84ea6cf2
NC
13566 break;
13567
e7af610e 13568 case OPTION_MIPS32:
0b35dfee 13569 file_mips_opts.isa = ISA_MIPS32;
252b5132
RH
13570 break;
13571
af7ee8bf 13572 case OPTION_MIPS32R2:
0b35dfee 13573 file_mips_opts.isa = ISA_MIPS32R2;
af7ee8bf
CD
13574 break;
13575
ae52f483 13576 case OPTION_MIPS32R3:
0ae19f05 13577 file_mips_opts.isa = ISA_MIPS32R3;
ae52f483
AB
13578 break;
13579
13580 case OPTION_MIPS32R5:
0ae19f05 13581 file_mips_opts.isa = ISA_MIPS32R5;
ae52f483
AB
13582 break;
13583
5f74bc13 13584 case OPTION_MIPS64R2:
0b35dfee 13585 file_mips_opts.isa = ISA_MIPS64R2;
5f74bc13
CD
13586 break;
13587
ae52f483 13588 case OPTION_MIPS64R3:
0ae19f05 13589 file_mips_opts.isa = ISA_MIPS64R3;
ae52f483
AB
13590 break;
13591
13592 case OPTION_MIPS64R5:
0ae19f05 13593 file_mips_opts.isa = ISA_MIPS64R5;
ae52f483
AB
13594 break;
13595
84ea6cf2 13596 case OPTION_MIPS64:
0b35dfee 13597 file_mips_opts.isa = ISA_MIPS64;
84ea6cf2
NC
13598 break;
13599
ec68c924 13600 case OPTION_MTUNE:
316f5878
RS
13601 mips_set_option_string (&mips_tune_string, arg);
13602 break;
ec68c924 13603
316f5878
RS
13604 case OPTION_MARCH:
13605 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
13606 break;
13607
13608 case OPTION_M4650:
316f5878
RS
13609 mips_set_option_string (&mips_arch_string, "4650");
13610 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
13611 break;
13612
13613 case OPTION_NO_M4650:
13614 break;
13615
13616 case OPTION_M4010:
316f5878
RS
13617 mips_set_option_string (&mips_arch_string, "4010");
13618 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
13619 break;
13620
13621 case OPTION_NO_M4010:
13622 break;
13623
13624 case OPTION_M4100:
316f5878
RS
13625 mips_set_option_string (&mips_arch_string, "4100");
13626 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
13627 break;
13628
13629 case OPTION_NO_M4100:
13630 break;
13631
252b5132 13632 case OPTION_M3900:
316f5878
RS
13633 mips_set_option_string (&mips_arch_string, "3900");
13634 mips_set_option_string (&mips_tune_string, "3900");
252b5132 13635 break;
bdaaa2e1 13636
252b5132
RH
13637 case OPTION_NO_M3900:
13638 break;
13639
df58fc94
RS
13640 case OPTION_MICROMIPS:
13641 if (mips_opts.mips16 == 1)
13642 {
13643 as_bad (_("-mmicromips cannot be used with -mips16"));
13644 return 0;
13645 }
13646 mips_opts.micromips = 1;
13647 mips_no_prev_insn ();
13648 break;
13649
13650 case OPTION_NO_MICROMIPS:
13651 mips_opts.micromips = 0;
13652 mips_no_prev_insn ();
13653 break;
13654
252b5132 13655 case OPTION_MIPS16:
df58fc94
RS
13656 if (mips_opts.micromips == 1)
13657 {
13658 as_bad (_("-mips16 cannot be used with -micromips"));
13659 return 0;
13660 }
252b5132 13661 mips_opts.mips16 = 1;
7d10b47d 13662 mips_no_prev_insn ();
252b5132
RH
13663 break;
13664
13665 case OPTION_NO_MIPS16:
13666 mips_opts.mips16 = 0;
7d10b47d 13667 mips_no_prev_insn ();
252b5132
RH
13668 break;
13669
6a32d874
CM
13670 case OPTION_FIX_24K:
13671 mips_fix_24k = 1;
13672 break;
13673
13674 case OPTION_NO_FIX_24K:
13675 mips_fix_24k = 0;
13676 break;
13677
a8d14a88
CM
13678 case OPTION_FIX_RM7000:
13679 mips_fix_rm7000 = 1;
13680 break;
13681
13682 case OPTION_NO_FIX_RM7000:
13683 mips_fix_rm7000 = 0;
13684 break;
13685
c67a084a
NC
13686 case OPTION_FIX_LOONGSON2F_JUMP:
13687 mips_fix_loongson2f_jump = TRUE;
13688 break;
13689
13690 case OPTION_NO_FIX_LOONGSON2F_JUMP:
13691 mips_fix_loongson2f_jump = FALSE;
13692 break;
13693
13694 case OPTION_FIX_LOONGSON2F_NOP:
13695 mips_fix_loongson2f_nop = TRUE;
13696 break;
13697
13698 case OPTION_NO_FIX_LOONGSON2F_NOP:
13699 mips_fix_loongson2f_nop = FALSE;
13700 break;
13701
d766e8ec
RS
13702 case OPTION_FIX_VR4120:
13703 mips_fix_vr4120 = 1;
60b63b72
RS
13704 break;
13705
d766e8ec
RS
13706 case OPTION_NO_FIX_VR4120:
13707 mips_fix_vr4120 = 0;
60b63b72
RS
13708 break;
13709
7d8e00cf
RS
13710 case OPTION_FIX_VR4130:
13711 mips_fix_vr4130 = 1;
13712 break;
13713
13714 case OPTION_NO_FIX_VR4130:
13715 mips_fix_vr4130 = 0;
13716 break;
13717
d954098f
DD
13718 case OPTION_FIX_CN63XXP1:
13719 mips_fix_cn63xxp1 = TRUE;
13720 break;
13721
13722 case OPTION_NO_FIX_CN63XXP1:
13723 mips_fix_cn63xxp1 = FALSE;
13724 break;
13725
4a6a3df4
AO
13726 case OPTION_RELAX_BRANCH:
13727 mips_relax_branch = 1;
13728 break;
13729
13730 case OPTION_NO_RELAX_BRANCH:
13731 mips_relax_branch = 0;
13732 break;
13733
833794fc
MR
13734 case OPTION_INSN32:
13735 mips_opts.insn32 = TRUE;
13736 break;
13737
13738 case OPTION_NO_INSN32:
13739 mips_opts.insn32 = FALSE;
13740 break;
13741
aa6975fb
ILT
13742 case OPTION_MSHARED:
13743 mips_in_shared = TRUE;
13744 break;
13745
13746 case OPTION_MNO_SHARED:
13747 mips_in_shared = FALSE;
13748 break;
13749
aed1a261
RS
13750 case OPTION_MSYM32:
13751 mips_opts.sym32 = TRUE;
13752 break;
13753
13754 case OPTION_MNO_SYM32:
13755 mips_opts.sym32 = FALSE;
13756 break;
13757
252b5132
RH
13758 /* When generating ELF code, we permit -KPIC and -call_shared to
13759 select SVR4_PIC, and -non_shared to select no PIC. This is
13760 intended to be compatible with Irix 5. */
13761 case OPTION_CALL_SHARED:
252b5132 13762 mips_pic = SVR4_PIC;
143d77c5 13763 mips_abicalls = TRUE;
252b5132
RH
13764 break;
13765
861fb55a 13766 case OPTION_CALL_NONPIC:
861fb55a
DJ
13767 mips_pic = NO_PIC;
13768 mips_abicalls = TRUE;
13769 break;
13770
252b5132 13771 case OPTION_NON_SHARED:
252b5132 13772 mips_pic = NO_PIC;
143d77c5 13773 mips_abicalls = FALSE;
252b5132
RH
13774 break;
13775
44075ae2
TS
13776 /* The -xgot option tells the assembler to use 32 bit offsets
13777 when accessing the got in SVR4_PIC mode. It is for Irix
252b5132
RH
13778 compatibility. */
13779 case OPTION_XGOT:
13780 mips_big_got = 1;
13781 break;
13782
13783 case 'G':
6caf9ef4
TS
13784 g_switch_value = atoi (arg);
13785 g_switch_seen = 1;
252b5132
RH
13786 break;
13787
34ba82a8
TS
13788 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
13789 and -mabi=64. */
252b5132 13790 case OPTION_32:
f3ded42a 13791 mips_abi = O32_ABI;
252b5132
RH
13792 break;
13793
e013f690 13794 case OPTION_N32:
316f5878 13795 mips_abi = N32_ABI;
e013f690 13796 break;
252b5132 13797
e013f690 13798 case OPTION_64:
316f5878 13799 mips_abi = N64_ABI;
f43abd2b 13800 if (!support_64bit_objects())
1661c76c 13801 as_fatal (_("no compiled in support for 64 bit object file format"));
252b5132
RH
13802 break;
13803
c97ef257 13804 case OPTION_GP32:
bad1aba3 13805 file_mips_opts.gp = 32;
c97ef257
AH
13806 break;
13807
13808 case OPTION_GP64:
bad1aba3 13809 file_mips_opts.gp = 64;
c97ef257 13810 break;
252b5132 13811
ca4e0257 13812 case OPTION_FP32:
0b35dfee 13813 file_mips_opts.fp = 32;
316f5878
RS
13814 break;
13815
13816 case OPTION_FP64:
0b35dfee 13817 file_mips_opts.fp = 64;
ca4e0257
RS
13818 break;
13819
037b32b9 13820 case OPTION_SINGLE_FLOAT:
0b35dfee 13821 file_mips_opts.single_float = 1;
037b32b9
AN
13822 break;
13823
13824 case OPTION_DOUBLE_FLOAT:
0b35dfee 13825 file_mips_opts.single_float = 0;
037b32b9
AN
13826 break;
13827
13828 case OPTION_SOFT_FLOAT:
0b35dfee 13829 file_mips_opts.soft_float = 1;
037b32b9
AN
13830 break;
13831
13832 case OPTION_HARD_FLOAT:
0b35dfee 13833 file_mips_opts.soft_float = 0;
037b32b9
AN
13834 break;
13835
252b5132 13836 case OPTION_MABI:
e013f690 13837 if (strcmp (arg, "32") == 0)
316f5878 13838 mips_abi = O32_ABI;
e013f690 13839 else if (strcmp (arg, "o64") == 0)
316f5878 13840 mips_abi = O64_ABI;
e013f690 13841 else if (strcmp (arg, "n32") == 0)
316f5878 13842 mips_abi = N32_ABI;
e013f690
TS
13843 else if (strcmp (arg, "64") == 0)
13844 {
316f5878 13845 mips_abi = N64_ABI;
e013f690 13846 if (! support_64bit_objects())
1661c76c 13847 as_fatal (_("no compiled in support for 64 bit object file "
e013f690
TS
13848 "format"));
13849 }
13850 else if (strcmp (arg, "eabi") == 0)
316f5878 13851 mips_abi = EABI_ABI;
e013f690 13852 else
da0e507f
TS
13853 {
13854 as_fatal (_("invalid abi -mabi=%s"), arg);
13855 return 0;
13856 }
252b5132
RH
13857 break;
13858
6b76fefe 13859 case OPTION_M7000_HILO_FIX:
b34976b6 13860 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
13861 break;
13862
9ee72ff1 13863 case OPTION_MNO_7000_HILO_FIX:
b34976b6 13864 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
13865 break;
13866
ecb4347a 13867 case OPTION_MDEBUG:
b34976b6 13868 mips_flag_mdebug = TRUE;
ecb4347a
DJ
13869 break;
13870
13871 case OPTION_NO_MDEBUG:
b34976b6 13872 mips_flag_mdebug = FALSE;
ecb4347a 13873 break;
dcd410fe
RO
13874
13875 case OPTION_PDR:
13876 mips_flag_pdr = TRUE;
13877 break;
13878
13879 case OPTION_NO_PDR:
13880 mips_flag_pdr = FALSE;
13881 break;
0a44bf69
RS
13882
13883 case OPTION_MVXWORKS_PIC:
13884 mips_pic = VXWORKS_PIC;
13885 break;
ecb4347a 13886
ba92f887
MR
13887 case OPTION_NAN:
13888 if (strcmp (arg, "2008") == 0)
13889 mips_flag_nan2008 = TRUE;
13890 else if (strcmp (arg, "legacy") == 0)
13891 mips_flag_nan2008 = FALSE;
13892 else
13893 {
1661c76c 13894 as_fatal (_("invalid NaN setting -mnan=%s"), arg);
ba92f887
MR
13895 return 0;
13896 }
13897 break;
13898
252b5132
RH
13899 default:
13900 return 0;
13901 }
13902
c67a084a
NC
13903 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
13904
252b5132
RH
13905 return 1;
13906}
316f5878
RS
13907\f
13908/* Set up globals to generate code for the ISA or processor
13909 described by INFO. */
252b5132 13910
252b5132 13911static void
17a2f251 13912mips_set_architecture (const struct mips_cpu_info *info)
252b5132 13913{
316f5878 13914 if (info != 0)
252b5132 13915 {
0b35dfee 13916 file_mips_opts.arch = info->cpu;
fef14a42 13917 mips_opts.arch = info->cpu;
316f5878 13918 mips_opts.isa = info->isa;
252b5132 13919 }
252b5132
RH
13920}
13921
252b5132 13922
316f5878 13923/* Likewise for tuning. */
252b5132 13924
316f5878 13925static void
17a2f251 13926mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
13927{
13928 if (info != 0)
fef14a42 13929 mips_tune = info->cpu;
316f5878 13930}
80cc45a5 13931
34ba82a8 13932
252b5132 13933void
17a2f251 13934mips_after_parse_args (void)
e9670677 13935{
fef14a42
TS
13936 const struct mips_cpu_info *arch_info = 0;
13937 const struct mips_cpu_info *tune_info = 0;
13938
e9670677 13939 /* GP relative stuff not working for PE */
6caf9ef4 13940 if (strncmp (TARGET_OS, "pe", 2) == 0)
e9670677 13941 {
6caf9ef4 13942 if (g_switch_seen && g_switch_value != 0)
1661c76c 13943 as_bad (_("-G not supported in this configuration"));
e9670677
MR
13944 g_switch_value = 0;
13945 }
13946
cac012d6
AO
13947 if (mips_abi == NO_ABI)
13948 mips_abi = MIPS_DEFAULT_ABI;
13949
22923709
RS
13950 /* The following code determines the architecture and register size.
13951 Similar code was added to GCC 3.3 (see override_options() in
13952 config/mips/mips.c). The GAS and GCC code should be kept in sync
13953 as much as possible. */
e9670677 13954
316f5878 13955 if (mips_arch_string != 0)
fef14a42 13956 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 13957
0b35dfee 13958 if (file_mips_opts.isa != ISA_UNKNOWN)
e9670677 13959 {
0b35dfee 13960 /* Handle -mipsN. At this point, file_mips_opts.isa contains the
fef14a42 13961 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 13962 the -march selection (if any). */
fef14a42 13963 if (arch_info != 0)
e9670677 13964 {
316f5878
RS
13965 /* -march takes precedence over -mipsN, since it is more descriptive.
13966 There's no harm in specifying both as long as the ISA levels
13967 are the same. */
0b35dfee 13968 if (file_mips_opts.isa != arch_info->isa)
1661c76c
RS
13969 as_bad (_("-%s conflicts with the other architecture options,"
13970 " which imply -%s"),
0b35dfee 13971 mips_cpu_info_from_isa (file_mips_opts.isa)->name,
fef14a42 13972 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 13973 }
316f5878 13974 else
0b35dfee 13975 arch_info = mips_cpu_info_from_isa (file_mips_opts.isa);
e9670677
MR
13976 }
13977
fef14a42 13978 if (arch_info == 0)
95bfe26e
MF
13979 {
13980 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
13981 gas_assert (arch_info);
13982 }
e9670677 13983
fef14a42 13984 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
20203fb9 13985 as_bad (_("-march=%s is not compatible with the selected ABI"),
fef14a42
TS
13986 arch_info->name);
13987
13988 mips_set_architecture (arch_info);
13989
0b35dfee 13990 /* Optimize for file_mips_opts.arch, unless -mtune selects a different
13991 processor. */
fef14a42
TS
13992 if (mips_tune_string != 0)
13993 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 13994
fef14a42
TS
13995 if (tune_info == 0)
13996 mips_set_tune (arch_info);
13997 else
13998 mips_set_tune (tune_info);
e9670677 13999
bad1aba3 14000 if (file_mips_opts.gp >= 0)
e9670677 14001 {
316f5878
RS
14002 /* The user specified the size of the integer registers. Make sure
14003 it agrees with the ABI and ISA. */
bad1aba3 14004 if (file_mips_opts.gp == 64 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
316f5878 14005 as_bad (_("-mgp64 used with a 32-bit processor"));
bad1aba3 14006 else if (file_mips_opts.gp == 32 && ABI_NEEDS_64BIT_REGS (mips_abi))
316f5878 14007 as_bad (_("-mgp32 used with a 64-bit ABI"));
bad1aba3 14008 else if (file_mips_opts.gp == 64 && ABI_NEEDS_32BIT_REGS (mips_abi))
316f5878 14009 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
14010 }
14011 else
14012 {
316f5878
RS
14013 /* Infer the integer register size from the ABI and processor.
14014 Restrict ourselves to 32-bit registers if that's all the
14015 processor has, or if the ABI cannot handle 64-bit registers. */
bad1aba3 14016 file_mips_opts.gp = (ABI_NEEDS_32BIT_REGS (mips_abi)
14017 || !ISA_HAS_64BIT_REGS (mips_opts.isa))
14018 ? 32 : 64;
e9670677
MR
14019 }
14020
0b35dfee 14021 switch (file_mips_opts.fp)
ad3fea08
TS
14022 {
14023 default:
14024 case -1:
14025 /* No user specified float register size.
14026 ??? GAS treats single-float processors as though they had 64-bit
14027 float registers (although it complains when double-precision
14028 instructions are used). As things stand, saying they have 32-bit
14029 registers would lead to spurious "register must be even" messages.
14030 So here we assume float registers are never smaller than the
14031 integer ones. */
bad1aba3 14032 if (file_mips_opts.gp == 64)
ad3fea08 14033 /* 64-bit integer registers implies 64-bit float registers. */
0b35dfee 14034 file_mips_opts.fp = 64;
c6278170 14035 else if ((mips_opts.ase & FP64_ASES)
ad3fea08
TS
14036 && ISA_HAS_64BIT_FPRS (mips_opts.isa))
14037 /* -mips3d and -mdmx imply 64-bit float registers, if possible. */
0b35dfee 14038 file_mips_opts.fp = 64;
ad3fea08
TS
14039 else
14040 /* 32-bit float registers. */
0b35dfee 14041 file_mips_opts.fp = 32;
ad3fea08
TS
14042 break;
14043
14044 /* The user specified the size of the float registers. Check if it
14045 agrees with the ABI and ISA. */
0b35dfee 14046 case 64:
ad3fea08
TS
14047 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
14048 as_bad (_("-mfp64 used with a 32-bit fpu"));
14049 else if (ABI_NEEDS_32BIT_REGS (mips_abi)
14050 && !ISA_HAS_MXHC1 (mips_opts.isa))
14051 as_warn (_("-mfp64 used with a 32-bit ABI"));
14052 break;
0b35dfee 14053 case 32:
ad3fea08
TS
14054 if (ABI_NEEDS_64BIT_REGS (mips_abi))
14055 as_warn (_("-mfp32 used with a 64-bit ABI"));
14056 break;
14057 }
e9670677 14058
316f5878 14059 /* End of GCC-shared inference code. */
e9670677 14060
17a2f251
TS
14061 /* This flag is set when we have a 64-bit capable CPU but use only
14062 32-bit wide registers. Note that EABI does not use it. */
14063 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
bad1aba3 14064 && ((mips_abi == NO_ABI && file_mips_opts.gp == 32)
17a2f251 14065 || mips_abi == O32_ABI))
316f5878 14066 mips_32bitmode = 1;
e9670677
MR
14067
14068 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
14069 as_bad (_("trap exception not supported at ISA 1"));
14070
e9670677
MR
14071 /* If the selected architecture includes support for ASEs, enable
14072 generation of code for them. */
a4672219 14073 if (mips_opts.mips16 == -1)
0b35dfee 14074 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_opts.arch)) ? 1 : 0;
df58fc94 14075 if (mips_opts.micromips == -1)
0b35dfee 14076 mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_opts.arch))
14077 ? 1 : 0;
846ef2d0
RS
14078
14079 /* MIPS3D and MDMX require 64-bit FPRs, so -mfp32 should stop those
14080 ASEs from being selected implicitly. */
0b35dfee 14081 if (file_mips_opts.fp != 64)
846ef2d0
RS
14082 file_ase_explicit |= ASE_MIPS3D | ASE_MDMX;
14083
14084 /* If the user didn't explicitly select or deselect a particular ASE,
14085 use the default setting for the CPU. */
14086 mips_opts.ase |= (arch_info->ase & ~file_ase_explicit);
14087
0b35dfee 14088 file_mips_opts.isa = mips_opts.isa;
14089 file_mips_opts.ase = mips_opts.ase;
bad1aba3 14090 mips_opts.gp = file_mips_opts.gp;
0b35dfee 14091 mips_opts.fp = file_mips_opts.fp;
14092 mips_opts.soft_float = file_mips_opts.soft_float;
14093 mips_opts.single_float = file_mips_opts.single_float;
e9670677 14094
c6278170
RS
14095 mips_check_isa_supports_ases ();
14096
ecb4347a 14097 if (mips_flag_mdebug < 0)
e8044f35 14098 mips_flag_mdebug = 0;
e9670677
MR
14099}
14100\f
14101void
17a2f251 14102mips_init_after_args (void)
252b5132
RH
14103{
14104 /* initialize opcodes */
14105 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 14106 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
14107}
14108
14109long
17a2f251 14110md_pcrel_from (fixS *fixP)
252b5132 14111{
a7ebbfdf
TS
14112 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
14113 switch (fixP->fx_r_type)
14114 {
df58fc94
RS
14115 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
14116 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
14117 /* Return the address of the delay slot. */
14118 return addr + 2;
14119
14120 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
14121 case BFD_RELOC_MICROMIPS_JMP:
a7ebbfdf
TS
14122 case BFD_RELOC_16_PCREL_S2:
14123 case BFD_RELOC_MIPS_JMP:
14124 /* Return the address of the delay slot. */
14125 return addr + 4;
df58fc94 14126
a7ebbfdf
TS
14127 default:
14128 return addr;
14129 }
252b5132
RH
14130}
14131
252b5132
RH
14132/* This is called before the symbol table is processed. In order to
14133 work with gcc when using mips-tfile, we must keep all local labels.
14134 However, in other cases, we want to discard them. If we were
14135 called with -g, but we didn't see any debugging information, it may
14136 mean that gcc is smuggling debugging information through to
14137 mips-tfile, in which case we must generate all local labels. */
14138
14139void
17a2f251 14140mips_frob_file_before_adjust (void)
252b5132
RH
14141{
14142#ifndef NO_ECOFF_DEBUGGING
14143 if (ECOFF_DEBUGGING
14144 && mips_debug != 0
14145 && ! ecoff_debugging_seen)
14146 flag_keep_locals = 1;
14147#endif
14148}
14149
3b91255e 14150/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 14151 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
14152 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
14153 relocation operators.
14154
14155 For our purposes, a %lo() expression matches a %got() or %hi()
14156 expression if:
14157
14158 (a) it refers to the same symbol; and
14159 (b) the offset applied in the %lo() expression is no lower than
14160 the offset applied in the %got() or %hi().
14161
14162 (b) allows us to cope with code like:
14163
14164 lui $4,%hi(foo)
14165 lh $4,%lo(foo+2)($4)
14166
14167 ...which is legal on RELA targets, and has a well-defined behaviour
14168 if the user knows that adding 2 to "foo" will not induce a carry to
14169 the high 16 bits.
14170
14171 When several %lo()s match a particular %got() or %hi(), we use the
14172 following rules to distinguish them:
14173
14174 (1) %lo()s with smaller offsets are a better match than %lo()s with
14175 higher offsets.
14176
14177 (2) %lo()s with no matching %got() or %hi() are better than those
14178 that already have a matching %got() or %hi().
14179
14180 (3) later %lo()s are better than earlier %lo()s.
14181
14182 These rules are applied in order.
14183
14184 (1) means, among other things, that %lo()s with identical offsets are
14185 chosen if they exist.
14186
14187 (2) means that we won't associate several high-part relocations with
14188 the same low-part relocation unless there's no alternative. Having
14189 several high parts for the same low part is a GNU extension; this rule
14190 allows careful users to avoid it.
14191
14192 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
14193 with the last high-part relocation being at the front of the list.
14194 It therefore makes sense to choose the last matching low-part
14195 relocation, all other things being equal. It's also easier
14196 to code that way. */
252b5132
RH
14197
14198void
17a2f251 14199mips_frob_file (void)
252b5132
RH
14200{
14201 struct mips_hi_fixup *l;
35903be0 14202 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
252b5132
RH
14203
14204 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
14205 {
14206 segment_info_type *seginfo;
3b91255e
RS
14207 bfd_boolean matched_lo_p;
14208 fixS **hi_pos, **lo_pos, **pos;
252b5132 14209
9c2799c2 14210 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 14211
5919d012 14212 /* If a GOT16 relocation turns out to be against a global symbol,
b886a2ab
RS
14213 there isn't supposed to be a matching LO. Ignore %gots against
14214 constants; we'll report an error for those later. */
738e5348 14215 if (got16_reloc_p (l->fixp->fx_r_type)
b886a2ab
RS
14216 && !(l->fixp->fx_addsy
14217 && pic_need_relax (l->fixp->fx_addsy, l->seg)))
5919d012
RS
14218 continue;
14219
14220 /* Check quickly whether the next fixup happens to be a matching %lo. */
14221 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
14222 continue;
14223
252b5132 14224 seginfo = seg_info (l->seg);
252b5132 14225
3b91255e
RS
14226 /* Set HI_POS to the position of this relocation in the chain.
14227 Set LO_POS to the position of the chosen low-part relocation.
14228 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
14229 relocation that matches an immediately-preceding high-part
14230 relocation. */
14231 hi_pos = NULL;
14232 lo_pos = NULL;
14233 matched_lo_p = FALSE;
738e5348 14234 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
35903be0 14235
3b91255e
RS
14236 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
14237 {
14238 if (*pos == l->fixp)
14239 hi_pos = pos;
14240
35903be0 14241 if ((*pos)->fx_r_type == looking_for_rtype
30cfc97a 14242 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
3b91255e
RS
14243 && (*pos)->fx_offset >= l->fixp->fx_offset
14244 && (lo_pos == NULL
14245 || (*pos)->fx_offset < (*lo_pos)->fx_offset
14246 || (!matched_lo_p
14247 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
14248 lo_pos = pos;
14249
14250 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
14251 && fixup_has_matching_lo_p (*pos));
14252 }
14253
14254 /* If we found a match, remove the high-part relocation from its
14255 current position and insert it before the low-part relocation.
14256 Make the offsets match so that fixup_has_matching_lo_p()
14257 will return true.
14258
14259 We don't warn about unmatched high-part relocations since some
14260 versions of gcc have been known to emit dead "lui ...%hi(...)"
14261 instructions. */
14262 if (lo_pos != NULL)
14263 {
14264 l->fixp->fx_offset = (*lo_pos)->fx_offset;
14265 if (l->fixp->fx_next != *lo_pos)
252b5132 14266 {
3b91255e
RS
14267 *hi_pos = l->fixp->fx_next;
14268 l->fixp->fx_next = *lo_pos;
14269 *lo_pos = l->fixp;
252b5132 14270 }
252b5132
RH
14271 }
14272 }
14273}
14274
252b5132 14275int
17a2f251 14276mips_force_relocation (fixS *fixp)
252b5132 14277{
ae6063d4 14278 if (generic_force_reloc (fixp))
252b5132
RH
14279 return 1;
14280
df58fc94
RS
14281 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
14282 so that the linker relaxation can update targets. */
14283 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
14284 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
14285 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
14286 return 1;
14287
3e722fb5 14288 return 0;
252b5132
RH
14289}
14290
b886a2ab
RS
14291/* Read the instruction associated with RELOC from BUF. */
14292
14293static unsigned int
14294read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
14295{
14296 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
14297 return read_compressed_insn (buf, 4);
14298 else
14299 return read_insn (buf);
14300}
14301
14302/* Write instruction INSN to BUF, given that it has been relocated
14303 by RELOC. */
14304
14305static void
14306write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
14307 unsigned long insn)
14308{
14309 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
14310 write_compressed_insn (buf, insn, 4);
14311 else
14312 write_insn (buf, insn);
14313}
14314
252b5132
RH
14315/* Apply a fixup to the object file. */
14316
94f592af 14317void
55cf6793 14318md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 14319{
4d68580a 14320 char *buf;
b886a2ab 14321 unsigned long insn;
a7ebbfdf 14322 reloc_howto_type *howto;
252b5132 14323
d56a8dda
RS
14324 if (fixP->fx_pcrel)
14325 switch (fixP->fx_r_type)
14326 {
14327 case BFD_RELOC_16_PCREL_S2:
14328 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
14329 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
14330 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
14331 case BFD_RELOC_32_PCREL:
14332 break;
14333
14334 case BFD_RELOC_32:
14335 fixP->fx_r_type = BFD_RELOC_32_PCREL;
14336 break;
14337
14338 default:
14339 as_bad_where (fixP->fx_file, fixP->fx_line,
14340 _("PC-relative reference to a different section"));
14341 break;
14342 }
14343
14344 /* Handle BFD_RELOC_8, since it's easy. Punt on other bfd relocations
14345 that have no MIPS ELF equivalent. */
14346 if (fixP->fx_r_type != BFD_RELOC_8)
14347 {
14348 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
14349 if (!howto)
14350 return;
14351 }
65551fa4 14352
df58fc94
RS
14353 gas_assert (fixP->fx_size == 2
14354 || fixP->fx_size == 4
d56a8dda 14355 || fixP->fx_r_type == BFD_RELOC_8
90ecf173
MR
14356 || fixP->fx_r_type == BFD_RELOC_16
14357 || fixP->fx_r_type == BFD_RELOC_64
14358 || fixP->fx_r_type == BFD_RELOC_CTOR
14359 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
df58fc94 14360 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
90ecf173
MR
14361 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
14362 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
14363 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64);
252b5132 14364
4d68580a 14365 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132 14366
b1dca8ee
RS
14367 /* Don't treat parts of a composite relocation as done. There are two
14368 reasons for this:
14369
14370 (1) The second and third parts will be against 0 (RSS_UNDEF) but
14371 should nevertheless be emitted if the first part is.
14372
14373 (2) In normal usage, composite relocations are never assembly-time
14374 constants. The easiest way of dealing with the pathological
14375 exceptions is to generate a relocation against STN_UNDEF and
14376 leave everything up to the linker. */
3994f87e 14377 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
252b5132
RH
14378 fixP->fx_done = 1;
14379
14380 switch (fixP->fx_r_type)
14381 {
3f98094e
DJ
14382 case BFD_RELOC_MIPS_TLS_GD:
14383 case BFD_RELOC_MIPS_TLS_LDM:
741d6ea8
JM
14384 case BFD_RELOC_MIPS_TLS_DTPREL32:
14385 case BFD_RELOC_MIPS_TLS_DTPREL64:
3f98094e
DJ
14386 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
14387 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
14388 case BFD_RELOC_MIPS_TLS_GOTTPREL:
d0f13682
CLT
14389 case BFD_RELOC_MIPS_TLS_TPREL32:
14390 case BFD_RELOC_MIPS_TLS_TPREL64:
3f98094e
DJ
14391 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
14392 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
df58fc94
RS
14393 case BFD_RELOC_MICROMIPS_TLS_GD:
14394 case BFD_RELOC_MICROMIPS_TLS_LDM:
14395 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
14396 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
14397 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
14398 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
14399 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
d0f13682
CLT
14400 case BFD_RELOC_MIPS16_TLS_GD:
14401 case BFD_RELOC_MIPS16_TLS_LDM:
14402 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
14403 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
14404 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
14405 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
14406 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
b886a2ab
RS
14407 if (!fixP->fx_addsy)
14408 {
14409 as_bad_where (fixP->fx_file, fixP->fx_line,
14410 _("TLS relocation against a constant"));
14411 break;
14412 }
3f98094e
DJ
14413 S_SET_THREAD_LOCAL (fixP->fx_addsy);
14414 /* fall through */
14415
252b5132 14416 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
14417 case BFD_RELOC_MIPS_SHIFT5:
14418 case BFD_RELOC_MIPS_SHIFT6:
14419 case BFD_RELOC_MIPS_GOT_DISP:
14420 case BFD_RELOC_MIPS_GOT_PAGE:
14421 case BFD_RELOC_MIPS_GOT_OFST:
14422 case BFD_RELOC_MIPS_SUB:
14423 case BFD_RELOC_MIPS_INSERT_A:
14424 case BFD_RELOC_MIPS_INSERT_B:
14425 case BFD_RELOC_MIPS_DELETE:
14426 case BFD_RELOC_MIPS_HIGHEST:
14427 case BFD_RELOC_MIPS_HIGHER:
14428 case BFD_RELOC_MIPS_SCN_DISP:
14429 case BFD_RELOC_MIPS_REL16:
14430 case BFD_RELOC_MIPS_RELGOT:
14431 case BFD_RELOC_MIPS_JALR:
252b5132
RH
14432 case BFD_RELOC_HI16:
14433 case BFD_RELOC_HI16_S:
b886a2ab 14434 case BFD_RELOC_LO16:
cdf6fd85 14435 case BFD_RELOC_GPREL16:
252b5132
RH
14436 case BFD_RELOC_MIPS_LITERAL:
14437 case BFD_RELOC_MIPS_CALL16:
14438 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 14439 case BFD_RELOC_GPREL32:
252b5132
RH
14440 case BFD_RELOC_MIPS_GOT_HI16:
14441 case BFD_RELOC_MIPS_GOT_LO16:
14442 case BFD_RELOC_MIPS_CALL_HI16:
14443 case BFD_RELOC_MIPS_CALL_LO16:
14444 case BFD_RELOC_MIPS16_GPREL:
738e5348
RS
14445 case BFD_RELOC_MIPS16_GOT16:
14446 case BFD_RELOC_MIPS16_CALL16:
d6f16593
MR
14447 case BFD_RELOC_MIPS16_HI16:
14448 case BFD_RELOC_MIPS16_HI16_S:
b886a2ab 14449 case BFD_RELOC_MIPS16_LO16:
252b5132 14450 case BFD_RELOC_MIPS16_JMP:
df58fc94
RS
14451 case BFD_RELOC_MICROMIPS_JMP:
14452 case BFD_RELOC_MICROMIPS_GOT_DISP:
14453 case BFD_RELOC_MICROMIPS_GOT_PAGE:
14454 case BFD_RELOC_MICROMIPS_GOT_OFST:
14455 case BFD_RELOC_MICROMIPS_SUB:
14456 case BFD_RELOC_MICROMIPS_HIGHEST:
14457 case BFD_RELOC_MICROMIPS_HIGHER:
14458 case BFD_RELOC_MICROMIPS_SCN_DISP:
14459 case BFD_RELOC_MICROMIPS_JALR:
14460 case BFD_RELOC_MICROMIPS_HI16:
14461 case BFD_RELOC_MICROMIPS_HI16_S:
b886a2ab 14462 case BFD_RELOC_MICROMIPS_LO16:
df58fc94
RS
14463 case BFD_RELOC_MICROMIPS_GPREL16:
14464 case BFD_RELOC_MICROMIPS_LITERAL:
14465 case BFD_RELOC_MICROMIPS_CALL16:
14466 case BFD_RELOC_MICROMIPS_GOT16:
14467 case BFD_RELOC_MICROMIPS_GOT_HI16:
14468 case BFD_RELOC_MICROMIPS_GOT_LO16:
14469 case BFD_RELOC_MICROMIPS_CALL_HI16:
14470 case BFD_RELOC_MICROMIPS_CALL_LO16:
067ec077 14471 case BFD_RELOC_MIPS_EH:
b886a2ab
RS
14472 if (fixP->fx_done)
14473 {
14474 offsetT value;
14475
14476 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
14477 {
14478 insn = read_reloc_insn (buf, fixP->fx_r_type);
14479 if (mips16_reloc_p (fixP->fx_r_type))
14480 insn |= mips16_immed_extend (value, 16);
14481 else
14482 insn |= (value & 0xffff);
14483 write_reloc_insn (buf, fixP->fx_r_type, insn);
14484 }
14485 else
14486 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 14487 _("unsupported constant in relocation"));
b886a2ab 14488 }
252b5132
RH
14489 break;
14490
252b5132
RH
14491 case BFD_RELOC_64:
14492 /* This is handled like BFD_RELOC_32, but we output a sign
14493 extended value if we are only 32 bits. */
3e722fb5 14494 if (fixP->fx_done)
252b5132
RH
14495 {
14496 if (8 <= sizeof (valueT))
4d68580a 14497 md_number_to_chars (buf, *valP, 8);
252b5132
RH
14498 else
14499 {
a7ebbfdf 14500 valueT hiv;
252b5132 14501
a7ebbfdf 14502 if ((*valP & 0x80000000) != 0)
252b5132
RH
14503 hiv = 0xffffffff;
14504 else
14505 hiv = 0;
4d68580a
RS
14506 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
14507 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
252b5132
RH
14508 }
14509 }
14510 break;
14511
056350c6 14512 case BFD_RELOC_RVA:
252b5132 14513 case BFD_RELOC_32:
b47468a6 14514 case BFD_RELOC_32_PCREL:
252b5132 14515 case BFD_RELOC_16:
d56a8dda 14516 case BFD_RELOC_8:
252b5132 14517 /* If we are deleting this reloc entry, we must fill in the
54f4ddb3
TS
14518 value now. This can happen if we have a .word which is not
14519 resolved when it appears but is later defined. */
252b5132 14520 if (fixP->fx_done)
4d68580a 14521 md_number_to_chars (buf, *valP, fixP->fx_size);
252b5132
RH
14522 break;
14523
252b5132 14524 case BFD_RELOC_16_PCREL_S2:
a7ebbfdf 14525 if ((*valP & 0x3) != 0)
cb56d3d3 14526 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 14527 _("branch to misaligned address (%lx)"), (long) *valP);
cb56d3d3 14528
54f4ddb3
TS
14529 /* We need to save the bits in the instruction since fixup_segment()
14530 might be deleting the relocation entry (i.e., a branch within
14531 the current segment). */
a7ebbfdf 14532 if (! fixP->fx_done)
bb2d6cd7 14533 break;
252b5132 14534
54f4ddb3 14535 /* Update old instruction data. */
4d68580a 14536 insn = read_insn (buf);
252b5132 14537
a7ebbfdf
TS
14538 if (*valP + 0x20000 <= 0x3ffff)
14539 {
14540 insn |= (*valP >> 2) & 0xffff;
4d68580a 14541 write_insn (buf, insn);
a7ebbfdf
TS
14542 }
14543 else if (mips_pic == NO_PIC
14544 && fixP->fx_done
14545 && fixP->fx_frag->fr_address >= text_section->vma
14546 && (fixP->fx_frag->fr_address
587aac4e 14547 < text_section->vma + bfd_get_section_size (text_section))
a7ebbfdf
TS
14548 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
14549 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
14550 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
14551 {
14552 /* The branch offset is too large. If this is an
14553 unconditional branch, and we are not generating PIC code,
14554 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
14555 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
14556 insn = 0x0c000000; /* jal */
252b5132 14557 else
a7ebbfdf
TS
14558 insn = 0x08000000; /* j */
14559 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
14560 fixP->fx_done = 0;
14561 fixP->fx_addsy = section_symbol (text_section);
14562 *valP += md_pcrel_from (fixP);
4d68580a 14563 write_insn (buf, insn);
a7ebbfdf
TS
14564 }
14565 else
14566 {
14567 /* If we got here, we have branch-relaxation disabled,
14568 and there's nothing we can do to fix this instruction
14569 without turning it into a longer sequence. */
14570 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 14571 _("branch out of range"));
252b5132 14572 }
252b5132
RH
14573 break;
14574
df58fc94
RS
14575 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
14576 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
14577 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
14578 /* We adjust the offset back to even. */
14579 if ((*valP & 0x1) != 0)
14580 --(*valP);
14581
14582 if (! fixP->fx_done)
14583 break;
14584
14585 /* Should never visit here, because we keep the relocation. */
14586 abort ();
14587 break;
14588
252b5132
RH
14589 case BFD_RELOC_VTABLE_INHERIT:
14590 fixP->fx_done = 0;
14591 if (fixP->fx_addsy
14592 && !S_IS_DEFINED (fixP->fx_addsy)
14593 && !S_IS_WEAK (fixP->fx_addsy))
14594 S_SET_WEAK (fixP->fx_addsy);
14595 break;
14596
14597 case BFD_RELOC_VTABLE_ENTRY:
14598 fixP->fx_done = 0;
14599 break;
14600
14601 default:
b37df7c4 14602 abort ();
252b5132 14603 }
a7ebbfdf
TS
14604
14605 /* Remember value for tc_gen_reloc. */
14606 fixP->fx_addnumber = *valP;
252b5132
RH
14607}
14608
252b5132 14609static symbolS *
17a2f251 14610get_symbol (void)
252b5132
RH
14611{
14612 int c;
14613 char *name;
14614 symbolS *p;
14615
14616 name = input_line_pointer;
14617 c = get_symbol_end ();
14618 p = (symbolS *) symbol_find_or_make (name);
14619 *input_line_pointer = c;
14620 return p;
14621}
14622
742a56fe
RS
14623/* Align the current frag to a given power of two. If a particular
14624 fill byte should be used, FILL points to an integer that contains
14625 that byte, otherwise FILL is null.
14626
462427c4
RS
14627 This function used to have the comment:
14628
14629 The MIPS assembler also automatically adjusts any preceding label.
14630
14631 The implementation therefore applied the adjustment to a maximum of
14632 one label. However, other label adjustments are applied to batches
14633 of labels, and adjusting just one caused problems when new labels
14634 were added for the sake of debugging or unwind information.
14635 We therefore adjust all preceding labels (given as LABELS) instead. */
252b5132
RH
14636
14637static void
462427c4 14638mips_align (int to, int *fill, struct insn_label_list *labels)
252b5132 14639{
7d10b47d 14640 mips_emit_delays ();
df58fc94 14641 mips_record_compressed_mode ();
742a56fe
RS
14642 if (fill == NULL && subseg_text_p (now_seg))
14643 frag_align_code (to, 0);
14644 else
14645 frag_align (to, fill ? *fill : 0, 0);
252b5132 14646 record_alignment (now_seg, to);
462427c4 14647 mips_move_labels (labels, FALSE);
252b5132
RH
14648}
14649
14650/* Align to a given power of two. .align 0 turns off the automatic
14651 alignment used by the data creating pseudo-ops. */
14652
14653static void
17a2f251 14654s_align (int x ATTRIBUTE_UNUSED)
252b5132 14655{
742a56fe 14656 int temp, fill_value, *fill_ptr;
49954fb4 14657 long max_alignment = 28;
252b5132 14658
54f4ddb3 14659 /* o Note that the assembler pulls down any immediately preceding label
252b5132 14660 to the aligned address.
54f4ddb3 14661 o It's not documented but auto alignment is reinstated by
252b5132 14662 a .align pseudo instruction.
54f4ddb3 14663 o Note also that after auto alignment is turned off the mips assembler
252b5132 14664 issues an error on attempt to assemble an improperly aligned data item.
54f4ddb3 14665 We don't. */
252b5132
RH
14666
14667 temp = get_absolute_expression ();
14668 if (temp > max_alignment)
1661c76c 14669 as_bad (_("alignment too large, %d assumed"), temp = max_alignment);
252b5132
RH
14670 else if (temp < 0)
14671 {
1661c76c 14672 as_warn (_("alignment negative, 0 assumed"));
252b5132
RH
14673 temp = 0;
14674 }
14675 if (*input_line_pointer == ',')
14676 {
f9419b05 14677 ++input_line_pointer;
742a56fe
RS
14678 fill_value = get_absolute_expression ();
14679 fill_ptr = &fill_value;
252b5132
RH
14680 }
14681 else
742a56fe 14682 fill_ptr = 0;
252b5132
RH
14683 if (temp)
14684 {
a8dbcb85
TS
14685 segment_info_type *si = seg_info (now_seg);
14686 struct insn_label_list *l = si->label_list;
54f4ddb3 14687 /* Auto alignment should be switched on by next section change. */
252b5132 14688 auto_align = 1;
462427c4 14689 mips_align (temp, fill_ptr, l);
252b5132
RH
14690 }
14691 else
14692 {
14693 auto_align = 0;
14694 }
14695
14696 demand_empty_rest_of_line ();
14697}
14698
252b5132 14699static void
17a2f251 14700s_change_sec (int sec)
252b5132
RH
14701{
14702 segT seg;
14703
252b5132
RH
14704 /* The ELF backend needs to know that we are changing sections, so
14705 that .previous works correctly. We could do something like check
b6ff326e 14706 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
14707 as it would not be appropriate to use it in the section changing
14708 functions in read.c, since obj-elf.c intercepts those. FIXME:
14709 This should be cleaner, somehow. */
f3ded42a 14710 obj_elf_section_change_hook ();
252b5132 14711
7d10b47d 14712 mips_emit_delays ();
6a32d874 14713
252b5132
RH
14714 switch (sec)
14715 {
14716 case 't':
14717 s_text (0);
14718 break;
14719 case 'd':
14720 s_data (0);
14721 break;
14722 case 'b':
14723 subseg_set (bss_section, (subsegT) get_absolute_expression ());
14724 demand_empty_rest_of_line ();
14725 break;
14726
14727 case 'r':
4d0d148d
TS
14728 seg = subseg_new (RDATA_SECTION_NAME,
14729 (subsegT) get_absolute_expression ());
f3ded42a
RS
14730 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
14731 | SEC_READONLY | SEC_RELOC
14732 | SEC_DATA));
14733 if (strncmp (TARGET_OS, "elf", 3) != 0)
14734 record_alignment (seg, 4);
4d0d148d 14735 demand_empty_rest_of_line ();
252b5132
RH
14736 break;
14737
14738 case 's':
4d0d148d 14739 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
f3ded42a
RS
14740 bfd_set_section_flags (stdoutput, seg,
14741 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
14742 if (strncmp (TARGET_OS, "elf", 3) != 0)
14743 record_alignment (seg, 4);
4d0d148d
TS
14744 demand_empty_rest_of_line ();
14745 break;
998b3c36
MR
14746
14747 case 'B':
14748 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
f3ded42a
RS
14749 bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
14750 if (strncmp (TARGET_OS, "elf", 3) != 0)
14751 record_alignment (seg, 4);
998b3c36
MR
14752 demand_empty_rest_of_line ();
14753 break;
252b5132
RH
14754 }
14755
14756 auto_align = 1;
14757}
b34976b6 14758
cca86cc8 14759void
17a2f251 14760s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 14761{
cca86cc8
SC
14762 char *section_name;
14763 char c;
684022ea 14764 char next_c = 0;
cca86cc8
SC
14765 int section_type;
14766 int section_flag;
14767 int section_entry_size;
14768 int section_alignment;
b34976b6 14769
cca86cc8
SC
14770 section_name = input_line_pointer;
14771 c = get_symbol_end ();
a816d1ed
AO
14772 if (c)
14773 next_c = *(input_line_pointer + 1);
cca86cc8 14774
4cf0dd0d
TS
14775 /* Do we have .section Name<,"flags">? */
14776 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 14777 {
4cf0dd0d
TS
14778 /* just after name is now '\0'. */
14779 *input_line_pointer = c;
cca86cc8
SC
14780 input_line_pointer = section_name;
14781 obj_elf_section (ignore);
14782 return;
14783 }
14784 input_line_pointer++;
14785
14786 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
14787 if (c == ',')
14788 section_type = get_absolute_expression ();
14789 else
14790 section_type = 0;
14791 if (*input_line_pointer++ == ',')
14792 section_flag = get_absolute_expression ();
14793 else
14794 section_flag = 0;
14795 if (*input_line_pointer++ == ',')
14796 section_entry_size = get_absolute_expression ();
14797 else
14798 section_entry_size = 0;
14799 if (*input_line_pointer++ == ',')
14800 section_alignment = get_absolute_expression ();
14801 else
14802 section_alignment = 0;
87975d2a
AM
14803 /* FIXME: really ignore? */
14804 (void) section_alignment;
cca86cc8 14805
a816d1ed
AO
14806 section_name = xstrdup (section_name);
14807
8ab8a5c8
RS
14808 /* When using the generic form of .section (as implemented by obj-elf.c),
14809 there's no way to set the section type to SHT_MIPS_DWARF. Users have
14810 traditionally had to fall back on the more common @progbits instead.
14811
14812 There's nothing really harmful in this, since bfd will correct
14813 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
708587a4 14814 means that, for backwards compatibility, the special_section entries
8ab8a5c8
RS
14815 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
14816
14817 Even so, we shouldn't force users of the MIPS .section syntax to
14818 incorrectly label the sections as SHT_PROGBITS. The best compromise
14819 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
14820 generic type-checking code. */
14821 if (section_type == SHT_MIPS_DWARF)
14822 section_type = SHT_PROGBITS;
14823
cca86cc8
SC
14824 obj_elf_change_section (section_name, section_type, section_flag,
14825 section_entry_size, 0, 0, 0);
a816d1ed
AO
14826
14827 if (now_seg->name != section_name)
14828 free (section_name);
cca86cc8 14829}
252b5132
RH
14830
14831void
17a2f251 14832mips_enable_auto_align (void)
252b5132
RH
14833{
14834 auto_align = 1;
14835}
14836
14837static void
17a2f251 14838s_cons (int log_size)
252b5132 14839{
a8dbcb85
TS
14840 segment_info_type *si = seg_info (now_seg);
14841 struct insn_label_list *l = si->label_list;
252b5132 14842
7d10b47d 14843 mips_emit_delays ();
252b5132 14844 if (log_size > 0 && auto_align)
462427c4 14845 mips_align (log_size, 0, l);
252b5132 14846 cons (1 << log_size);
a1facbec 14847 mips_clear_insn_labels ();
252b5132
RH
14848}
14849
14850static void
17a2f251 14851s_float_cons (int type)
252b5132 14852{
a8dbcb85
TS
14853 segment_info_type *si = seg_info (now_seg);
14854 struct insn_label_list *l = si->label_list;
252b5132 14855
7d10b47d 14856 mips_emit_delays ();
252b5132
RH
14857
14858 if (auto_align)
49309057
ILT
14859 {
14860 if (type == 'd')
462427c4 14861 mips_align (3, 0, l);
49309057 14862 else
462427c4 14863 mips_align (2, 0, l);
49309057 14864 }
252b5132 14865
252b5132 14866 float_cons (type);
a1facbec 14867 mips_clear_insn_labels ();
252b5132
RH
14868}
14869
14870/* Handle .globl. We need to override it because on Irix 5 you are
14871 permitted to say
14872 .globl foo .text
14873 where foo is an undefined symbol, to mean that foo should be
14874 considered to be the address of a function. */
14875
14876static void
17a2f251 14877s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
14878{
14879 char *name;
14880 int c;
14881 symbolS *symbolP;
14882 flagword flag;
14883
8a06b769 14884 do
252b5132 14885 {
8a06b769 14886 name = input_line_pointer;
252b5132 14887 c = get_symbol_end ();
8a06b769
TS
14888 symbolP = symbol_find_or_make (name);
14889 S_SET_EXTERNAL (symbolP);
14890
252b5132 14891 *input_line_pointer = c;
8a06b769 14892 SKIP_WHITESPACE ();
252b5132 14893
8a06b769
TS
14894 /* On Irix 5, every global symbol that is not explicitly labelled as
14895 being a function is apparently labelled as being an object. */
14896 flag = BSF_OBJECT;
252b5132 14897
8a06b769
TS
14898 if (!is_end_of_line[(unsigned char) *input_line_pointer]
14899 && (*input_line_pointer != ','))
14900 {
14901 char *secname;
14902 asection *sec;
14903
14904 secname = input_line_pointer;
14905 c = get_symbol_end ();
14906 sec = bfd_get_section_by_name (stdoutput, secname);
14907 if (sec == NULL)
14908 as_bad (_("%s: no such section"), secname);
14909 *input_line_pointer = c;
14910
14911 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
14912 flag = BSF_FUNCTION;
14913 }
14914
14915 symbol_get_bfdsym (symbolP)->flags |= flag;
14916
14917 c = *input_line_pointer;
14918 if (c == ',')
14919 {
14920 input_line_pointer++;
14921 SKIP_WHITESPACE ();
14922 if (is_end_of_line[(unsigned char) *input_line_pointer])
14923 c = '\n';
14924 }
14925 }
14926 while (c == ',');
252b5132 14927
252b5132
RH
14928 demand_empty_rest_of_line ();
14929}
14930
14931static void
17a2f251 14932s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
14933{
14934 char *opt;
14935 char c;
14936
14937 opt = input_line_pointer;
14938 c = get_symbol_end ();
14939
14940 if (*opt == 'O')
14941 {
14942 /* FIXME: What does this mean? */
14943 }
14944 else if (strncmp (opt, "pic", 3) == 0)
14945 {
14946 int i;
14947
14948 i = atoi (opt + 3);
14949 if (i == 0)
14950 mips_pic = NO_PIC;
14951 else if (i == 2)
143d77c5 14952 {
8b828383 14953 mips_pic = SVR4_PIC;
143d77c5
EC
14954 mips_abicalls = TRUE;
14955 }
252b5132
RH
14956 else
14957 as_bad (_(".option pic%d not supported"), i);
14958
4d0d148d 14959 if (mips_pic == SVR4_PIC)
252b5132
RH
14960 {
14961 if (g_switch_seen && g_switch_value != 0)
14962 as_warn (_("-G may not be used with SVR4 PIC code"));
14963 g_switch_value = 0;
14964 bfd_set_gp_size (stdoutput, 0);
14965 }
14966 }
14967 else
1661c76c 14968 as_warn (_("unrecognized option \"%s\""), opt);
252b5132
RH
14969
14970 *input_line_pointer = c;
14971 demand_empty_rest_of_line ();
14972}
14973
14974/* This structure is used to hold a stack of .set values. */
14975
e972090a
NC
14976struct mips_option_stack
14977{
252b5132
RH
14978 struct mips_option_stack *next;
14979 struct mips_set_options options;
14980};
14981
14982static struct mips_option_stack *mips_opts_stack;
14983
14984/* Handle the .set pseudo-op. */
14985
14986static void
17a2f251 14987s_mipsset (int x ATTRIBUTE_UNUSED)
252b5132
RH
14988{
14989 char *name = input_line_pointer, ch;
c6278170 14990 const struct mips_ase *ase;
252b5132
RH
14991
14992 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 14993 ++input_line_pointer;
252b5132
RH
14994 ch = *input_line_pointer;
14995 *input_line_pointer = '\0';
14996
14997 if (strcmp (name, "reorder") == 0)
14998 {
7d10b47d
RS
14999 if (mips_opts.noreorder)
15000 end_noreorder ();
252b5132
RH
15001 }
15002 else if (strcmp (name, "noreorder") == 0)
15003 {
7d10b47d
RS
15004 if (!mips_opts.noreorder)
15005 start_noreorder ();
252b5132 15006 }
741fe287
MR
15007 else if (strncmp (name, "at=", 3) == 0)
15008 {
15009 char *s = name + 3;
15010
15011 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
1661c76c 15012 as_bad (_("unrecognized register name `%s'"), s);
741fe287 15013 }
252b5132
RH
15014 else if (strcmp (name, "at") == 0)
15015 {
741fe287 15016 mips_opts.at = ATREG;
252b5132
RH
15017 }
15018 else if (strcmp (name, "noat") == 0)
15019 {
741fe287 15020 mips_opts.at = ZERO;
252b5132
RH
15021 }
15022 else if (strcmp (name, "macro") == 0)
15023 {
15024 mips_opts.warn_about_macros = 0;
15025 }
15026 else if (strcmp (name, "nomacro") == 0)
15027 {
15028 if (mips_opts.noreorder == 0)
15029 as_bad (_("`noreorder' must be set before `nomacro'"));
15030 mips_opts.warn_about_macros = 1;
15031 }
15032 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
15033 {
15034 mips_opts.nomove = 0;
15035 }
15036 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
15037 {
15038 mips_opts.nomove = 1;
15039 }
15040 else if (strcmp (name, "bopt") == 0)
15041 {
15042 mips_opts.nobopt = 0;
15043 }
15044 else if (strcmp (name, "nobopt") == 0)
15045 {
15046 mips_opts.nobopt = 1;
15047 }
ad3fea08 15048 else if (strcmp (name, "gp=default") == 0)
bad1aba3 15049 mips_opts.gp = file_mips_opts.gp;
ad3fea08 15050 else if (strcmp (name, "gp=32") == 0)
bad1aba3 15051 mips_opts.gp = 32;
ad3fea08
TS
15052 else if (strcmp (name, "gp=64") == 0)
15053 {
15054 if (!ISA_HAS_64BIT_REGS (mips_opts.isa))
20203fb9 15055 as_warn (_("%s isa does not support 64-bit registers"),
ad3fea08 15056 mips_cpu_info_from_isa (mips_opts.isa)->name);
bad1aba3 15057 mips_opts.gp = 64;
ad3fea08
TS
15058 }
15059 else if (strcmp (name, "fp=default") == 0)
0b35dfee 15060 mips_opts.fp = file_mips_opts.fp;
ad3fea08 15061 else if (strcmp (name, "fp=32") == 0)
0b35dfee 15062 mips_opts.fp = 32;
ad3fea08
TS
15063 else if (strcmp (name, "fp=64") == 0)
15064 {
15065 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
20203fb9 15066 as_warn (_("%s isa does not support 64-bit floating point registers"),
ad3fea08 15067 mips_cpu_info_from_isa (mips_opts.isa)->name);
0b35dfee 15068 mips_opts.fp = 64;
ad3fea08 15069 }
037b32b9
AN
15070 else if (strcmp (name, "softfloat") == 0)
15071 mips_opts.soft_float = 1;
15072 else if (strcmp (name, "hardfloat") == 0)
15073 mips_opts.soft_float = 0;
15074 else if (strcmp (name, "singlefloat") == 0)
15075 mips_opts.single_float = 1;
15076 else if (strcmp (name, "doublefloat") == 0)
15077 mips_opts.single_float = 0;
252b5132
RH
15078 else if (strcmp (name, "mips16") == 0
15079 || strcmp (name, "MIPS-16") == 0)
df58fc94
RS
15080 {
15081 if (mips_opts.micromips == 1)
15082 as_fatal (_("`mips16' cannot be used with `micromips'"));
15083 mips_opts.mips16 = 1;
15084 }
252b5132
RH
15085 else if (strcmp (name, "nomips16") == 0
15086 || strcmp (name, "noMIPS-16") == 0)
15087 mips_opts.mips16 = 0;
df58fc94
RS
15088 else if (strcmp (name, "micromips") == 0)
15089 {
15090 if (mips_opts.mips16 == 1)
15091 as_fatal (_("`micromips' cannot be used with `mips16'"));
15092 mips_opts.micromips = 1;
15093 }
15094 else if (strcmp (name, "nomicromips") == 0)
15095 mips_opts.micromips = 0;
c6278170
RS
15096 else if (name[0] == 'n'
15097 && name[1] == 'o'
15098 && (ase = mips_lookup_ase (name + 2)))
15099 mips_set_ase (ase, FALSE);
15100 else if ((ase = mips_lookup_ase (name)))
15101 mips_set_ase (ase, TRUE);
1a2c1fad 15102 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 15103 {
af7ee8bf 15104 int reset = 0;
252b5132 15105
1a2c1fad
CD
15106 /* Permit the user to change the ISA and architecture on the fly.
15107 Needless to say, misuse can cause serious problems. */
81a21e38 15108 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
af7ee8bf
CD
15109 {
15110 reset = 1;
0b35dfee 15111 mips_opts.isa = file_mips_opts.isa;
15112 mips_opts.arch = file_mips_opts.arch;
1a2c1fad
CD
15113 }
15114 else if (strncmp (name, "arch=", 5) == 0)
15115 {
15116 const struct mips_cpu_info *p;
15117
15118 p = mips_parse_cpu("internal use", name + 5);
15119 if (!p)
15120 as_bad (_("unknown architecture %s"), name + 5);
15121 else
15122 {
15123 mips_opts.arch = p->cpu;
15124 mips_opts.isa = p->isa;
15125 }
15126 }
81a21e38
TS
15127 else if (strncmp (name, "mips", 4) == 0)
15128 {
15129 const struct mips_cpu_info *p;
15130
15131 p = mips_parse_cpu("internal use", name);
15132 if (!p)
15133 as_bad (_("unknown ISA level %s"), name + 4);
15134 else
15135 {
15136 mips_opts.arch = p->cpu;
15137 mips_opts.isa = p->isa;
15138 }
15139 }
af7ee8bf 15140 else
81a21e38 15141 as_bad (_("unknown ISA or architecture %s"), name);
af7ee8bf
CD
15142
15143 switch (mips_opts.isa)
98d3f06f
KH
15144 {
15145 case 0:
98d3f06f 15146 break;
af7ee8bf
CD
15147 case ISA_MIPS1:
15148 case ISA_MIPS2:
15149 case ISA_MIPS32:
15150 case ISA_MIPS32R2:
ae52f483
AB
15151 case ISA_MIPS32R3:
15152 case ISA_MIPS32R5:
bad1aba3 15153 mips_opts.gp = 32;
0b35dfee 15154 mips_opts.fp = 32;
98d3f06f 15155 break;
af7ee8bf
CD
15156 case ISA_MIPS3:
15157 case ISA_MIPS4:
15158 case ISA_MIPS5:
15159 case ISA_MIPS64:
5f74bc13 15160 case ISA_MIPS64R2:
ae52f483
AB
15161 case ISA_MIPS64R3:
15162 case ISA_MIPS64R5:
bad1aba3 15163 mips_opts.gp = 64;
e407c74b
NC
15164 if (mips_opts.arch == CPU_R5900)
15165 {
0b35dfee 15166 mips_opts.fp = 32;
e407c74b
NC
15167 }
15168 else
15169 {
0b35dfee 15170 mips_opts.fp = 64;
e407c74b 15171 }
98d3f06f
KH
15172 break;
15173 default:
15174 as_bad (_("unknown ISA level %s"), name + 4);
15175 break;
15176 }
af7ee8bf 15177 if (reset)
98d3f06f 15178 {
bad1aba3 15179 mips_opts.gp = file_mips_opts.gp;
0b35dfee 15180 mips_opts.fp = file_mips_opts.fp;
98d3f06f 15181 }
252b5132
RH
15182 }
15183 else if (strcmp (name, "autoextend") == 0)
15184 mips_opts.noautoextend = 0;
15185 else if (strcmp (name, "noautoextend") == 0)
15186 mips_opts.noautoextend = 1;
833794fc
MR
15187 else if (strcmp (name, "insn32") == 0)
15188 mips_opts.insn32 = TRUE;
15189 else if (strcmp (name, "noinsn32") == 0)
15190 mips_opts.insn32 = FALSE;
252b5132
RH
15191 else if (strcmp (name, "push") == 0)
15192 {
15193 struct mips_option_stack *s;
15194
15195 s = (struct mips_option_stack *) xmalloc (sizeof *s);
15196 s->next = mips_opts_stack;
15197 s->options = mips_opts;
15198 mips_opts_stack = s;
15199 }
15200 else if (strcmp (name, "pop") == 0)
15201 {
15202 struct mips_option_stack *s;
15203
15204 s = mips_opts_stack;
15205 if (s == NULL)
15206 as_bad (_(".set pop with no .set push"));
15207 else
15208 {
15209 /* If we're changing the reorder mode we need to handle
15210 delay slots correctly. */
15211 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 15212 start_noreorder ();
252b5132 15213 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 15214 end_noreorder ();
252b5132
RH
15215
15216 mips_opts = s->options;
15217 mips_opts_stack = s->next;
15218 free (s);
15219 }
15220 }
aed1a261
RS
15221 else if (strcmp (name, "sym32") == 0)
15222 mips_opts.sym32 = TRUE;
15223 else if (strcmp (name, "nosym32") == 0)
15224 mips_opts.sym32 = FALSE;
e6559e01
JM
15225 else if (strchr (name, ','))
15226 {
15227 /* Generic ".set" directive; use the generic handler. */
15228 *input_line_pointer = ch;
15229 input_line_pointer = name;
15230 s_set (0);
15231 return;
15232 }
252b5132
RH
15233 else
15234 {
1661c76c 15235 as_warn (_("tried to set unrecognized symbol: %s\n"), name);
252b5132 15236 }
c6278170 15237 mips_check_isa_supports_ases ();
252b5132
RH
15238 *input_line_pointer = ch;
15239 demand_empty_rest_of_line ();
15240}
15241
15242/* Handle the .abicalls pseudo-op. I believe this is equivalent to
15243 .option pic2. It means to generate SVR4 PIC calls. */
15244
15245static void
17a2f251 15246s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
15247{
15248 mips_pic = SVR4_PIC;
143d77c5 15249 mips_abicalls = TRUE;
4d0d148d
TS
15250
15251 if (g_switch_seen && g_switch_value != 0)
15252 as_warn (_("-G may not be used with SVR4 PIC code"));
15253 g_switch_value = 0;
15254
252b5132
RH
15255 bfd_set_gp_size (stdoutput, 0);
15256 demand_empty_rest_of_line ();
15257}
15258
15259/* Handle the .cpload pseudo-op. This is used when generating SVR4
15260 PIC code. It sets the $gp register for the function based on the
15261 function address, which is in the register named in the argument.
15262 This uses a relocation against _gp_disp, which is handled specially
15263 by the linker. The result is:
15264 lui $gp,%hi(_gp_disp)
15265 addiu $gp,$gp,%lo(_gp_disp)
15266 addu $gp,$gp,.cpload argument
aa6975fb
ILT
15267 The .cpload argument is normally $25 == $t9.
15268
15269 The -mno-shared option changes this to:
bbe506e8
TS
15270 lui $gp,%hi(__gnu_local_gp)
15271 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
15272 and the argument is ignored. This saves an instruction, but the
15273 resulting code is not position independent; it uses an absolute
bbe506e8
TS
15274 address for __gnu_local_gp. Thus code assembled with -mno-shared
15275 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
15276
15277static void
17a2f251 15278s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
15279{
15280 expressionS ex;
aa6975fb
ILT
15281 int reg;
15282 int in_shared;
252b5132 15283
6478892d
TS
15284 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
15285 .cpload is ignored. */
15286 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
15287 {
15288 s_ignore (0);
15289 return;
15290 }
15291
a276b80c
MR
15292 if (mips_opts.mips16)
15293 {
15294 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
15295 ignore_rest_of_line ();
15296 return;
15297 }
15298
d3ecfc59 15299 /* .cpload should be in a .set noreorder section. */
252b5132
RH
15300 if (mips_opts.noreorder == 0)
15301 as_warn (_(".cpload not in noreorder section"));
15302
aa6975fb
ILT
15303 reg = tc_get_register (0);
15304
15305 /* If we need to produce a 64-bit address, we are better off using
15306 the default instruction sequence. */
aed1a261 15307 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 15308
252b5132 15309 ex.X_op = O_symbol;
bbe506e8
TS
15310 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
15311 "__gnu_local_gp");
252b5132
RH
15312 ex.X_op_symbol = NULL;
15313 ex.X_add_number = 0;
15314
15315 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 15316 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 15317
8a75745d
MR
15318 mips_mark_labels ();
15319 mips_assembling_insn = TRUE;
15320
584892a6 15321 macro_start ();
67c0d1eb
RS
15322 macro_build_lui (&ex, mips_gp_register);
15323 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 15324 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
15325 if (in_shared)
15326 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
15327 mips_gp_register, reg);
584892a6 15328 macro_end ();
252b5132 15329
8a75745d 15330 mips_assembling_insn = FALSE;
252b5132
RH
15331 demand_empty_rest_of_line ();
15332}
15333
6478892d
TS
15334/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
15335 .cpsetup $reg1, offset|$reg2, label
15336
15337 If offset is given, this results in:
15338 sd $gp, offset($sp)
956cd1d6 15339 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
15340 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
15341 daddu $gp, $gp, $reg1
6478892d
TS
15342
15343 If $reg2 is given, this results in:
15344 daddu $reg2, $gp, $0
956cd1d6 15345 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
15346 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
15347 daddu $gp, $gp, $reg1
aa6975fb
ILT
15348 $reg1 is normally $25 == $t9.
15349
15350 The -mno-shared option replaces the last three instructions with
15351 lui $gp,%hi(_gp)
54f4ddb3 15352 addiu $gp,$gp,%lo(_gp) */
aa6975fb 15353
6478892d 15354static void
17a2f251 15355s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
15356{
15357 expressionS ex_off;
15358 expressionS ex_sym;
15359 int reg1;
6478892d 15360
8586fc66 15361 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
15362 We also need NewABI support. */
15363 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
15364 {
15365 s_ignore (0);
15366 return;
15367 }
15368
a276b80c
MR
15369 if (mips_opts.mips16)
15370 {
15371 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
15372 ignore_rest_of_line ();
15373 return;
15374 }
15375
6478892d
TS
15376 reg1 = tc_get_register (0);
15377 SKIP_WHITESPACE ();
15378 if (*input_line_pointer != ',')
15379 {
15380 as_bad (_("missing argument separator ',' for .cpsetup"));
15381 return;
15382 }
15383 else
80245285 15384 ++input_line_pointer;
6478892d
TS
15385 SKIP_WHITESPACE ();
15386 if (*input_line_pointer == '$')
80245285
TS
15387 {
15388 mips_cpreturn_register = tc_get_register (0);
15389 mips_cpreturn_offset = -1;
15390 }
6478892d 15391 else
80245285
TS
15392 {
15393 mips_cpreturn_offset = get_absolute_expression ();
15394 mips_cpreturn_register = -1;
15395 }
6478892d
TS
15396 SKIP_WHITESPACE ();
15397 if (*input_line_pointer != ',')
15398 {
15399 as_bad (_("missing argument separator ',' for .cpsetup"));
15400 return;
15401 }
15402 else
f9419b05 15403 ++input_line_pointer;
6478892d 15404 SKIP_WHITESPACE ();
f21f8242 15405 expression (&ex_sym);
6478892d 15406
8a75745d
MR
15407 mips_mark_labels ();
15408 mips_assembling_insn = TRUE;
15409
584892a6 15410 macro_start ();
6478892d
TS
15411 if (mips_cpreturn_register == -1)
15412 {
15413 ex_off.X_op = O_constant;
15414 ex_off.X_add_symbol = NULL;
15415 ex_off.X_op_symbol = NULL;
15416 ex_off.X_add_number = mips_cpreturn_offset;
15417
67c0d1eb 15418 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 15419 BFD_RELOC_LO16, SP);
6478892d
TS
15420 }
15421 else
67c0d1eb 15422 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
17a2f251 15423 mips_gp_register, 0);
6478892d 15424
aed1a261 15425 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb 15426 {
df58fc94 15427 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
aa6975fb
ILT
15428 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
15429 BFD_RELOC_HI16_S);
15430
15431 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
15432 mips_gp_register, -1, BFD_RELOC_GPREL16,
15433 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
15434
15435 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
15436 mips_gp_register, reg1);
15437 }
15438 else
15439 {
15440 expressionS ex;
15441
15442 ex.X_op = O_symbol;
4184909a 15443 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
15444 ex.X_op_symbol = NULL;
15445 ex.X_add_number = 0;
6e1304d8 15446
aa6975fb
ILT
15447 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
15448 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
15449
15450 macro_build_lui (&ex, mips_gp_register);
15451 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
15452 mips_gp_register, BFD_RELOC_LO16);
15453 }
f21f8242 15454
584892a6 15455 macro_end ();
6478892d 15456
8a75745d 15457 mips_assembling_insn = FALSE;
6478892d
TS
15458 demand_empty_rest_of_line ();
15459}
15460
15461static void
17a2f251 15462s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
15463{
15464 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
54f4ddb3 15465 .cplocal is ignored. */
6478892d
TS
15466 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
15467 {
15468 s_ignore (0);
15469 return;
15470 }
15471
a276b80c
MR
15472 if (mips_opts.mips16)
15473 {
15474 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
15475 ignore_rest_of_line ();
15476 return;
15477 }
15478
6478892d 15479 mips_gp_register = tc_get_register (0);
85b51719 15480 demand_empty_rest_of_line ();
6478892d
TS
15481}
15482
252b5132
RH
15483/* Handle the .cprestore pseudo-op. This stores $gp into a given
15484 offset from $sp. The offset is remembered, and after making a PIC
15485 call $gp is restored from that location. */
15486
15487static void
17a2f251 15488s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
15489{
15490 expressionS ex;
252b5132 15491
6478892d 15492 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 15493 .cprestore is ignored. */
6478892d 15494 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
15495 {
15496 s_ignore (0);
15497 return;
15498 }
15499
a276b80c
MR
15500 if (mips_opts.mips16)
15501 {
15502 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
15503 ignore_rest_of_line ();
15504 return;
15505 }
15506
252b5132 15507 mips_cprestore_offset = get_absolute_expression ();
7a621144 15508 mips_cprestore_valid = 1;
252b5132
RH
15509
15510 ex.X_op = O_constant;
15511 ex.X_add_symbol = NULL;
15512 ex.X_op_symbol = NULL;
15513 ex.X_add_number = mips_cprestore_offset;
15514
8a75745d
MR
15515 mips_mark_labels ();
15516 mips_assembling_insn = TRUE;
15517
584892a6 15518 macro_start ();
67c0d1eb
RS
15519 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
15520 SP, HAVE_64BIT_ADDRESSES);
584892a6 15521 macro_end ();
252b5132 15522
8a75745d 15523 mips_assembling_insn = FALSE;
252b5132
RH
15524 demand_empty_rest_of_line ();
15525}
15526
6478892d 15527/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 15528 was given in the preceding .cpsetup, it results in:
6478892d 15529 ld $gp, offset($sp)
76b3015f 15530
6478892d 15531 If a register $reg2 was given there, it results in:
54f4ddb3
TS
15532 daddu $gp, $reg2, $0 */
15533
6478892d 15534static void
17a2f251 15535s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
15536{
15537 expressionS ex;
6478892d
TS
15538
15539 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
15540 We also need NewABI support. */
15541 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
15542 {
15543 s_ignore (0);
15544 return;
15545 }
15546
a276b80c
MR
15547 if (mips_opts.mips16)
15548 {
15549 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
15550 ignore_rest_of_line ();
15551 return;
15552 }
15553
8a75745d
MR
15554 mips_mark_labels ();
15555 mips_assembling_insn = TRUE;
15556
584892a6 15557 macro_start ();
6478892d
TS
15558 if (mips_cpreturn_register == -1)
15559 {
15560 ex.X_op = O_constant;
15561 ex.X_add_symbol = NULL;
15562 ex.X_op_symbol = NULL;
15563 ex.X_add_number = mips_cpreturn_offset;
15564
67c0d1eb 15565 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
15566 }
15567 else
67c0d1eb 15568 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
17a2f251 15569 mips_cpreturn_register, 0);
584892a6 15570 macro_end ();
6478892d 15571
8a75745d 15572 mips_assembling_insn = FALSE;
6478892d
TS
15573 demand_empty_rest_of_line ();
15574}
15575
d0f13682
CLT
15576/* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
15577 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
15578 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
15579 debug information or MIPS16 TLS. */
741d6ea8
JM
15580
15581static void
d0f13682
CLT
15582s_tls_rel_directive (const size_t bytes, const char *dirstr,
15583 bfd_reloc_code_real_type rtype)
741d6ea8
JM
15584{
15585 expressionS ex;
15586 char *p;
15587
15588 expression (&ex);
15589
15590 if (ex.X_op != O_symbol)
15591 {
1661c76c 15592 as_bad (_("unsupported use of %s"), dirstr);
741d6ea8
JM
15593 ignore_rest_of_line ();
15594 }
15595
15596 p = frag_more (bytes);
15597 md_number_to_chars (p, 0, bytes);
d0f13682 15598 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
741d6ea8 15599 demand_empty_rest_of_line ();
de64cffd 15600 mips_clear_insn_labels ();
741d6ea8
JM
15601}
15602
15603/* Handle .dtprelword. */
15604
15605static void
15606s_dtprelword (int ignore ATTRIBUTE_UNUSED)
15607{
d0f13682 15608 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
741d6ea8
JM
15609}
15610
15611/* Handle .dtpreldword. */
15612
15613static void
15614s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
15615{
d0f13682
CLT
15616 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
15617}
15618
15619/* Handle .tprelword. */
15620
15621static void
15622s_tprelword (int ignore ATTRIBUTE_UNUSED)
15623{
15624 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
15625}
15626
15627/* Handle .tpreldword. */
15628
15629static void
15630s_tpreldword (int ignore ATTRIBUTE_UNUSED)
15631{
15632 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
741d6ea8
JM
15633}
15634
6478892d
TS
15635/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
15636 code. It sets the offset to use in gp_rel relocations. */
15637
15638static void
17a2f251 15639s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
15640{
15641 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
15642 We also need NewABI support. */
15643 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
15644 {
15645 s_ignore (0);
15646 return;
15647 }
15648
def2e0dd 15649 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
15650
15651 demand_empty_rest_of_line ();
15652}
15653
252b5132
RH
15654/* Handle the .gpword pseudo-op. This is used when generating PIC
15655 code. It generates a 32 bit GP relative reloc. */
15656
15657static void
17a2f251 15658s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132 15659{
a8dbcb85
TS
15660 segment_info_type *si;
15661 struct insn_label_list *l;
252b5132
RH
15662 expressionS ex;
15663 char *p;
15664
15665 /* When not generating PIC code, this is treated as .word. */
15666 if (mips_pic != SVR4_PIC)
15667 {
15668 s_cons (2);
15669 return;
15670 }
15671
a8dbcb85
TS
15672 si = seg_info (now_seg);
15673 l = si->label_list;
7d10b47d 15674 mips_emit_delays ();
252b5132 15675 if (auto_align)
462427c4 15676 mips_align (2, 0, l);
252b5132
RH
15677
15678 expression (&ex);
a1facbec 15679 mips_clear_insn_labels ();
252b5132
RH
15680
15681 if (ex.X_op != O_symbol || ex.X_add_number != 0)
15682 {
1661c76c 15683 as_bad (_("unsupported use of .gpword"));
252b5132
RH
15684 ignore_rest_of_line ();
15685 }
15686
15687 p = frag_more (4);
17a2f251 15688 md_number_to_chars (p, 0, 4);
b34976b6 15689 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 15690 BFD_RELOC_GPREL32);
252b5132
RH
15691
15692 demand_empty_rest_of_line ();
15693}
15694
10181a0d 15695static void
17a2f251 15696s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d 15697{
a8dbcb85
TS
15698 segment_info_type *si;
15699 struct insn_label_list *l;
10181a0d
AO
15700 expressionS ex;
15701 char *p;
15702
15703 /* When not generating PIC code, this is treated as .dword. */
15704 if (mips_pic != SVR4_PIC)
15705 {
15706 s_cons (3);
15707 return;
15708 }
15709
a8dbcb85
TS
15710 si = seg_info (now_seg);
15711 l = si->label_list;
7d10b47d 15712 mips_emit_delays ();
10181a0d 15713 if (auto_align)
462427c4 15714 mips_align (3, 0, l);
10181a0d
AO
15715
15716 expression (&ex);
a1facbec 15717 mips_clear_insn_labels ();
10181a0d
AO
15718
15719 if (ex.X_op != O_symbol || ex.X_add_number != 0)
15720 {
1661c76c 15721 as_bad (_("unsupported use of .gpdword"));
10181a0d
AO
15722 ignore_rest_of_line ();
15723 }
15724
15725 p = frag_more (8);
17a2f251 15726 md_number_to_chars (p, 0, 8);
a105a300 15727 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
6e1304d8 15728 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
15729
15730 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8
RS
15731 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
15732 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
15733
15734 demand_empty_rest_of_line ();
15735}
15736
a3f278e2
CM
15737/* Handle the .ehword pseudo-op. This is used when generating unwinding
15738 tables. It generates a R_MIPS_EH reloc. */
15739
15740static void
15741s_ehword (int ignore ATTRIBUTE_UNUSED)
15742{
15743 expressionS ex;
15744 char *p;
15745
15746 mips_emit_delays ();
15747
15748 expression (&ex);
15749 mips_clear_insn_labels ();
15750
15751 if (ex.X_op != O_symbol || ex.X_add_number != 0)
15752 {
1661c76c 15753 as_bad (_("unsupported use of .ehword"));
a3f278e2
CM
15754 ignore_rest_of_line ();
15755 }
15756
15757 p = frag_more (4);
15758 md_number_to_chars (p, 0, 4);
15759 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
15760 BFD_RELOC_MIPS_EH);
15761
15762 demand_empty_rest_of_line ();
15763}
15764
252b5132
RH
15765/* Handle the .cpadd pseudo-op. This is used when dealing with switch
15766 tables in SVR4 PIC code. */
15767
15768static void
17a2f251 15769s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 15770{
252b5132
RH
15771 int reg;
15772
10181a0d
AO
15773 /* This is ignored when not generating SVR4 PIC code. */
15774 if (mips_pic != SVR4_PIC)
252b5132
RH
15775 {
15776 s_ignore (0);
15777 return;
15778 }
15779
8a75745d
MR
15780 mips_mark_labels ();
15781 mips_assembling_insn = TRUE;
15782
252b5132 15783 /* Add $gp to the register named as an argument. */
584892a6 15784 macro_start ();
252b5132 15785 reg = tc_get_register (0);
67c0d1eb 15786 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 15787 macro_end ();
252b5132 15788
8a75745d 15789 mips_assembling_insn = FALSE;
bdaaa2e1 15790 demand_empty_rest_of_line ();
252b5132
RH
15791}
15792
15793/* Handle the .insn pseudo-op. This marks instruction labels in
df58fc94 15794 mips16/micromips mode. This permits the linker to handle them specially,
252b5132
RH
15795 such as generating jalx instructions when needed. We also make
15796 them odd for the duration of the assembly, in order to generate the
15797 right sort of code. We will make them even in the adjust_symtab
15798 routine, while leaving them marked. This is convenient for the
15799 debugger and the disassembler. The linker knows to make them odd
15800 again. */
15801
15802static void
17a2f251 15803s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 15804{
df58fc94 15805 mips_mark_labels ();
252b5132
RH
15806
15807 demand_empty_rest_of_line ();
15808}
15809
ba92f887
MR
15810/* Handle the .nan pseudo-op. */
15811
15812static void
15813s_nan (int ignore ATTRIBUTE_UNUSED)
15814{
15815 static const char str_legacy[] = "legacy";
15816 static const char str_2008[] = "2008";
15817 size_t i;
15818
15819 for (i = 0; !is_end_of_line[(unsigned char) input_line_pointer[i]]; i++);
15820
15821 if (i == sizeof (str_2008) - 1
15822 && memcmp (input_line_pointer, str_2008, i) == 0)
15823 mips_flag_nan2008 = TRUE;
15824 else if (i == sizeof (str_legacy) - 1
15825 && memcmp (input_line_pointer, str_legacy, i) == 0)
15826 mips_flag_nan2008 = FALSE;
15827 else
1661c76c 15828 as_bad (_("bad .nan directive"));
ba92f887
MR
15829
15830 input_line_pointer += i;
15831 demand_empty_rest_of_line ();
15832}
15833
754e2bb9
RS
15834/* Handle a .stab[snd] directive. Ideally these directives would be
15835 implemented in a transparent way, so that removing them would not
15836 have any effect on the generated instructions. However, s_stab
15837 internally changes the section, so in practice we need to decide
15838 now whether the preceding label marks compressed code. We do not
15839 support changing the compression mode of a label after a .stab*
15840 directive, such as in:
15841
15842 foo:
15843 .stabs ...
15844 .set mips16
15845
15846 so the current mode wins. */
252b5132
RH
15847
15848static void
17a2f251 15849s_mips_stab (int type)
252b5132 15850{
754e2bb9 15851 mips_mark_labels ();
252b5132
RH
15852 s_stab (type);
15853}
15854
54f4ddb3 15855/* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
252b5132
RH
15856
15857static void
17a2f251 15858s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
15859{
15860 char *name;
15861 int c;
15862 symbolS *symbolP;
15863 expressionS exp;
15864
15865 name = input_line_pointer;
15866 c = get_symbol_end ();
15867 symbolP = symbol_find_or_make (name);
15868 S_SET_WEAK (symbolP);
15869 *input_line_pointer = c;
15870
15871 SKIP_WHITESPACE ();
15872
15873 if (! is_end_of_line[(unsigned char) *input_line_pointer])
15874 {
15875 if (S_IS_DEFINED (symbolP))
15876 {
20203fb9 15877 as_bad (_("ignoring attempt to redefine symbol %s"),
252b5132
RH
15878 S_GET_NAME (symbolP));
15879 ignore_rest_of_line ();
15880 return;
15881 }
bdaaa2e1 15882
252b5132
RH
15883 if (*input_line_pointer == ',')
15884 {
15885 ++input_line_pointer;
15886 SKIP_WHITESPACE ();
15887 }
bdaaa2e1 15888
252b5132
RH
15889 expression (&exp);
15890 if (exp.X_op != O_symbol)
15891 {
20203fb9 15892 as_bad (_("bad .weakext directive"));
98d3f06f 15893 ignore_rest_of_line ();
252b5132
RH
15894 return;
15895 }
49309057 15896 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
15897 }
15898
15899 demand_empty_rest_of_line ();
15900}
15901
15902/* Parse a register string into a number. Called from the ECOFF code
15903 to parse .frame. The argument is non-zero if this is the frame
15904 register, so that we can record it in mips_frame_reg. */
15905
15906int
17a2f251 15907tc_get_register (int frame)
252b5132 15908{
707bfff6 15909 unsigned int reg;
252b5132
RH
15910
15911 SKIP_WHITESPACE ();
707bfff6
TS
15912 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
15913 reg = 0;
252b5132 15914 if (frame)
7a621144
DJ
15915 {
15916 mips_frame_reg = reg != 0 ? reg : SP;
15917 mips_frame_reg_valid = 1;
15918 mips_cprestore_valid = 0;
15919 }
252b5132
RH
15920 return reg;
15921}
15922
15923valueT
17a2f251 15924md_section_align (asection *seg, valueT addr)
252b5132
RH
15925{
15926 int align = bfd_get_section_alignment (stdoutput, seg);
15927
f3ded42a
RS
15928 /* We don't need to align ELF sections to the full alignment.
15929 However, Irix 5 may prefer that we align them at least to a 16
15930 byte boundary. We don't bother to align the sections if we
15931 are targeted for an embedded system. */
15932 if (strncmp (TARGET_OS, "elf", 3) == 0)
15933 return addr;
15934 if (align > 4)
15935 align = 4;
252b5132
RH
15936
15937 return ((addr + (1 << align) - 1) & (-1 << align));
15938}
15939
15940/* Utility routine, called from above as well. If called while the
15941 input file is still being read, it's only an approximation. (For
15942 example, a symbol may later become defined which appeared to be
15943 undefined earlier.) */
15944
15945static int
17a2f251 15946nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
15947{
15948 if (sym == 0)
15949 return 0;
15950
4d0d148d 15951 if (g_switch_value > 0)
252b5132
RH
15952 {
15953 const char *symname;
15954 int change;
15955
c9914766 15956 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
15957 register. It can be if it is smaller than the -G size or if
15958 it is in the .sdata or .sbss section. Certain symbols can
c9914766 15959 not be referenced off the $gp, although it appears as though
252b5132
RH
15960 they can. */
15961 symname = S_GET_NAME (sym);
15962 if (symname != (const char *) NULL
15963 && (strcmp (symname, "eprol") == 0
15964 || strcmp (symname, "etext") == 0
15965 || strcmp (symname, "_gp") == 0
15966 || strcmp (symname, "edata") == 0
15967 || strcmp (symname, "_fbss") == 0
15968 || strcmp (symname, "_fdata") == 0
15969 || strcmp (symname, "_ftext") == 0
15970 || strcmp (symname, "end") == 0
15971 || strcmp (symname, "_gp_disp") == 0))
15972 change = 1;
15973 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
15974 && (0
15975#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
15976 || (symbol_get_obj (sym)->ecoff_extern_size != 0
15977 && (symbol_get_obj (sym)->ecoff_extern_size
15978 <= g_switch_value))
252b5132
RH
15979#endif
15980 /* We must defer this decision until after the whole
15981 file has been read, since there might be a .extern
15982 after the first use of this symbol. */
15983 || (before_relaxing
15984#ifndef NO_ECOFF_DEBUGGING
49309057 15985 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
15986#endif
15987 && S_GET_VALUE (sym) == 0)
15988 || (S_GET_VALUE (sym) != 0
15989 && S_GET_VALUE (sym) <= g_switch_value)))
15990 change = 0;
15991 else
15992 {
15993 const char *segname;
15994
15995 segname = segment_name (S_GET_SEGMENT (sym));
9c2799c2 15996 gas_assert (strcmp (segname, ".lit8") != 0
252b5132
RH
15997 && strcmp (segname, ".lit4") != 0);
15998 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
15999 && strcmp (segname, ".sbss") != 0
16000 && strncmp (segname, ".sdata.", 7) != 0
d4dc2f22
TS
16001 && strncmp (segname, ".sbss.", 6) != 0
16002 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
fba2b7f9 16003 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
16004 }
16005 return change;
16006 }
16007 else
c9914766 16008 /* We are not optimizing for the $gp register. */
252b5132
RH
16009 return 1;
16010}
16011
5919d012
RS
16012
16013/* Return true if the given symbol should be considered local for SVR4 PIC. */
16014
16015static bfd_boolean
17a2f251 16016pic_need_relax (symbolS *sym, asection *segtype)
5919d012
RS
16017{
16018 asection *symsec;
5919d012
RS
16019
16020 /* Handle the case of a symbol equated to another symbol. */
16021 while (symbol_equated_reloc_p (sym))
16022 {
16023 symbolS *n;
16024
5f0fe04b 16025 /* It's possible to get a loop here in a badly written program. */
5919d012
RS
16026 n = symbol_get_value_expression (sym)->X_add_symbol;
16027 if (n == sym)
16028 break;
16029 sym = n;
16030 }
16031
df1f3cda
DD
16032 if (symbol_section_p (sym))
16033 return TRUE;
16034
5919d012
RS
16035 symsec = S_GET_SEGMENT (sym);
16036
5919d012 16037 /* This must duplicate the test in adjust_reloc_syms. */
45dfa85a
AM
16038 return (!bfd_is_und_section (symsec)
16039 && !bfd_is_abs_section (symsec)
5f0fe04b
TS
16040 && !bfd_is_com_section (symsec)
16041 && !s_is_linkonce (sym, segtype)
5919d012 16042 /* A global or weak symbol is treated as external. */
f3ded42a 16043 && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
5919d012
RS
16044}
16045
16046
252b5132
RH
16047/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
16048 extended opcode. SEC is the section the frag is in. */
16049
16050static int
17a2f251 16051mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132
RH
16052{
16053 int type;
3ccad066 16054 const struct mips_int_operand *operand;
252b5132 16055 offsetT val;
252b5132 16056 segT symsec;
98aa84af 16057 fragS *sym_frag;
252b5132
RH
16058
16059 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
16060 return 0;
16061 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
16062 return 1;
16063
16064 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
3ccad066 16065 operand = mips16_immed_operand (type, FALSE);
252b5132 16066
98aa84af 16067 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 16068 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 16069 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132 16070
3ccad066 16071 if (operand->root.type == OP_PCREL)
252b5132 16072 {
3ccad066 16073 const struct mips_pcrel_operand *pcrel_op;
252b5132 16074 addressT addr;
3ccad066 16075 offsetT maxtiny;
252b5132
RH
16076
16077 /* We won't have the section when we are called from
16078 mips_relax_frag. However, we will always have been called
16079 from md_estimate_size_before_relax first. If this is a
16080 branch to a different section, we mark it as such. If SEC is
16081 NULL, and the frag is not marked, then it must be a branch to
16082 the same section. */
3ccad066 16083 pcrel_op = (const struct mips_pcrel_operand *) operand;
252b5132
RH
16084 if (sec == NULL)
16085 {
16086 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
16087 return 1;
16088 }
16089 else
16090 {
98aa84af 16091 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
16092 if (symsec != sec)
16093 {
16094 fragp->fr_subtype =
16095 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
16096
16097 /* FIXME: We should support this, and let the linker
16098 catch branches and loads that are out of range. */
16099 as_bad_where (fragp->fr_file, fragp->fr_line,
16100 _("unsupported PC relative reference to different section"));
16101
16102 return 1;
16103 }
98aa84af
AM
16104 if (fragp != sym_frag && sym_frag->fr_address == 0)
16105 /* Assume non-extended on the first relaxation pass.
16106 The address we have calculated will be bogus if this is
16107 a forward branch to another frag, as the forward frag
16108 will have fr_address == 0. */
16109 return 0;
252b5132
RH
16110 }
16111
16112 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
16113 the same section. If the relax_marker of the symbol fragment
16114 differs from the relax_marker of this fragment, we have not
16115 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
16116 in STRETCH in order to get a better estimate of the address.
16117 This particularly matters because of the shift bits. */
16118 if (stretch != 0
98aa84af 16119 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
16120 {
16121 fragS *f;
16122
16123 /* Adjust stretch for any alignment frag. Note that if have
16124 been expanding the earlier code, the symbol may be
16125 defined in what appears to be an earlier frag. FIXME:
16126 This doesn't handle the fr_subtype field, which specifies
16127 a maximum number of bytes to skip when doing an
16128 alignment. */
98aa84af 16129 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
16130 {
16131 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
16132 {
16133 if (stretch < 0)
16134 stretch = - ((- stretch)
16135 & ~ ((1 << (int) f->fr_offset) - 1));
16136 else
16137 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
16138 if (stretch == 0)
16139 break;
16140 }
16141 }
16142 if (f != NULL)
16143 val += stretch;
16144 }
16145
16146 addr = fragp->fr_address + fragp->fr_fix;
16147
16148 /* The base address rules are complicated. The base address of
16149 a branch is the following instruction. The base address of a
16150 PC relative load or add is the instruction itself, but if it
16151 is in a delay slot (in which case it can not be extended) use
16152 the address of the instruction whose delay slot it is in. */
3ccad066 16153 if (pcrel_op->include_isa_bit)
252b5132
RH
16154 {
16155 addr += 2;
16156
16157 /* If we are currently assuming that this frag should be
16158 extended, then, the current address is two bytes
bdaaa2e1 16159 higher. */
252b5132
RH
16160 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
16161 addr += 2;
16162
16163 /* Ignore the low bit in the target, since it will be set
16164 for a text label. */
3ccad066 16165 val &= -2;
252b5132
RH
16166 }
16167 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
16168 addr -= 4;
16169 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
16170 addr -= 2;
16171
3ccad066 16172 val -= addr & -(1 << pcrel_op->align_log2);
252b5132
RH
16173
16174 /* If any of the shifted bits are set, we must use an extended
16175 opcode. If the address depends on the size of this
16176 instruction, this can lead to a loop, so we arrange to always
16177 use an extended opcode. We only check this when we are in
16178 the main relaxation loop, when SEC is NULL. */
3ccad066 16179 if ((val & ((1 << operand->shift) - 1)) != 0 && sec == NULL)
252b5132
RH
16180 {
16181 fragp->fr_subtype =
16182 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
16183 return 1;
16184 }
16185
16186 /* If we are about to mark a frag as extended because the value
3ccad066
RS
16187 is precisely the next value above maxtiny, then there is a
16188 chance of an infinite loop as in the following code:
252b5132
RH
16189 la $4,foo
16190 .skip 1020
16191 .align 2
16192 foo:
16193 In this case when the la is extended, foo is 0x3fc bytes
16194 away, so the la can be shrunk, but then foo is 0x400 away, so
16195 the la must be extended. To avoid this loop, we mark the
16196 frag as extended if it was small, and is about to become
3ccad066
RS
16197 extended with the next value above maxtiny. */
16198 maxtiny = mips_int_operand_max (operand);
16199 if (val == maxtiny + (1 << operand->shift)
252b5132
RH
16200 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
16201 && sec == NULL)
16202 {
16203 fragp->fr_subtype =
16204 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
16205 return 1;
16206 }
16207 }
16208 else if (symsec != absolute_section && sec != NULL)
16209 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
16210
3ccad066 16211 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
252b5132
RH
16212}
16213
4a6a3df4
AO
16214/* Compute the length of a branch sequence, and adjust the
16215 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
16216 worst-case length is computed, with UPDATE being used to indicate
16217 whether an unconditional (-1), branch-likely (+1) or regular (0)
16218 branch is to be computed. */
16219static int
17a2f251 16220relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 16221{
b34976b6 16222 bfd_boolean toofar;
4a6a3df4
AO
16223 int length;
16224
16225 if (fragp
16226 && S_IS_DEFINED (fragp->fr_symbol)
16227 && sec == S_GET_SEGMENT (fragp->fr_symbol))
16228 {
16229 addressT addr;
16230 offsetT val;
16231
16232 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
16233
16234 addr = fragp->fr_address + fragp->fr_fix + 4;
16235
16236 val -= addr;
16237
16238 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
16239 }
16240 else if (fragp)
16241 /* If the symbol is not defined or it's in a different segment,
16242 assume the user knows what's going on and emit a short
16243 branch. */
b34976b6 16244 toofar = FALSE;
4a6a3df4 16245 else
b34976b6 16246 toofar = TRUE;
4a6a3df4
AO
16247
16248 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
16249 fragp->fr_subtype
66b3e8da
MR
16250 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
16251 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
16252 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
16253 RELAX_BRANCH_LINK (fragp->fr_subtype),
16254 toofar);
16255
16256 length = 4;
16257 if (toofar)
16258 {
16259 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
16260 length += 8;
16261
16262 if (mips_pic != NO_PIC)
16263 {
16264 /* Additional space for PIC loading of target address. */
16265 length += 8;
16266 if (mips_opts.isa == ISA_MIPS1)
16267 /* Additional space for $at-stabilizing nop. */
16268 length += 4;
16269 }
16270
16271 /* If branch is conditional. */
16272 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
16273 length += 8;
16274 }
b34976b6 16275
4a6a3df4
AO
16276 return length;
16277}
16278
df58fc94
RS
16279/* Compute the length of a branch sequence, and adjust the
16280 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
16281 worst-case length is computed, with UPDATE being used to indicate
16282 whether an unconditional (-1), or regular (0) branch is to be
16283 computed. */
16284
16285static int
16286relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
16287{
16288 bfd_boolean toofar;
16289 int length;
16290
16291 if (fragp
16292 && S_IS_DEFINED (fragp->fr_symbol)
16293 && sec == S_GET_SEGMENT (fragp->fr_symbol))
16294 {
16295 addressT addr;
16296 offsetT val;
16297
16298 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
16299 /* Ignore the low bit in the target, since it will be set
16300 for a text label. */
16301 if ((val & 1) != 0)
16302 --val;
16303
16304 addr = fragp->fr_address + fragp->fr_fix + 4;
16305
16306 val -= addr;
16307
16308 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
16309 }
16310 else if (fragp)
16311 /* If the symbol is not defined or it's in a different segment,
16312 assume the user knows what's going on and emit a short
16313 branch. */
16314 toofar = FALSE;
16315 else
16316 toofar = TRUE;
16317
16318 if (fragp && update
16319 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
16320 fragp->fr_subtype = (toofar
16321 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
16322 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
16323
16324 length = 4;
16325 if (toofar)
16326 {
16327 bfd_boolean compact_known = fragp != NULL;
16328 bfd_boolean compact = FALSE;
16329 bfd_boolean uncond;
16330
16331 if (compact_known)
16332 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
16333 if (fragp)
16334 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
16335 else
16336 uncond = update < 0;
16337
16338 /* If label is out of range, we turn branch <br>:
16339
16340 <br> label # 4 bytes
16341 0:
16342
16343 into:
16344
16345 j label # 4 bytes
16346 nop # 2 bytes if compact && !PIC
16347 0:
16348 */
16349 if (mips_pic == NO_PIC && (!compact_known || compact))
16350 length += 2;
16351
16352 /* If assembling PIC code, we further turn:
16353
16354 j label # 4 bytes
16355
16356 into:
16357
16358 lw/ld at, %got(label)(gp) # 4 bytes
16359 d/addiu at, %lo(label) # 4 bytes
16360 jr/c at # 2 bytes
16361 */
16362 if (mips_pic != NO_PIC)
16363 length += 6;
16364
16365 /* If branch <br> is conditional, we prepend negated branch <brneg>:
16366
16367 <brneg> 0f # 4 bytes
16368 nop # 2 bytes if !compact
16369 */
16370 if (!uncond)
16371 length += (compact_known && compact) ? 4 : 6;
16372 }
16373
16374 return length;
16375}
16376
16377/* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
16378 bit accordingly. */
16379
16380static int
16381relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
16382{
16383 bfd_boolean toofar;
16384
df58fc94
RS
16385 if (fragp
16386 && S_IS_DEFINED (fragp->fr_symbol)
16387 && sec == S_GET_SEGMENT (fragp->fr_symbol))
16388 {
16389 addressT addr;
16390 offsetT val;
16391 int type;
16392
16393 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
16394 /* Ignore the low bit in the target, since it will be set
16395 for a text label. */
16396 if ((val & 1) != 0)
16397 --val;
16398
16399 /* Assume this is a 2-byte branch. */
16400 addr = fragp->fr_address + fragp->fr_fix + 2;
16401
16402 /* We try to avoid the infinite loop by not adding 2 more bytes for
16403 long branches. */
16404
16405 val -= addr;
16406
16407 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
16408 if (type == 'D')
16409 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
16410 else if (type == 'E')
16411 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
16412 else
16413 abort ();
16414 }
16415 else
16416 /* If the symbol is not defined or it's in a different segment,
16417 we emit a normal 32-bit branch. */
16418 toofar = TRUE;
16419
16420 if (fragp && update
16421 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
16422 fragp->fr_subtype
16423 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
16424 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
16425
16426 if (toofar)
16427 return 4;
16428
16429 return 2;
16430}
16431
252b5132
RH
16432/* Estimate the size of a frag before relaxing. Unless this is the
16433 mips16, we are not really relaxing here, and the final size is
16434 encoded in the subtype information. For the mips16, we have to
16435 decide whether we are using an extended opcode or not. */
16436
252b5132 16437int
17a2f251 16438md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 16439{
5919d012 16440 int change;
252b5132 16441
4a6a3df4
AO
16442 if (RELAX_BRANCH_P (fragp->fr_subtype))
16443 {
16444
b34976b6
AM
16445 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
16446
4a6a3df4
AO
16447 return fragp->fr_var;
16448 }
16449
252b5132 16450 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
16451 /* We don't want to modify the EXTENDED bit here; it might get us
16452 into infinite loops. We change it only in mips_relax_frag(). */
16453 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132 16454
df58fc94
RS
16455 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
16456 {
16457 int length = 4;
16458
16459 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
16460 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
16461 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
16462 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
16463 fragp->fr_var = length;
16464
16465 return length;
16466 }
16467
252b5132 16468 if (mips_pic == NO_PIC)
5919d012 16469 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132 16470 else if (mips_pic == SVR4_PIC)
5919d012 16471 change = pic_need_relax (fragp->fr_symbol, segtype);
0a44bf69
RS
16472 else if (mips_pic == VXWORKS_PIC)
16473 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
16474 change = 0;
252b5132
RH
16475 else
16476 abort ();
16477
16478 if (change)
16479 {
4d7206a2 16480 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 16481 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 16482 }
4d7206a2
RS
16483 else
16484 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
16485}
16486
16487/* This is called to see whether a reloc against a defined symbol
de7e6852 16488 should be converted into a reloc against a section. */
252b5132
RH
16489
16490int
17a2f251 16491mips_fix_adjustable (fixS *fixp)
252b5132 16492{
252b5132
RH
16493 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
16494 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
16495 return 0;
a161fe53 16496
252b5132
RH
16497 if (fixp->fx_addsy == NULL)
16498 return 1;
a161fe53 16499
de7e6852
RS
16500 /* If symbol SYM is in a mergeable section, relocations of the form
16501 SYM + 0 can usually be made section-relative. The mergeable data
16502 is then identified by the section offset rather than by the symbol.
16503
16504 However, if we're generating REL LO16 relocations, the offset is split
16505 between the LO16 and parterning high part relocation. The linker will
16506 need to recalculate the complete offset in order to correctly identify
16507 the merge data.
16508
16509 The linker has traditionally not looked for the parterning high part
16510 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
16511 placed anywhere. Rather than break backwards compatibility by changing
16512 this, it seems better not to force the issue, and instead keep the
16513 original symbol. This will work with either linker behavior. */
738e5348 16514 if ((lo16_reloc_p (fixp->fx_r_type)
704803a9 16515 || reloc_needs_lo_p (fixp->fx_r_type))
de7e6852
RS
16516 && HAVE_IN_PLACE_ADDENDS
16517 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
16518 return 0;
16519
ce70d90a 16520 /* There is no place to store an in-place offset for JALR relocations.
2de39019
CM
16521 Likewise an in-range offset of limited PC-relative relocations may
16522 overflow the in-place relocatable field if recalculated against the
16523 start address of the symbol's containing section. */
ce70d90a 16524 if (HAVE_IN_PLACE_ADDENDS
2de39019
CM
16525 && (limited_pcrel_reloc_p (fixp->fx_r_type)
16526 || jalr_reloc_p (fixp->fx_r_type)))
1180b5a4
RS
16527 return 0;
16528
b314ec0e
RS
16529 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
16530 to a floating-point stub. The same is true for non-R_MIPS16_26
16531 relocations against MIPS16 functions; in this case, the stub becomes
16532 the function's canonical address.
16533
16534 Floating-point stubs are stored in unique .mips16.call.* or
16535 .mips16.fn.* sections. If a stub T for function F is in section S,
16536 the first relocation in section S must be against F; this is how the
16537 linker determines the target function. All relocations that might
16538 resolve to T must also be against F. We therefore have the following
16539 restrictions, which are given in an intentionally-redundant way:
16540
16541 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
16542 symbols.
16543
16544 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
16545 if that stub might be used.
16546
16547 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
16548 symbols.
16549
16550 4. We cannot reduce a stub's relocations against MIPS16 symbols if
16551 that stub might be used.
16552
16553 There is a further restriction:
16554
df58fc94
RS
16555 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
16556 R_MICROMIPS_26_S1) against MIPS16 or microMIPS symbols on
16557 targets with in-place addends; the relocation field cannot
b314ec0e
RS
16558 encode the low bit.
16559
df58fc94
RS
16560 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
16561 against a MIPS16 symbol. We deal with (5) by by not reducing any
16562 such relocations on REL targets.
b314ec0e
RS
16563
16564 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
16565 relocation against some symbol R, no relocation against R may be
16566 reduced. (Note that this deals with (2) as well as (1) because
16567 relocations against global symbols will never be reduced on ELF
16568 targets.) This approach is a little simpler than trying to detect
16569 stub sections, and gives the "all or nothing" per-symbol consistency
16570 that we have for MIPS16 symbols. */
f3ded42a 16571 if (fixp->fx_subsy == NULL
30c09090 16572 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
df58fc94
RS
16573 || *symbol_get_tc (fixp->fx_addsy)
16574 || (HAVE_IN_PLACE_ADDENDS
16575 && ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
16576 && jmp_reloc_p (fixp->fx_r_type))))
252b5132 16577 return 0;
a161fe53 16578
252b5132
RH
16579 return 1;
16580}
16581
16582/* Translate internal representation of relocation info to BFD target
16583 format. */
16584
16585arelent **
17a2f251 16586tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
16587{
16588 static arelent *retval[4];
16589 arelent *reloc;
16590 bfd_reloc_code_real_type code;
16591
4b0cff4e
TS
16592 memset (retval, 0, sizeof(retval));
16593 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
49309057
ILT
16594 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
16595 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
16596 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
16597
bad36eac
DJ
16598 if (fixp->fx_pcrel)
16599 {
df58fc94
RS
16600 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
16601 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
16602 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
b47468a6
CM
16603 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
16604 || fixp->fx_r_type == BFD_RELOC_32_PCREL);
bad36eac
DJ
16605
16606 /* At this point, fx_addnumber is "symbol offset - pcrel address".
16607 Relocations want only the symbol offset. */
16608 reloc->addend = fixp->fx_addnumber + reloc->address;
bad36eac
DJ
16609 }
16610 else
16611 reloc->addend = fixp->fx_addnumber;
252b5132 16612
438c16b8
TS
16613 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
16614 entry to be used in the relocation's section offset. */
16615 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
16616 {
16617 reloc->address = reloc->addend;
16618 reloc->addend = 0;
16619 }
16620
252b5132 16621 code = fixp->fx_r_type;
252b5132 16622
bad36eac 16623 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
252b5132
RH
16624 if (reloc->howto == NULL)
16625 {
16626 as_bad_where (fixp->fx_file, fixp->fx_line,
1661c76c
RS
16627 _("cannot represent %s relocation in this object file"
16628 " format"),
252b5132
RH
16629 bfd_get_reloc_code_name (code));
16630 retval[0] = NULL;
16631 }
16632
16633 return retval;
16634}
16635
16636/* Relax a machine dependent frag. This returns the amount by which
16637 the current size of the frag should change. */
16638
16639int
17a2f251 16640mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 16641{
4a6a3df4
AO
16642 if (RELAX_BRANCH_P (fragp->fr_subtype))
16643 {
16644 offsetT old_var = fragp->fr_var;
b34976b6
AM
16645
16646 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
16647
16648 return fragp->fr_var - old_var;
16649 }
16650
df58fc94
RS
16651 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
16652 {
16653 offsetT old_var = fragp->fr_var;
16654 offsetT new_var = 4;
16655
16656 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
16657 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
16658 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
16659 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
16660 fragp->fr_var = new_var;
16661
16662 return new_var - old_var;
16663 }
16664
252b5132
RH
16665 if (! RELAX_MIPS16_P (fragp->fr_subtype))
16666 return 0;
16667
c4e7957c 16668 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
16669 {
16670 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
16671 return 0;
16672 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
16673 return 2;
16674 }
16675 else
16676 {
16677 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
16678 return 0;
16679 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
16680 return -2;
16681 }
16682
16683 return 0;
16684}
16685
16686/* Convert a machine dependent frag. */
16687
16688void
17a2f251 16689md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 16690{
4a6a3df4
AO
16691 if (RELAX_BRANCH_P (fragp->fr_subtype))
16692 {
4d68580a 16693 char *buf;
4a6a3df4
AO
16694 unsigned long insn;
16695 expressionS exp;
16696 fixS *fixp;
b34976b6 16697
4d68580a
RS
16698 buf = fragp->fr_literal + fragp->fr_fix;
16699 insn = read_insn (buf);
b34976b6 16700
4a6a3df4
AO
16701 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
16702 {
16703 /* We generate a fixup instead of applying it right now
16704 because, if there are linker relaxations, we're going to
16705 need the relocations. */
16706 exp.X_op = O_symbol;
16707 exp.X_add_symbol = fragp->fr_symbol;
16708 exp.X_add_number = fragp->fr_offset;
16709
4d68580a
RS
16710 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
16711 BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
16712 fixp->fx_file = fragp->fr_file;
16713 fixp->fx_line = fragp->fr_line;
b34976b6 16714
4d68580a 16715 buf = write_insn (buf, insn);
4a6a3df4
AO
16716 }
16717 else
16718 {
16719 int i;
16720
16721 as_warn_where (fragp->fr_file, fragp->fr_line,
1661c76c 16722 _("relaxed out-of-range branch into a jump"));
4a6a3df4
AO
16723
16724 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
16725 goto uncond;
16726
16727 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
16728 {
16729 /* Reverse the branch. */
16730 switch ((insn >> 28) & 0xf)
16731 {
16732 case 4:
56d438b1
CF
16733 if ((insn & 0xff000000) == 0x47000000
16734 || (insn & 0xff600000) == 0x45600000)
16735 {
16736 /* BZ.df/BNZ.df, BZ.V/BNZ.V can have the condition
16737 reversed by tweaking bit 23. */
16738 insn ^= 0x00800000;
16739 }
16740 else
16741 {
16742 /* bc[0-3][tf]l? instructions can have the condition
16743 reversed by tweaking a single TF bit, and their
16744 opcodes all have 0x4???????. */
16745 gas_assert ((insn & 0xf3e00000) == 0x41000000);
16746 insn ^= 0x00010000;
16747 }
4a6a3df4
AO
16748 break;
16749
16750 case 0:
16751 /* bltz 0x04000000 bgez 0x04010000
54f4ddb3 16752 bltzal 0x04100000 bgezal 0x04110000 */
9c2799c2 16753 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
4a6a3df4
AO
16754 insn ^= 0x00010000;
16755 break;
b34976b6 16756
4a6a3df4
AO
16757 case 1:
16758 /* beq 0x10000000 bne 0x14000000
54f4ddb3 16759 blez 0x18000000 bgtz 0x1c000000 */
4a6a3df4
AO
16760 insn ^= 0x04000000;
16761 break;
16762
16763 default:
16764 abort ();
16765 }
16766 }
16767
16768 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
16769 {
16770 /* Clear the and-link bit. */
9c2799c2 16771 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
4a6a3df4 16772
54f4ddb3
TS
16773 /* bltzal 0x04100000 bgezal 0x04110000
16774 bltzall 0x04120000 bgezall 0x04130000 */
4a6a3df4
AO
16775 insn &= ~0x00100000;
16776 }
16777
16778 /* Branch over the branch (if the branch was likely) or the
16779 full jump (not likely case). Compute the offset from the
16780 current instruction to branch to. */
16781 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
16782 i = 16;
16783 else
16784 {
16785 /* How many bytes in instructions we've already emitted? */
4d68580a 16786 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
16787 /* How many bytes in instructions from here to the end? */
16788 i = fragp->fr_var - i;
16789 }
16790 /* Convert to instruction count. */
16791 i >>= 2;
16792 /* Branch counts from the next instruction. */
b34976b6 16793 i--;
4a6a3df4
AO
16794 insn |= i;
16795 /* Branch over the jump. */
4d68580a 16796 buf = write_insn (buf, insn);
4a6a3df4 16797
54f4ddb3 16798 /* nop */
4d68580a 16799 buf = write_insn (buf, 0);
4a6a3df4
AO
16800
16801 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
16802 {
16803 /* beql $0, $0, 2f */
16804 insn = 0x50000000;
16805 /* Compute the PC offset from the current instruction to
16806 the end of the variable frag. */
16807 /* How many bytes in instructions we've already emitted? */
4d68580a 16808 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
16809 /* How many bytes in instructions from here to the end? */
16810 i = fragp->fr_var - i;
16811 /* Convert to instruction count. */
16812 i >>= 2;
16813 /* Don't decrement i, because we want to branch over the
16814 delay slot. */
4a6a3df4 16815 insn |= i;
4a6a3df4 16816
4d68580a
RS
16817 buf = write_insn (buf, insn);
16818 buf = write_insn (buf, 0);
4a6a3df4
AO
16819 }
16820
16821 uncond:
16822 if (mips_pic == NO_PIC)
16823 {
16824 /* j or jal. */
16825 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
16826 ? 0x0c000000 : 0x08000000);
16827 exp.X_op = O_symbol;
16828 exp.X_add_symbol = fragp->fr_symbol;
16829 exp.X_add_number = fragp->fr_offset;
16830
4d68580a
RS
16831 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
16832 FALSE, BFD_RELOC_MIPS_JMP);
4a6a3df4
AO
16833 fixp->fx_file = fragp->fr_file;
16834 fixp->fx_line = fragp->fr_line;
16835
4d68580a 16836 buf = write_insn (buf, insn);
4a6a3df4
AO
16837 }
16838 else
16839 {
66b3e8da
MR
16840 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
16841
4a6a3df4 16842 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
66b3e8da
MR
16843 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
16844 insn |= at << OP_SH_RT;
4a6a3df4
AO
16845 exp.X_op = O_symbol;
16846 exp.X_add_symbol = fragp->fr_symbol;
16847 exp.X_add_number = fragp->fr_offset;
16848
16849 if (fragp->fr_offset)
16850 {
16851 exp.X_add_symbol = make_expr_symbol (&exp);
16852 exp.X_add_number = 0;
16853 }
16854
4d68580a
RS
16855 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
16856 FALSE, BFD_RELOC_MIPS_GOT16);
4a6a3df4
AO
16857 fixp->fx_file = fragp->fr_file;
16858 fixp->fx_line = fragp->fr_line;
16859
4d68580a 16860 buf = write_insn (buf, insn);
b34976b6 16861
4a6a3df4 16862 if (mips_opts.isa == ISA_MIPS1)
4d68580a
RS
16863 /* nop */
16864 buf = write_insn (buf, 0);
4a6a3df4
AO
16865
16866 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
66b3e8da
MR
16867 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
16868 insn |= at << OP_SH_RS | at << OP_SH_RT;
4a6a3df4 16869
4d68580a
RS
16870 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
16871 FALSE, BFD_RELOC_LO16);
4a6a3df4
AO
16872 fixp->fx_file = fragp->fr_file;
16873 fixp->fx_line = fragp->fr_line;
b34976b6 16874
4d68580a 16875 buf = write_insn (buf, insn);
4a6a3df4
AO
16876
16877 /* j(al)r $at. */
16878 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
66b3e8da 16879 insn = 0x0000f809;
4a6a3df4 16880 else
66b3e8da
MR
16881 insn = 0x00000008;
16882 insn |= at << OP_SH_RS;
4a6a3df4 16883
4d68580a 16884 buf = write_insn (buf, insn);
4a6a3df4
AO
16885 }
16886 }
16887
4a6a3df4 16888 fragp->fr_fix += fragp->fr_var;
4d68580a 16889 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
4a6a3df4
AO
16890 return;
16891 }
16892
df58fc94
RS
16893 /* Relax microMIPS branches. */
16894 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
16895 {
4d68580a 16896 char *buf = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
16897 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
16898 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
16899 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
2309ddf2 16900 bfd_boolean short_ds;
df58fc94
RS
16901 unsigned long insn;
16902 expressionS exp;
16903 fixS *fixp;
16904
16905 exp.X_op = O_symbol;
16906 exp.X_add_symbol = fragp->fr_symbol;
16907 exp.X_add_number = fragp->fr_offset;
16908
16909 fragp->fr_fix += fragp->fr_var;
16910
16911 /* Handle 16-bit branches that fit or are forced to fit. */
16912 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
16913 {
16914 /* We generate a fixup instead of applying it right now,
16915 because if there is linker relaxation, we're going to
16916 need the relocations. */
16917 if (type == 'D')
4d68580a 16918 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
df58fc94
RS
16919 BFD_RELOC_MICROMIPS_10_PCREL_S1);
16920 else if (type == 'E')
4d68580a 16921 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
df58fc94
RS
16922 BFD_RELOC_MICROMIPS_7_PCREL_S1);
16923 else
16924 abort ();
16925
16926 fixp->fx_file = fragp->fr_file;
16927 fixp->fx_line = fragp->fr_line;
16928
16929 /* These relocations can have an addend that won't fit in
16930 2 octets. */
16931 fixp->fx_no_overflow = 1;
16932
16933 return;
16934 }
16935
2309ddf2 16936 /* Handle 32-bit branches that fit or are forced to fit. */
df58fc94
RS
16937 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
16938 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
16939 {
16940 /* We generate a fixup instead of applying it right now,
16941 because if there is linker relaxation, we're going to
16942 need the relocations. */
4d68580a
RS
16943 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
16944 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
16945 fixp->fx_file = fragp->fr_file;
16946 fixp->fx_line = fragp->fr_line;
16947
16948 if (type == 0)
16949 return;
16950 }
16951
16952 /* Relax 16-bit branches to 32-bit branches. */
16953 if (type != 0)
16954 {
4d68580a 16955 insn = read_compressed_insn (buf, 2);
df58fc94
RS
16956
16957 if ((insn & 0xfc00) == 0xcc00) /* b16 */
16958 insn = 0x94000000; /* beq */
16959 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
16960 {
16961 unsigned long regno;
16962
16963 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
16964 regno = micromips_to_32_reg_d_map [regno];
16965 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
16966 insn |= regno << MICROMIPSOP_SH_RS;
16967 }
16968 else
16969 abort ();
16970
16971 /* Nothing else to do, just write it out. */
16972 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
16973 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
16974 {
4d68580a
RS
16975 buf = write_compressed_insn (buf, insn, 4);
16976 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
16977 return;
16978 }
16979 }
16980 else
4d68580a 16981 insn = read_compressed_insn (buf, 4);
df58fc94
RS
16982
16983 /* Relax 32-bit branches to a sequence of instructions. */
16984 as_warn_where (fragp->fr_file, fragp->fr_line,
1661c76c 16985 _("relaxed out-of-range branch into a jump"));
df58fc94 16986
2309ddf2
MR
16987 /* Set the short-delay-slot bit. */
16988 short_ds = al && (insn & 0x02000000) != 0;
df58fc94
RS
16989
16990 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
16991 {
16992 symbolS *l;
16993
16994 /* Reverse the branch. */
16995 if ((insn & 0xfc000000) == 0x94000000 /* beq */
16996 || (insn & 0xfc000000) == 0xb4000000) /* bne */
16997 insn ^= 0x20000000;
16998 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
16999 || (insn & 0xffe00000) == 0x40400000 /* bgez */
17000 || (insn & 0xffe00000) == 0x40800000 /* blez */
17001 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
17002 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
17003 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
17004 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
17005 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
17006 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
17007 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
17008 insn ^= 0x00400000;
17009 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
17010 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
17011 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
17012 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
17013 insn ^= 0x00200000;
56d438b1
CF
17014 else if ((insn & 0xff000000) == 0x83000000 /* BZ.df
17015 BNZ.df */
17016 || (insn & 0xff600000) == 0x81600000) /* BZ.V
17017 BNZ.V */
17018 insn ^= 0x00800000;
df58fc94
RS
17019 else
17020 abort ();
17021
17022 if (al)
17023 {
17024 /* Clear the and-link and short-delay-slot bits. */
17025 gas_assert ((insn & 0xfda00000) == 0x40200000);
17026
17027 /* bltzal 0x40200000 bgezal 0x40600000 */
17028 /* bltzals 0x42200000 bgezals 0x42600000 */
17029 insn &= ~0x02200000;
17030 }
17031
17032 /* Make a label at the end for use with the branch. */
17033 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
17034 micromips_label_inc ();
f3ded42a 17035 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
df58fc94
RS
17036
17037 /* Refer to it. */
4d68580a
RS
17038 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
17039 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
17040 fixp->fx_file = fragp->fr_file;
17041 fixp->fx_line = fragp->fr_line;
17042
17043 /* Branch over the jump. */
4d68580a 17044 buf = write_compressed_insn (buf, insn, 4);
df58fc94 17045 if (!compact)
4d68580a
RS
17046 /* nop */
17047 buf = write_compressed_insn (buf, 0x0c00, 2);
df58fc94
RS
17048 }
17049
17050 if (mips_pic == NO_PIC)
17051 {
2309ddf2
MR
17052 unsigned long jal = short_ds ? 0x74000000 : 0xf4000000; /* jal/s */
17053
df58fc94
RS
17054 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
17055 insn = al ? jal : 0xd4000000;
17056
4d68580a
RS
17057 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
17058 BFD_RELOC_MICROMIPS_JMP);
df58fc94
RS
17059 fixp->fx_file = fragp->fr_file;
17060 fixp->fx_line = fragp->fr_line;
17061
4d68580a 17062 buf = write_compressed_insn (buf, insn, 4);
df58fc94 17063 if (compact)
4d68580a
RS
17064 /* nop */
17065 buf = write_compressed_insn (buf, 0x0c00, 2);
df58fc94
RS
17066 }
17067 else
17068 {
17069 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
2309ddf2
MR
17070 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
17071 unsigned long jr = compact ? 0x45a0 : 0x4580; /* jr/c */
df58fc94
RS
17072
17073 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
17074 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
17075 insn |= at << MICROMIPSOP_SH_RT;
17076
17077 if (exp.X_add_number)
17078 {
17079 exp.X_add_symbol = make_expr_symbol (&exp);
17080 exp.X_add_number = 0;
17081 }
17082
4d68580a
RS
17083 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
17084 BFD_RELOC_MICROMIPS_GOT16);
df58fc94
RS
17085 fixp->fx_file = fragp->fr_file;
17086 fixp->fx_line = fragp->fr_line;
17087
4d68580a 17088 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
17089
17090 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
17091 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
17092 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
17093
4d68580a
RS
17094 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
17095 BFD_RELOC_MICROMIPS_LO16);
df58fc94
RS
17096 fixp->fx_file = fragp->fr_file;
17097 fixp->fx_line = fragp->fr_line;
17098
4d68580a 17099 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
17100
17101 /* jr/jrc/jalr/jalrs $at */
17102 insn = al ? jalr : jr;
17103 insn |= at << MICROMIPSOP_SH_MJ;
17104
4d68580a 17105 buf = write_compressed_insn (buf, insn, 2);
df58fc94
RS
17106 }
17107
4d68580a 17108 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
17109 return;
17110 }
17111
252b5132
RH
17112 if (RELAX_MIPS16_P (fragp->fr_subtype))
17113 {
17114 int type;
3ccad066 17115 const struct mips_int_operand *operand;
252b5132 17116 offsetT val;
5c04167a
RS
17117 char *buf;
17118 unsigned int user_length, length;
252b5132 17119 unsigned long insn;
5c04167a 17120 bfd_boolean ext;
252b5132
RH
17121
17122 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
3ccad066 17123 operand = mips16_immed_operand (type, FALSE);
252b5132 17124
5c04167a 17125 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
5f5f22c0 17126 val = resolve_symbol_value (fragp->fr_symbol);
3ccad066 17127 if (operand->root.type == OP_PCREL)
252b5132 17128 {
3ccad066 17129 const struct mips_pcrel_operand *pcrel_op;
252b5132
RH
17130 addressT addr;
17131
3ccad066 17132 pcrel_op = (const struct mips_pcrel_operand *) operand;
252b5132
RH
17133 addr = fragp->fr_address + fragp->fr_fix;
17134
17135 /* The rules for the base address of a PC relative reloc are
17136 complicated; see mips16_extended_frag. */
3ccad066 17137 if (pcrel_op->include_isa_bit)
252b5132
RH
17138 {
17139 addr += 2;
17140 if (ext)
17141 addr += 2;
17142 /* Ignore the low bit in the target, since it will be
17143 set for a text label. */
3ccad066 17144 val &= -2;
252b5132
RH
17145 }
17146 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17147 addr -= 4;
17148 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17149 addr -= 2;
17150
3ccad066 17151 addr &= -(1 << pcrel_op->align_log2);
252b5132
RH
17152 val -= addr;
17153
17154 /* Make sure the section winds up with the alignment we have
17155 assumed. */
3ccad066
RS
17156 if (operand->shift > 0)
17157 record_alignment (asec, operand->shift);
252b5132
RH
17158 }
17159
17160 if (ext
17161 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
17162 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
17163 as_warn_where (fragp->fr_file, fragp->fr_line,
17164 _("extended instruction in delay slot"));
17165
5c04167a 17166 buf = fragp->fr_literal + fragp->fr_fix;
252b5132 17167
4d68580a 17168 insn = read_compressed_insn (buf, 2);
5c04167a
RS
17169 if (ext)
17170 insn |= MIPS16_EXTEND;
252b5132 17171
5c04167a
RS
17172 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17173 user_length = 4;
17174 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17175 user_length = 2;
17176 else
17177 user_length = 0;
17178
43c0598f 17179 mips16_immed (fragp->fr_file, fragp->fr_line, type,
c150d1d2 17180 BFD_RELOC_UNUSED, val, user_length, &insn);
252b5132 17181
5c04167a
RS
17182 length = (ext ? 4 : 2);
17183 gas_assert (mips16_opcode_length (insn) == length);
17184 write_compressed_insn (buf, insn, length);
17185 fragp->fr_fix += length;
252b5132
RH
17186 }
17187 else
17188 {
df58fc94
RS
17189 relax_substateT subtype = fragp->fr_subtype;
17190 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
17191 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
4d7206a2
RS
17192 int first, second;
17193 fixS *fixp;
252b5132 17194
df58fc94
RS
17195 first = RELAX_FIRST (subtype);
17196 second = RELAX_SECOND (subtype);
4d7206a2 17197 fixp = (fixS *) fragp->fr_opcode;
252b5132 17198
df58fc94
RS
17199 /* If the delay slot chosen does not match the size of the instruction,
17200 then emit a warning. */
17201 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
17202 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
17203 {
17204 relax_substateT s;
17205 const char *msg;
17206
17207 s = subtype & (RELAX_DELAY_SLOT_16BIT
17208 | RELAX_DELAY_SLOT_SIZE_FIRST
17209 | RELAX_DELAY_SLOT_SIZE_SECOND);
17210 msg = macro_warning (s);
17211 if (msg != NULL)
db9b2be4 17212 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94
RS
17213 subtype &= ~s;
17214 }
17215
584892a6 17216 /* Possibly emit a warning if we've chosen the longer option. */
df58fc94 17217 if (use_second == second_longer)
584892a6 17218 {
df58fc94
RS
17219 relax_substateT s;
17220 const char *msg;
17221
17222 s = (subtype
17223 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
17224 msg = macro_warning (s);
17225 if (msg != NULL)
db9b2be4 17226 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94 17227 subtype &= ~s;
584892a6
RS
17228 }
17229
4d7206a2
RS
17230 /* Go through all the fixups for the first sequence. Disable them
17231 (by marking them as done) if we're going to use the second
17232 sequence instead. */
17233 while (fixp
17234 && fixp->fx_frag == fragp
17235 && fixp->fx_where < fragp->fr_fix - second)
17236 {
df58fc94 17237 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
17238 fixp->fx_done = 1;
17239 fixp = fixp->fx_next;
17240 }
252b5132 17241
4d7206a2
RS
17242 /* Go through the fixups for the second sequence. Disable them if
17243 we're going to use the first sequence, otherwise adjust their
17244 addresses to account for the relaxation. */
17245 while (fixp && fixp->fx_frag == fragp)
17246 {
df58fc94 17247 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
17248 fixp->fx_where -= first;
17249 else
17250 fixp->fx_done = 1;
17251 fixp = fixp->fx_next;
17252 }
17253
17254 /* Now modify the frag contents. */
df58fc94 17255 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
17256 {
17257 char *start;
17258
17259 start = fragp->fr_literal + fragp->fr_fix - first - second;
17260 memmove (start, start + first, second);
17261 fragp->fr_fix -= first;
17262 }
17263 else
17264 fragp->fr_fix -= second;
252b5132
RH
17265 }
17266}
17267
252b5132
RH
17268/* This function is called after the relocs have been generated.
17269 We've been storing mips16 text labels as odd. Here we convert them
17270 back to even for the convenience of the debugger. */
17271
17272void
17a2f251 17273mips_frob_file_after_relocs (void)
252b5132
RH
17274{
17275 asymbol **syms;
17276 unsigned int count, i;
17277
252b5132
RH
17278 syms = bfd_get_outsymbols (stdoutput);
17279 count = bfd_get_symcount (stdoutput);
17280 for (i = 0; i < count; i++, syms++)
df58fc94
RS
17281 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
17282 && ((*syms)->value & 1) != 0)
17283 {
17284 (*syms)->value &= ~1;
17285 /* If the symbol has an odd size, it was probably computed
17286 incorrectly, so adjust that as well. */
17287 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
17288 ++elf_symbol (*syms)->internal_elf_sym.st_size;
17289 }
252b5132
RH
17290}
17291
a1facbec
MR
17292/* This function is called whenever a label is defined, including fake
17293 labels instantiated off the dot special symbol. It is used when
17294 handling branch delays; if a branch has a label, we assume we cannot
17295 move it. This also bumps the value of the symbol by 1 in compressed
17296 code. */
252b5132 17297
e1b47bd5 17298static void
a1facbec 17299mips_record_label (symbolS *sym)
252b5132 17300{
a8dbcb85 17301 segment_info_type *si = seg_info (now_seg);
252b5132
RH
17302 struct insn_label_list *l;
17303
17304 if (free_insn_labels == NULL)
17305 l = (struct insn_label_list *) xmalloc (sizeof *l);
17306 else
17307 {
17308 l = free_insn_labels;
17309 free_insn_labels = l->next;
17310 }
17311
17312 l->label = sym;
a8dbcb85
TS
17313 l->next = si->label_list;
17314 si->label_list = l;
a1facbec 17315}
07a53e5c 17316
a1facbec
MR
17317/* This function is called as tc_frob_label() whenever a label is defined
17318 and adds a DWARF-2 record we only want for true labels. */
17319
17320void
17321mips_define_label (symbolS *sym)
17322{
17323 mips_record_label (sym);
07a53e5c 17324 dwarf2_emit_label (sym);
252b5132 17325}
e1b47bd5
RS
17326
17327/* This function is called by tc_new_dot_label whenever a new dot symbol
17328 is defined. */
17329
17330void
17331mips_add_dot_label (symbolS *sym)
17332{
17333 mips_record_label (sym);
17334 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
17335 mips_compressed_mark_label (sym);
17336}
252b5132 17337\f
252b5132
RH
17338/* Some special processing for a MIPS ELF file. */
17339
17340void
17a2f251 17341mips_elf_final_processing (void)
252b5132
RH
17342{
17343 /* Write out the register information. */
316f5878 17344 if (mips_abi != N64_ABI)
252b5132
RH
17345 {
17346 Elf32_RegInfo s;
17347
17348 s.ri_gprmask = mips_gprmask;
17349 s.ri_cprmask[0] = mips_cprmask[0];
17350 s.ri_cprmask[1] = mips_cprmask[1];
17351 s.ri_cprmask[2] = mips_cprmask[2];
17352 s.ri_cprmask[3] = mips_cprmask[3];
17353 /* The gp_value field is set by the MIPS ELF backend. */
17354
17355 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
17356 ((Elf32_External_RegInfo *)
17357 mips_regmask_frag));
17358 }
17359 else
17360 {
17361 Elf64_Internal_RegInfo s;
17362
17363 s.ri_gprmask = mips_gprmask;
17364 s.ri_pad = 0;
17365 s.ri_cprmask[0] = mips_cprmask[0];
17366 s.ri_cprmask[1] = mips_cprmask[1];
17367 s.ri_cprmask[2] = mips_cprmask[2];
17368 s.ri_cprmask[3] = mips_cprmask[3];
17369 /* The gp_value field is set by the MIPS ELF backend. */
17370
17371 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
17372 ((Elf64_External_RegInfo *)
17373 mips_regmask_frag));
17374 }
17375
17376 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
17377 sort of BFD interface for this. */
17378 if (mips_any_noreorder)
17379 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
17380 if (mips_pic != NO_PIC)
143d77c5 17381 {
8b828383 17382 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
17383 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
17384 }
17385 if (mips_abicalls)
17386 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 17387
b015e599
AP
17388 /* Set MIPS ELF flags for ASEs. Note that not all ASEs have flags
17389 defined at present; this might need to change in future. */
a4672219
TS
17390 if (file_ase_mips16)
17391 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
df58fc94
RS
17392 if (file_ase_micromips)
17393 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
846ef2d0 17394 if (file_ase & ASE_MDMX)
deec1734 17395 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 17396
bdaaa2e1 17397 /* Set the MIPS ELF ABI flags. */
316f5878 17398 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 17399 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 17400 else if (mips_abi == O64_ABI)
252b5132 17401 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 17402 else if (mips_abi == EABI_ABI)
252b5132 17403 {
bad1aba3 17404 if (file_mips_opts.gp == 64)
252b5132
RH
17405 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
17406 else
17407 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
17408 }
316f5878 17409 else if (mips_abi == N32_ABI)
be00bddd
TS
17410 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
17411
c9914766 17412 /* Nothing to do for N64_ABI. */
252b5132
RH
17413
17414 if (mips_32bitmode)
17415 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
ad3fea08 17416
ba92f887
MR
17417 if (mips_flag_nan2008)
17418 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NAN2008;
17419
ad3fea08 17420 /* 32 bit code with 64 bit FP registers. */
0b35dfee 17421 if (file_mips_opts.fp == 64 && ABI_NEEDS_32BIT_REGS (mips_abi))
f1c38003 17422 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_FP64;
252b5132 17423}
252b5132 17424\f
beae10d5 17425typedef struct proc {
9b2f1d35
EC
17426 symbolS *func_sym;
17427 symbolS *func_end_sym;
beae10d5
KH
17428 unsigned long reg_mask;
17429 unsigned long reg_offset;
17430 unsigned long fpreg_mask;
17431 unsigned long fpreg_offset;
17432 unsigned long frame_offset;
17433 unsigned long frame_reg;
17434 unsigned long pc_reg;
17435} procS;
252b5132
RH
17436
17437static procS cur_proc;
17438static procS *cur_proc_ptr;
17439static int numprocs;
17440
df58fc94
RS
17441/* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
17442 as "2", and a normal nop as "0". */
17443
17444#define NOP_OPCODE_MIPS 0
17445#define NOP_OPCODE_MIPS16 1
17446#define NOP_OPCODE_MICROMIPS 2
742a56fe
RS
17447
17448char
17449mips_nop_opcode (void)
17450{
df58fc94
RS
17451 if (seg_info (now_seg)->tc_segment_info_data.micromips)
17452 return NOP_OPCODE_MICROMIPS;
17453 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
17454 return NOP_OPCODE_MIPS16;
17455 else
17456 return NOP_OPCODE_MIPS;
742a56fe
RS
17457}
17458
df58fc94
RS
17459/* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
17460 32-bit microMIPS NOPs here (if applicable). */
a19d8eb0 17461
0a9ef439 17462void
17a2f251 17463mips_handle_align (fragS *fragp)
a19d8eb0 17464{
df58fc94 17465 char nop_opcode;
742a56fe 17466 char *p;
c67a084a
NC
17467 int bytes, size, excess;
17468 valueT opcode;
742a56fe 17469
0a9ef439
RH
17470 if (fragp->fr_type != rs_align_code)
17471 return;
17472
742a56fe 17473 p = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
17474 nop_opcode = *p;
17475 switch (nop_opcode)
a19d8eb0 17476 {
df58fc94
RS
17477 case NOP_OPCODE_MICROMIPS:
17478 opcode = micromips_nop32_insn.insn_opcode;
17479 size = 4;
17480 break;
17481 case NOP_OPCODE_MIPS16:
c67a084a
NC
17482 opcode = mips16_nop_insn.insn_opcode;
17483 size = 2;
df58fc94
RS
17484 break;
17485 case NOP_OPCODE_MIPS:
17486 default:
c67a084a
NC
17487 opcode = nop_insn.insn_opcode;
17488 size = 4;
df58fc94 17489 break;
c67a084a 17490 }
a19d8eb0 17491
c67a084a
NC
17492 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
17493 excess = bytes % size;
df58fc94
RS
17494
17495 /* Handle the leading part if we're not inserting a whole number of
17496 instructions, and make it the end of the fixed part of the frag.
17497 Try to fit in a short microMIPS NOP if applicable and possible,
17498 and use zeroes otherwise. */
17499 gas_assert (excess < 4);
17500 fragp->fr_fix += excess;
17501 switch (excess)
c67a084a 17502 {
df58fc94
RS
17503 case 3:
17504 *p++ = '\0';
17505 /* Fall through. */
17506 case 2:
833794fc 17507 if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
df58fc94 17508 {
4d68580a 17509 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
df58fc94
RS
17510 break;
17511 }
17512 *p++ = '\0';
17513 /* Fall through. */
17514 case 1:
17515 *p++ = '\0';
17516 /* Fall through. */
17517 case 0:
17518 break;
a19d8eb0 17519 }
c67a084a
NC
17520
17521 md_number_to_chars (p, opcode, size);
17522 fragp->fr_var = size;
a19d8eb0
CP
17523}
17524
252b5132 17525static void
17a2f251 17526md_obj_begin (void)
252b5132
RH
17527{
17528}
17529
17530static void
17a2f251 17531md_obj_end (void)
252b5132 17532{
54f4ddb3 17533 /* Check for premature end, nesting errors, etc. */
252b5132 17534 if (cur_proc_ptr)
9a41af64 17535 as_warn (_("missing .end at end of assembly"));
252b5132
RH
17536}
17537
17538static long
17a2f251 17539get_number (void)
252b5132
RH
17540{
17541 int negative = 0;
17542 long val = 0;
17543
17544 if (*input_line_pointer == '-')
17545 {
17546 ++input_line_pointer;
17547 negative = 1;
17548 }
3882b010 17549 if (!ISDIGIT (*input_line_pointer))
956cd1d6 17550 as_bad (_("expected simple number"));
252b5132
RH
17551 if (input_line_pointer[0] == '0')
17552 {
17553 if (input_line_pointer[1] == 'x')
17554 {
17555 input_line_pointer += 2;
3882b010 17556 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
17557 {
17558 val <<= 4;
17559 val |= hex_value (*input_line_pointer++);
17560 }
17561 return negative ? -val : val;
17562 }
17563 else
17564 {
17565 ++input_line_pointer;
3882b010 17566 while (ISDIGIT (*input_line_pointer))
252b5132
RH
17567 {
17568 val <<= 3;
17569 val |= *input_line_pointer++ - '0';
17570 }
17571 return negative ? -val : val;
17572 }
17573 }
3882b010 17574 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
17575 {
17576 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
17577 *input_line_pointer, *input_line_pointer);
956cd1d6 17578 as_warn (_("invalid number"));
252b5132
RH
17579 return -1;
17580 }
3882b010 17581 while (ISDIGIT (*input_line_pointer))
252b5132
RH
17582 {
17583 val *= 10;
17584 val += *input_line_pointer++ - '0';
17585 }
17586 return negative ? -val : val;
17587}
17588
17589/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
17590 is an initial number which is the ECOFF file index. In the non-ECOFF
17591 case .file implies DWARF-2. */
17592
17593static void
17a2f251 17594s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 17595{
ecb4347a
DJ
17596 static int first_file_directive = 0;
17597
c5dd6aab
DJ
17598 if (ECOFF_DEBUGGING)
17599 {
17600 get_number ();
17601 s_app_file (0);
17602 }
17603 else
ecb4347a
DJ
17604 {
17605 char *filename;
17606
17607 filename = dwarf2_directive_file (0);
17608
17609 /* Versions of GCC up to 3.1 start files with a ".file"
17610 directive even for stabs output. Make sure that this
17611 ".file" is handled. Note that you need a version of GCC
17612 after 3.1 in order to support DWARF-2 on MIPS. */
17613 if (filename != NULL && ! first_file_directive)
17614 {
17615 (void) new_logical_line (filename, -1);
c04f5787 17616 s_app_file_string (filename, 0);
ecb4347a
DJ
17617 }
17618 first_file_directive = 1;
17619 }
c5dd6aab
DJ
17620}
17621
17622/* The .loc directive, implying DWARF-2. */
252b5132
RH
17623
17624static void
17a2f251 17625s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 17626{
c5dd6aab
DJ
17627 if (!ECOFF_DEBUGGING)
17628 dwarf2_directive_loc (0);
252b5132
RH
17629}
17630
252b5132
RH
17631/* The .end directive. */
17632
17633static void
17a2f251 17634s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
17635{
17636 symbolS *p;
252b5132 17637
7a621144
DJ
17638 /* Following functions need their own .frame and .cprestore directives. */
17639 mips_frame_reg_valid = 0;
17640 mips_cprestore_valid = 0;
17641
252b5132
RH
17642 if (!is_end_of_line[(unsigned char) *input_line_pointer])
17643 {
17644 p = get_symbol ();
17645 demand_empty_rest_of_line ();
17646 }
17647 else
17648 p = NULL;
17649
14949570 17650 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
17651 as_warn (_(".end not in text section"));
17652
17653 if (!cur_proc_ptr)
17654 {
1661c76c 17655 as_warn (_(".end directive without a preceding .ent directive"));
252b5132
RH
17656 demand_empty_rest_of_line ();
17657 return;
17658 }
17659
17660 if (p != NULL)
17661 {
9c2799c2 17662 gas_assert (S_GET_NAME (p));
9b2f1d35 17663 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
1661c76c 17664 as_warn (_(".end symbol does not match .ent symbol"));
ecb4347a
DJ
17665
17666 if (debug_type == DEBUG_STABS)
17667 stabs_generate_asm_endfunc (S_GET_NAME (p),
17668 S_GET_NAME (p));
252b5132
RH
17669 }
17670 else
17671 as_warn (_(".end directive missing or unknown symbol"));
17672
9b2f1d35
EC
17673 /* Create an expression to calculate the size of the function. */
17674 if (p && cur_proc_ptr)
17675 {
17676 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
17677 expressionS *exp = xmalloc (sizeof (expressionS));
17678
17679 obj->size = exp;
17680 exp->X_op = O_subtract;
17681 exp->X_add_symbol = symbol_temp_new_now ();
17682 exp->X_op_symbol = p;
17683 exp->X_add_number = 0;
17684
17685 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
17686 }
17687
ecb4347a 17688 /* Generate a .pdr section. */
f3ded42a 17689 if (!ECOFF_DEBUGGING && mips_flag_pdr)
ecb4347a
DJ
17690 {
17691 segT saved_seg = now_seg;
17692 subsegT saved_subseg = now_subseg;
ecb4347a
DJ
17693 expressionS exp;
17694 char *fragp;
252b5132 17695
252b5132 17696#ifdef md_flush_pending_output
ecb4347a 17697 md_flush_pending_output ();
252b5132
RH
17698#endif
17699
9c2799c2 17700 gas_assert (pdr_seg);
ecb4347a 17701 subseg_set (pdr_seg, 0);
252b5132 17702
ecb4347a
DJ
17703 /* Write the symbol. */
17704 exp.X_op = O_symbol;
17705 exp.X_add_symbol = p;
17706 exp.X_add_number = 0;
17707 emit_expr (&exp, 4);
252b5132 17708
ecb4347a 17709 fragp = frag_more (7 * 4);
252b5132 17710
17a2f251
TS
17711 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
17712 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
17713 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
17714 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
17715 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
17716 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
17717 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 17718
ecb4347a
DJ
17719 subseg_set (saved_seg, saved_subseg);
17720 }
252b5132
RH
17721
17722 cur_proc_ptr = NULL;
17723}
17724
17725/* The .aent and .ent directives. */
17726
17727static void
17a2f251 17728s_mips_ent (int aent)
252b5132 17729{
252b5132 17730 symbolS *symbolP;
252b5132
RH
17731
17732 symbolP = get_symbol ();
17733 if (*input_line_pointer == ',')
f9419b05 17734 ++input_line_pointer;
252b5132 17735 SKIP_WHITESPACE ();
3882b010 17736 if (ISDIGIT (*input_line_pointer)
d9a62219 17737 || *input_line_pointer == '-')
874e8986 17738 get_number ();
252b5132 17739
14949570 17740 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
1661c76c 17741 as_warn (_(".ent or .aent not in text section"));
252b5132
RH
17742
17743 if (!aent && cur_proc_ptr)
9a41af64 17744 as_warn (_("missing .end"));
252b5132
RH
17745
17746 if (!aent)
17747 {
7a621144
DJ
17748 /* This function needs its own .frame and .cprestore directives. */
17749 mips_frame_reg_valid = 0;
17750 mips_cprestore_valid = 0;
17751
252b5132
RH
17752 cur_proc_ptr = &cur_proc;
17753 memset (cur_proc_ptr, '\0', sizeof (procS));
17754
9b2f1d35 17755 cur_proc_ptr->func_sym = symbolP;
252b5132 17756
f9419b05 17757 ++numprocs;
ecb4347a
DJ
17758
17759 if (debug_type == DEBUG_STABS)
17760 stabs_generate_asm_func (S_GET_NAME (symbolP),
17761 S_GET_NAME (symbolP));
252b5132
RH
17762 }
17763
7c0fc524
MR
17764 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
17765
252b5132
RH
17766 demand_empty_rest_of_line ();
17767}
17768
17769/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 17770 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 17771 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 17772 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
17773 symbol table (in the mdebug section). */
17774
17775static void
17a2f251 17776s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 17777{
f3ded42a
RS
17778 if (ECOFF_DEBUGGING)
17779 s_ignore (ignore);
17780 else
ecb4347a
DJ
17781 {
17782 long val;
252b5132 17783
ecb4347a
DJ
17784 if (cur_proc_ptr == (procS *) NULL)
17785 {
17786 as_warn (_(".frame outside of .ent"));
17787 demand_empty_rest_of_line ();
17788 return;
17789 }
252b5132 17790
ecb4347a
DJ
17791 cur_proc_ptr->frame_reg = tc_get_register (1);
17792
17793 SKIP_WHITESPACE ();
17794 if (*input_line_pointer++ != ','
17795 || get_absolute_expression_and_terminator (&val) != ',')
17796 {
1661c76c 17797 as_warn (_("bad .frame directive"));
ecb4347a
DJ
17798 --input_line_pointer;
17799 demand_empty_rest_of_line ();
17800 return;
17801 }
252b5132 17802
ecb4347a
DJ
17803 cur_proc_ptr->frame_offset = val;
17804 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 17805
252b5132 17806 demand_empty_rest_of_line ();
252b5132 17807 }
252b5132
RH
17808}
17809
bdaaa2e1
KH
17810/* The .fmask and .mask directives. If the mdebug section is present
17811 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 17812 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 17813 information correctly. We can't use the ecoff routines because they
252b5132
RH
17814 make reference to the ecoff symbol table (in the mdebug section). */
17815
17816static void
17a2f251 17817s_mips_mask (int reg_type)
252b5132 17818{
f3ded42a
RS
17819 if (ECOFF_DEBUGGING)
17820 s_ignore (reg_type);
17821 else
252b5132 17822 {
ecb4347a 17823 long mask, off;
252b5132 17824
ecb4347a
DJ
17825 if (cur_proc_ptr == (procS *) NULL)
17826 {
17827 as_warn (_(".mask/.fmask outside of .ent"));
17828 demand_empty_rest_of_line ();
17829 return;
17830 }
252b5132 17831
ecb4347a
DJ
17832 if (get_absolute_expression_and_terminator (&mask) != ',')
17833 {
1661c76c 17834 as_warn (_("bad .mask/.fmask directive"));
ecb4347a
DJ
17835 --input_line_pointer;
17836 demand_empty_rest_of_line ();
17837 return;
17838 }
252b5132 17839
ecb4347a
DJ
17840 off = get_absolute_expression ();
17841
17842 if (reg_type == 'F')
17843 {
17844 cur_proc_ptr->fpreg_mask = mask;
17845 cur_proc_ptr->fpreg_offset = off;
17846 }
17847 else
17848 {
17849 cur_proc_ptr->reg_mask = mask;
17850 cur_proc_ptr->reg_offset = off;
17851 }
17852
17853 demand_empty_rest_of_line ();
252b5132 17854 }
252b5132
RH
17855}
17856
316f5878
RS
17857/* A table describing all the processors gas knows about. Names are
17858 matched in the order listed.
e7af610e 17859
316f5878
RS
17860 To ease comparison, please keep this table in the same order as
17861 gcc's mips_cpu_info_table[]. */
e972090a
NC
17862static const struct mips_cpu_info mips_cpu_info_table[] =
17863{
316f5878 17864 /* Entries for generic ISAs */
d16afab6
RS
17865 { "mips1", MIPS_CPU_IS_ISA, 0, ISA_MIPS1, CPU_R3000 },
17866 { "mips2", MIPS_CPU_IS_ISA, 0, ISA_MIPS2, CPU_R6000 },
17867 { "mips3", MIPS_CPU_IS_ISA, 0, ISA_MIPS3, CPU_R4000 },
17868 { "mips4", MIPS_CPU_IS_ISA, 0, ISA_MIPS4, CPU_R8000 },
17869 { "mips5", MIPS_CPU_IS_ISA, 0, ISA_MIPS5, CPU_MIPS5 },
17870 { "mips32", MIPS_CPU_IS_ISA, 0, ISA_MIPS32, CPU_MIPS32 },
17871 { "mips32r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
ae52f483
AB
17872 { "mips32r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R3, CPU_MIPS32R3 },
17873 { "mips32r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R5, CPU_MIPS32R5 },
d16afab6
RS
17874 { "mips64", MIPS_CPU_IS_ISA, 0, ISA_MIPS64, CPU_MIPS64 },
17875 { "mips64r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R2, CPU_MIPS64R2 },
ae52f483
AB
17876 { "mips64r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R3, CPU_MIPS64R3 },
17877 { "mips64r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R5, CPU_MIPS64R5 },
316f5878
RS
17878
17879 /* MIPS I */
d16afab6
RS
17880 { "r3000", 0, 0, ISA_MIPS1, CPU_R3000 },
17881 { "r2000", 0, 0, ISA_MIPS1, CPU_R3000 },
17882 { "r3900", 0, 0, ISA_MIPS1, CPU_R3900 },
316f5878
RS
17883
17884 /* MIPS II */
d16afab6 17885 { "r6000", 0, 0, ISA_MIPS2, CPU_R6000 },
316f5878
RS
17886
17887 /* MIPS III */
d16afab6
RS
17888 { "r4000", 0, 0, ISA_MIPS3, CPU_R4000 },
17889 { "r4010", 0, 0, ISA_MIPS2, CPU_R4010 },
17890 { "vr4100", 0, 0, ISA_MIPS3, CPU_VR4100 },
17891 { "vr4111", 0, 0, ISA_MIPS3, CPU_R4111 },
17892 { "vr4120", 0, 0, ISA_MIPS3, CPU_VR4120 },
17893 { "vr4130", 0, 0, ISA_MIPS3, CPU_VR4120 },
17894 { "vr4181", 0, 0, ISA_MIPS3, CPU_R4111 },
17895 { "vr4300", 0, 0, ISA_MIPS3, CPU_R4300 },
17896 { "r4400", 0, 0, ISA_MIPS3, CPU_R4400 },
17897 { "r4600", 0, 0, ISA_MIPS3, CPU_R4600 },
17898 { "orion", 0, 0, ISA_MIPS3, CPU_R4600 },
17899 { "r4650", 0, 0, ISA_MIPS3, CPU_R4650 },
17900 { "r5900", 0, 0, ISA_MIPS3, CPU_R5900 },
b15591bb 17901 /* ST Microelectronics Loongson 2E and 2F cores */
d16afab6
RS
17902 { "loongson2e", 0, 0, ISA_MIPS3, CPU_LOONGSON_2E },
17903 { "loongson2f", 0, 0, ISA_MIPS3, CPU_LOONGSON_2F },
316f5878
RS
17904
17905 /* MIPS IV */
d16afab6
RS
17906 { "r8000", 0, 0, ISA_MIPS4, CPU_R8000 },
17907 { "r10000", 0, 0, ISA_MIPS4, CPU_R10000 },
17908 { "r12000", 0, 0, ISA_MIPS4, CPU_R12000 },
17909 { "r14000", 0, 0, ISA_MIPS4, CPU_R14000 },
17910 { "r16000", 0, 0, ISA_MIPS4, CPU_R16000 },
17911 { "vr5000", 0, 0, ISA_MIPS4, CPU_R5000 },
17912 { "vr5400", 0, 0, ISA_MIPS4, CPU_VR5400 },
17913 { "vr5500", 0, 0, ISA_MIPS4, CPU_VR5500 },
17914 { "rm5200", 0, 0, ISA_MIPS4, CPU_R5000 },
17915 { "rm5230", 0, 0, ISA_MIPS4, CPU_R5000 },
17916 { "rm5231", 0, 0, ISA_MIPS4, CPU_R5000 },
17917 { "rm5261", 0, 0, ISA_MIPS4, CPU_R5000 },
17918 { "rm5721", 0, 0, ISA_MIPS4, CPU_R5000 },
17919 { "rm7000", 0, 0, ISA_MIPS4, CPU_RM7000 },
17920 { "rm9000", 0, 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
17921
17922 /* MIPS 32 */
d16afab6
RS
17923 { "4kc", 0, 0, ISA_MIPS32, CPU_MIPS32 },
17924 { "4km", 0, 0, ISA_MIPS32, CPU_MIPS32 },
17925 { "4kp", 0, 0, ISA_MIPS32, CPU_MIPS32 },
17926 { "4ksc", 0, ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
ad3fea08
TS
17927
17928 /* MIPS 32 Release 2 */
d16afab6
RS
17929 { "4kec", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17930 { "4kem", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17931 { "4kep", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17932 { "4ksd", 0, ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
17933 { "m4k", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17934 { "m4kp", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17935 { "m14k", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
17936 { "m14kc", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
17937 { "m14ke", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
17938 ISA_MIPS32R2, CPU_MIPS32R2 },
17939 { "m14kec", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
17940 ISA_MIPS32R2, CPU_MIPS32R2 },
17941 { "24kc", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17942 { "24kf2_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17943 { "24kf", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17944 { "24kf1_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 17945 /* Deprecated forms of the above. */
d16afab6
RS
17946 { "24kfx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17947 { "24kx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 17948 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
d16afab6
RS
17949 { "24kec", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
17950 { "24kef2_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
17951 { "24kef", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
17952 { "24kef1_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 17953 /* Deprecated forms of the above. */
d16afab6
RS
17954 { "24kefx", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
17955 { "24kex", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 17956 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
d16afab6
RS
17957 { "34kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17958 { "34kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17959 { "34kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17960 { "34kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 17961 /* Deprecated forms of the above. */
d16afab6
RS
17962 { "34kfx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17963 { "34kx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
711eefe4 17964 /* 34Kn is a 34kc without DSP. */
d16afab6 17965 { "34kn", 0, ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 17966 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
d16afab6
RS
17967 { "74kc", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
17968 { "74kf2_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
17969 { "74kf", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
17970 { "74kf1_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
17971 { "74kf3_2", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 17972 /* Deprecated forms of the above. */
d16afab6
RS
17973 { "74kfx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
17974 { "74kx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
30f8113a 17975 /* 1004K cores are multiprocessor versions of the 34K. */
d16afab6
RS
17976 { "1004kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17977 { "1004kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17978 { "1004kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17979 { "1004kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
bbaa46c0 17980 /* P5600 with EVA and Virtualization ASEs, other ASEs are optional. */
ae52f483 17981 { "p5600", 0, ASE_VIRT | ASE_EVA | ASE_XPA, ISA_MIPS32R5, CPU_MIPS32R5 },
32b26a03 17982
316f5878 17983 /* MIPS 64 */
d16afab6
RS
17984 { "5kc", 0, 0, ISA_MIPS64, CPU_MIPS64 },
17985 { "5kf", 0, 0, ISA_MIPS64, CPU_MIPS64 },
17986 { "20kc", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
17987 { "25kf", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
ad3fea08 17988
c7a23324 17989 /* Broadcom SB-1 CPU core */
d16afab6 17990 { "sb1", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
1e85aad8 17991 /* Broadcom SB-1A CPU core */
d16afab6 17992 { "sb1a", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
d051516a 17993
4ba154f5 17994 { "loongson3a", 0, 0, ISA_MIPS64R2, CPU_LOONGSON_3A },
e7af610e 17995
ed163775
MR
17996 /* MIPS 64 Release 2 */
17997
967344c6 17998 /* Cavium Networks Octeon CPU core */
d16afab6
RS
17999 { "octeon", 0, 0, ISA_MIPS64R2, CPU_OCTEON },
18000 { "octeon+", 0, 0, ISA_MIPS64R2, CPU_OCTEONP },
18001 { "octeon2", 0, 0, ISA_MIPS64R2, CPU_OCTEON2 },
967344c6 18002
52b6b6b9 18003 /* RMI Xlr */
d16afab6 18004 { "xlr", 0, 0, ISA_MIPS64, CPU_XLR },
52b6b6b9 18005
55a36193
MK
18006 /* Broadcom XLP.
18007 XLP is mostly like XLR, with the prominent exception that it is
18008 MIPS64R2 rather than MIPS64. */
d16afab6 18009 { "xlp", 0, 0, ISA_MIPS64R2, CPU_XLR },
55a36193 18010
316f5878 18011 /* End marker */
d16afab6 18012 { NULL, 0, 0, 0, 0 }
316f5878 18013};
e7af610e 18014
84ea6cf2 18015
316f5878
RS
18016/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
18017 with a final "000" replaced by "k". Ignore case.
e7af610e 18018
316f5878 18019 Note: this function is shared between GCC and GAS. */
c6c98b38 18020
b34976b6 18021static bfd_boolean
17a2f251 18022mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
18023{
18024 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
18025 given++, canonical++;
18026
18027 return ((*given == 0 && *canonical == 0)
18028 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
18029}
18030
18031
18032/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
18033 CPU name. We've traditionally allowed a lot of variation here.
18034
18035 Note: this function is shared between GCC and GAS. */
18036
b34976b6 18037static bfd_boolean
17a2f251 18038mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
18039{
18040 /* First see if the name matches exactly, or with a final "000"
18041 turned into "k". */
18042 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 18043 return TRUE;
316f5878
RS
18044
18045 /* If not, try comparing based on numerical designation alone.
18046 See if GIVEN is an unadorned number, or 'r' followed by a number. */
18047 if (TOLOWER (*given) == 'r')
18048 given++;
18049 if (!ISDIGIT (*given))
b34976b6 18050 return FALSE;
316f5878
RS
18051
18052 /* Skip over some well-known prefixes in the canonical name,
18053 hoping to find a number there too. */
18054 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
18055 canonical += 2;
18056 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
18057 canonical += 2;
18058 else if (TOLOWER (canonical[0]) == 'r')
18059 canonical += 1;
18060
18061 return mips_strict_matching_cpu_name_p (canonical, given);
18062}
18063
18064
18065/* Parse an option that takes the name of a processor as its argument.
18066 OPTION is the name of the option and CPU_STRING is the argument.
18067 Return the corresponding processor enumeration if the CPU_STRING is
18068 recognized, otherwise report an error and return null.
18069
18070 A similar function exists in GCC. */
e7af610e
NC
18071
18072static const struct mips_cpu_info *
17a2f251 18073mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 18074{
316f5878 18075 const struct mips_cpu_info *p;
e7af610e 18076
316f5878
RS
18077 /* 'from-abi' selects the most compatible architecture for the given
18078 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
18079 EABIs, we have to decide whether we're using the 32-bit or 64-bit
18080 version. Look first at the -mgp options, if given, otherwise base
18081 the choice on MIPS_DEFAULT_64BIT.
e7af610e 18082
316f5878
RS
18083 Treat NO_ABI like the EABIs. One reason to do this is that the
18084 plain 'mips' and 'mips64' configs have 'from-abi' as their default
18085 architecture. This code picks MIPS I for 'mips' and MIPS III for
18086 'mips64', just as we did in the days before 'from-abi'. */
18087 if (strcasecmp (cpu_string, "from-abi") == 0)
18088 {
18089 if (ABI_NEEDS_32BIT_REGS (mips_abi))
18090 return mips_cpu_info_from_isa (ISA_MIPS1);
18091
18092 if (ABI_NEEDS_64BIT_REGS (mips_abi))
18093 return mips_cpu_info_from_isa (ISA_MIPS3);
18094
bad1aba3 18095 if (file_mips_opts.gp >= 0)
18096 return mips_cpu_info_from_isa (file_mips_opts.gp == 32
0b35dfee 18097 ? ISA_MIPS1 : ISA_MIPS3);
316f5878
RS
18098
18099 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
18100 ? ISA_MIPS3
18101 : ISA_MIPS1);
18102 }
18103
18104 /* 'default' has traditionally been a no-op. Probably not very useful. */
18105 if (strcasecmp (cpu_string, "default") == 0)
18106 return 0;
18107
18108 for (p = mips_cpu_info_table; p->name != 0; p++)
18109 if (mips_matching_cpu_name_p (p->name, cpu_string))
18110 return p;
18111
1661c76c 18112 as_bad (_("bad value (%s) for %s"), cpu_string, option);
316f5878 18113 return 0;
e7af610e
NC
18114}
18115
316f5878
RS
18116/* Return the canonical processor information for ISA (a member of the
18117 ISA_MIPS* enumeration). */
18118
e7af610e 18119static const struct mips_cpu_info *
17a2f251 18120mips_cpu_info_from_isa (int isa)
e7af610e
NC
18121{
18122 int i;
18123
18124 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
ad3fea08 18125 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
316f5878 18126 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
18127 return (&mips_cpu_info_table[i]);
18128
e972090a 18129 return NULL;
e7af610e 18130}
fef14a42
TS
18131
18132static const struct mips_cpu_info *
17a2f251 18133mips_cpu_info_from_arch (int arch)
fef14a42
TS
18134{
18135 int i;
18136
18137 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
18138 if (arch == mips_cpu_info_table[i].cpu)
18139 return (&mips_cpu_info_table[i]);
18140
18141 return NULL;
18142}
316f5878
RS
18143\f
18144static void
17a2f251 18145show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
18146{
18147 if (*first_p)
18148 {
18149 fprintf (stream, "%24s", "");
18150 *col_p = 24;
18151 }
18152 else
18153 {
18154 fprintf (stream, ", ");
18155 *col_p += 2;
18156 }
e7af610e 18157
316f5878
RS
18158 if (*col_p + strlen (string) > 72)
18159 {
18160 fprintf (stream, "\n%24s", "");
18161 *col_p = 24;
18162 }
18163
18164 fprintf (stream, "%s", string);
18165 *col_p += strlen (string);
18166
18167 *first_p = 0;
18168}
18169
18170void
17a2f251 18171md_show_usage (FILE *stream)
e7af610e 18172{
316f5878
RS
18173 int column, first;
18174 size_t i;
18175
18176 fprintf (stream, _("\
18177MIPS options:\n\
316f5878
RS
18178-EB generate big endian output\n\
18179-EL generate little endian output\n\
18180-g, -g2 do not remove unneeded NOPs or swap branches\n\
18181-G NUM allow referencing objects up to NUM bytes\n\
18182 implicitly with the gp register [default 8]\n"));
18183 fprintf (stream, _("\
18184-mips1 generate MIPS ISA I instructions\n\
18185-mips2 generate MIPS ISA II instructions\n\
18186-mips3 generate MIPS ISA III instructions\n\
18187-mips4 generate MIPS ISA IV instructions\n\
18188-mips5 generate MIPS ISA V instructions\n\
18189-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 18190-mips32r2 generate MIPS32 release 2 ISA instructions\n\
ae52f483
AB
18191-mips32r3 generate MIPS32 release 3 ISA instructions\n\
18192-mips32r5 generate MIPS32 release 5 ISA instructions\n\
316f5878 18193-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 18194-mips64r2 generate MIPS64 release 2 ISA instructions\n\
ae52f483
AB
18195-mips64r3 generate MIPS64 release 3 ISA instructions\n\
18196-mips64r5 generate MIPS64 release 5 ISA instructions\n\
316f5878
RS
18197-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
18198
18199 first = 1;
e7af610e
NC
18200
18201 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
18202 show (stream, mips_cpu_info_table[i].name, &column, &first);
18203 show (stream, "from-abi", &column, &first);
18204 fputc ('\n', stream);
e7af610e 18205
316f5878
RS
18206 fprintf (stream, _("\
18207-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
18208-no-mCPU don't generate code specific to CPU.\n\
18209 For -mCPU and -no-mCPU, CPU must be one of:\n"));
18210
18211 first = 1;
18212
18213 show (stream, "3900", &column, &first);
18214 show (stream, "4010", &column, &first);
18215 show (stream, "4100", &column, &first);
18216 show (stream, "4650", &column, &first);
18217 fputc ('\n', stream);
18218
18219 fprintf (stream, _("\
18220-mips16 generate mips16 instructions\n\
18221-no-mips16 do not generate mips16 instructions\n"));
18222 fprintf (stream, _("\
df58fc94
RS
18223-mmicromips generate microMIPS instructions\n\
18224-mno-micromips do not generate microMIPS instructions\n"));
18225 fprintf (stream, _("\
e16bfa71
TS
18226-msmartmips generate smartmips instructions\n\
18227-mno-smartmips do not generate smartmips instructions\n"));
18228 fprintf (stream, _("\
74cd071d
CF
18229-mdsp generate DSP instructions\n\
18230-mno-dsp do not generate DSP instructions\n"));
18231 fprintf (stream, _("\
8b082fb1
TS
18232-mdspr2 generate DSP R2 instructions\n\
18233-mno-dspr2 do not generate DSP R2 instructions\n"));
18234 fprintf (stream, _("\
ef2e4d86
CF
18235-mmt generate MT instructions\n\
18236-mno-mt do not generate MT instructions\n"));
18237 fprintf (stream, _("\
dec0624d
MR
18238-mmcu generate MCU instructions\n\
18239-mno-mcu do not generate MCU instructions\n"));
18240 fprintf (stream, _("\
56d438b1
CF
18241-mmsa generate MSA instructions\n\
18242-mno-msa do not generate MSA instructions\n"));
18243 fprintf (stream, _("\
7d64c587
AB
18244-mxpa generate eXtended Physical Address (XPA) instructions\n\
18245-mno-xpa do not generate eXtended Physical Address (XPA) instructions\n"));
18246 fprintf (stream, _("\
b015e599
AP
18247-mvirt generate Virtualization instructions\n\
18248-mno-virt do not generate Virtualization instructions\n"));
18249 fprintf (stream, _("\
833794fc
MR
18250-minsn32 only generate 32-bit microMIPS instructions\n\
18251-mno-insn32 generate all microMIPS instructions\n"));
18252 fprintf (stream, _("\
c67a084a
NC
18253-mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
18254-mfix-loongson2f-nop work around Loongson2F NOP errata\n\
d766e8ec 18255-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 18256-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
6a32d874 18257-mfix-24k insert a nop after ERET and DERET instructions\n\
d954098f 18258-mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
316f5878
RS
18259-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
18260-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261 18261-msym32 assume all symbols have 32-bit values\n\
316f5878
RS
18262-O0 remove unneeded NOPs, do not swap branches\n\
18263-O remove unneeded NOPs and swap branches\n\
316f5878
RS
18264--trap, --no-break trap exception on div by 0 and mult overflow\n\
18265--break, --no-trap break exception on div by 0 and mult overflow\n"));
037b32b9
AN
18266 fprintf (stream, _("\
18267-mhard-float allow floating-point instructions\n\
18268-msoft-float do not allow floating-point instructions\n\
18269-msingle-float only allow 32-bit floating-point operations\n\
18270-mdouble-float allow 32-bit and 64-bit floating-point operations\n\
3bf0dbfb 18271--[no-]construct-floats [dis]allow floating point values to be constructed\n\
ba92f887
MR
18272--[no-]relax-branch [dis]allow out-of-range branches to be relaxed\n\
18273-mnan=ENCODING select an IEEE 754 NaN encoding convention, either of:\n"));
18274
18275 first = 1;
18276
18277 show (stream, "legacy", &column, &first);
18278 show (stream, "2008", &column, &first);
18279
18280 fputc ('\n', stream);
18281
316f5878
RS
18282 fprintf (stream, _("\
18283-KPIC, -call_shared generate SVR4 position independent code\n\
861fb55a 18284-call_nonpic generate non-PIC code that can operate with DSOs\n\
0c000745 18285-mvxworks-pic generate VxWorks position independent code\n\
861fb55a 18286-non_shared do not generate code that can operate with DSOs\n\
316f5878 18287-xgot assume a 32 bit GOT\n\
dcd410fe 18288-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8 18289-mshared, -mno-shared disable/enable .cpload optimization for\n\
d821e36b 18290 position dependent (non shared) code\n\
316f5878
RS
18291-mabi=ABI create ABI conformant object file for:\n"));
18292
18293 first = 1;
18294
18295 show (stream, "32", &column, &first);
18296 show (stream, "o64", &column, &first);
18297 show (stream, "n32", &column, &first);
18298 show (stream, "64", &column, &first);
18299 show (stream, "eabi", &column, &first);
18300
18301 fputc ('\n', stream);
18302
18303 fprintf (stream, _("\
18304-32 create o32 ABI object file (default)\n\
18305-n32 create n32 ABI object file\n\
18306-64 create 64 ABI object file\n"));
e7af610e 18307}
14e777e0 18308
1575952e 18309#ifdef TE_IRIX
14e777e0 18310enum dwarf2_format
413a266c 18311mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
14e777e0 18312{
369943fe 18313 if (HAVE_64BIT_SYMBOLS)
1575952e 18314 return dwarf2_format_64bit_irix;
14e777e0
KB
18315 else
18316 return dwarf2_format_32bit;
18317}
1575952e 18318#endif
73369e65
EC
18319
18320int
18321mips_dwarf2_addr_size (void)
18322{
6b6b3450 18323 if (HAVE_64BIT_OBJECTS)
73369e65 18324 return 8;
73369e65
EC
18325 else
18326 return 4;
18327}
5862107c
EC
18328
18329/* Standard calling conventions leave the CFA at SP on entry. */
18330void
18331mips_cfi_frame_initial_instructions (void)
18332{
18333 cfi_add_CFA_def_cfa_register (SP);
18334}
18335
707bfff6
TS
18336int
18337tc_mips_regname_to_dw2regnum (char *regname)
18338{
18339 unsigned int regnum = -1;
18340 unsigned int reg;
18341
18342 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
18343 regnum = reg;
18344
18345 return regnum;
18346}
263b2574 18347
18348/* Implement CONVERT_SYMBOLIC_ATTRIBUTE.
18349 Given a symbolic attribute NAME, return the proper integer value.
18350 Returns -1 if the attribute is not known. */
18351
18352int
18353mips_convert_symbolic_attribute (const char *name)
18354{
18355 static const struct
18356 {
18357 const char * name;
18358 const int tag;
18359 }
18360 attribute_table[] =
18361 {
18362#define T(tag) {#tag, tag}
18363 T (Tag_GNU_MIPS_ABI_FP),
18364 T (Tag_GNU_MIPS_ABI_MSA),
18365#undef T
18366 };
18367 unsigned int i;
18368
18369 if (name == NULL)
18370 return -1;
18371
18372 for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
18373 if (streq (name, attribute_table[i].name))
18374 return attribute_table[i].tag;
18375
18376 return -1;
18377}
This page took 2.689932 seconds and 4 git commands to generate.