daily update
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
CommitLineData
252b5132 1/* tc-mips.c -- assemble code for a MIPS chip.
81912461 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
45dfa85a 3 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
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
RH
36
37#ifdef DEBUG
38#define DBG(x) printf x
39#else
40#define DBG(x)
41#endif
42
43#ifdef OBJ_MAYBE_ELF
44/* Clean up namespace so we can include obj-elf.h too. */
17a2f251
TS
45static int mips_output_flavor (void);
46static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
252b5132
RH
47#undef OBJ_PROCESS_STAB
48#undef OUTPUT_FLAVOR
49#undef S_GET_ALIGN
50#undef S_GET_SIZE
51#undef S_SET_ALIGN
52#undef S_SET_SIZE
252b5132
RH
53#undef obj_frob_file
54#undef obj_frob_file_after_relocs
55#undef obj_frob_symbol
56#undef obj_pop_insert
57#undef obj_sec_sym_ok_for_reloc
58#undef OBJ_COPY_SYMBOL_ATTRIBUTES
59
60#include "obj-elf.h"
61/* Fix any of them that we actually care about. */
62#undef OUTPUT_FLAVOR
63#define OUTPUT_FLAVOR mips_output_flavor()
64#endif
65
66#if defined (OBJ_ELF)
67#include "elf/mips.h"
68#endif
69
70#ifndef ECOFF_DEBUGGING
71#define NO_ECOFF_DEBUGGING
72#define ECOFF_DEBUGGING 0
73#endif
74
ecb4347a
DJ
75int mips_flag_mdebug = -1;
76
dcd410fe
RO
77/* Control generation of .pdr sections. Off by default on IRIX: the native
78 linker doesn't know about and discards them, but relocations against them
79 remain, leading to rld crashes. */
80#ifdef TE_IRIX
81int mips_flag_pdr = FALSE;
82#else
83int mips_flag_pdr = TRUE;
84#endif
85
252b5132
RH
86#include "ecoff.h"
87
88#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
89static char *mips_regmask_frag;
90#endif
91
85b51719 92#define ZERO 0
741fe287 93#define ATREG 1
df58fc94
RS
94#define S0 16
95#define S7 23
252b5132
RH
96#define TREG 24
97#define PIC_CALL_REG 25
98#define KT0 26
99#define KT1 27
100#define GP 28
101#define SP 29
102#define FP 30
103#define RA 31
104
105#define ILLEGAL_REG (32)
106
741fe287
MR
107#define AT mips_opts.at
108
252b5132
RH
109/* Allow override of standard little-endian ECOFF format. */
110
111#ifndef ECOFF_LITTLE_FORMAT
112#define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
113#endif
114
115extern int target_big_endian;
116
252b5132 117/* The name of the readonly data section. */
4d0d148d 118#define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
252b5132 119 ? ".rdata" \
056350c6
NC
120 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
121 ? ".rdata" \
252b5132
RH
122 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
123 ? ".rodata" \
124 : (abort (), ""))
125
a4e06468
RS
126/* Ways in which an instruction can be "appended" to the output. */
127enum append_method {
128 /* Just add it normally. */
129 APPEND_ADD,
130
131 /* Add it normally and then add a nop. */
132 APPEND_ADD_WITH_NOP,
133
134 /* Turn an instruction with a delay slot into a "compact" version. */
135 APPEND_ADD_COMPACT,
136
137 /* Insert the instruction before the last one. */
138 APPEND_SWAP
139};
140
47e39b9d
RS
141/* Information about an instruction, including its format, operands
142 and fixups. */
143struct mips_cl_insn
144{
145 /* The opcode's entry in mips_opcodes or mips16_opcodes. */
146 const struct mips_opcode *insn_mo;
147
47e39b9d 148 /* The 16-bit or 32-bit bitstring of the instruction itself. This is
5c04167a
RS
149 a copy of INSN_MO->match with the operands filled in. If we have
150 decided to use an extended MIPS16 instruction, this includes the
151 extension. */
47e39b9d
RS
152 unsigned long insn_opcode;
153
154 /* The frag that contains the instruction. */
155 struct frag *frag;
156
157 /* The offset into FRAG of the first instruction byte. */
158 long where;
159
160 /* The relocs associated with the instruction, if any. */
161 fixS *fixp[3];
162
a38419a5
RS
163 /* True if this entry cannot be moved from its current position. */
164 unsigned int fixed_p : 1;
47e39b9d 165
708587a4 166 /* True if this instruction occurred in a .set noreorder block. */
47e39b9d
RS
167 unsigned int noreorder_p : 1;
168
2fa15973
RS
169 /* True for mips16 instructions that jump to an absolute address. */
170 unsigned int mips16_absolute_jump_p : 1;
15be625d
CM
171
172 /* True if this instruction is complete. */
173 unsigned int complete_p : 1;
47e39b9d
RS
174};
175
a325df1d
TS
176/* The ABI to use. */
177enum mips_abi_level
178{
179 NO_ABI = 0,
180 O32_ABI,
181 O64_ABI,
182 N32_ABI,
183 N64_ABI,
184 EABI_ABI
185};
186
187/* MIPS ABI we are using for this output file. */
316f5878 188static enum mips_abi_level mips_abi = NO_ABI;
a325df1d 189
143d77c5
EC
190/* Whether or not we have code that can call pic code. */
191int mips_abicalls = FALSE;
192
aa6975fb
ILT
193/* Whether or not we have code which can be put into a shared
194 library. */
195static bfd_boolean mips_in_shared = TRUE;
196
252b5132
RH
197/* This is the set of options which may be modified by the .set
198 pseudo-op. We use a struct so that .set push and .set pop are more
199 reliable. */
200
e972090a
NC
201struct mips_set_options
202{
252b5132
RH
203 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
204 if it has not been initialized. Changed by `.set mipsN', and the
205 -mipsN command line option, and the default CPU. */
206 int isa;
1f25f5d3
CD
207 /* Enabled Application Specific Extensions (ASEs). These are set to -1
208 if they have not been initialized. Changed by `.set <asename>', by
209 command line options, and based on the default architecture. */
210 int ase_mips3d;
deec1734 211 int ase_mdmx;
e16bfa71 212 int ase_smartmips;
74cd071d 213 int ase_dsp;
8b082fb1 214 int ase_dspr2;
ef2e4d86 215 int ase_mt;
dec0624d 216 int ase_mcu;
252b5132
RH
217 /* Whether we are assembling for the mips16 processor. 0 if we are
218 not, 1 if we are, and -1 if the value has not been initialized.
219 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
220 -nomips16 command line options, and the default CPU. */
221 int mips16;
df58fc94
RS
222 /* Whether we are assembling for the mipsMIPS ASE. 0 if we are not,
223 1 if we are, and -1 if the value has not been initialized. Changed
224 by `.set micromips' and `.set nomicromips', and the -mmicromips
225 and -mno-micromips command line options, and the default CPU. */
226 int micromips;
252b5132
RH
227 /* Non-zero if we should not reorder instructions. Changed by `.set
228 reorder' and `.set noreorder'. */
229 int noreorder;
741fe287
MR
230 /* Non-zero if we should not permit the register designated "assembler
231 temporary" to be used in instructions. The value is the register
232 number, normally $at ($1). Changed by `.set at=REG', `.set noat'
233 (same as `.set at=$0') and `.set at' (same as `.set at=$1'). */
234 unsigned int at;
252b5132
RH
235 /* Non-zero if we should warn when a macro instruction expands into
236 more than one machine instruction. Changed by `.set nomacro' and
237 `.set macro'. */
238 int warn_about_macros;
239 /* Non-zero if we should not move instructions. Changed by `.set
240 move', `.set volatile', `.set nomove', and `.set novolatile'. */
241 int nomove;
242 /* Non-zero if we should not optimize branches by moving the target
243 of the branch into the delay slot. Actually, we don't perform
244 this optimization anyhow. Changed by `.set bopt' and `.set
245 nobopt'. */
246 int nobopt;
247 /* Non-zero if we should not autoextend mips16 instructions.
248 Changed by `.set autoextend' and `.set noautoextend'. */
249 int noautoextend;
a325df1d
TS
250 /* Restrict general purpose registers and floating point registers
251 to 32 bit. This is initially determined when -mgp32 or -mfp32
252 is passed but can changed if the assembler code uses .set mipsN. */
253 int gp32;
254 int fp32;
fef14a42
TS
255 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
256 command line option, and the default CPU. */
257 int arch;
aed1a261
RS
258 /* True if ".set sym32" is in effect. */
259 bfd_boolean sym32;
037b32b9
AN
260 /* True if floating-point operations are not allowed. Changed by .set
261 softfloat or .set hardfloat, by command line options -msoft-float or
262 -mhard-float. The default is false. */
263 bfd_boolean soft_float;
264
265 /* True if only single-precision floating-point operations are allowed.
266 Changed by .set singlefloat or .set doublefloat, command-line options
267 -msingle-float or -mdouble-float. The default is false. */
268 bfd_boolean single_float;
252b5132
RH
269};
270
037b32b9
AN
271/* This is the struct we use to hold the current set of options. Note
272 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
273 -1 to indicate that they have not been initialized. */
274
a325df1d 275/* True if -mgp32 was passed. */
a8e8e863 276static int file_mips_gp32 = -1;
a325df1d
TS
277
278/* True if -mfp32 was passed. */
a8e8e863 279static int file_mips_fp32 = -1;
a325df1d 280
037b32b9
AN
281/* 1 if -msoft-float, 0 if -mhard-float. The default is 0. */
282static int file_mips_soft_float = 0;
283
284/* 1 if -msingle-float, 0 if -mdouble-float. The default is 0. */
285static int file_mips_single_float = 0;
252b5132 286
e972090a
NC
287static struct mips_set_options mips_opts =
288{
037b32b9
AN
289 /* isa */ ISA_UNKNOWN, /* ase_mips3d */ -1, /* ase_mdmx */ -1,
290 /* ase_smartmips */ 0, /* ase_dsp */ -1, /* ase_dspr2 */ -1, /* ase_mt */ -1,
dec0624d
MR
291 /* ase_mcu */ -1, /* mips16 */ -1, /* micromips */ -1, /* noreorder */ 0,
292 /* at */ ATREG, /* warn_about_macros */ 0, /* nomove */ 0, /* nobopt */ 0,
037b32b9
AN
293 /* noautoextend */ 0, /* gp32 */ 0, /* fp32 */ 0, /* arch */ CPU_UNKNOWN,
294 /* sym32 */ FALSE, /* soft_float */ FALSE, /* single_float */ FALSE
e7af610e 295};
252b5132
RH
296
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
1f25f5d3
CD
329/* True if -mips3d was passed or implied by arguments passed on the
330 command line (e.g., by -march). */
331static int file_ase_mips3d;
332
deec1734
CD
333/* True if -mdmx was passed or implied by arguments passed on the
334 command line (e.g., by -march). */
335static int file_ase_mdmx;
336
e16bfa71
TS
337/* True if -msmartmips was passed or implied by arguments passed on the
338 command line (e.g., by -march). */
339static int file_ase_smartmips;
340
ad3fea08
TS
341#define ISA_SUPPORTS_SMARTMIPS (mips_opts.isa == ISA_MIPS32 \
342 || mips_opts.isa == ISA_MIPS32R2)
e16bfa71 343
74cd071d
CF
344/* True if -mdsp was passed or implied by arguments passed on the
345 command line (e.g., by -march). */
346static int file_ase_dsp;
347
ad3fea08 348#define ISA_SUPPORTS_DSP_ASE (mips_opts.isa == ISA_MIPS32R2 \
03f66e8a
MR
349 || mips_opts.isa == ISA_MIPS64R2 \
350 || mips_opts.micromips)
ad3fea08 351
65263ce3
TS
352#define ISA_SUPPORTS_DSP64_ASE (mips_opts.isa == ISA_MIPS64R2)
353
8b082fb1
TS
354/* True if -mdspr2 was passed or implied by arguments passed on the
355 command line (e.g., by -march). */
356static int file_ase_dspr2;
357
358#define ISA_SUPPORTS_DSPR2_ASE (mips_opts.isa == ISA_MIPS32R2 \
03f66e8a
MR
359 || mips_opts.isa == ISA_MIPS64R2 \
360 || mips_opts.micromips)
8b082fb1 361
ef2e4d86
CF
362/* True if -mmt was passed or implied by arguments passed on the
363 command line (e.g., by -march). */
364static int file_ase_mt;
365
ad3fea08
TS
366#define ISA_SUPPORTS_MT_ASE (mips_opts.isa == ISA_MIPS32R2 \
367 || mips_opts.isa == ISA_MIPS64R2)
368
dec0624d 369#define ISA_SUPPORTS_MCU_ASE (mips_opts.isa == ISA_MIPS32R2 \
9ddc84cc
MR
370 || mips_opts.isa == ISA_MIPS64R2 \
371 || mips_opts.micromips)
dec0624d 372
ec68c924 373/* The argument of the -march= flag. The architecture we are assembling. */
fef14a42 374static int file_mips_arch = CPU_UNKNOWN;
316f5878 375static const char *mips_arch_string;
ec68c924
EC
376
377/* The argument of the -mtune= flag. The architecture for which we
378 are optimizing. */
379static int mips_tune = CPU_UNKNOWN;
316f5878 380static const char *mips_tune_string;
ec68c924 381
316f5878 382/* True when generating 32-bit code for a 64-bit processor. */
252b5132
RH
383static int mips_32bitmode = 0;
384
316f5878
RS
385/* True if the given ABI requires 32-bit registers. */
386#define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
387
388/* Likewise 64-bit registers. */
707bfff6
TS
389#define ABI_NEEDS_64BIT_REGS(ABI) \
390 ((ABI) == N32_ABI \
391 || (ABI) == N64_ABI \
316f5878
RS
392 || (ABI) == O64_ABI)
393
ad3fea08 394/* Return true if ISA supports 64 bit wide gp registers. */
707bfff6
TS
395#define ISA_HAS_64BIT_REGS(ISA) \
396 ((ISA) == ISA_MIPS3 \
397 || (ISA) == ISA_MIPS4 \
398 || (ISA) == ISA_MIPS5 \
399 || (ISA) == ISA_MIPS64 \
400 || (ISA) == ISA_MIPS64R2)
9ce8a5dd 401
ad3fea08
TS
402/* Return true if ISA supports 64 bit wide float registers. */
403#define ISA_HAS_64BIT_FPRS(ISA) \
404 ((ISA) == ISA_MIPS3 \
405 || (ISA) == ISA_MIPS4 \
406 || (ISA) == ISA_MIPS5 \
407 || (ISA) == ISA_MIPS32R2 \
408 || (ISA) == ISA_MIPS64 \
409 || (ISA) == ISA_MIPS64R2)
410
af7ee8bf
CD
411/* Return true if ISA supports 64-bit right rotate (dror et al.)
412 instructions. */
707bfff6 413#define ISA_HAS_DROR(ISA) \
df58fc94
RS
414 ((ISA) == ISA_MIPS64R2 \
415 || (mips_opts.micromips \
416 && ISA_HAS_64BIT_REGS (ISA)) \
417 )
af7ee8bf
CD
418
419/* Return true if ISA supports 32-bit right rotate (ror et al.)
420 instructions. */
707bfff6
TS
421#define ISA_HAS_ROR(ISA) \
422 ((ISA) == ISA_MIPS32R2 \
423 || (ISA) == ISA_MIPS64R2 \
df58fc94
RS
424 || mips_opts.ase_smartmips \
425 || mips_opts.micromips \
426 )
707bfff6 427
7455baf8
TS
428/* Return true if ISA supports single-precision floats in odd registers. */
429#define ISA_HAS_ODD_SINGLE_FPR(ISA) \
430 ((ISA) == ISA_MIPS32 \
431 || (ISA) == ISA_MIPS32R2 \
432 || (ISA) == ISA_MIPS64 \
433 || (ISA) == ISA_MIPS64R2)
af7ee8bf 434
ad3fea08
TS
435/* Return true if ISA supports move to/from high part of a 64-bit
436 floating-point register. */
437#define ISA_HAS_MXHC1(ISA) \
438 ((ISA) == ISA_MIPS32R2 \
439 || (ISA) == ISA_MIPS64R2)
440
e013f690 441#define HAVE_32BIT_GPRS \
ad3fea08 442 (mips_opts.gp32 || !ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257 443
e013f690 444#define HAVE_32BIT_FPRS \
ad3fea08 445 (mips_opts.fp32 || !ISA_HAS_64BIT_FPRS (mips_opts.isa))
ca4e0257 446
ad3fea08
TS
447#define HAVE_64BIT_GPRS (!HAVE_32BIT_GPRS)
448#define HAVE_64BIT_FPRS (!HAVE_32BIT_FPRS)
ca4e0257 449
316f5878 450#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
e013f690 451
316f5878 452#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
e013f690 453
3b91255e
RS
454/* True if relocations are stored in-place. */
455#define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
456
aed1a261
RS
457/* The ABI-derived address size. */
458#define HAVE_64BIT_ADDRESSES \
459 (HAVE_64BIT_GPRS && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
460#define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
e013f690 461
aed1a261
RS
462/* The size of symbolic constants (i.e., expressions of the form
463 "SYMBOL" or "SYMBOL + OFFSET"). */
464#define HAVE_32BIT_SYMBOLS \
465 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
466#define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
ca4e0257 467
b7c7d6c1
TS
468/* Addresses are loaded in different ways, depending on the address size
469 in use. The n32 ABI Documentation also mandates the use of additions
470 with overflow checking, but existing implementations don't follow it. */
f899b4b8 471#define ADDRESS_ADD_INSN \
b7c7d6c1 472 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
f899b4b8
TS
473
474#define ADDRESS_ADDI_INSN \
b7c7d6c1 475 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
f899b4b8
TS
476
477#define ADDRESS_LOAD_INSN \
478 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
479
480#define ADDRESS_STORE_INSN \
481 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
482
a4672219 483/* Return true if the given CPU supports the MIPS16 ASE. */
3396de36
TS
484#define CPU_HAS_MIPS16(cpu) \
485 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
486 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
a4672219 487
2309ddf2 488/* Return true if the given CPU supports the microMIPS ASE. */
df58fc94
RS
489#define CPU_HAS_MICROMIPS(cpu) 0
490
60b63b72
RS
491/* True if CPU has a dror instruction. */
492#define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
493
494/* True if CPU has a ror instruction. */
495#define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
496
dd6a37e7 497/* True if CPU is in the Octeon family */
432233b3 498#define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP || (CPU) == CPU_OCTEON2)
dd6a37e7 499
dd3cbb7e 500/* True if CPU has seq/sne and seqi/snei instructions. */
dd6a37e7 501#define CPU_HAS_SEQ(CPU) (CPU_IS_OCTEON (CPU))
dd3cbb7e 502
c8978940
CD
503/* True if mflo and mfhi can be immediately followed by instructions
504 which write to the HI and LO registers.
505
506 According to MIPS specifications, MIPS ISAs I, II, and III need
507 (at least) two instructions between the reads of HI/LO and
508 instructions which write them, and later ISAs do not. Contradicting
509 the MIPS specifications, some MIPS IV processor user manuals (e.g.
510 the UM for the NEC Vr5000) document needing the instructions between
511 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
512 MIPS64 and later ISAs to have the interlocks, plus any specific
513 earlier-ISA CPUs for which CPU documentation declares that the
514 instructions are really interlocked. */
515#define hilo_interlocks \
516 (mips_opts.isa == ISA_MIPS32 \
517 || mips_opts.isa == ISA_MIPS32R2 \
518 || mips_opts.isa == ISA_MIPS64 \
519 || mips_opts.isa == ISA_MIPS64R2 \
520 || mips_opts.arch == CPU_R4010 \
521 || mips_opts.arch == CPU_R10000 \
522 || mips_opts.arch == CPU_R12000 \
3aa3176b
TS
523 || mips_opts.arch == CPU_R14000 \
524 || mips_opts.arch == CPU_R16000 \
c8978940 525 || mips_opts.arch == CPU_RM7000 \
c8978940 526 || mips_opts.arch == CPU_VR5500 \
df58fc94 527 || mips_opts.micromips \
c8978940 528 )
252b5132
RH
529
530/* Whether the processor uses hardware interlocks to protect reads
81912461
ILT
531 from the GPRs after they are loaded from memory, and thus does not
532 require nops to be inserted. This applies to instructions marked
533 INSN_LOAD_MEMORY_DELAY. These nops are only required at MIPS ISA
df58fc94
RS
534 level I and microMIPS mode instructions are always interlocked. */
535#define gpr_interlocks \
536 (mips_opts.isa != ISA_MIPS1 \
537 || mips_opts.arch == CPU_R3900 \
538 || mips_opts.micromips \
539 )
252b5132 540
81912461
ILT
541/* Whether the processor uses hardware interlocks to avoid delays
542 required by coprocessor instructions, and thus does not require
543 nops to be inserted. This applies to instructions marked
544 INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
545 between instructions marked INSN_WRITE_COND_CODE and ones marked
546 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
df58fc94
RS
547 levels I, II, and III and microMIPS mode instructions are always
548 interlocked. */
bdaaa2e1 549/* Itbl support may require additional care here. */
81912461
ILT
550#define cop_interlocks \
551 ((mips_opts.isa != ISA_MIPS1 \
552 && mips_opts.isa != ISA_MIPS2 \
553 && mips_opts.isa != ISA_MIPS3) \
554 || mips_opts.arch == CPU_R4300 \
df58fc94 555 || mips_opts.micromips \
81912461
ILT
556 )
557
558/* Whether the processor uses hardware interlocks to protect reads
559 from coprocessor registers after they are loaded from memory, and
560 thus does not require nops to be inserted. This applies to
561 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
df58fc94
RS
562 requires at MIPS ISA level I and microMIPS mode instructions are
563 always interlocked. */
564#define cop_mem_interlocks \
565 (mips_opts.isa != ISA_MIPS1 \
566 || mips_opts.micromips \
567 )
252b5132 568
6b76fefe
CM
569/* Is this a mfhi or mflo instruction? */
570#define MF_HILO_INSN(PINFO) \
b19e8a9b
AN
571 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
572
df58fc94
RS
573/* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
574 has been selected. This implies, in particular, that addresses of text
575 labels have their LSB set. */
576#define HAVE_CODE_COMPRESSION \
577 ((mips_opts.mips16 | mips_opts.micromips) != 0)
578
252b5132
RH
579/* MIPS PIC level. */
580
a161fe53 581enum mips_pic_level mips_pic;
252b5132 582
c9914766 583/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 584 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 585static int mips_big_got = 0;
252b5132
RH
586
587/* 1 if trap instructions should used for overflow rather than break
588 instructions. */
c9914766 589static int mips_trap = 0;
252b5132 590
119d663a 591/* 1 if double width floating point constants should not be constructed
b6ff326e 592 by assembling two single width halves into two single width floating
119d663a
NC
593 point registers which just happen to alias the double width destination
594 register. On some architectures this aliasing can be disabled by a bit
d547a75e 595 in the status register, and the setting of this bit cannot be determined
119d663a
NC
596 automatically at assemble time. */
597static int mips_disable_float_construction;
598
252b5132
RH
599/* Non-zero if any .set noreorder directives were used. */
600
601static int mips_any_noreorder;
602
6b76fefe
CM
603/* Non-zero if nops should be inserted when the register referenced in
604 an mfhi/mflo instruction is read in the next two instructions. */
605static int mips_7000_hilo_fix;
606
02ffd3e4 607/* The size of objects in the small data section. */
156c2f8b 608static unsigned int g_switch_value = 8;
252b5132
RH
609/* Whether the -G option was used. */
610static int g_switch_seen = 0;
611
612#define N_RMASK 0xc4
613#define N_VFP 0xd4
614
615/* If we can determine in advance that GP optimization won't be
616 possible, we can skip the relaxation stuff that tries to produce
617 GP-relative references. This makes delay slot optimization work
618 better.
619
620 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
621 gcc output. It needs to guess right for gcc, otherwise gcc
622 will put what it thinks is a GP-relative instruction in a branch
623 delay slot.
252b5132
RH
624
625 I don't know if a fix is needed for the SVR4_PIC mode. I've only
626 fixed it for the non-PIC mode. KR 95/04/07 */
17a2f251 627static int nopic_need_relax (symbolS *, int);
252b5132
RH
628
629/* handle of the OPCODE hash table */
630static struct hash_control *op_hash = NULL;
631
632/* The opcode hash table we use for the mips16. */
633static struct hash_control *mips16_op_hash = NULL;
634
df58fc94
RS
635/* The opcode hash table we use for the microMIPS ASE. */
636static struct hash_control *micromips_op_hash = NULL;
637
252b5132
RH
638/* This array holds the chars that always start a comment. If the
639 pre-processor is disabled, these aren't very useful */
640const char comment_chars[] = "#";
641
642/* This array holds the chars that only start a comment at the beginning of
643 a line. If the line seems to have the form '# 123 filename'
644 .line and .file directives will appear in the pre-processed output */
645/* Note that input_file.c hand checks for '#' at the beginning of the
646 first line of the input file. This is because the compiler outputs
bdaaa2e1 647 #NO_APP at the beginning of its output. */
252b5132
RH
648/* Also note that C style comments are always supported. */
649const char line_comment_chars[] = "#";
650
bdaaa2e1 651/* This array holds machine specific line separator characters. */
63a0b638 652const char line_separator_chars[] = ";";
252b5132
RH
653
654/* Chars that can be used to separate mant from exp in floating point nums */
655const char EXP_CHARS[] = "eE";
656
657/* Chars that mean this number is a floating point constant */
658/* As in 0f12.456 */
659/* or 0d1.2345e12 */
660const char FLT_CHARS[] = "rRsSfFdDxXpP";
661
662/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
663 changed in read.c . Ideally it shouldn't have to know about it at all,
664 but nothing is ideal around here.
665 */
666
667static char *insn_error;
668
669static int auto_align = 1;
670
671/* When outputting SVR4 PIC code, the assembler needs to know the
672 offset in the stack frame from which to restore the $gp register.
673 This is set by the .cprestore pseudo-op, and saved in this
674 variable. */
675static offsetT mips_cprestore_offset = -1;
676
67c1ffbe 677/* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
6478892d 678 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 679 offset and even an other register than $gp as global pointer. */
6478892d
TS
680static offsetT mips_cpreturn_offset = -1;
681static int mips_cpreturn_register = -1;
682static int mips_gp_register = GP;
def2e0dd 683static int mips_gprel_offset = 0;
6478892d 684
7a621144
DJ
685/* Whether mips_cprestore_offset has been set in the current function
686 (or whether it has already been warned about, if not). */
687static int mips_cprestore_valid = 0;
688
252b5132
RH
689/* This is the register which holds the stack frame, as set by the
690 .frame pseudo-op. This is needed to implement .cprestore. */
691static int mips_frame_reg = SP;
692
7a621144
DJ
693/* Whether mips_frame_reg has been set in the current function
694 (or whether it has already been warned about, if not). */
695static int mips_frame_reg_valid = 0;
696
252b5132
RH
697/* To output NOP instructions correctly, we need to keep information
698 about the previous two instructions. */
699
700/* Whether we are optimizing. The default value of 2 means to remove
701 unneeded NOPs and swap branch instructions when possible. A value
702 of 1 means to not swap branches. A value of 0 means to always
703 insert NOPs. */
704static int mips_optimize = 2;
705
706/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
707 equivalent to seeing no -g option at all. */
708static int mips_debug = 0;
709
7d8e00cf
RS
710/* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
711#define MAX_VR4130_NOPS 4
712
713/* The maximum number of NOPs needed to fill delay slots. */
714#define MAX_DELAY_NOPS 2
715
716/* The maximum number of NOPs needed for any purpose. */
717#define MAX_NOPS 4
71400594
RS
718
719/* A list of previous instructions, with index 0 being the most recent.
720 We need to look back MAX_NOPS instructions when filling delay slots
721 or working around processor errata. We need to look back one
722 instruction further if we're thinking about using history[0] to
723 fill a branch delay slot. */
724static struct mips_cl_insn history[1 + MAX_NOPS];
252b5132 725
1e915849 726/* Nop instructions used by emit_nop. */
df58fc94
RS
727static struct mips_cl_insn nop_insn;
728static struct mips_cl_insn mips16_nop_insn;
729static struct mips_cl_insn micromips_nop16_insn;
730static struct mips_cl_insn micromips_nop32_insn;
1e915849
RS
731
732/* The appropriate nop for the current mode. */
df58fc94
RS
733#define NOP_INSN (mips_opts.mips16 ? &mips16_nop_insn \
734 : (mips_opts.micromips ? &micromips_nop16_insn : &nop_insn))
735
736/* The size of NOP_INSN in bytes. */
737#define NOP_INSN_SIZE (HAVE_CODE_COMPRESSION ? 2 : 4)
252b5132 738
252b5132
RH
739/* If this is set, it points to a frag holding nop instructions which
740 were inserted before the start of a noreorder section. If those
741 nops turn out to be unnecessary, the size of the frag can be
742 decreased. */
743static fragS *prev_nop_frag;
744
745/* The number of nop instructions we created in prev_nop_frag. */
746static int prev_nop_frag_holds;
747
748/* The number of nop instructions that we know we need in
bdaaa2e1 749 prev_nop_frag. */
252b5132
RH
750static int prev_nop_frag_required;
751
752/* The number of instructions we've seen since prev_nop_frag. */
753static int prev_nop_frag_since;
754
755/* For ECOFF and ELF, relocations against symbols are done in two
756 parts, with a HI relocation and a LO relocation. Each relocation
757 has only 16 bits of space to store an addend. This means that in
758 order for the linker to handle carries correctly, it must be able
759 to locate both the HI and the LO relocation. This means that the
760 relocations must appear in order in the relocation table.
761
762 In order to implement this, we keep track of each unmatched HI
763 relocation. We then sort them so that they immediately precede the
bdaaa2e1 764 corresponding LO relocation. */
252b5132 765
e972090a
NC
766struct mips_hi_fixup
767{
252b5132
RH
768 /* Next HI fixup. */
769 struct mips_hi_fixup *next;
770 /* This fixup. */
771 fixS *fixp;
772 /* The section this fixup is in. */
773 segT seg;
774};
775
776/* The list of unmatched HI relocs. */
777
778static struct mips_hi_fixup *mips_hi_fixup_list;
779
64bdfcaf
RS
780/* The frag containing the last explicit relocation operator.
781 Null if explicit relocations have not been used. */
782
783static fragS *prev_reloc_op_frag;
784
252b5132
RH
785/* Map normal MIPS register numbers to mips16 register numbers. */
786
787#define X ILLEGAL_REG
e972090a
NC
788static const int mips32_to_16_reg_map[] =
789{
252b5132
RH
790 X, X, 2, 3, 4, 5, 6, 7,
791 X, X, X, X, X, X, X, X,
792 0, 1, X, X, X, X, X, X,
793 X, X, X, X, X, X, X, X
794};
795#undef X
796
797/* Map mips16 register numbers to normal MIPS register numbers. */
798
e972090a
NC
799static const unsigned int mips16_to_32_reg_map[] =
800{
252b5132
RH
801 16, 17, 2, 3, 4, 5, 6, 7
802};
60b63b72 803
df58fc94
RS
804/* Map normal MIPS register numbers to microMIPS register numbers. */
805
806#define mips32_to_micromips_reg_b_map mips32_to_16_reg_map
807#define mips32_to_micromips_reg_c_map mips32_to_16_reg_map
808#define mips32_to_micromips_reg_d_map mips32_to_16_reg_map
809#define mips32_to_micromips_reg_e_map mips32_to_16_reg_map
810#define mips32_to_micromips_reg_f_map mips32_to_16_reg_map
811#define mips32_to_micromips_reg_g_map mips32_to_16_reg_map
812#define mips32_to_micromips_reg_l_map mips32_to_16_reg_map
813
814#define X ILLEGAL_REG
815/* reg type h: 4, 5, 6. */
816static const int mips32_to_micromips_reg_h_map[] =
817{
818 X, X, X, X, 4, 5, 6, X,
819 X, X, X, X, X, X, X, X,
820 X, X, X, X, X, X, X, X,
821 X, X, X, X, X, X, X, X
822};
823
824/* reg type m: 0, 17, 2, 3, 16, 18, 19, 20. */
825static const int mips32_to_micromips_reg_m_map[] =
826{
827 0, X, 2, 3, X, X, X, X,
828 X, X, X, X, X, X, X, X,
829 4, 1, 5, 6, 7, X, X, X,
830 X, X, X, X, X, X, X, X
831};
832
833/* reg type q: 0, 2-7. 17. */
834static const int mips32_to_micromips_reg_q_map[] =
835{
836 0, X, 2, 3, 4, 5, 6, 7,
837 X, X, X, X, X, X, X, X,
838 X, 1, X, X, X, X, X, X,
839 X, X, X, X, X, X, X, X
840};
841
842#define mips32_to_micromips_reg_n_map mips32_to_micromips_reg_m_map
843#undef X
844
845/* Map microMIPS register numbers to normal MIPS register numbers. */
846
847#define micromips_to_32_reg_b_map mips16_to_32_reg_map
848#define micromips_to_32_reg_c_map mips16_to_32_reg_map
849#define micromips_to_32_reg_d_map mips16_to_32_reg_map
850#define micromips_to_32_reg_e_map mips16_to_32_reg_map
851#define micromips_to_32_reg_f_map mips16_to_32_reg_map
852#define micromips_to_32_reg_g_map mips16_to_32_reg_map
853
854/* The microMIPS registers with type h. */
855static const unsigned int micromips_to_32_reg_h_map[] =
856{
857 5, 5, 6, 4, 4, 4, 4, 4
858};
859
860/* The microMIPS registers with type i. */
861static const unsigned int micromips_to_32_reg_i_map[] =
862{
863 6, 7, 7, 21, 22, 5, 6, 7
864};
865
866#define micromips_to_32_reg_l_map mips16_to_32_reg_map
867
868/* The microMIPS registers with type m. */
869static const unsigned int micromips_to_32_reg_m_map[] =
870{
871 0, 17, 2, 3, 16, 18, 19, 20
872};
873
874#define micromips_to_32_reg_n_map micromips_to_32_reg_m_map
875
876/* The microMIPS registers with type q. */
877static const unsigned int micromips_to_32_reg_q_map[] =
878{
879 0, 17, 2, 3, 4, 5, 6, 7
880};
881
882/* microMIPS imm type B. */
883static const int micromips_imm_b_map[] =
884{
885 1, 4, 8, 12, 16, 20, 24, -1
886};
887
888/* microMIPS imm type C. */
889static const int micromips_imm_c_map[] =
890{
891 128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64, 255, 32768, 65535
892};
893
71400594
RS
894/* Classifies the kind of instructions we're interested in when
895 implementing -mfix-vr4120. */
c67a084a
NC
896enum fix_vr4120_class
897{
71400594
RS
898 FIX_VR4120_MACC,
899 FIX_VR4120_DMACC,
900 FIX_VR4120_MULT,
901 FIX_VR4120_DMULT,
902 FIX_VR4120_DIV,
903 FIX_VR4120_MTHILO,
904 NUM_FIX_VR4120_CLASSES
905};
906
c67a084a
NC
907/* ...likewise -mfix-loongson2f-jump. */
908static bfd_boolean mips_fix_loongson2f_jump;
909
910/* ...likewise -mfix-loongson2f-nop. */
911static bfd_boolean mips_fix_loongson2f_nop;
912
913/* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed. */
914static bfd_boolean mips_fix_loongson2f;
915
71400594
RS
916/* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
917 there must be at least one other instruction between an instruction
918 of type X and an instruction of type Y. */
919static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
920
921/* True if -mfix-vr4120 is in force. */
d766e8ec 922static int mips_fix_vr4120;
4a6a3df4 923
7d8e00cf
RS
924/* ...likewise -mfix-vr4130. */
925static int mips_fix_vr4130;
926
6a32d874
CM
927/* ...likewise -mfix-24k. */
928static int mips_fix_24k;
929
d954098f
DD
930/* ...likewise -mfix-cn63xxp1 */
931static bfd_boolean mips_fix_cn63xxp1;
932
4a6a3df4
AO
933/* We don't relax branches by default, since this causes us to expand
934 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
935 fail to compute the offset before expanding the macro to the most
936 efficient expansion. */
937
938static int mips_relax_branch;
252b5132 939\f
4d7206a2
RS
940/* The expansion of many macros depends on the type of symbol that
941 they refer to. For example, when generating position-dependent code,
942 a macro that refers to a symbol may have two different expansions,
943 one which uses GP-relative addresses and one which uses absolute
944 addresses. When generating SVR4-style PIC, a macro may have
945 different expansions for local and global symbols.
946
947 We handle these situations by generating both sequences and putting
948 them in variant frags. In position-dependent code, the first sequence
949 will be the GP-relative one and the second sequence will be the
950 absolute one. In SVR4 PIC, the first sequence will be for global
951 symbols and the second will be for local symbols.
952
584892a6
RS
953 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
954 SECOND are the lengths of the two sequences in bytes. These fields
955 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
956 the subtype has the following flags:
4d7206a2 957
584892a6
RS
958 RELAX_USE_SECOND
959 Set if it has been decided that we should use the second
960 sequence instead of the first.
961
962 RELAX_SECOND_LONGER
963 Set in the first variant frag if the macro's second implementation
964 is longer than its first. This refers to the macro as a whole,
965 not an individual relaxation.
966
967 RELAX_NOMACRO
968 Set in the first variant frag if the macro appeared in a .set nomacro
969 block and if one alternative requires a warning but the other does not.
970
971 RELAX_DELAY_SLOT
972 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
973 delay slot.
4d7206a2 974
df58fc94
RS
975 RELAX_DELAY_SLOT_16BIT
976 Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
977 16-bit instruction.
978
979 RELAX_DELAY_SLOT_SIZE_FIRST
980 Like RELAX_DELAY_SLOT, but indicates that the first implementation of
981 the macro is of the wrong size for the branch delay slot.
982
983 RELAX_DELAY_SLOT_SIZE_SECOND
984 Like RELAX_DELAY_SLOT, but indicates that the second implementation of
985 the macro is of the wrong size for the branch delay slot.
986
4d7206a2
RS
987 The frag's "opcode" points to the first fixup for relaxable code.
988
989 Relaxable macros are generated using a sequence such as:
990
991 relax_start (SYMBOL);
992 ... generate first expansion ...
993 relax_switch ();
994 ... generate second expansion ...
995 relax_end ();
996
997 The code and fixups for the unwanted alternative are discarded
998 by md_convert_frag. */
584892a6 999#define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
4d7206a2 1000
584892a6
RS
1001#define RELAX_FIRST(X) (((X) >> 8) & 0xff)
1002#define RELAX_SECOND(X) ((X) & 0xff)
1003#define RELAX_USE_SECOND 0x10000
1004#define RELAX_SECOND_LONGER 0x20000
1005#define RELAX_NOMACRO 0x40000
1006#define RELAX_DELAY_SLOT 0x80000
df58fc94
RS
1007#define RELAX_DELAY_SLOT_16BIT 0x100000
1008#define RELAX_DELAY_SLOT_SIZE_FIRST 0x200000
1009#define RELAX_DELAY_SLOT_SIZE_SECOND 0x400000
252b5132 1010
4a6a3df4
AO
1011/* Branch without likely bit. If label is out of range, we turn:
1012
1013 beq reg1, reg2, label
1014 delay slot
1015
1016 into
1017
1018 bne reg1, reg2, 0f
1019 nop
1020 j label
1021 0: delay slot
1022
1023 with the following opcode replacements:
1024
1025 beq <-> bne
1026 blez <-> bgtz
1027 bltz <-> bgez
1028 bc1f <-> bc1t
1029
1030 bltzal <-> bgezal (with jal label instead of j label)
1031
1032 Even though keeping the delay slot instruction in the delay slot of
1033 the branch would be more efficient, it would be very tricky to do
1034 correctly, because we'd have to introduce a variable frag *after*
1035 the delay slot instruction, and expand that instead. Let's do it
1036 the easy way for now, even if the branch-not-taken case now costs
1037 one additional instruction. Out-of-range branches are not supposed
1038 to be common, anyway.
1039
1040 Branch likely. If label is out of range, we turn:
1041
1042 beql reg1, reg2, label
1043 delay slot (annulled if branch not taken)
1044
1045 into
1046
1047 beql reg1, reg2, 1f
1048 nop
1049 beql $0, $0, 2f
1050 nop
1051 1: j[al] label
1052 delay slot (executed only if branch taken)
1053 2:
1054
1055 It would be possible to generate a shorter sequence by losing the
1056 likely bit, generating something like:
b34976b6 1057
4a6a3df4
AO
1058 bne reg1, reg2, 0f
1059 nop
1060 j[al] label
1061 delay slot (executed only if branch taken)
1062 0:
1063
1064 beql -> bne
1065 bnel -> beq
1066 blezl -> bgtz
1067 bgtzl -> blez
1068 bltzl -> bgez
1069 bgezl -> bltz
1070 bc1fl -> bc1t
1071 bc1tl -> bc1f
1072
1073 bltzall -> bgezal (with jal label instead of j label)
1074 bgezall -> bltzal (ditto)
1075
1076
1077 but it's not clear that it would actually improve performance. */
66b3e8da
MR
1078#define RELAX_BRANCH_ENCODE(at, uncond, likely, link, toofar) \
1079 ((relax_substateT) \
1080 (0xc0000000 \
1081 | ((at) & 0x1f) \
1082 | ((toofar) ? 0x20 : 0) \
1083 | ((link) ? 0x40 : 0) \
1084 | ((likely) ? 0x80 : 0) \
1085 | ((uncond) ? 0x100 : 0)))
4a6a3df4 1086#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
66b3e8da
MR
1087#define RELAX_BRANCH_UNCOND(i) (((i) & 0x100) != 0)
1088#define RELAX_BRANCH_LIKELY(i) (((i) & 0x80) != 0)
1089#define RELAX_BRANCH_LINK(i) (((i) & 0x40) != 0)
1090#define RELAX_BRANCH_TOOFAR(i) (((i) & 0x20) != 0)
1091#define RELAX_BRANCH_AT(i) ((i) & 0x1f)
4a6a3df4 1092
252b5132
RH
1093/* For mips16 code, we use an entirely different form of relaxation.
1094 mips16 supports two versions of most instructions which take
1095 immediate values: a small one which takes some small value, and a
1096 larger one which takes a 16 bit value. Since branches also follow
1097 this pattern, relaxing these values is required.
1098
1099 We can assemble both mips16 and normal MIPS code in a single
1100 object. Therefore, we need to support this type of relaxation at
1101 the same time that we support the relaxation described above. We
1102 use the high bit of the subtype field to distinguish these cases.
1103
1104 The information we store for this type of relaxation is the
1105 argument code found in the opcode file for this relocation, whether
1106 the user explicitly requested a small or extended form, and whether
1107 the relocation is in a jump or jal delay slot. That tells us the
1108 size of the value, and how it should be stored. We also store
1109 whether the fragment is considered to be extended or not. We also
1110 store whether this is known to be a branch to a different section,
1111 whether we have tried to relax this frag yet, and whether we have
1112 ever extended a PC relative fragment because of a shift count. */
1113#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
1114 (0x80000000 \
1115 | ((type) & 0xff) \
1116 | ((small) ? 0x100 : 0) \
1117 | ((ext) ? 0x200 : 0) \
1118 | ((dslot) ? 0x400 : 0) \
1119 | ((jal_dslot) ? 0x800 : 0))
4a6a3df4 1120#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132
RH
1121#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
1122#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
1123#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
1124#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
1125#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
1126#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
1127#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
1128#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
1129#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
1130#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
1131#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
885add95 1132
df58fc94
RS
1133/* For microMIPS code, we use relaxation similar to one we use for
1134 MIPS16 code. Some instructions that take immediate values support
1135 two encodings: a small one which takes some small value, and a
1136 larger one which takes a 16 bit value. As some branches also follow
1137 this pattern, relaxing these values is required.
1138
1139 We can assemble both microMIPS and normal MIPS code in a single
1140 object. Therefore, we need to support this type of relaxation at
1141 the same time that we support the relaxation described above. We
1142 use one of the high bits of the subtype field to distinguish these
1143 cases.
1144
1145 The information we store for this type of relaxation is the argument
1146 code found in the opcode file for this relocation, the register
40209cad
MR
1147 selected as the assembler temporary, whether the branch is
1148 unconditional, whether it is compact, whether it stores the link
1149 address implicitly in $ra, whether relaxation of out-of-range 32-bit
1150 branches to a sequence of instructions is enabled, and whether the
1151 displacement of a branch is too large to fit as an immediate argument
1152 of a 16-bit and a 32-bit branch, respectively. */
1153#define RELAX_MICROMIPS_ENCODE(type, at, uncond, compact, link, \
1154 relax32, toofar16, toofar32) \
1155 (0x40000000 \
1156 | ((type) & 0xff) \
1157 | (((at) & 0x1f) << 8) \
1158 | ((uncond) ? 0x2000 : 0) \
1159 | ((compact) ? 0x4000 : 0) \
1160 | ((link) ? 0x8000 : 0) \
1161 | ((relax32) ? 0x10000 : 0) \
1162 | ((toofar16) ? 0x20000 : 0) \
1163 | ((toofar32) ? 0x40000 : 0))
df58fc94
RS
1164#define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1165#define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1166#define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
40209cad
MR
1167#define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x2000) != 0)
1168#define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x4000) != 0)
1169#define RELAX_MICROMIPS_LINK(i) (((i) & 0x8000) != 0)
1170#define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x10000) != 0)
1171
1172#define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x20000) != 0)
1173#define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x20000)
1174#define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x20000)
1175#define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x40000) != 0)
1176#define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x40000)
1177#define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x40000)
df58fc94 1178
43c0598f
RS
1179/* Sign-extend 16-bit value X. */
1180#define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1181
885add95
CD
1182/* Is the given value a sign-extended 32-bit value? */
1183#define IS_SEXT_32BIT_NUM(x) \
1184 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1185 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1186
1187/* Is the given value a sign-extended 16-bit value? */
1188#define IS_SEXT_16BIT_NUM(x) \
1189 (((x) &~ (offsetT) 0x7fff) == 0 \
1190 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1191
df58fc94
RS
1192/* Is the given value a sign-extended 12-bit value? */
1193#define IS_SEXT_12BIT_NUM(x) \
1194 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1195
2051e8c4
MR
1196/* Is the given value a zero-extended 32-bit value? Or a negated one? */
1197#define IS_ZEXT_32BIT_NUM(x) \
1198 (((x) &~ (offsetT) 0xffffffff) == 0 \
1199 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1200
bf12938e
RS
1201/* Replace bits MASK << SHIFT of STRUCT with the equivalent bits in
1202 VALUE << SHIFT. VALUE is evaluated exactly once. */
1203#define INSERT_BITS(STRUCT, VALUE, MASK, SHIFT) \
1204 (STRUCT) = (((STRUCT) & ~((MASK) << (SHIFT))) \
1205 | (((VALUE) & (MASK)) << (SHIFT)))
1206
1207/* Extract bits MASK << SHIFT from STRUCT and shift them right
1208 SHIFT places. */
1209#define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1210 (((STRUCT) >> (SHIFT)) & (MASK))
1211
1212/* Change INSN's opcode so that the operand given by FIELD has value VALUE.
1213 INSN is a mips_cl_insn structure and VALUE is evaluated exactly once.
1214
1215 include/opcode/mips.h specifies operand fields using the macros
1216 OP_MASK_<FIELD> and OP_SH_<FIELD>. The MIPS16 equivalents start
1217 with "MIPS16OP" instead of "OP". */
df58fc94
RS
1218#define INSERT_OPERAND(MICROMIPS, FIELD, INSN, VALUE) \
1219 do \
1220 if (!(MICROMIPS)) \
1221 INSERT_BITS ((INSN).insn_opcode, VALUE, \
1222 OP_MASK_##FIELD, OP_SH_##FIELD); \
1223 else \
1224 INSERT_BITS ((INSN).insn_opcode, VALUE, \
1225 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD); \
1226 while (0)
bf12938e
RS
1227#define MIPS16_INSERT_OPERAND(FIELD, INSN, VALUE) \
1228 INSERT_BITS ((INSN).insn_opcode, VALUE, \
1229 MIPS16OP_MASK_##FIELD, MIPS16OP_SH_##FIELD)
1230
1231/* Extract the operand given by FIELD from mips_cl_insn INSN. */
df58fc94
RS
1232#define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1233 (!(MICROMIPS) \
1234 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1235 : EXTRACT_BITS ((INSN).insn_opcode, \
1236 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
bf12938e
RS
1237#define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1238 EXTRACT_BITS ((INSN).insn_opcode, \
1239 MIPS16OP_MASK_##FIELD, \
1240 MIPS16OP_SH_##FIELD)
5c04167a
RS
1241
1242/* The MIPS16 EXTEND opcode, shifted left 16 places. */
1243#define MIPS16_EXTEND (0xf000U << 16)
4d7206a2 1244\f
df58fc94
RS
1245/* Whether or not we are emitting a branch-likely macro. */
1246static bfd_boolean emit_branch_likely_macro = FALSE;
1247
4d7206a2
RS
1248/* Global variables used when generating relaxable macros. See the
1249 comment above RELAX_ENCODE for more details about how relaxation
1250 is used. */
1251static struct {
1252 /* 0 if we're not emitting a relaxable macro.
1253 1 if we're emitting the first of the two relaxation alternatives.
1254 2 if we're emitting the second alternative. */
1255 int sequence;
1256
1257 /* The first relaxable fixup in the current frag. (In other words,
1258 the first fixup that refers to relaxable code.) */
1259 fixS *first_fixup;
1260
1261 /* sizes[0] says how many bytes of the first alternative are stored in
1262 the current frag. Likewise sizes[1] for the second alternative. */
1263 unsigned int sizes[2];
1264
1265 /* The symbol on which the choice of sequence depends. */
1266 symbolS *symbol;
1267} mips_relax;
252b5132 1268\f
584892a6
RS
1269/* Global variables used to decide whether a macro needs a warning. */
1270static struct {
1271 /* True if the macro is in a branch delay slot. */
1272 bfd_boolean delay_slot_p;
1273
df58fc94
RS
1274 /* Set to the length in bytes required if the macro is in a delay slot
1275 that requires a specific length of instruction, otherwise zero. */
1276 unsigned int delay_slot_length;
1277
584892a6
RS
1278 /* For relaxable macros, sizes[0] is the length of the first alternative
1279 in bytes and sizes[1] is the length of the second alternative.
1280 For non-relaxable macros, both elements give the length of the
1281 macro in bytes. */
1282 unsigned int sizes[2];
1283
df58fc94
RS
1284 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1285 instruction of the first alternative in bytes and first_insn_sizes[1]
1286 is the length of the first instruction of the second alternative.
1287 For non-relaxable macros, both elements give the length of the first
1288 instruction in bytes.
1289
1290 Set to zero if we haven't yet seen the first instruction. */
1291 unsigned int first_insn_sizes[2];
1292
1293 /* For relaxable macros, insns[0] is the number of instructions for the
1294 first alternative and insns[1] is the number of instructions for the
1295 second alternative.
1296
1297 For non-relaxable macros, both elements give the number of
1298 instructions for the macro. */
1299 unsigned int insns[2];
1300
584892a6
RS
1301 /* The first variant frag for this macro. */
1302 fragS *first_frag;
1303} mips_macro_warning;
1304\f
252b5132
RH
1305/* Prototypes for static functions. */
1306
17a2f251 1307#define internalError() \
252b5132 1308 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
252b5132
RH
1309
1310enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1311
b34976b6 1312static void append_insn
df58fc94
RS
1313 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1314 bfd_boolean expansionp);
7d10b47d 1315static void mips_no_prev_insn (void);
c67a084a 1316static void macro_build (expressionS *, const char *, const char *, ...);
b34976b6 1317static void mips16_macro_build
03ea81db 1318 (expressionS *, const char *, const char *, va_list *);
67c0d1eb 1319static void load_register (int, expressionS *, int);
584892a6
RS
1320static void macro_start (void);
1321static void macro_end (void);
17a2f251
TS
1322static void macro (struct mips_cl_insn * ip);
1323static void mips16_macro (struct mips_cl_insn * ip);
17a2f251
TS
1324static void mips_ip (char *str, struct mips_cl_insn * ip);
1325static void mips16_ip (char *str, struct mips_cl_insn * ip);
b34976b6 1326static void mips16_immed
43c0598f
RS
1327 (char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
1328 unsigned int, unsigned long *);
5e0116d5 1329static size_t my_getSmallExpression
17a2f251
TS
1330 (expressionS *, bfd_reloc_code_real_type *, char *);
1331static void my_getExpression (expressionS *, char *);
1332static void s_align (int);
1333static void s_change_sec (int);
1334static void s_change_section (int);
1335static void s_cons (int);
1336static void s_float_cons (int);
1337static void s_mips_globl (int);
1338static void s_option (int);
1339static void s_mipsset (int);
1340static void s_abicalls (int);
1341static void s_cpload (int);
1342static void s_cpsetup (int);
1343static void s_cplocal (int);
1344static void s_cprestore (int);
1345static void s_cpreturn (int);
741d6ea8
JM
1346static void s_dtprelword (int);
1347static void s_dtpreldword (int);
d0f13682
CLT
1348static void s_tprelword (int);
1349static void s_tpreldword (int);
17a2f251
TS
1350static void s_gpvalue (int);
1351static void s_gpword (int);
1352static void s_gpdword (int);
1353static void s_cpadd (int);
1354static void s_insn (int);
1355static void md_obj_begin (void);
1356static void md_obj_end (void);
1357static void s_mips_ent (int);
1358static void s_mips_end (int);
1359static void s_mips_frame (int);
1360static void s_mips_mask (int reg_type);
1361static void s_mips_stab (int);
1362static void s_mips_weakext (int);
1363static void s_mips_file (int);
1364static void s_mips_loc (int);
1365static bfd_boolean pic_need_relax (symbolS *, asection *);
4a6a3df4 1366static int relaxed_branch_length (fragS *, asection *, int);
17a2f251 1367static int validate_mips_insn (const struct mips_opcode *);
df58fc94
RS
1368static int validate_micromips_insn (const struct mips_opcode *);
1369static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1370static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
e7af610e
NC
1371
1372/* Table and functions used to map between CPU/ISA names, and
1373 ISA levels, and CPU numbers. */
1374
e972090a
NC
1375struct mips_cpu_info
1376{
e7af610e 1377 const char *name; /* CPU or ISA name. */
ad3fea08 1378 int flags; /* ASEs available, or ISA flag. */
e7af610e
NC
1379 int isa; /* ISA level. */
1380 int cpu; /* CPU number (default CPU if ISA). */
1381};
1382
ad3fea08
TS
1383#define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
1384#define MIPS_CPU_ASE_SMARTMIPS 0x0002 /* CPU implements SmartMIPS ASE */
1385#define MIPS_CPU_ASE_DSP 0x0004 /* CPU implements DSP ASE */
1386#define MIPS_CPU_ASE_MT 0x0008 /* CPU implements MT ASE */
1387#define MIPS_CPU_ASE_MIPS3D 0x0010 /* CPU implements MIPS-3D ASE */
1388#define MIPS_CPU_ASE_MDMX 0x0020 /* CPU implements MDMX ASE */
8b082fb1 1389#define MIPS_CPU_ASE_DSPR2 0x0040 /* CPU implements DSP R2 ASE */
dec0624d 1390#define MIPS_CPU_ASE_MCU 0x0080 /* CPU implements MCU ASE */
ad3fea08 1391
17a2f251
TS
1392static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1393static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1394static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132
RH
1395\f
1396/* Pseudo-op table.
1397
1398 The following pseudo-ops from the Kane and Heinrich MIPS book
1399 should be defined here, but are currently unsupported: .alias,
1400 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1401
1402 The following pseudo-ops from the Kane and Heinrich MIPS book are
1403 specific to the type of debugging information being generated, and
1404 should be defined by the object format: .aent, .begin, .bend,
1405 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1406 .vreg.
1407
1408 The following pseudo-ops from the Kane and Heinrich MIPS book are
1409 not MIPS CPU specific, but are also not specific to the object file
1410 format. This file is probably the best place to define them, but
d84bcf09 1411 they are not currently supported: .asm0, .endr, .lab, .struct. */
252b5132 1412
e972090a
NC
1413static const pseudo_typeS mips_pseudo_table[] =
1414{
beae10d5 1415 /* MIPS specific pseudo-ops. */
252b5132
RH
1416 {"option", s_option, 0},
1417 {"set", s_mipsset, 0},
1418 {"rdata", s_change_sec, 'r'},
1419 {"sdata", s_change_sec, 's'},
1420 {"livereg", s_ignore, 0},
1421 {"abicalls", s_abicalls, 0},
1422 {"cpload", s_cpload, 0},
6478892d
TS
1423 {"cpsetup", s_cpsetup, 0},
1424 {"cplocal", s_cplocal, 0},
252b5132 1425 {"cprestore", s_cprestore, 0},
6478892d 1426 {"cpreturn", s_cpreturn, 0},
741d6ea8
JM
1427 {"dtprelword", s_dtprelword, 0},
1428 {"dtpreldword", s_dtpreldword, 0},
d0f13682
CLT
1429 {"tprelword", s_tprelword, 0},
1430 {"tpreldword", s_tpreldword, 0},
6478892d 1431 {"gpvalue", s_gpvalue, 0},
252b5132 1432 {"gpword", s_gpword, 0},
10181a0d 1433 {"gpdword", s_gpdword, 0},
252b5132
RH
1434 {"cpadd", s_cpadd, 0},
1435 {"insn", s_insn, 0},
1436
beae10d5 1437 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132 1438 chips. */
38a57ae7 1439 {"asciiz", stringer, 8 + 1},
252b5132
RH
1440 {"bss", s_change_sec, 'b'},
1441 {"err", s_err, 0},
1442 {"half", s_cons, 1},
1443 {"dword", s_cons, 3},
1444 {"weakext", s_mips_weakext, 0},
7c752c2a
TS
1445 {"origin", s_org, 0},
1446 {"repeat", s_rept, 0},
252b5132 1447
998b3c36
MR
1448 /* For MIPS this is non-standard, but we define it for consistency. */
1449 {"sbss", s_change_sec, 'B'},
1450
beae10d5 1451 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1452 here for one reason or another. */
1453 {"align", s_align, 0},
1454 {"byte", s_cons, 0},
1455 {"data", s_change_sec, 'd'},
1456 {"double", s_float_cons, 'd'},
1457 {"float", s_float_cons, 'f'},
1458 {"globl", s_mips_globl, 0},
1459 {"global", s_mips_globl, 0},
1460 {"hword", s_cons, 1},
1461 {"int", s_cons, 2},
1462 {"long", s_cons, 2},
1463 {"octa", s_cons, 4},
1464 {"quad", s_cons, 3},
cca86cc8 1465 {"section", s_change_section, 0},
252b5132
RH
1466 {"short", s_cons, 1},
1467 {"single", s_float_cons, 'f'},
1468 {"stabn", s_mips_stab, 'n'},
1469 {"text", s_change_sec, 't'},
1470 {"word", s_cons, 2},
add56521 1471
add56521 1472 { "extern", ecoff_directive_extern, 0},
add56521 1473
43841e91 1474 { NULL, NULL, 0 },
252b5132
RH
1475};
1476
e972090a
NC
1477static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1478{
beae10d5
KH
1479 /* These pseudo-ops should be defined by the object file format.
1480 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1481 {"aent", s_mips_ent, 1},
1482 {"bgnb", s_ignore, 0},
1483 {"end", s_mips_end, 0},
1484 {"endb", s_ignore, 0},
1485 {"ent", s_mips_ent, 0},
c5dd6aab 1486 {"file", s_mips_file, 0},
252b5132
RH
1487 {"fmask", s_mips_mask, 'F'},
1488 {"frame", s_mips_frame, 0},
c5dd6aab 1489 {"loc", s_mips_loc, 0},
252b5132
RH
1490 {"mask", s_mips_mask, 'R'},
1491 {"verstamp", s_ignore, 0},
43841e91 1492 { NULL, NULL, 0 },
252b5132
RH
1493};
1494
3ae8dd8d
MR
1495/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1496 purpose of the `.dc.a' internal pseudo-op. */
1497
1498int
1499mips_address_bytes (void)
1500{
1501 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1502}
1503
17a2f251 1504extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1505
1506void
17a2f251 1507mips_pop_insert (void)
252b5132
RH
1508{
1509 pop_insert (mips_pseudo_table);
1510 if (! ECOFF_DEBUGGING)
1511 pop_insert (mips_nonecoff_pseudo_table);
1512}
1513\f
1514/* Symbols labelling the current insn. */
1515
e972090a
NC
1516struct insn_label_list
1517{
252b5132
RH
1518 struct insn_label_list *next;
1519 symbolS *label;
1520};
1521
252b5132 1522static struct insn_label_list *free_insn_labels;
742a56fe 1523#define label_list tc_segment_info_data.labels
252b5132 1524
17a2f251 1525static void mips_clear_insn_labels (void);
df58fc94
RS
1526static void mips_mark_labels (void);
1527static void mips_compressed_mark_labels (void);
252b5132
RH
1528
1529static inline void
17a2f251 1530mips_clear_insn_labels (void)
252b5132
RH
1531{
1532 register struct insn_label_list **pl;
a8dbcb85 1533 segment_info_type *si;
252b5132 1534
a8dbcb85
TS
1535 if (now_seg)
1536 {
1537 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1538 ;
1539
1540 si = seg_info (now_seg);
1541 *pl = si->label_list;
1542 si->label_list = NULL;
1543 }
252b5132 1544}
a8dbcb85 1545
df58fc94
RS
1546/* Mark instruction labels in MIPS16/microMIPS mode. */
1547
1548static inline void
1549mips_mark_labels (void)
1550{
1551 if (HAVE_CODE_COMPRESSION)
1552 mips_compressed_mark_labels ();
1553}
252b5132
RH
1554\f
1555static char *expr_end;
1556
1557/* Expressions which appear in instructions. These are set by
1558 mips_ip. */
1559
1560static expressionS imm_expr;
5f74bc13 1561static expressionS imm2_expr;
252b5132
RH
1562static expressionS offset_expr;
1563
1564/* Relocs associated with imm_expr and offset_expr. */
1565
f6688943
TS
1566static bfd_reloc_code_real_type imm_reloc[3]
1567 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1568static bfd_reloc_code_real_type offset_reloc[3]
1569 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 1570
df58fc94
RS
1571/* This is set to the resulting size of the instruction to be produced
1572 by mips16_ip if an explicit extension is used or by mips_ip if an
1573 explicit size is supplied. */
252b5132 1574
df58fc94 1575static unsigned int forced_insn_length;
252b5132 1576
e1b47bd5
RS
1577/* True if we are assembling an instruction. All dot symbols defined during
1578 this time should be treated as code labels. */
1579
1580static bfd_boolean mips_assembling_insn;
1581
7ed4a06a 1582#ifdef OBJ_ELF
ecb4347a
DJ
1583/* The pdr segment for per procedure frame/regmask info. Not used for
1584 ECOFF debugging. */
252b5132
RH
1585
1586static segT pdr_seg;
7ed4a06a 1587#endif
252b5132 1588
e013f690
TS
1589/* The default target format to use. */
1590
aeffff67
RS
1591#if defined (TE_FreeBSD)
1592#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
1593#elif defined (TE_TMIPS)
1594#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
1595#else
1596#define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
1597#endif
1598
e013f690 1599const char *
17a2f251 1600mips_target_format (void)
e013f690
TS
1601{
1602 switch (OUTPUT_FLAVOR)
1603 {
e013f690
TS
1604 case bfd_target_ecoff_flavour:
1605 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1606 case bfd_target_coff_flavour:
1607 return "pe-mips";
1608 case bfd_target_elf_flavour:
0a44bf69
RS
1609#ifdef TE_VXWORKS
1610 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1611 return (target_big_endian
1612 ? "elf32-bigmips-vxworks"
1613 : "elf32-littlemips-vxworks");
1614#endif
e013f690 1615 return (target_big_endian
cfe86eaa 1616 ? (HAVE_64BIT_OBJECTS
aeffff67 1617 ? ELF_TARGET ("elf64-", "big")
cfe86eaa 1618 : (HAVE_NEWABI
aeffff67
RS
1619 ? ELF_TARGET ("elf32-n", "big")
1620 : ELF_TARGET ("elf32-", "big")))
cfe86eaa 1621 : (HAVE_64BIT_OBJECTS
aeffff67 1622 ? ELF_TARGET ("elf64-", "little")
cfe86eaa 1623 : (HAVE_NEWABI
aeffff67
RS
1624 ? ELF_TARGET ("elf32-n", "little")
1625 : ELF_TARGET ("elf32-", "little"))));
e013f690
TS
1626 default:
1627 abort ();
1628 return NULL;
1629 }
1630}
1631
df58fc94
RS
1632/* Return the length of a microMIPS instruction in bytes. If bits of
1633 the mask beyond the low 16 are 0, then it is a 16-bit instruction.
1634 Otherwise assume a 32-bit instruction; 48-bit instructions (0x1f
1635 major opcode) will require further modifications to the opcode
1636 table. */
1637
1638static inline unsigned int
1639micromips_insn_length (const struct mips_opcode *mo)
1640{
1641 return (mo->mask >> 16) == 0 ? 2 : 4;
1642}
1643
5c04167a
RS
1644/* Return the length of MIPS16 instruction OPCODE. */
1645
1646static inline unsigned int
1647mips16_opcode_length (unsigned long opcode)
1648{
1649 return (opcode >> 16) == 0 ? 2 : 4;
1650}
1651
1e915849
RS
1652/* Return the length of instruction INSN. */
1653
1654static inline unsigned int
1655insn_length (const struct mips_cl_insn *insn)
1656{
df58fc94
RS
1657 if (mips_opts.micromips)
1658 return micromips_insn_length (insn->insn_mo);
1659 else if (mips_opts.mips16)
5c04167a 1660 return mips16_opcode_length (insn->insn_opcode);
df58fc94 1661 else
1e915849 1662 return 4;
1e915849
RS
1663}
1664
1665/* Initialise INSN from opcode entry MO. Leave its position unspecified. */
1666
1667static void
1668create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
1669{
1670 size_t i;
1671
1672 insn->insn_mo = mo;
1e915849
RS
1673 insn->insn_opcode = mo->match;
1674 insn->frag = NULL;
1675 insn->where = 0;
1676 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1677 insn->fixp[i] = NULL;
1678 insn->fixed_p = (mips_opts.noreorder > 0);
1679 insn->noreorder_p = (mips_opts.noreorder > 0);
1680 insn->mips16_absolute_jump_p = 0;
15be625d 1681 insn->complete_p = 0;
1e915849
RS
1682}
1683
df58fc94 1684/* Record the current MIPS16/microMIPS mode in now_seg. */
742a56fe
RS
1685
1686static void
df58fc94 1687mips_record_compressed_mode (void)
742a56fe
RS
1688{
1689 segment_info_type *si;
1690
1691 si = seg_info (now_seg);
1692 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
1693 si->tc_segment_info_data.mips16 = mips_opts.mips16;
df58fc94
RS
1694 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
1695 si->tc_segment_info_data.micromips = mips_opts.micromips;
742a56fe
RS
1696}
1697
4d68580a
RS
1698/* Read a standard MIPS instruction from BUF. */
1699
1700static unsigned long
1701read_insn (char *buf)
1702{
1703 if (target_big_endian)
1704 return bfd_getb32 ((bfd_byte *) buf);
1705 else
1706 return bfd_getl32 ((bfd_byte *) buf);
1707}
1708
1709/* Write standard MIPS instruction INSN to BUF. Return a pointer to
1710 the next byte. */
1711
1712static char *
1713write_insn (char *buf, unsigned int insn)
1714{
1715 md_number_to_chars (buf, insn, 4);
1716 return buf + 4;
1717}
1718
1719/* Read a microMIPS or MIPS16 opcode from BUF, given that it
1720 has length LENGTH. */
1721
1722static unsigned long
1723read_compressed_insn (char *buf, unsigned int length)
1724{
1725 unsigned long insn;
1726 unsigned int i;
1727
1728 insn = 0;
1729 for (i = 0; i < length; i += 2)
1730 {
1731 insn <<= 16;
1732 if (target_big_endian)
1733 insn |= bfd_getb16 ((char *) buf);
1734 else
1735 insn |= bfd_getl16 ((char *) buf);
1736 buf += 2;
1737 }
1738 return insn;
1739}
1740
5c04167a
RS
1741/* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
1742 instruction is LENGTH bytes long. Return a pointer to the next byte. */
1743
1744static char *
1745write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
1746{
1747 unsigned int i;
1748
1749 for (i = 0; i < length; i += 2)
1750 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
1751 return buf + length;
1752}
1753
1e915849
RS
1754/* Install INSN at the location specified by its "frag" and "where" fields. */
1755
1756static void
1757install_insn (const struct mips_cl_insn *insn)
1758{
1759 char *f = insn->frag->fr_literal + insn->where;
5c04167a
RS
1760 if (HAVE_CODE_COMPRESSION)
1761 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
1e915849 1762 else
4d68580a 1763 write_insn (f, insn->insn_opcode);
df58fc94 1764 mips_record_compressed_mode ();
1e915849
RS
1765}
1766
1767/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
1768 and install the opcode in the new location. */
1769
1770static void
1771move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
1772{
1773 size_t i;
1774
1775 insn->frag = frag;
1776 insn->where = where;
1777 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1778 if (insn->fixp[i] != NULL)
1779 {
1780 insn->fixp[i]->fx_frag = frag;
1781 insn->fixp[i]->fx_where = where;
1782 }
1783 install_insn (insn);
1784}
1785
1786/* Add INSN to the end of the output. */
1787
1788static void
1789add_fixed_insn (struct mips_cl_insn *insn)
1790{
1791 char *f = frag_more (insn_length (insn));
1792 move_insn (insn, frag_now, f - frag_now->fr_literal);
1793}
1794
1795/* Start a variant frag and move INSN to the start of the variant part,
1796 marking it as fixed. The other arguments are as for frag_var. */
1797
1798static void
1799add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
1800 relax_substateT subtype, symbolS *symbol, offsetT offset)
1801{
1802 frag_grow (max_chars);
1803 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
1804 insn->fixed_p = 1;
1805 frag_var (rs_machine_dependent, max_chars, var,
1806 subtype, symbol, offset, NULL);
1807}
1808
1809/* Insert N copies of INSN into the history buffer, starting at
1810 position FIRST. Neither FIRST nor N need to be clipped. */
1811
1812static void
1813insert_into_history (unsigned int first, unsigned int n,
1814 const struct mips_cl_insn *insn)
1815{
1816 if (mips_relax.sequence != 2)
1817 {
1818 unsigned int i;
1819
1820 for (i = ARRAY_SIZE (history); i-- > first;)
1821 if (i >= first + n)
1822 history[i] = history[i - n];
1823 else
1824 history[i] = *insn;
1825 }
1826}
1827
71400594
RS
1828/* Initialize vr4120_conflicts. There is a bit of duplication here:
1829 the idea is to make it obvious at a glance that each errata is
1830 included. */
1831
1832static void
1833init_vr4120_conflicts (void)
1834{
1835#define CONFLICT(FIRST, SECOND) \
1836 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
1837
1838 /* Errata 21 - [D]DIV[U] after [D]MACC */
1839 CONFLICT (MACC, DIV);
1840 CONFLICT (DMACC, DIV);
1841
1842 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
1843 CONFLICT (DMULT, DMULT);
1844 CONFLICT (DMULT, DMACC);
1845 CONFLICT (DMACC, DMULT);
1846 CONFLICT (DMACC, DMACC);
1847
1848 /* Errata 24 - MT{LO,HI} after [D]MACC */
1849 CONFLICT (MACC, MTHILO);
1850 CONFLICT (DMACC, MTHILO);
1851
1852 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
1853 instruction is executed immediately after a MACC or DMACC
1854 instruction, the result of [either instruction] is incorrect." */
1855 CONFLICT (MACC, MULT);
1856 CONFLICT (MACC, DMULT);
1857 CONFLICT (DMACC, MULT);
1858 CONFLICT (DMACC, DMULT);
1859
1860 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
1861 executed immediately after a DMULT, DMULTU, DIV, DIVU,
1862 DDIV or DDIVU instruction, the result of the MACC or
1863 DMACC instruction is incorrect.". */
1864 CONFLICT (DMULT, MACC);
1865 CONFLICT (DMULT, DMACC);
1866 CONFLICT (DIV, MACC);
1867 CONFLICT (DIV, DMACC);
1868
1869#undef CONFLICT
1870}
1871
707bfff6
TS
1872struct regname {
1873 const char *name;
1874 unsigned int num;
1875};
1876
1877#define RTYPE_MASK 0x1ff00
1878#define RTYPE_NUM 0x00100
1879#define RTYPE_FPU 0x00200
1880#define RTYPE_FCC 0x00400
1881#define RTYPE_VEC 0x00800
1882#define RTYPE_GP 0x01000
1883#define RTYPE_CP0 0x02000
1884#define RTYPE_PC 0x04000
1885#define RTYPE_ACC 0x08000
1886#define RTYPE_CCC 0x10000
1887#define RNUM_MASK 0x000ff
1888#define RWARN 0x80000
1889
1890#define GENERIC_REGISTER_NUMBERS \
1891 {"$0", RTYPE_NUM | 0}, \
1892 {"$1", RTYPE_NUM | 1}, \
1893 {"$2", RTYPE_NUM | 2}, \
1894 {"$3", RTYPE_NUM | 3}, \
1895 {"$4", RTYPE_NUM | 4}, \
1896 {"$5", RTYPE_NUM | 5}, \
1897 {"$6", RTYPE_NUM | 6}, \
1898 {"$7", RTYPE_NUM | 7}, \
1899 {"$8", RTYPE_NUM | 8}, \
1900 {"$9", RTYPE_NUM | 9}, \
1901 {"$10", RTYPE_NUM | 10}, \
1902 {"$11", RTYPE_NUM | 11}, \
1903 {"$12", RTYPE_NUM | 12}, \
1904 {"$13", RTYPE_NUM | 13}, \
1905 {"$14", RTYPE_NUM | 14}, \
1906 {"$15", RTYPE_NUM | 15}, \
1907 {"$16", RTYPE_NUM | 16}, \
1908 {"$17", RTYPE_NUM | 17}, \
1909 {"$18", RTYPE_NUM | 18}, \
1910 {"$19", RTYPE_NUM | 19}, \
1911 {"$20", RTYPE_NUM | 20}, \
1912 {"$21", RTYPE_NUM | 21}, \
1913 {"$22", RTYPE_NUM | 22}, \
1914 {"$23", RTYPE_NUM | 23}, \
1915 {"$24", RTYPE_NUM | 24}, \
1916 {"$25", RTYPE_NUM | 25}, \
1917 {"$26", RTYPE_NUM | 26}, \
1918 {"$27", RTYPE_NUM | 27}, \
1919 {"$28", RTYPE_NUM | 28}, \
1920 {"$29", RTYPE_NUM | 29}, \
1921 {"$30", RTYPE_NUM | 30}, \
1922 {"$31", RTYPE_NUM | 31}
1923
1924#define FPU_REGISTER_NAMES \
1925 {"$f0", RTYPE_FPU | 0}, \
1926 {"$f1", RTYPE_FPU | 1}, \
1927 {"$f2", RTYPE_FPU | 2}, \
1928 {"$f3", RTYPE_FPU | 3}, \
1929 {"$f4", RTYPE_FPU | 4}, \
1930 {"$f5", RTYPE_FPU | 5}, \
1931 {"$f6", RTYPE_FPU | 6}, \
1932 {"$f7", RTYPE_FPU | 7}, \
1933 {"$f8", RTYPE_FPU | 8}, \
1934 {"$f9", RTYPE_FPU | 9}, \
1935 {"$f10", RTYPE_FPU | 10}, \
1936 {"$f11", RTYPE_FPU | 11}, \
1937 {"$f12", RTYPE_FPU | 12}, \
1938 {"$f13", RTYPE_FPU | 13}, \
1939 {"$f14", RTYPE_FPU | 14}, \
1940 {"$f15", RTYPE_FPU | 15}, \
1941 {"$f16", RTYPE_FPU | 16}, \
1942 {"$f17", RTYPE_FPU | 17}, \
1943 {"$f18", RTYPE_FPU | 18}, \
1944 {"$f19", RTYPE_FPU | 19}, \
1945 {"$f20", RTYPE_FPU | 20}, \
1946 {"$f21", RTYPE_FPU | 21}, \
1947 {"$f22", RTYPE_FPU | 22}, \
1948 {"$f23", RTYPE_FPU | 23}, \
1949 {"$f24", RTYPE_FPU | 24}, \
1950 {"$f25", RTYPE_FPU | 25}, \
1951 {"$f26", RTYPE_FPU | 26}, \
1952 {"$f27", RTYPE_FPU | 27}, \
1953 {"$f28", RTYPE_FPU | 28}, \
1954 {"$f29", RTYPE_FPU | 29}, \
1955 {"$f30", RTYPE_FPU | 30}, \
1956 {"$f31", RTYPE_FPU | 31}
1957
1958#define FPU_CONDITION_CODE_NAMES \
1959 {"$fcc0", RTYPE_FCC | 0}, \
1960 {"$fcc1", RTYPE_FCC | 1}, \
1961 {"$fcc2", RTYPE_FCC | 2}, \
1962 {"$fcc3", RTYPE_FCC | 3}, \
1963 {"$fcc4", RTYPE_FCC | 4}, \
1964 {"$fcc5", RTYPE_FCC | 5}, \
1965 {"$fcc6", RTYPE_FCC | 6}, \
1966 {"$fcc7", RTYPE_FCC | 7}
1967
1968#define COPROC_CONDITION_CODE_NAMES \
1969 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
1970 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
1971 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
1972 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
1973 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
1974 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
1975 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
1976 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
1977
1978#define N32N64_SYMBOLIC_REGISTER_NAMES \
1979 {"$a4", RTYPE_GP | 8}, \
1980 {"$a5", RTYPE_GP | 9}, \
1981 {"$a6", RTYPE_GP | 10}, \
1982 {"$a7", RTYPE_GP | 11}, \
1983 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
1984 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
1985 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
1986 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
1987 {"$t0", RTYPE_GP | 12}, \
1988 {"$t1", RTYPE_GP | 13}, \
1989 {"$t2", RTYPE_GP | 14}, \
1990 {"$t3", RTYPE_GP | 15}
1991
1992#define O32_SYMBOLIC_REGISTER_NAMES \
1993 {"$t0", RTYPE_GP | 8}, \
1994 {"$t1", RTYPE_GP | 9}, \
1995 {"$t2", RTYPE_GP | 10}, \
1996 {"$t3", RTYPE_GP | 11}, \
1997 {"$t4", RTYPE_GP | 12}, \
1998 {"$t5", RTYPE_GP | 13}, \
1999 {"$t6", RTYPE_GP | 14}, \
2000 {"$t7", RTYPE_GP | 15}, \
2001 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2002 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2003 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
2004 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
2005
2006/* Remaining symbolic register names */
2007#define SYMBOLIC_REGISTER_NAMES \
2008 {"$zero", RTYPE_GP | 0}, \
2009 {"$at", RTYPE_GP | 1}, \
2010 {"$AT", RTYPE_GP | 1}, \
2011 {"$v0", RTYPE_GP | 2}, \
2012 {"$v1", RTYPE_GP | 3}, \
2013 {"$a0", RTYPE_GP | 4}, \
2014 {"$a1", RTYPE_GP | 5}, \
2015 {"$a2", RTYPE_GP | 6}, \
2016 {"$a3", RTYPE_GP | 7}, \
2017 {"$s0", RTYPE_GP | 16}, \
2018 {"$s1", RTYPE_GP | 17}, \
2019 {"$s2", RTYPE_GP | 18}, \
2020 {"$s3", RTYPE_GP | 19}, \
2021 {"$s4", RTYPE_GP | 20}, \
2022 {"$s5", RTYPE_GP | 21}, \
2023 {"$s6", RTYPE_GP | 22}, \
2024 {"$s7", RTYPE_GP | 23}, \
2025 {"$t8", RTYPE_GP | 24}, \
2026 {"$t9", RTYPE_GP | 25}, \
2027 {"$k0", RTYPE_GP | 26}, \
2028 {"$kt0", RTYPE_GP | 26}, \
2029 {"$k1", RTYPE_GP | 27}, \
2030 {"$kt1", RTYPE_GP | 27}, \
2031 {"$gp", RTYPE_GP | 28}, \
2032 {"$sp", RTYPE_GP | 29}, \
2033 {"$s8", RTYPE_GP | 30}, \
2034 {"$fp", RTYPE_GP | 30}, \
2035 {"$ra", RTYPE_GP | 31}
2036
2037#define MIPS16_SPECIAL_REGISTER_NAMES \
2038 {"$pc", RTYPE_PC | 0}
2039
2040#define MDMX_VECTOR_REGISTER_NAMES \
2041 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
2042 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
2043 {"$v2", RTYPE_VEC | 2}, \
2044 {"$v3", RTYPE_VEC | 3}, \
2045 {"$v4", RTYPE_VEC | 4}, \
2046 {"$v5", RTYPE_VEC | 5}, \
2047 {"$v6", RTYPE_VEC | 6}, \
2048 {"$v7", RTYPE_VEC | 7}, \
2049 {"$v8", RTYPE_VEC | 8}, \
2050 {"$v9", RTYPE_VEC | 9}, \
2051 {"$v10", RTYPE_VEC | 10}, \
2052 {"$v11", RTYPE_VEC | 11}, \
2053 {"$v12", RTYPE_VEC | 12}, \
2054 {"$v13", RTYPE_VEC | 13}, \
2055 {"$v14", RTYPE_VEC | 14}, \
2056 {"$v15", RTYPE_VEC | 15}, \
2057 {"$v16", RTYPE_VEC | 16}, \
2058 {"$v17", RTYPE_VEC | 17}, \
2059 {"$v18", RTYPE_VEC | 18}, \
2060 {"$v19", RTYPE_VEC | 19}, \
2061 {"$v20", RTYPE_VEC | 20}, \
2062 {"$v21", RTYPE_VEC | 21}, \
2063 {"$v22", RTYPE_VEC | 22}, \
2064 {"$v23", RTYPE_VEC | 23}, \
2065 {"$v24", RTYPE_VEC | 24}, \
2066 {"$v25", RTYPE_VEC | 25}, \
2067 {"$v26", RTYPE_VEC | 26}, \
2068 {"$v27", RTYPE_VEC | 27}, \
2069 {"$v28", RTYPE_VEC | 28}, \
2070 {"$v29", RTYPE_VEC | 29}, \
2071 {"$v30", RTYPE_VEC | 30}, \
2072 {"$v31", RTYPE_VEC | 31}
2073
2074#define MIPS_DSP_ACCUMULATOR_NAMES \
2075 {"$ac0", RTYPE_ACC | 0}, \
2076 {"$ac1", RTYPE_ACC | 1}, \
2077 {"$ac2", RTYPE_ACC | 2}, \
2078 {"$ac3", RTYPE_ACC | 3}
2079
2080static const struct regname reg_names[] = {
2081 GENERIC_REGISTER_NUMBERS,
2082 FPU_REGISTER_NAMES,
2083 FPU_CONDITION_CODE_NAMES,
2084 COPROC_CONDITION_CODE_NAMES,
2085
2086 /* The $txx registers depends on the abi,
2087 these will be added later into the symbol table from
2088 one of the tables below once mips_abi is set after
2089 parsing of arguments from the command line. */
2090 SYMBOLIC_REGISTER_NAMES,
2091
2092 MIPS16_SPECIAL_REGISTER_NAMES,
2093 MDMX_VECTOR_REGISTER_NAMES,
2094 MIPS_DSP_ACCUMULATOR_NAMES,
2095 {0, 0}
2096};
2097
2098static const struct regname reg_names_o32[] = {
2099 O32_SYMBOLIC_REGISTER_NAMES,
2100 {0, 0}
2101};
2102
2103static const struct regname reg_names_n32n64[] = {
2104 N32N64_SYMBOLIC_REGISTER_NAMES,
2105 {0, 0}
2106};
2107
df58fc94
RS
2108/* Check if S points at a valid register specifier according to TYPES.
2109 If so, then return 1, advance S to consume the specifier and store
2110 the register's number in REGNOP, otherwise return 0. */
2111
707bfff6
TS
2112static int
2113reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2114{
2115 symbolS *symbolP;
2116 char *e;
2117 char save_c;
2118 int reg = -1;
2119
2120 /* Find end of name. */
2121 e = *s;
2122 if (is_name_beginner (*e))
2123 ++e;
2124 while (is_part_of_name (*e))
2125 ++e;
2126
2127 /* Terminate name. */
2128 save_c = *e;
2129 *e = '\0';
2130
2131 /* Look for a register symbol. */
2132 if ((symbolP = symbol_find (*s)) && S_GET_SEGMENT (symbolP) == reg_section)
2133 {
2134 int r = S_GET_VALUE (symbolP);
2135 if (r & types)
2136 reg = r & RNUM_MASK;
2137 else if ((types & RTYPE_VEC) && (r & ~1) == (RTYPE_GP | 2))
2138 /* Convert GP reg $v0/1 to MDMX reg $v0/1! */
2139 reg = (r & RNUM_MASK) - 2;
2140 }
2141 /* Else see if this is a register defined in an itbl entry. */
2142 else if ((types & RTYPE_GP) && itbl_have_entries)
2143 {
2144 char *n = *s;
2145 unsigned long r;
2146
2147 if (*n == '$')
2148 ++n;
2149 if (itbl_get_reg_val (n, &r))
2150 reg = r & RNUM_MASK;
2151 }
2152
2153 /* Advance to next token if a register was recognised. */
2154 if (reg >= 0)
2155 *s = e;
2156 else if (types & RWARN)
20203fb9 2157 as_warn (_("Unrecognized register name `%s'"), *s);
707bfff6
TS
2158
2159 *e = save_c;
2160 if (regnop)
2161 *regnop = reg;
2162 return reg >= 0;
2163}
2164
df58fc94
RS
2165/* Check if S points at a valid register list according to TYPES.
2166 If so, then return 1, advance S to consume the list and store
2167 the registers present on the list as a bitmask of ones in REGLISTP,
2168 otherwise return 0. A valid list comprises a comma-separated
2169 enumeration of valid single registers and/or dash-separated
2170 contiguous register ranges as determined by their numbers.
2171
2172 As a special exception if one of s0-s7 registers is specified as
2173 the range's lower delimiter and s8 (fp) is its upper one, then no
2174 registers whose numbers place them between s7 and s8 (i.e. $24-$29)
2309ddf2 2175 are selected; they have to be listed separately if needed. */
df58fc94
RS
2176
2177static int
2178reglist_lookup (char **s, unsigned int types, unsigned int *reglistp)
2179{
2180 unsigned int reglist = 0;
2181 unsigned int lastregno;
2182 bfd_boolean ok = TRUE;
2183 unsigned int regmask;
2309ddf2 2184 char *s_endlist = *s;
df58fc94 2185 char *s_reset = *s;
2309ddf2 2186 unsigned int regno;
df58fc94
RS
2187
2188 while (reg_lookup (s, types, &regno))
2189 {
2190 lastregno = regno;
2191 if (**s == '-')
2192 {
2193 (*s)++;
2194 ok = reg_lookup (s, types, &lastregno);
2195 if (ok && lastregno < regno)
2196 ok = FALSE;
2197 if (!ok)
2198 break;
2199 }
2200
2201 if (lastregno == FP && regno >= S0 && regno <= S7)
2202 {
2203 lastregno = S7;
2204 reglist |= 1 << FP;
2205 }
2206 regmask = 1 << lastregno;
2207 regmask = (regmask << 1) - 1;
2208 regmask ^= (1 << regno) - 1;
2209 reglist |= regmask;
2210
2309ddf2 2211 s_endlist = *s;
df58fc94
RS
2212 if (**s != ',')
2213 break;
2214 (*s)++;
2215 }
2216
2217 if (ok)
2309ddf2 2218 *s = s_endlist;
df58fc94
RS
2219 else
2220 *s = s_reset;
2221 if (reglistp)
2222 *reglistp = reglist;
2223 return ok && reglist != 0;
2224}
2225
037b32b9 2226/* Return TRUE if opcode MO is valid on the currently selected ISA and
f79e2745 2227 architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
037b32b9
AN
2228
2229static bfd_boolean
f79e2745 2230is_opcode_valid (const struct mips_opcode *mo)
037b32b9
AN
2231{
2232 int isa = mips_opts.isa;
2233 int fp_s, fp_d;
2234
2235 if (mips_opts.ase_mdmx)
2236 isa |= INSN_MDMX;
2237 if (mips_opts.ase_dsp)
2238 isa |= INSN_DSP;
2239 if (mips_opts.ase_dsp && ISA_SUPPORTS_DSP64_ASE)
2240 isa |= INSN_DSP64;
2241 if (mips_opts.ase_dspr2)
2242 isa |= INSN_DSPR2;
2243 if (mips_opts.ase_mt)
2244 isa |= INSN_MT;
2245 if (mips_opts.ase_mips3d)
2246 isa |= INSN_MIPS3D;
2247 if (mips_opts.ase_smartmips)
2248 isa |= INSN_SMARTMIPS;
dec0624d
MR
2249 if (mips_opts.ase_mcu)
2250 isa |= INSN_MCU;
037b32b9 2251
35d0a169 2252 if (!opcode_is_member (mo, isa, mips_opts.arch))
037b32b9
AN
2253 return FALSE;
2254
2255 /* Check whether the instruction or macro requires single-precision or
2256 double-precision floating-point support. Note that this information is
2257 stored differently in the opcode table for insns and macros. */
2258 if (mo->pinfo == INSN_MACRO)
2259 {
2260 fp_s = mo->pinfo2 & INSN2_M_FP_S;
2261 fp_d = mo->pinfo2 & INSN2_M_FP_D;
2262 }
2263 else
2264 {
2265 fp_s = mo->pinfo & FP_S;
2266 fp_d = mo->pinfo & FP_D;
2267 }
2268
2269 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
2270 return FALSE;
2271
2272 if (fp_s && mips_opts.soft_float)
2273 return FALSE;
2274
2275 return TRUE;
2276}
2277
2278/* Return TRUE if the MIPS16 opcode MO is valid on the currently
2279 selected ISA and architecture. */
2280
2281static bfd_boolean
2282is_opcode_valid_16 (const struct mips_opcode *mo)
2283{
35d0a169 2284 return opcode_is_member (mo, mips_opts.isa, mips_opts.arch);
037b32b9
AN
2285}
2286
df58fc94
RS
2287/* Return TRUE if the size of the microMIPS opcode MO matches one
2288 explicitly requested. Always TRUE in the standard MIPS mode. */
2289
2290static bfd_boolean
2291is_size_valid (const struct mips_opcode *mo)
2292{
2293 if (!mips_opts.micromips)
2294 return TRUE;
2295
2296 if (!forced_insn_length)
2297 return TRUE;
2298 if (mo->pinfo == INSN_MACRO)
2299 return FALSE;
2300 return forced_insn_length == micromips_insn_length (mo);
2301}
2302
2303/* Return TRUE if the microMIPS opcode MO is valid for the delay slot
e64af278
MR
2304 of the preceding instruction. Always TRUE in the standard MIPS mode.
2305
2306 We don't accept macros in 16-bit delay slots to avoid a case where
2307 a macro expansion fails because it relies on a preceding 32-bit real
2308 instruction to have matched and does not handle the operands correctly.
2309 The only macros that may expand to 16-bit instructions are JAL that
2310 cannot be placed in a delay slot anyway, and corner cases of BALIGN
2311 and BGT (that likewise cannot be placed in a delay slot) that decay to
2312 a NOP. In all these cases the macros precede any corresponding real
2313 instruction definitions in the opcode table, so they will match in the
2314 second pass where the size of the delay slot is ignored and therefore
2315 produce correct code. */
df58fc94
RS
2316
2317static bfd_boolean
2318is_delay_slot_valid (const struct mips_opcode *mo)
2319{
2320 if (!mips_opts.micromips)
2321 return TRUE;
2322
2323 if (mo->pinfo == INSN_MACRO)
e64af278
MR
2324 return ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0
2325 ? TRUE : FALSE);
df58fc94
RS
2326 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
2327 && micromips_insn_length (mo) != 4)
2328 return FALSE;
2329 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
2330 && micromips_insn_length (mo) != 2)
2331 return FALSE;
2332
2333 return TRUE;
2334}
2335
707bfff6
TS
2336/* This function is called once, at assembler startup time. It should set up
2337 all the tables, etc. that the MD part of the assembler will need. */
156c2f8b 2338
252b5132 2339void
17a2f251 2340md_begin (void)
252b5132 2341{
3994f87e 2342 const char *retval = NULL;
156c2f8b 2343 int i = 0;
252b5132 2344 int broken = 0;
1f25f5d3 2345
0a44bf69
RS
2346 if (mips_pic != NO_PIC)
2347 {
2348 if (g_switch_seen && g_switch_value != 0)
2349 as_bad (_("-G may not be used in position-independent code"));
2350 g_switch_value = 0;
2351 }
2352
fef14a42 2353 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
252b5132
RH
2354 as_warn (_("Could not set architecture and machine"));
2355
252b5132
RH
2356 op_hash = hash_new ();
2357
2358 for (i = 0; i < NUMOPCODES;)
2359 {
2360 const char *name = mips_opcodes[i].name;
2361
17a2f251 2362 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
2363 if (retval != NULL)
2364 {
2365 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
2366 mips_opcodes[i].name, retval);
2367 /* Probably a memory allocation problem? Give up now. */
2368 as_fatal (_("Broken assembler. No assembly attempted."));
2369 }
2370 do
2371 {
2372 if (mips_opcodes[i].pinfo != INSN_MACRO)
2373 {
2374 if (!validate_mips_insn (&mips_opcodes[i]))
2375 broken = 1;
1e915849
RS
2376 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
2377 {
2378 create_insn (&nop_insn, mips_opcodes + i);
c67a084a
NC
2379 if (mips_fix_loongson2f_nop)
2380 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
1e915849
RS
2381 nop_insn.fixed_p = 1;
2382 }
252b5132
RH
2383 }
2384 ++i;
2385 }
2386 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
2387 }
2388
2389 mips16_op_hash = hash_new ();
2390
2391 i = 0;
2392 while (i < bfd_mips16_num_opcodes)
2393 {
2394 const char *name = mips16_opcodes[i].name;
2395
17a2f251 2396 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
2397 if (retval != NULL)
2398 as_fatal (_("internal: can't hash `%s': %s"),
2399 mips16_opcodes[i].name, retval);
2400 do
2401 {
2402 if (mips16_opcodes[i].pinfo != INSN_MACRO
2403 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
2404 != mips16_opcodes[i].match))
2405 {
2406 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
2407 mips16_opcodes[i].name, mips16_opcodes[i].args);
2408 broken = 1;
2409 }
1e915849
RS
2410 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
2411 {
2412 create_insn (&mips16_nop_insn, mips16_opcodes + i);
2413 mips16_nop_insn.fixed_p = 1;
2414 }
252b5132
RH
2415 ++i;
2416 }
2417 while (i < bfd_mips16_num_opcodes
2418 && strcmp (mips16_opcodes[i].name, name) == 0);
2419 }
2420
df58fc94
RS
2421 micromips_op_hash = hash_new ();
2422
2423 i = 0;
2424 while (i < bfd_micromips_num_opcodes)
2425 {
2426 const char *name = micromips_opcodes[i].name;
2427
2428 retval = hash_insert (micromips_op_hash, name,
2429 (void *) &micromips_opcodes[i]);
2430 if (retval != NULL)
2431 as_fatal (_("internal: can't hash `%s': %s"),
2432 micromips_opcodes[i].name, retval);
2433 do
2434 if (micromips_opcodes[i].pinfo != INSN_MACRO)
2435 {
2436 struct mips_cl_insn *micromips_nop_insn;
2437
2438 if (!validate_micromips_insn (&micromips_opcodes[i]))
2439 broken = 1;
2440
2441 if (micromips_insn_length (micromips_opcodes + i) == 2)
2442 micromips_nop_insn = &micromips_nop16_insn;
2443 else if (micromips_insn_length (micromips_opcodes + i) == 4)
2444 micromips_nop_insn = &micromips_nop32_insn;
2445 else
2446 continue;
2447
2448 if (micromips_nop_insn->insn_mo == NULL
2449 && strcmp (name, "nop") == 0)
2450 {
2451 create_insn (micromips_nop_insn, micromips_opcodes + i);
2452 micromips_nop_insn->fixed_p = 1;
2453 }
2454 }
2455 while (++i < bfd_micromips_num_opcodes
2456 && strcmp (micromips_opcodes[i].name, name) == 0);
2457 }
2458
252b5132
RH
2459 if (broken)
2460 as_fatal (_("Broken assembler. No assembly attempted."));
2461
2462 /* We add all the general register names to the symbol table. This
2463 helps us detect invalid uses of them. */
707bfff6
TS
2464 for (i = 0; reg_names[i].name; i++)
2465 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
8fc4ee9b 2466 reg_names[i].num, /* & RNUM_MASK, */
707bfff6
TS
2467 &zero_address_frag));
2468 if (HAVE_NEWABI)
2469 for (i = 0; reg_names_n32n64[i].name; i++)
2470 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
8fc4ee9b 2471 reg_names_n32n64[i].num, /* & RNUM_MASK, */
252b5132 2472 &zero_address_frag));
707bfff6
TS
2473 else
2474 for (i = 0; reg_names_o32[i].name; i++)
2475 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
8fc4ee9b 2476 reg_names_o32[i].num, /* & RNUM_MASK, */
6047c971 2477 &zero_address_frag));
6047c971 2478
7d10b47d 2479 mips_no_prev_insn ();
252b5132
RH
2480
2481 mips_gprmask = 0;
2482 mips_cprmask[0] = 0;
2483 mips_cprmask[1] = 0;
2484 mips_cprmask[2] = 0;
2485 mips_cprmask[3] = 0;
2486
2487 /* set the default alignment for the text section (2**2) */
2488 record_alignment (text_section, 2);
2489
4d0d148d 2490 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132 2491
707bfff6 2492#ifdef OBJ_ELF
f43abd2b 2493 if (IS_ELF)
252b5132 2494 {
0a44bf69
RS
2495 /* On a native system other than VxWorks, sections must be aligned
2496 to 16 byte boundaries. When configured for an embedded ELF
2497 target, we don't bother. */
c41e87e3
CF
2498 if (strncmp (TARGET_OS, "elf", 3) != 0
2499 && strncmp (TARGET_OS, "vxworks", 7) != 0)
252b5132
RH
2500 {
2501 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
2502 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
2503 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
2504 }
2505
2506 /* Create a .reginfo section for register masks and a .mdebug
2507 section for debugging information. */
2508 {
2509 segT seg;
2510 subsegT subseg;
2511 flagword flags;
2512 segT sec;
2513
2514 seg = now_seg;
2515 subseg = now_subseg;
2516
2517 /* The ABI says this section should be loaded so that the
2518 running program can access it. However, we don't load it
2519 if we are configured for an embedded target */
2520 flags = SEC_READONLY | SEC_DATA;
c41e87e3 2521 if (strncmp (TARGET_OS, "elf", 3) != 0)
252b5132
RH
2522 flags |= SEC_ALLOC | SEC_LOAD;
2523
316f5878 2524 if (mips_abi != N64_ABI)
252b5132
RH
2525 {
2526 sec = subseg_new (".reginfo", (subsegT) 0);
2527
195325d2
TS
2528 bfd_set_section_flags (stdoutput, sec, flags);
2529 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
bdaaa2e1 2530
252b5132 2531 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
252b5132
RH
2532 }
2533 else
2534 {
2535 /* The 64-bit ABI uses a .MIPS.options section rather than
2536 .reginfo section. */
2537 sec = subseg_new (".MIPS.options", (subsegT) 0);
195325d2
TS
2538 bfd_set_section_flags (stdoutput, sec, flags);
2539 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132 2540
252b5132
RH
2541 /* Set up the option header. */
2542 {
2543 Elf_Internal_Options opthdr;
2544 char *f;
2545
2546 opthdr.kind = ODK_REGINFO;
2547 opthdr.size = (sizeof (Elf_External_Options)
2548 + sizeof (Elf64_External_RegInfo));
2549 opthdr.section = 0;
2550 opthdr.info = 0;
2551 f = frag_more (sizeof (Elf_External_Options));
2552 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
2553 (Elf_External_Options *) f);
2554
2555 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
2556 }
252b5132
RH
2557 }
2558
2559 if (ECOFF_DEBUGGING)
2560 {
2561 sec = subseg_new (".mdebug", (subsegT) 0);
2562 (void) bfd_set_section_flags (stdoutput, sec,
2563 SEC_HAS_CONTENTS | SEC_READONLY);
2564 (void) bfd_set_section_alignment (stdoutput, sec, 2);
2565 }
f43abd2b 2566 else if (mips_flag_pdr)
ecb4347a
DJ
2567 {
2568 pdr_seg = subseg_new (".pdr", (subsegT) 0);
2569 (void) bfd_set_section_flags (stdoutput, pdr_seg,
2570 SEC_READONLY | SEC_RELOC
2571 | SEC_DEBUGGING);
2572 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
2573 }
252b5132
RH
2574
2575 subseg_set (seg, subseg);
2576 }
2577 }
707bfff6 2578#endif /* OBJ_ELF */
252b5132
RH
2579
2580 if (! ECOFF_DEBUGGING)
2581 md_obj_begin ();
71400594
RS
2582
2583 if (mips_fix_vr4120)
2584 init_vr4120_conflicts ();
252b5132
RH
2585}
2586
2587void
17a2f251 2588md_mips_end (void)
252b5132 2589{
02b1ab82 2590 mips_emit_delays ();
252b5132
RH
2591 if (! ECOFF_DEBUGGING)
2592 md_obj_end ();
2593}
2594
2595void
17a2f251 2596md_assemble (char *str)
252b5132
RH
2597{
2598 struct mips_cl_insn insn;
f6688943
TS
2599 bfd_reloc_code_real_type unused_reloc[3]
2600 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
2601
2602 imm_expr.X_op = O_absent;
5f74bc13 2603 imm2_expr.X_op = O_absent;
252b5132 2604 offset_expr.X_op = O_absent;
f6688943
TS
2605 imm_reloc[0] = BFD_RELOC_UNUSED;
2606 imm_reloc[1] = BFD_RELOC_UNUSED;
2607 imm_reloc[2] = BFD_RELOC_UNUSED;
2608 offset_reloc[0] = BFD_RELOC_UNUSED;
2609 offset_reloc[1] = BFD_RELOC_UNUSED;
2610 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132 2611
e1b47bd5
RS
2612 mips_mark_labels ();
2613 mips_assembling_insn = TRUE;
2614
252b5132
RH
2615 if (mips_opts.mips16)
2616 mips16_ip (str, &insn);
2617 else
2618 {
2619 mips_ip (str, &insn);
beae10d5
KH
2620 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
2621 str, insn.insn_opcode));
252b5132
RH
2622 }
2623
2624 if (insn_error)
e1b47bd5
RS
2625 as_bad ("%s `%s'", insn_error, str);
2626 else if (insn.insn_mo->pinfo == INSN_MACRO)
252b5132 2627 {
584892a6 2628 macro_start ();
252b5132
RH
2629 if (mips_opts.mips16)
2630 mips16_macro (&insn);
2631 else
2632 macro (&insn);
584892a6 2633 macro_end ();
252b5132
RH
2634 }
2635 else
2636 {
2637 if (imm_expr.X_op != O_absent)
df58fc94 2638 append_insn (&insn, &imm_expr, imm_reloc, FALSE);
252b5132 2639 else if (offset_expr.X_op != O_absent)
df58fc94 2640 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
252b5132 2641 else
df58fc94 2642 append_insn (&insn, NULL, unused_reloc, FALSE);
252b5132 2643 }
e1b47bd5
RS
2644
2645 mips_assembling_insn = FALSE;
252b5132
RH
2646}
2647
738e5348
RS
2648/* Convenience functions for abstracting away the differences between
2649 MIPS16 and non-MIPS16 relocations. */
2650
2651static inline bfd_boolean
2652mips16_reloc_p (bfd_reloc_code_real_type reloc)
2653{
2654 switch (reloc)
2655 {
2656 case BFD_RELOC_MIPS16_JMP:
2657 case BFD_RELOC_MIPS16_GPREL:
2658 case BFD_RELOC_MIPS16_GOT16:
2659 case BFD_RELOC_MIPS16_CALL16:
2660 case BFD_RELOC_MIPS16_HI16_S:
2661 case BFD_RELOC_MIPS16_HI16:
2662 case BFD_RELOC_MIPS16_LO16:
2663 return TRUE;
2664
2665 default:
2666 return FALSE;
2667 }
2668}
2669
df58fc94
RS
2670static inline bfd_boolean
2671micromips_reloc_p (bfd_reloc_code_real_type reloc)
2672{
2673 switch (reloc)
2674 {
2675 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
2676 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
2677 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
2678 case BFD_RELOC_MICROMIPS_GPREL16:
2679 case BFD_RELOC_MICROMIPS_JMP:
2680 case BFD_RELOC_MICROMIPS_HI16:
2681 case BFD_RELOC_MICROMIPS_HI16_S:
2682 case BFD_RELOC_MICROMIPS_LO16:
2683 case BFD_RELOC_MICROMIPS_LITERAL:
2684 case BFD_RELOC_MICROMIPS_GOT16:
2685 case BFD_RELOC_MICROMIPS_CALL16:
2686 case BFD_RELOC_MICROMIPS_GOT_HI16:
2687 case BFD_RELOC_MICROMIPS_GOT_LO16:
2688 case BFD_RELOC_MICROMIPS_CALL_HI16:
2689 case BFD_RELOC_MICROMIPS_CALL_LO16:
2690 case BFD_RELOC_MICROMIPS_SUB:
2691 case BFD_RELOC_MICROMIPS_GOT_PAGE:
2692 case BFD_RELOC_MICROMIPS_GOT_OFST:
2693 case BFD_RELOC_MICROMIPS_GOT_DISP:
2694 case BFD_RELOC_MICROMIPS_HIGHEST:
2695 case BFD_RELOC_MICROMIPS_HIGHER:
2696 case BFD_RELOC_MICROMIPS_SCN_DISP:
2697 case BFD_RELOC_MICROMIPS_JALR:
2698 return TRUE;
2699
2700 default:
2701 return FALSE;
2702 }
2703}
2704
2309ddf2
MR
2705static inline bfd_boolean
2706jmp_reloc_p (bfd_reloc_code_real_type reloc)
2707{
2708 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
2709}
2710
738e5348
RS
2711static inline bfd_boolean
2712got16_reloc_p (bfd_reloc_code_real_type reloc)
2713{
2309ddf2 2714 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
df58fc94 2715 || reloc == BFD_RELOC_MICROMIPS_GOT16);
738e5348
RS
2716}
2717
2718static inline bfd_boolean
2719hi16_reloc_p (bfd_reloc_code_real_type reloc)
2720{
2309ddf2 2721 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
df58fc94 2722 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
738e5348
RS
2723}
2724
2725static inline bfd_boolean
2726lo16_reloc_p (bfd_reloc_code_real_type reloc)
2727{
2309ddf2 2728 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
df58fc94
RS
2729 || reloc == BFD_RELOC_MICROMIPS_LO16);
2730}
2731
df58fc94
RS
2732static inline bfd_boolean
2733jalr_reloc_p (bfd_reloc_code_real_type reloc)
2734{
2309ddf2 2735 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
738e5348
RS
2736}
2737
5919d012 2738/* Return true if the given relocation might need a matching %lo().
0a44bf69
RS
2739 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
2740 need a matching %lo() when applied to local symbols. */
5919d012
RS
2741
2742static inline bfd_boolean
17a2f251 2743reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 2744{
3b91255e 2745 return (HAVE_IN_PLACE_ADDENDS
738e5348 2746 && (hi16_reloc_p (reloc)
0a44bf69
RS
2747 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
2748 all GOT16 relocations evaluate to "G". */
738e5348
RS
2749 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
2750}
2751
2752/* Return the type of %lo() reloc needed by RELOC, given that
2753 reloc_needs_lo_p. */
2754
2755static inline bfd_reloc_code_real_type
2756matching_lo_reloc (bfd_reloc_code_real_type reloc)
2757{
df58fc94
RS
2758 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
2759 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
2760 : BFD_RELOC_LO16));
5919d012
RS
2761}
2762
2763/* Return true if the given fixup is followed by a matching R_MIPS_LO16
2764 relocation. */
2765
2766static inline bfd_boolean
17a2f251 2767fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
2768{
2769 return (fixp->fx_next != NULL
738e5348 2770 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
5919d012
RS
2771 && fixp->fx_addsy == fixp->fx_next->fx_addsy
2772 && fixp->fx_offset == fixp->fx_next->fx_offset);
2773}
2774
252b5132
RH
2775/* This function returns true if modifying a register requires a
2776 delay. */
2777
2778static int
17a2f251 2779reg_needs_delay (unsigned int reg)
252b5132
RH
2780{
2781 unsigned long prev_pinfo;
2782
47e39b9d 2783 prev_pinfo = history[0].insn_mo->pinfo;
252b5132 2784 if (! mips_opts.noreorder
81912461
ILT
2785 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
2786 && ! gpr_interlocks)
2787 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
2788 && ! cop_interlocks)))
252b5132 2789 {
81912461
ILT
2790 /* A load from a coprocessor or from memory. All load delays
2791 delay the use of general register rt for one instruction. */
bdaaa2e1 2792 /* Itbl support may require additional care here. */
252b5132 2793 know (prev_pinfo & INSN_WRITE_GPR_T);
df58fc94 2794 if (reg == EXTRACT_OPERAND (mips_opts.micromips, RT, history[0]))
252b5132
RH
2795 return 1;
2796 }
2797
2798 return 0;
2799}
2800
462427c4
RS
2801/* Move all labels in LABELS to the current insertion point. TEXT_P
2802 says whether the labels refer to text or data. */
404a8071
RS
2803
2804static void
462427c4 2805mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
404a8071
RS
2806{
2807 struct insn_label_list *l;
2808 valueT val;
2809
462427c4 2810 for (l = labels; l != NULL; l = l->next)
404a8071 2811 {
9c2799c2 2812 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
404a8071
RS
2813 symbol_set_frag (l->label, frag_now);
2814 val = (valueT) frag_now_fix ();
df58fc94 2815 /* MIPS16/microMIPS text labels are stored as odd. */
462427c4 2816 if (text_p && HAVE_CODE_COMPRESSION)
404a8071
RS
2817 ++val;
2818 S_SET_VALUE (l->label, val);
2819 }
2820}
2821
462427c4
RS
2822/* Move all labels in insn_labels to the current insertion point
2823 and treat them as text labels. */
2824
2825static void
2826mips_move_text_labels (void)
2827{
2828 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
2829}
2830
5f0fe04b
TS
2831static bfd_boolean
2832s_is_linkonce (symbolS *sym, segT from_seg)
2833{
2834 bfd_boolean linkonce = FALSE;
2835 segT symseg = S_GET_SEGMENT (sym);
2836
2837 if (symseg != from_seg && !S_IS_LOCAL (sym))
2838 {
2839 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
2840 linkonce = TRUE;
2841#ifdef OBJ_ELF
2842 /* The GNU toolchain uses an extension for ELF: a section
2843 beginning with the magic string .gnu.linkonce is a
2844 linkonce section. */
2845 if (strncmp (segment_name (symseg), ".gnu.linkonce",
2846 sizeof ".gnu.linkonce" - 1) == 0)
2847 linkonce = TRUE;
2848#endif
2849 }
2850 return linkonce;
2851}
2852
e1b47bd5 2853/* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
df58fc94
RS
2854 linker to handle them specially, such as generating jalx instructions
2855 when needed. We also make them odd for the duration of the assembly,
2856 in order to generate the right sort of code. We will make them even
252b5132
RH
2857 in the adjust_symtab routine, while leaving them marked. This is
2858 convenient for the debugger and the disassembler. The linker knows
2859 to make them odd again. */
2860
2861static void
e1b47bd5 2862mips_compressed_mark_label (symbolS *label)
252b5132 2863{
df58fc94 2864 gas_assert (HAVE_CODE_COMPRESSION);
a8dbcb85 2865
a8dbcb85 2866#if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
e1b47bd5
RS
2867 if (IS_ELF)
2868 {
2869 if (mips_opts.mips16)
2870 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
2871 else
2872 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
252b5132 2873 }
e1b47bd5
RS
2874#endif
2875 if ((S_GET_VALUE (label) & 1) == 0
2876 /* Don't adjust the address if the label is global or weak, or
2877 in a link-once section, since we'll be emitting symbol reloc
2878 references to it which will be patched up by the linker, and
2879 the final value of the symbol may or may not be MIPS16/microMIPS. */
2880 && !S_IS_WEAK (label)
2881 && !S_IS_EXTERNAL (label)
2882 && !s_is_linkonce (label, now_seg))
2883 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
2884}
2885
2886/* Mark preceding MIPS16 or microMIPS instruction labels. */
2887
2888static void
2889mips_compressed_mark_labels (void)
2890{
2891 struct insn_label_list *l;
2892
2893 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
2894 mips_compressed_mark_label (l->label);
252b5132
RH
2895}
2896
4d7206a2
RS
2897/* End the current frag. Make it a variant frag and record the
2898 relaxation info. */
2899
2900static void
2901relax_close_frag (void)
2902{
584892a6 2903 mips_macro_warning.first_frag = frag_now;
4d7206a2 2904 frag_var (rs_machine_dependent, 0, 0,
584892a6 2905 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
4d7206a2
RS
2906 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
2907
2908 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
2909 mips_relax.first_fixup = 0;
2910}
2911
2912/* Start a new relaxation sequence whose expansion depends on SYMBOL.
2913 See the comment above RELAX_ENCODE for more details. */
2914
2915static void
2916relax_start (symbolS *symbol)
2917{
9c2799c2 2918 gas_assert (mips_relax.sequence == 0);
4d7206a2
RS
2919 mips_relax.sequence = 1;
2920 mips_relax.symbol = symbol;
2921}
2922
2923/* Start generating the second version of a relaxable sequence.
2924 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
2925
2926static void
4d7206a2
RS
2927relax_switch (void)
2928{
9c2799c2 2929 gas_assert (mips_relax.sequence == 1);
4d7206a2
RS
2930 mips_relax.sequence = 2;
2931}
2932
2933/* End the current relaxable sequence. */
2934
2935static void
2936relax_end (void)
2937{
9c2799c2 2938 gas_assert (mips_relax.sequence == 2);
4d7206a2
RS
2939 relax_close_frag ();
2940 mips_relax.sequence = 0;
2941}
2942
11625dd8
RS
2943/* Return true if IP is a delayed branch or jump. */
2944
2945static inline bfd_boolean
2946delayed_branch_p (const struct mips_cl_insn *ip)
2947{
2948 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
2949 | INSN_COND_BRANCH_DELAY
2950 | INSN_COND_BRANCH_LIKELY)) != 0;
2951}
2952
2953/* Return true if IP is a compact branch or jump. */
2954
2955static inline bfd_boolean
2956compact_branch_p (const struct mips_cl_insn *ip)
2957{
2958 if (mips_opts.mips16)
2959 return (ip->insn_mo->pinfo & (MIPS16_INSN_UNCOND_BRANCH
2960 | MIPS16_INSN_COND_BRANCH)) != 0;
2961 else
2962 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
2963 | INSN2_COND_BRANCH)) != 0;
2964}
2965
2966/* Return true if IP is an unconditional branch or jump. */
2967
2968static inline bfd_boolean
2969uncond_branch_p (const struct mips_cl_insn *ip)
2970{
2971 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
2972 || (mips_opts.mips16
2973 ? (ip->insn_mo->pinfo & MIPS16_INSN_UNCOND_BRANCH) != 0
2974 : (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0));
2975}
2976
2977/* Return true if IP is a branch-likely instruction. */
2978
2979static inline bfd_boolean
2980branch_likely_p (const struct mips_cl_insn *ip)
2981{
2982 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
2983}
2984
14fe068b
RS
2985/* Return the type of nop that should be used to fill the delay slot
2986 of delayed branch IP. */
2987
2988static struct mips_cl_insn *
2989get_delay_slot_nop (const struct mips_cl_insn *ip)
2990{
2991 if (mips_opts.micromips
2992 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
2993 return &micromips_nop32_insn;
2994 return NOP_INSN;
2995}
2996
2309ddf2 2997/* Return the mask of core registers that IP reads or writes. */
df58fc94
RS
2998
2999static unsigned int
3000gpr_mod_mask (const struct mips_cl_insn *ip)
3001{
2309ddf2 3002 unsigned long pinfo2;
df58fc94
RS
3003 unsigned int mask;
3004
3005 mask = 0;
df58fc94
RS
3006 pinfo2 = ip->insn_mo->pinfo2;
3007 if (mips_opts.micromips)
3008 {
df58fc94
RS
3009 if (pinfo2 & INSN2_MOD_GPR_MD)
3010 mask |= 1 << micromips_to_32_reg_d_map[EXTRACT_OPERAND (1, MD, *ip)];
df58fc94
RS
3011 if (pinfo2 & INSN2_MOD_GPR_MF)
3012 mask |= 1 << micromips_to_32_reg_f_map[EXTRACT_OPERAND (1, MF, *ip)];
df58fc94
RS
3013 if (pinfo2 & INSN2_MOD_SP)
3014 mask |= 1 << SP;
3015 }
3016 return mask;
3017}
3018
4c260379
RS
3019/* Return the mask of core registers that IP reads. */
3020
3021static unsigned int
3022gpr_read_mask (const struct mips_cl_insn *ip)
3023{
3024 unsigned long pinfo, pinfo2;
3025 unsigned int mask;
3026
df58fc94 3027 mask = gpr_mod_mask (ip);
4c260379
RS
3028 pinfo = ip->insn_mo->pinfo;
3029 pinfo2 = ip->insn_mo->pinfo2;
3030 if (mips_opts.mips16)
3031 {
3032 if (pinfo & MIPS16_INSN_READ_X)
3033 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)];
3034 if (pinfo & MIPS16_INSN_READ_Y)
3035 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)];
3036 if (pinfo & MIPS16_INSN_READ_T)
3037 mask |= 1 << TREG;
3038 if (pinfo & MIPS16_INSN_READ_SP)
3039 mask |= 1 << SP;
3040 if (pinfo & MIPS16_INSN_READ_31)
3041 mask |= 1 << RA;
3042 if (pinfo & MIPS16_INSN_READ_Z)
3043 mask |= 1 << (mips16_to_32_reg_map
3044 [MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip)]);
3045 if (pinfo & MIPS16_INSN_READ_GPR_X)
3046 mask |= 1 << MIPS16_EXTRACT_OPERAND (REGR32, *ip);
3047 }
3048 else
3049 {
3050 if (pinfo2 & INSN2_READ_GPR_D)
2309ddf2 3051 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4c260379 3052 if (pinfo & INSN_READ_GPR_T)
2309ddf2 3053 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4c260379 3054 if (pinfo & INSN_READ_GPR_S)
2309ddf2
MR
3055 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
3056 if (pinfo2 & INSN2_READ_GP)
3057 mask |= 1 << GP;
3058 if (pinfo2 & INSN2_READ_GPR_31)
3059 mask |= 1 << RA;
4c260379 3060 if (pinfo2 & INSN2_READ_GPR_Z)
2309ddf2 3061 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RZ, *ip);
4c260379 3062 }
2b0c8b40
MR
3063 if (mips_opts.micromips)
3064 {
3065 if (pinfo2 & INSN2_READ_GPR_MC)
3066 mask |= 1 << micromips_to_32_reg_c_map[EXTRACT_OPERAND (1, MC, *ip)];
3067 if (pinfo2 & INSN2_READ_GPR_ME)
3068 mask |= 1 << micromips_to_32_reg_e_map[EXTRACT_OPERAND (1, ME, *ip)];
3069 if (pinfo2 & INSN2_READ_GPR_MG)
3070 mask |= 1 << micromips_to_32_reg_g_map[EXTRACT_OPERAND (1, MG, *ip)];
3071 if (pinfo2 & INSN2_READ_GPR_MJ)
3072 mask |= 1 << EXTRACT_OPERAND (1, MJ, *ip);
3073 if (pinfo2 & INSN2_READ_GPR_MMN)
3074 {
3075 mask |= 1 << micromips_to_32_reg_m_map[EXTRACT_OPERAND (1, MM, *ip)];
3076 mask |= 1 << micromips_to_32_reg_n_map[EXTRACT_OPERAND (1, MN, *ip)];
3077 }
3078 if (pinfo2 & INSN2_READ_GPR_MP)
3079 mask |= 1 << EXTRACT_OPERAND (1, MP, *ip);
3080 if (pinfo2 & INSN2_READ_GPR_MQ)
3081 mask |= 1 << micromips_to_32_reg_q_map[EXTRACT_OPERAND (1, MQ, *ip)];
3082 }
fe35f09f
RS
3083 /* Don't include register 0. */
3084 return mask & ~1;
4c260379
RS
3085}
3086
3087/* Return the mask of core registers that IP writes. */
3088
3089static unsigned int
3090gpr_write_mask (const struct mips_cl_insn *ip)
3091{
3092 unsigned long pinfo, pinfo2;
3093 unsigned int mask;
3094
df58fc94 3095 mask = gpr_mod_mask (ip);
4c260379
RS
3096 pinfo = ip->insn_mo->pinfo;
3097 pinfo2 = ip->insn_mo->pinfo2;
3098 if (mips_opts.mips16)
3099 {
3100 if (pinfo & MIPS16_INSN_WRITE_X)
3101 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)];
3102 if (pinfo & MIPS16_INSN_WRITE_Y)
3103 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)];
3104 if (pinfo & MIPS16_INSN_WRITE_Z)
3105 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RZ, *ip)];
3106 if (pinfo & MIPS16_INSN_WRITE_T)
3107 mask |= 1 << TREG;
3108 if (pinfo & MIPS16_INSN_WRITE_SP)
3109 mask |= 1 << SP;
3110 if (pinfo & MIPS16_INSN_WRITE_31)
3111 mask |= 1 << RA;
3112 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
3113 mask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
3114 }
3115 else
3116 {
3117 if (pinfo & INSN_WRITE_GPR_D)
df58fc94 3118 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4c260379 3119 if (pinfo & INSN_WRITE_GPR_T)
df58fc94 3120 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
2b0c8b40 3121 if (pinfo & INSN_WRITE_GPR_S)
2309ddf2 3122 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4c260379
RS
3123 if (pinfo & INSN_WRITE_GPR_31)
3124 mask |= 1 << RA;
3125 if (pinfo2 & INSN2_WRITE_GPR_Z)
df58fc94 3126 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RZ, *ip);
4c260379 3127 }
2b0c8b40
MR
3128 if (mips_opts.micromips)
3129 {
3130 if (pinfo2 & INSN2_WRITE_GPR_MB)
3131 mask |= 1 << micromips_to_32_reg_b_map[EXTRACT_OPERAND (1, MB, *ip)];
3132 if (pinfo2 & INSN2_WRITE_GPR_MHI)
3133 {
3134 mask |= 1 << micromips_to_32_reg_h_map[EXTRACT_OPERAND (1, MH, *ip)];
3135 mask |= 1 << micromips_to_32_reg_i_map[EXTRACT_OPERAND (1, MI, *ip)];
3136 }
3137 if (pinfo2 & INSN2_WRITE_GPR_MJ)
3138 mask |= 1 << EXTRACT_OPERAND (1, MJ, *ip);
3139 if (pinfo2 & INSN2_WRITE_GPR_MP)
3140 mask |= 1 << EXTRACT_OPERAND (1, MP, *ip);
3141 }
fe35f09f
RS
3142 /* Don't include register 0. */
3143 return mask & ~1;
4c260379
RS
3144}
3145
3146/* Return the mask of floating-point registers that IP reads. */
3147
3148static unsigned int
3149fpr_read_mask (const struct mips_cl_insn *ip)
3150{
3151 unsigned long pinfo, pinfo2;
3152 unsigned int mask;
3153
3154 mask = 0;
3155 pinfo = ip->insn_mo->pinfo;
3156 pinfo2 = ip->insn_mo->pinfo2;
2309ddf2 3157 if (!mips_opts.mips16)
df58fc94
RS
3158 {
3159 if (pinfo2 & INSN2_READ_FPR_D)
2309ddf2 3160 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FD, *ip);
4c260379 3161 if (pinfo & INSN_READ_FPR_S)
df58fc94 3162 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FS, *ip);
4c260379 3163 if (pinfo & INSN_READ_FPR_T)
df58fc94 3164 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FT, *ip);
4c260379 3165 if (pinfo & INSN_READ_FPR_R)
df58fc94 3166 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FR, *ip);
4c260379 3167 if (pinfo2 & INSN2_READ_FPR_Z)
df58fc94 3168 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FZ, *ip);
4c260379
RS
3169 }
3170 /* Conservatively treat all operands to an FP_D instruction are doubles.
3171 (This is overly pessimistic for things like cvt.d.s.) */
3172 if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3173 mask |= mask << 1;
3174 return mask;
3175}
3176
3177/* Return the mask of floating-point registers that IP writes. */
3178
3179static unsigned int
3180fpr_write_mask (const struct mips_cl_insn *ip)
3181{
3182 unsigned long pinfo, pinfo2;
3183 unsigned int mask;
3184
3185 mask = 0;
3186 pinfo = ip->insn_mo->pinfo;
3187 pinfo2 = ip->insn_mo->pinfo2;
2309ddf2 3188 if (!mips_opts.mips16)
4c260379
RS
3189 {
3190 if (pinfo & INSN_WRITE_FPR_D)
df58fc94 3191 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FD, *ip);
4c260379 3192 if (pinfo & INSN_WRITE_FPR_S)
df58fc94 3193 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FS, *ip);
4c260379 3194 if (pinfo & INSN_WRITE_FPR_T)
df58fc94 3195 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FT, *ip);
4c260379 3196 if (pinfo2 & INSN2_WRITE_FPR_Z)
df58fc94 3197 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FZ, *ip);
4c260379
RS
3198 }
3199 /* Conservatively treat all operands to an FP_D instruction are doubles.
3200 (This is overly pessimistic for things like cvt.s.d.) */
3201 if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3202 mask |= mask << 1;
3203 return mask;
3204}
3205
71400594
RS
3206/* Classify an instruction according to the FIX_VR4120_* enumeration.
3207 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
3208 by VR4120 errata. */
4d7206a2 3209
71400594
RS
3210static unsigned int
3211classify_vr4120_insn (const char *name)
252b5132 3212{
71400594
RS
3213 if (strncmp (name, "macc", 4) == 0)
3214 return FIX_VR4120_MACC;
3215 if (strncmp (name, "dmacc", 5) == 0)
3216 return FIX_VR4120_DMACC;
3217 if (strncmp (name, "mult", 4) == 0)
3218 return FIX_VR4120_MULT;
3219 if (strncmp (name, "dmult", 5) == 0)
3220 return FIX_VR4120_DMULT;
3221 if (strstr (name, "div"))
3222 return FIX_VR4120_DIV;
3223 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
3224 return FIX_VR4120_MTHILO;
3225 return NUM_FIX_VR4120_CLASSES;
3226}
252b5132 3227
ff239038
CM
3228#define INSN_ERET 0x42000018
3229#define INSN_DERET 0x4200001f
3230
71400594
RS
3231/* Return the number of instructions that must separate INSN1 and INSN2,
3232 where INSN1 is the earlier instruction. Return the worst-case value
3233 for any INSN2 if INSN2 is null. */
252b5132 3234
71400594
RS
3235static unsigned int
3236insns_between (const struct mips_cl_insn *insn1,
3237 const struct mips_cl_insn *insn2)
3238{
3239 unsigned long pinfo1, pinfo2;
4c260379 3240 unsigned int mask;
71400594
RS
3241
3242 /* This function needs to know which pinfo flags are set for INSN2
3243 and which registers INSN2 uses. The former is stored in PINFO2 and
4c260379
RS
3244 the latter is tested via INSN2_USES_GPR. If INSN2 is null, PINFO2
3245 will have every flag set and INSN2_USES_GPR will always return true. */
71400594
RS
3246 pinfo1 = insn1->insn_mo->pinfo;
3247 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
252b5132 3248
4c260379
RS
3249#define INSN2_USES_GPR(REG) \
3250 (insn2 == NULL || (gpr_read_mask (insn2) & (1U << (REG))) != 0)
71400594
RS
3251
3252 /* For most targets, write-after-read dependencies on the HI and LO
3253 registers must be separated by at least two instructions. */
3254 if (!hilo_interlocks)
252b5132 3255 {
71400594
RS
3256 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
3257 return 2;
3258 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
3259 return 2;
3260 }
3261
3262 /* If we're working around r7000 errata, there must be two instructions
3263 between an mfhi or mflo and any instruction that uses the result. */
3264 if (mips_7000_hilo_fix
df58fc94 3265 && !mips_opts.micromips
71400594 3266 && MF_HILO_INSN (pinfo1)
df58fc94 3267 && INSN2_USES_GPR (EXTRACT_OPERAND (0, RD, *insn1)))
71400594
RS
3268 return 2;
3269
ff239038
CM
3270 /* If we're working around 24K errata, one instruction is required
3271 if an ERET or DERET is followed by a branch instruction. */
df58fc94 3272 if (mips_fix_24k && !mips_opts.micromips)
ff239038
CM
3273 {
3274 if (insn1->insn_opcode == INSN_ERET
3275 || insn1->insn_opcode == INSN_DERET)
3276 {
3277 if (insn2 == NULL
3278 || insn2->insn_opcode == INSN_ERET
3279 || insn2->insn_opcode == INSN_DERET
11625dd8 3280 || delayed_branch_p (insn2))
ff239038
CM
3281 return 1;
3282 }
3283 }
3284
71400594
RS
3285 /* If working around VR4120 errata, check for combinations that need
3286 a single intervening instruction. */
df58fc94 3287 if (mips_fix_vr4120 && !mips_opts.micromips)
71400594
RS
3288 {
3289 unsigned int class1, class2;
252b5132 3290
71400594
RS
3291 class1 = classify_vr4120_insn (insn1->insn_mo->name);
3292 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
252b5132 3293 {
71400594
RS
3294 if (insn2 == NULL)
3295 return 1;
3296 class2 = classify_vr4120_insn (insn2->insn_mo->name);
3297 if (vr4120_conflicts[class1] & (1 << class2))
3298 return 1;
252b5132 3299 }
71400594
RS
3300 }
3301
df58fc94 3302 if (!HAVE_CODE_COMPRESSION)
71400594
RS
3303 {
3304 /* Check for GPR or coprocessor load delays. All such delays
3305 are on the RT register. */
3306 /* Itbl support may require additional care here. */
3307 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
3308 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
252b5132 3309 {
71400594 3310 know (pinfo1 & INSN_WRITE_GPR_T);
df58fc94 3311 if (INSN2_USES_GPR (EXTRACT_OPERAND (0, RT, *insn1)))
71400594
RS
3312 return 1;
3313 }
3314
3315 /* Check for generic coprocessor hazards.
3316
3317 This case is not handled very well. There is no special
3318 knowledge of CP0 handling, and the coprocessors other than
3319 the floating point unit are not distinguished at all. */
3320 /* Itbl support may require additional care here. FIXME!
3321 Need to modify this to include knowledge about
3322 user specified delays! */
3323 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
3324 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
3325 {
3326 /* Handle cases where INSN1 writes to a known general coprocessor
3327 register. There must be a one instruction delay before INSN2
3328 if INSN2 reads that register, otherwise no delay is needed. */
4c260379
RS
3329 mask = fpr_write_mask (insn1);
3330 if (mask != 0)
252b5132 3331 {
4c260379 3332 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
71400594 3333 return 1;
252b5132
RH
3334 }
3335 else
3336 {
71400594
RS
3337 /* Read-after-write dependencies on the control registers
3338 require a two-instruction gap. */
3339 if ((pinfo1 & INSN_WRITE_COND_CODE)
3340 && (pinfo2 & INSN_READ_COND_CODE))
3341 return 2;
3342
3343 /* We don't know exactly what INSN1 does. If INSN2 is
3344 also a coprocessor instruction, assume there must be
3345 a one instruction gap. */
3346 if (pinfo2 & INSN_COP)
3347 return 1;
252b5132
RH
3348 }
3349 }
6b76fefe 3350
71400594
RS
3351 /* Check for read-after-write dependencies on the coprocessor
3352 control registers in cases where INSN1 does not need a general
3353 coprocessor delay. This means that INSN1 is a floating point
3354 comparison instruction. */
3355 /* Itbl support may require additional care here. */
3356 else if (!cop_interlocks
3357 && (pinfo1 & INSN_WRITE_COND_CODE)
3358 && (pinfo2 & INSN_READ_COND_CODE))
3359 return 1;
3360 }
6b76fefe 3361
4c260379 3362#undef INSN2_USES_GPR
6b76fefe 3363
71400594
RS
3364 return 0;
3365}
6b76fefe 3366
7d8e00cf
RS
3367/* Return the number of nops that would be needed to work around the
3368 VR4130 mflo/mfhi errata if instruction INSN immediately followed
932d1a1b
RS
3369 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
3370 that are contained within the first IGNORE instructions of HIST. */
7d8e00cf
RS
3371
3372static int
932d1a1b 3373nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
7d8e00cf
RS
3374 const struct mips_cl_insn *insn)
3375{
4c260379
RS
3376 int i, j;
3377 unsigned int mask;
7d8e00cf
RS
3378
3379 /* Check if the instruction writes to HI or LO. MTHI and MTLO
3380 are not affected by the errata. */
3381 if (insn != 0
3382 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
3383 || strcmp (insn->insn_mo->name, "mtlo") == 0
3384 || strcmp (insn->insn_mo->name, "mthi") == 0))
3385 return 0;
3386
3387 /* Search for the first MFLO or MFHI. */
3388 for (i = 0; i < MAX_VR4130_NOPS; i++)
91d6fa6a 3389 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
7d8e00cf
RS
3390 {
3391 /* Extract the destination register. */
4c260379 3392 mask = gpr_write_mask (&hist[i]);
7d8e00cf
RS
3393
3394 /* No nops are needed if INSN reads that register. */
4c260379 3395 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
7d8e00cf
RS
3396 return 0;
3397
3398 /* ...or if any of the intervening instructions do. */
3399 for (j = 0; j < i; j++)
4c260379 3400 if (gpr_read_mask (&hist[j]) & mask)
7d8e00cf
RS
3401 return 0;
3402
932d1a1b
RS
3403 if (i >= ignore)
3404 return MAX_VR4130_NOPS - i;
7d8e00cf
RS
3405 }
3406 return 0;
3407}
3408
15be625d
CM
3409#define BASE_REG_EQ(INSN1, INSN2) \
3410 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
3411 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
3412
3413/* Return the minimum alignment for this store instruction. */
3414
3415static int
3416fix_24k_align_to (const struct mips_opcode *mo)
3417{
3418 if (strcmp (mo->name, "sh") == 0)
3419 return 2;
3420
3421 if (strcmp (mo->name, "swc1") == 0
3422 || strcmp (mo->name, "swc2") == 0
3423 || strcmp (mo->name, "sw") == 0
3424 || strcmp (mo->name, "sc") == 0
3425 || strcmp (mo->name, "s.s") == 0)
3426 return 4;
3427
3428 if (strcmp (mo->name, "sdc1") == 0
3429 || strcmp (mo->name, "sdc2") == 0
3430 || strcmp (mo->name, "s.d") == 0)
3431 return 8;
3432
3433 /* sb, swl, swr */
3434 return 1;
3435}
3436
3437struct fix_24k_store_info
3438 {
3439 /* Immediate offset, if any, for this store instruction. */
3440 short off;
3441 /* Alignment required by this store instruction. */
3442 int align_to;
3443 /* True for register offsets. */
3444 int register_offset;
3445 };
3446
3447/* Comparison function used by qsort. */
3448
3449static int
3450fix_24k_sort (const void *a, const void *b)
3451{
3452 const struct fix_24k_store_info *pos1 = a;
3453 const struct fix_24k_store_info *pos2 = b;
3454
3455 return (pos1->off - pos2->off);
3456}
3457
3458/* INSN is a store instruction. Try to record the store information
3459 in STINFO. Return false if the information isn't known. */
3460
3461static bfd_boolean
3462fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
ab9794cf 3463 const struct mips_cl_insn *insn)
15be625d
CM
3464{
3465 /* The instruction must have a known offset. */
3466 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
3467 return FALSE;
3468
3469 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
3470 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
3471 return TRUE;
3472}
3473
932d1a1b
RS
3474/* Return the number of nops that would be needed to work around the 24k
3475 "lost data on stores during refill" errata if instruction INSN
3476 immediately followed the 2 instructions described by HIST.
3477 Ignore hazards that are contained within the first IGNORE
3478 instructions of HIST.
3479
3480 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
3481 for the data cache refills and store data. The following describes
3482 the scenario where the store data could be lost.
3483
3484 * A data cache miss, due to either a load or a store, causing fill
3485 data to be supplied by the memory subsystem
3486 * The first three doublewords of fill data are returned and written
3487 into the cache
3488 * A sequence of four stores occurs in consecutive cycles around the
3489 final doubleword of the fill:
3490 * Store A
3491 * Store B
3492 * Store C
3493 * Zero, One or more instructions
3494 * Store D
3495
3496 The four stores A-D must be to different doublewords of the line that
3497 is being filled. The fourth instruction in the sequence above permits
3498 the fill of the final doubleword to be transferred from the FSB into
3499 the cache. In the sequence above, the stores may be either integer
3500 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
3501 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
3502 different doublewords on the line. If the floating point unit is
3503 running in 1:2 mode, it is not possible to create the sequence above
3504 using only floating point store instructions.
15be625d
CM
3505
3506 In this case, the cache line being filled is incorrectly marked
3507 invalid, thereby losing the data from any store to the line that
3508 occurs between the original miss and the completion of the five
3509 cycle sequence shown above.
3510
932d1a1b 3511 The workarounds are:
15be625d 3512
932d1a1b
RS
3513 * Run the data cache in write-through mode.
3514 * Insert a non-store instruction between
3515 Store A and Store B or Store B and Store C. */
15be625d
CM
3516
3517static int
932d1a1b 3518nops_for_24k (int ignore, const struct mips_cl_insn *hist,
15be625d
CM
3519 const struct mips_cl_insn *insn)
3520{
3521 struct fix_24k_store_info pos[3];
3522 int align, i, base_offset;
3523
932d1a1b
RS
3524 if (ignore >= 2)
3525 return 0;
3526
ab9794cf
RS
3527 /* If the previous instruction wasn't a store, there's nothing to
3528 worry about. */
15be625d
CM
3529 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
3530 return 0;
3531
ab9794cf
RS
3532 /* If the instructions after the previous one are unknown, we have
3533 to assume the worst. */
3534 if (!insn)
15be625d
CM
3535 return 1;
3536
ab9794cf
RS
3537 /* Check whether we are dealing with three consecutive stores. */
3538 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
3539 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
15be625d
CM
3540 return 0;
3541
3542 /* If we don't know the relationship between the store addresses,
3543 assume the worst. */
ab9794cf 3544 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
15be625d
CM
3545 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
3546 return 1;
3547
3548 if (!fix_24k_record_store_info (&pos[0], insn)
3549 || !fix_24k_record_store_info (&pos[1], &hist[0])
3550 || !fix_24k_record_store_info (&pos[2], &hist[1]))
3551 return 1;
3552
3553 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
3554
3555 /* Pick a value of ALIGN and X such that all offsets are adjusted by
3556 X bytes and such that the base register + X is known to be aligned
3557 to align bytes. */
3558
3559 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
3560 align = 8;
3561 else
3562 {
3563 align = pos[0].align_to;
3564 base_offset = pos[0].off;
3565 for (i = 1; i < 3; i++)
3566 if (align < pos[i].align_to)
3567 {
3568 align = pos[i].align_to;
3569 base_offset = pos[i].off;
3570 }
3571 for (i = 0; i < 3; i++)
3572 pos[i].off -= base_offset;
3573 }
3574
3575 pos[0].off &= ~align + 1;
3576 pos[1].off &= ~align + 1;
3577 pos[2].off &= ~align + 1;
3578
3579 /* If any two stores write to the same chunk, they also write to the
3580 same doubleword. The offsets are still sorted at this point. */
3581 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
3582 return 0;
3583
3584 /* A range of at least 9 bytes is needed for the stores to be in
3585 non-overlapping doublewords. */
3586 if (pos[2].off - pos[0].off <= 8)
3587 return 0;
3588
3589 if (pos[2].off - pos[1].off >= 24
3590 || pos[1].off - pos[0].off >= 24
3591 || pos[2].off - pos[0].off >= 32)
3592 return 0;
3593
3594 return 1;
3595}
3596
71400594 3597/* Return the number of nops that would be needed if instruction INSN
91d6fa6a 3598 immediately followed the MAX_NOPS instructions given by HIST,
932d1a1b
RS
3599 where HIST[0] is the most recent instruction. Ignore hazards
3600 between INSN and the first IGNORE instructions in HIST.
3601
3602 If INSN is null, return the worse-case number of nops for any
3603 instruction. */
bdaaa2e1 3604
71400594 3605static int
932d1a1b 3606nops_for_insn (int ignore, const struct mips_cl_insn *hist,
71400594
RS
3607 const struct mips_cl_insn *insn)
3608{
3609 int i, nops, tmp_nops;
bdaaa2e1 3610
71400594 3611 nops = 0;
932d1a1b 3612 for (i = ignore; i < MAX_DELAY_NOPS; i++)
65b02341 3613 {
91d6fa6a 3614 tmp_nops = insns_between (hist + i, insn) - i;
65b02341
RS
3615 if (tmp_nops > nops)
3616 nops = tmp_nops;
3617 }
7d8e00cf 3618
df58fc94 3619 if (mips_fix_vr4130 && !mips_opts.micromips)
7d8e00cf 3620 {
932d1a1b 3621 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
7d8e00cf
RS
3622 if (tmp_nops > nops)
3623 nops = tmp_nops;
3624 }
3625
df58fc94 3626 if (mips_fix_24k && !mips_opts.micromips)
15be625d 3627 {
932d1a1b 3628 tmp_nops = nops_for_24k (ignore, hist, insn);
15be625d
CM
3629 if (tmp_nops > nops)
3630 nops = tmp_nops;
3631 }
3632
71400594
RS
3633 return nops;
3634}
252b5132 3635
71400594 3636/* The variable arguments provide NUM_INSNS extra instructions that
91d6fa6a 3637 might be added to HIST. Return the largest number of nops that
932d1a1b
RS
3638 would be needed after the extended sequence, ignoring hazards
3639 in the first IGNORE instructions. */
252b5132 3640
71400594 3641static int
932d1a1b
RS
3642nops_for_sequence (int num_insns, int ignore,
3643 const struct mips_cl_insn *hist, ...)
71400594
RS
3644{
3645 va_list args;
3646 struct mips_cl_insn buffer[MAX_NOPS];
3647 struct mips_cl_insn *cursor;
3648 int nops;
3649
91d6fa6a 3650 va_start (args, hist);
71400594 3651 cursor = buffer + num_insns;
91d6fa6a 3652 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
71400594
RS
3653 while (cursor > buffer)
3654 *--cursor = *va_arg (args, const struct mips_cl_insn *);
3655
932d1a1b 3656 nops = nops_for_insn (ignore, buffer, NULL);
71400594
RS
3657 va_end (args);
3658 return nops;
3659}
252b5132 3660
71400594
RS
3661/* Like nops_for_insn, but if INSN is a branch, take into account the
3662 worst-case delay for the branch target. */
252b5132 3663
71400594 3664static int
932d1a1b 3665nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
71400594
RS
3666 const struct mips_cl_insn *insn)
3667{
3668 int nops, tmp_nops;
60b63b72 3669
932d1a1b 3670 nops = nops_for_insn (ignore, hist, insn);
11625dd8 3671 if (delayed_branch_p (insn))
71400594 3672 {
932d1a1b 3673 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
14fe068b 3674 hist, insn, get_delay_slot_nop (insn));
71400594
RS
3675 if (tmp_nops > nops)
3676 nops = tmp_nops;
3677 }
11625dd8 3678 else if (compact_branch_p (insn))
71400594 3679 {
932d1a1b 3680 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
71400594
RS
3681 if (tmp_nops > nops)
3682 nops = tmp_nops;
3683 }
3684 return nops;
3685}
3686
c67a084a
NC
3687/* Fix NOP issue: Replace nops by "or at,at,zero". */
3688
3689static void
3690fix_loongson2f_nop (struct mips_cl_insn * ip)
3691{
df58fc94 3692 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
3693 if (strcmp (ip->insn_mo->name, "nop") == 0)
3694 ip->insn_opcode = LOONGSON2F_NOP_INSN;
3695}
3696
3697/* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
3698 jr target pc &= 'hffff_ffff_cfff_ffff. */
3699
3700static void
3701fix_loongson2f_jump (struct mips_cl_insn * ip)
3702{
df58fc94 3703 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
3704 if (strcmp (ip->insn_mo->name, "j") == 0
3705 || strcmp (ip->insn_mo->name, "jr") == 0
3706 || strcmp (ip->insn_mo->name, "jalr") == 0)
3707 {
3708 int sreg;
3709 expressionS ep;
3710
3711 if (! mips_opts.at)
3712 return;
3713
df58fc94 3714 sreg = EXTRACT_OPERAND (0, RS, *ip);
c67a084a
NC
3715 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
3716 return;
3717
3718 ep.X_op = O_constant;
3719 ep.X_add_number = 0xcfff0000;
3720 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
3721 ep.X_add_number = 0xffff;
3722 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
3723 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
3724 }
3725}
3726
3727static void
3728fix_loongson2f (struct mips_cl_insn * ip)
3729{
3730 if (mips_fix_loongson2f_nop)
3731 fix_loongson2f_nop (ip);
3732
3733 if (mips_fix_loongson2f_jump)
3734 fix_loongson2f_jump (ip);
3735}
3736
a4e06468
RS
3737/* IP is a branch that has a delay slot, and we need to fill it
3738 automatically. Return true if we can do that by swapping IP
3739 with the previous instruction. */
3740
3741static bfd_boolean
3742can_swap_branch_p (struct mips_cl_insn *ip)
3743{
2b0c8b40 3744 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
a4e06468
RS
3745 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
3746
3747 /* -O2 and above is required for this optimization. */
3748 if (mips_optimize < 2)
3749 return FALSE;
3750
3751 /* If we have seen .set volatile or .set nomove, don't optimize. */
3752 if (mips_opts.nomove)
3753 return FALSE;
3754
3755 /* We can't swap if the previous instruction's position is fixed. */
3756 if (history[0].fixed_p)
3757 return FALSE;
3758
3759 /* If the previous previous insn was in a .set noreorder, we can't
3760 swap. Actually, the MIPS assembler will swap in this situation.
3761 However, gcc configured -with-gnu-as will generate code like
3762
3763 .set noreorder
3764 lw $4,XXX
3765 .set reorder
3766 INSN
3767 bne $4,$0,foo
3768
3769 in which we can not swap the bne and INSN. If gcc is not configured
3770 -with-gnu-as, it does not output the .set pseudo-ops. */
3771 if (history[1].noreorder_p)
3772 return FALSE;
3773
87333bb7
MR
3774 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
3775 This means that the previous instruction was a 4-byte one anyhow. */
a4e06468
RS
3776 if (mips_opts.mips16 && history[0].fixp[0])
3777 return FALSE;
3778
3779 /* If the branch is itself the target of a branch, we can not swap.
3780 We cheat on this; all we check for is whether there is a label on
3781 this instruction. If there are any branches to anything other than
3782 a label, users must use .set noreorder. */
3783 if (seg_info (now_seg)->label_list)
3784 return FALSE;
3785
3786 /* If the previous instruction is in a variant frag other than this
2309ddf2 3787 branch's one, we cannot do the swap. This does not apply to
9301f9c3
MR
3788 MIPS16 code, which uses variant frags for different purposes. */
3789 if (!mips_opts.mips16
a4e06468
RS
3790 && history[0].frag
3791 && history[0].frag->fr_type == rs_machine_dependent)
3792 return FALSE;
3793
bcd530a7
RS
3794 /* We do not swap with instructions that cannot architecturally
3795 be placed in a branch delay slot, such as SYNC or ERET. We
3796 also refrain from swapping with a trap instruction, since it
3797 complicates trap handlers to have the trap instruction be in
3798 a delay slot. */
a4e06468 3799 prev_pinfo = history[0].insn_mo->pinfo;
bcd530a7 3800 if (prev_pinfo & INSN_NO_DELAY_SLOT)
a4e06468
RS
3801 return FALSE;
3802
3803 /* Check for conflicts between the branch and the instructions
3804 before the candidate delay slot. */
3805 if (nops_for_insn (0, history + 1, ip) > 0)
3806 return FALSE;
3807
3808 /* Check for conflicts between the swapped sequence and the
3809 target of the branch. */
3810 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
3811 return FALSE;
3812
3813 /* If the branch reads a register that the previous
3814 instruction sets, we can not swap. */
3815 gpr_read = gpr_read_mask (ip);
3816 prev_gpr_write = gpr_write_mask (&history[0]);
3817 if (gpr_read & prev_gpr_write)
3818 return FALSE;
3819
3820 /* If the branch writes a register that the previous
3821 instruction sets, we can not swap. */
3822 gpr_write = gpr_write_mask (ip);
3823 if (gpr_write & prev_gpr_write)
3824 return FALSE;
3825
3826 /* If the branch writes a register that the previous
3827 instruction reads, we can not swap. */
3828 prev_gpr_read = gpr_read_mask (&history[0]);
3829 if (gpr_write & prev_gpr_read)
3830 return FALSE;
3831
3832 /* If one instruction sets a condition code and the
3833 other one uses a condition code, we can not swap. */
3834 pinfo = ip->insn_mo->pinfo;
3835 if ((pinfo & INSN_READ_COND_CODE)
3836 && (prev_pinfo & INSN_WRITE_COND_CODE))
3837 return FALSE;
3838 if ((pinfo & INSN_WRITE_COND_CODE)
3839 && (prev_pinfo & INSN_READ_COND_CODE))
3840 return FALSE;
3841
3842 /* If the previous instruction uses the PC, we can not swap. */
2b0c8b40 3843 prev_pinfo2 = history[0].insn_mo->pinfo2;
a4e06468
RS
3844 if (mips_opts.mips16 && (prev_pinfo & MIPS16_INSN_READ_PC))
3845 return FALSE;
2b0c8b40
MR
3846 if (mips_opts.micromips && (prev_pinfo2 & INSN2_READ_PC))
3847 return FALSE;
a4e06468 3848
df58fc94
RS
3849 /* If the previous instruction has an incorrect size for a fixed
3850 branch delay slot in microMIPS mode, we cannot swap. */
2309ddf2
MR
3851 pinfo2 = ip->insn_mo->pinfo2;
3852 if (mips_opts.micromips
3853 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
3854 && insn_length (history) != 2)
3855 return FALSE;
3856 if (mips_opts.micromips
3857 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
3858 && insn_length (history) != 4)
3859 return FALSE;
3860
a4e06468
RS
3861 return TRUE;
3862}
3863
3864/* Decide how we should add IP to the instruction stream. */
3865
3866static enum append_method
3867get_append_method (struct mips_cl_insn *ip)
3868{
3869 unsigned long pinfo;
3870
3871 /* The relaxed version of a macro sequence must be inherently
3872 hazard-free. */
3873 if (mips_relax.sequence == 2)
3874 return APPEND_ADD;
3875
3876 /* We must not dabble with instructions in a ".set norerorder" block. */
3877 if (mips_opts.noreorder)
3878 return APPEND_ADD;
3879
3880 /* Otherwise, it's our responsibility to fill branch delay slots. */
11625dd8 3881 if (delayed_branch_p (ip))
a4e06468 3882 {
11625dd8 3883 if (!branch_likely_p (ip) && can_swap_branch_p (ip))
a4e06468
RS
3884 return APPEND_SWAP;
3885
11625dd8 3886 pinfo = ip->insn_mo->pinfo;
a4e06468
RS
3887 if (mips_opts.mips16
3888 && ISA_SUPPORTS_MIPS16E
a4e06468
RS
3889 && (pinfo & (MIPS16_INSN_READ_X | MIPS16_INSN_READ_31)))
3890 return APPEND_ADD_COMPACT;
3891
3892 return APPEND_ADD_WITH_NOP;
3893 }
3894
a4e06468
RS
3895 return APPEND_ADD;
3896}
3897
ceb94aa5
RS
3898/* IP is a MIPS16 instruction whose opcode we have just changed.
3899 Point IP->insn_mo to the new opcode's definition. */
3900
3901static void
3902find_altered_mips16_opcode (struct mips_cl_insn *ip)
3903{
3904 const struct mips_opcode *mo, *end;
3905
3906 end = &mips16_opcodes[bfd_mips16_num_opcodes];
3907 for (mo = ip->insn_mo; mo < end; mo++)
3908 if ((ip->insn_opcode & mo->mask) == mo->match)
3909 {
3910 ip->insn_mo = mo;
3911 return;
3912 }
3913 abort ();
3914}
3915
df58fc94
RS
3916/* For microMIPS macros, we need to generate a local number label
3917 as the target of branches. */
3918#define MICROMIPS_LABEL_CHAR '\037'
3919static unsigned long micromips_target_label;
3920static char micromips_target_name[32];
3921
3922static char *
3923micromips_label_name (void)
3924{
3925 char *p = micromips_target_name;
3926 char symbol_name_temporary[24];
3927 unsigned long l;
3928 int i;
3929
3930 if (*p)
3931 return p;
3932
3933 i = 0;
3934 l = micromips_target_label;
3935#ifdef LOCAL_LABEL_PREFIX
3936 *p++ = LOCAL_LABEL_PREFIX;
3937#endif
3938 *p++ = 'L';
3939 *p++ = MICROMIPS_LABEL_CHAR;
3940 do
3941 {
3942 symbol_name_temporary[i++] = l % 10 + '0';
3943 l /= 10;
3944 }
3945 while (l != 0);
3946 while (i > 0)
3947 *p++ = symbol_name_temporary[--i];
3948 *p = '\0';
3949
3950 return micromips_target_name;
3951}
3952
3953static void
3954micromips_label_expr (expressionS *label_expr)
3955{
3956 label_expr->X_op = O_symbol;
3957 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
3958 label_expr->X_add_number = 0;
3959}
3960
3961static void
3962micromips_label_inc (void)
3963{
3964 micromips_target_label++;
3965 *micromips_target_name = '\0';
3966}
3967
3968static void
3969micromips_add_label (void)
3970{
3971 symbolS *s;
3972
3973 s = colon (micromips_label_name ());
3974 micromips_label_inc ();
3975#if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
3976 if (IS_ELF)
3977 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
db9b2be4
AM
3978#else
3979 (void) s;
df58fc94
RS
3980#endif
3981}
3982
3983/* If assembling microMIPS code, then return the microMIPS reloc
3984 corresponding to the requested one if any. Otherwise return
3985 the reloc unchanged. */
3986
3987static bfd_reloc_code_real_type
3988micromips_map_reloc (bfd_reloc_code_real_type reloc)
3989{
3990 static const bfd_reloc_code_real_type relocs[][2] =
3991 {
3992 /* Keep sorted incrementally by the left-hand key. */
3993 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
3994 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
3995 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
3996 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
3997 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
3998 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
3999 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
4000 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
4001 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
4002 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
4003 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
4004 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
4005 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
4006 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
4007 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
4008 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
4009 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
4010 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
4011 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
4012 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
4013 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
4014 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
4015 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
4016 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
4017 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
4018 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
4019 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
4020 };
4021 bfd_reloc_code_real_type r;
4022 size_t i;
4023
4024 if (!mips_opts.micromips)
4025 return reloc;
4026 for (i = 0; i < ARRAY_SIZE (relocs); i++)
4027 {
4028 r = relocs[i][0];
4029 if (r > reloc)
4030 return reloc;
4031 if (r == reloc)
4032 return relocs[i][1];
4033 }
4034 return reloc;
4035}
4036
b886a2ab
RS
4037/* Try to resolve relocation RELOC against constant OPERAND at assembly time.
4038 Return true on success, storing the resolved value in RESULT. */
4039
4040static bfd_boolean
4041calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
4042 offsetT *result)
4043{
4044 switch (reloc)
4045 {
4046 case BFD_RELOC_MIPS_HIGHEST:
4047 case BFD_RELOC_MICROMIPS_HIGHEST:
4048 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
4049 return TRUE;
4050
4051 case BFD_RELOC_MIPS_HIGHER:
4052 case BFD_RELOC_MICROMIPS_HIGHER:
4053 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
4054 return TRUE;
4055
4056 case BFD_RELOC_HI16_S:
4057 case BFD_RELOC_MICROMIPS_HI16_S:
4058 case BFD_RELOC_MIPS16_HI16_S:
4059 *result = ((operand + 0x8000) >> 16) & 0xffff;
4060 return TRUE;
4061
4062 case BFD_RELOC_HI16:
4063 case BFD_RELOC_MICROMIPS_HI16:
4064 case BFD_RELOC_MIPS16_HI16:
4065 *result = (operand >> 16) & 0xffff;
4066 return TRUE;
4067
4068 case BFD_RELOC_LO16:
4069 case BFD_RELOC_MICROMIPS_LO16:
4070 case BFD_RELOC_MIPS16_LO16:
4071 *result = operand & 0xffff;
4072 return TRUE;
4073
4074 case BFD_RELOC_UNUSED:
4075 *result = operand;
4076 return TRUE;
4077
4078 default:
4079 return FALSE;
4080 }
4081}
4082
71400594
RS
4083/* Output an instruction. IP is the instruction information.
4084 ADDRESS_EXPR is an operand of the instruction to be used with
df58fc94
RS
4085 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
4086 a macro expansion. */
71400594
RS
4087
4088static void
4089append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
df58fc94 4090 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
71400594 4091{
14fe068b 4092 unsigned long prev_pinfo2, pinfo;
71400594 4093 bfd_boolean relaxed_branch = FALSE;
a4e06468 4094 enum append_method method;
2309ddf2 4095 bfd_boolean relax32;
2b0c8b40 4096 int branch_disp;
71400594 4097
2309ddf2 4098 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
c67a084a
NC
4099 fix_loongson2f (ip);
4100
738f4d98 4101 file_ase_mips16 |= mips_opts.mips16;
df58fc94 4102 file_ase_micromips |= mips_opts.micromips;
738f4d98 4103
df58fc94 4104 prev_pinfo2 = history[0].insn_mo->pinfo2;
71400594 4105 pinfo = ip->insn_mo->pinfo;
df58fc94
RS
4106
4107 if (mips_opts.micromips
4108 && !expansionp
4109 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
4110 && micromips_insn_length (ip->insn_mo) != 2)
4111 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
4112 && micromips_insn_length (ip->insn_mo) != 4)))
4113 as_warn (_("Wrong size instruction in a %u-bit branch delay slot"),
4114 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
71400594 4115
15be625d
CM
4116 if (address_expr == NULL)
4117 ip->complete_p = 1;
b886a2ab
RS
4118 else if (reloc_type[0] <= BFD_RELOC_UNUSED
4119 && reloc_type[1] == BFD_RELOC_UNUSED
4120 && reloc_type[2] == BFD_RELOC_UNUSED
15be625d
CM
4121 && address_expr->X_op == O_constant)
4122 {
15be625d
CM
4123 switch (*reloc_type)
4124 {
15be625d 4125 case BFD_RELOC_MIPS_JMP:
df58fc94
RS
4126 {
4127 int shift;
4128
4129 shift = mips_opts.micromips ? 1 : 2;
4130 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
4131 as_bad (_("jump to misaligned address (0x%lx)"),
4132 (unsigned long) address_expr->X_add_number);
4133 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
4134 & 0x3ffffff);
335574df 4135 ip->complete_p = 1;
df58fc94 4136 }
15be625d
CM
4137 break;
4138
4139 case BFD_RELOC_MIPS16_JMP:
4140 if ((address_expr->X_add_number & 3) != 0)
4141 as_bad (_("jump to misaligned address (0x%lx)"),
4142 (unsigned long) address_expr->X_add_number);
4143 ip->insn_opcode |=
4144 (((address_expr->X_add_number & 0x7c0000) << 3)
4145 | ((address_expr->X_add_number & 0xf800000) >> 7)
4146 | ((address_expr->X_add_number & 0x3fffc) >> 2));
335574df 4147 ip->complete_p = 1;
15be625d
CM
4148 break;
4149
4150 case BFD_RELOC_16_PCREL_S2:
df58fc94
RS
4151 {
4152 int shift;
4153
4154 shift = mips_opts.micromips ? 1 : 2;
4155 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
4156 as_bad (_("branch to misaligned address (0x%lx)"),
4157 (unsigned long) address_expr->X_add_number);
4158 if (!mips_relax_branch)
4159 {
4160 if ((address_expr->X_add_number + (1 << (shift + 15)))
4161 & ~((1 << (shift + 16)) - 1))
4162 as_bad (_("branch address range overflow (0x%lx)"),
4163 (unsigned long) address_expr->X_add_number);
4164 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
4165 & 0xffff);
4166 }
df58fc94 4167 }
15be625d
CM
4168 break;
4169
4170 default:
b886a2ab
RS
4171 {
4172 offsetT value;
4173
4174 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
4175 &value))
4176 {
4177 ip->insn_opcode |= value & 0xffff;
4178 ip->complete_p = 1;
4179 }
4180 }
4181 break;
4182 }
15be625d
CM
4183 }
4184
71400594
RS
4185 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
4186 {
4187 /* There are a lot of optimizations we could do that we don't.
4188 In particular, we do not, in general, reorder instructions.
4189 If you use gcc with optimization, it will reorder
4190 instructions and generally do much more optimization then we
4191 do here; repeating all that work in the assembler would only
4192 benefit hand written assembly code, and does not seem worth
4193 it. */
4194 int nops = (mips_optimize == 0
932d1a1b
RS
4195 ? nops_for_insn (0, history, NULL)
4196 : nops_for_insn_or_target (0, history, ip));
71400594 4197 if (nops > 0)
252b5132
RH
4198 {
4199 fragS *old_frag;
4200 unsigned long old_frag_offset;
4201 int i;
252b5132
RH
4202
4203 old_frag = frag_now;
4204 old_frag_offset = frag_now_fix ();
4205
4206 for (i = 0; i < nops; i++)
14fe068b
RS
4207 add_fixed_insn (NOP_INSN);
4208 insert_into_history (0, nops, NOP_INSN);
252b5132
RH
4209
4210 if (listing)
4211 {
4212 listing_prev_line ();
4213 /* We may be at the start of a variant frag. In case we
4214 are, make sure there is enough space for the frag
4215 after the frags created by listing_prev_line. The
4216 argument to frag_grow here must be at least as large
4217 as the argument to all other calls to frag_grow in
4218 this file. We don't have to worry about being in the
4219 middle of a variant frag, because the variants insert
4220 all needed nop instructions themselves. */
4221 frag_grow (40);
4222 }
4223
462427c4 4224 mips_move_text_labels ();
252b5132
RH
4225
4226#ifndef NO_ECOFF_DEBUGGING
4227 if (ECOFF_DEBUGGING)
4228 ecoff_fix_loc (old_frag, old_frag_offset);
4229#endif
4230 }
71400594
RS
4231 }
4232 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
4233 {
932d1a1b
RS
4234 int nops;
4235
4236 /* Work out how many nops in prev_nop_frag are needed by IP,
4237 ignoring hazards generated by the first prev_nop_frag_since
4238 instructions. */
4239 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
9c2799c2 4240 gas_assert (nops <= prev_nop_frag_holds);
252b5132 4241
71400594
RS
4242 /* Enforce NOPS as a minimum. */
4243 if (nops > prev_nop_frag_required)
4244 prev_nop_frag_required = nops;
252b5132 4245
71400594
RS
4246 if (prev_nop_frag_holds == prev_nop_frag_required)
4247 {
4248 /* Settle for the current number of nops. Update the history
4249 accordingly (for the benefit of any future .set reorder code). */
4250 prev_nop_frag = NULL;
4251 insert_into_history (prev_nop_frag_since,
4252 prev_nop_frag_holds, NOP_INSN);
4253 }
4254 else
4255 {
4256 /* Allow this instruction to replace one of the nops that was
4257 tentatively added to prev_nop_frag. */
df58fc94 4258 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
71400594
RS
4259 prev_nop_frag_holds--;
4260 prev_nop_frag_since++;
252b5132
RH
4261 }
4262 }
4263
a4e06468 4264 method = get_append_method (ip);
2b0c8b40 4265 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
a4e06468 4266
58e2ea4d
MR
4267#ifdef OBJ_ELF
4268 /* The value passed to dwarf2_emit_insn is the distance between
4269 the beginning of the current instruction and the address that
e3a82c8e
MR
4270 should be recorded in the debug tables. This is normally the
4271 current address.
4272
df58fc94
RS
4273 For MIPS16/microMIPS debug info we want to use ISA-encoded
4274 addresses, so we use -1 for an address higher by one than the
4275 current one.
e3a82c8e
MR
4276
4277 If the instruction produced is a branch that we will swap with
4278 the preceding instruction, then we add the displacement by which
4279 the branch will be moved backwards. This is more appropriate
2309ddf2
MR
4280 and for MIPS16/microMIPS code also prevents a debugger from
4281 placing a breakpoint in the middle of the branch (and corrupting
4282 code if software breakpoints are used). */
2b0c8b40 4283 dwarf2_emit_insn ((HAVE_CODE_COMPRESSION ? -1 : 0) + branch_disp);
58e2ea4d
MR
4284#endif
4285
df58fc94
RS
4286 relax32 = (mips_relax_branch
4287 /* Don't try branch relaxation within .set nomacro, or within
4288 .set noat if we use $at for PIC computations. If it turns
4289 out that the branch was out-of-range, we'll get an error. */
4290 && !mips_opts.warn_about_macros
4291 && (mips_opts.at || mips_pic == NO_PIC)
4292 /* Don't relax BPOSGE32/64 as they have no complementing
4293 branches. */
40209cad 4294 && !(ip->insn_mo->membership & (INSN_DSP64 | INSN_DSP)));
df58fc94
RS
4295
4296 if (!HAVE_CODE_COMPRESSION
4297 && address_expr
4298 && relax32
0b25d3e6 4299 && *reloc_type == BFD_RELOC_16_PCREL_S2
11625dd8 4300 && delayed_branch_p (ip))
4a6a3df4 4301 {
895921c9 4302 relaxed_branch = TRUE;
1e915849
RS
4303 add_relaxed_insn (ip, (relaxed_branch_length
4304 (NULL, NULL,
11625dd8
RS
4305 uncond_branch_p (ip) ? -1
4306 : branch_likely_p (ip) ? 1
1e915849
RS
4307 : 0)), 4,
4308 RELAX_BRANCH_ENCODE
66b3e8da 4309 (AT,
11625dd8
RS
4310 uncond_branch_p (ip),
4311 branch_likely_p (ip),
1e915849
RS
4312 pinfo & INSN_WRITE_GPR_31,
4313 0),
4314 address_expr->X_add_symbol,
4315 address_expr->X_add_number);
4a6a3df4
AO
4316 *reloc_type = BFD_RELOC_UNUSED;
4317 }
df58fc94
RS
4318 else if (mips_opts.micromips
4319 && address_expr
4320 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
4321 || *reloc_type > BFD_RELOC_UNUSED)
40209cad
MR
4322 && (delayed_branch_p (ip) || compact_branch_p (ip))
4323 /* Don't try branch relaxation when users specify
4324 16-bit/32-bit instructions. */
4325 && !forced_insn_length)
df58fc94
RS
4326 {
4327 bfd_boolean relax16 = *reloc_type > BFD_RELOC_UNUSED;
4328 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
11625dd8
RS
4329 int uncond = uncond_branch_p (ip) ? -1 : 0;
4330 int compact = compact_branch_p (ip);
df58fc94
RS
4331 int al = pinfo & INSN_WRITE_GPR_31;
4332 int length32;
4333
4334 gas_assert (address_expr != NULL);
4335 gas_assert (!mips_relax.sequence);
4336
2b0c8b40 4337 relaxed_branch = TRUE;
df58fc94
RS
4338 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
4339 add_relaxed_insn (ip, relax32 ? length32 : 4, relax16 ? 2 : 4,
40209cad
MR
4340 RELAX_MICROMIPS_ENCODE (type, AT, uncond, compact, al,
4341 relax32, 0, 0),
df58fc94
RS
4342 address_expr->X_add_symbol,
4343 address_expr->X_add_number);
4344 *reloc_type = BFD_RELOC_UNUSED;
4345 }
4346 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
4347 {
4348 /* We need to set up a variant frag. */
df58fc94 4349 gas_assert (address_expr != NULL);
1e915849
RS
4350 add_relaxed_insn (ip, 4, 0,
4351 RELAX_MIPS16_ENCODE
4352 (*reloc_type - BFD_RELOC_UNUSED,
df58fc94 4353 forced_insn_length == 2, forced_insn_length == 4,
11625dd8 4354 delayed_branch_p (&history[0]),
1e915849
RS
4355 history[0].mips16_absolute_jump_p),
4356 make_expr_symbol (address_expr), 0);
252b5132 4357 }
5c04167a 4358 else if (mips_opts.mips16 && insn_length (ip) == 2)
9497f5ac 4359 {
11625dd8 4360 if (!delayed_branch_p (ip))
b8ee1a6e
DU
4361 /* Make sure there is enough room to swap this instruction with
4362 a following jump instruction. */
4363 frag_grow (6);
1e915849 4364 add_fixed_insn (ip);
252b5132
RH
4365 }
4366 else
4367 {
4368 if (mips_opts.mips16
4369 && mips_opts.noreorder
11625dd8 4370 && delayed_branch_p (&history[0]))
252b5132
RH
4371 as_warn (_("extended instruction in delay slot"));
4372
4d7206a2
RS
4373 if (mips_relax.sequence)
4374 {
4375 /* If we've reached the end of this frag, turn it into a variant
4376 frag and record the information for the instructions we've
4377 written so far. */
4378 if (frag_room () < 4)
4379 relax_close_frag ();
df58fc94 4380 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4d7206a2
RS
4381 }
4382
584892a6 4383 if (mips_relax.sequence != 2)
df58fc94
RS
4384 {
4385 if (mips_macro_warning.first_insn_sizes[0] == 0)
4386 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
4387 mips_macro_warning.sizes[0] += insn_length (ip);
4388 mips_macro_warning.insns[0]++;
4389 }
584892a6 4390 if (mips_relax.sequence != 1)
df58fc94
RS
4391 {
4392 if (mips_macro_warning.first_insn_sizes[1] == 0)
4393 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
4394 mips_macro_warning.sizes[1] += insn_length (ip);
4395 mips_macro_warning.insns[1]++;
4396 }
584892a6 4397
1e915849
RS
4398 if (mips_opts.mips16)
4399 {
4400 ip->fixed_p = 1;
4401 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
4402 }
4403 add_fixed_insn (ip);
252b5132
RH
4404 }
4405
9fe77896 4406 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
252b5132 4407 {
df58fc94 4408 bfd_reloc_code_real_type final_type[3];
2309ddf2 4409 reloc_howto_type *howto0;
9fe77896
RS
4410 reloc_howto_type *howto;
4411 int i;
34ce925e 4412
df58fc94
RS
4413 /* Perform any necessary conversion to microMIPS relocations
4414 and find out how many relocations there actually are. */
4415 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
4416 final_type[i] = micromips_map_reloc (reloc_type[i]);
4417
9fe77896
RS
4418 /* In a compound relocation, it is the final (outermost)
4419 operator that determines the relocated field. */
2309ddf2
MR
4420 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
4421
9fe77896
RS
4422 if (howto == NULL)
4423 {
4424 /* To reproduce this failure try assembling gas/testsuites/
4425 gas/mips/mips16-intermix.s with a mips-ecoff targeted
4426 assembler. */
df58fc94
RS
4427 as_bad (_("Unsupported MIPS relocation number %d"),
4428 final_type[i - 1]);
9fe77896
RS
4429 howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16);
4430 }
2309ddf2
MR
4431
4432 if (i > 1)
4433 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
9fe77896
RS
4434 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
4435 bfd_get_reloc_size (howto),
4436 address_expr,
2309ddf2
MR
4437 howto0 && howto0->pc_relative,
4438 final_type[0]);
9fe77896
RS
4439
4440 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
2309ddf2 4441 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
9fe77896
RS
4442 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
4443
4444 /* These relocations can have an addend that won't fit in
4445 4 octets for 64bit assembly. */
4446 if (HAVE_64BIT_GPRS
4447 && ! howto->partial_inplace
4448 && (reloc_type[0] == BFD_RELOC_16
4449 || reloc_type[0] == BFD_RELOC_32
4450 || reloc_type[0] == BFD_RELOC_MIPS_JMP
4451 || reloc_type[0] == BFD_RELOC_GPREL16
4452 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
4453 || reloc_type[0] == BFD_RELOC_GPREL32
4454 || reloc_type[0] == BFD_RELOC_64
4455 || reloc_type[0] == BFD_RELOC_CTOR
4456 || reloc_type[0] == BFD_RELOC_MIPS_SUB
4457 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
4458 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
4459 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
4460 || reloc_type[0] == BFD_RELOC_MIPS_REL16
4461 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
4462 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
4463 || hi16_reloc_p (reloc_type[0])
4464 || lo16_reloc_p (reloc_type[0])))
4465 ip->fixp[0]->fx_no_overflow = 1;
4466
ddaf2c41
MR
4467 /* These relocations can have an addend that won't fit in 2 octets. */
4468 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
4469 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
4470 ip->fixp[0]->fx_no_overflow = 1;
4471
9fe77896
RS
4472 if (mips_relax.sequence)
4473 {
4474 if (mips_relax.first_fixup == 0)
4475 mips_relax.first_fixup = ip->fixp[0];
4476 }
4477 else if (reloc_needs_lo_p (*reloc_type))
4478 {
4479 struct mips_hi_fixup *hi_fixup;
4480
4481 /* Reuse the last entry if it already has a matching %lo. */
4482 hi_fixup = mips_hi_fixup_list;
4483 if (hi_fixup == 0
4484 || !fixup_has_matching_lo_p (hi_fixup->fixp))
4d7206a2 4485 {
9fe77896
RS
4486 hi_fixup = ((struct mips_hi_fixup *)
4487 xmalloc (sizeof (struct mips_hi_fixup)));
4488 hi_fixup->next = mips_hi_fixup_list;
4489 mips_hi_fixup_list = hi_fixup;
4d7206a2 4490 }
9fe77896
RS
4491 hi_fixup->fixp = ip->fixp[0];
4492 hi_fixup->seg = now_seg;
4493 }
252b5132 4494
9fe77896
RS
4495 /* Add fixups for the second and third relocations, if given.
4496 Note that the ABI allows the second relocation to be
4497 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
4498 moment we only use RSS_UNDEF, but we could add support
4499 for the others if it ever becomes necessary. */
4500 for (i = 1; i < 3; i++)
4501 if (reloc_type[i] != BFD_RELOC_UNUSED)
4502 {
4503 ip->fixp[i] = fix_new (ip->frag, ip->where,
4504 ip->fixp[0]->fx_size, NULL, 0,
df58fc94 4505 FALSE, final_type[i]);
f6688943 4506
9fe77896
RS
4507 /* Use fx_tcbit to mark compound relocs. */
4508 ip->fixp[0]->fx_tcbit = 1;
4509 ip->fixp[i]->fx_tcbit = 1;
4510 }
252b5132 4511 }
1e915849 4512 install_insn (ip);
252b5132
RH
4513
4514 /* Update the register mask information. */
4c260379
RS
4515 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
4516 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
252b5132 4517
a4e06468 4518 switch (method)
252b5132 4519 {
a4e06468
RS
4520 case APPEND_ADD:
4521 insert_into_history (0, 1, ip);
4522 break;
4523
4524 case APPEND_ADD_WITH_NOP:
14fe068b
RS
4525 {
4526 struct mips_cl_insn *nop;
4527
4528 insert_into_history (0, 1, ip);
4529 nop = get_delay_slot_nop (ip);
4530 add_fixed_insn (nop);
4531 insert_into_history (0, 1, nop);
4532 if (mips_relax.sequence)
4533 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
4534 }
a4e06468
RS
4535 break;
4536
4537 case APPEND_ADD_COMPACT:
4538 /* Convert MIPS16 jr/jalr into a "compact" jump. */
4539 gas_assert (mips_opts.mips16);
4540 ip->insn_opcode |= 0x0080;
4541 find_altered_mips16_opcode (ip);
4542 install_insn (ip);
4543 insert_into_history (0, 1, ip);
4544 break;
4545
4546 case APPEND_SWAP:
4547 {
4548 struct mips_cl_insn delay = history[0];
4549 if (mips_opts.mips16)
4550 {
4551 know (delay.frag == ip->frag);
4552 move_insn (ip, delay.frag, delay.where);
4553 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
4554 }
464ab0e5 4555 else if (relaxed_branch || delay.frag != ip->frag)
a4e06468
RS
4556 {
4557 /* Add the delay slot instruction to the end of the
4558 current frag and shrink the fixed part of the
4559 original frag. If the branch occupies the tail of
4560 the latter, move it backwards to cover the gap. */
2b0c8b40 4561 delay.frag->fr_fix -= branch_disp;
a4e06468 4562 if (delay.frag == ip->frag)
2b0c8b40 4563 move_insn (ip, ip->frag, ip->where - branch_disp);
a4e06468
RS
4564 add_fixed_insn (&delay);
4565 }
4566 else
4567 {
2b0c8b40
MR
4568 move_insn (&delay, ip->frag,
4569 ip->where - branch_disp + insn_length (ip));
a4e06468
RS
4570 move_insn (ip, history[0].frag, history[0].where);
4571 }
4572 history[0] = *ip;
4573 delay.fixed_p = 1;
4574 insert_into_history (0, 1, &delay);
4575 }
4576 break;
252b5132
RH
4577 }
4578
13408f1e 4579 /* If we have just completed an unconditional branch, clear the history. */
11625dd8
RS
4580 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
4581 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
13408f1e
RS
4582 mips_no_prev_insn ();
4583
df58fc94
RS
4584 /* We need to emit a label at the end of branch-likely macros. */
4585 if (emit_branch_likely_macro)
4586 {
4587 emit_branch_likely_macro = FALSE;
4588 micromips_add_label ();
4589 }
4590
252b5132
RH
4591 /* We just output an insn, so the next one doesn't have a label. */
4592 mips_clear_insn_labels ();
252b5132
RH
4593}
4594
7d10b47d 4595/* Forget that there was any previous instruction or label. */
252b5132
RH
4596
4597static void
7d10b47d 4598mips_no_prev_insn (void)
252b5132 4599{
7d10b47d
RS
4600 prev_nop_frag = NULL;
4601 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
4602 mips_clear_insn_labels ();
4603}
4604
7d10b47d
RS
4605/* This function must be called before we emit something other than
4606 instructions. It is like mips_no_prev_insn except that it inserts
4607 any NOPS that might be needed by previous instructions. */
252b5132 4608
7d10b47d
RS
4609void
4610mips_emit_delays (void)
252b5132
RH
4611{
4612 if (! mips_opts.noreorder)
4613 {
932d1a1b 4614 int nops = nops_for_insn (0, history, NULL);
252b5132
RH
4615 if (nops > 0)
4616 {
7d10b47d
RS
4617 while (nops-- > 0)
4618 add_fixed_insn (NOP_INSN);
462427c4 4619 mips_move_text_labels ();
7d10b47d
RS
4620 }
4621 }
4622 mips_no_prev_insn ();
4623}
4624
4625/* Start a (possibly nested) noreorder block. */
4626
4627static void
4628start_noreorder (void)
4629{
4630 if (mips_opts.noreorder == 0)
4631 {
4632 unsigned int i;
4633 int nops;
4634
4635 /* None of the instructions before the .set noreorder can be moved. */
4636 for (i = 0; i < ARRAY_SIZE (history); i++)
4637 history[i].fixed_p = 1;
4638
4639 /* Insert any nops that might be needed between the .set noreorder
4640 block and the previous instructions. We will later remove any
4641 nops that turn out not to be needed. */
932d1a1b 4642 nops = nops_for_insn (0, history, NULL);
7d10b47d
RS
4643 if (nops > 0)
4644 {
4645 if (mips_optimize != 0)
252b5132
RH
4646 {
4647 /* Record the frag which holds the nop instructions, so
4648 that we can remove them if we don't need them. */
df58fc94 4649 frag_grow (nops * NOP_INSN_SIZE);
252b5132
RH
4650 prev_nop_frag = frag_now;
4651 prev_nop_frag_holds = nops;
4652 prev_nop_frag_required = 0;
4653 prev_nop_frag_since = 0;
4654 }
4655
4656 for (; nops > 0; --nops)
1e915849 4657 add_fixed_insn (NOP_INSN);
252b5132 4658
7d10b47d
RS
4659 /* Move on to a new frag, so that it is safe to simply
4660 decrease the size of prev_nop_frag. */
4661 frag_wane (frag_now);
4662 frag_new (0);
462427c4 4663 mips_move_text_labels ();
252b5132 4664 }
df58fc94 4665 mips_mark_labels ();
7d10b47d 4666 mips_clear_insn_labels ();
252b5132 4667 }
7d10b47d
RS
4668 mips_opts.noreorder++;
4669 mips_any_noreorder = 1;
4670}
252b5132 4671
7d10b47d 4672/* End a nested noreorder block. */
252b5132 4673
7d10b47d
RS
4674static void
4675end_noreorder (void)
4676{
4677 mips_opts.noreorder--;
4678 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
4679 {
4680 /* Commit to inserting prev_nop_frag_required nops and go back to
4681 handling nop insertion the .set reorder way. */
4682 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
df58fc94 4683 * NOP_INSN_SIZE);
7d10b47d
RS
4684 insert_into_history (prev_nop_frag_since,
4685 prev_nop_frag_required, NOP_INSN);
4686 prev_nop_frag = NULL;
4687 }
252b5132
RH
4688}
4689
584892a6
RS
4690/* Set up global variables for the start of a new macro. */
4691
4692static void
4693macro_start (void)
4694{
4695 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
df58fc94
RS
4696 memset (&mips_macro_warning.first_insn_sizes, 0,
4697 sizeof (mips_macro_warning.first_insn_sizes));
4698 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
584892a6 4699 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
11625dd8 4700 && delayed_branch_p (&history[0]));
df58fc94
RS
4701 switch (history[0].insn_mo->pinfo2
4702 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
4703 {
4704 case INSN2_BRANCH_DELAY_32BIT:
4705 mips_macro_warning.delay_slot_length = 4;
4706 break;
4707 case INSN2_BRANCH_DELAY_16BIT:
4708 mips_macro_warning.delay_slot_length = 2;
4709 break;
4710 default:
4711 mips_macro_warning.delay_slot_length = 0;
4712 break;
4713 }
4714 mips_macro_warning.first_frag = NULL;
584892a6
RS
4715}
4716
df58fc94
RS
4717/* Given that a macro is longer than one instruction or of the wrong size,
4718 return the appropriate warning for it. Return null if no warning is
4719 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
4720 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
4721 and RELAX_NOMACRO. */
584892a6
RS
4722
4723static const char *
4724macro_warning (relax_substateT subtype)
4725{
4726 if (subtype & RELAX_DELAY_SLOT)
4727 return _("Macro instruction expanded into multiple instructions"
4728 " in a branch delay slot");
4729 else if (subtype & RELAX_NOMACRO)
4730 return _("Macro instruction expanded into multiple instructions");
df58fc94
RS
4731 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
4732 | RELAX_DELAY_SLOT_SIZE_SECOND))
4733 return ((subtype & RELAX_DELAY_SLOT_16BIT)
4734 ? _("Macro instruction expanded into a wrong size instruction"
4735 " in a 16-bit branch delay slot")
4736 : _("Macro instruction expanded into a wrong size instruction"
4737 " in a 32-bit branch delay slot"));
584892a6
RS
4738 else
4739 return 0;
4740}
4741
4742/* Finish up a macro. Emit warnings as appropriate. */
4743
4744static void
4745macro_end (void)
4746{
df58fc94
RS
4747 /* Relaxation warning flags. */
4748 relax_substateT subtype = 0;
4749
4750 /* Check delay slot size requirements. */
4751 if (mips_macro_warning.delay_slot_length == 2)
4752 subtype |= RELAX_DELAY_SLOT_16BIT;
4753 if (mips_macro_warning.delay_slot_length != 0)
584892a6 4754 {
df58fc94
RS
4755 if (mips_macro_warning.delay_slot_length
4756 != mips_macro_warning.first_insn_sizes[0])
4757 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
4758 if (mips_macro_warning.delay_slot_length
4759 != mips_macro_warning.first_insn_sizes[1])
4760 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
4761 }
584892a6 4762
df58fc94
RS
4763 /* Check instruction count requirements. */
4764 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
4765 {
4766 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
584892a6
RS
4767 subtype |= RELAX_SECOND_LONGER;
4768 if (mips_opts.warn_about_macros)
4769 subtype |= RELAX_NOMACRO;
4770 if (mips_macro_warning.delay_slot_p)
4771 subtype |= RELAX_DELAY_SLOT;
df58fc94 4772 }
584892a6 4773
df58fc94
RS
4774 /* If both alternatives fail to fill a delay slot correctly,
4775 emit the warning now. */
4776 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
4777 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
4778 {
4779 relax_substateT s;
4780 const char *msg;
4781
4782 s = subtype & (RELAX_DELAY_SLOT_16BIT
4783 | RELAX_DELAY_SLOT_SIZE_FIRST
4784 | RELAX_DELAY_SLOT_SIZE_SECOND);
4785 msg = macro_warning (s);
4786 if (msg != NULL)
4787 as_warn ("%s", msg);
4788 subtype &= ~s;
4789 }
4790
4791 /* If both implementations are longer than 1 instruction, then emit the
4792 warning now. */
4793 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
4794 {
4795 relax_substateT s;
4796 const char *msg;
4797
4798 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
4799 msg = macro_warning (s);
4800 if (msg != NULL)
4801 as_warn ("%s", msg);
4802 subtype &= ~s;
584892a6 4803 }
df58fc94
RS
4804
4805 /* If any flags still set, then one implementation might need a warning
4806 and the other either will need one of a different kind or none at all.
4807 Pass any remaining flags over to relaxation. */
4808 if (mips_macro_warning.first_frag != NULL)
4809 mips_macro_warning.first_frag->fr_subtype |= subtype;
584892a6
RS
4810}
4811
df58fc94
RS
4812/* Instruction operand formats used in macros that vary between
4813 standard MIPS and microMIPS code. */
4814
4815static const char * const brk_fmt[2] = { "c", "mF" };
4816static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
4817static const char * const jalr_fmt[2] = { "d,s", "t,s" };
4818static const char * const lui_fmt[2] = { "t,u", "s,u" };
4819static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
4820static const char * const mfhl_fmt[2] = { "d", "mj" };
4821static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
4822static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
4823
4824#define BRK_FMT (brk_fmt[mips_opts.micromips])
4825#define COP12_FMT (cop12_fmt[mips_opts.micromips])
4826#define JALR_FMT (jalr_fmt[mips_opts.micromips])
4827#define LUI_FMT (lui_fmt[mips_opts.micromips])
4828#define MEM12_FMT (mem12_fmt[mips_opts.micromips])
4829#define MFHL_FMT (mfhl_fmt[mips_opts.micromips])
4830#define SHFT_FMT (shft_fmt[mips_opts.micromips])
4831#define TRAP_FMT (trap_fmt[mips_opts.micromips])
4832
6e1304d8
RS
4833/* Read a macro's relocation codes from *ARGS and store them in *R.
4834 The first argument in *ARGS will be either the code for a single
4835 relocation or -1 followed by the three codes that make up a
4836 composite relocation. */
4837
4838static void
4839macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
4840{
4841 int i, next;
4842
4843 next = va_arg (*args, int);
4844 if (next >= 0)
4845 r[0] = (bfd_reloc_code_real_type) next;
4846 else
4847 for (i = 0; i < 3; i++)
4848 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
4849}
4850
252b5132
RH
4851/* Build an instruction created by a macro expansion. This is passed
4852 a pointer to the count of instructions created so far, an
4853 expression, the name of the instruction to build, an operand format
4854 string, and corresponding arguments. */
4855
252b5132 4856static void
67c0d1eb 4857macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 4858{
df58fc94 4859 const struct mips_opcode *mo = NULL;
f6688943 4860 bfd_reloc_code_real_type r[3];
df58fc94
RS
4861 const struct mips_opcode *amo;
4862 struct hash_control *hash;
4863 struct mips_cl_insn insn;
252b5132 4864 va_list args;
252b5132 4865
252b5132 4866 va_start (args, fmt);
252b5132 4867
252b5132
RH
4868 if (mips_opts.mips16)
4869 {
03ea81db 4870 mips16_macro_build (ep, name, fmt, &args);
252b5132
RH
4871 va_end (args);
4872 return;
4873 }
4874
f6688943
TS
4875 r[0] = BFD_RELOC_UNUSED;
4876 r[1] = BFD_RELOC_UNUSED;
4877 r[2] = BFD_RELOC_UNUSED;
df58fc94
RS
4878 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
4879 amo = (struct mips_opcode *) hash_find (hash, name);
4880 gas_assert (amo);
4881 gas_assert (strcmp (name, amo->name) == 0);
1e915849 4882
df58fc94 4883 do
8b082fb1
TS
4884 {
4885 /* Search until we get a match for NAME. It is assumed here that
df58fc94
RS
4886 macros will never generate MDMX, MIPS-3D, or MT instructions.
4887 We try to match an instruction that fulfils the branch delay
4888 slot instruction length requirement (if any) of the previous
4889 instruction. While doing this we record the first instruction
4890 seen that matches all the other conditions and use it anyway
4891 if the requirement cannot be met; we will issue an appropriate
4892 warning later on. */
4893 if (strcmp (fmt, amo->args) == 0
4894 && amo->pinfo != INSN_MACRO
4895 && is_opcode_valid (amo)
4896 && is_size_valid (amo))
4897 {
4898 if (is_delay_slot_valid (amo))
4899 {
4900 mo = amo;
4901 break;
4902 }
4903 else if (!mo)
4904 mo = amo;
4905 }
8b082fb1 4906
df58fc94
RS
4907 ++amo;
4908 gas_assert (amo->name);
252b5132 4909 }
df58fc94 4910 while (strcmp (name, amo->name) == 0);
252b5132 4911
df58fc94 4912 gas_assert (mo);
1e915849 4913 create_insn (&insn, mo);
252b5132
RH
4914 for (;;)
4915 {
4916 switch (*fmt++)
4917 {
4918 case '\0':
4919 break;
4920
4921 case ',':
4922 case '(':
4923 case ')':
4924 continue;
4925
5f74bc13
CD
4926 case '+':
4927 switch (*fmt++)
4928 {
4929 case 'A':
4930 case 'E':
df58fc94
RS
4931 INSERT_OPERAND (mips_opts.micromips,
4932 EXTLSB, insn, va_arg (args, int));
5f74bc13
CD
4933 continue;
4934
4935 case 'B':
4936 case 'F':
4937 /* Note that in the macro case, these arguments are already
4938 in MSB form. (When handling the instruction in the
4939 non-macro case, these arguments are sizes from which
4940 MSB values must be calculated.) */
df58fc94
RS
4941 INSERT_OPERAND (mips_opts.micromips,
4942 INSMSB, insn, va_arg (args, int));
5f74bc13
CD
4943 continue;
4944
4945 case 'C':
4946 case 'G':
4947 case 'H':
4948 /* Note that in the macro case, these arguments are already
4949 in MSBD form. (When handling the instruction in the
4950 non-macro case, these arguments are sizes from which
4951 MSBD values must be calculated.) */
df58fc94
RS
4952 INSERT_OPERAND (mips_opts.micromips,
4953 EXTMSBD, insn, va_arg (args, int));
5f74bc13
CD
4954 continue;
4955
dd3cbb7e 4956 case 'Q':
df58fc94
RS
4957 gas_assert (!mips_opts.micromips);
4958 INSERT_OPERAND (0, SEQI, insn, va_arg (args, int));
dd3cbb7e
NC
4959 continue;
4960
5f74bc13
CD
4961 default:
4962 internalError ();
4963 }
4964 continue;
4965
8b082fb1 4966 case '2':
03f66e8a 4967 INSERT_OPERAND (mips_opts.micromips, BP, insn, va_arg (args, int));
8b082fb1
TS
4968 continue;
4969
df58fc94
RS
4970 case 'n':
4971 gas_assert (mips_opts.micromips);
252b5132
RH
4972 case 't':
4973 case 'w':
4974 case 'E':
df58fc94 4975 INSERT_OPERAND (mips_opts.micromips, RT, insn, va_arg (args, int));
252b5132
RH
4976 continue;
4977
4978 case 'c':
df58fc94
RS
4979 gas_assert (!mips_opts.micromips);
4980 INSERT_OPERAND (0, CODE, insn, va_arg (args, int));
38487616
TS
4981 continue;
4982
252b5132 4983 case 'W':
df58fc94
RS
4984 gas_assert (!mips_opts.micromips);
4985 case 'T':
4986 INSERT_OPERAND (mips_opts.micromips, FT, insn, va_arg (args, int));
252b5132
RH
4987 continue;
4988
252b5132 4989 case 'G':
df58fc94
RS
4990 if (mips_opts.micromips)
4991 INSERT_OPERAND (1, RS, insn, va_arg (args, int));
4992 else
4993 INSERT_OPERAND (0, RD, insn, va_arg (args, int));
4994 continue;
4995
af7ee8bf 4996 case 'K':
df58fc94
RS
4997 gas_assert (!mips_opts.micromips);
4998 case 'd':
4999 INSERT_OPERAND (mips_opts.micromips, RD, insn, va_arg (args, int));
252b5132
RH
5000 continue;
5001
4372b673 5002 case 'U':
df58fc94 5003 gas_assert (!mips_opts.micromips);
4372b673
NC
5004 {
5005 int tmp = va_arg (args, int);
5006
df58fc94
RS
5007 INSERT_OPERAND (0, RT, insn, tmp);
5008 INSERT_OPERAND (0, RD, insn, tmp);
4372b673 5009 }
df58fc94 5010 continue;
4372b673 5011
252b5132
RH
5012 case 'V':
5013 case 'S':
df58fc94
RS
5014 gas_assert (!mips_opts.micromips);
5015 INSERT_OPERAND (0, FS, insn, va_arg (args, int));
252b5132
RH
5016 continue;
5017
5018 case 'z':
5019 continue;
5020
5021 case '<':
df58fc94
RS
5022 INSERT_OPERAND (mips_opts.micromips,
5023 SHAMT, insn, va_arg (args, int));
252b5132
RH
5024 continue;
5025
5026 case 'D':
df58fc94
RS
5027 gas_assert (!mips_opts.micromips);
5028 INSERT_OPERAND (0, FD, insn, va_arg (args, int));
252b5132
RH
5029 continue;
5030
5031 case 'B':
df58fc94
RS
5032 gas_assert (!mips_opts.micromips);
5033 INSERT_OPERAND (0, CODE20, insn, va_arg (args, int));
252b5132
RH
5034 continue;
5035
4372b673 5036 case 'J':
df58fc94
RS
5037 gas_assert (!mips_opts.micromips);
5038 INSERT_OPERAND (0, CODE19, insn, va_arg (args, int));
4372b673
NC
5039 continue;
5040
252b5132 5041 case 'q':
df58fc94
RS
5042 gas_assert (!mips_opts.micromips);
5043 INSERT_OPERAND (0, CODE2, insn, va_arg (args, int));
252b5132
RH
5044 continue;
5045
5046 case 'b':
5047 case 's':
5048 case 'r':
5049 case 'v':
df58fc94 5050 INSERT_OPERAND (mips_opts.micromips, RS, insn, va_arg (args, int));
252b5132
RH
5051 continue;
5052
5053 case 'i':
5054 case 'j':
6e1304d8 5055 macro_read_relocs (&args, r);
9c2799c2 5056 gas_assert (*r == BFD_RELOC_GPREL16
e391c024
RS
5057 || *r == BFD_RELOC_MIPS_HIGHER
5058 || *r == BFD_RELOC_HI16_S
5059 || *r == BFD_RELOC_LO16
5060 || *r == BFD_RELOC_MIPS_GOT_OFST);
5061 continue;
5062
5063 case 'o':
5064 macro_read_relocs (&args, r);
252b5132
RH
5065 continue;
5066
5067 case 'u':
6e1304d8 5068 macro_read_relocs (&args, r);
9c2799c2 5069 gas_assert (ep != NULL
90ecf173
MR
5070 && (ep->X_op == O_constant
5071 || (ep->X_op == O_symbol
5072 && (*r == BFD_RELOC_MIPS_HIGHEST
5073 || *r == BFD_RELOC_HI16_S
5074 || *r == BFD_RELOC_HI16
5075 || *r == BFD_RELOC_GPREL16
5076 || *r == BFD_RELOC_MIPS_GOT_HI16
5077 || *r == BFD_RELOC_MIPS_CALL_HI16))));
252b5132
RH
5078 continue;
5079
5080 case 'p':
9c2799c2 5081 gas_assert (ep != NULL);
bad36eac 5082
252b5132
RH
5083 /*
5084 * This allows macro() to pass an immediate expression for
5085 * creating short branches without creating a symbol.
bad36eac
DJ
5086 *
5087 * We don't allow branch relaxation for these branches, as
5088 * they should only appear in ".set nomacro" anyway.
252b5132
RH
5089 */
5090 if (ep->X_op == O_constant)
5091 {
df58fc94
RS
5092 /* For microMIPS we always use relocations for branches.
5093 So we should not resolve immediate values. */
5094 gas_assert (!mips_opts.micromips);
5095
bad36eac
DJ
5096 if ((ep->X_add_number & 3) != 0)
5097 as_bad (_("branch to misaligned address (0x%lx)"),
5098 (unsigned long) ep->X_add_number);
5099 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
5100 as_bad (_("branch address range overflow (0x%lx)"),
5101 (unsigned long) ep->X_add_number);
252b5132
RH
5102 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
5103 ep = NULL;
5104 }
5105 else
0b25d3e6 5106 *r = BFD_RELOC_16_PCREL_S2;
252b5132
RH
5107 continue;
5108
5109 case 'a':
9c2799c2 5110 gas_assert (ep != NULL);
f6688943 5111 *r = BFD_RELOC_MIPS_JMP;
252b5132
RH
5112 continue;
5113
5114 case 'C':
df58fc94
RS
5115 gas_assert (!mips_opts.micromips);
5116 INSERT_OPERAND (0, COPZ, insn, va_arg (args, unsigned long));
252b5132
RH
5117 continue;
5118
d43b4baf 5119 case 'k':
df58fc94
RS
5120 INSERT_OPERAND (mips_opts.micromips,
5121 CACHE, insn, va_arg (args, unsigned long));
5122 continue;
5123
5124 case '|':
5125 gas_assert (mips_opts.micromips);
5126 INSERT_OPERAND (1, TRAP, insn, va_arg (args, int));
5127 continue;
5128
5129 case '.':
5130 gas_assert (mips_opts.micromips);
5131 INSERT_OPERAND (1, OFFSET10, insn, va_arg (args, int));
5132 continue;
5133
dec0624d
MR
5134 case '\\':
5135 INSERT_OPERAND (mips_opts.micromips,
5136 3BITPOS, insn, va_arg (args, unsigned int));
5137 continue;
5138
df58fc94 5139 case '~':
dec0624d
MR
5140 INSERT_OPERAND (mips_opts.micromips,
5141 OFFSET12, insn, va_arg (args, unsigned long));
df58fc94
RS
5142 continue;
5143
5144 case 'N':
5145 gas_assert (mips_opts.micromips);
5146 INSERT_OPERAND (1, BCC, insn, va_arg (args, int));
5147 continue;
5148
5149 case 'm': /* Opcode extension character. */
5150 gas_assert (mips_opts.micromips);
5151 switch (*fmt++)
5152 {
5153 case 'j':
5154 INSERT_OPERAND (1, MJ, insn, va_arg (args, int));
5155 break;
5156
5157 case 'p':
5158 INSERT_OPERAND (1, MP, insn, va_arg (args, int));
5159 break;
5160
5161 case 'F':
5162 INSERT_OPERAND (1, IMMF, insn, va_arg (args, int));
5163 break;
5164
5165 default:
5166 internalError ();
5167 }
d43b4baf
TS
5168 continue;
5169
252b5132
RH
5170 default:
5171 internalError ();
5172 }
5173 break;
5174 }
5175 va_end (args);
9c2799c2 5176 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 5177
df58fc94 5178 append_insn (&insn, ep, r, TRUE);
252b5132
RH
5179}
5180
5181static void
67c0d1eb 5182mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
03ea81db 5183 va_list *args)
252b5132 5184{
1e915849 5185 struct mips_opcode *mo;
252b5132 5186 struct mips_cl_insn insn;
f6688943
TS
5187 bfd_reloc_code_real_type r[3]
5188 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 5189
1e915849 5190 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
9c2799c2
NC
5191 gas_assert (mo);
5192 gas_assert (strcmp (name, mo->name) == 0);
252b5132 5193
1e915849 5194 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 5195 {
1e915849 5196 ++mo;
9c2799c2
NC
5197 gas_assert (mo->name);
5198 gas_assert (strcmp (name, mo->name) == 0);
252b5132
RH
5199 }
5200
1e915849 5201 create_insn (&insn, mo);
252b5132
RH
5202 for (;;)
5203 {
5204 int c;
5205
5206 c = *fmt++;
5207 switch (c)
5208 {
5209 case '\0':
5210 break;
5211
5212 case ',':
5213 case '(':
5214 case ')':
5215 continue;
5216
5217 case 'y':
5218 case 'w':
03ea81db 5219 MIPS16_INSERT_OPERAND (RY, insn, va_arg (*args, int));
252b5132
RH
5220 continue;
5221
5222 case 'x':
5223 case 'v':
03ea81db 5224 MIPS16_INSERT_OPERAND (RX, insn, va_arg (*args, int));
252b5132
RH
5225 continue;
5226
5227 case 'z':
03ea81db 5228 MIPS16_INSERT_OPERAND (RZ, insn, va_arg (*args, int));
252b5132
RH
5229 continue;
5230
5231 case 'Z':
03ea81db 5232 MIPS16_INSERT_OPERAND (MOVE32Z, insn, va_arg (*args, int));
252b5132
RH
5233 continue;
5234
5235 case '0':
5236 case 'S':
5237 case 'P':
5238 case 'R':
5239 continue;
5240
5241 case 'X':
03ea81db 5242 MIPS16_INSERT_OPERAND (REGR32, insn, va_arg (*args, int));
252b5132
RH
5243 continue;
5244
5245 case 'Y':
5246 {
5247 int regno;
5248
03ea81db 5249 regno = va_arg (*args, int);
252b5132 5250 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
a9e24354 5251 MIPS16_INSERT_OPERAND (REG32R, insn, regno);
252b5132
RH
5252 }
5253 continue;
5254
5255 case '<':
5256 case '>':
5257 case '4':
5258 case '5':
5259 case 'H':
5260 case 'W':
5261 case 'D':
5262 case 'j':
5263 case '8':
5264 case 'V':
5265 case 'C':
5266 case 'U':
5267 case 'k':
5268 case 'K':
5269 case 'p':
5270 case 'q':
5271 {
b886a2ab
RS
5272 offsetT value;
5273
9c2799c2 5274 gas_assert (ep != NULL);
252b5132
RH
5275
5276 if (ep->X_op != O_constant)
874e8986 5277 *r = (int) BFD_RELOC_UNUSED + c;
b886a2ab 5278 else if (calculate_reloc (*r, ep->X_add_number, &value))
252b5132 5279 {
b886a2ab 5280 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
252b5132 5281 ep = NULL;
f6688943 5282 *r = BFD_RELOC_UNUSED;
252b5132
RH
5283 }
5284 }
5285 continue;
5286
5287 case '6':
03ea81db 5288 MIPS16_INSERT_OPERAND (IMM6, insn, va_arg (*args, int));
252b5132
RH
5289 continue;
5290 }
5291
5292 break;
5293 }
5294
9c2799c2 5295 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 5296
df58fc94 5297 append_insn (&insn, ep, r, TRUE);
252b5132
RH
5298}
5299
2051e8c4
MR
5300/*
5301 * Sign-extend 32-bit mode constants that have bit 31 set and all
5302 * higher bits unset.
5303 */
9f872bbe 5304static void
2051e8c4
MR
5305normalize_constant_expr (expressionS *ex)
5306{
9ee2a2d4 5307 if (ex->X_op == O_constant
2051e8c4
MR
5308 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
5309 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
5310 - 0x80000000);
5311}
5312
5313/*
5314 * Sign-extend 32-bit mode address offsets that have bit 31 set and
5315 * all higher bits unset.
5316 */
5317static void
5318normalize_address_expr (expressionS *ex)
5319{
5320 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
5321 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
5322 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
5323 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
5324 - 0x80000000);
5325}
5326
438c16b8
TS
5327/*
5328 * Generate a "jalr" instruction with a relocation hint to the called
5329 * function. This occurs in NewABI PIC code.
5330 */
5331static void
df58fc94 5332macro_build_jalr (expressionS *ep, int cprestore)
438c16b8 5333{
df58fc94
RS
5334 static const bfd_reloc_code_real_type jalr_relocs[2]
5335 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
5336 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
5337 const char *jalr;
685736be 5338 char *f = NULL;
b34976b6 5339
1180b5a4 5340 if (MIPS_JALR_HINT_P (ep))
f21f8242 5341 {
cc3d92a5 5342 frag_grow (8);
f21f8242
AO
5343 f = frag_more (0);
5344 }
2906b037 5345 if (mips_opts.micromips)
df58fc94
RS
5346 {
5347 jalr = mips_opts.noreorder && !cprestore ? "jalr" : "jalrs";
e64af278
MR
5348 if (MIPS_JALR_HINT_P (ep)
5349 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
5350 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
5351 else
5352 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
5353 }
2906b037
MR
5354 else
5355 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
1180b5a4 5356 if (MIPS_JALR_HINT_P (ep))
df58fc94 5357 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
438c16b8
TS
5358}
5359
252b5132
RH
5360/*
5361 * Generate a "lui" instruction.
5362 */
5363static void
67c0d1eb 5364macro_build_lui (expressionS *ep, int regnum)
252b5132 5365{
9c2799c2 5366 gas_assert (! mips_opts.mips16);
252b5132 5367
df58fc94 5368 if (ep->X_op != O_constant)
252b5132 5369 {
9c2799c2 5370 gas_assert (ep->X_op == O_symbol);
bbe506e8
TS
5371 /* _gp_disp is a special case, used from s_cpload.
5372 __gnu_local_gp is used if mips_no_shared. */
9c2799c2 5373 gas_assert (mips_pic == NO_PIC
78e1bb40 5374 || (! HAVE_NEWABI
aa6975fb
ILT
5375 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
5376 || (! mips_in_shared
bbe506e8
TS
5377 && strcmp (S_GET_NAME (ep->X_add_symbol),
5378 "__gnu_local_gp") == 0));
252b5132
RH
5379 }
5380
df58fc94 5381 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
252b5132
RH
5382}
5383
885add95
CD
5384/* Generate a sequence of instructions to do a load or store from a constant
5385 offset off of a base register (breg) into/from a target register (treg),
5386 using AT if necessary. */
5387static void
67c0d1eb
RS
5388macro_build_ldst_constoffset (expressionS *ep, const char *op,
5389 int treg, int breg, int dbl)
885add95 5390{
9c2799c2 5391 gas_assert (ep->X_op == O_constant);
885add95 5392
256ab948 5393 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
5394 if (!dbl)
5395 normalize_constant_expr (ep);
256ab948 5396
67c1ffbe 5397 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 5398 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
5399 as_warn (_("operand overflow"));
5400
5401 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
5402 {
5403 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 5404 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
5405 }
5406 else
5407 {
5408 /* 32-bit offset, need multiple instructions and AT, like:
5409 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
5410 addu $tempreg,$tempreg,$breg
5411 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
5412 to handle the complete offset. */
67c0d1eb
RS
5413 macro_build_lui (ep, AT);
5414 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
5415 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95 5416
741fe287 5417 if (!mips_opts.at)
8fc2e39e 5418 as_bad (_("Macro used $at after \".set noat\""));
885add95
CD
5419 }
5420}
5421
252b5132
RH
5422/* set_at()
5423 * Generates code to set the $at register to true (one)
5424 * if reg is less than the immediate expression.
5425 */
5426static void
67c0d1eb 5427set_at (int reg, int unsignedp)
252b5132
RH
5428{
5429 if (imm_expr.X_op == O_constant
5430 && imm_expr.X_add_number >= -0x8000
5431 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
5432 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
5433 AT, reg, BFD_RELOC_LO16);
252b5132
RH
5434 else
5435 {
67c0d1eb
RS
5436 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
5437 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
5438 }
5439}
5440
5441/* Warn if an expression is not a constant. */
5442
5443static void
17a2f251 5444check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
252b5132
RH
5445{
5446 if (ex->X_op == O_big)
5447 as_bad (_("unsupported large constant"));
5448 else if (ex->X_op != O_constant)
9ee2a2d4
MR
5449 as_bad (_("Instruction %s requires absolute expression"),
5450 ip->insn_mo->name);
13757d0c 5451
9ee2a2d4
MR
5452 if (HAVE_32BIT_GPRS)
5453 normalize_constant_expr (ex);
252b5132
RH
5454}
5455
5456/* Count the leading zeroes by performing a binary chop. This is a
5457 bulky bit of source, but performance is a LOT better for the
5458 majority of values than a simple loop to count the bits:
5459 for (lcnt = 0; (lcnt < 32); lcnt++)
5460 if ((v) & (1 << (31 - lcnt)))
5461 break;
5462 However it is not code size friendly, and the gain will drop a bit
5463 on certain cached systems.
5464*/
5465#define COUNT_TOP_ZEROES(v) \
5466 (((v) & ~0xffff) == 0 \
5467 ? ((v) & ~0xff) == 0 \
5468 ? ((v) & ~0xf) == 0 \
5469 ? ((v) & ~0x3) == 0 \
5470 ? ((v) & ~0x1) == 0 \
5471 ? !(v) \
5472 ? 32 \
5473 : 31 \
5474 : 30 \
5475 : ((v) & ~0x7) == 0 \
5476 ? 29 \
5477 : 28 \
5478 : ((v) & ~0x3f) == 0 \
5479 ? ((v) & ~0x1f) == 0 \
5480 ? 27 \
5481 : 26 \
5482 : ((v) & ~0x7f) == 0 \
5483 ? 25 \
5484 : 24 \
5485 : ((v) & ~0xfff) == 0 \
5486 ? ((v) & ~0x3ff) == 0 \
5487 ? ((v) & ~0x1ff) == 0 \
5488 ? 23 \
5489 : 22 \
5490 : ((v) & ~0x7ff) == 0 \
5491 ? 21 \
5492 : 20 \
5493 : ((v) & ~0x3fff) == 0 \
5494 ? ((v) & ~0x1fff) == 0 \
5495 ? 19 \
5496 : 18 \
5497 : ((v) & ~0x7fff) == 0 \
5498 ? 17 \
5499 : 16 \
5500 : ((v) & ~0xffffff) == 0 \
5501 ? ((v) & ~0xfffff) == 0 \
5502 ? ((v) & ~0x3ffff) == 0 \
5503 ? ((v) & ~0x1ffff) == 0 \
5504 ? 15 \
5505 : 14 \
5506 : ((v) & ~0x7ffff) == 0 \
5507 ? 13 \
5508 : 12 \
5509 : ((v) & ~0x3fffff) == 0 \
5510 ? ((v) & ~0x1fffff) == 0 \
5511 ? 11 \
5512 : 10 \
5513 : ((v) & ~0x7fffff) == 0 \
5514 ? 9 \
5515 : 8 \
5516 : ((v) & ~0xfffffff) == 0 \
5517 ? ((v) & ~0x3ffffff) == 0 \
5518 ? ((v) & ~0x1ffffff) == 0 \
5519 ? 7 \
5520 : 6 \
5521 : ((v) & ~0x7ffffff) == 0 \
5522 ? 5 \
5523 : 4 \
5524 : ((v) & ~0x3fffffff) == 0 \
5525 ? ((v) & ~0x1fffffff) == 0 \
5526 ? 3 \
5527 : 2 \
5528 : ((v) & ~0x7fffffff) == 0 \
5529 ? 1 \
5530 : 0)
5531
5532/* load_register()
67c1ffbe 5533 * This routine generates the least number of instructions necessary to load
252b5132
RH
5534 * an absolute expression value into a register.
5535 */
5536static void
67c0d1eb 5537load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
5538{
5539 int freg;
5540 expressionS hi32, lo32;
5541
5542 if (ep->X_op != O_big)
5543 {
9c2799c2 5544 gas_assert (ep->X_op == O_constant);
256ab948
TS
5545
5546 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
5547 if (!dbl)
5548 normalize_constant_expr (ep);
256ab948
TS
5549
5550 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
5551 {
5552 /* We can handle 16 bit signed values with an addiu to
5553 $zero. No need to ever use daddiu here, since $zero and
5554 the result are always correct in 32 bit mode. */
67c0d1eb 5555 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
5556 return;
5557 }
5558 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
5559 {
5560 /* We can handle 16 bit unsigned values with an ori to
5561 $zero. */
67c0d1eb 5562 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
5563 return;
5564 }
256ab948 5565 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
5566 {
5567 /* 32 bit values require an lui. */
df58fc94 5568 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 5569 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 5570 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
5571 return;
5572 }
5573 }
5574
5575 /* The value is larger than 32 bits. */
5576
2051e8c4 5577 if (!dbl || HAVE_32BIT_GPRS)
252b5132 5578 {
55e08f71
NC
5579 char value[32];
5580
5581 sprintf_vma (value, ep->X_add_number);
20e1fcfd 5582 as_bad (_("Number (0x%s) larger than 32 bits"), value);
67c0d1eb 5583 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
5584 return;
5585 }
5586
5587 if (ep->X_op != O_big)
5588 {
5589 hi32 = *ep;
5590 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
5591 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
5592 hi32.X_add_number &= 0xffffffff;
5593 lo32 = *ep;
5594 lo32.X_add_number &= 0xffffffff;
5595 }
5596 else
5597 {
9c2799c2 5598 gas_assert (ep->X_add_number > 2);
252b5132
RH
5599 if (ep->X_add_number == 3)
5600 generic_bignum[3] = 0;
5601 else if (ep->X_add_number > 4)
5602 as_bad (_("Number larger than 64 bits"));
5603 lo32.X_op = O_constant;
5604 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
5605 hi32.X_op = O_constant;
5606 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
5607 }
5608
5609 if (hi32.X_add_number == 0)
5610 freg = 0;
5611 else
5612 {
5613 int shift, bit;
5614 unsigned long hi, lo;
5615
956cd1d6 5616 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
5617 {
5618 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
5619 {
67c0d1eb 5620 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
5621 return;
5622 }
5623 if (lo32.X_add_number & 0x80000000)
5624 {
df58fc94 5625 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 5626 if (lo32.X_add_number & 0xffff)
67c0d1eb 5627 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
5628 return;
5629 }
5630 }
252b5132
RH
5631
5632 /* Check for 16bit shifted constant. We know that hi32 is
5633 non-zero, so start the mask on the first bit of the hi32
5634 value. */
5635 shift = 17;
5636 do
beae10d5
KH
5637 {
5638 unsigned long himask, lomask;
5639
5640 if (shift < 32)
5641 {
5642 himask = 0xffff >> (32 - shift);
5643 lomask = (0xffff << shift) & 0xffffffff;
5644 }
5645 else
5646 {
5647 himask = 0xffff << (shift - 32);
5648 lomask = 0;
5649 }
5650 if ((hi32.X_add_number & ~(offsetT) himask) == 0
5651 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
5652 {
5653 expressionS tmp;
5654
5655 tmp.X_op = O_constant;
5656 if (shift < 32)
5657 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
5658 | (lo32.X_add_number >> shift));
5659 else
5660 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb 5661 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
df58fc94 5662 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 5663 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
5664 return;
5665 }
f9419b05 5666 ++shift;
beae10d5
KH
5667 }
5668 while (shift <= (64 - 16));
252b5132
RH
5669
5670 /* Find the bit number of the lowest one bit, and store the
5671 shifted value in hi/lo. */
5672 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
5673 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
5674 if (lo != 0)
5675 {
5676 bit = 0;
5677 while ((lo & 1) == 0)
5678 {
5679 lo >>= 1;
5680 ++bit;
5681 }
5682 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
5683 hi >>= bit;
5684 }
5685 else
5686 {
5687 bit = 32;
5688 while ((hi & 1) == 0)
5689 {
5690 hi >>= 1;
5691 ++bit;
5692 }
5693 lo = hi;
5694 hi = 0;
5695 }
5696
5697 /* Optimize if the shifted value is a (power of 2) - 1. */
5698 if ((hi == 0 && ((lo + 1) & lo) == 0)
5699 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
5700 {
5701 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 5702 if (shift != 0)
beae10d5 5703 {
252b5132
RH
5704 expressionS tmp;
5705
5706 /* This instruction will set the register to be all
5707 ones. */
beae10d5
KH
5708 tmp.X_op = O_constant;
5709 tmp.X_add_number = (offsetT) -1;
67c0d1eb 5710 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
5711 if (bit != 0)
5712 {
5713 bit += shift;
df58fc94 5714 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 5715 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 5716 }
df58fc94 5717 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
67c0d1eb 5718 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
5719 return;
5720 }
5721 }
252b5132
RH
5722
5723 /* Sign extend hi32 before calling load_register, because we can
5724 generally get better code when we load a sign extended value. */
5725 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 5726 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 5727 load_register (reg, &hi32, 0);
252b5132
RH
5728 freg = reg;
5729 }
5730 if ((lo32.X_add_number & 0xffff0000) == 0)
5731 {
5732 if (freg != 0)
5733 {
df58fc94 5734 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
252b5132
RH
5735 freg = reg;
5736 }
5737 }
5738 else
5739 {
5740 expressionS mid16;
5741
956cd1d6 5742 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 5743 {
df58fc94
RS
5744 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
5745 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
beae10d5
KH
5746 return;
5747 }
252b5132
RH
5748
5749 if (freg != 0)
5750 {
df58fc94 5751 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
252b5132
RH
5752 freg = reg;
5753 }
5754 mid16 = lo32;
5755 mid16.X_add_number >>= 16;
67c0d1eb 5756 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
df58fc94 5757 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
252b5132
RH
5758 freg = reg;
5759 }
5760 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 5761 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
5762}
5763
269137b2
TS
5764static inline void
5765load_delay_nop (void)
5766{
5767 if (!gpr_interlocks)
5768 macro_build (NULL, "nop", "");
5769}
5770
252b5132
RH
5771/* Load an address into a register. */
5772
5773static void
67c0d1eb 5774load_address (int reg, expressionS *ep, int *used_at)
252b5132 5775{
252b5132
RH
5776 if (ep->X_op != O_constant
5777 && ep->X_op != O_symbol)
5778 {
5779 as_bad (_("expression too complex"));
5780 ep->X_op = O_constant;
5781 }
5782
5783 if (ep->X_op == O_constant)
5784 {
67c0d1eb 5785 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
5786 return;
5787 }
5788
5789 if (mips_pic == NO_PIC)
5790 {
5791 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 5792 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
5793 Otherwise we want
5794 lui $reg,<sym> (BFD_RELOC_HI16_S)
5795 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 5796 If we have an addend, we always use the latter form.
76b3015f 5797
d6bc6245
TS
5798 With 64bit address space and a usable $at we want
5799 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5800 lui $at,<sym> (BFD_RELOC_HI16_S)
5801 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
5802 daddiu $at,<sym> (BFD_RELOC_LO16)
5803 dsll32 $reg,0
3a482fd5 5804 daddu $reg,$reg,$at
76b3015f 5805
c03099e6 5806 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
5807 on superscalar processors.
5808 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5809 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
5810 dsll $reg,16
5811 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
5812 dsll $reg,16
5813 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
5814
5815 For GP relative symbols in 64bit address space we can use
5816 the same sequence as in 32bit address space. */
aed1a261 5817 if (HAVE_64BIT_SYMBOLS)
d6bc6245 5818 {
6caf9ef4
TS
5819 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
5820 && !nopic_need_relax (ep->X_add_symbol, 1))
5821 {
5822 relax_start (ep->X_add_symbol);
5823 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
5824 mips_gp_register, BFD_RELOC_GPREL16);
5825 relax_switch ();
5826 }
d6bc6245 5827
741fe287 5828 if (*used_at == 0 && mips_opts.at)
d6bc6245 5829 {
df58fc94
RS
5830 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
5831 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
67c0d1eb
RS
5832 macro_build (ep, "daddiu", "t,r,j", reg, reg,
5833 BFD_RELOC_MIPS_HIGHER);
5834 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
df58fc94 5835 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
67c0d1eb 5836 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
5837 *used_at = 1;
5838 }
5839 else
5840 {
df58fc94 5841 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb
RS
5842 macro_build (ep, "daddiu", "t,r,j", reg, reg,
5843 BFD_RELOC_MIPS_HIGHER);
df58fc94 5844 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 5845 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
df58fc94 5846 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 5847 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 5848 }
6caf9ef4
TS
5849
5850 if (mips_relax.sequence)
5851 relax_end ();
d6bc6245 5852 }
252b5132
RH
5853 else
5854 {
d6bc6245 5855 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 5856 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 5857 {
4d7206a2 5858 relax_start (ep->X_add_symbol);
67c0d1eb 5859 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 5860 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 5861 relax_switch ();
d6bc6245 5862 }
67c0d1eb
RS
5863 macro_build_lui (ep, reg);
5864 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
5865 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
5866 if (mips_relax.sequence)
5867 relax_end ();
d6bc6245 5868 }
252b5132 5869 }
0a44bf69 5870 else if (!mips_big_got)
252b5132
RH
5871 {
5872 expressionS ex;
5873
5874 /* If this is a reference to an external symbol, we want
5875 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5876 Otherwise we want
5877 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5878 nop
5879 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
5880 If there is a constant, it must be added in after.
5881
ed6fb7bd 5882 If we have NewABI, we want
f5040a92
AO
5883 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5884 unless we're referencing a global symbol with a non-zero
5885 offset, in which case cst must be added separately. */
ed6fb7bd
SC
5886 if (HAVE_NEWABI)
5887 {
f5040a92
AO
5888 if (ep->X_add_number)
5889 {
4d7206a2 5890 ex.X_add_number = ep->X_add_number;
f5040a92 5891 ep->X_add_number = 0;
4d7206a2 5892 relax_start (ep->X_add_symbol);
67c0d1eb
RS
5893 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
5894 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
5895 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
5896 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5897 ex.X_op = O_constant;
67c0d1eb 5898 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5899 reg, reg, BFD_RELOC_LO16);
f5040a92 5900 ep->X_add_number = ex.X_add_number;
4d7206a2 5901 relax_switch ();
f5040a92 5902 }
67c0d1eb 5903 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 5904 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
5905 if (mips_relax.sequence)
5906 relax_end ();
ed6fb7bd
SC
5907 }
5908 else
5909 {
f5040a92
AO
5910 ex.X_add_number = ep->X_add_number;
5911 ep->X_add_number = 0;
67c0d1eb
RS
5912 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
5913 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 5914 load_delay_nop ();
4d7206a2
RS
5915 relax_start (ep->X_add_symbol);
5916 relax_switch ();
67c0d1eb 5917 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 5918 BFD_RELOC_LO16);
4d7206a2 5919 relax_end ();
ed6fb7bd 5920
f5040a92
AO
5921 if (ex.X_add_number != 0)
5922 {
5923 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
5924 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5925 ex.X_op = O_constant;
67c0d1eb 5926 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5927 reg, reg, BFD_RELOC_LO16);
f5040a92 5928 }
252b5132
RH
5929 }
5930 }
0a44bf69 5931 else if (mips_big_got)
252b5132
RH
5932 {
5933 expressionS ex;
252b5132
RH
5934
5935 /* This is the large GOT case. If this is a reference to an
5936 external symbol, we want
5937 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5938 addu $reg,$reg,$gp
5939 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
5940
5941 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
5942 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5943 nop
5944 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 5945 If there is a constant, it must be added in after.
f5040a92
AO
5946
5947 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
5948 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5949 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 5950 */
438c16b8
TS
5951 if (HAVE_NEWABI)
5952 {
4d7206a2 5953 ex.X_add_number = ep->X_add_number;
f5040a92 5954 ep->X_add_number = 0;
4d7206a2 5955 relax_start (ep->X_add_symbol);
df58fc94 5956 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
5957 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5958 reg, reg, mips_gp_register);
5959 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
5960 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
5961 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
5962 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5963 else if (ex.X_add_number)
5964 {
5965 ex.X_op = O_constant;
67c0d1eb
RS
5966 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
5967 BFD_RELOC_LO16);
f5040a92
AO
5968 }
5969
5970 ep->X_add_number = ex.X_add_number;
4d7206a2 5971 relax_switch ();
67c0d1eb 5972 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 5973 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
5974 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
5975 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 5976 relax_end ();
438c16b8 5977 }
252b5132 5978 else
438c16b8 5979 {
f5040a92
AO
5980 ex.X_add_number = ep->X_add_number;
5981 ep->X_add_number = 0;
4d7206a2 5982 relax_start (ep->X_add_symbol);
df58fc94 5983 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
5984 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5985 reg, reg, mips_gp_register);
5986 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
5987 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
5988 relax_switch ();
5989 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
5990 {
5991 /* We need a nop before loading from $gp. This special
5992 check is required because the lui which starts the main
5993 instruction stream does not refer to $gp, and so will not
5994 insert the nop which may be required. */
67c0d1eb 5995 macro_build (NULL, "nop", "");
438c16b8 5996 }
67c0d1eb 5997 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 5998 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 5999 load_delay_nop ();
67c0d1eb 6000 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 6001 BFD_RELOC_LO16);
4d7206a2 6002 relax_end ();
438c16b8 6003
f5040a92
AO
6004 if (ex.X_add_number != 0)
6005 {
6006 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6007 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6008 ex.X_op = O_constant;
67c0d1eb
RS
6009 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6010 BFD_RELOC_LO16);
f5040a92 6011 }
252b5132
RH
6012 }
6013 }
252b5132
RH
6014 else
6015 abort ();
8fc2e39e 6016
741fe287 6017 if (!mips_opts.at && *used_at == 1)
8fc2e39e 6018 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
6019}
6020
ea1fb5dc
RS
6021/* Move the contents of register SOURCE into register DEST. */
6022
6023static void
67c0d1eb 6024move_register (int dest, int source)
ea1fb5dc 6025{
df58fc94
RS
6026 /* Prefer to use a 16-bit microMIPS instruction unless the previous
6027 instruction specifically requires a 32-bit one. */
6028 if (mips_opts.micromips
6029 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7951ca42 6030 macro_build (NULL, "move", "mp,mj", dest, source);
df58fc94
RS
6031 else
6032 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
6033 dest, source, 0);
ea1fb5dc
RS
6034}
6035
4d7206a2 6036/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
6037 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
6038 The two alternatives are:
4d7206a2
RS
6039
6040 Global symbol Local sybmol
6041 ------------- ------------
6042 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
6043 ... ...
6044 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
6045
6046 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
6047 emits the second for a 16-bit offset or add_got_offset_hilo emits
6048 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
6049
6050static void
67c0d1eb 6051load_got_offset (int dest, expressionS *local)
4d7206a2
RS
6052{
6053 expressionS global;
6054
6055 global = *local;
6056 global.X_add_number = 0;
6057
6058 relax_start (local->X_add_symbol);
67c0d1eb
RS
6059 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
6060 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 6061 relax_switch ();
67c0d1eb
RS
6062 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
6063 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
6064 relax_end ();
6065}
6066
6067static void
67c0d1eb 6068add_got_offset (int dest, expressionS *local)
4d7206a2
RS
6069{
6070 expressionS global;
6071
6072 global.X_op = O_constant;
6073 global.X_op_symbol = NULL;
6074 global.X_add_symbol = NULL;
6075 global.X_add_number = local->X_add_number;
6076
6077 relax_start (local->X_add_symbol);
67c0d1eb 6078 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
6079 dest, dest, BFD_RELOC_LO16);
6080 relax_switch ();
67c0d1eb 6081 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
6082 relax_end ();
6083}
6084
f6a22291
MR
6085static void
6086add_got_offset_hilo (int dest, expressionS *local, int tmp)
6087{
6088 expressionS global;
6089 int hold_mips_optimize;
6090
6091 global.X_op = O_constant;
6092 global.X_op_symbol = NULL;
6093 global.X_add_symbol = NULL;
6094 global.X_add_number = local->X_add_number;
6095
6096 relax_start (local->X_add_symbol);
6097 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
6098 relax_switch ();
6099 /* Set mips_optimize around the lui instruction to avoid
6100 inserting an unnecessary nop after the lw. */
6101 hold_mips_optimize = mips_optimize;
6102 mips_optimize = 2;
6103 macro_build_lui (&global, tmp);
6104 mips_optimize = hold_mips_optimize;
6105 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
6106 relax_end ();
6107
6108 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
6109}
6110
df58fc94
RS
6111/* Emit a sequence of instructions to emulate a branch likely operation.
6112 BR is an ordinary branch corresponding to one to be emulated. BRNEG
6113 is its complementing branch with the original condition negated.
6114 CALL is set if the original branch specified the link operation.
6115 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
6116
6117 Code like this is produced in the noreorder mode:
6118
6119 BRNEG <args>, 1f
6120 nop
6121 b <sym>
6122 delay slot (executed only if branch taken)
6123 1:
6124
6125 or, if CALL is set:
6126
6127 BRNEG <args>, 1f
6128 nop
6129 bal <sym>
6130 delay slot (executed only if branch taken)
6131 1:
6132
6133 In the reorder mode the delay slot would be filled with a nop anyway,
6134 so code produced is simply:
6135
6136 BR <args>, <sym>
6137 nop
6138
6139 This function is used when producing code for the microMIPS ASE that
6140 does not implement branch likely instructions in hardware. */
6141
6142static void
6143macro_build_branch_likely (const char *br, const char *brneg,
6144 int call, expressionS *ep, const char *fmt,
6145 unsigned int sreg, unsigned int treg)
6146{
6147 int noreorder = mips_opts.noreorder;
6148 expressionS expr1;
6149
6150 gas_assert (mips_opts.micromips);
6151 start_noreorder ();
6152 if (noreorder)
6153 {
6154 micromips_label_expr (&expr1);
6155 macro_build (&expr1, brneg, fmt, sreg, treg);
6156 macro_build (NULL, "nop", "");
6157 macro_build (ep, call ? "bal" : "b", "p");
6158
6159 /* Set to true so that append_insn adds a label. */
6160 emit_branch_likely_macro = TRUE;
6161 }
6162 else
6163 {
6164 macro_build (ep, br, fmt, sreg, treg);
6165 macro_build (NULL, "nop", "");
6166 }
6167 end_noreorder ();
6168}
6169
6170/* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
6171 the condition code tested. EP specifies the branch target. */
6172
6173static void
6174macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
6175{
6176 const int call = 0;
6177 const char *brneg;
6178 const char *br;
6179
6180 switch (type)
6181 {
6182 case M_BC1FL:
6183 br = "bc1f";
6184 brneg = "bc1t";
6185 break;
6186 case M_BC1TL:
6187 br = "bc1t";
6188 brneg = "bc1f";
6189 break;
6190 case M_BC2FL:
6191 br = "bc2f";
6192 brneg = "bc2t";
6193 break;
6194 case M_BC2TL:
6195 br = "bc2t";
6196 brneg = "bc2f";
6197 break;
6198 default:
6199 abort ();
6200 }
6201 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
6202}
6203
6204/* Emit a two-argument branch macro specified by TYPE, using SREG as
6205 the register tested. EP specifies the branch target. */
6206
6207static void
6208macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
6209{
6210 const char *brneg = NULL;
6211 const char *br;
6212 int call = 0;
6213
6214 switch (type)
6215 {
6216 case M_BGEZ:
6217 br = "bgez";
6218 break;
6219 case M_BGEZL:
6220 br = mips_opts.micromips ? "bgez" : "bgezl";
6221 brneg = "bltz";
6222 break;
6223 case M_BGEZALL:
6224 gas_assert (mips_opts.micromips);
6225 br = "bgezals";
6226 brneg = "bltz";
6227 call = 1;
6228 break;
6229 case M_BGTZ:
6230 br = "bgtz";
6231 break;
6232 case M_BGTZL:
6233 br = mips_opts.micromips ? "bgtz" : "bgtzl";
6234 brneg = "blez";
6235 break;
6236 case M_BLEZ:
6237 br = "blez";
6238 break;
6239 case M_BLEZL:
6240 br = mips_opts.micromips ? "blez" : "blezl";
6241 brneg = "bgtz";
6242 break;
6243 case M_BLTZ:
6244 br = "bltz";
6245 break;
6246 case M_BLTZL:
6247 br = mips_opts.micromips ? "bltz" : "bltzl";
6248 brneg = "bgez";
6249 break;
6250 case M_BLTZALL:
6251 gas_assert (mips_opts.micromips);
6252 br = "bltzals";
6253 brneg = "bgez";
6254 call = 1;
6255 break;
6256 default:
6257 abort ();
6258 }
6259 if (mips_opts.micromips && brneg)
6260 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
6261 else
6262 macro_build (ep, br, "s,p", sreg);
6263}
6264
6265/* Emit a three-argument branch macro specified by TYPE, using SREG and
6266 TREG as the registers tested. EP specifies the branch target. */
6267
6268static void
6269macro_build_branch_rsrt (int type, expressionS *ep,
6270 unsigned int sreg, unsigned int treg)
6271{
6272 const char *brneg = NULL;
6273 const int call = 0;
6274 const char *br;
6275
6276 switch (type)
6277 {
6278 case M_BEQ:
6279 case M_BEQ_I:
6280 br = "beq";
6281 break;
6282 case M_BEQL:
6283 case M_BEQL_I:
6284 br = mips_opts.micromips ? "beq" : "beql";
6285 brneg = "bne";
6286 break;
6287 case M_BNE:
6288 case M_BNE_I:
6289 br = "bne";
6290 break;
6291 case M_BNEL:
6292 case M_BNEL_I:
6293 br = mips_opts.micromips ? "bne" : "bnel";
6294 brneg = "beq";
6295 break;
6296 default:
6297 abort ();
6298 }
6299 if (mips_opts.micromips && brneg)
6300 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
6301 else
6302 macro_build (ep, br, "s,t,p", sreg, treg);
6303}
6304
252b5132
RH
6305/*
6306 * Build macros
6307 * This routine implements the seemingly endless macro or synthesized
6308 * instructions and addressing modes in the mips assembly language. Many
6309 * of these macros are simple and are similar to each other. These could
67c1ffbe 6310 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
6311 * this verbose method. Others are not simple macros but are more like
6312 * optimizing code generation.
6313 * One interesting optimization is when several store macros appear
67c1ffbe 6314 * consecutively that would load AT with the upper half of the same address.
252b5132
RH
6315 * The ensuing load upper instructions are ommited. This implies some kind
6316 * of global optimization. We currently only optimize within a single macro.
6317 * For many of the load and store macros if the address is specified as a
6318 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
6319 * first load register 'at' with zero and use it as the base register. The
6320 * mips assembler simply uses register $zero. Just one tiny optimization
6321 * we're missing.
6322 */
6323static void
17a2f251 6324macro (struct mips_cl_insn *ip)
252b5132 6325{
741fe287
MR
6326 unsigned int treg, sreg, dreg, breg;
6327 unsigned int tempreg;
252b5132 6328 int mask;
43841e91 6329 int used_at = 0;
df58fc94 6330 expressionS label_expr;
252b5132 6331 expressionS expr1;
df58fc94 6332 expressionS *ep;
252b5132
RH
6333 const char *s;
6334 const char *s2;
6335 const char *fmt;
6336 int likely = 0;
252b5132 6337 int coproc = 0;
df58fc94 6338 int off12 = 0;
1abe91b1 6339 int call = 0;
df58fc94
RS
6340 int jals = 0;
6341 int dbl = 0;
6342 int imm = 0;
6343 int ust = 0;
6344 int lp = 0;
6345 int ab = 0;
dd6a37e7 6346 int off0 = 0;
252b5132 6347 int off;
67c0d1eb 6348 offsetT maxnum;
252b5132 6349 bfd_reloc_code_real_type r;
252b5132
RH
6350 int hold_mips_optimize;
6351
9c2799c2 6352 gas_assert (! mips_opts.mips16);
252b5132 6353
df58fc94
RS
6354 treg = EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
6355 dreg = EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
6356 sreg = breg = EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
252b5132
RH
6357 mask = ip->insn_mo->mask;
6358
df58fc94
RS
6359 label_expr.X_op = O_constant;
6360 label_expr.X_op_symbol = NULL;
6361 label_expr.X_add_symbol = NULL;
6362 label_expr.X_add_number = 0;
6363
252b5132
RH
6364 expr1.X_op = O_constant;
6365 expr1.X_op_symbol = NULL;
6366 expr1.X_add_symbol = NULL;
6367 expr1.X_add_number = 1;
6368
6369 switch (mask)
6370 {
6371 case M_DABS:
6372 dbl = 1;
6373 case M_ABS:
df58fc94
RS
6374 /* bgez $a0,1f
6375 move v0,$a0
6376 sub v0,$zero,$a0
6377 1:
6378 */
252b5132 6379
7d10b47d 6380 start_noreorder ();
252b5132 6381
df58fc94
RS
6382 if (mips_opts.micromips)
6383 micromips_label_expr (&label_expr);
6384 else
6385 label_expr.X_add_number = 8;
6386 macro_build (&label_expr, "bgez", "s,p", sreg);
252b5132 6387 if (dreg == sreg)
a605d2b3 6388 macro_build (NULL, "nop", "");
252b5132 6389 else
67c0d1eb
RS
6390 move_register (dreg, sreg);
6391 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
df58fc94
RS
6392 if (mips_opts.micromips)
6393 micromips_add_label ();
252b5132 6394
7d10b47d 6395 end_noreorder ();
8fc2e39e 6396 break;
252b5132
RH
6397
6398 case M_ADD_I:
6399 s = "addi";
6400 s2 = "add";
6401 goto do_addi;
6402 case M_ADDU_I:
6403 s = "addiu";
6404 s2 = "addu";
6405 goto do_addi;
6406 case M_DADD_I:
6407 dbl = 1;
6408 s = "daddi";
6409 s2 = "dadd";
df58fc94
RS
6410 if (!mips_opts.micromips)
6411 goto do_addi;
6412 if (imm_expr.X_op == O_constant
6413 && imm_expr.X_add_number >= -0x200
6414 && imm_expr.X_add_number < 0x200)
6415 {
6416 macro_build (NULL, s, "t,r,.", treg, sreg, imm_expr.X_add_number);
6417 break;
6418 }
6419 goto do_addi_i;
252b5132
RH
6420 case M_DADDU_I:
6421 dbl = 1;
6422 s = "daddiu";
6423 s2 = "daddu";
6424 do_addi:
6425 if (imm_expr.X_op == O_constant
6426 && imm_expr.X_add_number >= -0x8000
6427 && imm_expr.X_add_number < 0x8000)
6428 {
67c0d1eb 6429 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
8fc2e39e 6430 break;
252b5132 6431 }
df58fc94 6432 do_addi_i:
8fc2e39e 6433 used_at = 1;
67c0d1eb
RS
6434 load_register (AT, &imm_expr, dbl);
6435 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
6436 break;
6437
6438 case M_AND_I:
6439 s = "andi";
6440 s2 = "and";
6441 goto do_bit;
6442 case M_OR_I:
6443 s = "ori";
6444 s2 = "or";
6445 goto do_bit;
6446 case M_NOR_I:
6447 s = "";
6448 s2 = "nor";
6449 goto do_bit;
6450 case M_XOR_I:
6451 s = "xori";
6452 s2 = "xor";
6453 do_bit:
6454 if (imm_expr.X_op == O_constant
6455 && imm_expr.X_add_number >= 0
6456 && imm_expr.X_add_number < 0x10000)
6457 {
6458 if (mask != M_NOR_I)
67c0d1eb 6459 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
252b5132
RH
6460 else
6461 {
67c0d1eb
RS
6462 macro_build (&imm_expr, "ori", "t,r,i",
6463 treg, sreg, BFD_RELOC_LO16);
6464 macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
252b5132 6465 }
8fc2e39e 6466 break;
252b5132
RH
6467 }
6468
8fc2e39e 6469 used_at = 1;
67c0d1eb
RS
6470 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
6471 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
6472 break;
6473
8b082fb1
TS
6474 case M_BALIGN:
6475 switch (imm_expr.X_add_number)
6476 {
6477 case 0:
6478 macro_build (NULL, "nop", "");
6479 break;
6480 case 2:
6481 macro_build (NULL, "packrl.ph", "d,s,t", treg, treg, sreg);
6482 break;
03f66e8a
MR
6483 case 1:
6484 case 3:
8b082fb1 6485 macro_build (NULL, "balign", "t,s,2", treg, sreg,
90ecf173 6486 (int) imm_expr.X_add_number);
8b082fb1 6487 break;
03f66e8a
MR
6488 default:
6489 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
6490 (unsigned long) imm_expr.X_add_number);
6491 break;
8b082fb1
TS
6492 }
6493 break;
6494
df58fc94
RS
6495 case M_BC1FL:
6496 case M_BC1TL:
6497 case M_BC2FL:
6498 case M_BC2TL:
6499 gas_assert (mips_opts.micromips);
6500 macro_build_branch_ccl (mask, &offset_expr,
6501 EXTRACT_OPERAND (1, BCC, *ip));
6502 break;
6503
252b5132 6504 case M_BEQ_I:
252b5132 6505 case M_BEQL_I:
252b5132 6506 case M_BNE_I:
252b5132 6507 case M_BNEL_I:
252b5132 6508 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
df58fc94
RS
6509 treg = 0;
6510 else
252b5132 6511 {
df58fc94
RS
6512 treg = AT;
6513 used_at = 1;
6514 load_register (treg, &imm_expr, HAVE_64BIT_GPRS);
252b5132 6515 }
df58fc94
RS
6516 /* Fall through. */
6517 case M_BEQL:
6518 case M_BNEL:
6519 macro_build_branch_rsrt (mask, &offset_expr, sreg, treg);
252b5132
RH
6520 break;
6521
6522 case M_BGEL:
6523 likely = 1;
6524 case M_BGE:
6525 if (treg == 0)
df58fc94
RS
6526 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, sreg);
6527 else if (sreg == 0)
6528 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, treg);
6529 else
252b5132 6530 {
df58fc94
RS
6531 used_at = 1;
6532 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
6533 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6534 &offset_expr, AT, ZERO);
252b5132 6535 }
df58fc94
RS
6536 break;
6537
6538 case M_BGEZL:
6539 case M_BGEZALL:
6540 case M_BGTZL:
6541 case M_BLEZL:
6542 case M_BLTZL:
6543 case M_BLTZALL:
6544 macro_build_branch_rs (mask, &offset_expr, sreg);
252b5132
RH
6545 break;
6546
6547 case M_BGTL_I:
6548 likely = 1;
6549 case M_BGT_I:
90ecf173 6550 /* Check for > max integer. */
252b5132 6551 maxnum = 0x7fffffff;
ca4e0257 6552 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
6553 {
6554 maxnum <<= 16;
6555 maxnum |= 0xffff;
6556 maxnum <<= 16;
6557 maxnum |= 0xffff;
6558 }
6559 if (imm_expr.X_op == O_constant
6560 && imm_expr.X_add_number >= maxnum
ca4e0257 6561 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
6562 {
6563 do_false:
90ecf173 6564 /* Result is always false. */
252b5132 6565 if (! likely)
a605d2b3 6566 macro_build (NULL, "nop", "");
252b5132 6567 else
df58fc94 6568 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
8fc2e39e 6569 break;
252b5132
RH
6570 }
6571 if (imm_expr.X_op != O_constant)
6572 as_bad (_("Unsupported large constant"));
f9419b05 6573 ++imm_expr.X_add_number;
252b5132
RH
6574 /* FALLTHROUGH */
6575 case M_BGE_I:
6576 case M_BGEL_I:
6577 if (mask == M_BGEL_I)
6578 likely = 1;
6579 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6580 {
df58fc94
RS
6581 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
6582 &offset_expr, sreg);
8fc2e39e 6583 break;
252b5132
RH
6584 }
6585 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6586 {
df58fc94
RS
6587 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
6588 &offset_expr, sreg);
8fc2e39e 6589 break;
252b5132
RH
6590 }
6591 maxnum = 0x7fffffff;
ca4e0257 6592 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
6593 {
6594 maxnum <<= 16;
6595 maxnum |= 0xffff;
6596 maxnum <<= 16;
6597 maxnum |= 0xffff;
6598 }
6599 maxnum = - maxnum - 1;
6600 if (imm_expr.X_op == O_constant
6601 && imm_expr.X_add_number <= maxnum
ca4e0257 6602 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
6603 {
6604 do_true:
6605 /* result is always true */
6606 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
67c0d1eb 6607 macro_build (&offset_expr, "b", "p");
8fc2e39e 6608 break;
252b5132 6609 }
8fc2e39e 6610 used_at = 1;
67c0d1eb 6611 set_at (sreg, 0);
df58fc94
RS
6612 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6613 &offset_expr, AT, ZERO);
252b5132
RH
6614 break;
6615
6616 case M_BGEUL:
6617 likely = 1;
6618 case M_BGEU:
6619 if (treg == 0)
6620 goto do_true;
df58fc94
RS
6621 else if (sreg == 0)
6622 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6623 &offset_expr, ZERO, treg);
6624 else
252b5132 6625 {
df58fc94
RS
6626 used_at = 1;
6627 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
6628 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6629 &offset_expr, AT, ZERO);
252b5132 6630 }
252b5132
RH
6631 break;
6632
6633 case M_BGTUL_I:
6634 likely = 1;
6635 case M_BGTU_I:
6636 if (sreg == 0
ca4e0257 6637 || (HAVE_32BIT_GPRS
252b5132 6638 && imm_expr.X_op == O_constant
f01dc953 6639 && imm_expr.X_add_number == -1))
252b5132
RH
6640 goto do_false;
6641 if (imm_expr.X_op != O_constant)
6642 as_bad (_("Unsupported large constant"));
f9419b05 6643 ++imm_expr.X_add_number;
252b5132
RH
6644 /* FALLTHROUGH */
6645 case M_BGEU_I:
6646 case M_BGEUL_I:
6647 if (mask == M_BGEUL_I)
6648 likely = 1;
6649 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6650 goto do_true;
df58fc94
RS
6651 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6652 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6653 &offset_expr, sreg, ZERO);
6654 else
252b5132 6655 {
df58fc94
RS
6656 used_at = 1;
6657 set_at (sreg, 1);
6658 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6659 &offset_expr, AT, ZERO);
252b5132 6660 }
252b5132
RH
6661 break;
6662
6663 case M_BGTL:
6664 likely = 1;
6665 case M_BGT:
6666 if (treg == 0)
df58fc94
RS
6667 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, sreg);
6668 else if (sreg == 0)
6669 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, treg);
6670 else
252b5132 6671 {
df58fc94
RS
6672 used_at = 1;
6673 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
6674 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6675 &offset_expr, AT, ZERO);
252b5132 6676 }
252b5132
RH
6677 break;
6678
6679 case M_BGTUL:
6680 likely = 1;
6681 case M_BGTU:
6682 if (treg == 0)
df58fc94
RS
6683 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6684 &offset_expr, sreg, ZERO);
6685 else if (sreg == 0)
6686 goto do_false;
6687 else
252b5132 6688 {
df58fc94
RS
6689 used_at = 1;
6690 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
6691 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6692 &offset_expr, AT, ZERO);
252b5132 6693 }
252b5132
RH
6694 break;
6695
6696 case M_BLEL:
6697 likely = 1;
6698 case M_BLE:
6699 if (treg == 0)
df58fc94
RS
6700 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, sreg);
6701 else if (sreg == 0)
6702 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, treg);
6703 else
252b5132 6704 {
df58fc94
RS
6705 used_at = 1;
6706 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
6707 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6708 &offset_expr, AT, ZERO);
252b5132 6709 }
252b5132
RH
6710 break;
6711
6712 case M_BLEL_I:
6713 likely = 1;
6714 case M_BLE_I:
6715 maxnum = 0x7fffffff;
ca4e0257 6716 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
6717 {
6718 maxnum <<= 16;
6719 maxnum |= 0xffff;
6720 maxnum <<= 16;
6721 maxnum |= 0xffff;
6722 }
6723 if (imm_expr.X_op == O_constant
6724 && imm_expr.X_add_number >= maxnum
ca4e0257 6725 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
6726 goto do_true;
6727 if (imm_expr.X_op != O_constant)
6728 as_bad (_("Unsupported large constant"));
f9419b05 6729 ++imm_expr.X_add_number;
252b5132
RH
6730 /* FALLTHROUGH */
6731 case M_BLT_I:
6732 case M_BLTL_I:
6733 if (mask == M_BLTL_I)
6734 likely = 1;
6735 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
df58fc94
RS
6736 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, sreg);
6737 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6738 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, sreg);
6739 else
252b5132 6740 {
df58fc94
RS
6741 used_at = 1;
6742 set_at (sreg, 0);
6743 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6744 &offset_expr, AT, ZERO);
252b5132 6745 }
252b5132
RH
6746 break;
6747
6748 case M_BLEUL:
6749 likely = 1;
6750 case M_BLEU:
6751 if (treg == 0)
df58fc94
RS
6752 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6753 &offset_expr, sreg, ZERO);
6754 else if (sreg == 0)
6755 goto do_true;
6756 else
252b5132 6757 {
df58fc94
RS
6758 used_at = 1;
6759 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
6760 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6761 &offset_expr, AT, ZERO);
252b5132 6762 }
252b5132
RH
6763 break;
6764
6765 case M_BLEUL_I:
6766 likely = 1;
6767 case M_BLEU_I:
6768 if (sreg == 0
ca4e0257 6769 || (HAVE_32BIT_GPRS
252b5132 6770 && imm_expr.X_op == O_constant
f01dc953 6771 && imm_expr.X_add_number == -1))
252b5132
RH
6772 goto do_true;
6773 if (imm_expr.X_op != O_constant)
6774 as_bad (_("Unsupported large constant"));
f9419b05 6775 ++imm_expr.X_add_number;
252b5132
RH
6776 /* FALLTHROUGH */
6777 case M_BLTU_I:
6778 case M_BLTUL_I:
6779 if (mask == M_BLTUL_I)
6780 likely = 1;
6781 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6782 goto do_false;
df58fc94
RS
6783 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6784 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6785 &offset_expr, sreg, ZERO);
6786 else
252b5132 6787 {
df58fc94
RS
6788 used_at = 1;
6789 set_at (sreg, 1);
6790 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6791 &offset_expr, AT, ZERO);
252b5132 6792 }
252b5132
RH
6793 break;
6794
6795 case M_BLTL:
6796 likely = 1;
6797 case M_BLT:
6798 if (treg == 0)
df58fc94
RS
6799 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, sreg);
6800 else if (sreg == 0)
6801 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, treg);
6802 else
252b5132 6803 {
df58fc94
RS
6804 used_at = 1;
6805 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
6806 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6807 &offset_expr, AT, ZERO);
252b5132 6808 }
252b5132
RH
6809 break;
6810
6811 case M_BLTUL:
6812 likely = 1;
6813 case M_BLTU:
6814 if (treg == 0)
6815 goto do_false;
df58fc94
RS
6816 else if (sreg == 0)
6817 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6818 &offset_expr, ZERO, treg);
6819 else
252b5132 6820 {
df58fc94
RS
6821 used_at = 1;
6822 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
6823 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6824 &offset_expr, AT, ZERO);
252b5132 6825 }
252b5132
RH
6826 break;
6827
5f74bc13
CD
6828 case M_DEXT:
6829 {
d5818fca
MR
6830 /* Use unsigned arithmetic. */
6831 addressT pos;
6832 addressT size;
5f74bc13 6833
90ecf173 6834 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
5f74bc13
CD
6835 {
6836 as_bad (_("Unsupported large constant"));
6837 pos = size = 1;
6838 }
6839 else
6840 {
d5818fca
MR
6841 pos = imm_expr.X_add_number;
6842 size = imm2_expr.X_add_number;
5f74bc13
CD
6843 }
6844
6845 if (pos > 63)
6846 {
d5818fca 6847 as_bad (_("Improper position (%lu)"), (unsigned long) pos);
5f74bc13
CD
6848 pos = 1;
6849 }
90ecf173 6850 if (size == 0 || size > 64 || (pos + size - 1) > 63)
5f74bc13
CD
6851 {
6852 as_bad (_("Improper extract size (%lu, position %lu)"),
d5818fca 6853 (unsigned long) size, (unsigned long) pos);
5f74bc13
CD
6854 size = 1;
6855 }
6856
6857 if (size <= 32 && pos < 32)
6858 {
6859 s = "dext";
6860 fmt = "t,r,+A,+C";
6861 }
6862 else if (size <= 32)
6863 {
6864 s = "dextu";
6865 fmt = "t,r,+E,+H";
6866 }
6867 else
6868 {
6869 s = "dextm";
6870 fmt = "t,r,+A,+G";
6871 }
d5818fca
MR
6872 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
6873 (int) (size - 1));
5f74bc13 6874 }
8fc2e39e 6875 break;
5f74bc13
CD
6876
6877 case M_DINS:
6878 {
d5818fca
MR
6879 /* Use unsigned arithmetic. */
6880 addressT pos;
6881 addressT size;
5f74bc13 6882
90ecf173 6883 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
5f74bc13
CD
6884 {
6885 as_bad (_("Unsupported large constant"));
6886 pos = size = 1;
6887 }
6888 else
6889 {
d5818fca
MR
6890 pos = imm_expr.X_add_number;
6891 size = imm2_expr.X_add_number;
5f74bc13
CD
6892 }
6893
6894 if (pos > 63)
6895 {
d5818fca 6896 as_bad (_("Improper position (%lu)"), (unsigned long) pos);
5f74bc13
CD
6897 pos = 1;
6898 }
90ecf173 6899 if (size == 0 || size > 64 || (pos + size - 1) > 63)
5f74bc13
CD
6900 {
6901 as_bad (_("Improper insert size (%lu, position %lu)"),
d5818fca 6902 (unsigned long) size, (unsigned long) pos);
5f74bc13
CD
6903 size = 1;
6904 }
6905
6906 if (pos < 32 && (pos + size - 1) < 32)
6907 {
6908 s = "dins";
6909 fmt = "t,r,+A,+B";
6910 }
6911 else if (pos >= 32)
6912 {
6913 s = "dinsu";
6914 fmt = "t,r,+E,+F";
6915 }
6916 else
6917 {
6918 s = "dinsm";
6919 fmt = "t,r,+A,+F";
6920 }
750bdd57
AS
6921 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
6922 (int) (pos + size - 1));
5f74bc13 6923 }
8fc2e39e 6924 break;
5f74bc13 6925
252b5132
RH
6926 case M_DDIV_3:
6927 dbl = 1;
6928 case M_DIV_3:
6929 s = "mflo";
6930 goto do_div3;
6931 case M_DREM_3:
6932 dbl = 1;
6933 case M_REM_3:
6934 s = "mfhi";
6935 do_div3:
6936 if (treg == 0)
6937 {
6938 as_warn (_("Divide by zero."));
6939 if (mips_trap)
df58fc94 6940 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 6941 else
df58fc94 6942 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 6943 break;
252b5132
RH
6944 }
6945
7d10b47d 6946 start_noreorder ();
252b5132
RH
6947 if (mips_trap)
6948 {
df58fc94 6949 macro_build (NULL, "teq", TRAP_FMT, treg, ZERO, 7);
67c0d1eb 6950 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
252b5132
RH
6951 }
6952 else
6953 {
df58fc94
RS
6954 if (mips_opts.micromips)
6955 micromips_label_expr (&label_expr);
6956 else
6957 label_expr.X_add_number = 8;
6958 macro_build (&label_expr, "bne", "s,t,p", treg, ZERO);
67c0d1eb 6959 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
df58fc94
RS
6960 macro_build (NULL, "break", BRK_FMT, 7);
6961 if (mips_opts.micromips)
6962 micromips_add_label ();
252b5132
RH
6963 }
6964 expr1.X_add_number = -1;
8fc2e39e 6965 used_at = 1;
f6a22291 6966 load_register (AT, &expr1, dbl);
df58fc94
RS
6967 if (mips_opts.micromips)
6968 micromips_label_expr (&label_expr);
6969 else
6970 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
6971 macro_build (&label_expr, "bne", "s,t,p", treg, AT);
252b5132
RH
6972 if (dbl)
6973 {
6974 expr1.X_add_number = 1;
f6a22291 6975 load_register (AT, &expr1, dbl);
df58fc94 6976 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
252b5132
RH
6977 }
6978 else
6979 {
6980 expr1.X_add_number = 0x80000000;
df58fc94 6981 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
252b5132
RH
6982 }
6983 if (mips_trap)
6984 {
df58fc94 6985 macro_build (NULL, "teq", TRAP_FMT, sreg, AT, 6);
252b5132
RH
6986 /* We want to close the noreorder block as soon as possible, so
6987 that later insns are available for delay slot filling. */
7d10b47d 6988 end_noreorder ();
252b5132
RH
6989 }
6990 else
6991 {
df58fc94
RS
6992 if (mips_opts.micromips)
6993 micromips_label_expr (&label_expr);
6994 else
6995 label_expr.X_add_number = 8;
6996 macro_build (&label_expr, "bne", "s,t,p", sreg, AT);
a605d2b3 6997 macro_build (NULL, "nop", "");
252b5132
RH
6998
6999 /* We want to close the noreorder block as soon as possible, so
7000 that later insns are available for delay slot filling. */
7d10b47d 7001 end_noreorder ();
252b5132 7002
df58fc94 7003 macro_build (NULL, "break", BRK_FMT, 6);
252b5132 7004 }
df58fc94
RS
7005 if (mips_opts.micromips)
7006 micromips_add_label ();
7007 macro_build (NULL, s, MFHL_FMT, dreg);
252b5132
RH
7008 break;
7009
7010 case M_DIV_3I:
7011 s = "div";
7012 s2 = "mflo";
7013 goto do_divi;
7014 case M_DIVU_3I:
7015 s = "divu";
7016 s2 = "mflo";
7017 goto do_divi;
7018 case M_REM_3I:
7019 s = "div";
7020 s2 = "mfhi";
7021 goto do_divi;
7022 case M_REMU_3I:
7023 s = "divu";
7024 s2 = "mfhi";
7025 goto do_divi;
7026 case M_DDIV_3I:
7027 dbl = 1;
7028 s = "ddiv";
7029 s2 = "mflo";
7030 goto do_divi;
7031 case M_DDIVU_3I:
7032 dbl = 1;
7033 s = "ddivu";
7034 s2 = "mflo";
7035 goto do_divi;
7036 case M_DREM_3I:
7037 dbl = 1;
7038 s = "ddiv";
7039 s2 = "mfhi";
7040 goto do_divi;
7041 case M_DREMU_3I:
7042 dbl = 1;
7043 s = "ddivu";
7044 s2 = "mfhi";
7045 do_divi:
7046 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7047 {
7048 as_warn (_("Divide by zero."));
7049 if (mips_trap)
df58fc94 7050 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 7051 else
df58fc94 7052 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 7053 break;
252b5132
RH
7054 }
7055 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
7056 {
7057 if (strcmp (s2, "mflo") == 0)
67c0d1eb 7058 move_register (dreg, sreg);
252b5132 7059 else
c80c840e 7060 move_register (dreg, ZERO);
8fc2e39e 7061 break;
252b5132
RH
7062 }
7063 if (imm_expr.X_op == O_constant
7064 && imm_expr.X_add_number == -1
7065 && s[strlen (s) - 1] != 'u')
7066 {
7067 if (strcmp (s2, "mflo") == 0)
7068 {
67c0d1eb 7069 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
252b5132
RH
7070 }
7071 else
c80c840e 7072 move_register (dreg, ZERO);
8fc2e39e 7073 break;
252b5132
RH
7074 }
7075
8fc2e39e 7076 used_at = 1;
67c0d1eb
RS
7077 load_register (AT, &imm_expr, dbl);
7078 macro_build (NULL, s, "z,s,t", sreg, AT);
df58fc94 7079 macro_build (NULL, s2, MFHL_FMT, dreg);
252b5132
RH
7080 break;
7081
7082 case M_DIVU_3:
7083 s = "divu";
7084 s2 = "mflo";
7085 goto do_divu3;
7086 case M_REMU_3:
7087 s = "divu";
7088 s2 = "mfhi";
7089 goto do_divu3;
7090 case M_DDIVU_3:
7091 s = "ddivu";
7092 s2 = "mflo";
7093 goto do_divu3;
7094 case M_DREMU_3:
7095 s = "ddivu";
7096 s2 = "mfhi";
7097 do_divu3:
7d10b47d 7098 start_noreorder ();
252b5132
RH
7099 if (mips_trap)
7100 {
df58fc94 7101 macro_build (NULL, "teq", TRAP_FMT, treg, ZERO, 7);
67c0d1eb 7102 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
7103 /* We want to close the noreorder block as soon as possible, so
7104 that later insns are available for delay slot filling. */
7d10b47d 7105 end_noreorder ();
252b5132
RH
7106 }
7107 else
7108 {
df58fc94
RS
7109 if (mips_opts.micromips)
7110 micromips_label_expr (&label_expr);
7111 else
7112 label_expr.X_add_number = 8;
7113 macro_build (&label_expr, "bne", "s,t,p", treg, ZERO);
67c0d1eb 7114 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
7115
7116 /* We want to close the noreorder block as soon as possible, so
7117 that later insns are available for delay slot filling. */
7d10b47d 7118 end_noreorder ();
df58fc94
RS
7119 macro_build (NULL, "break", BRK_FMT, 7);
7120 if (mips_opts.micromips)
7121 micromips_add_label ();
252b5132 7122 }
df58fc94 7123 macro_build (NULL, s2, MFHL_FMT, dreg);
8fc2e39e 7124 break;
252b5132 7125
1abe91b1
MR
7126 case M_DLCA_AB:
7127 dbl = 1;
7128 case M_LCA_AB:
7129 call = 1;
7130 goto do_la;
252b5132
RH
7131 case M_DLA_AB:
7132 dbl = 1;
7133 case M_LA_AB:
1abe91b1 7134 do_la:
252b5132
RH
7135 /* Load the address of a symbol into a register. If breg is not
7136 zero, we then add a base register to it. */
7137
3bec30a8
TS
7138 if (dbl && HAVE_32BIT_GPRS)
7139 as_warn (_("dla used to load 32-bit register"));
7140
90ecf173 7141 if (!dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
7142 as_warn (_("la used to load 64-bit address"));
7143
0c11417f
MR
7144 if (offset_expr.X_op == O_constant
7145 && offset_expr.X_add_number >= -0x8000
7146 && offset_expr.X_add_number < 0x8000)
7147 {
aed1a261 7148 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
17a2f251 7149 "t,r,j", treg, sreg, BFD_RELOC_LO16);
8fc2e39e 7150 break;
0c11417f
MR
7151 }
7152
741fe287 7153 if (mips_opts.at && (treg == breg))
afdbd6d0
CD
7154 {
7155 tempreg = AT;
7156 used_at = 1;
7157 }
7158 else
7159 {
7160 tempreg = treg;
afdbd6d0
CD
7161 }
7162
252b5132
RH
7163 if (offset_expr.X_op != O_symbol
7164 && offset_expr.X_op != O_constant)
7165 {
f71d0d44 7166 as_bad (_("Expression too complex"));
252b5132
RH
7167 offset_expr.X_op = O_constant;
7168 }
7169
252b5132 7170 if (offset_expr.X_op == O_constant)
aed1a261 7171 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
7172 else if (mips_pic == NO_PIC)
7173 {
d6bc6245 7174 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 7175 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
7176 Otherwise we want
7177 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
7178 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7179 If we have a constant, we need two instructions anyhow,
d6bc6245 7180 so we may as well always use the latter form.
76b3015f 7181
6caf9ef4
TS
7182 With 64bit address space and a usable $at we want
7183 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
7184 lui $at,<sym> (BFD_RELOC_HI16_S)
7185 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
7186 daddiu $at,<sym> (BFD_RELOC_LO16)
7187 dsll32 $tempreg,0
7188 daddu $tempreg,$tempreg,$at
7189
7190 If $at is already in use, we use a path which is suboptimal
7191 on superscalar processors.
7192 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
7193 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
7194 dsll $tempreg,16
7195 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
7196 dsll $tempreg,16
7197 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
7198
7199 For GP relative symbols in 64bit address space we can use
7200 the same sequence as in 32bit address space. */
aed1a261 7201 if (HAVE_64BIT_SYMBOLS)
252b5132 7202 {
6caf9ef4
TS
7203 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
7204 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
7205 {
7206 relax_start (offset_expr.X_add_symbol);
7207 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7208 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
7209 relax_switch ();
7210 }
d6bc6245 7211
741fe287 7212 if (used_at == 0 && mips_opts.at)
98d3f06f 7213 {
df58fc94 7214 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 7215 tempreg, BFD_RELOC_MIPS_HIGHEST);
df58fc94 7216 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 7217 AT, BFD_RELOC_HI16_S);
67c0d1eb 7218 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7219 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 7220 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7221 AT, AT, BFD_RELOC_LO16);
df58fc94 7222 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 7223 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
7224 used_at = 1;
7225 }
7226 else
7227 {
df58fc94 7228 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 7229 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 7230 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7231 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 7232 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 7233 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7234 tempreg, tempreg, BFD_RELOC_HI16_S);
df58fc94 7235 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 7236 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7237 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 7238 }
6caf9ef4
TS
7239
7240 if (mips_relax.sequence)
7241 relax_end ();
98d3f06f
KH
7242 }
7243 else
7244 {
7245 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 7246 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 7247 {
4d7206a2 7248 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7249 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7250 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 7251 relax_switch ();
98d3f06f 7252 }
6943caf0 7253 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
f71d0d44 7254 as_bad (_("Offset too large"));
67c0d1eb
RS
7255 macro_build_lui (&offset_expr, tempreg);
7256 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7257 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
7258 if (mips_relax.sequence)
7259 relax_end ();
98d3f06f 7260 }
252b5132 7261 }
0a44bf69 7262 else if (!mips_big_got && !HAVE_NEWABI)
252b5132 7263 {
9117d219
NC
7264 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
7265
252b5132
RH
7266 /* If this is a reference to an external symbol, and there
7267 is no constant, we want
7268 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 7269 or for lca or if tempreg is PIC_CALL_REG
9117d219 7270 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
7271 For a local symbol, we want
7272 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7273 nop
7274 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7275
7276 If we have a small constant, and this is a reference to
7277 an external symbol, we want
7278 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7279 nop
7280 addiu $tempreg,$tempreg,<constant>
7281 For a local symbol, we want the same instruction
7282 sequence, but we output a BFD_RELOC_LO16 reloc on the
7283 addiu instruction.
7284
7285 If we have a large constant, and this is a reference to
7286 an external symbol, we want
7287 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7288 lui $at,<hiconstant>
7289 addiu $at,$at,<loconstant>
7290 addu $tempreg,$tempreg,$at
7291 For a local symbol, we want the same instruction
7292 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 7293 addiu instruction.
ed6fb7bd
SC
7294 */
7295
4d7206a2 7296 if (offset_expr.X_add_number == 0)
252b5132 7297 {
0a44bf69
RS
7298 if (mips_pic == SVR4_PIC
7299 && breg == 0
7300 && (call || tempreg == PIC_CALL_REG))
4d7206a2
RS
7301 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
7302
7303 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7304 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7305 lw_reloc_type, mips_gp_register);
4d7206a2 7306 if (breg != 0)
252b5132
RH
7307 {
7308 /* We're going to put in an addu instruction using
7309 tempreg, so we may as well insert the nop right
7310 now. */
269137b2 7311 load_delay_nop ();
252b5132 7312 }
4d7206a2 7313 relax_switch ();
67c0d1eb
RS
7314 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7315 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 7316 load_delay_nop ();
67c0d1eb
RS
7317 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7318 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 7319 relax_end ();
252b5132
RH
7320 /* FIXME: If breg == 0, and the next instruction uses
7321 $tempreg, then if this variant case is used an extra
7322 nop will be generated. */
7323 }
4d7206a2
RS
7324 else if (offset_expr.X_add_number >= -0x8000
7325 && offset_expr.X_add_number < 0x8000)
252b5132 7326 {
67c0d1eb 7327 load_got_offset (tempreg, &offset_expr);
269137b2 7328 load_delay_nop ();
67c0d1eb 7329 add_got_offset (tempreg, &offset_expr);
252b5132
RH
7330 }
7331 else
7332 {
4d7206a2
RS
7333 expr1.X_add_number = offset_expr.X_add_number;
7334 offset_expr.X_add_number =
43c0598f 7335 SEXT_16BIT (offset_expr.X_add_number);
67c0d1eb 7336 load_got_offset (tempreg, &offset_expr);
f6a22291 7337 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
7338 /* If we are going to add in a base register, and the
7339 target register and the base register are the same,
7340 then we are using AT as a temporary register. Since
7341 we want to load the constant into AT, we add our
7342 current AT (from the global offset table) and the
7343 register into the register now, and pretend we were
7344 not using a base register. */
67c0d1eb 7345 if (breg == treg)
252b5132 7346 {
269137b2 7347 load_delay_nop ();
67c0d1eb 7348 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7349 treg, AT, breg);
252b5132
RH
7350 breg = 0;
7351 tempreg = treg;
252b5132 7352 }
f6a22291 7353 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
7354 used_at = 1;
7355 }
7356 }
0a44bf69 7357 else if (!mips_big_got && HAVE_NEWABI)
f5040a92 7358 {
67c0d1eb 7359 int add_breg_early = 0;
f5040a92
AO
7360
7361 /* If this is a reference to an external, and there is no
7362 constant, or local symbol (*), with or without a
7363 constant, we want
7364 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 7365 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
7366 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
7367
7368 If we have a small constant, and this is a reference to
7369 an external symbol, we want
7370 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
7371 addiu $tempreg,$tempreg,<constant>
7372
7373 If we have a large constant, and this is a reference to
7374 an external symbol, we want
7375 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
7376 lui $at,<hiconstant>
7377 addiu $at,$at,<loconstant>
7378 addu $tempreg,$tempreg,$at
7379
7380 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
7381 local symbols, even though it introduces an additional
7382 instruction. */
7383
f5040a92
AO
7384 if (offset_expr.X_add_number)
7385 {
4d7206a2 7386 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
7387 offset_expr.X_add_number = 0;
7388
4d7206a2 7389 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7390 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7391 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
7392
7393 if (expr1.X_add_number >= -0x8000
7394 && expr1.X_add_number < 0x8000)
7395 {
67c0d1eb
RS
7396 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
7397 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 7398 }
ecd13cd3 7399 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 7400 {
f5040a92
AO
7401 /* If we are going to add in a base register, and the
7402 target register and the base register are the same,
7403 then we are using AT as a temporary register. Since
7404 we want to load the constant into AT, we add our
7405 current AT (from the global offset table) and the
7406 register into the register now, and pretend we were
7407 not using a base register. */
7408 if (breg != treg)
7409 dreg = tempreg;
7410 else
7411 {
9c2799c2 7412 gas_assert (tempreg == AT);
67c0d1eb
RS
7413 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7414 treg, AT, breg);
f5040a92 7415 dreg = treg;
67c0d1eb 7416 add_breg_early = 1;
f5040a92
AO
7417 }
7418
f6a22291 7419 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 7420 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7421 dreg, dreg, AT);
f5040a92 7422
f5040a92
AO
7423 used_at = 1;
7424 }
7425 else
7426 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
7427
4d7206a2 7428 relax_switch ();
f5040a92
AO
7429 offset_expr.X_add_number = expr1.X_add_number;
7430
67c0d1eb
RS
7431 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7432 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7433 if (add_breg_early)
f5040a92 7434 {
67c0d1eb 7435 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
f899b4b8 7436 treg, tempreg, breg);
f5040a92
AO
7437 breg = 0;
7438 tempreg = treg;
7439 }
4d7206a2 7440 relax_end ();
f5040a92 7441 }
4d7206a2 7442 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 7443 {
4d7206a2 7444 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7445 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7446 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 7447 relax_switch ();
67c0d1eb
RS
7448 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7449 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 7450 relax_end ();
f5040a92 7451 }
4d7206a2 7452 else
f5040a92 7453 {
67c0d1eb
RS
7454 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7455 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
7456 }
7457 }
0a44bf69 7458 else if (mips_big_got && !HAVE_NEWABI)
252b5132 7459 {
67c0d1eb 7460 int gpdelay;
9117d219
NC
7461 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
7462 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 7463 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
7464
7465 /* This is the large GOT case. If this is a reference to an
7466 external symbol, and there is no constant, we want
7467 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7468 addu $tempreg,$tempreg,$gp
7469 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 7470 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
7471 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
7472 addu $tempreg,$tempreg,$gp
7473 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
7474 For a local symbol, we want
7475 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7476 nop
7477 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7478
7479 If we have a small constant, and this is a reference to
7480 an external symbol, we want
7481 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7482 addu $tempreg,$tempreg,$gp
7483 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7484 nop
7485 addiu $tempreg,$tempreg,<constant>
7486 For a local symbol, we want
7487 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7488 nop
7489 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
7490
7491 If we have a large constant, and this is a reference to
7492 an external symbol, we want
7493 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7494 addu $tempreg,$tempreg,$gp
7495 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7496 lui $at,<hiconstant>
7497 addiu $at,$at,<loconstant>
7498 addu $tempreg,$tempreg,$at
7499 For a local symbol, we want
7500 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7501 lui $at,<hiconstant>
7502 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
7503 addu $tempreg,$tempreg,$at
f5040a92 7504 */
438c16b8 7505
252b5132
RH
7506 expr1.X_add_number = offset_expr.X_add_number;
7507 offset_expr.X_add_number = 0;
4d7206a2 7508 relax_start (offset_expr.X_add_symbol);
67c0d1eb 7509 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
7510 if (expr1.X_add_number == 0 && breg == 0
7511 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
7512 {
7513 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
7514 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
7515 }
df58fc94 7516 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 7517 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7518 tempreg, tempreg, mips_gp_register);
67c0d1eb 7519 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 7520 tempreg, lw_reloc_type, tempreg);
252b5132
RH
7521 if (expr1.X_add_number == 0)
7522 {
67c0d1eb 7523 if (breg != 0)
252b5132
RH
7524 {
7525 /* We're going to put in an addu instruction using
7526 tempreg, so we may as well insert the nop right
7527 now. */
269137b2 7528 load_delay_nop ();
252b5132 7529 }
252b5132
RH
7530 }
7531 else if (expr1.X_add_number >= -0x8000
7532 && expr1.X_add_number < 0x8000)
7533 {
269137b2 7534 load_delay_nop ();
67c0d1eb 7535 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 7536 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
7537 }
7538 else
7539 {
252b5132
RH
7540 /* If we are going to add in a base register, and the
7541 target register and the base register are the same,
7542 then we are using AT as a temporary register. Since
7543 we want to load the constant into AT, we add our
7544 current AT (from the global offset table) and the
7545 register into the register now, and pretend we were
7546 not using a base register. */
7547 if (breg != treg)
67c0d1eb 7548 dreg = tempreg;
252b5132
RH
7549 else
7550 {
9c2799c2 7551 gas_assert (tempreg == AT);
269137b2 7552 load_delay_nop ();
67c0d1eb 7553 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7554 treg, AT, breg);
252b5132 7555 dreg = treg;
252b5132
RH
7556 }
7557
f6a22291 7558 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 7559 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 7560
252b5132
RH
7561 used_at = 1;
7562 }
43c0598f 7563 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
4d7206a2 7564 relax_switch ();
252b5132 7565
67c0d1eb 7566 if (gpdelay)
252b5132
RH
7567 {
7568 /* This is needed because this instruction uses $gp, but
f5040a92 7569 the first instruction on the main stream does not. */
67c0d1eb 7570 macro_build (NULL, "nop", "");
252b5132 7571 }
ed6fb7bd 7572
67c0d1eb
RS
7573 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7574 local_reloc_type, mips_gp_register);
f5040a92 7575 if (expr1.X_add_number >= -0x8000
252b5132
RH
7576 && expr1.X_add_number < 0x8000)
7577 {
269137b2 7578 load_delay_nop ();
67c0d1eb
RS
7579 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7580 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 7581 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
7582 register, the external symbol case ended with a load,
7583 so if the symbol turns out to not be external, and
7584 the next instruction uses tempreg, an unnecessary nop
7585 will be inserted. */
252b5132
RH
7586 }
7587 else
7588 {
7589 if (breg == treg)
7590 {
7591 /* We must add in the base register now, as in the
f5040a92 7592 external symbol case. */
9c2799c2 7593 gas_assert (tempreg == AT);
269137b2 7594 load_delay_nop ();
67c0d1eb 7595 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7596 treg, AT, breg);
252b5132
RH
7597 tempreg = treg;
7598 /* We set breg to 0 because we have arranged to add
f5040a92 7599 it in in both cases. */
252b5132
RH
7600 breg = 0;
7601 }
7602
67c0d1eb
RS
7603 macro_build_lui (&expr1, AT);
7604 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 7605 AT, AT, BFD_RELOC_LO16);
67c0d1eb 7606 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7607 tempreg, tempreg, AT);
8fc2e39e 7608 used_at = 1;
252b5132 7609 }
4d7206a2 7610 relax_end ();
252b5132 7611 }
0a44bf69 7612 else if (mips_big_got && HAVE_NEWABI)
f5040a92 7613 {
f5040a92
AO
7614 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
7615 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 7616 int add_breg_early = 0;
f5040a92
AO
7617
7618 /* This is the large GOT case. If this is a reference to an
7619 external symbol, and there is no constant, we want
7620 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7621 add $tempreg,$tempreg,$gp
7622 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 7623 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
7624 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
7625 add $tempreg,$tempreg,$gp
7626 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
7627
7628 If we have a small constant, and this is a reference to
7629 an external symbol, we want
7630 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7631 add $tempreg,$tempreg,$gp
7632 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7633 addi $tempreg,$tempreg,<constant>
7634
7635 If we have a large constant, and this is a reference to
7636 an external symbol, we want
7637 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7638 addu $tempreg,$tempreg,$gp
7639 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7640 lui $at,<hiconstant>
7641 addi $at,$at,<loconstant>
7642 add $tempreg,$tempreg,$at
7643
7644 If we have NewABI, and we know it's a local symbol, we want
7645 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
7646 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
7647 otherwise we have to resort to GOT_HI16/GOT_LO16. */
7648
4d7206a2 7649 relax_start (offset_expr.X_add_symbol);
f5040a92 7650
4d7206a2 7651 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
7652 offset_expr.X_add_number = 0;
7653
1abe91b1
MR
7654 if (expr1.X_add_number == 0 && breg == 0
7655 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
7656 {
7657 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
7658 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
7659 }
df58fc94 7660 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 7661 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7662 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
7663 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7664 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
7665
7666 if (expr1.X_add_number == 0)
4d7206a2 7667 ;
f5040a92
AO
7668 else if (expr1.X_add_number >= -0x8000
7669 && expr1.X_add_number < 0x8000)
7670 {
67c0d1eb 7671 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 7672 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 7673 }
ecd13cd3 7674 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 7675 {
f5040a92
AO
7676 /* If we are going to add in a base register, and the
7677 target register and the base register are the same,
7678 then we are using AT as a temporary register. Since
7679 we want to load the constant into AT, we add our
7680 current AT (from the global offset table) and the
7681 register into the register now, and pretend we were
7682 not using a base register. */
7683 if (breg != treg)
7684 dreg = tempreg;
7685 else
7686 {
9c2799c2 7687 gas_assert (tempreg == AT);
67c0d1eb 7688 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7689 treg, AT, breg);
f5040a92 7690 dreg = treg;
67c0d1eb 7691 add_breg_early = 1;
f5040a92
AO
7692 }
7693
f6a22291 7694 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 7695 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 7696
f5040a92
AO
7697 used_at = 1;
7698 }
7699 else
7700 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
7701
4d7206a2 7702 relax_switch ();
f5040a92 7703 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
7704 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7705 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
7706 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
7707 tempreg, BFD_RELOC_MIPS_GOT_OFST);
7708 if (add_breg_early)
f5040a92 7709 {
67c0d1eb 7710 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7711 treg, tempreg, breg);
f5040a92
AO
7712 breg = 0;
7713 tempreg = treg;
7714 }
4d7206a2 7715 relax_end ();
f5040a92 7716 }
252b5132
RH
7717 else
7718 abort ();
7719
7720 if (breg != 0)
aed1a261 7721 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
252b5132
RH
7722 break;
7723
52b6b6b9 7724 case M_MSGSND:
df58fc94 7725 gas_assert (!mips_opts.micromips);
52b6b6b9
JM
7726 {
7727 unsigned long temp = (treg << 16) | (0x01);
7728 macro_build (NULL, "c2", "C", temp);
7729 }
c7af4273 7730 break;
52b6b6b9
JM
7731
7732 case M_MSGLD:
df58fc94 7733 gas_assert (!mips_opts.micromips);
52b6b6b9
JM
7734 {
7735 unsigned long temp = (0x02);
7736 macro_build (NULL, "c2", "C", temp);
7737 }
c7af4273 7738 break;
52b6b6b9
JM
7739
7740 case M_MSGLD_T:
df58fc94 7741 gas_assert (!mips_opts.micromips);
52b6b6b9
JM
7742 {
7743 unsigned long temp = (treg << 16) | (0x02);
7744 macro_build (NULL, "c2", "C", temp);
7745 }
c7af4273 7746 break;
52b6b6b9
JM
7747
7748 case M_MSGWAIT:
df58fc94 7749 gas_assert (!mips_opts.micromips);
52b6b6b9 7750 macro_build (NULL, "c2", "C", 3);
c7af4273 7751 break;
52b6b6b9
JM
7752
7753 case M_MSGWAIT_T:
df58fc94 7754 gas_assert (!mips_opts.micromips);
52b6b6b9
JM
7755 {
7756 unsigned long temp = (treg << 16) | 0x03;
7757 macro_build (NULL, "c2", "C", temp);
7758 }
c7af4273 7759 break;
52b6b6b9 7760
252b5132
RH
7761 case M_J_A:
7762 /* The j instruction may not be used in PIC code, since it
7763 requires an absolute address. We convert it to a b
7764 instruction. */
7765 if (mips_pic == NO_PIC)
67c0d1eb 7766 macro_build (&offset_expr, "j", "a");
252b5132 7767 else
67c0d1eb 7768 macro_build (&offset_expr, "b", "p");
8fc2e39e 7769 break;
252b5132
RH
7770
7771 /* The jal instructions must be handled as macros because when
7772 generating PIC code they expand to multi-instruction
7773 sequences. Normally they are simple instructions. */
df58fc94
RS
7774 case M_JALS_1:
7775 dreg = RA;
7776 /* Fall through. */
7777 case M_JALS_2:
7778 gas_assert (mips_opts.micromips);
7779 jals = 1;
7780 goto jal;
252b5132
RH
7781 case M_JAL_1:
7782 dreg = RA;
7783 /* Fall through. */
7784 case M_JAL_2:
df58fc94 7785 jal:
3e722fb5 7786 if (mips_pic == NO_PIC)
df58fc94
RS
7787 {
7788 s = jals ? "jalrs" : "jalr";
e64af278
MR
7789 if (mips_opts.micromips
7790 && dreg == RA
7791 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
7792 macro_build (NULL, s, "mj", sreg);
7793 else
7794 macro_build (NULL, s, JALR_FMT, dreg, sreg);
7795 }
0a44bf69 7796 else
252b5132 7797 {
df58fc94
RS
7798 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
7799 && mips_cprestore_offset >= 0);
7800
252b5132
RH
7801 if (sreg != PIC_CALL_REG)
7802 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 7803
df58fc94
RS
7804 s = (mips_opts.micromips && (!mips_opts.noreorder || cprestore)
7805 ? "jalrs" : "jalr");
e64af278
MR
7806 if (mips_opts.micromips
7807 && dreg == RA
7808 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
7809 macro_build (NULL, s, "mj", sreg);
7810 else
7811 macro_build (NULL, s, JALR_FMT, dreg, sreg);
0a44bf69 7812 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
252b5132 7813 {
6478892d
TS
7814 if (mips_cprestore_offset < 0)
7815 as_warn (_("No .cprestore pseudo-op used in PIC code"));
7816 else
7817 {
90ecf173 7818 if (!mips_frame_reg_valid)
7a621144
DJ
7819 {
7820 as_warn (_("No .frame pseudo-op used in PIC code"));
7821 /* Quiet this warning. */
7822 mips_frame_reg_valid = 1;
7823 }
90ecf173 7824 if (!mips_cprestore_valid)
7a621144
DJ
7825 {
7826 as_warn (_("No .cprestore pseudo-op used in PIC code"));
7827 /* Quiet this warning. */
7828 mips_cprestore_valid = 1;
7829 }
d3fca0b5
MR
7830 if (mips_opts.noreorder)
7831 macro_build (NULL, "nop", "");
6478892d 7832 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 7833 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 7834 mips_gp_register,
256ab948
TS
7835 mips_frame_reg,
7836 HAVE_64BIT_ADDRESSES);
6478892d 7837 }
252b5132
RH
7838 }
7839 }
252b5132 7840
8fc2e39e 7841 break;
252b5132 7842
df58fc94
RS
7843 case M_JALS_A:
7844 gas_assert (mips_opts.micromips);
7845 jals = 1;
7846 /* Fall through. */
252b5132
RH
7847 case M_JAL_A:
7848 if (mips_pic == NO_PIC)
df58fc94 7849 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
252b5132
RH
7850 else if (mips_pic == SVR4_PIC)
7851 {
7852 /* If this is a reference to an external symbol, and we are
7853 using a small GOT, we want
7854 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
7855 nop
f9419b05 7856 jalr $ra,$25
252b5132
RH
7857 nop
7858 lw $gp,cprestore($sp)
7859 The cprestore value is set using the .cprestore
7860 pseudo-op. If we are using a big GOT, we want
7861 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
7862 addu $25,$25,$gp
7863 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
7864 nop
f9419b05 7865 jalr $ra,$25
252b5132
RH
7866 nop
7867 lw $gp,cprestore($sp)
7868 If the symbol is not external, we want
7869 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7870 nop
7871 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 7872 jalr $ra,$25
252b5132 7873 nop
438c16b8 7874 lw $gp,cprestore($sp)
f5040a92
AO
7875
7876 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
7877 sequences above, minus nops, unless the symbol is local,
7878 which enables us to use GOT_PAGE/GOT_OFST (big got) or
7879 GOT_DISP. */
438c16b8 7880 if (HAVE_NEWABI)
252b5132 7881 {
90ecf173 7882 if (!mips_big_got)
f5040a92 7883 {
4d7206a2 7884 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7885 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7886 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 7887 mips_gp_register);
4d7206a2 7888 relax_switch ();
67c0d1eb
RS
7889 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7890 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
7891 mips_gp_register);
7892 relax_end ();
f5040a92
AO
7893 }
7894 else
7895 {
4d7206a2 7896 relax_start (offset_expr.X_add_symbol);
df58fc94 7897 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
7898 BFD_RELOC_MIPS_CALL_HI16);
7899 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
7900 PIC_CALL_REG, mips_gp_register);
7901 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7902 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
7903 PIC_CALL_REG);
4d7206a2 7904 relax_switch ();
67c0d1eb
RS
7905 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7906 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
7907 mips_gp_register);
7908 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7909 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 7910 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 7911 relax_end ();
f5040a92 7912 }
684022ea 7913
df58fc94 7914 macro_build_jalr (&offset_expr, 0);
252b5132
RH
7915 }
7916 else
7917 {
4d7206a2 7918 relax_start (offset_expr.X_add_symbol);
90ecf173 7919 if (!mips_big_got)
438c16b8 7920 {
67c0d1eb
RS
7921 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7922 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 7923 mips_gp_register);
269137b2 7924 load_delay_nop ();
4d7206a2 7925 relax_switch ();
438c16b8 7926 }
252b5132 7927 else
252b5132 7928 {
67c0d1eb
RS
7929 int gpdelay;
7930
7931 gpdelay = reg_needs_delay (mips_gp_register);
df58fc94 7932 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
7933 BFD_RELOC_MIPS_CALL_HI16);
7934 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
7935 PIC_CALL_REG, mips_gp_register);
7936 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7937 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
7938 PIC_CALL_REG);
269137b2 7939 load_delay_nop ();
4d7206a2 7940 relax_switch ();
67c0d1eb
RS
7941 if (gpdelay)
7942 macro_build (NULL, "nop", "");
252b5132 7943 }
67c0d1eb
RS
7944 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7945 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 7946 mips_gp_register);
269137b2 7947 load_delay_nop ();
67c0d1eb
RS
7948 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7949 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 7950 relax_end ();
df58fc94 7951 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
438c16b8 7952
6478892d
TS
7953 if (mips_cprestore_offset < 0)
7954 as_warn (_("No .cprestore pseudo-op used in PIC code"));
7955 else
7956 {
90ecf173 7957 if (!mips_frame_reg_valid)
7a621144
DJ
7958 {
7959 as_warn (_("No .frame pseudo-op used in PIC code"));
7960 /* Quiet this warning. */
7961 mips_frame_reg_valid = 1;
7962 }
90ecf173 7963 if (!mips_cprestore_valid)
7a621144
DJ
7964 {
7965 as_warn (_("No .cprestore pseudo-op used in PIC code"));
7966 /* Quiet this warning. */
7967 mips_cprestore_valid = 1;
7968 }
6478892d 7969 if (mips_opts.noreorder)
67c0d1eb 7970 macro_build (NULL, "nop", "");
6478892d 7971 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 7972 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 7973 mips_gp_register,
256ab948
TS
7974 mips_frame_reg,
7975 HAVE_64BIT_ADDRESSES);
6478892d 7976 }
252b5132
RH
7977 }
7978 }
0a44bf69
RS
7979 else if (mips_pic == VXWORKS_PIC)
7980 as_bad (_("Non-PIC jump used in PIC library"));
252b5132
RH
7981 else
7982 abort ();
7983
8fc2e39e 7984 break;
252b5132 7985
dec0624d
MR
7986 case M_ACLR_AB:
7987 ab = 1;
7988 case M_ACLR_OB:
7989 s = "aclr";
7990 treg = EXTRACT_OPERAND (mips_opts.micromips, 3BITPOS, *ip);
7991 fmt = "\\,~(b)";
7992 off12 = 1;
7993 goto ld_st;
7994 case M_ASET_AB:
7995 ab = 1;
7996 case M_ASET_OB:
7997 s = "aset";
7998 treg = EXTRACT_OPERAND (mips_opts.micromips, 3BITPOS, *ip);
7999 fmt = "\\,~(b)";
8000 off12 = 1;
8001 goto ld_st;
252b5132 8002 case M_LB_AB:
df58fc94 8003 ab = 1;
252b5132 8004 s = "lb";
df58fc94 8005 fmt = "t,o(b)";
252b5132
RH
8006 goto ld;
8007 case M_LBU_AB:
df58fc94 8008 ab = 1;
252b5132 8009 s = "lbu";
df58fc94 8010 fmt = "t,o(b)";
252b5132
RH
8011 goto ld;
8012 case M_LH_AB:
df58fc94 8013 ab = 1;
252b5132 8014 s = "lh";
df58fc94 8015 fmt = "t,o(b)";
252b5132
RH
8016 goto ld;
8017 case M_LHU_AB:
df58fc94 8018 ab = 1;
252b5132 8019 s = "lhu";
df58fc94 8020 fmt = "t,o(b)";
252b5132
RH
8021 goto ld;
8022 case M_LW_AB:
df58fc94 8023 ab = 1;
252b5132 8024 s = "lw";
df58fc94 8025 fmt = "t,o(b)";
252b5132
RH
8026 goto ld;
8027 case M_LWC0_AB:
df58fc94
RS
8028 ab = 1;
8029 gas_assert (!mips_opts.micromips);
252b5132 8030 s = "lwc0";
df58fc94 8031 fmt = "E,o(b)";
bdaaa2e1 8032 /* Itbl support may require additional care here. */
252b5132 8033 coproc = 1;
df58fc94 8034 goto ld_st;
252b5132 8035 case M_LWC1_AB:
df58fc94 8036 ab = 1;
252b5132 8037 s = "lwc1";
df58fc94 8038 fmt = "T,o(b)";
bdaaa2e1 8039 /* Itbl support may require additional care here. */
252b5132 8040 coproc = 1;
df58fc94 8041 goto ld_st;
252b5132 8042 case M_LWC2_AB:
df58fc94
RS
8043 ab = 1;
8044 case M_LWC2_OB:
252b5132 8045 s = "lwc2";
df58fc94
RS
8046 fmt = COP12_FMT;
8047 off12 = mips_opts.micromips;
bdaaa2e1 8048 /* Itbl support may require additional care here. */
252b5132 8049 coproc = 1;
df58fc94 8050 goto ld_st;
252b5132 8051 case M_LWC3_AB:
df58fc94
RS
8052 ab = 1;
8053 gas_assert (!mips_opts.micromips);
252b5132 8054 s = "lwc3";
df58fc94 8055 fmt = "E,o(b)";
bdaaa2e1 8056 /* Itbl support may require additional care here. */
252b5132 8057 coproc = 1;
df58fc94 8058 goto ld_st;
252b5132 8059 case M_LWL_AB:
df58fc94
RS
8060 ab = 1;
8061 case M_LWL_OB:
252b5132 8062 s = "lwl";
df58fc94
RS
8063 fmt = MEM12_FMT;
8064 off12 = mips_opts.micromips;
8065 goto ld_st;
252b5132 8066 case M_LWR_AB:
df58fc94
RS
8067 ab = 1;
8068 case M_LWR_OB:
252b5132 8069 s = "lwr";
df58fc94
RS
8070 fmt = MEM12_FMT;
8071 off12 = mips_opts.micromips;
8072 goto ld_st;
252b5132 8073 case M_LDC1_AB:
df58fc94 8074 ab = 1;
252b5132 8075 s = "ldc1";
df58fc94 8076 fmt = "T,o(b)";
bdaaa2e1 8077 /* Itbl support may require additional care here. */
252b5132 8078 coproc = 1;
df58fc94 8079 goto ld_st;
252b5132 8080 case M_LDC2_AB:
df58fc94
RS
8081 ab = 1;
8082 case M_LDC2_OB:
252b5132 8083 s = "ldc2";
df58fc94
RS
8084 fmt = COP12_FMT;
8085 off12 = mips_opts.micromips;
bdaaa2e1 8086 /* Itbl support may require additional care here. */
252b5132 8087 coproc = 1;
df58fc94 8088 goto ld_st;
252b5132 8089 case M_LDC3_AB:
df58fc94 8090 ab = 1;
252b5132 8091 s = "ldc3";
df58fc94 8092 fmt = "E,o(b)";
bdaaa2e1 8093 /* Itbl support may require additional care here. */
252b5132 8094 coproc = 1;
df58fc94 8095 goto ld_st;
252b5132 8096 case M_LDL_AB:
df58fc94
RS
8097 ab = 1;
8098 case M_LDL_OB:
252b5132 8099 s = "ldl";
df58fc94
RS
8100 fmt = MEM12_FMT;
8101 off12 = mips_opts.micromips;
8102 goto ld_st;
252b5132 8103 case M_LDR_AB:
df58fc94
RS
8104 ab = 1;
8105 case M_LDR_OB:
252b5132 8106 s = "ldr";
df58fc94
RS
8107 fmt = MEM12_FMT;
8108 off12 = mips_opts.micromips;
8109 goto ld_st;
252b5132 8110 case M_LL_AB:
df58fc94
RS
8111 ab = 1;
8112 case M_LL_OB:
252b5132 8113 s = "ll";
df58fc94
RS
8114 fmt = MEM12_FMT;
8115 off12 = mips_opts.micromips;
252b5132
RH
8116 goto ld;
8117 case M_LLD_AB:
df58fc94
RS
8118 ab = 1;
8119 case M_LLD_OB:
252b5132 8120 s = "lld";
df58fc94
RS
8121 fmt = MEM12_FMT;
8122 off12 = mips_opts.micromips;
252b5132
RH
8123 goto ld;
8124 case M_LWU_AB:
df58fc94
RS
8125 ab = 1;
8126 case M_LWU_OB:
252b5132 8127 s = "lwu";
df58fc94
RS
8128 fmt = MEM12_FMT;
8129 off12 = mips_opts.micromips;
8130 goto ld;
8131 case M_LWP_AB:
8132 ab = 1;
8133 case M_LWP_OB:
8134 gas_assert (mips_opts.micromips);
8135 s = "lwp";
8136 fmt = "t,~(b)";
8137 off12 = 1;
8138 lp = 1;
8139 goto ld;
8140 case M_LDP_AB:
8141 ab = 1;
8142 case M_LDP_OB:
8143 gas_assert (mips_opts.micromips);
8144 s = "ldp";
8145 fmt = "t,~(b)";
8146 off12 = 1;
8147 lp = 1;
8148 goto ld;
8149 case M_LWM_AB:
8150 ab = 1;
8151 case M_LWM_OB:
8152 gas_assert (mips_opts.micromips);
8153 s = "lwm";
8154 fmt = "n,~(b)";
8155 off12 = 1;
8156 goto ld_st;
8157 case M_LDM_AB:
8158 ab = 1;
8159 case M_LDM_OB:
8160 gas_assert (mips_opts.micromips);
8161 s = "ldm";
8162 fmt = "n,~(b)";
8163 off12 = 1;
8164 goto ld_st;
8165
252b5132 8166 ld:
df58fc94
RS
8167 if (breg == treg + lp)
8168 goto ld_st;
252b5132 8169 else
df58fc94
RS
8170 tempreg = treg + lp;
8171 goto ld_noat;
8172
252b5132 8173 case M_SB_AB:
df58fc94 8174 ab = 1;
252b5132 8175 s = "sb";
df58fc94
RS
8176 fmt = "t,o(b)";
8177 goto ld_st;
252b5132 8178 case M_SH_AB:
df58fc94 8179 ab = 1;
252b5132 8180 s = "sh";
df58fc94
RS
8181 fmt = "t,o(b)";
8182 goto ld_st;
252b5132 8183 case M_SW_AB:
df58fc94 8184 ab = 1;
252b5132 8185 s = "sw";
df58fc94
RS
8186 fmt = "t,o(b)";
8187 goto ld_st;
252b5132 8188 case M_SWC0_AB:
df58fc94
RS
8189 ab = 1;
8190 gas_assert (!mips_opts.micromips);
252b5132 8191 s = "swc0";
df58fc94 8192 fmt = "E,o(b)";
bdaaa2e1 8193 /* Itbl support may require additional care here. */
252b5132 8194 coproc = 1;
df58fc94 8195 goto ld_st;
252b5132 8196 case M_SWC1_AB:
df58fc94 8197 ab = 1;
252b5132 8198 s = "swc1";
df58fc94 8199 fmt = "T,o(b)";
bdaaa2e1 8200 /* Itbl support may require additional care here. */
252b5132 8201 coproc = 1;
df58fc94 8202 goto ld_st;
252b5132 8203 case M_SWC2_AB:
df58fc94
RS
8204 ab = 1;
8205 case M_SWC2_OB:
252b5132 8206 s = "swc2";
df58fc94
RS
8207 fmt = COP12_FMT;
8208 off12 = mips_opts.micromips;
bdaaa2e1 8209 /* Itbl support may require additional care here. */
252b5132 8210 coproc = 1;
df58fc94 8211 goto ld_st;
252b5132 8212 case M_SWC3_AB:
df58fc94
RS
8213 ab = 1;
8214 gas_assert (!mips_opts.micromips);
252b5132 8215 s = "swc3";
df58fc94 8216 fmt = "E,o(b)";
bdaaa2e1 8217 /* Itbl support may require additional care here. */
252b5132 8218 coproc = 1;
df58fc94 8219 goto ld_st;
252b5132 8220 case M_SWL_AB:
df58fc94
RS
8221 ab = 1;
8222 case M_SWL_OB:
252b5132 8223 s = "swl";
df58fc94
RS
8224 fmt = MEM12_FMT;
8225 off12 = mips_opts.micromips;
8226 goto ld_st;
252b5132 8227 case M_SWR_AB:
df58fc94
RS
8228 ab = 1;
8229 case M_SWR_OB:
252b5132 8230 s = "swr";
df58fc94
RS
8231 fmt = MEM12_FMT;
8232 off12 = mips_opts.micromips;
8233 goto ld_st;
252b5132 8234 case M_SC_AB:
df58fc94
RS
8235 ab = 1;
8236 case M_SC_OB:
252b5132 8237 s = "sc";
df58fc94
RS
8238 fmt = MEM12_FMT;
8239 off12 = mips_opts.micromips;
8240 goto ld_st;
252b5132 8241 case M_SCD_AB:
df58fc94
RS
8242 ab = 1;
8243 case M_SCD_OB:
252b5132 8244 s = "scd";
df58fc94
RS
8245 fmt = MEM12_FMT;
8246 off12 = mips_opts.micromips;
8247 goto ld_st;
d43b4baf 8248 case M_CACHE_AB:
df58fc94
RS
8249 ab = 1;
8250 case M_CACHE_OB:
d43b4baf 8251 s = "cache";
df58fc94
RS
8252 fmt = mips_opts.micromips ? "k,~(b)" : "k,o(b)";
8253 off12 = mips_opts.micromips;
8254 goto ld_st;
3eebd5eb 8255 case M_PREF_AB:
df58fc94
RS
8256 ab = 1;
8257 case M_PREF_OB:
3eebd5eb 8258 s = "pref";
df58fc94
RS
8259 fmt = !mips_opts.micromips ? "k,o(b)" : "k,~(b)";
8260 off12 = mips_opts.micromips;
8261 goto ld_st;
252b5132 8262 case M_SDC1_AB:
df58fc94 8263 ab = 1;
252b5132 8264 s = "sdc1";
df58fc94 8265 fmt = "T,o(b)";
252b5132 8266 coproc = 1;
bdaaa2e1 8267 /* Itbl support may require additional care here. */
df58fc94 8268 goto ld_st;
252b5132 8269 case M_SDC2_AB:
df58fc94
RS
8270 ab = 1;
8271 case M_SDC2_OB:
252b5132 8272 s = "sdc2";
df58fc94
RS
8273 fmt = COP12_FMT;
8274 off12 = mips_opts.micromips;
bdaaa2e1 8275 /* Itbl support may require additional care here. */
252b5132 8276 coproc = 1;
df58fc94 8277 goto ld_st;
252b5132 8278 case M_SDC3_AB:
df58fc94
RS
8279 ab = 1;
8280 gas_assert (!mips_opts.micromips);
252b5132 8281 s = "sdc3";
df58fc94 8282 fmt = "E,o(b)";
bdaaa2e1 8283 /* Itbl support may require additional care here. */
252b5132 8284 coproc = 1;
df58fc94 8285 goto ld_st;
252b5132 8286 case M_SDL_AB:
df58fc94
RS
8287 ab = 1;
8288 case M_SDL_OB:
252b5132 8289 s = "sdl";
df58fc94
RS
8290 fmt = MEM12_FMT;
8291 off12 = mips_opts.micromips;
8292 goto ld_st;
252b5132 8293 case M_SDR_AB:
df58fc94
RS
8294 ab = 1;
8295 case M_SDR_OB:
252b5132 8296 s = "sdr";
df58fc94
RS
8297 fmt = MEM12_FMT;
8298 off12 = mips_opts.micromips;
8299 goto ld_st;
8300 case M_SWP_AB:
8301 ab = 1;
8302 case M_SWP_OB:
8303 gas_assert (mips_opts.micromips);
8304 s = "swp";
8305 fmt = "t,~(b)";
8306 off12 = 1;
8307 goto ld_st;
8308 case M_SDP_AB:
8309 ab = 1;
8310 case M_SDP_OB:
8311 gas_assert (mips_opts.micromips);
8312 s = "sdp";
8313 fmt = "t,~(b)";
8314 off12 = 1;
8315 goto ld_st;
8316 case M_SWM_AB:
8317 ab = 1;
8318 case M_SWM_OB:
8319 gas_assert (mips_opts.micromips);
8320 s = "swm";
8321 fmt = "n,~(b)";
8322 off12 = 1;
8323 goto ld_st;
8324 case M_SDM_AB:
8325 ab = 1;
8326 case M_SDM_OB:
8327 gas_assert (mips_opts.micromips);
8328 s = "sdm";
8329 fmt = "n,~(b)";
8330 off12 = 1;
8331
8332 ld_st:
8fc2e39e
TS
8333 tempreg = AT;
8334 used_at = 1;
df58fc94 8335 ld_noat:
252b5132
RH
8336 if (offset_expr.X_op != O_constant
8337 && offset_expr.X_op != O_symbol)
8338 {
f71d0d44 8339 as_bad (_("Expression too complex"));
252b5132
RH
8340 offset_expr.X_op = O_constant;
8341 }
8342
2051e8c4
MR
8343 if (HAVE_32BIT_ADDRESSES
8344 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
8345 {
8346 char value [32];
8347
8348 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 8349 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 8350 }
2051e8c4 8351
252b5132
RH
8352 /* A constant expression in PIC code can be handled just as it
8353 is in non PIC code. */
aed1a261
RS
8354 if (offset_expr.X_op == O_constant)
8355 {
df58fc94
RS
8356 int hipart = 0;
8357
842f8b2a 8358 expr1.X_add_number = offset_expr.X_add_number;
2051e8c4 8359 normalize_address_expr (&expr1);
df58fc94 8360 if (!off12 && !IS_SEXT_16BIT_NUM (expr1.X_add_number))
842f8b2a
MR
8361 {
8362 expr1.X_add_number = ((expr1.X_add_number + 0x8000)
8363 & ~(bfd_vma) 0xffff);
df58fc94
RS
8364 hipart = 1;
8365 }
8366 else if (off12 && !IS_SEXT_12BIT_NUM (expr1.X_add_number))
8367 {
8368 expr1.X_add_number = ((expr1.X_add_number + 0x800)
8369 & ~(bfd_vma) 0xfff);
8370 hipart = 1;
8371 }
8372 if (hipart)
8373 {
842f8b2a
MR
8374 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
8375 if (breg != 0)
8376 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8377 tempreg, tempreg, breg);
8378 breg = tempreg;
8379 }
dd6a37e7
AP
8380 if (off0)
8381 {
8382 if (offset_expr.X_add_number == 0)
8383 tempreg = breg;
8384 else
8385 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
8386 "t,r,j", tempreg, breg, BFD_RELOC_LO16);
8387 macro_build (NULL, s, fmt, treg, tempreg);
8388 }
8389 else if (!off12)
df58fc94
RS
8390 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, breg);
8391 else
8392 macro_build (NULL, s, fmt,
8393 treg, (unsigned long) offset_expr.X_add_number, breg);
8394 }
dd6a37e7 8395 else if (off12 || off0)
df58fc94 8396 {
dd6a37e7
AP
8397 /* A 12-bit or 0-bit offset field is too narrow to be used
8398 for a low-part relocation, so load the whole address into
8399 the auxillary register. In the case of "A(b)" addresses,
8400 we first load absolute address "A" into the register and
8401 then add base register "b". In the case of "o(b)" addresses,
8402 we simply need to add 16-bit offset "o" to base register "b", and
df58fc94
RS
8403 offset_reloc already contains the relocations associated
8404 with "o". */
8405 if (ab)
8406 {
8407 load_address (tempreg, &offset_expr, &used_at);
8408 if (breg != 0)
8409 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8410 tempreg, tempreg, breg);
8411 }
8412 else
8413 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8414 tempreg, breg, -1,
8415 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8416 expr1.X_add_number = 0;
dd6a37e7
AP
8417 if (off0)
8418 macro_build (NULL, s, fmt, treg, tempreg);
8419 else
8420 macro_build (NULL, s, fmt,
8421 treg, (unsigned long) expr1.X_add_number, tempreg);
aed1a261
RS
8422 }
8423 else if (mips_pic == NO_PIC)
252b5132
RH
8424 {
8425 /* If this is a reference to a GP relative symbol, and there
8426 is no base register, we want
cdf6fd85 8427 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
8428 Otherwise, if there is no base register, we want
8429 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
8430 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8431 If we have a constant, we need two instructions anyhow,
8432 so we always use the latter form.
8433
8434 If we have a base register, and this is a reference to a
8435 GP relative symbol, we want
8436 addu $tempreg,$breg,$gp
cdf6fd85 8437 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
8438 Otherwise we want
8439 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
8440 addu $tempreg,$tempreg,$breg
8441 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 8442 With a constant we always use the latter case.
76b3015f 8443
d6bc6245
TS
8444 With 64bit address space and no base register and $at usable,
8445 we want
8446 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8447 lui $at,<sym> (BFD_RELOC_HI16_S)
8448 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8449 dsll32 $tempreg,0
8450 daddu $tempreg,$at
8451 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8452 If we have a base register, we want
8453 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8454 lui $at,<sym> (BFD_RELOC_HI16_S)
8455 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8456 daddu $at,$breg
8457 dsll32 $tempreg,0
8458 daddu $tempreg,$at
8459 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8460
8461 Without $at we can't generate the optimal path for superscalar
8462 processors here since this would require two temporary registers.
8463 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8464 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8465 dsll $tempreg,16
8466 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
8467 dsll $tempreg,16
8468 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8469 If we have a base register, we want
8470 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8471 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8472 dsll $tempreg,16
8473 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
8474 dsll $tempreg,16
8475 daddu $tempreg,$tempreg,$breg
8476 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 8477
6caf9ef4 8478 For GP relative symbols in 64bit address space we can use
aed1a261
RS
8479 the same sequence as in 32bit address space. */
8480 if (HAVE_64BIT_SYMBOLS)
d6bc6245 8481 {
aed1a261 8482 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
8483 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
8484 {
8485 relax_start (offset_expr.X_add_symbol);
8486 if (breg == 0)
8487 {
8488 macro_build (&offset_expr, s, fmt, treg,
8489 BFD_RELOC_GPREL16, mips_gp_register);
8490 }
8491 else
8492 {
8493 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8494 tempreg, breg, mips_gp_register);
8495 macro_build (&offset_expr, s, fmt, treg,
8496 BFD_RELOC_GPREL16, tempreg);
8497 }
8498 relax_switch ();
8499 }
d6bc6245 8500
741fe287 8501 if (used_at == 0 && mips_opts.at)
d6bc6245 8502 {
df58fc94 8503 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb 8504 BFD_RELOC_MIPS_HIGHEST);
df58fc94 8505 macro_build (&offset_expr, "lui", LUI_FMT, AT,
67c0d1eb
RS
8506 BFD_RELOC_HI16_S);
8507 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8508 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 8509 if (breg != 0)
67c0d1eb 8510 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
df58fc94 8511 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb
RS
8512 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
8513 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
8514 tempreg);
d6bc6245
TS
8515 used_at = 1;
8516 }
8517 else
8518 {
df58fc94 8519 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb
RS
8520 BFD_RELOC_MIPS_HIGHEST);
8521 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8522 tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 8523 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb
RS
8524 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8525 tempreg, BFD_RELOC_HI16_S);
df58fc94 8526 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
d6bc6245 8527 if (breg != 0)
67c0d1eb 8528 macro_build (NULL, "daddu", "d,v,t",
17a2f251 8529 tempreg, tempreg, breg);
67c0d1eb 8530 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8531 BFD_RELOC_LO16, tempreg);
d6bc6245 8532 }
6caf9ef4
TS
8533
8534 if (mips_relax.sequence)
8535 relax_end ();
8fc2e39e 8536 break;
d6bc6245 8537 }
256ab948 8538
252b5132
RH
8539 if (breg == 0)
8540 {
67c0d1eb 8541 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 8542 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 8543 {
4d7206a2 8544 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
8545 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
8546 mips_gp_register);
4d7206a2 8547 relax_switch ();
252b5132 8548 }
67c0d1eb
RS
8549 macro_build_lui (&offset_expr, tempreg);
8550 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8551 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
8552 if (mips_relax.sequence)
8553 relax_end ();
252b5132
RH
8554 }
8555 else
8556 {
67c0d1eb 8557 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 8558 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 8559 {
4d7206a2 8560 relax_start (offset_expr.X_add_symbol);
67c0d1eb 8561 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8562 tempreg, breg, mips_gp_register);
67c0d1eb 8563 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8564 BFD_RELOC_GPREL16, tempreg);
4d7206a2 8565 relax_switch ();
252b5132 8566 }
67c0d1eb
RS
8567 macro_build_lui (&offset_expr, tempreg);
8568 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8569 tempreg, tempreg, breg);
67c0d1eb 8570 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8571 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
8572 if (mips_relax.sequence)
8573 relax_end ();
252b5132
RH
8574 }
8575 }
0a44bf69 8576 else if (!mips_big_got)
252b5132 8577 {
ed6fb7bd 8578 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 8579
252b5132
RH
8580 /* If this is a reference to an external symbol, we want
8581 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8582 nop
8583 <op> $treg,0($tempreg)
8584 Otherwise we want
8585 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8586 nop
8587 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
8588 <op> $treg,0($tempreg)
f5040a92
AO
8589
8590 For NewABI, we want
8591 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
8592 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
8593
252b5132
RH
8594 If there is a base register, we add it to $tempreg before
8595 the <op>. If there is a constant, we stick it in the
8596 <op> instruction. We don't handle constants larger than
8597 16 bits, because we have no way to load the upper 16 bits
8598 (actually, we could handle them for the subset of cases
8599 in which we are not using $at). */
9c2799c2 8600 gas_assert (offset_expr.X_op == O_symbol);
f5040a92
AO
8601 if (HAVE_NEWABI)
8602 {
67c0d1eb
RS
8603 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8604 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 8605 if (breg != 0)
67c0d1eb 8606 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8607 tempreg, tempreg, breg);
67c0d1eb 8608 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8609 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
8610 break;
8611 }
252b5132
RH
8612 expr1.X_add_number = offset_expr.X_add_number;
8613 offset_expr.X_add_number = 0;
8614 if (expr1.X_add_number < -0x8000
8615 || expr1.X_add_number >= 0x8000)
8616 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
8617 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8618 lw_reloc_type, mips_gp_register);
269137b2 8619 load_delay_nop ();
4d7206a2
RS
8620 relax_start (offset_expr.X_add_symbol);
8621 relax_switch ();
67c0d1eb
RS
8622 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
8623 tempreg, BFD_RELOC_LO16);
4d7206a2 8624 relax_end ();
252b5132 8625 if (breg != 0)
67c0d1eb 8626 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8627 tempreg, tempreg, breg);
67c0d1eb 8628 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 8629 }
0a44bf69 8630 else if (mips_big_got && !HAVE_NEWABI)
252b5132 8631 {
67c0d1eb 8632 int gpdelay;
252b5132
RH
8633
8634 /* If this is a reference to an external symbol, we want
8635 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
8636 addu $tempreg,$tempreg,$gp
8637 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
8638 <op> $treg,0($tempreg)
8639 Otherwise we want
8640 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8641 nop
8642 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
8643 <op> $treg,0($tempreg)
8644 If there is a base register, we add it to $tempreg before
8645 the <op>. If there is a constant, we stick it in the
8646 <op> instruction. We don't handle constants larger than
8647 16 bits, because we have no way to load the upper 16 bits
8648 (actually, we could handle them for the subset of cases
f5040a92 8649 in which we are not using $at). */
9c2799c2 8650 gas_assert (offset_expr.X_op == O_symbol);
252b5132
RH
8651 expr1.X_add_number = offset_expr.X_add_number;
8652 offset_expr.X_add_number = 0;
8653 if (expr1.X_add_number < -0x8000
8654 || expr1.X_add_number >= 0x8000)
8655 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 8656 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 8657 relax_start (offset_expr.X_add_symbol);
df58fc94 8658 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 8659 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
8660 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
8661 mips_gp_register);
8662 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8663 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 8664 relax_switch ();
67c0d1eb
RS
8665 if (gpdelay)
8666 macro_build (NULL, "nop", "");
8667 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8668 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 8669 load_delay_nop ();
67c0d1eb
RS
8670 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
8671 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
8672 relax_end ();
8673
252b5132 8674 if (breg != 0)
67c0d1eb 8675 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8676 tempreg, tempreg, breg);
67c0d1eb 8677 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 8678 }
0a44bf69 8679 else if (mips_big_got && HAVE_NEWABI)
f5040a92 8680 {
f5040a92
AO
8681 /* If this is a reference to an external symbol, we want
8682 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
8683 add $tempreg,$tempreg,$gp
8684 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
8685 <op> $treg,<ofst>($tempreg)
8686 Otherwise, for local symbols, we want:
8687 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
8688 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
9c2799c2 8689 gas_assert (offset_expr.X_op == O_symbol);
4d7206a2 8690 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
8691 offset_expr.X_add_number = 0;
8692 if (expr1.X_add_number < -0x8000
8693 || expr1.X_add_number >= 0x8000)
8694 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 8695 relax_start (offset_expr.X_add_symbol);
df58fc94 8696 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 8697 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
8698 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
8699 mips_gp_register);
8700 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8701 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 8702 if (breg != 0)
67c0d1eb 8703 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8704 tempreg, tempreg, breg);
67c0d1eb 8705 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
684022ea 8706
4d7206a2 8707 relax_switch ();
f5040a92 8708 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
8709 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8710 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 8711 if (breg != 0)
67c0d1eb 8712 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8713 tempreg, tempreg, breg);
67c0d1eb 8714 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8715 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 8716 relax_end ();
f5040a92 8717 }
252b5132
RH
8718 else
8719 abort ();
8720
252b5132
RH
8721 break;
8722
8723 case M_LI:
8724 case M_LI_S:
67c0d1eb 8725 load_register (treg, &imm_expr, 0);
8fc2e39e 8726 break;
252b5132
RH
8727
8728 case M_DLI:
67c0d1eb 8729 load_register (treg, &imm_expr, 1);
8fc2e39e 8730 break;
252b5132
RH
8731
8732 case M_LI_SS:
8733 if (imm_expr.X_op == O_constant)
8734 {
8fc2e39e 8735 used_at = 1;
67c0d1eb
RS
8736 load_register (AT, &imm_expr, 0);
8737 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
8738 break;
8739 }
8740 else
8741 {
9c2799c2 8742 gas_assert (offset_expr.X_op == O_symbol
90ecf173
MR
8743 && strcmp (segment_name (S_GET_SEGMENT
8744 (offset_expr.X_add_symbol)),
8745 ".lit4") == 0
8746 && offset_expr.X_add_number == 0);
67c0d1eb 8747 macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
17a2f251 8748 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 8749 break;
252b5132
RH
8750 }
8751
8752 case M_LI_D:
ca4e0257
RS
8753 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
8754 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
8755 order 32 bits of the value and the low order 32 bits are either
8756 zero or in OFFSET_EXPR. */
252b5132
RH
8757 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
8758 {
ca4e0257 8759 if (HAVE_64BIT_GPRS)
67c0d1eb 8760 load_register (treg, &imm_expr, 1);
252b5132
RH
8761 else
8762 {
8763 int hreg, lreg;
8764
8765 if (target_big_endian)
8766 {
8767 hreg = treg;
8768 lreg = treg + 1;
8769 }
8770 else
8771 {
8772 hreg = treg + 1;
8773 lreg = treg;
8774 }
8775
8776 if (hreg <= 31)
67c0d1eb 8777 load_register (hreg, &imm_expr, 0);
252b5132
RH
8778 if (lreg <= 31)
8779 {
8780 if (offset_expr.X_op == O_absent)
67c0d1eb 8781 move_register (lreg, 0);
252b5132
RH
8782 else
8783 {
9c2799c2 8784 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 8785 load_register (lreg, &offset_expr, 0);
252b5132
RH
8786 }
8787 }
8788 }
8fc2e39e 8789 break;
252b5132
RH
8790 }
8791
8792 /* We know that sym is in the .rdata section. First we get the
8793 upper 16 bits of the address. */
8794 if (mips_pic == NO_PIC)
8795 {
67c0d1eb 8796 macro_build_lui (&offset_expr, AT);
8fc2e39e 8797 used_at = 1;
252b5132 8798 }
0a44bf69 8799 else
252b5132 8800 {
67c0d1eb
RS
8801 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
8802 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 8803 used_at = 1;
252b5132 8804 }
bdaaa2e1 8805
252b5132 8806 /* Now we load the register(s). */
ca4e0257 8807 if (HAVE_64BIT_GPRS)
8fc2e39e
TS
8808 {
8809 used_at = 1;
8810 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
8811 }
252b5132
RH
8812 else
8813 {
8fc2e39e 8814 used_at = 1;
67c0d1eb 8815 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
f9419b05 8816 if (treg != RA)
252b5132
RH
8817 {
8818 /* FIXME: How in the world do we deal with the possible
8819 overflow here? */
8820 offset_expr.X_add_number += 4;
67c0d1eb 8821 macro_build (&offset_expr, "lw", "t,o(b)",
17a2f251 8822 treg + 1, BFD_RELOC_LO16, AT);
252b5132
RH
8823 }
8824 }
252b5132
RH
8825 break;
8826
8827 case M_LI_DD:
ca4e0257
RS
8828 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
8829 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
8830 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
8831 the value and the low order 32 bits are either zero or in
8832 OFFSET_EXPR. */
252b5132
RH
8833 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
8834 {
8fc2e39e 8835 used_at = 1;
67c0d1eb 8836 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
ca4e0257
RS
8837 if (HAVE_64BIT_FPRS)
8838 {
9c2799c2 8839 gas_assert (HAVE_64BIT_GPRS);
67c0d1eb 8840 macro_build (NULL, "dmtc1", "t,S", AT, treg);
ca4e0257 8841 }
252b5132
RH
8842 else
8843 {
67c0d1eb 8844 macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
252b5132 8845 if (offset_expr.X_op == O_absent)
67c0d1eb 8846 macro_build (NULL, "mtc1", "t,G", 0, treg);
252b5132
RH
8847 else
8848 {
9c2799c2 8849 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb
RS
8850 load_register (AT, &offset_expr, 0);
8851 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
8852 }
8853 }
8854 break;
8855 }
8856
9c2799c2 8857 gas_assert (offset_expr.X_op == O_symbol
90ecf173 8858 && offset_expr.X_add_number == 0);
252b5132
RH
8859 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
8860 if (strcmp (s, ".lit8") == 0)
8861 {
df58fc94 8862 if (mips_opts.isa != ISA_MIPS1 || mips_opts.micromips)
252b5132 8863 {
67c0d1eb 8864 macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
17a2f251 8865 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 8866 break;
252b5132 8867 }
c9914766 8868 breg = mips_gp_register;
252b5132
RH
8869 r = BFD_RELOC_MIPS_LITERAL;
8870 goto dob;
8871 }
8872 else
8873 {
9c2799c2 8874 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 8875 used_at = 1;
0a44bf69 8876 if (mips_pic != NO_PIC)
67c0d1eb
RS
8877 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
8878 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
8879 else
8880 {
8881 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 8882 macro_build_lui (&offset_expr, AT);
252b5132 8883 }
bdaaa2e1 8884
df58fc94 8885 if (mips_opts.isa != ISA_MIPS1 || mips_opts.micromips)
252b5132 8886 {
67c0d1eb
RS
8887 macro_build (&offset_expr, "ldc1", "T,o(b)",
8888 treg, BFD_RELOC_LO16, AT);
252b5132
RH
8889 break;
8890 }
8891 breg = AT;
8892 r = BFD_RELOC_LO16;
8893 goto dob;
8894 }
8895
8896 case M_L_DOB:
252b5132
RH
8897 /* Even on a big endian machine $fn comes before $fn+1. We have
8898 to adjust when loading from memory. */
8899 r = BFD_RELOC_LO16;
8900 dob:
df58fc94 8901 gas_assert (!mips_opts.micromips);
9c2799c2 8902 gas_assert (mips_opts.isa == ISA_MIPS1);
67c0d1eb 8903 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 8904 target_big_endian ? treg + 1 : treg, r, breg);
252b5132
RH
8905 /* FIXME: A possible overflow which I don't know how to deal
8906 with. */
8907 offset_expr.X_add_number += 4;
67c0d1eb 8908 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 8909 target_big_endian ? treg : treg + 1, r, breg);
252b5132
RH
8910 break;
8911
c4a68bea 8912 case M_S_DOB:
df58fc94 8913 gas_assert (!mips_opts.micromips);
c4a68bea
MR
8914 gas_assert (mips_opts.isa == ISA_MIPS1);
8915 /* Even on a big endian machine $fn comes before $fn+1. We have
8916 to adjust when storing to memory. */
8917 macro_build (&offset_expr, "swc1", "T,o(b)",
8918 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
8919 offset_expr.X_add_number += 4;
8920 macro_build (&offset_expr, "swc1", "T,o(b)",
8921 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
8922 break;
8923
252b5132 8924 case M_L_DAB:
df58fc94 8925 gas_assert (!mips_opts.micromips);
252b5132
RH
8926 /*
8927 * The MIPS assembler seems to check for X_add_number not
8928 * being double aligned and generating:
8929 * lui at,%hi(foo+1)
8930 * addu at,at,v1
8931 * addiu at,at,%lo(foo+1)
8932 * lwc1 f2,0(at)
8933 * lwc1 f3,4(at)
8934 * But, the resulting address is the same after relocation so why
8935 * generate the extra instruction?
8936 */
bdaaa2e1 8937 /* Itbl support may require additional care here. */
252b5132 8938 coproc = 1;
df58fc94 8939 fmt = "T,o(b)";
e7af610e 8940 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
8941 {
8942 s = "ldc1";
df58fc94 8943 goto ld_st;
252b5132 8944 }
252b5132 8945 s = "lwc1";
252b5132
RH
8946 goto ldd_std;
8947
8948 case M_S_DAB:
df58fc94
RS
8949 gas_assert (!mips_opts.micromips);
8950 /* Itbl support may require additional care here. */
8951 coproc = 1;
8952 fmt = "T,o(b)";
e7af610e 8953 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
8954 {
8955 s = "sdc1";
df58fc94 8956 goto ld_st;
252b5132 8957 }
252b5132 8958 s = "swc1";
252b5132
RH
8959 goto ldd_std;
8960
8961 case M_LD_AB:
df58fc94 8962 fmt = "t,o(b)";
ca4e0257 8963 if (HAVE_64BIT_GPRS)
252b5132
RH
8964 {
8965 s = "ld";
8966 goto ld;
8967 }
252b5132 8968 s = "lw";
252b5132
RH
8969 goto ldd_std;
8970
8971 case M_SD_AB:
df58fc94 8972 fmt = "t,o(b)";
ca4e0257 8973 if (HAVE_64BIT_GPRS)
252b5132
RH
8974 {
8975 s = "sd";
df58fc94 8976 goto ld_st;
252b5132 8977 }
252b5132 8978 s = "sw";
252b5132
RH
8979
8980 ldd_std:
8981 if (offset_expr.X_op != O_symbol
8982 && offset_expr.X_op != O_constant)
8983 {
f71d0d44 8984 as_bad (_("Expression too complex"));
252b5132
RH
8985 offset_expr.X_op = O_constant;
8986 }
8987
2051e8c4
MR
8988 if (HAVE_32BIT_ADDRESSES
8989 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
8990 {
8991 char value [32];
8992
8993 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 8994 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 8995 }
2051e8c4 8996
252b5132
RH
8997 /* Even on a big endian machine $fn comes before $fn+1. We have
8998 to adjust when loading from memory. We set coproc if we must
8999 load $fn+1 first. */
bdaaa2e1 9000 /* Itbl support may require additional care here. */
90ecf173 9001 if (!target_big_endian)
252b5132
RH
9002 coproc = 0;
9003
90ecf173 9004 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
252b5132
RH
9005 {
9006 /* If this is a reference to a GP relative symbol, we want
cdf6fd85
TS
9007 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
9008 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
9009 If we have a base register, we use this
9010 addu $at,$breg,$gp
cdf6fd85
TS
9011 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
9012 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
9013 If this is not a GP relative symbol, we want
9014 lui $at,<sym> (BFD_RELOC_HI16_S)
9015 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
9016 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
9017 If there is a base register, we add it to $at after the
9018 lui instruction. If there is a constant, we always use
9019 the last case. */
39a59cf8
MR
9020 if (offset_expr.X_op == O_symbol
9021 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 9022 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 9023 {
4d7206a2 9024 relax_start (offset_expr.X_add_symbol);
252b5132
RH
9025 if (breg == 0)
9026 {
c9914766 9027 tempreg = mips_gp_register;
252b5132
RH
9028 }
9029 else
9030 {
67c0d1eb 9031 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9032 AT, breg, mips_gp_register);
252b5132 9033 tempreg = AT;
252b5132
RH
9034 used_at = 1;
9035 }
9036
beae10d5 9037 /* Itbl support may require additional care here. */
67c0d1eb 9038 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 9039 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
9040 offset_expr.X_add_number += 4;
9041
9042 /* Set mips_optimize to 2 to avoid inserting an
9043 undesired nop. */
9044 hold_mips_optimize = mips_optimize;
9045 mips_optimize = 2;
beae10d5 9046 /* Itbl support may require additional care here. */
67c0d1eb 9047 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 9048 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
9049 mips_optimize = hold_mips_optimize;
9050
4d7206a2 9051 relax_switch ();
252b5132 9052
0970e49e 9053 offset_expr.X_add_number -= 4;
252b5132 9054 }
8fc2e39e 9055 used_at = 1;
67c0d1eb 9056 macro_build_lui (&offset_expr, AT);
252b5132 9057 if (breg != 0)
67c0d1eb 9058 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 9059 /* Itbl support may require additional care here. */
67c0d1eb 9060 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 9061 BFD_RELOC_LO16, AT);
252b5132
RH
9062 /* FIXME: How do we handle overflow here? */
9063 offset_expr.X_add_number += 4;
beae10d5 9064 /* Itbl support may require additional care here. */
67c0d1eb 9065 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 9066 BFD_RELOC_LO16, AT);
4d7206a2
RS
9067 if (mips_relax.sequence)
9068 relax_end ();
bdaaa2e1 9069 }
0a44bf69 9070 else if (!mips_big_got)
252b5132 9071 {
252b5132
RH
9072 /* If this is a reference to an external symbol, we want
9073 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9074 nop
9075 <op> $treg,0($at)
9076 <op> $treg+1,4($at)
9077 Otherwise we want
9078 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9079 nop
9080 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
9081 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
9082 If there is a base register we add it to $at before the
9083 lwc1 instructions. If there is a constant we include it
9084 in the lwc1 instructions. */
9085 used_at = 1;
9086 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
9087 if (expr1.X_add_number < -0x8000
9088 || expr1.X_add_number >= 0x8000 - 4)
9089 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 9090 load_got_offset (AT, &offset_expr);
269137b2 9091 load_delay_nop ();
252b5132 9092 if (breg != 0)
67c0d1eb 9093 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
9094
9095 /* Set mips_optimize to 2 to avoid inserting an undesired
9096 nop. */
9097 hold_mips_optimize = mips_optimize;
9098 mips_optimize = 2;
4d7206a2 9099
beae10d5 9100 /* Itbl support may require additional care here. */
4d7206a2 9101 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
9102 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
9103 BFD_RELOC_LO16, AT);
4d7206a2 9104 expr1.X_add_number += 4;
67c0d1eb
RS
9105 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
9106 BFD_RELOC_LO16, AT);
4d7206a2 9107 relax_switch ();
67c0d1eb
RS
9108 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9109 BFD_RELOC_LO16, AT);
4d7206a2 9110 offset_expr.X_add_number += 4;
67c0d1eb
RS
9111 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9112 BFD_RELOC_LO16, AT);
4d7206a2 9113 relax_end ();
252b5132 9114
4d7206a2 9115 mips_optimize = hold_mips_optimize;
252b5132 9116 }
0a44bf69 9117 else if (mips_big_got)
252b5132 9118 {
67c0d1eb 9119 int gpdelay;
252b5132
RH
9120
9121 /* If this is a reference to an external symbol, we want
9122 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9123 addu $at,$at,$gp
9124 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
9125 nop
9126 <op> $treg,0($at)
9127 <op> $treg+1,4($at)
9128 Otherwise we want
9129 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9130 nop
9131 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
9132 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
9133 If there is a base register we add it to $at before the
9134 lwc1 instructions. If there is a constant we include it
9135 in the lwc1 instructions. */
9136 used_at = 1;
9137 expr1.X_add_number = offset_expr.X_add_number;
9138 offset_expr.X_add_number = 0;
9139 if (expr1.X_add_number < -0x8000
9140 || expr1.X_add_number >= 0x8000 - 4)
9141 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 9142 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 9143 relax_start (offset_expr.X_add_symbol);
df58fc94 9144 macro_build (&offset_expr, "lui", LUI_FMT,
67c0d1eb
RS
9145 AT, BFD_RELOC_MIPS_GOT_HI16);
9146 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9147 AT, AT, mips_gp_register);
67c0d1eb 9148 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 9149 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 9150 load_delay_nop ();
252b5132 9151 if (breg != 0)
67c0d1eb 9152 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 9153 /* Itbl support may require additional care here. */
67c0d1eb 9154 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
17a2f251 9155 BFD_RELOC_LO16, AT);
252b5132
RH
9156 expr1.X_add_number += 4;
9157
9158 /* Set mips_optimize to 2 to avoid inserting an undesired
9159 nop. */
9160 hold_mips_optimize = mips_optimize;
9161 mips_optimize = 2;
beae10d5 9162 /* Itbl support may require additional care here. */
67c0d1eb 9163 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
17a2f251 9164 BFD_RELOC_LO16, AT);
252b5132
RH
9165 mips_optimize = hold_mips_optimize;
9166 expr1.X_add_number -= 4;
9167
4d7206a2
RS
9168 relax_switch ();
9169 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
9170 if (gpdelay)
9171 macro_build (NULL, "nop", "");
9172 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
9173 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9174 load_delay_nop ();
252b5132 9175 if (breg != 0)
67c0d1eb 9176 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 9177 /* Itbl support may require additional care here. */
67c0d1eb
RS
9178 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9179 BFD_RELOC_LO16, AT);
4d7206a2 9180 offset_expr.X_add_number += 4;
252b5132
RH
9181
9182 /* Set mips_optimize to 2 to avoid inserting an undesired
9183 nop. */
9184 hold_mips_optimize = mips_optimize;
9185 mips_optimize = 2;
beae10d5 9186 /* Itbl support may require additional care here. */
67c0d1eb
RS
9187 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9188 BFD_RELOC_LO16, AT);
252b5132 9189 mips_optimize = hold_mips_optimize;
4d7206a2 9190 relax_end ();
252b5132 9191 }
252b5132
RH
9192 else
9193 abort ();
9194
252b5132
RH
9195 break;
9196
9197 case M_LD_OB:
704897fb 9198 s = HAVE_64BIT_GPRS ? "ld" : "lw";
252b5132
RH
9199 goto sd_ob;
9200 case M_SD_OB:
704897fb 9201 s = HAVE_64BIT_GPRS ? "sd" : "sw";
252b5132 9202 sd_ob:
4614d845
MR
9203 macro_build (&offset_expr, s, "t,o(b)", treg,
9204 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2],
9205 breg);
704897fb
MR
9206 if (!HAVE_64BIT_GPRS)
9207 {
9208 offset_expr.X_add_number += 4;
9209 macro_build (&offset_expr, s, "t,o(b)", treg + 1,
4614d845
MR
9210 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2],
9211 breg);
704897fb 9212 }
8fc2e39e 9213 break;
252b5132 9214
dd6a37e7
AP
9215
9216 case M_SAA_AB:
9217 ab = 1;
9218 case M_SAA_OB:
9219 s = "saa";
9220 off0 = 1;
9221 fmt = "t,(b)";
9222 goto ld_st;
9223 case M_SAAD_AB:
9224 ab = 1;
9225 case M_SAAD_OB:
9226 s = "saad";
9227 off0 = 1;
9228 fmt = "t,(b)";
9229 goto ld_st;
9230
252b5132
RH
9231 /* New code added to support COPZ instructions.
9232 This code builds table entries out of the macros in mip_opcodes.
9233 R4000 uses interlocks to handle coproc delays.
9234 Other chips (like the R3000) require nops to be inserted for delays.
9235
f72c8c98 9236 FIXME: Currently, we require that the user handle delays.
252b5132
RH
9237 In order to fill delay slots for non-interlocked chips,
9238 we must have a way to specify delays based on the coprocessor.
9239 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
9240 What are the side-effects of the cop instruction?
9241 What cache support might we have and what are its effects?
9242 Both coprocessor & memory require delays. how long???
bdaaa2e1 9243 What registers are read/set/modified?
252b5132
RH
9244
9245 If an itbl is provided to interpret cop instructions,
bdaaa2e1 9246 this knowledge can be encoded in the itbl spec. */
252b5132
RH
9247
9248 case M_COP0:
9249 s = "c0";
9250 goto copz;
9251 case M_COP1:
9252 s = "c1";
9253 goto copz;
9254 case M_COP2:
9255 s = "c2";
9256 goto copz;
9257 case M_COP3:
9258 s = "c3";
9259 copz:
df58fc94 9260 gas_assert (!mips_opts.micromips);
252b5132
RH
9261 /* For now we just do C (same as Cz). The parameter will be
9262 stored in insn_opcode by mips_ip. */
67c0d1eb 9263 macro_build (NULL, s, "C", ip->insn_opcode);
8fc2e39e 9264 break;
252b5132 9265
ea1fb5dc 9266 case M_MOVE:
67c0d1eb 9267 move_register (dreg, sreg);
8fc2e39e 9268 break;
ea1fb5dc 9269
252b5132
RH
9270 case M_DMUL:
9271 dbl = 1;
9272 case M_MUL:
67c0d1eb 9273 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
df58fc94 9274 macro_build (NULL, "mflo", MFHL_FMT, dreg);
8fc2e39e 9275 break;
252b5132
RH
9276
9277 case M_DMUL_I:
9278 dbl = 1;
9279 case M_MUL_I:
9280 /* The MIPS assembler some times generates shifts and adds. I'm
9281 not trying to be that fancy. GCC should do this for us
9282 anyway. */
8fc2e39e 9283 used_at = 1;
67c0d1eb
RS
9284 load_register (AT, &imm_expr, dbl);
9285 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
df58fc94 9286 macro_build (NULL, "mflo", MFHL_FMT, dreg);
252b5132
RH
9287 break;
9288
9289 case M_DMULO_I:
9290 dbl = 1;
9291 case M_MULO_I:
9292 imm = 1;
9293 goto do_mulo;
9294
9295 case M_DMULO:
9296 dbl = 1;
9297 case M_MULO:
9298 do_mulo:
7d10b47d 9299 start_noreorder ();
8fc2e39e 9300 used_at = 1;
252b5132 9301 if (imm)
67c0d1eb
RS
9302 load_register (AT, &imm_expr, dbl);
9303 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
df58fc94
RS
9304 macro_build (NULL, "mflo", MFHL_FMT, dreg);
9305 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, dreg, dreg, RA);
9306 macro_build (NULL, "mfhi", MFHL_FMT, AT);
252b5132 9307 if (mips_trap)
df58fc94 9308 macro_build (NULL, "tne", TRAP_FMT, dreg, AT, 6);
252b5132
RH
9309 else
9310 {
df58fc94
RS
9311 if (mips_opts.micromips)
9312 micromips_label_expr (&label_expr);
9313 else
9314 label_expr.X_add_number = 8;
9315 macro_build (&label_expr, "beq", "s,t,p", dreg, AT);
a605d2b3 9316 macro_build (NULL, "nop", "");
df58fc94
RS
9317 macro_build (NULL, "break", BRK_FMT, 6);
9318 if (mips_opts.micromips)
9319 micromips_add_label ();
252b5132 9320 }
7d10b47d 9321 end_noreorder ();
df58fc94 9322 macro_build (NULL, "mflo", MFHL_FMT, dreg);
252b5132
RH
9323 break;
9324
9325 case M_DMULOU_I:
9326 dbl = 1;
9327 case M_MULOU_I:
9328 imm = 1;
9329 goto do_mulou;
9330
9331 case M_DMULOU:
9332 dbl = 1;
9333 case M_MULOU:
9334 do_mulou:
7d10b47d 9335 start_noreorder ();
8fc2e39e 9336 used_at = 1;
252b5132 9337 if (imm)
67c0d1eb
RS
9338 load_register (AT, &imm_expr, dbl);
9339 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
17a2f251 9340 sreg, imm ? AT : treg);
df58fc94
RS
9341 macro_build (NULL, "mfhi", MFHL_FMT, AT);
9342 macro_build (NULL, "mflo", MFHL_FMT, dreg);
252b5132 9343 if (mips_trap)
df58fc94 9344 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
252b5132
RH
9345 else
9346 {
df58fc94
RS
9347 if (mips_opts.micromips)
9348 micromips_label_expr (&label_expr);
9349 else
9350 label_expr.X_add_number = 8;
9351 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
a605d2b3 9352 macro_build (NULL, "nop", "");
df58fc94
RS
9353 macro_build (NULL, "break", BRK_FMT, 6);
9354 if (mips_opts.micromips)
9355 micromips_add_label ();
252b5132 9356 }
7d10b47d 9357 end_noreorder ();
252b5132
RH
9358 break;
9359
771c7ce4 9360 case M_DROL:
fef14a42 9361 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
9362 {
9363 if (dreg == sreg)
9364 {
9365 tempreg = AT;
9366 used_at = 1;
9367 }
9368 else
9369 {
9370 tempreg = dreg;
82dd0097 9371 }
67c0d1eb
RS
9372 macro_build (NULL, "dnegu", "d,w", tempreg, treg);
9373 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
8fc2e39e 9374 break;
82dd0097 9375 }
8fc2e39e 9376 used_at = 1;
c80c840e 9377 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
67c0d1eb
RS
9378 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
9379 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
9380 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
9381 break;
9382
252b5132 9383 case M_ROL:
fef14a42 9384 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097
CD
9385 {
9386 if (dreg == sreg)
9387 {
9388 tempreg = AT;
9389 used_at = 1;
9390 }
9391 else
9392 {
9393 tempreg = dreg;
82dd0097 9394 }
67c0d1eb
RS
9395 macro_build (NULL, "negu", "d,w", tempreg, treg);
9396 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
8fc2e39e 9397 break;
82dd0097 9398 }
8fc2e39e 9399 used_at = 1;
c80c840e 9400 macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
67c0d1eb
RS
9401 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
9402 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
9403 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
9404 break;
9405
771c7ce4
TS
9406 case M_DROL_I:
9407 {
9408 unsigned int rot;
91d6fa6a
NC
9409 char *l;
9410 char *rr;
771c7ce4
TS
9411
9412 if (imm_expr.X_op != O_constant)
82dd0097 9413 as_bad (_("Improper rotate count"));
771c7ce4 9414 rot = imm_expr.X_add_number & 0x3f;
fef14a42 9415 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
9416 {
9417 rot = (64 - rot) & 0x3f;
9418 if (rot >= 32)
df58fc94 9419 macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
60b63b72 9420 else
df58fc94 9421 macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
8fc2e39e 9422 break;
60b63b72 9423 }
483fc7cd 9424 if (rot == 0)
483fc7cd 9425 {
df58fc94 9426 macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
8fc2e39e 9427 break;
483fc7cd 9428 }
82dd0097 9429 l = (rot < 0x20) ? "dsll" : "dsll32";
91d6fa6a 9430 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
82dd0097 9431 rot &= 0x1f;
8fc2e39e 9432 used_at = 1;
df58fc94
RS
9433 macro_build (NULL, l, SHFT_FMT, AT, sreg, rot);
9434 macro_build (NULL, rr, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
67c0d1eb 9435 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
9436 }
9437 break;
9438
252b5132 9439 case M_ROL_I:
771c7ce4
TS
9440 {
9441 unsigned int rot;
9442
9443 if (imm_expr.X_op != O_constant)
82dd0097 9444 as_bad (_("Improper rotate count"));
771c7ce4 9445 rot = imm_expr.X_add_number & 0x1f;
fef14a42 9446 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 9447 {
df58fc94 9448 macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, (32 - rot) & 0x1f);
8fc2e39e 9449 break;
60b63b72 9450 }
483fc7cd 9451 if (rot == 0)
483fc7cd 9452 {
df58fc94 9453 macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
8fc2e39e 9454 break;
483fc7cd 9455 }
8fc2e39e 9456 used_at = 1;
df58fc94
RS
9457 macro_build (NULL, "sll", SHFT_FMT, AT, sreg, rot);
9458 macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
67c0d1eb 9459 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
9460 }
9461 break;
9462
9463 case M_DROR:
fef14a42 9464 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 9465 {
67c0d1eb 9466 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
8fc2e39e 9467 break;
82dd0097 9468 }
8fc2e39e 9469 used_at = 1;
c80c840e 9470 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
67c0d1eb
RS
9471 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
9472 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
9473 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
9474 break;
9475
9476 case M_ROR:
fef14a42 9477 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 9478 {
67c0d1eb 9479 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
8fc2e39e 9480 break;
82dd0097 9481 }
8fc2e39e 9482 used_at = 1;
c80c840e 9483 macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
67c0d1eb
RS
9484 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
9485 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
9486 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
9487 break;
9488
771c7ce4
TS
9489 case M_DROR_I:
9490 {
9491 unsigned int rot;
91d6fa6a
NC
9492 char *l;
9493 char *rr;
771c7ce4
TS
9494
9495 if (imm_expr.X_op != O_constant)
82dd0097 9496 as_bad (_("Improper rotate count"));
771c7ce4 9497 rot = imm_expr.X_add_number & 0x3f;
fef14a42 9498 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
9499 {
9500 if (rot >= 32)
df58fc94 9501 macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
82dd0097 9502 else
df58fc94 9503 macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
8fc2e39e 9504 break;
82dd0097 9505 }
483fc7cd 9506 if (rot == 0)
483fc7cd 9507 {
df58fc94 9508 macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
8fc2e39e 9509 break;
483fc7cd 9510 }
91d6fa6a 9511 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
82dd0097
CD
9512 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
9513 rot &= 0x1f;
8fc2e39e 9514 used_at = 1;
df58fc94
RS
9515 macro_build (NULL, rr, SHFT_FMT, AT, sreg, rot);
9516 macro_build (NULL, l, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
67c0d1eb 9517 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
9518 }
9519 break;
9520
252b5132 9521 case M_ROR_I:
771c7ce4
TS
9522 {
9523 unsigned int rot;
9524
9525 if (imm_expr.X_op != O_constant)
82dd0097 9526 as_bad (_("Improper rotate count"));
771c7ce4 9527 rot = imm_expr.X_add_number & 0x1f;
fef14a42 9528 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 9529 {
df58fc94 9530 macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, rot);
8fc2e39e 9531 break;
82dd0097 9532 }
483fc7cd 9533 if (rot == 0)
483fc7cd 9534 {
df58fc94 9535 macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
8fc2e39e 9536 break;
483fc7cd 9537 }
8fc2e39e 9538 used_at = 1;
df58fc94
RS
9539 macro_build (NULL, "srl", SHFT_FMT, AT, sreg, rot);
9540 macro_build (NULL, "sll", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
67c0d1eb 9541 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4 9542 }
252b5132
RH
9543 break;
9544
252b5132
RH
9545 case M_SEQ:
9546 if (sreg == 0)
67c0d1eb 9547 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
252b5132 9548 else if (treg == 0)
67c0d1eb 9549 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
9550 else
9551 {
67c0d1eb
RS
9552 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
9553 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
252b5132 9554 }
8fc2e39e 9555 break;
252b5132
RH
9556
9557 case M_SEQ_I:
9558 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
9559 {
67c0d1eb 9560 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 9561 break;
252b5132
RH
9562 }
9563 if (sreg == 0)
9564 {
9565 as_warn (_("Instruction %s: result is always false"),
9566 ip->insn_mo->name);
67c0d1eb 9567 move_register (dreg, 0);
8fc2e39e 9568 break;
252b5132 9569 }
dd3cbb7e
NC
9570 if (CPU_HAS_SEQ (mips_opts.arch)
9571 && -512 <= imm_expr.X_add_number
9572 && imm_expr.X_add_number < 512)
9573 {
9574 macro_build (NULL, "seqi", "t,r,+Q", dreg, sreg,
750bdd57 9575 (int) imm_expr.X_add_number);
dd3cbb7e
NC
9576 break;
9577 }
252b5132
RH
9578 if (imm_expr.X_op == O_constant
9579 && imm_expr.X_add_number >= 0
9580 && imm_expr.X_add_number < 0x10000)
9581 {
67c0d1eb 9582 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
9583 }
9584 else if (imm_expr.X_op == O_constant
9585 && imm_expr.X_add_number > -0x8000
9586 && imm_expr.X_add_number < 0)
9587 {
9588 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 9589 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 9590 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132 9591 }
dd3cbb7e
NC
9592 else if (CPU_HAS_SEQ (mips_opts.arch))
9593 {
9594 used_at = 1;
9595 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9596 macro_build (NULL, "seq", "d,v,t", dreg, sreg, AT);
9597 break;
9598 }
252b5132
RH
9599 else
9600 {
67c0d1eb
RS
9601 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9602 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
9603 used_at = 1;
9604 }
67c0d1eb 9605 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 9606 break;
252b5132
RH
9607
9608 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
9609 s = "slt";
9610 goto sge;
9611 case M_SGEU:
9612 s = "sltu";
9613 sge:
67c0d1eb
RS
9614 macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
9615 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 9616 break;
252b5132
RH
9617
9618 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
9619 case M_SGEU_I:
9620 if (imm_expr.X_op == O_constant
9621 && imm_expr.X_add_number >= -0x8000
9622 && imm_expr.X_add_number < 0x8000)
9623 {
67c0d1eb
RS
9624 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
9625 dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
9626 }
9627 else
9628 {
67c0d1eb
RS
9629 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9630 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
9631 dreg, sreg, AT);
252b5132
RH
9632 used_at = 1;
9633 }
67c0d1eb 9634 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 9635 break;
252b5132
RH
9636
9637 case M_SGT: /* sreg > treg <==> treg < sreg */
9638 s = "slt";
9639 goto sgt;
9640 case M_SGTU:
9641 s = "sltu";
9642 sgt:
67c0d1eb 9643 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
8fc2e39e 9644 break;
252b5132
RH
9645
9646 case M_SGT_I: /* sreg > I <==> I < sreg */
9647 s = "slt";
9648 goto sgti;
9649 case M_SGTU_I:
9650 s = "sltu";
9651 sgti:
8fc2e39e 9652 used_at = 1;
67c0d1eb
RS
9653 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9654 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
252b5132
RH
9655 break;
9656
2396cfb9 9657 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
252b5132
RH
9658 s = "slt";
9659 goto sle;
9660 case M_SLEU:
9661 s = "sltu";
9662 sle:
67c0d1eb
RS
9663 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
9664 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 9665 break;
252b5132 9666
2396cfb9 9667 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
252b5132
RH
9668 s = "slt";
9669 goto slei;
9670 case M_SLEU_I:
9671 s = "sltu";
9672 slei:
8fc2e39e 9673 used_at = 1;
67c0d1eb
RS
9674 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9675 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
9676 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
9677 break;
9678
9679 case M_SLT_I:
9680 if (imm_expr.X_op == O_constant
9681 && imm_expr.X_add_number >= -0x8000
9682 && imm_expr.X_add_number < 0x8000)
9683 {
67c0d1eb 9684 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 9685 break;
252b5132 9686 }
8fc2e39e 9687 used_at = 1;
67c0d1eb
RS
9688 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9689 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
252b5132
RH
9690 break;
9691
9692 case M_SLTU_I:
9693 if (imm_expr.X_op == O_constant
9694 && imm_expr.X_add_number >= -0x8000
9695 && imm_expr.X_add_number < 0x8000)
9696 {
67c0d1eb 9697 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
17a2f251 9698 BFD_RELOC_LO16);
8fc2e39e 9699 break;
252b5132 9700 }
8fc2e39e 9701 used_at = 1;
67c0d1eb
RS
9702 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9703 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
252b5132
RH
9704 break;
9705
9706 case M_SNE:
9707 if (sreg == 0)
67c0d1eb 9708 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
252b5132 9709 else if (treg == 0)
67c0d1eb 9710 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
252b5132
RH
9711 else
9712 {
67c0d1eb
RS
9713 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
9714 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
252b5132 9715 }
8fc2e39e 9716 break;
252b5132
RH
9717
9718 case M_SNE_I:
9719 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
9720 {
67c0d1eb 9721 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
8fc2e39e 9722 break;
252b5132
RH
9723 }
9724 if (sreg == 0)
9725 {
9726 as_warn (_("Instruction %s: result is always true"),
9727 ip->insn_mo->name);
67c0d1eb
RS
9728 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
9729 dreg, 0, BFD_RELOC_LO16);
8fc2e39e 9730 break;
252b5132 9731 }
dd3cbb7e
NC
9732 if (CPU_HAS_SEQ (mips_opts.arch)
9733 && -512 <= imm_expr.X_add_number
9734 && imm_expr.X_add_number < 512)
9735 {
9736 macro_build (NULL, "snei", "t,r,+Q", dreg, sreg,
750bdd57 9737 (int) imm_expr.X_add_number);
dd3cbb7e
NC
9738 break;
9739 }
252b5132
RH
9740 if (imm_expr.X_op == O_constant
9741 && imm_expr.X_add_number >= 0
9742 && imm_expr.X_add_number < 0x10000)
9743 {
67c0d1eb 9744 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
9745 }
9746 else if (imm_expr.X_op == O_constant
9747 && imm_expr.X_add_number > -0x8000
9748 && imm_expr.X_add_number < 0)
9749 {
9750 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 9751 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 9752 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132 9753 }
dd3cbb7e
NC
9754 else if (CPU_HAS_SEQ (mips_opts.arch))
9755 {
9756 used_at = 1;
9757 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9758 macro_build (NULL, "sne", "d,v,t", dreg, sreg, AT);
9759 break;
9760 }
252b5132
RH
9761 else
9762 {
67c0d1eb
RS
9763 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9764 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
9765 used_at = 1;
9766 }
67c0d1eb 9767 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
8fc2e39e 9768 break;
252b5132 9769
df58fc94
RS
9770 case M_SUB_I:
9771 s = "addi";
9772 s2 = "sub";
9773 goto do_subi;
9774 case M_SUBU_I:
9775 s = "addiu";
9776 s2 = "subu";
9777 goto do_subi;
252b5132
RH
9778 case M_DSUB_I:
9779 dbl = 1;
df58fc94
RS
9780 s = "daddi";
9781 s2 = "dsub";
9782 if (!mips_opts.micromips)
9783 goto do_subi;
252b5132 9784 if (imm_expr.X_op == O_constant
df58fc94
RS
9785 && imm_expr.X_add_number > -0x200
9786 && imm_expr.X_add_number <= 0x200)
252b5132 9787 {
df58fc94 9788 macro_build (NULL, s, "t,r,.", dreg, sreg, -imm_expr.X_add_number);
8fc2e39e 9789 break;
252b5132 9790 }
df58fc94 9791 goto do_subi_i;
252b5132
RH
9792 case M_DSUBU_I:
9793 dbl = 1;
df58fc94
RS
9794 s = "daddiu";
9795 s2 = "dsubu";
9796 do_subi:
252b5132
RH
9797 if (imm_expr.X_op == O_constant
9798 && imm_expr.X_add_number > -0x8000
9799 && imm_expr.X_add_number <= 0x8000)
9800 {
9801 imm_expr.X_add_number = -imm_expr.X_add_number;
df58fc94 9802 macro_build (&imm_expr, s, "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 9803 break;
252b5132 9804 }
df58fc94 9805 do_subi_i:
8fc2e39e 9806 used_at = 1;
67c0d1eb 9807 load_register (AT, &imm_expr, dbl);
df58fc94 9808 macro_build (NULL, s2, "d,v,t", dreg, sreg, AT);
252b5132
RH
9809 break;
9810
9811 case M_TEQ_I:
9812 s = "teq";
9813 goto trap;
9814 case M_TGE_I:
9815 s = "tge";
9816 goto trap;
9817 case M_TGEU_I:
9818 s = "tgeu";
9819 goto trap;
9820 case M_TLT_I:
9821 s = "tlt";
9822 goto trap;
9823 case M_TLTU_I:
9824 s = "tltu";
9825 goto trap;
9826 case M_TNE_I:
9827 s = "tne";
9828 trap:
8fc2e39e 9829 used_at = 1;
67c0d1eb
RS
9830 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9831 macro_build (NULL, s, "s,t", sreg, AT);
252b5132
RH
9832 break;
9833
252b5132 9834 case M_TRUNCWS:
43841e91 9835 case M_TRUNCWD:
df58fc94 9836 gas_assert (!mips_opts.micromips);
9c2799c2 9837 gas_assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 9838 used_at = 1;
252b5132
RH
9839 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
9840 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
9841
9842 /*
9843 * Is the double cfc1 instruction a bug in the mips assembler;
9844 * or is there a reason for it?
9845 */
7d10b47d 9846 start_noreorder ();
67c0d1eb
RS
9847 macro_build (NULL, "cfc1", "t,G", treg, RA);
9848 macro_build (NULL, "cfc1", "t,G", treg, RA);
9849 macro_build (NULL, "nop", "");
252b5132 9850 expr1.X_add_number = 3;
67c0d1eb 9851 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
252b5132 9852 expr1.X_add_number = 2;
67c0d1eb
RS
9853 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
9854 macro_build (NULL, "ctc1", "t,G", AT, RA);
9855 macro_build (NULL, "nop", "");
9856 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
9857 dreg, sreg);
9858 macro_build (NULL, "ctc1", "t,G", treg, RA);
9859 macro_build (NULL, "nop", "");
7d10b47d 9860 end_noreorder ();
252b5132
RH
9861 break;
9862
df58fc94
RS
9863 case M_ULH_A:
9864 ab = 1;
252b5132
RH
9865 case M_ULH:
9866 s = "lb";
df58fc94
RS
9867 s2 = "lbu";
9868 off = 1;
9869 goto uld_st;
9870 case M_ULHU_A:
9871 ab = 1;
252b5132
RH
9872 case M_ULHU:
9873 s = "lbu";
df58fc94
RS
9874 s2 = "lbu";
9875 off = 1;
9876 goto uld_st;
9877 case M_ULW_A:
9878 ab = 1;
9879 case M_ULW:
9880 s = "lwl";
9881 s2 = "lwr";
9882 off12 = mips_opts.micromips;
9883 off = 3;
9884 goto uld_st;
9885 case M_ULD_A:
9886 ab = 1;
252b5132
RH
9887 case M_ULD:
9888 s = "ldl";
9889 s2 = "ldr";
df58fc94 9890 off12 = mips_opts.micromips;
252b5132 9891 off = 7;
df58fc94
RS
9892 goto uld_st;
9893 case M_USH_A:
9894 ab = 1;
9895 case M_USH:
9896 s = "sb";
9897 s2 = "sb";
9898 off = 1;
9899 ust = 1;
9900 goto uld_st;
9901 case M_USW_A:
9902 ab = 1;
9903 case M_USW:
9904 s = "swl";
9905 s2 = "swr";
9906 off12 = mips_opts.micromips;
252b5132 9907 off = 3;
df58fc94
RS
9908 ust = 1;
9909 goto uld_st;
9910 case M_USD_A:
9911 ab = 1;
9912 case M_USD:
9913 s = "sdl";
9914 s2 = "sdr";
9915 off12 = mips_opts.micromips;
9916 off = 7;
9917 ust = 1;
9918
9919 uld_st:
9920 if (!ab && offset_expr.X_add_number >= 0x8000 - off)
f71d0d44 9921 as_bad (_("Operand overflow"));
df58fc94
RS
9922
9923 ep = &offset_expr;
9924 expr1.X_add_number = 0;
9925 if (ab)
9926 {
9927 used_at = 1;
9928 tempreg = AT;
9929 load_address (tempreg, ep, &used_at);
9930 if (breg != 0)
9931 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9932 tempreg, tempreg, breg);
9933 breg = tempreg;
9934 tempreg = treg;
9935 ep = &expr1;
9936 }
9937 else if (off12
9938 && (offset_expr.X_op != O_constant
9939 || !IS_SEXT_12BIT_NUM (offset_expr.X_add_number)
9940 || !IS_SEXT_12BIT_NUM (offset_expr.X_add_number + off)))
9941 {
9942 used_at = 1;
9943 tempreg = AT;
9944 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg,
9945 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
9946 breg = tempreg;
9947 tempreg = treg;
9948 ep = &expr1;
9949 }
9950 else if (!ust && treg == breg)
8fc2e39e
TS
9951 {
9952 used_at = 1;
9953 tempreg = AT;
9954 }
252b5132 9955 else
df58fc94 9956 tempreg = treg;
af22f5b2 9957
df58fc94
RS
9958 if (off == 1)
9959 goto ulh_sh;
252b5132 9960
90ecf173 9961 if (!target_big_endian)
df58fc94
RS
9962 ep->X_add_number += off;
9963 if (!off12)
9964 macro_build (ep, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
252b5132 9965 else
df58fc94
RS
9966 macro_build (NULL, s, "t,~(b)",
9967 tempreg, (unsigned long) ep->X_add_number, breg);
9968
90ecf173 9969 if (!target_big_endian)
df58fc94 9970 ep->X_add_number -= off;
252b5132 9971 else
df58fc94
RS
9972 ep->X_add_number += off;
9973 if (!off12)
9974 macro_build (ep, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
9975 else
9976 macro_build (NULL, s2, "t,~(b)",
9977 tempreg, (unsigned long) ep->X_add_number, breg);
252b5132 9978
df58fc94
RS
9979 /* If necessary, move the result in tempreg to the final destination. */
9980 if (!ust && treg != tempreg)
9981 {
9982 /* Protect second load's delay slot. */
9983 load_delay_nop ();
9984 move_register (treg, tempreg);
9985 }
8fc2e39e 9986 break;
252b5132 9987
df58fc94 9988 ulh_sh:
d6bc6245 9989 used_at = 1;
df58fc94
RS
9990 if (target_big_endian == ust)
9991 ep->X_add_number += off;
9992 tempreg = ust || ab ? treg : AT;
9993 macro_build (ep, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
9994
9995 /* For halfword transfers we need a temporary register to shuffle
9996 bytes. Unfortunately for M_USH_A we have none available before
9997 the next store as AT holds the base address. We deal with this
9998 case by clobbering TREG and then restoring it as with ULH. */
9999 tempreg = ust == ab ? treg : AT;
10000 if (ust)
10001 macro_build (NULL, "srl", SHFT_FMT, tempreg, treg, 8);
10002
10003 if (target_big_endian == ust)
10004 ep->X_add_number -= off;
252b5132 10005 else
df58fc94
RS
10006 ep->X_add_number += off;
10007 macro_build (ep, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
252b5132 10008
df58fc94
RS
10009 /* For M_USH_A re-retrieve the LSB. */
10010 if (ust && ab)
10011 {
10012 if (target_big_endian)
10013 ep->X_add_number += off;
10014 else
10015 ep->X_add_number -= off;
10016 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
10017 }
10018 /* For ULH and M_USH_A OR the LSB in. */
10019 if (!ust || ab)
10020 {
10021 tempreg = !ab ? AT : treg;
10022 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
10023 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
10024 }
252b5132
RH
10025 break;
10026
10027 default:
10028 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 10029 are added dynamically. */
252b5132
RH
10030 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
10031 break;
10032 }
741fe287 10033 if (!mips_opts.at && used_at)
8fc2e39e 10034 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
10035}
10036
10037/* Implement macros in mips16 mode. */
10038
10039static void
17a2f251 10040mips16_macro (struct mips_cl_insn *ip)
252b5132
RH
10041{
10042 int mask;
10043 int xreg, yreg, zreg, tmp;
252b5132
RH
10044 expressionS expr1;
10045 int dbl;
10046 const char *s, *s2, *s3;
10047
10048 mask = ip->insn_mo->mask;
10049
bf12938e
RS
10050 xreg = MIPS16_EXTRACT_OPERAND (RX, *ip);
10051 yreg = MIPS16_EXTRACT_OPERAND (RY, *ip);
10052 zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip);
252b5132 10053
252b5132
RH
10054 expr1.X_op = O_constant;
10055 expr1.X_op_symbol = NULL;
10056 expr1.X_add_symbol = NULL;
10057 expr1.X_add_number = 1;
10058
10059 dbl = 0;
10060
10061 switch (mask)
10062 {
10063 default:
10064 internalError ();
10065
10066 case M_DDIV_3:
10067 dbl = 1;
10068 case M_DIV_3:
10069 s = "mflo";
10070 goto do_div3;
10071 case M_DREM_3:
10072 dbl = 1;
10073 case M_REM_3:
10074 s = "mfhi";
10075 do_div3:
7d10b47d 10076 start_noreorder ();
67c0d1eb 10077 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
252b5132 10078 expr1.X_add_number = 2;
67c0d1eb
RS
10079 macro_build (&expr1, "bnez", "x,p", yreg);
10080 macro_build (NULL, "break", "6", 7);
bdaaa2e1 10081
252b5132
RH
10082 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
10083 since that causes an overflow. We should do that as well,
10084 but I don't see how to do the comparisons without a temporary
10085 register. */
7d10b47d 10086 end_noreorder ();
67c0d1eb 10087 macro_build (NULL, s, "x", zreg);
252b5132
RH
10088 break;
10089
10090 case M_DIVU_3:
10091 s = "divu";
10092 s2 = "mflo";
10093 goto do_divu3;
10094 case M_REMU_3:
10095 s = "divu";
10096 s2 = "mfhi";
10097 goto do_divu3;
10098 case M_DDIVU_3:
10099 s = "ddivu";
10100 s2 = "mflo";
10101 goto do_divu3;
10102 case M_DREMU_3:
10103 s = "ddivu";
10104 s2 = "mfhi";
10105 do_divu3:
7d10b47d 10106 start_noreorder ();
67c0d1eb 10107 macro_build (NULL, s, "0,x,y", xreg, yreg);
252b5132 10108 expr1.X_add_number = 2;
67c0d1eb
RS
10109 macro_build (&expr1, "bnez", "x,p", yreg);
10110 macro_build (NULL, "break", "6", 7);
7d10b47d 10111 end_noreorder ();
67c0d1eb 10112 macro_build (NULL, s2, "x", zreg);
252b5132
RH
10113 break;
10114
10115 case M_DMUL:
10116 dbl = 1;
10117 case M_MUL:
67c0d1eb
RS
10118 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
10119 macro_build (NULL, "mflo", "x", zreg);
8fc2e39e 10120 break;
252b5132
RH
10121
10122 case M_DSUBU_I:
10123 dbl = 1;
10124 goto do_subu;
10125 case M_SUBU_I:
10126 do_subu:
10127 if (imm_expr.X_op != O_constant)
10128 as_bad (_("Unsupported large constant"));
10129 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 10130 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
252b5132
RH
10131 break;
10132
10133 case M_SUBU_I_2:
10134 if (imm_expr.X_op != O_constant)
10135 as_bad (_("Unsupported large constant"));
10136 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 10137 macro_build (&imm_expr, "addiu", "x,k", xreg);
252b5132
RH
10138 break;
10139
10140 case M_DSUBU_I_2:
10141 if (imm_expr.X_op != O_constant)
10142 as_bad (_("Unsupported large constant"));
10143 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 10144 macro_build (&imm_expr, "daddiu", "y,j", yreg);
252b5132
RH
10145 break;
10146
10147 case M_BEQ:
10148 s = "cmp";
10149 s2 = "bteqz";
10150 goto do_branch;
10151 case M_BNE:
10152 s = "cmp";
10153 s2 = "btnez";
10154 goto do_branch;
10155 case M_BLT:
10156 s = "slt";
10157 s2 = "btnez";
10158 goto do_branch;
10159 case M_BLTU:
10160 s = "sltu";
10161 s2 = "btnez";
10162 goto do_branch;
10163 case M_BLE:
10164 s = "slt";
10165 s2 = "bteqz";
10166 goto do_reverse_branch;
10167 case M_BLEU:
10168 s = "sltu";
10169 s2 = "bteqz";
10170 goto do_reverse_branch;
10171 case M_BGE:
10172 s = "slt";
10173 s2 = "bteqz";
10174 goto do_branch;
10175 case M_BGEU:
10176 s = "sltu";
10177 s2 = "bteqz";
10178 goto do_branch;
10179 case M_BGT:
10180 s = "slt";
10181 s2 = "btnez";
10182 goto do_reverse_branch;
10183 case M_BGTU:
10184 s = "sltu";
10185 s2 = "btnez";
10186
10187 do_reverse_branch:
10188 tmp = xreg;
10189 xreg = yreg;
10190 yreg = tmp;
10191
10192 do_branch:
67c0d1eb
RS
10193 macro_build (NULL, s, "x,y", xreg, yreg);
10194 macro_build (&offset_expr, s2, "p");
252b5132
RH
10195 break;
10196
10197 case M_BEQ_I:
10198 s = "cmpi";
10199 s2 = "bteqz";
10200 s3 = "x,U";
10201 goto do_branch_i;
10202 case M_BNE_I:
10203 s = "cmpi";
10204 s2 = "btnez";
10205 s3 = "x,U";
10206 goto do_branch_i;
10207 case M_BLT_I:
10208 s = "slti";
10209 s2 = "btnez";
10210 s3 = "x,8";
10211 goto do_branch_i;
10212 case M_BLTU_I:
10213 s = "sltiu";
10214 s2 = "btnez";
10215 s3 = "x,8";
10216 goto do_branch_i;
10217 case M_BLE_I:
10218 s = "slti";
10219 s2 = "btnez";
10220 s3 = "x,8";
10221 goto do_addone_branch_i;
10222 case M_BLEU_I:
10223 s = "sltiu";
10224 s2 = "btnez";
10225 s3 = "x,8";
10226 goto do_addone_branch_i;
10227 case M_BGE_I:
10228 s = "slti";
10229 s2 = "bteqz";
10230 s3 = "x,8";
10231 goto do_branch_i;
10232 case M_BGEU_I:
10233 s = "sltiu";
10234 s2 = "bteqz";
10235 s3 = "x,8";
10236 goto do_branch_i;
10237 case M_BGT_I:
10238 s = "slti";
10239 s2 = "bteqz";
10240 s3 = "x,8";
10241 goto do_addone_branch_i;
10242 case M_BGTU_I:
10243 s = "sltiu";
10244 s2 = "bteqz";
10245 s3 = "x,8";
10246
10247 do_addone_branch_i:
10248 if (imm_expr.X_op != O_constant)
10249 as_bad (_("Unsupported large constant"));
10250 ++imm_expr.X_add_number;
10251
10252 do_branch_i:
67c0d1eb
RS
10253 macro_build (&imm_expr, s, s3, xreg);
10254 macro_build (&offset_expr, s2, "p");
252b5132
RH
10255 break;
10256
10257 case M_ABS:
10258 expr1.X_add_number = 0;
67c0d1eb 10259 macro_build (&expr1, "slti", "x,8", yreg);
252b5132 10260 if (xreg != yreg)
67c0d1eb 10261 move_register (xreg, yreg);
252b5132 10262 expr1.X_add_number = 2;
67c0d1eb
RS
10263 macro_build (&expr1, "bteqz", "p");
10264 macro_build (NULL, "neg", "x,w", xreg, xreg);
252b5132
RH
10265 }
10266}
10267
10268/* For consistency checking, verify that all bits are specified either
10269 by the match/mask part of the instruction definition, or by the
10270 operand list. */
10271static int
17a2f251 10272validate_mips_insn (const struct mips_opcode *opc)
252b5132
RH
10273{
10274 const char *p = opc->args;
10275 char c;
10276 unsigned long used_bits = opc->mask;
10277
10278 if ((used_bits & opc->match) != opc->match)
10279 {
10280 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
10281 opc->name, opc->args);
10282 return 0;
10283 }
10284#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
10285 while (*p)
10286 switch (c = *p++)
10287 {
10288 case ',': break;
10289 case '(': break;
10290 case ')': break;
af7ee8bf
CD
10291 case '+':
10292 switch (c = *p++)
10293 {
9bcd4f99
TS
10294 case '1': USE_BITS (OP_MASK_UDI1, OP_SH_UDI1); break;
10295 case '2': USE_BITS (OP_MASK_UDI2, OP_SH_UDI2); break;
10296 case '3': USE_BITS (OP_MASK_UDI3, OP_SH_UDI3); break;
10297 case '4': USE_BITS (OP_MASK_UDI4, OP_SH_UDI4); break;
af7ee8bf
CD
10298 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10299 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
10300 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
bbcc0807
CD
10301 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
10302 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
5f74bc13
CD
10303 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10304 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
10305 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
10306 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
10307 case 'I': break;
ef2e4d86
CF
10308 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10309 case 'T': USE_BITS (OP_MASK_RT, OP_SH_RT);
10310 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
bb35fb24
NC
10311 case 'x': USE_BITS (OP_MASK_BBITIND, OP_SH_BBITIND); break;
10312 case 'X': USE_BITS (OP_MASK_BBITIND, OP_SH_BBITIND); break;
10313 case 'p': USE_BITS (OP_MASK_CINSPOS, OP_SH_CINSPOS); break;
10314 case 'P': USE_BITS (OP_MASK_CINSPOS, OP_SH_CINSPOS); break;
dd3cbb7e 10315 case 'Q': USE_BITS (OP_MASK_SEQI, OP_SH_SEQI); break;
bb35fb24
NC
10316 case 's': USE_BITS (OP_MASK_CINSLM1, OP_SH_CINSLM1); break;
10317 case 'S': USE_BITS (OP_MASK_CINSLM1, OP_SH_CINSLM1); break;
98675402
RS
10318 case 'z': USE_BITS (OP_MASK_RZ, OP_SH_RZ); break;
10319 case 'Z': USE_BITS (OP_MASK_FZ, OP_SH_FZ); break;
10320 case 'a': USE_BITS (OP_MASK_OFFSET_A, OP_SH_OFFSET_A); break;
10321 case 'b': USE_BITS (OP_MASK_OFFSET_B, OP_SH_OFFSET_B); break;
10322 case 'c': USE_BITS (OP_MASK_OFFSET_C, OP_SH_OFFSET_C); break;
bb35fb24 10323
af7ee8bf
CD
10324 default:
10325 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
10326 c, opc->name, opc->args);
10327 return 0;
10328 }
10329 break;
252b5132
RH
10330 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10331 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10332 case 'A': break;
4372b673 10333 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
10334 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
10335 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
10336 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10337 case 'F': break;
10338 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 10339 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 10340 case 'I': break;
e972090a 10341 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
af7ee8bf 10342 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
10343 case 'L': break;
10344 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
10345 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
deec1734
CD
10346 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
10347 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
10348 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
10349 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
10350 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
10351 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
10352 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
10353 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
deec1734
CD
10354 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
10355 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
10356 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
10357 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
10358 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10359 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
10360 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
10361 case 'f': break;
10362 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
10363 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
10364 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
10365 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
10366 case 'l': break;
10367 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
10368 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
10369 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
10370 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10371 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10372 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10373 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
10374 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10375 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10376 case 'x': break;
10377 case 'z': break;
10378 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
10379 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
10380 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
60b63b72
RS
10381 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
10382 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
10383 case '[': break;
10384 case ']': break;
620edafd 10385 case '1': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8b082fb1 10386 case '2': USE_BITS (OP_MASK_BP, OP_SH_BP); break;
74cd071d
CF
10387 case '3': USE_BITS (OP_MASK_SA3, OP_SH_SA3); break;
10388 case '4': USE_BITS (OP_MASK_SA4, OP_SH_SA4); break;
10389 case '5': USE_BITS (OP_MASK_IMM8, OP_SH_IMM8); break;
10390 case '6': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10391 case '7': USE_BITS (OP_MASK_DSPACC, OP_SH_DSPACC); break;
10392 case '8': USE_BITS (OP_MASK_WRDSP, OP_SH_WRDSP); break;
10393 case '9': USE_BITS (OP_MASK_DSPACC_S, OP_SH_DSPACC_S);break;
10394 case '0': USE_BITS (OP_MASK_DSPSFT, OP_SH_DSPSFT); break;
10395 case '\'': USE_BITS (OP_MASK_RDDSP, OP_SH_RDDSP); break;
10396 case ':': USE_BITS (OP_MASK_DSPSFT_7, OP_SH_DSPSFT_7);break;
10397 case '@': USE_BITS (OP_MASK_IMM10, OP_SH_IMM10); break;
ef2e4d86
CF
10398 case '!': USE_BITS (OP_MASK_MT_U, OP_SH_MT_U); break;
10399 case '$': USE_BITS (OP_MASK_MT_H, OP_SH_MT_H); break;
10400 case '*': USE_BITS (OP_MASK_MTACC_T, OP_SH_MTACC_T); break;
10401 case '&': USE_BITS (OP_MASK_MTACC_D, OP_SH_MTACC_D); break;
dec0624d
MR
10402 case '\\': USE_BITS (OP_MASK_3BITPOS, OP_SH_3BITPOS); break;
10403 case '~': USE_BITS (OP_MASK_OFFSET12, OP_SH_OFFSET12); break;
ef2e4d86 10404 case 'g': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
10405 default:
10406 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
10407 c, opc->name, opc->args);
10408 return 0;
10409 }
10410#undef USE_BITS
10411 if (used_bits != 0xffffffff)
10412 {
10413 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
10414 ~used_bits & 0xffffffff, opc->name, opc->args);
10415 return 0;
10416 }
10417 return 1;
10418}
10419
df58fc94
RS
10420/* For consistency checking, verify that the length implied matches the
10421 major opcode and that all bits are specified either by the match/mask
10422 part of the instruction definition, or by the operand list. */
10423
10424static int
10425validate_micromips_insn (const struct mips_opcode *opc)
10426{
10427 unsigned long match = opc->match;
10428 unsigned long mask = opc->mask;
10429 const char *p = opc->args;
10430 unsigned long insn_bits;
10431 unsigned long used_bits;
10432 unsigned long major;
10433 unsigned int length;
10434 char e;
10435 char c;
10436
10437 if ((mask & match) != match)
10438 {
10439 as_bad (_("Internal error: bad microMIPS opcode (mask error): %s %s"),
10440 opc->name, opc->args);
10441 return 0;
10442 }
10443 length = micromips_insn_length (opc);
10444 if (length != 2 && length != 4)
10445 {
10446 as_bad (_("Internal error: bad microMIPS opcode (incorrect length: %u): "
10447 "%s %s"), length, opc->name, opc->args);
10448 return 0;
10449 }
10450 major = match >> (10 + 8 * (length - 2));
10451 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
10452 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
10453 {
10454 as_bad (_("Internal error: bad microMIPS opcode "
10455 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
10456 return 0;
10457 }
10458
10459 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
10460 insn_bits = 1 << 4 * length;
10461 insn_bits <<= 4 * length;
10462 insn_bits -= 1;
10463 used_bits = mask;
10464#define USE_BITS(field) \
10465 (used_bits |= MICROMIPSOP_MASK_##field << MICROMIPSOP_SH_##field)
10466 while (*p)
10467 switch (c = *p++)
10468 {
10469 case ',': break;
10470 case '(': break;
10471 case ')': break;
10472 case '+':
10473 e = c;
10474 switch (c = *p++)
10475 {
10476 case 'A': USE_BITS (EXTLSB); break;
10477 case 'B': USE_BITS (INSMSB); break;
10478 case 'C': USE_BITS (EXTMSBD); break;
10479 case 'D': USE_BITS (RS); USE_BITS (SEL); break;
10480 case 'E': USE_BITS (EXTLSB); break;
10481 case 'F': USE_BITS (INSMSB); break;
10482 case 'G': USE_BITS (EXTMSBD); break;
10483 case 'H': USE_BITS (EXTMSBD); break;
10484 default:
10485 as_bad (_("Internal error: bad mips opcode "
10486 "(unknown extension operand type `%c%c'): %s %s"),
10487 e, c, opc->name, opc->args);
10488 return 0;
10489 }
10490 break;
10491 case 'm':
10492 e = c;
10493 switch (c = *p++)
10494 {
10495 case 'A': USE_BITS (IMMA); break;
10496 case 'B': USE_BITS (IMMB); break;
10497 case 'C': USE_BITS (IMMC); break;
10498 case 'D': USE_BITS (IMMD); break;
10499 case 'E': USE_BITS (IMME); break;
10500 case 'F': USE_BITS (IMMF); break;
10501 case 'G': USE_BITS (IMMG); break;
10502 case 'H': USE_BITS (IMMH); break;
10503 case 'I': USE_BITS (IMMI); break;
10504 case 'J': USE_BITS (IMMJ); break;
10505 case 'L': USE_BITS (IMML); break;
10506 case 'M': USE_BITS (IMMM); break;
10507 case 'N': USE_BITS (IMMN); break;
10508 case 'O': USE_BITS (IMMO); break;
10509 case 'P': USE_BITS (IMMP); break;
10510 case 'Q': USE_BITS (IMMQ); break;
10511 case 'U': USE_BITS (IMMU); break;
10512 case 'W': USE_BITS (IMMW); break;
10513 case 'X': USE_BITS (IMMX); break;
10514 case 'Y': USE_BITS (IMMY); break;
10515 case 'Z': break;
10516 case 'a': break;
10517 case 'b': USE_BITS (MB); break;
10518 case 'c': USE_BITS (MC); break;
10519 case 'd': USE_BITS (MD); break;
10520 case 'e': USE_BITS (ME); break;
10521 case 'f': USE_BITS (MF); break;
10522 case 'g': USE_BITS (MG); break;
10523 case 'h': USE_BITS (MH); break;
10524 case 'i': USE_BITS (MI); break;
10525 case 'j': USE_BITS (MJ); break;
10526 case 'l': USE_BITS (ML); break;
10527 case 'm': USE_BITS (MM); break;
10528 case 'n': USE_BITS (MN); break;
10529 case 'p': USE_BITS (MP); break;
10530 case 'q': USE_BITS (MQ); break;
10531 case 'r': break;
10532 case 's': break;
10533 case 't': break;
10534 case 'x': break;
10535 case 'y': break;
10536 case 'z': break;
10537 default:
10538 as_bad (_("Internal error: bad mips opcode "
10539 "(unknown extension operand type `%c%c'): %s %s"),
10540 e, c, opc->name, opc->args);
10541 return 0;
10542 }
10543 break;
10544 case '.': USE_BITS (OFFSET10); break;
10545 case '1': USE_BITS (STYPE); break;
03f66e8a
MR
10546 case '2': USE_BITS (BP); break;
10547 case '3': USE_BITS (SA3); break;
10548 case '4': USE_BITS (SA4); break;
10549 case '5': USE_BITS (IMM8); break;
10550 case '6': USE_BITS (RS); break;
10551 case '7': USE_BITS (DSPACC); break;
10552 case '8': USE_BITS (WRDSP); break;
10553 case '0': USE_BITS (DSPSFT); break;
df58fc94
RS
10554 case '<': USE_BITS (SHAMT); break;
10555 case '>': USE_BITS (SHAMT); break;
03f66e8a 10556 case '@': USE_BITS (IMM10); break;
df58fc94
RS
10557 case 'B': USE_BITS (CODE10); break;
10558 case 'C': USE_BITS (COPZ); break;
10559 case 'D': USE_BITS (FD); break;
10560 case 'E': USE_BITS (RT); break;
10561 case 'G': USE_BITS (RS); break;
444d75be 10562 case 'H': USE_BITS (SEL); break;
df58fc94
RS
10563 case 'K': USE_BITS (RS); break;
10564 case 'M': USE_BITS (CCC); break;
10565 case 'N': USE_BITS (BCC); break;
10566 case 'R': USE_BITS (FR); break;
10567 case 'S': USE_BITS (FS); break;
10568 case 'T': USE_BITS (FT); break;
10569 case 'V': USE_BITS (FS); break;
dec0624d 10570 case '\\': USE_BITS (3BITPOS); break;
03f66e8a 10571 case '^': USE_BITS (RD); break;
df58fc94
RS
10572 case 'a': USE_BITS (TARGET); break;
10573 case 'b': USE_BITS (RS); break;
10574 case 'c': USE_BITS (CODE); break;
10575 case 'd': USE_BITS (RD); break;
10576 case 'h': USE_BITS (PREFX); break;
10577 case 'i': USE_BITS (IMMEDIATE); break;
10578 case 'j': USE_BITS (DELTA); break;
10579 case 'k': USE_BITS (CACHE); break;
10580 case 'n': USE_BITS (RT); break;
10581 case 'o': USE_BITS (DELTA); break;
10582 case 'p': USE_BITS (DELTA); break;
10583 case 'q': USE_BITS (CODE2); break;
10584 case 'r': USE_BITS (RS); break;
10585 case 's': USE_BITS (RS); break;
10586 case 't': USE_BITS (RT); break;
10587 case 'u': USE_BITS (IMMEDIATE); break;
10588 case 'v': USE_BITS (RS); break;
10589 case 'w': USE_BITS (RT); break;
10590 case 'y': USE_BITS (RS3); break;
10591 case 'z': break;
10592 case '|': USE_BITS (TRAP); break;
10593 case '~': USE_BITS (OFFSET12); break;
10594 default:
10595 as_bad (_("Internal error: bad microMIPS opcode "
10596 "(unknown operand type `%c'): %s %s"),
10597 c, opc->name, opc->args);
10598 return 0;
10599 }
10600#undef USE_BITS
10601 if (used_bits != insn_bits)
10602 {
10603 if (~used_bits & insn_bits)
10604 as_bad (_("Internal error: bad microMIPS opcode "
10605 "(bits 0x%lx undefined): %s %s"),
10606 ~used_bits & insn_bits, opc->name, opc->args);
10607 if (used_bits & ~insn_bits)
10608 as_bad (_("Internal error: bad microMIPS opcode "
10609 "(bits 0x%lx defined): %s %s"),
10610 used_bits & ~insn_bits, opc->name, opc->args);
10611 return 0;
10612 }
10613 return 1;
10614}
10615
9bcd4f99
TS
10616/* UDI immediates. */
10617struct mips_immed {
10618 char type;
10619 unsigned int shift;
10620 unsigned long mask;
10621 const char * desc;
10622};
10623
10624static const struct mips_immed mips_immed[] = {
10625 { '1', OP_SH_UDI1, OP_MASK_UDI1, 0},
10626 { '2', OP_SH_UDI2, OP_MASK_UDI2, 0},
10627 { '3', OP_SH_UDI3, OP_MASK_UDI3, 0},
10628 { '4', OP_SH_UDI4, OP_MASK_UDI4, 0},
10629 { 0,0,0,0 }
10630};
10631
7455baf8
TS
10632/* Check whether an odd floating-point register is allowed. */
10633static int
10634mips_oddfpreg_ok (const struct mips_opcode *insn, int argnum)
10635{
10636 const char *s = insn->name;
10637
10638 if (insn->pinfo == INSN_MACRO)
10639 /* Let a macro pass, we'll catch it later when it is expanded. */
10640 return 1;
10641
10642 if (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa))
10643 {
10644 /* Allow odd registers for single-precision ops. */
10645 switch (insn->pinfo & (FP_S | FP_D))
10646 {
10647 case FP_S:
10648 case 0:
10649 return 1; /* both single precision - ok */
10650 case FP_D:
10651 return 0; /* both double precision - fail */
10652 default:
10653 break;
10654 }
10655
10656 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
10657 s = strchr (insn->name, '.');
10658 if (argnum == 2)
10659 s = s != NULL ? strchr (s + 1, '.') : NULL;
10660 return (s != NULL && (s[1] == 'w' || s[1] == 's'));
10661 }
10662
10663 /* Single-precision coprocessor loads and moves are OK too. */
10664 if ((insn->pinfo & FP_S)
10665 && (insn->pinfo & (INSN_COPROC_MEMORY_DELAY | INSN_STORE_MEMORY
10666 | INSN_LOAD_COPROC_DELAY | INSN_COPROC_MOVE_DELAY)))
10667 return 1;
10668
10669 return 0;
10670}
10671
df58fc94
RS
10672/* Check if EXPR is a constant between MIN (inclusive) and MAX (exclusive)
10673 taking bits from BIT up. */
10674static int
10675expr_const_in_range (expressionS *ep, offsetT min, offsetT max, int bit)
10676{
10677 return (ep->X_op == O_constant
10678 && (ep->X_add_number & ((1 << bit) - 1)) == 0
10679 && ep->X_add_number >= min << bit
10680 && ep->X_add_number < max << bit);
10681}
10682
252b5132
RH
10683/* This routine assembles an instruction into its binary format. As a
10684 side effect, it sets one of the global variables imm_reloc or
10685 offset_reloc to the type of relocation to do if one of the operands
10686 is an address expression. */
10687
10688static void
17a2f251 10689mips_ip (char *str, struct mips_cl_insn *ip)
252b5132 10690{
df58fc94
RS
10691 bfd_boolean wrong_delay_slot_insns = FALSE;
10692 bfd_boolean need_delay_slot_ok = TRUE;
10693 struct mips_opcode *firstinsn = NULL;
10694 const struct mips_opcode *past;
10695 struct hash_control *hash;
252b5132
RH
10696 char *s;
10697 const char *args;
43841e91 10698 char c = 0;
252b5132
RH
10699 struct mips_opcode *insn;
10700 char *argsStart;
10701 unsigned int regno;
34224acf 10702 unsigned int lastregno;
df58fc94 10703 unsigned int destregno = 0;
af7ee8bf 10704 unsigned int lastpos = 0;
071742cf 10705 unsigned int limlo, limhi;
252b5132 10706 char *s_reset;
74cd071d 10707 offsetT min_range, max_range;
df58fc94 10708 long opend;
a40bc9dd 10709 char *name;
707bfff6
TS
10710 int argnum;
10711 unsigned int rtype;
df58fc94 10712 char *dot;
a40bc9dd 10713 long end;
252b5132
RH
10714
10715 insn_error = NULL;
10716
df58fc94
RS
10717 if (mips_opts.micromips)
10718 {
10719 hash = micromips_op_hash;
10720 past = &micromips_opcodes[bfd_micromips_num_opcodes];
10721 }
10722 else
10723 {
10724 hash = op_hash;
10725 past = &mips_opcodes[NUMOPCODES];
10726 }
10727 forced_insn_length = 0;
252b5132 10728 insn = NULL;
252b5132 10729
df58fc94 10730 /* We first try to match an instruction up to a space or to the end. */
a40bc9dd
RS
10731 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
10732 continue;
bdaaa2e1 10733
a40bc9dd
RS
10734 /* Make a copy of the instruction so that we can fiddle with it. */
10735 name = alloca (end + 1);
10736 memcpy (name, str, end);
10737 name[end] = '\0';
252b5132 10738
df58fc94
RS
10739 for (;;)
10740 {
10741 insn = (struct mips_opcode *) hash_find (hash, name);
10742
10743 if (insn != NULL || !mips_opts.micromips)
10744 break;
10745 if (forced_insn_length)
10746 break;
10747
10748 /* See if there's an instruction size override suffix,
10749 either `16' or `32', at the end of the mnemonic proper,
10750 that defines the operation, i.e. before the first `.'
10751 character if any. Strip it and retry. */
10752 dot = strchr (name, '.');
10753 opend = dot != NULL ? dot - name : end;
10754 if (opend < 3)
10755 break;
10756 if (name[opend - 2] == '1' && name[opend - 1] == '6')
10757 forced_insn_length = 2;
10758 else if (name[opend - 2] == '3' && name[opend - 1] == '2')
10759 forced_insn_length = 4;
10760 else
10761 break;
10762 memcpy (name + opend - 2, name + opend, end - opend + 1);
10763 }
252b5132
RH
10764 if (insn == NULL)
10765 {
a40bc9dd
RS
10766 insn_error = _("Unrecognized opcode");
10767 return;
252b5132
RH
10768 }
10769
df58fc94
RS
10770 /* For microMIPS instructions placed in a fixed-length branch delay slot
10771 we make up to two passes over the relevant fragment of the opcode
10772 table. First we try instructions that meet the delay slot's length
10773 requirement. If none matched, then we retry with the remaining ones
10774 and if one matches, then we use it and then issue an appropriate
10775 warning later on. */
a40bc9dd 10776 argsStart = s = str + end;
252b5132
RH
10777 for (;;)
10778 {
df58fc94
RS
10779 bfd_boolean delay_slot_ok;
10780 bfd_boolean size_ok;
b34976b6 10781 bfd_boolean ok;
252b5132 10782
a40bc9dd 10783 gas_assert (strcmp (insn->name, name) == 0);
252b5132 10784
f79e2745 10785 ok = is_opcode_valid (insn);
df58fc94
RS
10786 size_ok = is_size_valid (insn);
10787 delay_slot_ok = is_delay_slot_valid (insn);
10788 if (!delay_slot_ok && !wrong_delay_slot_insns)
252b5132 10789 {
df58fc94
RS
10790 firstinsn = insn;
10791 wrong_delay_slot_insns = TRUE;
10792 }
10793 if (!ok || !size_ok || delay_slot_ok != need_delay_slot_ok)
10794 {
10795 static char buf[256];
10796
10797 if (insn + 1 < past && strcmp (insn->name, insn[1].name) == 0)
252b5132
RH
10798 {
10799 ++insn;
10800 continue;
10801 }
df58fc94 10802 if (wrong_delay_slot_insns && need_delay_slot_ok)
beae10d5 10803 {
df58fc94
RS
10804 gas_assert (firstinsn);
10805 need_delay_slot_ok = FALSE;
10806 past = insn + 1;
10807 insn = firstinsn;
10808 continue;
252b5132 10809 }
df58fc94
RS
10810
10811 if (insn_error)
10812 return;
10813
10814 if (!ok)
7bd942df 10815 sprintf (buf, _("Opcode not supported on this processor: %s (%s)"),
df58fc94
RS
10816 mips_cpu_info_from_arch (mips_opts.arch)->name,
10817 mips_cpu_info_from_isa (mips_opts.isa)->name);
10818 else
10819 sprintf (buf, _("Unrecognized %u-bit version of microMIPS opcode"),
10820 8 * forced_insn_length);
10821 insn_error = buf;
10822
10823 return;
252b5132
RH
10824 }
10825
1e915849 10826 create_insn (ip, insn);
268f6bed 10827 insn_error = NULL;
707bfff6 10828 argnum = 1;
24864476 10829 lastregno = 0xffffffff;
252b5132
RH
10830 for (args = insn->args;; ++args)
10831 {
deec1734
CD
10832 int is_mdmx;
10833
ad8d3bb3 10834 s += strspn (s, " \t");
deec1734 10835 is_mdmx = 0;
252b5132
RH
10836 switch (*args)
10837 {
10838 case '\0': /* end of args */
10839 if (*s == '\0')
10840 return;
10841 break;
10842
03f66e8a
MR
10843 case '2':
10844 /* DSP 2-bit unsigned immediate in bit 11 (for standard MIPS
10845 code) or 14 (for microMIPS code). */
8b082fb1
TS
10846 my_getExpression (&imm_expr, s);
10847 check_absolute_expr (ip, &imm_expr);
10848 if ((unsigned long) imm_expr.X_add_number != 1
10849 && (unsigned long) imm_expr.X_add_number != 3)
10850 {
10851 as_bad (_("BALIGN immediate not 1 or 3 (%lu)"),
10852 (unsigned long) imm_expr.X_add_number);
10853 }
03f66e8a
MR
10854 INSERT_OPERAND (mips_opts.micromips,
10855 BP, *ip, imm_expr.X_add_number);
8b082fb1
TS
10856 imm_expr.X_op = O_absent;
10857 s = expr_end;
10858 continue;
10859
03f66e8a
MR
10860 case '3':
10861 /* DSP 3-bit unsigned immediate in bit 13 (for standard MIPS
10862 code) or 21 (for microMIPS code). */
10863 {
10864 unsigned long mask = (mips_opts.micromips
10865 ? MICROMIPSOP_MASK_SA3 : OP_MASK_SA3);
10866
10867 my_getExpression (&imm_expr, s);
10868 check_absolute_expr (ip, &imm_expr);
10869 if ((unsigned long) imm_expr.X_add_number > mask)
10870 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
10871 mask, (unsigned long) imm_expr.X_add_number);
10872 INSERT_OPERAND (mips_opts.micromips,
10873 SA3, *ip, imm_expr.X_add_number);
10874 imm_expr.X_op = O_absent;
10875 s = expr_end;
10876 }
74cd071d
CF
10877 continue;
10878
03f66e8a
MR
10879 case '4':
10880 /* DSP 4-bit unsigned immediate in bit 12 (for standard MIPS
10881 code) or 21 (for microMIPS code). */
10882 {
10883 unsigned long mask = (mips_opts.micromips
10884 ? MICROMIPSOP_MASK_SA4 : OP_MASK_SA4);
10885
10886 my_getExpression (&imm_expr, s);
10887 check_absolute_expr (ip, &imm_expr);
10888 if ((unsigned long) imm_expr.X_add_number > mask)
10889 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
10890 mask, (unsigned long) imm_expr.X_add_number);
10891 INSERT_OPERAND (mips_opts.micromips,
10892 SA4, *ip, imm_expr.X_add_number);
10893 imm_expr.X_op = O_absent;
10894 s = expr_end;
10895 }
74cd071d
CF
10896 continue;
10897
03f66e8a
MR
10898 case '5':
10899 /* DSP 8-bit unsigned immediate in bit 13 (for standard MIPS
10900 code) or 16 (for microMIPS code). */
10901 {
10902 unsigned long mask = (mips_opts.micromips
10903 ? MICROMIPSOP_MASK_IMM8 : OP_MASK_IMM8);
10904
10905 my_getExpression (&imm_expr, s);
10906 check_absolute_expr (ip, &imm_expr);
10907 if ((unsigned long) imm_expr.X_add_number > mask)
10908 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
10909 mask, (unsigned long) imm_expr.X_add_number);
10910 INSERT_OPERAND (mips_opts.micromips,
10911 IMM8, *ip, imm_expr.X_add_number);
10912 imm_expr.X_op = O_absent;
10913 s = expr_end;
10914 }
74cd071d
CF
10915 continue;
10916
03f66e8a
MR
10917 case '6':
10918 /* DSP 5-bit unsigned immediate in bit 16 (for standard MIPS
10919 code) or 21 (for microMIPS code). */
10920 {
10921 unsigned long mask = (mips_opts.micromips
10922 ? MICROMIPSOP_MASK_RS : OP_MASK_RS);
10923
10924 my_getExpression (&imm_expr, s);
10925 check_absolute_expr (ip, &imm_expr);
10926 if ((unsigned long) imm_expr.X_add_number > mask)
10927 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
10928 mask, (unsigned long) imm_expr.X_add_number);
10929 INSERT_OPERAND (mips_opts.micromips,
10930 RS, *ip, imm_expr.X_add_number);
10931 imm_expr.X_op = O_absent;
10932 s = expr_end;
10933 }
74cd071d
CF
10934 continue;
10935
90ecf173 10936 case '7': /* Four DSP accumulators in bits 11,12. */
03f66e8a
MR
10937 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c'
10938 && s[3] >= '0' && s[3] <= '3')
74cd071d
CF
10939 {
10940 regno = s[3] - '0';
10941 s += 4;
03f66e8a 10942 INSERT_OPERAND (mips_opts.micromips, DSPACC, *ip, regno);
74cd071d
CF
10943 continue;
10944 }
10945 else
10946 as_bad (_("Invalid dsp acc register"));
10947 break;
10948
03f66e8a
MR
10949 case '8':
10950 /* DSP 6-bit unsigned immediate in bit 11 (for standard MIPS
10951 code) or 14 (for microMIPS code). */
10952 {
10953 unsigned long mask = (mips_opts.micromips
10954 ? MICROMIPSOP_MASK_WRDSP
10955 : OP_MASK_WRDSP);
10956
10957 my_getExpression (&imm_expr, s);
10958 check_absolute_expr (ip, &imm_expr);
10959 if ((unsigned long) imm_expr.X_add_number > mask)
10960 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
10961 mask, (unsigned long) imm_expr.X_add_number);
10962 INSERT_OPERAND (mips_opts.micromips,
10963 WRDSP, *ip, imm_expr.X_add_number);
10964 imm_expr.X_op = O_absent;
10965 s = expr_end;
10966 }
74cd071d
CF
10967 continue;
10968
90ecf173 10969 case '9': /* Four DSP accumulators in bits 21,22. */
df58fc94 10970 gas_assert (!mips_opts.micromips);
03f66e8a
MR
10971 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c'
10972 && s[3] >= '0' && s[3] <= '3')
74cd071d
CF
10973 {
10974 regno = s[3] - '0';
10975 s += 4;
df58fc94 10976 INSERT_OPERAND (0, DSPACC_S, *ip, regno);
74cd071d
CF
10977 continue;
10978 }
10979 else
10980 as_bad (_("Invalid dsp acc register"));
10981 break;
10982
03f66e8a
MR
10983 case '0':
10984 /* DSP 6-bit signed immediate in bit 16 (for standard MIPS
10985 code) or 20 (for microMIPS code). */
10986 {
10987 long mask = (mips_opts.micromips
10988 ? MICROMIPSOP_MASK_DSPSFT : OP_MASK_DSPSFT);
10989
10990 my_getExpression (&imm_expr, s);
10991 check_absolute_expr (ip, &imm_expr);
10992 min_range = -((mask + 1) >> 1);
10993 max_range = ((mask + 1) >> 1) - 1;
10994 if (imm_expr.X_add_number < min_range
10995 || imm_expr.X_add_number > max_range)
a9e24354
TS
10996 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
10997 (long) min_range, (long) max_range,
10998 (long) imm_expr.X_add_number);
03f66e8a
MR
10999 INSERT_OPERAND (mips_opts.micromips,
11000 DSPSFT, *ip, imm_expr.X_add_number);
11001 imm_expr.X_op = O_absent;
11002 s = expr_end;
11003 }
74cd071d
CF
11004 continue;
11005
90ecf173 11006 case '\'': /* DSP 6-bit unsigned immediate in bit 16. */
df58fc94 11007 gas_assert (!mips_opts.micromips);
74cd071d
CF
11008 my_getExpression (&imm_expr, s);
11009 check_absolute_expr (ip, &imm_expr);
11010 if (imm_expr.X_add_number & ~OP_MASK_RDDSP)
11011 {
a9e24354
TS
11012 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
11013 OP_MASK_RDDSP,
11014 (unsigned long) imm_expr.X_add_number);
74cd071d 11015 }
df58fc94 11016 INSERT_OPERAND (0, RDDSP, *ip, imm_expr.X_add_number);
74cd071d
CF
11017 imm_expr.X_op = O_absent;
11018 s = expr_end;
11019 continue;
11020
90ecf173 11021 case ':': /* DSP 7-bit signed immediate in bit 19. */
df58fc94 11022 gas_assert (!mips_opts.micromips);
74cd071d
CF
11023 my_getExpression (&imm_expr, s);
11024 check_absolute_expr (ip, &imm_expr);
11025 min_range = -((OP_MASK_DSPSFT_7 + 1) >> 1);
11026 max_range = ((OP_MASK_DSPSFT_7 + 1) >> 1) - 1;
11027 if (imm_expr.X_add_number < min_range ||
11028 imm_expr.X_add_number > max_range)
11029 {
a9e24354
TS
11030 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11031 (long) min_range, (long) max_range,
11032 (long) imm_expr.X_add_number);
74cd071d 11033 }
df58fc94 11034 INSERT_OPERAND (0, DSPSFT_7, *ip, imm_expr.X_add_number);
74cd071d
CF
11035 imm_expr.X_op = O_absent;
11036 s = expr_end;
11037 continue;
11038
90ecf173 11039 case '@': /* DSP 10-bit signed immediate in bit 16. */
03f66e8a
MR
11040 {
11041 long mask = (mips_opts.micromips
11042 ? MICROMIPSOP_MASK_IMM10 : OP_MASK_IMM10);
11043
11044 my_getExpression (&imm_expr, s);
11045 check_absolute_expr (ip, &imm_expr);
11046 min_range = -((mask + 1) >> 1);
11047 max_range = ((mask + 1) >> 1) - 1;
11048 if (imm_expr.X_add_number < min_range
11049 || imm_expr.X_add_number > max_range)
a9e24354
TS
11050 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11051 (long) min_range, (long) max_range,
11052 (long) imm_expr.X_add_number);
03f66e8a
MR
11053 INSERT_OPERAND (mips_opts.micromips,
11054 IMM10, *ip, imm_expr.X_add_number);
11055 imm_expr.X_op = O_absent;
11056 s = expr_end;
11057 }
11058 continue;
11059
11060 case '^': /* DSP 5-bit unsigned immediate in bit 11. */
11061 gas_assert (mips_opts.micromips);
11062 my_getExpression (&imm_expr, s);
11063 check_absolute_expr (ip, &imm_expr);
11064 if (imm_expr.X_add_number & ~MICROMIPSOP_MASK_RD)
11065 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
11066 MICROMIPSOP_MASK_RD,
11067 (unsigned long) imm_expr.X_add_number);
11068 INSERT_OPERAND (1, RD, *ip, imm_expr.X_add_number);
74cd071d
CF
11069 imm_expr.X_op = O_absent;
11070 s = expr_end;
11071 continue;
11072
a9e24354 11073 case '!': /* MT usermode flag bit. */
df58fc94 11074 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11075 my_getExpression (&imm_expr, s);
11076 check_absolute_expr (ip, &imm_expr);
11077 if (imm_expr.X_add_number & ~OP_MASK_MT_U)
a9e24354
TS
11078 as_bad (_("MT usermode bit not 0 or 1 (%lu)"),
11079 (unsigned long) imm_expr.X_add_number);
df58fc94 11080 INSERT_OPERAND (0, MT_U, *ip, imm_expr.X_add_number);
ef2e4d86
CF
11081 imm_expr.X_op = O_absent;
11082 s = expr_end;
11083 continue;
11084
a9e24354 11085 case '$': /* MT load high flag bit. */
df58fc94 11086 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11087 my_getExpression (&imm_expr, s);
11088 check_absolute_expr (ip, &imm_expr);
11089 if (imm_expr.X_add_number & ~OP_MASK_MT_H)
a9e24354
TS
11090 as_bad (_("MT load high bit not 0 or 1 (%lu)"),
11091 (unsigned long) imm_expr.X_add_number);
df58fc94 11092 INSERT_OPERAND (0, MT_H, *ip, imm_expr.X_add_number);
ef2e4d86
CF
11093 imm_expr.X_op = O_absent;
11094 s = expr_end;
11095 continue;
11096
90ecf173 11097 case '*': /* Four DSP accumulators in bits 18,19. */
df58fc94 11098 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11099 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
11100 s[3] >= '0' && s[3] <= '3')
11101 {
11102 regno = s[3] - '0';
11103 s += 4;
df58fc94 11104 INSERT_OPERAND (0, MTACC_T, *ip, regno);
ef2e4d86
CF
11105 continue;
11106 }
11107 else
11108 as_bad (_("Invalid dsp/smartmips acc register"));
11109 break;
11110
90ecf173 11111 case '&': /* Four DSP accumulators in bits 13,14. */
df58fc94 11112 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11113 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
11114 s[3] >= '0' && s[3] <= '3')
11115 {
11116 regno = s[3] - '0';
11117 s += 4;
df58fc94 11118 INSERT_OPERAND (0, MTACC_D, *ip, regno);
ef2e4d86
CF
11119 continue;
11120 }
11121 else
11122 as_bad (_("Invalid dsp/smartmips acc register"));
11123 break;
11124
dec0624d
MR
11125 case '\\': /* 3-bit bit position. */
11126 {
2906b037
MR
11127 unsigned long mask = (mips_opts.micromips
11128 ? MICROMIPSOP_MASK_3BITPOS
11129 : OP_MASK_3BITPOS);
dec0624d
MR
11130
11131 my_getExpression (&imm_expr, s);
11132 check_absolute_expr (ip, &imm_expr);
11133 if ((unsigned long) imm_expr.X_add_number > mask)
11134 as_warn (_("Bit position for %s not in range 0..%lu (%lu)"),
11135 ip->insn_mo->name,
11136 mask, (unsigned long) imm_expr.X_add_number);
11137 INSERT_OPERAND (mips_opts.micromips,
11138 3BITPOS, *ip, imm_expr.X_add_number);
11139 imm_expr.X_op = O_absent;
11140 s = expr_end;
11141 }
11142 continue;
11143
252b5132 11144 case ',':
a339155f 11145 ++argnum;
252b5132
RH
11146 if (*s++ == *args)
11147 continue;
11148 s--;
11149 switch (*++args)
11150 {
11151 case 'r':
11152 case 'v':
df58fc94 11153 INSERT_OPERAND (mips_opts.micromips, RS, *ip, lastregno);
252b5132
RH
11154 continue;
11155
11156 case 'w':
df58fc94 11157 INSERT_OPERAND (mips_opts.micromips, RT, *ip, lastregno);
38487616
TS
11158 continue;
11159
252b5132 11160 case 'W':
df58fc94
RS
11161 gas_assert (!mips_opts.micromips);
11162 INSERT_OPERAND (0, FT, *ip, lastregno);
252b5132
RH
11163 continue;
11164
11165 case 'V':
df58fc94 11166 INSERT_OPERAND (mips_opts.micromips, FS, *ip, lastregno);
252b5132
RH
11167 continue;
11168 }
11169 break;
11170
11171 case '(':
11172 /* Handle optional base register.
11173 Either the base register is omitted or
bdaaa2e1 11174 we must have a left paren. */
252b5132
RH
11175 /* This is dependent on the next operand specifier
11176 is a base register specification. */
df58fc94
RS
11177 gas_assert (args[1] == 'b'
11178 || (mips_opts.micromips
11179 && args[1] == 'm'
11180 && (args[2] == 'l' || args[2] == 'n'
11181 || args[2] == 's' || args[2] == 'a')));
11182 if (*s == '\0' && args[1] == 'b')
252b5132 11183 return;
df58fc94 11184 /* Fall through. */
252b5132 11185
90ecf173 11186 case ')': /* These must match exactly. */
df58fc94
RS
11187 if (*s++ == *args)
11188 continue;
11189 break;
11190
11191 case '[': /* These must match exactly. */
60b63b72 11192 case ']':
df58fc94 11193 gas_assert (!mips_opts.micromips);
252b5132
RH
11194 if (*s++ == *args)
11195 continue;
11196 break;
11197
af7ee8bf
CD
11198 case '+': /* Opcode extension character. */
11199 switch (*++args)
11200 {
9bcd4f99
TS
11201 case '1': /* UDI immediates. */
11202 case '2':
11203 case '3':
11204 case '4':
df58fc94 11205 gas_assert (!mips_opts.micromips);
9bcd4f99
TS
11206 {
11207 const struct mips_immed *imm = mips_immed;
11208
11209 while (imm->type && imm->type != *args)
11210 ++imm;
11211 if (! imm->type)
11212 internalError ();
11213 my_getExpression (&imm_expr, s);
11214 check_absolute_expr (ip, &imm_expr);
11215 if ((unsigned long) imm_expr.X_add_number & ~imm->mask)
11216 {
11217 as_warn (_("Illegal %s number (%lu, 0x%lx)"),
11218 imm->desc ? imm->desc : ip->insn_mo->name,
11219 (unsigned long) imm_expr.X_add_number,
11220 (unsigned long) imm_expr.X_add_number);
90ecf173 11221 imm_expr.X_add_number &= imm->mask;
9bcd4f99
TS
11222 }
11223 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
11224 << imm->shift);
11225 imm_expr.X_op = O_absent;
11226 s = expr_end;
11227 }
11228 continue;
90ecf173 11229
071742cf
CD
11230 case 'A': /* ins/ext position, becomes LSB. */
11231 limlo = 0;
11232 limhi = 31;
5f74bc13
CD
11233 goto do_lsb;
11234 case 'E':
11235 limlo = 32;
11236 limhi = 63;
11237 goto do_lsb;
90ecf173 11238 do_lsb:
071742cf
CD
11239 my_getExpression (&imm_expr, s);
11240 check_absolute_expr (ip, &imm_expr);
11241 if ((unsigned long) imm_expr.X_add_number < limlo
11242 || (unsigned long) imm_expr.X_add_number > limhi)
11243 {
11244 as_bad (_("Improper position (%lu)"),
11245 (unsigned long) imm_expr.X_add_number);
11246 imm_expr.X_add_number = limlo;
11247 }
11248 lastpos = imm_expr.X_add_number;
df58fc94
RS
11249 INSERT_OPERAND (mips_opts.micromips,
11250 EXTLSB, *ip, imm_expr.X_add_number);
071742cf
CD
11251 imm_expr.X_op = O_absent;
11252 s = expr_end;
11253 continue;
11254
11255 case 'B': /* ins size, becomes MSB. */
11256 limlo = 1;
11257 limhi = 32;
5f74bc13
CD
11258 goto do_msb;
11259 case 'F':
11260 limlo = 33;
11261 limhi = 64;
11262 goto do_msb;
90ecf173 11263 do_msb:
071742cf
CD
11264 my_getExpression (&imm_expr, s);
11265 check_absolute_expr (ip, &imm_expr);
11266 /* Check for negative input so that small negative numbers
11267 will not succeed incorrectly. The checks against
11268 (pos+size) transitively check "size" itself,
11269 assuming that "pos" is reasonable. */
11270 if ((long) imm_expr.X_add_number < 0
11271 || ((unsigned long) imm_expr.X_add_number
11272 + lastpos) < limlo
11273 || ((unsigned long) imm_expr.X_add_number
11274 + lastpos) > limhi)
11275 {
11276 as_bad (_("Improper insert size (%lu, position %lu)"),
11277 (unsigned long) imm_expr.X_add_number,
11278 (unsigned long) lastpos);
11279 imm_expr.X_add_number = limlo - lastpos;
11280 }
df58fc94
RS
11281 INSERT_OPERAND (mips_opts.micromips, INSMSB, *ip,
11282 lastpos + imm_expr.X_add_number - 1);
071742cf
CD
11283 imm_expr.X_op = O_absent;
11284 s = expr_end;
11285 continue;
11286
11287 case 'C': /* ext size, becomes MSBD. */
11288 limlo = 1;
11289 limhi = 32;
5f74bc13
CD
11290 goto do_msbd;
11291 case 'G':
11292 limlo = 33;
11293 limhi = 64;
11294 goto do_msbd;
11295 case 'H':
11296 limlo = 33;
11297 limhi = 64;
11298 goto do_msbd;
90ecf173 11299 do_msbd:
071742cf
CD
11300 my_getExpression (&imm_expr, s);
11301 check_absolute_expr (ip, &imm_expr);
11302 /* Check for negative input so that small negative numbers
11303 will not succeed incorrectly. The checks against
11304 (pos+size) transitively check "size" itself,
11305 assuming that "pos" is reasonable. */
11306 if ((long) imm_expr.X_add_number < 0
11307 || ((unsigned long) imm_expr.X_add_number
11308 + lastpos) < limlo
11309 || ((unsigned long) imm_expr.X_add_number
11310 + lastpos) > limhi)
11311 {
11312 as_bad (_("Improper extract size (%lu, position %lu)"),
11313 (unsigned long) imm_expr.X_add_number,
11314 (unsigned long) lastpos);
11315 imm_expr.X_add_number = limlo - lastpos;
11316 }
df58fc94
RS
11317 INSERT_OPERAND (mips_opts.micromips,
11318 EXTMSBD, *ip, imm_expr.X_add_number - 1);
071742cf
CD
11319 imm_expr.X_op = O_absent;
11320 s = expr_end;
11321 continue;
af7ee8bf 11322
bbcc0807
CD
11323 case 'D':
11324 /* +D is for disassembly only; never match. */
11325 break;
11326
5f74bc13
CD
11327 case 'I':
11328 /* "+I" is like "I", except that imm2_expr is used. */
11329 my_getExpression (&imm2_expr, s);
11330 if (imm2_expr.X_op != O_big
11331 && imm2_expr.X_op != O_constant)
11332 insn_error = _("absolute expression required");
9ee2a2d4
MR
11333 if (HAVE_32BIT_GPRS)
11334 normalize_constant_expr (&imm2_expr);
5f74bc13
CD
11335 s = expr_end;
11336 continue;
11337
707bfff6 11338 case 'T': /* Coprocessor register. */
df58fc94 11339 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11340 /* +T is for disassembly only; never match. */
11341 break;
11342
707bfff6 11343 case 't': /* Coprocessor register number. */
df58fc94 11344 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11345 if (s[0] == '$' && ISDIGIT (s[1]))
11346 {
11347 ++s;
11348 regno = 0;
11349 do
11350 {
11351 regno *= 10;
11352 regno += *s - '0';
11353 ++s;
11354 }
11355 while (ISDIGIT (*s));
11356 if (regno > 31)
11357 as_bad (_("Invalid register number (%d)"), regno);
11358 else
11359 {
df58fc94 11360 INSERT_OPERAND (0, RT, *ip, regno);
ef2e4d86
CF
11361 continue;
11362 }
11363 }
11364 else
11365 as_bad (_("Invalid coprocessor 0 register number"));
11366 break;
11367
bb35fb24
NC
11368 case 'x':
11369 /* bbit[01] and bbit[01]32 bit index. Give error if index
11370 is not in the valid range. */
df58fc94 11371 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11372 my_getExpression (&imm_expr, s);
11373 check_absolute_expr (ip, &imm_expr);
11374 if ((unsigned) imm_expr.X_add_number > 31)
11375 {
11376 as_bad (_("Improper bit index (%lu)"),
11377 (unsigned long) imm_expr.X_add_number);
11378 imm_expr.X_add_number = 0;
11379 }
df58fc94 11380 INSERT_OPERAND (0, BBITIND, *ip, imm_expr.X_add_number);
bb35fb24
NC
11381 imm_expr.X_op = O_absent;
11382 s = expr_end;
11383 continue;
11384
11385 case 'X':
11386 /* bbit[01] bit index when bbit is used but we generate
11387 bbit[01]32 because the index is over 32. Move to the
11388 next candidate if index is not in the valid range. */
df58fc94 11389 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11390 my_getExpression (&imm_expr, s);
11391 check_absolute_expr (ip, &imm_expr);
11392 if ((unsigned) imm_expr.X_add_number < 32
11393 || (unsigned) imm_expr.X_add_number > 63)
11394 break;
df58fc94 11395 INSERT_OPERAND (0, BBITIND, *ip, imm_expr.X_add_number - 32);
bb35fb24
NC
11396 imm_expr.X_op = O_absent;
11397 s = expr_end;
11398 continue;
11399
11400 case 'p':
11401 /* cins, cins32, exts and exts32 position field. Give error
11402 if it's not in the valid range. */
df58fc94 11403 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11404 my_getExpression (&imm_expr, s);
11405 check_absolute_expr (ip, &imm_expr);
11406 if ((unsigned) imm_expr.X_add_number > 31)
11407 {
11408 as_bad (_("Improper position (%lu)"),
11409 (unsigned long) imm_expr.X_add_number);
11410 imm_expr.X_add_number = 0;
11411 }
11412 /* Make the pos explicit to simplify +S. */
11413 lastpos = imm_expr.X_add_number + 32;
df58fc94 11414 INSERT_OPERAND (0, CINSPOS, *ip, imm_expr.X_add_number);
bb35fb24
NC
11415 imm_expr.X_op = O_absent;
11416 s = expr_end;
11417 continue;
11418
11419 case 'P':
11420 /* cins, cins32, exts and exts32 position field. Move to
11421 the next candidate if it's not in the valid range. */
df58fc94 11422 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11423 my_getExpression (&imm_expr, s);
11424 check_absolute_expr (ip, &imm_expr);
11425 if ((unsigned) imm_expr.X_add_number < 32
11426 || (unsigned) imm_expr.X_add_number > 63)
11427 break;
11428 lastpos = imm_expr.X_add_number;
df58fc94 11429 INSERT_OPERAND (0, CINSPOS, *ip, imm_expr.X_add_number - 32);
bb35fb24
NC
11430 imm_expr.X_op = O_absent;
11431 s = expr_end;
11432 continue;
11433
11434 case 's':
11435 /* cins and exts length-minus-one field. */
df58fc94 11436 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11437 my_getExpression (&imm_expr, s);
11438 check_absolute_expr (ip, &imm_expr);
11439 if ((unsigned long) imm_expr.X_add_number > 31)
11440 {
11441 as_bad (_("Improper size (%lu)"),
11442 (unsigned long) imm_expr.X_add_number);
11443 imm_expr.X_add_number = 0;
11444 }
df58fc94 11445 INSERT_OPERAND (0, CINSLM1, *ip, imm_expr.X_add_number);
bb35fb24
NC
11446 imm_expr.X_op = O_absent;
11447 s = expr_end;
11448 continue;
11449
11450 case 'S':
11451 /* cins32/exts32 and cins/exts aliasing cint32/exts32
11452 length-minus-one field. */
df58fc94 11453 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11454 my_getExpression (&imm_expr, s);
11455 check_absolute_expr (ip, &imm_expr);
11456 if ((long) imm_expr.X_add_number < 0
11457 || (unsigned long) imm_expr.X_add_number + lastpos > 63)
11458 {
11459 as_bad (_("Improper size (%lu)"),
11460 (unsigned long) imm_expr.X_add_number);
11461 imm_expr.X_add_number = 0;
11462 }
df58fc94 11463 INSERT_OPERAND (0, CINSLM1, *ip, imm_expr.X_add_number);
bb35fb24
NC
11464 imm_expr.X_op = O_absent;
11465 s = expr_end;
11466 continue;
11467
dd3cbb7e
NC
11468 case 'Q':
11469 /* seqi/snei immediate field. */
df58fc94 11470 gas_assert (!mips_opts.micromips);
dd3cbb7e
NC
11471 my_getExpression (&imm_expr, s);
11472 check_absolute_expr (ip, &imm_expr);
11473 if ((long) imm_expr.X_add_number < -512
11474 || (long) imm_expr.X_add_number >= 512)
11475 {
11476 as_bad (_("Improper immediate (%ld)"),
11477 (long) imm_expr.X_add_number);
11478 imm_expr.X_add_number = 0;
11479 }
df58fc94 11480 INSERT_OPERAND (0, SEQI, *ip, imm_expr.X_add_number);
dd3cbb7e
NC
11481 imm_expr.X_op = O_absent;
11482 s = expr_end;
11483 continue;
11484
98675402 11485 case 'a': /* 8-bit signed offset in bit 6 */
df58fc94 11486 gas_assert (!mips_opts.micromips);
98675402
RS
11487 my_getExpression (&imm_expr, s);
11488 check_absolute_expr (ip, &imm_expr);
11489 min_range = -((OP_MASK_OFFSET_A + 1) >> 1);
11490 max_range = ((OP_MASK_OFFSET_A + 1) >> 1) - 1;
11491 if (imm_expr.X_add_number < min_range
11492 || imm_expr.X_add_number > max_range)
11493 {
c95354ed 11494 as_bad (_("Offset not in range %ld..%ld (%ld)"),
98675402
RS
11495 (long) min_range, (long) max_range,
11496 (long) imm_expr.X_add_number);
11497 }
df58fc94 11498 INSERT_OPERAND (0, OFFSET_A, *ip, imm_expr.X_add_number);
98675402
RS
11499 imm_expr.X_op = O_absent;
11500 s = expr_end;
11501 continue;
11502
11503 case 'b': /* 8-bit signed offset in bit 3 */
df58fc94 11504 gas_assert (!mips_opts.micromips);
98675402
RS
11505 my_getExpression (&imm_expr, s);
11506 check_absolute_expr (ip, &imm_expr);
11507 min_range = -((OP_MASK_OFFSET_B + 1) >> 1);
11508 max_range = ((OP_MASK_OFFSET_B + 1) >> 1) - 1;
11509 if (imm_expr.X_add_number < min_range
11510 || imm_expr.X_add_number > max_range)
11511 {
c95354ed 11512 as_bad (_("Offset not in range %ld..%ld (%ld)"),
98675402
RS
11513 (long) min_range, (long) max_range,
11514 (long) imm_expr.X_add_number);
11515 }
df58fc94 11516 INSERT_OPERAND (0, OFFSET_B, *ip, imm_expr.X_add_number);
98675402
RS
11517 imm_expr.X_op = O_absent;
11518 s = expr_end;
11519 continue;
11520
11521 case 'c': /* 9-bit signed offset in bit 6 */
df58fc94 11522 gas_assert (!mips_opts.micromips);
98675402
RS
11523 my_getExpression (&imm_expr, s);
11524 check_absolute_expr (ip, &imm_expr);
11525 min_range = -((OP_MASK_OFFSET_C + 1) >> 1);
11526 max_range = ((OP_MASK_OFFSET_C + 1) >> 1) - 1;
c95354ed
MX
11527 /* We check the offset range before adjusted. */
11528 min_range <<= 4;
11529 max_range <<= 4;
98675402
RS
11530 if (imm_expr.X_add_number < min_range
11531 || imm_expr.X_add_number > max_range)
11532 {
c95354ed 11533 as_bad (_("Offset not in range %ld..%ld (%ld)"),
98675402
RS
11534 (long) min_range, (long) max_range,
11535 (long) imm_expr.X_add_number);
11536 }
c95354ed
MX
11537 if (imm_expr.X_add_number & 0xf)
11538 {
11539 as_bad (_("Offset not 16 bytes alignment (%ld)"),
11540 (long) imm_expr.X_add_number);
11541 }
11542 /* Right shift 4 bits to adjust the offset operand. */
df58fc94
RS
11543 INSERT_OPERAND (0, OFFSET_C, *ip,
11544 imm_expr.X_add_number >> 4);
98675402
RS
11545 imm_expr.X_op = O_absent;
11546 s = expr_end;
11547 continue;
11548
11549 case 'z':
df58fc94 11550 gas_assert (!mips_opts.micromips);
98675402
RS
11551 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
11552 break;
11553 if (regno == AT && mips_opts.at)
11554 {
11555 if (mips_opts.at == ATREG)
11556 as_warn (_("used $at without \".set noat\""));
11557 else
11558 as_warn (_("used $%u with \".set at=$%u\""),
11559 regno, mips_opts.at);
11560 }
df58fc94 11561 INSERT_OPERAND (0, RZ, *ip, regno);
98675402
RS
11562 continue;
11563
11564 case 'Z':
df58fc94 11565 gas_assert (!mips_opts.micromips);
98675402
RS
11566 if (!reg_lookup (&s, RTYPE_FPU, &regno))
11567 break;
df58fc94 11568 INSERT_OPERAND (0, FZ, *ip, regno);
98675402
RS
11569 continue;
11570
af7ee8bf 11571 default:
df58fc94 11572 as_bad (_("Internal error: bad %s opcode "
90ecf173 11573 "(unknown extension operand type `+%c'): %s %s"),
df58fc94 11574 mips_opts.micromips ? "microMIPS" : "MIPS",
90ecf173 11575 *args, insn->name, insn->args);
af7ee8bf
CD
11576 /* Further processing is fruitless. */
11577 return;
11578 }
11579 break;
11580
df58fc94 11581 case '.': /* 10-bit offset. */
df58fc94 11582 gas_assert (mips_opts.micromips);
dec0624d 11583 case '~': /* 12-bit offset. */
df58fc94
RS
11584 {
11585 int shift = *args == '.' ? 9 : 11;
11586 size_t i;
11587
11588 /* Check whether there is only a single bracketed expression
11589 left. If so, it must be the base register and the
11590 constant must be zero. */
11591 if (*s == '(' && strchr (s + 1, '(') == 0)
11592 continue;
11593
11594 /* If this value won't fit into the offset, then go find
11595 a macro that will generate a 16- or 32-bit offset code
11596 pattern. */
11597 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
11598 if ((i == 0 && (imm_expr.X_op != O_constant
11599 || imm_expr.X_add_number >= 1 << shift
11600 || imm_expr.X_add_number < -1 << shift))
11601 || i > 0)
11602 {
11603 imm_expr.X_op = O_absent;
11604 break;
11605 }
11606 if (shift == 9)
11607 INSERT_OPERAND (1, OFFSET10, *ip, imm_expr.X_add_number);
11608 else
dec0624d
MR
11609 INSERT_OPERAND (mips_opts.micromips,
11610 OFFSET12, *ip, imm_expr.X_add_number);
df58fc94
RS
11611 imm_expr.X_op = O_absent;
11612 s = expr_end;
11613 }
11614 continue;
11615
252b5132
RH
11616 case '<': /* must be at least one digit */
11617 /*
11618 * According to the manual, if the shift amount is greater
b6ff326e
KH
11619 * than 31 or less than 0, then the shift amount should be
11620 * mod 32. In reality the mips assembler issues an error.
252b5132
RH
11621 * We issue a warning and mask out all but the low 5 bits.
11622 */
11623 my_getExpression (&imm_expr, s);
11624 check_absolute_expr (ip, &imm_expr);
11625 if ((unsigned long) imm_expr.X_add_number > 31)
bf12938e
RS
11626 as_warn (_("Improper shift amount (%lu)"),
11627 (unsigned long) imm_expr.X_add_number);
df58fc94
RS
11628 INSERT_OPERAND (mips_opts.micromips,
11629 SHAMT, *ip, imm_expr.X_add_number);
252b5132
RH
11630 imm_expr.X_op = O_absent;
11631 s = expr_end;
11632 continue;
11633
11634 case '>': /* shift amount minus 32 */
11635 my_getExpression (&imm_expr, s);
11636 check_absolute_expr (ip, &imm_expr);
11637 if ((unsigned long) imm_expr.X_add_number < 32
11638 || (unsigned long) imm_expr.X_add_number > 63)
11639 break;
df58fc94
RS
11640 INSERT_OPERAND (mips_opts.micromips,
11641 SHAMT, *ip, imm_expr.X_add_number - 32);
252b5132
RH
11642 imm_expr.X_op = O_absent;
11643 s = expr_end;
11644 continue;
11645
90ecf173
MR
11646 case 'k': /* CACHE code. */
11647 case 'h': /* PREFX code. */
11648 case '1': /* SYNC type. */
252b5132
RH
11649 my_getExpression (&imm_expr, s);
11650 check_absolute_expr (ip, &imm_expr);
11651 if ((unsigned long) imm_expr.X_add_number > 31)
bf12938e
RS
11652 as_warn (_("Invalid value for `%s' (%lu)"),
11653 ip->insn_mo->name,
11654 (unsigned long) imm_expr.X_add_number);
df58fc94 11655 switch (*args)
d954098f 11656 {
df58fc94
RS
11657 case 'k':
11658 if (mips_fix_cn63xxp1
11659 && !mips_opts.micromips
11660 && strcmp ("pref", insn->name) == 0)
d954098f
DD
11661 switch (imm_expr.X_add_number)
11662 {
11663 case 5:
11664 case 25:
11665 case 26:
11666 case 27:
11667 case 28:
11668 case 29:
11669 case 30:
11670 case 31: /* These are ok. */
11671 break;
11672
11673 default: /* The rest must be changed to 28. */
11674 imm_expr.X_add_number = 28;
11675 break;
11676 }
df58fc94
RS
11677 INSERT_OPERAND (mips_opts.micromips,
11678 CACHE, *ip, imm_expr.X_add_number);
11679 break;
11680 case 'h':
11681 INSERT_OPERAND (mips_opts.micromips,
11682 PREFX, *ip, imm_expr.X_add_number);
11683 break;
11684 case '1':
11685 INSERT_OPERAND (mips_opts.micromips,
11686 STYPE, *ip, imm_expr.X_add_number);
11687 break;
d954098f 11688 }
252b5132
RH
11689 imm_expr.X_op = O_absent;
11690 s = expr_end;
11691 continue;
11692
90ecf173 11693 case 'c': /* BREAK code. */
df58fc94
RS
11694 {
11695 unsigned long mask = (mips_opts.micromips
11696 ? MICROMIPSOP_MASK_CODE
11697 : OP_MASK_CODE);
11698
11699 my_getExpression (&imm_expr, s);
11700 check_absolute_expr (ip, &imm_expr);
11701 if ((unsigned long) imm_expr.X_add_number > mask)
11702 as_warn (_("Code for %s not in range 0..%lu (%lu)"),
11703 ip->insn_mo->name,
11704 mask, (unsigned long) imm_expr.X_add_number);
11705 INSERT_OPERAND (mips_opts.micromips,
11706 CODE, *ip, imm_expr.X_add_number);
11707 imm_expr.X_op = O_absent;
11708 s = expr_end;
11709 }
252b5132
RH
11710 continue;
11711
90ecf173 11712 case 'q': /* Lower BREAK code. */
df58fc94
RS
11713 {
11714 unsigned long mask = (mips_opts.micromips
11715 ? MICROMIPSOP_MASK_CODE2
11716 : OP_MASK_CODE2);
11717
11718 my_getExpression (&imm_expr, s);
11719 check_absolute_expr (ip, &imm_expr);
11720 if ((unsigned long) imm_expr.X_add_number > mask)
11721 as_warn (_("Lower code for %s not in range 0..%lu (%lu)"),
11722 ip->insn_mo->name,
11723 mask, (unsigned long) imm_expr.X_add_number);
11724 INSERT_OPERAND (mips_opts.micromips,
11725 CODE2, *ip, imm_expr.X_add_number);
11726 imm_expr.X_op = O_absent;
11727 s = expr_end;
11728 }
252b5132
RH
11729 continue;
11730
df58fc94
RS
11731 case 'B': /* 20- or 10-bit syscall/break/wait code. */
11732 {
11733 unsigned long mask = (mips_opts.micromips
11734 ? MICROMIPSOP_MASK_CODE10
11735 : OP_MASK_CODE20);
11736
11737 my_getExpression (&imm_expr, s);
11738 check_absolute_expr (ip, &imm_expr);
11739 if ((unsigned long) imm_expr.X_add_number > mask)
11740 as_warn (_("Code for %s not in range 0..%lu (%lu)"),
11741 ip->insn_mo->name,
11742 mask, (unsigned long) imm_expr.X_add_number);
11743 if (mips_opts.micromips)
11744 INSERT_OPERAND (1, CODE10, *ip, imm_expr.X_add_number);
11745 else
11746 INSERT_OPERAND (0, CODE20, *ip, imm_expr.X_add_number);
11747 imm_expr.X_op = O_absent;
11748 s = expr_end;
11749 }
252b5132
RH
11750 continue;
11751
df58fc94
RS
11752 case 'C': /* 25- or 23-bit coprocessor code. */
11753 {
11754 unsigned long mask = (mips_opts.micromips
11755 ? MICROMIPSOP_MASK_COPZ
11756 : OP_MASK_COPZ);
11757
11758 my_getExpression (&imm_expr, s);
11759 check_absolute_expr (ip, &imm_expr);
11760 if ((unsigned long) imm_expr.X_add_number > mask)
11761 as_warn (_("Coproccesor code > %u bits (%lu)"),
11762 mips_opts.micromips ? 23U : 25U,
793b27f4 11763 (unsigned long) imm_expr.X_add_number);
df58fc94
RS
11764 INSERT_OPERAND (mips_opts.micromips,
11765 COPZ, *ip, imm_expr.X_add_number);
11766 imm_expr.X_op = O_absent;
11767 s = expr_end;
11768 }
beae10d5 11769 continue;
252b5132 11770
df58fc94
RS
11771 case 'J': /* 19-bit WAIT code. */
11772 gas_assert (!mips_opts.micromips);
4372b673
NC
11773 my_getExpression (&imm_expr, s);
11774 check_absolute_expr (ip, &imm_expr);
793b27f4 11775 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
df58fc94
RS
11776 {
11777 as_warn (_("Illegal 19-bit code (%lu)"),
a9e24354 11778 (unsigned long) imm_expr.X_add_number);
df58fc94
RS
11779 imm_expr.X_add_number &= OP_MASK_CODE19;
11780 }
11781 INSERT_OPERAND (0, CODE19, *ip, imm_expr.X_add_number);
4372b673
NC
11782 imm_expr.X_op = O_absent;
11783 s = expr_end;
11784 continue;
11785
707bfff6 11786 case 'P': /* Performance register. */
df58fc94 11787 gas_assert (!mips_opts.micromips);
beae10d5 11788 my_getExpression (&imm_expr, s);
252b5132 11789 check_absolute_expr (ip, &imm_expr);
beae10d5 11790 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
bf12938e
RS
11791 as_warn (_("Invalid performance register (%lu)"),
11792 (unsigned long) imm_expr.X_add_number);
df58fc94 11793 INSERT_OPERAND (0, PERFREG, *ip, imm_expr.X_add_number);
beae10d5
KH
11794 imm_expr.X_op = O_absent;
11795 s = expr_end;
11796 continue;
252b5132 11797
707bfff6 11798 case 'G': /* Coprocessor destination register. */
df58fc94
RS
11799 {
11800 unsigned long opcode = ip->insn_opcode;
11801 unsigned long mask;
11802 unsigned int types;
11803 int cop0;
11804
11805 if (mips_opts.micromips)
11806 {
11807 mask = ~((MICROMIPSOP_MASK_RT << MICROMIPSOP_SH_RT)
11808 | (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS)
11809 | (MICROMIPSOP_MASK_SEL << MICROMIPSOP_SH_SEL));
11810 opcode &= mask;
11811 switch (opcode)
11812 {
11813 case 0x000000fc: /* mfc0 */
11814 case 0x000002fc: /* mtc0 */
11815 case 0x580000fc: /* dmfc0 */
11816 case 0x580002fc: /* dmtc0 */
11817 cop0 = 1;
11818 break;
11819 default:
11820 cop0 = 0;
11821 break;
11822 }
11823 }
11824 else
11825 {
11826 opcode = (opcode >> OP_SH_OP) & OP_MASK_OP;
11827 cop0 = opcode == OP_OP_COP0;
11828 }
11829 types = RTYPE_NUM | (cop0 ? RTYPE_CP0 : RTYPE_GP);
11830 ok = reg_lookup (&s, types, &regno);
11831 if (mips_opts.micromips)
11832 INSERT_OPERAND (1, RS, *ip, regno);
11833 else
11834 INSERT_OPERAND (0, RD, *ip, regno);
11835 if (ok)
11836 {
11837 lastregno = regno;
11838 continue;
11839 }
11840 }
11841 break;
707bfff6 11842
df58fc94
RS
11843 case 'y': /* ALNV.PS source register. */
11844 gas_assert (mips_opts.micromips);
11845 goto do_reg;
11846 case 'x': /* Ignore register name. */
11847 case 'U': /* Destination register (CLO/CLZ). */
11848 case 'g': /* Coprocessor destination register. */
11849 gas_assert (!mips_opts.micromips);
90ecf173
MR
11850 case 'b': /* Base register. */
11851 case 'd': /* Destination register. */
11852 case 's': /* Source register. */
11853 case 't': /* Target register. */
11854 case 'r': /* Both target and source. */
11855 case 'v': /* Both dest and source. */
11856 case 'w': /* Both dest and target. */
11857 case 'E': /* Coprocessor target register. */
11858 case 'K': /* RDHWR destination register. */
90ecf173 11859 case 'z': /* Must be zero register. */
df58fc94 11860 do_reg:
90ecf173 11861 s_reset = s;
707bfff6
TS
11862 if (*args == 'E' || *args == 'K')
11863 ok = reg_lookup (&s, RTYPE_NUM, &regno);
11864 else
11865 {
11866 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
741fe287
MR
11867 if (regno == AT && mips_opts.at)
11868 {
11869 if (mips_opts.at == ATREG)
f71d0d44 11870 as_warn (_("Used $at without \".set noat\""));
741fe287 11871 else
f71d0d44 11872 as_warn (_("Used $%u with \".set at=$%u\""),
741fe287
MR
11873 regno, mips_opts.at);
11874 }
707bfff6
TS
11875 }
11876 if (ok)
252b5132 11877 {
252b5132
RH
11878 c = *args;
11879 if (*s == ' ')
f9419b05 11880 ++s;
252b5132
RH
11881 if (args[1] != *s)
11882 {
11883 if (c == 'r' || c == 'v' || c == 'w')
11884 {
11885 regno = lastregno;
11886 s = s_reset;
f9419b05 11887 ++args;
252b5132
RH
11888 }
11889 }
11890 /* 'z' only matches $0. */
11891 if (c == 'z' && regno != 0)
11892 break;
11893
24864476 11894 if (c == 's' && !strncmp (ip->insn_mo->name, "jalr", 4))
e7c604dd
CM
11895 {
11896 if (regno == lastregno)
90ecf173
MR
11897 {
11898 insn_error
f71d0d44 11899 = _("Source and destination must be different");
e7c604dd 11900 continue;
90ecf173 11901 }
24864476 11902 if (regno == 31 && lastregno == 0xffffffff)
90ecf173
MR
11903 {
11904 insn_error
f71d0d44 11905 = _("A destination register must be supplied");
e7c604dd 11906 continue;
90ecf173 11907 }
e7c604dd 11908 }
90ecf173
MR
11909 /* Now that we have assembled one operand, we use the args
11910 string to figure out where it goes in the instruction. */
252b5132
RH
11911 switch (c)
11912 {
11913 case 'r':
11914 case 's':
11915 case 'v':
11916 case 'b':
df58fc94 11917 INSERT_OPERAND (mips_opts.micromips, RS, *ip, regno);
252b5132 11918 break;
df58fc94 11919
af7ee8bf 11920 case 'K':
df58fc94
RS
11921 if (mips_opts.micromips)
11922 INSERT_OPERAND (1, RS, *ip, regno);
11923 else
11924 INSERT_OPERAND (0, RD, *ip, regno);
11925 break;
11926
11927 case 'd':
ef2e4d86 11928 case 'g':
df58fc94 11929 INSERT_OPERAND (mips_opts.micromips, RD, *ip, regno);
252b5132 11930 break;
df58fc94 11931
4372b673 11932 case 'U':
df58fc94
RS
11933 gas_assert (!mips_opts.micromips);
11934 INSERT_OPERAND (0, RD, *ip, regno);
11935 INSERT_OPERAND (0, RT, *ip, regno);
4372b673 11936 break;
df58fc94 11937
252b5132
RH
11938 case 'w':
11939 case 't':
11940 case 'E':
df58fc94
RS
11941 INSERT_OPERAND (mips_opts.micromips, RT, *ip, regno);
11942 break;
11943
11944 case 'y':
11945 gas_assert (mips_opts.micromips);
11946 INSERT_OPERAND (1, RS3, *ip, regno);
252b5132 11947 break;
df58fc94 11948
252b5132
RH
11949 case 'x':
11950 /* This case exists because on the r3000 trunc
11951 expands into a macro which requires a gp
11952 register. On the r6000 or r4000 it is
11953 assembled into a single instruction which
11954 ignores the register. Thus the insn version
11955 is MIPS_ISA2 and uses 'x', and the macro
11956 version is MIPS_ISA1 and uses 't'. */
11957 break;
df58fc94 11958
252b5132
RH
11959 case 'z':
11960 /* This case is for the div instruction, which
11961 acts differently if the destination argument
11962 is $0. This only matches $0, and is checked
11963 outside the switch. */
11964 break;
252b5132
RH
11965 }
11966 lastregno = regno;
11967 continue;
11968 }
252b5132
RH
11969 switch (*args++)
11970 {
11971 case 'r':
11972 case 'v':
df58fc94 11973 INSERT_OPERAND (mips_opts.micromips, RS, *ip, lastregno);
252b5132 11974 continue;
df58fc94 11975
252b5132 11976 case 'w':
df58fc94 11977 INSERT_OPERAND (mips_opts.micromips, RT, *ip, lastregno);
252b5132
RH
11978 continue;
11979 }
11980 break;
11981
deec1734 11982 case 'O': /* MDMX alignment immediate constant. */
df58fc94 11983 gas_assert (!mips_opts.micromips);
deec1734
CD
11984 my_getExpression (&imm_expr, s);
11985 check_absolute_expr (ip, &imm_expr);
11986 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
20203fb9 11987 as_warn (_("Improper align amount (%ld), using low bits"),
bf12938e 11988 (long) imm_expr.X_add_number);
df58fc94 11989 INSERT_OPERAND (0, ALN, *ip, imm_expr.X_add_number);
deec1734
CD
11990 imm_expr.X_op = O_absent;
11991 s = expr_end;
11992 continue;
11993
11994 case 'Q': /* MDMX vector, element sel, or const. */
11995 if (s[0] != '$')
11996 {
11997 /* MDMX Immediate. */
df58fc94 11998 gas_assert (!mips_opts.micromips);
deec1734
CD
11999 my_getExpression (&imm_expr, s);
12000 check_absolute_expr (ip, &imm_expr);
12001 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
bf12938e
RS
12002 as_warn (_("Invalid MDMX Immediate (%ld)"),
12003 (long) imm_expr.X_add_number);
df58fc94 12004 INSERT_OPERAND (0, FT, *ip, imm_expr.X_add_number);
deec1734
CD
12005 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
12006 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
12007 else
12008 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
deec1734
CD
12009 imm_expr.X_op = O_absent;
12010 s = expr_end;
12011 continue;
12012 }
12013 /* Not MDMX Immediate. Fall through. */
12014 case 'X': /* MDMX destination register. */
12015 case 'Y': /* MDMX source register. */
12016 case 'Z': /* MDMX target register. */
12017 is_mdmx = 1;
df58fc94
RS
12018 case 'W':
12019 gas_assert (!mips_opts.micromips);
90ecf173
MR
12020 case 'D': /* Floating point destination register. */
12021 case 'S': /* Floating point source register. */
12022 case 'T': /* Floating point target register. */
12023 case 'R': /* Floating point source register. */
252b5132 12024 case 'V':
707bfff6
TS
12025 rtype = RTYPE_FPU;
12026 if (is_mdmx
12027 || (mips_opts.ase_mdmx
12028 && (ip->insn_mo->pinfo & FP_D)
12029 && (ip->insn_mo->pinfo & (INSN_COPROC_MOVE_DELAY
12030 | INSN_COPROC_MEMORY_DELAY
12031 | INSN_LOAD_COPROC_DELAY
12032 | INSN_LOAD_MEMORY_DELAY
12033 | INSN_STORE_MEMORY))))
12034 rtype |= RTYPE_VEC;
252b5132 12035 s_reset = s;
707bfff6 12036 if (reg_lookup (&s, rtype, &regno))
252b5132 12037 {
252b5132 12038 if ((regno & 1) != 0
ca4e0257 12039 && HAVE_32BIT_FPRS
90ecf173 12040 && !mips_oddfpreg_ok (ip->insn_mo, argnum))
252b5132
RH
12041 as_warn (_("Float register should be even, was %d"),
12042 regno);
12043
12044 c = *args;
12045 if (*s == ' ')
f9419b05 12046 ++s;
252b5132
RH
12047 if (args[1] != *s)
12048 {
12049 if (c == 'V' || c == 'W')
12050 {
12051 regno = lastregno;
12052 s = s_reset;
f9419b05 12053 ++args;
252b5132
RH
12054 }
12055 }
12056 switch (c)
12057 {
12058 case 'D':
deec1734 12059 case 'X':
df58fc94 12060 INSERT_OPERAND (mips_opts.micromips, FD, *ip, regno);
252b5132 12061 break;
df58fc94 12062
252b5132
RH
12063 case 'V':
12064 case 'S':
deec1734 12065 case 'Y':
df58fc94 12066 INSERT_OPERAND (mips_opts.micromips, FS, *ip, regno);
252b5132 12067 break;
df58fc94 12068
deec1734
CD
12069 case 'Q':
12070 /* This is like 'Z', but also needs to fix the MDMX
12071 vector/scalar select bits. Note that the
12072 scalar immediate case is handled above. */
12073 if (*s == '[')
12074 {
12075 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
12076 int max_el = (is_qh ? 3 : 7);
12077 s++;
12078 my_getExpression(&imm_expr, s);
12079 check_absolute_expr (ip, &imm_expr);
12080 s = expr_end;
12081 if (imm_expr.X_add_number > max_el)
20203fb9
NC
12082 as_bad (_("Bad element selector %ld"),
12083 (long) imm_expr.X_add_number);
deec1734
CD
12084 imm_expr.X_add_number &= max_el;
12085 ip->insn_opcode |= (imm_expr.X_add_number
12086 << (OP_SH_VSEL +
12087 (is_qh ? 2 : 1)));
01a3f561 12088 imm_expr.X_op = O_absent;
deec1734 12089 if (*s != ']')
20203fb9 12090 as_warn (_("Expecting ']' found '%s'"), s);
deec1734
CD
12091 else
12092 s++;
12093 }
12094 else
12095 {
12096 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
12097 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
12098 << OP_SH_VSEL);
12099 else
12100 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
12101 OP_SH_VSEL);
12102 }
90ecf173 12103 /* Fall through. */
252b5132
RH
12104 case 'W':
12105 case 'T':
deec1734 12106 case 'Z':
df58fc94 12107 INSERT_OPERAND (mips_opts.micromips, FT, *ip, regno);
252b5132 12108 break;
df58fc94 12109
252b5132 12110 case 'R':
df58fc94 12111 INSERT_OPERAND (mips_opts.micromips, FR, *ip, regno);
252b5132
RH
12112 break;
12113 }
12114 lastregno = regno;
12115 continue;
12116 }
12117
252b5132
RH
12118 switch (*args++)
12119 {
12120 case 'V':
df58fc94 12121 INSERT_OPERAND (mips_opts.micromips, FS, *ip, lastregno);
252b5132 12122 continue;
df58fc94 12123
252b5132 12124 case 'W':
df58fc94 12125 INSERT_OPERAND (mips_opts.micromips, FT, *ip, lastregno);
252b5132
RH
12126 continue;
12127 }
12128 break;
12129
12130 case 'I':
12131 my_getExpression (&imm_expr, s);
12132 if (imm_expr.X_op != O_big
12133 && imm_expr.X_op != O_constant)
12134 insn_error = _("absolute expression required");
9ee2a2d4
MR
12135 if (HAVE_32BIT_GPRS)
12136 normalize_constant_expr (&imm_expr);
252b5132
RH
12137 s = expr_end;
12138 continue;
12139
12140 case 'A':
12141 my_getExpression (&offset_expr, s);
2051e8c4 12142 normalize_address_expr (&offset_expr);
f6688943 12143 *imm_reloc = BFD_RELOC_32;
252b5132
RH
12144 s = expr_end;
12145 continue;
12146
12147 case 'F':
12148 case 'L':
12149 case 'f':
12150 case 'l':
12151 {
12152 int f64;
ca4e0257 12153 int using_gprs;
252b5132
RH
12154 char *save_in;
12155 char *err;
12156 unsigned char temp[8];
12157 int len;
12158 unsigned int length;
12159 segT seg;
12160 subsegT subseg;
12161 char *p;
12162
12163 /* These only appear as the last operand in an
12164 instruction, and every instruction that accepts
12165 them in any variant accepts them in all variants.
12166 This means we don't have to worry about backing out
12167 any changes if the instruction does not match.
12168
12169 The difference between them is the size of the
12170 floating point constant and where it goes. For 'F'
12171 and 'L' the constant is 64 bits; for 'f' and 'l' it
12172 is 32 bits. Where the constant is placed is based
12173 on how the MIPS assembler does things:
12174 F -- .rdata
12175 L -- .lit8
12176 f -- immediate value
12177 l -- .lit4
12178
12179 The .lit4 and .lit8 sections are only used if
12180 permitted by the -G argument.
12181
ca4e0257
RS
12182 The code below needs to know whether the target register
12183 is 32 or 64 bits wide. It relies on the fact 'f' and
12184 'F' are used with GPR-based instructions and 'l' and
12185 'L' are used with FPR-based instructions. */
252b5132
RH
12186
12187 f64 = *args == 'F' || *args == 'L';
ca4e0257 12188 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
12189
12190 save_in = input_line_pointer;
12191 input_line_pointer = s;
12192 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
12193 length = len;
12194 s = input_line_pointer;
12195 input_line_pointer = save_in;
12196 if (err != NULL && *err != '\0')
12197 {
12198 as_bad (_("Bad floating point constant: %s"), err);
12199 memset (temp, '\0', sizeof temp);
12200 length = f64 ? 8 : 4;
12201 }
12202
9c2799c2 12203 gas_assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
12204
12205 if (*args == 'f'
12206 || (*args == 'l'
3e722fb5 12207 && (g_switch_value < 4
252b5132
RH
12208 || (temp[0] == 0 && temp[1] == 0)
12209 || (temp[2] == 0 && temp[3] == 0))))
12210 {
12211 imm_expr.X_op = O_constant;
90ecf173 12212 if (!target_big_endian)
252b5132
RH
12213 imm_expr.X_add_number = bfd_getl32 (temp);
12214 else
12215 imm_expr.X_add_number = bfd_getb32 (temp);
12216 }
12217 else if (length > 4
90ecf173 12218 && !mips_disable_float_construction
ca4e0257
RS
12219 /* Constants can only be constructed in GPRs and
12220 copied to FPRs if the GPRs are at least as wide
12221 as the FPRs. Force the constant into memory if
12222 we are using 64-bit FPRs but the GPRs are only
12223 32 bits wide. */
12224 && (using_gprs
90ecf173 12225 || !(HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
12226 && ((temp[0] == 0 && temp[1] == 0)
12227 || (temp[2] == 0 && temp[3] == 0))
12228 && ((temp[4] == 0 && temp[5] == 0)
12229 || (temp[6] == 0 && temp[7] == 0)))
12230 {
ca4e0257 12231 /* The value is simple enough to load with a couple of
90ecf173
MR
12232 instructions. If using 32-bit registers, set
12233 imm_expr to the high order 32 bits and offset_expr to
12234 the low order 32 bits. Otherwise, set imm_expr to
12235 the entire 64 bit constant. */
ca4e0257 12236 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
12237 {
12238 imm_expr.X_op = O_constant;
12239 offset_expr.X_op = O_constant;
90ecf173 12240 if (!target_big_endian)
252b5132
RH
12241 {
12242 imm_expr.X_add_number = bfd_getl32 (temp + 4);
12243 offset_expr.X_add_number = bfd_getl32 (temp);
12244 }
12245 else
12246 {
12247 imm_expr.X_add_number = bfd_getb32 (temp);
12248 offset_expr.X_add_number = bfd_getb32 (temp + 4);
12249 }
12250 if (offset_expr.X_add_number == 0)
12251 offset_expr.X_op = O_absent;
12252 }
12253 else if (sizeof (imm_expr.X_add_number) > 4)
12254 {
12255 imm_expr.X_op = O_constant;
90ecf173 12256 if (!target_big_endian)
252b5132
RH
12257 imm_expr.X_add_number = bfd_getl64 (temp);
12258 else
12259 imm_expr.X_add_number = bfd_getb64 (temp);
12260 }
12261 else
12262 {
12263 imm_expr.X_op = O_big;
12264 imm_expr.X_add_number = 4;
90ecf173 12265 if (!target_big_endian)
252b5132
RH
12266 {
12267 generic_bignum[0] = bfd_getl16 (temp);
12268 generic_bignum[1] = bfd_getl16 (temp + 2);
12269 generic_bignum[2] = bfd_getl16 (temp + 4);
12270 generic_bignum[3] = bfd_getl16 (temp + 6);
12271 }
12272 else
12273 {
12274 generic_bignum[0] = bfd_getb16 (temp + 6);
12275 generic_bignum[1] = bfd_getb16 (temp + 4);
12276 generic_bignum[2] = bfd_getb16 (temp + 2);
12277 generic_bignum[3] = bfd_getb16 (temp);
12278 }
12279 }
12280 }
12281 else
12282 {
12283 const char *newname;
12284 segT new_seg;
12285
12286 /* Switch to the right section. */
12287 seg = now_seg;
12288 subseg = now_subseg;
12289 switch (*args)
12290 {
12291 default: /* unused default case avoids warnings. */
12292 case 'L':
12293 newname = RDATA_SECTION_NAME;
3e722fb5 12294 if (g_switch_value >= 8)
252b5132
RH
12295 newname = ".lit8";
12296 break;
12297 case 'F':
3e722fb5 12298 newname = RDATA_SECTION_NAME;
252b5132
RH
12299 break;
12300 case 'l':
9c2799c2 12301 gas_assert (g_switch_value >= 4);
252b5132
RH
12302 newname = ".lit4";
12303 break;
12304 }
12305 new_seg = subseg_new (newname, (subsegT) 0);
f43abd2b 12306 if (IS_ELF)
252b5132
RH
12307 bfd_set_section_flags (stdoutput, new_seg,
12308 (SEC_ALLOC
12309 | SEC_LOAD
12310 | SEC_READONLY
12311 | SEC_DATA));
12312 frag_align (*args == 'l' ? 2 : 3, 0, 0);
c41e87e3 12313 if (IS_ELF && strncmp (TARGET_OS, "elf", 3) != 0)
252b5132
RH
12314 record_alignment (new_seg, 4);
12315 else
12316 record_alignment (new_seg, *args == 'l' ? 2 : 3);
12317 if (seg == now_seg)
12318 as_bad (_("Can't use floating point insn in this section"));
12319
df58fc94
RS
12320 /* Set the argument to the current address in the
12321 section. */
12322 offset_expr.X_op = O_symbol;
12323 offset_expr.X_add_symbol = symbol_temp_new_now ();
12324 offset_expr.X_add_number = 0;
12325
12326 /* Put the floating point number into the section. */
12327 p = frag_more ((int) length);
12328 memcpy (p, temp, length);
12329
12330 /* Switch back to the original section. */
12331 subseg_set (seg, subseg);
12332 }
12333 }
12334 continue;
12335
12336 case 'i': /* 16-bit unsigned immediate. */
12337 case 'j': /* 16-bit signed immediate. */
12338 *imm_reloc = BFD_RELOC_LO16;
12339 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
12340 {
12341 int more;
12342 offsetT minval, maxval;
12343
12344 more = (insn + 1 < past
12345 && strcmp (insn->name, insn[1].name) == 0);
12346
12347 /* If the expression was written as an unsigned number,
12348 only treat it as signed if there are no more
12349 alternatives. */
12350 if (more
12351 && *args == 'j'
12352 && sizeof (imm_expr.X_add_number) <= 4
12353 && imm_expr.X_op == O_constant
12354 && imm_expr.X_add_number < 0
12355 && imm_expr.X_unsigned
12356 && HAVE_64BIT_GPRS)
12357 break;
12358
12359 /* For compatibility with older assemblers, we accept
12360 0x8000-0xffff as signed 16-bit numbers when only
12361 signed numbers are allowed. */
12362 if (*args == 'i')
12363 minval = 0, maxval = 0xffff;
12364 else if (more)
12365 minval = -0x8000, maxval = 0x7fff;
12366 else
12367 minval = -0x8000, maxval = 0xffff;
12368
12369 if (imm_expr.X_op != O_constant
12370 || imm_expr.X_add_number < minval
12371 || imm_expr.X_add_number > maxval)
12372 {
12373 if (more)
12374 break;
12375 if (imm_expr.X_op == O_constant
12376 || imm_expr.X_op == O_big)
12377 as_bad (_("Expression out of range"));
12378 }
12379 }
12380 s = expr_end;
12381 continue;
12382
12383 case 'o': /* 16-bit offset. */
12384 offset_reloc[0] = BFD_RELOC_LO16;
12385 offset_reloc[1] = BFD_RELOC_UNUSED;
12386 offset_reloc[2] = BFD_RELOC_UNUSED;
12387
12388 /* Check whether there is only a single bracketed expression
12389 left. If so, it must be the base register and the
12390 constant must be zero. */
12391 if (*s == '(' && strchr (s + 1, '(') == 0)
12392 {
12393 offset_expr.X_op = O_constant;
12394 offset_expr.X_add_number = 0;
12395 continue;
12396 }
12397
12398 /* If this value won't fit into a 16 bit offset, then go
12399 find a macro that will generate the 32 bit offset
12400 code pattern. */
12401 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
12402 && (offset_expr.X_op != O_constant
12403 || offset_expr.X_add_number >= 0x8000
12404 || offset_expr.X_add_number < -0x8000))
12405 break;
12406
12407 s = expr_end;
12408 continue;
12409
12410 case 'p': /* PC-relative offset. */
12411 *offset_reloc = BFD_RELOC_16_PCREL_S2;
12412 my_getExpression (&offset_expr, s);
12413 s = expr_end;
12414 continue;
12415
12416 case 'u': /* Upper 16 bits. */
12417 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
12418 && imm_expr.X_op == O_constant
12419 && (imm_expr.X_add_number < 0
12420 || imm_expr.X_add_number >= 0x10000))
12421 as_bad (_("lui expression (%lu) not in range 0..65535"),
12422 (unsigned long) imm_expr.X_add_number);
12423 s = expr_end;
12424 continue;
12425
12426 case 'a': /* 26-bit address. */
12427 *offset_reloc = BFD_RELOC_MIPS_JMP;
12428 my_getExpression (&offset_expr, s);
12429 s = expr_end;
12430 continue;
12431
12432 case 'N': /* 3-bit branch condition code. */
12433 case 'M': /* 3-bit compare condition code. */
12434 rtype = RTYPE_CCC;
12435 if (ip->insn_mo->pinfo & (FP_D | FP_S))
12436 rtype |= RTYPE_FCC;
12437 if (!reg_lookup (&s, rtype, &regno))
12438 break;
12439 if ((strcmp (str + strlen (str) - 3, ".ps") == 0
12440 || strcmp (str + strlen (str) - 5, "any2f") == 0
12441 || strcmp (str + strlen (str) - 5, "any2t") == 0)
12442 && (regno & 1) != 0)
12443 as_warn (_("Condition code register should be even for %s, "
12444 "was %d"),
12445 str, regno);
12446 if ((strcmp (str + strlen (str) - 5, "any4f") == 0
12447 || strcmp (str + strlen (str) - 5, "any4t") == 0)
12448 && (regno & 3) != 0)
12449 as_warn (_("Condition code register should be 0 or 4 for %s, "
12450 "was %d"),
12451 str, regno);
12452 if (*args == 'N')
12453 INSERT_OPERAND (mips_opts.micromips, BCC, *ip, regno);
12454 else
12455 INSERT_OPERAND (mips_opts.micromips, CCC, *ip, regno);
12456 continue;
12457
12458 case 'H':
12459 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
12460 s += 2;
12461 if (ISDIGIT (*s))
12462 {
12463 c = 0;
12464 do
12465 {
12466 c *= 10;
12467 c += *s - '0';
12468 ++s;
12469 }
12470 while (ISDIGIT (*s));
12471 }
12472 else
12473 c = 8; /* Invalid sel value. */
12474
12475 if (c > 7)
12476 as_bad (_("Invalid coprocessor sub-selection value (0-7)"));
12477 INSERT_OPERAND (mips_opts.micromips, SEL, *ip, c);
12478 continue;
12479
12480 case 'e':
12481 gas_assert (!mips_opts.micromips);
12482 /* Must be at least one digit. */
12483 my_getExpression (&imm_expr, s);
12484 check_absolute_expr (ip, &imm_expr);
12485
12486 if ((unsigned long) imm_expr.X_add_number
12487 > (unsigned long) OP_MASK_VECBYTE)
12488 {
12489 as_bad (_("bad byte vector index (%ld)"),
12490 (long) imm_expr.X_add_number);
12491 imm_expr.X_add_number = 0;
12492 }
12493
12494 INSERT_OPERAND (0, VECBYTE, *ip, imm_expr.X_add_number);
12495 imm_expr.X_op = O_absent;
12496 s = expr_end;
12497 continue;
12498
12499 case '%':
12500 gas_assert (!mips_opts.micromips);
12501 my_getExpression (&imm_expr, s);
12502 check_absolute_expr (ip, &imm_expr);
12503
12504 if ((unsigned long) imm_expr.X_add_number
12505 > (unsigned long) OP_MASK_VECALIGN)
12506 {
12507 as_bad (_("bad byte vector index (%ld)"),
12508 (long) imm_expr.X_add_number);
12509 imm_expr.X_add_number = 0;
12510 }
12511
12512 INSERT_OPERAND (0, VECALIGN, *ip, imm_expr.X_add_number);
12513 imm_expr.X_op = O_absent;
12514 s = expr_end;
12515 continue;
12516
12517 case 'm': /* Opcode extension character. */
12518 gas_assert (mips_opts.micromips);
12519 c = *++args;
12520 switch (c)
12521 {
12522 case 'r':
12523 if (strncmp (s, "$pc", 3) == 0)
12524 {
12525 s += 3;
12526 continue;
12527 }
12528 break;
12529
12530 case 'a':
12531 case 'b':
12532 case 'c':
12533 case 'd':
12534 case 'e':
12535 case 'f':
12536 case 'g':
12537 case 'h':
12538 case 'i':
12539 case 'j':
12540 case 'l':
12541 case 'm':
12542 case 'n':
12543 case 'p':
12544 case 'q':
12545 case 's':
12546 case 't':
12547 case 'x':
12548 case 'y':
12549 case 'z':
12550 s_reset = s;
12551 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
12552 if (regno == AT && mips_opts.at)
12553 {
12554 if (mips_opts.at == ATREG)
12555 as_warn (_("Used $at without \".set noat\""));
12556 else
12557 as_warn (_("Used $%u with \".set at=$%u\""),
12558 regno, mips_opts.at);
12559 }
12560 if (!ok)
12561 {
12562 if (c == 'c')
12563 {
12564 gas_assert (args[1] == ',');
12565 regno = lastregno;
12566 ++args;
12567 }
12568 else if (c == 't')
12569 {
12570 gas_assert (args[1] == ',');
12571 ++args;
12572 continue; /* Nothing to do. */
12573 }
12574 else
12575 break;
12576 }
12577
12578 if (c == 'j' && !strncmp (ip->insn_mo->name, "jalr", 4))
12579 {
12580 if (regno == lastregno)
12581 {
12582 insn_error
12583 = _("Source and destination must be different");
12584 continue;
12585 }
12586 if (regno == 31 && lastregno == 0xffffffff)
12587 {
12588 insn_error
12589 = _("A destination register must be supplied");
12590 continue;
12591 }
12592 }
12593
12594 if (*s == ' ')
12595 ++s;
12596 if (args[1] != *s)
12597 {
12598 if (c == 'e')
12599 {
12600 gas_assert (args[1] == ',');
12601 regno = lastregno;
12602 s = s_reset;
12603 ++args;
12604 }
12605 else if (c == 't')
12606 {
12607 gas_assert (args[1] == ',');
12608 s = s_reset;
12609 ++args;
12610 continue; /* Nothing to do. */
12611 }
12612 }
12613
12614 /* Make sure regno is the same as lastregno. */
12615 if (c == 't' && regno != lastregno)
12616 break;
12617
12618 /* Make sure regno is the same as destregno. */
12619 if (c == 'x' && regno != destregno)
12620 break;
12621
12622 /* We need to save regno, before regno maps to the
12623 microMIPS register encoding. */
12624 lastregno = regno;
12625
12626 if (c == 'f')
12627 destregno = regno;
12628
12629 switch (c)
12630 {
12631 case 'a':
12632 if (regno != GP)
12633 regno = ILLEGAL_REG;
12634 break;
12635
12636 case 'b':
12637 regno = mips32_to_micromips_reg_b_map[regno];
12638 break;
12639
12640 case 'c':
12641 regno = mips32_to_micromips_reg_c_map[regno];
12642 break;
12643
12644 case 'd':
12645 regno = mips32_to_micromips_reg_d_map[regno];
12646 break;
12647
12648 case 'e':
12649 regno = mips32_to_micromips_reg_e_map[regno];
12650 break;
12651
12652 case 'f':
12653 regno = mips32_to_micromips_reg_f_map[regno];
12654 break;
12655
12656 case 'g':
12657 regno = mips32_to_micromips_reg_g_map[regno];
12658 break;
12659
12660 case 'h':
12661 regno = mips32_to_micromips_reg_h_map[regno];
12662 break;
12663
12664 case 'i':
12665 switch (EXTRACT_OPERAND (1, MI, *ip))
12666 {
12667 case 4:
12668 if (regno == 21)
12669 regno = 3;
12670 else if (regno == 22)
12671 regno = 4;
12672 else if (regno == 5)
12673 regno = 5;
12674 else if (regno == 6)
12675 regno = 6;
12676 else if (regno == 7)
12677 regno = 7;
12678 else
12679 regno = ILLEGAL_REG;
12680 break;
12681
12682 case 5:
12683 if (regno == 6)
12684 regno = 0;
12685 else if (regno == 7)
12686 regno = 1;
12687 else
12688 regno = ILLEGAL_REG;
12689 break;
12690
12691 case 6:
12692 if (regno == 7)
12693 regno = 2;
12694 else
12695 regno = ILLEGAL_REG;
12696 break;
12697
12698 default:
12699 regno = ILLEGAL_REG;
12700 break;
12701 }
12702 break;
12703
12704 case 'l':
12705 regno = mips32_to_micromips_reg_l_map[regno];
12706 break;
12707
12708 case 'm':
12709 regno = mips32_to_micromips_reg_m_map[regno];
12710 break;
12711
12712 case 'n':
12713 regno = mips32_to_micromips_reg_n_map[regno];
12714 break;
12715
12716 case 'q':
12717 regno = mips32_to_micromips_reg_q_map[regno];
12718 break;
12719
12720 case 's':
12721 if (regno != SP)
12722 regno = ILLEGAL_REG;
12723 break;
12724
12725 case 'y':
12726 if (regno != 31)
12727 regno = ILLEGAL_REG;
12728 break;
12729
12730 case 'z':
12731 if (regno != ZERO)
12732 regno = ILLEGAL_REG;
12733 break;
12734
12735 case 'j': /* Do nothing. */
12736 case 'p':
12737 case 't':
12738 case 'x':
12739 break;
12740
12741 default:
12742 internalError ();
12743 }
12744
12745 if (regno == ILLEGAL_REG)
12746 break;
12747
12748 switch (c)
12749 {
12750 case 'b':
12751 INSERT_OPERAND (1, MB, *ip, regno);
12752 break;
12753
12754 case 'c':
12755 INSERT_OPERAND (1, MC, *ip, regno);
12756 break;
12757
12758 case 'd':
12759 INSERT_OPERAND (1, MD, *ip, regno);
12760 break;
12761
12762 case 'e':
12763 INSERT_OPERAND (1, ME, *ip, regno);
12764 break;
12765
12766 case 'f':
12767 INSERT_OPERAND (1, MF, *ip, regno);
12768 break;
12769
12770 case 'g':
12771 INSERT_OPERAND (1, MG, *ip, regno);
12772 break;
12773
12774 case 'h':
12775 INSERT_OPERAND (1, MH, *ip, regno);
12776 break;
12777
12778 case 'i':
12779 INSERT_OPERAND (1, MI, *ip, regno);
12780 break;
12781
12782 case 'j':
12783 INSERT_OPERAND (1, MJ, *ip, regno);
12784 break;
12785
12786 case 'l':
12787 INSERT_OPERAND (1, ML, *ip, regno);
12788 break;
12789
12790 case 'm':
12791 INSERT_OPERAND (1, MM, *ip, regno);
12792 break;
12793
12794 case 'n':
12795 INSERT_OPERAND (1, MN, *ip, regno);
12796 break;
12797
12798 case 'p':
12799 INSERT_OPERAND (1, MP, *ip, regno);
12800 break;
12801
12802 case 'q':
12803 INSERT_OPERAND (1, MQ, *ip, regno);
12804 break;
12805
12806 case 'a': /* Do nothing. */
12807 case 's': /* Do nothing. */
12808 case 't': /* Do nothing. */
12809 case 'x': /* Do nothing. */
12810 case 'y': /* Do nothing. */
12811 case 'z': /* Do nothing. */
12812 break;
12813
12814 default:
12815 internalError ();
12816 }
12817 continue;
12818
12819 case 'A':
12820 {
12821 bfd_reloc_code_real_type r[3];
12822 expressionS ep;
12823 int imm;
12824
12825 /* Check whether there is only a single bracketed
12826 expression left. If so, it must be the base register
12827 and the constant must be zero. */
12828 if (*s == '(' && strchr (s + 1, '(') == 0)
12829 {
12830 INSERT_OPERAND (1, IMMA, *ip, 0);
12831 continue;
12832 }
12833
12834 if (my_getSmallExpression (&ep, r, s) > 0
12835 || !expr_const_in_range (&ep, -64, 64, 2))
12836 break;
12837
12838 imm = ep.X_add_number >> 2;
12839 INSERT_OPERAND (1, IMMA, *ip, imm);
12840 }
12841 s = expr_end;
12842 continue;
12843
12844 case 'B':
12845 {
12846 bfd_reloc_code_real_type r[3];
12847 expressionS ep;
12848 int imm;
12849
12850 if (my_getSmallExpression (&ep, r, s) > 0
12851 || ep.X_op != O_constant)
12852 break;
12853
12854 for (imm = 0; imm < 8; imm++)
12855 if (micromips_imm_b_map[imm] == ep.X_add_number)
12856 break;
12857 if (imm >= 8)
12858 break;
12859
12860 INSERT_OPERAND (1, IMMB, *ip, imm);
12861 }
12862 s = expr_end;
12863 continue;
12864
12865 case 'C':
12866 {
12867 bfd_reloc_code_real_type r[3];
12868 expressionS ep;
12869 int imm;
12870
12871 if (my_getSmallExpression (&ep, r, s) > 0
12872 || ep.X_op != O_constant)
12873 break;
12874
12875 for (imm = 0; imm < 16; imm++)
12876 if (micromips_imm_c_map[imm] == ep.X_add_number)
12877 break;
12878 if (imm >= 16)
12879 break;
12880
12881 INSERT_OPERAND (1, IMMC, *ip, imm);
12882 }
12883 s = expr_end;
12884 continue;
12885
12886 case 'D': /* pc relative offset */
12887 case 'E': /* pc relative offset */
12888 my_getExpression (&offset_expr, s);
12889 if (offset_expr.X_op == O_register)
12890 break;
12891
40209cad
MR
12892 if (!forced_insn_length)
12893 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
12894 else if (c == 'D')
12895 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
12896 else
12897 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
df58fc94
RS
12898 s = expr_end;
12899 continue;
12900
12901 case 'F':
12902 {
12903 bfd_reloc_code_real_type r[3];
12904 expressionS ep;
12905 int imm;
12906
12907 if (my_getSmallExpression (&ep, r, s) > 0
12908 || !expr_const_in_range (&ep, 0, 16, 0))
12909 break;
12910
12911 imm = ep.X_add_number;
12912 INSERT_OPERAND (1, IMMF, *ip, imm);
12913 }
12914 s = expr_end;
12915 continue;
12916
12917 case 'G':
12918 {
12919 bfd_reloc_code_real_type r[3];
12920 expressionS ep;
12921 int imm;
12922
12923 /* Check whether there is only a single bracketed
12924 expression left. If so, it must be the base register
12925 and the constant must be zero. */
12926 if (*s == '(' && strchr (s + 1, '(') == 0)
12927 {
12928 INSERT_OPERAND (1, IMMG, *ip, 0);
12929 continue;
12930 }
12931
12932 if (my_getSmallExpression (&ep, r, s) > 0
12933 || !expr_const_in_range (&ep, -1, 15, 0))
12934 break;
12935
12936 imm = ep.X_add_number & 15;
12937 INSERT_OPERAND (1, IMMG, *ip, imm);
12938 }
12939 s = expr_end;
12940 continue;
12941
12942 case 'H':
12943 {
12944 bfd_reloc_code_real_type r[3];
12945 expressionS ep;
12946 int imm;
12947
12948 /* Check whether there is only a single bracketed
12949 expression left. If so, it must be the base register
12950 and the constant must be zero. */
12951 if (*s == '(' && strchr (s + 1, '(') == 0)
12952 {
12953 INSERT_OPERAND (1, IMMH, *ip, 0);
12954 continue;
12955 }
12956
12957 if (my_getSmallExpression (&ep, r, s) > 0
12958 || !expr_const_in_range (&ep, 0, 16, 1))
12959 break;
12960
12961 imm = ep.X_add_number >> 1;
12962 INSERT_OPERAND (1, IMMH, *ip, imm);
12963 }
12964 s = expr_end;
12965 continue;
12966
12967 case 'I':
12968 {
12969 bfd_reloc_code_real_type r[3];
12970 expressionS ep;
12971 int imm;
12972
12973 if (my_getSmallExpression (&ep, r, s) > 0
12974 || !expr_const_in_range (&ep, -1, 127, 0))
12975 break;
12976
12977 imm = ep.X_add_number & 127;
12978 INSERT_OPERAND (1, IMMI, *ip, imm);
12979 }
12980 s = expr_end;
12981 continue;
12982
12983 case 'J':
12984 {
12985 bfd_reloc_code_real_type r[3];
12986 expressionS ep;
12987 int imm;
12988
12989 /* Check whether there is only a single bracketed
12990 expression left. If so, it must be the base register
12991 and the constant must be zero. */
12992 if (*s == '(' && strchr (s + 1, '(') == 0)
12993 {
12994 INSERT_OPERAND (1, IMMJ, *ip, 0);
12995 continue;
12996 }
12997
12998 if (my_getSmallExpression (&ep, r, s) > 0
12999 || !expr_const_in_range (&ep, 0, 16, 2))
13000 break;
13001
13002 imm = ep.X_add_number >> 2;
13003 INSERT_OPERAND (1, IMMJ, *ip, imm);
13004 }
13005 s = expr_end;
13006 continue;
13007
13008 case 'L':
13009 {
13010 bfd_reloc_code_real_type r[3];
13011 expressionS ep;
13012 int imm;
13013
13014 /* Check whether there is only a single bracketed
13015 expression left. If so, it must be the base register
13016 and the constant must be zero. */
13017 if (*s == '(' && strchr (s + 1, '(') == 0)
13018 {
13019 INSERT_OPERAND (1, IMML, *ip, 0);
13020 continue;
13021 }
13022
13023 if (my_getSmallExpression (&ep, r, s) > 0
13024 || !expr_const_in_range (&ep, 0, 16, 0))
13025 break;
13026
13027 imm = ep.X_add_number;
13028 INSERT_OPERAND (1, IMML, *ip, imm);
13029 }
13030 s = expr_end;
13031 continue;
13032
13033 case 'M':
13034 {
13035 bfd_reloc_code_real_type r[3];
13036 expressionS ep;
13037 int imm;
13038
13039 if (my_getSmallExpression (&ep, r, s) > 0
13040 || !expr_const_in_range (&ep, 1, 9, 0))
13041 break;
13042
13043 imm = ep.X_add_number & 7;
13044 INSERT_OPERAND (1, IMMM, *ip, imm);
13045 }
13046 s = expr_end;
13047 continue;
13048
13049 case 'N': /* Register list for lwm and swm. */
13050 {
13051 /* A comma-separated list of registers and/or
13052 dash-separated contiguous ranges including
13053 both ra and a set of one or more registers
13054 starting at s0 up to s3 which have to be
13055 consecutive, e.g.:
13056
13057 s0, ra
13058 s0, s1, ra, s2, s3
13059 s0-s2, ra
13060
13061 and any permutations of these. */
13062 unsigned int reglist;
13063 int imm;
13064
13065 if (!reglist_lookup (&s, RTYPE_NUM | RTYPE_GP, &reglist))
13066 break;
13067
13068 if ((reglist & 0xfff1ffff) != 0x80010000)
13069 break;
13070
13071 reglist = (reglist >> 17) & 7;
13072 reglist += 1;
13073 if ((reglist & -reglist) != reglist)
13074 break;
252b5132 13075
df58fc94
RS
13076 imm = ffs (reglist) - 1;
13077 INSERT_OPERAND (1, IMMN, *ip, imm);
13078 }
13079 continue;
252b5132 13080
df58fc94
RS
13081 case 'O': /* sdbbp 4-bit code. */
13082 {
13083 bfd_reloc_code_real_type r[3];
13084 expressionS ep;
13085 int imm;
13086
13087 if (my_getSmallExpression (&ep, r, s) > 0
13088 || !expr_const_in_range (&ep, 0, 16, 0))
13089 break;
13090
13091 imm = ep.X_add_number;
13092 INSERT_OPERAND (1, IMMO, *ip, imm);
252b5132 13093 }
df58fc94
RS
13094 s = expr_end;
13095 continue;
252b5132 13096
df58fc94
RS
13097 case 'P':
13098 {
13099 bfd_reloc_code_real_type r[3];
13100 expressionS ep;
13101 int imm;
5e0116d5 13102
df58fc94
RS
13103 if (my_getSmallExpression (&ep, r, s) > 0
13104 || !expr_const_in_range (&ep, 0, 32, 2))
13105 break;
5e0116d5 13106
df58fc94
RS
13107 imm = ep.X_add_number >> 2;
13108 INSERT_OPERAND (1, IMMP, *ip, imm);
13109 }
13110 s = expr_end;
13111 continue;
5e0116d5 13112
df58fc94
RS
13113 case 'Q':
13114 {
13115 bfd_reloc_code_real_type r[3];
13116 expressionS ep;
13117 int imm;
5e0116d5 13118
df58fc94
RS
13119 if (my_getSmallExpression (&ep, r, s) > 0
13120 || !expr_const_in_range (&ep, -0x400000, 0x400000, 2))
13121 break;
252b5132 13122
df58fc94
RS
13123 imm = ep.X_add_number >> 2;
13124 INSERT_OPERAND (1, IMMQ, *ip, imm);
13125 }
13126 s = expr_end;
13127 continue;
4614d845 13128
df58fc94
RS
13129 case 'U':
13130 {
13131 bfd_reloc_code_real_type r[3];
13132 expressionS ep;
13133 int imm;
13134
13135 /* Check whether there is only a single bracketed
13136 expression left. If so, it must be the base register
13137 and the constant must be zero. */
13138 if (*s == '(' && strchr (s + 1, '(') == 0)
13139 {
13140 INSERT_OPERAND (1, IMMU, *ip, 0);
13141 continue;
13142 }
13143
13144 if (my_getSmallExpression (&ep, r, s) > 0
13145 || !expr_const_in_range (&ep, 0, 32, 2))
13146 break;
13147
13148 imm = ep.X_add_number >> 2;
13149 INSERT_OPERAND (1, IMMU, *ip, imm);
13150 }
13151 s = expr_end;
5e0116d5 13152 continue;
252b5132 13153
df58fc94
RS
13154 case 'W':
13155 {
13156 bfd_reloc_code_real_type r[3];
13157 expressionS ep;
13158 int imm;
252b5132 13159
df58fc94
RS
13160 if (my_getSmallExpression (&ep, r, s) > 0
13161 || !expr_const_in_range (&ep, 0, 64, 2))
13162 break;
252b5132 13163
df58fc94
RS
13164 imm = ep.X_add_number >> 2;
13165 INSERT_OPERAND (1, IMMW, *ip, imm);
13166 }
13167 s = expr_end;
13168 continue;
252b5132 13169
df58fc94
RS
13170 case 'X':
13171 {
13172 bfd_reloc_code_real_type r[3];
13173 expressionS ep;
13174 int imm;
252b5132 13175
df58fc94
RS
13176 if (my_getSmallExpression (&ep, r, s) > 0
13177 || !expr_const_in_range (&ep, -8, 8, 0))
13178 break;
252b5132 13179
df58fc94
RS
13180 imm = ep.X_add_number;
13181 INSERT_OPERAND (1, IMMX, *ip, imm);
13182 }
13183 s = expr_end;
13184 continue;
252b5132 13185
df58fc94
RS
13186 case 'Y':
13187 {
13188 bfd_reloc_code_real_type r[3];
13189 expressionS ep;
13190 int imm;
156c2f8b 13191
df58fc94
RS
13192 if (my_getSmallExpression (&ep, r, s) > 0
13193 || expr_const_in_range (&ep, -2, 2, 2)
13194 || !expr_const_in_range (&ep, -258, 258, 2))
13195 break;
156c2f8b 13196
df58fc94
RS
13197 imm = ep.X_add_number >> 2;
13198 imm = ((imm >> 1) & ~0xff) | (imm & 0xff);
13199 INSERT_OPERAND (1, IMMY, *ip, imm);
13200 }
13201 s = expr_end;
13202 continue;
60b63b72 13203
df58fc94
RS
13204 case 'Z':
13205 {
13206 bfd_reloc_code_real_type r[3];
13207 expressionS ep;
13208
13209 if (my_getSmallExpression (&ep, r, s) > 0
13210 || !expr_const_in_range (&ep, 0, 1, 0))
13211 break;
13212 }
13213 s = expr_end;
13214 continue;
13215
13216 default:
13217 as_bad (_("Internal error: bad microMIPS opcode "
13218 "(unknown extension operand type `m%c'): %s %s"),
13219 *args, insn->name, insn->args);
13220 /* Further processing is fruitless. */
13221 return;
60b63b72 13222 }
df58fc94 13223 break;
60b63b72 13224
df58fc94
RS
13225 case 'n': /* Register list for 32-bit lwm and swm. */
13226 gas_assert (mips_opts.micromips);
13227 {
13228 /* A comma-separated list of registers and/or
13229 dash-separated contiguous ranges including
13230 at least one of ra and a set of one or more
13231 registers starting at s0 up to s7 and then
13232 s8 which have to be consecutive, e.g.:
13233
13234 ra
13235 s0
13236 ra, s0, s1, s2
13237 s0-s8
13238 s0-s5, ra
13239
13240 and any permutations of these. */
13241 unsigned int reglist;
13242 int imm;
13243 int ra;
13244
13245 if (!reglist_lookup (&s, RTYPE_NUM | RTYPE_GP, &reglist))
13246 break;
13247
13248 if ((reglist & 0x3f00ffff) != 0)
13249 break;
13250
13251 ra = (reglist >> 27) & 0x10;
13252 reglist = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
13253 reglist += 1;
13254 if ((reglist & -reglist) != reglist)
13255 break;
13256
13257 imm = (ffs (reglist) - 1) | ra;
13258 INSERT_OPERAND (1, RT, *ip, imm);
13259 imm_expr.X_op = O_absent;
13260 }
60b63b72
RS
13261 continue;
13262
df58fc94
RS
13263 case '|': /* 4-bit trap code. */
13264 gas_assert (mips_opts.micromips);
60b63b72
RS
13265 my_getExpression (&imm_expr, s);
13266 check_absolute_expr (ip, &imm_expr);
60b63b72 13267 if ((unsigned long) imm_expr.X_add_number
df58fc94
RS
13268 > MICROMIPSOP_MASK_TRAP)
13269 as_bad (_("Trap code (%lu) for %s not in 0..15 range"),
13270 (unsigned long) imm_expr.X_add_number,
13271 ip->insn_mo->name);
13272 INSERT_OPERAND (1, TRAP, *ip, imm_expr.X_add_number);
60b63b72
RS
13273 imm_expr.X_op = O_absent;
13274 s = expr_end;
13275 continue;
13276
252b5132 13277 default:
f71d0d44 13278 as_bad (_("Bad char = '%c'\n"), *args);
252b5132
RH
13279 internalError ();
13280 }
13281 break;
13282 }
13283 /* Args don't match. */
df58fc94
RS
13284 s = argsStart;
13285 insn_error = _("Illegal operands");
13286 if (insn + 1 < past && !strcmp (insn->name, insn[1].name))
252b5132
RH
13287 {
13288 ++insn;
252b5132
RH
13289 continue;
13290 }
df58fc94
RS
13291 else if (wrong_delay_slot_insns && need_delay_slot_ok)
13292 {
13293 gas_assert (firstinsn);
13294 need_delay_slot_ok = FALSE;
13295 past = insn + 1;
13296 insn = firstinsn;
13297 continue;
13298 }
252b5132
RH
13299 return;
13300 }
13301}
13302
0499d65b
TS
13303#define SKIP_SPACE_TABS(S) { while (*(S) == ' ' || *(S) == '\t') ++(S); }
13304
252b5132
RH
13305/* This routine assembles an instruction into its binary format when
13306 assembling for the mips16. As a side effect, it sets one of the
df58fc94
RS
13307 global variables imm_reloc or offset_reloc to the type of relocation
13308 to do if one of the operands is an address expression. It also sets
13309 forced_insn_length to the resulting instruction size in bytes if the
13310 user explicitly requested a small or extended instruction. */
252b5132
RH
13311
13312static void
17a2f251 13313mips16_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
13314{
13315 char *s;
13316 const char *args;
13317 struct mips_opcode *insn;
13318 char *argsstart;
13319 unsigned int regno;
13320 unsigned int lastregno = 0;
13321 char *s_reset;
d6f16593 13322 size_t i;
252b5132
RH
13323
13324 insn_error = NULL;
13325
df58fc94 13326 forced_insn_length = 0;
252b5132 13327
3882b010 13328 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
13329 ;
13330 switch (*s)
13331 {
13332 case '\0':
13333 break;
13334
13335 case ' ':
13336 *s++ = '\0';
13337 break;
13338
13339 case '.':
13340 if (s[1] == 't' && s[2] == ' ')
13341 {
13342 *s = '\0';
df58fc94 13343 forced_insn_length = 2;
252b5132
RH
13344 s += 3;
13345 break;
13346 }
13347 else if (s[1] == 'e' && s[2] == ' ')
13348 {
13349 *s = '\0';
df58fc94 13350 forced_insn_length = 4;
252b5132
RH
13351 s += 3;
13352 break;
13353 }
13354 /* Fall through. */
13355 default:
13356 insn_error = _("unknown opcode");
13357 return;
13358 }
13359
df58fc94
RS
13360 if (mips_opts.noautoextend && !forced_insn_length)
13361 forced_insn_length = 2;
252b5132
RH
13362
13363 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
13364 {
13365 insn_error = _("unrecognized opcode");
13366 return;
13367 }
13368
13369 argsstart = s;
13370 for (;;)
13371 {
9b3f89ee
TS
13372 bfd_boolean ok;
13373
9c2799c2 13374 gas_assert (strcmp (insn->name, str) == 0);
252b5132 13375
037b32b9 13376 ok = is_opcode_valid_16 (insn);
9b3f89ee
TS
13377 if (! ok)
13378 {
13379 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes]
13380 && strcmp (insn->name, insn[1].name) == 0)
13381 {
13382 ++insn;
13383 continue;
13384 }
13385 else
13386 {
13387 if (!insn_error)
13388 {
13389 static char buf[100];
13390 sprintf (buf,
7bd942df 13391 _("Opcode not supported on this processor: %s (%s)"),
9b3f89ee
TS
13392 mips_cpu_info_from_arch (mips_opts.arch)->name,
13393 mips_cpu_info_from_isa (mips_opts.isa)->name);
13394 insn_error = buf;
13395 }
13396 return;
13397 }
13398 }
13399
1e915849 13400 create_insn (ip, insn);
252b5132 13401 imm_expr.X_op = O_absent;
f6688943
TS
13402 imm_reloc[0] = BFD_RELOC_UNUSED;
13403 imm_reloc[1] = BFD_RELOC_UNUSED;
13404 imm_reloc[2] = BFD_RELOC_UNUSED;
5f74bc13 13405 imm2_expr.X_op = O_absent;
252b5132 13406 offset_expr.X_op = O_absent;
f6688943
TS
13407 offset_reloc[0] = BFD_RELOC_UNUSED;
13408 offset_reloc[1] = BFD_RELOC_UNUSED;
13409 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
13410 for (args = insn->args; 1; ++args)
13411 {
13412 int c;
13413
13414 if (*s == ' ')
13415 ++s;
13416
13417 /* In this switch statement we call break if we did not find
13418 a match, continue if we did find a match, or return if we
13419 are done. */
13420
13421 c = *args;
13422 switch (c)
13423 {
13424 case '\0':
13425 if (*s == '\0')
13426 {
b886a2ab
RS
13427 offsetT value;
13428
252b5132
RH
13429 /* Stuff the immediate value in now, if we can. */
13430 if (imm_expr.X_op == O_constant
f6688943 13431 && *imm_reloc > BFD_RELOC_UNUSED
b886a2ab
RS
13432 && insn->pinfo != INSN_MACRO
13433 && calculate_reloc (*offset_reloc,
13434 imm_expr.X_add_number, &value))
252b5132 13435 {
c4e7957c 13436 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
b886a2ab 13437 *offset_reloc, value, forced_insn_length,
43c0598f 13438 &ip->insn_opcode);
252b5132 13439 imm_expr.X_op = O_absent;
f6688943 13440 *imm_reloc = BFD_RELOC_UNUSED;
43c0598f 13441 *offset_reloc = BFD_RELOC_UNUSED;
252b5132
RH
13442 }
13443
13444 return;
13445 }
13446 break;
13447
13448 case ',':
13449 if (*s++ == c)
13450 continue;
13451 s--;
13452 switch (*++args)
13453 {
13454 case 'v':
bf12938e 13455 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
252b5132
RH
13456 continue;
13457 case 'w':
bf12938e 13458 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
252b5132
RH
13459 continue;
13460 }
13461 break;
13462
13463 case '(':
13464 case ')':
13465 if (*s++ == c)
13466 continue;
13467 break;
13468
13469 case 'v':
13470 case 'w':
13471 if (s[0] != '$')
13472 {
13473 if (c == 'v')
bf12938e 13474 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
252b5132 13475 else
bf12938e 13476 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
252b5132
RH
13477 ++args;
13478 continue;
13479 }
13480 /* Fall through. */
13481 case 'x':
13482 case 'y':
13483 case 'z':
13484 case 'Z':
13485 case '0':
13486 case 'S':
13487 case 'R':
13488 case 'X':
13489 case 'Y':
707bfff6
TS
13490 s_reset = s;
13491 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
252b5132 13492 {
707bfff6 13493 if (c == 'v' || c == 'w')
85b51719 13494 {
707bfff6 13495 if (c == 'v')
a9e24354 13496 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
707bfff6 13497 else
a9e24354 13498 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
707bfff6
TS
13499 ++args;
13500 continue;
85b51719 13501 }
707bfff6 13502 break;
252b5132
RH
13503 }
13504
13505 if (*s == ' ')
13506 ++s;
13507 if (args[1] != *s)
13508 {
13509 if (c == 'v' || c == 'w')
13510 {
13511 regno = mips16_to_32_reg_map[lastregno];
13512 s = s_reset;
f9419b05 13513 ++args;
252b5132
RH
13514 }
13515 }
13516
13517 switch (c)
13518 {
13519 case 'x':
13520 case 'y':
13521 case 'z':
13522 case 'v':
13523 case 'w':
13524 case 'Z':
13525 regno = mips32_to_16_reg_map[regno];
13526 break;
13527
13528 case '0':
13529 if (regno != 0)
13530 regno = ILLEGAL_REG;
13531 break;
13532
13533 case 'S':
13534 if (regno != SP)
13535 regno = ILLEGAL_REG;
13536 break;
13537
13538 case 'R':
13539 if (regno != RA)
13540 regno = ILLEGAL_REG;
13541 break;
13542
13543 case 'X':
13544 case 'Y':
741fe287
MR
13545 if (regno == AT && mips_opts.at)
13546 {
13547 if (mips_opts.at == ATREG)
13548 as_warn (_("used $at without \".set noat\""));
13549 else
13550 as_warn (_("used $%u with \".set at=$%u\""),
13551 regno, mips_opts.at);
13552 }
252b5132
RH
13553 break;
13554
13555 default:
13556 internalError ();
13557 }
13558
13559 if (regno == ILLEGAL_REG)
13560 break;
13561
13562 switch (c)
13563 {
13564 case 'x':
13565 case 'v':
bf12938e 13566 MIPS16_INSERT_OPERAND (RX, *ip, regno);
252b5132
RH
13567 break;
13568 case 'y':
13569 case 'w':
bf12938e 13570 MIPS16_INSERT_OPERAND (RY, *ip, regno);
252b5132
RH
13571 break;
13572 case 'z':
bf12938e 13573 MIPS16_INSERT_OPERAND (RZ, *ip, regno);
252b5132
RH
13574 break;
13575 case 'Z':
bf12938e 13576 MIPS16_INSERT_OPERAND (MOVE32Z, *ip, regno);
252b5132
RH
13577 case '0':
13578 case 'S':
13579 case 'R':
13580 break;
13581 case 'X':
bf12938e 13582 MIPS16_INSERT_OPERAND (REGR32, *ip, regno);
252b5132
RH
13583 break;
13584 case 'Y':
13585 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
bf12938e 13586 MIPS16_INSERT_OPERAND (REG32R, *ip, regno);
252b5132
RH
13587 break;
13588 default:
13589 internalError ();
13590 }
13591
13592 lastregno = regno;
13593 continue;
13594
13595 case 'P':
13596 if (strncmp (s, "$pc", 3) == 0)
13597 {
13598 s += 3;
13599 continue;
13600 }
13601 break;
13602
252b5132
RH
13603 case '5':
13604 case 'H':
13605 case 'W':
13606 case 'D':
13607 case 'j':
252b5132
RH
13608 case 'V':
13609 case 'C':
13610 case 'U':
13611 case 'k':
13612 case 'K':
d6f16593
MR
13613 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
13614 if (i > 0)
252b5132 13615 {
d6f16593 13616 if (imm_expr.X_op != O_constant)
252b5132 13617 {
df58fc94 13618 forced_insn_length = 4;
5c04167a 13619 ip->insn_opcode |= MIPS16_EXTEND;
252b5132 13620 }
d6f16593
MR
13621 else
13622 {
13623 /* We need to relax this instruction. */
13624 *offset_reloc = *imm_reloc;
13625 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
13626 }
13627 s = expr_end;
13628 continue;
252b5132 13629 }
d6f16593
MR
13630 *imm_reloc = BFD_RELOC_UNUSED;
13631 /* Fall through. */
13632 case '<':
13633 case '>':
13634 case '[':
13635 case ']':
13636 case '4':
13637 case '8':
13638 my_getExpression (&imm_expr, s);
252b5132
RH
13639 if (imm_expr.X_op == O_register)
13640 {
13641 /* What we thought was an expression turned out to
13642 be a register. */
13643
13644 if (s[0] == '(' && args[1] == '(')
13645 {
13646 /* It looks like the expression was omitted
13647 before a register indirection, which means
13648 that the expression is implicitly zero. We
13649 still set up imm_expr, so that we handle
13650 explicit extensions correctly. */
13651 imm_expr.X_op = O_constant;
13652 imm_expr.X_add_number = 0;
f6688943 13653 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
13654 continue;
13655 }
13656
13657 break;
13658 }
13659
13660 /* We need to relax this instruction. */
f6688943 13661 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
13662 s = expr_end;
13663 continue;
13664
13665 case 'p':
13666 case 'q':
13667 case 'A':
13668 case 'B':
13669 case 'E':
13670 /* We use offset_reloc rather than imm_reloc for the PC
13671 relative operands. This lets macros with both
13672 immediate and address operands work correctly. */
13673 my_getExpression (&offset_expr, s);
13674
13675 if (offset_expr.X_op == O_register)
13676 break;
13677
13678 /* We need to relax this instruction. */
f6688943 13679 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
13680 s = expr_end;
13681 continue;
13682
13683 case '6': /* break code */
13684 my_getExpression (&imm_expr, s);
13685 check_absolute_expr (ip, &imm_expr);
13686 if ((unsigned long) imm_expr.X_add_number > 63)
bf12938e
RS
13687 as_warn (_("Invalid value for `%s' (%lu)"),
13688 ip->insn_mo->name,
13689 (unsigned long) imm_expr.X_add_number);
13690 MIPS16_INSERT_OPERAND (IMM6, *ip, imm_expr.X_add_number);
252b5132
RH
13691 imm_expr.X_op = O_absent;
13692 s = expr_end;
13693 continue;
13694
13695 case 'a': /* 26 bit address */
13696 my_getExpression (&offset_expr, s);
13697 s = expr_end;
f6688943 13698 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132
RH
13699 ip->insn_opcode <<= 16;
13700 continue;
13701
13702 case 'l': /* register list for entry macro */
13703 case 'L': /* register list for exit macro */
13704 {
13705 int mask;
13706
13707 if (c == 'l')
13708 mask = 0;
13709 else
13710 mask = 7 << 3;
13711 while (*s != '\0')
13712 {
707bfff6 13713 unsigned int freg, reg1, reg2;
252b5132
RH
13714
13715 while (*s == ' ' || *s == ',')
13716 ++s;
707bfff6 13717 if (reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
252b5132 13718 freg = 0;
707bfff6
TS
13719 else if (reg_lookup (&s, RTYPE_FPU, &reg1))
13720 freg = 1;
252b5132
RH
13721 else
13722 {
707bfff6
TS
13723 as_bad (_("can't parse register list"));
13724 break;
252b5132
RH
13725 }
13726 if (*s == ' ')
13727 ++s;
13728 if (*s != '-')
13729 reg2 = reg1;
13730 else
13731 {
13732 ++s;
707bfff6
TS
13733 if (!reg_lookup (&s, freg ? RTYPE_FPU
13734 : (RTYPE_GP | RTYPE_NUM), &reg2))
252b5132 13735 {
707bfff6
TS
13736 as_bad (_("invalid register list"));
13737 break;
252b5132
RH
13738 }
13739 }
13740 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
13741 {
13742 mask &= ~ (7 << 3);
13743 mask |= 5 << 3;
13744 }
13745 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
13746 {
13747 mask &= ~ (7 << 3);
13748 mask |= 6 << 3;
13749 }
13750 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
13751 mask |= (reg2 - 3) << 3;
13752 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
13753 mask |= (reg2 - 15) << 1;
f9419b05 13754 else if (reg1 == RA && reg2 == RA)
252b5132
RH
13755 mask |= 1;
13756 else
13757 {
13758 as_bad (_("invalid register list"));
13759 break;
13760 }
13761 }
13762 /* The mask is filled in in the opcode table for the
13763 benefit of the disassembler. We remove it before
13764 applying the actual mask. */
13765 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
13766 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
13767 }
13768 continue;
13769
0499d65b
TS
13770 case 'm': /* Register list for save insn. */
13771 case 'M': /* Register list for restore insn. */
13772 {
5c04167a 13773 int opcode = ip->insn_opcode;
0499d65b 13774 int framesz = 0, seen_framesz = 0;
91d6fa6a 13775 int nargs = 0, statics = 0, sregs = 0;
0499d65b
TS
13776
13777 while (*s != '\0')
13778 {
13779 unsigned int reg1, reg2;
13780
13781 SKIP_SPACE_TABS (s);
13782 while (*s == ',')
13783 ++s;
13784 SKIP_SPACE_TABS (s);
13785
13786 my_getExpression (&imm_expr, s);
13787 if (imm_expr.X_op == O_constant)
13788 {
13789 /* Handle the frame size. */
13790 if (seen_framesz)
13791 {
13792 as_bad (_("more than one frame size in list"));
13793 break;
13794 }
13795 seen_framesz = 1;
13796 framesz = imm_expr.X_add_number;
13797 imm_expr.X_op = O_absent;
13798 s = expr_end;
13799 continue;
13800 }
13801
707bfff6 13802 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
0499d65b
TS
13803 {
13804 as_bad (_("can't parse register list"));
13805 break;
13806 }
0499d65b 13807
707bfff6
TS
13808 while (*s == ' ')
13809 ++s;
13810
0499d65b
TS
13811 if (*s != '-')
13812 reg2 = reg1;
13813 else
13814 {
13815 ++s;
707bfff6
TS
13816 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg2)
13817 || reg2 < reg1)
0499d65b
TS
13818 {
13819 as_bad (_("can't parse register list"));
13820 break;
13821 }
0499d65b
TS
13822 }
13823
13824 while (reg1 <= reg2)
13825 {
13826 if (reg1 >= 4 && reg1 <= 7)
13827 {
3a93f742 13828 if (!seen_framesz)
0499d65b 13829 /* args $a0-$a3 */
91d6fa6a 13830 nargs |= 1 << (reg1 - 4);
0499d65b
TS
13831 else
13832 /* statics $a0-$a3 */
13833 statics |= 1 << (reg1 - 4);
13834 }
13835 else if ((reg1 >= 16 && reg1 <= 23) || reg1 == 30)
13836 {
13837 /* $s0-$s8 */
13838 sregs |= 1 << ((reg1 == 30) ? 8 : (reg1 - 16));
13839 }
13840 else if (reg1 == 31)
13841 {
13842 /* Add $ra to insn. */
13843 opcode |= 0x40;
13844 }
13845 else
13846 {
13847 as_bad (_("unexpected register in list"));
13848 break;
13849 }
13850 if (++reg1 == 24)
13851 reg1 = 30;
13852 }
13853 }
13854
13855 /* Encode args/statics combination. */
91d6fa6a 13856 if (nargs & statics)
0499d65b 13857 as_bad (_("arg/static registers overlap"));
91d6fa6a 13858 else if (nargs == 0xf)
0499d65b
TS
13859 /* All $a0-$a3 are args. */
13860 opcode |= MIPS16_ALL_ARGS << 16;
13861 else if (statics == 0xf)
13862 /* All $a0-$a3 are statics. */
13863 opcode |= MIPS16_ALL_STATICS << 16;
13864 else
13865 {
13866 int narg = 0, nstat = 0;
13867
13868 /* Count arg registers. */
91d6fa6a 13869 while (nargs & 0x1)
0499d65b 13870 {
91d6fa6a 13871 nargs >>= 1;
0499d65b
TS
13872 narg++;
13873 }
91d6fa6a 13874 if (nargs != 0)
0499d65b
TS
13875 as_bad (_("invalid arg register list"));
13876
13877 /* Count static registers. */
13878 while (statics & 0x8)
13879 {
13880 statics = (statics << 1) & 0xf;
13881 nstat++;
13882 }
13883 if (statics != 0)
13884 as_bad (_("invalid static register list"));
13885
13886 /* Encode args/statics. */
13887 opcode |= ((narg << 2) | nstat) << 16;
13888 }
13889
13890 /* Encode $s0/$s1. */
13891 if (sregs & (1 << 0)) /* $s0 */
13892 opcode |= 0x20;
13893 if (sregs & (1 << 1)) /* $s1 */
13894 opcode |= 0x10;
13895 sregs >>= 2;
13896
13897 if (sregs != 0)
13898 {
13899 /* Count regs $s2-$s8. */
13900 int nsreg = 0;
13901 while (sregs & 1)
13902 {
13903 sregs >>= 1;
13904 nsreg++;
13905 }
13906 if (sregs != 0)
13907 as_bad (_("invalid static register list"));
13908 /* Encode $s2-$s8. */
13909 opcode |= nsreg << 24;
13910 }
13911
13912 /* Encode frame size. */
13913 if (!seen_framesz)
13914 as_bad (_("missing frame size"));
13915 else if ((framesz & 7) != 0 || framesz < 0
13916 || framesz > 0xff * 8)
13917 as_bad (_("invalid frame size"));
13918 else if (framesz != 128 || (opcode >> 16) != 0)
13919 {
13920 framesz /= 8;
13921 opcode |= (((framesz & 0xf0) << 16)
13922 | (framesz & 0x0f));
13923 }
13924
13925 /* Finally build the instruction. */
13926 if ((opcode >> 16) != 0 || framesz == 0)
5c04167a
RS
13927 opcode |= MIPS16_EXTEND;
13928 ip->insn_opcode = opcode;
0499d65b
TS
13929 }
13930 continue;
13931
252b5132
RH
13932 case 'e': /* extend code */
13933 my_getExpression (&imm_expr, s);
13934 check_absolute_expr (ip, &imm_expr);
13935 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
13936 {
13937 as_warn (_("Invalid value for `%s' (%lu)"),
13938 ip->insn_mo->name,
13939 (unsigned long) imm_expr.X_add_number);
13940 imm_expr.X_add_number &= 0x7ff;
13941 }
13942 ip->insn_opcode |= imm_expr.X_add_number;
13943 imm_expr.X_op = O_absent;
13944 s = expr_end;
13945 continue;
13946
13947 default:
13948 internalError ();
13949 }
13950 break;
13951 }
13952
13953 /* Args don't match. */
13954 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
13955 strcmp (insn->name, insn[1].name) == 0)
13956 {
13957 ++insn;
13958 s = argsstart;
13959 continue;
13960 }
13961
13962 insn_error = _("illegal operands");
13963
13964 return;
13965 }
13966}
13967
13968/* This structure holds information we know about a mips16 immediate
13969 argument type. */
13970
e972090a
NC
13971struct mips16_immed_operand
13972{
252b5132
RH
13973 /* The type code used in the argument string in the opcode table. */
13974 int type;
13975 /* The number of bits in the short form of the opcode. */
13976 int nbits;
13977 /* The number of bits in the extended form of the opcode. */
13978 int extbits;
13979 /* The amount by which the short form is shifted when it is used;
13980 for example, the sw instruction has a shift count of 2. */
13981 int shift;
13982 /* The amount by which the short form is shifted when it is stored
13983 into the instruction code. */
13984 int op_shift;
13985 /* Non-zero if the short form is unsigned. */
13986 int unsp;
13987 /* Non-zero if the extended form is unsigned. */
13988 int extu;
13989 /* Non-zero if the value is PC relative. */
13990 int pcrel;
13991};
13992
13993/* The mips16 immediate operand types. */
13994
13995static const struct mips16_immed_operand mips16_immed_operands[] =
13996{
13997 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
13998 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
13999 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
14000 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
14001 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
14002 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
14003 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
14004 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
14005 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
14006 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
14007 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
14008 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
14009 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
14010 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
14011 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
14012 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
14013 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
14014 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
14015 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
14016 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
14017 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
14018};
14019
14020#define MIPS16_NUM_IMMED \
14021 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
14022
b886a2ab
RS
14023/* Marshal immediate value VAL for an extended MIPS16 instruction.
14024 NBITS is the number of significant bits in VAL. */
14025
14026static unsigned long
14027mips16_immed_extend (offsetT val, unsigned int nbits)
14028{
14029 int extval;
14030 if (nbits == 16)
14031 {
14032 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14033 val &= 0x1f;
14034 }
14035 else if (nbits == 15)
14036 {
14037 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14038 val &= 0xf;
14039 }
14040 else
14041 {
14042 extval = ((val & 0x1f) << 6) | (val & 0x20);
14043 val = 0;
14044 }
14045 return (extval << 16) | val;
14046}
14047
5c04167a
RS
14048/* Install immediate value VAL into MIPS16 instruction *INSN,
14049 extending it if necessary. The instruction in *INSN may
14050 already be extended.
14051
43c0598f
RS
14052 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14053 if none. In the former case, VAL is a 16-bit number with no
14054 defined signedness.
14055
14056 TYPE is the type of the immediate field. USER_INSN_LENGTH
14057 is the length that the user requested, or 0 if none. */
252b5132
RH
14058
14059static void
43c0598f
RS
14060mips16_immed (char *file, unsigned int line, int type,
14061 bfd_reloc_code_real_type reloc, offsetT val,
5c04167a 14062 unsigned int user_insn_length, unsigned long *insn)
252b5132 14063{
3994f87e 14064 const struct mips16_immed_operand *op;
252b5132 14065 int mintiny, maxtiny;
252b5132
RH
14066
14067 op = mips16_immed_operands;
14068 while (op->type != type)
14069 {
14070 ++op;
9c2799c2 14071 gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
252b5132
RH
14072 }
14073
14074 if (op->unsp)
14075 {
14076 if (type == '<' || type == '>' || type == '[' || type == ']')
14077 {
14078 mintiny = 1;
14079 maxtiny = 1 << op->nbits;
14080 }
14081 else
14082 {
14083 mintiny = 0;
14084 maxtiny = (1 << op->nbits) - 1;
14085 }
43c0598f
RS
14086 if (reloc != BFD_RELOC_UNUSED)
14087 val &= 0xffff;
252b5132
RH
14088 }
14089 else
14090 {
14091 mintiny = - (1 << (op->nbits - 1));
14092 maxtiny = (1 << (op->nbits - 1)) - 1;
43c0598f
RS
14093 if (reloc != BFD_RELOC_UNUSED)
14094 val = SEXT_16BIT (val);
252b5132
RH
14095 }
14096
14097 /* Branch offsets have an implicit 0 in the lowest bit. */
14098 if (type == 'p' || type == 'q')
14099 val /= 2;
14100
14101 if ((val & ((1 << op->shift) - 1)) != 0
14102 || val < (mintiny << op->shift)
14103 || val > (maxtiny << op->shift))
5c04167a
RS
14104 {
14105 /* We need an extended instruction. */
14106 if (user_insn_length == 2)
14107 as_bad_where (file, line, _("invalid unextended operand value"));
14108 else
14109 *insn |= MIPS16_EXTEND;
14110 }
14111 else if (user_insn_length == 4)
14112 {
14113 /* The operand doesn't force an unextended instruction to be extended.
14114 Warn if the user wanted an extended instruction anyway. */
14115 *insn |= MIPS16_EXTEND;
14116 as_warn_where (file, line,
14117 _("extended operand requested but not required"));
14118 }
252b5132 14119
5c04167a 14120 if (mips16_opcode_length (*insn) == 2)
252b5132
RH
14121 {
14122 int insnval;
14123
252b5132
RH
14124 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
14125 insnval <<= op->op_shift;
14126 *insn |= insnval;
14127 }
14128 else
14129 {
14130 long minext, maxext;
252b5132 14131
43c0598f 14132 if (reloc == BFD_RELOC_UNUSED)
252b5132 14133 {
43c0598f
RS
14134 if (op->extu)
14135 {
14136 minext = 0;
14137 maxext = (1 << op->extbits) - 1;
14138 }
14139 else
14140 {
14141 minext = - (1 << (op->extbits - 1));
14142 maxext = (1 << (op->extbits - 1)) - 1;
14143 }
14144 if (val < minext || val > maxext)
14145 as_bad_where (file, line,
14146 _("operand value out of range for instruction"));
252b5132 14147 }
252b5132 14148
b886a2ab 14149 *insn |= mips16_immed_extend (val, op->extbits);
252b5132
RH
14150 }
14151}
14152\f
d6f16593 14153struct percent_op_match
ad8d3bb3 14154{
5e0116d5
RS
14155 const char *str;
14156 bfd_reloc_code_real_type reloc;
d6f16593
MR
14157};
14158
14159static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 14160{
5e0116d5 14161 {"%lo", BFD_RELOC_LO16},
ad8d3bb3 14162#ifdef OBJ_ELF
5e0116d5
RS
14163 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14164 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14165 {"%call16", BFD_RELOC_MIPS_CALL16},
14166 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14167 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14168 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14169 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14170 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14171 {"%got", BFD_RELOC_MIPS_GOT16},
14172 {"%gp_rel", BFD_RELOC_GPREL16},
14173 {"%half", BFD_RELOC_16},
14174 {"%highest", BFD_RELOC_MIPS_HIGHEST},
14175 {"%higher", BFD_RELOC_MIPS_HIGHER},
14176 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
14177 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14178 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14179 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14180 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14181 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14182 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14183 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
ad8d3bb3 14184#endif
5e0116d5 14185 {"%hi", BFD_RELOC_HI16_S}
ad8d3bb3
TS
14186};
14187
d6f16593
MR
14188static const struct percent_op_match mips16_percent_op[] =
14189{
14190 {"%lo", BFD_RELOC_MIPS16_LO16},
14191 {"%gprel", BFD_RELOC_MIPS16_GPREL},
738e5348
RS
14192 {"%got", BFD_RELOC_MIPS16_GOT16},
14193 {"%call16", BFD_RELOC_MIPS16_CALL16},
d0f13682
CLT
14194 {"%hi", BFD_RELOC_MIPS16_HI16_S},
14195 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14196 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14197 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14198 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14199 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14200 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14201 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
d6f16593
MR
14202};
14203
252b5132 14204
5e0116d5
RS
14205/* Return true if *STR points to a relocation operator. When returning true,
14206 move *STR over the operator and store its relocation code in *RELOC.
14207 Leave both *STR and *RELOC alone when returning false. */
14208
14209static bfd_boolean
17a2f251 14210parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 14211{
d6f16593
MR
14212 const struct percent_op_match *percent_op;
14213 size_t limit, i;
14214
14215 if (mips_opts.mips16)
14216 {
14217 percent_op = mips16_percent_op;
14218 limit = ARRAY_SIZE (mips16_percent_op);
14219 }
14220 else
14221 {
14222 percent_op = mips_percent_op;
14223 limit = ARRAY_SIZE (mips_percent_op);
14224 }
76b3015f 14225
d6f16593 14226 for (i = 0; i < limit; i++)
5e0116d5 14227 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 14228 {
3f98094e
DJ
14229 int len = strlen (percent_op[i].str);
14230
14231 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14232 continue;
14233
5e0116d5
RS
14234 *str += strlen (percent_op[i].str);
14235 *reloc = percent_op[i].reloc;
394f9b3a 14236
5e0116d5
RS
14237 /* Check whether the output BFD supports this relocation.
14238 If not, issue an error and fall back on something safe. */
14239 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 14240 {
20203fb9 14241 as_bad (_("relocation %s isn't supported by the current ABI"),
5e0116d5 14242 percent_op[i].str);
01a3f561 14243 *reloc = BFD_RELOC_UNUSED;
394f9b3a 14244 }
5e0116d5 14245 return TRUE;
394f9b3a 14246 }
5e0116d5 14247 return FALSE;
394f9b3a 14248}
ad8d3bb3 14249
ad8d3bb3 14250
5e0116d5
RS
14251/* Parse string STR as a 16-bit relocatable operand. Store the
14252 expression in *EP and the relocations in the array starting
14253 at RELOC. Return the number of relocation operators used.
ad8d3bb3 14254
01a3f561 14255 On exit, EXPR_END points to the first character after the expression. */
ad8d3bb3 14256
5e0116d5 14257static size_t
17a2f251
TS
14258my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14259 char *str)
ad8d3bb3 14260{
5e0116d5
RS
14261 bfd_reloc_code_real_type reversed_reloc[3];
14262 size_t reloc_index, i;
09b8f35a
RS
14263 int crux_depth, str_depth;
14264 char *crux;
5e0116d5
RS
14265
14266 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
14267 in REVERSED_RELOC. End the loop with CRUX pointing to the start
14268 of the main expression and with CRUX_DEPTH containing the number
14269 of open brackets at that point. */
14270 reloc_index = -1;
14271 str_depth = 0;
14272 do
fb1b3232 14273 {
09b8f35a
RS
14274 reloc_index++;
14275 crux = str;
14276 crux_depth = str_depth;
14277
14278 /* Skip over whitespace and brackets, keeping count of the number
14279 of brackets. */
14280 while (*str == ' ' || *str == '\t' || *str == '(')
14281 if (*str++ == '(')
14282 str_depth++;
5e0116d5 14283 }
09b8f35a
RS
14284 while (*str == '%'
14285 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14286 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 14287
09b8f35a 14288 my_getExpression (ep, crux);
5e0116d5 14289 str = expr_end;
394f9b3a 14290
5e0116d5 14291 /* Match every open bracket. */
09b8f35a 14292 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 14293 if (*str++ == ')')
09b8f35a 14294 crux_depth--;
394f9b3a 14295
09b8f35a 14296 if (crux_depth > 0)
20203fb9 14297 as_bad (_("unclosed '('"));
394f9b3a 14298
5e0116d5 14299 expr_end = str;
252b5132 14300
01a3f561 14301 if (reloc_index != 0)
64bdfcaf
RS
14302 {
14303 prev_reloc_op_frag = frag_now;
14304 for (i = 0; i < reloc_index; i++)
14305 reloc[i] = reversed_reloc[reloc_index - 1 - i];
14306 }
fb1b3232 14307
5e0116d5 14308 return reloc_index;
252b5132
RH
14309}
14310
14311static void
17a2f251 14312my_getExpression (expressionS *ep, char *str)
252b5132
RH
14313{
14314 char *save_in;
14315
14316 save_in = input_line_pointer;
14317 input_line_pointer = str;
14318 expression (ep);
14319 expr_end = input_line_pointer;
14320 input_line_pointer = save_in;
252b5132
RH
14321}
14322
252b5132 14323char *
17a2f251 14324md_atof (int type, char *litP, int *sizeP)
252b5132 14325{
499ac353 14326 return ieee_md_atof (type, litP, sizeP, target_big_endian);
252b5132
RH
14327}
14328
14329void
17a2f251 14330md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
14331{
14332 if (target_big_endian)
14333 number_to_chars_bigendian (buf, val, n);
14334 else
14335 number_to_chars_littleendian (buf, val, n);
14336}
14337\f
ae948b86 14338#ifdef OBJ_ELF
e013f690
TS
14339static int support_64bit_objects(void)
14340{
14341 const char **list, **l;
aa3d8fdf 14342 int yes;
e013f690
TS
14343
14344 list = bfd_target_list ();
14345 for (l = list; *l != NULL; l++)
aeffff67
RS
14346 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14347 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
e013f690 14348 break;
aa3d8fdf 14349 yes = (*l != NULL);
e013f690 14350 free (list);
aa3d8fdf 14351 return yes;
e013f690 14352}
ae948b86 14353#endif /* OBJ_ELF */
e013f690 14354
78849248 14355const char *md_shortopts = "O::g::G:";
252b5132 14356
23fce1e3
NC
14357enum options
14358 {
14359 OPTION_MARCH = OPTION_MD_BASE,
14360 OPTION_MTUNE,
14361 OPTION_MIPS1,
14362 OPTION_MIPS2,
14363 OPTION_MIPS3,
14364 OPTION_MIPS4,
14365 OPTION_MIPS5,
14366 OPTION_MIPS32,
14367 OPTION_MIPS64,
14368 OPTION_MIPS32R2,
14369 OPTION_MIPS64R2,
14370 OPTION_MIPS16,
14371 OPTION_NO_MIPS16,
14372 OPTION_MIPS3D,
14373 OPTION_NO_MIPS3D,
14374 OPTION_MDMX,
14375 OPTION_NO_MDMX,
14376 OPTION_DSP,
14377 OPTION_NO_DSP,
14378 OPTION_MT,
14379 OPTION_NO_MT,
14380 OPTION_SMARTMIPS,
14381 OPTION_NO_SMARTMIPS,
14382 OPTION_DSPR2,
14383 OPTION_NO_DSPR2,
df58fc94
RS
14384 OPTION_MICROMIPS,
14385 OPTION_NO_MICROMIPS,
dec0624d
MR
14386 OPTION_MCU,
14387 OPTION_NO_MCU,
23fce1e3
NC
14388 OPTION_COMPAT_ARCH_BASE,
14389 OPTION_M4650,
14390 OPTION_NO_M4650,
14391 OPTION_M4010,
14392 OPTION_NO_M4010,
14393 OPTION_M4100,
14394 OPTION_NO_M4100,
14395 OPTION_M3900,
14396 OPTION_NO_M3900,
14397 OPTION_M7000_HILO_FIX,
6a32d874
CM
14398 OPTION_MNO_7000_HILO_FIX,
14399 OPTION_FIX_24K,
14400 OPTION_NO_FIX_24K,
c67a084a
NC
14401 OPTION_FIX_LOONGSON2F_JUMP,
14402 OPTION_NO_FIX_LOONGSON2F_JUMP,
14403 OPTION_FIX_LOONGSON2F_NOP,
14404 OPTION_NO_FIX_LOONGSON2F_NOP,
23fce1e3
NC
14405 OPTION_FIX_VR4120,
14406 OPTION_NO_FIX_VR4120,
14407 OPTION_FIX_VR4130,
14408 OPTION_NO_FIX_VR4130,
d954098f
DD
14409 OPTION_FIX_CN63XXP1,
14410 OPTION_NO_FIX_CN63XXP1,
23fce1e3
NC
14411 OPTION_TRAP,
14412 OPTION_BREAK,
14413 OPTION_EB,
14414 OPTION_EL,
14415 OPTION_FP32,
14416 OPTION_GP32,
14417 OPTION_CONSTRUCT_FLOATS,
14418 OPTION_NO_CONSTRUCT_FLOATS,
14419 OPTION_FP64,
14420 OPTION_GP64,
14421 OPTION_RELAX_BRANCH,
14422 OPTION_NO_RELAX_BRANCH,
14423 OPTION_MSHARED,
14424 OPTION_MNO_SHARED,
14425 OPTION_MSYM32,
14426 OPTION_MNO_SYM32,
14427 OPTION_SOFT_FLOAT,
14428 OPTION_HARD_FLOAT,
14429 OPTION_SINGLE_FLOAT,
14430 OPTION_DOUBLE_FLOAT,
14431 OPTION_32,
14432#ifdef OBJ_ELF
14433 OPTION_CALL_SHARED,
14434 OPTION_CALL_NONPIC,
14435 OPTION_NON_SHARED,
14436 OPTION_XGOT,
14437 OPTION_MABI,
14438 OPTION_N32,
14439 OPTION_64,
14440 OPTION_MDEBUG,
14441 OPTION_NO_MDEBUG,
14442 OPTION_PDR,
14443 OPTION_NO_PDR,
14444 OPTION_MVXWORKS_PIC,
14445#endif /* OBJ_ELF */
14446 OPTION_END_OF_ENUM
14447 };
14448
e972090a
NC
14449struct option md_longopts[] =
14450{
f9b4148d 14451 /* Options which specify architecture. */
f9b4148d 14452 {"march", required_argument, NULL, OPTION_MARCH},
f9b4148d 14453 {"mtune", required_argument, NULL, OPTION_MTUNE},
252b5132
RH
14454 {"mips0", no_argument, NULL, OPTION_MIPS1},
14455 {"mips1", no_argument, NULL, OPTION_MIPS1},
252b5132 14456 {"mips2", no_argument, NULL, OPTION_MIPS2},
252b5132 14457 {"mips3", no_argument, NULL, OPTION_MIPS3},
252b5132 14458 {"mips4", no_argument, NULL, OPTION_MIPS4},
ae948b86 14459 {"mips5", no_argument, NULL, OPTION_MIPS5},
ae948b86 14460 {"mips32", no_argument, NULL, OPTION_MIPS32},
ae948b86 14461 {"mips64", no_argument, NULL, OPTION_MIPS64},
f9b4148d 14462 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
5f74bc13 14463 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
f9b4148d
CD
14464
14465 /* Options which specify Application Specific Extensions (ASEs). */
f9b4148d 14466 {"mips16", no_argument, NULL, OPTION_MIPS16},
f9b4148d 14467 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
f9b4148d 14468 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
f9b4148d 14469 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
f9b4148d 14470 {"mdmx", no_argument, NULL, OPTION_MDMX},
f9b4148d 14471 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
74cd071d 14472 {"mdsp", no_argument, NULL, OPTION_DSP},
74cd071d 14473 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
ef2e4d86 14474 {"mmt", no_argument, NULL, OPTION_MT},
ef2e4d86 14475 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
e16bfa71 14476 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
e16bfa71 14477 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
8b082fb1 14478 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
8b082fb1 14479 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
df58fc94
RS
14480 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
14481 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
dec0624d
MR
14482 {"mmcu", no_argument, NULL, OPTION_MCU},
14483 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
f9b4148d
CD
14484
14485 /* Old-style architecture options. Don't add more of these. */
f9b4148d 14486 {"m4650", no_argument, NULL, OPTION_M4650},
f9b4148d 14487 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
f9b4148d 14488 {"m4010", no_argument, NULL, OPTION_M4010},
f9b4148d 14489 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
f9b4148d 14490 {"m4100", no_argument, NULL, OPTION_M4100},
f9b4148d 14491 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
f9b4148d 14492 {"m3900", no_argument, NULL, OPTION_M3900},
f9b4148d
CD
14493 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
14494
14495 /* Options which enable bug fixes. */
f9b4148d 14496 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
f9b4148d
CD
14497 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
14498 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
c67a084a
NC
14499 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
14500 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
14501 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
14502 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
d766e8ec
RS
14503 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
14504 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
7d8e00cf
RS
14505 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
14506 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
6a32d874
CM
14507 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
14508 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
d954098f
DD
14509 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
14510 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
f9b4148d
CD
14511
14512 /* Miscellaneous options. */
252b5132
RH
14513 {"trap", no_argument, NULL, OPTION_TRAP},
14514 {"no-break", no_argument, NULL, OPTION_TRAP},
252b5132
RH
14515 {"break", no_argument, NULL, OPTION_BREAK},
14516 {"no-trap", no_argument, NULL, OPTION_BREAK},
252b5132 14517 {"EB", no_argument, NULL, OPTION_EB},
252b5132 14518 {"EL", no_argument, NULL, OPTION_EL},
ae948b86 14519 {"mfp32", no_argument, NULL, OPTION_FP32},
c97ef257 14520 {"mgp32", no_argument, NULL, OPTION_GP32},
119d663a 14521 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
119d663a 14522 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
316f5878 14523 {"mfp64", no_argument, NULL, OPTION_FP64},
ae948b86 14524 {"mgp64", no_argument, NULL, OPTION_GP64},
4a6a3df4
AO
14525 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
14526 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
aa6975fb
ILT
14527 {"mshared", no_argument, NULL, OPTION_MSHARED},
14528 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
aed1a261
RS
14529 {"msym32", no_argument, NULL, OPTION_MSYM32},
14530 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
037b32b9
AN
14531 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
14532 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
037b32b9
AN
14533 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
14534 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
23fce1e3
NC
14535
14536 /* Strictly speaking this next option is ELF specific,
14537 but we allow it for other ports as well in order to
14538 make testing easier. */
14539 {"32", no_argument, NULL, OPTION_32},
037b32b9 14540
f9b4148d 14541 /* ELF-specific options. */
156c2f8b 14542#ifdef OBJ_ELF
156c2f8b
NC
14543 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
14544 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
861fb55a 14545 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
156c2f8b
NC
14546 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
14547 {"xgot", no_argument, NULL, OPTION_XGOT},
ae948b86 14548 {"mabi", required_argument, NULL, OPTION_MABI},
e013f690 14549 {"n32", no_argument, NULL, OPTION_N32},
156c2f8b 14550 {"64", no_argument, NULL, OPTION_64},
ecb4347a 14551 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
ecb4347a 14552 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
dcd410fe 14553 {"mpdr", no_argument, NULL, OPTION_PDR},
dcd410fe 14554 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
0a44bf69 14555 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
ae948b86 14556#endif /* OBJ_ELF */
f9b4148d 14557
252b5132
RH
14558 {NULL, no_argument, NULL, 0}
14559};
156c2f8b 14560size_t md_longopts_size = sizeof (md_longopts);
252b5132 14561
316f5878
RS
14562/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14563 NEW_VALUE. Warn if another value was already specified. Note:
14564 we have to defer parsing the -march and -mtune arguments in order
14565 to handle 'from-abi' correctly, since the ABI might be specified
14566 in a later argument. */
14567
14568static void
17a2f251 14569mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
14570{
14571 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
14572 as_warn (_("A different %s was already specified, is now %s"),
14573 string_ptr == &mips_arch_string ? "-march" : "-mtune",
14574 new_value);
14575
14576 *string_ptr = new_value;
14577}
14578
252b5132 14579int
17a2f251 14580md_parse_option (int c, char *arg)
252b5132
RH
14581{
14582 switch (c)
14583 {
119d663a
NC
14584 case OPTION_CONSTRUCT_FLOATS:
14585 mips_disable_float_construction = 0;
14586 break;
bdaaa2e1 14587
119d663a
NC
14588 case OPTION_NO_CONSTRUCT_FLOATS:
14589 mips_disable_float_construction = 1;
14590 break;
bdaaa2e1 14591
252b5132
RH
14592 case OPTION_TRAP:
14593 mips_trap = 1;
14594 break;
14595
14596 case OPTION_BREAK:
14597 mips_trap = 0;
14598 break;
14599
14600 case OPTION_EB:
14601 target_big_endian = 1;
14602 break;
14603
14604 case OPTION_EL:
14605 target_big_endian = 0;
14606 break;
14607
14608 case 'O':
4ffff32f
TS
14609 if (arg == NULL)
14610 mips_optimize = 1;
14611 else if (arg[0] == '0')
14612 mips_optimize = 0;
14613 else if (arg[0] == '1')
252b5132
RH
14614 mips_optimize = 1;
14615 else
14616 mips_optimize = 2;
14617 break;
14618
14619 case 'g':
14620 if (arg == NULL)
14621 mips_debug = 2;
14622 else
14623 mips_debug = atoi (arg);
252b5132
RH
14624 break;
14625
14626 case OPTION_MIPS1:
316f5878 14627 file_mips_isa = ISA_MIPS1;
252b5132
RH
14628 break;
14629
14630 case OPTION_MIPS2:
316f5878 14631 file_mips_isa = ISA_MIPS2;
252b5132
RH
14632 break;
14633
14634 case OPTION_MIPS3:
316f5878 14635 file_mips_isa = ISA_MIPS3;
252b5132
RH
14636 break;
14637
14638 case OPTION_MIPS4:
316f5878 14639 file_mips_isa = ISA_MIPS4;
e7af610e
NC
14640 break;
14641
84ea6cf2 14642 case OPTION_MIPS5:
316f5878 14643 file_mips_isa = ISA_MIPS5;
84ea6cf2
NC
14644 break;
14645
e7af610e 14646 case OPTION_MIPS32:
316f5878 14647 file_mips_isa = ISA_MIPS32;
252b5132
RH
14648 break;
14649
af7ee8bf
CD
14650 case OPTION_MIPS32R2:
14651 file_mips_isa = ISA_MIPS32R2;
14652 break;
14653
5f74bc13
CD
14654 case OPTION_MIPS64R2:
14655 file_mips_isa = ISA_MIPS64R2;
14656 break;
14657
84ea6cf2 14658 case OPTION_MIPS64:
316f5878 14659 file_mips_isa = ISA_MIPS64;
84ea6cf2
NC
14660 break;
14661
ec68c924 14662 case OPTION_MTUNE:
316f5878
RS
14663 mips_set_option_string (&mips_tune_string, arg);
14664 break;
ec68c924 14665
316f5878
RS
14666 case OPTION_MARCH:
14667 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
14668 break;
14669
14670 case OPTION_M4650:
316f5878
RS
14671 mips_set_option_string (&mips_arch_string, "4650");
14672 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
14673 break;
14674
14675 case OPTION_NO_M4650:
14676 break;
14677
14678 case OPTION_M4010:
316f5878
RS
14679 mips_set_option_string (&mips_arch_string, "4010");
14680 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
14681 break;
14682
14683 case OPTION_NO_M4010:
14684 break;
14685
14686 case OPTION_M4100:
316f5878
RS
14687 mips_set_option_string (&mips_arch_string, "4100");
14688 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
14689 break;
14690
14691 case OPTION_NO_M4100:
14692 break;
14693
252b5132 14694 case OPTION_M3900:
316f5878
RS
14695 mips_set_option_string (&mips_arch_string, "3900");
14696 mips_set_option_string (&mips_tune_string, "3900");
252b5132 14697 break;
bdaaa2e1 14698
252b5132
RH
14699 case OPTION_NO_M3900:
14700 break;
14701
deec1734
CD
14702 case OPTION_MDMX:
14703 mips_opts.ase_mdmx = 1;
14704 break;
14705
14706 case OPTION_NO_MDMX:
14707 mips_opts.ase_mdmx = 0;
14708 break;
14709
74cd071d
CF
14710 case OPTION_DSP:
14711 mips_opts.ase_dsp = 1;
8b082fb1 14712 mips_opts.ase_dspr2 = 0;
74cd071d
CF
14713 break;
14714
14715 case OPTION_NO_DSP:
8b082fb1
TS
14716 mips_opts.ase_dsp = 0;
14717 mips_opts.ase_dspr2 = 0;
14718 break;
14719
14720 case OPTION_DSPR2:
14721 mips_opts.ase_dspr2 = 1;
14722 mips_opts.ase_dsp = 1;
14723 break;
14724
14725 case OPTION_NO_DSPR2:
14726 mips_opts.ase_dspr2 = 0;
74cd071d
CF
14727 mips_opts.ase_dsp = 0;
14728 break;
14729
ef2e4d86
CF
14730 case OPTION_MT:
14731 mips_opts.ase_mt = 1;
14732 break;
14733
14734 case OPTION_NO_MT:
14735 mips_opts.ase_mt = 0;
14736 break;
14737
dec0624d
MR
14738 case OPTION_MCU:
14739 mips_opts.ase_mcu = 1;
14740 break;
14741
14742 case OPTION_NO_MCU:
14743 mips_opts.ase_mcu = 0;
14744 break;
14745
df58fc94
RS
14746 case OPTION_MICROMIPS:
14747 if (mips_opts.mips16 == 1)
14748 {
14749 as_bad (_("-mmicromips cannot be used with -mips16"));
14750 return 0;
14751 }
14752 mips_opts.micromips = 1;
14753 mips_no_prev_insn ();
14754 break;
14755
14756 case OPTION_NO_MICROMIPS:
14757 mips_opts.micromips = 0;
14758 mips_no_prev_insn ();
14759 break;
14760
252b5132 14761 case OPTION_MIPS16:
df58fc94
RS
14762 if (mips_opts.micromips == 1)
14763 {
14764 as_bad (_("-mips16 cannot be used with -micromips"));
14765 return 0;
14766 }
252b5132 14767 mips_opts.mips16 = 1;
7d10b47d 14768 mips_no_prev_insn ();
252b5132
RH
14769 break;
14770
14771 case OPTION_NO_MIPS16:
14772 mips_opts.mips16 = 0;
7d10b47d 14773 mips_no_prev_insn ();
252b5132
RH
14774 break;
14775
1f25f5d3
CD
14776 case OPTION_MIPS3D:
14777 mips_opts.ase_mips3d = 1;
14778 break;
14779
14780 case OPTION_NO_MIPS3D:
14781 mips_opts.ase_mips3d = 0;
14782 break;
14783
e16bfa71
TS
14784 case OPTION_SMARTMIPS:
14785 mips_opts.ase_smartmips = 1;
14786 break;
14787
14788 case OPTION_NO_SMARTMIPS:
14789 mips_opts.ase_smartmips = 0;
14790 break;
14791
6a32d874
CM
14792 case OPTION_FIX_24K:
14793 mips_fix_24k = 1;
14794 break;
14795
14796 case OPTION_NO_FIX_24K:
14797 mips_fix_24k = 0;
14798 break;
14799
c67a084a
NC
14800 case OPTION_FIX_LOONGSON2F_JUMP:
14801 mips_fix_loongson2f_jump = TRUE;
14802 break;
14803
14804 case OPTION_NO_FIX_LOONGSON2F_JUMP:
14805 mips_fix_loongson2f_jump = FALSE;
14806 break;
14807
14808 case OPTION_FIX_LOONGSON2F_NOP:
14809 mips_fix_loongson2f_nop = TRUE;
14810 break;
14811
14812 case OPTION_NO_FIX_LOONGSON2F_NOP:
14813 mips_fix_loongson2f_nop = FALSE;
14814 break;
14815
d766e8ec
RS
14816 case OPTION_FIX_VR4120:
14817 mips_fix_vr4120 = 1;
60b63b72
RS
14818 break;
14819
d766e8ec
RS
14820 case OPTION_NO_FIX_VR4120:
14821 mips_fix_vr4120 = 0;
60b63b72
RS
14822 break;
14823
7d8e00cf
RS
14824 case OPTION_FIX_VR4130:
14825 mips_fix_vr4130 = 1;
14826 break;
14827
14828 case OPTION_NO_FIX_VR4130:
14829 mips_fix_vr4130 = 0;
14830 break;
14831
d954098f
DD
14832 case OPTION_FIX_CN63XXP1:
14833 mips_fix_cn63xxp1 = TRUE;
14834 break;
14835
14836 case OPTION_NO_FIX_CN63XXP1:
14837 mips_fix_cn63xxp1 = FALSE;
14838 break;
14839
4a6a3df4
AO
14840 case OPTION_RELAX_BRANCH:
14841 mips_relax_branch = 1;
14842 break;
14843
14844 case OPTION_NO_RELAX_BRANCH:
14845 mips_relax_branch = 0;
14846 break;
14847
aa6975fb
ILT
14848 case OPTION_MSHARED:
14849 mips_in_shared = TRUE;
14850 break;
14851
14852 case OPTION_MNO_SHARED:
14853 mips_in_shared = FALSE;
14854 break;
14855
aed1a261
RS
14856 case OPTION_MSYM32:
14857 mips_opts.sym32 = TRUE;
14858 break;
14859
14860 case OPTION_MNO_SYM32:
14861 mips_opts.sym32 = FALSE;
14862 break;
14863
0f074f60 14864#ifdef OBJ_ELF
252b5132
RH
14865 /* When generating ELF code, we permit -KPIC and -call_shared to
14866 select SVR4_PIC, and -non_shared to select no PIC. This is
14867 intended to be compatible with Irix 5. */
14868 case OPTION_CALL_SHARED:
f43abd2b 14869 if (!IS_ELF)
252b5132
RH
14870 {
14871 as_bad (_("-call_shared is supported only for ELF format"));
14872 return 0;
14873 }
14874 mips_pic = SVR4_PIC;
143d77c5 14875 mips_abicalls = TRUE;
252b5132
RH
14876 break;
14877
861fb55a
DJ
14878 case OPTION_CALL_NONPIC:
14879 if (!IS_ELF)
14880 {
14881 as_bad (_("-call_nonpic is supported only for ELF format"));
14882 return 0;
14883 }
14884 mips_pic = NO_PIC;
14885 mips_abicalls = TRUE;
14886 break;
14887
252b5132 14888 case OPTION_NON_SHARED:
f43abd2b 14889 if (!IS_ELF)
252b5132
RH
14890 {
14891 as_bad (_("-non_shared is supported only for ELF format"));
14892 return 0;
14893 }
14894 mips_pic = NO_PIC;
143d77c5 14895 mips_abicalls = FALSE;
252b5132
RH
14896 break;
14897
44075ae2
TS
14898 /* The -xgot option tells the assembler to use 32 bit offsets
14899 when accessing the got in SVR4_PIC mode. It is for Irix
252b5132
RH
14900 compatibility. */
14901 case OPTION_XGOT:
14902 mips_big_got = 1;
14903 break;
0f074f60 14904#endif /* OBJ_ELF */
252b5132
RH
14905
14906 case 'G':
6caf9ef4
TS
14907 g_switch_value = atoi (arg);
14908 g_switch_seen = 1;
252b5132
RH
14909 break;
14910
34ba82a8
TS
14911 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
14912 and -mabi=64. */
252b5132 14913 case OPTION_32:
23fce1e3
NC
14914 if (IS_ELF)
14915 mips_abi = O32_ABI;
14916 /* We silently ignore -32 for non-ELF targets. This greatly
14917 simplifies the construction of the MIPS GAS test cases. */
252b5132
RH
14918 break;
14919
23fce1e3 14920#ifdef OBJ_ELF
e013f690 14921 case OPTION_N32:
f43abd2b 14922 if (!IS_ELF)
34ba82a8
TS
14923 {
14924 as_bad (_("-n32 is supported for ELF format only"));
14925 return 0;
14926 }
316f5878 14927 mips_abi = N32_ABI;
e013f690 14928 break;
252b5132 14929
e013f690 14930 case OPTION_64:
f43abd2b 14931 if (!IS_ELF)
34ba82a8
TS
14932 {
14933 as_bad (_("-64 is supported for ELF format only"));
14934 return 0;
14935 }
316f5878 14936 mips_abi = N64_ABI;
f43abd2b 14937 if (!support_64bit_objects())
e013f690 14938 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132 14939 break;
ae948b86 14940#endif /* OBJ_ELF */
252b5132 14941
c97ef257 14942 case OPTION_GP32:
a325df1d 14943 file_mips_gp32 = 1;
c97ef257
AH
14944 break;
14945
14946 case OPTION_GP64:
a325df1d 14947 file_mips_gp32 = 0;
c97ef257 14948 break;
252b5132 14949
ca4e0257 14950 case OPTION_FP32:
a325df1d 14951 file_mips_fp32 = 1;
316f5878
RS
14952 break;
14953
14954 case OPTION_FP64:
14955 file_mips_fp32 = 0;
ca4e0257
RS
14956 break;
14957
037b32b9
AN
14958 case OPTION_SINGLE_FLOAT:
14959 file_mips_single_float = 1;
14960 break;
14961
14962 case OPTION_DOUBLE_FLOAT:
14963 file_mips_single_float = 0;
14964 break;
14965
14966 case OPTION_SOFT_FLOAT:
14967 file_mips_soft_float = 1;
14968 break;
14969
14970 case OPTION_HARD_FLOAT:
14971 file_mips_soft_float = 0;
14972 break;
14973
ae948b86 14974#ifdef OBJ_ELF
252b5132 14975 case OPTION_MABI:
f43abd2b 14976 if (!IS_ELF)
34ba82a8
TS
14977 {
14978 as_bad (_("-mabi is supported for ELF format only"));
14979 return 0;
14980 }
e013f690 14981 if (strcmp (arg, "32") == 0)
316f5878 14982 mips_abi = O32_ABI;
e013f690 14983 else if (strcmp (arg, "o64") == 0)
316f5878 14984 mips_abi = O64_ABI;
e013f690 14985 else if (strcmp (arg, "n32") == 0)
316f5878 14986 mips_abi = N32_ABI;
e013f690
TS
14987 else if (strcmp (arg, "64") == 0)
14988 {
316f5878 14989 mips_abi = N64_ABI;
e013f690
TS
14990 if (! support_64bit_objects())
14991 as_fatal (_("No compiled in support for 64 bit object file "
14992 "format"));
14993 }
14994 else if (strcmp (arg, "eabi") == 0)
316f5878 14995 mips_abi = EABI_ABI;
e013f690 14996 else
da0e507f
TS
14997 {
14998 as_fatal (_("invalid abi -mabi=%s"), arg);
14999 return 0;
15000 }
252b5132 15001 break;
e013f690 15002#endif /* OBJ_ELF */
252b5132 15003
6b76fefe 15004 case OPTION_M7000_HILO_FIX:
b34976b6 15005 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
15006 break;
15007
9ee72ff1 15008 case OPTION_MNO_7000_HILO_FIX:
b34976b6 15009 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
15010 break;
15011
ecb4347a
DJ
15012#ifdef OBJ_ELF
15013 case OPTION_MDEBUG:
b34976b6 15014 mips_flag_mdebug = TRUE;
ecb4347a
DJ
15015 break;
15016
15017 case OPTION_NO_MDEBUG:
b34976b6 15018 mips_flag_mdebug = FALSE;
ecb4347a 15019 break;
dcd410fe
RO
15020
15021 case OPTION_PDR:
15022 mips_flag_pdr = TRUE;
15023 break;
15024
15025 case OPTION_NO_PDR:
15026 mips_flag_pdr = FALSE;
15027 break;
0a44bf69
RS
15028
15029 case OPTION_MVXWORKS_PIC:
15030 mips_pic = VXWORKS_PIC;
15031 break;
ecb4347a
DJ
15032#endif /* OBJ_ELF */
15033
252b5132
RH
15034 default:
15035 return 0;
15036 }
15037
c67a084a
NC
15038 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
15039
252b5132
RH
15040 return 1;
15041}
316f5878
RS
15042\f
15043/* Set up globals to generate code for the ISA or processor
15044 described by INFO. */
252b5132 15045
252b5132 15046static void
17a2f251 15047mips_set_architecture (const struct mips_cpu_info *info)
252b5132 15048{
316f5878 15049 if (info != 0)
252b5132 15050 {
fef14a42
TS
15051 file_mips_arch = info->cpu;
15052 mips_opts.arch = info->cpu;
316f5878 15053 mips_opts.isa = info->isa;
252b5132 15054 }
252b5132
RH
15055}
15056
252b5132 15057
316f5878 15058/* Likewise for tuning. */
252b5132 15059
316f5878 15060static void
17a2f251 15061mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
15062{
15063 if (info != 0)
fef14a42 15064 mips_tune = info->cpu;
316f5878 15065}
80cc45a5 15066
34ba82a8 15067
252b5132 15068void
17a2f251 15069mips_after_parse_args (void)
e9670677 15070{
fef14a42
TS
15071 const struct mips_cpu_info *arch_info = 0;
15072 const struct mips_cpu_info *tune_info = 0;
15073
e9670677 15074 /* GP relative stuff not working for PE */
6caf9ef4 15075 if (strncmp (TARGET_OS, "pe", 2) == 0)
e9670677 15076 {
6caf9ef4 15077 if (g_switch_seen && g_switch_value != 0)
e9670677
MR
15078 as_bad (_("-G not supported in this configuration."));
15079 g_switch_value = 0;
15080 }
15081
cac012d6
AO
15082 if (mips_abi == NO_ABI)
15083 mips_abi = MIPS_DEFAULT_ABI;
15084
22923709
RS
15085 /* The following code determines the architecture and register size.
15086 Similar code was added to GCC 3.3 (see override_options() in
15087 config/mips/mips.c). The GAS and GCC code should be kept in sync
15088 as much as possible. */
e9670677 15089
316f5878 15090 if (mips_arch_string != 0)
fef14a42 15091 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 15092
316f5878 15093 if (file_mips_isa != ISA_UNKNOWN)
e9670677 15094 {
316f5878 15095 /* Handle -mipsN. At this point, file_mips_isa contains the
fef14a42 15096 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 15097 the -march selection (if any). */
fef14a42 15098 if (arch_info != 0)
e9670677 15099 {
316f5878
RS
15100 /* -march takes precedence over -mipsN, since it is more descriptive.
15101 There's no harm in specifying both as long as the ISA levels
15102 are the same. */
fef14a42 15103 if (file_mips_isa != arch_info->isa)
316f5878
RS
15104 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
15105 mips_cpu_info_from_isa (file_mips_isa)->name,
fef14a42 15106 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 15107 }
316f5878 15108 else
fef14a42 15109 arch_info = mips_cpu_info_from_isa (file_mips_isa);
e9670677
MR
15110 }
15111
fef14a42 15112 if (arch_info == 0)
95bfe26e
MF
15113 {
15114 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
15115 gas_assert (arch_info);
15116 }
e9670677 15117
fef14a42 15118 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
20203fb9 15119 as_bad (_("-march=%s is not compatible with the selected ABI"),
fef14a42
TS
15120 arch_info->name);
15121
15122 mips_set_architecture (arch_info);
15123
15124 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
15125 if (mips_tune_string != 0)
15126 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 15127
fef14a42
TS
15128 if (tune_info == 0)
15129 mips_set_tune (arch_info);
15130 else
15131 mips_set_tune (tune_info);
e9670677 15132
316f5878 15133 if (file_mips_gp32 >= 0)
e9670677 15134 {
316f5878
RS
15135 /* The user specified the size of the integer registers. Make sure
15136 it agrees with the ABI and ISA. */
15137 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
15138 as_bad (_("-mgp64 used with a 32-bit processor"));
15139 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
15140 as_bad (_("-mgp32 used with a 64-bit ABI"));
15141 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
15142 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
15143 }
15144 else
15145 {
316f5878
RS
15146 /* Infer the integer register size from the ABI and processor.
15147 Restrict ourselves to 32-bit registers if that's all the
15148 processor has, or if the ABI cannot handle 64-bit registers. */
15149 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
15150 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
e9670677
MR
15151 }
15152
ad3fea08
TS
15153 switch (file_mips_fp32)
15154 {
15155 default:
15156 case -1:
15157 /* No user specified float register size.
15158 ??? GAS treats single-float processors as though they had 64-bit
15159 float registers (although it complains when double-precision
15160 instructions are used). As things stand, saying they have 32-bit
15161 registers would lead to spurious "register must be even" messages.
15162 So here we assume float registers are never smaller than the
15163 integer ones. */
15164 if (file_mips_gp32 == 0)
15165 /* 64-bit integer registers implies 64-bit float registers. */
15166 file_mips_fp32 = 0;
15167 else if ((mips_opts.ase_mips3d > 0 || mips_opts.ase_mdmx > 0)
15168 && ISA_HAS_64BIT_FPRS (mips_opts.isa))
15169 /* -mips3d and -mdmx imply 64-bit float registers, if possible. */
15170 file_mips_fp32 = 0;
15171 else
15172 /* 32-bit float registers. */
15173 file_mips_fp32 = 1;
15174 break;
15175
15176 /* The user specified the size of the float registers. Check if it
15177 agrees with the ABI and ISA. */
15178 case 0:
15179 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
15180 as_bad (_("-mfp64 used with a 32-bit fpu"));
15181 else if (ABI_NEEDS_32BIT_REGS (mips_abi)
15182 && !ISA_HAS_MXHC1 (mips_opts.isa))
15183 as_warn (_("-mfp64 used with a 32-bit ABI"));
15184 break;
15185 case 1:
15186 if (ABI_NEEDS_64BIT_REGS (mips_abi))
15187 as_warn (_("-mfp32 used with a 64-bit ABI"));
15188 break;
15189 }
e9670677 15190
316f5878 15191 /* End of GCC-shared inference code. */
e9670677 15192
17a2f251
TS
15193 /* This flag is set when we have a 64-bit capable CPU but use only
15194 32-bit wide registers. Note that EABI does not use it. */
15195 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
15196 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
15197 || mips_abi == O32_ABI))
316f5878 15198 mips_32bitmode = 1;
e9670677
MR
15199
15200 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
15201 as_bad (_("trap exception not supported at ISA 1"));
15202
e9670677
MR
15203 /* If the selected architecture includes support for ASEs, enable
15204 generation of code for them. */
a4672219 15205 if (mips_opts.mips16 == -1)
fef14a42 15206 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
df58fc94
RS
15207 if (mips_opts.micromips == -1)
15208 mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_arch)) ? 1 : 0;
ffdefa66 15209 if (mips_opts.ase_mips3d == -1)
65263ce3 15210 mips_opts.ase_mips3d = ((arch_info->flags & MIPS_CPU_ASE_MIPS3D)
ad3fea08
TS
15211 && file_mips_fp32 == 0) ? 1 : 0;
15212 if (mips_opts.ase_mips3d && file_mips_fp32 == 1)
15213 as_bad (_("-mfp32 used with -mips3d"));
15214
ffdefa66 15215 if (mips_opts.ase_mdmx == -1)
65263ce3 15216 mips_opts.ase_mdmx = ((arch_info->flags & MIPS_CPU_ASE_MDMX)
ad3fea08
TS
15217 && file_mips_fp32 == 0) ? 1 : 0;
15218 if (mips_opts.ase_mdmx && file_mips_fp32 == 1)
15219 as_bad (_("-mfp32 used with -mdmx"));
15220
15221 if (mips_opts.ase_smartmips == -1)
15222 mips_opts.ase_smartmips = (arch_info->flags & MIPS_CPU_ASE_SMARTMIPS) ? 1 : 0;
15223 if (mips_opts.ase_smartmips && !ISA_SUPPORTS_SMARTMIPS)
20203fb9
NC
15224 as_warn (_("%s ISA does not support SmartMIPS"),
15225 mips_cpu_info_from_isa (mips_opts.isa)->name);
ad3fea08 15226
74cd071d 15227 if (mips_opts.ase_dsp == -1)
ad3fea08
TS
15228 mips_opts.ase_dsp = (arch_info->flags & MIPS_CPU_ASE_DSP) ? 1 : 0;
15229 if (mips_opts.ase_dsp && !ISA_SUPPORTS_DSP_ASE)
20203fb9
NC
15230 as_warn (_("%s ISA does not support DSP ASE"),
15231 mips_cpu_info_from_isa (mips_opts.isa)->name);
ad3fea08 15232
8b082fb1
TS
15233 if (mips_opts.ase_dspr2 == -1)
15234 {
15235 mips_opts.ase_dspr2 = (arch_info->flags & MIPS_CPU_ASE_DSPR2) ? 1 : 0;
15236 mips_opts.ase_dsp = (arch_info->flags & MIPS_CPU_ASE_DSP) ? 1 : 0;
15237 }
15238 if (mips_opts.ase_dspr2 && !ISA_SUPPORTS_DSPR2_ASE)
20203fb9
NC
15239 as_warn (_("%s ISA does not support DSP R2 ASE"),
15240 mips_cpu_info_from_isa (mips_opts.isa)->name);
8b082fb1 15241
ef2e4d86 15242 if (mips_opts.ase_mt == -1)
ad3fea08
TS
15243 mips_opts.ase_mt = (arch_info->flags & MIPS_CPU_ASE_MT) ? 1 : 0;
15244 if (mips_opts.ase_mt && !ISA_SUPPORTS_MT_ASE)
20203fb9
NC
15245 as_warn (_("%s ISA does not support MT ASE"),
15246 mips_cpu_info_from_isa (mips_opts.isa)->name);
e9670677 15247
dec0624d
MR
15248 if (mips_opts.ase_mcu == -1)
15249 mips_opts.ase_mcu = (arch_info->flags & MIPS_CPU_ASE_MCU) ? 1 : 0;
15250 if (mips_opts.ase_mcu && !ISA_SUPPORTS_MCU_ASE)
15251 as_warn (_("%s ISA does not support MCU ASE"),
15252 mips_cpu_info_from_isa (mips_opts.isa)->name);
15253
e9670677 15254 file_mips_isa = mips_opts.isa;
e9670677
MR
15255 file_ase_mips3d = mips_opts.ase_mips3d;
15256 file_ase_mdmx = mips_opts.ase_mdmx;
e16bfa71 15257 file_ase_smartmips = mips_opts.ase_smartmips;
74cd071d 15258 file_ase_dsp = mips_opts.ase_dsp;
8b082fb1 15259 file_ase_dspr2 = mips_opts.ase_dspr2;
ef2e4d86 15260 file_ase_mt = mips_opts.ase_mt;
e9670677
MR
15261 mips_opts.gp32 = file_mips_gp32;
15262 mips_opts.fp32 = file_mips_fp32;
037b32b9
AN
15263 mips_opts.soft_float = file_mips_soft_float;
15264 mips_opts.single_float = file_mips_single_float;
e9670677 15265
ecb4347a
DJ
15266 if (mips_flag_mdebug < 0)
15267 {
15268#ifdef OBJ_MAYBE_ECOFF
15269 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
15270 mips_flag_mdebug = 1;
15271 else
15272#endif /* OBJ_MAYBE_ECOFF */
15273 mips_flag_mdebug = 0;
15274 }
e9670677
MR
15275}
15276\f
15277void
17a2f251 15278mips_init_after_args (void)
252b5132
RH
15279{
15280 /* initialize opcodes */
15281 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 15282 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
15283}
15284
15285long
17a2f251 15286md_pcrel_from (fixS *fixP)
252b5132 15287{
a7ebbfdf
TS
15288 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
15289 switch (fixP->fx_r_type)
15290 {
df58fc94
RS
15291 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15292 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15293 /* Return the address of the delay slot. */
15294 return addr + 2;
15295
15296 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15297 case BFD_RELOC_MICROMIPS_JMP:
a7ebbfdf
TS
15298 case BFD_RELOC_16_PCREL_S2:
15299 case BFD_RELOC_MIPS_JMP:
15300 /* Return the address of the delay slot. */
15301 return addr + 4;
df58fc94 15302
a7ebbfdf 15303 default:
58ea3d6a 15304 /* We have no relocation type for PC relative MIPS16 instructions. */
64817874
TS
15305 if (fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != now_seg)
15306 as_bad_where (fixP->fx_file, fixP->fx_line,
15307 _("PC relative MIPS16 instruction references a different section"));
a7ebbfdf
TS
15308 return addr;
15309 }
252b5132
RH
15310}
15311
252b5132
RH
15312/* This is called before the symbol table is processed. In order to
15313 work with gcc when using mips-tfile, we must keep all local labels.
15314 However, in other cases, we want to discard them. If we were
15315 called with -g, but we didn't see any debugging information, it may
15316 mean that gcc is smuggling debugging information through to
15317 mips-tfile, in which case we must generate all local labels. */
15318
15319void
17a2f251 15320mips_frob_file_before_adjust (void)
252b5132
RH
15321{
15322#ifndef NO_ECOFF_DEBUGGING
15323 if (ECOFF_DEBUGGING
15324 && mips_debug != 0
15325 && ! ecoff_debugging_seen)
15326 flag_keep_locals = 1;
15327#endif
15328}
15329
3b91255e 15330/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 15331 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
15332 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
15333 relocation operators.
15334
15335 For our purposes, a %lo() expression matches a %got() or %hi()
15336 expression if:
15337
15338 (a) it refers to the same symbol; and
15339 (b) the offset applied in the %lo() expression is no lower than
15340 the offset applied in the %got() or %hi().
15341
15342 (b) allows us to cope with code like:
15343
15344 lui $4,%hi(foo)
15345 lh $4,%lo(foo+2)($4)
15346
15347 ...which is legal on RELA targets, and has a well-defined behaviour
15348 if the user knows that adding 2 to "foo" will not induce a carry to
15349 the high 16 bits.
15350
15351 When several %lo()s match a particular %got() or %hi(), we use the
15352 following rules to distinguish them:
15353
15354 (1) %lo()s with smaller offsets are a better match than %lo()s with
15355 higher offsets.
15356
15357 (2) %lo()s with no matching %got() or %hi() are better than those
15358 that already have a matching %got() or %hi().
15359
15360 (3) later %lo()s are better than earlier %lo()s.
15361
15362 These rules are applied in order.
15363
15364 (1) means, among other things, that %lo()s with identical offsets are
15365 chosen if they exist.
15366
15367 (2) means that we won't associate several high-part relocations with
15368 the same low-part relocation unless there's no alternative. Having
15369 several high parts for the same low part is a GNU extension; this rule
15370 allows careful users to avoid it.
15371
15372 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
15373 with the last high-part relocation being at the front of the list.
15374 It therefore makes sense to choose the last matching low-part
15375 relocation, all other things being equal. It's also easier
15376 to code that way. */
252b5132
RH
15377
15378void
17a2f251 15379mips_frob_file (void)
252b5132
RH
15380{
15381 struct mips_hi_fixup *l;
35903be0 15382 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
252b5132
RH
15383
15384 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15385 {
15386 segment_info_type *seginfo;
3b91255e
RS
15387 bfd_boolean matched_lo_p;
15388 fixS **hi_pos, **lo_pos, **pos;
252b5132 15389
9c2799c2 15390 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 15391
5919d012 15392 /* If a GOT16 relocation turns out to be against a global symbol,
b886a2ab
RS
15393 there isn't supposed to be a matching LO. Ignore %gots against
15394 constants; we'll report an error for those later. */
738e5348 15395 if (got16_reloc_p (l->fixp->fx_r_type)
b886a2ab
RS
15396 && !(l->fixp->fx_addsy
15397 && pic_need_relax (l->fixp->fx_addsy, l->seg)))
5919d012
RS
15398 continue;
15399
15400 /* Check quickly whether the next fixup happens to be a matching %lo. */
15401 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
15402 continue;
15403
252b5132 15404 seginfo = seg_info (l->seg);
252b5132 15405
3b91255e
RS
15406 /* Set HI_POS to the position of this relocation in the chain.
15407 Set LO_POS to the position of the chosen low-part relocation.
15408 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15409 relocation that matches an immediately-preceding high-part
15410 relocation. */
15411 hi_pos = NULL;
15412 lo_pos = NULL;
15413 matched_lo_p = FALSE;
738e5348 15414 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
35903be0 15415
3b91255e
RS
15416 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15417 {
15418 if (*pos == l->fixp)
15419 hi_pos = pos;
15420
35903be0 15421 if ((*pos)->fx_r_type == looking_for_rtype
30cfc97a 15422 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
3b91255e
RS
15423 && (*pos)->fx_offset >= l->fixp->fx_offset
15424 && (lo_pos == NULL
15425 || (*pos)->fx_offset < (*lo_pos)->fx_offset
15426 || (!matched_lo_p
15427 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15428 lo_pos = pos;
15429
15430 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15431 && fixup_has_matching_lo_p (*pos));
15432 }
15433
15434 /* If we found a match, remove the high-part relocation from its
15435 current position and insert it before the low-part relocation.
15436 Make the offsets match so that fixup_has_matching_lo_p()
15437 will return true.
15438
15439 We don't warn about unmatched high-part relocations since some
15440 versions of gcc have been known to emit dead "lui ...%hi(...)"
15441 instructions. */
15442 if (lo_pos != NULL)
15443 {
15444 l->fixp->fx_offset = (*lo_pos)->fx_offset;
15445 if (l->fixp->fx_next != *lo_pos)
252b5132 15446 {
3b91255e
RS
15447 *hi_pos = l->fixp->fx_next;
15448 l->fixp->fx_next = *lo_pos;
15449 *lo_pos = l->fixp;
252b5132 15450 }
252b5132
RH
15451 }
15452 }
15453}
15454
252b5132 15455int
17a2f251 15456mips_force_relocation (fixS *fixp)
252b5132 15457{
ae6063d4 15458 if (generic_force_reloc (fixp))
252b5132
RH
15459 return 1;
15460
df58fc94
RS
15461 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15462 so that the linker relaxation can update targets. */
15463 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15464 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15465 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15466 return 1;
15467
3e722fb5 15468 return 0;
252b5132
RH
15469}
15470
b886a2ab
RS
15471/* Read the instruction associated with RELOC from BUF. */
15472
15473static unsigned int
15474read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15475{
15476 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15477 return read_compressed_insn (buf, 4);
15478 else
15479 return read_insn (buf);
15480}
15481
15482/* Write instruction INSN to BUF, given that it has been relocated
15483 by RELOC. */
15484
15485static void
15486write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15487 unsigned long insn)
15488{
15489 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15490 write_compressed_insn (buf, insn, 4);
15491 else
15492 write_insn (buf, insn);
15493}
15494
252b5132
RH
15495/* Apply a fixup to the object file. */
15496
94f592af 15497void
55cf6793 15498md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 15499{
4d68580a 15500 char *buf;
b886a2ab 15501 unsigned long insn;
a7ebbfdf 15502 reloc_howto_type *howto;
252b5132 15503
a7ebbfdf
TS
15504 /* We ignore generic BFD relocations we don't know about. */
15505 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15506 if (! howto)
15507 return;
65551fa4 15508
df58fc94
RS
15509 gas_assert (fixP->fx_size == 2
15510 || fixP->fx_size == 4
90ecf173
MR
15511 || fixP->fx_r_type == BFD_RELOC_16
15512 || fixP->fx_r_type == BFD_RELOC_64
15513 || fixP->fx_r_type == BFD_RELOC_CTOR
15514 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
df58fc94 15515 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
90ecf173
MR
15516 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15517 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
15518 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64);
252b5132 15519
4d68580a 15520 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132 15521
df58fc94
RS
15522 gas_assert (!fixP->fx_pcrel || fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
15523 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15524 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15525 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1);
b1dca8ee
RS
15526
15527 /* Don't treat parts of a composite relocation as done. There are two
15528 reasons for this:
15529
15530 (1) The second and third parts will be against 0 (RSS_UNDEF) but
15531 should nevertheless be emitted if the first part is.
15532
15533 (2) In normal usage, composite relocations are never assembly-time
15534 constants. The easiest way of dealing with the pathological
15535 exceptions is to generate a relocation against STN_UNDEF and
15536 leave everything up to the linker. */
3994f87e 15537 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
252b5132
RH
15538 fixP->fx_done = 1;
15539
15540 switch (fixP->fx_r_type)
15541 {
3f98094e
DJ
15542 case BFD_RELOC_MIPS_TLS_GD:
15543 case BFD_RELOC_MIPS_TLS_LDM:
741d6ea8
JM
15544 case BFD_RELOC_MIPS_TLS_DTPREL32:
15545 case BFD_RELOC_MIPS_TLS_DTPREL64:
3f98094e
DJ
15546 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15547 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15548 case BFD_RELOC_MIPS_TLS_GOTTPREL:
d0f13682
CLT
15549 case BFD_RELOC_MIPS_TLS_TPREL32:
15550 case BFD_RELOC_MIPS_TLS_TPREL64:
3f98094e
DJ
15551 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15552 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
df58fc94
RS
15553 case BFD_RELOC_MICROMIPS_TLS_GD:
15554 case BFD_RELOC_MICROMIPS_TLS_LDM:
15555 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15556 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15557 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15558 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15559 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
d0f13682
CLT
15560 case BFD_RELOC_MIPS16_TLS_GD:
15561 case BFD_RELOC_MIPS16_TLS_LDM:
15562 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15563 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15564 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15565 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15566 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
b886a2ab
RS
15567 if (!fixP->fx_addsy)
15568 {
15569 as_bad_where (fixP->fx_file, fixP->fx_line,
15570 _("TLS relocation against a constant"));
15571 break;
15572 }
3f98094e
DJ
15573 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15574 /* fall through */
15575
252b5132 15576 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
15577 case BFD_RELOC_MIPS_SHIFT5:
15578 case BFD_RELOC_MIPS_SHIFT6:
15579 case BFD_RELOC_MIPS_GOT_DISP:
15580 case BFD_RELOC_MIPS_GOT_PAGE:
15581 case BFD_RELOC_MIPS_GOT_OFST:
15582 case BFD_RELOC_MIPS_SUB:
15583 case BFD_RELOC_MIPS_INSERT_A:
15584 case BFD_RELOC_MIPS_INSERT_B:
15585 case BFD_RELOC_MIPS_DELETE:
15586 case BFD_RELOC_MIPS_HIGHEST:
15587 case BFD_RELOC_MIPS_HIGHER:
15588 case BFD_RELOC_MIPS_SCN_DISP:
15589 case BFD_RELOC_MIPS_REL16:
15590 case BFD_RELOC_MIPS_RELGOT:
15591 case BFD_RELOC_MIPS_JALR:
252b5132
RH
15592 case BFD_RELOC_HI16:
15593 case BFD_RELOC_HI16_S:
b886a2ab 15594 case BFD_RELOC_LO16:
cdf6fd85 15595 case BFD_RELOC_GPREL16:
252b5132
RH
15596 case BFD_RELOC_MIPS_LITERAL:
15597 case BFD_RELOC_MIPS_CALL16:
15598 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 15599 case BFD_RELOC_GPREL32:
252b5132
RH
15600 case BFD_RELOC_MIPS_GOT_HI16:
15601 case BFD_RELOC_MIPS_GOT_LO16:
15602 case BFD_RELOC_MIPS_CALL_HI16:
15603 case BFD_RELOC_MIPS_CALL_LO16:
15604 case BFD_RELOC_MIPS16_GPREL:
738e5348
RS
15605 case BFD_RELOC_MIPS16_GOT16:
15606 case BFD_RELOC_MIPS16_CALL16:
d6f16593
MR
15607 case BFD_RELOC_MIPS16_HI16:
15608 case BFD_RELOC_MIPS16_HI16_S:
b886a2ab 15609 case BFD_RELOC_MIPS16_LO16:
252b5132 15610 case BFD_RELOC_MIPS16_JMP:
df58fc94
RS
15611 case BFD_RELOC_MICROMIPS_JMP:
15612 case BFD_RELOC_MICROMIPS_GOT_DISP:
15613 case BFD_RELOC_MICROMIPS_GOT_PAGE:
15614 case BFD_RELOC_MICROMIPS_GOT_OFST:
15615 case BFD_RELOC_MICROMIPS_SUB:
15616 case BFD_RELOC_MICROMIPS_HIGHEST:
15617 case BFD_RELOC_MICROMIPS_HIGHER:
15618 case BFD_RELOC_MICROMIPS_SCN_DISP:
15619 case BFD_RELOC_MICROMIPS_JALR:
15620 case BFD_RELOC_MICROMIPS_HI16:
15621 case BFD_RELOC_MICROMIPS_HI16_S:
b886a2ab 15622 case BFD_RELOC_MICROMIPS_LO16:
df58fc94
RS
15623 case BFD_RELOC_MICROMIPS_GPREL16:
15624 case BFD_RELOC_MICROMIPS_LITERAL:
15625 case BFD_RELOC_MICROMIPS_CALL16:
15626 case BFD_RELOC_MICROMIPS_GOT16:
15627 case BFD_RELOC_MICROMIPS_GOT_HI16:
15628 case BFD_RELOC_MICROMIPS_GOT_LO16:
15629 case BFD_RELOC_MICROMIPS_CALL_HI16:
15630 case BFD_RELOC_MICROMIPS_CALL_LO16:
b886a2ab
RS
15631 if (fixP->fx_done)
15632 {
15633 offsetT value;
15634
15635 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
15636 {
15637 insn = read_reloc_insn (buf, fixP->fx_r_type);
15638 if (mips16_reloc_p (fixP->fx_r_type))
15639 insn |= mips16_immed_extend (value, 16);
15640 else
15641 insn |= (value & 0xffff);
15642 write_reloc_insn (buf, fixP->fx_r_type, insn);
15643 }
15644 else
15645 as_bad_where (fixP->fx_file, fixP->fx_line,
15646 _("Unsupported constant in relocation"));
15647 }
252b5132
RH
15648 break;
15649
252b5132
RH
15650 case BFD_RELOC_64:
15651 /* This is handled like BFD_RELOC_32, but we output a sign
15652 extended value if we are only 32 bits. */
3e722fb5 15653 if (fixP->fx_done)
252b5132
RH
15654 {
15655 if (8 <= sizeof (valueT))
4d68580a 15656 md_number_to_chars (buf, *valP, 8);
252b5132
RH
15657 else
15658 {
a7ebbfdf 15659 valueT hiv;
252b5132 15660
a7ebbfdf 15661 if ((*valP & 0x80000000) != 0)
252b5132
RH
15662 hiv = 0xffffffff;
15663 else
15664 hiv = 0;
4d68580a
RS
15665 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
15666 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
252b5132
RH
15667 }
15668 }
15669 break;
15670
056350c6 15671 case BFD_RELOC_RVA:
252b5132 15672 case BFD_RELOC_32:
252b5132
RH
15673 case BFD_RELOC_16:
15674 /* If we are deleting this reloc entry, we must fill in the
54f4ddb3
TS
15675 value now. This can happen if we have a .word which is not
15676 resolved when it appears but is later defined. */
252b5132 15677 if (fixP->fx_done)
4d68580a 15678 md_number_to_chars (buf, *valP, fixP->fx_size);
252b5132
RH
15679 break;
15680
252b5132 15681 case BFD_RELOC_16_PCREL_S2:
a7ebbfdf 15682 if ((*valP & 0x3) != 0)
cb56d3d3 15683 as_bad_where (fixP->fx_file, fixP->fx_line,
bad36eac 15684 _("Branch to misaligned address (%lx)"), (long) *valP);
cb56d3d3 15685
54f4ddb3
TS
15686 /* We need to save the bits in the instruction since fixup_segment()
15687 might be deleting the relocation entry (i.e., a branch within
15688 the current segment). */
a7ebbfdf 15689 if (! fixP->fx_done)
bb2d6cd7 15690 break;
252b5132 15691
54f4ddb3 15692 /* Update old instruction data. */
4d68580a 15693 insn = read_insn (buf);
252b5132 15694
a7ebbfdf
TS
15695 if (*valP + 0x20000 <= 0x3ffff)
15696 {
15697 insn |= (*valP >> 2) & 0xffff;
4d68580a 15698 write_insn (buf, insn);
a7ebbfdf
TS
15699 }
15700 else if (mips_pic == NO_PIC
15701 && fixP->fx_done
15702 && fixP->fx_frag->fr_address >= text_section->vma
15703 && (fixP->fx_frag->fr_address
587aac4e 15704 < text_section->vma + bfd_get_section_size (text_section))
a7ebbfdf
TS
15705 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
15706 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
15707 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
15708 {
15709 /* The branch offset is too large. If this is an
15710 unconditional branch, and we are not generating PIC code,
15711 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
15712 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
15713 insn = 0x0c000000; /* jal */
252b5132 15714 else
a7ebbfdf
TS
15715 insn = 0x08000000; /* j */
15716 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
15717 fixP->fx_done = 0;
15718 fixP->fx_addsy = section_symbol (text_section);
15719 *valP += md_pcrel_from (fixP);
4d68580a 15720 write_insn (buf, insn);
a7ebbfdf
TS
15721 }
15722 else
15723 {
15724 /* If we got here, we have branch-relaxation disabled,
15725 and there's nothing we can do to fix this instruction
15726 without turning it into a longer sequence. */
15727 as_bad_where (fixP->fx_file, fixP->fx_line,
15728 _("Branch out of range"));
252b5132 15729 }
252b5132
RH
15730 break;
15731
df58fc94
RS
15732 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15733 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15734 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15735 /* We adjust the offset back to even. */
15736 if ((*valP & 0x1) != 0)
15737 --(*valP);
15738
15739 if (! fixP->fx_done)
15740 break;
15741
15742 /* Should never visit here, because we keep the relocation. */
15743 abort ();
15744 break;
15745
252b5132
RH
15746 case BFD_RELOC_VTABLE_INHERIT:
15747 fixP->fx_done = 0;
15748 if (fixP->fx_addsy
15749 && !S_IS_DEFINED (fixP->fx_addsy)
15750 && !S_IS_WEAK (fixP->fx_addsy))
15751 S_SET_WEAK (fixP->fx_addsy);
15752 break;
15753
15754 case BFD_RELOC_VTABLE_ENTRY:
15755 fixP->fx_done = 0;
15756 break;
15757
15758 default:
15759 internalError ();
15760 }
a7ebbfdf
TS
15761
15762 /* Remember value for tc_gen_reloc. */
15763 fixP->fx_addnumber = *valP;
252b5132
RH
15764}
15765
252b5132 15766static symbolS *
17a2f251 15767get_symbol (void)
252b5132
RH
15768{
15769 int c;
15770 char *name;
15771 symbolS *p;
15772
15773 name = input_line_pointer;
15774 c = get_symbol_end ();
15775 p = (symbolS *) symbol_find_or_make (name);
15776 *input_line_pointer = c;
15777 return p;
15778}
15779
742a56fe
RS
15780/* Align the current frag to a given power of two. If a particular
15781 fill byte should be used, FILL points to an integer that contains
15782 that byte, otherwise FILL is null.
15783
462427c4
RS
15784 This function used to have the comment:
15785
15786 The MIPS assembler also automatically adjusts any preceding label.
15787
15788 The implementation therefore applied the adjustment to a maximum of
15789 one label. However, other label adjustments are applied to batches
15790 of labels, and adjusting just one caused problems when new labels
15791 were added for the sake of debugging or unwind information.
15792 We therefore adjust all preceding labels (given as LABELS) instead. */
252b5132
RH
15793
15794static void
462427c4 15795mips_align (int to, int *fill, struct insn_label_list *labels)
252b5132 15796{
7d10b47d 15797 mips_emit_delays ();
df58fc94 15798 mips_record_compressed_mode ();
742a56fe
RS
15799 if (fill == NULL && subseg_text_p (now_seg))
15800 frag_align_code (to, 0);
15801 else
15802 frag_align (to, fill ? *fill : 0, 0);
252b5132 15803 record_alignment (now_seg, to);
462427c4 15804 mips_move_labels (labels, FALSE);
252b5132
RH
15805}
15806
15807/* Align to a given power of two. .align 0 turns off the automatic
15808 alignment used by the data creating pseudo-ops. */
15809
15810static void
17a2f251 15811s_align (int x ATTRIBUTE_UNUSED)
252b5132 15812{
742a56fe 15813 int temp, fill_value, *fill_ptr;
49954fb4 15814 long max_alignment = 28;
252b5132 15815
54f4ddb3 15816 /* o Note that the assembler pulls down any immediately preceding label
252b5132 15817 to the aligned address.
54f4ddb3 15818 o It's not documented but auto alignment is reinstated by
252b5132 15819 a .align pseudo instruction.
54f4ddb3 15820 o Note also that after auto alignment is turned off the mips assembler
252b5132 15821 issues an error on attempt to assemble an improperly aligned data item.
54f4ddb3 15822 We don't. */
252b5132
RH
15823
15824 temp = get_absolute_expression ();
15825 if (temp > max_alignment)
15826 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
15827 else if (temp < 0)
15828 {
15829 as_warn (_("Alignment negative: 0 assumed."));
15830 temp = 0;
15831 }
15832 if (*input_line_pointer == ',')
15833 {
f9419b05 15834 ++input_line_pointer;
742a56fe
RS
15835 fill_value = get_absolute_expression ();
15836 fill_ptr = &fill_value;
252b5132
RH
15837 }
15838 else
742a56fe 15839 fill_ptr = 0;
252b5132
RH
15840 if (temp)
15841 {
a8dbcb85
TS
15842 segment_info_type *si = seg_info (now_seg);
15843 struct insn_label_list *l = si->label_list;
54f4ddb3 15844 /* Auto alignment should be switched on by next section change. */
252b5132 15845 auto_align = 1;
462427c4 15846 mips_align (temp, fill_ptr, l);
252b5132
RH
15847 }
15848 else
15849 {
15850 auto_align = 0;
15851 }
15852
15853 demand_empty_rest_of_line ();
15854}
15855
252b5132 15856static void
17a2f251 15857s_change_sec (int sec)
252b5132
RH
15858{
15859 segT seg;
15860
252b5132
RH
15861#ifdef OBJ_ELF
15862 /* The ELF backend needs to know that we are changing sections, so
15863 that .previous works correctly. We could do something like check
b6ff326e 15864 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
15865 as it would not be appropriate to use it in the section changing
15866 functions in read.c, since obj-elf.c intercepts those. FIXME:
15867 This should be cleaner, somehow. */
f43abd2b
TS
15868 if (IS_ELF)
15869 obj_elf_section_change_hook ();
252b5132
RH
15870#endif
15871
7d10b47d 15872 mips_emit_delays ();
6a32d874 15873
252b5132
RH
15874 switch (sec)
15875 {
15876 case 't':
15877 s_text (0);
15878 break;
15879 case 'd':
15880 s_data (0);
15881 break;
15882 case 'b':
15883 subseg_set (bss_section, (subsegT) get_absolute_expression ());
15884 demand_empty_rest_of_line ();
15885 break;
15886
15887 case 'r':
4d0d148d
TS
15888 seg = subseg_new (RDATA_SECTION_NAME,
15889 (subsegT) get_absolute_expression ());
f43abd2b 15890 if (IS_ELF)
252b5132 15891 {
4d0d148d
TS
15892 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
15893 | SEC_READONLY | SEC_RELOC
15894 | SEC_DATA));
c41e87e3 15895 if (strncmp (TARGET_OS, "elf", 3) != 0)
4d0d148d 15896 record_alignment (seg, 4);
252b5132 15897 }
4d0d148d 15898 demand_empty_rest_of_line ();
252b5132
RH
15899 break;
15900
15901 case 's':
4d0d148d 15902 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
f43abd2b 15903 if (IS_ELF)
252b5132 15904 {
4d0d148d
TS
15905 bfd_set_section_flags (stdoutput, seg,
15906 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
c41e87e3 15907 if (strncmp (TARGET_OS, "elf", 3) != 0)
4d0d148d 15908 record_alignment (seg, 4);
252b5132 15909 }
4d0d148d
TS
15910 demand_empty_rest_of_line ();
15911 break;
998b3c36
MR
15912
15913 case 'B':
15914 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
15915 if (IS_ELF)
15916 {
15917 bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
15918 if (strncmp (TARGET_OS, "elf", 3) != 0)
15919 record_alignment (seg, 4);
15920 }
15921 demand_empty_rest_of_line ();
15922 break;
252b5132
RH
15923 }
15924
15925 auto_align = 1;
15926}
b34976b6 15927
cca86cc8 15928void
17a2f251 15929s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 15930{
7ed4a06a 15931#ifdef OBJ_ELF
cca86cc8
SC
15932 char *section_name;
15933 char c;
684022ea 15934 char next_c = 0;
cca86cc8
SC
15935 int section_type;
15936 int section_flag;
15937 int section_entry_size;
15938 int section_alignment;
b34976b6 15939
f43abd2b 15940 if (!IS_ELF)
7ed4a06a
TS
15941 return;
15942
cca86cc8
SC
15943 section_name = input_line_pointer;
15944 c = get_symbol_end ();
a816d1ed
AO
15945 if (c)
15946 next_c = *(input_line_pointer + 1);
cca86cc8 15947
4cf0dd0d
TS
15948 /* Do we have .section Name<,"flags">? */
15949 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 15950 {
4cf0dd0d
TS
15951 /* just after name is now '\0'. */
15952 *input_line_pointer = c;
cca86cc8
SC
15953 input_line_pointer = section_name;
15954 obj_elf_section (ignore);
15955 return;
15956 }
15957 input_line_pointer++;
15958
15959 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
15960 if (c == ',')
15961 section_type = get_absolute_expression ();
15962 else
15963 section_type = 0;
15964 if (*input_line_pointer++ == ',')
15965 section_flag = get_absolute_expression ();
15966 else
15967 section_flag = 0;
15968 if (*input_line_pointer++ == ',')
15969 section_entry_size = get_absolute_expression ();
15970 else
15971 section_entry_size = 0;
15972 if (*input_line_pointer++ == ',')
15973 section_alignment = get_absolute_expression ();
15974 else
15975 section_alignment = 0;
87975d2a
AM
15976 /* FIXME: really ignore? */
15977 (void) section_alignment;
cca86cc8 15978
a816d1ed
AO
15979 section_name = xstrdup (section_name);
15980
8ab8a5c8
RS
15981 /* When using the generic form of .section (as implemented by obj-elf.c),
15982 there's no way to set the section type to SHT_MIPS_DWARF. Users have
15983 traditionally had to fall back on the more common @progbits instead.
15984
15985 There's nothing really harmful in this, since bfd will correct
15986 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
708587a4 15987 means that, for backwards compatibility, the special_section entries
8ab8a5c8
RS
15988 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
15989
15990 Even so, we shouldn't force users of the MIPS .section syntax to
15991 incorrectly label the sections as SHT_PROGBITS. The best compromise
15992 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
15993 generic type-checking code. */
15994 if (section_type == SHT_MIPS_DWARF)
15995 section_type = SHT_PROGBITS;
15996
cca86cc8
SC
15997 obj_elf_change_section (section_name, section_type, section_flag,
15998 section_entry_size, 0, 0, 0);
a816d1ed
AO
15999
16000 if (now_seg->name != section_name)
16001 free (section_name);
7ed4a06a 16002#endif /* OBJ_ELF */
cca86cc8 16003}
252b5132
RH
16004
16005void
17a2f251 16006mips_enable_auto_align (void)
252b5132
RH
16007{
16008 auto_align = 1;
16009}
16010
16011static void
17a2f251 16012s_cons (int log_size)
252b5132 16013{
a8dbcb85
TS
16014 segment_info_type *si = seg_info (now_seg);
16015 struct insn_label_list *l = si->label_list;
252b5132 16016
7d10b47d 16017 mips_emit_delays ();
252b5132 16018 if (log_size > 0 && auto_align)
462427c4 16019 mips_align (log_size, 0, l);
252b5132 16020 cons (1 << log_size);
a1facbec 16021 mips_clear_insn_labels ();
252b5132
RH
16022}
16023
16024static void
17a2f251 16025s_float_cons (int type)
252b5132 16026{
a8dbcb85
TS
16027 segment_info_type *si = seg_info (now_seg);
16028 struct insn_label_list *l = si->label_list;
252b5132 16029
7d10b47d 16030 mips_emit_delays ();
252b5132
RH
16031
16032 if (auto_align)
49309057
ILT
16033 {
16034 if (type == 'd')
462427c4 16035 mips_align (3, 0, l);
49309057 16036 else
462427c4 16037 mips_align (2, 0, l);
49309057 16038 }
252b5132 16039
252b5132 16040 float_cons (type);
a1facbec 16041 mips_clear_insn_labels ();
252b5132
RH
16042}
16043
16044/* Handle .globl. We need to override it because on Irix 5 you are
16045 permitted to say
16046 .globl foo .text
16047 where foo is an undefined symbol, to mean that foo should be
16048 considered to be the address of a function. */
16049
16050static void
17a2f251 16051s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
16052{
16053 char *name;
16054 int c;
16055 symbolS *symbolP;
16056 flagword flag;
16057
8a06b769 16058 do
252b5132 16059 {
8a06b769 16060 name = input_line_pointer;
252b5132 16061 c = get_symbol_end ();
8a06b769
TS
16062 symbolP = symbol_find_or_make (name);
16063 S_SET_EXTERNAL (symbolP);
16064
252b5132 16065 *input_line_pointer = c;
8a06b769 16066 SKIP_WHITESPACE ();
252b5132 16067
8a06b769
TS
16068 /* On Irix 5, every global symbol that is not explicitly labelled as
16069 being a function is apparently labelled as being an object. */
16070 flag = BSF_OBJECT;
252b5132 16071
8a06b769
TS
16072 if (!is_end_of_line[(unsigned char) *input_line_pointer]
16073 && (*input_line_pointer != ','))
16074 {
16075 char *secname;
16076 asection *sec;
16077
16078 secname = input_line_pointer;
16079 c = get_symbol_end ();
16080 sec = bfd_get_section_by_name (stdoutput, secname);
16081 if (sec == NULL)
16082 as_bad (_("%s: no such section"), secname);
16083 *input_line_pointer = c;
16084
16085 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
16086 flag = BSF_FUNCTION;
16087 }
16088
16089 symbol_get_bfdsym (symbolP)->flags |= flag;
16090
16091 c = *input_line_pointer;
16092 if (c == ',')
16093 {
16094 input_line_pointer++;
16095 SKIP_WHITESPACE ();
16096 if (is_end_of_line[(unsigned char) *input_line_pointer])
16097 c = '\n';
16098 }
16099 }
16100 while (c == ',');
252b5132 16101
252b5132
RH
16102 demand_empty_rest_of_line ();
16103}
16104
16105static void
17a2f251 16106s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
16107{
16108 char *opt;
16109 char c;
16110
16111 opt = input_line_pointer;
16112 c = get_symbol_end ();
16113
16114 if (*opt == 'O')
16115 {
16116 /* FIXME: What does this mean? */
16117 }
16118 else if (strncmp (opt, "pic", 3) == 0)
16119 {
16120 int i;
16121
16122 i = atoi (opt + 3);
16123 if (i == 0)
16124 mips_pic = NO_PIC;
16125 else if (i == 2)
143d77c5 16126 {
8b828383 16127 mips_pic = SVR4_PIC;
143d77c5
EC
16128 mips_abicalls = TRUE;
16129 }
252b5132
RH
16130 else
16131 as_bad (_(".option pic%d not supported"), i);
16132
4d0d148d 16133 if (mips_pic == SVR4_PIC)
252b5132
RH
16134 {
16135 if (g_switch_seen && g_switch_value != 0)
16136 as_warn (_("-G may not be used with SVR4 PIC code"));
16137 g_switch_value = 0;
16138 bfd_set_gp_size (stdoutput, 0);
16139 }
16140 }
16141 else
16142 as_warn (_("Unrecognized option \"%s\""), opt);
16143
16144 *input_line_pointer = c;
16145 demand_empty_rest_of_line ();
16146}
16147
16148/* This structure is used to hold a stack of .set values. */
16149
e972090a
NC
16150struct mips_option_stack
16151{
252b5132
RH
16152 struct mips_option_stack *next;
16153 struct mips_set_options options;
16154};
16155
16156static struct mips_option_stack *mips_opts_stack;
16157
16158/* Handle the .set pseudo-op. */
16159
16160static void
17a2f251 16161s_mipsset (int x ATTRIBUTE_UNUSED)
252b5132
RH
16162{
16163 char *name = input_line_pointer, ch;
16164
16165 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 16166 ++input_line_pointer;
252b5132
RH
16167 ch = *input_line_pointer;
16168 *input_line_pointer = '\0';
16169
16170 if (strcmp (name, "reorder") == 0)
16171 {
7d10b47d
RS
16172 if (mips_opts.noreorder)
16173 end_noreorder ();
252b5132
RH
16174 }
16175 else if (strcmp (name, "noreorder") == 0)
16176 {
7d10b47d
RS
16177 if (!mips_opts.noreorder)
16178 start_noreorder ();
252b5132 16179 }
741fe287
MR
16180 else if (strncmp (name, "at=", 3) == 0)
16181 {
16182 char *s = name + 3;
16183
16184 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
16185 as_bad (_("Unrecognized register name `%s'"), s);
16186 }
252b5132
RH
16187 else if (strcmp (name, "at") == 0)
16188 {
741fe287 16189 mips_opts.at = ATREG;
252b5132
RH
16190 }
16191 else if (strcmp (name, "noat") == 0)
16192 {
741fe287 16193 mips_opts.at = ZERO;
252b5132
RH
16194 }
16195 else if (strcmp (name, "macro") == 0)
16196 {
16197 mips_opts.warn_about_macros = 0;
16198 }
16199 else if (strcmp (name, "nomacro") == 0)
16200 {
16201 if (mips_opts.noreorder == 0)
16202 as_bad (_("`noreorder' must be set before `nomacro'"));
16203 mips_opts.warn_about_macros = 1;
16204 }
16205 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
16206 {
16207 mips_opts.nomove = 0;
16208 }
16209 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
16210 {
16211 mips_opts.nomove = 1;
16212 }
16213 else if (strcmp (name, "bopt") == 0)
16214 {
16215 mips_opts.nobopt = 0;
16216 }
16217 else if (strcmp (name, "nobopt") == 0)
16218 {
16219 mips_opts.nobopt = 1;
16220 }
ad3fea08
TS
16221 else if (strcmp (name, "gp=default") == 0)
16222 mips_opts.gp32 = file_mips_gp32;
16223 else if (strcmp (name, "gp=32") == 0)
16224 mips_opts.gp32 = 1;
16225 else if (strcmp (name, "gp=64") == 0)
16226 {
16227 if (!ISA_HAS_64BIT_REGS (mips_opts.isa))
20203fb9 16228 as_warn (_("%s isa does not support 64-bit registers"),
ad3fea08
TS
16229 mips_cpu_info_from_isa (mips_opts.isa)->name);
16230 mips_opts.gp32 = 0;
16231 }
16232 else if (strcmp (name, "fp=default") == 0)
16233 mips_opts.fp32 = file_mips_fp32;
16234 else if (strcmp (name, "fp=32") == 0)
16235 mips_opts.fp32 = 1;
16236 else if (strcmp (name, "fp=64") == 0)
16237 {
16238 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
20203fb9 16239 as_warn (_("%s isa does not support 64-bit floating point registers"),
ad3fea08
TS
16240 mips_cpu_info_from_isa (mips_opts.isa)->name);
16241 mips_opts.fp32 = 0;
16242 }
037b32b9
AN
16243 else if (strcmp (name, "softfloat") == 0)
16244 mips_opts.soft_float = 1;
16245 else if (strcmp (name, "hardfloat") == 0)
16246 mips_opts.soft_float = 0;
16247 else if (strcmp (name, "singlefloat") == 0)
16248 mips_opts.single_float = 1;
16249 else if (strcmp (name, "doublefloat") == 0)
16250 mips_opts.single_float = 0;
252b5132
RH
16251 else if (strcmp (name, "mips16") == 0
16252 || strcmp (name, "MIPS-16") == 0)
df58fc94
RS
16253 {
16254 if (mips_opts.micromips == 1)
16255 as_fatal (_("`mips16' cannot be used with `micromips'"));
16256 mips_opts.mips16 = 1;
16257 }
252b5132
RH
16258 else if (strcmp (name, "nomips16") == 0
16259 || strcmp (name, "noMIPS-16") == 0)
16260 mips_opts.mips16 = 0;
df58fc94
RS
16261 else if (strcmp (name, "micromips") == 0)
16262 {
16263 if (mips_opts.mips16 == 1)
16264 as_fatal (_("`micromips' cannot be used with `mips16'"));
16265 mips_opts.micromips = 1;
16266 }
16267 else if (strcmp (name, "nomicromips") == 0)
16268 mips_opts.micromips = 0;
e16bfa71
TS
16269 else if (strcmp (name, "smartmips") == 0)
16270 {
ad3fea08 16271 if (!ISA_SUPPORTS_SMARTMIPS)
20203fb9 16272 as_warn (_("%s ISA does not support SmartMIPS ASE"),
e16bfa71
TS
16273 mips_cpu_info_from_isa (mips_opts.isa)->name);
16274 mips_opts.ase_smartmips = 1;
16275 }
16276 else if (strcmp (name, "nosmartmips") == 0)
16277 mips_opts.ase_smartmips = 0;
1f25f5d3
CD
16278 else if (strcmp (name, "mips3d") == 0)
16279 mips_opts.ase_mips3d = 1;
16280 else if (strcmp (name, "nomips3d") == 0)
16281 mips_opts.ase_mips3d = 0;
a4672219
TS
16282 else if (strcmp (name, "mdmx") == 0)
16283 mips_opts.ase_mdmx = 1;
16284 else if (strcmp (name, "nomdmx") == 0)
16285 mips_opts.ase_mdmx = 0;
74cd071d 16286 else if (strcmp (name, "dsp") == 0)
ad3fea08
TS
16287 {
16288 if (!ISA_SUPPORTS_DSP_ASE)
20203fb9 16289 as_warn (_("%s ISA does not support DSP ASE"),
ad3fea08
TS
16290 mips_cpu_info_from_isa (mips_opts.isa)->name);
16291 mips_opts.ase_dsp = 1;
8b082fb1 16292 mips_opts.ase_dspr2 = 0;
ad3fea08 16293 }
74cd071d 16294 else if (strcmp (name, "nodsp") == 0)
8b082fb1
TS
16295 {
16296 mips_opts.ase_dsp = 0;
16297 mips_opts.ase_dspr2 = 0;
16298 }
16299 else if (strcmp (name, "dspr2") == 0)
16300 {
16301 if (!ISA_SUPPORTS_DSPR2_ASE)
20203fb9 16302 as_warn (_("%s ISA does not support DSP R2 ASE"),
8b082fb1
TS
16303 mips_cpu_info_from_isa (mips_opts.isa)->name);
16304 mips_opts.ase_dspr2 = 1;
16305 mips_opts.ase_dsp = 1;
16306 }
16307 else if (strcmp (name, "nodspr2") == 0)
16308 {
16309 mips_opts.ase_dspr2 = 0;
16310 mips_opts.ase_dsp = 0;
16311 }
ef2e4d86 16312 else if (strcmp (name, "mt") == 0)
ad3fea08
TS
16313 {
16314 if (!ISA_SUPPORTS_MT_ASE)
20203fb9 16315 as_warn (_("%s ISA does not support MT ASE"),
ad3fea08
TS
16316 mips_cpu_info_from_isa (mips_opts.isa)->name);
16317 mips_opts.ase_mt = 1;
16318 }
ef2e4d86
CF
16319 else if (strcmp (name, "nomt") == 0)
16320 mips_opts.ase_mt = 0;
dec0624d
MR
16321 else if (strcmp (name, "mcu") == 0)
16322 mips_opts.ase_mcu = 1;
16323 else if (strcmp (name, "nomcu") == 0)
16324 mips_opts.ase_mcu = 0;
1a2c1fad 16325 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 16326 {
af7ee8bf 16327 int reset = 0;
252b5132 16328
1a2c1fad
CD
16329 /* Permit the user to change the ISA and architecture on the fly.
16330 Needless to say, misuse can cause serious problems. */
81a21e38 16331 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
af7ee8bf
CD
16332 {
16333 reset = 1;
16334 mips_opts.isa = file_mips_isa;
1a2c1fad 16335 mips_opts.arch = file_mips_arch;
1a2c1fad
CD
16336 }
16337 else if (strncmp (name, "arch=", 5) == 0)
16338 {
16339 const struct mips_cpu_info *p;
16340
16341 p = mips_parse_cpu("internal use", name + 5);
16342 if (!p)
16343 as_bad (_("unknown architecture %s"), name + 5);
16344 else
16345 {
16346 mips_opts.arch = p->cpu;
16347 mips_opts.isa = p->isa;
16348 }
16349 }
81a21e38
TS
16350 else if (strncmp (name, "mips", 4) == 0)
16351 {
16352 const struct mips_cpu_info *p;
16353
16354 p = mips_parse_cpu("internal use", name);
16355 if (!p)
16356 as_bad (_("unknown ISA level %s"), name + 4);
16357 else
16358 {
16359 mips_opts.arch = p->cpu;
16360 mips_opts.isa = p->isa;
16361 }
16362 }
af7ee8bf 16363 else
81a21e38 16364 as_bad (_("unknown ISA or architecture %s"), name);
af7ee8bf
CD
16365
16366 switch (mips_opts.isa)
98d3f06f
KH
16367 {
16368 case 0:
98d3f06f 16369 break;
af7ee8bf
CD
16370 case ISA_MIPS1:
16371 case ISA_MIPS2:
16372 case ISA_MIPS32:
16373 case ISA_MIPS32R2:
98d3f06f
KH
16374 mips_opts.gp32 = 1;
16375 mips_opts.fp32 = 1;
16376 break;
af7ee8bf
CD
16377 case ISA_MIPS3:
16378 case ISA_MIPS4:
16379 case ISA_MIPS5:
16380 case ISA_MIPS64:
5f74bc13 16381 case ISA_MIPS64R2:
98d3f06f
KH
16382 mips_opts.gp32 = 0;
16383 mips_opts.fp32 = 0;
16384 break;
16385 default:
16386 as_bad (_("unknown ISA level %s"), name + 4);
16387 break;
16388 }
af7ee8bf 16389 if (reset)
98d3f06f 16390 {
af7ee8bf
CD
16391 mips_opts.gp32 = file_mips_gp32;
16392 mips_opts.fp32 = file_mips_fp32;
98d3f06f 16393 }
252b5132
RH
16394 }
16395 else if (strcmp (name, "autoextend") == 0)
16396 mips_opts.noautoextend = 0;
16397 else if (strcmp (name, "noautoextend") == 0)
16398 mips_opts.noautoextend = 1;
16399 else if (strcmp (name, "push") == 0)
16400 {
16401 struct mips_option_stack *s;
16402
16403 s = (struct mips_option_stack *) xmalloc (sizeof *s);
16404 s->next = mips_opts_stack;
16405 s->options = mips_opts;
16406 mips_opts_stack = s;
16407 }
16408 else if (strcmp (name, "pop") == 0)
16409 {
16410 struct mips_option_stack *s;
16411
16412 s = mips_opts_stack;
16413 if (s == NULL)
16414 as_bad (_(".set pop with no .set push"));
16415 else
16416 {
16417 /* If we're changing the reorder mode we need to handle
16418 delay slots correctly. */
16419 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 16420 start_noreorder ();
252b5132 16421 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 16422 end_noreorder ();
252b5132
RH
16423
16424 mips_opts = s->options;
16425 mips_opts_stack = s->next;
16426 free (s);
16427 }
16428 }
aed1a261
RS
16429 else if (strcmp (name, "sym32") == 0)
16430 mips_opts.sym32 = TRUE;
16431 else if (strcmp (name, "nosym32") == 0)
16432 mips_opts.sym32 = FALSE;
e6559e01
JM
16433 else if (strchr (name, ','))
16434 {
16435 /* Generic ".set" directive; use the generic handler. */
16436 *input_line_pointer = ch;
16437 input_line_pointer = name;
16438 s_set (0);
16439 return;
16440 }
252b5132
RH
16441 else
16442 {
16443 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
16444 }
16445 *input_line_pointer = ch;
16446 demand_empty_rest_of_line ();
16447}
16448
16449/* Handle the .abicalls pseudo-op. I believe this is equivalent to
16450 .option pic2. It means to generate SVR4 PIC calls. */
16451
16452static void
17a2f251 16453s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16454{
16455 mips_pic = SVR4_PIC;
143d77c5 16456 mips_abicalls = TRUE;
4d0d148d
TS
16457
16458 if (g_switch_seen && g_switch_value != 0)
16459 as_warn (_("-G may not be used with SVR4 PIC code"));
16460 g_switch_value = 0;
16461
252b5132
RH
16462 bfd_set_gp_size (stdoutput, 0);
16463 demand_empty_rest_of_line ();
16464}
16465
16466/* Handle the .cpload pseudo-op. This is used when generating SVR4
16467 PIC code. It sets the $gp register for the function based on the
16468 function address, which is in the register named in the argument.
16469 This uses a relocation against _gp_disp, which is handled specially
16470 by the linker. The result is:
16471 lui $gp,%hi(_gp_disp)
16472 addiu $gp,$gp,%lo(_gp_disp)
16473 addu $gp,$gp,.cpload argument
aa6975fb
ILT
16474 The .cpload argument is normally $25 == $t9.
16475
16476 The -mno-shared option changes this to:
bbe506e8
TS
16477 lui $gp,%hi(__gnu_local_gp)
16478 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
16479 and the argument is ignored. This saves an instruction, but the
16480 resulting code is not position independent; it uses an absolute
bbe506e8
TS
16481 address for __gnu_local_gp. Thus code assembled with -mno-shared
16482 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
16483
16484static void
17a2f251 16485s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16486{
16487 expressionS ex;
aa6975fb
ILT
16488 int reg;
16489 int in_shared;
252b5132 16490
6478892d
TS
16491 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16492 .cpload is ignored. */
16493 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16494 {
16495 s_ignore (0);
16496 return;
16497 }
16498
a276b80c
MR
16499 if (mips_opts.mips16)
16500 {
16501 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16502 ignore_rest_of_line ();
16503 return;
16504 }
16505
d3ecfc59 16506 /* .cpload should be in a .set noreorder section. */
252b5132
RH
16507 if (mips_opts.noreorder == 0)
16508 as_warn (_(".cpload not in noreorder section"));
16509
aa6975fb
ILT
16510 reg = tc_get_register (0);
16511
16512 /* If we need to produce a 64-bit address, we are better off using
16513 the default instruction sequence. */
aed1a261 16514 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 16515
252b5132 16516 ex.X_op = O_symbol;
bbe506e8
TS
16517 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16518 "__gnu_local_gp");
252b5132
RH
16519 ex.X_op_symbol = NULL;
16520 ex.X_add_number = 0;
16521
16522 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 16523 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 16524
584892a6 16525 macro_start ();
67c0d1eb
RS
16526 macro_build_lui (&ex, mips_gp_register);
16527 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 16528 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
16529 if (in_shared)
16530 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16531 mips_gp_register, reg);
584892a6 16532 macro_end ();
252b5132
RH
16533
16534 demand_empty_rest_of_line ();
16535}
16536
6478892d
TS
16537/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
16538 .cpsetup $reg1, offset|$reg2, label
16539
16540 If offset is given, this results in:
16541 sd $gp, offset($sp)
956cd1d6 16542 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
16543 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16544 daddu $gp, $gp, $reg1
6478892d
TS
16545
16546 If $reg2 is given, this results in:
16547 daddu $reg2, $gp, $0
956cd1d6 16548 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
16549 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16550 daddu $gp, $gp, $reg1
aa6975fb
ILT
16551 $reg1 is normally $25 == $t9.
16552
16553 The -mno-shared option replaces the last three instructions with
16554 lui $gp,%hi(_gp)
54f4ddb3 16555 addiu $gp,$gp,%lo(_gp) */
aa6975fb 16556
6478892d 16557static void
17a2f251 16558s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16559{
16560 expressionS ex_off;
16561 expressionS ex_sym;
16562 int reg1;
6478892d 16563
8586fc66 16564 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
16565 We also need NewABI support. */
16566 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16567 {
16568 s_ignore (0);
16569 return;
16570 }
16571
a276b80c
MR
16572 if (mips_opts.mips16)
16573 {
16574 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
16575 ignore_rest_of_line ();
16576 return;
16577 }
16578
6478892d
TS
16579 reg1 = tc_get_register (0);
16580 SKIP_WHITESPACE ();
16581 if (*input_line_pointer != ',')
16582 {
16583 as_bad (_("missing argument separator ',' for .cpsetup"));
16584 return;
16585 }
16586 else
80245285 16587 ++input_line_pointer;
6478892d
TS
16588 SKIP_WHITESPACE ();
16589 if (*input_line_pointer == '$')
80245285
TS
16590 {
16591 mips_cpreturn_register = tc_get_register (0);
16592 mips_cpreturn_offset = -1;
16593 }
6478892d 16594 else
80245285
TS
16595 {
16596 mips_cpreturn_offset = get_absolute_expression ();
16597 mips_cpreturn_register = -1;
16598 }
6478892d
TS
16599 SKIP_WHITESPACE ();
16600 if (*input_line_pointer != ',')
16601 {
16602 as_bad (_("missing argument separator ',' for .cpsetup"));
16603 return;
16604 }
16605 else
f9419b05 16606 ++input_line_pointer;
6478892d 16607 SKIP_WHITESPACE ();
f21f8242 16608 expression (&ex_sym);
6478892d 16609
584892a6 16610 macro_start ();
6478892d
TS
16611 if (mips_cpreturn_register == -1)
16612 {
16613 ex_off.X_op = O_constant;
16614 ex_off.X_add_symbol = NULL;
16615 ex_off.X_op_symbol = NULL;
16616 ex_off.X_add_number = mips_cpreturn_offset;
16617
67c0d1eb 16618 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 16619 BFD_RELOC_LO16, SP);
6478892d
TS
16620 }
16621 else
67c0d1eb 16622 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
17a2f251 16623 mips_gp_register, 0);
6478892d 16624
aed1a261 16625 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb 16626 {
df58fc94 16627 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
aa6975fb
ILT
16628 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
16629 BFD_RELOC_HI16_S);
16630
16631 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
16632 mips_gp_register, -1, BFD_RELOC_GPREL16,
16633 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
16634
16635 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
16636 mips_gp_register, reg1);
16637 }
16638 else
16639 {
16640 expressionS ex;
16641
16642 ex.X_op = O_symbol;
4184909a 16643 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
16644 ex.X_op_symbol = NULL;
16645 ex.X_add_number = 0;
6e1304d8 16646
aa6975fb
ILT
16647 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16648 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16649
16650 macro_build_lui (&ex, mips_gp_register);
16651 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16652 mips_gp_register, BFD_RELOC_LO16);
16653 }
f21f8242 16654
584892a6 16655 macro_end ();
6478892d
TS
16656
16657 demand_empty_rest_of_line ();
16658}
16659
16660static void
17a2f251 16661s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16662{
16663 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
54f4ddb3 16664 .cplocal is ignored. */
6478892d
TS
16665 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16666 {
16667 s_ignore (0);
16668 return;
16669 }
16670
a276b80c
MR
16671 if (mips_opts.mips16)
16672 {
16673 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
16674 ignore_rest_of_line ();
16675 return;
16676 }
16677
6478892d 16678 mips_gp_register = tc_get_register (0);
85b51719 16679 demand_empty_rest_of_line ();
6478892d
TS
16680}
16681
252b5132
RH
16682/* Handle the .cprestore pseudo-op. This stores $gp into a given
16683 offset from $sp. The offset is remembered, and after making a PIC
16684 call $gp is restored from that location. */
16685
16686static void
17a2f251 16687s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16688{
16689 expressionS ex;
252b5132 16690
6478892d 16691 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 16692 .cprestore is ignored. */
6478892d 16693 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16694 {
16695 s_ignore (0);
16696 return;
16697 }
16698
a276b80c
MR
16699 if (mips_opts.mips16)
16700 {
16701 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
16702 ignore_rest_of_line ();
16703 return;
16704 }
16705
252b5132 16706 mips_cprestore_offset = get_absolute_expression ();
7a621144 16707 mips_cprestore_valid = 1;
252b5132
RH
16708
16709 ex.X_op = O_constant;
16710 ex.X_add_symbol = NULL;
16711 ex.X_op_symbol = NULL;
16712 ex.X_add_number = mips_cprestore_offset;
16713
584892a6 16714 macro_start ();
67c0d1eb
RS
16715 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
16716 SP, HAVE_64BIT_ADDRESSES);
584892a6 16717 macro_end ();
252b5132
RH
16718
16719 demand_empty_rest_of_line ();
16720}
16721
6478892d 16722/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 16723 was given in the preceding .cpsetup, it results in:
6478892d 16724 ld $gp, offset($sp)
76b3015f 16725
6478892d 16726 If a register $reg2 was given there, it results in:
54f4ddb3
TS
16727 daddu $gp, $reg2, $0 */
16728
6478892d 16729static void
17a2f251 16730s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16731{
16732 expressionS ex;
6478892d
TS
16733
16734 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
16735 We also need NewABI support. */
16736 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16737 {
16738 s_ignore (0);
16739 return;
16740 }
16741
a276b80c
MR
16742 if (mips_opts.mips16)
16743 {
16744 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
16745 ignore_rest_of_line ();
16746 return;
16747 }
16748
584892a6 16749 macro_start ();
6478892d
TS
16750 if (mips_cpreturn_register == -1)
16751 {
16752 ex.X_op = O_constant;
16753 ex.X_add_symbol = NULL;
16754 ex.X_op_symbol = NULL;
16755 ex.X_add_number = mips_cpreturn_offset;
16756
67c0d1eb 16757 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
16758 }
16759 else
67c0d1eb 16760 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
17a2f251 16761 mips_cpreturn_register, 0);
584892a6 16762 macro_end ();
6478892d
TS
16763
16764 demand_empty_rest_of_line ();
16765}
16766
d0f13682
CLT
16767/* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
16768 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
16769 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
16770 debug information or MIPS16 TLS. */
741d6ea8
JM
16771
16772static void
d0f13682
CLT
16773s_tls_rel_directive (const size_t bytes, const char *dirstr,
16774 bfd_reloc_code_real_type rtype)
741d6ea8
JM
16775{
16776 expressionS ex;
16777 char *p;
16778
16779 expression (&ex);
16780
16781 if (ex.X_op != O_symbol)
16782 {
d0f13682 16783 as_bad (_("Unsupported use of %s"), dirstr);
741d6ea8
JM
16784 ignore_rest_of_line ();
16785 }
16786
16787 p = frag_more (bytes);
16788 md_number_to_chars (p, 0, bytes);
d0f13682 16789 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
741d6ea8 16790 demand_empty_rest_of_line ();
de64cffd 16791 mips_clear_insn_labels ();
741d6ea8
JM
16792}
16793
16794/* Handle .dtprelword. */
16795
16796static void
16797s_dtprelword (int ignore ATTRIBUTE_UNUSED)
16798{
d0f13682 16799 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
741d6ea8
JM
16800}
16801
16802/* Handle .dtpreldword. */
16803
16804static void
16805s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
16806{
d0f13682
CLT
16807 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
16808}
16809
16810/* Handle .tprelword. */
16811
16812static void
16813s_tprelword (int ignore ATTRIBUTE_UNUSED)
16814{
16815 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
16816}
16817
16818/* Handle .tpreldword. */
16819
16820static void
16821s_tpreldword (int ignore ATTRIBUTE_UNUSED)
16822{
16823 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
741d6ea8
JM
16824}
16825
6478892d
TS
16826/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
16827 code. It sets the offset to use in gp_rel relocations. */
16828
16829static void
17a2f251 16830s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16831{
16832 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
16833 We also need NewABI support. */
16834 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16835 {
16836 s_ignore (0);
16837 return;
16838 }
16839
def2e0dd 16840 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
16841
16842 demand_empty_rest_of_line ();
16843}
16844
252b5132
RH
16845/* Handle the .gpword pseudo-op. This is used when generating PIC
16846 code. It generates a 32 bit GP relative reloc. */
16847
16848static void
17a2f251 16849s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132 16850{
a8dbcb85
TS
16851 segment_info_type *si;
16852 struct insn_label_list *l;
252b5132
RH
16853 expressionS ex;
16854 char *p;
16855
16856 /* When not generating PIC code, this is treated as .word. */
16857 if (mips_pic != SVR4_PIC)
16858 {
16859 s_cons (2);
16860 return;
16861 }
16862
a8dbcb85
TS
16863 si = seg_info (now_seg);
16864 l = si->label_list;
7d10b47d 16865 mips_emit_delays ();
252b5132 16866 if (auto_align)
462427c4 16867 mips_align (2, 0, l);
252b5132
RH
16868
16869 expression (&ex);
a1facbec 16870 mips_clear_insn_labels ();
252b5132
RH
16871
16872 if (ex.X_op != O_symbol || ex.X_add_number != 0)
16873 {
16874 as_bad (_("Unsupported use of .gpword"));
16875 ignore_rest_of_line ();
16876 }
16877
16878 p = frag_more (4);
17a2f251 16879 md_number_to_chars (p, 0, 4);
b34976b6 16880 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 16881 BFD_RELOC_GPREL32);
252b5132
RH
16882
16883 demand_empty_rest_of_line ();
16884}
16885
10181a0d 16886static void
17a2f251 16887s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d 16888{
a8dbcb85
TS
16889 segment_info_type *si;
16890 struct insn_label_list *l;
10181a0d
AO
16891 expressionS ex;
16892 char *p;
16893
16894 /* When not generating PIC code, this is treated as .dword. */
16895 if (mips_pic != SVR4_PIC)
16896 {
16897 s_cons (3);
16898 return;
16899 }
16900
a8dbcb85
TS
16901 si = seg_info (now_seg);
16902 l = si->label_list;
7d10b47d 16903 mips_emit_delays ();
10181a0d 16904 if (auto_align)
462427c4 16905 mips_align (3, 0, l);
10181a0d
AO
16906
16907 expression (&ex);
a1facbec 16908 mips_clear_insn_labels ();
10181a0d
AO
16909
16910 if (ex.X_op != O_symbol || ex.X_add_number != 0)
16911 {
16912 as_bad (_("Unsupported use of .gpdword"));
16913 ignore_rest_of_line ();
16914 }
16915
16916 p = frag_more (8);
17a2f251 16917 md_number_to_chars (p, 0, 8);
a105a300 16918 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
6e1304d8 16919 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
16920
16921 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8
RS
16922 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
16923 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
16924
16925 demand_empty_rest_of_line ();
16926}
16927
252b5132
RH
16928/* Handle the .cpadd pseudo-op. This is used when dealing with switch
16929 tables in SVR4 PIC code. */
16930
16931static void
17a2f251 16932s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 16933{
252b5132
RH
16934 int reg;
16935
10181a0d
AO
16936 /* This is ignored when not generating SVR4 PIC code. */
16937 if (mips_pic != SVR4_PIC)
252b5132
RH
16938 {
16939 s_ignore (0);
16940 return;
16941 }
16942
16943 /* Add $gp to the register named as an argument. */
584892a6 16944 macro_start ();
252b5132 16945 reg = tc_get_register (0);
67c0d1eb 16946 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 16947 macro_end ();
252b5132 16948
bdaaa2e1 16949 demand_empty_rest_of_line ();
252b5132
RH
16950}
16951
16952/* Handle the .insn pseudo-op. This marks instruction labels in
df58fc94 16953 mips16/micromips mode. This permits the linker to handle them specially,
252b5132
RH
16954 such as generating jalx instructions when needed. We also make
16955 them odd for the duration of the assembly, in order to generate the
16956 right sort of code. We will make them even in the adjust_symtab
16957 routine, while leaving them marked. This is convenient for the
16958 debugger and the disassembler. The linker knows to make them odd
16959 again. */
16960
16961static void
17a2f251 16962s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 16963{
df58fc94 16964 mips_mark_labels ();
252b5132
RH
16965
16966 demand_empty_rest_of_line ();
16967}
16968
16969/* Handle a .stabn directive. We need these in order to mark a label
16970 as being a mips16 text label correctly. Sometimes the compiler
16971 will emit a label, followed by a .stabn, and then switch sections.
16972 If the label and .stabn are in mips16 mode, then the label is
16973 really a mips16 text label. */
16974
16975static void
17a2f251 16976s_mips_stab (int type)
252b5132 16977{
f9419b05 16978 if (type == 'n')
df58fc94 16979 mips_mark_labels ();
252b5132
RH
16980
16981 s_stab (type);
16982}
16983
54f4ddb3 16984/* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
252b5132
RH
16985
16986static void
17a2f251 16987s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16988{
16989 char *name;
16990 int c;
16991 symbolS *symbolP;
16992 expressionS exp;
16993
16994 name = input_line_pointer;
16995 c = get_symbol_end ();
16996 symbolP = symbol_find_or_make (name);
16997 S_SET_WEAK (symbolP);
16998 *input_line_pointer = c;
16999
17000 SKIP_WHITESPACE ();
17001
17002 if (! is_end_of_line[(unsigned char) *input_line_pointer])
17003 {
17004 if (S_IS_DEFINED (symbolP))
17005 {
20203fb9 17006 as_bad (_("ignoring attempt to redefine symbol %s"),
252b5132
RH
17007 S_GET_NAME (symbolP));
17008 ignore_rest_of_line ();
17009 return;
17010 }
bdaaa2e1 17011
252b5132
RH
17012 if (*input_line_pointer == ',')
17013 {
17014 ++input_line_pointer;
17015 SKIP_WHITESPACE ();
17016 }
bdaaa2e1 17017
252b5132
RH
17018 expression (&exp);
17019 if (exp.X_op != O_symbol)
17020 {
20203fb9 17021 as_bad (_("bad .weakext directive"));
98d3f06f 17022 ignore_rest_of_line ();
252b5132
RH
17023 return;
17024 }
49309057 17025 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
17026 }
17027
17028 demand_empty_rest_of_line ();
17029}
17030
17031/* Parse a register string into a number. Called from the ECOFF code
17032 to parse .frame. The argument is non-zero if this is the frame
17033 register, so that we can record it in mips_frame_reg. */
17034
17035int
17a2f251 17036tc_get_register (int frame)
252b5132 17037{
707bfff6 17038 unsigned int reg;
252b5132
RH
17039
17040 SKIP_WHITESPACE ();
707bfff6
TS
17041 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17042 reg = 0;
252b5132 17043 if (frame)
7a621144
DJ
17044 {
17045 mips_frame_reg = reg != 0 ? reg : SP;
17046 mips_frame_reg_valid = 1;
17047 mips_cprestore_valid = 0;
17048 }
252b5132
RH
17049 return reg;
17050}
17051
17052valueT
17a2f251 17053md_section_align (asection *seg, valueT addr)
252b5132
RH
17054{
17055 int align = bfd_get_section_alignment (stdoutput, seg);
17056
b4c71f56
TS
17057 if (IS_ELF)
17058 {
17059 /* We don't need to align ELF sections to the full alignment.
17060 However, Irix 5 may prefer that we align them at least to a 16
17061 byte boundary. We don't bother to align the sections if we
17062 are targeted for an embedded system. */
c41e87e3 17063 if (strncmp (TARGET_OS, "elf", 3) == 0)
b4c71f56
TS
17064 return addr;
17065 if (align > 4)
17066 align = 4;
17067 }
252b5132
RH
17068
17069 return ((addr + (1 << align) - 1) & (-1 << align));
17070}
17071
17072/* Utility routine, called from above as well. If called while the
17073 input file is still being read, it's only an approximation. (For
17074 example, a symbol may later become defined which appeared to be
17075 undefined earlier.) */
17076
17077static int
17a2f251 17078nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
17079{
17080 if (sym == 0)
17081 return 0;
17082
4d0d148d 17083 if (g_switch_value > 0)
252b5132
RH
17084 {
17085 const char *symname;
17086 int change;
17087
c9914766 17088 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
17089 register. It can be if it is smaller than the -G size or if
17090 it is in the .sdata or .sbss section. Certain symbols can
c9914766 17091 not be referenced off the $gp, although it appears as though
252b5132
RH
17092 they can. */
17093 symname = S_GET_NAME (sym);
17094 if (symname != (const char *) NULL
17095 && (strcmp (symname, "eprol") == 0
17096 || strcmp (symname, "etext") == 0
17097 || strcmp (symname, "_gp") == 0
17098 || strcmp (symname, "edata") == 0
17099 || strcmp (symname, "_fbss") == 0
17100 || strcmp (symname, "_fdata") == 0
17101 || strcmp (symname, "_ftext") == 0
17102 || strcmp (symname, "end") == 0
17103 || strcmp (symname, "_gp_disp") == 0))
17104 change = 1;
17105 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17106 && (0
17107#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
17108 || (symbol_get_obj (sym)->ecoff_extern_size != 0
17109 && (symbol_get_obj (sym)->ecoff_extern_size
17110 <= g_switch_value))
252b5132
RH
17111#endif
17112 /* We must defer this decision until after the whole
17113 file has been read, since there might be a .extern
17114 after the first use of this symbol. */
17115 || (before_relaxing
17116#ifndef NO_ECOFF_DEBUGGING
49309057 17117 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
17118#endif
17119 && S_GET_VALUE (sym) == 0)
17120 || (S_GET_VALUE (sym) != 0
17121 && S_GET_VALUE (sym) <= g_switch_value)))
17122 change = 0;
17123 else
17124 {
17125 const char *segname;
17126
17127 segname = segment_name (S_GET_SEGMENT (sym));
9c2799c2 17128 gas_assert (strcmp (segname, ".lit8") != 0
252b5132
RH
17129 && strcmp (segname, ".lit4") != 0);
17130 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
17131 && strcmp (segname, ".sbss") != 0
17132 && strncmp (segname, ".sdata.", 7) != 0
d4dc2f22
TS
17133 && strncmp (segname, ".sbss.", 6) != 0
17134 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
fba2b7f9 17135 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
17136 }
17137 return change;
17138 }
17139 else
c9914766 17140 /* We are not optimizing for the $gp register. */
252b5132
RH
17141 return 1;
17142}
17143
5919d012
RS
17144
17145/* Return true if the given symbol should be considered local for SVR4 PIC. */
17146
17147static bfd_boolean
17a2f251 17148pic_need_relax (symbolS *sym, asection *segtype)
5919d012
RS
17149{
17150 asection *symsec;
5919d012
RS
17151
17152 /* Handle the case of a symbol equated to another symbol. */
17153 while (symbol_equated_reloc_p (sym))
17154 {
17155 symbolS *n;
17156
5f0fe04b 17157 /* It's possible to get a loop here in a badly written program. */
5919d012
RS
17158 n = symbol_get_value_expression (sym)->X_add_symbol;
17159 if (n == sym)
17160 break;
17161 sym = n;
17162 }
17163
df1f3cda
DD
17164 if (symbol_section_p (sym))
17165 return TRUE;
17166
5919d012
RS
17167 symsec = S_GET_SEGMENT (sym);
17168
5919d012 17169 /* This must duplicate the test in adjust_reloc_syms. */
45dfa85a
AM
17170 return (!bfd_is_und_section (symsec)
17171 && !bfd_is_abs_section (symsec)
5f0fe04b
TS
17172 && !bfd_is_com_section (symsec)
17173 && !s_is_linkonce (sym, segtype)
5919d012
RS
17174#ifdef OBJ_ELF
17175 /* A global or weak symbol is treated as external. */
f43abd2b 17176 && (!IS_ELF || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym)))
5919d012
RS
17177#endif
17178 );
17179}
17180
17181
252b5132
RH
17182/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17183 extended opcode. SEC is the section the frag is in. */
17184
17185static int
17a2f251 17186mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132
RH
17187{
17188 int type;
3994f87e 17189 const struct mips16_immed_operand *op;
252b5132
RH
17190 offsetT val;
17191 int mintiny, maxtiny;
17192 segT symsec;
98aa84af 17193 fragS *sym_frag;
252b5132
RH
17194
17195 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17196 return 0;
17197 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17198 return 1;
17199
17200 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17201 op = mips16_immed_operands;
17202 while (op->type != type)
17203 {
17204 ++op;
9c2799c2 17205 gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
252b5132
RH
17206 }
17207
17208 if (op->unsp)
17209 {
17210 if (type == '<' || type == '>' || type == '[' || type == ']')
17211 {
17212 mintiny = 1;
17213 maxtiny = 1 << op->nbits;
17214 }
17215 else
17216 {
17217 mintiny = 0;
17218 maxtiny = (1 << op->nbits) - 1;
17219 }
17220 }
17221 else
17222 {
17223 mintiny = - (1 << (op->nbits - 1));
17224 maxtiny = (1 << (op->nbits - 1)) - 1;
17225 }
17226
98aa84af 17227 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 17228 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 17229 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
17230
17231 if (op->pcrel)
17232 {
17233 addressT addr;
17234
17235 /* We won't have the section when we are called from
17236 mips_relax_frag. However, we will always have been called
17237 from md_estimate_size_before_relax first. If this is a
17238 branch to a different section, we mark it as such. If SEC is
17239 NULL, and the frag is not marked, then it must be a branch to
17240 the same section. */
17241 if (sec == NULL)
17242 {
17243 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
17244 return 1;
17245 }
17246 else
17247 {
98aa84af 17248 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
17249 if (symsec != sec)
17250 {
17251 fragp->fr_subtype =
17252 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17253
17254 /* FIXME: We should support this, and let the linker
17255 catch branches and loads that are out of range. */
17256 as_bad_where (fragp->fr_file, fragp->fr_line,
17257 _("unsupported PC relative reference to different section"));
17258
17259 return 1;
17260 }
98aa84af
AM
17261 if (fragp != sym_frag && sym_frag->fr_address == 0)
17262 /* Assume non-extended on the first relaxation pass.
17263 The address we have calculated will be bogus if this is
17264 a forward branch to another frag, as the forward frag
17265 will have fr_address == 0. */
17266 return 0;
252b5132
RH
17267 }
17268
17269 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
17270 the same section. If the relax_marker of the symbol fragment
17271 differs from the relax_marker of this fragment, we have not
17272 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
17273 in STRETCH in order to get a better estimate of the address.
17274 This particularly matters because of the shift bits. */
17275 if (stretch != 0
98aa84af 17276 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
17277 {
17278 fragS *f;
17279
17280 /* Adjust stretch for any alignment frag. Note that if have
17281 been expanding the earlier code, the symbol may be
17282 defined in what appears to be an earlier frag. FIXME:
17283 This doesn't handle the fr_subtype field, which specifies
17284 a maximum number of bytes to skip when doing an
17285 alignment. */
98aa84af 17286 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
17287 {
17288 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17289 {
17290 if (stretch < 0)
17291 stretch = - ((- stretch)
17292 & ~ ((1 << (int) f->fr_offset) - 1));
17293 else
17294 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
17295 if (stretch == 0)
17296 break;
17297 }
17298 }
17299 if (f != NULL)
17300 val += stretch;
17301 }
17302
17303 addr = fragp->fr_address + fragp->fr_fix;
17304
17305 /* The base address rules are complicated. The base address of
17306 a branch is the following instruction. The base address of a
17307 PC relative load or add is the instruction itself, but if it
17308 is in a delay slot (in which case it can not be extended) use
17309 the address of the instruction whose delay slot it is in. */
17310 if (type == 'p' || type == 'q')
17311 {
17312 addr += 2;
17313
17314 /* If we are currently assuming that this frag should be
17315 extended, then, the current address is two bytes
bdaaa2e1 17316 higher. */
252b5132
RH
17317 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17318 addr += 2;
17319
17320 /* Ignore the low bit in the target, since it will be set
17321 for a text label. */
17322 if ((val & 1) != 0)
17323 --val;
17324 }
17325 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17326 addr -= 4;
17327 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17328 addr -= 2;
17329
17330 val -= addr & ~ ((1 << op->shift) - 1);
17331
17332 /* Branch offsets have an implicit 0 in the lowest bit. */
17333 if (type == 'p' || type == 'q')
17334 val /= 2;
17335
17336 /* If any of the shifted bits are set, we must use an extended
17337 opcode. If the address depends on the size of this
17338 instruction, this can lead to a loop, so we arrange to always
17339 use an extended opcode. We only check this when we are in
17340 the main relaxation loop, when SEC is NULL. */
17341 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
17342 {
17343 fragp->fr_subtype =
17344 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17345 return 1;
17346 }
17347
17348 /* If we are about to mark a frag as extended because the value
17349 is precisely maxtiny + 1, then there is a chance of an
17350 infinite loop as in the following code:
17351 la $4,foo
17352 .skip 1020
17353 .align 2
17354 foo:
17355 In this case when the la is extended, foo is 0x3fc bytes
17356 away, so the la can be shrunk, but then foo is 0x400 away, so
17357 the la must be extended. To avoid this loop, we mark the
17358 frag as extended if it was small, and is about to become
17359 extended with a value of maxtiny + 1. */
17360 if (val == ((maxtiny + 1) << op->shift)
17361 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
17362 && sec == NULL)
17363 {
17364 fragp->fr_subtype =
17365 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17366 return 1;
17367 }
17368 }
17369 else if (symsec != absolute_section && sec != NULL)
17370 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
17371
17372 if ((val & ((1 << op->shift) - 1)) != 0
17373 || val < (mintiny << op->shift)
17374 || val > (maxtiny << op->shift))
17375 return 1;
17376 else
17377 return 0;
17378}
17379
4a6a3df4
AO
17380/* Compute the length of a branch sequence, and adjust the
17381 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
17382 worst-case length is computed, with UPDATE being used to indicate
17383 whether an unconditional (-1), branch-likely (+1) or regular (0)
17384 branch is to be computed. */
17385static int
17a2f251 17386relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 17387{
b34976b6 17388 bfd_boolean toofar;
4a6a3df4
AO
17389 int length;
17390
17391 if (fragp
17392 && S_IS_DEFINED (fragp->fr_symbol)
17393 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17394 {
17395 addressT addr;
17396 offsetT val;
17397
17398 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17399
17400 addr = fragp->fr_address + fragp->fr_fix + 4;
17401
17402 val -= addr;
17403
17404 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17405 }
17406 else if (fragp)
17407 /* If the symbol is not defined or it's in a different segment,
17408 assume the user knows what's going on and emit a short
17409 branch. */
b34976b6 17410 toofar = FALSE;
4a6a3df4 17411 else
b34976b6 17412 toofar = TRUE;
4a6a3df4
AO
17413
17414 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17415 fragp->fr_subtype
66b3e8da
MR
17416 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
17417 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
17418 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17419 RELAX_BRANCH_LINK (fragp->fr_subtype),
17420 toofar);
17421
17422 length = 4;
17423 if (toofar)
17424 {
17425 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17426 length += 8;
17427
17428 if (mips_pic != NO_PIC)
17429 {
17430 /* Additional space for PIC loading of target address. */
17431 length += 8;
17432 if (mips_opts.isa == ISA_MIPS1)
17433 /* Additional space for $at-stabilizing nop. */
17434 length += 4;
17435 }
17436
17437 /* If branch is conditional. */
17438 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17439 length += 8;
17440 }
b34976b6 17441
4a6a3df4
AO
17442 return length;
17443}
17444
df58fc94
RS
17445/* Compute the length of a branch sequence, and adjust the
17446 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
17447 worst-case length is computed, with UPDATE being used to indicate
17448 whether an unconditional (-1), or regular (0) branch is to be
17449 computed. */
17450
17451static int
17452relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
17453{
17454 bfd_boolean toofar;
17455 int length;
17456
17457 if (fragp
17458 && S_IS_DEFINED (fragp->fr_symbol)
17459 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17460 {
17461 addressT addr;
17462 offsetT val;
17463
17464 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17465 /* Ignore the low bit in the target, since it will be set
17466 for a text label. */
17467 if ((val & 1) != 0)
17468 --val;
17469
17470 addr = fragp->fr_address + fragp->fr_fix + 4;
17471
17472 val -= addr;
17473
17474 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
17475 }
17476 else if (fragp)
17477 /* If the symbol is not defined or it's in a different segment,
17478 assume the user knows what's going on and emit a short
17479 branch. */
17480 toofar = FALSE;
17481 else
17482 toofar = TRUE;
17483
17484 if (fragp && update
17485 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17486 fragp->fr_subtype = (toofar
17487 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
17488 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
17489
17490 length = 4;
17491 if (toofar)
17492 {
17493 bfd_boolean compact_known = fragp != NULL;
17494 bfd_boolean compact = FALSE;
17495 bfd_boolean uncond;
17496
17497 if (compact_known)
17498 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17499 if (fragp)
17500 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
17501 else
17502 uncond = update < 0;
17503
17504 /* If label is out of range, we turn branch <br>:
17505
17506 <br> label # 4 bytes
17507 0:
17508
17509 into:
17510
17511 j label # 4 bytes
17512 nop # 2 bytes if compact && !PIC
17513 0:
17514 */
17515 if (mips_pic == NO_PIC && (!compact_known || compact))
17516 length += 2;
17517
17518 /* If assembling PIC code, we further turn:
17519
17520 j label # 4 bytes
17521
17522 into:
17523
17524 lw/ld at, %got(label)(gp) # 4 bytes
17525 d/addiu at, %lo(label) # 4 bytes
17526 jr/c at # 2 bytes
17527 */
17528 if (mips_pic != NO_PIC)
17529 length += 6;
17530
17531 /* If branch <br> is conditional, we prepend negated branch <brneg>:
17532
17533 <brneg> 0f # 4 bytes
17534 nop # 2 bytes if !compact
17535 */
17536 if (!uncond)
17537 length += (compact_known && compact) ? 4 : 6;
17538 }
17539
17540 return length;
17541}
17542
17543/* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
17544 bit accordingly. */
17545
17546static int
17547relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
17548{
17549 bfd_boolean toofar;
17550
df58fc94
RS
17551 if (fragp
17552 && S_IS_DEFINED (fragp->fr_symbol)
17553 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17554 {
17555 addressT addr;
17556 offsetT val;
17557 int type;
17558
17559 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17560 /* Ignore the low bit in the target, since it will be set
17561 for a text label. */
17562 if ((val & 1) != 0)
17563 --val;
17564
17565 /* Assume this is a 2-byte branch. */
17566 addr = fragp->fr_address + fragp->fr_fix + 2;
17567
17568 /* We try to avoid the infinite loop by not adding 2 more bytes for
17569 long branches. */
17570
17571 val -= addr;
17572
17573 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
17574 if (type == 'D')
17575 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
17576 else if (type == 'E')
17577 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
17578 else
17579 abort ();
17580 }
17581 else
17582 /* If the symbol is not defined or it's in a different segment,
17583 we emit a normal 32-bit branch. */
17584 toofar = TRUE;
17585
17586 if (fragp && update
17587 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17588 fragp->fr_subtype
17589 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
17590 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
17591
17592 if (toofar)
17593 return 4;
17594
17595 return 2;
17596}
17597
252b5132
RH
17598/* Estimate the size of a frag before relaxing. Unless this is the
17599 mips16, we are not really relaxing here, and the final size is
17600 encoded in the subtype information. For the mips16, we have to
17601 decide whether we are using an extended opcode or not. */
17602
252b5132 17603int
17a2f251 17604md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 17605{
5919d012 17606 int change;
252b5132 17607
4a6a3df4
AO
17608 if (RELAX_BRANCH_P (fragp->fr_subtype))
17609 {
17610
b34976b6
AM
17611 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
17612
4a6a3df4
AO
17613 return fragp->fr_var;
17614 }
17615
252b5132 17616 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
17617 /* We don't want to modify the EXTENDED bit here; it might get us
17618 into infinite loops. We change it only in mips_relax_frag(). */
17619 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132 17620
df58fc94
RS
17621 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17622 {
17623 int length = 4;
17624
17625 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17626 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
17627 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17628 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
17629 fragp->fr_var = length;
17630
17631 return length;
17632 }
17633
252b5132 17634 if (mips_pic == NO_PIC)
5919d012 17635 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132 17636 else if (mips_pic == SVR4_PIC)
5919d012 17637 change = pic_need_relax (fragp->fr_symbol, segtype);
0a44bf69
RS
17638 else if (mips_pic == VXWORKS_PIC)
17639 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
17640 change = 0;
252b5132
RH
17641 else
17642 abort ();
17643
17644 if (change)
17645 {
4d7206a2 17646 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 17647 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 17648 }
4d7206a2
RS
17649 else
17650 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
17651}
17652
17653/* This is called to see whether a reloc against a defined symbol
de7e6852 17654 should be converted into a reloc against a section. */
252b5132
RH
17655
17656int
17a2f251 17657mips_fix_adjustable (fixS *fixp)
252b5132 17658{
252b5132
RH
17659 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
17660 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17661 return 0;
a161fe53 17662
252b5132
RH
17663 if (fixp->fx_addsy == NULL)
17664 return 1;
a161fe53 17665
de7e6852
RS
17666 /* If symbol SYM is in a mergeable section, relocations of the form
17667 SYM + 0 can usually be made section-relative. The mergeable data
17668 is then identified by the section offset rather than by the symbol.
17669
17670 However, if we're generating REL LO16 relocations, the offset is split
17671 between the LO16 and parterning high part relocation. The linker will
17672 need to recalculate the complete offset in order to correctly identify
17673 the merge data.
17674
17675 The linker has traditionally not looked for the parterning high part
17676 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
17677 placed anywhere. Rather than break backwards compatibility by changing
17678 this, it seems better not to force the issue, and instead keep the
17679 original symbol. This will work with either linker behavior. */
738e5348 17680 if ((lo16_reloc_p (fixp->fx_r_type)
704803a9 17681 || reloc_needs_lo_p (fixp->fx_r_type))
de7e6852
RS
17682 && HAVE_IN_PLACE_ADDENDS
17683 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
17684 return 0;
17685
ce70d90a
MR
17686 /* There is no place to store an in-place offset for JALR relocations.
17687 Likewise an in-range offset of PC-relative relocations may overflow
17688 the in-place relocatable field if recalculated against the start
17689 address of the symbol's containing section. */
17690 if (HAVE_IN_PLACE_ADDENDS
df58fc94 17691 && (fixp->fx_pcrel || jalr_reloc_p (fixp->fx_r_type)))
1180b5a4
RS
17692 return 0;
17693
252b5132 17694#ifdef OBJ_ELF
b314ec0e
RS
17695 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
17696 to a floating-point stub. The same is true for non-R_MIPS16_26
17697 relocations against MIPS16 functions; in this case, the stub becomes
17698 the function's canonical address.
17699
17700 Floating-point stubs are stored in unique .mips16.call.* or
17701 .mips16.fn.* sections. If a stub T for function F is in section S,
17702 the first relocation in section S must be against F; this is how the
17703 linker determines the target function. All relocations that might
17704 resolve to T must also be against F. We therefore have the following
17705 restrictions, which are given in an intentionally-redundant way:
17706
17707 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
17708 symbols.
17709
17710 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
17711 if that stub might be used.
17712
17713 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
17714 symbols.
17715
17716 4. We cannot reduce a stub's relocations against MIPS16 symbols if
17717 that stub might be used.
17718
17719 There is a further restriction:
17720
df58fc94
RS
17721 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
17722 R_MICROMIPS_26_S1) against MIPS16 or microMIPS symbols on
17723 targets with in-place addends; the relocation field cannot
b314ec0e
RS
17724 encode the low bit.
17725
df58fc94
RS
17726 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
17727 against a MIPS16 symbol. We deal with (5) by by not reducing any
17728 such relocations on REL targets.
b314ec0e
RS
17729
17730 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
17731 relocation against some symbol R, no relocation against R may be
17732 reduced. (Note that this deals with (2) as well as (1) because
17733 relocations against global symbols will never be reduced on ELF
17734 targets.) This approach is a little simpler than trying to detect
17735 stub sections, and gives the "all or nothing" per-symbol consistency
17736 that we have for MIPS16 symbols. */
f43abd2b 17737 if (IS_ELF
b314ec0e 17738 && fixp->fx_subsy == NULL
30c09090 17739 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
df58fc94
RS
17740 || *symbol_get_tc (fixp->fx_addsy)
17741 || (HAVE_IN_PLACE_ADDENDS
17742 && ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
17743 && jmp_reloc_p (fixp->fx_r_type))))
252b5132
RH
17744 return 0;
17745#endif
a161fe53 17746
252b5132
RH
17747 return 1;
17748}
17749
17750/* Translate internal representation of relocation info to BFD target
17751 format. */
17752
17753arelent **
17a2f251 17754tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
17755{
17756 static arelent *retval[4];
17757 arelent *reloc;
17758 bfd_reloc_code_real_type code;
17759
4b0cff4e
TS
17760 memset (retval, 0, sizeof(retval));
17761 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
49309057
ILT
17762 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
17763 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
17764 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
17765
bad36eac
DJ
17766 if (fixp->fx_pcrel)
17767 {
df58fc94
RS
17768 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
17769 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
17770 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
17771 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1);
bad36eac
DJ
17772
17773 /* At this point, fx_addnumber is "symbol offset - pcrel address".
17774 Relocations want only the symbol offset. */
17775 reloc->addend = fixp->fx_addnumber + reloc->address;
f43abd2b 17776 if (!IS_ELF)
bad36eac
DJ
17777 {
17778 /* A gruesome hack which is a result of the gruesome gas
17779 reloc handling. What's worse, for COFF (as opposed to
17780 ECOFF), we might need yet another copy of reloc->address.
17781 See bfd_install_relocation. */
17782 reloc->addend += reloc->address;
17783 }
17784 }
17785 else
17786 reloc->addend = fixp->fx_addnumber;
252b5132 17787
438c16b8
TS
17788 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
17789 entry to be used in the relocation's section offset. */
17790 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
17791 {
17792 reloc->address = reloc->addend;
17793 reloc->addend = 0;
17794 }
17795
252b5132 17796 code = fixp->fx_r_type;
252b5132 17797
bad36eac 17798 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
252b5132
RH
17799 if (reloc->howto == NULL)
17800 {
17801 as_bad_where (fixp->fx_file, fixp->fx_line,
17802 _("Can not represent %s relocation in this object file format"),
17803 bfd_get_reloc_code_name (code));
17804 retval[0] = NULL;
17805 }
17806
17807 return retval;
17808}
17809
17810/* Relax a machine dependent frag. This returns the amount by which
17811 the current size of the frag should change. */
17812
17813int
17a2f251 17814mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 17815{
4a6a3df4
AO
17816 if (RELAX_BRANCH_P (fragp->fr_subtype))
17817 {
17818 offsetT old_var = fragp->fr_var;
b34976b6
AM
17819
17820 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
17821
17822 return fragp->fr_var - old_var;
17823 }
17824
df58fc94
RS
17825 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17826 {
17827 offsetT old_var = fragp->fr_var;
17828 offsetT new_var = 4;
17829
17830 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17831 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
17832 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17833 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
17834 fragp->fr_var = new_var;
17835
17836 return new_var - old_var;
17837 }
17838
252b5132
RH
17839 if (! RELAX_MIPS16_P (fragp->fr_subtype))
17840 return 0;
17841
c4e7957c 17842 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
17843 {
17844 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17845 return 0;
17846 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
17847 return 2;
17848 }
17849 else
17850 {
17851 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17852 return 0;
17853 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
17854 return -2;
17855 }
17856
17857 return 0;
17858}
17859
17860/* Convert a machine dependent frag. */
17861
17862void
17a2f251 17863md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 17864{
4a6a3df4
AO
17865 if (RELAX_BRANCH_P (fragp->fr_subtype))
17866 {
4d68580a 17867 char *buf;
4a6a3df4
AO
17868 unsigned long insn;
17869 expressionS exp;
17870 fixS *fixp;
b34976b6 17871
4d68580a
RS
17872 buf = fragp->fr_literal + fragp->fr_fix;
17873 insn = read_insn (buf);
b34976b6 17874
4a6a3df4
AO
17875 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17876 {
17877 /* We generate a fixup instead of applying it right now
17878 because, if there are linker relaxations, we're going to
17879 need the relocations. */
17880 exp.X_op = O_symbol;
17881 exp.X_add_symbol = fragp->fr_symbol;
17882 exp.X_add_number = fragp->fr_offset;
17883
4d68580a
RS
17884 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
17885 BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
17886 fixp->fx_file = fragp->fr_file;
17887 fixp->fx_line = fragp->fr_line;
b34976b6 17888
4d68580a 17889 buf = write_insn (buf, insn);
4a6a3df4
AO
17890 }
17891 else
17892 {
17893 int i;
17894
17895 as_warn_where (fragp->fr_file, fragp->fr_line,
5c4f07ba 17896 _("Relaxed out-of-range branch into a jump"));
4a6a3df4
AO
17897
17898 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
17899 goto uncond;
17900
17901 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
17902 {
17903 /* Reverse the branch. */
17904 switch ((insn >> 28) & 0xf)
17905 {
17906 case 4:
17907 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
17908 have the condition reversed by tweaking a single
17909 bit, and their opcodes all have 0x4???????. */
9c2799c2 17910 gas_assert ((insn & 0xf1000000) == 0x41000000);
4a6a3df4
AO
17911 insn ^= 0x00010000;
17912 break;
17913
17914 case 0:
17915 /* bltz 0x04000000 bgez 0x04010000
54f4ddb3 17916 bltzal 0x04100000 bgezal 0x04110000 */
9c2799c2 17917 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
4a6a3df4
AO
17918 insn ^= 0x00010000;
17919 break;
b34976b6 17920
4a6a3df4
AO
17921 case 1:
17922 /* beq 0x10000000 bne 0x14000000
54f4ddb3 17923 blez 0x18000000 bgtz 0x1c000000 */
4a6a3df4
AO
17924 insn ^= 0x04000000;
17925 break;
17926
17927 default:
17928 abort ();
17929 }
17930 }
17931
17932 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
17933 {
17934 /* Clear the and-link bit. */
9c2799c2 17935 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
4a6a3df4 17936
54f4ddb3
TS
17937 /* bltzal 0x04100000 bgezal 0x04110000
17938 bltzall 0x04120000 bgezall 0x04130000 */
4a6a3df4
AO
17939 insn &= ~0x00100000;
17940 }
17941
17942 /* Branch over the branch (if the branch was likely) or the
17943 full jump (not likely case). Compute the offset from the
17944 current instruction to branch to. */
17945 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
17946 i = 16;
17947 else
17948 {
17949 /* How many bytes in instructions we've already emitted? */
4d68580a 17950 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
17951 /* How many bytes in instructions from here to the end? */
17952 i = fragp->fr_var - i;
17953 }
17954 /* Convert to instruction count. */
17955 i >>= 2;
17956 /* Branch counts from the next instruction. */
b34976b6 17957 i--;
4a6a3df4
AO
17958 insn |= i;
17959 /* Branch over the jump. */
4d68580a 17960 buf = write_insn (buf, insn);
4a6a3df4 17961
54f4ddb3 17962 /* nop */
4d68580a 17963 buf = write_insn (buf, 0);
4a6a3df4
AO
17964
17965 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
17966 {
17967 /* beql $0, $0, 2f */
17968 insn = 0x50000000;
17969 /* Compute the PC offset from the current instruction to
17970 the end of the variable frag. */
17971 /* How many bytes in instructions we've already emitted? */
4d68580a 17972 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
17973 /* How many bytes in instructions from here to the end? */
17974 i = fragp->fr_var - i;
17975 /* Convert to instruction count. */
17976 i >>= 2;
17977 /* Don't decrement i, because we want to branch over the
17978 delay slot. */
4a6a3df4 17979 insn |= i;
4a6a3df4 17980
4d68580a
RS
17981 buf = write_insn (buf, insn);
17982 buf = write_insn (buf, 0);
4a6a3df4
AO
17983 }
17984
17985 uncond:
17986 if (mips_pic == NO_PIC)
17987 {
17988 /* j or jal. */
17989 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
17990 ? 0x0c000000 : 0x08000000);
17991 exp.X_op = O_symbol;
17992 exp.X_add_symbol = fragp->fr_symbol;
17993 exp.X_add_number = fragp->fr_offset;
17994
4d68580a
RS
17995 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
17996 FALSE, BFD_RELOC_MIPS_JMP);
4a6a3df4
AO
17997 fixp->fx_file = fragp->fr_file;
17998 fixp->fx_line = fragp->fr_line;
17999
4d68580a 18000 buf = write_insn (buf, insn);
4a6a3df4
AO
18001 }
18002 else
18003 {
66b3e8da
MR
18004 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18005
4a6a3df4 18006 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
66b3e8da
MR
18007 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18008 insn |= at << OP_SH_RT;
4a6a3df4
AO
18009 exp.X_op = O_symbol;
18010 exp.X_add_symbol = fragp->fr_symbol;
18011 exp.X_add_number = fragp->fr_offset;
18012
18013 if (fragp->fr_offset)
18014 {
18015 exp.X_add_symbol = make_expr_symbol (&exp);
18016 exp.X_add_number = 0;
18017 }
18018
4d68580a
RS
18019 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18020 FALSE, BFD_RELOC_MIPS_GOT16);
4a6a3df4
AO
18021 fixp->fx_file = fragp->fr_file;
18022 fixp->fx_line = fragp->fr_line;
18023
4d68580a 18024 buf = write_insn (buf, insn);
b34976b6 18025
4a6a3df4 18026 if (mips_opts.isa == ISA_MIPS1)
4d68580a
RS
18027 /* nop */
18028 buf = write_insn (buf, 0);
4a6a3df4
AO
18029
18030 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
66b3e8da
MR
18031 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18032 insn |= at << OP_SH_RS | at << OP_SH_RT;
4a6a3df4 18033
4d68580a
RS
18034 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18035 FALSE, BFD_RELOC_LO16);
4a6a3df4
AO
18036 fixp->fx_file = fragp->fr_file;
18037 fixp->fx_line = fragp->fr_line;
b34976b6 18038
4d68580a 18039 buf = write_insn (buf, insn);
4a6a3df4
AO
18040
18041 /* j(al)r $at. */
18042 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
66b3e8da 18043 insn = 0x0000f809;
4a6a3df4 18044 else
66b3e8da
MR
18045 insn = 0x00000008;
18046 insn |= at << OP_SH_RS;
4a6a3df4 18047
4d68580a 18048 buf = write_insn (buf, insn);
4a6a3df4
AO
18049 }
18050 }
18051
4a6a3df4 18052 fragp->fr_fix += fragp->fr_var;
4d68580a 18053 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
4a6a3df4
AO
18054 return;
18055 }
18056
df58fc94
RS
18057 /* Relax microMIPS branches. */
18058 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18059 {
4d68580a 18060 char *buf = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
18061 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18062 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18063 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
2309ddf2 18064 bfd_boolean short_ds;
df58fc94
RS
18065 unsigned long insn;
18066 expressionS exp;
18067 fixS *fixp;
18068
18069 exp.X_op = O_symbol;
18070 exp.X_add_symbol = fragp->fr_symbol;
18071 exp.X_add_number = fragp->fr_offset;
18072
18073 fragp->fr_fix += fragp->fr_var;
18074
18075 /* Handle 16-bit branches that fit or are forced to fit. */
18076 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18077 {
18078 /* We generate a fixup instead of applying it right now,
18079 because if there is linker relaxation, we're going to
18080 need the relocations. */
18081 if (type == 'D')
4d68580a 18082 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
df58fc94
RS
18083 BFD_RELOC_MICROMIPS_10_PCREL_S1);
18084 else if (type == 'E')
4d68580a 18085 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
df58fc94
RS
18086 BFD_RELOC_MICROMIPS_7_PCREL_S1);
18087 else
18088 abort ();
18089
18090 fixp->fx_file = fragp->fr_file;
18091 fixp->fx_line = fragp->fr_line;
18092
18093 /* These relocations can have an addend that won't fit in
18094 2 octets. */
18095 fixp->fx_no_overflow = 1;
18096
18097 return;
18098 }
18099
2309ddf2 18100 /* Handle 32-bit branches that fit or are forced to fit. */
df58fc94
RS
18101 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18102 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18103 {
18104 /* We generate a fixup instead of applying it right now,
18105 because if there is linker relaxation, we're going to
18106 need the relocations. */
4d68580a
RS
18107 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
18108 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18109 fixp->fx_file = fragp->fr_file;
18110 fixp->fx_line = fragp->fr_line;
18111
18112 if (type == 0)
18113 return;
18114 }
18115
18116 /* Relax 16-bit branches to 32-bit branches. */
18117 if (type != 0)
18118 {
4d68580a 18119 insn = read_compressed_insn (buf, 2);
df58fc94
RS
18120
18121 if ((insn & 0xfc00) == 0xcc00) /* b16 */
18122 insn = 0x94000000; /* beq */
18123 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
18124 {
18125 unsigned long regno;
18126
18127 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18128 regno = micromips_to_32_reg_d_map [regno];
18129 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
18130 insn |= regno << MICROMIPSOP_SH_RS;
18131 }
18132 else
18133 abort ();
18134
18135 /* Nothing else to do, just write it out. */
18136 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18137 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18138 {
4d68580a
RS
18139 buf = write_compressed_insn (buf, insn, 4);
18140 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18141 return;
18142 }
18143 }
18144 else
4d68580a 18145 insn = read_compressed_insn (buf, 4);
df58fc94
RS
18146
18147 /* Relax 32-bit branches to a sequence of instructions. */
18148 as_warn_where (fragp->fr_file, fragp->fr_line,
18149 _("Relaxed out-of-range branch into a jump"));
18150
2309ddf2
MR
18151 /* Set the short-delay-slot bit. */
18152 short_ds = al && (insn & 0x02000000) != 0;
df58fc94
RS
18153
18154 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18155 {
18156 symbolS *l;
18157
18158 /* Reverse the branch. */
18159 if ((insn & 0xfc000000) == 0x94000000 /* beq */
18160 || (insn & 0xfc000000) == 0xb4000000) /* bne */
18161 insn ^= 0x20000000;
18162 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
18163 || (insn & 0xffe00000) == 0x40400000 /* bgez */
18164 || (insn & 0xffe00000) == 0x40800000 /* blez */
18165 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
18166 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
18167 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
18168 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
18169 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
18170 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
18171 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
18172 insn ^= 0x00400000;
18173 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
18174 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
18175 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
18176 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
18177 insn ^= 0x00200000;
18178 else
18179 abort ();
18180
18181 if (al)
18182 {
18183 /* Clear the and-link and short-delay-slot bits. */
18184 gas_assert ((insn & 0xfda00000) == 0x40200000);
18185
18186 /* bltzal 0x40200000 bgezal 0x40600000 */
18187 /* bltzals 0x42200000 bgezals 0x42600000 */
18188 insn &= ~0x02200000;
18189 }
18190
18191 /* Make a label at the end for use with the branch. */
18192 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
18193 micromips_label_inc ();
18194#if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
18195 if (IS_ELF)
18196 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
18197#endif
18198
18199 /* Refer to it. */
4d68580a
RS
18200 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
18201 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18202 fixp->fx_file = fragp->fr_file;
18203 fixp->fx_line = fragp->fr_line;
18204
18205 /* Branch over the jump. */
4d68580a 18206 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18207 if (!compact)
4d68580a
RS
18208 /* nop */
18209 buf = write_compressed_insn (buf, 0x0c00, 2);
df58fc94
RS
18210 }
18211
18212 if (mips_pic == NO_PIC)
18213 {
2309ddf2
MR
18214 unsigned long jal = short_ds ? 0x74000000 : 0xf4000000; /* jal/s */
18215
df58fc94
RS
18216 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
18217 insn = al ? jal : 0xd4000000;
18218
4d68580a
RS
18219 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18220 BFD_RELOC_MICROMIPS_JMP);
df58fc94
RS
18221 fixp->fx_file = fragp->fr_file;
18222 fixp->fx_line = fragp->fr_line;
18223
4d68580a 18224 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18225 if (compact)
4d68580a
RS
18226 /* nop */
18227 buf = write_compressed_insn (buf, 0x0c00, 2);
df58fc94
RS
18228 }
18229 else
18230 {
18231 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
2309ddf2
MR
18232 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
18233 unsigned long jr = compact ? 0x45a0 : 0x4580; /* jr/c */
df58fc94
RS
18234
18235 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
18236 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18237 insn |= at << MICROMIPSOP_SH_RT;
18238
18239 if (exp.X_add_number)
18240 {
18241 exp.X_add_symbol = make_expr_symbol (&exp);
18242 exp.X_add_number = 0;
18243 }
18244
4d68580a
RS
18245 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18246 BFD_RELOC_MICROMIPS_GOT16);
df58fc94
RS
18247 fixp->fx_file = fragp->fr_file;
18248 fixp->fx_line = fragp->fr_line;
18249
4d68580a 18250 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
18251
18252 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
18253 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18254 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18255
4d68580a
RS
18256 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18257 BFD_RELOC_MICROMIPS_LO16);
df58fc94
RS
18258 fixp->fx_file = fragp->fr_file;
18259 fixp->fx_line = fragp->fr_line;
18260
4d68580a 18261 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
18262
18263 /* jr/jrc/jalr/jalrs $at */
18264 insn = al ? jalr : jr;
18265 insn |= at << MICROMIPSOP_SH_MJ;
18266
4d68580a 18267 buf = write_compressed_insn (buf, insn, 2);
df58fc94
RS
18268 }
18269
4d68580a 18270 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18271 return;
18272 }
18273
252b5132
RH
18274 if (RELAX_MIPS16_P (fragp->fr_subtype))
18275 {
18276 int type;
3994f87e 18277 const struct mips16_immed_operand *op;
252b5132 18278 offsetT val;
5c04167a
RS
18279 char *buf;
18280 unsigned int user_length, length;
252b5132 18281 unsigned long insn;
5c04167a 18282 bfd_boolean ext;
252b5132
RH
18283
18284 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
18285 op = mips16_immed_operands;
18286 while (op->type != type)
18287 ++op;
18288
5c04167a 18289 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
5f5f22c0 18290 val = resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
18291 if (op->pcrel)
18292 {
18293 addressT addr;
18294
18295 addr = fragp->fr_address + fragp->fr_fix;
18296
18297 /* The rules for the base address of a PC relative reloc are
18298 complicated; see mips16_extended_frag. */
18299 if (type == 'p' || type == 'q')
18300 {
18301 addr += 2;
18302 if (ext)
18303 addr += 2;
18304 /* Ignore the low bit in the target, since it will be
18305 set for a text label. */
18306 if ((val & 1) != 0)
18307 --val;
18308 }
18309 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
18310 addr -= 4;
18311 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
18312 addr -= 2;
18313
18314 addr &= ~ (addressT) ((1 << op->shift) - 1);
18315 val -= addr;
18316
18317 /* Make sure the section winds up with the alignment we have
18318 assumed. */
18319 if (op->shift > 0)
18320 record_alignment (asec, op->shift);
18321 }
18322
18323 if (ext
18324 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
18325 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
18326 as_warn_where (fragp->fr_file, fragp->fr_line,
18327 _("extended instruction in delay slot"));
18328
5c04167a 18329 buf = fragp->fr_literal + fragp->fr_fix;
252b5132 18330
4d68580a 18331 insn = read_compressed_insn (buf, 2);
5c04167a
RS
18332 if (ext)
18333 insn |= MIPS16_EXTEND;
252b5132 18334
5c04167a
RS
18335 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
18336 user_length = 4;
18337 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
18338 user_length = 2;
18339 else
18340 user_length = 0;
18341
43c0598f 18342 mips16_immed (fragp->fr_file, fragp->fr_line, type,
c150d1d2 18343 BFD_RELOC_UNUSED, val, user_length, &insn);
252b5132 18344
5c04167a
RS
18345 length = (ext ? 4 : 2);
18346 gas_assert (mips16_opcode_length (insn) == length);
18347 write_compressed_insn (buf, insn, length);
18348 fragp->fr_fix += length;
252b5132
RH
18349 }
18350 else
18351 {
df58fc94
RS
18352 relax_substateT subtype = fragp->fr_subtype;
18353 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
18354 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
4d7206a2
RS
18355 int first, second;
18356 fixS *fixp;
252b5132 18357
df58fc94
RS
18358 first = RELAX_FIRST (subtype);
18359 second = RELAX_SECOND (subtype);
4d7206a2 18360 fixp = (fixS *) fragp->fr_opcode;
252b5132 18361
df58fc94
RS
18362 /* If the delay slot chosen does not match the size of the instruction,
18363 then emit a warning. */
18364 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
18365 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
18366 {
18367 relax_substateT s;
18368 const char *msg;
18369
18370 s = subtype & (RELAX_DELAY_SLOT_16BIT
18371 | RELAX_DELAY_SLOT_SIZE_FIRST
18372 | RELAX_DELAY_SLOT_SIZE_SECOND);
18373 msg = macro_warning (s);
18374 if (msg != NULL)
db9b2be4 18375 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94
RS
18376 subtype &= ~s;
18377 }
18378
584892a6 18379 /* Possibly emit a warning if we've chosen the longer option. */
df58fc94 18380 if (use_second == second_longer)
584892a6 18381 {
df58fc94
RS
18382 relax_substateT s;
18383 const char *msg;
18384
18385 s = (subtype
18386 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
18387 msg = macro_warning (s);
18388 if (msg != NULL)
db9b2be4 18389 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94 18390 subtype &= ~s;
584892a6
RS
18391 }
18392
4d7206a2
RS
18393 /* Go through all the fixups for the first sequence. Disable them
18394 (by marking them as done) if we're going to use the second
18395 sequence instead. */
18396 while (fixp
18397 && fixp->fx_frag == fragp
18398 && fixp->fx_where < fragp->fr_fix - second)
18399 {
df58fc94 18400 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18401 fixp->fx_done = 1;
18402 fixp = fixp->fx_next;
18403 }
252b5132 18404
4d7206a2
RS
18405 /* Go through the fixups for the second sequence. Disable them if
18406 we're going to use the first sequence, otherwise adjust their
18407 addresses to account for the relaxation. */
18408 while (fixp && fixp->fx_frag == fragp)
18409 {
df58fc94 18410 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18411 fixp->fx_where -= first;
18412 else
18413 fixp->fx_done = 1;
18414 fixp = fixp->fx_next;
18415 }
18416
18417 /* Now modify the frag contents. */
df58fc94 18418 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18419 {
18420 char *start;
18421
18422 start = fragp->fr_literal + fragp->fr_fix - first - second;
18423 memmove (start, start + first, second);
18424 fragp->fr_fix -= first;
18425 }
18426 else
18427 fragp->fr_fix -= second;
252b5132
RH
18428 }
18429}
18430
18431#ifdef OBJ_ELF
18432
18433/* This function is called after the relocs have been generated.
18434 We've been storing mips16 text labels as odd. Here we convert them
18435 back to even for the convenience of the debugger. */
18436
18437void
17a2f251 18438mips_frob_file_after_relocs (void)
252b5132
RH
18439{
18440 asymbol **syms;
18441 unsigned int count, i;
18442
f43abd2b 18443 if (!IS_ELF)
252b5132
RH
18444 return;
18445
18446 syms = bfd_get_outsymbols (stdoutput);
18447 count = bfd_get_symcount (stdoutput);
18448 for (i = 0; i < count; i++, syms++)
df58fc94
RS
18449 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
18450 && ((*syms)->value & 1) != 0)
18451 {
18452 (*syms)->value &= ~1;
18453 /* If the symbol has an odd size, it was probably computed
18454 incorrectly, so adjust that as well. */
18455 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
18456 ++elf_symbol (*syms)->internal_elf_sym.st_size;
18457 }
252b5132
RH
18458}
18459
18460#endif
18461
a1facbec
MR
18462/* This function is called whenever a label is defined, including fake
18463 labels instantiated off the dot special symbol. It is used when
18464 handling branch delays; if a branch has a label, we assume we cannot
18465 move it. This also bumps the value of the symbol by 1 in compressed
18466 code. */
252b5132 18467
e1b47bd5 18468static void
a1facbec 18469mips_record_label (symbolS *sym)
252b5132 18470{
a8dbcb85 18471 segment_info_type *si = seg_info (now_seg);
252b5132
RH
18472 struct insn_label_list *l;
18473
18474 if (free_insn_labels == NULL)
18475 l = (struct insn_label_list *) xmalloc (sizeof *l);
18476 else
18477 {
18478 l = free_insn_labels;
18479 free_insn_labels = l->next;
18480 }
18481
18482 l->label = sym;
a8dbcb85
TS
18483 l->next = si->label_list;
18484 si->label_list = l;
a1facbec 18485}
07a53e5c 18486
a1facbec
MR
18487/* This function is called as tc_frob_label() whenever a label is defined
18488 and adds a DWARF-2 record we only want for true labels. */
18489
18490void
18491mips_define_label (symbolS *sym)
18492{
18493 mips_record_label (sym);
07a53e5c
RH
18494#ifdef OBJ_ELF
18495 dwarf2_emit_label (sym);
18496#endif
252b5132 18497}
e1b47bd5
RS
18498
18499/* This function is called by tc_new_dot_label whenever a new dot symbol
18500 is defined. */
18501
18502void
18503mips_add_dot_label (symbolS *sym)
18504{
18505 mips_record_label (sym);
18506 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
18507 mips_compressed_mark_label (sym);
18508}
252b5132
RH
18509\f
18510#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
18511
18512/* Some special processing for a MIPS ELF file. */
18513
18514void
17a2f251 18515mips_elf_final_processing (void)
252b5132
RH
18516{
18517 /* Write out the register information. */
316f5878 18518 if (mips_abi != N64_ABI)
252b5132
RH
18519 {
18520 Elf32_RegInfo s;
18521
18522 s.ri_gprmask = mips_gprmask;
18523 s.ri_cprmask[0] = mips_cprmask[0];
18524 s.ri_cprmask[1] = mips_cprmask[1];
18525 s.ri_cprmask[2] = mips_cprmask[2];
18526 s.ri_cprmask[3] = mips_cprmask[3];
18527 /* The gp_value field is set by the MIPS ELF backend. */
18528
18529 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
18530 ((Elf32_External_RegInfo *)
18531 mips_regmask_frag));
18532 }
18533 else
18534 {
18535 Elf64_Internal_RegInfo s;
18536
18537 s.ri_gprmask = mips_gprmask;
18538 s.ri_pad = 0;
18539 s.ri_cprmask[0] = mips_cprmask[0];
18540 s.ri_cprmask[1] = mips_cprmask[1];
18541 s.ri_cprmask[2] = mips_cprmask[2];
18542 s.ri_cprmask[3] = mips_cprmask[3];
18543 /* The gp_value field is set by the MIPS ELF backend. */
18544
18545 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
18546 ((Elf64_External_RegInfo *)
18547 mips_regmask_frag));
18548 }
18549
18550 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
18551 sort of BFD interface for this. */
18552 if (mips_any_noreorder)
18553 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
18554 if (mips_pic != NO_PIC)
143d77c5 18555 {
8b828383 18556 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
18557 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
18558 }
18559 if (mips_abicalls)
18560 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 18561
98d3f06f 18562 /* Set MIPS ELF flags for ASEs. */
74cd071d
CF
18563 /* We may need to define a new flag for DSP ASE, and set this flag when
18564 file_ase_dsp is true. */
8b082fb1 18565 /* Same for DSP R2. */
ef2e4d86
CF
18566 /* We may need to define a new flag for MT ASE, and set this flag when
18567 file_ase_mt is true. */
a4672219
TS
18568 if (file_ase_mips16)
18569 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
df58fc94
RS
18570 if (file_ase_micromips)
18571 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
1f25f5d3
CD
18572#if 0 /* XXX FIXME */
18573 if (file_ase_mips3d)
18574 elf_elfheader (stdoutput)->e_flags |= ???;
18575#endif
deec1734
CD
18576 if (file_ase_mdmx)
18577 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 18578
bdaaa2e1 18579 /* Set the MIPS ELF ABI flags. */
316f5878 18580 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 18581 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 18582 else if (mips_abi == O64_ABI)
252b5132 18583 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 18584 else if (mips_abi == EABI_ABI)
252b5132 18585 {
316f5878 18586 if (!file_mips_gp32)
252b5132
RH
18587 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
18588 else
18589 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
18590 }
316f5878 18591 else if (mips_abi == N32_ABI)
be00bddd
TS
18592 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
18593
c9914766 18594 /* Nothing to do for N64_ABI. */
252b5132
RH
18595
18596 if (mips_32bitmode)
18597 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
ad3fea08
TS
18598
18599#if 0 /* XXX FIXME */
18600 /* 32 bit code with 64 bit FP registers. */
18601 if (!file_mips_fp32 && ABI_NEEDS_32BIT_REGS (mips_abi))
18602 elf_elfheader (stdoutput)->e_flags |= ???;
18603#endif
252b5132
RH
18604}
18605
18606#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
18607\f
beae10d5 18608typedef struct proc {
9b2f1d35
EC
18609 symbolS *func_sym;
18610 symbolS *func_end_sym;
beae10d5
KH
18611 unsigned long reg_mask;
18612 unsigned long reg_offset;
18613 unsigned long fpreg_mask;
18614 unsigned long fpreg_offset;
18615 unsigned long frame_offset;
18616 unsigned long frame_reg;
18617 unsigned long pc_reg;
18618} procS;
252b5132
RH
18619
18620static procS cur_proc;
18621static procS *cur_proc_ptr;
18622static int numprocs;
18623
df58fc94
RS
18624/* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
18625 as "2", and a normal nop as "0". */
18626
18627#define NOP_OPCODE_MIPS 0
18628#define NOP_OPCODE_MIPS16 1
18629#define NOP_OPCODE_MICROMIPS 2
742a56fe
RS
18630
18631char
18632mips_nop_opcode (void)
18633{
df58fc94
RS
18634 if (seg_info (now_seg)->tc_segment_info_data.micromips)
18635 return NOP_OPCODE_MICROMIPS;
18636 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
18637 return NOP_OPCODE_MIPS16;
18638 else
18639 return NOP_OPCODE_MIPS;
742a56fe
RS
18640}
18641
df58fc94
RS
18642/* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
18643 32-bit microMIPS NOPs here (if applicable). */
a19d8eb0 18644
0a9ef439 18645void
17a2f251 18646mips_handle_align (fragS *fragp)
a19d8eb0 18647{
df58fc94 18648 char nop_opcode;
742a56fe 18649 char *p;
c67a084a
NC
18650 int bytes, size, excess;
18651 valueT opcode;
742a56fe 18652
0a9ef439
RH
18653 if (fragp->fr_type != rs_align_code)
18654 return;
18655
742a56fe 18656 p = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
18657 nop_opcode = *p;
18658 switch (nop_opcode)
a19d8eb0 18659 {
df58fc94
RS
18660 case NOP_OPCODE_MICROMIPS:
18661 opcode = micromips_nop32_insn.insn_opcode;
18662 size = 4;
18663 break;
18664 case NOP_OPCODE_MIPS16:
c67a084a
NC
18665 opcode = mips16_nop_insn.insn_opcode;
18666 size = 2;
df58fc94
RS
18667 break;
18668 case NOP_OPCODE_MIPS:
18669 default:
c67a084a
NC
18670 opcode = nop_insn.insn_opcode;
18671 size = 4;
df58fc94 18672 break;
c67a084a 18673 }
a19d8eb0 18674
c67a084a
NC
18675 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
18676 excess = bytes % size;
df58fc94
RS
18677
18678 /* Handle the leading part if we're not inserting a whole number of
18679 instructions, and make it the end of the fixed part of the frag.
18680 Try to fit in a short microMIPS NOP if applicable and possible,
18681 and use zeroes otherwise. */
18682 gas_assert (excess < 4);
18683 fragp->fr_fix += excess;
18684 switch (excess)
c67a084a 18685 {
df58fc94
RS
18686 case 3:
18687 *p++ = '\0';
18688 /* Fall through. */
18689 case 2:
18690 if (nop_opcode == NOP_OPCODE_MICROMIPS)
18691 {
4d68580a 18692 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
df58fc94
RS
18693 break;
18694 }
18695 *p++ = '\0';
18696 /* Fall through. */
18697 case 1:
18698 *p++ = '\0';
18699 /* Fall through. */
18700 case 0:
18701 break;
a19d8eb0 18702 }
c67a084a
NC
18703
18704 md_number_to_chars (p, opcode, size);
18705 fragp->fr_var = size;
a19d8eb0
CP
18706}
18707
252b5132 18708static void
17a2f251 18709md_obj_begin (void)
252b5132
RH
18710{
18711}
18712
18713static void
17a2f251 18714md_obj_end (void)
252b5132 18715{
54f4ddb3 18716 /* Check for premature end, nesting errors, etc. */
252b5132 18717 if (cur_proc_ptr)
9a41af64 18718 as_warn (_("missing .end at end of assembly"));
252b5132
RH
18719}
18720
18721static long
17a2f251 18722get_number (void)
252b5132
RH
18723{
18724 int negative = 0;
18725 long val = 0;
18726
18727 if (*input_line_pointer == '-')
18728 {
18729 ++input_line_pointer;
18730 negative = 1;
18731 }
3882b010 18732 if (!ISDIGIT (*input_line_pointer))
956cd1d6 18733 as_bad (_("expected simple number"));
252b5132
RH
18734 if (input_line_pointer[0] == '0')
18735 {
18736 if (input_line_pointer[1] == 'x')
18737 {
18738 input_line_pointer += 2;
3882b010 18739 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
18740 {
18741 val <<= 4;
18742 val |= hex_value (*input_line_pointer++);
18743 }
18744 return negative ? -val : val;
18745 }
18746 else
18747 {
18748 ++input_line_pointer;
3882b010 18749 while (ISDIGIT (*input_line_pointer))
252b5132
RH
18750 {
18751 val <<= 3;
18752 val |= *input_line_pointer++ - '0';
18753 }
18754 return negative ? -val : val;
18755 }
18756 }
3882b010 18757 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
18758 {
18759 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
18760 *input_line_pointer, *input_line_pointer);
956cd1d6 18761 as_warn (_("invalid number"));
252b5132
RH
18762 return -1;
18763 }
3882b010 18764 while (ISDIGIT (*input_line_pointer))
252b5132
RH
18765 {
18766 val *= 10;
18767 val += *input_line_pointer++ - '0';
18768 }
18769 return negative ? -val : val;
18770}
18771
18772/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
18773 is an initial number which is the ECOFF file index. In the non-ECOFF
18774 case .file implies DWARF-2. */
18775
18776static void
17a2f251 18777s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 18778{
ecb4347a
DJ
18779 static int first_file_directive = 0;
18780
c5dd6aab
DJ
18781 if (ECOFF_DEBUGGING)
18782 {
18783 get_number ();
18784 s_app_file (0);
18785 }
18786 else
ecb4347a
DJ
18787 {
18788 char *filename;
18789
18790 filename = dwarf2_directive_file (0);
18791
18792 /* Versions of GCC up to 3.1 start files with a ".file"
18793 directive even for stabs output. Make sure that this
18794 ".file" is handled. Note that you need a version of GCC
18795 after 3.1 in order to support DWARF-2 on MIPS. */
18796 if (filename != NULL && ! first_file_directive)
18797 {
18798 (void) new_logical_line (filename, -1);
c04f5787 18799 s_app_file_string (filename, 0);
ecb4347a
DJ
18800 }
18801 first_file_directive = 1;
18802 }
c5dd6aab
DJ
18803}
18804
18805/* The .loc directive, implying DWARF-2. */
252b5132
RH
18806
18807static void
17a2f251 18808s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 18809{
c5dd6aab
DJ
18810 if (!ECOFF_DEBUGGING)
18811 dwarf2_directive_loc (0);
252b5132
RH
18812}
18813
252b5132
RH
18814/* The .end directive. */
18815
18816static void
17a2f251 18817s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
18818{
18819 symbolS *p;
252b5132 18820
7a621144
DJ
18821 /* Following functions need their own .frame and .cprestore directives. */
18822 mips_frame_reg_valid = 0;
18823 mips_cprestore_valid = 0;
18824
252b5132
RH
18825 if (!is_end_of_line[(unsigned char) *input_line_pointer])
18826 {
18827 p = get_symbol ();
18828 demand_empty_rest_of_line ();
18829 }
18830 else
18831 p = NULL;
18832
14949570 18833 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
18834 as_warn (_(".end not in text section"));
18835
18836 if (!cur_proc_ptr)
18837 {
18838 as_warn (_(".end directive without a preceding .ent directive."));
18839 demand_empty_rest_of_line ();
18840 return;
18841 }
18842
18843 if (p != NULL)
18844 {
9c2799c2 18845 gas_assert (S_GET_NAME (p));
9b2f1d35 18846 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
252b5132 18847 as_warn (_(".end symbol does not match .ent symbol."));
ecb4347a
DJ
18848
18849 if (debug_type == DEBUG_STABS)
18850 stabs_generate_asm_endfunc (S_GET_NAME (p),
18851 S_GET_NAME (p));
252b5132
RH
18852 }
18853 else
18854 as_warn (_(".end directive missing or unknown symbol"));
18855
2132e3a3 18856#ifdef OBJ_ELF
9b2f1d35
EC
18857 /* Create an expression to calculate the size of the function. */
18858 if (p && cur_proc_ptr)
18859 {
18860 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
18861 expressionS *exp = xmalloc (sizeof (expressionS));
18862
18863 obj->size = exp;
18864 exp->X_op = O_subtract;
18865 exp->X_add_symbol = symbol_temp_new_now ();
18866 exp->X_op_symbol = p;
18867 exp->X_add_number = 0;
18868
18869 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
18870 }
18871
ecb4347a 18872 /* Generate a .pdr section. */
f43abd2b 18873 if (IS_ELF && !ECOFF_DEBUGGING && mips_flag_pdr)
ecb4347a
DJ
18874 {
18875 segT saved_seg = now_seg;
18876 subsegT saved_subseg = now_subseg;
ecb4347a
DJ
18877 expressionS exp;
18878 char *fragp;
252b5132 18879
252b5132 18880#ifdef md_flush_pending_output
ecb4347a 18881 md_flush_pending_output ();
252b5132
RH
18882#endif
18883
9c2799c2 18884 gas_assert (pdr_seg);
ecb4347a 18885 subseg_set (pdr_seg, 0);
252b5132 18886
ecb4347a
DJ
18887 /* Write the symbol. */
18888 exp.X_op = O_symbol;
18889 exp.X_add_symbol = p;
18890 exp.X_add_number = 0;
18891 emit_expr (&exp, 4);
252b5132 18892
ecb4347a 18893 fragp = frag_more (7 * 4);
252b5132 18894
17a2f251
TS
18895 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
18896 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
18897 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
18898 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
18899 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
18900 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
18901 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 18902
ecb4347a
DJ
18903 subseg_set (saved_seg, saved_subseg);
18904 }
18905#endif /* OBJ_ELF */
252b5132
RH
18906
18907 cur_proc_ptr = NULL;
18908}
18909
18910/* The .aent and .ent directives. */
18911
18912static void
17a2f251 18913s_mips_ent (int aent)
252b5132 18914{
252b5132 18915 symbolS *symbolP;
252b5132
RH
18916
18917 symbolP = get_symbol ();
18918 if (*input_line_pointer == ',')
f9419b05 18919 ++input_line_pointer;
252b5132 18920 SKIP_WHITESPACE ();
3882b010 18921 if (ISDIGIT (*input_line_pointer)
d9a62219 18922 || *input_line_pointer == '-')
874e8986 18923 get_number ();
252b5132 18924
14949570 18925 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
18926 as_warn (_(".ent or .aent not in text section."));
18927
18928 if (!aent && cur_proc_ptr)
9a41af64 18929 as_warn (_("missing .end"));
252b5132
RH
18930
18931 if (!aent)
18932 {
7a621144
DJ
18933 /* This function needs its own .frame and .cprestore directives. */
18934 mips_frame_reg_valid = 0;
18935 mips_cprestore_valid = 0;
18936
252b5132
RH
18937 cur_proc_ptr = &cur_proc;
18938 memset (cur_proc_ptr, '\0', sizeof (procS));
18939
9b2f1d35 18940 cur_proc_ptr->func_sym = symbolP;
252b5132 18941
f9419b05 18942 ++numprocs;
ecb4347a
DJ
18943
18944 if (debug_type == DEBUG_STABS)
18945 stabs_generate_asm_func (S_GET_NAME (symbolP),
18946 S_GET_NAME (symbolP));
252b5132
RH
18947 }
18948
7c0fc524
MR
18949 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
18950
252b5132
RH
18951 demand_empty_rest_of_line ();
18952}
18953
18954/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 18955 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 18956 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 18957 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
18958 symbol table (in the mdebug section). */
18959
18960static void
17a2f251 18961s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 18962{
ecb4347a 18963#ifdef OBJ_ELF
f43abd2b 18964 if (IS_ELF && !ECOFF_DEBUGGING)
ecb4347a
DJ
18965 {
18966 long val;
252b5132 18967
ecb4347a
DJ
18968 if (cur_proc_ptr == (procS *) NULL)
18969 {
18970 as_warn (_(".frame outside of .ent"));
18971 demand_empty_rest_of_line ();
18972 return;
18973 }
252b5132 18974
ecb4347a
DJ
18975 cur_proc_ptr->frame_reg = tc_get_register (1);
18976
18977 SKIP_WHITESPACE ();
18978 if (*input_line_pointer++ != ','
18979 || get_absolute_expression_and_terminator (&val) != ',')
18980 {
18981 as_warn (_("Bad .frame directive"));
18982 --input_line_pointer;
18983 demand_empty_rest_of_line ();
18984 return;
18985 }
252b5132 18986
ecb4347a
DJ
18987 cur_proc_ptr->frame_offset = val;
18988 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 18989
252b5132 18990 demand_empty_rest_of_line ();
252b5132 18991 }
ecb4347a
DJ
18992 else
18993#endif /* OBJ_ELF */
18994 s_ignore (ignore);
252b5132
RH
18995}
18996
bdaaa2e1
KH
18997/* The .fmask and .mask directives. If the mdebug section is present
18998 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 18999 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 19000 information correctly. We can't use the ecoff routines because they
252b5132
RH
19001 make reference to the ecoff symbol table (in the mdebug section). */
19002
19003static void
17a2f251 19004s_mips_mask (int reg_type)
252b5132 19005{
ecb4347a 19006#ifdef OBJ_ELF
f43abd2b 19007 if (IS_ELF && !ECOFF_DEBUGGING)
252b5132 19008 {
ecb4347a 19009 long mask, off;
252b5132 19010
ecb4347a
DJ
19011 if (cur_proc_ptr == (procS *) NULL)
19012 {
19013 as_warn (_(".mask/.fmask outside of .ent"));
19014 demand_empty_rest_of_line ();
19015 return;
19016 }
252b5132 19017
ecb4347a
DJ
19018 if (get_absolute_expression_and_terminator (&mask) != ',')
19019 {
19020 as_warn (_("Bad .mask/.fmask directive"));
19021 --input_line_pointer;
19022 demand_empty_rest_of_line ();
19023 return;
19024 }
252b5132 19025
ecb4347a
DJ
19026 off = get_absolute_expression ();
19027
19028 if (reg_type == 'F')
19029 {
19030 cur_proc_ptr->fpreg_mask = mask;
19031 cur_proc_ptr->fpreg_offset = off;
19032 }
19033 else
19034 {
19035 cur_proc_ptr->reg_mask = mask;
19036 cur_proc_ptr->reg_offset = off;
19037 }
19038
19039 demand_empty_rest_of_line ();
252b5132
RH
19040 }
19041 else
ecb4347a
DJ
19042#endif /* OBJ_ELF */
19043 s_ignore (reg_type);
252b5132
RH
19044}
19045
316f5878
RS
19046/* A table describing all the processors gas knows about. Names are
19047 matched in the order listed.
e7af610e 19048
316f5878
RS
19049 To ease comparison, please keep this table in the same order as
19050 gcc's mips_cpu_info_table[]. */
e972090a
NC
19051static const struct mips_cpu_info mips_cpu_info_table[] =
19052{
316f5878 19053 /* Entries for generic ISAs */
ad3fea08
TS
19054 { "mips1", MIPS_CPU_IS_ISA, ISA_MIPS1, CPU_R3000 },
19055 { "mips2", MIPS_CPU_IS_ISA, ISA_MIPS2, CPU_R6000 },
19056 { "mips3", MIPS_CPU_IS_ISA, ISA_MIPS3, CPU_R4000 },
19057 { "mips4", MIPS_CPU_IS_ISA, ISA_MIPS4, CPU_R8000 },
19058 { "mips5", MIPS_CPU_IS_ISA, ISA_MIPS5, CPU_MIPS5 },
19059 { "mips32", MIPS_CPU_IS_ISA, ISA_MIPS32, CPU_MIPS32 },
19060 { "mips32r2", MIPS_CPU_IS_ISA, ISA_MIPS32R2, CPU_MIPS32R2 },
19061 { "mips64", MIPS_CPU_IS_ISA, ISA_MIPS64, CPU_MIPS64 },
19062 { "mips64r2", MIPS_CPU_IS_ISA, ISA_MIPS64R2, CPU_MIPS64R2 },
316f5878
RS
19063
19064 /* MIPS I */
ad3fea08
TS
19065 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
19066 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
19067 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
316f5878
RS
19068
19069 /* MIPS II */
ad3fea08 19070 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
316f5878
RS
19071
19072 /* MIPS III */
ad3fea08
TS
19073 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
19074 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
19075 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
19076 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
19077 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
19078 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
19079 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
19080 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
19081 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
19082 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
19083 { "orion", 0, ISA_MIPS3, CPU_R4600 },
19084 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
b15591bb
AN
19085 /* ST Microelectronics Loongson 2E and 2F cores */
19086 { "loongson2e", 0, ISA_MIPS3, CPU_LOONGSON_2E },
19087 { "loongson2f", 0, ISA_MIPS3, CPU_LOONGSON_2F },
316f5878
RS
19088
19089 /* MIPS IV */
ad3fea08
TS
19090 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
19091 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
19092 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
3aa3176b
TS
19093 { "r14000", 0, ISA_MIPS4, CPU_R14000 },
19094 { "r16000", 0, ISA_MIPS4, CPU_R16000 },
ad3fea08
TS
19095 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
19096 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
19097 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
19098 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
19099 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
19100 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
19101 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
19102 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
19103 { "rm7000", 0, ISA_MIPS4, CPU_RM7000 },
19104 { "rm9000", 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
19105
19106 /* MIPS 32 */
ad3fea08
TS
19107 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 },
19108 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
19109 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
19110 { "4ksc", MIPS_CPU_ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
19111
19112 /* MIPS 32 Release 2 */
19113 { "4kec", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19114 { "4kem", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19115 { "4kep", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19116 { "4ksd", MIPS_CPU_ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
19117 { "m4k", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19118 { "m4kp", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
b5503c7b
MR
19119 { "m14k", MIPS_CPU_ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19120 { "m14kc", MIPS_CPU_ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
7a795ef4
MR
19121 { "m14ke", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2 | MIPS_CPU_ASE_MCU,
19122 ISA_MIPS32R2, CPU_MIPS32R2 },
19123 { "m14kec", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2 | MIPS_CPU_ASE_MCU,
19124 ISA_MIPS32R2, CPU_MIPS32R2 },
ad3fea08 19125 { "24kc", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19126 { "24kf2_1", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
ad3fea08 19127 { "24kf", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
19128 { "24kf1_1", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19129 /* Deprecated forms of the above. */
19130 { "24kfx", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
ad3fea08 19131 { "24kx", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19132 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
ad3fea08 19133 { "24kec", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19134 { "24kef2_1", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
ad3fea08 19135 { "24kef", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
19136 { "24kef1_1", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19137 /* Deprecated forms of the above. */
19138 { "24kefx", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
65263ce3 19139 { "24kex", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19140 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
a360e743
TS
19141 { "34kc", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19142 ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
19143 { "34kf2_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19144 ISA_MIPS32R2, CPU_MIPS32R2 },
a360e743
TS
19145 { "34kf", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19146 ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
19147 { "34kf1_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19148 ISA_MIPS32R2, CPU_MIPS32R2 },
19149 /* Deprecated forms of the above. */
19150 { "34kfx", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19151 ISA_MIPS32R2, CPU_MIPS32R2 },
a360e743
TS
19152 { "34kx", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19153 ISA_MIPS32R2, CPU_MIPS32R2 },
711eefe4
SL
19154 /* 34Kn is a 34kc without DSP. */
19155 { "34kn", MIPS_CPU_ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f
TS
19156 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
19157 { "74kc", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19158 ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
19159 { "74kf2_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19160 ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f
TS
19161 { "74kf", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19162 ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
19163 { "74kf1_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19164 ISA_MIPS32R2, CPU_MIPS32R2 },
19165 { "74kf3_2", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19166 ISA_MIPS32R2, CPU_MIPS32R2 },
19167 /* Deprecated forms of the above. */
19168 { "74kfx", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19169 ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f
TS
19170 { "74kx", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19171 ISA_MIPS32R2, CPU_MIPS32R2 },
30f8113a
SL
19172 /* 1004K cores are multiprocessor versions of the 34K. */
19173 { "1004kc", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19174 ISA_MIPS32R2, CPU_MIPS32R2 },
19175 { "1004kf2_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19176 ISA_MIPS32R2, CPU_MIPS32R2 },
19177 { "1004kf", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19178 ISA_MIPS32R2, CPU_MIPS32R2 },
19179 { "1004kf1_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19180 ISA_MIPS32R2, CPU_MIPS32R2 },
32b26a03 19181
316f5878 19182 /* MIPS 64 */
ad3fea08
TS
19183 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
19184 { "5kf", 0, ISA_MIPS64, CPU_MIPS64 },
19185 { "20kc", MIPS_CPU_ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
7764b395 19186 { "25kf", MIPS_CPU_ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
ad3fea08 19187
c7a23324 19188 /* Broadcom SB-1 CPU core */
65263ce3
TS
19189 { "sb1", MIPS_CPU_ASE_MIPS3D | MIPS_CPU_ASE_MDMX,
19190 ISA_MIPS64, CPU_SB1 },
1e85aad8
JW
19191 /* Broadcom SB-1A CPU core */
19192 { "sb1a", MIPS_CPU_ASE_MIPS3D | MIPS_CPU_ASE_MDMX,
19193 ISA_MIPS64, CPU_SB1 },
d051516a
NC
19194
19195 { "loongson3a", 0, ISA_MIPS64, CPU_LOONGSON_3A },
e7af610e 19196
ed163775
MR
19197 /* MIPS 64 Release 2 */
19198
967344c6
AN
19199 /* Cavium Networks Octeon CPU core */
19200 { "octeon", 0, ISA_MIPS64R2, CPU_OCTEON },
dd6a37e7 19201 { "octeon+", 0, ISA_MIPS64R2, CPU_OCTEONP },
432233b3 19202 { "octeon2", 0, ISA_MIPS64R2, CPU_OCTEON2 },
967344c6 19203
52b6b6b9
JM
19204 /* RMI Xlr */
19205 { "xlr", 0, ISA_MIPS64, CPU_XLR },
19206
55a36193
MK
19207 /* Broadcom XLP.
19208 XLP is mostly like XLR, with the prominent exception that it is
19209 MIPS64R2 rather than MIPS64. */
19210 { "xlp", 0, ISA_MIPS64R2, CPU_XLR },
19211
316f5878
RS
19212 /* End marker */
19213 { NULL, 0, 0, 0 }
19214};
e7af610e 19215
84ea6cf2 19216
316f5878
RS
19217/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
19218 with a final "000" replaced by "k". Ignore case.
e7af610e 19219
316f5878 19220 Note: this function is shared between GCC and GAS. */
c6c98b38 19221
b34976b6 19222static bfd_boolean
17a2f251 19223mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
19224{
19225 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
19226 given++, canonical++;
19227
19228 return ((*given == 0 && *canonical == 0)
19229 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
19230}
19231
19232
19233/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
19234 CPU name. We've traditionally allowed a lot of variation here.
19235
19236 Note: this function is shared between GCC and GAS. */
19237
b34976b6 19238static bfd_boolean
17a2f251 19239mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
19240{
19241 /* First see if the name matches exactly, or with a final "000"
19242 turned into "k". */
19243 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 19244 return TRUE;
316f5878
RS
19245
19246 /* If not, try comparing based on numerical designation alone.
19247 See if GIVEN is an unadorned number, or 'r' followed by a number. */
19248 if (TOLOWER (*given) == 'r')
19249 given++;
19250 if (!ISDIGIT (*given))
b34976b6 19251 return FALSE;
316f5878
RS
19252
19253 /* Skip over some well-known prefixes in the canonical name,
19254 hoping to find a number there too. */
19255 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
19256 canonical += 2;
19257 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
19258 canonical += 2;
19259 else if (TOLOWER (canonical[0]) == 'r')
19260 canonical += 1;
19261
19262 return mips_strict_matching_cpu_name_p (canonical, given);
19263}
19264
19265
19266/* Parse an option that takes the name of a processor as its argument.
19267 OPTION is the name of the option and CPU_STRING is the argument.
19268 Return the corresponding processor enumeration if the CPU_STRING is
19269 recognized, otherwise report an error and return null.
19270
19271 A similar function exists in GCC. */
e7af610e
NC
19272
19273static const struct mips_cpu_info *
17a2f251 19274mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 19275{
316f5878 19276 const struct mips_cpu_info *p;
e7af610e 19277
316f5878
RS
19278 /* 'from-abi' selects the most compatible architecture for the given
19279 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
19280 EABIs, we have to decide whether we're using the 32-bit or 64-bit
19281 version. Look first at the -mgp options, if given, otherwise base
19282 the choice on MIPS_DEFAULT_64BIT.
e7af610e 19283
316f5878
RS
19284 Treat NO_ABI like the EABIs. One reason to do this is that the
19285 plain 'mips' and 'mips64' configs have 'from-abi' as their default
19286 architecture. This code picks MIPS I for 'mips' and MIPS III for
19287 'mips64', just as we did in the days before 'from-abi'. */
19288 if (strcasecmp (cpu_string, "from-abi") == 0)
19289 {
19290 if (ABI_NEEDS_32BIT_REGS (mips_abi))
19291 return mips_cpu_info_from_isa (ISA_MIPS1);
19292
19293 if (ABI_NEEDS_64BIT_REGS (mips_abi))
19294 return mips_cpu_info_from_isa (ISA_MIPS3);
19295
19296 if (file_mips_gp32 >= 0)
19297 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
19298
19299 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
19300 ? ISA_MIPS3
19301 : ISA_MIPS1);
19302 }
19303
19304 /* 'default' has traditionally been a no-op. Probably not very useful. */
19305 if (strcasecmp (cpu_string, "default") == 0)
19306 return 0;
19307
19308 for (p = mips_cpu_info_table; p->name != 0; p++)
19309 if (mips_matching_cpu_name_p (p->name, cpu_string))
19310 return p;
19311
20203fb9 19312 as_bad (_("Bad value (%s) for %s"), cpu_string, option);
316f5878 19313 return 0;
e7af610e
NC
19314}
19315
316f5878
RS
19316/* Return the canonical processor information for ISA (a member of the
19317 ISA_MIPS* enumeration). */
19318
e7af610e 19319static const struct mips_cpu_info *
17a2f251 19320mips_cpu_info_from_isa (int isa)
e7af610e
NC
19321{
19322 int i;
19323
19324 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
ad3fea08 19325 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
316f5878 19326 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
19327 return (&mips_cpu_info_table[i]);
19328
e972090a 19329 return NULL;
e7af610e 19330}
fef14a42
TS
19331
19332static const struct mips_cpu_info *
17a2f251 19333mips_cpu_info_from_arch (int arch)
fef14a42
TS
19334{
19335 int i;
19336
19337 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19338 if (arch == mips_cpu_info_table[i].cpu)
19339 return (&mips_cpu_info_table[i]);
19340
19341 return NULL;
19342}
316f5878
RS
19343\f
19344static void
17a2f251 19345show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
19346{
19347 if (*first_p)
19348 {
19349 fprintf (stream, "%24s", "");
19350 *col_p = 24;
19351 }
19352 else
19353 {
19354 fprintf (stream, ", ");
19355 *col_p += 2;
19356 }
e7af610e 19357
316f5878
RS
19358 if (*col_p + strlen (string) > 72)
19359 {
19360 fprintf (stream, "\n%24s", "");
19361 *col_p = 24;
19362 }
19363
19364 fprintf (stream, "%s", string);
19365 *col_p += strlen (string);
19366
19367 *first_p = 0;
19368}
19369
19370void
17a2f251 19371md_show_usage (FILE *stream)
e7af610e 19372{
316f5878
RS
19373 int column, first;
19374 size_t i;
19375
19376 fprintf (stream, _("\
19377MIPS options:\n\
316f5878
RS
19378-EB generate big endian output\n\
19379-EL generate little endian output\n\
19380-g, -g2 do not remove unneeded NOPs or swap branches\n\
19381-G NUM allow referencing objects up to NUM bytes\n\
19382 implicitly with the gp register [default 8]\n"));
19383 fprintf (stream, _("\
19384-mips1 generate MIPS ISA I instructions\n\
19385-mips2 generate MIPS ISA II instructions\n\
19386-mips3 generate MIPS ISA III instructions\n\
19387-mips4 generate MIPS ISA IV instructions\n\
19388-mips5 generate MIPS ISA V instructions\n\
19389-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 19390-mips32r2 generate MIPS32 release 2 ISA instructions\n\
316f5878 19391-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 19392-mips64r2 generate MIPS64 release 2 ISA instructions\n\
316f5878
RS
19393-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
19394
19395 first = 1;
e7af610e
NC
19396
19397 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
19398 show (stream, mips_cpu_info_table[i].name, &column, &first);
19399 show (stream, "from-abi", &column, &first);
19400 fputc ('\n', stream);
e7af610e 19401
316f5878
RS
19402 fprintf (stream, _("\
19403-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
19404-no-mCPU don't generate code specific to CPU.\n\
19405 For -mCPU and -no-mCPU, CPU must be one of:\n"));
19406
19407 first = 1;
19408
19409 show (stream, "3900", &column, &first);
19410 show (stream, "4010", &column, &first);
19411 show (stream, "4100", &column, &first);
19412 show (stream, "4650", &column, &first);
19413 fputc ('\n', stream);
19414
19415 fprintf (stream, _("\
19416-mips16 generate mips16 instructions\n\
19417-no-mips16 do not generate mips16 instructions\n"));
19418 fprintf (stream, _("\
df58fc94
RS
19419-mmicromips generate microMIPS instructions\n\
19420-mno-micromips do not generate microMIPS instructions\n"));
19421 fprintf (stream, _("\
e16bfa71
TS
19422-msmartmips generate smartmips instructions\n\
19423-mno-smartmips do not generate smartmips instructions\n"));
19424 fprintf (stream, _("\
74cd071d
CF
19425-mdsp generate DSP instructions\n\
19426-mno-dsp do not generate DSP instructions\n"));
19427 fprintf (stream, _("\
8b082fb1
TS
19428-mdspr2 generate DSP R2 instructions\n\
19429-mno-dspr2 do not generate DSP R2 instructions\n"));
19430 fprintf (stream, _("\
ef2e4d86
CF
19431-mmt generate MT instructions\n\
19432-mno-mt do not generate MT instructions\n"));
19433 fprintf (stream, _("\
dec0624d
MR
19434-mmcu generate MCU instructions\n\
19435-mno-mcu do not generate MCU instructions\n"));
19436 fprintf (stream, _("\
c67a084a
NC
19437-mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
19438-mfix-loongson2f-nop work around Loongson2F NOP errata\n\
d766e8ec 19439-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 19440-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
6a32d874 19441-mfix-24k insert a nop after ERET and DERET instructions\n\
d954098f 19442-mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
316f5878
RS
19443-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
19444-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261 19445-msym32 assume all symbols have 32-bit values\n\
316f5878
RS
19446-O0 remove unneeded NOPs, do not swap branches\n\
19447-O remove unneeded NOPs and swap branches\n\
316f5878
RS
19448--trap, --no-break trap exception on div by 0 and mult overflow\n\
19449--break, --no-trap break exception on div by 0 and mult overflow\n"));
037b32b9
AN
19450 fprintf (stream, _("\
19451-mhard-float allow floating-point instructions\n\
19452-msoft-float do not allow floating-point instructions\n\
19453-msingle-float only allow 32-bit floating-point operations\n\
19454-mdouble-float allow 32-bit and 64-bit floating-point operations\n\
19455--[no-]construct-floats [dis]allow floating point values to be constructed\n"
19456 ));
316f5878
RS
19457#ifdef OBJ_ELF
19458 fprintf (stream, _("\
19459-KPIC, -call_shared generate SVR4 position independent code\n\
861fb55a 19460-call_nonpic generate non-PIC code that can operate with DSOs\n\
0c000745 19461-mvxworks-pic generate VxWorks position independent code\n\
861fb55a 19462-non_shared do not generate code that can operate with DSOs\n\
316f5878 19463-xgot assume a 32 bit GOT\n\
dcd410fe 19464-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8 19465-mshared, -mno-shared disable/enable .cpload optimization for\n\
d821e36b 19466 position dependent (non shared) code\n\
316f5878
RS
19467-mabi=ABI create ABI conformant object file for:\n"));
19468
19469 first = 1;
19470
19471 show (stream, "32", &column, &first);
19472 show (stream, "o64", &column, &first);
19473 show (stream, "n32", &column, &first);
19474 show (stream, "64", &column, &first);
19475 show (stream, "eabi", &column, &first);
19476
19477 fputc ('\n', stream);
19478
19479 fprintf (stream, _("\
19480-32 create o32 ABI object file (default)\n\
19481-n32 create n32 ABI object file\n\
19482-64 create 64 ABI object file\n"));
19483#endif
e7af610e 19484}
14e777e0 19485
1575952e 19486#ifdef TE_IRIX
14e777e0 19487enum dwarf2_format
413a266c 19488mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
14e777e0 19489{
369943fe 19490 if (HAVE_64BIT_SYMBOLS)
1575952e 19491 return dwarf2_format_64bit_irix;
14e777e0
KB
19492 else
19493 return dwarf2_format_32bit;
19494}
1575952e 19495#endif
73369e65
EC
19496
19497int
19498mips_dwarf2_addr_size (void)
19499{
6b6b3450 19500 if (HAVE_64BIT_OBJECTS)
73369e65 19501 return 8;
73369e65
EC
19502 else
19503 return 4;
19504}
5862107c
EC
19505
19506/* Standard calling conventions leave the CFA at SP on entry. */
19507void
19508mips_cfi_frame_initial_instructions (void)
19509{
19510 cfi_add_CFA_def_cfa_register (SP);
19511}
19512
707bfff6
TS
19513int
19514tc_mips_regname_to_dw2regnum (char *regname)
19515{
19516 unsigned int regnum = -1;
19517 unsigned int reg;
19518
19519 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
19520 regnum = reg;
19521
19522 return regnum;
19523}
This page took 2.591043 seconds and 4 git commands to generate.