gas/
[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 692/* Arrays of operands for each instruction. */
14daeee3 693#define MAX_OPERANDS 6
fc76e730
RS
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
14daeee3
RS
2209#define RNUM_MASK 0x00000ff
2210#define RTYPE_MASK 0x0efff00
2211#define RTYPE_NUM 0x0000100
2212#define RTYPE_FPU 0x0000200
2213#define RTYPE_FCC 0x0000400
2214#define RTYPE_VEC 0x0000800
2215#define RTYPE_GP 0x0001000
2216#define RTYPE_CP0 0x0002000
2217#define RTYPE_PC 0x0004000
2218#define RTYPE_ACC 0x0008000
2219#define RTYPE_CCC 0x0010000
2220#define RTYPE_VI 0x0020000
2221#define RTYPE_VF 0x0040000
2222#define RTYPE_R5900_I 0x0080000
2223#define RTYPE_R5900_Q 0x0100000
2224#define RTYPE_R5900_R 0x0200000
2225#define RTYPE_R5900_ACC 0x0400000
2226#define RWARN 0x8000000
707bfff6
TS
2227
2228#define GENERIC_REGISTER_NUMBERS \
2229 {"$0", RTYPE_NUM | 0}, \
2230 {"$1", RTYPE_NUM | 1}, \
2231 {"$2", RTYPE_NUM | 2}, \
2232 {"$3", RTYPE_NUM | 3}, \
2233 {"$4", RTYPE_NUM | 4}, \
2234 {"$5", RTYPE_NUM | 5}, \
2235 {"$6", RTYPE_NUM | 6}, \
2236 {"$7", RTYPE_NUM | 7}, \
2237 {"$8", RTYPE_NUM | 8}, \
2238 {"$9", RTYPE_NUM | 9}, \
2239 {"$10", RTYPE_NUM | 10}, \
2240 {"$11", RTYPE_NUM | 11}, \
2241 {"$12", RTYPE_NUM | 12}, \
2242 {"$13", RTYPE_NUM | 13}, \
2243 {"$14", RTYPE_NUM | 14}, \
2244 {"$15", RTYPE_NUM | 15}, \
2245 {"$16", RTYPE_NUM | 16}, \
2246 {"$17", RTYPE_NUM | 17}, \
2247 {"$18", RTYPE_NUM | 18}, \
2248 {"$19", RTYPE_NUM | 19}, \
2249 {"$20", RTYPE_NUM | 20}, \
2250 {"$21", RTYPE_NUM | 21}, \
2251 {"$22", RTYPE_NUM | 22}, \
2252 {"$23", RTYPE_NUM | 23}, \
2253 {"$24", RTYPE_NUM | 24}, \
2254 {"$25", RTYPE_NUM | 25}, \
2255 {"$26", RTYPE_NUM | 26}, \
2256 {"$27", RTYPE_NUM | 27}, \
2257 {"$28", RTYPE_NUM | 28}, \
2258 {"$29", RTYPE_NUM | 29}, \
2259 {"$30", RTYPE_NUM | 30}, \
2260 {"$31", RTYPE_NUM | 31}
2261
2262#define FPU_REGISTER_NAMES \
2263 {"$f0", RTYPE_FPU | 0}, \
2264 {"$f1", RTYPE_FPU | 1}, \
2265 {"$f2", RTYPE_FPU | 2}, \
2266 {"$f3", RTYPE_FPU | 3}, \
2267 {"$f4", RTYPE_FPU | 4}, \
2268 {"$f5", RTYPE_FPU | 5}, \
2269 {"$f6", RTYPE_FPU | 6}, \
2270 {"$f7", RTYPE_FPU | 7}, \
2271 {"$f8", RTYPE_FPU | 8}, \
2272 {"$f9", RTYPE_FPU | 9}, \
2273 {"$f10", RTYPE_FPU | 10}, \
2274 {"$f11", RTYPE_FPU | 11}, \
2275 {"$f12", RTYPE_FPU | 12}, \
2276 {"$f13", RTYPE_FPU | 13}, \
2277 {"$f14", RTYPE_FPU | 14}, \
2278 {"$f15", RTYPE_FPU | 15}, \
2279 {"$f16", RTYPE_FPU | 16}, \
2280 {"$f17", RTYPE_FPU | 17}, \
2281 {"$f18", RTYPE_FPU | 18}, \
2282 {"$f19", RTYPE_FPU | 19}, \
2283 {"$f20", RTYPE_FPU | 20}, \
2284 {"$f21", RTYPE_FPU | 21}, \
2285 {"$f22", RTYPE_FPU | 22}, \
2286 {"$f23", RTYPE_FPU | 23}, \
2287 {"$f24", RTYPE_FPU | 24}, \
2288 {"$f25", RTYPE_FPU | 25}, \
2289 {"$f26", RTYPE_FPU | 26}, \
2290 {"$f27", RTYPE_FPU | 27}, \
2291 {"$f28", RTYPE_FPU | 28}, \
2292 {"$f29", RTYPE_FPU | 29}, \
2293 {"$f30", RTYPE_FPU | 30}, \
2294 {"$f31", RTYPE_FPU | 31}
2295
2296#define FPU_CONDITION_CODE_NAMES \
2297 {"$fcc0", RTYPE_FCC | 0}, \
2298 {"$fcc1", RTYPE_FCC | 1}, \
2299 {"$fcc2", RTYPE_FCC | 2}, \
2300 {"$fcc3", RTYPE_FCC | 3}, \
2301 {"$fcc4", RTYPE_FCC | 4}, \
2302 {"$fcc5", RTYPE_FCC | 5}, \
2303 {"$fcc6", RTYPE_FCC | 6}, \
2304 {"$fcc7", RTYPE_FCC | 7}
2305
2306#define COPROC_CONDITION_CODE_NAMES \
2307 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
2308 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
2309 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
2310 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
2311 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
2312 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
2313 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
2314 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
2315
2316#define N32N64_SYMBOLIC_REGISTER_NAMES \
2317 {"$a4", RTYPE_GP | 8}, \
2318 {"$a5", RTYPE_GP | 9}, \
2319 {"$a6", RTYPE_GP | 10}, \
2320 {"$a7", RTYPE_GP | 11}, \
2321 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
2322 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
2323 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
2324 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
2325 {"$t0", RTYPE_GP | 12}, \
2326 {"$t1", RTYPE_GP | 13}, \
2327 {"$t2", RTYPE_GP | 14}, \
2328 {"$t3", RTYPE_GP | 15}
2329
2330#define O32_SYMBOLIC_REGISTER_NAMES \
2331 {"$t0", RTYPE_GP | 8}, \
2332 {"$t1", RTYPE_GP | 9}, \
2333 {"$t2", RTYPE_GP | 10}, \
2334 {"$t3", RTYPE_GP | 11}, \
2335 {"$t4", RTYPE_GP | 12}, \
2336 {"$t5", RTYPE_GP | 13}, \
2337 {"$t6", RTYPE_GP | 14}, \
2338 {"$t7", RTYPE_GP | 15}, \
2339 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2340 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2341 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
2342 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
2343
2344/* Remaining symbolic register names */
2345#define SYMBOLIC_REGISTER_NAMES \
2346 {"$zero", RTYPE_GP | 0}, \
2347 {"$at", RTYPE_GP | 1}, \
2348 {"$AT", RTYPE_GP | 1}, \
2349 {"$v0", RTYPE_GP | 2}, \
2350 {"$v1", RTYPE_GP | 3}, \
2351 {"$a0", RTYPE_GP | 4}, \
2352 {"$a1", RTYPE_GP | 5}, \
2353 {"$a2", RTYPE_GP | 6}, \
2354 {"$a3", RTYPE_GP | 7}, \
2355 {"$s0", RTYPE_GP | 16}, \
2356 {"$s1", RTYPE_GP | 17}, \
2357 {"$s2", RTYPE_GP | 18}, \
2358 {"$s3", RTYPE_GP | 19}, \
2359 {"$s4", RTYPE_GP | 20}, \
2360 {"$s5", RTYPE_GP | 21}, \
2361 {"$s6", RTYPE_GP | 22}, \
2362 {"$s7", RTYPE_GP | 23}, \
2363 {"$t8", RTYPE_GP | 24}, \
2364 {"$t9", RTYPE_GP | 25}, \
2365 {"$k0", RTYPE_GP | 26}, \
2366 {"$kt0", RTYPE_GP | 26}, \
2367 {"$k1", RTYPE_GP | 27}, \
2368 {"$kt1", RTYPE_GP | 27}, \
2369 {"$gp", RTYPE_GP | 28}, \
2370 {"$sp", RTYPE_GP | 29}, \
2371 {"$s8", RTYPE_GP | 30}, \
2372 {"$fp", RTYPE_GP | 30}, \
2373 {"$ra", RTYPE_GP | 31}
2374
2375#define MIPS16_SPECIAL_REGISTER_NAMES \
2376 {"$pc", RTYPE_PC | 0}
2377
2378#define MDMX_VECTOR_REGISTER_NAMES \
2379 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
2380 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
2381 {"$v2", RTYPE_VEC | 2}, \
2382 {"$v3", RTYPE_VEC | 3}, \
2383 {"$v4", RTYPE_VEC | 4}, \
2384 {"$v5", RTYPE_VEC | 5}, \
2385 {"$v6", RTYPE_VEC | 6}, \
2386 {"$v7", RTYPE_VEC | 7}, \
2387 {"$v8", RTYPE_VEC | 8}, \
2388 {"$v9", RTYPE_VEC | 9}, \
2389 {"$v10", RTYPE_VEC | 10}, \
2390 {"$v11", RTYPE_VEC | 11}, \
2391 {"$v12", RTYPE_VEC | 12}, \
2392 {"$v13", RTYPE_VEC | 13}, \
2393 {"$v14", RTYPE_VEC | 14}, \
2394 {"$v15", RTYPE_VEC | 15}, \
2395 {"$v16", RTYPE_VEC | 16}, \
2396 {"$v17", RTYPE_VEC | 17}, \
2397 {"$v18", RTYPE_VEC | 18}, \
2398 {"$v19", RTYPE_VEC | 19}, \
2399 {"$v20", RTYPE_VEC | 20}, \
2400 {"$v21", RTYPE_VEC | 21}, \
2401 {"$v22", RTYPE_VEC | 22}, \
2402 {"$v23", RTYPE_VEC | 23}, \
2403 {"$v24", RTYPE_VEC | 24}, \
2404 {"$v25", RTYPE_VEC | 25}, \
2405 {"$v26", RTYPE_VEC | 26}, \
2406 {"$v27", RTYPE_VEC | 27}, \
2407 {"$v28", RTYPE_VEC | 28}, \
2408 {"$v29", RTYPE_VEC | 29}, \
2409 {"$v30", RTYPE_VEC | 30}, \
2410 {"$v31", RTYPE_VEC | 31}
2411
14daeee3
RS
2412#define R5900_I_NAMES \
2413 {"$I", RTYPE_R5900_I | 0}
2414
2415#define R5900_Q_NAMES \
2416 {"$Q", RTYPE_R5900_Q | 0}
2417
2418#define R5900_R_NAMES \
2419 {"$R", RTYPE_R5900_R | 0}
2420
2421#define R5900_ACC_NAMES \
2422 {"$ACC", RTYPE_R5900_ACC | 0 }
2423
707bfff6
TS
2424#define MIPS_DSP_ACCUMULATOR_NAMES \
2425 {"$ac0", RTYPE_ACC | 0}, \
2426 {"$ac1", RTYPE_ACC | 1}, \
2427 {"$ac2", RTYPE_ACC | 2}, \
2428 {"$ac3", RTYPE_ACC | 3}
2429
2430static const struct regname reg_names[] = {
2431 GENERIC_REGISTER_NUMBERS,
2432 FPU_REGISTER_NAMES,
2433 FPU_CONDITION_CODE_NAMES,
2434 COPROC_CONDITION_CODE_NAMES,
2435
2436 /* The $txx registers depends on the abi,
2437 these will be added later into the symbol table from
2438 one of the tables below once mips_abi is set after
2439 parsing of arguments from the command line. */
2440 SYMBOLIC_REGISTER_NAMES,
2441
2442 MIPS16_SPECIAL_REGISTER_NAMES,
2443 MDMX_VECTOR_REGISTER_NAMES,
14daeee3
RS
2444 R5900_I_NAMES,
2445 R5900_Q_NAMES,
2446 R5900_R_NAMES,
2447 R5900_ACC_NAMES,
707bfff6
TS
2448 MIPS_DSP_ACCUMULATOR_NAMES,
2449 {0, 0}
2450};
2451
2452static const struct regname reg_names_o32[] = {
2453 O32_SYMBOLIC_REGISTER_NAMES,
2454 {0, 0}
2455};
2456
2457static const struct regname reg_names_n32n64[] = {
2458 N32N64_SYMBOLIC_REGISTER_NAMES,
2459 {0, 0}
2460};
2461
a92713e6
RS
2462/* Register symbols $v0 and $v1 map to GPRs 2 and 3, but they can also be
2463 interpreted as vector registers 0 and 1. If SYMVAL is the value of one
2464 of these register symbols, return the associated vector register,
2465 otherwise return SYMVAL itself. */
df58fc94 2466
a92713e6
RS
2467static unsigned int
2468mips_prefer_vec_regno (unsigned int symval)
707bfff6 2469{
a92713e6
RS
2470 if ((symval & -2) == (RTYPE_GP | 2))
2471 return RTYPE_VEC | (symval & 1);
2472 return symval;
2473}
2474
14daeee3
RS
2475/* Return true if string [S, E) is a valid register name, storing its
2476 symbol value in *SYMVAL_PTR if so. */
a92713e6
RS
2477
2478static bfd_boolean
14daeee3 2479mips_parse_register_1 (char *s, char *e, unsigned int *symval_ptr)
a92713e6 2480{
707bfff6 2481 char save_c;
14daeee3 2482 symbolS *symbol;
707bfff6
TS
2483
2484 /* Terminate name. */
2485 save_c = *e;
2486 *e = '\0';
2487
a92713e6
RS
2488 /* Look up the name. */
2489 symbol = symbol_find (s);
2490 *e = save_c;
2491
2492 if (!symbol || S_GET_SEGMENT (symbol) != reg_section)
2493 return FALSE;
2494
14daeee3
RS
2495 *symval_ptr = S_GET_VALUE (symbol);
2496 return TRUE;
2497}
2498
2499/* Return true if the string at *SPTR is a valid register name. Allow it
2500 to have a VU0-style channel suffix of the form x?y?z?w? if CHANNELS_PTR
2501 is nonnull.
2502
2503 When returning true, move *SPTR past the register, store the
2504 register's symbol value in *SYMVAL_PTR and the channel mask in
2505 *CHANNELS_PTR (if nonnull). The symbol value includes the register
2506 number (RNUM_MASK) and register type (RTYPE_MASK). The channel mask
2507 is a 4-bit value of the form XYZW and is 0 if no suffix was given. */
2508
2509static bfd_boolean
2510mips_parse_register (char **sptr, unsigned int *symval_ptr,
2511 unsigned int *channels_ptr)
2512{
2513 char *s, *e, *m;
2514 const char *q;
2515 unsigned int channels, symval, bit;
2516
2517 /* Find end of name. */
2518 s = e = *sptr;
2519 if (is_name_beginner (*e))
2520 ++e;
2521 while (is_part_of_name (*e))
2522 ++e;
2523
2524 channels = 0;
2525 if (!mips_parse_register_1 (s, e, &symval))
2526 {
2527 if (!channels_ptr)
2528 return FALSE;
2529
2530 /* Eat characters from the end of the string that are valid
2531 channel suffixes. The preceding register must be $ACC or
2532 end with a digit, so there is no ambiguity. */
2533 bit = 1;
2534 m = e;
2535 for (q = "wzyx"; *q; q++, bit <<= 1)
2536 if (m > s && m[-1] == *q)
2537 {
2538 --m;
2539 channels |= bit;
2540 }
2541
2542 if (channels == 0
2543 || !mips_parse_register_1 (s, m, &symval)
2544 || (symval & (RTYPE_VI | RTYPE_VF | RTYPE_R5900_ACC)) == 0)
2545 return FALSE;
2546 }
2547
a92713e6 2548 *sptr = e;
14daeee3
RS
2549 *symval_ptr = symval;
2550 if (channels_ptr)
2551 *channels_ptr = channels;
a92713e6
RS
2552 return TRUE;
2553}
2554
2555/* Check if SPTR points at a valid register specifier according to TYPES.
2556 If so, then return 1, advance S to consume the specifier and store
2557 the register's number in REGNOP, otherwise return 0. */
2558
2559static int
2560reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2561{
2562 unsigned int regno;
2563
14daeee3 2564 if (mips_parse_register (s, &regno, NULL))
707bfff6 2565 {
a92713e6
RS
2566 if (types & RTYPE_VEC)
2567 regno = mips_prefer_vec_regno (regno);
2568 if (regno & types)
2569 regno &= RNUM_MASK;
2570 else
2571 regno = ~0;
707bfff6 2572 }
a92713e6 2573 else
707bfff6 2574 {
a92713e6
RS
2575 if (types & RWARN)
2576 as_warn (_("Unrecognized register name `%s'"), *s);
2577 regno = ~0;
707bfff6 2578 }
707bfff6 2579 if (regnop)
a92713e6
RS
2580 *regnop = regno;
2581 return regno <= RNUM_MASK;
707bfff6
TS
2582}
2583
14daeee3
RS
2584/* Parse a VU0 "x?y?z?w?" channel mask at S and store the associated
2585 mask in *CHANNELS. Return a pointer to the first unconsumed character. */
2586
2587static char *
2588mips_parse_vu0_channels (char *s, unsigned int *channels)
2589{
2590 unsigned int i;
2591
2592 *channels = 0;
2593 for (i = 0; i < 4; i++)
2594 if (*s == "xyzw"[i])
2595 {
2596 *channels |= 1 << (3 - i);
2597 ++s;
2598 }
2599 return s;
2600}
2601
a92713e6
RS
2602/* Token types for parsed operand lists. */
2603enum mips_operand_token_type {
2604 /* A plain register, e.g. $f2. */
2605 OT_REG,
df58fc94 2606
14daeee3
RS
2607 /* A 4-bit XYZW channel mask. */
2608 OT_CHANNELS,
2609
a92713e6
RS
2610 /* An element of a vector, e.g. $v0[1]. */
2611 OT_REG_ELEMENT,
df58fc94 2612
a92713e6
RS
2613 /* A continuous range of registers, e.g. $s0-$s4. */
2614 OT_REG_RANGE,
2615
2616 /* A (possibly relocated) expression. */
2617 OT_INTEGER,
2618
2619 /* A floating-point value. */
2620 OT_FLOAT,
2621
2622 /* A single character. This can be '(', ')' or ',', but '(' only appears
2623 before OT_REGs. */
2624 OT_CHAR,
2625
14daeee3
RS
2626 /* A doubled character, either "--" or "++". */
2627 OT_DOUBLE_CHAR,
2628
a92713e6
RS
2629 /* The end of the operand list. */
2630 OT_END
2631};
2632
2633/* A parsed operand token. */
2634struct mips_operand_token
2635{
2636 /* The type of token. */
2637 enum mips_operand_token_type type;
2638 union
2639 {
2640 /* The register symbol value for an OT_REG. */
2641 unsigned int regno;
2642
14daeee3
RS
2643 /* The 4-bit channel mask for an OT_CHANNEL_SUFFIX. */
2644 unsigned int channels;
2645
a92713e6
RS
2646 /* The register symbol value and index for an OT_REG_ELEMENT. */
2647 struct {
2648 unsigned int regno;
2649 addressT index;
2650 } reg_element;
2651
2652 /* The two register symbol values involved in an OT_REG_RANGE. */
2653 struct {
2654 unsigned int regno1;
2655 unsigned int regno2;
2656 } reg_range;
2657
2658 /* The value of an OT_INTEGER. The value is represented as an
2659 expression and the relocation operators that were applied to
2660 that expression. The reloc entries are BFD_RELOC_UNUSED if no
2661 relocation operators were used. */
2662 struct {
2663 expressionS value;
2664 bfd_reloc_code_real_type relocs[3];
2665 } integer;
2666
2667 /* The binary data for an OT_FLOAT constant, and the number of bytes
2668 in the constant. */
2669 struct {
2670 unsigned char data[8];
2671 int length;
2672 } flt;
2673
14daeee3 2674 /* The character represented by an OT_CHAR or OT_DOUBLE_CHAR. */
a92713e6
RS
2675 char ch;
2676 } u;
2677};
2678
2679/* An obstack used to construct lists of mips_operand_tokens. */
2680static struct obstack mips_operand_tokens;
2681
2682/* Give TOKEN type TYPE and add it to mips_operand_tokens. */
2683
2684static void
2685mips_add_token (struct mips_operand_token *token,
2686 enum mips_operand_token_type type)
2687{
2688 token->type = type;
2689 obstack_grow (&mips_operand_tokens, token, sizeof (*token));
2690}
2691
2692/* Check whether S is '(' followed by a register name. Add OT_CHAR
2693 and OT_REG tokens for them if so, and return a pointer to the first
2694 unconsumed character. Return null otherwise. */
2695
2696static char *
2697mips_parse_base_start (char *s)
2698{
2699 struct mips_operand_token token;
14daeee3
RS
2700 unsigned int regno, channels;
2701 bfd_boolean decrement_p;
df58fc94 2702
a92713e6
RS
2703 if (*s != '(')
2704 return 0;
2705
2706 ++s;
2707 SKIP_SPACE_TABS (s);
14daeee3
RS
2708
2709 /* Only match "--" as part of a base expression. In other contexts "--X"
2710 is a double negative. */
2711 decrement_p = (s[0] == '-' && s[1] == '-');
2712 if (decrement_p)
2713 {
2714 s += 2;
2715 SKIP_SPACE_TABS (s);
2716 }
2717
2718 /* Allow a channel specifier because that leads to better error messages
2719 than treating something like "$vf0x++" as an expression. */
2720 if (!mips_parse_register (&s, &regno, &channels))
a92713e6
RS
2721 return 0;
2722
2723 token.u.ch = '(';
2724 mips_add_token (&token, OT_CHAR);
2725
14daeee3
RS
2726 if (decrement_p)
2727 {
2728 token.u.ch = '-';
2729 mips_add_token (&token, OT_DOUBLE_CHAR);
2730 }
2731
a92713e6
RS
2732 token.u.regno = regno;
2733 mips_add_token (&token, OT_REG);
2734
14daeee3
RS
2735 if (channels)
2736 {
2737 token.u.channels = channels;
2738 mips_add_token (&token, OT_CHANNELS);
2739 }
2740
2741 /* For consistency, only match "++" as part of base expressions too. */
2742 SKIP_SPACE_TABS (s);
2743 if (s[0] == '+' && s[1] == '+')
2744 {
2745 s += 2;
2746 token.u.ch = '+';
2747 mips_add_token (&token, OT_DOUBLE_CHAR);
2748 }
2749
a92713e6
RS
2750 return s;
2751}
2752
2753/* Parse one or more tokens from S. Return a pointer to the first
2754 unconsumed character on success. Return null if an error was found
2755 and store the error text in insn_error. FLOAT_FORMAT is as for
2756 mips_parse_arguments. */
2757
2758static char *
2759mips_parse_argument_token (char *s, char float_format)
2760{
2761 char *end, *save_in, *err;
14daeee3 2762 unsigned int regno1, regno2, channels;
a92713e6
RS
2763 struct mips_operand_token token;
2764
2765 /* First look for "($reg", since we want to treat that as an
2766 OT_CHAR and OT_REG rather than an expression. */
2767 end = mips_parse_base_start (s);
2768 if (end)
2769 return end;
2770
2771 /* Handle other characters that end up as OT_CHARs. */
2772 if (*s == ')' || *s == ',')
2773 {
2774 token.u.ch = *s;
2775 mips_add_token (&token, OT_CHAR);
2776 ++s;
2777 return s;
2778 }
2779
2780 /* Handle tokens that start with a register. */
14daeee3 2781 if (mips_parse_register (&s, &regno1, &channels))
df58fc94 2782 {
14daeee3
RS
2783 if (channels)
2784 {
2785 /* A register and a VU0 channel suffix. */
2786 token.u.regno = regno1;
2787 mips_add_token (&token, OT_REG);
2788
2789 token.u.channels = channels;
2790 mips_add_token (&token, OT_CHANNELS);
2791 return s;
2792 }
2793
a92713e6
RS
2794 SKIP_SPACE_TABS (s);
2795 if (*s == '-')
df58fc94 2796 {
a92713e6
RS
2797 /* A register range. */
2798 ++s;
2799 SKIP_SPACE_TABS (s);
14daeee3 2800 if (!mips_parse_register (&s, &regno2, NULL))
a92713e6
RS
2801 {
2802 insn_error = _("Invalid register range");
2803 return 0;
2804 }
df58fc94 2805
a92713e6
RS
2806 token.u.reg_range.regno1 = regno1;
2807 token.u.reg_range.regno2 = regno2;
2808 mips_add_token (&token, OT_REG_RANGE);
2809 return s;
2810 }
2811 else if (*s == '[')
df58fc94 2812 {
a92713e6
RS
2813 /* A vector element. */
2814 expressionS element;
2815
2816 ++s;
2817 SKIP_SPACE_TABS (s);
2818 my_getExpression (&element, s);
2819 if (element.X_op != O_constant)
2820 {
2821 insn_error = _("Vector element must be constant");
2822 return 0;
2823 }
2824 s = expr_end;
2825 SKIP_SPACE_TABS (s);
2826 if (*s != ']')
2827 {
2828 insn_error = _("Missing `]'");
2829 return 0;
2830 }
2831 ++s;
2832
2833 token.u.reg_element.regno = regno1;
2834 token.u.reg_element.index = element.X_add_number;
2835 mips_add_token (&token, OT_REG_ELEMENT);
2836 return s;
df58fc94 2837 }
df58fc94 2838
a92713e6
RS
2839 /* Looks like just a plain register. */
2840 token.u.regno = regno1;
2841 mips_add_token (&token, OT_REG);
2842 return s;
df58fc94
RS
2843 }
2844
a92713e6
RS
2845 if (float_format)
2846 {
2847 /* First try to treat expressions as floats. */
2848 save_in = input_line_pointer;
2849 input_line_pointer = s;
2850 err = md_atof (float_format, (char *) token.u.flt.data,
2851 &token.u.flt.length);
2852 end = input_line_pointer;
2853 input_line_pointer = save_in;
2854 if (err && *err)
2855 {
2856 insn_error = err;
2857 return 0;
2858 }
2859 if (s != end)
2860 {
2861 mips_add_token (&token, OT_FLOAT);
2862 return end;
2863 }
2864 }
2865
2866 /* Treat everything else as an integer expression. */
2867 token.u.integer.relocs[0] = BFD_RELOC_UNUSED;
2868 token.u.integer.relocs[1] = BFD_RELOC_UNUSED;
2869 token.u.integer.relocs[2] = BFD_RELOC_UNUSED;
2870 my_getSmallExpression (&token.u.integer.value, token.u.integer.relocs, s);
2871 s = expr_end;
2872 mips_add_token (&token, OT_INTEGER);
2873 return s;
2874}
2875
2876/* S points to the operand list for an instruction. FLOAT_FORMAT is 'f'
2877 if expressions should be treated as 32-bit floating-point constants,
2878 'd' if they should be treated as 64-bit floating-point constants,
2879 or 0 if they should be treated as integer expressions (the usual case).
2880
2881 Return a list of tokens on success, otherwise return 0. The caller
2882 must obstack_free the list after use. */
2883
2884static struct mips_operand_token *
2885mips_parse_arguments (char *s, char float_format)
2886{
2887 struct mips_operand_token token;
2888
2889 SKIP_SPACE_TABS (s);
2890 while (*s)
2891 {
2892 s = mips_parse_argument_token (s, float_format);
2893 if (!s)
2894 {
2895 obstack_free (&mips_operand_tokens,
2896 obstack_finish (&mips_operand_tokens));
2897 return 0;
2898 }
2899 SKIP_SPACE_TABS (s);
2900 }
2901 mips_add_token (&token, OT_END);
2902 return (struct mips_operand_token *) obstack_finish (&mips_operand_tokens);
df58fc94
RS
2903}
2904
d301a56b
RS
2905/* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
2906 and architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
037b32b9
AN
2907
2908static bfd_boolean
f79e2745 2909is_opcode_valid (const struct mips_opcode *mo)
037b32b9
AN
2910{
2911 int isa = mips_opts.isa;
846ef2d0 2912 int ase = mips_opts.ase;
037b32b9 2913 int fp_s, fp_d;
c6278170 2914 unsigned int i;
037b32b9 2915
c6278170
RS
2916 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
2917 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2918 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
2919 ase |= mips_ases[i].flags64;
037b32b9 2920
d301a56b 2921 if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
037b32b9
AN
2922 return FALSE;
2923
2924 /* Check whether the instruction or macro requires single-precision or
2925 double-precision floating-point support. Note that this information is
2926 stored differently in the opcode table for insns and macros. */
2927 if (mo->pinfo == INSN_MACRO)
2928 {
2929 fp_s = mo->pinfo2 & INSN2_M_FP_S;
2930 fp_d = mo->pinfo2 & INSN2_M_FP_D;
2931 }
2932 else
2933 {
2934 fp_s = mo->pinfo & FP_S;
2935 fp_d = mo->pinfo & FP_D;
2936 }
2937
2938 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
2939 return FALSE;
2940
2941 if (fp_s && mips_opts.soft_float)
2942 return FALSE;
2943
2944 return TRUE;
2945}
2946
2947/* Return TRUE if the MIPS16 opcode MO is valid on the currently
2948 selected ISA and architecture. */
2949
2950static bfd_boolean
2951is_opcode_valid_16 (const struct mips_opcode *mo)
2952{
d301a56b 2953 return opcode_is_member (mo, mips_opts.isa, 0, mips_opts.arch);
037b32b9
AN
2954}
2955
df58fc94
RS
2956/* Return TRUE if the size of the microMIPS opcode MO matches one
2957 explicitly requested. Always TRUE in the standard MIPS mode. */
2958
2959static bfd_boolean
2960is_size_valid (const struct mips_opcode *mo)
2961{
2962 if (!mips_opts.micromips)
2963 return TRUE;
2964
833794fc
MR
2965 if (mips_opts.insn32)
2966 {
2967 if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
2968 return FALSE;
2969 if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
2970 return FALSE;
2971 }
df58fc94
RS
2972 if (!forced_insn_length)
2973 return TRUE;
2974 if (mo->pinfo == INSN_MACRO)
2975 return FALSE;
2976 return forced_insn_length == micromips_insn_length (mo);
2977}
2978
2979/* Return TRUE if the microMIPS opcode MO is valid for the delay slot
e64af278
MR
2980 of the preceding instruction. Always TRUE in the standard MIPS mode.
2981
2982 We don't accept macros in 16-bit delay slots to avoid a case where
2983 a macro expansion fails because it relies on a preceding 32-bit real
2984 instruction to have matched and does not handle the operands correctly.
2985 The only macros that may expand to 16-bit instructions are JAL that
2986 cannot be placed in a delay slot anyway, and corner cases of BALIGN
2987 and BGT (that likewise cannot be placed in a delay slot) that decay to
2988 a NOP. In all these cases the macros precede any corresponding real
2989 instruction definitions in the opcode table, so they will match in the
2990 second pass where the size of the delay slot is ignored and therefore
2991 produce correct code. */
df58fc94
RS
2992
2993static bfd_boolean
2994is_delay_slot_valid (const struct mips_opcode *mo)
2995{
2996 if (!mips_opts.micromips)
2997 return TRUE;
2998
2999 if (mo->pinfo == INSN_MACRO)
c06dec14 3000 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
df58fc94
RS
3001 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
3002 && micromips_insn_length (mo) != 4)
3003 return FALSE;
3004 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
3005 && micromips_insn_length (mo) != 2)
3006 return FALSE;
3007
3008 return TRUE;
3009}
3010
fc76e730
RS
3011/* For consistency checking, verify that all bits of OPCODE are specified
3012 either by the match/mask part of the instruction definition, or by the
3013 operand list. Also build up a list of operands in OPERANDS.
3014
3015 INSN_BITS says which bits of the instruction are significant.
3016 If OPCODE is a standard or microMIPS instruction, DECODE_OPERAND
3017 provides the mips_operand description of each operand. DECODE_OPERAND
3018 is null for MIPS16 instructions. */
ab902481
RS
3019
3020static int
3021validate_mips_insn (const struct mips_opcode *opcode,
3022 unsigned long insn_bits,
fc76e730
RS
3023 const struct mips_operand *(*decode_operand) (const char *),
3024 struct mips_operand_array *operands)
ab902481
RS
3025{
3026 const char *s;
fc76e730 3027 unsigned long used_bits, doubled, undefined, opno, mask;
ab902481
RS
3028 const struct mips_operand *operand;
3029
fc76e730
RS
3030 mask = (opcode->pinfo == INSN_MACRO ? 0 : opcode->mask);
3031 if ((mask & opcode->match) != opcode->match)
ab902481
RS
3032 {
3033 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
3034 opcode->name, opcode->args);
3035 return 0;
3036 }
3037 used_bits = 0;
fc76e730 3038 opno = 0;
14daeee3
RS
3039 if (opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX)
3040 used_bits = mips_insert_operand (&mips_vu0_channel_mask, used_bits, -1);
ab902481
RS
3041 for (s = opcode->args; *s; ++s)
3042 switch (*s)
3043 {
3044 case ',':
3045 case '(':
3046 case ')':
3047 break;
3048
14daeee3
RS
3049 case '#':
3050 s++;
3051 break;
3052
ab902481 3053 default:
fc76e730
RS
3054 if (!decode_operand)
3055 operand = decode_mips16_operand (*s, FALSE);
3056 else
3057 operand = decode_operand (s);
3058 if (!operand && opcode->pinfo != INSN_MACRO)
ab902481
RS
3059 {
3060 as_bad (_("internal: unknown operand type: %s %s"),
3061 opcode->name, opcode->args);
3062 return 0;
3063 }
fc76e730
RS
3064 gas_assert (opno < MAX_OPERANDS);
3065 operands->operand[opno] = operand;
14daeee3 3066 if (operand && operand->type != OP_VU0_MATCH_SUFFIX)
fc76e730 3067 {
14daeee3 3068 used_bits = mips_insert_operand (operand, used_bits, -1);
fc76e730
RS
3069 if (operand->type == OP_MDMX_IMM_REG)
3070 /* Bit 5 is the format selector (OB vs QH). The opcode table
3071 has separate entries for each format. */
3072 used_bits &= ~(1 << (operand->lsb + 5));
3073 if (operand->type == OP_ENTRY_EXIT_LIST)
3074 used_bits &= ~(mask & 0x700);
3075 }
ab902481 3076 /* Skip prefix characters. */
fc76e730 3077 if (decode_operand && (*s == '+' || *s == 'm'))
ab902481 3078 ++s;
fc76e730 3079 opno += 1;
ab902481
RS
3080 break;
3081 }
fc76e730 3082 doubled = used_bits & mask & insn_bits;
ab902481
RS
3083 if (doubled)
3084 {
3085 as_bad (_("internal: bad mips opcode (bits 0x%08lx doubly defined):"
3086 " %s %s"), doubled, opcode->name, opcode->args);
3087 return 0;
3088 }
fc76e730 3089 used_bits |= mask;
ab902481 3090 undefined = ~used_bits & insn_bits;
fc76e730 3091 if (opcode->pinfo != INSN_MACRO && undefined)
ab902481
RS
3092 {
3093 as_bad (_("internal: bad mips opcode (bits 0x%08lx undefined): %s %s"),
3094 undefined, opcode->name, opcode->args);
3095 return 0;
3096 }
3097 used_bits &= ~insn_bits;
3098 if (used_bits)
3099 {
3100 as_bad (_("internal: bad mips opcode (bits 0x%08lx defined): %s %s"),
3101 used_bits, opcode->name, opcode->args);
3102 return 0;
3103 }
3104 return 1;
3105}
3106
fc76e730
RS
3107/* The MIPS16 version of validate_mips_insn. */
3108
3109static int
3110validate_mips16_insn (const struct mips_opcode *opcode,
3111 struct mips_operand_array *operands)
3112{
3113 if (opcode->args[0] == 'a' || opcode->args[0] == 'i')
3114 {
3115 /* In this case OPCODE defines the first 16 bits in a 32-bit jump
3116 instruction. Use TMP to describe the full instruction. */
3117 struct mips_opcode tmp;
3118
3119 tmp = *opcode;
3120 tmp.match <<= 16;
3121 tmp.mask <<= 16;
3122 return validate_mips_insn (&tmp, 0xffffffff, 0, operands);
3123 }
3124 return validate_mips_insn (opcode, 0xffff, 0, operands);
3125}
3126
ab902481
RS
3127/* The microMIPS version of validate_mips_insn. */
3128
3129static int
fc76e730
RS
3130validate_micromips_insn (const struct mips_opcode *opc,
3131 struct mips_operand_array *operands)
ab902481
RS
3132{
3133 unsigned long insn_bits;
3134 unsigned long major;
3135 unsigned int length;
3136
fc76e730
RS
3137 if (opc->pinfo == INSN_MACRO)
3138 return validate_mips_insn (opc, 0xffffffff, decode_micromips_operand,
3139 operands);
3140
ab902481
RS
3141 length = micromips_insn_length (opc);
3142 if (length != 2 && length != 4)
3143 {
3144 as_bad (_("Internal error: bad microMIPS opcode (incorrect length: %u): "
3145 "%s %s"), length, opc->name, opc->args);
3146 return 0;
3147 }
3148 major = opc->match >> (10 + 8 * (length - 2));
3149 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
3150 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
3151 {
3152 as_bad (_("Internal error: bad microMIPS opcode "
3153 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
3154 return 0;
3155 }
3156
3157 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
3158 insn_bits = 1 << 4 * length;
3159 insn_bits <<= 4 * length;
3160 insn_bits -= 1;
fc76e730
RS
3161 return validate_mips_insn (opc, insn_bits, decode_micromips_operand,
3162 operands);
ab902481
RS
3163}
3164
707bfff6
TS
3165/* This function is called once, at assembler startup time. It should set up
3166 all the tables, etc. that the MD part of the assembler will need. */
156c2f8b 3167
252b5132 3168void
17a2f251 3169md_begin (void)
252b5132 3170{
3994f87e 3171 const char *retval = NULL;
156c2f8b 3172 int i = 0;
252b5132 3173 int broken = 0;
1f25f5d3 3174
0a44bf69
RS
3175 if (mips_pic != NO_PIC)
3176 {
3177 if (g_switch_seen && g_switch_value != 0)
3178 as_bad (_("-G may not be used in position-independent code"));
3179 g_switch_value = 0;
3180 }
3181
fef14a42 3182 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
252b5132
RH
3183 as_warn (_("Could not set architecture and machine"));
3184
252b5132
RH
3185 op_hash = hash_new ();
3186
fc76e730 3187 mips_operands = XCNEWVEC (struct mips_operand_array, NUMOPCODES);
252b5132
RH
3188 for (i = 0; i < NUMOPCODES;)
3189 {
3190 const char *name = mips_opcodes[i].name;
3191
17a2f251 3192 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
3193 if (retval != NULL)
3194 {
3195 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
3196 mips_opcodes[i].name, retval);
3197 /* Probably a memory allocation problem? Give up now. */
3198 as_fatal (_("Broken assembler. No assembly attempted."));
3199 }
3200 do
3201 {
fc76e730
RS
3202 if (!validate_mips_insn (&mips_opcodes[i], 0xffffffff,
3203 decode_mips_operand, &mips_operands[i]))
3204 broken = 1;
3205 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
252b5132 3206 {
fc76e730
RS
3207 create_insn (&nop_insn, mips_opcodes + i);
3208 if (mips_fix_loongson2f_nop)
3209 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
3210 nop_insn.fixed_p = 1;
252b5132
RH
3211 }
3212 ++i;
3213 }
3214 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
3215 }
3216
3217 mips16_op_hash = hash_new ();
fc76e730
RS
3218 mips16_operands = XCNEWVEC (struct mips_operand_array,
3219 bfd_mips16_num_opcodes);
252b5132
RH
3220
3221 i = 0;
3222 while (i < bfd_mips16_num_opcodes)
3223 {
3224 const char *name = mips16_opcodes[i].name;
3225
17a2f251 3226 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
3227 if (retval != NULL)
3228 as_fatal (_("internal: can't hash `%s': %s"),
3229 mips16_opcodes[i].name, retval);
3230 do
3231 {
fc76e730
RS
3232 if (!validate_mips16_insn (&mips16_opcodes[i], &mips16_operands[i]))
3233 broken = 1;
1e915849
RS
3234 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3235 {
3236 create_insn (&mips16_nop_insn, mips16_opcodes + i);
3237 mips16_nop_insn.fixed_p = 1;
3238 }
252b5132
RH
3239 ++i;
3240 }
3241 while (i < bfd_mips16_num_opcodes
3242 && strcmp (mips16_opcodes[i].name, name) == 0);
3243 }
3244
df58fc94 3245 micromips_op_hash = hash_new ();
fc76e730
RS
3246 micromips_operands = XCNEWVEC (struct mips_operand_array,
3247 bfd_micromips_num_opcodes);
df58fc94
RS
3248
3249 i = 0;
3250 while (i < bfd_micromips_num_opcodes)
3251 {
3252 const char *name = micromips_opcodes[i].name;
3253
3254 retval = hash_insert (micromips_op_hash, name,
3255 (void *) &micromips_opcodes[i]);
3256 if (retval != NULL)
3257 as_fatal (_("internal: can't hash `%s': %s"),
3258 micromips_opcodes[i].name, retval);
3259 do
fc76e730
RS
3260 {
3261 struct mips_cl_insn *micromips_nop_insn;
3262
3263 if (!validate_micromips_insn (&micromips_opcodes[i],
3264 &micromips_operands[i]))
3265 broken = 1;
3266
3267 if (micromips_opcodes[i].pinfo != INSN_MACRO)
3268 {
3269 if (micromips_insn_length (micromips_opcodes + i) == 2)
3270 micromips_nop_insn = &micromips_nop16_insn;
3271 else if (micromips_insn_length (micromips_opcodes + i) == 4)
3272 micromips_nop_insn = &micromips_nop32_insn;
3273 else
3274 continue;
3275
3276 if (micromips_nop_insn->insn_mo == NULL
3277 && strcmp (name, "nop") == 0)
3278 {
3279 create_insn (micromips_nop_insn, micromips_opcodes + i);
3280 micromips_nop_insn->fixed_p = 1;
3281 }
3282 }
3283 }
df58fc94
RS
3284 while (++i < bfd_micromips_num_opcodes
3285 && strcmp (micromips_opcodes[i].name, name) == 0);
3286 }
3287
252b5132
RH
3288 if (broken)
3289 as_fatal (_("Broken assembler. No assembly attempted."));
3290
3291 /* We add all the general register names to the symbol table. This
3292 helps us detect invalid uses of them. */
707bfff6
TS
3293 for (i = 0; reg_names[i].name; i++)
3294 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
8fc4ee9b 3295 reg_names[i].num, /* & RNUM_MASK, */
707bfff6
TS
3296 &zero_address_frag));
3297 if (HAVE_NEWABI)
3298 for (i = 0; reg_names_n32n64[i].name; i++)
3299 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
8fc4ee9b 3300 reg_names_n32n64[i].num, /* & RNUM_MASK, */
252b5132 3301 &zero_address_frag));
707bfff6
TS
3302 else
3303 for (i = 0; reg_names_o32[i].name; i++)
3304 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
8fc4ee9b 3305 reg_names_o32[i].num, /* & RNUM_MASK, */
6047c971 3306 &zero_address_frag));
6047c971 3307
14daeee3
RS
3308 for (i = 0; i < 32; i++)
3309 {
3310 char regname[7];
3311
3312 /* R5900 VU0 floating-point register. */
3313 regname[sizeof (rename) - 1] = 0;
3314 snprintf (regname, sizeof (regname) - 1, "$vf%d", i);
3315 symbol_table_insert (symbol_new (regname, reg_section,
3316 RTYPE_VF | i, &zero_address_frag));
3317
3318 /* R5900 VU0 integer register. */
3319 snprintf (regname, sizeof (regname) - 1, "$vi%d", i);
3320 symbol_table_insert (symbol_new (regname, reg_section,
3321 RTYPE_VI | i, &zero_address_frag));
3322
3323 }
3324
a92713e6
RS
3325 obstack_init (&mips_operand_tokens);
3326
7d10b47d 3327 mips_no_prev_insn ();
252b5132
RH
3328
3329 mips_gprmask = 0;
3330 mips_cprmask[0] = 0;
3331 mips_cprmask[1] = 0;
3332 mips_cprmask[2] = 0;
3333 mips_cprmask[3] = 0;
3334
3335 /* set the default alignment for the text section (2**2) */
3336 record_alignment (text_section, 2);
3337
4d0d148d 3338 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132 3339
f3ded42a
RS
3340 /* On a native system other than VxWorks, sections must be aligned
3341 to 16 byte boundaries. When configured for an embedded ELF
3342 target, we don't bother. */
3343 if (strncmp (TARGET_OS, "elf", 3) != 0
3344 && strncmp (TARGET_OS, "vxworks", 7) != 0)
252b5132 3345 {
f3ded42a
RS
3346 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
3347 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
3348 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
3349 }
252b5132 3350
f3ded42a
RS
3351 /* Create a .reginfo section for register masks and a .mdebug
3352 section for debugging information. */
3353 {
3354 segT seg;
3355 subsegT subseg;
3356 flagword flags;
3357 segT sec;
3358
3359 seg = now_seg;
3360 subseg = now_subseg;
3361
3362 /* The ABI says this section should be loaded so that the
3363 running program can access it. However, we don't load it
3364 if we are configured for an embedded target */
3365 flags = SEC_READONLY | SEC_DATA;
3366 if (strncmp (TARGET_OS, "elf", 3) != 0)
3367 flags |= SEC_ALLOC | SEC_LOAD;
3368
3369 if (mips_abi != N64_ABI)
252b5132 3370 {
f3ded42a 3371 sec = subseg_new (".reginfo", (subsegT) 0);
bdaaa2e1 3372
f3ded42a
RS
3373 bfd_set_section_flags (stdoutput, sec, flags);
3374 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
252b5132 3375
f3ded42a
RS
3376 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
3377 }
3378 else
3379 {
3380 /* The 64-bit ABI uses a .MIPS.options section rather than
3381 .reginfo section. */
3382 sec = subseg_new (".MIPS.options", (subsegT) 0);
3383 bfd_set_section_flags (stdoutput, sec, flags);
3384 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132 3385
f3ded42a
RS
3386 /* Set up the option header. */
3387 {
3388 Elf_Internal_Options opthdr;
3389 char *f;
3390
3391 opthdr.kind = ODK_REGINFO;
3392 opthdr.size = (sizeof (Elf_External_Options)
3393 + sizeof (Elf64_External_RegInfo));
3394 opthdr.section = 0;
3395 opthdr.info = 0;
3396 f = frag_more (sizeof (Elf_External_Options));
3397 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
3398 (Elf_External_Options *) f);
3399
3400 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
3401 }
3402 }
252b5132 3403
f3ded42a
RS
3404 if (ECOFF_DEBUGGING)
3405 {
3406 sec = subseg_new (".mdebug", (subsegT) 0);
3407 (void) bfd_set_section_flags (stdoutput, sec,
3408 SEC_HAS_CONTENTS | SEC_READONLY);
3409 (void) bfd_set_section_alignment (stdoutput, sec, 2);
252b5132 3410 }
f3ded42a
RS
3411 else if (mips_flag_pdr)
3412 {
3413 pdr_seg = subseg_new (".pdr", (subsegT) 0);
3414 (void) bfd_set_section_flags (stdoutput, pdr_seg,
3415 SEC_READONLY | SEC_RELOC
3416 | SEC_DEBUGGING);
3417 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
3418 }
3419
3420 subseg_set (seg, subseg);
3421 }
252b5132
RH
3422
3423 if (! ECOFF_DEBUGGING)
3424 md_obj_begin ();
71400594
RS
3425
3426 if (mips_fix_vr4120)
3427 init_vr4120_conflicts ();
252b5132
RH
3428}
3429
3430void
17a2f251 3431md_mips_end (void)
252b5132 3432{
02b1ab82 3433 mips_emit_delays ();
252b5132
RH
3434 if (! ECOFF_DEBUGGING)
3435 md_obj_end ();
3436}
3437
3438void
17a2f251 3439md_assemble (char *str)
252b5132
RH
3440{
3441 struct mips_cl_insn insn;
f6688943
TS
3442 bfd_reloc_code_real_type unused_reloc[3]
3443 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
3444
3445 imm_expr.X_op = O_absent;
5f74bc13 3446 imm2_expr.X_op = O_absent;
252b5132 3447 offset_expr.X_op = O_absent;
f6688943
TS
3448 offset_reloc[0] = BFD_RELOC_UNUSED;
3449 offset_reloc[1] = BFD_RELOC_UNUSED;
3450 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132 3451
e1b47bd5
RS
3452 mips_mark_labels ();
3453 mips_assembling_insn = TRUE;
3454
252b5132
RH
3455 if (mips_opts.mips16)
3456 mips16_ip (str, &insn);
3457 else
3458 {
3459 mips_ip (str, &insn);
beae10d5
KH
3460 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
3461 str, insn.insn_opcode));
252b5132
RH
3462 }
3463
3464 if (insn_error)
e1b47bd5
RS
3465 as_bad ("%s `%s'", insn_error, str);
3466 else if (insn.insn_mo->pinfo == INSN_MACRO)
252b5132 3467 {
584892a6 3468 macro_start ();
252b5132
RH
3469 if (mips_opts.mips16)
3470 mips16_macro (&insn);
3471 else
833794fc 3472 macro (&insn, str);
584892a6 3473 macro_end ();
252b5132
RH
3474 }
3475 else
3476 {
77bd4346 3477 if (offset_expr.X_op != O_absent)
df58fc94 3478 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
252b5132 3479 else
df58fc94 3480 append_insn (&insn, NULL, unused_reloc, FALSE);
252b5132 3481 }
e1b47bd5
RS
3482
3483 mips_assembling_insn = FALSE;
252b5132
RH
3484}
3485
738e5348
RS
3486/* Convenience functions for abstracting away the differences between
3487 MIPS16 and non-MIPS16 relocations. */
3488
3489static inline bfd_boolean
3490mips16_reloc_p (bfd_reloc_code_real_type reloc)
3491{
3492 switch (reloc)
3493 {
3494 case BFD_RELOC_MIPS16_JMP:
3495 case BFD_RELOC_MIPS16_GPREL:
3496 case BFD_RELOC_MIPS16_GOT16:
3497 case BFD_RELOC_MIPS16_CALL16:
3498 case BFD_RELOC_MIPS16_HI16_S:
3499 case BFD_RELOC_MIPS16_HI16:
3500 case BFD_RELOC_MIPS16_LO16:
3501 return TRUE;
3502
3503 default:
3504 return FALSE;
3505 }
3506}
3507
df58fc94
RS
3508static inline bfd_boolean
3509micromips_reloc_p (bfd_reloc_code_real_type reloc)
3510{
3511 switch (reloc)
3512 {
3513 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
3514 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
3515 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
3516 case BFD_RELOC_MICROMIPS_GPREL16:
3517 case BFD_RELOC_MICROMIPS_JMP:
3518 case BFD_RELOC_MICROMIPS_HI16:
3519 case BFD_RELOC_MICROMIPS_HI16_S:
3520 case BFD_RELOC_MICROMIPS_LO16:
3521 case BFD_RELOC_MICROMIPS_LITERAL:
3522 case BFD_RELOC_MICROMIPS_GOT16:
3523 case BFD_RELOC_MICROMIPS_CALL16:
3524 case BFD_RELOC_MICROMIPS_GOT_HI16:
3525 case BFD_RELOC_MICROMIPS_GOT_LO16:
3526 case BFD_RELOC_MICROMIPS_CALL_HI16:
3527 case BFD_RELOC_MICROMIPS_CALL_LO16:
3528 case BFD_RELOC_MICROMIPS_SUB:
3529 case BFD_RELOC_MICROMIPS_GOT_PAGE:
3530 case BFD_RELOC_MICROMIPS_GOT_OFST:
3531 case BFD_RELOC_MICROMIPS_GOT_DISP:
3532 case BFD_RELOC_MICROMIPS_HIGHEST:
3533 case BFD_RELOC_MICROMIPS_HIGHER:
3534 case BFD_RELOC_MICROMIPS_SCN_DISP:
3535 case BFD_RELOC_MICROMIPS_JALR:
3536 return TRUE;
3537
3538 default:
3539 return FALSE;
3540 }
3541}
3542
2309ddf2
MR
3543static inline bfd_boolean
3544jmp_reloc_p (bfd_reloc_code_real_type reloc)
3545{
3546 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
3547}
3548
738e5348
RS
3549static inline bfd_boolean
3550got16_reloc_p (bfd_reloc_code_real_type reloc)
3551{
2309ddf2 3552 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
df58fc94 3553 || reloc == BFD_RELOC_MICROMIPS_GOT16);
738e5348
RS
3554}
3555
3556static inline bfd_boolean
3557hi16_reloc_p (bfd_reloc_code_real_type reloc)
3558{
2309ddf2 3559 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
df58fc94 3560 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
738e5348
RS
3561}
3562
3563static inline bfd_boolean
3564lo16_reloc_p (bfd_reloc_code_real_type reloc)
3565{
2309ddf2 3566 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
df58fc94
RS
3567 || reloc == BFD_RELOC_MICROMIPS_LO16);
3568}
3569
df58fc94
RS
3570static inline bfd_boolean
3571jalr_reloc_p (bfd_reloc_code_real_type reloc)
3572{
2309ddf2 3573 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
738e5348
RS
3574}
3575
f2ae14a1
RS
3576static inline bfd_boolean
3577gprel16_reloc_p (bfd_reloc_code_real_type reloc)
3578{
3579 return (reloc == BFD_RELOC_GPREL16 || reloc == BFD_RELOC_MIPS16_GPREL
3580 || reloc == BFD_RELOC_MICROMIPS_GPREL16);
3581}
3582
2de39019
CM
3583/* Return true if RELOC is a PC-relative relocation that does not have
3584 full address range. */
3585
3586static inline bfd_boolean
3587limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
3588{
3589 switch (reloc)
3590 {
3591 case BFD_RELOC_16_PCREL_S2:
3592 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
3593 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
3594 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
3595 return TRUE;
3596
b47468a6
CM
3597 case BFD_RELOC_32_PCREL:
3598 return HAVE_64BIT_ADDRESSES;
3599
2de39019
CM
3600 default:
3601 return FALSE;
3602 }
3603}
b47468a6 3604
5919d012 3605/* Return true if the given relocation might need a matching %lo().
0a44bf69
RS
3606 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
3607 need a matching %lo() when applied to local symbols. */
5919d012
RS
3608
3609static inline bfd_boolean
17a2f251 3610reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 3611{
3b91255e 3612 return (HAVE_IN_PLACE_ADDENDS
738e5348 3613 && (hi16_reloc_p (reloc)
0a44bf69
RS
3614 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
3615 all GOT16 relocations evaluate to "G". */
738e5348
RS
3616 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
3617}
3618
3619/* Return the type of %lo() reloc needed by RELOC, given that
3620 reloc_needs_lo_p. */
3621
3622static inline bfd_reloc_code_real_type
3623matching_lo_reloc (bfd_reloc_code_real_type reloc)
3624{
df58fc94
RS
3625 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
3626 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
3627 : BFD_RELOC_LO16));
5919d012
RS
3628}
3629
3630/* Return true if the given fixup is followed by a matching R_MIPS_LO16
3631 relocation. */
3632
3633static inline bfd_boolean
17a2f251 3634fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
3635{
3636 return (fixp->fx_next != NULL
738e5348 3637 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
5919d012
RS
3638 && fixp->fx_addsy == fixp->fx_next->fx_addsy
3639 && fixp->fx_offset == fixp->fx_next->fx_offset);
3640}
3641
462427c4
RS
3642/* Move all labels in LABELS to the current insertion point. TEXT_P
3643 says whether the labels refer to text or data. */
404a8071
RS
3644
3645static void
462427c4 3646mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
404a8071
RS
3647{
3648 struct insn_label_list *l;
3649 valueT val;
3650
462427c4 3651 for (l = labels; l != NULL; l = l->next)
404a8071 3652 {
9c2799c2 3653 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
404a8071
RS
3654 symbol_set_frag (l->label, frag_now);
3655 val = (valueT) frag_now_fix ();
df58fc94 3656 /* MIPS16/microMIPS text labels are stored as odd. */
462427c4 3657 if (text_p && HAVE_CODE_COMPRESSION)
404a8071
RS
3658 ++val;
3659 S_SET_VALUE (l->label, val);
3660 }
3661}
3662
462427c4
RS
3663/* Move all labels in insn_labels to the current insertion point
3664 and treat them as text labels. */
3665
3666static void
3667mips_move_text_labels (void)
3668{
3669 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
3670}
3671
5f0fe04b
TS
3672static bfd_boolean
3673s_is_linkonce (symbolS *sym, segT from_seg)
3674{
3675 bfd_boolean linkonce = FALSE;
3676 segT symseg = S_GET_SEGMENT (sym);
3677
3678 if (symseg != from_seg && !S_IS_LOCAL (sym))
3679 {
3680 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
3681 linkonce = TRUE;
5f0fe04b
TS
3682 /* The GNU toolchain uses an extension for ELF: a section
3683 beginning with the magic string .gnu.linkonce is a
3684 linkonce section. */
3685 if (strncmp (segment_name (symseg), ".gnu.linkonce",
3686 sizeof ".gnu.linkonce" - 1) == 0)
3687 linkonce = TRUE;
5f0fe04b
TS
3688 }
3689 return linkonce;
3690}
3691
e1b47bd5 3692/* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
df58fc94
RS
3693 linker to handle them specially, such as generating jalx instructions
3694 when needed. We also make them odd for the duration of the assembly,
3695 in order to generate the right sort of code. We will make them even
252b5132
RH
3696 in the adjust_symtab routine, while leaving them marked. This is
3697 convenient for the debugger and the disassembler. The linker knows
3698 to make them odd again. */
3699
3700static void
e1b47bd5 3701mips_compressed_mark_label (symbolS *label)
252b5132 3702{
df58fc94 3703 gas_assert (HAVE_CODE_COMPRESSION);
a8dbcb85 3704
f3ded42a
RS
3705 if (mips_opts.mips16)
3706 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
3707 else
3708 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
e1b47bd5
RS
3709 if ((S_GET_VALUE (label) & 1) == 0
3710 /* Don't adjust the address if the label is global or weak, or
3711 in a link-once section, since we'll be emitting symbol reloc
3712 references to it which will be patched up by the linker, and
3713 the final value of the symbol may or may not be MIPS16/microMIPS. */
3714 && !S_IS_WEAK (label)
3715 && !S_IS_EXTERNAL (label)
3716 && !s_is_linkonce (label, now_seg))
3717 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
3718}
3719
3720/* Mark preceding MIPS16 or microMIPS instruction labels. */
3721
3722static void
3723mips_compressed_mark_labels (void)
3724{
3725 struct insn_label_list *l;
3726
3727 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
3728 mips_compressed_mark_label (l->label);
252b5132
RH
3729}
3730
4d7206a2
RS
3731/* End the current frag. Make it a variant frag and record the
3732 relaxation info. */
3733
3734static void
3735relax_close_frag (void)
3736{
584892a6 3737 mips_macro_warning.first_frag = frag_now;
4d7206a2 3738 frag_var (rs_machine_dependent, 0, 0,
584892a6 3739 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
4d7206a2
RS
3740 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
3741
3742 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
3743 mips_relax.first_fixup = 0;
3744}
3745
3746/* Start a new relaxation sequence whose expansion depends on SYMBOL.
3747 See the comment above RELAX_ENCODE for more details. */
3748
3749static void
3750relax_start (symbolS *symbol)
3751{
9c2799c2 3752 gas_assert (mips_relax.sequence == 0);
4d7206a2
RS
3753 mips_relax.sequence = 1;
3754 mips_relax.symbol = symbol;
3755}
3756
3757/* Start generating the second version of a relaxable sequence.
3758 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
3759
3760static void
4d7206a2
RS
3761relax_switch (void)
3762{
9c2799c2 3763 gas_assert (mips_relax.sequence == 1);
4d7206a2
RS
3764 mips_relax.sequence = 2;
3765}
3766
3767/* End the current relaxable sequence. */
3768
3769static void
3770relax_end (void)
3771{
9c2799c2 3772 gas_assert (mips_relax.sequence == 2);
4d7206a2
RS
3773 relax_close_frag ();
3774 mips_relax.sequence = 0;
3775}
3776
11625dd8
RS
3777/* Return true if IP is a delayed branch or jump. */
3778
3779static inline bfd_boolean
3780delayed_branch_p (const struct mips_cl_insn *ip)
3781{
3782 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
3783 | INSN_COND_BRANCH_DELAY
3784 | INSN_COND_BRANCH_LIKELY)) != 0;
3785}
3786
3787/* Return true if IP is a compact branch or jump. */
3788
3789static inline bfd_boolean
3790compact_branch_p (const struct mips_cl_insn *ip)
3791{
26545944
RS
3792 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
3793 | INSN2_COND_BRANCH)) != 0;
11625dd8
RS
3794}
3795
3796/* Return true if IP is an unconditional branch or jump. */
3797
3798static inline bfd_boolean
3799uncond_branch_p (const struct mips_cl_insn *ip)
3800{
3801 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
26545944 3802 || (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0);
11625dd8
RS
3803}
3804
3805/* Return true if IP is a branch-likely instruction. */
3806
3807static inline bfd_boolean
3808branch_likely_p (const struct mips_cl_insn *ip)
3809{
3810 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
3811}
3812
14fe068b
RS
3813/* Return the type of nop that should be used to fill the delay slot
3814 of delayed branch IP. */
3815
3816static struct mips_cl_insn *
3817get_delay_slot_nop (const struct mips_cl_insn *ip)
3818{
3819 if (mips_opts.micromips
3820 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
3821 return &micromips_nop32_insn;
3822 return NOP_INSN;
3823}
3824
fc76e730
RS
3825/* Return a mask that has bit N set if OPCODE reads the register(s)
3826 in operand N. */
df58fc94
RS
3827
3828static unsigned int
fc76e730 3829insn_read_mask (const struct mips_opcode *opcode)
df58fc94 3830{
fc76e730
RS
3831 return (opcode->pinfo & INSN_READ_ALL) >> INSN_READ_SHIFT;
3832}
df58fc94 3833
fc76e730
RS
3834/* Return a mask that has bit N set if OPCODE writes to the register(s)
3835 in operand N. */
3836
3837static unsigned int
3838insn_write_mask (const struct mips_opcode *opcode)
3839{
3840 return (opcode->pinfo & INSN_WRITE_ALL) >> INSN_WRITE_SHIFT;
3841}
3842
3843/* Return a mask of the registers specified by operand OPERAND of INSN.
3844 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
3845 is set. */
3846
3847static unsigned int
3848operand_reg_mask (const struct mips_cl_insn *insn,
3849 const struct mips_operand *operand,
3850 unsigned int type_mask)
3851{
3852 unsigned int uval, vsel;
3853
3854 switch (operand->type)
df58fc94 3855 {
fc76e730
RS
3856 case OP_INT:
3857 case OP_MAPPED_INT:
3858 case OP_MSB:
3859 case OP_PCREL:
3860 case OP_PERF_REG:
3861 case OP_ADDIUSP_INT:
3862 case OP_ENTRY_EXIT_LIST:
3863 case OP_REPEAT_DEST_REG:
3864 case OP_REPEAT_PREV_REG:
3865 case OP_PC:
14daeee3
RS
3866 case OP_VU0_SUFFIX:
3867 case OP_VU0_MATCH_SUFFIX:
fc76e730
RS
3868 abort ();
3869
3870 case OP_REG:
0f35dbc4 3871 case OP_OPTIONAL_REG:
fc76e730
RS
3872 {
3873 const struct mips_reg_operand *reg_op;
3874
3875 reg_op = (const struct mips_reg_operand *) operand;
3876 if (!(type_mask & (1 << reg_op->reg_type)))
3877 return 0;
3878 uval = insn_extract_operand (insn, operand);
3879 return 1 << mips_decode_reg_operand (reg_op, uval);
3880 }
3881
3882 case OP_REG_PAIR:
3883 {
3884 const struct mips_reg_pair_operand *pair_op;
3885
3886 pair_op = (const struct mips_reg_pair_operand *) operand;
3887 if (!(type_mask & (1 << pair_op->reg_type)))
3888 return 0;
3889 uval = insn_extract_operand (insn, operand);
3890 return (1 << pair_op->reg1_map[uval]) | (1 << pair_op->reg2_map[uval]);
3891 }
3892
3893 case OP_CLO_CLZ_DEST:
3894 if (!(type_mask & (1 << OP_REG_GP)))
3895 return 0;
3896 uval = insn_extract_operand (insn, operand);
3897 return (1 << (uval & 31)) | (1 << (uval >> 5));
3898
3899 case OP_LWM_SWM_LIST:
3900 abort ();
3901
3902 case OP_SAVE_RESTORE_LIST:
3903 abort ();
3904
3905 case OP_MDMX_IMM_REG:
3906 if (!(type_mask & (1 << OP_REG_VEC)))
3907 return 0;
3908 uval = insn_extract_operand (insn, operand);
3909 vsel = uval >> 5;
3910 if ((vsel & 0x18) == 0x18)
3911 return 0;
3912 return 1 << (uval & 31);
df58fc94 3913 }
fc76e730
RS
3914 abort ();
3915}
3916
3917/* Return a mask of the registers specified by operands OPNO_MASK of INSN,
3918 where bit N of OPNO_MASK is set if operand N should be included.
3919 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
3920 is set. */
3921
3922static unsigned int
3923insn_reg_mask (const struct mips_cl_insn *insn,
3924 unsigned int type_mask, unsigned int opno_mask)
3925{
3926 unsigned int opno, reg_mask;
3927
3928 opno = 0;
3929 reg_mask = 0;
3930 while (opno_mask != 0)
3931 {
3932 if (opno_mask & 1)
3933 reg_mask |= operand_reg_mask (insn, insn_opno (insn, opno), type_mask);
3934 opno_mask >>= 1;
3935 opno += 1;
3936 }
3937 return reg_mask;
df58fc94
RS
3938}
3939
4c260379
RS
3940/* Return the mask of core registers that IP reads. */
3941
3942static unsigned int
3943gpr_read_mask (const struct mips_cl_insn *ip)
3944{
3945 unsigned long pinfo, pinfo2;
3946 unsigned int mask;
3947
fc76e730 3948 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_read_mask (ip->insn_mo));
4c260379
RS
3949 pinfo = ip->insn_mo->pinfo;
3950 pinfo2 = ip->insn_mo->pinfo2;
fc76e730 3951 if (pinfo & INSN_UDI)
4c260379 3952 {
fc76e730
RS
3953 /* UDI instructions have traditionally been assumed to read RS
3954 and RT. */
3955 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
3956 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4c260379 3957 }
fc76e730
RS
3958 if (pinfo & INSN_READ_GPR_24)
3959 mask |= 1 << 24;
3960 if (pinfo2 & INSN2_READ_GPR_16)
3961 mask |= 1 << 16;
3962 if (pinfo2 & INSN2_READ_SP)
3963 mask |= 1 << SP;
26545944 3964 if (pinfo2 & INSN2_READ_GPR_31)
fc76e730 3965 mask |= 1 << 31;
fe35f09f
RS
3966 /* Don't include register 0. */
3967 return mask & ~1;
4c260379
RS
3968}
3969
3970/* Return the mask of core registers that IP writes. */
3971
3972static unsigned int
3973gpr_write_mask (const struct mips_cl_insn *ip)
3974{
3975 unsigned long pinfo, pinfo2;
3976 unsigned int mask;
3977
fc76e730 3978 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_write_mask (ip->insn_mo));
4c260379
RS
3979 pinfo = ip->insn_mo->pinfo;
3980 pinfo2 = ip->insn_mo->pinfo2;
fc76e730
RS
3981 if (pinfo & INSN_WRITE_GPR_24)
3982 mask |= 1 << 24;
3983 if (pinfo & INSN_WRITE_GPR_31)
3984 mask |= 1 << 31;
3985 if (pinfo & INSN_UDI)
3986 /* UDI instructions have traditionally been assumed to write to RD. */
3987 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
3988 if (pinfo2 & INSN2_WRITE_SP)
3989 mask |= 1 << SP;
fe35f09f
RS
3990 /* Don't include register 0. */
3991 return mask & ~1;
4c260379
RS
3992}
3993
3994/* Return the mask of floating-point registers that IP reads. */
3995
3996static unsigned int
3997fpr_read_mask (const struct mips_cl_insn *ip)
3998{
fc76e730 3999 unsigned long pinfo;
4c260379
RS
4000 unsigned int mask;
4001
fc76e730
RS
4002 mask = insn_reg_mask (ip, (1 << OP_REG_FP) | (1 << OP_REG_VEC),
4003 insn_read_mask (ip->insn_mo));
4c260379 4004 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4005 /* Conservatively treat all operands to an FP_D instruction are doubles.
4006 (This is overly pessimistic for things like cvt.d.s.) */
4007 if (HAVE_32BIT_FPRS && (pinfo & FP_D))
4008 mask |= mask << 1;
4009 return mask;
4010}
4011
4012/* Return the mask of floating-point registers that IP writes. */
4013
4014static unsigned int
4015fpr_write_mask (const struct mips_cl_insn *ip)
4016{
fc76e730 4017 unsigned long pinfo;
4c260379
RS
4018 unsigned int mask;
4019
fc76e730
RS
4020 mask = insn_reg_mask (ip, (1 << OP_REG_FP) | (1 << OP_REG_VEC),
4021 insn_write_mask (ip->insn_mo));
4c260379 4022 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4023 /* Conservatively treat all operands to an FP_D instruction are doubles.
4024 (This is overly pessimistic for things like cvt.s.d.) */
4025 if (HAVE_32BIT_FPRS && (pinfo & FP_D))
4026 mask |= mask << 1;
4027 return mask;
4028}
4029
a1d78564
RS
4030/* Operand OPNUM of INSN is an odd-numbered floating-point register.
4031 Check whether that is allowed. */
4032
4033static bfd_boolean
4034mips_oddfpreg_ok (const struct mips_opcode *insn, int opnum)
4035{
4036 const char *s = insn->name;
4037
4038 if (insn->pinfo == INSN_MACRO)
4039 /* Let a macro pass, we'll catch it later when it is expanded. */
4040 return TRUE;
4041
4042 if (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa) || mips_opts.arch == CPU_R5900)
4043 {
4044 /* Allow odd registers for single-precision ops. */
4045 switch (insn->pinfo & (FP_S | FP_D))
4046 {
4047 case FP_S:
4048 case 0:
4049 return TRUE;
4050 case FP_D:
4051 return FALSE;
4052 default:
4053 break;
4054 }
4055
4056 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
4057 s = strchr (insn->name, '.');
4058 if (s != NULL && opnum == 2)
4059 s = strchr (s + 1, '.');
4060 return (s != NULL && (s[1] == 'w' || s[1] == 's'));
4061 }
4062
4063 /* Single-precision coprocessor loads and moves are OK too. */
4064 if ((insn->pinfo & FP_S)
4065 && (insn->pinfo & (INSN_COPROC_MEMORY_DELAY | INSN_STORE_MEMORY
4066 | INSN_LOAD_COPROC_DELAY | INSN_COPROC_MOVE_DELAY)))
4067 return TRUE;
4068
4069 return FALSE;
4070}
4071
a1d78564
RS
4072/* Report that user-supplied argument ARGNUM for INSN was VAL, but should
4073 have been in the range [MIN_VAL, MAX_VAL]. PRINT_HEX says whether
4074 this operand is normally printed in hex or decimal. */
4075
4076static void
4077report_bad_range (struct mips_cl_insn *insn, int argnum,
4078 offsetT val, int min_val, int max_val,
4079 bfd_boolean print_hex)
4080{
4081 if (print_hex && val >= 0)
4082 as_bad (_("Operand %d of `%s' must be in the range [0x%x, 0x%x],"
4083 " was 0x%lx."),
4084 argnum, insn->insn_mo->name, min_val, max_val, (unsigned long) val);
4085 else if (print_hex)
4086 as_bad (_("Operand %d of `%s' must be in the range [0x%x, 0x%x],"
4087 " was %ld."),
4088 argnum, insn->insn_mo->name, min_val, max_val, (unsigned long) val);
4089 else
4090 as_bad (_("Operand %d of `%s' must be in the range [%d, %d],"
4091 " was %ld."),
4092 argnum, insn->insn_mo->name, min_val, max_val, (unsigned long) val);
4093}
4094
4095/* Report an invalid combination of position and size operands for a bitfield
4096 operation. POS and SIZE are the values that were given. */
4097
4098static void
4099report_bad_field (offsetT pos, offsetT size)
4100{
4101 as_bad (_("Invalid field specification (position %ld, size %ld)"),
4102 (unsigned long) pos, (unsigned long) size);
4103}
4104
4105/* Information about an instruction argument that we're trying to match. */
4106struct mips_arg_info
4107{
4108 /* The instruction so far. */
4109 struct mips_cl_insn *insn;
4110
a92713e6
RS
4111 /* The first unconsumed operand token. */
4112 struct mips_operand_token *token;
4113
a1d78564
RS
4114 /* The 1-based operand number, in terms of insn->insn_mo->args. */
4115 int opnum;
4116
4117 /* The 1-based argument number, for error reporting. This does not
4118 count elided optional registers, etc.. */
4119 int argnum;
4120
4121 /* The last OP_REG operand seen, or ILLEGAL_REG if none. */
4122 unsigned int last_regno;
4123
4124 /* If the first operand was an OP_REG, this is the register that it
4125 specified, otherwise it is ILLEGAL_REG. */
4126 unsigned int dest_regno;
4127
4128 /* The value of the last OP_INT operand. Only used for OP_MSB,
4129 where it gives the lsb position. */
4130 unsigned int last_op_int;
4131
4132 /* If true, match routines should silently reject invalid arguments.
4133 If false, match routines can accept invalid arguments as long as
4134 they report an appropriate error. They still have the option of
4135 silently rejecting arguments, in which case a generic "Invalid operands"
4136 style of error will be used instead. */
4137 bfd_boolean soft_match;
4138
4139 /* If true, the OP_INT match routine should treat plain symbolic operands
4140 as if a relocation operator like %lo(...) had been used. This is only
4141 ever true if the operand can be relocated. */
4142 bfd_boolean allow_nonconst;
4143
4144 /* When true, the OP_INT match routine should allow unsigned N-bit
4145 arguments to be used where a signed N-bit operand is expected. */
4146 bfd_boolean lax_max;
4147
a1d78564
RS
4148 /* True if a reference to the current AT register was seen. */
4149 bfd_boolean seen_at;
4150};
4151
a92713e6
RS
4152/* Try to match an OT_CHAR token for character CH. Consume the token
4153 and return true on success, otherwise return false. */
a1d78564 4154
a92713e6
RS
4155static bfd_boolean
4156match_char (struct mips_arg_info *arg, char ch)
a1d78564 4157{
a92713e6
RS
4158 if (arg->token->type == OT_CHAR && arg->token->u.ch == ch)
4159 {
4160 ++arg->token;
4161 if (ch == ',')
4162 arg->argnum += 1;
4163 return TRUE;
4164 }
4165 return FALSE;
4166}
a1d78564 4167
a92713e6
RS
4168/* Try to get an expression from the next tokens in ARG. Consume the
4169 tokens and return true on success, storing the expression value in
4170 VALUE and relocation types in R. */
4171
4172static bfd_boolean
4173match_expression (struct mips_arg_info *arg, expressionS *value,
4174 bfd_reloc_code_real_type *r)
4175{
4176 if (arg->token->type == OT_INTEGER)
a1d78564 4177 {
a92713e6
RS
4178 *value = arg->token->u.integer.value;
4179 memcpy (r, arg->token->u.integer.relocs, 3 * sizeof (*r));
4180 ++arg->token;
4181 return TRUE;
4182 }
4183
4184 /* Error-reporting is more consistent if we treat registers as O_register
4185 rather than rejecting them outright. "$1", "($1)" and "(($1))" are
4186 then handled in the same way. */
4187 if (arg->token->type == OT_REG)
4188 {
4189 value->X_add_number = arg->token->u.regno;
4190 ++arg->token;
a1d78564 4191 }
a92713e6
RS
4192 else if (arg->token[0].type == OT_CHAR
4193 && arg->token[0].u.ch == '('
4194 && arg->token[1].type == OT_REG
4195 && arg->token[2].type == OT_CHAR
4196 && arg->token[2].u.ch == ')')
4197 {
4198 value->X_add_number = arg->token[1].u.regno;
4199 arg->token += 3;
4200 }
4201 else
4202 return FALSE;
4203
4204 value->X_op = O_register;
4205 r[0] = r[1] = r[2] = BFD_RELOC_UNUSED;
4206 return TRUE;
4207}
4208
4209/* Try to get a constant expression from the next tokens in ARG. Consume
4210 the tokens and return return true on success, storing the constant value
4211 in *VALUE. Use FALLBACK as the value if the match succeeded with an
4212 error. */
4213
4214static bfd_boolean
4215match_const_int (struct mips_arg_info *arg, offsetT *value, offsetT fallback)
4216{
4217 expressionS ex;
4218 bfd_reloc_code_real_type r[3];
a1d78564 4219
a92713e6
RS
4220 if (!match_expression (arg, &ex, r))
4221 return FALSE;
4222
4223 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_constant)
a1d78564
RS
4224 *value = ex.X_add_number;
4225 else
4226 {
a92713e6
RS
4227 if (arg->soft_match)
4228 return FALSE;
a1d78564
RS
4229 as_bad (_("Operand %d of `%s' must be constant"),
4230 arg->argnum, arg->insn->insn_mo->name);
4231 *value = fallback;
4232 }
a92713e6 4233 return TRUE;
a1d78564
RS
4234}
4235
4236/* Return the RTYPE_* flags for a register operand of type TYPE that
4237 appears in instruction OPCODE. */
4238
4239static unsigned int
4240convert_reg_type (const struct mips_opcode *opcode,
4241 enum mips_reg_operand_type type)
4242{
4243 switch (type)
4244 {
4245 case OP_REG_GP:
4246 return RTYPE_NUM | RTYPE_GP;
4247
4248 case OP_REG_FP:
4249 /* Allow vector register names for MDMX if the instruction is a 64-bit
4250 FPR load, store or move (including moves to and from GPRs). */
4251 if ((mips_opts.ase & ASE_MDMX)
4252 && (opcode->pinfo & FP_D)
4253 && (opcode->pinfo & (INSN_COPROC_MOVE_DELAY
4254 | INSN_COPROC_MEMORY_DELAY
4255 | INSN_LOAD_COPROC_DELAY
4256 | INSN_LOAD_MEMORY_DELAY
4257 | INSN_STORE_MEMORY)))
4258 return RTYPE_FPU | RTYPE_VEC;
4259 return RTYPE_FPU;
4260
4261 case OP_REG_CCC:
4262 if (opcode->pinfo & (FP_D | FP_S))
4263 return RTYPE_CCC | RTYPE_FCC;
4264 return RTYPE_CCC;
4265
4266 case OP_REG_VEC:
4267 if (opcode->membership & INSN_5400)
4268 return RTYPE_FPU;
4269 return RTYPE_FPU | RTYPE_VEC;
4270
4271 case OP_REG_ACC:
4272 return RTYPE_ACC;
4273
4274 case OP_REG_COPRO:
4275 if (opcode->name[strlen (opcode->name) - 1] == '0')
4276 return RTYPE_NUM | RTYPE_CP0;
4277 return RTYPE_NUM;
4278
4279 case OP_REG_HW:
4280 return RTYPE_NUM;
14daeee3
RS
4281
4282 case OP_REG_VI:
4283 return RTYPE_NUM | RTYPE_VI;
4284
4285 case OP_REG_VF:
4286 return RTYPE_NUM | RTYPE_VF;
4287
4288 case OP_REG_R5900_I:
4289 return RTYPE_R5900_I;
4290
4291 case OP_REG_R5900_Q:
4292 return RTYPE_R5900_Q;
4293
4294 case OP_REG_R5900_R:
4295 return RTYPE_R5900_R;
4296
4297 case OP_REG_R5900_ACC:
4298 return RTYPE_R5900_ACC;
a1d78564
RS
4299 }
4300 abort ();
4301}
4302
4303/* ARG is register REGNO, of type TYPE. Warn about any dubious registers. */
4304
4305static void
4306check_regno (struct mips_arg_info *arg,
4307 enum mips_reg_operand_type type, unsigned int regno)
4308{
4309 if (AT && type == OP_REG_GP && regno == AT)
4310 arg->seen_at = TRUE;
4311
4312 if (type == OP_REG_FP
4313 && (regno & 1) != 0
4314 && HAVE_32BIT_FPRS
4315 && !mips_oddfpreg_ok (arg->insn->insn_mo, arg->opnum))
4316 as_warn (_("Float register should be even, was %d"), regno);
4317
4318 if (type == OP_REG_CCC)
4319 {
4320 const char *name;
4321 size_t length;
4322
4323 name = arg->insn->insn_mo->name;
4324 length = strlen (name);
4325 if ((regno & 1) != 0
4326 && ((length >= 3 && strcmp (name + length - 3, ".ps") == 0)
4327 || (length >= 5 && strncmp (name + length - 5, "any2", 4) == 0)))
4328 as_warn (_("Condition code register should be even for %s, was %d"),
4329 name, regno);
4330
4331 if ((regno & 3) != 0
4332 && (length >= 5 && strncmp (name + length - 5, "any4", 4) == 0))
4333 as_warn (_("Condition code register should be 0 or 4 for %s, was %d"),
4334 name, regno);
4335 }
4336}
4337
a92713e6
RS
4338/* ARG is a register with symbol value SYMVAL. Try to interpret it as
4339 a register of type TYPE. Return true on success, storing the register
4340 number in *REGNO and warning about any dubious uses. */
4341
4342static bfd_boolean
4343match_regno (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4344 unsigned int symval, unsigned int *regno)
4345{
4346 if (type == OP_REG_VEC)
4347 symval = mips_prefer_vec_regno (symval);
4348 if (!(symval & convert_reg_type (arg->insn->insn_mo, type)))
4349 return FALSE;
4350
4351 *regno = symval & RNUM_MASK;
4352 check_regno (arg, type, *regno);
4353 return TRUE;
4354}
4355
4356/* Try to interpret the next token in ARG as a register of type TYPE.
4357 Consume the token and return true on success, storing the register
4358 number in *REGNO. Return false on failure. */
4359
4360static bfd_boolean
4361match_reg (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4362 unsigned int *regno)
4363{
4364 if (arg->token->type == OT_REG
4365 && match_regno (arg, type, arg->token->u.regno, regno))
4366 {
4367 ++arg->token;
4368 return TRUE;
4369 }
4370 return FALSE;
4371}
4372
4373/* Try to interpret the next token in ARG as a range of registers of type TYPE.
4374 Consume the token and return true on success, storing the register numbers
4375 in *REGNO1 and *REGNO2. Return false on failure. */
4376
4377static bfd_boolean
4378match_reg_range (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4379 unsigned int *regno1, unsigned int *regno2)
4380{
4381 if (match_reg (arg, type, regno1))
4382 {
4383 *regno2 = *regno1;
4384 return TRUE;
4385 }
4386 if (arg->token->type == OT_REG_RANGE
4387 && match_regno (arg, type, arg->token->u.reg_range.regno1, regno1)
4388 && match_regno (arg, type, arg->token->u.reg_range.regno2, regno2)
4389 && *regno1 <= *regno2)
4390 {
4391 ++arg->token;
4392 return TRUE;
4393 }
4394 return FALSE;
4395}
4396
a1d78564
RS
4397/* OP_INT matcher. */
4398
a92713e6 4399static bfd_boolean
a1d78564 4400match_int_operand (struct mips_arg_info *arg,
a92713e6 4401 const struct mips_operand *operand_base)
a1d78564
RS
4402{
4403 const struct mips_int_operand *operand;
3ccad066 4404 unsigned int uval;
a1d78564
RS
4405 int min_val, max_val, factor;
4406 offsetT sval;
4407 bfd_boolean print_hex;
4408
4409 operand = (const struct mips_int_operand *) operand_base;
4410 factor = 1 << operand->shift;
3ccad066
RS
4411 min_val = mips_int_operand_min (operand);
4412 max_val = mips_int_operand_max (operand);
a1d78564 4413 if (arg->lax_max)
3ccad066 4414 max_val = ((1 << operand_base->size) - 1) << operand->shift;
a1d78564 4415
a92713e6
RS
4416 if (arg->token->type == OT_CHAR && arg->token->u.ch == '(')
4417 /* Assume we have an elided offset. The later match will fail
4418 if this turns out to be wrong. */
4419 sval = 0;
4420 else if (operand_base->lsb == 0
4421 && operand_base->size == 16
4422 && operand->shift == 0
4423 && operand->bias == 0
4424 && (operand->max_val == 32767 || operand->max_val == 65535))
a1d78564
RS
4425 {
4426 /* The operand can be relocated. */
a92713e6
RS
4427 if (!match_expression (arg, &offset_expr, offset_reloc))
4428 return FALSE;
4429
4430 if (offset_reloc[0] != BFD_RELOC_UNUSED)
a1d78564
RS
4431 /* Relocation operators were used. Accept the arguent and
4432 leave the relocation value in offset_expr and offset_relocs
4433 for the caller to process. */
a92713e6
RS
4434 return TRUE;
4435
4436 if (offset_expr.X_op != O_constant)
a1d78564 4437 {
a92713e6
RS
4438 /* If non-constant operands are allowed then leave them for
4439 the caller to process, otherwise fail the match. */
4440 if (!arg->allow_nonconst)
4441 return FALSE;
4442 offset_reloc[0] = BFD_RELOC_LO16;
4443 return TRUE;
a1d78564 4444 }
a92713e6 4445
a1d78564
RS
4446 /* Clear the global state; we're going to install the operand
4447 ourselves. */
a92713e6 4448 sval = offset_expr.X_add_number;
a1d78564
RS
4449 offset_expr.X_op = O_absent;
4450 }
4451 else
4452 {
a92713e6
RS
4453 if (!match_const_int (arg, &sval, min_val))
4454 return FALSE;
a1d78564
RS
4455 }
4456
4457 arg->last_op_int = sval;
4458
4459 /* Check the range. If there's a problem, record the lowest acceptable
4460 value in arg->last_op_int in order to prevent an unhelpful error
4461 from OP_MSB too.
4462
4463 Bit counts have traditionally been printed in hex by the disassembler
4464 but printed as decimal in error messages. Only resort to hex if
4465 the operand is bigger than 6 bits. */
4466 print_hex = operand->print_hex && operand_base->size > 6;
4467 if (sval < min_val || sval > max_val)
4468 {
4469 if (arg->soft_match)
a92713e6 4470 return FALSE;
a1d78564
RS
4471 report_bad_range (arg->insn, arg->argnum, sval, min_val, max_val,
4472 print_hex);
4473 arg->last_op_int = min_val;
4474 }
4475 else if (sval % factor)
4476 {
4477 if (arg->soft_match)
a92713e6 4478 return FALSE;
a1d78564
RS
4479 as_bad (print_hex && sval >= 0
4480 ? _("Operand %d of `%s' must be a factor of %d, was 0x%lx.")
4481 : _("Operand %d of `%s' must be a factor of %d, was %ld."),
4482 arg->argnum, arg->insn->insn_mo->name, factor,
4483 (unsigned long) sval);
4484 arg->last_op_int = min_val;
4485 }
4486
4487 uval = (unsigned int) sval >> operand->shift;
4488 uval -= operand->bias;
4489
4490 /* Handle -mfix-cn63xxp1. */
4491 if (arg->opnum == 1
4492 && mips_fix_cn63xxp1
4493 && !mips_opts.micromips
4494 && strcmp ("pref", arg->insn->insn_mo->name) == 0)
4495 switch (uval)
4496 {
4497 case 5:
4498 case 25:
4499 case 26:
4500 case 27:
4501 case 28:
4502 case 29:
4503 case 30:
4504 case 31:
4505 /* These are ok. */
4506 break;
4507
4508 default:
4509 /* The rest must be changed to 28. */
4510 uval = 28;
4511 break;
4512 }
4513
4514 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 4515 return TRUE;
a1d78564
RS
4516}
4517
4518/* OP_MAPPED_INT matcher. */
4519
a92713e6 4520static bfd_boolean
a1d78564 4521match_mapped_int_operand (struct mips_arg_info *arg,
a92713e6 4522 const struct mips_operand *operand_base)
a1d78564
RS
4523{
4524 const struct mips_mapped_int_operand *operand;
4525 unsigned int uval, num_vals;
4526 offsetT sval;
4527
4528 operand = (const struct mips_mapped_int_operand *) operand_base;
a92713e6
RS
4529 if (!match_const_int (arg, &sval, operand->int_map[0]))
4530 return FALSE;
a1d78564
RS
4531
4532 num_vals = 1 << operand_base->size;
4533 for (uval = 0; uval < num_vals; uval++)
4534 if (operand->int_map[uval] == sval)
4535 break;
4536 if (uval == num_vals)
a92713e6 4537 return FALSE;
a1d78564
RS
4538
4539 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 4540 return TRUE;
a1d78564
RS
4541}
4542
4543/* OP_MSB matcher. */
4544
a92713e6 4545static bfd_boolean
a1d78564 4546match_msb_operand (struct mips_arg_info *arg,
a92713e6 4547 const struct mips_operand *operand_base)
a1d78564
RS
4548{
4549 const struct mips_msb_operand *operand;
4550 int min_val, max_val, max_high;
4551 offsetT size, sval, high;
4552
4553 operand = (const struct mips_msb_operand *) operand_base;
4554 min_val = operand->bias;
4555 max_val = min_val + (1 << operand_base->size) - 1;
4556 max_high = operand->opsize;
4557
a92713e6
RS
4558 if (!match_const_int (arg, &size, 1))
4559 return FALSE;
a1d78564
RS
4560
4561 high = size + arg->last_op_int;
4562 sval = operand->add_lsb ? high : size;
4563
4564 if (size < 0 || high > max_high || sval < min_val || sval > max_val)
4565 {
4566 if (arg->soft_match)
a92713e6 4567 return FALSE;
a1d78564
RS
4568 report_bad_field (arg->last_op_int, size);
4569 sval = min_val;
4570 }
4571 insn_insert_operand (arg->insn, operand_base, sval - min_val);
a92713e6 4572 return TRUE;
a1d78564
RS
4573}
4574
4575/* OP_REG matcher. */
4576
a92713e6 4577static bfd_boolean
a1d78564 4578match_reg_operand (struct mips_arg_info *arg,
a92713e6 4579 const struct mips_operand *operand_base)
a1d78564
RS
4580{
4581 const struct mips_reg_operand *operand;
a92713e6 4582 unsigned int regno, uval, num_vals;
a1d78564
RS
4583
4584 operand = (const struct mips_reg_operand *) operand_base;
a92713e6
RS
4585 if (!match_reg (arg, operand->reg_type, &regno))
4586 return FALSE;
a1d78564
RS
4587
4588 if (operand->reg_map)
4589 {
4590 num_vals = 1 << operand->root.size;
4591 for (uval = 0; uval < num_vals; uval++)
4592 if (operand->reg_map[uval] == regno)
4593 break;
4594 if (num_vals == uval)
a92713e6 4595 return FALSE;
a1d78564
RS
4596 }
4597 else
4598 uval = regno;
4599
a1d78564
RS
4600 arg->last_regno = regno;
4601 if (arg->opnum == 1)
4602 arg->dest_regno = regno;
4603 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 4604 return TRUE;
a1d78564
RS
4605}
4606
4607/* OP_REG_PAIR matcher. */
4608
a92713e6 4609static bfd_boolean
a1d78564 4610match_reg_pair_operand (struct mips_arg_info *arg,
a92713e6 4611 const struct mips_operand *operand_base)
a1d78564
RS
4612{
4613 const struct mips_reg_pair_operand *operand;
a92713e6 4614 unsigned int regno1, regno2, uval, num_vals;
a1d78564
RS
4615
4616 operand = (const struct mips_reg_pair_operand *) operand_base;
a92713e6
RS
4617 if (!match_reg (arg, operand->reg_type, &regno1)
4618 || !match_char (arg, ',')
4619 || !match_reg (arg, operand->reg_type, &regno2))
4620 return FALSE;
a1d78564
RS
4621
4622 num_vals = 1 << operand_base->size;
4623 for (uval = 0; uval < num_vals; uval++)
4624 if (operand->reg1_map[uval] == regno1 && operand->reg2_map[uval] == regno2)
4625 break;
4626 if (uval == num_vals)
a92713e6 4627 return FALSE;
a1d78564 4628
a1d78564 4629 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 4630 return TRUE;
a1d78564
RS
4631}
4632
4633/* OP_PCREL matcher. The caller chooses the relocation type. */
4634
a92713e6
RS
4635static bfd_boolean
4636match_pcrel_operand (struct mips_arg_info *arg)
a1d78564 4637{
a92713e6
RS
4638 bfd_reloc_code_real_type r[3];
4639
4640 return match_expression (arg, &offset_expr, r) && r[0] == BFD_RELOC_UNUSED;
a1d78564
RS
4641}
4642
4643/* OP_PERF_REG matcher. */
4644
a92713e6 4645static bfd_boolean
a1d78564 4646match_perf_reg_operand (struct mips_arg_info *arg,
a92713e6 4647 const struct mips_operand *operand)
a1d78564
RS
4648{
4649 offsetT sval;
4650
a92713e6
RS
4651 if (!match_const_int (arg, &sval, 0))
4652 return FALSE;
a1d78564
RS
4653
4654 if (sval != 0
4655 && (sval != 1
4656 || (mips_opts.arch == CPU_R5900
4657 && (strcmp (arg->insn->insn_mo->name, "mfps") == 0
4658 || strcmp (arg->insn->insn_mo->name, "mtps") == 0))))
4659 {
4660 if (arg->soft_match)
a92713e6 4661 return FALSE;
a1d78564
RS
4662 as_bad (_("Invalid performance register (%ld)"), (unsigned long) sval);
4663 }
4664
4665 insn_insert_operand (arg->insn, operand, sval);
a92713e6 4666 return TRUE;
a1d78564
RS
4667}
4668
4669/* OP_ADDIUSP matcher. */
4670
a92713e6 4671static bfd_boolean
a1d78564 4672match_addiusp_operand (struct mips_arg_info *arg,
a92713e6 4673 const struct mips_operand *operand)
a1d78564
RS
4674{
4675 offsetT sval;
4676 unsigned int uval;
4677
a92713e6
RS
4678 if (!match_const_int (arg, &sval, -256))
4679 return FALSE;
a1d78564
RS
4680
4681 if (sval % 4)
a92713e6 4682 return FALSE;
a1d78564
RS
4683
4684 sval /= 4;
4685 if (!(sval >= -258 && sval <= 257) || (sval >= -2 && sval <= 1))
a92713e6 4686 return FALSE;
a1d78564
RS
4687
4688 uval = (unsigned int) sval;
4689 uval = ((uval >> 1) & ~0xff) | (uval & 0xff);
4690 insn_insert_operand (arg->insn, operand, uval);
a92713e6 4691 return TRUE;
a1d78564
RS
4692}
4693
4694/* OP_CLO_CLZ_DEST matcher. */
4695
a92713e6 4696static bfd_boolean
a1d78564 4697match_clo_clz_dest_operand (struct mips_arg_info *arg,
a92713e6 4698 const struct mips_operand *operand)
a1d78564
RS
4699{
4700 unsigned int regno;
4701
a92713e6
RS
4702 if (!match_reg (arg, OP_REG_GP, &regno))
4703 return FALSE;
a1d78564 4704
a1d78564 4705 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
a92713e6 4706 return TRUE;
a1d78564
RS
4707}
4708
4709/* OP_LWM_SWM_LIST matcher. */
4710
a92713e6 4711static bfd_boolean
a1d78564 4712match_lwm_swm_list_operand (struct mips_arg_info *arg,
a92713e6 4713 const struct mips_operand *operand)
a1d78564 4714{
a92713e6
RS
4715 unsigned int reglist, sregs, ra, regno1, regno2;
4716 struct mips_arg_info reset;
a1d78564 4717
a92713e6
RS
4718 reglist = 0;
4719 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
4720 return FALSE;
4721 do
4722 {
4723 if (regno2 == FP && regno1 >= S0 && regno1 <= S7)
4724 {
4725 reglist |= 1 << FP;
4726 regno2 = S7;
4727 }
4728 reglist |= ((1U << regno2 << 1) - 1) & -(1U << regno1);
4729 reset = *arg;
4730 }
4731 while (match_char (arg, ',')
4732 && match_reg_range (arg, OP_REG_GP, &regno1, &regno2));
4733 *arg = reset;
a1d78564
RS
4734
4735 if (operand->size == 2)
4736 {
4737 /* The list must include both ra and s0-sN, for 0 <= N <= 3. E.g.:
4738
4739 s0, ra
4740 s0, s1, ra, s2, s3
4741 s0-s2, ra
4742
4743 and any permutations of these. */
4744 if ((reglist & 0xfff1ffff) != 0x80010000)
a92713e6 4745 return FALSE;
a1d78564
RS
4746
4747 sregs = (reglist >> 17) & 7;
4748 ra = 0;
4749 }
4750 else
4751 {
4752 /* The list must include at least one of ra and s0-sN,
4753 for 0 <= N <= 8. (Note that there is a gap between s7 and s8,
4754 which are $23 and $30 respectively.) E.g.:
4755
4756 ra
4757 s0
4758 ra, s0, s1, s2
4759 s0-s8
4760 s0-s5, ra
4761
4762 and any permutations of these. */
4763 if ((reglist & 0x3f00ffff) != 0)
a92713e6 4764 return FALSE;
a1d78564
RS
4765
4766 ra = (reglist >> 27) & 0x10;
4767 sregs = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
4768 }
4769 sregs += 1;
4770 if ((sregs & -sregs) != sregs)
a92713e6 4771 return FALSE;
a1d78564
RS
4772
4773 insn_insert_operand (arg->insn, operand, (ffs (sregs) - 1) | ra);
a92713e6 4774 return TRUE;
a1d78564
RS
4775}
4776
364215c8
RS
4777/* OP_ENTRY_EXIT_LIST matcher. */
4778
a92713e6 4779static unsigned int
364215c8 4780match_entry_exit_operand (struct mips_arg_info *arg,
a92713e6 4781 const struct mips_operand *operand)
364215c8
RS
4782{
4783 unsigned int mask;
4784 bfd_boolean is_exit;
4785
4786 /* The format is the same for both ENTRY and EXIT, but the constraints
4787 are different. */
4788 is_exit = strcmp (arg->insn->insn_mo->name, "exit") == 0;
4789 mask = (is_exit ? 7 << 3 : 0);
a92713e6 4790 do
364215c8
RS
4791 {
4792 unsigned int regno1, regno2;
4793 bfd_boolean is_freg;
4794
a92713e6 4795 if (match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
364215c8 4796 is_freg = FALSE;
a92713e6 4797 else if (match_reg_range (arg, OP_REG_FP, &regno1, &regno2))
364215c8
RS
4798 is_freg = TRUE;
4799 else
a92713e6 4800 return FALSE;
364215c8
RS
4801
4802 if (is_exit && is_freg && regno1 == 0 && regno2 < 2)
4803 {
4804 mask &= ~(7 << 3);
4805 mask |= (5 + regno2) << 3;
4806 }
4807 else if (!is_exit && regno1 == 4 && regno2 >= 4 && regno2 <= 7)
4808 mask |= (regno2 - 3) << 3;
4809 else if (regno1 == 16 && regno2 >= 16 && regno2 <= 17)
4810 mask |= (regno2 - 15) << 1;
4811 else if (regno1 == RA && regno2 == RA)
4812 mask |= 1;
4813 else
a92713e6 4814 return FALSE;
364215c8 4815 }
a92713e6
RS
4816 while (match_char (arg, ','));
4817
364215c8 4818 insn_insert_operand (arg->insn, operand, mask);
a92713e6 4819 return TRUE;
364215c8
RS
4820}
4821
4822/* OP_SAVE_RESTORE_LIST matcher. */
4823
a92713e6
RS
4824static bfd_boolean
4825match_save_restore_list_operand (struct mips_arg_info *arg)
364215c8
RS
4826{
4827 unsigned int opcode, args, statics, sregs;
4828 unsigned int num_frame_sizes, num_args, num_statics, num_sregs;
364215c8
RS
4829 offsetT frame_size;
4830 const char *error;
4831
4832 error = 0;
4833 opcode = arg->insn->insn_opcode;
4834 frame_size = 0;
4835 num_frame_sizes = 0;
4836 args = 0;
4837 statics = 0;
4838 sregs = 0;
a92713e6 4839 do
364215c8
RS
4840 {
4841 unsigned int regno1, regno2;
4842
a92713e6 4843 if (arg->token->type == OT_INTEGER)
364215c8
RS
4844 {
4845 /* Handle the frame size. */
a92713e6
RS
4846 if (!match_const_int (arg, &frame_size, 0))
4847 return FALSE;
364215c8 4848 num_frame_sizes += 1;
364215c8
RS
4849 }
4850 else
4851 {
a92713e6
RS
4852 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
4853 return FALSE;
364215c8
RS
4854
4855 while (regno1 <= regno2)
4856 {
4857 if (regno1 >= 4 && regno1 <= 7)
4858 {
4859 if (num_frame_sizes == 0)
4860 /* args $a0-$a3 */
4861 args |= 1 << (regno1 - 4);
4862 else
4863 /* statics $a0-$a3 */
4864 statics |= 1 << (regno1 - 4);
4865 }
4866 else if (regno1 >= 16 && regno1 <= 23)
4867 /* $s0-$s7 */
4868 sregs |= 1 << (regno1 - 16);
4869 else if (regno1 == 30)
4870 /* $s8 */
4871 sregs |= 1 << 8;
4872 else if (regno1 == 31)
4873 /* Add $ra to insn. */
4874 opcode |= 0x40;
4875 else
a92713e6 4876 return FALSE;
364215c8
RS
4877 regno1 += 1;
4878 if (regno1 == 24)
4879 regno1 = 30;
4880 }
4881 }
364215c8 4882 }
a92713e6 4883 while (match_char (arg, ','));
364215c8
RS
4884
4885 /* Encode args/statics combination. */
4886 if (args & statics)
a92713e6 4887 return FALSE;
364215c8
RS
4888 else if (args == 0xf)
4889 /* All $a0-$a3 are args. */
4890 opcode |= MIPS16_ALL_ARGS << 16;
4891 else if (statics == 0xf)
4892 /* All $a0-$a3 are statics. */
4893 opcode |= MIPS16_ALL_STATICS << 16;
4894 else
4895 {
4896 /* Count arg registers. */
4897 num_args = 0;
4898 while (args & 0x1)
4899 {
4900 args >>= 1;
4901 num_args += 1;
4902 }
4903 if (args != 0)
a92713e6 4904 return FALSE;
364215c8
RS
4905
4906 /* Count static registers. */
4907 num_statics = 0;
4908 while (statics & 0x8)
4909 {
4910 statics = (statics << 1) & 0xf;
4911 num_statics += 1;
4912 }
4913 if (statics != 0)
a92713e6 4914 return FALSE;
364215c8
RS
4915
4916 /* Encode args/statics. */
4917 opcode |= ((num_args << 2) | num_statics) << 16;
4918 }
4919
4920 /* Encode $s0/$s1. */
4921 if (sregs & (1 << 0)) /* $s0 */
4922 opcode |= 0x20;
4923 if (sregs & (1 << 1)) /* $s1 */
4924 opcode |= 0x10;
4925 sregs >>= 2;
4926
4927 /* Encode $s2-$s8. */
4928 num_sregs = 0;
4929 while (sregs & 1)
4930 {
4931 sregs >>= 1;
4932 num_sregs += 1;
4933 }
4934 if (sregs != 0)
a92713e6 4935 return FALSE;
364215c8
RS
4936 opcode |= num_sregs << 24;
4937
4938 /* Encode frame size. */
4939 if (num_frame_sizes == 0)
4940 error = _("Missing frame size");
4941 else if (num_frame_sizes > 1)
4942 error = _("Frame size specified twice");
4943 else if ((frame_size & 7) != 0 || frame_size < 0 || frame_size > 0xff * 8)
4944 error = _("Invalid frame size");
4945 else if (frame_size != 128 || (opcode >> 16) != 0)
4946 {
4947 frame_size /= 8;
4948 opcode |= (((frame_size & 0xf0) << 16)
4949 | (frame_size & 0x0f));
4950 }
4951
4952 if (error)
4953 {
4954 if (arg->soft_match)
a92713e6 4955 return FALSE;
4a06e5a2 4956 as_bad ("%s", error);
364215c8
RS
4957 }
4958
4959 /* Finally build the instruction. */
4960 if ((opcode >> 16) != 0 || frame_size == 0)
4961 opcode |= MIPS16_EXTEND;
4962 arg->insn->insn_opcode = opcode;
a92713e6 4963 return TRUE;
364215c8
RS
4964}
4965
a1d78564
RS
4966/* OP_MDMX_IMM_REG matcher. */
4967
a92713e6 4968static bfd_boolean
a1d78564 4969match_mdmx_imm_reg_operand (struct mips_arg_info *arg,
a92713e6 4970 const struct mips_operand *operand)
a1d78564 4971{
a92713e6 4972 unsigned int regno, uval;
a1d78564
RS
4973 bfd_boolean is_qh;
4974 const struct mips_opcode *opcode;
4975
4976 /* The mips_opcode records whether this is an octobyte or quadhalf
4977 instruction. Start out with that bit in place. */
4978 opcode = arg->insn->insn_mo;
4979 uval = mips_extract_operand (operand, opcode->match);
4980 is_qh = (uval != 0);
4981
a92713e6 4982 if (arg->token->type == OT_REG || arg->token->type == OT_REG_ELEMENT)
a1d78564
RS
4983 {
4984 if ((opcode->membership & INSN_5400)
4985 && strcmp (opcode->name, "rzu.ob") == 0)
4986 {
4987 if (arg->soft_match)
a92713e6 4988 return FALSE;
a1d78564
RS
4989 as_bad (_("Operand %d of `%s' must be an immediate"),
4990 arg->argnum, opcode->name);
4991 }
4992
4993 /* Check whether this is a vector register or a broadcast of
4994 a single element. */
a92713e6 4995 if (arg->token->type == OT_REG_ELEMENT)
a1d78564 4996 {
a92713e6
RS
4997 if (!match_regno (arg, OP_REG_VEC, arg->token->u.reg_element.regno,
4998 &regno))
4999 return FALSE;
5000 if (arg->token->u.reg_element.index > (is_qh ? 3 : 7))
a1d78564
RS
5001 {
5002 if (arg->soft_match)
a92713e6 5003 return FALSE;
a1d78564 5004 as_bad (_("Invalid element selector"));
a1d78564 5005 }
a1d78564 5006 else
a92713e6 5007 uval |= arg->token->u.reg_element.index << (is_qh ? 2 : 1) << 5;
a1d78564
RS
5008 }
5009 else
5010 {
5011 /* A full vector. */
5012 if ((opcode->membership & INSN_5400)
5013 && (strcmp (opcode->name, "sll.ob") == 0
5014 || strcmp (opcode->name, "srl.ob") == 0))
5015 {
5016 if (arg->soft_match)
a92713e6 5017 return FALSE;
a1d78564
RS
5018 as_bad (_("Operand %d of `%s' must be scalar"),
5019 arg->argnum, opcode->name);
5020 }
5021
a92713e6
RS
5022 if (!match_regno (arg, OP_REG_VEC, arg->token->u.regno, &regno))
5023 return FALSE;
a1d78564
RS
5024 if (is_qh)
5025 uval |= MDMX_FMTSEL_VEC_QH << 5;
5026 else
5027 uval |= MDMX_FMTSEL_VEC_OB << 5;
5028 }
a1d78564 5029 uval |= regno;
a92713e6 5030 ++arg->token;
a1d78564
RS
5031 }
5032 else
5033 {
5034 offsetT sval;
5035
a92713e6
RS
5036 if (!match_const_int (arg, &sval, 0))
5037 return FALSE;
a1d78564
RS
5038 if (sval < 0 || sval > 31)
5039 {
5040 if (arg->soft_match)
a92713e6 5041 return FALSE;
a1d78564
RS
5042 report_bad_range (arg->insn, arg->argnum, sval, 0, 31, FALSE);
5043 }
5044 uval |= (sval & 31);
5045 if (is_qh)
5046 uval |= MDMX_FMTSEL_IMM_QH << 5;
5047 else
5048 uval |= MDMX_FMTSEL_IMM_OB << 5;
5049 }
5050 insn_insert_operand (arg->insn, operand, uval);
a92713e6 5051 return TRUE;
a1d78564
RS
5052}
5053
5054/* OP_PC matcher. */
5055
a92713e6
RS
5056static bfd_boolean
5057match_pc_operand (struct mips_arg_info *arg)
a1d78564 5058{
a92713e6
RS
5059 if (arg->token->type == OT_REG && (arg->token->u.regno & RTYPE_PC))
5060 {
5061 ++arg->token;
5062 return TRUE;
5063 }
5064 return FALSE;
a1d78564
RS
5065}
5066
5067/* OP_REPEAT_DEST_REG and OP_REPEAT_PREV_REG matcher. OTHER_REGNO is the
5068 register that we need to match. */
5069
a92713e6
RS
5070static bfd_boolean
5071match_tied_reg_operand (struct mips_arg_info *arg, unsigned int other_regno)
a1d78564
RS
5072{
5073 unsigned int regno;
5074
a92713e6 5075 return match_reg (arg, OP_REG_GP, &regno) && regno == other_regno;
a1d78564
RS
5076}
5077
89565f1b
RS
5078/* Read a floating-point constant from S for LI.S or LI.D. LENGTH is
5079 the length of the value in bytes (4 for float, 8 for double) and
5080 USING_GPRS says whether the destination is a GPR rather than an FPR.
5081
5082 Return the constant in IMM and OFFSET as follows:
5083
5084 - If the constant should be loaded via memory, set IMM to O_absent and
5085 OFFSET to the memory address.
5086
5087 - Otherwise, if the constant should be loaded into two 32-bit registers,
5088 set IMM to the O_constant to load into the high register and OFFSET
5089 to the corresponding value for the low register.
5090
5091 - Otherwise, set IMM to the full O_constant and set OFFSET to O_absent.
5092
5093 These constants only appear as the last operand in an instruction,
5094 and every instruction that accepts them in any variant accepts them
5095 in all variants. This means we don't have to worry about backing out
5096 any changes if the instruction does not match. We just match
5097 unconditionally and report an error if the constant is invalid. */
5098
a92713e6
RS
5099static bfd_boolean
5100match_float_constant (struct mips_arg_info *arg, expressionS *imm,
5101 expressionS *offset, int length, bfd_boolean using_gprs)
89565f1b 5102{
a92713e6 5103 char *p;
89565f1b
RS
5104 segT seg, new_seg;
5105 subsegT subseg;
5106 const char *newname;
a92713e6 5107 unsigned char *data;
89565f1b
RS
5108
5109 /* Where the constant is placed is based on how the MIPS assembler
5110 does things:
5111
5112 length == 4 && using_gprs -- immediate value only
5113 length == 8 && using_gprs -- .rdata or immediate value
5114 length == 4 && !using_gprs -- .lit4 or immediate value
5115 length == 8 && !using_gprs -- .lit8 or immediate value
5116
5117 The .lit4 and .lit8 sections are only used if permitted by the
5118 -G argument. */
a92713e6
RS
5119 if (arg->token->type != OT_FLOAT)
5120 return FALSE;
5121
5122 gas_assert (arg->token->u.flt.length == length);
5123 data = arg->token->u.flt.data;
5124 ++arg->token;
89565f1b
RS
5125
5126 /* Handle 32-bit constants for which an immediate value is best. */
5127 if (length == 4
5128 && (using_gprs
5129 || g_switch_value < 4
5130 || (data[0] == 0 && data[1] == 0)
5131 || (data[2] == 0 && data[3] == 0)))
5132 {
5133 imm->X_op = O_constant;
5134 if (!target_big_endian)
5135 imm->X_add_number = bfd_getl32 (data);
5136 else
5137 imm->X_add_number = bfd_getb32 (data);
5138 offset->X_op = O_absent;
a92713e6 5139 return TRUE;
89565f1b
RS
5140 }
5141
5142 /* Handle 64-bit constants for which an immediate value is best. */
5143 if (length == 8
5144 && !mips_disable_float_construction
5145 /* Constants can only be constructed in GPRs and copied
5146 to FPRs if the GPRs are at least as wide as the FPRs.
5147 Force the constant into memory if we are using 64-bit FPRs
5148 but the GPRs are only 32 bits wide. */
5149 /* ??? No longer true with the addition of MTHC1, but this
5150 is legacy code... */
5151 && (using_gprs || !(HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
5152 && ((data[0] == 0 && data[1] == 0)
5153 || (data[2] == 0 && data[3] == 0))
5154 && ((data[4] == 0 && data[5] == 0)
5155 || (data[6] == 0 && data[7] == 0)))
5156 {
5157 /* The value is simple enough to load with a couple of instructions.
5158 If using 32-bit registers, set IMM to the high order 32 bits and
5159 OFFSET to the low order 32 bits. Otherwise, set IMM to the entire
5160 64 bit constant. */
5161 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
5162 {
5163 imm->X_op = O_constant;
5164 offset->X_op = O_constant;
5165 if (!target_big_endian)
5166 {
5167 imm->X_add_number = bfd_getl32 (data + 4);
5168 offset->X_add_number = bfd_getl32 (data);
5169 }
5170 else
5171 {
5172 imm->X_add_number = bfd_getb32 (data);
5173 offset->X_add_number = bfd_getb32 (data + 4);
5174 }
5175 if (offset->X_add_number == 0)
5176 offset->X_op = O_absent;
5177 }
5178 else
5179 {
5180 imm->X_op = O_constant;
5181 if (!target_big_endian)
5182 imm->X_add_number = bfd_getl64 (data);
5183 else
5184 imm->X_add_number = bfd_getb64 (data);
5185 offset->X_op = O_absent;
5186 }
a92713e6 5187 return TRUE;
89565f1b
RS
5188 }
5189
5190 /* Switch to the right section. */
5191 seg = now_seg;
5192 subseg = now_subseg;
5193 if (length == 4)
5194 {
5195 gas_assert (!using_gprs && g_switch_value >= 4);
5196 newname = ".lit4";
5197 }
5198 else
5199 {
5200 if (using_gprs || g_switch_value < 8)
5201 newname = RDATA_SECTION_NAME;
5202 else
5203 newname = ".lit8";
5204 }
5205
5206 new_seg = subseg_new (newname, (subsegT) 0);
5207 bfd_set_section_flags (stdoutput, new_seg,
5208 SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA);
5209 frag_align (length == 4 ? 2 : 3, 0, 0);
5210 if (strncmp (TARGET_OS, "elf", 3) != 0)
5211 record_alignment (new_seg, 4);
5212 else
5213 record_alignment (new_seg, length == 4 ? 2 : 3);
5214 if (seg == now_seg)
5215 as_bad (_("Can't use floating point insn in this section"));
5216
5217 /* Set the argument to the current address in the section. */
5218 imm->X_op = O_absent;
5219 offset->X_op = O_symbol;
5220 offset->X_add_symbol = symbol_temp_new_now ();
5221 offset->X_add_number = 0;
5222
5223 /* Put the floating point number into the section. */
5224 p = frag_more (length);
5225 memcpy (p, data, length);
5226
5227 /* Switch back to the original section. */
5228 subseg_set (seg, subseg);
a92713e6 5229 return TRUE;
89565f1b
RS
5230}
5231
14daeee3
RS
5232/* OP_VU0_SUFFIX and OP_VU0_MATCH_SUFFIX matcher; MATCH_P selects between
5233 them. */
5234
5235static bfd_boolean
5236match_vu0_suffix_operand (struct mips_arg_info *arg,
5237 const struct mips_operand *operand,
5238 bfd_boolean match_p)
5239{
5240 unsigned int uval;
5241
5242 /* The operand can be an XYZW mask or a single 2-bit channel index
5243 (with X being 0). */
5244 gas_assert (operand->size == 2 || operand->size == 4);
5245
ee5734f0 5246 /* The suffix can be omitted when it is already part of the opcode. */
14daeee3 5247 if (arg->token->type != OT_CHANNELS)
ee5734f0 5248 return match_p;
14daeee3
RS
5249
5250 uval = arg->token->u.channels;
5251 if (operand->size == 2)
5252 {
5253 /* Check that a single bit is set and convert it into a 2-bit index. */
5254 if ((uval & -uval) != uval)
5255 return FALSE;
5256 uval = 4 - ffs (uval);
5257 }
5258
5259 if (match_p && insn_extract_operand (arg->insn, operand) != uval)
5260 return FALSE;
5261
5262 ++arg->token;
5263 if (!match_p)
5264 insn_insert_operand (arg->insn, operand, uval);
5265 return TRUE;
5266}
5267
a1d78564
RS
5268/* S is the text seen for ARG. Match it against OPERAND. Return the end
5269 of the argument text if the match is successful, otherwise return null. */
5270
a92713e6 5271static bfd_boolean
a1d78564 5272match_operand (struct mips_arg_info *arg,
a92713e6 5273 const struct mips_operand *operand)
a1d78564
RS
5274{
5275 switch (operand->type)
5276 {
5277 case OP_INT:
a92713e6 5278 return match_int_operand (arg, operand);
a1d78564
RS
5279
5280 case OP_MAPPED_INT:
a92713e6 5281 return match_mapped_int_operand (arg, operand);
a1d78564
RS
5282
5283 case OP_MSB:
a92713e6 5284 return match_msb_operand (arg, operand);
a1d78564
RS
5285
5286 case OP_REG:
0f35dbc4 5287 case OP_OPTIONAL_REG:
a92713e6 5288 return match_reg_operand (arg, operand);
a1d78564
RS
5289
5290 case OP_REG_PAIR:
a92713e6 5291 return match_reg_pair_operand (arg, operand);
a1d78564
RS
5292
5293 case OP_PCREL:
a92713e6 5294 return match_pcrel_operand (arg);
a1d78564
RS
5295
5296 case OP_PERF_REG:
a92713e6 5297 return match_perf_reg_operand (arg, operand);
a1d78564
RS
5298
5299 case OP_ADDIUSP_INT:
a92713e6 5300 return match_addiusp_operand (arg, operand);
a1d78564
RS
5301
5302 case OP_CLO_CLZ_DEST:
a92713e6 5303 return match_clo_clz_dest_operand (arg, operand);
a1d78564
RS
5304
5305 case OP_LWM_SWM_LIST:
a92713e6 5306 return match_lwm_swm_list_operand (arg, operand);
a1d78564
RS
5307
5308 case OP_ENTRY_EXIT_LIST:
a92713e6 5309 return match_entry_exit_operand (arg, operand);
364215c8 5310
a1d78564 5311 case OP_SAVE_RESTORE_LIST:
a92713e6 5312 return match_save_restore_list_operand (arg);
a1d78564
RS
5313
5314 case OP_MDMX_IMM_REG:
a92713e6 5315 return match_mdmx_imm_reg_operand (arg, operand);
a1d78564
RS
5316
5317 case OP_REPEAT_DEST_REG:
a92713e6 5318 return match_tied_reg_operand (arg, arg->dest_regno);
a1d78564
RS
5319
5320 case OP_REPEAT_PREV_REG:
a92713e6 5321 return match_tied_reg_operand (arg, arg->last_regno);
a1d78564
RS
5322
5323 case OP_PC:
a92713e6 5324 return match_pc_operand (arg);
14daeee3
RS
5325
5326 case OP_VU0_SUFFIX:
5327 return match_vu0_suffix_operand (arg, operand, FALSE);
5328
5329 case OP_VU0_MATCH_SUFFIX:
5330 return match_vu0_suffix_operand (arg, operand, TRUE);
a1d78564
RS
5331 }
5332 abort ();
5333}
5334
5335/* ARG is the state after successfully matching an instruction.
5336 Issue any queued-up warnings. */
5337
5338static void
5339check_completed_insn (struct mips_arg_info *arg)
5340{
5341 if (arg->seen_at)
5342 {
5343 if (AT == ATREG)
5344 as_warn (_("Used $at without \".set noat\""));
5345 else
5346 as_warn (_("Used $%u with \".set at=$%u\""), AT, AT);
5347 }
5348}
a1d78564 5349
85fcb30f
RS
5350/* Return true if modifying general-purpose register REG needs a delay. */
5351
5352static bfd_boolean
5353reg_needs_delay (unsigned int reg)
5354{
5355 unsigned long prev_pinfo;
5356
5357 prev_pinfo = history[0].insn_mo->pinfo;
5358 if (!mips_opts.noreorder
5359 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY) && !gpr_interlocks)
5360 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY) && !cop_interlocks))
5361 && (gpr_write_mask (&history[0]) & (1 << reg)))
5362 return TRUE;
5363
5364 return FALSE;
5365}
5366
71400594
RS
5367/* Classify an instruction according to the FIX_VR4120_* enumeration.
5368 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
5369 by VR4120 errata. */
4d7206a2 5370
71400594
RS
5371static unsigned int
5372classify_vr4120_insn (const char *name)
252b5132 5373{
71400594
RS
5374 if (strncmp (name, "macc", 4) == 0)
5375 return FIX_VR4120_MACC;
5376 if (strncmp (name, "dmacc", 5) == 0)
5377 return FIX_VR4120_DMACC;
5378 if (strncmp (name, "mult", 4) == 0)
5379 return FIX_VR4120_MULT;
5380 if (strncmp (name, "dmult", 5) == 0)
5381 return FIX_VR4120_DMULT;
5382 if (strstr (name, "div"))
5383 return FIX_VR4120_DIV;
5384 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
5385 return FIX_VR4120_MTHILO;
5386 return NUM_FIX_VR4120_CLASSES;
5387}
252b5132 5388
ff239038
CM
5389#define INSN_ERET 0x42000018
5390#define INSN_DERET 0x4200001f
5391
71400594
RS
5392/* Return the number of instructions that must separate INSN1 and INSN2,
5393 where INSN1 is the earlier instruction. Return the worst-case value
5394 for any INSN2 if INSN2 is null. */
252b5132 5395
71400594
RS
5396static unsigned int
5397insns_between (const struct mips_cl_insn *insn1,
5398 const struct mips_cl_insn *insn2)
5399{
5400 unsigned long pinfo1, pinfo2;
4c260379 5401 unsigned int mask;
71400594 5402
85fcb30f
RS
5403 /* If INFO2 is null, pessimistically assume that all flags are set for
5404 the second instruction. */
71400594
RS
5405 pinfo1 = insn1->insn_mo->pinfo;
5406 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
252b5132 5407
71400594
RS
5408 /* For most targets, write-after-read dependencies on the HI and LO
5409 registers must be separated by at least two instructions. */
5410 if (!hilo_interlocks)
252b5132 5411 {
71400594
RS
5412 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
5413 return 2;
5414 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
5415 return 2;
5416 }
5417
5418 /* If we're working around r7000 errata, there must be two instructions
5419 between an mfhi or mflo and any instruction that uses the result. */
5420 if (mips_7000_hilo_fix
df58fc94 5421 && !mips_opts.micromips
71400594 5422 && MF_HILO_INSN (pinfo1)
85fcb30f 5423 && (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1))))
71400594
RS
5424 return 2;
5425
ff239038
CM
5426 /* If we're working around 24K errata, one instruction is required
5427 if an ERET or DERET is followed by a branch instruction. */
df58fc94 5428 if (mips_fix_24k && !mips_opts.micromips)
ff239038
CM
5429 {
5430 if (insn1->insn_opcode == INSN_ERET
5431 || insn1->insn_opcode == INSN_DERET)
5432 {
5433 if (insn2 == NULL
5434 || insn2->insn_opcode == INSN_ERET
5435 || insn2->insn_opcode == INSN_DERET
11625dd8 5436 || delayed_branch_p (insn2))
ff239038
CM
5437 return 1;
5438 }
5439 }
5440
71400594
RS
5441 /* If working around VR4120 errata, check for combinations that need
5442 a single intervening instruction. */
df58fc94 5443 if (mips_fix_vr4120 && !mips_opts.micromips)
71400594
RS
5444 {
5445 unsigned int class1, class2;
252b5132 5446
71400594
RS
5447 class1 = classify_vr4120_insn (insn1->insn_mo->name);
5448 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
252b5132 5449 {
71400594
RS
5450 if (insn2 == NULL)
5451 return 1;
5452 class2 = classify_vr4120_insn (insn2->insn_mo->name);
5453 if (vr4120_conflicts[class1] & (1 << class2))
5454 return 1;
252b5132 5455 }
71400594
RS
5456 }
5457
df58fc94 5458 if (!HAVE_CODE_COMPRESSION)
71400594
RS
5459 {
5460 /* Check for GPR or coprocessor load delays. All such delays
5461 are on the RT register. */
5462 /* Itbl support may require additional care here. */
5463 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
5464 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
252b5132 5465 {
85fcb30f 5466 if (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1)))
71400594
RS
5467 return 1;
5468 }
5469
5470 /* Check for generic coprocessor hazards.
5471
5472 This case is not handled very well. There is no special
5473 knowledge of CP0 handling, and the coprocessors other than
5474 the floating point unit are not distinguished at all. */
5475 /* Itbl support may require additional care here. FIXME!
5476 Need to modify this to include knowledge about
5477 user specified delays! */
5478 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
5479 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
5480 {
5481 /* Handle cases where INSN1 writes to a known general coprocessor
5482 register. There must be a one instruction delay before INSN2
5483 if INSN2 reads that register, otherwise no delay is needed. */
4c260379
RS
5484 mask = fpr_write_mask (insn1);
5485 if (mask != 0)
252b5132 5486 {
4c260379 5487 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
71400594 5488 return 1;
252b5132
RH
5489 }
5490 else
5491 {
71400594
RS
5492 /* Read-after-write dependencies on the control registers
5493 require a two-instruction gap. */
5494 if ((pinfo1 & INSN_WRITE_COND_CODE)
5495 && (pinfo2 & INSN_READ_COND_CODE))
5496 return 2;
5497
5498 /* We don't know exactly what INSN1 does. If INSN2 is
5499 also a coprocessor instruction, assume there must be
5500 a one instruction gap. */
5501 if (pinfo2 & INSN_COP)
5502 return 1;
252b5132
RH
5503 }
5504 }
6b76fefe 5505
71400594
RS
5506 /* Check for read-after-write dependencies on the coprocessor
5507 control registers in cases where INSN1 does not need a general
5508 coprocessor delay. This means that INSN1 is a floating point
5509 comparison instruction. */
5510 /* Itbl support may require additional care here. */
5511 else if (!cop_interlocks
5512 && (pinfo1 & INSN_WRITE_COND_CODE)
5513 && (pinfo2 & INSN_READ_COND_CODE))
5514 return 1;
5515 }
6b76fefe 5516
71400594
RS
5517 return 0;
5518}
6b76fefe 5519
7d8e00cf
RS
5520/* Return the number of nops that would be needed to work around the
5521 VR4130 mflo/mfhi errata if instruction INSN immediately followed
932d1a1b
RS
5522 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
5523 that are contained within the first IGNORE instructions of HIST. */
7d8e00cf
RS
5524
5525static int
932d1a1b 5526nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
7d8e00cf
RS
5527 const struct mips_cl_insn *insn)
5528{
4c260379
RS
5529 int i, j;
5530 unsigned int mask;
7d8e00cf
RS
5531
5532 /* Check if the instruction writes to HI or LO. MTHI and MTLO
5533 are not affected by the errata. */
5534 if (insn != 0
5535 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
5536 || strcmp (insn->insn_mo->name, "mtlo") == 0
5537 || strcmp (insn->insn_mo->name, "mthi") == 0))
5538 return 0;
5539
5540 /* Search for the first MFLO or MFHI. */
5541 for (i = 0; i < MAX_VR4130_NOPS; i++)
91d6fa6a 5542 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
7d8e00cf
RS
5543 {
5544 /* Extract the destination register. */
4c260379 5545 mask = gpr_write_mask (&hist[i]);
7d8e00cf
RS
5546
5547 /* No nops are needed if INSN reads that register. */
4c260379 5548 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
7d8e00cf
RS
5549 return 0;
5550
5551 /* ...or if any of the intervening instructions do. */
5552 for (j = 0; j < i; j++)
4c260379 5553 if (gpr_read_mask (&hist[j]) & mask)
7d8e00cf
RS
5554 return 0;
5555
932d1a1b
RS
5556 if (i >= ignore)
5557 return MAX_VR4130_NOPS - i;
7d8e00cf
RS
5558 }
5559 return 0;
5560}
5561
15be625d
CM
5562#define BASE_REG_EQ(INSN1, INSN2) \
5563 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
5564 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
5565
5566/* Return the minimum alignment for this store instruction. */
5567
5568static int
5569fix_24k_align_to (const struct mips_opcode *mo)
5570{
5571 if (strcmp (mo->name, "sh") == 0)
5572 return 2;
5573
5574 if (strcmp (mo->name, "swc1") == 0
5575 || strcmp (mo->name, "swc2") == 0
5576 || strcmp (mo->name, "sw") == 0
5577 || strcmp (mo->name, "sc") == 0
5578 || strcmp (mo->name, "s.s") == 0)
5579 return 4;
5580
5581 if (strcmp (mo->name, "sdc1") == 0
5582 || strcmp (mo->name, "sdc2") == 0
5583 || strcmp (mo->name, "s.d") == 0)
5584 return 8;
5585
5586 /* sb, swl, swr */
5587 return 1;
5588}
5589
5590struct fix_24k_store_info
5591 {
5592 /* Immediate offset, if any, for this store instruction. */
5593 short off;
5594 /* Alignment required by this store instruction. */
5595 int align_to;
5596 /* True for register offsets. */
5597 int register_offset;
5598 };
5599
5600/* Comparison function used by qsort. */
5601
5602static int
5603fix_24k_sort (const void *a, const void *b)
5604{
5605 const struct fix_24k_store_info *pos1 = a;
5606 const struct fix_24k_store_info *pos2 = b;
5607
5608 return (pos1->off - pos2->off);
5609}
5610
5611/* INSN is a store instruction. Try to record the store information
5612 in STINFO. Return false if the information isn't known. */
5613
5614static bfd_boolean
5615fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
ab9794cf 5616 const struct mips_cl_insn *insn)
15be625d
CM
5617{
5618 /* The instruction must have a known offset. */
5619 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
5620 return FALSE;
5621
5622 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
5623 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
5624 return TRUE;
5625}
5626
932d1a1b
RS
5627/* Return the number of nops that would be needed to work around the 24k
5628 "lost data on stores during refill" errata if instruction INSN
5629 immediately followed the 2 instructions described by HIST.
5630 Ignore hazards that are contained within the first IGNORE
5631 instructions of HIST.
5632
5633 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
5634 for the data cache refills and store data. The following describes
5635 the scenario where the store data could be lost.
5636
5637 * A data cache miss, due to either a load or a store, causing fill
5638 data to be supplied by the memory subsystem
5639 * The first three doublewords of fill data are returned and written
5640 into the cache
5641 * A sequence of four stores occurs in consecutive cycles around the
5642 final doubleword of the fill:
5643 * Store A
5644 * Store B
5645 * Store C
5646 * Zero, One or more instructions
5647 * Store D
5648
5649 The four stores A-D must be to different doublewords of the line that
5650 is being filled. The fourth instruction in the sequence above permits
5651 the fill of the final doubleword to be transferred from the FSB into
5652 the cache. In the sequence above, the stores may be either integer
5653 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
5654 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
5655 different doublewords on the line. If the floating point unit is
5656 running in 1:2 mode, it is not possible to create the sequence above
5657 using only floating point store instructions.
15be625d
CM
5658
5659 In this case, the cache line being filled is incorrectly marked
5660 invalid, thereby losing the data from any store to the line that
5661 occurs between the original miss and the completion of the five
5662 cycle sequence shown above.
5663
932d1a1b 5664 The workarounds are:
15be625d 5665
932d1a1b
RS
5666 * Run the data cache in write-through mode.
5667 * Insert a non-store instruction between
5668 Store A and Store B or Store B and Store C. */
15be625d
CM
5669
5670static int
932d1a1b 5671nops_for_24k (int ignore, const struct mips_cl_insn *hist,
15be625d
CM
5672 const struct mips_cl_insn *insn)
5673{
5674 struct fix_24k_store_info pos[3];
5675 int align, i, base_offset;
5676
932d1a1b
RS
5677 if (ignore >= 2)
5678 return 0;
5679
ab9794cf
RS
5680 /* If the previous instruction wasn't a store, there's nothing to
5681 worry about. */
15be625d
CM
5682 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
5683 return 0;
5684
ab9794cf
RS
5685 /* If the instructions after the previous one are unknown, we have
5686 to assume the worst. */
5687 if (!insn)
15be625d
CM
5688 return 1;
5689
ab9794cf
RS
5690 /* Check whether we are dealing with three consecutive stores. */
5691 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
5692 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
15be625d
CM
5693 return 0;
5694
5695 /* If we don't know the relationship between the store addresses,
5696 assume the worst. */
ab9794cf 5697 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
15be625d
CM
5698 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
5699 return 1;
5700
5701 if (!fix_24k_record_store_info (&pos[0], insn)
5702 || !fix_24k_record_store_info (&pos[1], &hist[0])
5703 || !fix_24k_record_store_info (&pos[2], &hist[1]))
5704 return 1;
5705
5706 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
5707
5708 /* Pick a value of ALIGN and X such that all offsets are adjusted by
5709 X bytes and such that the base register + X is known to be aligned
5710 to align bytes. */
5711
5712 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
5713 align = 8;
5714 else
5715 {
5716 align = pos[0].align_to;
5717 base_offset = pos[0].off;
5718 for (i = 1; i < 3; i++)
5719 if (align < pos[i].align_to)
5720 {
5721 align = pos[i].align_to;
5722 base_offset = pos[i].off;
5723 }
5724 for (i = 0; i < 3; i++)
5725 pos[i].off -= base_offset;
5726 }
5727
5728 pos[0].off &= ~align + 1;
5729 pos[1].off &= ~align + 1;
5730 pos[2].off &= ~align + 1;
5731
5732 /* If any two stores write to the same chunk, they also write to the
5733 same doubleword. The offsets are still sorted at this point. */
5734 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
5735 return 0;
5736
5737 /* A range of at least 9 bytes is needed for the stores to be in
5738 non-overlapping doublewords. */
5739 if (pos[2].off - pos[0].off <= 8)
5740 return 0;
5741
5742 if (pos[2].off - pos[1].off >= 24
5743 || pos[1].off - pos[0].off >= 24
5744 || pos[2].off - pos[0].off >= 32)
5745 return 0;
5746
5747 return 1;
5748}
5749
71400594 5750/* Return the number of nops that would be needed if instruction INSN
91d6fa6a 5751 immediately followed the MAX_NOPS instructions given by HIST,
932d1a1b
RS
5752 where HIST[0] is the most recent instruction. Ignore hazards
5753 between INSN and the first IGNORE instructions in HIST.
5754
5755 If INSN is null, return the worse-case number of nops for any
5756 instruction. */
bdaaa2e1 5757
71400594 5758static int
932d1a1b 5759nops_for_insn (int ignore, const struct mips_cl_insn *hist,
71400594
RS
5760 const struct mips_cl_insn *insn)
5761{
5762 int i, nops, tmp_nops;
bdaaa2e1 5763
71400594 5764 nops = 0;
932d1a1b 5765 for (i = ignore; i < MAX_DELAY_NOPS; i++)
65b02341 5766 {
91d6fa6a 5767 tmp_nops = insns_between (hist + i, insn) - i;
65b02341
RS
5768 if (tmp_nops > nops)
5769 nops = tmp_nops;
5770 }
7d8e00cf 5771
df58fc94 5772 if (mips_fix_vr4130 && !mips_opts.micromips)
7d8e00cf 5773 {
932d1a1b 5774 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
7d8e00cf
RS
5775 if (tmp_nops > nops)
5776 nops = tmp_nops;
5777 }
5778
df58fc94 5779 if (mips_fix_24k && !mips_opts.micromips)
15be625d 5780 {
932d1a1b 5781 tmp_nops = nops_for_24k (ignore, hist, insn);
15be625d
CM
5782 if (tmp_nops > nops)
5783 nops = tmp_nops;
5784 }
5785
71400594
RS
5786 return nops;
5787}
252b5132 5788
71400594 5789/* The variable arguments provide NUM_INSNS extra instructions that
91d6fa6a 5790 might be added to HIST. Return the largest number of nops that
932d1a1b
RS
5791 would be needed after the extended sequence, ignoring hazards
5792 in the first IGNORE instructions. */
252b5132 5793
71400594 5794static int
932d1a1b
RS
5795nops_for_sequence (int num_insns, int ignore,
5796 const struct mips_cl_insn *hist, ...)
71400594
RS
5797{
5798 va_list args;
5799 struct mips_cl_insn buffer[MAX_NOPS];
5800 struct mips_cl_insn *cursor;
5801 int nops;
5802
91d6fa6a 5803 va_start (args, hist);
71400594 5804 cursor = buffer + num_insns;
91d6fa6a 5805 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
71400594
RS
5806 while (cursor > buffer)
5807 *--cursor = *va_arg (args, const struct mips_cl_insn *);
5808
932d1a1b 5809 nops = nops_for_insn (ignore, buffer, NULL);
71400594
RS
5810 va_end (args);
5811 return nops;
5812}
252b5132 5813
71400594
RS
5814/* Like nops_for_insn, but if INSN is a branch, take into account the
5815 worst-case delay for the branch target. */
252b5132 5816
71400594 5817static int
932d1a1b 5818nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
71400594
RS
5819 const struct mips_cl_insn *insn)
5820{
5821 int nops, tmp_nops;
60b63b72 5822
932d1a1b 5823 nops = nops_for_insn (ignore, hist, insn);
11625dd8 5824 if (delayed_branch_p (insn))
71400594 5825 {
932d1a1b 5826 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
14fe068b 5827 hist, insn, get_delay_slot_nop (insn));
71400594
RS
5828 if (tmp_nops > nops)
5829 nops = tmp_nops;
5830 }
11625dd8 5831 else if (compact_branch_p (insn))
71400594 5832 {
932d1a1b 5833 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
71400594
RS
5834 if (tmp_nops > nops)
5835 nops = tmp_nops;
5836 }
5837 return nops;
5838}
5839
c67a084a
NC
5840/* Fix NOP issue: Replace nops by "or at,at,zero". */
5841
5842static void
5843fix_loongson2f_nop (struct mips_cl_insn * ip)
5844{
df58fc94 5845 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
5846 if (strcmp (ip->insn_mo->name, "nop") == 0)
5847 ip->insn_opcode = LOONGSON2F_NOP_INSN;
5848}
5849
5850/* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
5851 jr target pc &= 'hffff_ffff_cfff_ffff. */
5852
5853static void
5854fix_loongson2f_jump (struct mips_cl_insn * ip)
5855{
df58fc94 5856 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
5857 if (strcmp (ip->insn_mo->name, "j") == 0
5858 || strcmp (ip->insn_mo->name, "jr") == 0
5859 || strcmp (ip->insn_mo->name, "jalr") == 0)
5860 {
5861 int sreg;
5862 expressionS ep;
5863
5864 if (! mips_opts.at)
5865 return;
5866
df58fc94 5867 sreg = EXTRACT_OPERAND (0, RS, *ip);
c67a084a
NC
5868 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
5869 return;
5870
5871 ep.X_op = O_constant;
5872 ep.X_add_number = 0xcfff0000;
5873 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
5874 ep.X_add_number = 0xffff;
5875 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
5876 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
5877 }
5878}
5879
5880static void
5881fix_loongson2f (struct mips_cl_insn * ip)
5882{
5883 if (mips_fix_loongson2f_nop)
5884 fix_loongson2f_nop (ip);
5885
5886 if (mips_fix_loongson2f_jump)
5887 fix_loongson2f_jump (ip);
5888}
5889
a4e06468
RS
5890/* IP is a branch that has a delay slot, and we need to fill it
5891 automatically. Return true if we can do that by swapping IP
e407c74b
NC
5892 with the previous instruction.
5893 ADDRESS_EXPR is an operand of the instruction to be used with
5894 RELOC_TYPE. */
a4e06468
RS
5895
5896static bfd_boolean
e407c74b 5897can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 5898 bfd_reloc_code_real_type *reloc_type)
a4e06468 5899{
2b0c8b40 5900 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
a4e06468
RS
5901 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
5902
5903 /* -O2 and above is required for this optimization. */
5904 if (mips_optimize < 2)
5905 return FALSE;
5906
5907 /* If we have seen .set volatile or .set nomove, don't optimize. */
5908 if (mips_opts.nomove)
5909 return FALSE;
5910
5911 /* We can't swap if the previous instruction's position is fixed. */
5912 if (history[0].fixed_p)
5913 return FALSE;
5914
5915 /* If the previous previous insn was in a .set noreorder, we can't
5916 swap. Actually, the MIPS assembler will swap in this situation.
5917 However, gcc configured -with-gnu-as will generate code like
5918
5919 .set noreorder
5920 lw $4,XXX
5921 .set reorder
5922 INSN
5923 bne $4,$0,foo
5924
5925 in which we can not swap the bne and INSN. If gcc is not configured
5926 -with-gnu-as, it does not output the .set pseudo-ops. */
5927 if (history[1].noreorder_p)
5928 return FALSE;
5929
87333bb7
MR
5930 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
5931 This means that the previous instruction was a 4-byte one anyhow. */
a4e06468
RS
5932 if (mips_opts.mips16 && history[0].fixp[0])
5933 return FALSE;
5934
5935 /* If the branch is itself the target of a branch, we can not swap.
5936 We cheat on this; all we check for is whether there is a label on
5937 this instruction. If there are any branches to anything other than
5938 a label, users must use .set noreorder. */
5939 if (seg_info (now_seg)->label_list)
5940 return FALSE;
5941
5942 /* If the previous instruction is in a variant frag other than this
2309ddf2 5943 branch's one, we cannot do the swap. This does not apply to
9301f9c3
MR
5944 MIPS16 code, which uses variant frags for different purposes. */
5945 if (!mips_opts.mips16
a4e06468
RS
5946 && history[0].frag
5947 && history[0].frag->fr_type == rs_machine_dependent)
5948 return FALSE;
5949
bcd530a7
RS
5950 /* We do not swap with instructions that cannot architecturally
5951 be placed in a branch delay slot, such as SYNC or ERET. We
5952 also refrain from swapping with a trap instruction, since it
5953 complicates trap handlers to have the trap instruction be in
5954 a delay slot. */
a4e06468 5955 prev_pinfo = history[0].insn_mo->pinfo;
bcd530a7 5956 if (prev_pinfo & INSN_NO_DELAY_SLOT)
a4e06468
RS
5957 return FALSE;
5958
5959 /* Check for conflicts between the branch and the instructions
5960 before the candidate delay slot. */
5961 if (nops_for_insn (0, history + 1, ip) > 0)
5962 return FALSE;
5963
5964 /* Check for conflicts between the swapped sequence and the
5965 target of the branch. */
5966 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
5967 return FALSE;
5968
5969 /* If the branch reads a register that the previous
5970 instruction sets, we can not swap. */
5971 gpr_read = gpr_read_mask (ip);
5972 prev_gpr_write = gpr_write_mask (&history[0]);
5973 if (gpr_read & prev_gpr_write)
5974 return FALSE;
5975
5976 /* If the branch writes a register that the previous
5977 instruction sets, we can not swap. */
5978 gpr_write = gpr_write_mask (ip);
5979 if (gpr_write & prev_gpr_write)
5980 return FALSE;
5981
5982 /* If the branch writes a register that the previous
5983 instruction reads, we can not swap. */
5984 prev_gpr_read = gpr_read_mask (&history[0]);
5985 if (gpr_write & prev_gpr_read)
5986 return FALSE;
5987
5988 /* If one instruction sets a condition code and the
5989 other one uses a condition code, we can not swap. */
5990 pinfo = ip->insn_mo->pinfo;
5991 if ((pinfo & INSN_READ_COND_CODE)
5992 && (prev_pinfo & INSN_WRITE_COND_CODE))
5993 return FALSE;
5994 if ((pinfo & INSN_WRITE_COND_CODE)
5995 && (prev_pinfo & INSN_READ_COND_CODE))
5996 return FALSE;
5997
5998 /* If the previous instruction uses the PC, we can not swap. */
2b0c8b40 5999 prev_pinfo2 = history[0].insn_mo->pinfo2;
26545944 6000 if (prev_pinfo2 & INSN2_READ_PC)
2b0c8b40 6001 return FALSE;
a4e06468 6002
df58fc94
RS
6003 /* If the previous instruction has an incorrect size for a fixed
6004 branch delay slot in microMIPS mode, we cannot swap. */
2309ddf2
MR
6005 pinfo2 = ip->insn_mo->pinfo2;
6006 if (mips_opts.micromips
6007 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
6008 && insn_length (history) != 2)
6009 return FALSE;
6010 if (mips_opts.micromips
6011 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
6012 && insn_length (history) != 4)
6013 return FALSE;
6014
e407c74b
NC
6015 /* On R5900 short loops need to be fixed by inserting a nop in
6016 the branch delay slots.
6017 A short loop can be terminated too early. */
6018 if (mips_opts.arch == CPU_R5900
6019 /* Check if instruction has a parameter, ignore "j $31". */
6020 && (address_expr != NULL)
6021 /* Parameter must be 16 bit. */
6022 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
6023 /* Branch to same segment. */
6024 && (S_GET_SEGMENT(address_expr->X_add_symbol) == now_seg)
6025 /* Branch to same code fragment. */
6026 && (symbol_get_frag(address_expr->X_add_symbol) == frag_now)
6027 /* Can only calculate branch offset if value is known. */
6028 && symbol_constant_p(address_expr->X_add_symbol)
6029 /* Check if branch is really conditional. */
6030 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
6031 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
6032 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
6033 {
6034 int distance;
6035 /* Check if loop is shorter than 6 instructions including
6036 branch and delay slot. */
6037 distance = frag_now_fix() - S_GET_VALUE(address_expr->X_add_symbol);
6038 if (distance <= 20)
6039 {
6040 int i;
6041 int rv;
6042
6043 rv = FALSE;
6044 /* When the loop includes branches or jumps,
6045 it is not a short loop. */
6046 for (i = 0; i < (distance / 4); i++)
6047 {
6048 if ((history[i].cleared_p)
6049 || delayed_branch_p(&history[i]))
6050 {
6051 rv = TRUE;
6052 break;
6053 }
6054 }
6055 if (rv == FALSE)
6056 {
6057 /* Insert nop after branch to fix short loop. */
6058 return FALSE;
6059 }
6060 }
6061 }
6062
a4e06468
RS
6063 return TRUE;
6064}
6065
e407c74b
NC
6066/* Decide how we should add IP to the instruction stream.
6067 ADDRESS_EXPR is an operand of the instruction to be used with
6068 RELOC_TYPE. */
a4e06468
RS
6069
6070static enum append_method
e407c74b 6071get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 6072 bfd_reloc_code_real_type *reloc_type)
a4e06468 6073{
a4e06468
RS
6074 /* The relaxed version of a macro sequence must be inherently
6075 hazard-free. */
6076 if (mips_relax.sequence == 2)
6077 return APPEND_ADD;
6078
6079 /* We must not dabble with instructions in a ".set norerorder" block. */
6080 if (mips_opts.noreorder)
6081 return APPEND_ADD;
6082
6083 /* Otherwise, it's our responsibility to fill branch delay slots. */
11625dd8 6084 if (delayed_branch_p (ip))
a4e06468 6085 {
e407c74b
NC
6086 if (!branch_likely_p (ip)
6087 && can_swap_branch_p (ip, address_expr, reloc_type))
a4e06468
RS
6088 return APPEND_SWAP;
6089
6090 if (mips_opts.mips16
6091 && ISA_SUPPORTS_MIPS16E
fc76e730 6092 && gpr_read_mask (ip) != 0)
a4e06468
RS
6093 return APPEND_ADD_COMPACT;
6094
6095 return APPEND_ADD_WITH_NOP;
6096 }
6097
a4e06468
RS
6098 return APPEND_ADD;
6099}
6100
ceb94aa5
RS
6101/* IP is a MIPS16 instruction whose opcode we have just changed.
6102 Point IP->insn_mo to the new opcode's definition. */
6103
6104static void
6105find_altered_mips16_opcode (struct mips_cl_insn *ip)
6106{
6107 const struct mips_opcode *mo, *end;
6108
6109 end = &mips16_opcodes[bfd_mips16_num_opcodes];
6110 for (mo = ip->insn_mo; mo < end; mo++)
6111 if ((ip->insn_opcode & mo->mask) == mo->match)
6112 {
6113 ip->insn_mo = mo;
6114 return;
6115 }
6116 abort ();
6117}
6118
df58fc94
RS
6119/* For microMIPS macros, we need to generate a local number label
6120 as the target of branches. */
6121#define MICROMIPS_LABEL_CHAR '\037'
6122static unsigned long micromips_target_label;
6123static char micromips_target_name[32];
6124
6125static char *
6126micromips_label_name (void)
6127{
6128 char *p = micromips_target_name;
6129 char symbol_name_temporary[24];
6130 unsigned long l;
6131 int i;
6132
6133 if (*p)
6134 return p;
6135
6136 i = 0;
6137 l = micromips_target_label;
6138#ifdef LOCAL_LABEL_PREFIX
6139 *p++ = LOCAL_LABEL_PREFIX;
6140#endif
6141 *p++ = 'L';
6142 *p++ = MICROMIPS_LABEL_CHAR;
6143 do
6144 {
6145 symbol_name_temporary[i++] = l % 10 + '0';
6146 l /= 10;
6147 }
6148 while (l != 0);
6149 while (i > 0)
6150 *p++ = symbol_name_temporary[--i];
6151 *p = '\0';
6152
6153 return micromips_target_name;
6154}
6155
6156static void
6157micromips_label_expr (expressionS *label_expr)
6158{
6159 label_expr->X_op = O_symbol;
6160 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
6161 label_expr->X_add_number = 0;
6162}
6163
6164static void
6165micromips_label_inc (void)
6166{
6167 micromips_target_label++;
6168 *micromips_target_name = '\0';
6169}
6170
6171static void
6172micromips_add_label (void)
6173{
6174 symbolS *s;
6175
6176 s = colon (micromips_label_name ());
6177 micromips_label_inc ();
f3ded42a 6178 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
df58fc94
RS
6179}
6180
6181/* If assembling microMIPS code, then return the microMIPS reloc
6182 corresponding to the requested one if any. Otherwise return
6183 the reloc unchanged. */
6184
6185static bfd_reloc_code_real_type
6186micromips_map_reloc (bfd_reloc_code_real_type reloc)
6187{
6188 static const bfd_reloc_code_real_type relocs[][2] =
6189 {
6190 /* Keep sorted incrementally by the left-hand key. */
6191 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
6192 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
6193 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
6194 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
6195 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
6196 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
6197 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
6198 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
6199 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
6200 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
6201 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
6202 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
6203 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
6204 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
6205 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
6206 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
6207 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
6208 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
6209 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
6210 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
6211 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
6212 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
6213 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
6214 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
6215 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
6216 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
6217 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
6218 };
6219 bfd_reloc_code_real_type r;
6220 size_t i;
6221
6222 if (!mips_opts.micromips)
6223 return reloc;
6224 for (i = 0; i < ARRAY_SIZE (relocs); i++)
6225 {
6226 r = relocs[i][0];
6227 if (r > reloc)
6228 return reloc;
6229 if (r == reloc)
6230 return relocs[i][1];
6231 }
6232 return reloc;
6233}
6234
b886a2ab
RS
6235/* Try to resolve relocation RELOC against constant OPERAND at assembly time.
6236 Return true on success, storing the resolved value in RESULT. */
6237
6238static bfd_boolean
6239calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
6240 offsetT *result)
6241{
6242 switch (reloc)
6243 {
6244 case BFD_RELOC_MIPS_HIGHEST:
6245 case BFD_RELOC_MICROMIPS_HIGHEST:
6246 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
6247 return TRUE;
6248
6249 case BFD_RELOC_MIPS_HIGHER:
6250 case BFD_RELOC_MICROMIPS_HIGHER:
6251 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
6252 return TRUE;
6253
6254 case BFD_RELOC_HI16_S:
6255 case BFD_RELOC_MICROMIPS_HI16_S:
6256 case BFD_RELOC_MIPS16_HI16_S:
6257 *result = ((operand + 0x8000) >> 16) & 0xffff;
6258 return TRUE;
6259
6260 case BFD_RELOC_HI16:
6261 case BFD_RELOC_MICROMIPS_HI16:
6262 case BFD_RELOC_MIPS16_HI16:
6263 *result = (operand >> 16) & 0xffff;
6264 return TRUE;
6265
6266 case BFD_RELOC_LO16:
6267 case BFD_RELOC_MICROMIPS_LO16:
6268 case BFD_RELOC_MIPS16_LO16:
6269 *result = operand & 0xffff;
6270 return TRUE;
6271
6272 case BFD_RELOC_UNUSED:
6273 *result = operand;
6274 return TRUE;
6275
6276 default:
6277 return FALSE;
6278 }
6279}
6280
71400594
RS
6281/* Output an instruction. IP is the instruction information.
6282 ADDRESS_EXPR is an operand of the instruction to be used with
df58fc94
RS
6283 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
6284 a macro expansion. */
71400594
RS
6285
6286static void
6287append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
df58fc94 6288 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
71400594 6289{
14fe068b 6290 unsigned long prev_pinfo2, pinfo;
71400594 6291 bfd_boolean relaxed_branch = FALSE;
a4e06468 6292 enum append_method method;
2309ddf2 6293 bfd_boolean relax32;
2b0c8b40 6294 int branch_disp;
71400594 6295
2309ddf2 6296 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
c67a084a
NC
6297 fix_loongson2f (ip);
6298
738f4d98 6299 file_ase_mips16 |= mips_opts.mips16;
df58fc94 6300 file_ase_micromips |= mips_opts.micromips;
738f4d98 6301
df58fc94 6302 prev_pinfo2 = history[0].insn_mo->pinfo2;
71400594 6303 pinfo = ip->insn_mo->pinfo;
df58fc94
RS
6304
6305 if (mips_opts.micromips
6306 && !expansionp
6307 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
6308 && micromips_insn_length (ip->insn_mo) != 2)
6309 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
6310 && micromips_insn_length (ip->insn_mo) != 4)))
6311 as_warn (_("Wrong size instruction in a %u-bit branch delay slot"),
6312 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
71400594 6313
15be625d
CM
6314 if (address_expr == NULL)
6315 ip->complete_p = 1;
b886a2ab
RS
6316 else if (reloc_type[0] <= BFD_RELOC_UNUSED
6317 && reloc_type[1] == BFD_RELOC_UNUSED
6318 && reloc_type[2] == BFD_RELOC_UNUSED
15be625d
CM
6319 && address_expr->X_op == O_constant)
6320 {
15be625d
CM
6321 switch (*reloc_type)
6322 {
15be625d 6323 case BFD_RELOC_MIPS_JMP:
df58fc94
RS
6324 {
6325 int shift;
6326
6327 shift = mips_opts.micromips ? 1 : 2;
6328 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
6329 as_bad (_("jump to misaligned address (0x%lx)"),
6330 (unsigned long) address_expr->X_add_number);
6331 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
6332 & 0x3ffffff);
335574df 6333 ip->complete_p = 1;
df58fc94 6334 }
15be625d
CM
6335 break;
6336
6337 case BFD_RELOC_MIPS16_JMP:
6338 if ((address_expr->X_add_number & 3) != 0)
6339 as_bad (_("jump to misaligned address (0x%lx)"),
6340 (unsigned long) address_expr->X_add_number);
6341 ip->insn_opcode |=
6342 (((address_expr->X_add_number & 0x7c0000) << 3)
6343 | ((address_expr->X_add_number & 0xf800000) >> 7)
6344 | ((address_expr->X_add_number & 0x3fffc) >> 2));
335574df 6345 ip->complete_p = 1;
15be625d
CM
6346 break;
6347
6348 case BFD_RELOC_16_PCREL_S2:
df58fc94
RS
6349 {
6350 int shift;
6351
6352 shift = mips_opts.micromips ? 1 : 2;
6353 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
6354 as_bad (_("branch to misaligned address (0x%lx)"),
6355 (unsigned long) address_expr->X_add_number);
6356 if (!mips_relax_branch)
6357 {
6358 if ((address_expr->X_add_number + (1 << (shift + 15)))
6359 & ~((1 << (shift + 16)) - 1))
6360 as_bad (_("branch address range overflow (0x%lx)"),
6361 (unsigned long) address_expr->X_add_number);
6362 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
6363 & 0xffff);
6364 }
df58fc94 6365 }
15be625d
CM
6366 break;
6367
6368 default:
b886a2ab
RS
6369 {
6370 offsetT value;
6371
6372 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
6373 &value))
6374 {
6375 ip->insn_opcode |= value & 0xffff;
6376 ip->complete_p = 1;
6377 }
6378 }
6379 break;
6380 }
15be625d
CM
6381 }
6382
71400594
RS
6383 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
6384 {
6385 /* There are a lot of optimizations we could do that we don't.
6386 In particular, we do not, in general, reorder instructions.
6387 If you use gcc with optimization, it will reorder
6388 instructions and generally do much more optimization then we
6389 do here; repeating all that work in the assembler would only
6390 benefit hand written assembly code, and does not seem worth
6391 it. */
6392 int nops = (mips_optimize == 0
932d1a1b
RS
6393 ? nops_for_insn (0, history, NULL)
6394 : nops_for_insn_or_target (0, history, ip));
71400594 6395 if (nops > 0)
252b5132
RH
6396 {
6397 fragS *old_frag;
6398 unsigned long old_frag_offset;
6399 int i;
252b5132
RH
6400
6401 old_frag = frag_now;
6402 old_frag_offset = frag_now_fix ();
6403
6404 for (i = 0; i < nops; i++)
14fe068b
RS
6405 add_fixed_insn (NOP_INSN);
6406 insert_into_history (0, nops, NOP_INSN);
252b5132
RH
6407
6408 if (listing)
6409 {
6410 listing_prev_line ();
6411 /* We may be at the start of a variant frag. In case we
6412 are, make sure there is enough space for the frag
6413 after the frags created by listing_prev_line. The
6414 argument to frag_grow here must be at least as large
6415 as the argument to all other calls to frag_grow in
6416 this file. We don't have to worry about being in the
6417 middle of a variant frag, because the variants insert
6418 all needed nop instructions themselves. */
6419 frag_grow (40);
6420 }
6421
462427c4 6422 mips_move_text_labels ();
252b5132
RH
6423
6424#ifndef NO_ECOFF_DEBUGGING
6425 if (ECOFF_DEBUGGING)
6426 ecoff_fix_loc (old_frag, old_frag_offset);
6427#endif
6428 }
71400594
RS
6429 }
6430 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
6431 {
932d1a1b
RS
6432 int nops;
6433
6434 /* Work out how many nops in prev_nop_frag are needed by IP,
6435 ignoring hazards generated by the first prev_nop_frag_since
6436 instructions. */
6437 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
9c2799c2 6438 gas_assert (nops <= prev_nop_frag_holds);
252b5132 6439
71400594
RS
6440 /* Enforce NOPS as a minimum. */
6441 if (nops > prev_nop_frag_required)
6442 prev_nop_frag_required = nops;
252b5132 6443
71400594
RS
6444 if (prev_nop_frag_holds == prev_nop_frag_required)
6445 {
6446 /* Settle for the current number of nops. Update the history
6447 accordingly (for the benefit of any future .set reorder code). */
6448 prev_nop_frag = NULL;
6449 insert_into_history (prev_nop_frag_since,
6450 prev_nop_frag_holds, NOP_INSN);
6451 }
6452 else
6453 {
6454 /* Allow this instruction to replace one of the nops that was
6455 tentatively added to prev_nop_frag. */
df58fc94 6456 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
71400594
RS
6457 prev_nop_frag_holds--;
6458 prev_nop_frag_since++;
252b5132
RH
6459 }
6460 }
6461
e407c74b 6462 method = get_append_method (ip, address_expr, reloc_type);
2b0c8b40 6463 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
a4e06468 6464
e410add4
RS
6465 dwarf2_emit_insn (0);
6466 /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
6467 so "move" the instruction address accordingly.
6468
6469 Also, it doesn't seem appropriate for the assembler to reorder .loc
6470 entries. If this instruction is a branch that we are going to swap
6471 with the previous instruction, the two instructions should be
6472 treated as a unit, and the debug information for both instructions
6473 should refer to the start of the branch sequence. Using the
6474 current position is certainly wrong when swapping a 32-bit branch
6475 and a 16-bit delay slot, since the current position would then be
6476 in the middle of a branch. */
6477 dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
58e2ea4d 6478
df58fc94
RS
6479 relax32 = (mips_relax_branch
6480 /* Don't try branch relaxation within .set nomacro, or within
6481 .set noat if we use $at for PIC computations. If it turns
6482 out that the branch was out-of-range, we'll get an error. */
6483 && !mips_opts.warn_about_macros
6484 && (mips_opts.at || mips_pic == NO_PIC)
3bf0dbfb
MR
6485 /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
6486 as they have no complementing branches. */
6487 && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
df58fc94
RS
6488
6489 if (!HAVE_CODE_COMPRESSION
6490 && address_expr
6491 && relax32
0b25d3e6 6492 && *reloc_type == BFD_RELOC_16_PCREL_S2
11625dd8 6493 && delayed_branch_p (ip))
4a6a3df4 6494 {
895921c9 6495 relaxed_branch = TRUE;
1e915849
RS
6496 add_relaxed_insn (ip, (relaxed_branch_length
6497 (NULL, NULL,
11625dd8
RS
6498 uncond_branch_p (ip) ? -1
6499 : branch_likely_p (ip) ? 1
1e915849
RS
6500 : 0)), 4,
6501 RELAX_BRANCH_ENCODE
66b3e8da 6502 (AT,
11625dd8
RS
6503 uncond_branch_p (ip),
6504 branch_likely_p (ip),
1e915849
RS
6505 pinfo & INSN_WRITE_GPR_31,
6506 0),
6507 address_expr->X_add_symbol,
6508 address_expr->X_add_number);
4a6a3df4
AO
6509 *reloc_type = BFD_RELOC_UNUSED;
6510 }
df58fc94
RS
6511 else if (mips_opts.micromips
6512 && address_expr
6513 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
6514 || *reloc_type > BFD_RELOC_UNUSED)
40209cad
MR
6515 && (delayed_branch_p (ip) || compact_branch_p (ip))
6516 /* Don't try branch relaxation when users specify
6517 16-bit/32-bit instructions. */
6518 && !forced_insn_length)
df58fc94
RS
6519 {
6520 bfd_boolean relax16 = *reloc_type > BFD_RELOC_UNUSED;
6521 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
11625dd8
RS
6522 int uncond = uncond_branch_p (ip) ? -1 : 0;
6523 int compact = compact_branch_p (ip);
df58fc94
RS
6524 int al = pinfo & INSN_WRITE_GPR_31;
6525 int length32;
6526
6527 gas_assert (address_expr != NULL);
6528 gas_assert (!mips_relax.sequence);
6529
2b0c8b40 6530 relaxed_branch = TRUE;
df58fc94
RS
6531 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
6532 add_relaxed_insn (ip, relax32 ? length32 : 4, relax16 ? 2 : 4,
40209cad
MR
6533 RELAX_MICROMIPS_ENCODE (type, AT, uncond, compact, al,
6534 relax32, 0, 0),
df58fc94
RS
6535 address_expr->X_add_symbol,
6536 address_expr->X_add_number);
6537 *reloc_type = BFD_RELOC_UNUSED;
6538 }
6539 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
6540 {
6541 /* We need to set up a variant frag. */
df58fc94 6542 gas_assert (address_expr != NULL);
1e915849
RS
6543 add_relaxed_insn (ip, 4, 0,
6544 RELAX_MIPS16_ENCODE
6545 (*reloc_type - BFD_RELOC_UNUSED,
df58fc94 6546 forced_insn_length == 2, forced_insn_length == 4,
11625dd8 6547 delayed_branch_p (&history[0]),
1e915849
RS
6548 history[0].mips16_absolute_jump_p),
6549 make_expr_symbol (address_expr), 0);
252b5132 6550 }
5c04167a 6551 else if (mips_opts.mips16 && insn_length (ip) == 2)
9497f5ac 6552 {
11625dd8 6553 if (!delayed_branch_p (ip))
b8ee1a6e
DU
6554 /* Make sure there is enough room to swap this instruction with
6555 a following jump instruction. */
6556 frag_grow (6);
1e915849 6557 add_fixed_insn (ip);
252b5132
RH
6558 }
6559 else
6560 {
6561 if (mips_opts.mips16
6562 && mips_opts.noreorder
11625dd8 6563 && delayed_branch_p (&history[0]))
252b5132
RH
6564 as_warn (_("extended instruction in delay slot"));
6565
4d7206a2
RS
6566 if (mips_relax.sequence)
6567 {
6568 /* If we've reached the end of this frag, turn it into a variant
6569 frag and record the information for the instructions we've
6570 written so far. */
6571 if (frag_room () < 4)
6572 relax_close_frag ();
df58fc94 6573 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4d7206a2
RS
6574 }
6575
584892a6 6576 if (mips_relax.sequence != 2)
df58fc94
RS
6577 {
6578 if (mips_macro_warning.first_insn_sizes[0] == 0)
6579 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
6580 mips_macro_warning.sizes[0] += insn_length (ip);
6581 mips_macro_warning.insns[0]++;
6582 }
584892a6 6583 if (mips_relax.sequence != 1)
df58fc94
RS
6584 {
6585 if (mips_macro_warning.first_insn_sizes[1] == 0)
6586 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
6587 mips_macro_warning.sizes[1] += insn_length (ip);
6588 mips_macro_warning.insns[1]++;
6589 }
584892a6 6590
1e915849
RS
6591 if (mips_opts.mips16)
6592 {
6593 ip->fixed_p = 1;
6594 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
6595 }
6596 add_fixed_insn (ip);
252b5132
RH
6597 }
6598
9fe77896 6599 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
252b5132 6600 {
df58fc94 6601 bfd_reloc_code_real_type final_type[3];
2309ddf2 6602 reloc_howto_type *howto0;
9fe77896
RS
6603 reloc_howto_type *howto;
6604 int i;
34ce925e 6605
df58fc94
RS
6606 /* Perform any necessary conversion to microMIPS relocations
6607 and find out how many relocations there actually are. */
6608 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
6609 final_type[i] = micromips_map_reloc (reloc_type[i]);
6610
9fe77896
RS
6611 /* In a compound relocation, it is the final (outermost)
6612 operator that determines the relocated field. */
2309ddf2 6613 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
e8044f35
RS
6614 if (!howto)
6615 abort ();
2309ddf2
MR
6616
6617 if (i > 1)
6618 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
9fe77896
RS
6619 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
6620 bfd_get_reloc_size (howto),
6621 address_expr,
2309ddf2
MR
6622 howto0 && howto0->pc_relative,
6623 final_type[0]);
9fe77896
RS
6624
6625 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
2309ddf2 6626 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
9fe77896
RS
6627 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
6628
6629 /* These relocations can have an addend that won't fit in
6630 4 octets for 64bit assembly. */
6631 if (HAVE_64BIT_GPRS
6632 && ! howto->partial_inplace
6633 && (reloc_type[0] == BFD_RELOC_16
6634 || reloc_type[0] == BFD_RELOC_32
6635 || reloc_type[0] == BFD_RELOC_MIPS_JMP
6636 || reloc_type[0] == BFD_RELOC_GPREL16
6637 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
6638 || reloc_type[0] == BFD_RELOC_GPREL32
6639 || reloc_type[0] == BFD_RELOC_64
6640 || reloc_type[0] == BFD_RELOC_CTOR
6641 || reloc_type[0] == BFD_RELOC_MIPS_SUB
6642 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
6643 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
6644 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
6645 || reloc_type[0] == BFD_RELOC_MIPS_REL16
6646 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
6647 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
6648 || hi16_reloc_p (reloc_type[0])
6649 || lo16_reloc_p (reloc_type[0])))
6650 ip->fixp[0]->fx_no_overflow = 1;
6651
ddaf2c41
MR
6652 /* These relocations can have an addend that won't fit in 2 octets. */
6653 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
6654 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
6655 ip->fixp[0]->fx_no_overflow = 1;
6656
9fe77896
RS
6657 if (mips_relax.sequence)
6658 {
6659 if (mips_relax.first_fixup == 0)
6660 mips_relax.first_fixup = ip->fixp[0];
6661 }
6662 else if (reloc_needs_lo_p (*reloc_type))
6663 {
6664 struct mips_hi_fixup *hi_fixup;
6665
6666 /* Reuse the last entry if it already has a matching %lo. */
6667 hi_fixup = mips_hi_fixup_list;
6668 if (hi_fixup == 0
6669 || !fixup_has_matching_lo_p (hi_fixup->fixp))
4d7206a2 6670 {
9fe77896
RS
6671 hi_fixup = ((struct mips_hi_fixup *)
6672 xmalloc (sizeof (struct mips_hi_fixup)));
6673 hi_fixup->next = mips_hi_fixup_list;
6674 mips_hi_fixup_list = hi_fixup;
4d7206a2 6675 }
9fe77896
RS
6676 hi_fixup->fixp = ip->fixp[0];
6677 hi_fixup->seg = now_seg;
6678 }
252b5132 6679
9fe77896
RS
6680 /* Add fixups for the second and third relocations, if given.
6681 Note that the ABI allows the second relocation to be
6682 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
6683 moment we only use RSS_UNDEF, but we could add support
6684 for the others if it ever becomes necessary. */
6685 for (i = 1; i < 3; i++)
6686 if (reloc_type[i] != BFD_RELOC_UNUSED)
6687 {
6688 ip->fixp[i] = fix_new (ip->frag, ip->where,
6689 ip->fixp[0]->fx_size, NULL, 0,
df58fc94 6690 FALSE, final_type[i]);
f6688943 6691
9fe77896
RS
6692 /* Use fx_tcbit to mark compound relocs. */
6693 ip->fixp[0]->fx_tcbit = 1;
6694 ip->fixp[i]->fx_tcbit = 1;
6695 }
252b5132 6696 }
1e915849 6697 install_insn (ip);
252b5132
RH
6698
6699 /* Update the register mask information. */
4c260379
RS
6700 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
6701 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
252b5132 6702
a4e06468 6703 switch (method)
252b5132 6704 {
a4e06468
RS
6705 case APPEND_ADD:
6706 insert_into_history (0, 1, ip);
6707 break;
6708
6709 case APPEND_ADD_WITH_NOP:
14fe068b
RS
6710 {
6711 struct mips_cl_insn *nop;
6712
6713 insert_into_history (0, 1, ip);
6714 nop = get_delay_slot_nop (ip);
6715 add_fixed_insn (nop);
6716 insert_into_history (0, 1, nop);
6717 if (mips_relax.sequence)
6718 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
6719 }
a4e06468
RS
6720 break;
6721
6722 case APPEND_ADD_COMPACT:
6723 /* Convert MIPS16 jr/jalr into a "compact" jump. */
6724 gas_assert (mips_opts.mips16);
6725 ip->insn_opcode |= 0x0080;
6726 find_altered_mips16_opcode (ip);
6727 install_insn (ip);
6728 insert_into_history (0, 1, ip);
6729 break;
6730
6731 case APPEND_SWAP:
6732 {
6733 struct mips_cl_insn delay = history[0];
6734 if (mips_opts.mips16)
6735 {
6736 know (delay.frag == ip->frag);
6737 move_insn (ip, delay.frag, delay.where);
6738 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
6739 }
464ab0e5 6740 else if (relaxed_branch || delay.frag != ip->frag)
a4e06468
RS
6741 {
6742 /* Add the delay slot instruction to the end of the
6743 current frag and shrink the fixed part of the
6744 original frag. If the branch occupies the tail of
6745 the latter, move it backwards to cover the gap. */
2b0c8b40 6746 delay.frag->fr_fix -= branch_disp;
a4e06468 6747 if (delay.frag == ip->frag)
2b0c8b40 6748 move_insn (ip, ip->frag, ip->where - branch_disp);
a4e06468
RS
6749 add_fixed_insn (&delay);
6750 }
6751 else
6752 {
2b0c8b40
MR
6753 move_insn (&delay, ip->frag,
6754 ip->where - branch_disp + insn_length (ip));
a4e06468
RS
6755 move_insn (ip, history[0].frag, history[0].where);
6756 }
6757 history[0] = *ip;
6758 delay.fixed_p = 1;
6759 insert_into_history (0, 1, &delay);
6760 }
6761 break;
252b5132
RH
6762 }
6763
13408f1e 6764 /* If we have just completed an unconditional branch, clear the history. */
11625dd8
RS
6765 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
6766 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
e407c74b
NC
6767 {
6768 unsigned int i;
6769
79850f26 6770 mips_no_prev_insn ();
13408f1e 6771
e407c74b 6772 for (i = 0; i < ARRAY_SIZE (history); i++)
79850f26 6773 history[i].cleared_p = 1;
e407c74b
NC
6774 }
6775
df58fc94
RS
6776 /* We need to emit a label at the end of branch-likely macros. */
6777 if (emit_branch_likely_macro)
6778 {
6779 emit_branch_likely_macro = FALSE;
6780 micromips_add_label ();
6781 }
6782
252b5132
RH
6783 /* We just output an insn, so the next one doesn't have a label. */
6784 mips_clear_insn_labels ();
252b5132
RH
6785}
6786
e407c74b
NC
6787/* Forget that there was any previous instruction or label.
6788 When BRANCH is true, the branch history is also flushed. */
252b5132
RH
6789
6790static void
7d10b47d 6791mips_no_prev_insn (void)
252b5132 6792{
7d10b47d
RS
6793 prev_nop_frag = NULL;
6794 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
6795 mips_clear_insn_labels ();
6796}
6797
7d10b47d
RS
6798/* This function must be called before we emit something other than
6799 instructions. It is like mips_no_prev_insn except that it inserts
6800 any NOPS that might be needed by previous instructions. */
252b5132 6801
7d10b47d
RS
6802void
6803mips_emit_delays (void)
252b5132
RH
6804{
6805 if (! mips_opts.noreorder)
6806 {
932d1a1b 6807 int nops = nops_for_insn (0, history, NULL);
252b5132
RH
6808 if (nops > 0)
6809 {
7d10b47d
RS
6810 while (nops-- > 0)
6811 add_fixed_insn (NOP_INSN);
462427c4 6812 mips_move_text_labels ();
7d10b47d
RS
6813 }
6814 }
6815 mips_no_prev_insn ();
6816}
6817
6818/* Start a (possibly nested) noreorder block. */
6819
6820static void
6821start_noreorder (void)
6822{
6823 if (mips_opts.noreorder == 0)
6824 {
6825 unsigned int i;
6826 int nops;
6827
6828 /* None of the instructions before the .set noreorder can be moved. */
6829 for (i = 0; i < ARRAY_SIZE (history); i++)
6830 history[i].fixed_p = 1;
6831
6832 /* Insert any nops that might be needed between the .set noreorder
6833 block and the previous instructions. We will later remove any
6834 nops that turn out not to be needed. */
932d1a1b 6835 nops = nops_for_insn (0, history, NULL);
7d10b47d
RS
6836 if (nops > 0)
6837 {
6838 if (mips_optimize != 0)
252b5132
RH
6839 {
6840 /* Record the frag which holds the nop instructions, so
6841 that we can remove them if we don't need them. */
df58fc94 6842 frag_grow (nops * NOP_INSN_SIZE);
252b5132
RH
6843 prev_nop_frag = frag_now;
6844 prev_nop_frag_holds = nops;
6845 prev_nop_frag_required = 0;
6846 prev_nop_frag_since = 0;
6847 }
6848
6849 for (; nops > 0; --nops)
1e915849 6850 add_fixed_insn (NOP_INSN);
252b5132 6851
7d10b47d
RS
6852 /* Move on to a new frag, so that it is safe to simply
6853 decrease the size of prev_nop_frag. */
6854 frag_wane (frag_now);
6855 frag_new (0);
462427c4 6856 mips_move_text_labels ();
252b5132 6857 }
df58fc94 6858 mips_mark_labels ();
7d10b47d 6859 mips_clear_insn_labels ();
252b5132 6860 }
7d10b47d
RS
6861 mips_opts.noreorder++;
6862 mips_any_noreorder = 1;
6863}
252b5132 6864
7d10b47d 6865/* End a nested noreorder block. */
252b5132 6866
7d10b47d
RS
6867static void
6868end_noreorder (void)
6869{
6870 mips_opts.noreorder--;
6871 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
6872 {
6873 /* Commit to inserting prev_nop_frag_required nops and go back to
6874 handling nop insertion the .set reorder way. */
6875 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
df58fc94 6876 * NOP_INSN_SIZE);
7d10b47d
RS
6877 insert_into_history (prev_nop_frag_since,
6878 prev_nop_frag_required, NOP_INSN);
6879 prev_nop_frag = NULL;
6880 }
252b5132
RH
6881}
6882
97d87491
RS
6883/* Sign-extend 32-bit mode constants that have bit 31 set and all
6884 higher bits unset. */
6885
6886static void
6887normalize_constant_expr (expressionS *ex)
6888{
6889 if (ex->X_op == O_constant
6890 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
6891 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
6892 - 0x80000000);
6893}
6894
6895/* Sign-extend 32-bit mode address offsets that have bit 31 set and
6896 all higher bits unset. */
6897
6898static void
6899normalize_address_expr (expressionS *ex)
6900{
6901 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
6902 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
6903 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
6904 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
6905 - 0x80000000);
6906}
6907
6908/* Try to match TOKENS against OPCODE, storing the result in INSN.
6909 Return true if the match was successful.
6910
6911 OPCODE_EXTRA is a value that should be ORed into the opcode
6912 (used for VU0 channel suffixes, etc.). MORE_ALTS is true if
6913 there are more alternatives after OPCODE and SOFT_MATCH is
6914 as for mips_arg_info. */
6915
6916static bfd_boolean
6917match_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
6918 struct mips_operand_token *tokens, unsigned int opcode_extra,
6919 bfd_boolean more_alts, bfd_boolean soft_match)
6920{
6921 const char *args;
6922 struct mips_arg_info arg;
6923 const struct mips_operand *operand;
6924 char c;
6925
6926 imm_expr.X_op = O_absent;
6927 imm2_expr.X_op = O_absent;
6928 offset_expr.X_op = O_absent;
6929 offset_reloc[0] = BFD_RELOC_UNUSED;
6930 offset_reloc[1] = BFD_RELOC_UNUSED;
6931 offset_reloc[2] = BFD_RELOC_UNUSED;
6932
6933 create_insn (insn, opcode);
6934 insn->insn_opcode |= opcode_extra;
6935 insn_error = NULL;
6936 memset (&arg, 0, sizeof (arg));
6937 arg.insn = insn;
6938 arg.token = tokens;
6939 arg.argnum = 1;
6940 arg.last_regno = ILLEGAL_REG;
6941 arg.dest_regno = ILLEGAL_REG;
6942 arg.soft_match = soft_match;
6943 for (args = opcode->args;; ++args)
6944 {
6945 if (arg.token->type == OT_END)
6946 {
6947 /* Handle unary instructions in which only one operand is given.
6948 The source is then the same as the destination. */
6949 if (arg.opnum == 1 && *args == ',')
6950 {
6951 operand = (mips_opts.micromips
6952 ? decode_micromips_operand (args + 1)
6953 : decode_mips_operand (args + 1));
6954 if (operand && mips_optional_operand_p (operand))
6955 {
6956 arg.token = tokens;
6957 arg.argnum = 1;
6958 continue;
6959 }
6960 }
6961
6962 /* Treat elided base registers as $0. */
6963 if (strcmp (args, "(b)") == 0)
6964 args += 3;
6965
6966 if (args[0] == '+')
6967 switch (args[1])
6968 {
6969 case 'K':
6970 case 'N':
6971 /* The register suffix is optional. */
6972 args += 2;
6973 break;
6974 }
6975
6976 /* Fail the match if there were too few operands. */
6977 if (*args)
6978 return FALSE;
6979
6980 /* Successful match. */
6981 if (arg.dest_regno == arg.last_regno
6982 && strncmp (insn->insn_mo->name, "jalr", 4) == 0)
6983 {
6984 if (arg.opnum == 2)
6985 as_bad (_("Source and destination must be different"));
6986 else if (arg.last_regno == 31)
6987 as_bad (_("A destination register must be supplied"));
6988 }
6989 check_completed_insn (&arg);
6990 return TRUE;
6991 }
6992
6993 /* Fail the match if the line has too many operands. */
6994 if (*args == 0)
6995 return FALSE;
6996
6997 /* Handle characters that need to match exactly. */
6998 if (*args == '(' || *args == ')' || *args == ',')
6999 {
7000 if (match_char (&arg, *args))
7001 continue;
7002 return FALSE;
7003 }
7004 if (*args == '#')
7005 {
7006 ++args;
7007 if (arg.token->type == OT_DOUBLE_CHAR
7008 && arg.token->u.ch == *args)
7009 {
7010 ++arg.token;
7011 continue;
7012 }
7013 return FALSE;
7014 }
7015
7016 /* Handle special macro operands. Work out the properties of
7017 other operands. */
7018 arg.opnum += 1;
7019 arg.lax_max = FALSE;
7020 switch (*args)
7021 {
7022 case '+':
7023 switch (args[1])
7024 {
7025 case '1':
7026 case '2':
7027 case '3':
7028 case '4':
7029 case 'B':
7030 case 'C':
7031 case 'F':
7032 case 'G':
7033 case 'H':
7034 case 'J':
7035 case 'Q':
7036 case 'S':
7037 case 's':
7038 /* If these integer forms come last, there is no other
7039 form of the instruction that could match. Prefer to
7040 give detailed error messages where possible. */
7041 if (args[2] == 0)
7042 arg.soft_match = FALSE;
7043 break;
7044
7045 case 'I':
7046 /* "+I" is like "I", except that imm2_expr is used. */
7047 if (match_const_int (&arg, &imm2_expr.X_add_number, 0))
7048 imm2_expr.X_op = O_constant;
7049 else
7050 insn_error = _("absolute expression required");
7051 if (HAVE_32BIT_GPRS)
7052 normalize_constant_expr (&imm2_expr);
7053 ++args;
7054 continue;
7055
7056 case 'i':
7057 *offset_reloc = BFD_RELOC_MIPS_JMP;
7058 break;
7059 }
7060 break;
7061
7062 case '\'':
7063 case ':':
7064 case '@':
7065 case '^':
7066 case '$':
7067 case '\\':
7068 case '%':
7069 case '|':
7070 case '0':
7071 case '1':
7072 case '2':
7073 case '3':
7074 case '4':
7075 case '5':
7076 case '6':
7077 case '8':
7078 case 'B':
7079 case 'C':
7080 case 'J':
7081 case 'O':
7082 case 'P':
7083 case 'Q':
7084 case 'c':
7085 case 'h':
7086 case 'q':
7087 /* If these integer forms come last, there is no other
7088 form of the instruction that could match. Prefer to
7089 give detailed error messages where possible. */
7090 if (args[1] == 0)
7091 arg.soft_match = FALSE;
7092 break;
7093
7094 case 'I':
7095 if (match_const_int (&arg, &imm_expr.X_add_number, 0))
7096 imm_expr.X_op = O_constant;
7097 else
7098 insn_error = _("absolute expression required");
7099 if (HAVE_32BIT_GPRS)
7100 normalize_constant_expr (&imm_expr);
7101 continue;
7102
7103 case 'A':
7104 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
7105 {
7106 /* Assume that the offset has been elided and that what
7107 we saw was a base register. The match will fail later
7108 if that assumption turns out to be wrong. */
7109 offset_expr.X_op = O_constant;
7110 offset_expr.X_add_number = 0;
7111 }
7112 else if (match_expression (&arg, &offset_expr, offset_reloc))
7113 normalize_address_expr (&offset_expr);
7114 else
7115 insn_error = _("absolute expression required");
7116 continue;
7117
7118 case 'F':
7119 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
7120 8, TRUE))
7121 insn_error = _("floating-point expression required");
7122 continue;
7123
7124 case 'L':
7125 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
7126 8, FALSE))
7127 insn_error = _("floating-point expression required");
7128 continue;
7129
7130 case 'f':
7131 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
7132 4, TRUE))
7133 insn_error = _("floating-point expression required");
7134 continue;
7135
7136 case 'l':
7137 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
7138 4, FALSE))
7139 insn_error = _("floating-point expression required");
7140 continue;
7141
7142 /* ??? This is the traditional behavior, but is flaky if
7143 there are alternative versions of the same instruction
7144 for different subarchitectures. The next alternative
7145 might not be suitable. */
7146 case 'j':
7147 /* For compatibility with older assemblers, we accept
7148 0x8000-0xffff as signed 16-bit numbers when only
7149 signed numbers are allowed. */
7150 arg.lax_max = !more_alts;
7151 case 'i':
7152 /* Only accept non-constant operands if this is the
7153 final alternative. Later alternatives might include
7154 a macro implementation. */
7155 arg.allow_nonconst = !more_alts;
7156 break;
7157
7158 case 'u':
7159 /* There are no macro implementations for out-of-range values. */
7160 arg.allow_nonconst = TRUE;
7161 break;
7162
7163 case 'o':
7164 /* There should always be a macro implementation. */
7165 arg.allow_nonconst = FALSE;
7166 break;
7167
7168 case 'p':
7169 *offset_reloc = BFD_RELOC_16_PCREL_S2;
7170 break;
7171
7172 case 'a':
7173 *offset_reloc = BFD_RELOC_MIPS_JMP;
7174 break;
7175
7176 case 'm':
7177 gas_assert (mips_opts.micromips);
7178 c = args[1];
7179 switch (c)
7180 {
7181 case 'D':
7182 case 'E':
7183 if (!forced_insn_length)
7184 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
7185 else if (c == 'D')
7186 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
7187 else
7188 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
7189 break;
7190 }
7191 break;
7192 }
7193
7194 operand = (mips_opts.micromips
7195 ? decode_micromips_operand (args)
7196 : decode_mips_operand (args));
7197 if (!operand)
7198 abort ();
7199
7200 /* Skip prefixes. */
7201 if (*args == '+' || *args == 'm')
7202 args++;
7203
7204 if (mips_optional_operand_p (operand)
7205 && args[1] == ','
7206 && (arg.token[0].type != OT_REG
7207 || arg.token[1].type == OT_END))
7208 {
7209 /* Assume that the register has been elided and is the
7210 same as the first operand. */
7211 arg.token = tokens;
7212 arg.argnum = 1;
7213 }
7214
7215 if (!match_operand (&arg, operand))
7216 return FALSE;
7217 }
7218}
7219
7220/* Like match_insn, but for MIPS16. */
7221
7222static bfd_boolean
7223match_mips16_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
7224 struct mips_operand_token *tokens, bfd_boolean soft_match)
7225{
7226 const char *args;
7227 const struct mips_operand *operand;
7228 const struct mips_operand *ext_operand;
7229 struct mips_arg_info arg;
7230 int relax_char;
7231
7232 create_insn (insn, opcode);
7233 imm_expr.X_op = O_absent;
7234 imm2_expr.X_op = O_absent;
7235 offset_expr.X_op = O_absent;
7236 offset_reloc[0] = BFD_RELOC_UNUSED;
7237 offset_reloc[1] = BFD_RELOC_UNUSED;
7238 offset_reloc[2] = BFD_RELOC_UNUSED;
7239 relax_char = 0;
7240
7241 memset (&arg, 0, sizeof (arg));
7242 arg.insn = insn;
7243 arg.token = tokens;
7244 arg.argnum = 1;
7245 arg.last_regno = ILLEGAL_REG;
7246 arg.dest_regno = ILLEGAL_REG;
7247 arg.soft_match = soft_match;
7248 relax_char = 0;
7249 for (args = opcode->args;; ++args)
7250 {
7251 int c;
7252
7253 if (arg.token->type == OT_END)
7254 {
7255 offsetT value;
7256
7257 /* Handle unary instructions in which only one operand is given.
7258 The source is then the same as the destination. */
7259 if (arg.opnum == 1 && *args == ',')
7260 {
7261 operand = decode_mips16_operand (args[1], FALSE);
7262 if (operand && mips_optional_operand_p (operand))
7263 {
7264 arg.token = tokens;
7265 arg.argnum = 1;
7266 continue;
7267 }
7268 }
7269
7270 /* Fail the match if there were too few operands. */
7271 if (*args)
7272 return FALSE;
7273
7274 /* Successful match. Stuff the immediate value in now, if
7275 we can. */
7276 if (opcode->pinfo == INSN_MACRO)
7277 {
7278 gas_assert (relax_char == 0 || relax_char == 'p');
7279 gas_assert (*offset_reloc == BFD_RELOC_UNUSED);
7280 }
7281 else if (relax_char
7282 && offset_expr.X_op == O_constant
7283 && calculate_reloc (*offset_reloc,
7284 offset_expr.X_add_number,
7285 &value))
7286 {
7287 mips16_immed (NULL, 0, relax_char, *offset_reloc, value,
7288 forced_insn_length, &insn->insn_opcode);
7289 offset_expr.X_op = O_absent;
7290 *offset_reloc = BFD_RELOC_UNUSED;
7291 }
7292 else if (relax_char && *offset_reloc != BFD_RELOC_UNUSED)
7293 {
7294 if (forced_insn_length == 2)
7295 as_bad (_("invalid unextended operand value"));
7296 forced_insn_length = 4;
7297 insn->insn_opcode |= MIPS16_EXTEND;
7298 }
7299 else if (relax_char)
7300 *offset_reloc = (int) BFD_RELOC_UNUSED + relax_char;
7301
7302 check_completed_insn (&arg);
7303 return TRUE;
7304 }
7305
7306 /* Fail the match if the line has too many operands. */
7307 if (*args == 0)
7308 return FALSE;
7309
7310 /* Handle characters that need to match exactly. */
7311 if (*args == '(' || *args == ')' || *args == ',')
7312 {
7313 if (match_char (&arg, *args))
7314 continue;
7315 return FALSE;
7316 }
7317
7318 arg.opnum += 1;
7319 c = *args;
7320 switch (c)
7321 {
7322 case 'p':
7323 case 'q':
7324 case 'A':
7325 case 'B':
7326 case 'E':
7327 relax_char = c;
7328 break;
7329
7330 case 'I':
7331 if (match_const_int (&arg, &imm_expr.X_add_number, 0))
7332 imm_expr.X_op = O_constant;
7333 else
7334 insn_error = _("absolute expression required");
7335 if (HAVE_32BIT_GPRS)
7336 normalize_constant_expr (&imm_expr);
7337 continue;
7338
7339 case 'a':
7340 case 'i':
7341 *offset_reloc = BFD_RELOC_MIPS16_JMP;
7342 insn->insn_opcode <<= 16;
7343 break;
7344 }
7345
7346 operand = decode_mips16_operand (c, FALSE);
7347 if (!operand)
7348 abort ();
7349
7350 /* '6' is a special case. It is used for BREAK and SDBBP,
7351 whose operands are only meaningful to the software that decodes
7352 them. This means that there is no architectural reason why
7353 they cannot be prefixed by EXTEND, but in practice,
7354 exception handlers will only look at the instruction
7355 itself. We therefore allow '6' to be extended when
7356 disassembling but not when assembling. */
7357 if (operand->type != OP_PCREL && c != '6')
7358 {
7359 ext_operand = decode_mips16_operand (c, TRUE);
7360 if (operand != ext_operand)
7361 {
7362 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
7363 {
7364 offset_expr.X_op = O_constant;
7365 offset_expr.X_add_number = 0;
7366 relax_char = c;
7367 continue;
7368 }
7369
7370 /* We need the OT_INTEGER check because some MIPS16
7371 immediate variants are listed before the register ones. */
7372 if (arg.token->type != OT_INTEGER
7373 || !match_expression (&arg, &offset_expr, offset_reloc))
7374 return FALSE;
7375
7376 /* '8' is used for SLTI(U) and has traditionally not
7377 been allowed to take relocation operators. */
7378 if (offset_reloc[0] != BFD_RELOC_UNUSED
7379 && (ext_operand->size != 16 || c == '8'))
7380 return FALSE;
7381
7382 relax_char = c;
7383 continue;
7384 }
7385 }
7386
7387 if (mips_optional_operand_p (operand)
7388 && args[1] == ','
7389 && (arg.token[0].type != OT_REG
7390 || arg.token[1].type == OT_END))
7391 {
7392 /* Assume that the register has been elided and is the
7393 same as the first operand. */
7394 arg.token = tokens;
7395 arg.argnum = 1;
7396 }
7397
7398 if (!match_operand (&arg, operand))
7399 return FALSE;
7400 }
7401}
7402
584892a6
RS
7403/* Set up global variables for the start of a new macro. */
7404
7405static void
7406macro_start (void)
7407{
7408 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
df58fc94
RS
7409 memset (&mips_macro_warning.first_insn_sizes, 0,
7410 sizeof (mips_macro_warning.first_insn_sizes));
7411 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
584892a6 7412 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
11625dd8 7413 && delayed_branch_p (&history[0]));
df58fc94
RS
7414 switch (history[0].insn_mo->pinfo2
7415 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
7416 {
7417 case INSN2_BRANCH_DELAY_32BIT:
7418 mips_macro_warning.delay_slot_length = 4;
7419 break;
7420 case INSN2_BRANCH_DELAY_16BIT:
7421 mips_macro_warning.delay_slot_length = 2;
7422 break;
7423 default:
7424 mips_macro_warning.delay_slot_length = 0;
7425 break;
7426 }
7427 mips_macro_warning.first_frag = NULL;
584892a6
RS
7428}
7429
df58fc94
RS
7430/* Given that a macro is longer than one instruction or of the wrong size,
7431 return the appropriate warning for it. Return null if no warning is
7432 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
7433 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
7434 and RELAX_NOMACRO. */
584892a6
RS
7435
7436static const char *
7437macro_warning (relax_substateT subtype)
7438{
7439 if (subtype & RELAX_DELAY_SLOT)
7440 return _("Macro instruction expanded into multiple instructions"
7441 " in a branch delay slot");
7442 else if (subtype & RELAX_NOMACRO)
7443 return _("Macro instruction expanded into multiple instructions");
df58fc94
RS
7444 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
7445 | RELAX_DELAY_SLOT_SIZE_SECOND))
7446 return ((subtype & RELAX_DELAY_SLOT_16BIT)
7447 ? _("Macro instruction expanded into a wrong size instruction"
7448 " in a 16-bit branch delay slot")
7449 : _("Macro instruction expanded into a wrong size instruction"
7450 " in a 32-bit branch delay slot"));
584892a6
RS
7451 else
7452 return 0;
7453}
7454
7455/* Finish up a macro. Emit warnings as appropriate. */
7456
7457static void
7458macro_end (void)
7459{
df58fc94
RS
7460 /* Relaxation warning flags. */
7461 relax_substateT subtype = 0;
7462
7463 /* Check delay slot size requirements. */
7464 if (mips_macro_warning.delay_slot_length == 2)
7465 subtype |= RELAX_DELAY_SLOT_16BIT;
7466 if (mips_macro_warning.delay_slot_length != 0)
584892a6 7467 {
df58fc94
RS
7468 if (mips_macro_warning.delay_slot_length
7469 != mips_macro_warning.first_insn_sizes[0])
7470 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
7471 if (mips_macro_warning.delay_slot_length
7472 != mips_macro_warning.first_insn_sizes[1])
7473 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
7474 }
584892a6 7475
df58fc94
RS
7476 /* Check instruction count requirements. */
7477 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
7478 {
7479 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
584892a6
RS
7480 subtype |= RELAX_SECOND_LONGER;
7481 if (mips_opts.warn_about_macros)
7482 subtype |= RELAX_NOMACRO;
7483 if (mips_macro_warning.delay_slot_p)
7484 subtype |= RELAX_DELAY_SLOT;
df58fc94 7485 }
584892a6 7486
df58fc94
RS
7487 /* If both alternatives fail to fill a delay slot correctly,
7488 emit the warning now. */
7489 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
7490 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
7491 {
7492 relax_substateT s;
7493 const char *msg;
7494
7495 s = subtype & (RELAX_DELAY_SLOT_16BIT
7496 | RELAX_DELAY_SLOT_SIZE_FIRST
7497 | RELAX_DELAY_SLOT_SIZE_SECOND);
7498 msg = macro_warning (s);
7499 if (msg != NULL)
7500 as_warn ("%s", msg);
7501 subtype &= ~s;
7502 }
7503
7504 /* If both implementations are longer than 1 instruction, then emit the
7505 warning now. */
7506 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
7507 {
7508 relax_substateT s;
7509 const char *msg;
7510
7511 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
7512 msg = macro_warning (s);
7513 if (msg != NULL)
7514 as_warn ("%s", msg);
7515 subtype &= ~s;
584892a6 7516 }
df58fc94
RS
7517
7518 /* If any flags still set, then one implementation might need a warning
7519 and the other either will need one of a different kind or none at all.
7520 Pass any remaining flags over to relaxation. */
7521 if (mips_macro_warning.first_frag != NULL)
7522 mips_macro_warning.first_frag->fr_subtype |= subtype;
584892a6
RS
7523}
7524
df58fc94
RS
7525/* Instruction operand formats used in macros that vary between
7526 standard MIPS and microMIPS code. */
7527
833794fc 7528static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
df58fc94
RS
7529static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
7530static const char * const jalr_fmt[2] = { "d,s", "t,s" };
7531static const char * const lui_fmt[2] = { "t,u", "s,u" };
7532static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
833794fc 7533static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
df58fc94
RS
7534static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
7535static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
7536
833794fc 7537#define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
df58fc94
RS
7538#define COP12_FMT (cop12_fmt[mips_opts.micromips])
7539#define JALR_FMT (jalr_fmt[mips_opts.micromips])
7540#define LUI_FMT (lui_fmt[mips_opts.micromips])
7541#define MEM12_FMT (mem12_fmt[mips_opts.micromips])
833794fc 7542#define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
df58fc94
RS
7543#define SHFT_FMT (shft_fmt[mips_opts.micromips])
7544#define TRAP_FMT (trap_fmt[mips_opts.micromips])
7545
6e1304d8
RS
7546/* Read a macro's relocation codes from *ARGS and store them in *R.
7547 The first argument in *ARGS will be either the code for a single
7548 relocation or -1 followed by the three codes that make up a
7549 composite relocation. */
7550
7551static void
7552macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
7553{
7554 int i, next;
7555
7556 next = va_arg (*args, int);
7557 if (next >= 0)
7558 r[0] = (bfd_reloc_code_real_type) next;
7559 else
f2ae14a1
RS
7560 {
7561 for (i = 0; i < 3; i++)
7562 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
7563 /* This function is only used for 16-bit relocation fields.
7564 To make the macro code simpler, treat an unrelocated value
7565 in the same way as BFD_RELOC_LO16. */
7566 if (r[0] == BFD_RELOC_UNUSED)
7567 r[0] = BFD_RELOC_LO16;
7568 }
6e1304d8
RS
7569}
7570
252b5132
RH
7571/* Build an instruction created by a macro expansion. This is passed
7572 a pointer to the count of instructions created so far, an
7573 expression, the name of the instruction to build, an operand format
7574 string, and corresponding arguments. */
7575
252b5132 7576static void
67c0d1eb 7577macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 7578{
df58fc94 7579 const struct mips_opcode *mo = NULL;
f6688943 7580 bfd_reloc_code_real_type r[3];
df58fc94 7581 const struct mips_opcode *amo;
e077a1c8 7582 const struct mips_operand *operand;
df58fc94
RS
7583 struct hash_control *hash;
7584 struct mips_cl_insn insn;
252b5132 7585 va_list args;
e077a1c8 7586 unsigned int uval;
252b5132 7587
252b5132 7588 va_start (args, fmt);
252b5132 7589
252b5132
RH
7590 if (mips_opts.mips16)
7591 {
03ea81db 7592 mips16_macro_build (ep, name, fmt, &args);
252b5132
RH
7593 va_end (args);
7594 return;
7595 }
7596
f6688943
TS
7597 r[0] = BFD_RELOC_UNUSED;
7598 r[1] = BFD_RELOC_UNUSED;
7599 r[2] = BFD_RELOC_UNUSED;
df58fc94
RS
7600 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
7601 amo = (struct mips_opcode *) hash_find (hash, name);
7602 gas_assert (amo);
7603 gas_assert (strcmp (name, amo->name) == 0);
1e915849 7604
df58fc94 7605 do
8b082fb1
TS
7606 {
7607 /* Search until we get a match for NAME. It is assumed here that
df58fc94
RS
7608 macros will never generate MDMX, MIPS-3D, or MT instructions.
7609 We try to match an instruction that fulfils the branch delay
7610 slot instruction length requirement (if any) of the previous
7611 instruction. While doing this we record the first instruction
7612 seen that matches all the other conditions and use it anyway
7613 if the requirement cannot be met; we will issue an appropriate
7614 warning later on. */
7615 if (strcmp (fmt, amo->args) == 0
7616 && amo->pinfo != INSN_MACRO
7617 && is_opcode_valid (amo)
7618 && is_size_valid (amo))
7619 {
7620 if (is_delay_slot_valid (amo))
7621 {
7622 mo = amo;
7623 break;
7624 }
7625 else if (!mo)
7626 mo = amo;
7627 }
8b082fb1 7628
df58fc94
RS
7629 ++amo;
7630 gas_assert (amo->name);
252b5132 7631 }
df58fc94 7632 while (strcmp (name, amo->name) == 0);
252b5132 7633
df58fc94 7634 gas_assert (mo);
1e915849 7635 create_insn (&insn, mo);
e077a1c8 7636 for (; *fmt; ++fmt)
252b5132 7637 {
e077a1c8 7638 switch (*fmt)
252b5132 7639 {
252b5132
RH
7640 case ',':
7641 case '(':
7642 case ')':
252b5132 7643 case 'z':
e077a1c8 7644 break;
252b5132
RH
7645
7646 case 'i':
7647 case 'j':
6e1304d8 7648 macro_read_relocs (&args, r);
9c2799c2 7649 gas_assert (*r == BFD_RELOC_GPREL16
e391c024
RS
7650 || *r == BFD_RELOC_MIPS_HIGHER
7651 || *r == BFD_RELOC_HI16_S
7652 || *r == BFD_RELOC_LO16
7653 || *r == BFD_RELOC_MIPS_GOT_OFST);
e077a1c8 7654 break;
e391c024
RS
7655
7656 case 'o':
7657 macro_read_relocs (&args, r);
e077a1c8 7658 break;
252b5132
RH
7659
7660 case 'u':
6e1304d8 7661 macro_read_relocs (&args, r);
9c2799c2 7662 gas_assert (ep != NULL
90ecf173
MR
7663 && (ep->X_op == O_constant
7664 || (ep->X_op == O_symbol
7665 && (*r == BFD_RELOC_MIPS_HIGHEST
7666 || *r == BFD_RELOC_HI16_S
7667 || *r == BFD_RELOC_HI16
7668 || *r == BFD_RELOC_GPREL16
7669 || *r == BFD_RELOC_MIPS_GOT_HI16
7670 || *r == BFD_RELOC_MIPS_CALL_HI16))));
e077a1c8 7671 break;
252b5132
RH
7672
7673 case 'p':
9c2799c2 7674 gas_assert (ep != NULL);
bad36eac 7675
252b5132
RH
7676 /*
7677 * This allows macro() to pass an immediate expression for
7678 * creating short branches without creating a symbol.
bad36eac
DJ
7679 *
7680 * We don't allow branch relaxation for these branches, as
7681 * they should only appear in ".set nomacro" anyway.
252b5132
RH
7682 */
7683 if (ep->X_op == O_constant)
7684 {
df58fc94
RS
7685 /* For microMIPS we always use relocations for branches.
7686 So we should not resolve immediate values. */
7687 gas_assert (!mips_opts.micromips);
7688
bad36eac
DJ
7689 if ((ep->X_add_number & 3) != 0)
7690 as_bad (_("branch to misaligned address (0x%lx)"),
7691 (unsigned long) ep->X_add_number);
7692 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
7693 as_bad (_("branch address range overflow (0x%lx)"),
7694 (unsigned long) ep->X_add_number);
252b5132
RH
7695 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
7696 ep = NULL;
7697 }
7698 else
0b25d3e6 7699 *r = BFD_RELOC_16_PCREL_S2;
e077a1c8 7700 break;
252b5132
RH
7701
7702 case 'a':
9c2799c2 7703 gas_assert (ep != NULL);
f6688943 7704 *r = BFD_RELOC_MIPS_JMP;
e077a1c8 7705 break;
d43b4baf 7706
252b5132 7707 default:
e077a1c8
RS
7708 operand = (mips_opts.micromips
7709 ? decode_micromips_operand (fmt)
7710 : decode_mips_operand (fmt));
7711 if (!operand)
7712 abort ();
7713
7714 uval = va_arg (args, int);
7715 if (operand->type == OP_CLO_CLZ_DEST)
7716 uval |= (uval << 5);
7717 insn_insert_operand (&insn, operand, uval);
7718
7719 if (*fmt == '+' || *fmt == 'm')
7720 ++fmt;
7721 break;
252b5132 7722 }
252b5132
RH
7723 }
7724 va_end (args);
9c2799c2 7725 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 7726
df58fc94 7727 append_insn (&insn, ep, r, TRUE);
252b5132
RH
7728}
7729
7730static void
67c0d1eb 7731mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
03ea81db 7732 va_list *args)
252b5132 7733{
1e915849 7734 struct mips_opcode *mo;
252b5132 7735 struct mips_cl_insn insn;
e077a1c8 7736 const struct mips_operand *operand;
f6688943
TS
7737 bfd_reloc_code_real_type r[3]
7738 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 7739
1e915849 7740 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
9c2799c2
NC
7741 gas_assert (mo);
7742 gas_assert (strcmp (name, mo->name) == 0);
252b5132 7743
1e915849 7744 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 7745 {
1e915849 7746 ++mo;
9c2799c2
NC
7747 gas_assert (mo->name);
7748 gas_assert (strcmp (name, mo->name) == 0);
252b5132
RH
7749 }
7750
1e915849 7751 create_insn (&insn, mo);
e077a1c8 7752 for (; *fmt; ++fmt)
252b5132
RH
7753 {
7754 int c;
7755
e077a1c8 7756 c = *fmt;
252b5132
RH
7757 switch (c)
7758 {
252b5132
RH
7759 case ',':
7760 case '(':
7761 case ')':
e077a1c8 7762 break;
252b5132
RH
7763
7764 case '0':
7765 case 'S':
7766 case 'P':
7767 case 'R':
e077a1c8 7768 break;
252b5132
RH
7769
7770 case '<':
7771 case '>':
7772 case '4':
7773 case '5':
7774 case 'H':
7775 case 'W':
7776 case 'D':
7777 case 'j':
7778 case '8':
7779 case 'V':
7780 case 'C':
7781 case 'U':
7782 case 'k':
7783 case 'K':
7784 case 'p':
7785 case 'q':
7786 {
b886a2ab
RS
7787 offsetT value;
7788
9c2799c2 7789 gas_assert (ep != NULL);
252b5132
RH
7790
7791 if (ep->X_op != O_constant)
874e8986 7792 *r = (int) BFD_RELOC_UNUSED + c;
b886a2ab 7793 else if (calculate_reloc (*r, ep->X_add_number, &value))
252b5132 7794 {
b886a2ab 7795 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
252b5132 7796 ep = NULL;
f6688943 7797 *r = BFD_RELOC_UNUSED;
252b5132
RH
7798 }
7799 }
e077a1c8 7800 break;
252b5132 7801
e077a1c8
RS
7802 default:
7803 operand = decode_mips16_operand (c, FALSE);
7804 if (!operand)
7805 abort ();
252b5132 7806
4a06e5a2 7807 insn_insert_operand (&insn, operand, va_arg (*args, int));
e077a1c8
RS
7808 break;
7809 }
252b5132
RH
7810 }
7811
9c2799c2 7812 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 7813
df58fc94 7814 append_insn (&insn, ep, r, TRUE);
252b5132
RH
7815}
7816
438c16b8
TS
7817/*
7818 * Generate a "jalr" instruction with a relocation hint to the called
7819 * function. This occurs in NewABI PIC code.
7820 */
7821static void
df58fc94 7822macro_build_jalr (expressionS *ep, int cprestore)
438c16b8 7823{
df58fc94
RS
7824 static const bfd_reloc_code_real_type jalr_relocs[2]
7825 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
7826 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
7827 const char *jalr;
685736be 7828 char *f = NULL;
b34976b6 7829
1180b5a4 7830 if (MIPS_JALR_HINT_P (ep))
f21f8242 7831 {
cc3d92a5 7832 frag_grow (8);
f21f8242
AO
7833 f = frag_more (0);
7834 }
2906b037 7835 if (mips_opts.micromips)
df58fc94 7836 {
833794fc
MR
7837 jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
7838 ? "jalr" : "jalrs");
e64af278 7839 if (MIPS_JALR_HINT_P (ep)
833794fc 7840 || mips_opts.insn32
e64af278 7841 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
7842 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
7843 else
7844 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
7845 }
2906b037
MR
7846 else
7847 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
1180b5a4 7848 if (MIPS_JALR_HINT_P (ep))
df58fc94 7849 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
438c16b8
TS
7850}
7851
252b5132
RH
7852/*
7853 * Generate a "lui" instruction.
7854 */
7855static void
67c0d1eb 7856macro_build_lui (expressionS *ep, int regnum)
252b5132 7857{
9c2799c2 7858 gas_assert (! mips_opts.mips16);
252b5132 7859
df58fc94 7860 if (ep->X_op != O_constant)
252b5132 7861 {
9c2799c2 7862 gas_assert (ep->X_op == O_symbol);
bbe506e8
TS
7863 /* _gp_disp is a special case, used from s_cpload.
7864 __gnu_local_gp is used if mips_no_shared. */
9c2799c2 7865 gas_assert (mips_pic == NO_PIC
78e1bb40 7866 || (! HAVE_NEWABI
aa6975fb
ILT
7867 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
7868 || (! mips_in_shared
bbe506e8
TS
7869 && strcmp (S_GET_NAME (ep->X_add_symbol),
7870 "__gnu_local_gp") == 0));
252b5132
RH
7871 }
7872
df58fc94 7873 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
252b5132
RH
7874}
7875
885add95
CD
7876/* Generate a sequence of instructions to do a load or store from a constant
7877 offset off of a base register (breg) into/from a target register (treg),
7878 using AT if necessary. */
7879static void
67c0d1eb
RS
7880macro_build_ldst_constoffset (expressionS *ep, const char *op,
7881 int treg, int breg, int dbl)
885add95 7882{
9c2799c2 7883 gas_assert (ep->X_op == O_constant);
885add95 7884
256ab948 7885 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
7886 if (!dbl)
7887 normalize_constant_expr (ep);
256ab948 7888
67c1ffbe 7889 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 7890 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
7891 as_warn (_("operand overflow"));
7892
7893 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
7894 {
7895 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 7896 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
7897 }
7898 else
7899 {
7900 /* 32-bit offset, need multiple instructions and AT, like:
7901 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
7902 addu $tempreg,$tempreg,$breg
7903 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
7904 to handle the complete offset. */
67c0d1eb
RS
7905 macro_build_lui (ep, AT);
7906 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7907 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95 7908
741fe287 7909 if (!mips_opts.at)
8fc2e39e 7910 as_bad (_("Macro used $at after \".set noat\""));
885add95
CD
7911 }
7912}
7913
252b5132
RH
7914/* set_at()
7915 * Generates code to set the $at register to true (one)
7916 * if reg is less than the immediate expression.
7917 */
7918static void
67c0d1eb 7919set_at (int reg, int unsignedp)
252b5132
RH
7920{
7921 if (imm_expr.X_op == O_constant
7922 && imm_expr.X_add_number >= -0x8000
7923 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
7924 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
7925 AT, reg, BFD_RELOC_LO16);
252b5132
RH
7926 else
7927 {
67c0d1eb
RS
7928 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7929 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
7930 }
7931}
7932
252b5132
RH
7933/* Count the leading zeroes by performing a binary chop. This is a
7934 bulky bit of source, but performance is a LOT better for the
7935 majority of values than a simple loop to count the bits:
7936 for (lcnt = 0; (lcnt < 32); lcnt++)
7937 if ((v) & (1 << (31 - lcnt)))
7938 break;
7939 However it is not code size friendly, and the gain will drop a bit
7940 on certain cached systems.
7941*/
7942#define COUNT_TOP_ZEROES(v) \
7943 (((v) & ~0xffff) == 0 \
7944 ? ((v) & ~0xff) == 0 \
7945 ? ((v) & ~0xf) == 0 \
7946 ? ((v) & ~0x3) == 0 \
7947 ? ((v) & ~0x1) == 0 \
7948 ? !(v) \
7949 ? 32 \
7950 : 31 \
7951 : 30 \
7952 : ((v) & ~0x7) == 0 \
7953 ? 29 \
7954 : 28 \
7955 : ((v) & ~0x3f) == 0 \
7956 ? ((v) & ~0x1f) == 0 \
7957 ? 27 \
7958 : 26 \
7959 : ((v) & ~0x7f) == 0 \
7960 ? 25 \
7961 : 24 \
7962 : ((v) & ~0xfff) == 0 \
7963 ? ((v) & ~0x3ff) == 0 \
7964 ? ((v) & ~0x1ff) == 0 \
7965 ? 23 \
7966 : 22 \
7967 : ((v) & ~0x7ff) == 0 \
7968 ? 21 \
7969 : 20 \
7970 : ((v) & ~0x3fff) == 0 \
7971 ? ((v) & ~0x1fff) == 0 \
7972 ? 19 \
7973 : 18 \
7974 : ((v) & ~0x7fff) == 0 \
7975 ? 17 \
7976 : 16 \
7977 : ((v) & ~0xffffff) == 0 \
7978 ? ((v) & ~0xfffff) == 0 \
7979 ? ((v) & ~0x3ffff) == 0 \
7980 ? ((v) & ~0x1ffff) == 0 \
7981 ? 15 \
7982 : 14 \
7983 : ((v) & ~0x7ffff) == 0 \
7984 ? 13 \
7985 : 12 \
7986 : ((v) & ~0x3fffff) == 0 \
7987 ? ((v) & ~0x1fffff) == 0 \
7988 ? 11 \
7989 : 10 \
7990 : ((v) & ~0x7fffff) == 0 \
7991 ? 9 \
7992 : 8 \
7993 : ((v) & ~0xfffffff) == 0 \
7994 ? ((v) & ~0x3ffffff) == 0 \
7995 ? ((v) & ~0x1ffffff) == 0 \
7996 ? 7 \
7997 : 6 \
7998 : ((v) & ~0x7ffffff) == 0 \
7999 ? 5 \
8000 : 4 \
8001 : ((v) & ~0x3fffffff) == 0 \
8002 ? ((v) & ~0x1fffffff) == 0 \
8003 ? 3 \
8004 : 2 \
8005 : ((v) & ~0x7fffffff) == 0 \
8006 ? 1 \
8007 : 0)
8008
8009/* load_register()
67c1ffbe 8010 * This routine generates the least number of instructions necessary to load
252b5132
RH
8011 * an absolute expression value into a register.
8012 */
8013static void
67c0d1eb 8014load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
8015{
8016 int freg;
8017 expressionS hi32, lo32;
8018
8019 if (ep->X_op != O_big)
8020 {
9c2799c2 8021 gas_assert (ep->X_op == O_constant);
256ab948
TS
8022
8023 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
8024 if (!dbl)
8025 normalize_constant_expr (ep);
256ab948
TS
8026
8027 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
8028 {
8029 /* We can handle 16 bit signed values with an addiu to
8030 $zero. No need to ever use daddiu here, since $zero and
8031 the result are always correct in 32 bit mode. */
67c0d1eb 8032 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
8033 return;
8034 }
8035 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
8036 {
8037 /* We can handle 16 bit unsigned values with an ori to
8038 $zero. */
67c0d1eb 8039 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
8040 return;
8041 }
256ab948 8042 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
8043 {
8044 /* 32 bit values require an lui. */
df58fc94 8045 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 8046 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 8047 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
8048 return;
8049 }
8050 }
8051
8052 /* The value is larger than 32 bits. */
8053
2051e8c4 8054 if (!dbl || HAVE_32BIT_GPRS)
252b5132 8055 {
55e08f71
NC
8056 char value[32];
8057
8058 sprintf_vma (value, ep->X_add_number);
20e1fcfd 8059 as_bad (_("Number (0x%s) larger than 32 bits"), value);
67c0d1eb 8060 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
8061 return;
8062 }
8063
8064 if (ep->X_op != O_big)
8065 {
8066 hi32 = *ep;
8067 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
8068 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
8069 hi32.X_add_number &= 0xffffffff;
8070 lo32 = *ep;
8071 lo32.X_add_number &= 0xffffffff;
8072 }
8073 else
8074 {
9c2799c2 8075 gas_assert (ep->X_add_number > 2);
252b5132
RH
8076 if (ep->X_add_number == 3)
8077 generic_bignum[3] = 0;
8078 else if (ep->X_add_number > 4)
8079 as_bad (_("Number larger than 64 bits"));
8080 lo32.X_op = O_constant;
8081 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
8082 hi32.X_op = O_constant;
8083 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
8084 }
8085
8086 if (hi32.X_add_number == 0)
8087 freg = 0;
8088 else
8089 {
8090 int shift, bit;
8091 unsigned long hi, lo;
8092
956cd1d6 8093 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
8094 {
8095 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
8096 {
67c0d1eb 8097 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
8098 return;
8099 }
8100 if (lo32.X_add_number & 0x80000000)
8101 {
df58fc94 8102 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 8103 if (lo32.X_add_number & 0xffff)
67c0d1eb 8104 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
8105 return;
8106 }
8107 }
252b5132
RH
8108
8109 /* Check for 16bit shifted constant. We know that hi32 is
8110 non-zero, so start the mask on the first bit of the hi32
8111 value. */
8112 shift = 17;
8113 do
beae10d5
KH
8114 {
8115 unsigned long himask, lomask;
8116
8117 if (shift < 32)
8118 {
8119 himask = 0xffff >> (32 - shift);
8120 lomask = (0xffff << shift) & 0xffffffff;
8121 }
8122 else
8123 {
8124 himask = 0xffff << (shift - 32);
8125 lomask = 0;
8126 }
8127 if ((hi32.X_add_number & ~(offsetT) himask) == 0
8128 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
8129 {
8130 expressionS tmp;
8131
8132 tmp.X_op = O_constant;
8133 if (shift < 32)
8134 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
8135 | (lo32.X_add_number >> shift));
8136 else
8137 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb 8138 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
df58fc94 8139 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 8140 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
8141 return;
8142 }
f9419b05 8143 ++shift;
beae10d5
KH
8144 }
8145 while (shift <= (64 - 16));
252b5132
RH
8146
8147 /* Find the bit number of the lowest one bit, and store the
8148 shifted value in hi/lo. */
8149 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
8150 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
8151 if (lo != 0)
8152 {
8153 bit = 0;
8154 while ((lo & 1) == 0)
8155 {
8156 lo >>= 1;
8157 ++bit;
8158 }
8159 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
8160 hi >>= bit;
8161 }
8162 else
8163 {
8164 bit = 32;
8165 while ((hi & 1) == 0)
8166 {
8167 hi >>= 1;
8168 ++bit;
8169 }
8170 lo = hi;
8171 hi = 0;
8172 }
8173
8174 /* Optimize if the shifted value is a (power of 2) - 1. */
8175 if ((hi == 0 && ((lo + 1) & lo) == 0)
8176 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
8177 {
8178 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 8179 if (shift != 0)
beae10d5 8180 {
252b5132
RH
8181 expressionS tmp;
8182
8183 /* This instruction will set the register to be all
8184 ones. */
beae10d5
KH
8185 tmp.X_op = O_constant;
8186 tmp.X_add_number = (offsetT) -1;
67c0d1eb 8187 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
8188 if (bit != 0)
8189 {
8190 bit += shift;
df58fc94 8191 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 8192 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 8193 }
df58fc94 8194 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
67c0d1eb 8195 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
8196 return;
8197 }
8198 }
252b5132
RH
8199
8200 /* Sign extend hi32 before calling load_register, because we can
8201 generally get better code when we load a sign extended value. */
8202 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 8203 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 8204 load_register (reg, &hi32, 0);
252b5132
RH
8205 freg = reg;
8206 }
8207 if ((lo32.X_add_number & 0xffff0000) == 0)
8208 {
8209 if (freg != 0)
8210 {
df58fc94 8211 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
252b5132
RH
8212 freg = reg;
8213 }
8214 }
8215 else
8216 {
8217 expressionS mid16;
8218
956cd1d6 8219 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 8220 {
df58fc94
RS
8221 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
8222 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
beae10d5
KH
8223 return;
8224 }
252b5132
RH
8225
8226 if (freg != 0)
8227 {
df58fc94 8228 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
252b5132
RH
8229 freg = reg;
8230 }
8231 mid16 = lo32;
8232 mid16.X_add_number >>= 16;
67c0d1eb 8233 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
df58fc94 8234 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
252b5132
RH
8235 freg = reg;
8236 }
8237 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 8238 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
8239}
8240
269137b2
TS
8241static inline void
8242load_delay_nop (void)
8243{
8244 if (!gpr_interlocks)
8245 macro_build (NULL, "nop", "");
8246}
8247
252b5132
RH
8248/* Load an address into a register. */
8249
8250static void
67c0d1eb 8251load_address (int reg, expressionS *ep, int *used_at)
252b5132 8252{
252b5132
RH
8253 if (ep->X_op != O_constant
8254 && ep->X_op != O_symbol)
8255 {
8256 as_bad (_("expression too complex"));
8257 ep->X_op = O_constant;
8258 }
8259
8260 if (ep->X_op == O_constant)
8261 {
67c0d1eb 8262 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
8263 return;
8264 }
8265
8266 if (mips_pic == NO_PIC)
8267 {
8268 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 8269 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
8270 Otherwise we want
8271 lui $reg,<sym> (BFD_RELOC_HI16_S)
8272 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 8273 If we have an addend, we always use the latter form.
76b3015f 8274
d6bc6245
TS
8275 With 64bit address space and a usable $at we want
8276 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8277 lui $at,<sym> (BFD_RELOC_HI16_S)
8278 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
8279 daddiu $at,<sym> (BFD_RELOC_LO16)
8280 dsll32 $reg,0
3a482fd5 8281 daddu $reg,$reg,$at
76b3015f 8282
c03099e6 8283 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
8284 on superscalar processors.
8285 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8286 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
8287 dsll $reg,16
8288 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
8289 dsll $reg,16
8290 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
8291
8292 For GP relative symbols in 64bit address space we can use
8293 the same sequence as in 32bit address space. */
aed1a261 8294 if (HAVE_64BIT_SYMBOLS)
d6bc6245 8295 {
6caf9ef4
TS
8296 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
8297 && !nopic_need_relax (ep->X_add_symbol, 1))
8298 {
8299 relax_start (ep->X_add_symbol);
8300 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
8301 mips_gp_register, BFD_RELOC_GPREL16);
8302 relax_switch ();
8303 }
d6bc6245 8304
741fe287 8305 if (*used_at == 0 && mips_opts.at)
d6bc6245 8306 {
df58fc94
RS
8307 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
8308 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
67c0d1eb
RS
8309 macro_build (ep, "daddiu", "t,r,j", reg, reg,
8310 BFD_RELOC_MIPS_HIGHER);
8311 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
df58fc94 8312 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
67c0d1eb 8313 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
8314 *used_at = 1;
8315 }
8316 else
8317 {
df58fc94 8318 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb
RS
8319 macro_build (ep, "daddiu", "t,r,j", reg, reg,
8320 BFD_RELOC_MIPS_HIGHER);
df58fc94 8321 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 8322 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
df58fc94 8323 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 8324 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 8325 }
6caf9ef4
TS
8326
8327 if (mips_relax.sequence)
8328 relax_end ();
d6bc6245 8329 }
252b5132
RH
8330 else
8331 {
d6bc6245 8332 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 8333 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 8334 {
4d7206a2 8335 relax_start (ep->X_add_symbol);
67c0d1eb 8336 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 8337 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 8338 relax_switch ();
d6bc6245 8339 }
67c0d1eb
RS
8340 macro_build_lui (ep, reg);
8341 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
8342 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
8343 if (mips_relax.sequence)
8344 relax_end ();
d6bc6245 8345 }
252b5132 8346 }
0a44bf69 8347 else if (!mips_big_got)
252b5132
RH
8348 {
8349 expressionS ex;
8350
8351 /* If this is a reference to an external symbol, we want
8352 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8353 Otherwise we want
8354 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8355 nop
8356 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
8357 If there is a constant, it must be added in after.
8358
ed6fb7bd 8359 If we have NewABI, we want
f5040a92
AO
8360 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
8361 unless we're referencing a global symbol with a non-zero
8362 offset, in which case cst must be added separately. */
ed6fb7bd
SC
8363 if (HAVE_NEWABI)
8364 {
f5040a92
AO
8365 if (ep->X_add_number)
8366 {
4d7206a2 8367 ex.X_add_number = ep->X_add_number;
f5040a92 8368 ep->X_add_number = 0;
4d7206a2 8369 relax_start (ep->X_add_symbol);
67c0d1eb
RS
8370 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
8371 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
8372 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
8373 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
8374 ex.X_op = O_constant;
67c0d1eb 8375 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 8376 reg, reg, BFD_RELOC_LO16);
f5040a92 8377 ep->X_add_number = ex.X_add_number;
4d7206a2 8378 relax_switch ();
f5040a92 8379 }
67c0d1eb 8380 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 8381 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
8382 if (mips_relax.sequence)
8383 relax_end ();
ed6fb7bd
SC
8384 }
8385 else
8386 {
f5040a92
AO
8387 ex.X_add_number = ep->X_add_number;
8388 ep->X_add_number = 0;
67c0d1eb
RS
8389 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
8390 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 8391 load_delay_nop ();
4d7206a2
RS
8392 relax_start (ep->X_add_symbol);
8393 relax_switch ();
67c0d1eb 8394 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 8395 BFD_RELOC_LO16);
4d7206a2 8396 relax_end ();
ed6fb7bd 8397
f5040a92
AO
8398 if (ex.X_add_number != 0)
8399 {
8400 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
8401 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
8402 ex.X_op = O_constant;
67c0d1eb 8403 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 8404 reg, reg, BFD_RELOC_LO16);
f5040a92 8405 }
252b5132
RH
8406 }
8407 }
0a44bf69 8408 else if (mips_big_got)
252b5132
RH
8409 {
8410 expressionS ex;
252b5132
RH
8411
8412 /* This is the large GOT case. If this is a reference to an
8413 external symbol, we want
8414 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
8415 addu $reg,$reg,$gp
8416 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
8417
8418 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
8419 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8420 nop
8421 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 8422 If there is a constant, it must be added in after.
f5040a92
AO
8423
8424 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
8425 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
8426 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 8427 */
438c16b8
TS
8428 if (HAVE_NEWABI)
8429 {
4d7206a2 8430 ex.X_add_number = ep->X_add_number;
f5040a92 8431 ep->X_add_number = 0;
4d7206a2 8432 relax_start (ep->X_add_symbol);
df58fc94 8433 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
8434 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8435 reg, reg, mips_gp_register);
8436 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
8437 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
8438 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
8439 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
8440 else if (ex.X_add_number)
8441 {
8442 ex.X_op = O_constant;
67c0d1eb
RS
8443 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
8444 BFD_RELOC_LO16);
f5040a92
AO
8445 }
8446
8447 ep->X_add_number = ex.X_add_number;
4d7206a2 8448 relax_switch ();
67c0d1eb 8449 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 8450 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
8451 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
8452 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 8453 relax_end ();
438c16b8 8454 }
252b5132 8455 else
438c16b8 8456 {
f5040a92
AO
8457 ex.X_add_number = ep->X_add_number;
8458 ep->X_add_number = 0;
4d7206a2 8459 relax_start (ep->X_add_symbol);
df58fc94 8460 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
8461 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8462 reg, reg, mips_gp_register);
8463 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
8464 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
8465 relax_switch ();
8466 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
8467 {
8468 /* We need a nop before loading from $gp. This special
8469 check is required because the lui which starts the main
8470 instruction stream does not refer to $gp, and so will not
8471 insert the nop which may be required. */
67c0d1eb 8472 macro_build (NULL, "nop", "");
438c16b8 8473 }
67c0d1eb 8474 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 8475 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 8476 load_delay_nop ();
67c0d1eb 8477 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 8478 BFD_RELOC_LO16);
4d7206a2 8479 relax_end ();
438c16b8 8480
f5040a92
AO
8481 if (ex.X_add_number != 0)
8482 {
8483 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
8484 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
8485 ex.X_op = O_constant;
67c0d1eb
RS
8486 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
8487 BFD_RELOC_LO16);
f5040a92 8488 }
252b5132
RH
8489 }
8490 }
252b5132
RH
8491 else
8492 abort ();
8fc2e39e 8493
741fe287 8494 if (!mips_opts.at && *used_at == 1)
8fc2e39e 8495 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
8496}
8497
ea1fb5dc
RS
8498/* Move the contents of register SOURCE into register DEST. */
8499
8500static void
67c0d1eb 8501move_register (int dest, int source)
ea1fb5dc 8502{
df58fc94
RS
8503 /* Prefer to use a 16-bit microMIPS instruction unless the previous
8504 instruction specifically requires a 32-bit one. */
8505 if (mips_opts.micromips
833794fc 8506 && !mips_opts.insn32
df58fc94 8507 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7951ca42 8508 macro_build (NULL, "move", "mp,mj", dest, source);
df58fc94
RS
8509 else
8510 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
8511 dest, source, 0);
ea1fb5dc
RS
8512}
8513
4d7206a2 8514/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
8515 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
8516 The two alternatives are:
4d7206a2
RS
8517
8518 Global symbol Local sybmol
8519 ------------- ------------
8520 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
8521 ... ...
8522 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
8523
8524 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
8525 emits the second for a 16-bit offset or add_got_offset_hilo emits
8526 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
8527
8528static void
67c0d1eb 8529load_got_offset (int dest, expressionS *local)
4d7206a2
RS
8530{
8531 expressionS global;
8532
8533 global = *local;
8534 global.X_add_number = 0;
8535
8536 relax_start (local->X_add_symbol);
67c0d1eb
RS
8537 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
8538 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 8539 relax_switch ();
67c0d1eb
RS
8540 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
8541 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
8542 relax_end ();
8543}
8544
8545static void
67c0d1eb 8546add_got_offset (int dest, expressionS *local)
4d7206a2
RS
8547{
8548 expressionS global;
8549
8550 global.X_op = O_constant;
8551 global.X_op_symbol = NULL;
8552 global.X_add_symbol = NULL;
8553 global.X_add_number = local->X_add_number;
8554
8555 relax_start (local->X_add_symbol);
67c0d1eb 8556 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
8557 dest, dest, BFD_RELOC_LO16);
8558 relax_switch ();
67c0d1eb 8559 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
8560 relax_end ();
8561}
8562
f6a22291
MR
8563static void
8564add_got_offset_hilo (int dest, expressionS *local, int tmp)
8565{
8566 expressionS global;
8567 int hold_mips_optimize;
8568
8569 global.X_op = O_constant;
8570 global.X_op_symbol = NULL;
8571 global.X_add_symbol = NULL;
8572 global.X_add_number = local->X_add_number;
8573
8574 relax_start (local->X_add_symbol);
8575 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
8576 relax_switch ();
8577 /* Set mips_optimize around the lui instruction to avoid
8578 inserting an unnecessary nop after the lw. */
8579 hold_mips_optimize = mips_optimize;
8580 mips_optimize = 2;
8581 macro_build_lui (&global, tmp);
8582 mips_optimize = hold_mips_optimize;
8583 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
8584 relax_end ();
8585
8586 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
8587}
8588
df58fc94
RS
8589/* Emit a sequence of instructions to emulate a branch likely operation.
8590 BR is an ordinary branch corresponding to one to be emulated. BRNEG
8591 is its complementing branch with the original condition negated.
8592 CALL is set if the original branch specified the link operation.
8593 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
8594
8595 Code like this is produced in the noreorder mode:
8596
8597 BRNEG <args>, 1f
8598 nop
8599 b <sym>
8600 delay slot (executed only if branch taken)
8601 1:
8602
8603 or, if CALL is set:
8604
8605 BRNEG <args>, 1f
8606 nop
8607 bal <sym>
8608 delay slot (executed only if branch taken)
8609 1:
8610
8611 In the reorder mode the delay slot would be filled with a nop anyway,
8612 so code produced is simply:
8613
8614 BR <args>, <sym>
8615 nop
8616
8617 This function is used when producing code for the microMIPS ASE that
8618 does not implement branch likely instructions in hardware. */
8619
8620static void
8621macro_build_branch_likely (const char *br, const char *brneg,
8622 int call, expressionS *ep, const char *fmt,
8623 unsigned int sreg, unsigned int treg)
8624{
8625 int noreorder = mips_opts.noreorder;
8626 expressionS expr1;
8627
8628 gas_assert (mips_opts.micromips);
8629 start_noreorder ();
8630 if (noreorder)
8631 {
8632 micromips_label_expr (&expr1);
8633 macro_build (&expr1, brneg, fmt, sreg, treg);
8634 macro_build (NULL, "nop", "");
8635 macro_build (ep, call ? "bal" : "b", "p");
8636
8637 /* Set to true so that append_insn adds a label. */
8638 emit_branch_likely_macro = TRUE;
8639 }
8640 else
8641 {
8642 macro_build (ep, br, fmt, sreg, treg);
8643 macro_build (NULL, "nop", "");
8644 }
8645 end_noreorder ();
8646}
8647
8648/* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
8649 the condition code tested. EP specifies the branch target. */
8650
8651static void
8652macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
8653{
8654 const int call = 0;
8655 const char *brneg;
8656 const char *br;
8657
8658 switch (type)
8659 {
8660 case M_BC1FL:
8661 br = "bc1f";
8662 brneg = "bc1t";
8663 break;
8664 case M_BC1TL:
8665 br = "bc1t";
8666 brneg = "bc1f";
8667 break;
8668 case M_BC2FL:
8669 br = "bc2f";
8670 brneg = "bc2t";
8671 break;
8672 case M_BC2TL:
8673 br = "bc2t";
8674 brneg = "bc2f";
8675 break;
8676 default:
8677 abort ();
8678 }
8679 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
8680}
8681
8682/* Emit a two-argument branch macro specified by TYPE, using SREG as
8683 the register tested. EP specifies the branch target. */
8684
8685static void
8686macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
8687{
8688 const char *brneg = NULL;
8689 const char *br;
8690 int call = 0;
8691
8692 switch (type)
8693 {
8694 case M_BGEZ:
8695 br = "bgez";
8696 break;
8697 case M_BGEZL:
8698 br = mips_opts.micromips ? "bgez" : "bgezl";
8699 brneg = "bltz";
8700 break;
8701 case M_BGEZALL:
8702 gas_assert (mips_opts.micromips);
833794fc 8703 br = mips_opts.insn32 ? "bgezal" : "bgezals";
df58fc94
RS
8704 brneg = "bltz";
8705 call = 1;
8706 break;
8707 case M_BGTZ:
8708 br = "bgtz";
8709 break;
8710 case M_BGTZL:
8711 br = mips_opts.micromips ? "bgtz" : "bgtzl";
8712 brneg = "blez";
8713 break;
8714 case M_BLEZ:
8715 br = "blez";
8716 break;
8717 case M_BLEZL:
8718 br = mips_opts.micromips ? "blez" : "blezl";
8719 brneg = "bgtz";
8720 break;
8721 case M_BLTZ:
8722 br = "bltz";
8723 break;
8724 case M_BLTZL:
8725 br = mips_opts.micromips ? "bltz" : "bltzl";
8726 brneg = "bgez";
8727 break;
8728 case M_BLTZALL:
8729 gas_assert (mips_opts.micromips);
833794fc 8730 br = mips_opts.insn32 ? "bltzal" : "bltzals";
df58fc94
RS
8731 brneg = "bgez";
8732 call = 1;
8733 break;
8734 default:
8735 abort ();
8736 }
8737 if (mips_opts.micromips && brneg)
8738 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
8739 else
8740 macro_build (ep, br, "s,p", sreg);
8741}
8742
8743/* Emit a three-argument branch macro specified by TYPE, using SREG and
8744 TREG as the registers tested. EP specifies the branch target. */
8745
8746static void
8747macro_build_branch_rsrt (int type, expressionS *ep,
8748 unsigned int sreg, unsigned int treg)
8749{
8750 const char *brneg = NULL;
8751 const int call = 0;
8752 const char *br;
8753
8754 switch (type)
8755 {
8756 case M_BEQ:
8757 case M_BEQ_I:
8758 br = "beq";
8759 break;
8760 case M_BEQL:
8761 case M_BEQL_I:
8762 br = mips_opts.micromips ? "beq" : "beql";
8763 brneg = "bne";
8764 break;
8765 case M_BNE:
8766 case M_BNE_I:
8767 br = "bne";
8768 break;
8769 case M_BNEL:
8770 case M_BNEL_I:
8771 br = mips_opts.micromips ? "bne" : "bnel";
8772 brneg = "beq";
8773 break;
8774 default:
8775 abort ();
8776 }
8777 if (mips_opts.micromips && brneg)
8778 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
8779 else
8780 macro_build (ep, br, "s,t,p", sreg, treg);
8781}
8782
f2ae14a1
RS
8783/* Return the high part that should be loaded in order to make the low
8784 part of VALUE accessible using an offset of OFFBITS bits. */
8785
8786static offsetT
8787offset_high_part (offsetT value, unsigned int offbits)
8788{
8789 offsetT bias;
8790 addressT low_mask;
8791
8792 if (offbits == 0)
8793 return value;
8794 bias = 1 << (offbits - 1);
8795 low_mask = bias * 2 - 1;
8796 return (value + bias) & ~low_mask;
8797}
8798
8799/* Return true if the value stored in offset_expr and offset_reloc
8800 fits into a signed offset of OFFBITS bits. RANGE is the maximum
8801 amount that the caller wants to add without inducing overflow
8802 and ALIGN is the known alignment of the value in bytes. */
8803
8804static bfd_boolean
8805small_offset_p (unsigned int range, unsigned int align, unsigned int offbits)
8806{
8807 if (offbits == 16)
8808 {
8809 /* Accept any relocation operator if overflow isn't a concern. */
8810 if (range < align && *offset_reloc != BFD_RELOC_UNUSED)
8811 return TRUE;
8812
8813 /* These relocations are guaranteed not to overflow in correct links. */
8814 if (*offset_reloc == BFD_RELOC_MIPS_LITERAL
8815 || gprel16_reloc_p (*offset_reloc))
8816 return TRUE;
8817 }
8818 if (offset_expr.X_op == O_constant
8819 && offset_high_part (offset_expr.X_add_number, offbits) == 0
8820 && offset_high_part (offset_expr.X_add_number + range, offbits) == 0)
8821 return TRUE;
8822 return FALSE;
8823}
8824
252b5132
RH
8825/*
8826 * Build macros
8827 * This routine implements the seemingly endless macro or synthesized
8828 * instructions and addressing modes in the mips assembly language. Many
8829 * of these macros are simple and are similar to each other. These could
67c1ffbe 8830 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
8831 * this verbose method. Others are not simple macros but are more like
8832 * optimizing code generation.
8833 * One interesting optimization is when several store macros appear
67c1ffbe 8834 * consecutively that would load AT with the upper half of the same address.
252b5132
RH
8835 * The ensuing load upper instructions are ommited. This implies some kind
8836 * of global optimization. We currently only optimize within a single macro.
8837 * For many of the load and store macros if the address is specified as a
8838 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
8839 * first load register 'at' with zero and use it as the base register. The
8840 * mips assembler simply uses register $zero. Just one tiny optimization
8841 * we're missing.
8842 */
8843static void
833794fc 8844macro (struct mips_cl_insn *ip, char *str)
252b5132 8845{
c0ebe874
RS
8846 const struct mips_operand_array *operands;
8847 unsigned int breg, i;
741fe287 8848 unsigned int tempreg;
252b5132 8849 int mask;
43841e91 8850 int used_at = 0;
df58fc94 8851 expressionS label_expr;
252b5132 8852 expressionS expr1;
df58fc94 8853 expressionS *ep;
252b5132
RH
8854 const char *s;
8855 const char *s2;
8856 const char *fmt;
8857 int likely = 0;
252b5132 8858 int coproc = 0;
7f3c4072 8859 int offbits = 16;
1abe91b1 8860 int call = 0;
df58fc94
RS
8861 int jals = 0;
8862 int dbl = 0;
8863 int imm = 0;
8864 int ust = 0;
8865 int lp = 0;
f2ae14a1 8866 bfd_boolean large_offset;
252b5132 8867 int off;
252b5132 8868 int hold_mips_optimize;
f2ae14a1 8869 unsigned int align;
c0ebe874 8870 unsigned int op[MAX_OPERANDS];
252b5132 8871
9c2799c2 8872 gas_assert (! mips_opts.mips16);
252b5132 8873
c0ebe874
RS
8874 operands = insn_operands (ip);
8875 for (i = 0; i < MAX_OPERANDS; i++)
8876 if (operands->operand[i])
8877 op[i] = insn_extract_operand (ip, operands->operand[i]);
8878 else
8879 op[i] = -1;
8880
252b5132
RH
8881 mask = ip->insn_mo->mask;
8882
df58fc94
RS
8883 label_expr.X_op = O_constant;
8884 label_expr.X_op_symbol = NULL;
8885 label_expr.X_add_symbol = NULL;
8886 label_expr.X_add_number = 0;
8887
252b5132
RH
8888 expr1.X_op = O_constant;
8889 expr1.X_op_symbol = NULL;
8890 expr1.X_add_symbol = NULL;
8891 expr1.X_add_number = 1;
f2ae14a1 8892 align = 1;
252b5132
RH
8893
8894 switch (mask)
8895 {
8896 case M_DABS:
8897 dbl = 1;
8898 case M_ABS:
df58fc94
RS
8899 /* bgez $a0,1f
8900 move v0,$a0
8901 sub v0,$zero,$a0
8902 1:
8903 */
252b5132 8904
7d10b47d 8905 start_noreorder ();
252b5132 8906
df58fc94
RS
8907 if (mips_opts.micromips)
8908 micromips_label_expr (&label_expr);
8909 else
8910 label_expr.X_add_number = 8;
c0ebe874
RS
8911 macro_build (&label_expr, "bgez", "s,p", op[1]);
8912 if (op[0] == op[1])
a605d2b3 8913 macro_build (NULL, "nop", "");
252b5132 8914 else
c0ebe874
RS
8915 move_register (op[0], op[1]);
8916 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", op[0], 0, op[1]);
df58fc94
RS
8917 if (mips_opts.micromips)
8918 micromips_add_label ();
252b5132 8919
7d10b47d 8920 end_noreorder ();
8fc2e39e 8921 break;
252b5132
RH
8922
8923 case M_ADD_I:
8924 s = "addi";
8925 s2 = "add";
8926 goto do_addi;
8927 case M_ADDU_I:
8928 s = "addiu";
8929 s2 = "addu";
8930 goto do_addi;
8931 case M_DADD_I:
8932 dbl = 1;
8933 s = "daddi";
8934 s2 = "dadd";
df58fc94
RS
8935 if (!mips_opts.micromips)
8936 goto do_addi;
8937 if (imm_expr.X_op == O_constant
8938 && imm_expr.X_add_number >= -0x200
8939 && imm_expr.X_add_number < 0x200)
8940 {
c0ebe874 8941 macro_build (NULL, s, "t,r,.", op[0], op[1], imm_expr.X_add_number);
df58fc94
RS
8942 break;
8943 }
8944 goto do_addi_i;
252b5132
RH
8945 case M_DADDU_I:
8946 dbl = 1;
8947 s = "daddiu";
8948 s2 = "daddu";
8949 do_addi:
8950 if (imm_expr.X_op == O_constant
8951 && imm_expr.X_add_number >= -0x8000
8952 && imm_expr.X_add_number < 0x8000)
8953 {
c0ebe874 8954 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 8955 break;
252b5132 8956 }
df58fc94 8957 do_addi_i:
8fc2e39e 8958 used_at = 1;
67c0d1eb 8959 load_register (AT, &imm_expr, dbl);
c0ebe874 8960 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
8961 break;
8962
8963 case M_AND_I:
8964 s = "andi";
8965 s2 = "and";
8966 goto do_bit;
8967 case M_OR_I:
8968 s = "ori";
8969 s2 = "or";
8970 goto do_bit;
8971 case M_NOR_I:
8972 s = "";
8973 s2 = "nor";
8974 goto do_bit;
8975 case M_XOR_I:
8976 s = "xori";
8977 s2 = "xor";
8978 do_bit:
8979 if (imm_expr.X_op == O_constant
8980 && imm_expr.X_add_number >= 0
8981 && imm_expr.X_add_number < 0x10000)
8982 {
8983 if (mask != M_NOR_I)
c0ebe874 8984 macro_build (&imm_expr, s, "t,r,i", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
8985 else
8986 {
67c0d1eb 8987 macro_build (&imm_expr, "ori", "t,r,i",
c0ebe874
RS
8988 op[0], op[1], BFD_RELOC_LO16);
8989 macro_build (NULL, "nor", "d,v,t", op[0], op[0], 0);
252b5132 8990 }
8fc2e39e 8991 break;
252b5132
RH
8992 }
8993
8fc2e39e 8994 used_at = 1;
67c0d1eb 8995 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 8996 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
8997 break;
8998
8b082fb1
TS
8999 case M_BALIGN:
9000 switch (imm_expr.X_add_number)
9001 {
9002 case 0:
9003 macro_build (NULL, "nop", "");
9004 break;
9005 case 2:
c0ebe874 9006 macro_build (NULL, "packrl.ph", "d,s,t", op[0], op[0], op[1]);
8b082fb1 9007 break;
03f66e8a
MR
9008 case 1:
9009 case 3:
c0ebe874 9010 macro_build (NULL, "balign", "t,s,2", op[0], op[1],
90ecf173 9011 (int) imm_expr.X_add_number);
8b082fb1 9012 break;
03f66e8a
MR
9013 default:
9014 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
9015 (unsigned long) imm_expr.X_add_number);
9016 break;
8b082fb1
TS
9017 }
9018 break;
9019
df58fc94
RS
9020 case M_BC1FL:
9021 case M_BC1TL:
9022 case M_BC2FL:
9023 case M_BC2TL:
9024 gas_assert (mips_opts.micromips);
9025 macro_build_branch_ccl (mask, &offset_expr,
9026 EXTRACT_OPERAND (1, BCC, *ip));
9027 break;
9028
252b5132 9029 case M_BEQ_I:
252b5132 9030 case M_BEQL_I:
252b5132 9031 case M_BNE_I:
252b5132 9032 case M_BNEL_I:
252b5132 9033 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
c0ebe874 9034 op[1] = 0;
df58fc94 9035 else
252b5132 9036 {
c0ebe874 9037 op[1] = AT;
df58fc94 9038 used_at = 1;
c0ebe874 9039 load_register (op[1], &imm_expr, HAVE_64BIT_GPRS);
252b5132 9040 }
df58fc94
RS
9041 /* Fall through. */
9042 case M_BEQL:
9043 case M_BNEL:
c0ebe874 9044 macro_build_branch_rsrt (mask, &offset_expr, op[0], op[1]);
252b5132
RH
9045 break;
9046
9047 case M_BGEL:
9048 likely = 1;
9049 case M_BGE:
c0ebe874
RS
9050 if (op[1] == 0)
9051 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[0]);
9052 else if (op[0] == 0)
9053 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[1]);
df58fc94 9054 else
252b5132 9055 {
df58fc94 9056 used_at = 1;
c0ebe874 9057 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
9058 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
9059 &offset_expr, AT, ZERO);
252b5132 9060 }
df58fc94
RS
9061 break;
9062
9063 case M_BGEZL:
9064 case M_BGEZALL:
9065 case M_BGTZL:
9066 case M_BLEZL:
9067 case M_BLTZL:
9068 case M_BLTZALL:
c0ebe874 9069 macro_build_branch_rs (mask, &offset_expr, op[0]);
252b5132
RH
9070 break;
9071
9072 case M_BGTL_I:
9073 likely = 1;
9074 case M_BGT_I:
90ecf173 9075 /* Check for > max integer. */
42429eac 9076 if (imm_expr.X_op == O_constant && imm_expr.X_add_number >= GPR_SMAX)
252b5132
RH
9077 {
9078 do_false:
90ecf173 9079 /* Result is always false. */
252b5132 9080 if (! likely)
a605d2b3 9081 macro_build (NULL, "nop", "");
252b5132 9082 else
df58fc94 9083 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
8fc2e39e 9084 break;
252b5132
RH
9085 }
9086 if (imm_expr.X_op != O_constant)
9087 as_bad (_("Unsupported large constant"));
f9419b05 9088 ++imm_expr.X_add_number;
252b5132
RH
9089 /* FALLTHROUGH */
9090 case M_BGE_I:
9091 case M_BGEL_I:
9092 if (mask == M_BGEL_I)
9093 likely = 1;
9094 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
9095 {
df58fc94 9096 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
c0ebe874 9097 &offset_expr, op[0]);
8fc2e39e 9098 break;
252b5132
RH
9099 }
9100 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
9101 {
df58fc94 9102 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
c0ebe874 9103 &offset_expr, op[0]);
8fc2e39e 9104 break;
252b5132 9105 }
42429eac 9106 if (imm_expr.X_op == O_constant && imm_expr.X_add_number <= GPR_SMIN)
252b5132
RH
9107 {
9108 do_true:
9109 /* result is always true */
9110 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
67c0d1eb 9111 macro_build (&offset_expr, "b", "p");
8fc2e39e 9112 break;
252b5132 9113 }
8fc2e39e 9114 used_at = 1;
c0ebe874 9115 set_at (op[0], 0);
df58fc94
RS
9116 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
9117 &offset_expr, AT, ZERO);
252b5132
RH
9118 break;
9119
9120 case M_BGEUL:
9121 likely = 1;
9122 case M_BGEU:
c0ebe874 9123 if (op[1] == 0)
252b5132 9124 goto do_true;
c0ebe874 9125 else if (op[0] == 0)
df58fc94 9126 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 9127 &offset_expr, ZERO, op[1]);
df58fc94 9128 else
252b5132 9129 {
df58fc94 9130 used_at = 1;
c0ebe874 9131 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
9132 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
9133 &offset_expr, AT, ZERO);
252b5132 9134 }
252b5132
RH
9135 break;
9136
9137 case M_BGTUL_I:
9138 likely = 1;
9139 case M_BGTU_I:
c0ebe874 9140 if (op[0] == 0
ca4e0257 9141 || (HAVE_32BIT_GPRS
252b5132 9142 && imm_expr.X_op == O_constant
f01dc953 9143 && imm_expr.X_add_number == -1))
252b5132
RH
9144 goto do_false;
9145 if (imm_expr.X_op != O_constant)
9146 as_bad (_("Unsupported large constant"));
f9419b05 9147 ++imm_expr.X_add_number;
252b5132
RH
9148 /* FALLTHROUGH */
9149 case M_BGEU_I:
9150 case M_BGEUL_I:
9151 if (mask == M_BGEUL_I)
9152 likely = 1;
9153 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
9154 goto do_true;
df58fc94
RS
9155 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
9156 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 9157 &offset_expr, op[0], ZERO);
df58fc94 9158 else
252b5132 9159 {
df58fc94 9160 used_at = 1;
c0ebe874 9161 set_at (op[0], 1);
df58fc94
RS
9162 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
9163 &offset_expr, AT, ZERO);
252b5132 9164 }
252b5132
RH
9165 break;
9166
9167 case M_BGTL:
9168 likely = 1;
9169 case M_BGT:
c0ebe874
RS
9170 if (op[1] == 0)
9171 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[0]);
9172 else if (op[0] == 0)
9173 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[1]);
df58fc94 9174 else
252b5132 9175 {
df58fc94 9176 used_at = 1;
c0ebe874 9177 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
9178 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
9179 &offset_expr, AT, ZERO);
252b5132 9180 }
252b5132
RH
9181 break;
9182
9183 case M_BGTUL:
9184 likely = 1;
9185 case M_BGTU:
c0ebe874 9186 if (op[1] == 0)
df58fc94 9187 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874
RS
9188 &offset_expr, op[0], ZERO);
9189 else if (op[0] == 0)
df58fc94
RS
9190 goto do_false;
9191 else
252b5132 9192 {
df58fc94 9193 used_at = 1;
c0ebe874 9194 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
9195 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
9196 &offset_expr, AT, ZERO);
252b5132 9197 }
252b5132
RH
9198 break;
9199
9200 case M_BLEL:
9201 likely = 1;
9202 case M_BLE:
c0ebe874
RS
9203 if (op[1] == 0)
9204 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
9205 else if (op[0] == 0)
9206 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[1]);
df58fc94 9207 else
252b5132 9208 {
df58fc94 9209 used_at = 1;
c0ebe874 9210 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
9211 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
9212 &offset_expr, AT, ZERO);
252b5132 9213 }
252b5132
RH
9214 break;
9215
9216 case M_BLEL_I:
9217 likely = 1;
9218 case M_BLE_I:
42429eac 9219 if (imm_expr.X_op == O_constant && imm_expr.X_add_number >= GPR_SMAX)
252b5132
RH
9220 goto do_true;
9221 if (imm_expr.X_op != O_constant)
9222 as_bad (_("Unsupported large constant"));
f9419b05 9223 ++imm_expr.X_add_number;
252b5132
RH
9224 /* FALLTHROUGH */
9225 case M_BLT_I:
9226 case M_BLTL_I:
9227 if (mask == M_BLTL_I)
9228 likely = 1;
9229 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
c0ebe874 9230 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
df58fc94 9231 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
c0ebe874 9232 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
df58fc94 9233 else
252b5132 9234 {
df58fc94 9235 used_at = 1;
c0ebe874 9236 set_at (op[0], 0);
df58fc94
RS
9237 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
9238 &offset_expr, AT, ZERO);
252b5132 9239 }
252b5132
RH
9240 break;
9241
9242 case M_BLEUL:
9243 likely = 1;
9244 case M_BLEU:
c0ebe874 9245 if (op[1] == 0)
df58fc94 9246 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874
RS
9247 &offset_expr, op[0], ZERO);
9248 else if (op[0] == 0)
df58fc94
RS
9249 goto do_true;
9250 else
252b5132 9251 {
df58fc94 9252 used_at = 1;
c0ebe874 9253 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
9254 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
9255 &offset_expr, AT, ZERO);
252b5132 9256 }
252b5132
RH
9257 break;
9258
9259 case M_BLEUL_I:
9260 likely = 1;
9261 case M_BLEU_I:
c0ebe874 9262 if (op[0] == 0
ca4e0257 9263 || (HAVE_32BIT_GPRS
252b5132 9264 && imm_expr.X_op == O_constant
f01dc953 9265 && imm_expr.X_add_number == -1))
252b5132
RH
9266 goto do_true;
9267 if (imm_expr.X_op != O_constant)
9268 as_bad (_("Unsupported large constant"));
f9419b05 9269 ++imm_expr.X_add_number;
252b5132
RH
9270 /* FALLTHROUGH */
9271 case M_BLTU_I:
9272 case M_BLTUL_I:
9273 if (mask == M_BLTUL_I)
9274 likely = 1;
9275 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
9276 goto do_false;
df58fc94
RS
9277 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
9278 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 9279 &offset_expr, op[0], ZERO);
df58fc94 9280 else
252b5132 9281 {
df58fc94 9282 used_at = 1;
c0ebe874 9283 set_at (op[0], 1);
df58fc94
RS
9284 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
9285 &offset_expr, AT, ZERO);
252b5132 9286 }
252b5132
RH
9287 break;
9288
9289 case M_BLTL:
9290 likely = 1;
9291 case M_BLT:
c0ebe874
RS
9292 if (op[1] == 0)
9293 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
9294 else if (op[0] == 0)
9295 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[1]);
df58fc94 9296 else
252b5132 9297 {
df58fc94 9298 used_at = 1;
c0ebe874 9299 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
9300 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
9301 &offset_expr, AT, ZERO);
252b5132 9302 }
252b5132
RH
9303 break;
9304
9305 case M_BLTUL:
9306 likely = 1;
9307 case M_BLTU:
c0ebe874 9308 if (op[1] == 0)
252b5132 9309 goto do_false;
c0ebe874 9310 else if (op[0] == 0)
df58fc94 9311 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 9312 &offset_expr, ZERO, op[1]);
df58fc94 9313 else
252b5132 9314 {
df58fc94 9315 used_at = 1;
c0ebe874 9316 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
9317 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
9318 &offset_expr, AT, ZERO);
252b5132 9319 }
252b5132
RH
9320 break;
9321
5f74bc13
CD
9322 case M_DEXT:
9323 {
d5818fca
MR
9324 /* Use unsigned arithmetic. */
9325 addressT pos;
9326 addressT size;
5f74bc13 9327
90ecf173 9328 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
5f74bc13
CD
9329 {
9330 as_bad (_("Unsupported large constant"));
9331 pos = size = 1;
9332 }
9333 else
9334 {
d5818fca
MR
9335 pos = imm_expr.X_add_number;
9336 size = imm2_expr.X_add_number;
5f74bc13
CD
9337 }
9338
9339 if (pos > 63)
9340 {
9e12b7a2 9341 report_bad_range (ip, 3, pos, 0, 63, FALSE);
5f74bc13
CD
9342 pos = 1;
9343 }
90ecf173 9344 if (size == 0 || size > 64 || (pos + size - 1) > 63)
5f74bc13 9345 {
9e12b7a2 9346 report_bad_field (pos, size);
5f74bc13
CD
9347 size = 1;
9348 }
9349
9350 if (size <= 32 && pos < 32)
9351 {
9352 s = "dext";
9353 fmt = "t,r,+A,+C";
9354 }
9355 else if (size <= 32)
9356 {
9357 s = "dextu";
9358 fmt = "t,r,+E,+H";
9359 }
9360 else
9361 {
9362 s = "dextm";
9363 fmt = "t,r,+A,+G";
9364 }
c0ebe874 9365 macro_build ((expressionS *) NULL, s, fmt, op[0], op[1], (int) pos,
d5818fca 9366 (int) (size - 1));
5f74bc13 9367 }
8fc2e39e 9368 break;
5f74bc13
CD
9369
9370 case M_DINS:
9371 {
d5818fca
MR
9372 /* Use unsigned arithmetic. */
9373 addressT pos;
9374 addressT size;
5f74bc13 9375
90ecf173 9376 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
5f74bc13
CD
9377 {
9378 as_bad (_("Unsupported large constant"));
9379 pos = size = 1;
9380 }
9381 else
9382 {
d5818fca
MR
9383 pos = imm_expr.X_add_number;
9384 size = imm2_expr.X_add_number;
5f74bc13
CD
9385 }
9386
9387 if (pos > 63)
9388 {
9e12b7a2 9389 report_bad_range (ip, 3, pos, 0, 63, FALSE);
5f74bc13
CD
9390 pos = 1;
9391 }
90ecf173 9392 if (size == 0 || size > 64 || (pos + size - 1) > 63)
5f74bc13 9393 {
9e12b7a2 9394 report_bad_field (pos, size);
5f74bc13
CD
9395 size = 1;
9396 }
9397
9398 if (pos < 32 && (pos + size - 1) < 32)
9399 {
9400 s = "dins";
9401 fmt = "t,r,+A,+B";
9402 }
9403 else if (pos >= 32)
9404 {
9405 s = "dinsu";
9406 fmt = "t,r,+E,+F";
9407 }
9408 else
9409 {
9410 s = "dinsm";
9411 fmt = "t,r,+A,+F";
9412 }
c0ebe874 9413 macro_build ((expressionS *) NULL, s, fmt, op[0], op[1], (int) pos,
750bdd57 9414 (int) (pos + size - 1));
5f74bc13 9415 }
8fc2e39e 9416 break;
5f74bc13 9417
252b5132
RH
9418 case M_DDIV_3:
9419 dbl = 1;
9420 case M_DIV_3:
9421 s = "mflo";
9422 goto do_div3;
9423 case M_DREM_3:
9424 dbl = 1;
9425 case M_REM_3:
9426 s = "mfhi";
9427 do_div3:
c0ebe874 9428 if (op[2] == 0)
252b5132
RH
9429 {
9430 as_warn (_("Divide by zero."));
9431 if (mips_trap)
df58fc94 9432 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 9433 else
df58fc94 9434 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 9435 break;
252b5132
RH
9436 }
9437
7d10b47d 9438 start_noreorder ();
252b5132
RH
9439 if (mips_trap)
9440 {
c0ebe874
RS
9441 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
9442 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
252b5132
RH
9443 }
9444 else
9445 {
df58fc94
RS
9446 if (mips_opts.micromips)
9447 micromips_label_expr (&label_expr);
9448 else
9449 label_expr.X_add_number = 8;
c0ebe874
RS
9450 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
9451 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
df58fc94
RS
9452 macro_build (NULL, "break", BRK_FMT, 7);
9453 if (mips_opts.micromips)
9454 micromips_add_label ();
252b5132
RH
9455 }
9456 expr1.X_add_number = -1;
8fc2e39e 9457 used_at = 1;
f6a22291 9458 load_register (AT, &expr1, dbl);
df58fc94
RS
9459 if (mips_opts.micromips)
9460 micromips_label_expr (&label_expr);
9461 else
9462 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
c0ebe874 9463 macro_build (&label_expr, "bne", "s,t,p", op[2], AT);
252b5132
RH
9464 if (dbl)
9465 {
9466 expr1.X_add_number = 1;
f6a22291 9467 load_register (AT, &expr1, dbl);
df58fc94 9468 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
252b5132
RH
9469 }
9470 else
9471 {
9472 expr1.X_add_number = 0x80000000;
df58fc94 9473 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
252b5132
RH
9474 }
9475 if (mips_trap)
9476 {
c0ebe874 9477 macro_build (NULL, "teq", TRAP_FMT, op[1], AT, 6);
252b5132
RH
9478 /* We want to close the noreorder block as soon as possible, so
9479 that later insns are available for delay slot filling. */
7d10b47d 9480 end_noreorder ();
252b5132
RH
9481 }
9482 else
9483 {
df58fc94
RS
9484 if (mips_opts.micromips)
9485 micromips_label_expr (&label_expr);
9486 else
9487 label_expr.X_add_number = 8;
c0ebe874 9488 macro_build (&label_expr, "bne", "s,t,p", op[1], AT);
a605d2b3 9489 macro_build (NULL, "nop", "");
252b5132
RH
9490
9491 /* We want to close the noreorder block as soon as possible, so
9492 that later insns are available for delay slot filling. */
7d10b47d 9493 end_noreorder ();
252b5132 9494
df58fc94 9495 macro_build (NULL, "break", BRK_FMT, 6);
252b5132 9496 }
df58fc94
RS
9497 if (mips_opts.micromips)
9498 micromips_add_label ();
c0ebe874 9499 macro_build (NULL, s, MFHL_FMT, op[0]);
252b5132
RH
9500 break;
9501
9502 case M_DIV_3I:
9503 s = "div";
9504 s2 = "mflo";
9505 goto do_divi;
9506 case M_DIVU_3I:
9507 s = "divu";
9508 s2 = "mflo";
9509 goto do_divi;
9510 case M_REM_3I:
9511 s = "div";
9512 s2 = "mfhi";
9513 goto do_divi;
9514 case M_REMU_3I:
9515 s = "divu";
9516 s2 = "mfhi";
9517 goto do_divi;
9518 case M_DDIV_3I:
9519 dbl = 1;
9520 s = "ddiv";
9521 s2 = "mflo";
9522 goto do_divi;
9523 case M_DDIVU_3I:
9524 dbl = 1;
9525 s = "ddivu";
9526 s2 = "mflo";
9527 goto do_divi;
9528 case M_DREM_3I:
9529 dbl = 1;
9530 s = "ddiv";
9531 s2 = "mfhi";
9532 goto do_divi;
9533 case M_DREMU_3I:
9534 dbl = 1;
9535 s = "ddivu";
9536 s2 = "mfhi";
9537 do_divi:
9538 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
9539 {
9540 as_warn (_("Divide by zero."));
9541 if (mips_trap)
df58fc94 9542 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 9543 else
df58fc94 9544 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 9545 break;
252b5132
RH
9546 }
9547 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
9548 {
9549 if (strcmp (s2, "mflo") == 0)
c0ebe874 9550 move_register (op[0], op[1]);
252b5132 9551 else
c0ebe874 9552 move_register (op[0], ZERO);
8fc2e39e 9553 break;
252b5132
RH
9554 }
9555 if (imm_expr.X_op == O_constant
9556 && imm_expr.X_add_number == -1
9557 && s[strlen (s) - 1] != 'u')
9558 {
9559 if (strcmp (s2, "mflo") == 0)
c0ebe874 9560 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", op[0], op[1]);
252b5132 9561 else
c0ebe874 9562 move_register (op[0], ZERO);
8fc2e39e 9563 break;
252b5132
RH
9564 }
9565
8fc2e39e 9566 used_at = 1;
67c0d1eb 9567 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
9568 macro_build (NULL, s, "z,s,t", op[1], AT);
9569 macro_build (NULL, s2, MFHL_FMT, op[0]);
252b5132
RH
9570 break;
9571
9572 case M_DIVU_3:
9573 s = "divu";
9574 s2 = "mflo";
9575 goto do_divu3;
9576 case M_REMU_3:
9577 s = "divu";
9578 s2 = "mfhi";
9579 goto do_divu3;
9580 case M_DDIVU_3:
9581 s = "ddivu";
9582 s2 = "mflo";
9583 goto do_divu3;
9584 case M_DREMU_3:
9585 s = "ddivu";
9586 s2 = "mfhi";
9587 do_divu3:
7d10b47d 9588 start_noreorder ();
252b5132
RH
9589 if (mips_trap)
9590 {
c0ebe874
RS
9591 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
9592 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
9593 /* We want to close the noreorder block as soon as possible, so
9594 that later insns are available for delay slot filling. */
7d10b47d 9595 end_noreorder ();
252b5132
RH
9596 }
9597 else
9598 {
df58fc94
RS
9599 if (mips_opts.micromips)
9600 micromips_label_expr (&label_expr);
9601 else
9602 label_expr.X_add_number = 8;
c0ebe874
RS
9603 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
9604 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
9605
9606 /* We want to close the noreorder block as soon as possible, so
9607 that later insns are available for delay slot filling. */
7d10b47d 9608 end_noreorder ();
df58fc94
RS
9609 macro_build (NULL, "break", BRK_FMT, 7);
9610 if (mips_opts.micromips)
9611 micromips_add_label ();
252b5132 9612 }
c0ebe874 9613 macro_build (NULL, s2, MFHL_FMT, op[0]);
8fc2e39e 9614 break;
252b5132 9615
1abe91b1
MR
9616 case M_DLCA_AB:
9617 dbl = 1;
9618 case M_LCA_AB:
9619 call = 1;
9620 goto do_la;
252b5132
RH
9621 case M_DLA_AB:
9622 dbl = 1;
9623 case M_LA_AB:
1abe91b1 9624 do_la:
252b5132
RH
9625 /* Load the address of a symbol into a register. If breg is not
9626 zero, we then add a base register to it. */
9627
c0ebe874 9628 breg = op[2];
3bec30a8
TS
9629 if (dbl && HAVE_32BIT_GPRS)
9630 as_warn (_("dla used to load 32-bit register"));
9631
90ecf173 9632 if (!dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
9633 as_warn (_("la used to load 64-bit address"));
9634
f2ae14a1 9635 if (small_offset_p (0, align, 16))
0c11417f 9636 {
c0ebe874 9637 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", op[0], breg,
f2ae14a1 9638 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8fc2e39e 9639 break;
0c11417f
MR
9640 }
9641
c0ebe874 9642 if (mips_opts.at && (op[0] == breg))
afdbd6d0
CD
9643 {
9644 tempreg = AT;
9645 used_at = 1;
9646 }
9647 else
c0ebe874 9648 tempreg = op[0];
afdbd6d0 9649
252b5132
RH
9650 if (offset_expr.X_op != O_symbol
9651 && offset_expr.X_op != O_constant)
9652 {
f71d0d44 9653 as_bad (_("Expression too complex"));
252b5132
RH
9654 offset_expr.X_op = O_constant;
9655 }
9656
252b5132 9657 if (offset_expr.X_op == O_constant)
aed1a261 9658 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
9659 else if (mips_pic == NO_PIC)
9660 {
d6bc6245 9661 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 9662 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
9663 Otherwise we want
9664 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
9665 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
9666 If we have a constant, we need two instructions anyhow,
d6bc6245 9667 so we may as well always use the latter form.
76b3015f 9668
6caf9ef4
TS
9669 With 64bit address space and a usable $at we want
9670 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9671 lui $at,<sym> (BFD_RELOC_HI16_S)
9672 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
9673 daddiu $at,<sym> (BFD_RELOC_LO16)
9674 dsll32 $tempreg,0
9675 daddu $tempreg,$tempreg,$at
9676
9677 If $at is already in use, we use a path which is suboptimal
9678 on superscalar processors.
9679 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9680 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
9681 dsll $tempreg,16
9682 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
9683 dsll $tempreg,16
9684 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
9685
9686 For GP relative symbols in 64bit address space we can use
9687 the same sequence as in 32bit address space. */
aed1a261 9688 if (HAVE_64BIT_SYMBOLS)
252b5132 9689 {
6caf9ef4
TS
9690 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
9691 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
9692 {
9693 relax_start (offset_expr.X_add_symbol);
9694 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
9695 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
9696 relax_switch ();
9697 }
d6bc6245 9698
741fe287 9699 if (used_at == 0 && mips_opts.at)
98d3f06f 9700 {
df58fc94 9701 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 9702 tempreg, BFD_RELOC_MIPS_HIGHEST);
df58fc94 9703 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 9704 AT, BFD_RELOC_HI16_S);
67c0d1eb 9705 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 9706 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 9707 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 9708 AT, AT, BFD_RELOC_LO16);
df58fc94 9709 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 9710 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
9711 used_at = 1;
9712 }
9713 else
9714 {
df58fc94 9715 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 9716 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 9717 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 9718 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 9719 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 9720 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 9721 tempreg, tempreg, BFD_RELOC_HI16_S);
df58fc94 9722 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 9723 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 9724 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 9725 }
6caf9ef4
TS
9726
9727 if (mips_relax.sequence)
9728 relax_end ();
98d3f06f
KH
9729 }
9730 else
9731 {
9732 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 9733 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 9734 {
4d7206a2 9735 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
9736 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
9737 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 9738 relax_switch ();
98d3f06f 9739 }
6943caf0 9740 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
f71d0d44 9741 as_bad (_("Offset too large"));
67c0d1eb
RS
9742 macro_build_lui (&offset_expr, tempreg);
9743 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
9744 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
9745 if (mips_relax.sequence)
9746 relax_end ();
98d3f06f 9747 }
252b5132 9748 }
0a44bf69 9749 else if (!mips_big_got && !HAVE_NEWABI)
252b5132 9750 {
9117d219
NC
9751 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
9752
252b5132
RH
9753 /* If this is a reference to an external symbol, and there
9754 is no constant, we want
9755 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 9756 or for lca or if tempreg is PIC_CALL_REG
9117d219 9757 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
9758 For a local symbol, we want
9759 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9760 nop
9761 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
9762
9763 If we have a small constant, and this is a reference to
9764 an external symbol, we want
9765 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9766 nop
9767 addiu $tempreg,$tempreg,<constant>
9768 For a local symbol, we want the same instruction
9769 sequence, but we output a BFD_RELOC_LO16 reloc on the
9770 addiu instruction.
9771
9772 If we have a large constant, and this is a reference to
9773 an external symbol, we want
9774 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9775 lui $at,<hiconstant>
9776 addiu $at,$at,<loconstant>
9777 addu $tempreg,$tempreg,$at
9778 For a local symbol, we want the same instruction
9779 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 9780 addiu instruction.
ed6fb7bd
SC
9781 */
9782
4d7206a2 9783 if (offset_expr.X_add_number == 0)
252b5132 9784 {
0a44bf69
RS
9785 if (mips_pic == SVR4_PIC
9786 && breg == 0
9787 && (call || tempreg == PIC_CALL_REG))
4d7206a2
RS
9788 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
9789
9790 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
9791 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9792 lw_reloc_type, mips_gp_register);
4d7206a2 9793 if (breg != 0)
252b5132
RH
9794 {
9795 /* We're going to put in an addu instruction using
9796 tempreg, so we may as well insert the nop right
9797 now. */
269137b2 9798 load_delay_nop ();
252b5132 9799 }
4d7206a2 9800 relax_switch ();
67c0d1eb
RS
9801 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9802 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9803 load_delay_nop ();
67c0d1eb
RS
9804 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
9805 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 9806 relax_end ();
252b5132
RH
9807 /* FIXME: If breg == 0, and the next instruction uses
9808 $tempreg, then if this variant case is used an extra
9809 nop will be generated. */
9810 }
4d7206a2
RS
9811 else if (offset_expr.X_add_number >= -0x8000
9812 && offset_expr.X_add_number < 0x8000)
252b5132 9813 {
67c0d1eb 9814 load_got_offset (tempreg, &offset_expr);
269137b2 9815 load_delay_nop ();
67c0d1eb 9816 add_got_offset (tempreg, &offset_expr);
252b5132
RH
9817 }
9818 else
9819 {
4d7206a2
RS
9820 expr1.X_add_number = offset_expr.X_add_number;
9821 offset_expr.X_add_number =
43c0598f 9822 SEXT_16BIT (offset_expr.X_add_number);
67c0d1eb 9823 load_got_offset (tempreg, &offset_expr);
f6a22291 9824 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
9825 /* If we are going to add in a base register, and the
9826 target register and the base register are the same,
9827 then we are using AT as a temporary register. Since
9828 we want to load the constant into AT, we add our
9829 current AT (from the global offset table) and the
9830 register into the register now, and pretend we were
9831 not using a base register. */
c0ebe874 9832 if (breg == op[0])
252b5132 9833 {
269137b2 9834 load_delay_nop ();
67c0d1eb 9835 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 9836 op[0], AT, breg);
252b5132 9837 breg = 0;
c0ebe874 9838 tempreg = op[0];
252b5132 9839 }
f6a22291 9840 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
9841 used_at = 1;
9842 }
9843 }
0a44bf69 9844 else if (!mips_big_got && HAVE_NEWABI)
f5040a92 9845 {
67c0d1eb 9846 int add_breg_early = 0;
f5040a92
AO
9847
9848 /* If this is a reference to an external, and there is no
9849 constant, or local symbol (*), with or without a
9850 constant, we want
9851 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 9852 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
9853 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
9854
9855 If we have a small constant, and this is a reference to
9856 an external symbol, we want
9857 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
9858 addiu $tempreg,$tempreg,<constant>
9859
9860 If we have a large constant, and this is a reference to
9861 an external symbol, we want
9862 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
9863 lui $at,<hiconstant>
9864 addiu $at,$at,<loconstant>
9865 addu $tempreg,$tempreg,$at
9866
9867 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
9868 local symbols, even though it introduces an additional
9869 instruction. */
9870
f5040a92
AO
9871 if (offset_expr.X_add_number)
9872 {
4d7206a2 9873 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
9874 offset_expr.X_add_number = 0;
9875
4d7206a2 9876 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
9877 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9878 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
9879
9880 if (expr1.X_add_number >= -0x8000
9881 && expr1.X_add_number < 0x8000)
9882 {
67c0d1eb
RS
9883 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
9884 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 9885 }
ecd13cd3 9886 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 9887 {
c0ebe874
RS
9888 unsigned int dreg;
9889
f5040a92
AO
9890 /* If we are going to add in a base register, and the
9891 target register and the base register are the same,
9892 then we are using AT as a temporary register. Since
9893 we want to load the constant into AT, we add our
9894 current AT (from the global offset table) and the
9895 register into the register now, and pretend we were
9896 not using a base register. */
c0ebe874 9897 if (breg != op[0])
f5040a92
AO
9898 dreg = tempreg;
9899 else
9900 {
9c2799c2 9901 gas_assert (tempreg == AT);
67c0d1eb 9902 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
9903 op[0], AT, breg);
9904 dreg = op[0];
67c0d1eb 9905 add_breg_early = 1;
f5040a92
AO
9906 }
9907
f6a22291 9908 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 9909 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9910 dreg, dreg, AT);
f5040a92 9911
f5040a92
AO
9912 used_at = 1;
9913 }
9914 else
9915 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
9916
4d7206a2 9917 relax_switch ();
f5040a92
AO
9918 offset_expr.X_add_number = expr1.X_add_number;
9919
67c0d1eb
RS
9920 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9921 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
9922 if (add_breg_early)
f5040a92 9923 {
67c0d1eb 9924 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 9925 op[0], tempreg, breg);
f5040a92 9926 breg = 0;
c0ebe874 9927 tempreg = op[0];
f5040a92 9928 }
4d7206a2 9929 relax_end ();
f5040a92 9930 }
4d7206a2 9931 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 9932 {
4d7206a2 9933 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
9934 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9935 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 9936 relax_switch ();
67c0d1eb
RS
9937 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9938 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 9939 relax_end ();
f5040a92 9940 }
4d7206a2 9941 else
f5040a92 9942 {
67c0d1eb
RS
9943 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9944 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
9945 }
9946 }
0a44bf69 9947 else if (mips_big_got && !HAVE_NEWABI)
252b5132 9948 {
67c0d1eb 9949 int gpdelay;
9117d219
NC
9950 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
9951 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 9952 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
9953
9954 /* This is the large GOT case. If this is a reference to an
9955 external symbol, and there is no constant, we want
9956 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9957 addu $tempreg,$tempreg,$gp
9958 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 9959 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
9960 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
9961 addu $tempreg,$tempreg,$gp
9962 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
9963 For a local symbol, we want
9964 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9965 nop
9966 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
9967
9968 If we have a small constant, and this is a reference to
9969 an external symbol, we want
9970 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9971 addu $tempreg,$tempreg,$gp
9972 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9973 nop
9974 addiu $tempreg,$tempreg,<constant>
9975 For a local symbol, we want
9976 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9977 nop
9978 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
9979
9980 If we have a large constant, and this is a reference to
9981 an external symbol, we want
9982 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9983 addu $tempreg,$tempreg,$gp
9984 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9985 lui $at,<hiconstant>
9986 addiu $at,$at,<loconstant>
9987 addu $tempreg,$tempreg,$at
9988 For a local symbol, we want
9989 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9990 lui $at,<hiconstant>
9991 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
9992 addu $tempreg,$tempreg,$at
f5040a92 9993 */
438c16b8 9994
252b5132
RH
9995 expr1.X_add_number = offset_expr.X_add_number;
9996 offset_expr.X_add_number = 0;
4d7206a2 9997 relax_start (offset_expr.X_add_symbol);
67c0d1eb 9998 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
9999 if (expr1.X_add_number == 0 && breg == 0
10000 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
10001 {
10002 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
10003 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
10004 }
df58fc94 10005 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 10006 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10007 tempreg, tempreg, mips_gp_register);
67c0d1eb 10008 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 10009 tempreg, lw_reloc_type, tempreg);
252b5132
RH
10010 if (expr1.X_add_number == 0)
10011 {
67c0d1eb 10012 if (breg != 0)
252b5132
RH
10013 {
10014 /* We're going to put in an addu instruction using
10015 tempreg, so we may as well insert the nop right
10016 now. */
269137b2 10017 load_delay_nop ();
252b5132 10018 }
252b5132
RH
10019 }
10020 else if (expr1.X_add_number >= -0x8000
10021 && expr1.X_add_number < 0x8000)
10022 {
269137b2 10023 load_delay_nop ();
67c0d1eb 10024 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 10025 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
10026 }
10027 else
10028 {
c0ebe874
RS
10029 unsigned int dreg;
10030
252b5132
RH
10031 /* If we are going to add in a base register, and the
10032 target register and the base register are the same,
10033 then we are using AT as a temporary register. Since
10034 we want to load the constant into AT, we add our
10035 current AT (from the global offset table) and the
10036 register into the register now, and pretend we were
10037 not using a base register. */
c0ebe874 10038 if (breg != op[0])
67c0d1eb 10039 dreg = tempreg;
252b5132
RH
10040 else
10041 {
9c2799c2 10042 gas_assert (tempreg == AT);
269137b2 10043 load_delay_nop ();
67c0d1eb 10044 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
10045 op[0], AT, breg);
10046 dreg = op[0];
252b5132
RH
10047 }
10048
f6a22291 10049 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 10050 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 10051
252b5132
RH
10052 used_at = 1;
10053 }
43c0598f 10054 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
4d7206a2 10055 relax_switch ();
252b5132 10056
67c0d1eb 10057 if (gpdelay)
252b5132
RH
10058 {
10059 /* This is needed because this instruction uses $gp, but
f5040a92 10060 the first instruction on the main stream does not. */
67c0d1eb 10061 macro_build (NULL, "nop", "");
252b5132 10062 }
ed6fb7bd 10063
67c0d1eb
RS
10064 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10065 local_reloc_type, mips_gp_register);
f5040a92 10066 if (expr1.X_add_number >= -0x8000
252b5132
RH
10067 && expr1.X_add_number < 0x8000)
10068 {
269137b2 10069 load_delay_nop ();
67c0d1eb
RS
10070 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10071 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 10072 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
10073 register, the external symbol case ended with a load,
10074 so if the symbol turns out to not be external, and
10075 the next instruction uses tempreg, an unnecessary nop
10076 will be inserted. */
252b5132
RH
10077 }
10078 else
10079 {
c0ebe874 10080 if (breg == op[0])
252b5132
RH
10081 {
10082 /* We must add in the base register now, as in the
f5040a92 10083 external symbol case. */
9c2799c2 10084 gas_assert (tempreg == AT);
269137b2 10085 load_delay_nop ();
67c0d1eb 10086 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
10087 op[0], AT, breg);
10088 tempreg = op[0];
252b5132 10089 /* We set breg to 0 because we have arranged to add
f5040a92 10090 it in in both cases. */
252b5132
RH
10091 breg = 0;
10092 }
10093
67c0d1eb
RS
10094 macro_build_lui (&expr1, AT);
10095 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 10096 AT, AT, BFD_RELOC_LO16);
67c0d1eb 10097 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10098 tempreg, tempreg, AT);
8fc2e39e 10099 used_at = 1;
252b5132 10100 }
4d7206a2 10101 relax_end ();
252b5132 10102 }
0a44bf69 10103 else if (mips_big_got && HAVE_NEWABI)
f5040a92 10104 {
f5040a92
AO
10105 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
10106 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 10107 int add_breg_early = 0;
f5040a92
AO
10108
10109 /* This is the large GOT case. If this is a reference to an
10110 external symbol, and there is no constant, we want
10111 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10112 add $tempreg,$tempreg,$gp
10113 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 10114 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
10115 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
10116 add $tempreg,$tempreg,$gp
10117 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
10118
10119 If we have a small constant, and this is a reference to
10120 an external symbol, we want
10121 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10122 add $tempreg,$tempreg,$gp
10123 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10124 addi $tempreg,$tempreg,<constant>
10125
10126 If we have a large constant, and this is a reference to
10127 an external symbol, we want
10128 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10129 addu $tempreg,$tempreg,$gp
10130 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10131 lui $at,<hiconstant>
10132 addi $at,$at,<loconstant>
10133 add $tempreg,$tempreg,$at
10134
10135 If we have NewABI, and we know it's a local symbol, we want
10136 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
10137 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
10138 otherwise we have to resort to GOT_HI16/GOT_LO16. */
10139
4d7206a2 10140 relax_start (offset_expr.X_add_symbol);
f5040a92 10141
4d7206a2 10142 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
10143 offset_expr.X_add_number = 0;
10144
1abe91b1
MR
10145 if (expr1.X_add_number == 0 && breg == 0
10146 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
10147 {
10148 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
10149 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
10150 }
df58fc94 10151 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 10152 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10153 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
10154 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10155 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
10156
10157 if (expr1.X_add_number == 0)
4d7206a2 10158 ;
f5040a92
AO
10159 else if (expr1.X_add_number >= -0x8000
10160 && expr1.X_add_number < 0x8000)
10161 {
67c0d1eb 10162 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 10163 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 10164 }
ecd13cd3 10165 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 10166 {
c0ebe874
RS
10167 unsigned int dreg;
10168
f5040a92
AO
10169 /* If we are going to add in a base register, and the
10170 target register and the base register are the same,
10171 then we are using AT as a temporary register. Since
10172 we want to load the constant into AT, we add our
10173 current AT (from the global offset table) and the
10174 register into the register now, and pretend we were
10175 not using a base register. */
c0ebe874 10176 if (breg != op[0])
f5040a92
AO
10177 dreg = tempreg;
10178 else
10179 {
9c2799c2 10180 gas_assert (tempreg == AT);
67c0d1eb 10181 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
10182 op[0], AT, breg);
10183 dreg = op[0];
67c0d1eb 10184 add_breg_early = 1;
f5040a92
AO
10185 }
10186
f6a22291 10187 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 10188 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 10189
f5040a92
AO
10190 used_at = 1;
10191 }
10192 else
10193 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
10194
4d7206a2 10195 relax_switch ();
f5040a92 10196 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
10197 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10198 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
10199 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
10200 tempreg, BFD_RELOC_MIPS_GOT_OFST);
10201 if (add_breg_early)
f5040a92 10202 {
67c0d1eb 10203 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 10204 op[0], tempreg, breg);
f5040a92 10205 breg = 0;
c0ebe874 10206 tempreg = op[0];
f5040a92 10207 }
4d7206a2 10208 relax_end ();
f5040a92 10209 }
252b5132
RH
10210 else
10211 abort ();
10212
10213 if (breg != 0)
c0ebe874 10214 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", op[0], tempreg, breg);
252b5132
RH
10215 break;
10216
52b6b6b9 10217 case M_MSGSND:
df58fc94 10218 gas_assert (!mips_opts.micromips);
c0ebe874 10219 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x01);
c7af4273 10220 break;
52b6b6b9
JM
10221
10222 case M_MSGLD:
df58fc94 10223 gas_assert (!mips_opts.micromips);
c8276761 10224 macro_build (NULL, "c2", "C", 0x02);
c7af4273 10225 break;
52b6b6b9
JM
10226
10227 case M_MSGLD_T:
df58fc94 10228 gas_assert (!mips_opts.micromips);
c0ebe874 10229 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x02);
c7af4273 10230 break;
52b6b6b9
JM
10231
10232 case M_MSGWAIT:
df58fc94 10233 gas_assert (!mips_opts.micromips);
52b6b6b9 10234 macro_build (NULL, "c2", "C", 3);
c7af4273 10235 break;
52b6b6b9
JM
10236
10237 case M_MSGWAIT_T:
df58fc94 10238 gas_assert (!mips_opts.micromips);
c0ebe874 10239 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x03);
c7af4273 10240 break;
52b6b6b9 10241
252b5132
RH
10242 case M_J_A:
10243 /* The j instruction may not be used in PIC code, since it
10244 requires an absolute address. We convert it to a b
10245 instruction. */
10246 if (mips_pic == NO_PIC)
67c0d1eb 10247 macro_build (&offset_expr, "j", "a");
252b5132 10248 else
67c0d1eb 10249 macro_build (&offset_expr, "b", "p");
8fc2e39e 10250 break;
252b5132
RH
10251
10252 /* The jal instructions must be handled as macros because when
10253 generating PIC code they expand to multi-instruction
10254 sequences. Normally they are simple instructions. */
df58fc94 10255 case M_JALS_1:
c0ebe874
RS
10256 op[1] = op[0];
10257 op[0] = RA;
df58fc94
RS
10258 /* Fall through. */
10259 case M_JALS_2:
10260 gas_assert (mips_opts.micromips);
833794fc
MR
10261 if (mips_opts.insn32)
10262 {
10263 as_bad (_("Opcode not supported in the `insn32' mode `%s'"), str);
10264 break;
10265 }
df58fc94
RS
10266 jals = 1;
10267 goto jal;
252b5132 10268 case M_JAL_1:
c0ebe874
RS
10269 op[1] = op[0];
10270 op[0] = RA;
252b5132
RH
10271 /* Fall through. */
10272 case M_JAL_2:
df58fc94 10273 jal:
3e722fb5 10274 if (mips_pic == NO_PIC)
df58fc94
RS
10275 {
10276 s = jals ? "jalrs" : "jalr";
e64af278 10277 if (mips_opts.micromips
833794fc 10278 && !mips_opts.insn32
c0ebe874 10279 && op[0] == RA
e64af278 10280 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 10281 macro_build (NULL, s, "mj", op[1]);
df58fc94 10282 else
c0ebe874 10283 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
df58fc94 10284 }
0a44bf69 10285 else
252b5132 10286 {
df58fc94
RS
10287 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
10288 && mips_cprestore_offset >= 0);
10289
c0ebe874 10290 if (op[1] != PIC_CALL_REG)
252b5132 10291 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 10292
833794fc
MR
10293 s = ((mips_opts.micromips
10294 && !mips_opts.insn32
10295 && (!mips_opts.noreorder || cprestore))
df58fc94 10296 ? "jalrs" : "jalr");
e64af278 10297 if (mips_opts.micromips
833794fc 10298 && !mips_opts.insn32
c0ebe874 10299 && op[0] == RA
e64af278 10300 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 10301 macro_build (NULL, s, "mj", op[1]);
df58fc94 10302 else
c0ebe874 10303 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
0a44bf69 10304 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
252b5132 10305 {
6478892d
TS
10306 if (mips_cprestore_offset < 0)
10307 as_warn (_("No .cprestore pseudo-op used in PIC code"));
10308 else
10309 {
90ecf173 10310 if (!mips_frame_reg_valid)
7a621144
DJ
10311 {
10312 as_warn (_("No .frame pseudo-op used in PIC code"));
10313 /* Quiet this warning. */
10314 mips_frame_reg_valid = 1;
10315 }
90ecf173 10316 if (!mips_cprestore_valid)
7a621144
DJ
10317 {
10318 as_warn (_("No .cprestore pseudo-op used in PIC code"));
10319 /* Quiet this warning. */
10320 mips_cprestore_valid = 1;
10321 }
d3fca0b5
MR
10322 if (mips_opts.noreorder)
10323 macro_build (NULL, "nop", "");
6478892d 10324 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 10325 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 10326 mips_gp_register,
256ab948
TS
10327 mips_frame_reg,
10328 HAVE_64BIT_ADDRESSES);
6478892d 10329 }
252b5132
RH
10330 }
10331 }
252b5132 10332
8fc2e39e 10333 break;
252b5132 10334
df58fc94
RS
10335 case M_JALS_A:
10336 gas_assert (mips_opts.micromips);
833794fc
MR
10337 if (mips_opts.insn32)
10338 {
10339 as_bad (_("Opcode not supported in the `insn32' mode `%s'"), str);
10340 break;
10341 }
df58fc94
RS
10342 jals = 1;
10343 /* Fall through. */
252b5132
RH
10344 case M_JAL_A:
10345 if (mips_pic == NO_PIC)
df58fc94 10346 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
252b5132
RH
10347 else if (mips_pic == SVR4_PIC)
10348 {
10349 /* If this is a reference to an external symbol, and we are
10350 using a small GOT, we want
10351 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
10352 nop
f9419b05 10353 jalr $ra,$25
252b5132
RH
10354 nop
10355 lw $gp,cprestore($sp)
10356 The cprestore value is set using the .cprestore
10357 pseudo-op. If we are using a big GOT, we want
10358 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
10359 addu $25,$25,$gp
10360 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
10361 nop
f9419b05 10362 jalr $ra,$25
252b5132
RH
10363 nop
10364 lw $gp,cprestore($sp)
10365 If the symbol is not external, we want
10366 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10367 nop
10368 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 10369 jalr $ra,$25
252b5132 10370 nop
438c16b8 10371 lw $gp,cprestore($sp)
f5040a92
AO
10372
10373 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
10374 sequences above, minus nops, unless the symbol is local,
10375 which enables us to use GOT_PAGE/GOT_OFST (big got) or
10376 GOT_DISP. */
438c16b8 10377 if (HAVE_NEWABI)
252b5132 10378 {
90ecf173 10379 if (!mips_big_got)
f5040a92 10380 {
4d7206a2 10381 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
10382 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10383 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 10384 mips_gp_register);
4d7206a2 10385 relax_switch ();
67c0d1eb
RS
10386 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10387 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
10388 mips_gp_register);
10389 relax_end ();
f5040a92
AO
10390 }
10391 else
10392 {
4d7206a2 10393 relax_start (offset_expr.X_add_symbol);
df58fc94 10394 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
10395 BFD_RELOC_MIPS_CALL_HI16);
10396 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
10397 PIC_CALL_REG, mips_gp_register);
10398 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10399 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
10400 PIC_CALL_REG);
4d7206a2 10401 relax_switch ();
67c0d1eb
RS
10402 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10403 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
10404 mips_gp_register);
10405 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10406 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 10407 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 10408 relax_end ();
f5040a92 10409 }
684022ea 10410
df58fc94 10411 macro_build_jalr (&offset_expr, 0);
252b5132
RH
10412 }
10413 else
10414 {
4d7206a2 10415 relax_start (offset_expr.X_add_symbol);
90ecf173 10416 if (!mips_big_got)
438c16b8 10417 {
67c0d1eb
RS
10418 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10419 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 10420 mips_gp_register);
269137b2 10421 load_delay_nop ();
4d7206a2 10422 relax_switch ();
438c16b8 10423 }
252b5132 10424 else
252b5132 10425 {
67c0d1eb
RS
10426 int gpdelay;
10427
10428 gpdelay = reg_needs_delay (mips_gp_register);
df58fc94 10429 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
10430 BFD_RELOC_MIPS_CALL_HI16);
10431 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
10432 PIC_CALL_REG, mips_gp_register);
10433 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10434 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
10435 PIC_CALL_REG);
269137b2 10436 load_delay_nop ();
4d7206a2 10437 relax_switch ();
67c0d1eb
RS
10438 if (gpdelay)
10439 macro_build (NULL, "nop", "");
252b5132 10440 }
67c0d1eb
RS
10441 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10442 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 10443 mips_gp_register);
269137b2 10444 load_delay_nop ();
67c0d1eb
RS
10445 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10446 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 10447 relax_end ();
df58fc94 10448 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
438c16b8 10449
6478892d
TS
10450 if (mips_cprestore_offset < 0)
10451 as_warn (_("No .cprestore pseudo-op used in PIC code"));
10452 else
10453 {
90ecf173 10454 if (!mips_frame_reg_valid)
7a621144
DJ
10455 {
10456 as_warn (_("No .frame pseudo-op used in PIC code"));
10457 /* Quiet this warning. */
10458 mips_frame_reg_valid = 1;
10459 }
90ecf173 10460 if (!mips_cprestore_valid)
7a621144
DJ
10461 {
10462 as_warn (_("No .cprestore pseudo-op used in PIC code"));
10463 /* Quiet this warning. */
10464 mips_cprestore_valid = 1;
10465 }
6478892d 10466 if (mips_opts.noreorder)
67c0d1eb 10467 macro_build (NULL, "nop", "");
6478892d 10468 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 10469 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 10470 mips_gp_register,
256ab948
TS
10471 mips_frame_reg,
10472 HAVE_64BIT_ADDRESSES);
6478892d 10473 }
252b5132
RH
10474 }
10475 }
0a44bf69
RS
10476 else if (mips_pic == VXWORKS_PIC)
10477 as_bad (_("Non-PIC jump used in PIC library"));
252b5132
RH
10478 else
10479 abort ();
10480
8fc2e39e 10481 break;
252b5132 10482
7f3c4072 10483 case M_LBUE_AB:
7f3c4072
CM
10484 s = "lbue";
10485 fmt = "t,+j(b)";
10486 offbits = 9;
10487 goto ld_st;
10488 case M_LHUE_AB:
7f3c4072
CM
10489 s = "lhue";
10490 fmt = "t,+j(b)";
10491 offbits = 9;
10492 goto ld_st;
10493 case M_LBE_AB:
7f3c4072
CM
10494 s = "lbe";
10495 fmt = "t,+j(b)";
10496 offbits = 9;
10497 goto ld_st;
10498 case M_LHE_AB:
7f3c4072
CM
10499 s = "lhe";
10500 fmt = "t,+j(b)";
10501 offbits = 9;
10502 goto ld_st;
10503 case M_LLE_AB:
7f3c4072
CM
10504 s = "lle";
10505 fmt = "t,+j(b)";
10506 offbits = 9;
10507 goto ld_st;
10508 case M_LWE_AB:
7f3c4072
CM
10509 s = "lwe";
10510 fmt = "t,+j(b)";
10511 offbits = 9;
10512 goto ld_st;
10513 case M_LWLE_AB:
7f3c4072
CM
10514 s = "lwle";
10515 fmt = "t,+j(b)";
10516 offbits = 9;
10517 goto ld_st;
10518 case M_LWRE_AB:
7f3c4072
CM
10519 s = "lwre";
10520 fmt = "t,+j(b)";
10521 offbits = 9;
10522 goto ld_st;
10523 case M_SBE_AB:
7f3c4072
CM
10524 s = "sbe";
10525 fmt = "t,+j(b)";
10526 offbits = 9;
10527 goto ld_st;
10528 case M_SCE_AB:
7f3c4072
CM
10529 s = "sce";
10530 fmt = "t,+j(b)";
10531 offbits = 9;
10532 goto ld_st;
10533 case M_SHE_AB:
7f3c4072
CM
10534 s = "she";
10535 fmt = "t,+j(b)";
10536 offbits = 9;
10537 goto ld_st;
10538 case M_SWE_AB:
7f3c4072
CM
10539 s = "swe";
10540 fmt = "t,+j(b)";
10541 offbits = 9;
10542 goto ld_st;
10543 case M_SWLE_AB:
7f3c4072
CM
10544 s = "swle";
10545 fmt = "t,+j(b)";
10546 offbits = 9;
10547 goto ld_st;
10548 case M_SWRE_AB:
7f3c4072
CM
10549 s = "swre";
10550 fmt = "t,+j(b)";
10551 offbits = 9;
10552 goto ld_st;
dec0624d 10553 case M_ACLR_AB:
dec0624d 10554 s = "aclr";
dec0624d 10555 fmt = "\\,~(b)";
7f3c4072 10556 offbits = 12;
dec0624d
MR
10557 goto ld_st;
10558 case M_ASET_AB:
dec0624d 10559 s = "aset";
dec0624d 10560 fmt = "\\,~(b)";
7f3c4072 10561 offbits = 12;
dec0624d 10562 goto ld_st;
252b5132
RH
10563 case M_LB_AB:
10564 s = "lb";
df58fc94 10565 fmt = "t,o(b)";
252b5132
RH
10566 goto ld;
10567 case M_LBU_AB:
10568 s = "lbu";
df58fc94 10569 fmt = "t,o(b)";
252b5132
RH
10570 goto ld;
10571 case M_LH_AB:
10572 s = "lh";
df58fc94 10573 fmt = "t,o(b)";
252b5132
RH
10574 goto ld;
10575 case M_LHU_AB:
10576 s = "lhu";
df58fc94 10577 fmt = "t,o(b)";
252b5132
RH
10578 goto ld;
10579 case M_LW_AB:
10580 s = "lw";
df58fc94 10581 fmt = "t,o(b)";
252b5132
RH
10582 goto ld;
10583 case M_LWC0_AB:
df58fc94 10584 gas_assert (!mips_opts.micromips);
252b5132 10585 s = "lwc0";
df58fc94 10586 fmt = "E,o(b)";
bdaaa2e1 10587 /* Itbl support may require additional care here. */
252b5132 10588 coproc = 1;
df58fc94 10589 goto ld_st;
252b5132
RH
10590 case M_LWC1_AB:
10591 s = "lwc1";
df58fc94 10592 fmt = "T,o(b)";
bdaaa2e1 10593 /* Itbl support may require additional care here. */
252b5132 10594 coproc = 1;
df58fc94 10595 goto ld_st;
252b5132
RH
10596 case M_LWC2_AB:
10597 s = "lwc2";
df58fc94 10598 fmt = COP12_FMT;
7f3c4072 10599 offbits = (mips_opts.micromips ? 12 : 16);
bdaaa2e1 10600 /* Itbl support may require additional care here. */
252b5132 10601 coproc = 1;
df58fc94 10602 goto ld_st;
252b5132 10603 case M_LWC3_AB:
df58fc94 10604 gas_assert (!mips_opts.micromips);
252b5132 10605 s = "lwc3";
df58fc94 10606 fmt = "E,o(b)";
bdaaa2e1 10607 /* Itbl support may require additional care here. */
252b5132 10608 coproc = 1;
df58fc94 10609 goto ld_st;
252b5132
RH
10610 case M_LWL_AB:
10611 s = "lwl";
df58fc94 10612 fmt = MEM12_FMT;
7f3c4072 10613 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10614 goto ld_st;
252b5132
RH
10615 case M_LWR_AB:
10616 s = "lwr";
df58fc94 10617 fmt = MEM12_FMT;
7f3c4072 10618 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10619 goto ld_st;
252b5132 10620 case M_LDC1_AB:
252b5132 10621 s = "ldc1";
df58fc94 10622 fmt = "T,o(b)";
bdaaa2e1 10623 /* Itbl support may require additional care here. */
252b5132 10624 coproc = 1;
df58fc94 10625 goto ld_st;
252b5132
RH
10626 case M_LDC2_AB:
10627 s = "ldc2";
df58fc94 10628 fmt = COP12_FMT;
7f3c4072 10629 offbits = (mips_opts.micromips ? 12 : 16);
bdaaa2e1 10630 /* Itbl support may require additional care here. */
252b5132 10631 coproc = 1;
df58fc94 10632 goto ld_st;
c77c0862 10633 case M_LQC2_AB:
c77c0862 10634 s = "lqc2";
14daeee3 10635 fmt = "+7,o(b)";
c77c0862
RS
10636 /* Itbl support may require additional care here. */
10637 coproc = 1;
10638 goto ld_st;
252b5132
RH
10639 case M_LDC3_AB:
10640 s = "ldc3";
df58fc94 10641 fmt = "E,o(b)";
bdaaa2e1 10642 /* Itbl support may require additional care here. */
252b5132 10643 coproc = 1;
df58fc94 10644 goto ld_st;
252b5132
RH
10645 case M_LDL_AB:
10646 s = "ldl";
df58fc94 10647 fmt = MEM12_FMT;
7f3c4072 10648 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10649 goto ld_st;
252b5132
RH
10650 case M_LDR_AB:
10651 s = "ldr";
df58fc94 10652 fmt = MEM12_FMT;
7f3c4072 10653 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10654 goto ld_st;
252b5132
RH
10655 case M_LL_AB:
10656 s = "ll";
df58fc94 10657 fmt = MEM12_FMT;
7f3c4072 10658 offbits = (mips_opts.micromips ? 12 : 16);
252b5132
RH
10659 goto ld;
10660 case M_LLD_AB:
10661 s = "lld";
df58fc94 10662 fmt = MEM12_FMT;
7f3c4072 10663 offbits = (mips_opts.micromips ? 12 : 16);
252b5132
RH
10664 goto ld;
10665 case M_LWU_AB:
10666 s = "lwu";
df58fc94 10667 fmt = MEM12_FMT;
7f3c4072 10668 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
10669 goto ld;
10670 case M_LWP_AB:
df58fc94
RS
10671 gas_assert (mips_opts.micromips);
10672 s = "lwp";
10673 fmt = "t,~(b)";
7f3c4072 10674 offbits = 12;
df58fc94
RS
10675 lp = 1;
10676 goto ld;
10677 case M_LDP_AB:
df58fc94
RS
10678 gas_assert (mips_opts.micromips);
10679 s = "ldp";
10680 fmt = "t,~(b)";
7f3c4072 10681 offbits = 12;
df58fc94
RS
10682 lp = 1;
10683 goto ld;
10684 case M_LWM_AB:
df58fc94
RS
10685 gas_assert (mips_opts.micromips);
10686 s = "lwm";
10687 fmt = "n,~(b)";
7f3c4072 10688 offbits = 12;
df58fc94
RS
10689 goto ld_st;
10690 case M_LDM_AB:
df58fc94
RS
10691 gas_assert (mips_opts.micromips);
10692 s = "ldm";
10693 fmt = "n,~(b)";
7f3c4072 10694 offbits = 12;
df58fc94
RS
10695 goto ld_st;
10696
252b5132 10697 ld:
f19ccbda 10698 /* We don't want to use $0 as tempreg. */
c0ebe874 10699 if (op[2] == op[0] + lp || op[0] + lp == ZERO)
df58fc94 10700 goto ld_st;
252b5132 10701 else
c0ebe874 10702 tempreg = op[0] + lp;
df58fc94
RS
10703 goto ld_noat;
10704
252b5132
RH
10705 case M_SB_AB:
10706 s = "sb";
df58fc94
RS
10707 fmt = "t,o(b)";
10708 goto ld_st;
252b5132
RH
10709 case M_SH_AB:
10710 s = "sh";
df58fc94
RS
10711 fmt = "t,o(b)";
10712 goto ld_st;
252b5132
RH
10713 case M_SW_AB:
10714 s = "sw";
df58fc94
RS
10715 fmt = "t,o(b)";
10716 goto ld_st;
252b5132 10717 case M_SWC0_AB:
df58fc94 10718 gas_assert (!mips_opts.micromips);
252b5132 10719 s = "swc0";
df58fc94 10720 fmt = "E,o(b)";
bdaaa2e1 10721 /* Itbl support may require additional care here. */
252b5132 10722 coproc = 1;
df58fc94 10723 goto ld_st;
252b5132
RH
10724 case M_SWC1_AB:
10725 s = "swc1";
df58fc94 10726 fmt = "T,o(b)";
bdaaa2e1 10727 /* Itbl support may require additional care here. */
252b5132 10728 coproc = 1;
df58fc94 10729 goto ld_st;
252b5132
RH
10730 case M_SWC2_AB:
10731 s = "swc2";
df58fc94 10732 fmt = COP12_FMT;
7f3c4072 10733 offbits = (mips_opts.micromips ? 12 : 16);
bdaaa2e1 10734 /* Itbl support may require additional care here. */
252b5132 10735 coproc = 1;
df58fc94 10736 goto ld_st;
252b5132 10737 case M_SWC3_AB:
df58fc94 10738 gas_assert (!mips_opts.micromips);
252b5132 10739 s = "swc3";
df58fc94 10740 fmt = "E,o(b)";
bdaaa2e1 10741 /* Itbl support may require additional care here. */
252b5132 10742 coproc = 1;
df58fc94 10743 goto ld_st;
252b5132
RH
10744 case M_SWL_AB:
10745 s = "swl";
df58fc94 10746 fmt = MEM12_FMT;
7f3c4072 10747 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10748 goto ld_st;
252b5132
RH
10749 case M_SWR_AB:
10750 s = "swr";
df58fc94 10751 fmt = MEM12_FMT;
7f3c4072 10752 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10753 goto ld_st;
252b5132
RH
10754 case M_SC_AB:
10755 s = "sc";
df58fc94 10756 fmt = MEM12_FMT;
7f3c4072 10757 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10758 goto ld_st;
252b5132
RH
10759 case M_SCD_AB:
10760 s = "scd";
df58fc94 10761 fmt = MEM12_FMT;
7f3c4072 10762 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10763 goto ld_st;
d43b4baf
TS
10764 case M_CACHE_AB:
10765 s = "cache";
df58fc94 10766 fmt = mips_opts.micromips ? "k,~(b)" : "k,o(b)";
7f3c4072
CM
10767 offbits = (mips_opts.micromips ? 12 : 16);
10768 goto ld_st;
10769 case M_CACHEE_AB:
7f3c4072
CM
10770 s = "cachee";
10771 fmt = "k,+j(b)";
10772 offbits = 9;
df58fc94 10773 goto ld_st;
3eebd5eb
MR
10774 case M_PREF_AB:
10775 s = "pref";
df58fc94 10776 fmt = !mips_opts.micromips ? "k,o(b)" : "k,~(b)";
7f3c4072
CM
10777 offbits = (mips_opts.micromips ? 12 : 16);
10778 goto ld_st;
10779 case M_PREFE_AB:
7f3c4072
CM
10780 s = "prefe";
10781 fmt = "k,+j(b)";
10782 offbits = 9;
df58fc94 10783 goto ld_st;
252b5132 10784 case M_SDC1_AB:
252b5132 10785 s = "sdc1";
df58fc94 10786 fmt = "T,o(b)";
252b5132 10787 coproc = 1;
bdaaa2e1 10788 /* Itbl support may require additional care here. */
df58fc94 10789 goto ld_st;
252b5132
RH
10790 case M_SDC2_AB:
10791 s = "sdc2";
df58fc94 10792 fmt = COP12_FMT;
7f3c4072 10793 offbits = (mips_opts.micromips ? 12 : 16);
c77c0862
RS
10794 /* Itbl support may require additional care here. */
10795 coproc = 1;
10796 goto ld_st;
10797 case M_SQC2_AB:
c77c0862 10798 s = "sqc2";
14daeee3 10799 fmt = "+7,o(b)";
bdaaa2e1 10800 /* Itbl support may require additional care here. */
252b5132 10801 coproc = 1;
df58fc94 10802 goto ld_st;
252b5132 10803 case M_SDC3_AB:
df58fc94 10804 gas_assert (!mips_opts.micromips);
252b5132 10805 s = "sdc3";
df58fc94 10806 fmt = "E,o(b)";
bdaaa2e1 10807 /* Itbl support may require additional care here. */
252b5132 10808 coproc = 1;
df58fc94 10809 goto ld_st;
252b5132
RH
10810 case M_SDL_AB:
10811 s = "sdl";
df58fc94 10812 fmt = MEM12_FMT;
7f3c4072 10813 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10814 goto ld_st;
252b5132
RH
10815 case M_SDR_AB:
10816 s = "sdr";
df58fc94 10817 fmt = MEM12_FMT;
7f3c4072 10818 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
10819 goto ld_st;
10820 case M_SWP_AB:
df58fc94
RS
10821 gas_assert (mips_opts.micromips);
10822 s = "swp";
10823 fmt = "t,~(b)";
7f3c4072 10824 offbits = 12;
df58fc94
RS
10825 goto ld_st;
10826 case M_SDP_AB:
df58fc94
RS
10827 gas_assert (mips_opts.micromips);
10828 s = "sdp";
10829 fmt = "t,~(b)";
7f3c4072 10830 offbits = 12;
df58fc94
RS
10831 goto ld_st;
10832 case M_SWM_AB:
df58fc94
RS
10833 gas_assert (mips_opts.micromips);
10834 s = "swm";
10835 fmt = "n,~(b)";
7f3c4072 10836 offbits = 12;
df58fc94
RS
10837 goto ld_st;
10838 case M_SDM_AB:
df58fc94
RS
10839 gas_assert (mips_opts.micromips);
10840 s = "sdm";
10841 fmt = "n,~(b)";
7f3c4072 10842 offbits = 12;
df58fc94
RS
10843
10844 ld_st:
8fc2e39e 10845 tempreg = AT;
df58fc94 10846 ld_noat:
c0ebe874 10847 breg = op[2];
f2ae14a1
RS
10848 if (small_offset_p (0, align, 16))
10849 {
10850 /* The first case exists for M_LD_AB and M_SD_AB, which are
10851 macros for o32 but which should act like normal instructions
10852 otherwise. */
10853 if (offbits == 16)
c0ebe874 10854 macro_build (&offset_expr, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
10855 offset_reloc[1], offset_reloc[2], breg);
10856 else if (small_offset_p (0, align, offbits))
10857 {
10858 if (offbits == 0)
c0ebe874 10859 macro_build (NULL, s, fmt, op[0], breg);
f2ae14a1 10860 else
c0ebe874 10861 macro_build (NULL, s, fmt, op[0],
c8276761 10862 (int) offset_expr.X_add_number, breg);
f2ae14a1
RS
10863 }
10864 else
10865 {
10866 if (tempreg == AT)
10867 used_at = 1;
10868 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10869 tempreg, breg, -1, offset_reloc[0],
10870 offset_reloc[1], offset_reloc[2]);
10871 if (offbits == 0)
c0ebe874 10872 macro_build (NULL, s, fmt, op[0], tempreg);
f2ae14a1 10873 else
c0ebe874 10874 macro_build (NULL, s, fmt, op[0], 0, tempreg);
f2ae14a1
RS
10875 }
10876 break;
10877 }
10878
10879 if (tempreg == AT)
10880 used_at = 1;
10881
252b5132
RH
10882 if (offset_expr.X_op != O_constant
10883 && offset_expr.X_op != O_symbol)
10884 {
f71d0d44 10885 as_bad (_("Expression too complex"));
252b5132
RH
10886 offset_expr.X_op = O_constant;
10887 }
10888
2051e8c4
MR
10889 if (HAVE_32BIT_ADDRESSES
10890 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
10891 {
10892 char value [32];
10893
10894 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 10895 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 10896 }
2051e8c4 10897
252b5132
RH
10898 /* A constant expression in PIC code can be handled just as it
10899 is in non PIC code. */
aed1a261
RS
10900 if (offset_expr.X_op == O_constant)
10901 {
f2ae14a1
RS
10902 expr1.X_add_number = offset_high_part (offset_expr.X_add_number,
10903 offbits == 0 ? 16 : offbits);
10904 offset_expr.X_add_number -= expr1.X_add_number;
df58fc94 10905
f2ae14a1
RS
10906 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
10907 if (breg != 0)
10908 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10909 tempreg, tempreg, breg);
7f3c4072 10910 if (offbits == 0)
dd6a37e7 10911 {
f2ae14a1 10912 if (offset_expr.X_add_number != 0)
dd6a37e7 10913 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
f2ae14a1 10914 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
c0ebe874 10915 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 10916 }
7f3c4072 10917 else if (offbits == 16)
c0ebe874 10918 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
df58fc94 10919 else
c0ebe874 10920 macro_build (NULL, s, fmt, op[0],
c8276761 10921 (int) offset_expr.X_add_number, tempreg);
df58fc94 10922 }
7f3c4072 10923 else if (offbits != 16)
df58fc94 10924 {
7f3c4072
CM
10925 /* The offset field is too narrow to be used for a low-part
10926 relocation, so load the whole address into the auxillary
f2ae14a1
RS
10927 register. */
10928 load_address (tempreg, &offset_expr, &used_at);
10929 if (breg != 0)
10930 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10931 tempreg, tempreg, breg);
7f3c4072 10932 if (offbits == 0)
c0ebe874 10933 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 10934 else
c0ebe874 10935 macro_build (NULL, s, fmt, op[0], 0, tempreg);
aed1a261
RS
10936 }
10937 else if (mips_pic == NO_PIC)
252b5132
RH
10938 {
10939 /* If this is a reference to a GP relative symbol, and there
10940 is no base register, we want
c0ebe874 10941 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
10942 Otherwise, if there is no base register, we want
10943 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
c0ebe874 10944 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
252b5132
RH
10945 If we have a constant, we need two instructions anyhow,
10946 so we always use the latter form.
10947
10948 If we have a base register, and this is a reference to a
10949 GP relative symbol, we want
10950 addu $tempreg,$breg,$gp
c0ebe874 10951 <op> op[0],<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
10952 Otherwise we want
10953 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
10954 addu $tempreg,$tempreg,$breg
c0ebe874 10955 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 10956 With a constant we always use the latter case.
76b3015f 10957
d6bc6245
TS
10958 With 64bit address space and no base register and $at usable,
10959 we want
10960 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10961 lui $at,<sym> (BFD_RELOC_HI16_S)
10962 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10963 dsll32 $tempreg,0
10964 daddu $tempreg,$at
c0ebe874 10965 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
10966 If we have a base register, we want
10967 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10968 lui $at,<sym> (BFD_RELOC_HI16_S)
10969 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10970 daddu $at,$breg
10971 dsll32 $tempreg,0
10972 daddu $tempreg,$at
c0ebe874 10973 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
10974
10975 Without $at we can't generate the optimal path for superscalar
10976 processors here since this would require two temporary registers.
10977 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10978 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10979 dsll $tempreg,16
10980 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
10981 dsll $tempreg,16
c0ebe874 10982 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
10983 If we have a base register, we want
10984 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10985 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10986 dsll $tempreg,16
10987 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
10988 dsll $tempreg,16
10989 daddu $tempreg,$tempreg,$breg
c0ebe874 10990 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 10991
6caf9ef4 10992 For GP relative symbols in 64bit address space we can use
aed1a261
RS
10993 the same sequence as in 32bit address space. */
10994 if (HAVE_64BIT_SYMBOLS)
d6bc6245 10995 {
aed1a261 10996 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
10997 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
10998 {
10999 relax_start (offset_expr.X_add_symbol);
11000 if (breg == 0)
11001 {
c0ebe874 11002 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
11003 BFD_RELOC_GPREL16, mips_gp_register);
11004 }
11005 else
11006 {
11007 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11008 tempreg, breg, mips_gp_register);
c0ebe874 11009 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
11010 BFD_RELOC_GPREL16, tempreg);
11011 }
11012 relax_switch ();
11013 }
d6bc6245 11014
741fe287 11015 if (used_at == 0 && mips_opts.at)
d6bc6245 11016 {
df58fc94 11017 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb 11018 BFD_RELOC_MIPS_HIGHEST);
df58fc94 11019 macro_build (&offset_expr, "lui", LUI_FMT, AT,
67c0d1eb
RS
11020 BFD_RELOC_HI16_S);
11021 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
11022 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 11023 if (breg != 0)
67c0d1eb 11024 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
df58fc94 11025 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 11026 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
c0ebe874 11027 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16,
67c0d1eb 11028 tempreg);
d6bc6245
TS
11029 used_at = 1;
11030 }
11031 else
11032 {
df58fc94 11033 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb
RS
11034 BFD_RELOC_MIPS_HIGHEST);
11035 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
11036 tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 11037 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb
RS
11038 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
11039 tempreg, BFD_RELOC_HI16_S);
df58fc94 11040 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
d6bc6245 11041 if (breg != 0)
67c0d1eb 11042 macro_build (NULL, "daddu", "d,v,t",
17a2f251 11043 tempreg, tempreg, breg);
c0ebe874 11044 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 11045 BFD_RELOC_LO16, tempreg);
d6bc6245 11046 }
6caf9ef4
TS
11047
11048 if (mips_relax.sequence)
11049 relax_end ();
8fc2e39e 11050 break;
d6bc6245 11051 }
256ab948 11052
252b5132
RH
11053 if (breg == 0)
11054 {
67c0d1eb 11055 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 11056 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 11057 {
4d7206a2 11058 relax_start (offset_expr.X_add_symbol);
c0ebe874 11059 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_GPREL16,
67c0d1eb 11060 mips_gp_register);
4d7206a2 11061 relax_switch ();
252b5132 11062 }
67c0d1eb 11063 macro_build_lui (&offset_expr, tempreg);
c0ebe874 11064 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 11065 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
11066 if (mips_relax.sequence)
11067 relax_end ();
252b5132
RH
11068 }
11069 else
11070 {
67c0d1eb 11071 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 11072 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 11073 {
4d7206a2 11074 relax_start (offset_expr.X_add_symbol);
67c0d1eb 11075 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11076 tempreg, breg, mips_gp_register);
c0ebe874 11077 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 11078 BFD_RELOC_GPREL16, tempreg);
4d7206a2 11079 relax_switch ();
252b5132 11080 }
67c0d1eb
RS
11081 macro_build_lui (&offset_expr, tempreg);
11082 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11083 tempreg, tempreg, breg);
c0ebe874 11084 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 11085 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
11086 if (mips_relax.sequence)
11087 relax_end ();
252b5132
RH
11088 }
11089 }
0a44bf69 11090 else if (!mips_big_got)
252b5132 11091 {
ed6fb7bd 11092 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 11093
252b5132
RH
11094 /* If this is a reference to an external symbol, we want
11095 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11096 nop
c0ebe874 11097 <op> op[0],0($tempreg)
252b5132
RH
11098 Otherwise we want
11099 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11100 nop
11101 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 11102 <op> op[0],0($tempreg)
f5040a92
AO
11103
11104 For NewABI, we want
11105 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 11106 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 11107
252b5132
RH
11108 If there is a base register, we add it to $tempreg before
11109 the <op>. If there is a constant, we stick it in the
11110 <op> instruction. We don't handle constants larger than
11111 16 bits, because we have no way to load the upper 16 bits
11112 (actually, we could handle them for the subset of cases
11113 in which we are not using $at). */
9c2799c2 11114 gas_assert (offset_expr.X_op == O_symbol);
f5040a92
AO
11115 if (HAVE_NEWABI)
11116 {
67c0d1eb
RS
11117 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11118 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 11119 if (breg != 0)
67c0d1eb 11120 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11121 tempreg, tempreg, breg);
c0ebe874 11122 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 11123 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
11124 break;
11125 }
252b5132
RH
11126 expr1.X_add_number = offset_expr.X_add_number;
11127 offset_expr.X_add_number = 0;
11128 if (expr1.X_add_number < -0x8000
11129 || expr1.X_add_number >= 0x8000)
11130 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
11131 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11132 lw_reloc_type, mips_gp_register);
269137b2 11133 load_delay_nop ();
4d7206a2
RS
11134 relax_start (offset_expr.X_add_symbol);
11135 relax_switch ();
67c0d1eb
RS
11136 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11137 tempreg, BFD_RELOC_LO16);
4d7206a2 11138 relax_end ();
252b5132 11139 if (breg != 0)
67c0d1eb 11140 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11141 tempreg, tempreg, breg);
c0ebe874 11142 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 11143 }
0a44bf69 11144 else if (mips_big_got && !HAVE_NEWABI)
252b5132 11145 {
67c0d1eb 11146 int gpdelay;
252b5132
RH
11147
11148 /* If this is a reference to an external symbol, we want
11149 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11150 addu $tempreg,$tempreg,$gp
11151 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 11152 <op> op[0],0($tempreg)
252b5132
RH
11153 Otherwise we want
11154 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11155 nop
11156 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 11157 <op> op[0],0($tempreg)
252b5132
RH
11158 If there is a base register, we add it to $tempreg before
11159 the <op>. If there is a constant, we stick it in the
11160 <op> instruction. We don't handle constants larger than
11161 16 bits, because we have no way to load the upper 16 bits
11162 (actually, we could handle them for the subset of cases
f5040a92 11163 in which we are not using $at). */
9c2799c2 11164 gas_assert (offset_expr.X_op == O_symbol);
252b5132
RH
11165 expr1.X_add_number = offset_expr.X_add_number;
11166 offset_expr.X_add_number = 0;
11167 if (expr1.X_add_number < -0x8000
11168 || expr1.X_add_number >= 0x8000)
11169 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 11170 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 11171 relax_start (offset_expr.X_add_symbol);
df58fc94 11172 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 11173 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
11174 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
11175 mips_gp_register);
11176 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11177 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 11178 relax_switch ();
67c0d1eb
RS
11179 if (gpdelay)
11180 macro_build (NULL, "nop", "");
11181 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11182 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 11183 load_delay_nop ();
67c0d1eb
RS
11184 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11185 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
11186 relax_end ();
11187
252b5132 11188 if (breg != 0)
67c0d1eb 11189 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11190 tempreg, tempreg, breg);
c0ebe874 11191 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 11192 }
0a44bf69 11193 else if (mips_big_got && HAVE_NEWABI)
f5040a92 11194 {
f5040a92
AO
11195 /* If this is a reference to an external symbol, we want
11196 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11197 add $tempreg,$tempreg,$gp
11198 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 11199 <op> op[0],<ofst>($tempreg)
f5040a92
AO
11200 Otherwise, for local symbols, we want:
11201 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 11202 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
9c2799c2 11203 gas_assert (offset_expr.X_op == O_symbol);
4d7206a2 11204 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
11205 offset_expr.X_add_number = 0;
11206 if (expr1.X_add_number < -0x8000
11207 || expr1.X_add_number >= 0x8000)
11208 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 11209 relax_start (offset_expr.X_add_symbol);
df58fc94 11210 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 11211 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
11212 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
11213 mips_gp_register);
11214 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11215 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 11216 if (breg != 0)
67c0d1eb 11217 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11218 tempreg, tempreg, breg);
c0ebe874 11219 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
684022ea 11220
4d7206a2 11221 relax_switch ();
f5040a92 11222 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
11223 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11224 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 11225 if (breg != 0)
67c0d1eb 11226 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11227 tempreg, tempreg, breg);
c0ebe874 11228 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 11229 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 11230 relax_end ();
f5040a92 11231 }
252b5132
RH
11232 else
11233 abort ();
11234
252b5132
RH
11235 break;
11236
833794fc
MR
11237 case M_JRADDIUSP:
11238 gas_assert (mips_opts.micromips);
11239 gas_assert (mips_opts.insn32);
11240 start_noreorder ();
11241 macro_build (NULL, "jr", "s", RA);
c0ebe874 11242 expr1.X_add_number = op[0] << 2;
833794fc
MR
11243 macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
11244 end_noreorder ();
11245 break;
11246
11247 case M_JRC:
11248 gas_assert (mips_opts.micromips);
11249 gas_assert (mips_opts.insn32);
c0ebe874 11250 macro_build (NULL, "jr", "s", op[0]);
833794fc
MR
11251 if (mips_opts.noreorder)
11252 macro_build (NULL, "nop", "");
11253 break;
11254
252b5132
RH
11255 case M_LI:
11256 case M_LI_S:
c0ebe874 11257 load_register (op[0], &imm_expr, 0);
8fc2e39e 11258 break;
252b5132
RH
11259
11260 case M_DLI:
c0ebe874 11261 load_register (op[0], &imm_expr, 1);
8fc2e39e 11262 break;
252b5132
RH
11263
11264 case M_LI_SS:
11265 if (imm_expr.X_op == O_constant)
11266 {
8fc2e39e 11267 used_at = 1;
67c0d1eb 11268 load_register (AT, &imm_expr, 0);
c0ebe874 11269 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
11270 break;
11271 }
11272 else
11273 {
9c2799c2 11274 gas_assert (offset_expr.X_op == O_symbol
90ecf173
MR
11275 && strcmp (segment_name (S_GET_SEGMENT
11276 (offset_expr.X_add_symbol)),
11277 ".lit4") == 0
11278 && offset_expr.X_add_number == 0);
c0ebe874 11279 macro_build (&offset_expr, "lwc1", "T,o(b)", op[0],
17a2f251 11280 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 11281 break;
252b5132
RH
11282 }
11283
11284 case M_LI_D:
ca4e0257
RS
11285 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
11286 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
11287 order 32 bits of the value and the low order 32 bits are either
11288 zero or in OFFSET_EXPR. */
252b5132
RH
11289 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
11290 {
ca4e0257 11291 if (HAVE_64BIT_GPRS)
c0ebe874 11292 load_register (op[0], &imm_expr, 1);
252b5132
RH
11293 else
11294 {
11295 int hreg, lreg;
11296
11297 if (target_big_endian)
11298 {
c0ebe874
RS
11299 hreg = op[0];
11300 lreg = op[0] + 1;
252b5132
RH
11301 }
11302 else
11303 {
c0ebe874
RS
11304 hreg = op[0] + 1;
11305 lreg = op[0];
252b5132
RH
11306 }
11307
11308 if (hreg <= 31)
67c0d1eb 11309 load_register (hreg, &imm_expr, 0);
252b5132
RH
11310 if (lreg <= 31)
11311 {
11312 if (offset_expr.X_op == O_absent)
67c0d1eb 11313 move_register (lreg, 0);
252b5132
RH
11314 else
11315 {
9c2799c2 11316 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 11317 load_register (lreg, &offset_expr, 0);
252b5132
RH
11318 }
11319 }
11320 }
8fc2e39e 11321 break;
252b5132
RH
11322 }
11323
11324 /* We know that sym is in the .rdata section. First we get the
11325 upper 16 bits of the address. */
11326 if (mips_pic == NO_PIC)
11327 {
67c0d1eb 11328 macro_build_lui (&offset_expr, AT);
8fc2e39e 11329 used_at = 1;
252b5132 11330 }
0a44bf69 11331 else
252b5132 11332 {
67c0d1eb
RS
11333 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
11334 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 11335 used_at = 1;
252b5132 11336 }
bdaaa2e1 11337
252b5132 11338 /* Now we load the register(s). */
ca4e0257 11339 if (HAVE_64BIT_GPRS)
8fc2e39e
TS
11340 {
11341 used_at = 1;
c0ebe874
RS
11342 macro_build (&offset_expr, "ld", "t,o(b)", op[0],
11343 BFD_RELOC_LO16, AT);
8fc2e39e 11344 }
252b5132
RH
11345 else
11346 {
8fc2e39e 11347 used_at = 1;
c0ebe874
RS
11348 macro_build (&offset_expr, "lw", "t,o(b)", op[0],
11349 BFD_RELOC_LO16, AT);
11350 if (op[0] != RA)
252b5132
RH
11351 {
11352 /* FIXME: How in the world do we deal with the possible
11353 overflow here? */
11354 offset_expr.X_add_number += 4;
67c0d1eb 11355 macro_build (&offset_expr, "lw", "t,o(b)",
c0ebe874 11356 op[0] + 1, BFD_RELOC_LO16, AT);
252b5132
RH
11357 }
11358 }
252b5132
RH
11359 break;
11360
11361 case M_LI_DD:
ca4e0257
RS
11362 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
11363 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
11364 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
11365 the value and the low order 32 bits are either zero or in
11366 OFFSET_EXPR. */
252b5132
RH
11367 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
11368 {
8fc2e39e 11369 used_at = 1;
67c0d1eb 11370 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
ca4e0257
RS
11371 if (HAVE_64BIT_FPRS)
11372 {
9c2799c2 11373 gas_assert (HAVE_64BIT_GPRS);
c0ebe874 11374 macro_build (NULL, "dmtc1", "t,S", AT, op[0]);
ca4e0257 11375 }
252b5132
RH
11376 else
11377 {
c0ebe874 11378 macro_build (NULL, "mtc1", "t,G", AT, op[0] + 1);
252b5132 11379 if (offset_expr.X_op == O_absent)
c0ebe874 11380 macro_build (NULL, "mtc1", "t,G", 0, op[0]);
252b5132
RH
11381 else
11382 {
9c2799c2 11383 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 11384 load_register (AT, &offset_expr, 0);
c0ebe874 11385 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
11386 }
11387 }
11388 break;
11389 }
11390
9c2799c2 11391 gas_assert (offset_expr.X_op == O_symbol
90ecf173 11392 && offset_expr.X_add_number == 0);
252b5132
RH
11393 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
11394 if (strcmp (s, ".lit8") == 0)
f2ae14a1 11395 {
c0ebe874 11396 op[2] = mips_gp_register;
f2ae14a1
RS
11397 offset_reloc[0] = BFD_RELOC_MIPS_LITERAL;
11398 offset_reloc[1] = BFD_RELOC_UNUSED;
11399 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
11400 }
11401 else
11402 {
9c2799c2 11403 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 11404 used_at = 1;
0a44bf69 11405 if (mips_pic != NO_PIC)
67c0d1eb
RS
11406 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
11407 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
11408 else
11409 {
11410 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 11411 macro_build_lui (&offset_expr, AT);
252b5132 11412 }
bdaaa2e1 11413
c0ebe874 11414 op[2] = AT;
f2ae14a1
RS
11415 offset_reloc[0] = BFD_RELOC_LO16;
11416 offset_reloc[1] = BFD_RELOC_UNUSED;
11417 offset_reloc[2] = BFD_RELOC_UNUSED;
11418 }
11419 align = 8;
11420 /* Fall through */
c4a68bea 11421
252b5132
RH
11422 case M_L_DAB:
11423 /*
11424 * The MIPS assembler seems to check for X_add_number not
11425 * being double aligned and generating:
11426 * lui at,%hi(foo+1)
11427 * addu at,at,v1
11428 * addiu at,at,%lo(foo+1)
11429 * lwc1 f2,0(at)
11430 * lwc1 f3,4(at)
11431 * But, the resulting address is the same after relocation so why
11432 * generate the extra instruction?
11433 */
bdaaa2e1 11434 /* Itbl support may require additional care here. */
252b5132 11435 coproc = 1;
df58fc94 11436 fmt = "T,o(b)";
0aa27725 11437 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
11438 {
11439 s = "ldc1";
df58fc94 11440 goto ld_st;
252b5132 11441 }
252b5132 11442 s = "lwc1";
252b5132
RH
11443 goto ldd_std;
11444
11445 case M_S_DAB:
df58fc94
RS
11446 gas_assert (!mips_opts.micromips);
11447 /* Itbl support may require additional care here. */
11448 coproc = 1;
11449 fmt = "T,o(b)";
0aa27725 11450 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
11451 {
11452 s = "sdc1";
df58fc94 11453 goto ld_st;
252b5132 11454 }
252b5132 11455 s = "swc1";
252b5132
RH
11456 goto ldd_std;
11457
e407c74b
NC
11458 case M_LQ_AB:
11459 fmt = "t,o(b)";
11460 s = "lq";
11461 goto ld;
11462
11463 case M_SQ_AB:
11464 fmt = "t,o(b)";
11465 s = "sq";
11466 goto ld_st;
11467
252b5132 11468 case M_LD_AB:
df58fc94 11469 fmt = "t,o(b)";
ca4e0257 11470 if (HAVE_64BIT_GPRS)
252b5132
RH
11471 {
11472 s = "ld";
11473 goto ld;
11474 }
252b5132 11475 s = "lw";
252b5132
RH
11476 goto ldd_std;
11477
11478 case M_SD_AB:
df58fc94 11479 fmt = "t,o(b)";
ca4e0257 11480 if (HAVE_64BIT_GPRS)
252b5132
RH
11481 {
11482 s = "sd";
df58fc94 11483 goto ld_st;
252b5132 11484 }
252b5132 11485 s = "sw";
252b5132
RH
11486
11487 ldd_std:
f2ae14a1
RS
11488 /* Even on a big endian machine $fn comes before $fn+1. We have
11489 to adjust when loading from memory. We set coproc if we must
11490 load $fn+1 first. */
11491 /* Itbl support may require additional care here. */
11492 if (!target_big_endian)
11493 coproc = 0;
11494
c0ebe874 11495 breg = op[2];
f2ae14a1
RS
11496 if (small_offset_p (0, align, 16))
11497 {
11498 ep = &offset_expr;
11499 if (!small_offset_p (4, align, 16))
11500 {
11501 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT, breg,
11502 -1, offset_reloc[0], offset_reloc[1],
11503 offset_reloc[2]);
11504 expr1.X_add_number = 0;
11505 ep = &expr1;
11506 breg = AT;
11507 used_at = 1;
11508 offset_reloc[0] = BFD_RELOC_LO16;
11509 offset_reloc[1] = BFD_RELOC_UNUSED;
11510 offset_reloc[2] = BFD_RELOC_UNUSED;
11511 }
c0ebe874 11512 if (strcmp (s, "lw") == 0 && op[0] == breg)
f2ae14a1
RS
11513 {
11514 ep->X_add_number += 4;
c0ebe874 11515 macro_build (ep, s, fmt, op[0] + 1, -1, offset_reloc[0],
f2ae14a1
RS
11516 offset_reloc[1], offset_reloc[2], breg);
11517 ep->X_add_number -= 4;
c0ebe874 11518 macro_build (ep, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
11519 offset_reloc[1], offset_reloc[2], breg);
11520 }
11521 else
11522 {
c0ebe874 11523 macro_build (ep, s, fmt, coproc ? op[0] + 1 : op[0], -1,
f2ae14a1
RS
11524 offset_reloc[0], offset_reloc[1], offset_reloc[2],
11525 breg);
11526 ep->X_add_number += 4;
c0ebe874 11527 macro_build (ep, s, fmt, coproc ? op[0] : op[0] + 1, -1,
f2ae14a1
RS
11528 offset_reloc[0], offset_reloc[1], offset_reloc[2],
11529 breg);
11530 }
11531 break;
11532 }
11533
252b5132
RH
11534 if (offset_expr.X_op != O_symbol
11535 && offset_expr.X_op != O_constant)
11536 {
f71d0d44 11537 as_bad (_("Expression too complex"));
252b5132
RH
11538 offset_expr.X_op = O_constant;
11539 }
11540
2051e8c4
MR
11541 if (HAVE_32BIT_ADDRESSES
11542 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
11543 {
11544 char value [32];
11545
11546 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 11547 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 11548 }
2051e8c4 11549
90ecf173 11550 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
252b5132
RH
11551 {
11552 /* If this is a reference to a GP relative symbol, we want
c0ebe874
RS
11553 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
11554 <op> op[0]+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
11555 If we have a base register, we use this
11556 addu $at,$breg,$gp
c0ebe874
RS
11557 <op> op[0],<sym>($at) (BFD_RELOC_GPREL16)
11558 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
11559 If this is not a GP relative symbol, we want
11560 lui $at,<sym> (BFD_RELOC_HI16_S)
c0ebe874
RS
11561 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
11562 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
11563 If there is a base register, we add it to $at after the
11564 lui instruction. If there is a constant, we always use
11565 the last case. */
39a59cf8
MR
11566 if (offset_expr.X_op == O_symbol
11567 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 11568 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 11569 {
4d7206a2 11570 relax_start (offset_expr.X_add_symbol);
252b5132
RH
11571 if (breg == 0)
11572 {
c9914766 11573 tempreg = mips_gp_register;
252b5132
RH
11574 }
11575 else
11576 {
67c0d1eb 11577 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11578 AT, breg, mips_gp_register);
252b5132 11579 tempreg = AT;
252b5132
RH
11580 used_at = 1;
11581 }
11582
beae10d5 11583 /* Itbl support may require additional care here. */
c0ebe874 11584 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 11585 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
11586 offset_expr.X_add_number += 4;
11587
11588 /* Set mips_optimize to 2 to avoid inserting an
11589 undesired nop. */
11590 hold_mips_optimize = mips_optimize;
11591 mips_optimize = 2;
beae10d5 11592 /* Itbl support may require additional care here. */
c0ebe874 11593 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 11594 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
11595 mips_optimize = hold_mips_optimize;
11596
4d7206a2 11597 relax_switch ();
252b5132 11598
0970e49e 11599 offset_expr.X_add_number -= 4;
252b5132 11600 }
8fc2e39e 11601 used_at = 1;
f2ae14a1
RS
11602 if (offset_high_part (offset_expr.X_add_number, 16)
11603 != offset_high_part (offset_expr.X_add_number + 4, 16))
11604 {
11605 load_address (AT, &offset_expr, &used_at);
11606 offset_expr.X_op = O_constant;
11607 offset_expr.X_add_number = 0;
11608 }
11609 else
11610 macro_build_lui (&offset_expr, AT);
252b5132 11611 if (breg != 0)
67c0d1eb 11612 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 11613 /* Itbl support may require additional care here. */
c0ebe874 11614 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 11615 BFD_RELOC_LO16, AT);
252b5132
RH
11616 /* FIXME: How do we handle overflow here? */
11617 offset_expr.X_add_number += 4;
beae10d5 11618 /* Itbl support may require additional care here. */
c0ebe874 11619 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 11620 BFD_RELOC_LO16, AT);
4d7206a2
RS
11621 if (mips_relax.sequence)
11622 relax_end ();
bdaaa2e1 11623 }
0a44bf69 11624 else if (!mips_big_got)
252b5132 11625 {
252b5132
RH
11626 /* If this is a reference to an external symbol, we want
11627 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11628 nop
c0ebe874
RS
11629 <op> op[0],0($at)
11630 <op> op[0]+1,4($at)
252b5132
RH
11631 Otherwise we want
11632 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11633 nop
c0ebe874
RS
11634 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
11635 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
11636 If there is a base register we add it to $at before the
11637 lwc1 instructions. If there is a constant we include it
11638 in the lwc1 instructions. */
11639 used_at = 1;
11640 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
11641 if (expr1.X_add_number < -0x8000
11642 || expr1.X_add_number >= 0x8000 - 4)
11643 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 11644 load_got_offset (AT, &offset_expr);
269137b2 11645 load_delay_nop ();
252b5132 11646 if (breg != 0)
67c0d1eb 11647 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
11648
11649 /* Set mips_optimize to 2 to avoid inserting an undesired
11650 nop. */
11651 hold_mips_optimize = mips_optimize;
11652 mips_optimize = 2;
4d7206a2 11653
beae10d5 11654 /* Itbl support may require additional care here. */
4d7206a2 11655 relax_start (offset_expr.X_add_symbol);
c0ebe874 11656 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 11657 BFD_RELOC_LO16, AT);
4d7206a2 11658 expr1.X_add_number += 4;
c0ebe874 11659 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 11660 BFD_RELOC_LO16, AT);
4d7206a2 11661 relax_switch ();
c0ebe874 11662 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 11663 BFD_RELOC_LO16, AT);
4d7206a2 11664 offset_expr.X_add_number += 4;
c0ebe874 11665 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 11666 BFD_RELOC_LO16, AT);
4d7206a2 11667 relax_end ();
252b5132 11668
4d7206a2 11669 mips_optimize = hold_mips_optimize;
252b5132 11670 }
0a44bf69 11671 else if (mips_big_got)
252b5132 11672 {
67c0d1eb 11673 int gpdelay;
252b5132
RH
11674
11675 /* If this is a reference to an external symbol, we want
11676 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11677 addu $at,$at,$gp
11678 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
11679 nop
c0ebe874
RS
11680 <op> op[0],0($at)
11681 <op> op[0]+1,4($at)
252b5132
RH
11682 Otherwise we want
11683 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11684 nop
c0ebe874
RS
11685 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
11686 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
11687 If there is a base register we add it to $at before the
11688 lwc1 instructions. If there is a constant we include it
11689 in the lwc1 instructions. */
11690 used_at = 1;
11691 expr1.X_add_number = offset_expr.X_add_number;
11692 offset_expr.X_add_number = 0;
11693 if (expr1.X_add_number < -0x8000
11694 || expr1.X_add_number >= 0x8000 - 4)
11695 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 11696 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 11697 relax_start (offset_expr.X_add_symbol);
df58fc94 11698 macro_build (&offset_expr, "lui", LUI_FMT,
67c0d1eb
RS
11699 AT, BFD_RELOC_MIPS_GOT_HI16);
11700 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11701 AT, AT, mips_gp_register);
67c0d1eb 11702 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 11703 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 11704 load_delay_nop ();
252b5132 11705 if (breg != 0)
67c0d1eb 11706 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 11707 /* Itbl support may require additional care here. */
c0ebe874 11708 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 11709 BFD_RELOC_LO16, AT);
252b5132
RH
11710 expr1.X_add_number += 4;
11711
11712 /* Set mips_optimize to 2 to avoid inserting an undesired
11713 nop. */
11714 hold_mips_optimize = mips_optimize;
11715 mips_optimize = 2;
beae10d5 11716 /* Itbl support may require additional care here. */
c0ebe874 11717 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 11718 BFD_RELOC_LO16, AT);
252b5132
RH
11719 mips_optimize = hold_mips_optimize;
11720 expr1.X_add_number -= 4;
11721
4d7206a2
RS
11722 relax_switch ();
11723 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
11724 if (gpdelay)
11725 macro_build (NULL, "nop", "");
11726 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
11727 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 11728 load_delay_nop ();
252b5132 11729 if (breg != 0)
67c0d1eb 11730 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 11731 /* Itbl support may require additional care here. */
c0ebe874 11732 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 11733 BFD_RELOC_LO16, AT);
4d7206a2 11734 offset_expr.X_add_number += 4;
252b5132
RH
11735
11736 /* Set mips_optimize to 2 to avoid inserting an undesired
11737 nop. */
11738 hold_mips_optimize = mips_optimize;
11739 mips_optimize = 2;
beae10d5 11740 /* Itbl support may require additional care here. */
c0ebe874 11741 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 11742 BFD_RELOC_LO16, AT);
252b5132 11743 mips_optimize = hold_mips_optimize;
4d7206a2 11744 relax_end ();
252b5132 11745 }
252b5132
RH
11746 else
11747 abort ();
11748
252b5132 11749 break;
dd6a37e7
AP
11750
11751 case M_SAA_AB:
dd6a37e7 11752 s = "saa";
7f3c4072 11753 offbits = 0;
dd6a37e7
AP
11754 fmt = "t,(b)";
11755 goto ld_st;
11756 case M_SAAD_AB:
dd6a37e7 11757 s = "saad";
7f3c4072 11758 offbits = 0;
dd6a37e7
AP
11759 fmt = "t,(b)";
11760 goto ld_st;
11761
252b5132
RH
11762 /* New code added to support COPZ instructions.
11763 This code builds table entries out of the macros in mip_opcodes.
11764 R4000 uses interlocks to handle coproc delays.
11765 Other chips (like the R3000) require nops to be inserted for delays.
11766
f72c8c98 11767 FIXME: Currently, we require that the user handle delays.
252b5132
RH
11768 In order to fill delay slots for non-interlocked chips,
11769 we must have a way to specify delays based on the coprocessor.
11770 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
11771 What are the side-effects of the cop instruction?
11772 What cache support might we have and what are its effects?
11773 Both coprocessor & memory require delays. how long???
bdaaa2e1 11774 What registers are read/set/modified?
252b5132
RH
11775
11776 If an itbl is provided to interpret cop instructions,
bdaaa2e1 11777 this knowledge can be encoded in the itbl spec. */
252b5132
RH
11778
11779 case M_COP0:
11780 s = "c0";
11781 goto copz;
11782 case M_COP1:
11783 s = "c1";
11784 goto copz;
11785 case M_COP2:
11786 s = "c2";
11787 goto copz;
11788 case M_COP3:
11789 s = "c3";
11790 copz:
df58fc94 11791 gas_assert (!mips_opts.micromips);
252b5132
RH
11792 /* For now we just do C (same as Cz). The parameter will be
11793 stored in insn_opcode by mips_ip. */
c8276761 11794 macro_build (NULL, s, "C", (int) ip->insn_opcode);
8fc2e39e 11795 break;
252b5132 11796
ea1fb5dc 11797 case M_MOVE:
c0ebe874 11798 move_register (op[0], op[1]);
8fc2e39e 11799 break;
ea1fb5dc 11800
833794fc
MR
11801 case M_MOVEP:
11802 gas_assert (mips_opts.micromips);
11803 gas_assert (mips_opts.insn32);
c0ebe874
RS
11804 move_register (micromips_to_32_reg_h_map1[op[0]],
11805 micromips_to_32_reg_m_map[op[1]]);
11806 move_register (micromips_to_32_reg_h_map2[op[0]],
11807 micromips_to_32_reg_n_map[op[2]]);
833794fc
MR
11808 break;
11809
252b5132
RH
11810 case M_DMUL:
11811 dbl = 1;
11812 case M_MUL:
e407c74b 11813 if (mips_opts.arch == CPU_R5900)
c0ebe874
RS
11814 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", op[0], op[1],
11815 op[2]);
e407c74b
NC
11816 else
11817 {
c0ebe874
RS
11818 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", op[1], op[2]);
11819 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
e407c74b 11820 }
8fc2e39e 11821 break;
252b5132
RH
11822
11823 case M_DMUL_I:
11824 dbl = 1;
11825 case M_MUL_I:
11826 /* The MIPS assembler some times generates shifts and adds. I'm
11827 not trying to be that fancy. GCC should do this for us
11828 anyway. */
8fc2e39e 11829 used_at = 1;
67c0d1eb 11830 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
11831 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", op[1], AT);
11832 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
11833 break;
11834
11835 case M_DMULO_I:
11836 dbl = 1;
11837 case M_MULO_I:
11838 imm = 1;
11839 goto do_mulo;
11840
11841 case M_DMULO:
11842 dbl = 1;
11843 case M_MULO:
11844 do_mulo:
7d10b47d 11845 start_noreorder ();
8fc2e39e 11846 used_at = 1;
252b5132 11847 if (imm)
67c0d1eb 11848 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
11849 macro_build (NULL, dbl ? "dmult" : "mult", "s,t",
11850 op[1], imm ? AT : op[2]);
11851 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
11852 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, op[0], op[0], 31);
df58fc94 11853 macro_build (NULL, "mfhi", MFHL_FMT, AT);
252b5132 11854 if (mips_trap)
c0ebe874 11855 macro_build (NULL, "tne", TRAP_FMT, op[0], AT, 6);
252b5132
RH
11856 else
11857 {
df58fc94
RS
11858 if (mips_opts.micromips)
11859 micromips_label_expr (&label_expr);
11860 else
11861 label_expr.X_add_number = 8;
c0ebe874 11862 macro_build (&label_expr, "beq", "s,t,p", op[0], AT);
a605d2b3 11863 macro_build (NULL, "nop", "");
df58fc94
RS
11864 macro_build (NULL, "break", BRK_FMT, 6);
11865 if (mips_opts.micromips)
11866 micromips_add_label ();
252b5132 11867 }
7d10b47d 11868 end_noreorder ();
c0ebe874 11869 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
11870 break;
11871
11872 case M_DMULOU_I:
11873 dbl = 1;
11874 case M_MULOU_I:
11875 imm = 1;
11876 goto do_mulou;
11877
11878 case M_DMULOU:
11879 dbl = 1;
11880 case M_MULOU:
11881 do_mulou:
7d10b47d 11882 start_noreorder ();
8fc2e39e 11883 used_at = 1;
252b5132 11884 if (imm)
67c0d1eb
RS
11885 load_register (AT, &imm_expr, dbl);
11886 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
c0ebe874 11887 op[1], imm ? AT : op[2]);
df58fc94 11888 macro_build (NULL, "mfhi", MFHL_FMT, AT);
c0ebe874 11889 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132 11890 if (mips_trap)
df58fc94 11891 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
252b5132
RH
11892 else
11893 {
df58fc94
RS
11894 if (mips_opts.micromips)
11895 micromips_label_expr (&label_expr);
11896 else
11897 label_expr.X_add_number = 8;
11898 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
a605d2b3 11899 macro_build (NULL, "nop", "");
df58fc94
RS
11900 macro_build (NULL, "break", BRK_FMT, 6);
11901 if (mips_opts.micromips)
11902 micromips_add_label ();
252b5132 11903 }
7d10b47d 11904 end_noreorder ();
252b5132
RH
11905 break;
11906
771c7ce4 11907 case M_DROL:
fef14a42 11908 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 11909 {
c0ebe874 11910 if (op[0] == op[1])
82dd0097
CD
11911 {
11912 tempreg = AT;
11913 used_at = 1;
11914 }
11915 else
c0ebe874
RS
11916 tempreg = op[0];
11917 macro_build (NULL, "dnegu", "d,w", tempreg, op[2]);
11918 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 11919 break;
82dd0097 11920 }
8fc2e39e 11921 used_at = 1;
c0ebe874
RS
11922 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
11923 macro_build (NULL, "dsrlv", "d,t,s", AT, op[1], AT);
11924 macro_build (NULL, "dsllv", "d,t,s", op[0], op[1], op[2]);
11925 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
11926 break;
11927
252b5132 11928 case M_ROL:
fef14a42 11929 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 11930 {
c0ebe874 11931 if (op[0] == op[1])
82dd0097
CD
11932 {
11933 tempreg = AT;
11934 used_at = 1;
11935 }
11936 else
c0ebe874
RS
11937 tempreg = op[0];
11938 macro_build (NULL, "negu", "d,w", tempreg, op[2]);
11939 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 11940 break;
82dd0097 11941 }
8fc2e39e 11942 used_at = 1;
c0ebe874
RS
11943 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
11944 macro_build (NULL, "srlv", "d,t,s", AT, op[1], AT);
11945 macro_build (NULL, "sllv", "d,t,s", op[0], op[1], op[2]);
11946 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
11947 break;
11948
771c7ce4
TS
11949 case M_DROL_I:
11950 {
11951 unsigned int rot;
91d6fa6a
NC
11952 char *l;
11953 char *rr;
771c7ce4
TS
11954
11955 if (imm_expr.X_op != O_constant)
82dd0097 11956 as_bad (_("Improper rotate count"));
771c7ce4 11957 rot = imm_expr.X_add_number & 0x3f;
fef14a42 11958 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
11959 {
11960 rot = (64 - rot) & 0x3f;
11961 if (rot >= 32)
c0ebe874 11962 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
60b63b72 11963 else
c0ebe874 11964 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 11965 break;
60b63b72 11966 }
483fc7cd 11967 if (rot == 0)
483fc7cd 11968 {
c0ebe874 11969 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 11970 break;
483fc7cd 11971 }
82dd0097 11972 l = (rot < 0x20) ? "dsll" : "dsll32";
91d6fa6a 11973 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
82dd0097 11974 rot &= 0x1f;
8fc2e39e 11975 used_at = 1;
c0ebe874
RS
11976 macro_build (NULL, l, SHFT_FMT, AT, op[1], rot);
11977 macro_build (NULL, rr, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
11978 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
11979 }
11980 break;
11981
252b5132 11982 case M_ROL_I:
771c7ce4
TS
11983 {
11984 unsigned int rot;
11985
11986 if (imm_expr.X_op != O_constant)
82dd0097 11987 as_bad (_("Improper rotate count"));
771c7ce4 11988 rot = imm_expr.X_add_number & 0x1f;
fef14a42 11989 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 11990 {
c0ebe874
RS
11991 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1],
11992 (32 - rot) & 0x1f);
8fc2e39e 11993 break;
60b63b72 11994 }
483fc7cd 11995 if (rot == 0)
483fc7cd 11996 {
c0ebe874 11997 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 11998 break;
483fc7cd 11999 }
8fc2e39e 12000 used_at = 1;
c0ebe874
RS
12001 macro_build (NULL, "sll", SHFT_FMT, AT, op[1], rot);
12002 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
12003 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
12004 }
12005 break;
12006
12007 case M_DROR:
fef14a42 12008 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 12009 {
c0ebe874 12010 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 12011 break;
82dd0097 12012 }
8fc2e39e 12013 used_at = 1;
c0ebe874
RS
12014 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
12015 macro_build (NULL, "dsllv", "d,t,s", AT, op[1], AT);
12016 macro_build (NULL, "dsrlv", "d,t,s", op[0], op[1], op[2]);
12017 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
12018 break;
12019
12020 case M_ROR:
fef14a42 12021 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 12022 {
c0ebe874 12023 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 12024 break;
82dd0097 12025 }
8fc2e39e 12026 used_at = 1;
c0ebe874
RS
12027 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
12028 macro_build (NULL, "sllv", "d,t,s", AT, op[1], AT);
12029 macro_build (NULL, "srlv", "d,t,s", op[0], op[1], op[2]);
12030 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
12031 break;
12032
771c7ce4
TS
12033 case M_DROR_I:
12034 {
12035 unsigned int rot;
91d6fa6a
NC
12036 char *l;
12037 char *rr;
771c7ce4
TS
12038
12039 if (imm_expr.X_op != O_constant)
82dd0097 12040 as_bad (_("Improper rotate count"));
771c7ce4 12041 rot = imm_expr.X_add_number & 0x3f;
fef14a42 12042 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
12043 {
12044 if (rot >= 32)
c0ebe874 12045 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
82dd0097 12046 else
c0ebe874 12047 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 12048 break;
82dd0097 12049 }
483fc7cd 12050 if (rot == 0)
483fc7cd 12051 {
c0ebe874 12052 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 12053 break;
483fc7cd 12054 }
91d6fa6a 12055 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
82dd0097
CD
12056 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
12057 rot &= 0x1f;
8fc2e39e 12058 used_at = 1;
c0ebe874
RS
12059 macro_build (NULL, rr, SHFT_FMT, AT, op[1], rot);
12060 macro_build (NULL, l, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
12061 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
12062 }
12063 break;
12064
252b5132 12065 case M_ROR_I:
771c7ce4
TS
12066 {
12067 unsigned int rot;
12068
12069 if (imm_expr.X_op != O_constant)
82dd0097 12070 as_bad (_("Improper rotate count"));
771c7ce4 12071 rot = imm_expr.X_add_number & 0x1f;
fef14a42 12072 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 12073 {
c0ebe874 12074 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 12075 break;
82dd0097 12076 }
483fc7cd 12077 if (rot == 0)
483fc7cd 12078 {
c0ebe874 12079 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 12080 break;
483fc7cd 12081 }
8fc2e39e 12082 used_at = 1;
c0ebe874
RS
12083 macro_build (NULL, "srl", SHFT_FMT, AT, op[1], rot);
12084 macro_build (NULL, "sll", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
12085 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4 12086 }
252b5132
RH
12087 break;
12088
252b5132 12089 case M_SEQ:
c0ebe874
RS
12090 if (op[1] == 0)
12091 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[2], BFD_RELOC_LO16);
12092 else if (op[2] == 0)
12093 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
12094 else
12095 {
c0ebe874
RS
12096 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
12097 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
252b5132 12098 }
8fc2e39e 12099 break;
252b5132
RH
12100
12101 case M_SEQ_I:
12102 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
12103 {
c0ebe874 12104 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 12105 break;
252b5132 12106 }
c0ebe874 12107 if (op[1] == 0)
252b5132
RH
12108 {
12109 as_warn (_("Instruction %s: result is always false"),
12110 ip->insn_mo->name);
c0ebe874 12111 move_register (op[0], 0);
8fc2e39e 12112 break;
252b5132 12113 }
dd3cbb7e
NC
12114 if (CPU_HAS_SEQ (mips_opts.arch)
12115 && -512 <= imm_expr.X_add_number
12116 && imm_expr.X_add_number < 512)
12117 {
c0ebe874 12118 macro_build (NULL, "seqi", "t,r,+Q", op[0], op[1],
750bdd57 12119 (int) imm_expr.X_add_number);
dd3cbb7e
NC
12120 break;
12121 }
252b5132
RH
12122 if (imm_expr.X_op == O_constant
12123 && imm_expr.X_add_number >= 0
12124 && imm_expr.X_add_number < 0x10000)
c0ebe874 12125 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
12126 else if (imm_expr.X_op == O_constant
12127 && imm_expr.X_add_number > -0x8000
12128 && imm_expr.X_add_number < 0)
12129 {
12130 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 12131 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
c0ebe874 12132 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 12133 }
dd3cbb7e
NC
12134 else if (CPU_HAS_SEQ (mips_opts.arch))
12135 {
12136 used_at = 1;
12137 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 12138 macro_build (NULL, "seq", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
12139 break;
12140 }
252b5132
RH
12141 else
12142 {
67c0d1eb 12143 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 12144 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
12145 used_at = 1;
12146 }
c0ebe874 12147 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 12148 break;
252b5132 12149
c0ebe874 12150 case M_SGE: /* X >= Y <==> not (X < Y) */
252b5132
RH
12151 s = "slt";
12152 goto sge;
12153 case M_SGEU:
12154 s = "sltu";
12155 sge:
c0ebe874
RS
12156 macro_build (NULL, s, "d,v,t", op[0], op[1], op[2]);
12157 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 12158 break;
252b5132 12159
c0ebe874 12160 case M_SGE_I: /* X >= I <==> not (X < I) */
252b5132
RH
12161 case M_SGEU_I:
12162 if (imm_expr.X_op == O_constant
12163 && imm_expr.X_add_number >= -0x8000
12164 && imm_expr.X_add_number < 0x8000)
c0ebe874
RS
12165 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
12166 op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
12167 else
12168 {
67c0d1eb
RS
12169 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
12170 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
c0ebe874 12171 op[0], op[1], AT);
252b5132
RH
12172 used_at = 1;
12173 }
c0ebe874 12174 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 12175 break;
252b5132 12176
c0ebe874 12177 case M_SGT: /* X > Y <==> Y < X */
252b5132
RH
12178 s = "slt";
12179 goto sgt;
12180 case M_SGTU:
12181 s = "sltu";
12182 sgt:
c0ebe874 12183 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
8fc2e39e 12184 break;
252b5132 12185
c0ebe874 12186 case M_SGT_I: /* X > I <==> I < X */
252b5132
RH
12187 s = "slt";
12188 goto sgti;
12189 case M_SGTU_I:
12190 s = "sltu";
12191 sgti:
8fc2e39e 12192 used_at = 1;
67c0d1eb 12193 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 12194 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
252b5132
RH
12195 break;
12196
c0ebe874 12197 case M_SLE: /* X <= Y <==> Y >= X <==> not (Y < X) */
252b5132
RH
12198 s = "slt";
12199 goto sle;
12200 case M_SLEU:
12201 s = "sltu";
12202 sle:
c0ebe874
RS
12203 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
12204 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 12205 break;
252b5132 12206
c0ebe874 12207 case M_SLE_I: /* X <= I <==> I >= X <==> not (I < X) */
252b5132
RH
12208 s = "slt";
12209 goto slei;
12210 case M_SLEU_I:
12211 s = "sltu";
12212 slei:
8fc2e39e 12213 used_at = 1;
67c0d1eb 12214 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874
RS
12215 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
12216 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
252b5132
RH
12217 break;
12218
12219 case M_SLT_I:
12220 if (imm_expr.X_op == O_constant
12221 && imm_expr.X_add_number >= -0x8000
12222 && imm_expr.X_add_number < 0x8000)
12223 {
c0ebe874
RS
12224 macro_build (&imm_expr, "slti", "t,r,j", op[0], op[1],
12225 BFD_RELOC_LO16);
8fc2e39e 12226 break;
252b5132 12227 }
8fc2e39e 12228 used_at = 1;
67c0d1eb 12229 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 12230 macro_build (NULL, "slt", "d,v,t", op[0], op[1], AT);
252b5132
RH
12231 break;
12232
12233 case M_SLTU_I:
12234 if (imm_expr.X_op == O_constant
12235 && imm_expr.X_add_number >= -0x8000
12236 && imm_expr.X_add_number < 0x8000)
12237 {
c0ebe874 12238 macro_build (&imm_expr, "sltiu", "t,r,j", op[0], op[1],
17a2f251 12239 BFD_RELOC_LO16);
8fc2e39e 12240 break;
252b5132 12241 }
8fc2e39e 12242 used_at = 1;
67c0d1eb 12243 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 12244 macro_build (NULL, "sltu", "d,v,t", op[0], op[1], AT);
252b5132
RH
12245 break;
12246
12247 case M_SNE:
c0ebe874
RS
12248 if (op[1] == 0)
12249 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[2]);
12250 else if (op[2] == 0)
12251 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
252b5132
RH
12252 else
12253 {
c0ebe874
RS
12254 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
12255 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
252b5132 12256 }
8fc2e39e 12257 break;
252b5132
RH
12258
12259 case M_SNE_I:
12260 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
12261 {
c0ebe874 12262 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
8fc2e39e 12263 break;
252b5132 12264 }
c0ebe874 12265 if (op[1] == 0)
252b5132
RH
12266 {
12267 as_warn (_("Instruction %s: result is always true"),
12268 ip->insn_mo->name);
67c0d1eb 12269 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
c0ebe874 12270 op[0], 0, BFD_RELOC_LO16);
8fc2e39e 12271 break;
252b5132 12272 }
dd3cbb7e
NC
12273 if (CPU_HAS_SEQ (mips_opts.arch)
12274 && -512 <= imm_expr.X_add_number
12275 && imm_expr.X_add_number < 512)
12276 {
c0ebe874 12277 macro_build (NULL, "snei", "t,r,+Q", op[0], op[1],
750bdd57 12278 (int) imm_expr.X_add_number);
dd3cbb7e
NC
12279 break;
12280 }
252b5132
RH
12281 if (imm_expr.X_op == O_constant
12282 && imm_expr.X_add_number >= 0
12283 && imm_expr.X_add_number < 0x10000)
12284 {
c0ebe874
RS
12285 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1],
12286 BFD_RELOC_LO16);
252b5132
RH
12287 }
12288 else if (imm_expr.X_op == O_constant
12289 && imm_expr.X_add_number > -0x8000
12290 && imm_expr.X_add_number < 0)
12291 {
12292 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 12293 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
c0ebe874 12294 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 12295 }
dd3cbb7e
NC
12296 else if (CPU_HAS_SEQ (mips_opts.arch))
12297 {
12298 used_at = 1;
12299 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 12300 macro_build (NULL, "sne", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
12301 break;
12302 }
252b5132
RH
12303 else
12304 {
67c0d1eb 12305 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 12306 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
12307 used_at = 1;
12308 }
c0ebe874 12309 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
8fc2e39e 12310 break;
252b5132 12311
df58fc94
RS
12312 case M_SUB_I:
12313 s = "addi";
12314 s2 = "sub";
12315 goto do_subi;
12316 case M_SUBU_I:
12317 s = "addiu";
12318 s2 = "subu";
12319 goto do_subi;
252b5132
RH
12320 case M_DSUB_I:
12321 dbl = 1;
df58fc94
RS
12322 s = "daddi";
12323 s2 = "dsub";
12324 if (!mips_opts.micromips)
12325 goto do_subi;
252b5132 12326 if (imm_expr.X_op == O_constant
df58fc94
RS
12327 && imm_expr.X_add_number > -0x200
12328 && imm_expr.X_add_number <= 0x200)
252b5132 12329 {
c0ebe874 12330 macro_build (NULL, s, "t,r,.", op[0], op[1], -imm_expr.X_add_number);
8fc2e39e 12331 break;
252b5132 12332 }
df58fc94 12333 goto do_subi_i;
252b5132
RH
12334 case M_DSUBU_I:
12335 dbl = 1;
df58fc94
RS
12336 s = "daddiu";
12337 s2 = "dsubu";
12338 do_subi:
252b5132
RH
12339 if (imm_expr.X_op == O_constant
12340 && imm_expr.X_add_number > -0x8000
12341 && imm_expr.X_add_number <= 0x8000)
12342 {
12343 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 12344 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 12345 break;
252b5132 12346 }
df58fc94 12347 do_subi_i:
8fc2e39e 12348 used_at = 1;
67c0d1eb 12349 load_register (AT, &imm_expr, dbl);
c0ebe874 12350 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
12351 break;
12352
12353 case M_TEQ_I:
12354 s = "teq";
12355 goto trap;
12356 case M_TGE_I:
12357 s = "tge";
12358 goto trap;
12359 case M_TGEU_I:
12360 s = "tgeu";
12361 goto trap;
12362 case M_TLT_I:
12363 s = "tlt";
12364 goto trap;
12365 case M_TLTU_I:
12366 s = "tltu";
12367 goto trap;
12368 case M_TNE_I:
12369 s = "tne";
12370 trap:
8fc2e39e 12371 used_at = 1;
67c0d1eb 12372 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 12373 macro_build (NULL, s, "s,t", op[0], AT);
252b5132
RH
12374 break;
12375
252b5132 12376 case M_TRUNCWS:
43841e91 12377 case M_TRUNCWD:
df58fc94 12378 gas_assert (!mips_opts.micromips);
0aa27725 12379 gas_assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 12380 used_at = 1;
252b5132
RH
12381
12382 /*
12383 * Is the double cfc1 instruction a bug in the mips assembler;
12384 * or is there a reason for it?
12385 */
7d10b47d 12386 start_noreorder ();
c0ebe874
RS
12387 macro_build (NULL, "cfc1", "t,G", op[2], RA);
12388 macro_build (NULL, "cfc1", "t,G", op[2], RA);
67c0d1eb 12389 macro_build (NULL, "nop", "");
252b5132 12390 expr1.X_add_number = 3;
c0ebe874 12391 macro_build (&expr1, "ori", "t,r,i", AT, op[2], BFD_RELOC_LO16);
252b5132 12392 expr1.X_add_number = 2;
67c0d1eb
RS
12393 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
12394 macro_build (NULL, "ctc1", "t,G", AT, RA);
12395 macro_build (NULL, "nop", "");
12396 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
c0ebe874
RS
12397 op[0], op[1]);
12398 macro_build (NULL, "ctc1", "t,G", op[2], RA);
67c0d1eb 12399 macro_build (NULL, "nop", "");
7d10b47d 12400 end_noreorder ();
252b5132
RH
12401 break;
12402
f2ae14a1 12403 case M_ULH_AB:
252b5132 12404 s = "lb";
df58fc94
RS
12405 s2 = "lbu";
12406 off = 1;
12407 goto uld_st;
f2ae14a1 12408 case M_ULHU_AB:
252b5132 12409 s = "lbu";
df58fc94
RS
12410 s2 = "lbu";
12411 off = 1;
12412 goto uld_st;
f2ae14a1 12413 case M_ULW_AB:
df58fc94
RS
12414 s = "lwl";
12415 s2 = "lwr";
7f3c4072 12416 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
12417 off = 3;
12418 goto uld_st;
f2ae14a1 12419 case M_ULD_AB:
252b5132
RH
12420 s = "ldl";
12421 s2 = "ldr";
7f3c4072 12422 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 12423 off = 7;
df58fc94 12424 goto uld_st;
f2ae14a1 12425 case M_USH_AB:
df58fc94
RS
12426 s = "sb";
12427 s2 = "sb";
12428 off = 1;
12429 ust = 1;
12430 goto uld_st;
f2ae14a1 12431 case M_USW_AB:
df58fc94
RS
12432 s = "swl";
12433 s2 = "swr";
7f3c4072 12434 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 12435 off = 3;
df58fc94
RS
12436 ust = 1;
12437 goto uld_st;
f2ae14a1 12438 case M_USD_AB:
df58fc94
RS
12439 s = "sdl";
12440 s2 = "sdr";
7f3c4072 12441 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
12442 off = 7;
12443 ust = 1;
12444
12445 uld_st:
c0ebe874 12446 breg = op[2];
f2ae14a1 12447 large_offset = !small_offset_p (off, align, offbits);
df58fc94
RS
12448 ep = &offset_expr;
12449 expr1.X_add_number = 0;
f2ae14a1 12450 if (large_offset)
df58fc94
RS
12451 {
12452 used_at = 1;
12453 tempreg = AT;
f2ae14a1
RS
12454 if (small_offset_p (0, align, 16))
12455 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg, -1,
12456 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
12457 else
12458 {
12459 load_address (tempreg, ep, &used_at);
12460 if (breg != 0)
12461 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12462 tempreg, tempreg, breg);
12463 }
12464 offset_reloc[0] = BFD_RELOC_LO16;
12465 offset_reloc[1] = BFD_RELOC_UNUSED;
12466 offset_reloc[2] = BFD_RELOC_UNUSED;
df58fc94 12467 breg = tempreg;
c0ebe874 12468 tempreg = op[0];
df58fc94
RS
12469 ep = &expr1;
12470 }
c0ebe874 12471 else if (!ust && op[0] == breg)
8fc2e39e
TS
12472 {
12473 used_at = 1;
12474 tempreg = AT;
12475 }
252b5132 12476 else
c0ebe874 12477 tempreg = op[0];
af22f5b2 12478
df58fc94
RS
12479 if (off == 1)
12480 goto ulh_sh;
252b5132 12481
90ecf173 12482 if (!target_big_endian)
df58fc94 12483 ep->X_add_number += off;
f2ae14a1 12484 if (offbits == 12)
c8276761 12485 macro_build (NULL, s, "t,~(b)", tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
12486 else
12487 macro_build (ep, s, "t,o(b)", tempreg, -1,
12488 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94 12489
90ecf173 12490 if (!target_big_endian)
df58fc94 12491 ep->X_add_number -= off;
252b5132 12492 else
df58fc94 12493 ep->X_add_number += off;
f2ae14a1 12494 if (offbits == 12)
df58fc94 12495 macro_build (NULL, s2, "t,~(b)",
c8276761 12496 tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
12497 else
12498 macro_build (ep, s2, "t,o(b)", tempreg, -1,
12499 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 12500
df58fc94 12501 /* If necessary, move the result in tempreg to the final destination. */
c0ebe874 12502 if (!ust && op[0] != tempreg)
df58fc94
RS
12503 {
12504 /* Protect second load's delay slot. */
12505 load_delay_nop ();
c0ebe874 12506 move_register (op[0], tempreg);
df58fc94 12507 }
8fc2e39e 12508 break;
252b5132 12509
df58fc94 12510 ulh_sh:
d6bc6245 12511 used_at = 1;
df58fc94
RS
12512 if (target_big_endian == ust)
12513 ep->X_add_number += off;
c0ebe874 12514 tempreg = ust || large_offset ? op[0] : AT;
f2ae14a1
RS
12515 macro_build (ep, s, "t,o(b)", tempreg, -1,
12516 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94
RS
12517
12518 /* For halfword transfers we need a temporary register to shuffle
12519 bytes. Unfortunately for M_USH_A we have none available before
12520 the next store as AT holds the base address. We deal with this
12521 case by clobbering TREG and then restoring it as with ULH. */
c0ebe874 12522 tempreg = ust == large_offset ? op[0] : AT;
df58fc94 12523 if (ust)
c0ebe874 12524 macro_build (NULL, "srl", SHFT_FMT, tempreg, op[0], 8);
df58fc94
RS
12525
12526 if (target_big_endian == ust)
12527 ep->X_add_number -= off;
252b5132 12528 else
df58fc94 12529 ep->X_add_number += off;
f2ae14a1
RS
12530 macro_build (ep, s2, "t,o(b)", tempreg, -1,
12531 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 12532
df58fc94 12533 /* For M_USH_A re-retrieve the LSB. */
f2ae14a1 12534 if (ust && large_offset)
df58fc94
RS
12535 {
12536 if (target_big_endian)
12537 ep->X_add_number += off;
12538 else
12539 ep->X_add_number -= off;
f2ae14a1
RS
12540 macro_build (&expr1, "lbu", "t,o(b)", AT, -1,
12541 offset_reloc[0], offset_reloc[1], offset_reloc[2], AT);
df58fc94
RS
12542 }
12543 /* For ULH and M_USH_A OR the LSB in. */
f2ae14a1 12544 if (!ust || large_offset)
df58fc94 12545 {
c0ebe874 12546 tempreg = !large_offset ? AT : op[0];
df58fc94 12547 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
c0ebe874 12548 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
df58fc94 12549 }
252b5132
RH
12550 break;
12551
12552 default:
12553 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 12554 are added dynamically. */
252b5132
RH
12555 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
12556 break;
12557 }
741fe287 12558 if (!mips_opts.at && used_at)
8fc2e39e 12559 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
12560}
12561
12562/* Implement macros in mips16 mode. */
12563
12564static void
17a2f251 12565mips16_macro (struct mips_cl_insn *ip)
252b5132 12566{
c0ebe874 12567 const struct mips_operand_array *operands;
252b5132 12568 int mask;
c0ebe874 12569 int tmp;
252b5132
RH
12570 expressionS expr1;
12571 int dbl;
12572 const char *s, *s2, *s3;
c0ebe874
RS
12573 unsigned int op[MAX_OPERANDS];
12574 unsigned int i;
252b5132
RH
12575
12576 mask = ip->insn_mo->mask;
12577
c0ebe874
RS
12578 operands = insn_operands (ip);
12579 for (i = 0; i < MAX_OPERANDS; i++)
12580 if (operands->operand[i])
12581 op[i] = insn_extract_operand (ip, operands->operand[i]);
12582 else
12583 op[i] = -1;
252b5132 12584
252b5132
RH
12585 expr1.X_op = O_constant;
12586 expr1.X_op_symbol = NULL;
12587 expr1.X_add_symbol = NULL;
12588 expr1.X_add_number = 1;
12589
12590 dbl = 0;
12591
12592 switch (mask)
12593 {
12594 default:
b37df7c4 12595 abort ();
252b5132
RH
12596
12597 case M_DDIV_3:
12598 dbl = 1;
12599 case M_DIV_3:
12600 s = "mflo";
12601 goto do_div3;
12602 case M_DREM_3:
12603 dbl = 1;
12604 case M_REM_3:
12605 s = "mfhi";
12606 do_div3:
7d10b47d 12607 start_noreorder ();
c0ebe874 12608 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", op[1], op[2]);
252b5132 12609 expr1.X_add_number = 2;
c0ebe874 12610 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 12611 macro_build (NULL, "break", "6", 7);
bdaaa2e1 12612
252b5132
RH
12613 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
12614 since that causes an overflow. We should do that as well,
12615 but I don't see how to do the comparisons without a temporary
12616 register. */
7d10b47d 12617 end_noreorder ();
c0ebe874 12618 macro_build (NULL, s, "x", op[0]);
252b5132
RH
12619 break;
12620
12621 case M_DIVU_3:
12622 s = "divu";
12623 s2 = "mflo";
12624 goto do_divu3;
12625 case M_REMU_3:
12626 s = "divu";
12627 s2 = "mfhi";
12628 goto do_divu3;
12629 case M_DDIVU_3:
12630 s = "ddivu";
12631 s2 = "mflo";
12632 goto do_divu3;
12633 case M_DREMU_3:
12634 s = "ddivu";
12635 s2 = "mfhi";
12636 do_divu3:
7d10b47d 12637 start_noreorder ();
c0ebe874 12638 macro_build (NULL, s, "0,x,y", op[1], op[2]);
252b5132 12639 expr1.X_add_number = 2;
c0ebe874 12640 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 12641 macro_build (NULL, "break", "6", 7);
7d10b47d 12642 end_noreorder ();
c0ebe874 12643 macro_build (NULL, s2, "x", op[0]);
252b5132
RH
12644 break;
12645
12646 case M_DMUL:
12647 dbl = 1;
12648 case M_MUL:
c0ebe874
RS
12649 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", op[1], op[2]);
12650 macro_build (NULL, "mflo", "x", op[0]);
8fc2e39e 12651 break;
252b5132
RH
12652
12653 case M_DSUBU_I:
12654 dbl = 1;
12655 goto do_subu;
12656 case M_SUBU_I:
12657 do_subu:
12658 if (imm_expr.X_op != O_constant)
12659 as_bad (_("Unsupported large constant"));
12660 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 12661 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", op[0], op[1]);
252b5132
RH
12662 break;
12663
12664 case M_SUBU_I_2:
12665 if (imm_expr.X_op != O_constant)
12666 as_bad (_("Unsupported large constant"));
12667 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 12668 macro_build (&imm_expr, "addiu", "x,k", op[0]);
252b5132
RH
12669 break;
12670
12671 case M_DSUBU_I_2:
12672 if (imm_expr.X_op != O_constant)
12673 as_bad (_("Unsupported large constant"));
12674 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 12675 macro_build (&imm_expr, "daddiu", "y,j", op[0]);
252b5132
RH
12676 break;
12677
12678 case M_BEQ:
12679 s = "cmp";
12680 s2 = "bteqz";
12681 goto do_branch;
12682 case M_BNE:
12683 s = "cmp";
12684 s2 = "btnez";
12685 goto do_branch;
12686 case M_BLT:
12687 s = "slt";
12688 s2 = "btnez";
12689 goto do_branch;
12690 case M_BLTU:
12691 s = "sltu";
12692 s2 = "btnez";
12693 goto do_branch;
12694 case M_BLE:
12695 s = "slt";
12696 s2 = "bteqz";
12697 goto do_reverse_branch;
12698 case M_BLEU:
12699 s = "sltu";
12700 s2 = "bteqz";
12701 goto do_reverse_branch;
12702 case M_BGE:
12703 s = "slt";
12704 s2 = "bteqz";
12705 goto do_branch;
12706 case M_BGEU:
12707 s = "sltu";
12708 s2 = "bteqz";
12709 goto do_branch;
12710 case M_BGT:
12711 s = "slt";
12712 s2 = "btnez";
12713 goto do_reverse_branch;
12714 case M_BGTU:
12715 s = "sltu";
12716 s2 = "btnez";
12717
12718 do_reverse_branch:
c0ebe874
RS
12719 tmp = op[1];
12720 op[1] = op[0];
12721 op[0] = tmp;
252b5132
RH
12722
12723 do_branch:
c0ebe874 12724 macro_build (NULL, s, "x,y", op[0], op[1]);
67c0d1eb 12725 macro_build (&offset_expr, s2, "p");
252b5132
RH
12726 break;
12727
12728 case M_BEQ_I:
12729 s = "cmpi";
12730 s2 = "bteqz";
12731 s3 = "x,U";
12732 goto do_branch_i;
12733 case M_BNE_I:
12734 s = "cmpi";
12735 s2 = "btnez";
12736 s3 = "x,U";
12737 goto do_branch_i;
12738 case M_BLT_I:
12739 s = "slti";
12740 s2 = "btnez";
12741 s3 = "x,8";
12742 goto do_branch_i;
12743 case M_BLTU_I:
12744 s = "sltiu";
12745 s2 = "btnez";
12746 s3 = "x,8";
12747 goto do_branch_i;
12748 case M_BLE_I:
12749 s = "slti";
12750 s2 = "btnez";
12751 s3 = "x,8";
12752 goto do_addone_branch_i;
12753 case M_BLEU_I:
12754 s = "sltiu";
12755 s2 = "btnez";
12756 s3 = "x,8";
12757 goto do_addone_branch_i;
12758 case M_BGE_I:
12759 s = "slti";
12760 s2 = "bteqz";
12761 s3 = "x,8";
12762 goto do_branch_i;
12763 case M_BGEU_I:
12764 s = "sltiu";
12765 s2 = "bteqz";
12766 s3 = "x,8";
12767 goto do_branch_i;
12768 case M_BGT_I:
12769 s = "slti";
12770 s2 = "bteqz";
12771 s3 = "x,8";
12772 goto do_addone_branch_i;
12773 case M_BGTU_I:
12774 s = "sltiu";
12775 s2 = "bteqz";
12776 s3 = "x,8";
12777
12778 do_addone_branch_i:
12779 if (imm_expr.X_op != O_constant)
12780 as_bad (_("Unsupported large constant"));
12781 ++imm_expr.X_add_number;
12782
12783 do_branch_i:
c0ebe874 12784 macro_build (&imm_expr, s, s3, op[0]);
67c0d1eb 12785 macro_build (&offset_expr, s2, "p");
252b5132
RH
12786 break;
12787
12788 case M_ABS:
12789 expr1.X_add_number = 0;
c0ebe874
RS
12790 macro_build (&expr1, "slti", "x,8", op[1]);
12791 if (op[0] != op[1])
12792 macro_build (NULL, "move", "y,X", op[0], mips16_to_32_reg_map[op[1]]);
252b5132 12793 expr1.X_add_number = 2;
67c0d1eb 12794 macro_build (&expr1, "bteqz", "p");
c0ebe874 12795 macro_build (NULL, "neg", "x,w", op[0], op[0]);
0acfaea6 12796 break;
252b5132
RH
12797 }
12798}
12799
14daeee3
RS
12800/* Look up instruction [START, START + LENGTH) in HASH. Record any extra
12801 opcode bits in *OPCODE_EXTRA. */
12802
12803static struct mips_opcode *
12804mips_lookup_insn (struct hash_control *hash, const char *start,
da8bca91 12805 ssize_t length, unsigned int *opcode_extra)
14daeee3
RS
12806{
12807 char *name, *dot, *p;
12808 unsigned int mask, suffix;
da8bca91 12809 ssize_t opend;
14daeee3
RS
12810 struct mips_opcode *insn;
12811
12812 /* Make a copy of the instruction so that we can fiddle with it. */
12813 name = alloca (length + 1);
12814 memcpy (name, start, length);
12815 name[length] = '\0';
12816
12817 /* Look up the instruction as-is. */
12818 insn = (struct mips_opcode *) hash_find (hash, name);
ee5734f0 12819 if (insn)
14daeee3
RS
12820 return insn;
12821
12822 dot = strchr (name, '.');
12823 if (dot && dot[1])
12824 {
12825 /* Try to interpret the text after the dot as a VU0 channel suffix. */
12826 p = mips_parse_vu0_channels (dot + 1, &mask);
12827 if (*p == 0 && mask != 0)
12828 {
12829 *dot = 0;
12830 insn = (struct mips_opcode *) hash_find (hash, name);
12831 *dot = '.';
12832 if (insn && (insn->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0)
12833 {
12834 *opcode_extra |= mask << mips_vu0_channel_mask.lsb;
12835 return insn;
12836 }
12837 }
12838 }
12839
12840 if (mips_opts.micromips)
12841 {
12842 /* See if there's an instruction size override suffix,
12843 either `16' or `32', at the end of the mnemonic proper,
12844 that defines the operation, i.e. before the first `.'
12845 character if any. Strip it and retry. */
12846 opend = dot != NULL ? dot - name : length;
12847 if (opend >= 3 && name[opend - 2] == '1' && name[opend - 1] == '6')
12848 suffix = 2;
12849 else if (name[opend - 2] == '3' && name[opend - 1] == '2')
12850 suffix = 4;
12851 else
12852 suffix = 0;
12853 if (suffix)
12854 {
12855 memcpy (name + opend - 2, name + opend, length - opend + 1);
12856 insn = (struct mips_opcode *) hash_find (hash, name);
ee5734f0 12857 if (insn)
14daeee3
RS
12858 {
12859 forced_insn_length = suffix;
12860 return insn;
12861 }
12862 }
12863 }
12864
12865 return NULL;
12866}
12867
77bd4346
RS
12868/* Assemble an instruction into its binary format. If the instruction
12869 is a macro, set imm_expr, imm2_expr and offset_expr to the values
12870 associated with "I", "+I" and "A" operands respectively. Otherwise
12871 store the value of the relocatable field (if any) in offset_expr.
12872 In both cases set offset_reloc to the relocation operators applied
12873 to offset_expr. */
252b5132
RH
12874
12875static void
17a2f251 12876mips_ip (char *str, struct mips_cl_insn *ip)
252b5132 12877{
df58fc94
RS
12878 bfd_boolean wrong_delay_slot_insns = FALSE;
12879 bfd_boolean need_delay_slot_ok = TRUE;
12880 struct mips_opcode *firstinsn = NULL;
12881 const struct mips_opcode *past;
12882 struct hash_control *hash;
14daeee3 12883 struct mips_opcode *first, *insn;
a92713e6 12884 char format;
14daeee3 12885 size_t end;
a92713e6 12886 struct mips_operand_token *tokens;
14daeee3 12887 unsigned int opcode_extra;
252b5132
RH
12888
12889 insn_error = NULL;
12890
df58fc94
RS
12891 if (mips_opts.micromips)
12892 {
12893 hash = micromips_op_hash;
12894 past = &micromips_opcodes[bfd_micromips_num_opcodes];
12895 }
12896 else
12897 {
12898 hash = op_hash;
12899 past = &mips_opcodes[NUMOPCODES];
12900 }
12901 forced_insn_length = 0;
252b5132 12902 insn = NULL;
14daeee3 12903 opcode_extra = 0;
252b5132 12904
df58fc94 12905 /* We first try to match an instruction up to a space or to the end. */
a40bc9dd
RS
12906 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
12907 continue;
bdaaa2e1 12908
14daeee3 12909 first = insn = mips_lookup_insn (hash, str, end, &opcode_extra);
252b5132
RH
12910 if (insn == NULL)
12911 {
a40bc9dd
RS
12912 insn_error = _("Unrecognized opcode");
12913 return;
252b5132 12914 }
ee5734f0
RS
12915 /* When no opcode suffix is specified, assume ".xyzw". */
12916 if ((insn->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0 && opcode_extra == 0)
12917 opcode_extra = 0xf << mips_vu0_channel_mask.lsb;
252b5132 12918
14daeee3 12919 if (strcmp (insn->name, "li.s") == 0)
a92713e6 12920 format = 'f';
14daeee3 12921 else if (strcmp (insn->name, "li.d") == 0)
a92713e6
RS
12922 format = 'd';
12923 else
12924 format = 0;
12925 tokens = mips_parse_arguments (str + end, format);
12926 if (!tokens)
12927 return;
12928
df58fc94
RS
12929 /* For microMIPS instructions placed in a fixed-length branch delay slot
12930 we make up to two passes over the relevant fragment of the opcode
12931 table. First we try instructions that meet the delay slot's length
12932 requirement. If none matched, then we retry with the remaining ones
12933 and if one matches, then we use it and then issue an appropriate
12934 warning later on. */
252b5132
RH
12935 for (;;)
12936 {
df58fc94
RS
12937 bfd_boolean delay_slot_ok;
12938 bfd_boolean size_ok;
b34976b6 12939 bfd_boolean ok;
9e12b7a2 12940 bfd_boolean more_alts;
252b5132 12941
14daeee3 12942 gas_assert (strcmp (insn->name, first->name) == 0);
252b5132 12943
f79e2745 12944 ok = is_opcode_valid (insn);
df58fc94
RS
12945 size_ok = is_size_valid (insn);
12946 delay_slot_ok = is_delay_slot_valid (insn);
12947 if (!delay_slot_ok && !wrong_delay_slot_insns)
252b5132 12948 {
df58fc94
RS
12949 firstinsn = insn;
12950 wrong_delay_slot_insns = TRUE;
12951 }
9e12b7a2
RS
12952 more_alts = (insn + 1 < past
12953 && strcmp (insn[0].name, insn[1].name) == 0);
df58fc94
RS
12954 if (!ok || !size_ok || delay_slot_ok != need_delay_slot_ok)
12955 {
12956 static char buf[256];
12957
9e12b7a2 12958 if (more_alts)
252b5132
RH
12959 {
12960 ++insn;
12961 continue;
12962 }
df58fc94 12963 if (wrong_delay_slot_insns && need_delay_slot_ok)
beae10d5 12964 {
df58fc94
RS
12965 gas_assert (firstinsn);
12966 need_delay_slot_ok = FALSE;
12967 past = insn + 1;
12968 insn = firstinsn;
12969 continue;
252b5132 12970 }
df58fc94 12971
a92713e6 12972 obstack_free (&mips_operand_tokens, tokens);
df58fc94
RS
12973 if (insn_error)
12974 return;
12975
12976 if (!ok)
7bd942df 12977 sprintf (buf, _("Opcode not supported on this processor: %s (%s)"),
df58fc94
RS
12978 mips_cpu_info_from_arch (mips_opts.arch)->name,
12979 mips_cpu_info_from_isa (mips_opts.isa)->name);
833794fc
MR
12980 else if (mips_opts.insn32)
12981 sprintf (buf, _("Opcode not supported in the `insn32' mode"));
df58fc94
RS
12982 else
12983 sprintf (buf, _("Unrecognized %u-bit version of microMIPS opcode"),
12984 8 * forced_insn_length);
12985 insn_error = buf;
12986
12987 return;
252b5132
RH
12988 }
12989
97d87491
RS
12990 if (match_insn (ip, insn, tokens, opcode_extra, more_alts,
12991 more_alts || (wrong_delay_slot_insns
12992 && need_delay_slot_ok)))
12993 {
12994 obstack_free (&mips_operand_tokens, tokens);
12995 return;
252b5132 12996 }
97d87491 12997
252b5132 12998 /* Args don't match. */
df58fc94 12999 insn_error = _("Illegal operands");
9e12b7a2 13000 if (more_alts)
252b5132
RH
13001 {
13002 ++insn;
252b5132
RH
13003 continue;
13004 }
9e12b7a2 13005 if (wrong_delay_slot_insns && need_delay_slot_ok)
df58fc94
RS
13006 {
13007 gas_assert (firstinsn);
13008 need_delay_slot_ok = FALSE;
13009 past = insn + 1;
13010 insn = firstinsn;
13011 continue;
13012 }
a92713e6 13013 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
13014 return;
13015 }
13016}
13017
77bd4346
RS
13018/* As for mips_ip, but used when assembling MIPS16 code.
13019 Also set forced_insn_length to the resulting instruction size in
13020 bytes if the user explicitly requested a small or extended instruction. */
252b5132
RH
13021
13022static void
17a2f251 13023mips16_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
13024{
13025 char *s;
252b5132 13026 struct mips_opcode *insn;
a92713e6 13027 struct mips_operand_token *tokens;
252b5132
RH
13028
13029 insn_error = NULL;
13030
df58fc94 13031 forced_insn_length = 0;
252b5132 13032
3882b010 13033 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
13034 ;
13035 switch (*s)
13036 {
13037 case '\0':
13038 break;
13039
13040 case ' ':
13041 *s++ = '\0';
13042 break;
13043
13044 case '.':
13045 if (s[1] == 't' && s[2] == ' ')
13046 {
13047 *s = '\0';
df58fc94 13048 forced_insn_length = 2;
252b5132
RH
13049 s += 3;
13050 break;
13051 }
13052 else if (s[1] == 'e' && s[2] == ' ')
13053 {
13054 *s = '\0';
df58fc94 13055 forced_insn_length = 4;
252b5132
RH
13056 s += 3;
13057 break;
13058 }
13059 /* Fall through. */
13060 default:
13061 insn_error = _("unknown opcode");
13062 return;
13063 }
13064
df58fc94
RS
13065 if (mips_opts.noautoextend && !forced_insn_length)
13066 forced_insn_length = 2;
252b5132
RH
13067
13068 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
13069 {
13070 insn_error = _("unrecognized opcode");
13071 return;
13072 }
13073
a92713e6
RS
13074 tokens = mips_parse_arguments (s, 0);
13075 if (!tokens)
13076 return;
13077
252b5132
RH
13078 for (;;)
13079 {
9b3f89ee 13080 bfd_boolean ok;
364215c8 13081 bfd_boolean more_alts;
9b3f89ee 13082
9c2799c2 13083 gas_assert (strcmp (insn->name, str) == 0);
252b5132 13084
037b32b9 13085 ok = is_opcode_valid_16 (insn);
364215c8
RS
13086 more_alts = (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes]
13087 && strcmp (insn[0].name, insn[1].name) == 0);
9b3f89ee
TS
13088 if (! ok)
13089 {
364215c8 13090 if (more_alts)
9b3f89ee
TS
13091 {
13092 ++insn;
13093 continue;
13094 }
13095 else
13096 {
13097 if (!insn_error)
13098 {
13099 static char buf[100];
13100 sprintf (buf,
7bd942df 13101 _("Opcode not supported on this processor: %s (%s)"),
9b3f89ee
TS
13102 mips_cpu_info_from_arch (mips_opts.arch)->name,
13103 mips_cpu_info_from_isa (mips_opts.isa)->name);
13104 insn_error = buf;
13105 }
a92713e6 13106 obstack_free (&mips_operand_tokens, tokens);
9b3f89ee
TS
13107 return;
13108 }
13109 }
13110
97d87491
RS
13111 if (match_mips16_insn (ip, insn, tokens, more_alts))
13112 {
13113 obstack_free (&mips_operand_tokens, tokens);
13114 return;
252b5132
RH
13115 }
13116
13117 /* Args don't match. */
364215c8 13118 if (more_alts)
252b5132
RH
13119 {
13120 ++insn;
252b5132
RH
13121 continue;
13122 }
13123
13124 insn_error = _("illegal operands");
13125
a92713e6 13126 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
13127 return;
13128 }
13129}
13130
b886a2ab
RS
13131/* Marshal immediate value VAL for an extended MIPS16 instruction.
13132 NBITS is the number of significant bits in VAL. */
13133
13134static unsigned long
13135mips16_immed_extend (offsetT val, unsigned int nbits)
13136{
13137 int extval;
13138 if (nbits == 16)
13139 {
13140 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
13141 val &= 0x1f;
13142 }
13143 else if (nbits == 15)
13144 {
13145 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
13146 val &= 0xf;
13147 }
13148 else
13149 {
13150 extval = ((val & 0x1f) << 6) | (val & 0x20);
13151 val = 0;
13152 }
13153 return (extval << 16) | val;
13154}
13155
3ccad066
RS
13156/* Like decode_mips16_operand, but require the operand to be defined and
13157 require it to be an integer. */
13158
13159static const struct mips_int_operand *
13160mips16_immed_operand (int type, bfd_boolean extended_p)
13161{
13162 const struct mips_operand *operand;
13163
13164 operand = decode_mips16_operand (type, extended_p);
13165 if (!operand || (operand->type != OP_INT && operand->type != OP_PCREL))
13166 abort ();
13167 return (const struct mips_int_operand *) operand;
13168}
13169
13170/* Return true if SVAL fits OPERAND. RELOC is as for mips16_immed. */
13171
13172static bfd_boolean
13173mips16_immed_in_range_p (const struct mips_int_operand *operand,
13174 bfd_reloc_code_real_type reloc, offsetT sval)
13175{
13176 int min_val, max_val;
13177
13178 min_val = mips_int_operand_min (operand);
13179 max_val = mips_int_operand_max (operand);
13180 if (reloc != BFD_RELOC_UNUSED)
13181 {
13182 if (min_val < 0)
13183 sval = SEXT_16BIT (sval);
13184 else
13185 sval &= 0xffff;
13186 }
13187
13188 return (sval >= min_val
13189 && sval <= max_val
13190 && (sval & ((1 << operand->shift) - 1)) == 0);
13191}
13192
5c04167a
RS
13193/* Install immediate value VAL into MIPS16 instruction *INSN,
13194 extending it if necessary. The instruction in *INSN may
13195 already be extended.
13196
43c0598f
RS
13197 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
13198 if none. In the former case, VAL is a 16-bit number with no
13199 defined signedness.
13200
13201 TYPE is the type of the immediate field. USER_INSN_LENGTH
13202 is the length that the user requested, or 0 if none. */
252b5132
RH
13203
13204static void
43c0598f
RS
13205mips16_immed (char *file, unsigned int line, int type,
13206 bfd_reloc_code_real_type reloc, offsetT val,
5c04167a 13207 unsigned int user_insn_length, unsigned long *insn)
252b5132 13208{
3ccad066
RS
13209 const struct mips_int_operand *operand;
13210 unsigned int uval, length;
252b5132 13211
3ccad066
RS
13212 operand = mips16_immed_operand (type, FALSE);
13213 if (!mips16_immed_in_range_p (operand, reloc, val))
5c04167a
RS
13214 {
13215 /* We need an extended instruction. */
13216 if (user_insn_length == 2)
13217 as_bad_where (file, line, _("invalid unextended operand value"));
13218 else
13219 *insn |= MIPS16_EXTEND;
13220 }
13221 else if (user_insn_length == 4)
13222 {
13223 /* The operand doesn't force an unextended instruction to be extended.
13224 Warn if the user wanted an extended instruction anyway. */
13225 *insn |= MIPS16_EXTEND;
13226 as_warn_where (file, line,
13227 _("extended operand requested but not required"));
13228 }
252b5132 13229
3ccad066
RS
13230 length = mips16_opcode_length (*insn);
13231 if (length == 4)
252b5132 13232 {
3ccad066
RS
13233 operand = mips16_immed_operand (type, TRUE);
13234 if (!mips16_immed_in_range_p (operand, reloc, val))
13235 as_bad_where (file, line,
13236 _("operand value out of range for instruction"));
252b5132 13237 }
3ccad066
RS
13238 uval = ((unsigned int) val >> operand->shift) - operand->bias;
13239 if (length == 2)
13240 *insn = mips_insert_operand (&operand->root, *insn, uval);
252b5132 13241 else
3ccad066 13242 *insn |= mips16_immed_extend (uval, operand->root.size);
252b5132
RH
13243}
13244\f
d6f16593 13245struct percent_op_match
ad8d3bb3 13246{
5e0116d5
RS
13247 const char *str;
13248 bfd_reloc_code_real_type reloc;
d6f16593
MR
13249};
13250
13251static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 13252{
5e0116d5 13253 {"%lo", BFD_RELOC_LO16},
5e0116d5
RS
13254 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
13255 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
13256 {"%call16", BFD_RELOC_MIPS_CALL16},
13257 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
13258 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
13259 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
13260 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
13261 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
13262 {"%got", BFD_RELOC_MIPS_GOT16},
13263 {"%gp_rel", BFD_RELOC_GPREL16},
13264 {"%half", BFD_RELOC_16},
13265 {"%highest", BFD_RELOC_MIPS_HIGHEST},
13266 {"%higher", BFD_RELOC_MIPS_HIGHER},
13267 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
13268 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
13269 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
13270 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
13271 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
13272 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
13273 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
13274 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
5e0116d5 13275 {"%hi", BFD_RELOC_HI16_S}
ad8d3bb3
TS
13276};
13277
d6f16593
MR
13278static const struct percent_op_match mips16_percent_op[] =
13279{
13280 {"%lo", BFD_RELOC_MIPS16_LO16},
13281 {"%gprel", BFD_RELOC_MIPS16_GPREL},
738e5348
RS
13282 {"%got", BFD_RELOC_MIPS16_GOT16},
13283 {"%call16", BFD_RELOC_MIPS16_CALL16},
d0f13682
CLT
13284 {"%hi", BFD_RELOC_MIPS16_HI16_S},
13285 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
13286 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
13287 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
13288 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
13289 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
13290 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
13291 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
d6f16593
MR
13292};
13293
252b5132 13294
5e0116d5
RS
13295/* Return true if *STR points to a relocation operator. When returning true,
13296 move *STR over the operator and store its relocation code in *RELOC.
13297 Leave both *STR and *RELOC alone when returning false. */
13298
13299static bfd_boolean
17a2f251 13300parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 13301{
d6f16593
MR
13302 const struct percent_op_match *percent_op;
13303 size_t limit, i;
13304
13305 if (mips_opts.mips16)
13306 {
13307 percent_op = mips16_percent_op;
13308 limit = ARRAY_SIZE (mips16_percent_op);
13309 }
13310 else
13311 {
13312 percent_op = mips_percent_op;
13313 limit = ARRAY_SIZE (mips_percent_op);
13314 }
76b3015f 13315
d6f16593 13316 for (i = 0; i < limit; i++)
5e0116d5 13317 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 13318 {
3f98094e
DJ
13319 int len = strlen (percent_op[i].str);
13320
13321 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
13322 continue;
13323
5e0116d5
RS
13324 *str += strlen (percent_op[i].str);
13325 *reloc = percent_op[i].reloc;
394f9b3a 13326
5e0116d5
RS
13327 /* Check whether the output BFD supports this relocation.
13328 If not, issue an error and fall back on something safe. */
13329 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 13330 {
20203fb9 13331 as_bad (_("relocation %s isn't supported by the current ABI"),
5e0116d5 13332 percent_op[i].str);
01a3f561 13333 *reloc = BFD_RELOC_UNUSED;
394f9b3a 13334 }
5e0116d5 13335 return TRUE;
394f9b3a 13336 }
5e0116d5 13337 return FALSE;
394f9b3a 13338}
ad8d3bb3 13339
ad8d3bb3 13340
5e0116d5
RS
13341/* Parse string STR as a 16-bit relocatable operand. Store the
13342 expression in *EP and the relocations in the array starting
13343 at RELOC. Return the number of relocation operators used.
ad8d3bb3 13344
01a3f561 13345 On exit, EXPR_END points to the first character after the expression. */
ad8d3bb3 13346
5e0116d5 13347static size_t
17a2f251
TS
13348my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
13349 char *str)
ad8d3bb3 13350{
5e0116d5
RS
13351 bfd_reloc_code_real_type reversed_reloc[3];
13352 size_t reloc_index, i;
09b8f35a
RS
13353 int crux_depth, str_depth;
13354 char *crux;
5e0116d5
RS
13355
13356 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
13357 in REVERSED_RELOC. End the loop with CRUX pointing to the start
13358 of the main expression and with CRUX_DEPTH containing the number
13359 of open brackets at that point. */
13360 reloc_index = -1;
13361 str_depth = 0;
13362 do
fb1b3232 13363 {
09b8f35a
RS
13364 reloc_index++;
13365 crux = str;
13366 crux_depth = str_depth;
13367
13368 /* Skip over whitespace and brackets, keeping count of the number
13369 of brackets. */
13370 while (*str == ' ' || *str == '\t' || *str == '(')
13371 if (*str++ == '(')
13372 str_depth++;
5e0116d5 13373 }
09b8f35a
RS
13374 while (*str == '%'
13375 && reloc_index < (HAVE_NEWABI ? 3 : 1)
13376 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 13377
09b8f35a 13378 my_getExpression (ep, crux);
5e0116d5 13379 str = expr_end;
394f9b3a 13380
5e0116d5 13381 /* Match every open bracket. */
09b8f35a 13382 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 13383 if (*str++ == ')')
09b8f35a 13384 crux_depth--;
394f9b3a 13385
09b8f35a 13386 if (crux_depth > 0)
20203fb9 13387 as_bad (_("unclosed '('"));
394f9b3a 13388
5e0116d5 13389 expr_end = str;
252b5132 13390
01a3f561 13391 if (reloc_index != 0)
64bdfcaf
RS
13392 {
13393 prev_reloc_op_frag = frag_now;
13394 for (i = 0; i < reloc_index; i++)
13395 reloc[i] = reversed_reloc[reloc_index - 1 - i];
13396 }
fb1b3232 13397
5e0116d5 13398 return reloc_index;
252b5132
RH
13399}
13400
13401static void
17a2f251 13402my_getExpression (expressionS *ep, char *str)
252b5132
RH
13403{
13404 char *save_in;
13405
13406 save_in = input_line_pointer;
13407 input_line_pointer = str;
13408 expression (ep);
13409 expr_end = input_line_pointer;
13410 input_line_pointer = save_in;
252b5132
RH
13411}
13412
252b5132 13413char *
17a2f251 13414md_atof (int type, char *litP, int *sizeP)
252b5132 13415{
499ac353 13416 return ieee_md_atof (type, litP, sizeP, target_big_endian);
252b5132
RH
13417}
13418
13419void
17a2f251 13420md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
13421{
13422 if (target_big_endian)
13423 number_to_chars_bigendian (buf, val, n);
13424 else
13425 number_to_chars_littleendian (buf, val, n);
13426}
13427\f
e013f690
TS
13428static int support_64bit_objects(void)
13429{
13430 const char **list, **l;
aa3d8fdf 13431 int yes;
e013f690
TS
13432
13433 list = bfd_target_list ();
13434 for (l = list; *l != NULL; l++)
aeffff67
RS
13435 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
13436 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
e013f690 13437 break;
aa3d8fdf 13438 yes = (*l != NULL);
e013f690 13439 free (list);
aa3d8fdf 13440 return yes;
e013f690
TS
13441}
13442
316f5878
RS
13443/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
13444 NEW_VALUE. Warn if another value was already specified. Note:
13445 we have to defer parsing the -march and -mtune arguments in order
13446 to handle 'from-abi' correctly, since the ABI might be specified
13447 in a later argument. */
13448
13449static void
17a2f251 13450mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
13451{
13452 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
13453 as_warn (_("A different %s was already specified, is now %s"),
13454 string_ptr == &mips_arch_string ? "-march" : "-mtune",
13455 new_value);
13456
13457 *string_ptr = new_value;
13458}
13459
252b5132 13460int
17a2f251 13461md_parse_option (int c, char *arg)
252b5132 13462{
c6278170
RS
13463 unsigned int i;
13464
13465 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
13466 if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
13467 {
13468 file_ase_explicit |= mips_set_ase (&mips_ases[i],
13469 c == mips_ases[i].option_on);
13470 return 1;
13471 }
13472
252b5132
RH
13473 switch (c)
13474 {
119d663a
NC
13475 case OPTION_CONSTRUCT_FLOATS:
13476 mips_disable_float_construction = 0;
13477 break;
bdaaa2e1 13478
119d663a
NC
13479 case OPTION_NO_CONSTRUCT_FLOATS:
13480 mips_disable_float_construction = 1;
13481 break;
bdaaa2e1 13482
252b5132
RH
13483 case OPTION_TRAP:
13484 mips_trap = 1;
13485 break;
13486
13487 case OPTION_BREAK:
13488 mips_trap = 0;
13489 break;
13490
13491 case OPTION_EB:
13492 target_big_endian = 1;
13493 break;
13494
13495 case OPTION_EL:
13496 target_big_endian = 0;
13497 break;
13498
13499 case 'O':
4ffff32f
TS
13500 if (arg == NULL)
13501 mips_optimize = 1;
13502 else if (arg[0] == '0')
13503 mips_optimize = 0;
13504 else if (arg[0] == '1')
252b5132
RH
13505 mips_optimize = 1;
13506 else
13507 mips_optimize = 2;
13508 break;
13509
13510 case 'g':
13511 if (arg == NULL)
13512 mips_debug = 2;
13513 else
13514 mips_debug = atoi (arg);
252b5132
RH
13515 break;
13516
13517 case OPTION_MIPS1:
316f5878 13518 file_mips_isa = ISA_MIPS1;
252b5132
RH
13519 break;
13520
13521 case OPTION_MIPS2:
316f5878 13522 file_mips_isa = ISA_MIPS2;
252b5132
RH
13523 break;
13524
13525 case OPTION_MIPS3:
316f5878 13526 file_mips_isa = ISA_MIPS3;
252b5132
RH
13527 break;
13528
13529 case OPTION_MIPS4:
316f5878 13530 file_mips_isa = ISA_MIPS4;
e7af610e
NC
13531 break;
13532
84ea6cf2 13533 case OPTION_MIPS5:
316f5878 13534 file_mips_isa = ISA_MIPS5;
84ea6cf2
NC
13535 break;
13536
e7af610e 13537 case OPTION_MIPS32:
316f5878 13538 file_mips_isa = ISA_MIPS32;
252b5132
RH
13539 break;
13540
af7ee8bf
CD
13541 case OPTION_MIPS32R2:
13542 file_mips_isa = ISA_MIPS32R2;
13543 break;
13544
5f74bc13
CD
13545 case OPTION_MIPS64R2:
13546 file_mips_isa = ISA_MIPS64R2;
13547 break;
13548
84ea6cf2 13549 case OPTION_MIPS64:
316f5878 13550 file_mips_isa = ISA_MIPS64;
84ea6cf2
NC
13551 break;
13552
ec68c924 13553 case OPTION_MTUNE:
316f5878
RS
13554 mips_set_option_string (&mips_tune_string, arg);
13555 break;
ec68c924 13556
316f5878
RS
13557 case OPTION_MARCH:
13558 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
13559 break;
13560
13561 case OPTION_M4650:
316f5878
RS
13562 mips_set_option_string (&mips_arch_string, "4650");
13563 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
13564 break;
13565
13566 case OPTION_NO_M4650:
13567 break;
13568
13569 case OPTION_M4010:
316f5878
RS
13570 mips_set_option_string (&mips_arch_string, "4010");
13571 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
13572 break;
13573
13574 case OPTION_NO_M4010:
13575 break;
13576
13577 case OPTION_M4100:
316f5878
RS
13578 mips_set_option_string (&mips_arch_string, "4100");
13579 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
13580 break;
13581
13582 case OPTION_NO_M4100:
13583 break;
13584
252b5132 13585 case OPTION_M3900:
316f5878
RS
13586 mips_set_option_string (&mips_arch_string, "3900");
13587 mips_set_option_string (&mips_tune_string, "3900");
252b5132 13588 break;
bdaaa2e1 13589
252b5132
RH
13590 case OPTION_NO_M3900:
13591 break;
13592
df58fc94
RS
13593 case OPTION_MICROMIPS:
13594 if (mips_opts.mips16 == 1)
13595 {
13596 as_bad (_("-mmicromips cannot be used with -mips16"));
13597 return 0;
13598 }
13599 mips_opts.micromips = 1;
13600 mips_no_prev_insn ();
13601 break;
13602
13603 case OPTION_NO_MICROMIPS:
13604 mips_opts.micromips = 0;
13605 mips_no_prev_insn ();
13606 break;
13607
252b5132 13608 case OPTION_MIPS16:
df58fc94
RS
13609 if (mips_opts.micromips == 1)
13610 {
13611 as_bad (_("-mips16 cannot be used with -micromips"));
13612 return 0;
13613 }
252b5132 13614 mips_opts.mips16 = 1;
7d10b47d 13615 mips_no_prev_insn ();
252b5132
RH
13616 break;
13617
13618 case OPTION_NO_MIPS16:
13619 mips_opts.mips16 = 0;
7d10b47d 13620 mips_no_prev_insn ();
252b5132
RH
13621 break;
13622
6a32d874
CM
13623 case OPTION_FIX_24K:
13624 mips_fix_24k = 1;
13625 break;
13626
13627 case OPTION_NO_FIX_24K:
13628 mips_fix_24k = 0;
13629 break;
13630
c67a084a
NC
13631 case OPTION_FIX_LOONGSON2F_JUMP:
13632 mips_fix_loongson2f_jump = TRUE;
13633 break;
13634
13635 case OPTION_NO_FIX_LOONGSON2F_JUMP:
13636 mips_fix_loongson2f_jump = FALSE;
13637 break;
13638
13639 case OPTION_FIX_LOONGSON2F_NOP:
13640 mips_fix_loongson2f_nop = TRUE;
13641 break;
13642
13643 case OPTION_NO_FIX_LOONGSON2F_NOP:
13644 mips_fix_loongson2f_nop = FALSE;
13645 break;
13646
d766e8ec
RS
13647 case OPTION_FIX_VR4120:
13648 mips_fix_vr4120 = 1;
60b63b72
RS
13649 break;
13650
d766e8ec
RS
13651 case OPTION_NO_FIX_VR4120:
13652 mips_fix_vr4120 = 0;
60b63b72
RS
13653 break;
13654
7d8e00cf
RS
13655 case OPTION_FIX_VR4130:
13656 mips_fix_vr4130 = 1;
13657 break;
13658
13659 case OPTION_NO_FIX_VR4130:
13660 mips_fix_vr4130 = 0;
13661 break;
13662
d954098f
DD
13663 case OPTION_FIX_CN63XXP1:
13664 mips_fix_cn63xxp1 = TRUE;
13665 break;
13666
13667 case OPTION_NO_FIX_CN63XXP1:
13668 mips_fix_cn63xxp1 = FALSE;
13669 break;
13670
4a6a3df4
AO
13671 case OPTION_RELAX_BRANCH:
13672 mips_relax_branch = 1;
13673 break;
13674
13675 case OPTION_NO_RELAX_BRANCH:
13676 mips_relax_branch = 0;
13677 break;
13678
833794fc
MR
13679 case OPTION_INSN32:
13680 mips_opts.insn32 = TRUE;
13681 break;
13682
13683 case OPTION_NO_INSN32:
13684 mips_opts.insn32 = FALSE;
13685 break;
13686
aa6975fb
ILT
13687 case OPTION_MSHARED:
13688 mips_in_shared = TRUE;
13689 break;
13690
13691 case OPTION_MNO_SHARED:
13692 mips_in_shared = FALSE;
13693 break;
13694
aed1a261
RS
13695 case OPTION_MSYM32:
13696 mips_opts.sym32 = TRUE;
13697 break;
13698
13699 case OPTION_MNO_SYM32:
13700 mips_opts.sym32 = FALSE;
13701 break;
13702
252b5132
RH
13703 /* When generating ELF code, we permit -KPIC and -call_shared to
13704 select SVR4_PIC, and -non_shared to select no PIC. This is
13705 intended to be compatible with Irix 5. */
13706 case OPTION_CALL_SHARED:
252b5132 13707 mips_pic = SVR4_PIC;
143d77c5 13708 mips_abicalls = TRUE;
252b5132
RH
13709 break;
13710
861fb55a 13711 case OPTION_CALL_NONPIC:
861fb55a
DJ
13712 mips_pic = NO_PIC;
13713 mips_abicalls = TRUE;
13714 break;
13715
252b5132 13716 case OPTION_NON_SHARED:
252b5132 13717 mips_pic = NO_PIC;
143d77c5 13718 mips_abicalls = FALSE;
252b5132
RH
13719 break;
13720
44075ae2
TS
13721 /* The -xgot option tells the assembler to use 32 bit offsets
13722 when accessing the got in SVR4_PIC mode. It is for Irix
252b5132
RH
13723 compatibility. */
13724 case OPTION_XGOT:
13725 mips_big_got = 1;
13726 break;
13727
13728 case 'G':
6caf9ef4
TS
13729 g_switch_value = atoi (arg);
13730 g_switch_seen = 1;
252b5132
RH
13731 break;
13732
34ba82a8
TS
13733 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
13734 and -mabi=64. */
252b5132 13735 case OPTION_32:
f3ded42a 13736 mips_abi = O32_ABI;
252b5132
RH
13737 break;
13738
e013f690 13739 case OPTION_N32:
316f5878 13740 mips_abi = N32_ABI;
e013f690 13741 break;
252b5132 13742
e013f690 13743 case OPTION_64:
316f5878 13744 mips_abi = N64_ABI;
f43abd2b 13745 if (!support_64bit_objects())
e013f690 13746 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132
RH
13747 break;
13748
c97ef257 13749 case OPTION_GP32:
a325df1d 13750 file_mips_gp32 = 1;
c97ef257
AH
13751 break;
13752
13753 case OPTION_GP64:
a325df1d 13754 file_mips_gp32 = 0;
c97ef257 13755 break;
252b5132 13756
ca4e0257 13757 case OPTION_FP32:
a325df1d 13758 file_mips_fp32 = 1;
316f5878
RS
13759 break;
13760
13761 case OPTION_FP64:
13762 file_mips_fp32 = 0;
ca4e0257
RS
13763 break;
13764
037b32b9
AN
13765 case OPTION_SINGLE_FLOAT:
13766 file_mips_single_float = 1;
13767 break;
13768
13769 case OPTION_DOUBLE_FLOAT:
13770 file_mips_single_float = 0;
13771 break;
13772
13773 case OPTION_SOFT_FLOAT:
13774 file_mips_soft_float = 1;
13775 break;
13776
13777 case OPTION_HARD_FLOAT:
13778 file_mips_soft_float = 0;
13779 break;
13780
252b5132 13781 case OPTION_MABI:
e013f690 13782 if (strcmp (arg, "32") == 0)
316f5878 13783 mips_abi = O32_ABI;
e013f690 13784 else if (strcmp (arg, "o64") == 0)
316f5878 13785 mips_abi = O64_ABI;
e013f690 13786 else if (strcmp (arg, "n32") == 0)
316f5878 13787 mips_abi = N32_ABI;
e013f690
TS
13788 else if (strcmp (arg, "64") == 0)
13789 {
316f5878 13790 mips_abi = N64_ABI;
e013f690
TS
13791 if (! support_64bit_objects())
13792 as_fatal (_("No compiled in support for 64 bit object file "
13793 "format"));
13794 }
13795 else if (strcmp (arg, "eabi") == 0)
316f5878 13796 mips_abi = EABI_ABI;
e013f690 13797 else
da0e507f
TS
13798 {
13799 as_fatal (_("invalid abi -mabi=%s"), arg);
13800 return 0;
13801 }
252b5132
RH
13802 break;
13803
6b76fefe 13804 case OPTION_M7000_HILO_FIX:
b34976b6 13805 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
13806 break;
13807
9ee72ff1 13808 case OPTION_MNO_7000_HILO_FIX:
b34976b6 13809 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
13810 break;
13811
ecb4347a 13812 case OPTION_MDEBUG:
b34976b6 13813 mips_flag_mdebug = TRUE;
ecb4347a
DJ
13814 break;
13815
13816 case OPTION_NO_MDEBUG:
b34976b6 13817 mips_flag_mdebug = FALSE;
ecb4347a 13818 break;
dcd410fe
RO
13819
13820 case OPTION_PDR:
13821 mips_flag_pdr = TRUE;
13822 break;
13823
13824 case OPTION_NO_PDR:
13825 mips_flag_pdr = FALSE;
13826 break;
0a44bf69
RS
13827
13828 case OPTION_MVXWORKS_PIC:
13829 mips_pic = VXWORKS_PIC;
13830 break;
ecb4347a 13831
ba92f887
MR
13832 case OPTION_NAN:
13833 if (strcmp (arg, "2008") == 0)
13834 mips_flag_nan2008 = TRUE;
13835 else if (strcmp (arg, "legacy") == 0)
13836 mips_flag_nan2008 = FALSE;
13837 else
13838 {
13839 as_fatal (_("Invalid NaN setting -mnan=%s"), arg);
13840 return 0;
13841 }
13842 break;
13843
252b5132
RH
13844 default:
13845 return 0;
13846 }
13847
c67a084a
NC
13848 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
13849
252b5132
RH
13850 return 1;
13851}
316f5878
RS
13852\f
13853/* Set up globals to generate code for the ISA or processor
13854 described by INFO. */
252b5132 13855
252b5132 13856static void
17a2f251 13857mips_set_architecture (const struct mips_cpu_info *info)
252b5132 13858{
316f5878 13859 if (info != 0)
252b5132 13860 {
fef14a42
TS
13861 file_mips_arch = info->cpu;
13862 mips_opts.arch = info->cpu;
316f5878 13863 mips_opts.isa = info->isa;
252b5132 13864 }
252b5132
RH
13865}
13866
252b5132 13867
316f5878 13868/* Likewise for tuning. */
252b5132 13869
316f5878 13870static void
17a2f251 13871mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
13872{
13873 if (info != 0)
fef14a42 13874 mips_tune = info->cpu;
316f5878 13875}
80cc45a5 13876
34ba82a8 13877
252b5132 13878void
17a2f251 13879mips_after_parse_args (void)
e9670677 13880{
fef14a42
TS
13881 const struct mips_cpu_info *arch_info = 0;
13882 const struct mips_cpu_info *tune_info = 0;
13883
e9670677 13884 /* GP relative stuff not working for PE */
6caf9ef4 13885 if (strncmp (TARGET_OS, "pe", 2) == 0)
e9670677 13886 {
6caf9ef4 13887 if (g_switch_seen && g_switch_value != 0)
e9670677
MR
13888 as_bad (_("-G not supported in this configuration."));
13889 g_switch_value = 0;
13890 }
13891
cac012d6
AO
13892 if (mips_abi == NO_ABI)
13893 mips_abi = MIPS_DEFAULT_ABI;
13894
22923709
RS
13895 /* The following code determines the architecture and register size.
13896 Similar code was added to GCC 3.3 (see override_options() in
13897 config/mips/mips.c). The GAS and GCC code should be kept in sync
13898 as much as possible. */
e9670677 13899
316f5878 13900 if (mips_arch_string != 0)
fef14a42 13901 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 13902
316f5878 13903 if (file_mips_isa != ISA_UNKNOWN)
e9670677 13904 {
316f5878 13905 /* Handle -mipsN. At this point, file_mips_isa contains the
fef14a42 13906 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 13907 the -march selection (if any). */
fef14a42 13908 if (arch_info != 0)
e9670677 13909 {
316f5878
RS
13910 /* -march takes precedence over -mipsN, since it is more descriptive.
13911 There's no harm in specifying both as long as the ISA levels
13912 are the same. */
fef14a42 13913 if (file_mips_isa != arch_info->isa)
316f5878
RS
13914 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
13915 mips_cpu_info_from_isa (file_mips_isa)->name,
fef14a42 13916 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 13917 }
316f5878 13918 else
fef14a42 13919 arch_info = mips_cpu_info_from_isa (file_mips_isa);
e9670677
MR
13920 }
13921
fef14a42 13922 if (arch_info == 0)
95bfe26e
MF
13923 {
13924 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
13925 gas_assert (arch_info);
13926 }
e9670677 13927
fef14a42 13928 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
20203fb9 13929 as_bad (_("-march=%s is not compatible with the selected ABI"),
fef14a42
TS
13930 arch_info->name);
13931
13932 mips_set_architecture (arch_info);
13933
13934 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
13935 if (mips_tune_string != 0)
13936 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 13937
fef14a42
TS
13938 if (tune_info == 0)
13939 mips_set_tune (arch_info);
13940 else
13941 mips_set_tune (tune_info);
e9670677 13942
316f5878 13943 if (file_mips_gp32 >= 0)
e9670677 13944 {
316f5878
RS
13945 /* The user specified the size of the integer registers. Make sure
13946 it agrees with the ABI and ISA. */
13947 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
13948 as_bad (_("-mgp64 used with a 32-bit processor"));
13949 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
13950 as_bad (_("-mgp32 used with a 64-bit ABI"));
13951 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
13952 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
13953 }
13954 else
13955 {
316f5878
RS
13956 /* Infer the integer register size from the ABI and processor.
13957 Restrict ourselves to 32-bit registers if that's all the
13958 processor has, or if the ABI cannot handle 64-bit registers. */
13959 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
13960 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
e9670677
MR
13961 }
13962
ad3fea08
TS
13963 switch (file_mips_fp32)
13964 {
13965 default:
13966 case -1:
13967 /* No user specified float register size.
13968 ??? GAS treats single-float processors as though they had 64-bit
13969 float registers (although it complains when double-precision
13970 instructions are used). As things stand, saying they have 32-bit
13971 registers would lead to spurious "register must be even" messages.
13972 So here we assume float registers are never smaller than the
13973 integer ones. */
13974 if (file_mips_gp32 == 0)
13975 /* 64-bit integer registers implies 64-bit float registers. */
13976 file_mips_fp32 = 0;
c6278170 13977 else if ((mips_opts.ase & FP64_ASES)
ad3fea08
TS
13978 && ISA_HAS_64BIT_FPRS (mips_opts.isa))
13979 /* -mips3d and -mdmx imply 64-bit float registers, if possible. */
13980 file_mips_fp32 = 0;
13981 else
13982 /* 32-bit float registers. */
13983 file_mips_fp32 = 1;
13984 break;
13985
13986 /* The user specified the size of the float registers. Check if it
13987 agrees with the ABI and ISA. */
13988 case 0:
13989 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
13990 as_bad (_("-mfp64 used with a 32-bit fpu"));
13991 else if (ABI_NEEDS_32BIT_REGS (mips_abi)
13992 && !ISA_HAS_MXHC1 (mips_opts.isa))
13993 as_warn (_("-mfp64 used with a 32-bit ABI"));
13994 break;
13995 case 1:
13996 if (ABI_NEEDS_64BIT_REGS (mips_abi))
13997 as_warn (_("-mfp32 used with a 64-bit ABI"));
13998 break;
13999 }
e9670677 14000
316f5878 14001 /* End of GCC-shared inference code. */
e9670677 14002
17a2f251
TS
14003 /* This flag is set when we have a 64-bit capable CPU but use only
14004 32-bit wide registers. Note that EABI does not use it. */
14005 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
14006 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
14007 || mips_abi == O32_ABI))
316f5878 14008 mips_32bitmode = 1;
e9670677
MR
14009
14010 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
14011 as_bad (_("trap exception not supported at ISA 1"));
14012
e9670677
MR
14013 /* If the selected architecture includes support for ASEs, enable
14014 generation of code for them. */
a4672219 14015 if (mips_opts.mips16 == -1)
fef14a42 14016 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
df58fc94
RS
14017 if (mips_opts.micromips == -1)
14018 mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_arch)) ? 1 : 0;
846ef2d0
RS
14019
14020 /* MIPS3D and MDMX require 64-bit FPRs, so -mfp32 should stop those
14021 ASEs from being selected implicitly. */
14022 if (file_mips_fp32 == 1)
14023 file_ase_explicit |= ASE_MIPS3D | ASE_MDMX;
14024
14025 /* If the user didn't explicitly select or deselect a particular ASE,
14026 use the default setting for the CPU. */
14027 mips_opts.ase |= (arch_info->ase & ~file_ase_explicit);
14028
e9670677 14029 file_mips_isa = mips_opts.isa;
846ef2d0 14030 file_ase = mips_opts.ase;
e9670677
MR
14031 mips_opts.gp32 = file_mips_gp32;
14032 mips_opts.fp32 = file_mips_fp32;
037b32b9
AN
14033 mips_opts.soft_float = file_mips_soft_float;
14034 mips_opts.single_float = file_mips_single_float;
e9670677 14035
c6278170
RS
14036 mips_check_isa_supports_ases ();
14037
ecb4347a 14038 if (mips_flag_mdebug < 0)
e8044f35 14039 mips_flag_mdebug = 0;
e9670677
MR
14040}
14041\f
14042void
17a2f251 14043mips_init_after_args (void)
252b5132
RH
14044{
14045 /* initialize opcodes */
14046 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 14047 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
14048}
14049
14050long
17a2f251 14051md_pcrel_from (fixS *fixP)
252b5132 14052{
a7ebbfdf
TS
14053 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
14054 switch (fixP->fx_r_type)
14055 {
df58fc94
RS
14056 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
14057 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
14058 /* Return the address of the delay slot. */
14059 return addr + 2;
14060
14061 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
14062 case BFD_RELOC_MICROMIPS_JMP:
a7ebbfdf
TS
14063 case BFD_RELOC_16_PCREL_S2:
14064 case BFD_RELOC_MIPS_JMP:
14065 /* Return the address of the delay slot. */
14066 return addr + 4;
df58fc94 14067
b47468a6
CM
14068 case BFD_RELOC_32_PCREL:
14069 return addr;
14070
a7ebbfdf 14071 default:
58ea3d6a 14072 /* We have no relocation type for PC relative MIPS16 instructions. */
64817874
TS
14073 if (fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != now_seg)
14074 as_bad_where (fixP->fx_file, fixP->fx_line,
14075 _("PC relative MIPS16 instruction references a different section"));
a7ebbfdf
TS
14076 return addr;
14077 }
252b5132
RH
14078}
14079
252b5132
RH
14080/* This is called before the symbol table is processed. In order to
14081 work with gcc when using mips-tfile, we must keep all local labels.
14082 However, in other cases, we want to discard them. If we were
14083 called with -g, but we didn't see any debugging information, it may
14084 mean that gcc is smuggling debugging information through to
14085 mips-tfile, in which case we must generate all local labels. */
14086
14087void
17a2f251 14088mips_frob_file_before_adjust (void)
252b5132
RH
14089{
14090#ifndef NO_ECOFF_DEBUGGING
14091 if (ECOFF_DEBUGGING
14092 && mips_debug != 0
14093 && ! ecoff_debugging_seen)
14094 flag_keep_locals = 1;
14095#endif
14096}
14097
3b91255e 14098/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 14099 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
14100 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
14101 relocation operators.
14102
14103 For our purposes, a %lo() expression matches a %got() or %hi()
14104 expression if:
14105
14106 (a) it refers to the same symbol; and
14107 (b) the offset applied in the %lo() expression is no lower than
14108 the offset applied in the %got() or %hi().
14109
14110 (b) allows us to cope with code like:
14111
14112 lui $4,%hi(foo)
14113 lh $4,%lo(foo+2)($4)
14114
14115 ...which is legal on RELA targets, and has a well-defined behaviour
14116 if the user knows that adding 2 to "foo" will not induce a carry to
14117 the high 16 bits.
14118
14119 When several %lo()s match a particular %got() or %hi(), we use the
14120 following rules to distinguish them:
14121
14122 (1) %lo()s with smaller offsets are a better match than %lo()s with
14123 higher offsets.
14124
14125 (2) %lo()s with no matching %got() or %hi() are better than those
14126 that already have a matching %got() or %hi().
14127
14128 (3) later %lo()s are better than earlier %lo()s.
14129
14130 These rules are applied in order.
14131
14132 (1) means, among other things, that %lo()s with identical offsets are
14133 chosen if they exist.
14134
14135 (2) means that we won't associate several high-part relocations with
14136 the same low-part relocation unless there's no alternative. Having
14137 several high parts for the same low part is a GNU extension; this rule
14138 allows careful users to avoid it.
14139
14140 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
14141 with the last high-part relocation being at the front of the list.
14142 It therefore makes sense to choose the last matching low-part
14143 relocation, all other things being equal. It's also easier
14144 to code that way. */
252b5132
RH
14145
14146void
17a2f251 14147mips_frob_file (void)
252b5132
RH
14148{
14149 struct mips_hi_fixup *l;
35903be0 14150 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
252b5132
RH
14151
14152 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
14153 {
14154 segment_info_type *seginfo;
3b91255e
RS
14155 bfd_boolean matched_lo_p;
14156 fixS **hi_pos, **lo_pos, **pos;
252b5132 14157
9c2799c2 14158 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 14159
5919d012 14160 /* If a GOT16 relocation turns out to be against a global symbol,
b886a2ab
RS
14161 there isn't supposed to be a matching LO. Ignore %gots against
14162 constants; we'll report an error for those later. */
738e5348 14163 if (got16_reloc_p (l->fixp->fx_r_type)
b886a2ab
RS
14164 && !(l->fixp->fx_addsy
14165 && pic_need_relax (l->fixp->fx_addsy, l->seg)))
5919d012
RS
14166 continue;
14167
14168 /* Check quickly whether the next fixup happens to be a matching %lo. */
14169 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
14170 continue;
14171
252b5132 14172 seginfo = seg_info (l->seg);
252b5132 14173
3b91255e
RS
14174 /* Set HI_POS to the position of this relocation in the chain.
14175 Set LO_POS to the position of the chosen low-part relocation.
14176 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
14177 relocation that matches an immediately-preceding high-part
14178 relocation. */
14179 hi_pos = NULL;
14180 lo_pos = NULL;
14181 matched_lo_p = FALSE;
738e5348 14182 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
35903be0 14183
3b91255e
RS
14184 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
14185 {
14186 if (*pos == l->fixp)
14187 hi_pos = pos;
14188
35903be0 14189 if ((*pos)->fx_r_type == looking_for_rtype
30cfc97a 14190 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
3b91255e
RS
14191 && (*pos)->fx_offset >= l->fixp->fx_offset
14192 && (lo_pos == NULL
14193 || (*pos)->fx_offset < (*lo_pos)->fx_offset
14194 || (!matched_lo_p
14195 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
14196 lo_pos = pos;
14197
14198 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
14199 && fixup_has_matching_lo_p (*pos));
14200 }
14201
14202 /* If we found a match, remove the high-part relocation from its
14203 current position and insert it before the low-part relocation.
14204 Make the offsets match so that fixup_has_matching_lo_p()
14205 will return true.
14206
14207 We don't warn about unmatched high-part relocations since some
14208 versions of gcc have been known to emit dead "lui ...%hi(...)"
14209 instructions. */
14210 if (lo_pos != NULL)
14211 {
14212 l->fixp->fx_offset = (*lo_pos)->fx_offset;
14213 if (l->fixp->fx_next != *lo_pos)
252b5132 14214 {
3b91255e
RS
14215 *hi_pos = l->fixp->fx_next;
14216 l->fixp->fx_next = *lo_pos;
14217 *lo_pos = l->fixp;
252b5132 14218 }
252b5132
RH
14219 }
14220 }
14221}
14222
252b5132 14223int
17a2f251 14224mips_force_relocation (fixS *fixp)
252b5132 14225{
ae6063d4 14226 if (generic_force_reloc (fixp))
252b5132
RH
14227 return 1;
14228
df58fc94
RS
14229 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
14230 so that the linker relaxation can update targets. */
14231 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
14232 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
14233 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
14234 return 1;
14235
3e722fb5 14236 return 0;
252b5132
RH
14237}
14238
b886a2ab
RS
14239/* Read the instruction associated with RELOC from BUF. */
14240
14241static unsigned int
14242read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
14243{
14244 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
14245 return read_compressed_insn (buf, 4);
14246 else
14247 return read_insn (buf);
14248}
14249
14250/* Write instruction INSN to BUF, given that it has been relocated
14251 by RELOC. */
14252
14253static void
14254write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
14255 unsigned long insn)
14256{
14257 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
14258 write_compressed_insn (buf, insn, 4);
14259 else
14260 write_insn (buf, insn);
14261}
14262
252b5132
RH
14263/* Apply a fixup to the object file. */
14264
94f592af 14265void
55cf6793 14266md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 14267{
4d68580a 14268 char *buf;
b886a2ab 14269 unsigned long insn;
a7ebbfdf 14270 reloc_howto_type *howto;
252b5132 14271
a7ebbfdf
TS
14272 /* We ignore generic BFD relocations we don't know about. */
14273 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
14274 if (! howto)
14275 return;
65551fa4 14276
df58fc94
RS
14277 gas_assert (fixP->fx_size == 2
14278 || fixP->fx_size == 4
90ecf173
MR
14279 || fixP->fx_r_type == BFD_RELOC_16
14280 || fixP->fx_r_type == BFD_RELOC_64
14281 || fixP->fx_r_type == BFD_RELOC_CTOR
14282 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
df58fc94 14283 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
90ecf173
MR
14284 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
14285 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
14286 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64);
252b5132 14287
4d68580a 14288 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132 14289
df58fc94
RS
14290 gas_assert (!fixP->fx_pcrel || fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
14291 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
14292 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
b47468a6
CM
14293 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
14294 || fixP->fx_r_type == BFD_RELOC_32_PCREL);
b1dca8ee
RS
14295
14296 /* Don't treat parts of a composite relocation as done. There are two
14297 reasons for this:
14298
14299 (1) The second and third parts will be against 0 (RSS_UNDEF) but
14300 should nevertheless be emitted if the first part is.
14301
14302 (2) In normal usage, composite relocations are never assembly-time
14303 constants. The easiest way of dealing with the pathological
14304 exceptions is to generate a relocation against STN_UNDEF and
14305 leave everything up to the linker. */
3994f87e 14306 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
252b5132
RH
14307 fixP->fx_done = 1;
14308
14309 switch (fixP->fx_r_type)
14310 {
3f98094e
DJ
14311 case BFD_RELOC_MIPS_TLS_GD:
14312 case BFD_RELOC_MIPS_TLS_LDM:
741d6ea8
JM
14313 case BFD_RELOC_MIPS_TLS_DTPREL32:
14314 case BFD_RELOC_MIPS_TLS_DTPREL64:
3f98094e
DJ
14315 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
14316 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
14317 case BFD_RELOC_MIPS_TLS_GOTTPREL:
d0f13682
CLT
14318 case BFD_RELOC_MIPS_TLS_TPREL32:
14319 case BFD_RELOC_MIPS_TLS_TPREL64:
3f98094e
DJ
14320 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
14321 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
df58fc94
RS
14322 case BFD_RELOC_MICROMIPS_TLS_GD:
14323 case BFD_RELOC_MICROMIPS_TLS_LDM:
14324 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
14325 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
14326 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
14327 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
14328 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
d0f13682
CLT
14329 case BFD_RELOC_MIPS16_TLS_GD:
14330 case BFD_RELOC_MIPS16_TLS_LDM:
14331 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
14332 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
14333 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
14334 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
14335 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
b886a2ab
RS
14336 if (!fixP->fx_addsy)
14337 {
14338 as_bad_where (fixP->fx_file, fixP->fx_line,
14339 _("TLS relocation against a constant"));
14340 break;
14341 }
3f98094e
DJ
14342 S_SET_THREAD_LOCAL (fixP->fx_addsy);
14343 /* fall through */
14344
252b5132 14345 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
14346 case BFD_RELOC_MIPS_SHIFT5:
14347 case BFD_RELOC_MIPS_SHIFT6:
14348 case BFD_RELOC_MIPS_GOT_DISP:
14349 case BFD_RELOC_MIPS_GOT_PAGE:
14350 case BFD_RELOC_MIPS_GOT_OFST:
14351 case BFD_RELOC_MIPS_SUB:
14352 case BFD_RELOC_MIPS_INSERT_A:
14353 case BFD_RELOC_MIPS_INSERT_B:
14354 case BFD_RELOC_MIPS_DELETE:
14355 case BFD_RELOC_MIPS_HIGHEST:
14356 case BFD_RELOC_MIPS_HIGHER:
14357 case BFD_RELOC_MIPS_SCN_DISP:
14358 case BFD_RELOC_MIPS_REL16:
14359 case BFD_RELOC_MIPS_RELGOT:
14360 case BFD_RELOC_MIPS_JALR:
252b5132
RH
14361 case BFD_RELOC_HI16:
14362 case BFD_RELOC_HI16_S:
b886a2ab 14363 case BFD_RELOC_LO16:
cdf6fd85 14364 case BFD_RELOC_GPREL16:
252b5132
RH
14365 case BFD_RELOC_MIPS_LITERAL:
14366 case BFD_RELOC_MIPS_CALL16:
14367 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 14368 case BFD_RELOC_GPREL32:
252b5132
RH
14369 case BFD_RELOC_MIPS_GOT_HI16:
14370 case BFD_RELOC_MIPS_GOT_LO16:
14371 case BFD_RELOC_MIPS_CALL_HI16:
14372 case BFD_RELOC_MIPS_CALL_LO16:
14373 case BFD_RELOC_MIPS16_GPREL:
738e5348
RS
14374 case BFD_RELOC_MIPS16_GOT16:
14375 case BFD_RELOC_MIPS16_CALL16:
d6f16593
MR
14376 case BFD_RELOC_MIPS16_HI16:
14377 case BFD_RELOC_MIPS16_HI16_S:
b886a2ab 14378 case BFD_RELOC_MIPS16_LO16:
252b5132 14379 case BFD_RELOC_MIPS16_JMP:
df58fc94
RS
14380 case BFD_RELOC_MICROMIPS_JMP:
14381 case BFD_RELOC_MICROMIPS_GOT_DISP:
14382 case BFD_RELOC_MICROMIPS_GOT_PAGE:
14383 case BFD_RELOC_MICROMIPS_GOT_OFST:
14384 case BFD_RELOC_MICROMIPS_SUB:
14385 case BFD_RELOC_MICROMIPS_HIGHEST:
14386 case BFD_RELOC_MICROMIPS_HIGHER:
14387 case BFD_RELOC_MICROMIPS_SCN_DISP:
14388 case BFD_RELOC_MICROMIPS_JALR:
14389 case BFD_RELOC_MICROMIPS_HI16:
14390 case BFD_RELOC_MICROMIPS_HI16_S:
b886a2ab 14391 case BFD_RELOC_MICROMIPS_LO16:
df58fc94
RS
14392 case BFD_RELOC_MICROMIPS_GPREL16:
14393 case BFD_RELOC_MICROMIPS_LITERAL:
14394 case BFD_RELOC_MICROMIPS_CALL16:
14395 case BFD_RELOC_MICROMIPS_GOT16:
14396 case BFD_RELOC_MICROMIPS_GOT_HI16:
14397 case BFD_RELOC_MICROMIPS_GOT_LO16:
14398 case BFD_RELOC_MICROMIPS_CALL_HI16:
14399 case BFD_RELOC_MICROMIPS_CALL_LO16:
067ec077 14400 case BFD_RELOC_MIPS_EH:
b886a2ab
RS
14401 if (fixP->fx_done)
14402 {
14403 offsetT value;
14404
14405 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
14406 {
14407 insn = read_reloc_insn (buf, fixP->fx_r_type);
14408 if (mips16_reloc_p (fixP->fx_r_type))
14409 insn |= mips16_immed_extend (value, 16);
14410 else
14411 insn |= (value & 0xffff);
14412 write_reloc_insn (buf, fixP->fx_r_type, insn);
14413 }
14414 else
14415 as_bad_where (fixP->fx_file, fixP->fx_line,
14416 _("Unsupported constant in relocation"));
14417 }
252b5132
RH
14418 break;
14419
252b5132
RH
14420 case BFD_RELOC_64:
14421 /* This is handled like BFD_RELOC_32, but we output a sign
14422 extended value if we are only 32 bits. */
3e722fb5 14423 if (fixP->fx_done)
252b5132
RH
14424 {
14425 if (8 <= sizeof (valueT))
4d68580a 14426 md_number_to_chars (buf, *valP, 8);
252b5132
RH
14427 else
14428 {
a7ebbfdf 14429 valueT hiv;
252b5132 14430
a7ebbfdf 14431 if ((*valP & 0x80000000) != 0)
252b5132
RH
14432 hiv = 0xffffffff;
14433 else
14434 hiv = 0;
4d68580a
RS
14435 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
14436 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
252b5132
RH
14437 }
14438 }
14439 break;
14440
056350c6 14441 case BFD_RELOC_RVA:
252b5132 14442 case BFD_RELOC_32:
b47468a6 14443 case BFD_RELOC_32_PCREL:
252b5132
RH
14444 case BFD_RELOC_16:
14445 /* If we are deleting this reloc entry, we must fill in the
54f4ddb3
TS
14446 value now. This can happen if we have a .word which is not
14447 resolved when it appears but is later defined. */
252b5132 14448 if (fixP->fx_done)
4d68580a 14449 md_number_to_chars (buf, *valP, fixP->fx_size);
252b5132
RH
14450 break;
14451
252b5132 14452 case BFD_RELOC_16_PCREL_S2:
a7ebbfdf 14453 if ((*valP & 0x3) != 0)
cb56d3d3 14454 as_bad_where (fixP->fx_file, fixP->fx_line,
bad36eac 14455 _("Branch to misaligned address (%lx)"), (long) *valP);
cb56d3d3 14456
54f4ddb3
TS
14457 /* We need to save the bits in the instruction since fixup_segment()
14458 might be deleting the relocation entry (i.e., a branch within
14459 the current segment). */
a7ebbfdf 14460 if (! fixP->fx_done)
bb2d6cd7 14461 break;
252b5132 14462
54f4ddb3 14463 /* Update old instruction data. */
4d68580a 14464 insn = read_insn (buf);
252b5132 14465
a7ebbfdf
TS
14466 if (*valP + 0x20000 <= 0x3ffff)
14467 {
14468 insn |= (*valP >> 2) & 0xffff;
4d68580a 14469 write_insn (buf, insn);
a7ebbfdf
TS
14470 }
14471 else if (mips_pic == NO_PIC
14472 && fixP->fx_done
14473 && fixP->fx_frag->fr_address >= text_section->vma
14474 && (fixP->fx_frag->fr_address
587aac4e 14475 < text_section->vma + bfd_get_section_size (text_section))
a7ebbfdf
TS
14476 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
14477 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
14478 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
14479 {
14480 /* The branch offset is too large. If this is an
14481 unconditional branch, and we are not generating PIC code,
14482 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
14483 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
14484 insn = 0x0c000000; /* jal */
252b5132 14485 else
a7ebbfdf
TS
14486 insn = 0x08000000; /* j */
14487 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
14488 fixP->fx_done = 0;
14489 fixP->fx_addsy = section_symbol (text_section);
14490 *valP += md_pcrel_from (fixP);
4d68580a 14491 write_insn (buf, insn);
a7ebbfdf
TS
14492 }
14493 else
14494 {
14495 /* If we got here, we have branch-relaxation disabled,
14496 and there's nothing we can do to fix this instruction
14497 without turning it into a longer sequence. */
14498 as_bad_where (fixP->fx_file, fixP->fx_line,
14499 _("Branch out of range"));
252b5132 14500 }
252b5132
RH
14501 break;
14502
df58fc94
RS
14503 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
14504 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
14505 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
14506 /* We adjust the offset back to even. */
14507 if ((*valP & 0x1) != 0)
14508 --(*valP);
14509
14510 if (! fixP->fx_done)
14511 break;
14512
14513 /* Should never visit here, because we keep the relocation. */
14514 abort ();
14515 break;
14516
252b5132
RH
14517 case BFD_RELOC_VTABLE_INHERIT:
14518 fixP->fx_done = 0;
14519 if (fixP->fx_addsy
14520 && !S_IS_DEFINED (fixP->fx_addsy)
14521 && !S_IS_WEAK (fixP->fx_addsy))
14522 S_SET_WEAK (fixP->fx_addsy);
14523 break;
14524
14525 case BFD_RELOC_VTABLE_ENTRY:
14526 fixP->fx_done = 0;
14527 break;
14528
14529 default:
b37df7c4 14530 abort ();
252b5132 14531 }
a7ebbfdf
TS
14532
14533 /* Remember value for tc_gen_reloc. */
14534 fixP->fx_addnumber = *valP;
252b5132
RH
14535}
14536
252b5132 14537static symbolS *
17a2f251 14538get_symbol (void)
252b5132
RH
14539{
14540 int c;
14541 char *name;
14542 symbolS *p;
14543
14544 name = input_line_pointer;
14545 c = get_symbol_end ();
14546 p = (symbolS *) symbol_find_or_make (name);
14547 *input_line_pointer = c;
14548 return p;
14549}
14550
742a56fe
RS
14551/* Align the current frag to a given power of two. If a particular
14552 fill byte should be used, FILL points to an integer that contains
14553 that byte, otherwise FILL is null.
14554
462427c4
RS
14555 This function used to have the comment:
14556
14557 The MIPS assembler also automatically adjusts any preceding label.
14558
14559 The implementation therefore applied the adjustment to a maximum of
14560 one label. However, other label adjustments are applied to batches
14561 of labels, and adjusting just one caused problems when new labels
14562 were added for the sake of debugging or unwind information.
14563 We therefore adjust all preceding labels (given as LABELS) instead. */
252b5132
RH
14564
14565static void
462427c4 14566mips_align (int to, int *fill, struct insn_label_list *labels)
252b5132 14567{
7d10b47d 14568 mips_emit_delays ();
df58fc94 14569 mips_record_compressed_mode ();
742a56fe
RS
14570 if (fill == NULL && subseg_text_p (now_seg))
14571 frag_align_code (to, 0);
14572 else
14573 frag_align (to, fill ? *fill : 0, 0);
252b5132 14574 record_alignment (now_seg, to);
462427c4 14575 mips_move_labels (labels, FALSE);
252b5132
RH
14576}
14577
14578/* Align to a given power of two. .align 0 turns off the automatic
14579 alignment used by the data creating pseudo-ops. */
14580
14581static void
17a2f251 14582s_align (int x ATTRIBUTE_UNUSED)
252b5132 14583{
742a56fe 14584 int temp, fill_value, *fill_ptr;
49954fb4 14585 long max_alignment = 28;
252b5132 14586
54f4ddb3 14587 /* o Note that the assembler pulls down any immediately preceding label
252b5132 14588 to the aligned address.
54f4ddb3 14589 o It's not documented but auto alignment is reinstated by
252b5132 14590 a .align pseudo instruction.
54f4ddb3 14591 o Note also that after auto alignment is turned off the mips assembler
252b5132 14592 issues an error on attempt to assemble an improperly aligned data item.
54f4ddb3 14593 We don't. */
252b5132
RH
14594
14595 temp = get_absolute_expression ();
14596 if (temp > max_alignment)
14597 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
14598 else if (temp < 0)
14599 {
14600 as_warn (_("Alignment negative: 0 assumed."));
14601 temp = 0;
14602 }
14603 if (*input_line_pointer == ',')
14604 {
f9419b05 14605 ++input_line_pointer;
742a56fe
RS
14606 fill_value = get_absolute_expression ();
14607 fill_ptr = &fill_value;
252b5132
RH
14608 }
14609 else
742a56fe 14610 fill_ptr = 0;
252b5132
RH
14611 if (temp)
14612 {
a8dbcb85
TS
14613 segment_info_type *si = seg_info (now_seg);
14614 struct insn_label_list *l = si->label_list;
54f4ddb3 14615 /* Auto alignment should be switched on by next section change. */
252b5132 14616 auto_align = 1;
462427c4 14617 mips_align (temp, fill_ptr, l);
252b5132
RH
14618 }
14619 else
14620 {
14621 auto_align = 0;
14622 }
14623
14624 demand_empty_rest_of_line ();
14625}
14626
252b5132 14627static void
17a2f251 14628s_change_sec (int sec)
252b5132
RH
14629{
14630 segT seg;
14631
252b5132
RH
14632 /* The ELF backend needs to know that we are changing sections, so
14633 that .previous works correctly. We could do something like check
b6ff326e 14634 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
14635 as it would not be appropriate to use it in the section changing
14636 functions in read.c, since obj-elf.c intercepts those. FIXME:
14637 This should be cleaner, somehow. */
f3ded42a 14638 obj_elf_section_change_hook ();
252b5132 14639
7d10b47d 14640 mips_emit_delays ();
6a32d874 14641
252b5132
RH
14642 switch (sec)
14643 {
14644 case 't':
14645 s_text (0);
14646 break;
14647 case 'd':
14648 s_data (0);
14649 break;
14650 case 'b':
14651 subseg_set (bss_section, (subsegT) get_absolute_expression ());
14652 demand_empty_rest_of_line ();
14653 break;
14654
14655 case 'r':
4d0d148d
TS
14656 seg = subseg_new (RDATA_SECTION_NAME,
14657 (subsegT) get_absolute_expression ());
f3ded42a
RS
14658 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
14659 | SEC_READONLY | SEC_RELOC
14660 | SEC_DATA));
14661 if (strncmp (TARGET_OS, "elf", 3) != 0)
14662 record_alignment (seg, 4);
4d0d148d 14663 demand_empty_rest_of_line ();
252b5132
RH
14664 break;
14665
14666 case 's':
4d0d148d 14667 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
f3ded42a
RS
14668 bfd_set_section_flags (stdoutput, seg,
14669 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
14670 if (strncmp (TARGET_OS, "elf", 3) != 0)
14671 record_alignment (seg, 4);
4d0d148d
TS
14672 demand_empty_rest_of_line ();
14673 break;
998b3c36
MR
14674
14675 case 'B':
14676 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
f3ded42a
RS
14677 bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
14678 if (strncmp (TARGET_OS, "elf", 3) != 0)
14679 record_alignment (seg, 4);
998b3c36
MR
14680 demand_empty_rest_of_line ();
14681 break;
252b5132
RH
14682 }
14683
14684 auto_align = 1;
14685}
b34976b6 14686
cca86cc8 14687void
17a2f251 14688s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 14689{
cca86cc8
SC
14690 char *section_name;
14691 char c;
684022ea 14692 char next_c = 0;
cca86cc8
SC
14693 int section_type;
14694 int section_flag;
14695 int section_entry_size;
14696 int section_alignment;
b34976b6 14697
cca86cc8
SC
14698 section_name = input_line_pointer;
14699 c = get_symbol_end ();
a816d1ed
AO
14700 if (c)
14701 next_c = *(input_line_pointer + 1);
cca86cc8 14702
4cf0dd0d
TS
14703 /* Do we have .section Name<,"flags">? */
14704 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 14705 {
4cf0dd0d
TS
14706 /* just after name is now '\0'. */
14707 *input_line_pointer = c;
cca86cc8
SC
14708 input_line_pointer = section_name;
14709 obj_elf_section (ignore);
14710 return;
14711 }
14712 input_line_pointer++;
14713
14714 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
14715 if (c == ',')
14716 section_type = get_absolute_expression ();
14717 else
14718 section_type = 0;
14719 if (*input_line_pointer++ == ',')
14720 section_flag = get_absolute_expression ();
14721 else
14722 section_flag = 0;
14723 if (*input_line_pointer++ == ',')
14724 section_entry_size = get_absolute_expression ();
14725 else
14726 section_entry_size = 0;
14727 if (*input_line_pointer++ == ',')
14728 section_alignment = get_absolute_expression ();
14729 else
14730 section_alignment = 0;
87975d2a
AM
14731 /* FIXME: really ignore? */
14732 (void) section_alignment;
cca86cc8 14733
a816d1ed
AO
14734 section_name = xstrdup (section_name);
14735
8ab8a5c8
RS
14736 /* When using the generic form of .section (as implemented by obj-elf.c),
14737 there's no way to set the section type to SHT_MIPS_DWARF. Users have
14738 traditionally had to fall back on the more common @progbits instead.
14739
14740 There's nothing really harmful in this, since bfd will correct
14741 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
708587a4 14742 means that, for backwards compatibility, the special_section entries
8ab8a5c8
RS
14743 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
14744
14745 Even so, we shouldn't force users of the MIPS .section syntax to
14746 incorrectly label the sections as SHT_PROGBITS. The best compromise
14747 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
14748 generic type-checking code. */
14749 if (section_type == SHT_MIPS_DWARF)
14750 section_type = SHT_PROGBITS;
14751
cca86cc8
SC
14752 obj_elf_change_section (section_name, section_type, section_flag,
14753 section_entry_size, 0, 0, 0);
a816d1ed
AO
14754
14755 if (now_seg->name != section_name)
14756 free (section_name);
cca86cc8 14757}
252b5132
RH
14758
14759void
17a2f251 14760mips_enable_auto_align (void)
252b5132
RH
14761{
14762 auto_align = 1;
14763}
14764
14765static void
17a2f251 14766s_cons (int log_size)
252b5132 14767{
a8dbcb85
TS
14768 segment_info_type *si = seg_info (now_seg);
14769 struct insn_label_list *l = si->label_list;
252b5132 14770
7d10b47d 14771 mips_emit_delays ();
252b5132 14772 if (log_size > 0 && auto_align)
462427c4 14773 mips_align (log_size, 0, l);
252b5132 14774 cons (1 << log_size);
a1facbec 14775 mips_clear_insn_labels ();
252b5132
RH
14776}
14777
14778static void
17a2f251 14779s_float_cons (int type)
252b5132 14780{
a8dbcb85
TS
14781 segment_info_type *si = seg_info (now_seg);
14782 struct insn_label_list *l = si->label_list;
252b5132 14783
7d10b47d 14784 mips_emit_delays ();
252b5132
RH
14785
14786 if (auto_align)
49309057
ILT
14787 {
14788 if (type == 'd')
462427c4 14789 mips_align (3, 0, l);
49309057 14790 else
462427c4 14791 mips_align (2, 0, l);
49309057 14792 }
252b5132 14793
252b5132 14794 float_cons (type);
a1facbec 14795 mips_clear_insn_labels ();
252b5132
RH
14796}
14797
14798/* Handle .globl. We need to override it because on Irix 5 you are
14799 permitted to say
14800 .globl foo .text
14801 where foo is an undefined symbol, to mean that foo should be
14802 considered to be the address of a function. */
14803
14804static void
17a2f251 14805s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
14806{
14807 char *name;
14808 int c;
14809 symbolS *symbolP;
14810 flagword flag;
14811
8a06b769 14812 do
252b5132 14813 {
8a06b769 14814 name = input_line_pointer;
252b5132 14815 c = get_symbol_end ();
8a06b769
TS
14816 symbolP = symbol_find_or_make (name);
14817 S_SET_EXTERNAL (symbolP);
14818
252b5132 14819 *input_line_pointer = c;
8a06b769 14820 SKIP_WHITESPACE ();
252b5132 14821
8a06b769
TS
14822 /* On Irix 5, every global symbol that is not explicitly labelled as
14823 being a function is apparently labelled as being an object. */
14824 flag = BSF_OBJECT;
252b5132 14825
8a06b769
TS
14826 if (!is_end_of_line[(unsigned char) *input_line_pointer]
14827 && (*input_line_pointer != ','))
14828 {
14829 char *secname;
14830 asection *sec;
14831
14832 secname = input_line_pointer;
14833 c = get_symbol_end ();
14834 sec = bfd_get_section_by_name (stdoutput, secname);
14835 if (sec == NULL)
14836 as_bad (_("%s: no such section"), secname);
14837 *input_line_pointer = c;
14838
14839 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
14840 flag = BSF_FUNCTION;
14841 }
14842
14843 symbol_get_bfdsym (symbolP)->flags |= flag;
14844
14845 c = *input_line_pointer;
14846 if (c == ',')
14847 {
14848 input_line_pointer++;
14849 SKIP_WHITESPACE ();
14850 if (is_end_of_line[(unsigned char) *input_line_pointer])
14851 c = '\n';
14852 }
14853 }
14854 while (c == ',');
252b5132 14855
252b5132
RH
14856 demand_empty_rest_of_line ();
14857}
14858
14859static void
17a2f251 14860s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
14861{
14862 char *opt;
14863 char c;
14864
14865 opt = input_line_pointer;
14866 c = get_symbol_end ();
14867
14868 if (*opt == 'O')
14869 {
14870 /* FIXME: What does this mean? */
14871 }
14872 else if (strncmp (opt, "pic", 3) == 0)
14873 {
14874 int i;
14875
14876 i = atoi (opt + 3);
14877 if (i == 0)
14878 mips_pic = NO_PIC;
14879 else if (i == 2)
143d77c5 14880 {
8b828383 14881 mips_pic = SVR4_PIC;
143d77c5
EC
14882 mips_abicalls = TRUE;
14883 }
252b5132
RH
14884 else
14885 as_bad (_(".option pic%d not supported"), i);
14886
4d0d148d 14887 if (mips_pic == SVR4_PIC)
252b5132
RH
14888 {
14889 if (g_switch_seen && g_switch_value != 0)
14890 as_warn (_("-G may not be used with SVR4 PIC code"));
14891 g_switch_value = 0;
14892 bfd_set_gp_size (stdoutput, 0);
14893 }
14894 }
14895 else
14896 as_warn (_("Unrecognized option \"%s\""), opt);
14897
14898 *input_line_pointer = c;
14899 demand_empty_rest_of_line ();
14900}
14901
14902/* This structure is used to hold a stack of .set values. */
14903
e972090a
NC
14904struct mips_option_stack
14905{
252b5132
RH
14906 struct mips_option_stack *next;
14907 struct mips_set_options options;
14908};
14909
14910static struct mips_option_stack *mips_opts_stack;
14911
14912/* Handle the .set pseudo-op. */
14913
14914static void
17a2f251 14915s_mipsset (int x ATTRIBUTE_UNUSED)
252b5132
RH
14916{
14917 char *name = input_line_pointer, ch;
c6278170 14918 const struct mips_ase *ase;
252b5132
RH
14919
14920 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 14921 ++input_line_pointer;
252b5132
RH
14922 ch = *input_line_pointer;
14923 *input_line_pointer = '\0';
14924
14925 if (strcmp (name, "reorder") == 0)
14926 {
7d10b47d
RS
14927 if (mips_opts.noreorder)
14928 end_noreorder ();
252b5132
RH
14929 }
14930 else if (strcmp (name, "noreorder") == 0)
14931 {
7d10b47d
RS
14932 if (!mips_opts.noreorder)
14933 start_noreorder ();
252b5132 14934 }
741fe287
MR
14935 else if (strncmp (name, "at=", 3) == 0)
14936 {
14937 char *s = name + 3;
14938
14939 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
14940 as_bad (_("Unrecognized register name `%s'"), s);
14941 }
252b5132
RH
14942 else if (strcmp (name, "at") == 0)
14943 {
741fe287 14944 mips_opts.at = ATREG;
252b5132
RH
14945 }
14946 else if (strcmp (name, "noat") == 0)
14947 {
741fe287 14948 mips_opts.at = ZERO;
252b5132
RH
14949 }
14950 else if (strcmp (name, "macro") == 0)
14951 {
14952 mips_opts.warn_about_macros = 0;
14953 }
14954 else if (strcmp (name, "nomacro") == 0)
14955 {
14956 if (mips_opts.noreorder == 0)
14957 as_bad (_("`noreorder' must be set before `nomacro'"));
14958 mips_opts.warn_about_macros = 1;
14959 }
14960 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
14961 {
14962 mips_opts.nomove = 0;
14963 }
14964 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
14965 {
14966 mips_opts.nomove = 1;
14967 }
14968 else if (strcmp (name, "bopt") == 0)
14969 {
14970 mips_opts.nobopt = 0;
14971 }
14972 else if (strcmp (name, "nobopt") == 0)
14973 {
14974 mips_opts.nobopt = 1;
14975 }
ad3fea08
TS
14976 else if (strcmp (name, "gp=default") == 0)
14977 mips_opts.gp32 = file_mips_gp32;
14978 else if (strcmp (name, "gp=32") == 0)
14979 mips_opts.gp32 = 1;
14980 else if (strcmp (name, "gp=64") == 0)
14981 {
14982 if (!ISA_HAS_64BIT_REGS (mips_opts.isa))
20203fb9 14983 as_warn (_("%s isa does not support 64-bit registers"),
ad3fea08
TS
14984 mips_cpu_info_from_isa (mips_opts.isa)->name);
14985 mips_opts.gp32 = 0;
14986 }
14987 else if (strcmp (name, "fp=default") == 0)
14988 mips_opts.fp32 = file_mips_fp32;
14989 else if (strcmp (name, "fp=32") == 0)
14990 mips_opts.fp32 = 1;
14991 else if (strcmp (name, "fp=64") == 0)
14992 {
14993 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
20203fb9 14994 as_warn (_("%s isa does not support 64-bit floating point registers"),
ad3fea08
TS
14995 mips_cpu_info_from_isa (mips_opts.isa)->name);
14996 mips_opts.fp32 = 0;
14997 }
037b32b9
AN
14998 else if (strcmp (name, "softfloat") == 0)
14999 mips_opts.soft_float = 1;
15000 else if (strcmp (name, "hardfloat") == 0)
15001 mips_opts.soft_float = 0;
15002 else if (strcmp (name, "singlefloat") == 0)
15003 mips_opts.single_float = 1;
15004 else if (strcmp (name, "doublefloat") == 0)
15005 mips_opts.single_float = 0;
252b5132
RH
15006 else if (strcmp (name, "mips16") == 0
15007 || strcmp (name, "MIPS-16") == 0)
df58fc94
RS
15008 {
15009 if (mips_opts.micromips == 1)
15010 as_fatal (_("`mips16' cannot be used with `micromips'"));
15011 mips_opts.mips16 = 1;
15012 }
252b5132
RH
15013 else if (strcmp (name, "nomips16") == 0
15014 || strcmp (name, "noMIPS-16") == 0)
15015 mips_opts.mips16 = 0;
df58fc94
RS
15016 else if (strcmp (name, "micromips") == 0)
15017 {
15018 if (mips_opts.mips16 == 1)
15019 as_fatal (_("`micromips' cannot be used with `mips16'"));
15020 mips_opts.micromips = 1;
15021 }
15022 else if (strcmp (name, "nomicromips") == 0)
15023 mips_opts.micromips = 0;
c6278170
RS
15024 else if (name[0] == 'n'
15025 && name[1] == 'o'
15026 && (ase = mips_lookup_ase (name + 2)))
15027 mips_set_ase (ase, FALSE);
15028 else if ((ase = mips_lookup_ase (name)))
15029 mips_set_ase (ase, TRUE);
1a2c1fad 15030 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 15031 {
af7ee8bf 15032 int reset = 0;
252b5132 15033
1a2c1fad
CD
15034 /* Permit the user to change the ISA and architecture on the fly.
15035 Needless to say, misuse can cause serious problems. */
81a21e38 15036 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
af7ee8bf
CD
15037 {
15038 reset = 1;
15039 mips_opts.isa = file_mips_isa;
1a2c1fad 15040 mips_opts.arch = file_mips_arch;
1a2c1fad
CD
15041 }
15042 else if (strncmp (name, "arch=", 5) == 0)
15043 {
15044 const struct mips_cpu_info *p;
15045
15046 p = mips_parse_cpu("internal use", name + 5);
15047 if (!p)
15048 as_bad (_("unknown architecture %s"), name + 5);
15049 else
15050 {
15051 mips_opts.arch = p->cpu;
15052 mips_opts.isa = p->isa;
15053 }
15054 }
81a21e38
TS
15055 else if (strncmp (name, "mips", 4) == 0)
15056 {
15057 const struct mips_cpu_info *p;
15058
15059 p = mips_parse_cpu("internal use", name);
15060 if (!p)
15061 as_bad (_("unknown ISA level %s"), name + 4);
15062 else
15063 {
15064 mips_opts.arch = p->cpu;
15065 mips_opts.isa = p->isa;
15066 }
15067 }
af7ee8bf 15068 else
81a21e38 15069 as_bad (_("unknown ISA or architecture %s"), name);
af7ee8bf
CD
15070
15071 switch (mips_opts.isa)
98d3f06f
KH
15072 {
15073 case 0:
98d3f06f 15074 break;
af7ee8bf
CD
15075 case ISA_MIPS1:
15076 case ISA_MIPS2:
15077 case ISA_MIPS32:
15078 case ISA_MIPS32R2:
98d3f06f
KH
15079 mips_opts.gp32 = 1;
15080 mips_opts.fp32 = 1;
15081 break;
af7ee8bf
CD
15082 case ISA_MIPS3:
15083 case ISA_MIPS4:
15084 case ISA_MIPS5:
15085 case ISA_MIPS64:
5f74bc13 15086 case ISA_MIPS64R2:
98d3f06f 15087 mips_opts.gp32 = 0;
e407c74b
NC
15088 if (mips_opts.arch == CPU_R5900)
15089 {
15090 mips_opts.fp32 = 1;
15091 }
15092 else
15093 {
98d3f06f 15094 mips_opts.fp32 = 0;
e407c74b 15095 }
98d3f06f
KH
15096 break;
15097 default:
15098 as_bad (_("unknown ISA level %s"), name + 4);
15099 break;
15100 }
af7ee8bf 15101 if (reset)
98d3f06f 15102 {
af7ee8bf
CD
15103 mips_opts.gp32 = file_mips_gp32;
15104 mips_opts.fp32 = file_mips_fp32;
98d3f06f 15105 }
252b5132
RH
15106 }
15107 else if (strcmp (name, "autoextend") == 0)
15108 mips_opts.noautoextend = 0;
15109 else if (strcmp (name, "noautoextend") == 0)
15110 mips_opts.noautoextend = 1;
833794fc
MR
15111 else if (strcmp (name, "insn32") == 0)
15112 mips_opts.insn32 = TRUE;
15113 else if (strcmp (name, "noinsn32") == 0)
15114 mips_opts.insn32 = FALSE;
252b5132
RH
15115 else if (strcmp (name, "push") == 0)
15116 {
15117 struct mips_option_stack *s;
15118
15119 s = (struct mips_option_stack *) xmalloc (sizeof *s);
15120 s->next = mips_opts_stack;
15121 s->options = mips_opts;
15122 mips_opts_stack = s;
15123 }
15124 else if (strcmp (name, "pop") == 0)
15125 {
15126 struct mips_option_stack *s;
15127
15128 s = mips_opts_stack;
15129 if (s == NULL)
15130 as_bad (_(".set pop with no .set push"));
15131 else
15132 {
15133 /* If we're changing the reorder mode we need to handle
15134 delay slots correctly. */
15135 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 15136 start_noreorder ();
252b5132 15137 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 15138 end_noreorder ();
252b5132
RH
15139
15140 mips_opts = s->options;
15141 mips_opts_stack = s->next;
15142 free (s);
15143 }
15144 }
aed1a261
RS
15145 else if (strcmp (name, "sym32") == 0)
15146 mips_opts.sym32 = TRUE;
15147 else if (strcmp (name, "nosym32") == 0)
15148 mips_opts.sym32 = FALSE;
e6559e01
JM
15149 else if (strchr (name, ','))
15150 {
15151 /* Generic ".set" directive; use the generic handler. */
15152 *input_line_pointer = ch;
15153 input_line_pointer = name;
15154 s_set (0);
15155 return;
15156 }
252b5132
RH
15157 else
15158 {
15159 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
15160 }
c6278170 15161 mips_check_isa_supports_ases ();
252b5132
RH
15162 *input_line_pointer = ch;
15163 demand_empty_rest_of_line ();
15164}
15165
15166/* Handle the .abicalls pseudo-op. I believe this is equivalent to
15167 .option pic2. It means to generate SVR4 PIC calls. */
15168
15169static void
17a2f251 15170s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
15171{
15172 mips_pic = SVR4_PIC;
143d77c5 15173 mips_abicalls = TRUE;
4d0d148d
TS
15174
15175 if (g_switch_seen && g_switch_value != 0)
15176 as_warn (_("-G may not be used with SVR4 PIC code"));
15177 g_switch_value = 0;
15178
252b5132
RH
15179 bfd_set_gp_size (stdoutput, 0);
15180 demand_empty_rest_of_line ();
15181}
15182
15183/* Handle the .cpload pseudo-op. This is used when generating SVR4
15184 PIC code. It sets the $gp register for the function based on the
15185 function address, which is in the register named in the argument.
15186 This uses a relocation against _gp_disp, which is handled specially
15187 by the linker. The result is:
15188 lui $gp,%hi(_gp_disp)
15189 addiu $gp,$gp,%lo(_gp_disp)
15190 addu $gp,$gp,.cpload argument
aa6975fb
ILT
15191 The .cpload argument is normally $25 == $t9.
15192
15193 The -mno-shared option changes this to:
bbe506e8
TS
15194 lui $gp,%hi(__gnu_local_gp)
15195 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
15196 and the argument is ignored. This saves an instruction, but the
15197 resulting code is not position independent; it uses an absolute
bbe506e8
TS
15198 address for __gnu_local_gp. Thus code assembled with -mno-shared
15199 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
15200
15201static void
17a2f251 15202s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
15203{
15204 expressionS ex;
aa6975fb
ILT
15205 int reg;
15206 int in_shared;
252b5132 15207
6478892d
TS
15208 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
15209 .cpload is ignored. */
15210 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
15211 {
15212 s_ignore (0);
15213 return;
15214 }
15215
a276b80c
MR
15216 if (mips_opts.mips16)
15217 {
15218 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
15219 ignore_rest_of_line ();
15220 return;
15221 }
15222
d3ecfc59 15223 /* .cpload should be in a .set noreorder section. */
252b5132
RH
15224 if (mips_opts.noreorder == 0)
15225 as_warn (_(".cpload not in noreorder section"));
15226
aa6975fb
ILT
15227 reg = tc_get_register (0);
15228
15229 /* If we need to produce a 64-bit address, we are better off using
15230 the default instruction sequence. */
aed1a261 15231 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 15232
252b5132 15233 ex.X_op = O_symbol;
bbe506e8
TS
15234 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
15235 "__gnu_local_gp");
252b5132
RH
15236 ex.X_op_symbol = NULL;
15237 ex.X_add_number = 0;
15238
15239 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 15240 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 15241
8a75745d
MR
15242 mips_mark_labels ();
15243 mips_assembling_insn = TRUE;
15244
584892a6 15245 macro_start ();
67c0d1eb
RS
15246 macro_build_lui (&ex, mips_gp_register);
15247 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 15248 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
15249 if (in_shared)
15250 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
15251 mips_gp_register, reg);
584892a6 15252 macro_end ();
252b5132 15253
8a75745d 15254 mips_assembling_insn = FALSE;
252b5132
RH
15255 demand_empty_rest_of_line ();
15256}
15257
6478892d
TS
15258/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
15259 .cpsetup $reg1, offset|$reg2, label
15260
15261 If offset is given, this results in:
15262 sd $gp, offset($sp)
956cd1d6 15263 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
15264 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
15265 daddu $gp, $gp, $reg1
6478892d
TS
15266
15267 If $reg2 is given, this results in:
15268 daddu $reg2, $gp, $0
956cd1d6 15269 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
15270 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
15271 daddu $gp, $gp, $reg1
aa6975fb
ILT
15272 $reg1 is normally $25 == $t9.
15273
15274 The -mno-shared option replaces the last three instructions with
15275 lui $gp,%hi(_gp)
54f4ddb3 15276 addiu $gp,$gp,%lo(_gp) */
aa6975fb 15277
6478892d 15278static void
17a2f251 15279s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
15280{
15281 expressionS ex_off;
15282 expressionS ex_sym;
15283 int reg1;
6478892d 15284
8586fc66 15285 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
15286 We also need NewABI support. */
15287 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
15288 {
15289 s_ignore (0);
15290 return;
15291 }
15292
a276b80c
MR
15293 if (mips_opts.mips16)
15294 {
15295 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
15296 ignore_rest_of_line ();
15297 return;
15298 }
15299
6478892d
TS
15300 reg1 = tc_get_register (0);
15301 SKIP_WHITESPACE ();
15302 if (*input_line_pointer != ',')
15303 {
15304 as_bad (_("missing argument separator ',' for .cpsetup"));
15305 return;
15306 }
15307 else
80245285 15308 ++input_line_pointer;
6478892d
TS
15309 SKIP_WHITESPACE ();
15310 if (*input_line_pointer == '$')
80245285
TS
15311 {
15312 mips_cpreturn_register = tc_get_register (0);
15313 mips_cpreturn_offset = -1;
15314 }
6478892d 15315 else
80245285
TS
15316 {
15317 mips_cpreturn_offset = get_absolute_expression ();
15318 mips_cpreturn_register = -1;
15319 }
6478892d
TS
15320 SKIP_WHITESPACE ();
15321 if (*input_line_pointer != ',')
15322 {
15323 as_bad (_("missing argument separator ',' for .cpsetup"));
15324 return;
15325 }
15326 else
f9419b05 15327 ++input_line_pointer;
6478892d 15328 SKIP_WHITESPACE ();
f21f8242 15329 expression (&ex_sym);
6478892d 15330
8a75745d
MR
15331 mips_mark_labels ();
15332 mips_assembling_insn = TRUE;
15333
584892a6 15334 macro_start ();
6478892d
TS
15335 if (mips_cpreturn_register == -1)
15336 {
15337 ex_off.X_op = O_constant;
15338 ex_off.X_add_symbol = NULL;
15339 ex_off.X_op_symbol = NULL;
15340 ex_off.X_add_number = mips_cpreturn_offset;
15341
67c0d1eb 15342 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 15343 BFD_RELOC_LO16, SP);
6478892d
TS
15344 }
15345 else
67c0d1eb 15346 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
17a2f251 15347 mips_gp_register, 0);
6478892d 15348
aed1a261 15349 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb 15350 {
df58fc94 15351 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
aa6975fb
ILT
15352 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
15353 BFD_RELOC_HI16_S);
15354
15355 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
15356 mips_gp_register, -1, BFD_RELOC_GPREL16,
15357 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
15358
15359 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
15360 mips_gp_register, reg1);
15361 }
15362 else
15363 {
15364 expressionS ex;
15365
15366 ex.X_op = O_symbol;
4184909a 15367 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
15368 ex.X_op_symbol = NULL;
15369 ex.X_add_number = 0;
6e1304d8 15370
aa6975fb
ILT
15371 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
15372 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
15373
15374 macro_build_lui (&ex, mips_gp_register);
15375 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
15376 mips_gp_register, BFD_RELOC_LO16);
15377 }
f21f8242 15378
584892a6 15379 macro_end ();
6478892d 15380
8a75745d 15381 mips_assembling_insn = FALSE;
6478892d
TS
15382 demand_empty_rest_of_line ();
15383}
15384
15385static void
17a2f251 15386s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
15387{
15388 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
54f4ddb3 15389 .cplocal is ignored. */
6478892d
TS
15390 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
15391 {
15392 s_ignore (0);
15393 return;
15394 }
15395
a276b80c
MR
15396 if (mips_opts.mips16)
15397 {
15398 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
15399 ignore_rest_of_line ();
15400 return;
15401 }
15402
6478892d 15403 mips_gp_register = tc_get_register (0);
85b51719 15404 demand_empty_rest_of_line ();
6478892d
TS
15405}
15406
252b5132
RH
15407/* Handle the .cprestore pseudo-op. This stores $gp into a given
15408 offset from $sp. The offset is remembered, and after making a PIC
15409 call $gp is restored from that location. */
15410
15411static void
17a2f251 15412s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
15413{
15414 expressionS ex;
252b5132 15415
6478892d 15416 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 15417 .cprestore is ignored. */
6478892d 15418 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
15419 {
15420 s_ignore (0);
15421 return;
15422 }
15423
a276b80c
MR
15424 if (mips_opts.mips16)
15425 {
15426 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
15427 ignore_rest_of_line ();
15428 return;
15429 }
15430
252b5132 15431 mips_cprestore_offset = get_absolute_expression ();
7a621144 15432 mips_cprestore_valid = 1;
252b5132
RH
15433
15434 ex.X_op = O_constant;
15435 ex.X_add_symbol = NULL;
15436 ex.X_op_symbol = NULL;
15437 ex.X_add_number = mips_cprestore_offset;
15438
8a75745d
MR
15439 mips_mark_labels ();
15440 mips_assembling_insn = TRUE;
15441
584892a6 15442 macro_start ();
67c0d1eb
RS
15443 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
15444 SP, HAVE_64BIT_ADDRESSES);
584892a6 15445 macro_end ();
252b5132 15446
8a75745d 15447 mips_assembling_insn = FALSE;
252b5132
RH
15448 demand_empty_rest_of_line ();
15449}
15450
6478892d 15451/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 15452 was given in the preceding .cpsetup, it results in:
6478892d 15453 ld $gp, offset($sp)
76b3015f 15454
6478892d 15455 If a register $reg2 was given there, it results in:
54f4ddb3
TS
15456 daddu $gp, $reg2, $0 */
15457
6478892d 15458static void
17a2f251 15459s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
15460{
15461 expressionS ex;
6478892d
TS
15462
15463 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
15464 We also need NewABI support. */
15465 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
15466 {
15467 s_ignore (0);
15468 return;
15469 }
15470
a276b80c
MR
15471 if (mips_opts.mips16)
15472 {
15473 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
15474 ignore_rest_of_line ();
15475 return;
15476 }
15477
8a75745d
MR
15478 mips_mark_labels ();
15479 mips_assembling_insn = TRUE;
15480
584892a6 15481 macro_start ();
6478892d
TS
15482 if (mips_cpreturn_register == -1)
15483 {
15484 ex.X_op = O_constant;
15485 ex.X_add_symbol = NULL;
15486 ex.X_op_symbol = NULL;
15487 ex.X_add_number = mips_cpreturn_offset;
15488
67c0d1eb 15489 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
15490 }
15491 else
67c0d1eb 15492 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
17a2f251 15493 mips_cpreturn_register, 0);
584892a6 15494 macro_end ();
6478892d 15495
8a75745d 15496 mips_assembling_insn = FALSE;
6478892d
TS
15497 demand_empty_rest_of_line ();
15498}
15499
d0f13682
CLT
15500/* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
15501 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
15502 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
15503 debug information or MIPS16 TLS. */
741d6ea8
JM
15504
15505static void
d0f13682
CLT
15506s_tls_rel_directive (const size_t bytes, const char *dirstr,
15507 bfd_reloc_code_real_type rtype)
741d6ea8
JM
15508{
15509 expressionS ex;
15510 char *p;
15511
15512 expression (&ex);
15513
15514 if (ex.X_op != O_symbol)
15515 {
d0f13682 15516 as_bad (_("Unsupported use of %s"), dirstr);
741d6ea8
JM
15517 ignore_rest_of_line ();
15518 }
15519
15520 p = frag_more (bytes);
15521 md_number_to_chars (p, 0, bytes);
d0f13682 15522 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
741d6ea8 15523 demand_empty_rest_of_line ();
de64cffd 15524 mips_clear_insn_labels ();
741d6ea8
JM
15525}
15526
15527/* Handle .dtprelword. */
15528
15529static void
15530s_dtprelword (int ignore ATTRIBUTE_UNUSED)
15531{
d0f13682 15532 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
741d6ea8
JM
15533}
15534
15535/* Handle .dtpreldword. */
15536
15537static void
15538s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
15539{
d0f13682
CLT
15540 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
15541}
15542
15543/* Handle .tprelword. */
15544
15545static void
15546s_tprelword (int ignore ATTRIBUTE_UNUSED)
15547{
15548 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
15549}
15550
15551/* Handle .tpreldword. */
15552
15553static void
15554s_tpreldword (int ignore ATTRIBUTE_UNUSED)
15555{
15556 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
741d6ea8
JM
15557}
15558
6478892d
TS
15559/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
15560 code. It sets the offset to use in gp_rel relocations. */
15561
15562static void
17a2f251 15563s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
15564{
15565 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
15566 We also need NewABI support. */
15567 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
15568 {
15569 s_ignore (0);
15570 return;
15571 }
15572
def2e0dd 15573 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
15574
15575 demand_empty_rest_of_line ();
15576}
15577
252b5132
RH
15578/* Handle the .gpword pseudo-op. This is used when generating PIC
15579 code. It generates a 32 bit GP relative reloc. */
15580
15581static void
17a2f251 15582s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132 15583{
a8dbcb85
TS
15584 segment_info_type *si;
15585 struct insn_label_list *l;
252b5132
RH
15586 expressionS ex;
15587 char *p;
15588
15589 /* When not generating PIC code, this is treated as .word. */
15590 if (mips_pic != SVR4_PIC)
15591 {
15592 s_cons (2);
15593 return;
15594 }
15595
a8dbcb85
TS
15596 si = seg_info (now_seg);
15597 l = si->label_list;
7d10b47d 15598 mips_emit_delays ();
252b5132 15599 if (auto_align)
462427c4 15600 mips_align (2, 0, l);
252b5132
RH
15601
15602 expression (&ex);
a1facbec 15603 mips_clear_insn_labels ();
252b5132
RH
15604
15605 if (ex.X_op != O_symbol || ex.X_add_number != 0)
15606 {
15607 as_bad (_("Unsupported use of .gpword"));
15608 ignore_rest_of_line ();
15609 }
15610
15611 p = frag_more (4);
17a2f251 15612 md_number_to_chars (p, 0, 4);
b34976b6 15613 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 15614 BFD_RELOC_GPREL32);
252b5132
RH
15615
15616 demand_empty_rest_of_line ();
15617}
15618
10181a0d 15619static void
17a2f251 15620s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d 15621{
a8dbcb85
TS
15622 segment_info_type *si;
15623 struct insn_label_list *l;
10181a0d
AO
15624 expressionS ex;
15625 char *p;
15626
15627 /* When not generating PIC code, this is treated as .dword. */
15628 if (mips_pic != SVR4_PIC)
15629 {
15630 s_cons (3);
15631 return;
15632 }
15633
a8dbcb85
TS
15634 si = seg_info (now_seg);
15635 l = si->label_list;
7d10b47d 15636 mips_emit_delays ();
10181a0d 15637 if (auto_align)
462427c4 15638 mips_align (3, 0, l);
10181a0d
AO
15639
15640 expression (&ex);
a1facbec 15641 mips_clear_insn_labels ();
10181a0d
AO
15642
15643 if (ex.X_op != O_symbol || ex.X_add_number != 0)
15644 {
15645 as_bad (_("Unsupported use of .gpdword"));
15646 ignore_rest_of_line ();
15647 }
15648
15649 p = frag_more (8);
17a2f251 15650 md_number_to_chars (p, 0, 8);
a105a300 15651 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
6e1304d8 15652 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
15653
15654 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8
RS
15655 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
15656 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
15657
15658 demand_empty_rest_of_line ();
15659}
15660
a3f278e2
CM
15661/* Handle the .ehword pseudo-op. This is used when generating unwinding
15662 tables. It generates a R_MIPS_EH reloc. */
15663
15664static void
15665s_ehword (int ignore ATTRIBUTE_UNUSED)
15666{
15667 expressionS ex;
15668 char *p;
15669
15670 mips_emit_delays ();
15671
15672 expression (&ex);
15673 mips_clear_insn_labels ();
15674
15675 if (ex.X_op != O_symbol || ex.X_add_number != 0)
15676 {
15677 as_bad (_("Unsupported use of .ehword"));
15678 ignore_rest_of_line ();
15679 }
15680
15681 p = frag_more (4);
15682 md_number_to_chars (p, 0, 4);
15683 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
15684 BFD_RELOC_MIPS_EH);
15685
15686 demand_empty_rest_of_line ();
15687}
15688
252b5132
RH
15689/* Handle the .cpadd pseudo-op. This is used when dealing with switch
15690 tables in SVR4 PIC code. */
15691
15692static void
17a2f251 15693s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 15694{
252b5132
RH
15695 int reg;
15696
10181a0d
AO
15697 /* This is ignored when not generating SVR4 PIC code. */
15698 if (mips_pic != SVR4_PIC)
252b5132
RH
15699 {
15700 s_ignore (0);
15701 return;
15702 }
15703
8a75745d
MR
15704 mips_mark_labels ();
15705 mips_assembling_insn = TRUE;
15706
252b5132 15707 /* Add $gp to the register named as an argument. */
584892a6 15708 macro_start ();
252b5132 15709 reg = tc_get_register (0);
67c0d1eb 15710 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 15711 macro_end ();
252b5132 15712
8a75745d 15713 mips_assembling_insn = FALSE;
bdaaa2e1 15714 demand_empty_rest_of_line ();
252b5132
RH
15715}
15716
15717/* Handle the .insn pseudo-op. This marks instruction labels in
df58fc94 15718 mips16/micromips mode. This permits the linker to handle them specially,
252b5132
RH
15719 such as generating jalx instructions when needed. We also make
15720 them odd for the duration of the assembly, in order to generate the
15721 right sort of code. We will make them even in the adjust_symtab
15722 routine, while leaving them marked. This is convenient for the
15723 debugger and the disassembler. The linker knows to make them odd
15724 again. */
15725
15726static void
17a2f251 15727s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 15728{
df58fc94 15729 mips_mark_labels ();
252b5132
RH
15730
15731 demand_empty_rest_of_line ();
15732}
15733
ba92f887
MR
15734/* Handle the .nan pseudo-op. */
15735
15736static void
15737s_nan (int ignore ATTRIBUTE_UNUSED)
15738{
15739 static const char str_legacy[] = "legacy";
15740 static const char str_2008[] = "2008";
15741 size_t i;
15742
15743 for (i = 0; !is_end_of_line[(unsigned char) input_line_pointer[i]]; i++);
15744
15745 if (i == sizeof (str_2008) - 1
15746 && memcmp (input_line_pointer, str_2008, i) == 0)
15747 mips_flag_nan2008 = TRUE;
15748 else if (i == sizeof (str_legacy) - 1
15749 && memcmp (input_line_pointer, str_legacy, i) == 0)
15750 mips_flag_nan2008 = FALSE;
15751 else
15752 as_bad (_("Bad .nan directive"));
15753
15754 input_line_pointer += i;
15755 demand_empty_rest_of_line ();
15756}
15757
754e2bb9
RS
15758/* Handle a .stab[snd] directive. Ideally these directives would be
15759 implemented in a transparent way, so that removing them would not
15760 have any effect on the generated instructions. However, s_stab
15761 internally changes the section, so in practice we need to decide
15762 now whether the preceding label marks compressed code. We do not
15763 support changing the compression mode of a label after a .stab*
15764 directive, such as in:
15765
15766 foo:
15767 .stabs ...
15768 .set mips16
15769
15770 so the current mode wins. */
252b5132
RH
15771
15772static void
17a2f251 15773s_mips_stab (int type)
252b5132 15774{
754e2bb9 15775 mips_mark_labels ();
252b5132
RH
15776 s_stab (type);
15777}
15778
54f4ddb3 15779/* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
252b5132
RH
15780
15781static void
17a2f251 15782s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
15783{
15784 char *name;
15785 int c;
15786 symbolS *symbolP;
15787 expressionS exp;
15788
15789 name = input_line_pointer;
15790 c = get_symbol_end ();
15791 symbolP = symbol_find_or_make (name);
15792 S_SET_WEAK (symbolP);
15793 *input_line_pointer = c;
15794
15795 SKIP_WHITESPACE ();
15796
15797 if (! is_end_of_line[(unsigned char) *input_line_pointer])
15798 {
15799 if (S_IS_DEFINED (symbolP))
15800 {
20203fb9 15801 as_bad (_("ignoring attempt to redefine symbol %s"),
252b5132
RH
15802 S_GET_NAME (symbolP));
15803 ignore_rest_of_line ();
15804 return;
15805 }
bdaaa2e1 15806
252b5132
RH
15807 if (*input_line_pointer == ',')
15808 {
15809 ++input_line_pointer;
15810 SKIP_WHITESPACE ();
15811 }
bdaaa2e1 15812
252b5132
RH
15813 expression (&exp);
15814 if (exp.X_op != O_symbol)
15815 {
20203fb9 15816 as_bad (_("bad .weakext directive"));
98d3f06f 15817 ignore_rest_of_line ();
252b5132
RH
15818 return;
15819 }
49309057 15820 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
15821 }
15822
15823 demand_empty_rest_of_line ();
15824}
15825
15826/* Parse a register string into a number. Called from the ECOFF code
15827 to parse .frame. The argument is non-zero if this is the frame
15828 register, so that we can record it in mips_frame_reg. */
15829
15830int
17a2f251 15831tc_get_register (int frame)
252b5132 15832{
707bfff6 15833 unsigned int reg;
252b5132
RH
15834
15835 SKIP_WHITESPACE ();
707bfff6
TS
15836 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
15837 reg = 0;
252b5132 15838 if (frame)
7a621144
DJ
15839 {
15840 mips_frame_reg = reg != 0 ? reg : SP;
15841 mips_frame_reg_valid = 1;
15842 mips_cprestore_valid = 0;
15843 }
252b5132
RH
15844 return reg;
15845}
15846
15847valueT
17a2f251 15848md_section_align (asection *seg, valueT addr)
252b5132
RH
15849{
15850 int align = bfd_get_section_alignment (stdoutput, seg);
15851
f3ded42a
RS
15852 /* We don't need to align ELF sections to the full alignment.
15853 However, Irix 5 may prefer that we align them at least to a 16
15854 byte boundary. We don't bother to align the sections if we
15855 are targeted for an embedded system. */
15856 if (strncmp (TARGET_OS, "elf", 3) == 0)
15857 return addr;
15858 if (align > 4)
15859 align = 4;
252b5132
RH
15860
15861 return ((addr + (1 << align) - 1) & (-1 << align));
15862}
15863
15864/* Utility routine, called from above as well. If called while the
15865 input file is still being read, it's only an approximation. (For
15866 example, a symbol may later become defined which appeared to be
15867 undefined earlier.) */
15868
15869static int
17a2f251 15870nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
15871{
15872 if (sym == 0)
15873 return 0;
15874
4d0d148d 15875 if (g_switch_value > 0)
252b5132
RH
15876 {
15877 const char *symname;
15878 int change;
15879
c9914766 15880 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
15881 register. It can be if it is smaller than the -G size or if
15882 it is in the .sdata or .sbss section. Certain symbols can
c9914766 15883 not be referenced off the $gp, although it appears as though
252b5132
RH
15884 they can. */
15885 symname = S_GET_NAME (sym);
15886 if (symname != (const char *) NULL
15887 && (strcmp (symname, "eprol") == 0
15888 || strcmp (symname, "etext") == 0
15889 || strcmp (symname, "_gp") == 0
15890 || strcmp (symname, "edata") == 0
15891 || strcmp (symname, "_fbss") == 0
15892 || strcmp (symname, "_fdata") == 0
15893 || strcmp (symname, "_ftext") == 0
15894 || strcmp (symname, "end") == 0
15895 || strcmp (symname, "_gp_disp") == 0))
15896 change = 1;
15897 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
15898 && (0
15899#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
15900 || (symbol_get_obj (sym)->ecoff_extern_size != 0
15901 && (symbol_get_obj (sym)->ecoff_extern_size
15902 <= g_switch_value))
252b5132
RH
15903#endif
15904 /* We must defer this decision until after the whole
15905 file has been read, since there might be a .extern
15906 after the first use of this symbol. */
15907 || (before_relaxing
15908#ifndef NO_ECOFF_DEBUGGING
49309057 15909 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
15910#endif
15911 && S_GET_VALUE (sym) == 0)
15912 || (S_GET_VALUE (sym) != 0
15913 && S_GET_VALUE (sym) <= g_switch_value)))
15914 change = 0;
15915 else
15916 {
15917 const char *segname;
15918
15919 segname = segment_name (S_GET_SEGMENT (sym));
9c2799c2 15920 gas_assert (strcmp (segname, ".lit8") != 0
252b5132
RH
15921 && strcmp (segname, ".lit4") != 0);
15922 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
15923 && strcmp (segname, ".sbss") != 0
15924 && strncmp (segname, ".sdata.", 7) != 0
d4dc2f22
TS
15925 && strncmp (segname, ".sbss.", 6) != 0
15926 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
fba2b7f9 15927 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
15928 }
15929 return change;
15930 }
15931 else
c9914766 15932 /* We are not optimizing for the $gp register. */
252b5132
RH
15933 return 1;
15934}
15935
5919d012
RS
15936
15937/* Return true if the given symbol should be considered local for SVR4 PIC. */
15938
15939static bfd_boolean
17a2f251 15940pic_need_relax (symbolS *sym, asection *segtype)
5919d012
RS
15941{
15942 asection *symsec;
5919d012
RS
15943
15944 /* Handle the case of a symbol equated to another symbol. */
15945 while (symbol_equated_reloc_p (sym))
15946 {
15947 symbolS *n;
15948
5f0fe04b 15949 /* It's possible to get a loop here in a badly written program. */
5919d012
RS
15950 n = symbol_get_value_expression (sym)->X_add_symbol;
15951 if (n == sym)
15952 break;
15953 sym = n;
15954 }
15955
df1f3cda
DD
15956 if (symbol_section_p (sym))
15957 return TRUE;
15958
5919d012
RS
15959 symsec = S_GET_SEGMENT (sym);
15960
5919d012 15961 /* This must duplicate the test in adjust_reloc_syms. */
45dfa85a
AM
15962 return (!bfd_is_und_section (symsec)
15963 && !bfd_is_abs_section (symsec)
5f0fe04b
TS
15964 && !bfd_is_com_section (symsec)
15965 && !s_is_linkonce (sym, segtype)
5919d012 15966 /* A global or weak symbol is treated as external. */
f3ded42a 15967 && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
5919d012
RS
15968}
15969
15970
252b5132
RH
15971/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
15972 extended opcode. SEC is the section the frag is in. */
15973
15974static int
17a2f251 15975mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132
RH
15976{
15977 int type;
3ccad066 15978 const struct mips_int_operand *operand;
252b5132 15979 offsetT val;
252b5132 15980 segT symsec;
98aa84af 15981 fragS *sym_frag;
252b5132
RH
15982
15983 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
15984 return 0;
15985 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
15986 return 1;
15987
15988 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
3ccad066 15989 operand = mips16_immed_operand (type, FALSE);
252b5132 15990
98aa84af 15991 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 15992 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 15993 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132 15994
3ccad066 15995 if (operand->root.type == OP_PCREL)
252b5132 15996 {
3ccad066 15997 const struct mips_pcrel_operand *pcrel_op;
252b5132 15998 addressT addr;
3ccad066 15999 offsetT maxtiny;
252b5132
RH
16000
16001 /* We won't have the section when we are called from
16002 mips_relax_frag. However, we will always have been called
16003 from md_estimate_size_before_relax first. If this is a
16004 branch to a different section, we mark it as such. If SEC is
16005 NULL, and the frag is not marked, then it must be a branch to
16006 the same section. */
3ccad066 16007 pcrel_op = (const struct mips_pcrel_operand *) operand;
252b5132
RH
16008 if (sec == NULL)
16009 {
16010 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
16011 return 1;
16012 }
16013 else
16014 {
98aa84af 16015 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
16016 if (symsec != sec)
16017 {
16018 fragp->fr_subtype =
16019 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
16020
16021 /* FIXME: We should support this, and let the linker
16022 catch branches and loads that are out of range. */
16023 as_bad_where (fragp->fr_file, fragp->fr_line,
16024 _("unsupported PC relative reference to different section"));
16025
16026 return 1;
16027 }
98aa84af
AM
16028 if (fragp != sym_frag && sym_frag->fr_address == 0)
16029 /* Assume non-extended on the first relaxation pass.
16030 The address we have calculated will be bogus if this is
16031 a forward branch to another frag, as the forward frag
16032 will have fr_address == 0. */
16033 return 0;
252b5132
RH
16034 }
16035
16036 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
16037 the same section. If the relax_marker of the symbol fragment
16038 differs from the relax_marker of this fragment, we have not
16039 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
16040 in STRETCH in order to get a better estimate of the address.
16041 This particularly matters because of the shift bits. */
16042 if (stretch != 0
98aa84af 16043 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
16044 {
16045 fragS *f;
16046
16047 /* Adjust stretch for any alignment frag. Note that if have
16048 been expanding the earlier code, the symbol may be
16049 defined in what appears to be an earlier frag. FIXME:
16050 This doesn't handle the fr_subtype field, which specifies
16051 a maximum number of bytes to skip when doing an
16052 alignment. */
98aa84af 16053 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
16054 {
16055 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
16056 {
16057 if (stretch < 0)
16058 stretch = - ((- stretch)
16059 & ~ ((1 << (int) f->fr_offset) - 1));
16060 else
16061 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
16062 if (stretch == 0)
16063 break;
16064 }
16065 }
16066 if (f != NULL)
16067 val += stretch;
16068 }
16069
16070 addr = fragp->fr_address + fragp->fr_fix;
16071
16072 /* The base address rules are complicated. The base address of
16073 a branch is the following instruction. The base address of a
16074 PC relative load or add is the instruction itself, but if it
16075 is in a delay slot (in which case it can not be extended) use
16076 the address of the instruction whose delay slot it is in. */
3ccad066 16077 if (pcrel_op->include_isa_bit)
252b5132
RH
16078 {
16079 addr += 2;
16080
16081 /* If we are currently assuming that this frag should be
16082 extended, then, the current address is two bytes
bdaaa2e1 16083 higher. */
252b5132
RH
16084 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
16085 addr += 2;
16086
16087 /* Ignore the low bit in the target, since it will be set
16088 for a text label. */
3ccad066 16089 val &= -2;
252b5132
RH
16090 }
16091 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
16092 addr -= 4;
16093 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
16094 addr -= 2;
16095
3ccad066 16096 val -= addr & -(1 << pcrel_op->align_log2);
252b5132
RH
16097
16098 /* If any of the shifted bits are set, we must use an extended
16099 opcode. If the address depends on the size of this
16100 instruction, this can lead to a loop, so we arrange to always
16101 use an extended opcode. We only check this when we are in
16102 the main relaxation loop, when SEC is NULL. */
3ccad066 16103 if ((val & ((1 << operand->shift) - 1)) != 0 && sec == NULL)
252b5132
RH
16104 {
16105 fragp->fr_subtype =
16106 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
16107 return 1;
16108 }
16109
16110 /* If we are about to mark a frag as extended because the value
3ccad066
RS
16111 is precisely the next value above maxtiny, then there is a
16112 chance of an infinite loop as in the following code:
252b5132
RH
16113 la $4,foo
16114 .skip 1020
16115 .align 2
16116 foo:
16117 In this case when the la is extended, foo is 0x3fc bytes
16118 away, so the la can be shrunk, but then foo is 0x400 away, so
16119 the la must be extended. To avoid this loop, we mark the
16120 frag as extended if it was small, and is about to become
3ccad066
RS
16121 extended with the next value above maxtiny. */
16122 maxtiny = mips_int_operand_max (operand);
16123 if (val == maxtiny + (1 << operand->shift)
252b5132
RH
16124 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
16125 && sec == NULL)
16126 {
16127 fragp->fr_subtype =
16128 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
16129 return 1;
16130 }
16131 }
16132 else if (symsec != absolute_section && sec != NULL)
16133 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
16134
3ccad066 16135 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
252b5132
RH
16136}
16137
4a6a3df4
AO
16138/* Compute the length of a branch sequence, and adjust the
16139 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
16140 worst-case length is computed, with UPDATE being used to indicate
16141 whether an unconditional (-1), branch-likely (+1) or regular (0)
16142 branch is to be computed. */
16143static int
17a2f251 16144relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 16145{
b34976b6 16146 bfd_boolean toofar;
4a6a3df4
AO
16147 int length;
16148
16149 if (fragp
16150 && S_IS_DEFINED (fragp->fr_symbol)
16151 && sec == S_GET_SEGMENT (fragp->fr_symbol))
16152 {
16153 addressT addr;
16154 offsetT val;
16155
16156 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
16157
16158 addr = fragp->fr_address + fragp->fr_fix + 4;
16159
16160 val -= addr;
16161
16162 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
16163 }
16164 else if (fragp)
16165 /* If the symbol is not defined or it's in a different segment,
16166 assume the user knows what's going on and emit a short
16167 branch. */
b34976b6 16168 toofar = FALSE;
4a6a3df4 16169 else
b34976b6 16170 toofar = TRUE;
4a6a3df4
AO
16171
16172 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
16173 fragp->fr_subtype
66b3e8da
MR
16174 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
16175 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
16176 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
16177 RELAX_BRANCH_LINK (fragp->fr_subtype),
16178 toofar);
16179
16180 length = 4;
16181 if (toofar)
16182 {
16183 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
16184 length += 8;
16185
16186 if (mips_pic != NO_PIC)
16187 {
16188 /* Additional space for PIC loading of target address. */
16189 length += 8;
16190 if (mips_opts.isa == ISA_MIPS1)
16191 /* Additional space for $at-stabilizing nop. */
16192 length += 4;
16193 }
16194
16195 /* If branch is conditional. */
16196 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
16197 length += 8;
16198 }
b34976b6 16199
4a6a3df4
AO
16200 return length;
16201}
16202
df58fc94
RS
16203/* Compute the length of a branch sequence, and adjust the
16204 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
16205 worst-case length is computed, with UPDATE being used to indicate
16206 whether an unconditional (-1), or regular (0) branch is to be
16207 computed. */
16208
16209static int
16210relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
16211{
16212 bfd_boolean toofar;
16213 int length;
16214
16215 if (fragp
16216 && S_IS_DEFINED (fragp->fr_symbol)
16217 && sec == S_GET_SEGMENT (fragp->fr_symbol))
16218 {
16219 addressT addr;
16220 offsetT val;
16221
16222 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
16223 /* Ignore the low bit in the target, since it will be set
16224 for a text label. */
16225 if ((val & 1) != 0)
16226 --val;
16227
16228 addr = fragp->fr_address + fragp->fr_fix + 4;
16229
16230 val -= addr;
16231
16232 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
16233 }
16234 else if (fragp)
16235 /* If the symbol is not defined or it's in a different segment,
16236 assume the user knows what's going on and emit a short
16237 branch. */
16238 toofar = FALSE;
16239 else
16240 toofar = TRUE;
16241
16242 if (fragp && update
16243 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
16244 fragp->fr_subtype = (toofar
16245 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
16246 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
16247
16248 length = 4;
16249 if (toofar)
16250 {
16251 bfd_boolean compact_known = fragp != NULL;
16252 bfd_boolean compact = FALSE;
16253 bfd_boolean uncond;
16254
16255 if (compact_known)
16256 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
16257 if (fragp)
16258 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
16259 else
16260 uncond = update < 0;
16261
16262 /* If label is out of range, we turn branch <br>:
16263
16264 <br> label # 4 bytes
16265 0:
16266
16267 into:
16268
16269 j label # 4 bytes
16270 nop # 2 bytes if compact && !PIC
16271 0:
16272 */
16273 if (mips_pic == NO_PIC && (!compact_known || compact))
16274 length += 2;
16275
16276 /* If assembling PIC code, we further turn:
16277
16278 j label # 4 bytes
16279
16280 into:
16281
16282 lw/ld at, %got(label)(gp) # 4 bytes
16283 d/addiu at, %lo(label) # 4 bytes
16284 jr/c at # 2 bytes
16285 */
16286 if (mips_pic != NO_PIC)
16287 length += 6;
16288
16289 /* If branch <br> is conditional, we prepend negated branch <brneg>:
16290
16291 <brneg> 0f # 4 bytes
16292 nop # 2 bytes if !compact
16293 */
16294 if (!uncond)
16295 length += (compact_known && compact) ? 4 : 6;
16296 }
16297
16298 return length;
16299}
16300
16301/* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
16302 bit accordingly. */
16303
16304static int
16305relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
16306{
16307 bfd_boolean toofar;
16308
df58fc94
RS
16309 if (fragp
16310 && S_IS_DEFINED (fragp->fr_symbol)
16311 && sec == S_GET_SEGMENT (fragp->fr_symbol))
16312 {
16313 addressT addr;
16314 offsetT val;
16315 int type;
16316
16317 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
16318 /* Ignore the low bit in the target, since it will be set
16319 for a text label. */
16320 if ((val & 1) != 0)
16321 --val;
16322
16323 /* Assume this is a 2-byte branch. */
16324 addr = fragp->fr_address + fragp->fr_fix + 2;
16325
16326 /* We try to avoid the infinite loop by not adding 2 more bytes for
16327 long branches. */
16328
16329 val -= addr;
16330
16331 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
16332 if (type == 'D')
16333 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
16334 else if (type == 'E')
16335 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
16336 else
16337 abort ();
16338 }
16339 else
16340 /* If the symbol is not defined or it's in a different segment,
16341 we emit a normal 32-bit branch. */
16342 toofar = TRUE;
16343
16344 if (fragp && update
16345 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
16346 fragp->fr_subtype
16347 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
16348 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
16349
16350 if (toofar)
16351 return 4;
16352
16353 return 2;
16354}
16355
252b5132
RH
16356/* Estimate the size of a frag before relaxing. Unless this is the
16357 mips16, we are not really relaxing here, and the final size is
16358 encoded in the subtype information. For the mips16, we have to
16359 decide whether we are using an extended opcode or not. */
16360
252b5132 16361int
17a2f251 16362md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 16363{
5919d012 16364 int change;
252b5132 16365
4a6a3df4
AO
16366 if (RELAX_BRANCH_P (fragp->fr_subtype))
16367 {
16368
b34976b6
AM
16369 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
16370
4a6a3df4
AO
16371 return fragp->fr_var;
16372 }
16373
252b5132 16374 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
16375 /* We don't want to modify the EXTENDED bit here; it might get us
16376 into infinite loops. We change it only in mips_relax_frag(). */
16377 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132 16378
df58fc94
RS
16379 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
16380 {
16381 int length = 4;
16382
16383 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
16384 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
16385 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
16386 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
16387 fragp->fr_var = length;
16388
16389 return length;
16390 }
16391
252b5132 16392 if (mips_pic == NO_PIC)
5919d012 16393 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132 16394 else if (mips_pic == SVR4_PIC)
5919d012 16395 change = pic_need_relax (fragp->fr_symbol, segtype);
0a44bf69
RS
16396 else if (mips_pic == VXWORKS_PIC)
16397 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
16398 change = 0;
252b5132
RH
16399 else
16400 abort ();
16401
16402 if (change)
16403 {
4d7206a2 16404 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 16405 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 16406 }
4d7206a2
RS
16407 else
16408 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
16409}
16410
16411/* This is called to see whether a reloc against a defined symbol
de7e6852 16412 should be converted into a reloc against a section. */
252b5132
RH
16413
16414int
17a2f251 16415mips_fix_adjustable (fixS *fixp)
252b5132 16416{
252b5132
RH
16417 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
16418 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
16419 return 0;
a161fe53 16420
252b5132
RH
16421 if (fixp->fx_addsy == NULL)
16422 return 1;
a161fe53 16423
de7e6852
RS
16424 /* If symbol SYM is in a mergeable section, relocations of the form
16425 SYM + 0 can usually be made section-relative. The mergeable data
16426 is then identified by the section offset rather than by the symbol.
16427
16428 However, if we're generating REL LO16 relocations, the offset is split
16429 between the LO16 and parterning high part relocation. The linker will
16430 need to recalculate the complete offset in order to correctly identify
16431 the merge data.
16432
16433 The linker has traditionally not looked for the parterning high part
16434 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
16435 placed anywhere. Rather than break backwards compatibility by changing
16436 this, it seems better not to force the issue, and instead keep the
16437 original symbol. This will work with either linker behavior. */
738e5348 16438 if ((lo16_reloc_p (fixp->fx_r_type)
704803a9 16439 || reloc_needs_lo_p (fixp->fx_r_type))
de7e6852
RS
16440 && HAVE_IN_PLACE_ADDENDS
16441 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
16442 return 0;
16443
ce70d90a 16444 /* There is no place to store an in-place offset for JALR relocations.
2de39019
CM
16445 Likewise an in-range offset of limited PC-relative relocations may
16446 overflow the in-place relocatable field if recalculated against the
16447 start address of the symbol's containing section. */
ce70d90a 16448 if (HAVE_IN_PLACE_ADDENDS
2de39019
CM
16449 && (limited_pcrel_reloc_p (fixp->fx_r_type)
16450 || jalr_reloc_p (fixp->fx_r_type)))
1180b5a4
RS
16451 return 0;
16452
b314ec0e
RS
16453 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
16454 to a floating-point stub. The same is true for non-R_MIPS16_26
16455 relocations against MIPS16 functions; in this case, the stub becomes
16456 the function's canonical address.
16457
16458 Floating-point stubs are stored in unique .mips16.call.* or
16459 .mips16.fn.* sections. If a stub T for function F is in section S,
16460 the first relocation in section S must be against F; this is how the
16461 linker determines the target function. All relocations that might
16462 resolve to T must also be against F. We therefore have the following
16463 restrictions, which are given in an intentionally-redundant way:
16464
16465 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
16466 symbols.
16467
16468 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
16469 if that stub might be used.
16470
16471 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
16472 symbols.
16473
16474 4. We cannot reduce a stub's relocations against MIPS16 symbols if
16475 that stub might be used.
16476
16477 There is a further restriction:
16478
df58fc94
RS
16479 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
16480 R_MICROMIPS_26_S1) against MIPS16 or microMIPS symbols on
16481 targets with in-place addends; the relocation field cannot
b314ec0e
RS
16482 encode the low bit.
16483
df58fc94
RS
16484 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
16485 against a MIPS16 symbol. We deal with (5) by by not reducing any
16486 such relocations on REL targets.
b314ec0e
RS
16487
16488 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
16489 relocation against some symbol R, no relocation against R may be
16490 reduced. (Note that this deals with (2) as well as (1) because
16491 relocations against global symbols will never be reduced on ELF
16492 targets.) This approach is a little simpler than trying to detect
16493 stub sections, and gives the "all or nothing" per-symbol consistency
16494 that we have for MIPS16 symbols. */
f3ded42a 16495 if (fixp->fx_subsy == NULL
30c09090 16496 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
df58fc94
RS
16497 || *symbol_get_tc (fixp->fx_addsy)
16498 || (HAVE_IN_PLACE_ADDENDS
16499 && ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
16500 && jmp_reloc_p (fixp->fx_r_type))))
252b5132 16501 return 0;
a161fe53 16502
252b5132
RH
16503 return 1;
16504}
16505
16506/* Translate internal representation of relocation info to BFD target
16507 format. */
16508
16509arelent **
17a2f251 16510tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
16511{
16512 static arelent *retval[4];
16513 arelent *reloc;
16514 bfd_reloc_code_real_type code;
16515
4b0cff4e
TS
16516 memset (retval, 0, sizeof(retval));
16517 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
49309057
ILT
16518 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
16519 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
16520 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
16521
bad36eac
DJ
16522 if (fixp->fx_pcrel)
16523 {
df58fc94
RS
16524 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
16525 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
16526 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
b47468a6
CM
16527 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
16528 || fixp->fx_r_type == BFD_RELOC_32_PCREL);
bad36eac
DJ
16529
16530 /* At this point, fx_addnumber is "symbol offset - pcrel address".
16531 Relocations want only the symbol offset. */
16532 reloc->addend = fixp->fx_addnumber + reloc->address;
bad36eac
DJ
16533 }
16534 else
16535 reloc->addend = fixp->fx_addnumber;
252b5132 16536
438c16b8
TS
16537 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
16538 entry to be used in the relocation's section offset. */
16539 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
16540 {
16541 reloc->address = reloc->addend;
16542 reloc->addend = 0;
16543 }
16544
252b5132 16545 code = fixp->fx_r_type;
252b5132 16546
bad36eac 16547 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
252b5132
RH
16548 if (reloc->howto == NULL)
16549 {
16550 as_bad_where (fixp->fx_file, fixp->fx_line,
16551 _("Can not represent %s relocation in this object file format"),
16552 bfd_get_reloc_code_name (code));
16553 retval[0] = NULL;
16554 }
16555
16556 return retval;
16557}
16558
16559/* Relax a machine dependent frag. This returns the amount by which
16560 the current size of the frag should change. */
16561
16562int
17a2f251 16563mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 16564{
4a6a3df4
AO
16565 if (RELAX_BRANCH_P (fragp->fr_subtype))
16566 {
16567 offsetT old_var = fragp->fr_var;
b34976b6
AM
16568
16569 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
16570
16571 return fragp->fr_var - old_var;
16572 }
16573
df58fc94
RS
16574 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
16575 {
16576 offsetT old_var = fragp->fr_var;
16577 offsetT new_var = 4;
16578
16579 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
16580 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
16581 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
16582 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
16583 fragp->fr_var = new_var;
16584
16585 return new_var - old_var;
16586 }
16587
252b5132
RH
16588 if (! RELAX_MIPS16_P (fragp->fr_subtype))
16589 return 0;
16590
c4e7957c 16591 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
16592 {
16593 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
16594 return 0;
16595 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
16596 return 2;
16597 }
16598 else
16599 {
16600 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
16601 return 0;
16602 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
16603 return -2;
16604 }
16605
16606 return 0;
16607}
16608
16609/* Convert a machine dependent frag. */
16610
16611void
17a2f251 16612md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 16613{
4a6a3df4
AO
16614 if (RELAX_BRANCH_P (fragp->fr_subtype))
16615 {
4d68580a 16616 char *buf;
4a6a3df4
AO
16617 unsigned long insn;
16618 expressionS exp;
16619 fixS *fixp;
b34976b6 16620
4d68580a
RS
16621 buf = fragp->fr_literal + fragp->fr_fix;
16622 insn = read_insn (buf);
b34976b6 16623
4a6a3df4
AO
16624 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
16625 {
16626 /* We generate a fixup instead of applying it right now
16627 because, if there are linker relaxations, we're going to
16628 need the relocations. */
16629 exp.X_op = O_symbol;
16630 exp.X_add_symbol = fragp->fr_symbol;
16631 exp.X_add_number = fragp->fr_offset;
16632
4d68580a
RS
16633 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
16634 BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
16635 fixp->fx_file = fragp->fr_file;
16636 fixp->fx_line = fragp->fr_line;
b34976b6 16637
4d68580a 16638 buf = write_insn (buf, insn);
4a6a3df4
AO
16639 }
16640 else
16641 {
16642 int i;
16643
16644 as_warn_where (fragp->fr_file, fragp->fr_line,
5c4f07ba 16645 _("Relaxed out-of-range branch into a jump"));
4a6a3df4
AO
16646
16647 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
16648 goto uncond;
16649
16650 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
16651 {
16652 /* Reverse the branch. */
16653 switch ((insn >> 28) & 0xf)
16654 {
16655 case 4:
3bf0dbfb
MR
16656 /* bc[0-3][tf]l? instructions can have the condition
16657 reversed by tweaking a single TF bit, and their
16658 opcodes all have 0x4???????. */
16659 gas_assert ((insn & 0xf3e00000) == 0x41000000);
4a6a3df4
AO
16660 insn ^= 0x00010000;
16661 break;
16662
16663 case 0:
16664 /* bltz 0x04000000 bgez 0x04010000
54f4ddb3 16665 bltzal 0x04100000 bgezal 0x04110000 */
9c2799c2 16666 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
4a6a3df4
AO
16667 insn ^= 0x00010000;
16668 break;
b34976b6 16669
4a6a3df4
AO
16670 case 1:
16671 /* beq 0x10000000 bne 0x14000000
54f4ddb3 16672 blez 0x18000000 bgtz 0x1c000000 */
4a6a3df4
AO
16673 insn ^= 0x04000000;
16674 break;
16675
16676 default:
16677 abort ();
16678 }
16679 }
16680
16681 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
16682 {
16683 /* Clear the and-link bit. */
9c2799c2 16684 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
4a6a3df4 16685
54f4ddb3
TS
16686 /* bltzal 0x04100000 bgezal 0x04110000
16687 bltzall 0x04120000 bgezall 0x04130000 */
4a6a3df4
AO
16688 insn &= ~0x00100000;
16689 }
16690
16691 /* Branch over the branch (if the branch was likely) or the
16692 full jump (not likely case). Compute the offset from the
16693 current instruction to branch to. */
16694 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
16695 i = 16;
16696 else
16697 {
16698 /* How many bytes in instructions we've already emitted? */
4d68580a 16699 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
16700 /* How many bytes in instructions from here to the end? */
16701 i = fragp->fr_var - i;
16702 }
16703 /* Convert to instruction count. */
16704 i >>= 2;
16705 /* Branch counts from the next instruction. */
b34976b6 16706 i--;
4a6a3df4
AO
16707 insn |= i;
16708 /* Branch over the jump. */
4d68580a 16709 buf = write_insn (buf, insn);
4a6a3df4 16710
54f4ddb3 16711 /* nop */
4d68580a 16712 buf = write_insn (buf, 0);
4a6a3df4
AO
16713
16714 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
16715 {
16716 /* beql $0, $0, 2f */
16717 insn = 0x50000000;
16718 /* Compute the PC offset from the current instruction to
16719 the end of the variable frag. */
16720 /* How many bytes in instructions we've already emitted? */
4d68580a 16721 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
16722 /* How many bytes in instructions from here to the end? */
16723 i = fragp->fr_var - i;
16724 /* Convert to instruction count. */
16725 i >>= 2;
16726 /* Don't decrement i, because we want to branch over the
16727 delay slot. */
4a6a3df4 16728 insn |= i;
4a6a3df4 16729
4d68580a
RS
16730 buf = write_insn (buf, insn);
16731 buf = write_insn (buf, 0);
4a6a3df4
AO
16732 }
16733
16734 uncond:
16735 if (mips_pic == NO_PIC)
16736 {
16737 /* j or jal. */
16738 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
16739 ? 0x0c000000 : 0x08000000);
16740 exp.X_op = O_symbol;
16741 exp.X_add_symbol = fragp->fr_symbol;
16742 exp.X_add_number = fragp->fr_offset;
16743
4d68580a
RS
16744 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
16745 FALSE, BFD_RELOC_MIPS_JMP);
4a6a3df4
AO
16746 fixp->fx_file = fragp->fr_file;
16747 fixp->fx_line = fragp->fr_line;
16748
4d68580a 16749 buf = write_insn (buf, insn);
4a6a3df4
AO
16750 }
16751 else
16752 {
66b3e8da
MR
16753 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
16754
4a6a3df4 16755 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
66b3e8da
MR
16756 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
16757 insn |= at << OP_SH_RT;
4a6a3df4
AO
16758 exp.X_op = O_symbol;
16759 exp.X_add_symbol = fragp->fr_symbol;
16760 exp.X_add_number = fragp->fr_offset;
16761
16762 if (fragp->fr_offset)
16763 {
16764 exp.X_add_symbol = make_expr_symbol (&exp);
16765 exp.X_add_number = 0;
16766 }
16767
4d68580a
RS
16768 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
16769 FALSE, BFD_RELOC_MIPS_GOT16);
4a6a3df4
AO
16770 fixp->fx_file = fragp->fr_file;
16771 fixp->fx_line = fragp->fr_line;
16772
4d68580a 16773 buf = write_insn (buf, insn);
b34976b6 16774
4a6a3df4 16775 if (mips_opts.isa == ISA_MIPS1)
4d68580a
RS
16776 /* nop */
16777 buf = write_insn (buf, 0);
4a6a3df4
AO
16778
16779 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
66b3e8da
MR
16780 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
16781 insn |= at << OP_SH_RS | at << OP_SH_RT;
4a6a3df4 16782
4d68580a
RS
16783 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
16784 FALSE, BFD_RELOC_LO16);
4a6a3df4
AO
16785 fixp->fx_file = fragp->fr_file;
16786 fixp->fx_line = fragp->fr_line;
b34976b6 16787
4d68580a 16788 buf = write_insn (buf, insn);
4a6a3df4
AO
16789
16790 /* j(al)r $at. */
16791 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
66b3e8da 16792 insn = 0x0000f809;
4a6a3df4 16793 else
66b3e8da
MR
16794 insn = 0x00000008;
16795 insn |= at << OP_SH_RS;
4a6a3df4 16796
4d68580a 16797 buf = write_insn (buf, insn);
4a6a3df4
AO
16798 }
16799 }
16800
4a6a3df4 16801 fragp->fr_fix += fragp->fr_var;
4d68580a 16802 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
4a6a3df4
AO
16803 return;
16804 }
16805
df58fc94
RS
16806 /* Relax microMIPS branches. */
16807 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
16808 {
4d68580a 16809 char *buf = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
16810 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
16811 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
16812 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
2309ddf2 16813 bfd_boolean short_ds;
df58fc94
RS
16814 unsigned long insn;
16815 expressionS exp;
16816 fixS *fixp;
16817
16818 exp.X_op = O_symbol;
16819 exp.X_add_symbol = fragp->fr_symbol;
16820 exp.X_add_number = fragp->fr_offset;
16821
16822 fragp->fr_fix += fragp->fr_var;
16823
16824 /* Handle 16-bit branches that fit or are forced to fit. */
16825 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
16826 {
16827 /* We generate a fixup instead of applying it right now,
16828 because if there is linker relaxation, we're going to
16829 need the relocations. */
16830 if (type == 'D')
4d68580a 16831 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
df58fc94
RS
16832 BFD_RELOC_MICROMIPS_10_PCREL_S1);
16833 else if (type == 'E')
4d68580a 16834 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
df58fc94
RS
16835 BFD_RELOC_MICROMIPS_7_PCREL_S1);
16836 else
16837 abort ();
16838
16839 fixp->fx_file = fragp->fr_file;
16840 fixp->fx_line = fragp->fr_line;
16841
16842 /* These relocations can have an addend that won't fit in
16843 2 octets. */
16844 fixp->fx_no_overflow = 1;
16845
16846 return;
16847 }
16848
2309ddf2 16849 /* Handle 32-bit branches that fit or are forced to fit. */
df58fc94
RS
16850 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
16851 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
16852 {
16853 /* We generate a fixup instead of applying it right now,
16854 because if there is linker relaxation, we're going to
16855 need the relocations. */
4d68580a
RS
16856 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
16857 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
16858 fixp->fx_file = fragp->fr_file;
16859 fixp->fx_line = fragp->fr_line;
16860
16861 if (type == 0)
16862 return;
16863 }
16864
16865 /* Relax 16-bit branches to 32-bit branches. */
16866 if (type != 0)
16867 {
4d68580a 16868 insn = read_compressed_insn (buf, 2);
df58fc94
RS
16869
16870 if ((insn & 0xfc00) == 0xcc00) /* b16 */
16871 insn = 0x94000000; /* beq */
16872 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
16873 {
16874 unsigned long regno;
16875
16876 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
16877 regno = micromips_to_32_reg_d_map [regno];
16878 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
16879 insn |= regno << MICROMIPSOP_SH_RS;
16880 }
16881 else
16882 abort ();
16883
16884 /* Nothing else to do, just write it out. */
16885 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
16886 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
16887 {
4d68580a
RS
16888 buf = write_compressed_insn (buf, insn, 4);
16889 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
16890 return;
16891 }
16892 }
16893 else
4d68580a 16894 insn = read_compressed_insn (buf, 4);
df58fc94
RS
16895
16896 /* Relax 32-bit branches to a sequence of instructions. */
16897 as_warn_where (fragp->fr_file, fragp->fr_line,
16898 _("Relaxed out-of-range branch into a jump"));
16899
2309ddf2
MR
16900 /* Set the short-delay-slot bit. */
16901 short_ds = al && (insn & 0x02000000) != 0;
df58fc94
RS
16902
16903 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
16904 {
16905 symbolS *l;
16906
16907 /* Reverse the branch. */
16908 if ((insn & 0xfc000000) == 0x94000000 /* beq */
16909 || (insn & 0xfc000000) == 0xb4000000) /* bne */
16910 insn ^= 0x20000000;
16911 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
16912 || (insn & 0xffe00000) == 0x40400000 /* bgez */
16913 || (insn & 0xffe00000) == 0x40800000 /* blez */
16914 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
16915 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
16916 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
16917 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
16918 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
16919 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
16920 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
16921 insn ^= 0x00400000;
16922 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
16923 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
16924 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
16925 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
16926 insn ^= 0x00200000;
16927 else
16928 abort ();
16929
16930 if (al)
16931 {
16932 /* Clear the and-link and short-delay-slot bits. */
16933 gas_assert ((insn & 0xfda00000) == 0x40200000);
16934
16935 /* bltzal 0x40200000 bgezal 0x40600000 */
16936 /* bltzals 0x42200000 bgezals 0x42600000 */
16937 insn &= ~0x02200000;
16938 }
16939
16940 /* Make a label at the end for use with the branch. */
16941 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
16942 micromips_label_inc ();
f3ded42a 16943 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
df58fc94
RS
16944
16945 /* Refer to it. */
4d68580a
RS
16946 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
16947 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
16948 fixp->fx_file = fragp->fr_file;
16949 fixp->fx_line = fragp->fr_line;
16950
16951 /* Branch over the jump. */
4d68580a 16952 buf = write_compressed_insn (buf, insn, 4);
df58fc94 16953 if (!compact)
4d68580a
RS
16954 /* nop */
16955 buf = write_compressed_insn (buf, 0x0c00, 2);
df58fc94
RS
16956 }
16957
16958 if (mips_pic == NO_PIC)
16959 {
2309ddf2
MR
16960 unsigned long jal = short_ds ? 0x74000000 : 0xf4000000; /* jal/s */
16961
df58fc94
RS
16962 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
16963 insn = al ? jal : 0xd4000000;
16964
4d68580a
RS
16965 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
16966 BFD_RELOC_MICROMIPS_JMP);
df58fc94
RS
16967 fixp->fx_file = fragp->fr_file;
16968 fixp->fx_line = fragp->fr_line;
16969
4d68580a 16970 buf = write_compressed_insn (buf, insn, 4);
df58fc94 16971 if (compact)
4d68580a
RS
16972 /* nop */
16973 buf = write_compressed_insn (buf, 0x0c00, 2);
df58fc94
RS
16974 }
16975 else
16976 {
16977 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
2309ddf2
MR
16978 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
16979 unsigned long jr = compact ? 0x45a0 : 0x4580; /* jr/c */
df58fc94
RS
16980
16981 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
16982 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
16983 insn |= at << MICROMIPSOP_SH_RT;
16984
16985 if (exp.X_add_number)
16986 {
16987 exp.X_add_symbol = make_expr_symbol (&exp);
16988 exp.X_add_number = 0;
16989 }
16990
4d68580a
RS
16991 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
16992 BFD_RELOC_MICROMIPS_GOT16);
df58fc94
RS
16993 fixp->fx_file = fragp->fr_file;
16994 fixp->fx_line = fragp->fr_line;
16995
4d68580a 16996 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
16997
16998 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
16999 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
17000 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
17001
4d68580a
RS
17002 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
17003 BFD_RELOC_MICROMIPS_LO16);
df58fc94
RS
17004 fixp->fx_file = fragp->fr_file;
17005 fixp->fx_line = fragp->fr_line;
17006
4d68580a 17007 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
17008
17009 /* jr/jrc/jalr/jalrs $at */
17010 insn = al ? jalr : jr;
17011 insn |= at << MICROMIPSOP_SH_MJ;
17012
4d68580a 17013 buf = write_compressed_insn (buf, insn, 2);
df58fc94
RS
17014 }
17015
4d68580a 17016 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
17017 return;
17018 }
17019
252b5132
RH
17020 if (RELAX_MIPS16_P (fragp->fr_subtype))
17021 {
17022 int type;
3ccad066 17023 const struct mips_int_operand *operand;
252b5132 17024 offsetT val;
5c04167a
RS
17025 char *buf;
17026 unsigned int user_length, length;
252b5132 17027 unsigned long insn;
5c04167a 17028 bfd_boolean ext;
252b5132
RH
17029
17030 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
3ccad066 17031 operand = mips16_immed_operand (type, FALSE);
252b5132 17032
5c04167a 17033 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
5f5f22c0 17034 val = resolve_symbol_value (fragp->fr_symbol);
3ccad066 17035 if (operand->root.type == OP_PCREL)
252b5132 17036 {
3ccad066 17037 const struct mips_pcrel_operand *pcrel_op;
252b5132
RH
17038 addressT addr;
17039
3ccad066 17040 pcrel_op = (const struct mips_pcrel_operand *) operand;
252b5132
RH
17041 addr = fragp->fr_address + fragp->fr_fix;
17042
17043 /* The rules for the base address of a PC relative reloc are
17044 complicated; see mips16_extended_frag. */
3ccad066 17045 if (pcrel_op->include_isa_bit)
252b5132
RH
17046 {
17047 addr += 2;
17048 if (ext)
17049 addr += 2;
17050 /* Ignore the low bit in the target, since it will be
17051 set for a text label. */
3ccad066 17052 val &= -2;
252b5132
RH
17053 }
17054 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17055 addr -= 4;
17056 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17057 addr -= 2;
17058
3ccad066 17059 addr &= -(1 << pcrel_op->align_log2);
252b5132
RH
17060 val -= addr;
17061
17062 /* Make sure the section winds up with the alignment we have
17063 assumed. */
3ccad066
RS
17064 if (operand->shift > 0)
17065 record_alignment (asec, operand->shift);
252b5132
RH
17066 }
17067
17068 if (ext
17069 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
17070 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
17071 as_warn_where (fragp->fr_file, fragp->fr_line,
17072 _("extended instruction in delay slot"));
17073
5c04167a 17074 buf = fragp->fr_literal + fragp->fr_fix;
252b5132 17075
4d68580a 17076 insn = read_compressed_insn (buf, 2);
5c04167a
RS
17077 if (ext)
17078 insn |= MIPS16_EXTEND;
252b5132 17079
5c04167a
RS
17080 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17081 user_length = 4;
17082 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17083 user_length = 2;
17084 else
17085 user_length = 0;
17086
43c0598f 17087 mips16_immed (fragp->fr_file, fragp->fr_line, type,
c150d1d2 17088 BFD_RELOC_UNUSED, val, user_length, &insn);
252b5132 17089
5c04167a
RS
17090 length = (ext ? 4 : 2);
17091 gas_assert (mips16_opcode_length (insn) == length);
17092 write_compressed_insn (buf, insn, length);
17093 fragp->fr_fix += length;
252b5132
RH
17094 }
17095 else
17096 {
df58fc94
RS
17097 relax_substateT subtype = fragp->fr_subtype;
17098 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
17099 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
4d7206a2
RS
17100 int first, second;
17101 fixS *fixp;
252b5132 17102
df58fc94
RS
17103 first = RELAX_FIRST (subtype);
17104 second = RELAX_SECOND (subtype);
4d7206a2 17105 fixp = (fixS *) fragp->fr_opcode;
252b5132 17106
df58fc94
RS
17107 /* If the delay slot chosen does not match the size of the instruction,
17108 then emit a warning. */
17109 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
17110 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
17111 {
17112 relax_substateT s;
17113 const char *msg;
17114
17115 s = subtype & (RELAX_DELAY_SLOT_16BIT
17116 | RELAX_DELAY_SLOT_SIZE_FIRST
17117 | RELAX_DELAY_SLOT_SIZE_SECOND);
17118 msg = macro_warning (s);
17119 if (msg != NULL)
db9b2be4 17120 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94
RS
17121 subtype &= ~s;
17122 }
17123
584892a6 17124 /* Possibly emit a warning if we've chosen the longer option. */
df58fc94 17125 if (use_second == second_longer)
584892a6 17126 {
df58fc94
RS
17127 relax_substateT s;
17128 const char *msg;
17129
17130 s = (subtype
17131 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
17132 msg = macro_warning (s);
17133 if (msg != NULL)
db9b2be4 17134 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94 17135 subtype &= ~s;
584892a6
RS
17136 }
17137
4d7206a2
RS
17138 /* Go through all the fixups for the first sequence. Disable them
17139 (by marking them as done) if we're going to use the second
17140 sequence instead. */
17141 while (fixp
17142 && fixp->fx_frag == fragp
17143 && fixp->fx_where < fragp->fr_fix - second)
17144 {
df58fc94 17145 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
17146 fixp->fx_done = 1;
17147 fixp = fixp->fx_next;
17148 }
252b5132 17149
4d7206a2
RS
17150 /* Go through the fixups for the second sequence. Disable them if
17151 we're going to use the first sequence, otherwise adjust their
17152 addresses to account for the relaxation. */
17153 while (fixp && fixp->fx_frag == fragp)
17154 {
df58fc94 17155 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
17156 fixp->fx_where -= first;
17157 else
17158 fixp->fx_done = 1;
17159 fixp = fixp->fx_next;
17160 }
17161
17162 /* Now modify the frag contents. */
df58fc94 17163 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
17164 {
17165 char *start;
17166
17167 start = fragp->fr_literal + fragp->fr_fix - first - second;
17168 memmove (start, start + first, second);
17169 fragp->fr_fix -= first;
17170 }
17171 else
17172 fragp->fr_fix -= second;
252b5132
RH
17173 }
17174}
17175
252b5132
RH
17176/* This function is called after the relocs have been generated.
17177 We've been storing mips16 text labels as odd. Here we convert them
17178 back to even for the convenience of the debugger. */
17179
17180void
17a2f251 17181mips_frob_file_after_relocs (void)
252b5132
RH
17182{
17183 asymbol **syms;
17184 unsigned int count, i;
17185
252b5132
RH
17186 syms = bfd_get_outsymbols (stdoutput);
17187 count = bfd_get_symcount (stdoutput);
17188 for (i = 0; i < count; i++, syms++)
df58fc94
RS
17189 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
17190 && ((*syms)->value & 1) != 0)
17191 {
17192 (*syms)->value &= ~1;
17193 /* If the symbol has an odd size, it was probably computed
17194 incorrectly, so adjust that as well. */
17195 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
17196 ++elf_symbol (*syms)->internal_elf_sym.st_size;
17197 }
252b5132
RH
17198}
17199
a1facbec
MR
17200/* This function is called whenever a label is defined, including fake
17201 labels instantiated off the dot special symbol. It is used when
17202 handling branch delays; if a branch has a label, we assume we cannot
17203 move it. This also bumps the value of the symbol by 1 in compressed
17204 code. */
252b5132 17205
e1b47bd5 17206static void
a1facbec 17207mips_record_label (symbolS *sym)
252b5132 17208{
a8dbcb85 17209 segment_info_type *si = seg_info (now_seg);
252b5132
RH
17210 struct insn_label_list *l;
17211
17212 if (free_insn_labels == NULL)
17213 l = (struct insn_label_list *) xmalloc (sizeof *l);
17214 else
17215 {
17216 l = free_insn_labels;
17217 free_insn_labels = l->next;
17218 }
17219
17220 l->label = sym;
a8dbcb85
TS
17221 l->next = si->label_list;
17222 si->label_list = l;
a1facbec 17223}
07a53e5c 17224
a1facbec
MR
17225/* This function is called as tc_frob_label() whenever a label is defined
17226 and adds a DWARF-2 record we only want for true labels. */
17227
17228void
17229mips_define_label (symbolS *sym)
17230{
17231 mips_record_label (sym);
07a53e5c 17232 dwarf2_emit_label (sym);
252b5132 17233}
e1b47bd5
RS
17234
17235/* This function is called by tc_new_dot_label whenever a new dot symbol
17236 is defined. */
17237
17238void
17239mips_add_dot_label (symbolS *sym)
17240{
17241 mips_record_label (sym);
17242 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
17243 mips_compressed_mark_label (sym);
17244}
252b5132 17245\f
252b5132
RH
17246/* Some special processing for a MIPS ELF file. */
17247
17248void
17a2f251 17249mips_elf_final_processing (void)
252b5132
RH
17250{
17251 /* Write out the register information. */
316f5878 17252 if (mips_abi != N64_ABI)
252b5132
RH
17253 {
17254 Elf32_RegInfo s;
17255
17256 s.ri_gprmask = mips_gprmask;
17257 s.ri_cprmask[0] = mips_cprmask[0];
17258 s.ri_cprmask[1] = mips_cprmask[1];
17259 s.ri_cprmask[2] = mips_cprmask[2];
17260 s.ri_cprmask[3] = mips_cprmask[3];
17261 /* The gp_value field is set by the MIPS ELF backend. */
17262
17263 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
17264 ((Elf32_External_RegInfo *)
17265 mips_regmask_frag));
17266 }
17267 else
17268 {
17269 Elf64_Internal_RegInfo s;
17270
17271 s.ri_gprmask = mips_gprmask;
17272 s.ri_pad = 0;
17273 s.ri_cprmask[0] = mips_cprmask[0];
17274 s.ri_cprmask[1] = mips_cprmask[1];
17275 s.ri_cprmask[2] = mips_cprmask[2];
17276 s.ri_cprmask[3] = mips_cprmask[3];
17277 /* The gp_value field is set by the MIPS ELF backend. */
17278
17279 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
17280 ((Elf64_External_RegInfo *)
17281 mips_regmask_frag));
17282 }
17283
17284 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
17285 sort of BFD interface for this. */
17286 if (mips_any_noreorder)
17287 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
17288 if (mips_pic != NO_PIC)
143d77c5 17289 {
8b828383 17290 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
17291 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
17292 }
17293 if (mips_abicalls)
17294 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 17295
b015e599
AP
17296 /* Set MIPS ELF flags for ASEs. Note that not all ASEs have flags
17297 defined at present; this might need to change in future. */
a4672219
TS
17298 if (file_ase_mips16)
17299 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
df58fc94
RS
17300 if (file_ase_micromips)
17301 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
846ef2d0 17302 if (file_ase & ASE_MDMX)
deec1734 17303 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 17304
bdaaa2e1 17305 /* Set the MIPS ELF ABI flags. */
316f5878 17306 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 17307 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 17308 else if (mips_abi == O64_ABI)
252b5132 17309 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 17310 else if (mips_abi == EABI_ABI)
252b5132 17311 {
316f5878 17312 if (!file_mips_gp32)
252b5132
RH
17313 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
17314 else
17315 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
17316 }
316f5878 17317 else if (mips_abi == N32_ABI)
be00bddd
TS
17318 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
17319
c9914766 17320 /* Nothing to do for N64_ABI. */
252b5132
RH
17321
17322 if (mips_32bitmode)
17323 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
ad3fea08 17324
ba92f887
MR
17325 if (mips_flag_nan2008)
17326 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NAN2008;
17327
ad3fea08
TS
17328#if 0 /* XXX FIXME */
17329 /* 32 bit code with 64 bit FP registers. */
17330 if (!file_mips_fp32 && ABI_NEEDS_32BIT_REGS (mips_abi))
17331 elf_elfheader (stdoutput)->e_flags |= ???;
17332#endif
252b5132 17333}
252b5132 17334\f
beae10d5 17335typedef struct proc {
9b2f1d35
EC
17336 symbolS *func_sym;
17337 symbolS *func_end_sym;
beae10d5
KH
17338 unsigned long reg_mask;
17339 unsigned long reg_offset;
17340 unsigned long fpreg_mask;
17341 unsigned long fpreg_offset;
17342 unsigned long frame_offset;
17343 unsigned long frame_reg;
17344 unsigned long pc_reg;
17345} procS;
252b5132
RH
17346
17347static procS cur_proc;
17348static procS *cur_proc_ptr;
17349static int numprocs;
17350
df58fc94
RS
17351/* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
17352 as "2", and a normal nop as "0". */
17353
17354#define NOP_OPCODE_MIPS 0
17355#define NOP_OPCODE_MIPS16 1
17356#define NOP_OPCODE_MICROMIPS 2
742a56fe
RS
17357
17358char
17359mips_nop_opcode (void)
17360{
df58fc94
RS
17361 if (seg_info (now_seg)->tc_segment_info_data.micromips)
17362 return NOP_OPCODE_MICROMIPS;
17363 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
17364 return NOP_OPCODE_MIPS16;
17365 else
17366 return NOP_OPCODE_MIPS;
742a56fe
RS
17367}
17368
df58fc94
RS
17369/* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
17370 32-bit microMIPS NOPs here (if applicable). */
a19d8eb0 17371
0a9ef439 17372void
17a2f251 17373mips_handle_align (fragS *fragp)
a19d8eb0 17374{
df58fc94 17375 char nop_opcode;
742a56fe 17376 char *p;
c67a084a
NC
17377 int bytes, size, excess;
17378 valueT opcode;
742a56fe 17379
0a9ef439
RH
17380 if (fragp->fr_type != rs_align_code)
17381 return;
17382
742a56fe 17383 p = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
17384 nop_opcode = *p;
17385 switch (nop_opcode)
a19d8eb0 17386 {
df58fc94
RS
17387 case NOP_OPCODE_MICROMIPS:
17388 opcode = micromips_nop32_insn.insn_opcode;
17389 size = 4;
17390 break;
17391 case NOP_OPCODE_MIPS16:
c67a084a
NC
17392 opcode = mips16_nop_insn.insn_opcode;
17393 size = 2;
df58fc94
RS
17394 break;
17395 case NOP_OPCODE_MIPS:
17396 default:
c67a084a
NC
17397 opcode = nop_insn.insn_opcode;
17398 size = 4;
df58fc94 17399 break;
c67a084a 17400 }
a19d8eb0 17401
c67a084a
NC
17402 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
17403 excess = bytes % size;
df58fc94
RS
17404
17405 /* Handle the leading part if we're not inserting a whole number of
17406 instructions, and make it the end of the fixed part of the frag.
17407 Try to fit in a short microMIPS NOP if applicable and possible,
17408 and use zeroes otherwise. */
17409 gas_assert (excess < 4);
17410 fragp->fr_fix += excess;
17411 switch (excess)
c67a084a 17412 {
df58fc94
RS
17413 case 3:
17414 *p++ = '\0';
17415 /* Fall through. */
17416 case 2:
833794fc 17417 if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
df58fc94 17418 {
4d68580a 17419 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
df58fc94
RS
17420 break;
17421 }
17422 *p++ = '\0';
17423 /* Fall through. */
17424 case 1:
17425 *p++ = '\0';
17426 /* Fall through. */
17427 case 0:
17428 break;
a19d8eb0 17429 }
c67a084a
NC
17430
17431 md_number_to_chars (p, opcode, size);
17432 fragp->fr_var = size;
a19d8eb0
CP
17433}
17434
252b5132 17435static void
17a2f251 17436md_obj_begin (void)
252b5132
RH
17437{
17438}
17439
17440static void
17a2f251 17441md_obj_end (void)
252b5132 17442{
54f4ddb3 17443 /* Check for premature end, nesting errors, etc. */
252b5132 17444 if (cur_proc_ptr)
9a41af64 17445 as_warn (_("missing .end at end of assembly"));
252b5132
RH
17446}
17447
17448static long
17a2f251 17449get_number (void)
252b5132
RH
17450{
17451 int negative = 0;
17452 long val = 0;
17453
17454 if (*input_line_pointer == '-')
17455 {
17456 ++input_line_pointer;
17457 negative = 1;
17458 }
3882b010 17459 if (!ISDIGIT (*input_line_pointer))
956cd1d6 17460 as_bad (_("expected simple number"));
252b5132
RH
17461 if (input_line_pointer[0] == '0')
17462 {
17463 if (input_line_pointer[1] == 'x')
17464 {
17465 input_line_pointer += 2;
3882b010 17466 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
17467 {
17468 val <<= 4;
17469 val |= hex_value (*input_line_pointer++);
17470 }
17471 return negative ? -val : val;
17472 }
17473 else
17474 {
17475 ++input_line_pointer;
3882b010 17476 while (ISDIGIT (*input_line_pointer))
252b5132
RH
17477 {
17478 val <<= 3;
17479 val |= *input_line_pointer++ - '0';
17480 }
17481 return negative ? -val : val;
17482 }
17483 }
3882b010 17484 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
17485 {
17486 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
17487 *input_line_pointer, *input_line_pointer);
956cd1d6 17488 as_warn (_("invalid number"));
252b5132
RH
17489 return -1;
17490 }
3882b010 17491 while (ISDIGIT (*input_line_pointer))
252b5132
RH
17492 {
17493 val *= 10;
17494 val += *input_line_pointer++ - '0';
17495 }
17496 return negative ? -val : val;
17497}
17498
17499/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
17500 is an initial number which is the ECOFF file index. In the non-ECOFF
17501 case .file implies DWARF-2. */
17502
17503static void
17a2f251 17504s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 17505{
ecb4347a
DJ
17506 static int first_file_directive = 0;
17507
c5dd6aab
DJ
17508 if (ECOFF_DEBUGGING)
17509 {
17510 get_number ();
17511 s_app_file (0);
17512 }
17513 else
ecb4347a
DJ
17514 {
17515 char *filename;
17516
17517 filename = dwarf2_directive_file (0);
17518
17519 /* Versions of GCC up to 3.1 start files with a ".file"
17520 directive even for stabs output. Make sure that this
17521 ".file" is handled. Note that you need a version of GCC
17522 after 3.1 in order to support DWARF-2 on MIPS. */
17523 if (filename != NULL && ! first_file_directive)
17524 {
17525 (void) new_logical_line (filename, -1);
c04f5787 17526 s_app_file_string (filename, 0);
ecb4347a
DJ
17527 }
17528 first_file_directive = 1;
17529 }
c5dd6aab
DJ
17530}
17531
17532/* The .loc directive, implying DWARF-2. */
252b5132
RH
17533
17534static void
17a2f251 17535s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 17536{
c5dd6aab
DJ
17537 if (!ECOFF_DEBUGGING)
17538 dwarf2_directive_loc (0);
252b5132
RH
17539}
17540
252b5132
RH
17541/* The .end directive. */
17542
17543static void
17a2f251 17544s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
17545{
17546 symbolS *p;
252b5132 17547
7a621144
DJ
17548 /* Following functions need their own .frame and .cprestore directives. */
17549 mips_frame_reg_valid = 0;
17550 mips_cprestore_valid = 0;
17551
252b5132
RH
17552 if (!is_end_of_line[(unsigned char) *input_line_pointer])
17553 {
17554 p = get_symbol ();
17555 demand_empty_rest_of_line ();
17556 }
17557 else
17558 p = NULL;
17559
14949570 17560 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
17561 as_warn (_(".end not in text section"));
17562
17563 if (!cur_proc_ptr)
17564 {
17565 as_warn (_(".end directive without a preceding .ent directive."));
17566 demand_empty_rest_of_line ();
17567 return;
17568 }
17569
17570 if (p != NULL)
17571 {
9c2799c2 17572 gas_assert (S_GET_NAME (p));
9b2f1d35 17573 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
252b5132 17574 as_warn (_(".end symbol does not match .ent symbol."));
ecb4347a
DJ
17575
17576 if (debug_type == DEBUG_STABS)
17577 stabs_generate_asm_endfunc (S_GET_NAME (p),
17578 S_GET_NAME (p));
252b5132
RH
17579 }
17580 else
17581 as_warn (_(".end directive missing or unknown symbol"));
17582
9b2f1d35
EC
17583 /* Create an expression to calculate the size of the function. */
17584 if (p && cur_proc_ptr)
17585 {
17586 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
17587 expressionS *exp = xmalloc (sizeof (expressionS));
17588
17589 obj->size = exp;
17590 exp->X_op = O_subtract;
17591 exp->X_add_symbol = symbol_temp_new_now ();
17592 exp->X_op_symbol = p;
17593 exp->X_add_number = 0;
17594
17595 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
17596 }
17597
ecb4347a 17598 /* Generate a .pdr section. */
f3ded42a 17599 if (!ECOFF_DEBUGGING && mips_flag_pdr)
ecb4347a
DJ
17600 {
17601 segT saved_seg = now_seg;
17602 subsegT saved_subseg = now_subseg;
ecb4347a
DJ
17603 expressionS exp;
17604 char *fragp;
252b5132 17605
252b5132 17606#ifdef md_flush_pending_output
ecb4347a 17607 md_flush_pending_output ();
252b5132
RH
17608#endif
17609
9c2799c2 17610 gas_assert (pdr_seg);
ecb4347a 17611 subseg_set (pdr_seg, 0);
252b5132 17612
ecb4347a
DJ
17613 /* Write the symbol. */
17614 exp.X_op = O_symbol;
17615 exp.X_add_symbol = p;
17616 exp.X_add_number = 0;
17617 emit_expr (&exp, 4);
252b5132 17618
ecb4347a 17619 fragp = frag_more (7 * 4);
252b5132 17620
17a2f251
TS
17621 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
17622 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
17623 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
17624 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
17625 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
17626 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
17627 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 17628
ecb4347a
DJ
17629 subseg_set (saved_seg, saved_subseg);
17630 }
252b5132
RH
17631
17632 cur_proc_ptr = NULL;
17633}
17634
17635/* The .aent and .ent directives. */
17636
17637static void
17a2f251 17638s_mips_ent (int aent)
252b5132 17639{
252b5132 17640 symbolS *symbolP;
252b5132
RH
17641
17642 symbolP = get_symbol ();
17643 if (*input_line_pointer == ',')
f9419b05 17644 ++input_line_pointer;
252b5132 17645 SKIP_WHITESPACE ();
3882b010 17646 if (ISDIGIT (*input_line_pointer)
d9a62219 17647 || *input_line_pointer == '-')
874e8986 17648 get_number ();
252b5132 17649
14949570 17650 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
17651 as_warn (_(".ent or .aent not in text section."));
17652
17653 if (!aent && cur_proc_ptr)
9a41af64 17654 as_warn (_("missing .end"));
252b5132
RH
17655
17656 if (!aent)
17657 {
7a621144
DJ
17658 /* This function needs its own .frame and .cprestore directives. */
17659 mips_frame_reg_valid = 0;
17660 mips_cprestore_valid = 0;
17661
252b5132
RH
17662 cur_proc_ptr = &cur_proc;
17663 memset (cur_proc_ptr, '\0', sizeof (procS));
17664
9b2f1d35 17665 cur_proc_ptr->func_sym = symbolP;
252b5132 17666
f9419b05 17667 ++numprocs;
ecb4347a
DJ
17668
17669 if (debug_type == DEBUG_STABS)
17670 stabs_generate_asm_func (S_GET_NAME (symbolP),
17671 S_GET_NAME (symbolP));
252b5132
RH
17672 }
17673
7c0fc524
MR
17674 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
17675
252b5132
RH
17676 demand_empty_rest_of_line ();
17677}
17678
17679/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 17680 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 17681 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 17682 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
17683 symbol table (in the mdebug section). */
17684
17685static void
17a2f251 17686s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 17687{
f3ded42a
RS
17688 if (ECOFF_DEBUGGING)
17689 s_ignore (ignore);
17690 else
ecb4347a
DJ
17691 {
17692 long val;
252b5132 17693
ecb4347a
DJ
17694 if (cur_proc_ptr == (procS *) NULL)
17695 {
17696 as_warn (_(".frame outside of .ent"));
17697 demand_empty_rest_of_line ();
17698 return;
17699 }
252b5132 17700
ecb4347a
DJ
17701 cur_proc_ptr->frame_reg = tc_get_register (1);
17702
17703 SKIP_WHITESPACE ();
17704 if (*input_line_pointer++ != ','
17705 || get_absolute_expression_and_terminator (&val) != ',')
17706 {
17707 as_warn (_("Bad .frame directive"));
17708 --input_line_pointer;
17709 demand_empty_rest_of_line ();
17710 return;
17711 }
252b5132 17712
ecb4347a
DJ
17713 cur_proc_ptr->frame_offset = val;
17714 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 17715
252b5132 17716 demand_empty_rest_of_line ();
252b5132 17717 }
252b5132
RH
17718}
17719
bdaaa2e1
KH
17720/* The .fmask and .mask directives. If the mdebug section is present
17721 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 17722 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 17723 information correctly. We can't use the ecoff routines because they
252b5132
RH
17724 make reference to the ecoff symbol table (in the mdebug section). */
17725
17726static void
17a2f251 17727s_mips_mask (int reg_type)
252b5132 17728{
f3ded42a
RS
17729 if (ECOFF_DEBUGGING)
17730 s_ignore (reg_type);
17731 else
252b5132 17732 {
ecb4347a 17733 long mask, off;
252b5132 17734
ecb4347a
DJ
17735 if (cur_proc_ptr == (procS *) NULL)
17736 {
17737 as_warn (_(".mask/.fmask outside of .ent"));
17738 demand_empty_rest_of_line ();
17739 return;
17740 }
252b5132 17741
ecb4347a
DJ
17742 if (get_absolute_expression_and_terminator (&mask) != ',')
17743 {
17744 as_warn (_("Bad .mask/.fmask directive"));
17745 --input_line_pointer;
17746 demand_empty_rest_of_line ();
17747 return;
17748 }
252b5132 17749
ecb4347a
DJ
17750 off = get_absolute_expression ();
17751
17752 if (reg_type == 'F')
17753 {
17754 cur_proc_ptr->fpreg_mask = mask;
17755 cur_proc_ptr->fpreg_offset = off;
17756 }
17757 else
17758 {
17759 cur_proc_ptr->reg_mask = mask;
17760 cur_proc_ptr->reg_offset = off;
17761 }
17762
17763 demand_empty_rest_of_line ();
252b5132 17764 }
252b5132
RH
17765}
17766
316f5878
RS
17767/* A table describing all the processors gas knows about. Names are
17768 matched in the order listed.
e7af610e 17769
316f5878
RS
17770 To ease comparison, please keep this table in the same order as
17771 gcc's mips_cpu_info_table[]. */
e972090a
NC
17772static const struct mips_cpu_info mips_cpu_info_table[] =
17773{
316f5878 17774 /* Entries for generic ISAs */
d16afab6
RS
17775 { "mips1", MIPS_CPU_IS_ISA, 0, ISA_MIPS1, CPU_R3000 },
17776 { "mips2", MIPS_CPU_IS_ISA, 0, ISA_MIPS2, CPU_R6000 },
17777 { "mips3", MIPS_CPU_IS_ISA, 0, ISA_MIPS3, CPU_R4000 },
17778 { "mips4", MIPS_CPU_IS_ISA, 0, ISA_MIPS4, CPU_R8000 },
17779 { "mips5", MIPS_CPU_IS_ISA, 0, ISA_MIPS5, CPU_MIPS5 },
17780 { "mips32", MIPS_CPU_IS_ISA, 0, ISA_MIPS32, CPU_MIPS32 },
17781 { "mips32r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17782 { "mips64", MIPS_CPU_IS_ISA, 0, ISA_MIPS64, CPU_MIPS64 },
17783 { "mips64r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R2, CPU_MIPS64R2 },
316f5878
RS
17784
17785 /* MIPS I */
d16afab6
RS
17786 { "r3000", 0, 0, ISA_MIPS1, CPU_R3000 },
17787 { "r2000", 0, 0, ISA_MIPS1, CPU_R3000 },
17788 { "r3900", 0, 0, ISA_MIPS1, CPU_R3900 },
316f5878
RS
17789
17790 /* MIPS II */
d16afab6 17791 { "r6000", 0, 0, ISA_MIPS2, CPU_R6000 },
316f5878
RS
17792
17793 /* MIPS III */
d16afab6
RS
17794 { "r4000", 0, 0, ISA_MIPS3, CPU_R4000 },
17795 { "r4010", 0, 0, ISA_MIPS2, CPU_R4010 },
17796 { "vr4100", 0, 0, ISA_MIPS3, CPU_VR4100 },
17797 { "vr4111", 0, 0, ISA_MIPS3, CPU_R4111 },
17798 { "vr4120", 0, 0, ISA_MIPS3, CPU_VR4120 },
17799 { "vr4130", 0, 0, ISA_MIPS3, CPU_VR4120 },
17800 { "vr4181", 0, 0, ISA_MIPS3, CPU_R4111 },
17801 { "vr4300", 0, 0, ISA_MIPS3, CPU_R4300 },
17802 { "r4400", 0, 0, ISA_MIPS3, CPU_R4400 },
17803 { "r4600", 0, 0, ISA_MIPS3, CPU_R4600 },
17804 { "orion", 0, 0, ISA_MIPS3, CPU_R4600 },
17805 { "r4650", 0, 0, ISA_MIPS3, CPU_R4650 },
17806 { "r5900", 0, 0, ISA_MIPS3, CPU_R5900 },
b15591bb 17807 /* ST Microelectronics Loongson 2E and 2F cores */
d16afab6
RS
17808 { "loongson2e", 0, 0, ISA_MIPS3, CPU_LOONGSON_2E },
17809 { "loongson2f", 0, 0, ISA_MIPS3, CPU_LOONGSON_2F },
316f5878
RS
17810
17811 /* MIPS IV */
d16afab6
RS
17812 { "r8000", 0, 0, ISA_MIPS4, CPU_R8000 },
17813 { "r10000", 0, 0, ISA_MIPS4, CPU_R10000 },
17814 { "r12000", 0, 0, ISA_MIPS4, CPU_R12000 },
17815 { "r14000", 0, 0, ISA_MIPS4, CPU_R14000 },
17816 { "r16000", 0, 0, ISA_MIPS4, CPU_R16000 },
17817 { "vr5000", 0, 0, ISA_MIPS4, CPU_R5000 },
17818 { "vr5400", 0, 0, ISA_MIPS4, CPU_VR5400 },
17819 { "vr5500", 0, 0, ISA_MIPS4, CPU_VR5500 },
17820 { "rm5200", 0, 0, ISA_MIPS4, CPU_R5000 },
17821 { "rm5230", 0, 0, ISA_MIPS4, CPU_R5000 },
17822 { "rm5231", 0, 0, ISA_MIPS4, CPU_R5000 },
17823 { "rm5261", 0, 0, ISA_MIPS4, CPU_R5000 },
17824 { "rm5721", 0, 0, ISA_MIPS4, CPU_R5000 },
17825 { "rm7000", 0, 0, ISA_MIPS4, CPU_RM7000 },
17826 { "rm9000", 0, 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
17827
17828 /* MIPS 32 */
d16afab6
RS
17829 { "4kc", 0, 0, ISA_MIPS32, CPU_MIPS32 },
17830 { "4km", 0, 0, ISA_MIPS32, CPU_MIPS32 },
17831 { "4kp", 0, 0, ISA_MIPS32, CPU_MIPS32 },
17832 { "4ksc", 0, ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
ad3fea08
TS
17833
17834 /* MIPS 32 Release 2 */
d16afab6
RS
17835 { "4kec", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17836 { "4kem", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17837 { "4kep", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17838 { "4ksd", 0, ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
17839 { "m4k", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17840 { "m4kp", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17841 { "m14k", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
17842 { "m14kc", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
17843 { "m14ke", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
17844 ISA_MIPS32R2, CPU_MIPS32R2 },
17845 { "m14kec", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
17846 ISA_MIPS32R2, CPU_MIPS32R2 },
17847 { "24kc", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17848 { "24kf2_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17849 { "24kf", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17850 { "24kf1_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 17851 /* Deprecated forms of the above. */
d16afab6
RS
17852 { "24kfx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17853 { "24kx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 17854 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
d16afab6
RS
17855 { "24kec", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
17856 { "24kef2_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
17857 { "24kef", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
17858 { "24kef1_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 17859 /* Deprecated forms of the above. */
d16afab6
RS
17860 { "24kefx", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
17861 { "24kex", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 17862 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
d16afab6
RS
17863 { "34kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17864 { "34kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17865 { "34kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17866 { "34kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 17867 /* Deprecated forms of the above. */
d16afab6
RS
17868 { "34kfx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17869 { "34kx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
711eefe4 17870 /* 34Kn is a 34kc without DSP. */
d16afab6 17871 { "34kn", 0, ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 17872 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
d16afab6
RS
17873 { "74kc", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
17874 { "74kf2_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
17875 { "74kf", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
17876 { "74kf1_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
17877 { "74kf3_2", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 17878 /* Deprecated forms of the above. */
d16afab6
RS
17879 { "74kfx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
17880 { "74kx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
30f8113a 17881 /* 1004K cores are multiprocessor versions of the 34K. */
d16afab6
RS
17882 { "1004kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17883 { "1004kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17884 { "1004kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17885 { "1004kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
32b26a03 17886
316f5878 17887 /* MIPS 64 */
d16afab6
RS
17888 { "5kc", 0, 0, ISA_MIPS64, CPU_MIPS64 },
17889 { "5kf", 0, 0, ISA_MIPS64, CPU_MIPS64 },
17890 { "20kc", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
17891 { "25kf", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
ad3fea08 17892
c7a23324 17893 /* Broadcom SB-1 CPU core */
d16afab6 17894 { "sb1", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
1e85aad8 17895 /* Broadcom SB-1A CPU core */
d16afab6 17896 { "sb1a", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
d051516a 17897
d16afab6 17898 { "loongson3a", 0, 0, ISA_MIPS64, CPU_LOONGSON_3A },
e7af610e 17899
ed163775
MR
17900 /* MIPS 64 Release 2 */
17901
967344c6 17902 /* Cavium Networks Octeon CPU core */
d16afab6
RS
17903 { "octeon", 0, 0, ISA_MIPS64R2, CPU_OCTEON },
17904 { "octeon+", 0, 0, ISA_MIPS64R2, CPU_OCTEONP },
17905 { "octeon2", 0, 0, ISA_MIPS64R2, CPU_OCTEON2 },
967344c6 17906
52b6b6b9 17907 /* RMI Xlr */
d16afab6 17908 { "xlr", 0, 0, ISA_MIPS64, CPU_XLR },
52b6b6b9 17909
55a36193
MK
17910 /* Broadcom XLP.
17911 XLP is mostly like XLR, with the prominent exception that it is
17912 MIPS64R2 rather than MIPS64. */
d16afab6 17913 { "xlp", 0, 0, ISA_MIPS64R2, CPU_XLR },
55a36193 17914
316f5878 17915 /* End marker */
d16afab6 17916 { NULL, 0, 0, 0, 0 }
316f5878 17917};
e7af610e 17918
84ea6cf2 17919
316f5878
RS
17920/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
17921 with a final "000" replaced by "k". Ignore case.
e7af610e 17922
316f5878 17923 Note: this function is shared between GCC and GAS. */
c6c98b38 17924
b34976b6 17925static bfd_boolean
17a2f251 17926mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
17927{
17928 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
17929 given++, canonical++;
17930
17931 return ((*given == 0 && *canonical == 0)
17932 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
17933}
17934
17935
17936/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
17937 CPU name. We've traditionally allowed a lot of variation here.
17938
17939 Note: this function is shared between GCC and GAS. */
17940
b34976b6 17941static bfd_boolean
17a2f251 17942mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
17943{
17944 /* First see if the name matches exactly, or with a final "000"
17945 turned into "k". */
17946 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 17947 return TRUE;
316f5878
RS
17948
17949 /* If not, try comparing based on numerical designation alone.
17950 See if GIVEN is an unadorned number, or 'r' followed by a number. */
17951 if (TOLOWER (*given) == 'r')
17952 given++;
17953 if (!ISDIGIT (*given))
b34976b6 17954 return FALSE;
316f5878
RS
17955
17956 /* Skip over some well-known prefixes in the canonical name,
17957 hoping to find a number there too. */
17958 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
17959 canonical += 2;
17960 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
17961 canonical += 2;
17962 else if (TOLOWER (canonical[0]) == 'r')
17963 canonical += 1;
17964
17965 return mips_strict_matching_cpu_name_p (canonical, given);
17966}
17967
17968
17969/* Parse an option that takes the name of a processor as its argument.
17970 OPTION is the name of the option and CPU_STRING is the argument.
17971 Return the corresponding processor enumeration if the CPU_STRING is
17972 recognized, otherwise report an error and return null.
17973
17974 A similar function exists in GCC. */
e7af610e
NC
17975
17976static const struct mips_cpu_info *
17a2f251 17977mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 17978{
316f5878 17979 const struct mips_cpu_info *p;
e7af610e 17980
316f5878
RS
17981 /* 'from-abi' selects the most compatible architecture for the given
17982 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
17983 EABIs, we have to decide whether we're using the 32-bit or 64-bit
17984 version. Look first at the -mgp options, if given, otherwise base
17985 the choice on MIPS_DEFAULT_64BIT.
e7af610e 17986
316f5878
RS
17987 Treat NO_ABI like the EABIs. One reason to do this is that the
17988 plain 'mips' and 'mips64' configs have 'from-abi' as their default
17989 architecture. This code picks MIPS I for 'mips' and MIPS III for
17990 'mips64', just as we did in the days before 'from-abi'. */
17991 if (strcasecmp (cpu_string, "from-abi") == 0)
17992 {
17993 if (ABI_NEEDS_32BIT_REGS (mips_abi))
17994 return mips_cpu_info_from_isa (ISA_MIPS1);
17995
17996 if (ABI_NEEDS_64BIT_REGS (mips_abi))
17997 return mips_cpu_info_from_isa (ISA_MIPS3);
17998
17999 if (file_mips_gp32 >= 0)
18000 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
18001
18002 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
18003 ? ISA_MIPS3
18004 : ISA_MIPS1);
18005 }
18006
18007 /* 'default' has traditionally been a no-op. Probably not very useful. */
18008 if (strcasecmp (cpu_string, "default") == 0)
18009 return 0;
18010
18011 for (p = mips_cpu_info_table; p->name != 0; p++)
18012 if (mips_matching_cpu_name_p (p->name, cpu_string))
18013 return p;
18014
20203fb9 18015 as_bad (_("Bad value (%s) for %s"), cpu_string, option);
316f5878 18016 return 0;
e7af610e
NC
18017}
18018
316f5878
RS
18019/* Return the canonical processor information for ISA (a member of the
18020 ISA_MIPS* enumeration). */
18021
e7af610e 18022static const struct mips_cpu_info *
17a2f251 18023mips_cpu_info_from_isa (int isa)
e7af610e
NC
18024{
18025 int i;
18026
18027 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
ad3fea08 18028 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
316f5878 18029 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
18030 return (&mips_cpu_info_table[i]);
18031
e972090a 18032 return NULL;
e7af610e 18033}
fef14a42
TS
18034
18035static const struct mips_cpu_info *
17a2f251 18036mips_cpu_info_from_arch (int arch)
fef14a42
TS
18037{
18038 int i;
18039
18040 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
18041 if (arch == mips_cpu_info_table[i].cpu)
18042 return (&mips_cpu_info_table[i]);
18043
18044 return NULL;
18045}
316f5878
RS
18046\f
18047static void
17a2f251 18048show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
18049{
18050 if (*first_p)
18051 {
18052 fprintf (stream, "%24s", "");
18053 *col_p = 24;
18054 }
18055 else
18056 {
18057 fprintf (stream, ", ");
18058 *col_p += 2;
18059 }
e7af610e 18060
316f5878
RS
18061 if (*col_p + strlen (string) > 72)
18062 {
18063 fprintf (stream, "\n%24s", "");
18064 *col_p = 24;
18065 }
18066
18067 fprintf (stream, "%s", string);
18068 *col_p += strlen (string);
18069
18070 *first_p = 0;
18071}
18072
18073void
17a2f251 18074md_show_usage (FILE *stream)
e7af610e 18075{
316f5878
RS
18076 int column, first;
18077 size_t i;
18078
18079 fprintf (stream, _("\
18080MIPS options:\n\
316f5878
RS
18081-EB generate big endian output\n\
18082-EL generate little endian output\n\
18083-g, -g2 do not remove unneeded NOPs or swap branches\n\
18084-G NUM allow referencing objects up to NUM bytes\n\
18085 implicitly with the gp register [default 8]\n"));
18086 fprintf (stream, _("\
18087-mips1 generate MIPS ISA I instructions\n\
18088-mips2 generate MIPS ISA II instructions\n\
18089-mips3 generate MIPS ISA III instructions\n\
18090-mips4 generate MIPS ISA IV instructions\n\
18091-mips5 generate MIPS ISA V instructions\n\
18092-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 18093-mips32r2 generate MIPS32 release 2 ISA instructions\n\
316f5878 18094-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 18095-mips64r2 generate MIPS64 release 2 ISA instructions\n\
316f5878
RS
18096-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
18097
18098 first = 1;
e7af610e
NC
18099
18100 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
18101 show (stream, mips_cpu_info_table[i].name, &column, &first);
18102 show (stream, "from-abi", &column, &first);
18103 fputc ('\n', stream);
e7af610e 18104
316f5878
RS
18105 fprintf (stream, _("\
18106-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
18107-no-mCPU don't generate code specific to CPU.\n\
18108 For -mCPU and -no-mCPU, CPU must be one of:\n"));
18109
18110 first = 1;
18111
18112 show (stream, "3900", &column, &first);
18113 show (stream, "4010", &column, &first);
18114 show (stream, "4100", &column, &first);
18115 show (stream, "4650", &column, &first);
18116 fputc ('\n', stream);
18117
18118 fprintf (stream, _("\
18119-mips16 generate mips16 instructions\n\
18120-no-mips16 do not generate mips16 instructions\n"));
18121 fprintf (stream, _("\
df58fc94
RS
18122-mmicromips generate microMIPS instructions\n\
18123-mno-micromips do not generate microMIPS instructions\n"));
18124 fprintf (stream, _("\
e16bfa71
TS
18125-msmartmips generate smartmips instructions\n\
18126-mno-smartmips do not generate smartmips instructions\n"));
18127 fprintf (stream, _("\
74cd071d
CF
18128-mdsp generate DSP instructions\n\
18129-mno-dsp do not generate DSP instructions\n"));
18130 fprintf (stream, _("\
8b082fb1
TS
18131-mdspr2 generate DSP R2 instructions\n\
18132-mno-dspr2 do not generate DSP R2 instructions\n"));
18133 fprintf (stream, _("\
ef2e4d86
CF
18134-mmt generate MT instructions\n\
18135-mno-mt do not generate MT instructions\n"));
18136 fprintf (stream, _("\
dec0624d
MR
18137-mmcu generate MCU instructions\n\
18138-mno-mcu do not generate MCU instructions\n"));
18139 fprintf (stream, _("\
b015e599
AP
18140-mvirt generate Virtualization instructions\n\
18141-mno-virt do not generate Virtualization instructions\n"));
18142 fprintf (stream, _("\
833794fc
MR
18143-minsn32 only generate 32-bit microMIPS instructions\n\
18144-mno-insn32 generate all microMIPS instructions\n"));
18145 fprintf (stream, _("\
c67a084a
NC
18146-mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
18147-mfix-loongson2f-nop work around Loongson2F NOP errata\n\
d766e8ec 18148-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 18149-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
6a32d874 18150-mfix-24k insert a nop after ERET and DERET instructions\n\
d954098f 18151-mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
316f5878
RS
18152-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
18153-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261 18154-msym32 assume all symbols have 32-bit values\n\
316f5878
RS
18155-O0 remove unneeded NOPs, do not swap branches\n\
18156-O remove unneeded NOPs and swap branches\n\
316f5878
RS
18157--trap, --no-break trap exception on div by 0 and mult overflow\n\
18158--break, --no-trap break exception on div by 0 and mult overflow\n"));
037b32b9
AN
18159 fprintf (stream, _("\
18160-mhard-float allow floating-point instructions\n\
18161-msoft-float do not allow floating-point instructions\n\
18162-msingle-float only allow 32-bit floating-point operations\n\
18163-mdouble-float allow 32-bit and 64-bit floating-point operations\n\
3bf0dbfb 18164--[no-]construct-floats [dis]allow floating point values to be constructed\n\
ba92f887
MR
18165--[no-]relax-branch [dis]allow out-of-range branches to be relaxed\n\
18166-mnan=ENCODING select an IEEE 754 NaN encoding convention, either of:\n"));
18167
18168 first = 1;
18169
18170 show (stream, "legacy", &column, &first);
18171 show (stream, "2008", &column, &first);
18172
18173 fputc ('\n', stream);
18174
316f5878
RS
18175 fprintf (stream, _("\
18176-KPIC, -call_shared generate SVR4 position independent code\n\
861fb55a 18177-call_nonpic generate non-PIC code that can operate with DSOs\n\
0c000745 18178-mvxworks-pic generate VxWorks position independent code\n\
861fb55a 18179-non_shared do not generate code that can operate with DSOs\n\
316f5878 18180-xgot assume a 32 bit GOT\n\
dcd410fe 18181-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8 18182-mshared, -mno-shared disable/enable .cpload optimization for\n\
d821e36b 18183 position dependent (non shared) code\n\
316f5878
RS
18184-mabi=ABI create ABI conformant object file for:\n"));
18185
18186 first = 1;
18187
18188 show (stream, "32", &column, &first);
18189 show (stream, "o64", &column, &first);
18190 show (stream, "n32", &column, &first);
18191 show (stream, "64", &column, &first);
18192 show (stream, "eabi", &column, &first);
18193
18194 fputc ('\n', stream);
18195
18196 fprintf (stream, _("\
18197-32 create o32 ABI object file (default)\n\
18198-n32 create n32 ABI object file\n\
18199-64 create 64 ABI object file\n"));
e7af610e 18200}
14e777e0 18201
1575952e 18202#ifdef TE_IRIX
14e777e0 18203enum dwarf2_format
413a266c 18204mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
14e777e0 18205{
369943fe 18206 if (HAVE_64BIT_SYMBOLS)
1575952e 18207 return dwarf2_format_64bit_irix;
14e777e0
KB
18208 else
18209 return dwarf2_format_32bit;
18210}
1575952e 18211#endif
73369e65
EC
18212
18213int
18214mips_dwarf2_addr_size (void)
18215{
6b6b3450 18216 if (HAVE_64BIT_OBJECTS)
73369e65 18217 return 8;
73369e65
EC
18218 else
18219 return 4;
18220}
5862107c
EC
18221
18222/* Standard calling conventions leave the CFA at SP on entry. */
18223void
18224mips_cfi_frame_initial_instructions (void)
18225{
18226 cfi_add_CFA_def_cfa_register (SP);
18227}
18228
707bfff6
TS
18229int
18230tc_mips_regname_to_dw2regnum (char *regname)
18231{
18232 unsigned int regnum = -1;
18233 unsigned int reg;
18234
18235 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
18236 regnum = reg;
18237
18238 return regnum;
18239}
This page took 2.712743 seconds and 4 git commands to generate.