include/opcode/
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
CommitLineData
252b5132 1/* tc-mips.c -- assemble code for a MIPS chip.
81912461 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
e407c74b 3 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
c67a084a 4 Free Software Foundation, Inc.
252b5132
RH
5 Contributed by the OSF and Ralph Campbell.
6 Written by Keith Knowles and Ralph Campbell, working independently.
7 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
8 Support.
9
10 This file is part of GAS.
11
12 GAS is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
ec2655a6 14 the Free Software Foundation; either version 3, or (at your option)
252b5132
RH
15 any later version.
16
17 GAS is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with GAS; see the file COPYING. If not, write to the Free
4b4da160
NC
24 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
25 02110-1301, USA. */
252b5132
RH
26
27#include "as.h"
28#include "config.h"
29#include "subsegs.h"
3882b010 30#include "safe-ctype.h"
252b5132 31
252b5132
RH
32#include "opcode/mips.h"
33#include "itbl-ops.h"
c5dd6aab 34#include "dwarf2dbg.h"
5862107c 35#include "dw2gencfi.h"
252b5132 36
42429eac
RS
37/* Check assumptions made in this file. */
38typedef char static_assert1[sizeof (offsetT) < 8 ? -1 : 1];
39typedef char static_assert2[sizeof (valueT) < 8 ? -1 : 1];
40
252b5132
RH
41#ifdef DEBUG
42#define DBG(x) printf x
43#else
44#define DBG(x)
45#endif
46
9e12b7a2
RS
47#define SKIP_SPACE_TABS(S) \
48 do { while (*(S) == ' ' || *(S) == '\t') ++(S); } while (0)
49
252b5132 50/* Clean up namespace so we can include obj-elf.h too. */
17a2f251
TS
51static int mips_output_flavor (void);
52static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
252b5132
RH
53#undef OBJ_PROCESS_STAB
54#undef OUTPUT_FLAVOR
55#undef S_GET_ALIGN
56#undef S_GET_SIZE
57#undef S_SET_ALIGN
58#undef S_SET_SIZE
252b5132
RH
59#undef obj_frob_file
60#undef obj_frob_file_after_relocs
61#undef obj_frob_symbol
62#undef obj_pop_insert
63#undef obj_sec_sym_ok_for_reloc
64#undef OBJ_COPY_SYMBOL_ATTRIBUTES
65
66#include "obj-elf.h"
67/* Fix any of them that we actually care about. */
68#undef OUTPUT_FLAVOR
69#define OUTPUT_FLAVOR mips_output_flavor()
252b5132 70
252b5132 71#include "elf/mips.h"
252b5132
RH
72
73#ifndef ECOFF_DEBUGGING
74#define NO_ECOFF_DEBUGGING
75#define ECOFF_DEBUGGING 0
76#endif
77
ecb4347a
DJ
78int mips_flag_mdebug = -1;
79
dcd410fe
RO
80/* Control generation of .pdr sections. Off by default on IRIX: the native
81 linker doesn't know about and discards them, but relocations against them
82 remain, leading to rld crashes. */
83#ifdef TE_IRIX
84int mips_flag_pdr = FALSE;
85#else
86int mips_flag_pdr = TRUE;
87#endif
88
252b5132
RH
89#include "ecoff.h"
90
252b5132 91static char *mips_regmask_frag;
252b5132 92
85b51719 93#define ZERO 0
741fe287 94#define ATREG 1
df58fc94
RS
95#define S0 16
96#define S7 23
252b5132
RH
97#define TREG 24
98#define PIC_CALL_REG 25
99#define KT0 26
100#define KT1 27
101#define GP 28
102#define SP 29
103#define FP 30
104#define RA 31
105
106#define ILLEGAL_REG (32)
107
741fe287
MR
108#define AT mips_opts.at
109
252b5132
RH
110extern int target_big_endian;
111
252b5132 112/* The name of the readonly data section. */
e8044f35 113#define RDATA_SECTION_NAME ".rodata"
252b5132 114
a4e06468
RS
115/* Ways in which an instruction can be "appended" to the output. */
116enum append_method {
117 /* Just add it normally. */
118 APPEND_ADD,
119
120 /* Add it normally and then add a nop. */
121 APPEND_ADD_WITH_NOP,
122
123 /* Turn an instruction with a delay slot into a "compact" version. */
124 APPEND_ADD_COMPACT,
125
126 /* Insert the instruction before the last one. */
127 APPEND_SWAP
128};
129
47e39b9d
RS
130/* Information about an instruction, including its format, operands
131 and fixups. */
132struct mips_cl_insn
133{
134 /* The opcode's entry in mips_opcodes or mips16_opcodes. */
135 const struct mips_opcode *insn_mo;
136
47e39b9d 137 /* The 16-bit or 32-bit bitstring of the instruction itself. This is
5c04167a
RS
138 a copy of INSN_MO->match with the operands filled in. If we have
139 decided to use an extended MIPS16 instruction, this includes the
140 extension. */
47e39b9d
RS
141 unsigned long insn_opcode;
142
143 /* The frag that contains the instruction. */
144 struct frag *frag;
145
146 /* The offset into FRAG of the first instruction byte. */
147 long where;
148
149 /* The relocs associated with the instruction, if any. */
150 fixS *fixp[3];
151
a38419a5
RS
152 /* True if this entry cannot be moved from its current position. */
153 unsigned int fixed_p : 1;
47e39b9d 154
708587a4 155 /* True if this instruction occurred in a .set noreorder block. */
47e39b9d
RS
156 unsigned int noreorder_p : 1;
157
2fa15973
RS
158 /* True for mips16 instructions that jump to an absolute address. */
159 unsigned int mips16_absolute_jump_p : 1;
15be625d
CM
160
161 /* True if this instruction is complete. */
162 unsigned int complete_p : 1;
e407c74b
NC
163
164 /* True if this instruction is cleared from history by unconditional
165 branch. */
166 unsigned int cleared_p : 1;
47e39b9d
RS
167};
168
a325df1d
TS
169/* The ABI to use. */
170enum mips_abi_level
171{
172 NO_ABI = 0,
173 O32_ABI,
174 O64_ABI,
175 N32_ABI,
176 N64_ABI,
177 EABI_ABI
178};
179
180/* MIPS ABI we are using for this output file. */
316f5878 181static enum mips_abi_level mips_abi = NO_ABI;
a325df1d 182
143d77c5
EC
183/* Whether or not we have code that can call pic code. */
184int mips_abicalls = FALSE;
185
aa6975fb
ILT
186/* Whether or not we have code which can be put into a shared
187 library. */
188static bfd_boolean mips_in_shared = TRUE;
189
252b5132
RH
190/* This is the set of options which may be modified by the .set
191 pseudo-op. We use a struct so that .set push and .set pop are more
192 reliable. */
193
e972090a
NC
194struct mips_set_options
195{
252b5132
RH
196 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
197 if it has not been initialized. Changed by `.set mipsN', and the
198 -mipsN command line option, and the default CPU. */
199 int isa;
846ef2d0
RS
200 /* Enabled Application Specific Extensions (ASEs). Changed by `.set
201 <asename>', by command line options, and based on the default
202 architecture. */
203 int ase;
252b5132
RH
204 /* Whether we are assembling for the mips16 processor. 0 if we are
205 not, 1 if we are, and -1 if the value has not been initialized.
206 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
207 -nomips16 command line options, and the default CPU. */
208 int mips16;
df58fc94
RS
209 /* Whether we are assembling for the mipsMIPS ASE. 0 if we are not,
210 1 if we are, and -1 if the value has not been initialized. Changed
211 by `.set micromips' and `.set nomicromips', and the -mmicromips
212 and -mno-micromips command line options, and the default CPU. */
213 int micromips;
252b5132
RH
214 /* Non-zero if we should not reorder instructions. Changed by `.set
215 reorder' and `.set noreorder'. */
216 int noreorder;
741fe287
MR
217 /* Non-zero if we should not permit the register designated "assembler
218 temporary" to be used in instructions. The value is the register
219 number, normally $at ($1). Changed by `.set at=REG', `.set noat'
220 (same as `.set at=$0') and `.set at' (same as `.set at=$1'). */
221 unsigned int at;
252b5132
RH
222 /* Non-zero if we should warn when a macro instruction expands into
223 more than one machine instruction. Changed by `.set nomacro' and
224 `.set macro'. */
225 int warn_about_macros;
226 /* Non-zero if we should not move instructions. Changed by `.set
227 move', `.set volatile', `.set nomove', and `.set novolatile'. */
228 int nomove;
229 /* Non-zero if we should not optimize branches by moving the target
230 of the branch into the delay slot. Actually, we don't perform
231 this optimization anyhow. Changed by `.set bopt' and `.set
232 nobopt'. */
233 int nobopt;
234 /* Non-zero if we should not autoextend mips16 instructions.
235 Changed by `.set autoextend' and `.set noautoextend'. */
236 int noautoextend;
833794fc
MR
237 /* True if we should only emit 32-bit microMIPS instructions.
238 Changed by `.set insn32' and `.set noinsn32', and the -minsn32
239 and -mno-insn32 command line options. */
240 bfd_boolean insn32;
a325df1d
TS
241 /* Restrict general purpose registers and floating point registers
242 to 32 bit. This is initially determined when -mgp32 or -mfp32
243 is passed but can changed if the assembler code uses .set mipsN. */
244 int gp32;
245 int fp32;
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
037b32b9
AN
262/* This is the struct we use to hold the current set of options. Note
263 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
264 -1 to indicate that they have not been initialized. */
265
a325df1d 266/* True if -mgp32 was passed. */
a8e8e863 267static int file_mips_gp32 = -1;
a325df1d
TS
268
269/* True if -mfp32 was passed. */
a8e8e863 270static int file_mips_fp32 = -1;
a325df1d 271
037b32b9
AN
272/* 1 if -msoft-float, 0 if -mhard-float. The default is 0. */
273static int file_mips_soft_float = 0;
274
275/* 1 if -msingle-float, 0 if -mdouble-float. The default is 0. */
276static int file_mips_single_float = 0;
252b5132 277
ba92f887
MR
278/* True if -mnan=2008, false if -mnan=legacy. */
279static bfd_boolean mips_flag_nan2008 = FALSE;
280
e972090a
NC
281static struct mips_set_options mips_opts =
282{
846ef2d0 283 /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
b015e599 284 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
833794fc
MR
285 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
286 /* gp32 */ 0, /* fp32 */ 0, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
b015e599 287 /* soft_float */ FALSE, /* single_float */ FALSE
e7af610e 288};
252b5132 289
846ef2d0
RS
290/* The set of ASEs that were selected on the command line, either
291 explicitly via ASE options or implicitly through things like -march. */
292static unsigned int file_ase;
293
294/* Which bits of file_ase were explicitly set or cleared by ASE options. */
295static unsigned int file_ase_explicit;
296
252b5132
RH
297/* These variables are filled in with the masks of registers used.
298 The object format code reads them and puts them in the appropriate
299 place. */
300unsigned long mips_gprmask;
301unsigned long mips_cprmask[4];
302
303/* MIPS ISA we are using for this output file. */
e7af610e 304static int file_mips_isa = ISA_UNKNOWN;
252b5132 305
738f4d98 306/* True if any MIPS16 code was produced. */
a4672219
TS
307static int file_ase_mips16;
308
3994f87e
TS
309#define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32 \
310 || mips_opts.isa == ISA_MIPS32R2 \
311 || mips_opts.isa == ISA_MIPS64 \
312 || mips_opts.isa == ISA_MIPS64R2)
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. */
fef14a42 330static int file_mips_arch = CPU_UNKNOWN;
316f5878 331static const char *mips_arch_string;
ec68c924
EC
332
333/* The argument of the -mtune= flag. The architecture for which we
334 are optimizing. */
335static int mips_tune = CPU_UNKNOWN;
316f5878 336static const char *mips_tune_string;
ec68c924 337
316f5878 338/* True when generating 32-bit code for a 64-bit processor. */
252b5132
RH
339static int mips_32bitmode = 0;
340
316f5878
RS
341/* True if the given ABI requires 32-bit registers. */
342#define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
343
344/* Likewise 64-bit registers. */
707bfff6
TS
345#define ABI_NEEDS_64BIT_REGS(ABI) \
346 ((ABI) == N32_ABI \
347 || (ABI) == N64_ABI \
316f5878
RS
348 || (ABI) == O64_ABI)
349
ad3fea08 350/* Return true if ISA supports 64 bit wide gp registers. */
707bfff6
TS
351#define ISA_HAS_64BIT_REGS(ISA) \
352 ((ISA) == ISA_MIPS3 \
353 || (ISA) == ISA_MIPS4 \
354 || (ISA) == ISA_MIPS5 \
355 || (ISA) == ISA_MIPS64 \
356 || (ISA) == ISA_MIPS64R2)
9ce8a5dd 357
ad3fea08
TS
358/* Return true if ISA supports 64 bit wide float registers. */
359#define ISA_HAS_64BIT_FPRS(ISA) \
360 ((ISA) == ISA_MIPS3 \
361 || (ISA) == ISA_MIPS4 \
362 || (ISA) == ISA_MIPS5 \
363 || (ISA) == ISA_MIPS32R2 \
364 || (ISA) == ISA_MIPS64 \
365 || (ISA) == ISA_MIPS64R2)
366
af7ee8bf
CD
367/* Return true if ISA supports 64-bit right rotate (dror et al.)
368 instructions. */
707bfff6 369#define ISA_HAS_DROR(ISA) \
df58fc94
RS
370 ((ISA) == ISA_MIPS64R2 \
371 || (mips_opts.micromips \
372 && ISA_HAS_64BIT_REGS (ISA)) \
373 )
af7ee8bf
CD
374
375/* Return true if ISA supports 32-bit right rotate (ror et al.)
376 instructions. */
707bfff6
TS
377#define ISA_HAS_ROR(ISA) \
378 ((ISA) == ISA_MIPS32R2 \
379 || (ISA) == ISA_MIPS64R2 \
846ef2d0 380 || (mips_opts.ase & ASE_SMARTMIPS) \
df58fc94
RS
381 || mips_opts.micromips \
382 )
707bfff6 383
7455baf8
TS
384/* Return true if ISA supports single-precision floats in odd registers. */
385#define ISA_HAS_ODD_SINGLE_FPR(ISA) \
386 ((ISA) == ISA_MIPS32 \
387 || (ISA) == ISA_MIPS32R2 \
388 || (ISA) == ISA_MIPS64 \
389 || (ISA) == ISA_MIPS64R2)
af7ee8bf 390
ad3fea08
TS
391/* Return true if ISA supports move to/from high part of a 64-bit
392 floating-point register. */
393#define ISA_HAS_MXHC1(ISA) \
394 ((ISA) == ISA_MIPS32R2 \
395 || (ISA) == ISA_MIPS64R2)
396
e013f690 397#define HAVE_32BIT_GPRS \
ad3fea08 398 (mips_opts.gp32 || !ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257 399
e013f690 400#define HAVE_32BIT_FPRS \
ad3fea08 401 (mips_opts.fp32 || !ISA_HAS_64BIT_FPRS (mips_opts.isa))
ca4e0257 402
ad3fea08
TS
403#define HAVE_64BIT_GPRS (!HAVE_32BIT_GPRS)
404#define HAVE_64BIT_FPRS (!HAVE_32BIT_FPRS)
ca4e0257 405
316f5878 406#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
e013f690 407
316f5878 408#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
e013f690 409
3b91255e
RS
410/* True if relocations are stored in-place. */
411#define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
412
aed1a261
RS
413/* The ABI-derived address size. */
414#define HAVE_64BIT_ADDRESSES \
415 (HAVE_64BIT_GPRS && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
416#define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
e013f690 417
aed1a261
RS
418/* The size of symbolic constants (i.e., expressions of the form
419 "SYMBOL" or "SYMBOL + OFFSET"). */
420#define HAVE_32BIT_SYMBOLS \
421 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
422#define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
ca4e0257 423
b7c7d6c1
TS
424/* Addresses are loaded in different ways, depending on the address size
425 in use. The n32 ABI Documentation also mandates the use of additions
426 with overflow checking, but existing implementations don't follow it. */
f899b4b8 427#define ADDRESS_ADD_INSN \
b7c7d6c1 428 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
f899b4b8
TS
429
430#define ADDRESS_ADDI_INSN \
b7c7d6c1 431 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
f899b4b8
TS
432
433#define ADDRESS_LOAD_INSN \
434 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
435
436#define ADDRESS_STORE_INSN \
437 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
438
a4672219 439/* Return true if the given CPU supports the MIPS16 ASE. */
3396de36
TS
440#define CPU_HAS_MIPS16(cpu) \
441 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
442 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
a4672219 443
2309ddf2 444/* Return true if the given CPU supports the microMIPS ASE. */
df58fc94
RS
445#define CPU_HAS_MICROMIPS(cpu) 0
446
60b63b72
RS
447/* True if CPU has a dror instruction. */
448#define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
449
450/* True if CPU has a ror instruction. */
451#define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
452
dd6a37e7 453/* True if CPU is in the Octeon family */
432233b3 454#define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP || (CPU) == CPU_OCTEON2)
dd6a37e7 455
dd3cbb7e 456/* True if CPU has seq/sne and seqi/snei instructions. */
dd6a37e7 457#define CPU_HAS_SEQ(CPU) (CPU_IS_OCTEON (CPU))
dd3cbb7e 458
0aa27725
RS
459/* True, if CPU has support for ldc1 and sdc1. */
460#define CPU_HAS_LDC1_SDC1(CPU) \
461 ((mips_opts.isa != ISA_MIPS1) && ((CPU) != CPU_R5900))
462
c8978940
CD
463/* True if mflo and mfhi can be immediately followed by instructions
464 which write to the HI and LO registers.
465
466 According to MIPS specifications, MIPS ISAs I, II, and III need
467 (at least) two instructions between the reads of HI/LO and
468 instructions which write them, and later ISAs do not. Contradicting
469 the MIPS specifications, some MIPS IV processor user manuals (e.g.
470 the UM for the NEC Vr5000) document needing the instructions between
471 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
472 MIPS64 and later ISAs to have the interlocks, plus any specific
473 earlier-ISA CPUs for which CPU documentation declares that the
474 instructions are really interlocked. */
475#define hilo_interlocks \
476 (mips_opts.isa == ISA_MIPS32 \
477 || mips_opts.isa == ISA_MIPS32R2 \
478 || mips_opts.isa == ISA_MIPS64 \
479 || mips_opts.isa == ISA_MIPS64R2 \
480 || mips_opts.arch == CPU_R4010 \
e407c74b 481 || mips_opts.arch == CPU_R5900 \
c8978940
CD
482 || mips_opts.arch == CPU_R10000 \
483 || mips_opts.arch == CPU_R12000 \
3aa3176b
TS
484 || mips_opts.arch == CPU_R14000 \
485 || mips_opts.arch == CPU_R16000 \
c8978940 486 || mips_opts.arch == CPU_RM7000 \
c8978940 487 || mips_opts.arch == CPU_VR5500 \
df58fc94 488 || mips_opts.micromips \
c8978940 489 )
252b5132
RH
490
491/* Whether the processor uses hardware interlocks to protect reads
81912461
ILT
492 from the GPRs after they are loaded from memory, and thus does not
493 require nops to be inserted. This applies to instructions marked
494 INSN_LOAD_MEMORY_DELAY. These nops are only required at MIPS ISA
df58fc94
RS
495 level I and microMIPS mode instructions are always interlocked. */
496#define gpr_interlocks \
497 (mips_opts.isa != ISA_MIPS1 \
498 || mips_opts.arch == CPU_R3900 \
e407c74b 499 || mips_opts.arch == CPU_R5900 \
df58fc94
RS
500 || mips_opts.micromips \
501 )
252b5132 502
81912461
ILT
503/* Whether the processor uses hardware interlocks to avoid delays
504 required by coprocessor instructions, and thus does not require
505 nops to be inserted. This applies to instructions marked
506 INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
507 between instructions marked INSN_WRITE_COND_CODE and ones marked
508 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
df58fc94
RS
509 levels I, II, and III and microMIPS mode instructions are always
510 interlocked. */
bdaaa2e1 511/* Itbl support may require additional care here. */
81912461
ILT
512#define cop_interlocks \
513 ((mips_opts.isa != ISA_MIPS1 \
514 && mips_opts.isa != ISA_MIPS2 \
515 && mips_opts.isa != ISA_MIPS3) \
516 || mips_opts.arch == CPU_R4300 \
df58fc94 517 || mips_opts.micromips \
81912461
ILT
518 )
519
520/* Whether the processor uses hardware interlocks to protect reads
521 from coprocessor registers after they are loaded from memory, and
522 thus does not require nops to be inserted. This applies to
523 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
df58fc94
RS
524 requires at MIPS ISA level I and microMIPS mode instructions are
525 always interlocked. */
526#define cop_mem_interlocks \
527 (mips_opts.isa != ISA_MIPS1 \
528 || mips_opts.micromips \
529 )
252b5132 530
6b76fefe
CM
531/* Is this a mfhi or mflo instruction? */
532#define MF_HILO_INSN(PINFO) \
b19e8a9b
AN
533 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
534
df58fc94
RS
535/* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
536 has been selected. This implies, in particular, that addresses of text
537 labels have their LSB set. */
538#define HAVE_CODE_COMPRESSION \
539 ((mips_opts.mips16 | mips_opts.micromips) != 0)
540
42429eac
RS
541/* The minimum and maximum signed values that can be stored in a GPR. */
542#define GPR_SMAX ((offsetT) (((valueT) 1 << (HAVE_64BIT_GPRS ? 63 : 31)) - 1))
543#define GPR_SMIN (-GPR_SMAX - 1)
544
252b5132
RH
545/* MIPS PIC level. */
546
a161fe53 547enum mips_pic_level mips_pic;
252b5132 548
c9914766 549/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 550 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 551static int mips_big_got = 0;
252b5132
RH
552
553/* 1 if trap instructions should used for overflow rather than break
554 instructions. */
c9914766 555static int mips_trap = 0;
252b5132 556
119d663a 557/* 1 if double width floating point constants should not be constructed
b6ff326e 558 by assembling two single width halves into two single width floating
119d663a
NC
559 point registers which just happen to alias the double width destination
560 register. On some architectures this aliasing can be disabled by a bit
d547a75e 561 in the status register, and the setting of this bit cannot be determined
119d663a
NC
562 automatically at assemble time. */
563static int mips_disable_float_construction;
564
252b5132
RH
565/* Non-zero if any .set noreorder directives were used. */
566
567static int mips_any_noreorder;
568
6b76fefe
CM
569/* Non-zero if nops should be inserted when the register referenced in
570 an mfhi/mflo instruction is read in the next two instructions. */
571static int mips_7000_hilo_fix;
572
02ffd3e4 573/* The size of objects in the small data section. */
156c2f8b 574static unsigned int g_switch_value = 8;
252b5132
RH
575/* Whether the -G option was used. */
576static int g_switch_seen = 0;
577
578#define N_RMASK 0xc4
579#define N_VFP 0xd4
580
581/* If we can determine in advance that GP optimization won't be
582 possible, we can skip the relaxation stuff that tries to produce
583 GP-relative references. This makes delay slot optimization work
584 better.
585
586 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
587 gcc output. It needs to guess right for gcc, otherwise gcc
588 will put what it thinks is a GP-relative instruction in a branch
589 delay slot.
252b5132
RH
590
591 I don't know if a fix is needed for the SVR4_PIC mode. I've only
592 fixed it for the non-PIC mode. KR 95/04/07 */
17a2f251 593static int nopic_need_relax (symbolS *, int);
252b5132
RH
594
595/* handle of the OPCODE hash table */
596static struct hash_control *op_hash = NULL;
597
598/* The opcode hash table we use for the mips16. */
599static struct hash_control *mips16_op_hash = NULL;
600
df58fc94
RS
601/* The opcode hash table we use for the microMIPS ASE. */
602static struct hash_control *micromips_op_hash = NULL;
603
252b5132
RH
604/* This array holds the chars that always start a comment. If the
605 pre-processor is disabled, these aren't very useful */
606const char comment_chars[] = "#";
607
608/* This array holds the chars that only start a comment at the beginning of
609 a line. If the line seems to have the form '# 123 filename'
610 .line and .file directives will appear in the pre-processed output */
611/* Note that input_file.c hand checks for '#' at the beginning of the
612 first line of the input file. This is because the compiler outputs
bdaaa2e1 613 #NO_APP at the beginning of its output. */
252b5132
RH
614/* Also note that C style comments are always supported. */
615const char line_comment_chars[] = "#";
616
bdaaa2e1 617/* This array holds machine specific line separator characters. */
63a0b638 618const char line_separator_chars[] = ";";
252b5132
RH
619
620/* Chars that can be used to separate mant from exp in floating point nums */
621const char EXP_CHARS[] = "eE";
622
623/* Chars that mean this number is a floating point constant */
624/* As in 0f12.456 */
625/* or 0d1.2345e12 */
626const char FLT_CHARS[] = "rRsSfFdDxXpP";
627
628/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
629 changed in read.c . Ideally it shouldn't have to know about it at all,
630 but nothing is ideal around here.
631 */
632
e3de51ce
RS
633/* Types of printf format used for instruction-related error messages.
634 "I" means int ("%d") and "S" means string ("%s"). */
635enum mips_insn_error_format {
636 ERR_FMT_PLAIN,
637 ERR_FMT_I,
638 ERR_FMT_SS,
639};
640
641/* Information about an error that was found while assembling the current
642 instruction. */
643struct mips_insn_error {
644 /* We sometimes need to match an instruction against more than one
645 opcode table entry. Errors found during this matching are reported
646 against a particular syntactic argument rather than against the
647 instruction as a whole. We grade these messages so that errors
648 against argument N have a greater priority than an error against
649 any argument < N, since the former implies that arguments up to N
650 were acceptable and that the opcode entry was therefore a closer match.
651 If several matches report an error against the same argument,
652 we only use that error if it is the same in all cases.
653
654 min_argnum is the minimum argument number for which an error message
655 should be accepted. It is 0 if MSG is against the instruction as
656 a whole. */
657 int min_argnum;
658
659 /* The printf()-style message, including its format and arguments. */
660 enum mips_insn_error_format format;
661 const char *msg;
662 union {
663 int i;
664 const char *ss[2];
665 } u;
666};
667
668/* The error that should be reported for the current instruction. */
669static struct mips_insn_error insn_error;
252b5132
RH
670
671static int auto_align = 1;
672
673/* When outputting SVR4 PIC code, the assembler needs to know the
674 offset in the stack frame from which to restore the $gp register.
675 This is set by the .cprestore pseudo-op, and saved in this
676 variable. */
677static offsetT mips_cprestore_offset = -1;
678
67c1ffbe 679/* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
6478892d 680 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 681 offset and even an other register than $gp as global pointer. */
6478892d
TS
682static offsetT mips_cpreturn_offset = -1;
683static int mips_cpreturn_register = -1;
684static int mips_gp_register = GP;
def2e0dd 685static int mips_gprel_offset = 0;
6478892d 686
7a621144
DJ
687/* Whether mips_cprestore_offset has been set in the current function
688 (or whether it has already been warned about, if not). */
689static int mips_cprestore_valid = 0;
690
252b5132
RH
691/* This is the register which holds the stack frame, as set by the
692 .frame pseudo-op. This is needed to implement .cprestore. */
693static int mips_frame_reg = SP;
694
7a621144
DJ
695/* Whether mips_frame_reg has been set in the current function
696 (or whether it has already been warned about, if not). */
697static int mips_frame_reg_valid = 0;
698
252b5132
RH
699/* To output NOP instructions correctly, we need to keep information
700 about the previous two instructions. */
701
702/* Whether we are optimizing. The default value of 2 means to remove
703 unneeded NOPs and swap branch instructions when possible. A value
704 of 1 means to not swap branches. A value of 0 means to always
705 insert NOPs. */
706static int mips_optimize = 2;
707
708/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
709 equivalent to seeing no -g option at all. */
710static int mips_debug = 0;
711
7d8e00cf
RS
712/* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
713#define MAX_VR4130_NOPS 4
714
715/* The maximum number of NOPs needed to fill delay slots. */
716#define MAX_DELAY_NOPS 2
717
718/* The maximum number of NOPs needed for any purpose. */
719#define MAX_NOPS 4
71400594
RS
720
721/* A list of previous instructions, with index 0 being the most recent.
722 We need to look back MAX_NOPS instructions when filling delay slots
723 or working around processor errata. We need to look back one
724 instruction further if we're thinking about using history[0] to
725 fill a branch delay slot. */
726static struct mips_cl_insn history[1 + MAX_NOPS];
252b5132 727
fc76e730 728/* Arrays of operands for each instruction. */
14daeee3 729#define MAX_OPERANDS 6
fc76e730
RS
730struct mips_operand_array {
731 const struct mips_operand *operand[MAX_OPERANDS];
732};
733static struct mips_operand_array *mips_operands;
734static struct mips_operand_array *mips16_operands;
735static struct mips_operand_array *micromips_operands;
736
1e915849 737/* Nop instructions used by emit_nop. */
df58fc94
RS
738static struct mips_cl_insn nop_insn;
739static struct mips_cl_insn mips16_nop_insn;
740static struct mips_cl_insn micromips_nop16_insn;
741static struct mips_cl_insn micromips_nop32_insn;
1e915849
RS
742
743/* The appropriate nop for the current mode. */
833794fc
MR
744#define NOP_INSN (mips_opts.mips16 \
745 ? &mips16_nop_insn \
746 : (mips_opts.micromips \
747 ? (mips_opts.insn32 \
748 ? &micromips_nop32_insn \
749 : &micromips_nop16_insn) \
750 : &nop_insn))
df58fc94
RS
751
752/* The size of NOP_INSN in bytes. */
833794fc
MR
753#define NOP_INSN_SIZE ((mips_opts.mips16 \
754 || (mips_opts.micromips && !mips_opts.insn32)) \
755 ? 2 : 4)
252b5132 756
252b5132
RH
757/* If this is set, it points to a frag holding nop instructions which
758 were inserted before the start of a noreorder section. If those
759 nops turn out to be unnecessary, the size of the frag can be
760 decreased. */
761static fragS *prev_nop_frag;
762
763/* The number of nop instructions we created in prev_nop_frag. */
764static int prev_nop_frag_holds;
765
766/* The number of nop instructions that we know we need in
bdaaa2e1 767 prev_nop_frag. */
252b5132
RH
768static int prev_nop_frag_required;
769
770/* The number of instructions we've seen since prev_nop_frag. */
771static int prev_nop_frag_since;
772
e8044f35
RS
773/* Relocations against symbols are sometimes done in two parts, with a HI
774 relocation and a LO relocation. Each relocation has only 16 bits of
775 space to store an addend. This means that in order for the linker to
776 handle carries correctly, it must be able to locate both the HI and
777 the LO relocation. This means that the relocations must appear in
778 order in the relocation table.
252b5132
RH
779
780 In order to implement this, we keep track of each unmatched HI
781 relocation. We then sort them so that they immediately precede the
bdaaa2e1 782 corresponding LO relocation. */
252b5132 783
e972090a
NC
784struct mips_hi_fixup
785{
252b5132
RH
786 /* Next HI fixup. */
787 struct mips_hi_fixup *next;
788 /* This fixup. */
789 fixS *fixp;
790 /* The section this fixup is in. */
791 segT seg;
792};
793
794/* The list of unmatched HI relocs. */
795
796static struct mips_hi_fixup *mips_hi_fixup_list;
797
64bdfcaf
RS
798/* The frag containing the last explicit relocation operator.
799 Null if explicit relocations have not been used. */
800
801static fragS *prev_reloc_op_frag;
802
252b5132
RH
803/* Map mips16 register numbers to normal MIPS register numbers. */
804
e972090a
NC
805static const unsigned int mips16_to_32_reg_map[] =
806{
252b5132
RH
807 16, 17, 2, 3, 4, 5, 6, 7
808};
60b63b72 809
df58fc94
RS
810/* Map microMIPS register numbers to normal MIPS register numbers. */
811
df58fc94 812#define micromips_to_32_reg_d_map mips16_to_32_reg_map
df58fc94
RS
813
814/* The microMIPS registers with type h. */
e76ff5ab 815static const unsigned int micromips_to_32_reg_h_map1[] =
df58fc94
RS
816{
817 5, 5, 6, 4, 4, 4, 4, 4
818};
e76ff5ab 819static const unsigned int micromips_to_32_reg_h_map2[] =
df58fc94
RS
820{
821 6, 7, 7, 21, 22, 5, 6, 7
822};
823
df58fc94
RS
824/* The microMIPS registers with type m. */
825static const unsigned int micromips_to_32_reg_m_map[] =
826{
827 0, 17, 2, 3, 16, 18, 19, 20
828};
829
830#define micromips_to_32_reg_n_map micromips_to_32_reg_m_map
831
71400594
RS
832/* Classifies the kind of instructions we're interested in when
833 implementing -mfix-vr4120. */
c67a084a
NC
834enum fix_vr4120_class
835{
71400594
RS
836 FIX_VR4120_MACC,
837 FIX_VR4120_DMACC,
838 FIX_VR4120_MULT,
839 FIX_VR4120_DMULT,
840 FIX_VR4120_DIV,
841 FIX_VR4120_MTHILO,
842 NUM_FIX_VR4120_CLASSES
843};
844
c67a084a
NC
845/* ...likewise -mfix-loongson2f-jump. */
846static bfd_boolean mips_fix_loongson2f_jump;
847
848/* ...likewise -mfix-loongson2f-nop. */
849static bfd_boolean mips_fix_loongson2f_nop;
850
851/* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed. */
852static bfd_boolean mips_fix_loongson2f;
853
71400594
RS
854/* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
855 there must be at least one other instruction between an instruction
856 of type X and an instruction of type Y. */
857static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
858
859/* True if -mfix-vr4120 is in force. */
d766e8ec 860static int mips_fix_vr4120;
4a6a3df4 861
7d8e00cf
RS
862/* ...likewise -mfix-vr4130. */
863static int mips_fix_vr4130;
864
6a32d874
CM
865/* ...likewise -mfix-24k. */
866static int mips_fix_24k;
867
d954098f
DD
868/* ...likewise -mfix-cn63xxp1 */
869static bfd_boolean mips_fix_cn63xxp1;
870
4a6a3df4
AO
871/* We don't relax branches by default, since this causes us to expand
872 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
873 fail to compute the offset before expanding the macro to the most
874 efficient expansion. */
875
876static int mips_relax_branch;
252b5132 877\f
4d7206a2
RS
878/* The expansion of many macros depends on the type of symbol that
879 they refer to. For example, when generating position-dependent code,
880 a macro that refers to a symbol may have two different expansions,
881 one which uses GP-relative addresses and one which uses absolute
882 addresses. When generating SVR4-style PIC, a macro may have
883 different expansions for local and global symbols.
884
885 We handle these situations by generating both sequences and putting
886 them in variant frags. In position-dependent code, the first sequence
887 will be the GP-relative one and the second sequence will be the
888 absolute one. In SVR4 PIC, the first sequence will be for global
889 symbols and the second will be for local symbols.
890
584892a6
RS
891 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
892 SECOND are the lengths of the two sequences in bytes. These fields
893 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
894 the subtype has the following flags:
4d7206a2 895
584892a6
RS
896 RELAX_USE_SECOND
897 Set if it has been decided that we should use the second
898 sequence instead of the first.
899
900 RELAX_SECOND_LONGER
901 Set in the first variant frag if the macro's second implementation
902 is longer than its first. This refers to the macro as a whole,
903 not an individual relaxation.
904
905 RELAX_NOMACRO
906 Set in the first variant frag if the macro appeared in a .set nomacro
907 block and if one alternative requires a warning but the other does not.
908
909 RELAX_DELAY_SLOT
910 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
911 delay slot.
4d7206a2 912
df58fc94
RS
913 RELAX_DELAY_SLOT_16BIT
914 Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
915 16-bit instruction.
916
917 RELAX_DELAY_SLOT_SIZE_FIRST
918 Like RELAX_DELAY_SLOT, but indicates that the first implementation of
919 the macro is of the wrong size for the branch delay slot.
920
921 RELAX_DELAY_SLOT_SIZE_SECOND
922 Like RELAX_DELAY_SLOT, but indicates that the second implementation of
923 the macro is of the wrong size for the branch delay slot.
924
4d7206a2
RS
925 The frag's "opcode" points to the first fixup for relaxable code.
926
927 Relaxable macros are generated using a sequence such as:
928
929 relax_start (SYMBOL);
930 ... generate first expansion ...
931 relax_switch ();
932 ... generate second expansion ...
933 relax_end ();
934
935 The code and fixups for the unwanted alternative are discarded
936 by md_convert_frag. */
584892a6 937#define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
4d7206a2 938
584892a6
RS
939#define RELAX_FIRST(X) (((X) >> 8) & 0xff)
940#define RELAX_SECOND(X) ((X) & 0xff)
941#define RELAX_USE_SECOND 0x10000
942#define RELAX_SECOND_LONGER 0x20000
943#define RELAX_NOMACRO 0x40000
944#define RELAX_DELAY_SLOT 0x80000
df58fc94
RS
945#define RELAX_DELAY_SLOT_16BIT 0x100000
946#define RELAX_DELAY_SLOT_SIZE_FIRST 0x200000
947#define RELAX_DELAY_SLOT_SIZE_SECOND 0x400000
252b5132 948
4a6a3df4
AO
949/* Branch without likely bit. If label is out of range, we turn:
950
951 beq reg1, reg2, label
952 delay slot
953
954 into
955
956 bne reg1, reg2, 0f
957 nop
958 j label
959 0: delay slot
960
961 with the following opcode replacements:
962
963 beq <-> bne
964 blez <-> bgtz
965 bltz <-> bgez
966 bc1f <-> bc1t
967
968 bltzal <-> bgezal (with jal label instead of j label)
969
970 Even though keeping the delay slot instruction in the delay slot of
971 the branch would be more efficient, it would be very tricky to do
972 correctly, because we'd have to introduce a variable frag *after*
973 the delay slot instruction, and expand that instead. Let's do it
974 the easy way for now, even if the branch-not-taken case now costs
975 one additional instruction. Out-of-range branches are not supposed
976 to be common, anyway.
977
978 Branch likely. If label is out of range, we turn:
979
980 beql reg1, reg2, label
981 delay slot (annulled if branch not taken)
982
983 into
984
985 beql reg1, reg2, 1f
986 nop
987 beql $0, $0, 2f
988 nop
989 1: j[al] label
990 delay slot (executed only if branch taken)
991 2:
992
993 It would be possible to generate a shorter sequence by losing the
994 likely bit, generating something like:
b34976b6 995
4a6a3df4
AO
996 bne reg1, reg2, 0f
997 nop
998 j[al] label
999 delay slot (executed only if branch taken)
1000 0:
1001
1002 beql -> bne
1003 bnel -> beq
1004 blezl -> bgtz
1005 bgtzl -> blez
1006 bltzl -> bgez
1007 bgezl -> bltz
1008 bc1fl -> bc1t
1009 bc1tl -> bc1f
1010
1011 bltzall -> bgezal (with jal label instead of j label)
1012 bgezall -> bltzal (ditto)
1013
1014
1015 but it's not clear that it would actually improve performance. */
66b3e8da
MR
1016#define RELAX_BRANCH_ENCODE(at, uncond, likely, link, toofar) \
1017 ((relax_substateT) \
1018 (0xc0000000 \
1019 | ((at) & 0x1f) \
1020 | ((toofar) ? 0x20 : 0) \
1021 | ((link) ? 0x40 : 0) \
1022 | ((likely) ? 0x80 : 0) \
1023 | ((uncond) ? 0x100 : 0)))
4a6a3df4 1024#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
66b3e8da
MR
1025#define RELAX_BRANCH_UNCOND(i) (((i) & 0x100) != 0)
1026#define RELAX_BRANCH_LIKELY(i) (((i) & 0x80) != 0)
1027#define RELAX_BRANCH_LINK(i) (((i) & 0x40) != 0)
1028#define RELAX_BRANCH_TOOFAR(i) (((i) & 0x20) != 0)
1029#define RELAX_BRANCH_AT(i) ((i) & 0x1f)
4a6a3df4 1030
252b5132
RH
1031/* For mips16 code, we use an entirely different form of relaxation.
1032 mips16 supports two versions of most instructions which take
1033 immediate values: a small one which takes some small value, and a
1034 larger one which takes a 16 bit value. Since branches also follow
1035 this pattern, relaxing these values is required.
1036
1037 We can assemble both mips16 and normal MIPS code in a single
1038 object. Therefore, we need to support this type of relaxation at
1039 the same time that we support the relaxation described above. We
1040 use the high bit of the subtype field to distinguish these cases.
1041
1042 The information we store for this type of relaxation is the
1043 argument code found in the opcode file for this relocation, whether
1044 the user explicitly requested a small or extended form, and whether
1045 the relocation is in a jump or jal delay slot. That tells us the
1046 size of the value, and how it should be stored. We also store
1047 whether the fragment is considered to be extended or not. We also
1048 store whether this is known to be a branch to a different section,
1049 whether we have tried to relax this frag yet, and whether we have
1050 ever extended a PC relative fragment because of a shift count. */
1051#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
1052 (0x80000000 \
1053 | ((type) & 0xff) \
1054 | ((small) ? 0x100 : 0) \
1055 | ((ext) ? 0x200 : 0) \
1056 | ((dslot) ? 0x400 : 0) \
1057 | ((jal_dslot) ? 0x800 : 0))
4a6a3df4 1058#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132
RH
1059#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
1060#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
1061#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
1062#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
1063#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
1064#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
1065#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
1066#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
1067#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
1068#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
1069#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
885add95 1070
df58fc94
RS
1071/* For microMIPS code, we use relaxation similar to one we use for
1072 MIPS16 code. Some instructions that take immediate values support
1073 two encodings: a small one which takes some small value, and a
1074 larger one which takes a 16 bit value. As some branches also follow
1075 this pattern, relaxing these values is required.
1076
1077 We can assemble both microMIPS and normal MIPS code in a single
1078 object. Therefore, we need to support this type of relaxation at
1079 the same time that we support the relaxation described above. We
1080 use one of the high bits of the subtype field to distinguish these
1081 cases.
1082
1083 The information we store for this type of relaxation is the argument
1084 code found in the opcode file for this relocation, the register
40209cad
MR
1085 selected as the assembler temporary, whether the branch is
1086 unconditional, whether it is compact, whether it stores the link
1087 address implicitly in $ra, whether relaxation of out-of-range 32-bit
1088 branches to a sequence of instructions is enabled, and whether the
1089 displacement of a branch is too large to fit as an immediate argument
1090 of a 16-bit and a 32-bit branch, respectively. */
1091#define RELAX_MICROMIPS_ENCODE(type, at, uncond, compact, link, \
1092 relax32, toofar16, toofar32) \
1093 (0x40000000 \
1094 | ((type) & 0xff) \
1095 | (((at) & 0x1f) << 8) \
1096 | ((uncond) ? 0x2000 : 0) \
1097 | ((compact) ? 0x4000 : 0) \
1098 | ((link) ? 0x8000 : 0) \
1099 | ((relax32) ? 0x10000 : 0) \
1100 | ((toofar16) ? 0x20000 : 0) \
1101 | ((toofar32) ? 0x40000 : 0))
df58fc94
RS
1102#define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1103#define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1104#define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
40209cad
MR
1105#define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x2000) != 0)
1106#define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x4000) != 0)
1107#define RELAX_MICROMIPS_LINK(i) (((i) & 0x8000) != 0)
1108#define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x10000) != 0)
1109
1110#define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x20000) != 0)
1111#define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x20000)
1112#define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x20000)
1113#define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x40000) != 0)
1114#define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x40000)
1115#define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x40000)
df58fc94 1116
43c0598f
RS
1117/* Sign-extend 16-bit value X. */
1118#define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1119
885add95
CD
1120/* Is the given value a sign-extended 32-bit value? */
1121#define IS_SEXT_32BIT_NUM(x) \
1122 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1123 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1124
1125/* Is the given value a sign-extended 16-bit value? */
1126#define IS_SEXT_16BIT_NUM(x) \
1127 (((x) &~ (offsetT) 0x7fff) == 0 \
1128 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1129
df58fc94
RS
1130/* Is the given value a sign-extended 12-bit value? */
1131#define IS_SEXT_12BIT_NUM(x) \
1132 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1133
7f3c4072
CM
1134/* Is the given value a sign-extended 9-bit value? */
1135#define IS_SEXT_9BIT_NUM(x) \
1136 (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1137
2051e8c4
MR
1138/* Is the given value a zero-extended 32-bit value? Or a negated one? */
1139#define IS_ZEXT_32BIT_NUM(x) \
1140 (((x) &~ (offsetT) 0xffffffff) == 0 \
1141 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1142
bf12938e
RS
1143/* Extract bits MASK << SHIFT from STRUCT and shift them right
1144 SHIFT places. */
1145#define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1146 (((STRUCT) >> (SHIFT)) & (MASK))
1147
bf12938e 1148/* Extract the operand given by FIELD from mips_cl_insn INSN. */
df58fc94
RS
1149#define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1150 (!(MICROMIPS) \
1151 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1152 : EXTRACT_BITS ((INSN).insn_opcode, \
1153 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
bf12938e
RS
1154#define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1155 EXTRACT_BITS ((INSN).insn_opcode, \
1156 MIPS16OP_MASK_##FIELD, \
1157 MIPS16OP_SH_##FIELD)
5c04167a
RS
1158
1159/* The MIPS16 EXTEND opcode, shifted left 16 places. */
1160#define MIPS16_EXTEND (0xf000U << 16)
4d7206a2 1161\f
df58fc94
RS
1162/* Whether or not we are emitting a branch-likely macro. */
1163static bfd_boolean emit_branch_likely_macro = FALSE;
1164
4d7206a2
RS
1165/* Global variables used when generating relaxable macros. See the
1166 comment above RELAX_ENCODE for more details about how relaxation
1167 is used. */
1168static struct {
1169 /* 0 if we're not emitting a relaxable macro.
1170 1 if we're emitting the first of the two relaxation alternatives.
1171 2 if we're emitting the second alternative. */
1172 int sequence;
1173
1174 /* The first relaxable fixup in the current frag. (In other words,
1175 the first fixup that refers to relaxable code.) */
1176 fixS *first_fixup;
1177
1178 /* sizes[0] says how many bytes of the first alternative are stored in
1179 the current frag. Likewise sizes[1] for the second alternative. */
1180 unsigned int sizes[2];
1181
1182 /* The symbol on which the choice of sequence depends. */
1183 symbolS *symbol;
1184} mips_relax;
252b5132 1185\f
584892a6
RS
1186/* Global variables used to decide whether a macro needs a warning. */
1187static struct {
1188 /* True if the macro is in a branch delay slot. */
1189 bfd_boolean delay_slot_p;
1190
df58fc94
RS
1191 /* Set to the length in bytes required if the macro is in a delay slot
1192 that requires a specific length of instruction, otherwise zero. */
1193 unsigned int delay_slot_length;
1194
584892a6
RS
1195 /* For relaxable macros, sizes[0] is the length of the first alternative
1196 in bytes and sizes[1] is the length of the second alternative.
1197 For non-relaxable macros, both elements give the length of the
1198 macro in bytes. */
1199 unsigned int sizes[2];
1200
df58fc94
RS
1201 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1202 instruction of the first alternative in bytes and first_insn_sizes[1]
1203 is the length of the first instruction of the second alternative.
1204 For non-relaxable macros, both elements give the length of the first
1205 instruction in bytes.
1206
1207 Set to zero if we haven't yet seen the first instruction. */
1208 unsigned int first_insn_sizes[2];
1209
1210 /* For relaxable macros, insns[0] is the number of instructions for the
1211 first alternative and insns[1] is the number of instructions for the
1212 second alternative.
1213
1214 For non-relaxable macros, both elements give the number of
1215 instructions for the macro. */
1216 unsigned int insns[2];
1217
584892a6
RS
1218 /* The first variant frag for this macro. */
1219 fragS *first_frag;
1220} mips_macro_warning;
1221\f
252b5132
RH
1222/* Prototypes for static functions. */
1223
252b5132
RH
1224enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1225
b34976b6 1226static void append_insn
df58fc94
RS
1227 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1228 bfd_boolean expansionp);
7d10b47d 1229static void mips_no_prev_insn (void);
c67a084a 1230static void macro_build (expressionS *, const char *, const char *, ...);
b34976b6 1231static void mips16_macro_build
03ea81db 1232 (expressionS *, const char *, const char *, va_list *);
67c0d1eb 1233static void load_register (int, expressionS *, int);
584892a6
RS
1234static void macro_start (void);
1235static void macro_end (void);
833794fc 1236static void macro (struct mips_cl_insn *ip, char *str);
17a2f251 1237static void mips16_macro (struct mips_cl_insn * ip);
17a2f251
TS
1238static void mips_ip (char *str, struct mips_cl_insn * ip);
1239static void mips16_ip (char *str, struct mips_cl_insn * ip);
b34976b6 1240static void mips16_immed
43c0598f
RS
1241 (char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
1242 unsigned int, unsigned long *);
5e0116d5 1243static size_t my_getSmallExpression
17a2f251
TS
1244 (expressionS *, bfd_reloc_code_real_type *, char *);
1245static void my_getExpression (expressionS *, char *);
1246static void s_align (int);
1247static void s_change_sec (int);
1248static void s_change_section (int);
1249static void s_cons (int);
1250static void s_float_cons (int);
1251static void s_mips_globl (int);
1252static void s_option (int);
1253static void s_mipsset (int);
1254static void s_abicalls (int);
1255static void s_cpload (int);
1256static void s_cpsetup (int);
1257static void s_cplocal (int);
1258static void s_cprestore (int);
1259static void s_cpreturn (int);
741d6ea8
JM
1260static void s_dtprelword (int);
1261static void s_dtpreldword (int);
d0f13682
CLT
1262static void s_tprelword (int);
1263static void s_tpreldword (int);
17a2f251
TS
1264static void s_gpvalue (int);
1265static void s_gpword (int);
1266static void s_gpdword (int);
a3f278e2 1267static void s_ehword (int);
17a2f251
TS
1268static void s_cpadd (int);
1269static void s_insn (int);
ba92f887 1270static void s_nan (int);
17a2f251
TS
1271static void md_obj_begin (void);
1272static void md_obj_end (void);
1273static void s_mips_ent (int);
1274static void s_mips_end (int);
1275static void s_mips_frame (int);
1276static void s_mips_mask (int reg_type);
1277static void s_mips_stab (int);
1278static void s_mips_weakext (int);
1279static void s_mips_file (int);
1280static void s_mips_loc (int);
1281static bfd_boolean pic_need_relax (symbolS *, asection *);
4a6a3df4 1282static int relaxed_branch_length (fragS *, asection *, int);
df58fc94
RS
1283static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1284static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
e7af610e
NC
1285
1286/* Table and functions used to map between CPU/ISA names, and
1287 ISA levels, and CPU numbers. */
1288
e972090a
NC
1289struct mips_cpu_info
1290{
e7af610e 1291 const char *name; /* CPU or ISA name. */
d16afab6
RS
1292 int flags; /* MIPS_CPU_* flags. */
1293 int ase; /* Set of ASEs implemented by the CPU. */
e7af610e
NC
1294 int isa; /* ISA level. */
1295 int cpu; /* CPU number (default CPU if ISA). */
1296};
1297
ad3fea08 1298#define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
ad3fea08 1299
17a2f251
TS
1300static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1301static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1302static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132 1303\f
c31f3936
RS
1304/* Command-line options. */
1305const char *md_shortopts = "O::g::G:";
1306
1307enum options
1308 {
1309 OPTION_MARCH = OPTION_MD_BASE,
1310 OPTION_MTUNE,
1311 OPTION_MIPS1,
1312 OPTION_MIPS2,
1313 OPTION_MIPS3,
1314 OPTION_MIPS4,
1315 OPTION_MIPS5,
1316 OPTION_MIPS32,
1317 OPTION_MIPS64,
1318 OPTION_MIPS32R2,
1319 OPTION_MIPS64R2,
1320 OPTION_MIPS16,
1321 OPTION_NO_MIPS16,
1322 OPTION_MIPS3D,
1323 OPTION_NO_MIPS3D,
1324 OPTION_MDMX,
1325 OPTION_NO_MDMX,
1326 OPTION_DSP,
1327 OPTION_NO_DSP,
1328 OPTION_MT,
1329 OPTION_NO_MT,
1330 OPTION_VIRT,
1331 OPTION_NO_VIRT,
1332 OPTION_SMARTMIPS,
1333 OPTION_NO_SMARTMIPS,
1334 OPTION_DSPR2,
1335 OPTION_NO_DSPR2,
1336 OPTION_EVA,
1337 OPTION_NO_EVA,
1338 OPTION_MICROMIPS,
1339 OPTION_NO_MICROMIPS,
1340 OPTION_MCU,
1341 OPTION_NO_MCU,
1342 OPTION_COMPAT_ARCH_BASE,
1343 OPTION_M4650,
1344 OPTION_NO_M4650,
1345 OPTION_M4010,
1346 OPTION_NO_M4010,
1347 OPTION_M4100,
1348 OPTION_NO_M4100,
1349 OPTION_M3900,
1350 OPTION_NO_M3900,
1351 OPTION_M7000_HILO_FIX,
1352 OPTION_MNO_7000_HILO_FIX,
1353 OPTION_FIX_24K,
1354 OPTION_NO_FIX_24K,
1355 OPTION_FIX_LOONGSON2F_JUMP,
1356 OPTION_NO_FIX_LOONGSON2F_JUMP,
1357 OPTION_FIX_LOONGSON2F_NOP,
1358 OPTION_NO_FIX_LOONGSON2F_NOP,
1359 OPTION_FIX_VR4120,
1360 OPTION_NO_FIX_VR4120,
1361 OPTION_FIX_VR4130,
1362 OPTION_NO_FIX_VR4130,
1363 OPTION_FIX_CN63XXP1,
1364 OPTION_NO_FIX_CN63XXP1,
1365 OPTION_TRAP,
1366 OPTION_BREAK,
1367 OPTION_EB,
1368 OPTION_EL,
1369 OPTION_FP32,
1370 OPTION_GP32,
1371 OPTION_CONSTRUCT_FLOATS,
1372 OPTION_NO_CONSTRUCT_FLOATS,
1373 OPTION_FP64,
1374 OPTION_GP64,
1375 OPTION_RELAX_BRANCH,
1376 OPTION_NO_RELAX_BRANCH,
833794fc
MR
1377 OPTION_INSN32,
1378 OPTION_NO_INSN32,
c31f3936
RS
1379 OPTION_MSHARED,
1380 OPTION_MNO_SHARED,
1381 OPTION_MSYM32,
1382 OPTION_MNO_SYM32,
1383 OPTION_SOFT_FLOAT,
1384 OPTION_HARD_FLOAT,
1385 OPTION_SINGLE_FLOAT,
1386 OPTION_DOUBLE_FLOAT,
1387 OPTION_32,
c31f3936
RS
1388 OPTION_CALL_SHARED,
1389 OPTION_CALL_NONPIC,
1390 OPTION_NON_SHARED,
1391 OPTION_XGOT,
1392 OPTION_MABI,
1393 OPTION_N32,
1394 OPTION_64,
1395 OPTION_MDEBUG,
1396 OPTION_NO_MDEBUG,
1397 OPTION_PDR,
1398 OPTION_NO_PDR,
1399 OPTION_MVXWORKS_PIC,
ba92f887 1400 OPTION_NAN,
c31f3936
RS
1401 OPTION_END_OF_ENUM
1402 };
1403
1404struct option md_longopts[] =
1405{
1406 /* Options which specify architecture. */
1407 {"march", required_argument, NULL, OPTION_MARCH},
1408 {"mtune", required_argument, NULL, OPTION_MTUNE},
1409 {"mips0", no_argument, NULL, OPTION_MIPS1},
1410 {"mips1", no_argument, NULL, OPTION_MIPS1},
1411 {"mips2", no_argument, NULL, OPTION_MIPS2},
1412 {"mips3", no_argument, NULL, OPTION_MIPS3},
1413 {"mips4", no_argument, NULL, OPTION_MIPS4},
1414 {"mips5", no_argument, NULL, OPTION_MIPS5},
1415 {"mips32", no_argument, NULL, OPTION_MIPS32},
1416 {"mips64", no_argument, NULL, OPTION_MIPS64},
1417 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
1418 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
1419
1420 /* Options which specify Application Specific Extensions (ASEs). */
1421 {"mips16", no_argument, NULL, OPTION_MIPS16},
1422 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1423 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1424 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1425 {"mdmx", no_argument, NULL, OPTION_MDMX},
1426 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1427 {"mdsp", no_argument, NULL, OPTION_DSP},
1428 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1429 {"mmt", no_argument, NULL, OPTION_MT},
1430 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1431 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1432 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1433 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1434 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
1435 {"meva", no_argument, NULL, OPTION_EVA},
1436 {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1437 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1438 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1439 {"mmcu", no_argument, NULL, OPTION_MCU},
1440 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1441 {"mvirt", no_argument, NULL, OPTION_VIRT},
1442 {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
1443
1444 /* Old-style architecture options. Don't add more of these. */
1445 {"m4650", no_argument, NULL, OPTION_M4650},
1446 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1447 {"m4010", no_argument, NULL, OPTION_M4010},
1448 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1449 {"m4100", no_argument, NULL, OPTION_M4100},
1450 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1451 {"m3900", no_argument, NULL, OPTION_M3900},
1452 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1453
1454 /* Options which enable bug fixes. */
1455 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1456 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1457 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1458 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1459 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1460 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1461 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1462 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
1463 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1464 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
1465 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1466 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
1467 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
1468 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1469 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
1470
1471 /* Miscellaneous options. */
1472 {"trap", no_argument, NULL, OPTION_TRAP},
1473 {"no-break", no_argument, NULL, OPTION_TRAP},
1474 {"break", no_argument, NULL, OPTION_BREAK},
1475 {"no-trap", no_argument, NULL, OPTION_BREAK},
1476 {"EB", no_argument, NULL, OPTION_EB},
1477 {"EL", no_argument, NULL, OPTION_EL},
1478 {"mfp32", no_argument, NULL, OPTION_FP32},
1479 {"mgp32", no_argument, NULL, OPTION_GP32},
1480 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1481 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1482 {"mfp64", no_argument, NULL, OPTION_FP64},
1483 {"mgp64", no_argument, NULL, OPTION_GP64},
1484 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1485 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
833794fc
MR
1486 {"minsn32", no_argument, NULL, OPTION_INSN32},
1487 {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
c31f3936
RS
1488 {"mshared", no_argument, NULL, OPTION_MSHARED},
1489 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1490 {"msym32", no_argument, NULL, OPTION_MSYM32},
1491 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1492 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1493 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1494 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1495 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
1496
1497 /* Strictly speaking this next option is ELF specific,
1498 but we allow it for other ports as well in order to
1499 make testing easier. */
1500 {"32", no_argument, NULL, OPTION_32},
1501
1502 /* ELF-specific options. */
c31f3936
RS
1503 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1504 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1505 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1506 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
1507 {"xgot", no_argument, NULL, OPTION_XGOT},
1508 {"mabi", required_argument, NULL, OPTION_MABI},
1509 {"n32", no_argument, NULL, OPTION_N32},
1510 {"64", no_argument, NULL, OPTION_64},
1511 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1512 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1513 {"mpdr", no_argument, NULL, OPTION_PDR},
1514 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1515 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
ba92f887 1516 {"mnan", required_argument, NULL, OPTION_NAN},
c31f3936
RS
1517
1518 {NULL, no_argument, NULL, 0}
1519};
1520size_t md_longopts_size = sizeof (md_longopts);
1521\f
c6278170
RS
1522/* Information about either an Application Specific Extension or an
1523 optional architecture feature that, for simplicity, we treat in the
1524 same way as an ASE. */
1525struct mips_ase
1526{
1527 /* The name of the ASE, used in both the command-line and .set options. */
1528 const char *name;
1529
1530 /* The associated ASE_* flags. If the ASE is available on both 32-bit
1531 and 64-bit architectures, the flags here refer to the subset that
1532 is available on both. */
1533 unsigned int flags;
1534
1535 /* The ASE_* flag used for instructions that are available on 64-bit
1536 architectures but that are not included in FLAGS. */
1537 unsigned int flags64;
1538
1539 /* The command-line options that turn the ASE on and off. */
1540 int option_on;
1541 int option_off;
1542
1543 /* The minimum required architecture revisions for MIPS32, MIPS64,
1544 microMIPS32 and microMIPS64, or -1 if the extension isn't supported. */
1545 int mips32_rev;
1546 int mips64_rev;
1547 int micromips32_rev;
1548 int micromips64_rev;
1549};
1550
1551/* A table of all supported ASEs. */
1552static const struct mips_ase mips_ases[] = {
1553 { "dsp", ASE_DSP, ASE_DSP64,
1554 OPTION_DSP, OPTION_NO_DSP,
1555 2, 2, 2, 2 },
1556
1557 { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1558 OPTION_DSPR2, OPTION_NO_DSPR2,
1559 2, 2, 2, 2 },
1560
1561 { "eva", ASE_EVA, 0,
1562 OPTION_EVA, OPTION_NO_EVA,
1563 2, 2, 2, 2 },
1564
1565 { "mcu", ASE_MCU, 0,
1566 OPTION_MCU, OPTION_NO_MCU,
1567 2, 2, 2, 2 },
1568
1569 /* Deprecated in MIPS64r5, but we don't implement that yet. */
1570 { "mdmx", ASE_MDMX, 0,
1571 OPTION_MDMX, OPTION_NO_MDMX,
1572 -1, 1, -1, -1 },
1573
1574 /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2. */
1575 { "mips3d", ASE_MIPS3D, 0,
1576 OPTION_MIPS3D, OPTION_NO_MIPS3D,
1577 2, 1, -1, -1 },
1578
1579 { "mt", ASE_MT, 0,
1580 OPTION_MT, OPTION_NO_MT,
1581 2, 2, -1, -1 },
1582
1583 { "smartmips", ASE_SMARTMIPS, 0,
1584 OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
1585 1, -1, -1, -1 },
1586
1587 { "virt", ASE_VIRT, ASE_VIRT64,
1588 OPTION_VIRT, OPTION_NO_VIRT,
1589 2, 2, 2, 2 }
1590};
1591
1592/* The set of ASEs that require -mfp64. */
1593#define FP64_ASES (ASE_MIPS3D | ASE_MDMX)
1594
1595/* Groups of ASE_* flags that represent different revisions of an ASE. */
1596static const unsigned int mips_ase_groups[] = {
1597 ASE_DSP | ASE_DSPR2
1598};
1599\f
252b5132
RH
1600/* Pseudo-op table.
1601
1602 The following pseudo-ops from the Kane and Heinrich MIPS book
1603 should be defined here, but are currently unsupported: .alias,
1604 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1605
1606 The following pseudo-ops from the Kane and Heinrich MIPS book are
1607 specific to the type of debugging information being generated, and
1608 should be defined by the object format: .aent, .begin, .bend,
1609 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1610 .vreg.
1611
1612 The following pseudo-ops from the Kane and Heinrich MIPS book are
1613 not MIPS CPU specific, but are also not specific to the object file
1614 format. This file is probably the best place to define them, but
d84bcf09 1615 they are not currently supported: .asm0, .endr, .lab, .struct. */
252b5132 1616
e972090a
NC
1617static const pseudo_typeS mips_pseudo_table[] =
1618{
beae10d5 1619 /* MIPS specific pseudo-ops. */
252b5132
RH
1620 {"option", s_option, 0},
1621 {"set", s_mipsset, 0},
1622 {"rdata", s_change_sec, 'r'},
1623 {"sdata", s_change_sec, 's'},
1624 {"livereg", s_ignore, 0},
1625 {"abicalls", s_abicalls, 0},
1626 {"cpload", s_cpload, 0},
6478892d
TS
1627 {"cpsetup", s_cpsetup, 0},
1628 {"cplocal", s_cplocal, 0},
252b5132 1629 {"cprestore", s_cprestore, 0},
6478892d 1630 {"cpreturn", s_cpreturn, 0},
741d6ea8
JM
1631 {"dtprelword", s_dtprelword, 0},
1632 {"dtpreldword", s_dtpreldword, 0},
d0f13682
CLT
1633 {"tprelword", s_tprelword, 0},
1634 {"tpreldword", s_tpreldword, 0},
6478892d 1635 {"gpvalue", s_gpvalue, 0},
252b5132 1636 {"gpword", s_gpword, 0},
10181a0d 1637 {"gpdword", s_gpdword, 0},
a3f278e2 1638 {"ehword", s_ehword, 0},
252b5132
RH
1639 {"cpadd", s_cpadd, 0},
1640 {"insn", s_insn, 0},
ba92f887 1641 {"nan", s_nan, 0},
252b5132 1642
beae10d5 1643 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132 1644 chips. */
38a57ae7 1645 {"asciiz", stringer, 8 + 1},
252b5132
RH
1646 {"bss", s_change_sec, 'b'},
1647 {"err", s_err, 0},
1648 {"half", s_cons, 1},
1649 {"dword", s_cons, 3},
1650 {"weakext", s_mips_weakext, 0},
7c752c2a
TS
1651 {"origin", s_org, 0},
1652 {"repeat", s_rept, 0},
252b5132 1653
998b3c36
MR
1654 /* For MIPS this is non-standard, but we define it for consistency. */
1655 {"sbss", s_change_sec, 'B'},
1656
beae10d5 1657 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1658 here for one reason or another. */
1659 {"align", s_align, 0},
1660 {"byte", s_cons, 0},
1661 {"data", s_change_sec, 'd'},
1662 {"double", s_float_cons, 'd'},
1663 {"float", s_float_cons, 'f'},
1664 {"globl", s_mips_globl, 0},
1665 {"global", s_mips_globl, 0},
1666 {"hword", s_cons, 1},
1667 {"int", s_cons, 2},
1668 {"long", s_cons, 2},
1669 {"octa", s_cons, 4},
1670 {"quad", s_cons, 3},
cca86cc8 1671 {"section", s_change_section, 0},
252b5132
RH
1672 {"short", s_cons, 1},
1673 {"single", s_float_cons, 'f'},
754e2bb9 1674 {"stabd", s_mips_stab, 'd'},
252b5132 1675 {"stabn", s_mips_stab, 'n'},
754e2bb9 1676 {"stabs", s_mips_stab, 's'},
252b5132
RH
1677 {"text", s_change_sec, 't'},
1678 {"word", s_cons, 2},
add56521 1679
add56521 1680 { "extern", ecoff_directive_extern, 0},
add56521 1681
43841e91 1682 { NULL, NULL, 0 },
252b5132
RH
1683};
1684
e972090a
NC
1685static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1686{
beae10d5
KH
1687 /* These pseudo-ops should be defined by the object file format.
1688 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1689 {"aent", s_mips_ent, 1},
1690 {"bgnb", s_ignore, 0},
1691 {"end", s_mips_end, 0},
1692 {"endb", s_ignore, 0},
1693 {"ent", s_mips_ent, 0},
c5dd6aab 1694 {"file", s_mips_file, 0},
252b5132
RH
1695 {"fmask", s_mips_mask, 'F'},
1696 {"frame", s_mips_frame, 0},
c5dd6aab 1697 {"loc", s_mips_loc, 0},
252b5132
RH
1698 {"mask", s_mips_mask, 'R'},
1699 {"verstamp", s_ignore, 0},
43841e91 1700 { NULL, NULL, 0 },
252b5132
RH
1701};
1702
3ae8dd8d
MR
1703/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1704 purpose of the `.dc.a' internal pseudo-op. */
1705
1706int
1707mips_address_bytes (void)
1708{
1709 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1710}
1711
17a2f251 1712extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1713
1714void
17a2f251 1715mips_pop_insert (void)
252b5132
RH
1716{
1717 pop_insert (mips_pseudo_table);
1718 if (! ECOFF_DEBUGGING)
1719 pop_insert (mips_nonecoff_pseudo_table);
1720}
1721\f
1722/* Symbols labelling the current insn. */
1723
e972090a
NC
1724struct insn_label_list
1725{
252b5132
RH
1726 struct insn_label_list *next;
1727 symbolS *label;
1728};
1729
252b5132 1730static struct insn_label_list *free_insn_labels;
742a56fe 1731#define label_list tc_segment_info_data.labels
252b5132 1732
17a2f251 1733static void mips_clear_insn_labels (void);
df58fc94
RS
1734static void mips_mark_labels (void);
1735static void mips_compressed_mark_labels (void);
252b5132
RH
1736
1737static inline void
17a2f251 1738mips_clear_insn_labels (void)
252b5132
RH
1739{
1740 register struct insn_label_list **pl;
a8dbcb85 1741 segment_info_type *si;
252b5132 1742
a8dbcb85
TS
1743 if (now_seg)
1744 {
1745 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1746 ;
1747
1748 si = seg_info (now_seg);
1749 *pl = si->label_list;
1750 si->label_list = NULL;
1751 }
252b5132 1752}
a8dbcb85 1753
df58fc94
RS
1754/* Mark instruction labels in MIPS16/microMIPS mode. */
1755
1756static inline void
1757mips_mark_labels (void)
1758{
1759 if (HAVE_CODE_COMPRESSION)
1760 mips_compressed_mark_labels ();
1761}
252b5132
RH
1762\f
1763static char *expr_end;
1764
e423441d
RS
1765/* An expression in a macro instruction. This is set by mips_ip and
1766 mips16_ip. */
252b5132
RH
1767
1768static expressionS imm_expr;
252b5132 1769
77bd4346
RS
1770/* The relocatable field in an instruction and the relocs associated
1771 with it. These variables are used for instructions like LUI and
1772 JAL as well as true offsets. They are also used for address
1773 operands in macros. */
252b5132 1774
77bd4346 1775static expressionS offset_expr;
f6688943
TS
1776static bfd_reloc_code_real_type offset_reloc[3]
1777 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 1778
df58fc94
RS
1779/* This is set to the resulting size of the instruction to be produced
1780 by mips16_ip if an explicit extension is used or by mips_ip if an
1781 explicit size is supplied. */
252b5132 1782
df58fc94 1783static unsigned int forced_insn_length;
252b5132 1784
e1b47bd5
RS
1785/* True if we are assembling an instruction. All dot symbols defined during
1786 this time should be treated as code labels. */
1787
1788static bfd_boolean mips_assembling_insn;
1789
ecb4347a
DJ
1790/* The pdr segment for per procedure frame/regmask info. Not used for
1791 ECOFF debugging. */
252b5132
RH
1792
1793static segT pdr_seg;
252b5132 1794
e013f690
TS
1795/* The default target format to use. */
1796
aeffff67
RS
1797#if defined (TE_FreeBSD)
1798#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
1799#elif defined (TE_TMIPS)
1800#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
1801#else
1802#define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
1803#endif
1804
e013f690 1805const char *
17a2f251 1806mips_target_format (void)
e013f690
TS
1807{
1808 switch (OUTPUT_FLAVOR)
1809 {
e013f690 1810 case bfd_target_elf_flavour:
0a44bf69
RS
1811#ifdef TE_VXWORKS
1812 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1813 return (target_big_endian
1814 ? "elf32-bigmips-vxworks"
1815 : "elf32-littlemips-vxworks");
1816#endif
e013f690 1817 return (target_big_endian
cfe86eaa 1818 ? (HAVE_64BIT_OBJECTS
aeffff67 1819 ? ELF_TARGET ("elf64-", "big")
cfe86eaa 1820 : (HAVE_NEWABI
aeffff67
RS
1821 ? ELF_TARGET ("elf32-n", "big")
1822 : ELF_TARGET ("elf32-", "big")))
cfe86eaa 1823 : (HAVE_64BIT_OBJECTS
aeffff67 1824 ? ELF_TARGET ("elf64-", "little")
cfe86eaa 1825 : (HAVE_NEWABI
aeffff67
RS
1826 ? ELF_TARGET ("elf32-n", "little")
1827 : ELF_TARGET ("elf32-", "little"))));
e013f690
TS
1828 default:
1829 abort ();
1830 return NULL;
1831 }
1832}
1833
c6278170
RS
1834/* Return the ISA revision that is currently in use, or 0 if we are
1835 generating code for MIPS V or below. */
1836
1837static int
1838mips_isa_rev (void)
1839{
1840 if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
1841 return 2;
1842
1843 /* microMIPS implies revision 2 or above. */
1844 if (mips_opts.micromips)
1845 return 2;
1846
1847 if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
1848 return 1;
1849
1850 return 0;
1851}
1852
1853/* Return the mask of all ASEs that are revisions of those in FLAGS. */
1854
1855static unsigned int
1856mips_ase_mask (unsigned int flags)
1857{
1858 unsigned int i;
1859
1860 for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
1861 if (flags & mips_ase_groups[i])
1862 flags |= mips_ase_groups[i];
1863 return flags;
1864}
1865
1866/* Check whether the current ISA supports ASE. Issue a warning if
1867 appropriate. */
1868
1869static void
1870mips_check_isa_supports_ase (const struct mips_ase *ase)
1871{
1872 const char *base;
1873 int min_rev, size;
1874 static unsigned int warned_isa;
1875 static unsigned int warned_fp32;
1876
1877 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
1878 min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
1879 else
1880 min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
1881 if ((min_rev < 0 || mips_isa_rev () < min_rev)
1882 && (warned_isa & ase->flags) != ase->flags)
1883 {
1884 warned_isa |= ase->flags;
1885 base = mips_opts.micromips ? "microMIPS" : "MIPS";
1886 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
1887 if (min_rev < 0)
1888 as_warn (_("The %d-bit %s architecture does not support the"
1889 " `%s' extension"), size, base, ase->name);
1890 else
1891 as_warn (_("The `%s' extension requires %s%d revision %d or greater"),
1892 ase->name, base, size, min_rev);
1893 }
1894 if ((ase->flags & FP64_ASES)
1895 && mips_opts.fp32
1896 && (warned_fp32 & ase->flags) != ase->flags)
1897 {
1898 warned_fp32 |= ase->flags;
1899 as_warn (_("The `%s' extension requires 64-bit FPRs"), ase->name);
1900 }
1901}
1902
1903/* Check all enabled ASEs to see whether they are supported by the
1904 chosen architecture. */
1905
1906static void
1907mips_check_isa_supports_ases (void)
1908{
1909 unsigned int i, mask;
1910
1911 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
1912 {
1913 mask = mips_ase_mask (mips_ases[i].flags);
1914 if ((mips_opts.ase & mask) == mips_ases[i].flags)
1915 mips_check_isa_supports_ase (&mips_ases[i]);
1916 }
1917}
1918
1919/* Set the state of ASE to ENABLED_P. Return the mask of ASE_* flags
1920 that were affected. */
1921
1922static unsigned int
1923mips_set_ase (const struct mips_ase *ase, bfd_boolean enabled_p)
1924{
1925 unsigned int mask;
1926
1927 mask = mips_ase_mask (ase->flags);
1928 mips_opts.ase &= ~mask;
1929 if (enabled_p)
1930 mips_opts.ase |= ase->flags;
1931 return mask;
1932}
1933
1934/* Return the ASE called NAME, or null if none. */
1935
1936static const struct mips_ase *
1937mips_lookup_ase (const char *name)
1938{
1939 unsigned int i;
1940
1941 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
1942 if (strcmp (name, mips_ases[i].name) == 0)
1943 return &mips_ases[i];
1944 return NULL;
1945}
1946
df58fc94
RS
1947/* Return the length of a microMIPS instruction in bytes. If bits of
1948 the mask beyond the low 16 are 0, then it is a 16-bit instruction.
1949 Otherwise assume a 32-bit instruction; 48-bit instructions (0x1f
1950 major opcode) will require further modifications to the opcode
1951 table. */
1952
1953static inline unsigned int
1954micromips_insn_length (const struct mips_opcode *mo)
1955{
1956 return (mo->mask >> 16) == 0 ? 2 : 4;
1957}
1958
5c04167a
RS
1959/* Return the length of MIPS16 instruction OPCODE. */
1960
1961static inline unsigned int
1962mips16_opcode_length (unsigned long opcode)
1963{
1964 return (opcode >> 16) == 0 ? 2 : 4;
1965}
1966
1e915849
RS
1967/* Return the length of instruction INSN. */
1968
1969static inline unsigned int
1970insn_length (const struct mips_cl_insn *insn)
1971{
df58fc94
RS
1972 if (mips_opts.micromips)
1973 return micromips_insn_length (insn->insn_mo);
1974 else if (mips_opts.mips16)
5c04167a 1975 return mips16_opcode_length (insn->insn_opcode);
df58fc94 1976 else
1e915849 1977 return 4;
1e915849
RS
1978}
1979
1980/* Initialise INSN from opcode entry MO. Leave its position unspecified. */
1981
1982static void
1983create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
1984{
1985 size_t i;
1986
1987 insn->insn_mo = mo;
1e915849
RS
1988 insn->insn_opcode = mo->match;
1989 insn->frag = NULL;
1990 insn->where = 0;
1991 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1992 insn->fixp[i] = NULL;
1993 insn->fixed_p = (mips_opts.noreorder > 0);
1994 insn->noreorder_p = (mips_opts.noreorder > 0);
1995 insn->mips16_absolute_jump_p = 0;
15be625d 1996 insn->complete_p = 0;
e407c74b 1997 insn->cleared_p = 0;
1e915849
RS
1998}
1999
fc76e730
RS
2000/* Get a list of all the operands in INSN. */
2001
2002static const struct mips_operand_array *
2003insn_operands (const struct mips_cl_insn *insn)
2004{
2005 if (insn->insn_mo >= &mips_opcodes[0]
2006 && insn->insn_mo < &mips_opcodes[NUMOPCODES])
2007 return &mips_operands[insn->insn_mo - &mips_opcodes[0]];
2008
2009 if (insn->insn_mo >= &mips16_opcodes[0]
2010 && insn->insn_mo < &mips16_opcodes[bfd_mips16_num_opcodes])
2011 return &mips16_operands[insn->insn_mo - &mips16_opcodes[0]];
2012
2013 if (insn->insn_mo >= &micromips_opcodes[0]
2014 && insn->insn_mo < &micromips_opcodes[bfd_micromips_num_opcodes])
2015 return &micromips_operands[insn->insn_mo - &micromips_opcodes[0]];
2016
2017 abort ();
2018}
2019
2020/* Get a description of operand OPNO of INSN. */
2021
2022static const struct mips_operand *
2023insn_opno (const struct mips_cl_insn *insn, unsigned opno)
2024{
2025 const struct mips_operand_array *operands;
2026
2027 operands = insn_operands (insn);
2028 if (opno >= MAX_OPERANDS || !operands->operand[opno])
2029 abort ();
2030 return operands->operand[opno];
2031}
2032
e077a1c8
RS
2033/* Install UVAL as the value of OPERAND in INSN. */
2034
2035static inline void
2036insn_insert_operand (struct mips_cl_insn *insn,
2037 const struct mips_operand *operand, unsigned int uval)
2038{
2039 insn->insn_opcode = mips_insert_operand (operand, insn->insn_opcode, uval);
2040}
2041
fc76e730
RS
2042/* Extract the value of OPERAND from INSN. */
2043
2044static inline unsigned
2045insn_extract_operand (const struct mips_cl_insn *insn,
2046 const struct mips_operand *operand)
2047{
2048 return mips_extract_operand (operand, insn->insn_opcode);
2049}
2050
df58fc94 2051/* Record the current MIPS16/microMIPS mode in now_seg. */
742a56fe
RS
2052
2053static void
df58fc94 2054mips_record_compressed_mode (void)
742a56fe
RS
2055{
2056 segment_info_type *si;
2057
2058 si = seg_info (now_seg);
2059 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2060 si->tc_segment_info_data.mips16 = mips_opts.mips16;
df58fc94
RS
2061 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2062 si->tc_segment_info_data.micromips = mips_opts.micromips;
742a56fe
RS
2063}
2064
4d68580a
RS
2065/* Read a standard MIPS instruction from BUF. */
2066
2067static unsigned long
2068read_insn (char *buf)
2069{
2070 if (target_big_endian)
2071 return bfd_getb32 ((bfd_byte *) buf);
2072 else
2073 return bfd_getl32 ((bfd_byte *) buf);
2074}
2075
2076/* Write standard MIPS instruction INSN to BUF. Return a pointer to
2077 the next byte. */
2078
2079static char *
2080write_insn (char *buf, unsigned int insn)
2081{
2082 md_number_to_chars (buf, insn, 4);
2083 return buf + 4;
2084}
2085
2086/* Read a microMIPS or MIPS16 opcode from BUF, given that it
2087 has length LENGTH. */
2088
2089static unsigned long
2090read_compressed_insn (char *buf, unsigned int length)
2091{
2092 unsigned long insn;
2093 unsigned int i;
2094
2095 insn = 0;
2096 for (i = 0; i < length; i += 2)
2097 {
2098 insn <<= 16;
2099 if (target_big_endian)
2100 insn |= bfd_getb16 ((char *) buf);
2101 else
2102 insn |= bfd_getl16 ((char *) buf);
2103 buf += 2;
2104 }
2105 return insn;
2106}
2107
5c04167a
RS
2108/* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2109 instruction is LENGTH bytes long. Return a pointer to the next byte. */
2110
2111static char *
2112write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2113{
2114 unsigned int i;
2115
2116 for (i = 0; i < length; i += 2)
2117 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2118 return buf + length;
2119}
2120
1e915849
RS
2121/* Install INSN at the location specified by its "frag" and "where" fields. */
2122
2123static void
2124install_insn (const struct mips_cl_insn *insn)
2125{
2126 char *f = insn->frag->fr_literal + insn->where;
5c04167a
RS
2127 if (HAVE_CODE_COMPRESSION)
2128 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
1e915849 2129 else
4d68580a 2130 write_insn (f, insn->insn_opcode);
df58fc94 2131 mips_record_compressed_mode ();
1e915849
RS
2132}
2133
2134/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
2135 and install the opcode in the new location. */
2136
2137static void
2138move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2139{
2140 size_t i;
2141
2142 insn->frag = frag;
2143 insn->where = where;
2144 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2145 if (insn->fixp[i] != NULL)
2146 {
2147 insn->fixp[i]->fx_frag = frag;
2148 insn->fixp[i]->fx_where = where;
2149 }
2150 install_insn (insn);
2151}
2152
2153/* Add INSN to the end of the output. */
2154
2155static void
2156add_fixed_insn (struct mips_cl_insn *insn)
2157{
2158 char *f = frag_more (insn_length (insn));
2159 move_insn (insn, frag_now, f - frag_now->fr_literal);
2160}
2161
2162/* Start a variant frag and move INSN to the start of the variant part,
2163 marking it as fixed. The other arguments are as for frag_var. */
2164
2165static void
2166add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2167 relax_substateT subtype, symbolS *symbol, offsetT offset)
2168{
2169 frag_grow (max_chars);
2170 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2171 insn->fixed_p = 1;
2172 frag_var (rs_machine_dependent, max_chars, var,
2173 subtype, symbol, offset, NULL);
2174}
2175
2176/* Insert N copies of INSN into the history buffer, starting at
2177 position FIRST. Neither FIRST nor N need to be clipped. */
2178
2179static void
2180insert_into_history (unsigned int first, unsigned int n,
2181 const struct mips_cl_insn *insn)
2182{
2183 if (mips_relax.sequence != 2)
2184 {
2185 unsigned int i;
2186
2187 for (i = ARRAY_SIZE (history); i-- > first;)
2188 if (i >= first + n)
2189 history[i] = history[i - n];
2190 else
2191 history[i] = *insn;
2192 }
2193}
2194
e3de51ce
RS
2195/* Clear the error in insn_error. */
2196
2197static void
2198clear_insn_error (void)
2199{
2200 memset (&insn_error, 0, sizeof (insn_error));
2201}
2202
2203/* Possibly record error message MSG for the current instruction.
2204 If the error is about a particular argument, ARGNUM is the 1-based
2205 number of that argument, otherwise it is 0. FORMAT is the format
2206 of MSG. Return true if MSG was used, false if the current message
2207 was kept. */
2208
2209static bfd_boolean
2210set_insn_error_format (int argnum, enum mips_insn_error_format format,
2211 const char *msg)
2212{
2213 if (argnum == 0)
2214 {
2215 /* Give priority to errors against specific arguments, and to
2216 the first whole-instruction message. */
2217 if (insn_error.msg)
2218 return FALSE;
2219 }
2220 else
2221 {
2222 /* Keep insn_error if it is against a later argument. */
2223 if (argnum < insn_error.min_argnum)
2224 return FALSE;
2225
2226 /* If both errors are against the same argument but are different,
2227 give up on reporting a specific error for this argument.
2228 See the comment about mips_insn_error for details. */
2229 if (argnum == insn_error.min_argnum
2230 && insn_error.msg
2231 && strcmp (insn_error.msg, msg) != 0)
2232 {
2233 insn_error.msg = 0;
2234 insn_error.min_argnum += 1;
2235 return FALSE;
2236 }
2237 }
2238 insn_error.min_argnum = argnum;
2239 insn_error.format = format;
2240 insn_error.msg = msg;
2241 return TRUE;
2242}
2243
2244/* Record an instruction error with no % format fields. ARGNUM and MSG are
2245 as for set_insn_error_format. */
2246
2247static void
2248set_insn_error (int argnum, const char *msg)
2249{
2250 set_insn_error_format (argnum, ERR_FMT_PLAIN, msg);
2251}
2252
2253/* Record an instruction error with one %d field I. ARGNUM and MSG are
2254 as for set_insn_error_format. */
2255
2256static void
2257set_insn_error_i (int argnum, const char *msg, int i)
2258{
2259 if (set_insn_error_format (argnum, ERR_FMT_I, msg))
2260 insn_error.u.i = i;
2261}
2262
2263/* Record an instruction error with two %s fields S1 and S2. ARGNUM and MSG
2264 are as for set_insn_error_format. */
2265
2266static void
2267set_insn_error_ss (int argnum, const char *msg, const char *s1, const char *s2)
2268{
2269 if (set_insn_error_format (argnum, ERR_FMT_SS, msg))
2270 {
2271 insn_error.u.ss[0] = s1;
2272 insn_error.u.ss[1] = s2;
2273 }
2274}
2275
2276/* Report the error in insn_error, which is against assembly code STR. */
2277
2278static void
2279report_insn_error (const char *str)
2280{
2281 const char *msg;
2282
2283 msg = ACONCAT ((insn_error.msg, " `%s'", NULL));
2284 switch (insn_error.format)
2285 {
2286 case ERR_FMT_PLAIN:
2287 as_bad (msg, str);
2288 break;
2289
2290 case ERR_FMT_I:
2291 as_bad (msg, insn_error.u.i, str);
2292 break;
2293
2294 case ERR_FMT_SS:
2295 as_bad (msg, insn_error.u.ss[0], insn_error.u.ss[1], str);
2296 break;
2297 }
2298}
2299
71400594
RS
2300/* Initialize vr4120_conflicts. There is a bit of duplication here:
2301 the idea is to make it obvious at a glance that each errata is
2302 included. */
2303
2304static void
2305init_vr4120_conflicts (void)
2306{
2307#define CONFLICT(FIRST, SECOND) \
2308 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2309
2310 /* Errata 21 - [D]DIV[U] after [D]MACC */
2311 CONFLICT (MACC, DIV);
2312 CONFLICT (DMACC, DIV);
2313
2314 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
2315 CONFLICT (DMULT, DMULT);
2316 CONFLICT (DMULT, DMACC);
2317 CONFLICT (DMACC, DMULT);
2318 CONFLICT (DMACC, DMACC);
2319
2320 /* Errata 24 - MT{LO,HI} after [D]MACC */
2321 CONFLICT (MACC, MTHILO);
2322 CONFLICT (DMACC, MTHILO);
2323
2324 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2325 instruction is executed immediately after a MACC or DMACC
2326 instruction, the result of [either instruction] is incorrect." */
2327 CONFLICT (MACC, MULT);
2328 CONFLICT (MACC, DMULT);
2329 CONFLICT (DMACC, MULT);
2330 CONFLICT (DMACC, DMULT);
2331
2332 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2333 executed immediately after a DMULT, DMULTU, DIV, DIVU,
2334 DDIV or DDIVU instruction, the result of the MACC or
2335 DMACC instruction is incorrect.". */
2336 CONFLICT (DMULT, MACC);
2337 CONFLICT (DMULT, DMACC);
2338 CONFLICT (DIV, MACC);
2339 CONFLICT (DIV, DMACC);
2340
2341#undef CONFLICT
2342}
2343
707bfff6
TS
2344struct regname {
2345 const char *name;
2346 unsigned int num;
2347};
2348
14daeee3
RS
2349#define RNUM_MASK 0x00000ff
2350#define RTYPE_MASK 0x0efff00
2351#define RTYPE_NUM 0x0000100
2352#define RTYPE_FPU 0x0000200
2353#define RTYPE_FCC 0x0000400
2354#define RTYPE_VEC 0x0000800
2355#define RTYPE_GP 0x0001000
2356#define RTYPE_CP0 0x0002000
2357#define RTYPE_PC 0x0004000
2358#define RTYPE_ACC 0x0008000
2359#define RTYPE_CCC 0x0010000
2360#define RTYPE_VI 0x0020000
2361#define RTYPE_VF 0x0040000
2362#define RTYPE_R5900_I 0x0080000
2363#define RTYPE_R5900_Q 0x0100000
2364#define RTYPE_R5900_R 0x0200000
2365#define RTYPE_R5900_ACC 0x0400000
2366#define RWARN 0x8000000
707bfff6
TS
2367
2368#define GENERIC_REGISTER_NUMBERS \
2369 {"$0", RTYPE_NUM | 0}, \
2370 {"$1", RTYPE_NUM | 1}, \
2371 {"$2", RTYPE_NUM | 2}, \
2372 {"$3", RTYPE_NUM | 3}, \
2373 {"$4", RTYPE_NUM | 4}, \
2374 {"$5", RTYPE_NUM | 5}, \
2375 {"$6", RTYPE_NUM | 6}, \
2376 {"$7", RTYPE_NUM | 7}, \
2377 {"$8", RTYPE_NUM | 8}, \
2378 {"$9", RTYPE_NUM | 9}, \
2379 {"$10", RTYPE_NUM | 10}, \
2380 {"$11", RTYPE_NUM | 11}, \
2381 {"$12", RTYPE_NUM | 12}, \
2382 {"$13", RTYPE_NUM | 13}, \
2383 {"$14", RTYPE_NUM | 14}, \
2384 {"$15", RTYPE_NUM | 15}, \
2385 {"$16", RTYPE_NUM | 16}, \
2386 {"$17", RTYPE_NUM | 17}, \
2387 {"$18", RTYPE_NUM | 18}, \
2388 {"$19", RTYPE_NUM | 19}, \
2389 {"$20", RTYPE_NUM | 20}, \
2390 {"$21", RTYPE_NUM | 21}, \
2391 {"$22", RTYPE_NUM | 22}, \
2392 {"$23", RTYPE_NUM | 23}, \
2393 {"$24", RTYPE_NUM | 24}, \
2394 {"$25", RTYPE_NUM | 25}, \
2395 {"$26", RTYPE_NUM | 26}, \
2396 {"$27", RTYPE_NUM | 27}, \
2397 {"$28", RTYPE_NUM | 28}, \
2398 {"$29", RTYPE_NUM | 29}, \
2399 {"$30", RTYPE_NUM | 30}, \
2400 {"$31", RTYPE_NUM | 31}
2401
2402#define FPU_REGISTER_NAMES \
2403 {"$f0", RTYPE_FPU | 0}, \
2404 {"$f1", RTYPE_FPU | 1}, \
2405 {"$f2", RTYPE_FPU | 2}, \
2406 {"$f3", RTYPE_FPU | 3}, \
2407 {"$f4", RTYPE_FPU | 4}, \
2408 {"$f5", RTYPE_FPU | 5}, \
2409 {"$f6", RTYPE_FPU | 6}, \
2410 {"$f7", RTYPE_FPU | 7}, \
2411 {"$f8", RTYPE_FPU | 8}, \
2412 {"$f9", RTYPE_FPU | 9}, \
2413 {"$f10", RTYPE_FPU | 10}, \
2414 {"$f11", RTYPE_FPU | 11}, \
2415 {"$f12", RTYPE_FPU | 12}, \
2416 {"$f13", RTYPE_FPU | 13}, \
2417 {"$f14", RTYPE_FPU | 14}, \
2418 {"$f15", RTYPE_FPU | 15}, \
2419 {"$f16", RTYPE_FPU | 16}, \
2420 {"$f17", RTYPE_FPU | 17}, \
2421 {"$f18", RTYPE_FPU | 18}, \
2422 {"$f19", RTYPE_FPU | 19}, \
2423 {"$f20", RTYPE_FPU | 20}, \
2424 {"$f21", RTYPE_FPU | 21}, \
2425 {"$f22", RTYPE_FPU | 22}, \
2426 {"$f23", RTYPE_FPU | 23}, \
2427 {"$f24", RTYPE_FPU | 24}, \
2428 {"$f25", RTYPE_FPU | 25}, \
2429 {"$f26", RTYPE_FPU | 26}, \
2430 {"$f27", RTYPE_FPU | 27}, \
2431 {"$f28", RTYPE_FPU | 28}, \
2432 {"$f29", RTYPE_FPU | 29}, \
2433 {"$f30", RTYPE_FPU | 30}, \
2434 {"$f31", RTYPE_FPU | 31}
2435
2436#define FPU_CONDITION_CODE_NAMES \
2437 {"$fcc0", RTYPE_FCC | 0}, \
2438 {"$fcc1", RTYPE_FCC | 1}, \
2439 {"$fcc2", RTYPE_FCC | 2}, \
2440 {"$fcc3", RTYPE_FCC | 3}, \
2441 {"$fcc4", RTYPE_FCC | 4}, \
2442 {"$fcc5", RTYPE_FCC | 5}, \
2443 {"$fcc6", RTYPE_FCC | 6}, \
2444 {"$fcc7", RTYPE_FCC | 7}
2445
2446#define COPROC_CONDITION_CODE_NAMES \
2447 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
2448 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
2449 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
2450 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
2451 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
2452 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
2453 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
2454 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
2455
2456#define N32N64_SYMBOLIC_REGISTER_NAMES \
2457 {"$a4", RTYPE_GP | 8}, \
2458 {"$a5", RTYPE_GP | 9}, \
2459 {"$a6", RTYPE_GP | 10}, \
2460 {"$a7", RTYPE_GP | 11}, \
2461 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
2462 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
2463 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
2464 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
2465 {"$t0", RTYPE_GP | 12}, \
2466 {"$t1", RTYPE_GP | 13}, \
2467 {"$t2", RTYPE_GP | 14}, \
2468 {"$t3", RTYPE_GP | 15}
2469
2470#define O32_SYMBOLIC_REGISTER_NAMES \
2471 {"$t0", RTYPE_GP | 8}, \
2472 {"$t1", RTYPE_GP | 9}, \
2473 {"$t2", RTYPE_GP | 10}, \
2474 {"$t3", RTYPE_GP | 11}, \
2475 {"$t4", RTYPE_GP | 12}, \
2476 {"$t5", RTYPE_GP | 13}, \
2477 {"$t6", RTYPE_GP | 14}, \
2478 {"$t7", RTYPE_GP | 15}, \
2479 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2480 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2481 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
2482 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
2483
2484/* Remaining symbolic register names */
2485#define SYMBOLIC_REGISTER_NAMES \
2486 {"$zero", RTYPE_GP | 0}, \
2487 {"$at", RTYPE_GP | 1}, \
2488 {"$AT", RTYPE_GP | 1}, \
2489 {"$v0", RTYPE_GP | 2}, \
2490 {"$v1", RTYPE_GP | 3}, \
2491 {"$a0", RTYPE_GP | 4}, \
2492 {"$a1", RTYPE_GP | 5}, \
2493 {"$a2", RTYPE_GP | 6}, \
2494 {"$a3", RTYPE_GP | 7}, \
2495 {"$s0", RTYPE_GP | 16}, \
2496 {"$s1", RTYPE_GP | 17}, \
2497 {"$s2", RTYPE_GP | 18}, \
2498 {"$s3", RTYPE_GP | 19}, \
2499 {"$s4", RTYPE_GP | 20}, \
2500 {"$s5", RTYPE_GP | 21}, \
2501 {"$s6", RTYPE_GP | 22}, \
2502 {"$s7", RTYPE_GP | 23}, \
2503 {"$t8", RTYPE_GP | 24}, \
2504 {"$t9", RTYPE_GP | 25}, \
2505 {"$k0", RTYPE_GP | 26}, \
2506 {"$kt0", RTYPE_GP | 26}, \
2507 {"$k1", RTYPE_GP | 27}, \
2508 {"$kt1", RTYPE_GP | 27}, \
2509 {"$gp", RTYPE_GP | 28}, \
2510 {"$sp", RTYPE_GP | 29}, \
2511 {"$s8", RTYPE_GP | 30}, \
2512 {"$fp", RTYPE_GP | 30}, \
2513 {"$ra", RTYPE_GP | 31}
2514
2515#define MIPS16_SPECIAL_REGISTER_NAMES \
2516 {"$pc", RTYPE_PC | 0}
2517
2518#define MDMX_VECTOR_REGISTER_NAMES \
2519 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
2520 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
2521 {"$v2", RTYPE_VEC | 2}, \
2522 {"$v3", RTYPE_VEC | 3}, \
2523 {"$v4", RTYPE_VEC | 4}, \
2524 {"$v5", RTYPE_VEC | 5}, \
2525 {"$v6", RTYPE_VEC | 6}, \
2526 {"$v7", RTYPE_VEC | 7}, \
2527 {"$v8", RTYPE_VEC | 8}, \
2528 {"$v9", RTYPE_VEC | 9}, \
2529 {"$v10", RTYPE_VEC | 10}, \
2530 {"$v11", RTYPE_VEC | 11}, \
2531 {"$v12", RTYPE_VEC | 12}, \
2532 {"$v13", RTYPE_VEC | 13}, \
2533 {"$v14", RTYPE_VEC | 14}, \
2534 {"$v15", RTYPE_VEC | 15}, \
2535 {"$v16", RTYPE_VEC | 16}, \
2536 {"$v17", RTYPE_VEC | 17}, \
2537 {"$v18", RTYPE_VEC | 18}, \
2538 {"$v19", RTYPE_VEC | 19}, \
2539 {"$v20", RTYPE_VEC | 20}, \
2540 {"$v21", RTYPE_VEC | 21}, \
2541 {"$v22", RTYPE_VEC | 22}, \
2542 {"$v23", RTYPE_VEC | 23}, \
2543 {"$v24", RTYPE_VEC | 24}, \
2544 {"$v25", RTYPE_VEC | 25}, \
2545 {"$v26", RTYPE_VEC | 26}, \
2546 {"$v27", RTYPE_VEC | 27}, \
2547 {"$v28", RTYPE_VEC | 28}, \
2548 {"$v29", RTYPE_VEC | 29}, \
2549 {"$v30", RTYPE_VEC | 30}, \
2550 {"$v31", RTYPE_VEC | 31}
2551
14daeee3
RS
2552#define R5900_I_NAMES \
2553 {"$I", RTYPE_R5900_I | 0}
2554
2555#define R5900_Q_NAMES \
2556 {"$Q", RTYPE_R5900_Q | 0}
2557
2558#define R5900_R_NAMES \
2559 {"$R", RTYPE_R5900_R | 0}
2560
2561#define R5900_ACC_NAMES \
2562 {"$ACC", RTYPE_R5900_ACC | 0 }
2563
707bfff6
TS
2564#define MIPS_DSP_ACCUMULATOR_NAMES \
2565 {"$ac0", RTYPE_ACC | 0}, \
2566 {"$ac1", RTYPE_ACC | 1}, \
2567 {"$ac2", RTYPE_ACC | 2}, \
2568 {"$ac3", RTYPE_ACC | 3}
2569
2570static const struct regname reg_names[] = {
2571 GENERIC_REGISTER_NUMBERS,
2572 FPU_REGISTER_NAMES,
2573 FPU_CONDITION_CODE_NAMES,
2574 COPROC_CONDITION_CODE_NAMES,
2575
2576 /* The $txx registers depends on the abi,
2577 these will be added later into the symbol table from
2578 one of the tables below once mips_abi is set after
2579 parsing of arguments from the command line. */
2580 SYMBOLIC_REGISTER_NAMES,
2581
2582 MIPS16_SPECIAL_REGISTER_NAMES,
2583 MDMX_VECTOR_REGISTER_NAMES,
14daeee3
RS
2584 R5900_I_NAMES,
2585 R5900_Q_NAMES,
2586 R5900_R_NAMES,
2587 R5900_ACC_NAMES,
707bfff6
TS
2588 MIPS_DSP_ACCUMULATOR_NAMES,
2589 {0, 0}
2590};
2591
2592static const struct regname reg_names_o32[] = {
2593 O32_SYMBOLIC_REGISTER_NAMES,
2594 {0, 0}
2595};
2596
2597static const struct regname reg_names_n32n64[] = {
2598 N32N64_SYMBOLIC_REGISTER_NAMES,
2599 {0, 0}
2600};
2601
a92713e6
RS
2602/* Register symbols $v0 and $v1 map to GPRs 2 and 3, but they can also be
2603 interpreted as vector registers 0 and 1. If SYMVAL is the value of one
2604 of these register symbols, return the associated vector register,
2605 otherwise return SYMVAL itself. */
df58fc94 2606
a92713e6
RS
2607static unsigned int
2608mips_prefer_vec_regno (unsigned int symval)
707bfff6 2609{
a92713e6
RS
2610 if ((symval & -2) == (RTYPE_GP | 2))
2611 return RTYPE_VEC | (symval & 1);
2612 return symval;
2613}
2614
14daeee3
RS
2615/* Return true if string [S, E) is a valid register name, storing its
2616 symbol value in *SYMVAL_PTR if so. */
a92713e6
RS
2617
2618static bfd_boolean
14daeee3 2619mips_parse_register_1 (char *s, char *e, unsigned int *symval_ptr)
a92713e6 2620{
707bfff6 2621 char save_c;
14daeee3 2622 symbolS *symbol;
707bfff6
TS
2623
2624 /* Terminate name. */
2625 save_c = *e;
2626 *e = '\0';
2627
a92713e6
RS
2628 /* Look up the name. */
2629 symbol = symbol_find (s);
2630 *e = save_c;
2631
2632 if (!symbol || S_GET_SEGMENT (symbol) != reg_section)
2633 return FALSE;
2634
14daeee3
RS
2635 *symval_ptr = S_GET_VALUE (symbol);
2636 return TRUE;
2637}
2638
2639/* Return true if the string at *SPTR is a valid register name. Allow it
2640 to have a VU0-style channel suffix of the form x?y?z?w? if CHANNELS_PTR
2641 is nonnull.
2642
2643 When returning true, move *SPTR past the register, store the
2644 register's symbol value in *SYMVAL_PTR and the channel mask in
2645 *CHANNELS_PTR (if nonnull). The symbol value includes the register
2646 number (RNUM_MASK) and register type (RTYPE_MASK). The channel mask
2647 is a 4-bit value of the form XYZW and is 0 if no suffix was given. */
2648
2649static bfd_boolean
2650mips_parse_register (char **sptr, unsigned int *symval_ptr,
2651 unsigned int *channels_ptr)
2652{
2653 char *s, *e, *m;
2654 const char *q;
2655 unsigned int channels, symval, bit;
2656
2657 /* Find end of name. */
2658 s = e = *sptr;
2659 if (is_name_beginner (*e))
2660 ++e;
2661 while (is_part_of_name (*e))
2662 ++e;
2663
2664 channels = 0;
2665 if (!mips_parse_register_1 (s, e, &symval))
2666 {
2667 if (!channels_ptr)
2668 return FALSE;
2669
2670 /* Eat characters from the end of the string that are valid
2671 channel suffixes. The preceding register must be $ACC or
2672 end with a digit, so there is no ambiguity. */
2673 bit = 1;
2674 m = e;
2675 for (q = "wzyx"; *q; q++, bit <<= 1)
2676 if (m > s && m[-1] == *q)
2677 {
2678 --m;
2679 channels |= bit;
2680 }
2681
2682 if (channels == 0
2683 || !mips_parse_register_1 (s, m, &symval)
2684 || (symval & (RTYPE_VI | RTYPE_VF | RTYPE_R5900_ACC)) == 0)
2685 return FALSE;
2686 }
2687
a92713e6 2688 *sptr = e;
14daeee3
RS
2689 *symval_ptr = symval;
2690 if (channels_ptr)
2691 *channels_ptr = channels;
a92713e6
RS
2692 return TRUE;
2693}
2694
2695/* Check if SPTR points at a valid register specifier according to TYPES.
2696 If so, then return 1, advance S to consume the specifier and store
2697 the register's number in REGNOP, otherwise return 0. */
2698
2699static int
2700reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2701{
2702 unsigned int regno;
2703
14daeee3 2704 if (mips_parse_register (s, &regno, NULL))
707bfff6 2705 {
a92713e6
RS
2706 if (types & RTYPE_VEC)
2707 regno = mips_prefer_vec_regno (regno);
2708 if (regno & types)
2709 regno &= RNUM_MASK;
2710 else
2711 regno = ~0;
707bfff6 2712 }
a92713e6 2713 else
707bfff6 2714 {
a92713e6
RS
2715 if (types & RWARN)
2716 as_warn (_("Unrecognized register name `%s'"), *s);
2717 regno = ~0;
707bfff6 2718 }
707bfff6 2719 if (regnop)
a92713e6
RS
2720 *regnop = regno;
2721 return regno <= RNUM_MASK;
707bfff6
TS
2722}
2723
14daeee3
RS
2724/* Parse a VU0 "x?y?z?w?" channel mask at S and store the associated
2725 mask in *CHANNELS. Return a pointer to the first unconsumed character. */
2726
2727static char *
2728mips_parse_vu0_channels (char *s, unsigned int *channels)
2729{
2730 unsigned int i;
2731
2732 *channels = 0;
2733 for (i = 0; i < 4; i++)
2734 if (*s == "xyzw"[i])
2735 {
2736 *channels |= 1 << (3 - i);
2737 ++s;
2738 }
2739 return s;
2740}
2741
a92713e6
RS
2742/* Token types for parsed operand lists. */
2743enum mips_operand_token_type {
2744 /* A plain register, e.g. $f2. */
2745 OT_REG,
df58fc94 2746
14daeee3
RS
2747 /* A 4-bit XYZW channel mask. */
2748 OT_CHANNELS,
2749
a92713e6
RS
2750 /* An element of a vector, e.g. $v0[1]. */
2751 OT_REG_ELEMENT,
df58fc94 2752
a92713e6
RS
2753 /* A continuous range of registers, e.g. $s0-$s4. */
2754 OT_REG_RANGE,
2755
2756 /* A (possibly relocated) expression. */
2757 OT_INTEGER,
2758
2759 /* A floating-point value. */
2760 OT_FLOAT,
2761
2762 /* A single character. This can be '(', ')' or ',', but '(' only appears
2763 before OT_REGs. */
2764 OT_CHAR,
2765
14daeee3
RS
2766 /* A doubled character, either "--" or "++". */
2767 OT_DOUBLE_CHAR,
2768
a92713e6
RS
2769 /* The end of the operand list. */
2770 OT_END
2771};
2772
2773/* A parsed operand token. */
2774struct mips_operand_token
2775{
2776 /* The type of token. */
2777 enum mips_operand_token_type type;
2778 union
2779 {
2780 /* The register symbol value for an OT_REG. */
2781 unsigned int regno;
2782
14daeee3
RS
2783 /* The 4-bit channel mask for an OT_CHANNEL_SUFFIX. */
2784 unsigned int channels;
2785
a92713e6
RS
2786 /* The register symbol value and index for an OT_REG_ELEMENT. */
2787 struct {
2788 unsigned int regno;
2789 addressT index;
2790 } reg_element;
2791
2792 /* The two register symbol values involved in an OT_REG_RANGE. */
2793 struct {
2794 unsigned int regno1;
2795 unsigned int regno2;
2796 } reg_range;
2797
2798 /* The value of an OT_INTEGER. The value is represented as an
2799 expression and the relocation operators that were applied to
2800 that expression. The reloc entries are BFD_RELOC_UNUSED if no
2801 relocation operators were used. */
2802 struct {
2803 expressionS value;
2804 bfd_reloc_code_real_type relocs[3];
2805 } integer;
2806
2807 /* The binary data for an OT_FLOAT constant, and the number of bytes
2808 in the constant. */
2809 struct {
2810 unsigned char data[8];
2811 int length;
2812 } flt;
2813
14daeee3 2814 /* The character represented by an OT_CHAR or OT_DOUBLE_CHAR. */
a92713e6
RS
2815 char ch;
2816 } u;
2817};
2818
2819/* An obstack used to construct lists of mips_operand_tokens. */
2820static struct obstack mips_operand_tokens;
2821
2822/* Give TOKEN type TYPE and add it to mips_operand_tokens. */
2823
2824static void
2825mips_add_token (struct mips_operand_token *token,
2826 enum mips_operand_token_type type)
2827{
2828 token->type = type;
2829 obstack_grow (&mips_operand_tokens, token, sizeof (*token));
2830}
2831
2832/* Check whether S is '(' followed by a register name. Add OT_CHAR
2833 and OT_REG tokens for them if so, and return a pointer to the first
2834 unconsumed character. Return null otherwise. */
2835
2836static char *
2837mips_parse_base_start (char *s)
2838{
2839 struct mips_operand_token token;
14daeee3
RS
2840 unsigned int regno, channels;
2841 bfd_boolean decrement_p;
df58fc94 2842
a92713e6
RS
2843 if (*s != '(')
2844 return 0;
2845
2846 ++s;
2847 SKIP_SPACE_TABS (s);
14daeee3
RS
2848
2849 /* Only match "--" as part of a base expression. In other contexts "--X"
2850 is a double negative. */
2851 decrement_p = (s[0] == '-' && s[1] == '-');
2852 if (decrement_p)
2853 {
2854 s += 2;
2855 SKIP_SPACE_TABS (s);
2856 }
2857
2858 /* Allow a channel specifier because that leads to better error messages
2859 than treating something like "$vf0x++" as an expression. */
2860 if (!mips_parse_register (&s, &regno, &channels))
a92713e6
RS
2861 return 0;
2862
2863 token.u.ch = '(';
2864 mips_add_token (&token, OT_CHAR);
2865
14daeee3
RS
2866 if (decrement_p)
2867 {
2868 token.u.ch = '-';
2869 mips_add_token (&token, OT_DOUBLE_CHAR);
2870 }
2871
a92713e6
RS
2872 token.u.regno = regno;
2873 mips_add_token (&token, OT_REG);
2874
14daeee3
RS
2875 if (channels)
2876 {
2877 token.u.channels = channels;
2878 mips_add_token (&token, OT_CHANNELS);
2879 }
2880
2881 /* For consistency, only match "++" as part of base expressions too. */
2882 SKIP_SPACE_TABS (s);
2883 if (s[0] == '+' && s[1] == '+')
2884 {
2885 s += 2;
2886 token.u.ch = '+';
2887 mips_add_token (&token, OT_DOUBLE_CHAR);
2888 }
2889
a92713e6
RS
2890 return s;
2891}
2892
2893/* Parse one or more tokens from S. Return a pointer to the first
2894 unconsumed character on success. Return null if an error was found
2895 and store the error text in insn_error. FLOAT_FORMAT is as for
2896 mips_parse_arguments. */
2897
2898static char *
2899mips_parse_argument_token (char *s, char float_format)
2900{
2901 char *end, *save_in, *err;
14daeee3 2902 unsigned int regno1, regno2, channels;
a92713e6
RS
2903 struct mips_operand_token token;
2904
2905 /* First look for "($reg", since we want to treat that as an
2906 OT_CHAR and OT_REG rather than an expression. */
2907 end = mips_parse_base_start (s);
2908 if (end)
2909 return end;
2910
2911 /* Handle other characters that end up as OT_CHARs. */
2912 if (*s == ')' || *s == ',')
2913 {
2914 token.u.ch = *s;
2915 mips_add_token (&token, OT_CHAR);
2916 ++s;
2917 return s;
2918 }
2919
2920 /* Handle tokens that start with a register. */
14daeee3 2921 if (mips_parse_register (&s, &regno1, &channels))
df58fc94 2922 {
14daeee3
RS
2923 if (channels)
2924 {
2925 /* A register and a VU0 channel suffix. */
2926 token.u.regno = regno1;
2927 mips_add_token (&token, OT_REG);
2928
2929 token.u.channels = channels;
2930 mips_add_token (&token, OT_CHANNELS);
2931 return s;
2932 }
2933
a92713e6
RS
2934 SKIP_SPACE_TABS (s);
2935 if (*s == '-')
df58fc94 2936 {
a92713e6
RS
2937 /* A register range. */
2938 ++s;
2939 SKIP_SPACE_TABS (s);
14daeee3 2940 if (!mips_parse_register (&s, &regno2, NULL))
a92713e6 2941 {
e3de51ce 2942 set_insn_error (0, _("Invalid register range"));
a92713e6
RS
2943 return 0;
2944 }
df58fc94 2945
a92713e6
RS
2946 token.u.reg_range.regno1 = regno1;
2947 token.u.reg_range.regno2 = regno2;
2948 mips_add_token (&token, OT_REG_RANGE);
2949 return s;
2950 }
2951 else if (*s == '[')
df58fc94 2952 {
a92713e6
RS
2953 /* A vector element. */
2954 expressionS element;
2955
2956 ++s;
2957 SKIP_SPACE_TABS (s);
2958 my_getExpression (&element, s);
2959 if (element.X_op != O_constant)
2960 {
e3de51ce 2961 set_insn_error (0, _("Vector element must be constant"));
a92713e6
RS
2962 return 0;
2963 }
2964 s = expr_end;
2965 SKIP_SPACE_TABS (s);
2966 if (*s != ']')
2967 {
e3de51ce 2968 set_insn_error (0, _("Missing `]'"));
a92713e6
RS
2969 return 0;
2970 }
2971 ++s;
2972
2973 token.u.reg_element.regno = regno1;
2974 token.u.reg_element.index = element.X_add_number;
2975 mips_add_token (&token, OT_REG_ELEMENT);
2976 return s;
df58fc94 2977 }
df58fc94 2978
a92713e6
RS
2979 /* Looks like just a plain register. */
2980 token.u.regno = regno1;
2981 mips_add_token (&token, OT_REG);
2982 return s;
df58fc94
RS
2983 }
2984
a92713e6
RS
2985 if (float_format)
2986 {
2987 /* First try to treat expressions as floats. */
2988 save_in = input_line_pointer;
2989 input_line_pointer = s;
2990 err = md_atof (float_format, (char *) token.u.flt.data,
2991 &token.u.flt.length);
2992 end = input_line_pointer;
2993 input_line_pointer = save_in;
2994 if (err && *err)
2995 {
e3de51ce 2996 set_insn_error (0, err);
a92713e6
RS
2997 return 0;
2998 }
2999 if (s != end)
3000 {
3001 mips_add_token (&token, OT_FLOAT);
3002 return end;
3003 }
3004 }
3005
3006 /* Treat everything else as an integer expression. */
3007 token.u.integer.relocs[0] = BFD_RELOC_UNUSED;
3008 token.u.integer.relocs[1] = BFD_RELOC_UNUSED;
3009 token.u.integer.relocs[2] = BFD_RELOC_UNUSED;
3010 my_getSmallExpression (&token.u.integer.value, token.u.integer.relocs, s);
3011 s = expr_end;
3012 mips_add_token (&token, OT_INTEGER);
3013 return s;
3014}
3015
3016/* S points to the operand list for an instruction. FLOAT_FORMAT is 'f'
3017 if expressions should be treated as 32-bit floating-point constants,
3018 'd' if they should be treated as 64-bit floating-point constants,
3019 or 0 if they should be treated as integer expressions (the usual case).
3020
3021 Return a list of tokens on success, otherwise return 0. The caller
3022 must obstack_free the list after use. */
3023
3024static struct mips_operand_token *
3025mips_parse_arguments (char *s, char float_format)
3026{
3027 struct mips_operand_token token;
3028
3029 SKIP_SPACE_TABS (s);
3030 while (*s)
3031 {
3032 s = mips_parse_argument_token (s, float_format);
3033 if (!s)
3034 {
3035 obstack_free (&mips_operand_tokens,
3036 obstack_finish (&mips_operand_tokens));
3037 return 0;
3038 }
3039 SKIP_SPACE_TABS (s);
3040 }
3041 mips_add_token (&token, OT_END);
3042 return (struct mips_operand_token *) obstack_finish (&mips_operand_tokens);
df58fc94
RS
3043}
3044
d301a56b
RS
3045/* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
3046 and architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
037b32b9
AN
3047
3048static bfd_boolean
f79e2745 3049is_opcode_valid (const struct mips_opcode *mo)
037b32b9
AN
3050{
3051 int isa = mips_opts.isa;
846ef2d0 3052 int ase = mips_opts.ase;
037b32b9 3053 int fp_s, fp_d;
c6278170 3054 unsigned int i;
037b32b9 3055
c6278170
RS
3056 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
3057 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3058 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3059 ase |= mips_ases[i].flags64;
037b32b9 3060
d301a56b 3061 if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
037b32b9
AN
3062 return FALSE;
3063
3064 /* Check whether the instruction or macro requires single-precision or
3065 double-precision floating-point support. Note that this information is
3066 stored differently in the opcode table for insns and macros. */
3067 if (mo->pinfo == INSN_MACRO)
3068 {
3069 fp_s = mo->pinfo2 & INSN2_M_FP_S;
3070 fp_d = mo->pinfo2 & INSN2_M_FP_D;
3071 }
3072 else
3073 {
3074 fp_s = mo->pinfo & FP_S;
3075 fp_d = mo->pinfo & FP_D;
3076 }
3077
3078 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
3079 return FALSE;
3080
3081 if (fp_s && mips_opts.soft_float)
3082 return FALSE;
3083
3084 return TRUE;
3085}
3086
3087/* Return TRUE if the MIPS16 opcode MO is valid on the currently
3088 selected ISA and architecture. */
3089
3090static bfd_boolean
3091is_opcode_valid_16 (const struct mips_opcode *mo)
3092{
d301a56b 3093 return opcode_is_member (mo, mips_opts.isa, 0, mips_opts.arch);
037b32b9
AN
3094}
3095
df58fc94
RS
3096/* Return TRUE if the size of the microMIPS opcode MO matches one
3097 explicitly requested. Always TRUE in the standard MIPS mode. */
3098
3099static bfd_boolean
3100is_size_valid (const struct mips_opcode *mo)
3101{
3102 if (!mips_opts.micromips)
3103 return TRUE;
3104
833794fc
MR
3105 if (mips_opts.insn32)
3106 {
3107 if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
3108 return FALSE;
3109 if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
3110 return FALSE;
3111 }
df58fc94
RS
3112 if (!forced_insn_length)
3113 return TRUE;
3114 if (mo->pinfo == INSN_MACRO)
3115 return FALSE;
3116 return forced_insn_length == micromips_insn_length (mo);
3117}
3118
3119/* Return TRUE if the microMIPS opcode MO is valid for the delay slot
e64af278
MR
3120 of the preceding instruction. Always TRUE in the standard MIPS mode.
3121
3122 We don't accept macros in 16-bit delay slots to avoid a case where
3123 a macro expansion fails because it relies on a preceding 32-bit real
3124 instruction to have matched and does not handle the operands correctly.
3125 The only macros that may expand to 16-bit instructions are JAL that
3126 cannot be placed in a delay slot anyway, and corner cases of BALIGN
3127 and BGT (that likewise cannot be placed in a delay slot) that decay to
3128 a NOP. In all these cases the macros precede any corresponding real
3129 instruction definitions in the opcode table, so they will match in the
3130 second pass where the size of the delay slot is ignored and therefore
3131 produce correct code. */
df58fc94
RS
3132
3133static bfd_boolean
3134is_delay_slot_valid (const struct mips_opcode *mo)
3135{
3136 if (!mips_opts.micromips)
3137 return TRUE;
3138
3139 if (mo->pinfo == INSN_MACRO)
c06dec14 3140 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
df58fc94
RS
3141 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
3142 && micromips_insn_length (mo) != 4)
3143 return FALSE;
3144 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
3145 && micromips_insn_length (mo) != 2)
3146 return FALSE;
3147
3148 return TRUE;
3149}
3150
fc76e730
RS
3151/* For consistency checking, verify that all bits of OPCODE are specified
3152 either by the match/mask part of the instruction definition, or by the
3153 operand list. Also build up a list of operands in OPERANDS.
3154
3155 INSN_BITS says which bits of the instruction are significant.
3156 If OPCODE is a standard or microMIPS instruction, DECODE_OPERAND
3157 provides the mips_operand description of each operand. DECODE_OPERAND
3158 is null for MIPS16 instructions. */
ab902481
RS
3159
3160static int
3161validate_mips_insn (const struct mips_opcode *opcode,
3162 unsigned long insn_bits,
fc76e730
RS
3163 const struct mips_operand *(*decode_operand) (const char *),
3164 struct mips_operand_array *operands)
ab902481
RS
3165{
3166 const char *s;
fc76e730 3167 unsigned long used_bits, doubled, undefined, opno, mask;
ab902481
RS
3168 const struct mips_operand *operand;
3169
fc76e730
RS
3170 mask = (opcode->pinfo == INSN_MACRO ? 0 : opcode->mask);
3171 if ((mask & opcode->match) != opcode->match)
ab902481
RS
3172 {
3173 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
3174 opcode->name, opcode->args);
3175 return 0;
3176 }
3177 used_bits = 0;
fc76e730 3178 opno = 0;
14daeee3
RS
3179 if (opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX)
3180 used_bits = mips_insert_operand (&mips_vu0_channel_mask, used_bits, -1);
ab902481
RS
3181 for (s = opcode->args; *s; ++s)
3182 switch (*s)
3183 {
3184 case ',':
3185 case '(':
3186 case ')':
3187 break;
3188
14daeee3
RS
3189 case '#':
3190 s++;
3191 break;
3192
ab902481 3193 default:
fc76e730
RS
3194 if (!decode_operand)
3195 operand = decode_mips16_operand (*s, FALSE);
3196 else
3197 operand = decode_operand (s);
3198 if (!operand && opcode->pinfo != INSN_MACRO)
ab902481
RS
3199 {
3200 as_bad (_("internal: unknown operand type: %s %s"),
3201 opcode->name, opcode->args);
3202 return 0;
3203 }
fc76e730
RS
3204 gas_assert (opno < MAX_OPERANDS);
3205 operands->operand[opno] = operand;
14daeee3 3206 if (operand && operand->type != OP_VU0_MATCH_SUFFIX)
fc76e730 3207 {
14daeee3 3208 used_bits = mips_insert_operand (operand, used_bits, -1);
fc76e730
RS
3209 if (operand->type == OP_MDMX_IMM_REG)
3210 /* Bit 5 is the format selector (OB vs QH). The opcode table
3211 has separate entries for each format. */
3212 used_bits &= ~(1 << (operand->lsb + 5));
3213 if (operand->type == OP_ENTRY_EXIT_LIST)
3214 used_bits &= ~(mask & 0x700);
3215 }
ab902481 3216 /* Skip prefix characters. */
fc76e730 3217 if (decode_operand && (*s == '+' || *s == 'm'))
ab902481 3218 ++s;
fc76e730 3219 opno += 1;
ab902481
RS
3220 break;
3221 }
fc76e730 3222 doubled = used_bits & mask & insn_bits;
ab902481
RS
3223 if (doubled)
3224 {
3225 as_bad (_("internal: bad mips opcode (bits 0x%08lx doubly defined):"
3226 " %s %s"), doubled, opcode->name, opcode->args);
3227 return 0;
3228 }
fc76e730 3229 used_bits |= mask;
ab902481 3230 undefined = ~used_bits & insn_bits;
fc76e730 3231 if (opcode->pinfo != INSN_MACRO && undefined)
ab902481
RS
3232 {
3233 as_bad (_("internal: bad mips opcode (bits 0x%08lx undefined): %s %s"),
3234 undefined, opcode->name, opcode->args);
3235 return 0;
3236 }
3237 used_bits &= ~insn_bits;
3238 if (used_bits)
3239 {
3240 as_bad (_("internal: bad mips opcode (bits 0x%08lx defined): %s %s"),
3241 used_bits, opcode->name, opcode->args);
3242 return 0;
3243 }
3244 return 1;
3245}
3246
fc76e730
RS
3247/* The MIPS16 version of validate_mips_insn. */
3248
3249static int
3250validate_mips16_insn (const struct mips_opcode *opcode,
3251 struct mips_operand_array *operands)
3252{
3253 if (opcode->args[0] == 'a' || opcode->args[0] == 'i')
3254 {
3255 /* In this case OPCODE defines the first 16 bits in a 32-bit jump
3256 instruction. Use TMP to describe the full instruction. */
3257 struct mips_opcode tmp;
3258
3259 tmp = *opcode;
3260 tmp.match <<= 16;
3261 tmp.mask <<= 16;
3262 return validate_mips_insn (&tmp, 0xffffffff, 0, operands);
3263 }
3264 return validate_mips_insn (opcode, 0xffff, 0, operands);
3265}
3266
ab902481
RS
3267/* The microMIPS version of validate_mips_insn. */
3268
3269static int
fc76e730
RS
3270validate_micromips_insn (const struct mips_opcode *opc,
3271 struct mips_operand_array *operands)
ab902481
RS
3272{
3273 unsigned long insn_bits;
3274 unsigned long major;
3275 unsigned int length;
3276
fc76e730
RS
3277 if (opc->pinfo == INSN_MACRO)
3278 return validate_mips_insn (opc, 0xffffffff, decode_micromips_operand,
3279 operands);
3280
ab902481
RS
3281 length = micromips_insn_length (opc);
3282 if (length != 2 && length != 4)
3283 {
3284 as_bad (_("Internal error: bad microMIPS opcode (incorrect length: %u): "
3285 "%s %s"), length, opc->name, opc->args);
3286 return 0;
3287 }
3288 major = opc->match >> (10 + 8 * (length - 2));
3289 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
3290 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
3291 {
3292 as_bad (_("Internal error: bad microMIPS opcode "
3293 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
3294 return 0;
3295 }
3296
3297 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
3298 insn_bits = 1 << 4 * length;
3299 insn_bits <<= 4 * length;
3300 insn_bits -= 1;
fc76e730
RS
3301 return validate_mips_insn (opc, insn_bits, decode_micromips_operand,
3302 operands);
ab902481
RS
3303}
3304
707bfff6
TS
3305/* This function is called once, at assembler startup time. It should set up
3306 all the tables, etc. that the MD part of the assembler will need. */
156c2f8b 3307
252b5132 3308void
17a2f251 3309md_begin (void)
252b5132 3310{
3994f87e 3311 const char *retval = NULL;
156c2f8b 3312 int i = 0;
252b5132 3313 int broken = 0;
1f25f5d3 3314
0a44bf69
RS
3315 if (mips_pic != NO_PIC)
3316 {
3317 if (g_switch_seen && g_switch_value != 0)
3318 as_bad (_("-G may not be used in position-independent code"));
3319 g_switch_value = 0;
3320 }
3321
fef14a42 3322 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
252b5132
RH
3323 as_warn (_("Could not set architecture and machine"));
3324
252b5132
RH
3325 op_hash = hash_new ();
3326
fc76e730 3327 mips_operands = XCNEWVEC (struct mips_operand_array, NUMOPCODES);
252b5132
RH
3328 for (i = 0; i < NUMOPCODES;)
3329 {
3330 const char *name = mips_opcodes[i].name;
3331
17a2f251 3332 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
3333 if (retval != NULL)
3334 {
3335 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
3336 mips_opcodes[i].name, retval);
3337 /* Probably a memory allocation problem? Give up now. */
3338 as_fatal (_("Broken assembler. No assembly attempted."));
3339 }
3340 do
3341 {
fc76e730
RS
3342 if (!validate_mips_insn (&mips_opcodes[i], 0xffffffff,
3343 decode_mips_operand, &mips_operands[i]))
3344 broken = 1;
3345 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
252b5132 3346 {
fc76e730
RS
3347 create_insn (&nop_insn, mips_opcodes + i);
3348 if (mips_fix_loongson2f_nop)
3349 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
3350 nop_insn.fixed_p = 1;
252b5132
RH
3351 }
3352 ++i;
3353 }
3354 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
3355 }
3356
3357 mips16_op_hash = hash_new ();
fc76e730
RS
3358 mips16_operands = XCNEWVEC (struct mips_operand_array,
3359 bfd_mips16_num_opcodes);
252b5132
RH
3360
3361 i = 0;
3362 while (i < bfd_mips16_num_opcodes)
3363 {
3364 const char *name = mips16_opcodes[i].name;
3365
17a2f251 3366 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
3367 if (retval != NULL)
3368 as_fatal (_("internal: can't hash `%s': %s"),
3369 mips16_opcodes[i].name, retval);
3370 do
3371 {
fc76e730
RS
3372 if (!validate_mips16_insn (&mips16_opcodes[i], &mips16_operands[i]))
3373 broken = 1;
1e915849
RS
3374 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3375 {
3376 create_insn (&mips16_nop_insn, mips16_opcodes + i);
3377 mips16_nop_insn.fixed_p = 1;
3378 }
252b5132
RH
3379 ++i;
3380 }
3381 while (i < bfd_mips16_num_opcodes
3382 && strcmp (mips16_opcodes[i].name, name) == 0);
3383 }
3384
df58fc94 3385 micromips_op_hash = hash_new ();
fc76e730
RS
3386 micromips_operands = XCNEWVEC (struct mips_operand_array,
3387 bfd_micromips_num_opcodes);
df58fc94
RS
3388
3389 i = 0;
3390 while (i < bfd_micromips_num_opcodes)
3391 {
3392 const char *name = micromips_opcodes[i].name;
3393
3394 retval = hash_insert (micromips_op_hash, name,
3395 (void *) &micromips_opcodes[i]);
3396 if (retval != NULL)
3397 as_fatal (_("internal: can't hash `%s': %s"),
3398 micromips_opcodes[i].name, retval);
3399 do
fc76e730
RS
3400 {
3401 struct mips_cl_insn *micromips_nop_insn;
3402
3403 if (!validate_micromips_insn (&micromips_opcodes[i],
3404 &micromips_operands[i]))
3405 broken = 1;
3406
3407 if (micromips_opcodes[i].pinfo != INSN_MACRO)
3408 {
3409 if (micromips_insn_length (micromips_opcodes + i) == 2)
3410 micromips_nop_insn = &micromips_nop16_insn;
3411 else if (micromips_insn_length (micromips_opcodes + i) == 4)
3412 micromips_nop_insn = &micromips_nop32_insn;
3413 else
3414 continue;
3415
3416 if (micromips_nop_insn->insn_mo == NULL
3417 && strcmp (name, "nop") == 0)
3418 {
3419 create_insn (micromips_nop_insn, micromips_opcodes + i);
3420 micromips_nop_insn->fixed_p = 1;
3421 }
3422 }
3423 }
df58fc94
RS
3424 while (++i < bfd_micromips_num_opcodes
3425 && strcmp (micromips_opcodes[i].name, name) == 0);
3426 }
3427
252b5132
RH
3428 if (broken)
3429 as_fatal (_("Broken assembler. No assembly attempted."));
3430
3431 /* We add all the general register names to the symbol table. This
3432 helps us detect invalid uses of them. */
707bfff6
TS
3433 for (i = 0; reg_names[i].name; i++)
3434 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
8fc4ee9b 3435 reg_names[i].num, /* & RNUM_MASK, */
707bfff6
TS
3436 &zero_address_frag));
3437 if (HAVE_NEWABI)
3438 for (i = 0; reg_names_n32n64[i].name; i++)
3439 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
8fc4ee9b 3440 reg_names_n32n64[i].num, /* & RNUM_MASK, */
252b5132 3441 &zero_address_frag));
707bfff6
TS
3442 else
3443 for (i = 0; reg_names_o32[i].name; i++)
3444 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
8fc4ee9b 3445 reg_names_o32[i].num, /* & RNUM_MASK, */
6047c971 3446 &zero_address_frag));
6047c971 3447
14daeee3
RS
3448 for (i = 0; i < 32; i++)
3449 {
3450 char regname[7];
3451
3452 /* R5900 VU0 floating-point register. */
3453 regname[sizeof (rename) - 1] = 0;
3454 snprintf (regname, sizeof (regname) - 1, "$vf%d", i);
3455 symbol_table_insert (symbol_new (regname, reg_section,
3456 RTYPE_VF | i, &zero_address_frag));
3457
3458 /* R5900 VU0 integer register. */
3459 snprintf (regname, sizeof (regname) - 1, "$vi%d", i);
3460 symbol_table_insert (symbol_new (regname, reg_section,
3461 RTYPE_VI | i, &zero_address_frag));
3462
3463 }
3464
a92713e6
RS
3465 obstack_init (&mips_operand_tokens);
3466
7d10b47d 3467 mips_no_prev_insn ();
252b5132
RH
3468
3469 mips_gprmask = 0;
3470 mips_cprmask[0] = 0;
3471 mips_cprmask[1] = 0;
3472 mips_cprmask[2] = 0;
3473 mips_cprmask[3] = 0;
3474
3475 /* set the default alignment for the text section (2**2) */
3476 record_alignment (text_section, 2);
3477
4d0d148d 3478 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132 3479
f3ded42a
RS
3480 /* On a native system other than VxWorks, sections must be aligned
3481 to 16 byte boundaries. When configured for an embedded ELF
3482 target, we don't bother. */
3483 if (strncmp (TARGET_OS, "elf", 3) != 0
3484 && strncmp (TARGET_OS, "vxworks", 7) != 0)
252b5132 3485 {
f3ded42a
RS
3486 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
3487 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
3488 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
3489 }
252b5132 3490
f3ded42a
RS
3491 /* Create a .reginfo section for register masks and a .mdebug
3492 section for debugging information. */
3493 {
3494 segT seg;
3495 subsegT subseg;
3496 flagword flags;
3497 segT sec;
3498
3499 seg = now_seg;
3500 subseg = now_subseg;
3501
3502 /* The ABI says this section should be loaded so that the
3503 running program can access it. However, we don't load it
3504 if we are configured for an embedded target */
3505 flags = SEC_READONLY | SEC_DATA;
3506 if (strncmp (TARGET_OS, "elf", 3) != 0)
3507 flags |= SEC_ALLOC | SEC_LOAD;
3508
3509 if (mips_abi != N64_ABI)
252b5132 3510 {
f3ded42a 3511 sec = subseg_new (".reginfo", (subsegT) 0);
bdaaa2e1 3512
f3ded42a
RS
3513 bfd_set_section_flags (stdoutput, sec, flags);
3514 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
252b5132 3515
f3ded42a
RS
3516 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
3517 }
3518 else
3519 {
3520 /* The 64-bit ABI uses a .MIPS.options section rather than
3521 .reginfo section. */
3522 sec = subseg_new (".MIPS.options", (subsegT) 0);
3523 bfd_set_section_flags (stdoutput, sec, flags);
3524 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132 3525
f3ded42a
RS
3526 /* Set up the option header. */
3527 {
3528 Elf_Internal_Options opthdr;
3529 char *f;
3530
3531 opthdr.kind = ODK_REGINFO;
3532 opthdr.size = (sizeof (Elf_External_Options)
3533 + sizeof (Elf64_External_RegInfo));
3534 opthdr.section = 0;
3535 opthdr.info = 0;
3536 f = frag_more (sizeof (Elf_External_Options));
3537 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
3538 (Elf_External_Options *) f);
3539
3540 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
3541 }
3542 }
252b5132 3543
f3ded42a
RS
3544 if (ECOFF_DEBUGGING)
3545 {
3546 sec = subseg_new (".mdebug", (subsegT) 0);
3547 (void) bfd_set_section_flags (stdoutput, sec,
3548 SEC_HAS_CONTENTS | SEC_READONLY);
3549 (void) bfd_set_section_alignment (stdoutput, sec, 2);
252b5132 3550 }
f3ded42a
RS
3551 else if (mips_flag_pdr)
3552 {
3553 pdr_seg = subseg_new (".pdr", (subsegT) 0);
3554 (void) bfd_set_section_flags (stdoutput, pdr_seg,
3555 SEC_READONLY | SEC_RELOC
3556 | SEC_DEBUGGING);
3557 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
3558 }
3559
3560 subseg_set (seg, subseg);
3561 }
252b5132
RH
3562
3563 if (! ECOFF_DEBUGGING)
3564 md_obj_begin ();
71400594
RS
3565
3566 if (mips_fix_vr4120)
3567 init_vr4120_conflicts ();
252b5132
RH
3568}
3569
3570void
17a2f251 3571md_mips_end (void)
252b5132 3572{
02b1ab82 3573 mips_emit_delays ();
252b5132
RH
3574 if (! ECOFF_DEBUGGING)
3575 md_obj_end ();
3576}
3577
3578void
17a2f251 3579md_assemble (char *str)
252b5132
RH
3580{
3581 struct mips_cl_insn insn;
f6688943
TS
3582 bfd_reloc_code_real_type unused_reloc[3]
3583 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
3584
3585 imm_expr.X_op = O_absent;
252b5132 3586 offset_expr.X_op = O_absent;
f6688943
TS
3587 offset_reloc[0] = BFD_RELOC_UNUSED;
3588 offset_reloc[1] = BFD_RELOC_UNUSED;
3589 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132 3590
e1b47bd5
RS
3591 mips_mark_labels ();
3592 mips_assembling_insn = TRUE;
e3de51ce 3593 clear_insn_error ();
e1b47bd5 3594
252b5132
RH
3595 if (mips_opts.mips16)
3596 mips16_ip (str, &insn);
3597 else
3598 {
3599 mips_ip (str, &insn);
beae10d5
KH
3600 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
3601 str, insn.insn_opcode));
252b5132
RH
3602 }
3603
e3de51ce
RS
3604 if (insn_error.msg)
3605 report_insn_error (str);
e1b47bd5 3606 else if (insn.insn_mo->pinfo == INSN_MACRO)
252b5132 3607 {
584892a6 3608 macro_start ();
252b5132
RH
3609 if (mips_opts.mips16)
3610 mips16_macro (&insn);
3611 else
833794fc 3612 macro (&insn, str);
584892a6 3613 macro_end ();
252b5132
RH
3614 }
3615 else
3616 {
77bd4346 3617 if (offset_expr.X_op != O_absent)
df58fc94 3618 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
252b5132 3619 else
df58fc94 3620 append_insn (&insn, NULL, unused_reloc, FALSE);
252b5132 3621 }
e1b47bd5
RS
3622
3623 mips_assembling_insn = FALSE;
252b5132
RH
3624}
3625
738e5348
RS
3626/* Convenience functions for abstracting away the differences between
3627 MIPS16 and non-MIPS16 relocations. */
3628
3629static inline bfd_boolean
3630mips16_reloc_p (bfd_reloc_code_real_type reloc)
3631{
3632 switch (reloc)
3633 {
3634 case BFD_RELOC_MIPS16_JMP:
3635 case BFD_RELOC_MIPS16_GPREL:
3636 case BFD_RELOC_MIPS16_GOT16:
3637 case BFD_RELOC_MIPS16_CALL16:
3638 case BFD_RELOC_MIPS16_HI16_S:
3639 case BFD_RELOC_MIPS16_HI16:
3640 case BFD_RELOC_MIPS16_LO16:
3641 return TRUE;
3642
3643 default:
3644 return FALSE;
3645 }
3646}
3647
df58fc94
RS
3648static inline bfd_boolean
3649micromips_reloc_p (bfd_reloc_code_real_type reloc)
3650{
3651 switch (reloc)
3652 {
3653 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
3654 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
3655 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
3656 case BFD_RELOC_MICROMIPS_GPREL16:
3657 case BFD_RELOC_MICROMIPS_JMP:
3658 case BFD_RELOC_MICROMIPS_HI16:
3659 case BFD_RELOC_MICROMIPS_HI16_S:
3660 case BFD_RELOC_MICROMIPS_LO16:
3661 case BFD_RELOC_MICROMIPS_LITERAL:
3662 case BFD_RELOC_MICROMIPS_GOT16:
3663 case BFD_RELOC_MICROMIPS_CALL16:
3664 case BFD_RELOC_MICROMIPS_GOT_HI16:
3665 case BFD_RELOC_MICROMIPS_GOT_LO16:
3666 case BFD_RELOC_MICROMIPS_CALL_HI16:
3667 case BFD_RELOC_MICROMIPS_CALL_LO16:
3668 case BFD_RELOC_MICROMIPS_SUB:
3669 case BFD_RELOC_MICROMIPS_GOT_PAGE:
3670 case BFD_RELOC_MICROMIPS_GOT_OFST:
3671 case BFD_RELOC_MICROMIPS_GOT_DISP:
3672 case BFD_RELOC_MICROMIPS_HIGHEST:
3673 case BFD_RELOC_MICROMIPS_HIGHER:
3674 case BFD_RELOC_MICROMIPS_SCN_DISP:
3675 case BFD_RELOC_MICROMIPS_JALR:
3676 return TRUE;
3677
3678 default:
3679 return FALSE;
3680 }
3681}
3682
2309ddf2
MR
3683static inline bfd_boolean
3684jmp_reloc_p (bfd_reloc_code_real_type reloc)
3685{
3686 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
3687}
3688
738e5348
RS
3689static inline bfd_boolean
3690got16_reloc_p (bfd_reloc_code_real_type reloc)
3691{
2309ddf2 3692 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
df58fc94 3693 || reloc == BFD_RELOC_MICROMIPS_GOT16);
738e5348
RS
3694}
3695
3696static inline bfd_boolean
3697hi16_reloc_p (bfd_reloc_code_real_type reloc)
3698{
2309ddf2 3699 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
df58fc94 3700 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
738e5348
RS
3701}
3702
3703static inline bfd_boolean
3704lo16_reloc_p (bfd_reloc_code_real_type reloc)
3705{
2309ddf2 3706 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
df58fc94
RS
3707 || reloc == BFD_RELOC_MICROMIPS_LO16);
3708}
3709
df58fc94
RS
3710static inline bfd_boolean
3711jalr_reloc_p (bfd_reloc_code_real_type reloc)
3712{
2309ddf2 3713 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
738e5348
RS
3714}
3715
f2ae14a1
RS
3716static inline bfd_boolean
3717gprel16_reloc_p (bfd_reloc_code_real_type reloc)
3718{
3719 return (reloc == BFD_RELOC_GPREL16 || reloc == BFD_RELOC_MIPS16_GPREL
3720 || reloc == BFD_RELOC_MICROMIPS_GPREL16);
3721}
3722
2de39019
CM
3723/* Return true if RELOC is a PC-relative relocation that does not have
3724 full address range. */
3725
3726static inline bfd_boolean
3727limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
3728{
3729 switch (reloc)
3730 {
3731 case BFD_RELOC_16_PCREL_S2:
3732 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
3733 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
3734 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
3735 return TRUE;
3736
b47468a6
CM
3737 case BFD_RELOC_32_PCREL:
3738 return HAVE_64BIT_ADDRESSES;
3739
2de39019
CM
3740 default:
3741 return FALSE;
3742 }
3743}
b47468a6 3744
5919d012 3745/* Return true if the given relocation might need a matching %lo().
0a44bf69
RS
3746 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
3747 need a matching %lo() when applied to local symbols. */
5919d012
RS
3748
3749static inline bfd_boolean
17a2f251 3750reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 3751{
3b91255e 3752 return (HAVE_IN_PLACE_ADDENDS
738e5348 3753 && (hi16_reloc_p (reloc)
0a44bf69
RS
3754 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
3755 all GOT16 relocations evaluate to "G". */
738e5348
RS
3756 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
3757}
3758
3759/* Return the type of %lo() reloc needed by RELOC, given that
3760 reloc_needs_lo_p. */
3761
3762static inline bfd_reloc_code_real_type
3763matching_lo_reloc (bfd_reloc_code_real_type reloc)
3764{
df58fc94
RS
3765 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
3766 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
3767 : BFD_RELOC_LO16));
5919d012
RS
3768}
3769
3770/* Return true if the given fixup is followed by a matching R_MIPS_LO16
3771 relocation. */
3772
3773static inline bfd_boolean
17a2f251 3774fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
3775{
3776 return (fixp->fx_next != NULL
738e5348 3777 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
5919d012
RS
3778 && fixp->fx_addsy == fixp->fx_next->fx_addsy
3779 && fixp->fx_offset == fixp->fx_next->fx_offset);
3780}
3781
462427c4
RS
3782/* Move all labels in LABELS to the current insertion point. TEXT_P
3783 says whether the labels refer to text or data. */
404a8071
RS
3784
3785static void
462427c4 3786mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
404a8071
RS
3787{
3788 struct insn_label_list *l;
3789 valueT val;
3790
462427c4 3791 for (l = labels; l != NULL; l = l->next)
404a8071 3792 {
9c2799c2 3793 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
404a8071
RS
3794 symbol_set_frag (l->label, frag_now);
3795 val = (valueT) frag_now_fix ();
df58fc94 3796 /* MIPS16/microMIPS text labels are stored as odd. */
462427c4 3797 if (text_p && HAVE_CODE_COMPRESSION)
404a8071
RS
3798 ++val;
3799 S_SET_VALUE (l->label, val);
3800 }
3801}
3802
462427c4
RS
3803/* Move all labels in insn_labels to the current insertion point
3804 and treat them as text labels. */
3805
3806static void
3807mips_move_text_labels (void)
3808{
3809 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
3810}
3811
5f0fe04b
TS
3812static bfd_boolean
3813s_is_linkonce (symbolS *sym, segT from_seg)
3814{
3815 bfd_boolean linkonce = FALSE;
3816 segT symseg = S_GET_SEGMENT (sym);
3817
3818 if (symseg != from_seg && !S_IS_LOCAL (sym))
3819 {
3820 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
3821 linkonce = TRUE;
5f0fe04b
TS
3822 /* The GNU toolchain uses an extension for ELF: a section
3823 beginning with the magic string .gnu.linkonce is a
3824 linkonce section. */
3825 if (strncmp (segment_name (symseg), ".gnu.linkonce",
3826 sizeof ".gnu.linkonce" - 1) == 0)
3827 linkonce = TRUE;
5f0fe04b
TS
3828 }
3829 return linkonce;
3830}
3831
e1b47bd5 3832/* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
df58fc94
RS
3833 linker to handle them specially, such as generating jalx instructions
3834 when needed. We also make them odd for the duration of the assembly,
3835 in order to generate the right sort of code. We will make them even
252b5132
RH
3836 in the adjust_symtab routine, while leaving them marked. This is
3837 convenient for the debugger and the disassembler. The linker knows
3838 to make them odd again. */
3839
3840static void
e1b47bd5 3841mips_compressed_mark_label (symbolS *label)
252b5132 3842{
df58fc94 3843 gas_assert (HAVE_CODE_COMPRESSION);
a8dbcb85 3844
f3ded42a
RS
3845 if (mips_opts.mips16)
3846 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
3847 else
3848 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
e1b47bd5
RS
3849 if ((S_GET_VALUE (label) & 1) == 0
3850 /* Don't adjust the address if the label is global or weak, or
3851 in a link-once section, since we'll be emitting symbol reloc
3852 references to it which will be patched up by the linker, and
3853 the final value of the symbol may or may not be MIPS16/microMIPS. */
3854 && !S_IS_WEAK (label)
3855 && !S_IS_EXTERNAL (label)
3856 && !s_is_linkonce (label, now_seg))
3857 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
3858}
3859
3860/* Mark preceding MIPS16 or microMIPS instruction labels. */
3861
3862static void
3863mips_compressed_mark_labels (void)
3864{
3865 struct insn_label_list *l;
3866
3867 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
3868 mips_compressed_mark_label (l->label);
252b5132
RH
3869}
3870
4d7206a2
RS
3871/* End the current frag. Make it a variant frag and record the
3872 relaxation info. */
3873
3874static void
3875relax_close_frag (void)
3876{
584892a6 3877 mips_macro_warning.first_frag = frag_now;
4d7206a2 3878 frag_var (rs_machine_dependent, 0, 0,
584892a6 3879 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
4d7206a2
RS
3880 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
3881
3882 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
3883 mips_relax.first_fixup = 0;
3884}
3885
3886/* Start a new relaxation sequence whose expansion depends on SYMBOL.
3887 See the comment above RELAX_ENCODE for more details. */
3888
3889static void
3890relax_start (symbolS *symbol)
3891{
9c2799c2 3892 gas_assert (mips_relax.sequence == 0);
4d7206a2
RS
3893 mips_relax.sequence = 1;
3894 mips_relax.symbol = symbol;
3895}
3896
3897/* Start generating the second version of a relaxable sequence.
3898 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
3899
3900static void
4d7206a2
RS
3901relax_switch (void)
3902{
9c2799c2 3903 gas_assert (mips_relax.sequence == 1);
4d7206a2
RS
3904 mips_relax.sequence = 2;
3905}
3906
3907/* End the current relaxable sequence. */
3908
3909static void
3910relax_end (void)
3911{
9c2799c2 3912 gas_assert (mips_relax.sequence == 2);
4d7206a2
RS
3913 relax_close_frag ();
3914 mips_relax.sequence = 0;
3915}
3916
11625dd8
RS
3917/* Return true if IP is a delayed branch or jump. */
3918
3919static inline bfd_boolean
3920delayed_branch_p (const struct mips_cl_insn *ip)
3921{
3922 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
3923 | INSN_COND_BRANCH_DELAY
3924 | INSN_COND_BRANCH_LIKELY)) != 0;
3925}
3926
3927/* Return true if IP is a compact branch or jump. */
3928
3929static inline bfd_boolean
3930compact_branch_p (const struct mips_cl_insn *ip)
3931{
26545944
RS
3932 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
3933 | INSN2_COND_BRANCH)) != 0;
11625dd8
RS
3934}
3935
3936/* Return true if IP is an unconditional branch or jump. */
3937
3938static inline bfd_boolean
3939uncond_branch_p (const struct mips_cl_insn *ip)
3940{
3941 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
26545944 3942 || (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0);
11625dd8
RS
3943}
3944
3945/* Return true if IP is a branch-likely instruction. */
3946
3947static inline bfd_boolean
3948branch_likely_p (const struct mips_cl_insn *ip)
3949{
3950 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
3951}
3952
14fe068b
RS
3953/* Return the type of nop that should be used to fill the delay slot
3954 of delayed branch IP. */
3955
3956static struct mips_cl_insn *
3957get_delay_slot_nop (const struct mips_cl_insn *ip)
3958{
3959 if (mips_opts.micromips
3960 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
3961 return &micromips_nop32_insn;
3962 return NOP_INSN;
3963}
3964
fc76e730
RS
3965/* Return a mask that has bit N set if OPCODE reads the register(s)
3966 in operand N. */
df58fc94
RS
3967
3968static unsigned int
fc76e730 3969insn_read_mask (const struct mips_opcode *opcode)
df58fc94 3970{
fc76e730
RS
3971 return (opcode->pinfo & INSN_READ_ALL) >> INSN_READ_SHIFT;
3972}
df58fc94 3973
fc76e730
RS
3974/* Return a mask that has bit N set if OPCODE writes to the register(s)
3975 in operand N. */
3976
3977static unsigned int
3978insn_write_mask (const struct mips_opcode *opcode)
3979{
3980 return (opcode->pinfo & INSN_WRITE_ALL) >> INSN_WRITE_SHIFT;
3981}
3982
3983/* Return a mask of the registers specified by operand OPERAND of INSN.
3984 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
3985 is set. */
3986
3987static unsigned int
3988operand_reg_mask (const struct mips_cl_insn *insn,
3989 const struct mips_operand *operand,
3990 unsigned int type_mask)
3991{
3992 unsigned int uval, vsel;
3993
3994 switch (operand->type)
df58fc94 3995 {
fc76e730
RS
3996 case OP_INT:
3997 case OP_MAPPED_INT:
3998 case OP_MSB:
3999 case OP_PCREL:
4000 case OP_PERF_REG:
4001 case OP_ADDIUSP_INT:
4002 case OP_ENTRY_EXIT_LIST:
4003 case OP_REPEAT_DEST_REG:
4004 case OP_REPEAT_PREV_REG:
4005 case OP_PC:
14daeee3
RS
4006 case OP_VU0_SUFFIX:
4007 case OP_VU0_MATCH_SUFFIX:
fc76e730
RS
4008 abort ();
4009
4010 case OP_REG:
0f35dbc4 4011 case OP_OPTIONAL_REG:
fc76e730
RS
4012 {
4013 const struct mips_reg_operand *reg_op;
4014
4015 reg_op = (const struct mips_reg_operand *) operand;
4016 if (!(type_mask & (1 << reg_op->reg_type)))
4017 return 0;
4018 uval = insn_extract_operand (insn, operand);
4019 return 1 << mips_decode_reg_operand (reg_op, uval);
4020 }
4021
4022 case OP_REG_PAIR:
4023 {
4024 const struct mips_reg_pair_operand *pair_op;
4025
4026 pair_op = (const struct mips_reg_pair_operand *) operand;
4027 if (!(type_mask & (1 << pair_op->reg_type)))
4028 return 0;
4029 uval = insn_extract_operand (insn, operand);
4030 return (1 << pair_op->reg1_map[uval]) | (1 << pair_op->reg2_map[uval]);
4031 }
4032
4033 case OP_CLO_CLZ_DEST:
4034 if (!(type_mask & (1 << OP_REG_GP)))
4035 return 0;
4036 uval = insn_extract_operand (insn, operand);
4037 return (1 << (uval & 31)) | (1 << (uval >> 5));
4038
4039 case OP_LWM_SWM_LIST:
4040 abort ();
4041
4042 case OP_SAVE_RESTORE_LIST:
4043 abort ();
4044
4045 case OP_MDMX_IMM_REG:
4046 if (!(type_mask & (1 << OP_REG_VEC)))
4047 return 0;
4048 uval = insn_extract_operand (insn, operand);
4049 vsel = uval >> 5;
4050 if ((vsel & 0x18) == 0x18)
4051 return 0;
4052 return 1 << (uval & 31);
df58fc94 4053 }
fc76e730
RS
4054 abort ();
4055}
4056
4057/* Return a mask of the registers specified by operands OPNO_MASK of INSN,
4058 where bit N of OPNO_MASK is set if operand N should be included.
4059 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4060 is set. */
4061
4062static unsigned int
4063insn_reg_mask (const struct mips_cl_insn *insn,
4064 unsigned int type_mask, unsigned int opno_mask)
4065{
4066 unsigned int opno, reg_mask;
4067
4068 opno = 0;
4069 reg_mask = 0;
4070 while (opno_mask != 0)
4071 {
4072 if (opno_mask & 1)
4073 reg_mask |= operand_reg_mask (insn, insn_opno (insn, opno), type_mask);
4074 opno_mask >>= 1;
4075 opno += 1;
4076 }
4077 return reg_mask;
df58fc94
RS
4078}
4079
4c260379
RS
4080/* Return the mask of core registers that IP reads. */
4081
4082static unsigned int
4083gpr_read_mask (const struct mips_cl_insn *ip)
4084{
4085 unsigned long pinfo, pinfo2;
4086 unsigned int mask;
4087
fc76e730 4088 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_read_mask (ip->insn_mo));
4c260379
RS
4089 pinfo = ip->insn_mo->pinfo;
4090 pinfo2 = ip->insn_mo->pinfo2;
fc76e730 4091 if (pinfo & INSN_UDI)
4c260379 4092 {
fc76e730
RS
4093 /* UDI instructions have traditionally been assumed to read RS
4094 and RT. */
4095 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4096 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4c260379 4097 }
fc76e730
RS
4098 if (pinfo & INSN_READ_GPR_24)
4099 mask |= 1 << 24;
4100 if (pinfo2 & INSN2_READ_GPR_16)
4101 mask |= 1 << 16;
4102 if (pinfo2 & INSN2_READ_SP)
4103 mask |= 1 << SP;
26545944 4104 if (pinfo2 & INSN2_READ_GPR_31)
fc76e730 4105 mask |= 1 << 31;
fe35f09f
RS
4106 /* Don't include register 0. */
4107 return mask & ~1;
4c260379
RS
4108}
4109
4110/* Return the mask of core registers that IP writes. */
4111
4112static unsigned int
4113gpr_write_mask (const struct mips_cl_insn *ip)
4114{
4115 unsigned long pinfo, pinfo2;
4116 unsigned int mask;
4117
fc76e730 4118 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_write_mask (ip->insn_mo));
4c260379
RS
4119 pinfo = ip->insn_mo->pinfo;
4120 pinfo2 = ip->insn_mo->pinfo2;
fc76e730
RS
4121 if (pinfo & INSN_WRITE_GPR_24)
4122 mask |= 1 << 24;
4123 if (pinfo & INSN_WRITE_GPR_31)
4124 mask |= 1 << 31;
4125 if (pinfo & INSN_UDI)
4126 /* UDI instructions have traditionally been assumed to write to RD. */
4127 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4128 if (pinfo2 & INSN2_WRITE_SP)
4129 mask |= 1 << SP;
fe35f09f
RS
4130 /* Don't include register 0. */
4131 return mask & ~1;
4c260379
RS
4132}
4133
4134/* Return the mask of floating-point registers that IP reads. */
4135
4136static unsigned int
4137fpr_read_mask (const struct mips_cl_insn *ip)
4138{
fc76e730 4139 unsigned long pinfo;
4c260379
RS
4140 unsigned int mask;
4141
fc76e730
RS
4142 mask = insn_reg_mask (ip, (1 << OP_REG_FP) | (1 << OP_REG_VEC),
4143 insn_read_mask (ip->insn_mo));
4c260379 4144 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4145 /* Conservatively treat all operands to an FP_D instruction are doubles.
4146 (This is overly pessimistic for things like cvt.d.s.) */
4147 if (HAVE_32BIT_FPRS && (pinfo & FP_D))
4148 mask |= mask << 1;
4149 return mask;
4150}
4151
4152/* Return the mask of floating-point registers that IP writes. */
4153
4154static unsigned int
4155fpr_write_mask (const struct mips_cl_insn *ip)
4156{
fc76e730 4157 unsigned long pinfo;
4c260379
RS
4158 unsigned int mask;
4159
fc76e730
RS
4160 mask = insn_reg_mask (ip, (1 << OP_REG_FP) | (1 << OP_REG_VEC),
4161 insn_write_mask (ip->insn_mo));
4c260379 4162 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4163 /* Conservatively treat all operands to an FP_D instruction are doubles.
4164 (This is overly pessimistic for things like cvt.s.d.) */
4165 if (HAVE_32BIT_FPRS && (pinfo & FP_D))
4166 mask |= mask << 1;
4167 return mask;
4168}
4169
a1d78564
RS
4170/* Operand OPNUM of INSN is an odd-numbered floating-point register.
4171 Check whether that is allowed. */
4172
4173static bfd_boolean
4174mips_oddfpreg_ok (const struct mips_opcode *insn, int opnum)
4175{
4176 const char *s = insn->name;
4177
4178 if (insn->pinfo == INSN_MACRO)
4179 /* Let a macro pass, we'll catch it later when it is expanded. */
4180 return TRUE;
4181
4182 if (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa) || mips_opts.arch == CPU_R5900)
4183 {
4184 /* Allow odd registers for single-precision ops. */
4185 switch (insn->pinfo & (FP_S | FP_D))
4186 {
4187 case FP_S:
4188 case 0:
4189 return TRUE;
4190 case FP_D:
4191 return FALSE;
4192 default:
4193 break;
4194 }
4195
4196 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
4197 s = strchr (insn->name, '.');
4198 if (s != NULL && opnum == 2)
4199 s = strchr (s + 1, '.');
4200 return (s != NULL && (s[1] == 'w' || s[1] == 's'));
4201 }
4202
4203 /* Single-precision coprocessor loads and moves are OK too. */
4204 if ((insn->pinfo & FP_S)
4205 && (insn->pinfo & (INSN_COPROC_MEMORY_DELAY | INSN_STORE_MEMORY
4206 | INSN_LOAD_COPROC_DELAY | INSN_COPROC_MOVE_DELAY)))
4207 return TRUE;
4208
4209 return FALSE;
4210}
4211
a1d78564
RS
4212/* Information about an instruction argument that we're trying to match. */
4213struct mips_arg_info
4214{
4215 /* The instruction so far. */
4216 struct mips_cl_insn *insn;
4217
a92713e6
RS
4218 /* The first unconsumed operand token. */
4219 struct mips_operand_token *token;
4220
a1d78564
RS
4221 /* The 1-based operand number, in terms of insn->insn_mo->args. */
4222 int opnum;
4223
4224 /* The 1-based argument number, for error reporting. This does not
4225 count elided optional registers, etc.. */
4226 int argnum;
4227
4228 /* The last OP_REG operand seen, or ILLEGAL_REG if none. */
4229 unsigned int last_regno;
4230
4231 /* If the first operand was an OP_REG, this is the register that it
4232 specified, otherwise it is ILLEGAL_REG. */
4233 unsigned int dest_regno;
4234
4235 /* The value of the last OP_INT operand. Only used for OP_MSB,
4236 where it gives the lsb position. */
4237 unsigned int last_op_int;
4238
60f20e8b
RS
4239 /* If true, match routines should assume that no later instruction
4240 alternative matches and should therefore be as accomodating as
4241 possible. Match routines should not report errors if something
4242 is only invalid for !LAX_MATCH. */
4243 bfd_boolean lax_match;
a1d78564 4244
a1d78564
RS
4245 /* True if a reference to the current AT register was seen. */
4246 bfd_boolean seen_at;
4247};
4248
1a00e612
RS
4249/* Record that the argument is out of range. */
4250
4251static void
4252match_out_of_range (struct mips_arg_info *arg)
4253{
4254 set_insn_error_i (arg->argnum, _("operand %d out of range"), arg->argnum);
4255}
4256
4257/* Record that the argument isn't constant but needs to be. */
4258
4259static void
4260match_not_constant (struct mips_arg_info *arg)
4261{
4262 set_insn_error_i (arg->argnum, _("operand %d must be constant"),
4263 arg->argnum);
4264}
4265
a92713e6
RS
4266/* Try to match an OT_CHAR token for character CH. Consume the token
4267 and return true on success, otherwise return false. */
a1d78564 4268
a92713e6
RS
4269static bfd_boolean
4270match_char (struct mips_arg_info *arg, char ch)
a1d78564 4271{
a92713e6
RS
4272 if (arg->token->type == OT_CHAR && arg->token->u.ch == ch)
4273 {
4274 ++arg->token;
4275 if (ch == ',')
4276 arg->argnum += 1;
4277 return TRUE;
4278 }
4279 return FALSE;
4280}
a1d78564 4281
a92713e6
RS
4282/* Try to get an expression from the next tokens in ARG. Consume the
4283 tokens and return true on success, storing the expression value in
4284 VALUE and relocation types in R. */
4285
4286static bfd_boolean
4287match_expression (struct mips_arg_info *arg, expressionS *value,
4288 bfd_reloc_code_real_type *r)
4289{
d436c1c2
RS
4290 /* If the next token is a '(' that was parsed as being part of a base
4291 expression, assume we have an elided offset. The later match will fail
4292 if this turns out to be wrong. */
4293 if (arg->token->type == OT_CHAR && arg->token->u.ch == '(')
a1d78564 4294 {
d436c1c2
RS
4295 value->X_op = O_constant;
4296 value->X_add_number = 0;
4297 r[0] = r[1] = r[2] = BFD_RELOC_UNUSED;
a92713e6
RS
4298 return TRUE;
4299 }
4300
d436c1c2
RS
4301 /* Reject register-based expressions such as "0+$2" and "(($2))".
4302 For plain registers the default error seems more appropriate. */
4303 if (arg->token->type == OT_INTEGER
4304 && arg->token->u.integer.value.X_op == O_register)
a92713e6 4305 {
d436c1c2
RS
4306 set_insn_error (arg->argnum, _("register value used as expression"));
4307 return FALSE;
a1d78564 4308 }
d436c1c2
RS
4309
4310 if (arg->token->type == OT_INTEGER)
a92713e6 4311 {
d436c1c2
RS
4312 *value = arg->token->u.integer.value;
4313 memcpy (r, arg->token->u.integer.relocs, 3 * sizeof (*r));
4314 ++arg->token;
4315 return TRUE;
a92713e6 4316 }
a92713e6 4317
d436c1c2
RS
4318 set_insn_error_i
4319 (arg->argnum, _("operand %d must be an immediate expression"),
4320 arg->argnum);
4321 return FALSE;
a92713e6
RS
4322}
4323
4324/* Try to get a constant expression from the next tokens in ARG. Consume
4325 the tokens and return return true on success, storing the constant value
4326 in *VALUE. Use FALLBACK as the value if the match succeeded with an
4327 error. */
4328
4329static bfd_boolean
1a00e612 4330match_const_int (struct mips_arg_info *arg, offsetT *value)
a92713e6
RS
4331{
4332 expressionS ex;
4333 bfd_reloc_code_real_type r[3];
a1d78564 4334
a92713e6
RS
4335 if (!match_expression (arg, &ex, r))
4336 return FALSE;
4337
4338 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_constant)
a1d78564
RS
4339 *value = ex.X_add_number;
4340 else
4341 {
1a00e612
RS
4342 match_not_constant (arg);
4343 return FALSE;
a1d78564 4344 }
a92713e6 4345 return TRUE;
a1d78564
RS
4346}
4347
4348/* Return the RTYPE_* flags for a register operand of type TYPE that
4349 appears in instruction OPCODE. */
4350
4351static unsigned int
4352convert_reg_type (const struct mips_opcode *opcode,
4353 enum mips_reg_operand_type type)
4354{
4355 switch (type)
4356 {
4357 case OP_REG_GP:
4358 return RTYPE_NUM | RTYPE_GP;
4359
4360 case OP_REG_FP:
4361 /* Allow vector register names for MDMX if the instruction is a 64-bit
4362 FPR load, store or move (including moves to and from GPRs). */
4363 if ((mips_opts.ase & ASE_MDMX)
4364 && (opcode->pinfo & FP_D)
4365 && (opcode->pinfo & (INSN_COPROC_MOVE_DELAY
4366 | INSN_COPROC_MEMORY_DELAY
4367 | INSN_LOAD_COPROC_DELAY
4368 | INSN_LOAD_MEMORY_DELAY
4369 | INSN_STORE_MEMORY)))
4370 return RTYPE_FPU | RTYPE_VEC;
4371 return RTYPE_FPU;
4372
4373 case OP_REG_CCC:
4374 if (opcode->pinfo & (FP_D | FP_S))
4375 return RTYPE_CCC | RTYPE_FCC;
4376 return RTYPE_CCC;
4377
4378 case OP_REG_VEC:
4379 if (opcode->membership & INSN_5400)
4380 return RTYPE_FPU;
4381 return RTYPE_FPU | RTYPE_VEC;
4382
4383 case OP_REG_ACC:
4384 return RTYPE_ACC;
4385
4386 case OP_REG_COPRO:
4387 if (opcode->name[strlen (opcode->name) - 1] == '0')
4388 return RTYPE_NUM | RTYPE_CP0;
4389 return RTYPE_NUM;
4390
4391 case OP_REG_HW:
4392 return RTYPE_NUM;
14daeee3
RS
4393
4394 case OP_REG_VI:
4395 return RTYPE_NUM | RTYPE_VI;
4396
4397 case OP_REG_VF:
4398 return RTYPE_NUM | RTYPE_VF;
4399
4400 case OP_REG_R5900_I:
4401 return RTYPE_R5900_I;
4402
4403 case OP_REG_R5900_Q:
4404 return RTYPE_R5900_Q;
4405
4406 case OP_REG_R5900_R:
4407 return RTYPE_R5900_R;
4408
4409 case OP_REG_R5900_ACC:
4410 return RTYPE_R5900_ACC;
a1d78564
RS
4411 }
4412 abort ();
4413}
4414
4415/* ARG is register REGNO, of type TYPE. Warn about any dubious registers. */
4416
4417static void
4418check_regno (struct mips_arg_info *arg,
4419 enum mips_reg_operand_type type, unsigned int regno)
4420{
4421 if (AT && type == OP_REG_GP && regno == AT)
4422 arg->seen_at = TRUE;
4423
4424 if (type == OP_REG_FP
4425 && (regno & 1) != 0
4426 && HAVE_32BIT_FPRS
4427 && !mips_oddfpreg_ok (arg->insn->insn_mo, arg->opnum))
4428 as_warn (_("Float register should be even, was %d"), regno);
4429
4430 if (type == OP_REG_CCC)
4431 {
4432 const char *name;
4433 size_t length;
4434
4435 name = arg->insn->insn_mo->name;
4436 length = strlen (name);
4437 if ((regno & 1) != 0
4438 && ((length >= 3 && strcmp (name + length - 3, ".ps") == 0)
4439 || (length >= 5 && strncmp (name + length - 5, "any2", 4) == 0)))
4440 as_warn (_("Condition code register should be even for %s, was %d"),
4441 name, regno);
4442
4443 if ((regno & 3) != 0
4444 && (length >= 5 && strncmp (name + length - 5, "any4", 4) == 0))
4445 as_warn (_("Condition code register should be 0 or 4 for %s, was %d"),
4446 name, regno);
4447 }
4448}
4449
a92713e6
RS
4450/* ARG is a register with symbol value SYMVAL. Try to interpret it as
4451 a register of type TYPE. Return true on success, storing the register
4452 number in *REGNO and warning about any dubious uses. */
4453
4454static bfd_boolean
4455match_regno (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4456 unsigned int symval, unsigned int *regno)
4457{
4458 if (type == OP_REG_VEC)
4459 symval = mips_prefer_vec_regno (symval);
4460 if (!(symval & convert_reg_type (arg->insn->insn_mo, type)))
4461 return FALSE;
4462
4463 *regno = symval & RNUM_MASK;
4464 check_regno (arg, type, *regno);
4465 return TRUE;
4466}
4467
4468/* Try to interpret the next token in ARG as a register of type TYPE.
4469 Consume the token and return true on success, storing the register
4470 number in *REGNO. Return false on failure. */
4471
4472static bfd_boolean
4473match_reg (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4474 unsigned int *regno)
4475{
4476 if (arg->token->type == OT_REG
4477 && match_regno (arg, type, arg->token->u.regno, regno))
4478 {
4479 ++arg->token;
4480 return TRUE;
4481 }
4482 return FALSE;
4483}
4484
4485/* Try to interpret the next token in ARG as a range of registers of type TYPE.
4486 Consume the token and return true on success, storing the register numbers
4487 in *REGNO1 and *REGNO2. Return false on failure. */
4488
4489static bfd_boolean
4490match_reg_range (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4491 unsigned int *regno1, unsigned int *regno2)
4492{
4493 if (match_reg (arg, type, regno1))
4494 {
4495 *regno2 = *regno1;
4496 return TRUE;
4497 }
4498 if (arg->token->type == OT_REG_RANGE
4499 && match_regno (arg, type, arg->token->u.reg_range.regno1, regno1)
4500 && match_regno (arg, type, arg->token->u.reg_range.regno2, regno2)
4501 && *regno1 <= *regno2)
4502 {
4503 ++arg->token;
4504 return TRUE;
4505 }
4506 return FALSE;
4507}
4508
a1d78564
RS
4509/* OP_INT matcher. */
4510
a92713e6 4511static bfd_boolean
a1d78564 4512match_int_operand (struct mips_arg_info *arg,
a92713e6 4513 const struct mips_operand *operand_base)
a1d78564
RS
4514{
4515 const struct mips_int_operand *operand;
3ccad066 4516 unsigned int uval;
a1d78564
RS
4517 int min_val, max_val, factor;
4518 offsetT sval;
a1d78564
RS
4519
4520 operand = (const struct mips_int_operand *) operand_base;
4521 factor = 1 << operand->shift;
3ccad066
RS
4522 min_val = mips_int_operand_min (operand);
4523 max_val = mips_int_operand_max (operand);
a1d78564 4524
d436c1c2
RS
4525 if (operand_base->lsb == 0
4526 && operand_base->size == 16
4527 && operand->shift == 0
4528 && operand->bias == 0
4529 && (operand->max_val == 32767 || operand->max_val == 65535))
a1d78564
RS
4530 {
4531 /* The operand can be relocated. */
a92713e6
RS
4532 if (!match_expression (arg, &offset_expr, offset_reloc))
4533 return FALSE;
4534
4535 if (offset_reloc[0] != BFD_RELOC_UNUSED)
a1d78564
RS
4536 /* Relocation operators were used. Accept the arguent and
4537 leave the relocation value in offset_expr and offset_relocs
4538 for the caller to process. */
a92713e6
RS
4539 return TRUE;
4540
4541 if (offset_expr.X_op != O_constant)
a1d78564 4542 {
60f20e8b
RS
4543 /* Accept non-constant operands if no later alternative matches,
4544 leaving it for the caller to process. */
4545 if (!arg->lax_match)
4546 return FALSE;
a92713e6
RS
4547 offset_reloc[0] = BFD_RELOC_LO16;
4548 return TRUE;
a1d78564 4549 }
a92713e6 4550
a1d78564
RS
4551 /* Clear the global state; we're going to install the operand
4552 ourselves. */
a92713e6 4553 sval = offset_expr.X_add_number;
a1d78564 4554 offset_expr.X_op = O_absent;
60f20e8b
RS
4555
4556 /* For compatibility with older assemblers, we accept
4557 0x8000-0xffff as signed 16-bit numbers when only
4558 signed numbers are allowed. */
4559 if (sval > max_val)
4560 {
4561 max_val = ((1 << operand_base->size) - 1) << operand->shift;
4562 if (!arg->lax_match && sval <= max_val)
4563 return FALSE;
4564 }
a1d78564
RS
4565 }
4566 else
4567 {
1a00e612 4568 if (!match_const_int (arg, &sval))
a92713e6 4569 return FALSE;
a1d78564
RS
4570 }
4571
4572 arg->last_op_int = sval;
4573
1a00e612 4574 if (sval < min_val || sval > max_val || sval % factor)
a1d78564 4575 {
1a00e612
RS
4576 match_out_of_range (arg);
4577 return FALSE;
a1d78564
RS
4578 }
4579
4580 uval = (unsigned int) sval >> operand->shift;
4581 uval -= operand->bias;
4582
4583 /* Handle -mfix-cn63xxp1. */
4584 if (arg->opnum == 1
4585 && mips_fix_cn63xxp1
4586 && !mips_opts.micromips
4587 && strcmp ("pref", arg->insn->insn_mo->name) == 0)
4588 switch (uval)
4589 {
4590 case 5:
4591 case 25:
4592 case 26:
4593 case 27:
4594 case 28:
4595 case 29:
4596 case 30:
4597 case 31:
4598 /* These are ok. */
4599 break;
4600
4601 default:
4602 /* The rest must be changed to 28. */
4603 uval = 28;
4604 break;
4605 }
4606
4607 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 4608 return TRUE;
a1d78564
RS
4609}
4610
4611/* OP_MAPPED_INT matcher. */
4612
a92713e6 4613static bfd_boolean
a1d78564 4614match_mapped_int_operand (struct mips_arg_info *arg,
a92713e6 4615 const struct mips_operand *operand_base)
a1d78564
RS
4616{
4617 const struct mips_mapped_int_operand *operand;
4618 unsigned int uval, num_vals;
4619 offsetT sval;
4620
4621 operand = (const struct mips_mapped_int_operand *) operand_base;
1a00e612 4622 if (!match_const_int (arg, &sval))
a92713e6 4623 return FALSE;
a1d78564
RS
4624
4625 num_vals = 1 << operand_base->size;
4626 for (uval = 0; uval < num_vals; uval++)
4627 if (operand->int_map[uval] == sval)
4628 break;
4629 if (uval == num_vals)
1a00e612
RS
4630 {
4631 match_out_of_range (arg);
4632 return FALSE;
4633 }
a1d78564
RS
4634
4635 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 4636 return TRUE;
a1d78564
RS
4637}
4638
4639/* OP_MSB matcher. */
4640
a92713e6 4641static bfd_boolean
a1d78564 4642match_msb_operand (struct mips_arg_info *arg,
a92713e6 4643 const struct mips_operand *operand_base)
a1d78564
RS
4644{
4645 const struct mips_msb_operand *operand;
4646 int min_val, max_val, max_high;
4647 offsetT size, sval, high;
4648
4649 operand = (const struct mips_msb_operand *) operand_base;
4650 min_val = operand->bias;
4651 max_val = min_val + (1 << operand_base->size) - 1;
4652 max_high = operand->opsize;
4653
1a00e612 4654 if (!match_const_int (arg, &size))
a92713e6 4655 return FALSE;
a1d78564
RS
4656
4657 high = size + arg->last_op_int;
4658 sval = operand->add_lsb ? high : size;
4659
4660 if (size < 0 || high > max_high || sval < min_val || sval > max_val)
4661 {
1a00e612
RS
4662 match_out_of_range (arg);
4663 return FALSE;
a1d78564
RS
4664 }
4665 insn_insert_operand (arg->insn, operand_base, sval - min_val);
a92713e6 4666 return TRUE;
a1d78564
RS
4667}
4668
4669/* OP_REG matcher. */
4670
a92713e6 4671static bfd_boolean
a1d78564 4672match_reg_operand (struct mips_arg_info *arg,
a92713e6 4673 const struct mips_operand *operand_base)
a1d78564
RS
4674{
4675 const struct mips_reg_operand *operand;
a92713e6 4676 unsigned int regno, uval, num_vals;
a1d78564
RS
4677
4678 operand = (const struct mips_reg_operand *) operand_base;
a92713e6
RS
4679 if (!match_reg (arg, operand->reg_type, &regno))
4680 return FALSE;
a1d78564
RS
4681
4682 if (operand->reg_map)
4683 {
4684 num_vals = 1 << operand->root.size;
4685 for (uval = 0; uval < num_vals; uval++)
4686 if (operand->reg_map[uval] == regno)
4687 break;
4688 if (num_vals == uval)
a92713e6 4689 return FALSE;
a1d78564
RS
4690 }
4691 else
4692 uval = regno;
4693
a1d78564
RS
4694 arg->last_regno = regno;
4695 if (arg->opnum == 1)
4696 arg->dest_regno = regno;
4697 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 4698 return TRUE;
a1d78564
RS
4699}
4700
4701/* OP_REG_PAIR matcher. */
4702
a92713e6 4703static bfd_boolean
a1d78564 4704match_reg_pair_operand (struct mips_arg_info *arg,
a92713e6 4705 const struct mips_operand *operand_base)
a1d78564
RS
4706{
4707 const struct mips_reg_pair_operand *operand;
a92713e6 4708 unsigned int regno1, regno2, uval, num_vals;
a1d78564
RS
4709
4710 operand = (const struct mips_reg_pair_operand *) operand_base;
a92713e6
RS
4711 if (!match_reg (arg, operand->reg_type, &regno1)
4712 || !match_char (arg, ',')
4713 || !match_reg (arg, operand->reg_type, &regno2))
4714 return FALSE;
a1d78564
RS
4715
4716 num_vals = 1 << operand_base->size;
4717 for (uval = 0; uval < num_vals; uval++)
4718 if (operand->reg1_map[uval] == regno1 && operand->reg2_map[uval] == regno2)
4719 break;
4720 if (uval == num_vals)
a92713e6 4721 return FALSE;
a1d78564 4722
a1d78564 4723 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 4724 return TRUE;
a1d78564
RS
4725}
4726
4727/* OP_PCREL matcher. The caller chooses the relocation type. */
4728
a92713e6
RS
4729static bfd_boolean
4730match_pcrel_operand (struct mips_arg_info *arg)
a1d78564 4731{
a92713e6
RS
4732 bfd_reloc_code_real_type r[3];
4733
4734 return match_expression (arg, &offset_expr, r) && r[0] == BFD_RELOC_UNUSED;
a1d78564
RS
4735}
4736
4737/* OP_PERF_REG matcher. */
4738
a92713e6 4739static bfd_boolean
a1d78564 4740match_perf_reg_operand (struct mips_arg_info *arg,
a92713e6 4741 const struct mips_operand *operand)
a1d78564
RS
4742{
4743 offsetT sval;
4744
1a00e612 4745 if (!match_const_int (arg, &sval))
a92713e6 4746 return FALSE;
a1d78564
RS
4747
4748 if (sval != 0
4749 && (sval != 1
4750 || (mips_opts.arch == CPU_R5900
4751 && (strcmp (arg->insn->insn_mo->name, "mfps") == 0
4752 || strcmp (arg->insn->insn_mo->name, "mtps") == 0))))
4753 {
1a00e612
RS
4754 set_insn_error (arg->argnum, _("invalid performance register"));
4755 return FALSE;
a1d78564
RS
4756 }
4757
4758 insn_insert_operand (arg->insn, operand, sval);
a92713e6 4759 return TRUE;
a1d78564
RS
4760}
4761
4762/* OP_ADDIUSP matcher. */
4763
a92713e6 4764static bfd_boolean
a1d78564 4765match_addiusp_operand (struct mips_arg_info *arg,
a92713e6 4766 const struct mips_operand *operand)
a1d78564
RS
4767{
4768 offsetT sval;
4769 unsigned int uval;
4770
1a00e612 4771 if (!match_const_int (arg, &sval))
a92713e6 4772 return FALSE;
a1d78564
RS
4773
4774 if (sval % 4)
1a00e612
RS
4775 {
4776 match_out_of_range (arg);
4777 return FALSE;
4778 }
a1d78564
RS
4779
4780 sval /= 4;
4781 if (!(sval >= -258 && sval <= 257) || (sval >= -2 && sval <= 1))
1a00e612
RS
4782 {
4783 match_out_of_range (arg);
4784 return FALSE;
4785 }
a1d78564
RS
4786
4787 uval = (unsigned int) sval;
4788 uval = ((uval >> 1) & ~0xff) | (uval & 0xff);
4789 insn_insert_operand (arg->insn, operand, uval);
a92713e6 4790 return TRUE;
a1d78564
RS
4791}
4792
4793/* OP_CLO_CLZ_DEST matcher. */
4794
a92713e6 4795static bfd_boolean
a1d78564 4796match_clo_clz_dest_operand (struct mips_arg_info *arg,
a92713e6 4797 const struct mips_operand *operand)
a1d78564
RS
4798{
4799 unsigned int regno;
4800
a92713e6
RS
4801 if (!match_reg (arg, OP_REG_GP, &regno))
4802 return FALSE;
a1d78564 4803
a1d78564 4804 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
a92713e6 4805 return TRUE;
a1d78564
RS
4806}
4807
4808/* OP_LWM_SWM_LIST matcher. */
4809
a92713e6 4810static bfd_boolean
a1d78564 4811match_lwm_swm_list_operand (struct mips_arg_info *arg,
a92713e6 4812 const struct mips_operand *operand)
a1d78564 4813{
a92713e6
RS
4814 unsigned int reglist, sregs, ra, regno1, regno2;
4815 struct mips_arg_info reset;
a1d78564 4816
a92713e6
RS
4817 reglist = 0;
4818 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
4819 return FALSE;
4820 do
4821 {
4822 if (regno2 == FP && regno1 >= S0 && regno1 <= S7)
4823 {
4824 reglist |= 1 << FP;
4825 regno2 = S7;
4826 }
4827 reglist |= ((1U << regno2 << 1) - 1) & -(1U << regno1);
4828 reset = *arg;
4829 }
4830 while (match_char (arg, ',')
4831 && match_reg_range (arg, OP_REG_GP, &regno1, &regno2));
4832 *arg = reset;
a1d78564
RS
4833
4834 if (operand->size == 2)
4835 {
4836 /* The list must include both ra and s0-sN, for 0 <= N <= 3. E.g.:
4837
4838 s0, ra
4839 s0, s1, ra, s2, s3
4840 s0-s2, ra
4841
4842 and any permutations of these. */
4843 if ((reglist & 0xfff1ffff) != 0x80010000)
a92713e6 4844 return FALSE;
a1d78564
RS
4845
4846 sregs = (reglist >> 17) & 7;
4847 ra = 0;
4848 }
4849 else
4850 {
4851 /* The list must include at least one of ra and s0-sN,
4852 for 0 <= N <= 8. (Note that there is a gap between s7 and s8,
4853 which are $23 and $30 respectively.) E.g.:
4854
4855 ra
4856 s0
4857 ra, s0, s1, s2
4858 s0-s8
4859 s0-s5, ra
4860
4861 and any permutations of these. */
4862 if ((reglist & 0x3f00ffff) != 0)
a92713e6 4863 return FALSE;
a1d78564
RS
4864
4865 ra = (reglist >> 27) & 0x10;
4866 sregs = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
4867 }
4868 sregs += 1;
4869 if ((sregs & -sregs) != sregs)
a92713e6 4870 return FALSE;
a1d78564
RS
4871
4872 insn_insert_operand (arg->insn, operand, (ffs (sregs) - 1) | ra);
a92713e6 4873 return TRUE;
a1d78564
RS
4874}
4875
364215c8
RS
4876/* OP_ENTRY_EXIT_LIST matcher. */
4877
a92713e6 4878static unsigned int
364215c8 4879match_entry_exit_operand (struct mips_arg_info *arg,
a92713e6 4880 const struct mips_operand *operand)
364215c8
RS
4881{
4882 unsigned int mask;
4883 bfd_boolean is_exit;
4884
4885 /* The format is the same for both ENTRY and EXIT, but the constraints
4886 are different. */
4887 is_exit = strcmp (arg->insn->insn_mo->name, "exit") == 0;
4888 mask = (is_exit ? 7 << 3 : 0);
a92713e6 4889 do
364215c8
RS
4890 {
4891 unsigned int regno1, regno2;
4892 bfd_boolean is_freg;
4893
a92713e6 4894 if (match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
364215c8 4895 is_freg = FALSE;
a92713e6 4896 else if (match_reg_range (arg, OP_REG_FP, &regno1, &regno2))
364215c8
RS
4897 is_freg = TRUE;
4898 else
a92713e6 4899 return FALSE;
364215c8
RS
4900
4901 if (is_exit && is_freg && regno1 == 0 && regno2 < 2)
4902 {
4903 mask &= ~(7 << 3);
4904 mask |= (5 + regno2) << 3;
4905 }
4906 else if (!is_exit && regno1 == 4 && regno2 >= 4 && regno2 <= 7)
4907 mask |= (regno2 - 3) << 3;
4908 else if (regno1 == 16 && regno2 >= 16 && regno2 <= 17)
4909 mask |= (regno2 - 15) << 1;
4910 else if (regno1 == RA && regno2 == RA)
4911 mask |= 1;
4912 else
a92713e6 4913 return FALSE;
364215c8 4914 }
a92713e6
RS
4915 while (match_char (arg, ','));
4916
364215c8 4917 insn_insert_operand (arg->insn, operand, mask);
a92713e6 4918 return TRUE;
364215c8
RS
4919}
4920
4921/* OP_SAVE_RESTORE_LIST matcher. */
4922
a92713e6
RS
4923static bfd_boolean
4924match_save_restore_list_operand (struct mips_arg_info *arg)
364215c8
RS
4925{
4926 unsigned int opcode, args, statics, sregs;
4927 unsigned int num_frame_sizes, num_args, num_statics, num_sregs;
364215c8 4928 offsetT frame_size;
364215c8 4929
364215c8
RS
4930 opcode = arg->insn->insn_opcode;
4931 frame_size = 0;
4932 num_frame_sizes = 0;
4933 args = 0;
4934 statics = 0;
4935 sregs = 0;
a92713e6 4936 do
364215c8
RS
4937 {
4938 unsigned int regno1, regno2;
4939
a92713e6 4940 if (arg->token->type == OT_INTEGER)
364215c8
RS
4941 {
4942 /* Handle the frame size. */
1a00e612 4943 if (!match_const_int (arg, &frame_size))
a92713e6 4944 return FALSE;
364215c8 4945 num_frame_sizes += 1;
364215c8
RS
4946 }
4947 else
4948 {
a92713e6
RS
4949 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
4950 return FALSE;
364215c8
RS
4951
4952 while (regno1 <= regno2)
4953 {
4954 if (regno1 >= 4 && regno1 <= 7)
4955 {
4956 if (num_frame_sizes == 0)
4957 /* args $a0-$a3 */
4958 args |= 1 << (regno1 - 4);
4959 else
4960 /* statics $a0-$a3 */
4961 statics |= 1 << (regno1 - 4);
4962 }
4963 else if (regno1 >= 16 && regno1 <= 23)
4964 /* $s0-$s7 */
4965 sregs |= 1 << (regno1 - 16);
4966 else if (regno1 == 30)
4967 /* $s8 */
4968 sregs |= 1 << 8;
4969 else if (regno1 == 31)
4970 /* Add $ra to insn. */
4971 opcode |= 0x40;
4972 else
a92713e6 4973 return FALSE;
364215c8
RS
4974 regno1 += 1;
4975 if (regno1 == 24)
4976 regno1 = 30;
4977 }
4978 }
364215c8 4979 }
a92713e6 4980 while (match_char (arg, ','));
364215c8
RS
4981
4982 /* Encode args/statics combination. */
4983 if (args & statics)
a92713e6 4984 return FALSE;
364215c8
RS
4985 else if (args == 0xf)
4986 /* All $a0-$a3 are args. */
4987 opcode |= MIPS16_ALL_ARGS << 16;
4988 else if (statics == 0xf)
4989 /* All $a0-$a3 are statics. */
4990 opcode |= MIPS16_ALL_STATICS << 16;
4991 else
4992 {
4993 /* Count arg registers. */
4994 num_args = 0;
4995 while (args & 0x1)
4996 {
4997 args >>= 1;
4998 num_args += 1;
4999 }
5000 if (args != 0)
a92713e6 5001 return FALSE;
364215c8
RS
5002
5003 /* Count static registers. */
5004 num_statics = 0;
5005 while (statics & 0x8)
5006 {
5007 statics = (statics << 1) & 0xf;
5008 num_statics += 1;
5009 }
5010 if (statics != 0)
a92713e6 5011 return FALSE;
364215c8
RS
5012
5013 /* Encode args/statics. */
5014 opcode |= ((num_args << 2) | num_statics) << 16;
5015 }
5016
5017 /* Encode $s0/$s1. */
5018 if (sregs & (1 << 0)) /* $s0 */
5019 opcode |= 0x20;
5020 if (sregs & (1 << 1)) /* $s1 */
5021 opcode |= 0x10;
5022 sregs >>= 2;
5023
5024 /* Encode $s2-$s8. */
5025 num_sregs = 0;
5026 while (sregs & 1)
5027 {
5028 sregs >>= 1;
5029 num_sregs += 1;
5030 }
5031 if (sregs != 0)
a92713e6 5032 return FALSE;
364215c8
RS
5033 opcode |= num_sregs << 24;
5034
5035 /* Encode frame size. */
5036 if (num_frame_sizes == 0)
1a00e612
RS
5037 {
5038 set_insn_error (arg->argnum, _("missing frame size"));
5039 return FALSE;
5040 }
5041 if (num_frame_sizes > 1)
5042 {
5043 set_insn_error (arg->argnum, _("frame size specified twice"));
5044 return FALSE;
5045 }
5046 if ((frame_size & 7) != 0 || frame_size < 0 || frame_size > 0xff * 8)
5047 {
5048 set_insn_error (arg->argnum, _("invalid frame size"));
5049 return FALSE;
5050 }
5051 if (frame_size != 128 || (opcode >> 16) != 0)
364215c8
RS
5052 {
5053 frame_size /= 8;
5054 opcode |= (((frame_size & 0xf0) << 16)
5055 | (frame_size & 0x0f));
5056 }
5057
364215c8
RS
5058 /* Finally build the instruction. */
5059 if ((opcode >> 16) != 0 || frame_size == 0)
5060 opcode |= MIPS16_EXTEND;
5061 arg->insn->insn_opcode = opcode;
a92713e6 5062 return TRUE;
364215c8
RS
5063}
5064
a1d78564
RS
5065/* OP_MDMX_IMM_REG matcher. */
5066
a92713e6 5067static bfd_boolean
a1d78564 5068match_mdmx_imm_reg_operand (struct mips_arg_info *arg,
a92713e6 5069 const struct mips_operand *operand)
a1d78564 5070{
a92713e6 5071 unsigned int regno, uval;
a1d78564
RS
5072 bfd_boolean is_qh;
5073 const struct mips_opcode *opcode;
5074
5075 /* The mips_opcode records whether this is an octobyte or quadhalf
5076 instruction. Start out with that bit in place. */
5077 opcode = arg->insn->insn_mo;
5078 uval = mips_extract_operand (operand, opcode->match);
5079 is_qh = (uval != 0);
5080
a92713e6 5081 if (arg->token->type == OT_REG || arg->token->type == OT_REG_ELEMENT)
a1d78564
RS
5082 {
5083 if ((opcode->membership & INSN_5400)
5084 && strcmp (opcode->name, "rzu.ob") == 0)
5085 {
1a00e612
RS
5086 set_insn_error_i (arg->argnum, _("operand %d must be an immediate"),
5087 arg->argnum);
5088 return FALSE;
a1d78564
RS
5089 }
5090
5091 /* Check whether this is a vector register or a broadcast of
5092 a single element. */
a92713e6 5093 if (arg->token->type == OT_REG_ELEMENT)
a1d78564 5094 {
a92713e6
RS
5095 if (!match_regno (arg, OP_REG_VEC, arg->token->u.reg_element.regno,
5096 &regno))
5097 return FALSE;
5098 if (arg->token->u.reg_element.index > (is_qh ? 3 : 7))
a1d78564 5099 {
1a00e612
RS
5100 set_insn_error (arg->argnum, _("invalid element selector"));
5101 return FALSE;
a1d78564 5102 }
a1d78564 5103 else
a92713e6 5104 uval |= arg->token->u.reg_element.index << (is_qh ? 2 : 1) << 5;
a1d78564
RS
5105 }
5106 else
5107 {
5108 /* A full vector. */
5109 if ((opcode->membership & INSN_5400)
5110 && (strcmp (opcode->name, "sll.ob") == 0
5111 || strcmp (opcode->name, "srl.ob") == 0))
5112 {
1a00e612
RS
5113 set_insn_error_i (arg->argnum, _("operand %d must be scalar"),
5114 arg->argnum);
5115 return FALSE;
a1d78564
RS
5116 }
5117
a92713e6
RS
5118 if (!match_regno (arg, OP_REG_VEC, arg->token->u.regno, &regno))
5119 return FALSE;
a1d78564
RS
5120 if (is_qh)
5121 uval |= MDMX_FMTSEL_VEC_QH << 5;
5122 else
5123 uval |= MDMX_FMTSEL_VEC_OB << 5;
5124 }
a1d78564 5125 uval |= regno;
a92713e6 5126 ++arg->token;
a1d78564
RS
5127 }
5128 else
5129 {
5130 offsetT sval;
5131
1a00e612 5132 if (!match_const_int (arg, &sval))
a92713e6 5133 return FALSE;
a1d78564
RS
5134 if (sval < 0 || sval > 31)
5135 {
1a00e612
RS
5136 match_out_of_range (arg);
5137 return FALSE;
a1d78564
RS
5138 }
5139 uval |= (sval & 31);
5140 if (is_qh)
5141 uval |= MDMX_FMTSEL_IMM_QH << 5;
5142 else
5143 uval |= MDMX_FMTSEL_IMM_OB << 5;
5144 }
5145 insn_insert_operand (arg->insn, operand, uval);
a92713e6 5146 return TRUE;
a1d78564
RS
5147}
5148
5149/* OP_PC matcher. */
5150
a92713e6
RS
5151static bfd_boolean
5152match_pc_operand (struct mips_arg_info *arg)
a1d78564 5153{
a92713e6
RS
5154 if (arg->token->type == OT_REG && (arg->token->u.regno & RTYPE_PC))
5155 {
5156 ++arg->token;
5157 return TRUE;
5158 }
5159 return FALSE;
a1d78564
RS
5160}
5161
5162/* OP_REPEAT_DEST_REG and OP_REPEAT_PREV_REG matcher. OTHER_REGNO is the
5163 register that we need to match. */
5164
a92713e6
RS
5165static bfd_boolean
5166match_tied_reg_operand (struct mips_arg_info *arg, unsigned int other_regno)
a1d78564
RS
5167{
5168 unsigned int regno;
5169
a92713e6 5170 return match_reg (arg, OP_REG_GP, &regno) && regno == other_regno;
a1d78564
RS
5171}
5172
89565f1b
RS
5173/* Read a floating-point constant from S for LI.S or LI.D. LENGTH is
5174 the length of the value in bytes (4 for float, 8 for double) and
5175 USING_GPRS says whether the destination is a GPR rather than an FPR.
5176
5177 Return the constant in IMM and OFFSET as follows:
5178
5179 - If the constant should be loaded via memory, set IMM to O_absent and
5180 OFFSET to the memory address.
5181
5182 - Otherwise, if the constant should be loaded into two 32-bit registers,
5183 set IMM to the O_constant to load into the high register and OFFSET
5184 to the corresponding value for the low register.
5185
5186 - Otherwise, set IMM to the full O_constant and set OFFSET to O_absent.
5187
5188 These constants only appear as the last operand in an instruction,
5189 and every instruction that accepts them in any variant accepts them
5190 in all variants. This means we don't have to worry about backing out
5191 any changes if the instruction does not match. We just match
5192 unconditionally and report an error if the constant is invalid. */
5193
a92713e6
RS
5194static bfd_boolean
5195match_float_constant (struct mips_arg_info *arg, expressionS *imm,
5196 expressionS *offset, int length, bfd_boolean using_gprs)
89565f1b 5197{
a92713e6 5198 char *p;
89565f1b
RS
5199 segT seg, new_seg;
5200 subsegT subseg;
5201 const char *newname;
a92713e6 5202 unsigned char *data;
89565f1b
RS
5203
5204 /* Where the constant is placed is based on how the MIPS assembler
5205 does things:
5206
5207 length == 4 && using_gprs -- immediate value only
5208 length == 8 && using_gprs -- .rdata or immediate value
5209 length == 4 && !using_gprs -- .lit4 or immediate value
5210 length == 8 && !using_gprs -- .lit8 or immediate value
5211
5212 The .lit4 and .lit8 sections are only used if permitted by the
5213 -G argument. */
a92713e6 5214 if (arg->token->type != OT_FLOAT)
1a00e612
RS
5215 {
5216 set_insn_error (arg->argnum, _("floating-point expression required"));
5217 return FALSE;
5218 }
a92713e6
RS
5219
5220 gas_assert (arg->token->u.flt.length == length);
5221 data = arg->token->u.flt.data;
5222 ++arg->token;
89565f1b
RS
5223
5224 /* Handle 32-bit constants for which an immediate value is best. */
5225 if (length == 4
5226 && (using_gprs
5227 || g_switch_value < 4
5228 || (data[0] == 0 && data[1] == 0)
5229 || (data[2] == 0 && data[3] == 0)))
5230 {
5231 imm->X_op = O_constant;
5232 if (!target_big_endian)
5233 imm->X_add_number = bfd_getl32 (data);
5234 else
5235 imm->X_add_number = bfd_getb32 (data);
5236 offset->X_op = O_absent;
a92713e6 5237 return TRUE;
89565f1b
RS
5238 }
5239
5240 /* Handle 64-bit constants for which an immediate value is best. */
5241 if (length == 8
5242 && !mips_disable_float_construction
5243 /* Constants can only be constructed in GPRs and copied
5244 to FPRs if the GPRs are at least as wide as the FPRs.
5245 Force the constant into memory if we are using 64-bit FPRs
5246 but the GPRs are only 32 bits wide. */
5247 /* ??? No longer true with the addition of MTHC1, but this
5248 is legacy code... */
5249 && (using_gprs || !(HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
5250 && ((data[0] == 0 && data[1] == 0)
5251 || (data[2] == 0 && data[3] == 0))
5252 && ((data[4] == 0 && data[5] == 0)
5253 || (data[6] == 0 && data[7] == 0)))
5254 {
5255 /* The value is simple enough to load with a couple of instructions.
5256 If using 32-bit registers, set IMM to the high order 32 bits and
5257 OFFSET to the low order 32 bits. Otherwise, set IMM to the entire
5258 64 bit constant. */
5259 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
5260 {
5261 imm->X_op = O_constant;
5262 offset->X_op = O_constant;
5263 if (!target_big_endian)
5264 {
5265 imm->X_add_number = bfd_getl32 (data + 4);
5266 offset->X_add_number = bfd_getl32 (data);
5267 }
5268 else
5269 {
5270 imm->X_add_number = bfd_getb32 (data);
5271 offset->X_add_number = bfd_getb32 (data + 4);
5272 }
5273 if (offset->X_add_number == 0)
5274 offset->X_op = O_absent;
5275 }
5276 else
5277 {
5278 imm->X_op = O_constant;
5279 if (!target_big_endian)
5280 imm->X_add_number = bfd_getl64 (data);
5281 else
5282 imm->X_add_number = bfd_getb64 (data);
5283 offset->X_op = O_absent;
5284 }
a92713e6 5285 return TRUE;
89565f1b
RS
5286 }
5287
5288 /* Switch to the right section. */
5289 seg = now_seg;
5290 subseg = now_subseg;
5291 if (length == 4)
5292 {
5293 gas_assert (!using_gprs && g_switch_value >= 4);
5294 newname = ".lit4";
5295 }
5296 else
5297 {
5298 if (using_gprs || g_switch_value < 8)
5299 newname = RDATA_SECTION_NAME;
5300 else
5301 newname = ".lit8";
5302 }
5303
5304 new_seg = subseg_new (newname, (subsegT) 0);
5305 bfd_set_section_flags (stdoutput, new_seg,
5306 SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA);
5307 frag_align (length == 4 ? 2 : 3, 0, 0);
5308 if (strncmp (TARGET_OS, "elf", 3) != 0)
5309 record_alignment (new_seg, 4);
5310 else
5311 record_alignment (new_seg, length == 4 ? 2 : 3);
5312 if (seg == now_seg)
5313 as_bad (_("Can't use floating point insn in this section"));
5314
5315 /* Set the argument to the current address in the section. */
5316 imm->X_op = O_absent;
5317 offset->X_op = O_symbol;
5318 offset->X_add_symbol = symbol_temp_new_now ();
5319 offset->X_add_number = 0;
5320
5321 /* Put the floating point number into the section. */
5322 p = frag_more (length);
5323 memcpy (p, data, length);
5324
5325 /* Switch back to the original section. */
5326 subseg_set (seg, subseg);
a92713e6 5327 return TRUE;
89565f1b
RS
5328}
5329
14daeee3
RS
5330/* OP_VU0_SUFFIX and OP_VU0_MATCH_SUFFIX matcher; MATCH_P selects between
5331 them. */
5332
5333static bfd_boolean
5334match_vu0_suffix_operand (struct mips_arg_info *arg,
5335 const struct mips_operand *operand,
5336 bfd_boolean match_p)
5337{
5338 unsigned int uval;
5339
5340 /* The operand can be an XYZW mask or a single 2-bit channel index
5341 (with X being 0). */
5342 gas_assert (operand->size == 2 || operand->size == 4);
5343
ee5734f0 5344 /* The suffix can be omitted when it is already part of the opcode. */
14daeee3 5345 if (arg->token->type != OT_CHANNELS)
ee5734f0 5346 return match_p;
14daeee3
RS
5347
5348 uval = arg->token->u.channels;
5349 if (operand->size == 2)
5350 {
5351 /* Check that a single bit is set and convert it into a 2-bit index. */
5352 if ((uval & -uval) != uval)
5353 return FALSE;
5354 uval = 4 - ffs (uval);
5355 }
5356
5357 if (match_p && insn_extract_operand (arg->insn, operand) != uval)
5358 return FALSE;
5359
5360 ++arg->token;
5361 if (!match_p)
5362 insn_insert_operand (arg->insn, operand, uval);
5363 return TRUE;
5364}
5365
a1d78564
RS
5366/* S is the text seen for ARG. Match it against OPERAND. Return the end
5367 of the argument text if the match is successful, otherwise return null. */
5368
a92713e6 5369static bfd_boolean
a1d78564 5370match_operand (struct mips_arg_info *arg,
a92713e6 5371 const struct mips_operand *operand)
a1d78564
RS
5372{
5373 switch (operand->type)
5374 {
5375 case OP_INT:
a92713e6 5376 return match_int_operand (arg, operand);
a1d78564
RS
5377
5378 case OP_MAPPED_INT:
a92713e6 5379 return match_mapped_int_operand (arg, operand);
a1d78564
RS
5380
5381 case OP_MSB:
a92713e6 5382 return match_msb_operand (arg, operand);
a1d78564
RS
5383
5384 case OP_REG:
0f35dbc4 5385 case OP_OPTIONAL_REG:
a92713e6 5386 return match_reg_operand (arg, operand);
a1d78564
RS
5387
5388 case OP_REG_PAIR:
a92713e6 5389 return match_reg_pair_operand (arg, operand);
a1d78564
RS
5390
5391 case OP_PCREL:
a92713e6 5392 return match_pcrel_operand (arg);
a1d78564
RS
5393
5394 case OP_PERF_REG:
a92713e6 5395 return match_perf_reg_operand (arg, operand);
a1d78564
RS
5396
5397 case OP_ADDIUSP_INT:
a92713e6 5398 return match_addiusp_operand (arg, operand);
a1d78564
RS
5399
5400 case OP_CLO_CLZ_DEST:
a92713e6 5401 return match_clo_clz_dest_operand (arg, operand);
a1d78564
RS
5402
5403 case OP_LWM_SWM_LIST:
a92713e6 5404 return match_lwm_swm_list_operand (arg, operand);
a1d78564
RS
5405
5406 case OP_ENTRY_EXIT_LIST:
a92713e6 5407 return match_entry_exit_operand (arg, operand);
364215c8 5408
a1d78564 5409 case OP_SAVE_RESTORE_LIST:
a92713e6 5410 return match_save_restore_list_operand (arg);
a1d78564
RS
5411
5412 case OP_MDMX_IMM_REG:
a92713e6 5413 return match_mdmx_imm_reg_operand (arg, operand);
a1d78564
RS
5414
5415 case OP_REPEAT_DEST_REG:
a92713e6 5416 return match_tied_reg_operand (arg, arg->dest_regno);
a1d78564
RS
5417
5418 case OP_REPEAT_PREV_REG:
a92713e6 5419 return match_tied_reg_operand (arg, arg->last_regno);
a1d78564
RS
5420
5421 case OP_PC:
a92713e6 5422 return match_pc_operand (arg);
14daeee3
RS
5423
5424 case OP_VU0_SUFFIX:
5425 return match_vu0_suffix_operand (arg, operand, FALSE);
5426
5427 case OP_VU0_MATCH_SUFFIX:
5428 return match_vu0_suffix_operand (arg, operand, TRUE);
a1d78564
RS
5429 }
5430 abort ();
5431}
5432
5433/* ARG is the state after successfully matching an instruction.
5434 Issue any queued-up warnings. */
5435
5436static void
5437check_completed_insn (struct mips_arg_info *arg)
5438{
5439 if (arg->seen_at)
5440 {
5441 if (AT == ATREG)
5442 as_warn (_("Used $at without \".set noat\""));
5443 else
5444 as_warn (_("Used $%u with \".set at=$%u\""), AT, AT);
5445 }
5446}
a1d78564 5447
85fcb30f
RS
5448/* Return true if modifying general-purpose register REG needs a delay. */
5449
5450static bfd_boolean
5451reg_needs_delay (unsigned int reg)
5452{
5453 unsigned long prev_pinfo;
5454
5455 prev_pinfo = history[0].insn_mo->pinfo;
5456 if (!mips_opts.noreorder
5457 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY) && !gpr_interlocks)
5458 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY) && !cop_interlocks))
5459 && (gpr_write_mask (&history[0]) & (1 << reg)))
5460 return TRUE;
5461
5462 return FALSE;
5463}
5464
71400594
RS
5465/* Classify an instruction according to the FIX_VR4120_* enumeration.
5466 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
5467 by VR4120 errata. */
4d7206a2 5468
71400594
RS
5469static unsigned int
5470classify_vr4120_insn (const char *name)
252b5132 5471{
71400594
RS
5472 if (strncmp (name, "macc", 4) == 0)
5473 return FIX_VR4120_MACC;
5474 if (strncmp (name, "dmacc", 5) == 0)
5475 return FIX_VR4120_DMACC;
5476 if (strncmp (name, "mult", 4) == 0)
5477 return FIX_VR4120_MULT;
5478 if (strncmp (name, "dmult", 5) == 0)
5479 return FIX_VR4120_DMULT;
5480 if (strstr (name, "div"))
5481 return FIX_VR4120_DIV;
5482 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
5483 return FIX_VR4120_MTHILO;
5484 return NUM_FIX_VR4120_CLASSES;
5485}
252b5132 5486
ff239038
CM
5487#define INSN_ERET 0x42000018
5488#define INSN_DERET 0x4200001f
5489
71400594
RS
5490/* Return the number of instructions that must separate INSN1 and INSN2,
5491 where INSN1 is the earlier instruction. Return the worst-case value
5492 for any INSN2 if INSN2 is null. */
252b5132 5493
71400594
RS
5494static unsigned int
5495insns_between (const struct mips_cl_insn *insn1,
5496 const struct mips_cl_insn *insn2)
5497{
5498 unsigned long pinfo1, pinfo2;
4c260379 5499 unsigned int mask;
71400594 5500
85fcb30f
RS
5501 /* If INFO2 is null, pessimistically assume that all flags are set for
5502 the second instruction. */
71400594
RS
5503 pinfo1 = insn1->insn_mo->pinfo;
5504 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
252b5132 5505
71400594
RS
5506 /* For most targets, write-after-read dependencies on the HI and LO
5507 registers must be separated by at least two instructions. */
5508 if (!hilo_interlocks)
252b5132 5509 {
71400594
RS
5510 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
5511 return 2;
5512 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
5513 return 2;
5514 }
5515
5516 /* If we're working around r7000 errata, there must be two instructions
5517 between an mfhi or mflo and any instruction that uses the result. */
5518 if (mips_7000_hilo_fix
df58fc94 5519 && !mips_opts.micromips
71400594 5520 && MF_HILO_INSN (pinfo1)
85fcb30f 5521 && (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1))))
71400594
RS
5522 return 2;
5523
ff239038
CM
5524 /* If we're working around 24K errata, one instruction is required
5525 if an ERET or DERET is followed by a branch instruction. */
df58fc94 5526 if (mips_fix_24k && !mips_opts.micromips)
ff239038
CM
5527 {
5528 if (insn1->insn_opcode == INSN_ERET
5529 || insn1->insn_opcode == INSN_DERET)
5530 {
5531 if (insn2 == NULL
5532 || insn2->insn_opcode == INSN_ERET
5533 || insn2->insn_opcode == INSN_DERET
11625dd8 5534 || delayed_branch_p (insn2))
ff239038
CM
5535 return 1;
5536 }
5537 }
5538
71400594
RS
5539 /* If working around VR4120 errata, check for combinations that need
5540 a single intervening instruction. */
df58fc94 5541 if (mips_fix_vr4120 && !mips_opts.micromips)
71400594
RS
5542 {
5543 unsigned int class1, class2;
252b5132 5544
71400594
RS
5545 class1 = classify_vr4120_insn (insn1->insn_mo->name);
5546 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
252b5132 5547 {
71400594
RS
5548 if (insn2 == NULL)
5549 return 1;
5550 class2 = classify_vr4120_insn (insn2->insn_mo->name);
5551 if (vr4120_conflicts[class1] & (1 << class2))
5552 return 1;
252b5132 5553 }
71400594
RS
5554 }
5555
df58fc94 5556 if (!HAVE_CODE_COMPRESSION)
71400594
RS
5557 {
5558 /* Check for GPR or coprocessor load delays. All such delays
5559 are on the RT register. */
5560 /* Itbl support may require additional care here. */
5561 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
5562 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
252b5132 5563 {
85fcb30f 5564 if (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1)))
71400594
RS
5565 return 1;
5566 }
5567
5568 /* Check for generic coprocessor hazards.
5569
5570 This case is not handled very well. There is no special
5571 knowledge of CP0 handling, and the coprocessors other than
5572 the floating point unit are not distinguished at all. */
5573 /* Itbl support may require additional care here. FIXME!
5574 Need to modify this to include knowledge about
5575 user specified delays! */
5576 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
5577 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
5578 {
5579 /* Handle cases where INSN1 writes to a known general coprocessor
5580 register. There must be a one instruction delay before INSN2
5581 if INSN2 reads that register, otherwise no delay is needed. */
4c260379
RS
5582 mask = fpr_write_mask (insn1);
5583 if (mask != 0)
252b5132 5584 {
4c260379 5585 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
71400594 5586 return 1;
252b5132
RH
5587 }
5588 else
5589 {
71400594
RS
5590 /* Read-after-write dependencies on the control registers
5591 require a two-instruction gap. */
5592 if ((pinfo1 & INSN_WRITE_COND_CODE)
5593 && (pinfo2 & INSN_READ_COND_CODE))
5594 return 2;
5595
5596 /* We don't know exactly what INSN1 does. If INSN2 is
5597 also a coprocessor instruction, assume there must be
5598 a one instruction gap. */
5599 if (pinfo2 & INSN_COP)
5600 return 1;
252b5132
RH
5601 }
5602 }
6b76fefe 5603
71400594
RS
5604 /* Check for read-after-write dependencies on the coprocessor
5605 control registers in cases where INSN1 does not need a general
5606 coprocessor delay. This means that INSN1 is a floating point
5607 comparison instruction. */
5608 /* Itbl support may require additional care here. */
5609 else if (!cop_interlocks
5610 && (pinfo1 & INSN_WRITE_COND_CODE)
5611 && (pinfo2 & INSN_READ_COND_CODE))
5612 return 1;
5613 }
6b76fefe 5614
71400594
RS
5615 return 0;
5616}
6b76fefe 5617
7d8e00cf
RS
5618/* Return the number of nops that would be needed to work around the
5619 VR4130 mflo/mfhi errata if instruction INSN immediately followed
932d1a1b
RS
5620 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
5621 that are contained within the first IGNORE instructions of HIST. */
7d8e00cf
RS
5622
5623static int
932d1a1b 5624nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
7d8e00cf
RS
5625 const struct mips_cl_insn *insn)
5626{
4c260379
RS
5627 int i, j;
5628 unsigned int mask;
7d8e00cf
RS
5629
5630 /* Check if the instruction writes to HI or LO. MTHI and MTLO
5631 are not affected by the errata. */
5632 if (insn != 0
5633 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
5634 || strcmp (insn->insn_mo->name, "mtlo") == 0
5635 || strcmp (insn->insn_mo->name, "mthi") == 0))
5636 return 0;
5637
5638 /* Search for the first MFLO or MFHI. */
5639 for (i = 0; i < MAX_VR4130_NOPS; i++)
91d6fa6a 5640 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
7d8e00cf
RS
5641 {
5642 /* Extract the destination register. */
4c260379 5643 mask = gpr_write_mask (&hist[i]);
7d8e00cf
RS
5644
5645 /* No nops are needed if INSN reads that register. */
4c260379 5646 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
7d8e00cf
RS
5647 return 0;
5648
5649 /* ...or if any of the intervening instructions do. */
5650 for (j = 0; j < i; j++)
4c260379 5651 if (gpr_read_mask (&hist[j]) & mask)
7d8e00cf
RS
5652 return 0;
5653
932d1a1b
RS
5654 if (i >= ignore)
5655 return MAX_VR4130_NOPS - i;
7d8e00cf
RS
5656 }
5657 return 0;
5658}
5659
15be625d
CM
5660#define BASE_REG_EQ(INSN1, INSN2) \
5661 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
5662 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
5663
5664/* Return the minimum alignment for this store instruction. */
5665
5666static int
5667fix_24k_align_to (const struct mips_opcode *mo)
5668{
5669 if (strcmp (mo->name, "sh") == 0)
5670 return 2;
5671
5672 if (strcmp (mo->name, "swc1") == 0
5673 || strcmp (mo->name, "swc2") == 0
5674 || strcmp (mo->name, "sw") == 0
5675 || strcmp (mo->name, "sc") == 0
5676 || strcmp (mo->name, "s.s") == 0)
5677 return 4;
5678
5679 if (strcmp (mo->name, "sdc1") == 0
5680 || strcmp (mo->name, "sdc2") == 0
5681 || strcmp (mo->name, "s.d") == 0)
5682 return 8;
5683
5684 /* sb, swl, swr */
5685 return 1;
5686}
5687
5688struct fix_24k_store_info
5689 {
5690 /* Immediate offset, if any, for this store instruction. */
5691 short off;
5692 /* Alignment required by this store instruction. */
5693 int align_to;
5694 /* True for register offsets. */
5695 int register_offset;
5696 };
5697
5698/* Comparison function used by qsort. */
5699
5700static int
5701fix_24k_sort (const void *a, const void *b)
5702{
5703 const struct fix_24k_store_info *pos1 = a;
5704 const struct fix_24k_store_info *pos2 = b;
5705
5706 return (pos1->off - pos2->off);
5707}
5708
5709/* INSN is a store instruction. Try to record the store information
5710 in STINFO. Return false if the information isn't known. */
5711
5712static bfd_boolean
5713fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
ab9794cf 5714 const struct mips_cl_insn *insn)
15be625d
CM
5715{
5716 /* The instruction must have a known offset. */
5717 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
5718 return FALSE;
5719
5720 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
5721 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
5722 return TRUE;
5723}
5724
932d1a1b
RS
5725/* Return the number of nops that would be needed to work around the 24k
5726 "lost data on stores during refill" errata if instruction INSN
5727 immediately followed the 2 instructions described by HIST.
5728 Ignore hazards that are contained within the first IGNORE
5729 instructions of HIST.
5730
5731 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
5732 for the data cache refills and store data. The following describes
5733 the scenario where the store data could be lost.
5734
5735 * A data cache miss, due to either a load or a store, causing fill
5736 data to be supplied by the memory subsystem
5737 * The first three doublewords of fill data are returned and written
5738 into the cache
5739 * A sequence of four stores occurs in consecutive cycles around the
5740 final doubleword of the fill:
5741 * Store A
5742 * Store B
5743 * Store C
5744 * Zero, One or more instructions
5745 * Store D
5746
5747 The four stores A-D must be to different doublewords of the line that
5748 is being filled. The fourth instruction in the sequence above permits
5749 the fill of the final doubleword to be transferred from the FSB into
5750 the cache. In the sequence above, the stores may be either integer
5751 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
5752 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
5753 different doublewords on the line. If the floating point unit is
5754 running in 1:2 mode, it is not possible to create the sequence above
5755 using only floating point store instructions.
15be625d
CM
5756
5757 In this case, the cache line being filled is incorrectly marked
5758 invalid, thereby losing the data from any store to the line that
5759 occurs between the original miss and the completion of the five
5760 cycle sequence shown above.
5761
932d1a1b 5762 The workarounds are:
15be625d 5763
932d1a1b
RS
5764 * Run the data cache in write-through mode.
5765 * Insert a non-store instruction between
5766 Store A and Store B or Store B and Store C. */
15be625d
CM
5767
5768static int
932d1a1b 5769nops_for_24k (int ignore, const struct mips_cl_insn *hist,
15be625d
CM
5770 const struct mips_cl_insn *insn)
5771{
5772 struct fix_24k_store_info pos[3];
5773 int align, i, base_offset;
5774
932d1a1b
RS
5775 if (ignore >= 2)
5776 return 0;
5777
ab9794cf
RS
5778 /* If the previous instruction wasn't a store, there's nothing to
5779 worry about. */
15be625d
CM
5780 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
5781 return 0;
5782
ab9794cf
RS
5783 /* If the instructions after the previous one are unknown, we have
5784 to assume the worst. */
5785 if (!insn)
15be625d
CM
5786 return 1;
5787
ab9794cf
RS
5788 /* Check whether we are dealing with three consecutive stores. */
5789 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
5790 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
15be625d
CM
5791 return 0;
5792
5793 /* If we don't know the relationship between the store addresses,
5794 assume the worst. */
ab9794cf 5795 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
15be625d
CM
5796 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
5797 return 1;
5798
5799 if (!fix_24k_record_store_info (&pos[0], insn)
5800 || !fix_24k_record_store_info (&pos[1], &hist[0])
5801 || !fix_24k_record_store_info (&pos[2], &hist[1]))
5802 return 1;
5803
5804 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
5805
5806 /* Pick a value of ALIGN and X such that all offsets are adjusted by
5807 X bytes and such that the base register + X is known to be aligned
5808 to align bytes. */
5809
5810 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
5811 align = 8;
5812 else
5813 {
5814 align = pos[0].align_to;
5815 base_offset = pos[0].off;
5816 for (i = 1; i < 3; i++)
5817 if (align < pos[i].align_to)
5818 {
5819 align = pos[i].align_to;
5820 base_offset = pos[i].off;
5821 }
5822 for (i = 0; i < 3; i++)
5823 pos[i].off -= base_offset;
5824 }
5825
5826 pos[0].off &= ~align + 1;
5827 pos[1].off &= ~align + 1;
5828 pos[2].off &= ~align + 1;
5829
5830 /* If any two stores write to the same chunk, they also write to the
5831 same doubleword. The offsets are still sorted at this point. */
5832 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
5833 return 0;
5834
5835 /* A range of at least 9 bytes is needed for the stores to be in
5836 non-overlapping doublewords. */
5837 if (pos[2].off - pos[0].off <= 8)
5838 return 0;
5839
5840 if (pos[2].off - pos[1].off >= 24
5841 || pos[1].off - pos[0].off >= 24
5842 || pos[2].off - pos[0].off >= 32)
5843 return 0;
5844
5845 return 1;
5846}
5847
71400594 5848/* Return the number of nops that would be needed if instruction INSN
91d6fa6a 5849 immediately followed the MAX_NOPS instructions given by HIST,
932d1a1b
RS
5850 where HIST[0] is the most recent instruction. Ignore hazards
5851 between INSN and the first IGNORE instructions in HIST.
5852
5853 If INSN is null, return the worse-case number of nops for any
5854 instruction. */
bdaaa2e1 5855
71400594 5856static int
932d1a1b 5857nops_for_insn (int ignore, const struct mips_cl_insn *hist,
71400594
RS
5858 const struct mips_cl_insn *insn)
5859{
5860 int i, nops, tmp_nops;
bdaaa2e1 5861
71400594 5862 nops = 0;
932d1a1b 5863 for (i = ignore; i < MAX_DELAY_NOPS; i++)
65b02341 5864 {
91d6fa6a 5865 tmp_nops = insns_between (hist + i, insn) - i;
65b02341
RS
5866 if (tmp_nops > nops)
5867 nops = tmp_nops;
5868 }
7d8e00cf 5869
df58fc94 5870 if (mips_fix_vr4130 && !mips_opts.micromips)
7d8e00cf 5871 {
932d1a1b 5872 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
7d8e00cf
RS
5873 if (tmp_nops > nops)
5874 nops = tmp_nops;
5875 }
5876
df58fc94 5877 if (mips_fix_24k && !mips_opts.micromips)
15be625d 5878 {
932d1a1b 5879 tmp_nops = nops_for_24k (ignore, hist, insn);
15be625d
CM
5880 if (tmp_nops > nops)
5881 nops = tmp_nops;
5882 }
5883
71400594
RS
5884 return nops;
5885}
252b5132 5886
71400594 5887/* The variable arguments provide NUM_INSNS extra instructions that
91d6fa6a 5888 might be added to HIST. Return the largest number of nops that
932d1a1b
RS
5889 would be needed after the extended sequence, ignoring hazards
5890 in the first IGNORE instructions. */
252b5132 5891
71400594 5892static int
932d1a1b
RS
5893nops_for_sequence (int num_insns, int ignore,
5894 const struct mips_cl_insn *hist, ...)
71400594
RS
5895{
5896 va_list args;
5897 struct mips_cl_insn buffer[MAX_NOPS];
5898 struct mips_cl_insn *cursor;
5899 int nops;
5900
91d6fa6a 5901 va_start (args, hist);
71400594 5902 cursor = buffer + num_insns;
91d6fa6a 5903 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
71400594
RS
5904 while (cursor > buffer)
5905 *--cursor = *va_arg (args, const struct mips_cl_insn *);
5906
932d1a1b 5907 nops = nops_for_insn (ignore, buffer, NULL);
71400594
RS
5908 va_end (args);
5909 return nops;
5910}
252b5132 5911
71400594
RS
5912/* Like nops_for_insn, but if INSN is a branch, take into account the
5913 worst-case delay for the branch target. */
252b5132 5914
71400594 5915static int
932d1a1b 5916nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
71400594
RS
5917 const struct mips_cl_insn *insn)
5918{
5919 int nops, tmp_nops;
60b63b72 5920
932d1a1b 5921 nops = nops_for_insn (ignore, hist, insn);
11625dd8 5922 if (delayed_branch_p (insn))
71400594 5923 {
932d1a1b 5924 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
14fe068b 5925 hist, insn, get_delay_slot_nop (insn));
71400594
RS
5926 if (tmp_nops > nops)
5927 nops = tmp_nops;
5928 }
11625dd8 5929 else if (compact_branch_p (insn))
71400594 5930 {
932d1a1b 5931 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
71400594
RS
5932 if (tmp_nops > nops)
5933 nops = tmp_nops;
5934 }
5935 return nops;
5936}
5937
c67a084a
NC
5938/* Fix NOP issue: Replace nops by "or at,at,zero". */
5939
5940static void
5941fix_loongson2f_nop (struct mips_cl_insn * ip)
5942{
df58fc94 5943 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
5944 if (strcmp (ip->insn_mo->name, "nop") == 0)
5945 ip->insn_opcode = LOONGSON2F_NOP_INSN;
5946}
5947
5948/* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
5949 jr target pc &= 'hffff_ffff_cfff_ffff. */
5950
5951static void
5952fix_loongson2f_jump (struct mips_cl_insn * ip)
5953{
df58fc94 5954 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
5955 if (strcmp (ip->insn_mo->name, "j") == 0
5956 || strcmp (ip->insn_mo->name, "jr") == 0
5957 || strcmp (ip->insn_mo->name, "jalr") == 0)
5958 {
5959 int sreg;
5960 expressionS ep;
5961
5962 if (! mips_opts.at)
5963 return;
5964
df58fc94 5965 sreg = EXTRACT_OPERAND (0, RS, *ip);
c67a084a
NC
5966 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
5967 return;
5968
5969 ep.X_op = O_constant;
5970 ep.X_add_number = 0xcfff0000;
5971 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
5972 ep.X_add_number = 0xffff;
5973 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
5974 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
5975 }
5976}
5977
5978static void
5979fix_loongson2f (struct mips_cl_insn * ip)
5980{
5981 if (mips_fix_loongson2f_nop)
5982 fix_loongson2f_nop (ip);
5983
5984 if (mips_fix_loongson2f_jump)
5985 fix_loongson2f_jump (ip);
5986}
5987
a4e06468
RS
5988/* IP is a branch that has a delay slot, and we need to fill it
5989 automatically. Return true if we can do that by swapping IP
e407c74b
NC
5990 with the previous instruction.
5991 ADDRESS_EXPR is an operand of the instruction to be used with
5992 RELOC_TYPE. */
a4e06468
RS
5993
5994static bfd_boolean
e407c74b 5995can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 5996 bfd_reloc_code_real_type *reloc_type)
a4e06468 5997{
2b0c8b40 5998 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
a4e06468
RS
5999 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
6000
6001 /* -O2 and above is required for this optimization. */
6002 if (mips_optimize < 2)
6003 return FALSE;
6004
6005 /* If we have seen .set volatile or .set nomove, don't optimize. */
6006 if (mips_opts.nomove)
6007 return FALSE;
6008
6009 /* We can't swap if the previous instruction's position is fixed. */
6010 if (history[0].fixed_p)
6011 return FALSE;
6012
6013 /* If the previous previous insn was in a .set noreorder, we can't
6014 swap. Actually, the MIPS assembler will swap in this situation.
6015 However, gcc configured -with-gnu-as will generate code like
6016
6017 .set noreorder
6018 lw $4,XXX
6019 .set reorder
6020 INSN
6021 bne $4,$0,foo
6022
6023 in which we can not swap the bne and INSN. If gcc is not configured
6024 -with-gnu-as, it does not output the .set pseudo-ops. */
6025 if (history[1].noreorder_p)
6026 return FALSE;
6027
87333bb7
MR
6028 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
6029 This means that the previous instruction was a 4-byte one anyhow. */
a4e06468
RS
6030 if (mips_opts.mips16 && history[0].fixp[0])
6031 return FALSE;
6032
6033 /* If the branch is itself the target of a branch, we can not swap.
6034 We cheat on this; all we check for is whether there is a label on
6035 this instruction. If there are any branches to anything other than
6036 a label, users must use .set noreorder. */
6037 if (seg_info (now_seg)->label_list)
6038 return FALSE;
6039
6040 /* If the previous instruction is in a variant frag other than this
2309ddf2 6041 branch's one, we cannot do the swap. This does not apply to
9301f9c3
MR
6042 MIPS16 code, which uses variant frags for different purposes. */
6043 if (!mips_opts.mips16
a4e06468
RS
6044 && history[0].frag
6045 && history[0].frag->fr_type == rs_machine_dependent)
6046 return FALSE;
6047
bcd530a7
RS
6048 /* We do not swap with instructions that cannot architecturally
6049 be placed in a branch delay slot, such as SYNC or ERET. We
6050 also refrain from swapping with a trap instruction, since it
6051 complicates trap handlers to have the trap instruction be in
6052 a delay slot. */
a4e06468 6053 prev_pinfo = history[0].insn_mo->pinfo;
bcd530a7 6054 if (prev_pinfo & INSN_NO_DELAY_SLOT)
a4e06468
RS
6055 return FALSE;
6056
6057 /* Check for conflicts between the branch and the instructions
6058 before the candidate delay slot. */
6059 if (nops_for_insn (0, history + 1, ip) > 0)
6060 return FALSE;
6061
6062 /* Check for conflicts between the swapped sequence and the
6063 target of the branch. */
6064 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
6065 return FALSE;
6066
6067 /* If the branch reads a register that the previous
6068 instruction sets, we can not swap. */
6069 gpr_read = gpr_read_mask (ip);
6070 prev_gpr_write = gpr_write_mask (&history[0]);
6071 if (gpr_read & prev_gpr_write)
6072 return FALSE;
6073
6074 /* If the branch writes a register that the previous
6075 instruction sets, we can not swap. */
6076 gpr_write = gpr_write_mask (ip);
6077 if (gpr_write & prev_gpr_write)
6078 return FALSE;
6079
6080 /* If the branch writes a register that the previous
6081 instruction reads, we can not swap. */
6082 prev_gpr_read = gpr_read_mask (&history[0]);
6083 if (gpr_write & prev_gpr_read)
6084 return FALSE;
6085
6086 /* If one instruction sets a condition code and the
6087 other one uses a condition code, we can not swap. */
6088 pinfo = ip->insn_mo->pinfo;
6089 if ((pinfo & INSN_READ_COND_CODE)
6090 && (prev_pinfo & INSN_WRITE_COND_CODE))
6091 return FALSE;
6092 if ((pinfo & INSN_WRITE_COND_CODE)
6093 && (prev_pinfo & INSN_READ_COND_CODE))
6094 return FALSE;
6095
6096 /* If the previous instruction uses the PC, we can not swap. */
2b0c8b40 6097 prev_pinfo2 = history[0].insn_mo->pinfo2;
26545944 6098 if (prev_pinfo2 & INSN2_READ_PC)
2b0c8b40 6099 return FALSE;
a4e06468 6100
df58fc94
RS
6101 /* If the previous instruction has an incorrect size for a fixed
6102 branch delay slot in microMIPS mode, we cannot swap. */
2309ddf2
MR
6103 pinfo2 = ip->insn_mo->pinfo2;
6104 if (mips_opts.micromips
6105 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
6106 && insn_length (history) != 2)
6107 return FALSE;
6108 if (mips_opts.micromips
6109 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
6110 && insn_length (history) != 4)
6111 return FALSE;
6112
e407c74b
NC
6113 /* On R5900 short loops need to be fixed by inserting a nop in
6114 the branch delay slots.
6115 A short loop can be terminated too early. */
6116 if (mips_opts.arch == CPU_R5900
6117 /* Check if instruction has a parameter, ignore "j $31". */
6118 && (address_expr != NULL)
6119 /* Parameter must be 16 bit. */
6120 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
6121 /* Branch to same segment. */
6122 && (S_GET_SEGMENT(address_expr->X_add_symbol) == now_seg)
6123 /* Branch to same code fragment. */
6124 && (symbol_get_frag(address_expr->X_add_symbol) == frag_now)
6125 /* Can only calculate branch offset if value is known. */
6126 && symbol_constant_p(address_expr->X_add_symbol)
6127 /* Check if branch is really conditional. */
6128 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
6129 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
6130 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
6131 {
6132 int distance;
6133 /* Check if loop is shorter than 6 instructions including
6134 branch and delay slot. */
6135 distance = frag_now_fix() - S_GET_VALUE(address_expr->X_add_symbol);
6136 if (distance <= 20)
6137 {
6138 int i;
6139 int rv;
6140
6141 rv = FALSE;
6142 /* When the loop includes branches or jumps,
6143 it is not a short loop. */
6144 for (i = 0; i < (distance / 4); i++)
6145 {
6146 if ((history[i].cleared_p)
6147 || delayed_branch_p(&history[i]))
6148 {
6149 rv = TRUE;
6150 break;
6151 }
6152 }
6153 if (rv == FALSE)
6154 {
6155 /* Insert nop after branch to fix short loop. */
6156 return FALSE;
6157 }
6158 }
6159 }
6160
a4e06468
RS
6161 return TRUE;
6162}
6163
e407c74b
NC
6164/* Decide how we should add IP to the instruction stream.
6165 ADDRESS_EXPR is an operand of the instruction to be used with
6166 RELOC_TYPE. */
a4e06468
RS
6167
6168static enum append_method
e407c74b 6169get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 6170 bfd_reloc_code_real_type *reloc_type)
a4e06468 6171{
a4e06468
RS
6172 /* The relaxed version of a macro sequence must be inherently
6173 hazard-free. */
6174 if (mips_relax.sequence == 2)
6175 return APPEND_ADD;
6176
6177 /* We must not dabble with instructions in a ".set norerorder" block. */
6178 if (mips_opts.noreorder)
6179 return APPEND_ADD;
6180
6181 /* Otherwise, it's our responsibility to fill branch delay slots. */
11625dd8 6182 if (delayed_branch_p (ip))
a4e06468 6183 {
e407c74b
NC
6184 if (!branch_likely_p (ip)
6185 && can_swap_branch_p (ip, address_expr, reloc_type))
a4e06468
RS
6186 return APPEND_SWAP;
6187
6188 if (mips_opts.mips16
6189 && ISA_SUPPORTS_MIPS16E
fc76e730 6190 && gpr_read_mask (ip) != 0)
a4e06468
RS
6191 return APPEND_ADD_COMPACT;
6192
6193 return APPEND_ADD_WITH_NOP;
6194 }
6195
a4e06468
RS
6196 return APPEND_ADD;
6197}
6198
ceb94aa5
RS
6199/* IP is a MIPS16 instruction whose opcode we have just changed.
6200 Point IP->insn_mo to the new opcode's definition. */
6201
6202static void
6203find_altered_mips16_opcode (struct mips_cl_insn *ip)
6204{
6205 const struct mips_opcode *mo, *end;
6206
6207 end = &mips16_opcodes[bfd_mips16_num_opcodes];
6208 for (mo = ip->insn_mo; mo < end; mo++)
6209 if ((ip->insn_opcode & mo->mask) == mo->match)
6210 {
6211 ip->insn_mo = mo;
6212 return;
6213 }
6214 abort ();
6215}
6216
df58fc94
RS
6217/* For microMIPS macros, we need to generate a local number label
6218 as the target of branches. */
6219#define MICROMIPS_LABEL_CHAR '\037'
6220static unsigned long micromips_target_label;
6221static char micromips_target_name[32];
6222
6223static char *
6224micromips_label_name (void)
6225{
6226 char *p = micromips_target_name;
6227 char symbol_name_temporary[24];
6228 unsigned long l;
6229 int i;
6230
6231 if (*p)
6232 return p;
6233
6234 i = 0;
6235 l = micromips_target_label;
6236#ifdef LOCAL_LABEL_PREFIX
6237 *p++ = LOCAL_LABEL_PREFIX;
6238#endif
6239 *p++ = 'L';
6240 *p++ = MICROMIPS_LABEL_CHAR;
6241 do
6242 {
6243 symbol_name_temporary[i++] = l % 10 + '0';
6244 l /= 10;
6245 }
6246 while (l != 0);
6247 while (i > 0)
6248 *p++ = symbol_name_temporary[--i];
6249 *p = '\0';
6250
6251 return micromips_target_name;
6252}
6253
6254static void
6255micromips_label_expr (expressionS *label_expr)
6256{
6257 label_expr->X_op = O_symbol;
6258 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
6259 label_expr->X_add_number = 0;
6260}
6261
6262static void
6263micromips_label_inc (void)
6264{
6265 micromips_target_label++;
6266 *micromips_target_name = '\0';
6267}
6268
6269static void
6270micromips_add_label (void)
6271{
6272 symbolS *s;
6273
6274 s = colon (micromips_label_name ());
6275 micromips_label_inc ();
f3ded42a 6276 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
df58fc94
RS
6277}
6278
6279/* If assembling microMIPS code, then return the microMIPS reloc
6280 corresponding to the requested one if any. Otherwise return
6281 the reloc unchanged. */
6282
6283static bfd_reloc_code_real_type
6284micromips_map_reloc (bfd_reloc_code_real_type reloc)
6285{
6286 static const bfd_reloc_code_real_type relocs[][2] =
6287 {
6288 /* Keep sorted incrementally by the left-hand key. */
6289 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
6290 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
6291 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
6292 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
6293 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
6294 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
6295 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
6296 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
6297 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
6298 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
6299 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
6300 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
6301 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
6302 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
6303 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
6304 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
6305 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
6306 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
6307 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
6308 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
6309 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
6310 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
6311 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
6312 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
6313 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
6314 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
6315 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
6316 };
6317 bfd_reloc_code_real_type r;
6318 size_t i;
6319
6320 if (!mips_opts.micromips)
6321 return reloc;
6322 for (i = 0; i < ARRAY_SIZE (relocs); i++)
6323 {
6324 r = relocs[i][0];
6325 if (r > reloc)
6326 return reloc;
6327 if (r == reloc)
6328 return relocs[i][1];
6329 }
6330 return reloc;
6331}
6332
b886a2ab
RS
6333/* Try to resolve relocation RELOC against constant OPERAND at assembly time.
6334 Return true on success, storing the resolved value in RESULT. */
6335
6336static bfd_boolean
6337calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
6338 offsetT *result)
6339{
6340 switch (reloc)
6341 {
6342 case BFD_RELOC_MIPS_HIGHEST:
6343 case BFD_RELOC_MICROMIPS_HIGHEST:
6344 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
6345 return TRUE;
6346
6347 case BFD_RELOC_MIPS_HIGHER:
6348 case BFD_RELOC_MICROMIPS_HIGHER:
6349 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
6350 return TRUE;
6351
6352 case BFD_RELOC_HI16_S:
6353 case BFD_RELOC_MICROMIPS_HI16_S:
6354 case BFD_RELOC_MIPS16_HI16_S:
6355 *result = ((operand + 0x8000) >> 16) & 0xffff;
6356 return TRUE;
6357
6358 case BFD_RELOC_HI16:
6359 case BFD_RELOC_MICROMIPS_HI16:
6360 case BFD_RELOC_MIPS16_HI16:
6361 *result = (operand >> 16) & 0xffff;
6362 return TRUE;
6363
6364 case BFD_RELOC_LO16:
6365 case BFD_RELOC_MICROMIPS_LO16:
6366 case BFD_RELOC_MIPS16_LO16:
6367 *result = operand & 0xffff;
6368 return TRUE;
6369
6370 case BFD_RELOC_UNUSED:
6371 *result = operand;
6372 return TRUE;
6373
6374 default:
6375 return FALSE;
6376 }
6377}
6378
71400594
RS
6379/* Output an instruction. IP is the instruction information.
6380 ADDRESS_EXPR is an operand of the instruction to be used with
df58fc94
RS
6381 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
6382 a macro expansion. */
71400594
RS
6383
6384static void
6385append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
df58fc94 6386 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
71400594 6387{
14fe068b 6388 unsigned long prev_pinfo2, pinfo;
71400594 6389 bfd_boolean relaxed_branch = FALSE;
a4e06468 6390 enum append_method method;
2309ddf2 6391 bfd_boolean relax32;
2b0c8b40 6392 int branch_disp;
71400594 6393
2309ddf2 6394 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
c67a084a
NC
6395 fix_loongson2f (ip);
6396
738f4d98 6397 file_ase_mips16 |= mips_opts.mips16;
df58fc94 6398 file_ase_micromips |= mips_opts.micromips;
738f4d98 6399
df58fc94 6400 prev_pinfo2 = history[0].insn_mo->pinfo2;
71400594 6401 pinfo = ip->insn_mo->pinfo;
df58fc94
RS
6402
6403 if (mips_opts.micromips
6404 && !expansionp
6405 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
6406 && micromips_insn_length (ip->insn_mo) != 2)
6407 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
6408 && micromips_insn_length (ip->insn_mo) != 4)))
6409 as_warn (_("Wrong size instruction in a %u-bit branch delay slot"),
6410 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
71400594 6411
15be625d
CM
6412 if (address_expr == NULL)
6413 ip->complete_p = 1;
b886a2ab
RS
6414 else if (reloc_type[0] <= BFD_RELOC_UNUSED
6415 && reloc_type[1] == BFD_RELOC_UNUSED
6416 && reloc_type[2] == BFD_RELOC_UNUSED
15be625d
CM
6417 && address_expr->X_op == O_constant)
6418 {
15be625d
CM
6419 switch (*reloc_type)
6420 {
15be625d 6421 case BFD_RELOC_MIPS_JMP:
df58fc94
RS
6422 {
6423 int shift;
6424
6425 shift = mips_opts.micromips ? 1 : 2;
6426 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
6427 as_bad (_("jump to misaligned address (0x%lx)"),
6428 (unsigned long) address_expr->X_add_number);
6429 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
6430 & 0x3ffffff);
335574df 6431 ip->complete_p = 1;
df58fc94 6432 }
15be625d
CM
6433 break;
6434
6435 case BFD_RELOC_MIPS16_JMP:
6436 if ((address_expr->X_add_number & 3) != 0)
6437 as_bad (_("jump to misaligned address (0x%lx)"),
6438 (unsigned long) address_expr->X_add_number);
6439 ip->insn_opcode |=
6440 (((address_expr->X_add_number & 0x7c0000) << 3)
6441 | ((address_expr->X_add_number & 0xf800000) >> 7)
6442 | ((address_expr->X_add_number & 0x3fffc) >> 2));
335574df 6443 ip->complete_p = 1;
15be625d
CM
6444 break;
6445
6446 case BFD_RELOC_16_PCREL_S2:
df58fc94
RS
6447 {
6448 int shift;
6449
6450 shift = mips_opts.micromips ? 1 : 2;
6451 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
6452 as_bad (_("branch to misaligned address (0x%lx)"),
6453 (unsigned long) address_expr->X_add_number);
6454 if (!mips_relax_branch)
6455 {
6456 if ((address_expr->X_add_number + (1 << (shift + 15)))
6457 & ~((1 << (shift + 16)) - 1))
6458 as_bad (_("branch address range overflow (0x%lx)"),
6459 (unsigned long) address_expr->X_add_number);
6460 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
6461 & 0xffff);
6462 }
df58fc94 6463 }
15be625d
CM
6464 break;
6465
6466 default:
b886a2ab
RS
6467 {
6468 offsetT value;
6469
6470 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
6471 &value))
6472 {
6473 ip->insn_opcode |= value & 0xffff;
6474 ip->complete_p = 1;
6475 }
6476 }
6477 break;
6478 }
15be625d
CM
6479 }
6480
71400594
RS
6481 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
6482 {
6483 /* There are a lot of optimizations we could do that we don't.
6484 In particular, we do not, in general, reorder instructions.
6485 If you use gcc with optimization, it will reorder
6486 instructions and generally do much more optimization then we
6487 do here; repeating all that work in the assembler would only
6488 benefit hand written assembly code, and does not seem worth
6489 it. */
6490 int nops = (mips_optimize == 0
932d1a1b
RS
6491 ? nops_for_insn (0, history, NULL)
6492 : nops_for_insn_or_target (0, history, ip));
71400594 6493 if (nops > 0)
252b5132
RH
6494 {
6495 fragS *old_frag;
6496 unsigned long old_frag_offset;
6497 int i;
252b5132
RH
6498
6499 old_frag = frag_now;
6500 old_frag_offset = frag_now_fix ();
6501
6502 for (i = 0; i < nops; i++)
14fe068b
RS
6503 add_fixed_insn (NOP_INSN);
6504 insert_into_history (0, nops, NOP_INSN);
252b5132
RH
6505
6506 if (listing)
6507 {
6508 listing_prev_line ();
6509 /* We may be at the start of a variant frag. In case we
6510 are, make sure there is enough space for the frag
6511 after the frags created by listing_prev_line. The
6512 argument to frag_grow here must be at least as large
6513 as the argument to all other calls to frag_grow in
6514 this file. We don't have to worry about being in the
6515 middle of a variant frag, because the variants insert
6516 all needed nop instructions themselves. */
6517 frag_grow (40);
6518 }
6519
462427c4 6520 mips_move_text_labels ();
252b5132
RH
6521
6522#ifndef NO_ECOFF_DEBUGGING
6523 if (ECOFF_DEBUGGING)
6524 ecoff_fix_loc (old_frag, old_frag_offset);
6525#endif
6526 }
71400594
RS
6527 }
6528 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
6529 {
932d1a1b
RS
6530 int nops;
6531
6532 /* Work out how many nops in prev_nop_frag are needed by IP,
6533 ignoring hazards generated by the first prev_nop_frag_since
6534 instructions. */
6535 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
9c2799c2 6536 gas_assert (nops <= prev_nop_frag_holds);
252b5132 6537
71400594
RS
6538 /* Enforce NOPS as a minimum. */
6539 if (nops > prev_nop_frag_required)
6540 prev_nop_frag_required = nops;
252b5132 6541
71400594
RS
6542 if (prev_nop_frag_holds == prev_nop_frag_required)
6543 {
6544 /* Settle for the current number of nops. Update the history
6545 accordingly (for the benefit of any future .set reorder code). */
6546 prev_nop_frag = NULL;
6547 insert_into_history (prev_nop_frag_since,
6548 prev_nop_frag_holds, NOP_INSN);
6549 }
6550 else
6551 {
6552 /* Allow this instruction to replace one of the nops that was
6553 tentatively added to prev_nop_frag. */
df58fc94 6554 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
71400594
RS
6555 prev_nop_frag_holds--;
6556 prev_nop_frag_since++;
252b5132
RH
6557 }
6558 }
6559
e407c74b 6560 method = get_append_method (ip, address_expr, reloc_type);
2b0c8b40 6561 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
a4e06468 6562
e410add4
RS
6563 dwarf2_emit_insn (0);
6564 /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
6565 so "move" the instruction address accordingly.
6566
6567 Also, it doesn't seem appropriate for the assembler to reorder .loc
6568 entries. If this instruction is a branch that we are going to swap
6569 with the previous instruction, the two instructions should be
6570 treated as a unit, and the debug information for both instructions
6571 should refer to the start of the branch sequence. Using the
6572 current position is certainly wrong when swapping a 32-bit branch
6573 and a 16-bit delay slot, since the current position would then be
6574 in the middle of a branch. */
6575 dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
58e2ea4d 6576
df58fc94
RS
6577 relax32 = (mips_relax_branch
6578 /* Don't try branch relaxation within .set nomacro, or within
6579 .set noat if we use $at for PIC computations. If it turns
6580 out that the branch was out-of-range, we'll get an error. */
6581 && !mips_opts.warn_about_macros
6582 && (mips_opts.at || mips_pic == NO_PIC)
3bf0dbfb
MR
6583 /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
6584 as they have no complementing branches. */
6585 && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
df58fc94
RS
6586
6587 if (!HAVE_CODE_COMPRESSION
6588 && address_expr
6589 && relax32
0b25d3e6 6590 && *reloc_type == BFD_RELOC_16_PCREL_S2
11625dd8 6591 && delayed_branch_p (ip))
4a6a3df4 6592 {
895921c9 6593 relaxed_branch = TRUE;
1e915849
RS
6594 add_relaxed_insn (ip, (relaxed_branch_length
6595 (NULL, NULL,
11625dd8
RS
6596 uncond_branch_p (ip) ? -1
6597 : branch_likely_p (ip) ? 1
1e915849
RS
6598 : 0)), 4,
6599 RELAX_BRANCH_ENCODE
66b3e8da 6600 (AT,
11625dd8
RS
6601 uncond_branch_p (ip),
6602 branch_likely_p (ip),
1e915849
RS
6603 pinfo & INSN_WRITE_GPR_31,
6604 0),
6605 address_expr->X_add_symbol,
6606 address_expr->X_add_number);
4a6a3df4
AO
6607 *reloc_type = BFD_RELOC_UNUSED;
6608 }
df58fc94
RS
6609 else if (mips_opts.micromips
6610 && address_expr
6611 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
6612 || *reloc_type > BFD_RELOC_UNUSED)
40209cad
MR
6613 && (delayed_branch_p (ip) || compact_branch_p (ip))
6614 /* Don't try branch relaxation when users specify
6615 16-bit/32-bit instructions. */
6616 && !forced_insn_length)
df58fc94
RS
6617 {
6618 bfd_boolean relax16 = *reloc_type > BFD_RELOC_UNUSED;
6619 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
11625dd8
RS
6620 int uncond = uncond_branch_p (ip) ? -1 : 0;
6621 int compact = compact_branch_p (ip);
df58fc94
RS
6622 int al = pinfo & INSN_WRITE_GPR_31;
6623 int length32;
6624
6625 gas_assert (address_expr != NULL);
6626 gas_assert (!mips_relax.sequence);
6627
2b0c8b40 6628 relaxed_branch = TRUE;
df58fc94
RS
6629 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
6630 add_relaxed_insn (ip, relax32 ? length32 : 4, relax16 ? 2 : 4,
40209cad
MR
6631 RELAX_MICROMIPS_ENCODE (type, AT, uncond, compact, al,
6632 relax32, 0, 0),
df58fc94
RS
6633 address_expr->X_add_symbol,
6634 address_expr->X_add_number);
6635 *reloc_type = BFD_RELOC_UNUSED;
6636 }
6637 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
6638 {
6639 /* We need to set up a variant frag. */
df58fc94 6640 gas_assert (address_expr != NULL);
1e915849
RS
6641 add_relaxed_insn (ip, 4, 0,
6642 RELAX_MIPS16_ENCODE
6643 (*reloc_type - BFD_RELOC_UNUSED,
df58fc94 6644 forced_insn_length == 2, forced_insn_length == 4,
11625dd8 6645 delayed_branch_p (&history[0]),
1e915849
RS
6646 history[0].mips16_absolute_jump_p),
6647 make_expr_symbol (address_expr), 0);
252b5132 6648 }
5c04167a 6649 else if (mips_opts.mips16 && insn_length (ip) == 2)
9497f5ac 6650 {
11625dd8 6651 if (!delayed_branch_p (ip))
b8ee1a6e
DU
6652 /* Make sure there is enough room to swap this instruction with
6653 a following jump instruction. */
6654 frag_grow (6);
1e915849 6655 add_fixed_insn (ip);
252b5132
RH
6656 }
6657 else
6658 {
6659 if (mips_opts.mips16
6660 && mips_opts.noreorder
11625dd8 6661 && delayed_branch_p (&history[0]))
252b5132
RH
6662 as_warn (_("extended instruction in delay slot"));
6663
4d7206a2
RS
6664 if (mips_relax.sequence)
6665 {
6666 /* If we've reached the end of this frag, turn it into a variant
6667 frag and record the information for the instructions we've
6668 written so far. */
6669 if (frag_room () < 4)
6670 relax_close_frag ();
df58fc94 6671 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4d7206a2
RS
6672 }
6673
584892a6 6674 if (mips_relax.sequence != 2)
df58fc94
RS
6675 {
6676 if (mips_macro_warning.first_insn_sizes[0] == 0)
6677 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
6678 mips_macro_warning.sizes[0] += insn_length (ip);
6679 mips_macro_warning.insns[0]++;
6680 }
584892a6 6681 if (mips_relax.sequence != 1)
df58fc94
RS
6682 {
6683 if (mips_macro_warning.first_insn_sizes[1] == 0)
6684 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
6685 mips_macro_warning.sizes[1] += insn_length (ip);
6686 mips_macro_warning.insns[1]++;
6687 }
584892a6 6688
1e915849
RS
6689 if (mips_opts.mips16)
6690 {
6691 ip->fixed_p = 1;
6692 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
6693 }
6694 add_fixed_insn (ip);
252b5132
RH
6695 }
6696
9fe77896 6697 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
252b5132 6698 {
df58fc94 6699 bfd_reloc_code_real_type final_type[3];
2309ddf2 6700 reloc_howto_type *howto0;
9fe77896
RS
6701 reloc_howto_type *howto;
6702 int i;
34ce925e 6703
df58fc94
RS
6704 /* Perform any necessary conversion to microMIPS relocations
6705 and find out how many relocations there actually are. */
6706 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
6707 final_type[i] = micromips_map_reloc (reloc_type[i]);
6708
9fe77896
RS
6709 /* In a compound relocation, it is the final (outermost)
6710 operator that determines the relocated field. */
2309ddf2 6711 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
e8044f35
RS
6712 if (!howto)
6713 abort ();
2309ddf2
MR
6714
6715 if (i > 1)
6716 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
9fe77896
RS
6717 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
6718 bfd_get_reloc_size (howto),
6719 address_expr,
2309ddf2
MR
6720 howto0 && howto0->pc_relative,
6721 final_type[0]);
9fe77896
RS
6722
6723 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
2309ddf2 6724 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
9fe77896
RS
6725 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
6726
6727 /* These relocations can have an addend that won't fit in
6728 4 octets for 64bit assembly. */
6729 if (HAVE_64BIT_GPRS
6730 && ! howto->partial_inplace
6731 && (reloc_type[0] == BFD_RELOC_16
6732 || reloc_type[0] == BFD_RELOC_32
6733 || reloc_type[0] == BFD_RELOC_MIPS_JMP
6734 || reloc_type[0] == BFD_RELOC_GPREL16
6735 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
6736 || reloc_type[0] == BFD_RELOC_GPREL32
6737 || reloc_type[0] == BFD_RELOC_64
6738 || reloc_type[0] == BFD_RELOC_CTOR
6739 || reloc_type[0] == BFD_RELOC_MIPS_SUB
6740 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
6741 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
6742 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
6743 || reloc_type[0] == BFD_RELOC_MIPS_REL16
6744 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
6745 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
6746 || hi16_reloc_p (reloc_type[0])
6747 || lo16_reloc_p (reloc_type[0])))
6748 ip->fixp[0]->fx_no_overflow = 1;
6749
ddaf2c41
MR
6750 /* These relocations can have an addend that won't fit in 2 octets. */
6751 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
6752 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
6753 ip->fixp[0]->fx_no_overflow = 1;
6754
9fe77896
RS
6755 if (mips_relax.sequence)
6756 {
6757 if (mips_relax.first_fixup == 0)
6758 mips_relax.first_fixup = ip->fixp[0];
6759 }
6760 else if (reloc_needs_lo_p (*reloc_type))
6761 {
6762 struct mips_hi_fixup *hi_fixup;
6763
6764 /* Reuse the last entry if it already has a matching %lo. */
6765 hi_fixup = mips_hi_fixup_list;
6766 if (hi_fixup == 0
6767 || !fixup_has_matching_lo_p (hi_fixup->fixp))
4d7206a2 6768 {
9fe77896
RS
6769 hi_fixup = ((struct mips_hi_fixup *)
6770 xmalloc (sizeof (struct mips_hi_fixup)));
6771 hi_fixup->next = mips_hi_fixup_list;
6772 mips_hi_fixup_list = hi_fixup;
4d7206a2 6773 }
9fe77896
RS
6774 hi_fixup->fixp = ip->fixp[0];
6775 hi_fixup->seg = now_seg;
6776 }
252b5132 6777
9fe77896
RS
6778 /* Add fixups for the second and third relocations, if given.
6779 Note that the ABI allows the second relocation to be
6780 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
6781 moment we only use RSS_UNDEF, but we could add support
6782 for the others if it ever becomes necessary. */
6783 for (i = 1; i < 3; i++)
6784 if (reloc_type[i] != BFD_RELOC_UNUSED)
6785 {
6786 ip->fixp[i] = fix_new (ip->frag, ip->where,
6787 ip->fixp[0]->fx_size, NULL, 0,
df58fc94 6788 FALSE, final_type[i]);
f6688943 6789
9fe77896
RS
6790 /* Use fx_tcbit to mark compound relocs. */
6791 ip->fixp[0]->fx_tcbit = 1;
6792 ip->fixp[i]->fx_tcbit = 1;
6793 }
252b5132 6794 }
1e915849 6795 install_insn (ip);
252b5132
RH
6796
6797 /* Update the register mask information. */
4c260379
RS
6798 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
6799 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
252b5132 6800
a4e06468 6801 switch (method)
252b5132 6802 {
a4e06468
RS
6803 case APPEND_ADD:
6804 insert_into_history (0, 1, ip);
6805 break;
6806
6807 case APPEND_ADD_WITH_NOP:
14fe068b
RS
6808 {
6809 struct mips_cl_insn *nop;
6810
6811 insert_into_history (0, 1, ip);
6812 nop = get_delay_slot_nop (ip);
6813 add_fixed_insn (nop);
6814 insert_into_history (0, 1, nop);
6815 if (mips_relax.sequence)
6816 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
6817 }
a4e06468
RS
6818 break;
6819
6820 case APPEND_ADD_COMPACT:
6821 /* Convert MIPS16 jr/jalr into a "compact" jump. */
6822 gas_assert (mips_opts.mips16);
6823 ip->insn_opcode |= 0x0080;
6824 find_altered_mips16_opcode (ip);
6825 install_insn (ip);
6826 insert_into_history (0, 1, ip);
6827 break;
6828
6829 case APPEND_SWAP:
6830 {
6831 struct mips_cl_insn delay = history[0];
6832 if (mips_opts.mips16)
6833 {
6834 know (delay.frag == ip->frag);
6835 move_insn (ip, delay.frag, delay.where);
6836 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
6837 }
464ab0e5 6838 else if (relaxed_branch || delay.frag != ip->frag)
a4e06468
RS
6839 {
6840 /* Add the delay slot instruction to the end of the
6841 current frag and shrink the fixed part of the
6842 original frag. If the branch occupies the tail of
6843 the latter, move it backwards to cover the gap. */
2b0c8b40 6844 delay.frag->fr_fix -= branch_disp;
a4e06468 6845 if (delay.frag == ip->frag)
2b0c8b40 6846 move_insn (ip, ip->frag, ip->where - branch_disp);
a4e06468
RS
6847 add_fixed_insn (&delay);
6848 }
6849 else
6850 {
2b0c8b40
MR
6851 move_insn (&delay, ip->frag,
6852 ip->where - branch_disp + insn_length (ip));
a4e06468
RS
6853 move_insn (ip, history[0].frag, history[0].where);
6854 }
6855 history[0] = *ip;
6856 delay.fixed_p = 1;
6857 insert_into_history (0, 1, &delay);
6858 }
6859 break;
252b5132
RH
6860 }
6861
13408f1e 6862 /* If we have just completed an unconditional branch, clear the history. */
11625dd8
RS
6863 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
6864 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
e407c74b
NC
6865 {
6866 unsigned int i;
6867
79850f26 6868 mips_no_prev_insn ();
13408f1e 6869
e407c74b 6870 for (i = 0; i < ARRAY_SIZE (history); i++)
79850f26 6871 history[i].cleared_p = 1;
e407c74b
NC
6872 }
6873
df58fc94
RS
6874 /* We need to emit a label at the end of branch-likely macros. */
6875 if (emit_branch_likely_macro)
6876 {
6877 emit_branch_likely_macro = FALSE;
6878 micromips_add_label ();
6879 }
6880
252b5132
RH
6881 /* We just output an insn, so the next one doesn't have a label. */
6882 mips_clear_insn_labels ();
252b5132
RH
6883}
6884
e407c74b
NC
6885/* Forget that there was any previous instruction or label.
6886 When BRANCH is true, the branch history is also flushed. */
252b5132
RH
6887
6888static void
7d10b47d 6889mips_no_prev_insn (void)
252b5132 6890{
7d10b47d
RS
6891 prev_nop_frag = NULL;
6892 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
6893 mips_clear_insn_labels ();
6894}
6895
7d10b47d
RS
6896/* This function must be called before we emit something other than
6897 instructions. It is like mips_no_prev_insn except that it inserts
6898 any NOPS that might be needed by previous instructions. */
252b5132 6899
7d10b47d
RS
6900void
6901mips_emit_delays (void)
252b5132
RH
6902{
6903 if (! mips_opts.noreorder)
6904 {
932d1a1b 6905 int nops = nops_for_insn (0, history, NULL);
252b5132
RH
6906 if (nops > 0)
6907 {
7d10b47d
RS
6908 while (nops-- > 0)
6909 add_fixed_insn (NOP_INSN);
462427c4 6910 mips_move_text_labels ();
7d10b47d
RS
6911 }
6912 }
6913 mips_no_prev_insn ();
6914}
6915
6916/* Start a (possibly nested) noreorder block. */
6917
6918static void
6919start_noreorder (void)
6920{
6921 if (mips_opts.noreorder == 0)
6922 {
6923 unsigned int i;
6924 int nops;
6925
6926 /* None of the instructions before the .set noreorder can be moved. */
6927 for (i = 0; i < ARRAY_SIZE (history); i++)
6928 history[i].fixed_p = 1;
6929
6930 /* Insert any nops that might be needed between the .set noreorder
6931 block and the previous instructions. We will later remove any
6932 nops that turn out not to be needed. */
932d1a1b 6933 nops = nops_for_insn (0, history, NULL);
7d10b47d
RS
6934 if (nops > 0)
6935 {
6936 if (mips_optimize != 0)
252b5132
RH
6937 {
6938 /* Record the frag which holds the nop instructions, so
6939 that we can remove them if we don't need them. */
df58fc94 6940 frag_grow (nops * NOP_INSN_SIZE);
252b5132
RH
6941 prev_nop_frag = frag_now;
6942 prev_nop_frag_holds = nops;
6943 prev_nop_frag_required = 0;
6944 prev_nop_frag_since = 0;
6945 }
6946
6947 for (; nops > 0; --nops)
1e915849 6948 add_fixed_insn (NOP_INSN);
252b5132 6949
7d10b47d
RS
6950 /* Move on to a new frag, so that it is safe to simply
6951 decrease the size of prev_nop_frag. */
6952 frag_wane (frag_now);
6953 frag_new (0);
462427c4 6954 mips_move_text_labels ();
252b5132 6955 }
df58fc94 6956 mips_mark_labels ();
7d10b47d 6957 mips_clear_insn_labels ();
252b5132 6958 }
7d10b47d
RS
6959 mips_opts.noreorder++;
6960 mips_any_noreorder = 1;
6961}
252b5132 6962
7d10b47d 6963/* End a nested noreorder block. */
252b5132 6964
7d10b47d
RS
6965static void
6966end_noreorder (void)
6967{
6968 mips_opts.noreorder--;
6969 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
6970 {
6971 /* Commit to inserting prev_nop_frag_required nops and go back to
6972 handling nop insertion the .set reorder way. */
6973 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
df58fc94 6974 * NOP_INSN_SIZE);
7d10b47d
RS
6975 insert_into_history (prev_nop_frag_since,
6976 prev_nop_frag_required, NOP_INSN);
6977 prev_nop_frag = NULL;
6978 }
252b5132
RH
6979}
6980
97d87491
RS
6981/* Sign-extend 32-bit mode constants that have bit 31 set and all
6982 higher bits unset. */
6983
6984static void
6985normalize_constant_expr (expressionS *ex)
6986{
6987 if (ex->X_op == O_constant
6988 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
6989 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
6990 - 0x80000000);
6991}
6992
6993/* Sign-extend 32-bit mode address offsets that have bit 31 set and
6994 all higher bits unset. */
6995
6996static void
6997normalize_address_expr (expressionS *ex)
6998{
6999 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
7000 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
7001 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7002 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7003 - 0x80000000);
7004}
7005
7006/* Try to match TOKENS against OPCODE, storing the result in INSN.
7007 Return true if the match was successful.
7008
7009 OPCODE_EXTRA is a value that should be ORed into the opcode
7010 (used for VU0 channel suffixes, etc.). MORE_ALTS is true if
7011 there are more alternatives after OPCODE and SOFT_MATCH is
7012 as for mips_arg_info. */
7013
7014static bfd_boolean
7015match_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
7016 struct mips_operand_token *tokens, unsigned int opcode_extra,
60f20e8b 7017 bfd_boolean lax_match, bfd_boolean complete_p)
97d87491
RS
7018{
7019 const char *args;
7020 struct mips_arg_info arg;
7021 const struct mips_operand *operand;
7022 char c;
7023
7024 imm_expr.X_op = O_absent;
97d87491
RS
7025 offset_expr.X_op = O_absent;
7026 offset_reloc[0] = BFD_RELOC_UNUSED;
7027 offset_reloc[1] = BFD_RELOC_UNUSED;
7028 offset_reloc[2] = BFD_RELOC_UNUSED;
7029
7030 create_insn (insn, opcode);
60f20e8b
RS
7031 /* When no opcode suffix is specified, assume ".xyzw". */
7032 if ((opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0 && opcode_extra == 0)
7033 insn->insn_opcode |= 0xf << mips_vu0_channel_mask.lsb;
7034 else
7035 insn->insn_opcode |= opcode_extra;
97d87491
RS
7036 memset (&arg, 0, sizeof (arg));
7037 arg.insn = insn;
7038 arg.token = tokens;
7039 arg.argnum = 1;
7040 arg.last_regno = ILLEGAL_REG;
7041 arg.dest_regno = ILLEGAL_REG;
60f20e8b 7042 arg.lax_match = lax_match;
97d87491
RS
7043 for (args = opcode->args;; ++args)
7044 {
7045 if (arg.token->type == OT_END)
7046 {
7047 /* Handle unary instructions in which only one operand is given.
7048 The source is then the same as the destination. */
7049 if (arg.opnum == 1 && *args == ',')
7050 {
7051 operand = (mips_opts.micromips
7052 ? decode_micromips_operand (args + 1)
7053 : decode_mips_operand (args + 1));
7054 if (operand && mips_optional_operand_p (operand))
7055 {
7056 arg.token = tokens;
7057 arg.argnum = 1;
7058 continue;
7059 }
7060 }
7061
7062 /* Treat elided base registers as $0. */
7063 if (strcmp (args, "(b)") == 0)
7064 args += 3;
7065
7066 if (args[0] == '+')
7067 switch (args[1])
7068 {
7069 case 'K':
7070 case 'N':
7071 /* The register suffix is optional. */
7072 args += 2;
7073 break;
7074 }
7075
7076 /* Fail the match if there were too few operands. */
7077 if (*args)
7078 return FALSE;
7079
7080 /* Successful match. */
60f20e8b
RS
7081 if (!complete_p)
7082 return TRUE;
e3de51ce 7083 clear_insn_error ();
97d87491
RS
7084 if (arg.dest_regno == arg.last_regno
7085 && strncmp (insn->insn_mo->name, "jalr", 4) == 0)
7086 {
7087 if (arg.opnum == 2)
e3de51ce
RS
7088 set_insn_error
7089 (0, _("Source and destination must be different"));
97d87491 7090 else if (arg.last_regno == 31)
e3de51ce
RS
7091 set_insn_error
7092 (0, _("A destination register must be supplied"));
97d87491
RS
7093 }
7094 check_completed_insn (&arg);
7095 return TRUE;
7096 }
7097
7098 /* Fail the match if the line has too many operands. */
7099 if (*args == 0)
7100 return FALSE;
7101
7102 /* Handle characters that need to match exactly. */
7103 if (*args == '(' || *args == ')' || *args == ',')
7104 {
7105 if (match_char (&arg, *args))
7106 continue;
7107 return FALSE;
7108 }
7109 if (*args == '#')
7110 {
7111 ++args;
7112 if (arg.token->type == OT_DOUBLE_CHAR
7113 && arg.token->u.ch == *args)
7114 {
7115 ++arg.token;
7116 continue;
7117 }
7118 return FALSE;
7119 }
7120
7121 /* Handle special macro operands. Work out the properties of
7122 other operands. */
7123 arg.opnum += 1;
97d87491
RS
7124 switch (*args)
7125 {
7126 case '+':
7127 switch (args[1])
7128 {
97d87491
RS
7129 case 'i':
7130 *offset_reloc = BFD_RELOC_MIPS_JMP;
7131 break;
7132 }
7133 break;
7134
97d87491 7135 case 'I':
1a00e612
RS
7136 if (!match_const_int (&arg, &imm_expr.X_add_number))
7137 return FALSE;
7138 imm_expr.X_op = O_constant;
97d87491
RS
7139 if (HAVE_32BIT_GPRS)
7140 normalize_constant_expr (&imm_expr);
7141 continue;
7142
7143 case 'A':
7144 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
7145 {
7146 /* Assume that the offset has been elided and that what
7147 we saw was a base register. The match will fail later
7148 if that assumption turns out to be wrong. */
7149 offset_expr.X_op = O_constant;
7150 offset_expr.X_add_number = 0;
7151 }
97d87491 7152 else
1a00e612
RS
7153 {
7154 if (!match_expression (&arg, &offset_expr, offset_reloc))
7155 return FALSE;
7156 normalize_address_expr (&offset_expr);
7157 }
97d87491
RS
7158 continue;
7159
7160 case 'F':
7161 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
7162 8, TRUE))
1a00e612 7163 return FALSE;
97d87491
RS
7164 continue;
7165
7166 case 'L':
7167 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
7168 8, FALSE))
1a00e612 7169 return FALSE;
97d87491
RS
7170 continue;
7171
7172 case 'f':
7173 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
7174 4, TRUE))
1a00e612 7175 return FALSE;
97d87491
RS
7176 continue;
7177
7178 case 'l':
7179 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
7180 4, FALSE))
1a00e612 7181 return FALSE;
97d87491
RS
7182 continue;
7183
97d87491
RS
7184 case 'p':
7185 *offset_reloc = BFD_RELOC_16_PCREL_S2;
7186 break;
7187
7188 case 'a':
7189 *offset_reloc = BFD_RELOC_MIPS_JMP;
7190 break;
7191
7192 case 'm':
7193 gas_assert (mips_opts.micromips);
7194 c = args[1];
7195 switch (c)
7196 {
7197 case 'D':
7198 case 'E':
7199 if (!forced_insn_length)
7200 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
7201 else if (c == 'D')
7202 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
7203 else
7204 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
7205 break;
7206 }
7207 break;
7208 }
7209
7210 operand = (mips_opts.micromips
7211 ? decode_micromips_operand (args)
7212 : decode_mips_operand (args));
7213 if (!operand)
7214 abort ();
7215
7216 /* Skip prefixes. */
7217 if (*args == '+' || *args == 'm')
7218 args++;
7219
7220 if (mips_optional_operand_p (operand)
7221 && args[1] == ','
7222 && (arg.token[0].type != OT_REG
7223 || arg.token[1].type == OT_END))
7224 {
7225 /* Assume that the register has been elided and is the
7226 same as the first operand. */
7227 arg.token = tokens;
7228 arg.argnum = 1;
7229 }
7230
7231 if (!match_operand (&arg, operand))
7232 return FALSE;
7233 }
7234}
7235
7236/* Like match_insn, but for MIPS16. */
7237
7238static bfd_boolean
7239match_mips16_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
1a00e612 7240 struct mips_operand_token *tokens)
97d87491
RS
7241{
7242 const char *args;
7243 const struct mips_operand *operand;
7244 const struct mips_operand *ext_operand;
7245 struct mips_arg_info arg;
7246 int relax_char;
7247
7248 create_insn (insn, opcode);
7249 imm_expr.X_op = O_absent;
97d87491
RS
7250 offset_expr.X_op = O_absent;
7251 offset_reloc[0] = BFD_RELOC_UNUSED;
7252 offset_reloc[1] = BFD_RELOC_UNUSED;
7253 offset_reloc[2] = BFD_RELOC_UNUSED;
7254 relax_char = 0;
7255
7256 memset (&arg, 0, sizeof (arg));
7257 arg.insn = insn;
7258 arg.token = tokens;
7259 arg.argnum = 1;
7260 arg.last_regno = ILLEGAL_REG;
7261 arg.dest_regno = ILLEGAL_REG;
97d87491
RS
7262 relax_char = 0;
7263 for (args = opcode->args;; ++args)
7264 {
7265 int c;
7266
7267 if (arg.token->type == OT_END)
7268 {
7269 offsetT value;
7270
7271 /* Handle unary instructions in which only one operand is given.
7272 The source is then the same as the destination. */
7273 if (arg.opnum == 1 && *args == ',')
7274 {
7275 operand = decode_mips16_operand (args[1], FALSE);
7276 if (operand && mips_optional_operand_p (operand))
7277 {
7278 arg.token = tokens;
7279 arg.argnum = 1;
7280 continue;
7281 }
7282 }
7283
7284 /* Fail the match if there were too few operands. */
7285 if (*args)
7286 return FALSE;
7287
7288 /* Successful match. Stuff the immediate value in now, if
7289 we can. */
e3de51ce 7290 clear_insn_error ();
97d87491
RS
7291 if (opcode->pinfo == INSN_MACRO)
7292 {
7293 gas_assert (relax_char == 0 || relax_char == 'p');
7294 gas_assert (*offset_reloc == BFD_RELOC_UNUSED);
7295 }
7296 else if (relax_char
7297 && offset_expr.X_op == O_constant
7298 && calculate_reloc (*offset_reloc,
7299 offset_expr.X_add_number,
7300 &value))
7301 {
7302 mips16_immed (NULL, 0, relax_char, *offset_reloc, value,
7303 forced_insn_length, &insn->insn_opcode);
7304 offset_expr.X_op = O_absent;
7305 *offset_reloc = BFD_RELOC_UNUSED;
7306 }
7307 else if (relax_char && *offset_reloc != BFD_RELOC_UNUSED)
7308 {
7309 if (forced_insn_length == 2)
e3de51ce 7310 set_insn_error (0, _("invalid unextended operand value"));
97d87491
RS
7311 forced_insn_length = 4;
7312 insn->insn_opcode |= MIPS16_EXTEND;
7313 }
7314 else if (relax_char)
7315 *offset_reloc = (int) BFD_RELOC_UNUSED + relax_char;
7316
7317 check_completed_insn (&arg);
7318 return TRUE;
7319 }
7320
7321 /* Fail the match if the line has too many operands. */
7322 if (*args == 0)
7323 return FALSE;
7324
7325 /* Handle characters that need to match exactly. */
7326 if (*args == '(' || *args == ')' || *args == ',')
7327 {
7328 if (match_char (&arg, *args))
7329 continue;
7330 return FALSE;
7331 }
7332
7333 arg.opnum += 1;
7334 c = *args;
7335 switch (c)
7336 {
7337 case 'p':
7338 case 'q':
7339 case 'A':
7340 case 'B':
7341 case 'E':
7342 relax_char = c;
7343 break;
7344
7345 case 'I':
1a00e612
RS
7346 if (!match_const_int (&arg, &imm_expr.X_add_number))
7347 return FALSE;
7348 imm_expr.X_op = O_constant;
97d87491
RS
7349 if (HAVE_32BIT_GPRS)
7350 normalize_constant_expr (&imm_expr);
7351 continue;
7352
7353 case 'a':
7354 case 'i':
7355 *offset_reloc = BFD_RELOC_MIPS16_JMP;
7356 insn->insn_opcode <<= 16;
7357 break;
7358 }
7359
7360 operand = decode_mips16_operand (c, FALSE);
7361 if (!operand)
7362 abort ();
7363
7364 /* '6' is a special case. It is used for BREAK and SDBBP,
7365 whose operands are only meaningful to the software that decodes
7366 them. This means that there is no architectural reason why
7367 they cannot be prefixed by EXTEND, but in practice,
7368 exception handlers will only look at the instruction
7369 itself. We therefore allow '6' to be extended when
7370 disassembling but not when assembling. */
7371 if (operand->type != OP_PCREL && c != '6')
7372 {
7373 ext_operand = decode_mips16_operand (c, TRUE);
7374 if (operand != ext_operand)
7375 {
7376 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
7377 {
7378 offset_expr.X_op = O_constant;
7379 offset_expr.X_add_number = 0;
7380 relax_char = c;
7381 continue;
7382 }
7383
7384 /* We need the OT_INTEGER check because some MIPS16
7385 immediate variants are listed before the register ones. */
7386 if (arg.token->type != OT_INTEGER
7387 || !match_expression (&arg, &offset_expr, offset_reloc))
7388 return FALSE;
7389
7390 /* '8' is used for SLTI(U) and has traditionally not
7391 been allowed to take relocation operators. */
7392 if (offset_reloc[0] != BFD_RELOC_UNUSED
7393 && (ext_operand->size != 16 || c == '8'))
7394 return FALSE;
7395
7396 relax_char = c;
7397 continue;
7398 }
7399 }
7400
7401 if (mips_optional_operand_p (operand)
7402 && args[1] == ','
7403 && (arg.token[0].type != OT_REG
7404 || arg.token[1].type == OT_END))
7405 {
7406 /* Assume that the register has been elided and is the
7407 same as the first operand. */
7408 arg.token = tokens;
7409 arg.argnum = 1;
7410 }
7411
7412 if (!match_operand (&arg, operand))
7413 return FALSE;
7414 }
7415}
7416
60f20e8b
RS
7417/* Record that the current instruction is invalid for the current ISA. */
7418
7419static void
7420match_invalid_for_isa (void)
7421{
7422 set_insn_error_ss
7423 (0, _("Opcode not supported on this processor: %s (%s)"),
7424 mips_cpu_info_from_arch (mips_opts.arch)->name,
7425 mips_cpu_info_from_isa (mips_opts.isa)->name);
7426}
7427
7428/* Try to match TOKENS against a series of opcode entries, starting at FIRST.
7429 Return true if a definite match or failure was found, storing any match
7430 in INSN. OPCODE_EXTRA is a value that should be ORed into the opcode
7431 (to handle things like VU0 suffixes). LAX_MATCH is true if we have already
7432 tried and failed to match under normal conditions and now want to try a
7433 more relaxed match. */
7434
7435static bfd_boolean
7436match_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
7437 const struct mips_opcode *past, struct mips_operand_token *tokens,
7438 int opcode_extra, bfd_boolean lax_match)
7439{
7440 const struct mips_opcode *opcode;
7441 const struct mips_opcode *invalid_delay_slot;
7442 bfd_boolean seen_valid_for_isa, seen_valid_for_size;
7443
7444 /* Search for a match, ignoring alternatives that don't satisfy the
7445 current ISA or forced_length. */
7446 invalid_delay_slot = 0;
7447 seen_valid_for_isa = FALSE;
7448 seen_valid_for_size = FALSE;
7449 opcode = first;
7450 do
7451 {
7452 gas_assert (strcmp (opcode->name, first->name) == 0);
7453 if (is_opcode_valid (opcode))
7454 {
7455 seen_valid_for_isa = TRUE;
7456 if (is_size_valid (opcode))
7457 {
7458 bfd_boolean delay_slot_ok;
7459
7460 seen_valid_for_size = TRUE;
7461 delay_slot_ok = is_delay_slot_valid (opcode);
7462 if (match_insn (insn, opcode, tokens, opcode_extra,
7463 lax_match, delay_slot_ok))
7464 {
7465 if (!delay_slot_ok)
7466 {
7467 if (!invalid_delay_slot)
7468 invalid_delay_slot = opcode;
7469 }
7470 else
7471 return TRUE;
7472 }
7473 }
7474 }
7475 ++opcode;
7476 }
7477 while (opcode < past && strcmp (opcode->name, first->name) == 0);
7478
7479 /* If the only matches we found had the wrong length for the delay slot,
7480 pick the first such match. We'll issue an appropriate warning later. */
7481 if (invalid_delay_slot)
7482 {
7483 if (match_insn (insn, invalid_delay_slot, tokens, opcode_extra,
7484 lax_match, TRUE))
7485 return TRUE;
7486 abort ();
7487 }
7488
7489 /* Handle the case where we didn't try to match an instruction because
7490 all the alternatives were incompatible with the current ISA. */
7491 if (!seen_valid_for_isa)
7492 {
7493 match_invalid_for_isa ();
7494 return TRUE;
7495 }
7496
7497 /* Handle the case where we didn't try to match an instruction because
7498 all the alternatives were of the wrong size. */
7499 if (!seen_valid_for_size)
7500 {
7501 if (mips_opts.insn32)
7502 set_insn_error (0, _("Opcode not supported in the `insn32' mode"));
7503 else
7504 set_insn_error_i
7505 (0, _("Unrecognized %d-bit version of microMIPS opcode"),
7506 8 * forced_insn_length);
7507 return TRUE;
7508 }
7509
7510 return FALSE;
7511}
7512
7513/* Like match_insns, but for MIPS16. */
7514
7515static bfd_boolean
7516match_mips16_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
7517 struct mips_operand_token *tokens)
7518{
7519 const struct mips_opcode *opcode;
7520 bfd_boolean seen_valid_for_isa;
7521
7522 /* Search for a match, ignoring alternatives that don't satisfy the
7523 current ISA. There are no separate entries for extended forms so
7524 we deal with forced_length later. */
7525 seen_valid_for_isa = FALSE;
7526 opcode = first;
7527 do
7528 {
7529 gas_assert (strcmp (opcode->name, first->name) == 0);
7530 if (is_opcode_valid_16 (opcode))
7531 {
7532 seen_valid_for_isa = TRUE;
7533 if (match_mips16_insn (insn, opcode, tokens))
7534 return TRUE;
7535 }
7536 ++opcode;
7537 }
7538 while (opcode < &mips16_opcodes[bfd_mips16_num_opcodes]
7539 && strcmp (opcode->name, first->name) == 0);
7540
7541 /* Handle the case where we didn't try to match an instruction because
7542 all the alternatives were incompatible with the current ISA. */
7543 if (!seen_valid_for_isa)
7544 {
7545 match_invalid_for_isa ();
7546 return TRUE;
7547 }
7548
7549 return FALSE;
7550}
7551
584892a6
RS
7552/* Set up global variables for the start of a new macro. */
7553
7554static void
7555macro_start (void)
7556{
7557 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
df58fc94
RS
7558 memset (&mips_macro_warning.first_insn_sizes, 0,
7559 sizeof (mips_macro_warning.first_insn_sizes));
7560 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
584892a6 7561 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
11625dd8 7562 && delayed_branch_p (&history[0]));
df58fc94
RS
7563 switch (history[0].insn_mo->pinfo2
7564 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
7565 {
7566 case INSN2_BRANCH_DELAY_32BIT:
7567 mips_macro_warning.delay_slot_length = 4;
7568 break;
7569 case INSN2_BRANCH_DELAY_16BIT:
7570 mips_macro_warning.delay_slot_length = 2;
7571 break;
7572 default:
7573 mips_macro_warning.delay_slot_length = 0;
7574 break;
7575 }
7576 mips_macro_warning.first_frag = NULL;
584892a6
RS
7577}
7578
df58fc94
RS
7579/* Given that a macro is longer than one instruction or of the wrong size,
7580 return the appropriate warning for it. Return null if no warning is
7581 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
7582 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
7583 and RELAX_NOMACRO. */
584892a6
RS
7584
7585static const char *
7586macro_warning (relax_substateT subtype)
7587{
7588 if (subtype & RELAX_DELAY_SLOT)
7589 return _("Macro instruction expanded into multiple instructions"
7590 " in a branch delay slot");
7591 else if (subtype & RELAX_NOMACRO)
7592 return _("Macro instruction expanded into multiple instructions");
df58fc94
RS
7593 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
7594 | RELAX_DELAY_SLOT_SIZE_SECOND))
7595 return ((subtype & RELAX_DELAY_SLOT_16BIT)
7596 ? _("Macro instruction expanded into a wrong size instruction"
7597 " in a 16-bit branch delay slot")
7598 : _("Macro instruction expanded into a wrong size instruction"
7599 " in a 32-bit branch delay slot"));
584892a6
RS
7600 else
7601 return 0;
7602}
7603
7604/* Finish up a macro. Emit warnings as appropriate. */
7605
7606static void
7607macro_end (void)
7608{
df58fc94
RS
7609 /* Relaxation warning flags. */
7610 relax_substateT subtype = 0;
7611
7612 /* Check delay slot size requirements. */
7613 if (mips_macro_warning.delay_slot_length == 2)
7614 subtype |= RELAX_DELAY_SLOT_16BIT;
7615 if (mips_macro_warning.delay_slot_length != 0)
584892a6 7616 {
df58fc94
RS
7617 if (mips_macro_warning.delay_slot_length
7618 != mips_macro_warning.first_insn_sizes[0])
7619 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
7620 if (mips_macro_warning.delay_slot_length
7621 != mips_macro_warning.first_insn_sizes[1])
7622 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
7623 }
584892a6 7624
df58fc94
RS
7625 /* Check instruction count requirements. */
7626 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
7627 {
7628 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
584892a6
RS
7629 subtype |= RELAX_SECOND_LONGER;
7630 if (mips_opts.warn_about_macros)
7631 subtype |= RELAX_NOMACRO;
7632 if (mips_macro_warning.delay_slot_p)
7633 subtype |= RELAX_DELAY_SLOT;
df58fc94 7634 }
584892a6 7635
df58fc94
RS
7636 /* If both alternatives fail to fill a delay slot correctly,
7637 emit the warning now. */
7638 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
7639 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
7640 {
7641 relax_substateT s;
7642 const char *msg;
7643
7644 s = subtype & (RELAX_DELAY_SLOT_16BIT
7645 | RELAX_DELAY_SLOT_SIZE_FIRST
7646 | RELAX_DELAY_SLOT_SIZE_SECOND);
7647 msg = macro_warning (s);
7648 if (msg != NULL)
7649 as_warn ("%s", msg);
7650 subtype &= ~s;
7651 }
7652
7653 /* If both implementations are longer than 1 instruction, then emit the
7654 warning now. */
7655 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
7656 {
7657 relax_substateT s;
7658 const char *msg;
7659
7660 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
7661 msg = macro_warning (s);
7662 if (msg != NULL)
7663 as_warn ("%s", msg);
7664 subtype &= ~s;
584892a6 7665 }
df58fc94
RS
7666
7667 /* If any flags still set, then one implementation might need a warning
7668 and the other either will need one of a different kind or none at all.
7669 Pass any remaining flags over to relaxation. */
7670 if (mips_macro_warning.first_frag != NULL)
7671 mips_macro_warning.first_frag->fr_subtype |= subtype;
584892a6
RS
7672}
7673
df58fc94
RS
7674/* Instruction operand formats used in macros that vary between
7675 standard MIPS and microMIPS code. */
7676
833794fc 7677static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
df58fc94
RS
7678static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
7679static const char * const jalr_fmt[2] = { "d,s", "t,s" };
7680static const char * const lui_fmt[2] = { "t,u", "s,u" };
7681static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
833794fc 7682static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
df58fc94
RS
7683static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
7684static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
7685
833794fc 7686#define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
df58fc94
RS
7687#define COP12_FMT (cop12_fmt[mips_opts.micromips])
7688#define JALR_FMT (jalr_fmt[mips_opts.micromips])
7689#define LUI_FMT (lui_fmt[mips_opts.micromips])
7690#define MEM12_FMT (mem12_fmt[mips_opts.micromips])
833794fc 7691#define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
df58fc94
RS
7692#define SHFT_FMT (shft_fmt[mips_opts.micromips])
7693#define TRAP_FMT (trap_fmt[mips_opts.micromips])
7694
6e1304d8
RS
7695/* Read a macro's relocation codes from *ARGS and store them in *R.
7696 The first argument in *ARGS will be either the code for a single
7697 relocation or -1 followed by the three codes that make up a
7698 composite relocation. */
7699
7700static void
7701macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
7702{
7703 int i, next;
7704
7705 next = va_arg (*args, int);
7706 if (next >= 0)
7707 r[0] = (bfd_reloc_code_real_type) next;
7708 else
f2ae14a1
RS
7709 {
7710 for (i = 0; i < 3; i++)
7711 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
7712 /* This function is only used for 16-bit relocation fields.
7713 To make the macro code simpler, treat an unrelocated value
7714 in the same way as BFD_RELOC_LO16. */
7715 if (r[0] == BFD_RELOC_UNUSED)
7716 r[0] = BFD_RELOC_LO16;
7717 }
6e1304d8
RS
7718}
7719
252b5132
RH
7720/* Build an instruction created by a macro expansion. This is passed
7721 a pointer to the count of instructions created so far, an
7722 expression, the name of the instruction to build, an operand format
7723 string, and corresponding arguments. */
7724
252b5132 7725static void
67c0d1eb 7726macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 7727{
df58fc94 7728 const struct mips_opcode *mo = NULL;
f6688943 7729 bfd_reloc_code_real_type r[3];
df58fc94 7730 const struct mips_opcode *amo;
e077a1c8 7731 const struct mips_operand *operand;
df58fc94
RS
7732 struct hash_control *hash;
7733 struct mips_cl_insn insn;
252b5132 7734 va_list args;
e077a1c8 7735 unsigned int uval;
252b5132 7736
252b5132 7737 va_start (args, fmt);
252b5132 7738
252b5132
RH
7739 if (mips_opts.mips16)
7740 {
03ea81db 7741 mips16_macro_build (ep, name, fmt, &args);
252b5132
RH
7742 va_end (args);
7743 return;
7744 }
7745
f6688943
TS
7746 r[0] = BFD_RELOC_UNUSED;
7747 r[1] = BFD_RELOC_UNUSED;
7748 r[2] = BFD_RELOC_UNUSED;
df58fc94
RS
7749 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
7750 amo = (struct mips_opcode *) hash_find (hash, name);
7751 gas_assert (amo);
7752 gas_assert (strcmp (name, amo->name) == 0);
1e915849 7753
df58fc94 7754 do
8b082fb1
TS
7755 {
7756 /* Search until we get a match for NAME. It is assumed here that
df58fc94
RS
7757 macros will never generate MDMX, MIPS-3D, or MT instructions.
7758 We try to match an instruction that fulfils the branch delay
7759 slot instruction length requirement (if any) of the previous
7760 instruction. While doing this we record the first instruction
7761 seen that matches all the other conditions and use it anyway
7762 if the requirement cannot be met; we will issue an appropriate
7763 warning later on. */
7764 if (strcmp (fmt, amo->args) == 0
7765 && amo->pinfo != INSN_MACRO
7766 && is_opcode_valid (amo)
7767 && is_size_valid (amo))
7768 {
7769 if (is_delay_slot_valid (amo))
7770 {
7771 mo = amo;
7772 break;
7773 }
7774 else if (!mo)
7775 mo = amo;
7776 }
8b082fb1 7777
df58fc94
RS
7778 ++amo;
7779 gas_assert (amo->name);
252b5132 7780 }
df58fc94 7781 while (strcmp (name, amo->name) == 0);
252b5132 7782
df58fc94 7783 gas_assert (mo);
1e915849 7784 create_insn (&insn, mo);
e077a1c8 7785 for (; *fmt; ++fmt)
252b5132 7786 {
e077a1c8 7787 switch (*fmt)
252b5132 7788 {
252b5132
RH
7789 case ',':
7790 case '(':
7791 case ')':
252b5132 7792 case 'z':
e077a1c8 7793 break;
252b5132
RH
7794
7795 case 'i':
7796 case 'j':
6e1304d8 7797 macro_read_relocs (&args, r);
9c2799c2 7798 gas_assert (*r == BFD_RELOC_GPREL16
e391c024
RS
7799 || *r == BFD_RELOC_MIPS_HIGHER
7800 || *r == BFD_RELOC_HI16_S
7801 || *r == BFD_RELOC_LO16
7802 || *r == BFD_RELOC_MIPS_GOT_OFST);
e077a1c8 7803 break;
e391c024
RS
7804
7805 case 'o':
7806 macro_read_relocs (&args, r);
e077a1c8 7807 break;
252b5132
RH
7808
7809 case 'u':
6e1304d8 7810 macro_read_relocs (&args, r);
9c2799c2 7811 gas_assert (ep != NULL
90ecf173
MR
7812 && (ep->X_op == O_constant
7813 || (ep->X_op == O_symbol
7814 && (*r == BFD_RELOC_MIPS_HIGHEST
7815 || *r == BFD_RELOC_HI16_S
7816 || *r == BFD_RELOC_HI16
7817 || *r == BFD_RELOC_GPREL16
7818 || *r == BFD_RELOC_MIPS_GOT_HI16
7819 || *r == BFD_RELOC_MIPS_CALL_HI16))));
e077a1c8 7820 break;
252b5132
RH
7821
7822 case 'p':
9c2799c2 7823 gas_assert (ep != NULL);
bad36eac 7824
252b5132
RH
7825 /*
7826 * This allows macro() to pass an immediate expression for
7827 * creating short branches without creating a symbol.
bad36eac
DJ
7828 *
7829 * We don't allow branch relaxation for these branches, as
7830 * they should only appear in ".set nomacro" anyway.
252b5132
RH
7831 */
7832 if (ep->X_op == O_constant)
7833 {
df58fc94
RS
7834 /* For microMIPS we always use relocations for branches.
7835 So we should not resolve immediate values. */
7836 gas_assert (!mips_opts.micromips);
7837
bad36eac
DJ
7838 if ((ep->X_add_number & 3) != 0)
7839 as_bad (_("branch to misaligned address (0x%lx)"),
7840 (unsigned long) ep->X_add_number);
7841 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
7842 as_bad (_("branch address range overflow (0x%lx)"),
7843 (unsigned long) ep->X_add_number);
252b5132
RH
7844 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
7845 ep = NULL;
7846 }
7847 else
0b25d3e6 7848 *r = BFD_RELOC_16_PCREL_S2;
e077a1c8 7849 break;
252b5132
RH
7850
7851 case 'a':
9c2799c2 7852 gas_assert (ep != NULL);
f6688943 7853 *r = BFD_RELOC_MIPS_JMP;
e077a1c8 7854 break;
d43b4baf 7855
252b5132 7856 default:
e077a1c8
RS
7857 operand = (mips_opts.micromips
7858 ? decode_micromips_operand (fmt)
7859 : decode_mips_operand (fmt));
7860 if (!operand)
7861 abort ();
7862
7863 uval = va_arg (args, int);
7864 if (operand->type == OP_CLO_CLZ_DEST)
7865 uval |= (uval << 5);
7866 insn_insert_operand (&insn, operand, uval);
7867
7868 if (*fmt == '+' || *fmt == 'm')
7869 ++fmt;
7870 break;
252b5132 7871 }
252b5132
RH
7872 }
7873 va_end (args);
9c2799c2 7874 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 7875
df58fc94 7876 append_insn (&insn, ep, r, TRUE);
252b5132
RH
7877}
7878
7879static void
67c0d1eb 7880mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
03ea81db 7881 va_list *args)
252b5132 7882{
1e915849 7883 struct mips_opcode *mo;
252b5132 7884 struct mips_cl_insn insn;
e077a1c8 7885 const struct mips_operand *operand;
f6688943
TS
7886 bfd_reloc_code_real_type r[3]
7887 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 7888
1e915849 7889 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
9c2799c2
NC
7890 gas_assert (mo);
7891 gas_assert (strcmp (name, mo->name) == 0);
252b5132 7892
1e915849 7893 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 7894 {
1e915849 7895 ++mo;
9c2799c2
NC
7896 gas_assert (mo->name);
7897 gas_assert (strcmp (name, mo->name) == 0);
252b5132
RH
7898 }
7899
1e915849 7900 create_insn (&insn, mo);
e077a1c8 7901 for (; *fmt; ++fmt)
252b5132
RH
7902 {
7903 int c;
7904
e077a1c8 7905 c = *fmt;
252b5132
RH
7906 switch (c)
7907 {
252b5132
RH
7908 case ',':
7909 case '(':
7910 case ')':
e077a1c8 7911 break;
252b5132
RH
7912
7913 case '0':
7914 case 'S':
7915 case 'P':
7916 case 'R':
e077a1c8 7917 break;
252b5132
RH
7918
7919 case '<':
7920 case '>':
7921 case '4':
7922 case '5':
7923 case 'H':
7924 case 'W':
7925 case 'D':
7926 case 'j':
7927 case '8':
7928 case 'V':
7929 case 'C':
7930 case 'U':
7931 case 'k':
7932 case 'K':
7933 case 'p':
7934 case 'q':
7935 {
b886a2ab
RS
7936 offsetT value;
7937
9c2799c2 7938 gas_assert (ep != NULL);
252b5132
RH
7939
7940 if (ep->X_op != O_constant)
874e8986 7941 *r = (int) BFD_RELOC_UNUSED + c;
b886a2ab 7942 else if (calculate_reloc (*r, ep->X_add_number, &value))
252b5132 7943 {
b886a2ab 7944 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
252b5132 7945 ep = NULL;
f6688943 7946 *r = BFD_RELOC_UNUSED;
252b5132
RH
7947 }
7948 }
e077a1c8 7949 break;
252b5132 7950
e077a1c8
RS
7951 default:
7952 operand = decode_mips16_operand (c, FALSE);
7953 if (!operand)
7954 abort ();
252b5132 7955
4a06e5a2 7956 insn_insert_operand (&insn, operand, va_arg (*args, int));
e077a1c8
RS
7957 break;
7958 }
252b5132
RH
7959 }
7960
9c2799c2 7961 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 7962
df58fc94 7963 append_insn (&insn, ep, r, TRUE);
252b5132
RH
7964}
7965
438c16b8
TS
7966/*
7967 * Generate a "jalr" instruction with a relocation hint to the called
7968 * function. This occurs in NewABI PIC code.
7969 */
7970static void
df58fc94 7971macro_build_jalr (expressionS *ep, int cprestore)
438c16b8 7972{
df58fc94
RS
7973 static const bfd_reloc_code_real_type jalr_relocs[2]
7974 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
7975 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
7976 const char *jalr;
685736be 7977 char *f = NULL;
b34976b6 7978
1180b5a4 7979 if (MIPS_JALR_HINT_P (ep))
f21f8242 7980 {
cc3d92a5 7981 frag_grow (8);
f21f8242
AO
7982 f = frag_more (0);
7983 }
2906b037 7984 if (mips_opts.micromips)
df58fc94 7985 {
833794fc
MR
7986 jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
7987 ? "jalr" : "jalrs");
e64af278 7988 if (MIPS_JALR_HINT_P (ep)
833794fc 7989 || mips_opts.insn32
e64af278 7990 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
7991 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
7992 else
7993 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
7994 }
2906b037
MR
7995 else
7996 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
1180b5a4 7997 if (MIPS_JALR_HINT_P (ep))
df58fc94 7998 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
438c16b8
TS
7999}
8000
252b5132
RH
8001/*
8002 * Generate a "lui" instruction.
8003 */
8004static void
67c0d1eb 8005macro_build_lui (expressionS *ep, int regnum)
252b5132 8006{
9c2799c2 8007 gas_assert (! mips_opts.mips16);
252b5132 8008
df58fc94 8009 if (ep->X_op != O_constant)
252b5132 8010 {
9c2799c2 8011 gas_assert (ep->X_op == O_symbol);
bbe506e8
TS
8012 /* _gp_disp is a special case, used from s_cpload.
8013 __gnu_local_gp is used if mips_no_shared. */
9c2799c2 8014 gas_assert (mips_pic == NO_PIC
78e1bb40 8015 || (! HAVE_NEWABI
aa6975fb
ILT
8016 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
8017 || (! mips_in_shared
bbe506e8
TS
8018 && strcmp (S_GET_NAME (ep->X_add_symbol),
8019 "__gnu_local_gp") == 0));
252b5132
RH
8020 }
8021
df58fc94 8022 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
252b5132
RH
8023}
8024
885add95
CD
8025/* Generate a sequence of instructions to do a load or store from a constant
8026 offset off of a base register (breg) into/from a target register (treg),
8027 using AT if necessary. */
8028static void
67c0d1eb
RS
8029macro_build_ldst_constoffset (expressionS *ep, const char *op,
8030 int treg, int breg, int dbl)
885add95 8031{
9c2799c2 8032 gas_assert (ep->X_op == O_constant);
885add95 8033
256ab948 8034 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
8035 if (!dbl)
8036 normalize_constant_expr (ep);
256ab948 8037
67c1ffbe 8038 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 8039 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
8040 as_warn (_("operand overflow"));
8041
8042 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
8043 {
8044 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 8045 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
8046 }
8047 else
8048 {
8049 /* 32-bit offset, need multiple instructions and AT, like:
8050 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
8051 addu $tempreg,$tempreg,$breg
8052 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
8053 to handle the complete offset. */
67c0d1eb
RS
8054 macro_build_lui (ep, AT);
8055 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
8056 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95 8057
741fe287 8058 if (!mips_opts.at)
8fc2e39e 8059 as_bad (_("Macro used $at after \".set noat\""));
885add95
CD
8060 }
8061}
8062
252b5132
RH
8063/* set_at()
8064 * Generates code to set the $at register to true (one)
8065 * if reg is less than the immediate expression.
8066 */
8067static void
67c0d1eb 8068set_at (int reg, int unsignedp)
252b5132
RH
8069{
8070 if (imm_expr.X_op == O_constant
8071 && imm_expr.X_add_number >= -0x8000
8072 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
8073 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
8074 AT, reg, BFD_RELOC_LO16);
252b5132
RH
8075 else
8076 {
67c0d1eb
RS
8077 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
8078 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
8079 }
8080}
8081
252b5132
RH
8082/* Count the leading zeroes by performing a binary chop. This is a
8083 bulky bit of source, but performance is a LOT better for the
8084 majority of values than a simple loop to count the bits:
8085 for (lcnt = 0; (lcnt < 32); lcnt++)
8086 if ((v) & (1 << (31 - lcnt)))
8087 break;
8088 However it is not code size friendly, and the gain will drop a bit
8089 on certain cached systems.
8090*/
8091#define COUNT_TOP_ZEROES(v) \
8092 (((v) & ~0xffff) == 0 \
8093 ? ((v) & ~0xff) == 0 \
8094 ? ((v) & ~0xf) == 0 \
8095 ? ((v) & ~0x3) == 0 \
8096 ? ((v) & ~0x1) == 0 \
8097 ? !(v) \
8098 ? 32 \
8099 : 31 \
8100 : 30 \
8101 : ((v) & ~0x7) == 0 \
8102 ? 29 \
8103 : 28 \
8104 : ((v) & ~0x3f) == 0 \
8105 ? ((v) & ~0x1f) == 0 \
8106 ? 27 \
8107 : 26 \
8108 : ((v) & ~0x7f) == 0 \
8109 ? 25 \
8110 : 24 \
8111 : ((v) & ~0xfff) == 0 \
8112 ? ((v) & ~0x3ff) == 0 \
8113 ? ((v) & ~0x1ff) == 0 \
8114 ? 23 \
8115 : 22 \
8116 : ((v) & ~0x7ff) == 0 \
8117 ? 21 \
8118 : 20 \
8119 : ((v) & ~0x3fff) == 0 \
8120 ? ((v) & ~0x1fff) == 0 \
8121 ? 19 \
8122 : 18 \
8123 : ((v) & ~0x7fff) == 0 \
8124 ? 17 \
8125 : 16 \
8126 : ((v) & ~0xffffff) == 0 \
8127 ? ((v) & ~0xfffff) == 0 \
8128 ? ((v) & ~0x3ffff) == 0 \
8129 ? ((v) & ~0x1ffff) == 0 \
8130 ? 15 \
8131 : 14 \
8132 : ((v) & ~0x7ffff) == 0 \
8133 ? 13 \
8134 : 12 \
8135 : ((v) & ~0x3fffff) == 0 \
8136 ? ((v) & ~0x1fffff) == 0 \
8137 ? 11 \
8138 : 10 \
8139 : ((v) & ~0x7fffff) == 0 \
8140 ? 9 \
8141 : 8 \
8142 : ((v) & ~0xfffffff) == 0 \
8143 ? ((v) & ~0x3ffffff) == 0 \
8144 ? ((v) & ~0x1ffffff) == 0 \
8145 ? 7 \
8146 : 6 \
8147 : ((v) & ~0x7ffffff) == 0 \
8148 ? 5 \
8149 : 4 \
8150 : ((v) & ~0x3fffffff) == 0 \
8151 ? ((v) & ~0x1fffffff) == 0 \
8152 ? 3 \
8153 : 2 \
8154 : ((v) & ~0x7fffffff) == 0 \
8155 ? 1 \
8156 : 0)
8157
8158/* load_register()
67c1ffbe 8159 * This routine generates the least number of instructions necessary to load
252b5132
RH
8160 * an absolute expression value into a register.
8161 */
8162static void
67c0d1eb 8163load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
8164{
8165 int freg;
8166 expressionS hi32, lo32;
8167
8168 if (ep->X_op != O_big)
8169 {
9c2799c2 8170 gas_assert (ep->X_op == O_constant);
256ab948
TS
8171
8172 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
8173 if (!dbl)
8174 normalize_constant_expr (ep);
256ab948
TS
8175
8176 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
8177 {
8178 /* We can handle 16 bit signed values with an addiu to
8179 $zero. No need to ever use daddiu here, since $zero and
8180 the result are always correct in 32 bit mode. */
67c0d1eb 8181 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
8182 return;
8183 }
8184 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
8185 {
8186 /* We can handle 16 bit unsigned values with an ori to
8187 $zero. */
67c0d1eb 8188 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
8189 return;
8190 }
256ab948 8191 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
8192 {
8193 /* 32 bit values require an lui. */
df58fc94 8194 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 8195 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 8196 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
8197 return;
8198 }
8199 }
8200
8201 /* The value is larger than 32 bits. */
8202
2051e8c4 8203 if (!dbl || HAVE_32BIT_GPRS)
252b5132 8204 {
55e08f71
NC
8205 char value[32];
8206
8207 sprintf_vma (value, ep->X_add_number);
20e1fcfd 8208 as_bad (_("Number (0x%s) larger than 32 bits"), value);
67c0d1eb 8209 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
8210 return;
8211 }
8212
8213 if (ep->X_op != O_big)
8214 {
8215 hi32 = *ep;
8216 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
8217 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
8218 hi32.X_add_number &= 0xffffffff;
8219 lo32 = *ep;
8220 lo32.X_add_number &= 0xffffffff;
8221 }
8222 else
8223 {
9c2799c2 8224 gas_assert (ep->X_add_number > 2);
252b5132
RH
8225 if (ep->X_add_number == 3)
8226 generic_bignum[3] = 0;
8227 else if (ep->X_add_number > 4)
8228 as_bad (_("Number larger than 64 bits"));
8229 lo32.X_op = O_constant;
8230 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
8231 hi32.X_op = O_constant;
8232 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
8233 }
8234
8235 if (hi32.X_add_number == 0)
8236 freg = 0;
8237 else
8238 {
8239 int shift, bit;
8240 unsigned long hi, lo;
8241
956cd1d6 8242 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
8243 {
8244 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
8245 {
67c0d1eb 8246 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
8247 return;
8248 }
8249 if (lo32.X_add_number & 0x80000000)
8250 {
df58fc94 8251 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 8252 if (lo32.X_add_number & 0xffff)
67c0d1eb 8253 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
8254 return;
8255 }
8256 }
252b5132
RH
8257
8258 /* Check for 16bit shifted constant. We know that hi32 is
8259 non-zero, so start the mask on the first bit of the hi32
8260 value. */
8261 shift = 17;
8262 do
beae10d5
KH
8263 {
8264 unsigned long himask, lomask;
8265
8266 if (shift < 32)
8267 {
8268 himask = 0xffff >> (32 - shift);
8269 lomask = (0xffff << shift) & 0xffffffff;
8270 }
8271 else
8272 {
8273 himask = 0xffff << (shift - 32);
8274 lomask = 0;
8275 }
8276 if ((hi32.X_add_number & ~(offsetT) himask) == 0
8277 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
8278 {
8279 expressionS tmp;
8280
8281 tmp.X_op = O_constant;
8282 if (shift < 32)
8283 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
8284 | (lo32.X_add_number >> shift));
8285 else
8286 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb 8287 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
df58fc94 8288 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 8289 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
8290 return;
8291 }
f9419b05 8292 ++shift;
beae10d5
KH
8293 }
8294 while (shift <= (64 - 16));
252b5132
RH
8295
8296 /* Find the bit number of the lowest one bit, and store the
8297 shifted value in hi/lo. */
8298 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
8299 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
8300 if (lo != 0)
8301 {
8302 bit = 0;
8303 while ((lo & 1) == 0)
8304 {
8305 lo >>= 1;
8306 ++bit;
8307 }
8308 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
8309 hi >>= bit;
8310 }
8311 else
8312 {
8313 bit = 32;
8314 while ((hi & 1) == 0)
8315 {
8316 hi >>= 1;
8317 ++bit;
8318 }
8319 lo = hi;
8320 hi = 0;
8321 }
8322
8323 /* Optimize if the shifted value is a (power of 2) - 1. */
8324 if ((hi == 0 && ((lo + 1) & lo) == 0)
8325 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
8326 {
8327 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 8328 if (shift != 0)
beae10d5 8329 {
252b5132
RH
8330 expressionS tmp;
8331
8332 /* This instruction will set the register to be all
8333 ones. */
beae10d5
KH
8334 tmp.X_op = O_constant;
8335 tmp.X_add_number = (offsetT) -1;
67c0d1eb 8336 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
8337 if (bit != 0)
8338 {
8339 bit += shift;
df58fc94 8340 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 8341 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 8342 }
df58fc94 8343 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
67c0d1eb 8344 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
8345 return;
8346 }
8347 }
252b5132
RH
8348
8349 /* Sign extend hi32 before calling load_register, because we can
8350 generally get better code when we load a sign extended value. */
8351 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 8352 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 8353 load_register (reg, &hi32, 0);
252b5132
RH
8354 freg = reg;
8355 }
8356 if ((lo32.X_add_number & 0xffff0000) == 0)
8357 {
8358 if (freg != 0)
8359 {
df58fc94 8360 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
252b5132
RH
8361 freg = reg;
8362 }
8363 }
8364 else
8365 {
8366 expressionS mid16;
8367
956cd1d6 8368 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 8369 {
df58fc94
RS
8370 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
8371 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
beae10d5
KH
8372 return;
8373 }
252b5132
RH
8374
8375 if (freg != 0)
8376 {
df58fc94 8377 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
252b5132
RH
8378 freg = reg;
8379 }
8380 mid16 = lo32;
8381 mid16.X_add_number >>= 16;
67c0d1eb 8382 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
df58fc94 8383 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
252b5132
RH
8384 freg = reg;
8385 }
8386 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 8387 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
8388}
8389
269137b2
TS
8390static inline void
8391load_delay_nop (void)
8392{
8393 if (!gpr_interlocks)
8394 macro_build (NULL, "nop", "");
8395}
8396
252b5132
RH
8397/* Load an address into a register. */
8398
8399static void
67c0d1eb 8400load_address (int reg, expressionS *ep, int *used_at)
252b5132 8401{
252b5132
RH
8402 if (ep->X_op != O_constant
8403 && ep->X_op != O_symbol)
8404 {
8405 as_bad (_("expression too complex"));
8406 ep->X_op = O_constant;
8407 }
8408
8409 if (ep->X_op == O_constant)
8410 {
67c0d1eb 8411 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
8412 return;
8413 }
8414
8415 if (mips_pic == NO_PIC)
8416 {
8417 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 8418 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
8419 Otherwise we want
8420 lui $reg,<sym> (BFD_RELOC_HI16_S)
8421 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 8422 If we have an addend, we always use the latter form.
76b3015f 8423
d6bc6245
TS
8424 With 64bit address space and a usable $at we want
8425 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8426 lui $at,<sym> (BFD_RELOC_HI16_S)
8427 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
8428 daddiu $at,<sym> (BFD_RELOC_LO16)
8429 dsll32 $reg,0
3a482fd5 8430 daddu $reg,$reg,$at
76b3015f 8431
c03099e6 8432 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
8433 on superscalar processors.
8434 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8435 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
8436 dsll $reg,16
8437 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
8438 dsll $reg,16
8439 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
8440
8441 For GP relative symbols in 64bit address space we can use
8442 the same sequence as in 32bit address space. */
aed1a261 8443 if (HAVE_64BIT_SYMBOLS)
d6bc6245 8444 {
6caf9ef4
TS
8445 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
8446 && !nopic_need_relax (ep->X_add_symbol, 1))
8447 {
8448 relax_start (ep->X_add_symbol);
8449 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
8450 mips_gp_register, BFD_RELOC_GPREL16);
8451 relax_switch ();
8452 }
d6bc6245 8453
741fe287 8454 if (*used_at == 0 && mips_opts.at)
d6bc6245 8455 {
df58fc94
RS
8456 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
8457 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
67c0d1eb
RS
8458 macro_build (ep, "daddiu", "t,r,j", reg, reg,
8459 BFD_RELOC_MIPS_HIGHER);
8460 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
df58fc94 8461 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
67c0d1eb 8462 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
8463 *used_at = 1;
8464 }
8465 else
8466 {
df58fc94 8467 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb
RS
8468 macro_build (ep, "daddiu", "t,r,j", reg, reg,
8469 BFD_RELOC_MIPS_HIGHER);
df58fc94 8470 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 8471 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
df58fc94 8472 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 8473 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 8474 }
6caf9ef4
TS
8475
8476 if (mips_relax.sequence)
8477 relax_end ();
d6bc6245 8478 }
252b5132
RH
8479 else
8480 {
d6bc6245 8481 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 8482 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 8483 {
4d7206a2 8484 relax_start (ep->X_add_symbol);
67c0d1eb 8485 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 8486 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 8487 relax_switch ();
d6bc6245 8488 }
67c0d1eb
RS
8489 macro_build_lui (ep, reg);
8490 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
8491 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
8492 if (mips_relax.sequence)
8493 relax_end ();
d6bc6245 8494 }
252b5132 8495 }
0a44bf69 8496 else if (!mips_big_got)
252b5132
RH
8497 {
8498 expressionS ex;
8499
8500 /* If this is a reference to an external symbol, we want
8501 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8502 Otherwise we want
8503 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8504 nop
8505 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
8506 If there is a constant, it must be added in after.
8507
ed6fb7bd 8508 If we have NewABI, we want
f5040a92
AO
8509 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
8510 unless we're referencing a global symbol with a non-zero
8511 offset, in which case cst must be added separately. */
ed6fb7bd
SC
8512 if (HAVE_NEWABI)
8513 {
f5040a92
AO
8514 if (ep->X_add_number)
8515 {
4d7206a2 8516 ex.X_add_number = ep->X_add_number;
f5040a92 8517 ep->X_add_number = 0;
4d7206a2 8518 relax_start (ep->X_add_symbol);
67c0d1eb
RS
8519 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
8520 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
8521 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
8522 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
8523 ex.X_op = O_constant;
67c0d1eb 8524 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 8525 reg, reg, BFD_RELOC_LO16);
f5040a92 8526 ep->X_add_number = ex.X_add_number;
4d7206a2 8527 relax_switch ();
f5040a92 8528 }
67c0d1eb 8529 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 8530 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
8531 if (mips_relax.sequence)
8532 relax_end ();
ed6fb7bd
SC
8533 }
8534 else
8535 {
f5040a92
AO
8536 ex.X_add_number = ep->X_add_number;
8537 ep->X_add_number = 0;
67c0d1eb
RS
8538 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
8539 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 8540 load_delay_nop ();
4d7206a2
RS
8541 relax_start (ep->X_add_symbol);
8542 relax_switch ();
67c0d1eb 8543 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 8544 BFD_RELOC_LO16);
4d7206a2 8545 relax_end ();
ed6fb7bd 8546
f5040a92
AO
8547 if (ex.X_add_number != 0)
8548 {
8549 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
8550 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
8551 ex.X_op = O_constant;
67c0d1eb 8552 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 8553 reg, reg, BFD_RELOC_LO16);
f5040a92 8554 }
252b5132
RH
8555 }
8556 }
0a44bf69 8557 else if (mips_big_got)
252b5132
RH
8558 {
8559 expressionS ex;
252b5132
RH
8560
8561 /* This is the large GOT case. If this is a reference to an
8562 external symbol, we want
8563 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
8564 addu $reg,$reg,$gp
8565 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
8566
8567 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
8568 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8569 nop
8570 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 8571 If there is a constant, it must be added in after.
f5040a92
AO
8572
8573 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
8574 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
8575 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 8576 */
438c16b8
TS
8577 if (HAVE_NEWABI)
8578 {
4d7206a2 8579 ex.X_add_number = ep->X_add_number;
f5040a92 8580 ep->X_add_number = 0;
4d7206a2 8581 relax_start (ep->X_add_symbol);
df58fc94 8582 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
8583 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8584 reg, reg, mips_gp_register);
8585 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
8586 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
8587 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
8588 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
8589 else if (ex.X_add_number)
8590 {
8591 ex.X_op = O_constant;
67c0d1eb
RS
8592 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
8593 BFD_RELOC_LO16);
f5040a92
AO
8594 }
8595
8596 ep->X_add_number = ex.X_add_number;
4d7206a2 8597 relax_switch ();
67c0d1eb 8598 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 8599 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
8600 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
8601 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 8602 relax_end ();
438c16b8 8603 }
252b5132 8604 else
438c16b8 8605 {
f5040a92
AO
8606 ex.X_add_number = ep->X_add_number;
8607 ep->X_add_number = 0;
4d7206a2 8608 relax_start (ep->X_add_symbol);
df58fc94 8609 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
8610 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8611 reg, reg, mips_gp_register);
8612 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
8613 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
8614 relax_switch ();
8615 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
8616 {
8617 /* We need a nop before loading from $gp. This special
8618 check is required because the lui which starts the main
8619 instruction stream does not refer to $gp, and so will not
8620 insert the nop which may be required. */
67c0d1eb 8621 macro_build (NULL, "nop", "");
438c16b8 8622 }
67c0d1eb 8623 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 8624 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 8625 load_delay_nop ();
67c0d1eb 8626 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 8627 BFD_RELOC_LO16);
4d7206a2 8628 relax_end ();
438c16b8 8629
f5040a92
AO
8630 if (ex.X_add_number != 0)
8631 {
8632 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
8633 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
8634 ex.X_op = O_constant;
67c0d1eb
RS
8635 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
8636 BFD_RELOC_LO16);
f5040a92 8637 }
252b5132
RH
8638 }
8639 }
252b5132
RH
8640 else
8641 abort ();
8fc2e39e 8642
741fe287 8643 if (!mips_opts.at && *used_at == 1)
8fc2e39e 8644 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
8645}
8646
ea1fb5dc
RS
8647/* Move the contents of register SOURCE into register DEST. */
8648
8649static void
67c0d1eb 8650move_register (int dest, int source)
ea1fb5dc 8651{
df58fc94
RS
8652 /* Prefer to use a 16-bit microMIPS instruction unless the previous
8653 instruction specifically requires a 32-bit one. */
8654 if (mips_opts.micromips
833794fc 8655 && !mips_opts.insn32
df58fc94 8656 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7951ca42 8657 macro_build (NULL, "move", "mp,mj", dest, source);
df58fc94
RS
8658 else
8659 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
8660 dest, source, 0);
ea1fb5dc
RS
8661}
8662
4d7206a2 8663/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
8664 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
8665 The two alternatives are:
4d7206a2
RS
8666
8667 Global symbol Local sybmol
8668 ------------- ------------
8669 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
8670 ... ...
8671 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
8672
8673 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
8674 emits the second for a 16-bit offset or add_got_offset_hilo emits
8675 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
8676
8677static void
67c0d1eb 8678load_got_offset (int dest, expressionS *local)
4d7206a2
RS
8679{
8680 expressionS global;
8681
8682 global = *local;
8683 global.X_add_number = 0;
8684
8685 relax_start (local->X_add_symbol);
67c0d1eb
RS
8686 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
8687 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 8688 relax_switch ();
67c0d1eb
RS
8689 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
8690 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
8691 relax_end ();
8692}
8693
8694static void
67c0d1eb 8695add_got_offset (int dest, expressionS *local)
4d7206a2
RS
8696{
8697 expressionS global;
8698
8699 global.X_op = O_constant;
8700 global.X_op_symbol = NULL;
8701 global.X_add_symbol = NULL;
8702 global.X_add_number = local->X_add_number;
8703
8704 relax_start (local->X_add_symbol);
67c0d1eb 8705 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
8706 dest, dest, BFD_RELOC_LO16);
8707 relax_switch ();
67c0d1eb 8708 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
8709 relax_end ();
8710}
8711
f6a22291
MR
8712static void
8713add_got_offset_hilo (int dest, expressionS *local, int tmp)
8714{
8715 expressionS global;
8716 int hold_mips_optimize;
8717
8718 global.X_op = O_constant;
8719 global.X_op_symbol = NULL;
8720 global.X_add_symbol = NULL;
8721 global.X_add_number = local->X_add_number;
8722
8723 relax_start (local->X_add_symbol);
8724 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
8725 relax_switch ();
8726 /* Set mips_optimize around the lui instruction to avoid
8727 inserting an unnecessary nop after the lw. */
8728 hold_mips_optimize = mips_optimize;
8729 mips_optimize = 2;
8730 macro_build_lui (&global, tmp);
8731 mips_optimize = hold_mips_optimize;
8732 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
8733 relax_end ();
8734
8735 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
8736}
8737
df58fc94
RS
8738/* Emit a sequence of instructions to emulate a branch likely operation.
8739 BR is an ordinary branch corresponding to one to be emulated. BRNEG
8740 is its complementing branch with the original condition negated.
8741 CALL is set if the original branch specified the link operation.
8742 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
8743
8744 Code like this is produced in the noreorder mode:
8745
8746 BRNEG <args>, 1f
8747 nop
8748 b <sym>
8749 delay slot (executed only if branch taken)
8750 1:
8751
8752 or, if CALL is set:
8753
8754 BRNEG <args>, 1f
8755 nop
8756 bal <sym>
8757 delay slot (executed only if branch taken)
8758 1:
8759
8760 In the reorder mode the delay slot would be filled with a nop anyway,
8761 so code produced is simply:
8762
8763 BR <args>, <sym>
8764 nop
8765
8766 This function is used when producing code for the microMIPS ASE that
8767 does not implement branch likely instructions in hardware. */
8768
8769static void
8770macro_build_branch_likely (const char *br, const char *brneg,
8771 int call, expressionS *ep, const char *fmt,
8772 unsigned int sreg, unsigned int treg)
8773{
8774 int noreorder = mips_opts.noreorder;
8775 expressionS expr1;
8776
8777 gas_assert (mips_opts.micromips);
8778 start_noreorder ();
8779 if (noreorder)
8780 {
8781 micromips_label_expr (&expr1);
8782 macro_build (&expr1, brneg, fmt, sreg, treg);
8783 macro_build (NULL, "nop", "");
8784 macro_build (ep, call ? "bal" : "b", "p");
8785
8786 /* Set to true so that append_insn adds a label. */
8787 emit_branch_likely_macro = TRUE;
8788 }
8789 else
8790 {
8791 macro_build (ep, br, fmt, sreg, treg);
8792 macro_build (NULL, "nop", "");
8793 }
8794 end_noreorder ();
8795}
8796
8797/* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
8798 the condition code tested. EP specifies the branch target. */
8799
8800static void
8801macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
8802{
8803 const int call = 0;
8804 const char *brneg;
8805 const char *br;
8806
8807 switch (type)
8808 {
8809 case M_BC1FL:
8810 br = "bc1f";
8811 brneg = "bc1t";
8812 break;
8813 case M_BC1TL:
8814 br = "bc1t";
8815 brneg = "bc1f";
8816 break;
8817 case M_BC2FL:
8818 br = "bc2f";
8819 brneg = "bc2t";
8820 break;
8821 case M_BC2TL:
8822 br = "bc2t";
8823 brneg = "bc2f";
8824 break;
8825 default:
8826 abort ();
8827 }
8828 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
8829}
8830
8831/* Emit a two-argument branch macro specified by TYPE, using SREG as
8832 the register tested. EP specifies the branch target. */
8833
8834static void
8835macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
8836{
8837 const char *brneg = NULL;
8838 const char *br;
8839 int call = 0;
8840
8841 switch (type)
8842 {
8843 case M_BGEZ:
8844 br = "bgez";
8845 break;
8846 case M_BGEZL:
8847 br = mips_opts.micromips ? "bgez" : "bgezl";
8848 brneg = "bltz";
8849 break;
8850 case M_BGEZALL:
8851 gas_assert (mips_opts.micromips);
833794fc 8852 br = mips_opts.insn32 ? "bgezal" : "bgezals";
df58fc94
RS
8853 brneg = "bltz";
8854 call = 1;
8855 break;
8856 case M_BGTZ:
8857 br = "bgtz";
8858 break;
8859 case M_BGTZL:
8860 br = mips_opts.micromips ? "bgtz" : "bgtzl";
8861 brneg = "blez";
8862 break;
8863 case M_BLEZ:
8864 br = "blez";
8865 break;
8866 case M_BLEZL:
8867 br = mips_opts.micromips ? "blez" : "blezl";
8868 brneg = "bgtz";
8869 break;
8870 case M_BLTZ:
8871 br = "bltz";
8872 break;
8873 case M_BLTZL:
8874 br = mips_opts.micromips ? "bltz" : "bltzl";
8875 brneg = "bgez";
8876 break;
8877 case M_BLTZALL:
8878 gas_assert (mips_opts.micromips);
833794fc 8879 br = mips_opts.insn32 ? "bltzal" : "bltzals";
df58fc94
RS
8880 brneg = "bgez";
8881 call = 1;
8882 break;
8883 default:
8884 abort ();
8885 }
8886 if (mips_opts.micromips && brneg)
8887 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
8888 else
8889 macro_build (ep, br, "s,p", sreg);
8890}
8891
8892/* Emit a three-argument branch macro specified by TYPE, using SREG and
8893 TREG as the registers tested. EP specifies the branch target. */
8894
8895static void
8896macro_build_branch_rsrt (int type, expressionS *ep,
8897 unsigned int sreg, unsigned int treg)
8898{
8899 const char *brneg = NULL;
8900 const int call = 0;
8901 const char *br;
8902
8903 switch (type)
8904 {
8905 case M_BEQ:
8906 case M_BEQ_I:
8907 br = "beq";
8908 break;
8909 case M_BEQL:
8910 case M_BEQL_I:
8911 br = mips_opts.micromips ? "beq" : "beql";
8912 brneg = "bne";
8913 break;
8914 case M_BNE:
8915 case M_BNE_I:
8916 br = "bne";
8917 break;
8918 case M_BNEL:
8919 case M_BNEL_I:
8920 br = mips_opts.micromips ? "bne" : "bnel";
8921 brneg = "beq";
8922 break;
8923 default:
8924 abort ();
8925 }
8926 if (mips_opts.micromips && brneg)
8927 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
8928 else
8929 macro_build (ep, br, "s,t,p", sreg, treg);
8930}
8931
f2ae14a1
RS
8932/* Return the high part that should be loaded in order to make the low
8933 part of VALUE accessible using an offset of OFFBITS bits. */
8934
8935static offsetT
8936offset_high_part (offsetT value, unsigned int offbits)
8937{
8938 offsetT bias;
8939 addressT low_mask;
8940
8941 if (offbits == 0)
8942 return value;
8943 bias = 1 << (offbits - 1);
8944 low_mask = bias * 2 - 1;
8945 return (value + bias) & ~low_mask;
8946}
8947
8948/* Return true if the value stored in offset_expr and offset_reloc
8949 fits into a signed offset of OFFBITS bits. RANGE is the maximum
8950 amount that the caller wants to add without inducing overflow
8951 and ALIGN is the known alignment of the value in bytes. */
8952
8953static bfd_boolean
8954small_offset_p (unsigned int range, unsigned int align, unsigned int offbits)
8955{
8956 if (offbits == 16)
8957 {
8958 /* Accept any relocation operator if overflow isn't a concern. */
8959 if (range < align && *offset_reloc != BFD_RELOC_UNUSED)
8960 return TRUE;
8961
8962 /* These relocations are guaranteed not to overflow in correct links. */
8963 if (*offset_reloc == BFD_RELOC_MIPS_LITERAL
8964 || gprel16_reloc_p (*offset_reloc))
8965 return TRUE;
8966 }
8967 if (offset_expr.X_op == O_constant
8968 && offset_high_part (offset_expr.X_add_number, offbits) == 0
8969 && offset_high_part (offset_expr.X_add_number + range, offbits) == 0)
8970 return TRUE;
8971 return FALSE;
8972}
8973
252b5132
RH
8974/*
8975 * Build macros
8976 * This routine implements the seemingly endless macro or synthesized
8977 * instructions and addressing modes in the mips assembly language. Many
8978 * of these macros are simple and are similar to each other. These could
67c1ffbe 8979 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
8980 * this verbose method. Others are not simple macros but are more like
8981 * optimizing code generation.
8982 * One interesting optimization is when several store macros appear
67c1ffbe 8983 * consecutively that would load AT with the upper half of the same address.
252b5132
RH
8984 * The ensuing load upper instructions are ommited. This implies some kind
8985 * of global optimization. We currently only optimize within a single macro.
8986 * For many of the load and store macros if the address is specified as a
8987 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
8988 * first load register 'at' with zero and use it as the base register. The
8989 * mips assembler simply uses register $zero. Just one tiny optimization
8990 * we're missing.
8991 */
8992static void
833794fc 8993macro (struct mips_cl_insn *ip, char *str)
252b5132 8994{
c0ebe874
RS
8995 const struct mips_operand_array *operands;
8996 unsigned int breg, i;
741fe287 8997 unsigned int tempreg;
252b5132 8998 int mask;
43841e91 8999 int used_at = 0;
df58fc94 9000 expressionS label_expr;
252b5132 9001 expressionS expr1;
df58fc94 9002 expressionS *ep;
252b5132
RH
9003 const char *s;
9004 const char *s2;
9005 const char *fmt;
9006 int likely = 0;
252b5132 9007 int coproc = 0;
7f3c4072 9008 int offbits = 16;
1abe91b1 9009 int call = 0;
df58fc94
RS
9010 int jals = 0;
9011 int dbl = 0;
9012 int imm = 0;
9013 int ust = 0;
9014 int lp = 0;
f2ae14a1 9015 bfd_boolean large_offset;
252b5132 9016 int off;
252b5132 9017 int hold_mips_optimize;
f2ae14a1 9018 unsigned int align;
c0ebe874 9019 unsigned int op[MAX_OPERANDS];
252b5132 9020
9c2799c2 9021 gas_assert (! mips_opts.mips16);
252b5132 9022
c0ebe874
RS
9023 operands = insn_operands (ip);
9024 for (i = 0; i < MAX_OPERANDS; i++)
9025 if (operands->operand[i])
9026 op[i] = insn_extract_operand (ip, operands->operand[i]);
9027 else
9028 op[i] = -1;
9029
252b5132
RH
9030 mask = ip->insn_mo->mask;
9031
df58fc94
RS
9032 label_expr.X_op = O_constant;
9033 label_expr.X_op_symbol = NULL;
9034 label_expr.X_add_symbol = NULL;
9035 label_expr.X_add_number = 0;
9036
252b5132
RH
9037 expr1.X_op = O_constant;
9038 expr1.X_op_symbol = NULL;
9039 expr1.X_add_symbol = NULL;
9040 expr1.X_add_number = 1;
f2ae14a1 9041 align = 1;
252b5132
RH
9042
9043 switch (mask)
9044 {
9045 case M_DABS:
9046 dbl = 1;
9047 case M_ABS:
df58fc94
RS
9048 /* bgez $a0,1f
9049 move v0,$a0
9050 sub v0,$zero,$a0
9051 1:
9052 */
252b5132 9053
7d10b47d 9054 start_noreorder ();
252b5132 9055
df58fc94
RS
9056 if (mips_opts.micromips)
9057 micromips_label_expr (&label_expr);
9058 else
9059 label_expr.X_add_number = 8;
c0ebe874
RS
9060 macro_build (&label_expr, "bgez", "s,p", op[1]);
9061 if (op[0] == op[1])
a605d2b3 9062 macro_build (NULL, "nop", "");
252b5132 9063 else
c0ebe874
RS
9064 move_register (op[0], op[1]);
9065 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", op[0], 0, op[1]);
df58fc94
RS
9066 if (mips_opts.micromips)
9067 micromips_add_label ();
252b5132 9068
7d10b47d 9069 end_noreorder ();
8fc2e39e 9070 break;
252b5132
RH
9071
9072 case M_ADD_I:
9073 s = "addi";
9074 s2 = "add";
9075 goto do_addi;
9076 case M_ADDU_I:
9077 s = "addiu";
9078 s2 = "addu";
9079 goto do_addi;
9080 case M_DADD_I:
9081 dbl = 1;
9082 s = "daddi";
9083 s2 = "dadd";
df58fc94
RS
9084 if (!mips_opts.micromips)
9085 goto do_addi;
9086 if (imm_expr.X_op == O_constant
9087 && imm_expr.X_add_number >= -0x200
9088 && imm_expr.X_add_number < 0x200)
9089 {
c0ebe874 9090 macro_build (NULL, s, "t,r,.", op[0], op[1], imm_expr.X_add_number);
df58fc94
RS
9091 break;
9092 }
9093 goto do_addi_i;
252b5132
RH
9094 case M_DADDU_I:
9095 dbl = 1;
9096 s = "daddiu";
9097 s2 = "daddu";
9098 do_addi:
9099 if (imm_expr.X_op == O_constant
9100 && imm_expr.X_add_number >= -0x8000
9101 && imm_expr.X_add_number < 0x8000)
9102 {
c0ebe874 9103 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 9104 break;
252b5132 9105 }
df58fc94 9106 do_addi_i:
8fc2e39e 9107 used_at = 1;
67c0d1eb 9108 load_register (AT, &imm_expr, dbl);
c0ebe874 9109 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
9110 break;
9111
9112 case M_AND_I:
9113 s = "andi";
9114 s2 = "and";
9115 goto do_bit;
9116 case M_OR_I:
9117 s = "ori";
9118 s2 = "or";
9119 goto do_bit;
9120 case M_NOR_I:
9121 s = "";
9122 s2 = "nor";
9123 goto do_bit;
9124 case M_XOR_I:
9125 s = "xori";
9126 s2 = "xor";
9127 do_bit:
9128 if (imm_expr.X_op == O_constant
9129 && imm_expr.X_add_number >= 0
9130 && imm_expr.X_add_number < 0x10000)
9131 {
9132 if (mask != M_NOR_I)
c0ebe874 9133 macro_build (&imm_expr, s, "t,r,i", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
9134 else
9135 {
67c0d1eb 9136 macro_build (&imm_expr, "ori", "t,r,i",
c0ebe874
RS
9137 op[0], op[1], BFD_RELOC_LO16);
9138 macro_build (NULL, "nor", "d,v,t", op[0], op[0], 0);
252b5132 9139 }
8fc2e39e 9140 break;
252b5132
RH
9141 }
9142
8fc2e39e 9143 used_at = 1;
67c0d1eb 9144 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 9145 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
9146 break;
9147
8b082fb1
TS
9148 case M_BALIGN:
9149 switch (imm_expr.X_add_number)
9150 {
9151 case 0:
9152 macro_build (NULL, "nop", "");
9153 break;
9154 case 2:
c0ebe874 9155 macro_build (NULL, "packrl.ph", "d,s,t", op[0], op[0], op[1]);
8b082fb1 9156 break;
03f66e8a
MR
9157 case 1:
9158 case 3:
c0ebe874 9159 macro_build (NULL, "balign", "t,s,2", op[0], op[1],
90ecf173 9160 (int) imm_expr.X_add_number);
8b082fb1 9161 break;
03f66e8a
MR
9162 default:
9163 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
9164 (unsigned long) imm_expr.X_add_number);
9165 break;
8b082fb1
TS
9166 }
9167 break;
9168
df58fc94
RS
9169 case M_BC1FL:
9170 case M_BC1TL:
9171 case M_BC2FL:
9172 case M_BC2TL:
9173 gas_assert (mips_opts.micromips);
9174 macro_build_branch_ccl (mask, &offset_expr,
9175 EXTRACT_OPERAND (1, BCC, *ip));
9176 break;
9177
252b5132 9178 case M_BEQ_I:
252b5132 9179 case M_BEQL_I:
252b5132 9180 case M_BNE_I:
252b5132 9181 case M_BNEL_I:
252b5132 9182 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
c0ebe874 9183 op[1] = 0;
df58fc94 9184 else
252b5132 9185 {
c0ebe874 9186 op[1] = AT;
df58fc94 9187 used_at = 1;
c0ebe874 9188 load_register (op[1], &imm_expr, HAVE_64BIT_GPRS);
252b5132 9189 }
df58fc94
RS
9190 /* Fall through. */
9191 case M_BEQL:
9192 case M_BNEL:
c0ebe874 9193 macro_build_branch_rsrt (mask, &offset_expr, op[0], op[1]);
252b5132
RH
9194 break;
9195
9196 case M_BGEL:
9197 likely = 1;
9198 case M_BGE:
c0ebe874
RS
9199 if (op[1] == 0)
9200 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[0]);
9201 else if (op[0] == 0)
9202 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[1]);
df58fc94 9203 else
252b5132 9204 {
df58fc94 9205 used_at = 1;
c0ebe874 9206 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
9207 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
9208 &offset_expr, AT, ZERO);
252b5132 9209 }
df58fc94
RS
9210 break;
9211
9212 case M_BGEZL:
9213 case M_BGEZALL:
9214 case M_BGTZL:
9215 case M_BLEZL:
9216 case M_BLTZL:
9217 case M_BLTZALL:
c0ebe874 9218 macro_build_branch_rs (mask, &offset_expr, op[0]);
252b5132
RH
9219 break;
9220
9221 case M_BGTL_I:
9222 likely = 1;
9223 case M_BGT_I:
90ecf173 9224 /* Check for > max integer. */
42429eac 9225 if (imm_expr.X_op == O_constant && imm_expr.X_add_number >= GPR_SMAX)
252b5132
RH
9226 {
9227 do_false:
90ecf173 9228 /* Result is always false. */
252b5132 9229 if (! likely)
a605d2b3 9230 macro_build (NULL, "nop", "");
252b5132 9231 else
df58fc94 9232 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
8fc2e39e 9233 break;
252b5132
RH
9234 }
9235 if (imm_expr.X_op != O_constant)
9236 as_bad (_("Unsupported large constant"));
f9419b05 9237 ++imm_expr.X_add_number;
252b5132
RH
9238 /* FALLTHROUGH */
9239 case M_BGE_I:
9240 case M_BGEL_I:
9241 if (mask == M_BGEL_I)
9242 likely = 1;
9243 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
9244 {
df58fc94 9245 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
c0ebe874 9246 &offset_expr, op[0]);
8fc2e39e 9247 break;
252b5132
RH
9248 }
9249 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
9250 {
df58fc94 9251 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
c0ebe874 9252 &offset_expr, op[0]);
8fc2e39e 9253 break;
252b5132 9254 }
42429eac 9255 if (imm_expr.X_op == O_constant && imm_expr.X_add_number <= GPR_SMIN)
252b5132
RH
9256 {
9257 do_true:
9258 /* result is always true */
9259 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
67c0d1eb 9260 macro_build (&offset_expr, "b", "p");
8fc2e39e 9261 break;
252b5132 9262 }
8fc2e39e 9263 used_at = 1;
c0ebe874 9264 set_at (op[0], 0);
df58fc94
RS
9265 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
9266 &offset_expr, AT, ZERO);
252b5132
RH
9267 break;
9268
9269 case M_BGEUL:
9270 likely = 1;
9271 case M_BGEU:
c0ebe874 9272 if (op[1] == 0)
252b5132 9273 goto do_true;
c0ebe874 9274 else if (op[0] == 0)
df58fc94 9275 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 9276 &offset_expr, ZERO, op[1]);
df58fc94 9277 else
252b5132 9278 {
df58fc94 9279 used_at = 1;
c0ebe874 9280 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
9281 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
9282 &offset_expr, AT, ZERO);
252b5132 9283 }
252b5132
RH
9284 break;
9285
9286 case M_BGTUL_I:
9287 likely = 1;
9288 case M_BGTU_I:
c0ebe874 9289 if (op[0] == 0
ca4e0257 9290 || (HAVE_32BIT_GPRS
252b5132 9291 && imm_expr.X_op == O_constant
f01dc953 9292 && imm_expr.X_add_number == -1))
252b5132
RH
9293 goto do_false;
9294 if (imm_expr.X_op != O_constant)
9295 as_bad (_("Unsupported large constant"));
f9419b05 9296 ++imm_expr.X_add_number;
252b5132
RH
9297 /* FALLTHROUGH */
9298 case M_BGEU_I:
9299 case M_BGEUL_I:
9300 if (mask == M_BGEUL_I)
9301 likely = 1;
9302 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
9303 goto do_true;
df58fc94
RS
9304 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
9305 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 9306 &offset_expr, op[0], ZERO);
df58fc94 9307 else
252b5132 9308 {
df58fc94 9309 used_at = 1;
c0ebe874 9310 set_at (op[0], 1);
df58fc94
RS
9311 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
9312 &offset_expr, AT, ZERO);
252b5132 9313 }
252b5132
RH
9314 break;
9315
9316 case M_BGTL:
9317 likely = 1;
9318 case M_BGT:
c0ebe874
RS
9319 if (op[1] == 0)
9320 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[0]);
9321 else if (op[0] == 0)
9322 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[1]);
df58fc94 9323 else
252b5132 9324 {
df58fc94 9325 used_at = 1;
c0ebe874 9326 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
9327 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
9328 &offset_expr, AT, ZERO);
252b5132 9329 }
252b5132
RH
9330 break;
9331
9332 case M_BGTUL:
9333 likely = 1;
9334 case M_BGTU:
c0ebe874 9335 if (op[1] == 0)
df58fc94 9336 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874
RS
9337 &offset_expr, op[0], ZERO);
9338 else if (op[0] == 0)
df58fc94
RS
9339 goto do_false;
9340 else
252b5132 9341 {
df58fc94 9342 used_at = 1;
c0ebe874 9343 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
9344 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
9345 &offset_expr, AT, ZERO);
252b5132 9346 }
252b5132
RH
9347 break;
9348
9349 case M_BLEL:
9350 likely = 1;
9351 case M_BLE:
c0ebe874
RS
9352 if (op[1] == 0)
9353 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
9354 else if (op[0] == 0)
9355 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[1]);
df58fc94 9356 else
252b5132 9357 {
df58fc94 9358 used_at = 1;
c0ebe874 9359 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
9360 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
9361 &offset_expr, AT, ZERO);
252b5132 9362 }
252b5132
RH
9363 break;
9364
9365 case M_BLEL_I:
9366 likely = 1;
9367 case M_BLE_I:
42429eac 9368 if (imm_expr.X_op == O_constant && imm_expr.X_add_number >= GPR_SMAX)
252b5132
RH
9369 goto do_true;
9370 if (imm_expr.X_op != O_constant)
9371 as_bad (_("Unsupported large constant"));
f9419b05 9372 ++imm_expr.X_add_number;
252b5132
RH
9373 /* FALLTHROUGH */
9374 case M_BLT_I:
9375 case M_BLTL_I:
9376 if (mask == M_BLTL_I)
9377 likely = 1;
9378 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
c0ebe874 9379 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
df58fc94 9380 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
c0ebe874 9381 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
df58fc94 9382 else
252b5132 9383 {
df58fc94 9384 used_at = 1;
c0ebe874 9385 set_at (op[0], 0);
df58fc94
RS
9386 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
9387 &offset_expr, AT, ZERO);
252b5132 9388 }
252b5132
RH
9389 break;
9390
9391 case M_BLEUL:
9392 likely = 1;
9393 case M_BLEU:
c0ebe874 9394 if (op[1] == 0)
df58fc94 9395 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874
RS
9396 &offset_expr, op[0], ZERO);
9397 else if (op[0] == 0)
df58fc94
RS
9398 goto do_true;
9399 else
252b5132 9400 {
df58fc94 9401 used_at = 1;
c0ebe874 9402 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
9403 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
9404 &offset_expr, AT, ZERO);
252b5132 9405 }
252b5132
RH
9406 break;
9407
9408 case M_BLEUL_I:
9409 likely = 1;
9410 case M_BLEU_I:
c0ebe874 9411 if (op[0] == 0
ca4e0257 9412 || (HAVE_32BIT_GPRS
252b5132 9413 && imm_expr.X_op == O_constant
f01dc953 9414 && imm_expr.X_add_number == -1))
252b5132
RH
9415 goto do_true;
9416 if (imm_expr.X_op != O_constant)
9417 as_bad (_("Unsupported large constant"));
f9419b05 9418 ++imm_expr.X_add_number;
252b5132
RH
9419 /* FALLTHROUGH */
9420 case M_BLTU_I:
9421 case M_BLTUL_I:
9422 if (mask == M_BLTUL_I)
9423 likely = 1;
9424 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
9425 goto do_false;
df58fc94
RS
9426 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
9427 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 9428 &offset_expr, op[0], ZERO);
df58fc94 9429 else
252b5132 9430 {
df58fc94 9431 used_at = 1;
c0ebe874 9432 set_at (op[0], 1);
df58fc94
RS
9433 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
9434 &offset_expr, AT, ZERO);
252b5132 9435 }
252b5132
RH
9436 break;
9437
9438 case M_BLTL:
9439 likely = 1;
9440 case M_BLT:
c0ebe874
RS
9441 if (op[1] == 0)
9442 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
9443 else if (op[0] == 0)
9444 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[1]);
df58fc94 9445 else
252b5132 9446 {
df58fc94 9447 used_at = 1;
c0ebe874 9448 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
9449 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
9450 &offset_expr, AT, ZERO);
252b5132 9451 }
252b5132
RH
9452 break;
9453
9454 case M_BLTUL:
9455 likely = 1;
9456 case M_BLTU:
c0ebe874 9457 if (op[1] == 0)
252b5132 9458 goto do_false;
c0ebe874 9459 else if (op[0] == 0)
df58fc94 9460 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 9461 &offset_expr, ZERO, op[1]);
df58fc94 9462 else
252b5132 9463 {
df58fc94 9464 used_at = 1;
c0ebe874 9465 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
9466 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
9467 &offset_expr, AT, ZERO);
252b5132 9468 }
252b5132
RH
9469 break;
9470
9471 case M_DDIV_3:
9472 dbl = 1;
9473 case M_DIV_3:
9474 s = "mflo";
9475 goto do_div3;
9476 case M_DREM_3:
9477 dbl = 1;
9478 case M_REM_3:
9479 s = "mfhi";
9480 do_div3:
c0ebe874 9481 if (op[2] == 0)
252b5132
RH
9482 {
9483 as_warn (_("Divide by zero."));
9484 if (mips_trap)
df58fc94 9485 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 9486 else
df58fc94 9487 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 9488 break;
252b5132
RH
9489 }
9490
7d10b47d 9491 start_noreorder ();
252b5132
RH
9492 if (mips_trap)
9493 {
c0ebe874
RS
9494 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
9495 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
252b5132
RH
9496 }
9497 else
9498 {
df58fc94
RS
9499 if (mips_opts.micromips)
9500 micromips_label_expr (&label_expr);
9501 else
9502 label_expr.X_add_number = 8;
c0ebe874
RS
9503 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
9504 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
df58fc94
RS
9505 macro_build (NULL, "break", BRK_FMT, 7);
9506 if (mips_opts.micromips)
9507 micromips_add_label ();
252b5132
RH
9508 }
9509 expr1.X_add_number = -1;
8fc2e39e 9510 used_at = 1;
f6a22291 9511 load_register (AT, &expr1, dbl);
df58fc94
RS
9512 if (mips_opts.micromips)
9513 micromips_label_expr (&label_expr);
9514 else
9515 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
c0ebe874 9516 macro_build (&label_expr, "bne", "s,t,p", op[2], AT);
252b5132
RH
9517 if (dbl)
9518 {
9519 expr1.X_add_number = 1;
f6a22291 9520 load_register (AT, &expr1, dbl);
df58fc94 9521 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
252b5132
RH
9522 }
9523 else
9524 {
9525 expr1.X_add_number = 0x80000000;
df58fc94 9526 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
252b5132
RH
9527 }
9528 if (mips_trap)
9529 {
c0ebe874 9530 macro_build (NULL, "teq", TRAP_FMT, op[1], AT, 6);
252b5132
RH
9531 /* We want to close the noreorder block as soon as possible, so
9532 that later insns are available for delay slot filling. */
7d10b47d 9533 end_noreorder ();
252b5132
RH
9534 }
9535 else
9536 {
df58fc94
RS
9537 if (mips_opts.micromips)
9538 micromips_label_expr (&label_expr);
9539 else
9540 label_expr.X_add_number = 8;
c0ebe874 9541 macro_build (&label_expr, "bne", "s,t,p", op[1], AT);
a605d2b3 9542 macro_build (NULL, "nop", "");
252b5132
RH
9543
9544 /* We want to close the noreorder block as soon as possible, so
9545 that later insns are available for delay slot filling. */
7d10b47d 9546 end_noreorder ();
252b5132 9547
df58fc94 9548 macro_build (NULL, "break", BRK_FMT, 6);
252b5132 9549 }
df58fc94
RS
9550 if (mips_opts.micromips)
9551 micromips_add_label ();
c0ebe874 9552 macro_build (NULL, s, MFHL_FMT, op[0]);
252b5132
RH
9553 break;
9554
9555 case M_DIV_3I:
9556 s = "div";
9557 s2 = "mflo";
9558 goto do_divi;
9559 case M_DIVU_3I:
9560 s = "divu";
9561 s2 = "mflo";
9562 goto do_divi;
9563 case M_REM_3I:
9564 s = "div";
9565 s2 = "mfhi";
9566 goto do_divi;
9567 case M_REMU_3I:
9568 s = "divu";
9569 s2 = "mfhi";
9570 goto do_divi;
9571 case M_DDIV_3I:
9572 dbl = 1;
9573 s = "ddiv";
9574 s2 = "mflo";
9575 goto do_divi;
9576 case M_DDIVU_3I:
9577 dbl = 1;
9578 s = "ddivu";
9579 s2 = "mflo";
9580 goto do_divi;
9581 case M_DREM_3I:
9582 dbl = 1;
9583 s = "ddiv";
9584 s2 = "mfhi";
9585 goto do_divi;
9586 case M_DREMU_3I:
9587 dbl = 1;
9588 s = "ddivu";
9589 s2 = "mfhi";
9590 do_divi:
9591 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
9592 {
9593 as_warn (_("Divide by zero."));
9594 if (mips_trap)
df58fc94 9595 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 9596 else
df58fc94 9597 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 9598 break;
252b5132
RH
9599 }
9600 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
9601 {
9602 if (strcmp (s2, "mflo") == 0)
c0ebe874 9603 move_register (op[0], op[1]);
252b5132 9604 else
c0ebe874 9605 move_register (op[0], ZERO);
8fc2e39e 9606 break;
252b5132
RH
9607 }
9608 if (imm_expr.X_op == O_constant
9609 && imm_expr.X_add_number == -1
9610 && s[strlen (s) - 1] != 'u')
9611 {
9612 if (strcmp (s2, "mflo") == 0)
c0ebe874 9613 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", op[0], op[1]);
252b5132 9614 else
c0ebe874 9615 move_register (op[0], ZERO);
8fc2e39e 9616 break;
252b5132
RH
9617 }
9618
8fc2e39e 9619 used_at = 1;
67c0d1eb 9620 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
9621 macro_build (NULL, s, "z,s,t", op[1], AT);
9622 macro_build (NULL, s2, MFHL_FMT, op[0]);
252b5132
RH
9623 break;
9624
9625 case M_DIVU_3:
9626 s = "divu";
9627 s2 = "mflo";
9628 goto do_divu3;
9629 case M_REMU_3:
9630 s = "divu";
9631 s2 = "mfhi";
9632 goto do_divu3;
9633 case M_DDIVU_3:
9634 s = "ddivu";
9635 s2 = "mflo";
9636 goto do_divu3;
9637 case M_DREMU_3:
9638 s = "ddivu";
9639 s2 = "mfhi";
9640 do_divu3:
7d10b47d 9641 start_noreorder ();
252b5132
RH
9642 if (mips_trap)
9643 {
c0ebe874
RS
9644 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
9645 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
9646 /* We want to close the noreorder block as soon as possible, so
9647 that later insns are available for delay slot filling. */
7d10b47d 9648 end_noreorder ();
252b5132
RH
9649 }
9650 else
9651 {
df58fc94
RS
9652 if (mips_opts.micromips)
9653 micromips_label_expr (&label_expr);
9654 else
9655 label_expr.X_add_number = 8;
c0ebe874
RS
9656 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
9657 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
9658
9659 /* We want to close the noreorder block as soon as possible, so
9660 that later insns are available for delay slot filling. */
7d10b47d 9661 end_noreorder ();
df58fc94
RS
9662 macro_build (NULL, "break", BRK_FMT, 7);
9663 if (mips_opts.micromips)
9664 micromips_add_label ();
252b5132 9665 }
c0ebe874 9666 macro_build (NULL, s2, MFHL_FMT, op[0]);
8fc2e39e 9667 break;
252b5132 9668
1abe91b1
MR
9669 case M_DLCA_AB:
9670 dbl = 1;
9671 case M_LCA_AB:
9672 call = 1;
9673 goto do_la;
252b5132
RH
9674 case M_DLA_AB:
9675 dbl = 1;
9676 case M_LA_AB:
1abe91b1 9677 do_la:
252b5132
RH
9678 /* Load the address of a symbol into a register. If breg is not
9679 zero, we then add a base register to it. */
9680
c0ebe874 9681 breg = op[2];
3bec30a8
TS
9682 if (dbl && HAVE_32BIT_GPRS)
9683 as_warn (_("dla used to load 32-bit register"));
9684
90ecf173 9685 if (!dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
9686 as_warn (_("la used to load 64-bit address"));
9687
f2ae14a1 9688 if (small_offset_p (0, align, 16))
0c11417f 9689 {
c0ebe874 9690 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", op[0], breg,
f2ae14a1 9691 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8fc2e39e 9692 break;
0c11417f
MR
9693 }
9694
c0ebe874 9695 if (mips_opts.at && (op[0] == breg))
afdbd6d0
CD
9696 {
9697 tempreg = AT;
9698 used_at = 1;
9699 }
9700 else
c0ebe874 9701 tempreg = op[0];
afdbd6d0 9702
252b5132
RH
9703 if (offset_expr.X_op != O_symbol
9704 && offset_expr.X_op != O_constant)
9705 {
f71d0d44 9706 as_bad (_("Expression too complex"));
252b5132
RH
9707 offset_expr.X_op = O_constant;
9708 }
9709
252b5132 9710 if (offset_expr.X_op == O_constant)
aed1a261 9711 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
9712 else if (mips_pic == NO_PIC)
9713 {
d6bc6245 9714 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 9715 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
9716 Otherwise we want
9717 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
9718 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
9719 If we have a constant, we need two instructions anyhow,
d6bc6245 9720 so we may as well always use the latter form.
76b3015f 9721
6caf9ef4
TS
9722 With 64bit address space and a usable $at we want
9723 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9724 lui $at,<sym> (BFD_RELOC_HI16_S)
9725 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
9726 daddiu $at,<sym> (BFD_RELOC_LO16)
9727 dsll32 $tempreg,0
9728 daddu $tempreg,$tempreg,$at
9729
9730 If $at is already in use, we use a path which is suboptimal
9731 on superscalar processors.
9732 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9733 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
9734 dsll $tempreg,16
9735 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
9736 dsll $tempreg,16
9737 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
9738
9739 For GP relative symbols in 64bit address space we can use
9740 the same sequence as in 32bit address space. */
aed1a261 9741 if (HAVE_64BIT_SYMBOLS)
252b5132 9742 {
6caf9ef4
TS
9743 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
9744 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
9745 {
9746 relax_start (offset_expr.X_add_symbol);
9747 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
9748 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
9749 relax_switch ();
9750 }
d6bc6245 9751
741fe287 9752 if (used_at == 0 && mips_opts.at)
98d3f06f 9753 {
df58fc94 9754 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 9755 tempreg, BFD_RELOC_MIPS_HIGHEST);
df58fc94 9756 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 9757 AT, BFD_RELOC_HI16_S);
67c0d1eb 9758 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 9759 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 9760 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 9761 AT, AT, BFD_RELOC_LO16);
df58fc94 9762 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 9763 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
9764 used_at = 1;
9765 }
9766 else
9767 {
df58fc94 9768 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 9769 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 9770 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 9771 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 9772 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 9773 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 9774 tempreg, tempreg, BFD_RELOC_HI16_S);
df58fc94 9775 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 9776 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 9777 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 9778 }
6caf9ef4
TS
9779
9780 if (mips_relax.sequence)
9781 relax_end ();
98d3f06f
KH
9782 }
9783 else
9784 {
9785 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 9786 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 9787 {
4d7206a2 9788 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
9789 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
9790 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 9791 relax_switch ();
98d3f06f 9792 }
6943caf0 9793 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
f71d0d44 9794 as_bad (_("Offset too large"));
67c0d1eb
RS
9795 macro_build_lui (&offset_expr, tempreg);
9796 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
9797 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
9798 if (mips_relax.sequence)
9799 relax_end ();
98d3f06f 9800 }
252b5132 9801 }
0a44bf69 9802 else if (!mips_big_got && !HAVE_NEWABI)
252b5132 9803 {
9117d219
NC
9804 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
9805
252b5132
RH
9806 /* If this is a reference to an external symbol, and there
9807 is no constant, we want
9808 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 9809 or for lca or if tempreg is PIC_CALL_REG
9117d219 9810 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
9811 For a local symbol, we want
9812 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9813 nop
9814 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
9815
9816 If we have a small constant, and this is a reference to
9817 an external symbol, we want
9818 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9819 nop
9820 addiu $tempreg,$tempreg,<constant>
9821 For a local symbol, we want the same instruction
9822 sequence, but we output a BFD_RELOC_LO16 reloc on the
9823 addiu instruction.
9824
9825 If we have a large constant, and this is a reference to
9826 an external symbol, we want
9827 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9828 lui $at,<hiconstant>
9829 addiu $at,$at,<loconstant>
9830 addu $tempreg,$tempreg,$at
9831 For a local symbol, we want the same instruction
9832 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 9833 addiu instruction.
ed6fb7bd
SC
9834 */
9835
4d7206a2 9836 if (offset_expr.X_add_number == 0)
252b5132 9837 {
0a44bf69
RS
9838 if (mips_pic == SVR4_PIC
9839 && breg == 0
9840 && (call || tempreg == PIC_CALL_REG))
4d7206a2
RS
9841 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
9842
9843 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
9844 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9845 lw_reloc_type, mips_gp_register);
4d7206a2 9846 if (breg != 0)
252b5132
RH
9847 {
9848 /* We're going to put in an addu instruction using
9849 tempreg, so we may as well insert the nop right
9850 now. */
269137b2 9851 load_delay_nop ();
252b5132 9852 }
4d7206a2 9853 relax_switch ();
67c0d1eb
RS
9854 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9855 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9856 load_delay_nop ();
67c0d1eb
RS
9857 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
9858 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 9859 relax_end ();
252b5132
RH
9860 /* FIXME: If breg == 0, and the next instruction uses
9861 $tempreg, then if this variant case is used an extra
9862 nop will be generated. */
9863 }
4d7206a2
RS
9864 else if (offset_expr.X_add_number >= -0x8000
9865 && offset_expr.X_add_number < 0x8000)
252b5132 9866 {
67c0d1eb 9867 load_got_offset (tempreg, &offset_expr);
269137b2 9868 load_delay_nop ();
67c0d1eb 9869 add_got_offset (tempreg, &offset_expr);
252b5132
RH
9870 }
9871 else
9872 {
4d7206a2
RS
9873 expr1.X_add_number = offset_expr.X_add_number;
9874 offset_expr.X_add_number =
43c0598f 9875 SEXT_16BIT (offset_expr.X_add_number);
67c0d1eb 9876 load_got_offset (tempreg, &offset_expr);
f6a22291 9877 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
9878 /* If we are going to add in a base register, and the
9879 target register and the base register are the same,
9880 then we are using AT as a temporary register. Since
9881 we want to load the constant into AT, we add our
9882 current AT (from the global offset table) and the
9883 register into the register now, and pretend we were
9884 not using a base register. */
c0ebe874 9885 if (breg == op[0])
252b5132 9886 {
269137b2 9887 load_delay_nop ();
67c0d1eb 9888 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 9889 op[0], AT, breg);
252b5132 9890 breg = 0;
c0ebe874 9891 tempreg = op[0];
252b5132 9892 }
f6a22291 9893 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
9894 used_at = 1;
9895 }
9896 }
0a44bf69 9897 else if (!mips_big_got && HAVE_NEWABI)
f5040a92 9898 {
67c0d1eb 9899 int add_breg_early = 0;
f5040a92
AO
9900
9901 /* If this is a reference to an external, and there is no
9902 constant, or local symbol (*), with or without a
9903 constant, we want
9904 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 9905 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
9906 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
9907
9908 If we have a small constant, and this is a reference to
9909 an external symbol, we want
9910 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
9911 addiu $tempreg,$tempreg,<constant>
9912
9913 If we have a large constant, and this is a reference to
9914 an external symbol, we want
9915 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
9916 lui $at,<hiconstant>
9917 addiu $at,$at,<loconstant>
9918 addu $tempreg,$tempreg,$at
9919
9920 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
9921 local symbols, even though it introduces an additional
9922 instruction. */
9923
f5040a92
AO
9924 if (offset_expr.X_add_number)
9925 {
4d7206a2 9926 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
9927 offset_expr.X_add_number = 0;
9928
4d7206a2 9929 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
9930 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9931 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
9932
9933 if (expr1.X_add_number >= -0x8000
9934 && expr1.X_add_number < 0x8000)
9935 {
67c0d1eb
RS
9936 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
9937 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 9938 }
ecd13cd3 9939 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 9940 {
c0ebe874
RS
9941 unsigned int dreg;
9942
f5040a92
AO
9943 /* If we are going to add in a base register, and the
9944 target register and the base register are the same,
9945 then we are using AT as a temporary register. Since
9946 we want to load the constant into AT, we add our
9947 current AT (from the global offset table) and the
9948 register into the register now, and pretend we were
9949 not using a base register. */
c0ebe874 9950 if (breg != op[0])
f5040a92
AO
9951 dreg = tempreg;
9952 else
9953 {
9c2799c2 9954 gas_assert (tempreg == AT);
67c0d1eb 9955 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
9956 op[0], AT, breg);
9957 dreg = op[0];
67c0d1eb 9958 add_breg_early = 1;
f5040a92
AO
9959 }
9960
f6a22291 9961 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 9962 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9963 dreg, dreg, AT);
f5040a92 9964
f5040a92
AO
9965 used_at = 1;
9966 }
9967 else
9968 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
9969
4d7206a2 9970 relax_switch ();
f5040a92
AO
9971 offset_expr.X_add_number = expr1.X_add_number;
9972
67c0d1eb
RS
9973 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9974 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
9975 if (add_breg_early)
f5040a92 9976 {
67c0d1eb 9977 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 9978 op[0], tempreg, breg);
f5040a92 9979 breg = 0;
c0ebe874 9980 tempreg = op[0];
f5040a92 9981 }
4d7206a2 9982 relax_end ();
f5040a92 9983 }
4d7206a2 9984 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 9985 {
4d7206a2 9986 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
9987 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9988 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 9989 relax_switch ();
67c0d1eb
RS
9990 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9991 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 9992 relax_end ();
f5040a92 9993 }
4d7206a2 9994 else
f5040a92 9995 {
67c0d1eb
RS
9996 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9997 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
9998 }
9999 }
0a44bf69 10000 else if (mips_big_got && !HAVE_NEWABI)
252b5132 10001 {
67c0d1eb 10002 int gpdelay;
9117d219
NC
10003 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
10004 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 10005 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
10006
10007 /* This is the large GOT case. If this is a reference to an
10008 external symbol, and there is no constant, we want
10009 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10010 addu $tempreg,$tempreg,$gp
10011 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 10012 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
10013 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
10014 addu $tempreg,$tempreg,$gp
10015 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
10016 For a local symbol, we want
10017 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10018 nop
10019 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10020
10021 If we have a small constant, and this is a reference to
10022 an external symbol, we want
10023 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10024 addu $tempreg,$tempreg,$gp
10025 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10026 nop
10027 addiu $tempreg,$tempreg,<constant>
10028 For a local symbol, we want
10029 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10030 nop
10031 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
10032
10033 If we have a large constant, and this is a reference to
10034 an external symbol, we want
10035 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10036 addu $tempreg,$tempreg,$gp
10037 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10038 lui $at,<hiconstant>
10039 addiu $at,$at,<loconstant>
10040 addu $tempreg,$tempreg,$at
10041 For a local symbol, we want
10042 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10043 lui $at,<hiconstant>
10044 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
10045 addu $tempreg,$tempreg,$at
f5040a92 10046 */
438c16b8 10047
252b5132
RH
10048 expr1.X_add_number = offset_expr.X_add_number;
10049 offset_expr.X_add_number = 0;
4d7206a2 10050 relax_start (offset_expr.X_add_symbol);
67c0d1eb 10051 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
10052 if (expr1.X_add_number == 0 && breg == 0
10053 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
10054 {
10055 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
10056 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
10057 }
df58fc94 10058 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 10059 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10060 tempreg, tempreg, mips_gp_register);
67c0d1eb 10061 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 10062 tempreg, lw_reloc_type, tempreg);
252b5132
RH
10063 if (expr1.X_add_number == 0)
10064 {
67c0d1eb 10065 if (breg != 0)
252b5132
RH
10066 {
10067 /* We're going to put in an addu instruction using
10068 tempreg, so we may as well insert the nop right
10069 now. */
269137b2 10070 load_delay_nop ();
252b5132 10071 }
252b5132
RH
10072 }
10073 else if (expr1.X_add_number >= -0x8000
10074 && expr1.X_add_number < 0x8000)
10075 {
269137b2 10076 load_delay_nop ();
67c0d1eb 10077 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 10078 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
10079 }
10080 else
10081 {
c0ebe874
RS
10082 unsigned int dreg;
10083
252b5132
RH
10084 /* If we are going to add in a base register, and the
10085 target register and the base register are the same,
10086 then we are using AT as a temporary register. Since
10087 we want to load the constant into AT, we add our
10088 current AT (from the global offset table) and the
10089 register into the register now, and pretend we were
10090 not using a base register. */
c0ebe874 10091 if (breg != op[0])
67c0d1eb 10092 dreg = tempreg;
252b5132
RH
10093 else
10094 {
9c2799c2 10095 gas_assert (tempreg == AT);
269137b2 10096 load_delay_nop ();
67c0d1eb 10097 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
10098 op[0], AT, breg);
10099 dreg = op[0];
252b5132
RH
10100 }
10101
f6a22291 10102 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 10103 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 10104
252b5132
RH
10105 used_at = 1;
10106 }
43c0598f 10107 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
4d7206a2 10108 relax_switch ();
252b5132 10109
67c0d1eb 10110 if (gpdelay)
252b5132
RH
10111 {
10112 /* This is needed because this instruction uses $gp, but
f5040a92 10113 the first instruction on the main stream does not. */
67c0d1eb 10114 macro_build (NULL, "nop", "");
252b5132 10115 }
ed6fb7bd 10116
67c0d1eb
RS
10117 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10118 local_reloc_type, mips_gp_register);
f5040a92 10119 if (expr1.X_add_number >= -0x8000
252b5132
RH
10120 && expr1.X_add_number < 0x8000)
10121 {
269137b2 10122 load_delay_nop ();
67c0d1eb
RS
10123 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10124 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 10125 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
10126 register, the external symbol case ended with a load,
10127 so if the symbol turns out to not be external, and
10128 the next instruction uses tempreg, an unnecessary nop
10129 will be inserted. */
252b5132
RH
10130 }
10131 else
10132 {
c0ebe874 10133 if (breg == op[0])
252b5132
RH
10134 {
10135 /* We must add in the base register now, as in the
f5040a92 10136 external symbol case. */
9c2799c2 10137 gas_assert (tempreg == AT);
269137b2 10138 load_delay_nop ();
67c0d1eb 10139 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
10140 op[0], AT, breg);
10141 tempreg = op[0];
252b5132 10142 /* We set breg to 0 because we have arranged to add
f5040a92 10143 it in in both cases. */
252b5132
RH
10144 breg = 0;
10145 }
10146
67c0d1eb
RS
10147 macro_build_lui (&expr1, AT);
10148 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 10149 AT, AT, BFD_RELOC_LO16);
67c0d1eb 10150 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10151 tempreg, tempreg, AT);
8fc2e39e 10152 used_at = 1;
252b5132 10153 }
4d7206a2 10154 relax_end ();
252b5132 10155 }
0a44bf69 10156 else if (mips_big_got && HAVE_NEWABI)
f5040a92 10157 {
f5040a92
AO
10158 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
10159 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 10160 int add_breg_early = 0;
f5040a92
AO
10161
10162 /* This is the large GOT case. If this is a reference to an
10163 external symbol, and there is no constant, we want
10164 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10165 add $tempreg,$tempreg,$gp
10166 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 10167 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
10168 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
10169 add $tempreg,$tempreg,$gp
10170 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
10171
10172 If we have a small constant, and this is a reference to
10173 an external symbol, we want
10174 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10175 add $tempreg,$tempreg,$gp
10176 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10177 addi $tempreg,$tempreg,<constant>
10178
10179 If we have a large constant, and this is a reference to
10180 an external symbol, we want
10181 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10182 addu $tempreg,$tempreg,$gp
10183 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10184 lui $at,<hiconstant>
10185 addi $at,$at,<loconstant>
10186 add $tempreg,$tempreg,$at
10187
10188 If we have NewABI, and we know it's a local symbol, we want
10189 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
10190 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
10191 otherwise we have to resort to GOT_HI16/GOT_LO16. */
10192
4d7206a2 10193 relax_start (offset_expr.X_add_symbol);
f5040a92 10194
4d7206a2 10195 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
10196 offset_expr.X_add_number = 0;
10197
1abe91b1
MR
10198 if (expr1.X_add_number == 0 && breg == 0
10199 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
10200 {
10201 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
10202 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
10203 }
df58fc94 10204 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 10205 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10206 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
10207 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10208 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
10209
10210 if (expr1.X_add_number == 0)
4d7206a2 10211 ;
f5040a92
AO
10212 else if (expr1.X_add_number >= -0x8000
10213 && expr1.X_add_number < 0x8000)
10214 {
67c0d1eb 10215 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 10216 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 10217 }
ecd13cd3 10218 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 10219 {
c0ebe874
RS
10220 unsigned int dreg;
10221
f5040a92
AO
10222 /* If we are going to add in a base register, and the
10223 target register and the base register are the same,
10224 then we are using AT as a temporary register. Since
10225 we want to load the constant into AT, we add our
10226 current AT (from the global offset table) and the
10227 register into the register now, and pretend we were
10228 not using a base register. */
c0ebe874 10229 if (breg != op[0])
f5040a92
AO
10230 dreg = tempreg;
10231 else
10232 {
9c2799c2 10233 gas_assert (tempreg == AT);
67c0d1eb 10234 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
10235 op[0], AT, breg);
10236 dreg = op[0];
67c0d1eb 10237 add_breg_early = 1;
f5040a92
AO
10238 }
10239
f6a22291 10240 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 10241 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 10242
f5040a92
AO
10243 used_at = 1;
10244 }
10245 else
10246 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
10247
4d7206a2 10248 relax_switch ();
f5040a92 10249 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
10250 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10251 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
10252 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
10253 tempreg, BFD_RELOC_MIPS_GOT_OFST);
10254 if (add_breg_early)
f5040a92 10255 {
67c0d1eb 10256 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 10257 op[0], tempreg, breg);
f5040a92 10258 breg = 0;
c0ebe874 10259 tempreg = op[0];
f5040a92 10260 }
4d7206a2 10261 relax_end ();
f5040a92 10262 }
252b5132
RH
10263 else
10264 abort ();
10265
10266 if (breg != 0)
c0ebe874 10267 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", op[0], tempreg, breg);
252b5132
RH
10268 break;
10269
52b6b6b9 10270 case M_MSGSND:
df58fc94 10271 gas_assert (!mips_opts.micromips);
c0ebe874 10272 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x01);
c7af4273 10273 break;
52b6b6b9
JM
10274
10275 case M_MSGLD:
df58fc94 10276 gas_assert (!mips_opts.micromips);
c8276761 10277 macro_build (NULL, "c2", "C", 0x02);
c7af4273 10278 break;
52b6b6b9
JM
10279
10280 case M_MSGLD_T:
df58fc94 10281 gas_assert (!mips_opts.micromips);
c0ebe874 10282 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x02);
c7af4273 10283 break;
52b6b6b9
JM
10284
10285 case M_MSGWAIT:
df58fc94 10286 gas_assert (!mips_opts.micromips);
52b6b6b9 10287 macro_build (NULL, "c2", "C", 3);
c7af4273 10288 break;
52b6b6b9
JM
10289
10290 case M_MSGWAIT_T:
df58fc94 10291 gas_assert (!mips_opts.micromips);
c0ebe874 10292 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x03);
c7af4273 10293 break;
52b6b6b9 10294
252b5132
RH
10295 case M_J_A:
10296 /* The j instruction may not be used in PIC code, since it
10297 requires an absolute address. We convert it to a b
10298 instruction. */
10299 if (mips_pic == NO_PIC)
67c0d1eb 10300 macro_build (&offset_expr, "j", "a");
252b5132 10301 else
67c0d1eb 10302 macro_build (&offset_expr, "b", "p");
8fc2e39e 10303 break;
252b5132
RH
10304
10305 /* The jal instructions must be handled as macros because when
10306 generating PIC code they expand to multi-instruction
10307 sequences. Normally they are simple instructions. */
df58fc94 10308 case M_JALS_1:
c0ebe874
RS
10309 op[1] = op[0];
10310 op[0] = RA;
df58fc94
RS
10311 /* Fall through. */
10312 case M_JALS_2:
10313 gas_assert (mips_opts.micromips);
833794fc
MR
10314 if (mips_opts.insn32)
10315 {
10316 as_bad (_("Opcode not supported in the `insn32' mode `%s'"), str);
10317 break;
10318 }
df58fc94
RS
10319 jals = 1;
10320 goto jal;
252b5132 10321 case M_JAL_1:
c0ebe874
RS
10322 op[1] = op[0];
10323 op[0] = RA;
252b5132
RH
10324 /* Fall through. */
10325 case M_JAL_2:
df58fc94 10326 jal:
3e722fb5 10327 if (mips_pic == NO_PIC)
df58fc94
RS
10328 {
10329 s = jals ? "jalrs" : "jalr";
e64af278 10330 if (mips_opts.micromips
833794fc 10331 && !mips_opts.insn32
c0ebe874 10332 && op[0] == RA
e64af278 10333 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 10334 macro_build (NULL, s, "mj", op[1]);
df58fc94 10335 else
c0ebe874 10336 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
df58fc94 10337 }
0a44bf69 10338 else
252b5132 10339 {
df58fc94
RS
10340 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
10341 && mips_cprestore_offset >= 0);
10342
c0ebe874 10343 if (op[1] != PIC_CALL_REG)
252b5132 10344 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 10345
833794fc
MR
10346 s = ((mips_opts.micromips
10347 && !mips_opts.insn32
10348 && (!mips_opts.noreorder || cprestore))
df58fc94 10349 ? "jalrs" : "jalr");
e64af278 10350 if (mips_opts.micromips
833794fc 10351 && !mips_opts.insn32
c0ebe874 10352 && op[0] == RA
e64af278 10353 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 10354 macro_build (NULL, s, "mj", op[1]);
df58fc94 10355 else
c0ebe874 10356 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
0a44bf69 10357 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
252b5132 10358 {
6478892d
TS
10359 if (mips_cprestore_offset < 0)
10360 as_warn (_("No .cprestore pseudo-op used in PIC code"));
10361 else
10362 {
90ecf173 10363 if (!mips_frame_reg_valid)
7a621144
DJ
10364 {
10365 as_warn (_("No .frame pseudo-op used in PIC code"));
10366 /* Quiet this warning. */
10367 mips_frame_reg_valid = 1;
10368 }
90ecf173 10369 if (!mips_cprestore_valid)
7a621144
DJ
10370 {
10371 as_warn (_("No .cprestore pseudo-op used in PIC code"));
10372 /* Quiet this warning. */
10373 mips_cprestore_valid = 1;
10374 }
d3fca0b5
MR
10375 if (mips_opts.noreorder)
10376 macro_build (NULL, "nop", "");
6478892d 10377 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 10378 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 10379 mips_gp_register,
256ab948
TS
10380 mips_frame_reg,
10381 HAVE_64BIT_ADDRESSES);
6478892d 10382 }
252b5132
RH
10383 }
10384 }
252b5132 10385
8fc2e39e 10386 break;
252b5132 10387
df58fc94
RS
10388 case M_JALS_A:
10389 gas_assert (mips_opts.micromips);
833794fc
MR
10390 if (mips_opts.insn32)
10391 {
10392 as_bad (_("Opcode not supported in the `insn32' mode `%s'"), str);
10393 break;
10394 }
df58fc94
RS
10395 jals = 1;
10396 /* Fall through. */
252b5132
RH
10397 case M_JAL_A:
10398 if (mips_pic == NO_PIC)
df58fc94 10399 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
252b5132
RH
10400 else if (mips_pic == SVR4_PIC)
10401 {
10402 /* If this is a reference to an external symbol, and we are
10403 using a small GOT, we want
10404 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
10405 nop
f9419b05 10406 jalr $ra,$25
252b5132
RH
10407 nop
10408 lw $gp,cprestore($sp)
10409 The cprestore value is set using the .cprestore
10410 pseudo-op. If we are using a big GOT, we want
10411 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
10412 addu $25,$25,$gp
10413 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
10414 nop
f9419b05 10415 jalr $ra,$25
252b5132
RH
10416 nop
10417 lw $gp,cprestore($sp)
10418 If the symbol is not external, we want
10419 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10420 nop
10421 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 10422 jalr $ra,$25
252b5132 10423 nop
438c16b8 10424 lw $gp,cprestore($sp)
f5040a92
AO
10425
10426 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
10427 sequences above, minus nops, unless the symbol is local,
10428 which enables us to use GOT_PAGE/GOT_OFST (big got) or
10429 GOT_DISP. */
438c16b8 10430 if (HAVE_NEWABI)
252b5132 10431 {
90ecf173 10432 if (!mips_big_got)
f5040a92 10433 {
4d7206a2 10434 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
10435 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10436 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 10437 mips_gp_register);
4d7206a2 10438 relax_switch ();
67c0d1eb
RS
10439 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10440 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
10441 mips_gp_register);
10442 relax_end ();
f5040a92
AO
10443 }
10444 else
10445 {
4d7206a2 10446 relax_start (offset_expr.X_add_symbol);
df58fc94 10447 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
10448 BFD_RELOC_MIPS_CALL_HI16);
10449 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
10450 PIC_CALL_REG, mips_gp_register);
10451 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10452 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
10453 PIC_CALL_REG);
4d7206a2 10454 relax_switch ();
67c0d1eb
RS
10455 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10456 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
10457 mips_gp_register);
10458 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10459 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 10460 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 10461 relax_end ();
f5040a92 10462 }
684022ea 10463
df58fc94 10464 macro_build_jalr (&offset_expr, 0);
252b5132
RH
10465 }
10466 else
10467 {
4d7206a2 10468 relax_start (offset_expr.X_add_symbol);
90ecf173 10469 if (!mips_big_got)
438c16b8 10470 {
67c0d1eb
RS
10471 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10472 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 10473 mips_gp_register);
269137b2 10474 load_delay_nop ();
4d7206a2 10475 relax_switch ();
438c16b8 10476 }
252b5132 10477 else
252b5132 10478 {
67c0d1eb
RS
10479 int gpdelay;
10480
10481 gpdelay = reg_needs_delay (mips_gp_register);
df58fc94 10482 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
10483 BFD_RELOC_MIPS_CALL_HI16);
10484 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
10485 PIC_CALL_REG, mips_gp_register);
10486 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10487 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
10488 PIC_CALL_REG);
269137b2 10489 load_delay_nop ();
4d7206a2 10490 relax_switch ();
67c0d1eb
RS
10491 if (gpdelay)
10492 macro_build (NULL, "nop", "");
252b5132 10493 }
67c0d1eb
RS
10494 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10495 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 10496 mips_gp_register);
269137b2 10497 load_delay_nop ();
67c0d1eb
RS
10498 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10499 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 10500 relax_end ();
df58fc94 10501 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
438c16b8 10502
6478892d
TS
10503 if (mips_cprestore_offset < 0)
10504 as_warn (_("No .cprestore pseudo-op used in PIC code"));
10505 else
10506 {
90ecf173 10507 if (!mips_frame_reg_valid)
7a621144
DJ
10508 {
10509 as_warn (_("No .frame pseudo-op used in PIC code"));
10510 /* Quiet this warning. */
10511 mips_frame_reg_valid = 1;
10512 }
90ecf173 10513 if (!mips_cprestore_valid)
7a621144
DJ
10514 {
10515 as_warn (_("No .cprestore pseudo-op used in PIC code"));
10516 /* Quiet this warning. */
10517 mips_cprestore_valid = 1;
10518 }
6478892d 10519 if (mips_opts.noreorder)
67c0d1eb 10520 macro_build (NULL, "nop", "");
6478892d 10521 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 10522 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 10523 mips_gp_register,
256ab948
TS
10524 mips_frame_reg,
10525 HAVE_64BIT_ADDRESSES);
6478892d 10526 }
252b5132
RH
10527 }
10528 }
0a44bf69
RS
10529 else if (mips_pic == VXWORKS_PIC)
10530 as_bad (_("Non-PIC jump used in PIC library"));
252b5132
RH
10531 else
10532 abort ();
10533
8fc2e39e 10534 break;
252b5132 10535
7f3c4072 10536 case M_LBUE_AB:
7f3c4072
CM
10537 s = "lbue";
10538 fmt = "t,+j(b)";
10539 offbits = 9;
10540 goto ld_st;
10541 case M_LHUE_AB:
7f3c4072
CM
10542 s = "lhue";
10543 fmt = "t,+j(b)";
10544 offbits = 9;
10545 goto ld_st;
10546 case M_LBE_AB:
7f3c4072
CM
10547 s = "lbe";
10548 fmt = "t,+j(b)";
10549 offbits = 9;
10550 goto ld_st;
10551 case M_LHE_AB:
7f3c4072
CM
10552 s = "lhe";
10553 fmt = "t,+j(b)";
10554 offbits = 9;
10555 goto ld_st;
10556 case M_LLE_AB:
7f3c4072
CM
10557 s = "lle";
10558 fmt = "t,+j(b)";
10559 offbits = 9;
10560 goto ld_st;
10561 case M_LWE_AB:
7f3c4072
CM
10562 s = "lwe";
10563 fmt = "t,+j(b)";
10564 offbits = 9;
10565 goto ld_st;
10566 case M_LWLE_AB:
7f3c4072
CM
10567 s = "lwle";
10568 fmt = "t,+j(b)";
10569 offbits = 9;
10570 goto ld_st;
10571 case M_LWRE_AB:
7f3c4072
CM
10572 s = "lwre";
10573 fmt = "t,+j(b)";
10574 offbits = 9;
10575 goto ld_st;
10576 case M_SBE_AB:
7f3c4072
CM
10577 s = "sbe";
10578 fmt = "t,+j(b)";
10579 offbits = 9;
10580 goto ld_st;
10581 case M_SCE_AB:
7f3c4072
CM
10582 s = "sce";
10583 fmt = "t,+j(b)";
10584 offbits = 9;
10585 goto ld_st;
10586 case M_SHE_AB:
7f3c4072
CM
10587 s = "she";
10588 fmt = "t,+j(b)";
10589 offbits = 9;
10590 goto ld_st;
10591 case M_SWE_AB:
7f3c4072
CM
10592 s = "swe";
10593 fmt = "t,+j(b)";
10594 offbits = 9;
10595 goto ld_st;
10596 case M_SWLE_AB:
7f3c4072
CM
10597 s = "swle";
10598 fmt = "t,+j(b)";
10599 offbits = 9;
10600 goto ld_st;
10601 case M_SWRE_AB:
7f3c4072
CM
10602 s = "swre";
10603 fmt = "t,+j(b)";
10604 offbits = 9;
10605 goto ld_st;
dec0624d 10606 case M_ACLR_AB:
dec0624d 10607 s = "aclr";
dec0624d 10608 fmt = "\\,~(b)";
7f3c4072 10609 offbits = 12;
dec0624d
MR
10610 goto ld_st;
10611 case M_ASET_AB:
dec0624d 10612 s = "aset";
dec0624d 10613 fmt = "\\,~(b)";
7f3c4072 10614 offbits = 12;
dec0624d 10615 goto ld_st;
252b5132
RH
10616 case M_LB_AB:
10617 s = "lb";
df58fc94 10618 fmt = "t,o(b)";
252b5132
RH
10619 goto ld;
10620 case M_LBU_AB:
10621 s = "lbu";
df58fc94 10622 fmt = "t,o(b)";
252b5132
RH
10623 goto ld;
10624 case M_LH_AB:
10625 s = "lh";
df58fc94 10626 fmt = "t,o(b)";
252b5132
RH
10627 goto ld;
10628 case M_LHU_AB:
10629 s = "lhu";
df58fc94 10630 fmt = "t,o(b)";
252b5132
RH
10631 goto ld;
10632 case M_LW_AB:
10633 s = "lw";
df58fc94 10634 fmt = "t,o(b)";
252b5132
RH
10635 goto ld;
10636 case M_LWC0_AB:
df58fc94 10637 gas_assert (!mips_opts.micromips);
252b5132 10638 s = "lwc0";
df58fc94 10639 fmt = "E,o(b)";
bdaaa2e1 10640 /* Itbl support may require additional care here. */
252b5132 10641 coproc = 1;
df58fc94 10642 goto ld_st;
252b5132
RH
10643 case M_LWC1_AB:
10644 s = "lwc1";
df58fc94 10645 fmt = "T,o(b)";
bdaaa2e1 10646 /* Itbl support may require additional care here. */
252b5132 10647 coproc = 1;
df58fc94 10648 goto ld_st;
252b5132
RH
10649 case M_LWC2_AB:
10650 s = "lwc2";
df58fc94 10651 fmt = COP12_FMT;
7f3c4072 10652 offbits = (mips_opts.micromips ? 12 : 16);
bdaaa2e1 10653 /* Itbl support may require additional care here. */
252b5132 10654 coproc = 1;
df58fc94 10655 goto ld_st;
252b5132 10656 case M_LWC3_AB:
df58fc94 10657 gas_assert (!mips_opts.micromips);
252b5132 10658 s = "lwc3";
df58fc94 10659 fmt = "E,o(b)";
bdaaa2e1 10660 /* Itbl support may require additional care here. */
252b5132 10661 coproc = 1;
df58fc94 10662 goto ld_st;
252b5132
RH
10663 case M_LWL_AB:
10664 s = "lwl";
df58fc94 10665 fmt = MEM12_FMT;
7f3c4072 10666 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10667 goto ld_st;
252b5132
RH
10668 case M_LWR_AB:
10669 s = "lwr";
df58fc94 10670 fmt = MEM12_FMT;
7f3c4072 10671 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10672 goto ld_st;
252b5132 10673 case M_LDC1_AB:
252b5132 10674 s = "ldc1";
df58fc94 10675 fmt = "T,o(b)";
bdaaa2e1 10676 /* Itbl support may require additional care here. */
252b5132 10677 coproc = 1;
df58fc94 10678 goto ld_st;
252b5132
RH
10679 case M_LDC2_AB:
10680 s = "ldc2";
df58fc94 10681 fmt = COP12_FMT;
7f3c4072 10682 offbits = (mips_opts.micromips ? 12 : 16);
bdaaa2e1 10683 /* Itbl support may require additional care here. */
252b5132 10684 coproc = 1;
df58fc94 10685 goto ld_st;
c77c0862 10686 case M_LQC2_AB:
c77c0862 10687 s = "lqc2";
14daeee3 10688 fmt = "+7,o(b)";
c77c0862
RS
10689 /* Itbl support may require additional care here. */
10690 coproc = 1;
10691 goto ld_st;
252b5132
RH
10692 case M_LDC3_AB:
10693 s = "ldc3";
df58fc94 10694 fmt = "E,o(b)";
bdaaa2e1 10695 /* Itbl support may require additional care here. */
252b5132 10696 coproc = 1;
df58fc94 10697 goto ld_st;
252b5132
RH
10698 case M_LDL_AB:
10699 s = "ldl";
df58fc94 10700 fmt = MEM12_FMT;
7f3c4072 10701 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10702 goto ld_st;
252b5132
RH
10703 case M_LDR_AB:
10704 s = "ldr";
df58fc94 10705 fmt = MEM12_FMT;
7f3c4072 10706 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10707 goto ld_st;
252b5132
RH
10708 case M_LL_AB:
10709 s = "ll";
df58fc94 10710 fmt = MEM12_FMT;
7f3c4072 10711 offbits = (mips_opts.micromips ? 12 : 16);
252b5132
RH
10712 goto ld;
10713 case M_LLD_AB:
10714 s = "lld";
df58fc94 10715 fmt = MEM12_FMT;
7f3c4072 10716 offbits = (mips_opts.micromips ? 12 : 16);
252b5132
RH
10717 goto ld;
10718 case M_LWU_AB:
10719 s = "lwu";
df58fc94 10720 fmt = MEM12_FMT;
7f3c4072 10721 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
10722 goto ld;
10723 case M_LWP_AB:
df58fc94
RS
10724 gas_assert (mips_opts.micromips);
10725 s = "lwp";
10726 fmt = "t,~(b)";
7f3c4072 10727 offbits = 12;
df58fc94
RS
10728 lp = 1;
10729 goto ld;
10730 case M_LDP_AB:
df58fc94
RS
10731 gas_assert (mips_opts.micromips);
10732 s = "ldp";
10733 fmt = "t,~(b)";
7f3c4072 10734 offbits = 12;
df58fc94
RS
10735 lp = 1;
10736 goto ld;
10737 case M_LWM_AB:
df58fc94
RS
10738 gas_assert (mips_opts.micromips);
10739 s = "lwm";
10740 fmt = "n,~(b)";
7f3c4072 10741 offbits = 12;
df58fc94
RS
10742 goto ld_st;
10743 case M_LDM_AB:
df58fc94
RS
10744 gas_assert (mips_opts.micromips);
10745 s = "ldm";
10746 fmt = "n,~(b)";
7f3c4072 10747 offbits = 12;
df58fc94
RS
10748 goto ld_st;
10749
252b5132 10750 ld:
f19ccbda 10751 /* We don't want to use $0 as tempreg. */
c0ebe874 10752 if (op[2] == op[0] + lp || op[0] + lp == ZERO)
df58fc94 10753 goto ld_st;
252b5132 10754 else
c0ebe874 10755 tempreg = op[0] + lp;
df58fc94
RS
10756 goto ld_noat;
10757
252b5132
RH
10758 case M_SB_AB:
10759 s = "sb";
df58fc94
RS
10760 fmt = "t,o(b)";
10761 goto ld_st;
252b5132
RH
10762 case M_SH_AB:
10763 s = "sh";
df58fc94
RS
10764 fmt = "t,o(b)";
10765 goto ld_st;
252b5132
RH
10766 case M_SW_AB:
10767 s = "sw";
df58fc94
RS
10768 fmt = "t,o(b)";
10769 goto ld_st;
252b5132 10770 case M_SWC0_AB:
df58fc94 10771 gas_assert (!mips_opts.micromips);
252b5132 10772 s = "swc0";
df58fc94 10773 fmt = "E,o(b)";
bdaaa2e1 10774 /* Itbl support may require additional care here. */
252b5132 10775 coproc = 1;
df58fc94 10776 goto ld_st;
252b5132
RH
10777 case M_SWC1_AB:
10778 s = "swc1";
df58fc94 10779 fmt = "T,o(b)";
bdaaa2e1 10780 /* Itbl support may require additional care here. */
252b5132 10781 coproc = 1;
df58fc94 10782 goto ld_st;
252b5132
RH
10783 case M_SWC2_AB:
10784 s = "swc2";
df58fc94 10785 fmt = COP12_FMT;
7f3c4072 10786 offbits = (mips_opts.micromips ? 12 : 16);
bdaaa2e1 10787 /* Itbl support may require additional care here. */
252b5132 10788 coproc = 1;
df58fc94 10789 goto ld_st;
252b5132 10790 case M_SWC3_AB:
df58fc94 10791 gas_assert (!mips_opts.micromips);
252b5132 10792 s = "swc3";
df58fc94 10793 fmt = "E,o(b)";
bdaaa2e1 10794 /* Itbl support may require additional care here. */
252b5132 10795 coproc = 1;
df58fc94 10796 goto ld_st;
252b5132
RH
10797 case M_SWL_AB:
10798 s = "swl";
df58fc94 10799 fmt = MEM12_FMT;
7f3c4072 10800 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10801 goto ld_st;
252b5132
RH
10802 case M_SWR_AB:
10803 s = "swr";
df58fc94 10804 fmt = MEM12_FMT;
7f3c4072 10805 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10806 goto ld_st;
252b5132
RH
10807 case M_SC_AB:
10808 s = "sc";
df58fc94 10809 fmt = MEM12_FMT;
7f3c4072 10810 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10811 goto ld_st;
252b5132
RH
10812 case M_SCD_AB:
10813 s = "scd";
df58fc94 10814 fmt = MEM12_FMT;
7f3c4072 10815 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10816 goto ld_st;
d43b4baf
TS
10817 case M_CACHE_AB:
10818 s = "cache";
df58fc94 10819 fmt = mips_opts.micromips ? "k,~(b)" : "k,o(b)";
7f3c4072
CM
10820 offbits = (mips_opts.micromips ? 12 : 16);
10821 goto ld_st;
10822 case M_CACHEE_AB:
7f3c4072
CM
10823 s = "cachee";
10824 fmt = "k,+j(b)";
10825 offbits = 9;
df58fc94 10826 goto ld_st;
3eebd5eb
MR
10827 case M_PREF_AB:
10828 s = "pref";
df58fc94 10829 fmt = !mips_opts.micromips ? "k,o(b)" : "k,~(b)";
7f3c4072
CM
10830 offbits = (mips_opts.micromips ? 12 : 16);
10831 goto ld_st;
10832 case M_PREFE_AB:
7f3c4072
CM
10833 s = "prefe";
10834 fmt = "k,+j(b)";
10835 offbits = 9;
df58fc94 10836 goto ld_st;
252b5132 10837 case M_SDC1_AB:
252b5132 10838 s = "sdc1";
df58fc94 10839 fmt = "T,o(b)";
252b5132 10840 coproc = 1;
bdaaa2e1 10841 /* Itbl support may require additional care here. */
df58fc94 10842 goto ld_st;
252b5132
RH
10843 case M_SDC2_AB:
10844 s = "sdc2";
df58fc94 10845 fmt = COP12_FMT;
7f3c4072 10846 offbits = (mips_opts.micromips ? 12 : 16);
c77c0862
RS
10847 /* Itbl support may require additional care here. */
10848 coproc = 1;
10849 goto ld_st;
10850 case M_SQC2_AB:
c77c0862 10851 s = "sqc2";
14daeee3 10852 fmt = "+7,o(b)";
bdaaa2e1 10853 /* Itbl support may require additional care here. */
252b5132 10854 coproc = 1;
df58fc94 10855 goto ld_st;
252b5132 10856 case M_SDC3_AB:
df58fc94 10857 gas_assert (!mips_opts.micromips);
252b5132 10858 s = "sdc3";
df58fc94 10859 fmt = "E,o(b)";
bdaaa2e1 10860 /* Itbl support may require additional care here. */
252b5132 10861 coproc = 1;
df58fc94 10862 goto ld_st;
252b5132
RH
10863 case M_SDL_AB:
10864 s = "sdl";
df58fc94 10865 fmt = MEM12_FMT;
7f3c4072 10866 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10867 goto ld_st;
252b5132
RH
10868 case M_SDR_AB:
10869 s = "sdr";
df58fc94 10870 fmt = MEM12_FMT;
7f3c4072 10871 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
10872 goto ld_st;
10873 case M_SWP_AB:
df58fc94
RS
10874 gas_assert (mips_opts.micromips);
10875 s = "swp";
10876 fmt = "t,~(b)";
7f3c4072 10877 offbits = 12;
df58fc94
RS
10878 goto ld_st;
10879 case M_SDP_AB:
df58fc94
RS
10880 gas_assert (mips_opts.micromips);
10881 s = "sdp";
10882 fmt = "t,~(b)";
7f3c4072 10883 offbits = 12;
df58fc94
RS
10884 goto ld_st;
10885 case M_SWM_AB:
df58fc94
RS
10886 gas_assert (mips_opts.micromips);
10887 s = "swm";
10888 fmt = "n,~(b)";
7f3c4072 10889 offbits = 12;
df58fc94
RS
10890 goto ld_st;
10891 case M_SDM_AB:
df58fc94
RS
10892 gas_assert (mips_opts.micromips);
10893 s = "sdm";
10894 fmt = "n,~(b)";
7f3c4072 10895 offbits = 12;
df58fc94
RS
10896
10897 ld_st:
8fc2e39e 10898 tempreg = AT;
df58fc94 10899 ld_noat:
c0ebe874 10900 breg = op[2];
f2ae14a1
RS
10901 if (small_offset_p (0, align, 16))
10902 {
10903 /* The first case exists for M_LD_AB and M_SD_AB, which are
10904 macros for o32 but which should act like normal instructions
10905 otherwise. */
10906 if (offbits == 16)
c0ebe874 10907 macro_build (&offset_expr, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
10908 offset_reloc[1], offset_reloc[2], breg);
10909 else if (small_offset_p (0, align, offbits))
10910 {
10911 if (offbits == 0)
c0ebe874 10912 macro_build (NULL, s, fmt, op[0], breg);
f2ae14a1 10913 else
c0ebe874 10914 macro_build (NULL, s, fmt, op[0],
c8276761 10915 (int) offset_expr.X_add_number, breg);
f2ae14a1
RS
10916 }
10917 else
10918 {
10919 if (tempreg == AT)
10920 used_at = 1;
10921 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10922 tempreg, breg, -1, offset_reloc[0],
10923 offset_reloc[1], offset_reloc[2]);
10924 if (offbits == 0)
c0ebe874 10925 macro_build (NULL, s, fmt, op[0], tempreg);
f2ae14a1 10926 else
c0ebe874 10927 macro_build (NULL, s, fmt, op[0], 0, tempreg);
f2ae14a1
RS
10928 }
10929 break;
10930 }
10931
10932 if (tempreg == AT)
10933 used_at = 1;
10934
252b5132
RH
10935 if (offset_expr.X_op != O_constant
10936 && offset_expr.X_op != O_symbol)
10937 {
f71d0d44 10938 as_bad (_("Expression too complex"));
252b5132
RH
10939 offset_expr.X_op = O_constant;
10940 }
10941
2051e8c4
MR
10942 if (HAVE_32BIT_ADDRESSES
10943 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
10944 {
10945 char value [32];
10946
10947 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 10948 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 10949 }
2051e8c4 10950
252b5132
RH
10951 /* A constant expression in PIC code can be handled just as it
10952 is in non PIC code. */
aed1a261
RS
10953 if (offset_expr.X_op == O_constant)
10954 {
f2ae14a1
RS
10955 expr1.X_add_number = offset_high_part (offset_expr.X_add_number,
10956 offbits == 0 ? 16 : offbits);
10957 offset_expr.X_add_number -= expr1.X_add_number;
df58fc94 10958
f2ae14a1
RS
10959 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
10960 if (breg != 0)
10961 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10962 tempreg, tempreg, breg);
7f3c4072 10963 if (offbits == 0)
dd6a37e7 10964 {
f2ae14a1 10965 if (offset_expr.X_add_number != 0)
dd6a37e7 10966 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
f2ae14a1 10967 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
c0ebe874 10968 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 10969 }
7f3c4072 10970 else if (offbits == 16)
c0ebe874 10971 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
df58fc94 10972 else
c0ebe874 10973 macro_build (NULL, s, fmt, op[0],
c8276761 10974 (int) offset_expr.X_add_number, tempreg);
df58fc94 10975 }
7f3c4072 10976 else if (offbits != 16)
df58fc94 10977 {
7f3c4072
CM
10978 /* The offset field is too narrow to be used for a low-part
10979 relocation, so load the whole address into the auxillary
f2ae14a1
RS
10980 register. */
10981 load_address (tempreg, &offset_expr, &used_at);
10982 if (breg != 0)
10983 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10984 tempreg, tempreg, breg);
7f3c4072 10985 if (offbits == 0)
c0ebe874 10986 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 10987 else
c0ebe874 10988 macro_build (NULL, s, fmt, op[0], 0, tempreg);
aed1a261
RS
10989 }
10990 else if (mips_pic == NO_PIC)
252b5132
RH
10991 {
10992 /* If this is a reference to a GP relative symbol, and there
10993 is no base register, we want
c0ebe874 10994 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
10995 Otherwise, if there is no base register, we want
10996 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
c0ebe874 10997 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
252b5132
RH
10998 If we have a constant, we need two instructions anyhow,
10999 so we always use the latter form.
11000
11001 If we have a base register, and this is a reference to a
11002 GP relative symbol, we want
11003 addu $tempreg,$breg,$gp
c0ebe874 11004 <op> op[0],<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
11005 Otherwise we want
11006 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
11007 addu $tempreg,$tempreg,$breg
c0ebe874 11008 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 11009 With a constant we always use the latter case.
76b3015f 11010
d6bc6245
TS
11011 With 64bit address space and no base register and $at usable,
11012 we want
11013 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11014 lui $at,<sym> (BFD_RELOC_HI16_S)
11015 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11016 dsll32 $tempreg,0
11017 daddu $tempreg,$at
c0ebe874 11018 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
11019 If we have a base register, we want
11020 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11021 lui $at,<sym> (BFD_RELOC_HI16_S)
11022 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11023 daddu $at,$breg
11024 dsll32 $tempreg,0
11025 daddu $tempreg,$at
c0ebe874 11026 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
11027
11028 Without $at we can't generate the optimal path for superscalar
11029 processors here since this would require two temporary registers.
11030 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11031 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11032 dsll $tempreg,16
11033 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
11034 dsll $tempreg,16
c0ebe874 11035 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
11036 If we have a base register, we want
11037 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11038 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11039 dsll $tempreg,16
11040 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
11041 dsll $tempreg,16
11042 daddu $tempreg,$tempreg,$breg
c0ebe874 11043 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 11044
6caf9ef4 11045 For GP relative symbols in 64bit address space we can use
aed1a261
RS
11046 the same sequence as in 32bit address space. */
11047 if (HAVE_64BIT_SYMBOLS)
d6bc6245 11048 {
aed1a261 11049 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
11050 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
11051 {
11052 relax_start (offset_expr.X_add_symbol);
11053 if (breg == 0)
11054 {
c0ebe874 11055 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
11056 BFD_RELOC_GPREL16, mips_gp_register);
11057 }
11058 else
11059 {
11060 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11061 tempreg, breg, mips_gp_register);
c0ebe874 11062 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
11063 BFD_RELOC_GPREL16, tempreg);
11064 }
11065 relax_switch ();
11066 }
d6bc6245 11067
741fe287 11068 if (used_at == 0 && mips_opts.at)
d6bc6245 11069 {
df58fc94 11070 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb 11071 BFD_RELOC_MIPS_HIGHEST);
df58fc94 11072 macro_build (&offset_expr, "lui", LUI_FMT, AT,
67c0d1eb
RS
11073 BFD_RELOC_HI16_S);
11074 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
11075 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 11076 if (breg != 0)
67c0d1eb 11077 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
df58fc94 11078 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 11079 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
c0ebe874 11080 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16,
67c0d1eb 11081 tempreg);
d6bc6245
TS
11082 used_at = 1;
11083 }
11084 else
11085 {
df58fc94 11086 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb
RS
11087 BFD_RELOC_MIPS_HIGHEST);
11088 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
11089 tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 11090 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb
RS
11091 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
11092 tempreg, BFD_RELOC_HI16_S);
df58fc94 11093 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
d6bc6245 11094 if (breg != 0)
67c0d1eb 11095 macro_build (NULL, "daddu", "d,v,t",
17a2f251 11096 tempreg, tempreg, breg);
c0ebe874 11097 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 11098 BFD_RELOC_LO16, tempreg);
d6bc6245 11099 }
6caf9ef4
TS
11100
11101 if (mips_relax.sequence)
11102 relax_end ();
8fc2e39e 11103 break;
d6bc6245 11104 }
256ab948 11105
252b5132
RH
11106 if (breg == 0)
11107 {
67c0d1eb 11108 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 11109 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 11110 {
4d7206a2 11111 relax_start (offset_expr.X_add_symbol);
c0ebe874 11112 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_GPREL16,
67c0d1eb 11113 mips_gp_register);
4d7206a2 11114 relax_switch ();
252b5132 11115 }
67c0d1eb 11116 macro_build_lui (&offset_expr, tempreg);
c0ebe874 11117 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 11118 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
11119 if (mips_relax.sequence)
11120 relax_end ();
252b5132
RH
11121 }
11122 else
11123 {
67c0d1eb 11124 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 11125 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 11126 {
4d7206a2 11127 relax_start (offset_expr.X_add_symbol);
67c0d1eb 11128 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11129 tempreg, breg, mips_gp_register);
c0ebe874 11130 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 11131 BFD_RELOC_GPREL16, tempreg);
4d7206a2 11132 relax_switch ();
252b5132 11133 }
67c0d1eb
RS
11134 macro_build_lui (&offset_expr, tempreg);
11135 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11136 tempreg, tempreg, breg);
c0ebe874 11137 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 11138 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
11139 if (mips_relax.sequence)
11140 relax_end ();
252b5132
RH
11141 }
11142 }
0a44bf69 11143 else if (!mips_big_got)
252b5132 11144 {
ed6fb7bd 11145 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 11146
252b5132
RH
11147 /* If this is a reference to an external symbol, we want
11148 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11149 nop
c0ebe874 11150 <op> op[0],0($tempreg)
252b5132
RH
11151 Otherwise we want
11152 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11153 nop
11154 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 11155 <op> op[0],0($tempreg)
f5040a92
AO
11156
11157 For NewABI, we want
11158 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 11159 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 11160
252b5132
RH
11161 If there is a base register, we add it to $tempreg before
11162 the <op>. If there is a constant, we stick it in the
11163 <op> instruction. We don't handle constants larger than
11164 16 bits, because we have no way to load the upper 16 bits
11165 (actually, we could handle them for the subset of cases
11166 in which we are not using $at). */
9c2799c2 11167 gas_assert (offset_expr.X_op == O_symbol);
f5040a92
AO
11168 if (HAVE_NEWABI)
11169 {
67c0d1eb
RS
11170 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11171 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 11172 if (breg != 0)
67c0d1eb 11173 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11174 tempreg, tempreg, breg);
c0ebe874 11175 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 11176 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
11177 break;
11178 }
252b5132
RH
11179 expr1.X_add_number = offset_expr.X_add_number;
11180 offset_expr.X_add_number = 0;
11181 if (expr1.X_add_number < -0x8000
11182 || expr1.X_add_number >= 0x8000)
11183 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
11184 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11185 lw_reloc_type, mips_gp_register);
269137b2 11186 load_delay_nop ();
4d7206a2
RS
11187 relax_start (offset_expr.X_add_symbol);
11188 relax_switch ();
67c0d1eb
RS
11189 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11190 tempreg, BFD_RELOC_LO16);
4d7206a2 11191 relax_end ();
252b5132 11192 if (breg != 0)
67c0d1eb 11193 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11194 tempreg, tempreg, breg);
c0ebe874 11195 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 11196 }
0a44bf69 11197 else if (mips_big_got && !HAVE_NEWABI)
252b5132 11198 {
67c0d1eb 11199 int gpdelay;
252b5132
RH
11200
11201 /* If this is a reference to an external symbol, we want
11202 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11203 addu $tempreg,$tempreg,$gp
11204 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 11205 <op> op[0],0($tempreg)
252b5132
RH
11206 Otherwise we want
11207 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11208 nop
11209 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 11210 <op> op[0],0($tempreg)
252b5132
RH
11211 If there is a base register, we add it to $tempreg before
11212 the <op>. If there is a constant, we stick it in the
11213 <op> instruction. We don't handle constants larger than
11214 16 bits, because we have no way to load the upper 16 bits
11215 (actually, we could handle them for the subset of cases
f5040a92 11216 in which we are not using $at). */
9c2799c2 11217 gas_assert (offset_expr.X_op == O_symbol);
252b5132
RH
11218 expr1.X_add_number = offset_expr.X_add_number;
11219 offset_expr.X_add_number = 0;
11220 if (expr1.X_add_number < -0x8000
11221 || expr1.X_add_number >= 0x8000)
11222 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 11223 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 11224 relax_start (offset_expr.X_add_symbol);
df58fc94 11225 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 11226 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
11227 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
11228 mips_gp_register);
11229 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11230 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 11231 relax_switch ();
67c0d1eb
RS
11232 if (gpdelay)
11233 macro_build (NULL, "nop", "");
11234 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11235 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 11236 load_delay_nop ();
67c0d1eb
RS
11237 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11238 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
11239 relax_end ();
11240
252b5132 11241 if (breg != 0)
67c0d1eb 11242 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11243 tempreg, tempreg, breg);
c0ebe874 11244 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 11245 }
0a44bf69 11246 else if (mips_big_got && HAVE_NEWABI)
f5040a92 11247 {
f5040a92
AO
11248 /* If this is a reference to an external symbol, we want
11249 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11250 add $tempreg,$tempreg,$gp
11251 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 11252 <op> op[0],<ofst>($tempreg)
f5040a92
AO
11253 Otherwise, for local symbols, we want:
11254 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 11255 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
9c2799c2 11256 gas_assert (offset_expr.X_op == O_symbol);
4d7206a2 11257 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
11258 offset_expr.X_add_number = 0;
11259 if (expr1.X_add_number < -0x8000
11260 || expr1.X_add_number >= 0x8000)
11261 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 11262 relax_start (offset_expr.X_add_symbol);
df58fc94 11263 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 11264 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
11265 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
11266 mips_gp_register);
11267 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11268 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 11269 if (breg != 0)
67c0d1eb 11270 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11271 tempreg, tempreg, breg);
c0ebe874 11272 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
684022ea 11273
4d7206a2 11274 relax_switch ();
f5040a92 11275 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
11276 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11277 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 11278 if (breg != 0)
67c0d1eb 11279 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11280 tempreg, tempreg, breg);
c0ebe874 11281 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 11282 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 11283 relax_end ();
f5040a92 11284 }
252b5132
RH
11285 else
11286 abort ();
11287
252b5132
RH
11288 break;
11289
833794fc
MR
11290 case M_JRADDIUSP:
11291 gas_assert (mips_opts.micromips);
11292 gas_assert (mips_opts.insn32);
11293 start_noreorder ();
11294 macro_build (NULL, "jr", "s", RA);
c0ebe874 11295 expr1.X_add_number = op[0] << 2;
833794fc
MR
11296 macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
11297 end_noreorder ();
11298 break;
11299
11300 case M_JRC:
11301 gas_assert (mips_opts.micromips);
11302 gas_assert (mips_opts.insn32);
c0ebe874 11303 macro_build (NULL, "jr", "s", op[0]);
833794fc
MR
11304 if (mips_opts.noreorder)
11305 macro_build (NULL, "nop", "");
11306 break;
11307
252b5132
RH
11308 case M_LI:
11309 case M_LI_S:
c0ebe874 11310 load_register (op[0], &imm_expr, 0);
8fc2e39e 11311 break;
252b5132
RH
11312
11313 case M_DLI:
c0ebe874 11314 load_register (op[0], &imm_expr, 1);
8fc2e39e 11315 break;
252b5132
RH
11316
11317 case M_LI_SS:
11318 if (imm_expr.X_op == O_constant)
11319 {
8fc2e39e 11320 used_at = 1;
67c0d1eb 11321 load_register (AT, &imm_expr, 0);
c0ebe874 11322 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
11323 break;
11324 }
11325 else
11326 {
9c2799c2 11327 gas_assert (offset_expr.X_op == O_symbol
90ecf173
MR
11328 && strcmp (segment_name (S_GET_SEGMENT
11329 (offset_expr.X_add_symbol)),
11330 ".lit4") == 0
11331 && offset_expr.X_add_number == 0);
c0ebe874 11332 macro_build (&offset_expr, "lwc1", "T,o(b)", op[0],
17a2f251 11333 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 11334 break;
252b5132
RH
11335 }
11336
11337 case M_LI_D:
ca4e0257
RS
11338 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
11339 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
11340 order 32 bits of the value and the low order 32 bits are either
11341 zero or in OFFSET_EXPR. */
252b5132
RH
11342 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
11343 {
ca4e0257 11344 if (HAVE_64BIT_GPRS)
c0ebe874 11345 load_register (op[0], &imm_expr, 1);
252b5132
RH
11346 else
11347 {
11348 int hreg, lreg;
11349
11350 if (target_big_endian)
11351 {
c0ebe874
RS
11352 hreg = op[0];
11353 lreg = op[0] + 1;
252b5132
RH
11354 }
11355 else
11356 {
c0ebe874
RS
11357 hreg = op[0] + 1;
11358 lreg = op[0];
252b5132
RH
11359 }
11360
11361 if (hreg <= 31)
67c0d1eb 11362 load_register (hreg, &imm_expr, 0);
252b5132
RH
11363 if (lreg <= 31)
11364 {
11365 if (offset_expr.X_op == O_absent)
67c0d1eb 11366 move_register (lreg, 0);
252b5132
RH
11367 else
11368 {
9c2799c2 11369 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 11370 load_register (lreg, &offset_expr, 0);
252b5132
RH
11371 }
11372 }
11373 }
8fc2e39e 11374 break;
252b5132
RH
11375 }
11376
11377 /* We know that sym is in the .rdata section. First we get the
11378 upper 16 bits of the address. */
11379 if (mips_pic == NO_PIC)
11380 {
67c0d1eb 11381 macro_build_lui (&offset_expr, AT);
8fc2e39e 11382 used_at = 1;
252b5132 11383 }
0a44bf69 11384 else
252b5132 11385 {
67c0d1eb
RS
11386 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
11387 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 11388 used_at = 1;
252b5132 11389 }
bdaaa2e1 11390
252b5132 11391 /* Now we load the register(s). */
ca4e0257 11392 if (HAVE_64BIT_GPRS)
8fc2e39e
TS
11393 {
11394 used_at = 1;
c0ebe874
RS
11395 macro_build (&offset_expr, "ld", "t,o(b)", op[0],
11396 BFD_RELOC_LO16, AT);
8fc2e39e 11397 }
252b5132
RH
11398 else
11399 {
8fc2e39e 11400 used_at = 1;
c0ebe874
RS
11401 macro_build (&offset_expr, "lw", "t,o(b)", op[0],
11402 BFD_RELOC_LO16, AT);
11403 if (op[0] != RA)
252b5132
RH
11404 {
11405 /* FIXME: How in the world do we deal with the possible
11406 overflow here? */
11407 offset_expr.X_add_number += 4;
67c0d1eb 11408 macro_build (&offset_expr, "lw", "t,o(b)",
c0ebe874 11409 op[0] + 1, BFD_RELOC_LO16, AT);
252b5132
RH
11410 }
11411 }
252b5132
RH
11412 break;
11413
11414 case M_LI_DD:
ca4e0257
RS
11415 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
11416 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
11417 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
11418 the value and the low order 32 bits are either zero or in
11419 OFFSET_EXPR. */
252b5132
RH
11420 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
11421 {
8fc2e39e 11422 used_at = 1;
67c0d1eb 11423 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
ca4e0257
RS
11424 if (HAVE_64BIT_FPRS)
11425 {
9c2799c2 11426 gas_assert (HAVE_64BIT_GPRS);
c0ebe874 11427 macro_build (NULL, "dmtc1", "t,S", AT, op[0]);
ca4e0257 11428 }
252b5132
RH
11429 else
11430 {
c0ebe874 11431 macro_build (NULL, "mtc1", "t,G", AT, op[0] + 1);
252b5132 11432 if (offset_expr.X_op == O_absent)
c0ebe874 11433 macro_build (NULL, "mtc1", "t,G", 0, op[0]);
252b5132
RH
11434 else
11435 {
9c2799c2 11436 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 11437 load_register (AT, &offset_expr, 0);
c0ebe874 11438 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
11439 }
11440 }
11441 break;
11442 }
11443
9c2799c2 11444 gas_assert (offset_expr.X_op == O_symbol
90ecf173 11445 && offset_expr.X_add_number == 0);
252b5132
RH
11446 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
11447 if (strcmp (s, ".lit8") == 0)
f2ae14a1 11448 {
c0ebe874 11449 op[2] = mips_gp_register;
f2ae14a1
RS
11450 offset_reloc[0] = BFD_RELOC_MIPS_LITERAL;
11451 offset_reloc[1] = BFD_RELOC_UNUSED;
11452 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
11453 }
11454 else
11455 {
9c2799c2 11456 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 11457 used_at = 1;
0a44bf69 11458 if (mips_pic != NO_PIC)
67c0d1eb
RS
11459 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
11460 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
11461 else
11462 {
11463 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 11464 macro_build_lui (&offset_expr, AT);
252b5132 11465 }
bdaaa2e1 11466
c0ebe874 11467 op[2] = AT;
f2ae14a1
RS
11468 offset_reloc[0] = BFD_RELOC_LO16;
11469 offset_reloc[1] = BFD_RELOC_UNUSED;
11470 offset_reloc[2] = BFD_RELOC_UNUSED;
11471 }
11472 align = 8;
11473 /* Fall through */
c4a68bea 11474
252b5132
RH
11475 case M_L_DAB:
11476 /*
11477 * The MIPS assembler seems to check for X_add_number not
11478 * being double aligned and generating:
11479 * lui at,%hi(foo+1)
11480 * addu at,at,v1
11481 * addiu at,at,%lo(foo+1)
11482 * lwc1 f2,0(at)
11483 * lwc1 f3,4(at)
11484 * But, the resulting address is the same after relocation so why
11485 * generate the extra instruction?
11486 */
bdaaa2e1 11487 /* Itbl support may require additional care here. */
252b5132 11488 coproc = 1;
df58fc94 11489 fmt = "T,o(b)";
0aa27725 11490 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
11491 {
11492 s = "ldc1";
df58fc94 11493 goto ld_st;
252b5132 11494 }
252b5132 11495 s = "lwc1";
252b5132
RH
11496 goto ldd_std;
11497
11498 case M_S_DAB:
df58fc94
RS
11499 gas_assert (!mips_opts.micromips);
11500 /* Itbl support may require additional care here. */
11501 coproc = 1;
11502 fmt = "T,o(b)";
0aa27725 11503 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
11504 {
11505 s = "sdc1";
df58fc94 11506 goto ld_st;
252b5132 11507 }
252b5132 11508 s = "swc1";
252b5132
RH
11509 goto ldd_std;
11510
e407c74b
NC
11511 case M_LQ_AB:
11512 fmt = "t,o(b)";
11513 s = "lq";
11514 goto ld;
11515
11516 case M_SQ_AB:
11517 fmt = "t,o(b)";
11518 s = "sq";
11519 goto ld_st;
11520
252b5132 11521 case M_LD_AB:
df58fc94 11522 fmt = "t,o(b)";
ca4e0257 11523 if (HAVE_64BIT_GPRS)
252b5132
RH
11524 {
11525 s = "ld";
11526 goto ld;
11527 }
252b5132 11528 s = "lw";
252b5132
RH
11529 goto ldd_std;
11530
11531 case M_SD_AB:
df58fc94 11532 fmt = "t,o(b)";
ca4e0257 11533 if (HAVE_64BIT_GPRS)
252b5132
RH
11534 {
11535 s = "sd";
df58fc94 11536 goto ld_st;
252b5132 11537 }
252b5132 11538 s = "sw";
252b5132
RH
11539
11540 ldd_std:
f2ae14a1
RS
11541 /* Even on a big endian machine $fn comes before $fn+1. We have
11542 to adjust when loading from memory. We set coproc if we must
11543 load $fn+1 first. */
11544 /* Itbl support may require additional care here. */
11545 if (!target_big_endian)
11546 coproc = 0;
11547
c0ebe874 11548 breg = op[2];
f2ae14a1
RS
11549 if (small_offset_p (0, align, 16))
11550 {
11551 ep = &offset_expr;
11552 if (!small_offset_p (4, align, 16))
11553 {
11554 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT, breg,
11555 -1, offset_reloc[0], offset_reloc[1],
11556 offset_reloc[2]);
11557 expr1.X_add_number = 0;
11558 ep = &expr1;
11559 breg = AT;
11560 used_at = 1;
11561 offset_reloc[0] = BFD_RELOC_LO16;
11562 offset_reloc[1] = BFD_RELOC_UNUSED;
11563 offset_reloc[2] = BFD_RELOC_UNUSED;
11564 }
c0ebe874 11565 if (strcmp (s, "lw") == 0 && op[0] == breg)
f2ae14a1
RS
11566 {
11567 ep->X_add_number += 4;
c0ebe874 11568 macro_build (ep, s, fmt, op[0] + 1, -1, offset_reloc[0],
f2ae14a1
RS
11569 offset_reloc[1], offset_reloc[2], breg);
11570 ep->X_add_number -= 4;
c0ebe874 11571 macro_build (ep, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
11572 offset_reloc[1], offset_reloc[2], breg);
11573 }
11574 else
11575 {
c0ebe874 11576 macro_build (ep, s, fmt, coproc ? op[0] + 1 : op[0], -1,
f2ae14a1
RS
11577 offset_reloc[0], offset_reloc[1], offset_reloc[2],
11578 breg);
11579 ep->X_add_number += 4;
c0ebe874 11580 macro_build (ep, s, fmt, coproc ? op[0] : op[0] + 1, -1,
f2ae14a1
RS
11581 offset_reloc[0], offset_reloc[1], offset_reloc[2],
11582 breg);
11583 }
11584 break;
11585 }
11586
252b5132
RH
11587 if (offset_expr.X_op != O_symbol
11588 && offset_expr.X_op != O_constant)
11589 {
f71d0d44 11590 as_bad (_("Expression too complex"));
252b5132
RH
11591 offset_expr.X_op = O_constant;
11592 }
11593
2051e8c4
MR
11594 if (HAVE_32BIT_ADDRESSES
11595 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
11596 {
11597 char value [32];
11598
11599 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 11600 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 11601 }
2051e8c4 11602
90ecf173 11603 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
252b5132
RH
11604 {
11605 /* If this is a reference to a GP relative symbol, we want
c0ebe874
RS
11606 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
11607 <op> op[0]+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
11608 If we have a base register, we use this
11609 addu $at,$breg,$gp
c0ebe874
RS
11610 <op> op[0],<sym>($at) (BFD_RELOC_GPREL16)
11611 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
11612 If this is not a GP relative symbol, we want
11613 lui $at,<sym> (BFD_RELOC_HI16_S)
c0ebe874
RS
11614 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
11615 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
11616 If there is a base register, we add it to $at after the
11617 lui instruction. If there is a constant, we always use
11618 the last case. */
39a59cf8
MR
11619 if (offset_expr.X_op == O_symbol
11620 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 11621 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 11622 {
4d7206a2 11623 relax_start (offset_expr.X_add_symbol);
252b5132
RH
11624 if (breg == 0)
11625 {
c9914766 11626 tempreg = mips_gp_register;
252b5132
RH
11627 }
11628 else
11629 {
67c0d1eb 11630 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11631 AT, breg, mips_gp_register);
252b5132 11632 tempreg = AT;
252b5132
RH
11633 used_at = 1;
11634 }
11635
beae10d5 11636 /* Itbl support may require additional care here. */
c0ebe874 11637 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 11638 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
11639 offset_expr.X_add_number += 4;
11640
11641 /* Set mips_optimize to 2 to avoid inserting an
11642 undesired nop. */
11643 hold_mips_optimize = mips_optimize;
11644 mips_optimize = 2;
beae10d5 11645 /* Itbl support may require additional care here. */
c0ebe874 11646 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 11647 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
11648 mips_optimize = hold_mips_optimize;
11649
4d7206a2 11650 relax_switch ();
252b5132 11651
0970e49e 11652 offset_expr.X_add_number -= 4;
252b5132 11653 }
8fc2e39e 11654 used_at = 1;
f2ae14a1
RS
11655 if (offset_high_part (offset_expr.X_add_number, 16)
11656 != offset_high_part (offset_expr.X_add_number + 4, 16))
11657 {
11658 load_address (AT, &offset_expr, &used_at);
11659 offset_expr.X_op = O_constant;
11660 offset_expr.X_add_number = 0;
11661 }
11662 else
11663 macro_build_lui (&offset_expr, AT);
252b5132 11664 if (breg != 0)
67c0d1eb 11665 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 11666 /* Itbl support may require additional care here. */
c0ebe874 11667 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 11668 BFD_RELOC_LO16, AT);
252b5132
RH
11669 /* FIXME: How do we handle overflow here? */
11670 offset_expr.X_add_number += 4;
beae10d5 11671 /* Itbl support may require additional care here. */
c0ebe874 11672 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 11673 BFD_RELOC_LO16, AT);
4d7206a2
RS
11674 if (mips_relax.sequence)
11675 relax_end ();
bdaaa2e1 11676 }
0a44bf69 11677 else if (!mips_big_got)
252b5132 11678 {
252b5132
RH
11679 /* If this is a reference to an external symbol, we want
11680 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11681 nop
c0ebe874
RS
11682 <op> op[0],0($at)
11683 <op> op[0]+1,4($at)
252b5132
RH
11684 Otherwise we want
11685 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11686 nop
c0ebe874
RS
11687 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
11688 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
11689 If there is a base register we add it to $at before the
11690 lwc1 instructions. If there is a constant we include it
11691 in the lwc1 instructions. */
11692 used_at = 1;
11693 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
11694 if (expr1.X_add_number < -0x8000
11695 || expr1.X_add_number >= 0x8000 - 4)
11696 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 11697 load_got_offset (AT, &offset_expr);
269137b2 11698 load_delay_nop ();
252b5132 11699 if (breg != 0)
67c0d1eb 11700 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
11701
11702 /* Set mips_optimize to 2 to avoid inserting an undesired
11703 nop. */
11704 hold_mips_optimize = mips_optimize;
11705 mips_optimize = 2;
4d7206a2 11706
beae10d5 11707 /* Itbl support may require additional care here. */
4d7206a2 11708 relax_start (offset_expr.X_add_symbol);
c0ebe874 11709 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 11710 BFD_RELOC_LO16, AT);
4d7206a2 11711 expr1.X_add_number += 4;
c0ebe874 11712 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 11713 BFD_RELOC_LO16, AT);
4d7206a2 11714 relax_switch ();
c0ebe874 11715 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 11716 BFD_RELOC_LO16, AT);
4d7206a2 11717 offset_expr.X_add_number += 4;
c0ebe874 11718 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 11719 BFD_RELOC_LO16, AT);
4d7206a2 11720 relax_end ();
252b5132 11721
4d7206a2 11722 mips_optimize = hold_mips_optimize;
252b5132 11723 }
0a44bf69 11724 else if (mips_big_got)
252b5132 11725 {
67c0d1eb 11726 int gpdelay;
252b5132
RH
11727
11728 /* If this is a reference to an external symbol, we want
11729 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11730 addu $at,$at,$gp
11731 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
11732 nop
c0ebe874
RS
11733 <op> op[0],0($at)
11734 <op> op[0]+1,4($at)
252b5132
RH
11735 Otherwise we want
11736 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11737 nop
c0ebe874
RS
11738 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
11739 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
11740 If there is a base register we add it to $at before the
11741 lwc1 instructions. If there is a constant we include it
11742 in the lwc1 instructions. */
11743 used_at = 1;
11744 expr1.X_add_number = offset_expr.X_add_number;
11745 offset_expr.X_add_number = 0;
11746 if (expr1.X_add_number < -0x8000
11747 || expr1.X_add_number >= 0x8000 - 4)
11748 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 11749 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 11750 relax_start (offset_expr.X_add_symbol);
df58fc94 11751 macro_build (&offset_expr, "lui", LUI_FMT,
67c0d1eb
RS
11752 AT, BFD_RELOC_MIPS_GOT_HI16);
11753 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11754 AT, AT, mips_gp_register);
67c0d1eb 11755 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 11756 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 11757 load_delay_nop ();
252b5132 11758 if (breg != 0)
67c0d1eb 11759 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 11760 /* Itbl support may require additional care here. */
c0ebe874 11761 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 11762 BFD_RELOC_LO16, AT);
252b5132
RH
11763 expr1.X_add_number += 4;
11764
11765 /* Set mips_optimize to 2 to avoid inserting an undesired
11766 nop. */
11767 hold_mips_optimize = mips_optimize;
11768 mips_optimize = 2;
beae10d5 11769 /* Itbl support may require additional care here. */
c0ebe874 11770 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 11771 BFD_RELOC_LO16, AT);
252b5132
RH
11772 mips_optimize = hold_mips_optimize;
11773 expr1.X_add_number -= 4;
11774
4d7206a2
RS
11775 relax_switch ();
11776 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
11777 if (gpdelay)
11778 macro_build (NULL, "nop", "");
11779 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
11780 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 11781 load_delay_nop ();
252b5132 11782 if (breg != 0)
67c0d1eb 11783 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 11784 /* Itbl support may require additional care here. */
c0ebe874 11785 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 11786 BFD_RELOC_LO16, AT);
4d7206a2 11787 offset_expr.X_add_number += 4;
252b5132
RH
11788
11789 /* Set mips_optimize to 2 to avoid inserting an undesired
11790 nop. */
11791 hold_mips_optimize = mips_optimize;
11792 mips_optimize = 2;
beae10d5 11793 /* Itbl support may require additional care here. */
c0ebe874 11794 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 11795 BFD_RELOC_LO16, AT);
252b5132 11796 mips_optimize = hold_mips_optimize;
4d7206a2 11797 relax_end ();
252b5132 11798 }
252b5132
RH
11799 else
11800 abort ();
11801
252b5132 11802 break;
dd6a37e7
AP
11803
11804 case M_SAA_AB:
dd6a37e7 11805 s = "saa";
7f3c4072 11806 offbits = 0;
dd6a37e7
AP
11807 fmt = "t,(b)";
11808 goto ld_st;
11809 case M_SAAD_AB:
dd6a37e7 11810 s = "saad";
7f3c4072 11811 offbits = 0;
dd6a37e7
AP
11812 fmt = "t,(b)";
11813 goto ld_st;
11814
252b5132
RH
11815 /* New code added to support COPZ instructions.
11816 This code builds table entries out of the macros in mip_opcodes.
11817 R4000 uses interlocks to handle coproc delays.
11818 Other chips (like the R3000) require nops to be inserted for delays.
11819
f72c8c98 11820 FIXME: Currently, we require that the user handle delays.
252b5132
RH
11821 In order to fill delay slots for non-interlocked chips,
11822 we must have a way to specify delays based on the coprocessor.
11823 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
11824 What are the side-effects of the cop instruction?
11825 What cache support might we have and what are its effects?
11826 Both coprocessor & memory require delays. how long???
bdaaa2e1 11827 What registers are read/set/modified?
252b5132
RH
11828
11829 If an itbl is provided to interpret cop instructions,
bdaaa2e1 11830 this knowledge can be encoded in the itbl spec. */
252b5132
RH
11831
11832 case M_COP0:
11833 s = "c0";
11834 goto copz;
11835 case M_COP1:
11836 s = "c1";
11837 goto copz;
11838 case M_COP2:
11839 s = "c2";
11840 goto copz;
11841 case M_COP3:
11842 s = "c3";
11843 copz:
df58fc94 11844 gas_assert (!mips_opts.micromips);
252b5132
RH
11845 /* For now we just do C (same as Cz). The parameter will be
11846 stored in insn_opcode by mips_ip. */
c8276761 11847 macro_build (NULL, s, "C", (int) ip->insn_opcode);
8fc2e39e 11848 break;
252b5132 11849
ea1fb5dc 11850 case M_MOVE:
c0ebe874 11851 move_register (op[0], op[1]);
8fc2e39e 11852 break;
ea1fb5dc 11853
833794fc
MR
11854 case M_MOVEP:
11855 gas_assert (mips_opts.micromips);
11856 gas_assert (mips_opts.insn32);
c0ebe874
RS
11857 move_register (micromips_to_32_reg_h_map1[op[0]],
11858 micromips_to_32_reg_m_map[op[1]]);
11859 move_register (micromips_to_32_reg_h_map2[op[0]],
11860 micromips_to_32_reg_n_map[op[2]]);
833794fc
MR
11861 break;
11862
252b5132
RH
11863 case M_DMUL:
11864 dbl = 1;
11865 case M_MUL:
e407c74b 11866 if (mips_opts.arch == CPU_R5900)
c0ebe874
RS
11867 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", op[0], op[1],
11868 op[2]);
e407c74b
NC
11869 else
11870 {
c0ebe874
RS
11871 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", op[1], op[2]);
11872 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
e407c74b 11873 }
8fc2e39e 11874 break;
252b5132
RH
11875
11876 case M_DMUL_I:
11877 dbl = 1;
11878 case M_MUL_I:
11879 /* The MIPS assembler some times generates shifts and adds. I'm
11880 not trying to be that fancy. GCC should do this for us
11881 anyway. */
8fc2e39e 11882 used_at = 1;
67c0d1eb 11883 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
11884 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", op[1], AT);
11885 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
11886 break;
11887
11888 case M_DMULO_I:
11889 dbl = 1;
11890 case M_MULO_I:
11891 imm = 1;
11892 goto do_mulo;
11893
11894 case M_DMULO:
11895 dbl = 1;
11896 case M_MULO:
11897 do_mulo:
7d10b47d 11898 start_noreorder ();
8fc2e39e 11899 used_at = 1;
252b5132 11900 if (imm)
67c0d1eb 11901 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
11902 macro_build (NULL, dbl ? "dmult" : "mult", "s,t",
11903 op[1], imm ? AT : op[2]);
11904 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
11905 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, op[0], op[0], 31);
df58fc94 11906 macro_build (NULL, "mfhi", MFHL_FMT, AT);
252b5132 11907 if (mips_trap)
c0ebe874 11908 macro_build (NULL, "tne", TRAP_FMT, op[0], AT, 6);
252b5132
RH
11909 else
11910 {
df58fc94
RS
11911 if (mips_opts.micromips)
11912 micromips_label_expr (&label_expr);
11913 else
11914 label_expr.X_add_number = 8;
c0ebe874 11915 macro_build (&label_expr, "beq", "s,t,p", op[0], AT);
a605d2b3 11916 macro_build (NULL, "nop", "");
df58fc94
RS
11917 macro_build (NULL, "break", BRK_FMT, 6);
11918 if (mips_opts.micromips)
11919 micromips_add_label ();
252b5132 11920 }
7d10b47d 11921 end_noreorder ();
c0ebe874 11922 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
11923 break;
11924
11925 case M_DMULOU_I:
11926 dbl = 1;
11927 case M_MULOU_I:
11928 imm = 1;
11929 goto do_mulou;
11930
11931 case M_DMULOU:
11932 dbl = 1;
11933 case M_MULOU:
11934 do_mulou:
7d10b47d 11935 start_noreorder ();
8fc2e39e 11936 used_at = 1;
252b5132 11937 if (imm)
67c0d1eb
RS
11938 load_register (AT, &imm_expr, dbl);
11939 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
c0ebe874 11940 op[1], imm ? AT : op[2]);
df58fc94 11941 macro_build (NULL, "mfhi", MFHL_FMT, AT);
c0ebe874 11942 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132 11943 if (mips_trap)
df58fc94 11944 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
252b5132
RH
11945 else
11946 {
df58fc94
RS
11947 if (mips_opts.micromips)
11948 micromips_label_expr (&label_expr);
11949 else
11950 label_expr.X_add_number = 8;
11951 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
a605d2b3 11952 macro_build (NULL, "nop", "");
df58fc94
RS
11953 macro_build (NULL, "break", BRK_FMT, 6);
11954 if (mips_opts.micromips)
11955 micromips_add_label ();
252b5132 11956 }
7d10b47d 11957 end_noreorder ();
252b5132
RH
11958 break;
11959
771c7ce4 11960 case M_DROL:
fef14a42 11961 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 11962 {
c0ebe874 11963 if (op[0] == op[1])
82dd0097
CD
11964 {
11965 tempreg = AT;
11966 used_at = 1;
11967 }
11968 else
c0ebe874
RS
11969 tempreg = op[0];
11970 macro_build (NULL, "dnegu", "d,w", tempreg, op[2]);
11971 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 11972 break;
82dd0097 11973 }
8fc2e39e 11974 used_at = 1;
c0ebe874
RS
11975 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
11976 macro_build (NULL, "dsrlv", "d,t,s", AT, op[1], AT);
11977 macro_build (NULL, "dsllv", "d,t,s", op[0], op[1], op[2]);
11978 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
11979 break;
11980
252b5132 11981 case M_ROL:
fef14a42 11982 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 11983 {
c0ebe874 11984 if (op[0] == op[1])
82dd0097
CD
11985 {
11986 tempreg = AT;
11987 used_at = 1;
11988 }
11989 else
c0ebe874
RS
11990 tempreg = op[0];
11991 macro_build (NULL, "negu", "d,w", tempreg, op[2]);
11992 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 11993 break;
82dd0097 11994 }
8fc2e39e 11995 used_at = 1;
c0ebe874
RS
11996 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
11997 macro_build (NULL, "srlv", "d,t,s", AT, op[1], AT);
11998 macro_build (NULL, "sllv", "d,t,s", op[0], op[1], op[2]);
11999 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
12000 break;
12001
771c7ce4
TS
12002 case M_DROL_I:
12003 {
12004 unsigned int rot;
91d6fa6a
NC
12005 char *l;
12006 char *rr;
771c7ce4
TS
12007
12008 if (imm_expr.X_op != O_constant)
82dd0097 12009 as_bad (_("Improper rotate count"));
771c7ce4 12010 rot = imm_expr.X_add_number & 0x3f;
fef14a42 12011 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
12012 {
12013 rot = (64 - rot) & 0x3f;
12014 if (rot >= 32)
c0ebe874 12015 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
60b63b72 12016 else
c0ebe874 12017 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 12018 break;
60b63b72 12019 }
483fc7cd 12020 if (rot == 0)
483fc7cd 12021 {
c0ebe874 12022 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 12023 break;
483fc7cd 12024 }
82dd0097 12025 l = (rot < 0x20) ? "dsll" : "dsll32";
91d6fa6a 12026 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
82dd0097 12027 rot &= 0x1f;
8fc2e39e 12028 used_at = 1;
c0ebe874
RS
12029 macro_build (NULL, l, SHFT_FMT, AT, op[1], rot);
12030 macro_build (NULL, rr, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
12031 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
12032 }
12033 break;
12034
252b5132 12035 case M_ROL_I:
771c7ce4
TS
12036 {
12037 unsigned int rot;
12038
12039 if (imm_expr.X_op != O_constant)
82dd0097 12040 as_bad (_("Improper rotate count"));
771c7ce4 12041 rot = imm_expr.X_add_number & 0x1f;
fef14a42 12042 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 12043 {
c0ebe874
RS
12044 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1],
12045 (32 - rot) & 0x1f);
8fc2e39e 12046 break;
60b63b72 12047 }
483fc7cd 12048 if (rot == 0)
483fc7cd 12049 {
c0ebe874 12050 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 12051 break;
483fc7cd 12052 }
8fc2e39e 12053 used_at = 1;
c0ebe874
RS
12054 macro_build (NULL, "sll", SHFT_FMT, AT, op[1], rot);
12055 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
12056 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
12057 }
12058 break;
12059
12060 case M_DROR:
fef14a42 12061 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 12062 {
c0ebe874 12063 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 12064 break;
82dd0097 12065 }
8fc2e39e 12066 used_at = 1;
c0ebe874
RS
12067 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
12068 macro_build (NULL, "dsllv", "d,t,s", AT, op[1], AT);
12069 macro_build (NULL, "dsrlv", "d,t,s", op[0], op[1], op[2]);
12070 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
12071 break;
12072
12073 case M_ROR:
fef14a42 12074 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 12075 {
c0ebe874 12076 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 12077 break;
82dd0097 12078 }
8fc2e39e 12079 used_at = 1;
c0ebe874
RS
12080 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
12081 macro_build (NULL, "sllv", "d,t,s", AT, op[1], AT);
12082 macro_build (NULL, "srlv", "d,t,s", op[0], op[1], op[2]);
12083 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
12084 break;
12085
771c7ce4
TS
12086 case M_DROR_I:
12087 {
12088 unsigned int rot;
91d6fa6a
NC
12089 char *l;
12090 char *rr;
771c7ce4
TS
12091
12092 if (imm_expr.X_op != O_constant)
82dd0097 12093 as_bad (_("Improper rotate count"));
771c7ce4 12094 rot = imm_expr.X_add_number & 0x3f;
fef14a42 12095 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
12096 {
12097 if (rot >= 32)
c0ebe874 12098 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
82dd0097 12099 else
c0ebe874 12100 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 12101 break;
82dd0097 12102 }
483fc7cd 12103 if (rot == 0)
483fc7cd 12104 {
c0ebe874 12105 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 12106 break;
483fc7cd 12107 }
91d6fa6a 12108 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
82dd0097
CD
12109 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
12110 rot &= 0x1f;
8fc2e39e 12111 used_at = 1;
c0ebe874
RS
12112 macro_build (NULL, rr, SHFT_FMT, AT, op[1], rot);
12113 macro_build (NULL, l, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
12114 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
12115 }
12116 break;
12117
252b5132 12118 case M_ROR_I:
771c7ce4
TS
12119 {
12120 unsigned int rot;
12121
12122 if (imm_expr.X_op != O_constant)
82dd0097 12123 as_bad (_("Improper rotate count"));
771c7ce4 12124 rot = imm_expr.X_add_number & 0x1f;
fef14a42 12125 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 12126 {
c0ebe874 12127 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 12128 break;
82dd0097 12129 }
483fc7cd 12130 if (rot == 0)
483fc7cd 12131 {
c0ebe874 12132 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 12133 break;
483fc7cd 12134 }
8fc2e39e 12135 used_at = 1;
c0ebe874
RS
12136 macro_build (NULL, "srl", SHFT_FMT, AT, op[1], rot);
12137 macro_build (NULL, "sll", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
12138 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4 12139 }
252b5132
RH
12140 break;
12141
252b5132 12142 case M_SEQ:
c0ebe874
RS
12143 if (op[1] == 0)
12144 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[2], BFD_RELOC_LO16);
12145 else if (op[2] == 0)
12146 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
12147 else
12148 {
c0ebe874
RS
12149 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
12150 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
252b5132 12151 }
8fc2e39e 12152 break;
252b5132
RH
12153
12154 case M_SEQ_I:
12155 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
12156 {
c0ebe874 12157 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 12158 break;
252b5132 12159 }
c0ebe874 12160 if (op[1] == 0)
252b5132
RH
12161 {
12162 as_warn (_("Instruction %s: result is always false"),
12163 ip->insn_mo->name);
c0ebe874 12164 move_register (op[0], 0);
8fc2e39e 12165 break;
252b5132 12166 }
dd3cbb7e
NC
12167 if (CPU_HAS_SEQ (mips_opts.arch)
12168 && -512 <= imm_expr.X_add_number
12169 && imm_expr.X_add_number < 512)
12170 {
c0ebe874 12171 macro_build (NULL, "seqi", "t,r,+Q", op[0], op[1],
750bdd57 12172 (int) imm_expr.X_add_number);
dd3cbb7e
NC
12173 break;
12174 }
252b5132
RH
12175 if (imm_expr.X_op == O_constant
12176 && imm_expr.X_add_number >= 0
12177 && imm_expr.X_add_number < 0x10000)
c0ebe874 12178 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
12179 else if (imm_expr.X_op == O_constant
12180 && imm_expr.X_add_number > -0x8000
12181 && imm_expr.X_add_number < 0)
12182 {
12183 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 12184 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
c0ebe874 12185 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 12186 }
dd3cbb7e
NC
12187 else if (CPU_HAS_SEQ (mips_opts.arch))
12188 {
12189 used_at = 1;
12190 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 12191 macro_build (NULL, "seq", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
12192 break;
12193 }
252b5132
RH
12194 else
12195 {
67c0d1eb 12196 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 12197 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
12198 used_at = 1;
12199 }
c0ebe874 12200 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 12201 break;
252b5132 12202
c0ebe874 12203 case M_SGE: /* X >= Y <==> not (X < Y) */
252b5132
RH
12204 s = "slt";
12205 goto sge;
12206 case M_SGEU:
12207 s = "sltu";
12208 sge:
c0ebe874
RS
12209 macro_build (NULL, s, "d,v,t", op[0], op[1], op[2]);
12210 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 12211 break;
252b5132 12212
c0ebe874 12213 case M_SGE_I: /* X >= I <==> not (X < I) */
252b5132
RH
12214 case M_SGEU_I:
12215 if (imm_expr.X_op == O_constant
12216 && imm_expr.X_add_number >= -0x8000
12217 && imm_expr.X_add_number < 0x8000)
c0ebe874
RS
12218 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
12219 op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
12220 else
12221 {
67c0d1eb
RS
12222 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
12223 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
c0ebe874 12224 op[0], op[1], AT);
252b5132
RH
12225 used_at = 1;
12226 }
c0ebe874 12227 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 12228 break;
252b5132 12229
c0ebe874 12230 case M_SGT: /* X > Y <==> Y < X */
252b5132
RH
12231 s = "slt";
12232 goto sgt;
12233 case M_SGTU:
12234 s = "sltu";
12235 sgt:
c0ebe874 12236 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
8fc2e39e 12237 break;
252b5132 12238
c0ebe874 12239 case M_SGT_I: /* X > I <==> I < X */
252b5132
RH
12240 s = "slt";
12241 goto sgti;
12242 case M_SGTU_I:
12243 s = "sltu";
12244 sgti:
8fc2e39e 12245 used_at = 1;
67c0d1eb 12246 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 12247 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
252b5132
RH
12248 break;
12249
c0ebe874 12250 case M_SLE: /* X <= Y <==> Y >= X <==> not (Y < X) */
252b5132
RH
12251 s = "slt";
12252 goto sle;
12253 case M_SLEU:
12254 s = "sltu";
12255 sle:
c0ebe874
RS
12256 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
12257 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 12258 break;
252b5132 12259
c0ebe874 12260 case M_SLE_I: /* X <= I <==> I >= X <==> not (I < X) */
252b5132
RH
12261 s = "slt";
12262 goto slei;
12263 case M_SLEU_I:
12264 s = "sltu";
12265 slei:
8fc2e39e 12266 used_at = 1;
67c0d1eb 12267 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874
RS
12268 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
12269 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
252b5132
RH
12270 break;
12271
12272 case M_SLT_I:
12273 if (imm_expr.X_op == O_constant
12274 && imm_expr.X_add_number >= -0x8000
12275 && imm_expr.X_add_number < 0x8000)
12276 {
c0ebe874
RS
12277 macro_build (&imm_expr, "slti", "t,r,j", op[0], op[1],
12278 BFD_RELOC_LO16);
8fc2e39e 12279 break;
252b5132 12280 }
8fc2e39e 12281 used_at = 1;
67c0d1eb 12282 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 12283 macro_build (NULL, "slt", "d,v,t", op[0], op[1], AT);
252b5132
RH
12284 break;
12285
12286 case M_SLTU_I:
12287 if (imm_expr.X_op == O_constant
12288 && imm_expr.X_add_number >= -0x8000
12289 && imm_expr.X_add_number < 0x8000)
12290 {
c0ebe874 12291 macro_build (&imm_expr, "sltiu", "t,r,j", op[0], op[1],
17a2f251 12292 BFD_RELOC_LO16);
8fc2e39e 12293 break;
252b5132 12294 }
8fc2e39e 12295 used_at = 1;
67c0d1eb 12296 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 12297 macro_build (NULL, "sltu", "d,v,t", op[0], op[1], AT);
252b5132
RH
12298 break;
12299
12300 case M_SNE:
c0ebe874
RS
12301 if (op[1] == 0)
12302 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[2]);
12303 else if (op[2] == 0)
12304 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
252b5132
RH
12305 else
12306 {
c0ebe874
RS
12307 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
12308 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
252b5132 12309 }
8fc2e39e 12310 break;
252b5132
RH
12311
12312 case M_SNE_I:
12313 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
12314 {
c0ebe874 12315 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
8fc2e39e 12316 break;
252b5132 12317 }
c0ebe874 12318 if (op[1] == 0)
252b5132
RH
12319 {
12320 as_warn (_("Instruction %s: result is always true"),
12321 ip->insn_mo->name);
67c0d1eb 12322 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
c0ebe874 12323 op[0], 0, BFD_RELOC_LO16);
8fc2e39e 12324 break;
252b5132 12325 }
dd3cbb7e
NC
12326 if (CPU_HAS_SEQ (mips_opts.arch)
12327 && -512 <= imm_expr.X_add_number
12328 && imm_expr.X_add_number < 512)
12329 {
c0ebe874 12330 macro_build (NULL, "snei", "t,r,+Q", op[0], op[1],
750bdd57 12331 (int) imm_expr.X_add_number);
dd3cbb7e
NC
12332 break;
12333 }
252b5132
RH
12334 if (imm_expr.X_op == O_constant
12335 && imm_expr.X_add_number >= 0
12336 && imm_expr.X_add_number < 0x10000)
12337 {
c0ebe874
RS
12338 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1],
12339 BFD_RELOC_LO16);
252b5132
RH
12340 }
12341 else if (imm_expr.X_op == O_constant
12342 && imm_expr.X_add_number > -0x8000
12343 && imm_expr.X_add_number < 0)
12344 {
12345 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 12346 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
c0ebe874 12347 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 12348 }
dd3cbb7e
NC
12349 else if (CPU_HAS_SEQ (mips_opts.arch))
12350 {
12351 used_at = 1;
12352 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 12353 macro_build (NULL, "sne", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
12354 break;
12355 }
252b5132
RH
12356 else
12357 {
67c0d1eb 12358 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 12359 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
12360 used_at = 1;
12361 }
c0ebe874 12362 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
8fc2e39e 12363 break;
252b5132 12364
df58fc94
RS
12365 case M_SUB_I:
12366 s = "addi";
12367 s2 = "sub";
12368 goto do_subi;
12369 case M_SUBU_I:
12370 s = "addiu";
12371 s2 = "subu";
12372 goto do_subi;
252b5132
RH
12373 case M_DSUB_I:
12374 dbl = 1;
df58fc94
RS
12375 s = "daddi";
12376 s2 = "dsub";
12377 if (!mips_opts.micromips)
12378 goto do_subi;
252b5132 12379 if (imm_expr.X_op == O_constant
df58fc94
RS
12380 && imm_expr.X_add_number > -0x200
12381 && imm_expr.X_add_number <= 0x200)
252b5132 12382 {
c0ebe874 12383 macro_build (NULL, s, "t,r,.", op[0], op[1], -imm_expr.X_add_number);
8fc2e39e 12384 break;
252b5132 12385 }
df58fc94 12386 goto do_subi_i;
252b5132
RH
12387 case M_DSUBU_I:
12388 dbl = 1;
df58fc94
RS
12389 s = "daddiu";
12390 s2 = "dsubu";
12391 do_subi:
252b5132
RH
12392 if (imm_expr.X_op == O_constant
12393 && imm_expr.X_add_number > -0x8000
12394 && imm_expr.X_add_number <= 0x8000)
12395 {
12396 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 12397 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 12398 break;
252b5132 12399 }
df58fc94 12400 do_subi_i:
8fc2e39e 12401 used_at = 1;
67c0d1eb 12402 load_register (AT, &imm_expr, dbl);
c0ebe874 12403 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
12404 break;
12405
12406 case M_TEQ_I:
12407 s = "teq";
12408 goto trap;
12409 case M_TGE_I:
12410 s = "tge";
12411 goto trap;
12412 case M_TGEU_I:
12413 s = "tgeu";
12414 goto trap;
12415 case M_TLT_I:
12416 s = "tlt";
12417 goto trap;
12418 case M_TLTU_I:
12419 s = "tltu";
12420 goto trap;
12421 case M_TNE_I:
12422 s = "tne";
12423 trap:
8fc2e39e 12424 used_at = 1;
67c0d1eb 12425 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 12426 macro_build (NULL, s, "s,t", op[0], AT);
252b5132
RH
12427 break;
12428
252b5132 12429 case M_TRUNCWS:
43841e91 12430 case M_TRUNCWD:
df58fc94 12431 gas_assert (!mips_opts.micromips);
0aa27725 12432 gas_assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 12433 used_at = 1;
252b5132
RH
12434
12435 /*
12436 * Is the double cfc1 instruction a bug in the mips assembler;
12437 * or is there a reason for it?
12438 */
7d10b47d 12439 start_noreorder ();
c0ebe874
RS
12440 macro_build (NULL, "cfc1", "t,G", op[2], RA);
12441 macro_build (NULL, "cfc1", "t,G", op[2], RA);
67c0d1eb 12442 macro_build (NULL, "nop", "");
252b5132 12443 expr1.X_add_number = 3;
c0ebe874 12444 macro_build (&expr1, "ori", "t,r,i", AT, op[2], BFD_RELOC_LO16);
252b5132 12445 expr1.X_add_number = 2;
67c0d1eb
RS
12446 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
12447 macro_build (NULL, "ctc1", "t,G", AT, RA);
12448 macro_build (NULL, "nop", "");
12449 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
c0ebe874
RS
12450 op[0], op[1]);
12451 macro_build (NULL, "ctc1", "t,G", op[2], RA);
67c0d1eb 12452 macro_build (NULL, "nop", "");
7d10b47d 12453 end_noreorder ();
252b5132
RH
12454 break;
12455
f2ae14a1 12456 case M_ULH_AB:
252b5132 12457 s = "lb";
df58fc94
RS
12458 s2 = "lbu";
12459 off = 1;
12460 goto uld_st;
f2ae14a1 12461 case M_ULHU_AB:
252b5132 12462 s = "lbu";
df58fc94
RS
12463 s2 = "lbu";
12464 off = 1;
12465 goto uld_st;
f2ae14a1 12466 case M_ULW_AB:
df58fc94
RS
12467 s = "lwl";
12468 s2 = "lwr";
7f3c4072 12469 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
12470 off = 3;
12471 goto uld_st;
f2ae14a1 12472 case M_ULD_AB:
252b5132
RH
12473 s = "ldl";
12474 s2 = "ldr";
7f3c4072 12475 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 12476 off = 7;
df58fc94 12477 goto uld_st;
f2ae14a1 12478 case M_USH_AB:
df58fc94
RS
12479 s = "sb";
12480 s2 = "sb";
12481 off = 1;
12482 ust = 1;
12483 goto uld_st;
f2ae14a1 12484 case M_USW_AB:
df58fc94
RS
12485 s = "swl";
12486 s2 = "swr";
7f3c4072 12487 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 12488 off = 3;
df58fc94
RS
12489 ust = 1;
12490 goto uld_st;
f2ae14a1 12491 case M_USD_AB:
df58fc94
RS
12492 s = "sdl";
12493 s2 = "sdr";
7f3c4072 12494 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
12495 off = 7;
12496 ust = 1;
12497
12498 uld_st:
c0ebe874 12499 breg = op[2];
f2ae14a1 12500 large_offset = !small_offset_p (off, align, offbits);
df58fc94
RS
12501 ep = &offset_expr;
12502 expr1.X_add_number = 0;
f2ae14a1 12503 if (large_offset)
df58fc94
RS
12504 {
12505 used_at = 1;
12506 tempreg = AT;
f2ae14a1
RS
12507 if (small_offset_p (0, align, 16))
12508 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg, -1,
12509 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
12510 else
12511 {
12512 load_address (tempreg, ep, &used_at);
12513 if (breg != 0)
12514 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12515 tempreg, tempreg, breg);
12516 }
12517 offset_reloc[0] = BFD_RELOC_LO16;
12518 offset_reloc[1] = BFD_RELOC_UNUSED;
12519 offset_reloc[2] = BFD_RELOC_UNUSED;
df58fc94 12520 breg = tempreg;
c0ebe874 12521 tempreg = op[0];
df58fc94
RS
12522 ep = &expr1;
12523 }
c0ebe874 12524 else if (!ust && op[0] == breg)
8fc2e39e
TS
12525 {
12526 used_at = 1;
12527 tempreg = AT;
12528 }
252b5132 12529 else
c0ebe874 12530 tempreg = op[0];
af22f5b2 12531
df58fc94
RS
12532 if (off == 1)
12533 goto ulh_sh;
252b5132 12534
90ecf173 12535 if (!target_big_endian)
df58fc94 12536 ep->X_add_number += off;
f2ae14a1 12537 if (offbits == 12)
c8276761 12538 macro_build (NULL, s, "t,~(b)", tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
12539 else
12540 macro_build (ep, s, "t,o(b)", tempreg, -1,
12541 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94 12542
90ecf173 12543 if (!target_big_endian)
df58fc94 12544 ep->X_add_number -= off;
252b5132 12545 else
df58fc94 12546 ep->X_add_number += off;
f2ae14a1 12547 if (offbits == 12)
df58fc94 12548 macro_build (NULL, s2, "t,~(b)",
c8276761 12549 tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
12550 else
12551 macro_build (ep, s2, "t,o(b)", tempreg, -1,
12552 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 12553
df58fc94 12554 /* If necessary, move the result in tempreg to the final destination. */
c0ebe874 12555 if (!ust && op[0] != tempreg)
df58fc94
RS
12556 {
12557 /* Protect second load's delay slot. */
12558 load_delay_nop ();
c0ebe874 12559 move_register (op[0], tempreg);
df58fc94 12560 }
8fc2e39e 12561 break;
252b5132 12562
df58fc94 12563 ulh_sh:
d6bc6245 12564 used_at = 1;
df58fc94
RS
12565 if (target_big_endian == ust)
12566 ep->X_add_number += off;
c0ebe874 12567 tempreg = ust || large_offset ? op[0] : AT;
f2ae14a1
RS
12568 macro_build (ep, s, "t,o(b)", tempreg, -1,
12569 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94
RS
12570
12571 /* For halfword transfers we need a temporary register to shuffle
12572 bytes. Unfortunately for M_USH_A we have none available before
12573 the next store as AT holds the base address. We deal with this
12574 case by clobbering TREG and then restoring it as with ULH. */
c0ebe874 12575 tempreg = ust == large_offset ? op[0] : AT;
df58fc94 12576 if (ust)
c0ebe874 12577 macro_build (NULL, "srl", SHFT_FMT, tempreg, op[0], 8);
df58fc94
RS
12578
12579 if (target_big_endian == ust)
12580 ep->X_add_number -= off;
252b5132 12581 else
df58fc94 12582 ep->X_add_number += off;
f2ae14a1
RS
12583 macro_build (ep, s2, "t,o(b)", tempreg, -1,
12584 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 12585
df58fc94 12586 /* For M_USH_A re-retrieve the LSB. */
f2ae14a1 12587 if (ust && large_offset)
df58fc94
RS
12588 {
12589 if (target_big_endian)
12590 ep->X_add_number += off;
12591 else
12592 ep->X_add_number -= off;
f2ae14a1
RS
12593 macro_build (&expr1, "lbu", "t,o(b)", AT, -1,
12594 offset_reloc[0], offset_reloc[1], offset_reloc[2], AT);
df58fc94
RS
12595 }
12596 /* For ULH and M_USH_A OR the LSB in. */
f2ae14a1 12597 if (!ust || large_offset)
df58fc94 12598 {
c0ebe874 12599 tempreg = !large_offset ? AT : op[0];
df58fc94 12600 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
c0ebe874 12601 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
df58fc94 12602 }
252b5132
RH
12603 break;
12604
12605 default:
12606 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 12607 are added dynamically. */
252b5132
RH
12608 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
12609 break;
12610 }
741fe287 12611 if (!mips_opts.at && used_at)
8fc2e39e 12612 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
12613}
12614
12615/* Implement macros in mips16 mode. */
12616
12617static void
17a2f251 12618mips16_macro (struct mips_cl_insn *ip)
252b5132 12619{
c0ebe874 12620 const struct mips_operand_array *operands;
252b5132 12621 int mask;
c0ebe874 12622 int tmp;
252b5132
RH
12623 expressionS expr1;
12624 int dbl;
12625 const char *s, *s2, *s3;
c0ebe874
RS
12626 unsigned int op[MAX_OPERANDS];
12627 unsigned int i;
252b5132
RH
12628
12629 mask = ip->insn_mo->mask;
12630
c0ebe874
RS
12631 operands = insn_operands (ip);
12632 for (i = 0; i < MAX_OPERANDS; i++)
12633 if (operands->operand[i])
12634 op[i] = insn_extract_operand (ip, operands->operand[i]);
12635 else
12636 op[i] = -1;
252b5132 12637
252b5132
RH
12638 expr1.X_op = O_constant;
12639 expr1.X_op_symbol = NULL;
12640 expr1.X_add_symbol = NULL;
12641 expr1.X_add_number = 1;
12642
12643 dbl = 0;
12644
12645 switch (mask)
12646 {
12647 default:
b37df7c4 12648 abort ();
252b5132
RH
12649
12650 case M_DDIV_3:
12651 dbl = 1;
12652 case M_DIV_3:
12653 s = "mflo";
12654 goto do_div3;
12655 case M_DREM_3:
12656 dbl = 1;
12657 case M_REM_3:
12658 s = "mfhi";
12659 do_div3:
7d10b47d 12660 start_noreorder ();
c0ebe874 12661 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", op[1], op[2]);
252b5132 12662 expr1.X_add_number = 2;
c0ebe874 12663 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 12664 macro_build (NULL, "break", "6", 7);
bdaaa2e1 12665
252b5132
RH
12666 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
12667 since that causes an overflow. We should do that as well,
12668 but I don't see how to do the comparisons without a temporary
12669 register. */
7d10b47d 12670 end_noreorder ();
c0ebe874 12671 macro_build (NULL, s, "x", op[0]);
252b5132
RH
12672 break;
12673
12674 case M_DIVU_3:
12675 s = "divu";
12676 s2 = "mflo";
12677 goto do_divu3;
12678 case M_REMU_3:
12679 s = "divu";
12680 s2 = "mfhi";
12681 goto do_divu3;
12682 case M_DDIVU_3:
12683 s = "ddivu";
12684 s2 = "mflo";
12685 goto do_divu3;
12686 case M_DREMU_3:
12687 s = "ddivu";
12688 s2 = "mfhi";
12689 do_divu3:
7d10b47d 12690 start_noreorder ();
c0ebe874 12691 macro_build (NULL, s, "0,x,y", op[1], op[2]);
252b5132 12692 expr1.X_add_number = 2;
c0ebe874 12693 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 12694 macro_build (NULL, "break", "6", 7);
7d10b47d 12695 end_noreorder ();
c0ebe874 12696 macro_build (NULL, s2, "x", op[0]);
252b5132
RH
12697 break;
12698
12699 case M_DMUL:
12700 dbl = 1;
12701 case M_MUL:
c0ebe874
RS
12702 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", op[1], op[2]);
12703 macro_build (NULL, "mflo", "x", op[0]);
8fc2e39e 12704 break;
252b5132
RH
12705
12706 case M_DSUBU_I:
12707 dbl = 1;
12708 goto do_subu;
12709 case M_SUBU_I:
12710 do_subu:
12711 if (imm_expr.X_op != O_constant)
12712 as_bad (_("Unsupported large constant"));
12713 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 12714 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", op[0], op[1]);
252b5132
RH
12715 break;
12716
12717 case M_SUBU_I_2:
12718 if (imm_expr.X_op != O_constant)
12719 as_bad (_("Unsupported large constant"));
12720 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 12721 macro_build (&imm_expr, "addiu", "x,k", op[0]);
252b5132
RH
12722 break;
12723
12724 case M_DSUBU_I_2:
12725 if (imm_expr.X_op != O_constant)
12726 as_bad (_("Unsupported large constant"));
12727 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 12728 macro_build (&imm_expr, "daddiu", "y,j", op[0]);
252b5132
RH
12729 break;
12730
12731 case M_BEQ:
12732 s = "cmp";
12733 s2 = "bteqz";
12734 goto do_branch;
12735 case M_BNE:
12736 s = "cmp";
12737 s2 = "btnez";
12738 goto do_branch;
12739 case M_BLT:
12740 s = "slt";
12741 s2 = "btnez";
12742 goto do_branch;
12743 case M_BLTU:
12744 s = "sltu";
12745 s2 = "btnez";
12746 goto do_branch;
12747 case M_BLE:
12748 s = "slt";
12749 s2 = "bteqz";
12750 goto do_reverse_branch;
12751 case M_BLEU:
12752 s = "sltu";
12753 s2 = "bteqz";
12754 goto do_reverse_branch;
12755 case M_BGE:
12756 s = "slt";
12757 s2 = "bteqz";
12758 goto do_branch;
12759 case M_BGEU:
12760 s = "sltu";
12761 s2 = "bteqz";
12762 goto do_branch;
12763 case M_BGT:
12764 s = "slt";
12765 s2 = "btnez";
12766 goto do_reverse_branch;
12767 case M_BGTU:
12768 s = "sltu";
12769 s2 = "btnez";
12770
12771 do_reverse_branch:
c0ebe874
RS
12772 tmp = op[1];
12773 op[1] = op[0];
12774 op[0] = tmp;
252b5132
RH
12775
12776 do_branch:
c0ebe874 12777 macro_build (NULL, s, "x,y", op[0], op[1]);
67c0d1eb 12778 macro_build (&offset_expr, s2, "p");
252b5132
RH
12779 break;
12780
12781 case M_BEQ_I:
12782 s = "cmpi";
12783 s2 = "bteqz";
12784 s3 = "x,U";
12785 goto do_branch_i;
12786 case M_BNE_I:
12787 s = "cmpi";
12788 s2 = "btnez";
12789 s3 = "x,U";
12790 goto do_branch_i;
12791 case M_BLT_I:
12792 s = "slti";
12793 s2 = "btnez";
12794 s3 = "x,8";
12795 goto do_branch_i;
12796 case M_BLTU_I:
12797 s = "sltiu";
12798 s2 = "btnez";
12799 s3 = "x,8";
12800 goto do_branch_i;
12801 case M_BLE_I:
12802 s = "slti";
12803 s2 = "btnez";
12804 s3 = "x,8";
12805 goto do_addone_branch_i;
12806 case M_BLEU_I:
12807 s = "sltiu";
12808 s2 = "btnez";
12809 s3 = "x,8";
12810 goto do_addone_branch_i;
12811 case M_BGE_I:
12812 s = "slti";
12813 s2 = "bteqz";
12814 s3 = "x,8";
12815 goto do_branch_i;
12816 case M_BGEU_I:
12817 s = "sltiu";
12818 s2 = "bteqz";
12819 s3 = "x,8";
12820 goto do_branch_i;
12821 case M_BGT_I:
12822 s = "slti";
12823 s2 = "bteqz";
12824 s3 = "x,8";
12825 goto do_addone_branch_i;
12826 case M_BGTU_I:
12827 s = "sltiu";
12828 s2 = "bteqz";
12829 s3 = "x,8";
12830
12831 do_addone_branch_i:
12832 if (imm_expr.X_op != O_constant)
12833 as_bad (_("Unsupported large constant"));
12834 ++imm_expr.X_add_number;
12835
12836 do_branch_i:
c0ebe874 12837 macro_build (&imm_expr, s, s3, op[0]);
67c0d1eb 12838 macro_build (&offset_expr, s2, "p");
252b5132
RH
12839 break;
12840
12841 case M_ABS:
12842 expr1.X_add_number = 0;
c0ebe874
RS
12843 macro_build (&expr1, "slti", "x,8", op[1]);
12844 if (op[0] != op[1])
12845 macro_build (NULL, "move", "y,X", op[0], mips16_to_32_reg_map[op[1]]);
252b5132 12846 expr1.X_add_number = 2;
67c0d1eb 12847 macro_build (&expr1, "bteqz", "p");
c0ebe874 12848 macro_build (NULL, "neg", "x,w", op[0], op[0]);
0acfaea6 12849 break;
252b5132
RH
12850 }
12851}
12852
14daeee3
RS
12853/* Look up instruction [START, START + LENGTH) in HASH. Record any extra
12854 opcode bits in *OPCODE_EXTRA. */
12855
12856static struct mips_opcode *
12857mips_lookup_insn (struct hash_control *hash, const char *start,
da8bca91 12858 ssize_t length, unsigned int *opcode_extra)
14daeee3
RS
12859{
12860 char *name, *dot, *p;
12861 unsigned int mask, suffix;
da8bca91 12862 ssize_t opend;
14daeee3
RS
12863 struct mips_opcode *insn;
12864
12865 /* Make a copy of the instruction so that we can fiddle with it. */
12866 name = alloca (length + 1);
12867 memcpy (name, start, length);
12868 name[length] = '\0';
12869
12870 /* Look up the instruction as-is. */
12871 insn = (struct mips_opcode *) hash_find (hash, name);
ee5734f0 12872 if (insn)
14daeee3
RS
12873 return insn;
12874
12875 dot = strchr (name, '.');
12876 if (dot && dot[1])
12877 {
12878 /* Try to interpret the text after the dot as a VU0 channel suffix. */
12879 p = mips_parse_vu0_channels (dot + 1, &mask);
12880 if (*p == 0 && mask != 0)
12881 {
12882 *dot = 0;
12883 insn = (struct mips_opcode *) hash_find (hash, name);
12884 *dot = '.';
12885 if (insn && (insn->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0)
12886 {
12887 *opcode_extra |= mask << mips_vu0_channel_mask.lsb;
12888 return insn;
12889 }
12890 }
12891 }
12892
12893 if (mips_opts.micromips)
12894 {
12895 /* See if there's an instruction size override suffix,
12896 either `16' or `32', at the end of the mnemonic proper,
12897 that defines the operation, i.e. before the first `.'
12898 character if any. Strip it and retry. */
12899 opend = dot != NULL ? dot - name : length;
12900 if (opend >= 3 && name[opend - 2] == '1' && name[opend - 1] == '6')
12901 suffix = 2;
12902 else if (name[opend - 2] == '3' && name[opend - 1] == '2')
12903 suffix = 4;
12904 else
12905 suffix = 0;
12906 if (suffix)
12907 {
12908 memcpy (name + opend - 2, name + opend, length - opend + 1);
12909 insn = (struct mips_opcode *) hash_find (hash, name);
ee5734f0 12910 if (insn)
14daeee3
RS
12911 {
12912 forced_insn_length = suffix;
12913 return insn;
12914 }
12915 }
12916 }
12917
12918 return NULL;
12919}
12920
77bd4346 12921/* Assemble an instruction into its binary format. If the instruction
e423441d
RS
12922 is a macro, set imm_expr and offset_expr to the values associated
12923 with "I" and "A" operands respectively. Otherwise store the value
12924 of the relocatable field (if any) in offset_expr. In both cases
12925 set offset_reloc to the relocation operators applied to offset_expr. */
252b5132
RH
12926
12927static void
60f20e8b 12928mips_ip (char *str, struct mips_cl_insn *insn)
252b5132 12929{
60f20e8b 12930 const struct mips_opcode *first, *past;
df58fc94 12931 struct hash_control *hash;
a92713e6 12932 char format;
14daeee3 12933 size_t end;
a92713e6 12934 struct mips_operand_token *tokens;
14daeee3 12935 unsigned int opcode_extra;
252b5132 12936
df58fc94
RS
12937 if (mips_opts.micromips)
12938 {
12939 hash = micromips_op_hash;
12940 past = &micromips_opcodes[bfd_micromips_num_opcodes];
12941 }
12942 else
12943 {
12944 hash = op_hash;
12945 past = &mips_opcodes[NUMOPCODES];
12946 }
12947 forced_insn_length = 0;
14daeee3 12948 opcode_extra = 0;
252b5132 12949
df58fc94 12950 /* We first try to match an instruction up to a space or to the end. */
a40bc9dd
RS
12951 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
12952 continue;
bdaaa2e1 12953
60f20e8b
RS
12954 first = mips_lookup_insn (hash, str, end, &opcode_extra);
12955 if (first == NULL)
252b5132 12956 {
e3de51ce 12957 set_insn_error (0, _("Unrecognized opcode"));
a40bc9dd 12958 return;
252b5132
RH
12959 }
12960
60f20e8b 12961 if (strcmp (first->name, "li.s") == 0)
a92713e6 12962 format = 'f';
60f20e8b 12963 else if (strcmp (first->name, "li.d") == 0)
a92713e6
RS
12964 format = 'd';
12965 else
12966 format = 0;
12967 tokens = mips_parse_arguments (str + end, format);
12968 if (!tokens)
12969 return;
12970
60f20e8b
RS
12971 if (!match_insns (insn, first, past, tokens, opcode_extra, FALSE)
12972 && !match_insns (insn, first, past, tokens, opcode_extra, TRUE))
12973 set_insn_error (0, _("Illegal operands"));
df58fc94 12974
e3de51ce 12975 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
12976}
12977
77bd4346
RS
12978/* As for mips_ip, but used when assembling MIPS16 code.
12979 Also set forced_insn_length to the resulting instruction size in
12980 bytes if the user explicitly requested a small or extended instruction. */
252b5132
RH
12981
12982static void
60f20e8b 12983mips16_ip (char *str, struct mips_cl_insn *insn)
252b5132 12984{
1a00e612 12985 char *end, *s, c;
60f20e8b 12986 struct mips_opcode *first;
a92713e6 12987 struct mips_operand_token *tokens;
252b5132 12988
df58fc94 12989 forced_insn_length = 0;
252b5132 12990
3882b010 12991 for (s = str; ISLOWER (*s); ++s)
252b5132 12992 ;
1a00e612
RS
12993 end = s;
12994 c = *end;
12995 switch (c)
252b5132
RH
12996 {
12997 case '\0':
12998 break;
12999
13000 case ' ':
1a00e612 13001 s++;
252b5132
RH
13002 break;
13003
13004 case '.':
13005 if (s[1] == 't' && s[2] == ' ')
13006 {
df58fc94 13007 forced_insn_length = 2;
252b5132
RH
13008 s += 3;
13009 break;
13010 }
13011 else if (s[1] == 'e' && s[2] == ' ')
13012 {
df58fc94 13013 forced_insn_length = 4;
252b5132
RH
13014 s += 3;
13015 break;
13016 }
13017 /* Fall through. */
13018 default:
e3de51ce 13019 set_insn_error (0, _("Unrecognized opcode"));
252b5132
RH
13020 return;
13021 }
13022
df58fc94
RS
13023 if (mips_opts.noautoextend && !forced_insn_length)
13024 forced_insn_length = 2;
252b5132 13025
1a00e612 13026 *end = 0;
60f20e8b 13027 first = (struct mips_opcode *) hash_find (mips16_op_hash, str);
1a00e612
RS
13028 *end = c;
13029
60f20e8b 13030 if (!first)
252b5132 13031 {
e3de51ce 13032 set_insn_error (0, _("Unrecognized opcode"));
252b5132
RH
13033 return;
13034 }
13035
a92713e6
RS
13036 tokens = mips_parse_arguments (s, 0);
13037 if (!tokens)
13038 return;
13039
60f20e8b
RS
13040 if (!match_mips16_insns (insn, first, tokens))
13041 set_insn_error (0, _("Illegal operands"));
252b5132 13042
e3de51ce 13043 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
13044}
13045
b886a2ab
RS
13046/* Marshal immediate value VAL for an extended MIPS16 instruction.
13047 NBITS is the number of significant bits in VAL. */
13048
13049static unsigned long
13050mips16_immed_extend (offsetT val, unsigned int nbits)
13051{
13052 int extval;
13053 if (nbits == 16)
13054 {
13055 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
13056 val &= 0x1f;
13057 }
13058 else if (nbits == 15)
13059 {
13060 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
13061 val &= 0xf;
13062 }
13063 else
13064 {
13065 extval = ((val & 0x1f) << 6) | (val & 0x20);
13066 val = 0;
13067 }
13068 return (extval << 16) | val;
13069}
13070
3ccad066
RS
13071/* Like decode_mips16_operand, but require the operand to be defined and
13072 require it to be an integer. */
13073
13074static const struct mips_int_operand *
13075mips16_immed_operand (int type, bfd_boolean extended_p)
13076{
13077 const struct mips_operand *operand;
13078
13079 operand = decode_mips16_operand (type, extended_p);
13080 if (!operand || (operand->type != OP_INT && operand->type != OP_PCREL))
13081 abort ();
13082 return (const struct mips_int_operand *) operand;
13083}
13084
13085/* Return true if SVAL fits OPERAND. RELOC is as for mips16_immed. */
13086
13087static bfd_boolean
13088mips16_immed_in_range_p (const struct mips_int_operand *operand,
13089 bfd_reloc_code_real_type reloc, offsetT sval)
13090{
13091 int min_val, max_val;
13092
13093 min_val = mips_int_operand_min (operand);
13094 max_val = mips_int_operand_max (operand);
13095 if (reloc != BFD_RELOC_UNUSED)
13096 {
13097 if (min_val < 0)
13098 sval = SEXT_16BIT (sval);
13099 else
13100 sval &= 0xffff;
13101 }
13102
13103 return (sval >= min_val
13104 && sval <= max_val
13105 && (sval & ((1 << operand->shift) - 1)) == 0);
13106}
13107
5c04167a
RS
13108/* Install immediate value VAL into MIPS16 instruction *INSN,
13109 extending it if necessary. The instruction in *INSN may
13110 already be extended.
13111
43c0598f
RS
13112 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
13113 if none. In the former case, VAL is a 16-bit number with no
13114 defined signedness.
13115
13116 TYPE is the type of the immediate field. USER_INSN_LENGTH
13117 is the length that the user requested, or 0 if none. */
252b5132
RH
13118
13119static void
43c0598f
RS
13120mips16_immed (char *file, unsigned int line, int type,
13121 bfd_reloc_code_real_type reloc, offsetT val,
5c04167a 13122 unsigned int user_insn_length, unsigned long *insn)
252b5132 13123{
3ccad066
RS
13124 const struct mips_int_operand *operand;
13125 unsigned int uval, length;
252b5132 13126
3ccad066
RS
13127 operand = mips16_immed_operand (type, FALSE);
13128 if (!mips16_immed_in_range_p (operand, reloc, val))
5c04167a
RS
13129 {
13130 /* We need an extended instruction. */
13131 if (user_insn_length == 2)
13132 as_bad_where (file, line, _("invalid unextended operand value"));
13133 else
13134 *insn |= MIPS16_EXTEND;
13135 }
13136 else if (user_insn_length == 4)
13137 {
13138 /* The operand doesn't force an unextended instruction to be extended.
13139 Warn if the user wanted an extended instruction anyway. */
13140 *insn |= MIPS16_EXTEND;
13141 as_warn_where (file, line,
13142 _("extended operand requested but not required"));
13143 }
252b5132 13144
3ccad066
RS
13145 length = mips16_opcode_length (*insn);
13146 if (length == 4)
252b5132 13147 {
3ccad066
RS
13148 operand = mips16_immed_operand (type, TRUE);
13149 if (!mips16_immed_in_range_p (operand, reloc, val))
13150 as_bad_where (file, line,
13151 _("operand value out of range for instruction"));
252b5132 13152 }
3ccad066
RS
13153 uval = ((unsigned int) val >> operand->shift) - operand->bias;
13154 if (length == 2)
13155 *insn = mips_insert_operand (&operand->root, *insn, uval);
252b5132 13156 else
3ccad066 13157 *insn |= mips16_immed_extend (uval, operand->root.size);
252b5132
RH
13158}
13159\f
d6f16593 13160struct percent_op_match
ad8d3bb3 13161{
5e0116d5
RS
13162 const char *str;
13163 bfd_reloc_code_real_type reloc;
d6f16593
MR
13164};
13165
13166static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 13167{
5e0116d5 13168 {"%lo", BFD_RELOC_LO16},
5e0116d5
RS
13169 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
13170 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
13171 {"%call16", BFD_RELOC_MIPS_CALL16},
13172 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
13173 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
13174 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
13175 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
13176 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
13177 {"%got", BFD_RELOC_MIPS_GOT16},
13178 {"%gp_rel", BFD_RELOC_GPREL16},
13179 {"%half", BFD_RELOC_16},
13180 {"%highest", BFD_RELOC_MIPS_HIGHEST},
13181 {"%higher", BFD_RELOC_MIPS_HIGHER},
13182 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
13183 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
13184 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
13185 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
13186 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
13187 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
13188 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
13189 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
5e0116d5 13190 {"%hi", BFD_RELOC_HI16_S}
ad8d3bb3
TS
13191};
13192
d6f16593
MR
13193static const struct percent_op_match mips16_percent_op[] =
13194{
13195 {"%lo", BFD_RELOC_MIPS16_LO16},
13196 {"%gprel", BFD_RELOC_MIPS16_GPREL},
738e5348
RS
13197 {"%got", BFD_RELOC_MIPS16_GOT16},
13198 {"%call16", BFD_RELOC_MIPS16_CALL16},
d0f13682
CLT
13199 {"%hi", BFD_RELOC_MIPS16_HI16_S},
13200 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
13201 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
13202 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
13203 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
13204 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
13205 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
13206 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
d6f16593
MR
13207};
13208
252b5132 13209
5e0116d5
RS
13210/* Return true if *STR points to a relocation operator. When returning true,
13211 move *STR over the operator and store its relocation code in *RELOC.
13212 Leave both *STR and *RELOC alone when returning false. */
13213
13214static bfd_boolean
17a2f251 13215parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 13216{
d6f16593
MR
13217 const struct percent_op_match *percent_op;
13218 size_t limit, i;
13219
13220 if (mips_opts.mips16)
13221 {
13222 percent_op = mips16_percent_op;
13223 limit = ARRAY_SIZE (mips16_percent_op);
13224 }
13225 else
13226 {
13227 percent_op = mips_percent_op;
13228 limit = ARRAY_SIZE (mips_percent_op);
13229 }
76b3015f 13230
d6f16593 13231 for (i = 0; i < limit; i++)
5e0116d5 13232 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 13233 {
3f98094e
DJ
13234 int len = strlen (percent_op[i].str);
13235
13236 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
13237 continue;
13238
5e0116d5
RS
13239 *str += strlen (percent_op[i].str);
13240 *reloc = percent_op[i].reloc;
394f9b3a 13241
5e0116d5
RS
13242 /* Check whether the output BFD supports this relocation.
13243 If not, issue an error and fall back on something safe. */
13244 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 13245 {
20203fb9 13246 as_bad (_("relocation %s isn't supported by the current ABI"),
5e0116d5 13247 percent_op[i].str);
01a3f561 13248 *reloc = BFD_RELOC_UNUSED;
394f9b3a 13249 }
5e0116d5 13250 return TRUE;
394f9b3a 13251 }
5e0116d5 13252 return FALSE;
394f9b3a 13253}
ad8d3bb3 13254
ad8d3bb3 13255
5e0116d5
RS
13256/* Parse string STR as a 16-bit relocatable operand. Store the
13257 expression in *EP and the relocations in the array starting
13258 at RELOC. Return the number of relocation operators used.
ad8d3bb3 13259
01a3f561 13260 On exit, EXPR_END points to the first character after the expression. */
ad8d3bb3 13261
5e0116d5 13262static size_t
17a2f251
TS
13263my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
13264 char *str)
ad8d3bb3 13265{
5e0116d5
RS
13266 bfd_reloc_code_real_type reversed_reloc[3];
13267 size_t reloc_index, i;
09b8f35a
RS
13268 int crux_depth, str_depth;
13269 char *crux;
5e0116d5
RS
13270
13271 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
13272 in REVERSED_RELOC. End the loop with CRUX pointing to the start
13273 of the main expression and with CRUX_DEPTH containing the number
13274 of open brackets at that point. */
13275 reloc_index = -1;
13276 str_depth = 0;
13277 do
fb1b3232 13278 {
09b8f35a
RS
13279 reloc_index++;
13280 crux = str;
13281 crux_depth = str_depth;
13282
13283 /* Skip over whitespace and brackets, keeping count of the number
13284 of brackets. */
13285 while (*str == ' ' || *str == '\t' || *str == '(')
13286 if (*str++ == '(')
13287 str_depth++;
5e0116d5 13288 }
09b8f35a
RS
13289 while (*str == '%'
13290 && reloc_index < (HAVE_NEWABI ? 3 : 1)
13291 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 13292
09b8f35a 13293 my_getExpression (ep, crux);
5e0116d5 13294 str = expr_end;
394f9b3a 13295
5e0116d5 13296 /* Match every open bracket. */
09b8f35a 13297 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 13298 if (*str++ == ')')
09b8f35a 13299 crux_depth--;
394f9b3a 13300
09b8f35a 13301 if (crux_depth > 0)
20203fb9 13302 as_bad (_("unclosed '('"));
394f9b3a 13303
5e0116d5 13304 expr_end = str;
252b5132 13305
01a3f561 13306 if (reloc_index != 0)
64bdfcaf
RS
13307 {
13308 prev_reloc_op_frag = frag_now;
13309 for (i = 0; i < reloc_index; i++)
13310 reloc[i] = reversed_reloc[reloc_index - 1 - i];
13311 }
fb1b3232 13312
5e0116d5 13313 return reloc_index;
252b5132
RH
13314}
13315
13316static void
17a2f251 13317my_getExpression (expressionS *ep, char *str)
252b5132
RH
13318{
13319 char *save_in;
13320
13321 save_in = input_line_pointer;
13322 input_line_pointer = str;
13323 expression (ep);
13324 expr_end = input_line_pointer;
13325 input_line_pointer = save_in;
252b5132
RH
13326}
13327
252b5132 13328char *
17a2f251 13329md_atof (int type, char *litP, int *sizeP)
252b5132 13330{
499ac353 13331 return ieee_md_atof (type, litP, sizeP, target_big_endian);
252b5132
RH
13332}
13333
13334void
17a2f251 13335md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
13336{
13337 if (target_big_endian)
13338 number_to_chars_bigendian (buf, val, n);
13339 else
13340 number_to_chars_littleendian (buf, val, n);
13341}
13342\f
e013f690
TS
13343static int support_64bit_objects(void)
13344{
13345 const char **list, **l;
aa3d8fdf 13346 int yes;
e013f690
TS
13347
13348 list = bfd_target_list ();
13349 for (l = list; *l != NULL; l++)
aeffff67
RS
13350 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
13351 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
e013f690 13352 break;
aa3d8fdf 13353 yes = (*l != NULL);
e013f690 13354 free (list);
aa3d8fdf 13355 return yes;
e013f690
TS
13356}
13357
316f5878
RS
13358/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
13359 NEW_VALUE. Warn if another value was already specified. Note:
13360 we have to defer parsing the -march and -mtune arguments in order
13361 to handle 'from-abi' correctly, since the ABI might be specified
13362 in a later argument. */
13363
13364static void
17a2f251 13365mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
13366{
13367 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
13368 as_warn (_("A different %s was already specified, is now %s"),
13369 string_ptr == &mips_arch_string ? "-march" : "-mtune",
13370 new_value);
13371
13372 *string_ptr = new_value;
13373}
13374
252b5132 13375int
17a2f251 13376md_parse_option (int c, char *arg)
252b5132 13377{
c6278170
RS
13378 unsigned int i;
13379
13380 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
13381 if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
13382 {
13383 file_ase_explicit |= mips_set_ase (&mips_ases[i],
13384 c == mips_ases[i].option_on);
13385 return 1;
13386 }
13387
252b5132
RH
13388 switch (c)
13389 {
119d663a
NC
13390 case OPTION_CONSTRUCT_FLOATS:
13391 mips_disable_float_construction = 0;
13392 break;
bdaaa2e1 13393
119d663a
NC
13394 case OPTION_NO_CONSTRUCT_FLOATS:
13395 mips_disable_float_construction = 1;
13396 break;
bdaaa2e1 13397
252b5132
RH
13398 case OPTION_TRAP:
13399 mips_trap = 1;
13400 break;
13401
13402 case OPTION_BREAK:
13403 mips_trap = 0;
13404 break;
13405
13406 case OPTION_EB:
13407 target_big_endian = 1;
13408 break;
13409
13410 case OPTION_EL:
13411 target_big_endian = 0;
13412 break;
13413
13414 case 'O':
4ffff32f
TS
13415 if (arg == NULL)
13416 mips_optimize = 1;
13417 else if (arg[0] == '0')
13418 mips_optimize = 0;
13419 else if (arg[0] == '1')
252b5132
RH
13420 mips_optimize = 1;
13421 else
13422 mips_optimize = 2;
13423 break;
13424
13425 case 'g':
13426 if (arg == NULL)
13427 mips_debug = 2;
13428 else
13429 mips_debug = atoi (arg);
252b5132
RH
13430 break;
13431
13432 case OPTION_MIPS1:
316f5878 13433 file_mips_isa = ISA_MIPS1;
252b5132
RH
13434 break;
13435
13436 case OPTION_MIPS2:
316f5878 13437 file_mips_isa = ISA_MIPS2;
252b5132
RH
13438 break;
13439
13440 case OPTION_MIPS3:
316f5878 13441 file_mips_isa = ISA_MIPS3;
252b5132
RH
13442 break;
13443
13444 case OPTION_MIPS4:
316f5878 13445 file_mips_isa = ISA_MIPS4;
e7af610e
NC
13446 break;
13447
84ea6cf2 13448 case OPTION_MIPS5:
316f5878 13449 file_mips_isa = ISA_MIPS5;
84ea6cf2
NC
13450 break;
13451
e7af610e 13452 case OPTION_MIPS32:
316f5878 13453 file_mips_isa = ISA_MIPS32;
252b5132
RH
13454 break;
13455
af7ee8bf
CD
13456 case OPTION_MIPS32R2:
13457 file_mips_isa = ISA_MIPS32R2;
13458 break;
13459
5f74bc13
CD
13460 case OPTION_MIPS64R2:
13461 file_mips_isa = ISA_MIPS64R2;
13462 break;
13463
84ea6cf2 13464 case OPTION_MIPS64:
316f5878 13465 file_mips_isa = ISA_MIPS64;
84ea6cf2
NC
13466 break;
13467
ec68c924 13468 case OPTION_MTUNE:
316f5878
RS
13469 mips_set_option_string (&mips_tune_string, arg);
13470 break;
ec68c924 13471
316f5878
RS
13472 case OPTION_MARCH:
13473 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
13474 break;
13475
13476 case OPTION_M4650:
316f5878
RS
13477 mips_set_option_string (&mips_arch_string, "4650");
13478 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
13479 break;
13480
13481 case OPTION_NO_M4650:
13482 break;
13483
13484 case OPTION_M4010:
316f5878
RS
13485 mips_set_option_string (&mips_arch_string, "4010");
13486 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
13487 break;
13488
13489 case OPTION_NO_M4010:
13490 break;
13491
13492 case OPTION_M4100:
316f5878
RS
13493 mips_set_option_string (&mips_arch_string, "4100");
13494 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
13495 break;
13496
13497 case OPTION_NO_M4100:
13498 break;
13499
252b5132 13500 case OPTION_M3900:
316f5878
RS
13501 mips_set_option_string (&mips_arch_string, "3900");
13502 mips_set_option_string (&mips_tune_string, "3900");
252b5132 13503 break;
bdaaa2e1 13504
252b5132
RH
13505 case OPTION_NO_M3900:
13506 break;
13507
df58fc94
RS
13508 case OPTION_MICROMIPS:
13509 if (mips_opts.mips16 == 1)
13510 {
13511 as_bad (_("-mmicromips cannot be used with -mips16"));
13512 return 0;
13513 }
13514 mips_opts.micromips = 1;
13515 mips_no_prev_insn ();
13516 break;
13517
13518 case OPTION_NO_MICROMIPS:
13519 mips_opts.micromips = 0;
13520 mips_no_prev_insn ();
13521 break;
13522
252b5132 13523 case OPTION_MIPS16:
df58fc94
RS
13524 if (mips_opts.micromips == 1)
13525 {
13526 as_bad (_("-mips16 cannot be used with -micromips"));
13527 return 0;
13528 }
252b5132 13529 mips_opts.mips16 = 1;
7d10b47d 13530 mips_no_prev_insn ();
252b5132
RH
13531 break;
13532
13533 case OPTION_NO_MIPS16:
13534 mips_opts.mips16 = 0;
7d10b47d 13535 mips_no_prev_insn ();
252b5132
RH
13536 break;
13537
6a32d874
CM
13538 case OPTION_FIX_24K:
13539 mips_fix_24k = 1;
13540 break;
13541
13542 case OPTION_NO_FIX_24K:
13543 mips_fix_24k = 0;
13544 break;
13545
c67a084a
NC
13546 case OPTION_FIX_LOONGSON2F_JUMP:
13547 mips_fix_loongson2f_jump = TRUE;
13548 break;
13549
13550 case OPTION_NO_FIX_LOONGSON2F_JUMP:
13551 mips_fix_loongson2f_jump = FALSE;
13552 break;
13553
13554 case OPTION_FIX_LOONGSON2F_NOP:
13555 mips_fix_loongson2f_nop = TRUE;
13556 break;
13557
13558 case OPTION_NO_FIX_LOONGSON2F_NOP:
13559 mips_fix_loongson2f_nop = FALSE;
13560 break;
13561
d766e8ec
RS
13562 case OPTION_FIX_VR4120:
13563 mips_fix_vr4120 = 1;
60b63b72
RS
13564 break;
13565
d766e8ec
RS
13566 case OPTION_NO_FIX_VR4120:
13567 mips_fix_vr4120 = 0;
60b63b72
RS
13568 break;
13569
7d8e00cf
RS
13570 case OPTION_FIX_VR4130:
13571 mips_fix_vr4130 = 1;
13572 break;
13573
13574 case OPTION_NO_FIX_VR4130:
13575 mips_fix_vr4130 = 0;
13576 break;
13577
d954098f
DD
13578 case OPTION_FIX_CN63XXP1:
13579 mips_fix_cn63xxp1 = TRUE;
13580 break;
13581
13582 case OPTION_NO_FIX_CN63XXP1:
13583 mips_fix_cn63xxp1 = FALSE;
13584 break;
13585
4a6a3df4
AO
13586 case OPTION_RELAX_BRANCH:
13587 mips_relax_branch = 1;
13588 break;
13589
13590 case OPTION_NO_RELAX_BRANCH:
13591 mips_relax_branch = 0;
13592 break;
13593
833794fc
MR
13594 case OPTION_INSN32:
13595 mips_opts.insn32 = TRUE;
13596 break;
13597
13598 case OPTION_NO_INSN32:
13599 mips_opts.insn32 = FALSE;
13600 break;
13601
aa6975fb
ILT
13602 case OPTION_MSHARED:
13603 mips_in_shared = TRUE;
13604 break;
13605
13606 case OPTION_MNO_SHARED:
13607 mips_in_shared = FALSE;
13608 break;
13609
aed1a261
RS
13610 case OPTION_MSYM32:
13611 mips_opts.sym32 = TRUE;
13612 break;
13613
13614 case OPTION_MNO_SYM32:
13615 mips_opts.sym32 = FALSE;
13616 break;
13617
252b5132
RH
13618 /* When generating ELF code, we permit -KPIC and -call_shared to
13619 select SVR4_PIC, and -non_shared to select no PIC. This is
13620 intended to be compatible with Irix 5. */
13621 case OPTION_CALL_SHARED:
252b5132 13622 mips_pic = SVR4_PIC;
143d77c5 13623 mips_abicalls = TRUE;
252b5132
RH
13624 break;
13625
861fb55a 13626 case OPTION_CALL_NONPIC:
861fb55a
DJ
13627 mips_pic = NO_PIC;
13628 mips_abicalls = TRUE;
13629 break;
13630
252b5132 13631 case OPTION_NON_SHARED:
252b5132 13632 mips_pic = NO_PIC;
143d77c5 13633 mips_abicalls = FALSE;
252b5132
RH
13634 break;
13635
44075ae2
TS
13636 /* The -xgot option tells the assembler to use 32 bit offsets
13637 when accessing the got in SVR4_PIC mode. It is for Irix
252b5132
RH
13638 compatibility. */
13639 case OPTION_XGOT:
13640 mips_big_got = 1;
13641 break;
13642
13643 case 'G':
6caf9ef4
TS
13644 g_switch_value = atoi (arg);
13645 g_switch_seen = 1;
252b5132
RH
13646 break;
13647
34ba82a8
TS
13648 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
13649 and -mabi=64. */
252b5132 13650 case OPTION_32:
f3ded42a 13651 mips_abi = O32_ABI;
252b5132
RH
13652 break;
13653
e013f690 13654 case OPTION_N32:
316f5878 13655 mips_abi = N32_ABI;
e013f690 13656 break;
252b5132 13657
e013f690 13658 case OPTION_64:
316f5878 13659 mips_abi = N64_ABI;
f43abd2b 13660 if (!support_64bit_objects())
e013f690 13661 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132
RH
13662 break;
13663
c97ef257 13664 case OPTION_GP32:
a325df1d 13665 file_mips_gp32 = 1;
c97ef257
AH
13666 break;
13667
13668 case OPTION_GP64:
a325df1d 13669 file_mips_gp32 = 0;
c97ef257 13670 break;
252b5132 13671
ca4e0257 13672 case OPTION_FP32:
a325df1d 13673 file_mips_fp32 = 1;
316f5878
RS
13674 break;
13675
13676 case OPTION_FP64:
13677 file_mips_fp32 = 0;
ca4e0257
RS
13678 break;
13679
037b32b9
AN
13680 case OPTION_SINGLE_FLOAT:
13681 file_mips_single_float = 1;
13682 break;
13683
13684 case OPTION_DOUBLE_FLOAT:
13685 file_mips_single_float = 0;
13686 break;
13687
13688 case OPTION_SOFT_FLOAT:
13689 file_mips_soft_float = 1;
13690 break;
13691
13692 case OPTION_HARD_FLOAT:
13693 file_mips_soft_float = 0;
13694 break;
13695
252b5132 13696 case OPTION_MABI:
e013f690 13697 if (strcmp (arg, "32") == 0)
316f5878 13698 mips_abi = O32_ABI;
e013f690 13699 else if (strcmp (arg, "o64") == 0)
316f5878 13700 mips_abi = O64_ABI;
e013f690 13701 else if (strcmp (arg, "n32") == 0)
316f5878 13702 mips_abi = N32_ABI;
e013f690
TS
13703 else if (strcmp (arg, "64") == 0)
13704 {
316f5878 13705 mips_abi = N64_ABI;
e013f690
TS
13706 if (! support_64bit_objects())
13707 as_fatal (_("No compiled in support for 64 bit object file "
13708 "format"));
13709 }
13710 else if (strcmp (arg, "eabi") == 0)
316f5878 13711 mips_abi = EABI_ABI;
e013f690 13712 else
da0e507f
TS
13713 {
13714 as_fatal (_("invalid abi -mabi=%s"), arg);
13715 return 0;
13716 }
252b5132
RH
13717 break;
13718
6b76fefe 13719 case OPTION_M7000_HILO_FIX:
b34976b6 13720 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
13721 break;
13722
9ee72ff1 13723 case OPTION_MNO_7000_HILO_FIX:
b34976b6 13724 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
13725 break;
13726
ecb4347a 13727 case OPTION_MDEBUG:
b34976b6 13728 mips_flag_mdebug = TRUE;
ecb4347a
DJ
13729 break;
13730
13731 case OPTION_NO_MDEBUG:
b34976b6 13732 mips_flag_mdebug = FALSE;
ecb4347a 13733 break;
dcd410fe
RO
13734
13735 case OPTION_PDR:
13736 mips_flag_pdr = TRUE;
13737 break;
13738
13739 case OPTION_NO_PDR:
13740 mips_flag_pdr = FALSE;
13741 break;
0a44bf69
RS
13742
13743 case OPTION_MVXWORKS_PIC:
13744 mips_pic = VXWORKS_PIC;
13745 break;
ecb4347a 13746
ba92f887
MR
13747 case OPTION_NAN:
13748 if (strcmp (arg, "2008") == 0)
13749 mips_flag_nan2008 = TRUE;
13750 else if (strcmp (arg, "legacy") == 0)
13751 mips_flag_nan2008 = FALSE;
13752 else
13753 {
13754 as_fatal (_("Invalid NaN setting -mnan=%s"), arg);
13755 return 0;
13756 }
13757 break;
13758
252b5132
RH
13759 default:
13760 return 0;
13761 }
13762
c67a084a
NC
13763 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
13764
252b5132
RH
13765 return 1;
13766}
316f5878
RS
13767\f
13768/* Set up globals to generate code for the ISA or processor
13769 described by INFO. */
252b5132 13770
252b5132 13771static void
17a2f251 13772mips_set_architecture (const struct mips_cpu_info *info)
252b5132 13773{
316f5878 13774 if (info != 0)
252b5132 13775 {
fef14a42
TS
13776 file_mips_arch = info->cpu;
13777 mips_opts.arch = info->cpu;
316f5878 13778 mips_opts.isa = info->isa;
252b5132 13779 }
252b5132
RH
13780}
13781
252b5132 13782
316f5878 13783/* Likewise for tuning. */
252b5132 13784
316f5878 13785static void
17a2f251 13786mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
13787{
13788 if (info != 0)
fef14a42 13789 mips_tune = info->cpu;
316f5878 13790}
80cc45a5 13791
34ba82a8 13792
252b5132 13793void
17a2f251 13794mips_after_parse_args (void)
e9670677 13795{
fef14a42
TS
13796 const struct mips_cpu_info *arch_info = 0;
13797 const struct mips_cpu_info *tune_info = 0;
13798
e9670677 13799 /* GP relative stuff not working for PE */
6caf9ef4 13800 if (strncmp (TARGET_OS, "pe", 2) == 0)
e9670677 13801 {
6caf9ef4 13802 if (g_switch_seen && g_switch_value != 0)
e9670677
MR
13803 as_bad (_("-G not supported in this configuration."));
13804 g_switch_value = 0;
13805 }
13806
cac012d6
AO
13807 if (mips_abi == NO_ABI)
13808 mips_abi = MIPS_DEFAULT_ABI;
13809
22923709
RS
13810 /* The following code determines the architecture and register size.
13811 Similar code was added to GCC 3.3 (see override_options() in
13812 config/mips/mips.c). The GAS and GCC code should be kept in sync
13813 as much as possible. */
e9670677 13814
316f5878 13815 if (mips_arch_string != 0)
fef14a42 13816 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 13817
316f5878 13818 if (file_mips_isa != ISA_UNKNOWN)
e9670677 13819 {
316f5878 13820 /* Handle -mipsN. At this point, file_mips_isa contains the
fef14a42 13821 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 13822 the -march selection (if any). */
fef14a42 13823 if (arch_info != 0)
e9670677 13824 {
316f5878
RS
13825 /* -march takes precedence over -mipsN, since it is more descriptive.
13826 There's no harm in specifying both as long as the ISA levels
13827 are the same. */
fef14a42 13828 if (file_mips_isa != arch_info->isa)
316f5878
RS
13829 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
13830 mips_cpu_info_from_isa (file_mips_isa)->name,
fef14a42 13831 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 13832 }
316f5878 13833 else
fef14a42 13834 arch_info = mips_cpu_info_from_isa (file_mips_isa);
e9670677
MR
13835 }
13836
fef14a42 13837 if (arch_info == 0)
95bfe26e
MF
13838 {
13839 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
13840 gas_assert (arch_info);
13841 }
e9670677 13842
fef14a42 13843 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
20203fb9 13844 as_bad (_("-march=%s is not compatible with the selected ABI"),
fef14a42
TS
13845 arch_info->name);
13846
13847 mips_set_architecture (arch_info);
13848
13849 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
13850 if (mips_tune_string != 0)
13851 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 13852
fef14a42
TS
13853 if (tune_info == 0)
13854 mips_set_tune (arch_info);
13855 else
13856 mips_set_tune (tune_info);
e9670677 13857
316f5878 13858 if (file_mips_gp32 >= 0)
e9670677 13859 {
316f5878
RS
13860 /* The user specified the size of the integer registers. Make sure
13861 it agrees with the ABI and ISA. */
13862 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
13863 as_bad (_("-mgp64 used with a 32-bit processor"));
13864 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
13865 as_bad (_("-mgp32 used with a 64-bit ABI"));
13866 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
13867 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
13868 }
13869 else
13870 {
316f5878
RS
13871 /* Infer the integer register size from the ABI and processor.
13872 Restrict ourselves to 32-bit registers if that's all the
13873 processor has, or if the ABI cannot handle 64-bit registers. */
13874 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
13875 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
e9670677
MR
13876 }
13877
ad3fea08
TS
13878 switch (file_mips_fp32)
13879 {
13880 default:
13881 case -1:
13882 /* No user specified float register size.
13883 ??? GAS treats single-float processors as though they had 64-bit
13884 float registers (although it complains when double-precision
13885 instructions are used). As things stand, saying they have 32-bit
13886 registers would lead to spurious "register must be even" messages.
13887 So here we assume float registers are never smaller than the
13888 integer ones. */
13889 if (file_mips_gp32 == 0)
13890 /* 64-bit integer registers implies 64-bit float registers. */
13891 file_mips_fp32 = 0;
c6278170 13892 else if ((mips_opts.ase & FP64_ASES)
ad3fea08
TS
13893 && ISA_HAS_64BIT_FPRS (mips_opts.isa))
13894 /* -mips3d and -mdmx imply 64-bit float registers, if possible. */
13895 file_mips_fp32 = 0;
13896 else
13897 /* 32-bit float registers. */
13898 file_mips_fp32 = 1;
13899 break;
13900
13901 /* The user specified the size of the float registers. Check if it
13902 agrees with the ABI and ISA. */
13903 case 0:
13904 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
13905 as_bad (_("-mfp64 used with a 32-bit fpu"));
13906 else if (ABI_NEEDS_32BIT_REGS (mips_abi)
13907 && !ISA_HAS_MXHC1 (mips_opts.isa))
13908 as_warn (_("-mfp64 used with a 32-bit ABI"));
13909 break;
13910 case 1:
13911 if (ABI_NEEDS_64BIT_REGS (mips_abi))
13912 as_warn (_("-mfp32 used with a 64-bit ABI"));
13913 break;
13914 }
e9670677 13915
316f5878 13916 /* End of GCC-shared inference code. */
e9670677 13917
17a2f251
TS
13918 /* This flag is set when we have a 64-bit capable CPU but use only
13919 32-bit wide registers. Note that EABI does not use it. */
13920 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
13921 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
13922 || mips_abi == O32_ABI))
316f5878 13923 mips_32bitmode = 1;
e9670677
MR
13924
13925 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
13926 as_bad (_("trap exception not supported at ISA 1"));
13927
e9670677
MR
13928 /* If the selected architecture includes support for ASEs, enable
13929 generation of code for them. */
a4672219 13930 if (mips_opts.mips16 == -1)
fef14a42 13931 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
df58fc94
RS
13932 if (mips_opts.micromips == -1)
13933 mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_arch)) ? 1 : 0;
846ef2d0
RS
13934
13935 /* MIPS3D and MDMX require 64-bit FPRs, so -mfp32 should stop those
13936 ASEs from being selected implicitly. */
13937 if (file_mips_fp32 == 1)
13938 file_ase_explicit |= ASE_MIPS3D | ASE_MDMX;
13939
13940 /* If the user didn't explicitly select or deselect a particular ASE,
13941 use the default setting for the CPU. */
13942 mips_opts.ase |= (arch_info->ase & ~file_ase_explicit);
13943
e9670677 13944 file_mips_isa = mips_opts.isa;
846ef2d0 13945 file_ase = mips_opts.ase;
e9670677
MR
13946 mips_opts.gp32 = file_mips_gp32;
13947 mips_opts.fp32 = file_mips_fp32;
037b32b9
AN
13948 mips_opts.soft_float = file_mips_soft_float;
13949 mips_opts.single_float = file_mips_single_float;
e9670677 13950
c6278170
RS
13951 mips_check_isa_supports_ases ();
13952
ecb4347a 13953 if (mips_flag_mdebug < 0)
e8044f35 13954 mips_flag_mdebug = 0;
e9670677
MR
13955}
13956\f
13957void
17a2f251 13958mips_init_after_args (void)
252b5132
RH
13959{
13960 /* initialize opcodes */
13961 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 13962 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
13963}
13964
13965long
17a2f251 13966md_pcrel_from (fixS *fixP)
252b5132 13967{
a7ebbfdf
TS
13968 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
13969 switch (fixP->fx_r_type)
13970 {
df58fc94
RS
13971 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
13972 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
13973 /* Return the address of the delay slot. */
13974 return addr + 2;
13975
13976 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
13977 case BFD_RELOC_MICROMIPS_JMP:
a7ebbfdf
TS
13978 case BFD_RELOC_16_PCREL_S2:
13979 case BFD_RELOC_MIPS_JMP:
13980 /* Return the address of the delay slot. */
13981 return addr + 4;
df58fc94 13982
b47468a6
CM
13983 case BFD_RELOC_32_PCREL:
13984 return addr;
13985
a7ebbfdf 13986 default:
58ea3d6a 13987 /* We have no relocation type for PC relative MIPS16 instructions. */
64817874
TS
13988 if (fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != now_seg)
13989 as_bad_where (fixP->fx_file, fixP->fx_line,
13990 _("PC relative MIPS16 instruction references a different section"));
a7ebbfdf
TS
13991 return addr;
13992 }
252b5132
RH
13993}
13994
252b5132
RH
13995/* This is called before the symbol table is processed. In order to
13996 work with gcc when using mips-tfile, we must keep all local labels.
13997 However, in other cases, we want to discard them. If we were
13998 called with -g, but we didn't see any debugging information, it may
13999 mean that gcc is smuggling debugging information through to
14000 mips-tfile, in which case we must generate all local labels. */
14001
14002void
17a2f251 14003mips_frob_file_before_adjust (void)
252b5132
RH
14004{
14005#ifndef NO_ECOFF_DEBUGGING
14006 if (ECOFF_DEBUGGING
14007 && mips_debug != 0
14008 && ! ecoff_debugging_seen)
14009 flag_keep_locals = 1;
14010#endif
14011}
14012
3b91255e 14013/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 14014 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
14015 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
14016 relocation operators.
14017
14018 For our purposes, a %lo() expression matches a %got() or %hi()
14019 expression if:
14020
14021 (a) it refers to the same symbol; and
14022 (b) the offset applied in the %lo() expression is no lower than
14023 the offset applied in the %got() or %hi().
14024
14025 (b) allows us to cope with code like:
14026
14027 lui $4,%hi(foo)
14028 lh $4,%lo(foo+2)($4)
14029
14030 ...which is legal on RELA targets, and has a well-defined behaviour
14031 if the user knows that adding 2 to "foo" will not induce a carry to
14032 the high 16 bits.
14033
14034 When several %lo()s match a particular %got() or %hi(), we use the
14035 following rules to distinguish them:
14036
14037 (1) %lo()s with smaller offsets are a better match than %lo()s with
14038 higher offsets.
14039
14040 (2) %lo()s with no matching %got() or %hi() are better than those
14041 that already have a matching %got() or %hi().
14042
14043 (3) later %lo()s are better than earlier %lo()s.
14044
14045 These rules are applied in order.
14046
14047 (1) means, among other things, that %lo()s with identical offsets are
14048 chosen if they exist.
14049
14050 (2) means that we won't associate several high-part relocations with
14051 the same low-part relocation unless there's no alternative. Having
14052 several high parts for the same low part is a GNU extension; this rule
14053 allows careful users to avoid it.
14054
14055 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
14056 with the last high-part relocation being at the front of the list.
14057 It therefore makes sense to choose the last matching low-part
14058 relocation, all other things being equal. It's also easier
14059 to code that way. */
252b5132
RH
14060
14061void
17a2f251 14062mips_frob_file (void)
252b5132
RH
14063{
14064 struct mips_hi_fixup *l;
35903be0 14065 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
252b5132
RH
14066
14067 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
14068 {
14069 segment_info_type *seginfo;
3b91255e
RS
14070 bfd_boolean matched_lo_p;
14071 fixS **hi_pos, **lo_pos, **pos;
252b5132 14072
9c2799c2 14073 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 14074
5919d012 14075 /* If a GOT16 relocation turns out to be against a global symbol,
b886a2ab
RS
14076 there isn't supposed to be a matching LO. Ignore %gots against
14077 constants; we'll report an error for those later. */
738e5348 14078 if (got16_reloc_p (l->fixp->fx_r_type)
b886a2ab
RS
14079 && !(l->fixp->fx_addsy
14080 && pic_need_relax (l->fixp->fx_addsy, l->seg)))
5919d012
RS
14081 continue;
14082
14083 /* Check quickly whether the next fixup happens to be a matching %lo. */
14084 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
14085 continue;
14086
252b5132 14087 seginfo = seg_info (l->seg);
252b5132 14088
3b91255e
RS
14089 /* Set HI_POS to the position of this relocation in the chain.
14090 Set LO_POS to the position of the chosen low-part relocation.
14091 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
14092 relocation that matches an immediately-preceding high-part
14093 relocation. */
14094 hi_pos = NULL;
14095 lo_pos = NULL;
14096 matched_lo_p = FALSE;
738e5348 14097 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
35903be0 14098
3b91255e
RS
14099 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
14100 {
14101 if (*pos == l->fixp)
14102 hi_pos = pos;
14103
35903be0 14104 if ((*pos)->fx_r_type == looking_for_rtype
30cfc97a 14105 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
3b91255e
RS
14106 && (*pos)->fx_offset >= l->fixp->fx_offset
14107 && (lo_pos == NULL
14108 || (*pos)->fx_offset < (*lo_pos)->fx_offset
14109 || (!matched_lo_p
14110 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
14111 lo_pos = pos;
14112
14113 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
14114 && fixup_has_matching_lo_p (*pos));
14115 }
14116
14117 /* If we found a match, remove the high-part relocation from its
14118 current position and insert it before the low-part relocation.
14119 Make the offsets match so that fixup_has_matching_lo_p()
14120 will return true.
14121
14122 We don't warn about unmatched high-part relocations since some
14123 versions of gcc have been known to emit dead "lui ...%hi(...)"
14124 instructions. */
14125 if (lo_pos != NULL)
14126 {
14127 l->fixp->fx_offset = (*lo_pos)->fx_offset;
14128 if (l->fixp->fx_next != *lo_pos)
252b5132 14129 {
3b91255e
RS
14130 *hi_pos = l->fixp->fx_next;
14131 l->fixp->fx_next = *lo_pos;
14132 *lo_pos = l->fixp;
252b5132 14133 }
252b5132
RH
14134 }
14135 }
14136}
14137
252b5132 14138int
17a2f251 14139mips_force_relocation (fixS *fixp)
252b5132 14140{
ae6063d4 14141 if (generic_force_reloc (fixp))
252b5132
RH
14142 return 1;
14143
df58fc94
RS
14144 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
14145 so that the linker relaxation can update targets. */
14146 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
14147 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
14148 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
14149 return 1;
14150
3e722fb5 14151 return 0;
252b5132
RH
14152}
14153
b886a2ab
RS
14154/* Read the instruction associated with RELOC from BUF. */
14155
14156static unsigned int
14157read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
14158{
14159 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
14160 return read_compressed_insn (buf, 4);
14161 else
14162 return read_insn (buf);
14163}
14164
14165/* Write instruction INSN to BUF, given that it has been relocated
14166 by RELOC. */
14167
14168static void
14169write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
14170 unsigned long insn)
14171{
14172 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
14173 write_compressed_insn (buf, insn, 4);
14174 else
14175 write_insn (buf, insn);
14176}
14177
252b5132
RH
14178/* Apply a fixup to the object file. */
14179
94f592af 14180void
55cf6793 14181md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 14182{
4d68580a 14183 char *buf;
b886a2ab 14184 unsigned long insn;
a7ebbfdf 14185 reloc_howto_type *howto;
252b5132 14186
a7ebbfdf
TS
14187 /* We ignore generic BFD relocations we don't know about. */
14188 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
14189 if (! howto)
14190 return;
65551fa4 14191
df58fc94
RS
14192 gas_assert (fixP->fx_size == 2
14193 || fixP->fx_size == 4
90ecf173
MR
14194 || fixP->fx_r_type == BFD_RELOC_16
14195 || fixP->fx_r_type == BFD_RELOC_64
14196 || fixP->fx_r_type == BFD_RELOC_CTOR
14197 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
df58fc94 14198 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
90ecf173
MR
14199 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
14200 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
14201 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64);
252b5132 14202
4d68580a 14203 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132 14204
df58fc94
RS
14205 gas_assert (!fixP->fx_pcrel || fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
14206 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
14207 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
b47468a6
CM
14208 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
14209 || fixP->fx_r_type == BFD_RELOC_32_PCREL);
b1dca8ee
RS
14210
14211 /* Don't treat parts of a composite relocation as done. There are two
14212 reasons for this:
14213
14214 (1) The second and third parts will be against 0 (RSS_UNDEF) but
14215 should nevertheless be emitted if the first part is.
14216
14217 (2) In normal usage, composite relocations are never assembly-time
14218 constants. The easiest way of dealing with the pathological
14219 exceptions is to generate a relocation against STN_UNDEF and
14220 leave everything up to the linker. */
3994f87e 14221 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
252b5132
RH
14222 fixP->fx_done = 1;
14223
14224 switch (fixP->fx_r_type)
14225 {
3f98094e
DJ
14226 case BFD_RELOC_MIPS_TLS_GD:
14227 case BFD_RELOC_MIPS_TLS_LDM:
741d6ea8
JM
14228 case BFD_RELOC_MIPS_TLS_DTPREL32:
14229 case BFD_RELOC_MIPS_TLS_DTPREL64:
3f98094e
DJ
14230 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
14231 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
14232 case BFD_RELOC_MIPS_TLS_GOTTPREL:
d0f13682
CLT
14233 case BFD_RELOC_MIPS_TLS_TPREL32:
14234 case BFD_RELOC_MIPS_TLS_TPREL64:
3f98094e
DJ
14235 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
14236 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
df58fc94
RS
14237 case BFD_RELOC_MICROMIPS_TLS_GD:
14238 case BFD_RELOC_MICROMIPS_TLS_LDM:
14239 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
14240 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
14241 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
14242 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
14243 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
d0f13682
CLT
14244 case BFD_RELOC_MIPS16_TLS_GD:
14245 case BFD_RELOC_MIPS16_TLS_LDM:
14246 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
14247 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
14248 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
14249 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
14250 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
b886a2ab
RS
14251 if (!fixP->fx_addsy)
14252 {
14253 as_bad_where (fixP->fx_file, fixP->fx_line,
14254 _("TLS relocation against a constant"));
14255 break;
14256 }
3f98094e
DJ
14257 S_SET_THREAD_LOCAL (fixP->fx_addsy);
14258 /* fall through */
14259
252b5132 14260 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
14261 case BFD_RELOC_MIPS_SHIFT5:
14262 case BFD_RELOC_MIPS_SHIFT6:
14263 case BFD_RELOC_MIPS_GOT_DISP:
14264 case BFD_RELOC_MIPS_GOT_PAGE:
14265 case BFD_RELOC_MIPS_GOT_OFST:
14266 case BFD_RELOC_MIPS_SUB:
14267 case BFD_RELOC_MIPS_INSERT_A:
14268 case BFD_RELOC_MIPS_INSERT_B:
14269 case BFD_RELOC_MIPS_DELETE:
14270 case BFD_RELOC_MIPS_HIGHEST:
14271 case BFD_RELOC_MIPS_HIGHER:
14272 case BFD_RELOC_MIPS_SCN_DISP:
14273 case BFD_RELOC_MIPS_REL16:
14274 case BFD_RELOC_MIPS_RELGOT:
14275 case BFD_RELOC_MIPS_JALR:
252b5132
RH
14276 case BFD_RELOC_HI16:
14277 case BFD_RELOC_HI16_S:
b886a2ab 14278 case BFD_RELOC_LO16:
cdf6fd85 14279 case BFD_RELOC_GPREL16:
252b5132
RH
14280 case BFD_RELOC_MIPS_LITERAL:
14281 case BFD_RELOC_MIPS_CALL16:
14282 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 14283 case BFD_RELOC_GPREL32:
252b5132
RH
14284 case BFD_RELOC_MIPS_GOT_HI16:
14285 case BFD_RELOC_MIPS_GOT_LO16:
14286 case BFD_RELOC_MIPS_CALL_HI16:
14287 case BFD_RELOC_MIPS_CALL_LO16:
14288 case BFD_RELOC_MIPS16_GPREL:
738e5348
RS
14289 case BFD_RELOC_MIPS16_GOT16:
14290 case BFD_RELOC_MIPS16_CALL16:
d6f16593
MR
14291 case BFD_RELOC_MIPS16_HI16:
14292 case BFD_RELOC_MIPS16_HI16_S:
b886a2ab 14293 case BFD_RELOC_MIPS16_LO16:
252b5132 14294 case BFD_RELOC_MIPS16_JMP:
df58fc94
RS
14295 case BFD_RELOC_MICROMIPS_JMP:
14296 case BFD_RELOC_MICROMIPS_GOT_DISP:
14297 case BFD_RELOC_MICROMIPS_GOT_PAGE:
14298 case BFD_RELOC_MICROMIPS_GOT_OFST:
14299 case BFD_RELOC_MICROMIPS_SUB:
14300 case BFD_RELOC_MICROMIPS_HIGHEST:
14301 case BFD_RELOC_MICROMIPS_HIGHER:
14302 case BFD_RELOC_MICROMIPS_SCN_DISP:
14303 case BFD_RELOC_MICROMIPS_JALR:
14304 case BFD_RELOC_MICROMIPS_HI16:
14305 case BFD_RELOC_MICROMIPS_HI16_S:
b886a2ab 14306 case BFD_RELOC_MICROMIPS_LO16:
df58fc94
RS
14307 case BFD_RELOC_MICROMIPS_GPREL16:
14308 case BFD_RELOC_MICROMIPS_LITERAL:
14309 case BFD_RELOC_MICROMIPS_CALL16:
14310 case BFD_RELOC_MICROMIPS_GOT16:
14311 case BFD_RELOC_MICROMIPS_GOT_HI16:
14312 case BFD_RELOC_MICROMIPS_GOT_LO16:
14313 case BFD_RELOC_MICROMIPS_CALL_HI16:
14314 case BFD_RELOC_MICROMIPS_CALL_LO16:
067ec077 14315 case BFD_RELOC_MIPS_EH:
b886a2ab
RS
14316 if (fixP->fx_done)
14317 {
14318 offsetT value;
14319
14320 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
14321 {
14322 insn = read_reloc_insn (buf, fixP->fx_r_type);
14323 if (mips16_reloc_p (fixP->fx_r_type))
14324 insn |= mips16_immed_extend (value, 16);
14325 else
14326 insn |= (value & 0xffff);
14327 write_reloc_insn (buf, fixP->fx_r_type, insn);
14328 }
14329 else
14330 as_bad_where (fixP->fx_file, fixP->fx_line,
14331 _("Unsupported constant in relocation"));
14332 }
252b5132
RH
14333 break;
14334
252b5132
RH
14335 case BFD_RELOC_64:
14336 /* This is handled like BFD_RELOC_32, but we output a sign
14337 extended value if we are only 32 bits. */
3e722fb5 14338 if (fixP->fx_done)
252b5132
RH
14339 {
14340 if (8 <= sizeof (valueT))
4d68580a 14341 md_number_to_chars (buf, *valP, 8);
252b5132
RH
14342 else
14343 {
a7ebbfdf 14344 valueT hiv;
252b5132 14345
a7ebbfdf 14346 if ((*valP & 0x80000000) != 0)
252b5132
RH
14347 hiv = 0xffffffff;
14348 else
14349 hiv = 0;
4d68580a
RS
14350 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
14351 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
252b5132
RH
14352 }
14353 }
14354 break;
14355
056350c6 14356 case BFD_RELOC_RVA:
252b5132 14357 case BFD_RELOC_32:
b47468a6 14358 case BFD_RELOC_32_PCREL:
252b5132
RH
14359 case BFD_RELOC_16:
14360 /* If we are deleting this reloc entry, we must fill in the
54f4ddb3
TS
14361 value now. This can happen if we have a .word which is not
14362 resolved when it appears but is later defined. */
252b5132 14363 if (fixP->fx_done)
4d68580a 14364 md_number_to_chars (buf, *valP, fixP->fx_size);
252b5132
RH
14365 break;
14366
252b5132 14367 case BFD_RELOC_16_PCREL_S2:
a7ebbfdf 14368 if ((*valP & 0x3) != 0)
cb56d3d3 14369 as_bad_where (fixP->fx_file, fixP->fx_line,
bad36eac 14370 _("Branch to misaligned address (%lx)"), (long) *valP);
cb56d3d3 14371
54f4ddb3
TS
14372 /* We need to save the bits in the instruction since fixup_segment()
14373 might be deleting the relocation entry (i.e., a branch within
14374 the current segment). */
a7ebbfdf 14375 if (! fixP->fx_done)
bb2d6cd7 14376 break;
252b5132 14377
54f4ddb3 14378 /* Update old instruction data. */
4d68580a 14379 insn = read_insn (buf);
252b5132 14380
a7ebbfdf
TS
14381 if (*valP + 0x20000 <= 0x3ffff)
14382 {
14383 insn |= (*valP >> 2) & 0xffff;
4d68580a 14384 write_insn (buf, insn);
a7ebbfdf
TS
14385 }
14386 else if (mips_pic == NO_PIC
14387 && fixP->fx_done
14388 && fixP->fx_frag->fr_address >= text_section->vma
14389 && (fixP->fx_frag->fr_address
587aac4e 14390 < text_section->vma + bfd_get_section_size (text_section))
a7ebbfdf
TS
14391 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
14392 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
14393 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
14394 {
14395 /* The branch offset is too large. If this is an
14396 unconditional branch, and we are not generating PIC code,
14397 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
14398 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
14399 insn = 0x0c000000; /* jal */
252b5132 14400 else
a7ebbfdf
TS
14401 insn = 0x08000000; /* j */
14402 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
14403 fixP->fx_done = 0;
14404 fixP->fx_addsy = section_symbol (text_section);
14405 *valP += md_pcrel_from (fixP);
4d68580a 14406 write_insn (buf, insn);
a7ebbfdf
TS
14407 }
14408 else
14409 {
14410 /* If we got here, we have branch-relaxation disabled,
14411 and there's nothing we can do to fix this instruction
14412 without turning it into a longer sequence. */
14413 as_bad_where (fixP->fx_file, fixP->fx_line,
14414 _("Branch out of range"));
252b5132 14415 }
252b5132
RH
14416 break;
14417
df58fc94
RS
14418 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
14419 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
14420 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
14421 /* We adjust the offset back to even. */
14422 if ((*valP & 0x1) != 0)
14423 --(*valP);
14424
14425 if (! fixP->fx_done)
14426 break;
14427
14428 /* Should never visit here, because we keep the relocation. */
14429 abort ();
14430 break;
14431
252b5132
RH
14432 case BFD_RELOC_VTABLE_INHERIT:
14433 fixP->fx_done = 0;
14434 if (fixP->fx_addsy
14435 && !S_IS_DEFINED (fixP->fx_addsy)
14436 && !S_IS_WEAK (fixP->fx_addsy))
14437 S_SET_WEAK (fixP->fx_addsy);
14438 break;
14439
14440 case BFD_RELOC_VTABLE_ENTRY:
14441 fixP->fx_done = 0;
14442 break;
14443
14444 default:
b37df7c4 14445 abort ();
252b5132 14446 }
a7ebbfdf
TS
14447
14448 /* Remember value for tc_gen_reloc. */
14449 fixP->fx_addnumber = *valP;
252b5132
RH
14450}
14451
252b5132 14452static symbolS *
17a2f251 14453get_symbol (void)
252b5132
RH
14454{
14455 int c;
14456 char *name;
14457 symbolS *p;
14458
14459 name = input_line_pointer;
14460 c = get_symbol_end ();
14461 p = (symbolS *) symbol_find_or_make (name);
14462 *input_line_pointer = c;
14463 return p;
14464}
14465
742a56fe
RS
14466/* Align the current frag to a given power of two. If a particular
14467 fill byte should be used, FILL points to an integer that contains
14468 that byte, otherwise FILL is null.
14469
462427c4
RS
14470 This function used to have the comment:
14471
14472 The MIPS assembler also automatically adjusts any preceding label.
14473
14474 The implementation therefore applied the adjustment to a maximum of
14475 one label. However, other label adjustments are applied to batches
14476 of labels, and adjusting just one caused problems when new labels
14477 were added for the sake of debugging or unwind information.
14478 We therefore adjust all preceding labels (given as LABELS) instead. */
252b5132
RH
14479
14480static void
462427c4 14481mips_align (int to, int *fill, struct insn_label_list *labels)
252b5132 14482{
7d10b47d 14483 mips_emit_delays ();
df58fc94 14484 mips_record_compressed_mode ();
742a56fe
RS
14485 if (fill == NULL && subseg_text_p (now_seg))
14486 frag_align_code (to, 0);
14487 else
14488 frag_align (to, fill ? *fill : 0, 0);
252b5132 14489 record_alignment (now_seg, to);
462427c4 14490 mips_move_labels (labels, FALSE);
252b5132
RH
14491}
14492
14493/* Align to a given power of two. .align 0 turns off the automatic
14494 alignment used by the data creating pseudo-ops. */
14495
14496static void
17a2f251 14497s_align (int x ATTRIBUTE_UNUSED)
252b5132 14498{
742a56fe 14499 int temp, fill_value, *fill_ptr;
49954fb4 14500 long max_alignment = 28;
252b5132 14501
54f4ddb3 14502 /* o Note that the assembler pulls down any immediately preceding label
252b5132 14503 to the aligned address.
54f4ddb3 14504 o It's not documented but auto alignment is reinstated by
252b5132 14505 a .align pseudo instruction.
54f4ddb3 14506 o Note also that after auto alignment is turned off the mips assembler
252b5132 14507 issues an error on attempt to assemble an improperly aligned data item.
54f4ddb3 14508 We don't. */
252b5132
RH
14509
14510 temp = get_absolute_expression ();
14511 if (temp > max_alignment)
14512 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
14513 else if (temp < 0)
14514 {
14515 as_warn (_("Alignment negative: 0 assumed."));
14516 temp = 0;
14517 }
14518 if (*input_line_pointer == ',')
14519 {
f9419b05 14520 ++input_line_pointer;
742a56fe
RS
14521 fill_value = get_absolute_expression ();
14522 fill_ptr = &fill_value;
252b5132
RH
14523 }
14524 else
742a56fe 14525 fill_ptr = 0;
252b5132
RH
14526 if (temp)
14527 {
a8dbcb85
TS
14528 segment_info_type *si = seg_info (now_seg);
14529 struct insn_label_list *l = si->label_list;
54f4ddb3 14530 /* Auto alignment should be switched on by next section change. */
252b5132 14531 auto_align = 1;
462427c4 14532 mips_align (temp, fill_ptr, l);
252b5132
RH
14533 }
14534 else
14535 {
14536 auto_align = 0;
14537 }
14538
14539 demand_empty_rest_of_line ();
14540}
14541
252b5132 14542static void
17a2f251 14543s_change_sec (int sec)
252b5132
RH
14544{
14545 segT seg;
14546
252b5132
RH
14547 /* The ELF backend needs to know that we are changing sections, so
14548 that .previous works correctly. We could do something like check
b6ff326e 14549 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
14550 as it would not be appropriate to use it in the section changing
14551 functions in read.c, since obj-elf.c intercepts those. FIXME:
14552 This should be cleaner, somehow. */
f3ded42a 14553 obj_elf_section_change_hook ();
252b5132 14554
7d10b47d 14555 mips_emit_delays ();
6a32d874 14556
252b5132
RH
14557 switch (sec)
14558 {
14559 case 't':
14560 s_text (0);
14561 break;
14562 case 'd':
14563 s_data (0);
14564 break;
14565 case 'b':
14566 subseg_set (bss_section, (subsegT) get_absolute_expression ());
14567 demand_empty_rest_of_line ();
14568 break;
14569
14570 case 'r':
4d0d148d
TS
14571 seg = subseg_new (RDATA_SECTION_NAME,
14572 (subsegT) get_absolute_expression ());
f3ded42a
RS
14573 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
14574 | SEC_READONLY | SEC_RELOC
14575 | SEC_DATA));
14576 if (strncmp (TARGET_OS, "elf", 3) != 0)
14577 record_alignment (seg, 4);
4d0d148d 14578 demand_empty_rest_of_line ();
252b5132
RH
14579 break;
14580
14581 case 's':
4d0d148d 14582 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
f3ded42a
RS
14583 bfd_set_section_flags (stdoutput, seg,
14584 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
14585 if (strncmp (TARGET_OS, "elf", 3) != 0)
14586 record_alignment (seg, 4);
4d0d148d
TS
14587 demand_empty_rest_of_line ();
14588 break;
998b3c36
MR
14589
14590 case 'B':
14591 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
f3ded42a
RS
14592 bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
14593 if (strncmp (TARGET_OS, "elf", 3) != 0)
14594 record_alignment (seg, 4);
998b3c36
MR
14595 demand_empty_rest_of_line ();
14596 break;
252b5132
RH
14597 }
14598
14599 auto_align = 1;
14600}
b34976b6 14601
cca86cc8 14602void
17a2f251 14603s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 14604{
cca86cc8
SC
14605 char *section_name;
14606 char c;
684022ea 14607 char next_c = 0;
cca86cc8
SC
14608 int section_type;
14609 int section_flag;
14610 int section_entry_size;
14611 int section_alignment;
b34976b6 14612
cca86cc8
SC
14613 section_name = input_line_pointer;
14614 c = get_symbol_end ();
a816d1ed
AO
14615 if (c)
14616 next_c = *(input_line_pointer + 1);
cca86cc8 14617
4cf0dd0d
TS
14618 /* Do we have .section Name<,"flags">? */
14619 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 14620 {
4cf0dd0d
TS
14621 /* just after name is now '\0'. */
14622 *input_line_pointer = c;
cca86cc8
SC
14623 input_line_pointer = section_name;
14624 obj_elf_section (ignore);
14625 return;
14626 }
14627 input_line_pointer++;
14628
14629 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
14630 if (c == ',')
14631 section_type = get_absolute_expression ();
14632 else
14633 section_type = 0;
14634 if (*input_line_pointer++ == ',')
14635 section_flag = get_absolute_expression ();
14636 else
14637 section_flag = 0;
14638 if (*input_line_pointer++ == ',')
14639 section_entry_size = get_absolute_expression ();
14640 else
14641 section_entry_size = 0;
14642 if (*input_line_pointer++ == ',')
14643 section_alignment = get_absolute_expression ();
14644 else
14645 section_alignment = 0;
87975d2a
AM
14646 /* FIXME: really ignore? */
14647 (void) section_alignment;
cca86cc8 14648
a816d1ed
AO
14649 section_name = xstrdup (section_name);
14650
8ab8a5c8
RS
14651 /* When using the generic form of .section (as implemented by obj-elf.c),
14652 there's no way to set the section type to SHT_MIPS_DWARF. Users have
14653 traditionally had to fall back on the more common @progbits instead.
14654
14655 There's nothing really harmful in this, since bfd will correct
14656 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
708587a4 14657 means that, for backwards compatibility, the special_section entries
8ab8a5c8
RS
14658 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
14659
14660 Even so, we shouldn't force users of the MIPS .section syntax to
14661 incorrectly label the sections as SHT_PROGBITS. The best compromise
14662 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
14663 generic type-checking code. */
14664 if (section_type == SHT_MIPS_DWARF)
14665 section_type = SHT_PROGBITS;
14666
cca86cc8
SC
14667 obj_elf_change_section (section_name, section_type, section_flag,
14668 section_entry_size, 0, 0, 0);
a816d1ed
AO
14669
14670 if (now_seg->name != section_name)
14671 free (section_name);
cca86cc8 14672}
252b5132
RH
14673
14674void
17a2f251 14675mips_enable_auto_align (void)
252b5132
RH
14676{
14677 auto_align = 1;
14678}
14679
14680static void
17a2f251 14681s_cons (int log_size)
252b5132 14682{
a8dbcb85
TS
14683 segment_info_type *si = seg_info (now_seg);
14684 struct insn_label_list *l = si->label_list;
252b5132 14685
7d10b47d 14686 mips_emit_delays ();
252b5132 14687 if (log_size > 0 && auto_align)
462427c4 14688 mips_align (log_size, 0, l);
252b5132 14689 cons (1 << log_size);
a1facbec 14690 mips_clear_insn_labels ();
252b5132
RH
14691}
14692
14693static void
17a2f251 14694s_float_cons (int type)
252b5132 14695{
a8dbcb85
TS
14696 segment_info_type *si = seg_info (now_seg);
14697 struct insn_label_list *l = si->label_list;
252b5132 14698
7d10b47d 14699 mips_emit_delays ();
252b5132
RH
14700
14701 if (auto_align)
49309057
ILT
14702 {
14703 if (type == 'd')
462427c4 14704 mips_align (3, 0, l);
49309057 14705 else
462427c4 14706 mips_align (2, 0, l);
49309057 14707 }
252b5132 14708
252b5132 14709 float_cons (type);
a1facbec 14710 mips_clear_insn_labels ();
252b5132
RH
14711}
14712
14713/* Handle .globl. We need to override it because on Irix 5 you are
14714 permitted to say
14715 .globl foo .text
14716 where foo is an undefined symbol, to mean that foo should be
14717 considered to be the address of a function. */
14718
14719static void
17a2f251 14720s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
14721{
14722 char *name;
14723 int c;
14724 symbolS *symbolP;
14725 flagword flag;
14726
8a06b769 14727 do
252b5132 14728 {
8a06b769 14729 name = input_line_pointer;
252b5132 14730 c = get_symbol_end ();
8a06b769
TS
14731 symbolP = symbol_find_or_make (name);
14732 S_SET_EXTERNAL (symbolP);
14733
252b5132 14734 *input_line_pointer = c;
8a06b769 14735 SKIP_WHITESPACE ();
252b5132 14736
8a06b769
TS
14737 /* On Irix 5, every global symbol that is not explicitly labelled as
14738 being a function is apparently labelled as being an object. */
14739 flag = BSF_OBJECT;
252b5132 14740
8a06b769
TS
14741 if (!is_end_of_line[(unsigned char) *input_line_pointer]
14742 && (*input_line_pointer != ','))
14743 {
14744 char *secname;
14745 asection *sec;
14746
14747 secname = input_line_pointer;
14748 c = get_symbol_end ();
14749 sec = bfd_get_section_by_name (stdoutput, secname);
14750 if (sec == NULL)
14751 as_bad (_("%s: no such section"), secname);
14752 *input_line_pointer = c;
14753
14754 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
14755 flag = BSF_FUNCTION;
14756 }
14757
14758 symbol_get_bfdsym (symbolP)->flags |= flag;
14759
14760 c = *input_line_pointer;
14761 if (c == ',')
14762 {
14763 input_line_pointer++;
14764 SKIP_WHITESPACE ();
14765 if (is_end_of_line[(unsigned char) *input_line_pointer])
14766 c = '\n';
14767 }
14768 }
14769 while (c == ',');
252b5132 14770
252b5132
RH
14771 demand_empty_rest_of_line ();
14772}
14773
14774static void
17a2f251 14775s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
14776{
14777 char *opt;
14778 char c;
14779
14780 opt = input_line_pointer;
14781 c = get_symbol_end ();
14782
14783 if (*opt == 'O')
14784 {
14785 /* FIXME: What does this mean? */
14786 }
14787 else if (strncmp (opt, "pic", 3) == 0)
14788 {
14789 int i;
14790
14791 i = atoi (opt + 3);
14792 if (i == 0)
14793 mips_pic = NO_PIC;
14794 else if (i == 2)
143d77c5 14795 {
8b828383 14796 mips_pic = SVR4_PIC;
143d77c5
EC
14797 mips_abicalls = TRUE;
14798 }
252b5132
RH
14799 else
14800 as_bad (_(".option pic%d not supported"), i);
14801
4d0d148d 14802 if (mips_pic == SVR4_PIC)
252b5132
RH
14803 {
14804 if (g_switch_seen && g_switch_value != 0)
14805 as_warn (_("-G may not be used with SVR4 PIC code"));
14806 g_switch_value = 0;
14807 bfd_set_gp_size (stdoutput, 0);
14808 }
14809 }
14810 else
14811 as_warn (_("Unrecognized option \"%s\""), opt);
14812
14813 *input_line_pointer = c;
14814 demand_empty_rest_of_line ();
14815}
14816
14817/* This structure is used to hold a stack of .set values. */
14818
e972090a
NC
14819struct mips_option_stack
14820{
252b5132
RH
14821 struct mips_option_stack *next;
14822 struct mips_set_options options;
14823};
14824
14825static struct mips_option_stack *mips_opts_stack;
14826
14827/* Handle the .set pseudo-op. */
14828
14829static void
17a2f251 14830s_mipsset (int x ATTRIBUTE_UNUSED)
252b5132
RH
14831{
14832 char *name = input_line_pointer, ch;
c6278170 14833 const struct mips_ase *ase;
252b5132
RH
14834
14835 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 14836 ++input_line_pointer;
252b5132
RH
14837 ch = *input_line_pointer;
14838 *input_line_pointer = '\0';
14839
14840 if (strcmp (name, "reorder") == 0)
14841 {
7d10b47d
RS
14842 if (mips_opts.noreorder)
14843 end_noreorder ();
252b5132
RH
14844 }
14845 else if (strcmp (name, "noreorder") == 0)
14846 {
7d10b47d
RS
14847 if (!mips_opts.noreorder)
14848 start_noreorder ();
252b5132 14849 }
741fe287
MR
14850 else if (strncmp (name, "at=", 3) == 0)
14851 {
14852 char *s = name + 3;
14853
14854 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
14855 as_bad (_("Unrecognized register name `%s'"), s);
14856 }
252b5132
RH
14857 else if (strcmp (name, "at") == 0)
14858 {
741fe287 14859 mips_opts.at = ATREG;
252b5132
RH
14860 }
14861 else if (strcmp (name, "noat") == 0)
14862 {
741fe287 14863 mips_opts.at = ZERO;
252b5132
RH
14864 }
14865 else if (strcmp (name, "macro") == 0)
14866 {
14867 mips_opts.warn_about_macros = 0;
14868 }
14869 else if (strcmp (name, "nomacro") == 0)
14870 {
14871 if (mips_opts.noreorder == 0)
14872 as_bad (_("`noreorder' must be set before `nomacro'"));
14873 mips_opts.warn_about_macros = 1;
14874 }
14875 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
14876 {
14877 mips_opts.nomove = 0;
14878 }
14879 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
14880 {
14881 mips_opts.nomove = 1;
14882 }
14883 else if (strcmp (name, "bopt") == 0)
14884 {
14885 mips_opts.nobopt = 0;
14886 }
14887 else if (strcmp (name, "nobopt") == 0)
14888 {
14889 mips_opts.nobopt = 1;
14890 }
ad3fea08
TS
14891 else if (strcmp (name, "gp=default") == 0)
14892 mips_opts.gp32 = file_mips_gp32;
14893 else if (strcmp (name, "gp=32") == 0)
14894 mips_opts.gp32 = 1;
14895 else if (strcmp (name, "gp=64") == 0)
14896 {
14897 if (!ISA_HAS_64BIT_REGS (mips_opts.isa))
20203fb9 14898 as_warn (_("%s isa does not support 64-bit registers"),
ad3fea08
TS
14899 mips_cpu_info_from_isa (mips_opts.isa)->name);
14900 mips_opts.gp32 = 0;
14901 }
14902 else if (strcmp (name, "fp=default") == 0)
14903 mips_opts.fp32 = file_mips_fp32;
14904 else if (strcmp (name, "fp=32") == 0)
14905 mips_opts.fp32 = 1;
14906 else if (strcmp (name, "fp=64") == 0)
14907 {
14908 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
20203fb9 14909 as_warn (_("%s isa does not support 64-bit floating point registers"),
ad3fea08
TS
14910 mips_cpu_info_from_isa (mips_opts.isa)->name);
14911 mips_opts.fp32 = 0;
14912 }
037b32b9
AN
14913 else if (strcmp (name, "softfloat") == 0)
14914 mips_opts.soft_float = 1;
14915 else if (strcmp (name, "hardfloat") == 0)
14916 mips_opts.soft_float = 0;
14917 else if (strcmp (name, "singlefloat") == 0)
14918 mips_opts.single_float = 1;
14919 else if (strcmp (name, "doublefloat") == 0)
14920 mips_opts.single_float = 0;
252b5132
RH
14921 else if (strcmp (name, "mips16") == 0
14922 || strcmp (name, "MIPS-16") == 0)
df58fc94
RS
14923 {
14924 if (mips_opts.micromips == 1)
14925 as_fatal (_("`mips16' cannot be used with `micromips'"));
14926 mips_opts.mips16 = 1;
14927 }
252b5132
RH
14928 else if (strcmp (name, "nomips16") == 0
14929 || strcmp (name, "noMIPS-16") == 0)
14930 mips_opts.mips16 = 0;
df58fc94
RS
14931 else if (strcmp (name, "micromips") == 0)
14932 {
14933 if (mips_opts.mips16 == 1)
14934 as_fatal (_("`micromips' cannot be used with `mips16'"));
14935 mips_opts.micromips = 1;
14936 }
14937 else if (strcmp (name, "nomicromips") == 0)
14938 mips_opts.micromips = 0;
c6278170
RS
14939 else if (name[0] == 'n'
14940 && name[1] == 'o'
14941 && (ase = mips_lookup_ase (name + 2)))
14942 mips_set_ase (ase, FALSE);
14943 else if ((ase = mips_lookup_ase (name)))
14944 mips_set_ase (ase, TRUE);
1a2c1fad 14945 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 14946 {
af7ee8bf 14947 int reset = 0;
252b5132 14948
1a2c1fad
CD
14949 /* Permit the user to change the ISA and architecture on the fly.
14950 Needless to say, misuse can cause serious problems. */
81a21e38 14951 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
af7ee8bf
CD
14952 {
14953 reset = 1;
14954 mips_opts.isa = file_mips_isa;
1a2c1fad 14955 mips_opts.arch = file_mips_arch;
1a2c1fad
CD
14956 }
14957 else if (strncmp (name, "arch=", 5) == 0)
14958 {
14959 const struct mips_cpu_info *p;
14960
14961 p = mips_parse_cpu("internal use", name + 5);
14962 if (!p)
14963 as_bad (_("unknown architecture %s"), name + 5);
14964 else
14965 {
14966 mips_opts.arch = p->cpu;
14967 mips_opts.isa = p->isa;
14968 }
14969 }
81a21e38
TS
14970 else if (strncmp (name, "mips", 4) == 0)
14971 {
14972 const struct mips_cpu_info *p;
14973
14974 p = mips_parse_cpu("internal use", name);
14975 if (!p)
14976 as_bad (_("unknown ISA level %s"), name + 4);
14977 else
14978 {
14979 mips_opts.arch = p->cpu;
14980 mips_opts.isa = p->isa;
14981 }
14982 }
af7ee8bf 14983 else
81a21e38 14984 as_bad (_("unknown ISA or architecture %s"), name);
af7ee8bf
CD
14985
14986 switch (mips_opts.isa)
98d3f06f
KH
14987 {
14988 case 0:
98d3f06f 14989 break;
af7ee8bf
CD
14990 case ISA_MIPS1:
14991 case ISA_MIPS2:
14992 case ISA_MIPS32:
14993 case ISA_MIPS32R2:
98d3f06f
KH
14994 mips_opts.gp32 = 1;
14995 mips_opts.fp32 = 1;
14996 break;
af7ee8bf
CD
14997 case ISA_MIPS3:
14998 case ISA_MIPS4:
14999 case ISA_MIPS5:
15000 case ISA_MIPS64:
5f74bc13 15001 case ISA_MIPS64R2:
98d3f06f 15002 mips_opts.gp32 = 0;
e407c74b
NC
15003 if (mips_opts.arch == CPU_R5900)
15004 {
15005 mips_opts.fp32 = 1;
15006 }
15007 else
15008 {
98d3f06f 15009 mips_opts.fp32 = 0;
e407c74b 15010 }
98d3f06f
KH
15011 break;
15012 default:
15013 as_bad (_("unknown ISA level %s"), name + 4);
15014 break;
15015 }
af7ee8bf 15016 if (reset)
98d3f06f 15017 {
af7ee8bf
CD
15018 mips_opts.gp32 = file_mips_gp32;
15019 mips_opts.fp32 = file_mips_fp32;
98d3f06f 15020 }
252b5132
RH
15021 }
15022 else if (strcmp (name, "autoextend") == 0)
15023 mips_opts.noautoextend = 0;
15024 else if (strcmp (name, "noautoextend") == 0)
15025 mips_opts.noautoextend = 1;
833794fc
MR
15026 else if (strcmp (name, "insn32") == 0)
15027 mips_opts.insn32 = TRUE;
15028 else if (strcmp (name, "noinsn32") == 0)
15029 mips_opts.insn32 = FALSE;
252b5132
RH
15030 else if (strcmp (name, "push") == 0)
15031 {
15032 struct mips_option_stack *s;
15033
15034 s = (struct mips_option_stack *) xmalloc (sizeof *s);
15035 s->next = mips_opts_stack;
15036 s->options = mips_opts;
15037 mips_opts_stack = s;
15038 }
15039 else if (strcmp (name, "pop") == 0)
15040 {
15041 struct mips_option_stack *s;
15042
15043 s = mips_opts_stack;
15044 if (s == NULL)
15045 as_bad (_(".set pop with no .set push"));
15046 else
15047 {
15048 /* If we're changing the reorder mode we need to handle
15049 delay slots correctly. */
15050 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 15051 start_noreorder ();
252b5132 15052 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 15053 end_noreorder ();
252b5132
RH
15054
15055 mips_opts = s->options;
15056 mips_opts_stack = s->next;
15057 free (s);
15058 }
15059 }
aed1a261
RS
15060 else if (strcmp (name, "sym32") == 0)
15061 mips_opts.sym32 = TRUE;
15062 else if (strcmp (name, "nosym32") == 0)
15063 mips_opts.sym32 = FALSE;
e6559e01
JM
15064 else if (strchr (name, ','))
15065 {
15066 /* Generic ".set" directive; use the generic handler. */
15067 *input_line_pointer = ch;
15068 input_line_pointer = name;
15069 s_set (0);
15070 return;
15071 }
252b5132
RH
15072 else
15073 {
15074 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
15075 }
c6278170 15076 mips_check_isa_supports_ases ();
252b5132
RH
15077 *input_line_pointer = ch;
15078 demand_empty_rest_of_line ();
15079}
15080
15081/* Handle the .abicalls pseudo-op. I believe this is equivalent to
15082 .option pic2. It means to generate SVR4 PIC calls. */
15083
15084static void
17a2f251 15085s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
15086{
15087 mips_pic = SVR4_PIC;
143d77c5 15088 mips_abicalls = TRUE;
4d0d148d
TS
15089
15090 if (g_switch_seen && g_switch_value != 0)
15091 as_warn (_("-G may not be used with SVR4 PIC code"));
15092 g_switch_value = 0;
15093
252b5132
RH
15094 bfd_set_gp_size (stdoutput, 0);
15095 demand_empty_rest_of_line ();
15096}
15097
15098/* Handle the .cpload pseudo-op. This is used when generating SVR4
15099 PIC code. It sets the $gp register for the function based on the
15100 function address, which is in the register named in the argument.
15101 This uses a relocation against _gp_disp, which is handled specially
15102 by the linker. The result is:
15103 lui $gp,%hi(_gp_disp)
15104 addiu $gp,$gp,%lo(_gp_disp)
15105 addu $gp,$gp,.cpload argument
aa6975fb
ILT
15106 The .cpload argument is normally $25 == $t9.
15107
15108 The -mno-shared option changes this to:
bbe506e8
TS
15109 lui $gp,%hi(__gnu_local_gp)
15110 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
15111 and the argument is ignored. This saves an instruction, but the
15112 resulting code is not position independent; it uses an absolute
bbe506e8
TS
15113 address for __gnu_local_gp. Thus code assembled with -mno-shared
15114 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
15115
15116static void
17a2f251 15117s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
15118{
15119 expressionS ex;
aa6975fb
ILT
15120 int reg;
15121 int in_shared;
252b5132 15122
6478892d
TS
15123 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
15124 .cpload is ignored. */
15125 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
15126 {
15127 s_ignore (0);
15128 return;
15129 }
15130
a276b80c
MR
15131 if (mips_opts.mips16)
15132 {
15133 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
15134 ignore_rest_of_line ();
15135 return;
15136 }
15137
d3ecfc59 15138 /* .cpload should be in a .set noreorder section. */
252b5132
RH
15139 if (mips_opts.noreorder == 0)
15140 as_warn (_(".cpload not in noreorder section"));
15141
aa6975fb
ILT
15142 reg = tc_get_register (0);
15143
15144 /* If we need to produce a 64-bit address, we are better off using
15145 the default instruction sequence. */
aed1a261 15146 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 15147
252b5132 15148 ex.X_op = O_symbol;
bbe506e8
TS
15149 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
15150 "__gnu_local_gp");
252b5132
RH
15151 ex.X_op_symbol = NULL;
15152 ex.X_add_number = 0;
15153
15154 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 15155 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 15156
8a75745d
MR
15157 mips_mark_labels ();
15158 mips_assembling_insn = TRUE;
15159
584892a6 15160 macro_start ();
67c0d1eb
RS
15161 macro_build_lui (&ex, mips_gp_register);
15162 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 15163 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
15164 if (in_shared)
15165 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
15166 mips_gp_register, reg);
584892a6 15167 macro_end ();
252b5132 15168
8a75745d 15169 mips_assembling_insn = FALSE;
252b5132
RH
15170 demand_empty_rest_of_line ();
15171}
15172
6478892d
TS
15173/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
15174 .cpsetup $reg1, offset|$reg2, label
15175
15176 If offset is given, this results in:
15177 sd $gp, offset($sp)
956cd1d6 15178 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
15179 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
15180 daddu $gp, $gp, $reg1
6478892d
TS
15181
15182 If $reg2 is given, this results in:
15183 daddu $reg2, $gp, $0
956cd1d6 15184 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
15185 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
15186 daddu $gp, $gp, $reg1
aa6975fb
ILT
15187 $reg1 is normally $25 == $t9.
15188
15189 The -mno-shared option replaces the last three instructions with
15190 lui $gp,%hi(_gp)
54f4ddb3 15191 addiu $gp,$gp,%lo(_gp) */
aa6975fb 15192
6478892d 15193static void
17a2f251 15194s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
15195{
15196 expressionS ex_off;
15197 expressionS ex_sym;
15198 int reg1;
6478892d 15199
8586fc66 15200 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
15201 We also need NewABI support. */
15202 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
15203 {
15204 s_ignore (0);
15205 return;
15206 }
15207
a276b80c
MR
15208 if (mips_opts.mips16)
15209 {
15210 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
15211 ignore_rest_of_line ();
15212 return;
15213 }
15214
6478892d
TS
15215 reg1 = tc_get_register (0);
15216 SKIP_WHITESPACE ();
15217 if (*input_line_pointer != ',')
15218 {
15219 as_bad (_("missing argument separator ',' for .cpsetup"));
15220 return;
15221 }
15222 else
80245285 15223 ++input_line_pointer;
6478892d
TS
15224 SKIP_WHITESPACE ();
15225 if (*input_line_pointer == '$')
80245285
TS
15226 {
15227 mips_cpreturn_register = tc_get_register (0);
15228 mips_cpreturn_offset = -1;
15229 }
6478892d 15230 else
80245285
TS
15231 {
15232 mips_cpreturn_offset = get_absolute_expression ();
15233 mips_cpreturn_register = -1;
15234 }
6478892d
TS
15235 SKIP_WHITESPACE ();
15236 if (*input_line_pointer != ',')
15237 {
15238 as_bad (_("missing argument separator ',' for .cpsetup"));
15239 return;
15240 }
15241 else
f9419b05 15242 ++input_line_pointer;
6478892d 15243 SKIP_WHITESPACE ();
f21f8242 15244 expression (&ex_sym);
6478892d 15245
8a75745d
MR
15246 mips_mark_labels ();
15247 mips_assembling_insn = TRUE;
15248
584892a6 15249 macro_start ();
6478892d
TS
15250 if (mips_cpreturn_register == -1)
15251 {
15252 ex_off.X_op = O_constant;
15253 ex_off.X_add_symbol = NULL;
15254 ex_off.X_op_symbol = NULL;
15255 ex_off.X_add_number = mips_cpreturn_offset;
15256
67c0d1eb 15257 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 15258 BFD_RELOC_LO16, SP);
6478892d
TS
15259 }
15260 else
67c0d1eb 15261 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
17a2f251 15262 mips_gp_register, 0);
6478892d 15263
aed1a261 15264 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb 15265 {
df58fc94 15266 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
aa6975fb
ILT
15267 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
15268 BFD_RELOC_HI16_S);
15269
15270 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
15271 mips_gp_register, -1, BFD_RELOC_GPREL16,
15272 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
15273
15274 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
15275 mips_gp_register, reg1);
15276 }
15277 else
15278 {
15279 expressionS ex;
15280
15281 ex.X_op = O_symbol;
4184909a 15282 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
15283 ex.X_op_symbol = NULL;
15284 ex.X_add_number = 0;
6e1304d8 15285
aa6975fb
ILT
15286 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
15287 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
15288
15289 macro_build_lui (&ex, mips_gp_register);
15290 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
15291 mips_gp_register, BFD_RELOC_LO16);
15292 }
f21f8242 15293
584892a6 15294 macro_end ();
6478892d 15295
8a75745d 15296 mips_assembling_insn = FALSE;
6478892d
TS
15297 demand_empty_rest_of_line ();
15298}
15299
15300static void
17a2f251 15301s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
15302{
15303 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
54f4ddb3 15304 .cplocal is ignored. */
6478892d
TS
15305 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
15306 {
15307 s_ignore (0);
15308 return;
15309 }
15310
a276b80c
MR
15311 if (mips_opts.mips16)
15312 {
15313 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
15314 ignore_rest_of_line ();
15315 return;
15316 }
15317
6478892d 15318 mips_gp_register = tc_get_register (0);
85b51719 15319 demand_empty_rest_of_line ();
6478892d
TS
15320}
15321
252b5132
RH
15322/* Handle the .cprestore pseudo-op. This stores $gp into a given
15323 offset from $sp. The offset is remembered, and after making a PIC
15324 call $gp is restored from that location. */
15325
15326static void
17a2f251 15327s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
15328{
15329 expressionS ex;
252b5132 15330
6478892d 15331 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 15332 .cprestore is ignored. */
6478892d 15333 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
15334 {
15335 s_ignore (0);
15336 return;
15337 }
15338
a276b80c
MR
15339 if (mips_opts.mips16)
15340 {
15341 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
15342 ignore_rest_of_line ();
15343 return;
15344 }
15345
252b5132 15346 mips_cprestore_offset = get_absolute_expression ();
7a621144 15347 mips_cprestore_valid = 1;
252b5132
RH
15348
15349 ex.X_op = O_constant;
15350 ex.X_add_symbol = NULL;
15351 ex.X_op_symbol = NULL;
15352 ex.X_add_number = mips_cprestore_offset;
15353
8a75745d
MR
15354 mips_mark_labels ();
15355 mips_assembling_insn = TRUE;
15356
584892a6 15357 macro_start ();
67c0d1eb
RS
15358 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
15359 SP, HAVE_64BIT_ADDRESSES);
584892a6 15360 macro_end ();
252b5132 15361
8a75745d 15362 mips_assembling_insn = FALSE;
252b5132
RH
15363 demand_empty_rest_of_line ();
15364}
15365
6478892d 15366/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 15367 was given in the preceding .cpsetup, it results in:
6478892d 15368 ld $gp, offset($sp)
76b3015f 15369
6478892d 15370 If a register $reg2 was given there, it results in:
54f4ddb3
TS
15371 daddu $gp, $reg2, $0 */
15372
6478892d 15373static void
17a2f251 15374s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
15375{
15376 expressionS ex;
6478892d
TS
15377
15378 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
15379 We also need NewABI support. */
15380 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
15381 {
15382 s_ignore (0);
15383 return;
15384 }
15385
a276b80c
MR
15386 if (mips_opts.mips16)
15387 {
15388 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
15389 ignore_rest_of_line ();
15390 return;
15391 }
15392
8a75745d
MR
15393 mips_mark_labels ();
15394 mips_assembling_insn = TRUE;
15395
584892a6 15396 macro_start ();
6478892d
TS
15397 if (mips_cpreturn_register == -1)
15398 {
15399 ex.X_op = O_constant;
15400 ex.X_add_symbol = NULL;
15401 ex.X_op_symbol = NULL;
15402 ex.X_add_number = mips_cpreturn_offset;
15403
67c0d1eb 15404 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
15405 }
15406 else
67c0d1eb 15407 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
17a2f251 15408 mips_cpreturn_register, 0);
584892a6 15409 macro_end ();
6478892d 15410
8a75745d 15411 mips_assembling_insn = FALSE;
6478892d
TS
15412 demand_empty_rest_of_line ();
15413}
15414
d0f13682
CLT
15415/* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
15416 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
15417 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
15418 debug information or MIPS16 TLS. */
741d6ea8
JM
15419
15420static void
d0f13682
CLT
15421s_tls_rel_directive (const size_t bytes, const char *dirstr,
15422 bfd_reloc_code_real_type rtype)
741d6ea8
JM
15423{
15424 expressionS ex;
15425 char *p;
15426
15427 expression (&ex);
15428
15429 if (ex.X_op != O_symbol)
15430 {
d0f13682 15431 as_bad (_("Unsupported use of %s"), dirstr);
741d6ea8
JM
15432 ignore_rest_of_line ();
15433 }
15434
15435 p = frag_more (bytes);
15436 md_number_to_chars (p, 0, bytes);
d0f13682 15437 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
741d6ea8 15438 demand_empty_rest_of_line ();
de64cffd 15439 mips_clear_insn_labels ();
741d6ea8
JM
15440}
15441
15442/* Handle .dtprelword. */
15443
15444static void
15445s_dtprelword (int ignore ATTRIBUTE_UNUSED)
15446{
d0f13682 15447 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
741d6ea8
JM
15448}
15449
15450/* Handle .dtpreldword. */
15451
15452static void
15453s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
15454{
d0f13682
CLT
15455 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
15456}
15457
15458/* Handle .tprelword. */
15459
15460static void
15461s_tprelword (int ignore ATTRIBUTE_UNUSED)
15462{
15463 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
15464}
15465
15466/* Handle .tpreldword. */
15467
15468static void
15469s_tpreldword (int ignore ATTRIBUTE_UNUSED)
15470{
15471 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
741d6ea8
JM
15472}
15473
6478892d
TS
15474/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
15475 code. It sets the offset to use in gp_rel relocations. */
15476
15477static void
17a2f251 15478s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
15479{
15480 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
15481 We also need NewABI support. */
15482 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
15483 {
15484 s_ignore (0);
15485 return;
15486 }
15487
def2e0dd 15488 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
15489
15490 demand_empty_rest_of_line ();
15491}
15492
252b5132
RH
15493/* Handle the .gpword pseudo-op. This is used when generating PIC
15494 code. It generates a 32 bit GP relative reloc. */
15495
15496static void
17a2f251 15497s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132 15498{
a8dbcb85
TS
15499 segment_info_type *si;
15500 struct insn_label_list *l;
252b5132
RH
15501 expressionS ex;
15502 char *p;
15503
15504 /* When not generating PIC code, this is treated as .word. */
15505 if (mips_pic != SVR4_PIC)
15506 {
15507 s_cons (2);
15508 return;
15509 }
15510
a8dbcb85
TS
15511 si = seg_info (now_seg);
15512 l = si->label_list;
7d10b47d 15513 mips_emit_delays ();
252b5132 15514 if (auto_align)
462427c4 15515 mips_align (2, 0, l);
252b5132
RH
15516
15517 expression (&ex);
a1facbec 15518 mips_clear_insn_labels ();
252b5132
RH
15519
15520 if (ex.X_op != O_symbol || ex.X_add_number != 0)
15521 {
15522 as_bad (_("Unsupported use of .gpword"));
15523 ignore_rest_of_line ();
15524 }
15525
15526 p = frag_more (4);
17a2f251 15527 md_number_to_chars (p, 0, 4);
b34976b6 15528 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 15529 BFD_RELOC_GPREL32);
252b5132
RH
15530
15531 demand_empty_rest_of_line ();
15532}
15533
10181a0d 15534static void
17a2f251 15535s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d 15536{
a8dbcb85
TS
15537 segment_info_type *si;
15538 struct insn_label_list *l;
10181a0d
AO
15539 expressionS ex;
15540 char *p;
15541
15542 /* When not generating PIC code, this is treated as .dword. */
15543 if (mips_pic != SVR4_PIC)
15544 {
15545 s_cons (3);
15546 return;
15547 }
15548
a8dbcb85
TS
15549 si = seg_info (now_seg);
15550 l = si->label_list;
7d10b47d 15551 mips_emit_delays ();
10181a0d 15552 if (auto_align)
462427c4 15553 mips_align (3, 0, l);
10181a0d
AO
15554
15555 expression (&ex);
a1facbec 15556 mips_clear_insn_labels ();
10181a0d
AO
15557
15558 if (ex.X_op != O_symbol || ex.X_add_number != 0)
15559 {
15560 as_bad (_("Unsupported use of .gpdword"));
15561 ignore_rest_of_line ();
15562 }
15563
15564 p = frag_more (8);
17a2f251 15565 md_number_to_chars (p, 0, 8);
a105a300 15566 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
6e1304d8 15567 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
15568
15569 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8
RS
15570 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
15571 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
15572
15573 demand_empty_rest_of_line ();
15574}
15575
a3f278e2
CM
15576/* Handle the .ehword pseudo-op. This is used when generating unwinding
15577 tables. It generates a R_MIPS_EH reloc. */
15578
15579static void
15580s_ehword (int ignore ATTRIBUTE_UNUSED)
15581{
15582 expressionS ex;
15583 char *p;
15584
15585 mips_emit_delays ();
15586
15587 expression (&ex);
15588 mips_clear_insn_labels ();
15589
15590 if (ex.X_op != O_symbol || ex.X_add_number != 0)
15591 {
15592 as_bad (_("Unsupported use of .ehword"));
15593 ignore_rest_of_line ();
15594 }
15595
15596 p = frag_more (4);
15597 md_number_to_chars (p, 0, 4);
15598 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
15599 BFD_RELOC_MIPS_EH);
15600
15601 demand_empty_rest_of_line ();
15602}
15603
252b5132
RH
15604/* Handle the .cpadd pseudo-op. This is used when dealing with switch
15605 tables in SVR4 PIC code. */
15606
15607static void
17a2f251 15608s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 15609{
252b5132
RH
15610 int reg;
15611
10181a0d
AO
15612 /* This is ignored when not generating SVR4 PIC code. */
15613 if (mips_pic != SVR4_PIC)
252b5132
RH
15614 {
15615 s_ignore (0);
15616 return;
15617 }
15618
8a75745d
MR
15619 mips_mark_labels ();
15620 mips_assembling_insn = TRUE;
15621
252b5132 15622 /* Add $gp to the register named as an argument. */
584892a6 15623 macro_start ();
252b5132 15624 reg = tc_get_register (0);
67c0d1eb 15625 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 15626 macro_end ();
252b5132 15627
8a75745d 15628 mips_assembling_insn = FALSE;
bdaaa2e1 15629 demand_empty_rest_of_line ();
252b5132
RH
15630}
15631
15632/* Handle the .insn pseudo-op. This marks instruction labels in
df58fc94 15633 mips16/micromips mode. This permits the linker to handle them specially,
252b5132
RH
15634 such as generating jalx instructions when needed. We also make
15635 them odd for the duration of the assembly, in order to generate the
15636 right sort of code. We will make them even in the adjust_symtab
15637 routine, while leaving them marked. This is convenient for the
15638 debugger and the disassembler. The linker knows to make them odd
15639 again. */
15640
15641static void
17a2f251 15642s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 15643{
df58fc94 15644 mips_mark_labels ();
252b5132
RH
15645
15646 demand_empty_rest_of_line ();
15647}
15648
ba92f887
MR
15649/* Handle the .nan pseudo-op. */
15650
15651static void
15652s_nan (int ignore ATTRIBUTE_UNUSED)
15653{
15654 static const char str_legacy[] = "legacy";
15655 static const char str_2008[] = "2008";
15656 size_t i;
15657
15658 for (i = 0; !is_end_of_line[(unsigned char) input_line_pointer[i]]; i++);
15659
15660 if (i == sizeof (str_2008) - 1
15661 && memcmp (input_line_pointer, str_2008, i) == 0)
15662 mips_flag_nan2008 = TRUE;
15663 else if (i == sizeof (str_legacy) - 1
15664 && memcmp (input_line_pointer, str_legacy, i) == 0)
15665 mips_flag_nan2008 = FALSE;
15666 else
15667 as_bad (_("Bad .nan directive"));
15668
15669 input_line_pointer += i;
15670 demand_empty_rest_of_line ();
15671}
15672
754e2bb9
RS
15673/* Handle a .stab[snd] directive. Ideally these directives would be
15674 implemented in a transparent way, so that removing them would not
15675 have any effect on the generated instructions. However, s_stab
15676 internally changes the section, so in practice we need to decide
15677 now whether the preceding label marks compressed code. We do not
15678 support changing the compression mode of a label after a .stab*
15679 directive, such as in:
15680
15681 foo:
15682 .stabs ...
15683 .set mips16
15684
15685 so the current mode wins. */
252b5132
RH
15686
15687static void
17a2f251 15688s_mips_stab (int type)
252b5132 15689{
754e2bb9 15690 mips_mark_labels ();
252b5132
RH
15691 s_stab (type);
15692}
15693
54f4ddb3 15694/* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
252b5132
RH
15695
15696static void
17a2f251 15697s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
15698{
15699 char *name;
15700 int c;
15701 symbolS *symbolP;
15702 expressionS exp;
15703
15704 name = input_line_pointer;
15705 c = get_symbol_end ();
15706 symbolP = symbol_find_or_make (name);
15707 S_SET_WEAK (symbolP);
15708 *input_line_pointer = c;
15709
15710 SKIP_WHITESPACE ();
15711
15712 if (! is_end_of_line[(unsigned char) *input_line_pointer])
15713 {
15714 if (S_IS_DEFINED (symbolP))
15715 {
20203fb9 15716 as_bad (_("ignoring attempt to redefine symbol %s"),
252b5132
RH
15717 S_GET_NAME (symbolP));
15718 ignore_rest_of_line ();
15719 return;
15720 }
bdaaa2e1 15721
252b5132
RH
15722 if (*input_line_pointer == ',')
15723 {
15724 ++input_line_pointer;
15725 SKIP_WHITESPACE ();
15726 }
bdaaa2e1 15727
252b5132
RH
15728 expression (&exp);
15729 if (exp.X_op != O_symbol)
15730 {
20203fb9 15731 as_bad (_("bad .weakext directive"));
98d3f06f 15732 ignore_rest_of_line ();
252b5132
RH
15733 return;
15734 }
49309057 15735 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
15736 }
15737
15738 demand_empty_rest_of_line ();
15739}
15740
15741/* Parse a register string into a number. Called from the ECOFF code
15742 to parse .frame. The argument is non-zero if this is the frame
15743 register, so that we can record it in mips_frame_reg. */
15744
15745int
17a2f251 15746tc_get_register (int frame)
252b5132 15747{
707bfff6 15748 unsigned int reg;
252b5132
RH
15749
15750 SKIP_WHITESPACE ();
707bfff6
TS
15751 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
15752 reg = 0;
252b5132 15753 if (frame)
7a621144
DJ
15754 {
15755 mips_frame_reg = reg != 0 ? reg : SP;
15756 mips_frame_reg_valid = 1;
15757 mips_cprestore_valid = 0;
15758 }
252b5132
RH
15759 return reg;
15760}
15761
15762valueT
17a2f251 15763md_section_align (asection *seg, valueT addr)
252b5132
RH
15764{
15765 int align = bfd_get_section_alignment (stdoutput, seg);
15766
f3ded42a
RS
15767 /* We don't need to align ELF sections to the full alignment.
15768 However, Irix 5 may prefer that we align them at least to a 16
15769 byte boundary. We don't bother to align the sections if we
15770 are targeted for an embedded system. */
15771 if (strncmp (TARGET_OS, "elf", 3) == 0)
15772 return addr;
15773 if (align > 4)
15774 align = 4;
252b5132
RH
15775
15776 return ((addr + (1 << align) - 1) & (-1 << align));
15777}
15778
15779/* Utility routine, called from above as well. If called while the
15780 input file is still being read, it's only an approximation. (For
15781 example, a symbol may later become defined which appeared to be
15782 undefined earlier.) */
15783
15784static int
17a2f251 15785nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
15786{
15787 if (sym == 0)
15788 return 0;
15789
4d0d148d 15790 if (g_switch_value > 0)
252b5132
RH
15791 {
15792 const char *symname;
15793 int change;
15794
c9914766 15795 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
15796 register. It can be if it is smaller than the -G size or if
15797 it is in the .sdata or .sbss section. Certain symbols can
c9914766 15798 not be referenced off the $gp, although it appears as though
252b5132
RH
15799 they can. */
15800 symname = S_GET_NAME (sym);
15801 if (symname != (const char *) NULL
15802 && (strcmp (symname, "eprol") == 0
15803 || strcmp (symname, "etext") == 0
15804 || strcmp (symname, "_gp") == 0
15805 || strcmp (symname, "edata") == 0
15806 || strcmp (symname, "_fbss") == 0
15807 || strcmp (symname, "_fdata") == 0
15808 || strcmp (symname, "_ftext") == 0
15809 || strcmp (symname, "end") == 0
15810 || strcmp (symname, "_gp_disp") == 0))
15811 change = 1;
15812 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
15813 && (0
15814#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
15815 || (symbol_get_obj (sym)->ecoff_extern_size != 0
15816 && (symbol_get_obj (sym)->ecoff_extern_size
15817 <= g_switch_value))
252b5132
RH
15818#endif
15819 /* We must defer this decision until after the whole
15820 file has been read, since there might be a .extern
15821 after the first use of this symbol. */
15822 || (before_relaxing
15823#ifndef NO_ECOFF_DEBUGGING
49309057 15824 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
15825#endif
15826 && S_GET_VALUE (sym) == 0)
15827 || (S_GET_VALUE (sym) != 0
15828 && S_GET_VALUE (sym) <= g_switch_value)))
15829 change = 0;
15830 else
15831 {
15832 const char *segname;
15833
15834 segname = segment_name (S_GET_SEGMENT (sym));
9c2799c2 15835 gas_assert (strcmp (segname, ".lit8") != 0
252b5132
RH
15836 && strcmp (segname, ".lit4") != 0);
15837 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
15838 && strcmp (segname, ".sbss") != 0
15839 && strncmp (segname, ".sdata.", 7) != 0
d4dc2f22
TS
15840 && strncmp (segname, ".sbss.", 6) != 0
15841 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
fba2b7f9 15842 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
15843 }
15844 return change;
15845 }
15846 else
c9914766 15847 /* We are not optimizing for the $gp register. */
252b5132
RH
15848 return 1;
15849}
15850
5919d012
RS
15851
15852/* Return true if the given symbol should be considered local for SVR4 PIC. */
15853
15854static bfd_boolean
17a2f251 15855pic_need_relax (symbolS *sym, asection *segtype)
5919d012
RS
15856{
15857 asection *symsec;
5919d012
RS
15858
15859 /* Handle the case of a symbol equated to another symbol. */
15860 while (symbol_equated_reloc_p (sym))
15861 {
15862 symbolS *n;
15863
5f0fe04b 15864 /* It's possible to get a loop here in a badly written program. */
5919d012
RS
15865 n = symbol_get_value_expression (sym)->X_add_symbol;
15866 if (n == sym)
15867 break;
15868 sym = n;
15869 }
15870
df1f3cda
DD
15871 if (symbol_section_p (sym))
15872 return TRUE;
15873
5919d012
RS
15874 symsec = S_GET_SEGMENT (sym);
15875
5919d012 15876 /* This must duplicate the test in adjust_reloc_syms. */
45dfa85a
AM
15877 return (!bfd_is_und_section (symsec)
15878 && !bfd_is_abs_section (symsec)
5f0fe04b
TS
15879 && !bfd_is_com_section (symsec)
15880 && !s_is_linkonce (sym, segtype)
5919d012 15881 /* A global or weak symbol is treated as external. */
f3ded42a 15882 && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
5919d012
RS
15883}
15884
15885
252b5132
RH
15886/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
15887 extended opcode. SEC is the section the frag is in. */
15888
15889static int
17a2f251 15890mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132
RH
15891{
15892 int type;
3ccad066 15893 const struct mips_int_operand *operand;
252b5132 15894 offsetT val;
252b5132 15895 segT symsec;
98aa84af 15896 fragS *sym_frag;
252b5132
RH
15897
15898 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
15899 return 0;
15900 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
15901 return 1;
15902
15903 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
3ccad066 15904 operand = mips16_immed_operand (type, FALSE);
252b5132 15905
98aa84af 15906 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 15907 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 15908 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132 15909
3ccad066 15910 if (operand->root.type == OP_PCREL)
252b5132 15911 {
3ccad066 15912 const struct mips_pcrel_operand *pcrel_op;
252b5132 15913 addressT addr;
3ccad066 15914 offsetT maxtiny;
252b5132
RH
15915
15916 /* We won't have the section when we are called from
15917 mips_relax_frag. However, we will always have been called
15918 from md_estimate_size_before_relax first. If this is a
15919 branch to a different section, we mark it as such. If SEC is
15920 NULL, and the frag is not marked, then it must be a branch to
15921 the same section. */
3ccad066 15922 pcrel_op = (const struct mips_pcrel_operand *) operand;
252b5132
RH
15923 if (sec == NULL)
15924 {
15925 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
15926 return 1;
15927 }
15928 else
15929 {
98aa84af 15930 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
15931 if (symsec != sec)
15932 {
15933 fragp->fr_subtype =
15934 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
15935
15936 /* FIXME: We should support this, and let the linker
15937 catch branches and loads that are out of range. */
15938 as_bad_where (fragp->fr_file, fragp->fr_line,
15939 _("unsupported PC relative reference to different section"));
15940
15941 return 1;
15942 }
98aa84af
AM
15943 if (fragp != sym_frag && sym_frag->fr_address == 0)
15944 /* Assume non-extended on the first relaxation pass.
15945 The address we have calculated will be bogus if this is
15946 a forward branch to another frag, as the forward frag
15947 will have fr_address == 0. */
15948 return 0;
252b5132
RH
15949 }
15950
15951 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
15952 the same section. If the relax_marker of the symbol fragment
15953 differs from the relax_marker of this fragment, we have not
15954 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
15955 in STRETCH in order to get a better estimate of the address.
15956 This particularly matters because of the shift bits. */
15957 if (stretch != 0
98aa84af 15958 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
15959 {
15960 fragS *f;
15961
15962 /* Adjust stretch for any alignment frag. Note that if have
15963 been expanding the earlier code, the symbol may be
15964 defined in what appears to be an earlier frag. FIXME:
15965 This doesn't handle the fr_subtype field, which specifies
15966 a maximum number of bytes to skip when doing an
15967 alignment. */
98aa84af 15968 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
15969 {
15970 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
15971 {
15972 if (stretch < 0)
15973 stretch = - ((- stretch)
15974 & ~ ((1 << (int) f->fr_offset) - 1));
15975 else
15976 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
15977 if (stretch == 0)
15978 break;
15979 }
15980 }
15981 if (f != NULL)
15982 val += stretch;
15983 }
15984
15985 addr = fragp->fr_address + fragp->fr_fix;
15986
15987 /* The base address rules are complicated. The base address of
15988 a branch is the following instruction. The base address of a
15989 PC relative load or add is the instruction itself, but if it
15990 is in a delay slot (in which case it can not be extended) use
15991 the address of the instruction whose delay slot it is in. */
3ccad066 15992 if (pcrel_op->include_isa_bit)
252b5132
RH
15993 {
15994 addr += 2;
15995
15996 /* If we are currently assuming that this frag should be
15997 extended, then, the current address is two bytes
bdaaa2e1 15998 higher. */
252b5132
RH
15999 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
16000 addr += 2;
16001
16002 /* Ignore the low bit in the target, since it will be set
16003 for a text label. */
3ccad066 16004 val &= -2;
252b5132
RH
16005 }
16006 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
16007 addr -= 4;
16008 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
16009 addr -= 2;
16010
3ccad066 16011 val -= addr & -(1 << pcrel_op->align_log2);
252b5132
RH
16012
16013 /* If any of the shifted bits are set, we must use an extended
16014 opcode. If the address depends on the size of this
16015 instruction, this can lead to a loop, so we arrange to always
16016 use an extended opcode. We only check this when we are in
16017 the main relaxation loop, when SEC is NULL. */
3ccad066 16018 if ((val & ((1 << operand->shift) - 1)) != 0 && sec == NULL)
252b5132
RH
16019 {
16020 fragp->fr_subtype =
16021 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
16022 return 1;
16023 }
16024
16025 /* If we are about to mark a frag as extended because the value
3ccad066
RS
16026 is precisely the next value above maxtiny, then there is a
16027 chance of an infinite loop as in the following code:
252b5132
RH
16028 la $4,foo
16029 .skip 1020
16030 .align 2
16031 foo:
16032 In this case when the la is extended, foo is 0x3fc bytes
16033 away, so the la can be shrunk, but then foo is 0x400 away, so
16034 the la must be extended. To avoid this loop, we mark the
16035 frag as extended if it was small, and is about to become
3ccad066
RS
16036 extended with the next value above maxtiny. */
16037 maxtiny = mips_int_operand_max (operand);
16038 if (val == maxtiny + (1 << operand->shift)
252b5132
RH
16039 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
16040 && sec == NULL)
16041 {
16042 fragp->fr_subtype =
16043 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
16044 return 1;
16045 }
16046 }
16047 else if (symsec != absolute_section && sec != NULL)
16048 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
16049
3ccad066 16050 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
252b5132
RH
16051}
16052
4a6a3df4
AO
16053/* Compute the length of a branch sequence, and adjust the
16054 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
16055 worst-case length is computed, with UPDATE being used to indicate
16056 whether an unconditional (-1), branch-likely (+1) or regular (0)
16057 branch is to be computed. */
16058static int
17a2f251 16059relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 16060{
b34976b6 16061 bfd_boolean toofar;
4a6a3df4
AO
16062 int length;
16063
16064 if (fragp
16065 && S_IS_DEFINED (fragp->fr_symbol)
16066 && sec == S_GET_SEGMENT (fragp->fr_symbol))
16067 {
16068 addressT addr;
16069 offsetT val;
16070
16071 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
16072
16073 addr = fragp->fr_address + fragp->fr_fix + 4;
16074
16075 val -= addr;
16076
16077 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
16078 }
16079 else if (fragp)
16080 /* If the symbol is not defined or it's in a different segment,
16081 assume the user knows what's going on and emit a short
16082 branch. */
b34976b6 16083 toofar = FALSE;
4a6a3df4 16084 else
b34976b6 16085 toofar = TRUE;
4a6a3df4
AO
16086
16087 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
16088 fragp->fr_subtype
66b3e8da
MR
16089 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
16090 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
16091 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
16092 RELAX_BRANCH_LINK (fragp->fr_subtype),
16093 toofar);
16094
16095 length = 4;
16096 if (toofar)
16097 {
16098 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
16099 length += 8;
16100
16101 if (mips_pic != NO_PIC)
16102 {
16103 /* Additional space for PIC loading of target address. */
16104 length += 8;
16105 if (mips_opts.isa == ISA_MIPS1)
16106 /* Additional space for $at-stabilizing nop. */
16107 length += 4;
16108 }
16109
16110 /* If branch is conditional. */
16111 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
16112 length += 8;
16113 }
b34976b6 16114
4a6a3df4
AO
16115 return length;
16116}
16117
df58fc94
RS
16118/* Compute the length of a branch sequence, and adjust the
16119 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
16120 worst-case length is computed, with UPDATE being used to indicate
16121 whether an unconditional (-1), or regular (0) branch is to be
16122 computed. */
16123
16124static int
16125relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
16126{
16127 bfd_boolean toofar;
16128 int length;
16129
16130 if (fragp
16131 && S_IS_DEFINED (fragp->fr_symbol)
16132 && sec == S_GET_SEGMENT (fragp->fr_symbol))
16133 {
16134 addressT addr;
16135 offsetT val;
16136
16137 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
16138 /* Ignore the low bit in the target, since it will be set
16139 for a text label. */
16140 if ((val & 1) != 0)
16141 --val;
16142
16143 addr = fragp->fr_address + fragp->fr_fix + 4;
16144
16145 val -= addr;
16146
16147 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
16148 }
16149 else if (fragp)
16150 /* If the symbol is not defined or it's in a different segment,
16151 assume the user knows what's going on and emit a short
16152 branch. */
16153 toofar = FALSE;
16154 else
16155 toofar = TRUE;
16156
16157 if (fragp && update
16158 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
16159 fragp->fr_subtype = (toofar
16160 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
16161 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
16162
16163 length = 4;
16164 if (toofar)
16165 {
16166 bfd_boolean compact_known = fragp != NULL;
16167 bfd_boolean compact = FALSE;
16168 bfd_boolean uncond;
16169
16170 if (compact_known)
16171 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
16172 if (fragp)
16173 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
16174 else
16175 uncond = update < 0;
16176
16177 /* If label is out of range, we turn branch <br>:
16178
16179 <br> label # 4 bytes
16180 0:
16181
16182 into:
16183
16184 j label # 4 bytes
16185 nop # 2 bytes if compact && !PIC
16186 0:
16187 */
16188 if (mips_pic == NO_PIC && (!compact_known || compact))
16189 length += 2;
16190
16191 /* If assembling PIC code, we further turn:
16192
16193 j label # 4 bytes
16194
16195 into:
16196
16197 lw/ld at, %got(label)(gp) # 4 bytes
16198 d/addiu at, %lo(label) # 4 bytes
16199 jr/c at # 2 bytes
16200 */
16201 if (mips_pic != NO_PIC)
16202 length += 6;
16203
16204 /* If branch <br> is conditional, we prepend negated branch <brneg>:
16205
16206 <brneg> 0f # 4 bytes
16207 nop # 2 bytes if !compact
16208 */
16209 if (!uncond)
16210 length += (compact_known && compact) ? 4 : 6;
16211 }
16212
16213 return length;
16214}
16215
16216/* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
16217 bit accordingly. */
16218
16219static int
16220relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
16221{
16222 bfd_boolean toofar;
16223
df58fc94
RS
16224 if (fragp
16225 && S_IS_DEFINED (fragp->fr_symbol)
16226 && sec == S_GET_SEGMENT (fragp->fr_symbol))
16227 {
16228 addressT addr;
16229 offsetT val;
16230 int type;
16231
16232 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
16233 /* Ignore the low bit in the target, since it will be set
16234 for a text label. */
16235 if ((val & 1) != 0)
16236 --val;
16237
16238 /* Assume this is a 2-byte branch. */
16239 addr = fragp->fr_address + fragp->fr_fix + 2;
16240
16241 /* We try to avoid the infinite loop by not adding 2 more bytes for
16242 long branches. */
16243
16244 val -= addr;
16245
16246 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
16247 if (type == 'D')
16248 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
16249 else if (type == 'E')
16250 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
16251 else
16252 abort ();
16253 }
16254 else
16255 /* If the symbol is not defined or it's in a different segment,
16256 we emit a normal 32-bit branch. */
16257 toofar = TRUE;
16258
16259 if (fragp && update
16260 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
16261 fragp->fr_subtype
16262 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
16263 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
16264
16265 if (toofar)
16266 return 4;
16267
16268 return 2;
16269}
16270
252b5132
RH
16271/* Estimate the size of a frag before relaxing. Unless this is the
16272 mips16, we are not really relaxing here, and the final size is
16273 encoded in the subtype information. For the mips16, we have to
16274 decide whether we are using an extended opcode or not. */
16275
252b5132 16276int
17a2f251 16277md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 16278{
5919d012 16279 int change;
252b5132 16280
4a6a3df4
AO
16281 if (RELAX_BRANCH_P (fragp->fr_subtype))
16282 {
16283
b34976b6
AM
16284 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
16285
4a6a3df4
AO
16286 return fragp->fr_var;
16287 }
16288
252b5132 16289 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
16290 /* We don't want to modify the EXTENDED bit here; it might get us
16291 into infinite loops. We change it only in mips_relax_frag(). */
16292 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132 16293
df58fc94
RS
16294 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
16295 {
16296 int length = 4;
16297
16298 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
16299 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
16300 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
16301 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
16302 fragp->fr_var = length;
16303
16304 return length;
16305 }
16306
252b5132 16307 if (mips_pic == NO_PIC)
5919d012 16308 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132 16309 else if (mips_pic == SVR4_PIC)
5919d012 16310 change = pic_need_relax (fragp->fr_symbol, segtype);
0a44bf69
RS
16311 else if (mips_pic == VXWORKS_PIC)
16312 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
16313 change = 0;
252b5132
RH
16314 else
16315 abort ();
16316
16317 if (change)
16318 {
4d7206a2 16319 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 16320 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 16321 }
4d7206a2
RS
16322 else
16323 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
16324}
16325
16326/* This is called to see whether a reloc against a defined symbol
de7e6852 16327 should be converted into a reloc against a section. */
252b5132
RH
16328
16329int
17a2f251 16330mips_fix_adjustable (fixS *fixp)
252b5132 16331{
252b5132
RH
16332 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
16333 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
16334 return 0;
a161fe53 16335
252b5132
RH
16336 if (fixp->fx_addsy == NULL)
16337 return 1;
a161fe53 16338
de7e6852
RS
16339 /* If symbol SYM is in a mergeable section, relocations of the form
16340 SYM + 0 can usually be made section-relative. The mergeable data
16341 is then identified by the section offset rather than by the symbol.
16342
16343 However, if we're generating REL LO16 relocations, the offset is split
16344 between the LO16 and parterning high part relocation. The linker will
16345 need to recalculate the complete offset in order to correctly identify
16346 the merge data.
16347
16348 The linker has traditionally not looked for the parterning high part
16349 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
16350 placed anywhere. Rather than break backwards compatibility by changing
16351 this, it seems better not to force the issue, and instead keep the
16352 original symbol. This will work with either linker behavior. */
738e5348 16353 if ((lo16_reloc_p (fixp->fx_r_type)
704803a9 16354 || reloc_needs_lo_p (fixp->fx_r_type))
de7e6852
RS
16355 && HAVE_IN_PLACE_ADDENDS
16356 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
16357 return 0;
16358
ce70d90a 16359 /* There is no place to store an in-place offset for JALR relocations.
2de39019
CM
16360 Likewise an in-range offset of limited PC-relative relocations may
16361 overflow the in-place relocatable field if recalculated against the
16362 start address of the symbol's containing section. */
ce70d90a 16363 if (HAVE_IN_PLACE_ADDENDS
2de39019
CM
16364 && (limited_pcrel_reloc_p (fixp->fx_r_type)
16365 || jalr_reloc_p (fixp->fx_r_type)))
1180b5a4
RS
16366 return 0;
16367
b314ec0e
RS
16368 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
16369 to a floating-point stub. The same is true for non-R_MIPS16_26
16370 relocations against MIPS16 functions; in this case, the stub becomes
16371 the function's canonical address.
16372
16373 Floating-point stubs are stored in unique .mips16.call.* or
16374 .mips16.fn.* sections. If a stub T for function F is in section S,
16375 the first relocation in section S must be against F; this is how the
16376 linker determines the target function. All relocations that might
16377 resolve to T must also be against F. We therefore have the following
16378 restrictions, which are given in an intentionally-redundant way:
16379
16380 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
16381 symbols.
16382
16383 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
16384 if that stub might be used.
16385
16386 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
16387 symbols.
16388
16389 4. We cannot reduce a stub's relocations against MIPS16 symbols if
16390 that stub might be used.
16391
16392 There is a further restriction:
16393
df58fc94
RS
16394 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
16395 R_MICROMIPS_26_S1) against MIPS16 or microMIPS symbols on
16396 targets with in-place addends; the relocation field cannot
b314ec0e
RS
16397 encode the low bit.
16398
df58fc94
RS
16399 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
16400 against a MIPS16 symbol. We deal with (5) by by not reducing any
16401 such relocations on REL targets.
b314ec0e
RS
16402
16403 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
16404 relocation against some symbol R, no relocation against R may be
16405 reduced. (Note that this deals with (2) as well as (1) because
16406 relocations against global symbols will never be reduced on ELF
16407 targets.) This approach is a little simpler than trying to detect
16408 stub sections, and gives the "all or nothing" per-symbol consistency
16409 that we have for MIPS16 symbols. */
f3ded42a 16410 if (fixp->fx_subsy == NULL
30c09090 16411 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
df58fc94
RS
16412 || *symbol_get_tc (fixp->fx_addsy)
16413 || (HAVE_IN_PLACE_ADDENDS
16414 && ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
16415 && jmp_reloc_p (fixp->fx_r_type))))
252b5132 16416 return 0;
a161fe53 16417
252b5132
RH
16418 return 1;
16419}
16420
16421/* Translate internal representation of relocation info to BFD target
16422 format. */
16423
16424arelent **
17a2f251 16425tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
16426{
16427 static arelent *retval[4];
16428 arelent *reloc;
16429 bfd_reloc_code_real_type code;
16430
4b0cff4e
TS
16431 memset (retval, 0, sizeof(retval));
16432 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
49309057
ILT
16433 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
16434 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
16435 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
16436
bad36eac
DJ
16437 if (fixp->fx_pcrel)
16438 {
df58fc94
RS
16439 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
16440 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
16441 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
b47468a6
CM
16442 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
16443 || fixp->fx_r_type == BFD_RELOC_32_PCREL);
bad36eac
DJ
16444
16445 /* At this point, fx_addnumber is "symbol offset - pcrel address".
16446 Relocations want only the symbol offset. */
16447 reloc->addend = fixp->fx_addnumber + reloc->address;
bad36eac
DJ
16448 }
16449 else
16450 reloc->addend = fixp->fx_addnumber;
252b5132 16451
438c16b8
TS
16452 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
16453 entry to be used in the relocation's section offset. */
16454 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
16455 {
16456 reloc->address = reloc->addend;
16457 reloc->addend = 0;
16458 }
16459
252b5132 16460 code = fixp->fx_r_type;
252b5132 16461
bad36eac 16462 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
252b5132
RH
16463 if (reloc->howto == NULL)
16464 {
16465 as_bad_where (fixp->fx_file, fixp->fx_line,
16466 _("Can not represent %s relocation in this object file format"),
16467 bfd_get_reloc_code_name (code));
16468 retval[0] = NULL;
16469 }
16470
16471 return retval;
16472}
16473
16474/* Relax a machine dependent frag. This returns the amount by which
16475 the current size of the frag should change. */
16476
16477int
17a2f251 16478mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 16479{
4a6a3df4
AO
16480 if (RELAX_BRANCH_P (fragp->fr_subtype))
16481 {
16482 offsetT old_var = fragp->fr_var;
b34976b6
AM
16483
16484 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
16485
16486 return fragp->fr_var - old_var;
16487 }
16488
df58fc94
RS
16489 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
16490 {
16491 offsetT old_var = fragp->fr_var;
16492 offsetT new_var = 4;
16493
16494 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
16495 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
16496 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
16497 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
16498 fragp->fr_var = new_var;
16499
16500 return new_var - old_var;
16501 }
16502
252b5132
RH
16503 if (! RELAX_MIPS16_P (fragp->fr_subtype))
16504 return 0;
16505
c4e7957c 16506 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
16507 {
16508 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
16509 return 0;
16510 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
16511 return 2;
16512 }
16513 else
16514 {
16515 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
16516 return 0;
16517 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
16518 return -2;
16519 }
16520
16521 return 0;
16522}
16523
16524/* Convert a machine dependent frag. */
16525
16526void
17a2f251 16527md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 16528{
4a6a3df4
AO
16529 if (RELAX_BRANCH_P (fragp->fr_subtype))
16530 {
4d68580a 16531 char *buf;
4a6a3df4
AO
16532 unsigned long insn;
16533 expressionS exp;
16534 fixS *fixp;
b34976b6 16535
4d68580a
RS
16536 buf = fragp->fr_literal + fragp->fr_fix;
16537 insn = read_insn (buf);
b34976b6 16538
4a6a3df4
AO
16539 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
16540 {
16541 /* We generate a fixup instead of applying it right now
16542 because, if there are linker relaxations, we're going to
16543 need the relocations. */
16544 exp.X_op = O_symbol;
16545 exp.X_add_symbol = fragp->fr_symbol;
16546 exp.X_add_number = fragp->fr_offset;
16547
4d68580a
RS
16548 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
16549 BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
16550 fixp->fx_file = fragp->fr_file;
16551 fixp->fx_line = fragp->fr_line;
b34976b6 16552
4d68580a 16553 buf = write_insn (buf, insn);
4a6a3df4
AO
16554 }
16555 else
16556 {
16557 int i;
16558
16559 as_warn_where (fragp->fr_file, fragp->fr_line,
5c4f07ba 16560 _("Relaxed out-of-range branch into a jump"));
4a6a3df4
AO
16561
16562 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
16563 goto uncond;
16564
16565 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
16566 {
16567 /* Reverse the branch. */
16568 switch ((insn >> 28) & 0xf)
16569 {
16570 case 4:
3bf0dbfb
MR
16571 /* bc[0-3][tf]l? instructions can have the condition
16572 reversed by tweaking a single TF bit, and their
16573 opcodes all have 0x4???????. */
16574 gas_assert ((insn & 0xf3e00000) == 0x41000000);
4a6a3df4
AO
16575 insn ^= 0x00010000;
16576 break;
16577
16578 case 0:
16579 /* bltz 0x04000000 bgez 0x04010000
54f4ddb3 16580 bltzal 0x04100000 bgezal 0x04110000 */
9c2799c2 16581 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
4a6a3df4
AO
16582 insn ^= 0x00010000;
16583 break;
b34976b6 16584
4a6a3df4
AO
16585 case 1:
16586 /* beq 0x10000000 bne 0x14000000
54f4ddb3 16587 blez 0x18000000 bgtz 0x1c000000 */
4a6a3df4
AO
16588 insn ^= 0x04000000;
16589 break;
16590
16591 default:
16592 abort ();
16593 }
16594 }
16595
16596 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
16597 {
16598 /* Clear the and-link bit. */
9c2799c2 16599 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
4a6a3df4 16600
54f4ddb3
TS
16601 /* bltzal 0x04100000 bgezal 0x04110000
16602 bltzall 0x04120000 bgezall 0x04130000 */
4a6a3df4
AO
16603 insn &= ~0x00100000;
16604 }
16605
16606 /* Branch over the branch (if the branch was likely) or the
16607 full jump (not likely case). Compute the offset from the
16608 current instruction to branch to. */
16609 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
16610 i = 16;
16611 else
16612 {
16613 /* How many bytes in instructions we've already emitted? */
4d68580a 16614 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
16615 /* How many bytes in instructions from here to the end? */
16616 i = fragp->fr_var - i;
16617 }
16618 /* Convert to instruction count. */
16619 i >>= 2;
16620 /* Branch counts from the next instruction. */
b34976b6 16621 i--;
4a6a3df4
AO
16622 insn |= i;
16623 /* Branch over the jump. */
4d68580a 16624 buf = write_insn (buf, insn);
4a6a3df4 16625
54f4ddb3 16626 /* nop */
4d68580a 16627 buf = write_insn (buf, 0);
4a6a3df4
AO
16628
16629 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
16630 {
16631 /* beql $0, $0, 2f */
16632 insn = 0x50000000;
16633 /* Compute the PC offset from the current instruction to
16634 the end of the variable frag. */
16635 /* How many bytes in instructions we've already emitted? */
4d68580a 16636 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
16637 /* How many bytes in instructions from here to the end? */
16638 i = fragp->fr_var - i;
16639 /* Convert to instruction count. */
16640 i >>= 2;
16641 /* Don't decrement i, because we want to branch over the
16642 delay slot. */
4a6a3df4 16643 insn |= i;
4a6a3df4 16644
4d68580a
RS
16645 buf = write_insn (buf, insn);
16646 buf = write_insn (buf, 0);
4a6a3df4
AO
16647 }
16648
16649 uncond:
16650 if (mips_pic == NO_PIC)
16651 {
16652 /* j or jal. */
16653 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
16654 ? 0x0c000000 : 0x08000000);
16655 exp.X_op = O_symbol;
16656 exp.X_add_symbol = fragp->fr_symbol;
16657 exp.X_add_number = fragp->fr_offset;
16658
4d68580a
RS
16659 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
16660 FALSE, BFD_RELOC_MIPS_JMP);
4a6a3df4
AO
16661 fixp->fx_file = fragp->fr_file;
16662 fixp->fx_line = fragp->fr_line;
16663
4d68580a 16664 buf = write_insn (buf, insn);
4a6a3df4
AO
16665 }
16666 else
16667 {
66b3e8da
MR
16668 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
16669
4a6a3df4 16670 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
66b3e8da
MR
16671 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
16672 insn |= at << OP_SH_RT;
4a6a3df4
AO
16673 exp.X_op = O_symbol;
16674 exp.X_add_symbol = fragp->fr_symbol;
16675 exp.X_add_number = fragp->fr_offset;
16676
16677 if (fragp->fr_offset)
16678 {
16679 exp.X_add_symbol = make_expr_symbol (&exp);
16680 exp.X_add_number = 0;
16681 }
16682
4d68580a
RS
16683 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
16684 FALSE, BFD_RELOC_MIPS_GOT16);
4a6a3df4
AO
16685 fixp->fx_file = fragp->fr_file;
16686 fixp->fx_line = fragp->fr_line;
16687
4d68580a 16688 buf = write_insn (buf, insn);
b34976b6 16689
4a6a3df4 16690 if (mips_opts.isa == ISA_MIPS1)
4d68580a
RS
16691 /* nop */
16692 buf = write_insn (buf, 0);
4a6a3df4
AO
16693
16694 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
66b3e8da
MR
16695 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
16696 insn |= at << OP_SH_RS | at << OP_SH_RT;
4a6a3df4 16697
4d68580a
RS
16698 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
16699 FALSE, BFD_RELOC_LO16);
4a6a3df4
AO
16700 fixp->fx_file = fragp->fr_file;
16701 fixp->fx_line = fragp->fr_line;
b34976b6 16702
4d68580a 16703 buf = write_insn (buf, insn);
4a6a3df4
AO
16704
16705 /* j(al)r $at. */
16706 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
66b3e8da 16707 insn = 0x0000f809;
4a6a3df4 16708 else
66b3e8da
MR
16709 insn = 0x00000008;
16710 insn |= at << OP_SH_RS;
4a6a3df4 16711
4d68580a 16712 buf = write_insn (buf, insn);
4a6a3df4
AO
16713 }
16714 }
16715
4a6a3df4 16716 fragp->fr_fix += fragp->fr_var;
4d68580a 16717 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
4a6a3df4
AO
16718 return;
16719 }
16720
df58fc94
RS
16721 /* Relax microMIPS branches. */
16722 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
16723 {
4d68580a 16724 char *buf = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
16725 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
16726 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
16727 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
2309ddf2 16728 bfd_boolean short_ds;
df58fc94
RS
16729 unsigned long insn;
16730 expressionS exp;
16731 fixS *fixp;
16732
16733 exp.X_op = O_symbol;
16734 exp.X_add_symbol = fragp->fr_symbol;
16735 exp.X_add_number = fragp->fr_offset;
16736
16737 fragp->fr_fix += fragp->fr_var;
16738
16739 /* Handle 16-bit branches that fit or are forced to fit. */
16740 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
16741 {
16742 /* We generate a fixup instead of applying it right now,
16743 because if there is linker relaxation, we're going to
16744 need the relocations. */
16745 if (type == 'D')
4d68580a 16746 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
df58fc94
RS
16747 BFD_RELOC_MICROMIPS_10_PCREL_S1);
16748 else if (type == 'E')
4d68580a 16749 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
df58fc94
RS
16750 BFD_RELOC_MICROMIPS_7_PCREL_S1);
16751 else
16752 abort ();
16753
16754 fixp->fx_file = fragp->fr_file;
16755 fixp->fx_line = fragp->fr_line;
16756
16757 /* These relocations can have an addend that won't fit in
16758 2 octets. */
16759 fixp->fx_no_overflow = 1;
16760
16761 return;
16762 }
16763
2309ddf2 16764 /* Handle 32-bit branches that fit or are forced to fit. */
df58fc94
RS
16765 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
16766 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
16767 {
16768 /* We generate a fixup instead of applying it right now,
16769 because if there is linker relaxation, we're going to
16770 need the relocations. */
4d68580a
RS
16771 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
16772 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
16773 fixp->fx_file = fragp->fr_file;
16774 fixp->fx_line = fragp->fr_line;
16775
16776 if (type == 0)
16777 return;
16778 }
16779
16780 /* Relax 16-bit branches to 32-bit branches. */
16781 if (type != 0)
16782 {
4d68580a 16783 insn = read_compressed_insn (buf, 2);
df58fc94
RS
16784
16785 if ((insn & 0xfc00) == 0xcc00) /* b16 */
16786 insn = 0x94000000; /* beq */
16787 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
16788 {
16789 unsigned long regno;
16790
16791 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
16792 regno = micromips_to_32_reg_d_map [regno];
16793 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
16794 insn |= regno << MICROMIPSOP_SH_RS;
16795 }
16796 else
16797 abort ();
16798
16799 /* Nothing else to do, just write it out. */
16800 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
16801 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
16802 {
4d68580a
RS
16803 buf = write_compressed_insn (buf, insn, 4);
16804 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
16805 return;
16806 }
16807 }
16808 else
4d68580a 16809 insn = read_compressed_insn (buf, 4);
df58fc94
RS
16810
16811 /* Relax 32-bit branches to a sequence of instructions. */
16812 as_warn_where (fragp->fr_file, fragp->fr_line,
16813 _("Relaxed out-of-range branch into a jump"));
16814
2309ddf2
MR
16815 /* Set the short-delay-slot bit. */
16816 short_ds = al && (insn & 0x02000000) != 0;
df58fc94
RS
16817
16818 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
16819 {
16820 symbolS *l;
16821
16822 /* Reverse the branch. */
16823 if ((insn & 0xfc000000) == 0x94000000 /* beq */
16824 || (insn & 0xfc000000) == 0xb4000000) /* bne */
16825 insn ^= 0x20000000;
16826 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
16827 || (insn & 0xffe00000) == 0x40400000 /* bgez */
16828 || (insn & 0xffe00000) == 0x40800000 /* blez */
16829 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
16830 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
16831 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
16832 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
16833 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
16834 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
16835 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
16836 insn ^= 0x00400000;
16837 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
16838 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
16839 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
16840 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
16841 insn ^= 0x00200000;
16842 else
16843 abort ();
16844
16845 if (al)
16846 {
16847 /* Clear the and-link and short-delay-slot bits. */
16848 gas_assert ((insn & 0xfda00000) == 0x40200000);
16849
16850 /* bltzal 0x40200000 bgezal 0x40600000 */
16851 /* bltzals 0x42200000 bgezals 0x42600000 */
16852 insn &= ~0x02200000;
16853 }
16854
16855 /* Make a label at the end for use with the branch. */
16856 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
16857 micromips_label_inc ();
f3ded42a 16858 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
df58fc94
RS
16859
16860 /* Refer to it. */
4d68580a
RS
16861 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
16862 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
16863 fixp->fx_file = fragp->fr_file;
16864 fixp->fx_line = fragp->fr_line;
16865
16866 /* Branch over the jump. */
4d68580a 16867 buf = write_compressed_insn (buf, insn, 4);
df58fc94 16868 if (!compact)
4d68580a
RS
16869 /* nop */
16870 buf = write_compressed_insn (buf, 0x0c00, 2);
df58fc94
RS
16871 }
16872
16873 if (mips_pic == NO_PIC)
16874 {
2309ddf2
MR
16875 unsigned long jal = short_ds ? 0x74000000 : 0xf4000000; /* jal/s */
16876
df58fc94
RS
16877 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
16878 insn = al ? jal : 0xd4000000;
16879
4d68580a
RS
16880 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
16881 BFD_RELOC_MICROMIPS_JMP);
df58fc94
RS
16882 fixp->fx_file = fragp->fr_file;
16883 fixp->fx_line = fragp->fr_line;
16884
4d68580a 16885 buf = write_compressed_insn (buf, insn, 4);
df58fc94 16886 if (compact)
4d68580a
RS
16887 /* nop */
16888 buf = write_compressed_insn (buf, 0x0c00, 2);
df58fc94
RS
16889 }
16890 else
16891 {
16892 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
2309ddf2
MR
16893 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
16894 unsigned long jr = compact ? 0x45a0 : 0x4580; /* jr/c */
df58fc94
RS
16895
16896 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
16897 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
16898 insn |= at << MICROMIPSOP_SH_RT;
16899
16900 if (exp.X_add_number)
16901 {
16902 exp.X_add_symbol = make_expr_symbol (&exp);
16903 exp.X_add_number = 0;
16904 }
16905
4d68580a
RS
16906 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
16907 BFD_RELOC_MICROMIPS_GOT16);
df58fc94
RS
16908 fixp->fx_file = fragp->fr_file;
16909 fixp->fx_line = fragp->fr_line;
16910
4d68580a 16911 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
16912
16913 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
16914 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
16915 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
16916
4d68580a
RS
16917 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
16918 BFD_RELOC_MICROMIPS_LO16);
df58fc94
RS
16919 fixp->fx_file = fragp->fr_file;
16920 fixp->fx_line = fragp->fr_line;
16921
4d68580a 16922 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
16923
16924 /* jr/jrc/jalr/jalrs $at */
16925 insn = al ? jalr : jr;
16926 insn |= at << MICROMIPSOP_SH_MJ;
16927
4d68580a 16928 buf = write_compressed_insn (buf, insn, 2);
df58fc94
RS
16929 }
16930
4d68580a 16931 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
16932 return;
16933 }
16934
252b5132
RH
16935 if (RELAX_MIPS16_P (fragp->fr_subtype))
16936 {
16937 int type;
3ccad066 16938 const struct mips_int_operand *operand;
252b5132 16939 offsetT val;
5c04167a
RS
16940 char *buf;
16941 unsigned int user_length, length;
252b5132 16942 unsigned long insn;
5c04167a 16943 bfd_boolean ext;
252b5132
RH
16944
16945 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
3ccad066 16946 operand = mips16_immed_operand (type, FALSE);
252b5132 16947
5c04167a 16948 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
5f5f22c0 16949 val = resolve_symbol_value (fragp->fr_symbol);
3ccad066 16950 if (operand->root.type == OP_PCREL)
252b5132 16951 {
3ccad066 16952 const struct mips_pcrel_operand *pcrel_op;
252b5132
RH
16953 addressT addr;
16954
3ccad066 16955 pcrel_op = (const struct mips_pcrel_operand *) operand;
252b5132
RH
16956 addr = fragp->fr_address + fragp->fr_fix;
16957
16958 /* The rules for the base address of a PC relative reloc are
16959 complicated; see mips16_extended_frag. */
3ccad066 16960 if (pcrel_op->include_isa_bit)
252b5132
RH
16961 {
16962 addr += 2;
16963 if (ext)
16964 addr += 2;
16965 /* Ignore the low bit in the target, since it will be
16966 set for a text label. */
3ccad066 16967 val &= -2;
252b5132
RH
16968 }
16969 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
16970 addr -= 4;
16971 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
16972 addr -= 2;
16973
3ccad066 16974 addr &= -(1 << pcrel_op->align_log2);
252b5132
RH
16975 val -= addr;
16976
16977 /* Make sure the section winds up with the alignment we have
16978 assumed. */
3ccad066
RS
16979 if (operand->shift > 0)
16980 record_alignment (asec, operand->shift);
252b5132
RH
16981 }
16982
16983 if (ext
16984 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
16985 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
16986 as_warn_where (fragp->fr_file, fragp->fr_line,
16987 _("extended instruction in delay slot"));
16988
5c04167a 16989 buf = fragp->fr_literal + fragp->fr_fix;
252b5132 16990
4d68580a 16991 insn = read_compressed_insn (buf, 2);
5c04167a
RS
16992 if (ext)
16993 insn |= MIPS16_EXTEND;
252b5132 16994
5c04167a
RS
16995 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
16996 user_length = 4;
16997 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
16998 user_length = 2;
16999 else
17000 user_length = 0;
17001
43c0598f 17002 mips16_immed (fragp->fr_file, fragp->fr_line, type,
c150d1d2 17003 BFD_RELOC_UNUSED, val, user_length, &insn);
252b5132 17004
5c04167a
RS
17005 length = (ext ? 4 : 2);
17006 gas_assert (mips16_opcode_length (insn) == length);
17007 write_compressed_insn (buf, insn, length);
17008 fragp->fr_fix += length;
252b5132
RH
17009 }
17010 else
17011 {
df58fc94
RS
17012 relax_substateT subtype = fragp->fr_subtype;
17013 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
17014 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
4d7206a2
RS
17015 int first, second;
17016 fixS *fixp;
252b5132 17017
df58fc94
RS
17018 first = RELAX_FIRST (subtype);
17019 second = RELAX_SECOND (subtype);
4d7206a2 17020 fixp = (fixS *) fragp->fr_opcode;
252b5132 17021
df58fc94
RS
17022 /* If the delay slot chosen does not match the size of the instruction,
17023 then emit a warning. */
17024 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
17025 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
17026 {
17027 relax_substateT s;
17028 const char *msg;
17029
17030 s = subtype & (RELAX_DELAY_SLOT_16BIT
17031 | RELAX_DELAY_SLOT_SIZE_FIRST
17032 | RELAX_DELAY_SLOT_SIZE_SECOND);
17033 msg = macro_warning (s);
17034 if (msg != NULL)
db9b2be4 17035 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94
RS
17036 subtype &= ~s;
17037 }
17038
584892a6 17039 /* Possibly emit a warning if we've chosen the longer option. */
df58fc94 17040 if (use_second == second_longer)
584892a6 17041 {
df58fc94
RS
17042 relax_substateT s;
17043 const char *msg;
17044
17045 s = (subtype
17046 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
17047 msg = macro_warning (s);
17048 if (msg != NULL)
db9b2be4 17049 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94 17050 subtype &= ~s;
584892a6
RS
17051 }
17052
4d7206a2
RS
17053 /* Go through all the fixups for the first sequence. Disable them
17054 (by marking them as done) if we're going to use the second
17055 sequence instead. */
17056 while (fixp
17057 && fixp->fx_frag == fragp
17058 && fixp->fx_where < fragp->fr_fix - second)
17059 {
df58fc94 17060 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
17061 fixp->fx_done = 1;
17062 fixp = fixp->fx_next;
17063 }
252b5132 17064
4d7206a2
RS
17065 /* Go through the fixups for the second sequence. Disable them if
17066 we're going to use the first sequence, otherwise adjust their
17067 addresses to account for the relaxation. */
17068 while (fixp && fixp->fx_frag == fragp)
17069 {
df58fc94 17070 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
17071 fixp->fx_where -= first;
17072 else
17073 fixp->fx_done = 1;
17074 fixp = fixp->fx_next;
17075 }
17076
17077 /* Now modify the frag contents. */
df58fc94 17078 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
17079 {
17080 char *start;
17081
17082 start = fragp->fr_literal + fragp->fr_fix - first - second;
17083 memmove (start, start + first, second);
17084 fragp->fr_fix -= first;
17085 }
17086 else
17087 fragp->fr_fix -= second;
252b5132
RH
17088 }
17089}
17090
252b5132
RH
17091/* This function is called after the relocs have been generated.
17092 We've been storing mips16 text labels as odd. Here we convert them
17093 back to even for the convenience of the debugger. */
17094
17095void
17a2f251 17096mips_frob_file_after_relocs (void)
252b5132
RH
17097{
17098 asymbol **syms;
17099 unsigned int count, i;
17100
252b5132
RH
17101 syms = bfd_get_outsymbols (stdoutput);
17102 count = bfd_get_symcount (stdoutput);
17103 for (i = 0; i < count; i++, syms++)
df58fc94
RS
17104 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
17105 && ((*syms)->value & 1) != 0)
17106 {
17107 (*syms)->value &= ~1;
17108 /* If the symbol has an odd size, it was probably computed
17109 incorrectly, so adjust that as well. */
17110 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
17111 ++elf_symbol (*syms)->internal_elf_sym.st_size;
17112 }
252b5132
RH
17113}
17114
a1facbec
MR
17115/* This function is called whenever a label is defined, including fake
17116 labels instantiated off the dot special symbol. It is used when
17117 handling branch delays; if a branch has a label, we assume we cannot
17118 move it. This also bumps the value of the symbol by 1 in compressed
17119 code. */
252b5132 17120
e1b47bd5 17121static void
a1facbec 17122mips_record_label (symbolS *sym)
252b5132 17123{
a8dbcb85 17124 segment_info_type *si = seg_info (now_seg);
252b5132
RH
17125 struct insn_label_list *l;
17126
17127 if (free_insn_labels == NULL)
17128 l = (struct insn_label_list *) xmalloc (sizeof *l);
17129 else
17130 {
17131 l = free_insn_labels;
17132 free_insn_labels = l->next;
17133 }
17134
17135 l->label = sym;
a8dbcb85
TS
17136 l->next = si->label_list;
17137 si->label_list = l;
a1facbec 17138}
07a53e5c 17139
a1facbec
MR
17140/* This function is called as tc_frob_label() whenever a label is defined
17141 and adds a DWARF-2 record we only want for true labels. */
17142
17143void
17144mips_define_label (symbolS *sym)
17145{
17146 mips_record_label (sym);
07a53e5c 17147 dwarf2_emit_label (sym);
252b5132 17148}
e1b47bd5
RS
17149
17150/* This function is called by tc_new_dot_label whenever a new dot symbol
17151 is defined. */
17152
17153void
17154mips_add_dot_label (symbolS *sym)
17155{
17156 mips_record_label (sym);
17157 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
17158 mips_compressed_mark_label (sym);
17159}
252b5132 17160\f
252b5132
RH
17161/* Some special processing for a MIPS ELF file. */
17162
17163void
17a2f251 17164mips_elf_final_processing (void)
252b5132
RH
17165{
17166 /* Write out the register information. */
316f5878 17167 if (mips_abi != N64_ABI)
252b5132
RH
17168 {
17169 Elf32_RegInfo s;
17170
17171 s.ri_gprmask = mips_gprmask;
17172 s.ri_cprmask[0] = mips_cprmask[0];
17173 s.ri_cprmask[1] = mips_cprmask[1];
17174 s.ri_cprmask[2] = mips_cprmask[2];
17175 s.ri_cprmask[3] = mips_cprmask[3];
17176 /* The gp_value field is set by the MIPS ELF backend. */
17177
17178 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
17179 ((Elf32_External_RegInfo *)
17180 mips_regmask_frag));
17181 }
17182 else
17183 {
17184 Elf64_Internal_RegInfo s;
17185
17186 s.ri_gprmask = mips_gprmask;
17187 s.ri_pad = 0;
17188 s.ri_cprmask[0] = mips_cprmask[0];
17189 s.ri_cprmask[1] = mips_cprmask[1];
17190 s.ri_cprmask[2] = mips_cprmask[2];
17191 s.ri_cprmask[3] = mips_cprmask[3];
17192 /* The gp_value field is set by the MIPS ELF backend. */
17193
17194 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
17195 ((Elf64_External_RegInfo *)
17196 mips_regmask_frag));
17197 }
17198
17199 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
17200 sort of BFD interface for this. */
17201 if (mips_any_noreorder)
17202 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
17203 if (mips_pic != NO_PIC)
143d77c5 17204 {
8b828383 17205 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
17206 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
17207 }
17208 if (mips_abicalls)
17209 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 17210
b015e599
AP
17211 /* Set MIPS ELF flags for ASEs. Note that not all ASEs have flags
17212 defined at present; this might need to change in future. */
a4672219
TS
17213 if (file_ase_mips16)
17214 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
df58fc94
RS
17215 if (file_ase_micromips)
17216 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
846ef2d0 17217 if (file_ase & ASE_MDMX)
deec1734 17218 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 17219
bdaaa2e1 17220 /* Set the MIPS ELF ABI flags. */
316f5878 17221 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 17222 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 17223 else if (mips_abi == O64_ABI)
252b5132 17224 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 17225 else if (mips_abi == EABI_ABI)
252b5132 17226 {
316f5878 17227 if (!file_mips_gp32)
252b5132
RH
17228 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
17229 else
17230 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
17231 }
316f5878 17232 else if (mips_abi == N32_ABI)
be00bddd
TS
17233 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
17234
c9914766 17235 /* Nothing to do for N64_ABI. */
252b5132
RH
17236
17237 if (mips_32bitmode)
17238 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
ad3fea08 17239
ba92f887
MR
17240 if (mips_flag_nan2008)
17241 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NAN2008;
17242
ad3fea08
TS
17243#if 0 /* XXX FIXME */
17244 /* 32 bit code with 64 bit FP registers. */
17245 if (!file_mips_fp32 && ABI_NEEDS_32BIT_REGS (mips_abi))
17246 elf_elfheader (stdoutput)->e_flags |= ???;
17247#endif
252b5132 17248}
252b5132 17249\f
beae10d5 17250typedef struct proc {
9b2f1d35
EC
17251 symbolS *func_sym;
17252 symbolS *func_end_sym;
beae10d5
KH
17253 unsigned long reg_mask;
17254 unsigned long reg_offset;
17255 unsigned long fpreg_mask;
17256 unsigned long fpreg_offset;
17257 unsigned long frame_offset;
17258 unsigned long frame_reg;
17259 unsigned long pc_reg;
17260} procS;
252b5132
RH
17261
17262static procS cur_proc;
17263static procS *cur_proc_ptr;
17264static int numprocs;
17265
df58fc94
RS
17266/* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
17267 as "2", and a normal nop as "0". */
17268
17269#define NOP_OPCODE_MIPS 0
17270#define NOP_OPCODE_MIPS16 1
17271#define NOP_OPCODE_MICROMIPS 2
742a56fe
RS
17272
17273char
17274mips_nop_opcode (void)
17275{
df58fc94
RS
17276 if (seg_info (now_seg)->tc_segment_info_data.micromips)
17277 return NOP_OPCODE_MICROMIPS;
17278 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
17279 return NOP_OPCODE_MIPS16;
17280 else
17281 return NOP_OPCODE_MIPS;
742a56fe
RS
17282}
17283
df58fc94
RS
17284/* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
17285 32-bit microMIPS NOPs here (if applicable). */
a19d8eb0 17286
0a9ef439 17287void
17a2f251 17288mips_handle_align (fragS *fragp)
a19d8eb0 17289{
df58fc94 17290 char nop_opcode;
742a56fe 17291 char *p;
c67a084a
NC
17292 int bytes, size, excess;
17293 valueT opcode;
742a56fe 17294
0a9ef439
RH
17295 if (fragp->fr_type != rs_align_code)
17296 return;
17297
742a56fe 17298 p = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
17299 nop_opcode = *p;
17300 switch (nop_opcode)
a19d8eb0 17301 {
df58fc94
RS
17302 case NOP_OPCODE_MICROMIPS:
17303 opcode = micromips_nop32_insn.insn_opcode;
17304 size = 4;
17305 break;
17306 case NOP_OPCODE_MIPS16:
c67a084a
NC
17307 opcode = mips16_nop_insn.insn_opcode;
17308 size = 2;
df58fc94
RS
17309 break;
17310 case NOP_OPCODE_MIPS:
17311 default:
c67a084a
NC
17312 opcode = nop_insn.insn_opcode;
17313 size = 4;
df58fc94 17314 break;
c67a084a 17315 }
a19d8eb0 17316
c67a084a
NC
17317 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
17318 excess = bytes % size;
df58fc94
RS
17319
17320 /* Handle the leading part if we're not inserting a whole number of
17321 instructions, and make it the end of the fixed part of the frag.
17322 Try to fit in a short microMIPS NOP if applicable and possible,
17323 and use zeroes otherwise. */
17324 gas_assert (excess < 4);
17325 fragp->fr_fix += excess;
17326 switch (excess)
c67a084a 17327 {
df58fc94
RS
17328 case 3:
17329 *p++ = '\0';
17330 /* Fall through. */
17331 case 2:
833794fc 17332 if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
df58fc94 17333 {
4d68580a 17334 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
df58fc94
RS
17335 break;
17336 }
17337 *p++ = '\0';
17338 /* Fall through. */
17339 case 1:
17340 *p++ = '\0';
17341 /* Fall through. */
17342 case 0:
17343 break;
a19d8eb0 17344 }
c67a084a
NC
17345
17346 md_number_to_chars (p, opcode, size);
17347 fragp->fr_var = size;
a19d8eb0
CP
17348}
17349
252b5132 17350static void
17a2f251 17351md_obj_begin (void)
252b5132
RH
17352{
17353}
17354
17355static void
17a2f251 17356md_obj_end (void)
252b5132 17357{
54f4ddb3 17358 /* Check for premature end, nesting errors, etc. */
252b5132 17359 if (cur_proc_ptr)
9a41af64 17360 as_warn (_("missing .end at end of assembly"));
252b5132
RH
17361}
17362
17363static long
17a2f251 17364get_number (void)
252b5132
RH
17365{
17366 int negative = 0;
17367 long val = 0;
17368
17369 if (*input_line_pointer == '-')
17370 {
17371 ++input_line_pointer;
17372 negative = 1;
17373 }
3882b010 17374 if (!ISDIGIT (*input_line_pointer))
956cd1d6 17375 as_bad (_("expected simple number"));
252b5132
RH
17376 if (input_line_pointer[0] == '0')
17377 {
17378 if (input_line_pointer[1] == 'x')
17379 {
17380 input_line_pointer += 2;
3882b010 17381 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
17382 {
17383 val <<= 4;
17384 val |= hex_value (*input_line_pointer++);
17385 }
17386 return negative ? -val : val;
17387 }
17388 else
17389 {
17390 ++input_line_pointer;
3882b010 17391 while (ISDIGIT (*input_line_pointer))
252b5132
RH
17392 {
17393 val <<= 3;
17394 val |= *input_line_pointer++ - '0';
17395 }
17396 return negative ? -val : val;
17397 }
17398 }
3882b010 17399 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
17400 {
17401 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
17402 *input_line_pointer, *input_line_pointer);
956cd1d6 17403 as_warn (_("invalid number"));
252b5132
RH
17404 return -1;
17405 }
3882b010 17406 while (ISDIGIT (*input_line_pointer))
252b5132
RH
17407 {
17408 val *= 10;
17409 val += *input_line_pointer++ - '0';
17410 }
17411 return negative ? -val : val;
17412}
17413
17414/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
17415 is an initial number which is the ECOFF file index. In the non-ECOFF
17416 case .file implies DWARF-2. */
17417
17418static void
17a2f251 17419s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 17420{
ecb4347a
DJ
17421 static int first_file_directive = 0;
17422
c5dd6aab
DJ
17423 if (ECOFF_DEBUGGING)
17424 {
17425 get_number ();
17426 s_app_file (0);
17427 }
17428 else
ecb4347a
DJ
17429 {
17430 char *filename;
17431
17432 filename = dwarf2_directive_file (0);
17433
17434 /* Versions of GCC up to 3.1 start files with a ".file"
17435 directive even for stabs output. Make sure that this
17436 ".file" is handled. Note that you need a version of GCC
17437 after 3.1 in order to support DWARF-2 on MIPS. */
17438 if (filename != NULL && ! first_file_directive)
17439 {
17440 (void) new_logical_line (filename, -1);
c04f5787 17441 s_app_file_string (filename, 0);
ecb4347a
DJ
17442 }
17443 first_file_directive = 1;
17444 }
c5dd6aab
DJ
17445}
17446
17447/* The .loc directive, implying DWARF-2. */
252b5132
RH
17448
17449static void
17a2f251 17450s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 17451{
c5dd6aab
DJ
17452 if (!ECOFF_DEBUGGING)
17453 dwarf2_directive_loc (0);
252b5132
RH
17454}
17455
252b5132
RH
17456/* The .end directive. */
17457
17458static void
17a2f251 17459s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
17460{
17461 symbolS *p;
252b5132 17462
7a621144
DJ
17463 /* Following functions need their own .frame and .cprestore directives. */
17464 mips_frame_reg_valid = 0;
17465 mips_cprestore_valid = 0;
17466
252b5132
RH
17467 if (!is_end_of_line[(unsigned char) *input_line_pointer])
17468 {
17469 p = get_symbol ();
17470 demand_empty_rest_of_line ();
17471 }
17472 else
17473 p = NULL;
17474
14949570 17475 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
17476 as_warn (_(".end not in text section"));
17477
17478 if (!cur_proc_ptr)
17479 {
17480 as_warn (_(".end directive without a preceding .ent directive."));
17481 demand_empty_rest_of_line ();
17482 return;
17483 }
17484
17485 if (p != NULL)
17486 {
9c2799c2 17487 gas_assert (S_GET_NAME (p));
9b2f1d35 17488 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
252b5132 17489 as_warn (_(".end symbol does not match .ent symbol."));
ecb4347a
DJ
17490
17491 if (debug_type == DEBUG_STABS)
17492 stabs_generate_asm_endfunc (S_GET_NAME (p),
17493 S_GET_NAME (p));
252b5132
RH
17494 }
17495 else
17496 as_warn (_(".end directive missing or unknown symbol"));
17497
9b2f1d35
EC
17498 /* Create an expression to calculate the size of the function. */
17499 if (p && cur_proc_ptr)
17500 {
17501 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
17502 expressionS *exp = xmalloc (sizeof (expressionS));
17503
17504 obj->size = exp;
17505 exp->X_op = O_subtract;
17506 exp->X_add_symbol = symbol_temp_new_now ();
17507 exp->X_op_symbol = p;
17508 exp->X_add_number = 0;
17509
17510 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
17511 }
17512
ecb4347a 17513 /* Generate a .pdr section. */
f3ded42a 17514 if (!ECOFF_DEBUGGING && mips_flag_pdr)
ecb4347a
DJ
17515 {
17516 segT saved_seg = now_seg;
17517 subsegT saved_subseg = now_subseg;
ecb4347a
DJ
17518 expressionS exp;
17519 char *fragp;
252b5132 17520
252b5132 17521#ifdef md_flush_pending_output
ecb4347a 17522 md_flush_pending_output ();
252b5132
RH
17523#endif
17524
9c2799c2 17525 gas_assert (pdr_seg);
ecb4347a 17526 subseg_set (pdr_seg, 0);
252b5132 17527
ecb4347a
DJ
17528 /* Write the symbol. */
17529 exp.X_op = O_symbol;
17530 exp.X_add_symbol = p;
17531 exp.X_add_number = 0;
17532 emit_expr (&exp, 4);
252b5132 17533
ecb4347a 17534 fragp = frag_more (7 * 4);
252b5132 17535
17a2f251
TS
17536 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
17537 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
17538 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
17539 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
17540 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
17541 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
17542 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 17543
ecb4347a
DJ
17544 subseg_set (saved_seg, saved_subseg);
17545 }
252b5132
RH
17546
17547 cur_proc_ptr = NULL;
17548}
17549
17550/* The .aent and .ent directives. */
17551
17552static void
17a2f251 17553s_mips_ent (int aent)
252b5132 17554{
252b5132 17555 symbolS *symbolP;
252b5132
RH
17556
17557 symbolP = get_symbol ();
17558 if (*input_line_pointer == ',')
f9419b05 17559 ++input_line_pointer;
252b5132 17560 SKIP_WHITESPACE ();
3882b010 17561 if (ISDIGIT (*input_line_pointer)
d9a62219 17562 || *input_line_pointer == '-')
874e8986 17563 get_number ();
252b5132 17564
14949570 17565 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
17566 as_warn (_(".ent or .aent not in text section."));
17567
17568 if (!aent && cur_proc_ptr)
9a41af64 17569 as_warn (_("missing .end"));
252b5132
RH
17570
17571 if (!aent)
17572 {
7a621144
DJ
17573 /* This function needs its own .frame and .cprestore directives. */
17574 mips_frame_reg_valid = 0;
17575 mips_cprestore_valid = 0;
17576
252b5132
RH
17577 cur_proc_ptr = &cur_proc;
17578 memset (cur_proc_ptr, '\0', sizeof (procS));
17579
9b2f1d35 17580 cur_proc_ptr->func_sym = symbolP;
252b5132 17581
f9419b05 17582 ++numprocs;
ecb4347a
DJ
17583
17584 if (debug_type == DEBUG_STABS)
17585 stabs_generate_asm_func (S_GET_NAME (symbolP),
17586 S_GET_NAME (symbolP));
252b5132
RH
17587 }
17588
7c0fc524
MR
17589 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
17590
252b5132
RH
17591 demand_empty_rest_of_line ();
17592}
17593
17594/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 17595 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 17596 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 17597 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
17598 symbol table (in the mdebug section). */
17599
17600static void
17a2f251 17601s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 17602{
f3ded42a
RS
17603 if (ECOFF_DEBUGGING)
17604 s_ignore (ignore);
17605 else
ecb4347a
DJ
17606 {
17607 long val;
252b5132 17608
ecb4347a
DJ
17609 if (cur_proc_ptr == (procS *) NULL)
17610 {
17611 as_warn (_(".frame outside of .ent"));
17612 demand_empty_rest_of_line ();
17613 return;
17614 }
252b5132 17615
ecb4347a
DJ
17616 cur_proc_ptr->frame_reg = tc_get_register (1);
17617
17618 SKIP_WHITESPACE ();
17619 if (*input_line_pointer++ != ','
17620 || get_absolute_expression_and_terminator (&val) != ',')
17621 {
17622 as_warn (_("Bad .frame directive"));
17623 --input_line_pointer;
17624 demand_empty_rest_of_line ();
17625 return;
17626 }
252b5132 17627
ecb4347a
DJ
17628 cur_proc_ptr->frame_offset = val;
17629 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 17630
252b5132 17631 demand_empty_rest_of_line ();
252b5132 17632 }
252b5132
RH
17633}
17634
bdaaa2e1
KH
17635/* The .fmask and .mask directives. If the mdebug section is present
17636 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 17637 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 17638 information correctly. We can't use the ecoff routines because they
252b5132
RH
17639 make reference to the ecoff symbol table (in the mdebug section). */
17640
17641static void
17a2f251 17642s_mips_mask (int reg_type)
252b5132 17643{
f3ded42a
RS
17644 if (ECOFF_DEBUGGING)
17645 s_ignore (reg_type);
17646 else
252b5132 17647 {
ecb4347a 17648 long mask, off;
252b5132 17649
ecb4347a
DJ
17650 if (cur_proc_ptr == (procS *) NULL)
17651 {
17652 as_warn (_(".mask/.fmask outside of .ent"));
17653 demand_empty_rest_of_line ();
17654 return;
17655 }
252b5132 17656
ecb4347a
DJ
17657 if (get_absolute_expression_and_terminator (&mask) != ',')
17658 {
17659 as_warn (_("Bad .mask/.fmask directive"));
17660 --input_line_pointer;
17661 demand_empty_rest_of_line ();
17662 return;
17663 }
252b5132 17664
ecb4347a
DJ
17665 off = get_absolute_expression ();
17666
17667 if (reg_type == 'F')
17668 {
17669 cur_proc_ptr->fpreg_mask = mask;
17670 cur_proc_ptr->fpreg_offset = off;
17671 }
17672 else
17673 {
17674 cur_proc_ptr->reg_mask = mask;
17675 cur_proc_ptr->reg_offset = off;
17676 }
17677
17678 demand_empty_rest_of_line ();
252b5132 17679 }
252b5132
RH
17680}
17681
316f5878
RS
17682/* A table describing all the processors gas knows about. Names are
17683 matched in the order listed.
e7af610e 17684
316f5878
RS
17685 To ease comparison, please keep this table in the same order as
17686 gcc's mips_cpu_info_table[]. */
e972090a
NC
17687static const struct mips_cpu_info mips_cpu_info_table[] =
17688{
316f5878 17689 /* Entries for generic ISAs */
d16afab6
RS
17690 { "mips1", MIPS_CPU_IS_ISA, 0, ISA_MIPS1, CPU_R3000 },
17691 { "mips2", MIPS_CPU_IS_ISA, 0, ISA_MIPS2, CPU_R6000 },
17692 { "mips3", MIPS_CPU_IS_ISA, 0, ISA_MIPS3, CPU_R4000 },
17693 { "mips4", MIPS_CPU_IS_ISA, 0, ISA_MIPS4, CPU_R8000 },
17694 { "mips5", MIPS_CPU_IS_ISA, 0, ISA_MIPS5, CPU_MIPS5 },
17695 { "mips32", MIPS_CPU_IS_ISA, 0, ISA_MIPS32, CPU_MIPS32 },
17696 { "mips32r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17697 { "mips64", MIPS_CPU_IS_ISA, 0, ISA_MIPS64, CPU_MIPS64 },
17698 { "mips64r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R2, CPU_MIPS64R2 },
316f5878
RS
17699
17700 /* MIPS I */
d16afab6
RS
17701 { "r3000", 0, 0, ISA_MIPS1, CPU_R3000 },
17702 { "r2000", 0, 0, ISA_MIPS1, CPU_R3000 },
17703 { "r3900", 0, 0, ISA_MIPS1, CPU_R3900 },
316f5878
RS
17704
17705 /* MIPS II */
d16afab6 17706 { "r6000", 0, 0, ISA_MIPS2, CPU_R6000 },
316f5878
RS
17707
17708 /* MIPS III */
d16afab6
RS
17709 { "r4000", 0, 0, ISA_MIPS3, CPU_R4000 },
17710 { "r4010", 0, 0, ISA_MIPS2, CPU_R4010 },
17711 { "vr4100", 0, 0, ISA_MIPS3, CPU_VR4100 },
17712 { "vr4111", 0, 0, ISA_MIPS3, CPU_R4111 },
17713 { "vr4120", 0, 0, ISA_MIPS3, CPU_VR4120 },
17714 { "vr4130", 0, 0, ISA_MIPS3, CPU_VR4120 },
17715 { "vr4181", 0, 0, ISA_MIPS3, CPU_R4111 },
17716 { "vr4300", 0, 0, ISA_MIPS3, CPU_R4300 },
17717 { "r4400", 0, 0, ISA_MIPS3, CPU_R4400 },
17718 { "r4600", 0, 0, ISA_MIPS3, CPU_R4600 },
17719 { "orion", 0, 0, ISA_MIPS3, CPU_R4600 },
17720 { "r4650", 0, 0, ISA_MIPS3, CPU_R4650 },
17721 { "r5900", 0, 0, ISA_MIPS3, CPU_R5900 },
b15591bb 17722 /* ST Microelectronics Loongson 2E and 2F cores */
d16afab6
RS
17723 { "loongson2e", 0, 0, ISA_MIPS3, CPU_LOONGSON_2E },
17724 { "loongson2f", 0, 0, ISA_MIPS3, CPU_LOONGSON_2F },
316f5878
RS
17725
17726 /* MIPS IV */
d16afab6
RS
17727 { "r8000", 0, 0, ISA_MIPS4, CPU_R8000 },
17728 { "r10000", 0, 0, ISA_MIPS4, CPU_R10000 },
17729 { "r12000", 0, 0, ISA_MIPS4, CPU_R12000 },
17730 { "r14000", 0, 0, ISA_MIPS4, CPU_R14000 },
17731 { "r16000", 0, 0, ISA_MIPS4, CPU_R16000 },
17732 { "vr5000", 0, 0, ISA_MIPS4, CPU_R5000 },
17733 { "vr5400", 0, 0, ISA_MIPS4, CPU_VR5400 },
17734 { "vr5500", 0, 0, ISA_MIPS4, CPU_VR5500 },
17735 { "rm5200", 0, 0, ISA_MIPS4, CPU_R5000 },
17736 { "rm5230", 0, 0, ISA_MIPS4, CPU_R5000 },
17737 { "rm5231", 0, 0, ISA_MIPS4, CPU_R5000 },
17738 { "rm5261", 0, 0, ISA_MIPS4, CPU_R5000 },
17739 { "rm5721", 0, 0, ISA_MIPS4, CPU_R5000 },
17740 { "rm7000", 0, 0, ISA_MIPS4, CPU_RM7000 },
17741 { "rm9000", 0, 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
17742
17743 /* MIPS 32 */
d16afab6
RS
17744 { "4kc", 0, 0, ISA_MIPS32, CPU_MIPS32 },
17745 { "4km", 0, 0, ISA_MIPS32, CPU_MIPS32 },
17746 { "4kp", 0, 0, ISA_MIPS32, CPU_MIPS32 },
17747 { "4ksc", 0, ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
ad3fea08
TS
17748
17749 /* MIPS 32 Release 2 */
d16afab6
RS
17750 { "4kec", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17751 { "4kem", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17752 { "4kep", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17753 { "4ksd", 0, ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
17754 { "m4k", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17755 { "m4kp", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17756 { "m14k", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
17757 { "m14kc", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
17758 { "m14ke", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
17759 ISA_MIPS32R2, CPU_MIPS32R2 },
17760 { "m14kec", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
17761 ISA_MIPS32R2, CPU_MIPS32R2 },
17762 { "24kc", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17763 { "24kf2_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17764 { "24kf", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17765 { "24kf1_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 17766 /* Deprecated forms of the above. */
d16afab6
RS
17767 { "24kfx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17768 { "24kx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 17769 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
d16afab6
RS
17770 { "24kec", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
17771 { "24kef2_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
17772 { "24kef", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
17773 { "24kef1_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 17774 /* Deprecated forms of the above. */
d16afab6
RS
17775 { "24kefx", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
17776 { "24kex", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 17777 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
d16afab6
RS
17778 { "34kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17779 { "34kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17780 { "34kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17781 { "34kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 17782 /* Deprecated forms of the above. */
d16afab6
RS
17783 { "34kfx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17784 { "34kx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
711eefe4 17785 /* 34Kn is a 34kc without DSP. */
d16afab6 17786 { "34kn", 0, ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 17787 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
d16afab6
RS
17788 { "74kc", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
17789 { "74kf2_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
17790 { "74kf", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
17791 { "74kf1_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
17792 { "74kf3_2", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 17793 /* Deprecated forms of the above. */
d16afab6
RS
17794 { "74kfx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
17795 { "74kx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
30f8113a 17796 /* 1004K cores are multiprocessor versions of the 34K. */
d16afab6
RS
17797 { "1004kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17798 { "1004kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17799 { "1004kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17800 { "1004kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
32b26a03 17801
316f5878 17802 /* MIPS 64 */
d16afab6
RS
17803 { "5kc", 0, 0, ISA_MIPS64, CPU_MIPS64 },
17804 { "5kf", 0, 0, ISA_MIPS64, CPU_MIPS64 },
17805 { "20kc", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
17806 { "25kf", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
ad3fea08 17807
c7a23324 17808 /* Broadcom SB-1 CPU core */
d16afab6 17809 { "sb1", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
1e85aad8 17810 /* Broadcom SB-1A CPU core */
d16afab6 17811 { "sb1a", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
d051516a 17812
d16afab6 17813 { "loongson3a", 0, 0, ISA_MIPS64, CPU_LOONGSON_3A },
e7af610e 17814
ed163775
MR
17815 /* MIPS 64 Release 2 */
17816
967344c6 17817 /* Cavium Networks Octeon CPU core */
d16afab6
RS
17818 { "octeon", 0, 0, ISA_MIPS64R2, CPU_OCTEON },
17819 { "octeon+", 0, 0, ISA_MIPS64R2, CPU_OCTEONP },
17820 { "octeon2", 0, 0, ISA_MIPS64R2, CPU_OCTEON2 },
967344c6 17821
52b6b6b9 17822 /* RMI Xlr */
d16afab6 17823 { "xlr", 0, 0, ISA_MIPS64, CPU_XLR },
52b6b6b9 17824
55a36193
MK
17825 /* Broadcom XLP.
17826 XLP is mostly like XLR, with the prominent exception that it is
17827 MIPS64R2 rather than MIPS64. */
d16afab6 17828 { "xlp", 0, 0, ISA_MIPS64R2, CPU_XLR },
55a36193 17829
316f5878 17830 /* End marker */
d16afab6 17831 { NULL, 0, 0, 0, 0 }
316f5878 17832};
e7af610e 17833
84ea6cf2 17834
316f5878
RS
17835/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
17836 with a final "000" replaced by "k". Ignore case.
e7af610e 17837
316f5878 17838 Note: this function is shared between GCC and GAS. */
c6c98b38 17839
b34976b6 17840static bfd_boolean
17a2f251 17841mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
17842{
17843 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
17844 given++, canonical++;
17845
17846 return ((*given == 0 && *canonical == 0)
17847 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
17848}
17849
17850
17851/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
17852 CPU name. We've traditionally allowed a lot of variation here.
17853
17854 Note: this function is shared between GCC and GAS. */
17855
b34976b6 17856static bfd_boolean
17a2f251 17857mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
17858{
17859 /* First see if the name matches exactly, or with a final "000"
17860 turned into "k". */
17861 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 17862 return TRUE;
316f5878
RS
17863
17864 /* If not, try comparing based on numerical designation alone.
17865 See if GIVEN is an unadorned number, or 'r' followed by a number. */
17866 if (TOLOWER (*given) == 'r')
17867 given++;
17868 if (!ISDIGIT (*given))
b34976b6 17869 return FALSE;
316f5878
RS
17870
17871 /* Skip over some well-known prefixes in the canonical name,
17872 hoping to find a number there too. */
17873 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
17874 canonical += 2;
17875 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
17876 canonical += 2;
17877 else if (TOLOWER (canonical[0]) == 'r')
17878 canonical += 1;
17879
17880 return mips_strict_matching_cpu_name_p (canonical, given);
17881}
17882
17883
17884/* Parse an option that takes the name of a processor as its argument.
17885 OPTION is the name of the option and CPU_STRING is the argument.
17886 Return the corresponding processor enumeration if the CPU_STRING is
17887 recognized, otherwise report an error and return null.
17888
17889 A similar function exists in GCC. */
e7af610e
NC
17890
17891static const struct mips_cpu_info *
17a2f251 17892mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 17893{
316f5878 17894 const struct mips_cpu_info *p;
e7af610e 17895
316f5878
RS
17896 /* 'from-abi' selects the most compatible architecture for the given
17897 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
17898 EABIs, we have to decide whether we're using the 32-bit or 64-bit
17899 version. Look first at the -mgp options, if given, otherwise base
17900 the choice on MIPS_DEFAULT_64BIT.
e7af610e 17901
316f5878
RS
17902 Treat NO_ABI like the EABIs. One reason to do this is that the
17903 plain 'mips' and 'mips64' configs have 'from-abi' as their default
17904 architecture. This code picks MIPS I for 'mips' and MIPS III for
17905 'mips64', just as we did in the days before 'from-abi'. */
17906 if (strcasecmp (cpu_string, "from-abi") == 0)
17907 {
17908 if (ABI_NEEDS_32BIT_REGS (mips_abi))
17909 return mips_cpu_info_from_isa (ISA_MIPS1);
17910
17911 if (ABI_NEEDS_64BIT_REGS (mips_abi))
17912 return mips_cpu_info_from_isa (ISA_MIPS3);
17913
17914 if (file_mips_gp32 >= 0)
17915 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
17916
17917 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
17918 ? ISA_MIPS3
17919 : ISA_MIPS1);
17920 }
17921
17922 /* 'default' has traditionally been a no-op. Probably not very useful. */
17923 if (strcasecmp (cpu_string, "default") == 0)
17924 return 0;
17925
17926 for (p = mips_cpu_info_table; p->name != 0; p++)
17927 if (mips_matching_cpu_name_p (p->name, cpu_string))
17928 return p;
17929
20203fb9 17930 as_bad (_("Bad value (%s) for %s"), cpu_string, option);
316f5878 17931 return 0;
e7af610e
NC
17932}
17933
316f5878
RS
17934/* Return the canonical processor information for ISA (a member of the
17935 ISA_MIPS* enumeration). */
17936
e7af610e 17937static const struct mips_cpu_info *
17a2f251 17938mips_cpu_info_from_isa (int isa)
e7af610e
NC
17939{
17940 int i;
17941
17942 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
ad3fea08 17943 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
316f5878 17944 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
17945 return (&mips_cpu_info_table[i]);
17946
e972090a 17947 return NULL;
e7af610e 17948}
fef14a42
TS
17949
17950static const struct mips_cpu_info *
17a2f251 17951mips_cpu_info_from_arch (int arch)
fef14a42
TS
17952{
17953 int i;
17954
17955 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
17956 if (arch == mips_cpu_info_table[i].cpu)
17957 return (&mips_cpu_info_table[i]);
17958
17959 return NULL;
17960}
316f5878
RS
17961\f
17962static void
17a2f251 17963show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
17964{
17965 if (*first_p)
17966 {
17967 fprintf (stream, "%24s", "");
17968 *col_p = 24;
17969 }
17970 else
17971 {
17972 fprintf (stream, ", ");
17973 *col_p += 2;
17974 }
e7af610e 17975
316f5878
RS
17976 if (*col_p + strlen (string) > 72)
17977 {
17978 fprintf (stream, "\n%24s", "");
17979 *col_p = 24;
17980 }
17981
17982 fprintf (stream, "%s", string);
17983 *col_p += strlen (string);
17984
17985 *first_p = 0;
17986}
17987
17988void
17a2f251 17989md_show_usage (FILE *stream)
e7af610e 17990{
316f5878
RS
17991 int column, first;
17992 size_t i;
17993
17994 fprintf (stream, _("\
17995MIPS options:\n\
316f5878
RS
17996-EB generate big endian output\n\
17997-EL generate little endian output\n\
17998-g, -g2 do not remove unneeded NOPs or swap branches\n\
17999-G NUM allow referencing objects up to NUM bytes\n\
18000 implicitly with the gp register [default 8]\n"));
18001 fprintf (stream, _("\
18002-mips1 generate MIPS ISA I instructions\n\
18003-mips2 generate MIPS ISA II instructions\n\
18004-mips3 generate MIPS ISA III instructions\n\
18005-mips4 generate MIPS ISA IV instructions\n\
18006-mips5 generate MIPS ISA V instructions\n\
18007-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 18008-mips32r2 generate MIPS32 release 2 ISA instructions\n\
316f5878 18009-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 18010-mips64r2 generate MIPS64 release 2 ISA instructions\n\
316f5878
RS
18011-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
18012
18013 first = 1;
e7af610e
NC
18014
18015 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
18016 show (stream, mips_cpu_info_table[i].name, &column, &first);
18017 show (stream, "from-abi", &column, &first);
18018 fputc ('\n', stream);
e7af610e 18019
316f5878
RS
18020 fprintf (stream, _("\
18021-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
18022-no-mCPU don't generate code specific to CPU.\n\
18023 For -mCPU and -no-mCPU, CPU must be one of:\n"));
18024
18025 first = 1;
18026
18027 show (stream, "3900", &column, &first);
18028 show (stream, "4010", &column, &first);
18029 show (stream, "4100", &column, &first);
18030 show (stream, "4650", &column, &first);
18031 fputc ('\n', stream);
18032
18033 fprintf (stream, _("\
18034-mips16 generate mips16 instructions\n\
18035-no-mips16 do not generate mips16 instructions\n"));
18036 fprintf (stream, _("\
df58fc94
RS
18037-mmicromips generate microMIPS instructions\n\
18038-mno-micromips do not generate microMIPS instructions\n"));
18039 fprintf (stream, _("\
e16bfa71
TS
18040-msmartmips generate smartmips instructions\n\
18041-mno-smartmips do not generate smartmips instructions\n"));
18042 fprintf (stream, _("\
74cd071d
CF
18043-mdsp generate DSP instructions\n\
18044-mno-dsp do not generate DSP instructions\n"));
18045 fprintf (stream, _("\
8b082fb1
TS
18046-mdspr2 generate DSP R2 instructions\n\
18047-mno-dspr2 do not generate DSP R2 instructions\n"));
18048 fprintf (stream, _("\
ef2e4d86
CF
18049-mmt generate MT instructions\n\
18050-mno-mt do not generate MT instructions\n"));
18051 fprintf (stream, _("\
dec0624d
MR
18052-mmcu generate MCU instructions\n\
18053-mno-mcu do not generate MCU instructions\n"));
18054 fprintf (stream, _("\
b015e599
AP
18055-mvirt generate Virtualization instructions\n\
18056-mno-virt do not generate Virtualization instructions\n"));
18057 fprintf (stream, _("\
833794fc
MR
18058-minsn32 only generate 32-bit microMIPS instructions\n\
18059-mno-insn32 generate all microMIPS instructions\n"));
18060 fprintf (stream, _("\
c67a084a
NC
18061-mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
18062-mfix-loongson2f-nop work around Loongson2F NOP errata\n\
d766e8ec 18063-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 18064-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
6a32d874 18065-mfix-24k insert a nop after ERET and DERET instructions\n\
d954098f 18066-mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
316f5878
RS
18067-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
18068-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261 18069-msym32 assume all symbols have 32-bit values\n\
316f5878
RS
18070-O0 remove unneeded NOPs, do not swap branches\n\
18071-O remove unneeded NOPs and swap branches\n\
316f5878
RS
18072--trap, --no-break trap exception on div by 0 and mult overflow\n\
18073--break, --no-trap break exception on div by 0 and mult overflow\n"));
037b32b9
AN
18074 fprintf (stream, _("\
18075-mhard-float allow floating-point instructions\n\
18076-msoft-float do not allow floating-point instructions\n\
18077-msingle-float only allow 32-bit floating-point operations\n\
18078-mdouble-float allow 32-bit and 64-bit floating-point operations\n\
3bf0dbfb 18079--[no-]construct-floats [dis]allow floating point values to be constructed\n\
ba92f887
MR
18080--[no-]relax-branch [dis]allow out-of-range branches to be relaxed\n\
18081-mnan=ENCODING select an IEEE 754 NaN encoding convention, either of:\n"));
18082
18083 first = 1;
18084
18085 show (stream, "legacy", &column, &first);
18086 show (stream, "2008", &column, &first);
18087
18088 fputc ('\n', stream);
18089
316f5878
RS
18090 fprintf (stream, _("\
18091-KPIC, -call_shared generate SVR4 position independent code\n\
861fb55a 18092-call_nonpic generate non-PIC code that can operate with DSOs\n\
0c000745 18093-mvxworks-pic generate VxWorks position independent code\n\
861fb55a 18094-non_shared do not generate code that can operate with DSOs\n\
316f5878 18095-xgot assume a 32 bit GOT\n\
dcd410fe 18096-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8 18097-mshared, -mno-shared disable/enable .cpload optimization for\n\
d821e36b 18098 position dependent (non shared) code\n\
316f5878
RS
18099-mabi=ABI create ABI conformant object file for:\n"));
18100
18101 first = 1;
18102
18103 show (stream, "32", &column, &first);
18104 show (stream, "o64", &column, &first);
18105 show (stream, "n32", &column, &first);
18106 show (stream, "64", &column, &first);
18107 show (stream, "eabi", &column, &first);
18108
18109 fputc ('\n', stream);
18110
18111 fprintf (stream, _("\
18112-32 create o32 ABI object file (default)\n\
18113-n32 create n32 ABI object file\n\
18114-64 create 64 ABI object file\n"));
e7af610e 18115}
14e777e0 18116
1575952e 18117#ifdef TE_IRIX
14e777e0 18118enum dwarf2_format
413a266c 18119mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
14e777e0 18120{
369943fe 18121 if (HAVE_64BIT_SYMBOLS)
1575952e 18122 return dwarf2_format_64bit_irix;
14e777e0
KB
18123 else
18124 return dwarf2_format_32bit;
18125}
1575952e 18126#endif
73369e65
EC
18127
18128int
18129mips_dwarf2_addr_size (void)
18130{
6b6b3450 18131 if (HAVE_64BIT_OBJECTS)
73369e65 18132 return 8;
73369e65
EC
18133 else
18134 return 4;
18135}
5862107c
EC
18136
18137/* Standard calling conventions leave the CFA at SP on entry. */
18138void
18139mips_cfi_frame_initial_instructions (void)
18140{
18141 cfi_add_CFA_def_cfa_register (SP);
18142}
18143
707bfff6
TS
18144int
18145tc_mips_regname_to_dw2regnum (char *regname)
18146{
18147 unsigned int regnum = -1;
18148 unsigned int reg;
18149
18150 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
18151 regnum = reg;
18152
18153 return regnum;
18154}
This page took 2.795448 seconds and 4 git commands to generate.