daily update
[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
633static char *insn_error;
634
635static int auto_align = 1;
636
637/* When outputting SVR4 PIC code, the assembler needs to know the
638 offset in the stack frame from which to restore the $gp register.
639 This is set by the .cprestore pseudo-op, and saved in this
640 variable. */
641static offsetT mips_cprestore_offset = -1;
642
67c1ffbe 643/* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
6478892d 644 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 645 offset and even an other register than $gp as global pointer. */
6478892d
TS
646static offsetT mips_cpreturn_offset = -1;
647static int mips_cpreturn_register = -1;
648static int mips_gp_register = GP;
def2e0dd 649static int mips_gprel_offset = 0;
6478892d 650
7a621144
DJ
651/* Whether mips_cprestore_offset has been set in the current function
652 (or whether it has already been warned about, if not). */
653static int mips_cprestore_valid = 0;
654
252b5132
RH
655/* This is the register which holds the stack frame, as set by the
656 .frame pseudo-op. This is needed to implement .cprestore. */
657static int mips_frame_reg = SP;
658
7a621144
DJ
659/* Whether mips_frame_reg has been set in the current function
660 (or whether it has already been warned about, if not). */
661static int mips_frame_reg_valid = 0;
662
252b5132
RH
663/* To output NOP instructions correctly, we need to keep information
664 about the previous two instructions. */
665
666/* Whether we are optimizing. The default value of 2 means to remove
667 unneeded NOPs and swap branch instructions when possible. A value
668 of 1 means to not swap branches. A value of 0 means to always
669 insert NOPs. */
670static int mips_optimize = 2;
671
672/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
673 equivalent to seeing no -g option at all. */
674static int mips_debug = 0;
675
7d8e00cf
RS
676/* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
677#define MAX_VR4130_NOPS 4
678
679/* The maximum number of NOPs needed to fill delay slots. */
680#define MAX_DELAY_NOPS 2
681
682/* The maximum number of NOPs needed for any purpose. */
683#define MAX_NOPS 4
71400594
RS
684
685/* A list of previous instructions, with index 0 being the most recent.
686 We need to look back MAX_NOPS instructions when filling delay slots
687 or working around processor errata. We need to look back one
688 instruction further if we're thinking about using history[0] to
689 fill a branch delay slot. */
690static struct mips_cl_insn history[1 + MAX_NOPS];
252b5132 691
fc76e730
RS
692/* Arrays of operands for each instruction. */
693#define MAX_OPERANDS 5
694struct mips_operand_array {
695 const struct mips_operand *operand[MAX_OPERANDS];
696};
697static struct mips_operand_array *mips_operands;
698static struct mips_operand_array *mips16_operands;
699static struct mips_operand_array *micromips_operands;
700
1e915849 701/* Nop instructions used by emit_nop. */
df58fc94
RS
702static struct mips_cl_insn nop_insn;
703static struct mips_cl_insn mips16_nop_insn;
704static struct mips_cl_insn micromips_nop16_insn;
705static struct mips_cl_insn micromips_nop32_insn;
1e915849
RS
706
707/* The appropriate nop for the current mode. */
833794fc
MR
708#define NOP_INSN (mips_opts.mips16 \
709 ? &mips16_nop_insn \
710 : (mips_opts.micromips \
711 ? (mips_opts.insn32 \
712 ? &micromips_nop32_insn \
713 : &micromips_nop16_insn) \
714 : &nop_insn))
df58fc94
RS
715
716/* The size of NOP_INSN in bytes. */
833794fc
MR
717#define NOP_INSN_SIZE ((mips_opts.mips16 \
718 || (mips_opts.micromips && !mips_opts.insn32)) \
719 ? 2 : 4)
252b5132 720
252b5132
RH
721/* If this is set, it points to a frag holding nop instructions which
722 were inserted before the start of a noreorder section. If those
723 nops turn out to be unnecessary, the size of the frag can be
724 decreased. */
725static fragS *prev_nop_frag;
726
727/* The number of nop instructions we created in prev_nop_frag. */
728static int prev_nop_frag_holds;
729
730/* The number of nop instructions that we know we need in
bdaaa2e1 731 prev_nop_frag. */
252b5132
RH
732static int prev_nop_frag_required;
733
734/* The number of instructions we've seen since prev_nop_frag. */
735static int prev_nop_frag_since;
736
e8044f35
RS
737/* Relocations against symbols are sometimes done in two parts, with a HI
738 relocation and a LO relocation. Each relocation has only 16 bits of
739 space to store an addend. This means that in order for the linker to
740 handle carries correctly, it must be able to locate both the HI and
741 the LO relocation. This means that the relocations must appear in
742 order in the relocation table.
252b5132
RH
743
744 In order to implement this, we keep track of each unmatched HI
745 relocation. We then sort them so that they immediately precede the
bdaaa2e1 746 corresponding LO relocation. */
252b5132 747
e972090a
NC
748struct mips_hi_fixup
749{
252b5132
RH
750 /* Next HI fixup. */
751 struct mips_hi_fixup *next;
752 /* This fixup. */
753 fixS *fixp;
754 /* The section this fixup is in. */
755 segT seg;
756};
757
758/* The list of unmatched HI relocs. */
759
760static struct mips_hi_fixup *mips_hi_fixup_list;
761
64bdfcaf
RS
762/* The frag containing the last explicit relocation operator.
763 Null if explicit relocations have not been used. */
764
765static fragS *prev_reloc_op_frag;
766
252b5132
RH
767/* Map mips16 register numbers to normal MIPS register numbers. */
768
e972090a
NC
769static const unsigned int mips16_to_32_reg_map[] =
770{
252b5132
RH
771 16, 17, 2, 3, 4, 5, 6, 7
772};
60b63b72 773
df58fc94
RS
774/* Map microMIPS register numbers to normal MIPS register numbers. */
775
df58fc94 776#define micromips_to_32_reg_d_map mips16_to_32_reg_map
df58fc94
RS
777
778/* The microMIPS registers with type h. */
e76ff5ab 779static const unsigned int micromips_to_32_reg_h_map1[] =
df58fc94
RS
780{
781 5, 5, 6, 4, 4, 4, 4, 4
782};
e76ff5ab 783static const unsigned int micromips_to_32_reg_h_map2[] =
df58fc94
RS
784{
785 6, 7, 7, 21, 22, 5, 6, 7
786};
787
df58fc94
RS
788/* The microMIPS registers with type m. */
789static const unsigned int micromips_to_32_reg_m_map[] =
790{
791 0, 17, 2, 3, 16, 18, 19, 20
792};
793
794#define micromips_to_32_reg_n_map micromips_to_32_reg_m_map
795
71400594
RS
796/* Classifies the kind of instructions we're interested in when
797 implementing -mfix-vr4120. */
c67a084a
NC
798enum fix_vr4120_class
799{
71400594
RS
800 FIX_VR4120_MACC,
801 FIX_VR4120_DMACC,
802 FIX_VR4120_MULT,
803 FIX_VR4120_DMULT,
804 FIX_VR4120_DIV,
805 FIX_VR4120_MTHILO,
806 NUM_FIX_VR4120_CLASSES
807};
808
c67a084a
NC
809/* ...likewise -mfix-loongson2f-jump. */
810static bfd_boolean mips_fix_loongson2f_jump;
811
812/* ...likewise -mfix-loongson2f-nop. */
813static bfd_boolean mips_fix_loongson2f_nop;
814
815/* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed. */
816static bfd_boolean mips_fix_loongson2f;
817
71400594
RS
818/* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
819 there must be at least one other instruction between an instruction
820 of type X and an instruction of type Y. */
821static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
822
823/* True if -mfix-vr4120 is in force. */
d766e8ec 824static int mips_fix_vr4120;
4a6a3df4 825
7d8e00cf
RS
826/* ...likewise -mfix-vr4130. */
827static int mips_fix_vr4130;
828
6a32d874
CM
829/* ...likewise -mfix-24k. */
830static int mips_fix_24k;
831
d954098f
DD
832/* ...likewise -mfix-cn63xxp1 */
833static bfd_boolean mips_fix_cn63xxp1;
834
4a6a3df4
AO
835/* We don't relax branches by default, since this causes us to expand
836 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
837 fail to compute the offset before expanding the macro to the most
838 efficient expansion. */
839
840static int mips_relax_branch;
252b5132 841\f
4d7206a2
RS
842/* The expansion of many macros depends on the type of symbol that
843 they refer to. For example, when generating position-dependent code,
844 a macro that refers to a symbol may have two different expansions,
845 one which uses GP-relative addresses and one which uses absolute
846 addresses. When generating SVR4-style PIC, a macro may have
847 different expansions for local and global symbols.
848
849 We handle these situations by generating both sequences and putting
850 them in variant frags. In position-dependent code, the first sequence
851 will be the GP-relative one and the second sequence will be the
852 absolute one. In SVR4 PIC, the first sequence will be for global
853 symbols and the second will be for local symbols.
854
584892a6
RS
855 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
856 SECOND are the lengths of the two sequences in bytes. These fields
857 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
858 the subtype has the following flags:
4d7206a2 859
584892a6
RS
860 RELAX_USE_SECOND
861 Set if it has been decided that we should use the second
862 sequence instead of the first.
863
864 RELAX_SECOND_LONGER
865 Set in the first variant frag if the macro's second implementation
866 is longer than its first. This refers to the macro as a whole,
867 not an individual relaxation.
868
869 RELAX_NOMACRO
870 Set in the first variant frag if the macro appeared in a .set nomacro
871 block and if one alternative requires a warning but the other does not.
872
873 RELAX_DELAY_SLOT
874 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
875 delay slot.
4d7206a2 876
df58fc94
RS
877 RELAX_DELAY_SLOT_16BIT
878 Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
879 16-bit instruction.
880
881 RELAX_DELAY_SLOT_SIZE_FIRST
882 Like RELAX_DELAY_SLOT, but indicates that the first implementation of
883 the macro is of the wrong size for the branch delay slot.
884
885 RELAX_DELAY_SLOT_SIZE_SECOND
886 Like RELAX_DELAY_SLOT, but indicates that the second implementation of
887 the macro is of the wrong size for the branch delay slot.
888
4d7206a2
RS
889 The frag's "opcode" points to the first fixup for relaxable code.
890
891 Relaxable macros are generated using a sequence such as:
892
893 relax_start (SYMBOL);
894 ... generate first expansion ...
895 relax_switch ();
896 ... generate second expansion ...
897 relax_end ();
898
899 The code and fixups for the unwanted alternative are discarded
900 by md_convert_frag. */
584892a6 901#define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
4d7206a2 902
584892a6
RS
903#define RELAX_FIRST(X) (((X) >> 8) & 0xff)
904#define RELAX_SECOND(X) ((X) & 0xff)
905#define RELAX_USE_SECOND 0x10000
906#define RELAX_SECOND_LONGER 0x20000
907#define RELAX_NOMACRO 0x40000
908#define RELAX_DELAY_SLOT 0x80000
df58fc94
RS
909#define RELAX_DELAY_SLOT_16BIT 0x100000
910#define RELAX_DELAY_SLOT_SIZE_FIRST 0x200000
911#define RELAX_DELAY_SLOT_SIZE_SECOND 0x400000
252b5132 912
4a6a3df4
AO
913/* Branch without likely bit. If label is out of range, we turn:
914
915 beq reg1, reg2, label
916 delay slot
917
918 into
919
920 bne reg1, reg2, 0f
921 nop
922 j label
923 0: delay slot
924
925 with the following opcode replacements:
926
927 beq <-> bne
928 blez <-> bgtz
929 bltz <-> bgez
930 bc1f <-> bc1t
931
932 bltzal <-> bgezal (with jal label instead of j label)
933
934 Even though keeping the delay slot instruction in the delay slot of
935 the branch would be more efficient, it would be very tricky to do
936 correctly, because we'd have to introduce a variable frag *after*
937 the delay slot instruction, and expand that instead. Let's do it
938 the easy way for now, even if the branch-not-taken case now costs
939 one additional instruction. Out-of-range branches are not supposed
940 to be common, anyway.
941
942 Branch likely. If label is out of range, we turn:
943
944 beql reg1, reg2, label
945 delay slot (annulled if branch not taken)
946
947 into
948
949 beql reg1, reg2, 1f
950 nop
951 beql $0, $0, 2f
952 nop
953 1: j[al] label
954 delay slot (executed only if branch taken)
955 2:
956
957 It would be possible to generate a shorter sequence by losing the
958 likely bit, generating something like:
b34976b6 959
4a6a3df4
AO
960 bne reg1, reg2, 0f
961 nop
962 j[al] label
963 delay slot (executed only if branch taken)
964 0:
965
966 beql -> bne
967 bnel -> beq
968 blezl -> bgtz
969 bgtzl -> blez
970 bltzl -> bgez
971 bgezl -> bltz
972 bc1fl -> bc1t
973 bc1tl -> bc1f
974
975 bltzall -> bgezal (with jal label instead of j label)
976 bgezall -> bltzal (ditto)
977
978
979 but it's not clear that it would actually improve performance. */
66b3e8da
MR
980#define RELAX_BRANCH_ENCODE(at, uncond, likely, link, toofar) \
981 ((relax_substateT) \
982 (0xc0000000 \
983 | ((at) & 0x1f) \
984 | ((toofar) ? 0x20 : 0) \
985 | ((link) ? 0x40 : 0) \
986 | ((likely) ? 0x80 : 0) \
987 | ((uncond) ? 0x100 : 0)))
4a6a3df4 988#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
66b3e8da
MR
989#define RELAX_BRANCH_UNCOND(i) (((i) & 0x100) != 0)
990#define RELAX_BRANCH_LIKELY(i) (((i) & 0x80) != 0)
991#define RELAX_BRANCH_LINK(i) (((i) & 0x40) != 0)
992#define RELAX_BRANCH_TOOFAR(i) (((i) & 0x20) != 0)
993#define RELAX_BRANCH_AT(i) ((i) & 0x1f)
4a6a3df4 994
252b5132
RH
995/* For mips16 code, we use an entirely different form of relaxation.
996 mips16 supports two versions of most instructions which take
997 immediate values: a small one which takes some small value, and a
998 larger one which takes a 16 bit value. Since branches also follow
999 this pattern, relaxing these values is required.
1000
1001 We can assemble both mips16 and normal MIPS code in a single
1002 object. Therefore, we need to support this type of relaxation at
1003 the same time that we support the relaxation described above. We
1004 use the high bit of the subtype field to distinguish these cases.
1005
1006 The information we store for this type of relaxation is the
1007 argument code found in the opcode file for this relocation, whether
1008 the user explicitly requested a small or extended form, and whether
1009 the relocation is in a jump or jal delay slot. That tells us the
1010 size of the value, and how it should be stored. We also store
1011 whether the fragment is considered to be extended or not. We also
1012 store whether this is known to be a branch to a different section,
1013 whether we have tried to relax this frag yet, and whether we have
1014 ever extended a PC relative fragment because of a shift count. */
1015#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
1016 (0x80000000 \
1017 | ((type) & 0xff) \
1018 | ((small) ? 0x100 : 0) \
1019 | ((ext) ? 0x200 : 0) \
1020 | ((dslot) ? 0x400 : 0) \
1021 | ((jal_dslot) ? 0x800 : 0))
4a6a3df4 1022#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132
RH
1023#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
1024#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
1025#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
1026#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
1027#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
1028#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
1029#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
1030#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
1031#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
1032#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
1033#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
885add95 1034
df58fc94
RS
1035/* For microMIPS code, we use relaxation similar to one we use for
1036 MIPS16 code. Some instructions that take immediate values support
1037 two encodings: a small one which takes some small value, and a
1038 larger one which takes a 16 bit value. As some branches also follow
1039 this pattern, relaxing these values is required.
1040
1041 We can assemble both microMIPS and normal MIPS code in a single
1042 object. Therefore, we need to support this type of relaxation at
1043 the same time that we support the relaxation described above. We
1044 use one of the high bits of the subtype field to distinguish these
1045 cases.
1046
1047 The information we store for this type of relaxation is the argument
1048 code found in the opcode file for this relocation, the register
40209cad
MR
1049 selected as the assembler temporary, whether the branch is
1050 unconditional, whether it is compact, whether it stores the link
1051 address implicitly in $ra, whether relaxation of out-of-range 32-bit
1052 branches to a sequence of instructions is enabled, and whether the
1053 displacement of a branch is too large to fit as an immediate argument
1054 of a 16-bit and a 32-bit branch, respectively. */
1055#define RELAX_MICROMIPS_ENCODE(type, at, uncond, compact, link, \
1056 relax32, toofar16, toofar32) \
1057 (0x40000000 \
1058 | ((type) & 0xff) \
1059 | (((at) & 0x1f) << 8) \
1060 | ((uncond) ? 0x2000 : 0) \
1061 | ((compact) ? 0x4000 : 0) \
1062 | ((link) ? 0x8000 : 0) \
1063 | ((relax32) ? 0x10000 : 0) \
1064 | ((toofar16) ? 0x20000 : 0) \
1065 | ((toofar32) ? 0x40000 : 0))
df58fc94
RS
1066#define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1067#define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1068#define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
40209cad
MR
1069#define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x2000) != 0)
1070#define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x4000) != 0)
1071#define RELAX_MICROMIPS_LINK(i) (((i) & 0x8000) != 0)
1072#define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x10000) != 0)
1073
1074#define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x20000) != 0)
1075#define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x20000)
1076#define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x20000)
1077#define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x40000) != 0)
1078#define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x40000)
1079#define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x40000)
df58fc94 1080
43c0598f
RS
1081/* Sign-extend 16-bit value X. */
1082#define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1083
885add95
CD
1084/* Is the given value a sign-extended 32-bit value? */
1085#define IS_SEXT_32BIT_NUM(x) \
1086 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1087 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1088
1089/* Is the given value a sign-extended 16-bit value? */
1090#define IS_SEXT_16BIT_NUM(x) \
1091 (((x) &~ (offsetT) 0x7fff) == 0 \
1092 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1093
df58fc94
RS
1094/* Is the given value a sign-extended 12-bit value? */
1095#define IS_SEXT_12BIT_NUM(x) \
1096 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1097
7f3c4072
CM
1098/* Is the given value a sign-extended 9-bit value? */
1099#define IS_SEXT_9BIT_NUM(x) \
1100 (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1101
2051e8c4
MR
1102/* Is the given value a zero-extended 32-bit value? Or a negated one? */
1103#define IS_ZEXT_32BIT_NUM(x) \
1104 (((x) &~ (offsetT) 0xffffffff) == 0 \
1105 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1106
bf12938e
RS
1107/* Extract bits MASK << SHIFT from STRUCT and shift them right
1108 SHIFT places. */
1109#define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1110 (((STRUCT) >> (SHIFT)) & (MASK))
1111
bf12938e 1112/* Extract the operand given by FIELD from mips_cl_insn INSN. */
df58fc94
RS
1113#define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1114 (!(MICROMIPS) \
1115 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1116 : EXTRACT_BITS ((INSN).insn_opcode, \
1117 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
bf12938e
RS
1118#define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1119 EXTRACT_BITS ((INSN).insn_opcode, \
1120 MIPS16OP_MASK_##FIELD, \
1121 MIPS16OP_SH_##FIELD)
5c04167a
RS
1122
1123/* The MIPS16 EXTEND opcode, shifted left 16 places. */
1124#define MIPS16_EXTEND (0xf000U << 16)
4d7206a2 1125\f
df58fc94
RS
1126/* Whether or not we are emitting a branch-likely macro. */
1127static bfd_boolean emit_branch_likely_macro = FALSE;
1128
4d7206a2
RS
1129/* Global variables used when generating relaxable macros. See the
1130 comment above RELAX_ENCODE for more details about how relaxation
1131 is used. */
1132static struct {
1133 /* 0 if we're not emitting a relaxable macro.
1134 1 if we're emitting the first of the two relaxation alternatives.
1135 2 if we're emitting the second alternative. */
1136 int sequence;
1137
1138 /* The first relaxable fixup in the current frag. (In other words,
1139 the first fixup that refers to relaxable code.) */
1140 fixS *first_fixup;
1141
1142 /* sizes[0] says how many bytes of the first alternative are stored in
1143 the current frag. Likewise sizes[1] for the second alternative. */
1144 unsigned int sizes[2];
1145
1146 /* The symbol on which the choice of sequence depends. */
1147 symbolS *symbol;
1148} mips_relax;
252b5132 1149\f
584892a6
RS
1150/* Global variables used to decide whether a macro needs a warning. */
1151static struct {
1152 /* True if the macro is in a branch delay slot. */
1153 bfd_boolean delay_slot_p;
1154
df58fc94
RS
1155 /* Set to the length in bytes required if the macro is in a delay slot
1156 that requires a specific length of instruction, otherwise zero. */
1157 unsigned int delay_slot_length;
1158
584892a6
RS
1159 /* For relaxable macros, sizes[0] is the length of the first alternative
1160 in bytes and sizes[1] is the length of the second alternative.
1161 For non-relaxable macros, both elements give the length of the
1162 macro in bytes. */
1163 unsigned int sizes[2];
1164
df58fc94
RS
1165 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1166 instruction of the first alternative in bytes and first_insn_sizes[1]
1167 is the length of the first instruction of the second alternative.
1168 For non-relaxable macros, both elements give the length of the first
1169 instruction in bytes.
1170
1171 Set to zero if we haven't yet seen the first instruction. */
1172 unsigned int first_insn_sizes[2];
1173
1174 /* For relaxable macros, insns[0] is the number of instructions for the
1175 first alternative and insns[1] is the number of instructions for the
1176 second alternative.
1177
1178 For non-relaxable macros, both elements give the number of
1179 instructions for the macro. */
1180 unsigned int insns[2];
1181
584892a6
RS
1182 /* The first variant frag for this macro. */
1183 fragS *first_frag;
1184} mips_macro_warning;
1185\f
252b5132
RH
1186/* Prototypes for static functions. */
1187
252b5132
RH
1188enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1189
b34976b6 1190static void append_insn
df58fc94
RS
1191 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1192 bfd_boolean expansionp);
7d10b47d 1193static void mips_no_prev_insn (void);
c67a084a 1194static void macro_build (expressionS *, const char *, const char *, ...);
b34976b6 1195static void mips16_macro_build
03ea81db 1196 (expressionS *, const char *, const char *, va_list *);
67c0d1eb 1197static void load_register (int, expressionS *, int);
584892a6
RS
1198static void macro_start (void);
1199static void macro_end (void);
833794fc 1200static void macro (struct mips_cl_insn *ip, char *str);
17a2f251 1201static void mips16_macro (struct mips_cl_insn * ip);
17a2f251
TS
1202static void mips_ip (char *str, struct mips_cl_insn * ip);
1203static void mips16_ip (char *str, struct mips_cl_insn * ip);
b34976b6 1204static void mips16_immed
43c0598f
RS
1205 (char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
1206 unsigned int, unsigned long *);
5e0116d5 1207static size_t my_getSmallExpression
17a2f251
TS
1208 (expressionS *, bfd_reloc_code_real_type *, char *);
1209static void my_getExpression (expressionS *, char *);
1210static void s_align (int);
1211static void s_change_sec (int);
1212static void s_change_section (int);
1213static void s_cons (int);
1214static void s_float_cons (int);
1215static void s_mips_globl (int);
1216static void s_option (int);
1217static void s_mipsset (int);
1218static void s_abicalls (int);
1219static void s_cpload (int);
1220static void s_cpsetup (int);
1221static void s_cplocal (int);
1222static void s_cprestore (int);
1223static void s_cpreturn (int);
741d6ea8
JM
1224static void s_dtprelword (int);
1225static void s_dtpreldword (int);
d0f13682
CLT
1226static void s_tprelword (int);
1227static void s_tpreldword (int);
17a2f251
TS
1228static void s_gpvalue (int);
1229static void s_gpword (int);
1230static void s_gpdword (int);
a3f278e2 1231static void s_ehword (int);
17a2f251
TS
1232static void s_cpadd (int);
1233static void s_insn (int);
ba92f887 1234static void s_nan (int);
17a2f251
TS
1235static void md_obj_begin (void);
1236static void md_obj_end (void);
1237static void s_mips_ent (int);
1238static void s_mips_end (int);
1239static void s_mips_frame (int);
1240static void s_mips_mask (int reg_type);
1241static void s_mips_stab (int);
1242static void s_mips_weakext (int);
1243static void s_mips_file (int);
1244static void s_mips_loc (int);
1245static bfd_boolean pic_need_relax (symbolS *, asection *);
4a6a3df4 1246static int relaxed_branch_length (fragS *, asection *, int);
df58fc94
RS
1247static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1248static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
e7af610e
NC
1249
1250/* Table and functions used to map between CPU/ISA names, and
1251 ISA levels, and CPU numbers. */
1252
e972090a
NC
1253struct mips_cpu_info
1254{
e7af610e 1255 const char *name; /* CPU or ISA name. */
d16afab6
RS
1256 int flags; /* MIPS_CPU_* flags. */
1257 int ase; /* Set of ASEs implemented by the CPU. */
e7af610e
NC
1258 int isa; /* ISA level. */
1259 int cpu; /* CPU number (default CPU if ISA). */
1260};
1261
ad3fea08 1262#define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
ad3fea08 1263
17a2f251
TS
1264static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1265static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1266static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132 1267\f
c31f3936
RS
1268/* Command-line options. */
1269const char *md_shortopts = "O::g::G:";
1270
1271enum options
1272 {
1273 OPTION_MARCH = OPTION_MD_BASE,
1274 OPTION_MTUNE,
1275 OPTION_MIPS1,
1276 OPTION_MIPS2,
1277 OPTION_MIPS3,
1278 OPTION_MIPS4,
1279 OPTION_MIPS5,
1280 OPTION_MIPS32,
1281 OPTION_MIPS64,
1282 OPTION_MIPS32R2,
1283 OPTION_MIPS64R2,
1284 OPTION_MIPS16,
1285 OPTION_NO_MIPS16,
1286 OPTION_MIPS3D,
1287 OPTION_NO_MIPS3D,
1288 OPTION_MDMX,
1289 OPTION_NO_MDMX,
1290 OPTION_DSP,
1291 OPTION_NO_DSP,
1292 OPTION_MT,
1293 OPTION_NO_MT,
1294 OPTION_VIRT,
1295 OPTION_NO_VIRT,
1296 OPTION_SMARTMIPS,
1297 OPTION_NO_SMARTMIPS,
1298 OPTION_DSPR2,
1299 OPTION_NO_DSPR2,
1300 OPTION_EVA,
1301 OPTION_NO_EVA,
1302 OPTION_MICROMIPS,
1303 OPTION_NO_MICROMIPS,
1304 OPTION_MCU,
1305 OPTION_NO_MCU,
1306 OPTION_COMPAT_ARCH_BASE,
1307 OPTION_M4650,
1308 OPTION_NO_M4650,
1309 OPTION_M4010,
1310 OPTION_NO_M4010,
1311 OPTION_M4100,
1312 OPTION_NO_M4100,
1313 OPTION_M3900,
1314 OPTION_NO_M3900,
1315 OPTION_M7000_HILO_FIX,
1316 OPTION_MNO_7000_HILO_FIX,
1317 OPTION_FIX_24K,
1318 OPTION_NO_FIX_24K,
1319 OPTION_FIX_LOONGSON2F_JUMP,
1320 OPTION_NO_FIX_LOONGSON2F_JUMP,
1321 OPTION_FIX_LOONGSON2F_NOP,
1322 OPTION_NO_FIX_LOONGSON2F_NOP,
1323 OPTION_FIX_VR4120,
1324 OPTION_NO_FIX_VR4120,
1325 OPTION_FIX_VR4130,
1326 OPTION_NO_FIX_VR4130,
1327 OPTION_FIX_CN63XXP1,
1328 OPTION_NO_FIX_CN63XXP1,
1329 OPTION_TRAP,
1330 OPTION_BREAK,
1331 OPTION_EB,
1332 OPTION_EL,
1333 OPTION_FP32,
1334 OPTION_GP32,
1335 OPTION_CONSTRUCT_FLOATS,
1336 OPTION_NO_CONSTRUCT_FLOATS,
1337 OPTION_FP64,
1338 OPTION_GP64,
1339 OPTION_RELAX_BRANCH,
1340 OPTION_NO_RELAX_BRANCH,
833794fc
MR
1341 OPTION_INSN32,
1342 OPTION_NO_INSN32,
c31f3936
RS
1343 OPTION_MSHARED,
1344 OPTION_MNO_SHARED,
1345 OPTION_MSYM32,
1346 OPTION_MNO_SYM32,
1347 OPTION_SOFT_FLOAT,
1348 OPTION_HARD_FLOAT,
1349 OPTION_SINGLE_FLOAT,
1350 OPTION_DOUBLE_FLOAT,
1351 OPTION_32,
c31f3936
RS
1352 OPTION_CALL_SHARED,
1353 OPTION_CALL_NONPIC,
1354 OPTION_NON_SHARED,
1355 OPTION_XGOT,
1356 OPTION_MABI,
1357 OPTION_N32,
1358 OPTION_64,
1359 OPTION_MDEBUG,
1360 OPTION_NO_MDEBUG,
1361 OPTION_PDR,
1362 OPTION_NO_PDR,
1363 OPTION_MVXWORKS_PIC,
ba92f887 1364 OPTION_NAN,
c31f3936
RS
1365 OPTION_END_OF_ENUM
1366 };
1367
1368struct option md_longopts[] =
1369{
1370 /* Options which specify architecture. */
1371 {"march", required_argument, NULL, OPTION_MARCH},
1372 {"mtune", required_argument, NULL, OPTION_MTUNE},
1373 {"mips0", no_argument, NULL, OPTION_MIPS1},
1374 {"mips1", no_argument, NULL, OPTION_MIPS1},
1375 {"mips2", no_argument, NULL, OPTION_MIPS2},
1376 {"mips3", no_argument, NULL, OPTION_MIPS3},
1377 {"mips4", no_argument, NULL, OPTION_MIPS4},
1378 {"mips5", no_argument, NULL, OPTION_MIPS5},
1379 {"mips32", no_argument, NULL, OPTION_MIPS32},
1380 {"mips64", no_argument, NULL, OPTION_MIPS64},
1381 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
1382 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
1383
1384 /* Options which specify Application Specific Extensions (ASEs). */
1385 {"mips16", no_argument, NULL, OPTION_MIPS16},
1386 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1387 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1388 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1389 {"mdmx", no_argument, NULL, OPTION_MDMX},
1390 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1391 {"mdsp", no_argument, NULL, OPTION_DSP},
1392 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1393 {"mmt", no_argument, NULL, OPTION_MT},
1394 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1395 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1396 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1397 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1398 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
1399 {"meva", no_argument, NULL, OPTION_EVA},
1400 {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1401 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1402 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1403 {"mmcu", no_argument, NULL, OPTION_MCU},
1404 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1405 {"mvirt", no_argument, NULL, OPTION_VIRT},
1406 {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
1407
1408 /* Old-style architecture options. Don't add more of these. */
1409 {"m4650", no_argument, NULL, OPTION_M4650},
1410 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1411 {"m4010", no_argument, NULL, OPTION_M4010},
1412 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1413 {"m4100", no_argument, NULL, OPTION_M4100},
1414 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1415 {"m3900", no_argument, NULL, OPTION_M3900},
1416 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1417
1418 /* Options which enable bug fixes. */
1419 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1420 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1421 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1422 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1423 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1424 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1425 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1426 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
1427 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1428 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
1429 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1430 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
1431 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
1432 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1433 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
1434
1435 /* Miscellaneous options. */
1436 {"trap", no_argument, NULL, OPTION_TRAP},
1437 {"no-break", no_argument, NULL, OPTION_TRAP},
1438 {"break", no_argument, NULL, OPTION_BREAK},
1439 {"no-trap", no_argument, NULL, OPTION_BREAK},
1440 {"EB", no_argument, NULL, OPTION_EB},
1441 {"EL", no_argument, NULL, OPTION_EL},
1442 {"mfp32", no_argument, NULL, OPTION_FP32},
1443 {"mgp32", no_argument, NULL, OPTION_GP32},
1444 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1445 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1446 {"mfp64", no_argument, NULL, OPTION_FP64},
1447 {"mgp64", no_argument, NULL, OPTION_GP64},
1448 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1449 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
833794fc
MR
1450 {"minsn32", no_argument, NULL, OPTION_INSN32},
1451 {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
c31f3936
RS
1452 {"mshared", no_argument, NULL, OPTION_MSHARED},
1453 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1454 {"msym32", no_argument, NULL, OPTION_MSYM32},
1455 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1456 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1457 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1458 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1459 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
1460
1461 /* Strictly speaking this next option is ELF specific,
1462 but we allow it for other ports as well in order to
1463 make testing easier. */
1464 {"32", no_argument, NULL, OPTION_32},
1465
1466 /* ELF-specific options. */
c31f3936
RS
1467 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1468 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1469 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1470 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
1471 {"xgot", no_argument, NULL, OPTION_XGOT},
1472 {"mabi", required_argument, NULL, OPTION_MABI},
1473 {"n32", no_argument, NULL, OPTION_N32},
1474 {"64", no_argument, NULL, OPTION_64},
1475 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1476 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1477 {"mpdr", no_argument, NULL, OPTION_PDR},
1478 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1479 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
ba92f887 1480 {"mnan", required_argument, NULL, OPTION_NAN},
c31f3936
RS
1481
1482 {NULL, no_argument, NULL, 0}
1483};
1484size_t md_longopts_size = sizeof (md_longopts);
1485\f
c6278170
RS
1486/* Information about either an Application Specific Extension or an
1487 optional architecture feature that, for simplicity, we treat in the
1488 same way as an ASE. */
1489struct mips_ase
1490{
1491 /* The name of the ASE, used in both the command-line and .set options. */
1492 const char *name;
1493
1494 /* The associated ASE_* flags. If the ASE is available on both 32-bit
1495 and 64-bit architectures, the flags here refer to the subset that
1496 is available on both. */
1497 unsigned int flags;
1498
1499 /* The ASE_* flag used for instructions that are available on 64-bit
1500 architectures but that are not included in FLAGS. */
1501 unsigned int flags64;
1502
1503 /* The command-line options that turn the ASE on and off. */
1504 int option_on;
1505 int option_off;
1506
1507 /* The minimum required architecture revisions for MIPS32, MIPS64,
1508 microMIPS32 and microMIPS64, or -1 if the extension isn't supported. */
1509 int mips32_rev;
1510 int mips64_rev;
1511 int micromips32_rev;
1512 int micromips64_rev;
1513};
1514
1515/* A table of all supported ASEs. */
1516static const struct mips_ase mips_ases[] = {
1517 { "dsp", ASE_DSP, ASE_DSP64,
1518 OPTION_DSP, OPTION_NO_DSP,
1519 2, 2, 2, 2 },
1520
1521 { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1522 OPTION_DSPR2, OPTION_NO_DSPR2,
1523 2, 2, 2, 2 },
1524
1525 { "eva", ASE_EVA, 0,
1526 OPTION_EVA, OPTION_NO_EVA,
1527 2, 2, 2, 2 },
1528
1529 { "mcu", ASE_MCU, 0,
1530 OPTION_MCU, OPTION_NO_MCU,
1531 2, 2, 2, 2 },
1532
1533 /* Deprecated in MIPS64r5, but we don't implement that yet. */
1534 { "mdmx", ASE_MDMX, 0,
1535 OPTION_MDMX, OPTION_NO_MDMX,
1536 -1, 1, -1, -1 },
1537
1538 /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2. */
1539 { "mips3d", ASE_MIPS3D, 0,
1540 OPTION_MIPS3D, OPTION_NO_MIPS3D,
1541 2, 1, -1, -1 },
1542
1543 { "mt", ASE_MT, 0,
1544 OPTION_MT, OPTION_NO_MT,
1545 2, 2, -1, -1 },
1546
1547 { "smartmips", ASE_SMARTMIPS, 0,
1548 OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
1549 1, -1, -1, -1 },
1550
1551 { "virt", ASE_VIRT, ASE_VIRT64,
1552 OPTION_VIRT, OPTION_NO_VIRT,
1553 2, 2, 2, 2 }
1554};
1555
1556/* The set of ASEs that require -mfp64. */
1557#define FP64_ASES (ASE_MIPS3D | ASE_MDMX)
1558
1559/* Groups of ASE_* flags that represent different revisions of an ASE. */
1560static const unsigned int mips_ase_groups[] = {
1561 ASE_DSP | ASE_DSPR2
1562};
1563\f
252b5132
RH
1564/* Pseudo-op table.
1565
1566 The following pseudo-ops from the Kane and Heinrich MIPS book
1567 should be defined here, but are currently unsupported: .alias,
1568 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1569
1570 The following pseudo-ops from the Kane and Heinrich MIPS book are
1571 specific to the type of debugging information being generated, and
1572 should be defined by the object format: .aent, .begin, .bend,
1573 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1574 .vreg.
1575
1576 The following pseudo-ops from the Kane and Heinrich MIPS book are
1577 not MIPS CPU specific, but are also not specific to the object file
1578 format. This file is probably the best place to define them, but
d84bcf09 1579 they are not currently supported: .asm0, .endr, .lab, .struct. */
252b5132 1580
e972090a
NC
1581static const pseudo_typeS mips_pseudo_table[] =
1582{
beae10d5 1583 /* MIPS specific pseudo-ops. */
252b5132
RH
1584 {"option", s_option, 0},
1585 {"set", s_mipsset, 0},
1586 {"rdata", s_change_sec, 'r'},
1587 {"sdata", s_change_sec, 's'},
1588 {"livereg", s_ignore, 0},
1589 {"abicalls", s_abicalls, 0},
1590 {"cpload", s_cpload, 0},
6478892d
TS
1591 {"cpsetup", s_cpsetup, 0},
1592 {"cplocal", s_cplocal, 0},
252b5132 1593 {"cprestore", s_cprestore, 0},
6478892d 1594 {"cpreturn", s_cpreturn, 0},
741d6ea8
JM
1595 {"dtprelword", s_dtprelword, 0},
1596 {"dtpreldword", s_dtpreldword, 0},
d0f13682
CLT
1597 {"tprelword", s_tprelword, 0},
1598 {"tpreldword", s_tpreldword, 0},
6478892d 1599 {"gpvalue", s_gpvalue, 0},
252b5132 1600 {"gpword", s_gpword, 0},
10181a0d 1601 {"gpdword", s_gpdword, 0},
a3f278e2 1602 {"ehword", s_ehword, 0},
252b5132
RH
1603 {"cpadd", s_cpadd, 0},
1604 {"insn", s_insn, 0},
ba92f887 1605 {"nan", s_nan, 0},
252b5132 1606
beae10d5 1607 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132 1608 chips. */
38a57ae7 1609 {"asciiz", stringer, 8 + 1},
252b5132
RH
1610 {"bss", s_change_sec, 'b'},
1611 {"err", s_err, 0},
1612 {"half", s_cons, 1},
1613 {"dword", s_cons, 3},
1614 {"weakext", s_mips_weakext, 0},
7c752c2a
TS
1615 {"origin", s_org, 0},
1616 {"repeat", s_rept, 0},
252b5132 1617
998b3c36
MR
1618 /* For MIPS this is non-standard, but we define it for consistency. */
1619 {"sbss", s_change_sec, 'B'},
1620
beae10d5 1621 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1622 here for one reason or another. */
1623 {"align", s_align, 0},
1624 {"byte", s_cons, 0},
1625 {"data", s_change_sec, 'd'},
1626 {"double", s_float_cons, 'd'},
1627 {"float", s_float_cons, 'f'},
1628 {"globl", s_mips_globl, 0},
1629 {"global", s_mips_globl, 0},
1630 {"hword", s_cons, 1},
1631 {"int", s_cons, 2},
1632 {"long", s_cons, 2},
1633 {"octa", s_cons, 4},
1634 {"quad", s_cons, 3},
cca86cc8 1635 {"section", s_change_section, 0},
252b5132
RH
1636 {"short", s_cons, 1},
1637 {"single", s_float_cons, 'f'},
754e2bb9 1638 {"stabd", s_mips_stab, 'd'},
252b5132 1639 {"stabn", s_mips_stab, 'n'},
754e2bb9 1640 {"stabs", s_mips_stab, 's'},
252b5132
RH
1641 {"text", s_change_sec, 't'},
1642 {"word", s_cons, 2},
add56521 1643
add56521 1644 { "extern", ecoff_directive_extern, 0},
add56521 1645
43841e91 1646 { NULL, NULL, 0 },
252b5132
RH
1647};
1648
e972090a
NC
1649static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1650{
beae10d5
KH
1651 /* These pseudo-ops should be defined by the object file format.
1652 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1653 {"aent", s_mips_ent, 1},
1654 {"bgnb", s_ignore, 0},
1655 {"end", s_mips_end, 0},
1656 {"endb", s_ignore, 0},
1657 {"ent", s_mips_ent, 0},
c5dd6aab 1658 {"file", s_mips_file, 0},
252b5132
RH
1659 {"fmask", s_mips_mask, 'F'},
1660 {"frame", s_mips_frame, 0},
c5dd6aab 1661 {"loc", s_mips_loc, 0},
252b5132
RH
1662 {"mask", s_mips_mask, 'R'},
1663 {"verstamp", s_ignore, 0},
43841e91 1664 { NULL, NULL, 0 },
252b5132
RH
1665};
1666
3ae8dd8d
MR
1667/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1668 purpose of the `.dc.a' internal pseudo-op. */
1669
1670int
1671mips_address_bytes (void)
1672{
1673 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1674}
1675
17a2f251 1676extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1677
1678void
17a2f251 1679mips_pop_insert (void)
252b5132
RH
1680{
1681 pop_insert (mips_pseudo_table);
1682 if (! ECOFF_DEBUGGING)
1683 pop_insert (mips_nonecoff_pseudo_table);
1684}
1685\f
1686/* Symbols labelling the current insn. */
1687
e972090a
NC
1688struct insn_label_list
1689{
252b5132
RH
1690 struct insn_label_list *next;
1691 symbolS *label;
1692};
1693
252b5132 1694static struct insn_label_list *free_insn_labels;
742a56fe 1695#define label_list tc_segment_info_data.labels
252b5132 1696
17a2f251 1697static void mips_clear_insn_labels (void);
df58fc94
RS
1698static void mips_mark_labels (void);
1699static void mips_compressed_mark_labels (void);
252b5132
RH
1700
1701static inline void
17a2f251 1702mips_clear_insn_labels (void)
252b5132
RH
1703{
1704 register struct insn_label_list **pl;
a8dbcb85 1705 segment_info_type *si;
252b5132 1706
a8dbcb85
TS
1707 if (now_seg)
1708 {
1709 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1710 ;
1711
1712 si = seg_info (now_seg);
1713 *pl = si->label_list;
1714 si->label_list = NULL;
1715 }
252b5132 1716}
a8dbcb85 1717
df58fc94
RS
1718/* Mark instruction labels in MIPS16/microMIPS mode. */
1719
1720static inline void
1721mips_mark_labels (void)
1722{
1723 if (HAVE_CODE_COMPRESSION)
1724 mips_compressed_mark_labels ();
1725}
252b5132
RH
1726\f
1727static char *expr_end;
1728
77bd4346
RS
1729/* Expressions which appear in macro instructions. These are set by
1730 mips_ip and read by macro. */
252b5132
RH
1731
1732static expressionS imm_expr;
5f74bc13 1733static expressionS imm2_expr;
252b5132 1734
77bd4346
RS
1735/* The relocatable field in an instruction and the relocs associated
1736 with it. These variables are used for instructions like LUI and
1737 JAL as well as true offsets. They are also used for address
1738 operands in macros. */
252b5132 1739
77bd4346 1740static expressionS offset_expr;
f6688943
TS
1741static bfd_reloc_code_real_type offset_reloc[3]
1742 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 1743
df58fc94
RS
1744/* This is set to the resulting size of the instruction to be produced
1745 by mips16_ip if an explicit extension is used or by mips_ip if an
1746 explicit size is supplied. */
252b5132 1747
df58fc94 1748static unsigned int forced_insn_length;
252b5132 1749
e1b47bd5
RS
1750/* True if we are assembling an instruction. All dot symbols defined during
1751 this time should be treated as code labels. */
1752
1753static bfd_boolean mips_assembling_insn;
1754
ecb4347a
DJ
1755/* The pdr segment for per procedure frame/regmask info. Not used for
1756 ECOFF debugging. */
252b5132
RH
1757
1758static segT pdr_seg;
252b5132 1759
e013f690
TS
1760/* The default target format to use. */
1761
aeffff67
RS
1762#if defined (TE_FreeBSD)
1763#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
1764#elif defined (TE_TMIPS)
1765#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
1766#else
1767#define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
1768#endif
1769
e013f690 1770const char *
17a2f251 1771mips_target_format (void)
e013f690
TS
1772{
1773 switch (OUTPUT_FLAVOR)
1774 {
e013f690 1775 case bfd_target_elf_flavour:
0a44bf69
RS
1776#ifdef TE_VXWORKS
1777 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1778 return (target_big_endian
1779 ? "elf32-bigmips-vxworks"
1780 : "elf32-littlemips-vxworks");
1781#endif
e013f690 1782 return (target_big_endian
cfe86eaa 1783 ? (HAVE_64BIT_OBJECTS
aeffff67 1784 ? ELF_TARGET ("elf64-", "big")
cfe86eaa 1785 : (HAVE_NEWABI
aeffff67
RS
1786 ? ELF_TARGET ("elf32-n", "big")
1787 : ELF_TARGET ("elf32-", "big")))
cfe86eaa 1788 : (HAVE_64BIT_OBJECTS
aeffff67 1789 ? ELF_TARGET ("elf64-", "little")
cfe86eaa 1790 : (HAVE_NEWABI
aeffff67
RS
1791 ? ELF_TARGET ("elf32-n", "little")
1792 : ELF_TARGET ("elf32-", "little"))));
e013f690
TS
1793 default:
1794 abort ();
1795 return NULL;
1796 }
1797}
1798
c6278170
RS
1799/* Return the ISA revision that is currently in use, or 0 if we are
1800 generating code for MIPS V or below. */
1801
1802static int
1803mips_isa_rev (void)
1804{
1805 if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
1806 return 2;
1807
1808 /* microMIPS implies revision 2 or above. */
1809 if (mips_opts.micromips)
1810 return 2;
1811
1812 if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
1813 return 1;
1814
1815 return 0;
1816}
1817
1818/* Return the mask of all ASEs that are revisions of those in FLAGS. */
1819
1820static unsigned int
1821mips_ase_mask (unsigned int flags)
1822{
1823 unsigned int i;
1824
1825 for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
1826 if (flags & mips_ase_groups[i])
1827 flags |= mips_ase_groups[i];
1828 return flags;
1829}
1830
1831/* Check whether the current ISA supports ASE. Issue a warning if
1832 appropriate. */
1833
1834static void
1835mips_check_isa_supports_ase (const struct mips_ase *ase)
1836{
1837 const char *base;
1838 int min_rev, size;
1839 static unsigned int warned_isa;
1840 static unsigned int warned_fp32;
1841
1842 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
1843 min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
1844 else
1845 min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
1846 if ((min_rev < 0 || mips_isa_rev () < min_rev)
1847 && (warned_isa & ase->flags) != ase->flags)
1848 {
1849 warned_isa |= ase->flags;
1850 base = mips_opts.micromips ? "microMIPS" : "MIPS";
1851 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
1852 if (min_rev < 0)
1853 as_warn (_("The %d-bit %s architecture does not support the"
1854 " `%s' extension"), size, base, ase->name);
1855 else
1856 as_warn (_("The `%s' extension requires %s%d revision %d or greater"),
1857 ase->name, base, size, min_rev);
1858 }
1859 if ((ase->flags & FP64_ASES)
1860 && mips_opts.fp32
1861 && (warned_fp32 & ase->flags) != ase->flags)
1862 {
1863 warned_fp32 |= ase->flags;
1864 as_warn (_("The `%s' extension requires 64-bit FPRs"), ase->name);
1865 }
1866}
1867
1868/* Check all enabled ASEs to see whether they are supported by the
1869 chosen architecture. */
1870
1871static void
1872mips_check_isa_supports_ases (void)
1873{
1874 unsigned int i, mask;
1875
1876 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
1877 {
1878 mask = mips_ase_mask (mips_ases[i].flags);
1879 if ((mips_opts.ase & mask) == mips_ases[i].flags)
1880 mips_check_isa_supports_ase (&mips_ases[i]);
1881 }
1882}
1883
1884/* Set the state of ASE to ENABLED_P. Return the mask of ASE_* flags
1885 that were affected. */
1886
1887static unsigned int
1888mips_set_ase (const struct mips_ase *ase, bfd_boolean enabled_p)
1889{
1890 unsigned int mask;
1891
1892 mask = mips_ase_mask (ase->flags);
1893 mips_opts.ase &= ~mask;
1894 if (enabled_p)
1895 mips_opts.ase |= ase->flags;
1896 return mask;
1897}
1898
1899/* Return the ASE called NAME, or null if none. */
1900
1901static const struct mips_ase *
1902mips_lookup_ase (const char *name)
1903{
1904 unsigned int i;
1905
1906 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
1907 if (strcmp (name, mips_ases[i].name) == 0)
1908 return &mips_ases[i];
1909 return NULL;
1910}
1911
df58fc94
RS
1912/* Return the length of a microMIPS instruction in bytes. If bits of
1913 the mask beyond the low 16 are 0, then it is a 16-bit instruction.
1914 Otherwise assume a 32-bit instruction; 48-bit instructions (0x1f
1915 major opcode) will require further modifications to the opcode
1916 table. */
1917
1918static inline unsigned int
1919micromips_insn_length (const struct mips_opcode *mo)
1920{
1921 return (mo->mask >> 16) == 0 ? 2 : 4;
1922}
1923
5c04167a
RS
1924/* Return the length of MIPS16 instruction OPCODE. */
1925
1926static inline unsigned int
1927mips16_opcode_length (unsigned long opcode)
1928{
1929 return (opcode >> 16) == 0 ? 2 : 4;
1930}
1931
1e915849
RS
1932/* Return the length of instruction INSN. */
1933
1934static inline unsigned int
1935insn_length (const struct mips_cl_insn *insn)
1936{
df58fc94
RS
1937 if (mips_opts.micromips)
1938 return micromips_insn_length (insn->insn_mo);
1939 else if (mips_opts.mips16)
5c04167a 1940 return mips16_opcode_length (insn->insn_opcode);
df58fc94 1941 else
1e915849 1942 return 4;
1e915849
RS
1943}
1944
1945/* Initialise INSN from opcode entry MO. Leave its position unspecified. */
1946
1947static void
1948create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
1949{
1950 size_t i;
1951
1952 insn->insn_mo = mo;
1e915849
RS
1953 insn->insn_opcode = mo->match;
1954 insn->frag = NULL;
1955 insn->where = 0;
1956 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1957 insn->fixp[i] = NULL;
1958 insn->fixed_p = (mips_opts.noreorder > 0);
1959 insn->noreorder_p = (mips_opts.noreorder > 0);
1960 insn->mips16_absolute_jump_p = 0;
15be625d 1961 insn->complete_p = 0;
e407c74b 1962 insn->cleared_p = 0;
1e915849
RS
1963}
1964
fc76e730
RS
1965/* Get a list of all the operands in INSN. */
1966
1967static const struct mips_operand_array *
1968insn_operands (const struct mips_cl_insn *insn)
1969{
1970 if (insn->insn_mo >= &mips_opcodes[0]
1971 && insn->insn_mo < &mips_opcodes[NUMOPCODES])
1972 return &mips_operands[insn->insn_mo - &mips_opcodes[0]];
1973
1974 if (insn->insn_mo >= &mips16_opcodes[0]
1975 && insn->insn_mo < &mips16_opcodes[bfd_mips16_num_opcodes])
1976 return &mips16_operands[insn->insn_mo - &mips16_opcodes[0]];
1977
1978 if (insn->insn_mo >= &micromips_opcodes[0]
1979 && insn->insn_mo < &micromips_opcodes[bfd_micromips_num_opcodes])
1980 return &micromips_operands[insn->insn_mo - &micromips_opcodes[0]];
1981
1982 abort ();
1983}
1984
1985/* Get a description of operand OPNO of INSN. */
1986
1987static const struct mips_operand *
1988insn_opno (const struct mips_cl_insn *insn, unsigned opno)
1989{
1990 const struct mips_operand_array *operands;
1991
1992 operands = insn_operands (insn);
1993 if (opno >= MAX_OPERANDS || !operands->operand[opno])
1994 abort ();
1995 return operands->operand[opno];
1996}
1997
e077a1c8
RS
1998/* Install UVAL as the value of OPERAND in INSN. */
1999
2000static inline void
2001insn_insert_operand (struct mips_cl_insn *insn,
2002 const struct mips_operand *operand, unsigned int uval)
2003{
2004 insn->insn_opcode = mips_insert_operand (operand, insn->insn_opcode, uval);
2005}
2006
fc76e730
RS
2007/* Extract the value of OPERAND from INSN. */
2008
2009static inline unsigned
2010insn_extract_operand (const struct mips_cl_insn *insn,
2011 const struct mips_operand *operand)
2012{
2013 return mips_extract_operand (operand, insn->insn_opcode);
2014}
2015
df58fc94 2016/* Record the current MIPS16/microMIPS mode in now_seg. */
742a56fe
RS
2017
2018static void
df58fc94 2019mips_record_compressed_mode (void)
742a56fe
RS
2020{
2021 segment_info_type *si;
2022
2023 si = seg_info (now_seg);
2024 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2025 si->tc_segment_info_data.mips16 = mips_opts.mips16;
df58fc94
RS
2026 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2027 si->tc_segment_info_data.micromips = mips_opts.micromips;
742a56fe
RS
2028}
2029
4d68580a
RS
2030/* Read a standard MIPS instruction from BUF. */
2031
2032static unsigned long
2033read_insn (char *buf)
2034{
2035 if (target_big_endian)
2036 return bfd_getb32 ((bfd_byte *) buf);
2037 else
2038 return bfd_getl32 ((bfd_byte *) buf);
2039}
2040
2041/* Write standard MIPS instruction INSN to BUF. Return a pointer to
2042 the next byte. */
2043
2044static char *
2045write_insn (char *buf, unsigned int insn)
2046{
2047 md_number_to_chars (buf, insn, 4);
2048 return buf + 4;
2049}
2050
2051/* Read a microMIPS or MIPS16 opcode from BUF, given that it
2052 has length LENGTH. */
2053
2054static unsigned long
2055read_compressed_insn (char *buf, unsigned int length)
2056{
2057 unsigned long insn;
2058 unsigned int i;
2059
2060 insn = 0;
2061 for (i = 0; i < length; i += 2)
2062 {
2063 insn <<= 16;
2064 if (target_big_endian)
2065 insn |= bfd_getb16 ((char *) buf);
2066 else
2067 insn |= bfd_getl16 ((char *) buf);
2068 buf += 2;
2069 }
2070 return insn;
2071}
2072
5c04167a
RS
2073/* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2074 instruction is LENGTH bytes long. Return a pointer to the next byte. */
2075
2076static char *
2077write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2078{
2079 unsigned int i;
2080
2081 for (i = 0; i < length; i += 2)
2082 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2083 return buf + length;
2084}
2085
1e915849
RS
2086/* Install INSN at the location specified by its "frag" and "where" fields. */
2087
2088static void
2089install_insn (const struct mips_cl_insn *insn)
2090{
2091 char *f = insn->frag->fr_literal + insn->where;
5c04167a
RS
2092 if (HAVE_CODE_COMPRESSION)
2093 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
1e915849 2094 else
4d68580a 2095 write_insn (f, insn->insn_opcode);
df58fc94 2096 mips_record_compressed_mode ();
1e915849
RS
2097}
2098
2099/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
2100 and install the opcode in the new location. */
2101
2102static void
2103move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2104{
2105 size_t i;
2106
2107 insn->frag = frag;
2108 insn->where = where;
2109 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2110 if (insn->fixp[i] != NULL)
2111 {
2112 insn->fixp[i]->fx_frag = frag;
2113 insn->fixp[i]->fx_where = where;
2114 }
2115 install_insn (insn);
2116}
2117
2118/* Add INSN to the end of the output. */
2119
2120static void
2121add_fixed_insn (struct mips_cl_insn *insn)
2122{
2123 char *f = frag_more (insn_length (insn));
2124 move_insn (insn, frag_now, f - frag_now->fr_literal);
2125}
2126
2127/* Start a variant frag and move INSN to the start of the variant part,
2128 marking it as fixed. The other arguments are as for frag_var. */
2129
2130static void
2131add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2132 relax_substateT subtype, symbolS *symbol, offsetT offset)
2133{
2134 frag_grow (max_chars);
2135 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2136 insn->fixed_p = 1;
2137 frag_var (rs_machine_dependent, max_chars, var,
2138 subtype, symbol, offset, NULL);
2139}
2140
2141/* Insert N copies of INSN into the history buffer, starting at
2142 position FIRST. Neither FIRST nor N need to be clipped. */
2143
2144static void
2145insert_into_history (unsigned int first, unsigned int n,
2146 const struct mips_cl_insn *insn)
2147{
2148 if (mips_relax.sequence != 2)
2149 {
2150 unsigned int i;
2151
2152 for (i = ARRAY_SIZE (history); i-- > first;)
2153 if (i >= first + n)
2154 history[i] = history[i - n];
2155 else
2156 history[i] = *insn;
2157 }
2158}
2159
71400594
RS
2160/* Initialize vr4120_conflicts. There is a bit of duplication here:
2161 the idea is to make it obvious at a glance that each errata is
2162 included. */
2163
2164static void
2165init_vr4120_conflicts (void)
2166{
2167#define CONFLICT(FIRST, SECOND) \
2168 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2169
2170 /* Errata 21 - [D]DIV[U] after [D]MACC */
2171 CONFLICT (MACC, DIV);
2172 CONFLICT (DMACC, DIV);
2173
2174 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
2175 CONFLICT (DMULT, DMULT);
2176 CONFLICT (DMULT, DMACC);
2177 CONFLICT (DMACC, DMULT);
2178 CONFLICT (DMACC, DMACC);
2179
2180 /* Errata 24 - MT{LO,HI} after [D]MACC */
2181 CONFLICT (MACC, MTHILO);
2182 CONFLICT (DMACC, MTHILO);
2183
2184 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2185 instruction is executed immediately after a MACC or DMACC
2186 instruction, the result of [either instruction] is incorrect." */
2187 CONFLICT (MACC, MULT);
2188 CONFLICT (MACC, DMULT);
2189 CONFLICT (DMACC, MULT);
2190 CONFLICT (DMACC, DMULT);
2191
2192 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2193 executed immediately after a DMULT, DMULTU, DIV, DIVU,
2194 DDIV or DDIVU instruction, the result of the MACC or
2195 DMACC instruction is incorrect.". */
2196 CONFLICT (DMULT, MACC);
2197 CONFLICT (DMULT, DMACC);
2198 CONFLICT (DIV, MACC);
2199 CONFLICT (DIV, DMACC);
2200
2201#undef CONFLICT
2202}
2203
707bfff6
TS
2204struct regname {
2205 const char *name;
2206 unsigned int num;
2207};
2208
2209#define RTYPE_MASK 0x1ff00
2210#define RTYPE_NUM 0x00100
2211#define RTYPE_FPU 0x00200
2212#define RTYPE_FCC 0x00400
2213#define RTYPE_VEC 0x00800
2214#define RTYPE_GP 0x01000
2215#define RTYPE_CP0 0x02000
2216#define RTYPE_PC 0x04000
2217#define RTYPE_ACC 0x08000
2218#define RTYPE_CCC 0x10000
2219#define RNUM_MASK 0x000ff
2220#define RWARN 0x80000
2221
2222#define GENERIC_REGISTER_NUMBERS \
2223 {"$0", RTYPE_NUM | 0}, \
2224 {"$1", RTYPE_NUM | 1}, \
2225 {"$2", RTYPE_NUM | 2}, \
2226 {"$3", RTYPE_NUM | 3}, \
2227 {"$4", RTYPE_NUM | 4}, \
2228 {"$5", RTYPE_NUM | 5}, \
2229 {"$6", RTYPE_NUM | 6}, \
2230 {"$7", RTYPE_NUM | 7}, \
2231 {"$8", RTYPE_NUM | 8}, \
2232 {"$9", RTYPE_NUM | 9}, \
2233 {"$10", RTYPE_NUM | 10}, \
2234 {"$11", RTYPE_NUM | 11}, \
2235 {"$12", RTYPE_NUM | 12}, \
2236 {"$13", RTYPE_NUM | 13}, \
2237 {"$14", RTYPE_NUM | 14}, \
2238 {"$15", RTYPE_NUM | 15}, \
2239 {"$16", RTYPE_NUM | 16}, \
2240 {"$17", RTYPE_NUM | 17}, \
2241 {"$18", RTYPE_NUM | 18}, \
2242 {"$19", RTYPE_NUM | 19}, \
2243 {"$20", RTYPE_NUM | 20}, \
2244 {"$21", RTYPE_NUM | 21}, \
2245 {"$22", RTYPE_NUM | 22}, \
2246 {"$23", RTYPE_NUM | 23}, \
2247 {"$24", RTYPE_NUM | 24}, \
2248 {"$25", RTYPE_NUM | 25}, \
2249 {"$26", RTYPE_NUM | 26}, \
2250 {"$27", RTYPE_NUM | 27}, \
2251 {"$28", RTYPE_NUM | 28}, \
2252 {"$29", RTYPE_NUM | 29}, \
2253 {"$30", RTYPE_NUM | 30}, \
2254 {"$31", RTYPE_NUM | 31}
2255
2256#define FPU_REGISTER_NAMES \
2257 {"$f0", RTYPE_FPU | 0}, \
2258 {"$f1", RTYPE_FPU | 1}, \
2259 {"$f2", RTYPE_FPU | 2}, \
2260 {"$f3", RTYPE_FPU | 3}, \
2261 {"$f4", RTYPE_FPU | 4}, \
2262 {"$f5", RTYPE_FPU | 5}, \
2263 {"$f6", RTYPE_FPU | 6}, \
2264 {"$f7", RTYPE_FPU | 7}, \
2265 {"$f8", RTYPE_FPU | 8}, \
2266 {"$f9", RTYPE_FPU | 9}, \
2267 {"$f10", RTYPE_FPU | 10}, \
2268 {"$f11", RTYPE_FPU | 11}, \
2269 {"$f12", RTYPE_FPU | 12}, \
2270 {"$f13", RTYPE_FPU | 13}, \
2271 {"$f14", RTYPE_FPU | 14}, \
2272 {"$f15", RTYPE_FPU | 15}, \
2273 {"$f16", RTYPE_FPU | 16}, \
2274 {"$f17", RTYPE_FPU | 17}, \
2275 {"$f18", RTYPE_FPU | 18}, \
2276 {"$f19", RTYPE_FPU | 19}, \
2277 {"$f20", RTYPE_FPU | 20}, \
2278 {"$f21", RTYPE_FPU | 21}, \
2279 {"$f22", RTYPE_FPU | 22}, \
2280 {"$f23", RTYPE_FPU | 23}, \
2281 {"$f24", RTYPE_FPU | 24}, \
2282 {"$f25", RTYPE_FPU | 25}, \
2283 {"$f26", RTYPE_FPU | 26}, \
2284 {"$f27", RTYPE_FPU | 27}, \
2285 {"$f28", RTYPE_FPU | 28}, \
2286 {"$f29", RTYPE_FPU | 29}, \
2287 {"$f30", RTYPE_FPU | 30}, \
2288 {"$f31", RTYPE_FPU | 31}
2289
2290#define FPU_CONDITION_CODE_NAMES \
2291 {"$fcc0", RTYPE_FCC | 0}, \
2292 {"$fcc1", RTYPE_FCC | 1}, \
2293 {"$fcc2", RTYPE_FCC | 2}, \
2294 {"$fcc3", RTYPE_FCC | 3}, \
2295 {"$fcc4", RTYPE_FCC | 4}, \
2296 {"$fcc5", RTYPE_FCC | 5}, \
2297 {"$fcc6", RTYPE_FCC | 6}, \
2298 {"$fcc7", RTYPE_FCC | 7}
2299
2300#define COPROC_CONDITION_CODE_NAMES \
2301 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
2302 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
2303 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
2304 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
2305 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
2306 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
2307 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
2308 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
2309
2310#define N32N64_SYMBOLIC_REGISTER_NAMES \
2311 {"$a4", RTYPE_GP | 8}, \
2312 {"$a5", RTYPE_GP | 9}, \
2313 {"$a6", RTYPE_GP | 10}, \
2314 {"$a7", RTYPE_GP | 11}, \
2315 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
2316 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
2317 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
2318 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
2319 {"$t0", RTYPE_GP | 12}, \
2320 {"$t1", RTYPE_GP | 13}, \
2321 {"$t2", RTYPE_GP | 14}, \
2322 {"$t3", RTYPE_GP | 15}
2323
2324#define O32_SYMBOLIC_REGISTER_NAMES \
2325 {"$t0", RTYPE_GP | 8}, \
2326 {"$t1", RTYPE_GP | 9}, \
2327 {"$t2", RTYPE_GP | 10}, \
2328 {"$t3", RTYPE_GP | 11}, \
2329 {"$t4", RTYPE_GP | 12}, \
2330 {"$t5", RTYPE_GP | 13}, \
2331 {"$t6", RTYPE_GP | 14}, \
2332 {"$t7", RTYPE_GP | 15}, \
2333 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2334 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2335 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
2336 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
2337
2338/* Remaining symbolic register names */
2339#define SYMBOLIC_REGISTER_NAMES \
2340 {"$zero", RTYPE_GP | 0}, \
2341 {"$at", RTYPE_GP | 1}, \
2342 {"$AT", RTYPE_GP | 1}, \
2343 {"$v0", RTYPE_GP | 2}, \
2344 {"$v1", RTYPE_GP | 3}, \
2345 {"$a0", RTYPE_GP | 4}, \
2346 {"$a1", RTYPE_GP | 5}, \
2347 {"$a2", RTYPE_GP | 6}, \
2348 {"$a3", RTYPE_GP | 7}, \
2349 {"$s0", RTYPE_GP | 16}, \
2350 {"$s1", RTYPE_GP | 17}, \
2351 {"$s2", RTYPE_GP | 18}, \
2352 {"$s3", RTYPE_GP | 19}, \
2353 {"$s4", RTYPE_GP | 20}, \
2354 {"$s5", RTYPE_GP | 21}, \
2355 {"$s6", RTYPE_GP | 22}, \
2356 {"$s7", RTYPE_GP | 23}, \
2357 {"$t8", RTYPE_GP | 24}, \
2358 {"$t9", RTYPE_GP | 25}, \
2359 {"$k0", RTYPE_GP | 26}, \
2360 {"$kt0", RTYPE_GP | 26}, \
2361 {"$k1", RTYPE_GP | 27}, \
2362 {"$kt1", RTYPE_GP | 27}, \
2363 {"$gp", RTYPE_GP | 28}, \
2364 {"$sp", RTYPE_GP | 29}, \
2365 {"$s8", RTYPE_GP | 30}, \
2366 {"$fp", RTYPE_GP | 30}, \
2367 {"$ra", RTYPE_GP | 31}
2368
2369#define MIPS16_SPECIAL_REGISTER_NAMES \
2370 {"$pc", RTYPE_PC | 0}
2371
2372#define MDMX_VECTOR_REGISTER_NAMES \
2373 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
2374 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
2375 {"$v2", RTYPE_VEC | 2}, \
2376 {"$v3", RTYPE_VEC | 3}, \
2377 {"$v4", RTYPE_VEC | 4}, \
2378 {"$v5", RTYPE_VEC | 5}, \
2379 {"$v6", RTYPE_VEC | 6}, \
2380 {"$v7", RTYPE_VEC | 7}, \
2381 {"$v8", RTYPE_VEC | 8}, \
2382 {"$v9", RTYPE_VEC | 9}, \
2383 {"$v10", RTYPE_VEC | 10}, \
2384 {"$v11", RTYPE_VEC | 11}, \
2385 {"$v12", RTYPE_VEC | 12}, \
2386 {"$v13", RTYPE_VEC | 13}, \
2387 {"$v14", RTYPE_VEC | 14}, \
2388 {"$v15", RTYPE_VEC | 15}, \
2389 {"$v16", RTYPE_VEC | 16}, \
2390 {"$v17", RTYPE_VEC | 17}, \
2391 {"$v18", RTYPE_VEC | 18}, \
2392 {"$v19", RTYPE_VEC | 19}, \
2393 {"$v20", RTYPE_VEC | 20}, \
2394 {"$v21", RTYPE_VEC | 21}, \
2395 {"$v22", RTYPE_VEC | 22}, \
2396 {"$v23", RTYPE_VEC | 23}, \
2397 {"$v24", RTYPE_VEC | 24}, \
2398 {"$v25", RTYPE_VEC | 25}, \
2399 {"$v26", RTYPE_VEC | 26}, \
2400 {"$v27", RTYPE_VEC | 27}, \
2401 {"$v28", RTYPE_VEC | 28}, \
2402 {"$v29", RTYPE_VEC | 29}, \
2403 {"$v30", RTYPE_VEC | 30}, \
2404 {"$v31", RTYPE_VEC | 31}
2405
2406#define MIPS_DSP_ACCUMULATOR_NAMES \
2407 {"$ac0", RTYPE_ACC | 0}, \
2408 {"$ac1", RTYPE_ACC | 1}, \
2409 {"$ac2", RTYPE_ACC | 2}, \
2410 {"$ac3", RTYPE_ACC | 3}
2411
2412static const struct regname reg_names[] = {
2413 GENERIC_REGISTER_NUMBERS,
2414 FPU_REGISTER_NAMES,
2415 FPU_CONDITION_CODE_NAMES,
2416 COPROC_CONDITION_CODE_NAMES,
2417
2418 /* The $txx registers depends on the abi,
2419 these will be added later into the symbol table from
2420 one of the tables below once mips_abi is set after
2421 parsing of arguments from the command line. */
2422 SYMBOLIC_REGISTER_NAMES,
2423
2424 MIPS16_SPECIAL_REGISTER_NAMES,
2425 MDMX_VECTOR_REGISTER_NAMES,
2426 MIPS_DSP_ACCUMULATOR_NAMES,
2427 {0, 0}
2428};
2429
2430static const struct regname reg_names_o32[] = {
2431 O32_SYMBOLIC_REGISTER_NAMES,
2432 {0, 0}
2433};
2434
2435static const struct regname reg_names_n32n64[] = {
2436 N32N64_SYMBOLIC_REGISTER_NAMES,
2437 {0, 0}
2438};
2439
a92713e6
RS
2440/* Register symbols $v0 and $v1 map to GPRs 2 and 3, but they can also be
2441 interpreted as vector registers 0 and 1. If SYMVAL is the value of one
2442 of these register symbols, return the associated vector register,
2443 otherwise return SYMVAL itself. */
df58fc94 2444
a92713e6
RS
2445static unsigned int
2446mips_prefer_vec_regno (unsigned int symval)
707bfff6 2447{
a92713e6
RS
2448 if ((symval & -2) == (RTYPE_GP | 2))
2449 return RTYPE_VEC | (symval & 1);
2450 return symval;
2451}
2452
2453/* Return true if the string at *SPTR is a valid register name. If so,
2454 move *SPTR past the register and store the register's symbol value
2455 in *SYMVAL. This symbol value includes the register number
2456 (RNUM_MASK) and register type (RTYPE_MASK). */
2457
2458static bfd_boolean
2459mips_parse_register (char **sptr, unsigned int *symval)
2460{
2461 symbolS *symbol;
2462 char *s, *e;
707bfff6 2463 char save_c;
707bfff6
TS
2464
2465 /* Find end of name. */
a92713e6 2466 s = e = *sptr;
707bfff6
TS
2467 if (is_name_beginner (*e))
2468 ++e;
2469 while (is_part_of_name (*e))
2470 ++e;
2471
2472 /* Terminate name. */
2473 save_c = *e;
2474 *e = '\0';
2475
a92713e6
RS
2476 /* Look up the name. */
2477 symbol = symbol_find (s);
2478 *e = save_c;
2479
2480 if (!symbol || S_GET_SEGMENT (symbol) != reg_section)
2481 return FALSE;
2482
2483 *sptr = e;
2484 *symval = S_GET_VALUE (symbol);
2485 return TRUE;
2486}
2487
2488/* Check if SPTR points at a valid register specifier according to TYPES.
2489 If so, then return 1, advance S to consume the specifier and store
2490 the register's number in REGNOP, otherwise return 0. */
2491
2492static int
2493reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2494{
2495 unsigned int regno;
2496
2497 if (mips_parse_register (s, &regno))
707bfff6 2498 {
a92713e6
RS
2499 if (types & RTYPE_VEC)
2500 regno = mips_prefer_vec_regno (regno);
2501 if (regno & types)
2502 regno &= RNUM_MASK;
2503 else
2504 regno = ~0;
707bfff6 2505 }
a92713e6 2506 else
707bfff6 2507 {
a92713e6
RS
2508 if (types & RWARN)
2509 as_warn (_("Unrecognized register name `%s'"), *s);
2510 regno = ~0;
707bfff6 2511 }
707bfff6 2512 if (regnop)
a92713e6
RS
2513 *regnop = regno;
2514 return regno <= RNUM_MASK;
707bfff6
TS
2515}
2516
a92713e6
RS
2517/* Token types for parsed operand lists. */
2518enum mips_operand_token_type {
2519 /* A plain register, e.g. $f2. */
2520 OT_REG,
df58fc94 2521
a92713e6
RS
2522 /* An element of a vector, e.g. $v0[1]. */
2523 OT_REG_ELEMENT,
df58fc94 2524
a92713e6
RS
2525 /* A continuous range of registers, e.g. $s0-$s4. */
2526 OT_REG_RANGE,
2527
2528 /* A (possibly relocated) expression. */
2529 OT_INTEGER,
2530
2531 /* A floating-point value. */
2532 OT_FLOAT,
2533
2534 /* A single character. This can be '(', ')' or ',', but '(' only appears
2535 before OT_REGs. */
2536 OT_CHAR,
2537
2538 /* The end of the operand list. */
2539 OT_END
2540};
2541
2542/* A parsed operand token. */
2543struct mips_operand_token
2544{
2545 /* The type of token. */
2546 enum mips_operand_token_type type;
2547 union
2548 {
2549 /* The register symbol value for an OT_REG. */
2550 unsigned int regno;
2551
2552 /* The register symbol value and index for an OT_REG_ELEMENT. */
2553 struct {
2554 unsigned int regno;
2555 addressT index;
2556 } reg_element;
2557
2558 /* The two register symbol values involved in an OT_REG_RANGE. */
2559 struct {
2560 unsigned int regno1;
2561 unsigned int regno2;
2562 } reg_range;
2563
2564 /* The value of an OT_INTEGER. The value is represented as an
2565 expression and the relocation operators that were applied to
2566 that expression. The reloc entries are BFD_RELOC_UNUSED if no
2567 relocation operators were used. */
2568 struct {
2569 expressionS value;
2570 bfd_reloc_code_real_type relocs[3];
2571 } integer;
2572
2573 /* The binary data for an OT_FLOAT constant, and the number of bytes
2574 in the constant. */
2575 struct {
2576 unsigned char data[8];
2577 int length;
2578 } flt;
2579
2580 /* The character represented by an OT_CHAR. */
2581 char ch;
2582 } u;
2583};
2584
2585/* An obstack used to construct lists of mips_operand_tokens. */
2586static struct obstack mips_operand_tokens;
2587
2588/* Give TOKEN type TYPE and add it to mips_operand_tokens. */
2589
2590static void
2591mips_add_token (struct mips_operand_token *token,
2592 enum mips_operand_token_type type)
2593{
2594 token->type = type;
2595 obstack_grow (&mips_operand_tokens, token, sizeof (*token));
2596}
2597
2598/* Check whether S is '(' followed by a register name. Add OT_CHAR
2599 and OT_REG tokens for them if so, and return a pointer to the first
2600 unconsumed character. Return null otherwise. */
2601
2602static char *
2603mips_parse_base_start (char *s)
2604{
2605 struct mips_operand_token token;
2309ddf2 2606 unsigned int regno;
df58fc94 2607
a92713e6
RS
2608 if (*s != '(')
2609 return 0;
2610
2611 ++s;
2612 SKIP_SPACE_TABS (s);
2613 if (!mips_parse_register (&s, &regno))
2614 return 0;
2615
2616 token.u.ch = '(';
2617 mips_add_token (&token, OT_CHAR);
2618
2619 token.u.regno = regno;
2620 mips_add_token (&token, OT_REG);
2621
2622 return s;
2623}
2624
2625/* Parse one or more tokens from S. Return a pointer to the first
2626 unconsumed character on success. Return null if an error was found
2627 and store the error text in insn_error. FLOAT_FORMAT is as for
2628 mips_parse_arguments. */
2629
2630static char *
2631mips_parse_argument_token (char *s, char float_format)
2632{
2633 char *end, *save_in, *err;
2634 unsigned int regno1, regno2;
2635 struct mips_operand_token token;
2636
2637 /* First look for "($reg", since we want to treat that as an
2638 OT_CHAR and OT_REG rather than an expression. */
2639 end = mips_parse_base_start (s);
2640 if (end)
2641 return end;
2642
2643 /* Handle other characters that end up as OT_CHARs. */
2644 if (*s == ')' || *s == ',')
2645 {
2646 token.u.ch = *s;
2647 mips_add_token (&token, OT_CHAR);
2648 ++s;
2649 return s;
2650 }
2651
2652 /* Handle tokens that start with a register. */
2653 if (mips_parse_register (&s, &regno1))
df58fc94 2654 {
a92713e6
RS
2655 SKIP_SPACE_TABS (s);
2656 if (*s == '-')
df58fc94 2657 {
a92713e6
RS
2658 /* A register range. */
2659 ++s;
2660 SKIP_SPACE_TABS (s);
2661 if (!mips_parse_register (&s, &regno2))
2662 {
2663 insn_error = _("Invalid register range");
2664 return 0;
2665 }
df58fc94 2666
a92713e6
RS
2667 token.u.reg_range.regno1 = regno1;
2668 token.u.reg_range.regno2 = regno2;
2669 mips_add_token (&token, OT_REG_RANGE);
2670 return s;
2671 }
2672 else if (*s == '[')
df58fc94 2673 {
a92713e6
RS
2674 /* A vector element. */
2675 expressionS element;
2676
2677 ++s;
2678 SKIP_SPACE_TABS (s);
2679 my_getExpression (&element, s);
2680 if (element.X_op != O_constant)
2681 {
2682 insn_error = _("Vector element must be constant");
2683 return 0;
2684 }
2685 s = expr_end;
2686 SKIP_SPACE_TABS (s);
2687 if (*s != ']')
2688 {
2689 insn_error = _("Missing `]'");
2690 return 0;
2691 }
2692 ++s;
2693
2694 token.u.reg_element.regno = regno1;
2695 token.u.reg_element.index = element.X_add_number;
2696 mips_add_token (&token, OT_REG_ELEMENT);
2697 return s;
df58fc94 2698 }
df58fc94 2699
a92713e6
RS
2700 /* Looks like just a plain register. */
2701 token.u.regno = regno1;
2702 mips_add_token (&token, OT_REG);
2703 return s;
df58fc94
RS
2704 }
2705
a92713e6
RS
2706 if (float_format)
2707 {
2708 /* First try to treat expressions as floats. */
2709 save_in = input_line_pointer;
2710 input_line_pointer = s;
2711 err = md_atof (float_format, (char *) token.u.flt.data,
2712 &token.u.flt.length);
2713 end = input_line_pointer;
2714 input_line_pointer = save_in;
2715 if (err && *err)
2716 {
2717 insn_error = err;
2718 return 0;
2719 }
2720 if (s != end)
2721 {
2722 mips_add_token (&token, OT_FLOAT);
2723 return end;
2724 }
2725 }
2726
2727 /* Treat everything else as an integer expression. */
2728 token.u.integer.relocs[0] = BFD_RELOC_UNUSED;
2729 token.u.integer.relocs[1] = BFD_RELOC_UNUSED;
2730 token.u.integer.relocs[2] = BFD_RELOC_UNUSED;
2731 my_getSmallExpression (&token.u.integer.value, token.u.integer.relocs, s);
2732 s = expr_end;
2733 mips_add_token (&token, OT_INTEGER);
2734 return s;
2735}
2736
2737/* S points to the operand list for an instruction. FLOAT_FORMAT is 'f'
2738 if expressions should be treated as 32-bit floating-point constants,
2739 'd' if they should be treated as 64-bit floating-point constants,
2740 or 0 if they should be treated as integer expressions (the usual case).
2741
2742 Return a list of tokens on success, otherwise return 0. The caller
2743 must obstack_free the list after use. */
2744
2745static struct mips_operand_token *
2746mips_parse_arguments (char *s, char float_format)
2747{
2748 struct mips_operand_token token;
2749
2750 SKIP_SPACE_TABS (s);
2751 while (*s)
2752 {
2753 s = mips_parse_argument_token (s, float_format);
2754 if (!s)
2755 {
2756 obstack_free (&mips_operand_tokens,
2757 obstack_finish (&mips_operand_tokens));
2758 return 0;
2759 }
2760 SKIP_SPACE_TABS (s);
2761 }
2762 mips_add_token (&token, OT_END);
2763 return (struct mips_operand_token *) obstack_finish (&mips_operand_tokens);
df58fc94
RS
2764}
2765
d301a56b
RS
2766/* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
2767 and architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
037b32b9
AN
2768
2769static bfd_boolean
f79e2745 2770is_opcode_valid (const struct mips_opcode *mo)
037b32b9
AN
2771{
2772 int isa = mips_opts.isa;
846ef2d0 2773 int ase = mips_opts.ase;
037b32b9 2774 int fp_s, fp_d;
c6278170 2775 unsigned int i;
037b32b9 2776
c6278170
RS
2777 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
2778 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2779 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
2780 ase |= mips_ases[i].flags64;
037b32b9 2781
d301a56b 2782 if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
037b32b9
AN
2783 return FALSE;
2784
2785 /* Check whether the instruction or macro requires single-precision or
2786 double-precision floating-point support. Note that this information is
2787 stored differently in the opcode table for insns and macros. */
2788 if (mo->pinfo == INSN_MACRO)
2789 {
2790 fp_s = mo->pinfo2 & INSN2_M_FP_S;
2791 fp_d = mo->pinfo2 & INSN2_M_FP_D;
2792 }
2793 else
2794 {
2795 fp_s = mo->pinfo & FP_S;
2796 fp_d = mo->pinfo & FP_D;
2797 }
2798
2799 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
2800 return FALSE;
2801
2802 if (fp_s && mips_opts.soft_float)
2803 return FALSE;
2804
2805 return TRUE;
2806}
2807
2808/* Return TRUE if the MIPS16 opcode MO is valid on the currently
2809 selected ISA and architecture. */
2810
2811static bfd_boolean
2812is_opcode_valid_16 (const struct mips_opcode *mo)
2813{
d301a56b 2814 return opcode_is_member (mo, mips_opts.isa, 0, mips_opts.arch);
037b32b9
AN
2815}
2816
df58fc94
RS
2817/* Return TRUE if the size of the microMIPS opcode MO matches one
2818 explicitly requested. Always TRUE in the standard MIPS mode. */
2819
2820static bfd_boolean
2821is_size_valid (const struct mips_opcode *mo)
2822{
2823 if (!mips_opts.micromips)
2824 return TRUE;
2825
833794fc
MR
2826 if (mips_opts.insn32)
2827 {
2828 if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
2829 return FALSE;
2830 if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
2831 return FALSE;
2832 }
df58fc94
RS
2833 if (!forced_insn_length)
2834 return TRUE;
2835 if (mo->pinfo == INSN_MACRO)
2836 return FALSE;
2837 return forced_insn_length == micromips_insn_length (mo);
2838}
2839
2840/* Return TRUE if the microMIPS opcode MO is valid for the delay slot
e64af278
MR
2841 of the preceding instruction. Always TRUE in the standard MIPS mode.
2842
2843 We don't accept macros in 16-bit delay slots to avoid a case where
2844 a macro expansion fails because it relies on a preceding 32-bit real
2845 instruction to have matched and does not handle the operands correctly.
2846 The only macros that may expand to 16-bit instructions are JAL that
2847 cannot be placed in a delay slot anyway, and corner cases of BALIGN
2848 and BGT (that likewise cannot be placed in a delay slot) that decay to
2849 a NOP. In all these cases the macros precede any corresponding real
2850 instruction definitions in the opcode table, so they will match in the
2851 second pass where the size of the delay slot is ignored and therefore
2852 produce correct code. */
df58fc94
RS
2853
2854static bfd_boolean
2855is_delay_slot_valid (const struct mips_opcode *mo)
2856{
2857 if (!mips_opts.micromips)
2858 return TRUE;
2859
2860 if (mo->pinfo == INSN_MACRO)
c06dec14 2861 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
df58fc94
RS
2862 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
2863 && micromips_insn_length (mo) != 4)
2864 return FALSE;
2865 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
2866 && micromips_insn_length (mo) != 2)
2867 return FALSE;
2868
2869 return TRUE;
2870}
2871
fc76e730
RS
2872/* For consistency checking, verify that all bits of OPCODE are specified
2873 either by the match/mask part of the instruction definition, or by the
2874 operand list. Also build up a list of operands in OPERANDS.
2875
2876 INSN_BITS says which bits of the instruction are significant.
2877 If OPCODE is a standard or microMIPS instruction, DECODE_OPERAND
2878 provides the mips_operand description of each operand. DECODE_OPERAND
2879 is null for MIPS16 instructions. */
ab902481
RS
2880
2881static int
2882validate_mips_insn (const struct mips_opcode *opcode,
2883 unsigned long insn_bits,
fc76e730
RS
2884 const struct mips_operand *(*decode_operand) (const char *),
2885 struct mips_operand_array *operands)
ab902481
RS
2886{
2887 const char *s;
fc76e730 2888 unsigned long used_bits, doubled, undefined, opno, mask;
ab902481
RS
2889 const struct mips_operand *operand;
2890
fc76e730
RS
2891 mask = (opcode->pinfo == INSN_MACRO ? 0 : opcode->mask);
2892 if ((mask & opcode->match) != opcode->match)
ab902481
RS
2893 {
2894 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
2895 opcode->name, opcode->args);
2896 return 0;
2897 }
2898 used_bits = 0;
fc76e730 2899 opno = 0;
ab902481
RS
2900 for (s = opcode->args; *s; ++s)
2901 switch (*s)
2902 {
2903 case ',':
2904 case '(':
2905 case ')':
2906 break;
2907
2908 default:
fc76e730
RS
2909 if (!decode_operand)
2910 operand = decode_mips16_operand (*s, FALSE);
2911 else
2912 operand = decode_operand (s);
2913 if (!operand && opcode->pinfo != INSN_MACRO)
ab902481
RS
2914 {
2915 as_bad (_("internal: unknown operand type: %s %s"),
2916 opcode->name, opcode->args);
2917 return 0;
2918 }
fc76e730
RS
2919 gas_assert (opno < MAX_OPERANDS);
2920 operands->operand[opno] = operand;
2921 if (operand)
2922 {
2923 used_bits |= ((1 << operand->size) - 1) << operand->lsb;
2924 if (operand->type == OP_MDMX_IMM_REG)
2925 /* Bit 5 is the format selector (OB vs QH). The opcode table
2926 has separate entries for each format. */
2927 used_bits &= ~(1 << (operand->lsb + 5));
2928 if (operand->type == OP_ENTRY_EXIT_LIST)
2929 used_bits &= ~(mask & 0x700);
2930 }
ab902481 2931 /* Skip prefix characters. */
fc76e730 2932 if (decode_operand && (*s == '+' || *s == 'm'))
ab902481 2933 ++s;
fc76e730 2934 opno += 1;
ab902481
RS
2935 break;
2936 }
fc76e730 2937 doubled = used_bits & mask & insn_bits;
ab902481
RS
2938 if (doubled)
2939 {
2940 as_bad (_("internal: bad mips opcode (bits 0x%08lx doubly defined):"
2941 " %s %s"), doubled, opcode->name, opcode->args);
2942 return 0;
2943 }
fc76e730 2944 used_bits |= mask;
ab902481 2945 undefined = ~used_bits & insn_bits;
fc76e730 2946 if (opcode->pinfo != INSN_MACRO && undefined)
ab902481
RS
2947 {
2948 as_bad (_("internal: bad mips opcode (bits 0x%08lx undefined): %s %s"),
2949 undefined, opcode->name, opcode->args);
2950 return 0;
2951 }
2952 used_bits &= ~insn_bits;
2953 if (used_bits)
2954 {
2955 as_bad (_("internal: bad mips opcode (bits 0x%08lx defined): %s %s"),
2956 used_bits, opcode->name, opcode->args);
2957 return 0;
2958 }
2959 return 1;
2960}
2961
fc76e730
RS
2962/* The MIPS16 version of validate_mips_insn. */
2963
2964static int
2965validate_mips16_insn (const struct mips_opcode *opcode,
2966 struct mips_operand_array *operands)
2967{
2968 if (opcode->args[0] == 'a' || opcode->args[0] == 'i')
2969 {
2970 /* In this case OPCODE defines the first 16 bits in a 32-bit jump
2971 instruction. Use TMP to describe the full instruction. */
2972 struct mips_opcode tmp;
2973
2974 tmp = *opcode;
2975 tmp.match <<= 16;
2976 tmp.mask <<= 16;
2977 return validate_mips_insn (&tmp, 0xffffffff, 0, operands);
2978 }
2979 return validate_mips_insn (opcode, 0xffff, 0, operands);
2980}
2981
ab902481
RS
2982/* The microMIPS version of validate_mips_insn. */
2983
2984static int
fc76e730
RS
2985validate_micromips_insn (const struct mips_opcode *opc,
2986 struct mips_operand_array *operands)
ab902481
RS
2987{
2988 unsigned long insn_bits;
2989 unsigned long major;
2990 unsigned int length;
2991
fc76e730
RS
2992 if (opc->pinfo == INSN_MACRO)
2993 return validate_mips_insn (opc, 0xffffffff, decode_micromips_operand,
2994 operands);
2995
ab902481
RS
2996 length = micromips_insn_length (opc);
2997 if (length != 2 && length != 4)
2998 {
2999 as_bad (_("Internal error: bad microMIPS opcode (incorrect length: %u): "
3000 "%s %s"), length, opc->name, opc->args);
3001 return 0;
3002 }
3003 major = opc->match >> (10 + 8 * (length - 2));
3004 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
3005 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
3006 {
3007 as_bad (_("Internal error: bad microMIPS opcode "
3008 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
3009 return 0;
3010 }
3011
3012 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
3013 insn_bits = 1 << 4 * length;
3014 insn_bits <<= 4 * length;
3015 insn_bits -= 1;
fc76e730
RS
3016 return validate_mips_insn (opc, insn_bits, decode_micromips_operand,
3017 operands);
ab902481
RS
3018}
3019
707bfff6
TS
3020/* This function is called once, at assembler startup time. It should set up
3021 all the tables, etc. that the MD part of the assembler will need. */
156c2f8b 3022
252b5132 3023void
17a2f251 3024md_begin (void)
252b5132 3025{
3994f87e 3026 const char *retval = NULL;
156c2f8b 3027 int i = 0;
252b5132 3028 int broken = 0;
1f25f5d3 3029
0a44bf69
RS
3030 if (mips_pic != NO_PIC)
3031 {
3032 if (g_switch_seen && g_switch_value != 0)
3033 as_bad (_("-G may not be used in position-independent code"));
3034 g_switch_value = 0;
3035 }
3036
fef14a42 3037 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
252b5132
RH
3038 as_warn (_("Could not set architecture and machine"));
3039
252b5132
RH
3040 op_hash = hash_new ();
3041
fc76e730 3042 mips_operands = XCNEWVEC (struct mips_operand_array, NUMOPCODES);
252b5132
RH
3043 for (i = 0; i < NUMOPCODES;)
3044 {
3045 const char *name = mips_opcodes[i].name;
3046
17a2f251 3047 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
3048 if (retval != NULL)
3049 {
3050 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
3051 mips_opcodes[i].name, retval);
3052 /* Probably a memory allocation problem? Give up now. */
3053 as_fatal (_("Broken assembler. No assembly attempted."));
3054 }
3055 do
3056 {
fc76e730
RS
3057 if (!validate_mips_insn (&mips_opcodes[i], 0xffffffff,
3058 decode_mips_operand, &mips_operands[i]))
3059 broken = 1;
3060 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
252b5132 3061 {
fc76e730
RS
3062 create_insn (&nop_insn, mips_opcodes + i);
3063 if (mips_fix_loongson2f_nop)
3064 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
3065 nop_insn.fixed_p = 1;
252b5132
RH
3066 }
3067 ++i;
3068 }
3069 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
3070 }
3071
3072 mips16_op_hash = hash_new ();
fc76e730
RS
3073 mips16_operands = XCNEWVEC (struct mips_operand_array,
3074 bfd_mips16_num_opcodes);
252b5132
RH
3075
3076 i = 0;
3077 while (i < bfd_mips16_num_opcodes)
3078 {
3079 const char *name = mips16_opcodes[i].name;
3080
17a2f251 3081 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
3082 if (retval != NULL)
3083 as_fatal (_("internal: can't hash `%s': %s"),
3084 mips16_opcodes[i].name, retval);
3085 do
3086 {
fc76e730
RS
3087 if (!validate_mips16_insn (&mips16_opcodes[i], &mips16_operands[i]))
3088 broken = 1;
1e915849
RS
3089 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3090 {
3091 create_insn (&mips16_nop_insn, mips16_opcodes + i);
3092 mips16_nop_insn.fixed_p = 1;
3093 }
252b5132
RH
3094 ++i;
3095 }
3096 while (i < bfd_mips16_num_opcodes
3097 && strcmp (mips16_opcodes[i].name, name) == 0);
3098 }
3099
df58fc94 3100 micromips_op_hash = hash_new ();
fc76e730
RS
3101 micromips_operands = XCNEWVEC (struct mips_operand_array,
3102 bfd_micromips_num_opcodes);
df58fc94
RS
3103
3104 i = 0;
3105 while (i < bfd_micromips_num_opcodes)
3106 {
3107 const char *name = micromips_opcodes[i].name;
3108
3109 retval = hash_insert (micromips_op_hash, name,
3110 (void *) &micromips_opcodes[i]);
3111 if (retval != NULL)
3112 as_fatal (_("internal: can't hash `%s': %s"),
3113 micromips_opcodes[i].name, retval);
3114 do
fc76e730
RS
3115 {
3116 struct mips_cl_insn *micromips_nop_insn;
3117
3118 if (!validate_micromips_insn (&micromips_opcodes[i],
3119 &micromips_operands[i]))
3120 broken = 1;
3121
3122 if (micromips_opcodes[i].pinfo != INSN_MACRO)
3123 {
3124 if (micromips_insn_length (micromips_opcodes + i) == 2)
3125 micromips_nop_insn = &micromips_nop16_insn;
3126 else if (micromips_insn_length (micromips_opcodes + i) == 4)
3127 micromips_nop_insn = &micromips_nop32_insn;
3128 else
3129 continue;
3130
3131 if (micromips_nop_insn->insn_mo == NULL
3132 && strcmp (name, "nop") == 0)
3133 {
3134 create_insn (micromips_nop_insn, micromips_opcodes + i);
3135 micromips_nop_insn->fixed_p = 1;
3136 }
3137 }
3138 }
df58fc94
RS
3139 while (++i < bfd_micromips_num_opcodes
3140 && strcmp (micromips_opcodes[i].name, name) == 0);
3141 }
3142
252b5132
RH
3143 if (broken)
3144 as_fatal (_("Broken assembler. No assembly attempted."));
3145
3146 /* We add all the general register names to the symbol table. This
3147 helps us detect invalid uses of them. */
707bfff6
TS
3148 for (i = 0; reg_names[i].name; i++)
3149 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
8fc4ee9b 3150 reg_names[i].num, /* & RNUM_MASK, */
707bfff6
TS
3151 &zero_address_frag));
3152 if (HAVE_NEWABI)
3153 for (i = 0; reg_names_n32n64[i].name; i++)
3154 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
8fc4ee9b 3155 reg_names_n32n64[i].num, /* & RNUM_MASK, */
252b5132 3156 &zero_address_frag));
707bfff6
TS
3157 else
3158 for (i = 0; reg_names_o32[i].name; i++)
3159 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
8fc4ee9b 3160 reg_names_o32[i].num, /* & RNUM_MASK, */
6047c971 3161 &zero_address_frag));
6047c971 3162
a92713e6
RS
3163 obstack_init (&mips_operand_tokens);
3164
7d10b47d 3165 mips_no_prev_insn ();
252b5132
RH
3166
3167 mips_gprmask = 0;
3168 mips_cprmask[0] = 0;
3169 mips_cprmask[1] = 0;
3170 mips_cprmask[2] = 0;
3171 mips_cprmask[3] = 0;
3172
3173 /* set the default alignment for the text section (2**2) */
3174 record_alignment (text_section, 2);
3175
4d0d148d 3176 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132 3177
f3ded42a
RS
3178 /* On a native system other than VxWorks, sections must be aligned
3179 to 16 byte boundaries. When configured for an embedded ELF
3180 target, we don't bother. */
3181 if (strncmp (TARGET_OS, "elf", 3) != 0
3182 && strncmp (TARGET_OS, "vxworks", 7) != 0)
252b5132 3183 {
f3ded42a
RS
3184 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
3185 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
3186 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
3187 }
252b5132 3188
f3ded42a
RS
3189 /* Create a .reginfo section for register masks and a .mdebug
3190 section for debugging information. */
3191 {
3192 segT seg;
3193 subsegT subseg;
3194 flagword flags;
3195 segT sec;
3196
3197 seg = now_seg;
3198 subseg = now_subseg;
3199
3200 /* The ABI says this section should be loaded so that the
3201 running program can access it. However, we don't load it
3202 if we are configured for an embedded target */
3203 flags = SEC_READONLY | SEC_DATA;
3204 if (strncmp (TARGET_OS, "elf", 3) != 0)
3205 flags |= SEC_ALLOC | SEC_LOAD;
3206
3207 if (mips_abi != N64_ABI)
252b5132 3208 {
f3ded42a 3209 sec = subseg_new (".reginfo", (subsegT) 0);
bdaaa2e1 3210
f3ded42a
RS
3211 bfd_set_section_flags (stdoutput, sec, flags);
3212 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
252b5132 3213
f3ded42a
RS
3214 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
3215 }
3216 else
3217 {
3218 /* The 64-bit ABI uses a .MIPS.options section rather than
3219 .reginfo section. */
3220 sec = subseg_new (".MIPS.options", (subsegT) 0);
3221 bfd_set_section_flags (stdoutput, sec, flags);
3222 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132 3223
f3ded42a
RS
3224 /* Set up the option header. */
3225 {
3226 Elf_Internal_Options opthdr;
3227 char *f;
3228
3229 opthdr.kind = ODK_REGINFO;
3230 opthdr.size = (sizeof (Elf_External_Options)
3231 + sizeof (Elf64_External_RegInfo));
3232 opthdr.section = 0;
3233 opthdr.info = 0;
3234 f = frag_more (sizeof (Elf_External_Options));
3235 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
3236 (Elf_External_Options *) f);
3237
3238 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
3239 }
3240 }
252b5132 3241
f3ded42a
RS
3242 if (ECOFF_DEBUGGING)
3243 {
3244 sec = subseg_new (".mdebug", (subsegT) 0);
3245 (void) bfd_set_section_flags (stdoutput, sec,
3246 SEC_HAS_CONTENTS | SEC_READONLY);
3247 (void) bfd_set_section_alignment (stdoutput, sec, 2);
252b5132 3248 }
f3ded42a
RS
3249 else if (mips_flag_pdr)
3250 {
3251 pdr_seg = subseg_new (".pdr", (subsegT) 0);
3252 (void) bfd_set_section_flags (stdoutput, pdr_seg,
3253 SEC_READONLY | SEC_RELOC
3254 | SEC_DEBUGGING);
3255 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
3256 }
3257
3258 subseg_set (seg, subseg);
3259 }
252b5132
RH
3260
3261 if (! ECOFF_DEBUGGING)
3262 md_obj_begin ();
71400594
RS
3263
3264 if (mips_fix_vr4120)
3265 init_vr4120_conflicts ();
252b5132
RH
3266}
3267
3268void
17a2f251 3269md_mips_end (void)
252b5132 3270{
02b1ab82 3271 mips_emit_delays ();
252b5132
RH
3272 if (! ECOFF_DEBUGGING)
3273 md_obj_end ();
3274}
3275
3276void
17a2f251 3277md_assemble (char *str)
252b5132
RH
3278{
3279 struct mips_cl_insn insn;
f6688943
TS
3280 bfd_reloc_code_real_type unused_reloc[3]
3281 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
3282
3283 imm_expr.X_op = O_absent;
5f74bc13 3284 imm2_expr.X_op = O_absent;
252b5132 3285 offset_expr.X_op = O_absent;
f6688943
TS
3286 offset_reloc[0] = BFD_RELOC_UNUSED;
3287 offset_reloc[1] = BFD_RELOC_UNUSED;
3288 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132 3289
e1b47bd5
RS
3290 mips_mark_labels ();
3291 mips_assembling_insn = TRUE;
3292
252b5132
RH
3293 if (mips_opts.mips16)
3294 mips16_ip (str, &insn);
3295 else
3296 {
3297 mips_ip (str, &insn);
beae10d5
KH
3298 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
3299 str, insn.insn_opcode));
252b5132
RH
3300 }
3301
3302 if (insn_error)
e1b47bd5
RS
3303 as_bad ("%s `%s'", insn_error, str);
3304 else if (insn.insn_mo->pinfo == INSN_MACRO)
252b5132 3305 {
584892a6 3306 macro_start ();
252b5132
RH
3307 if (mips_opts.mips16)
3308 mips16_macro (&insn);
3309 else
833794fc 3310 macro (&insn, str);
584892a6 3311 macro_end ();
252b5132
RH
3312 }
3313 else
3314 {
77bd4346 3315 if (offset_expr.X_op != O_absent)
df58fc94 3316 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
252b5132 3317 else
df58fc94 3318 append_insn (&insn, NULL, unused_reloc, FALSE);
252b5132 3319 }
e1b47bd5
RS
3320
3321 mips_assembling_insn = FALSE;
252b5132
RH
3322}
3323
738e5348
RS
3324/* Convenience functions for abstracting away the differences between
3325 MIPS16 and non-MIPS16 relocations. */
3326
3327static inline bfd_boolean
3328mips16_reloc_p (bfd_reloc_code_real_type reloc)
3329{
3330 switch (reloc)
3331 {
3332 case BFD_RELOC_MIPS16_JMP:
3333 case BFD_RELOC_MIPS16_GPREL:
3334 case BFD_RELOC_MIPS16_GOT16:
3335 case BFD_RELOC_MIPS16_CALL16:
3336 case BFD_RELOC_MIPS16_HI16_S:
3337 case BFD_RELOC_MIPS16_HI16:
3338 case BFD_RELOC_MIPS16_LO16:
3339 return TRUE;
3340
3341 default:
3342 return FALSE;
3343 }
3344}
3345
df58fc94
RS
3346static inline bfd_boolean
3347micromips_reloc_p (bfd_reloc_code_real_type reloc)
3348{
3349 switch (reloc)
3350 {
3351 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
3352 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
3353 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
3354 case BFD_RELOC_MICROMIPS_GPREL16:
3355 case BFD_RELOC_MICROMIPS_JMP:
3356 case BFD_RELOC_MICROMIPS_HI16:
3357 case BFD_RELOC_MICROMIPS_HI16_S:
3358 case BFD_RELOC_MICROMIPS_LO16:
3359 case BFD_RELOC_MICROMIPS_LITERAL:
3360 case BFD_RELOC_MICROMIPS_GOT16:
3361 case BFD_RELOC_MICROMIPS_CALL16:
3362 case BFD_RELOC_MICROMIPS_GOT_HI16:
3363 case BFD_RELOC_MICROMIPS_GOT_LO16:
3364 case BFD_RELOC_MICROMIPS_CALL_HI16:
3365 case BFD_RELOC_MICROMIPS_CALL_LO16:
3366 case BFD_RELOC_MICROMIPS_SUB:
3367 case BFD_RELOC_MICROMIPS_GOT_PAGE:
3368 case BFD_RELOC_MICROMIPS_GOT_OFST:
3369 case BFD_RELOC_MICROMIPS_GOT_DISP:
3370 case BFD_RELOC_MICROMIPS_HIGHEST:
3371 case BFD_RELOC_MICROMIPS_HIGHER:
3372 case BFD_RELOC_MICROMIPS_SCN_DISP:
3373 case BFD_RELOC_MICROMIPS_JALR:
3374 return TRUE;
3375
3376 default:
3377 return FALSE;
3378 }
3379}
3380
2309ddf2
MR
3381static inline bfd_boolean
3382jmp_reloc_p (bfd_reloc_code_real_type reloc)
3383{
3384 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
3385}
3386
738e5348
RS
3387static inline bfd_boolean
3388got16_reloc_p (bfd_reloc_code_real_type reloc)
3389{
2309ddf2 3390 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
df58fc94 3391 || reloc == BFD_RELOC_MICROMIPS_GOT16);
738e5348
RS
3392}
3393
3394static inline bfd_boolean
3395hi16_reloc_p (bfd_reloc_code_real_type reloc)
3396{
2309ddf2 3397 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
df58fc94 3398 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
738e5348
RS
3399}
3400
3401static inline bfd_boolean
3402lo16_reloc_p (bfd_reloc_code_real_type reloc)
3403{
2309ddf2 3404 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
df58fc94
RS
3405 || reloc == BFD_RELOC_MICROMIPS_LO16);
3406}
3407
df58fc94
RS
3408static inline bfd_boolean
3409jalr_reloc_p (bfd_reloc_code_real_type reloc)
3410{
2309ddf2 3411 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
738e5348
RS
3412}
3413
f2ae14a1
RS
3414static inline bfd_boolean
3415gprel16_reloc_p (bfd_reloc_code_real_type reloc)
3416{
3417 return (reloc == BFD_RELOC_GPREL16 || reloc == BFD_RELOC_MIPS16_GPREL
3418 || reloc == BFD_RELOC_MICROMIPS_GPREL16);
3419}
3420
2de39019
CM
3421/* Return true if RELOC is a PC-relative relocation that does not have
3422 full address range. */
3423
3424static inline bfd_boolean
3425limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
3426{
3427 switch (reloc)
3428 {
3429 case BFD_RELOC_16_PCREL_S2:
3430 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
3431 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
3432 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
3433 return TRUE;
3434
b47468a6
CM
3435 case BFD_RELOC_32_PCREL:
3436 return HAVE_64BIT_ADDRESSES;
3437
2de39019
CM
3438 default:
3439 return FALSE;
3440 }
3441}
b47468a6 3442
5919d012 3443/* Return true if the given relocation might need a matching %lo().
0a44bf69
RS
3444 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
3445 need a matching %lo() when applied to local symbols. */
5919d012
RS
3446
3447static inline bfd_boolean
17a2f251 3448reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 3449{
3b91255e 3450 return (HAVE_IN_PLACE_ADDENDS
738e5348 3451 && (hi16_reloc_p (reloc)
0a44bf69
RS
3452 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
3453 all GOT16 relocations evaluate to "G". */
738e5348
RS
3454 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
3455}
3456
3457/* Return the type of %lo() reloc needed by RELOC, given that
3458 reloc_needs_lo_p. */
3459
3460static inline bfd_reloc_code_real_type
3461matching_lo_reloc (bfd_reloc_code_real_type reloc)
3462{
df58fc94
RS
3463 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
3464 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
3465 : BFD_RELOC_LO16));
5919d012
RS
3466}
3467
3468/* Return true if the given fixup is followed by a matching R_MIPS_LO16
3469 relocation. */
3470
3471static inline bfd_boolean
17a2f251 3472fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
3473{
3474 return (fixp->fx_next != NULL
738e5348 3475 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
5919d012
RS
3476 && fixp->fx_addsy == fixp->fx_next->fx_addsy
3477 && fixp->fx_offset == fixp->fx_next->fx_offset);
3478}
3479
462427c4
RS
3480/* Move all labels in LABELS to the current insertion point. TEXT_P
3481 says whether the labels refer to text or data. */
404a8071
RS
3482
3483static void
462427c4 3484mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
404a8071
RS
3485{
3486 struct insn_label_list *l;
3487 valueT val;
3488
462427c4 3489 for (l = labels; l != NULL; l = l->next)
404a8071 3490 {
9c2799c2 3491 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
404a8071
RS
3492 symbol_set_frag (l->label, frag_now);
3493 val = (valueT) frag_now_fix ();
df58fc94 3494 /* MIPS16/microMIPS text labels are stored as odd. */
462427c4 3495 if (text_p && HAVE_CODE_COMPRESSION)
404a8071
RS
3496 ++val;
3497 S_SET_VALUE (l->label, val);
3498 }
3499}
3500
462427c4
RS
3501/* Move all labels in insn_labels to the current insertion point
3502 and treat them as text labels. */
3503
3504static void
3505mips_move_text_labels (void)
3506{
3507 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
3508}
3509
5f0fe04b
TS
3510static bfd_boolean
3511s_is_linkonce (symbolS *sym, segT from_seg)
3512{
3513 bfd_boolean linkonce = FALSE;
3514 segT symseg = S_GET_SEGMENT (sym);
3515
3516 if (symseg != from_seg && !S_IS_LOCAL (sym))
3517 {
3518 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
3519 linkonce = TRUE;
5f0fe04b
TS
3520 /* The GNU toolchain uses an extension for ELF: a section
3521 beginning with the magic string .gnu.linkonce is a
3522 linkonce section. */
3523 if (strncmp (segment_name (symseg), ".gnu.linkonce",
3524 sizeof ".gnu.linkonce" - 1) == 0)
3525 linkonce = TRUE;
5f0fe04b
TS
3526 }
3527 return linkonce;
3528}
3529
e1b47bd5 3530/* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
df58fc94
RS
3531 linker to handle them specially, such as generating jalx instructions
3532 when needed. We also make them odd for the duration of the assembly,
3533 in order to generate the right sort of code. We will make them even
252b5132
RH
3534 in the adjust_symtab routine, while leaving them marked. This is
3535 convenient for the debugger and the disassembler. The linker knows
3536 to make them odd again. */
3537
3538static void
e1b47bd5 3539mips_compressed_mark_label (symbolS *label)
252b5132 3540{
df58fc94 3541 gas_assert (HAVE_CODE_COMPRESSION);
a8dbcb85 3542
f3ded42a
RS
3543 if (mips_opts.mips16)
3544 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
3545 else
3546 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
e1b47bd5
RS
3547 if ((S_GET_VALUE (label) & 1) == 0
3548 /* Don't adjust the address if the label is global or weak, or
3549 in a link-once section, since we'll be emitting symbol reloc
3550 references to it which will be patched up by the linker, and
3551 the final value of the symbol may or may not be MIPS16/microMIPS. */
3552 && !S_IS_WEAK (label)
3553 && !S_IS_EXTERNAL (label)
3554 && !s_is_linkonce (label, now_seg))
3555 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
3556}
3557
3558/* Mark preceding MIPS16 or microMIPS instruction labels. */
3559
3560static void
3561mips_compressed_mark_labels (void)
3562{
3563 struct insn_label_list *l;
3564
3565 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
3566 mips_compressed_mark_label (l->label);
252b5132
RH
3567}
3568
4d7206a2
RS
3569/* End the current frag. Make it a variant frag and record the
3570 relaxation info. */
3571
3572static void
3573relax_close_frag (void)
3574{
584892a6 3575 mips_macro_warning.first_frag = frag_now;
4d7206a2 3576 frag_var (rs_machine_dependent, 0, 0,
584892a6 3577 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
4d7206a2
RS
3578 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
3579
3580 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
3581 mips_relax.first_fixup = 0;
3582}
3583
3584/* Start a new relaxation sequence whose expansion depends on SYMBOL.
3585 See the comment above RELAX_ENCODE for more details. */
3586
3587static void
3588relax_start (symbolS *symbol)
3589{
9c2799c2 3590 gas_assert (mips_relax.sequence == 0);
4d7206a2
RS
3591 mips_relax.sequence = 1;
3592 mips_relax.symbol = symbol;
3593}
3594
3595/* Start generating the second version of a relaxable sequence.
3596 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
3597
3598static void
4d7206a2
RS
3599relax_switch (void)
3600{
9c2799c2 3601 gas_assert (mips_relax.sequence == 1);
4d7206a2
RS
3602 mips_relax.sequence = 2;
3603}
3604
3605/* End the current relaxable sequence. */
3606
3607static void
3608relax_end (void)
3609{
9c2799c2 3610 gas_assert (mips_relax.sequence == 2);
4d7206a2
RS
3611 relax_close_frag ();
3612 mips_relax.sequence = 0;
3613}
3614
11625dd8
RS
3615/* Return true if IP is a delayed branch or jump. */
3616
3617static inline bfd_boolean
3618delayed_branch_p (const struct mips_cl_insn *ip)
3619{
3620 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
3621 | INSN_COND_BRANCH_DELAY
3622 | INSN_COND_BRANCH_LIKELY)) != 0;
3623}
3624
3625/* Return true if IP is a compact branch or jump. */
3626
3627static inline bfd_boolean
3628compact_branch_p (const struct mips_cl_insn *ip)
3629{
26545944
RS
3630 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
3631 | INSN2_COND_BRANCH)) != 0;
11625dd8
RS
3632}
3633
3634/* Return true if IP is an unconditional branch or jump. */
3635
3636static inline bfd_boolean
3637uncond_branch_p (const struct mips_cl_insn *ip)
3638{
3639 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
26545944 3640 || (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0);
11625dd8
RS
3641}
3642
3643/* Return true if IP is a branch-likely instruction. */
3644
3645static inline bfd_boolean
3646branch_likely_p (const struct mips_cl_insn *ip)
3647{
3648 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
3649}
3650
14fe068b
RS
3651/* Return the type of nop that should be used to fill the delay slot
3652 of delayed branch IP. */
3653
3654static struct mips_cl_insn *
3655get_delay_slot_nop (const struct mips_cl_insn *ip)
3656{
3657 if (mips_opts.micromips
3658 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
3659 return &micromips_nop32_insn;
3660 return NOP_INSN;
3661}
3662
fc76e730
RS
3663/* Return a mask that has bit N set if OPCODE reads the register(s)
3664 in operand N. */
df58fc94
RS
3665
3666static unsigned int
fc76e730 3667insn_read_mask (const struct mips_opcode *opcode)
df58fc94 3668{
fc76e730
RS
3669 return (opcode->pinfo & INSN_READ_ALL) >> INSN_READ_SHIFT;
3670}
df58fc94 3671
fc76e730
RS
3672/* Return a mask that has bit N set if OPCODE writes to the register(s)
3673 in operand N. */
3674
3675static unsigned int
3676insn_write_mask (const struct mips_opcode *opcode)
3677{
3678 return (opcode->pinfo & INSN_WRITE_ALL) >> INSN_WRITE_SHIFT;
3679}
3680
3681/* Return a mask of the registers specified by operand OPERAND of INSN.
3682 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
3683 is set. */
3684
3685static unsigned int
3686operand_reg_mask (const struct mips_cl_insn *insn,
3687 const struct mips_operand *operand,
3688 unsigned int type_mask)
3689{
3690 unsigned int uval, vsel;
3691
3692 switch (operand->type)
df58fc94 3693 {
fc76e730
RS
3694 case OP_INT:
3695 case OP_MAPPED_INT:
3696 case OP_MSB:
3697 case OP_PCREL:
3698 case OP_PERF_REG:
3699 case OP_ADDIUSP_INT:
3700 case OP_ENTRY_EXIT_LIST:
3701 case OP_REPEAT_DEST_REG:
3702 case OP_REPEAT_PREV_REG:
3703 case OP_PC:
3704 abort ();
3705
3706 case OP_REG:
3707 {
3708 const struct mips_reg_operand *reg_op;
3709
3710 reg_op = (const struct mips_reg_operand *) operand;
3711 if (!(type_mask & (1 << reg_op->reg_type)))
3712 return 0;
3713 uval = insn_extract_operand (insn, operand);
3714 return 1 << mips_decode_reg_operand (reg_op, uval);
3715 }
3716
3717 case OP_REG_PAIR:
3718 {
3719 const struct mips_reg_pair_operand *pair_op;
3720
3721 pair_op = (const struct mips_reg_pair_operand *) operand;
3722 if (!(type_mask & (1 << pair_op->reg_type)))
3723 return 0;
3724 uval = insn_extract_operand (insn, operand);
3725 return (1 << pair_op->reg1_map[uval]) | (1 << pair_op->reg2_map[uval]);
3726 }
3727
3728 case OP_CLO_CLZ_DEST:
3729 if (!(type_mask & (1 << OP_REG_GP)))
3730 return 0;
3731 uval = insn_extract_operand (insn, operand);
3732 return (1 << (uval & 31)) | (1 << (uval >> 5));
3733
3734 case OP_LWM_SWM_LIST:
3735 abort ();
3736
3737 case OP_SAVE_RESTORE_LIST:
3738 abort ();
3739
3740 case OP_MDMX_IMM_REG:
3741 if (!(type_mask & (1 << OP_REG_VEC)))
3742 return 0;
3743 uval = insn_extract_operand (insn, operand);
3744 vsel = uval >> 5;
3745 if ((vsel & 0x18) == 0x18)
3746 return 0;
3747 return 1 << (uval & 31);
df58fc94 3748 }
fc76e730
RS
3749 abort ();
3750}
3751
3752/* Return a mask of the registers specified by operands OPNO_MASK of INSN,
3753 where bit N of OPNO_MASK is set if operand N should be included.
3754 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
3755 is set. */
3756
3757static unsigned int
3758insn_reg_mask (const struct mips_cl_insn *insn,
3759 unsigned int type_mask, unsigned int opno_mask)
3760{
3761 unsigned int opno, reg_mask;
3762
3763 opno = 0;
3764 reg_mask = 0;
3765 while (opno_mask != 0)
3766 {
3767 if (opno_mask & 1)
3768 reg_mask |= operand_reg_mask (insn, insn_opno (insn, opno), type_mask);
3769 opno_mask >>= 1;
3770 opno += 1;
3771 }
3772 return reg_mask;
df58fc94
RS
3773}
3774
4c260379
RS
3775/* Return the mask of core registers that IP reads. */
3776
3777static unsigned int
3778gpr_read_mask (const struct mips_cl_insn *ip)
3779{
3780 unsigned long pinfo, pinfo2;
3781 unsigned int mask;
3782
fc76e730 3783 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_read_mask (ip->insn_mo));
4c260379
RS
3784 pinfo = ip->insn_mo->pinfo;
3785 pinfo2 = ip->insn_mo->pinfo2;
fc76e730 3786 if (pinfo & INSN_UDI)
4c260379 3787 {
fc76e730
RS
3788 /* UDI instructions have traditionally been assumed to read RS
3789 and RT. */
3790 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
3791 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4c260379 3792 }
fc76e730
RS
3793 if (pinfo & INSN_READ_GPR_24)
3794 mask |= 1 << 24;
3795 if (pinfo2 & INSN2_READ_GPR_16)
3796 mask |= 1 << 16;
3797 if (pinfo2 & INSN2_READ_SP)
3798 mask |= 1 << SP;
26545944 3799 if (pinfo2 & INSN2_READ_GPR_31)
fc76e730 3800 mask |= 1 << 31;
fe35f09f
RS
3801 /* Don't include register 0. */
3802 return mask & ~1;
4c260379
RS
3803}
3804
3805/* Return the mask of core registers that IP writes. */
3806
3807static unsigned int
3808gpr_write_mask (const struct mips_cl_insn *ip)
3809{
3810 unsigned long pinfo, pinfo2;
3811 unsigned int mask;
3812
fc76e730 3813 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_write_mask (ip->insn_mo));
4c260379
RS
3814 pinfo = ip->insn_mo->pinfo;
3815 pinfo2 = ip->insn_mo->pinfo2;
fc76e730
RS
3816 if (pinfo & INSN_WRITE_GPR_24)
3817 mask |= 1 << 24;
3818 if (pinfo & INSN_WRITE_GPR_31)
3819 mask |= 1 << 31;
3820 if (pinfo & INSN_UDI)
3821 /* UDI instructions have traditionally been assumed to write to RD. */
3822 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
3823 if (pinfo2 & INSN2_WRITE_SP)
3824 mask |= 1 << SP;
fe35f09f
RS
3825 /* Don't include register 0. */
3826 return mask & ~1;
4c260379
RS
3827}
3828
3829/* Return the mask of floating-point registers that IP reads. */
3830
3831static unsigned int
3832fpr_read_mask (const struct mips_cl_insn *ip)
3833{
fc76e730 3834 unsigned long pinfo;
4c260379
RS
3835 unsigned int mask;
3836
fc76e730
RS
3837 mask = insn_reg_mask (ip, (1 << OP_REG_FP) | (1 << OP_REG_VEC),
3838 insn_read_mask (ip->insn_mo));
4c260379 3839 pinfo = ip->insn_mo->pinfo;
4c260379
RS
3840 /* Conservatively treat all operands to an FP_D instruction are doubles.
3841 (This is overly pessimistic for things like cvt.d.s.) */
3842 if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3843 mask |= mask << 1;
3844 return mask;
3845}
3846
3847/* Return the mask of floating-point registers that IP writes. */
3848
3849static unsigned int
3850fpr_write_mask (const struct mips_cl_insn *ip)
3851{
fc76e730 3852 unsigned long pinfo;
4c260379
RS
3853 unsigned int mask;
3854
fc76e730
RS
3855 mask = insn_reg_mask (ip, (1 << OP_REG_FP) | (1 << OP_REG_VEC),
3856 insn_write_mask (ip->insn_mo));
4c260379 3857 pinfo = ip->insn_mo->pinfo;
4c260379
RS
3858 /* Conservatively treat all operands to an FP_D instruction are doubles.
3859 (This is overly pessimistic for things like cvt.s.d.) */
3860 if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3861 mask |= mask << 1;
3862 return mask;
3863}
3864
a1d78564
RS
3865/* Operand OPNUM of INSN is an odd-numbered floating-point register.
3866 Check whether that is allowed. */
3867
3868static bfd_boolean
3869mips_oddfpreg_ok (const struct mips_opcode *insn, int opnum)
3870{
3871 const char *s = insn->name;
3872
3873 if (insn->pinfo == INSN_MACRO)
3874 /* Let a macro pass, we'll catch it later when it is expanded. */
3875 return TRUE;
3876
3877 if (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa) || mips_opts.arch == CPU_R5900)
3878 {
3879 /* Allow odd registers for single-precision ops. */
3880 switch (insn->pinfo & (FP_S | FP_D))
3881 {
3882 case FP_S:
3883 case 0:
3884 return TRUE;
3885 case FP_D:
3886 return FALSE;
3887 default:
3888 break;
3889 }
3890
3891 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
3892 s = strchr (insn->name, '.');
3893 if (s != NULL && opnum == 2)
3894 s = strchr (s + 1, '.');
3895 return (s != NULL && (s[1] == 'w' || s[1] == 's'));
3896 }
3897
3898 /* Single-precision coprocessor loads and moves are OK too. */
3899 if ((insn->pinfo & FP_S)
3900 && (insn->pinfo & (INSN_COPROC_MEMORY_DELAY | INSN_STORE_MEMORY
3901 | INSN_LOAD_COPROC_DELAY | INSN_COPROC_MOVE_DELAY)))
3902 return TRUE;
3903
3904 return FALSE;
3905}
3906
a1d78564
RS
3907/* Report that user-supplied argument ARGNUM for INSN was VAL, but should
3908 have been in the range [MIN_VAL, MAX_VAL]. PRINT_HEX says whether
3909 this operand is normally printed in hex or decimal. */
3910
3911static void
3912report_bad_range (struct mips_cl_insn *insn, int argnum,
3913 offsetT val, int min_val, int max_val,
3914 bfd_boolean print_hex)
3915{
3916 if (print_hex && val >= 0)
3917 as_bad (_("Operand %d of `%s' must be in the range [0x%x, 0x%x],"
3918 " was 0x%lx."),
3919 argnum, insn->insn_mo->name, min_val, max_val, (unsigned long) val);
3920 else if (print_hex)
3921 as_bad (_("Operand %d of `%s' must be in the range [0x%x, 0x%x],"
3922 " was %ld."),
3923 argnum, insn->insn_mo->name, min_val, max_val, (unsigned long) val);
3924 else
3925 as_bad (_("Operand %d of `%s' must be in the range [%d, %d],"
3926 " was %ld."),
3927 argnum, insn->insn_mo->name, min_val, max_val, (unsigned long) val);
3928}
3929
3930/* Report an invalid combination of position and size operands for a bitfield
3931 operation. POS and SIZE are the values that were given. */
3932
3933static void
3934report_bad_field (offsetT pos, offsetT size)
3935{
3936 as_bad (_("Invalid field specification (position %ld, size %ld)"),
3937 (unsigned long) pos, (unsigned long) size);
3938}
3939
3940/* Information about an instruction argument that we're trying to match. */
3941struct mips_arg_info
3942{
3943 /* The instruction so far. */
3944 struct mips_cl_insn *insn;
3945
a92713e6
RS
3946 /* The first unconsumed operand token. */
3947 struct mips_operand_token *token;
3948
a1d78564
RS
3949 /* The 1-based operand number, in terms of insn->insn_mo->args. */
3950 int opnum;
3951
3952 /* The 1-based argument number, for error reporting. This does not
3953 count elided optional registers, etc.. */
3954 int argnum;
3955
3956 /* The last OP_REG operand seen, or ILLEGAL_REG if none. */
3957 unsigned int last_regno;
3958
3959 /* If the first operand was an OP_REG, this is the register that it
3960 specified, otherwise it is ILLEGAL_REG. */
3961 unsigned int dest_regno;
3962
3963 /* The value of the last OP_INT operand. Only used for OP_MSB,
3964 where it gives the lsb position. */
3965 unsigned int last_op_int;
3966
3967 /* If true, match routines should silently reject invalid arguments.
3968 If false, match routines can accept invalid arguments as long as
3969 they report an appropriate error. They still have the option of
3970 silently rejecting arguments, in which case a generic "Invalid operands"
3971 style of error will be used instead. */
3972 bfd_boolean soft_match;
3973
3974 /* If true, the OP_INT match routine should treat plain symbolic operands
3975 as if a relocation operator like %lo(...) had been used. This is only
3976 ever true if the operand can be relocated. */
3977 bfd_boolean allow_nonconst;
3978
3979 /* When true, the OP_INT match routine should allow unsigned N-bit
3980 arguments to be used where a signed N-bit operand is expected. */
3981 bfd_boolean lax_max;
3982
a1d78564
RS
3983 /* True if a reference to the current AT register was seen. */
3984 bfd_boolean seen_at;
3985};
3986
a92713e6
RS
3987/* Try to match an OT_CHAR token for character CH. Consume the token
3988 and return true on success, otherwise return false. */
a1d78564 3989
a92713e6
RS
3990static bfd_boolean
3991match_char (struct mips_arg_info *arg, char ch)
a1d78564 3992{
a92713e6
RS
3993 if (arg->token->type == OT_CHAR && arg->token->u.ch == ch)
3994 {
3995 ++arg->token;
3996 if (ch == ',')
3997 arg->argnum += 1;
3998 return TRUE;
3999 }
4000 return FALSE;
4001}
a1d78564 4002
a92713e6
RS
4003/* Try to get an expression from the next tokens in ARG. Consume the
4004 tokens and return true on success, storing the expression value in
4005 VALUE and relocation types in R. */
4006
4007static bfd_boolean
4008match_expression (struct mips_arg_info *arg, expressionS *value,
4009 bfd_reloc_code_real_type *r)
4010{
4011 if (arg->token->type == OT_INTEGER)
a1d78564 4012 {
a92713e6
RS
4013 *value = arg->token->u.integer.value;
4014 memcpy (r, arg->token->u.integer.relocs, 3 * sizeof (*r));
4015 ++arg->token;
4016 return TRUE;
4017 }
4018
4019 /* Error-reporting is more consistent if we treat registers as O_register
4020 rather than rejecting them outright. "$1", "($1)" and "(($1))" are
4021 then handled in the same way. */
4022 if (arg->token->type == OT_REG)
4023 {
4024 value->X_add_number = arg->token->u.regno;
4025 ++arg->token;
a1d78564 4026 }
a92713e6
RS
4027 else if (arg->token[0].type == OT_CHAR
4028 && arg->token[0].u.ch == '('
4029 && arg->token[1].type == OT_REG
4030 && arg->token[2].type == OT_CHAR
4031 && arg->token[2].u.ch == ')')
4032 {
4033 value->X_add_number = arg->token[1].u.regno;
4034 arg->token += 3;
4035 }
4036 else
4037 return FALSE;
4038
4039 value->X_op = O_register;
4040 r[0] = r[1] = r[2] = BFD_RELOC_UNUSED;
4041 return TRUE;
4042}
4043
4044/* Try to get a constant expression from the next tokens in ARG. Consume
4045 the tokens and return return true on success, storing the constant value
4046 in *VALUE. Use FALLBACK as the value if the match succeeded with an
4047 error. */
4048
4049static bfd_boolean
4050match_const_int (struct mips_arg_info *arg, offsetT *value, offsetT fallback)
4051{
4052 expressionS ex;
4053 bfd_reloc_code_real_type r[3];
a1d78564 4054
a92713e6
RS
4055 if (!match_expression (arg, &ex, r))
4056 return FALSE;
4057
4058 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_constant)
a1d78564
RS
4059 *value = ex.X_add_number;
4060 else
4061 {
a92713e6
RS
4062 if (arg->soft_match)
4063 return FALSE;
a1d78564
RS
4064 as_bad (_("Operand %d of `%s' must be constant"),
4065 arg->argnum, arg->insn->insn_mo->name);
4066 *value = fallback;
4067 }
a92713e6 4068 return TRUE;
a1d78564
RS
4069}
4070
4071/* Return the RTYPE_* flags for a register operand of type TYPE that
4072 appears in instruction OPCODE. */
4073
4074static unsigned int
4075convert_reg_type (const struct mips_opcode *opcode,
4076 enum mips_reg_operand_type type)
4077{
4078 switch (type)
4079 {
4080 case OP_REG_GP:
4081 return RTYPE_NUM | RTYPE_GP;
4082
4083 case OP_REG_FP:
4084 /* Allow vector register names for MDMX if the instruction is a 64-bit
4085 FPR load, store or move (including moves to and from GPRs). */
4086 if ((mips_opts.ase & ASE_MDMX)
4087 && (opcode->pinfo & FP_D)
4088 && (opcode->pinfo & (INSN_COPROC_MOVE_DELAY
4089 | INSN_COPROC_MEMORY_DELAY
4090 | INSN_LOAD_COPROC_DELAY
4091 | INSN_LOAD_MEMORY_DELAY
4092 | INSN_STORE_MEMORY)))
4093 return RTYPE_FPU | RTYPE_VEC;
4094 return RTYPE_FPU;
4095
4096 case OP_REG_CCC:
4097 if (opcode->pinfo & (FP_D | FP_S))
4098 return RTYPE_CCC | RTYPE_FCC;
4099 return RTYPE_CCC;
4100
4101 case OP_REG_VEC:
4102 if (opcode->membership & INSN_5400)
4103 return RTYPE_FPU;
4104 return RTYPE_FPU | RTYPE_VEC;
4105
4106 case OP_REG_ACC:
4107 return RTYPE_ACC;
4108
4109 case OP_REG_COPRO:
4110 if (opcode->name[strlen (opcode->name) - 1] == '0')
4111 return RTYPE_NUM | RTYPE_CP0;
4112 return RTYPE_NUM;
4113
4114 case OP_REG_HW:
4115 return RTYPE_NUM;
4116 }
4117 abort ();
4118}
4119
4120/* ARG is register REGNO, of type TYPE. Warn about any dubious registers. */
4121
4122static void
4123check_regno (struct mips_arg_info *arg,
4124 enum mips_reg_operand_type type, unsigned int regno)
4125{
4126 if (AT && type == OP_REG_GP && regno == AT)
4127 arg->seen_at = TRUE;
4128
4129 if (type == OP_REG_FP
4130 && (regno & 1) != 0
4131 && HAVE_32BIT_FPRS
4132 && !mips_oddfpreg_ok (arg->insn->insn_mo, arg->opnum))
4133 as_warn (_("Float register should be even, was %d"), regno);
4134
4135 if (type == OP_REG_CCC)
4136 {
4137 const char *name;
4138 size_t length;
4139
4140 name = arg->insn->insn_mo->name;
4141 length = strlen (name);
4142 if ((regno & 1) != 0
4143 && ((length >= 3 && strcmp (name + length - 3, ".ps") == 0)
4144 || (length >= 5 && strncmp (name + length - 5, "any2", 4) == 0)))
4145 as_warn (_("Condition code register should be even for %s, was %d"),
4146 name, regno);
4147
4148 if ((regno & 3) != 0
4149 && (length >= 5 && strncmp (name + length - 5, "any4", 4) == 0))
4150 as_warn (_("Condition code register should be 0 or 4 for %s, was %d"),
4151 name, regno);
4152 }
4153}
4154
a92713e6
RS
4155/* ARG is a register with symbol value SYMVAL. Try to interpret it as
4156 a register of type TYPE. Return true on success, storing the register
4157 number in *REGNO and warning about any dubious uses. */
4158
4159static bfd_boolean
4160match_regno (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4161 unsigned int symval, unsigned int *regno)
4162{
4163 if (type == OP_REG_VEC)
4164 symval = mips_prefer_vec_regno (symval);
4165 if (!(symval & convert_reg_type (arg->insn->insn_mo, type)))
4166 return FALSE;
4167
4168 *regno = symval & RNUM_MASK;
4169 check_regno (arg, type, *regno);
4170 return TRUE;
4171}
4172
4173/* Try to interpret the next token in ARG as a register of type TYPE.
4174 Consume the token and return true on success, storing the register
4175 number in *REGNO. Return false on failure. */
4176
4177static bfd_boolean
4178match_reg (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4179 unsigned int *regno)
4180{
4181 if (arg->token->type == OT_REG
4182 && match_regno (arg, type, arg->token->u.regno, regno))
4183 {
4184 ++arg->token;
4185 return TRUE;
4186 }
4187 return FALSE;
4188}
4189
4190/* Try to interpret the next token in ARG as a range of registers of type TYPE.
4191 Consume the token and return true on success, storing the register numbers
4192 in *REGNO1 and *REGNO2. Return false on failure. */
4193
4194static bfd_boolean
4195match_reg_range (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4196 unsigned int *regno1, unsigned int *regno2)
4197{
4198 if (match_reg (arg, type, regno1))
4199 {
4200 *regno2 = *regno1;
4201 return TRUE;
4202 }
4203 if (arg->token->type == OT_REG_RANGE
4204 && match_regno (arg, type, arg->token->u.reg_range.regno1, regno1)
4205 && match_regno (arg, type, arg->token->u.reg_range.regno2, regno2)
4206 && *regno1 <= *regno2)
4207 {
4208 ++arg->token;
4209 return TRUE;
4210 }
4211 return FALSE;
4212}
4213
a1d78564
RS
4214/* OP_INT matcher. */
4215
a92713e6 4216static bfd_boolean
a1d78564 4217match_int_operand (struct mips_arg_info *arg,
a92713e6 4218 const struct mips_operand *operand_base)
a1d78564
RS
4219{
4220 const struct mips_int_operand *operand;
3ccad066 4221 unsigned int uval;
a1d78564
RS
4222 int min_val, max_val, factor;
4223 offsetT sval;
4224 bfd_boolean print_hex;
4225
4226 operand = (const struct mips_int_operand *) operand_base;
4227 factor = 1 << operand->shift;
3ccad066
RS
4228 min_val = mips_int_operand_min (operand);
4229 max_val = mips_int_operand_max (operand);
a1d78564 4230 if (arg->lax_max)
3ccad066 4231 max_val = ((1 << operand_base->size) - 1) << operand->shift;
a1d78564 4232
a92713e6
RS
4233 if (arg->token->type == OT_CHAR && arg->token->u.ch == '(')
4234 /* Assume we have an elided offset. The later match will fail
4235 if this turns out to be wrong. */
4236 sval = 0;
4237 else if (operand_base->lsb == 0
4238 && operand_base->size == 16
4239 && operand->shift == 0
4240 && operand->bias == 0
4241 && (operand->max_val == 32767 || operand->max_val == 65535))
a1d78564
RS
4242 {
4243 /* The operand can be relocated. */
a92713e6
RS
4244 if (!match_expression (arg, &offset_expr, offset_reloc))
4245 return FALSE;
4246
4247 if (offset_reloc[0] != BFD_RELOC_UNUSED)
a1d78564
RS
4248 /* Relocation operators were used. Accept the arguent and
4249 leave the relocation value in offset_expr and offset_relocs
4250 for the caller to process. */
a92713e6
RS
4251 return TRUE;
4252
4253 if (offset_expr.X_op != O_constant)
a1d78564 4254 {
a92713e6
RS
4255 /* If non-constant operands are allowed then leave them for
4256 the caller to process, otherwise fail the match. */
4257 if (!arg->allow_nonconst)
4258 return FALSE;
4259 offset_reloc[0] = BFD_RELOC_LO16;
4260 return TRUE;
a1d78564 4261 }
a92713e6 4262
a1d78564
RS
4263 /* Clear the global state; we're going to install the operand
4264 ourselves. */
a92713e6 4265 sval = offset_expr.X_add_number;
a1d78564
RS
4266 offset_expr.X_op = O_absent;
4267 }
4268 else
4269 {
a92713e6
RS
4270 if (!match_const_int (arg, &sval, min_val))
4271 return FALSE;
a1d78564
RS
4272 }
4273
4274 arg->last_op_int = sval;
4275
4276 /* Check the range. If there's a problem, record the lowest acceptable
4277 value in arg->last_op_int in order to prevent an unhelpful error
4278 from OP_MSB too.
4279
4280 Bit counts have traditionally been printed in hex by the disassembler
4281 but printed as decimal in error messages. Only resort to hex if
4282 the operand is bigger than 6 bits. */
4283 print_hex = operand->print_hex && operand_base->size > 6;
4284 if (sval < min_val || sval > max_val)
4285 {
4286 if (arg->soft_match)
a92713e6 4287 return FALSE;
a1d78564
RS
4288 report_bad_range (arg->insn, arg->argnum, sval, min_val, max_val,
4289 print_hex);
4290 arg->last_op_int = min_val;
4291 }
4292 else if (sval % factor)
4293 {
4294 if (arg->soft_match)
a92713e6 4295 return FALSE;
a1d78564
RS
4296 as_bad (print_hex && sval >= 0
4297 ? _("Operand %d of `%s' must be a factor of %d, was 0x%lx.")
4298 : _("Operand %d of `%s' must be a factor of %d, was %ld."),
4299 arg->argnum, arg->insn->insn_mo->name, factor,
4300 (unsigned long) sval);
4301 arg->last_op_int = min_val;
4302 }
4303
4304 uval = (unsigned int) sval >> operand->shift;
4305 uval -= operand->bias;
4306
4307 /* Handle -mfix-cn63xxp1. */
4308 if (arg->opnum == 1
4309 && mips_fix_cn63xxp1
4310 && !mips_opts.micromips
4311 && strcmp ("pref", arg->insn->insn_mo->name) == 0)
4312 switch (uval)
4313 {
4314 case 5:
4315 case 25:
4316 case 26:
4317 case 27:
4318 case 28:
4319 case 29:
4320 case 30:
4321 case 31:
4322 /* These are ok. */
4323 break;
4324
4325 default:
4326 /* The rest must be changed to 28. */
4327 uval = 28;
4328 break;
4329 }
4330
4331 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 4332 return TRUE;
a1d78564
RS
4333}
4334
4335/* OP_MAPPED_INT matcher. */
4336
a92713e6 4337static bfd_boolean
a1d78564 4338match_mapped_int_operand (struct mips_arg_info *arg,
a92713e6 4339 const struct mips_operand *operand_base)
a1d78564
RS
4340{
4341 const struct mips_mapped_int_operand *operand;
4342 unsigned int uval, num_vals;
4343 offsetT sval;
4344
4345 operand = (const struct mips_mapped_int_operand *) operand_base;
a92713e6
RS
4346 if (!match_const_int (arg, &sval, operand->int_map[0]))
4347 return FALSE;
a1d78564
RS
4348
4349 num_vals = 1 << operand_base->size;
4350 for (uval = 0; uval < num_vals; uval++)
4351 if (operand->int_map[uval] == sval)
4352 break;
4353 if (uval == num_vals)
a92713e6 4354 return FALSE;
a1d78564
RS
4355
4356 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 4357 return TRUE;
a1d78564
RS
4358}
4359
4360/* OP_MSB matcher. */
4361
a92713e6 4362static bfd_boolean
a1d78564 4363match_msb_operand (struct mips_arg_info *arg,
a92713e6 4364 const struct mips_operand *operand_base)
a1d78564
RS
4365{
4366 const struct mips_msb_operand *operand;
4367 int min_val, max_val, max_high;
4368 offsetT size, sval, high;
4369
4370 operand = (const struct mips_msb_operand *) operand_base;
4371 min_val = operand->bias;
4372 max_val = min_val + (1 << operand_base->size) - 1;
4373 max_high = operand->opsize;
4374
a92713e6
RS
4375 if (!match_const_int (arg, &size, 1))
4376 return FALSE;
a1d78564
RS
4377
4378 high = size + arg->last_op_int;
4379 sval = operand->add_lsb ? high : size;
4380
4381 if (size < 0 || high > max_high || sval < min_val || sval > max_val)
4382 {
4383 if (arg->soft_match)
a92713e6 4384 return FALSE;
a1d78564
RS
4385 report_bad_field (arg->last_op_int, size);
4386 sval = min_val;
4387 }
4388 insn_insert_operand (arg->insn, operand_base, sval - min_val);
a92713e6 4389 return TRUE;
a1d78564
RS
4390}
4391
4392/* OP_REG matcher. */
4393
a92713e6 4394static bfd_boolean
a1d78564 4395match_reg_operand (struct mips_arg_info *arg,
a92713e6 4396 const struct mips_operand *operand_base)
a1d78564
RS
4397{
4398 const struct mips_reg_operand *operand;
a92713e6 4399 unsigned int regno, uval, num_vals;
a1d78564
RS
4400
4401 operand = (const struct mips_reg_operand *) operand_base;
a92713e6
RS
4402 if (!match_reg (arg, operand->reg_type, &regno))
4403 return FALSE;
a1d78564
RS
4404
4405 if (operand->reg_map)
4406 {
4407 num_vals = 1 << operand->root.size;
4408 for (uval = 0; uval < num_vals; uval++)
4409 if (operand->reg_map[uval] == regno)
4410 break;
4411 if (num_vals == uval)
a92713e6 4412 return FALSE;
a1d78564
RS
4413 }
4414 else
4415 uval = regno;
4416
a1d78564
RS
4417 arg->last_regno = regno;
4418 if (arg->opnum == 1)
4419 arg->dest_regno = regno;
4420 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 4421 return TRUE;
a1d78564
RS
4422}
4423
4424/* OP_REG_PAIR matcher. */
4425
a92713e6 4426static bfd_boolean
a1d78564 4427match_reg_pair_operand (struct mips_arg_info *arg,
a92713e6 4428 const struct mips_operand *operand_base)
a1d78564
RS
4429{
4430 const struct mips_reg_pair_operand *operand;
a92713e6 4431 unsigned int regno1, regno2, uval, num_vals;
a1d78564
RS
4432
4433 operand = (const struct mips_reg_pair_operand *) operand_base;
a92713e6
RS
4434 if (!match_reg (arg, operand->reg_type, &regno1)
4435 || !match_char (arg, ',')
4436 || !match_reg (arg, operand->reg_type, &regno2))
4437 return FALSE;
a1d78564
RS
4438
4439 num_vals = 1 << operand_base->size;
4440 for (uval = 0; uval < num_vals; uval++)
4441 if (operand->reg1_map[uval] == regno1 && operand->reg2_map[uval] == regno2)
4442 break;
4443 if (uval == num_vals)
a92713e6 4444 return FALSE;
a1d78564 4445
a1d78564 4446 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 4447 return TRUE;
a1d78564
RS
4448}
4449
4450/* OP_PCREL matcher. The caller chooses the relocation type. */
4451
a92713e6
RS
4452static bfd_boolean
4453match_pcrel_operand (struct mips_arg_info *arg)
a1d78564 4454{
a92713e6
RS
4455 bfd_reloc_code_real_type r[3];
4456
4457 return match_expression (arg, &offset_expr, r) && r[0] == BFD_RELOC_UNUSED;
a1d78564
RS
4458}
4459
4460/* OP_PERF_REG matcher. */
4461
a92713e6 4462static bfd_boolean
a1d78564 4463match_perf_reg_operand (struct mips_arg_info *arg,
a92713e6 4464 const struct mips_operand *operand)
a1d78564
RS
4465{
4466 offsetT sval;
4467
a92713e6
RS
4468 if (!match_const_int (arg, &sval, 0))
4469 return FALSE;
a1d78564
RS
4470
4471 if (sval != 0
4472 && (sval != 1
4473 || (mips_opts.arch == CPU_R5900
4474 && (strcmp (arg->insn->insn_mo->name, "mfps") == 0
4475 || strcmp (arg->insn->insn_mo->name, "mtps") == 0))))
4476 {
4477 if (arg->soft_match)
a92713e6 4478 return FALSE;
a1d78564
RS
4479 as_bad (_("Invalid performance register (%ld)"), (unsigned long) sval);
4480 }
4481
4482 insn_insert_operand (arg->insn, operand, sval);
a92713e6 4483 return TRUE;
a1d78564
RS
4484}
4485
4486/* OP_ADDIUSP matcher. */
4487
a92713e6 4488static bfd_boolean
a1d78564 4489match_addiusp_operand (struct mips_arg_info *arg,
a92713e6 4490 const struct mips_operand *operand)
a1d78564
RS
4491{
4492 offsetT sval;
4493 unsigned int uval;
4494
a92713e6
RS
4495 if (!match_const_int (arg, &sval, -256))
4496 return FALSE;
a1d78564
RS
4497
4498 if (sval % 4)
a92713e6 4499 return FALSE;
a1d78564
RS
4500
4501 sval /= 4;
4502 if (!(sval >= -258 && sval <= 257) || (sval >= -2 && sval <= 1))
a92713e6 4503 return FALSE;
a1d78564
RS
4504
4505 uval = (unsigned int) sval;
4506 uval = ((uval >> 1) & ~0xff) | (uval & 0xff);
4507 insn_insert_operand (arg->insn, operand, uval);
a92713e6 4508 return TRUE;
a1d78564
RS
4509}
4510
4511/* OP_CLO_CLZ_DEST matcher. */
4512
a92713e6 4513static bfd_boolean
a1d78564 4514match_clo_clz_dest_operand (struct mips_arg_info *arg,
a92713e6 4515 const struct mips_operand *operand)
a1d78564
RS
4516{
4517 unsigned int regno;
4518
a92713e6
RS
4519 if (!match_reg (arg, OP_REG_GP, &regno))
4520 return FALSE;
a1d78564 4521
a1d78564 4522 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
a92713e6 4523 return TRUE;
a1d78564
RS
4524}
4525
4526/* OP_LWM_SWM_LIST matcher. */
4527
a92713e6 4528static bfd_boolean
a1d78564 4529match_lwm_swm_list_operand (struct mips_arg_info *arg,
a92713e6 4530 const struct mips_operand *operand)
a1d78564 4531{
a92713e6
RS
4532 unsigned int reglist, sregs, ra, regno1, regno2;
4533 struct mips_arg_info reset;
a1d78564 4534
a92713e6
RS
4535 reglist = 0;
4536 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
4537 return FALSE;
4538 do
4539 {
4540 if (regno2 == FP && regno1 >= S0 && regno1 <= S7)
4541 {
4542 reglist |= 1 << FP;
4543 regno2 = S7;
4544 }
4545 reglist |= ((1U << regno2 << 1) - 1) & -(1U << regno1);
4546 reset = *arg;
4547 }
4548 while (match_char (arg, ',')
4549 && match_reg_range (arg, OP_REG_GP, &regno1, &regno2));
4550 *arg = reset;
a1d78564
RS
4551
4552 if (operand->size == 2)
4553 {
4554 /* The list must include both ra and s0-sN, for 0 <= N <= 3. E.g.:
4555
4556 s0, ra
4557 s0, s1, ra, s2, s3
4558 s0-s2, ra
4559
4560 and any permutations of these. */
4561 if ((reglist & 0xfff1ffff) != 0x80010000)
a92713e6 4562 return FALSE;
a1d78564
RS
4563
4564 sregs = (reglist >> 17) & 7;
4565 ra = 0;
4566 }
4567 else
4568 {
4569 /* The list must include at least one of ra and s0-sN,
4570 for 0 <= N <= 8. (Note that there is a gap between s7 and s8,
4571 which are $23 and $30 respectively.) E.g.:
4572
4573 ra
4574 s0
4575 ra, s0, s1, s2
4576 s0-s8
4577 s0-s5, ra
4578
4579 and any permutations of these. */
4580 if ((reglist & 0x3f00ffff) != 0)
a92713e6 4581 return FALSE;
a1d78564
RS
4582
4583 ra = (reglist >> 27) & 0x10;
4584 sregs = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
4585 }
4586 sregs += 1;
4587 if ((sregs & -sregs) != sregs)
a92713e6 4588 return FALSE;
a1d78564
RS
4589
4590 insn_insert_operand (arg->insn, operand, (ffs (sregs) - 1) | ra);
a92713e6 4591 return TRUE;
a1d78564
RS
4592}
4593
364215c8
RS
4594/* OP_ENTRY_EXIT_LIST matcher. */
4595
a92713e6 4596static unsigned int
364215c8 4597match_entry_exit_operand (struct mips_arg_info *arg,
a92713e6 4598 const struct mips_operand *operand)
364215c8
RS
4599{
4600 unsigned int mask;
4601 bfd_boolean is_exit;
4602
4603 /* The format is the same for both ENTRY and EXIT, but the constraints
4604 are different. */
4605 is_exit = strcmp (arg->insn->insn_mo->name, "exit") == 0;
4606 mask = (is_exit ? 7 << 3 : 0);
a92713e6 4607 do
364215c8
RS
4608 {
4609 unsigned int regno1, regno2;
4610 bfd_boolean is_freg;
4611
a92713e6 4612 if (match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
364215c8 4613 is_freg = FALSE;
a92713e6 4614 else if (match_reg_range (arg, OP_REG_FP, &regno1, &regno2))
364215c8
RS
4615 is_freg = TRUE;
4616 else
a92713e6 4617 return FALSE;
364215c8
RS
4618
4619 if (is_exit && is_freg && regno1 == 0 && regno2 < 2)
4620 {
4621 mask &= ~(7 << 3);
4622 mask |= (5 + regno2) << 3;
4623 }
4624 else if (!is_exit && regno1 == 4 && regno2 >= 4 && regno2 <= 7)
4625 mask |= (regno2 - 3) << 3;
4626 else if (regno1 == 16 && regno2 >= 16 && regno2 <= 17)
4627 mask |= (regno2 - 15) << 1;
4628 else if (regno1 == RA && regno2 == RA)
4629 mask |= 1;
4630 else
a92713e6 4631 return FALSE;
364215c8 4632 }
a92713e6
RS
4633 while (match_char (arg, ','));
4634
364215c8 4635 insn_insert_operand (arg->insn, operand, mask);
a92713e6 4636 return TRUE;
364215c8
RS
4637}
4638
4639/* OP_SAVE_RESTORE_LIST matcher. */
4640
a92713e6
RS
4641static bfd_boolean
4642match_save_restore_list_operand (struct mips_arg_info *arg)
364215c8
RS
4643{
4644 unsigned int opcode, args, statics, sregs;
4645 unsigned int num_frame_sizes, num_args, num_statics, num_sregs;
364215c8
RS
4646 offsetT frame_size;
4647 const char *error;
4648
4649 error = 0;
4650 opcode = arg->insn->insn_opcode;
4651 frame_size = 0;
4652 num_frame_sizes = 0;
4653 args = 0;
4654 statics = 0;
4655 sregs = 0;
a92713e6 4656 do
364215c8
RS
4657 {
4658 unsigned int regno1, regno2;
4659
a92713e6 4660 if (arg->token->type == OT_INTEGER)
364215c8
RS
4661 {
4662 /* Handle the frame size. */
a92713e6
RS
4663 if (!match_const_int (arg, &frame_size, 0))
4664 return FALSE;
364215c8 4665 num_frame_sizes += 1;
364215c8
RS
4666 }
4667 else
4668 {
a92713e6
RS
4669 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
4670 return FALSE;
364215c8
RS
4671
4672 while (regno1 <= regno2)
4673 {
4674 if (regno1 >= 4 && regno1 <= 7)
4675 {
4676 if (num_frame_sizes == 0)
4677 /* args $a0-$a3 */
4678 args |= 1 << (regno1 - 4);
4679 else
4680 /* statics $a0-$a3 */
4681 statics |= 1 << (regno1 - 4);
4682 }
4683 else if (regno1 >= 16 && regno1 <= 23)
4684 /* $s0-$s7 */
4685 sregs |= 1 << (regno1 - 16);
4686 else if (regno1 == 30)
4687 /* $s8 */
4688 sregs |= 1 << 8;
4689 else if (regno1 == 31)
4690 /* Add $ra to insn. */
4691 opcode |= 0x40;
4692 else
a92713e6 4693 return FALSE;
364215c8
RS
4694 regno1 += 1;
4695 if (regno1 == 24)
4696 regno1 = 30;
4697 }
4698 }
364215c8 4699 }
a92713e6 4700 while (match_char (arg, ','));
364215c8
RS
4701
4702 /* Encode args/statics combination. */
4703 if (args & statics)
a92713e6 4704 return FALSE;
364215c8
RS
4705 else if (args == 0xf)
4706 /* All $a0-$a3 are args. */
4707 opcode |= MIPS16_ALL_ARGS << 16;
4708 else if (statics == 0xf)
4709 /* All $a0-$a3 are statics. */
4710 opcode |= MIPS16_ALL_STATICS << 16;
4711 else
4712 {
4713 /* Count arg registers. */
4714 num_args = 0;
4715 while (args & 0x1)
4716 {
4717 args >>= 1;
4718 num_args += 1;
4719 }
4720 if (args != 0)
a92713e6 4721 return FALSE;
364215c8
RS
4722
4723 /* Count static registers. */
4724 num_statics = 0;
4725 while (statics & 0x8)
4726 {
4727 statics = (statics << 1) & 0xf;
4728 num_statics += 1;
4729 }
4730 if (statics != 0)
a92713e6 4731 return FALSE;
364215c8
RS
4732
4733 /* Encode args/statics. */
4734 opcode |= ((num_args << 2) | num_statics) << 16;
4735 }
4736
4737 /* Encode $s0/$s1. */
4738 if (sregs & (1 << 0)) /* $s0 */
4739 opcode |= 0x20;
4740 if (sregs & (1 << 1)) /* $s1 */
4741 opcode |= 0x10;
4742 sregs >>= 2;
4743
4744 /* Encode $s2-$s8. */
4745 num_sregs = 0;
4746 while (sregs & 1)
4747 {
4748 sregs >>= 1;
4749 num_sregs += 1;
4750 }
4751 if (sregs != 0)
a92713e6 4752 return FALSE;
364215c8
RS
4753 opcode |= num_sregs << 24;
4754
4755 /* Encode frame size. */
4756 if (num_frame_sizes == 0)
4757 error = _("Missing frame size");
4758 else if (num_frame_sizes > 1)
4759 error = _("Frame size specified twice");
4760 else if ((frame_size & 7) != 0 || frame_size < 0 || frame_size > 0xff * 8)
4761 error = _("Invalid frame size");
4762 else if (frame_size != 128 || (opcode >> 16) != 0)
4763 {
4764 frame_size /= 8;
4765 opcode |= (((frame_size & 0xf0) << 16)
4766 | (frame_size & 0x0f));
4767 }
4768
4769 if (error)
4770 {
4771 if (arg->soft_match)
a92713e6 4772 return FALSE;
4a06e5a2 4773 as_bad ("%s", error);
364215c8
RS
4774 }
4775
4776 /* Finally build the instruction. */
4777 if ((opcode >> 16) != 0 || frame_size == 0)
4778 opcode |= MIPS16_EXTEND;
4779 arg->insn->insn_opcode = opcode;
a92713e6 4780 return TRUE;
364215c8
RS
4781}
4782
a1d78564
RS
4783/* OP_MDMX_IMM_REG matcher. */
4784
a92713e6 4785static bfd_boolean
a1d78564 4786match_mdmx_imm_reg_operand (struct mips_arg_info *arg,
a92713e6 4787 const struct mips_operand *operand)
a1d78564 4788{
a92713e6 4789 unsigned int regno, uval;
a1d78564
RS
4790 bfd_boolean is_qh;
4791 const struct mips_opcode *opcode;
4792
4793 /* The mips_opcode records whether this is an octobyte or quadhalf
4794 instruction. Start out with that bit in place. */
4795 opcode = arg->insn->insn_mo;
4796 uval = mips_extract_operand (operand, opcode->match);
4797 is_qh = (uval != 0);
4798
a92713e6 4799 if (arg->token->type == OT_REG || arg->token->type == OT_REG_ELEMENT)
a1d78564
RS
4800 {
4801 if ((opcode->membership & INSN_5400)
4802 && strcmp (opcode->name, "rzu.ob") == 0)
4803 {
4804 if (arg->soft_match)
a92713e6 4805 return FALSE;
a1d78564
RS
4806 as_bad (_("Operand %d of `%s' must be an immediate"),
4807 arg->argnum, opcode->name);
4808 }
4809
4810 /* Check whether this is a vector register or a broadcast of
4811 a single element. */
a92713e6 4812 if (arg->token->type == OT_REG_ELEMENT)
a1d78564 4813 {
a92713e6
RS
4814 if (!match_regno (arg, OP_REG_VEC, arg->token->u.reg_element.regno,
4815 &regno))
4816 return FALSE;
4817 if (arg->token->u.reg_element.index > (is_qh ? 3 : 7))
a1d78564
RS
4818 {
4819 if (arg->soft_match)
a92713e6 4820 return FALSE;
a1d78564 4821 as_bad (_("Invalid element selector"));
a1d78564 4822 }
a1d78564 4823 else
a92713e6 4824 uval |= arg->token->u.reg_element.index << (is_qh ? 2 : 1) << 5;
a1d78564
RS
4825 }
4826 else
4827 {
4828 /* A full vector. */
4829 if ((opcode->membership & INSN_5400)
4830 && (strcmp (opcode->name, "sll.ob") == 0
4831 || strcmp (opcode->name, "srl.ob") == 0))
4832 {
4833 if (arg->soft_match)
a92713e6 4834 return FALSE;
a1d78564
RS
4835 as_bad (_("Operand %d of `%s' must be scalar"),
4836 arg->argnum, opcode->name);
4837 }
4838
a92713e6
RS
4839 if (!match_regno (arg, OP_REG_VEC, arg->token->u.regno, &regno))
4840 return FALSE;
a1d78564
RS
4841 if (is_qh)
4842 uval |= MDMX_FMTSEL_VEC_QH << 5;
4843 else
4844 uval |= MDMX_FMTSEL_VEC_OB << 5;
4845 }
a1d78564 4846 uval |= regno;
a92713e6 4847 ++arg->token;
a1d78564
RS
4848 }
4849 else
4850 {
4851 offsetT sval;
4852
a92713e6
RS
4853 if (!match_const_int (arg, &sval, 0))
4854 return FALSE;
a1d78564
RS
4855 if (sval < 0 || sval > 31)
4856 {
4857 if (arg->soft_match)
a92713e6 4858 return FALSE;
a1d78564
RS
4859 report_bad_range (arg->insn, arg->argnum, sval, 0, 31, FALSE);
4860 }
4861 uval |= (sval & 31);
4862 if (is_qh)
4863 uval |= MDMX_FMTSEL_IMM_QH << 5;
4864 else
4865 uval |= MDMX_FMTSEL_IMM_OB << 5;
4866 }
4867 insn_insert_operand (arg->insn, operand, uval);
a92713e6 4868 return TRUE;
a1d78564
RS
4869}
4870
4871/* OP_PC matcher. */
4872
a92713e6
RS
4873static bfd_boolean
4874match_pc_operand (struct mips_arg_info *arg)
a1d78564 4875{
a92713e6
RS
4876 if (arg->token->type == OT_REG && (arg->token->u.regno & RTYPE_PC))
4877 {
4878 ++arg->token;
4879 return TRUE;
4880 }
4881 return FALSE;
a1d78564
RS
4882}
4883
4884/* OP_REPEAT_DEST_REG and OP_REPEAT_PREV_REG matcher. OTHER_REGNO is the
4885 register that we need to match. */
4886
a92713e6
RS
4887static bfd_boolean
4888match_tied_reg_operand (struct mips_arg_info *arg, unsigned int other_regno)
a1d78564
RS
4889{
4890 unsigned int regno;
4891
a92713e6 4892 return match_reg (arg, OP_REG_GP, &regno) && regno == other_regno;
a1d78564
RS
4893}
4894
89565f1b
RS
4895/* Read a floating-point constant from S for LI.S or LI.D. LENGTH is
4896 the length of the value in bytes (4 for float, 8 for double) and
4897 USING_GPRS says whether the destination is a GPR rather than an FPR.
4898
4899 Return the constant in IMM and OFFSET as follows:
4900
4901 - If the constant should be loaded via memory, set IMM to O_absent and
4902 OFFSET to the memory address.
4903
4904 - Otherwise, if the constant should be loaded into two 32-bit registers,
4905 set IMM to the O_constant to load into the high register and OFFSET
4906 to the corresponding value for the low register.
4907
4908 - Otherwise, set IMM to the full O_constant and set OFFSET to O_absent.
4909
4910 These constants only appear as the last operand in an instruction,
4911 and every instruction that accepts them in any variant accepts them
4912 in all variants. This means we don't have to worry about backing out
4913 any changes if the instruction does not match. We just match
4914 unconditionally and report an error if the constant is invalid. */
4915
a92713e6
RS
4916static bfd_boolean
4917match_float_constant (struct mips_arg_info *arg, expressionS *imm,
4918 expressionS *offset, int length, bfd_boolean using_gprs)
89565f1b 4919{
a92713e6 4920 char *p;
89565f1b
RS
4921 segT seg, new_seg;
4922 subsegT subseg;
4923 const char *newname;
a92713e6 4924 unsigned char *data;
89565f1b
RS
4925
4926 /* Where the constant is placed is based on how the MIPS assembler
4927 does things:
4928
4929 length == 4 && using_gprs -- immediate value only
4930 length == 8 && using_gprs -- .rdata or immediate value
4931 length == 4 && !using_gprs -- .lit4 or immediate value
4932 length == 8 && !using_gprs -- .lit8 or immediate value
4933
4934 The .lit4 and .lit8 sections are only used if permitted by the
4935 -G argument. */
a92713e6
RS
4936 if (arg->token->type != OT_FLOAT)
4937 return FALSE;
4938
4939 gas_assert (arg->token->u.flt.length == length);
4940 data = arg->token->u.flt.data;
4941 ++arg->token;
89565f1b
RS
4942
4943 /* Handle 32-bit constants for which an immediate value is best. */
4944 if (length == 4
4945 && (using_gprs
4946 || g_switch_value < 4
4947 || (data[0] == 0 && data[1] == 0)
4948 || (data[2] == 0 && data[3] == 0)))
4949 {
4950 imm->X_op = O_constant;
4951 if (!target_big_endian)
4952 imm->X_add_number = bfd_getl32 (data);
4953 else
4954 imm->X_add_number = bfd_getb32 (data);
4955 offset->X_op = O_absent;
a92713e6 4956 return TRUE;
89565f1b
RS
4957 }
4958
4959 /* Handle 64-bit constants for which an immediate value is best. */
4960 if (length == 8
4961 && !mips_disable_float_construction
4962 /* Constants can only be constructed in GPRs and copied
4963 to FPRs if the GPRs are at least as wide as the FPRs.
4964 Force the constant into memory if we are using 64-bit FPRs
4965 but the GPRs are only 32 bits wide. */
4966 /* ??? No longer true with the addition of MTHC1, but this
4967 is legacy code... */
4968 && (using_gprs || !(HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
4969 && ((data[0] == 0 && data[1] == 0)
4970 || (data[2] == 0 && data[3] == 0))
4971 && ((data[4] == 0 && data[5] == 0)
4972 || (data[6] == 0 && data[7] == 0)))
4973 {
4974 /* The value is simple enough to load with a couple of instructions.
4975 If using 32-bit registers, set IMM to the high order 32 bits and
4976 OFFSET to the low order 32 bits. Otherwise, set IMM to the entire
4977 64 bit constant. */
4978 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
4979 {
4980 imm->X_op = O_constant;
4981 offset->X_op = O_constant;
4982 if (!target_big_endian)
4983 {
4984 imm->X_add_number = bfd_getl32 (data + 4);
4985 offset->X_add_number = bfd_getl32 (data);
4986 }
4987 else
4988 {
4989 imm->X_add_number = bfd_getb32 (data);
4990 offset->X_add_number = bfd_getb32 (data + 4);
4991 }
4992 if (offset->X_add_number == 0)
4993 offset->X_op = O_absent;
4994 }
4995 else
4996 {
4997 imm->X_op = O_constant;
4998 if (!target_big_endian)
4999 imm->X_add_number = bfd_getl64 (data);
5000 else
5001 imm->X_add_number = bfd_getb64 (data);
5002 offset->X_op = O_absent;
5003 }
a92713e6 5004 return TRUE;
89565f1b
RS
5005 }
5006
5007 /* Switch to the right section. */
5008 seg = now_seg;
5009 subseg = now_subseg;
5010 if (length == 4)
5011 {
5012 gas_assert (!using_gprs && g_switch_value >= 4);
5013 newname = ".lit4";
5014 }
5015 else
5016 {
5017 if (using_gprs || g_switch_value < 8)
5018 newname = RDATA_SECTION_NAME;
5019 else
5020 newname = ".lit8";
5021 }
5022
5023 new_seg = subseg_new (newname, (subsegT) 0);
5024 bfd_set_section_flags (stdoutput, new_seg,
5025 SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA);
5026 frag_align (length == 4 ? 2 : 3, 0, 0);
5027 if (strncmp (TARGET_OS, "elf", 3) != 0)
5028 record_alignment (new_seg, 4);
5029 else
5030 record_alignment (new_seg, length == 4 ? 2 : 3);
5031 if (seg == now_seg)
5032 as_bad (_("Can't use floating point insn in this section"));
5033
5034 /* Set the argument to the current address in the section. */
5035 imm->X_op = O_absent;
5036 offset->X_op = O_symbol;
5037 offset->X_add_symbol = symbol_temp_new_now ();
5038 offset->X_add_number = 0;
5039
5040 /* Put the floating point number into the section. */
5041 p = frag_more (length);
5042 memcpy (p, data, length);
5043
5044 /* Switch back to the original section. */
5045 subseg_set (seg, subseg);
a92713e6 5046 return TRUE;
89565f1b
RS
5047}
5048
a1d78564
RS
5049/* S is the text seen for ARG. Match it against OPERAND. Return the end
5050 of the argument text if the match is successful, otherwise return null. */
5051
a92713e6 5052static bfd_boolean
a1d78564 5053match_operand (struct mips_arg_info *arg,
a92713e6 5054 const struct mips_operand *operand)
a1d78564
RS
5055{
5056 switch (operand->type)
5057 {
5058 case OP_INT:
a92713e6 5059 return match_int_operand (arg, operand);
a1d78564
RS
5060
5061 case OP_MAPPED_INT:
a92713e6 5062 return match_mapped_int_operand (arg, operand);
a1d78564
RS
5063
5064 case OP_MSB:
a92713e6 5065 return match_msb_operand (arg, operand);
a1d78564
RS
5066
5067 case OP_REG:
a92713e6 5068 return match_reg_operand (arg, operand);
a1d78564
RS
5069
5070 case OP_REG_PAIR:
a92713e6 5071 return match_reg_pair_operand (arg, operand);
a1d78564
RS
5072
5073 case OP_PCREL:
a92713e6 5074 return match_pcrel_operand (arg);
a1d78564
RS
5075
5076 case OP_PERF_REG:
a92713e6 5077 return match_perf_reg_operand (arg, operand);
a1d78564
RS
5078
5079 case OP_ADDIUSP_INT:
a92713e6 5080 return match_addiusp_operand (arg, operand);
a1d78564
RS
5081
5082 case OP_CLO_CLZ_DEST:
a92713e6 5083 return match_clo_clz_dest_operand (arg, operand);
a1d78564
RS
5084
5085 case OP_LWM_SWM_LIST:
a92713e6 5086 return match_lwm_swm_list_operand (arg, operand);
a1d78564
RS
5087
5088 case OP_ENTRY_EXIT_LIST:
a92713e6 5089 return match_entry_exit_operand (arg, operand);
364215c8 5090
a1d78564 5091 case OP_SAVE_RESTORE_LIST:
a92713e6 5092 return match_save_restore_list_operand (arg);
a1d78564
RS
5093
5094 case OP_MDMX_IMM_REG:
a92713e6 5095 return match_mdmx_imm_reg_operand (arg, operand);
a1d78564
RS
5096
5097 case OP_REPEAT_DEST_REG:
a92713e6 5098 return match_tied_reg_operand (arg, arg->dest_regno);
a1d78564
RS
5099
5100 case OP_REPEAT_PREV_REG:
a92713e6 5101 return match_tied_reg_operand (arg, arg->last_regno);
a1d78564
RS
5102
5103 case OP_PC:
a92713e6 5104 return match_pc_operand (arg);
a1d78564
RS
5105 }
5106 abort ();
5107}
5108
5109/* ARG is the state after successfully matching an instruction.
5110 Issue any queued-up warnings. */
5111
5112static void
5113check_completed_insn (struct mips_arg_info *arg)
5114{
5115 if (arg->seen_at)
5116 {
5117 if (AT == ATREG)
5118 as_warn (_("Used $at without \".set noat\""));
5119 else
5120 as_warn (_("Used $%u with \".set at=$%u\""), AT, AT);
5121 }
5122}
a1d78564 5123
85fcb30f
RS
5124/* Return true if modifying general-purpose register REG needs a delay. */
5125
5126static bfd_boolean
5127reg_needs_delay (unsigned int reg)
5128{
5129 unsigned long prev_pinfo;
5130
5131 prev_pinfo = history[0].insn_mo->pinfo;
5132 if (!mips_opts.noreorder
5133 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY) && !gpr_interlocks)
5134 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY) && !cop_interlocks))
5135 && (gpr_write_mask (&history[0]) & (1 << reg)))
5136 return TRUE;
5137
5138 return FALSE;
5139}
5140
71400594
RS
5141/* Classify an instruction according to the FIX_VR4120_* enumeration.
5142 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
5143 by VR4120 errata. */
4d7206a2 5144
71400594
RS
5145static unsigned int
5146classify_vr4120_insn (const char *name)
252b5132 5147{
71400594
RS
5148 if (strncmp (name, "macc", 4) == 0)
5149 return FIX_VR4120_MACC;
5150 if (strncmp (name, "dmacc", 5) == 0)
5151 return FIX_VR4120_DMACC;
5152 if (strncmp (name, "mult", 4) == 0)
5153 return FIX_VR4120_MULT;
5154 if (strncmp (name, "dmult", 5) == 0)
5155 return FIX_VR4120_DMULT;
5156 if (strstr (name, "div"))
5157 return FIX_VR4120_DIV;
5158 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
5159 return FIX_VR4120_MTHILO;
5160 return NUM_FIX_VR4120_CLASSES;
5161}
252b5132 5162
ff239038
CM
5163#define INSN_ERET 0x42000018
5164#define INSN_DERET 0x4200001f
5165
71400594
RS
5166/* Return the number of instructions that must separate INSN1 and INSN2,
5167 where INSN1 is the earlier instruction. Return the worst-case value
5168 for any INSN2 if INSN2 is null. */
252b5132 5169
71400594
RS
5170static unsigned int
5171insns_between (const struct mips_cl_insn *insn1,
5172 const struct mips_cl_insn *insn2)
5173{
5174 unsigned long pinfo1, pinfo2;
4c260379 5175 unsigned int mask;
71400594 5176
85fcb30f
RS
5177 /* If INFO2 is null, pessimistically assume that all flags are set for
5178 the second instruction. */
71400594
RS
5179 pinfo1 = insn1->insn_mo->pinfo;
5180 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
252b5132 5181
71400594
RS
5182 /* For most targets, write-after-read dependencies on the HI and LO
5183 registers must be separated by at least two instructions. */
5184 if (!hilo_interlocks)
252b5132 5185 {
71400594
RS
5186 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
5187 return 2;
5188 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
5189 return 2;
5190 }
5191
5192 /* If we're working around r7000 errata, there must be two instructions
5193 between an mfhi or mflo and any instruction that uses the result. */
5194 if (mips_7000_hilo_fix
df58fc94 5195 && !mips_opts.micromips
71400594 5196 && MF_HILO_INSN (pinfo1)
85fcb30f 5197 && (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1))))
71400594
RS
5198 return 2;
5199
ff239038
CM
5200 /* If we're working around 24K errata, one instruction is required
5201 if an ERET or DERET is followed by a branch instruction. */
df58fc94 5202 if (mips_fix_24k && !mips_opts.micromips)
ff239038
CM
5203 {
5204 if (insn1->insn_opcode == INSN_ERET
5205 || insn1->insn_opcode == INSN_DERET)
5206 {
5207 if (insn2 == NULL
5208 || insn2->insn_opcode == INSN_ERET
5209 || insn2->insn_opcode == INSN_DERET
11625dd8 5210 || delayed_branch_p (insn2))
ff239038
CM
5211 return 1;
5212 }
5213 }
5214
71400594
RS
5215 /* If working around VR4120 errata, check for combinations that need
5216 a single intervening instruction. */
df58fc94 5217 if (mips_fix_vr4120 && !mips_opts.micromips)
71400594
RS
5218 {
5219 unsigned int class1, class2;
252b5132 5220
71400594
RS
5221 class1 = classify_vr4120_insn (insn1->insn_mo->name);
5222 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
252b5132 5223 {
71400594
RS
5224 if (insn2 == NULL)
5225 return 1;
5226 class2 = classify_vr4120_insn (insn2->insn_mo->name);
5227 if (vr4120_conflicts[class1] & (1 << class2))
5228 return 1;
252b5132 5229 }
71400594
RS
5230 }
5231
df58fc94 5232 if (!HAVE_CODE_COMPRESSION)
71400594
RS
5233 {
5234 /* Check for GPR or coprocessor load delays. All such delays
5235 are on the RT register. */
5236 /* Itbl support may require additional care here. */
5237 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
5238 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
252b5132 5239 {
85fcb30f 5240 if (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1)))
71400594
RS
5241 return 1;
5242 }
5243
5244 /* Check for generic coprocessor hazards.
5245
5246 This case is not handled very well. There is no special
5247 knowledge of CP0 handling, and the coprocessors other than
5248 the floating point unit are not distinguished at all. */
5249 /* Itbl support may require additional care here. FIXME!
5250 Need to modify this to include knowledge about
5251 user specified delays! */
5252 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
5253 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
5254 {
5255 /* Handle cases where INSN1 writes to a known general coprocessor
5256 register. There must be a one instruction delay before INSN2
5257 if INSN2 reads that register, otherwise no delay is needed. */
4c260379
RS
5258 mask = fpr_write_mask (insn1);
5259 if (mask != 0)
252b5132 5260 {
4c260379 5261 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
71400594 5262 return 1;
252b5132
RH
5263 }
5264 else
5265 {
71400594
RS
5266 /* Read-after-write dependencies on the control registers
5267 require a two-instruction gap. */
5268 if ((pinfo1 & INSN_WRITE_COND_CODE)
5269 && (pinfo2 & INSN_READ_COND_CODE))
5270 return 2;
5271
5272 /* We don't know exactly what INSN1 does. If INSN2 is
5273 also a coprocessor instruction, assume there must be
5274 a one instruction gap. */
5275 if (pinfo2 & INSN_COP)
5276 return 1;
252b5132
RH
5277 }
5278 }
6b76fefe 5279
71400594
RS
5280 /* Check for read-after-write dependencies on the coprocessor
5281 control registers in cases where INSN1 does not need a general
5282 coprocessor delay. This means that INSN1 is a floating point
5283 comparison instruction. */
5284 /* Itbl support may require additional care here. */
5285 else if (!cop_interlocks
5286 && (pinfo1 & INSN_WRITE_COND_CODE)
5287 && (pinfo2 & INSN_READ_COND_CODE))
5288 return 1;
5289 }
6b76fefe 5290
71400594
RS
5291 return 0;
5292}
6b76fefe 5293
7d8e00cf
RS
5294/* Return the number of nops that would be needed to work around the
5295 VR4130 mflo/mfhi errata if instruction INSN immediately followed
932d1a1b
RS
5296 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
5297 that are contained within the first IGNORE instructions of HIST. */
7d8e00cf
RS
5298
5299static int
932d1a1b 5300nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
7d8e00cf
RS
5301 const struct mips_cl_insn *insn)
5302{
4c260379
RS
5303 int i, j;
5304 unsigned int mask;
7d8e00cf
RS
5305
5306 /* Check if the instruction writes to HI or LO. MTHI and MTLO
5307 are not affected by the errata. */
5308 if (insn != 0
5309 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
5310 || strcmp (insn->insn_mo->name, "mtlo") == 0
5311 || strcmp (insn->insn_mo->name, "mthi") == 0))
5312 return 0;
5313
5314 /* Search for the first MFLO or MFHI. */
5315 for (i = 0; i < MAX_VR4130_NOPS; i++)
91d6fa6a 5316 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
7d8e00cf
RS
5317 {
5318 /* Extract the destination register. */
4c260379 5319 mask = gpr_write_mask (&hist[i]);
7d8e00cf
RS
5320
5321 /* No nops are needed if INSN reads that register. */
4c260379 5322 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
7d8e00cf
RS
5323 return 0;
5324
5325 /* ...or if any of the intervening instructions do. */
5326 for (j = 0; j < i; j++)
4c260379 5327 if (gpr_read_mask (&hist[j]) & mask)
7d8e00cf
RS
5328 return 0;
5329
932d1a1b
RS
5330 if (i >= ignore)
5331 return MAX_VR4130_NOPS - i;
7d8e00cf
RS
5332 }
5333 return 0;
5334}
5335
15be625d
CM
5336#define BASE_REG_EQ(INSN1, INSN2) \
5337 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
5338 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
5339
5340/* Return the minimum alignment for this store instruction. */
5341
5342static int
5343fix_24k_align_to (const struct mips_opcode *mo)
5344{
5345 if (strcmp (mo->name, "sh") == 0)
5346 return 2;
5347
5348 if (strcmp (mo->name, "swc1") == 0
5349 || strcmp (mo->name, "swc2") == 0
5350 || strcmp (mo->name, "sw") == 0
5351 || strcmp (mo->name, "sc") == 0
5352 || strcmp (mo->name, "s.s") == 0)
5353 return 4;
5354
5355 if (strcmp (mo->name, "sdc1") == 0
5356 || strcmp (mo->name, "sdc2") == 0
5357 || strcmp (mo->name, "s.d") == 0)
5358 return 8;
5359
5360 /* sb, swl, swr */
5361 return 1;
5362}
5363
5364struct fix_24k_store_info
5365 {
5366 /* Immediate offset, if any, for this store instruction. */
5367 short off;
5368 /* Alignment required by this store instruction. */
5369 int align_to;
5370 /* True for register offsets. */
5371 int register_offset;
5372 };
5373
5374/* Comparison function used by qsort. */
5375
5376static int
5377fix_24k_sort (const void *a, const void *b)
5378{
5379 const struct fix_24k_store_info *pos1 = a;
5380 const struct fix_24k_store_info *pos2 = b;
5381
5382 return (pos1->off - pos2->off);
5383}
5384
5385/* INSN is a store instruction. Try to record the store information
5386 in STINFO. Return false if the information isn't known. */
5387
5388static bfd_boolean
5389fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
ab9794cf 5390 const struct mips_cl_insn *insn)
15be625d
CM
5391{
5392 /* The instruction must have a known offset. */
5393 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
5394 return FALSE;
5395
5396 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
5397 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
5398 return TRUE;
5399}
5400
932d1a1b
RS
5401/* Return the number of nops that would be needed to work around the 24k
5402 "lost data on stores during refill" errata if instruction INSN
5403 immediately followed the 2 instructions described by HIST.
5404 Ignore hazards that are contained within the first IGNORE
5405 instructions of HIST.
5406
5407 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
5408 for the data cache refills and store data. The following describes
5409 the scenario where the store data could be lost.
5410
5411 * A data cache miss, due to either a load or a store, causing fill
5412 data to be supplied by the memory subsystem
5413 * The first three doublewords of fill data are returned and written
5414 into the cache
5415 * A sequence of four stores occurs in consecutive cycles around the
5416 final doubleword of the fill:
5417 * Store A
5418 * Store B
5419 * Store C
5420 * Zero, One or more instructions
5421 * Store D
5422
5423 The four stores A-D must be to different doublewords of the line that
5424 is being filled. The fourth instruction in the sequence above permits
5425 the fill of the final doubleword to be transferred from the FSB into
5426 the cache. In the sequence above, the stores may be either integer
5427 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
5428 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
5429 different doublewords on the line. If the floating point unit is
5430 running in 1:2 mode, it is not possible to create the sequence above
5431 using only floating point store instructions.
15be625d
CM
5432
5433 In this case, the cache line being filled is incorrectly marked
5434 invalid, thereby losing the data from any store to the line that
5435 occurs between the original miss and the completion of the five
5436 cycle sequence shown above.
5437
932d1a1b 5438 The workarounds are:
15be625d 5439
932d1a1b
RS
5440 * Run the data cache in write-through mode.
5441 * Insert a non-store instruction between
5442 Store A and Store B or Store B and Store C. */
15be625d
CM
5443
5444static int
932d1a1b 5445nops_for_24k (int ignore, const struct mips_cl_insn *hist,
15be625d
CM
5446 const struct mips_cl_insn *insn)
5447{
5448 struct fix_24k_store_info pos[3];
5449 int align, i, base_offset;
5450
932d1a1b
RS
5451 if (ignore >= 2)
5452 return 0;
5453
ab9794cf
RS
5454 /* If the previous instruction wasn't a store, there's nothing to
5455 worry about. */
15be625d
CM
5456 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
5457 return 0;
5458
ab9794cf
RS
5459 /* If the instructions after the previous one are unknown, we have
5460 to assume the worst. */
5461 if (!insn)
15be625d
CM
5462 return 1;
5463
ab9794cf
RS
5464 /* Check whether we are dealing with three consecutive stores. */
5465 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
5466 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
15be625d
CM
5467 return 0;
5468
5469 /* If we don't know the relationship between the store addresses,
5470 assume the worst. */
ab9794cf 5471 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
15be625d
CM
5472 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
5473 return 1;
5474
5475 if (!fix_24k_record_store_info (&pos[0], insn)
5476 || !fix_24k_record_store_info (&pos[1], &hist[0])
5477 || !fix_24k_record_store_info (&pos[2], &hist[1]))
5478 return 1;
5479
5480 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
5481
5482 /* Pick a value of ALIGN and X such that all offsets are adjusted by
5483 X bytes and such that the base register + X is known to be aligned
5484 to align bytes. */
5485
5486 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
5487 align = 8;
5488 else
5489 {
5490 align = pos[0].align_to;
5491 base_offset = pos[0].off;
5492 for (i = 1; i < 3; i++)
5493 if (align < pos[i].align_to)
5494 {
5495 align = pos[i].align_to;
5496 base_offset = pos[i].off;
5497 }
5498 for (i = 0; i < 3; i++)
5499 pos[i].off -= base_offset;
5500 }
5501
5502 pos[0].off &= ~align + 1;
5503 pos[1].off &= ~align + 1;
5504 pos[2].off &= ~align + 1;
5505
5506 /* If any two stores write to the same chunk, they also write to the
5507 same doubleword. The offsets are still sorted at this point. */
5508 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
5509 return 0;
5510
5511 /* A range of at least 9 bytes is needed for the stores to be in
5512 non-overlapping doublewords. */
5513 if (pos[2].off - pos[0].off <= 8)
5514 return 0;
5515
5516 if (pos[2].off - pos[1].off >= 24
5517 || pos[1].off - pos[0].off >= 24
5518 || pos[2].off - pos[0].off >= 32)
5519 return 0;
5520
5521 return 1;
5522}
5523
71400594 5524/* Return the number of nops that would be needed if instruction INSN
91d6fa6a 5525 immediately followed the MAX_NOPS instructions given by HIST,
932d1a1b
RS
5526 where HIST[0] is the most recent instruction. Ignore hazards
5527 between INSN and the first IGNORE instructions in HIST.
5528
5529 If INSN is null, return the worse-case number of nops for any
5530 instruction. */
bdaaa2e1 5531
71400594 5532static int
932d1a1b 5533nops_for_insn (int ignore, const struct mips_cl_insn *hist,
71400594
RS
5534 const struct mips_cl_insn *insn)
5535{
5536 int i, nops, tmp_nops;
bdaaa2e1 5537
71400594 5538 nops = 0;
932d1a1b 5539 for (i = ignore; i < MAX_DELAY_NOPS; i++)
65b02341 5540 {
91d6fa6a 5541 tmp_nops = insns_between (hist + i, insn) - i;
65b02341
RS
5542 if (tmp_nops > nops)
5543 nops = tmp_nops;
5544 }
7d8e00cf 5545
df58fc94 5546 if (mips_fix_vr4130 && !mips_opts.micromips)
7d8e00cf 5547 {
932d1a1b 5548 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
7d8e00cf
RS
5549 if (tmp_nops > nops)
5550 nops = tmp_nops;
5551 }
5552
df58fc94 5553 if (mips_fix_24k && !mips_opts.micromips)
15be625d 5554 {
932d1a1b 5555 tmp_nops = nops_for_24k (ignore, hist, insn);
15be625d
CM
5556 if (tmp_nops > nops)
5557 nops = tmp_nops;
5558 }
5559
71400594
RS
5560 return nops;
5561}
252b5132 5562
71400594 5563/* The variable arguments provide NUM_INSNS extra instructions that
91d6fa6a 5564 might be added to HIST. Return the largest number of nops that
932d1a1b
RS
5565 would be needed after the extended sequence, ignoring hazards
5566 in the first IGNORE instructions. */
252b5132 5567
71400594 5568static int
932d1a1b
RS
5569nops_for_sequence (int num_insns, int ignore,
5570 const struct mips_cl_insn *hist, ...)
71400594
RS
5571{
5572 va_list args;
5573 struct mips_cl_insn buffer[MAX_NOPS];
5574 struct mips_cl_insn *cursor;
5575 int nops;
5576
91d6fa6a 5577 va_start (args, hist);
71400594 5578 cursor = buffer + num_insns;
91d6fa6a 5579 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
71400594
RS
5580 while (cursor > buffer)
5581 *--cursor = *va_arg (args, const struct mips_cl_insn *);
5582
932d1a1b 5583 nops = nops_for_insn (ignore, buffer, NULL);
71400594
RS
5584 va_end (args);
5585 return nops;
5586}
252b5132 5587
71400594
RS
5588/* Like nops_for_insn, but if INSN is a branch, take into account the
5589 worst-case delay for the branch target. */
252b5132 5590
71400594 5591static int
932d1a1b 5592nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
71400594
RS
5593 const struct mips_cl_insn *insn)
5594{
5595 int nops, tmp_nops;
60b63b72 5596
932d1a1b 5597 nops = nops_for_insn (ignore, hist, insn);
11625dd8 5598 if (delayed_branch_p (insn))
71400594 5599 {
932d1a1b 5600 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
14fe068b 5601 hist, insn, get_delay_slot_nop (insn));
71400594
RS
5602 if (tmp_nops > nops)
5603 nops = tmp_nops;
5604 }
11625dd8 5605 else if (compact_branch_p (insn))
71400594 5606 {
932d1a1b 5607 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
71400594
RS
5608 if (tmp_nops > nops)
5609 nops = tmp_nops;
5610 }
5611 return nops;
5612}
5613
c67a084a
NC
5614/* Fix NOP issue: Replace nops by "or at,at,zero". */
5615
5616static void
5617fix_loongson2f_nop (struct mips_cl_insn * ip)
5618{
df58fc94 5619 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
5620 if (strcmp (ip->insn_mo->name, "nop") == 0)
5621 ip->insn_opcode = LOONGSON2F_NOP_INSN;
5622}
5623
5624/* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
5625 jr target pc &= 'hffff_ffff_cfff_ffff. */
5626
5627static void
5628fix_loongson2f_jump (struct mips_cl_insn * ip)
5629{
df58fc94 5630 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
5631 if (strcmp (ip->insn_mo->name, "j") == 0
5632 || strcmp (ip->insn_mo->name, "jr") == 0
5633 || strcmp (ip->insn_mo->name, "jalr") == 0)
5634 {
5635 int sreg;
5636 expressionS ep;
5637
5638 if (! mips_opts.at)
5639 return;
5640
df58fc94 5641 sreg = EXTRACT_OPERAND (0, RS, *ip);
c67a084a
NC
5642 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
5643 return;
5644
5645 ep.X_op = O_constant;
5646 ep.X_add_number = 0xcfff0000;
5647 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
5648 ep.X_add_number = 0xffff;
5649 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
5650 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
5651 }
5652}
5653
5654static void
5655fix_loongson2f (struct mips_cl_insn * ip)
5656{
5657 if (mips_fix_loongson2f_nop)
5658 fix_loongson2f_nop (ip);
5659
5660 if (mips_fix_loongson2f_jump)
5661 fix_loongson2f_jump (ip);
5662}
5663
a4e06468
RS
5664/* IP is a branch that has a delay slot, and we need to fill it
5665 automatically. Return true if we can do that by swapping IP
e407c74b
NC
5666 with the previous instruction.
5667 ADDRESS_EXPR is an operand of the instruction to be used with
5668 RELOC_TYPE. */
a4e06468
RS
5669
5670static bfd_boolean
e407c74b 5671can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 5672 bfd_reloc_code_real_type *reloc_type)
a4e06468 5673{
2b0c8b40 5674 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
a4e06468
RS
5675 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
5676
5677 /* -O2 and above is required for this optimization. */
5678 if (mips_optimize < 2)
5679 return FALSE;
5680
5681 /* If we have seen .set volatile or .set nomove, don't optimize. */
5682 if (mips_opts.nomove)
5683 return FALSE;
5684
5685 /* We can't swap if the previous instruction's position is fixed. */
5686 if (history[0].fixed_p)
5687 return FALSE;
5688
5689 /* If the previous previous insn was in a .set noreorder, we can't
5690 swap. Actually, the MIPS assembler will swap in this situation.
5691 However, gcc configured -with-gnu-as will generate code like
5692
5693 .set noreorder
5694 lw $4,XXX
5695 .set reorder
5696 INSN
5697 bne $4,$0,foo
5698
5699 in which we can not swap the bne and INSN. If gcc is not configured
5700 -with-gnu-as, it does not output the .set pseudo-ops. */
5701 if (history[1].noreorder_p)
5702 return FALSE;
5703
87333bb7
MR
5704 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
5705 This means that the previous instruction was a 4-byte one anyhow. */
a4e06468
RS
5706 if (mips_opts.mips16 && history[0].fixp[0])
5707 return FALSE;
5708
5709 /* If the branch is itself the target of a branch, we can not swap.
5710 We cheat on this; all we check for is whether there is a label on
5711 this instruction. If there are any branches to anything other than
5712 a label, users must use .set noreorder. */
5713 if (seg_info (now_seg)->label_list)
5714 return FALSE;
5715
5716 /* If the previous instruction is in a variant frag other than this
2309ddf2 5717 branch's one, we cannot do the swap. This does not apply to
9301f9c3
MR
5718 MIPS16 code, which uses variant frags for different purposes. */
5719 if (!mips_opts.mips16
a4e06468
RS
5720 && history[0].frag
5721 && history[0].frag->fr_type == rs_machine_dependent)
5722 return FALSE;
5723
bcd530a7
RS
5724 /* We do not swap with instructions that cannot architecturally
5725 be placed in a branch delay slot, such as SYNC or ERET. We
5726 also refrain from swapping with a trap instruction, since it
5727 complicates trap handlers to have the trap instruction be in
5728 a delay slot. */
a4e06468 5729 prev_pinfo = history[0].insn_mo->pinfo;
bcd530a7 5730 if (prev_pinfo & INSN_NO_DELAY_SLOT)
a4e06468
RS
5731 return FALSE;
5732
5733 /* Check for conflicts between the branch and the instructions
5734 before the candidate delay slot. */
5735 if (nops_for_insn (0, history + 1, ip) > 0)
5736 return FALSE;
5737
5738 /* Check for conflicts between the swapped sequence and the
5739 target of the branch. */
5740 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
5741 return FALSE;
5742
5743 /* If the branch reads a register that the previous
5744 instruction sets, we can not swap. */
5745 gpr_read = gpr_read_mask (ip);
5746 prev_gpr_write = gpr_write_mask (&history[0]);
5747 if (gpr_read & prev_gpr_write)
5748 return FALSE;
5749
5750 /* If the branch writes a register that the previous
5751 instruction sets, we can not swap. */
5752 gpr_write = gpr_write_mask (ip);
5753 if (gpr_write & prev_gpr_write)
5754 return FALSE;
5755
5756 /* If the branch writes a register that the previous
5757 instruction reads, we can not swap. */
5758 prev_gpr_read = gpr_read_mask (&history[0]);
5759 if (gpr_write & prev_gpr_read)
5760 return FALSE;
5761
5762 /* If one instruction sets a condition code and the
5763 other one uses a condition code, we can not swap. */
5764 pinfo = ip->insn_mo->pinfo;
5765 if ((pinfo & INSN_READ_COND_CODE)
5766 && (prev_pinfo & INSN_WRITE_COND_CODE))
5767 return FALSE;
5768 if ((pinfo & INSN_WRITE_COND_CODE)
5769 && (prev_pinfo & INSN_READ_COND_CODE))
5770 return FALSE;
5771
5772 /* If the previous instruction uses the PC, we can not swap. */
2b0c8b40 5773 prev_pinfo2 = history[0].insn_mo->pinfo2;
26545944 5774 if (prev_pinfo2 & INSN2_READ_PC)
2b0c8b40 5775 return FALSE;
a4e06468 5776
df58fc94
RS
5777 /* If the previous instruction has an incorrect size for a fixed
5778 branch delay slot in microMIPS mode, we cannot swap. */
2309ddf2
MR
5779 pinfo2 = ip->insn_mo->pinfo2;
5780 if (mips_opts.micromips
5781 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
5782 && insn_length (history) != 2)
5783 return FALSE;
5784 if (mips_opts.micromips
5785 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
5786 && insn_length (history) != 4)
5787 return FALSE;
5788
e407c74b
NC
5789 /* On R5900 short loops need to be fixed by inserting a nop in
5790 the branch delay slots.
5791 A short loop can be terminated too early. */
5792 if (mips_opts.arch == CPU_R5900
5793 /* Check if instruction has a parameter, ignore "j $31". */
5794 && (address_expr != NULL)
5795 /* Parameter must be 16 bit. */
5796 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
5797 /* Branch to same segment. */
5798 && (S_GET_SEGMENT(address_expr->X_add_symbol) == now_seg)
5799 /* Branch to same code fragment. */
5800 && (symbol_get_frag(address_expr->X_add_symbol) == frag_now)
5801 /* Can only calculate branch offset if value is known. */
5802 && symbol_constant_p(address_expr->X_add_symbol)
5803 /* Check if branch is really conditional. */
5804 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
5805 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
5806 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
5807 {
5808 int distance;
5809 /* Check if loop is shorter than 6 instructions including
5810 branch and delay slot. */
5811 distance = frag_now_fix() - S_GET_VALUE(address_expr->X_add_symbol);
5812 if (distance <= 20)
5813 {
5814 int i;
5815 int rv;
5816
5817 rv = FALSE;
5818 /* When the loop includes branches or jumps,
5819 it is not a short loop. */
5820 for (i = 0; i < (distance / 4); i++)
5821 {
5822 if ((history[i].cleared_p)
5823 || delayed_branch_p(&history[i]))
5824 {
5825 rv = TRUE;
5826 break;
5827 }
5828 }
5829 if (rv == FALSE)
5830 {
5831 /* Insert nop after branch to fix short loop. */
5832 return FALSE;
5833 }
5834 }
5835 }
5836
a4e06468
RS
5837 return TRUE;
5838}
5839
e407c74b
NC
5840/* Decide how we should add IP to the instruction stream.
5841 ADDRESS_EXPR is an operand of the instruction to be used with
5842 RELOC_TYPE. */
a4e06468
RS
5843
5844static enum append_method
e407c74b 5845get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 5846 bfd_reloc_code_real_type *reloc_type)
a4e06468 5847{
a4e06468
RS
5848 /* The relaxed version of a macro sequence must be inherently
5849 hazard-free. */
5850 if (mips_relax.sequence == 2)
5851 return APPEND_ADD;
5852
5853 /* We must not dabble with instructions in a ".set norerorder" block. */
5854 if (mips_opts.noreorder)
5855 return APPEND_ADD;
5856
5857 /* Otherwise, it's our responsibility to fill branch delay slots. */
11625dd8 5858 if (delayed_branch_p (ip))
a4e06468 5859 {
e407c74b
NC
5860 if (!branch_likely_p (ip)
5861 && can_swap_branch_p (ip, address_expr, reloc_type))
a4e06468
RS
5862 return APPEND_SWAP;
5863
5864 if (mips_opts.mips16
5865 && ISA_SUPPORTS_MIPS16E
fc76e730 5866 && gpr_read_mask (ip) != 0)
a4e06468
RS
5867 return APPEND_ADD_COMPACT;
5868
5869 return APPEND_ADD_WITH_NOP;
5870 }
5871
a4e06468
RS
5872 return APPEND_ADD;
5873}
5874
ceb94aa5
RS
5875/* IP is a MIPS16 instruction whose opcode we have just changed.
5876 Point IP->insn_mo to the new opcode's definition. */
5877
5878static void
5879find_altered_mips16_opcode (struct mips_cl_insn *ip)
5880{
5881 const struct mips_opcode *mo, *end;
5882
5883 end = &mips16_opcodes[bfd_mips16_num_opcodes];
5884 for (mo = ip->insn_mo; mo < end; mo++)
5885 if ((ip->insn_opcode & mo->mask) == mo->match)
5886 {
5887 ip->insn_mo = mo;
5888 return;
5889 }
5890 abort ();
5891}
5892
df58fc94
RS
5893/* For microMIPS macros, we need to generate a local number label
5894 as the target of branches. */
5895#define MICROMIPS_LABEL_CHAR '\037'
5896static unsigned long micromips_target_label;
5897static char micromips_target_name[32];
5898
5899static char *
5900micromips_label_name (void)
5901{
5902 char *p = micromips_target_name;
5903 char symbol_name_temporary[24];
5904 unsigned long l;
5905 int i;
5906
5907 if (*p)
5908 return p;
5909
5910 i = 0;
5911 l = micromips_target_label;
5912#ifdef LOCAL_LABEL_PREFIX
5913 *p++ = LOCAL_LABEL_PREFIX;
5914#endif
5915 *p++ = 'L';
5916 *p++ = MICROMIPS_LABEL_CHAR;
5917 do
5918 {
5919 symbol_name_temporary[i++] = l % 10 + '0';
5920 l /= 10;
5921 }
5922 while (l != 0);
5923 while (i > 0)
5924 *p++ = symbol_name_temporary[--i];
5925 *p = '\0';
5926
5927 return micromips_target_name;
5928}
5929
5930static void
5931micromips_label_expr (expressionS *label_expr)
5932{
5933 label_expr->X_op = O_symbol;
5934 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
5935 label_expr->X_add_number = 0;
5936}
5937
5938static void
5939micromips_label_inc (void)
5940{
5941 micromips_target_label++;
5942 *micromips_target_name = '\0';
5943}
5944
5945static void
5946micromips_add_label (void)
5947{
5948 symbolS *s;
5949
5950 s = colon (micromips_label_name ());
5951 micromips_label_inc ();
f3ded42a 5952 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
df58fc94
RS
5953}
5954
5955/* If assembling microMIPS code, then return the microMIPS reloc
5956 corresponding to the requested one if any. Otherwise return
5957 the reloc unchanged. */
5958
5959static bfd_reloc_code_real_type
5960micromips_map_reloc (bfd_reloc_code_real_type reloc)
5961{
5962 static const bfd_reloc_code_real_type relocs[][2] =
5963 {
5964 /* Keep sorted incrementally by the left-hand key. */
5965 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
5966 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
5967 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
5968 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
5969 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
5970 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
5971 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
5972 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
5973 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
5974 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
5975 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
5976 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
5977 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
5978 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
5979 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
5980 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
5981 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
5982 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
5983 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
5984 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
5985 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
5986 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
5987 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
5988 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
5989 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
5990 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
5991 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
5992 };
5993 bfd_reloc_code_real_type r;
5994 size_t i;
5995
5996 if (!mips_opts.micromips)
5997 return reloc;
5998 for (i = 0; i < ARRAY_SIZE (relocs); i++)
5999 {
6000 r = relocs[i][0];
6001 if (r > reloc)
6002 return reloc;
6003 if (r == reloc)
6004 return relocs[i][1];
6005 }
6006 return reloc;
6007}
6008
b886a2ab
RS
6009/* Try to resolve relocation RELOC against constant OPERAND at assembly time.
6010 Return true on success, storing the resolved value in RESULT. */
6011
6012static bfd_boolean
6013calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
6014 offsetT *result)
6015{
6016 switch (reloc)
6017 {
6018 case BFD_RELOC_MIPS_HIGHEST:
6019 case BFD_RELOC_MICROMIPS_HIGHEST:
6020 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
6021 return TRUE;
6022
6023 case BFD_RELOC_MIPS_HIGHER:
6024 case BFD_RELOC_MICROMIPS_HIGHER:
6025 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
6026 return TRUE;
6027
6028 case BFD_RELOC_HI16_S:
6029 case BFD_RELOC_MICROMIPS_HI16_S:
6030 case BFD_RELOC_MIPS16_HI16_S:
6031 *result = ((operand + 0x8000) >> 16) & 0xffff;
6032 return TRUE;
6033
6034 case BFD_RELOC_HI16:
6035 case BFD_RELOC_MICROMIPS_HI16:
6036 case BFD_RELOC_MIPS16_HI16:
6037 *result = (operand >> 16) & 0xffff;
6038 return TRUE;
6039
6040 case BFD_RELOC_LO16:
6041 case BFD_RELOC_MICROMIPS_LO16:
6042 case BFD_RELOC_MIPS16_LO16:
6043 *result = operand & 0xffff;
6044 return TRUE;
6045
6046 case BFD_RELOC_UNUSED:
6047 *result = operand;
6048 return TRUE;
6049
6050 default:
6051 return FALSE;
6052 }
6053}
6054
71400594
RS
6055/* Output an instruction. IP is the instruction information.
6056 ADDRESS_EXPR is an operand of the instruction to be used with
df58fc94
RS
6057 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
6058 a macro expansion. */
71400594
RS
6059
6060static void
6061append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
df58fc94 6062 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
71400594 6063{
14fe068b 6064 unsigned long prev_pinfo2, pinfo;
71400594 6065 bfd_boolean relaxed_branch = FALSE;
a4e06468 6066 enum append_method method;
2309ddf2 6067 bfd_boolean relax32;
2b0c8b40 6068 int branch_disp;
71400594 6069
2309ddf2 6070 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
c67a084a
NC
6071 fix_loongson2f (ip);
6072
738f4d98 6073 file_ase_mips16 |= mips_opts.mips16;
df58fc94 6074 file_ase_micromips |= mips_opts.micromips;
738f4d98 6075
df58fc94 6076 prev_pinfo2 = history[0].insn_mo->pinfo2;
71400594 6077 pinfo = ip->insn_mo->pinfo;
df58fc94
RS
6078
6079 if (mips_opts.micromips
6080 && !expansionp
6081 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
6082 && micromips_insn_length (ip->insn_mo) != 2)
6083 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
6084 && micromips_insn_length (ip->insn_mo) != 4)))
6085 as_warn (_("Wrong size instruction in a %u-bit branch delay slot"),
6086 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
71400594 6087
15be625d
CM
6088 if (address_expr == NULL)
6089 ip->complete_p = 1;
b886a2ab
RS
6090 else if (reloc_type[0] <= BFD_RELOC_UNUSED
6091 && reloc_type[1] == BFD_RELOC_UNUSED
6092 && reloc_type[2] == BFD_RELOC_UNUSED
15be625d
CM
6093 && address_expr->X_op == O_constant)
6094 {
15be625d
CM
6095 switch (*reloc_type)
6096 {
15be625d 6097 case BFD_RELOC_MIPS_JMP:
df58fc94
RS
6098 {
6099 int shift;
6100
6101 shift = mips_opts.micromips ? 1 : 2;
6102 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
6103 as_bad (_("jump to misaligned address (0x%lx)"),
6104 (unsigned long) address_expr->X_add_number);
6105 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
6106 & 0x3ffffff);
335574df 6107 ip->complete_p = 1;
df58fc94 6108 }
15be625d
CM
6109 break;
6110
6111 case BFD_RELOC_MIPS16_JMP:
6112 if ((address_expr->X_add_number & 3) != 0)
6113 as_bad (_("jump to misaligned address (0x%lx)"),
6114 (unsigned long) address_expr->X_add_number);
6115 ip->insn_opcode |=
6116 (((address_expr->X_add_number & 0x7c0000) << 3)
6117 | ((address_expr->X_add_number & 0xf800000) >> 7)
6118 | ((address_expr->X_add_number & 0x3fffc) >> 2));
335574df 6119 ip->complete_p = 1;
15be625d
CM
6120 break;
6121
6122 case BFD_RELOC_16_PCREL_S2:
df58fc94
RS
6123 {
6124 int shift;
6125
6126 shift = mips_opts.micromips ? 1 : 2;
6127 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
6128 as_bad (_("branch to misaligned address (0x%lx)"),
6129 (unsigned long) address_expr->X_add_number);
6130 if (!mips_relax_branch)
6131 {
6132 if ((address_expr->X_add_number + (1 << (shift + 15)))
6133 & ~((1 << (shift + 16)) - 1))
6134 as_bad (_("branch address range overflow (0x%lx)"),
6135 (unsigned long) address_expr->X_add_number);
6136 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
6137 & 0xffff);
6138 }
df58fc94 6139 }
15be625d
CM
6140 break;
6141
6142 default:
b886a2ab
RS
6143 {
6144 offsetT value;
6145
6146 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
6147 &value))
6148 {
6149 ip->insn_opcode |= value & 0xffff;
6150 ip->complete_p = 1;
6151 }
6152 }
6153 break;
6154 }
15be625d
CM
6155 }
6156
71400594
RS
6157 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
6158 {
6159 /* There are a lot of optimizations we could do that we don't.
6160 In particular, we do not, in general, reorder instructions.
6161 If you use gcc with optimization, it will reorder
6162 instructions and generally do much more optimization then we
6163 do here; repeating all that work in the assembler would only
6164 benefit hand written assembly code, and does not seem worth
6165 it. */
6166 int nops = (mips_optimize == 0
932d1a1b
RS
6167 ? nops_for_insn (0, history, NULL)
6168 : nops_for_insn_or_target (0, history, ip));
71400594 6169 if (nops > 0)
252b5132
RH
6170 {
6171 fragS *old_frag;
6172 unsigned long old_frag_offset;
6173 int i;
252b5132
RH
6174
6175 old_frag = frag_now;
6176 old_frag_offset = frag_now_fix ();
6177
6178 for (i = 0; i < nops; i++)
14fe068b
RS
6179 add_fixed_insn (NOP_INSN);
6180 insert_into_history (0, nops, NOP_INSN);
252b5132
RH
6181
6182 if (listing)
6183 {
6184 listing_prev_line ();
6185 /* We may be at the start of a variant frag. In case we
6186 are, make sure there is enough space for the frag
6187 after the frags created by listing_prev_line. The
6188 argument to frag_grow here must be at least as large
6189 as the argument to all other calls to frag_grow in
6190 this file. We don't have to worry about being in the
6191 middle of a variant frag, because the variants insert
6192 all needed nop instructions themselves. */
6193 frag_grow (40);
6194 }
6195
462427c4 6196 mips_move_text_labels ();
252b5132
RH
6197
6198#ifndef NO_ECOFF_DEBUGGING
6199 if (ECOFF_DEBUGGING)
6200 ecoff_fix_loc (old_frag, old_frag_offset);
6201#endif
6202 }
71400594
RS
6203 }
6204 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
6205 {
932d1a1b
RS
6206 int nops;
6207
6208 /* Work out how many nops in prev_nop_frag are needed by IP,
6209 ignoring hazards generated by the first prev_nop_frag_since
6210 instructions. */
6211 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
9c2799c2 6212 gas_assert (nops <= prev_nop_frag_holds);
252b5132 6213
71400594
RS
6214 /* Enforce NOPS as a minimum. */
6215 if (nops > prev_nop_frag_required)
6216 prev_nop_frag_required = nops;
252b5132 6217
71400594
RS
6218 if (prev_nop_frag_holds == prev_nop_frag_required)
6219 {
6220 /* Settle for the current number of nops. Update the history
6221 accordingly (for the benefit of any future .set reorder code). */
6222 prev_nop_frag = NULL;
6223 insert_into_history (prev_nop_frag_since,
6224 prev_nop_frag_holds, NOP_INSN);
6225 }
6226 else
6227 {
6228 /* Allow this instruction to replace one of the nops that was
6229 tentatively added to prev_nop_frag. */
df58fc94 6230 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
71400594
RS
6231 prev_nop_frag_holds--;
6232 prev_nop_frag_since++;
252b5132
RH
6233 }
6234 }
6235
e407c74b 6236 method = get_append_method (ip, address_expr, reloc_type);
2b0c8b40 6237 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
a4e06468 6238
e410add4
RS
6239 dwarf2_emit_insn (0);
6240 /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
6241 so "move" the instruction address accordingly.
6242
6243 Also, it doesn't seem appropriate for the assembler to reorder .loc
6244 entries. If this instruction is a branch that we are going to swap
6245 with the previous instruction, the two instructions should be
6246 treated as a unit, and the debug information for both instructions
6247 should refer to the start of the branch sequence. Using the
6248 current position is certainly wrong when swapping a 32-bit branch
6249 and a 16-bit delay slot, since the current position would then be
6250 in the middle of a branch. */
6251 dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
58e2ea4d 6252
df58fc94
RS
6253 relax32 = (mips_relax_branch
6254 /* Don't try branch relaxation within .set nomacro, or within
6255 .set noat if we use $at for PIC computations. If it turns
6256 out that the branch was out-of-range, we'll get an error. */
6257 && !mips_opts.warn_about_macros
6258 && (mips_opts.at || mips_pic == NO_PIC)
3bf0dbfb
MR
6259 /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
6260 as they have no complementing branches. */
6261 && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
df58fc94
RS
6262
6263 if (!HAVE_CODE_COMPRESSION
6264 && address_expr
6265 && relax32
0b25d3e6 6266 && *reloc_type == BFD_RELOC_16_PCREL_S2
11625dd8 6267 && delayed_branch_p (ip))
4a6a3df4 6268 {
895921c9 6269 relaxed_branch = TRUE;
1e915849
RS
6270 add_relaxed_insn (ip, (relaxed_branch_length
6271 (NULL, NULL,
11625dd8
RS
6272 uncond_branch_p (ip) ? -1
6273 : branch_likely_p (ip) ? 1
1e915849
RS
6274 : 0)), 4,
6275 RELAX_BRANCH_ENCODE
66b3e8da 6276 (AT,
11625dd8
RS
6277 uncond_branch_p (ip),
6278 branch_likely_p (ip),
1e915849
RS
6279 pinfo & INSN_WRITE_GPR_31,
6280 0),
6281 address_expr->X_add_symbol,
6282 address_expr->X_add_number);
4a6a3df4
AO
6283 *reloc_type = BFD_RELOC_UNUSED;
6284 }
df58fc94
RS
6285 else if (mips_opts.micromips
6286 && address_expr
6287 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
6288 || *reloc_type > BFD_RELOC_UNUSED)
40209cad
MR
6289 && (delayed_branch_p (ip) || compact_branch_p (ip))
6290 /* Don't try branch relaxation when users specify
6291 16-bit/32-bit instructions. */
6292 && !forced_insn_length)
df58fc94
RS
6293 {
6294 bfd_boolean relax16 = *reloc_type > BFD_RELOC_UNUSED;
6295 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
11625dd8
RS
6296 int uncond = uncond_branch_p (ip) ? -1 : 0;
6297 int compact = compact_branch_p (ip);
df58fc94
RS
6298 int al = pinfo & INSN_WRITE_GPR_31;
6299 int length32;
6300
6301 gas_assert (address_expr != NULL);
6302 gas_assert (!mips_relax.sequence);
6303
2b0c8b40 6304 relaxed_branch = TRUE;
df58fc94
RS
6305 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
6306 add_relaxed_insn (ip, relax32 ? length32 : 4, relax16 ? 2 : 4,
40209cad
MR
6307 RELAX_MICROMIPS_ENCODE (type, AT, uncond, compact, al,
6308 relax32, 0, 0),
df58fc94
RS
6309 address_expr->X_add_symbol,
6310 address_expr->X_add_number);
6311 *reloc_type = BFD_RELOC_UNUSED;
6312 }
6313 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
6314 {
6315 /* We need to set up a variant frag. */
df58fc94 6316 gas_assert (address_expr != NULL);
1e915849
RS
6317 add_relaxed_insn (ip, 4, 0,
6318 RELAX_MIPS16_ENCODE
6319 (*reloc_type - BFD_RELOC_UNUSED,
df58fc94 6320 forced_insn_length == 2, forced_insn_length == 4,
11625dd8 6321 delayed_branch_p (&history[0]),
1e915849
RS
6322 history[0].mips16_absolute_jump_p),
6323 make_expr_symbol (address_expr), 0);
252b5132 6324 }
5c04167a 6325 else if (mips_opts.mips16 && insn_length (ip) == 2)
9497f5ac 6326 {
11625dd8 6327 if (!delayed_branch_p (ip))
b8ee1a6e
DU
6328 /* Make sure there is enough room to swap this instruction with
6329 a following jump instruction. */
6330 frag_grow (6);
1e915849 6331 add_fixed_insn (ip);
252b5132
RH
6332 }
6333 else
6334 {
6335 if (mips_opts.mips16
6336 && mips_opts.noreorder
11625dd8 6337 && delayed_branch_p (&history[0]))
252b5132
RH
6338 as_warn (_("extended instruction in delay slot"));
6339
4d7206a2
RS
6340 if (mips_relax.sequence)
6341 {
6342 /* If we've reached the end of this frag, turn it into a variant
6343 frag and record the information for the instructions we've
6344 written so far. */
6345 if (frag_room () < 4)
6346 relax_close_frag ();
df58fc94 6347 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4d7206a2
RS
6348 }
6349
584892a6 6350 if (mips_relax.sequence != 2)
df58fc94
RS
6351 {
6352 if (mips_macro_warning.first_insn_sizes[0] == 0)
6353 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
6354 mips_macro_warning.sizes[0] += insn_length (ip);
6355 mips_macro_warning.insns[0]++;
6356 }
584892a6 6357 if (mips_relax.sequence != 1)
df58fc94
RS
6358 {
6359 if (mips_macro_warning.first_insn_sizes[1] == 0)
6360 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
6361 mips_macro_warning.sizes[1] += insn_length (ip);
6362 mips_macro_warning.insns[1]++;
6363 }
584892a6 6364
1e915849
RS
6365 if (mips_opts.mips16)
6366 {
6367 ip->fixed_p = 1;
6368 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
6369 }
6370 add_fixed_insn (ip);
252b5132
RH
6371 }
6372
9fe77896 6373 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
252b5132 6374 {
df58fc94 6375 bfd_reloc_code_real_type final_type[3];
2309ddf2 6376 reloc_howto_type *howto0;
9fe77896
RS
6377 reloc_howto_type *howto;
6378 int i;
34ce925e 6379
df58fc94
RS
6380 /* Perform any necessary conversion to microMIPS relocations
6381 and find out how many relocations there actually are. */
6382 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
6383 final_type[i] = micromips_map_reloc (reloc_type[i]);
6384
9fe77896
RS
6385 /* In a compound relocation, it is the final (outermost)
6386 operator that determines the relocated field. */
2309ddf2 6387 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
e8044f35
RS
6388 if (!howto)
6389 abort ();
2309ddf2
MR
6390
6391 if (i > 1)
6392 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
9fe77896
RS
6393 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
6394 bfd_get_reloc_size (howto),
6395 address_expr,
2309ddf2
MR
6396 howto0 && howto0->pc_relative,
6397 final_type[0]);
9fe77896
RS
6398
6399 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
2309ddf2 6400 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
9fe77896
RS
6401 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
6402
6403 /* These relocations can have an addend that won't fit in
6404 4 octets for 64bit assembly. */
6405 if (HAVE_64BIT_GPRS
6406 && ! howto->partial_inplace
6407 && (reloc_type[0] == BFD_RELOC_16
6408 || reloc_type[0] == BFD_RELOC_32
6409 || reloc_type[0] == BFD_RELOC_MIPS_JMP
6410 || reloc_type[0] == BFD_RELOC_GPREL16
6411 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
6412 || reloc_type[0] == BFD_RELOC_GPREL32
6413 || reloc_type[0] == BFD_RELOC_64
6414 || reloc_type[0] == BFD_RELOC_CTOR
6415 || reloc_type[0] == BFD_RELOC_MIPS_SUB
6416 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
6417 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
6418 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
6419 || reloc_type[0] == BFD_RELOC_MIPS_REL16
6420 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
6421 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
6422 || hi16_reloc_p (reloc_type[0])
6423 || lo16_reloc_p (reloc_type[0])))
6424 ip->fixp[0]->fx_no_overflow = 1;
6425
ddaf2c41
MR
6426 /* These relocations can have an addend that won't fit in 2 octets. */
6427 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
6428 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
6429 ip->fixp[0]->fx_no_overflow = 1;
6430
9fe77896
RS
6431 if (mips_relax.sequence)
6432 {
6433 if (mips_relax.first_fixup == 0)
6434 mips_relax.first_fixup = ip->fixp[0];
6435 }
6436 else if (reloc_needs_lo_p (*reloc_type))
6437 {
6438 struct mips_hi_fixup *hi_fixup;
6439
6440 /* Reuse the last entry if it already has a matching %lo. */
6441 hi_fixup = mips_hi_fixup_list;
6442 if (hi_fixup == 0
6443 || !fixup_has_matching_lo_p (hi_fixup->fixp))
4d7206a2 6444 {
9fe77896
RS
6445 hi_fixup = ((struct mips_hi_fixup *)
6446 xmalloc (sizeof (struct mips_hi_fixup)));
6447 hi_fixup->next = mips_hi_fixup_list;
6448 mips_hi_fixup_list = hi_fixup;
4d7206a2 6449 }
9fe77896
RS
6450 hi_fixup->fixp = ip->fixp[0];
6451 hi_fixup->seg = now_seg;
6452 }
252b5132 6453
9fe77896
RS
6454 /* Add fixups for the second and third relocations, if given.
6455 Note that the ABI allows the second relocation to be
6456 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
6457 moment we only use RSS_UNDEF, but we could add support
6458 for the others if it ever becomes necessary. */
6459 for (i = 1; i < 3; i++)
6460 if (reloc_type[i] != BFD_RELOC_UNUSED)
6461 {
6462 ip->fixp[i] = fix_new (ip->frag, ip->where,
6463 ip->fixp[0]->fx_size, NULL, 0,
df58fc94 6464 FALSE, final_type[i]);
f6688943 6465
9fe77896
RS
6466 /* Use fx_tcbit to mark compound relocs. */
6467 ip->fixp[0]->fx_tcbit = 1;
6468 ip->fixp[i]->fx_tcbit = 1;
6469 }
252b5132 6470 }
1e915849 6471 install_insn (ip);
252b5132
RH
6472
6473 /* Update the register mask information. */
4c260379
RS
6474 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
6475 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
252b5132 6476
a4e06468 6477 switch (method)
252b5132 6478 {
a4e06468
RS
6479 case APPEND_ADD:
6480 insert_into_history (0, 1, ip);
6481 break;
6482
6483 case APPEND_ADD_WITH_NOP:
14fe068b
RS
6484 {
6485 struct mips_cl_insn *nop;
6486
6487 insert_into_history (0, 1, ip);
6488 nop = get_delay_slot_nop (ip);
6489 add_fixed_insn (nop);
6490 insert_into_history (0, 1, nop);
6491 if (mips_relax.sequence)
6492 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
6493 }
a4e06468
RS
6494 break;
6495
6496 case APPEND_ADD_COMPACT:
6497 /* Convert MIPS16 jr/jalr into a "compact" jump. */
6498 gas_assert (mips_opts.mips16);
6499 ip->insn_opcode |= 0x0080;
6500 find_altered_mips16_opcode (ip);
6501 install_insn (ip);
6502 insert_into_history (0, 1, ip);
6503 break;
6504
6505 case APPEND_SWAP:
6506 {
6507 struct mips_cl_insn delay = history[0];
6508 if (mips_opts.mips16)
6509 {
6510 know (delay.frag == ip->frag);
6511 move_insn (ip, delay.frag, delay.where);
6512 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
6513 }
464ab0e5 6514 else if (relaxed_branch || delay.frag != ip->frag)
a4e06468
RS
6515 {
6516 /* Add the delay slot instruction to the end of the
6517 current frag and shrink the fixed part of the
6518 original frag. If the branch occupies the tail of
6519 the latter, move it backwards to cover the gap. */
2b0c8b40 6520 delay.frag->fr_fix -= branch_disp;
a4e06468 6521 if (delay.frag == ip->frag)
2b0c8b40 6522 move_insn (ip, ip->frag, ip->where - branch_disp);
a4e06468
RS
6523 add_fixed_insn (&delay);
6524 }
6525 else
6526 {
2b0c8b40
MR
6527 move_insn (&delay, ip->frag,
6528 ip->where - branch_disp + insn_length (ip));
a4e06468
RS
6529 move_insn (ip, history[0].frag, history[0].where);
6530 }
6531 history[0] = *ip;
6532 delay.fixed_p = 1;
6533 insert_into_history (0, 1, &delay);
6534 }
6535 break;
252b5132
RH
6536 }
6537
13408f1e 6538 /* If we have just completed an unconditional branch, clear the history. */
11625dd8
RS
6539 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
6540 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
e407c74b
NC
6541 {
6542 unsigned int i;
6543
79850f26 6544 mips_no_prev_insn ();
13408f1e 6545
e407c74b 6546 for (i = 0; i < ARRAY_SIZE (history); i++)
79850f26 6547 history[i].cleared_p = 1;
e407c74b
NC
6548 }
6549
df58fc94
RS
6550 /* We need to emit a label at the end of branch-likely macros. */
6551 if (emit_branch_likely_macro)
6552 {
6553 emit_branch_likely_macro = FALSE;
6554 micromips_add_label ();
6555 }
6556
252b5132
RH
6557 /* We just output an insn, so the next one doesn't have a label. */
6558 mips_clear_insn_labels ();
252b5132
RH
6559}
6560
e407c74b
NC
6561/* Forget that there was any previous instruction or label.
6562 When BRANCH is true, the branch history is also flushed. */
252b5132
RH
6563
6564static void
7d10b47d 6565mips_no_prev_insn (void)
252b5132 6566{
7d10b47d
RS
6567 prev_nop_frag = NULL;
6568 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
6569 mips_clear_insn_labels ();
6570}
6571
7d10b47d
RS
6572/* This function must be called before we emit something other than
6573 instructions. It is like mips_no_prev_insn except that it inserts
6574 any NOPS that might be needed by previous instructions. */
252b5132 6575
7d10b47d
RS
6576void
6577mips_emit_delays (void)
252b5132
RH
6578{
6579 if (! mips_opts.noreorder)
6580 {
932d1a1b 6581 int nops = nops_for_insn (0, history, NULL);
252b5132
RH
6582 if (nops > 0)
6583 {
7d10b47d
RS
6584 while (nops-- > 0)
6585 add_fixed_insn (NOP_INSN);
462427c4 6586 mips_move_text_labels ();
7d10b47d
RS
6587 }
6588 }
6589 mips_no_prev_insn ();
6590}
6591
6592/* Start a (possibly nested) noreorder block. */
6593
6594static void
6595start_noreorder (void)
6596{
6597 if (mips_opts.noreorder == 0)
6598 {
6599 unsigned int i;
6600 int nops;
6601
6602 /* None of the instructions before the .set noreorder can be moved. */
6603 for (i = 0; i < ARRAY_SIZE (history); i++)
6604 history[i].fixed_p = 1;
6605
6606 /* Insert any nops that might be needed between the .set noreorder
6607 block and the previous instructions. We will later remove any
6608 nops that turn out not to be needed. */
932d1a1b 6609 nops = nops_for_insn (0, history, NULL);
7d10b47d
RS
6610 if (nops > 0)
6611 {
6612 if (mips_optimize != 0)
252b5132
RH
6613 {
6614 /* Record the frag which holds the nop instructions, so
6615 that we can remove them if we don't need them. */
df58fc94 6616 frag_grow (nops * NOP_INSN_SIZE);
252b5132
RH
6617 prev_nop_frag = frag_now;
6618 prev_nop_frag_holds = nops;
6619 prev_nop_frag_required = 0;
6620 prev_nop_frag_since = 0;
6621 }
6622
6623 for (; nops > 0; --nops)
1e915849 6624 add_fixed_insn (NOP_INSN);
252b5132 6625
7d10b47d
RS
6626 /* Move on to a new frag, so that it is safe to simply
6627 decrease the size of prev_nop_frag. */
6628 frag_wane (frag_now);
6629 frag_new (0);
462427c4 6630 mips_move_text_labels ();
252b5132 6631 }
df58fc94 6632 mips_mark_labels ();
7d10b47d 6633 mips_clear_insn_labels ();
252b5132 6634 }
7d10b47d
RS
6635 mips_opts.noreorder++;
6636 mips_any_noreorder = 1;
6637}
252b5132 6638
7d10b47d 6639/* End a nested noreorder block. */
252b5132 6640
7d10b47d
RS
6641static void
6642end_noreorder (void)
6643{
6644 mips_opts.noreorder--;
6645 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
6646 {
6647 /* Commit to inserting prev_nop_frag_required nops and go back to
6648 handling nop insertion the .set reorder way. */
6649 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
df58fc94 6650 * NOP_INSN_SIZE);
7d10b47d
RS
6651 insert_into_history (prev_nop_frag_since,
6652 prev_nop_frag_required, NOP_INSN);
6653 prev_nop_frag = NULL;
6654 }
252b5132
RH
6655}
6656
584892a6
RS
6657/* Set up global variables for the start of a new macro. */
6658
6659static void
6660macro_start (void)
6661{
6662 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
df58fc94
RS
6663 memset (&mips_macro_warning.first_insn_sizes, 0,
6664 sizeof (mips_macro_warning.first_insn_sizes));
6665 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
584892a6 6666 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
11625dd8 6667 && delayed_branch_p (&history[0]));
df58fc94
RS
6668 switch (history[0].insn_mo->pinfo2
6669 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
6670 {
6671 case INSN2_BRANCH_DELAY_32BIT:
6672 mips_macro_warning.delay_slot_length = 4;
6673 break;
6674 case INSN2_BRANCH_DELAY_16BIT:
6675 mips_macro_warning.delay_slot_length = 2;
6676 break;
6677 default:
6678 mips_macro_warning.delay_slot_length = 0;
6679 break;
6680 }
6681 mips_macro_warning.first_frag = NULL;
584892a6
RS
6682}
6683
df58fc94
RS
6684/* Given that a macro is longer than one instruction or of the wrong size,
6685 return the appropriate warning for it. Return null if no warning is
6686 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
6687 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
6688 and RELAX_NOMACRO. */
584892a6
RS
6689
6690static const char *
6691macro_warning (relax_substateT subtype)
6692{
6693 if (subtype & RELAX_DELAY_SLOT)
6694 return _("Macro instruction expanded into multiple instructions"
6695 " in a branch delay slot");
6696 else if (subtype & RELAX_NOMACRO)
6697 return _("Macro instruction expanded into multiple instructions");
df58fc94
RS
6698 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
6699 | RELAX_DELAY_SLOT_SIZE_SECOND))
6700 return ((subtype & RELAX_DELAY_SLOT_16BIT)
6701 ? _("Macro instruction expanded into a wrong size instruction"
6702 " in a 16-bit branch delay slot")
6703 : _("Macro instruction expanded into a wrong size instruction"
6704 " in a 32-bit branch delay slot"));
584892a6
RS
6705 else
6706 return 0;
6707}
6708
6709/* Finish up a macro. Emit warnings as appropriate. */
6710
6711static void
6712macro_end (void)
6713{
df58fc94
RS
6714 /* Relaxation warning flags. */
6715 relax_substateT subtype = 0;
6716
6717 /* Check delay slot size requirements. */
6718 if (mips_macro_warning.delay_slot_length == 2)
6719 subtype |= RELAX_DELAY_SLOT_16BIT;
6720 if (mips_macro_warning.delay_slot_length != 0)
584892a6 6721 {
df58fc94
RS
6722 if (mips_macro_warning.delay_slot_length
6723 != mips_macro_warning.first_insn_sizes[0])
6724 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
6725 if (mips_macro_warning.delay_slot_length
6726 != mips_macro_warning.first_insn_sizes[1])
6727 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
6728 }
584892a6 6729
df58fc94
RS
6730 /* Check instruction count requirements. */
6731 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
6732 {
6733 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
584892a6
RS
6734 subtype |= RELAX_SECOND_LONGER;
6735 if (mips_opts.warn_about_macros)
6736 subtype |= RELAX_NOMACRO;
6737 if (mips_macro_warning.delay_slot_p)
6738 subtype |= RELAX_DELAY_SLOT;
df58fc94 6739 }
584892a6 6740
df58fc94
RS
6741 /* If both alternatives fail to fill a delay slot correctly,
6742 emit the warning now. */
6743 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
6744 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
6745 {
6746 relax_substateT s;
6747 const char *msg;
6748
6749 s = subtype & (RELAX_DELAY_SLOT_16BIT
6750 | RELAX_DELAY_SLOT_SIZE_FIRST
6751 | RELAX_DELAY_SLOT_SIZE_SECOND);
6752 msg = macro_warning (s);
6753 if (msg != NULL)
6754 as_warn ("%s", msg);
6755 subtype &= ~s;
6756 }
6757
6758 /* If both implementations are longer than 1 instruction, then emit the
6759 warning now. */
6760 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
6761 {
6762 relax_substateT s;
6763 const char *msg;
6764
6765 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
6766 msg = macro_warning (s);
6767 if (msg != NULL)
6768 as_warn ("%s", msg);
6769 subtype &= ~s;
584892a6 6770 }
df58fc94
RS
6771
6772 /* If any flags still set, then one implementation might need a warning
6773 and the other either will need one of a different kind or none at all.
6774 Pass any remaining flags over to relaxation. */
6775 if (mips_macro_warning.first_frag != NULL)
6776 mips_macro_warning.first_frag->fr_subtype |= subtype;
584892a6
RS
6777}
6778
df58fc94
RS
6779/* Instruction operand formats used in macros that vary between
6780 standard MIPS and microMIPS code. */
6781
833794fc 6782static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
df58fc94
RS
6783static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
6784static const char * const jalr_fmt[2] = { "d,s", "t,s" };
6785static const char * const lui_fmt[2] = { "t,u", "s,u" };
6786static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
833794fc 6787static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
df58fc94
RS
6788static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
6789static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
6790
833794fc 6791#define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
df58fc94
RS
6792#define COP12_FMT (cop12_fmt[mips_opts.micromips])
6793#define JALR_FMT (jalr_fmt[mips_opts.micromips])
6794#define LUI_FMT (lui_fmt[mips_opts.micromips])
6795#define MEM12_FMT (mem12_fmt[mips_opts.micromips])
833794fc 6796#define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
df58fc94
RS
6797#define SHFT_FMT (shft_fmt[mips_opts.micromips])
6798#define TRAP_FMT (trap_fmt[mips_opts.micromips])
6799
6e1304d8
RS
6800/* Read a macro's relocation codes from *ARGS and store them in *R.
6801 The first argument in *ARGS will be either the code for a single
6802 relocation or -1 followed by the three codes that make up a
6803 composite relocation. */
6804
6805static void
6806macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
6807{
6808 int i, next;
6809
6810 next = va_arg (*args, int);
6811 if (next >= 0)
6812 r[0] = (bfd_reloc_code_real_type) next;
6813 else
f2ae14a1
RS
6814 {
6815 for (i = 0; i < 3; i++)
6816 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
6817 /* This function is only used for 16-bit relocation fields.
6818 To make the macro code simpler, treat an unrelocated value
6819 in the same way as BFD_RELOC_LO16. */
6820 if (r[0] == BFD_RELOC_UNUSED)
6821 r[0] = BFD_RELOC_LO16;
6822 }
6e1304d8
RS
6823}
6824
252b5132
RH
6825/* Build an instruction created by a macro expansion. This is passed
6826 a pointer to the count of instructions created so far, an
6827 expression, the name of the instruction to build, an operand format
6828 string, and corresponding arguments. */
6829
252b5132 6830static void
67c0d1eb 6831macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 6832{
df58fc94 6833 const struct mips_opcode *mo = NULL;
f6688943 6834 bfd_reloc_code_real_type r[3];
df58fc94 6835 const struct mips_opcode *amo;
e077a1c8 6836 const struct mips_operand *operand;
df58fc94
RS
6837 struct hash_control *hash;
6838 struct mips_cl_insn insn;
252b5132 6839 va_list args;
e077a1c8 6840 unsigned int uval;
252b5132 6841
252b5132 6842 va_start (args, fmt);
252b5132 6843
252b5132
RH
6844 if (mips_opts.mips16)
6845 {
03ea81db 6846 mips16_macro_build (ep, name, fmt, &args);
252b5132
RH
6847 va_end (args);
6848 return;
6849 }
6850
f6688943
TS
6851 r[0] = BFD_RELOC_UNUSED;
6852 r[1] = BFD_RELOC_UNUSED;
6853 r[2] = BFD_RELOC_UNUSED;
df58fc94
RS
6854 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
6855 amo = (struct mips_opcode *) hash_find (hash, name);
6856 gas_assert (amo);
6857 gas_assert (strcmp (name, amo->name) == 0);
1e915849 6858
df58fc94 6859 do
8b082fb1
TS
6860 {
6861 /* Search until we get a match for NAME. It is assumed here that
df58fc94
RS
6862 macros will never generate MDMX, MIPS-3D, or MT instructions.
6863 We try to match an instruction that fulfils the branch delay
6864 slot instruction length requirement (if any) of the previous
6865 instruction. While doing this we record the first instruction
6866 seen that matches all the other conditions and use it anyway
6867 if the requirement cannot be met; we will issue an appropriate
6868 warning later on. */
6869 if (strcmp (fmt, amo->args) == 0
6870 && amo->pinfo != INSN_MACRO
6871 && is_opcode_valid (amo)
6872 && is_size_valid (amo))
6873 {
6874 if (is_delay_slot_valid (amo))
6875 {
6876 mo = amo;
6877 break;
6878 }
6879 else if (!mo)
6880 mo = amo;
6881 }
8b082fb1 6882
df58fc94
RS
6883 ++amo;
6884 gas_assert (amo->name);
252b5132 6885 }
df58fc94 6886 while (strcmp (name, amo->name) == 0);
252b5132 6887
df58fc94 6888 gas_assert (mo);
1e915849 6889 create_insn (&insn, mo);
e077a1c8 6890 for (; *fmt; ++fmt)
252b5132 6891 {
e077a1c8 6892 switch (*fmt)
252b5132 6893 {
252b5132
RH
6894 case ',':
6895 case '(':
6896 case ')':
252b5132 6897 case 'z':
e077a1c8 6898 break;
252b5132
RH
6899
6900 case 'i':
6901 case 'j':
6e1304d8 6902 macro_read_relocs (&args, r);
9c2799c2 6903 gas_assert (*r == BFD_RELOC_GPREL16
e391c024
RS
6904 || *r == BFD_RELOC_MIPS_HIGHER
6905 || *r == BFD_RELOC_HI16_S
6906 || *r == BFD_RELOC_LO16
6907 || *r == BFD_RELOC_MIPS_GOT_OFST);
e077a1c8 6908 break;
e391c024
RS
6909
6910 case 'o':
6911 macro_read_relocs (&args, r);
e077a1c8 6912 break;
252b5132
RH
6913
6914 case 'u':
6e1304d8 6915 macro_read_relocs (&args, r);
9c2799c2 6916 gas_assert (ep != NULL
90ecf173
MR
6917 && (ep->X_op == O_constant
6918 || (ep->X_op == O_symbol
6919 && (*r == BFD_RELOC_MIPS_HIGHEST
6920 || *r == BFD_RELOC_HI16_S
6921 || *r == BFD_RELOC_HI16
6922 || *r == BFD_RELOC_GPREL16
6923 || *r == BFD_RELOC_MIPS_GOT_HI16
6924 || *r == BFD_RELOC_MIPS_CALL_HI16))));
e077a1c8 6925 break;
252b5132
RH
6926
6927 case 'p':
9c2799c2 6928 gas_assert (ep != NULL);
bad36eac 6929
252b5132
RH
6930 /*
6931 * This allows macro() to pass an immediate expression for
6932 * creating short branches without creating a symbol.
bad36eac
DJ
6933 *
6934 * We don't allow branch relaxation for these branches, as
6935 * they should only appear in ".set nomacro" anyway.
252b5132
RH
6936 */
6937 if (ep->X_op == O_constant)
6938 {
df58fc94
RS
6939 /* For microMIPS we always use relocations for branches.
6940 So we should not resolve immediate values. */
6941 gas_assert (!mips_opts.micromips);
6942
bad36eac
DJ
6943 if ((ep->X_add_number & 3) != 0)
6944 as_bad (_("branch to misaligned address (0x%lx)"),
6945 (unsigned long) ep->X_add_number);
6946 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
6947 as_bad (_("branch address range overflow (0x%lx)"),
6948 (unsigned long) ep->X_add_number);
252b5132
RH
6949 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
6950 ep = NULL;
6951 }
6952 else
0b25d3e6 6953 *r = BFD_RELOC_16_PCREL_S2;
e077a1c8 6954 break;
252b5132
RH
6955
6956 case 'a':
9c2799c2 6957 gas_assert (ep != NULL);
f6688943 6958 *r = BFD_RELOC_MIPS_JMP;
e077a1c8 6959 break;
d43b4baf 6960
252b5132 6961 default:
e077a1c8
RS
6962 operand = (mips_opts.micromips
6963 ? decode_micromips_operand (fmt)
6964 : decode_mips_operand (fmt));
6965 if (!operand)
6966 abort ();
6967
6968 uval = va_arg (args, int);
6969 if (operand->type == OP_CLO_CLZ_DEST)
6970 uval |= (uval << 5);
6971 insn_insert_operand (&insn, operand, uval);
6972
6973 if (*fmt == '+' || *fmt == 'm')
6974 ++fmt;
6975 break;
252b5132 6976 }
252b5132
RH
6977 }
6978 va_end (args);
9c2799c2 6979 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 6980
df58fc94 6981 append_insn (&insn, ep, r, TRUE);
252b5132
RH
6982}
6983
6984static void
67c0d1eb 6985mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
03ea81db 6986 va_list *args)
252b5132 6987{
1e915849 6988 struct mips_opcode *mo;
252b5132 6989 struct mips_cl_insn insn;
e077a1c8 6990 const struct mips_operand *operand;
f6688943
TS
6991 bfd_reloc_code_real_type r[3]
6992 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 6993
1e915849 6994 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
9c2799c2
NC
6995 gas_assert (mo);
6996 gas_assert (strcmp (name, mo->name) == 0);
252b5132 6997
1e915849 6998 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 6999 {
1e915849 7000 ++mo;
9c2799c2
NC
7001 gas_assert (mo->name);
7002 gas_assert (strcmp (name, mo->name) == 0);
252b5132
RH
7003 }
7004
1e915849 7005 create_insn (&insn, mo);
e077a1c8 7006 for (; *fmt; ++fmt)
252b5132
RH
7007 {
7008 int c;
7009
e077a1c8 7010 c = *fmt;
252b5132
RH
7011 switch (c)
7012 {
252b5132
RH
7013 case ',':
7014 case '(':
7015 case ')':
e077a1c8 7016 break;
252b5132
RH
7017
7018 case '0':
7019 case 'S':
7020 case 'P':
7021 case 'R':
e077a1c8 7022 break;
252b5132
RH
7023
7024 case '<':
7025 case '>':
7026 case '4':
7027 case '5':
7028 case 'H':
7029 case 'W':
7030 case 'D':
7031 case 'j':
7032 case '8':
7033 case 'V':
7034 case 'C':
7035 case 'U':
7036 case 'k':
7037 case 'K':
7038 case 'p':
7039 case 'q':
7040 {
b886a2ab
RS
7041 offsetT value;
7042
9c2799c2 7043 gas_assert (ep != NULL);
252b5132
RH
7044
7045 if (ep->X_op != O_constant)
874e8986 7046 *r = (int) BFD_RELOC_UNUSED + c;
b886a2ab 7047 else if (calculate_reloc (*r, ep->X_add_number, &value))
252b5132 7048 {
b886a2ab 7049 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
252b5132 7050 ep = NULL;
f6688943 7051 *r = BFD_RELOC_UNUSED;
252b5132
RH
7052 }
7053 }
e077a1c8 7054 break;
252b5132 7055
e077a1c8
RS
7056 default:
7057 operand = decode_mips16_operand (c, FALSE);
7058 if (!operand)
7059 abort ();
252b5132 7060
4a06e5a2 7061 insn_insert_operand (&insn, operand, va_arg (*args, int));
e077a1c8
RS
7062 break;
7063 }
252b5132
RH
7064 }
7065
9c2799c2 7066 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 7067
df58fc94 7068 append_insn (&insn, ep, r, TRUE);
252b5132
RH
7069}
7070
2051e8c4
MR
7071/*
7072 * Sign-extend 32-bit mode constants that have bit 31 set and all
7073 * higher bits unset.
7074 */
9f872bbe 7075static void
2051e8c4
MR
7076normalize_constant_expr (expressionS *ex)
7077{
9ee2a2d4 7078 if (ex->X_op == O_constant
2051e8c4
MR
7079 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7080 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7081 - 0x80000000);
7082}
7083
7084/*
7085 * Sign-extend 32-bit mode address offsets that have bit 31 set and
7086 * all higher bits unset.
7087 */
7088static void
7089normalize_address_expr (expressionS *ex)
7090{
7091 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
7092 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
7093 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7094 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7095 - 0x80000000);
7096}
7097
438c16b8
TS
7098/*
7099 * Generate a "jalr" instruction with a relocation hint to the called
7100 * function. This occurs in NewABI PIC code.
7101 */
7102static void
df58fc94 7103macro_build_jalr (expressionS *ep, int cprestore)
438c16b8 7104{
df58fc94
RS
7105 static const bfd_reloc_code_real_type jalr_relocs[2]
7106 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
7107 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
7108 const char *jalr;
685736be 7109 char *f = NULL;
b34976b6 7110
1180b5a4 7111 if (MIPS_JALR_HINT_P (ep))
f21f8242 7112 {
cc3d92a5 7113 frag_grow (8);
f21f8242
AO
7114 f = frag_more (0);
7115 }
2906b037 7116 if (mips_opts.micromips)
df58fc94 7117 {
833794fc
MR
7118 jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
7119 ? "jalr" : "jalrs");
e64af278 7120 if (MIPS_JALR_HINT_P (ep)
833794fc 7121 || mips_opts.insn32
e64af278 7122 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
7123 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
7124 else
7125 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
7126 }
2906b037
MR
7127 else
7128 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
1180b5a4 7129 if (MIPS_JALR_HINT_P (ep))
df58fc94 7130 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
438c16b8
TS
7131}
7132
252b5132
RH
7133/*
7134 * Generate a "lui" instruction.
7135 */
7136static void
67c0d1eb 7137macro_build_lui (expressionS *ep, int regnum)
252b5132 7138{
9c2799c2 7139 gas_assert (! mips_opts.mips16);
252b5132 7140
df58fc94 7141 if (ep->X_op != O_constant)
252b5132 7142 {
9c2799c2 7143 gas_assert (ep->X_op == O_symbol);
bbe506e8
TS
7144 /* _gp_disp is a special case, used from s_cpload.
7145 __gnu_local_gp is used if mips_no_shared. */
9c2799c2 7146 gas_assert (mips_pic == NO_PIC
78e1bb40 7147 || (! HAVE_NEWABI
aa6975fb
ILT
7148 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
7149 || (! mips_in_shared
bbe506e8
TS
7150 && strcmp (S_GET_NAME (ep->X_add_symbol),
7151 "__gnu_local_gp") == 0));
252b5132
RH
7152 }
7153
df58fc94 7154 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
252b5132
RH
7155}
7156
885add95
CD
7157/* Generate a sequence of instructions to do a load or store from a constant
7158 offset off of a base register (breg) into/from a target register (treg),
7159 using AT if necessary. */
7160static void
67c0d1eb
RS
7161macro_build_ldst_constoffset (expressionS *ep, const char *op,
7162 int treg, int breg, int dbl)
885add95 7163{
9c2799c2 7164 gas_assert (ep->X_op == O_constant);
885add95 7165
256ab948 7166 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
7167 if (!dbl)
7168 normalize_constant_expr (ep);
256ab948 7169
67c1ffbe 7170 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 7171 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
7172 as_warn (_("operand overflow"));
7173
7174 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
7175 {
7176 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 7177 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
7178 }
7179 else
7180 {
7181 /* 32-bit offset, need multiple instructions and AT, like:
7182 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
7183 addu $tempreg,$tempreg,$breg
7184 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
7185 to handle the complete offset. */
67c0d1eb
RS
7186 macro_build_lui (ep, AT);
7187 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7188 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95 7189
741fe287 7190 if (!mips_opts.at)
8fc2e39e 7191 as_bad (_("Macro used $at after \".set noat\""));
885add95
CD
7192 }
7193}
7194
252b5132
RH
7195/* set_at()
7196 * Generates code to set the $at register to true (one)
7197 * if reg is less than the immediate expression.
7198 */
7199static void
67c0d1eb 7200set_at (int reg, int unsignedp)
252b5132
RH
7201{
7202 if (imm_expr.X_op == O_constant
7203 && imm_expr.X_add_number >= -0x8000
7204 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
7205 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
7206 AT, reg, BFD_RELOC_LO16);
252b5132
RH
7207 else
7208 {
67c0d1eb
RS
7209 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7210 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
7211 }
7212}
7213
252b5132
RH
7214/* Count the leading zeroes by performing a binary chop. This is a
7215 bulky bit of source, but performance is a LOT better for the
7216 majority of values than a simple loop to count the bits:
7217 for (lcnt = 0; (lcnt < 32); lcnt++)
7218 if ((v) & (1 << (31 - lcnt)))
7219 break;
7220 However it is not code size friendly, and the gain will drop a bit
7221 on certain cached systems.
7222*/
7223#define COUNT_TOP_ZEROES(v) \
7224 (((v) & ~0xffff) == 0 \
7225 ? ((v) & ~0xff) == 0 \
7226 ? ((v) & ~0xf) == 0 \
7227 ? ((v) & ~0x3) == 0 \
7228 ? ((v) & ~0x1) == 0 \
7229 ? !(v) \
7230 ? 32 \
7231 : 31 \
7232 : 30 \
7233 : ((v) & ~0x7) == 0 \
7234 ? 29 \
7235 : 28 \
7236 : ((v) & ~0x3f) == 0 \
7237 ? ((v) & ~0x1f) == 0 \
7238 ? 27 \
7239 : 26 \
7240 : ((v) & ~0x7f) == 0 \
7241 ? 25 \
7242 : 24 \
7243 : ((v) & ~0xfff) == 0 \
7244 ? ((v) & ~0x3ff) == 0 \
7245 ? ((v) & ~0x1ff) == 0 \
7246 ? 23 \
7247 : 22 \
7248 : ((v) & ~0x7ff) == 0 \
7249 ? 21 \
7250 : 20 \
7251 : ((v) & ~0x3fff) == 0 \
7252 ? ((v) & ~0x1fff) == 0 \
7253 ? 19 \
7254 : 18 \
7255 : ((v) & ~0x7fff) == 0 \
7256 ? 17 \
7257 : 16 \
7258 : ((v) & ~0xffffff) == 0 \
7259 ? ((v) & ~0xfffff) == 0 \
7260 ? ((v) & ~0x3ffff) == 0 \
7261 ? ((v) & ~0x1ffff) == 0 \
7262 ? 15 \
7263 : 14 \
7264 : ((v) & ~0x7ffff) == 0 \
7265 ? 13 \
7266 : 12 \
7267 : ((v) & ~0x3fffff) == 0 \
7268 ? ((v) & ~0x1fffff) == 0 \
7269 ? 11 \
7270 : 10 \
7271 : ((v) & ~0x7fffff) == 0 \
7272 ? 9 \
7273 : 8 \
7274 : ((v) & ~0xfffffff) == 0 \
7275 ? ((v) & ~0x3ffffff) == 0 \
7276 ? ((v) & ~0x1ffffff) == 0 \
7277 ? 7 \
7278 : 6 \
7279 : ((v) & ~0x7ffffff) == 0 \
7280 ? 5 \
7281 : 4 \
7282 : ((v) & ~0x3fffffff) == 0 \
7283 ? ((v) & ~0x1fffffff) == 0 \
7284 ? 3 \
7285 : 2 \
7286 : ((v) & ~0x7fffffff) == 0 \
7287 ? 1 \
7288 : 0)
7289
7290/* load_register()
67c1ffbe 7291 * This routine generates the least number of instructions necessary to load
252b5132
RH
7292 * an absolute expression value into a register.
7293 */
7294static void
67c0d1eb 7295load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
7296{
7297 int freg;
7298 expressionS hi32, lo32;
7299
7300 if (ep->X_op != O_big)
7301 {
9c2799c2 7302 gas_assert (ep->X_op == O_constant);
256ab948
TS
7303
7304 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
7305 if (!dbl)
7306 normalize_constant_expr (ep);
256ab948
TS
7307
7308 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
7309 {
7310 /* We can handle 16 bit signed values with an addiu to
7311 $zero. No need to ever use daddiu here, since $zero and
7312 the result are always correct in 32 bit mode. */
67c0d1eb 7313 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
7314 return;
7315 }
7316 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
7317 {
7318 /* We can handle 16 bit unsigned values with an ori to
7319 $zero. */
67c0d1eb 7320 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
7321 return;
7322 }
256ab948 7323 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
7324 {
7325 /* 32 bit values require an lui. */
df58fc94 7326 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 7327 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 7328 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
7329 return;
7330 }
7331 }
7332
7333 /* The value is larger than 32 bits. */
7334
2051e8c4 7335 if (!dbl || HAVE_32BIT_GPRS)
252b5132 7336 {
55e08f71
NC
7337 char value[32];
7338
7339 sprintf_vma (value, ep->X_add_number);
20e1fcfd 7340 as_bad (_("Number (0x%s) larger than 32 bits"), value);
67c0d1eb 7341 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
7342 return;
7343 }
7344
7345 if (ep->X_op != O_big)
7346 {
7347 hi32 = *ep;
7348 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
7349 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
7350 hi32.X_add_number &= 0xffffffff;
7351 lo32 = *ep;
7352 lo32.X_add_number &= 0xffffffff;
7353 }
7354 else
7355 {
9c2799c2 7356 gas_assert (ep->X_add_number > 2);
252b5132
RH
7357 if (ep->X_add_number == 3)
7358 generic_bignum[3] = 0;
7359 else if (ep->X_add_number > 4)
7360 as_bad (_("Number larger than 64 bits"));
7361 lo32.X_op = O_constant;
7362 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
7363 hi32.X_op = O_constant;
7364 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
7365 }
7366
7367 if (hi32.X_add_number == 0)
7368 freg = 0;
7369 else
7370 {
7371 int shift, bit;
7372 unsigned long hi, lo;
7373
956cd1d6 7374 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
7375 {
7376 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
7377 {
67c0d1eb 7378 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
7379 return;
7380 }
7381 if (lo32.X_add_number & 0x80000000)
7382 {
df58fc94 7383 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 7384 if (lo32.X_add_number & 0xffff)
67c0d1eb 7385 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
7386 return;
7387 }
7388 }
252b5132
RH
7389
7390 /* Check for 16bit shifted constant. We know that hi32 is
7391 non-zero, so start the mask on the first bit of the hi32
7392 value. */
7393 shift = 17;
7394 do
beae10d5
KH
7395 {
7396 unsigned long himask, lomask;
7397
7398 if (shift < 32)
7399 {
7400 himask = 0xffff >> (32 - shift);
7401 lomask = (0xffff << shift) & 0xffffffff;
7402 }
7403 else
7404 {
7405 himask = 0xffff << (shift - 32);
7406 lomask = 0;
7407 }
7408 if ((hi32.X_add_number & ~(offsetT) himask) == 0
7409 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
7410 {
7411 expressionS tmp;
7412
7413 tmp.X_op = O_constant;
7414 if (shift < 32)
7415 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
7416 | (lo32.X_add_number >> shift));
7417 else
7418 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb 7419 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
df58fc94 7420 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 7421 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
7422 return;
7423 }
f9419b05 7424 ++shift;
beae10d5
KH
7425 }
7426 while (shift <= (64 - 16));
252b5132
RH
7427
7428 /* Find the bit number of the lowest one bit, and store the
7429 shifted value in hi/lo. */
7430 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
7431 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
7432 if (lo != 0)
7433 {
7434 bit = 0;
7435 while ((lo & 1) == 0)
7436 {
7437 lo >>= 1;
7438 ++bit;
7439 }
7440 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
7441 hi >>= bit;
7442 }
7443 else
7444 {
7445 bit = 32;
7446 while ((hi & 1) == 0)
7447 {
7448 hi >>= 1;
7449 ++bit;
7450 }
7451 lo = hi;
7452 hi = 0;
7453 }
7454
7455 /* Optimize if the shifted value is a (power of 2) - 1. */
7456 if ((hi == 0 && ((lo + 1) & lo) == 0)
7457 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
7458 {
7459 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 7460 if (shift != 0)
beae10d5 7461 {
252b5132
RH
7462 expressionS tmp;
7463
7464 /* This instruction will set the register to be all
7465 ones. */
beae10d5
KH
7466 tmp.X_op = O_constant;
7467 tmp.X_add_number = (offsetT) -1;
67c0d1eb 7468 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
7469 if (bit != 0)
7470 {
7471 bit += shift;
df58fc94 7472 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 7473 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 7474 }
df58fc94 7475 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
67c0d1eb 7476 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
7477 return;
7478 }
7479 }
252b5132
RH
7480
7481 /* Sign extend hi32 before calling load_register, because we can
7482 generally get better code when we load a sign extended value. */
7483 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 7484 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 7485 load_register (reg, &hi32, 0);
252b5132
RH
7486 freg = reg;
7487 }
7488 if ((lo32.X_add_number & 0xffff0000) == 0)
7489 {
7490 if (freg != 0)
7491 {
df58fc94 7492 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
252b5132
RH
7493 freg = reg;
7494 }
7495 }
7496 else
7497 {
7498 expressionS mid16;
7499
956cd1d6 7500 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 7501 {
df58fc94
RS
7502 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
7503 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
beae10d5
KH
7504 return;
7505 }
252b5132
RH
7506
7507 if (freg != 0)
7508 {
df58fc94 7509 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
252b5132
RH
7510 freg = reg;
7511 }
7512 mid16 = lo32;
7513 mid16.X_add_number >>= 16;
67c0d1eb 7514 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
df58fc94 7515 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
252b5132
RH
7516 freg = reg;
7517 }
7518 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 7519 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
7520}
7521
269137b2
TS
7522static inline void
7523load_delay_nop (void)
7524{
7525 if (!gpr_interlocks)
7526 macro_build (NULL, "nop", "");
7527}
7528
252b5132
RH
7529/* Load an address into a register. */
7530
7531static void
67c0d1eb 7532load_address (int reg, expressionS *ep, int *used_at)
252b5132 7533{
252b5132
RH
7534 if (ep->X_op != O_constant
7535 && ep->X_op != O_symbol)
7536 {
7537 as_bad (_("expression too complex"));
7538 ep->X_op = O_constant;
7539 }
7540
7541 if (ep->X_op == O_constant)
7542 {
67c0d1eb 7543 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
7544 return;
7545 }
7546
7547 if (mips_pic == NO_PIC)
7548 {
7549 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 7550 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
7551 Otherwise we want
7552 lui $reg,<sym> (BFD_RELOC_HI16_S)
7553 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 7554 If we have an addend, we always use the latter form.
76b3015f 7555
d6bc6245
TS
7556 With 64bit address space and a usable $at we want
7557 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
7558 lui $at,<sym> (BFD_RELOC_HI16_S)
7559 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
7560 daddiu $at,<sym> (BFD_RELOC_LO16)
7561 dsll32 $reg,0
3a482fd5 7562 daddu $reg,$reg,$at
76b3015f 7563
c03099e6 7564 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
7565 on superscalar processors.
7566 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
7567 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
7568 dsll $reg,16
7569 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
7570 dsll $reg,16
7571 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
7572
7573 For GP relative symbols in 64bit address space we can use
7574 the same sequence as in 32bit address space. */
aed1a261 7575 if (HAVE_64BIT_SYMBOLS)
d6bc6245 7576 {
6caf9ef4
TS
7577 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
7578 && !nopic_need_relax (ep->X_add_symbol, 1))
7579 {
7580 relax_start (ep->X_add_symbol);
7581 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
7582 mips_gp_register, BFD_RELOC_GPREL16);
7583 relax_switch ();
7584 }
d6bc6245 7585
741fe287 7586 if (*used_at == 0 && mips_opts.at)
d6bc6245 7587 {
df58fc94
RS
7588 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
7589 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
67c0d1eb
RS
7590 macro_build (ep, "daddiu", "t,r,j", reg, reg,
7591 BFD_RELOC_MIPS_HIGHER);
7592 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
df58fc94 7593 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
67c0d1eb 7594 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
7595 *used_at = 1;
7596 }
7597 else
7598 {
df58fc94 7599 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb
RS
7600 macro_build (ep, "daddiu", "t,r,j", reg, reg,
7601 BFD_RELOC_MIPS_HIGHER);
df58fc94 7602 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 7603 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
df58fc94 7604 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 7605 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 7606 }
6caf9ef4
TS
7607
7608 if (mips_relax.sequence)
7609 relax_end ();
d6bc6245 7610 }
252b5132
RH
7611 else
7612 {
d6bc6245 7613 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 7614 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 7615 {
4d7206a2 7616 relax_start (ep->X_add_symbol);
67c0d1eb 7617 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 7618 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 7619 relax_switch ();
d6bc6245 7620 }
67c0d1eb
RS
7621 macro_build_lui (ep, reg);
7622 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
7623 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
7624 if (mips_relax.sequence)
7625 relax_end ();
d6bc6245 7626 }
252b5132 7627 }
0a44bf69 7628 else if (!mips_big_got)
252b5132
RH
7629 {
7630 expressionS ex;
7631
7632 /* If this is a reference to an external symbol, we want
7633 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7634 Otherwise we want
7635 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7636 nop
7637 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
7638 If there is a constant, it must be added in after.
7639
ed6fb7bd 7640 If we have NewABI, we want
f5040a92
AO
7641 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
7642 unless we're referencing a global symbol with a non-zero
7643 offset, in which case cst must be added separately. */
ed6fb7bd
SC
7644 if (HAVE_NEWABI)
7645 {
f5040a92
AO
7646 if (ep->X_add_number)
7647 {
4d7206a2 7648 ex.X_add_number = ep->X_add_number;
f5040a92 7649 ep->X_add_number = 0;
4d7206a2 7650 relax_start (ep->X_add_symbol);
67c0d1eb
RS
7651 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
7652 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
7653 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
7654 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
7655 ex.X_op = O_constant;
67c0d1eb 7656 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 7657 reg, reg, BFD_RELOC_LO16);
f5040a92 7658 ep->X_add_number = ex.X_add_number;
4d7206a2 7659 relax_switch ();
f5040a92 7660 }
67c0d1eb 7661 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 7662 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
7663 if (mips_relax.sequence)
7664 relax_end ();
ed6fb7bd
SC
7665 }
7666 else
7667 {
f5040a92
AO
7668 ex.X_add_number = ep->X_add_number;
7669 ep->X_add_number = 0;
67c0d1eb
RS
7670 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
7671 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 7672 load_delay_nop ();
4d7206a2
RS
7673 relax_start (ep->X_add_symbol);
7674 relax_switch ();
67c0d1eb 7675 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 7676 BFD_RELOC_LO16);
4d7206a2 7677 relax_end ();
ed6fb7bd 7678
f5040a92
AO
7679 if (ex.X_add_number != 0)
7680 {
7681 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
7682 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
7683 ex.X_op = O_constant;
67c0d1eb 7684 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 7685 reg, reg, BFD_RELOC_LO16);
f5040a92 7686 }
252b5132
RH
7687 }
7688 }
0a44bf69 7689 else if (mips_big_got)
252b5132
RH
7690 {
7691 expressionS ex;
252b5132
RH
7692
7693 /* This is the large GOT case. If this is a reference to an
7694 external symbol, we want
7695 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7696 addu $reg,$reg,$gp
7697 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
7698
7699 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
7700 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7701 nop
7702 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 7703 If there is a constant, it must be added in after.
f5040a92
AO
7704
7705 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
7706 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
7707 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 7708 */
438c16b8
TS
7709 if (HAVE_NEWABI)
7710 {
4d7206a2 7711 ex.X_add_number = ep->X_add_number;
f5040a92 7712 ep->X_add_number = 0;
4d7206a2 7713 relax_start (ep->X_add_symbol);
df58fc94 7714 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
7715 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7716 reg, reg, mips_gp_register);
7717 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
7718 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
7719 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
7720 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
7721 else if (ex.X_add_number)
7722 {
7723 ex.X_op = O_constant;
67c0d1eb
RS
7724 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
7725 BFD_RELOC_LO16);
f5040a92
AO
7726 }
7727
7728 ep->X_add_number = ex.X_add_number;
4d7206a2 7729 relax_switch ();
67c0d1eb 7730 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 7731 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
7732 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
7733 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 7734 relax_end ();
438c16b8 7735 }
252b5132 7736 else
438c16b8 7737 {
f5040a92
AO
7738 ex.X_add_number = ep->X_add_number;
7739 ep->X_add_number = 0;
4d7206a2 7740 relax_start (ep->X_add_symbol);
df58fc94 7741 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
7742 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7743 reg, reg, mips_gp_register);
7744 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
7745 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
7746 relax_switch ();
7747 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
7748 {
7749 /* We need a nop before loading from $gp. This special
7750 check is required because the lui which starts the main
7751 instruction stream does not refer to $gp, and so will not
7752 insert the nop which may be required. */
67c0d1eb 7753 macro_build (NULL, "nop", "");
438c16b8 7754 }
67c0d1eb 7755 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 7756 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 7757 load_delay_nop ();
67c0d1eb 7758 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 7759 BFD_RELOC_LO16);
4d7206a2 7760 relax_end ();
438c16b8 7761
f5040a92
AO
7762 if (ex.X_add_number != 0)
7763 {
7764 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
7765 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
7766 ex.X_op = O_constant;
67c0d1eb
RS
7767 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
7768 BFD_RELOC_LO16);
f5040a92 7769 }
252b5132
RH
7770 }
7771 }
252b5132
RH
7772 else
7773 abort ();
8fc2e39e 7774
741fe287 7775 if (!mips_opts.at && *used_at == 1)
8fc2e39e 7776 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
7777}
7778
ea1fb5dc
RS
7779/* Move the contents of register SOURCE into register DEST. */
7780
7781static void
67c0d1eb 7782move_register (int dest, int source)
ea1fb5dc 7783{
df58fc94
RS
7784 /* Prefer to use a 16-bit microMIPS instruction unless the previous
7785 instruction specifically requires a 32-bit one. */
7786 if (mips_opts.micromips
833794fc 7787 && !mips_opts.insn32
df58fc94 7788 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7951ca42 7789 macro_build (NULL, "move", "mp,mj", dest, source);
df58fc94
RS
7790 else
7791 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
7792 dest, source, 0);
ea1fb5dc
RS
7793}
7794
4d7206a2 7795/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
7796 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
7797 The two alternatives are:
4d7206a2
RS
7798
7799 Global symbol Local sybmol
7800 ------------- ------------
7801 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
7802 ... ...
7803 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
7804
7805 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
7806 emits the second for a 16-bit offset or add_got_offset_hilo emits
7807 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
7808
7809static void
67c0d1eb 7810load_got_offset (int dest, expressionS *local)
4d7206a2
RS
7811{
7812 expressionS global;
7813
7814 global = *local;
7815 global.X_add_number = 0;
7816
7817 relax_start (local->X_add_symbol);
67c0d1eb
RS
7818 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
7819 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 7820 relax_switch ();
67c0d1eb
RS
7821 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
7822 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
7823 relax_end ();
7824}
7825
7826static void
67c0d1eb 7827add_got_offset (int dest, expressionS *local)
4d7206a2
RS
7828{
7829 expressionS global;
7830
7831 global.X_op = O_constant;
7832 global.X_op_symbol = NULL;
7833 global.X_add_symbol = NULL;
7834 global.X_add_number = local->X_add_number;
7835
7836 relax_start (local->X_add_symbol);
67c0d1eb 7837 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
7838 dest, dest, BFD_RELOC_LO16);
7839 relax_switch ();
67c0d1eb 7840 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
7841 relax_end ();
7842}
7843
f6a22291
MR
7844static void
7845add_got_offset_hilo (int dest, expressionS *local, int tmp)
7846{
7847 expressionS global;
7848 int hold_mips_optimize;
7849
7850 global.X_op = O_constant;
7851 global.X_op_symbol = NULL;
7852 global.X_add_symbol = NULL;
7853 global.X_add_number = local->X_add_number;
7854
7855 relax_start (local->X_add_symbol);
7856 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
7857 relax_switch ();
7858 /* Set mips_optimize around the lui instruction to avoid
7859 inserting an unnecessary nop after the lw. */
7860 hold_mips_optimize = mips_optimize;
7861 mips_optimize = 2;
7862 macro_build_lui (&global, tmp);
7863 mips_optimize = hold_mips_optimize;
7864 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
7865 relax_end ();
7866
7867 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
7868}
7869
df58fc94
RS
7870/* Emit a sequence of instructions to emulate a branch likely operation.
7871 BR is an ordinary branch corresponding to one to be emulated. BRNEG
7872 is its complementing branch with the original condition negated.
7873 CALL is set if the original branch specified the link operation.
7874 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
7875
7876 Code like this is produced in the noreorder mode:
7877
7878 BRNEG <args>, 1f
7879 nop
7880 b <sym>
7881 delay slot (executed only if branch taken)
7882 1:
7883
7884 or, if CALL is set:
7885
7886 BRNEG <args>, 1f
7887 nop
7888 bal <sym>
7889 delay slot (executed only if branch taken)
7890 1:
7891
7892 In the reorder mode the delay slot would be filled with a nop anyway,
7893 so code produced is simply:
7894
7895 BR <args>, <sym>
7896 nop
7897
7898 This function is used when producing code for the microMIPS ASE that
7899 does not implement branch likely instructions in hardware. */
7900
7901static void
7902macro_build_branch_likely (const char *br, const char *brneg,
7903 int call, expressionS *ep, const char *fmt,
7904 unsigned int sreg, unsigned int treg)
7905{
7906 int noreorder = mips_opts.noreorder;
7907 expressionS expr1;
7908
7909 gas_assert (mips_opts.micromips);
7910 start_noreorder ();
7911 if (noreorder)
7912 {
7913 micromips_label_expr (&expr1);
7914 macro_build (&expr1, brneg, fmt, sreg, treg);
7915 macro_build (NULL, "nop", "");
7916 macro_build (ep, call ? "bal" : "b", "p");
7917
7918 /* Set to true so that append_insn adds a label. */
7919 emit_branch_likely_macro = TRUE;
7920 }
7921 else
7922 {
7923 macro_build (ep, br, fmt, sreg, treg);
7924 macro_build (NULL, "nop", "");
7925 }
7926 end_noreorder ();
7927}
7928
7929/* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
7930 the condition code tested. EP specifies the branch target. */
7931
7932static void
7933macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
7934{
7935 const int call = 0;
7936 const char *brneg;
7937 const char *br;
7938
7939 switch (type)
7940 {
7941 case M_BC1FL:
7942 br = "bc1f";
7943 brneg = "bc1t";
7944 break;
7945 case M_BC1TL:
7946 br = "bc1t";
7947 brneg = "bc1f";
7948 break;
7949 case M_BC2FL:
7950 br = "bc2f";
7951 brneg = "bc2t";
7952 break;
7953 case M_BC2TL:
7954 br = "bc2t";
7955 brneg = "bc2f";
7956 break;
7957 default:
7958 abort ();
7959 }
7960 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
7961}
7962
7963/* Emit a two-argument branch macro specified by TYPE, using SREG as
7964 the register tested. EP specifies the branch target. */
7965
7966static void
7967macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
7968{
7969 const char *brneg = NULL;
7970 const char *br;
7971 int call = 0;
7972
7973 switch (type)
7974 {
7975 case M_BGEZ:
7976 br = "bgez";
7977 break;
7978 case M_BGEZL:
7979 br = mips_opts.micromips ? "bgez" : "bgezl";
7980 brneg = "bltz";
7981 break;
7982 case M_BGEZALL:
7983 gas_assert (mips_opts.micromips);
833794fc 7984 br = mips_opts.insn32 ? "bgezal" : "bgezals";
df58fc94
RS
7985 brneg = "bltz";
7986 call = 1;
7987 break;
7988 case M_BGTZ:
7989 br = "bgtz";
7990 break;
7991 case M_BGTZL:
7992 br = mips_opts.micromips ? "bgtz" : "bgtzl";
7993 brneg = "blez";
7994 break;
7995 case M_BLEZ:
7996 br = "blez";
7997 break;
7998 case M_BLEZL:
7999 br = mips_opts.micromips ? "blez" : "blezl";
8000 brneg = "bgtz";
8001 break;
8002 case M_BLTZ:
8003 br = "bltz";
8004 break;
8005 case M_BLTZL:
8006 br = mips_opts.micromips ? "bltz" : "bltzl";
8007 brneg = "bgez";
8008 break;
8009 case M_BLTZALL:
8010 gas_assert (mips_opts.micromips);
833794fc 8011 br = mips_opts.insn32 ? "bltzal" : "bltzals";
df58fc94
RS
8012 brneg = "bgez";
8013 call = 1;
8014 break;
8015 default:
8016 abort ();
8017 }
8018 if (mips_opts.micromips && brneg)
8019 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
8020 else
8021 macro_build (ep, br, "s,p", sreg);
8022}
8023
8024/* Emit a three-argument branch macro specified by TYPE, using SREG and
8025 TREG as the registers tested. EP specifies the branch target. */
8026
8027static void
8028macro_build_branch_rsrt (int type, expressionS *ep,
8029 unsigned int sreg, unsigned int treg)
8030{
8031 const char *brneg = NULL;
8032 const int call = 0;
8033 const char *br;
8034
8035 switch (type)
8036 {
8037 case M_BEQ:
8038 case M_BEQ_I:
8039 br = "beq";
8040 break;
8041 case M_BEQL:
8042 case M_BEQL_I:
8043 br = mips_opts.micromips ? "beq" : "beql";
8044 brneg = "bne";
8045 break;
8046 case M_BNE:
8047 case M_BNE_I:
8048 br = "bne";
8049 break;
8050 case M_BNEL:
8051 case M_BNEL_I:
8052 br = mips_opts.micromips ? "bne" : "bnel";
8053 brneg = "beq";
8054 break;
8055 default:
8056 abort ();
8057 }
8058 if (mips_opts.micromips && brneg)
8059 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
8060 else
8061 macro_build (ep, br, "s,t,p", sreg, treg);
8062}
8063
f2ae14a1
RS
8064/* Return the high part that should be loaded in order to make the low
8065 part of VALUE accessible using an offset of OFFBITS bits. */
8066
8067static offsetT
8068offset_high_part (offsetT value, unsigned int offbits)
8069{
8070 offsetT bias;
8071 addressT low_mask;
8072
8073 if (offbits == 0)
8074 return value;
8075 bias = 1 << (offbits - 1);
8076 low_mask = bias * 2 - 1;
8077 return (value + bias) & ~low_mask;
8078}
8079
8080/* Return true if the value stored in offset_expr and offset_reloc
8081 fits into a signed offset of OFFBITS bits. RANGE is the maximum
8082 amount that the caller wants to add without inducing overflow
8083 and ALIGN is the known alignment of the value in bytes. */
8084
8085static bfd_boolean
8086small_offset_p (unsigned int range, unsigned int align, unsigned int offbits)
8087{
8088 if (offbits == 16)
8089 {
8090 /* Accept any relocation operator if overflow isn't a concern. */
8091 if (range < align && *offset_reloc != BFD_RELOC_UNUSED)
8092 return TRUE;
8093
8094 /* These relocations are guaranteed not to overflow in correct links. */
8095 if (*offset_reloc == BFD_RELOC_MIPS_LITERAL
8096 || gprel16_reloc_p (*offset_reloc))
8097 return TRUE;
8098 }
8099 if (offset_expr.X_op == O_constant
8100 && offset_high_part (offset_expr.X_add_number, offbits) == 0
8101 && offset_high_part (offset_expr.X_add_number + range, offbits) == 0)
8102 return TRUE;
8103 return FALSE;
8104}
8105
252b5132
RH
8106/*
8107 * Build macros
8108 * This routine implements the seemingly endless macro or synthesized
8109 * instructions and addressing modes in the mips assembly language. Many
8110 * of these macros are simple and are similar to each other. These could
67c1ffbe 8111 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
8112 * this verbose method. Others are not simple macros but are more like
8113 * optimizing code generation.
8114 * One interesting optimization is when several store macros appear
67c1ffbe 8115 * consecutively that would load AT with the upper half of the same address.
252b5132
RH
8116 * The ensuing load upper instructions are ommited. This implies some kind
8117 * of global optimization. We currently only optimize within a single macro.
8118 * For many of the load and store macros if the address is specified as a
8119 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
8120 * first load register 'at' with zero and use it as the base register. The
8121 * mips assembler simply uses register $zero. Just one tiny optimization
8122 * we're missing.
8123 */
8124static void
833794fc 8125macro (struct mips_cl_insn *ip, char *str)
252b5132 8126{
c0ebe874
RS
8127 const struct mips_operand_array *operands;
8128 unsigned int breg, i;
741fe287 8129 unsigned int tempreg;
252b5132 8130 int mask;
43841e91 8131 int used_at = 0;
df58fc94 8132 expressionS label_expr;
252b5132 8133 expressionS expr1;
df58fc94 8134 expressionS *ep;
252b5132
RH
8135 const char *s;
8136 const char *s2;
8137 const char *fmt;
8138 int likely = 0;
252b5132 8139 int coproc = 0;
7f3c4072 8140 int offbits = 16;
1abe91b1 8141 int call = 0;
df58fc94
RS
8142 int jals = 0;
8143 int dbl = 0;
8144 int imm = 0;
8145 int ust = 0;
8146 int lp = 0;
f2ae14a1 8147 bfd_boolean large_offset;
252b5132 8148 int off;
252b5132 8149 int hold_mips_optimize;
f2ae14a1 8150 unsigned int align;
c0ebe874 8151 unsigned int op[MAX_OPERANDS];
252b5132 8152
9c2799c2 8153 gas_assert (! mips_opts.mips16);
252b5132 8154
c0ebe874
RS
8155 operands = insn_operands (ip);
8156 for (i = 0; i < MAX_OPERANDS; i++)
8157 if (operands->operand[i])
8158 op[i] = insn_extract_operand (ip, operands->operand[i]);
8159 else
8160 op[i] = -1;
8161
252b5132
RH
8162 mask = ip->insn_mo->mask;
8163
df58fc94
RS
8164 label_expr.X_op = O_constant;
8165 label_expr.X_op_symbol = NULL;
8166 label_expr.X_add_symbol = NULL;
8167 label_expr.X_add_number = 0;
8168
252b5132
RH
8169 expr1.X_op = O_constant;
8170 expr1.X_op_symbol = NULL;
8171 expr1.X_add_symbol = NULL;
8172 expr1.X_add_number = 1;
f2ae14a1 8173 align = 1;
252b5132
RH
8174
8175 switch (mask)
8176 {
8177 case M_DABS:
8178 dbl = 1;
8179 case M_ABS:
df58fc94
RS
8180 /* bgez $a0,1f
8181 move v0,$a0
8182 sub v0,$zero,$a0
8183 1:
8184 */
252b5132 8185
7d10b47d 8186 start_noreorder ();
252b5132 8187
df58fc94
RS
8188 if (mips_opts.micromips)
8189 micromips_label_expr (&label_expr);
8190 else
8191 label_expr.X_add_number = 8;
c0ebe874
RS
8192 macro_build (&label_expr, "bgez", "s,p", op[1]);
8193 if (op[0] == op[1])
a605d2b3 8194 macro_build (NULL, "nop", "");
252b5132 8195 else
c0ebe874
RS
8196 move_register (op[0], op[1]);
8197 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", op[0], 0, op[1]);
df58fc94
RS
8198 if (mips_opts.micromips)
8199 micromips_add_label ();
252b5132 8200
7d10b47d 8201 end_noreorder ();
8fc2e39e 8202 break;
252b5132
RH
8203
8204 case M_ADD_I:
8205 s = "addi";
8206 s2 = "add";
8207 goto do_addi;
8208 case M_ADDU_I:
8209 s = "addiu";
8210 s2 = "addu";
8211 goto do_addi;
8212 case M_DADD_I:
8213 dbl = 1;
8214 s = "daddi";
8215 s2 = "dadd";
df58fc94
RS
8216 if (!mips_opts.micromips)
8217 goto do_addi;
8218 if (imm_expr.X_op == O_constant
8219 && imm_expr.X_add_number >= -0x200
8220 && imm_expr.X_add_number < 0x200)
8221 {
c0ebe874 8222 macro_build (NULL, s, "t,r,.", op[0], op[1], imm_expr.X_add_number);
df58fc94
RS
8223 break;
8224 }
8225 goto do_addi_i;
252b5132
RH
8226 case M_DADDU_I:
8227 dbl = 1;
8228 s = "daddiu";
8229 s2 = "daddu";
8230 do_addi:
8231 if (imm_expr.X_op == O_constant
8232 && imm_expr.X_add_number >= -0x8000
8233 && imm_expr.X_add_number < 0x8000)
8234 {
c0ebe874 8235 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 8236 break;
252b5132 8237 }
df58fc94 8238 do_addi_i:
8fc2e39e 8239 used_at = 1;
67c0d1eb 8240 load_register (AT, &imm_expr, dbl);
c0ebe874 8241 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
8242 break;
8243
8244 case M_AND_I:
8245 s = "andi";
8246 s2 = "and";
8247 goto do_bit;
8248 case M_OR_I:
8249 s = "ori";
8250 s2 = "or";
8251 goto do_bit;
8252 case M_NOR_I:
8253 s = "";
8254 s2 = "nor";
8255 goto do_bit;
8256 case M_XOR_I:
8257 s = "xori";
8258 s2 = "xor";
8259 do_bit:
8260 if (imm_expr.X_op == O_constant
8261 && imm_expr.X_add_number >= 0
8262 && imm_expr.X_add_number < 0x10000)
8263 {
8264 if (mask != M_NOR_I)
c0ebe874 8265 macro_build (&imm_expr, s, "t,r,i", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
8266 else
8267 {
67c0d1eb 8268 macro_build (&imm_expr, "ori", "t,r,i",
c0ebe874
RS
8269 op[0], op[1], BFD_RELOC_LO16);
8270 macro_build (NULL, "nor", "d,v,t", op[0], op[0], 0);
252b5132 8271 }
8fc2e39e 8272 break;
252b5132
RH
8273 }
8274
8fc2e39e 8275 used_at = 1;
67c0d1eb 8276 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 8277 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
8278 break;
8279
8b082fb1
TS
8280 case M_BALIGN:
8281 switch (imm_expr.X_add_number)
8282 {
8283 case 0:
8284 macro_build (NULL, "nop", "");
8285 break;
8286 case 2:
c0ebe874 8287 macro_build (NULL, "packrl.ph", "d,s,t", op[0], op[0], op[1]);
8b082fb1 8288 break;
03f66e8a
MR
8289 case 1:
8290 case 3:
c0ebe874 8291 macro_build (NULL, "balign", "t,s,2", op[0], op[1],
90ecf173 8292 (int) imm_expr.X_add_number);
8b082fb1 8293 break;
03f66e8a
MR
8294 default:
8295 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
8296 (unsigned long) imm_expr.X_add_number);
8297 break;
8b082fb1
TS
8298 }
8299 break;
8300
df58fc94
RS
8301 case M_BC1FL:
8302 case M_BC1TL:
8303 case M_BC2FL:
8304 case M_BC2TL:
8305 gas_assert (mips_opts.micromips);
8306 macro_build_branch_ccl (mask, &offset_expr,
8307 EXTRACT_OPERAND (1, BCC, *ip));
8308 break;
8309
252b5132 8310 case M_BEQ_I:
252b5132 8311 case M_BEQL_I:
252b5132 8312 case M_BNE_I:
252b5132 8313 case M_BNEL_I:
252b5132 8314 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
c0ebe874 8315 op[1] = 0;
df58fc94 8316 else
252b5132 8317 {
c0ebe874 8318 op[1] = AT;
df58fc94 8319 used_at = 1;
c0ebe874 8320 load_register (op[1], &imm_expr, HAVE_64BIT_GPRS);
252b5132 8321 }
df58fc94
RS
8322 /* Fall through. */
8323 case M_BEQL:
8324 case M_BNEL:
c0ebe874 8325 macro_build_branch_rsrt (mask, &offset_expr, op[0], op[1]);
252b5132
RH
8326 break;
8327
8328 case M_BGEL:
8329 likely = 1;
8330 case M_BGE:
c0ebe874
RS
8331 if (op[1] == 0)
8332 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[0]);
8333 else if (op[0] == 0)
8334 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[1]);
df58fc94 8335 else
252b5132 8336 {
df58fc94 8337 used_at = 1;
c0ebe874 8338 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
8339 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
8340 &offset_expr, AT, ZERO);
252b5132 8341 }
df58fc94
RS
8342 break;
8343
8344 case M_BGEZL:
8345 case M_BGEZALL:
8346 case M_BGTZL:
8347 case M_BLEZL:
8348 case M_BLTZL:
8349 case M_BLTZALL:
c0ebe874 8350 macro_build_branch_rs (mask, &offset_expr, op[0]);
252b5132
RH
8351 break;
8352
8353 case M_BGTL_I:
8354 likely = 1;
8355 case M_BGT_I:
90ecf173 8356 /* Check for > max integer. */
42429eac 8357 if (imm_expr.X_op == O_constant && imm_expr.X_add_number >= GPR_SMAX)
252b5132
RH
8358 {
8359 do_false:
90ecf173 8360 /* Result is always false. */
252b5132 8361 if (! likely)
a605d2b3 8362 macro_build (NULL, "nop", "");
252b5132 8363 else
df58fc94 8364 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
8fc2e39e 8365 break;
252b5132
RH
8366 }
8367 if (imm_expr.X_op != O_constant)
8368 as_bad (_("Unsupported large constant"));
f9419b05 8369 ++imm_expr.X_add_number;
252b5132
RH
8370 /* FALLTHROUGH */
8371 case M_BGE_I:
8372 case M_BGEL_I:
8373 if (mask == M_BGEL_I)
8374 likely = 1;
8375 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
8376 {
df58fc94 8377 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
c0ebe874 8378 &offset_expr, op[0]);
8fc2e39e 8379 break;
252b5132
RH
8380 }
8381 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
8382 {
df58fc94 8383 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
c0ebe874 8384 &offset_expr, op[0]);
8fc2e39e 8385 break;
252b5132 8386 }
42429eac 8387 if (imm_expr.X_op == O_constant && imm_expr.X_add_number <= GPR_SMIN)
252b5132
RH
8388 {
8389 do_true:
8390 /* result is always true */
8391 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
67c0d1eb 8392 macro_build (&offset_expr, "b", "p");
8fc2e39e 8393 break;
252b5132 8394 }
8fc2e39e 8395 used_at = 1;
c0ebe874 8396 set_at (op[0], 0);
df58fc94
RS
8397 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
8398 &offset_expr, AT, ZERO);
252b5132
RH
8399 break;
8400
8401 case M_BGEUL:
8402 likely = 1;
8403 case M_BGEU:
c0ebe874 8404 if (op[1] == 0)
252b5132 8405 goto do_true;
c0ebe874 8406 else if (op[0] == 0)
df58fc94 8407 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 8408 &offset_expr, ZERO, op[1]);
df58fc94 8409 else
252b5132 8410 {
df58fc94 8411 used_at = 1;
c0ebe874 8412 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
8413 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
8414 &offset_expr, AT, ZERO);
252b5132 8415 }
252b5132
RH
8416 break;
8417
8418 case M_BGTUL_I:
8419 likely = 1;
8420 case M_BGTU_I:
c0ebe874 8421 if (op[0] == 0
ca4e0257 8422 || (HAVE_32BIT_GPRS
252b5132 8423 && imm_expr.X_op == O_constant
f01dc953 8424 && imm_expr.X_add_number == -1))
252b5132
RH
8425 goto do_false;
8426 if (imm_expr.X_op != O_constant)
8427 as_bad (_("Unsupported large constant"));
f9419b05 8428 ++imm_expr.X_add_number;
252b5132
RH
8429 /* FALLTHROUGH */
8430 case M_BGEU_I:
8431 case M_BGEUL_I:
8432 if (mask == M_BGEUL_I)
8433 likely = 1;
8434 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
8435 goto do_true;
df58fc94
RS
8436 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
8437 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 8438 &offset_expr, op[0], ZERO);
df58fc94 8439 else
252b5132 8440 {
df58fc94 8441 used_at = 1;
c0ebe874 8442 set_at (op[0], 1);
df58fc94
RS
8443 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
8444 &offset_expr, AT, ZERO);
252b5132 8445 }
252b5132
RH
8446 break;
8447
8448 case M_BGTL:
8449 likely = 1;
8450 case M_BGT:
c0ebe874
RS
8451 if (op[1] == 0)
8452 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[0]);
8453 else if (op[0] == 0)
8454 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[1]);
df58fc94 8455 else
252b5132 8456 {
df58fc94 8457 used_at = 1;
c0ebe874 8458 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
8459 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
8460 &offset_expr, AT, ZERO);
252b5132 8461 }
252b5132
RH
8462 break;
8463
8464 case M_BGTUL:
8465 likely = 1;
8466 case M_BGTU:
c0ebe874 8467 if (op[1] == 0)
df58fc94 8468 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874
RS
8469 &offset_expr, op[0], ZERO);
8470 else if (op[0] == 0)
df58fc94
RS
8471 goto do_false;
8472 else
252b5132 8473 {
df58fc94 8474 used_at = 1;
c0ebe874 8475 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
8476 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
8477 &offset_expr, AT, ZERO);
252b5132 8478 }
252b5132
RH
8479 break;
8480
8481 case M_BLEL:
8482 likely = 1;
8483 case M_BLE:
c0ebe874
RS
8484 if (op[1] == 0)
8485 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
8486 else if (op[0] == 0)
8487 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[1]);
df58fc94 8488 else
252b5132 8489 {
df58fc94 8490 used_at = 1;
c0ebe874 8491 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
8492 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
8493 &offset_expr, AT, ZERO);
252b5132 8494 }
252b5132
RH
8495 break;
8496
8497 case M_BLEL_I:
8498 likely = 1;
8499 case M_BLE_I:
42429eac 8500 if (imm_expr.X_op == O_constant && imm_expr.X_add_number >= GPR_SMAX)
252b5132
RH
8501 goto do_true;
8502 if (imm_expr.X_op != O_constant)
8503 as_bad (_("Unsupported large constant"));
f9419b05 8504 ++imm_expr.X_add_number;
252b5132
RH
8505 /* FALLTHROUGH */
8506 case M_BLT_I:
8507 case M_BLTL_I:
8508 if (mask == M_BLTL_I)
8509 likely = 1;
8510 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
c0ebe874 8511 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
df58fc94 8512 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
c0ebe874 8513 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
df58fc94 8514 else
252b5132 8515 {
df58fc94 8516 used_at = 1;
c0ebe874 8517 set_at (op[0], 0);
df58fc94
RS
8518 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
8519 &offset_expr, AT, ZERO);
252b5132 8520 }
252b5132
RH
8521 break;
8522
8523 case M_BLEUL:
8524 likely = 1;
8525 case M_BLEU:
c0ebe874 8526 if (op[1] == 0)
df58fc94 8527 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874
RS
8528 &offset_expr, op[0], ZERO);
8529 else if (op[0] == 0)
df58fc94
RS
8530 goto do_true;
8531 else
252b5132 8532 {
df58fc94 8533 used_at = 1;
c0ebe874 8534 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
8535 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
8536 &offset_expr, AT, ZERO);
252b5132 8537 }
252b5132
RH
8538 break;
8539
8540 case M_BLEUL_I:
8541 likely = 1;
8542 case M_BLEU_I:
c0ebe874 8543 if (op[0] == 0
ca4e0257 8544 || (HAVE_32BIT_GPRS
252b5132 8545 && imm_expr.X_op == O_constant
f01dc953 8546 && imm_expr.X_add_number == -1))
252b5132
RH
8547 goto do_true;
8548 if (imm_expr.X_op != O_constant)
8549 as_bad (_("Unsupported large constant"));
f9419b05 8550 ++imm_expr.X_add_number;
252b5132
RH
8551 /* FALLTHROUGH */
8552 case M_BLTU_I:
8553 case M_BLTUL_I:
8554 if (mask == M_BLTUL_I)
8555 likely = 1;
8556 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
8557 goto do_false;
df58fc94
RS
8558 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
8559 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 8560 &offset_expr, op[0], ZERO);
df58fc94 8561 else
252b5132 8562 {
df58fc94 8563 used_at = 1;
c0ebe874 8564 set_at (op[0], 1);
df58fc94
RS
8565 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
8566 &offset_expr, AT, ZERO);
252b5132 8567 }
252b5132
RH
8568 break;
8569
8570 case M_BLTL:
8571 likely = 1;
8572 case M_BLT:
c0ebe874
RS
8573 if (op[1] == 0)
8574 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
8575 else if (op[0] == 0)
8576 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[1]);
df58fc94 8577 else
252b5132 8578 {
df58fc94 8579 used_at = 1;
c0ebe874 8580 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
8581 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
8582 &offset_expr, AT, ZERO);
252b5132 8583 }
252b5132
RH
8584 break;
8585
8586 case M_BLTUL:
8587 likely = 1;
8588 case M_BLTU:
c0ebe874 8589 if (op[1] == 0)
252b5132 8590 goto do_false;
c0ebe874 8591 else if (op[0] == 0)
df58fc94 8592 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 8593 &offset_expr, ZERO, op[1]);
df58fc94 8594 else
252b5132 8595 {
df58fc94 8596 used_at = 1;
c0ebe874 8597 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
8598 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
8599 &offset_expr, AT, ZERO);
252b5132 8600 }
252b5132
RH
8601 break;
8602
5f74bc13
CD
8603 case M_DEXT:
8604 {
d5818fca
MR
8605 /* Use unsigned arithmetic. */
8606 addressT pos;
8607 addressT size;
5f74bc13 8608
90ecf173 8609 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
5f74bc13
CD
8610 {
8611 as_bad (_("Unsupported large constant"));
8612 pos = size = 1;
8613 }
8614 else
8615 {
d5818fca
MR
8616 pos = imm_expr.X_add_number;
8617 size = imm2_expr.X_add_number;
5f74bc13
CD
8618 }
8619
8620 if (pos > 63)
8621 {
9e12b7a2 8622 report_bad_range (ip, 3, pos, 0, 63, FALSE);
5f74bc13
CD
8623 pos = 1;
8624 }
90ecf173 8625 if (size == 0 || size > 64 || (pos + size - 1) > 63)
5f74bc13 8626 {
9e12b7a2 8627 report_bad_field (pos, size);
5f74bc13
CD
8628 size = 1;
8629 }
8630
8631 if (size <= 32 && pos < 32)
8632 {
8633 s = "dext";
8634 fmt = "t,r,+A,+C";
8635 }
8636 else if (size <= 32)
8637 {
8638 s = "dextu";
8639 fmt = "t,r,+E,+H";
8640 }
8641 else
8642 {
8643 s = "dextm";
8644 fmt = "t,r,+A,+G";
8645 }
c0ebe874 8646 macro_build ((expressionS *) NULL, s, fmt, op[0], op[1], (int) pos,
d5818fca 8647 (int) (size - 1));
5f74bc13 8648 }
8fc2e39e 8649 break;
5f74bc13
CD
8650
8651 case M_DINS:
8652 {
d5818fca
MR
8653 /* Use unsigned arithmetic. */
8654 addressT pos;
8655 addressT size;
5f74bc13 8656
90ecf173 8657 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
5f74bc13
CD
8658 {
8659 as_bad (_("Unsupported large constant"));
8660 pos = size = 1;
8661 }
8662 else
8663 {
d5818fca
MR
8664 pos = imm_expr.X_add_number;
8665 size = imm2_expr.X_add_number;
5f74bc13
CD
8666 }
8667
8668 if (pos > 63)
8669 {
9e12b7a2 8670 report_bad_range (ip, 3, pos, 0, 63, FALSE);
5f74bc13
CD
8671 pos = 1;
8672 }
90ecf173 8673 if (size == 0 || size > 64 || (pos + size - 1) > 63)
5f74bc13 8674 {
9e12b7a2 8675 report_bad_field (pos, size);
5f74bc13
CD
8676 size = 1;
8677 }
8678
8679 if (pos < 32 && (pos + size - 1) < 32)
8680 {
8681 s = "dins";
8682 fmt = "t,r,+A,+B";
8683 }
8684 else if (pos >= 32)
8685 {
8686 s = "dinsu";
8687 fmt = "t,r,+E,+F";
8688 }
8689 else
8690 {
8691 s = "dinsm";
8692 fmt = "t,r,+A,+F";
8693 }
c0ebe874 8694 macro_build ((expressionS *) NULL, s, fmt, op[0], op[1], (int) pos,
750bdd57 8695 (int) (pos + size - 1));
5f74bc13 8696 }
8fc2e39e 8697 break;
5f74bc13 8698
252b5132
RH
8699 case M_DDIV_3:
8700 dbl = 1;
8701 case M_DIV_3:
8702 s = "mflo";
8703 goto do_div3;
8704 case M_DREM_3:
8705 dbl = 1;
8706 case M_REM_3:
8707 s = "mfhi";
8708 do_div3:
c0ebe874 8709 if (op[2] == 0)
252b5132
RH
8710 {
8711 as_warn (_("Divide by zero."));
8712 if (mips_trap)
df58fc94 8713 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 8714 else
df58fc94 8715 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 8716 break;
252b5132
RH
8717 }
8718
7d10b47d 8719 start_noreorder ();
252b5132
RH
8720 if (mips_trap)
8721 {
c0ebe874
RS
8722 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
8723 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
252b5132
RH
8724 }
8725 else
8726 {
df58fc94
RS
8727 if (mips_opts.micromips)
8728 micromips_label_expr (&label_expr);
8729 else
8730 label_expr.X_add_number = 8;
c0ebe874
RS
8731 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
8732 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
df58fc94
RS
8733 macro_build (NULL, "break", BRK_FMT, 7);
8734 if (mips_opts.micromips)
8735 micromips_add_label ();
252b5132
RH
8736 }
8737 expr1.X_add_number = -1;
8fc2e39e 8738 used_at = 1;
f6a22291 8739 load_register (AT, &expr1, dbl);
df58fc94
RS
8740 if (mips_opts.micromips)
8741 micromips_label_expr (&label_expr);
8742 else
8743 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
c0ebe874 8744 macro_build (&label_expr, "bne", "s,t,p", op[2], AT);
252b5132
RH
8745 if (dbl)
8746 {
8747 expr1.X_add_number = 1;
f6a22291 8748 load_register (AT, &expr1, dbl);
df58fc94 8749 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
252b5132
RH
8750 }
8751 else
8752 {
8753 expr1.X_add_number = 0x80000000;
df58fc94 8754 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
252b5132
RH
8755 }
8756 if (mips_trap)
8757 {
c0ebe874 8758 macro_build (NULL, "teq", TRAP_FMT, op[1], AT, 6);
252b5132
RH
8759 /* We want to close the noreorder block as soon as possible, so
8760 that later insns are available for delay slot filling. */
7d10b47d 8761 end_noreorder ();
252b5132
RH
8762 }
8763 else
8764 {
df58fc94
RS
8765 if (mips_opts.micromips)
8766 micromips_label_expr (&label_expr);
8767 else
8768 label_expr.X_add_number = 8;
c0ebe874 8769 macro_build (&label_expr, "bne", "s,t,p", op[1], AT);
a605d2b3 8770 macro_build (NULL, "nop", "");
252b5132
RH
8771
8772 /* We want to close the noreorder block as soon as possible, so
8773 that later insns are available for delay slot filling. */
7d10b47d 8774 end_noreorder ();
252b5132 8775
df58fc94 8776 macro_build (NULL, "break", BRK_FMT, 6);
252b5132 8777 }
df58fc94
RS
8778 if (mips_opts.micromips)
8779 micromips_add_label ();
c0ebe874 8780 macro_build (NULL, s, MFHL_FMT, op[0]);
252b5132
RH
8781 break;
8782
8783 case M_DIV_3I:
8784 s = "div";
8785 s2 = "mflo";
8786 goto do_divi;
8787 case M_DIVU_3I:
8788 s = "divu";
8789 s2 = "mflo";
8790 goto do_divi;
8791 case M_REM_3I:
8792 s = "div";
8793 s2 = "mfhi";
8794 goto do_divi;
8795 case M_REMU_3I:
8796 s = "divu";
8797 s2 = "mfhi";
8798 goto do_divi;
8799 case M_DDIV_3I:
8800 dbl = 1;
8801 s = "ddiv";
8802 s2 = "mflo";
8803 goto do_divi;
8804 case M_DDIVU_3I:
8805 dbl = 1;
8806 s = "ddivu";
8807 s2 = "mflo";
8808 goto do_divi;
8809 case M_DREM_3I:
8810 dbl = 1;
8811 s = "ddiv";
8812 s2 = "mfhi";
8813 goto do_divi;
8814 case M_DREMU_3I:
8815 dbl = 1;
8816 s = "ddivu";
8817 s2 = "mfhi";
8818 do_divi:
8819 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
8820 {
8821 as_warn (_("Divide by zero."));
8822 if (mips_trap)
df58fc94 8823 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 8824 else
df58fc94 8825 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 8826 break;
252b5132
RH
8827 }
8828 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
8829 {
8830 if (strcmp (s2, "mflo") == 0)
c0ebe874 8831 move_register (op[0], op[1]);
252b5132 8832 else
c0ebe874 8833 move_register (op[0], ZERO);
8fc2e39e 8834 break;
252b5132
RH
8835 }
8836 if (imm_expr.X_op == O_constant
8837 && imm_expr.X_add_number == -1
8838 && s[strlen (s) - 1] != 'u')
8839 {
8840 if (strcmp (s2, "mflo") == 0)
c0ebe874 8841 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", op[0], op[1]);
252b5132 8842 else
c0ebe874 8843 move_register (op[0], ZERO);
8fc2e39e 8844 break;
252b5132
RH
8845 }
8846
8fc2e39e 8847 used_at = 1;
67c0d1eb 8848 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
8849 macro_build (NULL, s, "z,s,t", op[1], AT);
8850 macro_build (NULL, s2, MFHL_FMT, op[0]);
252b5132
RH
8851 break;
8852
8853 case M_DIVU_3:
8854 s = "divu";
8855 s2 = "mflo";
8856 goto do_divu3;
8857 case M_REMU_3:
8858 s = "divu";
8859 s2 = "mfhi";
8860 goto do_divu3;
8861 case M_DDIVU_3:
8862 s = "ddivu";
8863 s2 = "mflo";
8864 goto do_divu3;
8865 case M_DREMU_3:
8866 s = "ddivu";
8867 s2 = "mfhi";
8868 do_divu3:
7d10b47d 8869 start_noreorder ();
252b5132
RH
8870 if (mips_trap)
8871 {
c0ebe874
RS
8872 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
8873 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
8874 /* We want to close the noreorder block as soon as possible, so
8875 that later insns are available for delay slot filling. */
7d10b47d 8876 end_noreorder ();
252b5132
RH
8877 }
8878 else
8879 {
df58fc94
RS
8880 if (mips_opts.micromips)
8881 micromips_label_expr (&label_expr);
8882 else
8883 label_expr.X_add_number = 8;
c0ebe874
RS
8884 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
8885 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
8886
8887 /* We want to close the noreorder block as soon as possible, so
8888 that later insns are available for delay slot filling. */
7d10b47d 8889 end_noreorder ();
df58fc94
RS
8890 macro_build (NULL, "break", BRK_FMT, 7);
8891 if (mips_opts.micromips)
8892 micromips_add_label ();
252b5132 8893 }
c0ebe874 8894 macro_build (NULL, s2, MFHL_FMT, op[0]);
8fc2e39e 8895 break;
252b5132 8896
1abe91b1
MR
8897 case M_DLCA_AB:
8898 dbl = 1;
8899 case M_LCA_AB:
8900 call = 1;
8901 goto do_la;
252b5132
RH
8902 case M_DLA_AB:
8903 dbl = 1;
8904 case M_LA_AB:
1abe91b1 8905 do_la:
252b5132
RH
8906 /* Load the address of a symbol into a register. If breg is not
8907 zero, we then add a base register to it. */
8908
c0ebe874 8909 breg = op[2];
3bec30a8
TS
8910 if (dbl && HAVE_32BIT_GPRS)
8911 as_warn (_("dla used to load 32-bit register"));
8912
90ecf173 8913 if (!dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
8914 as_warn (_("la used to load 64-bit address"));
8915
f2ae14a1 8916 if (small_offset_p (0, align, 16))
0c11417f 8917 {
c0ebe874 8918 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", op[0], breg,
f2ae14a1 8919 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8fc2e39e 8920 break;
0c11417f
MR
8921 }
8922
c0ebe874 8923 if (mips_opts.at && (op[0] == breg))
afdbd6d0
CD
8924 {
8925 tempreg = AT;
8926 used_at = 1;
8927 }
8928 else
c0ebe874 8929 tempreg = op[0];
afdbd6d0 8930
252b5132
RH
8931 if (offset_expr.X_op != O_symbol
8932 && offset_expr.X_op != O_constant)
8933 {
f71d0d44 8934 as_bad (_("Expression too complex"));
252b5132
RH
8935 offset_expr.X_op = O_constant;
8936 }
8937
252b5132 8938 if (offset_expr.X_op == O_constant)
aed1a261 8939 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
8940 else if (mips_pic == NO_PIC)
8941 {
d6bc6245 8942 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 8943 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
8944 Otherwise we want
8945 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
8946 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
8947 If we have a constant, we need two instructions anyhow,
d6bc6245 8948 so we may as well always use the latter form.
76b3015f 8949
6caf9ef4
TS
8950 With 64bit address space and a usable $at we want
8951 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8952 lui $at,<sym> (BFD_RELOC_HI16_S)
8953 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8954 daddiu $at,<sym> (BFD_RELOC_LO16)
8955 dsll32 $tempreg,0
8956 daddu $tempreg,$tempreg,$at
8957
8958 If $at is already in use, we use a path which is suboptimal
8959 on superscalar processors.
8960 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8961 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8962 dsll $tempreg,16
8963 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
8964 dsll $tempreg,16
8965 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
8966
8967 For GP relative symbols in 64bit address space we can use
8968 the same sequence as in 32bit address space. */
aed1a261 8969 if (HAVE_64BIT_SYMBOLS)
252b5132 8970 {
6caf9ef4
TS
8971 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
8972 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
8973 {
8974 relax_start (offset_expr.X_add_symbol);
8975 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8976 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
8977 relax_switch ();
8978 }
d6bc6245 8979
741fe287 8980 if (used_at == 0 && mips_opts.at)
98d3f06f 8981 {
df58fc94 8982 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 8983 tempreg, BFD_RELOC_MIPS_HIGHEST);
df58fc94 8984 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 8985 AT, BFD_RELOC_HI16_S);
67c0d1eb 8986 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 8987 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 8988 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 8989 AT, AT, BFD_RELOC_LO16);
df58fc94 8990 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 8991 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
8992 used_at = 1;
8993 }
8994 else
8995 {
df58fc94 8996 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 8997 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 8998 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 8999 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 9000 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 9001 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 9002 tempreg, tempreg, BFD_RELOC_HI16_S);
df58fc94 9003 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 9004 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 9005 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 9006 }
6caf9ef4
TS
9007
9008 if (mips_relax.sequence)
9009 relax_end ();
98d3f06f
KH
9010 }
9011 else
9012 {
9013 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 9014 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 9015 {
4d7206a2 9016 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
9017 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
9018 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 9019 relax_switch ();
98d3f06f 9020 }
6943caf0 9021 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
f71d0d44 9022 as_bad (_("Offset too large"));
67c0d1eb
RS
9023 macro_build_lui (&offset_expr, tempreg);
9024 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
9025 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
9026 if (mips_relax.sequence)
9027 relax_end ();
98d3f06f 9028 }
252b5132 9029 }
0a44bf69 9030 else if (!mips_big_got && !HAVE_NEWABI)
252b5132 9031 {
9117d219
NC
9032 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
9033
252b5132
RH
9034 /* If this is a reference to an external symbol, and there
9035 is no constant, we want
9036 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 9037 or for lca or if tempreg is PIC_CALL_REG
9117d219 9038 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
9039 For a local symbol, we want
9040 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9041 nop
9042 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
9043
9044 If we have a small constant, and this is a reference to
9045 an external symbol, we want
9046 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9047 nop
9048 addiu $tempreg,$tempreg,<constant>
9049 For a local symbol, we want the same instruction
9050 sequence, but we output a BFD_RELOC_LO16 reloc on the
9051 addiu instruction.
9052
9053 If we have a large constant, and this is a reference to
9054 an external symbol, we want
9055 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9056 lui $at,<hiconstant>
9057 addiu $at,$at,<loconstant>
9058 addu $tempreg,$tempreg,$at
9059 For a local symbol, we want the same instruction
9060 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 9061 addiu instruction.
ed6fb7bd
SC
9062 */
9063
4d7206a2 9064 if (offset_expr.X_add_number == 0)
252b5132 9065 {
0a44bf69
RS
9066 if (mips_pic == SVR4_PIC
9067 && breg == 0
9068 && (call || tempreg == PIC_CALL_REG))
4d7206a2
RS
9069 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
9070
9071 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
9072 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9073 lw_reloc_type, mips_gp_register);
4d7206a2 9074 if (breg != 0)
252b5132
RH
9075 {
9076 /* We're going to put in an addu instruction using
9077 tempreg, so we may as well insert the nop right
9078 now. */
269137b2 9079 load_delay_nop ();
252b5132 9080 }
4d7206a2 9081 relax_switch ();
67c0d1eb
RS
9082 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9083 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9084 load_delay_nop ();
67c0d1eb
RS
9085 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
9086 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 9087 relax_end ();
252b5132
RH
9088 /* FIXME: If breg == 0, and the next instruction uses
9089 $tempreg, then if this variant case is used an extra
9090 nop will be generated. */
9091 }
4d7206a2
RS
9092 else if (offset_expr.X_add_number >= -0x8000
9093 && offset_expr.X_add_number < 0x8000)
252b5132 9094 {
67c0d1eb 9095 load_got_offset (tempreg, &offset_expr);
269137b2 9096 load_delay_nop ();
67c0d1eb 9097 add_got_offset (tempreg, &offset_expr);
252b5132
RH
9098 }
9099 else
9100 {
4d7206a2
RS
9101 expr1.X_add_number = offset_expr.X_add_number;
9102 offset_expr.X_add_number =
43c0598f 9103 SEXT_16BIT (offset_expr.X_add_number);
67c0d1eb 9104 load_got_offset (tempreg, &offset_expr);
f6a22291 9105 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
9106 /* If we are going to add in a base register, and the
9107 target register and the base register are the same,
9108 then we are using AT as a temporary register. Since
9109 we want to load the constant into AT, we add our
9110 current AT (from the global offset table) and the
9111 register into the register now, and pretend we were
9112 not using a base register. */
c0ebe874 9113 if (breg == op[0])
252b5132 9114 {
269137b2 9115 load_delay_nop ();
67c0d1eb 9116 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 9117 op[0], AT, breg);
252b5132 9118 breg = 0;
c0ebe874 9119 tempreg = op[0];
252b5132 9120 }
f6a22291 9121 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
9122 used_at = 1;
9123 }
9124 }
0a44bf69 9125 else if (!mips_big_got && HAVE_NEWABI)
f5040a92 9126 {
67c0d1eb 9127 int add_breg_early = 0;
f5040a92
AO
9128
9129 /* If this is a reference to an external, and there is no
9130 constant, or local symbol (*), with or without a
9131 constant, we want
9132 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 9133 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
9134 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
9135
9136 If we have a small constant, and this is a reference to
9137 an external symbol, we want
9138 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
9139 addiu $tempreg,$tempreg,<constant>
9140
9141 If we have a large constant, and this is a reference to
9142 an external symbol, we want
9143 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
9144 lui $at,<hiconstant>
9145 addiu $at,$at,<loconstant>
9146 addu $tempreg,$tempreg,$at
9147
9148 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
9149 local symbols, even though it introduces an additional
9150 instruction. */
9151
f5040a92
AO
9152 if (offset_expr.X_add_number)
9153 {
4d7206a2 9154 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
9155 offset_expr.X_add_number = 0;
9156
4d7206a2 9157 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
9158 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9159 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
9160
9161 if (expr1.X_add_number >= -0x8000
9162 && expr1.X_add_number < 0x8000)
9163 {
67c0d1eb
RS
9164 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
9165 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 9166 }
ecd13cd3 9167 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 9168 {
c0ebe874
RS
9169 unsigned int dreg;
9170
f5040a92
AO
9171 /* If we are going to add in a base register, and the
9172 target register and the base register are the same,
9173 then we are using AT as a temporary register. Since
9174 we want to load the constant into AT, we add our
9175 current AT (from the global offset table) and the
9176 register into the register now, and pretend we were
9177 not using a base register. */
c0ebe874 9178 if (breg != op[0])
f5040a92
AO
9179 dreg = tempreg;
9180 else
9181 {
9c2799c2 9182 gas_assert (tempreg == AT);
67c0d1eb 9183 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
9184 op[0], AT, breg);
9185 dreg = op[0];
67c0d1eb 9186 add_breg_early = 1;
f5040a92
AO
9187 }
9188
f6a22291 9189 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 9190 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9191 dreg, dreg, AT);
f5040a92 9192
f5040a92
AO
9193 used_at = 1;
9194 }
9195 else
9196 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
9197
4d7206a2 9198 relax_switch ();
f5040a92
AO
9199 offset_expr.X_add_number = expr1.X_add_number;
9200
67c0d1eb
RS
9201 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9202 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
9203 if (add_breg_early)
f5040a92 9204 {
67c0d1eb 9205 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 9206 op[0], tempreg, breg);
f5040a92 9207 breg = 0;
c0ebe874 9208 tempreg = op[0];
f5040a92 9209 }
4d7206a2 9210 relax_end ();
f5040a92 9211 }
4d7206a2 9212 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 9213 {
4d7206a2 9214 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
9215 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9216 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 9217 relax_switch ();
67c0d1eb
RS
9218 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9219 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 9220 relax_end ();
f5040a92 9221 }
4d7206a2 9222 else
f5040a92 9223 {
67c0d1eb
RS
9224 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9225 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
9226 }
9227 }
0a44bf69 9228 else if (mips_big_got && !HAVE_NEWABI)
252b5132 9229 {
67c0d1eb 9230 int gpdelay;
9117d219
NC
9231 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
9232 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 9233 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
9234
9235 /* This is the large GOT case. If this is a reference to an
9236 external symbol, and there is no constant, we want
9237 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9238 addu $tempreg,$tempreg,$gp
9239 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 9240 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
9241 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
9242 addu $tempreg,$tempreg,$gp
9243 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
9244 For a local symbol, we want
9245 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9246 nop
9247 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
9248
9249 If we have a small constant, and this is a reference to
9250 an external symbol, we want
9251 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9252 addu $tempreg,$tempreg,$gp
9253 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9254 nop
9255 addiu $tempreg,$tempreg,<constant>
9256 For a local symbol, we want
9257 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9258 nop
9259 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
9260
9261 If we have a large constant, and this is a reference to
9262 an external symbol, we want
9263 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9264 addu $tempreg,$tempreg,$gp
9265 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9266 lui $at,<hiconstant>
9267 addiu $at,$at,<loconstant>
9268 addu $tempreg,$tempreg,$at
9269 For a local symbol, we want
9270 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9271 lui $at,<hiconstant>
9272 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
9273 addu $tempreg,$tempreg,$at
f5040a92 9274 */
438c16b8 9275
252b5132
RH
9276 expr1.X_add_number = offset_expr.X_add_number;
9277 offset_expr.X_add_number = 0;
4d7206a2 9278 relax_start (offset_expr.X_add_symbol);
67c0d1eb 9279 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
9280 if (expr1.X_add_number == 0 && breg == 0
9281 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
9282 {
9283 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
9284 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
9285 }
df58fc94 9286 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 9287 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9288 tempreg, tempreg, mips_gp_register);
67c0d1eb 9289 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 9290 tempreg, lw_reloc_type, tempreg);
252b5132
RH
9291 if (expr1.X_add_number == 0)
9292 {
67c0d1eb 9293 if (breg != 0)
252b5132
RH
9294 {
9295 /* We're going to put in an addu instruction using
9296 tempreg, so we may as well insert the nop right
9297 now. */
269137b2 9298 load_delay_nop ();
252b5132 9299 }
252b5132
RH
9300 }
9301 else if (expr1.X_add_number >= -0x8000
9302 && expr1.X_add_number < 0x8000)
9303 {
269137b2 9304 load_delay_nop ();
67c0d1eb 9305 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 9306 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
9307 }
9308 else
9309 {
c0ebe874
RS
9310 unsigned int dreg;
9311
252b5132
RH
9312 /* If we are going to add in a base register, and the
9313 target register and the base register are the same,
9314 then we are using AT as a temporary register. Since
9315 we want to load the constant into AT, we add our
9316 current AT (from the global offset table) and the
9317 register into the register now, and pretend we were
9318 not using a base register. */
c0ebe874 9319 if (breg != op[0])
67c0d1eb 9320 dreg = tempreg;
252b5132
RH
9321 else
9322 {
9c2799c2 9323 gas_assert (tempreg == AT);
269137b2 9324 load_delay_nop ();
67c0d1eb 9325 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
9326 op[0], AT, breg);
9327 dreg = op[0];
252b5132
RH
9328 }
9329
f6a22291 9330 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 9331 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 9332
252b5132
RH
9333 used_at = 1;
9334 }
43c0598f 9335 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
4d7206a2 9336 relax_switch ();
252b5132 9337
67c0d1eb 9338 if (gpdelay)
252b5132
RH
9339 {
9340 /* This is needed because this instruction uses $gp, but
f5040a92 9341 the first instruction on the main stream does not. */
67c0d1eb 9342 macro_build (NULL, "nop", "");
252b5132 9343 }
ed6fb7bd 9344
67c0d1eb
RS
9345 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9346 local_reloc_type, mips_gp_register);
f5040a92 9347 if (expr1.X_add_number >= -0x8000
252b5132
RH
9348 && expr1.X_add_number < 0x8000)
9349 {
269137b2 9350 load_delay_nop ();
67c0d1eb
RS
9351 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
9352 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 9353 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
9354 register, the external symbol case ended with a load,
9355 so if the symbol turns out to not be external, and
9356 the next instruction uses tempreg, an unnecessary nop
9357 will be inserted. */
252b5132
RH
9358 }
9359 else
9360 {
c0ebe874 9361 if (breg == op[0])
252b5132
RH
9362 {
9363 /* We must add in the base register now, as in the
f5040a92 9364 external symbol case. */
9c2799c2 9365 gas_assert (tempreg == AT);
269137b2 9366 load_delay_nop ();
67c0d1eb 9367 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
9368 op[0], AT, breg);
9369 tempreg = op[0];
252b5132 9370 /* We set breg to 0 because we have arranged to add
f5040a92 9371 it in in both cases. */
252b5132
RH
9372 breg = 0;
9373 }
9374
67c0d1eb
RS
9375 macro_build_lui (&expr1, AT);
9376 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 9377 AT, AT, BFD_RELOC_LO16);
67c0d1eb 9378 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9379 tempreg, tempreg, AT);
8fc2e39e 9380 used_at = 1;
252b5132 9381 }
4d7206a2 9382 relax_end ();
252b5132 9383 }
0a44bf69 9384 else if (mips_big_got && HAVE_NEWABI)
f5040a92 9385 {
f5040a92
AO
9386 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
9387 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 9388 int add_breg_early = 0;
f5040a92
AO
9389
9390 /* This is the large GOT case. If this is a reference to an
9391 external symbol, and there is no constant, we want
9392 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9393 add $tempreg,$tempreg,$gp
9394 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 9395 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
9396 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
9397 add $tempreg,$tempreg,$gp
9398 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
9399
9400 If we have a small constant, and this is a reference to
9401 an external symbol, we want
9402 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9403 add $tempreg,$tempreg,$gp
9404 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9405 addi $tempreg,$tempreg,<constant>
9406
9407 If we have a large constant, and this is a reference to
9408 an external symbol, we want
9409 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9410 addu $tempreg,$tempreg,$gp
9411 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9412 lui $at,<hiconstant>
9413 addi $at,$at,<loconstant>
9414 add $tempreg,$tempreg,$at
9415
9416 If we have NewABI, and we know it's a local symbol, we want
9417 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
9418 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
9419 otherwise we have to resort to GOT_HI16/GOT_LO16. */
9420
4d7206a2 9421 relax_start (offset_expr.X_add_symbol);
f5040a92 9422
4d7206a2 9423 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
9424 offset_expr.X_add_number = 0;
9425
1abe91b1
MR
9426 if (expr1.X_add_number == 0 && breg == 0
9427 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
9428 {
9429 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
9430 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
9431 }
df58fc94 9432 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 9433 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9434 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
9435 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9436 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
9437
9438 if (expr1.X_add_number == 0)
4d7206a2 9439 ;
f5040a92
AO
9440 else if (expr1.X_add_number >= -0x8000
9441 && expr1.X_add_number < 0x8000)
9442 {
67c0d1eb 9443 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 9444 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 9445 }
ecd13cd3 9446 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 9447 {
c0ebe874
RS
9448 unsigned int dreg;
9449
f5040a92
AO
9450 /* If we are going to add in a base register, and the
9451 target register and the base register are the same,
9452 then we are using AT as a temporary register. Since
9453 we want to load the constant into AT, we add our
9454 current AT (from the global offset table) and the
9455 register into the register now, and pretend we were
9456 not using a base register. */
c0ebe874 9457 if (breg != op[0])
f5040a92
AO
9458 dreg = tempreg;
9459 else
9460 {
9c2799c2 9461 gas_assert (tempreg == AT);
67c0d1eb 9462 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
9463 op[0], AT, breg);
9464 dreg = op[0];
67c0d1eb 9465 add_breg_early = 1;
f5040a92
AO
9466 }
9467
f6a22291 9468 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 9469 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 9470
f5040a92
AO
9471 used_at = 1;
9472 }
9473 else
9474 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
9475
4d7206a2 9476 relax_switch ();
f5040a92 9477 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
9478 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9479 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
9480 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
9481 tempreg, BFD_RELOC_MIPS_GOT_OFST);
9482 if (add_breg_early)
f5040a92 9483 {
67c0d1eb 9484 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 9485 op[0], tempreg, breg);
f5040a92 9486 breg = 0;
c0ebe874 9487 tempreg = op[0];
f5040a92 9488 }
4d7206a2 9489 relax_end ();
f5040a92 9490 }
252b5132
RH
9491 else
9492 abort ();
9493
9494 if (breg != 0)
c0ebe874 9495 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", op[0], tempreg, breg);
252b5132
RH
9496 break;
9497
52b6b6b9 9498 case M_MSGSND:
df58fc94 9499 gas_assert (!mips_opts.micromips);
c0ebe874 9500 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x01);
c7af4273 9501 break;
52b6b6b9
JM
9502
9503 case M_MSGLD:
df58fc94 9504 gas_assert (!mips_opts.micromips);
c8276761 9505 macro_build (NULL, "c2", "C", 0x02);
c7af4273 9506 break;
52b6b6b9
JM
9507
9508 case M_MSGLD_T:
df58fc94 9509 gas_assert (!mips_opts.micromips);
c0ebe874 9510 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x02);
c7af4273 9511 break;
52b6b6b9
JM
9512
9513 case M_MSGWAIT:
df58fc94 9514 gas_assert (!mips_opts.micromips);
52b6b6b9 9515 macro_build (NULL, "c2", "C", 3);
c7af4273 9516 break;
52b6b6b9
JM
9517
9518 case M_MSGWAIT_T:
df58fc94 9519 gas_assert (!mips_opts.micromips);
c0ebe874 9520 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x03);
c7af4273 9521 break;
52b6b6b9 9522
252b5132
RH
9523 case M_J_A:
9524 /* The j instruction may not be used in PIC code, since it
9525 requires an absolute address. We convert it to a b
9526 instruction. */
9527 if (mips_pic == NO_PIC)
67c0d1eb 9528 macro_build (&offset_expr, "j", "a");
252b5132 9529 else
67c0d1eb 9530 macro_build (&offset_expr, "b", "p");
8fc2e39e 9531 break;
252b5132
RH
9532
9533 /* The jal instructions must be handled as macros because when
9534 generating PIC code they expand to multi-instruction
9535 sequences. Normally they are simple instructions. */
df58fc94 9536 case M_JALS_1:
c0ebe874
RS
9537 op[1] = op[0];
9538 op[0] = RA;
df58fc94
RS
9539 /* Fall through. */
9540 case M_JALS_2:
9541 gas_assert (mips_opts.micromips);
833794fc
MR
9542 if (mips_opts.insn32)
9543 {
9544 as_bad (_("Opcode not supported in the `insn32' mode `%s'"), str);
9545 break;
9546 }
df58fc94
RS
9547 jals = 1;
9548 goto jal;
252b5132 9549 case M_JAL_1:
c0ebe874
RS
9550 op[1] = op[0];
9551 op[0] = RA;
252b5132
RH
9552 /* Fall through. */
9553 case M_JAL_2:
df58fc94 9554 jal:
3e722fb5 9555 if (mips_pic == NO_PIC)
df58fc94
RS
9556 {
9557 s = jals ? "jalrs" : "jalr";
e64af278 9558 if (mips_opts.micromips
833794fc 9559 && !mips_opts.insn32
c0ebe874 9560 && op[0] == RA
e64af278 9561 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 9562 macro_build (NULL, s, "mj", op[1]);
df58fc94 9563 else
c0ebe874 9564 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
df58fc94 9565 }
0a44bf69 9566 else
252b5132 9567 {
df58fc94
RS
9568 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
9569 && mips_cprestore_offset >= 0);
9570
c0ebe874 9571 if (op[1] != PIC_CALL_REG)
252b5132 9572 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 9573
833794fc
MR
9574 s = ((mips_opts.micromips
9575 && !mips_opts.insn32
9576 && (!mips_opts.noreorder || cprestore))
df58fc94 9577 ? "jalrs" : "jalr");
e64af278 9578 if (mips_opts.micromips
833794fc 9579 && !mips_opts.insn32
c0ebe874 9580 && op[0] == RA
e64af278 9581 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 9582 macro_build (NULL, s, "mj", op[1]);
df58fc94 9583 else
c0ebe874 9584 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
0a44bf69 9585 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
252b5132 9586 {
6478892d
TS
9587 if (mips_cprestore_offset < 0)
9588 as_warn (_("No .cprestore pseudo-op used in PIC code"));
9589 else
9590 {
90ecf173 9591 if (!mips_frame_reg_valid)
7a621144
DJ
9592 {
9593 as_warn (_("No .frame pseudo-op used in PIC code"));
9594 /* Quiet this warning. */
9595 mips_frame_reg_valid = 1;
9596 }
90ecf173 9597 if (!mips_cprestore_valid)
7a621144
DJ
9598 {
9599 as_warn (_("No .cprestore pseudo-op used in PIC code"));
9600 /* Quiet this warning. */
9601 mips_cprestore_valid = 1;
9602 }
d3fca0b5
MR
9603 if (mips_opts.noreorder)
9604 macro_build (NULL, "nop", "");
6478892d 9605 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 9606 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 9607 mips_gp_register,
256ab948
TS
9608 mips_frame_reg,
9609 HAVE_64BIT_ADDRESSES);
6478892d 9610 }
252b5132
RH
9611 }
9612 }
252b5132 9613
8fc2e39e 9614 break;
252b5132 9615
df58fc94
RS
9616 case M_JALS_A:
9617 gas_assert (mips_opts.micromips);
833794fc
MR
9618 if (mips_opts.insn32)
9619 {
9620 as_bad (_("Opcode not supported in the `insn32' mode `%s'"), str);
9621 break;
9622 }
df58fc94
RS
9623 jals = 1;
9624 /* Fall through. */
252b5132
RH
9625 case M_JAL_A:
9626 if (mips_pic == NO_PIC)
df58fc94 9627 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
252b5132
RH
9628 else if (mips_pic == SVR4_PIC)
9629 {
9630 /* If this is a reference to an external symbol, and we are
9631 using a small GOT, we want
9632 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
9633 nop
f9419b05 9634 jalr $ra,$25
252b5132
RH
9635 nop
9636 lw $gp,cprestore($sp)
9637 The cprestore value is set using the .cprestore
9638 pseudo-op. If we are using a big GOT, we want
9639 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
9640 addu $25,$25,$gp
9641 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
9642 nop
f9419b05 9643 jalr $ra,$25
252b5132
RH
9644 nop
9645 lw $gp,cprestore($sp)
9646 If the symbol is not external, we want
9647 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9648 nop
9649 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 9650 jalr $ra,$25
252b5132 9651 nop
438c16b8 9652 lw $gp,cprestore($sp)
f5040a92
AO
9653
9654 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
9655 sequences above, minus nops, unless the symbol is local,
9656 which enables us to use GOT_PAGE/GOT_OFST (big got) or
9657 GOT_DISP. */
438c16b8 9658 if (HAVE_NEWABI)
252b5132 9659 {
90ecf173 9660 if (!mips_big_got)
f5040a92 9661 {
4d7206a2 9662 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
9663 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9664 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 9665 mips_gp_register);
4d7206a2 9666 relax_switch ();
67c0d1eb
RS
9667 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9668 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
9669 mips_gp_register);
9670 relax_end ();
f5040a92
AO
9671 }
9672 else
9673 {
4d7206a2 9674 relax_start (offset_expr.X_add_symbol);
df58fc94 9675 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
9676 BFD_RELOC_MIPS_CALL_HI16);
9677 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
9678 PIC_CALL_REG, mips_gp_register);
9679 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9680 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
9681 PIC_CALL_REG);
4d7206a2 9682 relax_switch ();
67c0d1eb
RS
9683 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9684 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
9685 mips_gp_register);
9686 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
9687 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 9688 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 9689 relax_end ();
f5040a92 9690 }
684022ea 9691
df58fc94 9692 macro_build_jalr (&offset_expr, 0);
252b5132
RH
9693 }
9694 else
9695 {
4d7206a2 9696 relax_start (offset_expr.X_add_symbol);
90ecf173 9697 if (!mips_big_got)
438c16b8 9698 {
67c0d1eb
RS
9699 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9700 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 9701 mips_gp_register);
269137b2 9702 load_delay_nop ();
4d7206a2 9703 relax_switch ();
438c16b8 9704 }
252b5132 9705 else
252b5132 9706 {
67c0d1eb
RS
9707 int gpdelay;
9708
9709 gpdelay = reg_needs_delay (mips_gp_register);
df58fc94 9710 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
9711 BFD_RELOC_MIPS_CALL_HI16);
9712 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
9713 PIC_CALL_REG, mips_gp_register);
9714 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9715 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
9716 PIC_CALL_REG);
269137b2 9717 load_delay_nop ();
4d7206a2 9718 relax_switch ();
67c0d1eb
RS
9719 if (gpdelay)
9720 macro_build (NULL, "nop", "");
252b5132 9721 }
67c0d1eb
RS
9722 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9723 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 9724 mips_gp_register);
269137b2 9725 load_delay_nop ();
67c0d1eb
RS
9726 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
9727 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 9728 relax_end ();
df58fc94 9729 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
438c16b8 9730
6478892d
TS
9731 if (mips_cprestore_offset < 0)
9732 as_warn (_("No .cprestore pseudo-op used in PIC code"));
9733 else
9734 {
90ecf173 9735 if (!mips_frame_reg_valid)
7a621144
DJ
9736 {
9737 as_warn (_("No .frame pseudo-op used in PIC code"));
9738 /* Quiet this warning. */
9739 mips_frame_reg_valid = 1;
9740 }
90ecf173 9741 if (!mips_cprestore_valid)
7a621144
DJ
9742 {
9743 as_warn (_("No .cprestore pseudo-op used in PIC code"));
9744 /* Quiet this warning. */
9745 mips_cprestore_valid = 1;
9746 }
6478892d 9747 if (mips_opts.noreorder)
67c0d1eb 9748 macro_build (NULL, "nop", "");
6478892d 9749 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 9750 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 9751 mips_gp_register,
256ab948
TS
9752 mips_frame_reg,
9753 HAVE_64BIT_ADDRESSES);
6478892d 9754 }
252b5132
RH
9755 }
9756 }
0a44bf69
RS
9757 else if (mips_pic == VXWORKS_PIC)
9758 as_bad (_("Non-PIC jump used in PIC library"));
252b5132
RH
9759 else
9760 abort ();
9761
8fc2e39e 9762 break;
252b5132 9763
7f3c4072 9764 case M_LBUE_AB:
7f3c4072
CM
9765 s = "lbue";
9766 fmt = "t,+j(b)";
9767 offbits = 9;
9768 goto ld_st;
9769 case M_LHUE_AB:
7f3c4072
CM
9770 s = "lhue";
9771 fmt = "t,+j(b)";
9772 offbits = 9;
9773 goto ld_st;
9774 case M_LBE_AB:
7f3c4072
CM
9775 s = "lbe";
9776 fmt = "t,+j(b)";
9777 offbits = 9;
9778 goto ld_st;
9779 case M_LHE_AB:
7f3c4072
CM
9780 s = "lhe";
9781 fmt = "t,+j(b)";
9782 offbits = 9;
9783 goto ld_st;
9784 case M_LLE_AB:
7f3c4072
CM
9785 s = "lle";
9786 fmt = "t,+j(b)";
9787 offbits = 9;
9788 goto ld_st;
9789 case M_LWE_AB:
7f3c4072
CM
9790 s = "lwe";
9791 fmt = "t,+j(b)";
9792 offbits = 9;
9793 goto ld_st;
9794 case M_LWLE_AB:
7f3c4072
CM
9795 s = "lwle";
9796 fmt = "t,+j(b)";
9797 offbits = 9;
9798 goto ld_st;
9799 case M_LWRE_AB:
7f3c4072
CM
9800 s = "lwre";
9801 fmt = "t,+j(b)";
9802 offbits = 9;
9803 goto ld_st;
9804 case M_SBE_AB:
7f3c4072
CM
9805 s = "sbe";
9806 fmt = "t,+j(b)";
9807 offbits = 9;
9808 goto ld_st;
9809 case M_SCE_AB:
7f3c4072
CM
9810 s = "sce";
9811 fmt = "t,+j(b)";
9812 offbits = 9;
9813 goto ld_st;
9814 case M_SHE_AB:
7f3c4072
CM
9815 s = "she";
9816 fmt = "t,+j(b)";
9817 offbits = 9;
9818 goto ld_st;
9819 case M_SWE_AB:
7f3c4072
CM
9820 s = "swe";
9821 fmt = "t,+j(b)";
9822 offbits = 9;
9823 goto ld_st;
9824 case M_SWLE_AB:
7f3c4072
CM
9825 s = "swle";
9826 fmt = "t,+j(b)";
9827 offbits = 9;
9828 goto ld_st;
9829 case M_SWRE_AB:
7f3c4072
CM
9830 s = "swre";
9831 fmt = "t,+j(b)";
9832 offbits = 9;
9833 goto ld_st;
dec0624d 9834 case M_ACLR_AB:
dec0624d 9835 s = "aclr";
dec0624d 9836 fmt = "\\,~(b)";
7f3c4072 9837 offbits = 12;
dec0624d
MR
9838 goto ld_st;
9839 case M_ASET_AB:
dec0624d 9840 s = "aset";
dec0624d 9841 fmt = "\\,~(b)";
7f3c4072 9842 offbits = 12;
dec0624d 9843 goto ld_st;
252b5132
RH
9844 case M_LB_AB:
9845 s = "lb";
df58fc94 9846 fmt = "t,o(b)";
252b5132
RH
9847 goto ld;
9848 case M_LBU_AB:
9849 s = "lbu";
df58fc94 9850 fmt = "t,o(b)";
252b5132
RH
9851 goto ld;
9852 case M_LH_AB:
9853 s = "lh";
df58fc94 9854 fmt = "t,o(b)";
252b5132
RH
9855 goto ld;
9856 case M_LHU_AB:
9857 s = "lhu";
df58fc94 9858 fmt = "t,o(b)";
252b5132
RH
9859 goto ld;
9860 case M_LW_AB:
9861 s = "lw";
df58fc94 9862 fmt = "t,o(b)";
252b5132
RH
9863 goto ld;
9864 case M_LWC0_AB:
df58fc94 9865 gas_assert (!mips_opts.micromips);
252b5132 9866 s = "lwc0";
df58fc94 9867 fmt = "E,o(b)";
bdaaa2e1 9868 /* Itbl support may require additional care here. */
252b5132 9869 coproc = 1;
df58fc94 9870 goto ld_st;
252b5132
RH
9871 case M_LWC1_AB:
9872 s = "lwc1";
df58fc94 9873 fmt = "T,o(b)";
bdaaa2e1 9874 /* Itbl support may require additional care here. */
252b5132 9875 coproc = 1;
df58fc94 9876 goto ld_st;
252b5132
RH
9877 case M_LWC2_AB:
9878 s = "lwc2";
df58fc94 9879 fmt = COP12_FMT;
7f3c4072 9880 offbits = (mips_opts.micromips ? 12 : 16);
bdaaa2e1 9881 /* Itbl support may require additional care here. */
252b5132 9882 coproc = 1;
df58fc94 9883 goto ld_st;
252b5132 9884 case M_LWC3_AB:
df58fc94 9885 gas_assert (!mips_opts.micromips);
252b5132 9886 s = "lwc3";
df58fc94 9887 fmt = "E,o(b)";
bdaaa2e1 9888 /* Itbl support may require additional care here. */
252b5132 9889 coproc = 1;
df58fc94 9890 goto ld_st;
252b5132
RH
9891 case M_LWL_AB:
9892 s = "lwl";
df58fc94 9893 fmt = MEM12_FMT;
7f3c4072 9894 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 9895 goto ld_st;
252b5132
RH
9896 case M_LWR_AB:
9897 s = "lwr";
df58fc94 9898 fmt = MEM12_FMT;
7f3c4072 9899 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 9900 goto ld_st;
252b5132 9901 case M_LDC1_AB:
252b5132 9902 s = "ldc1";
df58fc94 9903 fmt = "T,o(b)";
bdaaa2e1 9904 /* Itbl support may require additional care here. */
252b5132 9905 coproc = 1;
df58fc94 9906 goto ld_st;
252b5132
RH
9907 case M_LDC2_AB:
9908 s = "ldc2";
df58fc94 9909 fmt = COP12_FMT;
7f3c4072 9910 offbits = (mips_opts.micromips ? 12 : 16);
bdaaa2e1 9911 /* Itbl support may require additional care here. */
252b5132 9912 coproc = 1;
df58fc94 9913 goto ld_st;
c77c0862 9914 case M_LQC2_AB:
c77c0862
RS
9915 s = "lqc2";
9916 fmt = "E,o(b)";
9917 /* Itbl support may require additional care here. */
9918 coproc = 1;
9919 goto ld_st;
252b5132
RH
9920 case M_LDC3_AB:
9921 s = "ldc3";
df58fc94 9922 fmt = "E,o(b)";
bdaaa2e1 9923 /* Itbl support may require additional care here. */
252b5132 9924 coproc = 1;
df58fc94 9925 goto ld_st;
252b5132
RH
9926 case M_LDL_AB:
9927 s = "ldl";
df58fc94 9928 fmt = MEM12_FMT;
7f3c4072 9929 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 9930 goto ld_st;
252b5132
RH
9931 case M_LDR_AB:
9932 s = "ldr";
df58fc94 9933 fmt = MEM12_FMT;
7f3c4072 9934 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 9935 goto ld_st;
252b5132
RH
9936 case M_LL_AB:
9937 s = "ll";
df58fc94 9938 fmt = MEM12_FMT;
7f3c4072 9939 offbits = (mips_opts.micromips ? 12 : 16);
252b5132
RH
9940 goto ld;
9941 case M_LLD_AB:
9942 s = "lld";
df58fc94 9943 fmt = MEM12_FMT;
7f3c4072 9944 offbits = (mips_opts.micromips ? 12 : 16);
252b5132
RH
9945 goto ld;
9946 case M_LWU_AB:
9947 s = "lwu";
df58fc94 9948 fmt = MEM12_FMT;
7f3c4072 9949 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
9950 goto ld;
9951 case M_LWP_AB:
df58fc94
RS
9952 gas_assert (mips_opts.micromips);
9953 s = "lwp";
9954 fmt = "t,~(b)";
7f3c4072 9955 offbits = 12;
df58fc94
RS
9956 lp = 1;
9957 goto ld;
9958 case M_LDP_AB:
df58fc94
RS
9959 gas_assert (mips_opts.micromips);
9960 s = "ldp";
9961 fmt = "t,~(b)";
7f3c4072 9962 offbits = 12;
df58fc94
RS
9963 lp = 1;
9964 goto ld;
9965 case M_LWM_AB:
df58fc94
RS
9966 gas_assert (mips_opts.micromips);
9967 s = "lwm";
9968 fmt = "n,~(b)";
7f3c4072 9969 offbits = 12;
df58fc94
RS
9970 goto ld_st;
9971 case M_LDM_AB:
df58fc94
RS
9972 gas_assert (mips_opts.micromips);
9973 s = "ldm";
9974 fmt = "n,~(b)";
7f3c4072 9975 offbits = 12;
df58fc94
RS
9976 goto ld_st;
9977
252b5132 9978 ld:
f19ccbda 9979 /* We don't want to use $0 as tempreg. */
c0ebe874 9980 if (op[2] == op[0] + lp || op[0] + lp == ZERO)
df58fc94 9981 goto ld_st;
252b5132 9982 else
c0ebe874 9983 tempreg = op[0] + lp;
df58fc94
RS
9984 goto ld_noat;
9985
252b5132
RH
9986 case M_SB_AB:
9987 s = "sb";
df58fc94
RS
9988 fmt = "t,o(b)";
9989 goto ld_st;
252b5132
RH
9990 case M_SH_AB:
9991 s = "sh";
df58fc94
RS
9992 fmt = "t,o(b)";
9993 goto ld_st;
252b5132
RH
9994 case M_SW_AB:
9995 s = "sw";
df58fc94
RS
9996 fmt = "t,o(b)";
9997 goto ld_st;
252b5132 9998 case M_SWC0_AB:
df58fc94 9999 gas_assert (!mips_opts.micromips);
252b5132 10000 s = "swc0";
df58fc94 10001 fmt = "E,o(b)";
bdaaa2e1 10002 /* Itbl support may require additional care here. */
252b5132 10003 coproc = 1;
df58fc94 10004 goto ld_st;
252b5132
RH
10005 case M_SWC1_AB:
10006 s = "swc1";
df58fc94 10007 fmt = "T,o(b)";
bdaaa2e1 10008 /* Itbl support may require additional care here. */
252b5132 10009 coproc = 1;
df58fc94 10010 goto ld_st;
252b5132
RH
10011 case M_SWC2_AB:
10012 s = "swc2";
df58fc94 10013 fmt = COP12_FMT;
7f3c4072 10014 offbits = (mips_opts.micromips ? 12 : 16);
bdaaa2e1 10015 /* Itbl support may require additional care here. */
252b5132 10016 coproc = 1;
df58fc94 10017 goto ld_st;
252b5132 10018 case M_SWC3_AB:
df58fc94 10019 gas_assert (!mips_opts.micromips);
252b5132 10020 s = "swc3";
df58fc94 10021 fmt = "E,o(b)";
bdaaa2e1 10022 /* Itbl support may require additional care here. */
252b5132 10023 coproc = 1;
df58fc94 10024 goto ld_st;
252b5132
RH
10025 case M_SWL_AB:
10026 s = "swl";
df58fc94 10027 fmt = MEM12_FMT;
7f3c4072 10028 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10029 goto ld_st;
252b5132
RH
10030 case M_SWR_AB:
10031 s = "swr";
df58fc94 10032 fmt = MEM12_FMT;
7f3c4072 10033 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10034 goto ld_st;
252b5132
RH
10035 case M_SC_AB:
10036 s = "sc";
df58fc94 10037 fmt = MEM12_FMT;
7f3c4072 10038 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10039 goto ld_st;
252b5132
RH
10040 case M_SCD_AB:
10041 s = "scd";
df58fc94 10042 fmt = MEM12_FMT;
7f3c4072 10043 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10044 goto ld_st;
d43b4baf
TS
10045 case M_CACHE_AB:
10046 s = "cache";
df58fc94 10047 fmt = mips_opts.micromips ? "k,~(b)" : "k,o(b)";
7f3c4072
CM
10048 offbits = (mips_opts.micromips ? 12 : 16);
10049 goto ld_st;
10050 case M_CACHEE_AB:
7f3c4072
CM
10051 s = "cachee";
10052 fmt = "k,+j(b)";
10053 offbits = 9;
df58fc94 10054 goto ld_st;
3eebd5eb
MR
10055 case M_PREF_AB:
10056 s = "pref";
df58fc94 10057 fmt = !mips_opts.micromips ? "k,o(b)" : "k,~(b)";
7f3c4072
CM
10058 offbits = (mips_opts.micromips ? 12 : 16);
10059 goto ld_st;
10060 case M_PREFE_AB:
7f3c4072
CM
10061 s = "prefe";
10062 fmt = "k,+j(b)";
10063 offbits = 9;
df58fc94 10064 goto ld_st;
252b5132 10065 case M_SDC1_AB:
252b5132 10066 s = "sdc1";
df58fc94 10067 fmt = "T,o(b)";
252b5132 10068 coproc = 1;
bdaaa2e1 10069 /* Itbl support may require additional care here. */
df58fc94 10070 goto ld_st;
252b5132
RH
10071 case M_SDC2_AB:
10072 s = "sdc2";
df58fc94 10073 fmt = COP12_FMT;
7f3c4072 10074 offbits = (mips_opts.micromips ? 12 : 16);
c77c0862
RS
10075 /* Itbl support may require additional care here. */
10076 coproc = 1;
10077 goto ld_st;
10078 case M_SQC2_AB:
c77c0862
RS
10079 s = "sqc2";
10080 fmt = "E,o(b)";
bdaaa2e1 10081 /* Itbl support may require additional care here. */
252b5132 10082 coproc = 1;
df58fc94 10083 goto ld_st;
252b5132 10084 case M_SDC3_AB:
df58fc94 10085 gas_assert (!mips_opts.micromips);
252b5132 10086 s = "sdc3";
df58fc94 10087 fmt = "E,o(b)";
bdaaa2e1 10088 /* Itbl support may require additional care here. */
252b5132 10089 coproc = 1;
df58fc94 10090 goto ld_st;
252b5132
RH
10091 case M_SDL_AB:
10092 s = "sdl";
df58fc94 10093 fmt = MEM12_FMT;
7f3c4072 10094 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10095 goto ld_st;
252b5132
RH
10096 case M_SDR_AB:
10097 s = "sdr";
df58fc94 10098 fmt = MEM12_FMT;
7f3c4072 10099 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
10100 goto ld_st;
10101 case M_SWP_AB:
df58fc94
RS
10102 gas_assert (mips_opts.micromips);
10103 s = "swp";
10104 fmt = "t,~(b)";
7f3c4072 10105 offbits = 12;
df58fc94
RS
10106 goto ld_st;
10107 case M_SDP_AB:
df58fc94
RS
10108 gas_assert (mips_opts.micromips);
10109 s = "sdp";
10110 fmt = "t,~(b)";
7f3c4072 10111 offbits = 12;
df58fc94
RS
10112 goto ld_st;
10113 case M_SWM_AB:
df58fc94
RS
10114 gas_assert (mips_opts.micromips);
10115 s = "swm";
10116 fmt = "n,~(b)";
7f3c4072 10117 offbits = 12;
df58fc94
RS
10118 goto ld_st;
10119 case M_SDM_AB:
df58fc94
RS
10120 gas_assert (mips_opts.micromips);
10121 s = "sdm";
10122 fmt = "n,~(b)";
7f3c4072 10123 offbits = 12;
df58fc94
RS
10124
10125 ld_st:
8fc2e39e 10126 tempreg = AT;
df58fc94 10127 ld_noat:
c0ebe874 10128 breg = op[2];
f2ae14a1
RS
10129 if (small_offset_p (0, align, 16))
10130 {
10131 /* The first case exists for M_LD_AB and M_SD_AB, which are
10132 macros for o32 but which should act like normal instructions
10133 otherwise. */
10134 if (offbits == 16)
c0ebe874 10135 macro_build (&offset_expr, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
10136 offset_reloc[1], offset_reloc[2], breg);
10137 else if (small_offset_p (0, align, offbits))
10138 {
10139 if (offbits == 0)
c0ebe874 10140 macro_build (NULL, s, fmt, op[0], breg);
f2ae14a1 10141 else
c0ebe874 10142 macro_build (NULL, s, fmt, op[0],
c8276761 10143 (int) offset_expr.X_add_number, breg);
f2ae14a1
RS
10144 }
10145 else
10146 {
10147 if (tempreg == AT)
10148 used_at = 1;
10149 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10150 tempreg, breg, -1, offset_reloc[0],
10151 offset_reloc[1], offset_reloc[2]);
10152 if (offbits == 0)
c0ebe874 10153 macro_build (NULL, s, fmt, op[0], tempreg);
f2ae14a1 10154 else
c0ebe874 10155 macro_build (NULL, s, fmt, op[0], 0, tempreg);
f2ae14a1
RS
10156 }
10157 break;
10158 }
10159
10160 if (tempreg == AT)
10161 used_at = 1;
10162
252b5132
RH
10163 if (offset_expr.X_op != O_constant
10164 && offset_expr.X_op != O_symbol)
10165 {
f71d0d44 10166 as_bad (_("Expression too complex"));
252b5132
RH
10167 offset_expr.X_op = O_constant;
10168 }
10169
2051e8c4
MR
10170 if (HAVE_32BIT_ADDRESSES
10171 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
10172 {
10173 char value [32];
10174
10175 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 10176 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 10177 }
2051e8c4 10178
252b5132
RH
10179 /* A constant expression in PIC code can be handled just as it
10180 is in non PIC code. */
aed1a261
RS
10181 if (offset_expr.X_op == O_constant)
10182 {
f2ae14a1
RS
10183 expr1.X_add_number = offset_high_part (offset_expr.X_add_number,
10184 offbits == 0 ? 16 : offbits);
10185 offset_expr.X_add_number -= expr1.X_add_number;
df58fc94 10186
f2ae14a1
RS
10187 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
10188 if (breg != 0)
10189 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10190 tempreg, tempreg, breg);
7f3c4072 10191 if (offbits == 0)
dd6a37e7 10192 {
f2ae14a1 10193 if (offset_expr.X_add_number != 0)
dd6a37e7 10194 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
f2ae14a1 10195 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
c0ebe874 10196 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 10197 }
7f3c4072 10198 else if (offbits == 16)
c0ebe874 10199 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
df58fc94 10200 else
c0ebe874 10201 macro_build (NULL, s, fmt, op[0],
c8276761 10202 (int) offset_expr.X_add_number, tempreg);
df58fc94 10203 }
7f3c4072 10204 else if (offbits != 16)
df58fc94 10205 {
7f3c4072
CM
10206 /* The offset field is too narrow to be used for a low-part
10207 relocation, so load the whole address into the auxillary
f2ae14a1
RS
10208 register. */
10209 load_address (tempreg, &offset_expr, &used_at);
10210 if (breg != 0)
10211 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10212 tempreg, tempreg, breg);
7f3c4072 10213 if (offbits == 0)
c0ebe874 10214 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 10215 else
c0ebe874 10216 macro_build (NULL, s, fmt, op[0], 0, tempreg);
aed1a261
RS
10217 }
10218 else if (mips_pic == NO_PIC)
252b5132
RH
10219 {
10220 /* If this is a reference to a GP relative symbol, and there
10221 is no base register, we want
c0ebe874 10222 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
10223 Otherwise, if there is no base register, we want
10224 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
c0ebe874 10225 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
252b5132
RH
10226 If we have a constant, we need two instructions anyhow,
10227 so we always use the latter form.
10228
10229 If we have a base register, and this is a reference to a
10230 GP relative symbol, we want
10231 addu $tempreg,$breg,$gp
c0ebe874 10232 <op> op[0],<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
10233 Otherwise we want
10234 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
10235 addu $tempreg,$tempreg,$breg
c0ebe874 10236 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 10237 With a constant we always use the latter case.
76b3015f 10238
d6bc6245
TS
10239 With 64bit address space and no base register and $at usable,
10240 we want
10241 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10242 lui $at,<sym> (BFD_RELOC_HI16_S)
10243 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10244 dsll32 $tempreg,0
10245 daddu $tempreg,$at
c0ebe874 10246 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
10247 If we have a base register, we want
10248 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10249 lui $at,<sym> (BFD_RELOC_HI16_S)
10250 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10251 daddu $at,$breg
10252 dsll32 $tempreg,0
10253 daddu $tempreg,$at
c0ebe874 10254 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
10255
10256 Without $at we can't generate the optimal path for superscalar
10257 processors here since this would require two temporary registers.
10258 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10259 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10260 dsll $tempreg,16
10261 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
10262 dsll $tempreg,16
c0ebe874 10263 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
10264 If we have a base register, we want
10265 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10266 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10267 dsll $tempreg,16
10268 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
10269 dsll $tempreg,16
10270 daddu $tempreg,$tempreg,$breg
c0ebe874 10271 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 10272
6caf9ef4 10273 For GP relative symbols in 64bit address space we can use
aed1a261
RS
10274 the same sequence as in 32bit address space. */
10275 if (HAVE_64BIT_SYMBOLS)
d6bc6245 10276 {
aed1a261 10277 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
10278 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
10279 {
10280 relax_start (offset_expr.X_add_symbol);
10281 if (breg == 0)
10282 {
c0ebe874 10283 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
10284 BFD_RELOC_GPREL16, mips_gp_register);
10285 }
10286 else
10287 {
10288 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10289 tempreg, breg, mips_gp_register);
c0ebe874 10290 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
10291 BFD_RELOC_GPREL16, tempreg);
10292 }
10293 relax_switch ();
10294 }
d6bc6245 10295
741fe287 10296 if (used_at == 0 && mips_opts.at)
d6bc6245 10297 {
df58fc94 10298 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb 10299 BFD_RELOC_MIPS_HIGHEST);
df58fc94 10300 macro_build (&offset_expr, "lui", LUI_FMT, AT,
67c0d1eb
RS
10301 BFD_RELOC_HI16_S);
10302 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
10303 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 10304 if (breg != 0)
67c0d1eb 10305 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
df58fc94 10306 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 10307 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
c0ebe874 10308 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16,
67c0d1eb 10309 tempreg);
d6bc6245
TS
10310 used_at = 1;
10311 }
10312 else
10313 {
df58fc94 10314 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb
RS
10315 BFD_RELOC_MIPS_HIGHEST);
10316 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
10317 tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 10318 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb
RS
10319 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
10320 tempreg, BFD_RELOC_HI16_S);
df58fc94 10321 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
d6bc6245 10322 if (breg != 0)
67c0d1eb 10323 macro_build (NULL, "daddu", "d,v,t",
17a2f251 10324 tempreg, tempreg, breg);
c0ebe874 10325 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 10326 BFD_RELOC_LO16, tempreg);
d6bc6245 10327 }
6caf9ef4
TS
10328
10329 if (mips_relax.sequence)
10330 relax_end ();
8fc2e39e 10331 break;
d6bc6245 10332 }
256ab948 10333
252b5132
RH
10334 if (breg == 0)
10335 {
67c0d1eb 10336 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 10337 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 10338 {
4d7206a2 10339 relax_start (offset_expr.X_add_symbol);
c0ebe874 10340 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_GPREL16,
67c0d1eb 10341 mips_gp_register);
4d7206a2 10342 relax_switch ();
252b5132 10343 }
67c0d1eb 10344 macro_build_lui (&offset_expr, tempreg);
c0ebe874 10345 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 10346 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
10347 if (mips_relax.sequence)
10348 relax_end ();
252b5132
RH
10349 }
10350 else
10351 {
67c0d1eb 10352 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 10353 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 10354 {
4d7206a2 10355 relax_start (offset_expr.X_add_symbol);
67c0d1eb 10356 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10357 tempreg, breg, mips_gp_register);
c0ebe874 10358 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 10359 BFD_RELOC_GPREL16, tempreg);
4d7206a2 10360 relax_switch ();
252b5132 10361 }
67c0d1eb
RS
10362 macro_build_lui (&offset_expr, tempreg);
10363 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10364 tempreg, tempreg, breg);
c0ebe874 10365 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 10366 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
10367 if (mips_relax.sequence)
10368 relax_end ();
252b5132
RH
10369 }
10370 }
0a44bf69 10371 else if (!mips_big_got)
252b5132 10372 {
ed6fb7bd 10373 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 10374
252b5132
RH
10375 /* If this is a reference to an external symbol, we want
10376 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10377 nop
c0ebe874 10378 <op> op[0],0($tempreg)
252b5132
RH
10379 Otherwise we want
10380 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10381 nop
10382 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 10383 <op> op[0],0($tempreg)
f5040a92
AO
10384
10385 For NewABI, we want
10386 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 10387 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 10388
252b5132
RH
10389 If there is a base register, we add it to $tempreg before
10390 the <op>. If there is a constant, we stick it in the
10391 <op> instruction. We don't handle constants larger than
10392 16 bits, because we have no way to load the upper 16 bits
10393 (actually, we could handle them for the subset of cases
10394 in which we are not using $at). */
9c2799c2 10395 gas_assert (offset_expr.X_op == O_symbol);
f5040a92
AO
10396 if (HAVE_NEWABI)
10397 {
67c0d1eb
RS
10398 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10399 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 10400 if (breg != 0)
67c0d1eb 10401 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10402 tempreg, tempreg, breg);
c0ebe874 10403 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 10404 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
10405 break;
10406 }
252b5132
RH
10407 expr1.X_add_number = offset_expr.X_add_number;
10408 offset_expr.X_add_number = 0;
10409 if (expr1.X_add_number < -0x8000
10410 || expr1.X_add_number >= 0x8000)
10411 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
10412 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10413 lw_reloc_type, mips_gp_register);
269137b2 10414 load_delay_nop ();
4d7206a2
RS
10415 relax_start (offset_expr.X_add_symbol);
10416 relax_switch ();
67c0d1eb
RS
10417 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
10418 tempreg, BFD_RELOC_LO16);
4d7206a2 10419 relax_end ();
252b5132 10420 if (breg != 0)
67c0d1eb 10421 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10422 tempreg, tempreg, breg);
c0ebe874 10423 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 10424 }
0a44bf69 10425 else if (mips_big_got && !HAVE_NEWABI)
252b5132 10426 {
67c0d1eb 10427 int gpdelay;
252b5132
RH
10428
10429 /* If this is a reference to an external symbol, we want
10430 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10431 addu $tempreg,$tempreg,$gp
10432 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 10433 <op> op[0],0($tempreg)
252b5132
RH
10434 Otherwise we want
10435 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10436 nop
10437 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 10438 <op> op[0],0($tempreg)
252b5132
RH
10439 If there is a base register, we add it to $tempreg before
10440 the <op>. If there is a constant, we stick it in the
10441 <op> instruction. We don't handle constants larger than
10442 16 bits, because we have no way to load the upper 16 bits
10443 (actually, we could handle them for the subset of cases
f5040a92 10444 in which we are not using $at). */
9c2799c2 10445 gas_assert (offset_expr.X_op == O_symbol);
252b5132
RH
10446 expr1.X_add_number = offset_expr.X_add_number;
10447 offset_expr.X_add_number = 0;
10448 if (expr1.X_add_number < -0x8000
10449 || expr1.X_add_number >= 0x8000)
10450 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 10451 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 10452 relax_start (offset_expr.X_add_symbol);
df58fc94 10453 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 10454 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
10455 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
10456 mips_gp_register);
10457 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10458 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 10459 relax_switch ();
67c0d1eb
RS
10460 if (gpdelay)
10461 macro_build (NULL, "nop", "");
10462 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10463 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 10464 load_delay_nop ();
67c0d1eb
RS
10465 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
10466 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
10467 relax_end ();
10468
252b5132 10469 if (breg != 0)
67c0d1eb 10470 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10471 tempreg, tempreg, breg);
c0ebe874 10472 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 10473 }
0a44bf69 10474 else if (mips_big_got && HAVE_NEWABI)
f5040a92 10475 {
f5040a92
AO
10476 /* If this is a reference to an external symbol, we want
10477 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10478 add $tempreg,$tempreg,$gp
10479 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 10480 <op> op[0],<ofst>($tempreg)
f5040a92
AO
10481 Otherwise, for local symbols, we want:
10482 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 10483 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
9c2799c2 10484 gas_assert (offset_expr.X_op == O_symbol);
4d7206a2 10485 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
10486 offset_expr.X_add_number = 0;
10487 if (expr1.X_add_number < -0x8000
10488 || expr1.X_add_number >= 0x8000)
10489 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 10490 relax_start (offset_expr.X_add_symbol);
df58fc94 10491 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 10492 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
10493 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
10494 mips_gp_register);
10495 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10496 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 10497 if (breg != 0)
67c0d1eb 10498 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10499 tempreg, tempreg, breg);
c0ebe874 10500 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
684022ea 10501
4d7206a2 10502 relax_switch ();
f5040a92 10503 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
10504 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10505 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 10506 if (breg != 0)
67c0d1eb 10507 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10508 tempreg, tempreg, breg);
c0ebe874 10509 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 10510 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 10511 relax_end ();
f5040a92 10512 }
252b5132
RH
10513 else
10514 abort ();
10515
252b5132
RH
10516 break;
10517
833794fc
MR
10518 case M_JRADDIUSP:
10519 gas_assert (mips_opts.micromips);
10520 gas_assert (mips_opts.insn32);
10521 start_noreorder ();
10522 macro_build (NULL, "jr", "s", RA);
c0ebe874 10523 expr1.X_add_number = op[0] << 2;
833794fc
MR
10524 macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
10525 end_noreorder ();
10526 break;
10527
10528 case M_JRC:
10529 gas_assert (mips_opts.micromips);
10530 gas_assert (mips_opts.insn32);
c0ebe874 10531 macro_build (NULL, "jr", "s", op[0]);
833794fc
MR
10532 if (mips_opts.noreorder)
10533 macro_build (NULL, "nop", "");
10534 break;
10535
252b5132
RH
10536 case M_LI:
10537 case M_LI_S:
c0ebe874 10538 load_register (op[0], &imm_expr, 0);
8fc2e39e 10539 break;
252b5132
RH
10540
10541 case M_DLI:
c0ebe874 10542 load_register (op[0], &imm_expr, 1);
8fc2e39e 10543 break;
252b5132
RH
10544
10545 case M_LI_SS:
10546 if (imm_expr.X_op == O_constant)
10547 {
8fc2e39e 10548 used_at = 1;
67c0d1eb 10549 load_register (AT, &imm_expr, 0);
c0ebe874 10550 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
10551 break;
10552 }
10553 else
10554 {
9c2799c2 10555 gas_assert (offset_expr.X_op == O_symbol
90ecf173
MR
10556 && strcmp (segment_name (S_GET_SEGMENT
10557 (offset_expr.X_add_symbol)),
10558 ".lit4") == 0
10559 && offset_expr.X_add_number == 0);
c0ebe874 10560 macro_build (&offset_expr, "lwc1", "T,o(b)", op[0],
17a2f251 10561 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 10562 break;
252b5132
RH
10563 }
10564
10565 case M_LI_D:
ca4e0257
RS
10566 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
10567 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
10568 order 32 bits of the value and the low order 32 bits are either
10569 zero or in OFFSET_EXPR. */
252b5132
RH
10570 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
10571 {
ca4e0257 10572 if (HAVE_64BIT_GPRS)
c0ebe874 10573 load_register (op[0], &imm_expr, 1);
252b5132
RH
10574 else
10575 {
10576 int hreg, lreg;
10577
10578 if (target_big_endian)
10579 {
c0ebe874
RS
10580 hreg = op[0];
10581 lreg = op[0] + 1;
252b5132
RH
10582 }
10583 else
10584 {
c0ebe874
RS
10585 hreg = op[0] + 1;
10586 lreg = op[0];
252b5132
RH
10587 }
10588
10589 if (hreg <= 31)
67c0d1eb 10590 load_register (hreg, &imm_expr, 0);
252b5132
RH
10591 if (lreg <= 31)
10592 {
10593 if (offset_expr.X_op == O_absent)
67c0d1eb 10594 move_register (lreg, 0);
252b5132
RH
10595 else
10596 {
9c2799c2 10597 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 10598 load_register (lreg, &offset_expr, 0);
252b5132
RH
10599 }
10600 }
10601 }
8fc2e39e 10602 break;
252b5132
RH
10603 }
10604
10605 /* We know that sym is in the .rdata section. First we get the
10606 upper 16 bits of the address. */
10607 if (mips_pic == NO_PIC)
10608 {
67c0d1eb 10609 macro_build_lui (&offset_expr, AT);
8fc2e39e 10610 used_at = 1;
252b5132 10611 }
0a44bf69 10612 else
252b5132 10613 {
67c0d1eb
RS
10614 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
10615 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 10616 used_at = 1;
252b5132 10617 }
bdaaa2e1 10618
252b5132 10619 /* Now we load the register(s). */
ca4e0257 10620 if (HAVE_64BIT_GPRS)
8fc2e39e
TS
10621 {
10622 used_at = 1;
c0ebe874
RS
10623 macro_build (&offset_expr, "ld", "t,o(b)", op[0],
10624 BFD_RELOC_LO16, AT);
8fc2e39e 10625 }
252b5132
RH
10626 else
10627 {
8fc2e39e 10628 used_at = 1;
c0ebe874
RS
10629 macro_build (&offset_expr, "lw", "t,o(b)", op[0],
10630 BFD_RELOC_LO16, AT);
10631 if (op[0] != RA)
252b5132
RH
10632 {
10633 /* FIXME: How in the world do we deal with the possible
10634 overflow here? */
10635 offset_expr.X_add_number += 4;
67c0d1eb 10636 macro_build (&offset_expr, "lw", "t,o(b)",
c0ebe874 10637 op[0] + 1, BFD_RELOC_LO16, AT);
252b5132
RH
10638 }
10639 }
252b5132
RH
10640 break;
10641
10642 case M_LI_DD:
ca4e0257
RS
10643 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
10644 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
10645 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
10646 the value and the low order 32 bits are either zero or in
10647 OFFSET_EXPR. */
252b5132
RH
10648 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
10649 {
8fc2e39e 10650 used_at = 1;
67c0d1eb 10651 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
ca4e0257
RS
10652 if (HAVE_64BIT_FPRS)
10653 {
9c2799c2 10654 gas_assert (HAVE_64BIT_GPRS);
c0ebe874 10655 macro_build (NULL, "dmtc1", "t,S", AT, op[0]);
ca4e0257 10656 }
252b5132
RH
10657 else
10658 {
c0ebe874 10659 macro_build (NULL, "mtc1", "t,G", AT, op[0] + 1);
252b5132 10660 if (offset_expr.X_op == O_absent)
c0ebe874 10661 macro_build (NULL, "mtc1", "t,G", 0, op[0]);
252b5132
RH
10662 else
10663 {
9c2799c2 10664 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 10665 load_register (AT, &offset_expr, 0);
c0ebe874 10666 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
10667 }
10668 }
10669 break;
10670 }
10671
9c2799c2 10672 gas_assert (offset_expr.X_op == O_symbol
90ecf173 10673 && offset_expr.X_add_number == 0);
252b5132
RH
10674 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
10675 if (strcmp (s, ".lit8") == 0)
f2ae14a1 10676 {
c0ebe874 10677 op[2] = mips_gp_register;
f2ae14a1
RS
10678 offset_reloc[0] = BFD_RELOC_MIPS_LITERAL;
10679 offset_reloc[1] = BFD_RELOC_UNUSED;
10680 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
10681 }
10682 else
10683 {
9c2799c2 10684 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 10685 used_at = 1;
0a44bf69 10686 if (mips_pic != NO_PIC)
67c0d1eb
RS
10687 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
10688 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
10689 else
10690 {
10691 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 10692 macro_build_lui (&offset_expr, AT);
252b5132 10693 }
bdaaa2e1 10694
c0ebe874 10695 op[2] = AT;
f2ae14a1
RS
10696 offset_reloc[0] = BFD_RELOC_LO16;
10697 offset_reloc[1] = BFD_RELOC_UNUSED;
10698 offset_reloc[2] = BFD_RELOC_UNUSED;
10699 }
10700 align = 8;
10701 /* Fall through */
c4a68bea 10702
252b5132
RH
10703 case M_L_DAB:
10704 /*
10705 * The MIPS assembler seems to check for X_add_number not
10706 * being double aligned and generating:
10707 * lui at,%hi(foo+1)
10708 * addu at,at,v1
10709 * addiu at,at,%lo(foo+1)
10710 * lwc1 f2,0(at)
10711 * lwc1 f3,4(at)
10712 * But, the resulting address is the same after relocation so why
10713 * generate the extra instruction?
10714 */
bdaaa2e1 10715 /* Itbl support may require additional care here. */
252b5132 10716 coproc = 1;
df58fc94 10717 fmt = "T,o(b)";
0aa27725 10718 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
10719 {
10720 s = "ldc1";
df58fc94 10721 goto ld_st;
252b5132 10722 }
252b5132 10723 s = "lwc1";
252b5132
RH
10724 goto ldd_std;
10725
10726 case M_S_DAB:
df58fc94
RS
10727 gas_assert (!mips_opts.micromips);
10728 /* Itbl support may require additional care here. */
10729 coproc = 1;
10730 fmt = "T,o(b)";
0aa27725 10731 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
10732 {
10733 s = "sdc1";
df58fc94 10734 goto ld_st;
252b5132 10735 }
252b5132 10736 s = "swc1";
252b5132
RH
10737 goto ldd_std;
10738
e407c74b
NC
10739 case M_LQ_AB:
10740 fmt = "t,o(b)";
10741 s = "lq";
10742 goto ld;
10743
10744 case M_SQ_AB:
10745 fmt = "t,o(b)";
10746 s = "sq";
10747 goto ld_st;
10748
252b5132 10749 case M_LD_AB:
df58fc94 10750 fmt = "t,o(b)";
ca4e0257 10751 if (HAVE_64BIT_GPRS)
252b5132
RH
10752 {
10753 s = "ld";
10754 goto ld;
10755 }
252b5132 10756 s = "lw";
252b5132
RH
10757 goto ldd_std;
10758
10759 case M_SD_AB:
df58fc94 10760 fmt = "t,o(b)";
ca4e0257 10761 if (HAVE_64BIT_GPRS)
252b5132
RH
10762 {
10763 s = "sd";
df58fc94 10764 goto ld_st;
252b5132 10765 }
252b5132 10766 s = "sw";
252b5132
RH
10767
10768 ldd_std:
f2ae14a1
RS
10769 /* Even on a big endian machine $fn comes before $fn+1. We have
10770 to adjust when loading from memory. We set coproc if we must
10771 load $fn+1 first. */
10772 /* Itbl support may require additional care here. */
10773 if (!target_big_endian)
10774 coproc = 0;
10775
c0ebe874 10776 breg = op[2];
f2ae14a1
RS
10777 if (small_offset_p (0, align, 16))
10778 {
10779 ep = &offset_expr;
10780 if (!small_offset_p (4, align, 16))
10781 {
10782 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT, breg,
10783 -1, offset_reloc[0], offset_reloc[1],
10784 offset_reloc[2]);
10785 expr1.X_add_number = 0;
10786 ep = &expr1;
10787 breg = AT;
10788 used_at = 1;
10789 offset_reloc[0] = BFD_RELOC_LO16;
10790 offset_reloc[1] = BFD_RELOC_UNUSED;
10791 offset_reloc[2] = BFD_RELOC_UNUSED;
10792 }
c0ebe874 10793 if (strcmp (s, "lw") == 0 && op[0] == breg)
f2ae14a1
RS
10794 {
10795 ep->X_add_number += 4;
c0ebe874 10796 macro_build (ep, s, fmt, op[0] + 1, -1, offset_reloc[0],
f2ae14a1
RS
10797 offset_reloc[1], offset_reloc[2], breg);
10798 ep->X_add_number -= 4;
c0ebe874 10799 macro_build (ep, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
10800 offset_reloc[1], offset_reloc[2], breg);
10801 }
10802 else
10803 {
c0ebe874 10804 macro_build (ep, s, fmt, coproc ? op[0] + 1 : op[0], -1,
f2ae14a1
RS
10805 offset_reloc[0], offset_reloc[1], offset_reloc[2],
10806 breg);
10807 ep->X_add_number += 4;
c0ebe874 10808 macro_build (ep, s, fmt, coproc ? op[0] : op[0] + 1, -1,
f2ae14a1
RS
10809 offset_reloc[0], offset_reloc[1], offset_reloc[2],
10810 breg);
10811 }
10812 break;
10813 }
10814
252b5132
RH
10815 if (offset_expr.X_op != O_symbol
10816 && offset_expr.X_op != O_constant)
10817 {
f71d0d44 10818 as_bad (_("Expression too complex"));
252b5132
RH
10819 offset_expr.X_op = O_constant;
10820 }
10821
2051e8c4
MR
10822 if (HAVE_32BIT_ADDRESSES
10823 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
10824 {
10825 char value [32];
10826
10827 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 10828 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 10829 }
2051e8c4 10830
90ecf173 10831 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
252b5132
RH
10832 {
10833 /* If this is a reference to a GP relative symbol, we want
c0ebe874
RS
10834 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
10835 <op> op[0]+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
10836 If we have a base register, we use this
10837 addu $at,$breg,$gp
c0ebe874
RS
10838 <op> op[0],<sym>($at) (BFD_RELOC_GPREL16)
10839 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
10840 If this is not a GP relative symbol, we want
10841 lui $at,<sym> (BFD_RELOC_HI16_S)
c0ebe874
RS
10842 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
10843 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
10844 If there is a base register, we add it to $at after the
10845 lui instruction. If there is a constant, we always use
10846 the last case. */
39a59cf8
MR
10847 if (offset_expr.X_op == O_symbol
10848 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 10849 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 10850 {
4d7206a2 10851 relax_start (offset_expr.X_add_symbol);
252b5132
RH
10852 if (breg == 0)
10853 {
c9914766 10854 tempreg = mips_gp_register;
252b5132
RH
10855 }
10856 else
10857 {
67c0d1eb 10858 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10859 AT, breg, mips_gp_register);
252b5132 10860 tempreg = AT;
252b5132
RH
10861 used_at = 1;
10862 }
10863
beae10d5 10864 /* Itbl support may require additional care here. */
c0ebe874 10865 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 10866 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
10867 offset_expr.X_add_number += 4;
10868
10869 /* Set mips_optimize to 2 to avoid inserting an
10870 undesired nop. */
10871 hold_mips_optimize = mips_optimize;
10872 mips_optimize = 2;
beae10d5 10873 /* Itbl support may require additional care here. */
c0ebe874 10874 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 10875 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
10876 mips_optimize = hold_mips_optimize;
10877
4d7206a2 10878 relax_switch ();
252b5132 10879
0970e49e 10880 offset_expr.X_add_number -= 4;
252b5132 10881 }
8fc2e39e 10882 used_at = 1;
f2ae14a1
RS
10883 if (offset_high_part (offset_expr.X_add_number, 16)
10884 != offset_high_part (offset_expr.X_add_number + 4, 16))
10885 {
10886 load_address (AT, &offset_expr, &used_at);
10887 offset_expr.X_op = O_constant;
10888 offset_expr.X_add_number = 0;
10889 }
10890 else
10891 macro_build_lui (&offset_expr, AT);
252b5132 10892 if (breg != 0)
67c0d1eb 10893 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 10894 /* Itbl support may require additional care here. */
c0ebe874 10895 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 10896 BFD_RELOC_LO16, AT);
252b5132
RH
10897 /* FIXME: How do we handle overflow here? */
10898 offset_expr.X_add_number += 4;
beae10d5 10899 /* Itbl support may require additional care here. */
c0ebe874 10900 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 10901 BFD_RELOC_LO16, AT);
4d7206a2
RS
10902 if (mips_relax.sequence)
10903 relax_end ();
bdaaa2e1 10904 }
0a44bf69 10905 else if (!mips_big_got)
252b5132 10906 {
252b5132
RH
10907 /* If this is a reference to an external symbol, we want
10908 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10909 nop
c0ebe874
RS
10910 <op> op[0],0($at)
10911 <op> op[0]+1,4($at)
252b5132
RH
10912 Otherwise we want
10913 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10914 nop
c0ebe874
RS
10915 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
10916 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
10917 If there is a base register we add it to $at before the
10918 lwc1 instructions. If there is a constant we include it
10919 in the lwc1 instructions. */
10920 used_at = 1;
10921 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
10922 if (expr1.X_add_number < -0x8000
10923 || expr1.X_add_number >= 0x8000 - 4)
10924 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 10925 load_got_offset (AT, &offset_expr);
269137b2 10926 load_delay_nop ();
252b5132 10927 if (breg != 0)
67c0d1eb 10928 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
10929
10930 /* Set mips_optimize to 2 to avoid inserting an undesired
10931 nop. */
10932 hold_mips_optimize = mips_optimize;
10933 mips_optimize = 2;
4d7206a2 10934
beae10d5 10935 /* Itbl support may require additional care here. */
4d7206a2 10936 relax_start (offset_expr.X_add_symbol);
c0ebe874 10937 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 10938 BFD_RELOC_LO16, AT);
4d7206a2 10939 expr1.X_add_number += 4;
c0ebe874 10940 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 10941 BFD_RELOC_LO16, AT);
4d7206a2 10942 relax_switch ();
c0ebe874 10943 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 10944 BFD_RELOC_LO16, AT);
4d7206a2 10945 offset_expr.X_add_number += 4;
c0ebe874 10946 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 10947 BFD_RELOC_LO16, AT);
4d7206a2 10948 relax_end ();
252b5132 10949
4d7206a2 10950 mips_optimize = hold_mips_optimize;
252b5132 10951 }
0a44bf69 10952 else if (mips_big_got)
252b5132 10953 {
67c0d1eb 10954 int gpdelay;
252b5132
RH
10955
10956 /* If this is a reference to an external symbol, we want
10957 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10958 addu $at,$at,$gp
10959 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
10960 nop
c0ebe874
RS
10961 <op> op[0],0($at)
10962 <op> op[0]+1,4($at)
252b5132
RH
10963 Otherwise we want
10964 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10965 nop
c0ebe874
RS
10966 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
10967 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
10968 If there is a base register we add it to $at before the
10969 lwc1 instructions. If there is a constant we include it
10970 in the lwc1 instructions. */
10971 used_at = 1;
10972 expr1.X_add_number = offset_expr.X_add_number;
10973 offset_expr.X_add_number = 0;
10974 if (expr1.X_add_number < -0x8000
10975 || expr1.X_add_number >= 0x8000 - 4)
10976 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 10977 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 10978 relax_start (offset_expr.X_add_symbol);
df58fc94 10979 macro_build (&offset_expr, "lui", LUI_FMT,
67c0d1eb
RS
10980 AT, BFD_RELOC_MIPS_GOT_HI16);
10981 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10982 AT, AT, mips_gp_register);
67c0d1eb 10983 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 10984 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 10985 load_delay_nop ();
252b5132 10986 if (breg != 0)
67c0d1eb 10987 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 10988 /* Itbl support may require additional care here. */
c0ebe874 10989 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 10990 BFD_RELOC_LO16, AT);
252b5132
RH
10991 expr1.X_add_number += 4;
10992
10993 /* Set mips_optimize to 2 to avoid inserting an undesired
10994 nop. */
10995 hold_mips_optimize = mips_optimize;
10996 mips_optimize = 2;
beae10d5 10997 /* Itbl support may require additional care here. */
c0ebe874 10998 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 10999 BFD_RELOC_LO16, AT);
252b5132
RH
11000 mips_optimize = hold_mips_optimize;
11001 expr1.X_add_number -= 4;
11002
4d7206a2
RS
11003 relax_switch ();
11004 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
11005 if (gpdelay)
11006 macro_build (NULL, "nop", "");
11007 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
11008 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 11009 load_delay_nop ();
252b5132 11010 if (breg != 0)
67c0d1eb 11011 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 11012 /* Itbl support may require additional care here. */
c0ebe874 11013 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 11014 BFD_RELOC_LO16, AT);
4d7206a2 11015 offset_expr.X_add_number += 4;
252b5132
RH
11016
11017 /* Set mips_optimize to 2 to avoid inserting an undesired
11018 nop. */
11019 hold_mips_optimize = mips_optimize;
11020 mips_optimize = 2;
beae10d5 11021 /* Itbl support may require additional care here. */
c0ebe874 11022 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 11023 BFD_RELOC_LO16, AT);
252b5132 11024 mips_optimize = hold_mips_optimize;
4d7206a2 11025 relax_end ();
252b5132 11026 }
252b5132
RH
11027 else
11028 abort ();
11029
252b5132 11030 break;
dd6a37e7
AP
11031
11032 case M_SAA_AB:
dd6a37e7 11033 s = "saa";
7f3c4072 11034 offbits = 0;
dd6a37e7
AP
11035 fmt = "t,(b)";
11036 goto ld_st;
11037 case M_SAAD_AB:
dd6a37e7 11038 s = "saad";
7f3c4072 11039 offbits = 0;
dd6a37e7
AP
11040 fmt = "t,(b)";
11041 goto ld_st;
11042
252b5132
RH
11043 /* New code added to support COPZ instructions.
11044 This code builds table entries out of the macros in mip_opcodes.
11045 R4000 uses interlocks to handle coproc delays.
11046 Other chips (like the R3000) require nops to be inserted for delays.
11047
f72c8c98 11048 FIXME: Currently, we require that the user handle delays.
252b5132
RH
11049 In order to fill delay slots for non-interlocked chips,
11050 we must have a way to specify delays based on the coprocessor.
11051 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
11052 What are the side-effects of the cop instruction?
11053 What cache support might we have and what are its effects?
11054 Both coprocessor & memory require delays. how long???
bdaaa2e1 11055 What registers are read/set/modified?
252b5132
RH
11056
11057 If an itbl is provided to interpret cop instructions,
bdaaa2e1 11058 this knowledge can be encoded in the itbl spec. */
252b5132
RH
11059
11060 case M_COP0:
11061 s = "c0";
11062 goto copz;
11063 case M_COP1:
11064 s = "c1";
11065 goto copz;
11066 case M_COP2:
11067 s = "c2";
11068 goto copz;
11069 case M_COP3:
11070 s = "c3";
11071 copz:
df58fc94 11072 gas_assert (!mips_opts.micromips);
252b5132
RH
11073 /* For now we just do C (same as Cz). The parameter will be
11074 stored in insn_opcode by mips_ip. */
c8276761 11075 macro_build (NULL, s, "C", (int) ip->insn_opcode);
8fc2e39e 11076 break;
252b5132 11077
ea1fb5dc 11078 case M_MOVE:
c0ebe874 11079 move_register (op[0], op[1]);
8fc2e39e 11080 break;
ea1fb5dc 11081
833794fc
MR
11082 case M_MOVEP:
11083 gas_assert (mips_opts.micromips);
11084 gas_assert (mips_opts.insn32);
c0ebe874
RS
11085 move_register (micromips_to_32_reg_h_map1[op[0]],
11086 micromips_to_32_reg_m_map[op[1]]);
11087 move_register (micromips_to_32_reg_h_map2[op[0]],
11088 micromips_to_32_reg_n_map[op[2]]);
833794fc
MR
11089 break;
11090
252b5132
RH
11091 case M_DMUL:
11092 dbl = 1;
11093 case M_MUL:
e407c74b 11094 if (mips_opts.arch == CPU_R5900)
c0ebe874
RS
11095 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", op[0], op[1],
11096 op[2]);
e407c74b
NC
11097 else
11098 {
c0ebe874
RS
11099 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", op[1], op[2]);
11100 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
e407c74b 11101 }
8fc2e39e 11102 break;
252b5132
RH
11103
11104 case M_DMUL_I:
11105 dbl = 1;
11106 case M_MUL_I:
11107 /* The MIPS assembler some times generates shifts and adds. I'm
11108 not trying to be that fancy. GCC should do this for us
11109 anyway. */
8fc2e39e 11110 used_at = 1;
67c0d1eb 11111 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
11112 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", op[1], AT);
11113 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
11114 break;
11115
11116 case M_DMULO_I:
11117 dbl = 1;
11118 case M_MULO_I:
11119 imm = 1;
11120 goto do_mulo;
11121
11122 case M_DMULO:
11123 dbl = 1;
11124 case M_MULO:
11125 do_mulo:
7d10b47d 11126 start_noreorder ();
8fc2e39e 11127 used_at = 1;
252b5132 11128 if (imm)
67c0d1eb 11129 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
11130 macro_build (NULL, dbl ? "dmult" : "mult", "s,t",
11131 op[1], imm ? AT : op[2]);
11132 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
11133 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, op[0], op[0], 31);
df58fc94 11134 macro_build (NULL, "mfhi", MFHL_FMT, AT);
252b5132 11135 if (mips_trap)
c0ebe874 11136 macro_build (NULL, "tne", TRAP_FMT, op[0], AT, 6);
252b5132
RH
11137 else
11138 {
df58fc94
RS
11139 if (mips_opts.micromips)
11140 micromips_label_expr (&label_expr);
11141 else
11142 label_expr.X_add_number = 8;
c0ebe874 11143 macro_build (&label_expr, "beq", "s,t,p", op[0], AT);
a605d2b3 11144 macro_build (NULL, "nop", "");
df58fc94
RS
11145 macro_build (NULL, "break", BRK_FMT, 6);
11146 if (mips_opts.micromips)
11147 micromips_add_label ();
252b5132 11148 }
7d10b47d 11149 end_noreorder ();
c0ebe874 11150 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
11151 break;
11152
11153 case M_DMULOU_I:
11154 dbl = 1;
11155 case M_MULOU_I:
11156 imm = 1;
11157 goto do_mulou;
11158
11159 case M_DMULOU:
11160 dbl = 1;
11161 case M_MULOU:
11162 do_mulou:
7d10b47d 11163 start_noreorder ();
8fc2e39e 11164 used_at = 1;
252b5132 11165 if (imm)
67c0d1eb
RS
11166 load_register (AT, &imm_expr, dbl);
11167 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
c0ebe874 11168 op[1], imm ? AT : op[2]);
df58fc94 11169 macro_build (NULL, "mfhi", MFHL_FMT, AT);
c0ebe874 11170 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132 11171 if (mips_trap)
df58fc94 11172 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
252b5132
RH
11173 else
11174 {
df58fc94
RS
11175 if (mips_opts.micromips)
11176 micromips_label_expr (&label_expr);
11177 else
11178 label_expr.X_add_number = 8;
11179 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
a605d2b3 11180 macro_build (NULL, "nop", "");
df58fc94
RS
11181 macro_build (NULL, "break", BRK_FMT, 6);
11182 if (mips_opts.micromips)
11183 micromips_add_label ();
252b5132 11184 }
7d10b47d 11185 end_noreorder ();
252b5132
RH
11186 break;
11187
771c7ce4 11188 case M_DROL:
fef14a42 11189 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 11190 {
c0ebe874 11191 if (op[0] == op[1])
82dd0097
CD
11192 {
11193 tempreg = AT;
11194 used_at = 1;
11195 }
11196 else
c0ebe874
RS
11197 tempreg = op[0];
11198 macro_build (NULL, "dnegu", "d,w", tempreg, op[2]);
11199 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 11200 break;
82dd0097 11201 }
8fc2e39e 11202 used_at = 1;
c0ebe874
RS
11203 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
11204 macro_build (NULL, "dsrlv", "d,t,s", AT, op[1], AT);
11205 macro_build (NULL, "dsllv", "d,t,s", op[0], op[1], op[2]);
11206 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
11207 break;
11208
252b5132 11209 case M_ROL:
fef14a42 11210 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 11211 {
c0ebe874 11212 if (op[0] == op[1])
82dd0097
CD
11213 {
11214 tempreg = AT;
11215 used_at = 1;
11216 }
11217 else
c0ebe874
RS
11218 tempreg = op[0];
11219 macro_build (NULL, "negu", "d,w", tempreg, op[2]);
11220 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 11221 break;
82dd0097 11222 }
8fc2e39e 11223 used_at = 1;
c0ebe874
RS
11224 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
11225 macro_build (NULL, "srlv", "d,t,s", AT, op[1], AT);
11226 macro_build (NULL, "sllv", "d,t,s", op[0], op[1], op[2]);
11227 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
11228 break;
11229
771c7ce4
TS
11230 case M_DROL_I:
11231 {
11232 unsigned int rot;
91d6fa6a
NC
11233 char *l;
11234 char *rr;
771c7ce4
TS
11235
11236 if (imm_expr.X_op != O_constant)
82dd0097 11237 as_bad (_("Improper rotate count"));
771c7ce4 11238 rot = imm_expr.X_add_number & 0x3f;
fef14a42 11239 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
11240 {
11241 rot = (64 - rot) & 0x3f;
11242 if (rot >= 32)
c0ebe874 11243 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
60b63b72 11244 else
c0ebe874 11245 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 11246 break;
60b63b72 11247 }
483fc7cd 11248 if (rot == 0)
483fc7cd 11249 {
c0ebe874 11250 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 11251 break;
483fc7cd 11252 }
82dd0097 11253 l = (rot < 0x20) ? "dsll" : "dsll32";
91d6fa6a 11254 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
82dd0097 11255 rot &= 0x1f;
8fc2e39e 11256 used_at = 1;
c0ebe874
RS
11257 macro_build (NULL, l, SHFT_FMT, AT, op[1], rot);
11258 macro_build (NULL, rr, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
11259 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
11260 }
11261 break;
11262
252b5132 11263 case M_ROL_I:
771c7ce4
TS
11264 {
11265 unsigned int rot;
11266
11267 if (imm_expr.X_op != O_constant)
82dd0097 11268 as_bad (_("Improper rotate count"));
771c7ce4 11269 rot = imm_expr.X_add_number & 0x1f;
fef14a42 11270 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 11271 {
c0ebe874
RS
11272 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1],
11273 (32 - rot) & 0x1f);
8fc2e39e 11274 break;
60b63b72 11275 }
483fc7cd 11276 if (rot == 0)
483fc7cd 11277 {
c0ebe874 11278 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 11279 break;
483fc7cd 11280 }
8fc2e39e 11281 used_at = 1;
c0ebe874
RS
11282 macro_build (NULL, "sll", SHFT_FMT, AT, op[1], rot);
11283 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
11284 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
11285 }
11286 break;
11287
11288 case M_DROR:
fef14a42 11289 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 11290 {
c0ebe874 11291 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 11292 break;
82dd0097 11293 }
8fc2e39e 11294 used_at = 1;
c0ebe874
RS
11295 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
11296 macro_build (NULL, "dsllv", "d,t,s", AT, op[1], AT);
11297 macro_build (NULL, "dsrlv", "d,t,s", op[0], op[1], op[2]);
11298 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
11299 break;
11300
11301 case M_ROR:
fef14a42 11302 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 11303 {
c0ebe874 11304 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 11305 break;
82dd0097 11306 }
8fc2e39e 11307 used_at = 1;
c0ebe874
RS
11308 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
11309 macro_build (NULL, "sllv", "d,t,s", AT, op[1], AT);
11310 macro_build (NULL, "srlv", "d,t,s", op[0], op[1], op[2]);
11311 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
11312 break;
11313
771c7ce4
TS
11314 case M_DROR_I:
11315 {
11316 unsigned int rot;
91d6fa6a
NC
11317 char *l;
11318 char *rr;
771c7ce4
TS
11319
11320 if (imm_expr.X_op != O_constant)
82dd0097 11321 as_bad (_("Improper rotate count"));
771c7ce4 11322 rot = imm_expr.X_add_number & 0x3f;
fef14a42 11323 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
11324 {
11325 if (rot >= 32)
c0ebe874 11326 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
82dd0097 11327 else
c0ebe874 11328 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 11329 break;
82dd0097 11330 }
483fc7cd 11331 if (rot == 0)
483fc7cd 11332 {
c0ebe874 11333 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 11334 break;
483fc7cd 11335 }
91d6fa6a 11336 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
82dd0097
CD
11337 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
11338 rot &= 0x1f;
8fc2e39e 11339 used_at = 1;
c0ebe874
RS
11340 macro_build (NULL, rr, SHFT_FMT, AT, op[1], rot);
11341 macro_build (NULL, l, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
11342 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
11343 }
11344 break;
11345
252b5132 11346 case M_ROR_I:
771c7ce4
TS
11347 {
11348 unsigned int rot;
11349
11350 if (imm_expr.X_op != O_constant)
82dd0097 11351 as_bad (_("Improper rotate count"));
771c7ce4 11352 rot = imm_expr.X_add_number & 0x1f;
fef14a42 11353 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 11354 {
c0ebe874 11355 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 11356 break;
82dd0097 11357 }
483fc7cd 11358 if (rot == 0)
483fc7cd 11359 {
c0ebe874 11360 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 11361 break;
483fc7cd 11362 }
8fc2e39e 11363 used_at = 1;
c0ebe874
RS
11364 macro_build (NULL, "srl", SHFT_FMT, AT, op[1], rot);
11365 macro_build (NULL, "sll", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
11366 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4 11367 }
252b5132
RH
11368 break;
11369
252b5132 11370 case M_SEQ:
c0ebe874
RS
11371 if (op[1] == 0)
11372 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[2], BFD_RELOC_LO16);
11373 else if (op[2] == 0)
11374 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
11375 else
11376 {
c0ebe874
RS
11377 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
11378 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
252b5132 11379 }
8fc2e39e 11380 break;
252b5132
RH
11381
11382 case M_SEQ_I:
11383 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
11384 {
c0ebe874 11385 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 11386 break;
252b5132 11387 }
c0ebe874 11388 if (op[1] == 0)
252b5132
RH
11389 {
11390 as_warn (_("Instruction %s: result is always false"),
11391 ip->insn_mo->name);
c0ebe874 11392 move_register (op[0], 0);
8fc2e39e 11393 break;
252b5132 11394 }
dd3cbb7e
NC
11395 if (CPU_HAS_SEQ (mips_opts.arch)
11396 && -512 <= imm_expr.X_add_number
11397 && imm_expr.X_add_number < 512)
11398 {
c0ebe874 11399 macro_build (NULL, "seqi", "t,r,+Q", op[0], op[1],
750bdd57 11400 (int) imm_expr.X_add_number);
dd3cbb7e
NC
11401 break;
11402 }
252b5132
RH
11403 if (imm_expr.X_op == O_constant
11404 && imm_expr.X_add_number >= 0
11405 && imm_expr.X_add_number < 0x10000)
c0ebe874 11406 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
11407 else if (imm_expr.X_op == O_constant
11408 && imm_expr.X_add_number > -0x8000
11409 && imm_expr.X_add_number < 0)
11410 {
11411 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 11412 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
c0ebe874 11413 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 11414 }
dd3cbb7e
NC
11415 else if (CPU_HAS_SEQ (mips_opts.arch))
11416 {
11417 used_at = 1;
11418 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 11419 macro_build (NULL, "seq", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
11420 break;
11421 }
252b5132
RH
11422 else
11423 {
67c0d1eb 11424 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 11425 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
11426 used_at = 1;
11427 }
c0ebe874 11428 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 11429 break;
252b5132 11430
c0ebe874 11431 case M_SGE: /* X >= Y <==> not (X < Y) */
252b5132
RH
11432 s = "slt";
11433 goto sge;
11434 case M_SGEU:
11435 s = "sltu";
11436 sge:
c0ebe874
RS
11437 macro_build (NULL, s, "d,v,t", op[0], op[1], op[2]);
11438 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 11439 break;
252b5132 11440
c0ebe874 11441 case M_SGE_I: /* X >= I <==> not (X < I) */
252b5132
RH
11442 case M_SGEU_I:
11443 if (imm_expr.X_op == O_constant
11444 && imm_expr.X_add_number >= -0x8000
11445 && imm_expr.X_add_number < 0x8000)
c0ebe874
RS
11446 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
11447 op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
11448 else
11449 {
67c0d1eb
RS
11450 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
11451 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
c0ebe874 11452 op[0], op[1], AT);
252b5132
RH
11453 used_at = 1;
11454 }
c0ebe874 11455 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 11456 break;
252b5132 11457
c0ebe874 11458 case M_SGT: /* X > Y <==> Y < X */
252b5132
RH
11459 s = "slt";
11460 goto sgt;
11461 case M_SGTU:
11462 s = "sltu";
11463 sgt:
c0ebe874 11464 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
8fc2e39e 11465 break;
252b5132 11466
c0ebe874 11467 case M_SGT_I: /* X > I <==> I < X */
252b5132
RH
11468 s = "slt";
11469 goto sgti;
11470 case M_SGTU_I:
11471 s = "sltu";
11472 sgti:
8fc2e39e 11473 used_at = 1;
67c0d1eb 11474 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 11475 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
252b5132
RH
11476 break;
11477
c0ebe874 11478 case M_SLE: /* X <= Y <==> Y >= X <==> not (Y < X) */
252b5132
RH
11479 s = "slt";
11480 goto sle;
11481 case M_SLEU:
11482 s = "sltu";
11483 sle:
c0ebe874
RS
11484 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
11485 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 11486 break;
252b5132 11487
c0ebe874 11488 case M_SLE_I: /* X <= I <==> I >= X <==> not (I < X) */
252b5132
RH
11489 s = "slt";
11490 goto slei;
11491 case M_SLEU_I:
11492 s = "sltu";
11493 slei:
8fc2e39e 11494 used_at = 1;
67c0d1eb 11495 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874
RS
11496 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
11497 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
252b5132
RH
11498 break;
11499
11500 case M_SLT_I:
11501 if (imm_expr.X_op == O_constant
11502 && imm_expr.X_add_number >= -0x8000
11503 && imm_expr.X_add_number < 0x8000)
11504 {
c0ebe874
RS
11505 macro_build (&imm_expr, "slti", "t,r,j", op[0], op[1],
11506 BFD_RELOC_LO16);
8fc2e39e 11507 break;
252b5132 11508 }
8fc2e39e 11509 used_at = 1;
67c0d1eb 11510 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 11511 macro_build (NULL, "slt", "d,v,t", op[0], op[1], AT);
252b5132
RH
11512 break;
11513
11514 case M_SLTU_I:
11515 if (imm_expr.X_op == O_constant
11516 && imm_expr.X_add_number >= -0x8000
11517 && imm_expr.X_add_number < 0x8000)
11518 {
c0ebe874 11519 macro_build (&imm_expr, "sltiu", "t,r,j", op[0], op[1],
17a2f251 11520 BFD_RELOC_LO16);
8fc2e39e 11521 break;
252b5132 11522 }
8fc2e39e 11523 used_at = 1;
67c0d1eb 11524 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 11525 macro_build (NULL, "sltu", "d,v,t", op[0], op[1], AT);
252b5132
RH
11526 break;
11527
11528 case M_SNE:
c0ebe874
RS
11529 if (op[1] == 0)
11530 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[2]);
11531 else if (op[2] == 0)
11532 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
252b5132
RH
11533 else
11534 {
c0ebe874
RS
11535 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
11536 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
252b5132 11537 }
8fc2e39e 11538 break;
252b5132
RH
11539
11540 case M_SNE_I:
11541 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
11542 {
c0ebe874 11543 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
8fc2e39e 11544 break;
252b5132 11545 }
c0ebe874 11546 if (op[1] == 0)
252b5132
RH
11547 {
11548 as_warn (_("Instruction %s: result is always true"),
11549 ip->insn_mo->name);
67c0d1eb 11550 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
c0ebe874 11551 op[0], 0, BFD_RELOC_LO16);
8fc2e39e 11552 break;
252b5132 11553 }
dd3cbb7e
NC
11554 if (CPU_HAS_SEQ (mips_opts.arch)
11555 && -512 <= imm_expr.X_add_number
11556 && imm_expr.X_add_number < 512)
11557 {
c0ebe874 11558 macro_build (NULL, "snei", "t,r,+Q", op[0], op[1],
750bdd57 11559 (int) imm_expr.X_add_number);
dd3cbb7e
NC
11560 break;
11561 }
252b5132
RH
11562 if (imm_expr.X_op == O_constant
11563 && imm_expr.X_add_number >= 0
11564 && imm_expr.X_add_number < 0x10000)
11565 {
c0ebe874
RS
11566 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1],
11567 BFD_RELOC_LO16);
252b5132
RH
11568 }
11569 else if (imm_expr.X_op == O_constant
11570 && imm_expr.X_add_number > -0x8000
11571 && imm_expr.X_add_number < 0)
11572 {
11573 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 11574 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
c0ebe874 11575 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 11576 }
dd3cbb7e
NC
11577 else if (CPU_HAS_SEQ (mips_opts.arch))
11578 {
11579 used_at = 1;
11580 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 11581 macro_build (NULL, "sne", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
11582 break;
11583 }
252b5132
RH
11584 else
11585 {
67c0d1eb 11586 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 11587 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
11588 used_at = 1;
11589 }
c0ebe874 11590 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
8fc2e39e 11591 break;
252b5132 11592
df58fc94
RS
11593 case M_SUB_I:
11594 s = "addi";
11595 s2 = "sub";
11596 goto do_subi;
11597 case M_SUBU_I:
11598 s = "addiu";
11599 s2 = "subu";
11600 goto do_subi;
252b5132
RH
11601 case M_DSUB_I:
11602 dbl = 1;
df58fc94
RS
11603 s = "daddi";
11604 s2 = "dsub";
11605 if (!mips_opts.micromips)
11606 goto do_subi;
252b5132 11607 if (imm_expr.X_op == O_constant
df58fc94
RS
11608 && imm_expr.X_add_number > -0x200
11609 && imm_expr.X_add_number <= 0x200)
252b5132 11610 {
c0ebe874 11611 macro_build (NULL, s, "t,r,.", op[0], op[1], -imm_expr.X_add_number);
8fc2e39e 11612 break;
252b5132 11613 }
df58fc94 11614 goto do_subi_i;
252b5132
RH
11615 case M_DSUBU_I:
11616 dbl = 1;
df58fc94
RS
11617 s = "daddiu";
11618 s2 = "dsubu";
11619 do_subi:
252b5132
RH
11620 if (imm_expr.X_op == O_constant
11621 && imm_expr.X_add_number > -0x8000
11622 && imm_expr.X_add_number <= 0x8000)
11623 {
11624 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 11625 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 11626 break;
252b5132 11627 }
df58fc94 11628 do_subi_i:
8fc2e39e 11629 used_at = 1;
67c0d1eb 11630 load_register (AT, &imm_expr, dbl);
c0ebe874 11631 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
11632 break;
11633
11634 case M_TEQ_I:
11635 s = "teq";
11636 goto trap;
11637 case M_TGE_I:
11638 s = "tge";
11639 goto trap;
11640 case M_TGEU_I:
11641 s = "tgeu";
11642 goto trap;
11643 case M_TLT_I:
11644 s = "tlt";
11645 goto trap;
11646 case M_TLTU_I:
11647 s = "tltu";
11648 goto trap;
11649 case M_TNE_I:
11650 s = "tne";
11651 trap:
8fc2e39e 11652 used_at = 1;
67c0d1eb 11653 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 11654 macro_build (NULL, s, "s,t", op[0], AT);
252b5132
RH
11655 break;
11656
252b5132 11657 case M_TRUNCWS:
43841e91 11658 case M_TRUNCWD:
df58fc94 11659 gas_assert (!mips_opts.micromips);
0aa27725 11660 gas_assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 11661 used_at = 1;
252b5132
RH
11662
11663 /*
11664 * Is the double cfc1 instruction a bug in the mips assembler;
11665 * or is there a reason for it?
11666 */
7d10b47d 11667 start_noreorder ();
c0ebe874
RS
11668 macro_build (NULL, "cfc1", "t,G", op[2], RA);
11669 macro_build (NULL, "cfc1", "t,G", op[2], RA);
67c0d1eb 11670 macro_build (NULL, "nop", "");
252b5132 11671 expr1.X_add_number = 3;
c0ebe874 11672 macro_build (&expr1, "ori", "t,r,i", AT, op[2], BFD_RELOC_LO16);
252b5132 11673 expr1.X_add_number = 2;
67c0d1eb
RS
11674 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
11675 macro_build (NULL, "ctc1", "t,G", AT, RA);
11676 macro_build (NULL, "nop", "");
11677 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
c0ebe874
RS
11678 op[0], op[1]);
11679 macro_build (NULL, "ctc1", "t,G", op[2], RA);
67c0d1eb 11680 macro_build (NULL, "nop", "");
7d10b47d 11681 end_noreorder ();
252b5132
RH
11682 break;
11683
f2ae14a1 11684 case M_ULH_AB:
252b5132 11685 s = "lb";
df58fc94
RS
11686 s2 = "lbu";
11687 off = 1;
11688 goto uld_st;
f2ae14a1 11689 case M_ULHU_AB:
252b5132 11690 s = "lbu";
df58fc94
RS
11691 s2 = "lbu";
11692 off = 1;
11693 goto uld_st;
f2ae14a1 11694 case M_ULW_AB:
df58fc94
RS
11695 s = "lwl";
11696 s2 = "lwr";
7f3c4072 11697 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
11698 off = 3;
11699 goto uld_st;
f2ae14a1 11700 case M_ULD_AB:
252b5132
RH
11701 s = "ldl";
11702 s2 = "ldr";
7f3c4072 11703 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 11704 off = 7;
df58fc94 11705 goto uld_st;
f2ae14a1 11706 case M_USH_AB:
df58fc94
RS
11707 s = "sb";
11708 s2 = "sb";
11709 off = 1;
11710 ust = 1;
11711 goto uld_st;
f2ae14a1 11712 case M_USW_AB:
df58fc94
RS
11713 s = "swl";
11714 s2 = "swr";
7f3c4072 11715 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 11716 off = 3;
df58fc94
RS
11717 ust = 1;
11718 goto uld_st;
f2ae14a1 11719 case M_USD_AB:
df58fc94
RS
11720 s = "sdl";
11721 s2 = "sdr";
7f3c4072 11722 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
11723 off = 7;
11724 ust = 1;
11725
11726 uld_st:
c0ebe874 11727 breg = op[2];
f2ae14a1 11728 large_offset = !small_offset_p (off, align, offbits);
df58fc94
RS
11729 ep = &offset_expr;
11730 expr1.X_add_number = 0;
f2ae14a1 11731 if (large_offset)
df58fc94
RS
11732 {
11733 used_at = 1;
11734 tempreg = AT;
f2ae14a1
RS
11735 if (small_offset_p (0, align, 16))
11736 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg, -1,
11737 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
11738 else
11739 {
11740 load_address (tempreg, ep, &used_at);
11741 if (breg != 0)
11742 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11743 tempreg, tempreg, breg);
11744 }
11745 offset_reloc[0] = BFD_RELOC_LO16;
11746 offset_reloc[1] = BFD_RELOC_UNUSED;
11747 offset_reloc[2] = BFD_RELOC_UNUSED;
df58fc94 11748 breg = tempreg;
c0ebe874 11749 tempreg = op[0];
df58fc94
RS
11750 ep = &expr1;
11751 }
c0ebe874 11752 else if (!ust && op[0] == breg)
8fc2e39e
TS
11753 {
11754 used_at = 1;
11755 tempreg = AT;
11756 }
252b5132 11757 else
c0ebe874 11758 tempreg = op[0];
af22f5b2 11759
df58fc94
RS
11760 if (off == 1)
11761 goto ulh_sh;
252b5132 11762
90ecf173 11763 if (!target_big_endian)
df58fc94 11764 ep->X_add_number += off;
f2ae14a1 11765 if (offbits == 12)
c8276761 11766 macro_build (NULL, s, "t,~(b)", tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
11767 else
11768 macro_build (ep, s, "t,o(b)", tempreg, -1,
11769 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94 11770
90ecf173 11771 if (!target_big_endian)
df58fc94 11772 ep->X_add_number -= off;
252b5132 11773 else
df58fc94 11774 ep->X_add_number += off;
f2ae14a1 11775 if (offbits == 12)
df58fc94 11776 macro_build (NULL, s2, "t,~(b)",
c8276761 11777 tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
11778 else
11779 macro_build (ep, s2, "t,o(b)", tempreg, -1,
11780 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 11781
df58fc94 11782 /* If necessary, move the result in tempreg to the final destination. */
c0ebe874 11783 if (!ust && op[0] != tempreg)
df58fc94
RS
11784 {
11785 /* Protect second load's delay slot. */
11786 load_delay_nop ();
c0ebe874 11787 move_register (op[0], tempreg);
df58fc94 11788 }
8fc2e39e 11789 break;
252b5132 11790
df58fc94 11791 ulh_sh:
d6bc6245 11792 used_at = 1;
df58fc94
RS
11793 if (target_big_endian == ust)
11794 ep->X_add_number += off;
c0ebe874 11795 tempreg = ust || large_offset ? op[0] : AT;
f2ae14a1
RS
11796 macro_build (ep, s, "t,o(b)", tempreg, -1,
11797 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94
RS
11798
11799 /* For halfword transfers we need a temporary register to shuffle
11800 bytes. Unfortunately for M_USH_A we have none available before
11801 the next store as AT holds the base address. We deal with this
11802 case by clobbering TREG and then restoring it as with ULH. */
c0ebe874 11803 tempreg = ust == large_offset ? op[0] : AT;
df58fc94 11804 if (ust)
c0ebe874 11805 macro_build (NULL, "srl", SHFT_FMT, tempreg, op[0], 8);
df58fc94
RS
11806
11807 if (target_big_endian == ust)
11808 ep->X_add_number -= off;
252b5132 11809 else
df58fc94 11810 ep->X_add_number += off;
f2ae14a1
RS
11811 macro_build (ep, s2, "t,o(b)", tempreg, -1,
11812 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 11813
df58fc94 11814 /* For M_USH_A re-retrieve the LSB. */
f2ae14a1 11815 if (ust && large_offset)
df58fc94
RS
11816 {
11817 if (target_big_endian)
11818 ep->X_add_number += off;
11819 else
11820 ep->X_add_number -= off;
f2ae14a1
RS
11821 macro_build (&expr1, "lbu", "t,o(b)", AT, -1,
11822 offset_reloc[0], offset_reloc[1], offset_reloc[2], AT);
df58fc94
RS
11823 }
11824 /* For ULH and M_USH_A OR the LSB in. */
f2ae14a1 11825 if (!ust || large_offset)
df58fc94 11826 {
c0ebe874 11827 tempreg = !large_offset ? AT : op[0];
df58fc94 11828 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
c0ebe874 11829 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
df58fc94 11830 }
252b5132
RH
11831 break;
11832
11833 default:
11834 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 11835 are added dynamically. */
252b5132
RH
11836 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
11837 break;
11838 }
741fe287 11839 if (!mips_opts.at && used_at)
8fc2e39e 11840 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
11841}
11842
11843/* Implement macros in mips16 mode. */
11844
11845static void
17a2f251 11846mips16_macro (struct mips_cl_insn *ip)
252b5132 11847{
c0ebe874 11848 const struct mips_operand_array *operands;
252b5132 11849 int mask;
c0ebe874 11850 int tmp;
252b5132
RH
11851 expressionS expr1;
11852 int dbl;
11853 const char *s, *s2, *s3;
c0ebe874
RS
11854 unsigned int op[MAX_OPERANDS];
11855 unsigned int i;
252b5132
RH
11856
11857 mask = ip->insn_mo->mask;
11858
c0ebe874
RS
11859 operands = insn_operands (ip);
11860 for (i = 0; i < MAX_OPERANDS; i++)
11861 if (operands->operand[i])
11862 op[i] = insn_extract_operand (ip, operands->operand[i]);
11863 else
11864 op[i] = -1;
252b5132 11865
252b5132
RH
11866 expr1.X_op = O_constant;
11867 expr1.X_op_symbol = NULL;
11868 expr1.X_add_symbol = NULL;
11869 expr1.X_add_number = 1;
11870
11871 dbl = 0;
11872
11873 switch (mask)
11874 {
11875 default:
b37df7c4 11876 abort ();
252b5132
RH
11877
11878 case M_DDIV_3:
11879 dbl = 1;
11880 case M_DIV_3:
11881 s = "mflo";
11882 goto do_div3;
11883 case M_DREM_3:
11884 dbl = 1;
11885 case M_REM_3:
11886 s = "mfhi";
11887 do_div3:
7d10b47d 11888 start_noreorder ();
c0ebe874 11889 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", op[1], op[2]);
252b5132 11890 expr1.X_add_number = 2;
c0ebe874 11891 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 11892 macro_build (NULL, "break", "6", 7);
bdaaa2e1 11893
252b5132
RH
11894 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
11895 since that causes an overflow. We should do that as well,
11896 but I don't see how to do the comparisons without a temporary
11897 register. */
7d10b47d 11898 end_noreorder ();
c0ebe874 11899 macro_build (NULL, s, "x", op[0]);
252b5132
RH
11900 break;
11901
11902 case M_DIVU_3:
11903 s = "divu";
11904 s2 = "mflo";
11905 goto do_divu3;
11906 case M_REMU_3:
11907 s = "divu";
11908 s2 = "mfhi";
11909 goto do_divu3;
11910 case M_DDIVU_3:
11911 s = "ddivu";
11912 s2 = "mflo";
11913 goto do_divu3;
11914 case M_DREMU_3:
11915 s = "ddivu";
11916 s2 = "mfhi";
11917 do_divu3:
7d10b47d 11918 start_noreorder ();
c0ebe874 11919 macro_build (NULL, s, "0,x,y", op[1], op[2]);
252b5132 11920 expr1.X_add_number = 2;
c0ebe874 11921 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 11922 macro_build (NULL, "break", "6", 7);
7d10b47d 11923 end_noreorder ();
c0ebe874 11924 macro_build (NULL, s2, "x", op[0]);
252b5132
RH
11925 break;
11926
11927 case M_DMUL:
11928 dbl = 1;
11929 case M_MUL:
c0ebe874
RS
11930 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", op[1], op[2]);
11931 macro_build (NULL, "mflo", "x", op[0]);
8fc2e39e 11932 break;
252b5132
RH
11933
11934 case M_DSUBU_I:
11935 dbl = 1;
11936 goto do_subu;
11937 case M_SUBU_I:
11938 do_subu:
11939 if (imm_expr.X_op != O_constant)
11940 as_bad (_("Unsupported large constant"));
11941 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 11942 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", op[0], op[1]);
252b5132
RH
11943 break;
11944
11945 case M_SUBU_I_2:
11946 if (imm_expr.X_op != O_constant)
11947 as_bad (_("Unsupported large constant"));
11948 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 11949 macro_build (&imm_expr, "addiu", "x,k", op[0]);
252b5132
RH
11950 break;
11951
11952 case M_DSUBU_I_2:
11953 if (imm_expr.X_op != O_constant)
11954 as_bad (_("Unsupported large constant"));
11955 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 11956 macro_build (&imm_expr, "daddiu", "y,j", op[0]);
252b5132
RH
11957 break;
11958
11959 case M_BEQ:
11960 s = "cmp";
11961 s2 = "bteqz";
11962 goto do_branch;
11963 case M_BNE:
11964 s = "cmp";
11965 s2 = "btnez";
11966 goto do_branch;
11967 case M_BLT:
11968 s = "slt";
11969 s2 = "btnez";
11970 goto do_branch;
11971 case M_BLTU:
11972 s = "sltu";
11973 s2 = "btnez";
11974 goto do_branch;
11975 case M_BLE:
11976 s = "slt";
11977 s2 = "bteqz";
11978 goto do_reverse_branch;
11979 case M_BLEU:
11980 s = "sltu";
11981 s2 = "bteqz";
11982 goto do_reverse_branch;
11983 case M_BGE:
11984 s = "slt";
11985 s2 = "bteqz";
11986 goto do_branch;
11987 case M_BGEU:
11988 s = "sltu";
11989 s2 = "bteqz";
11990 goto do_branch;
11991 case M_BGT:
11992 s = "slt";
11993 s2 = "btnez";
11994 goto do_reverse_branch;
11995 case M_BGTU:
11996 s = "sltu";
11997 s2 = "btnez";
11998
11999 do_reverse_branch:
c0ebe874
RS
12000 tmp = op[1];
12001 op[1] = op[0];
12002 op[0] = tmp;
252b5132
RH
12003
12004 do_branch:
c0ebe874 12005 macro_build (NULL, s, "x,y", op[0], op[1]);
67c0d1eb 12006 macro_build (&offset_expr, s2, "p");
252b5132
RH
12007 break;
12008
12009 case M_BEQ_I:
12010 s = "cmpi";
12011 s2 = "bteqz";
12012 s3 = "x,U";
12013 goto do_branch_i;
12014 case M_BNE_I:
12015 s = "cmpi";
12016 s2 = "btnez";
12017 s3 = "x,U";
12018 goto do_branch_i;
12019 case M_BLT_I:
12020 s = "slti";
12021 s2 = "btnez";
12022 s3 = "x,8";
12023 goto do_branch_i;
12024 case M_BLTU_I:
12025 s = "sltiu";
12026 s2 = "btnez";
12027 s3 = "x,8";
12028 goto do_branch_i;
12029 case M_BLE_I:
12030 s = "slti";
12031 s2 = "btnez";
12032 s3 = "x,8";
12033 goto do_addone_branch_i;
12034 case M_BLEU_I:
12035 s = "sltiu";
12036 s2 = "btnez";
12037 s3 = "x,8";
12038 goto do_addone_branch_i;
12039 case M_BGE_I:
12040 s = "slti";
12041 s2 = "bteqz";
12042 s3 = "x,8";
12043 goto do_branch_i;
12044 case M_BGEU_I:
12045 s = "sltiu";
12046 s2 = "bteqz";
12047 s3 = "x,8";
12048 goto do_branch_i;
12049 case M_BGT_I:
12050 s = "slti";
12051 s2 = "bteqz";
12052 s3 = "x,8";
12053 goto do_addone_branch_i;
12054 case M_BGTU_I:
12055 s = "sltiu";
12056 s2 = "bteqz";
12057 s3 = "x,8";
12058
12059 do_addone_branch_i:
12060 if (imm_expr.X_op != O_constant)
12061 as_bad (_("Unsupported large constant"));
12062 ++imm_expr.X_add_number;
12063
12064 do_branch_i:
c0ebe874 12065 macro_build (&imm_expr, s, s3, op[0]);
67c0d1eb 12066 macro_build (&offset_expr, s2, "p");
252b5132
RH
12067 break;
12068
12069 case M_ABS:
12070 expr1.X_add_number = 0;
c0ebe874
RS
12071 macro_build (&expr1, "slti", "x,8", op[1]);
12072 if (op[0] != op[1])
12073 macro_build (NULL, "move", "y,X", op[0], mips16_to_32_reg_map[op[1]]);
252b5132 12074 expr1.X_add_number = 2;
67c0d1eb 12075 macro_build (&expr1, "bteqz", "p");
c0ebe874 12076 macro_build (NULL, "neg", "x,w", op[0], op[0]);
0acfaea6 12077 break;
252b5132
RH
12078 }
12079}
12080
77bd4346
RS
12081/* Assemble an instruction into its binary format. If the instruction
12082 is a macro, set imm_expr, imm2_expr and offset_expr to the values
12083 associated with "I", "+I" and "A" operands respectively. Otherwise
12084 store the value of the relocatable field (if any) in offset_expr.
12085 In both cases set offset_reloc to the relocation operators applied
12086 to offset_expr. */
252b5132
RH
12087
12088static void
17a2f251 12089mips_ip (char *str, struct mips_cl_insn *ip)
252b5132 12090{
df58fc94
RS
12091 bfd_boolean wrong_delay_slot_insns = FALSE;
12092 bfd_boolean need_delay_slot_ok = TRUE;
12093 struct mips_opcode *firstinsn = NULL;
12094 const struct mips_opcode *past;
12095 struct hash_control *hash;
252b5132 12096 const char *args;
43841e91 12097 char c = 0;
252b5132 12098 struct mips_opcode *insn;
df58fc94 12099 long opend;
a40bc9dd 12100 char *name;
df58fc94 12101 char *dot;
a92713e6 12102 char format;
a40bc9dd 12103 long end;
9e12b7a2
RS
12104 const struct mips_operand *operand;
12105 struct mips_arg_info arg;
a92713e6
RS
12106 struct mips_operand_token *tokens;
12107 bfd_boolean optional_reg;
252b5132
RH
12108
12109 insn_error = NULL;
12110
df58fc94
RS
12111 if (mips_opts.micromips)
12112 {
12113 hash = micromips_op_hash;
12114 past = &micromips_opcodes[bfd_micromips_num_opcodes];
12115 }
12116 else
12117 {
12118 hash = op_hash;
12119 past = &mips_opcodes[NUMOPCODES];
12120 }
12121 forced_insn_length = 0;
252b5132 12122 insn = NULL;
252b5132 12123
df58fc94 12124 /* We first try to match an instruction up to a space or to the end. */
a40bc9dd
RS
12125 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
12126 continue;
bdaaa2e1 12127
a40bc9dd
RS
12128 /* Make a copy of the instruction so that we can fiddle with it. */
12129 name = alloca (end + 1);
12130 memcpy (name, str, end);
12131 name[end] = '\0';
252b5132 12132
df58fc94
RS
12133 for (;;)
12134 {
12135 insn = (struct mips_opcode *) hash_find (hash, name);
12136
12137 if (insn != NULL || !mips_opts.micromips)
12138 break;
12139 if (forced_insn_length)
12140 break;
12141
12142 /* See if there's an instruction size override suffix,
12143 either `16' or `32', at the end of the mnemonic proper,
12144 that defines the operation, i.e. before the first `.'
12145 character if any. Strip it and retry. */
12146 dot = strchr (name, '.');
12147 opend = dot != NULL ? dot - name : end;
12148 if (opend < 3)
12149 break;
12150 if (name[opend - 2] == '1' && name[opend - 1] == '6')
12151 forced_insn_length = 2;
12152 else if (name[opend - 2] == '3' && name[opend - 1] == '2')
12153 forced_insn_length = 4;
12154 else
12155 break;
12156 memcpy (name + opend - 2, name + opend, end - opend + 1);
12157 }
252b5132
RH
12158 if (insn == NULL)
12159 {
a40bc9dd
RS
12160 insn_error = _("Unrecognized opcode");
12161 return;
252b5132
RH
12162 }
12163
a92713e6
RS
12164 if (strcmp (name, "li.s") == 0)
12165 format = 'f';
12166 else if (strcmp (name, "li.d") == 0)
12167 format = 'd';
12168 else
12169 format = 0;
12170 tokens = mips_parse_arguments (str + end, format);
12171 if (!tokens)
12172 return;
12173
df58fc94
RS
12174 /* For microMIPS instructions placed in a fixed-length branch delay slot
12175 we make up to two passes over the relevant fragment of the opcode
12176 table. First we try instructions that meet the delay slot's length
12177 requirement. If none matched, then we retry with the remaining ones
12178 and if one matches, then we use it and then issue an appropriate
12179 warning later on. */
252b5132
RH
12180 for (;;)
12181 {
df58fc94
RS
12182 bfd_boolean delay_slot_ok;
12183 bfd_boolean size_ok;
b34976b6 12184 bfd_boolean ok;
9e12b7a2 12185 bfd_boolean more_alts;
252b5132 12186
a40bc9dd 12187 gas_assert (strcmp (insn->name, name) == 0);
252b5132 12188
f79e2745 12189 ok = is_opcode_valid (insn);
df58fc94
RS
12190 size_ok = is_size_valid (insn);
12191 delay_slot_ok = is_delay_slot_valid (insn);
12192 if (!delay_slot_ok && !wrong_delay_slot_insns)
252b5132 12193 {
df58fc94
RS
12194 firstinsn = insn;
12195 wrong_delay_slot_insns = TRUE;
12196 }
9e12b7a2
RS
12197 more_alts = (insn + 1 < past
12198 && strcmp (insn[0].name, insn[1].name) == 0);
df58fc94
RS
12199 if (!ok || !size_ok || delay_slot_ok != need_delay_slot_ok)
12200 {
12201 static char buf[256];
12202
9e12b7a2 12203 if (more_alts)
252b5132
RH
12204 {
12205 ++insn;
12206 continue;
12207 }
df58fc94 12208 if (wrong_delay_slot_insns && need_delay_slot_ok)
beae10d5 12209 {
df58fc94
RS
12210 gas_assert (firstinsn);
12211 need_delay_slot_ok = FALSE;
12212 past = insn + 1;
12213 insn = firstinsn;
12214 continue;
252b5132 12215 }
df58fc94 12216
a92713e6 12217 obstack_free (&mips_operand_tokens, tokens);
df58fc94
RS
12218 if (insn_error)
12219 return;
12220
12221 if (!ok)
7bd942df 12222 sprintf (buf, _("Opcode not supported on this processor: %s (%s)"),
df58fc94
RS
12223 mips_cpu_info_from_arch (mips_opts.arch)->name,
12224 mips_cpu_info_from_isa (mips_opts.isa)->name);
833794fc
MR
12225 else if (mips_opts.insn32)
12226 sprintf (buf, _("Opcode not supported in the `insn32' mode"));
df58fc94
RS
12227 else
12228 sprintf (buf, _("Unrecognized %u-bit version of microMIPS opcode"),
12229 8 * forced_insn_length);
12230 insn_error = buf;
12231
12232 return;
252b5132
RH
12233 }
12234
f2ae14a1
RS
12235 imm_expr.X_op = O_absent;
12236 imm2_expr.X_op = O_absent;
12237 offset_expr.X_op = O_absent;
f2ae14a1
RS
12238 offset_reloc[0] = BFD_RELOC_UNUSED;
12239 offset_reloc[1] = BFD_RELOC_UNUSED;
12240 offset_reloc[2] = BFD_RELOC_UNUSED;
12241
1e915849 12242 create_insn (ip, insn);
268f6bed 12243 insn_error = NULL;
9e12b7a2
RS
12244 memset (&arg, 0, sizeof (arg));
12245 arg.insn = ip;
a92713e6 12246 arg.token = tokens;
9e12b7a2
RS
12247 arg.argnum = 1;
12248 arg.last_regno = ILLEGAL_REG;
12249 arg.dest_regno = ILLEGAL_REG;
12250 arg.soft_match = (more_alts
12251 || (wrong_delay_slot_insns && need_delay_slot_ok));
252b5132
RH
12252 for (args = insn->args;; ++args)
12253 {
a92713e6 12254 if (arg.token->type == OT_END)
252b5132 12255 {
9e12b7a2
RS
12256 /* Handle unary instructions in which only one operand is given.
12257 The source is then the same as the destination. */
12258 if (arg.opnum == 1 && *args == ',')
12259 switch (args[1])
12260 {
12261 case 'r':
12262 case 'v':
12263 case 'w':
12264 case 'W':
12265 case 'V':
a92713e6 12266 arg.token = tokens;
9e12b7a2 12267 arg.argnum = 1;
9e12b7a2
RS
12268 continue;
12269 }
74cd071d 12270
9e12b7a2
RS
12271 /* Treat elided base registers as $0. */
12272 if (strcmp (args, "(b)") == 0)
12273 args += 3;
74cd071d 12274
9e12b7a2
RS
12275 /* Fail the match if there were too few operands. */
12276 if (*args)
12277 break;
74cd071d 12278
9e12b7a2
RS
12279 /* Successful match. */
12280 if (arg.dest_regno == arg.last_regno
12281 && strncmp (ip->insn_mo->name, "jalr", 4) == 0)
74cd071d 12282 {
9e12b7a2
RS
12283 if (arg.opnum == 2)
12284 as_bad (_("Source and destination must be different"));
12285 else if (arg.last_regno == 31)
12286 as_bad (_("A destination register must be supplied"));
74cd071d 12287 }
9e12b7a2 12288 check_completed_insn (&arg);
a92713e6 12289 obstack_free (&mips_operand_tokens, tokens);
9e12b7a2
RS
12290 return;
12291 }
74cd071d 12292
9e12b7a2
RS
12293 /* Fail the match if the line has too many operands. */
12294 if (*args == 0)
12295 break;
12296
12297 /* Handle characters that need to match exactly. */
12298 if (*args == '(' || *args == ')' || *args == ',')
12299 {
a92713e6
RS
12300 if (match_char (&arg, *args))
12301 continue;
12302 break;
9e12b7a2 12303 }
74cd071d 12304
9e12b7a2
RS
12305 /* Handle special macro operands. Work out the properties of
12306 other operands. */
12307 arg.opnum += 1;
9e12b7a2 12308 arg.lax_max = FALSE;
a92713e6 12309 optional_reg = FALSE;
9e12b7a2
RS
12310 switch (*args)
12311 {
12312 case '+':
12313 switch (args[1])
74cd071d 12314 {
9e12b7a2
RS
12315 case '1':
12316 case '2':
12317 case '3':
12318 case '4':
12319 case 'B':
12320 case 'C':
12321 case 'F':
12322 case 'G':
12323 case 'H':
12324 case 'J':
12325 case 'Q':
12326 case 'S':
12327 case 's':
12328 /* If these integer forms come last, there is no other
12329 form of the instruction that could match. Prefer to
12330 give detailed error messages where possible. */
12331 if (args[2] == 0)
12332 arg.soft_match = FALSE;
12333 break;
12334
12335 case 'I':
12336 /* "+I" is like "I", except that imm2_expr is used. */
a92713e6
RS
12337 if (match_const_int (&arg, &imm2_expr.X_add_number, 0))
12338 imm2_expr.X_op = O_constant;
12339 else
12340 insn_error = _("absolute expression required");
9e12b7a2
RS
12341 if (HAVE_32BIT_GPRS)
12342 normalize_constant_expr (&imm2_expr);
9e12b7a2 12343 ++args;
74cd071d 12344 continue;
9e12b7a2
RS
12345
12346 case 'i':
12347 *offset_reloc = BFD_RELOC_MIPS_JMP;
12348 break;
74cd071d 12349 }
74cd071d
CF
12350 break;
12351
9e12b7a2
RS
12352 case '\'':
12353 case ':':
12354 case '@':
12355 case '^':
12356 case '$':
12357 case '\\':
12358 case '%':
12359 case '|':
03f66e8a 12360 case '0':
9e12b7a2
RS
12361 case '1':
12362 case '2':
12363 case '3':
12364 case '4':
12365 case '5':
12366 case '6':
12367 case '8':
12368 case 'B':
12369 case 'C':
12370 case 'J':
12371 case 'O':
12372 case 'P':
12373 case 'Q':
12374 case 'c':
12375 case 'h':
12376 case 'q':
12377 /* If these integer forms come last, there is no other
12378 form of the instruction that could match. Prefer to
12379 give detailed error messages where possible. */
12380 if (args[1] == 0)
12381 arg.soft_match = FALSE;
12382 break;
74cd071d 12383
9e12b7a2
RS
12384 case 'r':
12385 case 'v':
12386 case 'w':
12387 case 'W':
12388 case 'V':
12389 /* We have already matched a comma by this point, so the register
12390 is only optional if there is another operand to come. */
12391 gas_assert (arg.opnum == 2);
a92713e6 12392 optional_reg = (args[1] == ',');
9e12b7a2 12393 break;
ef2e4d86 12394
9e12b7a2 12395 case 'I':
a92713e6
RS
12396 if (match_const_int (&arg, &imm_expr.X_add_number, 0))
12397 imm_expr.X_op = O_constant;
12398 else
9e12b7a2
RS
12399 insn_error = _("absolute expression required");
12400 if (HAVE_32BIT_GPRS)
12401 normalize_constant_expr (&imm_expr);
ef2e4d86
CF
12402 continue;
12403
9e12b7a2 12404 case 'A':
a92713e6 12405 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
ef2e4d86 12406 {
9e12b7a2
RS
12407 /* Assume that the offset has been elided and that what
12408 we saw was a base register. The match will fail later
12409 if that assumption turns out to be wrong. */
12410 offset_expr.X_op = O_constant;
12411 offset_expr.X_add_number = 0;
ef2e4d86 12412 }
a92713e6
RS
12413 else if (match_expression (&arg, &offset_expr, offset_reloc))
12414 normalize_address_expr (&offset_expr);
ef2e4d86 12415 else
a92713e6 12416 insn_error = _("absolute expression required");
9e12b7a2 12417 continue;
ef2e4d86 12418
9e12b7a2 12419 case 'F':
a92713e6
RS
12420 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
12421 8, TRUE))
12422 insn_error = _("floating-point expression required");
89565f1b
RS
12423 continue;
12424
9e12b7a2 12425 case 'L':
a92713e6
RS
12426 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
12427 8, FALSE))
12428 insn_error = _("floating-point expression required");
89565f1b 12429 continue;
90ecf173 12430
89565f1b 12431 case 'f':
a92713e6
RS
12432 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
12433 4, TRUE))
12434 insn_error = _("floating-point expression required");
89565f1b 12435 continue;
b015e599 12436
89565f1b 12437 case 'l':
a92713e6
RS
12438 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
12439 4, FALSE))
12440 insn_error = _("floating-point expression required");
9e12b7a2 12441 continue;
252b5132 12442
9e12b7a2
RS
12443 /* ??? This is the traditional behavior, but is flaky if
12444 there are alternative versions of the same instruction
12445 for different subarchitectures. The next alternative
12446 might not be suitable. */
12447 case 'j':
12448 /* For compatibility with older assemblers, we accept
12449 0x8000-0xffff as signed 16-bit numbers when only
12450 signed numbers are allowed. */
12451 arg.lax_max = !more_alts;
12452 case 'i':
12453 /* Only accept non-constant operands if this is the
12454 final alternative. Later alternatives might include
12455 a macro implementation. */
12456 arg.allow_nonconst = !more_alts;
12457 break;
252b5132 12458
9e12b7a2
RS
12459 case 'u':
12460 /* There are no macro implementations for out-of-range values. */
12461 arg.allow_nonconst = TRUE;
12462 break;
60b63b72 12463
9e12b7a2
RS
12464 case 'o':
12465 /* There should always be a macro implementation. */
12466 arg.allow_nonconst = FALSE;
12467 break;
df58fc94 12468
9e12b7a2
RS
12469 case 'p':
12470 *offset_reloc = BFD_RELOC_16_PCREL_S2;
12471 break;
df58fc94 12472
9e12b7a2
RS
12473 case 'a':
12474 *offset_reloc = BFD_RELOC_MIPS_JMP;
df58fc94 12475 break;
60b63b72 12476
9e12b7a2 12477 case 'm':
df58fc94 12478 gas_assert (mips_opts.micromips);
9e12b7a2
RS
12479 c = args[1];
12480 switch (c)
12481 {
12482 case 't':
12483 case 'c':
12484 case 'e':
12485 /* We have already matched a comma by this point,
12486 so the register is only optional if there is another
12487 operand to come. */
12488 gas_assert (arg.opnum == 2);
a92713e6 12489 optional_reg = (args[2] == ',');
df58fc94
RS
12490 break;
12491
9e12b7a2
RS
12492 case 'D':
12493 case 'E':
12494 if (!forced_insn_length)
12495 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
12496 else if (c == 'D')
12497 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
12498 else
12499 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
df58fc94 12500 break;
9e12b7a2
RS
12501 }
12502 break;
12503 }
df58fc94 12504
9e12b7a2
RS
12505 operand = (mips_opts.micromips
12506 ? decode_micromips_operand (args)
12507 : decode_mips_operand (args));
12508 if (!operand)
12509 abort ();
df58fc94 12510
a92713e6
RS
12511 if (optional_reg
12512 && (arg.token[0].type != OT_REG
12513 || arg.token[1].type == OT_END))
9e12b7a2
RS
12514 {
12515 /* Assume that the register has been elided and is the
12516 same as the first operand. */
a92713e6 12517 arg.token = tokens;
9e12b7a2 12518 arg.argnum = 1;
9e12b7a2 12519 }
a92713e6
RS
12520
12521 if (!match_operand (&arg, operand))
9e12b7a2 12522 break;
60b63b72 12523
9e12b7a2
RS
12524 /* Skip prefixes. */
12525 if (*args == '+' || *args == 'm')
12526 args++;
60b63b72 12527
9e12b7a2 12528 continue;
252b5132
RH
12529 }
12530 /* Args don't match. */
df58fc94 12531 insn_error = _("Illegal operands");
9e12b7a2 12532 if (more_alts)
252b5132
RH
12533 {
12534 ++insn;
252b5132
RH
12535 continue;
12536 }
9e12b7a2 12537 if (wrong_delay_slot_insns && need_delay_slot_ok)
df58fc94
RS
12538 {
12539 gas_assert (firstinsn);
12540 need_delay_slot_ok = FALSE;
12541 past = insn + 1;
12542 insn = firstinsn;
12543 continue;
12544 }
a92713e6 12545 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
12546 return;
12547 }
12548}
12549
77bd4346
RS
12550/* As for mips_ip, but used when assembling MIPS16 code.
12551 Also set forced_insn_length to the resulting instruction size in
12552 bytes if the user explicitly requested a small or extended instruction. */
252b5132
RH
12553
12554static void
17a2f251 12555mips16_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
12556{
12557 char *s;
12558 const char *args;
12559 struct mips_opcode *insn;
364215c8
RS
12560 const struct mips_operand *operand;
12561 const struct mips_operand *ext_operand;
12562 struct mips_arg_info arg;
a92713e6
RS
12563 struct mips_operand_token *tokens;
12564 bfd_boolean optional_reg;
252b5132
RH
12565
12566 insn_error = NULL;
12567
df58fc94 12568 forced_insn_length = 0;
252b5132 12569
3882b010 12570 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
12571 ;
12572 switch (*s)
12573 {
12574 case '\0':
12575 break;
12576
12577 case ' ':
12578 *s++ = '\0';
12579 break;
12580
12581 case '.':
12582 if (s[1] == 't' && s[2] == ' ')
12583 {
12584 *s = '\0';
df58fc94 12585 forced_insn_length = 2;
252b5132
RH
12586 s += 3;
12587 break;
12588 }
12589 else if (s[1] == 'e' && s[2] == ' ')
12590 {
12591 *s = '\0';
df58fc94 12592 forced_insn_length = 4;
252b5132
RH
12593 s += 3;
12594 break;
12595 }
12596 /* Fall through. */
12597 default:
12598 insn_error = _("unknown opcode");
12599 return;
12600 }
12601
df58fc94
RS
12602 if (mips_opts.noautoextend && !forced_insn_length)
12603 forced_insn_length = 2;
252b5132
RH
12604
12605 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
12606 {
12607 insn_error = _("unrecognized opcode");
12608 return;
12609 }
12610
a92713e6
RS
12611 tokens = mips_parse_arguments (s, 0);
12612 if (!tokens)
12613 return;
12614
252b5132
RH
12615 for (;;)
12616 {
9b3f89ee 12617 bfd_boolean ok;
364215c8 12618 bfd_boolean more_alts;
77bd4346 12619 char relax_char;
9b3f89ee 12620
9c2799c2 12621 gas_assert (strcmp (insn->name, str) == 0);
252b5132 12622
037b32b9 12623 ok = is_opcode_valid_16 (insn);
364215c8
RS
12624 more_alts = (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes]
12625 && strcmp (insn[0].name, insn[1].name) == 0);
9b3f89ee
TS
12626 if (! ok)
12627 {
364215c8 12628 if (more_alts)
9b3f89ee
TS
12629 {
12630 ++insn;
12631 continue;
12632 }
12633 else
12634 {
12635 if (!insn_error)
12636 {
12637 static char buf[100];
12638 sprintf (buf,
7bd942df 12639 _("Opcode not supported on this processor: %s (%s)"),
9b3f89ee
TS
12640 mips_cpu_info_from_arch (mips_opts.arch)->name,
12641 mips_cpu_info_from_isa (mips_opts.isa)->name);
12642 insn_error = buf;
12643 }
a92713e6 12644 obstack_free (&mips_operand_tokens, tokens);
9b3f89ee
TS
12645 return;
12646 }
12647 }
12648
1e915849 12649 create_insn (ip, insn);
252b5132 12650 imm_expr.X_op = O_absent;
5f74bc13 12651 imm2_expr.X_op = O_absent;
252b5132 12652 offset_expr.X_op = O_absent;
f6688943
TS
12653 offset_reloc[0] = BFD_RELOC_UNUSED;
12654 offset_reloc[1] = BFD_RELOC_UNUSED;
12655 offset_reloc[2] = BFD_RELOC_UNUSED;
77bd4346 12656 relax_char = 0;
364215c8
RS
12657
12658 memset (&arg, 0, sizeof (arg));
12659 arg.insn = ip;
a92713e6 12660 arg.token = tokens;
364215c8
RS
12661 arg.argnum = 1;
12662 arg.last_regno = ILLEGAL_REG;
12663 arg.dest_regno = ILLEGAL_REG;
12664 arg.soft_match = more_alts;
12665 relax_char = 0;
252b5132
RH
12666 for (args = insn->args; 1; ++args)
12667 {
12668 int c;
12669
a92713e6 12670 if (arg.token->type == OT_END)
252b5132 12671 {
364215c8 12672 offsetT value;
252b5132 12673
364215c8
RS
12674 /* Handle unary instructions in which only one operand is given.
12675 The source is then the same as the destination. */
12676 if (arg.opnum == 1 && *args == ',')
12677 switch (args[1])
12678 {
12679 case 'v':
12680 case 'w':
a92713e6 12681 arg.token = tokens;
364215c8 12682 arg.argnum = 1;
364215c8
RS
12683 continue;
12684 }
252b5132 12685
364215c8
RS
12686 /* Fail the match if there were too few operands. */
12687 if (*args)
12688 break;
252b5132 12689
364215c8
RS
12690 /* Successful match. Stuff the immediate value in now, if
12691 we can. */
12692 if (insn->pinfo == INSN_MACRO)
252b5132 12693 {
0acfaea6 12694 gas_assert (relax_char == 0 || relax_char == 'p');
364215c8 12695 gas_assert (*offset_reloc == BFD_RELOC_UNUSED);
252b5132 12696 }
364215c8
RS
12697 else if (relax_char
12698 && offset_expr.X_op == O_constant
12699 && calculate_reloc (*offset_reloc,
12700 offset_expr.X_add_number,
12701 &value))
252b5132 12702 {
364215c8
RS
12703 mips16_immed (NULL, 0, relax_char, *offset_reloc, value,
12704 forced_insn_length, &ip->insn_opcode);
12705 offset_expr.X_op = O_absent;
12706 *offset_reloc = BFD_RELOC_UNUSED;
252b5132 12707 }
364215c8 12708 else if (relax_char && *offset_reloc != BFD_RELOC_UNUSED)
252b5132 12709 {
364215c8
RS
12710 if (forced_insn_length == 2)
12711 as_bad (_("invalid unextended operand value"));
12712 forced_insn_length = 4;
12713 ip->insn_opcode |= MIPS16_EXTEND;
252b5132 12714 }
364215c8
RS
12715 else if (relax_char)
12716 *offset_reloc = (int) BFD_RELOC_UNUSED + relax_char;
252b5132 12717
364215c8 12718 check_completed_insn (&arg);
a92713e6 12719 obstack_free (&mips_operand_tokens, tokens);
364215c8
RS
12720 return;
12721 }
252b5132 12722
364215c8
RS
12723 /* Fail the match if the line has too many operands. */
12724 if (*args == 0)
12725 break;
252b5132 12726
364215c8
RS
12727 /* Handle characters that need to match exactly. */
12728 if (*args == '(' || *args == ')' || *args == ',')
12729 {
a92713e6
RS
12730 if (match_char (&arg, *args))
12731 continue;
12732 break;
364215c8 12733 }
252b5132 12734
364215c8 12735 arg.opnum += 1;
a92713e6 12736 optional_reg = FALSE;
364215c8
RS
12737 c = *args;
12738 switch (c)
12739 {
12740 case 'v':
12741 case 'w':
a92713e6 12742 optional_reg = (args[1] == ',');
252b5132
RH
12743 break;
12744
252b5132
RH
12745 case 'p':
12746 case 'q':
12747 case 'A':
12748 case 'B':
12749 case 'E':
77bd4346 12750 relax_char = c;
364215c8 12751 break;
252b5132 12752
cc537e56 12753 case 'I':
a92713e6
RS
12754 if (match_const_int (&arg, &imm_expr.X_add_number, 0))
12755 imm_expr.X_op = O_constant;
12756 else
cc537e56
RS
12757 insn_error = _("absolute expression required");
12758 if (HAVE_32BIT_GPRS)
12759 normalize_constant_expr (&imm_expr);
cc537e56
RS
12760 continue;
12761
364215c8 12762 case 'a':
27c5c572 12763 case 'i':
f6688943 12764 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132 12765 ip->insn_opcode <<= 16;
364215c8
RS
12766 break;
12767 }
0499d65b 12768
364215c8
RS
12769 operand = decode_mips16_operand (c, FALSE);
12770 if (!operand)
12771 abort ();
0499d65b 12772
364215c8
RS
12773 /* '6' is a special case. It is used for BREAK and SDBBP,
12774 whose operands are only meaningful to the software that decodes
12775 them. This means that there is no architectural reason why
12776 they cannot be prefixed by EXTEND, but in practice,
12777 exception handlers will only look at the instruction
12778 itself. We therefore allow '6' to be extended when
12779 disassembling but not when assembling. */
12780 if (operand->type != OP_PCREL && c != '6')
12781 {
12782 ext_operand = decode_mips16_operand (c, TRUE);
12783 if (operand != ext_operand)
252b5132 12784 {
a92713e6 12785 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
364215c8 12786 {
a92713e6
RS
12787 offset_expr.X_op = O_constant;
12788 offset_expr.X_add_number = 0;
12789 relax_char = c;
12790 continue;
364215c8 12791 }
a92713e6
RS
12792
12793 /* We need the OT_INTEGER check because some MIPS16
12794 immediate variants are listed before the register ones. */
12795 if (arg.token->type != OT_INTEGER
12796 || !match_expression (&arg, &offset_expr, offset_reloc))
12797 break;
12798
364215c8
RS
12799 /* '8' is used for SLTI(U) and has traditionally not
12800 been allowed to take relocation operators. */
a92713e6
RS
12801 if (offset_reloc[0] != BFD_RELOC_UNUSED
12802 && (ext_operand->size != 16 || c == '8'))
364215c8 12803 break;
a92713e6 12804
364215c8
RS
12805 relax_char = c;
12806 continue;
252b5132 12807 }
364215c8 12808 }
252b5132 12809
a92713e6
RS
12810 if (optional_reg
12811 && (arg.token[0].type != OT_REG
12812 || arg.token[1].type == OT_END))
364215c8
RS
12813 {
12814 /* Assume that the register has been elided and is the
12815 same as the first operand. */
a92713e6 12816 arg.token = tokens;
364215c8 12817 arg.argnum = 1;
252b5132 12818 }
a92713e6
RS
12819
12820 if (!match_operand (&arg, operand))
364215c8
RS
12821 break;
12822 continue;
252b5132
RH
12823 }
12824
12825 /* Args don't match. */
364215c8 12826 if (more_alts)
252b5132
RH
12827 {
12828 ++insn;
252b5132
RH
12829 continue;
12830 }
12831
12832 insn_error = _("illegal operands");
12833
a92713e6 12834 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
12835 return;
12836 }
12837}
12838
b886a2ab
RS
12839/* Marshal immediate value VAL for an extended MIPS16 instruction.
12840 NBITS is the number of significant bits in VAL. */
12841
12842static unsigned long
12843mips16_immed_extend (offsetT val, unsigned int nbits)
12844{
12845 int extval;
12846 if (nbits == 16)
12847 {
12848 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
12849 val &= 0x1f;
12850 }
12851 else if (nbits == 15)
12852 {
12853 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
12854 val &= 0xf;
12855 }
12856 else
12857 {
12858 extval = ((val & 0x1f) << 6) | (val & 0x20);
12859 val = 0;
12860 }
12861 return (extval << 16) | val;
12862}
12863
3ccad066
RS
12864/* Like decode_mips16_operand, but require the operand to be defined and
12865 require it to be an integer. */
12866
12867static const struct mips_int_operand *
12868mips16_immed_operand (int type, bfd_boolean extended_p)
12869{
12870 const struct mips_operand *operand;
12871
12872 operand = decode_mips16_operand (type, extended_p);
12873 if (!operand || (operand->type != OP_INT && operand->type != OP_PCREL))
12874 abort ();
12875 return (const struct mips_int_operand *) operand;
12876}
12877
12878/* Return true if SVAL fits OPERAND. RELOC is as for mips16_immed. */
12879
12880static bfd_boolean
12881mips16_immed_in_range_p (const struct mips_int_operand *operand,
12882 bfd_reloc_code_real_type reloc, offsetT sval)
12883{
12884 int min_val, max_val;
12885
12886 min_val = mips_int_operand_min (operand);
12887 max_val = mips_int_operand_max (operand);
12888 if (reloc != BFD_RELOC_UNUSED)
12889 {
12890 if (min_val < 0)
12891 sval = SEXT_16BIT (sval);
12892 else
12893 sval &= 0xffff;
12894 }
12895
12896 return (sval >= min_val
12897 && sval <= max_val
12898 && (sval & ((1 << operand->shift) - 1)) == 0);
12899}
12900
5c04167a
RS
12901/* Install immediate value VAL into MIPS16 instruction *INSN,
12902 extending it if necessary. The instruction in *INSN may
12903 already be extended.
12904
43c0598f
RS
12905 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
12906 if none. In the former case, VAL is a 16-bit number with no
12907 defined signedness.
12908
12909 TYPE is the type of the immediate field. USER_INSN_LENGTH
12910 is the length that the user requested, or 0 if none. */
252b5132
RH
12911
12912static void
43c0598f
RS
12913mips16_immed (char *file, unsigned int line, int type,
12914 bfd_reloc_code_real_type reloc, offsetT val,
5c04167a 12915 unsigned int user_insn_length, unsigned long *insn)
252b5132 12916{
3ccad066
RS
12917 const struct mips_int_operand *operand;
12918 unsigned int uval, length;
252b5132 12919
3ccad066
RS
12920 operand = mips16_immed_operand (type, FALSE);
12921 if (!mips16_immed_in_range_p (operand, reloc, val))
5c04167a
RS
12922 {
12923 /* We need an extended instruction. */
12924 if (user_insn_length == 2)
12925 as_bad_where (file, line, _("invalid unextended operand value"));
12926 else
12927 *insn |= MIPS16_EXTEND;
12928 }
12929 else if (user_insn_length == 4)
12930 {
12931 /* The operand doesn't force an unextended instruction to be extended.
12932 Warn if the user wanted an extended instruction anyway. */
12933 *insn |= MIPS16_EXTEND;
12934 as_warn_where (file, line,
12935 _("extended operand requested but not required"));
12936 }
252b5132 12937
3ccad066
RS
12938 length = mips16_opcode_length (*insn);
12939 if (length == 4)
252b5132 12940 {
3ccad066
RS
12941 operand = mips16_immed_operand (type, TRUE);
12942 if (!mips16_immed_in_range_p (operand, reloc, val))
12943 as_bad_where (file, line,
12944 _("operand value out of range for instruction"));
252b5132 12945 }
3ccad066
RS
12946 uval = ((unsigned int) val >> operand->shift) - operand->bias;
12947 if (length == 2)
12948 *insn = mips_insert_operand (&operand->root, *insn, uval);
252b5132 12949 else
3ccad066 12950 *insn |= mips16_immed_extend (uval, operand->root.size);
252b5132
RH
12951}
12952\f
d6f16593 12953struct percent_op_match
ad8d3bb3 12954{
5e0116d5
RS
12955 const char *str;
12956 bfd_reloc_code_real_type reloc;
d6f16593
MR
12957};
12958
12959static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 12960{
5e0116d5 12961 {"%lo", BFD_RELOC_LO16},
5e0116d5
RS
12962 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
12963 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
12964 {"%call16", BFD_RELOC_MIPS_CALL16},
12965 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
12966 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
12967 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
12968 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
12969 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
12970 {"%got", BFD_RELOC_MIPS_GOT16},
12971 {"%gp_rel", BFD_RELOC_GPREL16},
12972 {"%half", BFD_RELOC_16},
12973 {"%highest", BFD_RELOC_MIPS_HIGHEST},
12974 {"%higher", BFD_RELOC_MIPS_HIGHER},
12975 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
12976 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
12977 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
12978 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
12979 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
12980 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
12981 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
12982 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
5e0116d5 12983 {"%hi", BFD_RELOC_HI16_S}
ad8d3bb3
TS
12984};
12985
d6f16593
MR
12986static const struct percent_op_match mips16_percent_op[] =
12987{
12988 {"%lo", BFD_RELOC_MIPS16_LO16},
12989 {"%gprel", BFD_RELOC_MIPS16_GPREL},
738e5348
RS
12990 {"%got", BFD_RELOC_MIPS16_GOT16},
12991 {"%call16", BFD_RELOC_MIPS16_CALL16},
d0f13682
CLT
12992 {"%hi", BFD_RELOC_MIPS16_HI16_S},
12993 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
12994 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
12995 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
12996 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
12997 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
12998 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
12999 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
d6f16593
MR
13000};
13001
252b5132 13002
5e0116d5
RS
13003/* Return true if *STR points to a relocation operator. When returning true,
13004 move *STR over the operator and store its relocation code in *RELOC.
13005 Leave both *STR and *RELOC alone when returning false. */
13006
13007static bfd_boolean
17a2f251 13008parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 13009{
d6f16593
MR
13010 const struct percent_op_match *percent_op;
13011 size_t limit, i;
13012
13013 if (mips_opts.mips16)
13014 {
13015 percent_op = mips16_percent_op;
13016 limit = ARRAY_SIZE (mips16_percent_op);
13017 }
13018 else
13019 {
13020 percent_op = mips_percent_op;
13021 limit = ARRAY_SIZE (mips_percent_op);
13022 }
76b3015f 13023
d6f16593 13024 for (i = 0; i < limit; i++)
5e0116d5 13025 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 13026 {
3f98094e
DJ
13027 int len = strlen (percent_op[i].str);
13028
13029 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
13030 continue;
13031
5e0116d5
RS
13032 *str += strlen (percent_op[i].str);
13033 *reloc = percent_op[i].reloc;
394f9b3a 13034
5e0116d5
RS
13035 /* Check whether the output BFD supports this relocation.
13036 If not, issue an error and fall back on something safe. */
13037 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 13038 {
20203fb9 13039 as_bad (_("relocation %s isn't supported by the current ABI"),
5e0116d5 13040 percent_op[i].str);
01a3f561 13041 *reloc = BFD_RELOC_UNUSED;
394f9b3a 13042 }
5e0116d5 13043 return TRUE;
394f9b3a 13044 }
5e0116d5 13045 return FALSE;
394f9b3a 13046}
ad8d3bb3 13047
ad8d3bb3 13048
5e0116d5
RS
13049/* Parse string STR as a 16-bit relocatable operand. Store the
13050 expression in *EP and the relocations in the array starting
13051 at RELOC. Return the number of relocation operators used.
ad8d3bb3 13052
01a3f561 13053 On exit, EXPR_END points to the first character after the expression. */
ad8d3bb3 13054
5e0116d5 13055static size_t
17a2f251
TS
13056my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
13057 char *str)
ad8d3bb3 13058{
5e0116d5
RS
13059 bfd_reloc_code_real_type reversed_reloc[3];
13060 size_t reloc_index, i;
09b8f35a
RS
13061 int crux_depth, str_depth;
13062 char *crux;
5e0116d5
RS
13063
13064 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
13065 in REVERSED_RELOC. End the loop with CRUX pointing to the start
13066 of the main expression and with CRUX_DEPTH containing the number
13067 of open brackets at that point. */
13068 reloc_index = -1;
13069 str_depth = 0;
13070 do
fb1b3232 13071 {
09b8f35a
RS
13072 reloc_index++;
13073 crux = str;
13074 crux_depth = str_depth;
13075
13076 /* Skip over whitespace and brackets, keeping count of the number
13077 of brackets. */
13078 while (*str == ' ' || *str == '\t' || *str == '(')
13079 if (*str++ == '(')
13080 str_depth++;
5e0116d5 13081 }
09b8f35a
RS
13082 while (*str == '%'
13083 && reloc_index < (HAVE_NEWABI ? 3 : 1)
13084 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 13085
09b8f35a 13086 my_getExpression (ep, crux);
5e0116d5 13087 str = expr_end;
394f9b3a 13088
5e0116d5 13089 /* Match every open bracket. */
09b8f35a 13090 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 13091 if (*str++ == ')')
09b8f35a 13092 crux_depth--;
394f9b3a 13093
09b8f35a 13094 if (crux_depth > 0)
20203fb9 13095 as_bad (_("unclosed '('"));
394f9b3a 13096
5e0116d5 13097 expr_end = str;
252b5132 13098
01a3f561 13099 if (reloc_index != 0)
64bdfcaf
RS
13100 {
13101 prev_reloc_op_frag = frag_now;
13102 for (i = 0; i < reloc_index; i++)
13103 reloc[i] = reversed_reloc[reloc_index - 1 - i];
13104 }
fb1b3232 13105
5e0116d5 13106 return reloc_index;
252b5132
RH
13107}
13108
13109static void
17a2f251 13110my_getExpression (expressionS *ep, char *str)
252b5132
RH
13111{
13112 char *save_in;
13113
13114 save_in = input_line_pointer;
13115 input_line_pointer = str;
13116 expression (ep);
13117 expr_end = input_line_pointer;
13118 input_line_pointer = save_in;
252b5132
RH
13119}
13120
252b5132 13121char *
17a2f251 13122md_atof (int type, char *litP, int *sizeP)
252b5132 13123{
499ac353 13124 return ieee_md_atof (type, litP, sizeP, target_big_endian);
252b5132
RH
13125}
13126
13127void
17a2f251 13128md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
13129{
13130 if (target_big_endian)
13131 number_to_chars_bigendian (buf, val, n);
13132 else
13133 number_to_chars_littleendian (buf, val, n);
13134}
13135\f
e013f690
TS
13136static int support_64bit_objects(void)
13137{
13138 const char **list, **l;
aa3d8fdf 13139 int yes;
e013f690
TS
13140
13141 list = bfd_target_list ();
13142 for (l = list; *l != NULL; l++)
aeffff67
RS
13143 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
13144 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
e013f690 13145 break;
aa3d8fdf 13146 yes = (*l != NULL);
e013f690 13147 free (list);
aa3d8fdf 13148 return yes;
e013f690
TS
13149}
13150
316f5878
RS
13151/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
13152 NEW_VALUE. Warn if another value was already specified. Note:
13153 we have to defer parsing the -march and -mtune arguments in order
13154 to handle 'from-abi' correctly, since the ABI might be specified
13155 in a later argument. */
13156
13157static void
17a2f251 13158mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
13159{
13160 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
13161 as_warn (_("A different %s was already specified, is now %s"),
13162 string_ptr == &mips_arch_string ? "-march" : "-mtune",
13163 new_value);
13164
13165 *string_ptr = new_value;
13166}
13167
252b5132 13168int
17a2f251 13169md_parse_option (int c, char *arg)
252b5132 13170{
c6278170
RS
13171 unsigned int i;
13172
13173 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
13174 if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
13175 {
13176 file_ase_explicit |= mips_set_ase (&mips_ases[i],
13177 c == mips_ases[i].option_on);
13178 return 1;
13179 }
13180
252b5132
RH
13181 switch (c)
13182 {
119d663a
NC
13183 case OPTION_CONSTRUCT_FLOATS:
13184 mips_disable_float_construction = 0;
13185 break;
bdaaa2e1 13186
119d663a
NC
13187 case OPTION_NO_CONSTRUCT_FLOATS:
13188 mips_disable_float_construction = 1;
13189 break;
bdaaa2e1 13190
252b5132
RH
13191 case OPTION_TRAP:
13192 mips_trap = 1;
13193 break;
13194
13195 case OPTION_BREAK:
13196 mips_trap = 0;
13197 break;
13198
13199 case OPTION_EB:
13200 target_big_endian = 1;
13201 break;
13202
13203 case OPTION_EL:
13204 target_big_endian = 0;
13205 break;
13206
13207 case 'O':
4ffff32f
TS
13208 if (arg == NULL)
13209 mips_optimize = 1;
13210 else if (arg[0] == '0')
13211 mips_optimize = 0;
13212 else if (arg[0] == '1')
252b5132
RH
13213 mips_optimize = 1;
13214 else
13215 mips_optimize = 2;
13216 break;
13217
13218 case 'g':
13219 if (arg == NULL)
13220 mips_debug = 2;
13221 else
13222 mips_debug = atoi (arg);
252b5132
RH
13223 break;
13224
13225 case OPTION_MIPS1:
316f5878 13226 file_mips_isa = ISA_MIPS1;
252b5132
RH
13227 break;
13228
13229 case OPTION_MIPS2:
316f5878 13230 file_mips_isa = ISA_MIPS2;
252b5132
RH
13231 break;
13232
13233 case OPTION_MIPS3:
316f5878 13234 file_mips_isa = ISA_MIPS3;
252b5132
RH
13235 break;
13236
13237 case OPTION_MIPS4:
316f5878 13238 file_mips_isa = ISA_MIPS4;
e7af610e
NC
13239 break;
13240
84ea6cf2 13241 case OPTION_MIPS5:
316f5878 13242 file_mips_isa = ISA_MIPS5;
84ea6cf2
NC
13243 break;
13244
e7af610e 13245 case OPTION_MIPS32:
316f5878 13246 file_mips_isa = ISA_MIPS32;
252b5132
RH
13247 break;
13248
af7ee8bf
CD
13249 case OPTION_MIPS32R2:
13250 file_mips_isa = ISA_MIPS32R2;
13251 break;
13252
5f74bc13
CD
13253 case OPTION_MIPS64R2:
13254 file_mips_isa = ISA_MIPS64R2;
13255 break;
13256
84ea6cf2 13257 case OPTION_MIPS64:
316f5878 13258 file_mips_isa = ISA_MIPS64;
84ea6cf2
NC
13259 break;
13260
ec68c924 13261 case OPTION_MTUNE:
316f5878
RS
13262 mips_set_option_string (&mips_tune_string, arg);
13263 break;
ec68c924 13264
316f5878
RS
13265 case OPTION_MARCH:
13266 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
13267 break;
13268
13269 case OPTION_M4650:
316f5878
RS
13270 mips_set_option_string (&mips_arch_string, "4650");
13271 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
13272 break;
13273
13274 case OPTION_NO_M4650:
13275 break;
13276
13277 case OPTION_M4010:
316f5878
RS
13278 mips_set_option_string (&mips_arch_string, "4010");
13279 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
13280 break;
13281
13282 case OPTION_NO_M4010:
13283 break;
13284
13285 case OPTION_M4100:
316f5878
RS
13286 mips_set_option_string (&mips_arch_string, "4100");
13287 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
13288 break;
13289
13290 case OPTION_NO_M4100:
13291 break;
13292
252b5132 13293 case OPTION_M3900:
316f5878
RS
13294 mips_set_option_string (&mips_arch_string, "3900");
13295 mips_set_option_string (&mips_tune_string, "3900");
252b5132 13296 break;
bdaaa2e1 13297
252b5132
RH
13298 case OPTION_NO_M3900:
13299 break;
13300
df58fc94
RS
13301 case OPTION_MICROMIPS:
13302 if (mips_opts.mips16 == 1)
13303 {
13304 as_bad (_("-mmicromips cannot be used with -mips16"));
13305 return 0;
13306 }
13307 mips_opts.micromips = 1;
13308 mips_no_prev_insn ();
13309 break;
13310
13311 case OPTION_NO_MICROMIPS:
13312 mips_opts.micromips = 0;
13313 mips_no_prev_insn ();
13314 break;
13315
252b5132 13316 case OPTION_MIPS16:
df58fc94
RS
13317 if (mips_opts.micromips == 1)
13318 {
13319 as_bad (_("-mips16 cannot be used with -micromips"));
13320 return 0;
13321 }
252b5132 13322 mips_opts.mips16 = 1;
7d10b47d 13323 mips_no_prev_insn ();
252b5132
RH
13324 break;
13325
13326 case OPTION_NO_MIPS16:
13327 mips_opts.mips16 = 0;
7d10b47d 13328 mips_no_prev_insn ();
252b5132
RH
13329 break;
13330
6a32d874
CM
13331 case OPTION_FIX_24K:
13332 mips_fix_24k = 1;
13333 break;
13334
13335 case OPTION_NO_FIX_24K:
13336 mips_fix_24k = 0;
13337 break;
13338
c67a084a
NC
13339 case OPTION_FIX_LOONGSON2F_JUMP:
13340 mips_fix_loongson2f_jump = TRUE;
13341 break;
13342
13343 case OPTION_NO_FIX_LOONGSON2F_JUMP:
13344 mips_fix_loongson2f_jump = FALSE;
13345 break;
13346
13347 case OPTION_FIX_LOONGSON2F_NOP:
13348 mips_fix_loongson2f_nop = TRUE;
13349 break;
13350
13351 case OPTION_NO_FIX_LOONGSON2F_NOP:
13352 mips_fix_loongson2f_nop = FALSE;
13353 break;
13354
d766e8ec
RS
13355 case OPTION_FIX_VR4120:
13356 mips_fix_vr4120 = 1;
60b63b72
RS
13357 break;
13358
d766e8ec
RS
13359 case OPTION_NO_FIX_VR4120:
13360 mips_fix_vr4120 = 0;
60b63b72
RS
13361 break;
13362
7d8e00cf
RS
13363 case OPTION_FIX_VR4130:
13364 mips_fix_vr4130 = 1;
13365 break;
13366
13367 case OPTION_NO_FIX_VR4130:
13368 mips_fix_vr4130 = 0;
13369 break;
13370
d954098f
DD
13371 case OPTION_FIX_CN63XXP1:
13372 mips_fix_cn63xxp1 = TRUE;
13373 break;
13374
13375 case OPTION_NO_FIX_CN63XXP1:
13376 mips_fix_cn63xxp1 = FALSE;
13377 break;
13378
4a6a3df4
AO
13379 case OPTION_RELAX_BRANCH:
13380 mips_relax_branch = 1;
13381 break;
13382
13383 case OPTION_NO_RELAX_BRANCH:
13384 mips_relax_branch = 0;
13385 break;
13386
833794fc
MR
13387 case OPTION_INSN32:
13388 mips_opts.insn32 = TRUE;
13389 break;
13390
13391 case OPTION_NO_INSN32:
13392 mips_opts.insn32 = FALSE;
13393 break;
13394
aa6975fb
ILT
13395 case OPTION_MSHARED:
13396 mips_in_shared = TRUE;
13397 break;
13398
13399 case OPTION_MNO_SHARED:
13400 mips_in_shared = FALSE;
13401 break;
13402
aed1a261
RS
13403 case OPTION_MSYM32:
13404 mips_opts.sym32 = TRUE;
13405 break;
13406
13407 case OPTION_MNO_SYM32:
13408 mips_opts.sym32 = FALSE;
13409 break;
13410
252b5132
RH
13411 /* When generating ELF code, we permit -KPIC and -call_shared to
13412 select SVR4_PIC, and -non_shared to select no PIC. This is
13413 intended to be compatible with Irix 5. */
13414 case OPTION_CALL_SHARED:
252b5132 13415 mips_pic = SVR4_PIC;
143d77c5 13416 mips_abicalls = TRUE;
252b5132
RH
13417 break;
13418
861fb55a 13419 case OPTION_CALL_NONPIC:
861fb55a
DJ
13420 mips_pic = NO_PIC;
13421 mips_abicalls = TRUE;
13422 break;
13423
252b5132 13424 case OPTION_NON_SHARED:
252b5132 13425 mips_pic = NO_PIC;
143d77c5 13426 mips_abicalls = FALSE;
252b5132
RH
13427 break;
13428
44075ae2
TS
13429 /* The -xgot option tells the assembler to use 32 bit offsets
13430 when accessing the got in SVR4_PIC mode. It is for Irix
252b5132
RH
13431 compatibility. */
13432 case OPTION_XGOT:
13433 mips_big_got = 1;
13434 break;
13435
13436 case 'G':
6caf9ef4
TS
13437 g_switch_value = atoi (arg);
13438 g_switch_seen = 1;
252b5132
RH
13439 break;
13440
34ba82a8
TS
13441 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
13442 and -mabi=64. */
252b5132 13443 case OPTION_32:
f3ded42a 13444 mips_abi = O32_ABI;
252b5132
RH
13445 break;
13446
e013f690 13447 case OPTION_N32:
316f5878 13448 mips_abi = N32_ABI;
e013f690 13449 break;
252b5132 13450
e013f690 13451 case OPTION_64:
316f5878 13452 mips_abi = N64_ABI;
f43abd2b 13453 if (!support_64bit_objects())
e013f690 13454 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132
RH
13455 break;
13456
c97ef257 13457 case OPTION_GP32:
a325df1d 13458 file_mips_gp32 = 1;
c97ef257
AH
13459 break;
13460
13461 case OPTION_GP64:
a325df1d 13462 file_mips_gp32 = 0;
c97ef257 13463 break;
252b5132 13464
ca4e0257 13465 case OPTION_FP32:
a325df1d 13466 file_mips_fp32 = 1;
316f5878
RS
13467 break;
13468
13469 case OPTION_FP64:
13470 file_mips_fp32 = 0;
ca4e0257
RS
13471 break;
13472
037b32b9
AN
13473 case OPTION_SINGLE_FLOAT:
13474 file_mips_single_float = 1;
13475 break;
13476
13477 case OPTION_DOUBLE_FLOAT:
13478 file_mips_single_float = 0;
13479 break;
13480
13481 case OPTION_SOFT_FLOAT:
13482 file_mips_soft_float = 1;
13483 break;
13484
13485 case OPTION_HARD_FLOAT:
13486 file_mips_soft_float = 0;
13487 break;
13488
252b5132 13489 case OPTION_MABI:
e013f690 13490 if (strcmp (arg, "32") == 0)
316f5878 13491 mips_abi = O32_ABI;
e013f690 13492 else if (strcmp (arg, "o64") == 0)
316f5878 13493 mips_abi = O64_ABI;
e013f690 13494 else if (strcmp (arg, "n32") == 0)
316f5878 13495 mips_abi = N32_ABI;
e013f690
TS
13496 else if (strcmp (arg, "64") == 0)
13497 {
316f5878 13498 mips_abi = N64_ABI;
e013f690
TS
13499 if (! support_64bit_objects())
13500 as_fatal (_("No compiled in support for 64 bit object file "
13501 "format"));
13502 }
13503 else if (strcmp (arg, "eabi") == 0)
316f5878 13504 mips_abi = EABI_ABI;
e013f690 13505 else
da0e507f
TS
13506 {
13507 as_fatal (_("invalid abi -mabi=%s"), arg);
13508 return 0;
13509 }
252b5132
RH
13510 break;
13511
6b76fefe 13512 case OPTION_M7000_HILO_FIX:
b34976b6 13513 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
13514 break;
13515
9ee72ff1 13516 case OPTION_MNO_7000_HILO_FIX:
b34976b6 13517 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
13518 break;
13519
ecb4347a 13520 case OPTION_MDEBUG:
b34976b6 13521 mips_flag_mdebug = TRUE;
ecb4347a
DJ
13522 break;
13523
13524 case OPTION_NO_MDEBUG:
b34976b6 13525 mips_flag_mdebug = FALSE;
ecb4347a 13526 break;
dcd410fe
RO
13527
13528 case OPTION_PDR:
13529 mips_flag_pdr = TRUE;
13530 break;
13531
13532 case OPTION_NO_PDR:
13533 mips_flag_pdr = FALSE;
13534 break;
0a44bf69
RS
13535
13536 case OPTION_MVXWORKS_PIC:
13537 mips_pic = VXWORKS_PIC;
13538 break;
ecb4347a 13539
ba92f887
MR
13540 case OPTION_NAN:
13541 if (strcmp (arg, "2008") == 0)
13542 mips_flag_nan2008 = TRUE;
13543 else if (strcmp (arg, "legacy") == 0)
13544 mips_flag_nan2008 = FALSE;
13545 else
13546 {
13547 as_fatal (_("Invalid NaN setting -mnan=%s"), arg);
13548 return 0;
13549 }
13550 break;
13551
252b5132
RH
13552 default:
13553 return 0;
13554 }
13555
c67a084a
NC
13556 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
13557
252b5132
RH
13558 return 1;
13559}
316f5878
RS
13560\f
13561/* Set up globals to generate code for the ISA or processor
13562 described by INFO. */
252b5132 13563
252b5132 13564static void
17a2f251 13565mips_set_architecture (const struct mips_cpu_info *info)
252b5132 13566{
316f5878 13567 if (info != 0)
252b5132 13568 {
fef14a42
TS
13569 file_mips_arch = info->cpu;
13570 mips_opts.arch = info->cpu;
316f5878 13571 mips_opts.isa = info->isa;
252b5132 13572 }
252b5132
RH
13573}
13574
252b5132 13575
316f5878 13576/* Likewise for tuning. */
252b5132 13577
316f5878 13578static void
17a2f251 13579mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
13580{
13581 if (info != 0)
fef14a42 13582 mips_tune = info->cpu;
316f5878 13583}
80cc45a5 13584
34ba82a8 13585
252b5132 13586void
17a2f251 13587mips_after_parse_args (void)
e9670677 13588{
fef14a42
TS
13589 const struct mips_cpu_info *arch_info = 0;
13590 const struct mips_cpu_info *tune_info = 0;
13591
e9670677 13592 /* GP relative stuff not working for PE */
6caf9ef4 13593 if (strncmp (TARGET_OS, "pe", 2) == 0)
e9670677 13594 {
6caf9ef4 13595 if (g_switch_seen && g_switch_value != 0)
e9670677
MR
13596 as_bad (_("-G not supported in this configuration."));
13597 g_switch_value = 0;
13598 }
13599
cac012d6
AO
13600 if (mips_abi == NO_ABI)
13601 mips_abi = MIPS_DEFAULT_ABI;
13602
22923709
RS
13603 /* The following code determines the architecture and register size.
13604 Similar code was added to GCC 3.3 (see override_options() in
13605 config/mips/mips.c). The GAS and GCC code should be kept in sync
13606 as much as possible. */
e9670677 13607
316f5878 13608 if (mips_arch_string != 0)
fef14a42 13609 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 13610
316f5878 13611 if (file_mips_isa != ISA_UNKNOWN)
e9670677 13612 {
316f5878 13613 /* Handle -mipsN. At this point, file_mips_isa contains the
fef14a42 13614 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 13615 the -march selection (if any). */
fef14a42 13616 if (arch_info != 0)
e9670677 13617 {
316f5878
RS
13618 /* -march takes precedence over -mipsN, since it is more descriptive.
13619 There's no harm in specifying both as long as the ISA levels
13620 are the same. */
fef14a42 13621 if (file_mips_isa != arch_info->isa)
316f5878
RS
13622 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
13623 mips_cpu_info_from_isa (file_mips_isa)->name,
fef14a42 13624 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 13625 }
316f5878 13626 else
fef14a42 13627 arch_info = mips_cpu_info_from_isa (file_mips_isa);
e9670677
MR
13628 }
13629
fef14a42 13630 if (arch_info == 0)
95bfe26e
MF
13631 {
13632 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
13633 gas_assert (arch_info);
13634 }
e9670677 13635
fef14a42 13636 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
20203fb9 13637 as_bad (_("-march=%s is not compatible with the selected ABI"),
fef14a42
TS
13638 arch_info->name);
13639
13640 mips_set_architecture (arch_info);
13641
13642 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
13643 if (mips_tune_string != 0)
13644 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 13645
fef14a42
TS
13646 if (tune_info == 0)
13647 mips_set_tune (arch_info);
13648 else
13649 mips_set_tune (tune_info);
e9670677 13650
316f5878 13651 if (file_mips_gp32 >= 0)
e9670677 13652 {
316f5878
RS
13653 /* The user specified the size of the integer registers. Make sure
13654 it agrees with the ABI and ISA. */
13655 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
13656 as_bad (_("-mgp64 used with a 32-bit processor"));
13657 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
13658 as_bad (_("-mgp32 used with a 64-bit ABI"));
13659 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
13660 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
13661 }
13662 else
13663 {
316f5878
RS
13664 /* Infer the integer register size from the ABI and processor.
13665 Restrict ourselves to 32-bit registers if that's all the
13666 processor has, or if the ABI cannot handle 64-bit registers. */
13667 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
13668 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
e9670677
MR
13669 }
13670
ad3fea08
TS
13671 switch (file_mips_fp32)
13672 {
13673 default:
13674 case -1:
13675 /* No user specified float register size.
13676 ??? GAS treats single-float processors as though they had 64-bit
13677 float registers (although it complains when double-precision
13678 instructions are used). As things stand, saying they have 32-bit
13679 registers would lead to spurious "register must be even" messages.
13680 So here we assume float registers are never smaller than the
13681 integer ones. */
13682 if (file_mips_gp32 == 0)
13683 /* 64-bit integer registers implies 64-bit float registers. */
13684 file_mips_fp32 = 0;
c6278170 13685 else if ((mips_opts.ase & FP64_ASES)
ad3fea08
TS
13686 && ISA_HAS_64BIT_FPRS (mips_opts.isa))
13687 /* -mips3d and -mdmx imply 64-bit float registers, if possible. */
13688 file_mips_fp32 = 0;
13689 else
13690 /* 32-bit float registers. */
13691 file_mips_fp32 = 1;
13692 break;
13693
13694 /* The user specified the size of the float registers. Check if it
13695 agrees with the ABI and ISA. */
13696 case 0:
13697 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
13698 as_bad (_("-mfp64 used with a 32-bit fpu"));
13699 else if (ABI_NEEDS_32BIT_REGS (mips_abi)
13700 && !ISA_HAS_MXHC1 (mips_opts.isa))
13701 as_warn (_("-mfp64 used with a 32-bit ABI"));
13702 break;
13703 case 1:
13704 if (ABI_NEEDS_64BIT_REGS (mips_abi))
13705 as_warn (_("-mfp32 used with a 64-bit ABI"));
13706 break;
13707 }
e9670677 13708
316f5878 13709 /* End of GCC-shared inference code. */
e9670677 13710
17a2f251
TS
13711 /* This flag is set when we have a 64-bit capable CPU but use only
13712 32-bit wide registers. Note that EABI does not use it. */
13713 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
13714 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
13715 || mips_abi == O32_ABI))
316f5878 13716 mips_32bitmode = 1;
e9670677
MR
13717
13718 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
13719 as_bad (_("trap exception not supported at ISA 1"));
13720
e9670677
MR
13721 /* If the selected architecture includes support for ASEs, enable
13722 generation of code for them. */
a4672219 13723 if (mips_opts.mips16 == -1)
fef14a42 13724 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
df58fc94
RS
13725 if (mips_opts.micromips == -1)
13726 mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_arch)) ? 1 : 0;
846ef2d0
RS
13727
13728 /* MIPS3D and MDMX require 64-bit FPRs, so -mfp32 should stop those
13729 ASEs from being selected implicitly. */
13730 if (file_mips_fp32 == 1)
13731 file_ase_explicit |= ASE_MIPS3D | ASE_MDMX;
13732
13733 /* If the user didn't explicitly select or deselect a particular ASE,
13734 use the default setting for the CPU. */
13735 mips_opts.ase |= (arch_info->ase & ~file_ase_explicit);
13736
e9670677 13737 file_mips_isa = mips_opts.isa;
846ef2d0 13738 file_ase = mips_opts.ase;
e9670677
MR
13739 mips_opts.gp32 = file_mips_gp32;
13740 mips_opts.fp32 = file_mips_fp32;
037b32b9
AN
13741 mips_opts.soft_float = file_mips_soft_float;
13742 mips_opts.single_float = file_mips_single_float;
e9670677 13743
c6278170
RS
13744 mips_check_isa_supports_ases ();
13745
ecb4347a 13746 if (mips_flag_mdebug < 0)
e8044f35 13747 mips_flag_mdebug = 0;
e9670677
MR
13748}
13749\f
13750void
17a2f251 13751mips_init_after_args (void)
252b5132
RH
13752{
13753 /* initialize opcodes */
13754 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 13755 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
13756}
13757
13758long
17a2f251 13759md_pcrel_from (fixS *fixP)
252b5132 13760{
a7ebbfdf
TS
13761 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
13762 switch (fixP->fx_r_type)
13763 {
df58fc94
RS
13764 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
13765 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
13766 /* Return the address of the delay slot. */
13767 return addr + 2;
13768
13769 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
13770 case BFD_RELOC_MICROMIPS_JMP:
a7ebbfdf
TS
13771 case BFD_RELOC_16_PCREL_S2:
13772 case BFD_RELOC_MIPS_JMP:
13773 /* Return the address of the delay slot. */
13774 return addr + 4;
df58fc94 13775
b47468a6
CM
13776 case BFD_RELOC_32_PCREL:
13777 return addr;
13778
a7ebbfdf 13779 default:
58ea3d6a 13780 /* We have no relocation type for PC relative MIPS16 instructions. */
64817874
TS
13781 if (fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != now_seg)
13782 as_bad_where (fixP->fx_file, fixP->fx_line,
13783 _("PC relative MIPS16 instruction references a different section"));
a7ebbfdf
TS
13784 return addr;
13785 }
252b5132
RH
13786}
13787
252b5132
RH
13788/* This is called before the symbol table is processed. In order to
13789 work with gcc when using mips-tfile, we must keep all local labels.
13790 However, in other cases, we want to discard them. If we were
13791 called with -g, but we didn't see any debugging information, it may
13792 mean that gcc is smuggling debugging information through to
13793 mips-tfile, in which case we must generate all local labels. */
13794
13795void
17a2f251 13796mips_frob_file_before_adjust (void)
252b5132
RH
13797{
13798#ifndef NO_ECOFF_DEBUGGING
13799 if (ECOFF_DEBUGGING
13800 && mips_debug != 0
13801 && ! ecoff_debugging_seen)
13802 flag_keep_locals = 1;
13803#endif
13804}
13805
3b91255e 13806/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 13807 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
13808 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
13809 relocation operators.
13810
13811 For our purposes, a %lo() expression matches a %got() or %hi()
13812 expression if:
13813
13814 (a) it refers to the same symbol; and
13815 (b) the offset applied in the %lo() expression is no lower than
13816 the offset applied in the %got() or %hi().
13817
13818 (b) allows us to cope with code like:
13819
13820 lui $4,%hi(foo)
13821 lh $4,%lo(foo+2)($4)
13822
13823 ...which is legal on RELA targets, and has a well-defined behaviour
13824 if the user knows that adding 2 to "foo" will not induce a carry to
13825 the high 16 bits.
13826
13827 When several %lo()s match a particular %got() or %hi(), we use the
13828 following rules to distinguish them:
13829
13830 (1) %lo()s with smaller offsets are a better match than %lo()s with
13831 higher offsets.
13832
13833 (2) %lo()s with no matching %got() or %hi() are better than those
13834 that already have a matching %got() or %hi().
13835
13836 (3) later %lo()s are better than earlier %lo()s.
13837
13838 These rules are applied in order.
13839
13840 (1) means, among other things, that %lo()s with identical offsets are
13841 chosen if they exist.
13842
13843 (2) means that we won't associate several high-part relocations with
13844 the same low-part relocation unless there's no alternative. Having
13845 several high parts for the same low part is a GNU extension; this rule
13846 allows careful users to avoid it.
13847
13848 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
13849 with the last high-part relocation being at the front of the list.
13850 It therefore makes sense to choose the last matching low-part
13851 relocation, all other things being equal. It's also easier
13852 to code that way. */
252b5132
RH
13853
13854void
17a2f251 13855mips_frob_file (void)
252b5132
RH
13856{
13857 struct mips_hi_fixup *l;
35903be0 13858 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
252b5132
RH
13859
13860 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
13861 {
13862 segment_info_type *seginfo;
3b91255e
RS
13863 bfd_boolean matched_lo_p;
13864 fixS **hi_pos, **lo_pos, **pos;
252b5132 13865
9c2799c2 13866 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 13867
5919d012 13868 /* If a GOT16 relocation turns out to be against a global symbol,
b886a2ab
RS
13869 there isn't supposed to be a matching LO. Ignore %gots against
13870 constants; we'll report an error for those later. */
738e5348 13871 if (got16_reloc_p (l->fixp->fx_r_type)
b886a2ab
RS
13872 && !(l->fixp->fx_addsy
13873 && pic_need_relax (l->fixp->fx_addsy, l->seg)))
5919d012
RS
13874 continue;
13875
13876 /* Check quickly whether the next fixup happens to be a matching %lo. */
13877 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
13878 continue;
13879
252b5132 13880 seginfo = seg_info (l->seg);
252b5132 13881
3b91255e
RS
13882 /* Set HI_POS to the position of this relocation in the chain.
13883 Set LO_POS to the position of the chosen low-part relocation.
13884 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
13885 relocation that matches an immediately-preceding high-part
13886 relocation. */
13887 hi_pos = NULL;
13888 lo_pos = NULL;
13889 matched_lo_p = FALSE;
738e5348 13890 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
35903be0 13891
3b91255e
RS
13892 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
13893 {
13894 if (*pos == l->fixp)
13895 hi_pos = pos;
13896
35903be0 13897 if ((*pos)->fx_r_type == looking_for_rtype
30cfc97a 13898 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
3b91255e
RS
13899 && (*pos)->fx_offset >= l->fixp->fx_offset
13900 && (lo_pos == NULL
13901 || (*pos)->fx_offset < (*lo_pos)->fx_offset
13902 || (!matched_lo_p
13903 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
13904 lo_pos = pos;
13905
13906 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
13907 && fixup_has_matching_lo_p (*pos));
13908 }
13909
13910 /* If we found a match, remove the high-part relocation from its
13911 current position and insert it before the low-part relocation.
13912 Make the offsets match so that fixup_has_matching_lo_p()
13913 will return true.
13914
13915 We don't warn about unmatched high-part relocations since some
13916 versions of gcc have been known to emit dead "lui ...%hi(...)"
13917 instructions. */
13918 if (lo_pos != NULL)
13919 {
13920 l->fixp->fx_offset = (*lo_pos)->fx_offset;
13921 if (l->fixp->fx_next != *lo_pos)
252b5132 13922 {
3b91255e
RS
13923 *hi_pos = l->fixp->fx_next;
13924 l->fixp->fx_next = *lo_pos;
13925 *lo_pos = l->fixp;
252b5132 13926 }
252b5132
RH
13927 }
13928 }
13929}
13930
252b5132 13931int
17a2f251 13932mips_force_relocation (fixS *fixp)
252b5132 13933{
ae6063d4 13934 if (generic_force_reloc (fixp))
252b5132
RH
13935 return 1;
13936
df58fc94
RS
13937 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
13938 so that the linker relaxation can update targets. */
13939 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
13940 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
13941 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
13942 return 1;
13943
3e722fb5 13944 return 0;
252b5132
RH
13945}
13946
b886a2ab
RS
13947/* Read the instruction associated with RELOC from BUF. */
13948
13949static unsigned int
13950read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
13951{
13952 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
13953 return read_compressed_insn (buf, 4);
13954 else
13955 return read_insn (buf);
13956}
13957
13958/* Write instruction INSN to BUF, given that it has been relocated
13959 by RELOC. */
13960
13961static void
13962write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
13963 unsigned long insn)
13964{
13965 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
13966 write_compressed_insn (buf, insn, 4);
13967 else
13968 write_insn (buf, insn);
13969}
13970
252b5132
RH
13971/* Apply a fixup to the object file. */
13972
94f592af 13973void
55cf6793 13974md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 13975{
4d68580a 13976 char *buf;
b886a2ab 13977 unsigned long insn;
a7ebbfdf 13978 reloc_howto_type *howto;
252b5132 13979
a7ebbfdf
TS
13980 /* We ignore generic BFD relocations we don't know about. */
13981 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
13982 if (! howto)
13983 return;
65551fa4 13984
df58fc94
RS
13985 gas_assert (fixP->fx_size == 2
13986 || fixP->fx_size == 4
90ecf173
MR
13987 || fixP->fx_r_type == BFD_RELOC_16
13988 || fixP->fx_r_type == BFD_RELOC_64
13989 || fixP->fx_r_type == BFD_RELOC_CTOR
13990 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
df58fc94 13991 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
90ecf173
MR
13992 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13993 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
13994 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64);
252b5132 13995
4d68580a 13996 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132 13997
df58fc94
RS
13998 gas_assert (!fixP->fx_pcrel || fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
13999 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
14000 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
b47468a6
CM
14001 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
14002 || fixP->fx_r_type == BFD_RELOC_32_PCREL);
b1dca8ee
RS
14003
14004 /* Don't treat parts of a composite relocation as done. There are two
14005 reasons for this:
14006
14007 (1) The second and third parts will be against 0 (RSS_UNDEF) but
14008 should nevertheless be emitted if the first part is.
14009
14010 (2) In normal usage, composite relocations are never assembly-time
14011 constants. The easiest way of dealing with the pathological
14012 exceptions is to generate a relocation against STN_UNDEF and
14013 leave everything up to the linker. */
3994f87e 14014 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
252b5132
RH
14015 fixP->fx_done = 1;
14016
14017 switch (fixP->fx_r_type)
14018 {
3f98094e
DJ
14019 case BFD_RELOC_MIPS_TLS_GD:
14020 case BFD_RELOC_MIPS_TLS_LDM:
741d6ea8
JM
14021 case BFD_RELOC_MIPS_TLS_DTPREL32:
14022 case BFD_RELOC_MIPS_TLS_DTPREL64:
3f98094e
DJ
14023 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
14024 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
14025 case BFD_RELOC_MIPS_TLS_GOTTPREL:
d0f13682
CLT
14026 case BFD_RELOC_MIPS_TLS_TPREL32:
14027 case BFD_RELOC_MIPS_TLS_TPREL64:
3f98094e
DJ
14028 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
14029 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
df58fc94
RS
14030 case BFD_RELOC_MICROMIPS_TLS_GD:
14031 case BFD_RELOC_MICROMIPS_TLS_LDM:
14032 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
14033 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
14034 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
14035 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
14036 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
d0f13682
CLT
14037 case BFD_RELOC_MIPS16_TLS_GD:
14038 case BFD_RELOC_MIPS16_TLS_LDM:
14039 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
14040 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
14041 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
14042 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
14043 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
b886a2ab
RS
14044 if (!fixP->fx_addsy)
14045 {
14046 as_bad_where (fixP->fx_file, fixP->fx_line,
14047 _("TLS relocation against a constant"));
14048 break;
14049 }
3f98094e
DJ
14050 S_SET_THREAD_LOCAL (fixP->fx_addsy);
14051 /* fall through */
14052
252b5132 14053 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
14054 case BFD_RELOC_MIPS_SHIFT5:
14055 case BFD_RELOC_MIPS_SHIFT6:
14056 case BFD_RELOC_MIPS_GOT_DISP:
14057 case BFD_RELOC_MIPS_GOT_PAGE:
14058 case BFD_RELOC_MIPS_GOT_OFST:
14059 case BFD_RELOC_MIPS_SUB:
14060 case BFD_RELOC_MIPS_INSERT_A:
14061 case BFD_RELOC_MIPS_INSERT_B:
14062 case BFD_RELOC_MIPS_DELETE:
14063 case BFD_RELOC_MIPS_HIGHEST:
14064 case BFD_RELOC_MIPS_HIGHER:
14065 case BFD_RELOC_MIPS_SCN_DISP:
14066 case BFD_RELOC_MIPS_REL16:
14067 case BFD_RELOC_MIPS_RELGOT:
14068 case BFD_RELOC_MIPS_JALR:
252b5132
RH
14069 case BFD_RELOC_HI16:
14070 case BFD_RELOC_HI16_S:
b886a2ab 14071 case BFD_RELOC_LO16:
cdf6fd85 14072 case BFD_RELOC_GPREL16:
252b5132
RH
14073 case BFD_RELOC_MIPS_LITERAL:
14074 case BFD_RELOC_MIPS_CALL16:
14075 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 14076 case BFD_RELOC_GPREL32:
252b5132
RH
14077 case BFD_RELOC_MIPS_GOT_HI16:
14078 case BFD_RELOC_MIPS_GOT_LO16:
14079 case BFD_RELOC_MIPS_CALL_HI16:
14080 case BFD_RELOC_MIPS_CALL_LO16:
14081 case BFD_RELOC_MIPS16_GPREL:
738e5348
RS
14082 case BFD_RELOC_MIPS16_GOT16:
14083 case BFD_RELOC_MIPS16_CALL16:
d6f16593
MR
14084 case BFD_RELOC_MIPS16_HI16:
14085 case BFD_RELOC_MIPS16_HI16_S:
b886a2ab 14086 case BFD_RELOC_MIPS16_LO16:
252b5132 14087 case BFD_RELOC_MIPS16_JMP:
df58fc94
RS
14088 case BFD_RELOC_MICROMIPS_JMP:
14089 case BFD_RELOC_MICROMIPS_GOT_DISP:
14090 case BFD_RELOC_MICROMIPS_GOT_PAGE:
14091 case BFD_RELOC_MICROMIPS_GOT_OFST:
14092 case BFD_RELOC_MICROMIPS_SUB:
14093 case BFD_RELOC_MICROMIPS_HIGHEST:
14094 case BFD_RELOC_MICROMIPS_HIGHER:
14095 case BFD_RELOC_MICROMIPS_SCN_DISP:
14096 case BFD_RELOC_MICROMIPS_JALR:
14097 case BFD_RELOC_MICROMIPS_HI16:
14098 case BFD_RELOC_MICROMIPS_HI16_S:
b886a2ab 14099 case BFD_RELOC_MICROMIPS_LO16:
df58fc94
RS
14100 case BFD_RELOC_MICROMIPS_GPREL16:
14101 case BFD_RELOC_MICROMIPS_LITERAL:
14102 case BFD_RELOC_MICROMIPS_CALL16:
14103 case BFD_RELOC_MICROMIPS_GOT16:
14104 case BFD_RELOC_MICROMIPS_GOT_HI16:
14105 case BFD_RELOC_MICROMIPS_GOT_LO16:
14106 case BFD_RELOC_MICROMIPS_CALL_HI16:
14107 case BFD_RELOC_MICROMIPS_CALL_LO16:
067ec077 14108 case BFD_RELOC_MIPS_EH:
b886a2ab
RS
14109 if (fixP->fx_done)
14110 {
14111 offsetT value;
14112
14113 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
14114 {
14115 insn = read_reloc_insn (buf, fixP->fx_r_type);
14116 if (mips16_reloc_p (fixP->fx_r_type))
14117 insn |= mips16_immed_extend (value, 16);
14118 else
14119 insn |= (value & 0xffff);
14120 write_reloc_insn (buf, fixP->fx_r_type, insn);
14121 }
14122 else
14123 as_bad_where (fixP->fx_file, fixP->fx_line,
14124 _("Unsupported constant in relocation"));
14125 }
252b5132
RH
14126 break;
14127
252b5132
RH
14128 case BFD_RELOC_64:
14129 /* This is handled like BFD_RELOC_32, but we output a sign
14130 extended value if we are only 32 bits. */
3e722fb5 14131 if (fixP->fx_done)
252b5132
RH
14132 {
14133 if (8 <= sizeof (valueT))
4d68580a 14134 md_number_to_chars (buf, *valP, 8);
252b5132
RH
14135 else
14136 {
a7ebbfdf 14137 valueT hiv;
252b5132 14138
a7ebbfdf 14139 if ((*valP & 0x80000000) != 0)
252b5132
RH
14140 hiv = 0xffffffff;
14141 else
14142 hiv = 0;
4d68580a
RS
14143 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
14144 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
252b5132
RH
14145 }
14146 }
14147 break;
14148
056350c6 14149 case BFD_RELOC_RVA:
252b5132 14150 case BFD_RELOC_32:
b47468a6 14151 case BFD_RELOC_32_PCREL:
252b5132
RH
14152 case BFD_RELOC_16:
14153 /* If we are deleting this reloc entry, we must fill in the
54f4ddb3
TS
14154 value now. This can happen if we have a .word which is not
14155 resolved when it appears but is later defined. */
252b5132 14156 if (fixP->fx_done)
4d68580a 14157 md_number_to_chars (buf, *valP, fixP->fx_size);
252b5132
RH
14158 break;
14159
252b5132 14160 case BFD_RELOC_16_PCREL_S2:
a7ebbfdf 14161 if ((*valP & 0x3) != 0)
cb56d3d3 14162 as_bad_where (fixP->fx_file, fixP->fx_line,
bad36eac 14163 _("Branch to misaligned address (%lx)"), (long) *valP);
cb56d3d3 14164
54f4ddb3
TS
14165 /* We need to save the bits in the instruction since fixup_segment()
14166 might be deleting the relocation entry (i.e., a branch within
14167 the current segment). */
a7ebbfdf 14168 if (! fixP->fx_done)
bb2d6cd7 14169 break;
252b5132 14170
54f4ddb3 14171 /* Update old instruction data. */
4d68580a 14172 insn = read_insn (buf);
252b5132 14173
a7ebbfdf
TS
14174 if (*valP + 0x20000 <= 0x3ffff)
14175 {
14176 insn |= (*valP >> 2) & 0xffff;
4d68580a 14177 write_insn (buf, insn);
a7ebbfdf
TS
14178 }
14179 else if (mips_pic == NO_PIC
14180 && fixP->fx_done
14181 && fixP->fx_frag->fr_address >= text_section->vma
14182 && (fixP->fx_frag->fr_address
587aac4e 14183 < text_section->vma + bfd_get_section_size (text_section))
a7ebbfdf
TS
14184 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
14185 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
14186 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
14187 {
14188 /* The branch offset is too large. If this is an
14189 unconditional branch, and we are not generating PIC code,
14190 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
14191 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
14192 insn = 0x0c000000; /* jal */
252b5132 14193 else
a7ebbfdf
TS
14194 insn = 0x08000000; /* j */
14195 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
14196 fixP->fx_done = 0;
14197 fixP->fx_addsy = section_symbol (text_section);
14198 *valP += md_pcrel_from (fixP);
4d68580a 14199 write_insn (buf, insn);
a7ebbfdf
TS
14200 }
14201 else
14202 {
14203 /* If we got here, we have branch-relaxation disabled,
14204 and there's nothing we can do to fix this instruction
14205 without turning it into a longer sequence. */
14206 as_bad_where (fixP->fx_file, fixP->fx_line,
14207 _("Branch out of range"));
252b5132 14208 }
252b5132
RH
14209 break;
14210
df58fc94
RS
14211 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
14212 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
14213 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
14214 /* We adjust the offset back to even. */
14215 if ((*valP & 0x1) != 0)
14216 --(*valP);
14217
14218 if (! fixP->fx_done)
14219 break;
14220
14221 /* Should never visit here, because we keep the relocation. */
14222 abort ();
14223 break;
14224
252b5132
RH
14225 case BFD_RELOC_VTABLE_INHERIT:
14226 fixP->fx_done = 0;
14227 if (fixP->fx_addsy
14228 && !S_IS_DEFINED (fixP->fx_addsy)
14229 && !S_IS_WEAK (fixP->fx_addsy))
14230 S_SET_WEAK (fixP->fx_addsy);
14231 break;
14232
14233 case BFD_RELOC_VTABLE_ENTRY:
14234 fixP->fx_done = 0;
14235 break;
14236
14237 default:
b37df7c4 14238 abort ();
252b5132 14239 }
a7ebbfdf
TS
14240
14241 /* Remember value for tc_gen_reloc. */
14242 fixP->fx_addnumber = *valP;
252b5132
RH
14243}
14244
252b5132 14245static symbolS *
17a2f251 14246get_symbol (void)
252b5132
RH
14247{
14248 int c;
14249 char *name;
14250 symbolS *p;
14251
14252 name = input_line_pointer;
14253 c = get_symbol_end ();
14254 p = (symbolS *) symbol_find_or_make (name);
14255 *input_line_pointer = c;
14256 return p;
14257}
14258
742a56fe
RS
14259/* Align the current frag to a given power of two. If a particular
14260 fill byte should be used, FILL points to an integer that contains
14261 that byte, otherwise FILL is null.
14262
462427c4
RS
14263 This function used to have the comment:
14264
14265 The MIPS assembler also automatically adjusts any preceding label.
14266
14267 The implementation therefore applied the adjustment to a maximum of
14268 one label. However, other label adjustments are applied to batches
14269 of labels, and adjusting just one caused problems when new labels
14270 were added for the sake of debugging or unwind information.
14271 We therefore adjust all preceding labels (given as LABELS) instead. */
252b5132
RH
14272
14273static void
462427c4 14274mips_align (int to, int *fill, struct insn_label_list *labels)
252b5132 14275{
7d10b47d 14276 mips_emit_delays ();
df58fc94 14277 mips_record_compressed_mode ();
742a56fe
RS
14278 if (fill == NULL && subseg_text_p (now_seg))
14279 frag_align_code (to, 0);
14280 else
14281 frag_align (to, fill ? *fill : 0, 0);
252b5132 14282 record_alignment (now_seg, to);
462427c4 14283 mips_move_labels (labels, FALSE);
252b5132
RH
14284}
14285
14286/* Align to a given power of two. .align 0 turns off the automatic
14287 alignment used by the data creating pseudo-ops. */
14288
14289static void
17a2f251 14290s_align (int x ATTRIBUTE_UNUSED)
252b5132 14291{
742a56fe 14292 int temp, fill_value, *fill_ptr;
49954fb4 14293 long max_alignment = 28;
252b5132 14294
54f4ddb3 14295 /* o Note that the assembler pulls down any immediately preceding label
252b5132 14296 to the aligned address.
54f4ddb3 14297 o It's not documented but auto alignment is reinstated by
252b5132 14298 a .align pseudo instruction.
54f4ddb3 14299 o Note also that after auto alignment is turned off the mips assembler
252b5132 14300 issues an error on attempt to assemble an improperly aligned data item.
54f4ddb3 14301 We don't. */
252b5132
RH
14302
14303 temp = get_absolute_expression ();
14304 if (temp > max_alignment)
14305 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
14306 else if (temp < 0)
14307 {
14308 as_warn (_("Alignment negative: 0 assumed."));
14309 temp = 0;
14310 }
14311 if (*input_line_pointer == ',')
14312 {
f9419b05 14313 ++input_line_pointer;
742a56fe
RS
14314 fill_value = get_absolute_expression ();
14315 fill_ptr = &fill_value;
252b5132
RH
14316 }
14317 else
742a56fe 14318 fill_ptr = 0;
252b5132
RH
14319 if (temp)
14320 {
a8dbcb85
TS
14321 segment_info_type *si = seg_info (now_seg);
14322 struct insn_label_list *l = si->label_list;
54f4ddb3 14323 /* Auto alignment should be switched on by next section change. */
252b5132 14324 auto_align = 1;
462427c4 14325 mips_align (temp, fill_ptr, l);
252b5132
RH
14326 }
14327 else
14328 {
14329 auto_align = 0;
14330 }
14331
14332 demand_empty_rest_of_line ();
14333}
14334
252b5132 14335static void
17a2f251 14336s_change_sec (int sec)
252b5132
RH
14337{
14338 segT seg;
14339
252b5132
RH
14340 /* The ELF backend needs to know that we are changing sections, so
14341 that .previous works correctly. We could do something like check
b6ff326e 14342 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
14343 as it would not be appropriate to use it in the section changing
14344 functions in read.c, since obj-elf.c intercepts those. FIXME:
14345 This should be cleaner, somehow. */
f3ded42a 14346 obj_elf_section_change_hook ();
252b5132 14347
7d10b47d 14348 mips_emit_delays ();
6a32d874 14349
252b5132
RH
14350 switch (sec)
14351 {
14352 case 't':
14353 s_text (0);
14354 break;
14355 case 'd':
14356 s_data (0);
14357 break;
14358 case 'b':
14359 subseg_set (bss_section, (subsegT) get_absolute_expression ());
14360 demand_empty_rest_of_line ();
14361 break;
14362
14363 case 'r':
4d0d148d
TS
14364 seg = subseg_new (RDATA_SECTION_NAME,
14365 (subsegT) get_absolute_expression ());
f3ded42a
RS
14366 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
14367 | SEC_READONLY | SEC_RELOC
14368 | SEC_DATA));
14369 if (strncmp (TARGET_OS, "elf", 3) != 0)
14370 record_alignment (seg, 4);
4d0d148d 14371 demand_empty_rest_of_line ();
252b5132
RH
14372 break;
14373
14374 case 's':
4d0d148d 14375 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
f3ded42a
RS
14376 bfd_set_section_flags (stdoutput, seg,
14377 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
14378 if (strncmp (TARGET_OS, "elf", 3) != 0)
14379 record_alignment (seg, 4);
4d0d148d
TS
14380 demand_empty_rest_of_line ();
14381 break;
998b3c36
MR
14382
14383 case 'B':
14384 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
f3ded42a
RS
14385 bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
14386 if (strncmp (TARGET_OS, "elf", 3) != 0)
14387 record_alignment (seg, 4);
998b3c36
MR
14388 demand_empty_rest_of_line ();
14389 break;
252b5132
RH
14390 }
14391
14392 auto_align = 1;
14393}
b34976b6 14394
cca86cc8 14395void
17a2f251 14396s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 14397{
cca86cc8
SC
14398 char *section_name;
14399 char c;
684022ea 14400 char next_c = 0;
cca86cc8
SC
14401 int section_type;
14402 int section_flag;
14403 int section_entry_size;
14404 int section_alignment;
b34976b6 14405
cca86cc8
SC
14406 section_name = input_line_pointer;
14407 c = get_symbol_end ();
a816d1ed
AO
14408 if (c)
14409 next_c = *(input_line_pointer + 1);
cca86cc8 14410
4cf0dd0d
TS
14411 /* Do we have .section Name<,"flags">? */
14412 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 14413 {
4cf0dd0d
TS
14414 /* just after name is now '\0'. */
14415 *input_line_pointer = c;
cca86cc8
SC
14416 input_line_pointer = section_name;
14417 obj_elf_section (ignore);
14418 return;
14419 }
14420 input_line_pointer++;
14421
14422 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
14423 if (c == ',')
14424 section_type = get_absolute_expression ();
14425 else
14426 section_type = 0;
14427 if (*input_line_pointer++ == ',')
14428 section_flag = get_absolute_expression ();
14429 else
14430 section_flag = 0;
14431 if (*input_line_pointer++ == ',')
14432 section_entry_size = get_absolute_expression ();
14433 else
14434 section_entry_size = 0;
14435 if (*input_line_pointer++ == ',')
14436 section_alignment = get_absolute_expression ();
14437 else
14438 section_alignment = 0;
87975d2a
AM
14439 /* FIXME: really ignore? */
14440 (void) section_alignment;
cca86cc8 14441
a816d1ed
AO
14442 section_name = xstrdup (section_name);
14443
8ab8a5c8
RS
14444 /* When using the generic form of .section (as implemented by obj-elf.c),
14445 there's no way to set the section type to SHT_MIPS_DWARF. Users have
14446 traditionally had to fall back on the more common @progbits instead.
14447
14448 There's nothing really harmful in this, since bfd will correct
14449 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
708587a4 14450 means that, for backwards compatibility, the special_section entries
8ab8a5c8
RS
14451 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
14452
14453 Even so, we shouldn't force users of the MIPS .section syntax to
14454 incorrectly label the sections as SHT_PROGBITS. The best compromise
14455 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
14456 generic type-checking code. */
14457 if (section_type == SHT_MIPS_DWARF)
14458 section_type = SHT_PROGBITS;
14459
cca86cc8
SC
14460 obj_elf_change_section (section_name, section_type, section_flag,
14461 section_entry_size, 0, 0, 0);
a816d1ed
AO
14462
14463 if (now_seg->name != section_name)
14464 free (section_name);
cca86cc8 14465}
252b5132
RH
14466
14467void
17a2f251 14468mips_enable_auto_align (void)
252b5132
RH
14469{
14470 auto_align = 1;
14471}
14472
14473static void
17a2f251 14474s_cons (int log_size)
252b5132 14475{
a8dbcb85
TS
14476 segment_info_type *si = seg_info (now_seg);
14477 struct insn_label_list *l = si->label_list;
252b5132 14478
7d10b47d 14479 mips_emit_delays ();
252b5132 14480 if (log_size > 0 && auto_align)
462427c4 14481 mips_align (log_size, 0, l);
252b5132 14482 cons (1 << log_size);
a1facbec 14483 mips_clear_insn_labels ();
252b5132
RH
14484}
14485
14486static void
17a2f251 14487s_float_cons (int type)
252b5132 14488{
a8dbcb85
TS
14489 segment_info_type *si = seg_info (now_seg);
14490 struct insn_label_list *l = si->label_list;
252b5132 14491
7d10b47d 14492 mips_emit_delays ();
252b5132
RH
14493
14494 if (auto_align)
49309057
ILT
14495 {
14496 if (type == 'd')
462427c4 14497 mips_align (3, 0, l);
49309057 14498 else
462427c4 14499 mips_align (2, 0, l);
49309057 14500 }
252b5132 14501
252b5132 14502 float_cons (type);
a1facbec 14503 mips_clear_insn_labels ();
252b5132
RH
14504}
14505
14506/* Handle .globl. We need to override it because on Irix 5 you are
14507 permitted to say
14508 .globl foo .text
14509 where foo is an undefined symbol, to mean that foo should be
14510 considered to be the address of a function. */
14511
14512static void
17a2f251 14513s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
14514{
14515 char *name;
14516 int c;
14517 symbolS *symbolP;
14518 flagword flag;
14519
8a06b769 14520 do
252b5132 14521 {
8a06b769 14522 name = input_line_pointer;
252b5132 14523 c = get_symbol_end ();
8a06b769
TS
14524 symbolP = symbol_find_or_make (name);
14525 S_SET_EXTERNAL (symbolP);
14526
252b5132 14527 *input_line_pointer = c;
8a06b769 14528 SKIP_WHITESPACE ();
252b5132 14529
8a06b769
TS
14530 /* On Irix 5, every global symbol that is not explicitly labelled as
14531 being a function is apparently labelled as being an object. */
14532 flag = BSF_OBJECT;
252b5132 14533
8a06b769
TS
14534 if (!is_end_of_line[(unsigned char) *input_line_pointer]
14535 && (*input_line_pointer != ','))
14536 {
14537 char *secname;
14538 asection *sec;
14539
14540 secname = input_line_pointer;
14541 c = get_symbol_end ();
14542 sec = bfd_get_section_by_name (stdoutput, secname);
14543 if (sec == NULL)
14544 as_bad (_("%s: no such section"), secname);
14545 *input_line_pointer = c;
14546
14547 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
14548 flag = BSF_FUNCTION;
14549 }
14550
14551 symbol_get_bfdsym (symbolP)->flags |= flag;
14552
14553 c = *input_line_pointer;
14554 if (c == ',')
14555 {
14556 input_line_pointer++;
14557 SKIP_WHITESPACE ();
14558 if (is_end_of_line[(unsigned char) *input_line_pointer])
14559 c = '\n';
14560 }
14561 }
14562 while (c == ',');
252b5132 14563
252b5132
RH
14564 demand_empty_rest_of_line ();
14565}
14566
14567static void
17a2f251 14568s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
14569{
14570 char *opt;
14571 char c;
14572
14573 opt = input_line_pointer;
14574 c = get_symbol_end ();
14575
14576 if (*opt == 'O')
14577 {
14578 /* FIXME: What does this mean? */
14579 }
14580 else if (strncmp (opt, "pic", 3) == 0)
14581 {
14582 int i;
14583
14584 i = atoi (opt + 3);
14585 if (i == 0)
14586 mips_pic = NO_PIC;
14587 else if (i == 2)
143d77c5 14588 {
8b828383 14589 mips_pic = SVR4_PIC;
143d77c5
EC
14590 mips_abicalls = TRUE;
14591 }
252b5132
RH
14592 else
14593 as_bad (_(".option pic%d not supported"), i);
14594
4d0d148d 14595 if (mips_pic == SVR4_PIC)
252b5132
RH
14596 {
14597 if (g_switch_seen && g_switch_value != 0)
14598 as_warn (_("-G may not be used with SVR4 PIC code"));
14599 g_switch_value = 0;
14600 bfd_set_gp_size (stdoutput, 0);
14601 }
14602 }
14603 else
14604 as_warn (_("Unrecognized option \"%s\""), opt);
14605
14606 *input_line_pointer = c;
14607 demand_empty_rest_of_line ();
14608}
14609
14610/* This structure is used to hold a stack of .set values. */
14611
e972090a
NC
14612struct mips_option_stack
14613{
252b5132
RH
14614 struct mips_option_stack *next;
14615 struct mips_set_options options;
14616};
14617
14618static struct mips_option_stack *mips_opts_stack;
14619
14620/* Handle the .set pseudo-op. */
14621
14622static void
17a2f251 14623s_mipsset (int x ATTRIBUTE_UNUSED)
252b5132
RH
14624{
14625 char *name = input_line_pointer, ch;
c6278170 14626 const struct mips_ase *ase;
252b5132
RH
14627
14628 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 14629 ++input_line_pointer;
252b5132
RH
14630 ch = *input_line_pointer;
14631 *input_line_pointer = '\0';
14632
14633 if (strcmp (name, "reorder") == 0)
14634 {
7d10b47d
RS
14635 if (mips_opts.noreorder)
14636 end_noreorder ();
252b5132
RH
14637 }
14638 else if (strcmp (name, "noreorder") == 0)
14639 {
7d10b47d
RS
14640 if (!mips_opts.noreorder)
14641 start_noreorder ();
252b5132 14642 }
741fe287
MR
14643 else if (strncmp (name, "at=", 3) == 0)
14644 {
14645 char *s = name + 3;
14646
14647 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
14648 as_bad (_("Unrecognized register name `%s'"), s);
14649 }
252b5132
RH
14650 else if (strcmp (name, "at") == 0)
14651 {
741fe287 14652 mips_opts.at = ATREG;
252b5132
RH
14653 }
14654 else if (strcmp (name, "noat") == 0)
14655 {
741fe287 14656 mips_opts.at = ZERO;
252b5132
RH
14657 }
14658 else if (strcmp (name, "macro") == 0)
14659 {
14660 mips_opts.warn_about_macros = 0;
14661 }
14662 else if (strcmp (name, "nomacro") == 0)
14663 {
14664 if (mips_opts.noreorder == 0)
14665 as_bad (_("`noreorder' must be set before `nomacro'"));
14666 mips_opts.warn_about_macros = 1;
14667 }
14668 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
14669 {
14670 mips_opts.nomove = 0;
14671 }
14672 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
14673 {
14674 mips_opts.nomove = 1;
14675 }
14676 else if (strcmp (name, "bopt") == 0)
14677 {
14678 mips_opts.nobopt = 0;
14679 }
14680 else if (strcmp (name, "nobopt") == 0)
14681 {
14682 mips_opts.nobopt = 1;
14683 }
ad3fea08
TS
14684 else if (strcmp (name, "gp=default") == 0)
14685 mips_opts.gp32 = file_mips_gp32;
14686 else if (strcmp (name, "gp=32") == 0)
14687 mips_opts.gp32 = 1;
14688 else if (strcmp (name, "gp=64") == 0)
14689 {
14690 if (!ISA_HAS_64BIT_REGS (mips_opts.isa))
20203fb9 14691 as_warn (_("%s isa does not support 64-bit registers"),
ad3fea08
TS
14692 mips_cpu_info_from_isa (mips_opts.isa)->name);
14693 mips_opts.gp32 = 0;
14694 }
14695 else if (strcmp (name, "fp=default") == 0)
14696 mips_opts.fp32 = file_mips_fp32;
14697 else if (strcmp (name, "fp=32") == 0)
14698 mips_opts.fp32 = 1;
14699 else if (strcmp (name, "fp=64") == 0)
14700 {
14701 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
20203fb9 14702 as_warn (_("%s isa does not support 64-bit floating point registers"),
ad3fea08
TS
14703 mips_cpu_info_from_isa (mips_opts.isa)->name);
14704 mips_opts.fp32 = 0;
14705 }
037b32b9
AN
14706 else if (strcmp (name, "softfloat") == 0)
14707 mips_opts.soft_float = 1;
14708 else if (strcmp (name, "hardfloat") == 0)
14709 mips_opts.soft_float = 0;
14710 else if (strcmp (name, "singlefloat") == 0)
14711 mips_opts.single_float = 1;
14712 else if (strcmp (name, "doublefloat") == 0)
14713 mips_opts.single_float = 0;
252b5132
RH
14714 else if (strcmp (name, "mips16") == 0
14715 || strcmp (name, "MIPS-16") == 0)
df58fc94
RS
14716 {
14717 if (mips_opts.micromips == 1)
14718 as_fatal (_("`mips16' cannot be used with `micromips'"));
14719 mips_opts.mips16 = 1;
14720 }
252b5132
RH
14721 else if (strcmp (name, "nomips16") == 0
14722 || strcmp (name, "noMIPS-16") == 0)
14723 mips_opts.mips16 = 0;
df58fc94
RS
14724 else if (strcmp (name, "micromips") == 0)
14725 {
14726 if (mips_opts.mips16 == 1)
14727 as_fatal (_("`micromips' cannot be used with `mips16'"));
14728 mips_opts.micromips = 1;
14729 }
14730 else if (strcmp (name, "nomicromips") == 0)
14731 mips_opts.micromips = 0;
c6278170
RS
14732 else if (name[0] == 'n'
14733 && name[1] == 'o'
14734 && (ase = mips_lookup_ase (name + 2)))
14735 mips_set_ase (ase, FALSE);
14736 else if ((ase = mips_lookup_ase (name)))
14737 mips_set_ase (ase, TRUE);
1a2c1fad 14738 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 14739 {
af7ee8bf 14740 int reset = 0;
252b5132 14741
1a2c1fad
CD
14742 /* Permit the user to change the ISA and architecture on the fly.
14743 Needless to say, misuse can cause serious problems. */
81a21e38 14744 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
af7ee8bf
CD
14745 {
14746 reset = 1;
14747 mips_opts.isa = file_mips_isa;
1a2c1fad 14748 mips_opts.arch = file_mips_arch;
1a2c1fad
CD
14749 }
14750 else if (strncmp (name, "arch=", 5) == 0)
14751 {
14752 const struct mips_cpu_info *p;
14753
14754 p = mips_parse_cpu("internal use", name + 5);
14755 if (!p)
14756 as_bad (_("unknown architecture %s"), name + 5);
14757 else
14758 {
14759 mips_opts.arch = p->cpu;
14760 mips_opts.isa = p->isa;
14761 }
14762 }
81a21e38
TS
14763 else if (strncmp (name, "mips", 4) == 0)
14764 {
14765 const struct mips_cpu_info *p;
14766
14767 p = mips_parse_cpu("internal use", name);
14768 if (!p)
14769 as_bad (_("unknown ISA level %s"), name + 4);
14770 else
14771 {
14772 mips_opts.arch = p->cpu;
14773 mips_opts.isa = p->isa;
14774 }
14775 }
af7ee8bf 14776 else
81a21e38 14777 as_bad (_("unknown ISA or architecture %s"), name);
af7ee8bf
CD
14778
14779 switch (mips_opts.isa)
98d3f06f
KH
14780 {
14781 case 0:
98d3f06f 14782 break;
af7ee8bf
CD
14783 case ISA_MIPS1:
14784 case ISA_MIPS2:
14785 case ISA_MIPS32:
14786 case ISA_MIPS32R2:
98d3f06f
KH
14787 mips_opts.gp32 = 1;
14788 mips_opts.fp32 = 1;
14789 break;
af7ee8bf
CD
14790 case ISA_MIPS3:
14791 case ISA_MIPS4:
14792 case ISA_MIPS5:
14793 case ISA_MIPS64:
5f74bc13 14794 case ISA_MIPS64R2:
98d3f06f 14795 mips_opts.gp32 = 0;
e407c74b
NC
14796 if (mips_opts.arch == CPU_R5900)
14797 {
14798 mips_opts.fp32 = 1;
14799 }
14800 else
14801 {
98d3f06f 14802 mips_opts.fp32 = 0;
e407c74b 14803 }
98d3f06f
KH
14804 break;
14805 default:
14806 as_bad (_("unknown ISA level %s"), name + 4);
14807 break;
14808 }
af7ee8bf 14809 if (reset)
98d3f06f 14810 {
af7ee8bf
CD
14811 mips_opts.gp32 = file_mips_gp32;
14812 mips_opts.fp32 = file_mips_fp32;
98d3f06f 14813 }
252b5132
RH
14814 }
14815 else if (strcmp (name, "autoextend") == 0)
14816 mips_opts.noautoextend = 0;
14817 else if (strcmp (name, "noautoextend") == 0)
14818 mips_opts.noautoextend = 1;
833794fc
MR
14819 else if (strcmp (name, "insn32") == 0)
14820 mips_opts.insn32 = TRUE;
14821 else if (strcmp (name, "noinsn32") == 0)
14822 mips_opts.insn32 = FALSE;
252b5132
RH
14823 else if (strcmp (name, "push") == 0)
14824 {
14825 struct mips_option_stack *s;
14826
14827 s = (struct mips_option_stack *) xmalloc (sizeof *s);
14828 s->next = mips_opts_stack;
14829 s->options = mips_opts;
14830 mips_opts_stack = s;
14831 }
14832 else if (strcmp (name, "pop") == 0)
14833 {
14834 struct mips_option_stack *s;
14835
14836 s = mips_opts_stack;
14837 if (s == NULL)
14838 as_bad (_(".set pop with no .set push"));
14839 else
14840 {
14841 /* If we're changing the reorder mode we need to handle
14842 delay slots correctly. */
14843 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 14844 start_noreorder ();
252b5132 14845 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 14846 end_noreorder ();
252b5132
RH
14847
14848 mips_opts = s->options;
14849 mips_opts_stack = s->next;
14850 free (s);
14851 }
14852 }
aed1a261
RS
14853 else if (strcmp (name, "sym32") == 0)
14854 mips_opts.sym32 = TRUE;
14855 else if (strcmp (name, "nosym32") == 0)
14856 mips_opts.sym32 = FALSE;
e6559e01
JM
14857 else if (strchr (name, ','))
14858 {
14859 /* Generic ".set" directive; use the generic handler. */
14860 *input_line_pointer = ch;
14861 input_line_pointer = name;
14862 s_set (0);
14863 return;
14864 }
252b5132
RH
14865 else
14866 {
14867 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
14868 }
c6278170 14869 mips_check_isa_supports_ases ();
252b5132
RH
14870 *input_line_pointer = ch;
14871 demand_empty_rest_of_line ();
14872}
14873
14874/* Handle the .abicalls pseudo-op. I believe this is equivalent to
14875 .option pic2. It means to generate SVR4 PIC calls. */
14876
14877static void
17a2f251 14878s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
14879{
14880 mips_pic = SVR4_PIC;
143d77c5 14881 mips_abicalls = TRUE;
4d0d148d
TS
14882
14883 if (g_switch_seen && g_switch_value != 0)
14884 as_warn (_("-G may not be used with SVR4 PIC code"));
14885 g_switch_value = 0;
14886
252b5132
RH
14887 bfd_set_gp_size (stdoutput, 0);
14888 demand_empty_rest_of_line ();
14889}
14890
14891/* Handle the .cpload pseudo-op. This is used when generating SVR4
14892 PIC code. It sets the $gp register for the function based on the
14893 function address, which is in the register named in the argument.
14894 This uses a relocation against _gp_disp, which is handled specially
14895 by the linker. The result is:
14896 lui $gp,%hi(_gp_disp)
14897 addiu $gp,$gp,%lo(_gp_disp)
14898 addu $gp,$gp,.cpload argument
aa6975fb
ILT
14899 The .cpload argument is normally $25 == $t9.
14900
14901 The -mno-shared option changes this to:
bbe506e8
TS
14902 lui $gp,%hi(__gnu_local_gp)
14903 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
14904 and the argument is ignored. This saves an instruction, but the
14905 resulting code is not position independent; it uses an absolute
bbe506e8
TS
14906 address for __gnu_local_gp. Thus code assembled with -mno-shared
14907 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
14908
14909static void
17a2f251 14910s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
14911{
14912 expressionS ex;
aa6975fb
ILT
14913 int reg;
14914 int in_shared;
252b5132 14915
6478892d
TS
14916 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
14917 .cpload is ignored. */
14918 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
14919 {
14920 s_ignore (0);
14921 return;
14922 }
14923
a276b80c
MR
14924 if (mips_opts.mips16)
14925 {
14926 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
14927 ignore_rest_of_line ();
14928 return;
14929 }
14930
d3ecfc59 14931 /* .cpload should be in a .set noreorder section. */
252b5132
RH
14932 if (mips_opts.noreorder == 0)
14933 as_warn (_(".cpload not in noreorder section"));
14934
aa6975fb
ILT
14935 reg = tc_get_register (0);
14936
14937 /* If we need to produce a 64-bit address, we are better off using
14938 the default instruction sequence. */
aed1a261 14939 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 14940
252b5132 14941 ex.X_op = O_symbol;
bbe506e8
TS
14942 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
14943 "__gnu_local_gp");
252b5132
RH
14944 ex.X_op_symbol = NULL;
14945 ex.X_add_number = 0;
14946
14947 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 14948 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 14949
8a75745d
MR
14950 mips_mark_labels ();
14951 mips_assembling_insn = TRUE;
14952
584892a6 14953 macro_start ();
67c0d1eb
RS
14954 macro_build_lui (&ex, mips_gp_register);
14955 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 14956 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
14957 if (in_shared)
14958 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
14959 mips_gp_register, reg);
584892a6 14960 macro_end ();
252b5132 14961
8a75745d 14962 mips_assembling_insn = FALSE;
252b5132
RH
14963 demand_empty_rest_of_line ();
14964}
14965
6478892d
TS
14966/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
14967 .cpsetup $reg1, offset|$reg2, label
14968
14969 If offset is given, this results in:
14970 sd $gp, offset($sp)
956cd1d6 14971 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
14972 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
14973 daddu $gp, $gp, $reg1
6478892d
TS
14974
14975 If $reg2 is given, this results in:
14976 daddu $reg2, $gp, $0
956cd1d6 14977 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
14978 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
14979 daddu $gp, $gp, $reg1
aa6975fb
ILT
14980 $reg1 is normally $25 == $t9.
14981
14982 The -mno-shared option replaces the last three instructions with
14983 lui $gp,%hi(_gp)
54f4ddb3 14984 addiu $gp,$gp,%lo(_gp) */
aa6975fb 14985
6478892d 14986static void
17a2f251 14987s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
14988{
14989 expressionS ex_off;
14990 expressionS ex_sym;
14991 int reg1;
6478892d 14992
8586fc66 14993 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
14994 We also need NewABI support. */
14995 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
14996 {
14997 s_ignore (0);
14998 return;
14999 }
15000
a276b80c
MR
15001 if (mips_opts.mips16)
15002 {
15003 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
15004 ignore_rest_of_line ();
15005 return;
15006 }
15007
6478892d
TS
15008 reg1 = tc_get_register (0);
15009 SKIP_WHITESPACE ();
15010 if (*input_line_pointer != ',')
15011 {
15012 as_bad (_("missing argument separator ',' for .cpsetup"));
15013 return;
15014 }
15015 else
80245285 15016 ++input_line_pointer;
6478892d
TS
15017 SKIP_WHITESPACE ();
15018 if (*input_line_pointer == '$')
80245285
TS
15019 {
15020 mips_cpreturn_register = tc_get_register (0);
15021 mips_cpreturn_offset = -1;
15022 }
6478892d 15023 else
80245285
TS
15024 {
15025 mips_cpreturn_offset = get_absolute_expression ();
15026 mips_cpreturn_register = -1;
15027 }
6478892d
TS
15028 SKIP_WHITESPACE ();
15029 if (*input_line_pointer != ',')
15030 {
15031 as_bad (_("missing argument separator ',' for .cpsetup"));
15032 return;
15033 }
15034 else
f9419b05 15035 ++input_line_pointer;
6478892d 15036 SKIP_WHITESPACE ();
f21f8242 15037 expression (&ex_sym);
6478892d 15038
8a75745d
MR
15039 mips_mark_labels ();
15040 mips_assembling_insn = TRUE;
15041
584892a6 15042 macro_start ();
6478892d
TS
15043 if (mips_cpreturn_register == -1)
15044 {
15045 ex_off.X_op = O_constant;
15046 ex_off.X_add_symbol = NULL;
15047 ex_off.X_op_symbol = NULL;
15048 ex_off.X_add_number = mips_cpreturn_offset;
15049
67c0d1eb 15050 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 15051 BFD_RELOC_LO16, SP);
6478892d
TS
15052 }
15053 else
67c0d1eb 15054 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
17a2f251 15055 mips_gp_register, 0);
6478892d 15056
aed1a261 15057 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb 15058 {
df58fc94 15059 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
aa6975fb
ILT
15060 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
15061 BFD_RELOC_HI16_S);
15062
15063 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
15064 mips_gp_register, -1, BFD_RELOC_GPREL16,
15065 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
15066
15067 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
15068 mips_gp_register, reg1);
15069 }
15070 else
15071 {
15072 expressionS ex;
15073
15074 ex.X_op = O_symbol;
4184909a 15075 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
15076 ex.X_op_symbol = NULL;
15077 ex.X_add_number = 0;
6e1304d8 15078
aa6975fb
ILT
15079 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
15080 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
15081
15082 macro_build_lui (&ex, mips_gp_register);
15083 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
15084 mips_gp_register, BFD_RELOC_LO16);
15085 }
f21f8242 15086
584892a6 15087 macro_end ();
6478892d 15088
8a75745d 15089 mips_assembling_insn = FALSE;
6478892d
TS
15090 demand_empty_rest_of_line ();
15091}
15092
15093static void
17a2f251 15094s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
15095{
15096 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
54f4ddb3 15097 .cplocal is ignored. */
6478892d
TS
15098 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
15099 {
15100 s_ignore (0);
15101 return;
15102 }
15103
a276b80c
MR
15104 if (mips_opts.mips16)
15105 {
15106 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
15107 ignore_rest_of_line ();
15108 return;
15109 }
15110
6478892d 15111 mips_gp_register = tc_get_register (0);
85b51719 15112 demand_empty_rest_of_line ();
6478892d
TS
15113}
15114
252b5132
RH
15115/* Handle the .cprestore pseudo-op. This stores $gp into a given
15116 offset from $sp. The offset is remembered, and after making a PIC
15117 call $gp is restored from that location. */
15118
15119static void
17a2f251 15120s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
15121{
15122 expressionS ex;
252b5132 15123
6478892d 15124 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 15125 .cprestore is ignored. */
6478892d 15126 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
15127 {
15128 s_ignore (0);
15129 return;
15130 }
15131
a276b80c
MR
15132 if (mips_opts.mips16)
15133 {
15134 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
15135 ignore_rest_of_line ();
15136 return;
15137 }
15138
252b5132 15139 mips_cprestore_offset = get_absolute_expression ();
7a621144 15140 mips_cprestore_valid = 1;
252b5132
RH
15141
15142 ex.X_op = O_constant;
15143 ex.X_add_symbol = NULL;
15144 ex.X_op_symbol = NULL;
15145 ex.X_add_number = mips_cprestore_offset;
15146
8a75745d
MR
15147 mips_mark_labels ();
15148 mips_assembling_insn = TRUE;
15149
584892a6 15150 macro_start ();
67c0d1eb
RS
15151 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
15152 SP, HAVE_64BIT_ADDRESSES);
584892a6 15153 macro_end ();
252b5132 15154
8a75745d 15155 mips_assembling_insn = FALSE;
252b5132
RH
15156 demand_empty_rest_of_line ();
15157}
15158
6478892d 15159/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 15160 was given in the preceding .cpsetup, it results in:
6478892d 15161 ld $gp, offset($sp)
76b3015f 15162
6478892d 15163 If a register $reg2 was given there, it results in:
54f4ddb3
TS
15164 daddu $gp, $reg2, $0 */
15165
6478892d 15166static void
17a2f251 15167s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
15168{
15169 expressionS ex;
6478892d
TS
15170
15171 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
15172 We also need NewABI support. */
15173 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
15174 {
15175 s_ignore (0);
15176 return;
15177 }
15178
a276b80c
MR
15179 if (mips_opts.mips16)
15180 {
15181 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
15182 ignore_rest_of_line ();
15183 return;
15184 }
15185
8a75745d
MR
15186 mips_mark_labels ();
15187 mips_assembling_insn = TRUE;
15188
584892a6 15189 macro_start ();
6478892d
TS
15190 if (mips_cpreturn_register == -1)
15191 {
15192 ex.X_op = O_constant;
15193 ex.X_add_symbol = NULL;
15194 ex.X_op_symbol = NULL;
15195 ex.X_add_number = mips_cpreturn_offset;
15196
67c0d1eb 15197 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
15198 }
15199 else
67c0d1eb 15200 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
17a2f251 15201 mips_cpreturn_register, 0);
584892a6 15202 macro_end ();
6478892d 15203
8a75745d 15204 mips_assembling_insn = FALSE;
6478892d
TS
15205 demand_empty_rest_of_line ();
15206}
15207
d0f13682
CLT
15208/* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
15209 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
15210 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
15211 debug information or MIPS16 TLS. */
741d6ea8
JM
15212
15213static void
d0f13682
CLT
15214s_tls_rel_directive (const size_t bytes, const char *dirstr,
15215 bfd_reloc_code_real_type rtype)
741d6ea8
JM
15216{
15217 expressionS ex;
15218 char *p;
15219
15220 expression (&ex);
15221
15222 if (ex.X_op != O_symbol)
15223 {
d0f13682 15224 as_bad (_("Unsupported use of %s"), dirstr);
741d6ea8
JM
15225 ignore_rest_of_line ();
15226 }
15227
15228 p = frag_more (bytes);
15229 md_number_to_chars (p, 0, bytes);
d0f13682 15230 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
741d6ea8 15231 demand_empty_rest_of_line ();
de64cffd 15232 mips_clear_insn_labels ();
741d6ea8
JM
15233}
15234
15235/* Handle .dtprelword. */
15236
15237static void
15238s_dtprelword (int ignore ATTRIBUTE_UNUSED)
15239{
d0f13682 15240 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
741d6ea8
JM
15241}
15242
15243/* Handle .dtpreldword. */
15244
15245static void
15246s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
15247{
d0f13682
CLT
15248 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
15249}
15250
15251/* Handle .tprelword. */
15252
15253static void
15254s_tprelword (int ignore ATTRIBUTE_UNUSED)
15255{
15256 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
15257}
15258
15259/* Handle .tpreldword. */
15260
15261static void
15262s_tpreldword (int ignore ATTRIBUTE_UNUSED)
15263{
15264 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
741d6ea8
JM
15265}
15266
6478892d
TS
15267/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
15268 code. It sets the offset to use in gp_rel relocations. */
15269
15270static void
17a2f251 15271s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
15272{
15273 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
15274 We also need NewABI support. */
15275 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
15276 {
15277 s_ignore (0);
15278 return;
15279 }
15280
def2e0dd 15281 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
15282
15283 demand_empty_rest_of_line ();
15284}
15285
252b5132
RH
15286/* Handle the .gpword pseudo-op. This is used when generating PIC
15287 code. It generates a 32 bit GP relative reloc. */
15288
15289static void
17a2f251 15290s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132 15291{
a8dbcb85
TS
15292 segment_info_type *si;
15293 struct insn_label_list *l;
252b5132
RH
15294 expressionS ex;
15295 char *p;
15296
15297 /* When not generating PIC code, this is treated as .word. */
15298 if (mips_pic != SVR4_PIC)
15299 {
15300 s_cons (2);
15301 return;
15302 }
15303
a8dbcb85
TS
15304 si = seg_info (now_seg);
15305 l = si->label_list;
7d10b47d 15306 mips_emit_delays ();
252b5132 15307 if (auto_align)
462427c4 15308 mips_align (2, 0, l);
252b5132
RH
15309
15310 expression (&ex);
a1facbec 15311 mips_clear_insn_labels ();
252b5132
RH
15312
15313 if (ex.X_op != O_symbol || ex.X_add_number != 0)
15314 {
15315 as_bad (_("Unsupported use of .gpword"));
15316 ignore_rest_of_line ();
15317 }
15318
15319 p = frag_more (4);
17a2f251 15320 md_number_to_chars (p, 0, 4);
b34976b6 15321 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 15322 BFD_RELOC_GPREL32);
252b5132
RH
15323
15324 demand_empty_rest_of_line ();
15325}
15326
10181a0d 15327static void
17a2f251 15328s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d 15329{
a8dbcb85
TS
15330 segment_info_type *si;
15331 struct insn_label_list *l;
10181a0d
AO
15332 expressionS ex;
15333 char *p;
15334
15335 /* When not generating PIC code, this is treated as .dword. */
15336 if (mips_pic != SVR4_PIC)
15337 {
15338 s_cons (3);
15339 return;
15340 }
15341
a8dbcb85
TS
15342 si = seg_info (now_seg);
15343 l = si->label_list;
7d10b47d 15344 mips_emit_delays ();
10181a0d 15345 if (auto_align)
462427c4 15346 mips_align (3, 0, l);
10181a0d
AO
15347
15348 expression (&ex);
a1facbec 15349 mips_clear_insn_labels ();
10181a0d
AO
15350
15351 if (ex.X_op != O_symbol || ex.X_add_number != 0)
15352 {
15353 as_bad (_("Unsupported use of .gpdword"));
15354 ignore_rest_of_line ();
15355 }
15356
15357 p = frag_more (8);
17a2f251 15358 md_number_to_chars (p, 0, 8);
a105a300 15359 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
6e1304d8 15360 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
15361
15362 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8
RS
15363 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
15364 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
15365
15366 demand_empty_rest_of_line ();
15367}
15368
a3f278e2
CM
15369/* Handle the .ehword pseudo-op. This is used when generating unwinding
15370 tables. It generates a R_MIPS_EH reloc. */
15371
15372static void
15373s_ehword (int ignore ATTRIBUTE_UNUSED)
15374{
15375 expressionS ex;
15376 char *p;
15377
15378 mips_emit_delays ();
15379
15380 expression (&ex);
15381 mips_clear_insn_labels ();
15382
15383 if (ex.X_op != O_symbol || ex.X_add_number != 0)
15384 {
15385 as_bad (_("Unsupported use of .ehword"));
15386 ignore_rest_of_line ();
15387 }
15388
15389 p = frag_more (4);
15390 md_number_to_chars (p, 0, 4);
15391 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
15392 BFD_RELOC_MIPS_EH);
15393
15394 demand_empty_rest_of_line ();
15395}
15396
252b5132
RH
15397/* Handle the .cpadd pseudo-op. This is used when dealing with switch
15398 tables in SVR4 PIC code. */
15399
15400static void
17a2f251 15401s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 15402{
252b5132
RH
15403 int reg;
15404
10181a0d
AO
15405 /* This is ignored when not generating SVR4 PIC code. */
15406 if (mips_pic != SVR4_PIC)
252b5132
RH
15407 {
15408 s_ignore (0);
15409 return;
15410 }
15411
8a75745d
MR
15412 mips_mark_labels ();
15413 mips_assembling_insn = TRUE;
15414
252b5132 15415 /* Add $gp to the register named as an argument. */
584892a6 15416 macro_start ();
252b5132 15417 reg = tc_get_register (0);
67c0d1eb 15418 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 15419 macro_end ();
252b5132 15420
8a75745d 15421 mips_assembling_insn = FALSE;
bdaaa2e1 15422 demand_empty_rest_of_line ();
252b5132
RH
15423}
15424
15425/* Handle the .insn pseudo-op. This marks instruction labels in
df58fc94 15426 mips16/micromips mode. This permits the linker to handle them specially,
252b5132
RH
15427 such as generating jalx instructions when needed. We also make
15428 them odd for the duration of the assembly, in order to generate the
15429 right sort of code. We will make them even in the adjust_symtab
15430 routine, while leaving them marked. This is convenient for the
15431 debugger and the disassembler. The linker knows to make them odd
15432 again. */
15433
15434static void
17a2f251 15435s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 15436{
df58fc94 15437 mips_mark_labels ();
252b5132
RH
15438
15439 demand_empty_rest_of_line ();
15440}
15441
ba92f887
MR
15442/* Handle the .nan pseudo-op. */
15443
15444static void
15445s_nan (int ignore ATTRIBUTE_UNUSED)
15446{
15447 static const char str_legacy[] = "legacy";
15448 static const char str_2008[] = "2008";
15449 size_t i;
15450
15451 for (i = 0; !is_end_of_line[(unsigned char) input_line_pointer[i]]; i++);
15452
15453 if (i == sizeof (str_2008) - 1
15454 && memcmp (input_line_pointer, str_2008, i) == 0)
15455 mips_flag_nan2008 = TRUE;
15456 else if (i == sizeof (str_legacy) - 1
15457 && memcmp (input_line_pointer, str_legacy, i) == 0)
15458 mips_flag_nan2008 = FALSE;
15459 else
15460 as_bad (_("Bad .nan directive"));
15461
15462 input_line_pointer += i;
15463 demand_empty_rest_of_line ();
15464}
15465
754e2bb9
RS
15466/* Handle a .stab[snd] directive. Ideally these directives would be
15467 implemented in a transparent way, so that removing them would not
15468 have any effect on the generated instructions. However, s_stab
15469 internally changes the section, so in practice we need to decide
15470 now whether the preceding label marks compressed code. We do not
15471 support changing the compression mode of a label after a .stab*
15472 directive, such as in:
15473
15474 foo:
15475 .stabs ...
15476 .set mips16
15477
15478 so the current mode wins. */
252b5132
RH
15479
15480static void
17a2f251 15481s_mips_stab (int type)
252b5132 15482{
754e2bb9 15483 mips_mark_labels ();
252b5132
RH
15484 s_stab (type);
15485}
15486
54f4ddb3 15487/* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
252b5132
RH
15488
15489static void
17a2f251 15490s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
15491{
15492 char *name;
15493 int c;
15494 symbolS *symbolP;
15495 expressionS exp;
15496
15497 name = input_line_pointer;
15498 c = get_symbol_end ();
15499 symbolP = symbol_find_or_make (name);
15500 S_SET_WEAK (symbolP);
15501 *input_line_pointer = c;
15502
15503 SKIP_WHITESPACE ();
15504
15505 if (! is_end_of_line[(unsigned char) *input_line_pointer])
15506 {
15507 if (S_IS_DEFINED (symbolP))
15508 {
20203fb9 15509 as_bad (_("ignoring attempt to redefine symbol %s"),
252b5132
RH
15510 S_GET_NAME (symbolP));
15511 ignore_rest_of_line ();
15512 return;
15513 }
bdaaa2e1 15514
252b5132
RH
15515 if (*input_line_pointer == ',')
15516 {
15517 ++input_line_pointer;
15518 SKIP_WHITESPACE ();
15519 }
bdaaa2e1 15520
252b5132
RH
15521 expression (&exp);
15522 if (exp.X_op != O_symbol)
15523 {
20203fb9 15524 as_bad (_("bad .weakext directive"));
98d3f06f 15525 ignore_rest_of_line ();
252b5132
RH
15526 return;
15527 }
49309057 15528 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
15529 }
15530
15531 demand_empty_rest_of_line ();
15532}
15533
15534/* Parse a register string into a number. Called from the ECOFF code
15535 to parse .frame. The argument is non-zero if this is the frame
15536 register, so that we can record it in mips_frame_reg. */
15537
15538int
17a2f251 15539tc_get_register (int frame)
252b5132 15540{
707bfff6 15541 unsigned int reg;
252b5132
RH
15542
15543 SKIP_WHITESPACE ();
707bfff6
TS
15544 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
15545 reg = 0;
252b5132 15546 if (frame)
7a621144
DJ
15547 {
15548 mips_frame_reg = reg != 0 ? reg : SP;
15549 mips_frame_reg_valid = 1;
15550 mips_cprestore_valid = 0;
15551 }
252b5132
RH
15552 return reg;
15553}
15554
15555valueT
17a2f251 15556md_section_align (asection *seg, valueT addr)
252b5132
RH
15557{
15558 int align = bfd_get_section_alignment (stdoutput, seg);
15559
f3ded42a
RS
15560 /* We don't need to align ELF sections to the full alignment.
15561 However, Irix 5 may prefer that we align them at least to a 16
15562 byte boundary. We don't bother to align the sections if we
15563 are targeted for an embedded system. */
15564 if (strncmp (TARGET_OS, "elf", 3) == 0)
15565 return addr;
15566 if (align > 4)
15567 align = 4;
252b5132
RH
15568
15569 return ((addr + (1 << align) - 1) & (-1 << align));
15570}
15571
15572/* Utility routine, called from above as well. If called while the
15573 input file is still being read, it's only an approximation. (For
15574 example, a symbol may later become defined which appeared to be
15575 undefined earlier.) */
15576
15577static int
17a2f251 15578nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
15579{
15580 if (sym == 0)
15581 return 0;
15582
4d0d148d 15583 if (g_switch_value > 0)
252b5132
RH
15584 {
15585 const char *symname;
15586 int change;
15587
c9914766 15588 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
15589 register. It can be if it is smaller than the -G size or if
15590 it is in the .sdata or .sbss section. Certain symbols can
c9914766 15591 not be referenced off the $gp, although it appears as though
252b5132
RH
15592 they can. */
15593 symname = S_GET_NAME (sym);
15594 if (symname != (const char *) NULL
15595 && (strcmp (symname, "eprol") == 0
15596 || strcmp (symname, "etext") == 0
15597 || strcmp (symname, "_gp") == 0
15598 || strcmp (symname, "edata") == 0
15599 || strcmp (symname, "_fbss") == 0
15600 || strcmp (symname, "_fdata") == 0
15601 || strcmp (symname, "_ftext") == 0
15602 || strcmp (symname, "end") == 0
15603 || strcmp (symname, "_gp_disp") == 0))
15604 change = 1;
15605 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
15606 && (0
15607#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
15608 || (symbol_get_obj (sym)->ecoff_extern_size != 0
15609 && (symbol_get_obj (sym)->ecoff_extern_size
15610 <= g_switch_value))
252b5132
RH
15611#endif
15612 /* We must defer this decision until after the whole
15613 file has been read, since there might be a .extern
15614 after the first use of this symbol. */
15615 || (before_relaxing
15616#ifndef NO_ECOFF_DEBUGGING
49309057 15617 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
15618#endif
15619 && S_GET_VALUE (sym) == 0)
15620 || (S_GET_VALUE (sym) != 0
15621 && S_GET_VALUE (sym) <= g_switch_value)))
15622 change = 0;
15623 else
15624 {
15625 const char *segname;
15626
15627 segname = segment_name (S_GET_SEGMENT (sym));
9c2799c2 15628 gas_assert (strcmp (segname, ".lit8") != 0
252b5132
RH
15629 && strcmp (segname, ".lit4") != 0);
15630 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
15631 && strcmp (segname, ".sbss") != 0
15632 && strncmp (segname, ".sdata.", 7) != 0
d4dc2f22
TS
15633 && strncmp (segname, ".sbss.", 6) != 0
15634 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
fba2b7f9 15635 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
15636 }
15637 return change;
15638 }
15639 else
c9914766 15640 /* We are not optimizing for the $gp register. */
252b5132
RH
15641 return 1;
15642}
15643
5919d012
RS
15644
15645/* Return true if the given symbol should be considered local for SVR4 PIC. */
15646
15647static bfd_boolean
17a2f251 15648pic_need_relax (symbolS *sym, asection *segtype)
5919d012
RS
15649{
15650 asection *symsec;
5919d012
RS
15651
15652 /* Handle the case of a symbol equated to another symbol. */
15653 while (symbol_equated_reloc_p (sym))
15654 {
15655 symbolS *n;
15656
5f0fe04b 15657 /* It's possible to get a loop here in a badly written program. */
5919d012
RS
15658 n = symbol_get_value_expression (sym)->X_add_symbol;
15659 if (n == sym)
15660 break;
15661 sym = n;
15662 }
15663
df1f3cda
DD
15664 if (symbol_section_p (sym))
15665 return TRUE;
15666
5919d012
RS
15667 symsec = S_GET_SEGMENT (sym);
15668
5919d012 15669 /* This must duplicate the test in adjust_reloc_syms. */
45dfa85a
AM
15670 return (!bfd_is_und_section (symsec)
15671 && !bfd_is_abs_section (symsec)
5f0fe04b
TS
15672 && !bfd_is_com_section (symsec)
15673 && !s_is_linkonce (sym, segtype)
5919d012 15674 /* A global or weak symbol is treated as external. */
f3ded42a 15675 && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
5919d012
RS
15676}
15677
15678
252b5132
RH
15679/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
15680 extended opcode. SEC is the section the frag is in. */
15681
15682static int
17a2f251 15683mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132
RH
15684{
15685 int type;
3ccad066 15686 const struct mips_int_operand *operand;
252b5132 15687 offsetT val;
252b5132 15688 segT symsec;
98aa84af 15689 fragS *sym_frag;
252b5132
RH
15690
15691 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
15692 return 0;
15693 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
15694 return 1;
15695
15696 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
3ccad066 15697 operand = mips16_immed_operand (type, FALSE);
252b5132 15698
98aa84af 15699 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 15700 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 15701 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132 15702
3ccad066 15703 if (operand->root.type == OP_PCREL)
252b5132 15704 {
3ccad066 15705 const struct mips_pcrel_operand *pcrel_op;
252b5132 15706 addressT addr;
3ccad066 15707 offsetT maxtiny;
252b5132
RH
15708
15709 /* We won't have the section when we are called from
15710 mips_relax_frag. However, we will always have been called
15711 from md_estimate_size_before_relax first. If this is a
15712 branch to a different section, we mark it as such. If SEC is
15713 NULL, and the frag is not marked, then it must be a branch to
15714 the same section. */
3ccad066 15715 pcrel_op = (const struct mips_pcrel_operand *) operand;
252b5132
RH
15716 if (sec == NULL)
15717 {
15718 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
15719 return 1;
15720 }
15721 else
15722 {
98aa84af 15723 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
15724 if (symsec != sec)
15725 {
15726 fragp->fr_subtype =
15727 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
15728
15729 /* FIXME: We should support this, and let the linker
15730 catch branches and loads that are out of range. */
15731 as_bad_where (fragp->fr_file, fragp->fr_line,
15732 _("unsupported PC relative reference to different section"));
15733
15734 return 1;
15735 }
98aa84af
AM
15736 if (fragp != sym_frag && sym_frag->fr_address == 0)
15737 /* Assume non-extended on the first relaxation pass.
15738 The address we have calculated will be bogus if this is
15739 a forward branch to another frag, as the forward frag
15740 will have fr_address == 0. */
15741 return 0;
252b5132
RH
15742 }
15743
15744 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
15745 the same section. If the relax_marker of the symbol fragment
15746 differs from the relax_marker of this fragment, we have not
15747 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
15748 in STRETCH in order to get a better estimate of the address.
15749 This particularly matters because of the shift bits. */
15750 if (stretch != 0
98aa84af 15751 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
15752 {
15753 fragS *f;
15754
15755 /* Adjust stretch for any alignment frag. Note that if have
15756 been expanding the earlier code, the symbol may be
15757 defined in what appears to be an earlier frag. FIXME:
15758 This doesn't handle the fr_subtype field, which specifies
15759 a maximum number of bytes to skip when doing an
15760 alignment. */
98aa84af 15761 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
15762 {
15763 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
15764 {
15765 if (stretch < 0)
15766 stretch = - ((- stretch)
15767 & ~ ((1 << (int) f->fr_offset) - 1));
15768 else
15769 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
15770 if (stretch == 0)
15771 break;
15772 }
15773 }
15774 if (f != NULL)
15775 val += stretch;
15776 }
15777
15778 addr = fragp->fr_address + fragp->fr_fix;
15779
15780 /* The base address rules are complicated. The base address of
15781 a branch is the following instruction. The base address of a
15782 PC relative load or add is the instruction itself, but if it
15783 is in a delay slot (in which case it can not be extended) use
15784 the address of the instruction whose delay slot it is in. */
3ccad066 15785 if (pcrel_op->include_isa_bit)
252b5132
RH
15786 {
15787 addr += 2;
15788
15789 /* If we are currently assuming that this frag should be
15790 extended, then, the current address is two bytes
bdaaa2e1 15791 higher. */
252b5132
RH
15792 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
15793 addr += 2;
15794
15795 /* Ignore the low bit in the target, since it will be set
15796 for a text label. */
3ccad066 15797 val &= -2;
252b5132
RH
15798 }
15799 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
15800 addr -= 4;
15801 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
15802 addr -= 2;
15803
3ccad066 15804 val -= addr & -(1 << pcrel_op->align_log2);
252b5132
RH
15805
15806 /* If any of the shifted bits are set, we must use an extended
15807 opcode. If the address depends on the size of this
15808 instruction, this can lead to a loop, so we arrange to always
15809 use an extended opcode. We only check this when we are in
15810 the main relaxation loop, when SEC is NULL. */
3ccad066 15811 if ((val & ((1 << operand->shift) - 1)) != 0 && sec == NULL)
252b5132
RH
15812 {
15813 fragp->fr_subtype =
15814 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
15815 return 1;
15816 }
15817
15818 /* If we are about to mark a frag as extended because the value
3ccad066
RS
15819 is precisely the next value above maxtiny, then there is a
15820 chance of an infinite loop as in the following code:
252b5132
RH
15821 la $4,foo
15822 .skip 1020
15823 .align 2
15824 foo:
15825 In this case when the la is extended, foo is 0x3fc bytes
15826 away, so the la can be shrunk, but then foo is 0x400 away, so
15827 the la must be extended. To avoid this loop, we mark the
15828 frag as extended if it was small, and is about to become
3ccad066
RS
15829 extended with the next value above maxtiny. */
15830 maxtiny = mips_int_operand_max (operand);
15831 if (val == maxtiny + (1 << operand->shift)
252b5132
RH
15832 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
15833 && sec == NULL)
15834 {
15835 fragp->fr_subtype =
15836 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
15837 return 1;
15838 }
15839 }
15840 else if (symsec != absolute_section && sec != NULL)
15841 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
15842
3ccad066 15843 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
252b5132
RH
15844}
15845
4a6a3df4
AO
15846/* Compute the length of a branch sequence, and adjust the
15847 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
15848 worst-case length is computed, with UPDATE being used to indicate
15849 whether an unconditional (-1), branch-likely (+1) or regular (0)
15850 branch is to be computed. */
15851static int
17a2f251 15852relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 15853{
b34976b6 15854 bfd_boolean toofar;
4a6a3df4
AO
15855 int length;
15856
15857 if (fragp
15858 && S_IS_DEFINED (fragp->fr_symbol)
15859 && sec == S_GET_SEGMENT (fragp->fr_symbol))
15860 {
15861 addressT addr;
15862 offsetT val;
15863
15864 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
15865
15866 addr = fragp->fr_address + fragp->fr_fix + 4;
15867
15868 val -= addr;
15869
15870 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
15871 }
15872 else if (fragp)
15873 /* If the symbol is not defined or it's in a different segment,
15874 assume the user knows what's going on and emit a short
15875 branch. */
b34976b6 15876 toofar = FALSE;
4a6a3df4 15877 else
b34976b6 15878 toofar = TRUE;
4a6a3df4
AO
15879
15880 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
15881 fragp->fr_subtype
66b3e8da
MR
15882 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
15883 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
15884 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
15885 RELAX_BRANCH_LINK (fragp->fr_subtype),
15886 toofar);
15887
15888 length = 4;
15889 if (toofar)
15890 {
15891 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
15892 length += 8;
15893
15894 if (mips_pic != NO_PIC)
15895 {
15896 /* Additional space for PIC loading of target address. */
15897 length += 8;
15898 if (mips_opts.isa == ISA_MIPS1)
15899 /* Additional space for $at-stabilizing nop. */
15900 length += 4;
15901 }
15902
15903 /* If branch is conditional. */
15904 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
15905 length += 8;
15906 }
b34976b6 15907
4a6a3df4
AO
15908 return length;
15909}
15910
df58fc94
RS
15911/* Compute the length of a branch sequence, and adjust the
15912 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
15913 worst-case length is computed, with UPDATE being used to indicate
15914 whether an unconditional (-1), or regular (0) branch is to be
15915 computed. */
15916
15917static int
15918relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
15919{
15920 bfd_boolean toofar;
15921 int length;
15922
15923 if (fragp
15924 && S_IS_DEFINED (fragp->fr_symbol)
15925 && sec == S_GET_SEGMENT (fragp->fr_symbol))
15926 {
15927 addressT addr;
15928 offsetT val;
15929
15930 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
15931 /* Ignore the low bit in the target, since it will be set
15932 for a text label. */
15933 if ((val & 1) != 0)
15934 --val;
15935
15936 addr = fragp->fr_address + fragp->fr_fix + 4;
15937
15938 val -= addr;
15939
15940 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
15941 }
15942 else if (fragp)
15943 /* If the symbol is not defined or it's in a different segment,
15944 assume the user knows what's going on and emit a short
15945 branch. */
15946 toofar = FALSE;
15947 else
15948 toofar = TRUE;
15949
15950 if (fragp && update
15951 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
15952 fragp->fr_subtype = (toofar
15953 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
15954 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
15955
15956 length = 4;
15957 if (toofar)
15958 {
15959 bfd_boolean compact_known = fragp != NULL;
15960 bfd_boolean compact = FALSE;
15961 bfd_boolean uncond;
15962
15963 if (compact_known)
15964 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
15965 if (fragp)
15966 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
15967 else
15968 uncond = update < 0;
15969
15970 /* If label is out of range, we turn branch <br>:
15971
15972 <br> label # 4 bytes
15973 0:
15974
15975 into:
15976
15977 j label # 4 bytes
15978 nop # 2 bytes if compact && !PIC
15979 0:
15980 */
15981 if (mips_pic == NO_PIC && (!compact_known || compact))
15982 length += 2;
15983
15984 /* If assembling PIC code, we further turn:
15985
15986 j label # 4 bytes
15987
15988 into:
15989
15990 lw/ld at, %got(label)(gp) # 4 bytes
15991 d/addiu at, %lo(label) # 4 bytes
15992 jr/c at # 2 bytes
15993 */
15994 if (mips_pic != NO_PIC)
15995 length += 6;
15996
15997 /* If branch <br> is conditional, we prepend negated branch <brneg>:
15998
15999 <brneg> 0f # 4 bytes
16000 nop # 2 bytes if !compact
16001 */
16002 if (!uncond)
16003 length += (compact_known && compact) ? 4 : 6;
16004 }
16005
16006 return length;
16007}
16008
16009/* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
16010 bit accordingly. */
16011
16012static int
16013relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
16014{
16015 bfd_boolean toofar;
16016
df58fc94
RS
16017 if (fragp
16018 && S_IS_DEFINED (fragp->fr_symbol)
16019 && sec == S_GET_SEGMENT (fragp->fr_symbol))
16020 {
16021 addressT addr;
16022 offsetT val;
16023 int type;
16024
16025 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
16026 /* Ignore the low bit in the target, since it will be set
16027 for a text label. */
16028 if ((val & 1) != 0)
16029 --val;
16030
16031 /* Assume this is a 2-byte branch. */
16032 addr = fragp->fr_address + fragp->fr_fix + 2;
16033
16034 /* We try to avoid the infinite loop by not adding 2 more bytes for
16035 long branches. */
16036
16037 val -= addr;
16038
16039 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
16040 if (type == 'D')
16041 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
16042 else if (type == 'E')
16043 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
16044 else
16045 abort ();
16046 }
16047 else
16048 /* If the symbol is not defined or it's in a different segment,
16049 we emit a normal 32-bit branch. */
16050 toofar = TRUE;
16051
16052 if (fragp && update
16053 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
16054 fragp->fr_subtype
16055 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
16056 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
16057
16058 if (toofar)
16059 return 4;
16060
16061 return 2;
16062}
16063
252b5132
RH
16064/* Estimate the size of a frag before relaxing. Unless this is the
16065 mips16, we are not really relaxing here, and the final size is
16066 encoded in the subtype information. For the mips16, we have to
16067 decide whether we are using an extended opcode or not. */
16068
252b5132 16069int
17a2f251 16070md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 16071{
5919d012 16072 int change;
252b5132 16073
4a6a3df4
AO
16074 if (RELAX_BRANCH_P (fragp->fr_subtype))
16075 {
16076
b34976b6
AM
16077 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
16078
4a6a3df4
AO
16079 return fragp->fr_var;
16080 }
16081
252b5132 16082 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
16083 /* We don't want to modify the EXTENDED bit here; it might get us
16084 into infinite loops. We change it only in mips_relax_frag(). */
16085 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132 16086
df58fc94
RS
16087 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
16088 {
16089 int length = 4;
16090
16091 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
16092 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
16093 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
16094 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
16095 fragp->fr_var = length;
16096
16097 return length;
16098 }
16099
252b5132 16100 if (mips_pic == NO_PIC)
5919d012 16101 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132 16102 else if (mips_pic == SVR4_PIC)
5919d012 16103 change = pic_need_relax (fragp->fr_symbol, segtype);
0a44bf69
RS
16104 else if (mips_pic == VXWORKS_PIC)
16105 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
16106 change = 0;
252b5132
RH
16107 else
16108 abort ();
16109
16110 if (change)
16111 {
4d7206a2 16112 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 16113 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 16114 }
4d7206a2
RS
16115 else
16116 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
16117}
16118
16119/* This is called to see whether a reloc against a defined symbol
de7e6852 16120 should be converted into a reloc against a section. */
252b5132
RH
16121
16122int
17a2f251 16123mips_fix_adjustable (fixS *fixp)
252b5132 16124{
252b5132
RH
16125 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
16126 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
16127 return 0;
a161fe53 16128
252b5132
RH
16129 if (fixp->fx_addsy == NULL)
16130 return 1;
a161fe53 16131
de7e6852
RS
16132 /* If symbol SYM is in a mergeable section, relocations of the form
16133 SYM + 0 can usually be made section-relative. The mergeable data
16134 is then identified by the section offset rather than by the symbol.
16135
16136 However, if we're generating REL LO16 relocations, the offset is split
16137 between the LO16 and parterning high part relocation. The linker will
16138 need to recalculate the complete offset in order to correctly identify
16139 the merge data.
16140
16141 The linker has traditionally not looked for the parterning high part
16142 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
16143 placed anywhere. Rather than break backwards compatibility by changing
16144 this, it seems better not to force the issue, and instead keep the
16145 original symbol. This will work with either linker behavior. */
738e5348 16146 if ((lo16_reloc_p (fixp->fx_r_type)
704803a9 16147 || reloc_needs_lo_p (fixp->fx_r_type))
de7e6852
RS
16148 && HAVE_IN_PLACE_ADDENDS
16149 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
16150 return 0;
16151
ce70d90a 16152 /* There is no place to store an in-place offset for JALR relocations.
2de39019
CM
16153 Likewise an in-range offset of limited PC-relative relocations may
16154 overflow the in-place relocatable field if recalculated against the
16155 start address of the symbol's containing section. */
ce70d90a 16156 if (HAVE_IN_PLACE_ADDENDS
2de39019
CM
16157 && (limited_pcrel_reloc_p (fixp->fx_r_type)
16158 || jalr_reloc_p (fixp->fx_r_type)))
1180b5a4
RS
16159 return 0;
16160
b314ec0e
RS
16161 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
16162 to a floating-point stub. The same is true for non-R_MIPS16_26
16163 relocations against MIPS16 functions; in this case, the stub becomes
16164 the function's canonical address.
16165
16166 Floating-point stubs are stored in unique .mips16.call.* or
16167 .mips16.fn.* sections. If a stub T for function F is in section S,
16168 the first relocation in section S must be against F; this is how the
16169 linker determines the target function. All relocations that might
16170 resolve to T must also be against F. We therefore have the following
16171 restrictions, which are given in an intentionally-redundant way:
16172
16173 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
16174 symbols.
16175
16176 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
16177 if that stub might be used.
16178
16179 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
16180 symbols.
16181
16182 4. We cannot reduce a stub's relocations against MIPS16 symbols if
16183 that stub might be used.
16184
16185 There is a further restriction:
16186
df58fc94
RS
16187 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
16188 R_MICROMIPS_26_S1) against MIPS16 or microMIPS symbols on
16189 targets with in-place addends; the relocation field cannot
b314ec0e
RS
16190 encode the low bit.
16191
df58fc94
RS
16192 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
16193 against a MIPS16 symbol. We deal with (5) by by not reducing any
16194 such relocations on REL targets.
b314ec0e
RS
16195
16196 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
16197 relocation against some symbol R, no relocation against R may be
16198 reduced. (Note that this deals with (2) as well as (1) because
16199 relocations against global symbols will never be reduced on ELF
16200 targets.) This approach is a little simpler than trying to detect
16201 stub sections, and gives the "all or nothing" per-symbol consistency
16202 that we have for MIPS16 symbols. */
f3ded42a 16203 if (fixp->fx_subsy == NULL
30c09090 16204 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
df58fc94
RS
16205 || *symbol_get_tc (fixp->fx_addsy)
16206 || (HAVE_IN_PLACE_ADDENDS
16207 && ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
16208 && jmp_reloc_p (fixp->fx_r_type))))
252b5132 16209 return 0;
a161fe53 16210
252b5132
RH
16211 return 1;
16212}
16213
16214/* Translate internal representation of relocation info to BFD target
16215 format. */
16216
16217arelent **
17a2f251 16218tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
16219{
16220 static arelent *retval[4];
16221 arelent *reloc;
16222 bfd_reloc_code_real_type code;
16223
4b0cff4e
TS
16224 memset (retval, 0, sizeof(retval));
16225 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
49309057
ILT
16226 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
16227 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
16228 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
16229
bad36eac
DJ
16230 if (fixp->fx_pcrel)
16231 {
df58fc94
RS
16232 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
16233 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
16234 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
b47468a6
CM
16235 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
16236 || fixp->fx_r_type == BFD_RELOC_32_PCREL);
bad36eac
DJ
16237
16238 /* At this point, fx_addnumber is "symbol offset - pcrel address".
16239 Relocations want only the symbol offset. */
16240 reloc->addend = fixp->fx_addnumber + reloc->address;
bad36eac
DJ
16241 }
16242 else
16243 reloc->addend = fixp->fx_addnumber;
252b5132 16244
438c16b8
TS
16245 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
16246 entry to be used in the relocation's section offset. */
16247 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
16248 {
16249 reloc->address = reloc->addend;
16250 reloc->addend = 0;
16251 }
16252
252b5132 16253 code = fixp->fx_r_type;
252b5132 16254
bad36eac 16255 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
252b5132
RH
16256 if (reloc->howto == NULL)
16257 {
16258 as_bad_where (fixp->fx_file, fixp->fx_line,
16259 _("Can not represent %s relocation in this object file format"),
16260 bfd_get_reloc_code_name (code));
16261 retval[0] = NULL;
16262 }
16263
16264 return retval;
16265}
16266
16267/* Relax a machine dependent frag. This returns the amount by which
16268 the current size of the frag should change. */
16269
16270int
17a2f251 16271mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 16272{
4a6a3df4
AO
16273 if (RELAX_BRANCH_P (fragp->fr_subtype))
16274 {
16275 offsetT old_var = fragp->fr_var;
b34976b6
AM
16276
16277 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
16278
16279 return fragp->fr_var - old_var;
16280 }
16281
df58fc94
RS
16282 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
16283 {
16284 offsetT old_var = fragp->fr_var;
16285 offsetT new_var = 4;
16286
16287 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
16288 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
16289 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
16290 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
16291 fragp->fr_var = new_var;
16292
16293 return new_var - old_var;
16294 }
16295
252b5132
RH
16296 if (! RELAX_MIPS16_P (fragp->fr_subtype))
16297 return 0;
16298
c4e7957c 16299 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
16300 {
16301 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
16302 return 0;
16303 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
16304 return 2;
16305 }
16306 else
16307 {
16308 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
16309 return 0;
16310 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
16311 return -2;
16312 }
16313
16314 return 0;
16315}
16316
16317/* Convert a machine dependent frag. */
16318
16319void
17a2f251 16320md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 16321{
4a6a3df4
AO
16322 if (RELAX_BRANCH_P (fragp->fr_subtype))
16323 {
4d68580a 16324 char *buf;
4a6a3df4
AO
16325 unsigned long insn;
16326 expressionS exp;
16327 fixS *fixp;
b34976b6 16328
4d68580a
RS
16329 buf = fragp->fr_literal + fragp->fr_fix;
16330 insn = read_insn (buf);
b34976b6 16331
4a6a3df4
AO
16332 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
16333 {
16334 /* We generate a fixup instead of applying it right now
16335 because, if there are linker relaxations, we're going to
16336 need the relocations. */
16337 exp.X_op = O_symbol;
16338 exp.X_add_symbol = fragp->fr_symbol;
16339 exp.X_add_number = fragp->fr_offset;
16340
4d68580a
RS
16341 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
16342 BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
16343 fixp->fx_file = fragp->fr_file;
16344 fixp->fx_line = fragp->fr_line;
b34976b6 16345
4d68580a 16346 buf = write_insn (buf, insn);
4a6a3df4
AO
16347 }
16348 else
16349 {
16350 int i;
16351
16352 as_warn_where (fragp->fr_file, fragp->fr_line,
5c4f07ba 16353 _("Relaxed out-of-range branch into a jump"));
4a6a3df4
AO
16354
16355 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
16356 goto uncond;
16357
16358 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
16359 {
16360 /* Reverse the branch. */
16361 switch ((insn >> 28) & 0xf)
16362 {
16363 case 4:
3bf0dbfb
MR
16364 /* bc[0-3][tf]l? instructions can have the condition
16365 reversed by tweaking a single TF bit, and their
16366 opcodes all have 0x4???????. */
16367 gas_assert ((insn & 0xf3e00000) == 0x41000000);
4a6a3df4
AO
16368 insn ^= 0x00010000;
16369 break;
16370
16371 case 0:
16372 /* bltz 0x04000000 bgez 0x04010000
54f4ddb3 16373 bltzal 0x04100000 bgezal 0x04110000 */
9c2799c2 16374 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
4a6a3df4
AO
16375 insn ^= 0x00010000;
16376 break;
b34976b6 16377
4a6a3df4
AO
16378 case 1:
16379 /* beq 0x10000000 bne 0x14000000
54f4ddb3 16380 blez 0x18000000 bgtz 0x1c000000 */
4a6a3df4
AO
16381 insn ^= 0x04000000;
16382 break;
16383
16384 default:
16385 abort ();
16386 }
16387 }
16388
16389 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
16390 {
16391 /* Clear the and-link bit. */
9c2799c2 16392 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
4a6a3df4 16393
54f4ddb3
TS
16394 /* bltzal 0x04100000 bgezal 0x04110000
16395 bltzall 0x04120000 bgezall 0x04130000 */
4a6a3df4
AO
16396 insn &= ~0x00100000;
16397 }
16398
16399 /* Branch over the branch (if the branch was likely) or the
16400 full jump (not likely case). Compute the offset from the
16401 current instruction to branch to. */
16402 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
16403 i = 16;
16404 else
16405 {
16406 /* How many bytes in instructions we've already emitted? */
4d68580a 16407 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
16408 /* How many bytes in instructions from here to the end? */
16409 i = fragp->fr_var - i;
16410 }
16411 /* Convert to instruction count. */
16412 i >>= 2;
16413 /* Branch counts from the next instruction. */
b34976b6 16414 i--;
4a6a3df4
AO
16415 insn |= i;
16416 /* Branch over the jump. */
4d68580a 16417 buf = write_insn (buf, insn);
4a6a3df4 16418
54f4ddb3 16419 /* nop */
4d68580a 16420 buf = write_insn (buf, 0);
4a6a3df4
AO
16421
16422 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
16423 {
16424 /* beql $0, $0, 2f */
16425 insn = 0x50000000;
16426 /* Compute the PC offset from the current instruction to
16427 the end of the variable frag. */
16428 /* How many bytes in instructions we've already emitted? */
4d68580a 16429 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
16430 /* How many bytes in instructions from here to the end? */
16431 i = fragp->fr_var - i;
16432 /* Convert to instruction count. */
16433 i >>= 2;
16434 /* Don't decrement i, because we want to branch over the
16435 delay slot. */
4a6a3df4 16436 insn |= i;
4a6a3df4 16437
4d68580a
RS
16438 buf = write_insn (buf, insn);
16439 buf = write_insn (buf, 0);
4a6a3df4
AO
16440 }
16441
16442 uncond:
16443 if (mips_pic == NO_PIC)
16444 {
16445 /* j or jal. */
16446 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
16447 ? 0x0c000000 : 0x08000000);
16448 exp.X_op = O_symbol;
16449 exp.X_add_symbol = fragp->fr_symbol;
16450 exp.X_add_number = fragp->fr_offset;
16451
4d68580a
RS
16452 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
16453 FALSE, BFD_RELOC_MIPS_JMP);
4a6a3df4
AO
16454 fixp->fx_file = fragp->fr_file;
16455 fixp->fx_line = fragp->fr_line;
16456
4d68580a 16457 buf = write_insn (buf, insn);
4a6a3df4
AO
16458 }
16459 else
16460 {
66b3e8da
MR
16461 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
16462
4a6a3df4 16463 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
66b3e8da
MR
16464 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
16465 insn |= at << OP_SH_RT;
4a6a3df4
AO
16466 exp.X_op = O_symbol;
16467 exp.X_add_symbol = fragp->fr_symbol;
16468 exp.X_add_number = fragp->fr_offset;
16469
16470 if (fragp->fr_offset)
16471 {
16472 exp.X_add_symbol = make_expr_symbol (&exp);
16473 exp.X_add_number = 0;
16474 }
16475
4d68580a
RS
16476 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
16477 FALSE, BFD_RELOC_MIPS_GOT16);
4a6a3df4
AO
16478 fixp->fx_file = fragp->fr_file;
16479 fixp->fx_line = fragp->fr_line;
16480
4d68580a 16481 buf = write_insn (buf, insn);
b34976b6 16482
4a6a3df4 16483 if (mips_opts.isa == ISA_MIPS1)
4d68580a
RS
16484 /* nop */
16485 buf = write_insn (buf, 0);
4a6a3df4
AO
16486
16487 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
66b3e8da
MR
16488 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
16489 insn |= at << OP_SH_RS | at << OP_SH_RT;
4a6a3df4 16490
4d68580a
RS
16491 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
16492 FALSE, BFD_RELOC_LO16);
4a6a3df4
AO
16493 fixp->fx_file = fragp->fr_file;
16494 fixp->fx_line = fragp->fr_line;
b34976b6 16495
4d68580a 16496 buf = write_insn (buf, insn);
4a6a3df4
AO
16497
16498 /* j(al)r $at. */
16499 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
66b3e8da 16500 insn = 0x0000f809;
4a6a3df4 16501 else
66b3e8da
MR
16502 insn = 0x00000008;
16503 insn |= at << OP_SH_RS;
4a6a3df4 16504
4d68580a 16505 buf = write_insn (buf, insn);
4a6a3df4
AO
16506 }
16507 }
16508
4a6a3df4 16509 fragp->fr_fix += fragp->fr_var;
4d68580a 16510 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
4a6a3df4
AO
16511 return;
16512 }
16513
df58fc94
RS
16514 /* Relax microMIPS branches. */
16515 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
16516 {
4d68580a 16517 char *buf = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
16518 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
16519 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
16520 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
2309ddf2 16521 bfd_boolean short_ds;
df58fc94
RS
16522 unsigned long insn;
16523 expressionS exp;
16524 fixS *fixp;
16525
16526 exp.X_op = O_symbol;
16527 exp.X_add_symbol = fragp->fr_symbol;
16528 exp.X_add_number = fragp->fr_offset;
16529
16530 fragp->fr_fix += fragp->fr_var;
16531
16532 /* Handle 16-bit branches that fit or are forced to fit. */
16533 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
16534 {
16535 /* We generate a fixup instead of applying it right now,
16536 because if there is linker relaxation, we're going to
16537 need the relocations. */
16538 if (type == 'D')
4d68580a 16539 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
df58fc94
RS
16540 BFD_RELOC_MICROMIPS_10_PCREL_S1);
16541 else if (type == 'E')
4d68580a 16542 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
df58fc94
RS
16543 BFD_RELOC_MICROMIPS_7_PCREL_S1);
16544 else
16545 abort ();
16546
16547 fixp->fx_file = fragp->fr_file;
16548 fixp->fx_line = fragp->fr_line;
16549
16550 /* These relocations can have an addend that won't fit in
16551 2 octets. */
16552 fixp->fx_no_overflow = 1;
16553
16554 return;
16555 }
16556
2309ddf2 16557 /* Handle 32-bit branches that fit or are forced to fit. */
df58fc94
RS
16558 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
16559 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
16560 {
16561 /* We generate a fixup instead of applying it right now,
16562 because if there is linker relaxation, we're going to
16563 need the relocations. */
4d68580a
RS
16564 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
16565 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
16566 fixp->fx_file = fragp->fr_file;
16567 fixp->fx_line = fragp->fr_line;
16568
16569 if (type == 0)
16570 return;
16571 }
16572
16573 /* Relax 16-bit branches to 32-bit branches. */
16574 if (type != 0)
16575 {
4d68580a 16576 insn = read_compressed_insn (buf, 2);
df58fc94
RS
16577
16578 if ((insn & 0xfc00) == 0xcc00) /* b16 */
16579 insn = 0x94000000; /* beq */
16580 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
16581 {
16582 unsigned long regno;
16583
16584 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
16585 regno = micromips_to_32_reg_d_map [regno];
16586 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
16587 insn |= regno << MICROMIPSOP_SH_RS;
16588 }
16589 else
16590 abort ();
16591
16592 /* Nothing else to do, just write it out. */
16593 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
16594 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
16595 {
4d68580a
RS
16596 buf = write_compressed_insn (buf, insn, 4);
16597 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
16598 return;
16599 }
16600 }
16601 else
4d68580a 16602 insn = read_compressed_insn (buf, 4);
df58fc94
RS
16603
16604 /* Relax 32-bit branches to a sequence of instructions. */
16605 as_warn_where (fragp->fr_file, fragp->fr_line,
16606 _("Relaxed out-of-range branch into a jump"));
16607
2309ddf2
MR
16608 /* Set the short-delay-slot bit. */
16609 short_ds = al && (insn & 0x02000000) != 0;
df58fc94
RS
16610
16611 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
16612 {
16613 symbolS *l;
16614
16615 /* Reverse the branch. */
16616 if ((insn & 0xfc000000) == 0x94000000 /* beq */
16617 || (insn & 0xfc000000) == 0xb4000000) /* bne */
16618 insn ^= 0x20000000;
16619 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
16620 || (insn & 0xffe00000) == 0x40400000 /* bgez */
16621 || (insn & 0xffe00000) == 0x40800000 /* blez */
16622 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
16623 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
16624 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
16625 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
16626 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
16627 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
16628 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
16629 insn ^= 0x00400000;
16630 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
16631 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
16632 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
16633 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
16634 insn ^= 0x00200000;
16635 else
16636 abort ();
16637
16638 if (al)
16639 {
16640 /* Clear the and-link and short-delay-slot bits. */
16641 gas_assert ((insn & 0xfda00000) == 0x40200000);
16642
16643 /* bltzal 0x40200000 bgezal 0x40600000 */
16644 /* bltzals 0x42200000 bgezals 0x42600000 */
16645 insn &= ~0x02200000;
16646 }
16647
16648 /* Make a label at the end for use with the branch. */
16649 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
16650 micromips_label_inc ();
f3ded42a 16651 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
df58fc94
RS
16652
16653 /* Refer to it. */
4d68580a
RS
16654 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
16655 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
16656 fixp->fx_file = fragp->fr_file;
16657 fixp->fx_line = fragp->fr_line;
16658
16659 /* Branch over the jump. */
4d68580a 16660 buf = write_compressed_insn (buf, insn, 4);
df58fc94 16661 if (!compact)
4d68580a
RS
16662 /* nop */
16663 buf = write_compressed_insn (buf, 0x0c00, 2);
df58fc94
RS
16664 }
16665
16666 if (mips_pic == NO_PIC)
16667 {
2309ddf2
MR
16668 unsigned long jal = short_ds ? 0x74000000 : 0xf4000000; /* jal/s */
16669
df58fc94
RS
16670 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
16671 insn = al ? jal : 0xd4000000;
16672
4d68580a
RS
16673 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
16674 BFD_RELOC_MICROMIPS_JMP);
df58fc94
RS
16675 fixp->fx_file = fragp->fr_file;
16676 fixp->fx_line = fragp->fr_line;
16677
4d68580a 16678 buf = write_compressed_insn (buf, insn, 4);
df58fc94 16679 if (compact)
4d68580a
RS
16680 /* nop */
16681 buf = write_compressed_insn (buf, 0x0c00, 2);
df58fc94
RS
16682 }
16683 else
16684 {
16685 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
2309ddf2
MR
16686 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
16687 unsigned long jr = compact ? 0x45a0 : 0x4580; /* jr/c */
df58fc94
RS
16688
16689 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
16690 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
16691 insn |= at << MICROMIPSOP_SH_RT;
16692
16693 if (exp.X_add_number)
16694 {
16695 exp.X_add_symbol = make_expr_symbol (&exp);
16696 exp.X_add_number = 0;
16697 }
16698
4d68580a
RS
16699 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
16700 BFD_RELOC_MICROMIPS_GOT16);
df58fc94
RS
16701 fixp->fx_file = fragp->fr_file;
16702 fixp->fx_line = fragp->fr_line;
16703
4d68580a 16704 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
16705
16706 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
16707 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
16708 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
16709
4d68580a
RS
16710 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
16711 BFD_RELOC_MICROMIPS_LO16);
df58fc94
RS
16712 fixp->fx_file = fragp->fr_file;
16713 fixp->fx_line = fragp->fr_line;
16714
4d68580a 16715 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
16716
16717 /* jr/jrc/jalr/jalrs $at */
16718 insn = al ? jalr : jr;
16719 insn |= at << MICROMIPSOP_SH_MJ;
16720
4d68580a 16721 buf = write_compressed_insn (buf, insn, 2);
df58fc94
RS
16722 }
16723
4d68580a 16724 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
16725 return;
16726 }
16727
252b5132
RH
16728 if (RELAX_MIPS16_P (fragp->fr_subtype))
16729 {
16730 int type;
3ccad066 16731 const struct mips_int_operand *operand;
252b5132 16732 offsetT val;
5c04167a
RS
16733 char *buf;
16734 unsigned int user_length, length;
252b5132 16735 unsigned long insn;
5c04167a 16736 bfd_boolean ext;
252b5132
RH
16737
16738 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
3ccad066 16739 operand = mips16_immed_operand (type, FALSE);
252b5132 16740
5c04167a 16741 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
5f5f22c0 16742 val = resolve_symbol_value (fragp->fr_symbol);
3ccad066 16743 if (operand->root.type == OP_PCREL)
252b5132 16744 {
3ccad066 16745 const struct mips_pcrel_operand *pcrel_op;
252b5132
RH
16746 addressT addr;
16747
3ccad066 16748 pcrel_op = (const struct mips_pcrel_operand *) operand;
252b5132
RH
16749 addr = fragp->fr_address + fragp->fr_fix;
16750
16751 /* The rules for the base address of a PC relative reloc are
16752 complicated; see mips16_extended_frag. */
3ccad066 16753 if (pcrel_op->include_isa_bit)
252b5132
RH
16754 {
16755 addr += 2;
16756 if (ext)
16757 addr += 2;
16758 /* Ignore the low bit in the target, since it will be
16759 set for a text label. */
3ccad066 16760 val &= -2;
252b5132
RH
16761 }
16762 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
16763 addr -= 4;
16764 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
16765 addr -= 2;
16766
3ccad066 16767 addr &= -(1 << pcrel_op->align_log2);
252b5132
RH
16768 val -= addr;
16769
16770 /* Make sure the section winds up with the alignment we have
16771 assumed. */
3ccad066
RS
16772 if (operand->shift > 0)
16773 record_alignment (asec, operand->shift);
252b5132
RH
16774 }
16775
16776 if (ext
16777 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
16778 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
16779 as_warn_where (fragp->fr_file, fragp->fr_line,
16780 _("extended instruction in delay slot"));
16781
5c04167a 16782 buf = fragp->fr_literal + fragp->fr_fix;
252b5132 16783
4d68580a 16784 insn = read_compressed_insn (buf, 2);
5c04167a
RS
16785 if (ext)
16786 insn |= MIPS16_EXTEND;
252b5132 16787
5c04167a
RS
16788 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
16789 user_length = 4;
16790 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
16791 user_length = 2;
16792 else
16793 user_length = 0;
16794
43c0598f 16795 mips16_immed (fragp->fr_file, fragp->fr_line, type,
c150d1d2 16796 BFD_RELOC_UNUSED, val, user_length, &insn);
252b5132 16797
5c04167a
RS
16798 length = (ext ? 4 : 2);
16799 gas_assert (mips16_opcode_length (insn) == length);
16800 write_compressed_insn (buf, insn, length);
16801 fragp->fr_fix += length;
252b5132
RH
16802 }
16803 else
16804 {
df58fc94
RS
16805 relax_substateT subtype = fragp->fr_subtype;
16806 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
16807 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
4d7206a2
RS
16808 int first, second;
16809 fixS *fixp;
252b5132 16810
df58fc94
RS
16811 first = RELAX_FIRST (subtype);
16812 second = RELAX_SECOND (subtype);
4d7206a2 16813 fixp = (fixS *) fragp->fr_opcode;
252b5132 16814
df58fc94
RS
16815 /* If the delay slot chosen does not match the size of the instruction,
16816 then emit a warning. */
16817 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
16818 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
16819 {
16820 relax_substateT s;
16821 const char *msg;
16822
16823 s = subtype & (RELAX_DELAY_SLOT_16BIT
16824 | RELAX_DELAY_SLOT_SIZE_FIRST
16825 | RELAX_DELAY_SLOT_SIZE_SECOND);
16826 msg = macro_warning (s);
16827 if (msg != NULL)
db9b2be4 16828 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94
RS
16829 subtype &= ~s;
16830 }
16831
584892a6 16832 /* Possibly emit a warning if we've chosen the longer option. */
df58fc94 16833 if (use_second == second_longer)
584892a6 16834 {
df58fc94
RS
16835 relax_substateT s;
16836 const char *msg;
16837
16838 s = (subtype
16839 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
16840 msg = macro_warning (s);
16841 if (msg != NULL)
db9b2be4 16842 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94 16843 subtype &= ~s;
584892a6
RS
16844 }
16845
4d7206a2
RS
16846 /* Go through all the fixups for the first sequence. Disable them
16847 (by marking them as done) if we're going to use the second
16848 sequence instead. */
16849 while (fixp
16850 && fixp->fx_frag == fragp
16851 && fixp->fx_where < fragp->fr_fix - second)
16852 {
df58fc94 16853 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
16854 fixp->fx_done = 1;
16855 fixp = fixp->fx_next;
16856 }
252b5132 16857
4d7206a2
RS
16858 /* Go through the fixups for the second sequence. Disable them if
16859 we're going to use the first sequence, otherwise adjust their
16860 addresses to account for the relaxation. */
16861 while (fixp && fixp->fx_frag == fragp)
16862 {
df58fc94 16863 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
16864 fixp->fx_where -= first;
16865 else
16866 fixp->fx_done = 1;
16867 fixp = fixp->fx_next;
16868 }
16869
16870 /* Now modify the frag contents. */
df58fc94 16871 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
16872 {
16873 char *start;
16874
16875 start = fragp->fr_literal + fragp->fr_fix - first - second;
16876 memmove (start, start + first, second);
16877 fragp->fr_fix -= first;
16878 }
16879 else
16880 fragp->fr_fix -= second;
252b5132
RH
16881 }
16882}
16883
252b5132
RH
16884/* This function is called after the relocs have been generated.
16885 We've been storing mips16 text labels as odd. Here we convert them
16886 back to even for the convenience of the debugger. */
16887
16888void
17a2f251 16889mips_frob_file_after_relocs (void)
252b5132
RH
16890{
16891 asymbol **syms;
16892 unsigned int count, i;
16893
252b5132
RH
16894 syms = bfd_get_outsymbols (stdoutput);
16895 count = bfd_get_symcount (stdoutput);
16896 for (i = 0; i < count; i++, syms++)
df58fc94
RS
16897 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
16898 && ((*syms)->value & 1) != 0)
16899 {
16900 (*syms)->value &= ~1;
16901 /* If the symbol has an odd size, it was probably computed
16902 incorrectly, so adjust that as well. */
16903 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
16904 ++elf_symbol (*syms)->internal_elf_sym.st_size;
16905 }
252b5132
RH
16906}
16907
a1facbec
MR
16908/* This function is called whenever a label is defined, including fake
16909 labels instantiated off the dot special symbol. It is used when
16910 handling branch delays; if a branch has a label, we assume we cannot
16911 move it. This also bumps the value of the symbol by 1 in compressed
16912 code. */
252b5132 16913
e1b47bd5 16914static void
a1facbec 16915mips_record_label (symbolS *sym)
252b5132 16916{
a8dbcb85 16917 segment_info_type *si = seg_info (now_seg);
252b5132
RH
16918 struct insn_label_list *l;
16919
16920 if (free_insn_labels == NULL)
16921 l = (struct insn_label_list *) xmalloc (sizeof *l);
16922 else
16923 {
16924 l = free_insn_labels;
16925 free_insn_labels = l->next;
16926 }
16927
16928 l->label = sym;
a8dbcb85
TS
16929 l->next = si->label_list;
16930 si->label_list = l;
a1facbec 16931}
07a53e5c 16932
a1facbec
MR
16933/* This function is called as tc_frob_label() whenever a label is defined
16934 and adds a DWARF-2 record we only want for true labels. */
16935
16936void
16937mips_define_label (symbolS *sym)
16938{
16939 mips_record_label (sym);
07a53e5c 16940 dwarf2_emit_label (sym);
252b5132 16941}
e1b47bd5
RS
16942
16943/* This function is called by tc_new_dot_label whenever a new dot symbol
16944 is defined. */
16945
16946void
16947mips_add_dot_label (symbolS *sym)
16948{
16949 mips_record_label (sym);
16950 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
16951 mips_compressed_mark_label (sym);
16952}
252b5132 16953\f
252b5132
RH
16954/* Some special processing for a MIPS ELF file. */
16955
16956void
17a2f251 16957mips_elf_final_processing (void)
252b5132
RH
16958{
16959 /* Write out the register information. */
316f5878 16960 if (mips_abi != N64_ABI)
252b5132
RH
16961 {
16962 Elf32_RegInfo s;
16963
16964 s.ri_gprmask = mips_gprmask;
16965 s.ri_cprmask[0] = mips_cprmask[0];
16966 s.ri_cprmask[1] = mips_cprmask[1];
16967 s.ri_cprmask[2] = mips_cprmask[2];
16968 s.ri_cprmask[3] = mips_cprmask[3];
16969 /* The gp_value field is set by the MIPS ELF backend. */
16970
16971 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
16972 ((Elf32_External_RegInfo *)
16973 mips_regmask_frag));
16974 }
16975 else
16976 {
16977 Elf64_Internal_RegInfo s;
16978
16979 s.ri_gprmask = mips_gprmask;
16980 s.ri_pad = 0;
16981 s.ri_cprmask[0] = mips_cprmask[0];
16982 s.ri_cprmask[1] = mips_cprmask[1];
16983 s.ri_cprmask[2] = mips_cprmask[2];
16984 s.ri_cprmask[3] = mips_cprmask[3];
16985 /* The gp_value field is set by the MIPS ELF backend. */
16986
16987 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
16988 ((Elf64_External_RegInfo *)
16989 mips_regmask_frag));
16990 }
16991
16992 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
16993 sort of BFD interface for this. */
16994 if (mips_any_noreorder)
16995 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
16996 if (mips_pic != NO_PIC)
143d77c5 16997 {
8b828383 16998 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
16999 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
17000 }
17001 if (mips_abicalls)
17002 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 17003
b015e599
AP
17004 /* Set MIPS ELF flags for ASEs. Note that not all ASEs have flags
17005 defined at present; this might need to change in future. */
a4672219
TS
17006 if (file_ase_mips16)
17007 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
df58fc94
RS
17008 if (file_ase_micromips)
17009 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
846ef2d0 17010 if (file_ase & ASE_MDMX)
deec1734 17011 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 17012
bdaaa2e1 17013 /* Set the MIPS ELF ABI flags. */
316f5878 17014 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 17015 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 17016 else if (mips_abi == O64_ABI)
252b5132 17017 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 17018 else if (mips_abi == EABI_ABI)
252b5132 17019 {
316f5878 17020 if (!file_mips_gp32)
252b5132
RH
17021 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
17022 else
17023 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
17024 }
316f5878 17025 else if (mips_abi == N32_ABI)
be00bddd
TS
17026 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
17027
c9914766 17028 /* Nothing to do for N64_ABI. */
252b5132
RH
17029
17030 if (mips_32bitmode)
17031 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
ad3fea08 17032
ba92f887
MR
17033 if (mips_flag_nan2008)
17034 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NAN2008;
17035
ad3fea08
TS
17036#if 0 /* XXX FIXME */
17037 /* 32 bit code with 64 bit FP registers. */
17038 if (!file_mips_fp32 && ABI_NEEDS_32BIT_REGS (mips_abi))
17039 elf_elfheader (stdoutput)->e_flags |= ???;
17040#endif
252b5132 17041}
252b5132 17042\f
beae10d5 17043typedef struct proc {
9b2f1d35
EC
17044 symbolS *func_sym;
17045 symbolS *func_end_sym;
beae10d5
KH
17046 unsigned long reg_mask;
17047 unsigned long reg_offset;
17048 unsigned long fpreg_mask;
17049 unsigned long fpreg_offset;
17050 unsigned long frame_offset;
17051 unsigned long frame_reg;
17052 unsigned long pc_reg;
17053} procS;
252b5132
RH
17054
17055static procS cur_proc;
17056static procS *cur_proc_ptr;
17057static int numprocs;
17058
df58fc94
RS
17059/* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
17060 as "2", and a normal nop as "0". */
17061
17062#define NOP_OPCODE_MIPS 0
17063#define NOP_OPCODE_MIPS16 1
17064#define NOP_OPCODE_MICROMIPS 2
742a56fe
RS
17065
17066char
17067mips_nop_opcode (void)
17068{
df58fc94
RS
17069 if (seg_info (now_seg)->tc_segment_info_data.micromips)
17070 return NOP_OPCODE_MICROMIPS;
17071 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
17072 return NOP_OPCODE_MIPS16;
17073 else
17074 return NOP_OPCODE_MIPS;
742a56fe
RS
17075}
17076
df58fc94
RS
17077/* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
17078 32-bit microMIPS NOPs here (if applicable). */
a19d8eb0 17079
0a9ef439 17080void
17a2f251 17081mips_handle_align (fragS *fragp)
a19d8eb0 17082{
df58fc94 17083 char nop_opcode;
742a56fe 17084 char *p;
c67a084a
NC
17085 int bytes, size, excess;
17086 valueT opcode;
742a56fe 17087
0a9ef439
RH
17088 if (fragp->fr_type != rs_align_code)
17089 return;
17090
742a56fe 17091 p = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
17092 nop_opcode = *p;
17093 switch (nop_opcode)
a19d8eb0 17094 {
df58fc94
RS
17095 case NOP_OPCODE_MICROMIPS:
17096 opcode = micromips_nop32_insn.insn_opcode;
17097 size = 4;
17098 break;
17099 case NOP_OPCODE_MIPS16:
c67a084a
NC
17100 opcode = mips16_nop_insn.insn_opcode;
17101 size = 2;
df58fc94
RS
17102 break;
17103 case NOP_OPCODE_MIPS:
17104 default:
c67a084a
NC
17105 opcode = nop_insn.insn_opcode;
17106 size = 4;
df58fc94 17107 break;
c67a084a 17108 }
a19d8eb0 17109
c67a084a
NC
17110 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
17111 excess = bytes % size;
df58fc94
RS
17112
17113 /* Handle the leading part if we're not inserting a whole number of
17114 instructions, and make it the end of the fixed part of the frag.
17115 Try to fit in a short microMIPS NOP if applicable and possible,
17116 and use zeroes otherwise. */
17117 gas_assert (excess < 4);
17118 fragp->fr_fix += excess;
17119 switch (excess)
c67a084a 17120 {
df58fc94
RS
17121 case 3:
17122 *p++ = '\0';
17123 /* Fall through. */
17124 case 2:
833794fc 17125 if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
df58fc94 17126 {
4d68580a 17127 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
df58fc94
RS
17128 break;
17129 }
17130 *p++ = '\0';
17131 /* Fall through. */
17132 case 1:
17133 *p++ = '\0';
17134 /* Fall through. */
17135 case 0:
17136 break;
a19d8eb0 17137 }
c67a084a
NC
17138
17139 md_number_to_chars (p, opcode, size);
17140 fragp->fr_var = size;
a19d8eb0
CP
17141}
17142
252b5132 17143static void
17a2f251 17144md_obj_begin (void)
252b5132
RH
17145{
17146}
17147
17148static void
17a2f251 17149md_obj_end (void)
252b5132 17150{
54f4ddb3 17151 /* Check for premature end, nesting errors, etc. */
252b5132 17152 if (cur_proc_ptr)
9a41af64 17153 as_warn (_("missing .end at end of assembly"));
252b5132
RH
17154}
17155
17156static long
17a2f251 17157get_number (void)
252b5132
RH
17158{
17159 int negative = 0;
17160 long val = 0;
17161
17162 if (*input_line_pointer == '-')
17163 {
17164 ++input_line_pointer;
17165 negative = 1;
17166 }
3882b010 17167 if (!ISDIGIT (*input_line_pointer))
956cd1d6 17168 as_bad (_("expected simple number"));
252b5132
RH
17169 if (input_line_pointer[0] == '0')
17170 {
17171 if (input_line_pointer[1] == 'x')
17172 {
17173 input_line_pointer += 2;
3882b010 17174 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
17175 {
17176 val <<= 4;
17177 val |= hex_value (*input_line_pointer++);
17178 }
17179 return negative ? -val : val;
17180 }
17181 else
17182 {
17183 ++input_line_pointer;
3882b010 17184 while (ISDIGIT (*input_line_pointer))
252b5132
RH
17185 {
17186 val <<= 3;
17187 val |= *input_line_pointer++ - '0';
17188 }
17189 return negative ? -val : val;
17190 }
17191 }
3882b010 17192 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
17193 {
17194 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
17195 *input_line_pointer, *input_line_pointer);
956cd1d6 17196 as_warn (_("invalid number"));
252b5132
RH
17197 return -1;
17198 }
3882b010 17199 while (ISDIGIT (*input_line_pointer))
252b5132
RH
17200 {
17201 val *= 10;
17202 val += *input_line_pointer++ - '0';
17203 }
17204 return negative ? -val : val;
17205}
17206
17207/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
17208 is an initial number which is the ECOFF file index. In the non-ECOFF
17209 case .file implies DWARF-2. */
17210
17211static void
17a2f251 17212s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 17213{
ecb4347a
DJ
17214 static int first_file_directive = 0;
17215
c5dd6aab
DJ
17216 if (ECOFF_DEBUGGING)
17217 {
17218 get_number ();
17219 s_app_file (0);
17220 }
17221 else
ecb4347a
DJ
17222 {
17223 char *filename;
17224
17225 filename = dwarf2_directive_file (0);
17226
17227 /* Versions of GCC up to 3.1 start files with a ".file"
17228 directive even for stabs output. Make sure that this
17229 ".file" is handled. Note that you need a version of GCC
17230 after 3.1 in order to support DWARF-2 on MIPS. */
17231 if (filename != NULL && ! first_file_directive)
17232 {
17233 (void) new_logical_line (filename, -1);
c04f5787 17234 s_app_file_string (filename, 0);
ecb4347a
DJ
17235 }
17236 first_file_directive = 1;
17237 }
c5dd6aab
DJ
17238}
17239
17240/* The .loc directive, implying DWARF-2. */
252b5132
RH
17241
17242static void
17a2f251 17243s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 17244{
c5dd6aab
DJ
17245 if (!ECOFF_DEBUGGING)
17246 dwarf2_directive_loc (0);
252b5132
RH
17247}
17248
252b5132
RH
17249/* The .end directive. */
17250
17251static void
17a2f251 17252s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
17253{
17254 symbolS *p;
252b5132 17255
7a621144
DJ
17256 /* Following functions need their own .frame and .cprestore directives. */
17257 mips_frame_reg_valid = 0;
17258 mips_cprestore_valid = 0;
17259
252b5132
RH
17260 if (!is_end_of_line[(unsigned char) *input_line_pointer])
17261 {
17262 p = get_symbol ();
17263 demand_empty_rest_of_line ();
17264 }
17265 else
17266 p = NULL;
17267
14949570 17268 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
17269 as_warn (_(".end not in text section"));
17270
17271 if (!cur_proc_ptr)
17272 {
17273 as_warn (_(".end directive without a preceding .ent directive."));
17274 demand_empty_rest_of_line ();
17275 return;
17276 }
17277
17278 if (p != NULL)
17279 {
9c2799c2 17280 gas_assert (S_GET_NAME (p));
9b2f1d35 17281 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
252b5132 17282 as_warn (_(".end symbol does not match .ent symbol."));
ecb4347a
DJ
17283
17284 if (debug_type == DEBUG_STABS)
17285 stabs_generate_asm_endfunc (S_GET_NAME (p),
17286 S_GET_NAME (p));
252b5132
RH
17287 }
17288 else
17289 as_warn (_(".end directive missing or unknown symbol"));
17290
9b2f1d35
EC
17291 /* Create an expression to calculate the size of the function. */
17292 if (p && cur_proc_ptr)
17293 {
17294 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
17295 expressionS *exp = xmalloc (sizeof (expressionS));
17296
17297 obj->size = exp;
17298 exp->X_op = O_subtract;
17299 exp->X_add_symbol = symbol_temp_new_now ();
17300 exp->X_op_symbol = p;
17301 exp->X_add_number = 0;
17302
17303 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
17304 }
17305
ecb4347a 17306 /* Generate a .pdr section. */
f3ded42a 17307 if (!ECOFF_DEBUGGING && mips_flag_pdr)
ecb4347a
DJ
17308 {
17309 segT saved_seg = now_seg;
17310 subsegT saved_subseg = now_subseg;
ecb4347a
DJ
17311 expressionS exp;
17312 char *fragp;
252b5132 17313
252b5132 17314#ifdef md_flush_pending_output
ecb4347a 17315 md_flush_pending_output ();
252b5132
RH
17316#endif
17317
9c2799c2 17318 gas_assert (pdr_seg);
ecb4347a 17319 subseg_set (pdr_seg, 0);
252b5132 17320
ecb4347a
DJ
17321 /* Write the symbol. */
17322 exp.X_op = O_symbol;
17323 exp.X_add_symbol = p;
17324 exp.X_add_number = 0;
17325 emit_expr (&exp, 4);
252b5132 17326
ecb4347a 17327 fragp = frag_more (7 * 4);
252b5132 17328
17a2f251
TS
17329 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
17330 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
17331 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
17332 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
17333 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
17334 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
17335 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 17336
ecb4347a
DJ
17337 subseg_set (saved_seg, saved_subseg);
17338 }
252b5132
RH
17339
17340 cur_proc_ptr = NULL;
17341}
17342
17343/* The .aent and .ent directives. */
17344
17345static void
17a2f251 17346s_mips_ent (int aent)
252b5132 17347{
252b5132 17348 symbolS *symbolP;
252b5132
RH
17349
17350 symbolP = get_symbol ();
17351 if (*input_line_pointer == ',')
f9419b05 17352 ++input_line_pointer;
252b5132 17353 SKIP_WHITESPACE ();
3882b010 17354 if (ISDIGIT (*input_line_pointer)
d9a62219 17355 || *input_line_pointer == '-')
874e8986 17356 get_number ();
252b5132 17357
14949570 17358 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
17359 as_warn (_(".ent or .aent not in text section."));
17360
17361 if (!aent && cur_proc_ptr)
9a41af64 17362 as_warn (_("missing .end"));
252b5132
RH
17363
17364 if (!aent)
17365 {
7a621144
DJ
17366 /* This function needs its own .frame and .cprestore directives. */
17367 mips_frame_reg_valid = 0;
17368 mips_cprestore_valid = 0;
17369
252b5132
RH
17370 cur_proc_ptr = &cur_proc;
17371 memset (cur_proc_ptr, '\0', sizeof (procS));
17372
9b2f1d35 17373 cur_proc_ptr->func_sym = symbolP;
252b5132 17374
f9419b05 17375 ++numprocs;
ecb4347a
DJ
17376
17377 if (debug_type == DEBUG_STABS)
17378 stabs_generate_asm_func (S_GET_NAME (symbolP),
17379 S_GET_NAME (symbolP));
252b5132
RH
17380 }
17381
7c0fc524
MR
17382 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
17383
252b5132
RH
17384 demand_empty_rest_of_line ();
17385}
17386
17387/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 17388 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 17389 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 17390 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
17391 symbol table (in the mdebug section). */
17392
17393static void
17a2f251 17394s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 17395{
f3ded42a
RS
17396 if (ECOFF_DEBUGGING)
17397 s_ignore (ignore);
17398 else
ecb4347a
DJ
17399 {
17400 long val;
252b5132 17401
ecb4347a
DJ
17402 if (cur_proc_ptr == (procS *) NULL)
17403 {
17404 as_warn (_(".frame outside of .ent"));
17405 demand_empty_rest_of_line ();
17406 return;
17407 }
252b5132 17408
ecb4347a
DJ
17409 cur_proc_ptr->frame_reg = tc_get_register (1);
17410
17411 SKIP_WHITESPACE ();
17412 if (*input_line_pointer++ != ','
17413 || get_absolute_expression_and_terminator (&val) != ',')
17414 {
17415 as_warn (_("Bad .frame directive"));
17416 --input_line_pointer;
17417 demand_empty_rest_of_line ();
17418 return;
17419 }
252b5132 17420
ecb4347a
DJ
17421 cur_proc_ptr->frame_offset = val;
17422 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 17423
252b5132 17424 demand_empty_rest_of_line ();
252b5132 17425 }
252b5132
RH
17426}
17427
bdaaa2e1
KH
17428/* The .fmask and .mask directives. If the mdebug section is present
17429 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 17430 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 17431 information correctly. We can't use the ecoff routines because they
252b5132
RH
17432 make reference to the ecoff symbol table (in the mdebug section). */
17433
17434static void
17a2f251 17435s_mips_mask (int reg_type)
252b5132 17436{
f3ded42a
RS
17437 if (ECOFF_DEBUGGING)
17438 s_ignore (reg_type);
17439 else
252b5132 17440 {
ecb4347a 17441 long mask, off;
252b5132 17442
ecb4347a
DJ
17443 if (cur_proc_ptr == (procS *) NULL)
17444 {
17445 as_warn (_(".mask/.fmask outside of .ent"));
17446 demand_empty_rest_of_line ();
17447 return;
17448 }
252b5132 17449
ecb4347a
DJ
17450 if (get_absolute_expression_and_terminator (&mask) != ',')
17451 {
17452 as_warn (_("Bad .mask/.fmask directive"));
17453 --input_line_pointer;
17454 demand_empty_rest_of_line ();
17455 return;
17456 }
252b5132 17457
ecb4347a
DJ
17458 off = get_absolute_expression ();
17459
17460 if (reg_type == 'F')
17461 {
17462 cur_proc_ptr->fpreg_mask = mask;
17463 cur_proc_ptr->fpreg_offset = off;
17464 }
17465 else
17466 {
17467 cur_proc_ptr->reg_mask = mask;
17468 cur_proc_ptr->reg_offset = off;
17469 }
17470
17471 demand_empty_rest_of_line ();
252b5132 17472 }
252b5132
RH
17473}
17474
316f5878
RS
17475/* A table describing all the processors gas knows about. Names are
17476 matched in the order listed.
e7af610e 17477
316f5878
RS
17478 To ease comparison, please keep this table in the same order as
17479 gcc's mips_cpu_info_table[]. */
e972090a
NC
17480static const struct mips_cpu_info mips_cpu_info_table[] =
17481{
316f5878 17482 /* Entries for generic ISAs */
d16afab6
RS
17483 { "mips1", MIPS_CPU_IS_ISA, 0, ISA_MIPS1, CPU_R3000 },
17484 { "mips2", MIPS_CPU_IS_ISA, 0, ISA_MIPS2, CPU_R6000 },
17485 { "mips3", MIPS_CPU_IS_ISA, 0, ISA_MIPS3, CPU_R4000 },
17486 { "mips4", MIPS_CPU_IS_ISA, 0, ISA_MIPS4, CPU_R8000 },
17487 { "mips5", MIPS_CPU_IS_ISA, 0, ISA_MIPS5, CPU_MIPS5 },
17488 { "mips32", MIPS_CPU_IS_ISA, 0, ISA_MIPS32, CPU_MIPS32 },
17489 { "mips32r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17490 { "mips64", MIPS_CPU_IS_ISA, 0, ISA_MIPS64, CPU_MIPS64 },
17491 { "mips64r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R2, CPU_MIPS64R2 },
316f5878
RS
17492
17493 /* MIPS I */
d16afab6
RS
17494 { "r3000", 0, 0, ISA_MIPS1, CPU_R3000 },
17495 { "r2000", 0, 0, ISA_MIPS1, CPU_R3000 },
17496 { "r3900", 0, 0, ISA_MIPS1, CPU_R3900 },
316f5878
RS
17497
17498 /* MIPS II */
d16afab6 17499 { "r6000", 0, 0, ISA_MIPS2, CPU_R6000 },
316f5878
RS
17500
17501 /* MIPS III */
d16afab6
RS
17502 { "r4000", 0, 0, ISA_MIPS3, CPU_R4000 },
17503 { "r4010", 0, 0, ISA_MIPS2, CPU_R4010 },
17504 { "vr4100", 0, 0, ISA_MIPS3, CPU_VR4100 },
17505 { "vr4111", 0, 0, ISA_MIPS3, CPU_R4111 },
17506 { "vr4120", 0, 0, ISA_MIPS3, CPU_VR4120 },
17507 { "vr4130", 0, 0, ISA_MIPS3, CPU_VR4120 },
17508 { "vr4181", 0, 0, ISA_MIPS3, CPU_R4111 },
17509 { "vr4300", 0, 0, ISA_MIPS3, CPU_R4300 },
17510 { "r4400", 0, 0, ISA_MIPS3, CPU_R4400 },
17511 { "r4600", 0, 0, ISA_MIPS3, CPU_R4600 },
17512 { "orion", 0, 0, ISA_MIPS3, CPU_R4600 },
17513 { "r4650", 0, 0, ISA_MIPS3, CPU_R4650 },
17514 { "r5900", 0, 0, ISA_MIPS3, CPU_R5900 },
b15591bb 17515 /* ST Microelectronics Loongson 2E and 2F cores */
d16afab6
RS
17516 { "loongson2e", 0, 0, ISA_MIPS3, CPU_LOONGSON_2E },
17517 { "loongson2f", 0, 0, ISA_MIPS3, CPU_LOONGSON_2F },
316f5878
RS
17518
17519 /* MIPS IV */
d16afab6
RS
17520 { "r8000", 0, 0, ISA_MIPS4, CPU_R8000 },
17521 { "r10000", 0, 0, ISA_MIPS4, CPU_R10000 },
17522 { "r12000", 0, 0, ISA_MIPS4, CPU_R12000 },
17523 { "r14000", 0, 0, ISA_MIPS4, CPU_R14000 },
17524 { "r16000", 0, 0, ISA_MIPS4, CPU_R16000 },
17525 { "vr5000", 0, 0, ISA_MIPS4, CPU_R5000 },
17526 { "vr5400", 0, 0, ISA_MIPS4, CPU_VR5400 },
17527 { "vr5500", 0, 0, ISA_MIPS4, CPU_VR5500 },
17528 { "rm5200", 0, 0, ISA_MIPS4, CPU_R5000 },
17529 { "rm5230", 0, 0, ISA_MIPS4, CPU_R5000 },
17530 { "rm5231", 0, 0, ISA_MIPS4, CPU_R5000 },
17531 { "rm5261", 0, 0, ISA_MIPS4, CPU_R5000 },
17532 { "rm5721", 0, 0, ISA_MIPS4, CPU_R5000 },
17533 { "rm7000", 0, 0, ISA_MIPS4, CPU_RM7000 },
17534 { "rm9000", 0, 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
17535
17536 /* MIPS 32 */
d16afab6
RS
17537 { "4kc", 0, 0, ISA_MIPS32, CPU_MIPS32 },
17538 { "4km", 0, 0, ISA_MIPS32, CPU_MIPS32 },
17539 { "4kp", 0, 0, ISA_MIPS32, CPU_MIPS32 },
17540 { "4ksc", 0, ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
ad3fea08
TS
17541
17542 /* MIPS 32 Release 2 */
d16afab6
RS
17543 { "4kec", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17544 { "4kem", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17545 { "4kep", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17546 { "4ksd", 0, ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
17547 { "m4k", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17548 { "m4kp", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17549 { "m14k", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
17550 { "m14kc", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
17551 { "m14ke", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
17552 ISA_MIPS32R2, CPU_MIPS32R2 },
17553 { "m14kec", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
17554 ISA_MIPS32R2, CPU_MIPS32R2 },
17555 { "24kc", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17556 { "24kf2_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17557 { "24kf", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17558 { "24kf1_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 17559 /* Deprecated forms of the above. */
d16afab6
RS
17560 { "24kfx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17561 { "24kx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 17562 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
d16afab6
RS
17563 { "24kec", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
17564 { "24kef2_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
17565 { "24kef", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
17566 { "24kef1_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 17567 /* Deprecated forms of the above. */
d16afab6
RS
17568 { "24kefx", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
17569 { "24kex", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 17570 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
d16afab6
RS
17571 { "34kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17572 { "34kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17573 { "34kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17574 { "34kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 17575 /* Deprecated forms of the above. */
d16afab6
RS
17576 { "34kfx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17577 { "34kx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
711eefe4 17578 /* 34Kn is a 34kc without DSP. */
d16afab6 17579 { "34kn", 0, ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 17580 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
d16afab6
RS
17581 { "74kc", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
17582 { "74kf2_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
17583 { "74kf", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
17584 { "74kf1_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
17585 { "74kf3_2", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 17586 /* Deprecated forms of the above. */
d16afab6
RS
17587 { "74kfx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
17588 { "74kx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
30f8113a 17589 /* 1004K cores are multiprocessor versions of the 34K. */
d16afab6
RS
17590 { "1004kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17591 { "1004kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17592 { "1004kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17593 { "1004kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
32b26a03 17594
316f5878 17595 /* MIPS 64 */
d16afab6
RS
17596 { "5kc", 0, 0, ISA_MIPS64, CPU_MIPS64 },
17597 { "5kf", 0, 0, ISA_MIPS64, CPU_MIPS64 },
17598 { "20kc", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
17599 { "25kf", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
ad3fea08 17600
c7a23324 17601 /* Broadcom SB-1 CPU core */
d16afab6 17602 { "sb1", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
1e85aad8 17603 /* Broadcom SB-1A CPU core */
d16afab6 17604 { "sb1a", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
d051516a 17605
d16afab6 17606 { "loongson3a", 0, 0, ISA_MIPS64, CPU_LOONGSON_3A },
e7af610e 17607
ed163775
MR
17608 /* MIPS 64 Release 2 */
17609
967344c6 17610 /* Cavium Networks Octeon CPU core */
d16afab6
RS
17611 { "octeon", 0, 0, ISA_MIPS64R2, CPU_OCTEON },
17612 { "octeon+", 0, 0, ISA_MIPS64R2, CPU_OCTEONP },
17613 { "octeon2", 0, 0, ISA_MIPS64R2, CPU_OCTEON2 },
967344c6 17614
52b6b6b9 17615 /* RMI Xlr */
d16afab6 17616 { "xlr", 0, 0, ISA_MIPS64, CPU_XLR },
52b6b6b9 17617
55a36193
MK
17618 /* Broadcom XLP.
17619 XLP is mostly like XLR, with the prominent exception that it is
17620 MIPS64R2 rather than MIPS64. */
d16afab6 17621 { "xlp", 0, 0, ISA_MIPS64R2, CPU_XLR },
55a36193 17622
316f5878 17623 /* End marker */
d16afab6 17624 { NULL, 0, 0, 0, 0 }
316f5878 17625};
e7af610e 17626
84ea6cf2 17627
316f5878
RS
17628/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
17629 with a final "000" replaced by "k". Ignore case.
e7af610e 17630
316f5878 17631 Note: this function is shared between GCC and GAS. */
c6c98b38 17632
b34976b6 17633static bfd_boolean
17a2f251 17634mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
17635{
17636 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
17637 given++, canonical++;
17638
17639 return ((*given == 0 && *canonical == 0)
17640 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
17641}
17642
17643
17644/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
17645 CPU name. We've traditionally allowed a lot of variation here.
17646
17647 Note: this function is shared between GCC and GAS. */
17648
b34976b6 17649static bfd_boolean
17a2f251 17650mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
17651{
17652 /* First see if the name matches exactly, or with a final "000"
17653 turned into "k". */
17654 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 17655 return TRUE;
316f5878
RS
17656
17657 /* If not, try comparing based on numerical designation alone.
17658 See if GIVEN is an unadorned number, or 'r' followed by a number. */
17659 if (TOLOWER (*given) == 'r')
17660 given++;
17661 if (!ISDIGIT (*given))
b34976b6 17662 return FALSE;
316f5878
RS
17663
17664 /* Skip over some well-known prefixes in the canonical name,
17665 hoping to find a number there too. */
17666 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
17667 canonical += 2;
17668 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
17669 canonical += 2;
17670 else if (TOLOWER (canonical[0]) == 'r')
17671 canonical += 1;
17672
17673 return mips_strict_matching_cpu_name_p (canonical, given);
17674}
17675
17676
17677/* Parse an option that takes the name of a processor as its argument.
17678 OPTION is the name of the option and CPU_STRING is the argument.
17679 Return the corresponding processor enumeration if the CPU_STRING is
17680 recognized, otherwise report an error and return null.
17681
17682 A similar function exists in GCC. */
e7af610e
NC
17683
17684static const struct mips_cpu_info *
17a2f251 17685mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 17686{
316f5878 17687 const struct mips_cpu_info *p;
e7af610e 17688
316f5878
RS
17689 /* 'from-abi' selects the most compatible architecture for the given
17690 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
17691 EABIs, we have to decide whether we're using the 32-bit or 64-bit
17692 version. Look first at the -mgp options, if given, otherwise base
17693 the choice on MIPS_DEFAULT_64BIT.
e7af610e 17694
316f5878
RS
17695 Treat NO_ABI like the EABIs. One reason to do this is that the
17696 plain 'mips' and 'mips64' configs have 'from-abi' as their default
17697 architecture. This code picks MIPS I for 'mips' and MIPS III for
17698 'mips64', just as we did in the days before 'from-abi'. */
17699 if (strcasecmp (cpu_string, "from-abi") == 0)
17700 {
17701 if (ABI_NEEDS_32BIT_REGS (mips_abi))
17702 return mips_cpu_info_from_isa (ISA_MIPS1);
17703
17704 if (ABI_NEEDS_64BIT_REGS (mips_abi))
17705 return mips_cpu_info_from_isa (ISA_MIPS3);
17706
17707 if (file_mips_gp32 >= 0)
17708 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
17709
17710 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
17711 ? ISA_MIPS3
17712 : ISA_MIPS1);
17713 }
17714
17715 /* 'default' has traditionally been a no-op. Probably not very useful. */
17716 if (strcasecmp (cpu_string, "default") == 0)
17717 return 0;
17718
17719 for (p = mips_cpu_info_table; p->name != 0; p++)
17720 if (mips_matching_cpu_name_p (p->name, cpu_string))
17721 return p;
17722
20203fb9 17723 as_bad (_("Bad value (%s) for %s"), cpu_string, option);
316f5878 17724 return 0;
e7af610e
NC
17725}
17726
316f5878
RS
17727/* Return the canonical processor information for ISA (a member of the
17728 ISA_MIPS* enumeration). */
17729
e7af610e 17730static const struct mips_cpu_info *
17a2f251 17731mips_cpu_info_from_isa (int isa)
e7af610e
NC
17732{
17733 int i;
17734
17735 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
ad3fea08 17736 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
316f5878 17737 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
17738 return (&mips_cpu_info_table[i]);
17739
e972090a 17740 return NULL;
e7af610e 17741}
fef14a42
TS
17742
17743static const struct mips_cpu_info *
17a2f251 17744mips_cpu_info_from_arch (int arch)
fef14a42
TS
17745{
17746 int i;
17747
17748 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
17749 if (arch == mips_cpu_info_table[i].cpu)
17750 return (&mips_cpu_info_table[i]);
17751
17752 return NULL;
17753}
316f5878
RS
17754\f
17755static void
17a2f251 17756show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
17757{
17758 if (*first_p)
17759 {
17760 fprintf (stream, "%24s", "");
17761 *col_p = 24;
17762 }
17763 else
17764 {
17765 fprintf (stream, ", ");
17766 *col_p += 2;
17767 }
e7af610e 17768
316f5878
RS
17769 if (*col_p + strlen (string) > 72)
17770 {
17771 fprintf (stream, "\n%24s", "");
17772 *col_p = 24;
17773 }
17774
17775 fprintf (stream, "%s", string);
17776 *col_p += strlen (string);
17777
17778 *first_p = 0;
17779}
17780
17781void
17a2f251 17782md_show_usage (FILE *stream)
e7af610e 17783{
316f5878
RS
17784 int column, first;
17785 size_t i;
17786
17787 fprintf (stream, _("\
17788MIPS options:\n\
316f5878
RS
17789-EB generate big endian output\n\
17790-EL generate little endian output\n\
17791-g, -g2 do not remove unneeded NOPs or swap branches\n\
17792-G NUM allow referencing objects up to NUM bytes\n\
17793 implicitly with the gp register [default 8]\n"));
17794 fprintf (stream, _("\
17795-mips1 generate MIPS ISA I instructions\n\
17796-mips2 generate MIPS ISA II instructions\n\
17797-mips3 generate MIPS ISA III instructions\n\
17798-mips4 generate MIPS ISA IV instructions\n\
17799-mips5 generate MIPS ISA V instructions\n\
17800-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 17801-mips32r2 generate MIPS32 release 2 ISA instructions\n\
316f5878 17802-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 17803-mips64r2 generate MIPS64 release 2 ISA instructions\n\
316f5878
RS
17804-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
17805
17806 first = 1;
e7af610e
NC
17807
17808 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
17809 show (stream, mips_cpu_info_table[i].name, &column, &first);
17810 show (stream, "from-abi", &column, &first);
17811 fputc ('\n', stream);
e7af610e 17812
316f5878
RS
17813 fprintf (stream, _("\
17814-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
17815-no-mCPU don't generate code specific to CPU.\n\
17816 For -mCPU and -no-mCPU, CPU must be one of:\n"));
17817
17818 first = 1;
17819
17820 show (stream, "3900", &column, &first);
17821 show (stream, "4010", &column, &first);
17822 show (stream, "4100", &column, &first);
17823 show (stream, "4650", &column, &first);
17824 fputc ('\n', stream);
17825
17826 fprintf (stream, _("\
17827-mips16 generate mips16 instructions\n\
17828-no-mips16 do not generate mips16 instructions\n"));
17829 fprintf (stream, _("\
df58fc94
RS
17830-mmicromips generate microMIPS instructions\n\
17831-mno-micromips do not generate microMIPS instructions\n"));
17832 fprintf (stream, _("\
e16bfa71
TS
17833-msmartmips generate smartmips instructions\n\
17834-mno-smartmips do not generate smartmips instructions\n"));
17835 fprintf (stream, _("\
74cd071d
CF
17836-mdsp generate DSP instructions\n\
17837-mno-dsp do not generate DSP instructions\n"));
17838 fprintf (stream, _("\
8b082fb1
TS
17839-mdspr2 generate DSP R2 instructions\n\
17840-mno-dspr2 do not generate DSP R2 instructions\n"));
17841 fprintf (stream, _("\
ef2e4d86
CF
17842-mmt generate MT instructions\n\
17843-mno-mt do not generate MT instructions\n"));
17844 fprintf (stream, _("\
dec0624d
MR
17845-mmcu generate MCU instructions\n\
17846-mno-mcu do not generate MCU instructions\n"));
17847 fprintf (stream, _("\
b015e599
AP
17848-mvirt generate Virtualization instructions\n\
17849-mno-virt do not generate Virtualization instructions\n"));
17850 fprintf (stream, _("\
833794fc
MR
17851-minsn32 only generate 32-bit microMIPS instructions\n\
17852-mno-insn32 generate all microMIPS instructions\n"));
17853 fprintf (stream, _("\
c67a084a
NC
17854-mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
17855-mfix-loongson2f-nop work around Loongson2F NOP errata\n\
d766e8ec 17856-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 17857-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
6a32d874 17858-mfix-24k insert a nop after ERET and DERET instructions\n\
d954098f 17859-mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
316f5878
RS
17860-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
17861-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261 17862-msym32 assume all symbols have 32-bit values\n\
316f5878
RS
17863-O0 remove unneeded NOPs, do not swap branches\n\
17864-O remove unneeded NOPs and swap branches\n\
316f5878
RS
17865--trap, --no-break trap exception on div by 0 and mult overflow\n\
17866--break, --no-trap break exception on div by 0 and mult overflow\n"));
037b32b9
AN
17867 fprintf (stream, _("\
17868-mhard-float allow floating-point instructions\n\
17869-msoft-float do not allow floating-point instructions\n\
17870-msingle-float only allow 32-bit floating-point operations\n\
17871-mdouble-float allow 32-bit and 64-bit floating-point operations\n\
3bf0dbfb 17872--[no-]construct-floats [dis]allow floating point values to be constructed\n\
ba92f887
MR
17873--[no-]relax-branch [dis]allow out-of-range branches to be relaxed\n\
17874-mnan=ENCODING select an IEEE 754 NaN encoding convention, either of:\n"));
17875
17876 first = 1;
17877
17878 show (stream, "legacy", &column, &first);
17879 show (stream, "2008", &column, &first);
17880
17881 fputc ('\n', stream);
17882
316f5878
RS
17883 fprintf (stream, _("\
17884-KPIC, -call_shared generate SVR4 position independent code\n\
861fb55a 17885-call_nonpic generate non-PIC code that can operate with DSOs\n\
0c000745 17886-mvxworks-pic generate VxWorks position independent code\n\
861fb55a 17887-non_shared do not generate code that can operate with DSOs\n\
316f5878 17888-xgot assume a 32 bit GOT\n\
dcd410fe 17889-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8 17890-mshared, -mno-shared disable/enable .cpload optimization for\n\
d821e36b 17891 position dependent (non shared) code\n\
316f5878
RS
17892-mabi=ABI create ABI conformant object file for:\n"));
17893
17894 first = 1;
17895
17896 show (stream, "32", &column, &first);
17897 show (stream, "o64", &column, &first);
17898 show (stream, "n32", &column, &first);
17899 show (stream, "64", &column, &first);
17900 show (stream, "eabi", &column, &first);
17901
17902 fputc ('\n', stream);
17903
17904 fprintf (stream, _("\
17905-32 create o32 ABI object file (default)\n\
17906-n32 create n32 ABI object file\n\
17907-64 create 64 ABI object file\n"));
e7af610e 17908}
14e777e0 17909
1575952e 17910#ifdef TE_IRIX
14e777e0 17911enum dwarf2_format
413a266c 17912mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
14e777e0 17913{
369943fe 17914 if (HAVE_64BIT_SYMBOLS)
1575952e 17915 return dwarf2_format_64bit_irix;
14e777e0
KB
17916 else
17917 return dwarf2_format_32bit;
17918}
1575952e 17919#endif
73369e65
EC
17920
17921int
17922mips_dwarf2_addr_size (void)
17923{
6b6b3450 17924 if (HAVE_64BIT_OBJECTS)
73369e65 17925 return 8;
73369e65
EC
17926 else
17927 return 4;
17928}
5862107c
EC
17929
17930/* Standard calling conventions leave the CFA at SP on entry. */
17931void
17932mips_cfi_frame_initial_instructions (void)
17933{
17934 cfi_add_CFA_def_cfa_register (SP);
17935}
17936
707bfff6
TS
17937int
17938tc_mips_regname_to_dw2regnum (char *regname)
17939{
17940 unsigned int regnum = -1;
17941 unsigned int reg;
17942
17943 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
17944 regnum = reg;
17945
17946 return regnum;
17947}
This page took 2.608722 seconds and 4 git commands to generate.