daily update
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
CommitLineData
252b5132 1/* tc-mips.c -- assemble code for a MIPS chip.
81912461 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
e407c74b 3 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
c67a084a 4 Free Software Foundation, Inc.
252b5132
RH
5 Contributed by the OSF and Ralph Campbell.
6 Written by Keith Knowles and Ralph Campbell, working independently.
7 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
8 Support.
9
10 This file is part of GAS.
11
12 GAS is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
ec2655a6 14 the Free Software Foundation; either version 3, or (at your option)
252b5132
RH
15 any later version.
16
17 GAS is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with GAS; see the file COPYING. If not, write to the Free
4b4da160
NC
24 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
25 02110-1301, USA. */
252b5132
RH
26
27#include "as.h"
28#include "config.h"
29#include "subsegs.h"
3882b010 30#include "safe-ctype.h"
252b5132 31
252b5132
RH
32#include "opcode/mips.h"
33#include "itbl-ops.h"
c5dd6aab 34#include "dwarf2dbg.h"
5862107c 35#include "dw2gencfi.h"
252b5132 36
42429eac
RS
37/* Check assumptions made in this file. */
38typedef char static_assert1[sizeof (offsetT) < 8 ? -1 : 1];
39typedef char static_assert2[sizeof (valueT) < 8 ? -1 : 1];
40
252b5132
RH
41#ifdef DEBUG
42#define DBG(x) printf x
43#else
44#define DBG(x)
45#endif
46
9e12b7a2
RS
47#define SKIP_SPACE_TABS(S) \
48 do { while (*(S) == ' ' || *(S) == '\t') ++(S); } while (0)
49
252b5132 50/* Clean up namespace so we can include obj-elf.h too. */
17a2f251
TS
51static int mips_output_flavor (void);
52static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
252b5132
RH
53#undef OBJ_PROCESS_STAB
54#undef OUTPUT_FLAVOR
55#undef S_GET_ALIGN
56#undef S_GET_SIZE
57#undef S_SET_ALIGN
58#undef S_SET_SIZE
252b5132
RH
59#undef obj_frob_file
60#undef obj_frob_file_after_relocs
61#undef obj_frob_symbol
62#undef obj_pop_insert
63#undef obj_sec_sym_ok_for_reloc
64#undef OBJ_COPY_SYMBOL_ATTRIBUTES
65
66#include "obj-elf.h"
67/* Fix any of them that we actually care about. */
68#undef OUTPUT_FLAVOR
69#define OUTPUT_FLAVOR mips_output_flavor()
252b5132 70
252b5132 71#include "elf/mips.h"
252b5132
RH
72
73#ifndef ECOFF_DEBUGGING
74#define NO_ECOFF_DEBUGGING
75#define ECOFF_DEBUGGING 0
76#endif
77
ecb4347a
DJ
78int mips_flag_mdebug = -1;
79
dcd410fe
RO
80/* Control generation of .pdr sections. Off by default on IRIX: the native
81 linker doesn't know about and discards them, but relocations against them
82 remain, leading to rld crashes. */
83#ifdef TE_IRIX
84int mips_flag_pdr = FALSE;
85#else
86int mips_flag_pdr = TRUE;
87#endif
88
252b5132
RH
89#include "ecoff.h"
90
252b5132 91static char *mips_regmask_frag;
252b5132 92
85b51719 93#define ZERO 0
741fe287 94#define ATREG 1
df58fc94
RS
95#define S0 16
96#define S7 23
252b5132
RH
97#define TREG 24
98#define PIC_CALL_REG 25
99#define KT0 26
100#define KT1 27
101#define GP 28
102#define SP 29
103#define FP 30
104#define RA 31
105
106#define ILLEGAL_REG (32)
107
741fe287
MR
108#define AT mips_opts.at
109
252b5132
RH
110extern int target_big_endian;
111
252b5132 112/* The name of the readonly data section. */
e8044f35 113#define RDATA_SECTION_NAME ".rodata"
252b5132 114
a4e06468
RS
115/* Ways in which an instruction can be "appended" to the output. */
116enum append_method {
117 /* Just add it normally. */
118 APPEND_ADD,
119
120 /* Add it normally and then add a nop. */
121 APPEND_ADD_WITH_NOP,
122
123 /* Turn an instruction with a delay slot into a "compact" version. */
124 APPEND_ADD_COMPACT,
125
126 /* Insert the instruction before the last one. */
127 APPEND_SWAP
128};
129
47e39b9d
RS
130/* Information about an instruction, including its format, operands
131 and fixups. */
132struct mips_cl_insn
133{
134 /* The opcode's entry in mips_opcodes or mips16_opcodes. */
135 const struct mips_opcode *insn_mo;
136
47e39b9d 137 /* The 16-bit or 32-bit bitstring of the instruction itself. This is
5c04167a
RS
138 a copy of INSN_MO->match with the operands filled in. If we have
139 decided to use an extended MIPS16 instruction, this includes the
140 extension. */
47e39b9d
RS
141 unsigned long insn_opcode;
142
143 /* The frag that contains the instruction. */
144 struct frag *frag;
145
146 /* The offset into FRAG of the first instruction byte. */
147 long where;
148
149 /* The relocs associated with the instruction, if any. */
150 fixS *fixp[3];
151
a38419a5
RS
152 /* True if this entry cannot be moved from its current position. */
153 unsigned int fixed_p : 1;
47e39b9d 154
708587a4 155 /* True if this instruction occurred in a .set noreorder block. */
47e39b9d
RS
156 unsigned int noreorder_p : 1;
157
2fa15973
RS
158 /* True for mips16 instructions that jump to an absolute address. */
159 unsigned int mips16_absolute_jump_p : 1;
15be625d
CM
160
161 /* True if this instruction is complete. */
162 unsigned int complete_p : 1;
e407c74b
NC
163
164 /* True if this instruction is cleared from history by unconditional
165 branch. */
166 unsigned int cleared_p : 1;
47e39b9d
RS
167};
168
a325df1d
TS
169/* The ABI to use. */
170enum mips_abi_level
171{
172 NO_ABI = 0,
173 O32_ABI,
174 O64_ABI,
175 N32_ABI,
176 N64_ABI,
177 EABI_ABI
178};
179
180/* MIPS ABI we are using for this output file. */
316f5878 181static enum mips_abi_level mips_abi = NO_ABI;
a325df1d 182
143d77c5
EC
183/* Whether or not we have code that can call pic code. */
184int mips_abicalls = FALSE;
185
aa6975fb
ILT
186/* Whether or not we have code which can be put into a shared
187 library. */
188static bfd_boolean mips_in_shared = TRUE;
189
252b5132
RH
190/* This is the set of options which may be modified by the .set
191 pseudo-op. We use a struct so that .set push and .set pop are more
192 reliable. */
193
e972090a
NC
194struct mips_set_options
195{
252b5132
RH
196 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
197 if it has not been initialized. Changed by `.set mipsN', and the
198 -mipsN command line option, and the default CPU. */
199 int isa;
846ef2d0
RS
200 /* Enabled Application Specific Extensions (ASEs). Changed by `.set
201 <asename>', by command line options, and based on the default
202 architecture. */
203 int ase;
252b5132
RH
204 /* Whether we are assembling for the mips16 processor. 0 if we are
205 not, 1 if we are, and -1 if the value has not been initialized.
206 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
207 -nomips16 command line options, and the default CPU. */
208 int mips16;
df58fc94
RS
209 /* Whether we are assembling for the mipsMIPS ASE. 0 if we are not,
210 1 if we are, and -1 if the value has not been initialized. Changed
211 by `.set micromips' and `.set nomicromips', and the -mmicromips
212 and -mno-micromips command line options, and the default CPU. */
213 int micromips;
252b5132
RH
214 /* Non-zero if we should not reorder instructions. Changed by `.set
215 reorder' and `.set noreorder'. */
216 int noreorder;
741fe287
MR
217 /* Non-zero if we should not permit the register designated "assembler
218 temporary" to be used in instructions. The value is the register
219 number, normally $at ($1). Changed by `.set at=REG', `.set noat'
220 (same as `.set at=$0') and `.set at' (same as `.set at=$1'). */
221 unsigned int at;
252b5132
RH
222 /* Non-zero if we should warn when a macro instruction expands into
223 more than one machine instruction. Changed by `.set nomacro' and
224 `.set macro'. */
225 int warn_about_macros;
226 /* Non-zero if we should not move instructions. Changed by `.set
227 move', `.set volatile', `.set nomove', and `.set novolatile'. */
228 int nomove;
229 /* Non-zero if we should not optimize branches by moving the target
230 of the branch into the delay slot. Actually, we don't perform
231 this optimization anyhow. Changed by `.set bopt' and `.set
232 nobopt'. */
233 int nobopt;
234 /* Non-zero if we should not autoextend mips16 instructions.
235 Changed by `.set autoextend' and `.set noautoextend'. */
236 int noautoextend;
833794fc
MR
237 /* True if we should only emit 32-bit microMIPS instructions.
238 Changed by `.set insn32' and `.set noinsn32', and the -minsn32
239 and -mno-insn32 command line options. */
240 bfd_boolean insn32;
a325df1d
TS
241 /* Restrict general purpose registers and floating point registers
242 to 32 bit. This is initially determined when -mgp32 or -mfp32
243 is passed but can changed if the assembler code uses .set mipsN. */
244 int gp32;
245 int fp32;
fef14a42
TS
246 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
247 command line option, and the default CPU. */
248 int arch;
aed1a261
RS
249 /* True if ".set sym32" is in effect. */
250 bfd_boolean sym32;
037b32b9
AN
251 /* True if floating-point operations are not allowed. Changed by .set
252 softfloat or .set hardfloat, by command line options -msoft-float or
253 -mhard-float. The default is false. */
254 bfd_boolean soft_float;
255
256 /* True if only single-precision floating-point operations are allowed.
257 Changed by .set singlefloat or .set doublefloat, command-line options
258 -msingle-float or -mdouble-float. The default is false. */
259 bfd_boolean single_float;
252b5132
RH
260};
261
037b32b9
AN
262/* This is the struct we use to hold the current set of options. Note
263 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
264 -1 to indicate that they have not been initialized. */
265
a325df1d 266/* True if -mgp32 was passed. */
a8e8e863 267static int file_mips_gp32 = -1;
a325df1d
TS
268
269/* True if -mfp32 was passed. */
a8e8e863 270static int file_mips_fp32 = -1;
a325df1d 271
037b32b9
AN
272/* 1 if -msoft-float, 0 if -mhard-float. The default is 0. */
273static int file_mips_soft_float = 0;
274
275/* 1 if -msingle-float, 0 if -mdouble-float. The default is 0. */
276static int file_mips_single_float = 0;
252b5132 277
ba92f887
MR
278/* True if -mnan=2008, false if -mnan=legacy. */
279static bfd_boolean mips_flag_nan2008 = FALSE;
280
e972090a
NC
281static struct mips_set_options mips_opts =
282{
846ef2d0 283 /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
b015e599 284 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
833794fc
MR
285 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
286 /* gp32 */ 0, /* fp32 */ 0, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
b015e599 287 /* soft_float */ FALSE, /* single_float */ FALSE
e7af610e 288};
252b5132 289
846ef2d0
RS
290/* The set of ASEs that were selected on the command line, either
291 explicitly via ASE options or implicitly through things like -march. */
292static unsigned int file_ase;
293
294/* Which bits of file_ase were explicitly set or cleared by ASE options. */
295static unsigned int file_ase_explicit;
296
252b5132
RH
297/* These variables are filled in with the masks of registers used.
298 The object format code reads them and puts them in the appropriate
299 place. */
300unsigned long mips_gprmask;
301unsigned long mips_cprmask[4];
302
303/* MIPS ISA we are using for this output file. */
e7af610e 304static int file_mips_isa = ISA_UNKNOWN;
252b5132 305
738f4d98 306/* True if any MIPS16 code was produced. */
a4672219
TS
307static int file_ase_mips16;
308
3994f87e
TS
309#define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32 \
310 || mips_opts.isa == ISA_MIPS32R2 \
311 || mips_opts.isa == ISA_MIPS64 \
312 || mips_opts.isa == ISA_MIPS64R2)
313
df58fc94
RS
314/* True if any microMIPS code was produced. */
315static int file_ase_micromips;
316
b12dd2e4
CF
317/* True if we want to create R_MIPS_JALR for jalr $25. */
318#ifdef TE_IRIX
1180b5a4 319#define MIPS_JALR_HINT_P(EXPR) HAVE_NEWABI
b12dd2e4 320#else
1180b5a4
RS
321/* As a GNU extension, we use R_MIPS_JALR for o32 too. However,
322 because there's no place for any addend, the only acceptable
323 expression is a bare symbol. */
324#define MIPS_JALR_HINT_P(EXPR) \
325 (!HAVE_IN_PLACE_ADDENDS \
326 || ((EXPR)->X_op == O_symbol && (EXPR)->X_add_number == 0))
b12dd2e4
CF
327#endif
328
ec68c924 329/* The argument of the -march= flag. The architecture we are assembling. */
fef14a42 330static int file_mips_arch = CPU_UNKNOWN;
316f5878 331static const char *mips_arch_string;
ec68c924
EC
332
333/* The argument of the -mtune= flag. The architecture for which we
334 are optimizing. */
335static int mips_tune = CPU_UNKNOWN;
316f5878 336static const char *mips_tune_string;
ec68c924 337
316f5878 338/* True when generating 32-bit code for a 64-bit processor. */
252b5132
RH
339static int mips_32bitmode = 0;
340
316f5878
RS
341/* True if the given ABI requires 32-bit registers. */
342#define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
343
344/* Likewise 64-bit registers. */
707bfff6
TS
345#define ABI_NEEDS_64BIT_REGS(ABI) \
346 ((ABI) == N32_ABI \
347 || (ABI) == N64_ABI \
316f5878
RS
348 || (ABI) == O64_ABI)
349
ad3fea08 350/* Return true if ISA supports 64 bit wide gp registers. */
707bfff6
TS
351#define ISA_HAS_64BIT_REGS(ISA) \
352 ((ISA) == ISA_MIPS3 \
353 || (ISA) == ISA_MIPS4 \
354 || (ISA) == ISA_MIPS5 \
355 || (ISA) == ISA_MIPS64 \
356 || (ISA) == ISA_MIPS64R2)
9ce8a5dd 357
ad3fea08
TS
358/* Return true if ISA supports 64 bit wide float registers. */
359#define ISA_HAS_64BIT_FPRS(ISA) \
360 ((ISA) == ISA_MIPS3 \
361 || (ISA) == ISA_MIPS4 \
362 || (ISA) == ISA_MIPS5 \
363 || (ISA) == ISA_MIPS32R2 \
364 || (ISA) == ISA_MIPS64 \
365 || (ISA) == ISA_MIPS64R2)
366
af7ee8bf
CD
367/* Return true if ISA supports 64-bit right rotate (dror et al.)
368 instructions. */
707bfff6 369#define ISA_HAS_DROR(ISA) \
df58fc94
RS
370 ((ISA) == ISA_MIPS64R2 \
371 || (mips_opts.micromips \
372 && ISA_HAS_64BIT_REGS (ISA)) \
373 )
af7ee8bf
CD
374
375/* Return true if ISA supports 32-bit right rotate (ror et al.)
376 instructions. */
707bfff6
TS
377#define ISA_HAS_ROR(ISA) \
378 ((ISA) == ISA_MIPS32R2 \
379 || (ISA) == ISA_MIPS64R2 \
846ef2d0 380 || (mips_opts.ase & ASE_SMARTMIPS) \
df58fc94
RS
381 || mips_opts.micromips \
382 )
707bfff6 383
7455baf8
TS
384/* Return true if ISA supports single-precision floats in odd registers. */
385#define ISA_HAS_ODD_SINGLE_FPR(ISA) \
386 ((ISA) == ISA_MIPS32 \
387 || (ISA) == ISA_MIPS32R2 \
388 || (ISA) == ISA_MIPS64 \
389 || (ISA) == ISA_MIPS64R2)
af7ee8bf 390
ad3fea08
TS
391/* Return true if ISA supports move to/from high part of a 64-bit
392 floating-point register. */
393#define ISA_HAS_MXHC1(ISA) \
394 ((ISA) == ISA_MIPS32R2 \
395 || (ISA) == ISA_MIPS64R2)
396
e013f690 397#define HAVE_32BIT_GPRS \
ad3fea08 398 (mips_opts.gp32 || !ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257 399
e013f690 400#define HAVE_32BIT_FPRS \
ad3fea08 401 (mips_opts.fp32 || !ISA_HAS_64BIT_FPRS (mips_opts.isa))
ca4e0257 402
ad3fea08
TS
403#define HAVE_64BIT_GPRS (!HAVE_32BIT_GPRS)
404#define HAVE_64BIT_FPRS (!HAVE_32BIT_FPRS)
ca4e0257 405
316f5878 406#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
e013f690 407
316f5878 408#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
e013f690 409
3b91255e
RS
410/* True if relocations are stored in-place. */
411#define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
412
aed1a261
RS
413/* The ABI-derived address size. */
414#define HAVE_64BIT_ADDRESSES \
415 (HAVE_64BIT_GPRS && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
416#define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
e013f690 417
aed1a261
RS
418/* The size of symbolic constants (i.e., expressions of the form
419 "SYMBOL" or "SYMBOL + OFFSET"). */
420#define HAVE_32BIT_SYMBOLS \
421 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
422#define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
ca4e0257 423
b7c7d6c1
TS
424/* Addresses are loaded in different ways, depending on the address size
425 in use. The n32 ABI Documentation also mandates the use of additions
426 with overflow checking, but existing implementations don't follow it. */
f899b4b8 427#define ADDRESS_ADD_INSN \
b7c7d6c1 428 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
f899b4b8
TS
429
430#define ADDRESS_ADDI_INSN \
b7c7d6c1 431 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
f899b4b8
TS
432
433#define ADDRESS_LOAD_INSN \
434 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
435
436#define ADDRESS_STORE_INSN \
437 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
438
a4672219 439/* Return true if the given CPU supports the MIPS16 ASE. */
3396de36
TS
440#define CPU_HAS_MIPS16(cpu) \
441 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
442 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
a4672219 443
2309ddf2 444/* Return true if the given CPU supports the microMIPS ASE. */
df58fc94
RS
445#define CPU_HAS_MICROMIPS(cpu) 0
446
60b63b72
RS
447/* True if CPU has a dror instruction. */
448#define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
449
450/* True if CPU has a ror instruction. */
451#define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
452
dd6a37e7 453/* True if CPU is in the Octeon family */
432233b3 454#define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP || (CPU) == CPU_OCTEON2)
dd6a37e7 455
dd3cbb7e 456/* True if CPU has seq/sne and seqi/snei instructions. */
dd6a37e7 457#define CPU_HAS_SEQ(CPU) (CPU_IS_OCTEON (CPU))
dd3cbb7e 458
0aa27725
RS
459/* True, if CPU has support for ldc1 and sdc1. */
460#define CPU_HAS_LDC1_SDC1(CPU) \
461 ((mips_opts.isa != ISA_MIPS1) && ((CPU) != CPU_R5900))
462
c8978940
CD
463/* True if mflo and mfhi can be immediately followed by instructions
464 which write to the HI and LO registers.
465
466 According to MIPS specifications, MIPS ISAs I, II, and III need
467 (at least) two instructions between the reads of HI/LO and
468 instructions which write them, and later ISAs do not. Contradicting
469 the MIPS specifications, some MIPS IV processor user manuals (e.g.
470 the UM for the NEC Vr5000) document needing the instructions between
471 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
472 MIPS64 and later ISAs to have the interlocks, plus any specific
473 earlier-ISA CPUs for which CPU documentation declares that the
474 instructions are really interlocked. */
475#define hilo_interlocks \
476 (mips_opts.isa == ISA_MIPS32 \
477 || mips_opts.isa == ISA_MIPS32R2 \
478 || mips_opts.isa == ISA_MIPS64 \
479 || mips_opts.isa == ISA_MIPS64R2 \
480 || mips_opts.arch == CPU_R4010 \
e407c74b 481 || mips_opts.arch == CPU_R5900 \
c8978940
CD
482 || mips_opts.arch == CPU_R10000 \
483 || mips_opts.arch == CPU_R12000 \
3aa3176b
TS
484 || mips_opts.arch == CPU_R14000 \
485 || mips_opts.arch == CPU_R16000 \
c8978940 486 || mips_opts.arch == CPU_RM7000 \
c8978940 487 || mips_opts.arch == CPU_VR5500 \
df58fc94 488 || mips_opts.micromips \
c8978940 489 )
252b5132
RH
490
491/* Whether the processor uses hardware interlocks to protect reads
81912461
ILT
492 from the GPRs after they are loaded from memory, and thus does not
493 require nops to be inserted. This applies to instructions marked
494 INSN_LOAD_MEMORY_DELAY. These nops are only required at MIPS ISA
df58fc94
RS
495 level I and microMIPS mode instructions are always interlocked. */
496#define gpr_interlocks \
497 (mips_opts.isa != ISA_MIPS1 \
498 || mips_opts.arch == CPU_R3900 \
e407c74b 499 || mips_opts.arch == CPU_R5900 \
df58fc94
RS
500 || mips_opts.micromips \
501 )
252b5132 502
81912461
ILT
503/* Whether the processor uses hardware interlocks to avoid delays
504 required by coprocessor instructions, and thus does not require
505 nops to be inserted. This applies to instructions marked
506 INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
507 between instructions marked INSN_WRITE_COND_CODE and ones marked
508 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
df58fc94
RS
509 levels I, II, and III and microMIPS mode instructions are always
510 interlocked. */
bdaaa2e1 511/* Itbl support may require additional care here. */
81912461
ILT
512#define cop_interlocks \
513 ((mips_opts.isa != ISA_MIPS1 \
514 && mips_opts.isa != ISA_MIPS2 \
515 && mips_opts.isa != ISA_MIPS3) \
516 || mips_opts.arch == CPU_R4300 \
df58fc94 517 || mips_opts.micromips \
81912461
ILT
518 )
519
520/* Whether the processor uses hardware interlocks to protect reads
521 from coprocessor registers after they are loaded from memory, and
522 thus does not require nops to be inserted. This applies to
523 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
df58fc94
RS
524 requires at MIPS ISA level I and microMIPS mode instructions are
525 always interlocked. */
526#define cop_mem_interlocks \
527 (mips_opts.isa != ISA_MIPS1 \
528 || mips_opts.micromips \
529 )
252b5132 530
6b76fefe
CM
531/* Is this a mfhi or mflo instruction? */
532#define MF_HILO_INSN(PINFO) \
b19e8a9b
AN
533 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
534
df58fc94
RS
535/* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
536 has been selected. This implies, in particular, that addresses of text
537 labels have their LSB set. */
538#define HAVE_CODE_COMPRESSION \
539 ((mips_opts.mips16 | mips_opts.micromips) != 0)
540
42429eac
RS
541/* The minimum and maximum signed values that can be stored in a GPR. */
542#define GPR_SMAX ((offsetT) (((valueT) 1 << (HAVE_64BIT_GPRS ? 63 : 31)) - 1))
543#define GPR_SMIN (-GPR_SMAX - 1)
544
252b5132
RH
545/* MIPS PIC level. */
546
a161fe53 547enum mips_pic_level mips_pic;
252b5132 548
c9914766 549/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 550 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 551static int mips_big_got = 0;
252b5132
RH
552
553/* 1 if trap instructions should used for overflow rather than break
554 instructions. */
c9914766 555static int mips_trap = 0;
252b5132 556
119d663a 557/* 1 if double width floating point constants should not be constructed
b6ff326e 558 by assembling two single width halves into two single width floating
119d663a
NC
559 point registers which just happen to alias the double width destination
560 register. On some architectures this aliasing can be disabled by a bit
d547a75e 561 in the status register, and the setting of this bit cannot be determined
119d663a
NC
562 automatically at assemble time. */
563static int mips_disable_float_construction;
564
252b5132
RH
565/* Non-zero if any .set noreorder directives were used. */
566
567static int mips_any_noreorder;
568
6b76fefe
CM
569/* Non-zero if nops should be inserted when the register referenced in
570 an mfhi/mflo instruction is read in the next two instructions. */
571static int mips_7000_hilo_fix;
572
02ffd3e4 573/* The size of objects in the small data section. */
156c2f8b 574static unsigned int g_switch_value = 8;
252b5132
RH
575/* Whether the -G option was used. */
576static int g_switch_seen = 0;
577
578#define N_RMASK 0xc4
579#define N_VFP 0xd4
580
581/* If we can determine in advance that GP optimization won't be
582 possible, we can skip the relaxation stuff that tries to produce
583 GP-relative references. This makes delay slot optimization work
584 better.
585
586 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
587 gcc output. It needs to guess right for gcc, otherwise gcc
588 will put what it thinks is a GP-relative instruction in a branch
589 delay slot.
252b5132
RH
590
591 I don't know if a fix is needed for the SVR4_PIC mode. I've only
592 fixed it for the non-PIC mode. KR 95/04/07 */
17a2f251 593static int nopic_need_relax (symbolS *, int);
252b5132
RH
594
595/* handle of the OPCODE hash table */
596static struct hash_control *op_hash = NULL;
597
598/* The opcode hash table we use for the mips16. */
599static struct hash_control *mips16_op_hash = NULL;
600
df58fc94
RS
601/* The opcode hash table we use for the microMIPS ASE. */
602static struct hash_control *micromips_op_hash = NULL;
603
252b5132
RH
604/* This array holds the chars that always start a comment. If the
605 pre-processor is disabled, these aren't very useful */
606const char comment_chars[] = "#";
607
608/* This array holds the chars that only start a comment at the beginning of
609 a line. If the line seems to have the form '# 123 filename'
610 .line and .file directives will appear in the pre-processed output */
611/* Note that input_file.c hand checks for '#' at the beginning of the
612 first line of the input file. This is because the compiler outputs
bdaaa2e1 613 #NO_APP at the beginning of its output. */
252b5132
RH
614/* Also note that C style comments are always supported. */
615const char line_comment_chars[] = "#";
616
bdaaa2e1 617/* This array holds machine specific line separator characters. */
63a0b638 618const char line_separator_chars[] = ";";
252b5132
RH
619
620/* Chars that can be used to separate mant from exp in floating point nums */
621const char EXP_CHARS[] = "eE";
622
623/* Chars that mean this number is a floating point constant */
624/* As in 0f12.456 */
625/* or 0d1.2345e12 */
626const char FLT_CHARS[] = "rRsSfFdDxXpP";
627
628/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
629 changed in read.c . Ideally it shouldn't have to know about it at all,
630 but nothing is ideal around here.
631 */
632
633static char *insn_error;
634
635static int auto_align = 1;
636
637/* When outputting SVR4 PIC code, the assembler needs to know the
638 offset in the stack frame from which to restore the $gp register.
639 This is set by the .cprestore pseudo-op, and saved in this
640 variable. */
641static offsetT mips_cprestore_offset = -1;
642
67c1ffbe 643/* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
6478892d 644 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 645 offset and even an other register than $gp as global pointer. */
6478892d
TS
646static offsetT mips_cpreturn_offset = -1;
647static int mips_cpreturn_register = -1;
648static int mips_gp_register = GP;
def2e0dd 649static int mips_gprel_offset = 0;
6478892d 650
7a621144
DJ
651/* Whether mips_cprestore_offset has been set in the current function
652 (or whether it has already been warned about, if not). */
653static int mips_cprestore_valid = 0;
654
252b5132
RH
655/* This is the register which holds the stack frame, as set by the
656 .frame pseudo-op. This is needed to implement .cprestore. */
657static int mips_frame_reg = SP;
658
7a621144
DJ
659/* Whether mips_frame_reg has been set in the current function
660 (or whether it has already been warned about, if not). */
661static int mips_frame_reg_valid = 0;
662
252b5132
RH
663/* To output NOP instructions correctly, we need to keep information
664 about the previous two instructions. */
665
666/* Whether we are optimizing. The default value of 2 means to remove
667 unneeded NOPs and swap branch instructions when possible. A value
668 of 1 means to not swap branches. A value of 0 means to always
669 insert NOPs. */
670static int mips_optimize = 2;
671
672/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
673 equivalent to seeing no -g option at all. */
674static int mips_debug = 0;
675
7d8e00cf
RS
676/* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
677#define MAX_VR4130_NOPS 4
678
679/* The maximum number of NOPs needed to fill delay slots. */
680#define MAX_DELAY_NOPS 2
681
682/* The maximum number of NOPs needed for any purpose. */
683#define MAX_NOPS 4
71400594
RS
684
685/* A list of previous instructions, with index 0 being the most recent.
686 We need to look back MAX_NOPS instructions when filling delay slots
687 or working around processor errata. We need to look back one
688 instruction further if we're thinking about using history[0] to
689 fill a branch delay slot. */
690static struct mips_cl_insn history[1 + MAX_NOPS];
252b5132 691
fc76e730 692/* Arrays of operands for each instruction. */
14daeee3 693#define MAX_OPERANDS 6
fc76e730
RS
694struct mips_operand_array {
695 const struct mips_operand *operand[MAX_OPERANDS];
696};
697static struct mips_operand_array *mips_operands;
698static struct mips_operand_array *mips16_operands;
699static struct mips_operand_array *micromips_operands;
700
1e915849 701/* Nop instructions used by emit_nop. */
df58fc94
RS
702static struct mips_cl_insn nop_insn;
703static struct mips_cl_insn mips16_nop_insn;
704static struct mips_cl_insn micromips_nop16_insn;
705static struct mips_cl_insn micromips_nop32_insn;
1e915849
RS
706
707/* The appropriate nop for the current mode. */
833794fc
MR
708#define NOP_INSN (mips_opts.mips16 \
709 ? &mips16_nop_insn \
710 : (mips_opts.micromips \
711 ? (mips_opts.insn32 \
712 ? &micromips_nop32_insn \
713 : &micromips_nop16_insn) \
714 : &nop_insn))
df58fc94
RS
715
716/* The size of NOP_INSN in bytes. */
833794fc
MR
717#define NOP_INSN_SIZE ((mips_opts.mips16 \
718 || (mips_opts.micromips && !mips_opts.insn32)) \
719 ? 2 : 4)
252b5132 720
252b5132
RH
721/* If this is set, it points to a frag holding nop instructions which
722 were inserted before the start of a noreorder section. If those
723 nops turn out to be unnecessary, the size of the frag can be
724 decreased. */
725static fragS *prev_nop_frag;
726
727/* The number of nop instructions we created in prev_nop_frag. */
728static int prev_nop_frag_holds;
729
730/* The number of nop instructions that we know we need in
bdaaa2e1 731 prev_nop_frag. */
252b5132
RH
732static int prev_nop_frag_required;
733
734/* The number of instructions we've seen since prev_nop_frag. */
735static int prev_nop_frag_since;
736
e8044f35
RS
737/* Relocations against symbols are sometimes done in two parts, with a HI
738 relocation and a LO relocation. Each relocation has only 16 bits of
739 space to store an addend. This means that in order for the linker to
740 handle carries correctly, it must be able to locate both the HI and
741 the LO relocation. This means that the relocations must appear in
742 order in the relocation table.
252b5132
RH
743
744 In order to implement this, we keep track of each unmatched HI
745 relocation. We then sort them so that they immediately precede the
bdaaa2e1 746 corresponding LO relocation. */
252b5132 747
e972090a
NC
748struct mips_hi_fixup
749{
252b5132
RH
750 /* Next HI fixup. */
751 struct mips_hi_fixup *next;
752 /* This fixup. */
753 fixS *fixp;
754 /* The section this fixup is in. */
755 segT seg;
756};
757
758/* The list of unmatched HI relocs. */
759
760static struct mips_hi_fixup *mips_hi_fixup_list;
761
64bdfcaf
RS
762/* The frag containing the last explicit relocation operator.
763 Null if explicit relocations have not been used. */
764
765static fragS *prev_reloc_op_frag;
766
252b5132
RH
767/* Map mips16 register numbers to normal MIPS register numbers. */
768
e972090a
NC
769static const unsigned int mips16_to_32_reg_map[] =
770{
252b5132
RH
771 16, 17, 2, 3, 4, 5, 6, 7
772};
60b63b72 773
df58fc94
RS
774/* Map microMIPS register numbers to normal MIPS register numbers. */
775
df58fc94 776#define micromips_to_32_reg_d_map mips16_to_32_reg_map
df58fc94
RS
777
778/* The microMIPS registers with type h. */
e76ff5ab 779static const unsigned int micromips_to_32_reg_h_map1[] =
df58fc94
RS
780{
781 5, 5, 6, 4, 4, 4, 4, 4
782};
e76ff5ab 783static const unsigned int micromips_to_32_reg_h_map2[] =
df58fc94
RS
784{
785 6, 7, 7, 21, 22, 5, 6, 7
786};
787
df58fc94
RS
788/* The microMIPS registers with type m. */
789static const unsigned int micromips_to_32_reg_m_map[] =
790{
791 0, 17, 2, 3, 16, 18, 19, 20
792};
793
794#define micromips_to_32_reg_n_map micromips_to_32_reg_m_map
795
71400594
RS
796/* Classifies the kind of instructions we're interested in when
797 implementing -mfix-vr4120. */
c67a084a
NC
798enum fix_vr4120_class
799{
71400594
RS
800 FIX_VR4120_MACC,
801 FIX_VR4120_DMACC,
802 FIX_VR4120_MULT,
803 FIX_VR4120_DMULT,
804 FIX_VR4120_DIV,
805 FIX_VR4120_MTHILO,
806 NUM_FIX_VR4120_CLASSES
807};
808
c67a084a
NC
809/* ...likewise -mfix-loongson2f-jump. */
810static bfd_boolean mips_fix_loongson2f_jump;
811
812/* ...likewise -mfix-loongson2f-nop. */
813static bfd_boolean mips_fix_loongson2f_nop;
814
815/* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed. */
816static bfd_boolean mips_fix_loongson2f;
817
71400594
RS
818/* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
819 there must be at least one other instruction between an instruction
820 of type X and an instruction of type Y. */
821static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
822
823/* True if -mfix-vr4120 is in force. */
d766e8ec 824static int mips_fix_vr4120;
4a6a3df4 825
7d8e00cf
RS
826/* ...likewise -mfix-vr4130. */
827static int mips_fix_vr4130;
828
6a32d874
CM
829/* ...likewise -mfix-24k. */
830static int mips_fix_24k;
831
d954098f
DD
832/* ...likewise -mfix-cn63xxp1 */
833static bfd_boolean mips_fix_cn63xxp1;
834
4a6a3df4
AO
835/* We don't relax branches by default, since this causes us to expand
836 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
837 fail to compute the offset before expanding the macro to the most
838 efficient expansion. */
839
840static int mips_relax_branch;
252b5132 841\f
4d7206a2
RS
842/* The expansion of many macros depends on the type of symbol that
843 they refer to. For example, when generating position-dependent code,
844 a macro that refers to a symbol may have two different expansions,
845 one which uses GP-relative addresses and one which uses absolute
846 addresses. When generating SVR4-style PIC, a macro may have
847 different expansions for local and global symbols.
848
849 We handle these situations by generating both sequences and putting
850 them in variant frags. In position-dependent code, the first sequence
851 will be the GP-relative one and the second sequence will be the
852 absolute one. In SVR4 PIC, the first sequence will be for global
853 symbols and the second will be for local symbols.
854
584892a6
RS
855 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
856 SECOND are the lengths of the two sequences in bytes. These fields
857 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
858 the subtype has the following flags:
4d7206a2 859
584892a6
RS
860 RELAX_USE_SECOND
861 Set if it has been decided that we should use the second
862 sequence instead of the first.
863
864 RELAX_SECOND_LONGER
865 Set in the first variant frag if the macro's second implementation
866 is longer than its first. This refers to the macro as a whole,
867 not an individual relaxation.
868
869 RELAX_NOMACRO
870 Set in the first variant frag if the macro appeared in a .set nomacro
871 block and if one alternative requires a warning but the other does not.
872
873 RELAX_DELAY_SLOT
874 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
875 delay slot.
4d7206a2 876
df58fc94
RS
877 RELAX_DELAY_SLOT_16BIT
878 Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
879 16-bit instruction.
880
881 RELAX_DELAY_SLOT_SIZE_FIRST
882 Like RELAX_DELAY_SLOT, but indicates that the first implementation of
883 the macro is of the wrong size for the branch delay slot.
884
885 RELAX_DELAY_SLOT_SIZE_SECOND
886 Like RELAX_DELAY_SLOT, but indicates that the second implementation of
887 the macro is of the wrong size for the branch delay slot.
888
4d7206a2
RS
889 The frag's "opcode" points to the first fixup for relaxable code.
890
891 Relaxable macros are generated using a sequence such as:
892
893 relax_start (SYMBOL);
894 ... generate first expansion ...
895 relax_switch ();
896 ... generate second expansion ...
897 relax_end ();
898
899 The code and fixups for the unwanted alternative are discarded
900 by md_convert_frag. */
584892a6 901#define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
4d7206a2 902
584892a6
RS
903#define RELAX_FIRST(X) (((X) >> 8) & 0xff)
904#define RELAX_SECOND(X) ((X) & 0xff)
905#define RELAX_USE_SECOND 0x10000
906#define RELAX_SECOND_LONGER 0x20000
907#define RELAX_NOMACRO 0x40000
908#define RELAX_DELAY_SLOT 0x80000
df58fc94
RS
909#define RELAX_DELAY_SLOT_16BIT 0x100000
910#define RELAX_DELAY_SLOT_SIZE_FIRST 0x200000
911#define RELAX_DELAY_SLOT_SIZE_SECOND 0x400000
252b5132 912
4a6a3df4
AO
913/* Branch without likely bit. If label is out of range, we turn:
914
915 beq reg1, reg2, label
916 delay slot
917
918 into
919
920 bne reg1, reg2, 0f
921 nop
922 j label
923 0: delay slot
924
925 with the following opcode replacements:
926
927 beq <-> bne
928 blez <-> bgtz
929 bltz <-> bgez
930 bc1f <-> bc1t
931
932 bltzal <-> bgezal (with jal label instead of j label)
933
934 Even though keeping the delay slot instruction in the delay slot of
935 the branch would be more efficient, it would be very tricky to do
936 correctly, because we'd have to introduce a variable frag *after*
937 the delay slot instruction, and expand that instead. Let's do it
938 the easy way for now, even if the branch-not-taken case now costs
939 one additional instruction. Out-of-range branches are not supposed
940 to be common, anyway.
941
942 Branch likely. If label is out of range, we turn:
943
944 beql reg1, reg2, label
945 delay slot (annulled if branch not taken)
946
947 into
948
949 beql reg1, reg2, 1f
950 nop
951 beql $0, $0, 2f
952 nop
953 1: j[al] label
954 delay slot (executed only if branch taken)
955 2:
956
957 It would be possible to generate a shorter sequence by losing the
958 likely bit, generating something like:
b34976b6 959
4a6a3df4
AO
960 bne reg1, reg2, 0f
961 nop
962 j[al] label
963 delay slot (executed only if branch taken)
964 0:
965
966 beql -> bne
967 bnel -> beq
968 blezl -> bgtz
969 bgtzl -> blez
970 bltzl -> bgez
971 bgezl -> bltz
972 bc1fl -> bc1t
973 bc1tl -> bc1f
974
975 bltzall -> bgezal (with jal label instead of j label)
976 bgezall -> bltzal (ditto)
977
978
979 but it's not clear that it would actually improve performance. */
66b3e8da
MR
980#define RELAX_BRANCH_ENCODE(at, uncond, likely, link, toofar) \
981 ((relax_substateT) \
982 (0xc0000000 \
983 | ((at) & 0x1f) \
984 | ((toofar) ? 0x20 : 0) \
985 | ((link) ? 0x40 : 0) \
986 | ((likely) ? 0x80 : 0) \
987 | ((uncond) ? 0x100 : 0)))
4a6a3df4 988#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
66b3e8da
MR
989#define RELAX_BRANCH_UNCOND(i) (((i) & 0x100) != 0)
990#define RELAX_BRANCH_LIKELY(i) (((i) & 0x80) != 0)
991#define RELAX_BRANCH_LINK(i) (((i) & 0x40) != 0)
992#define RELAX_BRANCH_TOOFAR(i) (((i) & 0x20) != 0)
993#define RELAX_BRANCH_AT(i) ((i) & 0x1f)
4a6a3df4 994
252b5132
RH
995/* For mips16 code, we use an entirely different form of relaxation.
996 mips16 supports two versions of most instructions which take
997 immediate values: a small one which takes some small value, and a
998 larger one which takes a 16 bit value. Since branches also follow
999 this pattern, relaxing these values is required.
1000
1001 We can assemble both mips16 and normal MIPS code in a single
1002 object. Therefore, we need to support this type of relaxation at
1003 the same time that we support the relaxation described above. We
1004 use the high bit of the subtype field to distinguish these cases.
1005
1006 The information we store for this type of relaxation is the
1007 argument code found in the opcode file for this relocation, whether
1008 the user explicitly requested a small or extended form, and whether
1009 the relocation is in a jump or jal delay slot. That tells us the
1010 size of the value, and how it should be stored. We also store
1011 whether the fragment is considered to be extended or not. We also
1012 store whether this is known to be a branch to a different section,
1013 whether we have tried to relax this frag yet, and whether we have
1014 ever extended a PC relative fragment because of a shift count. */
1015#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
1016 (0x80000000 \
1017 | ((type) & 0xff) \
1018 | ((small) ? 0x100 : 0) \
1019 | ((ext) ? 0x200 : 0) \
1020 | ((dslot) ? 0x400 : 0) \
1021 | ((jal_dslot) ? 0x800 : 0))
4a6a3df4 1022#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132
RH
1023#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
1024#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
1025#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
1026#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
1027#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
1028#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
1029#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
1030#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
1031#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
1032#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
1033#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
885add95 1034
df58fc94
RS
1035/* For microMIPS code, we use relaxation similar to one we use for
1036 MIPS16 code. Some instructions that take immediate values support
1037 two encodings: a small one which takes some small value, and a
1038 larger one which takes a 16 bit value. As some branches also follow
1039 this pattern, relaxing these values is required.
1040
1041 We can assemble both microMIPS and normal MIPS code in a single
1042 object. Therefore, we need to support this type of relaxation at
1043 the same time that we support the relaxation described above. We
1044 use one of the high bits of the subtype field to distinguish these
1045 cases.
1046
1047 The information we store for this type of relaxation is the argument
1048 code found in the opcode file for this relocation, the register
40209cad
MR
1049 selected as the assembler temporary, whether the branch is
1050 unconditional, whether it is compact, whether it stores the link
1051 address implicitly in $ra, whether relaxation of out-of-range 32-bit
1052 branches to a sequence of instructions is enabled, and whether the
1053 displacement of a branch is too large to fit as an immediate argument
1054 of a 16-bit and a 32-bit branch, respectively. */
1055#define RELAX_MICROMIPS_ENCODE(type, at, uncond, compact, link, \
1056 relax32, toofar16, toofar32) \
1057 (0x40000000 \
1058 | ((type) & 0xff) \
1059 | (((at) & 0x1f) << 8) \
1060 | ((uncond) ? 0x2000 : 0) \
1061 | ((compact) ? 0x4000 : 0) \
1062 | ((link) ? 0x8000 : 0) \
1063 | ((relax32) ? 0x10000 : 0) \
1064 | ((toofar16) ? 0x20000 : 0) \
1065 | ((toofar32) ? 0x40000 : 0))
df58fc94
RS
1066#define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1067#define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1068#define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
40209cad
MR
1069#define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x2000) != 0)
1070#define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x4000) != 0)
1071#define RELAX_MICROMIPS_LINK(i) (((i) & 0x8000) != 0)
1072#define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x10000) != 0)
1073
1074#define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x20000) != 0)
1075#define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x20000)
1076#define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x20000)
1077#define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x40000) != 0)
1078#define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x40000)
1079#define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x40000)
df58fc94 1080
43c0598f
RS
1081/* Sign-extend 16-bit value X. */
1082#define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1083
885add95
CD
1084/* Is the given value a sign-extended 32-bit value? */
1085#define IS_SEXT_32BIT_NUM(x) \
1086 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1087 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1088
1089/* Is the given value a sign-extended 16-bit value? */
1090#define IS_SEXT_16BIT_NUM(x) \
1091 (((x) &~ (offsetT) 0x7fff) == 0 \
1092 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1093
df58fc94
RS
1094/* Is the given value a sign-extended 12-bit value? */
1095#define IS_SEXT_12BIT_NUM(x) \
1096 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1097
7f3c4072
CM
1098/* Is the given value a sign-extended 9-bit value? */
1099#define IS_SEXT_9BIT_NUM(x) \
1100 (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1101
2051e8c4
MR
1102/* Is the given value a zero-extended 32-bit value? Or a negated one? */
1103#define IS_ZEXT_32BIT_NUM(x) \
1104 (((x) &~ (offsetT) 0xffffffff) == 0 \
1105 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1106
bf12938e
RS
1107/* Extract bits MASK << SHIFT from STRUCT and shift them right
1108 SHIFT places. */
1109#define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1110 (((STRUCT) >> (SHIFT)) & (MASK))
1111
bf12938e 1112/* Extract the operand given by FIELD from mips_cl_insn INSN. */
df58fc94
RS
1113#define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1114 (!(MICROMIPS) \
1115 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1116 : EXTRACT_BITS ((INSN).insn_opcode, \
1117 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
bf12938e
RS
1118#define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1119 EXTRACT_BITS ((INSN).insn_opcode, \
1120 MIPS16OP_MASK_##FIELD, \
1121 MIPS16OP_SH_##FIELD)
5c04167a
RS
1122
1123/* The MIPS16 EXTEND opcode, shifted left 16 places. */
1124#define MIPS16_EXTEND (0xf000U << 16)
4d7206a2 1125\f
df58fc94
RS
1126/* Whether or not we are emitting a branch-likely macro. */
1127static bfd_boolean emit_branch_likely_macro = FALSE;
1128
4d7206a2
RS
1129/* Global variables used when generating relaxable macros. See the
1130 comment above RELAX_ENCODE for more details about how relaxation
1131 is used. */
1132static struct {
1133 /* 0 if we're not emitting a relaxable macro.
1134 1 if we're emitting the first of the two relaxation alternatives.
1135 2 if we're emitting the second alternative. */
1136 int sequence;
1137
1138 /* The first relaxable fixup in the current frag. (In other words,
1139 the first fixup that refers to relaxable code.) */
1140 fixS *first_fixup;
1141
1142 /* sizes[0] says how many bytes of the first alternative are stored in
1143 the current frag. Likewise sizes[1] for the second alternative. */
1144 unsigned int sizes[2];
1145
1146 /* The symbol on which the choice of sequence depends. */
1147 symbolS *symbol;
1148} mips_relax;
252b5132 1149\f
584892a6
RS
1150/* Global variables used to decide whether a macro needs a warning. */
1151static struct {
1152 /* True if the macro is in a branch delay slot. */
1153 bfd_boolean delay_slot_p;
1154
df58fc94
RS
1155 /* Set to the length in bytes required if the macro is in a delay slot
1156 that requires a specific length of instruction, otherwise zero. */
1157 unsigned int delay_slot_length;
1158
584892a6
RS
1159 /* For relaxable macros, sizes[0] is the length of the first alternative
1160 in bytes and sizes[1] is the length of the second alternative.
1161 For non-relaxable macros, both elements give the length of the
1162 macro in bytes. */
1163 unsigned int sizes[2];
1164
df58fc94
RS
1165 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1166 instruction of the first alternative in bytes and first_insn_sizes[1]
1167 is the length of the first instruction of the second alternative.
1168 For non-relaxable macros, both elements give the length of the first
1169 instruction in bytes.
1170
1171 Set to zero if we haven't yet seen the first instruction. */
1172 unsigned int first_insn_sizes[2];
1173
1174 /* For relaxable macros, insns[0] is the number of instructions for the
1175 first alternative and insns[1] is the number of instructions for the
1176 second alternative.
1177
1178 For non-relaxable macros, both elements give the number of
1179 instructions for the macro. */
1180 unsigned int insns[2];
1181
584892a6
RS
1182 /* The first variant frag for this macro. */
1183 fragS *first_frag;
1184} mips_macro_warning;
1185\f
252b5132
RH
1186/* Prototypes for static functions. */
1187
252b5132
RH
1188enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1189
b34976b6 1190static void append_insn
df58fc94
RS
1191 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1192 bfd_boolean expansionp);
7d10b47d 1193static void mips_no_prev_insn (void);
c67a084a 1194static void macro_build (expressionS *, const char *, const char *, ...);
b34976b6 1195static void mips16_macro_build
03ea81db 1196 (expressionS *, const char *, const char *, va_list *);
67c0d1eb 1197static void load_register (int, expressionS *, int);
584892a6
RS
1198static void macro_start (void);
1199static void macro_end (void);
833794fc 1200static void macro (struct mips_cl_insn *ip, char *str);
17a2f251 1201static void mips16_macro (struct mips_cl_insn * ip);
17a2f251
TS
1202static void mips_ip (char *str, struct mips_cl_insn * ip);
1203static void mips16_ip (char *str, struct mips_cl_insn * ip);
b34976b6 1204static void mips16_immed
43c0598f
RS
1205 (char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
1206 unsigned int, unsigned long *);
5e0116d5 1207static size_t my_getSmallExpression
17a2f251
TS
1208 (expressionS *, bfd_reloc_code_real_type *, char *);
1209static void my_getExpression (expressionS *, char *);
1210static void s_align (int);
1211static void s_change_sec (int);
1212static void s_change_section (int);
1213static void s_cons (int);
1214static void s_float_cons (int);
1215static void s_mips_globl (int);
1216static void s_option (int);
1217static void s_mipsset (int);
1218static void s_abicalls (int);
1219static void s_cpload (int);
1220static void s_cpsetup (int);
1221static void s_cplocal (int);
1222static void s_cprestore (int);
1223static void s_cpreturn (int);
741d6ea8
JM
1224static void s_dtprelword (int);
1225static void s_dtpreldword (int);
d0f13682
CLT
1226static void s_tprelword (int);
1227static void s_tpreldword (int);
17a2f251
TS
1228static void s_gpvalue (int);
1229static void s_gpword (int);
1230static void s_gpdword (int);
a3f278e2 1231static void s_ehword (int);
17a2f251
TS
1232static void s_cpadd (int);
1233static void s_insn (int);
ba92f887 1234static void s_nan (int);
17a2f251
TS
1235static void md_obj_begin (void);
1236static void md_obj_end (void);
1237static void s_mips_ent (int);
1238static void s_mips_end (int);
1239static void s_mips_frame (int);
1240static void s_mips_mask (int reg_type);
1241static void s_mips_stab (int);
1242static void s_mips_weakext (int);
1243static void s_mips_file (int);
1244static void s_mips_loc (int);
1245static bfd_boolean pic_need_relax (symbolS *, asection *);
4a6a3df4 1246static int relaxed_branch_length (fragS *, asection *, int);
df58fc94
RS
1247static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1248static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
e7af610e
NC
1249
1250/* Table and functions used to map between CPU/ISA names, and
1251 ISA levels, and CPU numbers. */
1252
e972090a
NC
1253struct mips_cpu_info
1254{
e7af610e 1255 const char *name; /* CPU or ISA name. */
d16afab6
RS
1256 int flags; /* MIPS_CPU_* flags. */
1257 int ase; /* Set of ASEs implemented by the CPU. */
e7af610e
NC
1258 int isa; /* ISA level. */
1259 int cpu; /* CPU number (default CPU if ISA). */
1260};
1261
ad3fea08 1262#define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
ad3fea08 1263
17a2f251
TS
1264static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1265static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1266static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132 1267\f
c31f3936
RS
1268/* Command-line options. */
1269const char *md_shortopts = "O::g::G:";
1270
1271enum options
1272 {
1273 OPTION_MARCH = OPTION_MD_BASE,
1274 OPTION_MTUNE,
1275 OPTION_MIPS1,
1276 OPTION_MIPS2,
1277 OPTION_MIPS3,
1278 OPTION_MIPS4,
1279 OPTION_MIPS5,
1280 OPTION_MIPS32,
1281 OPTION_MIPS64,
1282 OPTION_MIPS32R2,
1283 OPTION_MIPS64R2,
1284 OPTION_MIPS16,
1285 OPTION_NO_MIPS16,
1286 OPTION_MIPS3D,
1287 OPTION_NO_MIPS3D,
1288 OPTION_MDMX,
1289 OPTION_NO_MDMX,
1290 OPTION_DSP,
1291 OPTION_NO_DSP,
1292 OPTION_MT,
1293 OPTION_NO_MT,
1294 OPTION_VIRT,
1295 OPTION_NO_VIRT,
1296 OPTION_SMARTMIPS,
1297 OPTION_NO_SMARTMIPS,
1298 OPTION_DSPR2,
1299 OPTION_NO_DSPR2,
1300 OPTION_EVA,
1301 OPTION_NO_EVA,
1302 OPTION_MICROMIPS,
1303 OPTION_NO_MICROMIPS,
1304 OPTION_MCU,
1305 OPTION_NO_MCU,
1306 OPTION_COMPAT_ARCH_BASE,
1307 OPTION_M4650,
1308 OPTION_NO_M4650,
1309 OPTION_M4010,
1310 OPTION_NO_M4010,
1311 OPTION_M4100,
1312 OPTION_NO_M4100,
1313 OPTION_M3900,
1314 OPTION_NO_M3900,
1315 OPTION_M7000_HILO_FIX,
1316 OPTION_MNO_7000_HILO_FIX,
1317 OPTION_FIX_24K,
1318 OPTION_NO_FIX_24K,
1319 OPTION_FIX_LOONGSON2F_JUMP,
1320 OPTION_NO_FIX_LOONGSON2F_JUMP,
1321 OPTION_FIX_LOONGSON2F_NOP,
1322 OPTION_NO_FIX_LOONGSON2F_NOP,
1323 OPTION_FIX_VR4120,
1324 OPTION_NO_FIX_VR4120,
1325 OPTION_FIX_VR4130,
1326 OPTION_NO_FIX_VR4130,
1327 OPTION_FIX_CN63XXP1,
1328 OPTION_NO_FIX_CN63XXP1,
1329 OPTION_TRAP,
1330 OPTION_BREAK,
1331 OPTION_EB,
1332 OPTION_EL,
1333 OPTION_FP32,
1334 OPTION_GP32,
1335 OPTION_CONSTRUCT_FLOATS,
1336 OPTION_NO_CONSTRUCT_FLOATS,
1337 OPTION_FP64,
1338 OPTION_GP64,
1339 OPTION_RELAX_BRANCH,
1340 OPTION_NO_RELAX_BRANCH,
833794fc
MR
1341 OPTION_INSN32,
1342 OPTION_NO_INSN32,
c31f3936
RS
1343 OPTION_MSHARED,
1344 OPTION_MNO_SHARED,
1345 OPTION_MSYM32,
1346 OPTION_MNO_SYM32,
1347 OPTION_SOFT_FLOAT,
1348 OPTION_HARD_FLOAT,
1349 OPTION_SINGLE_FLOAT,
1350 OPTION_DOUBLE_FLOAT,
1351 OPTION_32,
c31f3936
RS
1352 OPTION_CALL_SHARED,
1353 OPTION_CALL_NONPIC,
1354 OPTION_NON_SHARED,
1355 OPTION_XGOT,
1356 OPTION_MABI,
1357 OPTION_N32,
1358 OPTION_64,
1359 OPTION_MDEBUG,
1360 OPTION_NO_MDEBUG,
1361 OPTION_PDR,
1362 OPTION_NO_PDR,
1363 OPTION_MVXWORKS_PIC,
ba92f887 1364 OPTION_NAN,
c31f3936
RS
1365 OPTION_END_OF_ENUM
1366 };
1367
1368struct option md_longopts[] =
1369{
1370 /* Options which specify architecture. */
1371 {"march", required_argument, NULL, OPTION_MARCH},
1372 {"mtune", required_argument, NULL, OPTION_MTUNE},
1373 {"mips0", no_argument, NULL, OPTION_MIPS1},
1374 {"mips1", no_argument, NULL, OPTION_MIPS1},
1375 {"mips2", no_argument, NULL, OPTION_MIPS2},
1376 {"mips3", no_argument, NULL, OPTION_MIPS3},
1377 {"mips4", no_argument, NULL, OPTION_MIPS4},
1378 {"mips5", no_argument, NULL, OPTION_MIPS5},
1379 {"mips32", no_argument, NULL, OPTION_MIPS32},
1380 {"mips64", no_argument, NULL, OPTION_MIPS64},
1381 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
1382 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
1383
1384 /* Options which specify Application Specific Extensions (ASEs). */
1385 {"mips16", no_argument, NULL, OPTION_MIPS16},
1386 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1387 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1388 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1389 {"mdmx", no_argument, NULL, OPTION_MDMX},
1390 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1391 {"mdsp", no_argument, NULL, OPTION_DSP},
1392 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1393 {"mmt", no_argument, NULL, OPTION_MT},
1394 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1395 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1396 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1397 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1398 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
1399 {"meva", no_argument, NULL, OPTION_EVA},
1400 {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1401 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1402 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1403 {"mmcu", no_argument, NULL, OPTION_MCU},
1404 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1405 {"mvirt", no_argument, NULL, OPTION_VIRT},
1406 {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
1407
1408 /* Old-style architecture options. Don't add more of these. */
1409 {"m4650", no_argument, NULL, OPTION_M4650},
1410 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1411 {"m4010", no_argument, NULL, OPTION_M4010},
1412 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1413 {"m4100", no_argument, NULL, OPTION_M4100},
1414 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1415 {"m3900", no_argument, NULL, OPTION_M3900},
1416 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1417
1418 /* Options which enable bug fixes. */
1419 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1420 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1421 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1422 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1423 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1424 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1425 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1426 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
1427 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1428 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
1429 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1430 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
1431 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
1432 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1433 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
1434
1435 /* Miscellaneous options. */
1436 {"trap", no_argument, NULL, OPTION_TRAP},
1437 {"no-break", no_argument, NULL, OPTION_TRAP},
1438 {"break", no_argument, NULL, OPTION_BREAK},
1439 {"no-trap", no_argument, NULL, OPTION_BREAK},
1440 {"EB", no_argument, NULL, OPTION_EB},
1441 {"EL", no_argument, NULL, OPTION_EL},
1442 {"mfp32", no_argument, NULL, OPTION_FP32},
1443 {"mgp32", no_argument, NULL, OPTION_GP32},
1444 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1445 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1446 {"mfp64", no_argument, NULL, OPTION_FP64},
1447 {"mgp64", no_argument, NULL, OPTION_GP64},
1448 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1449 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
833794fc
MR
1450 {"minsn32", no_argument, NULL, OPTION_INSN32},
1451 {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
c31f3936
RS
1452 {"mshared", no_argument, NULL, OPTION_MSHARED},
1453 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1454 {"msym32", no_argument, NULL, OPTION_MSYM32},
1455 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1456 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1457 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1458 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1459 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
1460
1461 /* Strictly speaking this next option is ELF specific,
1462 but we allow it for other ports as well in order to
1463 make testing easier. */
1464 {"32", no_argument, NULL, OPTION_32},
1465
1466 /* ELF-specific options. */
c31f3936
RS
1467 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1468 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1469 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1470 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
1471 {"xgot", no_argument, NULL, OPTION_XGOT},
1472 {"mabi", required_argument, NULL, OPTION_MABI},
1473 {"n32", no_argument, NULL, OPTION_N32},
1474 {"64", no_argument, NULL, OPTION_64},
1475 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1476 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1477 {"mpdr", no_argument, NULL, OPTION_PDR},
1478 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1479 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
ba92f887 1480 {"mnan", required_argument, NULL, OPTION_NAN},
c31f3936
RS
1481
1482 {NULL, no_argument, NULL, 0}
1483};
1484size_t md_longopts_size = sizeof (md_longopts);
1485\f
c6278170
RS
1486/* Information about either an Application Specific Extension or an
1487 optional architecture feature that, for simplicity, we treat in the
1488 same way as an ASE. */
1489struct mips_ase
1490{
1491 /* The name of the ASE, used in both the command-line and .set options. */
1492 const char *name;
1493
1494 /* The associated ASE_* flags. If the ASE is available on both 32-bit
1495 and 64-bit architectures, the flags here refer to the subset that
1496 is available on both. */
1497 unsigned int flags;
1498
1499 /* The ASE_* flag used for instructions that are available on 64-bit
1500 architectures but that are not included in FLAGS. */
1501 unsigned int flags64;
1502
1503 /* The command-line options that turn the ASE on and off. */
1504 int option_on;
1505 int option_off;
1506
1507 /* The minimum required architecture revisions for MIPS32, MIPS64,
1508 microMIPS32 and microMIPS64, or -1 if the extension isn't supported. */
1509 int mips32_rev;
1510 int mips64_rev;
1511 int micromips32_rev;
1512 int micromips64_rev;
1513};
1514
1515/* A table of all supported ASEs. */
1516static const struct mips_ase mips_ases[] = {
1517 { "dsp", ASE_DSP, ASE_DSP64,
1518 OPTION_DSP, OPTION_NO_DSP,
1519 2, 2, 2, 2 },
1520
1521 { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1522 OPTION_DSPR2, OPTION_NO_DSPR2,
1523 2, 2, 2, 2 },
1524
1525 { "eva", ASE_EVA, 0,
1526 OPTION_EVA, OPTION_NO_EVA,
1527 2, 2, 2, 2 },
1528
1529 { "mcu", ASE_MCU, 0,
1530 OPTION_MCU, OPTION_NO_MCU,
1531 2, 2, 2, 2 },
1532
1533 /* Deprecated in MIPS64r5, but we don't implement that yet. */
1534 { "mdmx", ASE_MDMX, 0,
1535 OPTION_MDMX, OPTION_NO_MDMX,
1536 -1, 1, -1, -1 },
1537
1538 /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2. */
1539 { "mips3d", ASE_MIPS3D, 0,
1540 OPTION_MIPS3D, OPTION_NO_MIPS3D,
1541 2, 1, -1, -1 },
1542
1543 { "mt", ASE_MT, 0,
1544 OPTION_MT, OPTION_NO_MT,
1545 2, 2, -1, -1 },
1546
1547 { "smartmips", ASE_SMARTMIPS, 0,
1548 OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
1549 1, -1, -1, -1 },
1550
1551 { "virt", ASE_VIRT, ASE_VIRT64,
1552 OPTION_VIRT, OPTION_NO_VIRT,
1553 2, 2, 2, 2 }
1554};
1555
1556/* The set of ASEs that require -mfp64. */
1557#define FP64_ASES (ASE_MIPS3D | ASE_MDMX)
1558
1559/* Groups of ASE_* flags that represent different revisions of an ASE. */
1560static const unsigned int mips_ase_groups[] = {
1561 ASE_DSP | ASE_DSPR2
1562};
1563\f
252b5132
RH
1564/* Pseudo-op table.
1565
1566 The following pseudo-ops from the Kane and Heinrich MIPS book
1567 should be defined here, but are currently unsupported: .alias,
1568 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1569
1570 The following pseudo-ops from the Kane and Heinrich MIPS book are
1571 specific to the type of debugging information being generated, and
1572 should be defined by the object format: .aent, .begin, .bend,
1573 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1574 .vreg.
1575
1576 The following pseudo-ops from the Kane and Heinrich MIPS book are
1577 not MIPS CPU specific, but are also not specific to the object file
1578 format. This file is probably the best place to define them, but
d84bcf09 1579 they are not currently supported: .asm0, .endr, .lab, .struct. */
252b5132 1580
e972090a
NC
1581static const pseudo_typeS mips_pseudo_table[] =
1582{
beae10d5 1583 /* MIPS specific pseudo-ops. */
252b5132
RH
1584 {"option", s_option, 0},
1585 {"set", s_mipsset, 0},
1586 {"rdata", s_change_sec, 'r'},
1587 {"sdata", s_change_sec, 's'},
1588 {"livereg", s_ignore, 0},
1589 {"abicalls", s_abicalls, 0},
1590 {"cpload", s_cpload, 0},
6478892d
TS
1591 {"cpsetup", s_cpsetup, 0},
1592 {"cplocal", s_cplocal, 0},
252b5132 1593 {"cprestore", s_cprestore, 0},
6478892d 1594 {"cpreturn", s_cpreturn, 0},
741d6ea8
JM
1595 {"dtprelword", s_dtprelword, 0},
1596 {"dtpreldword", s_dtpreldword, 0},
d0f13682
CLT
1597 {"tprelword", s_tprelword, 0},
1598 {"tpreldword", s_tpreldword, 0},
6478892d 1599 {"gpvalue", s_gpvalue, 0},
252b5132 1600 {"gpword", s_gpword, 0},
10181a0d 1601 {"gpdword", s_gpdword, 0},
a3f278e2 1602 {"ehword", s_ehword, 0},
252b5132
RH
1603 {"cpadd", s_cpadd, 0},
1604 {"insn", s_insn, 0},
ba92f887 1605 {"nan", s_nan, 0},
252b5132 1606
beae10d5 1607 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132 1608 chips. */
38a57ae7 1609 {"asciiz", stringer, 8 + 1},
252b5132
RH
1610 {"bss", s_change_sec, 'b'},
1611 {"err", s_err, 0},
1612 {"half", s_cons, 1},
1613 {"dword", s_cons, 3},
1614 {"weakext", s_mips_weakext, 0},
7c752c2a
TS
1615 {"origin", s_org, 0},
1616 {"repeat", s_rept, 0},
252b5132 1617
998b3c36
MR
1618 /* For MIPS this is non-standard, but we define it for consistency. */
1619 {"sbss", s_change_sec, 'B'},
1620
beae10d5 1621 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1622 here for one reason or another. */
1623 {"align", s_align, 0},
1624 {"byte", s_cons, 0},
1625 {"data", s_change_sec, 'd'},
1626 {"double", s_float_cons, 'd'},
1627 {"float", s_float_cons, 'f'},
1628 {"globl", s_mips_globl, 0},
1629 {"global", s_mips_globl, 0},
1630 {"hword", s_cons, 1},
1631 {"int", s_cons, 2},
1632 {"long", s_cons, 2},
1633 {"octa", s_cons, 4},
1634 {"quad", s_cons, 3},
cca86cc8 1635 {"section", s_change_section, 0},
252b5132
RH
1636 {"short", s_cons, 1},
1637 {"single", s_float_cons, 'f'},
754e2bb9 1638 {"stabd", s_mips_stab, 'd'},
252b5132 1639 {"stabn", s_mips_stab, 'n'},
754e2bb9 1640 {"stabs", s_mips_stab, 's'},
252b5132
RH
1641 {"text", s_change_sec, 't'},
1642 {"word", s_cons, 2},
add56521 1643
add56521 1644 { "extern", ecoff_directive_extern, 0},
add56521 1645
43841e91 1646 { NULL, NULL, 0 },
252b5132
RH
1647};
1648
e972090a
NC
1649static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1650{
beae10d5
KH
1651 /* These pseudo-ops should be defined by the object file format.
1652 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1653 {"aent", s_mips_ent, 1},
1654 {"bgnb", s_ignore, 0},
1655 {"end", s_mips_end, 0},
1656 {"endb", s_ignore, 0},
1657 {"ent", s_mips_ent, 0},
c5dd6aab 1658 {"file", s_mips_file, 0},
252b5132
RH
1659 {"fmask", s_mips_mask, 'F'},
1660 {"frame", s_mips_frame, 0},
c5dd6aab 1661 {"loc", s_mips_loc, 0},
252b5132
RH
1662 {"mask", s_mips_mask, 'R'},
1663 {"verstamp", s_ignore, 0},
43841e91 1664 { NULL, NULL, 0 },
252b5132
RH
1665};
1666
3ae8dd8d
MR
1667/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1668 purpose of the `.dc.a' internal pseudo-op. */
1669
1670int
1671mips_address_bytes (void)
1672{
1673 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1674}
1675
17a2f251 1676extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1677
1678void
17a2f251 1679mips_pop_insert (void)
252b5132
RH
1680{
1681 pop_insert (mips_pseudo_table);
1682 if (! ECOFF_DEBUGGING)
1683 pop_insert (mips_nonecoff_pseudo_table);
1684}
1685\f
1686/* Symbols labelling the current insn. */
1687
e972090a
NC
1688struct insn_label_list
1689{
252b5132
RH
1690 struct insn_label_list *next;
1691 symbolS *label;
1692};
1693
252b5132 1694static struct insn_label_list *free_insn_labels;
742a56fe 1695#define label_list tc_segment_info_data.labels
252b5132 1696
17a2f251 1697static void mips_clear_insn_labels (void);
df58fc94
RS
1698static void mips_mark_labels (void);
1699static void mips_compressed_mark_labels (void);
252b5132
RH
1700
1701static inline void
17a2f251 1702mips_clear_insn_labels (void)
252b5132
RH
1703{
1704 register struct insn_label_list **pl;
a8dbcb85 1705 segment_info_type *si;
252b5132 1706
a8dbcb85
TS
1707 if (now_seg)
1708 {
1709 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1710 ;
1711
1712 si = seg_info (now_seg);
1713 *pl = si->label_list;
1714 si->label_list = NULL;
1715 }
252b5132 1716}
a8dbcb85 1717
df58fc94
RS
1718/* Mark instruction labels in MIPS16/microMIPS mode. */
1719
1720static inline void
1721mips_mark_labels (void)
1722{
1723 if (HAVE_CODE_COMPRESSION)
1724 mips_compressed_mark_labels ();
1725}
252b5132
RH
1726\f
1727static char *expr_end;
1728
77bd4346
RS
1729/* Expressions which appear in macro instructions. These are set by
1730 mips_ip and read by macro. */
252b5132
RH
1731
1732static expressionS imm_expr;
5f74bc13 1733static expressionS imm2_expr;
252b5132 1734
77bd4346
RS
1735/* The relocatable field in an instruction and the relocs associated
1736 with it. These variables are used for instructions like LUI and
1737 JAL as well as true offsets. They are also used for address
1738 operands in macros. */
252b5132 1739
77bd4346 1740static expressionS offset_expr;
f6688943
TS
1741static bfd_reloc_code_real_type offset_reloc[3]
1742 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 1743
df58fc94
RS
1744/* This is set to the resulting size of the instruction to be produced
1745 by mips16_ip if an explicit extension is used or by mips_ip if an
1746 explicit size is supplied. */
252b5132 1747
df58fc94 1748static unsigned int forced_insn_length;
252b5132 1749
e1b47bd5
RS
1750/* True if we are assembling an instruction. All dot symbols defined during
1751 this time should be treated as code labels. */
1752
1753static bfd_boolean mips_assembling_insn;
1754
ecb4347a
DJ
1755/* The pdr segment for per procedure frame/regmask info. Not used for
1756 ECOFF debugging. */
252b5132
RH
1757
1758static segT pdr_seg;
252b5132 1759
e013f690
TS
1760/* The default target format to use. */
1761
aeffff67
RS
1762#if defined (TE_FreeBSD)
1763#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
1764#elif defined (TE_TMIPS)
1765#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
1766#else
1767#define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
1768#endif
1769
e013f690 1770const char *
17a2f251 1771mips_target_format (void)
e013f690
TS
1772{
1773 switch (OUTPUT_FLAVOR)
1774 {
e013f690 1775 case bfd_target_elf_flavour:
0a44bf69
RS
1776#ifdef TE_VXWORKS
1777 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1778 return (target_big_endian
1779 ? "elf32-bigmips-vxworks"
1780 : "elf32-littlemips-vxworks");
1781#endif
e013f690 1782 return (target_big_endian
cfe86eaa 1783 ? (HAVE_64BIT_OBJECTS
aeffff67 1784 ? ELF_TARGET ("elf64-", "big")
cfe86eaa 1785 : (HAVE_NEWABI
aeffff67
RS
1786 ? ELF_TARGET ("elf32-n", "big")
1787 : ELF_TARGET ("elf32-", "big")))
cfe86eaa 1788 : (HAVE_64BIT_OBJECTS
aeffff67 1789 ? ELF_TARGET ("elf64-", "little")
cfe86eaa 1790 : (HAVE_NEWABI
aeffff67
RS
1791 ? ELF_TARGET ("elf32-n", "little")
1792 : ELF_TARGET ("elf32-", "little"))));
e013f690
TS
1793 default:
1794 abort ();
1795 return NULL;
1796 }
1797}
1798
c6278170
RS
1799/* Return the ISA revision that is currently in use, or 0 if we are
1800 generating code for MIPS V or below. */
1801
1802static int
1803mips_isa_rev (void)
1804{
1805 if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
1806 return 2;
1807
1808 /* microMIPS implies revision 2 or above. */
1809 if (mips_opts.micromips)
1810 return 2;
1811
1812 if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
1813 return 1;
1814
1815 return 0;
1816}
1817
1818/* Return the mask of all ASEs that are revisions of those in FLAGS. */
1819
1820static unsigned int
1821mips_ase_mask (unsigned int flags)
1822{
1823 unsigned int i;
1824
1825 for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
1826 if (flags & mips_ase_groups[i])
1827 flags |= mips_ase_groups[i];
1828 return flags;
1829}
1830
1831/* Check whether the current ISA supports ASE. Issue a warning if
1832 appropriate. */
1833
1834static void
1835mips_check_isa_supports_ase (const struct mips_ase *ase)
1836{
1837 const char *base;
1838 int min_rev, size;
1839 static unsigned int warned_isa;
1840 static unsigned int warned_fp32;
1841
1842 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
1843 min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
1844 else
1845 min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
1846 if ((min_rev < 0 || mips_isa_rev () < min_rev)
1847 && (warned_isa & ase->flags) != ase->flags)
1848 {
1849 warned_isa |= ase->flags;
1850 base = mips_opts.micromips ? "microMIPS" : "MIPS";
1851 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
1852 if (min_rev < 0)
1853 as_warn (_("The %d-bit %s architecture does not support the"
1854 " `%s' extension"), size, base, ase->name);
1855 else
1856 as_warn (_("The `%s' extension requires %s%d revision %d or greater"),
1857 ase->name, base, size, min_rev);
1858 }
1859 if ((ase->flags & FP64_ASES)
1860 && mips_opts.fp32
1861 && (warned_fp32 & ase->flags) != ase->flags)
1862 {
1863 warned_fp32 |= ase->flags;
1864 as_warn (_("The `%s' extension requires 64-bit FPRs"), ase->name);
1865 }
1866}
1867
1868/* Check all enabled ASEs to see whether they are supported by the
1869 chosen architecture. */
1870
1871static void
1872mips_check_isa_supports_ases (void)
1873{
1874 unsigned int i, mask;
1875
1876 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
1877 {
1878 mask = mips_ase_mask (mips_ases[i].flags);
1879 if ((mips_opts.ase & mask) == mips_ases[i].flags)
1880 mips_check_isa_supports_ase (&mips_ases[i]);
1881 }
1882}
1883
1884/* Set the state of ASE to ENABLED_P. Return the mask of ASE_* flags
1885 that were affected. */
1886
1887static unsigned int
1888mips_set_ase (const struct mips_ase *ase, bfd_boolean enabled_p)
1889{
1890 unsigned int mask;
1891
1892 mask = mips_ase_mask (ase->flags);
1893 mips_opts.ase &= ~mask;
1894 if (enabled_p)
1895 mips_opts.ase |= ase->flags;
1896 return mask;
1897}
1898
1899/* Return the ASE called NAME, or null if none. */
1900
1901static const struct mips_ase *
1902mips_lookup_ase (const char *name)
1903{
1904 unsigned int i;
1905
1906 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
1907 if (strcmp (name, mips_ases[i].name) == 0)
1908 return &mips_ases[i];
1909 return NULL;
1910}
1911
df58fc94
RS
1912/* Return the length of a microMIPS instruction in bytes. If bits of
1913 the mask beyond the low 16 are 0, then it is a 16-bit instruction.
1914 Otherwise assume a 32-bit instruction; 48-bit instructions (0x1f
1915 major opcode) will require further modifications to the opcode
1916 table. */
1917
1918static inline unsigned int
1919micromips_insn_length (const struct mips_opcode *mo)
1920{
1921 return (mo->mask >> 16) == 0 ? 2 : 4;
1922}
1923
5c04167a
RS
1924/* Return the length of MIPS16 instruction OPCODE. */
1925
1926static inline unsigned int
1927mips16_opcode_length (unsigned long opcode)
1928{
1929 return (opcode >> 16) == 0 ? 2 : 4;
1930}
1931
1e915849
RS
1932/* Return the length of instruction INSN. */
1933
1934static inline unsigned int
1935insn_length (const struct mips_cl_insn *insn)
1936{
df58fc94
RS
1937 if (mips_opts.micromips)
1938 return micromips_insn_length (insn->insn_mo);
1939 else if (mips_opts.mips16)
5c04167a 1940 return mips16_opcode_length (insn->insn_opcode);
df58fc94 1941 else
1e915849 1942 return 4;
1e915849
RS
1943}
1944
1945/* Initialise INSN from opcode entry MO. Leave its position unspecified. */
1946
1947static void
1948create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
1949{
1950 size_t i;
1951
1952 insn->insn_mo = mo;
1e915849
RS
1953 insn->insn_opcode = mo->match;
1954 insn->frag = NULL;
1955 insn->where = 0;
1956 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1957 insn->fixp[i] = NULL;
1958 insn->fixed_p = (mips_opts.noreorder > 0);
1959 insn->noreorder_p = (mips_opts.noreorder > 0);
1960 insn->mips16_absolute_jump_p = 0;
15be625d 1961 insn->complete_p = 0;
e407c74b 1962 insn->cleared_p = 0;
1e915849
RS
1963}
1964
fc76e730
RS
1965/* Get a list of all the operands in INSN. */
1966
1967static const struct mips_operand_array *
1968insn_operands (const struct mips_cl_insn *insn)
1969{
1970 if (insn->insn_mo >= &mips_opcodes[0]
1971 && insn->insn_mo < &mips_opcodes[NUMOPCODES])
1972 return &mips_operands[insn->insn_mo - &mips_opcodes[0]];
1973
1974 if (insn->insn_mo >= &mips16_opcodes[0]
1975 && insn->insn_mo < &mips16_opcodes[bfd_mips16_num_opcodes])
1976 return &mips16_operands[insn->insn_mo - &mips16_opcodes[0]];
1977
1978 if (insn->insn_mo >= &micromips_opcodes[0]
1979 && insn->insn_mo < &micromips_opcodes[bfd_micromips_num_opcodes])
1980 return &micromips_operands[insn->insn_mo - &micromips_opcodes[0]];
1981
1982 abort ();
1983}
1984
1985/* Get a description of operand OPNO of INSN. */
1986
1987static const struct mips_operand *
1988insn_opno (const struct mips_cl_insn *insn, unsigned opno)
1989{
1990 const struct mips_operand_array *operands;
1991
1992 operands = insn_operands (insn);
1993 if (opno >= MAX_OPERANDS || !operands->operand[opno])
1994 abort ();
1995 return operands->operand[opno];
1996}
1997
e077a1c8
RS
1998/* Install UVAL as the value of OPERAND in INSN. */
1999
2000static inline void
2001insn_insert_operand (struct mips_cl_insn *insn,
2002 const struct mips_operand *operand, unsigned int uval)
2003{
2004 insn->insn_opcode = mips_insert_operand (operand, insn->insn_opcode, uval);
2005}
2006
fc76e730
RS
2007/* Extract the value of OPERAND from INSN. */
2008
2009static inline unsigned
2010insn_extract_operand (const struct mips_cl_insn *insn,
2011 const struct mips_operand *operand)
2012{
2013 return mips_extract_operand (operand, insn->insn_opcode);
2014}
2015
df58fc94 2016/* Record the current MIPS16/microMIPS mode in now_seg. */
742a56fe
RS
2017
2018static void
df58fc94 2019mips_record_compressed_mode (void)
742a56fe
RS
2020{
2021 segment_info_type *si;
2022
2023 si = seg_info (now_seg);
2024 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2025 si->tc_segment_info_data.mips16 = mips_opts.mips16;
df58fc94
RS
2026 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2027 si->tc_segment_info_data.micromips = mips_opts.micromips;
742a56fe
RS
2028}
2029
4d68580a
RS
2030/* Read a standard MIPS instruction from BUF. */
2031
2032static unsigned long
2033read_insn (char *buf)
2034{
2035 if (target_big_endian)
2036 return bfd_getb32 ((bfd_byte *) buf);
2037 else
2038 return bfd_getl32 ((bfd_byte *) buf);
2039}
2040
2041/* Write standard MIPS instruction INSN to BUF. Return a pointer to
2042 the next byte. */
2043
2044static char *
2045write_insn (char *buf, unsigned int insn)
2046{
2047 md_number_to_chars (buf, insn, 4);
2048 return buf + 4;
2049}
2050
2051/* Read a microMIPS or MIPS16 opcode from BUF, given that it
2052 has length LENGTH. */
2053
2054static unsigned long
2055read_compressed_insn (char *buf, unsigned int length)
2056{
2057 unsigned long insn;
2058 unsigned int i;
2059
2060 insn = 0;
2061 for (i = 0; i < length; i += 2)
2062 {
2063 insn <<= 16;
2064 if (target_big_endian)
2065 insn |= bfd_getb16 ((char *) buf);
2066 else
2067 insn |= bfd_getl16 ((char *) buf);
2068 buf += 2;
2069 }
2070 return insn;
2071}
2072
5c04167a
RS
2073/* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2074 instruction is LENGTH bytes long. Return a pointer to the next byte. */
2075
2076static char *
2077write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2078{
2079 unsigned int i;
2080
2081 for (i = 0; i < length; i += 2)
2082 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2083 return buf + length;
2084}
2085
1e915849
RS
2086/* Install INSN at the location specified by its "frag" and "where" fields. */
2087
2088static void
2089install_insn (const struct mips_cl_insn *insn)
2090{
2091 char *f = insn->frag->fr_literal + insn->where;
5c04167a
RS
2092 if (HAVE_CODE_COMPRESSION)
2093 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
1e915849 2094 else
4d68580a 2095 write_insn (f, insn->insn_opcode);
df58fc94 2096 mips_record_compressed_mode ();
1e915849
RS
2097}
2098
2099/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
2100 and install the opcode in the new location. */
2101
2102static void
2103move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2104{
2105 size_t i;
2106
2107 insn->frag = frag;
2108 insn->where = where;
2109 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2110 if (insn->fixp[i] != NULL)
2111 {
2112 insn->fixp[i]->fx_frag = frag;
2113 insn->fixp[i]->fx_where = where;
2114 }
2115 install_insn (insn);
2116}
2117
2118/* Add INSN to the end of the output. */
2119
2120static void
2121add_fixed_insn (struct mips_cl_insn *insn)
2122{
2123 char *f = frag_more (insn_length (insn));
2124 move_insn (insn, frag_now, f - frag_now->fr_literal);
2125}
2126
2127/* Start a variant frag and move INSN to the start of the variant part,
2128 marking it as fixed. The other arguments are as for frag_var. */
2129
2130static void
2131add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2132 relax_substateT subtype, symbolS *symbol, offsetT offset)
2133{
2134 frag_grow (max_chars);
2135 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2136 insn->fixed_p = 1;
2137 frag_var (rs_machine_dependent, max_chars, var,
2138 subtype, symbol, offset, NULL);
2139}
2140
2141/* Insert N copies of INSN into the history buffer, starting at
2142 position FIRST. Neither FIRST nor N need to be clipped. */
2143
2144static void
2145insert_into_history (unsigned int first, unsigned int n,
2146 const struct mips_cl_insn *insn)
2147{
2148 if (mips_relax.sequence != 2)
2149 {
2150 unsigned int i;
2151
2152 for (i = ARRAY_SIZE (history); i-- > first;)
2153 if (i >= first + n)
2154 history[i] = history[i - n];
2155 else
2156 history[i] = *insn;
2157 }
2158}
2159
71400594
RS
2160/* Initialize vr4120_conflicts. There is a bit of duplication here:
2161 the idea is to make it obvious at a glance that each errata is
2162 included. */
2163
2164static void
2165init_vr4120_conflicts (void)
2166{
2167#define CONFLICT(FIRST, SECOND) \
2168 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2169
2170 /* Errata 21 - [D]DIV[U] after [D]MACC */
2171 CONFLICT (MACC, DIV);
2172 CONFLICT (DMACC, DIV);
2173
2174 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
2175 CONFLICT (DMULT, DMULT);
2176 CONFLICT (DMULT, DMACC);
2177 CONFLICT (DMACC, DMULT);
2178 CONFLICT (DMACC, DMACC);
2179
2180 /* Errata 24 - MT{LO,HI} after [D]MACC */
2181 CONFLICT (MACC, MTHILO);
2182 CONFLICT (DMACC, MTHILO);
2183
2184 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2185 instruction is executed immediately after a MACC or DMACC
2186 instruction, the result of [either instruction] is incorrect." */
2187 CONFLICT (MACC, MULT);
2188 CONFLICT (MACC, DMULT);
2189 CONFLICT (DMACC, MULT);
2190 CONFLICT (DMACC, DMULT);
2191
2192 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2193 executed immediately after a DMULT, DMULTU, DIV, DIVU,
2194 DDIV or DDIVU instruction, the result of the MACC or
2195 DMACC instruction is incorrect.". */
2196 CONFLICT (DMULT, MACC);
2197 CONFLICT (DMULT, DMACC);
2198 CONFLICT (DIV, MACC);
2199 CONFLICT (DIV, DMACC);
2200
2201#undef CONFLICT
2202}
2203
707bfff6
TS
2204struct regname {
2205 const char *name;
2206 unsigned int num;
2207};
2208
14daeee3
RS
2209#define RNUM_MASK 0x00000ff
2210#define RTYPE_MASK 0x0efff00
2211#define RTYPE_NUM 0x0000100
2212#define RTYPE_FPU 0x0000200
2213#define RTYPE_FCC 0x0000400
2214#define RTYPE_VEC 0x0000800
2215#define RTYPE_GP 0x0001000
2216#define RTYPE_CP0 0x0002000
2217#define RTYPE_PC 0x0004000
2218#define RTYPE_ACC 0x0008000
2219#define RTYPE_CCC 0x0010000
2220#define RTYPE_VI 0x0020000
2221#define RTYPE_VF 0x0040000
2222#define RTYPE_R5900_I 0x0080000
2223#define RTYPE_R5900_Q 0x0100000
2224#define RTYPE_R5900_R 0x0200000
2225#define RTYPE_R5900_ACC 0x0400000
2226#define RWARN 0x8000000
707bfff6
TS
2227
2228#define GENERIC_REGISTER_NUMBERS \
2229 {"$0", RTYPE_NUM | 0}, \
2230 {"$1", RTYPE_NUM | 1}, \
2231 {"$2", RTYPE_NUM | 2}, \
2232 {"$3", RTYPE_NUM | 3}, \
2233 {"$4", RTYPE_NUM | 4}, \
2234 {"$5", RTYPE_NUM | 5}, \
2235 {"$6", RTYPE_NUM | 6}, \
2236 {"$7", RTYPE_NUM | 7}, \
2237 {"$8", RTYPE_NUM | 8}, \
2238 {"$9", RTYPE_NUM | 9}, \
2239 {"$10", RTYPE_NUM | 10}, \
2240 {"$11", RTYPE_NUM | 11}, \
2241 {"$12", RTYPE_NUM | 12}, \
2242 {"$13", RTYPE_NUM | 13}, \
2243 {"$14", RTYPE_NUM | 14}, \
2244 {"$15", RTYPE_NUM | 15}, \
2245 {"$16", RTYPE_NUM | 16}, \
2246 {"$17", RTYPE_NUM | 17}, \
2247 {"$18", RTYPE_NUM | 18}, \
2248 {"$19", RTYPE_NUM | 19}, \
2249 {"$20", RTYPE_NUM | 20}, \
2250 {"$21", RTYPE_NUM | 21}, \
2251 {"$22", RTYPE_NUM | 22}, \
2252 {"$23", RTYPE_NUM | 23}, \
2253 {"$24", RTYPE_NUM | 24}, \
2254 {"$25", RTYPE_NUM | 25}, \
2255 {"$26", RTYPE_NUM | 26}, \
2256 {"$27", RTYPE_NUM | 27}, \
2257 {"$28", RTYPE_NUM | 28}, \
2258 {"$29", RTYPE_NUM | 29}, \
2259 {"$30", RTYPE_NUM | 30}, \
2260 {"$31", RTYPE_NUM | 31}
2261
2262#define FPU_REGISTER_NAMES \
2263 {"$f0", RTYPE_FPU | 0}, \
2264 {"$f1", RTYPE_FPU | 1}, \
2265 {"$f2", RTYPE_FPU | 2}, \
2266 {"$f3", RTYPE_FPU | 3}, \
2267 {"$f4", RTYPE_FPU | 4}, \
2268 {"$f5", RTYPE_FPU | 5}, \
2269 {"$f6", RTYPE_FPU | 6}, \
2270 {"$f7", RTYPE_FPU | 7}, \
2271 {"$f8", RTYPE_FPU | 8}, \
2272 {"$f9", RTYPE_FPU | 9}, \
2273 {"$f10", RTYPE_FPU | 10}, \
2274 {"$f11", RTYPE_FPU | 11}, \
2275 {"$f12", RTYPE_FPU | 12}, \
2276 {"$f13", RTYPE_FPU | 13}, \
2277 {"$f14", RTYPE_FPU | 14}, \
2278 {"$f15", RTYPE_FPU | 15}, \
2279 {"$f16", RTYPE_FPU | 16}, \
2280 {"$f17", RTYPE_FPU | 17}, \
2281 {"$f18", RTYPE_FPU | 18}, \
2282 {"$f19", RTYPE_FPU | 19}, \
2283 {"$f20", RTYPE_FPU | 20}, \
2284 {"$f21", RTYPE_FPU | 21}, \
2285 {"$f22", RTYPE_FPU | 22}, \
2286 {"$f23", RTYPE_FPU | 23}, \
2287 {"$f24", RTYPE_FPU | 24}, \
2288 {"$f25", RTYPE_FPU | 25}, \
2289 {"$f26", RTYPE_FPU | 26}, \
2290 {"$f27", RTYPE_FPU | 27}, \
2291 {"$f28", RTYPE_FPU | 28}, \
2292 {"$f29", RTYPE_FPU | 29}, \
2293 {"$f30", RTYPE_FPU | 30}, \
2294 {"$f31", RTYPE_FPU | 31}
2295
2296#define FPU_CONDITION_CODE_NAMES \
2297 {"$fcc0", RTYPE_FCC | 0}, \
2298 {"$fcc1", RTYPE_FCC | 1}, \
2299 {"$fcc2", RTYPE_FCC | 2}, \
2300 {"$fcc3", RTYPE_FCC | 3}, \
2301 {"$fcc4", RTYPE_FCC | 4}, \
2302 {"$fcc5", RTYPE_FCC | 5}, \
2303 {"$fcc6", RTYPE_FCC | 6}, \
2304 {"$fcc7", RTYPE_FCC | 7}
2305
2306#define COPROC_CONDITION_CODE_NAMES \
2307 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
2308 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
2309 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
2310 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
2311 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
2312 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
2313 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
2314 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
2315
2316#define N32N64_SYMBOLIC_REGISTER_NAMES \
2317 {"$a4", RTYPE_GP | 8}, \
2318 {"$a5", RTYPE_GP | 9}, \
2319 {"$a6", RTYPE_GP | 10}, \
2320 {"$a7", RTYPE_GP | 11}, \
2321 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
2322 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
2323 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
2324 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
2325 {"$t0", RTYPE_GP | 12}, \
2326 {"$t1", RTYPE_GP | 13}, \
2327 {"$t2", RTYPE_GP | 14}, \
2328 {"$t3", RTYPE_GP | 15}
2329
2330#define O32_SYMBOLIC_REGISTER_NAMES \
2331 {"$t0", RTYPE_GP | 8}, \
2332 {"$t1", RTYPE_GP | 9}, \
2333 {"$t2", RTYPE_GP | 10}, \
2334 {"$t3", RTYPE_GP | 11}, \
2335 {"$t4", RTYPE_GP | 12}, \
2336 {"$t5", RTYPE_GP | 13}, \
2337 {"$t6", RTYPE_GP | 14}, \
2338 {"$t7", RTYPE_GP | 15}, \
2339 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2340 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2341 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
2342 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
2343
2344/* Remaining symbolic register names */
2345#define SYMBOLIC_REGISTER_NAMES \
2346 {"$zero", RTYPE_GP | 0}, \
2347 {"$at", RTYPE_GP | 1}, \
2348 {"$AT", RTYPE_GP | 1}, \
2349 {"$v0", RTYPE_GP | 2}, \
2350 {"$v1", RTYPE_GP | 3}, \
2351 {"$a0", RTYPE_GP | 4}, \
2352 {"$a1", RTYPE_GP | 5}, \
2353 {"$a2", RTYPE_GP | 6}, \
2354 {"$a3", RTYPE_GP | 7}, \
2355 {"$s0", RTYPE_GP | 16}, \
2356 {"$s1", RTYPE_GP | 17}, \
2357 {"$s2", RTYPE_GP | 18}, \
2358 {"$s3", RTYPE_GP | 19}, \
2359 {"$s4", RTYPE_GP | 20}, \
2360 {"$s5", RTYPE_GP | 21}, \
2361 {"$s6", RTYPE_GP | 22}, \
2362 {"$s7", RTYPE_GP | 23}, \
2363 {"$t8", RTYPE_GP | 24}, \
2364 {"$t9", RTYPE_GP | 25}, \
2365 {"$k0", RTYPE_GP | 26}, \
2366 {"$kt0", RTYPE_GP | 26}, \
2367 {"$k1", RTYPE_GP | 27}, \
2368 {"$kt1", RTYPE_GP | 27}, \
2369 {"$gp", RTYPE_GP | 28}, \
2370 {"$sp", RTYPE_GP | 29}, \
2371 {"$s8", RTYPE_GP | 30}, \
2372 {"$fp", RTYPE_GP | 30}, \
2373 {"$ra", RTYPE_GP | 31}
2374
2375#define MIPS16_SPECIAL_REGISTER_NAMES \
2376 {"$pc", RTYPE_PC | 0}
2377
2378#define MDMX_VECTOR_REGISTER_NAMES \
2379 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
2380 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
2381 {"$v2", RTYPE_VEC | 2}, \
2382 {"$v3", RTYPE_VEC | 3}, \
2383 {"$v4", RTYPE_VEC | 4}, \
2384 {"$v5", RTYPE_VEC | 5}, \
2385 {"$v6", RTYPE_VEC | 6}, \
2386 {"$v7", RTYPE_VEC | 7}, \
2387 {"$v8", RTYPE_VEC | 8}, \
2388 {"$v9", RTYPE_VEC | 9}, \
2389 {"$v10", RTYPE_VEC | 10}, \
2390 {"$v11", RTYPE_VEC | 11}, \
2391 {"$v12", RTYPE_VEC | 12}, \
2392 {"$v13", RTYPE_VEC | 13}, \
2393 {"$v14", RTYPE_VEC | 14}, \
2394 {"$v15", RTYPE_VEC | 15}, \
2395 {"$v16", RTYPE_VEC | 16}, \
2396 {"$v17", RTYPE_VEC | 17}, \
2397 {"$v18", RTYPE_VEC | 18}, \
2398 {"$v19", RTYPE_VEC | 19}, \
2399 {"$v20", RTYPE_VEC | 20}, \
2400 {"$v21", RTYPE_VEC | 21}, \
2401 {"$v22", RTYPE_VEC | 22}, \
2402 {"$v23", RTYPE_VEC | 23}, \
2403 {"$v24", RTYPE_VEC | 24}, \
2404 {"$v25", RTYPE_VEC | 25}, \
2405 {"$v26", RTYPE_VEC | 26}, \
2406 {"$v27", RTYPE_VEC | 27}, \
2407 {"$v28", RTYPE_VEC | 28}, \
2408 {"$v29", RTYPE_VEC | 29}, \
2409 {"$v30", RTYPE_VEC | 30}, \
2410 {"$v31", RTYPE_VEC | 31}
2411
14daeee3
RS
2412#define R5900_I_NAMES \
2413 {"$I", RTYPE_R5900_I | 0}
2414
2415#define R5900_Q_NAMES \
2416 {"$Q", RTYPE_R5900_Q | 0}
2417
2418#define R5900_R_NAMES \
2419 {"$R", RTYPE_R5900_R | 0}
2420
2421#define R5900_ACC_NAMES \
2422 {"$ACC", RTYPE_R5900_ACC | 0 }
2423
707bfff6
TS
2424#define MIPS_DSP_ACCUMULATOR_NAMES \
2425 {"$ac0", RTYPE_ACC | 0}, \
2426 {"$ac1", RTYPE_ACC | 1}, \
2427 {"$ac2", RTYPE_ACC | 2}, \
2428 {"$ac3", RTYPE_ACC | 3}
2429
2430static const struct regname reg_names[] = {
2431 GENERIC_REGISTER_NUMBERS,
2432 FPU_REGISTER_NAMES,
2433 FPU_CONDITION_CODE_NAMES,
2434 COPROC_CONDITION_CODE_NAMES,
2435
2436 /* The $txx registers depends on the abi,
2437 these will be added later into the symbol table from
2438 one of the tables below once mips_abi is set after
2439 parsing of arguments from the command line. */
2440 SYMBOLIC_REGISTER_NAMES,
2441
2442 MIPS16_SPECIAL_REGISTER_NAMES,
2443 MDMX_VECTOR_REGISTER_NAMES,
14daeee3
RS
2444 R5900_I_NAMES,
2445 R5900_Q_NAMES,
2446 R5900_R_NAMES,
2447 R5900_ACC_NAMES,
707bfff6
TS
2448 MIPS_DSP_ACCUMULATOR_NAMES,
2449 {0, 0}
2450};
2451
2452static const struct regname reg_names_o32[] = {
2453 O32_SYMBOLIC_REGISTER_NAMES,
2454 {0, 0}
2455};
2456
2457static const struct regname reg_names_n32n64[] = {
2458 N32N64_SYMBOLIC_REGISTER_NAMES,
2459 {0, 0}
2460};
2461
a92713e6
RS
2462/* Register symbols $v0 and $v1 map to GPRs 2 and 3, but they can also be
2463 interpreted as vector registers 0 and 1. If SYMVAL is the value of one
2464 of these register symbols, return the associated vector register,
2465 otherwise return SYMVAL itself. */
df58fc94 2466
a92713e6
RS
2467static unsigned int
2468mips_prefer_vec_regno (unsigned int symval)
707bfff6 2469{
a92713e6
RS
2470 if ((symval & -2) == (RTYPE_GP | 2))
2471 return RTYPE_VEC | (symval & 1);
2472 return symval;
2473}
2474
14daeee3
RS
2475/* Return true if string [S, E) is a valid register name, storing its
2476 symbol value in *SYMVAL_PTR if so. */
a92713e6
RS
2477
2478static bfd_boolean
14daeee3 2479mips_parse_register_1 (char *s, char *e, unsigned int *symval_ptr)
a92713e6 2480{
707bfff6 2481 char save_c;
14daeee3 2482 symbolS *symbol;
707bfff6
TS
2483
2484 /* Terminate name. */
2485 save_c = *e;
2486 *e = '\0';
2487
a92713e6
RS
2488 /* Look up the name. */
2489 symbol = symbol_find (s);
2490 *e = save_c;
2491
2492 if (!symbol || S_GET_SEGMENT (symbol) != reg_section)
2493 return FALSE;
2494
14daeee3
RS
2495 *symval_ptr = S_GET_VALUE (symbol);
2496 return TRUE;
2497}
2498
2499/* Return true if the string at *SPTR is a valid register name. Allow it
2500 to have a VU0-style channel suffix of the form x?y?z?w? if CHANNELS_PTR
2501 is nonnull.
2502
2503 When returning true, move *SPTR past the register, store the
2504 register's symbol value in *SYMVAL_PTR and the channel mask in
2505 *CHANNELS_PTR (if nonnull). The symbol value includes the register
2506 number (RNUM_MASK) and register type (RTYPE_MASK). The channel mask
2507 is a 4-bit value of the form XYZW and is 0 if no suffix was given. */
2508
2509static bfd_boolean
2510mips_parse_register (char **sptr, unsigned int *symval_ptr,
2511 unsigned int *channels_ptr)
2512{
2513 char *s, *e, *m;
2514 const char *q;
2515 unsigned int channels, symval, bit;
2516
2517 /* Find end of name. */
2518 s = e = *sptr;
2519 if (is_name_beginner (*e))
2520 ++e;
2521 while (is_part_of_name (*e))
2522 ++e;
2523
2524 channels = 0;
2525 if (!mips_parse_register_1 (s, e, &symval))
2526 {
2527 if (!channels_ptr)
2528 return FALSE;
2529
2530 /* Eat characters from the end of the string that are valid
2531 channel suffixes. The preceding register must be $ACC or
2532 end with a digit, so there is no ambiguity. */
2533 bit = 1;
2534 m = e;
2535 for (q = "wzyx"; *q; q++, bit <<= 1)
2536 if (m > s && m[-1] == *q)
2537 {
2538 --m;
2539 channels |= bit;
2540 }
2541
2542 if (channels == 0
2543 || !mips_parse_register_1 (s, m, &symval)
2544 || (symval & (RTYPE_VI | RTYPE_VF | RTYPE_R5900_ACC)) == 0)
2545 return FALSE;
2546 }
2547
a92713e6 2548 *sptr = e;
14daeee3
RS
2549 *symval_ptr = symval;
2550 if (channels_ptr)
2551 *channels_ptr = channels;
a92713e6
RS
2552 return TRUE;
2553}
2554
2555/* Check if SPTR points at a valid register specifier according to TYPES.
2556 If so, then return 1, advance S to consume the specifier and store
2557 the register's number in REGNOP, otherwise return 0. */
2558
2559static int
2560reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2561{
2562 unsigned int regno;
2563
14daeee3 2564 if (mips_parse_register (s, &regno, NULL))
707bfff6 2565 {
a92713e6
RS
2566 if (types & RTYPE_VEC)
2567 regno = mips_prefer_vec_regno (regno);
2568 if (regno & types)
2569 regno &= RNUM_MASK;
2570 else
2571 regno = ~0;
707bfff6 2572 }
a92713e6 2573 else
707bfff6 2574 {
a92713e6
RS
2575 if (types & RWARN)
2576 as_warn (_("Unrecognized register name `%s'"), *s);
2577 regno = ~0;
707bfff6 2578 }
707bfff6 2579 if (regnop)
a92713e6
RS
2580 *regnop = regno;
2581 return regno <= RNUM_MASK;
707bfff6
TS
2582}
2583
14daeee3
RS
2584/* Parse a VU0 "x?y?z?w?" channel mask at S and store the associated
2585 mask in *CHANNELS. Return a pointer to the first unconsumed character. */
2586
2587static char *
2588mips_parse_vu0_channels (char *s, unsigned int *channels)
2589{
2590 unsigned int i;
2591
2592 *channels = 0;
2593 for (i = 0; i < 4; i++)
2594 if (*s == "xyzw"[i])
2595 {
2596 *channels |= 1 << (3 - i);
2597 ++s;
2598 }
2599 return s;
2600}
2601
a92713e6
RS
2602/* Token types for parsed operand lists. */
2603enum mips_operand_token_type {
2604 /* A plain register, e.g. $f2. */
2605 OT_REG,
df58fc94 2606
14daeee3
RS
2607 /* A 4-bit XYZW channel mask. */
2608 OT_CHANNELS,
2609
a92713e6
RS
2610 /* An element of a vector, e.g. $v0[1]. */
2611 OT_REG_ELEMENT,
df58fc94 2612
a92713e6
RS
2613 /* A continuous range of registers, e.g. $s0-$s4. */
2614 OT_REG_RANGE,
2615
2616 /* A (possibly relocated) expression. */
2617 OT_INTEGER,
2618
2619 /* A floating-point value. */
2620 OT_FLOAT,
2621
2622 /* A single character. This can be '(', ')' or ',', but '(' only appears
2623 before OT_REGs. */
2624 OT_CHAR,
2625
14daeee3
RS
2626 /* A doubled character, either "--" or "++". */
2627 OT_DOUBLE_CHAR,
2628
a92713e6
RS
2629 /* The end of the operand list. */
2630 OT_END
2631};
2632
2633/* A parsed operand token. */
2634struct mips_operand_token
2635{
2636 /* The type of token. */
2637 enum mips_operand_token_type type;
2638 union
2639 {
2640 /* The register symbol value for an OT_REG. */
2641 unsigned int regno;
2642
14daeee3
RS
2643 /* The 4-bit channel mask for an OT_CHANNEL_SUFFIX. */
2644 unsigned int channels;
2645
a92713e6
RS
2646 /* The register symbol value and index for an OT_REG_ELEMENT. */
2647 struct {
2648 unsigned int regno;
2649 addressT index;
2650 } reg_element;
2651
2652 /* The two register symbol values involved in an OT_REG_RANGE. */
2653 struct {
2654 unsigned int regno1;
2655 unsigned int regno2;
2656 } reg_range;
2657
2658 /* The value of an OT_INTEGER. The value is represented as an
2659 expression and the relocation operators that were applied to
2660 that expression. The reloc entries are BFD_RELOC_UNUSED if no
2661 relocation operators were used. */
2662 struct {
2663 expressionS value;
2664 bfd_reloc_code_real_type relocs[3];
2665 } integer;
2666
2667 /* The binary data for an OT_FLOAT constant, and the number of bytes
2668 in the constant. */
2669 struct {
2670 unsigned char data[8];
2671 int length;
2672 } flt;
2673
14daeee3 2674 /* The character represented by an OT_CHAR or OT_DOUBLE_CHAR. */
a92713e6
RS
2675 char ch;
2676 } u;
2677};
2678
2679/* An obstack used to construct lists of mips_operand_tokens. */
2680static struct obstack mips_operand_tokens;
2681
2682/* Give TOKEN type TYPE and add it to mips_operand_tokens. */
2683
2684static void
2685mips_add_token (struct mips_operand_token *token,
2686 enum mips_operand_token_type type)
2687{
2688 token->type = type;
2689 obstack_grow (&mips_operand_tokens, token, sizeof (*token));
2690}
2691
2692/* Check whether S is '(' followed by a register name. Add OT_CHAR
2693 and OT_REG tokens for them if so, and return a pointer to the first
2694 unconsumed character. Return null otherwise. */
2695
2696static char *
2697mips_parse_base_start (char *s)
2698{
2699 struct mips_operand_token token;
14daeee3
RS
2700 unsigned int regno, channels;
2701 bfd_boolean decrement_p;
df58fc94 2702
a92713e6
RS
2703 if (*s != '(')
2704 return 0;
2705
2706 ++s;
2707 SKIP_SPACE_TABS (s);
14daeee3
RS
2708
2709 /* Only match "--" as part of a base expression. In other contexts "--X"
2710 is a double negative. */
2711 decrement_p = (s[0] == '-' && s[1] == '-');
2712 if (decrement_p)
2713 {
2714 s += 2;
2715 SKIP_SPACE_TABS (s);
2716 }
2717
2718 /* Allow a channel specifier because that leads to better error messages
2719 than treating something like "$vf0x++" as an expression. */
2720 if (!mips_parse_register (&s, &regno, &channels))
a92713e6
RS
2721 return 0;
2722
2723 token.u.ch = '(';
2724 mips_add_token (&token, OT_CHAR);
2725
14daeee3
RS
2726 if (decrement_p)
2727 {
2728 token.u.ch = '-';
2729 mips_add_token (&token, OT_DOUBLE_CHAR);
2730 }
2731
a92713e6
RS
2732 token.u.regno = regno;
2733 mips_add_token (&token, OT_REG);
2734
14daeee3
RS
2735 if (channels)
2736 {
2737 token.u.channels = channels;
2738 mips_add_token (&token, OT_CHANNELS);
2739 }
2740
2741 /* For consistency, only match "++" as part of base expressions too. */
2742 SKIP_SPACE_TABS (s);
2743 if (s[0] == '+' && s[1] == '+')
2744 {
2745 s += 2;
2746 token.u.ch = '+';
2747 mips_add_token (&token, OT_DOUBLE_CHAR);
2748 }
2749
a92713e6
RS
2750 return s;
2751}
2752
2753/* Parse one or more tokens from S. Return a pointer to the first
2754 unconsumed character on success. Return null if an error was found
2755 and store the error text in insn_error. FLOAT_FORMAT is as for
2756 mips_parse_arguments. */
2757
2758static char *
2759mips_parse_argument_token (char *s, char float_format)
2760{
2761 char *end, *save_in, *err;
14daeee3 2762 unsigned int regno1, regno2, channels;
a92713e6
RS
2763 struct mips_operand_token token;
2764
2765 /* First look for "($reg", since we want to treat that as an
2766 OT_CHAR and OT_REG rather than an expression. */
2767 end = mips_parse_base_start (s);
2768 if (end)
2769 return end;
2770
2771 /* Handle other characters that end up as OT_CHARs. */
2772 if (*s == ')' || *s == ',')
2773 {
2774 token.u.ch = *s;
2775 mips_add_token (&token, OT_CHAR);
2776 ++s;
2777 return s;
2778 }
2779
2780 /* Handle tokens that start with a register. */
14daeee3 2781 if (mips_parse_register (&s, &regno1, &channels))
df58fc94 2782 {
14daeee3
RS
2783 if (channels)
2784 {
2785 /* A register and a VU0 channel suffix. */
2786 token.u.regno = regno1;
2787 mips_add_token (&token, OT_REG);
2788
2789 token.u.channels = channels;
2790 mips_add_token (&token, OT_CHANNELS);
2791 return s;
2792 }
2793
a92713e6
RS
2794 SKIP_SPACE_TABS (s);
2795 if (*s == '-')
df58fc94 2796 {
a92713e6
RS
2797 /* A register range. */
2798 ++s;
2799 SKIP_SPACE_TABS (s);
14daeee3 2800 if (!mips_parse_register (&s, &regno2, NULL))
a92713e6
RS
2801 {
2802 insn_error = _("Invalid register range");
2803 return 0;
2804 }
df58fc94 2805
a92713e6
RS
2806 token.u.reg_range.regno1 = regno1;
2807 token.u.reg_range.regno2 = regno2;
2808 mips_add_token (&token, OT_REG_RANGE);
2809 return s;
2810 }
2811 else if (*s == '[')
df58fc94 2812 {
a92713e6
RS
2813 /* A vector element. */
2814 expressionS element;
2815
2816 ++s;
2817 SKIP_SPACE_TABS (s);
2818 my_getExpression (&element, s);
2819 if (element.X_op != O_constant)
2820 {
2821 insn_error = _("Vector element must be constant");
2822 return 0;
2823 }
2824 s = expr_end;
2825 SKIP_SPACE_TABS (s);
2826 if (*s != ']')
2827 {
2828 insn_error = _("Missing `]'");
2829 return 0;
2830 }
2831 ++s;
2832
2833 token.u.reg_element.regno = regno1;
2834 token.u.reg_element.index = element.X_add_number;
2835 mips_add_token (&token, OT_REG_ELEMENT);
2836 return s;
df58fc94 2837 }
df58fc94 2838
a92713e6
RS
2839 /* Looks like just a plain register. */
2840 token.u.regno = regno1;
2841 mips_add_token (&token, OT_REG);
2842 return s;
df58fc94
RS
2843 }
2844
a92713e6
RS
2845 if (float_format)
2846 {
2847 /* First try to treat expressions as floats. */
2848 save_in = input_line_pointer;
2849 input_line_pointer = s;
2850 err = md_atof (float_format, (char *) token.u.flt.data,
2851 &token.u.flt.length);
2852 end = input_line_pointer;
2853 input_line_pointer = save_in;
2854 if (err && *err)
2855 {
2856 insn_error = err;
2857 return 0;
2858 }
2859 if (s != end)
2860 {
2861 mips_add_token (&token, OT_FLOAT);
2862 return end;
2863 }
2864 }
2865
2866 /* Treat everything else as an integer expression. */
2867 token.u.integer.relocs[0] = BFD_RELOC_UNUSED;
2868 token.u.integer.relocs[1] = BFD_RELOC_UNUSED;
2869 token.u.integer.relocs[2] = BFD_RELOC_UNUSED;
2870 my_getSmallExpression (&token.u.integer.value, token.u.integer.relocs, s);
2871 s = expr_end;
2872 mips_add_token (&token, OT_INTEGER);
2873 return s;
2874}
2875
2876/* S points to the operand list for an instruction. FLOAT_FORMAT is 'f'
2877 if expressions should be treated as 32-bit floating-point constants,
2878 'd' if they should be treated as 64-bit floating-point constants,
2879 or 0 if they should be treated as integer expressions (the usual case).
2880
2881 Return a list of tokens on success, otherwise return 0. The caller
2882 must obstack_free the list after use. */
2883
2884static struct mips_operand_token *
2885mips_parse_arguments (char *s, char float_format)
2886{
2887 struct mips_operand_token token;
2888
2889 SKIP_SPACE_TABS (s);
2890 while (*s)
2891 {
2892 s = mips_parse_argument_token (s, float_format);
2893 if (!s)
2894 {
2895 obstack_free (&mips_operand_tokens,
2896 obstack_finish (&mips_operand_tokens));
2897 return 0;
2898 }
2899 SKIP_SPACE_TABS (s);
2900 }
2901 mips_add_token (&token, OT_END);
2902 return (struct mips_operand_token *) obstack_finish (&mips_operand_tokens);
df58fc94
RS
2903}
2904
d301a56b
RS
2905/* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
2906 and architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
037b32b9
AN
2907
2908static bfd_boolean
f79e2745 2909is_opcode_valid (const struct mips_opcode *mo)
037b32b9
AN
2910{
2911 int isa = mips_opts.isa;
846ef2d0 2912 int ase = mips_opts.ase;
037b32b9 2913 int fp_s, fp_d;
c6278170 2914 unsigned int i;
037b32b9 2915
c6278170
RS
2916 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
2917 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2918 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
2919 ase |= mips_ases[i].flags64;
037b32b9 2920
d301a56b 2921 if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
037b32b9
AN
2922 return FALSE;
2923
2924 /* Check whether the instruction or macro requires single-precision or
2925 double-precision floating-point support. Note that this information is
2926 stored differently in the opcode table for insns and macros. */
2927 if (mo->pinfo == INSN_MACRO)
2928 {
2929 fp_s = mo->pinfo2 & INSN2_M_FP_S;
2930 fp_d = mo->pinfo2 & INSN2_M_FP_D;
2931 }
2932 else
2933 {
2934 fp_s = mo->pinfo & FP_S;
2935 fp_d = mo->pinfo & FP_D;
2936 }
2937
2938 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
2939 return FALSE;
2940
2941 if (fp_s && mips_opts.soft_float)
2942 return FALSE;
2943
2944 return TRUE;
2945}
2946
2947/* Return TRUE if the MIPS16 opcode MO is valid on the currently
2948 selected ISA and architecture. */
2949
2950static bfd_boolean
2951is_opcode_valid_16 (const struct mips_opcode *mo)
2952{
d301a56b 2953 return opcode_is_member (mo, mips_opts.isa, 0, mips_opts.arch);
037b32b9
AN
2954}
2955
df58fc94
RS
2956/* Return TRUE if the size of the microMIPS opcode MO matches one
2957 explicitly requested. Always TRUE in the standard MIPS mode. */
2958
2959static bfd_boolean
2960is_size_valid (const struct mips_opcode *mo)
2961{
2962 if (!mips_opts.micromips)
2963 return TRUE;
2964
833794fc
MR
2965 if (mips_opts.insn32)
2966 {
2967 if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
2968 return FALSE;
2969 if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
2970 return FALSE;
2971 }
df58fc94
RS
2972 if (!forced_insn_length)
2973 return TRUE;
2974 if (mo->pinfo == INSN_MACRO)
2975 return FALSE;
2976 return forced_insn_length == micromips_insn_length (mo);
2977}
2978
2979/* Return TRUE if the microMIPS opcode MO is valid for the delay slot
e64af278
MR
2980 of the preceding instruction. Always TRUE in the standard MIPS mode.
2981
2982 We don't accept macros in 16-bit delay slots to avoid a case where
2983 a macro expansion fails because it relies on a preceding 32-bit real
2984 instruction to have matched and does not handle the operands correctly.
2985 The only macros that may expand to 16-bit instructions are JAL that
2986 cannot be placed in a delay slot anyway, and corner cases of BALIGN
2987 and BGT (that likewise cannot be placed in a delay slot) that decay to
2988 a NOP. In all these cases the macros precede any corresponding real
2989 instruction definitions in the opcode table, so they will match in the
2990 second pass where the size of the delay slot is ignored and therefore
2991 produce correct code. */
df58fc94
RS
2992
2993static bfd_boolean
2994is_delay_slot_valid (const struct mips_opcode *mo)
2995{
2996 if (!mips_opts.micromips)
2997 return TRUE;
2998
2999 if (mo->pinfo == INSN_MACRO)
c06dec14 3000 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
df58fc94
RS
3001 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
3002 && micromips_insn_length (mo) != 4)
3003 return FALSE;
3004 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
3005 && micromips_insn_length (mo) != 2)
3006 return FALSE;
3007
3008 return TRUE;
3009}
3010
fc76e730
RS
3011/* For consistency checking, verify that all bits of OPCODE are specified
3012 either by the match/mask part of the instruction definition, or by the
3013 operand list. Also build up a list of operands in OPERANDS.
3014
3015 INSN_BITS says which bits of the instruction are significant.
3016 If OPCODE is a standard or microMIPS instruction, DECODE_OPERAND
3017 provides the mips_operand description of each operand. DECODE_OPERAND
3018 is null for MIPS16 instructions. */
ab902481
RS
3019
3020static int
3021validate_mips_insn (const struct mips_opcode *opcode,
3022 unsigned long insn_bits,
fc76e730
RS
3023 const struct mips_operand *(*decode_operand) (const char *),
3024 struct mips_operand_array *operands)
ab902481
RS
3025{
3026 const char *s;
fc76e730 3027 unsigned long used_bits, doubled, undefined, opno, mask;
ab902481
RS
3028 const struct mips_operand *operand;
3029
fc76e730
RS
3030 mask = (opcode->pinfo == INSN_MACRO ? 0 : opcode->mask);
3031 if ((mask & opcode->match) != opcode->match)
ab902481
RS
3032 {
3033 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
3034 opcode->name, opcode->args);
3035 return 0;
3036 }
3037 used_bits = 0;
fc76e730 3038 opno = 0;
14daeee3
RS
3039 if (opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX)
3040 used_bits = mips_insert_operand (&mips_vu0_channel_mask, used_bits, -1);
ab902481
RS
3041 for (s = opcode->args; *s; ++s)
3042 switch (*s)
3043 {
3044 case ',':
3045 case '(':
3046 case ')':
3047 break;
3048
14daeee3
RS
3049 case '#':
3050 s++;
3051 break;
3052
ab902481 3053 default:
fc76e730
RS
3054 if (!decode_operand)
3055 operand = decode_mips16_operand (*s, FALSE);
3056 else
3057 operand = decode_operand (s);
3058 if (!operand && opcode->pinfo != INSN_MACRO)
ab902481
RS
3059 {
3060 as_bad (_("internal: unknown operand type: %s %s"),
3061 opcode->name, opcode->args);
3062 return 0;
3063 }
fc76e730
RS
3064 gas_assert (opno < MAX_OPERANDS);
3065 operands->operand[opno] = operand;
14daeee3 3066 if (operand && operand->type != OP_VU0_MATCH_SUFFIX)
fc76e730 3067 {
14daeee3 3068 used_bits = mips_insert_operand (operand, used_bits, -1);
fc76e730
RS
3069 if (operand->type == OP_MDMX_IMM_REG)
3070 /* Bit 5 is the format selector (OB vs QH). The opcode table
3071 has separate entries for each format. */
3072 used_bits &= ~(1 << (operand->lsb + 5));
3073 if (operand->type == OP_ENTRY_EXIT_LIST)
3074 used_bits &= ~(mask & 0x700);
3075 }
ab902481 3076 /* Skip prefix characters. */
fc76e730 3077 if (decode_operand && (*s == '+' || *s == 'm'))
ab902481 3078 ++s;
fc76e730 3079 opno += 1;
ab902481
RS
3080 break;
3081 }
fc76e730 3082 doubled = used_bits & mask & insn_bits;
ab902481
RS
3083 if (doubled)
3084 {
3085 as_bad (_("internal: bad mips opcode (bits 0x%08lx doubly defined):"
3086 " %s %s"), doubled, opcode->name, opcode->args);
3087 return 0;
3088 }
fc76e730 3089 used_bits |= mask;
ab902481 3090 undefined = ~used_bits & insn_bits;
fc76e730 3091 if (opcode->pinfo != INSN_MACRO && undefined)
ab902481
RS
3092 {
3093 as_bad (_("internal: bad mips opcode (bits 0x%08lx undefined): %s %s"),
3094 undefined, opcode->name, opcode->args);
3095 return 0;
3096 }
3097 used_bits &= ~insn_bits;
3098 if (used_bits)
3099 {
3100 as_bad (_("internal: bad mips opcode (bits 0x%08lx defined): %s %s"),
3101 used_bits, opcode->name, opcode->args);
3102 return 0;
3103 }
3104 return 1;
3105}
3106
fc76e730
RS
3107/* The MIPS16 version of validate_mips_insn. */
3108
3109static int
3110validate_mips16_insn (const struct mips_opcode *opcode,
3111 struct mips_operand_array *operands)
3112{
3113 if (opcode->args[0] == 'a' || opcode->args[0] == 'i')
3114 {
3115 /* In this case OPCODE defines the first 16 bits in a 32-bit jump
3116 instruction. Use TMP to describe the full instruction. */
3117 struct mips_opcode tmp;
3118
3119 tmp = *opcode;
3120 tmp.match <<= 16;
3121 tmp.mask <<= 16;
3122 return validate_mips_insn (&tmp, 0xffffffff, 0, operands);
3123 }
3124 return validate_mips_insn (opcode, 0xffff, 0, operands);
3125}
3126
ab902481
RS
3127/* The microMIPS version of validate_mips_insn. */
3128
3129static int
fc76e730
RS
3130validate_micromips_insn (const struct mips_opcode *opc,
3131 struct mips_operand_array *operands)
ab902481
RS
3132{
3133 unsigned long insn_bits;
3134 unsigned long major;
3135 unsigned int length;
3136
fc76e730
RS
3137 if (opc->pinfo == INSN_MACRO)
3138 return validate_mips_insn (opc, 0xffffffff, decode_micromips_operand,
3139 operands);
3140
ab902481
RS
3141 length = micromips_insn_length (opc);
3142 if (length != 2 && length != 4)
3143 {
3144 as_bad (_("Internal error: bad microMIPS opcode (incorrect length: %u): "
3145 "%s %s"), length, opc->name, opc->args);
3146 return 0;
3147 }
3148 major = opc->match >> (10 + 8 * (length - 2));
3149 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
3150 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
3151 {
3152 as_bad (_("Internal error: bad microMIPS opcode "
3153 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
3154 return 0;
3155 }
3156
3157 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
3158 insn_bits = 1 << 4 * length;
3159 insn_bits <<= 4 * length;
3160 insn_bits -= 1;
fc76e730
RS
3161 return validate_mips_insn (opc, insn_bits, decode_micromips_operand,
3162 operands);
ab902481
RS
3163}
3164
707bfff6
TS
3165/* This function is called once, at assembler startup time. It should set up
3166 all the tables, etc. that the MD part of the assembler will need. */
156c2f8b 3167
252b5132 3168void
17a2f251 3169md_begin (void)
252b5132 3170{
3994f87e 3171 const char *retval = NULL;
156c2f8b 3172 int i = 0;
252b5132 3173 int broken = 0;
1f25f5d3 3174
0a44bf69
RS
3175 if (mips_pic != NO_PIC)
3176 {
3177 if (g_switch_seen && g_switch_value != 0)
3178 as_bad (_("-G may not be used in position-independent code"));
3179 g_switch_value = 0;
3180 }
3181
fef14a42 3182 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
252b5132
RH
3183 as_warn (_("Could not set architecture and machine"));
3184
252b5132
RH
3185 op_hash = hash_new ();
3186
fc76e730 3187 mips_operands = XCNEWVEC (struct mips_operand_array, NUMOPCODES);
252b5132
RH
3188 for (i = 0; i < NUMOPCODES;)
3189 {
3190 const char *name = mips_opcodes[i].name;
3191
17a2f251 3192 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
3193 if (retval != NULL)
3194 {
3195 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
3196 mips_opcodes[i].name, retval);
3197 /* Probably a memory allocation problem? Give up now. */
3198 as_fatal (_("Broken assembler. No assembly attempted."));
3199 }
3200 do
3201 {
fc76e730
RS
3202 if (!validate_mips_insn (&mips_opcodes[i], 0xffffffff,
3203 decode_mips_operand, &mips_operands[i]))
3204 broken = 1;
3205 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
252b5132 3206 {
fc76e730
RS
3207 create_insn (&nop_insn, mips_opcodes + i);
3208 if (mips_fix_loongson2f_nop)
3209 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
3210 nop_insn.fixed_p = 1;
252b5132
RH
3211 }
3212 ++i;
3213 }
3214 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
3215 }
3216
3217 mips16_op_hash = hash_new ();
fc76e730
RS
3218 mips16_operands = XCNEWVEC (struct mips_operand_array,
3219 bfd_mips16_num_opcodes);
252b5132
RH
3220
3221 i = 0;
3222 while (i < bfd_mips16_num_opcodes)
3223 {
3224 const char *name = mips16_opcodes[i].name;
3225
17a2f251 3226 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
3227 if (retval != NULL)
3228 as_fatal (_("internal: can't hash `%s': %s"),
3229 mips16_opcodes[i].name, retval);
3230 do
3231 {
fc76e730
RS
3232 if (!validate_mips16_insn (&mips16_opcodes[i], &mips16_operands[i]))
3233 broken = 1;
1e915849
RS
3234 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3235 {
3236 create_insn (&mips16_nop_insn, mips16_opcodes + i);
3237 mips16_nop_insn.fixed_p = 1;
3238 }
252b5132
RH
3239 ++i;
3240 }
3241 while (i < bfd_mips16_num_opcodes
3242 && strcmp (mips16_opcodes[i].name, name) == 0);
3243 }
3244
df58fc94 3245 micromips_op_hash = hash_new ();
fc76e730
RS
3246 micromips_operands = XCNEWVEC (struct mips_operand_array,
3247 bfd_micromips_num_opcodes);
df58fc94
RS
3248
3249 i = 0;
3250 while (i < bfd_micromips_num_opcodes)
3251 {
3252 const char *name = micromips_opcodes[i].name;
3253
3254 retval = hash_insert (micromips_op_hash, name,
3255 (void *) &micromips_opcodes[i]);
3256 if (retval != NULL)
3257 as_fatal (_("internal: can't hash `%s': %s"),
3258 micromips_opcodes[i].name, retval);
3259 do
fc76e730
RS
3260 {
3261 struct mips_cl_insn *micromips_nop_insn;
3262
3263 if (!validate_micromips_insn (&micromips_opcodes[i],
3264 &micromips_operands[i]))
3265 broken = 1;
3266
3267 if (micromips_opcodes[i].pinfo != INSN_MACRO)
3268 {
3269 if (micromips_insn_length (micromips_opcodes + i) == 2)
3270 micromips_nop_insn = &micromips_nop16_insn;
3271 else if (micromips_insn_length (micromips_opcodes + i) == 4)
3272 micromips_nop_insn = &micromips_nop32_insn;
3273 else
3274 continue;
3275
3276 if (micromips_nop_insn->insn_mo == NULL
3277 && strcmp (name, "nop") == 0)
3278 {
3279 create_insn (micromips_nop_insn, micromips_opcodes + i);
3280 micromips_nop_insn->fixed_p = 1;
3281 }
3282 }
3283 }
df58fc94
RS
3284 while (++i < bfd_micromips_num_opcodes
3285 && strcmp (micromips_opcodes[i].name, name) == 0);
3286 }
3287
252b5132
RH
3288 if (broken)
3289 as_fatal (_("Broken assembler. No assembly attempted."));
3290
3291 /* We add all the general register names to the symbol table. This
3292 helps us detect invalid uses of them. */
707bfff6
TS
3293 for (i = 0; reg_names[i].name; i++)
3294 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
8fc4ee9b 3295 reg_names[i].num, /* & RNUM_MASK, */
707bfff6
TS
3296 &zero_address_frag));
3297 if (HAVE_NEWABI)
3298 for (i = 0; reg_names_n32n64[i].name; i++)
3299 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
8fc4ee9b 3300 reg_names_n32n64[i].num, /* & RNUM_MASK, */
252b5132 3301 &zero_address_frag));
707bfff6
TS
3302 else
3303 for (i = 0; reg_names_o32[i].name; i++)
3304 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
8fc4ee9b 3305 reg_names_o32[i].num, /* & RNUM_MASK, */
6047c971 3306 &zero_address_frag));
6047c971 3307
14daeee3
RS
3308 for (i = 0; i < 32; i++)
3309 {
3310 char regname[7];
3311
3312 /* R5900 VU0 floating-point register. */
3313 regname[sizeof (rename) - 1] = 0;
3314 snprintf (regname, sizeof (regname) - 1, "$vf%d", i);
3315 symbol_table_insert (symbol_new (regname, reg_section,
3316 RTYPE_VF | i, &zero_address_frag));
3317
3318 /* R5900 VU0 integer register. */
3319 snprintf (regname, sizeof (regname) - 1, "$vi%d", i);
3320 symbol_table_insert (symbol_new (regname, reg_section,
3321 RTYPE_VI | i, &zero_address_frag));
3322
3323 }
3324
a92713e6
RS
3325 obstack_init (&mips_operand_tokens);
3326
7d10b47d 3327 mips_no_prev_insn ();
252b5132
RH
3328
3329 mips_gprmask = 0;
3330 mips_cprmask[0] = 0;
3331 mips_cprmask[1] = 0;
3332 mips_cprmask[2] = 0;
3333 mips_cprmask[3] = 0;
3334
3335 /* set the default alignment for the text section (2**2) */
3336 record_alignment (text_section, 2);
3337
4d0d148d 3338 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132 3339
f3ded42a
RS
3340 /* On a native system other than VxWorks, sections must be aligned
3341 to 16 byte boundaries. When configured for an embedded ELF
3342 target, we don't bother. */
3343 if (strncmp (TARGET_OS, "elf", 3) != 0
3344 && strncmp (TARGET_OS, "vxworks", 7) != 0)
252b5132 3345 {
f3ded42a
RS
3346 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
3347 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
3348 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
3349 }
252b5132 3350
f3ded42a
RS
3351 /* Create a .reginfo section for register masks and a .mdebug
3352 section for debugging information. */
3353 {
3354 segT seg;
3355 subsegT subseg;
3356 flagword flags;
3357 segT sec;
3358
3359 seg = now_seg;
3360 subseg = now_subseg;
3361
3362 /* The ABI says this section should be loaded so that the
3363 running program can access it. However, we don't load it
3364 if we are configured for an embedded target */
3365 flags = SEC_READONLY | SEC_DATA;
3366 if (strncmp (TARGET_OS, "elf", 3) != 0)
3367 flags |= SEC_ALLOC | SEC_LOAD;
3368
3369 if (mips_abi != N64_ABI)
252b5132 3370 {
f3ded42a 3371 sec = subseg_new (".reginfo", (subsegT) 0);
bdaaa2e1 3372
f3ded42a
RS
3373 bfd_set_section_flags (stdoutput, sec, flags);
3374 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
252b5132 3375
f3ded42a
RS
3376 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
3377 }
3378 else
3379 {
3380 /* The 64-bit ABI uses a .MIPS.options section rather than
3381 .reginfo section. */
3382 sec = subseg_new (".MIPS.options", (subsegT) 0);
3383 bfd_set_section_flags (stdoutput, sec, flags);
3384 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132 3385
f3ded42a
RS
3386 /* Set up the option header. */
3387 {
3388 Elf_Internal_Options opthdr;
3389 char *f;
3390
3391 opthdr.kind = ODK_REGINFO;
3392 opthdr.size = (sizeof (Elf_External_Options)
3393 + sizeof (Elf64_External_RegInfo));
3394 opthdr.section = 0;
3395 opthdr.info = 0;
3396 f = frag_more (sizeof (Elf_External_Options));
3397 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
3398 (Elf_External_Options *) f);
3399
3400 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
3401 }
3402 }
252b5132 3403
f3ded42a
RS
3404 if (ECOFF_DEBUGGING)
3405 {
3406 sec = subseg_new (".mdebug", (subsegT) 0);
3407 (void) bfd_set_section_flags (stdoutput, sec,
3408 SEC_HAS_CONTENTS | SEC_READONLY);
3409 (void) bfd_set_section_alignment (stdoutput, sec, 2);
252b5132 3410 }
f3ded42a
RS
3411 else if (mips_flag_pdr)
3412 {
3413 pdr_seg = subseg_new (".pdr", (subsegT) 0);
3414 (void) bfd_set_section_flags (stdoutput, pdr_seg,
3415 SEC_READONLY | SEC_RELOC
3416 | SEC_DEBUGGING);
3417 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
3418 }
3419
3420 subseg_set (seg, subseg);
3421 }
252b5132
RH
3422
3423 if (! ECOFF_DEBUGGING)
3424 md_obj_begin ();
71400594
RS
3425
3426 if (mips_fix_vr4120)
3427 init_vr4120_conflicts ();
252b5132
RH
3428}
3429
3430void
17a2f251 3431md_mips_end (void)
252b5132 3432{
02b1ab82 3433 mips_emit_delays ();
252b5132
RH
3434 if (! ECOFF_DEBUGGING)
3435 md_obj_end ();
3436}
3437
3438void
17a2f251 3439md_assemble (char *str)
252b5132
RH
3440{
3441 struct mips_cl_insn insn;
f6688943
TS
3442 bfd_reloc_code_real_type unused_reloc[3]
3443 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
3444
3445 imm_expr.X_op = O_absent;
5f74bc13 3446 imm2_expr.X_op = O_absent;
252b5132 3447 offset_expr.X_op = O_absent;
f6688943
TS
3448 offset_reloc[0] = BFD_RELOC_UNUSED;
3449 offset_reloc[1] = BFD_RELOC_UNUSED;
3450 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132 3451
e1b47bd5
RS
3452 mips_mark_labels ();
3453 mips_assembling_insn = TRUE;
3454
252b5132
RH
3455 if (mips_opts.mips16)
3456 mips16_ip (str, &insn);
3457 else
3458 {
3459 mips_ip (str, &insn);
beae10d5
KH
3460 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
3461 str, insn.insn_opcode));
252b5132
RH
3462 }
3463
3464 if (insn_error)
e1b47bd5
RS
3465 as_bad ("%s `%s'", insn_error, str);
3466 else if (insn.insn_mo->pinfo == INSN_MACRO)
252b5132 3467 {
584892a6 3468 macro_start ();
252b5132
RH
3469 if (mips_opts.mips16)
3470 mips16_macro (&insn);
3471 else
833794fc 3472 macro (&insn, str);
584892a6 3473 macro_end ();
252b5132
RH
3474 }
3475 else
3476 {
77bd4346 3477 if (offset_expr.X_op != O_absent)
df58fc94 3478 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
252b5132 3479 else
df58fc94 3480 append_insn (&insn, NULL, unused_reloc, FALSE);
252b5132 3481 }
e1b47bd5
RS
3482
3483 mips_assembling_insn = FALSE;
252b5132
RH
3484}
3485
738e5348
RS
3486/* Convenience functions for abstracting away the differences between
3487 MIPS16 and non-MIPS16 relocations. */
3488
3489static inline bfd_boolean
3490mips16_reloc_p (bfd_reloc_code_real_type reloc)
3491{
3492 switch (reloc)
3493 {
3494 case BFD_RELOC_MIPS16_JMP:
3495 case BFD_RELOC_MIPS16_GPREL:
3496 case BFD_RELOC_MIPS16_GOT16:
3497 case BFD_RELOC_MIPS16_CALL16:
3498 case BFD_RELOC_MIPS16_HI16_S:
3499 case BFD_RELOC_MIPS16_HI16:
3500 case BFD_RELOC_MIPS16_LO16:
3501 return TRUE;
3502
3503 default:
3504 return FALSE;
3505 }
3506}
3507
df58fc94
RS
3508static inline bfd_boolean
3509micromips_reloc_p (bfd_reloc_code_real_type reloc)
3510{
3511 switch (reloc)
3512 {
3513 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
3514 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
3515 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
3516 case BFD_RELOC_MICROMIPS_GPREL16:
3517 case BFD_RELOC_MICROMIPS_JMP:
3518 case BFD_RELOC_MICROMIPS_HI16:
3519 case BFD_RELOC_MICROMIPS_HI16_S:
3520 case BFD_RELOC_MICROMIPS_LO16:
3521 case BFD_RELOC_MICROMIPS_LITERAL:
3522 case BFD_RELOC_MICROMIPS_GOT16:
3523 case BFD_RELOC_MICROMIPS_CALL16:
3524 case BFD_RELOC_MICROMIPS_GOT_HI16:
3525 case BFD_RELOC_MICROMIPS_GOT_LO16:
3526 case BFD_RELOC_MICROMIPS_CALL_HI16:
3527 case BFD_RELOC_MICROMIPS_CALL_LO16:
3528 case BFD_RELOC_MICROMIPS_SUB:
3529 case BFD_RELOC_MICROMIPS_GOT_PAGE:
3530 case BFD_RELOC_MICROMIPS_GOT_OFST:
3531 case BFD_RELOC_MICROMIPS_GOT_DISP:
3532 case BFD_RELOC_MICROMIPS_HIGHEST:
3533 case BFD_RELOC_MICROMIPS_HIGHER:
3534 case BFD_RELOC_MICROMIPS_SCN_DISP:
3535 case BFD_RELOC_MICROMIPS_JALR:
3536 return TRUE;
3537
3538 default:
3539 return FALSE;
3540 }
3541}
3542
2309ddf2
MR
3543static inline bfd_boolean
3544jmp_reloc_p (bfd_reloc_code_real_type reloc)
3545{
3546 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
3547}
3548
738e5348
RS
3549static inline bfd_boolean
3550got16_reloc_p (bfd_reloc_code_real_type reloc)
3551{
2309ddf2 3552 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
df58fc94 3553 || reloc == BFD_RELOC_MICROMIPS_GOT16);
738e5348
RS
3554}
3555
3556static inline bfd_boolean
3557hi16_reloc_p (bfd_reloc_code_real_type reloc)
3558{
2309ddf2 3559 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
df58fc94 3560 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
738e5348
RS
3561}
3562
3563static inline bfd_boolean
3564lo16_reloc_p (bfd_reloc_code_real_type reloc)
3565{
2309ddf2 3566 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
df58fc94
RS
3567 || reloc == BFD_RELOC_MICROMIPS_LO16);
3568}
3569
df58fc94
RS
3570static inline bfd_boolean
3571jalr_reloc_p (bfd_reloc_code_real_type reloc)
3572{
2309ddf2 3573 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
738e5348
RS
3574}
3575
f2ae14a1
RS
3576static inline bfd_boolean
3577gprel16_reloc_p (bfd_reloc_code_real_type reloc)
3578{
3579 return (reloc == BFD_RELOC_GPREL16 || reloc == BFD_RELOC_MIPS16_GPREL
3580 || reloc == BFD_RELOC_MICROMIPS_GPREL16);
3581}
3582
2de39019
CM
3583/* Return true if RELOC is a PC-relative relocation that does not have
3584 full address range. */
3585
3586static inline bfd_boolean
3587limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
3588{
3589 switch (reloc)
3590 {
3591 case BFD_RELOC_16_PCREL_S2:
3592 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
3593 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
3594 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
3595 return TRUE;
3596
b47468a6
CM
3597 case BFD_RELOC_32_PCREL:
3598 return HAVE_64BIT_ADDRESSES;
3599
2de39019
CM
3600 default:
3601 return FALSE;
3602 }
3603}
b47468a6 3604
5919d012 3605/* Return true if the given relocation might need a matching %lo().
0a44bf69
RS
3606 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
3607 need a matching %lo() when applied to local symbols. */
5919d012
RS
3608
3609static inline bfd_boolean
17a2f251 3610reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 3611{
3b91255e 3612 return (HAVE_IN_PLACE_ADDENDS
738e5348 3613 && (hi16_reloc_p (reloc)
0a44bf69
RS
3614 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
3615 all GOT16 relocations evaluate to "G". */
738e5348
RS
3616 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
3617}
3618
3619/* Return the type of %lo() reloc needed by RELOC, given that
3620 reloc_needs_lo_p. */
3621
3622static inline bfd_reloc_code_real_type
3623matching_lo_reloc (bfd_reloc_code_real_type reloc)
3624{
df58fc94
RS
3625 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
3626 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
3627 : BFD_RELOC_LO16));
5919d012
RS
3628}
3629
3630/* Return true if the given fixup is followed by a matching R_MIPS_LO16
3631 relocation. */
3632
3633static inline bfd_boolean
17a2f251 3634fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
3635{
3636 return (fixp->fx_next != NULL
738e5348 3637 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
5919d012
RS
3638 && fixp->fx_addsy == fixp->fx_next->fx_addsy
3639 && fixp->fx_offset == fixp->fx_next->fx_offset);
3640}
3641
462427c4
RS
3642/* Move all labels in LABELS to the current insertion point. TEXT_P
3643 says whether the labels refer to text or data. */
404a8071
RS
3644
3645static void
462427c4 3646mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
404a8071
RS
3647{
3648 struct insn_label_list *l;
3649 valueT val;
3650
462427c4 3651 for (l = labels; l != NULL; l = l->next)
404a8071 3652 {
9c2799c2 3653 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
404a8071
RS
3654 symbol_set_frag (l->label, frag_now);
3655 val = (valueT) frag_now_fix ();
df58fc94 3656 /* MIPS16/microMIPS text labels are stored as odd. */
462427c4 3657 if (text_p && HAVE_CODE_COMPRESSION)
404a8071
RS
3658 ++val;
3659 S_SET_VALUE (l->label, val);
3660 }
3661}
3662
462427c4
RS
3663/* Move all labels in insn_labels to the current insertion point
3664 and treat them as text labels. */
3665
3666static void
3667mips_move_text_labels (void)
3668{
3669 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
3670}
3671
5f0fe04b
TS
3672static bfd_boolean
3673s_is_linkonce (symbolS *sym, segT from_seg)
3674{
3675 bfd_boolean linkonce = FALSE;
3676 segT symseg = S_GET_SEGMENT (sym);
3677
3678 if (symseg != from_seg && !S_IS_LOCAL (sym))
3679 {
3680 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
3681 linkonce = TRUE;
5f0fe04b
TS
3682 /* The GNU toolchain uses an extension for ELF: a section
3683 beginning with the magic string .gnu.linkonce is a
3684 linkonce section. */
3685 if (strncmp (segment_name (symseg), ".gnu.linkonce",
3686 sizeof ".gnu.linkonce" - 1) == 0)
3687 linkonce = TRUE;
5f0fe04b
TS
3688 }
3689 return linkonce;
3690}
3691
e1b47bd5 3692/* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
df58fc94
RS
3693 linker to handle them specially, such as generating jalx instructions
3694 when needed. We also make them odd for the duration of the assembly,
3695 in order to generate the right sort of code. We will make them even
252b5132
RH
3696 in the adjust_symtab routine, while leaving them marked. This is
3697 convenient for the debugger and the disassembler. The linker knows
3698 to make them odd again. */
3699
3700static void
e1b47bd5 3701mips_compressed_mark_label (symbolS *label)
252b5132 3702{
df58fc94 3703 gas_assert (HAVE_CODE_COMPRESSION);
a8dbcb85 3704
f3ded42a
RS
3705 if (mips_opts.mips16)
3706 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
3707 else
3708 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
e1b47bd5
RS
3709 if ((S_GET_VALUE (label) & 1) == 0
3710 /* Don't adjust the address if the label is global or weak, or
3711 in a link-once section, since we'll be emitting symbol reloc
3712 references to it which will be patched up by the linker, and
3713 the final value of the symbol may or may not be MIPS16/microMIPS. */
3714 && !S_IS_WEAK (label)
3715 && !S_IS_EXTERNAL (label)
3716 && !s_is_linkonce (label, now_seg))
3717 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
3718}
3719
3720/* Mark preceding MIPS16 or microMIPS instruction labels. */
3721
3722static void
3723mips_compressed_mark_labels (void)
3724{
3725 struct insn_label_list *l;
3726
3727 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
3728 mips_compressed_mark_label (l->label);
252b5132
RH
3729}
3730
4d7206a2
RS
3731/* End the current frag. Make it a variant frag and record the
3732 relaxation info. */
3733
3734static void
3735relax_close_frag (void)
3736{
584892a6 3737 mips_macro_warning.first_frag = frag_now;
4d7206a2 3738 frag_var (rs_machine_dependent, 0, 0,
584892a6 3739 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
4d7206a2
RS
3740 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
3741
3742 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
3743 mips_relax.first_fixup = 0;
3744}
3745
3746/* Start a new relaxation sequence whose expansion depends on SYMBOL.
3747 See the comment above RELAX_ENCODE for more details. */
3748
3749static void
3750relax_start (symbolS *symbol)
3751{
9c2799c2 3752 gas_assert (mips_relax.sequence == 0);
4d7206a2
RS
3753 mips_relax.sequence = 1;
3754 mips_relax.symbol = symbol;
3755}
3756
3757/* Start generating the second version of a relaxable sequence.
3758 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
3759
3760static void
4d7206a2
RS
3761relax_switch (void)
3762{
9c2799c2 3763 gas_assert (mips_relax.sequence == 1);
4d7206a2
RS
3764 mips_relax.sequence = 2;
3765}
3766
3767/* End the current relaxable sequence. */
3768
3769static void
3770relax_end (void)
3771{
9c2799c2 3772 gas_assert (mips_relax.sequence == 2);
4d7206a2
RS
3773 relax_close_frag ();
3774 mips_relax.sequence = 0;
3775}
3776
11625dd8
RS
3777/* Return true if IP is a delayed branch or jump. */
3778
3779static inline bfd_boolean
3780delayed_branch_p (const struct mips_cl_insn *ip)
3781{
3782 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
3783 | INSN_COND_BRANCH_DELAY
3784 | INSN_COND_BRANCH_LIKELY)) != 0;
3785}
3786
3787/* Return true if IP is a compact branch or jump. */
3788
3789static inline bfd_boolean
3790compact_branch_p (const struct mips_cl_insn *ip)
3791{
26545944
RS
3792 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
3793 | INSN2_COND_BRANCH)) != 0;
11625dd8
RS
3794}
3795
3796/* Return true if IP is an unconditional branch or jump. */
3797
3798static inline bfd_boolean
3799uncond_branch_p (const struct mips_cl_insn *ip)
3800{
3801 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
26545944 3802 || (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0);
11625dd8
RS
3803}
3804
3805/* Return true if IP is a branch-likely instruction. */
3806
3807static inline bfd_boolean
3808branch_likely_p (const struct mips_cl_insn *ip)
3809{
3810 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
3811}
3812
14fe068b
RS
3813/* Return the type of nop that should be used to fill the delay slot
3814 of delayed branch IP. */
3815
3816static struct mips_cl_insn *
3817get_delay_slot_nop (const struct mips_cl_insn *ip)
3818{
3819 if (mips_opts.micromips
3820 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
3821 return &micromips_nop32_insn;
3822 return NOP_INSN;
3823}
3824
fc76e730
RS
3825/* Return a mask that has bit N set if OPCODE reads the register(s)
3826 in operand N. */
df58fc94
RS
3827
3828static unsigned int
fc76e730 3829insn_read_mask (const struct mips_opcode *opcode)
df58fc94 3830{
fc76e730
RS
3831 return (opcode->pinfo & INSN_READ_ALL) >> INSN_READ_SHIFT;
3832}
df58fc94 3833
fc76e730
RS
3834/* Return a mask that has bit N set if OPCODE writes to the register(s)
3835 in operand N. */
3836
3837static unsigned int
3838insn_write_mask (const struct mips_opcode *opcode)
3839{
3840 return (opcode->pinfo & INSN_WRITE_ALL) >> INSN_WRITE_SHIFT;
3841}
3842
3843/* Return a mask of the registers specified by operand OPERAND of INSN.
3844 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
3845 is set. */
3846
3847static unsigned int
3848operand_reg_mask (const struct mips_cl_insn *insn,
3849 const struct mips_operand *operand,
3850 unsigned int type_mask)
3851{
3852 unsigned int uval, vsel;
3853
3854 switch (operand->type)
df58fc94 3855 {
fc76e730
RS
3856 case OP_INT:
3857 case OP_MAPPED_INT:
3858 case OP_MSB:
3859 case OP_PCREL:
3860 case OP_PERF_REG:
3861 case OP_ADDIUSP_INT:
3862 case OP_ENTRY_EXIT_LIST:
3863 case OP_REPEAT_DEST_REG:
3864 case OP_REPEAT_PREV_REG:
3865 case OP_PC:
14daeee3
RS
3866 case OP_VU0_SUFFIX:
3867 case OP_VU0_MATCH_SUFFIX:
fc76e730
RS
3868 abort ();
3869
3870 case OP_REG:
3871 {
3872 const struct mips_reg_operand *reg_op;
3873
3874 reg_op = (const struct mips_reg_operand *) operand;
3875 if (!(type_mask & (1 << reg_op->reg_type)))
3876 return 0;
3877 uval = insn_extract_operand (insn, operand);
3878 return 1 << mips_decode_reg_operand (reg_op, uval);
3879 }
3880
3881 case OP_REG_PAIR:
3882 {
3883 const struct mips_reg_pair_operand *pair_op;
3884
3885 pair_op = (const struct mips_reg_pair_operand *) operand;
3886 if (!(type_mask & (1 << pair_op->reg_type)))
3887 return 0;
3888 uval = insn_extract_operand (insn, operand);
3889 return (1 << pair_op->reg1_map[uval]) | (1 << pair_op->reg2_map[uval]);
3890 }
3891
3892 case OP_CLO_CLZ_DEST:
3893 if (!(type_mask & (1 << OP_REG_GP)))
3894 return 0;
3895 uval = insn_extract_operand (insn, operand);
3896 return (1 << (uval & 31)) | (1 << (uval >> 5));
3897
3898 case OP_LWM_SWM_LIST:
3899 abort ();
3900
3901 case OP_SAVE_RESTORE_LIST:
3902 abort ();
3903
3904 case OP_MDMX_IMM_REG:
3905 if (!(type_mask & (1 << OP_REG_VEC)))
3906 return 0;
3907 uval = insn_extract_operand (insn, operand);
3908 vsel = uval >> 5;
3909 if ((vsel & 0x18) == 0x18)
3910 return 0;
3911 return 1 << (uval & 31);
df58fc94 3912 }
fc76e730
RS
3913 abort ();
3914}
3915
3916/* Return a mask of the registers specified by operands OPNO_MASK of INSN,
3917 where bit N of OPNO_MASK is set if operand N should be included.
3918 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
3919 is set. */
3920
3921static unsigned int
3922insn_reg_mask (const struct mips_cl_insn *insn,
3923 unsigned int type_mask, unsigned int opno_mask)
3924{
3925 unsigned int opno, reg_mask;
3926
3927 opno = 0;
3928 reg_mask = 0;
3929 while (opno_mask != 0)
3930 {
3931 if (opno_mask & 1)
3932 reg_mask |= operand_reg_mask (insn, insn_opno (insn, opno), type_mask);
3933 opno_mask >>= 1;
3934 opno += 1;
3935 }
3936 return reg_mask;
df58fc94
RS
3937}
3938
4c260379
RS
3939/* Return the mask of core registers that IP reads. */
3940
3941static unsigned int
3942gpr_read_mask (const struct mips_cl_insn *ip)
3943{
3944 unsigned long pinfo, pinfo2;
3945 unsigned int mask;
3946
fc76e730 3947 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_read_mask (ip->insn_mo));
4c260379
RS
3948 pinfo = ip->insn_mo->pinfo;
3949 pinfo2 = ip->insn_mo->pinfo2;
fc76e730 3950 if (pinfo & INSN_UDI)
4c260379 3951 {
fc76e730
RS
3952 /* UDI instructions have traditionally been assumed to read RS
3953 and RT. */
3954 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
3955 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4c260379 3956 }
fc76e730
RS
3957 if (pinfo & INSN_READ_GPR_24)
3958 mask |= 1 << 24;
3959 if (pinfo2 & INSN2_READ_GPR_16)
3960 mask |= 1 << 16;
3961 if (pinfo2 & INSN2_READ_SP)
3962 mask |= 1 << SP;
26545944 3963 if (pinfo2 & INSN2_READ_GPR_31)
fc76e730 3964 mask |= 1 << 31;
fe35f09f
RS
3965 /* Don't include register 0. */
3966 return mask & ~1;
4c260379
RS
3967}
3968
3969/* Return the mask of core registers that IP writes. */
3970
3971static unsigned int
3972gpr_write_mask (const struct mips_cl_insn *ip)
3973{
3974 unsigned long pinfo, pinfo2;
3975 unsigned int mask;
3976
fc76e730 3977 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_write_mask (ip->insn_mo));
4c260379
RS
3978 pinfo = ip->insn_mo->pinfo;
3979 pinfo2 = ip->insn_mo->pinfo2;
fc76e730
RS
3980 if (pinfo & INSN_WRITE_GPR_24)
3981 mask |= 1 << 24;
3982 if (pinfo & INSN_WRITE_GPR_31)
3983 mask |= 1 << 31;
3984 if (pinfo & INSN_UDI)
3985 /* UDI instructions have traditionally been assumed to write to RD. */
3986 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
3987 if (pinfo2 & INSN2_WRITE_SP)
3988 mask |= 1 << SP;
fe35f09f
RS
3989 /* Don't include register 0. */
3990 return mask & ~1;
4c260379
RS
3991}
3992
3993/* Return the mask of floating-point registers that IP reads. */
3994
3995static unsigned int
3996fpr_read_mask (const struct mips_cl_insn *ip)
3997{
fc76e730 3998 unsigned long pinfo;
4c260379
RS
3999 unsigned int mask;
4000
fc76e730
RS
4001 mask = insn_reg_mask (ip, (1 << OP_REG_FP) | (1 << OP_REG_VEC),
4002 insn_read_mask (ip->insn_mo));
4c260379 4003 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4004 /* Conservatively treat all operands to an FP_D instruction are doubles.
4005 (This is overly pessimistic for things like cvt.d.s.) */
4006 if (HAVE_32BIT_FPRS && (pinfo & FP_D))
4007 mask |= mask << 1;
4008 return mask;
4009}
4010
4011/* Return the mask of floating-point registers that IP writes. */
4012
4013static unsigned int
4014fpr_write_mask (const struct mips_cl_insn *ip)
4015{
fc76e730 4016 unsigned long pinfo;
4c260379
RS
4017 unsigned int mask;
4018
fc76e730
RS
4019 mask = insn_reg_mask (ip, (1 << OP_REG_FP) | (1 << OP_REG_VEC),
4020 insn_write_mask (ip->insn_mo));
4c260379 4021 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4022 /* Conservatively treat all operands to an FP_D instruction are doubles.
4023 (This is overly pessimistic for things like cvt.s.d.) */
4024 if (HAVE_32BIT_FPRS && (pinfo & FP_D))
4025 mask |= mask << 1;
4026 return mask;
4027}
4028
a1d78564
RS
4029/* Operand OPNUM of INSN is an odd-numbered floating-point register.
4030 Check whether that is allowed. */
4031
4032static bfd_boolean
4033mips_oddfpreg_ok (const struct mips_opcode *insn, int opnum)
4034{
4035 const char *s = insn->name;
4036
4037 if (insn->pinfo == INSN_MACRO)
4038 /* Let a macro pass, we'll catch it later when it is expanded. */
4039 return TRUE;
4040
4041 if (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa) || mips_opts.arch == CPU_R5900)
4042 {
4043 /* Allow odd registers for single-precision ops. */
4044 switch (insn->pinfo & (FP_S | FP_D))
4045 {
4046 case FP_S:
4047 case 0:
4048 return TRUE;
4049 case FP_D:
4050 return FALSE;
4051 default:
4052 break;
4053 }
4054
4055 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
4056 s = strchr (insn->name, '.');
4057 if (s != NULL && opnum == 2)
4058 s = strchr (s + 1, '.');
4059 return (s != NULL && (s[1] == 'w' || s[1] == 's'));
4060 }
4061
4062 /* Single-precision coprocessor loads and moves are OK too. */
4063 if ((insn->pinfo & FP_S)
4064 && (insn->pinfo & (INSN_COPROC_MEMORY_DELAY | INSN_STORE_MEMORY
4065 | INSN_LOAD_COPROC_DELAY | INSN_COPROC_MOVE_DELAY)))
4066 return TRUE;
4067
4068 return FALSE;
4069}
4070
a1d78564
RS
4071/* Report that user-supplied argument ARGNUM for INSN was VAL, but should
4072 have been in the range [MIN_VAL, MAX_VAL]. PRINT_HEX says whether
4073 this operand is normally printed in hex or decimal. */
4074
4075static void
4076report_bad_range (struct mips_cl_insn *insn, int argnum,
4077 offsetT val, int min_val, int max_val,
4078 bfd_boolean print_hex)
4079{
4080 if (print_hex && val >= 0)
4081 as_bad (_("Operand %d of `%s' must be in the range [0x%x, 0x%x],"
4082 " was 0x%lx."),
4083 argnum, insn->insn_mo->name, min_val, max_val, (unsigned long) val);
4084 else if (print_hex)
4085 as_bad (_("Operand %d of `%s' must be in the range [0x%x, 0x%x],"
4086 " was %ld."),
4087 argnum, insn->insn_mo->name, min_val, max_val, (unsigned long) val);
4088 else
4089 as_bad (_("Operand %d of `%s' must be in the range [%d, %d],"
4090 " was %ld."),
4091 argnum, insn->insn_mo->name, min_val, max_val, (unsigned long) val);
4092}
4093
4094/* Report an invalid combination of position and size operands for a bitfield
4095 operation. POS and SIZE are the values that were given. */
4096
4097static void
4098report_bad_field (offsetT pos, offsetT size)
4099{
4100 as_bad (_("Invalid field specification (position %ld, size %ld)"),
4101 (unsigned long) pos, (unsigned long) size);
4102}
4103
4104/* Information about an instruction argument that we're trying to match. */
4105struct mips_arg_info
4106{
4107 /* The instruction so far. */
4108 struct mips_cl_insn *insn;
4109
a92713e6
RS
4110 /* The first unconsumed operand token. */
4111 struct mips_operand_token *token;
4112
a1d78564
RS
4113 /* The 1-based operand number, in terms of insn->insn_mo->args. */
4114 int opnum;
4115
4116 /* The 1-based argument number, for error reporting. This does not
4117 count elided optional registers, etc.. */
4118 int argnum;
4119
4120 /* The last OP_REG operand seen, or ILLEGAL_REG if none. */
4121 unsigned int last_regno;
4122
4123 /* If the first operand was an OP_REG, this is the register that it
4124 specified, otherwise it is ILLEGAL_REG. */
4125 unsigned int dest_regno;
4126
4127 /* The value of the last OP_INT operand. Only used for OP_MSB,
4128 where it gives the lsb position. */
4129 unsigned int last_op_int;
4130
4131 /* If true, match routines should silently reject invalid arguments.
4132 If false, match routines can accept invalid arguments as long as
4133 they report an appropriate error. They still have the option of
4134 silently rejecting arguments, in which case a generic "Invalid operands"
4135 style of error will be used instead. */
4136 bfd_boolean soft_match;
4137
4138 /* If true, the OP_INT match routine should treat plain symbolic operands
4139 as if a relocation operator like %lo(...) had been used. This is only
4140 ever true if the operand can be relocated. */
4141 bfd_boolean allow_nonconst;
4142
4143 /* When true, the OP_INT match routine should allow unsigned N-bit
4144 arguments to be used where a signed N-bit operand is expected. */
4145 bfd_boolean lax_max;
4146
a1d78564
RS
4147 /* True if a reference to the current AT register was seen. */
4148 bfd_boolean seen_at;
4149};
4150
a92713e6
RS
4151/* Try to match an OT_CHAR token for character CH. Consume the token
4152 and return true on success, otherwise return false. */
a1d78564 4153
a92713e6
RS
4154static bfd_boolean
4155match_char (struct mips_arg_info *arg, char ch)
a1d78564 4156{
a92713e6
RS
4157 if (arg->token->type == OT_CHAR && arg->token->u.ch == ch)
4158 {
4159 ++arg->token;
4160 if (ch == ',')
4161 arg->argnum += 1;
4162 return TRUE;
4163 }
4164 return FALSE;
4165}
a1d78564 4166
a92713e6
RS
4167/* Try to get an expression from the next tokens in ARG. Consume the
4168 tokens and return true on success, storing the expression value in
4169 VALUE and relocation types in R. */
4170
4171static bfd_boolean
4172match_expression (struct mips_arg_info *arg, expressionS *value,
4173 bfd_reloc_code_real_type *r)
4174{
4175 if (arg->token->type == OT_INTEGER)
a1d78564 4176 {
a92713e6
RS
4177 *value = arg->token->u.integer.value;
4178 memcpy (r, arg->token->u.integer.relocs, 3 * sizeof (*r));
4179 ++arg->token;
4180 return TRUE;
4181 }
4182
4183 /* Error-reporting is more consistent if we treat registers as O_register
4184 rather than rejecting them outright. "$1", "($1)" and "(($1))" are
4185 then handled in the same way. */
4186 if (arg->token->type == OT_REG)
4187 {
4188 value->X_add_number = arg->token->u.regno;
4189 ++arg->token;
a1d78564 4190 }
a92713e6
RS
4191 else if (arg->token[0].type == OT_CHAR
4192 && arg->token[0].u.ch == '('
4193 && arg->token[1].type == OT_REG
4194 && arg->token[2].type == OT_CHAR
4195 && arg->token[2].u.ch == ')')
4196 {
4197 value->X_add_number = arg->token[1].u.regno;
4198 arg->token += 3;
4199 }
4200 else
4201 return FALSE;
4202
4203 value->X_op = O_register;
4204 r[0] = r[1] = r[2] = BFD_RELOC_UNUSED;
4205 return TRUE;
4206}
4207
4208/* Try to get a constant expression from the next tokens in ARG. Consume
4209 the tokens and return return true on success, storing the constant value
4210 in *VALUE. Use FALLBACK as the value if the match succeeded with an
4211 error. */
4212
4213static bfd_boolean
4214match_const_int (struct mips_arg_info *arg, offsetT *value, offsetT fallback)
4215{
4216 expressionS ex;
4217 bfd_reloc_code_real_type r[3];
a1d78564 4218
a92713e6
RS
4219 if (!match_expression (arg, &ex, r))
4220 return FALSE;
4221
4222 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_constant)
a1d78564
RS
4223 *value = ex.X_add_number;
4224 else
4225 {
a92713e6
RS
4226 if (arg->soft_match)
4227 return FALSE;
a1d78564
RS
4228 as_bad (_("Operand %d of `%s' must be constant"),
4229 arg->argnum, arg->insn->insn_mo->name);
4230 *value = fallback;
4231 }
a92713e6 4232 return TRUE;
a1d78564
RS
4233}
4234
4235/* Return the RTYPE_* flags for a register operand of type TYPE that
4236 appears in instruction OPCODE. */
4237
4238static unsigned int
4239convert_reg_type (const struct mips_opcode *opcode,
4240 enum mips_reg_operand_type type)
4241{
4242 switch (type)
4243 {
4244 case OP_REG_GP:
4245 return RTYPE_NUM | RTYPE_GP;
4246
4247 case OP_REG_FP:
4248 /* Allow vector register names for MDMX if the instruction is a 64-bit
4249 FPR load, store or move (including moves to and from GPRs). */
4250 if ((mips_opts.ase & ASE_MDMX)
4251 && (opcode->pinfo & FP_D)
4252 && (opcode->pinfo & (INSN_COPROC_MOVE_DELAY
4253 | INSN_COPROC_MEMORY_DELAY
4254 | INSN_LOAD_COPROC_DELAY
4255 | INSN_LOAD_MEMORY_DELAY
4256 | INSN_STORE_MEMORY)))
4257 return RTYPE_FPU | RTYPE_VEC;
4258 return RTYPE_FPU;
4259
4260 case OP_REG_CCC:
4261 if (opcode->pinfo & (FP_D | FP_S))
4262 return RTYPE_CCC | RTYPE_FCC;
4263 return RTYPE_CCC;
4264
4265 case OP_REG_VEC:
4266 if (opcode->membership & INSN_5400)
4267 return RTYPE_FPU;
4268 return RTYPE_FPU | RTYPE_VEC;
4269
4270 case OP_REG_ACC:
4271 return RTYPE_ACC;
4272
4273 case OP_REG_COPRO:
4274 if (opcode->name[strlen (opcode->name) - 1] == '0')
4275 return RTYPE_NUM | RTYPE_CP0;
4276 return RTYPE_NUM;
4277
4278 case OP_REG_HW:
4279 return RTYPE_NUM;
14daeee3
RS
4280
4281 case OP_REG_VI:
4282 return RTYPE_NUM | RTYPE_VI;
4283
4284 case OP_REG_VF:
4285 return RTYPE_NUM | RTYPE_VF;
4286
4287 case OP_REG_R5900_I:
4288 return RTYPE_R5900_I;
4289
4290 case OP_REG_R5900_Q:
4291 return RTYPE_R5900_Q;
4292
4293 case OP_REG_R5900_R:
4294 return RTYPE_R5900_R;
4295
4296 case OP_REG_R5900_ACC:
4297 return RTYPE_R5900_ACC;
a1d78564
RS
4298 }
4299 abort ();
4300}
4301
4302/* ARG is register REGNO, of type TYPE. Warn about any dubious registers. */
4303
4304static void
4305check_regno (struct mips_arg_info *arg,
4306 enum mips_reg_operand_type type, unsigned int regno)
4307{
4308 if (AT && type == OP_REG_GP && regno == AT)
4309 arg->seen_at = TRUE;
4310
4311 if (type == OP_REG_FP
4312 && (regno & 1) != 0
4313 && HAVE_32BIT_FPRS
4314 && !mips_oddfpreg_ok (arg->insn->insn_mo, arg->opnum))
4315 as_warn (_("Float register should be even, was %d"), regno);
4316
4317 if (type == OP_REG_CCC)
4318 {
4319 const char *name;
4320 size_t length;
4321
4322 name = arg->insn->insn_mo->name;
4323 length = strlen (name);
4324 if ((regno & 1) != 0
4325 && ((length >= 3 && strcmp (name + length - 3, ".ps") == 0)
4326 || (length >= 5 && strncmp (name + length - 5, "any2", 4) == 0)))
4327 as_warn (_("Condition code register should be even for %s, was %d"),
4328 name, regno);
4329
4330 if ((regno & 3) != 0
4331 && (length >= 5 && strncmp (name + length - 5, "any4", 4) == 0))
4332 as_warn (_("Condition code register should be 0 or 4 for %s, was %d"),
4333 name, regno);
4334 }
4335}
4336
a92713e6
RS
4337/* ARG is a register with symbol value SYMVAL. Try to interpret it as
4338 a register of type TYPE. Return true on success, storing the register
4339 number in *REGNO and warning about any dubious uses. */
4340
4341static bfd_boolean
4342match_regno (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4343 unsigned int symval, unsigned int *regno)
4344{
4345 if (type == OP_REG_VEC)
4346 symval = mips_prefer_vec_regno (symval);
4347 if (!(symval & convert_reg_type (arg->insn->insn_mo, type)))
4348 return FALSE;
4349
4350 *regno = symval & RNUM_MASK;
4351 check_regno (arg, type, *regno);
4352 return TRUE;
4353}
4354
4355/* Try to interpret the next token in ARG as a register of type TYPE.
4356 Consume the token and return true on success, storing the register
4357 number in *REGNO. Return false on failure. */
4358
4359static bfd_boolean
4360match_reg (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4361 unsigned int *regno)
4362{
4363 if (arg->token->type == OT_REG
4364 && match_regno (arg, type, arg->token->u.regno, regno))
4365 {
4366 ++arg->token;
4367 return TRUE;
4368 }
4369 return FALSE;
4370}
4371
4372/* Try to interpret the next token in ARG as a range of registers of type TYPE.
4373 Consume the token and return true on success, storing the register numbers
4374 in *REGNO1 and *REGNO2. Return false on failure. */
4375
4376static bfd_boolean
4377match_reg_range (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4378 unsigned int *regno1, unsigned int *regno2)
4379{
4380 if (match_reg (arg, type, regno1))
4381 {
4382 *regno2 = *regno1;
4383 return TRUE;
4384 }
4385 if (arg->token->type == OT_REG_RANGE
4386 && match_regno (arg, type, arg->token->u.reg_range.regno1, regno1)
4387 && match_regno (arg, type, arg->token->u.reg_range.regno2, regno2)
4388 && *regno1 <= *regno2)
4389 {
4390 ++arg->token;
4391 return TRUE;
4392 }
4393 return FALSE;
4394}
4395
a1d78564
RS
4396/* OP_INT matcher. */
4397
a92713e6 4398static bfd_boolean
a1d78564 4399match_int_operand (struct mips_arg_info *arg,
a92713e6 4400 const struct mips_operand *operand_base)
a1d78564
RS
4401{
4402 const struct mips_int_operand *operand;
3ccad066 4403 unsigned int uval;
a1d78564
RS
4404 int min_val, max_val, factor;
4405 offsetT sval;
4406 bfd_boolean print_hex;
4407
4408 operand = (const struct mips_int_operand *) operand_base;
4409 factor = 1 << operand->shift;
3ccad066
RS
4410 min_val = mips_int_operand_min (operand);
4411 max_val = mips_int_operand_max (operand);
a1d78564 4412 if (arg->lax_max)
3ccad066 4413 max_val = ((1 << operand_base->size) - 1) << operand->shift;
a1d78564 4414
a92713e6
RS
4415 if (arg->token->type == OT_CHAR && arg->token->u.ch == '(')
4416 /* Assume we have an elided offset. The later match will fail
4417 if this turns out to be wrong. */
4418 sval = 0;
4419 else if (operand_base->lsb == 0
4420 && operand_base->size == 16
4421 && operand->shift == 0
4422 && operand->bias == 0
4423 && (operand->max_val == 32767 || operand->max_val == 65535))
a1d78564
RS
4424 {
4425 /* The operand can be relocated. */
a92713e6
RS
4426 if (!match_expression (arg, &offset_expr, offset_reloc))
4427 return FALSE;
4428
4429 if (offset_reloc[0] != BFD_RELOC_UNUSED)
a1d78564
RS
4430 /* Relocation operators were used. Accept the arguent and
4431 leave the relocation value in offset_expr and offset_relocs
4432 for the caller to process. */
a92713e6
RS
4433 return TRUE;
4434
4435 if (offset_expr.X_op != O_constant)
a1d78564 4436 {
a92713e6
RS
4437 /* If non-constant operands are allowed then leave them for
4438 the caller to process, otherwise fail the match. */
4439 if (!arg->allow_nonconst)
4440 return FALSE;
4441 offset_reloc[0] = BFD_RELOC_LO16;
4442 return TRUE;
a1d78564 4443 }
a92713e6 4444
a1d78564
RS
4445 /* Clear the global state; we're going to install the operand
4446 ourselves. */
a92713e6 4447 sval = offset_expr.X_add_number;
a1d78564
RS
4448 offset_expr.X_op = O_absent;
4449 }
4450 else
4451 {
a92713e6
RS
4452 if (!match_const_int (arg, &sval, min_val))
4453 return FALSE;
a1d78564
RS
4454 }
4455
4456 arg->last_op_int = sval;
4457
4458 /* Check the range. If there's a problem, record the lowest acceptable
4459 value in arg->last_op_int in order to prevent an unhelpful error
4460 from OP_MSB too.
4461
4462 Bit counts have traditionally been printed in hex by the disassembler
4463 but printed as decimal in error messages. Only resort to hex if
4464 the operand is bigger than 6 bits. */
4465 print_hex = operand->print_hex && operand_base->size > 6;
4466 if (sval < min_val || sval > max_val)
4467 {
4468 if (arg->soft_match)
a92713e6 4469 return FALSE;
a1d78564
RS
4470 report_bad_range (arg->insn, arg->argnum, sval, min_val, max_val,
4471 print_hex);
4472 arg->last_op_int = min_val;
4473 }
4474 else if (sval % factor)
4475 {
4476 if (arg->soft_match)
a92713e6 4477 return FALSE;
a1d78564
RS
4478 as_bad (print_hex && sval >= 0
4479 ? _("Operand %d of `%s' must be a factor of %d, was 0x%lx.")
4480 : _("Operand %d of `%s' must be a factor of %d, was %ld."),
4481 arg->argnum, arg->insn->insn_mo->name, factor,
4482 (unsigned long) sval);
4483 arg->last_op_int = min_val;
4484 }
4485
4486 uval = (unsigned int) sval >> operand->shift;
4487 uval -= operand->bias;
4488
4489 /* Handle -mfix-cn63xxp1. */
4490 if (arg->opnum == 1
4491 && mips_fix_cn63xxp1
4492 && !mips_opts.micromips
4493 && strcmp ("pref", arg->insn->insn_mo->name) == 0)
4494 switch (uval)
4495 {
4496 case 5:
4497 case 25:
4498 case 26:
4499 case 27:
4500 case 28:
4501 case 29:
4502 case 30:
4503 case 31:
4504 /* These are ok. */
4505 break;
4506
4507 default:
4508 /* The rest must be changed to 28. */
4509 uval = 28;
4510 break;
4511 }
4512
4513 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 4514 return TRUE;
a1d78564
RS
4515}
4516
4517/* OP_MAPPED_INT matcher. */
4518
a92713e6 4519static bfd_boolean
a1d78564 4520match_mapped_int_operand (struct mips_arg_info *arg,
a92713e6 4521 const struct mips_operand *operand_base)
a1d78564
RS
4522{
4523 const struct mips_mapped_int_operand *operand;
4524 unsigned int uval, num_vals;
4525 offsetT sval;
4526
4527 operand = (const struct mips_mapped_int_operand *) operand_base;
a92713e6
RS
4528 if (!match_const_int (arg, &sval, operand->int_map[0]))
4529 return FALSE;
a1d78564
RS
4530
4531 num_vals = 1 << operand_base->size;
4532 for (uval = 0; uval < num_vals; uval++)
4533 if (operand->int_map[uval] == sval)
4534 break;
4535 if (uval == num_vals)
a92713e6 4536 return FALSE;
a1d78564
RS
4537
4538 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 4539 return TRUE;
a1d78564
RS
4540}
4541
4542/* OP_MSB matcher. */
4543
a92713e6 4544static bfd_boolean
a1d78564 4545match_msb_operand (struct mips_arg_info *arg,
a92713e6 4546 const struct mips_operand *operand_base)
a1d78564
RS
4547{
4548 const struct mips_msb_operand *operand;
4549 int min_val, max_val, max_high;
4550 offsetT size, sval, high;
4551
4552 operand = (const struct mips_msb_operand *) operand_base;
4553 min_val = operand->bias;
4554 max_val = min_val + (1 << operand_base->size) - 1;
4555 max_high = operand->opsize;
4556
a92713e6
RS
4557 if (!match_const_int (arg, &size, 1))
4558 return FALSE;
a1d78564
RS
4559
4560 high = size + arg->last_op_int;
4561 sval = operand->add_lsb ? high : size;
4562
4563 if (size < 0 || high > max_high || sval < min_val || sval > max_val)
4564 {
4565 if (arg->soft_match)
a92713e6 4566 return FALSE;
a1d78564
RS
4567 report_bad_field (arg->last_op_int, size);
4568 sval = min_val;
4569 }
4570 insn_insert_operand (arg->insn, operand_base, sval - min_val);
a92713e6 4571 return TRUE;
a1d78564
RS
4572}
4573
4574/* OP_REG matcher. */
4575
a92713e6 4576static bfd_boolean
a1d78564 4577match_reg_operand (struct mips_arg_info *arg,
a92713e6 4578 const struct mips_operand *operand_base)
a1d78564
RS
4579{
4580 const struct mips_reg_operand *operand;
a92713e6 4581 unsigned int regno, uval, num_vals;
a1d78564
RS
4582
4583 operand = (const struct mips_reg_operand *) operand_base;
a92713e6
RS
4584 if (!match_reg (arg, operand->reg_type, &regno))
4585 return FALSE;
a1d78564
RS
4586
4587 if (operand->reg_map)
4588 {
4589 num_vals = 1 << operand->root.size;
4590 for (uval = 0; uval < num_vals; uval++)
4591 if (operand->reg_map[uval] == regno)
4592 break;
4593 if (num_vals == uval)
a92713e6 4594 return FALSE;
a1d78564
RS
4595 }
4596 else
4597 uval = regno;
4598
a1d78564
RS
4599 arg->last_regno = regno;
4600 if (arg->opnum == 1)
4601 arg->dest_regno = regno;
4602 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 4603 return TRUE;
a1d78564
RS
4604}
4605
4606/* OP_REG_PAIR matcher. */
4607
a92713e6 4608static bfd_boolean
a1d78564 4609match_reg_pair_operand (struct mips_arg_info *arg,
a92713e6 4610 const struct mips_operand *operand_base)
a1d78564
RS
4611{
4612 const struct mips_reg_pair_operand *operand;
a92713e6 4613 unsigned int regno1, regno2, uval, num_vals;
a1d78564
RS
4614
4615 operand = (const struct mips_reg_pair_operand *) operand_base;
a92713e6
RS
4616 if (!match_reg (arg, operand->reg_type, &regno1)
4617 || !match_char (arg, ',')
4618 || !match_reg (arg, operand->reg_type, &regno2))
4619 return FALSE;
a1d78564
RS
4620
4621 num_vals = 1 << operand_base->size;
4622 for (uval = 0; uval < num_vals; uval++)
4623 if (operand->reg1_map[uval] == regno1 && operand->reg2_map[uval] == regno2)
4624 break;
4625 if (uval == num_vals)
a92713e6 4626 return FALSE;
a1d78564 4627
a1d78564 4628 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 4629 return TRUE;
a1d78564
RS
4630}
4631
4632/* OP_PCREL matcher. The caller chooses the relocation type. */
4633
a92713e6
RS
4634static bfd_boolean
4635match_pcrel_operand (struct mips_arg_info *arg)
a1d78564 4636{
a92713e6
RS
4637 bfd_reloc_code_real_type r[3];
4638
4639 return match_expression (arg, &offset_expr, r) && r[0] == BFD_RELOC_UNUSED;
a1d78564
RS
4640}
4641
4642/* OP_PERF_REG matcher. */
4643
a92713e6 4644static bfd_boolean
a1d78564 4645match_perf_reg_operand (struct mips_arg_info *arg,
a92713e6 4646 const struct mips_operand *operand)
a1d78564
RS
4647{
4648 offsetT sval;
4649
a92713e6
RS
4650 if (!match_const_int (arg, &sval, 0))
4651 return FALSE;
a1d78564
RS
4652
4653 if (sval != 0
4654 && (sval != 1
4655 || (mips_opts.arch == CPU_R5900
4656 && (strcmp (arg->insn->insn_mo->name, "mfps") == 0
4657 || strcmp (arg->insn->insn_mo->name, "mtps") == 0))))
4658 {
4659 if (arg->soft_match)
a92713e6 4660 return FALSE;
a1d78564
RS
4661 as_bad (_("Invalid performance register (%ld)"), (unsigned long) sval);
4662 }
4663
4664 insn_insert_operand (arg->insn, operand, sval);
a92713e6 4665 return TRUE;
a1d78564
RS
4666}
4667
4668/* OP_ADDIUSP matcher. */
4669
a92713e6 4670static bfd_boolean
a1d78564 4671match_addiusp_operand (struct mips_arg_info *arg,
a92713e6 4672 const struct mips_operand *operand)
a1d78564
RS
4673{
4674 offsetT sval;
4675 unsigned int uval;
4676
a92713e6
RS
4677 if (!match_const_int (arg, &sval, -256))
4678 return FALSE;
a1d78564
RS
4679
4680 if (sval % 4)
a92713e6 4681 return FALSE;
a1d78564
RS
4682
4683 sval /= 4;
4684 if (!(sval >= -258 && sval <= 257) || (sval >= -2 && sval <= 1))
a92713e6 4685 return FALSE;
a1d78564
RS
4686
4687 uval = (unsigned int) sval;
4688 uval = ((uval >> 1) & ~0xff) | (uval & 0xff);
4689 insn_insert_operand (arg->insn, operand, uval);
a92713e6 4690 return TRUE;
a1d78564
RS
4691}
4692
4693/* OP_CLO_CLZ_DEST matcher. */
4694
a92713e6 4695static bfd_boolean
a1d78564 4696match_clo_clz_dest_operand (struct mips_arg_info *arg,
a92713e6 4697 const struct mips_operand *operand)
a1d78564
RS
4698{
4699 unsigned int regno;
4700
a92713e6
RS
4701 if (!match_reg (arg, OP_REG_GP, &regno))
4702 return FALSE;
a1d78564 4703
a1d78564 4704 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
a92713e6 4705 return TRUE;
a1d78564
RS
4706}
4707
4708/* OP_LWM_SWM_LIST matcher. */
4709
a92713e6 4710static bfd_boolean
a1d78564 4711match_lwm_swm_list_operand (struct mips_arg_info *arg,
a92713e6 4712 const struct mips_operand *operand)
a1d78564 4713{
a92713e6
RS
4714 unsigned int reglist, sregs, ra, regno1, regno2;
4715 struct mips_arg_info reset;
a1d78564 4716
a92713e6
RS
4717 reglist = 0;
4718 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
4719 return FALSE;
4720 do
4721 {
4722 if (regno2 == FP && regno1 >= S0 && regno1 <= S7)
4723 {
4724 reglist |= 1 << FP;
4725 regno2 = S7;
4726 }
4727 reglist |= ((1U << regno2 << 1) - 1) & -(1U << regno1);
4728 reset = *arg;
4729 }
4730 while (match_char (arg, ',')
4731 && match_reg_range (arg, OP_REG_GP, &regno1, &regno2));
4732 *arg = reset;
a1d78564
RS
4733
4734 if (operand->size == 2)
4735 {
4736 /* The list must include both ra and s0-sN, for 0 <= N <= 3. E.g.:
4737
4738 s0, ra
4739 s0, s1, ra, s2, s3
4740 s0-s2, ra
4741
4742 and any permutations of these. */
4743 if ((reglist & 0xfff1ffff) != 0x80010000)
a92713e6 4744 return FALSE;
a1d78564
RS
4745
4746 sregs = (reglist >> 17) & 7;
4747 ra = 0;
4748 }
4749 else
4750 {
4751 /* The list must include at least one of ra and s0-sN,
4752 for 0 <= N <= 8. (Note that there is a gap between s7 and s8,
4753 which are $23 and $30 respectively.) E.g.:
4754
4755 ra
4756 s0
4757 ra, s0, s1, s2
4758 s0-s8
4759 s0-s5, ra
4760
4761 and any permutations of these. */
4762 if ((reglist & 0x3f00ffff) != 0)
a92713e6 4763 return FALSE;
a1d78564
RS
4764
4765 ra = (reglist >> 27) & 0x10;
4766 sregs = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
4767 }
4768 sregs += 1;
4769 if ((sregs & -sregs) != sregs)
a92713e6 4770 return FALSE;
a1d78564
RS
4771
4772 insn_insert_operand (arg->insn, operand, (ffs (sregs) - 1) | ra);
a92713e6 4773 return TRUE;
a1d78564
RS
4774}
4775
364215c8
RS
4776/* OP_ENTRY_EXIT_LIST matcher. */
4777
a92713e6 4778static unsigned int
364215c8 4779match_entry_exit_operand (struct mips_arg_info *arg,
a92713e6 4780 const struct mips_operand *operand)
364215c8
RS
4781{
4782 unsigned int mask;
4783 bfd_boolean is_exit;
4784
4785 /* The format is the same for both ENTRY and EXIT, but the constraints
4786 are different. */
4787 is_exit = strcmp (arg->insn->insn_mo->name, "exit") == 0;
4788 mask = (is_exit ? 7 << 3 : 0);
a92713e6 4789 do
364215c8
RS
4790 {
4791 unsigned int regno1, regno2;
4792 bfd_boolean is_freg;
4793
a92713e6 4794 if (match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
364215c8 4795 is_freg = FALSE;
a92713e6 4796 else if (match_reg_range (arg, OP_REG_FP, &regno1, &regno2))
364215c8
RS
4797 is_freg = TRUE;
4798 else
a92713e6 4799 return FALSE;
364215c8
RS
4800
4801 if (is_exit && is_freg && regno1 == 0 && regno2 < 2)
4802 {
4803 mask &= ~(7 << 3);
4804 mask |= (5 + regno2) << 3;
4805 }
4806 else if (!is_exit && regno1 == 4 && regno2 >= 4 && regno2 <= 7)
4807 mask |= (regno2 - 3) << 3;
4808 else if (regno1 == 16 && regno2 >= 16 && regno2 <= 17)
4809 mask |= (regno2 - 15) << 1;
4810 else if (regno1 == RA && regno2 == RA)
4811 mask |= 1;
4812 else
a92713e6 4813 return FALSE;
364215c8 4814 }
a92713e6
RS
4815 while (match_char (arg, ','));
4816
364215c8 4817 insn_insert_operand (arg->insn, operand, mask);
a92713e6 4818 return TRUE;
364215c8
RS
4819}
4820
4821/* OP_SAVE_RESTORE_LIST matcher. */
4822
a92713e6
RS
4823static bfd_boolean
4824match_save_restore_list_operand (struct mips_arg_info *arg)
364215c8
RS
4825{
4826 unsigned int opcode, args, statics, sregs;
4827 unsigned int num_frame_sizes, num_args, num_statics, num_sregs;
364215c8
RS
4828 offsetT frame_size;
4829 const char *error;
4830
4831 error = 0;
4832 opcode = arg->insn->insn_opcode;
4833 frame_size = 0;
4834 num_frame_sizes = 0;
4835 args = 0;
4836 statics = 0;
4837 sregs = 0;
a92713e6 4838 do
364215c8
RS
4839 {
4840 unsigned int regno1, regno2;
4841
a92713e6 4842 if (arg->token->type == OT_INTEGER)
364215c8
RS
4843 {
4844 /* Handle the frame size. */
a92713e6
RS
4845 if (!match_const_int (arg, &frame_size, 0))
4846 return FALSE;
364215c8 4847 num_frame_sizes += 1;
364215c8
RS
4848 }
4849 else
4850 {
a92713e6
RS
4851 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
4852 return FALSE;
364215c8
RS
4853
4854 while (regno1 <= regno2)
4855 {
4856 if (regno1 >= 4 && regno1 <= 7)
4857 {
4858 if (num_frame_sizes == 0)
4859 /* args $a0-$a3 */
4860 args |= 1 << (regno1 - 4);
4861 else
4862 /* statics $a0-$a3 */
4863 statics |= 1 << (regno1 - 4);
4864 }
4865 else if (regno1 >= 16 && regno1 <= 23)
4866 /* $s0-$s7 */
4867 sregs |= 1 << (regno1 - 16);
4868 else if (regno1 == 30)
4869 /* $s8 */
4870 sregs |= 1 << 8;
4871 else if (regno1 == 31)
4872 /* Add $ra to insn. */
4873 opcode |= 0x40;
4874 else
a92713e6 4875 return FALSE;
364215c8
RS
4876 regno1 += 1;
4877 if (regno1 == 24)
4878 regno1 = 30;
4879 }
4880 }
364215c8 4881 }
a92713e6 4882 while (match_char (arg, ','));
364215c8
RS
4883
4884 /* Encode args/statics combination. */
4885 if (args & statics)
a92713e6 4886 return FALSE;
364215c8
RS
4887 else if (args == 0xf)
4888 /* All $a0-$a3 are args. */
4889 opcode |= MIPS16_ALL_ARGS << 16;
4890 else if (statics == 0xf)
4891 /* All $a0-$a3 are statics. */
4892 opcode |= MIPS16_ALL_STATICS << 16;
4893 else
4894 {
4895 /* Count arg registers. */
4896 num_args = 0;
4897 while (args & 0x1)
4898 {
4899 args >>= 1;
4900 num_args += 1;
4901 }
4902 if (args != 0)
a92713e6 4903 return FALSE;
364215c8
RS
4904
4905 /* Count static registers. */
4906 num_statics = 0;
4907 while (statics & 0x8)
4908 {
4909 statics = (statics << 1) & 0xf;
4910 num_statics += 1;
4911 }
4912 if (statics != 0)
a92713e6 4913 return FALSE;
364215c8
RS
4914
4915 /* Encode args/statics. */
4916 opcode |= ((num_args << 2) | num_statics) << 16;
4917 }
4918
4919 /* Encode $s0/$s1. */
4920 if (sregs & (1 << 0)) /* $s0 */
4921 opcode |= 0x20;
4922 if (sregs & (1 << 1)) /* $s1 */
4923 opcode |= 0x10;
4924 sregs >>= 2;
4925
4926 /* Encode $s2-$s8. */
4927 num_sregs = 0;
4928 while (sregs & 1)
4929 {
4930 sregs >>= 1;
4931 num_sregs += 1;
4932 }
4933 if (sregs != 0)
a92713e6 4934 return FALSE;
364215c8
RS
4935 opcode |= num_sregs << 24;
4936
4937 /* Encode frame size. */
4938 if (num_frame_sizes == 0)
4939 error = _("Missing frame size");
4940 else if (num_frame_sizes > 1)
4941 error = _("Frame size specified twice");
4942 else if ((frame_size & 7) != 0 || frame_size < 0 || frame_size > 0xff * 8)
4943 error = _("Invalid frame size");
4944 else if (frame_size != 128 || (opcode >> 16) != 0)
4945 {
4946 frame_size /= 8;
4947 opcode |= (((frame_size & 0xf0) << 16)
4948 | (frame_size & 0x0f));
4949 }
4950
4951 if (error)
4952 {
4953 if (arg->soft_match)
a92713e6 4954 return FALSE;
4a06e5a2 4955 as_bad ("%s", error);
364215c8
RS
4956 }
4957
4958 /* Finally build the instruction. */
4959 if ((opcode >> 16) != 0 || frame_size == 0)
4960 opcode |= MIPS16_EXTEND;
4961 arg->insn->insn_opcode = opcode;
a92713e6 4962 return TRUE;
364215c8
RS
4963}
4964
a1d78564
RS
4965/* OP_MDMX_IMM_REG matcher. */
4966
a92713e6 4967static bfd_boolean
a1d78564 4968match_mdmx_imm_reg_operand (struct mips_arg_info *arg,
a92713e6 4969 const struct mips_operand *operand)
a1d78564 4970{
a92713e6 4971 unsigned int regno, uval;
a1d78564
RS
4972 bfd_boolean is_qh;
4973 const struct mips_opcode *opcode;
4974
4975 /* The mips_opcode records whether this is an octobyte or quadhalf
4976 instruction. Start out with that bit in place. */
4977 opcode = arg->insn->insn_mo;
4978 uval = mips_extract_operand (operand, opcode->match);
4979 is_qh = (uval != 0);
4980
a92713e6 4981 if (arg->token->type == OT_REG || arg->token->type == OT_REG_ELEMENT)
a1d78564
RS
4982 {
4983 if ((opcode->membership & INSN_5400)
4984 && strcmp (opcode->name, "rzu.ob") == 0)
4985 {
4986 if (arg->soft_match)
a92713e6 4987 return FALSE;
a1d78564
RS
4988 as_bad (_("Operand %d of `%s' must be an immediate"),
4989 arg->argnum, opcode->name);
4990 }
4991
4992 /* Check whether this is a vector register or a broadcast of
4993 a single element. */
a92713e6 4994 if (arg->token->type == OT_REG_ELEMENT)
a1d78564 4995 {
a92713e6
RS
4996 if (!match_regno (arg, OP_REG_VEC, arg->token->u.reg_element.regno,
4997 &regno))
4998 return FALSE;
4999 if (arg->token->u.reg_element.index > (is_qh ? 3 : 7))
a1d78564
RS
5000 {
5001 if (arg->soft_match)
a92713e6 5002 return FALSE;
a1d78564 5003 as_bad (_("Invalid element selector"));
a1d78564 5004 }
a1d78564 5005 else
a92713e6 5006 uval |= arg->token->u.reg_element.index << (is_qh ? 2 : 1) << 5;
a1d78564
RS
5007 }
5008 else
5009 {
5010 /* A full vector. */
5011 if ((opcode->membership & INSN_5400)
5012 && (strcmp (opcode->name, "sll.ob") == 0
5013 || strcmp (opcode->name, "srl.ob") == 0))
5014 {
5015 if (arg->soft_match)
a92713e6 5016 return FALSE;
a1d78564
RS
5017 as_bad (_("Operand %d of `%s' must be scalar"),
5018 arg->argnum, opcode->name);
5019 }
5020
a92713e6
RS
5021 if (!match_regno (arg, OP_REG_VEC, arg->token->u.regno, &regno))
5022 return FALSE;
a1d78564
RS
5023 if (is_qh)
5024 uval |= MDMX_FMTSEL_VEC_QH << 5;
5025 else
5026 uval |= MDMX_FMTSEL_VEC_OB << 5;
5027 }
a1d78564 5028 uval |= regno;
a92713e6 5029 ++arg->token;
a1d78564
RS
5030 }
5031 else
5032 {
5033 offsetT sval;
5034
a92713e6
RS
5035 if (!match_const_int (arg, &sval, 0))
5036 return FALSE;
a1d78564
RS
5037 if (sval < 0 || sval > 31)
5038 {
5039 if (arg->soft_match)
a92713e6 5040 return FALSE;
a1d78564
RS
5041 report_bad_range (arg->insn, arg->argnum, sval, 0, 31, FALSE);
5042 }
5043 uval |= (sval & 31);
5044 if (is_qh)
5045 uval |= MDMX_FMTSEL_IMM_QH << 5;
5046 else
5047 uval |= MDMX_FMTSEL_IMM_OB << 5;
5048 }
5049 insn_insert_operand (arg->insn, operand, uval);
a92713e6 5050 return TRUE;
a1d78564
RS
5051}
5052
5053/* OP_PC matcher. */
5054
a92713e6
RS
5055static bfd_boolean
5056match_pc_operand (struct mips_arg_info *arg)
a1d78564 5057{
a92713e6
RS
5058 if (arg->token->type == OT_REG && (arg->token->u.regno & RTYPE_PC))
5059 {
5060 ++arg->token;
5061 return TRUE;
5062 }
5063 return FALSE;
a1d78564
RS
5064}
5065
5066/* OP_REPEAT_DEST_REG and OP_REPEAT_PREV_REG matcher. OTHER_REGNO is the
5067 register that we need to match. */
5068
a92713e6
RS
5069static bfd_boolean
5070match_tied_reg_operand (struct mips_arg_info *arg, unsigned int other_regno)
a1d78564
RS
5071{
5072 unsigned int regno;
5073
a92713e6 5074 return match_reg (arg, OP_REG_GP, &regno) && regno == other_regno;
a1d78564
RS
5075}
5076
89565f1b
RS
5077/* Read a floating-point constant from S for LI.S or LI.D. LENGTH is
5078 the length of the value in bytes (4 for float, 8 for double) and
5079 USING_GPRS says whether the destination is a GPR rather than an FPR.
5080
5081 Return the constant in IMM and OFFSET as follows:
5082
5083 - If the constant should be loaded via memory, set IMM to O_absent and
5084 OFFSET to the memory address.
5085
5086 - Otherwise, if the constant should be loaded into two 32-bit registers,
5087 set IMM to the O_constant to load into the high register and OFFSET
5088 to the corresponding value for the low register.
5089
5090 - Otherwise, set IMM to the full O_constant and set OFFSET to O_absent.
5091
5092 These constants only appear as the last operand in an instruction,
5093 and every instruction that accepts them in any variant accepts them
5094 in all variants. This means we don't have to worry about backing out
5095 any changes if the instruction does not match. We just match
5096 unconditionally and report an error if the constant is invalid. */
5097
a92713e6
RS
5098static bfd_boolean
5099match_float_constant (struct mips_arg_info *arg, expressionS *imm,
5100 expressionS *offset, int length, bfd_boolean using_gprs)
89565f1b 5101{
a92713e6 5102 char *p;
89565f1b
RS
5103 segT seg, new_seg;
5104 subsegT subseg;
5105 const char *newname;
a92713e6 5106 unsigned char *data;
89565f1b
RS
5107
5108 /* Where the constant is placed is based on how the MIPS assembler
5109 does things:
5110
5111 length == 4 && using_gprs -- immediate value only
5112 length == 8 && using_gprs -- .rdata or immediate value
5113 length == 4 && !using_gprs -- .lit4 or immediate value
5114 length == 8 && !using_gprs -- .lit8 or immediate value
5115
5116 The .lit4 and .lit8 sections are only used if permitted by the
5117 -G argument. */
a92713e6
RS
5118 if (arg->token->type != OT_FLOAT)
5119 return FALSE;
5120
5121 gas_assert (arg->token->u.flt.length == length);
5122 data = arg->token->u.flt.data;
5123 ++arg->token;
89565f1b
RS
5124
5125 /* Handle 32-bit constants for which an immediate value is best. */
5126 if (length == 4
5127 && (using_gprs
5128 || g_switch_value < 4
5129 || (data[0] == 0 && data[1] == 0)
5130 || (data[2] == 0 && data[3] == 0)))
5131 {
5132 imm->X_op = O_constant;
5133 if (!target_big_endian)
5134 imm->X_add_number = bfd_getl32 (data);
5135 else
5136 imm->X_add_number = bfd_getb32 (data);
5137 offset->X_op = O_absent;
a92713e6 5138 return TRUE;
89565f1b
RS
5139 }
5140
5141 /* Handle 64-bit constants for which an immediate value is best. */
5142 if (length == 8
5143 && !mips_disable_float_construction
5144 /* Constants can only be constructed in GPRs and copied
5145 to FPRs if the GPRs are at least as wide as the FPRs.
5146 Force the constant into memory if we are using 64-bit FPRs
5147 but the GPRs are only 32 bits wide. */
5148 /* ??? No longer true with the addition of MTHC1, but this
5149 is legacy code... */
5150 && (using_gprs || !(HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
5151 && ((data[0] == 0 && data[1] == 0)
5152 || (data[2] == 0 && data[3] == 0))
5153 && ((data[4] == 0 && data[5] == 0)
5154 || (data[6] == 0 && data[7] == 0)))
5155 {
5156 /* The value is simple enough to load with a couple of instructions.
5157 If using 32-bit registers, set IMM to the high order 32 bits and
5158 OFFSET to the low order 32 bits. Otherwise, set IMM to the entire
5159 64 bit constant. */
5160 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
5161 {
5162 imm->X_op = O_constant;
5163 offset->X_op = O_constant;
5164 if (!target_big_endian)
5165 {
5166 imm->X_add_number = bfd_getl32 (data + 4);
5167 offset->X_add_number = bfd_getl32 (data);
5168 }
5169 else
5170 {
5171 imm->X_add_number = bfd_getb32 (data);
5172 offset->X_add_number = bfd_getb32 (data + 4);
5173 }
5174 if (offset->X_add_number == 0)
5175 offset->X_op = O_absent;
5176 }
5177 else
5178 {
5179 imm->X_op = O_constant;
5180 if (!target_big_endian)
5181 imm->X_add_number = bfd_getl64 (data);
5182 else
5183 imm->X_add_number = bfd_getb64 (data);
5184 offset->X_op = O_absent;
5185 }
a92713e6 5186 return TRUE;
89565f1b
RS
5187 }
5188
5189 /* Switch to the right section. */
5190 seg = now_seg;
5191 subseg = now_subseg;
5192 if (length == 4)
5193 {
5194 gas_assert (!using_gprs && g_switch_value >= 4);
5195 newname = ".lit4";
5196 }
5197 else
5198 {
5199 if (using_gprs || g_switch_value < 8)
5200 newname = RDATA_SECTION_NAME;
5201 else
5202 newname = ".lit8";
5203 }
5204
5205 new_seg = subseg_new (newname, (subsegT) 0);
5206 bfd_set_section_flags (stdoutput, new_seg,
5207 SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA);
5208 frag_align (length == 4 ? 2 : 3, 0, 0);
5209 if (strncmp (TARGET_OS, "elf", 3) != 0)
5210 record_alignment (new_seg, 4);
5211 else
5212 record_alignment (new_seg, length == 4 ? 2 : 3);
5213 if (seg == now_seg)
5214 as_bad (_("Can't use floating point insn in this section"));
5215
5216 /* Set the argument to the current address in the section. */
5217 imm->X_op = O_absent;
5218 offset->X_op = O_symbol;
5219 offset->X_add_symbol = symbol_temp_new_now ();
5220 offset->X_add_number = 0;
5221
5222 /* Put the floating point number into the section. */
5223 p = frag_more (length);
5224 memcpy (p, data, length);
5225
5226 /* Switch back to the original section. */
5227 subseg_set (seg, subseg);
a92713e6 5228 return TRUE;
89565f1b
RS
5229}
5230
14daeee3
RS
5231/* OP_VU0_SUFFIX and OP_VU0_MATCH_SUFFIX matcher; MATCH_P selects between
5232 them. */
5233
5234static bfd_boolean
5235match_vu0_suffix_operand (struct mips_arg_info *arg,
5236 const struct mips_operand *operand,
5237 bfd_boolean match_p)
5238{
5239 unsigned int uval;
5240
5241 /* The operand can be an XYZW mask or a single 2-bit channel index
5242 (with X being 0). */
5243 gas_assert (operand->size == 2 || operand->size == 4);
5244
5245 /* The suffix can be omitted when matching a previous 4-bit mask. */
5246 if (arg->token->type != OT_CHANNELS)
5247 return operand->size == 4 && match_p;
5248
5249 uval = arg->token->u.channels;
5250 if (operand->size == 2)
5251 {
5252 /* Check that a single bit is set and convert it into a 2-bit index. */
5253 if ((uval & -uval) != uval)
5254 return FALSE;
5255 uval = 4 - ffs (uval);
5256 }
5257
5258 if (match_p && insn_extract_operand (arg->insn, operand) != uval)
5259 return FALSE;
5260
5261 ++arg->token;
5262 if (!match_p)
5263 insn_insert_operand (arg->insn, operand, uval);
5264 return TRUE;
5265}
5266
a1d78564
RS
5267/* S is the text seen for ARG. Match it against OPERAND. Return the end
5268 of the argument text if the match is successful, otherwise return null. */
5269
a92713e6 5270static bfd_boolean
a1d78564 5271match_operand (struct mips_arg_info *arg,
a92713e6 5272 const struct mips_operand *operand)
a1d78564
RS
5273{
5274 switch (operand->type)
5275 {
5276 case OP_INT:
a92713e6 5277 return match_int_operand (arg, operand);
a1d78564
RS
5278
5279 case OP_MAPPED_INT:
a92713e6 5280 return match_mapped_int_operand (arg, operand);
a1d78564
RS
5281
5282 case OP_MSB:
a92713e6 5283 return match_msb_operand (arg, operand);
a1d78564
RS
5284
5285 case OP_REG:
a92713e6 5286 return match_reg_operand (arg, operand);
a1d78564
RS
5287
5288 case OP_REG_PAIR:
a92713e6 5289 return match_reg_pair_operand (arg, operand);
a1d78564
RS
5290
5291 case OP_PCREL:
a92713e6 5292 return match_pcrel_operand (arg);
a1d78564
RS
5293
5294 case OP_PERF_REG:
a92713e6 5295 return match_perf_reg_operand (arg, operand);
a1d78564
RS
5296
5297 case OP_ADDIUSP_INT:
a92713e6 5298 return match_addiusp_operand (arg, operand);
a1d78564
RS
5299
5300 case OP_CLO_CLZ_DEST:
a92713e6 5301 return match_clo_clz_dest_operand (arg, operand);
a1d78564
RS
5302
5303 case OP_LWM_SWM_LIST:
a92713e6 5304 return match_lwm_swm_list_operand (arg, operand);
a1d78564
RS
5305
5306 case OP_ENTRY_EXIT_LIST:
a92713e6 5307 return match_entry_exit_operand (arg, operand);
364215c8 5308
a1d78564 5309 case OP_SAVE_RESTORE_LIST:
a92713e6 5310 return match_save_restore_list_operand (arg);
a1d78564
RS
5311
5312 case OP_MDMX_IMM_REG:
a92713e6 5313 return match_mdmx_imm_reg_operand (arg, operand);
a1d78564
RS
5314
5315 case OP_REPEAT_DEST_REG:
a92713e6 5316 return match_tied_reg_operand (arg, arg->dest_regno);
a1d78564
RS
5317
5318 case OP_REPEAT_PREV_REG:
a92713e6 5319 return match_tied_reg_operand (arg, arg->last_regno);
a1d78564
RS
5320
5321 case OP_PC:
a92713e6 5322 return match_pc_operand (arg);
14daeee3
RS
5323
5324 case OP_VU0_SUFFIX:
5325 return match_vu0_suffix_operand (arg, operand, FALSE);
5326
5327 case OP_VU0_MATCH_SUFFIX:
5328 return match_vu0_suffix_operand (arg, operand, TRUE);
a1d78564
RS
5329 }
5330 abort ();
5331}
5332
5333/* ARG is the state after successfully matching an instruction.
5334 Issue any queued-up warnings. */
5335
5336static void
5337check_completed_insn (struct mips_arg_info *arg)
5338{
5339 if (arg->seen_at)
5340 {
5341 if (AT == ATREG)
5342 as_warn (_("Used $at without \".set noat\""));
5343 else
5344 as_warn (_("Used $%u with \".set at=$%u\""), AT, AT);
5345 }
5346}
a1d78564 5347
85fcb30f
RS
5348/* Return true if modifying general-purpose register REG needs a delay. */
5349
5350static bfd_boolean
5351reg_needs_delay (unsigned int reg)
5352{
5353 unsigned long prev_pinfo;
5354
5355 prev_pinfo = history[0].insn_mo->pinfo;
5356 if (!mips_opts.noreorder
5357 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY) && !gpr_interlocks)
5358 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY) && !cop_interlocks))
5359 && (gpr_write_mask (&history[0]) & (1 << reg)))
5360 return TRUE;
5361
5362 return FALSE;
5363}
5364
71400594
RS
5365/* Classify an instruction according to the FIX_VR4120_* enumeration.
5366 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
5367 by VR4120 errata. */
4d7206a2 5368
71400594
RS
5369static unsigned int
5370classify_vr4120_insn (const char *name)
252b5132 5371{
71400594
RS
5372 if (strncmp (name, "macc", 4) == 0)
5373 return FIX_VR4120_MACC;
5374 if (strncmp (name, "dmacc", 5) == 0)
5375 return FIX_VR4120_DMACC;
5376 if (strncmp (name, "mult", 4) == 0)
5377 return FIX_VR4120_MULT;
5378 if (strncmp (name, "dmult", 5) == 0)
5379 return FIX_VR4120_DMULT;
5380 if (strstr (name, "div"))
5381 return FIX_VR4120_DIV;
5382 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
5383 return FIX_VR4120_MTHILO;
5384 return NUM_FIX_VR4120_CLASSES;
5385}
252b5132 5386
ff239038
CM
5387#define INSN_ERET 0x42000018
5388#define INSN_DERET 0x4200001f
5389
71400594
RS
5390/* Return the number of instructions that must separate INSN1 and INSN2,
5391 where INSN1 is the earlier instruction. Return the worst-case value
5392 for any INSN2 if INSN2 is null. */
252b5132 5393
71400594
RS
5394static unsigned int
5395insns_between (const struct mips_cl_insn *insn1,
5396 const struct mips_cl_insn *insn2)
5397{
5398 unsigned long pinfo1, pinfo2;
4c260379 5399 unsigned int mask;
71400594 5400
85fcb30f
RS
5401 /* If INFO2 is null, pessimistically assume that all flags are set for
5402 the second instruction. */
71400594
RS
5403 pinfo1 = insn1->insn_mo->pinfo;
5404 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
252b5132 5405
71400594
RS
5406 /* For most targets, write-after-read dependencies on the HI and LO
5407 registers must be separated by at least two instructions. */
5408 if (!hilo_interlocks)
252b5132 5409 {
71400594
RS
5410 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
5411 return 2;
5412 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
5413 return 2;
5414 }
5415
5416 /* If we're working around r7000 errata, there must be two instructions
5417 between an mfhi or mflo and any instruction that uses the result. */
5418 if (mips_7000_hilo_fix
df58fc94 5419 && !mips_opts.micromips
71400594 5420 && MF_HILO_INSN (pinfo1)
85fcb30f 5421 && (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1))))
71400594
RS
5422 return 2;
5423
ff239038
CM
5424 /* If we're working around 24K errata, one instruction is required
5425 if an ERET or DERET is followed by a branch instruction. */
df58fc94 5426 if (mips_fix_24k && !mips_opts.micromips)
ff239038
CM
5427 {
5428 if (insn1->insn_opcode == INSN_ERET
5429 || insn1->insn_opcode == INSN_DERET)
5430 {
5431 if (insn2 == NULL
5432 || insn2->insn_opcode == INSN_ERET
5433 || insn2->insn_opcode == INSN_DERET
11625dd8 5434 || delayed_branch_p (insn2))
ff239038
CM
5435 return 1;
5436 }
5437 }
5438
71400594
RS
5439 /* If working around VR4120 errata, check for combinations that need
5440 a single intervening instruction. */
df58fc94 5441 if (mips_fix_vr4120 && !mips_opts.micromips)
71400594
RS
5442 {
5443 unsigned int class1, class2;
252b5132 5444
71400594
RS
5445 class1 = classify_vr4120_insn (insn1->insn_mo->name);
5446 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
252b5132 5447 {
71400594
RS
5448 if (insn2 == NULL)
5449 return 1;
5450 class2 = classify_vr4120_insn (insn2->insn_mo->name);
5451 if (vr4120_conflicts[class1] & (1 << class2))
5452 return 1;
252b5132 5453 }
71400594
RS
5454 }
5455
df58fc94 5456 if (!HAVE_CODE_COMPRESSION)
71400594
RS
5457 {
5458 /* Check for GPR or coprocessor load delays. All such delays
5459 are on the RT register. */
5460 /* Itbl support may require additional care here. */
5461 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
5462 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
252b5132 5463 {
85fcb30f 5464 if (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1)))
71400594
RS
5465 return 1;
5466 }
5467
5468 /* Check for generic coprocessor hazards.
5469
5470 This case is not handled very well. There is no special
5471 knowledge of CP0 handling, and the coprocessors other than
5472 the floating point unit are not distinguished at all. */
5473 /* Itbl support may require additional care here. FIXME!
5474 Need to modify this to include knowledge about
5475 user specified delays! */
5476 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
5477 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
5478 {
5479 /* Handle cases where INSN1 writes to a known general coprocessor
5480 register. There must be a one instruction delay before INSN2
5481 if INSN2 reads that register, otherwise no delay is needed. */
4c260379
RS
5482 mask = fpr_write_mask (insn1);
5483 if (mask != 0)
252b5132 5484 {
4c260379 5485 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
71400594 5486 return 1;
252b5132
RH
5487 }
5488 else
5489 {
71400594
RS
5490 /* Read-after-write dependencies on the control registers
5491 require a two-instruction gap. */
5492 if ((pinfo1 & INSN_WRITE_COND_CODE)
5493 && (pinfo2 & INSN_READ_COND_CODE))
5494 return 2;
5495
5496 /* We don't know exactly what INSN1 does. If INSN2 is
5497 also a coprocessor instruction, assume there must be
5498 a one instruction gap. */
5499 if (pinfo2 & INSN_COP)
5500 return 1;
252b5132
RH
5501 }
5502 }
6b76fefe 5503
71400594
RS
5504 /* Check for read-after-write dependencies on the coprocessor
5505 control registers in cases where INSN1 does not need a general
5506 coprocessor delay. This means that INSN1 is a floating point
5507 comparison instruction. */
5508 /* Itbl support may require additional care here. */
5509 else if (!cop_interlocks
5510 && (pinfo1 & INSN_WRITE_COND_CODE)
5511 && (pinfo2 & INSN_READ_COND_CODE))
5512 return 1;
5513 }
6b76fefe 5514
71400594
RS
5515 return 0;
5516}
6b76fefe 5517
7d8e00cf
RS
5518/* Return the number of nops that would be needed to work around the
5519 VR4130 mflo/mfhi errata if instruction INSN immediately followed
932d1a1b
RS
5520 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
5521 that are contained within the first IGNORE instructions of HIST. */
7d8e00cf
RS
5522
5523static int
932d1a1b 5524nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
7d8e00cf
RS
5525 const struct mips_cl_insn *insn)
5526{
4c260379
RS
5527 int i, j;
5528 unsigned int mask;
7d8e00cf
RS
5529
5530 /* Check if the instruction writes to HI or LO. MTHI and MTLO
5531 are not affected by the errata. */
5532 if (insn != 0
5533 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
5534 || strcmp (insn->insn_mo->name, "mtlo") == 0
5535 || strcmp (insn->insn_mo->name, "mthi") == 0))
5536 return 0;
5537
5538 /* Search for the first MFLO or MFHI. */
5539 for (i = 0; i < MAX_VR4130_NOPS; i++)
91d6fa6a 5540 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
7d8e00cf
RS
5541 {
5542 /* Extract the destination register. */
4c260379 5543 mask = gpr_write_mask (&hist[i]);
7d8e00cf
RS
5544
5545 /* No nops are needed if INSN reads that register. */
4c260379 5546 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
7d8e00cf
RS
5547 return 0;
5548
5549 /* ...or if any of the intervening instructions do. */
5550 for (j = 0; j < i; j++)
4c260379 5551 if (gpr_read_mask (&hist[j]) & mask)
7d8e00cf
RS
5552 return 0;
5553
932d1a1b
RS
5554 if (i >= ignore)
5555 return MAX_VR4130_NOPS - i;
7d8e00cf
RS
5556 }
5557 return 0;
5558}
5559
15be625d
CM
5560#define BASE_REG_EQ(INSN1, INSN2) \
5561 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
5562 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
5563
5564/* Return the minimum alignment for this store instruction. */
5565
5566static int
5567fix_24k_align_to (const struct mips_opcode *mo)
5568{
5569 if (strcmp (mo->name, "sh") == 0)
5570 return 2;
5571
5572 if (strcmp (mo->name, "swc1") == 0
5573 || strcmp (mo->name, "swc2") == 0
5574 || strcmp (mo->name, "sw") == 0
5575 || strcmp (mo->name, "sc") == 0
5576 || strcmp (mo->name, "s.s") == 0)
5577 return 4;
5578
5579 if (strcmp (mo->name, "sdc1") == 0
5580 || strcmp (mo->name, "sdc2") == 0
5581 || strcmp (mo->name, "s.d") == 0)
5582 return 8;
5583
5584 /* sb, swl, swr */
5585 return 1;
5586}
5587
5588struct fix_24k_store_info
5589 {
5590 /* Immediate offset, if any, for this store instruction. */
5591 short off;
5592 /* Alignment required by this store instruction. */
5593 int align_to;
5594 /* True for register offsets. */
5595 int register_offset;
5596 };
5597
5598/* Comparison function used by qsort. */
5599
5600static int
5601fix_24k_sort (const void *a, const void *b)
5602{
5603 const struct fix_24k_store_info *pos1 = a;
5604 const struct fix_24k_store_info *pos2 = b;
5605
5606 return (pos1->off - pos2->off);
5607}
5608
5609/* INSN is a store instruction. Try to record the store information
5610 in STINFO. Return false if the information isn't known. */
5611
5612static bfd_boolean
5613fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
ab9794cf 5614 const struct mips_cl_insn *insn)
15be625d
CM
5615{
5616 /* The instruction must have a known offset. */
5617 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
5618 return FALSE;
5619
5620 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
5621 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
5622 return TRUE;
5623}
5624
932d1a1b
RS
5625/* Return the number of nops that would be needed to work around the 24k
5626 "lost data on stores during refill" errata if instruction INSN
5627 immediately followed the 2 instructions described by HIST.
5628 Ignore hazards that are contained within the first IGNORE
5629 instructions of HIST.
5630
5631 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
5632 for the data cache refills and store data. The following describes
5633 the scenario where the store data could be lost.
5634
5635 * A data cache miss, due to either a load or a store, causing fill
5636 data to be supplied by the memory subsystem
5637 * The first three doublewords of fill data are returned and written
5638 into the cache
5639 * A sequence of four stores occurs in consecutive cycles around the
5640 final doubleword of the fill:
5641 * Store A
5642 * Store B
5643 * Store C
5644 * Zero, One or more instructions
5645 * Store D
5646
5647 The four stores A-D must be to different doublewords of the line that
5648 is being filled. The fourth instruction in the sequence above permits
5649 the fill of the final doubleword to be transferred from the FSB into
5650 the cache. In the sequence above, the stores may be either integer
5651 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
5652 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
5653 different doublewords on the line. If the floating point unit is
5654 running in 1:2 mode, it is not possible to create the sequence above
5655 using only floating point store instructions.
15be625d
CM
5656
5657 In this case, the cache line being filled is incorrectly marked
5658 invalid, thereby losing the data from any store to the line that
5659 occurs between the original miss and the completion of the five
5660 cycle sequence shown above.
5661
932d1a1b 5662 The workarounds are:
15be625d 5663
932d1a1b
RS
5664 * Run the data cache in write-through mode.
5665 * Insert a non-store instruction between
5666 Store A and Store B or Store B and Store C. */
15be625d
CM
5667
5668static int
932d1a1b 5669nops_for_24k (int ignore, const struct mips_cl_insn *hist,
15be625d
CM
5670 const struct mips_cl_insn *insn)
5671{
5672 struct fix_24k_store_info pos[3];
5673 int align, i, base_offset;
5674
932d1a1b
RS
5675 if (ignore >= 2)
5676 return 0;
5677
ab9794cf
RS
5678 /* If the previous instruction wasn't a store, there's nothing to
5679 worry about. */
15be625d
CM
5680 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
5681 return 0;
5682
ab9794cf
RS
5683 /* If the instructions after the previous one are unknown, we have
5684 to assume the worst. */
5685 if (!insn)
15be625d
CM
5686 return 1;
5687
ab9794cf
RS
5688 /* Check whether we are dealing with three consecutive stores. */
5689 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
5690 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
15be625d
CM
5691 return 0;
5692
5693 /* If we don't know the relationship between the store addresses,
5694 assume the worst. */
ab9794cf 5695 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
15be625d
CM
5696 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
5697 return 1;
5698
5699 if (!fix_24k_record_store_info (&pos[0], insn)
5700 || !fix_24k_record_store_info (&pos[1], &hist[0])
5701 || !fix_24k_record_store_info (&pos[2], &hist[1]))
5702 return 1;
5703
5704 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
5705
5706 /* Pick a value of ALIGN and X such that all offsets are adjusted by
5707 X bytes and such that the base register + X is known to be aligned
5708 to align bytes. */
5709
5710 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
5711 align = 8;
5712 else
5713 {
5714 align = pos[0].align_to;
5715 base_offset = pos[0].off;
5716 for (i = 1; i < 3; i++)
5717 if (align < pos[i].align_to)
5718 {
5719 align = pos[i].align_to;
5720 base_offset = pos[i].off;
5721 }
5722 for (i = 0; i < 3; i++)
5723 pos[i].off -= base_offset;
5724 }
5725
5726 pos[0].off &= ~align + 1;
5727 pos[1].off &= ~align + 1;
5728 pos[2].off &= ~align + 1;
5729
5730 /* If any two stores write to the same chunk, they also write to the
5731 same doubleword. The offsets are still sorted at this point. */
5732 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
5733 return 0;
5734
5735 /* A range of at least 9 bytes is needed for the stores to be in
5736 non-overlapping doublewords. */
5737 if (pos[2].off - pos[0].off <= 8)
5738 return 0;
5739
5740 if (pos[2].off - pos[1].off >= 24
5741 || pos[1].off - pos[0].off >= 24
5742 || pos[2].off - pos[0].off >= 32)
5743 return 0;
5744
5745 return 1;
5746}
5747
71400594 5748/* Return the number of nops that would be needed if instruction INSN
91d6fa6a 5749 immediately followed the MAX_NOPS instructions given by HIST,
932d1a1b
RS
5750 where HIST[0] is the most recent instruction. Ignore hazards
5751 between INSN and the first IGNORE instructions in HIST.
5752
5753 If INSN is null, return the worse-case number of nops for any
5754 instruction. */
bdaaa2e1 5755
71400594 5756static int
932d1a1b 5757nops_for_insn (int ignore, const struct mips_cl_insn *hist,
71400594
RS
5758 const struct mips_cl_insn *insn)
5759{
5760 int i, nops, tmp_nops;
bdaaa2e1 5761
71400594 5762 nops = 0;
932d1a1b 5763 for (i = ignore; i < MAX_DELAY_NOPS; i++)
65b02341 5764 {
91d6fa6a 5765 tmp_nops = insns_between (hist + i, insn) - i;
65b02341
RS
5766 if (tmp_nops > nops)
5767 nops = tmp_nops;
5768 }
7d8e00cf 5769
df58fc94 5770 if (mips_fix_vr4130 && !mips_opts.micromips)
7d8e00cf 5771 {
932d1a1b 5772 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
7d8e00cf
RS
5773 if (tmp_nops > nops)
5774 nops = tmp_nops;
5775 }
5776
df58fc94 5777 if (mips_fix_24k && !mips_opts.micromips)
15be625d 5778 {
932d1a1b 5779 tmp_nops = nops_for_24k (ignore, hist, insn);
15be625d
CM
5780 if (tmp_nops > nops)
5781 nops = tmp_nops;
5782 }
5783
71400594
RS
5784 return nops;
5785}
252b5132 5786
71400594 5787/* The variable arguments provide NUM_INSNS extra instructions that
91d6fa6a 5788 might be added to HIST. Return the largest number of nops that
932d1a1b
RS
5789 would be needed after the extended sequence, ignoring hazards
5790 in the first IGNORE instructions. */
252b5132 5791
71400594 5792static int
932d1a1b
RS
5793nops_for_sequence (int num_insns, int ignore,
5794 const struct mips_cl_insn *hist, ...)
71400594
RS
5795{
5796 va_list args;
5797 struct mips_cl_insn buffer[MAX_NOPS];
5798 struct mips_cl_insn *cursor;
5799 int nops;
5800
91d6fa6a 5801 va_start (args, hist);
71400594 5802 cursor = buffer + num_insns;
91d6fa6a 5803 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
71400594
RS
5804 while (cursor > buffer)
5805 *--cursor = *va_arg (args, const struct mips_cl_insn *);
5806
932d1a1b 5807 nops = nops_for_insn (ignore, buffer, NULL);
71400594
RS
5808 va_end (args);
5809 return nops;
5810}
252b5132 5811
71400594
RS
5812/* Like nops_for_insn, but if INSN is a branch, take into account the
5813 worst-case delay for the branch target. */
252b5132 5814
71400594 5815static int
932d1a1b 5816nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
71400594
RS
5817 const struct mips_cl_insn *insn)
5818{
5819 int nops, tmp_nops;
60b63b72 5820
932d1a1b 5821 nops = nops_for_insn (ignore, hist, insn);
11625dd8 5822 if (delayed_branch_p (insn))
71400594 5823 {
932d1a1b 5824 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
14fe068b 5825 hist, insn, get_delay_slot_nop (insn));
71400594
RS
5826 if (tmp_nops > nops)
5827 nops = tmp_nops;
5828 }
11625dd8 5829 else if (compact_branch_p (insn))
71400594 5830 {
932d1a1b 5831 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
71400594
RS
5832 if (tmp_nops > nops)
5833 nops = tmp_nops;
5834 }
5835 return nops;
5836}
5837
c67a084a
NC
5838/* Fix NOP issue: Replace nops by "or at,at,zero". */
5839
5840static void
5841fix_loongson2f_nop (struct mips_cl_insn * ip)
5842{
df58fc94 5843 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
5844 if (strcmp (ip->insn_mo->name, "nop") == 0)
5845 ip->insn_opcode = LOONGSON2F_NOP_INSN;
5846}
5847
5848/* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
5849 jr target pc &= 'hffff_ffff_cfff_ffff. */
5850
5851static void
5852fix_loongson2f_jump (struct mips_cl_insn * ip)
5853{
df58fc94 5854 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
5855 if (strcmp (ip->insn_mo->name, "j") == 0
5856 || strcmp (ip->insn_mo->name, "jr") == 0
5857 || strcmp (ip->insn_mo->name, "jalr") == 0)
5858 {
5859 int sreg;
5860 expressionS ep;
5861
5862 if (! mips_opts.at)
5863 return;
5864
df58fc94 5865 sreg = EXTRACT_OPERAND (0, RS, *ip);
c67a084a
NC
5866 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
5867 return;
5868
5869 ep.X_op = O_constant;
5870 ep.X_add_number = 0xcfff0000;
5871 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
5872 ep.X_add_number = 0xffff;
5873 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
5874 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
5875 }
5876}
5877
5878static void
5879fix_loongson2f (struct mips_cl_insn * ip)
5880{
5881 if (mips_fix_loongson2f_nop)
5882 fix_loongson2f_nop (ip);
5883
5884 if (mips_fix_loongson2f_jump)
5885 fix_loongson2f_jump (ip);
5886}
5887
a4e06468
RS
5888/* IP is a branch that has a delay slot, and we need to fill it
5889 automatically. Return true if we can do that by swapping IP
e407c74b
NC
5890 with the previous instruction.
5891 ADDRESS_EXPR is an operand of the instruction to be used with
5892 RELOC_TYPE. */
a4e06468
RS
5893
5894static bfd_boolean
e407c74b 5895can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 5896 bfd_reloc_code_real_type *reloc_type)
a4e06468 5897{
2b0c8b40 5898 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
a4e06468
RS
5899 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
5900
5901 /* -O2 and above is required for this optimization. */
5902 if (mips_optimize < 2)
5903 return FALSE;
5904
5905 /* If we have seen .set volatile or .set nomove, don't optimize. */
5906 if (mips_opts.nomove)
5907 return FALSE;
5908
5909 /* We can't swap if the previous instruction's position is fixed. */
5910 if (history[0].fixed_p)
5911 return FALSE;
5912
5913 /* If the previous previous insn was in a .set noreorder, we can't
5914 swap. Actually, the MIPS assembler will swap in this situation.
5915 However, gcc configured -with-gnu-as will generate code like
5916
5917 .set noreorder
5918 lw $4,XXX
5919 .set reorder
5920 INSN
5921 bne $4,$0,foo
5922
5923 in which we can not swap the bne and INSN. If gcc is not configured
5924 -with-gnu-as, it does not output the .set pseudo-ops. */
5925 if (history[1].noreorder_p)
5926 return FALSE;
5927
87333bb7
MR
5928 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
5929 This means that the previous instruction was a 4-byte one anyhow. */
a4e06468
RS
5930 if (mips_opts.mips16 && history[0].fixp[0])
5931 return FALSE;
5932
5933 /* If the branch is itself the target of a branch, we can not swap.
5934 We cheat on this; all we check for is whether there is a label on
5935 this instruction. If there are any branches to anything other than
5936 a label, users must use .set noreorder. */
5937 if (seg_info (now_seg)->label_list)
5938 return FALSE;
5939
5940 /* If the previous instruction is in a variant frag other than this
2309ddf2 5941 branch's one, we cannot do the swap. This does not apply to
9301f9c3
MR
5942 MIPS16 code, which uses variant frags for different purposes. */
5943 if (!mips_opts.mips16
a4e06468
RS
5944 && history[0].frag
5945 && history[0].frag->fr_type == rs_machine_dependent)
5946 return FALSE;
5947
bcd530a7
RS
5948 /* We do not swap with instructions that cannot architecturally
5949 be placed in a branch delay slot, such as SYNC or ERET. We
5950 also refrain from swapping with a trap instruction, since it
5951 complicates trap handlers to have the trap instruction be in
5952 a delay slot. */
a4e06468 5953 prev_pinfo = history[0].insn_mo->pinfo;
bcd530a7 5954 if (prev_pinfo & INSN_NO_DELAY_SLOT)
a4e06468
RS
5955 return FALSE;
5956
5957 /* Check for conflicts between the branch and the instructions
5958 before the candidate delay slot. */
5959 if (nops_for_insn (0, history + 1, ip) > 0)
5960 return FALSE;
5961
5962 /* Check for conflicts between the swapped sequence and the
5963 target of the branch. */
5964 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
5965 return FALSE;
5966
5967 /* If the branch reads a register that the previous
5968 instruction sets, we can not swap. */
5969 gpr_read = gpr_read_mask (ip);
5970 prev_gpr_write = gpr_write_mask (&history[0]);
5971 if (gpr_read & prev_gpr_write)
5972 return FALSE;
5973
5974 /* If the branch writes a register that the previous
5975 instruction sets, we can not swap. */
5976 gpr_write = gpr_write_mask (ip);
5977 if (gpr_write & prev_gpr_write)
5978 return FALSE;
5979
5980 /* If the branch writes a register that the previous
5981 instruction reads, we can not swap. */
5982 prev_gpr_read = gpr_read_mask (&history[0]);
5983 if (gpr_write & prev_gpr_read)
5984 return FALSE;
5985
5986 /* If one instruction sets a condition code and the
5987 other one uses a condition code, we can not swap. */
5988 pinfo = ip->insn_mo->pinfo;
5989 if ((pinfo & INSN_READ_COND_CODE)
5990 && (prev_pinfo & INSN_WRITE_COND_CODE))
5991 return FALSE;
5992 if ((pinfo & INSN_WRITE_COND_CODE)
5993 && (prev_pinfo & INSN_READ_COND_CODE))
5994 return FALSE;
5995
5996 /* If the previous instruction uses the PC, we can not swap. */
2b0c8b40 5997 prev_pinfo2 = history[0].insn_mo->pinfo2;
26545944 5998 if (prev_pinfo2 & INSN2_READ_PC)
2b0c8b40 5999 return FALSE;
a4e06468 6000
df58fc94
RS
6001 /* If the previous instruction has an incorrect size for a fixed
6002 branch delay slot in microMIPS mode, we cannot swap. */
2309ddf2
MR
6003 pinfo2 = ip->insn_mo->pinfo2;
6004 if (mips_opts.micromips
6005 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
6006 && insn_length (history) != 2)
6007 return FALSE;
6008 if (mips_opts.micromips
6009 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
6010 && insn_length (history) != 4)
6011 return FALSE;
6012
e407c74b
NC
6013 /* On R5900 short loops need to be fixed by inserting a nop in
6014 the branch delay slots.
6015 A short loop can be terminated too early. */
6016 if (mips_opts.arch == CPU_R5900
6017 /* Check if instruction has a parameter, ignore "j $31". */
6018 && (address_expr != NULL)
6019 /* Parameter must be 16 bit. */
6020 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
6021 /* Branch to same segment. */
6022 && (S_GET_SEGMENT(address_expr->X_add_symbol) == now_seg)
6023 /* Branch to same code fragment. */
6024 && (symbol_get_frag(address_expr->X_add_symbol) == frag_now)
6025 /* Can only calculate branch offset if value is known. */
6026 && symbol_constant_p(address_expr->X_add_symbol)
6027 /* Check if branch is really conditional. */
6028 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
6029 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
6030 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
6031 {
6032 int distance;
6033 /* Check if loop is shorter than 6 instructions including
6034 branch and delay slot. */
6035 distance = frag_now_fix() - S_GET_VALUE(address_expr->X_add_symbol);
6036 if (distance <= 20)
6037 {
6038 int i;
6039 int rv;
6040
6041 rv = FALSE;
6042 /* When the loop includes branches or jumps,
6043 it is not a short loop. */
6044 for (i = 0; i < (distance / 4); i++)
6045 {
6046 if ((history[i].cleared_p)
6047 || delayed_branch_p(&history[i]))
6048 {
6049 rv = TRUE;
6050 break;
6051 }
6052 }
6053 if (rv == FALSE)
6054 {
6055 /* Insert nop after branch to fix short loop. */
6056 return FALSE;
6057 }
6058 }
6059 }
6060
a4e06468
RS
6061 return TRUE;
6062}
6063
e407c74b
NC
6064/* Decide how we should add IP to the instruction stream.
6065 ADDRESS_EXPR is an operand of the instruction to be used with
6066 RELOC_TYPE. */
a4e06468
RS
6067
6068static enum append_method
e407c74b 6069get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 6070 bfd_reloc_code_real_type *reloc_type)
a4e06468 6071{
a4e06468
RS
6072 /* The relaxed version of a macro sequence must be inherently
6073 hazard-free. */
6074 if (mips_relax.sequence == 2)
6075 return APPEND_ADD;
6076
6077 /* We must not dabble with instructions in a ".set norerorder" block. */
6078 if (mips_opts.noreorder)
6079 return APPEND_ADD;
6080
6081 /* Otherwise, it's our responsibility to fill branch delay slots. */
11625dd8 6082 if (delayed_branch_p (ip))
a4e06468 6083 {
e407c74b
NC
6084 if (!branch_likely_p (ip)
6085 && can_swap_branch_p (ip, address_expr, reloc_type))
a4e06468
RS
6086 return APPEND_SWAP;
6087
6088 if (mips_opts.mips16
6089 && ISA_SUPPORTS_MIPS16E
fc76e730 6090 && gpr_read_mask (ip) != 0)
a4e06468
RS
6091 return APPEND_ADD_COMPACT;
6092
6093 return APPEND_ADD_WITH_NOP;
6094 }
6095
a4e06468
RS
6096 return APPEND_ADD;
6097}
6098
ceb94aa5
RS
6099/* IP is a MIPS16 instruction whose opcode we have just changed.
6100 Point IP->insn_mo to the new opcode's definition. */
6101
6102static void
6103find_altered_mips16_opcode (struct mips_cl_insn *ip)
6104{
6105 const struct mips_opcode *mo, *end;
6106
6107 end = &mips16_opcodes[bfd_mips16_num_opcodes];
6108 for (mo = ip->insn_mo; mo < end; mo++)
6109 if ((ip->insn_opcode & mo->mask) == mo->match)
6110 {
6111 ip->insn_mo = mo;
6112 return;
6113 }
6114 abort ();
6115}
6116
df58fc94
RS
6117/* For microMIPS macros, we need to generate a local number label
6118 as the target of branches. */
6119#define MICROMIPS_LABEL_CHAR '\037'
6120static unsigned long micromips_target_label;
6121static char micromips_target_name[32];
6122
6123static char *
6124micromips_label_name (void)
6125{
6126 char *p = micromips_target_name;
6127 char symbol_name_temporary[24];
6128 unsigned long l;
6129 int i;
6130
6131 if (*p)
6132 return p;
6133
6134 i = 0;
6135 l = micromips_target_label;
6136#ifdef LOCAL_LABEL_PREFIX
6137 *p++ = LOCAL_LABEL_PREFIX;
6138#endif
6139 *p++ = 'L';
6140 *p++ = MICROMIPS_LABEL_CHAR;
6141 do
6142 {
6143 symbol_name_temporary[i++] = l % 10 + '0';
6144 l /= 10;
6145 }
6146 while (l != 0);
6147 while (i > 0)
6148 *p++ = symbol_name_temporary[--i];
6149 *p = '\0';
6150
6151 return micromips_target_name;
6152}
6153
6154static void
6155micromips_label_expr (expressionS *label_expr)
6156{
6157 label_expr->X_op = O_symbol;
6158 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
6159 label_expr->X_add_number = 0;
6160}
6161
6162static void
6163micromips_label_inc (void)
6164{
6165 micromips_target_label++;
6166 *micromips_target_name = '\0';
6167}
6168
6169static void
6170micromips_add_label (void)
6171{
6172 symbolS *s;
6173
6174 s = colon (micromips_label_name ());
6175 micromips_label_inc ();
f3ded42a 6176 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
df58fc94
RS
6177}
6178
6179/* If assembling microMIPS code, then return the microMIPS reloc
6180 corresponding to the requested one if any. Otherwise return
6181 the reloc unchanged. */
6182
6183static bfd_reloc_code_real_type
6184micromips_map_reloc (bfd_reloc_code_real_type reloc)
6185{
6186 static const bfd_reloc_code_real_type relocs[][2] =
6187 {
6188 /* Keep sorted incrementally by the left-hand key. */
6189 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
6190 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
6191 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
6192 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
6193 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
6194 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
6195 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
6196 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
6197 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
6198 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
6199 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
6200 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
6201 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
6202 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
6203 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
6204 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
6205 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
6206 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
6207 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
6208 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
6209 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
6210 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
6211 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
6212 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
6213 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
6214 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
6215 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
6216 };
6217 bfd_reloc_code_real_type r;
6218 size_t i;
6219
6220 if (!mips_opts.micromips)
6221 return reloc;
6222 for (i = 0; i < ARRAY_SIZE (relocs); i++)
6223 {
6224 r = relocs[i][0];
6225 if (r > reloc)
6226 return reloc;
6227 if (r == reloc)
6228 return relocs[i][1];
6229 }
6230 return reloc;
6231}
6232
b886a2ab
RS
6233/* Try to resolve relocation RELOC against constant OPERAND at assembly time.
6234 Return true on success, storing the resolved value in RESULT. */
6235
6236static bfd_boolean
6237calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
6238 offsetT *result)
6239{
6240 switch (reloc)
6241 {
6242 case BFD_RELOC_MIPS_HIGHEST:
6243 case BFD_RELOC_MICROMIPS_HIGHEST:
6244 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
6245 return TRUE;
6246
6247 case BFD_RELOC_MIPS_HIGHER:
6248 case BFD_RELOC_MICROMIPS_HIGHER:
6249 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
6250 return TRUE;
6251
6252 case BFD_RELOC_HI16_S:
6253 case BFD_RELOC_MICROMIPS_HI16_S:
6254 case BFD_RELOC_MIPS16_HI16_S:
6255 *result = ((operand + 0x8000) >> 16) & 0xffff;
6256 return TRUE;
6257
6258 case BFD_RELOC_HI16:
6259 case BFD_RELOC_MICROMIPS_HI16:
6260 case BFD_RELOC_MIPS16_HI16:
6261 *result = (operand >> 16) & 0xffff;
6262 return TRUE;
6263
6264 case BFD_RELOC_LO16:
6265 case BFD_RELOC_MICROMIPS_LO16:
6266 case BFD_RELOC_MIPS16_LO16:
6267 *result = operand & 0xffff;
6268 return TRUE;
6269
6270 case BFD_RELOC_UNUSED:
6271 *result = operand;
6272 return TRUE;
6273
6274 default:
6275 return FALSE;
6276 }
6277}
6278
71400594
RS
6279/* Output an instruction. IP is the instruction information.
6280 ADDRESS_EXPR is an operand of the instruction to be used with
df58fc94
RS
6281 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
6282 a macro expansion. */
71400594
RS
6283
6284static void
6285append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
df58fc94 6286 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
71400594 6287{
14fe068b 6288 unsigned long prev_pinfo2, pinfo;
71400594 6289 bfd_boolean relaxed_branch = FALSE;
a4e06468 6290 enum append_method method;
2309ddf2 6291 bfd_boolean relax32;
2b0c8b40 6292 int branch_disp;
71400594 6293
2309ddf2 6294 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
c67a084a
NC
6295 fix_loongson2f (ip);
6296
738f4d98 6297 file_ase_mips16 |= mips_opts.mips16;
df58fc94 6298 file_ase_micromips |= mips_opts.micromips;
738f4d98 6299
df58fc94 6300 prev_pinfo2 = history[0].insn_mo->pinfo2;
71400594 6301 pinfo = ip->insn_mo->pinfo;
df58fc94
RS
6302
6303 if (mips_opts.micromips
6304 && !expansionp
6305 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
6306 && micromips_insn_length (ip->insn_mo) != 2)
6307 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
6308 && micromips_insn_length (ip->insn_mo) != 4)))
6309 as_warn (_("Wrong size instruction in a %u-bit branch delay slot"),
6310 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
71400594 6311
15be625d
CM
6312 if (address_expr == NULL)
6313 ip->complete_p = 1;
b886a2ab
RS
6314 else if (reloc_type[0] <= BFD_RELOC_UNUSED
6315 && reloc_type[1] == BFD_RELOC_UNUSED
6316 && reloc_type[2] == BFD_RELOC_UNUSED
15be625d
CM
6317 && address_expr->X_op == O_constant)
6318 {
15be625d
CM
6319 switch (*reloc_type)
6320 {
15be625d 6321 case BFD_RELOC_MIPS_JMP:
df58fc94
RS
6322 {
6323 int shift;
6324
6325 shift = mips_opts.micromips ? 1 : 2;
6326 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
6327 as_bad (_("jump to misaligned address (0x%lx)"),
6328 (unsigned long) address_expr->X_add_number);
6329 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
6330 & 0x3ffffff);
335574df 6331 ip->complete_p = 1;
df58fc94 6332 }
15be625d
CM
6333 break;
6334
6335 case BFD_RELOC_MIPS16_JMP:
6336 if ((address_expr->X_add_number & 3) != 0)
6337 as_bad (_("jump to misaligned address (0x%lx)"),
6338 (unsigned long) address_expr->X_add_number);
6339 ip->insn_opcode |=
6340 (((address_expr->X_add_number & 0x7c0000) << 3)
6341 | ((address_expr->X_add_number & 0xf800000) >> 7)
6342 | ((address_expr->X_add_number & 0x3fffc) >> 2));
335574df 6343 ip->complete_p = 1;
15be625d
CM
6344 break;
6345
6346 case BFD_RELOC_16_PCREL_S2:
df58fc94
RS
6347 {
6348 int shift;
6349
6350 shift = mips_opts.micromips ? 1 : 2;
6351 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
6352 as_bad (_("branch to misaligned address (0x%lx)"),
6353 (unsigned long) address_expr->X_add_number);
6354 if (!mips_relax_branch)
6355 {
6356 if ((address_expr->X_add_number + (1 << (shift + 15)))
6357 & ~((1 << (shift + 16)) - 1))
6358 as_bad (_("branch address range overflow (0x%lx)"),
6359 (unsigned long) address_expr->X_add_number);
6360 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
6361 & 0xffff);
6362 }
df58fc94 6363 }
15be625d
CM
6364 break;
6365
6366 default:
b886a2ab
RS
6367 {
6368 offsetT value;
6369
6370 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
6371 &value))
6372 {
6373 ip->insn_opcode |= value & 0xffff;
6374 ip->complete_p = 1;
6375 }
6376 }
6377 break;
6378 }
15be625d
CM
6379 }
6380
71400594
RS
6381 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
6382 {
6383 /* There are a lot of optimizations we could do that we don't.
6384 In particular, we do not, in general, reorder instructions.
6385 If you use gcc with optimization, it will reorder
6386 instructions and generally do much more optimization then we
6387 do here; repeating all that work in the assembler would only
6388 benefit hand written assembly code, and does not seem worth
6389 it. */
6390 int nops = (mips_optimize == 0
932d1a1b
RS
6391 ? nops_for_insn (0, history, NULL)
6392 : nops_for_insn_or_target (0, history, ip));
71400594 6393 if (nops > 0)
252b5132
RH
6394 {
6395 fragS *old_frag;
6396 unsigned long old_frag_offset;
6397 int i;
252b5132
RH
6398
6399 old_frag = frag_now;
6400 old_frag_offset = frag_now_fix ();
6401
6402 for (i = 0; i < nops; i++)
14fe068b
RS
6403 add_fixed_insn (NOP_INSN);
6404 insert_into_history (0, nops, NOP_INSN);
252b5132
RH
6405
6406 if (listing)
6407 {
6408 listing_prev_line ();
6409 /* We may be at the start of a variant frag. In case we
6410 are, make sure there is enough space for the frag
6411 after the frags created by listing_prev_line. The
6412 argument to frag_grow here must be at least as large
6413 as the argument to all other calls to frag_grow in
6414 this file. We don't have to worry about being in the
6415 middle of a variant frag, because the variants insert
6416 all needed nop instructions themselves. */
6417 frag_grow (40);
6418 }
6419
462427c4 6420 mips_move_text_labels ();
252b5132
RH
6421
6422#ifndef NO_ECOFF_DEBUGGING
6423 if (ECOFF_DEBUGGING)
6424 ecoff_fix_loc (old_frag, old_frag_offset);
6425#endif
6426 }
71400594
RS
6427 }
6428 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
6429 {
932d1a1b
RS
6430 int nops;
6431
6432 /* Work out how many nops in prev_nop_frag are needed by IP,
6433 ignoring hazards generated by the first prev_nop_frag_since
6434 instructions. */
6435 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
9c2799c2 6436 gas_assert (nops <= prev_nop_frag_holds);
252b5132 6437
71400594
RS
6438 /* Enforce NOPS as a minimum. */
6439 if (nops > prev_nop_frag_required)
6440 prev_nop_frag_required = nops;
252b5132 6441
71400594
RS
6442 if (prev_nop_frag_holds == prev_nop_frag_required)
6443 {
6444 /* Settle for the current number of nops. Update the history
6445 accordingly (for the benefit of any future .set reorder code). */
6446 prev_nop_frag = NULL;
6447 insert_into_history (prev_nop_frag_since,
6448 prev_nop_frag_holds, NOP_INSN);
6449 }
6450 else
6451 {
6452 /* Allow this instruction to replace one of the nops that was
6453 tentatively added to prev_nop_frag. */
df58fc94 6454 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
71400594
RS
6455 prev_nop_frag_holds--;
6456 prev_nop_frag_since++;
252b5132
RH
6457 }
6458 }
6459
e407c74b 6460 method = get_append_method (ip, address_expr, reloc_type);
2b0c8b40 6461 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
a4e06468 6462
e410add4
RS
6463 dwarf2_emit_insn (0);
6464 /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
6465 so "move" the instruction address accordingly.
6466
6467 Also, it doesn't seem appropriate for the assembler to reorder .loc
6468 entries. If this instruction is a branch that we are going to swap
6469 with the previous instruction, the two instructions should be
6470 treated as a unit, and the debug information for both instructions
6471 should refer to the start of the branch sequence. Using the
6472 current position is certainly wrong when swapping a 32-bit branch
6473 and a 16-bit delay slot, since the current position would then be
6474 in the middle of a branch. */
6475 dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
58e2ea4d 6476
df58fc94
RS
6477 relax32 = (mips_relax_branch
6478 /* Don't try branch relaxation within .set nomacro, or within
6479 .set noat if we use $at for PIC computations. If it turns
6480 out that the branch was out-of-range, we'll get an error. */
6481 && !mips_opts.warn_about_macros
6482 && (mips_opts.at || mips_pic == NO_PIC)
3bf0dbfb
MR
6483 /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
6484 as they have no complementing branches. */
6485 && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
df58fc94
RS
6486
6487 if (!HAVE_CODE_COMPRESSION
6488 && address_expr
6489 && relax32
0b25d3e6 6490 && *reloc_type == BFD_RELOC_16_PCREL_S2
11625dd8 6491 && delayed_branch_p (ip))
4a6a3df4 6492 {
895921c9 6493 relaxed_branch = TRUE;
1e915849
RS
6494 add_relaxed_insn (ip, (relaxed_branch_length
6495 (NULL, NULL,
11625dd8
RS
6496 uncond_branch_p (ip) ? -1
6497 : branch_likely_p (ip) ? 1
1e915849
RS
6498 : 0)), 4,
6499 RELAX_BRANCH_ENCODE
66b3e8da 6500 (AT,
11625dd8
RS
6501 uncond_branch_p (ip),
6502 branch_likely_p (ip),
1e915849
RS
6503 pinfo & INSN_WRITE_GPR_31,
6504 0),
6505 address_expr->X_add_symbol,
6506 address_expr->X_add_number);
4a6a3df4
AO
6507 *reloc_type = BFD_RELOC_UNUSED;
6508 }
df58fc94
RS
6509 else if (mips_opts.micromips
6510 && address_expr
6511 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
6512 || *reloc_type > BFD_RELOC_UNUSED)
40209cad
MR
6513 && (delayed_branch_p (ip) || compact_branch_p (ip))
6514 /* Don't try branch relaxation when users specify
6515 16-bit/32-bit instructions. */
6516 && !forced_insn_length)
df58fc94
RS
6517 {
6518 bfd_boolean relax16 = *reloc_type > BFD_RELOC_UNUSED;
6519 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
11625dd8
RS
6520 int uncond = uncond_branch_p (ip) ? -1 : 0;
6521 int compact = compact_branch_p (ip);
df58fc94
RS
6522 int al = pinfo & INSN_WRITE_GPR_31;
6523 int length32;
6524
6525 gas_assert (address_expr != NULL);
6526 gas_assert (!mips_relax.sequence);
6527
2b0c8b40 6528 relaxed_branch = TRUE;
df58fc94
RS
6529 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
6530 add_relaxed_insn (ip, relax32 ? length32 : 4, relax16 ? 2 : 4,
40209cad
MR
6531 RELAX_MICROMIPS_ENCODE (type, AT, uncond, compact, al,
6532 relax32, 0, 0),
df58fc94
RS
6533 address_expr->X_add_symbol,
6534 address_expr->X_add_number);
6535 *reloc_type = BFD_RELOC_UNUSED;
6536 }
6537 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
6538 {
6539 /* We need to set up a variant frag. */
df58fc94 6540 gas_assert (address_expr != NULL);
1e915849
RS
6541 add_relaxed_insn (ip, 4, 0,
6542 RELAX_MIPS16_ENCODE
6543 (*reloc_type - BFD_RELOC_UNUSED,
df58fc94 6544 forced_insn_length == 2, forced_insn_length == 4,
11625dd8 6545 delayed_branch_p (&history[0]),
1e915849
RS
6546 history[0].mips16_absolute_jump_p),
6547 make_expr_symbol (address_expr), 0);
252b5132 6548 }
5c04167a 6549 else if (mips_opts.mips16 && insn_length (ip) == 2)
9497f5ac 6550 {
11625dd8 6551 if (!delayed_branch_p (ip))
b8ee1a6e
DU
6552 /* Make sure there is enough room to swap this instruction with
6553 a following jump instruction. */
6554 frag_grow (6);
1e915849 6555 add_fixed_insn (ip);
252b5132
RH
6556 }
6557 else
6558 {
6559 if (mips_opts.mips16
6560 && mips_opts.noreorder
11625dd8 6561 && delayed_branch_p (&history[0]))
252b5132
RH
6562 as_warn (_("extended instruction in delay slot"));
6563
4d7206a2
RS
6564 if (mips_relax.sequence)
6565 {
6566 /* If we've reached the end of this frag, turn it into a variant
6567 frag and record the information for the instructions we've
6568 written so far. */
6569 if (frag_room () < 4)
6570 relax_close_frag ();
df58fc94 6571 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4d7206a2
RS
6572 }
6573
584892a6 6574 if (mips_relax.sequence != 2)
df58fc94
RS
6575 {
6576 if (mips_macro_warning.first_insn_sizes[0] == 0)
6577 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
6578 mips_macro_warning.sizes[0] += insn_length (ip);
6579 mips_macro_warning.insns[0]++;
6580 }
584892a6 6581 if (mips_relax.sequence != 1)
df58fc94
RS
6582 {
6583 if (mips_macro_warning.first_insn_sizes[1] == 0)
6584 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
6585 mips_macro_warning.sizes[1] += insn_length (ip);
6586 mips_macro_warning.insns[1]++;
6587 }
584892a6 6588
1e915849
RS
6589 if (mips_opts.mips16)
6590 {
6591 ip->fixed_p = 1;
6592 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
6593 }
6594 add_fixed_insn (ip);
252b5132
RH
6595 }
6596
9fe77896 6597 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
252b5132 6598 {
df58fc94 6599 bfd_reloc_code_real_type final_type[3];
2309ddf2 6600 reloc_howto_type *howto0;
9fe77896
RS
6601 reloc_howto_type *howto;
6602 int i;
34ce925e 6603
df58fc94
RS
6604 /* Perform any necessary conversion to microMIPS relocations
6605 and find out how many relocations there actually are. */
6606 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
6607 final_type[i] = micromips_map_reloc (reloc_type[i]);
6608
9fe77896
RS
6609 /* In a compound relocation, it is the final (outermost)
6610 operator that determines the relocated field. */
2309ddf2 6611 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
e8044f35
RS
6612 if (!howto)
6613 abort ();
2309ddf2
MR
6614
6615 if (i > 1)
6616 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
9fe77896
RS
6617 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
6618 bfd_get_reloc_size (howto),
6619 address_expr,
2309ddf2
MR
6620 howto0 && howto0->pc_relative,
6621 final_type[0]);
9fe77896
RS
6622
6623 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
2309ddf2 6624 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
9fe77896
RS
6625 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
6626
6627 /* These relocations can have an addend that won't fit in
6628 4 octets for 64bit assembly. */
6629 if (HAVE_64BIT_GPRS
6630 && ! howto->partial_inplace
6631 && (reloc_type[0] == BFD_RELOC_16
6632 || reloc_type[0] == BFD_RELOC_32
6633 || reloc_type[0] == BFD_RELOC_MIPS_JMP
6634 || reloc_type[0] == BFD_RELOC_GPREL16
6635 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
6636 || reloc_type[0] == BFD_RELOC_GPREL32
6637 || reloc_type[0] == BFD_RELOC_64
6638 || reloc_type[0] == BFD_RELOC_CTOR
6639 || reloc_type[0] == BFD_RELOC_MIPS_SUB
6640 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
6641 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
6642 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
6643 || reloc_type[0] == BFD_RELOC_MIPS_REL16
6644 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
6645 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
6646 || hi16_reloc_p (reloc_type[0])
6647 || lo16_reloc_p (reloc_type[0])))
6648 ip->fixp[0]->fx_no_overflow = 1;
6649
ddaf2c41
MR
6650 /* These relocations can have an addend that won't fit in 2 octets. */
6651 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
6652 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
6653 ip->fixp[0]->fx_no_overflow = 1;
6654
9fe77896
RS
6655 if (mips_relax.sequence)
6656 {
6657 if (mips_relax.first_fixup == 0)
6658 mips_relax.first_fixup = ip->fixp[0];
6659 }
6660 else if (reloc_needs_lo_p (*reloc_type))
6661 {
6662 struct mips_hi_fixup *hi_fixup;
6663
6664 /* Reuse the last entry if it already has a matching %lo. */
6665 hi_fixup = mips_hi_fixup_list;
6666 if (hi_fixup == 0
6667 || !fixup_has_matching_lo_p (hi_fixup->fixp))
4d7206a2 6668 {
9fe77896
RS
6669 hi_fixup = ((struct mips_hi_fixup *)
6670 xmalloc (sizeof (struct mips_hi_fixup)));
6671 hi_fixup->next = mips_hi_fixup_list;
6672 mips_hi_fixup_list = hi_fixup;
4d7206a2 6673 }
9fe77896
RS
6674 hi_fixup->fixp = ip->fixp[0];
6675 hi_fixup->seg = now_seg;
6676 }
252b5132 6677
9fe77896
RS
6678 /* Add fixups for the second and third relocations, if given.
6679 Note that the ABI allows the second relocation to be
6680 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
6681 moment we only use RSS_UNDEF, but we could add support
6682 for the others if it ever becomes necessary. */
6683 for (i = 1; i < 3; i++)
6684 if (reloc_type[i] != BFD_RELOC_UNUSED)
6685 {
6686 ip->fixp[i] = fix_new (ip->frag, ip->where,
6687 ip->fixp[0]->fx_size, NULL, 0,
df58fc94 6688 FALSE, final_type[i]);
f6688943 6689
9fe77896
RS
6690 /* Use fx_tcbit to mark compound relocs. */
6691 ip->fixp[0]->fx_tcbit = 1;
6692 ip->fixp[i]->fx_tcbit = 1;
6693 }
252b5132 6694 }
1e915849 6695 install_insn (ip);
252b5132
RH
6696
6697 /* Update the register mask information. */
4c260379
RS
6698 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
6699 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
252b5132 6700
a4e06468 6701 switch (method)
252b5132 6702 {
a4e06468
RS
6703 case APPEND_ADD:
6704 insert_into_history (0, 1, ip);
6705 break;
6706
6707 case APPEND_ADD_WITH_NOP:
14fe068b
RS
6708 {
6709 struct mips_cl_insn *nop;
6710
6711 insert_into_history (0, 1, ip);
6712 nop = get_delay_slot_nop (ip);
6713 add_fixed_insn (nop);
6714 insert_into_history (0, 1, nop);
6715 if (mips_relax.sequence)
6716 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
6717 }
a4e06468
RS
6718 break;
6719
6720 case APPEND_ADD_COMPACT:
6721 /* Convert MIPS16 jr/jalr into a "compact" jump. */
6722 gas_assert (mips_opts.mips16);
6723 ip->insn_opcode |= 0x0080;
6724 find_altered_mips16_opcode (ip);
6725 install_insn (ip);
6726 insert_into_history (0, 1, ip);
6727 break;
6728
6729 case APPEND_SWAP:
6730 {
6731 struct mips_cl_insn delay = history[0];
6732 if (mips_opts.mips16)
6733 {
6734 know (delay.frag == ip->frag);
6735 move_insn (ip, delay.frag, delay.where);
6736 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
6737 }
464ab0e5 6738 else if (relaxed_branch || delay.frag != ip->frag)
a4e06468
RS
6739 {
6740 /* Add the delay slot instruction to the end of the
6741 current frag and shrink the fixed part of the
6742 original frag. If the branch occupies the tail of
6743 the latter, move it backwards to cover the gap. */
2b0c8b40 6744 delay.frag->fr_fix -= branch_disp;
a4e06468 6745 if (delay.frag == ip->frag)
2b0c8b40 6746 move_insn (ip, ip->frag, ip->where - branch_disp);
a4e06468
RS
6747 add_fixed_insn (&delay);
6748 }
6749 else
6750 {
2b0c8b40
MR
6751 move_insn (&delay, ip->frag,
6752 ip->where - branch_disp + insn_length (ip));
a4e06468
RS
6753 move_insn (ip, history[0].frag, history[0].where);
6754 }
6755 history[0] = *ip;
6756 delay.fixed_p = 1;
6757 insert_into_history (0, 1, &delay);
6758 }
6759 break;
252b5132
RH
6760 }
6761
13408f1e 6762 /* If we have just completed an unconditional branch, clear the history. */
11625dd8
RS
6763 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
6764 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
e407c74b
NC
6765 {
6766 unsigned int i;
6767
79850f26 6768 mips_no_prev_insn ();
13408f1e 6769
e407c74b 6770 for (i = 0; i < ARRAY_SIZE (history); i++)
79850f26 6771 history[i].cleared_p = 1;
e407c74b
NC
6772 }
6773
df58fc94
RS
6774 /* We need to emit a label at the end of branch-likely macros. */
6775 if (emit_branch_likely_macro)
6776 {
6777 emit_branch_likely_macro = FALSE;
6778 micromips_add_label ();
6779 }
6780
252b5132
RH
6781 /* We just output an insn, so the next one doesn't have a label. */
6782 mips_clear_insn_labels ();
252b5132
RH
6783}
6784
e407c74b
NC
6785/* Forget that there was any previous instruction or label.
6786 When BRANCH is true, the branch history is also flushed. */
252b5132
RH
6787
6788static void
7d10b47d 6789mips_no_prev_insn (void)
252b5132 6790{
7d10b47d
RS
6791 prev_nop_frag = NULL;
6792 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
6793 mips_clear_insn_labels ();
6794}
6795
7d10b47d
RS
6796/* This function must be called before we emit something other than
6797 instructions. It is like mips_no_prev_insn except that it inserts
6798 any NOPS that might be needed by previous instructions. */
252b5132 6799
7d10b47d
RS
6800void
6801mips_emit_delays (void)
252b5132
RH
6802{
6803 if (! mips_opts.noreorder)
6804 {
932d1a1b 6805 int nops = nops_for_insn (0, history, NULL);
252b5132
RH
6806 if (nops > 0)
6807 {
7d10b47d
RS
6808 while (nops-- > 0)
6809 add_fixed_insn (NOP_INSN);
462427c4 6810 mips_move_text_labels ();
7d10b47d
RS
6811 }
6812 }
6813 mips_no_prev_insn ();
6814}
6815
6816/* Start a (possibly nested) noreorder block. */
6817
6818static void
6819start_noreorder (void)
6820{
6821 if (mips_opts.noreorder == 0)
6822 {
6823 unsigned int i;
6824 int nops;
6825
6826 /* None of the instructions before the .set noreorder can be moved. */
6827 for (i = 0; i < ARRAY_SIZE (history); i++)
6828 history[i].fixed_p = 1;
6829
6830 /* Insert any nops that might be needed between the .set noreorder
6831 block and the previous instructions. We will later remove any
6832 nops that turn out not to be needed. */
932d1a1b 6833 nops = nops_for_insn (0, history, NULL);
7d10b47d
RS
6834 if (nops > 0)
6835 {
6836 if (mips_optimize != 0)
252b5132
RH
6837 {
6838 /* Record the frag which holds the nop instructions, so
6839 that we can remove them if we don't need them. */
df58fc94 6840 frag_grow (nops * NOP_INSN_SIZE);
252b5132
RH
6841 prev_nop_frag = frag_now;
6842 prev_nop_frag_holds = nops;
6843 prev_nop_frag_required = 0;
6844 prev_nop_frag_since = 0;
6845 }
6846
6847 for (; nops > 0; --nops)
1e915849 6848 add_fixed_insn (NOP_INSN);
252b5132 6849
7d10b47d
RS
6850 /* Move on to a new frag, so that it is safe to simply
6851 decrease the size of prev_nop_frag. */
6852 frag_wane (frag_now);
6853 frag_new (0);
462427c4 6854 mips_move_text_labels ();
252b5132 6855 }
df58fc94 6856 mips_mark_labels ();
7d10b47d 6857 mips_clear_insn_labels ();
252b5132 6858 }
7d10b47d
RS
6859 mips_opts.noreorder++;
6860 mips_any_noreorder = 1;
6861}
252b5132 6862
7d10b47d 6863/* End a nested noreorder block. */
252b5132 6864
7d10b47d
RS
6865static void
6866end_noreorder (void)
6867{
6868 mips_opts.noreorder--;
6869 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
6870 {
6871 /* Commit to inserting prev_nop_frag_required nops and go back to
6872 handling nop insertion the .set reorder way. */
6873 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
df58fc94 6874 * NOP_INSN_SIZE);
7d10b47d
RS
6875 insert_into_history (prev_nop_frag_since,
6876 prev_nop_frag_required, NOP_INSN);
6877 prev_nop_frag = NULL;
6878 }
252b5132
RH
6879}
6880
584892a6
RS
6881/* Set up global variables for the start of a new macro. */
6882
6883static void
6884macro_start (void)
6885{
6886 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
df58fc94
RS
6887 memset (&mips_macro_warning.first_insn_sizes, 0,
6888 sizeof (mips_macro_warning.first_insn_sizes));
6889 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
584892a6 6890 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
11625dd8 6891 && delayed_branch_p (&history[0]));
df58fc94
RS
6892 switch (history[0].insn_mo->pinfo2
6893 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
6894 {
6895 case INSN2_BRANCH_DELAY_32BIT:
6896 mips_macro_warning.delay_slot_length = 4;
6897 break;
6898 case INSN2_BRANCH_DELAY_16BIT:
6899 mips_macro_warning.delay_slot_length = 2;
6900 break;
6901 default:
6902 mips_macro_warning.delay_slot_length = 0;
6903 break;
6904 }
6905 mips_macro_warning.first_frag = NULL;
584892a6
RS
6906}
6907
df58fc94
RS
6908/* Given that a macro is longer than one instruction or of the wrong size,
6909 return the appropriate warning for it. Return null if no warning is
6910 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
6911 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
6912 and RELAX_NOMACRO. */
584892a6
RS
6913
6914static const char *
6915macro_warning (relax_substateT subtype)
6916{
6917 if (subtype & RELAX_DELAY_SLOT)
6918 return _("Macro instruction expanded into multiple instructions"
6919 " in a branch delay slot");
6920 else if (subtype & RELAX_NOMACRO)
6921 return _("Macro instruction expanded into multiple instructions");
df58fc94
RS
6922 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
6923 | RELAX_DELAY_SLOT_SIZE_SECOND))
6924 return ((subtype & RELAX_DELAY_SLOT_16BIT)
6925 ? _("Macro instruction expanded into a wrong size instruction"
6926 " in a 16-bit branch delay slot")
6927 : _("Macro instruction expanded into a wrong size instruction"
6928 " in a 32-bit branch delay slot"));
584892a6
RS
6929 else
6930 return 0;
6931}
6932
6933/* Finish up a macro. Emit warnings as appropriate. */
6934
6935static void
6936macro_end (void)
6937{
df58fc94
RS
6938 /* Relaxation warning flags. */
6939 relax_substateT subtype = 0;
6940
6941 /* Check delay slot size requirements. */
6942 if (mips_macro_warning.delay_slot_length == 2)
6943 subtype |= RELAX_DELAY_SLOT_16BIT;
6944 if (mips_macro_warning.delay_slot_length != 0)
584892a6 6945 {
df58fc94
RS
6946 if (mips_macro_warning.delay_slot_length
6947 != mips_macro_warning.first_insn_sizes[0])
6948 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
6949 if (mips_macro_warning.delay_slot_length
6950 != mips_macro_warning.first_insn_sizes[1])
6951 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
6952 }
584892a6 6953
df58fc94
RS
6954 /* Check instruction count requirements. */
6955 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
6956 {
6957 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
584892a6
RS
6958 subtype |= RELAX_SECOND_LONGER;
6959 if (mips_opts.warn_about_macros)
6960 subtype |= RELAX_NOMACRO;
6961 if (mips_macro_warning.delay_slot_p)
6962 subtype |= RELAX_DELAY_SLOT;
df58fc94 6963 }
584892a6 6964
df58fc94
RS
6965 /* If both alternatives fail to fill a delay slot correctly,
6966 emit the warning now. */
6967 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
6968 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
6969 {
6970 relax_substateT s;
6971 const char *msg;
6972
6973 s = subtype & (RELAX_DELAY_SLOT_16BIT
6974 | RELAX_DELAY_SLOT_SIZE_FIRST
6975 | RELAX_DELAY_SLOT_SIZE_SECOND);
6976 msg = macro_warning (s);
6977 if (msg != NULL)
6978 as_warn ("%s", msg);
6979 subtype &= ~s;
6980 }
6981
6982 /* If both implementations are longer than 1 instruction, then emit the
6983 warning now. */
6984 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
6985 {
6986 relax_substateT s;
6987 const char *msg;
6988
6989 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
6990 msg = macro_warning (s);
6991 if (msg != NULL)
6992 as_warn ("%s", msg);
6993 subtype &= ~s;
584892a6 6994 }
df58fc94
RS
6995
6996 /* If any flags still set, then one implementation might need a warning
6997 and the other either will need one of a different kind or none at all.
6998 Pass any remaining flags over to relaxation. */
6999 if (mips_macro_warning.first_frag != NULL)
7000 mips_macro_warning.first_frag->fr_subtype |= subtype;
584892a6
RS
7001}
7002
df58fc94
RS
7003/* Instruction operand formats used in macros that vary between
7004 standard MIPS and microMIPS code. */
7005
833794fc 7006static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
df58fc94
RS
7007static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
7008static const char * const jalr_fmt[2] = { "d,s", "t,s" };
7009static const char * const lui_fmt[2] = { "t,u", "s,u" };
7010static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
833794fc 7011static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
df58fc94
RS
7012static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
7013static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
7014
833794fc 7015#define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
df58fc94
RS
7016#define COP12_FMT (cop12_fmt[mips_opts.micromips])
7017#define JALR_FMT (jalr_fmt[mips_opts.micromips])
7018#define LUI_FMT (lui_fmt[mips_opts.micromips])
7019#define MEM12_FMT (mem12_fmt[mips_opts.micromips])
833794fc 7020#define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
df58fc94
RS
7021#define SHFT_FMT (shft_fmt[mips_opts.micromips])
7022#define TRAP_FMT (trap_fmt[mips_opts.micromips])
7023
6e1304d8
RS
7024/* Read a macro's relocation codes from *ARGS and store them in *R.
7025 The first argument in *ARGS will be either the code for a single
7026 relocation or -1 followed by the three codes that make up a
7027 composite relocation. */
7028
7029static void
7030macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
7031{
7032 int i, next;
7033
7034 next = va_arg (*args, int);
7035 if (next >= 0)
7036 r[0] = (bfd_reloc_code_real_type) next;
7037 else
f2ae14a1
RS
7038 {
7039 for (i = 0; i < 3; i++)
7040 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
7041 /* This function is only used for 16-bit relocation fields.
7042 To make the macro code simpler, treat an unrelocated value
7043 in the same way as BFD_RELOC_LO16. */
7044 if (r[0] == BFD_RELOC_UNUSED)
7045 r[0] = BFD_RELOC_LO16;
7046 }
6e1304d8
RS
7047}
7048
252b5132
RH
7049/* Build an instruction created by a macro expansion. This is passed
7050 a pointer to the count of instructions created so far, an
7051 expression, the name of the instruction to build, an operand format
7052 string, and corresponding arguments. */
7053
252b5132 7054static void
67c0d1eb 7055macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 7056{
df58fc94 7057 const struct mips_opcode *mo = NULL;
f6688943 7058 bfd_reloc_code_real_type r[3];
df58fc94 7059 const struct mips_opcode *amo;
e077a1c8 7060 const struct mips_operand *operand;
df58fc94
RS
7061 struct hash_control *hash;
7062 struct mips_cl_insn insn;
252b5132 7063 va_list args;
e077a1c8 7064 unsigned int uval;
252b5132 7065
252b5132 7066 va_start (args, fmt);
252b5132 7067
252b5132
RH
7068 if (mips_opts.mips16)
7069 {
03ea81db 7070 mips16_macro_build (ep, name, fmt, &args);
252b5132
RH
7071 va_end (args);
7072 return;
7073 }
7074
f6688943
TS
7075 r[0] = BFD_RELOC_UNUSED;
7076 r[1] = BFD_RELOC_UNUSED;
7077 r[2] = BFD_RELOC_UNUSED;
df58fc94
RS
7078 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
7079 amo = (struct mips_opcode *) hash_find (hash, name);
7080 gas_assert (amo);
7081 gas_assert (strcmp (name, amo->name) == 0);
1e915849 7082
df58fc94 7083 do
8b082fb1
TS
7084 {
7085 /* Search until we get a match for NAME. It is assumed here that
df58fc94
RS
7086 macros will never generate MDMX, MIPS-3D, or MT instructions.
7087 We try to match an instruction that fulfils the branch delay
7088 slot instruction length requirement (if any) of the previous
7089 instruction. While doing this we record the first instruction
7090 seen that matches all the other conditions and use it anyway
7091 if the requirement cannot be met; we will issue an appropriate
7092 warning later on. */
7093 if (strcmp (fmt, amo->args) == 0
7094 && amo->pinfo != INSN_MACRO
7095 && is_opcode_valid (amo)
7096 && is_size_valid (amo))
7097 {
7098 if (is_delay_slot_valid (amo))
7099 {
7100 mo = amo;
7101 break;
7102 }
7103 else if (!mo)
7104 mo = amo;
7105 }
8b082fb1 7106
df58fc94
RS
7107 ++amo;
7108 gas_assert (amo->name);
252b5132 7109 }
df58fc94 7110 while (strcmp (name, amo->name) == 0);
252b5132 7111
df58fc94 7112 gas_assert (mo);
1e915849 7113 create_insn (&insn, mo);
e077a1c8 7114 for (; *fmt; ++fmt)
252b5132 7115 {
e077a1c8 7116 switch (*fmt)
252b5132 7117 {
252b5132
RH
7118 case ',':
7119 case '(':
7120 case ')':
252b5132 7121 case 'z':
e077a1c8 7122 break;
252b5132
RH
7123
7124 case 'i':
7125 case 'j':
6e1304d8 7126 macro_read_relocs (&args, r);
9c2799c2 7127 gas_assert (*r == BFD_RELOC_GPREL16
e391c024
RS
7128 || *r == BFD_RELOC_MIPS_HIGHER
7129 || *r == BFD_RELOC_HI16_S
7130 || *r == BFD_RELOC_LO16
7131 || *r == BFD_RELOC_MIPS_GOT_OFST);
e077a1c8 7132 break;
e391c024
RS
7133
7134 case 'o':
7135 macro_read_relocs (&args, r);
e077a1c8 7136 break;
252b5132
RH
7137
7138 case 'u':
6e1304d8 7139 macro_read_relocs (&args, r);
9c2799c2 7140 gas_assert (ep != NULL
90ecf173
MR
7141 && (ep->X_op == O_constant
7142 || (ep->X_op == O_symbol
7143 && (*r == BFD_RELOC_MIPS_HIGHEST
7144 || *r == BFD_RELOC_HI16_S
7145 || *r == BFD_RELOC_HI16
7146 || *r == BFD_RELOC_GPREL16
7147 || *r == BFD_RELOC_MIPS_GOT_HI16
7148 || *r == BFD_RELOC_MIPS_CALL_HI16))));
e077a1c8 7149 break;
252b5132
RH
7150
7151 case 'p':
9c2799c2 7152 gas_assert (ep != NULL);
bad36eac 7153
252b5132
RH
7154 /*
7155 * This allows macro() to pass an immediate expression for
7156 * creating short branches without creating a symbol.
bad36eac
DJ
7157 *
7158 * We don't allow branch relaxation for these branches, as
7159 * they should only appear in ".set nomacro" anyway.
252b5132
RH
7160 */
7161 if (ep->X_op == O_constant)
7162 {
df58fc94
RS
7163 /* For microMIPS we always use relocations for branches.
7164 So we should not resolve immediate values. */
7165 gas_assert (!mips_opts.micromips);
7166
bad36eac
DJ
7167 if ((ep->X_add_number & 3) != 0)
7168 as_bad (_("branch to misaligned address (0x%lx)"),
7169 (unsigned long) ep->X_add_number);
7170 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
7171 as_bad (_("branch address range overflow (0x%lx)"),
7172 (unsigned long) ep->X_add_number);
252b5132
RH
7173 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
7174 ep = NULL;
7175 }
7176 else
0b25d3e6 7177 *r = BFD_RELOC_16_PCREL_S2;
e077a1c8 7178 break;
252b5132
RH
7179
7180 case 'a':
9c2799c2 7181 gas_assert (ep != NULL);
f6688943 7182 *r = BFD_RELOC_MIPS_JMP;
e077a1c8 7183 break;
d43b4baf 7184
252b5132 7185 default:
e077a1c8
RS
7186 operand = (mips_opts.micromips
7187 ? decode_micromips_operand (fmt)
7188 : decode_mips_operand (fmt));
7189 if (!operand)
7190 abort ();
7191
7192 uval = va_arg (args, int);
7193 if (operand->type == OP_CLO_CLZ_DEST)
7194 uval |= (uval << 5);
7195 insn_insert_operand (&insn, operand, uval);
7196
7197 if (*fmt == '+' || *fmt == 'm')
7198 ++fmt;
7199 break;
252b5132 7200 }
252b5132
RH
7201 }
7202 va_end (args);
9c2799c2 7203 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 7204
df58fc94 7205 append_insn (&insn, ep, r, TRUE);
252b5132
RH
7206}
7207
7208static void
67c0d1eb 7209mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
03ea81db 7210 va_list *args)
252b5132 7211{
1e915849 7212 struct mips_opcode *mo;
252b5132 7213 struct mips_cl_insn insn;
e077a1c8 7214 const struct mips_operand *operand;
f6688943
TS
7215 bfd_reloc_code_real_type r[3]
7216 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 7217
1e915849 7218 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
9c2799c2
NC
7219 gas_assert (mo);
7220 gas_assert (strcmp (name, mo->name) == 0);
252b5132 7221
1e915849 7222 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 7223 {
1e915849 7224 ++mo;
9c2799c2
NC
7225 gas_assert (mo->name);
7226 gas_assert (strcmp (name, mo->name) == 0);
252b5132
RH
7227 }
7228
1e915849 7229 create_insn (&insn, mo);
e077a1c8 7230 for (; *fmt; ++fmt)
252b5132
RH
7231 {
7232 int c;
7233
e077a1c8 7234 c = *fmt;
252b5132
RH
7235 switch (c)
7236 {
252b5132
RH
7237 case ',':
7238 case '(':
7239 case ')':
e077a1c8 7240 break;
252b5132
RH
7241
7242 case '0':
7243 case 'S':
7244 case 'P':
7245 case 'R':
e077a1c8 7246 break;
252b5132
RH
7247
7248 case '<':
7249 case '>':
7250 case '4':
7251 case '5':
7252 case 'H':
7253 case 'W':
7254 case 'D':
7255 case 'j':
7256 case '8':
7257 case 'V':
7258 case 'C':
7259 case 'U':
7260 case 'k':
7261 case 'K':
7262 case 'p':
7263 case 'q':
7264 {
b886a2ab
RS
7265 offsetT value;
7266
9c2799c2 7267 gas_assert (ep != NULL);
252b5132
RH
7268
7269 if (ep->X_op != O_constant)
874e8986 7270 *r = (int) BFD_RELOC_UNUSED + c;
b886a2ab 7271 else if (calculate_reloc (*r, ep->X_add_number, &value))
252b5132 7272 {
b886a2ab 7273 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
252b5132 7274 ep = NULL;
f6688943 7275 *r = BFD_RELOC_UNUSED;
252b5132
RH
7276 }
7277 }
e077a1c8 7278 break;
252b5132 7279
e077a1c8
RS
7280 default:
7281 operand = decode_mips16_operand (c, FALSE);
7282 if (!operand)
7283 abort ();
252b5132 7284
4a06e5a2 7285 insn_insert_operand (&insn, operand, va_arg (*args, int));
e077a1c8
RS
7286 break;
7287 }
252b5132
RH
7288 }
7289
9c2799c2 7290 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 7291
df58fc94 7292 append_insn (&insn, ep, r, TRUE);
252b5132
RH
7293}
7294
2051e8c4
MR
7295/*
7296 * Sign-extend 32-bit mode constants that have bit 31 set and all
7297 * higher bits unset.
7298 */
9f872bbe 7299static void
2051e8c4
MR
7300normalize_constant_expr (expressionS *ex)
7301{
9ee2a2d4 7302 if (ex->X_op == O_constant
2051e8c4
MR
7303 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7304 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7305 - 0x80000000);
7306}
7307
7308/*
7309 * Sign-extend 32-bit mode address offsets that have bit 31 set and
7310 * all higher bits unset.
7311 */
7312static void
7313normalize_address_expr (expressionS *ex)
7314{
7315 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
7316 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
7317 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7318 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7319 - 0x80000000);
7320}
7321
438c16b8
TS
7322/*
7323 * Generate a "jalr" instruction with a relocation hint to the called
7324 * function. This occurs in NewABI PIC code.
7325 */
7326static void
df58fc94 7327macro_build_jalr (expressionS *ep, int cprestore)
438c16b8 7328{
df58fc94
RS
7329 static const bfd_reloc_code_real_type jalr_relocs[2]
7330 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
7331 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
7332 const char *jalr;
685736be 7333 char *f = NULL;
b34976b6 7334
1180b5a4 7335 if (MIPS_JALR_HINT_P (ep))
f21f8242 7336 {
cc3d92a5 7337 frag_grow (8);
f21f8242
AO
7338 f = frag_more (0);
7339 }
2906b037 7340 if (mips_opts.micromips)
df58fc94 7341 {
833794fc
MR
7342 jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
7343 ? "jalr" : "jalrs");
e64af278 7344 if (MIPS_JALR_HINT_P (ep)
833794fc 7345 || mips_opts.insn32
e64af278 7346 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
7347 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
7348 else
7349 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
7350 }
2906b037
MR
7351 else
7352 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
1180b5a4 7353 if (MIPS_JALR_HINT_P (ep))
df58fc94 7354 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
438c16b8
TS
7355}
7356
252b5132
RH
7357/*
7358 * Generate a "lui" instruction.
7359 */
7360static void
67c0d1eb 7361macro_build_lui (expressionS *ep, int regnum)
252b5132 7362{
9c2799c2 7363 gas_assert (! mips_opts.mips16);
252b5132 7364
df58fc94 7365 if (ep->X_op != O_constant)
252b5132 7366 {
9c2799c2 7367 gas_assert (ep->X_op == O_symbol);
bbe506e8
TS
7368 /* _gp_disp is a special case, used from s_cpload.
7369 __gnu_local_gp is used if mips_no_shared. */
9c2799c2 7370 gas_assert (mips_pic == NO_PIC
78e1bb40 7371 || (! HAVE_NEWABI
aa6975fb
ILT
7372 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
7373 || (! mips_in_shared
bbe506e8
TS
7374 && strcmp (S_GET_NAME (ep->X_add_symbol),
7375 "__gnu_local_gp") == 0));
252b5132
RH
7376 }
7377
df58fc94 7378 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
252b5132
RH
7379}
7380
885add95
CD
7381/* Generate a sequence of instructions to do a load or store from a constant
7382 offset off of a base register (breg) into/from a target register (treg),
7383 using AT if necessary. */
7384static void
67c0d1eb
RS
7385macro_build_ldst_constoffset (expressionS *ep, const char *op,
7386 int treg, int breg, int dbl)
885add95 7387{
9c2799c2 7388 gas_assert (ep->X_op == O_constant);
885add95 7389
256ab948 7390 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
7391 if (!dbl)
7392 normalize_constant_expr (ep);
256ab948 7393
67c1ffbe 7394 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 7395 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
7396 as_warn (_("operand overflow"));
7397
7398 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
7399 {
7400 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 7401 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
7402 }
7403 else
7404 {
7405 /* 32-bit offset, need multiple instructions and AT, like:
7406 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
7407 addu $tempreg,$tempreg,$breg
7408 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
7409 to handle the complete offset. */
67c0d1eb
RS
7410 macro_build_lui (ep, AT);
7411 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7412 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95 7413
741fe287 7414 if (!mips_opts.at)
8fc2e39e 7415 as_bad (_("Macro used $at after \".set noat\""));
885add95
CD
7416 }
7417}
7418
252b5132
RH
7419/* set_at()
7420 * Generates code to set the $at register to true (one)
7421 * if reg is less than the immediate expression.
7422 */
7423static void
67c0d1eb 7424set_at (int reg, int unsignedp)
252b5132
RH
7425{
7426 if (imm_expr.X_op == O_constant
7427 && imm_expr.X_add_number >= -0x8000
7428 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
7429 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
7430 AT, reg, BFD_RELOC_LO16);
252b5132
RH
7431 else
7432 {
67c0d1eb
RS
7433 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7434 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
7435 }
7436}
7437
252b5132
RH
7438/* Count the leading zeroes by performing a binary chop. This is a
7439 bulky bit of source, but performance is a LOT better for the
7440 majority of values than a simple loop to count the bits:
7441 for (lcnt = 0; (lcnt < 32); lcnt++)
7442 if ((v) & (1 << (31 - lcnt)))
7443 break;
7444 However it is not code size friendly, and the gain will drop a bit
7445 on certain cached systems.
7446*/
7447#define COUNT_TOP_ZEROES(v) \
7448 (((v) & ~0xffff) == 0 \
7449 ? ((v) & ~0xff) == 0 \
7450 ? ((v) & ~0xf) == 0 \
7451 ? ((v) & ~0x3) == 0 \
7452 ? ((v) & ~0x1) == 0 \
7453 ? !(v) \
7454 ? 32 \
7455 : 31 \
7456 : 30 \
7457 : ((v) & ~0x7) == 0 \
7458 ? 29 \
7459 : 28 \
7460 : ((v) & ~0x3f) == 0 \
7461 ? ((v) & ~0x1f) == 0 \
7462 ? 27 \
7463 : 26 \
7464 : ((v) & ~0x7f) == 0 \
7465 ? 25 \
7466 : 24 \
7467 : ((v) & ~0xfff) == 0 \
7468 ? ((v) & ~0x3ff) == 0 \
7469 ? ((v) & ~0x1ff) == 0 \
7470 ? 23 \
7471 : 22 \
7472 : ((v) & ~0x7ff) == 0 \
7473 ? 21 \
7474 : 20 \
7475 : ((v) & ~0x3fff) == 0 \
7476 ? ((v) & ~0x1fff) == 0 \
7477 ? 19 \
7478 : 18 \
7479 : ((v) & ~0x7fff) == 0 \
7480 ? 17 \
7481 : 16 \
7482 : ((v) & ~0xffffff) == 0 \
7483 ? ((v) & ~0xfffff) == 0 \
7484 ? ((v) & ~0x3ffff) == 0 \
7485 ? ((v) & ~0x1ffff) == 0 \
7486 ? 15 \
7487 : 14 \
7488 : ((v) & ~0x7ffff) == 0 \
7489 ? 13 \
7490 : 12 \
7491 : ((v) & ~0x3fffff) == 0 \
7492 ? ((v) & ~0x1fffff) == 0 \
7493 ? 11 \
7494 : 10 \
7495 : ((v) & ~0x7fffff) == 0 \
7496 ? 9 \
7497 : 8 \
7498 : ((v) & ~0xfffffff) == 0 \
7499 ? ((v) & ~0x3ffffff) == 0 \
7500 ? ((v) & ~0x1ffffff) == 0 \
7501 ? 7 \
7502 : 6 \
7503 : ((v) & ~0x7ffffff) == 0 \
7504 ? 5 \
7505 : 4 \
7506 : ((v) & ~0x3fffffff) == 0 \
7507 ? ((v) & ~0x1fffffff) == 0 \
7508 ? 3 \
7509 : 2 \
7510 : ((v) & ~0x7fffffff) == 0 \
7511 ? 1 \
7512 : 0)
7513
7514/* load_register()
67c1ffbe 7515 * This routine generates the least number of instructions necessary to load
252b5132
RH
7516 * an absolute expression value into a register.
7517 */
7518static void
67c0d1eb 7519load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
7520{
7521 int freg;
7522 expressionS hi32, lo32;
7523
7524 if (ep->X_op != O_big)
7525 {
9c2799c2 7526 gas_assert (ep->X_op == O_constant);
256ab948
TS
7527
7528 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
7529 if (!dbl)
7530 normalize_constant_expr (ep);
256ab948
TS
7531
7532 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
7533 {
7534 /* We can handle 16 bit signed values with an addiu to
7535 $zero. No need to ever use daddiu here, since $zero and
7536 the result are always correct in 32 bit mode. */
67c0d1eb 7537 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
7538 return;
7539 }
7540 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
7541 {
7542 /* We can handle 16 bit unsigned values with an ori to
7543 $zero. */
67c0d1eb 7544 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
7545 return;
7546 }
256ab948 7547 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
7548 {
7549 /* 32 bit values require an lui. */
df58fc94 7550 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 7551 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 7552 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
7553 return;
7554 }
7555 }
7556
7557 /* The value is larger than 32 bits. */
7558
2051e8c4 7559 if (!dbl || HAVE_32BIT_GPRS)
252b5132 7560 {
55e08f71
NC
7561 char value[32];
7562
7563 sprintf_vma (value, ep->X_add_number);
20e1fcfd 7564 as_bad (_("Number (0x%s) larger than 32 bits"), value);
67c0d1eb 7565 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
7566 return;
7567 }
7568
7569 if (ep->X_op != O_big)
7570 {
7571 hi32 = *ep;
7572 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
7573 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
7574 hi32.X_add_number &= 0xffffffff;
7575 lo32 = *ep;
7576 lo32.X_add_number &= 0xffffffff;
7577 }
7578 else
7579 {
9c2799c2 7580 gas_assert (ep->X_add_number > 2);
252b5132
RH
7581 if (ep->X_add_number == 3)
7582 generic_bignum[3] = 0;
7583 else if (ep->X_add_number > 4)
7584 as_bad (_("Number larger than 64 bits"));
7585 lo32.X_op = O_constant;
7586 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
7587 hi32.X_op = O_constant;
7588 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
7589 }
7590
7591 if (hi32.X_add_number == 0)
7592 freg = 0;
7593 else
7594 {
7595 int shift, bit;
7596 unsigned long hi, lo;
7597
956cd1d6 7598 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
7599 {
7600 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
7601 {
67c0d1eb 7602 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
7603 return;
7604 }
7605 if (lo32.X_add_number & 0x80000000)
7606 {
df58fc94 7607 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 7608 if (lo32.X_add_number & 0xffff)
67c0d1eb 7609 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
7610 return;
7611 }
7612 }
252b5132
RH
7613
7614 /* Check for 16bit shifted constant. We know that hi32 is
7615 non-zero, so start the mask on the first bit of the hi32
7616 value. */
7617 shift = 17;
7618 do
beae10d5
KH
7619 {
7620 unsigned long himask, lomask;
7621
7622 if (shift < 32)
7623 {
7624 himask = 0xffff >> (32 - shift);
7625 lomask = (0xffff << shift) & 0xffffffff;
7626 }
7627 else
7628 {
7629 himask = 0xffff << (shift - 32);
7630 lomask = 0;
7631 }
7632 if ((hi32.X_add_number & ~(offsetT) himask) == 0
7633 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
7634 {
7635 expressionS tmp;
7636
7637 tmp.X_op = O_constant;
7638 if (shift < 32)
7639 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
7640 | (lo32.X_add_number >> shift));
7641 else
7642 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb 7643 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
df58fc94 7644 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 7645 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
7646 return;
7647 }
f9419b05 7648 ++shift;
beae10d5
KH
7649 }
7650 while (shift <= (64 - 16));
252b5132
RH
7651
7652 /* Find the bit number of the lowest one bit, and store the
7653 shifted value in hi/lo. */
7654 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
7655 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
7656 if (lo != 0)
7657 {
7658 bit = 0;
7659 while ((lo & 1) == 0)
7660 {
7661 lo >>= 1;
7662 ++bit;
7663 }
7664 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
7665 hi >>= bit;
7666 }
7667 else
7668 {
7669 bit = 32;
7670 while ((hi & 1) == 0)
7671 {
7672 hi >>= 1;
7673 ++bit;
7674 }
7675 lo = hi;
7676 hi = 0;
7677 }
7678
7679 /* Optimize if the shifted value is a (power of 2) - 1. */
7680 if ((hi == 0 && ((lo + 1) & lo) == 0)
7681 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
7682 {
7683 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 7684 if (shift != 0)
beae10d5 7685 {
252b5132
RH
7686 expressionS tmp;
7687
7688 /* This instruction will set the register to be all
7689 ones. */
beae10d5
KH
7690 tmp.X_op = O_constant;
7691 tmp.X_add_number = (offsetT) -1;
67c0d1eb 7692 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
7693 if (bit != 0)
7694 {
7695 bit += shift;
df58fc94 7696 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 7697 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 7698 }
df58fc94 7699 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
67c0d1eb 7700 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
7701 return;
7702 }
7703 }
252b5132
RH
7704
7705 /* Sign extend hi32 before calling load_register, because we can
7706 generally get better code when we load a sign extended value. */
7707 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 7708 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 7709 load_register (reg, &hi32, 0);
252b5132
RH
7710 freg = reg;
7711 }
7712 if ((lo32.X_add_number & 0xffff0000) == 0)
7713 {
7714 if (freg != 0)
7715 {
df58fc94 7716 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
252b5132
RH
7717 freg = reg;
7718 }
7719 }
7720 else
7721 {
7722 expressionS mid16;
7723
956cd1d6 7724 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 7725 {
df58fc94
RS
7726 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
7727 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
beae10d5
KH
7728 return;
7729 }
252b5132
RH
7730
7731 if (freg != 0)
7732 {
df58fc94 7733 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
252b5132
RH
7734 freg = reg;
7735 }
7736 mid16 = lo32;
7737 mid16.X_add_number >>= 16;
67c0d1eb 7738 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
df58fc94 7739 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
252b5132
RH
7740 freg = reg;
7741 }
7742 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 7743 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
7744}
7745
269137b2
TS
7746static inline void
7747load_delay_nop (void)
7748{
7749 if (!gpr_interlocks)
7750 macro_build (NULL, "nop", "");
7751}
7752
252b5132
RH
7753/* Load an address into a register. */
7754
7755static void
67c0d1eb 7756load_address (int reg, expressionS *ep, int *used_at)
252b5132 7757{
252b5132
RH
7758 if (ep->X_op != O_constant
7759 && ep->X_op != O_symbol)
7760 {
7761 as_bad (_("expression too complex"));
7762 ep->X_op = O_constant;
7763 }
7764
7765 if (ep->X_op == O_constant)
7766 {
67c0d1eb 7767 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
7768 return;
7769 }
7770
7771 if (mips_pic == NO_PIC)
7772 {
7773 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 7774 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
7775 Otherwise we want
7776 lui $reg,<sym> (BFD_RELOC_HI16_S)
7777 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 7778 If we have an addend, we always use the latter form.
76b3015f 7779
d6bc6245
TS
7780 With 64bit address space and a usable $at we want
7781 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
7782 lui $at,<sym> (BFD_RELOC_HI16_S)
7783 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
7784 daddiu $at,<sym> (BFD_RELOC_LO16)
7785 dsll32 $reg,0
3a482fd5 7786 daddu $reg,$reg,$at
76b3015f 7787
c03099e6 7788 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
7789 on superscalar processors.
7790 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
7791 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
7792 dsll $reg,16
7793 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
7794 dsll $reg,16
7795 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
7796
7797 For GP relative symbols in 64bit address space we can use
7798 the same sequence as in 32bit address space. */
aed1a261 7799 if (HAVE_64BIT_SYMBOLS)
d6bc6245 7800 {
6caf9ef4
TS
7801 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
7802 && !nopic_need_relax (ep->X_add_symbol, 1))
7803 {
7804 relax_start (ep->X_add_symbol);
7805 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
7806 mips_gp_register, BFD_RELOC_GPREL16);
7807 relax_switch ();
7808 }
d6bc6245 7809
741fe287 7810 if (*used_at == 0 && mips_opts.at)
d6bc6245 7811 {
df58fc94
RS
7812 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
7813 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
67c0d1eb
RS
7814 macro_build (ep, "daddiu", "t,r,j", reg, reg,
7815 BFD_RELOC_MIPS_HIGHER);
7816 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
df58fc94 7817 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
67c0d1eb 7818 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
7819 *used_at = 1;
7820 }
7821 else
7822 {
df58fc94 7823 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb
RS
7824 macro_build (ep, "daddiu", "t,r,j", reg, reg,
7825 BFD_RELOC_MIPS_HIGHER);
df58fc94 7826 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 7827 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
df58fc94 7828 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 7829 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 7830 }
6caf9ef4
TS
7831
7832 if (mips_relax.sequence)
7833 relax_end ();
d6bc6245 7834 }
252b5132
RH
7835 else
7836 {
d6bc6245 7837 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 7838 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 7839 {
4d7206a2 7840 relax_start (ep->X_add_symbol);
67c0d1eb 7841 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 7842 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 7843 relax_switch ();
d6bc6245 7844 }
67c0d1eb
RS
7845 macro_build_lui (ep, reg);
7846 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
7847 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
7848 if (mips_relax.sequence)
7849 relax_end ();
d6bc6245 7850 }
252b5132 7851 }
0a44bf69 7852 else if (!mips_big_got)
252b5132
RH
7853 {
7854 expressionS ex;
7855
7856 /* If this is a reference to an external symbol, we want
7857 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7858 Otherwise we want
7859 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7860 nop
7861 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
7862 If there is a constant, it must be added in after.
7863
ed6fb7bd 7864 If we have NewABI, we want
f5040a92
AO
7865 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
7866 unless we're referencing a global symbol with a non-zero
7867 offset, in which case cst must be added separately. */
ed6fb7bd
SC
7868 if (HAVE_NEWABI)
7869 {
f5040a92
AO
7870 if (ep->X_add_number)
7871 {
4d7206a2 7872 ex.X_add_number = ep->X_add_number;
f5040a92 7873 ep->X_add_number = 0;
4d7206a2 7874 relax_start (ep->X_add_symbol);
67c0d1eb
RS
7875 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
7876 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
7877 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
7878 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
7879 ex.X_op = O_constant;
67c0d1eb 7880 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 7881 reg, reg, BFD_RELOC_LO16);
f5040a92 7882 ep->X_add_number = ex.X_add_number;
4d7206a2 7883 relax_switch ();
f5040a92 7884 }
67c0d1eb 7885 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 7886 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
7887 if (mips_relax.sequence)
7888 relax_end ();
ed6fb7bd
SC
7889 }
7890 else
7891 {
f5040a92
AO
7892 ex.X_add_number = ep->X_add_number;
7893 ep->X_add_number = 0;
67c0d1eb
RS
7894 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
7895 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 7896 load_delay_nop ();
4d7206a2
RS
7897 relax_start (ep->X_add_symbol);
7898 relax_switch ();
67c0d1eb 7899 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 7900 BFD_RELOC_LO16);
4d7206a2 7901 relax_end ();
ed6fb7bd 7902
f5040a92
AO
7903 if (ex.X_add_number != 0)
7904 {
7905 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
7906 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
7907 ex.X_op = O_constant;
67c0d1eb 7908 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 7909 reg, reg, BFD_RELOC_LO16);
f5040a92 7910 }
252b5132
RH
7911 }
7912 }
0a44bf69 7913 else if (mips_big_got)
252b5132
RH
7914 {
7915 expressionS ex;
252b5132
RH
7916
7917 /* This is the large GOT case. If this is a reference to an
7918 external symbol, we want
7919 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7920 addu $reg,$reg,$gp
7921 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
7922
7923 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
7924 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7925 nop
7926 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 7927 If there is a constant, it must be added in after.
f5040a92
AO
7928
7929 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
7930 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
7931 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 7932 */
438c16b8
TS
7933 if (HAVE_NEWABI)
7934 {
4d7206a2 7935 ex.X_add_number = ep->X_add_number;
f5040a92 7936 ep->X_add_number = 0;
4d7206a2 7937 relax_start (ep->X_add_symbol);
df58fc94 7938 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
7939 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7940 reg, reg, mips_gp_register);
7941 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
7942 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
7943 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
7944 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
7945 else if (ex.X_add_number)
7946 {
7947 ex.X_op = O_constant;
67c0d1eb
RS
7948 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
7949 BFD_RELOC_LO16);
f5040a92
AO
7950 }
7951
7952 ep->X_add_number = ex.X_add_number;
4d7206a2 7953 relax_switch ();
67c0d1eb 7954 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 7955 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
7956 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
7957 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 7958 relax_end ();
438c16b8 7959 }
252b5132 7960 else
438c16b8 7961 {
f5040a92
AO
7962 ex.X_add_number = ep->X_add_number;
7963 ep->X_add_number = 0;
4d7206a2 7964 relax_start (ep->X_add_symbol);
df58fc94 7965 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
7966 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7967 reg, reg, mips_gp_register);
7968 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
7969 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
7970 relax_switch ();
7971 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
7972 {
7973 /* We need a nop before loading from $gp. This special
7974 check is required because the lui which starts the main
7975 instruction stream does not refer to $gp, and so will not
7976 insert the nop which may be required. */
67c0d1eb 7977 macro_build (NULL, "nop", "");
438c16b8 7978 }
67c0d1eb 7979 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 7980 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 7981 load_delay_nop ();
67c0d1eb 7982 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 7983 BFD_RELOC_LO16);
4d7206a2 7984 relax_end ();
438c16b8 7985
f5040a92
AO
7986 if (ex.X_add_number != 0)
7987 {
7988 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
7989 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
7990 ex.X_op = O_constant;
67c0d1eb
RS
7991 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
7992 BFD_RELOC_LO16);
f5040a92 7993 }
252b5132
RH
7994 }
7995 }
252b5132
RH
7996 else
7997 abort ();
8fc2e39e 7998
741fe287 7999 if (!mips_opts.at && *used_at == 1)
8fc2e39e 8000 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
8001}
8002
ea1fb5dc
RS
8003/* Move the contents of register SOURCE into register DEST. */
8004
8005static void
67c0d1eb 8006move_register (int dest, int source)
ea1fb5dc 8007{
df58fc94
RS
8008 /* Prefer to use a 16-bit microMIPS instruction unless the previous
8009 instruction specifically requires a 32-bit one. */
8010 if (mips_opts.micromips
833794fc 8011 && !mips_opts.insn32
df58fc94 8012 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7951ca42 8013 macro_build (NULL, "move", "mp,mj", dest, source);
df58fc94
RS
8014 else
8015 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
8016 dest, source, 0);
ea1fb5dc
RS
8017}
8018
4d7206a2 8019/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
8020 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
8021 The two alternatives are:
4d7206a2
RS
8022
8023 Global symbol Local sybmol
8024 ------------- ------------
8025 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
8026 ... ...
8027 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
8028
8029 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
8030 emits the second for a 16-bit offset or add_got_offset_hilo emits
8031 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
8032
8033static void
67c0d1eb 8034load_got_offset (int dest, expressionS *local)
4d7206a2
RS
8035{
8036 expressionS global;
8037
8038 global = *local;
8039 global.X_add_number = 0;
8040
8041 relax_start (local->X_add_symbol);
67c0d1eb
RS
8042 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
8043 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 8044 relax_switch ();
67c0d1eb
RS
8045 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
8046 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
8047 relax_end ();
8048}
8049
8050static void
67c0d1eb 8051add_got_offset (int dest, expressionS *local)
4d7206a2
RS
8052{
8053 expressionS global;
8054
8055 global.X_op = O_constant;
8056 global.X_op_symbol = NULL;
8057 global.X_add_symbol = NULL;
8058 global.X_add_number = local->X_add_number;
8059
8060 relax_start (local->X_add_symbol);
67c0d1eb 8061 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
8062 dest, dest, BFD_RELOC_LO16);
8063 relax_switch ();
67c0d1eb 8064 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
8065 relax_end ();
8066}
8067
f6a22291
MR
8068static void
8069add_got_offset_hilo (int dest, expressionS *local, int tmp)
8070{
8071 expressionS global;
8072 int hold_mips_optimize;
8073
8074 global.X_op = O_constant;
8075 global.X_op_symbol = NULL;
8076 global.X_add_symbol = NULL;
8077 global.X_add_number = local->X_add_number;
8078
8079 relax_start (local->X_add_symbol);
8080 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
8081 relax_switch ();
8082 /* Set mips_optimize around the lui instruction to avoid
8083 inserting an unnecessary nop after the lw. */
8084 hold_mips_optimize = mips_optimize;
8085 mips_optimize = 2;
8086 macro_build_lui (&global, tmp);
8087 mips_optimize = hold_mips_optimize;
8088 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
8089 relax_end ();
8090
8091 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
8092}
8093
df58fc94
RS
8094/* Emit a sequence of instructions to emulate a branch likely operation.
8095 BR is an ordinary branch corresponding to one to be emulated. BRNEG
8096 is its complementing branch with the original condition negated.
8097 CALL is set if the original branch specified the link operation.
8098 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
8099
8100 Code like this is produced in the noreorder mode:
8101
8102 BRNEG <args>, 1f
8103 nop
8104 b <sym>
8105 delay slot (executed only if branch taken)
8106 1:
8107
8108 or, if CALL is set:
8109
8110 BRNEG <args>, 1f
8111 nop
8112 bal <sym>
8113 delay slot (executed only if branch taken)
8114 1:
8115
8116 In the reorder mode the delay slot would be filled with a nop anyway,
8117 so code produced is simply:
8118
8119 BR <args>, <sym>
8120 nop
8121
8122 This function is used when producing code for the microMIPS ASE that
8123 does not implement branch likely instructions in hardware. */
8124
8125static void
8126macro_build_branch_likely (const char *br, const char *brneg,
8127 int call, expressionS *ep, const char *fmt,
8128 unsigned int sreg, unsigned int treg)
8129{
8130 int noreorder = mips_opts.noreorder;
8131 expressionS expr1;
8132
8133 gas_assert (mips_opts.micromips);
8134 start_noreorder ();
8135 if (noreorder)
8136 {
8137 micromips_label_expr (&expr1);
8138 macro_build (&expr1, brneg, fmt, sreg, treg);
8139 macro_build (NULL, "nop", "");
8140 macro_build (ep, call ? "bal" : "b", "p");
8141
8142 /* Set to true so that append_insn adds a label. */
8143 emit_branch_likely_macro = TRUE;
8144 }
8145 else
8146 {
8147 macro_build (ep, br, fmt, sreg, treg);
8148 macro_build (NULL, "nop", "");
8149 }
8150 end_noreorder ();
8151}
8152
8153/* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
8154 the condition code tested. EP specifies the branch target. */
8155
8156static void
8157macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
8158{
8159 const int call = 0;
8160 const char *brneg;
8161 const char *br;
8162
8163 switch (type)
8164 {
8165 case M_BC1FL:
8166 br = "bc1f";
8167 brneg = "bc1t";
8168 break;
8169 case M_BC1TL:
8170 br = "bc1t";
8171 brneg = "bc1f";
8172 break;
8173 case M_BC2FL:
8174 br = "bc2f";
8175 brneg = "bc2t";
8176 break;
8177 case M_BC2TL:
8178 br = "bc2t";
8179 brneg = "bc2f";
8180 break;
8181 default:
8182 abort ();
8183 }
8184 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
8185}
8186
8187/* Emit a two-argument branch macro specified by TYPE, using SREG as
8188 the register tested. EP specifies the branch target. */
8189
8190static void
8191macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
8192{
8193 const char *brneg = NULL;
8194 const char *br;
8195 int call = 0;
8196
8197 switch (type)
8198 {
8199 case M_BGEZ:
8200 br = "bgez";
8201 break;
8202 case M_BGEZL:
8203 br = mips_opts.micromips ? "bgez" : "bgezl";
8204 brneg = "bltz";
8205 break;
8206 case M_BGEZALL:
8207 gas_assert (mips_opts.micromips);
833794fc 8208 br = mips_opts.insn32 ? "bgezal" : "bgezals";
df58fc94
RS
8209 brneg = "bltz";
8210 call = 1;
8211 break;
8212 case M_BGTZ:
8213 br = "bgtz";
8214 break;
8215 case M_BGTZL:
8216 br = mips_opts.micromips ? "bgtz" : "bgtzl";
8217 brneg = "blez";
8218 break;
8219 case M_BLEZ:
8220 br = "blez";
8221 break;
8222 case M_BLEZL:
8223 br = mips_opts.micromips ? "blez" : "blezl";
8224 brneg = "bgtz";
8225 break;
8226 case M_BLTZ:
8227 br = "bltz";
8228 break;
8229 case M_BLTZL:
8230 br = mips_opts.micromips ? "bltz" : "bltzl";
8231 brneg = "bgez";
8232 break;
8233 case M_BLTZALL:
8234 gas_assert (mips_opts.micromips);
833794fc 8235 br = mips_opts.insn32 ? "bltzal" : "bltzals";
df58fc94
RS
8236 brneg = "bgez";
8237 call = 1;
8238 break;
8239 default:
8240 abort ();
8241 }
8242 if (mips_opts.micromips && brneg)
8243 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
8244 else
8245 macro_build (ep, br, "s,p", sreg);
8246}
8247
8248/* Emit a three-argument branch macro specified by TYPE, using SREG and
8249 TREG as the registers tested. EP specifies the branch target. */
8250
8251static void
8252macro_build_branch_rsrt (int type, expressionS *ep,
8253 unsigned int sreg, unsigned int treg)
8254{
8255 const char *brneg = NULL;
8256 const int call = 0;
8257 const char *br;
8258
8259 switch (type)
8260 {
8261 case M_BEQ:
8262 case M_BEQ_I:
8263 br = "beq";
8264 break;
8265 case M_BEQL:
8266 case M_BEQL_I:
8267 br = mips_opts.micromips ? "beq" : "beql";
8268 brneg = "bne";
8269 break;
8270 case M_BNE:
8271 case M_BNE_I:
8272 br = "bne";
8273 break;
8274 case M_BNEL:
8275 case M_BNEL_I:
8276 br = mips_opts.micromips ? "bne" : "bnel";
8277 brneg = "beq";
8278 break;
8279 default:
8280 abort ();
8281 }
8282 if (mips_opts.micromips && brneg)
8283 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
8284 else
8285 macro_build (ep, br, "s,t,p", sreg, treg);
8286}
8287
f2ae14a1
RS
8288/* Return the high part that should be loaded in order to make the low
8289 part of VALUE accessible using an offset of OFFBITS bits. */
8290
8291static offsetT
8292offset_high_part (offsetT value, unsigned int offbits)
8293{
8294 offsetT bias;
8295 addressT low_mask;
8296
8297 if (offbits == 0)
8298 return value;
8299 bias = 1 << (offbits - 1);
8300 low_mask = bias * 2 - 1;
8301 return (value + bias) & ~low_mask;
8302}
8303
8304/* Return true if the value stored in offset_expr and offset_reloc
8305 fits into a signed offset of OFFBITS bits. RANGE is the maximum
8306 amount that the caller wants to add without inducing overflow
8307 and ALIGN is the known alignment of the value in bytes. */
8308
8309static bfd_boolean
8310small_offset_p (unsigned int range, unsigned int align, unsigned int offbits)
8311{
8312 if (offbits == 16)
8313 {
8314 /* Accept any relocation operator if overflow isn't a concern. */
8315 if (range < align && *offset_reloc != BFD_RELOC_UNUSED)
8316 return TRUE;
8317
8318 /* These relocations are guaranteed not to overflow in correct links. */
8319 if (*offset_reloc == BFD_RELOC_MIPS_LITERAL
8320 || gprel16_reloc_p (*offset_reloc))
8321 return TRUE;
8322 }
8323 if (offset_expr.X_op == O_constant
8324 && offset_high_part (offset_expr.X_add_number, offbits) == 0
8325 && offset_high_part (offset_expr.X_add_number + range, offbits) == 0)
8326 return TRUE;
8327 return FALSE;
8328}
8329
252b5132
RH
8330/*
8331 * Build macros
8332 * This routine implements the seemingly endless macro or synthesized
8333 * instructions and addressing modes in the mips assembly language. Many
8334 * of these macros are simple and are similar to each other. These could
67c1ffbe 8335 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
8336 * this verbose method. Others are not simple macros but are more like
8337 * optimizing code generation.
8338 * One interesting optimization is when several store macros appear
67c1ffbe 8339 * consecutively that would load AT with the upper half of the same address.
252b5132
RH
8340 * The ensuing load upper instructions are ommited. This implies some kind
8341 * of global optimization. We currently only optimize within a single macro.
8342 * For many of the load and store macros if the address is specified as a
8343 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
8344 * first load register 'at' with zero and use it as the base register. The
8345 * mips assembler simply uses register $zero. Just one tiny optimization
8346 * we're missing.
8347 */
8348static void
833794fc 8349macro (struct mips_cl_insn *ip, char *str)
252b5132 8350{
c0ebe874
RS
8351 const struct mips_operand_array *operands;
8352 unsigned int breg, i;
741fe287 8353 unsigned int tempreg;
252b5132 8354 int mask;
43841e91 8355 int used_at = 0;
df58fc94 8356 expressionS label_expr;
252b5132 8357 expressionS expr1;
df58fc94 8358 expressionS *ep;
252b5132
RH
8359 const char *s;
8360 const char *s2;
8361 const char *fmt;
8362 int likely = 0;
252b5132 8363 int coproc = 0;
7f3c4072 8364 int offbits = 16;
1abe91b1 8365 int call = 0;
df58fc94
RS
8366 int jals = 0;
8367 int dbl = 0;
8368 int imm = 0;
8369 int ust = 0;
8370 int lp = 0;
f2ae14a1 8371 bfd_boolean large_offset;
252b5132 8372 int off;
252b5132 8373 int hold_mips_optimize;
f2ae14a1 8374 unsigned int align;
c0ebe874 8375 unsigned int op[MAX_OPERANDS];
252b5132 8376
9c2799c2 8377 gas_assert (! mips_opts.mips16);
252b5132 8378
c0ebe874
RS
8379 operands = insn_operands (ip);
8380 for (i = 0; i < MAX_OPERANDS; i++)
8381 if (operands->operand[i])
8382 op[i] = insn_extract_operand (ip, operands->operand[i]);
8383 else
8384 op[i] = -1;
8385
252b5132
RH
8386 mask = ip->insn_mo->mask;
8387
df58fc94
RS
8388 label_expr.X_op = O_constant;
8389 label_expr.X_op_symbol = NULL;
8390 label_expr.X_add_symbol = NULL;
8391 label_expr.X_add_number = 0;
8392
252b5132
RH
8393 expr1.X_op = O_constant;
8394 expr1.X_op_symbol = NULL;
8395 expr1.X_add_symbol = NULL;
8396 expr1.X_add_number = 1;
f2ae14a1 8397 align = 1;
252b5132
RH
8398
8399 switch (mask)
8400 {
8401 case M_DABS:
8402 dbl = 1;
8403 case M_ABS:
df58fc94
RS
8404 /* bgez $a0,1f
8405 move v0,$a0
8406 sub v0,$zero,$a0
8407 1:
8408 */
252b5132 8409
7d10b47d 8410 start_noreorder ();
252b5132 8411
df58fc94
RS
8412 if (mips_opts.micromips)
8413 micromips_label_expr (&label_expr);
8414 else
8415 label_expr.X_add_number = 8;
c0ebe874
RS
8416 macro_build (&label_expr, "bgez", "s,p", op[1]);
8417 if (op[0] == op[1])
a605d2b3 8418 macro_build (NULL, "nop", "");
252b5132 8419 else
c0ebe874
RS
8420 move_register (op[0], op[1]);
8421 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", op[0], 0, op[1]);
df58fc94
RS
8422 if (mips_opts.micromips)
8423 micromips_add_label ();
252b5132 8424
7d10b47d 8425 end_noreorder ();
8fc2e39e 8426 break;
252b5132
RH
8427
8428 case M_ADD_I:
8429 s = "addi";
8430 s2 = "add";
8431 goto do_addi;
8432 case M_ADDU_I:
8433 s = "addiu";
8434 s2 = "addu";
8435 goto do_addi;
8436 case M_DADD_I:
8437 dbl = 1;
8438 s = "daddi";
8439 s2 = "dadd";
df58fc94
RS
8440 if (!mips_opts.micromips)
8441 goto do_addi;
8442 if (imm_expr.X_op == O_constant
8443 && imm_expr.X_add_number >= -0x200
8444 && imm_expr.X_add_number < 0x200)
8445 {
c0ebe874 8446 macro_build (NULL, s, "t,r,.", op[0], op[1], imm_expr.X_add_number);
df58fc94
RS
8447 break;
8448 }
8449 goto do_addi_i;
252b5132
RH
8450 case M_DADDU_I:
8451 dbl = 1;
8452 s = "daddiu";
8453 s2 = "daddu";
8454 do_addi:
8455 if (imm_expr.X_op == O_constant
8456 && imm_expr.X_add_number >= -0x8000
8457 && imm_expr.X_add_number < 0x8000)
8458 {
c0ebe874 8459 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 8460 break;
252b5132 8461 }
df58fc94 8462 do_addi_i:
8fc2e39e 8463 used_at = 1;
67c0d1eb 8464 load_register (AT, &imm_expr, dbl);
c0ebe874 8465 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
8466 break;
8467
8468 case M_AND_I:
8469 s = "andi";
8470 s2 = "and";
8471 goto do_bit;
8472 case M_OR_I:
8473 s = "ori";
8474 s2 = "or";
8475 goto do_bit;
8476 case M_NOR_I:
8477 s = "";
8478 s2 = "nor";
8479 goto do_bit;
8480 case M_XOR_I:
8481 s = "xori";
8482 s2 = "xor";
8483 do_bit:
8484 if (imm_expr.X_op == O_constant
8485 && imm_expr.X_add_number >= 0
8486 && imm_expr.X_add_number < 0x10000)
8487 {
8488 if (mask != M_NOR_I)
c0ebe874 8489 macro_build (&imm_expr, s, "t,r,i", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
8490 else
8491 {
67c0d1eb 8492 macro_build (&imm_expr, "ori", "t,r,i",
c0ebe874
RS
8493 op[0], op[1], BFD_RELOC_LO16);
8494 macro_build (NULL, "nor", "d,v,t", op[0], op[0], 0);
252b5132 8495 }
8fc2e39e 8496 break;
252b5132
RH
8497 }
8498
8fc2e39e 8499 used_at = 1;
67c0d1eb 8500 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 8501 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
8502 break;
8503
8b082fb1
TS
8504 case M_BALIGN:
8505 switch (imm_expr.X_add_number)
8506 {
8507 case 0:
8508 macro_build (NULL, "nop", "");
8509 break;
8510 case 2:
c0ebe874 8511 macro_build (NULL, "packrl.ph", "d,s,t", op[0], op[0], op[1]);
8b082fb1 8512 break;
03f66e8a
MR
8513 case 1:
8514 case 3:
c0ebe874 8515 macro_build (NULL, "balign", "t,s,2", op[0], op[1],
90ecf173 8516 (int) imm_expr.X_add_number);
8b082fb1 8517 break;
03f66e8a
MR
8518 default:
8519 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
8520 (unsigned long) imm_expr.X_add_number);
8521 break;
8b082fb1
TS
8522 }
8523 break;
8524
df58fc94
RS
8525 case M_BC1FL:
8526 case M_BC1TL:
8527 case M_BC2FL:
8528 case M_BC2TL:
8529 gas_assert (mips_opts.micromips);
8530 macro_build_branch_ccl (mask, &offset_expr,
8531 EXTRACT_OPERAND (1, BCC, *ip));
8532 break;
8533
252b5132 8534 case M_BEQ_I:
252b5132 8535 case M_BEQL_I:
252b5132 8536 case M_BNE_I:
252b5132 8537 case M_BNEL_I:
252b5132 8538 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
c0ebe874 8539 op[1] = 0;
df58fc94 8540 else
252b5132 8541 {
c0ebe874 8542 op[1] = AT;
df58fc94 8543 used_at = 1;
c0ebe874 8544 load_register (op[1], &imm_expr, HAVE_64BIT_GPRS);
252b5132 8545 }
df58fc94
RS
8546 /* Fall through. */
8547 case M_BEQL:
8548 case M_BNEL:
c0ebe874 8549 macro_build_branch_rsrt (mask, &offset_expr, op[0], op[1]);
252b5132
RH
8550 break;
8551
8552 case M_BGEL:
8553 likely = 1;
8554 case M_BGE:
c0ebe874
RS
8555 if (op[1] == 0)
8556 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[0]);
8557 else if (op[0] == 0)
8558 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[1]);
df58fc94 8559 else
252b5132 8560 {
df58fc94 8561 used_at = 1;
c0ebe874 8562 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
8563 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
8564 &offset_expr, AT, ZERO);
252b5132 8565 }
df58fc94
RS
8566 break;
8567
8568 case M_BGEZL:
8569 case M_BGEZALL:
8570 case M_BGTZL:
8571 case M_BLEZL:
8572 case M_BLTZL:
8573 case M_BLTZALL:
c0ebe874 8574 macro_build_branch_rs (mask, &offset_expr, op[0]);
252b5132
RH
8575 break;
8576
8577 case M_BGTL_I:
8578 likely = 1;
8579 case M_BGT_I:
90ecf173 8580 /* Check for > max integer. */
42429eac 8581 if (imm_expr.X_op == O_constant && imm_expr.X_add_number >= GPR_SMAX)
252b5132
RH
8582 {
8583 do_false:
90ecf173 8584 /* Result is always false. */
252b5132 8585 if (! likely)
a605d2b3 8586 macro_build (NULL, "nop", "");
252b5132 8587 else
df58fc94 8588 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
8fc2e39e 8589 break;
252b5132
RH
8590 }
8591 if (imm_expr.X_op != O_constant)
8592 as_bad (_("Unsupported large constant"));
f9419b05 8593 ++imm_expr.X_add_number;
252b5132
RH
8594 /* FALLTHROUGH */
8595 case M_BGE_I:
8596 case M_BGEL_I:
8597 if (mask == M_BGEL_I)
8598 likely = 1;
8599 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
8600 {
df58fc94 8601 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
c0ebe874 8602 &offset_expr, op[0]);
8fc2e39e 8603 break;
252b5132
RH
8604 }
8605 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
8606 {
df58fc94 8607 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
c0ebe874 8608 &offset_expr, op[0]);
8fc2e39e 8609 break;
252b5132 8610 }
42429eac 8611 if (imm_expr.X_op == O_constant && imm_expr.X_add_number <= GPR_SMIN)
252b5132
RH
8612 {
8613 do_true:
8614 /* result is always true */
8615 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
67c0d1eb 8616 macro_build (&offset_expr, "b", "p");
8fc2e39e 8617 break;
252b5132 8618 }
8fc2e39e 8619 used_at = 1;
c0ebe874 8620 set_at (op[0], 0);
df58fc94
RS
8621 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
8622 &offset_expr, AT, ZERO);
252b5132
RH
8623 break;
8624
8625 case M_BGEUL:
8626 likely = 1;
8627 case M_BGEU:
c0ebe874 8628 if (op[1] == 0)
252b5132 8629 goto do_true;
c0ebe874 8630 else if (op[0] == 0)
df58fc94 8631 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 8632 &offset_expr, ZERO, op[1]);
df58fc94 8633 else
252b5132 8634 {
df58fc94 8635 used_at = 1;
c0ebe874 8636 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
8637 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
8638 &offset_expr, AT, ZERO);
252b5132 8639 }
252b5132
RH
8640 break;
8641
8642 case M_BGTUL_I:
8643 likely = 1;
8644 case M_BGTU_I:
c0ebe874 8645 if (op[0] == 0
ca4e0257 8646 || (HAVE_32BIT_GPRS
252b5132 8647 && imm_expr.X_op == O_constant
f01dc953 8648 && imm_expr.X_add_number == -1))
252b5132
RH
8649 goto do_false;
8650 if (imm_expr.X_op != O_constant)
8651 as_bad (_("Unsupported large constant"));
f9419b05 8652 ++imm_expr.X_add_number;
252b5132
RH
8653 /* FALLTHROUGH */
8654 case M_BGEU_I:
8655 case M_BGEUL_I:
8656 if (mask == M_BGEUL_I)
8657 likely = 1;
8658 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
8659 goto do_true;
df58fc94
RS
8660 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
8661 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 8662 &offset_expr, op[0], ZERO);
df58fc94 8663 else
252b5132 8664 {
df58fc94 8665 used_at = 1;
c0ebe874 8666 set_at (op[0], 1);
df58fc94
RS
8667 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
8668 &offset_expr, AT, ZERO);
252b5132 8669 }
252b5132
RH
8670 break;
8671
8672 case M_BGTL:
8673 likely = 1;
8674 case M_BGT:
c0ebe874
RS
8675 if (op[1] == 0)
8676 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[0]);
8677 else if (op[0] == 0)
8678 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[1]);
df58fc94 8679 else
252b5132 8680 {
df58fc94 8681 used_at = 1;
c0ebe874 8682 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
8683 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
8684 &offset_expr, AT, ZERO);
252b5132 8685 }
252b5132
RH
8686 break;
8687
8688 case M_BGTUL:
8689 likely = 1;
8690 case M_BGTU:
c0ebe874 8691 if (op[1] == 0)
df58fc94 8692 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874
RS
8693 &offset_expr, op[0], ZERO);
8694 else if (op[0] == 0)
df58fc94
RS
8695 goto do_false;
8696 else
252b5132 8697 {
df58fc94 8698 used_at = 1;
c0ebe874 8699 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
8700 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
8701 &offset_expr, AT, ZERO);
252b5132 8702 }
252b5132
RH
8703 break;
8704
8705 case M_BLEL:
8706 likely = 1;
8707 case M_BLE:
c0ebe874
RS
8708 if (op[1] == 0)
8709 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
8710 else if (op[0] == 0)
8711 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[1]);
df58fc94 8712 else
252b5132 8713 {
df58fc94 8714 used_at = 1;
c0ebe874 8715 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
8716 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
8717 &offset_expr, AT, ZERO);
252b5132 8718 }
252b5132
RH
8719 break;
8720
8721 case M_BLEL_I:
8722 likely = 1;
8723 case M_BLE_I:
42429eac 8724 if (imm_expr.X_op == O_constant && imm_expr.X_add_number >= GPR_SMAX)
252b5132
RH
8725 goto do_true;
8726 if (imm_expr.X_op != O_constant)
8727 as_bad (_("Unsupported large constant"));
f9419b05 8728 ++imm_expr.X_add_number;
252b5132
RH
8729 /* FALLTHROUGH */
8730 case M_BLT_I:
8731 case M_BLTL_I:
8732 if (mask == M_BLTL_I)
8733 likely = 1;
8734 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
c0ebe874 8735 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
df58fc94 8736 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
c0ebe874 8737 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
df58fc94 8738 else
252b5132 8739 {
df58fc94 8740 used_at = 1;
c0ebe874 8741 set_at (op[0], 0);
df58fc94
RS
8742 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
8743 &offset_expr, AT, ZERO);
252b5132 8744 }
252b5132
RH
8745 break;
8746
8747 case M_BLEUL:
8748 likely = 1;
8749 case M_BLEU:
c0ebe874 8750 if (op[1] == 0)
df58fc94 8751 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874
RS
8752 &offset_expr, op[0], ZERO);
8753 else if (op[0] == 0)
df58fc94
RS
8754 goto do_true;
8755 else
252b5132 8756 {
df58fc94 8757 used_at = 1;
c0ebe874 8758 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
8759 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
8760 &offset_expr, AT, ZERO);
252b5132 8761 }
252b5132
RH
8762 break;
8763
8764 case M_BLEUL_I:
8765 likely = 1;
8766 case M_BLEU_I:
c0ebe874 8767 if (op[0] == 0
ca4e0257 8768 || (HAVE_32BIT_GPRS
252b5132 8769 && imm_expr.X_op == O_constant
f01dc953 8770 && imm_expr.X_add_number == -1))
252b5132
RH
8771 goto do_true;
8772 if (imm_expr.X_op != O_constant)
8773 as_bad (_("Unsupported large constant"));
f9419b05 8774 ++imm_expr.X_add_number;
252b5132
RH
8775 /* FALLTHROUGH */
8776 case M_BLTU_I:
8777 case M_BLTUL_I:
8778 if (mask == M_BLTUL_I)
8779 likely = 1;
8780 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
8781 goto do_false;
df58fc94
RS
8782 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
8783 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 8784 &offset_expr, op[0], ZERO);
df58fc94 8785 else
252b5132 8786 {
df58fc94 8787 used_at = 1;
c0ebe874 8788 set_at (op[0], 1);
df58fc94
RS
8789 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
8790 &offset_expr, AT, ZERO);
252b5132 8791 }
252b5132
RH
8792 break;
8793
8794 case M_BLTL:
8795 likely = 1;
8796 case M_BLT:
c0ebe874
RS
8797 if (op[1] == 0)
8798 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
8799 else if (op[0] == 0)
8800 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[1]);
df58fc94 8801 else
252b5132 8802 {
df58fc94 8803 used_at = 1;
c0ebe874 8804 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
8805 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
8806 &offset_expr, AT, ZERO);
252b5132 8807 }
252b5132
RH
8808 break;
8809
8810 case M_BLTUL:
8811 likely = 1;
8812 case M_BLTU:
c0ebe874 8813 if (op[1] == 0)
252b5132 8814 goto do_false;
c0ebe874 8815 else if (op[0] == 0)
df58fc94 8816 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 8817 &offset_expr, ZERO, op[1]);
df58fc94 8818 else
252b5132 8819 {
df58fc94 8820 used_at = 1;
c0ebe874 8821 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
8822 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
8823 &offset_expr, AT, ZERO);
252b5132 8824 }
252b5132
RH
8825 break;
8826
5f74bc13
CD
8827 case M_DEXT:
8828 {
d5818fca
MR
8829 /* Use unsigned arithmetic. */
8830 addressT pos;
8831 addressT size;
5f74bc13 8832
90ecf173 8833 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
5f74bc13
CD
8834 {
8835 as_bad (_("Unsupported large constant"));
8836 pos = size = 1;
8837 }
8838 else
8839 {
d5818fca
MR
8840 pos = imm_expr.X_add_number;
8841 size = imm2_expr.X_add_number;
5f74bc13
CD
8842 }
8843
8844 if (pos > 63)
8845 {
9e12b7a2 8846 report_bad_range (ip, 3, pos, 0, 63, FALSE);
5f74bc13
CD
8847 pos = 1;
8848 }
90ecf173 8849 if (size == 0 || size > 64 || (pos + size - 1) > 63)
5f74bc13 8850 {
9e12b7a2 8851 report_bad_field (pos, size);
5f74bc13
CD
8852 size = 1;
8853 }
8854
8855 if (size <= 32 && pos < 32)
8856 {
8857 s = "dext";
8858 fmt = "t,r,+A,+C";
8859 }
8860 else if (size <= 32)
8861 {
8862 s = "dextu";
8863 fmt = "t,r,+E,+H";
8864 }
8865 else
8866 {
8867 s = "dextm";
8868 fmt = "t,r,+A,+G";
8869 }
c0ebe874 8870 macro_build ((expressionS *) NULL, s, fmt, op[0], op[1], (int) pos,
d5818fca 8871 (int) (size - 1));
5f74bc13 8872 }
8fc2e39e 8873 break;
5f74bc13
CD
8874
8875 case M_DINS:
8876 {
d5818fca
MR
8877 /* Use unsigned arithmetic. */
8878 addressT pos;
8879 addressT size;
5f74bc13 8880
90ecf173 8881 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
5f74bc13
CD
8882 {
8883 as_bad (_("Unsupported large constant"));
8884 pos = size = 1;
8885 }
8886 else
8887 {
d5818fca
MR
8888 pos = imm_expr.X_add_number;
8889 size = imm2_expr.X_add_number;
5f74bc13
CD
8890 }
8891
8892 if (pos > 63)
8893 {
9e12b7a2 8894 report_bad_range (ip, 3, pos, 0, 63, FALSE);
5f74bc13
CD
8895 pos = 1;
8896 }
90ecf173 8897 if (size == 0 || size > 64 || (pos + size - 1) > 63)
5f74bc13 8898 {
9e12b7a2 8899 report_bad_field (pos, size);
5f74bc13
CD
8900 size = 1;
8901 }
8902
8903 if (pos < 32 && (pos + size - 1) < 32)
8904 {
8905 s = "dins";
8906 fmt = "t,r,+A,+B";
8907 }
8908 else if (pos >= 32)
8909 {
8910 s = "dinsu";
8911 fmt = "t,r,+E,+F";
8912 }
8913 else
8914 {
8915 s = "dinsm";
8916 fmt = "t,r,+A,+F";
8917 }
c0ebe874 8918 macro_build ((expressionS *) NULL, s, fmt, op[0], op[1], (int) pos,
750bdd57 8919 (int) (pos + size - 1));
5f74bc13 8920 }
8fc2e39e 8921 break;
5f74bc13 8922
252b5132
RH
8923 case M_DDIV_3:
8924 dbl = 1;
8925 case M_DIV_3:
8926 s = "mflo";
8927 goto do_div3;
8928 case M_DREM_3:
8929 dbl = 1;
8930 case M_REM_3:
8931 s = "mfhi";
8932 do_div3:
c0ebe874 8933 if (op[2] == 0)
252b5132
RH
8934 {
8935 as_warn (_("Divide by zero."));
8936 if (mips_trap)
df58fc94 8937 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 8938 else
df58fc94 8939 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 8940 break;
252b5132
RH
8941 }
8942
7d10b47d 8943 start_noreorder ();
252b5132
RH
8944 if (mips_trap)
8945 {
c0ebe874
RS
8946 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
8947 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
252b5132
RH
8948 }
8949 else
8950 {
df58fc94
RS
8951 if (mips_opts.micromips)
8952 micromips_label_expr (&label_expr);
8953 else
8954 label_expr.X_add_number = 8;
c0ebe874
RS
8955 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
8956 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
df58fc94
RS
8957 macro_build (NULL, "break", BRK_FMT, 7);
8958 if (mips_opts.micromips)
8959 micromips_add_label ();
252b5132
RH
8960 }
8961 expr1.X_add_number = -1;
8fc2e39e 8962 used_at = 1;
f6a22291 8963 load_register (AT, &expr1, dbl);
df58fc94
RS
8964 if (mips_opts.micromips)
8965 micromips_label_expr (&label_expr);
8966 else
8967 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
c0ebe874 8968 macro_build (&label_expr, "bne", "s,t,p", op[2], AT);
252b5132
RH
8969 if (dbl)
8970 {
8971 expr1.X_add_number = 1;
f6a22291 8972 load_register (AT, &expr1, dbl);
df58fc94 8973 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
252b5132
RH
8974 }
8975 else
8976 {
8977 expr1.X_add_number = 0x80000000;
df58fc94 8978 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
252b5132
RH
8979 }
8980 if (mips_trap)
8981 {
c0ebe874 8982 macro_build (NULL, "teq", TRAP_FMT, op[1], AT, 6);
252b5132
RH
8983 /* We want to close the noreorder block as soon as possible, so
8984 that later insns are available for delay slot filling. */
7d10b47d 8985 end_noreorder ();
252b5132
RH
8986 }
8987 else
8988 {
df58fc94
RS
8989 if (mips_opts.micromips)
8990 micromips_label_expr (&label_expr);
8991 else
8992 label_expr.X_add_number = 8;
c0ebe874 8993 macro_build (&label_expr, "bne", "s,t,p", op[1], AT);
a605d2b3 8994 macro_build (NULL, "nop", "");
252b5132
RH
8995
8996 /* We want to close the noreorder block as soon as possible, so
8997 that later insns are available for delay slot filling. */
7d10b47d 8998 end_noreorder ();
252b5132 8999
df58fc94 9000 macro_build (NULL, "break", BRK_FMT, 6);
252b5132 9001 }
df58fc94
RS
9002 if (mips_opts.micromips)
9003 micromips_add_label ();
c0ebe874 9004 macro_build (NULL, s, MFHL_FMT, op[0]);
252b5132
RH
9005 break;
9006
9007 case M_DIV_3I:
9008 s = "div";
9009 s2 = "mflo";
9010 goto do_divi;
9011 case M_DIVU_3I:
9012 s = "divu";
9013 s2 = "mflo";
9014 goto do_divi;
9015 case M_REM_3I:
9016 s = "div";
9017 s2 = "mfhi";
9018 goto do_divi;
9019 case M_REMU_3I:
9020 s = "divu";
9021 s2 = "mfhi";
9022 goto do_divi;
9023 case M_DDIV_3I:
9024 dbl = 1;
9025 s = "ddiv";
9026 s2 = "mflo";
9027 goto do_divi;
9028 case M_DDIVU_3I:
9029 dbl = 1;
9030 s = "ddivu";
9031 s2 = "mflo";
9032 goto do_divi;
9033 case M_DREM_3I:
9034 dbl = 1;
9035 s = "ddiv";
9036 s2 = "mfhi";
9037 goto do_divi;
9038 case M_DREMU_3I:
9039 dbl = 1;
9040 s = "ddivu";
9041 s2 = "mfhi";
9042 do_divi:
9043 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
9044 {
9045 as_warn (_("Divide by zero."));
9046 if (mips_trap)
df58fc94 9047 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 9048 else
df58fc94 9049 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 9050 break;
252b5132
RH
9051 }
9052 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
9053 {
9054 if (strcmp (s2, "mflo") == 0)
c0ebe874 9055 move_register (op[0], op[1]);
252b5132 9056 else
c0ebe874 9057 move_register (op[0], ZERO);
8fc2e39e 9058 break;
252b5132
RH
9059 }
9060 if (imm_expr.X_op == O_constant
9061 && imm_expr.X_add_number == -1
9062 && s[strlen (s) - 1] != 'u')
9063 {
9064 if (strcmp (s2, "mflo") == 0)
c0ebe874 9065 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", op[0], op[1]);
252b5132 9066 else
c0ebe874 9067 move_register (op[0], ZERO);
8fc2e39e 9068 break;
252b5132
RH
9069 }
9070
8fc2e39e 9071 used_at = 1;
67c0d1eb 9072 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
9073 macro_build (NULL, s, "z,s,t", op[1], AT);
9074 macro_build (NULL, s2, MFHL_FMT, op[0]);
252b5132
RH
9075 break;
9076
9077 case M_DIVU_3:
9078 s = "divu";
9079 s2 = "mflo";
9080 goto do_divu3;
9081 case M_REMU_3:
9082 s = "divu";
9083 s2 = "mfhi";
9084 goto do_divu3;
9085 case M_DDIVU_3:
9086 s = "ddivu";
9087 s2 = "mflo";
9088 goto do_divu3;
9089 case M_DREMU_3:
9090 s = "ddivu";
9091 s2 = "mfhi";
9092 do_divu3:
7d10b47d 9093 start_noreorder ();
252b5132
RH
9094 if (mips_trap)
9095 {
c0ebe874
RS
9096 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
9097 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
9098 /* We want to close the noreorder block as soon as possible, so
9099 that later insns are available for delay slot filling. */
7d10b47d 9100 end_noreorder ();
252b5132
RH
9101 }
9102 else
9103 {
df58fc94
RS
9104 if (mips_opts.micromips)
9105 micromips_label_expr (&label_expr);
9106 else
9107 label_expr.X_add_number = 8;
c0ebe874
RS
9108 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
9109 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
9110
9111 /* We want to close the noreorder block as soon as possible, so
9112 that later insns are available for delay slot filling. */
7d10b47d 9113 end_noreorder ();
df58fc94
RS
9114 macro_build (NULL, "break", BRK_FMT, 7);
9115 if (mips_opts.micromips)
9116 micromips_add_label ();
252b5132 9117 }
c0ebe874 9118 macro_build (NULL, s2, MFHL_FMT, op[0]);
8fc2e39e 9119 break;
252b5132 9120
1abe91b1
MR
9121 case M_DLCA_AB:
9122 dbl = 1;
9123 case M_LCA_AB:
9124 call = 1;
9125 goto do_la;
252b5132
RH
9126 case M_DLA_AB:
9127 dbl = 1;
9128 case M_LA_AB:
1abe91b1 9129 do_la:
252b5132
RH
9130 /* Load the address of a symbol into a register. If breg is not
9131 zero, we then add a base register to it. */
9132
c0ebe874 9133 breg = op[2];
3bec30a8
TS
9134 if (dbl && HAVE_32BIT_GPRS)
9135 as_warn (_("dla used to load 32-bit register"));
9136
90ecf173 9137 if (!dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
9138 as_warn (_("la used to load 64-bit address"));
9139
f2ae14a1 9140 if (small_offset_p (0, align, 16))
0c11417f 9141 {
c0ebe874 9142 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", op[0], breg,
f2ae14a1 9143 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8fc2e39e 9144 break;
0c11417f
MR
9145 }
9146
c0ebe874 9147 if (mips_opts.at && (op[0] == breg))
afdbd6d0
CD
9148 {
9149 tempreg = AT;
9150 used_at = 1;
9151 }
9152 else
c0ebe874 9153 tempreg = op[0];
afdbd6d0 9154
252b5132
RH
9155 if (offset_expr.X_op != O_symbol
9156 && offset_expr.X_op != O_constant)
9157 {
f71d0d44 9158 as_bad (_("Expression too complex"));
252b5132
RH
9159 offset_expr.X_op = O_constant;
9160 }
9161
252b5132 9162 if (offset_expr.X_op == O_constant)
aed1a261 9163 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
9164 else if (mips_pic == NO_PIC)
9165 {
d6bc6245 9166 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 9167 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
9168 Otherwise we want
9169 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
9170 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
9171 If we have a constant, we need two instructions anyhow,
d6bc6245 9172 so we may as well always use the latter form.
76b3015f 9173
6caf9ef4
TS
9174 With 64bit address space and a usable $at we want
9175 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9176 lui $at,<sym> (BFD_RELOC_HI16_S)
9177 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
9178 daddiu $at,<sym> (BFD_RELOC_LO16)
9179 dsll32 $tempreg,0
9180 daddu $tempreg,$tempreg,$at
9181
9182 If $at is already in use, we use a path which is suboptimal
9183 on superscalar processors.
9184 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9185 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
9186 dsll $tempreg,16
9187 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
9188 dsll $tempreg,16
9189 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
9190
9191 For GP relative symbols in 64bit address space we can use
9192 the same sequence as in 32bit address space. */
aed1a261 9193 if (HAVE_64BIT_SYMBOLS)
252b5132 9194 {
6caf9ef4
TS
9195 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
9196 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
9197 {
9198 relax_start (offset_expr.X_add_symbol);
9199 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
9200 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
9201 relax_switch ();
9202 }
d6bc6245 9203
741fe287 9204 if (used_at == 0 && mips_opts.at)
98d3f06f 9205 {
df58fc94 9206 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 9207 tempreg, BFD_RELOC_MIPS_HIGHEST);
df58fc94 9208 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 9209 AT, BFD_RELOC_HI16_S);
67c0d1eb 9210 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 9211 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 9212 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 9213 AT, AT, BFD_RELOC_LO16);
df58fc94 9214 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 9215 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
9216 used_at = 1;
9217 }
9218 else
9219 {
df58fc94 9220 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 9221 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 9222 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 9223 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 9224 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 9225 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 9226 tempreg, tempreg, BFD_RELOC_HI16_S);
df58fc94 9227 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 9228 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 9229 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 9230 }
6caf9ef4
TS
9231
9232 if (mips_relax.sequence)
9233 relax_end ();
98d3f06f
KH
9234 }
9235 else
9236 {
9237 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 9238 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 9239 {
4d7206a2 9240 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
9241 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
9242 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 9243 relax_switch ();
98d3f06f 9244 }
6943caf0 9245 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
f71d0d44 9246 as_bad (_("Offset too large"));
67c0d1eb
RS
9247 macro_build_lui (&offset_expr, tempreg);
9248 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
9249 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
9250 if (mips_relax.sequence)
9251 relax_end ();
98d3f06f 9252 }
252b5132 9253 }
0a44bf69 9254 else if (!mips_big_got && !HAVE_NEWABI)
252b5132 9255 {
9117d219
NC
9256 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
9257
252b5132
RH
9258 /* If this is a reference to an external symbol, and there
9259 is no constant, we want
9260 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 9261 or for lca or if tempreg is PIC_CALL_REG
9117d219 9262 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
9263 For a local symbol, we want
9264 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9265 nop
9266 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
9267
9268 If we have a small constant, and this is a reference to
9269 an external symbol, we want
9270 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9271 nop
9272 addiu $tempreg,$tempreg,<constant>
9273 For a local symbol, we want the same instruction
9274 sequence, but we output a BFD_RELOC_LO16 reloc on the
9275 addiu instruction.
9276
9277 If we have a large constant, and this is a reference to
9278 an external symbol, we want
9279 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9280 lui $at,<hiconstant>
9281 addiu $at,$at,<loconstant>
9282 addu $tempreg,$tempreg,$at
9283 For a local symbol, we want the same instruction
9284 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 9285 addiu instruction.
ed6fb7bd
SC
9286 */
9287
4d7206a2 9288 if (offset_expr.X_add_number == 0)
252b5132 9289 {
0a44bf69
RS
9290 if (mips_pic == SVR4_PIC
9291 && breg == 0
9292 && (call || tempreg == PIC_CALL_REG))
4d7206a2
RS
9293 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
9294
9295 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
9296 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9297 lw_reloc_type, mips_gp_register);
4d7206a2 9298 if (breg != 0)
252b5132
RH
9299 {
9300 /* We're going to put in an addu instruction using
9301 tempreg, so we may as well insert the nop right
9302 now. */
269137b2 9303 load_delay_nop ();
252b5132 9304 }
4d7206a2 9305 relax_switch ();
67c0d1eb
RS
9306 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9307 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9308 load_delay_nop ();
67c0d1eb
RS
9309 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
9310 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 9311 relax_end ();
252b5132
RH
9312 /* FIXME: If breg == 0, and the next instruction uses
9313 $tempreg, then if this variant case is used an extra
9314 nop will be generated. */
9315 }
4d7206a2
RS
9316 else if (offset_expr.X_add_number >= -0x8000
9317 && offset_expr.X_add_number < 0x8000)
252b5132 9318 {
67c0d1eb 9319 load_got_offset (tempreg, &offset_expr);
269137b2 9320 load_delay_nop ();
67c0d1eb 9321 add_got_offset (tempreg, &offset_expr);
252b5132
RH
9322 }
9323 else
9324 {
4d7206a2
RS
9325 expr1.X_add_number = offset_expr.X_add_number;
9326 offset_expr.X_add_number =
43c0598f 9327 SEXT_16BIT (offset_expr.X_add_number);
67c0d1eb 9328 load_got_offset (tempreg, &offset_expr);
f6a22291 9329 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
9330 /* If we are going to add in a base register, and the
9331 target register and the base register are the same,
9332 then we are using AT as a temporary register. Since
9333 we want to load the constant into AT, we add our
9334 current AT (from the global offset table) and the
9335 register into the register now, and pretend we were
9336 not using a base register. */
c0ebe874 9337 if (breg == op[0])
252b5132 9338 {
269137b2 9339 load_delay_nop ();
67c0d1eb 9340 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 9341 op[0], AT, breg);
252b5132 9342 breg = 0;
c0ebe874 9343 tempreg = op[0];
252b5132 9344 }
f6a22291 9345 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
9346 used_at = 1;
9347 }
9348 }
0a44bf69 9349 else if (!mips_big_got && HAVE_NEWABI)
f5040a92 9350 {
67c0d1eb 9351 int add_breg_early = 0;
f5040a92
AO
9352
9353 /* If this is a reference to an external, and there is no
9354 constant, or local symbol (*), with or without a
9355 constant, we want
9356 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 9357 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
9358 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
9359
9360 If we have a small constant, and this is a reference to
9361 an external symbol, we want
9362 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
9363 addiu $tempreg,$tempreg,<constant>
9364
9365 If we have a large constant, and this is a reference to
9366 an external symbol, we want
9367 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
9368 lui $at,<hiconstant>
9369 addiu $at,$at,<loconstant>
9370 addu $tempreg,$tempreg,$at
9371
9372 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
9373 local symbols, even though it introduces an additional
9374 instruction. */
9375
f5040a92
AO
9376 if (offset_expr.X_add_number)
9377 {
4d7206a2 9378 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
9379 offset_expr.X_add_number = 0;
9380
4d7206a2 9381 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
9382 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9383 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
9384
9385 if (expr1.X_add_number >= -0x8000
9386 && expr1.X_add_number < 0x8000)
9387 {
67c0d1eb
RS
9388 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
9389 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 9390 }
ecd13cd3 9391 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 9392 {
c0ebe874
RS
9393 unsigned int dreg;
9394
f5040a92
AO
9395 /* If we are going to add in a base register, and the
9396 target register and the base register are the same,
9397 then we are using AT as a temporary register. Since
9398 we want to load the constant into AT, we add our
9399 current AT (from the global offset table) and the
9400 register into the register now, and pretend we were
9401 not using a base register. */
c0ebe874 9402 if (breg != op[0])
f5040a92
AO
9403 dreg = tempreg;
9404 else
9405 {
9c2799c2 9406 gas_assert (tempreg == AT);
67c0d1eb 9407 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
9408 op[0], AT, breg);
9409 dreg = op[0];
67c0d1eb 9410 add_breg_early = 1;
f5040a92
AO
9411 }
9412
f6a22291 9413 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 9414 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9415 dreg, dreg, AT);
f5040a92 9416
f5040a92
AO
9417 used_at = 1;
9418 }
9419 else
9420 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
9421
4d7206a2 9422 relax_switch ();
f5040a92
AO
9423 offset_expr.X_add_number = expr1.X_add_number;
9424
67c0d1eb
RS
9425 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9426 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
9427 if (add_breg_early)
f5040a92 9428 {
67c0d1eb 9429 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 9430 op[0], tempreg, breg);
f5040a92 9431 breg = 0;
c0ebe874 9432 tempreg = op[0];
f5040a92 9433 }
4d7206a2 9434 relax_end ();
f5040a92 9435 }
4d7206a2 9436 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 9437 {
4d7206a2 9438 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
9439 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9440 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 9441 relax_switch ();
67c0d1eb
RS
9442 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9443 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 9444 relax_end ();
f5040a92 9445 }
4d7206a2 9446 else
f5040a92 9447 {
67c0d1eb
RS
9448 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9449 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
9450 }
9451 }
0a44bf69 9452 else if (mips_big_got && !HAVE_NEWABI)
252b5132 9453 {
67c0d1eb 9454 int gpdelay;
9117d219
NC
9455 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
9456 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 9457 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
9458
9459 /* This is the large GOT case. If this is a reference to an
9460 external symbol, and there is no constant, we want
9461 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9462 addu $tempreg,$tempreg,$gp
9463 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 9464 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
9465 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
9466 addu $tempreg,$tempreg,$gp
9467 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
9468 For a local symbol, we want
9469 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9470 nop
9471 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
9472
9473 If we have a small constant, and this is a reference to
9474 an external symbol, we want
9475 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9476 addu $tempreg,$tempreg,$gp
9477 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9478 nop
9479 addiu $tempreg,$tempreg,<constant>
9480 For a local symbol, we want
9481 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9482 nop
9483 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
9484
9485 If we have a large constant, and this is a reference to
9486 an external symbol, we want
9487 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9488 addu $tempreg,$tempreg,$gp
9489 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9490 lui $at,<hiconstant>
9491 addiu $at,$at,<loconstant>
9492 addu $tempreg,$tempreg,$at
9493 For a local symbol, we want
9494 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9495 lui $at,<hiconstant>
9496 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
9497 addu $tempreg,$tempreg,$at
f5040a92 9498 */
438c16b8 9499
252b5132
RH
9500 expr1.X_add_number = offset_expr.X_add_number;
9501 offset_expr.X_add_number = 0;
4d7206a2 9502 relax_start (offset_expr.X_add_symbol);
67c0d1eb 9503 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
9504 if (expr1.X_add_number == 0 && breg == 0
9505 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
9506 {
9507 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
9508 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
9509 }
df58fc94 9510 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 9511 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9512 tempreg, tempreg, mips_gp_register);
67c0d1eb 9513 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 9514 tempreg, lw_reloc_type, tempreg);
252b5132
RH
9515 if (expr1.X_add_number == 0)
9516 {
67c0d1eb 9517 if (breg != 0)
252b5132
RH
9518 {
9519 /* We're going to put in an addu instruction using
9520 tempreg, so we may as well insert the nop right
9521 now. */
269137b2 9522 load_delay_nop ();
252b5132 9523 }
252b5132
RH
9524 }
9525 else if (expr1.X_add_number >= -0x8000
9526 && expr1.X_add_number < 0x8000)
9527 {
269137b2 9528 load_delay_nop ();
67c0d1eb 9529 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 9530 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
9531 }
9532 else
9533 {
c0ebe874
RS
9534 unsigned int dreg;
9535
252b5132
RH
9536 /* If we are going to add in a base register, and the
9537 target register and the base register are the same,
9538 then we are using AT as a temporary register. Since
9539 we want to load the constant into AT, we add our
9540 current AT (from the global offset table) and the
9541 register into the register now, and pretend we were
9542 not using a base register. */
c0ebe874 9543 if (breg != op[0])
67c0d1eb 9544 dreg = tempreg;
252b5132
RH
9545 else
9546 {
9c2799c2 9547 gas_assert (tempreg == AT);
269137b2 9548 load_delay_nop ();
67c0d1eb 9549 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
9550 op[0], AT, breg);
9551 dreg = op[0];
252b5132
RH
9552 }
9553
f6a22291 9554 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 9555 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 9556
252b5132
RH
9557 used_at = 1;
9558 }
43c0598f 9559 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
4d7206a2 9560 relax_switch ();
252b5132 9561
67c0d1eb 9562 if (gpdelay)
252b5132
RH
9563 {
9564 /* This is needed because this instruction uses $gp, but
f5040a92 9565 the first instruction on the main stream does not. */
67c0d1eb 9566 macro_build (NULL, "nop", "");
252b5132 9567 }
ed6fb7bd 9568
67c0d1eb
RS
9569 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9570 local_reloc_type, mips_gp_register);
f5040a92 9571 if (expr1.X_add_number >= -0x8000
252b5132
RH
9572 && expr1.X_add_number < 0x8000)
9573 {
269137b2 9574 load_delay_nop ();
67c0d1eb
RS
9575 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
9576 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 9577 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
9578 register, the external symbol case ended with a load,
9579 so if the symbol turns out to not be external, and
9580 the next instruction uses tempreg, an unnecessary nop
9581 will be inserted. */
252b5132
RH
9582 }
9583 else
9584 {
c0ebe874 9585 if (breg == op[0])
252b5132
RH
9586 {
9587 /* We must add in the base register now, as in the
f5040a92 9588 external symbol case. */
9c2799c2 9589 gas_assert (tempreg == AT);
269137b2 9590 load_delay_nop ();
67c0d1eb 9591 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
9592 op[0], AT, breg);
9593 tempreg = op[0];
252b5132 9594 /* We set breg to 0 because we have arranged to add
f5040a92 9595 it in in both cases. */
252b5132
RH
9596 breg = 0;
9597 }
9598
67c0d1eb
RS
9599 macro_build_lui (&expr1, AT);
9600 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 9601 AT, AT, BFD_RELOC_LO16);
67c0d1eb 9602 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9603 tempreg, tempreg, AT);
8fc2e39e 9604 used_at = 1;
252b5132 9605 }
4d7206a2 9606 relax_end ();
252b5132 9607 }
0a44bf69 9608 else if (mips_big_got && HAVE_NEWABI)
f5040a92 9609 {
f5040a92
AO
9610 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
9611 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 9612 int add_breg_early = 0;
f5040a92
AO
9613
9614 /* This is the large GOT case. If this is a reference to an
9615 external symbol, and there is no constant, we want
9616 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9617 add $tempreg,$tempreg,$gp
9618 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 9619 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
9620 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
9621 add $tempreg,$tempreg,$gp
9622 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
9623
9624 If we have a small constant, and this is a reference to
9625 an external symbol, we want
9626 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9627 add $tempreg,$tempreg,$gp
9628 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9629 addi $tempreg,$tempreg,<constant>
9630
9631 If we have a large constant, and this is a reference to
9632 an external symbol, we want
9633 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9634 addu $tempreg,$tempreg,$gp
9635 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9636 lui $at,<hiconstant>
9637 addi $at,$at,<loconstant>
9638 add $tempreg,$tempreg,$at
9639
9640 If we have NewABI, and we know it's a local symbol, we want
9641 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
9642 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
9643 otherwise we have to resort to GOT_HI16/GOT_LO16. */
9644
4d7206a2 9645 relax_start (offset_expr.X_add_symbol);
f5040a92 9646
4d7206a2 9647 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
9648 offset_expr.X_add_number = 0;
9649
1abe91b1
MR
9650 if (expr1.X_add_number == 0 && breg == 0
9651 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
9652 {
9653 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
9654 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
9655 }
df58fc94 9656 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 9657 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9658 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
9659 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9660 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
9661
9662 if (expr1.X_add_number == 0)
4d7206a2 9663 ;
f5040a92
AO
9664 else if (expr1.X_add_number >= -0x8000
9665 && expr1.X_add_number < 0x8000)
9666 {
67c0d1eb 9667 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 9668 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 9669 }
ecd13cd3 9670 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 9671 {
c0ebe874
RS
9672 unsigned int dreg;
9673
f5040a92
AO
9674 /* If we are going to add in a base register, and the
9675 target register and the base register are the same,
9676 then we are using AT as a temporary register. Since
9677 we want to load the constant into AT, we add our
9678 current AT (from the global offset table) and the
9679 register into the register now, and pretend we were
9680 not using a base register. */
c0ebe874 9681 if (breg != op[0])
f5040a92
AO
9682 dreg = tempreg;
9683 else
9684 {
9c2799c2 9685 gas_assert (tempreg == AT);
67c0d1eb 9686 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
9687 op[0], AT, breg);
9688 dreg = op[0];
67c0d1eb 9689 add_breg_early = 1;
f5040a92
AO
9690 }
9691
f6a22291 9692 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 9693 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 9694
f5040a92
AO
9695 used_at = 1;
9696 }
9697 else
9698 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
9699
4d7206a2 9700 relax_switch ();
f5040a92 9701 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
9702 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9703 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
9704 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
9705 tempreg, BFD_RELOC_MIPS_GOT_OFST);
9706 if (add_breg_early)
f5040a92 9707 {
67c0d1eb 9708 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 9709 op[0], tempreg, breg);
f5040a92 9710 breg = 0;
c0ebe874 9711 tempreg = op[0];
f5040a92 9712 }
4d7206a2 9713 relax_end ();
f5040a92 9714 }
252b5132
RH
9715 else
9716 abort ();
9717
9718 if (breg != 0)
c0ebe874 9719 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", op[0], tempreg, breg);
252b5132
RH
9720 break;
9721
52b6b6b9 9722 case M_MSGSND:
df58fc94 9723 gas_assert (!mips_opts.micromips);
c0ebe874 9724 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x01);
c7af4273 9725 break;
52b6b6b9
JM
9726
9727 case M_MSGLD:
df58fc94 9728 gas_assert (!mips_opts.micromips);
c8276761 9729 macro_build (NULL, "c2", "C", 0x02);
c7af4273 9730 break;
52b6b6b9
JM
9731
9732 case M_MSGLD_T:
df58fc94 9733 gas_assert (!mips_opts.micromips);
c0ebe874 9734 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x02);
c7af4273 9735 break;
52b6b6b9
JM
9736
9737 case M_MSGWAIT:
df58fc94 9738 gas_assert (!mips_opts.micromips);
52b6b6b9 9739 macro_build (NULL, "c2", "C", 3);
c7af4273 9740 break;
52b6b6b9
JM
9741
9742 case M_MSGWAIT_T:
df58fc94 9743 gas_assert (!mips_opts.micromips);
c0ebe874 9744 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x03);
c7af4273 9745 break;
52b6b6b9 9746
252b5132
RH
9747 case M_J_A:
9748 /* The j instruction may not be used in PIC code, since it
9749 requires an absolute address. We convert it to a b
9750 instruction. */
9751 if (mips_pic == NO_PIC)
67c0d1eb 9752 macro_build (&offset_expr, "j", "a");
252b5132 9753 else
67c0d1eb 9754 macro_build (&offset_expr, "b", "p");
8fc2e39e 9755 break;
252b5132
RH
9756
9757 /* The jal instructions must be handled as macros because when
9758 generating PIC code they expand to multi-instruction
9759 sequences. Normally they are simple instructions. */
df58fc94 9760 case M_JALS_1:
c0ebe874
RS
9761 op[1] = op[0];
9762 op[0] = RA;
df58fc94
RS
9763 /* Fall through. */
9764 case M_JALS_2:
9765 gas_assert (mips_opts.micromips);
833794fc
MR
9766 if (mips_opts.insn32)
9767 {
9768 as_bad (_("Opcode not supported in the `insn32' mode `%s'"), str);
9769 break;
9770 }
df58fc94
RS
9771 jals = 1;
9772 goto jal;
252b5132 9773 case M_JAL_1:
c0ebe874
RS
9774 op[1] = op[0];
9775 op[0] = RA;
252b5132
RH
9776 /* Fall through. */
9777 case M_JAL_2:
df58fc94 9778 jal:
3e722fb5 9779 if (mips_pic == NO_PIC)
df58fc94
RS
9780 {
9781 s = jals ? "jalrs" : "jalr";
e64af278 9782 if (mips_opts.micromips
833794fc 9783 && !mips_opts.insn32
c0ebe874 9784 && op[0] == RA
e64af278 9785 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 9786 macro_build (NULL, s, "mj", op[1]);
df58fc94 9787 else
c0ebe874 9788 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
df58fc94 9789 }
0a44bf69 9790 else
252b5132 9791 {
df58fc94
RS
9792 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
9793 && mips_cprestore_offset >= 0);
9794
c0ebe874 9795 if (op[1] != PIC_CALL_REG)
252b5132 9796 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 9797
833794fc
MR
9798 s = ((mips_opts.micromips
9799 && !mips_opts.insn32
9800 && (!mips_opts.noreorder || cprestore))
df58fc94 9801 ? "jalrs" : "jalr");
e64af278 9802 if (mips_opts.micromips
833794fc 9803 && !mips_opts.insn32
c0ebe874 9804 && op[0] == RA
e64af278 9805 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 9806 macro_build (NULL, s, "mj", op[1]);
df58fc94 9807 else
c0ebe874 9808 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
0a44bf69 9809 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
252b5132 9810 {
6478892d
TS
9811 if (mips_cprestore_offset < 0)
9812 as_warn (_("No .cprestore pseudo-op used in PIC code"));
9813 else
9814 {
90ecf173 9815 if (!mips_frame_reg_valid)
7a621144
DJ
9816 {
9817 as_warn (_("No .frame pseudo-op used in PIC code"));
9818 /* Quiet this warning. */
9819 mips_frame_reg_valid = 1;
9820 }
90ecf173 9821 if (!mips_cprestore_valid)
7a621144
DJ
9822 {
9823 as_warn (_("No .cprestore pseudo-op used in PIC code"));
9824 /* Quiet this warning. */
9825 mips_cprestore_valid = 1;
9826 }
d3fca0b5
MR
9827 if (mips_opts.noreorder)
9828 macro_build (NULL, "nop", "");
6478892d 9829 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 9830 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 9831 mips_gp_register,
256ab948
TS
9832 mips_frame_reg,
9833 HAVE_64BIT_ADDRESSES);
6478892d 9834 }
252b5132
RH
9835 }
9836 }
252b5132 9837
8fc2e39e 9838 break;
252b5132 9839
df58fc94
RS
9840 case M_JALS_A:
9841 gas_assert (mips_opts.micromips);
833794fc
MR
9842 if (mips_opts.insn32)
9843 {
9844 as_bad (_("Opcode not supported in the `insn32' mode `%s'"), str);
9845 break;
9846 }
df58fc94
RS
9847 jals = 1;
9848 /* Fall through. */
252b5132
RH
9849 case M_JAL_A:
9850 if (mips_pic == NO_PIC)
df58fc94 9851 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
252b5132
RH
9852 else if (mips_pic == SVR4_PIC)
9853 {
9854 /* If this is a reference to an external symbol, and we are
9855 using a small GOT, we want
9856 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
9857 nop
f9419b05 9858 jalr $ra,$25
252b5132
RH
9859 nop
9860 lw $gp,cprestore($sp)
9861 The cprestore value is set using the .cprestore
9862 pseudo-op. If we are using a big GOT, we want
9863 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
9864 addu $25,$25,$gp
9865 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
9866 nop
f9419b05 9867 jalr $ra,$25
252b5132
RH
9868 nop
9869 lw $gp,cprestore($sp)
9870 If the symbol is not external, we want
9871 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9872 nop
9873 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 9874 jalr $ra,$25
252b5132 9875 nop
438c16b8 9876 lw $gp,cprestore($sp)
f5040a92
AO
9877
9878 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
9879 sequences above, minus nops, unless the symbol is local,
9880 which enables us to use GOT_PAGE/GOT_OFST (big got) or
9881 GOT_DISP. */
438c16b8 9882 if (HAVE_NEWABI)
252b5132 9883 {
90ecf173 9884 if (!mips_big_got)
f5040a92 9885 {
4d7206a2 9886 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
9887 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9888 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 9889 mips_gp_register);
4d7206a2 9890 relax_switch ();
67c0d1eb
RS
9891 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9892 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
9893 mips_gp_register);
9894 relax_end ();
f5040a92
AO
9895 }
9896 else
9897 {
4d7206a2 9898 relax_start (offset_expr.X_add_symbol);
df58fc94 9899 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
9900 BFD_RELOC_MIPS_CALL_HI16);
9901 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
9902 PIC_CALL_REG, mips_gp_register);
9903 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9904 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
9905 PIC_CALL_REG);
4d7206a2 9906 relax_switch ();
67c0d1eb
RS
9907 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9908 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
9909 mips_gp_register);
9910 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
9911 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 9912 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 9913 relax_end ();
f5040a92 9914 }
684022ea 9915
df58fc94 9916 macro_build_jalr (&offset_expr, 0);
252b5132
RH
9917 }
9918 else
9919 {
4d7206a2 9920 relax_start (offset_expr.X_add_symbol);
90ecf173 9921 if (!mips_big_got)
438c16b8 9922 {
67c0d1eb
RS
9923 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9924 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 9925 mips_gp_register);
269137b2 9926 load_delay_nop ();
4d7206a2 9927 relax_switch ();
438c16b8 9928 }
252b5132 9929 else
252b5132 9930 {
67c0d1eb
RS
9931 int gpdelay;
9932
9933 gpdelay = reg_needs_delay (mips_gp_register);
df58fc94 9934 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
9935 BFD_RELOC_MIPS_CALL_HI16);
9936 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
9937 PIC_CALL_REG, mips_gp_register);
9938 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9939 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
9940 PIC_CALL_REG);
269137b2 9941 load_delay_nop ();
4d7206a2 9942 relax_switch ();
67c0d1eb
RS
9943 if (gpdelay)
9944 macro_build (NULL, "nop", "");
252b5132 9945 }
67c0d1eb
RS
9946 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9947 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 9948 mips_gp_register);
269137b2 9949 load_delay_nop ();
67c0d1eb
RS
9950 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
9951 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 9952 relax_end ();
df58fc94 9953 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
438c16b8 9954
6478892d
TS
9955 if (mips_cprestore_offset < 0)
9956 as_warn (_("No .cprestore pseudo-op used in PIC code"));
9957 else
9958 {
90ecf173 9959 if (!mips_frame_reg_valid)
7a621144
DJ
9960 {
9961 as_warn (_("No .frame pseudo-op used in PIC code"));
9962 /* Quiet this warning. */
9963 mips_frame_reg_valid = 1;
9964 }
90ecf173 9965 if (!mips_cprestore_valid)
7a621144
DJ
9966 {
9967 as_warn (_("No .cprestore pseudo-op used in PIC code"));
9968 /* Quiet this warning. */
9969 mips_cprestore_valid = 1;
9970 }
6478892d 9971 if (mips_opts.noreorder)
67c0d1eb 9972 macro_build (NULL, "nop", "");
6478892d 9973 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 9974 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 9975 mips_gp_register,
256ab948
TS
9976 mips_frame_reg,
9977 HAVE_64BIT_ADDRESSES);
6478892d 9978 }
252b5132
RH
9979 }
9980 }
0a44bf69
RS
9981 else if (mips_pic == VXWORKS_PIC)
9982 as_bad (_("Non-PIC jump used in PIC library"));
252b5132
RH
9983 else
9984 abort ();
9985
8fc2e39e 9986 break;
252b5132 9987
7f3c4072 9988 case M_LBUE_AB:
7f3c4072
CM
9989 s = "lbue";
9990 fmt = "t,+j(b)";
9991 offbits = 9;
9992 goto ld_st;
9993 case M_LHUE_AB:
7f3c4072
CM
9994 s = "lhue";
9995 fmt = "t,+j(b)";
9996 offbits = 9;
9997 goto ld_st;
9998 case M_LBE_AB:
7f3c4072
CM
9999 s = "lbe";
10000 fmt = "t,+j(b)";
10001 offbits = 9;
10002 goto ld_st;
10003 case M_LHE_AB:
7f3c4072
CM
10004 s = "lhe";
10005 fmt = "t,+j(b)";
10006 offbits = 9;
10007 goto ld_st;
10008 case M_LLE_AB:
7f3c4072
CM
10009 s = "lle";
10010 fmt = "t,+j(b)";
10011 offbits = 9;
10012 goto ld_st;
10013 case M_LWE_AB:
7f3c4072
CM
10014 s = "lwe";
10015 fmt = "t,+j(b)";
10016 offbits = 9;
10017 goto ld_st;
10018 case M_LWLE_AB:
7f3c4072
CM
10019 s = "lwle";
10020 fmt = "t,+j(b)";
10021 offbits = 9;
10022 goto ld_st;
10023 case M_LWRE_AB:
7f3c4072
CM
10024 s = "lwre";
10025 fmt = "t,+j(b)";
10026 offbits = 9;
10027 goto ld_st;
10028 case M_SBE_AB:
7f3c4072
CM
10029 s = "sbe";
10030 fmt = "t,+j(b)";
10031 offbits = 9;
10032 goto ld_st;
10033 case M_SCE_AB:
7f3c4072
CM
10034 s = "sce";
10035 fmt = "t,+j(b)";
10036 offbits = 9;
10037 goto ld_st;
10038 case M_SHE_AB:
7f3c4072
CM
10039 s = "she";
10040 fmt = "t,+j(b)";
10041 offbits = 9;
10042 goto ld_st;
10043 case M_SWE_AB:
7f3c4072
CM
10044 s = "swe";
10045 fmt = "t,+j(b)";
10046 offbits = 9;
10047 goto ld_st;
10048 case M_SWLE_AB:
7f3c4072
CM
10049 s = "swle";
10050 fmt = "t,+j(b)";
10051 offbits = 9;
10052 goto ld_st;
10053 case M_SWRE_AB:
7f3c4072
CM
10054 s = "swre";
10055 fmt = "t,+j(b)";
10056 offbits = 9;
10057 goto ld_st;
dec0624d 10058 case M_ACLR_AB:
dec0624d 10059 s = "aclr";
dec0624d 10060 fmt = "\\,~(b)";
7f3c4072 10061 offbits = 12;
dec0624d
MR
10062 goto ld_st;
10063 case M_ASET_AB:
dec0624d 10064 s = "aset";
dec0624d 10065 fmt = "\\,~(b)";
7f3c4072 10066 offbits = 12;
dec0624d 10067 goto ld_st;
252b5132
RH
10068 case M_LB_AB:
10069 s = "lb";
df58fc94 10070 fmt = "t,o(b)";
252b5132
RH
10071 goto ld;
10072 case M_LBU_AB:
10073 s = "lbu";
df58fc94 10074 fmt = "t,o(b)";
252b5132
RH
10075 goto ld;
10076 case M_LH_AB:
10077 s = "lh";
df58fc94 10078 fmt = "t,o(b)";
252b5132
RH
10079 goto ld;
10080 case M_LHU_AB:
10081 s = "lhu";
df58fc94 10082 fmt = "t,o(b)";
252b5132
RH
10083 goto ld;
10084 case M_LW_AB:
10085 s = "lw";
df58fc94 10086 fmt = "t,o(b)";
252b5132
RH
10087 goto ld;
10088 case M_LWC0_AB:
df58fc94 10089 gas_assert (!mips_opts.micromips);
252b5132 10090 s = "lwc0";
df58fc94 10091 fmt = "E,o(b)";
bdaaa2e1 10092 /* Itbl support may require additional care here. */
252b5132 10093 coproc = 1;
df58fc94 10094 goto ld_st;
252b5132
RH
10095 case M_LWC1_AB:
10096 s = "lwc1";
df58fc94 10097 fmt = "T,o(b)";
bdaaa2e1 10098 /* Itbl support may require additional care here. */
252b5132 10099 coproc = 1;
df58fc94 10100 goto ld_st;
252b5132
RH
10101 case M_LWC2_AB:
10102 s = "lwc2";
df58fc94 10103 fmt = COP12_FMT;
7f3c4072 10104 offbits = (mips_opts.micromips ? 12 : 16);
bdaaa2e1 10105 /* Itbl support may require additional care here. */
252b5132 10106 coproc = 1;
df58fc94 10107 goto ld_st;
252b5132 10108 case M_LWC3_AB:
df58fc94 10109 gas_assert (!mips_opts.micromips);
252b5132 10110 s = "lwc3";
df58fc94 10111 fmt = "E,o(b)";
bdaaa2e1 10112 /* Itbl support may require additional care here. */
252b5132 10113 coproc = 1;
df58fc94 10114 goto ld_st;
252b5132
RH
10115 case M_LWL_AB:
10116 s = "lwl";
df58fc94 10117 fmt = MEM12_FMT;
7f3c4072 10118 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10119 goto ld_st;
252b5132
RH
10120 case M_LWR_AB:
10121 s = "lwr";
df58fc94 10122 fmt = MEM12_FMT;
7f3c4072 10123 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10124 goto ld_st;
252b5132 10125 case M_LDC1_AB:
252b5132 10126 s = "ldc1";
df58fc94 10127 fmt = "T,o(b)";
bdaaa2e1 10128 /* Itbl support may require additional care here. */
252b5132 10129 coproc = 1;
df58fc94 10130 goto ld_st;
252b5132
RH
10131 case M_LDC2_AB:
10132 s = "ldc2";
df58fc94 10133 fmt = COP12_FMT;
7f3c4072 10134 offbits = (mips_opts.micromips ? 12 : 16);
bdaaa2e1 10135 /* Itbl support may require additional care here. */
252b5132 10136 coproc = 1;
df58fc94 10137 goto ld_st;
c77c0862 10138 case M_LQC2_AB:
c77c0862 10139 s = "lqc2";
14daeee3 10140 fmt = "+7,o(b)";
c77c0862
RS
10141 /* Itbl support may require additional care here. */
10142 coproc = 1;
10143 goto ld_st;
252b5132
RH
10144 case M_LDC3_AB:
10145 s = "ldc3";
df58fc94 10146 fmt = "E,o(b)";
bdaaa2e1 10147 /* Itbl support may require additional care here. */
252b5132 10148 coproc = 1;
df58fc94 10149 goto ld_st;
252b5132
RH
10150 case M_LDL_AB:
10151 s = "ldl";
df58fc94 10152 fmt = MEM12_FMT;
7f3c4072 10153 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10154 goto ld_st;
252b5132
RH
10155 case M_LDR_AB:
10156 s = "ldr";
df58fc94 10157 fmt = MEM12_FMT;
7f3c4072 10158 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10159 goto ld_st;
252b5132
RH
10160 case M_LL_AB:
10161 s = "ll";
df58fc94 10162 fmt = MEM12_FMT;
7f3c4072 10163 offbits = (mips_opts.micromips ? 12 : 16);
252b5132
RH
10164 goto ld;
10165 case M_LLD_AB:
10166 s = "lld";
df58fc94 10167 fmt = MEM12_FMT;
7f3c4072 10168 offbits = (mips_opts.micromips ? 12 : 16);
252b5132
RH
10169 goto ld;
10170 case M_LWU_AB:
10171 s = "lwu";
df58fc94 10172 fmt = MEM12_FMT;
7f3c4072 10173 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
10174 goto ld;
10175 case M_LWP_AB:
df58fc94
RS
10176 gas_assert (mips_opts.micromips);
10177 s = "lwp";
10178 fmt = "t,~(b)";
7f3c4072 10179 offbits = 12;
df58fc94
RS
10180 lp = 1;
10181 goto ld;
10182 case M_LDP_AB:
df58fc94
RS
10183 gas_assert (mips_opts.micromips);
10184 s = "ldp";
10185 fmt = "t,~(b)";
7f3c4072 10186 offbits = 12;
df58fc94
RS
10187 lp = 1;
10188 goto ld;
10189 case M_LWM_AB:
df58fc94
RS
10190 gas_assert (mips_opts.micromips);
10191 s = "lwm";
10192 fmt = "n,~(b)";
7f3c4072 10193 offbits = 12;
df58fc94
RS
10194 goto ld_st;
10195 case M_LDM_AB:
df58fc94
RS
10196 gas_assert (mips_opts.micromips);
10197 s = "ldm";
10198 fmt = "n,~(b)";
7f3c4072 10199 offbits = 12;
df58fc94
RS
10200 goto ld_st;
10201
252b5132 10202 ld:
f19ccbda 10203 /* We don't want to use $0 as tempreg. */
c0ebe874 10204 if (op[2] == op[0] + lp || op[0] + lp == ZERO)
df58fc94 10205 goto ld_st;
252b5132 10206 else
c0ebe874 10207 tempreg = op[0] + lp;
df58fc94
RS
10208 goto ld_noat;
10209
252b5132
RH
10210 case M_SB_AB:
10211 s = "sb";
df58fc94
RS
10212 fmt = "t,o(b)";
10213 goto ld_st;
252b5132
RH
10214 case M_SH_AB:
10215 s = "sh";
df58fc94
RS
10216 fmt = "t,o(b)";
10217 goto ld_st;
252b5132
RH
10218 case M_SW_AB:
10219 s = "sw";
df58fc94
RS
10220 fmt = "t,o(b)";
10221 goto ld_st;
252b5132 10222 case M_SWC0_AB:
df58fc94 10223 gas_assert (!mips_opts.micromips);
252b5132 10224 s = "swc0";
df58fc94 10225 fmt = "E,o(b)";
bdaaa2e1 10226 /* Itbl support may require additional care here. */
252b5132 10227 coproc = 1;
df58fc94 10228 goto ld_st;
252b5132
RH
10229 case M_SWC1_AB:
10230 s = "swc1";
df58fc94 10231 fmt = "T,o(b)";
bdaaa2e1 10232 /* Itbl support may require additional care here. */
252b5132 10233 coproc = 1;
df58fc94 10234 goto ld_st;
252b5132
RH
10235 case M_SWC2_AB:
10236 s = "swc2";
df58fc94 10237 fmt = COP12_FMT;
7f3c4072 10238 offbits = (mips_opts.micromips ? 12 : 16);
bdaaa2e1 10239 /* Itbl support may require additional care here. */
252b5132 10240 coproc = 1;
df58fc94 10241 goto ld_st;
252b5132 10242 case M_SWC3_AB:
df58fc94 10243 gas_assert (!mips_opts.micromips);
252b5132 10244 s = "swc3";
df58fc94 10245 fmt = "E,o(b)";
bdaaa2e1 10246 /* Itbl support may require additional care here. */
252b5132 10247 coproc = 1;
df58fc94 10248 goto ld_st;
252b5132
RH
10249 case M_SWL_AB:
10250 s = "swl";
df58fc94 10251 fmt = MEM12_FMT;
7f3c4072 10252 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10253 goto ld_st;
252b5132
RH
10254 case M_SWR_AB:
10255 s = "swr";
df58fc94 10256 fmt = MEM12_FMT;
7f3c4072 10257 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10258 goto ld_st;
252b5132
RH
10259 case M_SC_AB:
10260 s = "sc";
df58fc94 10261 fmt = MEM12_FMT;
7f3c4072 10262 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10263 goto ld_st;
252b5132
RH
10264 case M_SCD_AB:
10265 s = "scd";
df58fc94 10266 fmt = MEM12_FMT;
7f3c4072 10267 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10268 goto ld_st;
d43b4baf
TS
10269 case M_CACHE_AB:
10270 s = "cache";
df58fc94 10271 fmt = mips_opts.micromips ? "k,~(b)" : "k,o(b)";
7f3c4072
CM
10272 offbits = (mips_opts.micromips ? 12 : 16);
10273 goto ld_st;
10274 case M_CACHEE_AB:
7f3c4072
CM
10275 s = "cachee";
10276 fmt = "k,+j(b)";
10277 offbits = 9;
df58fc94 10278 goto ld_st;
3eebd5eb
MR
10279 case M_PREF_AB:
10280 s = "pref";
df58fc94 10281 fmt = !mips_opts.micromips ? "k,o(b)" : "k,~(b)";
7f3c4072
CM
10282 offbits = (mips_opts.micromips ? 12 : 16);
10283 goto ld_st;
10284 case M_PREFE_AB:
7f3c4072
CM
10285 s = "prefe";
10286 fmt = "k,+j(b)";
10287 offbits = 9;
df58fc94 10288 goto ld_st;
252b5132 10289 case M_SDC1_AB:
252b5132 10290 s = "sdc1";
df58fc94 10291 fmt = "T,o(b)";
252b5132 10292 coproc = 1;
bdaaa2e1 10293 /* Itbl support may require additional care here. */
df58fc94 10294 goto ld_st;
252b5132
RH
10295 case M_SDC2_AB:
10296 s = "sdc2";
df58fc94 10297 fmt = COP12_FMT;
7f3c4072 10298 offbits = (mips_opts.micromips ? 12 : 16);
c77c0862
RS
10299 /* Itbl support may require additional care here. */
10300 coproc = 1;
10301 goto ld_st;
10302 case M_SQC2_AB:
c77c0862 10303 s = "sqc2";
14daeee3 10304 fmt = "+7,o(b)";
bdaaa2e1 10305 /* Itbl support may require additional care here. */
252b5132 10306 coproc = 1;
df58fc94 10307 goto ld_st;
252b5132 10308 case M_SDC3_AB:
df58fc94 10309 gas_assert (!mips_opts.micromips);
252b5132 10310 s = "sdc3";
df58fc94 10311 fmt = "E,o(b)";
bdaaa2e1 10312 /* Itbl support may require additional care here. */
252b5132 10313 coproc = 1;
df58fc94 10314 goto ld_st;
252b5132
RH
10315 case M_SDL_AB:
10316 s = "sdl";
df58fc94 10317 fmt = MEM12_FMT;
7f3c4072 10318 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 10319 goto ld_st;
252b5132
RH
10320 case M_SDR_AB:
10321 s = "sdr";
df58fc94 10322 fmt = MEM12_FMT;
7f3c4072 10323 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
10324 goto ld_st;
10325 case M_SWP_AB:
df58fc94
RS
10326 gas_assert (mips_opts.micromips);
10327 s = "swp";
10328 fmt = "t,~(b)";
7f3c4072 10329 offbits = 12;
df58fc94
RS
10330 goto ld_st;
10331 case M_SDP_AB:
df58fc94
RS
10332 gas_assert (mips_opts.micromips);
10333 s = "sdp";
10334 fmt = "t,~(b)";
7f3c4072 10335 offbits = 12;
df58fc94
RS
10336 goto ld_st;
10337 case M_SWM_AB:
df58fc94
RS
10338 gas_assert (mips_opts.micromips);
10339 s = "swm";
10340 fmt = "n,~(b)";
7f3c4072 10341 offbits = 12;
df58fc94
RS
10342 goto ld_st;
10343 case M_SDM_AB:
df58fc94
RS
10344 gas_assert (mips_opts.micromips);
10345 s = "sdm";
10346 fmt = "n,~(b)";
7f3c4072 10347 offbits = 12;
df58fc94
RS
10348
10349 ld_st:
8fc2e39e 10350 tempreg = AT;
df58fc94 10351 ld_noat:
c0ebe874 10352 breg = op[2];
f2ae14a1
RS
10353 if (small_offset_p (0, align, 16))
10354 {
10355 /* The first case exists for M_LD_AB and M_SD_AB, which are
10356 macros for o32 but which should act like normal instructions
10357 otherwise. */
10358 if (offbits == 16)
c0ebe874 10359 macro_build (&offset_expr, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
10360 offset_reloc[1], offset_reloc[2], breg);
10361 else if (small_offset_p (0, align, offbits))
10362 {
10363 if (offbits == 0)
c0ebe874 10364 macro_build (NULL, s, fmt, op[0], breg);
f2ae14a1 10365 else
c0ebe874 10366 macro_build (NULL, s, fmt, op[0],
c8276761 10367 (int) offset_expr.X_add_number, breg);
f2ae14a1
RS
10368 }
10369 else
10370 {
10371 if (tempreg == AT)
10372 used_at = 1;
10373 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10374 tempreg, breg, -1, offset_reloc[0],
10375 offset_reloc[1], offset_reloc[2]);
10376 if (offbits == 0)
c0ebe874 10377 macro_build (NULL, s, fmt, op[0], tempreg);
f2ae14a1 10378 else
c0ebe874 10379 macro_build (NULL, s, fmt, op[0], 0, tempreg);
f2ae14a1
RS
10380 }
10381 break;
10382 }
10383
10384 if (tempreg == AT)
10385 used_at = 1;
10386
252b5132
RH
10387 if (offset_expr.X_op != O_constant
10388 && offset_expr.X_op != O_symbol)
10389 {
f71d0d44 10390 as_bad (_("Expression too complex"));
252b5132
RH
10391 offset_expr.X_op = O_constant;
10392 }
10393
2051e8c4
MR
10394 if (HAVE_32BIT_ADDRESSES
10395 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
10396 {
10397 char value [32];
10398
10399 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 10400 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 10401 }
2051e8c4 10402
252b5132
RH
10403 /* A constant expression in PIC code can be handled just as it
10404 is in non PIC code. */
aed1a261
RS
10405 if (offset_expr.X_op == O_constant)
10406 {
f2ae14a1
RS
10407 expr1.X_add_number = offset_high_part (offset_expr.X_add_number,
10408 offbits == 0 ? 16 : offbits);
10409 offset_expr.X_add_number -= expr1.X_add_number;
df58fc94 10410
f2ae14a1
RS
10411 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
10412 if (breg != 0)
10413 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10414 tempreg, tempreg, breg);
7f3c4072 10415 if (offbits == 0)
dd6a37e7 10416 {
f2ae14a1 10417 if (offset_expr.X_add_number != 0)
dd6a37e7 10418 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
f2ae14a1 10419 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
c0ebe874 10420 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 10421 }
7f3c4072 10422 else if (offbits == 16)
c0ebe874 10423 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
df58fc94 10424 else
c0ebe874 10425 macro_build (NULL, s, fmt, op[0],
c8276761 10426 (int) offset_expr.X_add_number, tempreg);
df58fc94 10427 }
7f3c4072 10428 else if (offbits != 16)
df58fc94 10429 {
7f3c4072
CM
10430 /* The offset field is too narrow to be used for a low-part
10431 relocation, so load the whole address into the auxillary
f2ae14a1
RS
10432 register. */
10433 load_address (tempreg, &offset_expr, &used_at);
10434 if (breg != 0)
10435 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10436 tempreg, tempreg, breg);
7f3c4072 10437 if (offbits == 0)
c0ebe874 10438 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 10439 else
c0ebe874 10440 macro_build (NULL, s, fmt, op[0], 0, tempreg);
aed1a261
RS
10441 }
10442 else if (mips_pic == NO_PIC)
252b5132
RH
10443 {
10444 /* If this is a reference to a GP relative symbol, and there
10445 is no base register, we want
c0ebe874 10446 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
10447 Otherwise, if there is no base register, we want
10448 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
c0ebe874 10449 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
252b5132
RH
10450 If we have a constant, we need two instructions anyhow,
10451 so we always use the latter form.
10452
10453 If we have a base register, and this is a reference to a
10454 GP relative symbol, we want
10455 addu $tempreg,$breg,$gp
c0ebe874 10456 <op> op[0],<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
10457 Otherwise we want
10458 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
10459 addu $tempreg,$tempreg,$breg
c0ebe874 10460 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 10461 With a constant we always use the latter case.
76b3015f 10462
d6bc6245
TS
10463 With 64bit address space and no base register and $at usable,
10464 we want
10465 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10466 lui $at,<sym> (BFD_RELOC_HI16_S)
10467 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10468 dsll32 $tempreg,0
10469 daddu $tempreg,$at
c0ebe874 10470 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
10471 If we have a base register, we want
10472 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10473 lui $at,<sym> (BFD_RELOC_HI16_S)
10474 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10475 daddu $at,$breg
10476 dsll32 $tempreg,0
10477 daddu $tempreg,$at
c0ebe874 10478 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
10479
10480 Without $at we can't generate the optimal path for superscalar
10481 processors here since this would require two temporary registers.
10482 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10483 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10484 dsll $tempreg,16
10485 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
10486 dsll $tempreg,16
c0ebe874 10487 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
10488 If we have a base register, we want
10489 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10490 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10491 dsll $tempreg,16
10492 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
10493 dsll $tempreg,16
10494 daddu $tempreg,$tempreg,$breg
c0ebe874 10495 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 10496
6caf9ef4 10497 For GP relative symbols in 64bit address space we can use
aed1a261
RS
10498 the same sequence as in 32bit address space. */
10499 if (HAVE_64BIT_SYMBOLS)
d6bc6245 10500 {
aed1a261 10501 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
10502 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
10503 {
10504 relax_start (offset_expr.X_add_symbol);
10505 if (breg == 0)
10506 {
c0ebe874 10507 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
10508 BFD_RELOC_GPREL16, mips_gp_register);
10509 }
10510 else
10511 {
10512 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10513 tempreg, breg, mips_gp_register);
c0ebe874 10514 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
10515 BFD_RELOC_GPREL16, tempreg);
10516 }
10517 relax_switch ();
10518 }
d6bc6245 10519
741fe287 10520 if (used_at == 0 && mips_opts.at)
d6bc6245 10521 {
df58fc94 10522 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb 10523 BFD_RELOC_MIPS_HIGHEST);
df58fc94 10524 macro_build (&offset_expr, "lui", LUI_FMT, AT,
67c0d1eb
RS
10525 BFD_RELOC_HI16_S);
10526 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
10527 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 10528 if (breg != 0)
67c0d1eb 10529 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
df58fc94 10530 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 10531 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
c0ebe874 10532 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16,
67c0d1eb 10533 tempreg);
d6bc6245
TS
10534 used_at = 1;
10535 }
10536 else
10537 {
df58fc94 10538 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb
RS
10539 BFD_RELOC_MIPS_HIGHEST);
10540 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
10541 tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 10542 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb
RS
10543 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
10544 tempreg, BFD_RELOC_HI16_S);
df58fc94 10545 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
d6bc6245 10546 if (breg != 0)
67c0d1eb 10547 macro_build (NULL, "daddu", "d,v,t",
17a2f251 10548 tempreg, tempreg, breg);
c0ebe874 10549 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 10550 BFD_RELOC_LO16, tempreg);
d6bc6245 10551 }
6caf9ef4
TS
10552
10553 if (mips_relax.sequence)
10554 relax_end ();
8fc2e39e 10555 break;
d6bc6245 10556 }
256ab948 10557
252b5132
RH
10558 if (breg == 0)
10559 {
67c0d1eb 10560 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 10561 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 10562 {
4d7206a2 10563 relax_start (offset_expr.X_add_symbol);
c0ebe874 10564 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_GPREL16,
67c0d1eb 10565 mips_gp_register);
4d7206a2 10566 relax_switch ();
252b5132 10567 }
67c0d1eb 10568 macro_build_lui (&offset_expr, tempreg);
c0ebe874 10569 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 10570 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
10571 if (mips_relax.sequence)
10572 relax_end ();
252b5132
RH
10573 }
10574 else
10575 {
67c0d1eb 10576 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 10577 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 10578 {
4d7206a2 10579 relax_start (offset_expr.X_add_symbol);
67c0d1eb 10580 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10581 tempreg, breg, mips_gp_register);
c0ebe874 10582 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 10583 BFD_RELOC_GPREL16, tempreg);
4d7206a2 10584 relax_switch ();
252b5132 10585 }
67c0d1eb
RS
10586 macro_build_lui (&offset_expr, tempreg);
10587 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10588 tempreg, tempreg, breg);
c0ebe874 10589 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 10590 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
10591 if (mips_relax.sequence)
10592 relax_end ();
252b5132
RH
10593 }
10594 }
0a44bf69 10595 else if (!mips_big_got)
252b5132 10596 {
ed6fb7bd 10597 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 10598
252b5132
RH
10599 /* If this is a reference to an external symbol, we want
10600 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10601 nop
c0ebe874 10602 <op> op[0],0($tempreg)
252b5132
RH
10603 Otherwise we want
10604 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10605 nop
10606 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 10607 <op> op[0],0($tempreg)
f5040a92
AO
10608
10609 For NewABI, we want
10610 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 10611 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 10612
252b5132
RH
10613 If there is a base register, we add it to $tempreg before
10614 the <op>. If there is a constant, we stick it in the
10615 <op> instruction. We don't handle constants larger than
10616 16 bits, because we have no way to load the upper 16 bits
10617 (actually, we could handle them for the subset of cases
10618 in which we are not using $at). */
9c2799c2 10619 gas_assert (offset_expr.X_op == O_symbol);
f5040a92
AO
10620 if (HAVE_NEWABI)
10621 {
67c0d1eb
RS
10622 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10623 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 10624 if (breg != 0)
67c0d1eb 10625 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10626 tempreg, tempreg, breg);
c0ebe874 10627 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 10628 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
10629 break;
10630 }
252b5132
RH
10631 expr1.X_add_number = offset_expr.X_add_number;
10632 offset_expr.X_add_number = 0;
10633 if (expr1.X_add_number < -0x8000
10634 || expr1.X_add_number >= 0x8000)
10635 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
10636 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10637 lw_reloc_type, mips_gp_register);
269137b2 10638 load_delay_nop ();
4d7206a2
RS
10639 relax_start (offset_expr.X_add_symbol);
10640 relax_switch ();
67c0d1eb
RS
10641 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
10642 tempreg, BFD_RELOC_LO16);
4d7206a2 10643 relax_end ();
252b5132 10644 if (breg != 0)
67c0d1eb 10645 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10646 tempreg, tempreg, breg);
c0ebe874 10647 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 10648 }
0a44bf69 10649 else if (mips_big_got && !HAVE_NEWABI)
252b5132 10650 {
67c0d1eb 10651 int gpdelay;
252b5132
RH
10652
10653 /* If this is a reference to an external symbol, we want
10654 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10655 addu $tempreg,$tempreg,$gp
10656 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 10657 <op> op[0],0($tempreg)
252b5132
RH
10658 Otherwise we want
10659 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10660 nop
10661 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 10662 <op> op[0],0($tempreg)
252b5132
RH
10663 If there is a base register, we add it to $tempreg before
10664 the <op>. If there is a constant, we stick it in the
10665 <op> instruction. We don't handle constants larger than
10666 16 bits, because we have no way to load the upper 16 bits
10667 (actually, we could handle them for the subset of cases
f5040a92 10668 in which we are not using $at). */
9c2799c2 10669 gas_assert (offset_expr.X_op == O_symbol);
252b5132
RH
10670 expr1.X_add_number = offset_expr.X_add_number;
10671 offset_expr.X_add_number = 0;
10672 if (expr1.X_add_number < -0x8000
10673 || expr1.X_add_number >= 0x8000)
10674 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 10675 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 10676 relax_start (offset_expr.X_add_symbol);
df58fc94 10677 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 10678 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
10679 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
10680 mips_gp_register);
10681 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10682 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 10683 relax_switch ();
67c0d1eb
RS
10684 if (gpdelay)
10685 macro_build (NULL, "nop", "");
10686 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10687 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 10688 load_delay_nop ();
67c0d1eb
RS
10689 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
10690 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
10691 relax_end ();
10692
252b5132 10693 if (breg != 0)
67c0d1eb 10694 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10695 tempreg, tempreg, breg);
c0ebe874 10696 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 10697 }
0a44bf69 10698 else if (mips_big_got && HAVE_NEWABI)
f5040a92 10699 {
f5040a92
AO
10700 /* If this is a reference to an external symbol, we want
10701 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10702 add $tempreg,$tempreg,$gp
10703 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 10704 <op> op[0],<ofst>($tempreg)
f5040a92
AO
10705 Otherwise, for local symbols, we want:
10706 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 10707 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
9c2799c2 10708 gas_assert (offset_expr.X_op == O_symbol);
4d7206a2 10709 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
10710 offset_expr.X_add_number = 0;
10711 if (expr1.X_add_number < -0x8000
10712 || expr1.X_add_number >= 0x8000)
10713 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 10714 relax_start (offset_expr.X_add_symbol);
df58fc94 10715 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 10716 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
10717 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
10718 mips_gp_register);
10719 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10720 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 10721 if (breg != 0)
67c0d1eb 10722 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10723 tempreg, tempreg, breg);
c0ebe874 10724 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
684022ea 10725
4d7206a2 10726 relax_switch ();
f5040a92 10727 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
10728 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10729 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 10730 if (breg != 0)
67c0d1eb 10731 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10732 tempreg, tempreg, breg);
c0ebe874 10733 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 10734 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 10735 relax_end ();
f5040a92 10736 }
252b5132
RH
10737 else
10738 abort ();
10739
252b5132
RH
10740 break;
10741
833794fc
MR
10742 case M_JRADDIUSP:
10743 gas_assert (mips_opts.micromips);
10744 gas_assert (mips_opts.insn32);
10745 start_noreorder ();
10746 macro_build (NULL, "jr", "s", RA);
c0ebe874 10747 expr1.X_add_number = op[0] << 2;
833794fc
MR
10748 macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
10749 end_noreorder ();
10750 break;
10751
10752 case M_JRC:
10753 gas_assert (mips_opts.micromips);
10754 gas_assert (mips_opts.insn32);
c0ebe874 10755 macro_build (NULL, "jr", "s", op[0]);
833794fc
MR
10756 if (mips_opts.noreorder)
10757 macro_build (NULL, "nop", "");
10758 break;
10759
252b5132
RH
10760 case M_LI:
10761 case M_LI_S:
c0ebe874 10762 load_register (op[0], &imm_expr, 0);
8fc2e39e 10763 break;
252b5132
RH
10764
10765 case M_DLI:
c0ebe874 10766 load_register (op[0], &imm_expr, 1);
8fc2e39e 10767 break;
252b5132
RH
10768
10769 case M_LI_SS:
10770 if (imm_expr.X_op == O_constant)
10771 {
8fc2e39e 10772 used_at = 1;
67c0d1eb 10773 load_register (AT, &imm_expr, 0);
c0ebe874 10774 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
10775 break;
10776 }
10777 else
10778 {
9c2799c2 10779 gas_assert (offset_expr.X_op == O_symbol
90ecf173
MR
10780 && strcmp (segment_name (S_GET_SEGMENT
10781 (offset_expr.X_add_symbol)),
10782 ".lit4") == 0
10783 && offset_expr.X_add_number == 0);
c0ebe874 10784 macro_build (&offset_expr, "lwc1", "T,o(b)", op[0],
17a2f251 10785 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 10786 break;
252b5132
RH
10787 }
10788
10789 case M_LI_D:
ca4e0257
RS
10790 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
10791 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
10792 order 32 bits of the value and the low order 32 bits are either
10793 zero or in OFFSET_EXPR. */
252b5132
RH
10794 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
10795 {
ca4e0257 10796 if (HAVE_64BIT_GPRS)
c0ebe874 10797 load_register (op[0], &imm_expr, 1);
252b5132
RH
10798 else
10799 {
10800 int hreg, lreg;
10801
10802 if (target_big_endian)
10803 {
c0ebe874
RS
10804 hreg = op[0];
10805 lreg = op[0] + 1;
252b5132
RH
10806 }
10807 else
10808 {
c0ebe874
RS
10809 hreg = op[0] + 1;
10810 lreg = op[0];
252b5132
RH
10811 }
10812
10813 if (hreg <= 31)
67c0d1eb 10814 load_register (hreg, &imm_expr, 0);
252b5132
RH
10815 if (lreg <= 31)
10816 {
10817 if (offset_expr.X_op == O_absent)
67c0d1eb 10818 move_register (lreg, 0);
252b5132
RH
10819 else
10820 {
9c2799c2 10821 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 10822 load_register (lreg, &offset_expr, 0);
252b5132
RH
10823 }
10824 }
10825 }
8fc2e39e 10826 break;
252b5132
RH
10827 }
10828
10829 /* We know that sym is in the .rdata section. First we get the
10830 upper 16 bits of the address. */
10831 if (mips_pic == NO_PIC)
10832 {
67c0d1eb 10833 macro_build_lui (&offset_expr, AT);
8fc2e39e 10834 used_at = 1;
252b5132 10835 }
0a44bf69 10836 else
252b5132 10837 {
67c0d1eb
RS
10838 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
10839 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 10840 used_at = 1;
252b5132 10841 }
bdaaa2e1 10842
252b5132 10843 /* Now we load the register(s). */
ca4e0257 10844 if (HAVE_64BIT_GPRS)
8fc2e39e
TS
10845 {
10846 used_at = 1;
c0ebe874
RS
10847 macro_build (&offset_expr, "ld", "t,o(b)", op[0],
10848 BFD_RELOC_LO16, AT);
8fc2e39e 10849 }
252b5132
RH
10850 else
10851 {
8fc2e39e 10852 used_at = 1;
c0ebe874
RS
10853 macro_build (&offset_expr, "lw", "t,o(b)", op[0],
10854 BFD_RELOC_LO16, AT);
10855 if (op[0] != RA)
252b5132
RH
10856 {
10857 /* FIXME: How in the world do we deal with the possible
10858 overflow here? */
10859 offset_expr.X_add_number += 4;
67c0d1eb 10860 macro_build (&offset_expr, "lw", "t,o(b)",
c0ebe874 10861 op[0] + 1, BFD_RELOC_LO16, AT);
252b5132
RH
10862 }
10863 }
252b5132
RH
10864 break;
10865
10866 case M_LI_DD:
ca4e0257
RS
10867 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
10868 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
10869 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
10870 the value and the low order 32 bits are either zero or in
10871 OFFSET_EXPR. */
252b5132
RH
10872 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
10873 {
8fc2e39e 10874 used_at = 1;
67c0d1eb 10875 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
ca4e0257
RS
10876 if (HAVE_64BIT_FPRS)
10877 {
9c2799c2 10878 gas_assert (HAVE_64BIT_GPRS);
c0ebe874 10879 macro_build (NULL, "dmtc1", "t,S", AT, op[0]);
ca4e0257 10880 }
252b5132
RH
10881 else
10882 {
c0ebe874 10883 macro_build (NULL, "mtc1", "t,G", AT, op[0] + 1);
252b5132 10884 if (offset_expr.X_op == O_absent)
c0ebe874 10885 macro_build (NULL, "mtc1", "t,G", 0, op[0]);
252b5132
RH
10886 else
10887 {
9c2799c2 10888 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 10889 load_register (AT, &offset_expr, 0);
c0ebe874 10890 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
10891 }
10892 }
10893 break;
10894 }
10895
9c2799c2 10896 gas_assert (offset_expr.X_op == O_symbol
90ecf173 10897 && offset_expr.X_add_number == 0);
252b5132
RH
10898 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
10899 if (strcmp (s, ".lit8") == 0)
f2ae14a1 10900 {
c0ebe874 10901 op[2] = mips_gp_register;
f2ae14a1
RS
10902 offset_reloc[0] = BFD_RELOC_MIPS_LITERAL;
10903 offset_reloc[1] = BFD_RELOC_UNUSED;
10904 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
10905 }
10906 else
10907 {
9c2799c2 10908 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 10909 used_at = 1;
0a44bf69 10910 if (mips_pic != NO_PIC)
67c0d1eb
RS
10911 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
10912 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
10913 else
10914 {
10915 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 10916 macro_build_lui (&offset_expr, AT);
252b5132 10917 }
bdaaa2e1 10918
c0ebe874 10919 op[2] = AT;
f2ae14a1
RS
10920 offset_reloc[0] = BFD_RELOC_LO16;
10921 offset_reloc[1] = BFD_RELOC_UNUSED;
10922 offset_reloc[2] = BFD_RELOC_UNUSED;
10923 }
10924 align = 8;
10925 /* Fall through */
c4a68bea 10926
252b5132
RH
10927 case M_L_DAB:
10928 /*
10929 * The MIPS assembler seems to check for X_add_number not
10930 * being double aligned and generating:
10931 * lui at,%hi(foo+1)
10932 * addu at,at,v1
10933 * addiu at,at,%lo(foo+1)
10934 * lwc1 f2,0(at)
10935 * lwc1 f3,4(at)
10936 * But, the resulting address is the same after relocation so why
10937 * generate the extra instruction?
10938 */
bdaaa2e1 10939 /* Itbl support may require additional care here. */
252b5132 10940 coproc = 1;
df58fc94 10941 fmt = "T,o(b)";
0aa27725 10942 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
10943 {
10944 s = "ldc1";
df58fc94 10945 goto ld_st;
252b5132 10946 }
252b5132 10947 s = "lwc1";
252b5132
RH
10948 goto ldd_std;
10949
10950 case M_S_DAB:
df58fc94
RS
10951 gas_assert (!mips_opts.micromips);
10952 /* Itbl support may require additional care here. */
10953 coproc = 1;
10954 fmt = "T,o(b)";
0aa27725 10955 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
10956 {
10957 s = "sdc1";
df58fc94 10958 goto ld_st;
252b5132 10959 }
252b5132 10960 s = "swc1";
252b5132
RH
10961 goto ldd_std;
10962
e407c74b
NC
10963 case M_LQ_AB:
10964 fmt = "t,o(b)";
10965 s = "lq";
10966 goto ld;
10967
10968 case M_SQ_AB:
10969 fmt = "t,o(b)";
10970 s = "sq";
10971 goto ld_st;
10972
252b5132 10973 case M_LD_AB:
df58fc94 10974 fmt = "t,o(b)";
ca4e0257 10975 if (HAVE_64BIT_GPRS)
252b5132
RH
10976 {
10977 s = "ld";
10978 goto ld;
10979 }
252b5132 10980 s = "lw";
252b5132
RH
10981 goto ldd_std;
10982
10983 case M_SD_AB:
df58fc94 10984 fmt = "t,o(b)";
ca4e0257 10985 if (HAVE_64BIT_GPRS)
252b5132
RH
10986 {
10987 s = "sd";
df58fc94 10988 goto ld_st;
252b5132 10989 }
252b5132 10990 s = "sw";
252b5132
RH
10991
10992 ldd_std:
f2ae14a1
RS
10993 /* Even on a big endian machine $fn comes before $fn+1. We have
10994 to adjust when loading from memory. We set coproc if we must
10995 load $fn+1 first. */
10996 /* Itbl support may require additional care here. */
10997 if (!target_big_endian)
10998 coproc = 0;
10999
c0ebe874 11000 breg = op[2];
f2ae14a1
RS
11001 if (small_offset_p (0, align, 16))
11002 {
11003 ep = &offset_expr;
11004 if (!small_offset_p (4, align, 16))
11005 {
11006 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT, breg,
11007 -1, offset_reloc[0], offset_reloc[1],
11008 offset_reloc[2]);
11009 expr1.X_add_number = 0;
11010 ep = &expr1;
11011 breg = AT;
11012 used_at = 1;
11013 offset_reloc[0] = BFD_RELOC_LO16;
11014 offset_reloc[1] = BFD_RELOC_UNUSED;
11015 offset_reloc[2] = BFD_RELOC_UNUSED;
11016 }
c0ebe874 11017 if (strcmp (s, "lw") == 0 && op[0] == breg)
f2ae14a1
RS
11018 {
11019 ep->X_add_number += 4;
c0ebe874 11020 macro_build (ep, s, fmt, op[0] + 1, -1, offset_reloc[0],
f2ae14a1
RS
11021 offset_reloc[1], offset_reloc[2], breg);
11022 ep->X_add_number -= 4;
c0ebe874 11023 macro_build (ep, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
11024 offset_reloc[1], offset_reloc[2], breg);
11025 }
11026 else
11027 {
c0ebe874 11028 macro_build (ep, s, fmt, coproc ? op[0] + 1 : op[0], -1,
f2ae14a1
RS
11029 offset_reloc[0], offset_reloc[1], offset_reloc[2],
11030 breg);
11031 ep->X_add_number += 4;
c0ebe874 11032 macro_build (ep, s, fmt, coproc ? op[0] : op[0] + 1, -1,
f2ae14a1
RS
11033 offset_reloc[0], offset_reloc[1], offset_reloc[2],
11034 breg);
11035 }
11036 break;
11037 }
11038
252b5132
RH
11039 if (offset_expr.X_op != O_symbol
11040 && offset_expr.X_op != O_constant)
11041 {
f71d0d44 11042 as_bad (_("Expression too complex"));
252b5132
RH
11043 offset_expr.X_op = O_constant;
11044 }
11045
2051e8c4
MR
11046 if (HAVE_32BIT_ADDRESSES
11047 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
11048 {
11049 char value [32];
11050
11051 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 11052 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 11053 }
2051e8c4 11054
90ecf173 11055 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
252b5132
RH
11056 {
11057 /* If this is a reference to a GP relative symbol, we want
c0ebe874
RS
11058 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
11059 <op> op[0]+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
11060 If we have a base register, we use this
11061 addu $at,$breg,$gp
c0ebe874
RS
11062 <op> op[0],<sym>($at) (BFD_RELOC_GPREL16)
11063 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
11064 If this is not a GP relative symbol, we want
11065 lui $at,<sym> (BFD_RELOC_HI16_S)
c0ebe874
RS
11066 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
11067 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
11068 If there is a base register, we add it to $at after the
11069 lui instruction. If there is a constant, we always use
11070 the last case. */
39a59cf8
MR
11071 if (offset_expr.X_op == O_symbol
11072 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 11073 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 11074 {
4d7206a2 11075 relax_start (offset_expr.X_add_symbol);
252b5132
RH
11076 if (breg == 0)
11077 {
c9914766 11078 tempreg = mips_gp_register;
252b5132
RH
11079 }
11080 else
11081 {
67c0d1eb 11082 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11083 AT, breg, mips_gp_register);
252b5132 11084 tempreg = AT;
252b5132
RH
11085 used_at = 1;
11086 }
11087
beae10d5 11088 /* Itbl support may require additional care here. */
c0ebe874 11089 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 11090 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
11091 offset_expr.X_add_number += 4;
11092
11093 /* Set mips_optimize to 2 to avoid inserting an
11094 undesired nop. */
11095 hold_mips_optimize = mips_optimize;
11096 mips_optimize = 2;
beae10d5 11097 /* Itbl support may require additional care here. */
c0ebe874 11098 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 11099 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
11100 mips_optimize = hold_mips_optimize;
11101
4d7206a2 11102 relax_switch ();
252b5132 11103
0970e49e 11104 offset_expr.X_add_number -= 4;
252b5132 11105 }
8fc2e39e 11106 used_at = 1;
f2ae14a1
RS
11107 if (offset_high_part (offset_expr.X_add_number, 16)
11108 != offset_high_part (offset_expr.X_add_number + 4, 16))
11109 {
11110 load_address (AT, &offset_expr, &used_at);
11111 offset_expr.X_op = O_constant;
11112 offset_expr.X_add_number = 0;
11113 }
11114 else
11115 macro_build_lui (&offset_expr, AT);
252b5132 11116 if (breg != 0)
67c0d1eb 11117 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 11118 /* Itbl support may require additional care here. */
c0ebe874 11119 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 11120 BFD_RELOC_LO16, AT);
252b5132
RH
11121 /* FIXME: How do we handle overflow here? */
11122 offset_expr.X_add_number += 4;
beae10d5 11123 /* Itbl support may require additional care here. */
c0ebe874 11124 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 11125 BFD_RELOC_LO16, AT);
4d7206a2
RS
11126 if (mips_relax.sequence)
11127 relax_end ();
bdaaa2e1 11128 }
0a44bf69 11129 else if (!mips_big_got)
252b5132 11130 {
252b5132
RH
11131 /* If this is a reference to an external symbol, we want
11132 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11133 nop
c0ebe874
RS
11134 <op> op[0],0($at)
11135 <op> op[0]+1,4($at)
252b5132
RH
11136 Otherwise we want
11137 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11138 nop
c0ebe874
RS
11139 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
11140 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
11141 If there is a base register we add it to $at before the
11142 lwc1 instructions. If there is a constant we include it
11143 in the lwc1 instructions. */
11144 used_at = 1;
11145 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
11146 if (expr1.X_add_number < -0x8000
11147 || expr1.X_add_number >= 0x8000 - 4)
11148 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 11149 load_got_offset (AT, &offset_expr);
269137b2 11150 load_delay_nop ();
252b5132 11151 if (breg != 0)
67c0d1eb 11152 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
11153
11154 /* Set mips_optimize to 2 to avoid inserting an undesired
11155 nop. */
11156 hold_mips_optimize = mips_optimize;
11157 mips_optimize = 2;
4d7206a2 11158
beae10d5 11159 /* Itbl support may require additional care here. */
4d7206a2 11160 relax_start (offset_expr.X_add_symbol);
c0ebe874 11161 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 11162 BFD_RELOC_LO16, AT);
4d7206a2 11163 expr1.X_add_number += 4;
c0ebe874 11164 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 11165 BFD_RELOC_LO16, AT);
4d7206a2 11166 relax_switch ();
c0ebe874 11167 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 11168 BFD_RELOC_LO16, AT);
4d7206a2 11169 offset_expr.X_add_number += 4;
c0ebe874 11170 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 11171 BFD_RELOC_LO16, AT);
4d7206a2 11172 relax_end ();
252b5132 11173
4d7206a2 11174 mips_optimize = hold_mips_optimize;
252b5132 11175 }
0a44bf69 11176 else if (mips_big_got)
252b5132 11177 {
67c0d1eb 11178 int gpdelay;
252b5132
RH
11179
11180 /* If this is a reference to an external symbol, we want
11181 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11182 addu $at,$at,$gp
11183 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
11184 nop
c0ebe874
RS
11185 <op> op[0],0($at)
11186 <op> op[0]+1,4($at)
252b5132
RH
11187 Otherwise we want
11188 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11189 nop
c0ebe874
RS
11190 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
11191 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
11192 If there is a base register we add it to $at before the
11193 lwc1 instructions. If there is a constant we include it
11194 in the lwc1 instructions. */
11195 used_at = 1;
11196 expr1.X_add_number = offset_expr.X_add_number;
11197 offset_expr.X_add_number = 0;
11198 if (expr1.X_add_number < -0x8000
11199 || expr1.X_add_number >= 0x8000 - 4)
11200 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 11201 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 11202 relax_start (offset_expr.X_add_symbol);
df58fc94 11203 macro_build (&offset_expr, "lui", LUI_FMT,
67c0d1eb
RS
11204 AT, BFD_RELOC_MIPS_GOT_HI16);
11205 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11206 AT, AT, mips_gp_register);
67c0d1eb 11207 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 11208 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 11209 load_delay_nop ();
252b5132 11210 if (breg != 0)
67c0d1eb 11211 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 11212 /* Itbl support may require additional care here. */
c0ebe874 11213 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 11214 BFD_RELOC_LO16, AT);
252b5132
RH
11215 expr1.X_add_number += 4;
11216
11217 /* Set mips_optimize to 2 to avoid inserting an undesired
11218 nop. */
11219 hold_mips_optimize = mips_optimize;
11220 mips_optimize = 2;
beae10d5 11221 /* Itbl support may require additional care here. */
c0ebe874 11222 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 11223 BFD_RELOC_LO16, AT);
252b5132
RH
11224 mips_optimize = hold_mips_optimize;
11225 expr1.X_add_number -= 4;
11226
4d7206a2
RS
11227 relax_switch ();
11228 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
11229 if (gpdelay)
11230 macro_build (NULL, "nop", "");
11231 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
11232 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 11233 load_delay_nop ();
252b5132 11234 if (breg != 0)
67c0d1eb 11235 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 11236 /* Itbl support may require additional care here. */
c0ebe874 11237 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 11238 BFD_RELOC_LO16, AT);
4d7206a2 11239 offset_expr.X_add_number += 4;
252b5132
RH
11240
11241 /* Set mips_optimize to 2 to avoid inserting an undesired
11242 nop. */
11243 hold_mips_optimize = mips_optimize;
11244 mips_optimize = 2;
beae10d5 11245 /* Itbl support may require additional care here. */
c0ebe874 11246 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 11247 BFD_RELOC_LO16, AT);
252b5132 11248 mips_optimize = hold_mips_optimize;
4d7206a2 11249 relax_end ();
252b5132 11250 }
252b5132
RH
11251 else
11252 abort ();
11253
252b5132 11254 break;
dd6a37e7
AP
11255
11256 case M_SAA_AB:
dd6a37e7 11257 s = "saa";
7f3c4072 11258 offbits = 0;
dd6a37e7
AP
11259 fmt = "t,(b)";
11260 goto ld_st;
11261 case M_SAAD_AB:
dd6a37e7 11262 s = "saad";
7f3c4072 11263 offbits = 0;
dd6a37e7
AP
11264 fmt = "t,(b)";
11265 goto ld_st;
11266
252b5132
RH
11267 /* New code added to support COPZ instructions.
11268 This code builds table entries out of the macros in mip_opcodes.
11269 R4000 uses interlocks to handle coproc delays.
11270 Other chips (like the R3000) require nops to be inserted for delays.
11271
f72c8c98 11272 FIXME: Currently, we require that the user handle delays.
252b5132
RH
11273 In order to fill delay slots for non-interlocked chips,
11274 we must have a way to specify delays based on the coprocessor.
11275 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
11276 What are the side-effects of the cop instruction?
11277 What cache support might we have and what are its effects?
11278 Both coprocessor & memory require delays. how long???
bdaaa2e1 11279 What registers are read/set/modified?
252b5132
RH
11280
11281 If an itbl is provided to interpret cop instructions,
bdaaa2e1 11282 this knowledge can be encoded in the itbl spec. */
252b5132
RH
11283
11284 case M_COP0:
11285 s = "c0";
11286 goto copz;
11287 case M_COP1:
11288 s = "c1";
11289 goto copz;
11290 case M_COP2:
11291 s = "c2";
11292 goto copz;
11293 case M_COP3:
11294 s = "c3";
11295 copz:
df58fc94 11296 gas_assert (!mips_opts.micromips);
252b5132
RH
11297 /* For now we just do C (same as Cz). The parameter will be
11298 stored in insn_opcode by mips_ip. */
c8276761 11299 macro_build (NULL, s, "C", (int) ip->insn_opcode);
8fc2e39e 11300 break;
252b5132 11301
ea1fb5dc 11302 case M_MOVE:
c0ebe874 11303 move_register (op[0], op[1]);
8fc2e39e 11304 break;
ea1fb5dc 11305
833794fc
MR
11306 case M_MOVEP:
11307 gas_assert (mips_opts.micromips);
11308 gas_assert (mips_opts.insn32);
c0ebe874
RS
11309 move_register (micromips_to_32_reg_h_map1[op[0]],
11310 micromips_to_32_reg_m_map[op[1]]);
11311 move_register (micromips_to_32_reg_h_map2[op[0]],
11312 micromips_to_32_reg_n_map[op[2]]);
833794fc
MR
11313 break;
11314
252b5132
RH
11315 case M_DMUL:
11316 dbl = 1;
11317 case M_MUL:
e407c74b 11318 if (mips_opts.arch == CPU_R5900)
c0ebe874
RS
11319 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", op[0], op[1],
11320 op[2]);
e407c74b
NC
11321 else
11322 {
c0ebe874
RS
11323 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", op[1], op[2]);
11324 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
e407c74b 11325 }
8fc2e39e 11326 break;
252b5132
RH
11327
11328 case M_DMUL_I:
11329 dbl = 1;
11330 case M_MUL_I:
11331 /* The MIPS assembler some times generates shifts and adds. I'm
11332 not trying to be that fancy. GCC should do this for us
11333 anyway. */
8fc2e39e 11334 used_at = 1;
67c0d1eb 11335 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
11336 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", op[1], AT);
11337 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
11338 break;
11339
11340 case M_DMULO_I:
11341 dbl = 1;
11342 case M_MULO_I:
11343 imm = 1;
11344 goto do_mulo;
11345
11346 case M_DMULO:
11347 dbl = 1;
11348 case M_MULO:
11349 do_mulo:
7d10b47d 11350 start_noreorder ();
8fc2e39e 11351 used_at = 1;
252b5132 11352 if (imm)
67c0d1eb 11353 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
11354 macro_build (NULL, dbl ? "dmult" : "mult", "s,t",
11355 op[1], imm ? AT : op[2]);
11356 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
11357 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, op[0], op[0], 31);
df58fc94 11358 macro_build (NULL, "mfhi", MFHL_FMT, AT);
252b5132 11359 if (mips_trap)
c0ebe874 11360 macro_build (NULL, "tne", TRAP_FMT, op[0], AT, 6);
252b5132
RH
11361 else
11362 {
df58fc94
RS
11363 if (mips_opts.micromips)
11364 micromips_label_expr (&label_expr);
11365 else
11366 label_expr.X_add_number = 8;
c0ebe874 11367 macro_build (&label_expr, "beq", "s,t,p", op[0], AT);
a605d2b3 11368 macro_build (NULL, "nop", "");
df58fc94
RS
11369 macro_build (NULL, "break", BRK_FMT, 6);
11370 if (mips_opts.micromips)
11371 micromips_add_label ();
252b5132 11372 }
7d10b47d 11373 end_noreorder ();
c0ebe874 11374 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
11375 break;
11376
11377 case M_DMULOU_I:
11378 dbl = 1;
11379 case M_MULOU_I:
11380 imm = 1;
11381 goto do_mulou;
11382
11383 case M_DMULOU:
11384 dbl = 1;
11385 case M_MULOU:
11386 do_mulou:
7d10b47d 11387 start_noreorder ();
8fc2e39e 11388 used_at = 1;
252b5132 11389 if (imm)
67c0d1eb
RS
11390 load_register (AT, &imm_expr, dbl);
11391 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
c0ebe874 11392 op[1], imm ? AT : op[2]);
df58fc94 11393 macro_build (NULL, "mfhi", MFHL_FMT, AT);
c0ebe874 11394 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132 11395 if (mips_trap)
df58fc94 11396 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
252b5132
RH
11397 else
11398 {
df58fc94
RS
11399 if (mips_opts.micromips)
11400 micromips_label_expr (&label_expr);
11401 else
11402 label_expr.X_add_number = 8;
11403 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
a605d2b3 11404 macro_build (NULL, "nop", "");
df58fc94
RS
11405 macro_build (NULL, "break", BRK_FMT, 6);
11406 if (mips_opts.micromips)
11407 micromips_add_label ();
252b5132 11408 }
7d10b47d 11409 end_noreorder ();
252b5132
RH
11410 break;
11411
771c7ce4 11412 case M_DROL:
fef14a42 11413 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 11414 {
c0ebe874 11415 if (op[0] == op[1])
82dd0097
CD
11416 {
11417 tempreg = AT;
11418 used_at = 1;
11419 }
11420 else
c0ebe874
RS
11421 tempreg = op[0];
11422 macro_build (NULL, "dnegu", "d,w", tempreg, op[2]);
11423 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 11424 break;
82dd0097 11425 }
8fc2e39e 11426 used_at = 1;
c0ebe874
RS
11427 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
11428 macro_build (NULL, "dsrlv", "d,t,s", AT, op[1], AT);
11429 macro_build (NULL, "dsllv", "d,t,s", op[0], op[1], op[2]);
11430 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
11431 break;
11432
252b5132 11433 case M_ROL:
fef14a42 11434 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 11435 {
c0ebe874 11436 if (op[0] == op[1])
82dd0097
CD
11437 {
11438 tempreg = AT;
11439 used_at = 1;
11440 }
11441 else
c0ebe874
RS
11442 tempreg = op[0];
11443 macro_build (NULL, "negu", "d,w", tempreg, op[2]);
11444 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 11445 break;
82dd0097 11446 }
8fc2e39e 11447 used_at = 1;
c0ebe874
RS
11448 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
11449 macro_build (NULL, "srlv", "d,t,s", AT, op[1], AT);
11450 macro_build (NULL, "sllv", "d,t,s", op[0], op[1], op[2]);
11451 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
11452 break;
11453
771c7ce4
TS
11454 case M_DROL_I:
11455 {
11456 unsigned int rot;
91d6fa6a
NC
11457 char *l;
11458 char *rr;
771c7ce4
TS
11459
11460 if (imm_expr.X_op != O_constant)
82dd0097 11461 as_bad (_("Improper rotate count"));
771c7ce4 11462 rot = imm_expr.X_add_number & 0x3f;
fef14a42 11463 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
11464 {
11465 rot = (64 - rot) & 0x3f;
11466 if (rot >= 32)
c0ebe874 11467 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
60b63b72 11468 else
c0ebe874 11469 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 11470 break;
60b63b72 11471 }
483fc7cd 11472 if (rot == 0)
483fc7cd 11473 {
c0ebe874 11474 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 11475 break;
483fc7cd 11476 }
82dd0097 11477 l = (rot < 0x20) ? "dsll" : "dsll32";
91d6fa6a 11478 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
82dd0097 11479 rot &= 0x1f;
8fc2e39e 11480 used_at = 1;
c0ebe874
RS
11481 macro_build (NULL, l, SHFT_FMT, AT, op[1], rot);
11482 macro_build (NULL, rr, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
11483 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
11484 }
11485 break;
11486
252b5132 11487 case M_ROL_I:
771c7ce4
TS
11488 {
11489 unsigned int rot;
11490
11491 if (imm_expr.X_op != O_constant)
82dd0097 11492 as_bad (_("Improper rotate count"));
771c7ce4 11493 rot = imm_expr.X_add_number & 0x1f;
fef14a42 11494 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 11495 {
c0ebe874
RS
11496 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1],
11497 (32 - rot) & 0x1f);
8fc2e39e 11498 break;
60b63b72 11499 }
483fc7cd 11500 if (rot == 0)
483fc7cd 11501 {
c0ebe874 11502 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 11503 break;
483fc7cd 11504 }
8fc2e39e 11505 used_at = 1;
c0ebe874
RS
11506 macro_build (NULL, "sll", SHFT_FMT, AT, op[1], rot);
11507 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
11508 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
11509 }
11510 break;
11511
11512 case M_DROR:
fef14a42 11513 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 11514 {
c0ebe874 11515 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 11516 break;
82dd0097 11517 }
8fc2e39e 11518 used_at = 1;
c0ebe874
RS
11519 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
11520 macro_build (NULL, "dsllv", "d,t,s", AT, op[1], AT);
11521 macro_build (NULL, "dsrlv", "d,t,s", op[0], op[1], op[2]);
11522 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
11523 break;
11524
11525 case M_ROR:
fef14a42 11526 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 11527 {
c0ebe874 11528 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 11529 break;
82dd0097 11530 }
8fc2e39e 11531 used_at = 1;
c0ebe874
RS
11532 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
11533 macro_build (NULL, "sllv", "d,t,s", AT, op[1], AT);
11534 macro_build (NULL, "srlv", "d,t,s", op[0], op[1], op[2]);
11535 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
11536 break;
11537
771c7ce4
TS
11538 case M_DROR_I:
11539 {
11540 unsigned int rot;
91d6fa6a
NC
11541 char *l;
11542 char *rr;
771c7ce4
TS
11543
11544 if (imm_expr.X_op != O_constant)
82dd0097 11545 as_bad (_("Improper rotate count"));
771c7ce4 11546 rot = imm_expr.X_add_number & 0x3f;
fef14a42 11547 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
11548 {
11549 if (rot >= 32)
c0ebe874 11550 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
82dd0097 11551 else
c0ebe874 11552 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 11553 break;
82dd0097 11554 }
483fc7cd 11555 if (rot == 0)
483fc7cd 11556 {
c0ebe874 11557 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 11558 break;
483fc7cd 11559 }
91d6fa6a 11560 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
82dd0097
CD
11561 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
11562 rot &= 0x1f;
8fc2e39e 11563 used_at = 1;
c0ebe874
RS
11564 macro_build (NULL, rr, SHFT_FMT, AT, op[1], rot);
11565 macro_build (NULL, l, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
11566 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
11567 }
11568 break;
11569
252b5132 11570 case M_ROR_I:
771c7ce4
TS
11571 {
11572 unsigned int rot;
11573
11574 if (imm_expr.X_op != O_constant)
82dd0097 11575 as_bad (_("Improper rotate count"));
771c7ce4 11576 rot = imm_expr.X_add_number & 0x1f;
fef14a42 11577 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 11578 {
c0ebe874 11579 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 11580 break;
82dd0097 11581 }
483fc7cd 11582 if (rot == 0)
483fc7cd 11583 {
c0ebe874 11584 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 11585 break;
483fc7cd 11586 }
8fc2e39e 11587 used_at = 1;
c0ebe874
RS
11588 macro_build (NULL, "srl", SHFT_FMT, AT, op[1], rot);
11589 macro_build (NULL, "sll", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
11590 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4 11591 }
252b5132
RH
11592 break;
11593
252b5132 11594 case M_SEQ:
c0ebe874
RS
11595 if (op[1] == 0)
11596 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[2], BFD_RELOC_LO16);
11597 else if (op[2] == 0)
11598 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
11599 else
11600 {
c0ebe874
RS
11601 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
11602 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
252b5132 11603 }
8fc2e39e 11604 break;
252b5132
RH
11605
11606 case M_SEQ_I:
11607 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
11608 {
c0ebe874 11609 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 11610 break;
252b5132 11611 }
c0ebe874 11612 if (op[1] == 0)
252b5132
RH
11613 {
11614 as_warn (_("Instruction %s: result is always false"),
11615 ip->insn_mo->name);
c0ebe874 11616 move_register (op[0], 0);
8fc2e39e 11617 break;
252b5132 11618 }
dd3cbb7e
NC
11619 if (CPU_HAS_SEQ (mips_opts.arch)
11620 && -512 <= imm_expr.X_add_number
11621 && imm_expr.X_add_number < 512)
11622 {
c0ebe874 11623 macro_build (NULL, "seqi", "t,r,+Q", op[0], op[1],
750bdd57 11624 (int) imm_expr.X_add_number);
dd3cbb7e
NC
11625 break;
11626 }
252b5132
RH
11627 if (imm_expr.X_op == O_constant
11628 && imm_expr.X_add_number >= 0
11629 && imm_expr.X_add_number < 0x10000)
c0ebe874 11630 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
11631 else if (imm_expr.X_op == O_constant
11632 && imm_expr.X_add_number > -0x8000
11633 && imm_expr.X_add_number < 0)
11634 {
11635 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 11636 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
c0ebe874 11637 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 11638 }
dd3cbb7e
NC
11639 else if (CPU_HAS_SEQ (mips_opts.arch))
11640 {
11641 used_at = 1;
11642 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 11643 macro_build (NULL, "seq", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
11644 break;
11645 }
252b5132
RH
11646 else
11647 {
67c0d1eb 11648 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 11649 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
11650 used_at = 1;
11651 }
c0ebe874 11652 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 11653 break;
252b5132 11654
c0ebe874 11655 case M_SGE: /* X >= Y <==> not (X < Y) */
252b5132
RH
11656 s = "slt";
11657 goto sge;
11658 case M_SGEU:
11659 s = "sltu";
11660 sge:
c0ebe874
RS
11661 macro_build (NULL, s, "d,v,t", op[0], op[1], op[2]);
11662 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 11663 break;
252b5132 11664
c0ebe874 11665 case M_SGE_I: /* X >= I <==> not (X < I) */
252b5132
RH
11666 case M_SGEU_I:
11667 if (imm_expr.X_op == O_constant
11668 && imm_expr.X_add_number >= -0x8000
11669 && imm_expr.X_add_number < 0x8000)
c0ebe874
RS
11670 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
11671 op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
11672 else
11673 {
67c0d1eb
RS
11674 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
11675 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
c0ebe874 11676 op[0], op[1], AT);
252b5132
RH
11677 used_at = 1;
11678 }
c0ebe874 11679 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 11680 break;
252b5132 11681
c0ebe874 11682 case M_SGT: /* X > Y <==> Y < X */
252b5132
RH
11683 s = "slt";
11684 goto sgt;
11685 case M_SGTU:
11686 s = "sltu";
11687 sgt:
c0ebe874 11688 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
8fc2e39e 11689 break;
252b5132 11690
c0ebe874 11691 case M_SGT_I: /* X > I <==> I < X */
252b5132
RH
11692 s = "slt";
11693 goto sgti;
11694 case M_SGTU_I:
11695 s = "sltu";
11696 sgti:
8fc2e39e 11697 used_at = 1;
67c0d1eb 11698 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 11699 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
252b5132
RH
11700 break;
11701
c0ebe874 11702 case M_SLE: /* X <= Y <==> Y >= X <==> not (Y < X) */
252b5132
RH
11703 s = "slt";
11704 goto sle;
11705 case M_SLEU:
11706 s = "sltu";
11707 sle:
c0ebe874
RS
11708 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
11709 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 11710 break;
252b5132 11711
c0ebe874 11712 case M_SLE_I: /* X <= I <==> I >= X <==> not (I < X) */
252b5132
RH
11713 s = "slt";
11714 goto slei;
11715 case M_SLEU_I:
11716 s = "sltu";
11717 slei:
8fc2e39e 11718 used_at = 1;
67c0d1eb 11719 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874
RS
11720 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
11721 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
252b5132
RH
11722 break;
11723
11724 case M_SLT_I:
11725 if (imm_expr.X_op == O_constant
11726 && imm_expr.X_add_number >= -0x8000
11727 && imm_expr.X_add_number < 0x8000)
11728 {
c0ebe874
RS
11729 macro_build (&imm_expr, "slti", "t,r,j", op[0], op[1],
11730 BFD_RELOC_LO16);
8fc2e39e 11731 break;
252b5132 11732 }
8fc2e39e 11733 used_at = 1;
67c0d1eb 11734 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 11735 macro_build (NULL, "slt", "d,v,t", op[0], op[1], AT);
252b5132
RH
11736 break;
11737
11738 case M_SLTU_I:
11739 if (imm_expr.X_op == O_constant
11740 && imm_expr.X_add_number >= -0x8000
11741 && imm_expr.X_add_number < 0x8000)
11742 {
c0ebe874 11743 macro_build (&imm_expr, "sltiu", "t,r,j", op[0], op[1],
17a2f251 11744 BFD_RELOC_LO16);
8fc2e39e 11745 break;
252b5132 11746 }
8fc2e39e 11747 used_at = 1;
67c0d1eb 11748 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 11749 macro_build (NULL, "sltu", "d,v,t", op[0], op[1], AT);
252b5132
RH
11750 break;
11751
11752 case M_SNE:
c0ebe874
RS
11753 if (op[1] == 0)
11754 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[2]);
11755 else if (op[2] == 0)
11756 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
252b5132
RH
11757 else
11758 {
c0ebe874
RS
11759 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
11760 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
252b5132 11761 }
8fc2e39e 11762 break;
252b5132
RH
11763
11764 case M_SNE_I:
11765 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
11766 {
c0ebe874 11767 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
8fc2e39e 11768 break;
252b5132 11769 }
c0ebe874 11770 if (op[1] == 0)
252b5132
RH
11771 {
11772 as_warn (_("Instruction %s: result is always true"),
11773 ip->insn_mo->name);
67c0d1eb 11774 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
c0ebe874 11775 op[0], 0, BFD_RELOC_LO16);
8fc2e39e 11776 break;
252b5132 11777 }
dd3cbb7e
NC
11778 if (CPU_HAS_SEQ (mips_opts.arch)
11779 && -512 <= imm_expr.X_add_number
11780 && imm_expr.X_add_number < 512)
11781 {
c0ebe874 11782 macro_build (NULL, "snei", "t,r,+Q", op[0], op[1],
750bdd57 11783 (int) imm_expr.X_add_number);
dd3cbb7e
NC
11784 break;
11785 }
252b5132
RH
11786 if (imm_expr.X_op == O_constant
11787 && imm_expr.X_add_number >= 0
11788 && imm_expr.X_add_number < 0x10000)
11789 {
c0ebe874
RS
11790 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1],
11791 BFD_RELOC_LO16);
252b5132
RH
11792 }
11793 else if (imm_expr.X_op == O_constant
11794 && imm_expr.X_add_number > -0x8000
11795 && imm_expr.X_add_number < 0)
11796 {
11797 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 11798 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
c0ebe874 11799 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 11800 }
dd3cbb7e
NC
11801 else if (CPU_HAS_SEQ (mips_opts.arch))
11802 {
11803 used_at = 1;
11804 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 11805 macro_build (NULL, "sne", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
11806 break;
11807 }
252b5132
RH
11808 else
11809 {
67c0d1eb 11810 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 11811 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
11812 used_at = 1;
11813 }
c0ebe874 11814 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
8fc2e39e 11815 break;
252b5132 11816
df58fc94
RS
11817 case M_SUB_I:
11818 s = "addi";
11819 s2 = "sub";
11820 goto do_subi;
11821 case M_SUBU_I:
11822 s = "addiu";
11823 s2 = "subu";
11824 goto do_subi;
252b5132
RH
11825 case M_DSUB_I:
11826 dbl = 1;
df58fc94
RS
11827 s = "daddi";
11828 s2 = "dsub";
11829 if (!mips_opts.micromips)
11830 goto do_subi;
252b5132 11831 if (imm_expr.X_op == O_constant
df58fc94
RS
11832 && imm_expr.X_add_number > -0x200
11833 && imm_expr.X_add_number <= 0x200)
252b5132 11834 {
c0ebe874 11835 macro_build (NULL, s, "t,r,.", op[0], op[1], -imm_expr.X_add_number);
8fc2e39e 11836 break;
252b5132 11837 }
df58fc94 11838 goto do_subi_i;
252b5132
RH
11839 case M_DSUBU_I:
11840 dbl = 1;
df58fc94
RS
11841 s = "daddiu";
11842 s2 = "dsubu";
11843 do_subi:
252b5132
RH
11844 if (imm_expr.X_op == O_constant
11845 && imm_expr.X_add_number > -0x8000
11846 && imm_expr.X_add_number <= 0x8000)
11847 {
11848 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 11849 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 11850 break;
252b5132 11851 }
df58fc94 11852 do_subi_i:
8fc2e39e 11853 used_at = 1;
67c0d1eb 11854 load_register (AT, &imm_expr, dbl);
c0ebe874 11855 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
11856 break;
11857
11858 case M_TEQ_I:
11859 s = "teq";
11860 goto trap;
11861 case M_TGE_I:
11862 s = "tge";
11863 goto trap;
11864 case M_TGEU_I:
11865 s = "tgeu";
11866 goto trap;
11867 case M_TLT_I:
11868 s = "tlt";
11869 goto trap;
11870 case M_TLTU_I:
11871 s = "tltu";
11872 goto trap;
11873 case M_TNE_I:
11874 s = "tne";
11875 trap:
8fc2e39e 11876 used_at = 1;
67c0d1eb 11877 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
c0ebe874 11878 macro_build (NULL, s, "s,t", op[0], AT);
252b5132
RH
11879 break;
11880
252b5132 11881 case M_TRUNCWS:
43841e91 11882 case M_TRUNCWD:
df58fc94 11883 gas_assert (!mips_opts.micromips);
0aa27725 11884 gas_assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 11885 used_at = 1;
252b5132
RH
11886
11887 /*
11888 * Is the double cfc1 instruction a bug in the mips assembler;
11889 * or is there a reason for it?
11890 */
7d10b47d 11891 start_noreorder ();
c0ebe874
RS
11892 macro_build (NULL, "cfc1", "t,G", op[2], RA);
11893 macro_build (NULL, "cfc1", "t,G", op[2], RA);
67c0d1eb 11894 macro_build (NULL, "nop", "");
252b5132 11895 expr1.X_add_number = 3;
c0ebe874 11896 macro_build (&expr1, "ori", "t,r,i", AT, op[2], BFD_RELOC_LO16);
252b5132 11897 expr1.X_add_number = 2;
67c0d1eb
RS
11898 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
11899 macro_build (NULL, "ctc1", "t,G", AT, RA);
11900 macro_build (NULL, "nop", "");
11901 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
c0ebe874
RS
11902 op[0], op[1]);
11903 macro_build (NULL, "ctc1", "t,G", op[2], RA);
67c0d1eb 11904 macro_build (NULL, "nop", "");
7d10b47d 11905 end_noreorder ();
252b5132
RH
11906 break;
11907
f2ae14a1 11908 case M_ULH_AB:
252b5132 11909 s = "lb";
df58fc94
RS
11910 s2 = "lbu";
11911 off = 1;
11912 goto uld_st;
f2ae14a1 11913 case M_ULHU_AB:
252b5132 11914 s = "lbu";
df58fc94
RS
11915 s2 = "lbu";
11916 off = 1;
11917 goto uld_st;
f2ae14a1 11918 case M_ULW_AB:
df58fc94
RS
11919 s = "lwl";
11920 s2 = "lwr";
7f3c4072 11921 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
11922 off = 3;
11923 goto uld_st;
f2ae14a1 11924 case M_ULD_AB:
252b5132
RH
11925 s = "ldl";
11926 s2 = "ldr";
7f3c4072 11927 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 11928 off = 7;
df58fc94 11929 goto uld_st;
f2ae14a1 11930 case M_USH_AB:
df58fc94
RS
11931 s = "sb";
11932 s2 = "sb";
11933 off = 1;
11934 ust = 1;
11935 goto uld_st;
f2ae14a1 11936 case M_USW_AB:
df58fc94
RS
11937 s = "swl";
11938 s2 = "swr";
7f3c4072 11939 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 11940 off = 3;
df58fc94
RS
11941 ust = 1;
11942 goto uld_st;
f2ae14a1 11943 case M_USD_AB:
df58fc94
RS
11944 s = "sdl";
11945 s2 = "sdr";
7f3c4072 11946 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
11947 off = 7;
11948 ust = 1;
11949
11950 uld_st:
c0ebe874 11951 breg = op[2];
f2ae14a1 11952 large_offset = !small_offset_p (off, align, offbits);
df58fc94
RS
11953 ep = &offset_expr;
11954 expr1.X_add_number = 0;
f2ae14a1 11955 if (large_offset)
df58fc94
RS
11956 {
11957 used_at = 1;
11958 tempreg = AT;
f2ae14a1
RS
11959 if (small_offset_p (0, align, 16))
11960 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg, -1,
11961 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
11962 else
11963 {
11964 load_address (tempreg, ep, &used_at);
11965 if (breg != 0)
11966 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11967 tempreg, tempreg, breg);
11968 }
11969 offset_reloc[0] = BFD_RELOC_LO16;
11970 offset_reloc[1] = BFD_RELOC_UNUSED;
11971 offset_reloc[2] = BFD_RELOC_UNUSED;
df58fc94 11972 breg = tempreg;
c0ebe874 11973 tempreg = op[0];
df58fc94
RS
11974 ep = &expr1;
11975 }
c0ebe874 11976 else if (!ust && op[0] == breg)
8fc2e39e
TS
11977 {
11978 used_at = 1;
11979 tempreg = AT;
11980 }
252b5132 11981 else
c0ebe874 11982 tempreg = op[0];
af22f5b2 11983
df58fc94
RS
11984 if (off == 1)
11985 goto ulh_sh;
252b5132 11986
90ecf173 11987 if (!target_big_endian)
df58fc94 11988 ep->X_add_number += off;
f2ae14a1 11989 if (offbits == 12)
c8276761 11990 macro_build (NULL, s, "t,~(b)", tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
11991 else
11992 macro_build (ep, s, "t,o(b)", tempreg, -1,
11993 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94 11994
90ecf173 11995 if (!target_big_endian)
df58fc94 11996 ep->X_add_number -= off;
252b5132 11997 else
df58fc94 11998 ep->X_add_number += off;
f2ae14a1 11999 if (offbits == 12)
df58fc94 12000 macro_build (NULL, s2, "t,~(b)",
c8276761 12001 tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
12002 else
12003 macro_build (ep, s2, "t,o(b)", tempreg, -1,
12004 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 12005
df58fc94 12006 /* If necessary, move the result in tempreg to the final destination. */
c0ebe874 12007 if (!ust && op[0] != tempreg)
df58fc94
RS
12008 {
12009 /* Protect second load's delay slot. */
12010 load_delay_nop ();
c0ebe874 12011 move_register (op[0], tempreg);
df58fc94 12012 }
8fc2e39e 12013 break;
252b5132 12014
df58fc94 12015 ulh_sh:
d6bc6245 12016 used_at = 1;
df58fc94
RS
12017 if (target_big_endian == ust)
12018 ep->X_add_number += off;
c0ebe874 12019 tempreg = ust || large_offset ? op[0] : AT;
f2ae14a1
RS
12020 macro_build (ep, s, "t,o(b)", tempreg, -1,
12021 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94
RS
12022
12023 /* For halfword transfers we need a temporary register to shuffle
12024 bytes. Unfortunately for M_USH_A we have none available before
12025 the next store as AT holds the base address. We deal with this
12026 case by clobbering TREG and then restoring it as with ULH. */
c0ebe874 12027 tempreg = ust == large_offset ? op[0] : AT;
df58fc94 12028 if (ust)
c0ebe874 12029 macro_build (NULL, "srl", SHFT_FMT, tempreg, op[0], 8);
df58fc94
RS
12030
12031 if (target_big_endian == ust)
12032 ep->X_add_number -= off;
252b5132 12033 else
df58fc94 12034 ep->X_add_number += off;
f2ae14a1
RS
12035 macro_build (ep, s2, "t,o(b)", tempreg, -1,
12036 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 12037
df58fc94 12038 /* For M_USH_A re-retrieve the LSB. */
f2ae14a1 12039 if (ust && large_offset)
df58fc94
RS
12040 {
12041 if (target_big_endian)
12042 ep->X_add_number += off;
12043 else
12044 ep->X_add_number -= off;
f2ae14a1
RS
12045 macro_build (&expr1, "lbu", "t,o(b)", AT, -1,
12046 offset_reloc[0], offset_reloc[1], offset_reloc[2], AT);
df58fc94
RS
12047 }
12048 /* For ULH and M_USH_A OR the LSB in. */
f2ae14a1 12049 if (!ust || large_offset)
df58fc94 12050 {
c0ebe874 12051 tempreg = !large_offset ? AT : op[0];
df58fc94 12052 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
c0ebe874 12053 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
df58fc94 12054 }
252b5132
RH
12055 break;
12056
12057 default:
12058 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 12059 are added dynamically. */
252b5132
RH
12060 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
12061 break;
12062 }
741fe287 12063 if (!mips_opts.at && used_at)
8fc2e39e 12064 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
12065}
12066
12067/* Implement macros in mips16 mode. */
12068
12069static void
17a2f251 12070mips16_macro (struct mips_cl_insn *ip)
252b5132 12071{
c0ebe874 12072 const struct mips_operand_array *operands;
252b5132 12073 int mask;
c0ebe874 12074 int tmp;
252b5132
RH
12075 expressionS expr1;
12076 int dbl;
12077 const char *s, *s2, *s3;
c0ebe874
RS
12078 unsigned int op[MAX_OPERANDS];
12079 unsigned int i;
252b5132
RH
12080
12081 mask = ip->insn_mo->mask;
12082
c0ebe874
RS
12083 operands = insn_operands (ip);
12084 for (i = 0; i < MAX_OPERANDS; i++)
12085 if (operands->operand[i])
12086 op[i] = insn_extract_operand (ip, operands->operand[i]);
12087 else
12088 op[i] = -1;
252b5132 12089
252b5132
RH
12090 expr1.X_op = O_constant;
12091 expr1.X_op_symbol = NULL;
12092 expr1.X_add_symbol = NULL;
12093 expr1.X_add_number = 1;
12094
12095 dbl = 0;
12096
12097 switch (mask)
12098 {
12099 default:
b37df7c4 12100 abort ();
252b5132
RH
12101
12102 case M_DDIV_3:
12103 dbl = 1;
12104 case M_DIV_3:
12105 s = "mflo";
12106 goto do_div3;
12107 case M_DREM_3:
12108 dbl = 1;
12109 case M_REM_3:
12110 s = "mfhi";
12111 do_div3:
7d10b47d 12112 start_noreorder ();
c0ebe874 12113 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", op[1], op[2]);
252b5132 12114 expr1.X_add_number = 2;
c0ebe874 12115 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 12116 macro_build (NULL, "break", "6", 7);
bdaaa2e1 12117
252b5132
RH
12118 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
12119 since that causes an overflow. We should do that as well,
12120 but I don't see how to do the comparisons without a temporary
12121 register. */
7d10b47d 12122 end_noreorder ();
c0ebe874 12123 macro_build (NULL, s, "x", op[0]);
252b5132
RH
12124 break;
12125
12126 case M_DIVU_3:
12127 s = "divu";
12128 s2 = "mflo";
12129 goto do_divu3;
12130 case M_REMU_3:
12131 s = "divu";
12132 s2 = "mfhi";
12133 goto do_divu3;
12134 case M_DDIVU_3:
12135 s = "ddivu";
12136 s2 = "mflo";
12137 goto do_divu3;
12138 case M_DREMU_3:
12139 s = "ddivu";
12140 s2 = "mfhi";
12141 do_divu3:
7d10b47d 12142 start_noreorder ();
c0ebe874 12143 macro_build (NULL, s, "0,x,y", op[1], op[2]);
252b5132 12144 expr1.X_add_number = 2;
c0ebe874 12145 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 12146 macro_build (NULL, "break", "6", 7);
7d10b47d 12147 end_noreorder ();
c0ebe874 12148 macro_build (NULL, s2, "x", op[0]);
252b5132
RH
12149 break;
12150
12151 case M_DMUL:
12152 dbl = 1;
12153 case M_MUL:
c0ebe874
RS
12154 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", op[1], op[2]);
12155 macro_build (NULL, "mflo", "x", op[0]);
8fc2e39e 12156 break;
252b5132
RH
12157
12158 case M_DSUBU_I:
12159 dbl = 1;
12160 goto do_subu;
12161 case M_SUBU_I:
12162 do_subu:
12163 if (imm_expr.X_op != O_constant)
12164 as_bad (_("Unsupported large constant"));
12165 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 12166 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", op[0], op[1]);
252b5132
RH
12167 break;
12168
12169 case M_SUBU_I_2:
12170 if (imm_expr.X_op != O_constant)
12171 as_bad (_("Unsupported large constant"));
12172 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 12173 macro_build (&imm_expr, "addiu", "x,k", op[0]);
252b5132
RH
12174 break;
12175
12176 case M_DSUBU_I_2:
12177 if (imm_expr.X_op != O_constant)
12178 as_bad (_("Unsupported large constant"));
12179 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 12180 macro_build (&imm_expr, "daddiu", "y,j", op[0]);
252b5132
RH
12181 break;
12182
12183 case M_BEQ:
12184 s = "cmp";
12185 s2 = "bteqz";
12186 goto do_branch;
12187 case M_BNE:
12188 s = "cmp";
12189 s2 = "btnez";
12190 goto do_branch;
12191 case M_BLT:
12192 s = "slt";
12193 s2 = "btnez";
12194 goto do_branch;
12195 case M_BLTU:
12196 s = "sltu";
12197 s2 = "btnez";
12198 goto do_branch;
12199 case M_BLE:
12200 s = "slt";
12201 s2 = "bteqz";
12202 goto do_reverse_branch;
12203 case M_BLEU:
12204 s = "sltu";
12205 s2 = "bteqz";
12206 goto do_reverse_branch;
12207 case M_BGE:
12208 s = "slt";
12209 s2 = "bteqz";
12210 goto do_branch;
12211 case M_BGEU:
12212 s = "sltu";
12213 s2 = "bteqz";
12214 goto do_branch;
12215 case M_BGT:
12216 s = "slt";
12217 s2 = "btnez";
12218 goto do_reverse_branch;
12219 case M_BGTU:
12220 s = "sltu";
12221 s2 = "btnez";
12222
12223 do_reverse_branch:
c0ebe874
RS
12224 tmp = op[1];
12225 op[1] = op[0];
12226 op[0] = tmp;
252b5132
RH
12227
12228 do_branch:
c0ebe874 12229 macro_build (NULL, s, "x,y", op[0], op[1]);
67c0d1eb 12230 macro_build (&offset_expr, s2, "p");
252b5132
RH
12231 break;
12232
12233 case M_BEQ_I:
12234 s = "cmpi";
12235 s2 = "bteqz";
12236 s3 = "x,U";
12237 goto do_branch_i;
12238 case M_BNE_I:
12239 s = "cmpi";
12240 s2 = "btnez";
12241 s3 = "x,U";
12242 goto do_branch_i;
12243 case M_BLT_I:
12244 s = "slti";
12245 s2 = "btnez";
12246 s3 = "x,8";
12247 goto do_branch_i;
12248 case M_BLTU_I:
12249 s = "sltiu";
12250 s2 = "btnez";
12251 s3 = "x,8";
12252 goto do_branch_i;
12253 case M_BLE_I:
12254 s = "slti";
12255 s2 = "btnez";
12256 s3 = "x,8";
12257 goto do_addone_branch_i;
12258 case M_BLEU_I:
12259 s = "sltiu";
12260 s2 = "btnez";
12261 s3 = "x,8";
12262 goto do_addone_branch_i;
12263 case M_BGE_I:
12264 s = "slti";
12265 s2 = "bteqz";
12266 s3 = "x,8";
12267 goto do_branch_i;
12268 case M_BGEU_I:
12269 s = "sltiu";
12270 s2 = "bteqz";
12271 s3 = "x,8";
12272 goto do_branch_i;
12273 case M_BGT_I:
12274 s = "slti";
12275 s2 = "bteqz";
12276 s3 = "x,8";
12277 goto do_addone_branch_i;
12278 case M_BGTU_I:
12279 s = "sltiu";
12280 s2 = "bteqz";
12281 s3 = "x,8";
12282
12283 do_addone_branch_i:
12284 if (imm_expr.X_op != O_constant)
12285 as_bad (_("Unsupported large constant"));
12286 ++imm_expr.X_add_number;
12287
12288 do_branch_i:
c0ebe874 12289 macro_build (&imm_expr, s, s3, op[0]);
67c0d1eb 12290 macro_build (&offset_expr, s2, "p");
252b5132
RH
12291 break;
12292
12293 case M_ABS:
12294 expr1.X_add_number = 0;
c0ebe874
RS
12295 macro_build (&expr1, "slti", "x,8", op[1]);
12296 if (op[0] != op[1])
12297 macro_build (NULL, "move", "y,X", op[0], mips16_to_32_reg_map[op[1]]);
252b5132 12298 expr1.X_add_number = 2;
67c0d1eb 12299 macro_build (&expr1, "bteqz", "p");
c0ebe874 12300 macro_build (NULL, "neg", "x,w", op[0], op[0]);
0acfaea6 12301 break;
252b5132
RH
12302 }
12303}
12304
14daeee3
RS
12305/* Look up instruction [START, START + LENGTH) in HASH. Record any extra
12306 opcode bits in *OPCODE_EXTRA. */
12307
12308static struct mips_opcode *
12309mips_lookup_insn (struct hash_control *hash, const char *start,
da8bca91 12310 ssize_t length, unsigned int *opcode_extra)
14daeee3
RS
12311{
12312 char *name, *dot, *p;
12313 unsigned int mask, suffix;
da8bca91 12314 ssize_t opend;
14daeee3
RS
12315 struct mips_opcode *insn;
12316
12317 /* Make a copy of the instruction so that we can fiddle with it. */
12318 name = alloca (length + 1);
12319 memcpy (name, start, length);
12320 name[length] = '\0';
12321
12322 /* Look up the instruction as-is. */
12323 insn = (struct mips_opcode *) hash_find (hash, name);
12324 if (insn && (insn->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) == 0)
12325 return insn;
12326
12327 dot = strchr (name, '.');
12328 if (dot && dot[1])
12329 {
12330 /* Try to interpret the text after the dot as a VU0 channel suffix. */
12331 p = mips_parse_vu0_channels (dot + 1, &mask);
12332 if (*p == 0 && mask != 0)
12333 {
12334 *dot = 0;
12335 insn = (struct mips_opcode *) hash_find (hash, name);
12336 *dot = '.';
12337 if (insn && (insn->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0)
12338 {
12339 *opcode_extra |= mask << mips_vu0_channel_mask.lsb;
12340 return insn;
12341 }
12342 }
12343 }
12344
12345 if (mips_opts.micromips)
12346 {
12347 /* See if there's an instruction size override suffix,
12348 either `16' or `32', at the end of the mnemonic proper,
12349 that defines the operation, i.e. before the first `.'
12350 character if any. Strip it and retry. */
12351 opend = dot != NULL ? dot - name : length;
12352 if (opend >= 3 && name[opend - 2] == '1' && name[opend - 1] == '6')
12353 suffix = 2;
12354 else if (name[opend - 2] == '3' && name[opend - 1] == '2')
12355 suffix = 4;
12356 else
12357 suffix = 0;
12358 if (suffix)
12359 {
12360 memcpy (name + opend - 2, name + opend, length - opend + 1);
12361 insn = (struct mips_opcode *) hash_find (hash, name);
12362 if (insn && (insn->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) == 0)
12363 {
12364 forced_insn_length = suffix;
12365 return insn;
12366 }
12367 }
12368 }
12369
12370 return NULL;
12371}
12372
77bd4346
RS
12373/* Assemble an instruction into its binary format. If the instruction
12374 is a macro, set imm_expr, imm2_expr and offset_expr to the values
12375 associated with "I", "+I" and "A" operands respectively. Otherwise
12376 store the value of the relocatable field (if any) in offset_expr.
12377 In both cases set offset_reloc to the relocation operators applied
12378 to offset_expr. */
252b5132
RH
12379
12380static void
17a2f251 12381mips_ip (char *str, struct mips_cl_insn *ip)
252b5132 12382{
df58fc94
RS
12383 bfd_boolean wrong_delay_slot_insns = FALSE;
12384 bfd_boolean need_delay_slot_ok = TRUE;
12385 struct mips_opcode *firstinsn = NULL;
12386 const struct mips_opcode *past;
12387 struct hash_control *hash;
252b5132 12388 const char *args;
43841e91 12389 char c = 0;
14daeee3 12390 struct mips_opcode *first, *insn;
a92713e6 12391 char format;
14daeee3 12392 size_t end;
9e12b7a2
RS
12393 const struct mips_operand *operand;
12394 struct mips_arg_info arg;
a92713e6
RS
12395 struct mips_operand_token *tokens;
12396 bfd_boolean optional_reg;
14daeee3 12397 unsigned int opcode_extra;
252b5132
RH
12398
12399 insn_error = NULL;
12400
df58fc94
RS
12401 if (mips_opts.micromips)
12402 {
12403 hash = micromips_op_hash;
12404 past = &micromips_opcodes[bfd_micromips_num_opcodes];
12405 }
12406 else
12407 {
12408 hash = op_hash;
12409 past = &mips_opcodes[NUMOPCODES];
12410 }
12411 forced_insn_length = 0;
252b5132 12412 insn = NULL;
14daeee3 12413 opcode_extra = 0;
252b5132 12414
df58fc94 12415 /* We first try to match an instruction up to a space or to the end. */
a40bc9dd
RS
12416 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
12417 continue;
bdaaa2e1 12418
14daeee3 12419 first = insn = mips_lookup_insn (hash, str, end, &opcode_extra);
252b5132
RH
12420 if (insn == NULL)
12421 {
a40bc9dd
RS
12422 insn_error = _("Unrecognized opcode");
12423 return;
252b5132
RH
12424 }
12425
14daeee3 12426 if (strcmp (insn->name, "li.s") == 0)
a92713e6 12427 format = 'f';
14daeee3 12428 else if (strcmp (insn->name, "li.d") == 0)
a92713e6
RS
12429 format = 'd';
12430 else
12431 format = 0;
12432 tokens = mips_parse_arguments (str + end, format);
12433 if (!tokens)
12434 return;
12435
df58fc94
RS
12436 /* For microMIPS instructions placed in a fixed-length branch delay slot
12437 we make up to two passes over the relevant fragment of the opcode
12438 table. First we try instructions that meet the delay slot's length
12439 requirement. If none matched, then we retry with the remaining ones
12440 and if one matches, then we use it and then issue an appropriate
12441 warning later on. */
252b5132
RH
12442 for (;;)
12443 {
df58fc94
RS
12444 bfd_boolean delay_slot_ok;
12445 bfd_boolean size_ok;
b34976b6 12446 bfd_boolean ok;
9e12b7a2 12447 bfd_boolean more_alts;
252b5132 12448
14daeee3 12449 gas_assert (strcmp (insn->name, first->name) == 0);
252b5132 12450
f79e2745 12451 ok = is_opcode_valid (insn);
df58fc94
RS
12452 size_ok = is_size_valid (insn);
12453 delay_slot_ok = is_delay_slot_valid (insn);
12454 if (!delay_slot_ok && !wrong_delay_slot_insns)
252b5132 12455 {
df58fc94
RS
12456 firstinsn = insn;
12457 wrong_delay_slot_insns = TRUE;
12458 }
9e12b7a2
RS
12459 more_alts = (insn + 1 < past
12460 && strcmp (insn[0].name, insn[1].name) == 0);
df58fc94
RS
12461 if (!ok || !size_ok || delay_slot_ok != need_delay_slot_ok)
12462 {
12463 static char buf[256];
12464
9e12b7a2 12465 if (more_alts)
252b5132
RH
12466 {
12467 ++insn;
12468 continue;
12469 }
df58fc94 12470 if (wrong_delay_slot_insns && need_delay_slot_ok)
beae10d5 12471 {
df58fc94
RS
12472 gas_assert (firstinsn);
12473 need_delay_slot_ok = FALSE;
12474 past = insn + 1;
12475 insn = firstinsn;
12476 continue;
252b5132 12477 }
df58fc94 12478
a92713e6 12479 obstack_free (&mips_operand_tokens, tokens);
df58fc94
RS
12480 if (insn_error)
12481 return;
12482
12483 if (!ok)
7bd942df 12484 sprintf (buf, _("Opcode not supported on this processor: %s (%s)"),
df58fc94
RS
12485 mips_cpu_info_from_arch (mips_opts.arch)->name,
12486 mips_cpu_info_from_isa (mips_opts.isa)->name);
833794fc
MR
12487 else if (mips_opts.insn32)
12488 sprintf (buf, _("Opcode not supported in the `insn32' mode"));
df58fc94
RS
12489 else
12490 sprintf (buf, _("Unrecognized %u-bit version of microMIPS opcode"),
12491 8 * forced_insn_length);
12492 insn_error = buf;
12493
12494 return;
252b5132
RH
12495 }
12496
f2ae14a1
RS
12497 imm_expr.X_op = O_absent;
12498 imm2_expr.X_op = O_absent;
12499 offset_expr.X_op = O_absent;
f2ae14a1
RS
12500 offset_reloc[0] = BFD_RELOC_UNUSED;
12501 offset_reloc[1] = BFD_RELOC_UNUSED;
12502 offset_reloc[2] = BFD_RELOC_UNUSED;
12503
1e915849 12504 create_insn (ip, insn);
14daeee3 12505 ip->insn_opcode |= opcode_extra;
268f6bed 12506 insn_error = NULL;
9e12b7a2
RS
12507 memset (&arg, 0, sizeof (arg));
12508 arg.insn = ip;
a92713e6 12509 arg.token = tokens;
9e12b7a2
RS
12510 arg.argnum = 1;
12511 arg.last_regno = ILLEGAL_REG;
12512 arg.dest_regno = ILLEGAL_REG;
12513 arg.soft_match = (more_alts
12514 || (wrong_delay_slot_insns && need_delay_slot_ok));
252b5132
RH
12515 for (args = insn->args;; ++args)
12516 {
a92713e6 12517 if (arg.token->type == OT_END)
252b5132 12518 {
9e12b7a2
RS
12519 /* Handle unary instructions in which only one operand is given.
12520 The source is then the same as the destination. */
12521 if (arg.opnum == 1 && *args == ',')
12522 switch (args[1])
12523 {
12524 case 'r':
12525 case 'v':
12526 case 'w':
12527 case 'W':
12528 case 'V':
a92713e6 12529 arg.token = tokens;
9e12b7a2 12530 arg.argnum = 1;
9e12b7a2
RS
12531 continue;
12532 }
74cd071d 12533
9e12b7a2
RS
12534 /* Treat elided base registers as $0. */
12535 if (strcmp (args, "(b)") == 0)
12536 args += 3;
74cd071d 12537
14daeee3
RS
12538 if (args[0] == '+' && args[1] == 'K')
12539 args += 2;
12540
9e12b7a2
RS
12541 /* Fail the match if there were too few operands. */
12542 if (*args)
12543 break;
74cd071d 12544
9e12b7a2
RS
12545 /* Successful match. */
12546 if (arg.dest_regno == arg.last_regno
12547 && strncmp (ip->insn_mo->name, "jalr", 4) == 0)
74cd071d 12548 {
9e12b7a2
RS
12549 if (arg.opnum == 2)
12550 as_bad (_("Source and destination must be different"));
12551 else if (arg.last_regno == 31)
12552 as_bad (_("A destination register must be supplied"));
74cd071d 12553 }
9e12b7a2 12554 check_completed_insn (&arg);
a92713e6 12555 obstack_free (&mips_operand_tokens, tokens);
9e12b7a2
RS
12556 return;
12557 }
74cd071d 12558
9e12b7a2
RS
12559 /* Fail the match if the line has too many operands. */
12560 if (*args == 0)
12561 break;
12562
12563 /* Handle characters that need to match exactly. */
12564 if (*args == '(' || *args == ')' || *args == ',')
12565 {
a92713e6
RS
12566 if (match_char (&arg, *args))
12567 continue;
12568 break;
9e12b7a2 12569 }
14daeee3
RS
12570 if (*args == '#')
12571 {
12572 ++args;
12573 if (arg.token->type == OT_DOUBLE_CHAR
12574 && arg.token->u.ch == *args)
12575 {
12576 ++arg.token;
12577 continue;
12578 }
12579 break;
12580 }
74cd071d 12581
9e12b7a2
RS
12582 /* Handle special macro operands. Work out the properties of
12583 other operands. */
12584 arg.opnum += 1;
9e12b7a2 12585 arg.lax_max = FALSE;
a92713e6 12586 optional_reg = FALSE;
9e12b7a2
RS
12587 switch (*args)
12588 {
12589 case '+':
12590 switch (args[1])
74cd071d 12591 {
9e12b7a2
RS
12592 case '1':
12593 case '2':
12594 case '3':
12595 case '4':
12596 case 'B':
12597 case 'C':
12598 case 'F':
12599 case 'G':
12600 case 'H':
12601 case 'J':
12602 case 'Q':
12603 case 'S':
12604 case 's':
12605 /* If these integer forms come last, there is no other
12606 form of the instruction that could match. Prefer to
12607 give detailed error messages where possible. */
12608 if (args[2] == 0)
12609 arg.soft_match = FALSE;
12610 break;
12611
12612 case 'I':
12613 /* "+I" is like "I", except that imm2_expr is used. */
a92713e6
RS
12614 if (match_const_int (&arg, &imm2_expr.X_add_number, 0))
12615 imm2_expr.X_op = O_constant;
12616 else
12617 insn_error = _("absolute expression required");
9e12b7a2
RS
12618 if (HAVE_32BIT_GPRS)
12619 normalize_constant_expr (&imm2_expr);
9e12b7a2 12620 ++args;
74cd071d 12621 continue;
9e12b7a2
RS
12622
12623 case 'i':
12624 *offset_reloc = BFD_RELOC_MIPS_JMP;
12625 break;
74cd071d 12626 }
74cd071d
CF
12627 break;
12628
9e12b7a2
RS
12629 case '\'':
12630 case ':':
12631 case '@':
12632 case '^':
12633 case '$':
12634 case '\\':
12635 case '%':
12636 case '|':
03f66e8a 12637 case '0':
9e12b7a2
RS
12638 case '1':
12639 case '2':
12640 case '3':
12641 case '4':
12642 case '5':
12643 case '6':
12644 case '8':
12645 case 'B':
12646 case 'C':
12647 case 'J':
12648 case 'O':
12649 case 'P':
12650 case 'Q':
12651 case 'c':
12652 case 'h':
12653 case 'q':
12654 /* If these integer forms come last, there is no other
12655 form of the instruction that could match. Prefer to
12656 give detailed error messages where possible. */
12657 if (args[1] == 0)
12658 arg.soft_match = FALSE;
12659 break;
74cd071d 12660
9e12b7a2
RS
12661 case 'r':
12662 case 'v':
12663 case 'w':
12664 case 'W':
12665 case 'V':
12666 /* We have already matched a comma by this point, so the register
12667 is only optional if there is another operand to come. */
12668 gas_assert (arg.opnum == 2);
a92713e6 12669 optional_reg = (args[1] == ',');
9e12b7a2 12670 break;
ef2e4d86 12671
9e12b7a2 12672 case 'I':
a92713e6
RS
12673 if (match_const_int (&arg, &imm_expr.X_add_number, 0))
12674 imm_expr.X_op = O_constant;
12675 else
9e12b7a2
RS
12676 insn_error = _("absolute expression required");
12677 if (HAVE_32BIT_GPRS)
12678 normalize_constant_expr (&imm_expr);
ef2e4d86
CF
12679 continue;
12680
9e12b7a2 12681 case 'A':
a92713e6 12682 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
ef2e4d86 12683 {
9e12b7a2
RS
12684 /* Assume that the offset has been elided and that what
12685 we saw was a base register. The match will fail later
12686 if that assumption turns out to be wrong. */
12687 offset_expr.X_op = O_constant;
12688 offset_expr.X_add_number = 0;
ef2e4d86 12689 }
a92713e6
RS
12690 else if (match_expression (&arg, &offset_expr, offset_reloc))
12691 normalize_address_expr (&offset_expr);
ef2e4d86 12692 else
a92713e6 12693 insn_error = _("absolute expression required");
9e12b7a2 12694 continue;
ef2e4d86 12695
9e12b7a2 12696 case 'F':
a92713e6
RS
12697 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
12698 8, TRUE))
12699 insn_error = _("floating-point expression required");
89565f1b
RS
12700 continue;
12701
9e12b7a2 12702 case 'L':
a92713e6
RS
12703 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
12704 8, FALSE))
12705 insn_error = _("floating-point expression required");
89565f1b 12706 continue;
90ecf173 12707
89565f1b 12708 case 'f':
a92713e6
RS
12709 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
12710 4, TRUE))
12711 insn_error = _("floating-point expression required");
89565f1b 12712 continue;
b015e599 12713
89565f1b 12714 case 'l':
a92713e6
RS
12715 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
12716 4, FALSE))
12717 insn_error = _("floating-point expression required");
9e12b7a2 12718 continue;
252b5132 12719
9e12b7a2
RS
12720 /* ??? This is the traditional behavior, but is flaky if
12721 there are alternative versions of the same instruction
12722 for different subarchitectures. The next alternative
12723 might not be suitable. */
12724 case 'j':
12725 /* For compatibility with older assemblers, we accept
12726 0x8000-0xffff as signed 16-bit numbers when only
12727 signed numbers are allowed. */
12728 arg.lax_max = !more_alts;
12729 case 'i':
12730 /* Only accept non-constant operands if this is the
12731 final alternative. Later alternatives might include
12732 a macro implementation. */
12733 arg.allow_nonconst = !more_alts;
12734 break;
252b5132 12735
9e12b7a2
RS
12736 case 'u':
12737 /* There are no macro implementations for out-of-range values. */
12738 arg.allow_nonconst = TRUE;
12739 break;
60b63b72 12740
9e12b7a2
RS
12741 case 'o':
12742 /* There should always be a macro implementation. */
12743 arg.allow_nonconst = FALSE;
12744 break;
df58fc94 12745
9e12b7a2
RS
12746 case 'p':
12747 *offset_reloc = BFD_RELOC_16_PCREL_S2;
12748 break;
df58fc94 12749
9e12b7a2
RS
12750 case 'a':
12751 *offset_reloc = BFD_RELOC_MIPS_JMP;
df58fc94 12752 break;
60b63b72 12753
9e12b7a2 12754 case 'm':
df58fc94 12755 gas_assert (mips_opts.micromips);
9e12b7a2
RS
12756 c = args[1];
12757 switch (c)
12758 {
12759 case 't':
12760 case 'c':
12761 case 'e':
12762 /* We have already matched a comma by this point,
12763 so the register is only optional if there is another
12764 operand to come. */
12765 gas_assert (arg.opnum == 2);
a92713e6 12766 optional_reg = (args[2] == ',');
df58fc94
RS
12767 break;
12768
9e12b7a2
RS
12769 case 'D':
12770 case 'E':
12771 if (!forced_insn_length)
12772 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
12773 else if (c == 'D')
12774 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
12775 else
12776 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
df58fc94 12777 break;
9e12b7a2
RS
12778 }
12779 break;
12780 }
df58fc94 12781
9e12b7a2
RS
12782 operand = (mips_opts.micromips
12783 ? decode_micromips_operand (args)
12784 : decode_mips_operand (args));
12785 if (!operand)
12786 abort ();
df58fc94 12787
a92713e6
RS
12788 if (optional_reg
12789 && (arg.token[0].type != OT_REG
12790 || arg.token[1].type == OT_END))
9e12b7a2
RS
12791 {
12792 /* Assume that the register has been elided and is the
12793 same as the first operand. */
a92713e6 12794 arg.token = tokens;
9e12b7a2 12795 arg.argnum = 1;
9e12b7a2 12796 }
a92713e6
RS
12797
12798 if (!match_operand (&arg, operand))
9e12b7a2 12799 break;
60b63b72 12800
9e12b7a2
RS
12801 /* Skip prefixes. */
12802 if (*args == '+' || *args == 'm')
12803 args++;
60b63b72 12804
9e12b7a2 12805 continue;
252b5132
RH
12806 }
12807 /* Args don't match. */
df58fc94 12808 insn_error = _("Illegal operands");
9e12b7a2 12809 if (more_alts)
252b5132
RH
12810 {
12811 ++insn;
252b5132
RH
12812 continue;
12813 }
9e12b7a2 12814 if (wrong_delay_slot_insns && need_delay_slot_ok)
df58fc94
RS
12815 {
12816 gas_assert (firstinsn);
12817 need_delay_slot_ok = FALSE;
12818 past = insn + 1;
12819 insn = firstinsn;
12820 continue;
12821 }
a92713e6 12822 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
12823 return;
12824 }
12825}
12826
77bd4346
RS
12827/* As for mips_ip, but used when assembling MIPS16 code.
12828 Also set forced_insn_length to the resulting instruction size in
12829 bytes if the user explicitly requested a small or extended instruction. */
252b5132
RH
12830
12831static void
17a2f251 12832mips16_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
12833{
12834 char *s;
12835 const char *args;
12836 struct mips_opcode *insn;
364215c8
RS
12837 const struct mips_operand *operand;
12838 const struct mips_operand *ext_operand;
12839 struct mips_arg_info arg;
a92713e6
RS
12840 struct mips_operand_token *tokens;
12841 bfd_boolean optional_reg;
252b5132
RH
12842
12843 insn_error = NULL;
12844
df58fc94 12845 forced_insn_length = 0;
252b5132 12846
3882b010 12847 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
12848 ;
12849 switch (*s)
12850 {
12851 case '\0':
12852 break;
12853
12854 case ' ':
12855 *s++ = '\0';
12856 break;
12857
12858 case '.':
12859 if (s[1] == 't' && s[2] == ' ')
12860 {
12861 *s = '\0';
df58fc94 12862 forced_insn_length = 2;
252b5132
RH
12863 s += 3;
12864 break;
12865 }
12866 else if (s[1] == 'e' && s[2] == ' ')
12867 {
12868 *s = '\0';
df58fc94 12869 forced_insn_length = 4;
252b5132
RH
12870 s += 3;
12871 break;
12872 }
12873 /* Fall through. */
12874 default:
12875 insn_error = _("unknown opcode");
12876 return;
12877 }
12878
df58fc94
RS
12879 if (mips_opts.noautoextend && !forced_insn_length)
12880 forced_insn_length = 2;
252b5132
RH
12881
12882 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
12883 {
12884 insn_error = _("unrecognized opcode");
12885 return;
12886 }
12887
a92713e6
RS
12888 tokens = mips_parse_arguments (s, 0);
12889 if (!tokens)
12890 return;
12891
252b5132
RH
12892 for (;;)
12893 {
9b3f89ee 12894 bfd_boolean ok;
364215c8 12895 bfd_boolean more_alts;
77bd4346 12896 char relax_char;
9b3f89ee 12897
9c2799c2 12898 gas_assert (strcmp (insn->name, str) == 0);
252b5132 12899
037b32b9 12900 ok = is_opcode_valid_16 (insn);
364215c8
RS
12901 more_alts = (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes]
12902 && strcmp (insn[0].name, insn[1].name) == 0);
9b3f89ee
TS
12903 if (! ok)
12904 {
364215c8 12905 if (more_alts)
9b3f89ee
TS
12906 {
12907 ++insn;
12908 continue;
12909 }
12910 else
12911 {
12912 if (!insn_error)
12913 {
12914 static char buf[100];
12915 sprintf (buf,
7bd942df 12916 _("Opcode not supported on this processor: %s (%s)"),
9b3f89ee
TS
12917 mips_cpu_info_from_arch (mips_opts.arch)->name,
12918 mips_cpu_info_from_isa (mips_opts.isa)->name);
12919 insn_error = buf;
12920 }
a92713e6 12921 obstack_free (&mips_operand_tokens, tokens);
9b3f89ee
TS
12922 return;
12923 }
12924 }
12925
1e915849 12926 create_insn (ip, insn);
252b5132 12927 imm_expr.X_op = O_absent;
5f74bc13 12928 imm2_expr.X_op = O_absent;
252b5132 12929 offset_expr.X_op = O_absent;
f6688943
TS
12930 offset_reloc[0] = BFD_RELOC_UNUSED;
12931 offset_reloc[1] = BFD_RELOC_UNUSED;
12932 offset_reloc[2] = BFD_RELOC_UNUSED;
77bd4346 12933 relax_char = 0;
364215c8
RS
12934
12935 memset (&arg, 0, sizeof (arg));
12936 arg.insn = ip;
a92713e6 12937 arg.token = tokens;
364215c8
RS
12938 arg.argnum = 1;
12939 arg.last_regno = ILLEGAL_REG;
12940 arg.dest_regno = ILLEGAL_REG;
12941 arg.soft_match = more_alts;
12942 relax_char = 0;
252b5132
RH
12943 for (args = insn->args; 1; ++args)
12944 {
12945 int c;
12946
a92713e6 12947 if (arg.token->type == OT_END)
252b5132 12948 {
364215c8 12949 offsetT value;
252b5132 12950
364215c8
RS
12951 /* Handle unary instructions in which only one operand is given.
12952 The source is then the same as the destination. */
12953 if (arg.opnum == 1 && *args == ',')
12954 switch (args[1])
12955 {
12956 case 'v':
12957 case 'w':
a92713e6 12958 arg.token = tokens;
364215c8 12959 arg.argnum = 1;
364215c8
RS
12960 continue;
12961 }
252b5132 12962
364215c8
RS
12963 /* Fail the match if there were too few operands. */
12964 if (*args)
12965 break;
252b5132 12966
364215c8
RS
12967 /* Successful match. Stuff the immediate value in now, if
12968 we can. */
12969 if (insn->pinfo == INSN_MACRO)
252b5132 12970 {
0acfaea6 12971 gas_assert (relax_char == 0 || relax_char == 'p');
364215c8 12972 gas_assert (*offset_reloc == BFD_RELOC_UNUSED);
252b5132 12973 }
364215c8
RS
12974 else if (relax_char
12975 && offset_expr.X_op == O_constant
12976 && calculate_reloc (*offset_reloc,
12977 offset_expr.X_add_number,
12978 &value))
252b5132 12979 {
364215c8
RS
12980 mips16_immed (NULL, 0, relax_char, *offset_reloc, value,
12981 forced_insn_length, &ip->insn_opcode);
12982 offset_expr.X_op = O_absent;
12983 *offset_reloc = BFD_RELOC_UNUSED;
252b5132 12984 }
364215c8 12985 else if (relax_char && *offset_reloc != BFD_RELOC_UNUSED)
252b5132 12986 {
364215c8
RS
12987 if (forced_insn_length == 2)
12988 as_bad (_("invalid unextended operand value"));
12989 forced_insn_length = 4;
12990 ip->insn_opcode |= MIPS16_EXTEND;
252b5132 12991 }
364215c8
RS
12992 else if (relax_char)
12993 *offset_reloc = (int) BFD_RELOC_UNUSED + relax_char;
252b5132 12994
364215c8 12995 check_completed_insn (&arg);
a92713e6 12996 obstack_free (&mips_operand_tokens, tokens);
364215c8
RS
12997 return;
12998 }
252b5132 12999
364215c8
RS
13000 /* Fail the match if the line has too many operands. */
13001 if (*args == 0)
13002 break;
252b5132 13003
364215c8
RS
13004 /* Handle characters that need to match exactly. */
13005 if (*args == '(' || *args == ')' || *args == ',')
13006 {
a92713e6
RS
13007 if (match_char (&arg, *args))
13008 continue;
13009 break;
364215c8 13010 }
252b5132 13011
364215c8 13012 arg.opnum += 1;
a92713e6 13013 optional_reg = FALSE;
364215c8
RS
13014 c = *args;
13015 switch (c)
13016 {
13017 case 'v':
13018 case 'w':
a92713e6 13019 optional_reg = (args[1] == ',');
252b5132
RH
13020 break;
13021
252b5132
RH
13022 case 'p':
13023 case 'q':
13024 case 'A':
13025 case 'B':
13026 case 'E':
77bd4346 13027 relax_char = c;
364215c8 13028 break;
252b5132 13029
cc537e56 13030 case 'I':
a92713e6
RS
13031 if (match_const_int (&arg, &imm_expr.X_add_number, 0))
13032 imm_expr.X_op = O_constant;
13033 else
cc537e56
RS
13034 insn_error = _("absolute expression required");
13035 if (HAVE_32BIT_GPRS)
13036 normalize_constant_expr (&imm_expr);
cc537e56
RS
13037 continue;
13038
364215c8 13039 case 'a':
27c5c572 13040 case 'i':
f6688943 13041 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132 13042 ip->insn_opcode <<= 16;
364215c8
RS
13043 break;
13044 }
0499d65b 13045
364215c8
RS
13046 operand = decode_mips16_operand (c, FALSE);
13047 if (!operand)
13048 abort ();
0499d65b 13049
364215c8
RS
13050 /* '6' is a special case. It is used for BREAK and SDBBP,
13051 whose operands are only meaningful to the software that decodes
13052 them. This means that there is no architectural reason why
13053 they cannot be prefixed by EXTEND, but in practice,
13054 exception handlers will only look at the instruction
13055 itself. We therefore allow '6' to be extended when
13056 disassembling but not when assembling. */
13057 if (operand->type != OP_PCREL && c != '6')
13058 {
13059 ext_operand = decode_mips16_operand (c, TRUE);
13060 if (operand != ext_operand)
252b5132 13061 {
a92713e6 13062 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
364215c8 13063 {
a92713e6
RS
13064 offset_expr.X_op = O_constant;
13065 offset_expr.X_add_number = 0;
13066 relax_char = c;
13067 continue;
364215c8 13068 }
a92713e6
RS
13069
13070 /* We need the OT_INTEGER check because some MIPS16
13071 immediate variants are listed before the register ones. */
13072 if (arg.token->type != OT_INTEGER
13073 || !match_expression (&arg, &offset_expr, offset_reloc))
13074 break;
13075
364215c8
RS
13076 /* '8' is used for SLTI(U) and has traditionally not
13077 been allowed to take relocation operators. */
a92713e6
RS
13078 if (offset_reloc[0] != BFD_RELOC_UNUSED
13079 && (ext_operand->size != 16 || c == '8'))
364215c8 13080 break;
a92713e6 13081
364215c8
RS
13082 relax_char = c;
13083 continue;
252b5132 13084 }
364215c8 13085 }
252b5132 13086
a92713e6
RS
13087 if (optional_reg
13088 && (arg.token[0].type != OT_REG
13089 || arg.token[1].type == OT_END))
364215c8
RS
13090 {
13091 /* Assume that the register has been elided and is the
13092 same as the first operand. */
a92713e6 13093 arg.token = tokens;
364215c8 13094 arg.argnum = 1;
252b5132 13095 }
a92713e6
RS
13096
13097 if (!match_operand (&arg, operand))
364215c8
RS
13098 break;
13099 continue;
252b5132
RH
13100 }
13101
13102 /* Args don't match. */
364215c8 13103 if (more_alts)
252b5132
RH
13104 {
13105 ++insn;
252b5132
RH
13106 continue;
13107 }
13108
13109 insn_error = _("illegal operands");
13110
a92713e6 13111 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
13112 return;
13113 }
13114}
13115
b886a2ab
RS
13116/* Marshal immediate value VAL for an extended MIPS16 instruction.
13117 NBITS is the number of significant bits in VAL. */
13118
13119static unsigned long
13120mips16_immed_extend (offsetT val, unsigned int nbits)
13121{
13122 int extval;
13123 if (nbits == 16)
13124 {
13125 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
13126 val &= 0x1f;
13127 }
13128 else if (nbits == 15)
13129 {
13130 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
13131 val &= 0xf;
13132 }
13133 else
13134 {
13135 extval = ((val & 0x1f) << 6) | (val & 0x20);
13136 val = 0;
13137 }
13138 return (extval << 16) | val;
13139}
13140
3ccad066
RS
13141/* Like decode_mips16_operand, but require the operand to be defined and
13142 require it to be an integer. */
13143
13144static const struct mips_int_operand *
13145mips16_immed_operand (int type, bfd_boolean extended_p)
13146{
13147 const struct mips_operand *operand;
13148
13149 operand = decode_mips16_operand (type, extended_p);
13150 if (!operand || (operand->type != OP_INT && operand->type != OP_PCREL))
13151 abort ();
13152 return (const struct mips_int_operand *) operand;
13153}
13154
13155/* Return true if SVAL fits OPERAND. RELOC is as for mips16_immed. */
13156
13157static bfd_boolean
13158mips16_immed_in_range_p (const struct mips_int_operand *operand,
13159 bfd_reloc_code_real_type reloc, offsetT sval)
13160{
13161 int min_val, max_val;
13162
13163 min_val = mips_int_operand_min (operand);
13164 max_val = mips_int_operand_max (operand);
13165 if (reloc != BFD_RELOC_UNUSED)
13166 {
13167 if (min_val < 0)
13168 sval = SEXT_16BIT (sval);
13169 else
13170 sval &= 0xffff;
13171 }
13172
13173 return (sval >= min_val
13174 && sval <= max_val
13175 && (sval & ((1 << operand->shift) - 1)) == 0);
13176}
13177
5c04167a
RS
13178/* Install immediate value VAL into MIPS16 instruction *INSN,
13179 extending it if necessary. The instruction in *INSN may
13180 already be extended.
13181
43c0598f
RS
13182 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
13183 if none. In the former case, VAL is a 16-bit number with no
13184 defined signedness.
13185
13186 TYPE is the type of the immediate field. USER_INSN_LENGTH
13187 is the length that the user requested, or 0 if none. */
252b5132
RH
13188
13189static void
43c0598f
RS
13190mips16_immed (char *file, unsigned int line, int type,
13191 bfd_reloc_code_real_type reloc, offsetT val,
5c04167a 13192 unsigned int user_insn_length, unsigned long *insn)
252b5132 13193{
3ccad066
RS
13194 const struct mips_int_operand *operand;
13195 unsigned int uval, length;
252b5132 13196
3ccad066
RS
13197 operand = mips16_immed_operand (type, FALSE);
13198 if (!mips16_immed_in_range_p (operand, reloc, val))
5c04167a
RS
13199 {
13200 /* We need an extended instruction. */
13201 if (user_insn_length == 2)
13202 as_bad_where (file, line, _("invalid unextended operand value"));
13203 else
13204 *insn |= MIPS16_EXTEND;
13205 }
13206 else if (user_insn_length == 4)
13207 {
13208 /* The operand doesn't force an unextended instruction to be extended.
13209 Warn if the user wanted an extended instruction anyway. */
13210 *insn |= MIPS16_EXTEND;
13211 as_warn_where (file, line,
13212 _("extended operand requested but not required"));
13213 }
252b5132 13214
3ccad066
RS
13215 length = mips16_opcode_length (*insn);
13216 if (length == 4)
252b5132 13217 {
3ccad066
RS
13218 operand = mips16_immed_operand (type, TRUE);
13219 if (!mips16_immed_in_range_p (operand, reloc, val))
13220 as_bad_where (file, line,
13221 _("operand value out of range for instruction"));
252b5132 13222 }
3ccad066
RS
13223 uval = ((unsigned int) val >> operand->shift) - operand->bias;
13224 if (length == 2)
13225 *insn = mips_insert_operand (&operand->root, *insn, uval);
252b5132 13226 else
3ccad066 13227 *insn |= mips16_immed_extend (uval, operand->root.size);
252b5132
RH
13228}
13229\f
d6f16593 13230struct percent_op_match
ad8d3bb3 13231{
5e0116d5
RS
13232 const char *str;
13233 bfd_reloc_code_real_type reloc;
d6f16593
MR
13234};
13235
13236static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 13237{
5e0116d5 13238 {"%lo", BFD_RELOC_LO16},
5e0116d5
RS
13239 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
13240 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
13241 {"%call16", BFD_RELOC_MIPS_CALL16},
13242 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
13243 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
13244 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
13245 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
13246 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
13247 {"%got", BFD_RELOC_MIPS_GOT16},
13248 {"%gp_rel", BFD_RELOC_GPREL16},
13249 {"%half", BFD_RELOC_16},
13250 {"%highest", BFD_RELOC_MIPS_HIGHEST},
13251 {"%higher", BFD_RELOC_MIPS_HIGHER},
13252 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
13253 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
13254 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
13255 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
13256 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
13257 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
13258 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
13259 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
5e0116d5 13260 {"%hi", BFD_RELOC_HI16_S}
ad8d3bb3
TS
13261};
13262
d6f16593
MR
13263static const struct percent_op_match mips16_percent_op[] =
13264{
13265 {"%lo", BFD_RELOC_MIPS16_LO16},
13266 {"%gprel", BFD_RELOC_MIPS16_GPREL},
738e5348
RS
13267 {"%got", BFD_RELOC_MIPS16_GOT16},
13268 {"%call16", BFD_RELOC_MIPS16_CALL16},
d0f13682
CLT
13269 {"%hi", BFD_RELOC_MIPS16_HI16_S},
13270 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
13271 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
13272 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
13273 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
13274 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
13275 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
13276 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
d6f16593
MR
13277};
13278
252b5132 13279
5e0116d5
RS
13280/* Return true if *STR points to a relocation operator. When returning true,
13281 move *STR over the operator and store its relocation code in *RELOC.
13282 Leave both *STR and *RELOC alone when returning false. */
13283
13284static bfd_boolean
17a2f251 13285parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 13286{
d6f16593
MR
13287 const struct percent_op_match *percent_op;
13288 size_t limit, i;
13289
13290 if (mips_opts.mips16)
13291 {
13292 percent_op = mips16_percent_op;
13293 limit = ARRAY_SIZE (mips16_percent_op);
13294 }
13295 else
13296 {
13297 percent_op = mips_percent_op;
13298 limit = ARRAY_SIZE (mips_percent_op);
13299 }
76b3015f 13300
d6f16593 13301 for (i = 0; i < limit; i++)
5e0116d5 13302 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 13303 {
3f98094e
DJ
13304 int len = strlen (percent_op[i].str);
13305
13306 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
13307 continue;
13308
5e0116d5
RS
13309 *str += strlen (percent_op[i].str);
13310 *reloc = percent_op[i].reloc;
394f9b3a 13311
5e0116d5
RS
13312 /* Check whether the output BFD supports this relocation.
13313 If not, issue an error and fall back on something safe. */
13314 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 13315 {
20203fb9 13316 as_bad (_("relocation %s isn't supported by the current ABI"),
5e0116d5 13317 percent_op[i].str);
01a3f561 13318 *reloc = BFD_RELOC_UNUSED;
394f9b3a 13319 }
5e0116d5 13320 return TRUE;
394f9b3a 13321 }
5e0116d5 13322 return FALSE;
394f9b3a 13323}
ad8d3bb3 13324
ad8d3bb3 13325
5e0116d5
RS
13326/* Parse string STR as a 16-bit relocatable operand. Store the
13327 expression in *EP and the relocations in the array starting
13328 at RELOC. Return the number of relocation operators used.
ad8d3bb3 13329
01a3f561 13330 On exit, EXPR_END points to the first character after the expression. */
ad8d3bb3 13331
5e0116d5 13332static size_t
17a2f251
TS
13333my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
13334 char *str)
ad8d3bb3 13335{
5e0116d5
RS
13336 bfd_reloc_code_real_type reversed_reloc[3];
13337 size_t reloc_index, i;
09b8f35a
RS
13338 int crux_depth, str_depth;
13339 char *crux;
5e0116d5
RS
13340
13341 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
13342 in REVERSED_RELOC. End the loop with CRUX pointing to the start
13343 of the main expression and with CRUX_DEPTH containing the number
13344 of open brackets at that point. */
13345 reloc_index = -1;
13346 str_depth = 0;
13347 do
fb1b3232 13348 {
09b8f35a
RS
13349 reloc_index++;
13350 crux = str;
13351 crux_depth = str_depth;
13352
13353 /* Skip over whitespace and brackets, keeping count of the number
13354 of brackets. */
13355 while (*str == ' ' || *str == '\t' || *str == '(')
13356 if (*str++ == '(')
13357 str_depth++;
5e0116d5 13358 }
09b8f35a
RS
13359 while (*str == '%'
13360 && reloc_index < (HAVE_NEWABI ? 3 : 1)
13361 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 13362
09b8f35a 13363 my_getExpression (ep, crux);
5e0116d5 13364 str = expr_end;
394f9b3a 13365
5e0116d5 13366 /* Match every open bracket. */
09b8f35a 13367 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 13368 if (*str++ == ')')
09b8f35a 13369 crux_depth--;
394f9b3a 13370
09b8f35a 13371 if (crux_depth > 0)
20203fb9 13372 as_bad (_("unclosed '('"));
394f9b3a 13373
5e0116d5 13374 expr_end = str;
252b5132 13375
01a3f561 13376 if (reloc_index != 0)
64bdfcaf
RS
13377 {
13378 prev_reloc_op_frag = frag_now;
13379 for (i = 0; i < reloc_index; i++)
13380 reloc[i] = reversed_reloc[reloc_index - 1 - i];
13381 }
fb1b3232 13382
5e0116d5 13383 return reloc_index;
252b5132
RH
13384}
13385
13386static void
17a2f251 13387my_getExpression (expressionS *ep, char *str)
252b5132
RH
13388{
13389 char *save_in;
13390
13391 save_in = input_line_pointer;
13392 input_line_pointer = str;
13393 expression (ep);
13394 expr_end = input_line_pointer;
13395 input_line_pointer = save_in;
252b5132
RH
13396}
13397
252b5132 13398char *
17a2f251 13399md_atof (int type, char *litP, int *sizeP)
252b5132 13400{
499ac353 13401 return ieee_md_atof (type, litP, sizeP, target_big_endian);
252b5132
RH
13402}
13403
13404void
17a2f251 13405md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
13406{
13407 if (target_big_endian)
13408 number_to_chars_bigendian (buf, val, n);
13409 else
13410 number_to_chars_littleendian (buf, val, n);
13411}
13412\f
e013f690
TS
13413static int support_64bit_objects(void)
13414{
13415 const char **list, **l;
aa3d8fdf 13416 int yes;
e013f690
TS
13417
13418 list = bfd_target_list ();
13419 for (l = list; *l != NULL; l++)
aeffff67
RS
13420 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
13421 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
e013f690 13422 break;
aa3d8fdf 13423 yes = (*l != NULL);
e013f690 13424 free (list);
aa3d8fdf 13425 return yes;
e013f690
TS
13426}
13427
316f5878
RS
13428/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
13429 NEW_VALUE. Warn if another value was already specified. Note:
13430 we have to defer parsing the -march and -mtune arguments in order
13431 to handle 'from-abi' correctly, since the ABI might be specified
13432 in a later argument. */
13433
13434static void
17a2f251 13435mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
13436{
13437 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
13438 as_warn (_("A different %s was already specified, is now %s"),
13439 string_ptr == &mips_arch_string ? "-march" : "-mtune",
13440 new_value);
13441
13442 *string_ptr = new_value;
13443}
13444
252b5132 13445int
17a2f251 13446md_parse_option (int c, char *arg)
252b5132 13447{
c6278170
RS
13448 unsigned int i;
13449
13450 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
13451 if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
13452 {
13453 file_ase_explicit |= mips_set_ase (&mips_ases[i],
13454 c == mips_ases[i].option_on);
13455 return 1;
13456 }
13457
252b5132
RH
13458 switch (c)
13459 {
119d663a
NC
13460 case OPTION_CONSTRUCT_FLOATS:
13461 mips_disable_float_construction = 0;
13462 break;
bdaaa2e1 13463
119d663a
NC
13464 case OPTION_NO_CONSTRUCT_FLOATS:
13465 mips_disable_float_construction = 1;
13466 break;
bdaaa2e1 13467
252b5132
RH
13468 case OPTION_TRAP:
13469 mips_trap = 1;
13470 break;
13471
13472 case OPTION_BREAK:
13473 mips_trap = 0;
13474 break;
13475
13476 case OPTION_EB:
13477 target_big_endian = 1;
13478 break;
13479
13480 case OPTION_EL:
13481 target_big_endian = 0;
13482 break;
13483
13484 case 'O':
4ffff32f
TS
13485 if (arg == NULL)
13486 mips_optimize = 1;
13487 else if (arg[0] == '0')
13488 mips_optimize = 0;
13489 else if (arg[0] == '1')
252b5132
RH
13490 mips_optimize = 1;
13491 else
13492 mips_optimize = 2;
13493 break;
13494
13495 case 'g':
13496 if (arg == NULL)
13497 mips_debug = 2;
13498 else
13499 mips_debug = atoi (arg);
252b5132
RH
13500 break;
13501
13502 case OPTION_MIPS1:
316f5878 13503 file_mips_isa = ISA_MIPS1;
252b5132
RH
13504 break;
13505
13506 case OPTION_MIPS2:
316f5878 13507 file_mips_isa = ISA_MIPS2;
252b5132
RH
13508 break;
13509
13510 case OPTION_MIPS3:
316f5878 13511 file_mips_isa = ISA_MIPS3;
252b5132
RH
13512 break;
13513
13514 case OPTION_MIPS4:
316f5878 13515 file_mips_isa = ISA_MIPS4;
e7af610e
NC
13516 break;
13517
84ea6cf2 13518 case OPTION_MIPS5:
316f5878 13519 file_mips_isa = ISA_MIPS5;
84ea6cf2
NC
13520 break;
13521
e7af610e 13522 case OPTION_MIPS32:
316f5878 13523 file_mips_isa = ISA_MIPS32;
252b5132
RH
13524 break;
13525
af7ee8bf
CD
13526 case OPTION_MIPS32R2:
13527 file_mips_isa = ISA_MIPS32R2;
13528 break;
13529
5f74bc13
CD
13530 case OPTION_MIPS64R2:
13531 file_mips_isa = ISA_MIPS64R2;
13532 break;
13533
84ea6cf2 13534 case OPTION_MIPS64:
316f5878 13535 file_mips_isa = ISA_MIPS64;
84ea6cf2
NC
13536 break;
13537
ec68c924 13538 case OPTION_MTUNE:
316f5878
RS
13539 mips_set_option_string (&mips_tune_string, arg);
13540 break;
ec68c924 13541
316f5878
RS
13542 case OPTION_MARCH:
13543 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
13544 break;
13545
13546 case OPTION_M4650:
316f5878
RS
13547 mips_set_option_string (&mips_arch_string, "4650");
13548 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
13549 break;
13550
13551 case OPTION_NO_M4650:
13552 break;
13553
13554 case OPTION_M4010:
316f5878
RS
13555 mips_set_option_string (&mips_arch_string, "4010");
13556 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
13557 break;
13558
13559 case OPTION_NO_M4010:
13560 break;
13561
13562 case OPTION_M4100:
316f5878
RS
13563 mips_set_option_string (&mips_arch_string, "4100");
13564 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
13565 break;
13566
13567 case OPTION_NO_M4100:
13568 break;
13569
252b5132 13570 case OPTION_M3900:
316f5878
RS
13571 mips_set_option_string (&mips_arch_string, "3900");
13572 mips_set_option_string (&mips_tune_string, "3900");
252b5132 13573 break;
bdaaa2e1 13574
252b5132
RH
13575 case OPTION_NO_M3900:
13576 break;
13577
df58fc94
RS
13578 case OPTION_MICROMIPS:
13579 if (mips_opts.mips16 == 1)
13580 {
13581 as_bad (_("-mmicromips cannot be used with -mips16"));
13582 return 0;
13583 }
13584 mips_opts.micromips = 1;
13585 mips_no_prev_insn ();
13586 break;
13587
13588 case OPTION_NO_MICROMIPS:
13589 mips_opts.micromips = 0;
13590 mips_no_prev_insn ();
13591 break;
13592
252b5132 13593 case OPTION_MIPS16:
df58fc94
RS
13594 if (mips_opts.micromips == 1)
13595 {
13596 as_bad (_("-mips16 cannot be used with -micromips"));
13597 return 0;
13598 }
252b5132 13599 mips_opts.mips16 = 1;
7d10b47d 13600 mips_no_prev_insn ();
252b5132
RH
13601 break;
13602
13603 case OPTION_NO_MIPS16:
13604 mips_opts.mips16 = 0;
7d10b47d 13605 mips_no_prev_insn ();
252b5132
RH
13606 break;
13607
6a32d874
CM
13608 case OPTION_FIX_24K:
13609 mips_fix_24k = 1;
13610 break;
13611
13612 case OPTION_NO_FIX_24K:
13613 mips_fix_24k = 0;
13614 break;
13615
c67a084a
NC
13616 case OPTION_FIX_LOONGSON2F_JUMP:
13617 mips_fix_loongson2f_jump = TRUE;
13618 break;
13619
13620 case OPTION_NO_FIX_LOONGSON2F_JUMP:
13621 mips_fix_loongson2f_jump = FALSE;
13622 break;
13623
13624 case OPTION_FIX_LOONGSON2F_NOP:
13625 mips_fix_loongson2f_nop = TRUE;
13626 break;
13627
13628 case OPTION_NO_FIX_LOONGSON2F_NOP:
13629 mips_fix_loongson2f_nop = FALSE;
13630 break;
13631
d766e8ec
RS
13632 case OPTION_FIX_VR4120:
13633 mips_fix_vr4120 = 1;
60b63b72
RS
13634 break;
13635
d766e8ec
RS
13636 case OPTION_NO_FIX_VR4120:
13637 mips_fix_vr4120 = 0;
60b63b72
RS
13638 break;
13639
7d8e00cf
RS
13640 case OPTION_FIX_VR4130:
13641 mips_fix_vr4130 = 1;
13642 break;
13643
13644 case OPTION_NO_FIX_VR4130:
13645 mips_fix_vr4130 = 0;
13646 break;
13647
d954098f
DD
13648 case OPTION_FIX_CN63XXP1:
13649 mips_fix_cn63xxp1 = TRUE;
13650 break;
13651
13652 case OPTION_NO_FIX_CN63XXP1:
13653 mips_fix_cn63xxp1 = FALSE;
13654 break;
13655
4a6a3df4
AO
13656 case OPTION_RELAX_BRANCH:
13657 mips_relax_branch = 1;
13658 break;
13659
13660 case OPTION_NO_RELAX_BRANCH:
13661 mips_relax_branch = 0;
13662 break;
13663
833794fc
MR
13664 case OPTION_INSN32:
13665 mips_opts.insn32 = TRUE;
13666 break;
13667
13668 case OPTION_NO_INSN32:
13669 mips_opts.insn32 = FALSE;
13670 break;
13671
aa6975fb
ILT
13672 case OPTION_MSHARED:
13673 mips_in_shared = TRUE;
13674 break;
13675
13676 case OPTION_MNO_SHARED:
13677 mips_in_shared = FALSE;
13678 break;
13679
aed1a261
RS
13680 case OPTION_MSYM32:
13681 mips_opts.sym32 = TRUE;
13682 break;
13683
13684 case OPTION_MNO_SYM32:
13685 mips_opts.sym32 = FALSE;
13686 break;
13687
252b5132
RH
13688 /* When generating ELF code, we permit -KPIC and -call_shared to
13689 select SVR4_PIC, and -non_shared to select no PIC. This is
13690 intended to be compatible with Irix 5. */
13691 case OPTION_CALL_SHARED:
252b5132 13692 mips_pic = SVR4_PIC;
143d77c5 13693 mips_abicalls = TRUE;
252b5132
RH
13694 break;
13695
861fb55a 13696 case OPTION_CALL_NONPIC:
861fb55a
DJ
13697 mips_pic = NO_PIC;
13698 mips_abicalls = TRUE;
13699 break;
13700
252b5132 13701 case OPTION_NON_SHARED:
252b5132 13702 mips_pic = NO_PIC;
143d77c5 13703 mips_abicalls = FALSE;
252b5132
RH
13704 break;
13705
44075ae2
TS
13706 /* The -xgot option tells the assembler to use 32 bit offsets
13707 when accessing the got in SVR4_PIC mode. It is for Irix
252b5132
RH
13708 compatibility. */
13709 case OPTION_XGOT:
13710 mips_big_got = 1;
13711 break;
13712
13713 case 'G':
6caf9ef4
TS
13714 g_switch_value = atoi (arg);
13715 g_switch_seen = 1;
252b5132
RH
13716 break;
13717
34ba82a8
TS
13718 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
13719 and -mabi=64. */
252b5132 13720 case OPTION_32:
f3ded42a 13721 mips_abi = O32_ABI;
252b5132
RH
13722 break;
13723
e013f690 13724 case OPTION_N32:
316f5878 13725 mips_abi = N32_ABI;
e013f690 13726 break;
252b5132 13727
e013f690 13728 case OPTION_64:
316f5878 13729 mips_abi = N64_ABI;
f43abd2b 13730 if (!support_64bit_objects())
e013f690 13731 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132
RH
13732 break;
13733
c97ef257 13734 case OPTION_GP32:
a325df1d 13735 file_mips_gp32 = 1;
c97ef257
AH
13736 break;
13737
13738 case OPTION_GP64:
a325df1d 13739 file_mips_gp32 = 0;
c97ef257 13740 break;
252b5132 13741
ca4e0257 13742 case OPTION_FP32:
a325df1d 13743 file_mips_fp32 = 1;
316f5878
RS
13744 break;
13745
13746 case OPTION_FP64:
13747 file_mips_fp32 = 0;
ca4e0257
RS
13748 break;
13749
037b32b9
AN
13750 case OPTION_SINGLE_FLOAT:
13751 file_mips_single_float = 1;
13752 break;
13753
13754 case OPTION_DOUBLE_FLOAT:
13755 file_mips_single_float = 0;
13756 break;
13757
13758 case OPTION_SOFT_FLOAT:
13759 file_mips_soft_float = 1;
13760 break;
13761
13762 case OPTION_HARD_FLOAT:
13763 file_mips_soft_float = 0;
13764 break;
13765
252b5132 13766 case OPTION_MABI:
e013f690 13767 if (strcmp (arg, "32") == 0)
316f5878 13768 mips_abi = O32_ABI;
e013f690 13769 else if (strcmp (arg, "o64") == 0)
316f5878 13770 mips_abi = O64_ABI;
e013f690 13771 else if (strcmp (arg, "n32") == 0)
316f5878 13772 mips_abi = N32_ABI;
e013f690
TS
13773 else if (strcmp (arg, "64") == 0)
13774 {
316f5878 13775 mips_abi = N64_ABI;
e013f690
TS
13776 if (! support_64bit_objects())
13777 as_fatal (_("No compiled in support for 64 bit object file "
13778 "format"));
13779 }
13780 else if (strcmp (arg, "eabi") == 0)
316f5878 13781 mips_abi = EABI_ABI;
e013f690 13782 else
da0e507f
TS
13783 {
13784 as_fatal (_("invalid abi -mabi=%s"), arg);
13785 return 0;
13786 }
252b5132
RH
13787 break;
13788
6b76fefe 13789 case OPTION_M7000_HILO_FIX:
b34976b6 13790 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
13791 break;
13792
9ee72ff1 13793 case OPTION_MNO_7000_HILO_FIX:
b34976b6 13794 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
13795 break;
13796
ecb4347a 13797 case OPTION_MDEBUG:
b34976b6 13798 mips_flag_mdebug = TRUE;
ecb4347a
DJ
13799 break;
13800
13801 case OPTION_NO_MDEBUG:
b34976b6 13802 mips_flag_mdebug = FALSE;
ecb4347a 13803 break;
dcd410fe
RO
13804
13805 case OPTION_PDR:
13806 mips_flag_pdr = TRUE;
13807 break;
13808
13809 case OPTION_NO_PDR:
13810 mips_flag_pdr = FALSE;
13811 break;
0a44bf69
RS
13812
13813 case OPTION_MVXWORKS_PIC:
13814 mips_pic = VXWORKS_PIC;
13815 break;
ecb4347a 13816
ba92f887
MR
13817 case OPTION_NAN:
13818 if (strcmp (arg, "2008") == 0)
13819 mips_flag_nan2008 = TRUE;
13820 else if (strcmp (arg, "legacy") == 0)
13821 mips_flag_nan2008 = FALSE;
13822 else
13823 {
13824 as_fatal (_("Invalid NaN setting -mnan=%s"), arg);
13825 return 0;
13826 }
13827 break;
13828
252b5132
RH
13829 default:
13830 return 0;
13831 }
13832
c67a084a
NC
13833 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
13834
252b5132
RH
13835 return 1;
13836}
316f5878
RS
13837\f
13838/* Set up globals to generate code for the ISA or processor
13839 described by INFO. */
252b5132 13840
252b5132 13841static void
17a2f251 13842mips_set_architecture (const struct mips_cpu_info *info)
252b5132 13843{
316f5878 13844 if (info != 0)
252b5132 13845 {
fef14a42
TS
13846 file_mips_arch = info->cpu;
13847 mips_opts.arch = info->cpu;
316f5878 13848 mips_opts.isa = info->isa;
252b5132 13849 }
252b5132
RH
13850}
13851
252b5132 13852
316f5878 13853/* Likewise for tuning. */
252b5132 13854
316f5878 13855static void
17a2f251 13856mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
13857{
13858 if (info != 0)
fef14a42 13859 mips_tune = info->cpu;
316f5878 13860}
80cc45a5 13861
34ba82a8 13862
252b5132 13863void
17a2f251 13864mips_after_parse_args (void)
e9670677 13865{
fef14a42
TS
13866 const struct mips_cpu_info *arch_info = 0;
13867 const struct mips_cpu_info *tune_info = 0;
13868
e9670677 13869 /* GP relative stuff not working for PE */
6caf9ef4 13870 if (strncmp (TARGET_OS, "pe", 2) == 0)
e9670677 13871 {
6caf9ef4 13872 if (g_switch_seen && g_switch_value != 0)
e9670677
MR
13873 as_bad (_("-G not supported in this configuration."));
13874 g_switch_value = 0;
13875 }
13876
cac012d6
AO
13877 if (mips_abi == NO_ABI)
13878 mips_abi = MIPS_DEFAULT_ABI;
13879
22923709
RS
13880 /* The following code determines the architecture and register size.
13881 Similar code was added to GCC 3.3 (see override_options() in
13882 config/mips/mips.c). The GAS and GCC code should be kept in sync
13883 as much as possible. */
e9670677 13884
316f5878 13885 if (mips_arch_string != 0)
fef14a42 13886 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 13887
316f5878 13888 if (file_mips_isa != ISA_UNKNOWN)
e9670677 13889 {
316f5878 13890 /* Handle -mipsN. At this point, file_mips_isa contains the
fef14a42 13891 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 13892 the -march selection (if any). */
fef14a42 13893 if (arch_info != 0)
e9670677 13894 {
316f5878
RS
13895 /* -march takes precedence over -mipsN, since it is more descriptive.
13896 There's no harm in specifying both as long as the ISA levels
13897 are the same. */
fef14a42 13898 if (file_mips_isa != arch_info->isa)
316f5878
RS
13899 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
13900 mips_cpu_info_from_isa (file_mips_isa)->name,
fef14a42 13901 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 13902 }
316f5878 13903 else
fef14a42 13904 arch_info = mips_cpu_info_from_isa (file_mips_isa);
e9670677
MR
13905 }
13906
fef14a42 13907 if (arch_info == 0)
95bfe26e
MF
13908 {
13909 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
13910 gas_assert (arch_info);
13911 }
e9670677 13912
fef14a42 13913 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
20203fb9 13914 as_bad (_("-march=%s is not compatible with the selected ABI"),
fef14a42
TS
13915 arch_info->name);
13916
13917 mips_set_architecture (arch_info);
13918
13919 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
13920 if (mips_tune_string != 0)
13921 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 13922
fef14a42
TS
13923 if (tune_info == 0)
13924 mips_set_tune (arch_info);
13925 else
13926 mips_set_tune (tune_info);
e9670677 13927
316f5878 13928 if (file_mips_gp32 >= 0)
e9670677 13929 {
316f5878
RS
13930 /* The user specified the size of the integer registers. Make sure
13931 it agrees with the ABI and ISA. */
13932 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
13933 as_bad (_("-mgp64 used with a 32-bit processor"));
13934 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
13935 as_bad (_("-mgp32 used with a 64-bit ABI"));
13936 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
13937 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
13938 }
13939 else
13940 {
316f5878
RS
13941 /* Infer the integer register size from the ABI and processor.
13942 Restrict ourselves to 32-bit registers if that's all the
13943 processor has, or if the ABI cannot handle 64-bit registers. */
13944 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
13945 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
e9670677
MR
13946 }
13947
ad3fea08
TS
13948 switch (file_mips_fp32)
13949 {
13950 default:
13951 case -1:
13952 /* No user specified float register size.
13953 ??? GAS treats single-float processors as though they had 64-bit
13954 float registers (although it complains when double-precision
13955 instructions are used). As things stand, saying they have 32-bit
13956 registers would lead to spurious "register must be even" messages.
13957 So here we assume float registers are never smaller than the
13958 integer ones. */
13959 if (file_mips_gp32 == 0)
13960 /* 64-bit integer registers implies 64-bit float registers. */
13961 file_mips_fp32 = 0;
c6278170 13962 else if ((mips_opts.ase & FP64_ASES)
ad3fea08
TS
13963 && ISA_HAS_64BIT_FPRS (mips_opts.isa))
13964 /* -mips3d and -mdmx imply 64-bit float registers, if possible. */
13965 file_mips_fp32 = 0;
13966 else
13967 /* 32-bit float registers. */
13968 file_mips_fp32 = 1;
13969 break;
13970
13971 /* The user specified the size of the float registers. Check if it
13972 agrees with the ABI and ISA. */
13973 case 0:
13974 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
13975 as_bad (_("-mfp64 used with a 32-bit fpu"));
13976 else if (ABI_NEEDS_32BIT_REGS (mips_abi)
13977 && !ISA_HAS_MXHC1 (mips_opts.isa))
13978 as_warn (_("-mfp64 used with a 32-bit ABI"));
13979 break;
13980 case 1:
13981 if (ABI_NEEDS_64BIT_REGS (mips_abi))
13982 as_warn (_("-mfp32 used with a 64-bit ABI"));
13983 break;
13984 }
e9670677 13985
316f5878 13986 /* End of GCC-shared inference code. */
e9670677 13987
17a2f251
TS
13988 /* This flag is set when we have a 64-bit capable CPU but use only
13989 32-bit wide registers. Note that EABI does not use it. */
13990 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
13991 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
13992 || mips_abi == O32_ABI))
316f5878 13993 mips_32bitmode = 1;
e9670677
MR
13994
13995 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
13996 as_bad (_("trap exception not supported at ISA 1"));
13997
e9670677
MR
13998 /* If the selected architecture includes support for ASEs, enable
13999 generation of code for them. */
a4672219 14000 if (mips_opts.mips16 == -1)
fef14a42 14001 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
df58fc94
RS
14002 if (mips_opts.micromips == -1)
14003 mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_arch)) ? 1 : 0;
846ef2d0
RS
14004
14005 /* MIPS3D and MDMX require 64-bit FPRs, so -mfp32 should stop those
14006 ASEs from being selected implicitly. */
14007 if (file_mips_fp32 == 1)
14008 file_ase_explicit |= ASE_MIPS3D | ASE_MDMX;
14009
14010 /* If the user didn't explicitly select or deselect a particular ASE,
14011 use the default setting for the CPU. */
14012 mips_opts.ase |= (arch_info->ase & ~file_ase_explicit);
14013
e9670677 14014 file_mips_isa = mips_opts.isa;
846ef2d0 14015 file_ase = mips_opts.ase;
e9670677
MR
14016 mips_opts.gp32 = file_mips_gp32;
14017 mips_opts.fp32 = file_mips_fp32;
037b32b9
AN
14018 mips_opts.soft_float = file_mips_soft_float;
14019 mips_opts.single_float = file_mips_single_float;
e9670677 14020
c6278170
RS
14021 mips_check_isa_supports_ases ();
14022
ecb4347a 14023 if (mips_flag_mdebug < 0)
e8044f35 14024 mips_flag_mdebug = 0;
e9670677
MR
14025}
14026\f
14027void
17a2f251 14028mips_init_after_args (void)
252b5132
RH
14029{
14030 /* initialize opcodes */
14031 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 14032 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
14033}
14034
14035long
17a2f251 14036md_pcrel_from (fixS *fixP)
252b5132 14037{
a7ebbfdf
TS
14038 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
14039 switch (fixP->fx_r_type)
14040 {
df58fc94
RS
14041 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
14042 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
14043 /* Return the address of the delay slot. */
14044 return addr + 2;
14045
14046 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
14047 case BFD_RELOC_MICROMIPS_JMP:
a7ebbfdf
TS
14048 case BFD_RELOC_16_PCREL_S2:
14049 case BFD_RELOC_MIPS_JMP:
14050 /* Return the address of the delay slot. */
14051 return addr + 4;
df58fc94 14052
b47468a6
CM
14053 case BFD_RELOC_32_PCREL:
14054 return addr;
14055
a7ebbfdf 14056 default:
58ea3d6a 14057 /* We have no relocation type for PC relative MIPS16 instructions. */
64817874
TS
14058 if (fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != now_seg)
14059 as_bad_where (fixP->fx_file, fixP->fx_line,
14060 _("PC relative MIPS16 instruction references a different section"));
a7ebbfdf
TS
14061 return addr;
14062 }
252b5132
RH
14063}
14064
252b5132
RH
14065/* This is called before the symbol table is processed. In order to
14066 work with gcc when using mips-tfile, we must keep all local labels.
14067 However, in other cases, we want to discard them. If we were
14068 called with -g, but we didn't see any debugging information, it may
14069 mean that gcc is smuggling debugging information through to
14070 mips-tfile, in which case we must generate all local labels. */
14071
14072void
17a2f251 14073mips_frob_file_before_adjust (void)
252b5132
RH
14074{
14075#ifndef NO_ECOFF_DEBUGGING
14076 if (ECOFF_DEBUGGING
14077 && mips_debug != 0
14078 && ! ecoff_debugging_seen)
14079 flag_keep_locals = 1;
14080#endif
14081}
14082
3b91255e 14083/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 14084 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
14085 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
14086 relocation operators.
14087
14088 For our purposes, a %lo() expression matches a %got() or %hi()
14089 expression if:
14090
14091 (a) it refers to the same symbol; and
14092 (b) the offset applied in the %lo() expression is no lower than
14093 the offset applied in the %got() or %hi().
14094
14095 (b) allows us to cope with code like:
14096
14097 lui $4,%hi(foo)
14098 lh $4,%lo(foo+2)($4)
14099
14100 ...which is legal on RELA targets, and has a well-defined behaviour
14101 if the user knows that adding 2 to "foo" will not induce a carry to
14102 the high 16 bits.
14103
14104 When several %lo()s match a particular %got() or %hi(), we use the
14105 following rules to distinguish them:
14106
14107 (1) %lo()s with smaller offsets are a better match than %lo()s with
14108 higher offsets.
14109
14110 (2) %lo()s with no matching %got() or %hi() are better than those
14111 that already have a matching %got() or %hi().
14112
14113 (3) later %lo()s are better than earlier %lo()s.
14114
14115 These rules are applied in order.
14116
14117 (1) means, among other things, that %lo()s with identical offsets are
14118 chosen if they exist.
14119
14120 (2) means that we won't associate several high-part relocations with
14121 the same low-part relocation unless there's no alternative. Having
14122 several high parts for the same low part is a GNU extension; this rule
14123 allows careful users to avoid it.
14124
14125 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
14126 with the last high-part relocation being at the front of the list.
14127 It therefore makes sense to choose the last matching low-part
14128 relocation, all other things being equal. It's also easier
14129 to code that way. */
252b5132
RH
14130
14131void
17a2f251 14132mips_frob_file (void)
252b5132
RH
14133{
14134 struct mips_hi_fixup *l;
35903be0 14135 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
252b5132
RH
14136
14137 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
14138 {
14139 segment_info_type *seginfo;
3b91255e
RS
14140 bfd_boolean matched_lo_p;
14141 fixS **hi_pos, **lo_pos, **pos;
252b5132 14142
9c2799c2 14143 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 14144
5919d012 14145 /* If a GOT16 relocation turns out to be against a global symbol,
b886a2ab
RS
14146 there isn't supposed to be a matching LO. Ignore %gots against
14147 constants; we'll report an error for those later. */
738e5348 14148 if (got16_reloc_p (l->fixp->fx_r_type)
b886a2ab
RS
14149 && !(l->fixp->fx_addsy
14150 && pic_need_relax (l->fixp->fx_addsy, l->seg)))
5919d012
RS
14151 continue;
14152
14153 /* Check quickly whether the next fixup happens to be a matching %lo. */
14154 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
14155 continue;
14156
252b5132 14157 seginfo = seg_info (l->seg);
252b5132 14158
3b91255e
RS
14159 /* Set HI_POS to the position of this relocation in the chain.
14160 Set LO_POS to the position of the chosen low-part relocation.
14161 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
14162 relocation that matches an immediately-preceding high-part
14163 relocation. */
14164 hi_pos = NULL;
14165 lo_pos = NULL;
14166 matched_lo_p = FALSE;
738e5348 14167 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
35903be0 14168
3b91255e
RS
14169 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
14170 {
14171 if (*pos == l->fixp)
14172 hi_pos = pos;
14173
35903be0 14174 if ((*pos)->fx_r_type == looking_for_rtype
30cfc97a 14175 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
3b91255e
RS
14176 && (*pos)->fx_offset >= l->fixp->fx_offset
14177 && (lo_pos == NULL
14178 || (*pos)->fx_offset < (*lo_pos)->fx_offset
14179 || (!matched_lo_p
14180 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
14181 lo_pos = pos;
14182
14183 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
14184 && fixup_has_matching_lo_p (*pos));
14185 }
14186
14187 /* If we found a match, remove the high-part relocation from its
14188 current position and insert it before the low-part relocation.
14189 Make the offsets match so that fixup_has_matching_lo_p()
14190 will return true.
14191
14192 We don't warn about unmatched high-part relocations since some
14193 versions of gcc have been known to emit dead "lui ...%hi(...)"
14194 instructions. */
14195 if (lo_pos != NULL)
14196 {
14197 l->fixp->fx_offset = (*lo_pos)->fx_offset;
14198 if (l->fixp->fx_next != *lo_pos)
252b5132 14199 {
3b91255e
RS
14200 *hi_pos = l->fixp->fx_next;
14201 l->fixp->fx_next = *lo_pos;
14202 *lo_pos = l->fixp;
252b5132 14203 }
252b5132
RH
14204 }
14205 }
14206}
14207
252b5132 14208int
17a2f251 14209mips_force_relocation (fixS *fixp)
252b5132 14210{
ae6063d4 14211 if (generic_force_reloc (fixp))
252b5132
RH
14212 return 1;
14213
df58fc94
RS
14214 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
14215 so that the linker relaxation can update targets. */
14216 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
14217 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
14218 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
14219 return 1;
14220
3e722fb5 14221 return 0;
252b5132
RH
14222}
14223
b886a2ab
RS
14224/* Read the instruction associated with RELOC from BUF. */
14225
14226static unsigned int
14227read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
14228{
14229 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
14230 return read_compressed_insn (buf, 4);
14231 else
14232 return read_insn (buf);
14233}
14234
14235/* Write instruction INSN to BUF, given that it has been relocated
14236 by RELOC. */
14237
14238static void
14239write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
14240 unsigned long insn)
14241{
14242 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
14243 write_compressed_insn (buf, insn, 4);
14244 else
14245 write_insn (buf, insn);
14246}
14247
252b5132
RH
14248/* Apply a fixup to the object file. */
14249
94f592af 14250void
55cf6793 14251md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 14252{
4d68580a 14253 char *buf;
b886a2ab 14254 unsigned long insn;
a7ebbfdf 14255 reloc_howto_type *howto;
252b5132 14256
a7ebbfdf
TS
14257 /* We ignore generic BFD relocations we don't know about. */
14258 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
14259 if (! howto)
14260 return;
65551fa4 14261
df58fc94
RS
14262 gas_assert (fixP->fx_size == 2
14263 || fixP->fx_size == 4
90ecf173
MR
14264 || fixP->fx_r_type == BFD_RELOC_16
14265 || fixP->fx_r_type == BFD_RELOC_64
14266 || fixP->fx_r_type == BFD_RELOC_CTOR
14267 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
df58fc94 14268 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
90ecf173
MR
14269 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
14270 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
14271 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64);
252b5132 14272
4d68580a 14273 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132 14274
df58fc94
RS
14275 gas_assert (!fixP->fx_pcrel || fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
14276 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
14277 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
b47468a6
CM
14278 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
14279 || fixP->fx_r_type == BFD_RELOC_32_PCREL);
b1dca8ee
RS
14280
14281 /* Don't treat parts of a composite relocation as done. There are two
14282 reasons for this:
14283
14284 (1) The second and third parts will be against 0 (RSS_UNDEF) but
14285 should nevertheless be emitted if the first part is.
14286
14287 (2) In normal usage, composite relocations are never assembly-time
14288 constants. The easiest way of dealing with the pathological
14289 exceptions is to generate a relocation against STN_UNDEF and
14290 leave everything up to the linker. */
3994f87e 14291 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
252b5132
RH
14292 fixP->fx_done = 1;
14293
14294 switch (fixP->fx_r_type)
14295 {
3f98094e
DJ
14296 case BFD_RELOC_MIPS_TLS_GD:
14297 case BFD_RELOC_MIPS_TLS_LDM:
741d6ea8
JM
14298 case BFD_RELOC_MIPS_TLS_DTPREL32:
14299 case BFD_RELOC_MIPS_TLS_DTPREL64:
3f98094e
DJ
14300 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
14301 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
14302 case BFD_RELOC_MIPS_TLS_GOTTPREL:
d0f13682
CLT
14303 case BFD_RELOC_MIPS_TLS_TPREL32:
14304 case BFD_RELOC_MIPS_TLS_TPREL64:
3f98094e
DJ
14305 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
14306 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
df58fc94
RS
14307 case BFD_RELOC_MICROMIPS_TLS_GD:
14308 case BFD_RELOC_MICROMIPS_TLS_LDM:
14309 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
14310 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
14311 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
14312 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
14313 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
d0f13682
CLT
14314 case BFD_RELOC_MIPS16_TLS_GD:
14315 case BFD_RELOC_MIPS16_TLS_LDM:
14316 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
14317 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
14318 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
14319 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
14320 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
b886a2ab
RS
14321 if (!fixP->fx_addsy)
14322 {
14323 as_bad_where (fixP->fx_file, fixP->fx_line,
14324 _("TLS relocation against a constant"));
14325 break;
14326 }
3f98094e
DJ
14327 S_SET_THREAD_LOCAL (fixP->fx_addsy);
14328 /* fall through */
14329
252b5132 14330 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
14331 case BFD_RELOC_MIPS_SHIFT5:
14332 case BFD_RELOC_MIPS_SHIFT6:
14333 case BFD_RELOC_MIPS_GOT_DISP:
14334 case BFD_RELOC_MIPS_GOT_PAGE:
14335 case BFD_RELOC_MIPS_GOT_OFST:
14336 case BFD_RELOC_MIPS_SUB:
14337 case BFD_RELOC_MIPS_INSERT_A:
14338 case BFD_RELOC_MIPS_INSERT_B:
14339 case BFD_RELOC_MIPS_DELETE:
14340 case BFD_RELOC_MIPS_HIGHEST:
14341 case BFD_RELOC_MIPS_HIGHER:
14342 case BFD_RELOC_MIPS_SCN_DISP:
14343 case BFD_RELOC_MIPS_REL16:
14344 case BFD_RELOC_MIPS_RELGOT:
14345 case BFD_RELOC_MIPS_JALR:
252b5132
RH
14346 case BFD_RELOC_HI16:
14347 case BFD_RELOC_HI16_S:
b886a2ab 14348 case BFD_RELOC_LO16:
cdf6fd85 14349 case BFD_RELOC_GPREL16:
252b5132
RH
14350 case BFD_RELOC_MIPS_LITERAL:
14351 case BFD_RELOC_MIPS_CALL16:
14352 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 14353 case BFD_RELOC_GPREL32:
252b5132
RH
14354 case BFD_RELOC_MIPS_GOT_HI16:
14355 case BFD_RELOC_MIPS_GOT_LO16:
14356 case BFD_RELOC_MIPS_CALL_HI16:
14357 case BFD_RELOC_MIPS_CALL_LO16:
14358 case BFD_RELOC_MIPS16_GPREL:
738e5348
RS
14359 case BFD_RELOC_MIPS16_GOT16:
14360 case BFD_RELOC_MIPS16_CALL16:
d6f16593
MR
14361 case BFD_RELOC_MIPS16_HI16:
14362 case BFD_RELOC_MIPS16_HI16_S:
b886a2ab 14363 case BFD_RELOC_MIPS16_LO16:
252b5132 14364 case BFD_RELOC_MIPS16_JMP:
df58fc94
RS
14365 case BFD_RELOC_MICROMIPS_JMP:
14366 case BFD_RELOC_MICROMIPS_GOT_DISP:
14367 case BFD_RELOC_MICROMIPS_GOT_PAGE:
14368 case BFD_RELOC_MICROMIPS_GOT_OFST:
14369 case BFD_RELOC_MICROMIPS_SUB:
14370 case BFD_RELOC_MICROMIPS_HIGHEST:
14371 case BFD_RELOC_MICROMIPS_HIGHER:
14372 case BFD_RELOC_MICROMIPS_SCN_DISP:
14373 case BFD_RELOC_MICROMIPS_JALR:
14374 case BFD_RELOC_MICROMIPS_HI16:
14375 case BFD_RELOC_MICROMIPS_HI16_S:
b886a2ab 14376 case BFD_RELOC_MICROMIPS_LO16:
df58fc94
RS
14377 case BFD_RELOC_MICROMIPS_GPREL16:
14378 case BFD_RELOC_MICROMIPS_LITERAL:
14379 case BFD_RELOC_MICROMIPS_CALL16:
14380 case BFD_RELOC_MICROMIPS_GOT16:
14381 case BFD_RELOC_MICROMIPS_GOT_HI16:
14382 case BFD_RELOC_MICROMIPS_GOT_LO16:
14383 case BFD_RELOC_MICROMIPS_CALL_HI16:
14384 case BFD_RELOC_MICROMIPS_CALL_LO16:
067ec077 14385 case BFD_RELOC_MIPS_EH:
b886a2ab
RS
14386 if (fixP->fx_done)
14387 {
14388 offsetT value;
14389
14390 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
14391 {
14392 insn = read_reloc_insn (buf, fixP->fx_r_type);
14393 if (mips16_reloc_p (fixP->fx_r_type))
14394 insn |= mips16_immed_extend (value, 16);
14395 else
14396 insn |= (value & 0xffff);
14397 write_reloc_insn (buf, fixP->fx_r_type, insn);
14398 }
14399 else
14400 as_bad_where (fixP->fx_file, fixP->fx_line,
14401 _("Unsupported constant in relocation"));
14402 }
252b5132
RH
14403 break;
14404
252b5132
RH
14405 case BFD_RELOC_64:
14406 /* This is handled like BFD_RELOC_32, but we output a sign
14407 extended value if we are only 32 bits. */
3e722fb5 14408 if (fixP->fx_done)
252b5132
RH
14409 {
14410 if (8 <= sizeof (valueT))
4d68580a 14411 md_number_to_chars (buf, *valP, 8);
252b5132
RH
14412 else
14413 {
a7ebbfdf 14414 valueT hiv;
252b5132 14415
a7ebbfdf 14416 if ((*valP & 0x80000000) != 0)
252b5132
RH
14417 hiv = 0xffffffff;
14418 else
14419 hiv = 0;
4d68580a
RS
14420 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
14421 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
252b5132
RH
14422 }
14423 }
14424 break;
14425
056350c6 14426 case BFD_RELOC_RVA:
252b5132 14427 case BFD_RELOC_32:
b47468a6 14428 case BFD_RELOC_32_PCREL:
252b5132
RH
14429 case BFD_RELOC_16:
14430 /* If we are deleting this reloc entry, we must fill in the
54f4ddb3
TS
14431 value now. This can happen if we have a .word which is not
14432 resolved when it appears but is later defined. */
252b5132 14433 if (fixP->fx_done)
4d68580a 14434 md_number_to_chars (buf, *valP, fixP->fx_size);
252b5132
RH
14435 break;
14436
252b5132 14437 case BFD_RELOC_16_PCREL_S2:
a7ebbfdf 14438 if ((*valP & 0x3) != 0)
cb56d3d3 14439 as_bad_where (fixP->fx_file, fixP->fx_line,
bad36eac 14440 _("Branch to misaligned address (%lx)"), (long) *valP);
cb56d3d3 14441
54f4ddb3
TS
14442 /* We need to save the bits in the instruction since fixup_segment()
14443 might be deleting the relocation entry (i.e., a branch within
14444 the current segment). */
a7ebbfdf 14445 if (! fixP->fx_done)
bb2d6cd7 14446 break;
252b5132 14447
54f4ddb3 14448 /* Update old instruction data. */
4d68580a 14449 insn = read_insn (buf);
252b5132 14450
a7ebbfdf
TS
14451 if (*valP + 0x20000 <= 0x3ffff)
14452 {
14453 insn |= (*valP >> 2) & 0xffff;
4d68580a 14454 write_insn (buf, insn);
a7ebbfdf
TS
14455 }
14456 else if (mips_pic == NO_PIC
14457 && fixP->fx_done
14458 && fixP->fx_frag->fr_address >= text_section->vma
14459 && (fixP->fx_frag->fr_address
587aac4e 14460 < text_section->vma + bfd_get_section_size (text_section))
a7ebbfdf
TS
14461 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
14462 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
14463 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
14464 {
14465 /* The branch offset is too large. If this is an
14466 unconditional branch, and we are not generating PIC code,
14467 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
14468 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
14469 insn = 0x0c000000; /* jal */
252b5132 14470 else
a7ebbfdf
TS
14471 insn = 0x08000000; /* j */
14472 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
14473 fixP->fx_done = 0;
14474 fixP->fx_addsy = section_symbol (text_section);
14475 *valP += md_pcrel_from (fixP);
4d68580a 14476 write_insn (buf, insn);
a7ebbfdf
TS
14477 }
14478 else
14479 {
14480 /* If we got here, we have branch-relaxation disabled,
14481 and there's nothing we can do to fix this instruction
14482 without turning it into a longer sequence. */
14483 as_bad_where (fixP->fx_file, fixP->fx_line,
14484 _("Branch out of range"));
252b5132 14485 }
252b5132
RH
14486 break;
14487
df58fc94
RS
14488 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
14489 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
14490 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
14491 /* We adjust the offset back to even. */
14492 if ((*valP & 0x1) != 0)
14493 --(*valP);
14494
14495 if (! fixP->fx_done)
14496 break;
14497
14498 /* Should never visit here, because we keep the relocation. */
14499 abort ();
14500 break;
14501
252b5132
RH
14502 case BFD_RELOC_VTABLE_INHERIT:
14503 fixP->fx_done = 0;
14504 if (fixP->fx_addsy
14505 && !S_IS_DEFINED (fixP->fx_addsy)
14506 && !S_IS_WEAK (fixP->fx_addsy))
14507 S_SET_WEAK (fixP->fx_addsy);
14508 break;
14509
14510 case BFD_RELOC_VTABLE_ENTRY:
14511 fixP->fx_done = 0;
14512 break;
14513
14514 default:
b37df7c4 14515 abort ();
252b5132 14516 }
a7ebbfdf
TS
14517
14518 /* Remember value for tc_gen_reloc. */
14519 fixP->fx_addnumber = *valP;
252b5132
RH
14520}
14521
252b5132 14522static symbolS *
17a2f251 14523get_symbol (void)
252b5132
RH
14524{
14525 int c;
14526 char *name;
14527 symbolS *p;
14528
14529 name = input_line_pointer;
14530 c = get_symbol_end ();
14531 p = (symbolS *) symbol_find_or_make (name);
14532 *input_line_pointer = c;
14533 return p;
14534}
14535
742a56fe
RS
14536/* Align the current frag to a given power of two. If a particular
14537 fill byte should be used, FILL points to an integer that contains
14538 that byte, otherwise FILL is null.
14539
462427c4
RS
14540 This function used to have the comment:
14541
14542 The MIPS assembler also automatically adjusts any preceding label.
14543
14544 The implementation therefore applied the adjustment to a maximum of
14545 one label. However, other label adjustments are applied to batches
14546 of labels, and adjusting just one caused problems when new labels
14547 were added for the sake of debugging or unwind information.
14548 We therefore adjust all preceding labels (given as LABELS) instead. */
252b5132
RH
14549
14550static void
462427c4 14551mips_align (int to, int *fill, struct insn_label_list *labels)
252b5132 14552{
7d10b47d 14553 mips_emit_delays ();
df58fc94 14554 mips_record_compressed_mode ();
742a56fe
RS
14555 if (fill == NULL && subseg_text_p (now_seg))
14556 frag_align_code (to, 0);
14557 else
14558 frag_align (to, fill ? *fill : 0, 0);
252b5132 14559 record_alignment (now_seg, to);
462427c4 14560 mips_move_labels (labels, FALSE);
252b5132
RH
14561}
14562
14563/* Align to a given power of two. .align 0 turns off the automatic
14564 alignment used by the data creating pseudo-ops. */
14565
14566static void
17a2f251 14567s_align (int x ATTRIBUTE_UNUSED)
252b5132 14568{
742a56fe 14569 int temp, fill_value, *fill_ptr;
49954fb4 14570 long max_alignment = 28;
252b5132 14571
54f4ddb3 14572 /* o Note that the assembler pulls down any immediately preceding label
252b5132 14573 to the aligned address.
54f4ddb3 14574 o It's not documented but auto alignment is reinstated by
252b5132 14575 a .align pseudo instruction.
54f4ddb3 14576 o Note also that after auto alignment is turned off the mips assembler
252b5132 14577 issues an error on attempt to assemble an improperly aligned data item.
54f4ddb3 14578 We don't. */
252b5132
RH
14579
14580 temp = get_absolute_expression ();
14581 if (temp > max_alignment)
14582 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
14583 else if (temp < 0)
14584 {
14585 as_warn (_("Alignment negative: 0 assumed."));
14586 temp = 0;
14587 }
14588 if (*input_line_pointer == ',')
14589 {
f9419b05 14590 ++input_line_pointer;
742a56fe
RS
14591 fill_value = get_absolute_expression ();
14592 fill_ptr = &fill_value;
252b5132
RH
14593 }
14594 else
742a56fe 14595 fill_ptr = 0;
252b5132
RH
14596 if (temp)
14597 {
a8dbcb85
TS
14598 segment_info_type *si = seg_info (now_seg);
14599 struct insn_label_list *l = si->label_list;
54f4ddb3 14600 /* Auto alignment should be switched on by next section change. */
252b5132 14601 auto_align = 1;
462427c4 14602 mips_align (temp, fill_ptr, l);
252b5132
RH
14603 }
14604 else
14605 {
14606 auto_align = 0;
14607 }
14608
14609 demand_empty_rest_of_line ();
14610}
14611
252b5132 14612static void
17a2f251 14613s_change_sec (int sec)
252b5132
RH
14614{
14615 segT seg;
14616
252b5132
RH
14617 /* The ELF backend needs to know that we are changing sections, so
14618 that .previous works correctly. We could do something like check
b6ff326e 14619 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
14620 as it would not be appropriate to use it in the section changing
14621 functions in read.c, since obj-elf.c intercepts those. FIXME:
14622 This should be cleaner, somehow. */
f3ded42a 14623 obj_elf_section_change_hook ();
252b5132 14624
7d10b47d 14625 mips_emit_delays ();
6a32d874 14626
252b5132
RH
14627 switch (sec)
14628 {
14629 case 't':
14630 s_text (0);
14631 break;
14632 case 'd':
14633 s_data (0);
14634 break;
14635 case 'b':
14636 subseg_set (bss_section, (subsegT) get_absolute_expression ());
14637 demand_empty_rest_of_line ();
14638 break;
14639
14640 case 'r':
4d0d148d
TS
14641 seg = subseg_new (RDATA_SECTION_NAME,
14642 (subsegT) get_absolute_expression ());
f3ded42a
RS
14643 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
14644 | SEC_READONLY | SEC_RELOC
14645 | SEC_DATA));
14646 if (strncmp (TARGET_OS, "elf", 3) != 0)
14647 record_alignment (seg, 4);
4d0d148d 14648 demand_empty_rest_of_line ();
252b5132
RH
14649 break;
14650
14651 case 's':
4d0d148d 14652 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
f3ded42a
RS
14653 bfd_set_section_flags (stdoutput, seg,
14654 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
14655 if (strncmp (TARGET_OS, "elf", 3) != 0)
14656 record_alignment (seg, 4);
4d0d148d
TS
14657 demand_empty_rest_of_line ();
14658 break;
998b3c36
MR
14659
14660 case 'B':
14661 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
f3ded42a
RS
14662 bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
14663 if (strncmp (TARGET_OS, "elf", 3) != 0)
14664 record_alignment (seg, 4);
998b3c36
MR
14665 demand_empty_rest_of_line ();
14666 break;
252b5132
RH
14667 }
14668
14669 auto_align = 1;
14670}
b34976b6 14671
cca86cc8 14672void
17a2f251 14673s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 14674{
cca86cc8
SC
14675 char *section_name;
14676 char c;
684022ea 14677 char next_c = 0;
cca86cc8
SC
14678 int section_type;
14679 int section_flag;
14680 int section_entry_size;
14681 int section_alignment;
b34976b6 14682
cca86cc8
SC
14683 section_name = input_line_pointer;
14684 c = get_symbol_end ();
a816d1ed
AO
14685 if (c)
14686 next_c = *(input_line_pointer + 1);
cca86cc8 14687
4cf0dd0d
TS
14688 /* Do we have .section Name<,"flags">? */
14689 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 14690 {
4cf0dd0d
TS
14691 /* just after name is now '\0'. */
14692 *input_line_pointer = c;
cca86cc8
SC
14693 input_line_pointer = section_name;
14694 obj_elf_section (ignore);
14695 return;
14696 }
14697 input_line_pointer++;
14698
14699 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
14700 if (c == ',')
14701 section_type = get_absolute_expression ();
14702 else
14703 section_type = 0;
14704 if (*input_line_pointer++ == ',')
14705 section_flag = get_absolute_expression ();
14706 else
14707 section_flag = 0;
14708 if (*input_line_pointer++ == ',')
14709 section_entry_size = get_absolute_expression ();
14710 else
14711 section_entry_size = 0;
14712 if (*input_line_pointer++ == ',')
14713 section_alignment = get_absolute_expression ();
14714 else
14715 section_alignment = 0;
87975d2a
AM
14716 /* FIXME: really ignore? */
14717 (void) section_alignment;
cca86cc8 14718
a816d1ed
AO
14719 section_name = xstrdup (section_name);
14720
8ab8a5c8
RS
14721 /* When using the generic form of .section (as implemented by obj-elf.c),
14722 there's no way to set the section type to SHT_MIPS_DWARF. Users have
14723 traditionally had to fall back on the more common @progbits instead.
14724
14725 There's nothing really harmful in this, since bfd will correct
14726 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
708587a4 14727 means that, for backwards compatibility, the special_section entries
8ab8a5c8
RS
14728 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
14729
14730 Even so, we shouldn't force users of the MIPS .section syntax to
14731 incorrectly label the sections as SHT_PROGBITS. The best compromise
14732 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
14733 generic type-checking code. */
14734 if (section_type == SHT_MIPS_DWARF)
14735 section_type = SHT_PROGBITS;
14736
cca86cc8
SC
14737 obj_elf_change_section (section_name, section_type, section_flag,
14738 section_entry_size, 0, 0, 0);
a816d1ed
AO
14739
14740 if (now_seg->name != section_name)
14741 free (section_name);
cca86cc8 14742}
252b5132
RH
14743
14744void
17a2f251 14745mips_enable_auto_align (void)
252b5132
RH
14746{
14747 auto_align = 1;
14748}
14749
14750static void
17a2f251 14751s_cons (int log_size)
252b5132 14752{
a8dbcb85
TS
14753 segment_info_type *si = seg_info (now_seg);
14754 struct insn_label_list *l = si->label_list;
252b5132 14755
7d10b47d 14756 mips_emit_delays ();
252b5132 14757 if (log_size > 0 && auto_align)
462427c4 14758 mips_align (log_size, 0, l);
252b5132 14759 cons (1 << log_size);
a1facbec 14760 mips_clear_insn_labels ();
252b5132
RH
14761}
14762
14763static void
17a2f251 14764s_float_cons (int type)
252b5132 14765{
a8dbcb85
TS
14766 segment_info_type *si = seg_info (now_seg);
14767 struct insn_label_list *l = si->label_list;
252b5132 14768
7d10b47d 14769 mips_emit_delays ();
252b5132
RH
14770
14771 if (auto_align)
49309057
ILT
14772 {
14773 if (type == 'd')
462427c4 14774 mips_align (3, 0, l);
49309057 14775 else
462427c4 14776 mips_align (2, 0, l);
49309057 14777 }
252b5132 14778
252b5132 14779 float_cons (type);
a1facbec 14780 mips_clear_insn_labels ();
252b5132
RH
14781}
14782
14783/* Handle .globl. We need to override it because on Irix 5 you are
14784 permitted to say
14785 .globl foo .text
14786 where foo is an undefined symbol, to mean that foo should be
14787 considered to be the address of a function. */
14788
14789static void
17a2f251 14790s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
14791{
14792 char *name;
14793 int c;
14794 symbolS *symbolP;
14795 flagword flag;
14796
8a06b769 14797 do
252b5132 14798 {
8a06b769 14799 name = input_line_pointer;
252b5132 14800 c = get_symbol_end ();
8a06b769
TS
14801 symbolP = symbol_find_or_make (name);
14802 S_SET_EXTERNAL (symbolP);
14803
252b5132 14804 *input_line_pointer = c;
8a06b769 14805 SKIP_WHITESPACE ();
252b5132 14806
8a06b769
TS
14807 /* On Irix 5, every global symbol that is not explicitly labelled as
14808 being a function is apparently labelled as being an object. */
14809 flag = BSF_OBJECT;
252b5132 14810
8a06b769
TS
14811 if (!is_end_of_line[(unsigned char) *input_line_pointer]
14812 && (*input_line_pointer != ','))
14813 {
14814 char *secname;
14815 asection *sec;
14816
14817 secname = input_line_pointer;
14818 c = get_symbol_end ();
14819 sec = bfd_get_section_by_name (stdoutput, secname);
14820 if (sec == NULL)
14821 as_bad (_("%s: no such section"), secname);
14822 *input_line_pointer = c;
14823
14824 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
14825 flag = BSF_FUNCTION;
14826 }
14827
14828 symbol_get_bfdsym (symbolP)->flags |= flag;
14829
14830 c = *input_line_pointer;
14831 if (c == ',')
14832 {
14833 input_line_pointer++;
14834 SKIP_WHITESPACE ();
14835 if (is_end_of_line[(unsigned char) *input_line_pointer])
14836 c = '\n';
14837 }
14838 }
14839 while (c == ',');
252b5132 14840
252b5132
RH
14841 demand_empty_rest_of_line ();
14842}
14843
14844static void
17a2f251 14845s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
14846{
14847 char *opt;
14848 char c;
14849
14850 opt = input_line_pointer;
14851 c = get_symbol_end ();
14852
14853 if (*opt == 'O')
14854 {
14855 /* FIXME: What does this mean? */
14856 }
14857 else if (strncmp (opt, "pic", 3) == 0)
14858 {
14859 int i;
14860
14861 i = atoi (opt + 3);
14862 if (i == 0)
14863 mips_pic = NO_PIC;
14864 else if (i == 2)
143d77c5 14865 {
8b828383 14866 mips_pic = SVR4_PIC;
143d77c5
EC
14867 mips_abicalls = TRUE;
14868 }
252b5132
RH
14869 else
14870 as_bad (_(".option pic%d not supported"), i);
14871
4d0d148d 14872 if (mips_pic == SVR4_PIC)
252b5132
RH
14873 {
14874 if (g_switch_seen && g_switch_value != 0)
14875 as_warn (_("-G may not be used with SVR4 PIC code"));
14876 g_switch_value = 0;
14877 bfd_set_gp_size (stdoutput, 0);
14878 }
14879 }
14880 else
14881 as_warn (_("Unrecognized option \"%s\""), opt);
14882
14883 *input_line_pointer = c;
14884 demand_empty_rest_of_line ();
14885}
14886
14887/* This structure is used to hold a stack of .set values. */
14888
e972090a
NC
14889struct mips_option_stack
14890{
252b5132
RH
14891 struct mips_option_stack *next;
14892 struct mips_set_options options;
14893};
14894
14895static struct mips_option_stack *mips_opts_stack;
14896
14897/* Handle the .set pseudo-op. */
14898
14899static void
17a2f251 14900s_mipsset (int x ATTRIBUTE_UNUSED)
252b5132
RH
14901{
14902 char *name = input_line_pointer, ch;
c6278170 14903 const struct mips_ase *ase;
252b5132
RH
14904
14905 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 14906 ++input_line_pointer;
252b5132
RH
14907 ch = *input_line_pointer;
14908 *input_line_pointer = '\0';
14909
14910 if (strcmp (name, "reorder") == 0)
14911 {
7d10b47d
RS
14912 if (mips_opts.noreorder)
14913 end_noreorder ();
252b5132
RH
14914 }
14915 else if (strcmp (name, "noreorder") == 0)
14916 {
7d10b47d
RS
14917 if (!mips_opts.noreorder)
14918 start_noreorder ();
252b5132 14919 }
741fe287
MR
14920 else if (strncmp (name, "at=", 3) == 0)
14921 {
14922 char *s = name + 3;
14923
14924 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
14925 as_bad (_("Unrecognized register name `%s'"), s);
14926 }
252b5132
RH
14927 else if (strcmp (name, "at") == 0)
14928 {
741fe287 14929 mips_opts.at = ATREG;
252b5132
RH
14930 }
14931 else if (strcmp (name, "noat") == 0)
14932 {
741fe287 14933 mips_opts.at = ZERO;
252b5132
RH
14934 }
14935 else if (strcmp (name, "macro") == 0)
14936 {
14937 mips_opts.warn_about_macros = 0;
14938 }
14939 else if (strcmp (name, "nomacro") == 0)
14940 {
14941 if (mips_opts.noreorder == 0)
14942 as_bad (_("`noreorder' must be set before `nomacro'"));
14943 mips_opts.warn_about_macros = 1;
14944 }
14945 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
14946 {
14947 mips_opts.nomove = 0;
14948 }
14949 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
14950 {
14951 mips_opts.nomove = 1;
14952 }
14953 else if (strcmp (name, "bopt") == 0)
14954 {
14955 mips_opts.nobopt = 0;
14956 }
14957 else if (strcmp (name, "nobopt") == 0)
14958 {
14959 mips_opts.nobopt = 1;
14960 }
ad3fea08
TS
14961 else if (strcmp (name, "gp=default") == 0)
14962 mips_opts.gp32 = file_mips_gp32;
14963 else if (strcmp (name, "gp=32") == 0)
14964 mips_opts.gp32 = 1;
14965 else if (strcmp (name, "gp=64") == 0)
14966 {
14967 if (!ISA_HAS_64BIT_REGS (mips_opts.isa))
20203fb9 14968 as_warn (_("%s isa does not support 64-bit registers"),
ad3fea08
TS
14969 mips_cpu_info_from_isa (mips_opts.isa)->name);
14970 mips_opts.gp32 = 0;
14971 }
14972 else if (strcmp (name, "fp=default") == 0)
14973 mips_opts.fp32 = file_mips_fp32;
14974 else if (strcmp (name, "fp=32") == 0)
14975 mips_opts.fp32 = 1;
14976 else if (strcmp (name, "fp=64") == 0)
14977 {
14978 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
20203fb9 14979 as_warn (_("%s isa does not support 64-bit floating point registers"),
ad3fea08
TS
14980 mips_cpu_info_from_isa (mips_opts.isa)->name);
14981 mips_opts.fp32 = 0;
14982 }
037b32b9
AN
14983 else if (strcmp (name, "softfloat") == 0)
14984 mips_opts.soft_float = 1;
14985 else if (strcmp (name, "hardfloat") == 0)
14986 mips_opts.soft_float = 0;
14987 else if (strcmp (name, "singlefloat") == 0)
14988 mips_opts.single_float = 1;
14989 else if (strcmp (name, "doublefloat") == 0)
14990 mips_opts.single_float = 0;
252b5132
RH
14991 else if (strcmp (name, "mips16") == 0
14992 || strcmp (name, "MIPS-16") == 0)
df58fc94
RS
14993 {
14994 if (mips_opts.micromips == 1)
14995 as_fatal (_("`mips16' cannot be used with `micromips'"));
14996 mips_opts.mips16 = 1;
14997 }
252b5132
RH
14998 else if (strcmp (name, "nomips16") == 0
14999 || strcmp (name, "noMIPS-16") == 0)
15000 mips_opts.mips16 = 0;
df58fc94
RS
15001 else if (strcmp (name, "micromips") == 0)
15002 {
15003 if (mips_opts.mips16 == 1)
15004 as_fatal (_("`micromips' cannot be used with `mips16'"));
15005 mips_opts.micromips = 1;
15006 }
15007 else if (strcmp (name, "nomicromips") == 0)
15008 mips_opts.micromips = 0;
c6278170
RS
15009 else if (name[0] == 'n'
15010 && name[1] == 'o'
15011 && (ase = mips_lookup_ase (name + 2)))
15012 mips_set_ase (ase, FALSE);
15013 else if ((ase = mips_lookup_ase (name)))
15014 mips_set_ase (ase, TRUE);
1a2c1fad 15015 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 15016 {
af7ee8bf 15017 int reset = 0;
252b5132 15018
1a2c1fad
CD
15019 /* Permit the user to change the ISA and architecture on the fly.
15020 Needless to say, misuse can cause serious problems. */
81a21e38 15021 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
af7ee8bf
CD
15022 {
15023 reset = 1;
15024 mips_opts.isa = file_mips_isa;
1a2c1fad 15025 mips_opts.arch = file_mips_arch;
1a2c1fad
CD
15026 }
15027 else if (strncmp (name, "arch=", 5) == 0)
15028 {
15029 const struct mips_cpu_info *p;
15030
15031 p = mips_parse_cpu("internal use", name + 5);
15032 if (!p)
15033 as_bad (_("unknown architecture %s"), name + 5);
15034 else
15035 {
15036 mips_opts.arch = p->cpu;
15037 mips_opts.isa = p->isa;
15038 }
15039 }
81a21e38
TS
15040 else if (strncmp (name, "mips", 4) == 0)
15041 {
15042 const struct mips_cpu_info *p;
15043
15044 p = mips_parse_cpu("internal use", name);
15045 if (!p)
15046 as_bad (_("unknown ISA level %s"), name + 4);
15047 else
15048 {
15049 mips_opts.arch = p->cpu;
15050 mips_opts.isa = p->isa;
15051 }
15052 }
af7ee8bf 15053 else
81a21e38 15054 as_bad (_("unknown ISA or architecture %s"), name);
af7ee8bf
CD
15055
15056 switch (mips_opts.isa)
98d3f06f
KH
15057 {
15058 case 0:
98d3f06f 15059 break;
af7ee8bf
CD
15060 case ISA_MIPS1:
15061 case ISA_MIPS2:
15062 case ISA_MIPS32:
15063 case ISA_MIPS32R2:
98d3f06f
KH
15064 mips_opts.gp32 = 1;
15065 mips_opts.fp32 = 1;
15066 break;
af7ee8bf
CD
15067 case ISA_MIPS3:
15068 case ISA_MIPS4:
15069 case ISA_MIPS5:
15070 case ISA_MIPS64:
5f74bc13 15071 case ISA_MIPS64R2:
98d3f06f 15072 mips_opts.gp32 = 0;
e407c74b
NC
15073 if (mips_opts.arch == CPU_R5900)
15074 {
15075 mips_opts.fp32 = 1;
15076 }
15077 else
15078 {
98d3f06f 15079 mips_opts.fp32 = 0;
e407c74b 15080 }
98d3f06f
KH
15081 break;
15082 default:
15083 as_bad (_("unknown ISA level %s"), name + 4);
15084 break;
15085 }
af7ee8bf 15086 if (reset)
98d3f06f 15087 {
af7ee8bf
CD
15088 mips_opts.gp32 = file_mips_gp32;
15089 mips_opts.fp32 = file_mips_fp32;
98d3f06f 15090 }
252b5132
RH
15091 }
15092 else if (strcmp (name, "autoextend") == 0)
15093 mips_opts.noautoextend = 0;
15094 else if (strcmp (name, "noautoextend") == 0)
15095 mips_opts.noautoextend = 1;
833794fc
MR
15096 else if (strcmp (name, "insn32") == 0)
15097 mips_opts.insn32 = TRUE;
15098 else if (strcmp (name, "noinsn32") == 0)
15099 mips_opts.insn32 = FALSE;
252b5132
RH
15100 else if (strcmp (name, "push") == 0)
15101 {
15102 struct mips_option_stack *s;
15103
15104 s = (struct mips_option_stack *) xmalloc (sizeof *s);
15105 s->next = mips_opts_stack;
15106 s->options = mips_opts;
15107 mips_opts_stack = s;
15108 }
15109 else if (strcmp (name, "pop") == 0)
15110 {
15111 struct mips_option_stack *s;
15112
15113 s = mips_opts_stack;
15114 if (s == NULL)
15115 as_bad (_(".set pop with no .set push"));
15116 else
15117 {
15118 /* If we're changing the reorder mode we need to handle
15119 delay slots correctly. */
15120 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 15121 start_noreorder ();
252b5132 15122 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 15123 end_noreorder ();
252b5132
RH
15124
15125 mips_opts = s->options;
15126 mips_opts_stack = s->next;
15127 free (s);
15128 }
15129 }
aed1a261
RS
15130 else if (strcmp (name, "sym32") == 0)
15131 mips_opts.sym32 = TRUE;
15132 else if (strcmp (name, "nosym32") == 0)
15133 mips_opts.sym32 = FALSE;
e6559e01
JM
15134 else if (strchr (name, ','))
15135 {
15136 /* Generic ".set" directive; use the generic handler. */
15137 *input_line_pointer = ch;
15138 input_line_pointer = name;
15139 s_set (0);
15140 return;
15141 }
252b5132
RH
15142 else
15143 {
15144 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
15145 }
c6278170 15146 mips_check_isa_supports_ases ();
252b5132
RH
15147 *input_line_pointer = ch;
15148 demand_empty_rest_of_line ();
15149}
15150
15151/* Handle the .abicalls pseudo-op. I believe this is equivalent to
15152 .option pic2. It means to generate SVR4 PIC calls. */
15153
15154static void
17a2f251 15155s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
15156{
15157 mips_pic = SVR4_PIC;
143d77c5 15158 mips_abicalls = TRUE;
4d0d148d
TS
15159
15160 if (g_switch_seen && g_switch_value != 0)
15161 as_warn (_("-G may not be used with SVR4 PIC code"));
15162 g_switch_value = 0;
15163
252b5132
RH
15164 bfd_set_gp_size (stdoutput, 0);
15165 demand_empty_rest_of_line ();
15166}
15167
15168/* Handle the .cpload pseudo-op. This is used when generating SVR4
15169 PIC code. It sets the $gp register for the function based on the
15170 function address, which is in the register named in the argument.
15171 This uses a relocation against _gp_disp, which is handled specially
15172 by the linker. The result is:
15173 lui $gp,%hi(_gp_disp)
15174 addiu $gp,$gp,%lo(_gp_disp)
15175 addu $gp,$gp,.cpload argument
aa6975fb
ILT
15176 The .cpload argument is normally $25 == $t9.
15177
15178 The -mno-shared option changes this to:
bbe506e8
TS
15179 lui $gp,%hi(__gnu_local_gp)
15180 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
15181 and the argument is ignored. This saves an instruction, but the
15182 resulting code is not position independent; it uses an absolute
bbe506e8
TS
15183 address for __gnu_local_gp. Thus code assembled with -mno-shared
15184 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
15185
15186static void
17a2f251 15187s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
15188{
15189 expressionS ex;
aa6975fb
ILT
15190 int reg;
15191 int in_shared;
252b5132 15192
6478892d
TS
15193 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
15194 .cpload is ignored. */
15195 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
15196 {
15197 s_ignore (0);
15198 return;
15199 }
15200
a276b80c
MR
15201 if (mips_opts.mips16)
15202 {
15203 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
15204 ignore_rest_of_line ();
15205 return;
15206 }
15207
d3ecfc59 15208 /* .cpload should be in a .set noreorder section. */
252b5132
RH
15209 if (mips_opts.noreorder == 0)
15210 as_warn (_(".cpload not in noreorder section"));
15211
aa6975fb
ILT
15212 reg = tc_get_register (0);
15213
15214 /* If we need to produce a 64-bit address, we are better off using
15215 the default instruction sequence. */
aed1a261 15216 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 15217
252b5132 15218 ex.X_op = O_symbol;
bbe506e8
TS
15219 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
15220 "__gnu_local_gp");
252b5132
RH
15221 ex.X_op_symbol = NULL;
15222 ex.X_add_number = 0;
15223
15224 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 15225 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 15226
8a75745d
MR
15227 mips_mark_labels ();
15228 mips_assembling_insn = TRUE;
15229
584892a6 15230 macro_start ();
67c0d1eb
RS
15231 macro_build_lui (&ex, mips_gp_register);
15232 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 15233 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
15234 if (in_shared)
15235 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
15236 mips_gp_register, reg);
584892a6 15237 macro_end ();
252b5132 15238
8a75745d 15239 mips_assembling_insn = FALSE;
252b5132
RH
15240 demand_empty_rest_of_line ();
15241}
15242
6478892d
TS
15243/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
15244 .cpsetup $reg1, offset|$reg2, label
15245
15246 If offset is given, this results in:
15247 sd $gp, offset($sp)
956cd1d6 15248 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
15249 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
15250 daddu $gp, $gp, $reg1
6478892d
TS
15251
15252 If $reg2 is given, this results in:
15253 daddu $reg2, $gp, $0
956cd1d6 15254 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
15255 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
15256 daddu $gp, $gp, $reg1
aa6975fb
ILT
15257 $reg1 is normally $25 == $t9.
15258
15259 The -mno-shared option replaces the last three instructions with
15260 lui $gp,%hi(_gp)
54f4ddb3 15261 addiu $gp,$gp,%lo(_gp) */
aa6975fb 15262
6478892d 15263static void
17a2f251 15264s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
15265{
15266 expressionS ex_off;
15267 expressionS ex_sym;
15268 int reg1;
6478892d 15269
8586fc66 15270 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
15271 We also need NewABI support. */
15272 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
15273 {
15274 s_ignore (0);
15275 return;
15276 }
15277
a276b80c
MR
15278 if (mips_opts.mips16)
15279 {
15280 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
15281 ignore_rest_of_line ();
15282 return;
15283 }
15284
6478892d
TS
15285 reg1 = tc_get_register (0);
15286 SKIP_WHITESPACE ();
15287 if (*input_line_pointer != ',')
15288 {
15289 as_bad (_("missing argument separator ',' for .cpsetup"));
15290 return;
15291 }
15292 else
80245285 15293 ++input_line_pointer;
6478892d
TS
15294 SKIP_WHITESPACE ();
15295 if (*input_line_pointer == '$')
80245285
TS
15296 {
15297 mips_cpreturn_register = tc_get_register (0);
15298 mips_cpreturn_offset = -1;
15299 }
6478892d 15300 else
80245285
TS
15301 {
15302 mips_cpreturn_offset = get_absolute_expression ();
15303 mips_cpreturn_register = -1;
15304 }
6478892d
TS
15305 SKIP_WHITESPACE ();
15306 if (*input_line_pointer != ',')
15307 {
15308 as_bad (_("missing argument separator ',' for .cpsetup"));
15309 return;
15310 }
15311 else
f9419b05 15312 ++input_line_pointer;
6478892d 15313 SKIP_WHITESPACE ();
f21f8242 15314 expression (&ex_sym);
6478892d 15315
8a75745d
MR
15316 mips_mark_labels ();
15317 mips_assembling_insn = TRUE;
15318
584892a6 15319 macro_start ();
6478892d
TS
15320 if (mips_cpreturn_register == -1)
15321 {
15322 ex_off.X_op = O_constant;
15323 ex_off.X_add_symbol = NULL;
15324 ex_off.X_op_symbol = NULL;
15325 ex_off.X_add_number = mips_cpreturn_offset;
15326
67c0d1eb 15327 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 15328 BFD_RELOC_LO16, SP);
6478892d
TS
15329 }
15330 else
67c0d1eb 15331 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
17a2f251 15332 mips_gp_register, 0);
6478892d 15333
aed1a261 15334 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb 15335 {
df58fc94 15336 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
aa6975fb
ILT
15337 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
15338 BFD_RELOC_HI16_S);
15339
15340 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
15341 mips_gp_register, -1, BFD_RELOC_GPREL16,
15342 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
15343
15344 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
15345 mips_gp_register, reg1);
15346 }
15347 else
15348 {
15349 expressionS ex;
15350
15351 ex.X_op = O_symbol;
4184909a 15352 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
15353 ex.X_op_symbol = NULL;
15354 ex.X_add_number = 0;
6e1304d8 15355
aa6975fb
ILT
15356 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
15357 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
15358
15359 macro_build_lui (&ex, mips_gp_register);
15360 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
15361 mips_gp_register, BFD_RELOC_LO16);
15362 }
f21f8242 15363
584892a6 15364 macro_end ();
6478892d 15365
8a75745d 15366 mips_assembling_insn = FALSE;
6478892d
TS
15367 demand_empty_rest_of_line ();
15368}
15369
15370static void
17a2f251 15371s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
15372{
15373 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
54f4ddb3 15374 .cplocal is ignored. */
6478892d
TS
15375 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
15376 {
15377 s_ignore (0);
15378 return;
15379 }
15380
a276b80c
MR
15381 if (mips_opts.mips16)
15382 {
15383 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
15384 ignore_rest_of_line ();
15385 return;
15386 }
15387
6478892d 15388 mips_gp_register = tc_get_register (0);
85b51719 15389 demand_empty_rest_of_line ();
6478892d
TS
15390}
15391
252b5132
RH
15392/* Handle the .cprestore pseudo-op. This stores $gp into a given
15393 offset from $sp. The offset is remembered, and after making a PIC
15394 call $gp is restored from that location. */
15395
15396static void
17a2f251 15397s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
15398{
15399 expressionS ex;
252b5132 15400
6478892d 15401 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 15402 .cprestore is ignored. */
6478892d 15403 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
15404 {
15405 s_ignore (0);
15406 return;
15407 }
15408
a276b80c
MR
15409 if (mips_opts.mips16)
15410 {
15411 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
15412 ignore_rest_of_line ();
15413 return;
15414 }
15415
252b5132 15416 mips_cprestore_offset = get_absolute_expression ();
7a621144 15417 mips_cprestore_valid = 1;
252b5132
RH
15418
15419 ex.X_op = O_constant;
15420 ex.X_add_symbol = NULL;
15421 ex.X_op_symbol = NULL;
15422 ex.X_add_number = mips_cprestore_offset;
15423
8a75745d
MR
15424 mips_mark_labels ();
15425 mips_assembling_insn = TRUE;
15426
584892a6 15427 macro_start ();
67c0d1eb
RS
15428 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
15429 SP, HAVE_64BIT_ADDRESSES);
584892a6 15430 macro_end ();
252b5132 15431
8a75745d 15432 mips_assembling_insn = FALSE;
252b5132
RH
15433 demand_empty_rest_of_line ();
15434}
15435
6478892d 15436/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 15437 was given in the preceding .cpsetup, it results in:
6478892d 15438 ld $gp, offset($sp)
76b3015f 15439
6478892d 15440 If a register $reg2 was given there, it results in:
54f4ddb3
TS
15441 daddu $gp, $reg2, $0 */
15442
6478892d 15443static void
17a2f251 15444s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
15445{
15446 expressionS ex;
6478892d
TS
15447
15448 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
15449 We also need NewABI support. */
15450 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
15451 {
15452 s_ignore (0);
15453 return;
15454 }
15455
a276b80c
MR
15456 if (mips_opts.mips16)
15457 {
15458 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
15459 ignore_rest_of_line ();
15460 return;
15461 }
15462
8a75745d
MR
15463 mips_mark_labels ();
15464 mips_assembling_insn = TRUE;
15465
584892a6 15466 macro_start ();
6478892d
TS
15467 if (mips_cpreturn_register == -1)
15468 {
15469 ex.X_op = O_constant;
15470 ex.X_add_symbol = NULL;
15471 ex.X_op_symbol = NULL;
15472 ex.X_add_number = mips_cpreturn_offset;
15473
67c0d1eb 15474 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
15475 }
15476 else
67c0d1eb 15477 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
17a2f251 15478 mips_cpreturn_register, 0);
584892a6 15479 macro_end ();
6478892d 15480
8a75745d 15481 mips_assembling_insn = FALSE;
6478892d
TS
15482 demand_empty_rest_of_line ();
15483}
15484
d0f13682
CLT
15485/* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
15486 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
15487 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
15488 debug information or MIPS16 TLS. */
741d6ea8
JM
15489
15490static void
d0f13682
CLT
15491s_tls_rel_directive (const size_t bytes, const char *dirstr,
15492 bfd_reloc_code_real_type rtype)
741d6ea8
JM
15493{
15494 expressionS ex;
15495 char *p;
15496
15497 expression (&ex);
15498
15499 if (ex.X_op != O_symbol)
15500 {
d0f13682 15501 as_bad (_("Unsupported use of %s"), dirstr);
741d6ea8
JM
15502 ignore_rest_of_line ();
15503 }
15504
15505 p = frag_more (bytes);
15506 md_number_to_chars (p, 0, bytes);
d0f13682 15507 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
741d6ea8 15508 demand_empty_rest_of_line ();
de64cffd 15509 mips_clear_insn_labels ();
741d6ea8
JM
15510}
15511
15512/* Handle .dtprelword. */
15513
15514static void
15515s_dtprelword (int ignore ATTRIBUTE_UNUSED)
15516{
d0f13682 15517 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
741d6ea8
JM
15518}
15519
15520/* Handle .dtpreldword. */
15521
15522static void
15523s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
15524{
d0f13682
CLT
15525 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
15526}
15527
15528/* Handle .tprelword. */
15529
15530static void
15531s_tprelword (int ignore ATTRIBUTE_UNUSED)
15532{
15533 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
15534}
15535
15536/* Handle .tpreldword. */
15537
15538static void
15539s_tpreldword (int ignore ATTRIBUTE_UNUSED)
15540{
15541 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
741d6ea8
JM
15542}
15543
6478892d
TS
15544/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
15545 code. It sets the offset to use in gp_rel relocations. */
15546
15547static void
17a2f251 15548s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
15549{
15550 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
15551 We also need NewABI support. */
15552 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
15553 {
15554 s_ignore (0);
15555 return;
15556 }
15557
def2e0dd 15558 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
15559
15560 demand_empty_rest_of_line ();
15561}
15562
252b5132
RH
15563/* Handle the .gpword pseudo-op. This is used when generating PIC
15564 code. It generates a 32 bit GP relative reloc. */
15565
15566static void
17a2f251 15567s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132 15568{
a8dbcb85
TS
15569 segment_info_type *si;
15570 struct insn_label_list *l;
252b5132
RH
15571 expressionS ex;
15572 char *p;
15573
15574 /* When not generating PIC code, this is treated as .word. */
15575 if (mips_pic != SVR4_PIC)
15576 {
15577 s_cons (2);
15578 return;
15579 }
15580
a8dbcb85
TS
15581 si = seg_info (now_seg);
15582 l = si->label_list;
7d10b47d 15583 mips_emit_delays ();
252b5132 15584 if (auto_align)
462427c4 15585 mips_align (2, 0, l);
252b5132
RH
15586
15587 expression (&ex);
a1facbec 15588 mips_clear_insn_labels ();
252b5132
RH
15589
15590 if (ex.X_op != O_symbol || ex.X_add_number != 0)
15591 {
15592 as_bad (_("Unsupported use of .gpword"));
15593 ignore_rest_of_line ();
15594 }
15595
15596 p = frag_more (4);
17a2f251 15597 md_number_to_chars (p, 0, 4);
b34976b6 15598 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 15599 BFD_RELOC_GPREL32);
252b5132
RH
15600
15601 demand_empty_rest_of_line ();
15602}
15603
10181a0d 15604static void
17a2f251 15605s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d 15606{
a8dbcb85
TS
15607 segment_info_type *si;
15608 struct insn_label_list *l;
10181a0d
AO
15609 expressionS ex;
15610 char *p;
15611
15612 /* When not generating PIC code, this is treated as .dword. */
15613 if (mips_pic != SVR4_PIC)
15614 {
15615 s_cons (3);
15616 return;
15617 }
15618
a8dbcb85
TS
15619 si = seg_info (now_seg);
15620 l = si->label_list;
7d10b47d 15621 mips_emit_delays ();
10181a0d 15622 if (auto_align)
462427c4 15623 mips_align (3, 0, l);
10181a0d
AO
15624
15625 expression (&ex);
a1facbec 15626 mips_clear_insn_labels ();
10181a0d
AO
15627
15628 if (ex.X_op != O_symbol || ex.X_add_number != 0)
15629 {
15630 as_bad (_("Unsupported use of .gpdword"));
15631 ignore_rest_of_line ();
15632 }
15633
15634 p = frag_more (8);
17a2f251 15635 md_number_to_chars (p, 0, 8);
a105a300 15636 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
6e1304d8 15637 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
15638
15639 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8
RS
15640 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
15641 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
15642
15643 demand_empty_rest_of_line ();
15644}
15645
a3f278e2
CM
15646/* Handle the .ehword pseudo-op. This is used when generating unwinding
15647 tables. It generates a R_MIPS_EH reloc. */
15648
15649static void
15650s_ehword (int ignore ATTRIBUTE_UNUSED)
15651{
15652 expressionS ex;
15653 char *p;
15654
15655 mips_emit_delays ();
15656
15657 expression (&ex);
15658 mips_clear_insn_labels ();
15659
15660 if (ex.X_op != O_symbol || ex.X_add_number != 0)
15661 {
15662 as_bad (_("Unsupported use of .ehword"));
15663 ignore_rest_of_line ();
15664 }
15665
15666 p = frag_more (4);
15667 md_number_to_chars (p, 0, 4);
15668 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
15669 BFD_RELOC_MIPS_EH);
15670
15671 demand_empty_rest_of_line ();
15672}
15673
252b5132
RH
15674/* Handle the .cpadd pseudo-op. This is used when dealing with switch
15675 tables in SVR4 PIC code. */
15676
15677static void
17a2f251 15678s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 15679{
252b5132
RH
15680 int reg;
15681
10181a0d
AO
15682 /* This is ignored when not generating SVR4 PIC code. */
15683 if (mips_pic != SVR4_PIC)
252b5132
RH
15684 {
15685 s_ignore (0);
15686 return;
15687 }
15688
8a75745d
MR
15689 mips_mark_labels ();
15690 mips_assembling_insn = TRUE;
15691
252b5132 15692 /* Add $gp to the register named as an argument. */
584892a6 15693 macro_start ();
252b5132 15694 reg = tc_get_register (0);
67c0d1eb 15695 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 15696 macro_end ();
252b5132 15697
8a75745d 15698 mips_assembling_insn = FALSE;
bdaaa2e1 15699 demand_empty_rest_of_line ();
252b5132
RH
15700}
15701
15702/* Handle the .insn pseudo-op. This marks instruction labels in
df58fc94 15703 mips16/micromips mode. This permits the linker to handle them specially,
252b5132
RH
15704 such as generating jalx instructions when needed. We also make
15705 them odd for the duration of the assembly, in order to generate the
15706 right sort of code. We will make them even in the adjust_symtab
15707 routine, while leaving them marked. This is convenient for the
15708 debugger and the disassembler. The linker knows to make them odd
15709 again. */
15710
15711static void
17a2f251 15712s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 15713{
df58fc94 15714 mips_mark_labels ();
252b5132
RH
15715
15716 demand_empty_rest_of_line ();
15717}
15718
ba92f887
MR
15719/* Handle the .nan pseudo-op. */
15720
15721static void
15722s_nan (int ignore ATTRIBUTE_UNUSED)
15723{
15724 static const char str_legacy[] = "legacy";
15725 static const char str_2008[] = "2008";
15726 size_t i;
15727
15728 for (i = 0; !is_end_of_line[(unsigned char) input_line_pointer[i]]; i++);
15729
15730 if (i == sizeof (str_2008) - 1
15731 && memcmp (input_line_pointer, str_2008, i) == 0)
15732 mips_flag_nan2008 = TRUE;
15733 else if (i == sizeof (str_legacy) - 1
15734 && memcmp (input_line_pointer, str_legacy, i) == 0)
15735 mips_flag_nan2008 = FALSE;
15736 else
15737 as_bad (_("Bad .nan directive"));
15738
15739 input_line_pointer += i;
15740 demand_empty_rest_of_line ();
15741}
15742
754e2bb9
RS
15743/* Handle a .stab[snd] directive. Ideally these directives would be
15744 implemented in a transparent way, so that removing them would not
15745 have any effect on the generated instructions. However, s_stab
15746 internally changes the section, so in practice we need to decide
15747 now whether the preceding label marks compressed code. We do not
15748 support changing the compression mode of a label after a .stab*
15749 directive, such as in:
15750
15751 foo:
15752 .stabs ...
15753 .set mips16
15754
15755 so the current mode wins. */
252b5132
RH
15756
15757static void
17a2f251 15758s_mips_stab (int type)
252b5132 15759{
754e2bb9 15760 mips_mark_labels ();
252b5132
RH
15761 s_stab (type);
15762}
15763
54f4ddb3 15764/* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
252b5132
RH
15765
15766static void
17a2f251 15767s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
15768{
15769 char *name;
15770 int c;
15771 symbolS *symbolP;
15772 expressionS exp;
15773
15774 name = input_line_pointer;
15775 c = get_symbol_end ();
15776 symbolP = symbol_find_or_make (name);
15777 S_SET_WEAK (symbolP);
15778 *input_line_pointer = c;
15779
15780 SKIP_WHITESPACE ();
15781
15782 if (! is_end_of_line[(unsigned char) *input_line_pointer])
15783 {
15784 if (S_IS_DEFINED (symbolP))
15785 {
20203fb9 15786 as_bad (_("ignoring attempt to redefine symbol %s"),
252b5132
RH
15787 S_GET_NAME (symbolP));
15788 ignore_rest_of_line ();
15789 return;
15790 }
bdaaa2e1 15791
252b5132
RH
15792 if (*input_line_pointer == ',')
15793 {
15794 ++input_line_pointer;
15795 SKIP_WHITESPACE ();
15796 }
bdaaa2e1 15797
252b5132
RH
15798 expression (&exp);
15799 if (exp.X_op != O_symbol)
15800 {
20203fb9 15801 as_bad (_("bad .weakext directive"));
98d3f06f 15802 ignore_rest_of_line ();
252b5132
RH
15803 return;
15804 }
49309057 15805 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
15806 }
15807
15808 demand_empty_rest_of_line ();
15809}
15810
15811/* Parse a register string into a number. Called from the ECOFF code
15812 to parse .frame. The argument is non-zero if this is the frame
15813 register, so that we can record it in mips_frame_reg. */
15814
15815int
17a2f251 15816tc_get_register (int frame)
252b5132 15817{
707bfff6 15818 unsigned int reg;
252b5132
RH
15819
15820 SKIP_WHITESPACE ();
707bfff6
TS
15821 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
15822 reg = 0;
252b5132 15823 if (frame)
7a621144
DJ
15824 {
15825 mips_frame_reg = reg != 0 ? reg : SP;
15826 mips_frame_reg_valid = 1;
15827 mips_cprestore_valid = 0;
15828 }
252b5132
RH
15829 return reg;
15830}
15831
15832valueT
17a2f251 15833md_section_align (asection *seg, valueT addr)
252b5132
RH
15834{
15835 int align = bfd_get_section_alignment (stdoutput, seg);
15836
f3ded42a
RS
15837 /* We don't need to align ELF sections to the full alignment.
15838 However, Irix 5 may prefer that we align them at least to a 16
15839 byte boundary. We don't bother to align the sections if we
15840 are targeted for an embedded system. */
15841 if (strncmp (TARGET_OS, "elf", 3) == 0)
15842 return addr;
15843 if (align > 4)
15844 align = 4;
252b5132
RH
15845
15846 return ((addr + (1 << align) - 1) & (-1 << align));
15847}
15848
15849/* Utility routine, called from above as well. If called while the
15850 input file is still being read, it's only an approximation. (For
15851 example, a symbol may later become defined which appeared to be
15852 undefined earlier.) */
15853
15854static int
17a2f251 15855nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
15856{
15857 if (sym == 0)
15858 return 0;
15859
4d0d148d 15860 if (g_switch_value > 0)
252b5132
RH
15861 {
15862 const char *symname;
15863 int change;
15864
c9914766 15865 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
15866 register. It can be if it is smaller than the -G size or if
15867 it is in the .sdata or .sbss section. Certain symbols can
c9914766 15868 not be referenced off the $gp, although it appears as though
252b5132
RH
15869 they can. */
15870 symname = S_GET_NAME (sym);
15871 if (symname != (const char *) NULL
15872 && (strcmp (symname, "eprol") == 0
15873 || strcmp (symname, "etext") == 0
15874 || strcmp (symname, "_gp") == 0
15875 || strcmp (symname, "edata") == 0
15876 || strcmp (symname, "_fbss") == 0
15877 || strcmp (symname, "_fdata") == 0
15878 || strcmp (symname, "_ftext") == 0
15879 || strcmp (symname, "end") == 0
15880 || strcmp (symname, "_gp_disp") == 0))
15881 change = 1;
15882 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
15883 && (0
15884#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
15885 || (symbol_get_obj (sym)->ecoff_extern_size != 0
15886 && (symbol_get_obj (sym)->ecoff_extern_size
15887 <= g_switch_value))
252b5132
RH
15888#endif
15889 /* We must defer this decision until after the whole
15890 file has been read, since there might be a .extern
15891 after the first use of this symbol. */
15892 || (before_relaxing
15893#ifndef NO_ECOFF_DEBUGGING
49309057 15894 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
15895#endif
15896 && S_GET_VALUE (sym) == 0)
15897 || (S_GET_VALUE (sym) != 0
15898 && S_GET_VALUE (sym) <= g_switch_value)))
15899 change = 0;
15900 else
15901 {
15902 const char *segname;
15903
15904 segname = segment_name (S_GET_SEGMENT (sym));
9c2799c2 15905 gas_assert (strcmp (segname, ".lit8") != 0
252b5132
RH
15906 && strcmp (segname, ".lit4") != 0);
15907 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
15908 && strcmp (segname, ".sbss") != 0
15909 && strncmp (segname, ".sdata.", 7) != 0
d4dc2f22
TS
15910 && strncmp (segname, ".sbss.", 6) != 0
15911 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
fba2b7f9 15912 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
15913 }
15914 return change;
15915 }
15916 else
c9914766 15917 /* We are not optimizing for the $gp register. */
252b5132
RH
15918 return 1;
15919}
15920
5919d012
RS
15921
15922/* Return true if the given symbol should be considered local for SVR4 PIC. */
15923
15924static bfd_boolean
17a2f251 15925pic_need_relax (symbolS *sym, asection *segtype)
5919d012
RS
15926{
15927 asection *symsec;
5919d012
RS
15928
15929 /* Handle the case of a symbol equated to another symbol. */
15930 while (symbol_equated_reloc_p (sym))
15931 {
15932 symbolS *n;
15933
5f0fe04b 15934 /* It's possible to get a loop here in a badly written program. */
5919d012
RS
15935 n = symbol_get_value_expression (sym)->X_add_symbol;
15936 if (n == sym)
15937 break;
15938 sym = n;
15939 }
15940
df1f3cda
DD
15941 if (symbol_section_p (sym))
15942 return TRUE;
15943
5919d012
RS
15944 symsec = S_GET_SEGMENT (sym);
15945
5919d012 15946 /* This must duplicate the test in adjust_reloc_syms. */
45dfa85a
AM
15947 return (!bfd_is_und_section (symsec)
15948 && !bfd_is_abs_section (symsec)
5f0fe04b
TS
15949 && !bfd_is_com_section (symsec)
15950 && !s_is_linkonce (sym, segtype)
5919d012 15951 /* A global or weak symbol is treated as external. */
f3ded42a 15952 && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
5919d012
RS
15953}
15954
15955
252b5132
RH
15956/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
15957 extended opcode. SEC is the section the frag is in. */
15958
15959static int
17a2f251 15960mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132
RH
15961{
15962 int type;
3ccad066 15963 const struct mips_int_operand *operand;
252b5132 15964 offsetT val;
252b5132 15965 segT symsec;
98aa84af 15966 fragS *sym_frag;
252b5132
RH
15967
15968 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
15969 return 0;
15970 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
15971 return 1;
15972
15973 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
3ccad066 15974 operand = mips16_immed_operand (type, FALSE);
252b5132 15975
98aa84af 15976 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 15977 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 15978 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132 15979
3ccad066 15980 if (operand->root.type == OP_PCREL)
252b5132 15981 {
3ccad066 15982 const struct mips_pcrel_operand *pcrel_op;
252b5132 15983 addressT addr;
3ccad066 15984 offsetT maxtiny;
252b5132
RH
15985
15986 /* We won't have the section when we are called from
15987 mips_relax_frag. However, we will always have been called
15988 from md_estimate_size_before_relax first. If this is a
15989 branch to a different section, we mark it as such. If SEC is
15990 NULL, and the frag is not marked, then it must be a branch to
15991 the same section. */
3ccad066 15992 pcrel_op = (const struct mips_pcrel_operand *) operand;
252b5132
RH
15993 if (sec == NULL)
15994 {
15995 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
15996 return 1;
15997 }
15998 else
15999 {
98aa84af 16000 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
16001 if (symsec != sec)
16002 {
16003 fragp->fr_subtype =
16004 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
16005
16006 /* FIXME: We should support this, and let the linker
16007 catch branches and loads that are out of range. */
16008 as_bad_where (fragp->fr_file, fragp->fr_line,
16009 _("unsupported PC relative reference to different section"));
16010
16011 return 1;
16012 }
98aa84af
AM
16013 if (fragp != sym_frag && sym_frag->fr_address == 0)
16014 /* Assume non-extended on the first relaxation pass.
16015 The address we have calculated will be bogus if this is
16016 a forward branch to another frag, as the forward frag
16017 will have fr_address == 0. */
16018 return 0;
252b5132
RH
16019 }
16020
16021 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
16022 the same section. If the relax_marker of the symbol fragment
16023 differs from the relax_marker of this fragment, we have not
16024 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
16025 in STRETCH in order to get a better estimate of the address.
16026 This particularly matters because of the shift bits. */
16027 if (stretch != 0
98aa84af 16028 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
16029 {
16030 fragS *f;
16031
16032 /* Adjust stretch for any alignment frag. Note that if have
16033 been expanding the earlier code, the symbol may be
16034 defined in what appears to be an earlier frag. FIXME:
16035 This doesn't handle the fr_subtype field, which specifies
16036 a maximum number of bytes to skip when doing an
16037 alignment. */
98aa84af 16038 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
16039 {
16040 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
16041 {
16042 if (stretch < 0)
16043 stretch = - ((- stretch)
16044 & ~ ((1 << (int) f->fr_offset) - 1));
16045 else
16046 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
16047 if (stretch == 0)
16048 break;
16049 }
16050 }
16051 if (f != NULL)
16052 val += stretch;
16053 }
16054
16055 addr = fragp->fr_address + fragp->fr_fix;
16056
16057 /* The base address rules are complicated. The base address of
16058 a branch is the following instruction. The base address of a
16059 PC relative load or add is the instruction itself, but if it
16060 is in a delay slot (in which case it can not be extended) use
16061 the address of the instruction whose delay slot it is in. */
3ccad066 16062 if (pcrel_op->include_isa_bit)
252b5132
RH
16063 {
16064 addr += 2;
16065
16066 /* If we are currently assuming that this frag should be
16067 extended, then, the current address is two bytes
bdaaa2e1 16068 higher. */
252b5132
RH
16069 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
16070 addr += 2;
16071
16072 /* Ignore the low bit in the target, since it will be set
16073 for a text label. */
3ccad066 16074 val &= -2;
252b5132
RH
16075 }
16076 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
16077 addr -= 4;
16078 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
16079 addr -= 2;
16080
3ccad066 16081 val -= addr & -(1 << pcrel_op->align_log2);
252b5132
RH
16082
16083 /* If any of the shifted bits are set, we must use an extended
16084 opcode. If the address depends on the size of this
16085 instruction, this can lead to a loop, so we arrange to always
16086 use an extended opcode. We only check this when we are in
16087 the main relaxation loop, when SEC is NULL. */
3ccad066 16088 if ((val & ((1 << operand->shift) - 1)) != 0 && sec == NULL)
252b5132
RH
16089 {
16090 fragp->fr_subtype =
16091 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
16092 return 1;
16093 }
16094
16095 /* If we are about to mark a frag as extended because the value
3ccad066
RS
16096 is precisely the next value above maxtiny, then there is a
16097 chance of an infinite loop as in the following code:
252b5132
RH
16098 la $4,foo
16099 .skip 1020
16100 .align 2
16101 foo:
16102 In this case when the la is extended, foo is 0x3fc bytes
16103 away, so the la can be shrunk, but then foo is 0x400 away, so
16104 the la must be extended. To avoid this loop, we mark the
16105 frag as extended if it was small, and is about to become
3ccad066
RS
16106 extended with the next value above maxtiny. */
16107 maxtiny = mips_int_operand_max (operand);
16108 if (val == maxtiny + (1 << operand->shift)
252b5132
RH
16109 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
16110 && sec == NULL)
16111 {
16112 fragp->fr_subtype =
16113 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
16114 return 1;
16115 }
16116 }
16117 else if (symsec != absolute_section && sec != NULL)
16118 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
16119
3ccad066 16120 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
252b5132
RH
16121}
16122
4a6a3df4
AO
16123/* Compute the length of a branch sequence, and adjust the
16124 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
16125 worst-case length is computed, with UPDATE being used to indicate
16126 whether an unconditional (-1), branch-likely (+1) or regular (0)
16127 branch is to be computed. */
16128static int
17a2f251 16129relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 16130{
b34976b6 16131 bfd_boolean toofar;
4a6a3df4
AO
16132 int length;
16133
16134 if (fragp
16135 && S_IS_DEFINED (fragp->fr_symbol)
16136 && sec == S_GET_SEGMENT (fragp->fr_symbol))
16137 {
16138 addressT addr;
16139 offsetT val;
16140
16141 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
16142
16143 addr = fragp->fr_address + fragp->fr_fix + 4;
16144
16145 val -= addr;
16146
16147 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
16148 }
16149 else if (fragp)
16150 /* If the symbol is not defined or it's in a different segment,
16151 assume the user knows what's going on and emit a short
16152 branch. */
b34976b6 16153 toofar = FALSE;
4a6a3df4 16154 else
b34976b6 16155 toofar = TRUE;
4a6a3df4
AO
16156
16157 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
16158 fragp->fr_subtype
66b3e8da
MR
16159 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
16160 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
16161 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
16162 RELAX_BRANCH_LINK (fragp->fr_subtype),
16163 toofar);
16164
16165 length = 4;
16166 if (toofar)
16167 {
16168 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
16169 length += 8;
16170
16171 if (mips_pic != NO_PIC)
16172 {
16173 /* Additional space for PIC loading of target address. */
16174 length += 8;
16175 if (mips_opts.isa == ISA_MIPS1)
16176 /* Additional space for $at-stabilizing nop. */
16177 length += 4;
16178 }
16179
16180 /* If branch is conditional. */
16181 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
16182 length += 8;
16183 }
b34976b6 16184
4a6a3df4
AO
16185 return length;
16186}
16187
df58fc94
RS
16188/* Compute the length of a branch sequence, and adjust the
16189 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
16190 worst-case length is computed, with UPDATE being used to indicate
16191 whether an unconditional (-1), or regular (0) branch is to be
16192 computed. */
16193
16194static int
16195relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
16196{
16197 bfd_boolean toofar;
16198 int length;
16199
16200 if (fragp
16201 && S_IS_DEFINED (fragp->fr_symbol)
16202 && sec == S_GET_SEGMENT (fragp->fr_symbol))
16203 {
16204 addressT addr;
16205 offsetT val;
16206
16207 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
16208 /* Ignore the low bit in the target, since it will be set
16209 for a text label. */
16210 if ((val & 1) != 0)
16211 --val;
16212
16213 addr = fragp->fr_address + fragp->fr_fix + 4;
16214
16215 val -= addr;
16216
16217 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
16218 }
16219 else if (fragp)
16220 /* If the symbol is not defined or it's in a different segment,
16221 assume the user knows what's going on and emit a short
16222 branch. */
16223 toofar = FALSE;
16224 else
16225 toofar = TRUE;
16226
16227 if (fragp && update
16228 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
16229 fragp->fr_subtype = (toofar
16230 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
16231 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
16232
16233 length = 4;
16234 if (toofar)
16235 {
16236 bfd_boolean compact_known = fragp != NULL;
16237 bfd_boolean compact = FALSE;
16238 bfd_boolean uncond;
16239
16240 if (compact_known)
16241 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
16242 if (fragp)
16243 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
16244 else
16245 uncond = update < 0;
16246
16247 /* If label is out of range, we turn branch <br>:
16248
16249 <br> label # 4 bytes
16250 0:
16251
16252 into:
16253
16254 j label # 4 bytes
16255 nop # 2 bytes if compact && !PIC
16256 0:
16257 */
16258 if (mips_pic == NO_PIC && (!compact_known || compact))
16259 length += 2;
16260
16261 /* If assembling PIC code, we further turn:
16262
16263 j label # 4 bytes
16264
16265 into:
16266
16267 lw/ld at, %got(label)(gp) # 4 bytes
16268 d/addiu at, %lo(label) # 4 bytes
16269 jr/c at # 2 bytes
16270 */
16271 if (mips_pic != NO_PIC)
16272 length += 6;
16273
16274 /* If branch <br> is conditional, we prepend negated branch <brneg>:
16275
16276 <brneg> 0f # 4 bytes
16277 nop # 2 bytes if !compact
16278 */
16279 if (!uncond)
16280 length += (compact_known && compact) ? 4 : 6;
16281 }
16282
16283 return length;
16284}
16285
16286/* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
16287 bit accordingly. */
16288
16289static int
16290relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
16291{
16292 bfd_boolean toofar;
16293
df58fc94
RS
16294 if (fragp
16295 && S_IS_DEFINED (fragp->fr_symbol)
16296 && sec == S_GET_SEGMENT (fragp->fr_symbol))
16297 {
16298 addressT addr;
16299 offsetT val;
16300 int type;
16301
16302 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
16303 /* Ignore the low bit in the target, since it will be set
16304 for a text label. */
16305 if ((val & 1) != 0)
16306 --val;
16307
16308 /* Assume this is a 2-byte branch. */
16309 addr = fragp->fr_address + fragp->fr_fix + 2;
16310
16311 /* We try to avoid the infinite loop by not adding 2 more bytes for
16312 long branches. */
16313
16314 val -= addr;
16315
16316 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
16317 if (type == 'D')
16318 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
16319 else if (type == 'E')
16320 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
16321 else
16322 abort ();
16323 }
16324 else
16325 /* If the symbol is not defined or it's in a different segment,
16326 we emit a normal 32-bit branch. */
16327 toofar = TRUE;
16328
16329 if (fragp && update
16330 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
16331 fragp->fr_subtype
16332 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
16333 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
16334
16335 if (toofar)
16336 return 4;
16337
16338 return 2;
16339}
16340
252b5132
RH
16341/* Estimate the size of a frag before relaxing. Unless this is the
16342 mips16, we are not really relaxing here, and the final size is
16343 encoded in the subtype information. For the mips16, we have to
16344 decide whether we are using an extended opcode or not. */
16345
252b5132 16346int
17a2f251 16347md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 16348{
5919d012 16349 int change;
252b5132 16350
4a6a3df4
AO
16351 if (RELAX_BRANCH_P (fragp->fr_subtype))
16352 {
16353
b34976b6
AM
16354 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
16355
4a6a3df4
AO
16356 return fragp->fr_var;
16357 }
16358
252b5132 16359 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
16360 /* We don't want to modify the EXTENDED bit here; it might get us
16361 into infinite loops. We change it only in mips_relax_frag(). */
16362 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132 16363
df58fc94
RS
16364 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
16365 {
16366 int length = 4;
16367
16368 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
16369 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
16370 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
16371 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
16372 fragp->fr_var = length;
16373
16374 return length;
16375 }
16376
252b5132 16377 if (mips_pic == NO_PIC)
5919d012 16378 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132 16379 else if (mips_pic == SVR4_PIC)
5919d012 16380 change = pic_need_relax (fragp->fr_symbol, segtype);
0a44bf69
RS
16381 else if (mips_pic == VXWORKS_PIC)
16382 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
16383 change = 0;
252b5132
RH
16384 else
16385 abort ();
16386
16387 if (change)
16388 {
4d7206a2 16389 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 16390 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 16391 }
4d7206a2
RS
16392 else
16393 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
16394}
16395
16396/* This is called to see whether a reloc against a defined symbol
de7e6852 16397 should be converted into a reloc against a section. */
252b5132
RH
16398
16399int
17a2f251 16400mips_fix_adjustable (fixS *fixp)
252b5132 16401{
252b5132
RH
16402 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
16403 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
16404 return 0;
a161fe53 16405
252b5132
RH
16406 if (fixp->fx_addsy == NULL)
16407 return 1;
a161fe53 16408
de7e6852
RS
16409 /* If symbol SYM is in a mergeable section, relocations of the form
16410 SYM + 0 can usually be made section-relative. The mergeable data
16411 is then identified by the section offset rather than by the symbol.
16412
16413 However, if we're generating REL LO16 relocations, the offset is split
16414 between the LO16 and parterning high part relocation. The linker will
16415 need to recalculate the complete offset in order to correctly identify
16416 the merge data.
16417
16418 The linker has traditionally not looked for the parterning high part
16419 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
16420 placed anywhere. Rather than break backwards compatibility by changing
16421 this, it seems better not to force the issue, and instead keep the
16422 original symbol. This will work with either linker behavior. */
738e5348 16423 if ((lo16_reloc_p (fixp->fx_r_type)
704803a9 16424 || reloc_needs_lo_p (fixp->fx_r_type))
de7e6852
RS
16425 && HAVE_IN_PLACE_ADDENDS
16426 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
16427 return 0;
16428
ce70d90a 16429 /* There is no place to store an in-place offset for JALR relocations.
2de39019
CM
16430 Likewise an in-range offset of limited PC-relative relocations may
16431 overflow the in-place relocatable field if recalculated against the
16432 start address of the symbol's containing section. */
ce70d90a 16433 if (HAVE_IN_PLACE_ADDENDS
2de39019
CM
16434 && (limited_pcrel_reloc_p (fixp->fx_r_type)
16435 || jalr_reloc_p (fixp->fx_r_type)))
1180b5a4
RS
16436 return 0;
16437
b314ec0e
RS
16438 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
16439 to a floating-point stub. The same is true for non-R_MIPS16_26
16440 relocations against MIPS16 functions; in this case, the stub becomes
16441 the function's canonical address.
16442
16443 Floating-point stubs are stored in unique .mips16.call.* or
16444 .mips16.fn.* sections. If a stub T for function F is in section S,
16445 the first relocation in section S must be against F; this is how the
16446 linker determines the target function. All relocations that might
16447 resolve to T must also be against F. We therefore have the following
16448 restrictions, which are given in an intentionally-redundant way:
16449
16450 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
16451 symbols.
16452
16453 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
16454 if that stub might be used.
16455
16456 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
16457 symbols.
16458
16459 4. We cannot reduce a stub's relocations against MIPS16 symbols if
16460 that stub might be used.
16461
16462 There is a further restriction:
16463
df58fc94
RS
16464 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
16465 R_MICROMIPS_26_S1) against MIPS16 or microMIPS symbols on
16466 targets with in-place addends; the relocation field cannot
b314ec0e
RS
16467 encode the low bit.
16468
df58fc94
RS
16469 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
16470 against a MIPS16 symbol. We deal with (5) by by not reducing any
16471 such relocations on REL targets.
b314ec0e
RS
16472
16473 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
16474 relocation against some symbol R, no relocation against R may be
16475 reduced. (Note that this deals with (2) as well as (1) because
16476 relocations against global symbols will never be reduced on ELF
16477 targets.) This approach is a little simpler than trying to detect
16478 stub sections, and gives the "all or nothing" per-symbol consistency
16479 that we have for MIPS16 symbols. */
f3ded42a 16480 if (fixp->fx_subsy == NULL
30c09090 16481 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
df58fc94
RS
16482 || *symbol_get_tc (fixp->fx_addsy)
16483 || (HAVE_IN_PLACE_ADDENDS
16484 && ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
16485 && jmp_reloc_p (fixp->fx_r_type))))
252b5132 16486 return 0;
a161fe53 16487
252b5132
RH
16488 return 1;
16489}
16490
16491/* Translate internal representation of relocation info to BFD target
16492 format. */
16493
16494arelent **
17a2f251 16495tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
16496{
16497 static arelent *retval[4];
16498 arelent *reloc;
16499 bfd_reloc_code_real_type code;
16500
4b0cff4e
TS
16501 memset (retval, 0, sizeof(retval));
16502 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
49309057
ILT
16503 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
16504 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
16505 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
16506
bad36eac
DJ
16507 if (fixp->fx_pcrel)
16508 {
df58fc94
RS
16509 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
16510 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
16511 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
b47468a6
CM
16512 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
16513 || fixp->fx_r_type == BFD_RELOC_32_PCREL);
bad36eac
DJ
16514
16515 /* At this point, fx_addnumber is "symbol offset - pcrel address".
16516 Relocations want only the symbol offset. */
16517 reloc->addend = fixp->fx_addnumber + reloc->address;
bad36eac
DJ
16518 }
16519 else
16520 reloc->addend = fixp->fx_addnumber;
252b5132 16521
438c16b8
TS
16522 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
16523 entry to be used in the relocation's section offset. */
16524 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
16525 {
16526 reloc->address = reloc->addend;
16527 reloc->addend = 0;
16528 }
16529
252b5132 16530 code = fixp->fx_r_type;
252b5132 16531
bad36eac 16532 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
252b5132
RH
16533 if (reloc->howto == NULL)
16534 {
16535 as_bad_where (fixp->fx_file, fixp->fx_line,
16536 _("Can not represent %s relocation in this object file format"),
16537 bfd_get_reloc_code_name (code));
16538 retval[0] = NULL;
16539 }
16540
16541 return retval;
16542}
16543
16544/* Relax a machine dependent frag. This returns the amount by which
16545 the current size of the frag should change. */
16546
16547int
17a2f251 16548mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 16549{
4a6a3df4
AO
16550 if (RELAX_BRANCH_P (fragp->fr_subtype))
16551 {
16552 offsetT old_var = fragp->fr_var;
b34976b6
AM
16553
16554 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
16555
16556 return fragp->fr_var - old_var;
16557 }
16558
df58fc94
RS
16559 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
16560 {
16561 offsetT old_var = fragp->fr_var;
16562 offsetT new_var = 4;
16563
16564 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
16565 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
16566 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
16567 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
16568 fragp->fr_var = new_var;
16569
16570 return new_var - old_var;
16571 }
16572
252b5132
RH
16573 if (! RELAX_MIPS16_P (fragp->fr_subtype))
16574 return 0;
16575
c4e7957c 16576 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
16577 {
16578 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
16579 return 0;
16580 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
16581 return 2;
16582 }
16583 else
16584 {
16585 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
16586 return 0;
16587 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
16588 return -2;
16589 }
16590
16591 return 0;
16592}
16593
16594/* Convert a machine dependent frag. */
16595
16596void
17a2f251 16597md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 16598{
4a6a3df4
AO
16599 if (RELAX_BRANCH_P (fragp->fr_subtype))
16600 {
4d68580a 16601 char *buf;
4a6a3df4
AO
16602 unsigned long insn;
16603 expressionS exp;
16604 fixS *fixp;
b34976b6 16605
4d68580a
RS
16606 buf = fragp->fr_literal + fragp->fr_fix;
16607 insn = read_insn (buf);
b34976b6 16608
4a6a3df4
AO
16609 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
16610 {
16611 /* We generate a fixup instead of applying it right now
16612 because, if there are linker relaxations, we're going to
16613 need the relocations. */
16614 exp.X_op = O_symbol;
16615 exp.X_add_symbol = fragp->fr_symbol;
16616 exp.X_add_number = fragp->fr_offset;
16617
4d68580a
RS
16618 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
16619 BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
16620 fixp->fx_file = fragp->fr_file;
16621 fixp->fx_line = fragp->fr_line;
b34976b6 16622
4d68580a 16623 buf = write_insn (buf, insn);
4a6a3df4
AO
16624 }
16625 else
16626 {
16627 int i;
16628
16629 as_warn_where (fragp->fr_file, fragp->fr_line,
5c4f07ba 16630 _("Relaxed out-of-range branch into a jump"));
4a6a3df4
AO
16631
16632 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
16633 goto uncond;
16634
16635 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
16636 {
16637 /* Reverse the branch. */
16638 switch ((insn >> 28) & 0xf)
16639 {
16640 case 4:
3bf0dbfb
MR
16641 /* bc[0-3][tf]l? instructions can have the condition
16642 reversed by tweaking a single TF bit, and their
16643 opcodes all have 0x4???????. */
16644 gas_assert ((insn & 0xf3e00000) == 0x41000000);
4a6a3df4
AO
16645 insn ^= 0x00010000;
16646 break;
16647
16648 case 0:
16649 /* bltz 0x04000000 bgez 0x04010000
54f4ddb3 16650 bltzal 0x04100000 bgezal 0x04110000 */
9c2799c2 16651 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
4a6a3df4
AO
16652 insn ^= 0x00010000;
16653 break;
b34976b6 16654
4a6a3df4
AO
16655 case 1:
16656 /* beq 0x10000000 bne 0x14000000
54f4ddb3 16657 blez 0x18000000 bgtz 0x1c000000 */
4a6a3df4
AO
16658 insn ^= 0x04000000;
16659 break;
16660
16661 default:
16662 abort ();
16663 }
16664 }
16665
16666 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
16667 {
16668 /* Clear the and-link bit. */
9c2799c2 16669 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
4a6a3df4 16670
54f4ddb3
TS
16671 /* bltzal 0x04100000 bgezal 0x04110000
16672 bltzall 0x04120000 bgezall 0x04130000 */
4a6a3df4
AO
16673 insn &= ~0x00100000;
16674 }
16675
16676 /* Branch over the branch (if the branch was likely) or the
16677 full jump (not likely case). Compute the offset from the
16678 current instruction to branch to. */
16679 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
16680 i = 16;
16681 else
16682 {
16683 /* How many bytes in instructions we've already emitted? */
4d68580a 16684 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
16685 /* How many bytes in instructions from here to the end? */
16686 i = fragp->fr_var - i;
16687 }
16688 /* Convert to instruction count. */
16689 i >>= 2;
16690 /* Branch counts from the next instruction. */
b34976b6 16691 i--;
4a6a3df4
AO
16692 insn |= i;
16693 /* Branch over the jump. */
4d68580a 16694 buf = write_insn (buf, insn);
4a6a3df4 16695
54f4ddb3 16696 /* nop */
4d68580a 16697 buf = write_insn (buf, 0);
4a6a3df4
AO
16698
16699 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
16700 {
16701 /* beql $0, $0, 2f */
16702 insn = 0x50000000;
16703 /* Compute the PC offset from the current instruction to
16704 the end of the variable frag. */
16705 /* How many bytes in instructions we've already emitted? */
4d68580a 16706 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
16707 /* How many bytes in instructions from here to the end? */
16708 i = fragp->fr_var - i;
16709 /* Convert to instruction count. */
16710 i >>= 2;
16711 /* Don't decrement i, because we want to branch over the
16712 delay slot. */
4a6a3df4 16713 insn |= i;
4a6a3df4 16714
4d68580a
RS
16715 buf = write_insn (buf, insn);
16716 buf = write_insn (buf, 0);
4a6a3df4
AO
16717 }
16718
16719 uncond:
16720 if (mips_pic == NO_PIC)
16721 {
16722 /* j or jal. */
16723 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
16724 ? 0x0c000000 : 0x08000000);
16725 exp.X_op = O_symbol;
16726 exp.X_add_symbol = fragp->fr_symbol;
16727 exp.X_add_number = fragp->fr_offset;
16728
4d68580a
RS
16729 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
16730 FALSE, BFD_RELOC_MIPS_JMP);
4a6a3df4
AO
16731 fixp->fx_file = fragp->fr_file;
16732 fixp->fx_line = fragp->fr_line;
16733
4d68580a 16734 buf = write_insn (buf, insn);
4a6a3df4
AO
16735 }
16736 else
16737 {
66b3e8da
MR
16738 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
16739
4a6a3df4 16740 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
66b3e8da
MR
16741 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
16742 insn |= at << OP_SH_RT;
4a6a3df4
AO
16743 exp.X_op = O_symbol;
16744 exp.X_add_symbol = fragp->fr_symbol;
16745 exp.X_add_number = fragp->fr_offset;
16746
16747 if (fragp->fr_offset)
16748 {
16749 exp.X_add_symbol = make_expr_symbol (&exp);
16750 exp.X_add_number = 0;
16751 }
16752
4d68580a
RS
16753 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
16754 FALSE, BFD_RELOC_MIPS_GOT16);
4a6a3df4
AO
16755 fixp->fx_file = fragp->fr_file;
16756 fixp->fx_line = fragp->fr_line;
16757
4d68580a 16758 buf = write_insn (buf, insn);
b34976b6 16759
4a6a3df4 16760 if (mips_opts.isa == ISA_MIPS1)
4d68580a
RS
16761 /* nop */
16762 buf = write_insn (buf, 0);
4a6a3df4
AO
16763
16764 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
66b3e8da
MR
16765 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
16766 insn |= at << OP_SH_RS | at << OP_SH_RT;
4a6a3df4 16767
4d68580a
RS
16768 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
16769 FALSE, BFD_RELOC_LO16);
4a6a3df4
AO
16770 fixp->fx_file = fragp->fr_file;
16771 fixp->fx_line = fragp->fr_line;
b34976b6 16772
4d68580a 16773 buf = write_insn (buf, insn);
4a6a3df4
AO
16774
16775 /* j(al)r $at. */
16776 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
66b3e8da 16777 insn = 0x0000f809;
4a6a3df4 16778 else
66b3e8da
MR
16779 insn = 0x00000008;
16780 insn |= at << OP_SH_RS;
4a6a3df4 16781
4d68580a 16782 buf = write_insn (buf, insn);
4a6a3df4
AO
16783 }
16784 }
16785
4a6a3df4 16786 fragp->fr_fix += fragp->fr_var;
4d68580a 16787 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
4a6a3df4
AO
16788 return;
16789 }
16790
df58fc94
RS
16791 /* Relax microMIPS branches. */
16792 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
16793 {
4d68580a 16794 char *buf = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
16795 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
16796 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
16797 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
2309ddf2 16798 bfd_boolean short_ds;
df58fc94
RS
16799 unsigned long insn;
16800 expressionS exp;
16801 fixS *fixp;
16802
16803 exp.X_op = O_symbol;
16804 exp.X_add_symbol = fragp->fr_symbol;
16805 exp.X_add_number = fragp->fr_offset;
16806
16807 fragp->fr_fix += fragp->fr_var;
16808
16809 /* Handle 16-bit branches that fit or are forced to fit. */
16810 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
16811 {
16812 /* We generate a fixup instead of applying it right now,
16813 because if there is linker relaxation, we're going to
16814 need the relocations. */
16815 if (type == 'D')
4d68580a 16816 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
df58fc94
RS
16817 BFD_RELOC_MICROMIPS_10_PCREL_S1);
16818 else if (type == 'E')
4d68580a 16819 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
df58fc94
RS
16820 BFD_RELOC_MICROMIPS_7_PCREL_S1);
16821 else
16822 abort ();
16823
16824 fixp->fx_file = fragp->fr_file;
16825 fixp->fx_line = fragp->fr_line;
16826
16827 /* These relocations can have an addend that won't fit in
16828 2 octets. */
16829 fixp->fx_no_overflow = 1;
16830
16831 return;
16832 }
16833
2309ddf2 16834 /* Handle 32-bit branches that fit or are forced to fit. */
df58fc94
RS
16835 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
16836 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
16837 {
16838 /* We generate a fixup instead of applying it right now,
16839 because if there is linker relaxation, we're going to
16840 need the relocations. */
4d68580a
RS
16841 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
16842 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
16843 fixp->fx_file = fragp->fr_file;
16844 fixp->fx_line = fragp->fr_line;
16845
16846 if (type == 0)
16847 return;
16848 }
16849
16850 /* Relax 16-bit branches to 32-bit branches. */
16851 if (type != 0)
16852 {
4d68580a 16853 insn = read_compressed_insn (buf, 2);
df58fc94
RS
16854
16855 if ((insn & 0xfc00) == 0xcc00) /* b16 */
16856 insn = 0x94000000; /* beq */
16857 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
16858 {
16859 unsigned long regno;
16860
16861 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
16862 regno = micromips_to_32_reg_d_map [regno];
16863 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
16864 insn |= regno << MICROMIPSOP_SH_RS;
16865 }
16866 else
16867 abort ();
16868
16869 /* Nothing else to do, just write it out. */
16870 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
16871 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
16872 {
4d68580a
RS
16873 buf = write_compressed_insn (buf, insn, 4);
16874 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
16875 return;
16876 }
16877 }
16878 else
4d68580a 16879 insn = read_compressed_insn (buf, 4);
df58fc94
RS
16880
16881 /* Relax 32-bit branches to a sequence of instructions. */
16882 as_warn_where (fragp->fr_file, fragp->fr_line,
16883 _("Relaxed out-of-range branch into a jump"));
16884
2309ddf2
MR
16885 /* Set the short-delay-slot bit. */
16886 short_ds = al && (insn & 0x02000000) != 0;
df58fc94
RS
16887
16888 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
16889 {
16890 symbolS *l;
16891
16892 /* Reverse the branch. */
16893 if ((insn & 0xfc000000) == 0x94000000 /* beq */
16894 || (insn & 0xfc000000) == 0xb4000000) /* bne */
16895 insn ^= 0x20000000;
16896 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
16897 || (insn & 0xffe00000) == 0x40400000 /* bgez */
16898 || (insn & 0xffe00000) == 0x40800000 /* blez */
16899 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
16900 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
16901 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
16902 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
16903 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
16904 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
16905 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
16906 insn ^= 0x00400000;
16907 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
16908 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
16909 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
16910 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
16911 insn ^= 0x00200000;
16912 else
16913 abort ();
16914
16915 if (al)
16916 {
16917 /* Clear the and-link and short-delay-slot bits. */
16918 gas_assert ((insn & 0xfda00000) == 0x40200000);
16919
16920 /* bltzal 0x40200000 bgezal 0x40600000 */
16921 /* bltzals 0x42200000 bgezals 0x42600000 */
16922 insn &= ~0x02200000;
16923 }
16924
16925 /* Make a label at the end for use with the branch. */
16926 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
16927 micromips_label_inc ();
f3ded42a 16928 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
df58fc94
RS
16929
16930 /* Refer to it. */
4d68580a
RS
16931 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
16932 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
16933 fixp->fx_file = fragp->fr_file;
16934 fixp->fx_line = fragp->fr_line;
16935
16936 /* Branch over the jump. */
4d68580a 16937 buf = write_compressed_insn (buf, insn, 4);
df58fc94 16938 if (!compact)
4d68580a
RS
16939 /* nop */
16940 buf = write_compressed_insn (buf, 0x0c00, 2);
df58fc94
RS
16941 }
16942
16943 if (mips_pic == NO_PIC)
16944 {
2309ddf2
MR
16945 unsigned long jal = short_ds ? 0x74000000 : 0xf4000000; /* jal/s */
16946
df58fc94
RS
16947 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
16948 insn = al ? jal : 0xd4000000;
16949
4d68580a
RS
16950 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
16951 BFD_RELOC_MICROMIPS_JMP);
df58fc94
RS
16952 fixp->fx_file = fragp->fr_file;
16953 fixp->fx_line = fragp->fr_line;
16954
4d68580a 16955 buf = write_compressed_insn (buf, insn, 4);
df58fc94 16956 if (compact)
4d68580a
RS
16957 /* nop */
16958 buf = write_compressed_insn (buf, 0x0c00, 2);
df58fc94
RS
16959 }
16960 else
16961 {
16962 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
2309ddf2
MR
16963 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
16964 unsigned long jr = compact ? 0x45a0 : 0x4580; /* jr/c */
df58fc94
RS
16965
16966 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
16967 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
16968 insn |= at << MICROMIPSOP_SH_RT;
16969
16970 if (exp.X_add_number)
16971 {
16972 exp.X_add_symbol = make_expr_symbol (&exp);
16973 exp.X_add_number = 0;
16974 }
16975
4d68580a
RS
16976 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
16977 BFD_RELOC_MICROMIPS_GOT16);
df58fc94
RS
16978 fixp->fx_file = fragp->fr_file;
16979 fixp->fx_line = fragp->fr_line;
16980
4d68580a 16981 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
16982
16983 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
16984 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
16985 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
16986
4d68580a
RS
16987 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
16988 BFD_RELOC_MICROMIPS_LO16);
df58fc94
RS
16989 fixp->fx_file = fragp->fr_file;
16990 fixp->fx_line = fragp->fr_line;
16991
4d68580a 16992 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
16993
16994 /* jr/jrc/jalr/jalrs $at */
16995 insn = al ? jalr : jr;
16996 insn |= at << MICROMIPSOP_SH_MJ;
16997
4d68580a 16998 buf = write_compressed_insn (buf, insn, 2);
df58fc94
RS
16999 }
17000
4d68580a 17001 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
17002 return;
17003 }
17004
252b5132
RH
17005 if (RELAX_MIPS16_P (fragp->fr_subtype))
17006 {
17007 int type;
3ccad066 17008 const struct mips_int_operand *operand;
252b5132 17009 offsetT val;
5c04167a
RS
17010 char *buf;
17011 unsigned int user_length, length;
252b5132 17012 unsigned long insn;
5c04167a 17013 bfd_boolean ext;
252b5132
RH
17014
17015 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
3ccad066 17016 operand = mips16_immed_operand (type, FALSE);
252b5132 17017
5c04167a 17018 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
5f5f22c0 17019 val = resolve_symbol_value (fragp->fr_symbol);
3ccad066 17020 if (operand->root.type == OP_PCREL)
252b5132 17021 {
3ccad066 17022 const struct mips_pcrel_operand *pcrel_op;
252b5132
RH
17023 addressT addr;
17024
3ccad066 17025 pcrel_op = (const struct mips_pcrel_operand *) operand;
252b5132
RH
17026 addr = fragp->fr_address + fragp->fr_fix;
17027
17028 /* The rules for the base address of a PC relative reloc are
17029 complicated; see mips16_extended_frag. */
3ccad066 17030 if (pcrel_op->include_isa_bit)
252b5132
RH
17031 {
17032 addr += 2;
17033 if (ext)
17034 addr += 2;
17035 /* Ignore the low bit in the target, since it will be
17036 set for a text label. */
3ccad066 17037 val &= -2;
252b5132
RH
17038 }
17039 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17040 addr -= 4;
17041 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17042 addr -= 2;
17043
3ccad066 17044 addr &= -(1 << pcrel_op->align_log2);
252b5132
RH
17045 val -= addr;
17046
17047 /* Make sure the section winds up with the alignment we have
17048 assumed. */
3ccad066
RS
17049 if (operand->shift > 0)
17050 record_alignment (asec, operand->shift);
252b5132
RH
17051 }
17052
17053 if (ext
17054 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
17055 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
17056 as_warn_where (fragp->fr_file, fragp->fr_line,
17057 _("extended instruction in delay slot"));
17058
5c04167a 17059 buf = fragp->fr_literal + fragp->fr_fix;
252b5132 17060
4d68580a 17061 insn = read_compressed_insn (buf, 2);
5c04167a
RS
17062 if (ext)
17063 insn |= MIPS16_EXTEND;
252b5132 17064
5c04167a
RS
17065 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17066 user_length = 4;
17067 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17068 user_length = 2;
17069 else
17070 user_length = 0;
17071
43c0598f 17072 mips16_immed (fragp->fr_file, fragp->fr_line, type,
c150d1d2 17073 BFD_RELOC_UNUSED, val, user_length, &insn);
252b5132 17074
5c04167a
RS
17075 length = (ext ? 4 : 2);
17076 gas_assert (mips16_opcode_length (insn) == length);
17077 write_compressed_insn (buf, insn, length);
17078 fragp->fr_fix += length;
252b5132
RH
17079 }
17080 else
17081 {
df58fc94
RS
17082 relax_substateT subtype = fragp->fr_subtype;
17083 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
17084 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
4d7206a2
RS
17085 int first, second;
17086 fixS *fixp;
252b5132 17087
df58fc94
RS
17088 first = RELAX_FIRST (subtype);
17089 second = RELAX_SECOND (subtype);
4d7206a2 17090 fixp = (fixS *) fragp->fr_opcode;
252b5132 17091
df58fc94
RS
17092 /* If the delay slot chosen does not match the size of the instruction,
17093 then emit a warning. */
17094 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
17095 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
17096 {
17097 relax_substateT s;
17098 const char *msg;
17099
17100 s = subtype & (RELAX_DELAY_SLOT_16BIT
17101 | RELAX_DELAY_SLOT_SIZE_FIRST
17102 | RELAX_DELAY_SLOT_SIZE_SECOND);
17103 msg = macro_warning (s);
17104 if (msg != NULL)
db9b2be4 17105 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94
RS
17106 subtype &= ~s;
17107 }
17108
584892a6 17109 /* Possibly emit a warning if we've chosen the longer option. */
df58fc94 17110 if (use_second == second_longer)
584892a6 17111 {
df58fc94
RS
17112 relax_substateT s;
17113 const char *msg;
17114
17115 s = (subtype
17116 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
17117 msg = macro_warning (s);
17118 if (msg != NULL)
db9b2be4 17119 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94 17120 subtype &= ~s;
584892a6
RS
17121 }
17122
4d7206a2
RS
17123 /* Go through all the fixups for the first sequence. Disable them
17124 (by marking them as done) if we're going to use the second
17125 sequence instead. */
17126 while (fixp
17127 && fixp->fx_frag == fragp
17128 && fixp->fx_where < fragp->fr_fix - second)
17129 {
df58fc94 17130 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
17131 fixp->fx_done = 1;
17132 fixp = fixp->fx_next;
17133 }
252b5132 17134
4d7206a2
RS
17135 /* Go through the fixups for the second sequence. Disable them if
17136 we're going to use the first sequence, otherwise adjust their
17137 addresses to account for the relaxation. */
17138 while (fixp && fixp->fx_frag == fragp)
17139 {
df58fc94 17140 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
17141 fixp->fx_where -= first;
17142 else
17143 fixp->fx_done = 1;
17144 fixp = fixp->fx_next;
17145 }
17146
17147 /* Now modify the frag contents. */
df58fc94 17148 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
17149 {
17150 char *start;
17151
17152 start = fragp->fr_literal + fragp->fr_fix - first - second;
17153 memmove (start, start + first, second);
17154 fragp->fr_fix -= first;
17155 }
17156 else
17157 fragp->fr_fix -= second;
252b5132
RH
17158 }
17159}
17160
252b5132
RH
17161/* This function is called after the relocs have been generated.
17162 We've been storing mips16 text labels as odd. Here we convert them
17163 back to even for the convenience of the debugger. */
17164
17165void
17a2f251 17166mips_frob_file_after_relocs (void)
252b5132
RH
17167{
17168 asymbol **syms;
17169 unsigned int count, i;
17170
252b5132
RH
17171 syms = bfd_get_outsymbols (stdoutput);
17172 count = bfd_get_symcount (stdoutput);
17173 for (i = 0; i < count; i++, syms++)
df58fc94
RS
17174 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
17175 && ((*syms)->value & 1) != 0)
17176 {
17177 (*syms)->value &= ~1;
17178 /* If the symbol has an odd size, it was probably computed
17179 incorrectly, so adjust that as well. */
17180 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
17181 ++elf_symbol (*syms)->internal_elf_sym.st_size;
17182 }
252b5132
RH
17183}
17184
a1facbec
MR
17185/* This function is called whenever a label is defined, including fake
17186 labels instantiated off the dot special symbol. It is used when
17187 handling branch delays; if a branch has a label, we assume we cannot
17188 move it. This also bumps the value of the symbol by 1 in compressed
17189 code. */
252b5132 17190
e1b47bd5 17191static void
a1facbec 17192mips_record_label (symbolS *sym)
252b5132 17193{
a8dbcb85 17194 segment_info_type *si = seg_info (now_seg);
252b5132
RH
17195 struct insn_label_list *l;
17196
17197 if (free_insn_labels == NULL)
17198 l = (struct insn_label_list *) xmalloc (sizeof *l);
17199 else
17200 {
17201 l = free_insn_labels;
17202 free_insn_labels = l->next;
17203 }
17204
17205 l->label = sym;
a8dbcb85
TS
17206 l->next = si->label_list;
17207 si->label_list = l;
a1facbec 17208}
07a53e5c 17209
a1facbec
MR
17210/* This function is called as tc_frob_label() whenever a label is defined
17211 and adds a DWARF-2 record we only want for true labels. */
17212
17213void
17214mips_define_label (symbolS *sym)
17215{
17216 mips_record_label (sym);
07a53e5c 17217 dwarf2_emit_label (sym);
252b5132 17218}
e1b47bd5
RS
17219
17220/* This function is called by tc_new_dot_label whenever a new dot symbol
17221 is defined. */
17222
17223void
17224mips_add_dot_label (symbolS *sym)
17225{
17226 mips_record_label (sym);
17227 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
17228 mips_compressed_mark_label (sym);
17229}
252b5132 17230\f
252b5132
RH
17231/* Some special processing for a MIPS ELF file. */
17232
17233void
17a2f251 17234mips_elf_final_processing (void)
252b5132
RH
17235{
17236 /* Write out the register information. */
316f5878 17237 if (mips_abi != N64_ABI)
252b5132
RH
17238 {
17239 Elf32_RegInfo s;
17240
17241 s.ri_gprmask = mips_gprmask;
17242 s.ri_cprmask[0] = mips_cprmask[0];
17243 s.ri_cprmask[1] = mips_cprmask[1];
17244 s.ri_cprmask[2] = mips_cprmask[2];
17245 s.ri_cprmask[3] = mips_cprmask[3];
17246 /* The gp_value field is set by the MIPS ELF backend. */
17247
17248 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
17249 ((Elf32_External_RegInfo *)
17250 mips_regmask_frag));
17251 }
17252 else
17253 {
17254 Elf64_Internal_RegInfo s;
17255
17256 s.ri_gprmask = mips_gprmask;
17257 s.ri_pad = 0;
17258 s.ri_cprmask[0] = mips_cprmask[0];
17259 s.ri_cprmask[1] = mips_cprmask[1];
17260 s.ri_cprmask[2] = mips_cprmask[2];
17261 s.ri_cprmask[3] = mips_cprmask[3];
17262 /* The gp_value field is set by the MIPS ELF backend. */
17263
17264 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
17265 ((Elf64_External_RegInfo *)
17266 mips_regmask_frag));
17267 }
17268
17269 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
17270 sort of BFD interface for this. */
17271 if (mips_any_noreorder)
17272 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
17273 if (mips_pic != NO_PIC)
143d77c5 17274 {
8b828383 17275 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
17276 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
17277 }
17278 if (mips_abicalls)
17279 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 17280
b015e599
AP
17281 /* Set MIPS ELF flags for ASEs. Note that not all ASEs have flags
17282 defined at present; this might need to change in future. */
a4672219
TS
17283 if (file_ase_mips16)
17284 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
df58fc94
RS
17285 if (file_ase_micromips)
17286 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
846ef2d0 17287 if (file_ase & ASE_MDMX)
deec1734 17288 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 17289
bdaaa2e1 17290 /* Set the MIPS ELF ABI flags. */
316f5878 17291 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 17292 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 17293 else if (mips_abi == O64_ABI)
252b5132 17294 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 17295 else if (mips_abi == EABI_ABI)
252b5132 17296 {
316f5878 17297 if (!file_mips_gp32)
252b5132
RH
17298 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
17299 else
17300 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
17301 }
316f5878 17302 else if (mips_abi == N32_ABI)
be00bddd
TS
17303 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
17304
c9914766 17305 /* Nothing to do for N64_ABI. */
252b5132
RH
17306
17307 if (mips_32bitmode)
17308 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
ad3fea08 17309
ba92f887
MR
17310 if (mips_flag_nan2008)
17311 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NAN2008;
17312
ad3fea08
TS
17313#if 0 /* XXX FIXME */
17314 /* 32 bit code with 64 bit FP registers. */
17315 if (!file_mips_fp32 && ABI_NEEDS_32BIT_REGS (mips_abi))
17316 elf_elfheader (stdoutput)->e_flags |= ???;
17317#endif
252b5132 17318}
252b5132 17319\f
beae10d5 17320typedef struct proc {
9b2f1d35
EC
17321 symbolS *func_sym;
17322 symbolS *func_end_sym;
beae10d5
KH
17323 unsigned long reg_mask;
17324 unsigned long reg_offset;
17325 unsigned long fpreg_mask;
17326 unsigned long fpreg_offset;
17327 unsigned long frame_offset;
17328 unsigned long frame_reg;
17329 unsigned long pc_reg;
17330} procS;
252b5132
RH
17331
17332static procS cur_proc;
17333static procS *cur_proc_ptr;
17334static int numprocs;
17335
df58fc94
RS
17336/* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
17337 as "2", and a normal nop as "0". */
17338
17339#define NOP_OPCODE_MIPS 0
17340#define NOP_OPCODE_MIPS16 1
17341#define NOP_OPCODE_MICROMIPS 2
742a56fe
RS
17342
17343char
17344mips_nop_opcode (void)
17345{
df58fc94
RS
17346 if (seg_info (now_seg)->tc_segment_info_data.micromips)
17347 return NOP_OPCODE_MICROMIPS;
17348 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
17349 return NOP_OPCODE_MIPS16;
17350 else
17351 return NOP_OPCODE_MIPS;
742a56fe
RS
17352}
17353
df58fc94
RS
17354/* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
17355 32-bit microMIPS NOPs here (if applicable). */
a19d8eb0 17356
0a9ef439 17357void
17a2f251 17358mips_handle_align (fragS *fragp)
a19d8eb0 17359{
df58fc94 17360 char nop_opcode;
742a56fe 17361 char *p;
c67a084a
NC
17362 int bytes, size, excess;
17363 valueT opcode;
742a56fe 17364
0a9ef439
RH
17365 if (fragp->fr_type != rs_align_code)
17366 return;
17367
742a56fe 17368 p = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
17369 nop_opcode = *p;
17370 switch (nop_opcode)
a19d8eb0 17371 {
df58fc94
RS
17372 case NOP_OPCODE_MICROMIPS:
17373 opcode = micromips_nop32_insn.insn_opcode;
17374 size = 4;
17375 break;
17376 case NOP_OPCODE_MIPS16:
c67a084a
NC
17377 opcode = mips16_nop_insn.insn_opcode;
17378 size = 2;
df58fc94
RS
17379 break;
17380 case NOP_OPCODE_MIPS:
17381 default:
c67a084a
NC
17382 opcode = nop_insn.insn_opcode;
17383 size = 4;
df58fc94 17384 break;
c67a084a 17385 }
a19d8eb0 17386
c67a084a
NC
17387 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
17388 excess = bytes % size;
df58fc94
RS
17389
17390 /* Handle the leading part if we're not inserting a whole number of
17391 instructions, and make it the end of the fixed part of the frag.
17392 Try to fit in a short microMIPS NOP if applicable and possible,
17393 and use zeroes otherwise. */
17394 gas_assert (excess < 4);
17395 fragp->fr_fix += excess;
17396 switch (excess)
c67a084a 17397 {
df58fc94
RS
17398 case 3:
17399 *p++ = '\0';
17400 /* Fall through. */
17401 case 2:
833794fc 17402 if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
df58fc94 17403 {
4d68580a 17404 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
df58fc94
RS
17405 break;
17406 }
17407 *p++ = '\0';
17408 /* Fall through. */
17409 case 1:
17410 *p++ = '\0';
17411 /* Fall through. */
17412 case 0:
17413 break;
a19d8eb0 17414 }
c67a084a
NC
17415
17416 md_number_to_chars (p, opcode, size);
17417 fragp->fr_var = size;
a19d8eb0
CP
17418}
17419
252b5132 17420static void
17a2f251 17421md_obj_begin (void)
252b5132
RH
17422{
17423}
17424
17425static void
17a2f251 17426md_obj_end (void)
252b5132 17427{
54f4ddb3 17428 /* Check for premature end, nesting errors, etc. */
252b5132 17429 if (cur_proc_ptr)
9a41af64 17430 as_warn (_("missing .end at end of assembly"));
252b5132
RH
17431}
17432
17433static long
17a2f251 17434get_number (void)
252b5132
RH
17435{
17436 int negative = 0;
17437 long val = 0;
17438
17439 if (*input_line_pointer == '-')
17440 {
17441 ++input_line_pointer;
17442 negative = 1;
17443 }
3882b010 17444 if (!ISDIGIT (*input_line_pointer))
956cd1d6 17445 as_bad (_("expected simple number"));
252b5132
RH
17446 if (input_line_pointer[0] == '0')
17447 {
17448 if (input_line_pointer[1] == 'x')
17449 {
17450 input_line_pointer += 2;
3882b010 17451 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
17452 {
17453 val <<= 4;
17454 val |= hex_value (*input_line_pointer++);
17455 }
17456 return negative ? -val : val;
17457 }
17458 else
17459 {
17460 ++input_line_pointer;
3882b010 17461 while (ISDIGIT (*input_line_pointer))
252b5132
RH
17462 {
17463 val <<= 3;
17464 val |= *input_line_pointer++ - '0';
17465 }
17466 return negative ? -val : val;
17467 }
17468 }
3882b010 17469 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
17470 {
17471 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
17472 *input_line_pointer, *input_line_pointer);
956cd1d6 17473 as_warn (_("invalid number"));
252b5132
RH
17474 return -1;
17475 }
3882b010 17476 while (ISDIGIT (*input_line_pointer))
252b5132
RH
17477 {
17478 val *= 10;
17479 val += *input_line_pointer++ - '0';
17480 }
17481 return negative ? -val : val;
17482}
17483
17484/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
17485 is an initial number which is the ECOFF file index. In the non-ECOFF
17486 case .file implies DWARF-2. */
17487
17488static void
17a2f251 17489s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 17490{
ecb4347a
DJ
17491 static int first_file_directive = 0;
17492
c5dd6aab
DJ
17493 if (ECOFF_DEBUGGING)
17494 {
17495 get_number ();
17496 s_app_file (0);
17497 }
17498 else
ecb4347a
DJ
17499 {
17500 char *filename;
17501
17502 filename = dwarf2_directive_file (0);
17503
17504 /* Versions of GCC up to 3.1 start files with a ".file"
17505 directive even for stabs output. Make sure that this
17506 ".file" is handled. Note that you need a version of GCC
17507 after 3.1 in order to support DWARF-2 on MIPS. */
17508 if (filename != NULL && ! first_file_directive)
17509 {
17510 (void) new_logical_line (filename, -1);
c04f5787 17511 s_app_file_string (filename, 0);
ecb4347a
DJ
17512 }
17513 first_file_directive = 1;
17514 }
c5dd6aab
DJ
17515}
17516
17517/* The .loc directive, implying DWARF-2. */
252b5132
RH
17518
17519static void
17a2f251 17520s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 17521{
c5dd6aab
DJ
17522 if (!ECOFF_DEBUGGING)
17523 dwarf2_directive_loc (0);
252b5132
RH
17524}
17525
252b5132
RH
17526/* The .end directive. */
17527
17528static void
17a2f251 17529s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
17530{
17531 symbolS *p;
252b5132 17532
7a621144
DJ
17533 /* Following functions need their own .frame and .cprestore directives. */
17534 mips_frame_reg_valid = 0;
17535 mips_cprestore_valid = 0;
17536
252b5132
RH
17537 if (!is_end_of_line[(unsigned char) *input_line_pointer])
17538 {
17539 p = get_symbol ();
17540 demand_empty_rest_of_line ();
17541 }
17542 else
17543 p = NULL;
17544
14949570 17545 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
17546 as_warn (_(".end not in text section"));
17547
17548 if (!cur_proc_ptr)
17549 {
17550 as_warn (_(".end directive without a preceding .ent directive."));
17551 demand_empty_rest_of_line ();
17552 return;
17553 }
17554
17555 if (p != NULL)
17556 {
9c2799c2 17557 gas_assert (S_GET_NAME (p));
9b2f1d35 17558 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
252b5132 17559 as_warn (_(".end symbol does not match .ent symbol."));
ecb4347a
DJ
17560
17561 if (debug_type == DEBUG_STABS)
17562 stabs_generate_asm_endfunc (S_GET_NAME (p),
17563 S_GET_NAME (p));
252b5132
RH
17564 }
17565 else
17566 as_warn (_(".end directive missing or unknown symbol"));
17567
9b2f1d35
EC
17568 /* Create an expression to calculate the size of the function. */
17569 if (p && cur_proc_ptr)
17570 {
17571 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
17572 expressionS *exp = xmalloc (sizeof (expressionS));
17573
17574 obj->size = exp;
17575 exp->X_op = O_subtract;
17576 exp->X_add_symbol = symbol_temp_new_now ();
17577 exp->X_op_symbol = p;
17578 exp->X_add_number = 0;
17579
17580 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
17581 }
17582
ecb4347a 17583 /* Generate a .pdr section. */
f3ded42a 17584 if (!ECOFF_DEBUGGING && mips_flag_pdr)
ecb4347a
DJ
17585 {
17586 segT saved_seg = now_seg;
17587 subsegT saved_subseg = now_subseg;
ecb4347a
DJ
17588 expressionS exp;
17589 char *fragp;
252b5132 17590
252b5132 17591#ifdef md_flush_pending_output
ecb4347a 17592 md_flush_pending_output ();
252b5132
RH
17593#endif
17594
9c2799c2 17595 gas_assert (pdr_seg);
ecb4347a 17596 subseg_set (pdr_seg, 0);
252b5132 17597
ecb4347a
DJ
17598 /* Write the symbol. */
17599 exp.X_op = O_symbol;
17600 exp.X_add_symbol = p;
17601 exp.X_add_number = 0;
17602 emit_expr (&exp, 4);
252b5132 17603
ecb4347a 17604 fragp = frag_more (7 * 4);
252b5132 17605
17a2f251
TS
17606 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
17607 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
17608 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
17609 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
17610 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
17611 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
17612 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 17613
ecb4347a
DJ
17614 subseg_set (saved_seg, saved_subseg);
17615 }
252b5132
RH
17616
17617 cur_proc_ptr = NULL;
17618}
17619
17620/* The .aent and .ent directives. */
17621
17622static void
17a2f251 17623s_mips_ent (int aent)
252b5132 17624{
252b5132 17625 symbolS *symbolP;
252b5132
RH
17626
17627 symbolP = get_symbol ();
17628 if (*input_line_pointer == ',')
f9419b05 17629 ++input_line_pointer;
252b5132 17630 SKIP_WHITESPACE ();
3882b010 17631 if (ISDIGIT (*input_line_pointer)
d9a62219 17632 || *input_line_pointer == '-')
874e8986 17633 get_number ();
252b5132 17634
14949570 17635 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
17636 as_warn (_(".ent or .aent not in text section."));
17637
17638 if (!aent && cur_proc_ptr)
9a41af64 17639 as_warn (_("missing .end"));
252b5132
RH
17640
17641 if (!aent)
17642 {
7a621144
DJ
17643 /* This function needs its own .frame and .cprestore directives. */
17644 mips_frame_reg_valid = 0;
17645 mips_cprestore_valid = 0;
17646
252b5132
RH
17647 cur_proc_ptr = &cur_proc;
17648 memset (cur_proc_ptr, '\0', sizeof (procS));
17649
9b2f1d35 17650 cur_proc_ptr->func_sym = symbolP;
252b5132 17651
f9419b05 17652 ++numprocs;
ecb4347a
DJ
17653
17654 if (debug_type == DEBUG_STABS)
17655 stabs_generate_asm_func (S_GET_NAME (symbolP),
17656 S_GET_NAME (symbolP));
252b5132
RH
17657 }
17658
7c0fc524
MR
17659 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
17660
252b5132
RH
17661 demand_empty_rest_of_line ();
17662}
17663
17664/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 17665 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 17666 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 17667 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
17668 symbol table (in the mdebug section). */
17669
17670static void
17a2f251 17671s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 17672{
f3ded42a
RS
17673 if (ECOFF_DEBUGGING)
17674 s_ignore (ignore);
17675 else
ecb4347a
DJ
17676 {
17677 long val;
252b5132 17678
ecb4347a
DJ
17679 if (cur_proc_ptr == (procS *) NULL)
17680 {
17681 as_warn (_(".frame outside of .ent"));
17682 demand_empty_rest_of_line ();
17683 return;
17684 }
252b5132 17685
ecb4347a
DJ
17686 cur_proc_ptr->frame_reg = tc_get_register (1);
17687
17688 SKIP_WHITESPACE ();
17689 if (*input_line_pointer++ != ','
17690 || get_absolute_expression_and_terminator (&val) != ',')
17691 {
17692 as_warn (_("Bad .frame directive"));
17693 --input_line_pointer;
17694 demand_empty_rest_of_line ();
17695 return;
17696 }
252b5132 17697
ecb4347a
DJ
17698 cur_proc_ptr->frame_offset = val;
17699 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 17700
252b5132 17701 demand_empty_rest_of_line ();
252b5132 17702 }
252b5132
RH
17703}
17704
bdaaa2e1
KH
17705/* The .fmask and .mask directives. If the mdebug section is present
17706 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 17707 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 17708 information correctly. We can't use the ecoff routines because they
252b5132
RH
17709 make reference to the ecoff symbol table (in the mdebug section). */
17710
17711static void
17a2f251 17712s_mips_mask (int reg_type)
252b5132 17713{
f3ded42a
RS
17714 if (ECOFF_DEBUGGING)
17715 s_ignore (reg_type);
17716 else
252b5132 17717 {
ecb4347a 17718 long mask, off;
252b5132 17719
ecb4347a
DJ
17720 if (cur_proc_ptr == (procS *) NULL)
17721 {
17722 as_warn (_(".mask/.fmask outside of .ent"));
17723 demand_empty_rest_of_line ();
17724 return;
17725 }
252b5132 17726
ecb4347a
DJ
17727 if (get_absolute_expression_and_terminator (&mask) != ',')
17728 {
17729 as_warn (_("Bad .mask/.fmask directive"));
17730 --input_line_pointer;
17731 demand_empty_rest_of_line ();
17732 return;
17733 }
252b5132 17734
ecb4347a
DJ
17735 off = get_absolute_expression ();
17736
17737 if (reg_type == 'F')
17738 {
17739 cur_proc_ptr->fpreg_mask = mask;
17740 cur_proc_ptr->fpreg_offset = off;
17741 }
17742 else
17743 {
17744 cur_proc_ptr->reg_mask = mask;
17745 cur_proc_ptr->reg_offset = off;
17746 }
17747
17748 demand_empty_rest_of_line ();
252b5132 17749 }
252b5132
RH
17750}
17751
316f5878
RS
17752/* A table describing all the processors gas knows about. Names are
17753 matched in the order listed.
e7af610e 17754
316f5878
RS
17755 To ease comparison, please keep this table in the same order as
17756 gcc's mips_cpu_info_table[]. */
e972090a
NC
17757static const struct mips_cpu_info mips_cpu_info_table[] =
17758{
316f5878 17759 /* Entries for generic ISAs */
d16afab6
RS
17760 { "mips1", MIPS_CPU_IS_ISA, 0, ISA_MIPS1, CPU_R3000 },
17761 { "mips2", MIPS_CPU_IS_ISA, 0, ISA_MIPS2, CPU_R6000 },
17762 { "mips3", MIPS_CPU_IS_ISA, 0, ISA_MIPS3, CPU_R4000 },
17763 { "mips4", MIPS_CPU_IS_ISA, 0, ISA_MIPS4, CPU_R8000 },
17764 { "mips5", MIPS_CPU_IS_ISA, 0, ISA_MIPS5, CPU_MIPS5 },
17765 { "mips32", MIPS_CPU_IS_ISA, 0, ISA_MIPS32, CPU_MIPS32 },
17766 { "mips32r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17767 { "mips64", MIPS_CPU_IS_ISA, 0, ISA_MIPS64, CPU_MIPS64 },
17768 { "mips64r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R2, CPU_MIPS64R2 },
316f5878
RS
17769
17770 /* MIPS I */
d16afab6
RS
17771 { "r3000", 0, 0, ISA_MIPS1, CPU_R3000 },
17772 { "r2000", 0, 0, ISA_MIPS1, CPU_R3000 },
17773 { "r3900", 0, 0, ISA_MIPS1, CPU_R3900 },
316f5878
RS
17774
17775 /* MIPS II */
d16afab6 17776 { "r6000", 0, 0, ISA_MIPS2, CPU_R6000 },
316f5878
RS
17777
17778 /* MIPS III */
d16afab6
RS
17779 { "r4000", 0, 0, ISA_MIPS3, CPU_R4000 },
17780 { "r4010", 0, 0, ISA_MIPS2, CPU_R4010 },
17781 { "vr4100", 0, 0, ISA_MIPS3, CPU_VR4100 },
17782 { "vr4111", 0, 0, ISA_MIPS3, CPU_R4111 },
17783 { "vr4120", 0, 0, ISA_MIPS3, CPU_VR4120 },
17784 { "vr4130", 0, 0, ISA_MIPS3, CPU_VR4120 },
17785 { "vr4181", 0, 0, ISA_MIPS3, CPU_R4111 },
17786 { "vr4300", 0, 0, ISA_MIPS3, CPU_R4300 },
17787 { "r4400", 0, 0, ISA_MIPS3, CPU_R4400 },
17788 { "r4600", 0, 0, ISA_MIPS3, CPU_R4600 },
17789 { "orion", 0, 0, ISA_MIPS3, CPU_R4600 },
17790 { "r4650", 0, 0, ISA_MIPS3, CPU_R4650 },
17791 { "r5900", 0, 0, ISA_MIPS3, CPU_R5900 },
b15591bb 17792 /* ST Microelectronics Loongson 2E and 2F cores */
d16afab6
RS
17793 { "loongson2e", 0, 0, ISA_MIPS3, CPU_LOONGSON_2E },
17794 { "loongson2f", 0, 0, ISA_MIPS3, CPU_LOONGSON_2F },
316f5878
RS
17795
17796 /* MIPS IV */
d16afab6
RS
17797 { "r8000", 0, 0, ISA_MIPS4, CPU_R8000 },
17798 { "r10000", 0, 0, ISA_MIPS4, CPU_R10000 },
17799 { "r12000", 0, 0, ISA_MIPS4, CPU_R12000 },
17800 { "r14000", 0, 0, ISA_MIPS4, CPU_R14000 },
17801 { "r16000", 0, 0, ISA_MIPS4, CPU_R16000 },
17802 { "vr5000", 0, 0, ISA_MIPS4, CPU_R5000 },
17803 { "vr5400", 0, 0, ISA_MIPS4, CPU_VR5400 },
17804 { "vr5500", 0, 0, ISA_MIPS4, CPU_VR5500 },
17805 { "rm5200", 0, 0, ISA_MIPS4, CPU_R5000 },
17806 { "rm5230", 0, 0, ISA_MIPS4, CPU_R5000 },
17807 { "rm5231", 0, 0, ISA_MIPS4, CPU_R5000 },
17808 { "rm5261", 0, 0, ISA_MIPS4, CPU_R5000 },
17809 { "rm5721", 0, 0, ISA_MIPS4, CPU_R5000 },
17810 { "rm7000", 0, 0, ISA_MIPS4, CPU_RM7000 },
17811 { "rm9000", 0, 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
17812
17813 /* MIPS 32 */
d16afab6
RS
17814 { "4kc", 0, 0, ISA_MIPS32, CPU_MIPS32 },
17815 { "4km", 0, 0, ISA_MIPS32, CPU_MIPS32 },
17816 { "4kp", 0, 0, ISA_MIPS32, CPU_MIPS32 },
17817 { "4ksc", 0, ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
ad3fea08
TS
17818
17819 /* MIPS 32 Release 2 */
d16afab6
RS
17820 { "4kec", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17821 { "4kem", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17822 { "4kep", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17823 { "4ksd", 0, ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
17824 { "m4k", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17825 { "m4kp", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17826 { "m14k", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
17827 { "m14kc", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
17828 { "m14ke", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
17829 ISA_MIPS32R2, CPU_MIPS32R2 },
17830 { "m14kec", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
17831 ISA_MIPS32R2, CPU_MIPS32R2 },
17832 { "24kc", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17833 { "24kf2_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17834 { "24kf", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17835 { "24kf1_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 17836 /* Deprecated forms of the above. */
d16afab6
RS
17837 { "24kfx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
17838 { "24kx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 17839 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
d16afab6
RS
17840 { "24kec", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
17841 { "24kef2_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
17842 { "24kef", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
17843 { "24kef1_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 17844 /* Deprecated forms of the above. */
d16afab6
RS
17845 { "24kefx", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
17846 { "24kex", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 17847 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
d16afab6
RS
17848 { "34kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17849 { "34kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17850 { "34kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17851 { "34kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 17852 /* Deprecated forms of the above. */
d16afab6
RS
17853 { "34kfx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17854 { "34kx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
711eefe4 17855 /* 34Kn is a 34kc without DSP. */
d16afab6 17856 { "34kn", 0, ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 17857 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
d16afab6
RS
17858 { "74kc", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
17859 { "74kf2_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
17860 { "74kf", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
17861 { "74kf1_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
17862 { "74kf3_2", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 17863 /* Deprecated forms of the above. */
d16afab6
RS
17864 { "74kfx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
17865 { "74kx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
30f8113a 17866 /* 1004K cores are multiprocessor versions of the 34K. */
d16afab6
RS
17867 { "1004kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17868 { "1004kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17869 { "1004kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
17870 { "1004kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
32b26a03 17871
316f5878 17872 /* MIPS 64 */
d16afab6
RS
17873 { "5kc", 0, 0, ISA_MIPS64, CPU_MIPS64 },
17874 { "5kf", 0, 0, ISA_MIPS64, CPU_MIPS64 },
17875 { "20kc", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
17876 { "25kf", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
ad3fea08 17877
c7a23324 17878 /* Broadcom SB-1 CPU core */
d16afab6 17879 { "sb1", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
1e85aad8 17880 /* Broadcom SB-1A CPU core */
d16afab6 17881 { "sb1a", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
d051516a 17882
d16afab6 17883 { "loongson3a", 0, 0, ISA_MIPS64, CPU_LOONGSON_3A },
e7af610e 17884
ed163775
MR
17885 /* MIPS 64 Release 2 */
17886
967344c6 17887 /* Cavium Networks Octeon CPU core */
d16afab6
RS
17888 { "octeon", 0, 0, ISA_MIPS64R2, CPU_OCTEON },
17889 { "octeon+", 0, 0, ISA_MIPS64R2, CPU_OCTEONP },
17890 { "octeon2", 0, 0, ISA_MIPS64R2, CPU_OCTEON2 },
967344c6 17891
52b6b6b9 17892 /* RMI Xlr */
d16afab6 17893 { "xlr", 0, 0, ISA_MIPS64, CPU_XLR },
52b6b6b9 17894
55a36193
MK
17895 /* Broadcom XLP.
17896 XLP is mostly like XLR, with the prominent exception that it is
17897 MIPS64R2 rather than MIPS64. */
d16afab6 17898 { "xlp", 0, 0, ISA_MIPS64R2, CPU_XLR },
55a36193 17899
316f5878 17900 /* End marker */
d16afab6 17901 { NULL, 0, 0, 0, 0 }
316f5878 17902};
e7af610e 17903
84ea6cf2 17904
316f5878
RS
17905/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
17906 with a final "000" replaced by "k". Ignore case.
e7af610e 17907
316f5878 17908 Note: this function is shared between GCC and GAS. */
c6c98b38 17909
b34976b6 17910static bfd_boolean
17a2f251 17911mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
17912{
17913 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
17914 given++, canonical++;
17915
17916 return ((*given == 0 && *canonical == 0)
17917 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
17918}
17919
17920
17921/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
17922 CPU name. We've traditionally allowed a lot of variation here.
17923
17924 Note: this function is shared between GCC and GAS. */
17925
b34976b6 17926static bfd_boolean
17a2f251 17927mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
17928{
17929 /* First see if the name matches exactly, or with a final "000"
17930 turned into "k". */
17931 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 17932 return TRUE;
316f5878
RS
17933
17934 /* If not, try comparing based on numerical designation alone.
17935 See if GIVEN is an unadorned number, or 'r' followed by a number. */
17936 if (TOLOWER (*given) == 'r')
17937 given++;
17938 if (!ISDIGIT (*given))
b34976b6 17939 return FALSE;
316f5878
RS
17940
17941 /* Skip over some well-known prefixes in the canonical name,
17942 hoping to find a number there too. */
17943 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
17944 canonical += 2;
17945 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
17946 canonical += 2;
17947 else if (TOLOWER (canonical[0]) == 'r')
17948 canonical += 1;
17949
17950 return mips_strict_matching_cpu_name_p (canonical, given);
17951}
17952
17953
17954/* Parse an option that takes the name of a processor as its argument.
17955 OPTION is the name of the option and CPU_STRING is the argument.
17956 Return the corresponding processor enumeration if the CPU_STRING is
17957 recognized, otherwise report an error and return null.
17958
17959 A similar function exists in GCC. */
e7af610e
NC
17960
17961static const struct mips_cpu_info *
17a2f251 17962mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 17963{
316f5878 17964 const struct mips_cpu_info *p;
e7af610e 17965
316f5878
RS
17966 /* 'from-abi' selects the most compatible architecture for the given
17967 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
17968 EABIs, we have to decide whether we're using the 32-bit or 64-bit
17969 version. Look first at the -mgp options, if given, otherwise base
17970 the choice on MIPS_DEFAULT_64BIT.
e7af610e 17971
316f5878
RS
17972 Treat NO_ABI like the EABIs. One reason to do this is that the
17973 plain 'mips' and 'mips64' configs have 'from-abi' as their default
17974 architecture. This code picks MIPS I for 'mips' and MIPS III for
17975 'mips64', just as we did in the days before 'from-abi'. */
17976 if (strcasecmp (cpu_string, "from-abi") == 0)
17977 {
17978 if (ABI_NEEDS_32BIT_REGS (mips_abi))
17979 return mips_cpu_info_from_isa (ISA_MIPS1);
17980
17981 if (ABI_NEEDS_64BIT_REGS (mips_abi))
17982 return mips_cpu_info_from_isa (ISA_MIPS3);
17983
17984 if (file_mips_gp32 >= 0)
17985 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
17986
17987 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
17988 ? ISA_MIPS3
17989 : ISA_MIPS1);
17990 }
17991
17992 /* 'default' has traditionally been a no-op. Probably not very useful. */
17993 if (strcasecmp (cpu_string, "default") == 0)
17994 return 0;
17995
17996 for (p = mips_cpu_info_table; p->name != 0; p++)
17997 if (mips_matching_cpu_name_p (p->name, cpu_string))
17998 return p;
17999
20203fb9 18000 as_bad (_("Bad value (%s) for %s"), cpu_string, option);
316f5878 18001 return 0;
e7af610e
NC
18002}
18003
316f5878
RS
18004/* Return the canonical processor information for ISA (a member of the
18005 ISA_MIPS* enumeration). */
18006
e7af610e 18007static const struct mips_cpu_info *
17a2f251 18008mips_cpu_info_from_isa (int isa)
e7af610e
NC
18009{
18010 int i;
18011
18012 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
ad3fea08 18013 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
316f5878 18014 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
18015 return (&mips_cpu_info_table[i]);
18016
e972090a 18017 return NULL;
e7af610e 18018}
fef14a42
TS
18019
18020static const struct mips_cpu_info *
17a2f251 18021mips_cpu_info_from_arch (int arch)
fef14a42
TS
18022{
18023 int i;
18024
18025 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
18026 if (arch == mips_cpu_info_table[i].cpu)
18027 return (&mips_cpu_info_table[i]);
18028
18029 return NULL;
18030}
316f5878
RS
18031\f
18032static void
17a2f251 18033show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
18034{
18035 if (*first_p)
18036 {
18037 fprintf (stream, "%24s", "");
18038 *col_p = 24;
18039 }
18040 else
18041 {
18042 fprintf (stream, ", ");
18043 *col_p += 2;
18044 }
e7af610e 18045
316f5878
RS
18046 if (*col_p + strlen (string) > 72)
18047 {
18048 fprintf (stream, "\n%24s", "");
18049 *col_p = 24;
18050 }
18051
18052 fprintf (stream, "%s", string);
18053 *col_p += strlen (string);
18054
18055 *first_p = 0;
18056}
18057
18058void
17a2f251 18059md_show_usage (FILE *stream)
e7af610e 18060{
316f5878
RS
18061 int column, first;
18062 size_t i;
18063
18064 fprintf (stream, _("\
18065MIPS options:\n\
316f5878
RS
18066-EB generate big endian output\n\
18067-EL generate little endian output\n\
18068-g, -g2 do not remove unneeded NOPs or swap branches\n\
18069-G NUM allow referencing objects up to NUM bytes\n\
18070 implicitly with the gp register [default 8]\n"));
18071 fprintf (stream, _("\
18072-mips1 generate MIPS ISA I instructions\n\
18073-mips2 generate MIPS ISA II instructions\n\
18074-mips3 generate MIPS ISA III instructions\n\
18075-mips4 generate MIPS ISA IV instructions\n\
18076-mips5 generate MIPS ISA V instructions\n\
18077-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 18078-mips32r2 generate MIPS32 release 2 ISA instructions\n\
316f5878 18079-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 18080-mips64r2 generate MIPS64 release 2 ISA instructions\n\
316f5878
RS
18081-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
18082
18083 first = 1;
e7af610e
NC
18084
18085 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
18086 show (stream, mips_cpu_info_table[i].name, &column, &first);
18087 show (stream, "from-abi", &column, &first);
18088 fputc ('\n', stream);
e7af610e 18089
316f5878
RS
18090 fprintf (stream, _("\
18091-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
18092-no-mCPU don't generate code specific to CPU.\n\
18093 For -mCPU and -no-mCPU, CPU must be one of:\n"));
18094
18095 first = 1;
18096
18097 show (stream, "3900", &column, &first);
18098 show (stream, "4010", &column, &first);
18099 show (stream, "4100", &column, &first);
18100 show (stream, "4650", &column, &first);
18101 fputc ('\n', stream);
18102
18103 fprintf (stream, _("\
18104-mips16 generate mips16 instructions\n\
18105-no-mips16 do not generate mips16 instructions\n"));
18106 fprintf (stream, _("\
df58fc94
RS
18107-mmicromips generate microMIPS instructions\n\
18108-mno-micromips do not generate microMIPS instructions\n"));
18109 fprintf (stream, _("\
e16bfa71
TS
18110-msmartmips generate smartmips instructions\n\
18111-mno-smartmips do not generate smartmips instructions\n"));
18112 fprintf (stream, _("\
74cd071d
CF
18113-mdsp generate DSP instructions\n\
18114-mno-dsp do not generate DSP instructions\n"));
18115 fprintf (stream, _("\
8b082fb1
TS
18116-mdspr2 generate DSP R2 instructions\n\
18117-mno-dspr2 do not generate DSP R2 instructions\n"));
18118 fprintf (stream, _("\
ef2e4d86
CF
18119-mmt generate MT instructions\n\
18120-mno-mt do not generate MT instructions\n"));
18121 fprintf (stream, _("\
dec0624d
MR
18122-mmcu generate MCU instructions\n\
18123-mno-mcu do not generate MCU instructions\n"));
18124 fprintf (stream, _("\
b015e599
AP
18125-mvirt generate Virtualization instructions\n\
18126-mno-virt do not generate Virtualization instructions\n"));
18127 fprintf (stream, _("\
833794fc
MR
18128-minsn32 only generate 32-bit microMIPS instructions\n\
18129-mno-insn32 generate all microMIPS instructions\n"));
18130 fprintf (stream, _("\
c67a084a
NC
18131-mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
18132-mfix-loongson2f-nop work around Loongson2F NOP errata\n\
d766e8ec 18133-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 18134-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
6a32d874 18135-mfix-24k insert a nop after ERET and DERET instructions\n\
d954098f 18136-mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
316f5878
RS
18137-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
18138-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261 18139-msym32 assume all symbols have 32-bit values\n\
316f5878
RS
18140-O0 remove unneeded NOPs, do not swap branches\n\
18141-O remove unneeded NOPs and swap branches\n\
316f5878
RS
18142--trap, --no-break trap exception on div by 0 and mult overflow\n\
18143--break, --no-trap break exception on div by 0 and mult overflow\n"));
037b32b9
AN
18144 fprintf (stream, _("\
18145-mhard-float allow floating-point instructions\n\
18146-msoft-float do not allow floating-point instructions\n\
18147-msingle-float only allow 32-bit floating-point operations\n\
18148-mdouble-float allow 32-bit and 64-bit floating-point operations\n\
3bf0dbfb 18149--[no-]construct-floats [dis]allow floating point values to be constructed\n\
ba92f887
MR
18150--[no-]relax-branch [dis]allow out-of-range branches to be relaxed\n\
18151-mnan=ENCODING select an IEEE 754 NaN encoding convention, either of:\n"));
18152
18153 first = 1;
18154
18155 show (stream, "legacy", &column, &first);
18156 show (stream, "2008", &column, &first);
18157
18158 fputc ('\n', stream);
18159
316f5878
RS
18160 fprintf (stream, _("\
18161-KPIC, -call_shared generate SVR4 position independent code\n\
861fb55a 18162-call_nonpic generate non-PIC code that can operate with DSOs\n\
0c000745 18163-mvxworks-pic generate VxWorks position independent code\n\
861fb55a 18164-non_shared do not generate code that can operate with DSOs\n\
316f5878 18165-xgot assume a 32 bit GOT\n\
dcd410fe 18166-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8 18167-mshared, -mno-shared disable/enable .cpload optimization for\n\
d821e36b 18168 position dependent (non shared) code\n\
316f5878
RS
18169-mabi=ABI create ABI conformant object file for:\n"));
18170
18171 first = 1;
18172
18173 show (stream, "32", &column, &first);
18174 show (stream, "o64", &column, &first);
18175 show (stream, "n32", &column, &first);
18176 show (stream, "64", &column, &first);
18177 show (stream, "eabi", &column, &first);
18178
18179 fputc ('\n', stream);
18180
18181 fprintf (stream, _("\
18182-32 create o32 ABI object file (default)\n\
18183-n32 create n32 ABI object file\n\
18184-64 create 64 ABI object file\n"));
e7af610e 18185}
14e777e0 18186
1575952e 18187#ifdef TE_IRIX
14e777e0 18188enum dwarf2_format
413a266c 18189mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
14e777e0 18190{
369943fe 18191 if (HAVE_64BIT_SYMBOLS)
1575952e 18192 return dwarf2_format_64bit_irix;
14e777e0
KB
18193 else
18194 return dwarf2_format_32bit;
18195}
1575952e 18196#endif
73369e65
EC
18197
18198int
18199mips_dwarf2_addr_size (void)
18200{
6b6b3450 18201 if (HAVE_64BIT_OBJECTS)
73369e65 18202 return 8;
73369e65
EC
18203 else
18204 return 4;
18205}
5862107c
EC
18206
18207/* Standard calling conventions leave the CFA at SP on entry. */
18208void
18209mips_cfi_frame_initial_instructions (void)
18210{
18211 cfi_add_CFA_def_cfa_register (SP);
18212}
18213
707bfff6
TS
18214int
18215tc_mips_regname_to_dw2regnum (char *regname)
18216{
18217 unsigned int regnum = -1;
18218 unsigned int reg;
18219
18220 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
18221 regnum = reg;
18222
18223 return regnum;
18224}
This page took 2.477022 seconds and 4 git commands to generate.