Initial python support.
[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,
e7c604dd 3 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
252b5132
RH
4 Contributed by the OSF and Ralph Campbell.
5 Written by Keith Knowles and Ralph Campbell, working independently.
6 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
7 Support.
8
9 This file is part of GAS.
10
11 GAS is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
ec2655a6 13 the Free Software Foundation; either version 3, or (at your option)
252b5132
RH
14 any later version.
15
16 GAS is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GAS; see the file COPYING. If not, write to the Free
4b4da160
NC
23 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
24 02110-1301, USA. */
252b5132
RH
25
26#include "as.h"
27#include "config.h"
28#include "subsegs.h"
3882b010 29#include "safe-ctype.h"
252b5132 30
252b5132
RH
31#include "opcode/mips.h"
32#include "itbl-ops.h"
c5dd6aab 33#include "dwarf2dbg.h"
5862107c 34#include "dw2gencfi.h"
252b5132
RH
35
36#ifdef DEBUG
37#define DBG(x) printf x
38#else
39#define DBG(x)
40#endif
41
42#ifdef OBJ_MAYBE_ELF
43/* Clean up namespace so we can include obj-elf.h too. */
17a2f251
TS
44static int mips_output_flavor (void);
45static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
252b5132
RH
46#undef OBJ_PROCESS_STAB
47#undef OUTPUT_FLAVOR
48#undef S_GET_ALIGN
49#undef S_GET_SIZE
50#undef S_SET_ALIGN
51#undef S_SET_SIZE
252b5132
RH
52#undef obj_frob_file
53#undef obj_frob_file_after_relocs
54#undef obj_frob_symbol
55#undef obj_pop_insert
56#undef obj_sec_sym_ok_for_reloc
57#undef OBJ_COPY_SYMBOL_ATTRIBUTES
58
59#include "obj-elf.h"
60/* Fix any of them that we actually care about. */
61#undef OUTPUT_FLAVOR
62#define OUTPUT_FLAVOR mips_output_flavor()
63#endif
64
65#if defined (OBJ_ELF)
66#include "elf/mips.h"
67#endif
68
69#ifndef ECOFF_DEBUGGING
70#define NO_ECOFF_DEBUGGING
71#define ECOFF_DEBUGGING 0
72#endif
73
ecb4347a
DJ
74int mips_flag_mdebug = -1;
75
dcd410fe
RO
76/* Control generation of .pdr sections. Off by default on IRIX: the native
77 linker doesn't know about and discards them, but relocations against them
78 remain, leading to rld crashes. */
79#ifdef TE_IRIX
80int mips_flag_pdr = FALSE;
81#else
82int mips_flag_pdr = TRUE;
83#endif
84
252b5132
RH
85#include "ecoff.h"
86
87#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
88static char *mips_regmask_frag;
89#endif
90
85b51719 91#define ZERO 0
741fe287 92#define ATREG 1
252b5132
RH
93#define TREG 24
94#define PIC_CALL_REG 25
95#define KT0 26
96#define KT1 27
97#define GP 28
98#define SP 29
99#define FP 30
100#define RA 31
101
102#define ILLEGAL_REG (32)
103
741fe287
MR
104#define AT mips_opts.at
105
252b5132
RH
106/* Allow override of standard little-endian ECOFF format. */
107
108#ifndef ECOFF_LITTLE_FORMAT
109#define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
110#endif
111
112extern int target_big_endian;
113
252b5132 114/* The name of the readonly data section. */
4d0d148d 115#define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
252b5132 116 ? ".rdata" \
056350c6
NC
117 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
118 ? ".rdata" \
252b5132
RH
119 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
120 ? ".rodata" \
121 : (abort (), ""))
122
47e39b9d
RS
123/* Information about an instruction, including its format, operands
124 and fixups. */
125struct mips_cl_insn
126{
127 /* The opcode's entry in mips_opcodes or mips16_opcodes. */
128 const struct mips_opcode *insn_mo;
129
130 /* True if this is a mips16 instruction and if we want the extended
131 form of INSN_MO. */
132 bfd_boolean use_extend;
133
134 /* The 16-bit extension instruction to use when USE_EXTEND is true. */
135 unsigned short extend;
136
137 /* The 16-bit or 32-bit bitstring of the instruction itself. This is
138 a copy of INSN_MO->match with the operands filled in. */
139 unsigned long insn_opcode;
140
141 /* The frag that contains the instruction. */
142 struct frag *frag;
143
144 /* The offset into FRAG of the first instruction byte. */
145 long where;
146
147 /* The relocs associated with the instruction, if any. */
148 fixS *fixp[3];
149
a38419a5
RS
150 /* True if this entry cannot be moved from its current position. */
151 unsigned int fixed_p : 1;
47e39b9d 152
708587a4 153 /* True if this instruction occurred in a .set noreorder block. */
47e39b9d
RS
154 unsigned int noreorder_p : 1;
155
2fa15973
RS
156 /* True for mips16 instructions that jump to an absolute address. */
157 unsigned int mips16_absolute_jump_p : 1;
47e39b9d
RS
158};
159
a325df1d
TS
160/* The ABI to use. */
161enum mips_abi_level
162{
163 NO_ABI = 0,
164 O32_ABI,
165 O64_ABI,
166 N32_ABI,
167 N64_ABI,
168 EABI_ABI
169};
170
171/* MIPS ABI we are using for this output file. */
316f5878 172static enum mips_abi_level mips_abi = NO_ABI;
a325df1d 173
143d77c5
EC
174/* Whether or not we have code that can call pic code. */
175int mips_abicalls = FALSE;
176
aa6975fb
ILT
177/* Whether or not we have code which can be put into a shared
178 library. */
179static bfd_boolean mips_in_shared = TRUE;
180
252b5132
RH
181/* This is the set of options which may be modified by the .set
182 pseudo-op. We use a struct so that .set push and .set pop are more
183 reliable. */
184
e972090a
NC
185struct mips_set_options
186{
252b5132
RH
187 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
188 if it has not been initialized. Changed by `.set mipsN', and the
189 -mipsN command line option, and the default CPU. */
190 int isa;
1f25f5d3
CD
191 /* Enabled Application Specific Extensions (ASEs). These are set to -1
192 if they have not been initialized. Changed by `.set <asename>', by
193 command line options, and based on the default architecture. */
194 int ase_mips3d;
deec1734 195 int ase_mdmx;
e16bfa71 196 int ase_smartmips;
74cd071d 197 int ase_dsp;
8b082fb1 198 int ase_dspr2;
ef2e4d86 199 int ase_mt;
252b5132
RH
200 /* Whether we are assembling for the mips16 processor. 0 if we are
201 not, 1 if we are, and -1 if the value has not been initialized.
202 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
203 -nomips16 command line options, and the default CPU. */
204 int mips16;
205 /* Non-zero if we should not reorder instructions. Changed by `.set
206 reorder' and `.set noreorder'. */
207 int noreorder;
741fe287
MR
208 /* Non-zero if we should not permit the register designated "assembler
209 temporary" to be used in instructions. The value is the register
210 number, normally $at ($1). Changed by `.set at=REG', `.set noat'
211 (same as `.set at=$0') and `.set at' (same as `.set at=$1'). */
212 unsigned int at;
252b5132
RH
213 /* Non-zero if we should warn when a macro instruction expands into
214 more than one machine instruction. Changed by `.set nomacro' and
215 `.set macro'. */
216 int warn_about_macros;
217 /* Non-zero if we should not move instructions. Changed by `.set
218 move', `.set volatile', `.set nomove', and `.set novolatile'. */
219 int nomove;
220 /* Non-zero if we should not optimize branches by moving the target
221 of the branch into the delay slot. Actually, we don't perform
222 this optimization anyhow. Changed by `.set bopt' and `.set
223 nobopt'. */
224 int nobopt;
225 /* Non-zero if we should not autoextend mips16 instructions.
226 Changed by `.set autoextend' and `.set noautoextend'. */
227 int noautoextend;
a325df1d
TS
228 /* Restrict general purpose registers and floating point registers
229 to 32 bit. This is initially determined when -mgp32 or -mfp32
230 is passed but can changed if the assembler code uses .set mipsN. */
231 int gp32;
232 int fp32;
fef14a42
TS
233 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
234 command line option, and the default CPU. */
235 int arch;
aed1a261
RS
236 /* True if ".set sym32" is in effect. */
237 bfd_boolean sym32;
037b32b9
AN
238 /* True if floating-point operations are not allowed. Changed by .set
239 softfloat or .set hardfloat, by command line options -msoft-float or
240 -mhard-float. The default is false. */
241 bfd_boolean soft_float;
242
243 /* True if only single-precision floating-point operations are allowed.
244 Changed by .set singlefloat or .set doublefloat, command-line options
245 -msingle-float or -mdouble-float. The default is false. */
246 bfd_boolean single_float;
252b5132
RH
247};
248
037b32b9
AN
249/* This is the struct we use to hold the current set of options. Note
250 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
251 -1 to indicate that they have not been initialized. */
252
a325df1d 253/* True if -mgp32 was passed. */
a8e8e863 254static int file_mips_gp32 = -1;
a325df1d
TS
255
256/* True if -mfp32 was passed. */
a8e8e863 257static int file_mips_fp32 = -1;
a325df1d 258
037b32b9
AN
259/* 1 if -msoft-float, 0 if -mhard-float. The default is 0. */
260static int file_mips_soft_float = 0;
261
262/* 1 if -msingle-float, 0 if -mdouble-float. The default is 0. */
263static int file_mips_single_float = 0;
252b5132 264
e972090a
NC
265static struct mips_set_options mips_opts =
266{
037b32b9
AN
267 /* isa */ ISA_UNKNOWN, /* ase_mips3d */ -1, /* ase_mdmx */ -1,
268 /* ase_smartmips */ 0, /* ase_dsp */ -1, /* ase_dspr2 */ -1, /* ase_mt */ -1,
269 /* mips16 */ -1, /* noreorder */ 0, /* at */ ATREG,
270 /* warn_about_macros */ 0, /* nomove */ 0, /* nobopt */ 0,
271 /* noautoextend */ 0, /* gp32 */ 0, /* fp32 */ 0, /* arch */ CPU_UNKNOWN,
272 /* sym32 */ FALSE, /* soft_float */ FALSE, /* single_float */ FALSE
e7af610e 273};
252b5132
RH
274
275/* These variables are filled in with the masks of registers used.
276 The object format code reads them and puts them in the appropriate
277 place. */
278unsigned long mips_gprmask;
279unsigned long mips_cprmask[4];
280
281/* MIPS ISA we are using for this output file. */
e7af610e 282static int file_mips_isa = ISA_UNKNOWN;
252b5132 283
a4672219
TS
284/* True if -mips16 was passed or implied by arguments passed on the
285 command line (e.g., by -march). */
286static int file_ase_mips16;
287
3994f87e
TS
288#define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32 \
289 || mips_opts.isa == ISA_MIPS32R2 \
290 || mips_opts.isa == ISA_MIPS64 \
291 || mips_opts.isa == ISA_MIPS64R2)
292
1f25f5d3
CD
293/* True if -mips3d was passed or implied by arguments passed on the
294 command line (e.g., by -march). */
295static int file_ase_mips3d;
296
deec1734
CD
297/* True if -mdmx was passed or implied by arguments passed on the
298 command line (e.g., by -march). */
299static int file_ase_mdmx;
300
e16bfa71
TS
301/* True if -msmartmips was passed or implied by arguments passed on the
302 command line (e.g., by -march). */
303static int file_ase_smartmips;
304
ad3fea08
TS
305#define ISA_SUPPORTS_SMARTMIPS (mips_opts.isa == ISA_MIPS32 \
306 || mips_opts.isa == ISA_MIPS32R2)
e16bfa71 307
74cd071d
CF
308/* True if -mdsp was passed or implied by arguments passed on the
309 command line (e.g., by -march). */
310static int file_ase_dsp;
311
ad3fea08
TS
312#define ISA_SUPPORTS_DSP_ASE (mips_opts.isa == ISA_MIPS32R2 \
313 || mips_opts.isa == ISA_MIPS64R2)
314
65263ce3
TS
315#define ISA_SUPPORTS_DSP64_ASE (mips_opts.isa == ISA_MIPS64R2)
316
8b082fb1
TS
317/* True if -mdspr2 was passed or implied by arguments passed on the
318 command line (e.g., by -march). */
319static int file_ase_dspr2;
320
321#define ISA_SUPPORTS_DSPR2_ASE (mips_opts.isa == ISA_MIPS32R2 \
322 || mips_opts.isa == ISA_MIPS64R2)
323
ef2e4d86
CF
324/* True if -mmt was passed or implied by arguments passed on the
325 command line (e.g., by -march). */
326static int file_ase_mt;
327
ad3fea08
TS
328#define ISA_SUPPORTS_MT_ASE (mips_opts.isa == ISA_MIPS32R2 \
329 || mips_opts.isa == ISA_MIPS64R2)
330
ec68c924 331/* The argument of the -march= flag. The architecture we are assembling. */
fef14a42 332static int file_mips_arch = CPU_UNKNOWN;
316f5878 333static const char *mips_arch_string;
ec68c924
EC
334
335/* The argument of the -mtune= flag. The architecture for which we
336 are optimizing. */
337static int mips_tune = CPU_UNKNOWN;
316f5878 338static const char *mips_tune_string;
ec68c924 339
316f5878 340/* True when generating 32-bit code for a 64-bit processor. */
252b5132
RH
341static int mips_32bitmode = 0;
342
316f5878
RS
343/* True if the given ABI requires 32-bit registers. */
344#define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
345
346/* Likewise 64-bit registers. */
707bfff6
TS
347#define ABI_NEEDS_64BIT_REGS(ABI) \
348 ((ABI) == N32_ABI \
349 || (ABI) == N64_ABI \
316f5878
RS
350 || (ABI) == O64_ABI)
351
ad3fea08 352/* Return true if ISA supports 64 bit wide gp registers. */
707bfff6
TS
353#define ISA_HAS_64BIT_REGS(ISA) \
354 ((ISA) == ISA_MIPS3 \
355 || (ISA) == ISA_MIPS4 \
356 || (ISA) == ISA_MIPS5 \
357 || (ISA) == ISA_MIPS64 \
358 || (ISA) == ISA_MIPS64R2)
9ce8a5dd 359
ad3fea08
TS
360/* Return true if ISA supports 64 bit wide float registers. */
361#define ISA_HAS_64BIT_FPRS(ISA) \
362 ((ISA) == ISA_MIPS3 \
363 || (ISA) == ISA_MIPS4 \
364 || (ISA) == ISA_MIPS5 \
365 || (ISA) == ISA_MIPS32R2 \
366 || (ISA) == ISA_MIPS64 \
367 || (ISA) == ISA_MIPS64R2)
368
af7ee8bf
CD
369/* Return true if ISA supports 64-bit right rotate (dror et al.)
370 instructions. */
707bfff6
TS
371#define ISA_HAS_DROR(ISA) \
372 ((ISA) == ISA_MIPS64R2)
af7ee8bf
CD
373
374/* Return true if ISA supports 32-bit right rotate (ror et al.)
375 instructions. */
707bfff6
TS
376#define ISA_HAS_ROR(ISA) \
377 ((ISA) == ISA_MIPS32R2 \
378 || (ISA) == ISA_MIPS64R2 \
379 || mips_opts.ase_smartmips)
380
7455baf8
TS
381/* Return true if ISA supports single-precision floats in odd registers. */
382#define ISA_HAS_ODD_SINGLE_FPR(ISA) \
383 ((ISA) == ISA_MIPS32 \
384 || (ISA) == ISA_MIPS32R2 \
385 || (ISA) == ISA_MIPS64 \
386 || (ISA) == ISA_MIPS64R2)
af7ee8bf 387
ad3fea08
TS
388/* Return true if ISA supports move to/from high part of a 64-bit
389 floating-point register. */
390#define ISA_HAS_MXHC1(ISA) \
391 ((ISA) == ISA_MIPS32R2 \
392 || (ISA) == ISA_MIPS64R2)
393
e013f690 394#define HAVE_32BIT_GPRS \
ad3fea08 395 (mips_opts.gp32 || !ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257 396
e013f690 397#define HAVE_32BIT_FPRS \
ad3fea08 398 (mips_opts.fp32 || !ISA_HAS_64BIT_FPRS (mips_opts.isa))
ca4e0257 399
ad3fea08
TS
400#define HAVE_64BIT_GPRS (!HAVE_32BIT_GPRS)
401#define HAVE_64BIT_FPRS (!HAVE_32BIT_FPRS)
ca4e0257 402
316f5878 403#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
e013f690 404
316f5878 405#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
e013f690 406
3b91255e
RS
407/* True if relocations are stored in-place. */
408#define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
409
aed1a261
RS
410/* The ABI-derived address size. */
411#define HAVE_64BIT_ADDRESSES \
412 (HAVE_64BIT_GPRS && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
413#define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
e013f690 414
aed1a261
RS
415/* The size of symbolic constants (i.e., expressions of the form
416 "SYMBOL" or "SYMBOL + OFFSET"). */
417#define HAVE_32BIT_SYMBOLS \
418 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
419#define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
ca4e0257 420
b7c7d6c1
TS
421/* Addresses are loaded in different ways, depending on the address size
422 in use. The n32 ABI Documentation also mandates the use of additions
423 with overflow checking, but existing implementations don't follow it. */
f899b4b8 424#define ADDRESS_ADD_INSN \
b7c7d6c1 425 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
f899b4b8
TS
426
427#define ADDRESS_ADDI_INSN \
b7c7d6c1 428 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
f899b4b8
TS
429
430#define ADDRESS_LOAD_INSN \
431 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
432
433#define ADDRESS_STORE_INSN \
434 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
435
a4672219 436/* Return true if the given CPU supports the MIPS16 ASE. */
3396de36
TS
437#define CPU_HAS_MIPS16(cpu) \
438 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
439 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
a4672219 440
60b63b72
RS
441/* True if CPU has a dror instruction. */
442#define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
443
444/* True if CPU has a ror instruction. */
445#define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
446
dd3cbb7e
NC
447/* True if CPU has seq/sne and seqi/snei instructions. */
448#define CPU_HAS_SEQ(CPU) ((CPU) == CPU_OCTEON)
449
b19e8a9b
AN
450/* True if CPU does not implement the all the coprocessor insns. For these
451 CPUs only those COP insns are accepted that are explicitly marked to be
452 available on the CPU. ISA membership for COP insns is ignored. */
453#define NO_ISA_COP(CPU) ((CPU) == CPU_OCTEON)
454
c8978940
CD
455/* True if mflo and mfhi can be immediately followed by instructions
456 which write to the HI and LO registers.
457
458 According to MIPS specifications, MIPS ISAs I, II, and III need
459 (at least) two instructions between the reads of HI/LO and
460 instructions which write them, and later ISAs do not. Contradicting
461 the MIPS specifications, some MIPS IV processor user manuals (e.g.
462 the UM for the NEC Vr5000) document needing the instructions between
463 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
464 MIPS64 and later ISAs to have the interlocks, plus any specific
465 earlier-ISA CPUs for which CPU documentation declares that the
466 instructions are really interlocked. */
467#define hilo_interlocks \
468 (mips_opts.isa == ISA_MIPS32 \
469 || mips_opts.isa == ISA_MIPS32R2 \
470 || mips_opts.isa == ISA_MIPS64 \
471 || mips_opts.isa == ISA_MIPS64R2 \
472 || mips_opts.arch == CPU_R4010 \
473 || mips_opts.arch == CPU_R10000 \
474 || mips_opts.arch == CPU_R12000 \
475 || mips_opts.arch == CPU_RM7000 \
c8978940
CD
476 || mips_opts.arch == CPU_VR5500 \
477 )
252b5132
RH
478
479/* Whether the processor uses hardware interlocks to protect reads
81912461
ILT
480 from the GPRs after they are loaded from memory, and thus does not
481 require nops to be inserted. This applies to instructions marked
482 INSN_LOAD_MEMORY_DELAY. These nops are only required at MIPS ISA
483 level I. */
252b5132 484#define gpr_interlocks \
e7af610e 485 (mips_opts.isa != ISA_MIPS1 \
fef14a42 486 || mips_opts.arch == CPU_R3900)
252b5132 487
81912461
ILT
488/* Whether the processor uses hardware interlocks to avoid delays
489 required by coprocessor instructions, and thus does not require
490 nops to be inserted. This applies to instructions marked
491 INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
492 between instructions marked INSN_WRITE_COND_CODE and ones marked
493 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
494 levels I, II, and III. */
bdaaa2e1 495/* Itbl support may require additional care here. */
81912461
ILT
496#define cop_interlocks \
497 ((mips_opts.isa != ISA_MIPS1 \
498 && mips_opts.isa != ISA_MIPS2 \
499 && mips_opts.isa != ISA_MIPS3) \
500 || mips_opts.arch == CPU_R4300 \
81912461
ILT
501 )
502
503/* Whether the processor uses hardware interlocks to protect reads
504 from coprocessor registers after they are loaded from memory, and
505 thus does not require nops to be inserted. This applies to
506 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
507 requires at MIPS ISA level I. */
508#define cop_mem_interlocks (mips_opts.isa != ISA_MIPS1)
252b5132 509
6b76fefe
CM
510/* Is this a mfhi or mflo instruction? */
511#define MF_HILO_INSN(PINFO) \
b19e8a9b
AN
512 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
513
514/* Returns true for a (non floating-point) coprocessor instruction. Reading
515 or writing the condition code is only possible on the coprocessors and
516 these insns are not marked with INSN_COP. Thus for these insns use the
517 condition-code flags unless this is the floating-point coprocessor. */
518#define COP_INSN(PINFO) \
519 (PINFO != INSN_MACRO \
520 && (((PINFO) & INSN_COP) \
521 || ((PINFO) & (INSN_READ_COND_CODE | INSN_WRITE_COND_CODE) \
522 && ((PINFO) & (FP_S | FP_D)) == 0)))
6b76fefe 523
252b5132
RH
524/* MIPS PIC level. */
525
a161fe53 526enum mips_pic_level mips_pic;
252b5132 527
c9914766 528/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 529 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 530static int mips_big_got = 0;
252b5132
RH
531
532/* 1 if trap instructions should used for overflow rather than break
533 instructions. */
c9914766 534static int mips_trap = 0;
252b5132 535
119d663a 536/* 1 if double width floating point constants should not be constructed
b6ff326e 537 by assembling two single width halves into two single width floating
119d663a
NC
538 point registers which just happen to alias the double width destination
539 register. On some architectures this aliasing can be disabled by a bit
d547a75e 540 in the status register, and the setting of this bit cannot be determined
119d663a
NC
541 automatically at assemble time. */
542static int mips_disable_float_construction;
543
252b5132
RH
544/* Non-zero if any .set noreorder directives were used. */
545
546static int mips_any_noreorder;
547
6b76fefe
CM
548/* Non-zero if nops should be inserted when the register referenced in
549 an mfhi/mflo instruction is read in the next two instructions. */
550static int mips_7000_hilo_fix;
551
02ffd3e4 552/* The size of objects in the small data section. */
156c2f8b 553static unsigned int g_switch_value = 8;
252b5132
RH
554/* Whether the -G option was used. */
555static int g_switch_seen = 0;
556
557#define N_RMASK 0xc4
558#define N_VFP 0xd4
559
560/* If we can determine in advance that GP optimization won't be
561 possible, we can skip the relaxation stuff that tries to produce
562 GP-relative references. This makes delay slot optimization work
563 better.
564
565 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
566 gcc output. It needs to guess right for gcc, otherwise gcc
567 will put what it thinks is a GP-relative instruction in a branch
568 delay slot.
252b5132
RH
569
570 I don't know if a fix is needed for the SVR4_PIC mode. I've only
571 fixed it for the non-PIC mode. KR 95/04/07 */
17a2f251 572static int nopic_need_relax (symbolS *, int);
252b5132
RH
573
574/* handle of the OPCODE hash table */
575static struct hash_control *op_hash = NULL;
576
577/* The opcode hash table we use for the mips16. */
578static struct hash_control *mips16_op_hash = NULL;
579
580/* This array holds the chars that always start a comment. If the
581 pre-processor is disabled, these aren't very useful */
582const char comment_chars[] = "#";
583
584/* This array holds the chars that only start a comment at the beginning of
585 a line. If the line seems to have the form '# 123 filename'
586 .line and .file directives will appear in the pre-processed output */
587/* Note that input_file.c hand checks for '#' at the beginning of the
588 first line of the input file. This is because the compiler outputs
bdaaa2e1 589 #NO_APP at the beginning of its output. */
252b5132
RH
590/* Also note that C style comments are always supported. */
591const char line_comment_chars[] = "#";
592
bdaaa2e1 593/* This array holds machine specific line separator characters. */
63a0b638 594const char line_separator_chars[] = ";";
252b5132
RH
595
596/* Chars that can be used to separate mant from exp in floating point nums */
597const char EXP_CHARS[] = "eE";
598
599/* Chars that mean this number is a floating point constant */
600/* As in 0f12.456 */
601/* or 0d1.2345e12 */
602const char FLT_CHARS[] = "rRsSfFdDxXpP";
603
604/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
605 changed in read.c . Ideally it shouldn't have to know about it at all,
606 but nothing is ideal around here.
607 */
608
609static char *insn_error;
610
611static int auto_align = 1;
612
613/* When outputting SVR4 PIC code, the assembler needs to know the
614 offset in the stack frame from which to restore the $gp register.
615 This is set by the .cprestore pseudo-op, and saved in this
616 variable. */
617static offsetT mips_cprestore_offset = -1;
618
67c1ffbe 619/* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
6478892d 620 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 621 offset and even an other register than $gp as global pointer. */
6478892d
TS
622static offsetT mips_cpreturn_offset = -1;
623static int mips_cpreturn_register = -1;
624static int mips_gp_register = GP;
def2e0dd 625static int mips_gprel_offset = 0;
6478892d 626
7a621144
DJ
627/* Whether mips_cprestore_offset has been set in the current function
628 (or whether it has already been warned about, if not). */
629static int mips_cprestore_valid = 0;
630
252b5132
RH
631/* This is the register which holds the stack frame, as set by the
632 .frame pseudo-op. This is needed to implement .cprestore. */
633static int mips_frame_reg = SP;
634
7a621144
DJ
635/* Whether mips_frame_reg has been set in the current function
636 (or whether it has already been warned about, if not). */
637static int mips_frame_reg_valid = 0;
638
252b5132
RH
639/* To output NOP instructions correctly, we need to keep information
640 about the previous two instructions. */
641
642/* Whether we are optimizing. The default value of 2 means to remove
643 unneeded NOPs and swap branch instructions when possible. A value
644 of 1 means to not swap branches. A value of 0 means to always
645 insert NOPs. */
646static int mips_optimize = 2;
647
648/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
649 equivalent to seeing no -g option at all. */
650static int mips_debug = 0;
651
7d8e00cf
RS
652/* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
653#define MAX_VR4130_NOPS 4
654
655/* The maximum number of NOPs needed to fill delay slots. */
656#define MAX_DELAY_NOPS 2
657
658/* The maximum number of NOPs needed for any purpose. */
659#define MAX_NOPS 4
71400594
RS
660
661/* A list of previous instructions, with index 0 being the most recent.
662 We need to look back MAX_NOPS instructions when filling delay slots
663 or working around processor errata. We need to look back one
664 instruction further if we're thinking about using history[0] to
665 fill a branch delay slot. */
666static struct mips_cl_insn history[1 + MAX_NOPS];
252b5132 667
1e915849
RS
668/* Nop instructions used by emit_nop. */
669static struct mips_cl_insn nop_insn, mips16_nop_insn;
670
671/* The appropriate nop for the current mode. */
672#define NOP_INSN (mips_opts.mips16 ? &mips16_nop_insn : &nop_insn)
252b5132 673
252b5132
RH
674/* If this is set, it points to a frag holding nop instructions which
675 were inserted before the start of a noreorder section. If those
676 nops turn out to be unnecessary, the size of the frag can be
677 decreased. */
678static fragS *prev_nop_frag;
679
680/* The number of nop instructions we created in prev_nop_frag. */
681static int prev_nop_frag_holds;
682
683/* The number of nop instructions that we know we need in
bdaaa2e1 684 prev_nop_frag. */
252b5132
RH
685static int prev_nop_frag_required;
686
687/* The number of instructions we've seen since prev_nop_frag. */
688static int prev_nop_frag_since;
689
690/* For ECOFF and ELF, relocations against symbols are done in two
691 parts, with a HI relocation and a LO relocation. Each relocation
692 has only 16 bits of space to store an addend. This means that in
693 order for the linker to handle carries correctly, it must be able
694 to locate both the HI and the LO relocation. This means that the
695 relocations must appear in order in the relocation table.
696
697 In order to implement this, we keep track of each unmatched HI
698 relocation. We then sort them so that they immediately precede the
bdaaa2e1 699 corresponding LO relocation. */
252b5132 700
e972090a
NC
701struct mips_hi_fixup
702{
252b5132
RH
703 /* Next HI fixup. */
704 struct mips_hi_fixup *next;
705 /* This fixup. */
706 fixS *fixp;
707 /* The section this fixup is in. */
708 segT seg;
709};
710
711/* The list of unmatched HI relocs. */
712
713static struct mips_hi_fixup *mips_hi_fixup_list;
714
64bdfcaf
RS
715/* The frag containing the last explicit relocation operator.
716 Null if explicit relocations have not been used. */
717
718static fragS *prev_reloc_op_frag;
719
252b5132
RH
720/* Map normal MIPS register numbers to mips16 register numbers. */
721
722#define X ILLEGAL_REG
e972090a
NC
723static const int mips32_to_16_reg_map[] =
724{
252b5132
RH
725 X, X, 2, 3, 4, 5, 6, 7,
726 X, X, X, X, X, X, X, X,
727 0, 1, X, X, X, X, X, X,
728 X, X, X, X, X, X, X, X
729};
730#undef X
731
732/* Map mips16 register numbers to normal MIPS register numbers. */
733
e972090a
NC
734static const unsigned int mips16_to_32_reg_map[] =
735{
252b5132
RH
736 16, 17, 2, 3, 4, 5, 6, 7
737};
60b63b72 738
71400594
RS
739/* Classifies the kind of instructions we're interested in when
740 implementing -mfix-vr4120. */
741enum fix_vr4120_class {
742 FIX_VR4120_MACC,
743 FIX_VR4120_DMACC,
744 FIX_VR4120_MULT,
745 FIX_VR4120_DMULT,
746 FIX_VR4120_DIV,
747 FIX_VR4120_MTHILO,
748 NUM_FIX_VR4120_CLASSES
749};
750
751/* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
752 there must be at least one other instruction between an instruction
753 of type X and an instruction of type Y. */
754static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
755
756/* True if -mfix-vr4120 is in force. */
d766e8ec 757static int mips_fix_vr4120;
4a6a3df4 758
7d8e00cf
RS
759/* ...likewise -mfix-vr4130. */
760static int mips_fix_vr4130;
761
4a6a3df4
AO
762/* We don't relax branches by default, since this causes us to expand
763 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
764 fail to compute the offset before expanding the macro to the most
765 efficient expansion. */
766
767static int mips_relax_branch;
252b5132 768\f
4d7206a2
RS
769/* The expansion of many macros depends on the type of symbol that
770 they refer to. For example, when generating position-dependent code,
771 a macro that refers to a symbol may have two different expansions,
772 one which uses GP-relative addresses and one which uses absolute
773 addresses. When generating SVR4-style PIC, a macro may have
774 different expansions for local and global symbols.
775
776 We handle these situations by generating both sequences and putting
777 them in variant frags. In position-dependent code, the first sequence
778 will be the GP-relative one and the second sequence will be the
779 absolute one. In SVR4 PIC, the first sequence will be for global
780 symbols and the second will be for local symbols.
781
584892a6
RS
782 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
783 SECOND are the lengths of the two sequences in bytes. These fields
784 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
785 the subtype has the following flags:
4d7206a2 786
584892a6
RS
787 RELAX_USE_SECOND
788 Set if it has been decided that we should use the second
789 sequence instead of the first.
790
791 RELAX_SECOND_LONGER
792 Set in the first variant frag if the macro's second implementation
793 is longer than its first. This refers to the macro as a whole,
794 not an individual relaxation.
795
796 RELAX_NOMACRO
797 Set in the first variant frag if the macro appeared in a .set nomacro
798 block and if one alternative requires a warning but the other does not.
799
800 RELAX_DELAY_SLOT
801 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
802 delay slot.
4d7206a2
RS
803
804 The frag's "opcode" points to the first fixup for relaxable code.
805
806 Relaxable macros are generated using a sequence such as:
807
808 relax_start (SYMBOL);
809 ... generate first expansion ...
810 relax_switch ();
811 ... generate second expansion ...
812 relax_end ();
813
814 The code and fixups for the unwanted alternative are discarded
815 by md_convert_frag. */
584892a6 816#define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
4d7206a2 817
584892a6
RS
818#define RELAX_FIRST(X) (((X) >> 8) & 0xff)
819#define RELAX_SECOND(X) ((X) & 0xff)
820#define RELAX_USE_SECOND 0x10000
821#define RELAX_SECOND_LONGER 0x20000
822#define RELAX_NOMACRO 0x40000
823#define RELAX_DELAY_SLOT 0x80000
252b5132 824
4a6a3df4
AO
825/* Branch without likely bit. If label is out of range, we turn:
826
827 beq reg1, reg2, label
828 delay slot
829
830 into
831
832 bne reg1, reg2, 0f
833 nop
834 j label
835 0: delay slot
836
837 with the following opcode replacements:
838
839 beq <-> bne
840 blez <-> bgtz
841 bltz <-> bgez
842 bc1f <-> bc1t
843
844 bltzal <-> bgezal (with jal label instead of j label)
845
846 Even though keeping the delay slot instruction in the delay slot of
847 the branch would be more efficient, it would be very tricky to do
848 correctly, because we'd have to introduce a variable frag *after*
849 the delay slot instruction, and expand that instead. Let's do it
850 the easy way for now, even if the branch-not-taken case now costs
851 one additional instruction. Out-of-range branches are not supposed
852 to be common, anyway.
853
854 Branch likely. If label is out of range, we turn:
855
856 beql reg1, reg2, label
857 delay slot (annulled if branch not taken)
858
859 into
860
861 beql reg1, reg2, 1f
862 nop
863 beql $0, $0, 2f
864 nop
865 1: j[al] label
866 delay slot (executed only if branch taken)
867 2:
868
869 It would be possible to generate a shorter sequence by losing the
870 likely bit, generating something like:
b34976b6 871
4a6a3df4
AO
872 bne reg1, reg2, 0f
873 nop
874 j[al] label
875 delay slot (executed only if branch taken)
876 0:
877
878 beql -> bne
879 bnel -> beq
880 blezl -> bgtz
881 bgtzl -> blez
882 bltzl -> bgez
883 bgezl -> bltz
884 bc1fl -> bc1t
885 bc1tl -> bc1f
886
887 bltzall -> bgezal (with jal label instead of j label)
888 bgezall -> bltzal (ditto)
889
890
891 but it's not clear that it would actually improve performance. */
af6ae2ad 892#define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
4a6a3df4
AO
893 ((relax_substateT) \
894 (0xc0000000 \
895 | ((toofar) ? 1 : 0) \
896 | ((link) ? 2 : 0) \
897 | ((likely) ? 4 : 0) \
af6ae2ad 898 | ((uncond) ? 8 : 0)))
4a6a3df4 899#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
4a6a3df4
AO
900#define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
901#define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
902#define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
ae6063d4 903#define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
4a6a3df4 904
252b5132
RH
905/* For mips16 code, we use an entirely different form of relaxation.
906 mips16 supports two versions of most instructions which take
907 immediate values: a small one which takes some small value, and a
908 larger one which takes a 16 bit value. Since branches also follow
909 this pattern, relaxing these values is required.
910
911 We can assemble both mips16 and normal MIPS code in a single
912 object. Therefore, we need to support this type of relaxation at
913 the same time that we support the relaxation described above. We
914 use the high bit of the subtype field to distinguish these cases.
915
916 The information we store for this type of relaxation is the
917 argument code found in the opcode file for this relocation, whether
918 the user explicitly requested a small or extended form, and whether
919 the relocation is in a jump or jal delay slot. That tells us the
920 size of the value, and how it should be stored. We also store
921 whether the fragment is considered to be extended or not. We also
922 store whether this is known to be a branch to a different section,
923 whether we have tried to relax this frag yet, and whether we have
924 ever extended a PC relative fragment because of a shift count. */
925#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
926 (0x80000000 \
927 | ((type) & 0xff) \
928 | ((small) ? 0x100 : 0) \
929 | ((ext) ? 0x200 : 0) \
930 | ((dslot) ? 0x400 : 0) \
931 | ((jal_dslot) ? 0x800 : 0))
4a6a3df4 932#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132
RH
933#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
934#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
935#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
936#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
937#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
938#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
939#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
940#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
941#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
942#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
943#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
885add95
CD
944
945/* Is the given value a sign-extended 32-bit value? */
946#define IS_SEXT_32BIT_NUM(x) \
947 (((x) &~ (offsetT) 0x7fffffff) == 0 \
948 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
949
950/* Is the given value a sign-extended 16-bit value? */
951#define IS_SEXT_16BIT_NUM(x) \
952 (((x) &~ (offsetT) 0x7fff) == 0 \
953 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
954
2051e8c4
MR
955/* Is the given value a zero-extended 32-bit value? Or a negated one? */
956#define IS_ZEXT_32BIT_NUM(x) \
957 (((x) &~ (offsetT) 0xffffffff) == 0 \
958 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
959
bf12938e
RS
960/* Replace bits MASK << SHIFT of STRUCT with the equivalent bits in
961 VALUE << SHIFT. VALUE is evaluated exactly once. */
962#define INSERT_BITS(STRUCT, VALUE, MASK, SHIFT) \
963 (STRUCT) = (((STRUCT) & ~((MASK) << (SHIFT))) \
964 | (((VALUE) & (MASK)) << (SHIFT)))
965
966/* Extract bits MASK << SHIFT from STRUCT and shift them right
967 SHIFT places. */
968#define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
969 (((STRUCT) >> (SHIFT)) & (MASK))
970
971/* Change INSN's opcode so that the operand given by FIELD has value VALUE.
972 INSN is a mips_cl_insn structure and VALUE is evaluated exactly once.
973
974 include/opcode/mips.h specifies operand fields using the macros
975 OP_MASK_<FIELD> and OP_SH_<FIELD>. The MIPS16 equivalents start
976 with "MIPS16OP" instead of "OP". */
977#define INSERT_OPERAND(FIELD, INSN, VALUE) \
978 INSERT_BITS ((INSN).insn_opcode, VALUE, OP_MASK_##FIELD, OP_SH_##FIELD)
979#define MIPS16_INSERT_OPERAND(FIELD, INSN, VALUE) \
980 INSERT_BITS ((INSN).insn_opcode, VALUE, \
981 MIPS16OP_MASK_##FIELD, MIPS16OP_SH_##FIELD)
982
983/* Extract the operand given by FIELD from mips_cl_insn INSN. */
984#define EXTRACT_OPERAND(FIELD, INSN) \
985 EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD)
986#define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
987 EXTRACT_BITS ((INSN).insn_opcode, \
988 MIPS16OP_MASK_##FIELD, \
989 MIPS16OP_SH_##FIELD)
4d7206a2
RS
990\f
991/* Global variables used when generating relaxable macros. See the
992 comment above RELAX_ENCODE for more details about how relaxation
993 is used. */
994static struct {
995 /* 0 if we're not emitting a relaxable macro.
996 1 if we're emitting the first of the two relaxation alternatives.
997 2 if we're emitting the second alternative. */
998 int sequence;
999
1000 /* The first relaxable fixup in the current frag. (In other words,
1001 the first fixup that refers to relaxable code.) */
1002 fixS *first_fixup;
1003
1004 /* sizes[0] says how many bytes of the first alternative are stored in
1005 the current frag. Likewise sizes[1] for the second alternative. */
1006 unsigned int sizes[2];
1007
1008 /* The symbol on which the choice of sequence depends. */
1009 symbolS *symbol;
1010} mips_relax;
252b5132 1011\f
584892a6
RS
1012/* Global variables used to decide whether a macro needs a warning. */
1013static struct {
1014 /* True if the macro is in a branch delay slot. */
1015 bfd_boolean delay_slot_p;
1016
1017 /* For relaxable macros, sizes[0] is the length of the first alternative
1018 in bytes and sizes[1] is the length of the second alternative.
1019 For non-relaxable macros, both elements give the length of the
1020 macro in bytes. */
1021 unsigned int sizes[2];
1022
1023 /* The first variant frag for this macro. */
1024 fragS *first_frag;
1025} mips_macro_warning;
1026\f
252b5132
RH
1027/* Prototypes for static functions. */
1028
17a2f251 1029#define internalError() \
252b5132 1030 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
252b5132
RH
1031
1032enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1033
b34976b6 1034static void append_insn
4d7206a2 1035 (struct mips_cl_insn *ip, expressionS *p, bfd_reloc_code_real_type *r);
7d10b47d 1036static void mips_no_prev_insn (void);
b34976b6 1037static void mips16_macro_build
67c0d1eb
RS
1038 (expressionS *, const char *, const char *, va_list);
1039static void load_register (int, expressionS *, int);
584892a6
RS
1040static void macro_start (void);
1041static void macro_end (void);
17a2f251
TS
1042static void macro (struct mips_cl_insn * ip);
1043static void mips16_macro (struct mips_cl_insn * ip);
252b5132 1044#ifdef LOSING_COMPILER
17a2f251 1045static void macro2 (struct mips_cl_insn * ip);
252b5132 1046#endif
17a2f251
TS
1047static void mips_ip (char *str, struct mips_cl_insn * ip);
1048static void mips16_ip (char *str, struct mips_cl_insn * ip);
b34976b6 1049static void mips16_immed
17a2f251
TS
1050 (char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean, bfd_boolean,
1051 unsigned long *, bfd_boolean *, unsigned short *);
5e0116d5 1052static size_t my_getSmallExpression
17a2f251
TS
1053 (expressionS *, bfd_reloc_code_real_type *, char *);
1054static void my_getExpression (expressionS *, char *);
1055static void s_align (int);
1056static void s_change_sec (int);
1057static void s_change_section (int);
1058static void s_cons (int);
1059static void s_float_cons (int);
1060static void s_mips_globl (int);
1061static void s_option (int);
1062static void s_mipsset (int);
1063static void s_abicalls (int);
1064static void s_cpload (int);
1065static void s_cpsetup (int);
1066static void s_cplocal (int);
1067static void s_cprestore (int);
1068static void s_cpreturn (int);
741d6ea8
JM
1069static void s_dtprelword (int);
1070static void s_dtpreldword (int);
17a2f251
TS
1071static void s_gpvalue (int);
1072static void s_gpword (int);
1073static void s_gpdword (int);
1074static void s_cpadd (int);
1075static void s_insn (int);
1076static void md_obj_begin (void);
1077static void md_obj_end (void);
1078static void s_mips_ent (int);
1079static void s_mips_end (int);
1080static void s_mips_frame (int);
1081static void s_mips_mask (int reg_type);
1082static void s_mips_stab (int);
1083static void s_mips_weakext (int);
1084static void s_mips_file (int);
1085static void s_mips_loc (int);
1086static bfd_boolean pic_need_relax (symbolS *, asection *);
4a6a3df4 1087static int relaxed_branch_length (fragS *, asection *, int);
17a2f251 1088static int validate_mips_insn (const struct mips_opcode *);
e7af610e
NC
1089
1090/* Table and functions used to map between CPU/ISA names, and
1091 ISA levels, and CPU numbers. */
1092
e972090a
NC
1093struct mips_cpu_info
1094{
e7af610e 1095 const char *name; /* CPU or ISA name. */
ad3fea08 1096 int flags; /* ASEs available, or ISA flag. */
e7af610e
NC
1097 int isa; /* ISA level. */
1098 int cpu; /* CPU number (default CPU if ISA). */
1099};
1100
ad3fea08
TS
1101#define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
1102#define MIPS_CPU_ASE_SMARTMIPS 0x0002 /* CPU implements SmartMIPS ASE */
1103#define MIPS_CPU_ASE_DSP 0x0004 /* CPU implements DSP ASE */
1104#define MIPS_CPU_ASE_MT 0x0008 /* CPU implements MT ASE */
1105#define MIPS_CPU_ASE_MIPS3D 0x0010 /* CPU implements MIPS-3D ASE */
1106#define MIPS_CPU_ASE_MDMX 0x0020 /* CPU implements MDMX ASE */
8b082fb1 1107#define MIPS_CPU_ASE_DSPR2 0x0040 /* CPU implements DSP R2 ASE */
ad3fea08 1108
17a2f251
TS
1109static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1110static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1111static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132
RH
1112\f
1113/* Pseudo-op table.
1114
1115 The following pseudo-ops from the Kane and Heinrich MIPS book
1116 should be defined here, but are currently unsupported: .alias,
1117 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1118
1119 The following pseudo-ops from the Kane and Heinrich MIPS book are
1120 specific to the type of debugging information being generated, and
1121 should be defined by the object format: .aent, .begin, .bend,
1122 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1123 .vreg.
1124
1125 The following pseudo-ops from the Kane and Heinrich MIPS book are
1126 not MIPS CPU specific, but are also not specific to the object file
1127 format. This file is probably the best place to define them, but
d84bcf09 1128 they are not currently supported: .asm0, .endr, .lab, .struct. */
252b5132 1129
e972090a
NC
1130static const pseudo_typeS mips_pseudo_table[] =
1131{
beae10d5 1132 /* MIPS specific pseudo-ops. */
252b5132
RH
1133 {"option", s_option, 0},
1134 {"set", s_mipsset, 0},
1135 {"rdata", s_change_sec, 'r'},
1136 {"sdata", s_change_sec, 's'},
1137 {"livereg", s_ignore, 0},
1138 {"abicalls", s_abicalls, 0},
1139 {"cpload", s_cpload, 0},
6478892d
TS
1140 {"cpsetup", s_cpsetup, 0},
1141 {"cplocal", s_cplocal, 0},
252b5132 1142 {"cprestore", s_cprestore, 0},
6478892d 1143 {"cpreturn", s_cpreturn, 0},
741d6ea8
JM
1144 {"dtprelword", s_dtprelword, 0},
1145 {"dtpreldword", s_dtpreldword, 0},
6478892d 1146 {"gpvalue", s_gpvalue, 0},
252b5132 1147 {"gpword", s_gpword, 0},
10181a0d 1148 {"gpdword", s_gpdword, 0},
252b5132
RH
1149 {"cpadd", s_cpadd, 0},
1150 {"insn", s_insn, 0},
1151
beae10d5 1152 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132 1153 chips. */
38a57ae7 1154 {"asciiz", stringer, 8 + 1},
252b5132
RH
1155 {"bss", s_change_sec, 'b'},
1156 {"err", s_err, 0},
1157 {"half", s_cons, 1},
1158 {"dword", s_cons, 3},
1159 {"weakext", s_mips_weakext, 0},
7c752c2a
TS
1160 {"origin", s_org, 0},
1161 {"repeat", s_rept, 0},
252b5132 1162
beae10d5 1163 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1164 here for one reason or another. */
1165 {"align", s_align, 0},
1166 {"byte", s_cons, 0},
1167 {"data", s_change_sec, 'd'},
1168 {"double", s_float_cons, 'd'},
1169 {"float", s_float_cons, 'f'},
1170 {"globl", s_mips_globl, 0},
1171 {"global", s_mips_globl, 0},
1172 {"hword", s_cons, 1},
1173 {"int", s_cons, 2},
1174 {"long", s_cons, 2},
1175 {"octa", s_cons, 4},
1176 {"quad", s_cons, 3},
cca86cc8 1177 {"section", s_change_section, 0},
252b5132
RH
1178 {"short", s_cons, 1},
1179 {"single", s_float_cons, 'f'},
1180 {"stabn", s_mips_stab, 'n'},
1181 {"text", s_change_sec, 't'},
1182 {"word", s_cons, 2},
add56521 1183
add56521 1184 { "extern", ecoff_directive_extern, 0},
add56521 1185
43841e91 1186 { NULL, NULL, 0 },
252b5132
RH
1187};
1188
e972090a
NC
1189static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1190{
beae10d5
KH
1191 /* These pseudo-ops should be defined by the object file format.
1192 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1193 {"aent", s_mips_ent, 1},
1194 {"bgnb", s_ignore, 0},
1195 {"end", s_mips_end, 0},
1196 {"endb", s_ignore, 0},
1197 {"ent", s_mips_ent, 0},
c5dd6aab 1198 {"file", s_mips_file, 0},
252b5132
RH
1199 {"fmask", s_mips_mask, 'F'},
1200 {"frame", s_mips_frame, 0},
c5dd6aab 1201 {"loc", s_mips_loc, 0},
252b5132
RH
1202 {"mask", s_mips_mask, 'R'},
1203 {"verstamp", s_ignore, 0},
43841e91 1204 { NULL, NULL, 0 },
252b5132
RH
1205};
1206
17a2f251 1207extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1208
1209void
17a2f251 1210mips_pop_insert (void)
252b5132
RH
1211{
1212 pop_insert (mips_pseudo_table);
1213 if (! ECOFF_DEBUGGING)
1214 pop_insert (mips_nonecoff_pseudo_table);
1215}
1216\f
1217/* Symbols labelling the current insn. */
1218
e972090a
NC
1219struct insn_label_list
1220{
252b5132
RH
1221 struct insn_label_list *next;
1222 symbolS *label;
1223};
1224
252b5132 1225static struct insn_label_list *free_insn_labels;
742a56fe 1226#define label_list tc_segment_info_data.labels
252b5132 1227
17a2f251 1228static void mips_clear_insn_labels (void);
252b5132
RH
1229
1230static inline void
17a2f251 1231mips_clear_insn_labels (void)
252b5132
RH
1232{
1233 register struct insn_label_list **pl;
a8dbcb85 1234 segment_info_type *si;
252b5132 1235
a8dbcb85
TS
1236 if (now_seg)
1237 {
1238 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1239 ;
1240
1241 si = seg_info (now_seg);
1242 *pl = si->label_list;
1243 si->label_list = NULL;
1244 }
252b5132 1245}
a8dbcb85 1246
252b5132
RH
1247\f
1248static char *expr_end;
1249
1250/* Expressions which appear in instructions. These are set by
1251 mips_ip. */
1252
1253static expressionS imm_expr;
5f74bc13 1254static expressionS imm2_expr;
252b5132
RH
1255static expressionS offset_expr;
1256
1257/* Relocs associated with imm_expr and offset_expr. */
1258
f6688943
TS
1259static bfd_reloc_code_real_type imm_reloc[3]
1260 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1261static bfd_reloc_code_real_type offset_reloc[3]
1262 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 1263
252b5132
RH
1264/* These are set by mips16_ip if an explicit extension is used. */
1265
b34976b6 1266static bfd_boolean mips16_small, mips16_ext;
252b5132 1267
7ed4a06a 1268#ifdef OBJ_ELF
ecb4347a
DJ
1269/* The pdr segment for per procedure frame/regmask info. Not used for
1270 ECOFF debugging. */
252b5132
RH
1271
1272static segT pdr_seg;
7ed4a06a 1273#endif
252b5132 1274
e013f690
TS
1275/* The default target format to use. */
1276
1277const char *
17a2f251 1278mips_target_format (void)
e013f690
TS
1279{
1280 switch (OUTPUT_FLAVOR)
1281 {
e013f690
TS
1282 case bfd_target_ecoff_flavour:
1283 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1284 case bfd_target_coff_flavour:
1285 return "pe-mips";
1286 case bfd_target_elf_flavour:
0a44bf69
RS
1287#ifdef TE_VXWORKS
1288 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1289 return (target_big_endian
1290 ? "elf32-bigmips-vxworks"
1291 : "elf32-littlemips-vxworks");
1292#endif
e013f690 1293#ifdef TE_TMIPS
cfe86eaa 1294 /* This is traditional mips. */
e013f690 1295 return (target_big_endian
cfe86eaa
TS
1296 ? (HAVE_64BIT_OBJECTS
1297 ? "elf64-tradbigmips"
1298 : (HAVE_NEWABI
1299 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1300 : (HAVE_64BIT_OBJECTS
1301 ? "elf64-tradlittlemips"
1302 : (HAVE_NEWABI
1303 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
e013f690
TS
1304#else
1305 return (target_big_endian
cfe86eaa
TS
1306 ? (HAVE_64BIT_OBJECTS
1307 ? "elf64-bigmips"
1308 : (HAVE_NEWABI
1309 ? "elf32-nbigmips" : "elf32-bigmips"))
1310 : (HAVE_64BIT_OBJECTS
1311 ? "elf64-littlemips"
1312 : (HAVE_NEWABI
1313 ? "elf32-nlittlemips" : "elf32-littlemips")));
e013f690
TS
1314#endif
1315 default:
1316 abort ();
1317 return NULL;
1318 }
1319}
1320
1e915849
RS
1321/* Return the length of instruction INSN. */
1322
1323static inline unsigned int
1324insn_length (const struct mips_cl_insn *insn)
1325{
1326 if (!mips_opts.mips16)
1327 return 4;
1328 return insn->mips16_absolute_jump_p || insn->use_extend ? 4 : 2;
1329}
1330
1331/* Initialise INSN from opcode entry MO. Leave its position unspecified. */
1332
1333static void
1334create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
1335{
1336 size_t i;
1337
1338 insn->insn_mo = mo;
1339 insn->use_extend = FALSE;
1340 insn->extend = 0;
1341 insn->insn_opcode = mo->match;
1342 insn->frag = NULL;
1343 insn->where = 0;
1344 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1345 insn->fixp[i] = NULL;
1346 insn->fixed_p = (mips_opts.noreorder > 0);
1347 insn->noreorder_p = (mips_opts.noreorder > 0);
1348 insn->mips16_absolute_jump_p = 0;
1349}
1350
742a56fe
RS
1351/* Record the current MIPS16 mode in now_seg. */
1352
1353static void
1354mips_record_mips16_mode (void)
1355{
1356 segment_info_type *si;
1357
1358 si = seg_info (now_seg);
1359 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
1360 si->tc_segment_info_data.mips16 = mips_opts.mips16;
1361}
1362
1e915849
RS
1363/* Install INSN at the location specified by its "frag" and "where" fields. */
1364
1365static void
1366install_insn (const struct mips_cl_insn *insn)
1367{
1368 char *f = insn->frag->fr_literal + insn->where;
1369 if (!mips_opts.mips16)
1370 md_number_to_chars (f, insn->insn_opcode, 4);
1371 else if (insn->mips16_absolute_jump_p)
1372 {
1373 md_number_to_chars (f, insn->insn_opcode >> 16, 2);
1374 md_number_to_chars (f + 2, insn->insn_opcode & 0xffff, 2);
1375 }
1376 else
1377 {
1378 if (insn->use_extend)
1379 {
1380 md_number_to_chars (f, 0xf000 | insn->extend, 2);
1381 f += 2;
1382 }
1383 md_number_to_chars (f, insn->insn_opcode, 2);
1384 }
742a56fe 1385 mips_record_mips16_mode ();
1e915849
RS
1386}
1387
1388/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
1389 and install the opcode in the new location. */
1390
1391static void
1392move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
1393{
1394 size_t i;
1395
1396 insn->frag = frag;
1397 insn->where = where;
1398 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1399 if (insn->fixp[i] != NULL)
1400 {
1401 insn->fixp[i]->fx_frag = frag;
1402 insn->fixp[i]->fx_where = where;
1403 }
1404 install_insn (insn);
1405}
1406
1407/* Add INSN to the end of the output. */
1408
1409static void
1410add_fixed_insn (struct mips_cl_insn *insn)
1411{
1412 char *f = frag_more (insn_length (insn));
1413 move_insn (insn, frag_now, f - frag_now->fr_literal);
1414}
1415
1416/* Start a variant frag and move INSN to the start of the variant part,
1417 marking it as fixed. The other arguments are as for frag_var. */
1418
1419static void
1420add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
1421 relax_substateT subtype, symbolS *symbol, offsetT offset)
1422{
1423 frag_grow (max_chars);
1424 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
1425 insn->fixed_p = 1;
1426 frag_var (rs_machine_dependent, max_chars, var,
1427 subtype, symbol, offset, NULL);
1428}
1429
1430/* Insert N copies of INSN into the history buffer, starting at
1431 position FIRST. Neither FIRST nor N need to be clipped. */
1432
1433static void
1434insert_into_history (unsigned int first, unsigned int n,
1435 const struct mips_cl_insn *insn)
1436{
1437 if (mips_relax.sequence != 2)
1438 {
1439 unsigned int i;
1440
1441 for (i = ARRAY_SIZE (history); i-- > first;)
1442 if (i >= first + n)
1443 history[i] = history[i - n];
1444 else
1445 history[i] = *insn;
1446 }
1447}
1448
1449/* Emit a nop instruction, recording it in the history buffer. */
1450
1451static void
1452emit_nop (void)
1453{
1454 add_fixed_insn (NOP_INSN);
1455 insert_into_history (0, 1, NOP_INSN);
1456}
1457
71400594
RS
1458/* Initialize vr4120_conflicts. There is a bit of duplication here:
1459 the idea is to make it obvious at a glance that each errata is
1460 included. */
1461
1462static void
1463init_vr4120_conflicts (void)
1464{
1465#define CONFLICT(FIRST, SECOND) \
1466 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
1467
1468 /* Errata 21 - [D]DIV[U] after [D]MACC */
1469 CONFLICT (MACC, DIV);
1470 CONFLICT (DMACC, DIV);
1471
1472 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
1473 CONFLICT (DMULT, DMULT);
1474 CONFLICT (DMULT, DMACC);
1475 CONFLICT (DMACC, DMULT);
1476 CONFLICT (DMACC, DMACC);
1477
1478 /* Errata 24 - MT{LO,HI} after [D]MACC */
1479 CONFLICT (MACC, MTHILO);
1480 CONFLICT (DMACC, MTHILO);
1481
1482 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
1483 instruction is executed immediately after a MACC or DMACC
1484 instruction, the result of [either instruction] is incorrect." */
1485 CONFLICT (MACC, MULT);
1486 CONFLICT (MACC, DMULT);
1487 CONFLICT (DMACC, MULT);
1488 CONFLICT (DMACC, DMULT);
1489
1490 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
1491 executed immediately after a DMULT, DMULTU, DIV, DIVU,
1492 DDIV or DDIVU instruction, the result of the MACC or
1493 DMACC instruction is incorrect.". */
1494 CONFLICT (DMULT, MACC);
1495 CONFLICT (DMULT, DMACC);
1496 CONFLICT (DIV, MACC);
1497 CONFLICT (DIV, DMACC);
1498
1499#undef CONFLICT
1500}
1501
707bfff6
TS
1502struct regname {
1503 const char *name;
1504 unsigned int num;
1505};
1506
1507#define RTYPE_MASK 0x1ff00
1508#define RTYPE_NUM 0x00100
1509#define RTYPE_FPU 0x00200
1510#define RTYPE_FCC 0x00400
1511#define RTYPE_VEC 0x00800
1512#define RTYPE_GP 0x01000
1513#define RTYPE_CP0 0x02000
1514#define RTYPE_PC 0x04000
1515#define RTYPE_ACC 0x08000
1516#define RTYPE_CCC 0x10000
1517#define RNUM_MASK 0x000ff
1518#define RWARN 0x80000
1519
1520#define GENERIC_REGISTER_NUMBERS \
1521 {"$0", RTYPE_NUM | 0}, \
1522 {"$1", RTYPE_NUM | 1}, \
1523 {"$2", RTYPE_NUM | 2}, \
1524 {"$3", RTYPE_NUM | 3}, \
1525 {"$4", RTYPE_NUM | 4}, \
1526 {"$5", RTYPE_NUM | 5}, \
1527 {"$6", RTYPE_NUM | 6}, \
1528 {"$7", RTYPE_NUM | 7}, \
1529 {"$8", RTYPE_NUM | 8}, \
1530 {"$9", RTYPE_NUM | 9}, \
1531 {"$10", RTYPE_NUM | 10}, \
1532 {"$11", RTYPE_NUM | 11}, \
1533 {"$12", RTYPE_NUM | 12}, \
1534 {"$13", RTYPE_NUM | 13}, \
1535 {"$14", RTYPE_NUM | 14}, \
1536 {"$15", RTYPE_NUM | 15}, \
1537 {"$16", RTYPE_NUM | 16}, \
1538 {"$17", RTYPE_NUM | 17}, \
1539 {"$18", RTYPE_NUM | 18}, \
1540 {"$19", RTYPE_NUM | 19}, \
1541 {"$20", RTYPE_NUM | 20}, \
1542 {"$21", RTYPE_NUM | 21}, \
1543 {"$22", RTYPE_NUM | 22}, \
1544 {"$23", RTYPE_NUM | 23}, \
1545 {"$24", RTYPE_NUM | 24}, \
1546 {"$25", RTYPE_NUM | 25}, \
1547 {"$26", RTYPE_NUM | 26}, \
1548 {"$27", RTYPE_NUM | 27}, \
1549 {"$28", RTYPE_NUM | 28}, \
1550 {"$29", RTYPE_NUM | 29}, \
1551 {"$30", RTYPE_NUM | 30}, \
1552 {"$31", RTYPE_NUM | 31}
1553
1554#define FPU_REGISTER_NAMES \
1555 {"$f0", RTYPE_FPU | 0}, \
1556 {"$f1", RTYPE_FPU | 1}, \
1557 {"$f2", RTYPE_FPU | 2}, \
1558 {"$f3", RTYPE_FPU | 3}, \
1559 {"$f4", RTYPE_FPU | 4}, \
1560 {"$f5", RTYPE_FPU | 5}, \
1561 {"$f6", RTYPE_FPU | 6}, \
1562 {"$f7", RTYPE_FPU | 7}, \
1563 {"$f8", RTYPE_FPU | 8}, \
1564 {"$f9", RTYPE_FPU | 9}, \
1565 {"$f10", RTYPE_FPU | 10}, \
1566 {"$f11", RTYPE_FPU | 11}, \
1567 {"$f12", RTYPE_FPU | 12}, \
1568 {"$f13", RTYPE_FPU | 13}, \
1569 {"$f14", RTYPE_FPU | 14}, \
1570 {"$f15", RTYPE_FPU | 15}, \
1571 {"$f16", RTYPE_FPU | 16}, \
1572 {"$f17", RTYPE_FPU | 17}, \
1573 {"$f18", RTYPE_FPU | 18}, \
1574 {"$f19", RTYPE_FPU | 19}, \
1575 {"$f20", RTYPE_FPU | 20}, \
1576 {"$f21", RTYPE_FPU | 21}, \
1577 {"$f22", RTYPE_FPU | 22}, \
1578 {"$f23", RTYPE_FPU | 23}, \
1579 {"$f24", RTYPE_FPU | 24}, \
1580 {"$f25", RTYPE_FPU | 25}, \
1581 {"$f26", RTYPE_FPU | 26}, \
1582 {"$f27", RTYPE_FPU | 27}, \
1583 {"$f28", RTYPE_FPU | 28}, \
1584 {"$f29", RTYPE_FPU | 29}, \
1585 {"$f30", RTYPE_FPU | 30}, \
1586 {"$f31", RTYPE_FPU | 31}
1587
1588#define FPU_CONDITION_CODE_NAMES \
1589 {"$fcc0", RTYPE_FCC | 0}, \
1590 {"$fcc1", RTYPE_FCC | 1}, \
1591 {"$fcc2", RTYPE_FCC | 2}, \
1592 {"$fcc3", RTYPE_FCC | 3}, \
1593 {"$fcc4", RTYPE_FCC | 4}, \
1594 {"$fcc5", RTYPE_FCC | 5}, \
1595 {"$fcc6", RTYPE_FCC | 6}, \
1596 {"$fcc7", RTYPE_FCC | 7}
1597
1598#define COPROC_CONDITION_CODE_NAMES \
1599 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
1600 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
1601 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
1602 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
1603 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
1604 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
1605 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
1606 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
1607
1608#define N32N64_SYMBOLIC_REGISTER_NAMES \
1609 {"$a4", RTYPE_GP | 8}, \
1610 {"$a5", RTYPE_GP | 9}, \
1611 {"$a6", RTYPE_GP | 10}, \
1612 {"$a7", RTYPE_GP | 11}, \
1613 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
1614 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
1615 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
1616 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
1617 {"$t0", RTYPE_GP | 12}, \
1618 {"$t1", RTYPE_GP | 13}, \
1619 {"$t2", RTYPE_GP | 14}, \
1620 {"$t3", RTYPE_GP | 15}
1621
1622#define O32_SYMBOLIC_REGISTER_NAMES \
1623 {"$t0", RTYPE_GP | 8}, \
1624 {"$t1", RTYPE_GP | 9}, \
1625 {"$t2", RTYPE_GP | 10}, \
1626 {"$t3", RTYPE_GP | 11}, \
1627 {"$t4", RTYPE_GP | 12}, \
1628 {"$t5", RTYPE_GP | 13}, \
1629 {"$t6", RTYPE_GP | 14}, \
1630 {"$t7", RTYPE_GP | 15}, \
1631 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
1632 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
1633 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
1634 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
1635
1636/* Remaining symbolic register names */
1637#define SYMBOLIC_REGISTER_NAMES \
1638 {"$zero", RTYPE_GP | 0}, \
1639 {"$at", RTYPE_GP | 1}, \
1640 {"$AT", RTYPE_GP | 1}, \
1641 {"$v0", RTYPE_GP | 2}, \
1642 {"$v1", RTYPE_GP | 3}, \
1643 {"$a0", RTYPE_GP | 4}, \
1644 {"$a1", RTYPE_GP | 5}, \
1645 {"$a2", RTYPE_GP | 6}, \
1646 {"$a3", RTYPE_GP | 7}, \
1647 {"$s0", RTYPE_GP | 16}, \
1648 {"$s1", RTYPE_GP | 17}, \
1649 {"$s2", RTYPE_GP | 18}, \
1650 {"$s3", RTYPE_GP | 19}, \
1651 {"$s4", RTYPE_GP | 20}, \
1652 {"$s5", RTYPE_GP | 21}, \
1653 {"$s6", RTYPE_GP | 22}, \
1654 {"$s7", RTYPE_GP | 23}, \
1655 {"$t8", RTYPE_GP | 24}, \
1656 {"$t9", RTYPE_GP | 25}, \
1657 {"$k0", RTYPE_GP | 26}, \
1658 {"$kt0", RTYPE_GP | 26}, \
1659 {"$k1", RTYPE_GP | 27}, \
1660 {"$kt1", RTYPE_GP | 27}, \
1661 {"$gp", RTYPE_GP | 28}, \
1662 {"$sp", RTYPE_GP | 29}, \
1663 {"$s8", RTYPE_GP | 30}, \
1664 {"$fp", RTYPE_GP | 30}, \
1665 {"$ra", RTYPE_GP | 31}
1666
1667#define MIPS16_SPECIAL_REGISTER_NAMES \
1668 {"$pc", RTYPE_PC | 0}
1669
1670#define MDMX_VECTOR_REGISTER_NAMES \
1671 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
1672 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
1673 {"$v2", RTYPE_VEC | 2}, \
1674 {"$v3", RTYPE_VEC | 3}, \
1675 {"$v4", RTYPE_VEC | 4}, \
1676 {"$v5", RTYPE_VEC | 5}, \
1677 {"$v6", RTYPE_VEC | 6}, \
1678 {"$v7", RTYPE_VEC | 7}, \
1679 {"$v8", RTYPE_VEC | 8}, \
1680 {"$v9", RTYPE_VEC | 9}, \
1681 {"$v10", RTYPE_VEC | 10}, \
1682 {"$v11", RTYPE_VEC | 11}, \
1683 {"$v12", RTYPE_VEC | 12}, \
1684 {"$v13", RTYPE_VEC | 13}, \
1685 {"$v14", RTYPE_VEC | 14}, \
1686 {"$v15", RTYPE_VEC | 15}, \
1687 {"$v16", RTYPE_VEC | 16}, \
1688 {"$v17", RTYPE_VEC | 17}, \
1689 {"$v18", RTYPE_VEC | 18}, \
1690 {"$v19", RTYPE_VEC | 19}, \
1691 {"$v20", RTYPE_VEC | 20}, \
1692 {"$v21", RTYPE_VEC | 21}, \
1693 {"$v22", RTYPE_VEC | 22}, \
1694 {"$v23", RTYPE_VEC | 23}, \
1695 {"$v24", RTYPE_VEC | 24}, \
1696 {"$v25", RTYPE_VEC | 25}, \
1697 {"$v26", RTYPE_VEC | 26}, \
1698 {"$v27", RTYPE_VEC | 27}, \
1699 {"$v28", RTYPE_VEC | 28}, \
1700 {"$v29", RTYPE_VEC | 29}, \
1701 {"$v30", RTYPE_VEC | 30}, \
1702 {"$v31", RTYPE_VEC | 31}
1703
1704#define MIPS_DSP_ACCUMULATOR_NAMES \
1705 {"$ac0", RTYPE_ACC | 0}, \
1706 {"$ac1", RTYPE_ACC | 1}, \
1707 {"$ac2", RTYPE_ACC | 2}, \
1708 {"$ac3", RTYPE_ACC | 3}
1709
1710static const struct regname reg_names[] = {
1711 GENERIC_REGISTER_NUMBERS,
1712 FPU_REGISTER_NAMES,
1713 FPU_CONDITION_CODE_NAMES,
1714 COPROC_CONDITION_CODE_NAMES,
1715
1716 /* The $txx registers depends on the abi,
1717 these will be added later into the symbol table from
1718 one of the tables below once mips_abi is set after
1719 parsing of arguments from the command line. */
1720 SYMBOLIC_REGISTER_NAMES,
1721
1722 MIPS16_SPECIAL_REGISTER_NAMES,
1723 MDMX_VECTOR_REGISTER_NAMES,
1724 MIPS_DSP_ACCUMULATOR_NAMES,
1725 {0, 0}
1726};
1727
1728static const struct regname reg_names_o32[] = {
1729 O32_SYMBOLIC_REGISTER_NAMES,
1730 {0, 0}
1731};
1732
1733static const struct regname reg_names_n32n64[] = {
1734 N32N64_SYMBOLIC_REGISTER_NAMES,
1735 {0, 0}
1736};
1737
1738static int
1739reg_lookup (char **s, unsigned int types, unsigned int *regnop)
1740{
1741 symbolS *symbolP;
1742 char *e;
1743 char save_c;
1744 int reg = -1;
1745
1746 /* Find end of name. */
1747 e = *s;
1748 if (is_name_beginner (*e))
1749 ++e;
1750 while (is_part_of_name (*e))
1751 ++e;
1752
1753 /* Terminate name. */
1754 save_c = *e;
1755 *e = '\0';
1756
1757 /* Look for a register symbol. */
1758 if ((symbolP = symbol_find (*s)) && S_GET_SEGMENT (symbolP) == reg_section)
1759 {
1760 int r = S_GET_VALUE (symbolP);
1761 if (r & types)
1762 reg = r & RNUM_MASK;
1763 else if ((types & RTYPE_VEC) && (r & ~1) == (RTYPE_GP | 2))
1764 /* Convert GP reg $v0/1 to MDMX reg $v0/1! */
1765 reg = (r & RNUM_MASK) - 2;
1766 }
1767 /* Else see if this is a register defined in an itbl entry. */
1768 else if ((types & RTYPE_GP) && itbl_have_entries)
1769 {
1770 char *n = *s;
1771 unsigned long r;
1772
1773 if (*n == '$')
1774 ++n;
1775 if (itbl_get_reg_val (n, &r))
1776 reg = r & RNUM_MASK;
1777 }
1778
1779 /* Advance to next token if a register was recognised. */
1780 if (reg >= 0)
1781 *s = e;
1782 else if (types & RWARN)
1783 as_warn ("Unrecognized register name `%s'", *s);
1784
1785 *e = save_c;
1786 if (regnop)
1787 *regnop = reg;
1788 return reg >= 0;
1789}
1790
037b32b9
AN
1791/* Return TRUE if opcode MO is valid on the currently selected ISA and
1792 architecture. If EXPANSIONP is TRUE then this check is done while
1793 expanding a macro. Use is_opcode_valid_16 for MIPS16 opcodes. */
1794
1795static bfd_boolean
1796is_opcode_valid (const struct mips_opcode *mo, bfd_boolean expansionp)
1797{
1798 int isa = mips_opts.isa;
1799 int fp_s, fp_d;
1800
1801 if (mips_opts.ase_mdmx)
1802 isa |= INSN_MDMX;
1803 if (mips_opts.ase_dsp)
1804 isa |= INSN_DSP;
1805 if (mips_opts.ase_dsp && ISA_SUPPORTS_DSP64_ASE)
1806 isa |= INSN_DSP64;
1807 if (mips_opts.ase_dspr2)
1808 isa |= INSN_DSPR2;
1809 if (mips_opts.ase_mt)
1810 isa |= INSN_MT;
1811 if (mips_opts.ase_mips3d)
1812 isa |= INSN_MIPS3D;
1813 if (mips_opts.ase_smartmips)
1814 isa |= INSN_SMARTMIPS;
1815
1816 /* For user code we don't check for mips_opts.mips16 since we want
1817 to allow jalx if -mips16 was specified on the command line. */
1818 if (expansionp ? mips_opts.mips16 : file_ase_mips16)
1819 isa |= INSN_MIPS16;
1820
b19e8a9b
AN
1821 /* Don't accept instructions based on the ISA if the CPU does not implement
1822 all the coprocessor insns. */
1823 if (NO_ISA_COP (mips_opts.arch)
1824 && COP_INSN (mo->pinfo))
1825 isa = 0;
1826
037b32b9
AN
1827 if (!OPCODE_IS_MEMBER (mo, isa, mips_opts.arch))
1828 return FALSE;
1829
1830 /* Check whether the instruction or macro requires single-precision or
1831 double-precision floating-point support. Note that this information is
1832 stored differently in the opcode table for insns and macros. */
1833 if (mo->pinfo == INSN_MACRO)
1834 {
1835 fp_s = mo->pinfo2 & INSN2_M_FP_S;
1836 fp_d = mo->pinfo2 & INSN2_M_FP_D;
1837 }
1838 else
1839 {
1840 fp_s = mo->pinfo & FP_S;
1841 fp_d = mo->pinfo & FP_D;
1842 }
1843
1844 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
1845 return FALSE;
1846
1847 if (fp_s && mips_opts.soft_float)
1848 return FALSE;
1849
1850 return TRUE;
1851}
1852
1853/* Return TRUE if the MIPS16 opcode MO is valid on the currently
1854 selected ISA and architecture. */
1855
1856static bfd_boolean
1857is_opcode_valid_16 (const struct mips_opcode *mo)
1858{
1859 return OPCODE_IS_MEMBER (mo, mips_opts.isa, mips_opts.arch) ? TRUE : FALSE;
1860}
1861
707bfff6
TS
1862/* This function is called once, at assembler startup time. It should set up
1863 all the tables, etc. that the MD part of the assembler will need. */
156c2f8b 1864
252b5132 1865void
17a2f251 1866md_begin (void)
252b5132 1867{
3994f87e 1868 const char *retval = NULL;
156c2f8b 1869 int i = 0;
252b5132 1870 int broken = 0;
1f25f5d3 1871
0a44bf69
RS
1872 if (mips_pic != NO_PIC)
1873 {
1874 if (g_switch_seen && g_switch_value != 0)
1875 as_bad (_("-G may not be used in position-independent code"));
1876 g_switch_value = 0;
1877 }
1878
fef14a42 1879 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
252b5132
RH
1880 as_warn (_("Could not set architecture and machine"));
1881
252b5132
RH
1882 op_hash = hash_new ();
1883
1884 for (i = 0; i < NUMOPCODES;)
1885 {
1886 const char *name = mips_opcodes[i].name;
1887
17a2f251 1888 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
1889 if (retval != NULL)
1890 {
1891 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1892 mips_opcodes[i].name, retval);
1893 /* Probably a memory allocation problem? Give up now. */
1894 as_fatal (_("Broken assembler. No assembly attempted."));
1895 }
1896 do
1897 {
1898 if (mips_opcodes[i].pinfo != INSN_MACRO)
1899 {
1900 if (!validate_mips_insn (&mips_opcodes[i]))
1901 broken = 1;
1e915849
RS
1902 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
1903 {
1904 create_insn (&nop_insn, mips_opcodes + i);
1905 nop_insn.fixed_p = 1;
1906 }
252b5132
RH
1907 }
1908 ++i;
1909 }
1910 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1911 }
1912
1913 mips16_op_hash = hash_new ();
1914
1915 i = 0;
1916 while (i < bfd_mips16_num_opcodes)
1917 {
1918 const char *name = mips16_opcodes[i].name;
1919
17a2f251 1920 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
1921 if (retval != NULL)
1922 as_fatal (_("internal: can't hash `%s': %s"),
1923 mips16_opcodes[i].name, retval);
1924 do
1925 {
1926 if (mips16_opcodes[i].pinfo != INSN_MACRO
1927 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1928 != mips16_opcodes[i].match))
1929 {
1930 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1931 mips16_opcodes[i].name, mips16_opcodes[i].args);
1932 broken = 1;
1933 }
1e915849
RS
1934 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
1935 {
1936 create_insn (&mips16_nop_insn, mips16_opcodes + i);
1937 mips16_nop_insn.fixed_p = 1;
1938 }
252b5132
RH
1939 ++i;
1940 }
1941 while (i < bfd_mips16_num_opcodes
1942 && strcmp (mips16_opcodes[i].name, name) == 0);
1943 }
1944
1945 if (broken)
1946 as_fatal (_("Broken assembler. No assembly attempted."));
1947
1948 /* We add all the general register names to the symbol table. This
1949 helps us detect invalid uses of them. */
707bfff6
TS
1950 for (i = 0; reg_names[i].name; i++)
1951 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
1952 reg_names[i].num, // & RNUM_MASK,
1953 &zero_address_frag));
1954 if (HAVE_NEWABI)
1955 for (i = 0; reg_names_n32n64[i].name; i++)
1956 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
1957 reg_names_n32n64[i].num, // & RNUM_MASK,
252b5132 1958 &zero_address_frag));
707bfff6
TS
1959 else
1960 for (i = 0; reg_names_o32[i].name; i++)
1961 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
1962 reg_names_o32[i].num, // & RNUM_MASK,
6047c971 1963 &zero_address_frag));
6047c971 1964
7d10b47d 1965 mips_no_prev_insn ();
252b5132
RH
1966
1967 mips_gprmask = 0;
1968 mips_cprmask[0] = 0;
1969 mips_cprmask[1] = 0;
1970 mips_cprmask[2] = 0;
1971 mips_cprmask[3] = 0;
1972
1973 /* set the default alignment for the text section (2**2) */
1974 record_alignment (text_section, 2);
1975
4d0d148d 1976 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132 1977
707bfff6 1978#ifdef OBJ_ELF
f43abd2b 1979 if (IS_ELF)
252b5132 1980 {
0a44bf69
RS
1981 /* On a native system other than VxWorks, sections must be aligned
1982 to 16 byte boundaries. When configured for an embedded ELF
1983 target, we don't bother. */
c41e87e3
CF
1984 if (strncmp (TARGET_OS, "elf", 3) != 0
1985 && strncmp (TARGET_OS, "vxworks", 7) != 0)
252b5132
RH
1986 {
1987 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1988 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1989 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1990 }
1991
1992 /* Create a .reginfo section for register masks and a .mdebug
1993 section for debugging information. */
1994 {
1995 segT seg;
1996 subsegT subseg;
1997 flagword flags;
1998 segT sec;
1999
2000 seg = now_seg;
2001 subseg = now_subseg;
2002
2003 /* The ABI says this section should be loaded so that the
2004 running program can access it. However, we don't load it
2005 if we are configured for an embedded target */
2006 flags = SEC_READONLY | SEC_DATA;
c41e87e3 2007 if (strncmp (TARGET_OS, "elf", 3) != 0)
252b5132
RH
2008 flags |= SEC_ALLOC | SEC_LOAD;
2009
316f5878 2010 if (mips_abi != N64_ABI)
252b5132
RH
2011 {
2012 sec = subseg_new (".reginfo", (subsegT) 0);
2013
195325d2
TS
2014 bfd_set_section_flags (stdoutput, sec, flags);
2015 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
bdaaa2e1 2016
252b5132 2017 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
252b5132
RH
2018 }
2019 else
2020 {
2021 /* The 64-bit ABI uses a .MIPS.options section rather than
2022 .reginfo section. */
2023 sec = subseg_new (".MIPS.options", (subsegT) 0);
195325d2
TS
2024 bfd_set_section_flags (stdoutput, sec, flags);
2025 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132 2026
252b5132
RH
2027 /* Set up the option header. */
2028 {
2029 Elf_Internal_Options opthdr;
2030 char *f;
2031
2032 opthdr.kind = ODK_REGINFO;
2033 opthdr.size = (sizeof (Elf_External_Options)
2034 + sizeof (Elf64_External_RegInfo));
2035 opthdr.section = 0;
2036 opthdr.info = 0;
2037 f = frag_more (sizeof (Elf_External_Options));
2038 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
2039 (Elf_External_Options *) f);
2040
2041 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
2042 }
252b5132
RH
2043 }
2044
2045 if (ECOFF_DEBUGGING)
2046 {
2047 sec = subseg_new (".mdebug", (subsegT) 0);
2048 (void) bfd_set_section_flags (stdoutput, sec,
2049 SEC_HAS_CONTENTS | SEC_READONLY);
2050 (void) bfd_set_section_alignment (stdoutput, sec, 2);
2051 }
f43abd2b 2052 else if (mips_flag_pdr)
ecb4347a
DJ
2053 {
2054 pdr_seg = subseg_new (".pdr", (subsegT) 0);
2055 (void) bfd_set_section_flags (stdoutput, pdr_seg,
2056 SEC_READONLY | SEC_RELOC
2057 | SEC_DEBUGGING);
2058 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
2059 }
252b5132
RH
2060
2061 subseg_set (seg, subseg);
2062 }
2063 }
707bfff6 2064#endif /* OBJ_ELF */
252b5132
RH
2065
2066 if (! ECOFF_DEBUGGING)
2067 md_obj_begin ();
71400594
RS
2068
2069 if (mips_fix_vr4120)
2070 init_vr4120_conflicts ();
252b5132
RH
2071}
2072
2073void
17a2f251 2074md_mips_end (void)
252b5132
RH
2075{
2076 if (! ECOFF_DEBUGGING)
2077 md_obj_end ();
2078}
2079
2080void
17a2f251 2081md_assemble (char *str)
252b5132
RH
2082{
2083 struct mips_cl_insn insn;
f6688943
TS
2084 bfd_reloc_code_real_type unused_reloc[3]
2085 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
2086
2087 imm_expr.X_op = O_absent;
5f74bc13 2088 imm2_expr.X_op = O_absent;
252b5132 2089 offset_expr.X_op = O_absent;
f6688943
TS
2090 imm_reloc[0] = BFD_RELOC_UNUSED;
2091 imm_reloc[1] = BFD_RELOC_UNUSED;
2092 imm_reloc[2] = BFD_RELOC_UNUSED;
2093 offset_reloc[0] = BFD_RELOC_UNUSED;
2094 offset_reloc[1] = BFD_RELOC_UNUSED;
2095 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
2096
2097 if (mips_opts.mips16)
2098 mips16_ip (str, &insn);
2099 else
2100 {
2101 mips_ip (str, &insn);
beae10d5
KH
2102 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
2103 str, insn.insn_opcode));
252b5132
RH
2104 }
2105
2106 if (insn_error)
2107 {
2108 as_bad ("%s `%s'", insn_error, str);
2109 return;
2110 }
2111
2112 if (insn.insn_mo->pinfo == INSN_MACRO)
2113 {
584892a6 2114 macro_start ();
252b5132
RH
2115 if (mips_opts.mips16)
2116 mips16_macro (&insn);
2117 else
2118 macro (&insn);
584892a6 2119 macro_end ();
252b5132
RH
2120 }
2121 else
2122 {
2123 if (imm_expr.X_op != O_absent)
4d7206a2 2124 append_insn (&insn, &imm_expr, imm_reloc);
252b5132 2125 else if (offset_expr.X_op != O_absent)
4d7206a2 2126 append_insn (&insn, &offset_expr, offset_reloc);
252b5132 2127 else
4d7206a2 2128 append_insn (&insn, NULL, unused_reloc);
252b5132
RH
2129 }
2130}
2131
5919d012 2132/* Return true if the given relocation might need a matching %lo().
0a44bf69
RS
2133 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
2134 need a matching %lo() when applied to local symbols. */
5919d012
RS
2135
2136static inline bfd_boolean
17a2f251 2137reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 2138{
3b91255e
RS
2139 return (HAVE_IN_PLACE_ADDENDS
2140 && (reloc == BFD_RELOC_HI16_S
0a44bf69
RS
2141 || reloc == BFD_RELOC_MIPS16_HI16_S
2142 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
2143 all GOT16 relocations evaluate to "G". */
2144 || (reloc == BFD_RELOC_MIPS_GOT16 && mips_pic != VXWORKS_PIC)));
5919d012
RS
2145}
2146
2147/* Return true if the given fixup is followed by a matching R_MIPS_LO16
2148 relocation. */
2149
2150static inline bfd_boolean
17a2f251 2151fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
2152{
2153 return (fixp->fx_next != NULL
d6f16593
MR
2154 && (fixp->fx_next->fx_r_type == BFD_RELOC_LO16
2155 || fixp->fx_next->fx_r_type == BFD_RELOC_MIPS16_LO16)
5919d012
RS
2156 && fixp->fx_addsy == fixp->fx_next->fx_addsy
2157 && fixp->fx_offset == fixp->fx_next->fx_offset);
2158}
2159
252b5132
RH
2160/* See whether instruction IP reads register REG. CLASS is the type
2161 of register. */
2162
2163static int
71400594 2164insn_uses_reg (const struct mips_cl_insn *ip, unsigned int reg,
17a2f251 2165 enum mips_regclass class)
252b5132
RH
2166{
2167 if (class == MIPS16_REG)
2168 {
2169 assert (mips_opts.mips16);
2170 reg = mips16_to_32_reg_map[reg];
2171 class = MIPS_GR_REG;
2172 }
2173
85b51719
TS
2174 /* Don't report on general register ZERO, since it never changes. */
2175 if (class == MIPS_GR_REG && reg == ZERO)
252b5132
RH
2176 return 0;
2177
2178 if (class == MIPS_FP_REG)
2179 {
2180 assert (! mips_opts.mips16);
2181 /* If we are called with either $f0 or $f1, we must check $f0.
2182 This is not optimal, because it will introduce an unnecessary
2183 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
2184 need to distinguish reading both $f0 and $f1 or just one of
2185 them. Note that we don't have to check the other way,
2186 because there is no instruction that sets both $f0 and $f1
2187 and requires a delay. */
2188 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
bf12938e 2189 && ((EXTRACT_OPERAND (FS, *ip) & ~(unsigned) 1)
252b5132
RH
2190 == (reg &~ (unsigned) 1)))
2191 return 1;
2192 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
bf12938e 2193 && ((EXTRACT_OPERAND (FT, *ip) & ~(unsigned) 1)
252b5132
RH
2194 == (reg &~ (unsigned) 1)))
2195 return 1;
2196 }
2197 else if (! mips_opts.mips16)
2198 {
2199 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
bf12938e 2200 && EXTRACT_OPERAND (RS, *ip) == reg)
252b5132
RH
2201 return 1;
2202 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
bf12938e 2203 && EXTRACT_OPERAND (RT, *ip) == reg)
252b5132
RH
2204 return 1;
2205 }
2206 else
2207 {
2208 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
bf12938e 2209 && mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)] == reg)
252b5132
RH
2210 return 1;
2211 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
bf12938e 2212 && mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)] == reg)
252b5132
RH
2213 return 1;
2214 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
bf12938e 2215 && (mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip)]
252b5132
RH
2216 == reg))
2217 return 1;
2218 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
2219 return 1;
2220 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
2221 return 1;
2222 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
2223 return 1;
2224 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
bf12938e 2225 && MIPS16_EXTRACT_OPERAND (REGR32, *ip) == reg)
252b5132
RH
2226 return 1;
2227 }
2228
2229 return 0;
2230}
2231
2232/* This function returns true if modifying a register requires a
2233 delay. */
2234
2235static int
17a2f251 2236reg_needs_delay (unsigned int reg)
252b5132
RH
2237{
2238 unsigned long prev_pinfo;
2239
47e39b9d 2240 prev_pinfo = history[0].insn_mo->pinfo;
252b5132 2241 if (! mips_opts.noreorder
81912461
ILT
2242 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
2243 && ! gpr_interlocks)
2244 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
2245 && ! cop_interlocks)))
252b5132 2246 {
81912461
ILT
2247 /* A load from a coprocessor or from memory. All load delays
2248 delay the use of general register rt for one instruction. */
bdaaa2e1 2249 /* Itbl support may require additional care here. */
252b5132 2250 know (prev_pinfo & INSN_WRITE_GPR_T);
bf12938e 2251 if (reg == EXTRACT_OPERAND (RT, history[0]))
252b5132
RH
2252 return 1;
2253 }
2254
2255 return 0;
2256}
2257
404a8071
RS
2258/* Move all labels in insn_labels to the current insertion point. */
2259
2260static void
2261mips_move_labels (void)
2262{
a8dbcb85 2263 segment_info_type *si = seg_info (now_seg);
404a8071
RS
2264 struct insn_label_list *l;
2265 valueT val;
2266
a8dbcb85 2267 for (l = si->label_list; l != NULL; l = l->next)
404a8071
RS
2268 {
2269 assert (S_GET_SEGMENT (l->label) == now_seg);
2270 symbol_set_frag (l->label, frag_now);
2271 val = (valueT) frag_now_fix ();
2272 /* mips16 text labels are stored as odd. */
2273 if (mips_opts.mips16)
2274 ++val;
2275 S_SET_VALUE (l->label, val);
2276 }
2277}
2278
5f0fe04b
TS
2279static bfd_boolean
2280s_is_linkonce (symbolS *sym, segT from_seg)
2281{
2282 bfd_boolean linkonce = FALSE;
2283 segT symseg = S_GET_SEGMENT (sym);
2284
2285 if (symseg != from_seg && !S_IS_LOCAL (sym))
2286 {
2287 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
2288 linkonce = TRUE;
2289#ifdef OBJ_ELF
2290 /* The GNU toolchain uses an extension for ELF: a section
2291 beginning with the magic string .gnu.linkonce is a
2292 linkonce section. */
2293 if (strncmp (segment_name (symseg), ".gnu.linkonce",
2294 sizeof ".gnu.linkonce" - 1) == 0)
2295 linkonce = TRUE;
2296#endif
2297 }
2298 return linkonce;
2299}
2300
252b5132
RH
2301/* Mark instruction labels in mips16 mode. This permits the linker to
2302 handle them specially, such as generating jalx instructions when
2303 needed. We also make them odd for the duration of the assembly, in
2304 order to generate the right sort of code. We will make them even
2305 in the adjust_symtab routine, while leaving them marked. This is
2306 convenient for the debugger and the disassembler. The linker knows
2307 to make them odd again. */
2308
2309static void
17a2f251 2310mips16_mark_labels (void)
252b5132 2311{
a8dbcb85
TS
2312 segment_info_type *si = seg_info (now_seg);
2313 struct insn_label_list *l;
252b5132 2314
a8dbcb85
TS
2315 if (!mips_opts.mips16)
2316 return;
2317
2318 for (l = si->label_list; l != NULL; l = l->next)
2319 {
2320 symbolS *label = l->label;
2321
2322#if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
f43abd2b 2323 if (IS_ELF)
30c09090 2324 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
252b5132 2325#endif
5f0fe04b
TS
2326 if ((S_GET_VALUE (label) & 1) == 0
2327 /* Don't adjust the address if the label is global or weak, or
2328 in a link-once section, since we'll be emitting symbol reloc
2329 references to it which will be patched up by the linker, and
2330 the final value of the symbol may or may not be MIPS16. */
2331 && ! S_IS_WEAK (label)
2332 && ! S_IS_EXTERNAL (label)
2333 && ! s_is_linkonce (label, now_seg))
a8dbcb85 2334 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
252b5132
RH
2335 }
2336}
2337
4d7206a2
RS
2338/* End the current frag. Make it a variant frag and record the
2339 relaxation info. */
2340
2341static void
2342relax_close_frag (void)
2343{
584892a6 2344 mips_macro_warning.first_frag = frag_now;
4d7206a2 2345 frag_var (rs_machine_dependent, 0, 0,
584892a6 2346 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
4d7206a2
RS
2347 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
2348
2349 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
2350 mips_relax.first_fixup = 0;
2351}
2352
2353/* Start a new relaxation sequence whose expansion depends on SYMBOL.
2354 See the comment above RELAX_ENCODE for more details. */
2355
2356static void
2357relax_start (symbolS *symbol)
2358{
2359 assert (mips_relax.sequence == 0);
2360 mips_relax.sequence = 1;
2361 mips_relax.symbol = symbol;
2362}
2363
2364/* Start generating the second version of a relaxable sequence.
2365 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
2366
2367static void
4d7206a2
RS
2368relax_switch (void)
2369{
2370 assert (mips_relax.sequence == 1);
2371 mips_relax.sequence = 2;
2372}
2373
2374/* End the current relaxable sequence. */
2375
2376static void
2377relax_end (void)
2378{
2379 assert (mips_relax.sequence == 2);
2380 relax_close_frag ();
2381 mips_relax.sequence = 0;
2382}
2383
71400594
RS
2384/* Classify an instruction according to the FIX_VR4120_* enumeration.
2385 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
2386 by VR4120 errata. */
4d7206a2 2387
71400594
RS
2388static unsigned int
2389classify_vr4120_insn (const char *name)
252b5132 2390{
71400594
RS
2391 if (strncmp (name, "macc", 4) == 0)
2392 return FIX_VR4120_MACC;
2393 if (strncmp (name, "dmacc", 5) == 0)
2394 return FIX_VR4120_DMACC;
2395 if (strncmp (name, "mult", 4) == 0)
2396 return FIX_VR4120_MULT;
2397 if (strncmp (name, "dmult", 5) == 0)
2398 return FIX_VR4120_DMULT;
2399 if (strstr (name, "div"))
2400 return FIX_VR4120_DIV;
2401 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
2402 return FIX_VR4120_MTHILO;
2403 return NUM_FIX_VR4120_CLASSES;
2404}
252b5132 2405
71400594
RS
2406/* Return the number of instructions that must separate INSN1 and INSN2,
2407 where INSN1 is the earlier instruction. Return the worst-case value
2408 for any INSN2 if INSN2 is null. */
252b5132 2409
71400594
RS
2410static unsigned int
2411insns_between (const struct mips_cl_insn *insn1,
2412 const struct mips_cl_insn *insn2)
2413{
2414 unsigned long pinfo1, pinfo2;
2415
2416 /* This function needs to know which pinfo flags are set for INSN2
2417 and which registers INSN2 uses. The former is stored in PINFO2 and
2418 the latter is tested via INSN2_USES_REG. If INSN2 is null, PINFO2
2419 will have every flag set and INSN2_USES_REG will always return true. */
2420 pinfo1 = insn1->insn_mo->pinfo;
2421 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
252b5132 2422
71400594
RS
2423#define INSN2_USES_REG(REG, CLASS) \
2424 (insn2 == NULL || insn_uses_reg (insn2, REG, CLASS))
2425
2426 /* For most targets, write-after-read dependencies on the HI and LO
2427 registers must be separated by at least two instructions. */
2428 if (!hilo_interlocks)
252b5132 2429 {
71400594
RS
2430 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
2431 return 2;
2432 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
2433 return 2;
2434 }
2435
2436 /* If we're working around r7000 errata, there must be two instructions
2437 between an mfhi or mflo and any instruction that uses the result. */
2438 if (mips_7000_hilo_fix
2439 && MF_HILO_INSN (pinfo1)
2440 && INSN2_USES_REG (EXTRACT_OPERAND (RD, *insn1), MIPS_GR_REG))
2441 return 2;
2442
2443 /* If working around VR4120 errata, check for combinations that need
2444 a single intervening instruction. */
2445 if (mips_fix_vr4120)
2446 {
2447 unsigned int class1, class2;
252b5132 2448
71400594
RS
2449 class1 = classify_vr4120_insn (insn1->insn_mo->name);
2450 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
252b5132 2451 {
71400594
RS
2452 if (insn2 == NULL)
2453 return 1;
2454 class2 = classify_vr4120_insn (insn2->insn_mo->name);
2455 if (vr4120_conflicts[class1] & (1 << class2))
2456 return 1;
252b5132 2457 }
71400594
RS
2458 }
2459
2460 if (!mips_opts.mips16)
2461 {
2462 /* Check for GPR or coprocessor load delays. All such delays
2463 are on the RT register. */
2464 /* Itbl support may require additional care here. */
2465 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
2466 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
252b5132 2467 {
71400594
RS
2468 know (pinfo1 & INSN_WRITE_GPR_T);
2469 if (INSN2_USES_REG (EXTRACT_OPERAND (RT, *insn1), MIPS_GR_REG))
2470 return 1;
2471 }
2472
2473 /* Check for generic coprocessor hazards.
2474
2475 This case is not handled very well. There is no special
2476 knowledge of CP0 handling, and the coprocessors other than
2477 the floating point unit are not distinguished at all. */
2478 /* Itbl support may require additional care here. FIXME!
2479 Need to modify this to include knowledge about
2480 user specified delays! */
2481 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
2482 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
2483 {
2484 /* Handle cases where INSN1 writes to a known general coprocessor
2485 register. There must be a one instruction delay before INSN2
2486 if INSN2 reads that register, otherwise no delay is needed. */
2487 if (pinfo1 & INSN_WRITE_FPR_T)
252b5132 2488 {
71400594
RS
2489 if (INSN2_USES_REG (EXTRACT_OPERAND (FT, *insn1), MIPS_FP_REG))
2490 return 1;
252b5132 2491 }
71400594 2492 else if (pinfo1 & INSN_WRITE_FPR_S)
252b5132 2493 {
71400594
RS
2494 if (INSN2_USES_REG (EXTRACT_OPERAND (FS, *insn1), MIPS_FP_REG))
2495 return 1;
252b5132
RH
2496 }
2497 else
2498 {
71400594
RS
2499 /* Read-after-write dependencies on the control registers
2500 require a two-instruction gap. */
2501 if ((pinfo1 & INSN_WRITE_COND_CODE)
2502 && (pinfo2 & INSN_READ_COND_CODE))
2503 return 2;
2504
2505 /* We don't know exactly what INSN1 does. If INSN2 is
2506 also a coprocessor instruction, assume there must be
2507 a one instruction gap. */
2508 if (pinfo2 & INSN_COP)
2509 return 1;
252b5132
RH
2510 }
2511 }
6b76fefe 2512
71400594
RS
2513 /* Check for read-after-write dependencies on the coprocessor
2514 control registers in cases where INSN1 does not need a general
2515 coprocessor delay. This means that INSN1 is a floating point
2516 comparison instruction. */
2517 /* Itbl support may require additional care here. */
2518 else if (!cop_interlocks
2519 && (pinfo1 & INSN_WRITE_COND_CODE)
2520 && (pinfo2 & INSN_READ_COND_CODE))
2521 return 1;
2522 }
6b76fefe 2523
71400594 2524#undef INSN2_USES_REG
6b76fefe 2525
71400594
RS
2526 return 0;
2527}
6b76fefe 2528
7d8e00cf
RS
2529/* Return the number of nops that would be needed to work around the
2530 VR4130 mflo/mfhi errata if instruction INSN immediately followed
2531 the MAX_VR4130_NOPS instructions described by HISTORY. */
2532
2533static int
2534nops_for_vr4130 (const struct mips_cl_insn *history,
2535 const struct mips_cl_insn *insn)
2536{
2537 int i, j, reg;
2538
2539 /* Check if the instruction writes to HI or LO. MTHI and MTLO
2540 are not affected by the errata. */
2541 if (insn != 0
2542 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
2543 || strcmp (insn->insn_mo->name, "mtlo") == 0
2544 || strcmp (insn->insn_mo->name, "mthi") == 0))
2545 return 0;
2546
2547 /* Search for the first MFLO or MFHI. */
2548 for (i = 0; i < MAX_VR4130_NOPS; i++)
2549 if (!history[i].noreorder_p && MF_HILO_INSN (history[i].insn_mo->pinfo))
2550 {
2551 /* Extract the destination register. */
2552 if (mips_opts.mips16)
2553 reg = mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, history[i])];
2554 else
2555 reg = EXTRACT_OPERAND (RD, history[i]);
2556
2557 /* No nops are needed if INSN reads that register. */
2558 if (insn != NULL && insn_uses_reg (insn, reg, MIPS_GR_REG))
2559 return 0;
2560
2561 /* ...or if any of the intervening instructions do. */
2562 for (j = 0; j < i; j++)
2563 if (insn_uses_reg (&history[j], reg, MIPS_GR_REG))
2564 return 0;
2565
2566 return MAX_VR4130_NOPS - i;
2567 }
2568 return 0;
2569}
2570
71400594
RS
2571/* Return the number of nops that would be needed if instruction INSN
2572 immediately followed the MAX_NOPS instructions given by HISTORY,
2573 where HISTORY[0] is the most recent instruction. If INSN is null,
2574 return the worse-case number of nops for any instruction. */
bdaaa2e1 2575
71400594
RS
2576static int
2577nops_for_insn (const struct mips_cl_insn *history,
2578 const struct mips_cl_insn *insn)
2579{
2580 int i, nops, tmp_nops;
bdaaa2e1 2581
71400594 2582 nops = 0;
7d8e00cf 2583 for (i = 0; i < MAX_DELAY_NOPS; i++)
71400594
RS
2584 if (!history[i].noreorder_p)
2585 {
2586 tmp_nops = insns_between (history + i, insn) - i;
2587 if (tmp_nops > nops)
2588 nops = tmp_nops;
2589 }
7d8e00cf
RS
2590
2591 if (mips_fix_vr4130)
2592 {
2593 tmp_nops = nops_for_vr4130 (history, insn);
2594 if (tmp_nops > nops)
2595 nops = tmp_nops;
2596 }
2597
71400594
RS
2598 return nops;
2599}
252b5132 2600
71400594
RS
2601/* The variable arguments provide NUM_INSNS extra instructions that
2602 might be added to HISTORY. Return the largest number of nops that
2603 would be needed after the extended sequence. */
252b5132 2604
71400594
RS
2605static int
2606nops_for_sequence (int num_insns, const struct mips_cl_insn *history, ...)
2607{
2608 va_list args;
2609 struct mips_cl_insn buffer[MAX_NOPS];
2610 struct mips_cl_insn *cursor;
2611 int nops;
2612
2613 va_start (args, history);
2614 cursor = buffer + num_insns;
2615 memcpy (cursor, history, (MAX_NOPS - num_insns) * sizeof (*cursor));
2616 while (cursor > buffer)
2617 *--cursor = *va_arg (args, const struct mips_cl_insn *);
2618
2619 nops = nops_for_insn (buffer, NULL);
2620 va_end (args);
2621 return nops;
2622}
252b5132 2623
71400594
RS
2624/* Like nops_for_insn, but if INSN is a branch, take into account the
2625 worst-case delay for the branch target. */
252b5132 2626
71400594
RS
2627static int
2628nops_for_insn_or_target (const struct mips_cl_insn *history,
2629 const struct mips_cl_insn *insn)
2630{
2631 int nops, tmp_nops;
60b63b72 2632
71400594
RS
2633 nops = nops_for_insn (history, insn);
2634 if (insn->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
2635 | INSN_COND_BRANCH_DELAY
2636 | INSN_COND_BRANCH_LIKELY))
2637 {
2638 tmp_nops = nops_for_sequence (2, history, insn, NOP_INSN);
2639 if (tmp_nops > nops)
2640 nops = tmp_nops;
2641 }
2642 else if (mips_opts.mips16 && (insn->insn_mo->pinfo & MIPS16_INSN_BRANCH))
2643 {
2644 tmp_nops = nops_for_sequence (1, history, insn);
2645 if (tmp_nops > nops)
2646 nops = tmp_nops;
2647 }
2648 return nops;
2649}
2650
2651/* Output an instruction. IP is the instruction information.
2652 ADDRESS_EXPR is an operand of the instruction to be used with
2653 RELOC_TYPE. */
2654
2655static void
2656append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
2657 bfd_reloc_code_real_type *reloc_type)
2658{
3994f87e 2659 unsigned long prev_pinfo, pinfo;
71400594
RS
2660 relax_stateT prev_insn_frag_type = 0;
2661 bfd_boolean relaxed_branch = FALSE;
a8dbcb85 2662 segment_info_type *si = seg_info (now_seg);
71400594
RS
2663
2664 /* Mark instruction labels in mips16 mode. */
2665 mips16_mark_labels ();
2666
2667 prev_pinfo = history[0].insn_mo->pinfo;
2668 pinfo = ip->insn_mo->pinfo;
2669
2670 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
2671 {
2672 /* There are a lot of optimizations we could do that we don't.
2673 In particular, we do not, in general, reorder instructions.
2674 If you use gcc with optimization, it will reorder
2675 instructions and generally do much more optimization then we
2676 do here; repeating all that work in the assembler would only
2677 benefit hand written assembly code, and does not seem worth
2678 it. */
2679 int nops = (mips_optimize == 0
2680 ? nops_for_insn (history, NULL)
2681 : nops_for_insn_or_target (history, ip));
2682 if (nops > 0)
252b5132
RH
2683 {
2684 fragS *old_frag;
2685 unsigned long old_frag_offset;
2686 int i;
252b5132
RH
2687
2688 old_frag = frag_now;
2689 old_frag_offset = frag_now_fix ();
2690
2691 for (i = 0; i < nops; i++)
2692 emit_nop ();
2693
2694 if (listing)
2695 {
2696 listing_prev_line ();
2697 /* We may be at the start of a variant frag. In case we
2698 are, make sure there is enough space for the frag
2699 after the frags created by listing_prev_line. The
2700 argument to frag_grow here must be at least as large
2701 as the argument to all other calls to frag_grow in
2702 this file. We don't have to worry about being in the
2703 middle of a variant frag, because the variants insert
2704 all needed nop instructions themselves. */
2705 frag_grow (40);
2706 }
2707
404a8071 2708 mips_move_labels ();
252b5132
RH
2709
2710#ifndef NO_ECOFF_DEBUGGING
2711 if (ECOFF_DEBUGGING)
2712 ecoff_fix_loc (old_frag, old_frag_offset);
2713#endif
2714 }
71400594
RS
2715 }
2716 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
2717 {
2718 /* Work out how many nops in prev_nop_frag are needed by IP. */
2719 int nops = nops_for_insn_or_target (history, ip);
2720 assert (nops <= prev_nop_frag_holds);
252b5132 2721
71400594
RS
2722 /* Enforce NOPS as a minimum. */
2723 if (nops > prev_nop_frag_required)
2724 prev_nop_frag_required = nops;
252b5132 2725
71400594
RS
2726 if (prev_nop_frag_holds == prev_nop_frag_required)
2727 {
2728 /* Settle for the current number of nops. Update the history
2729 accordingly (for the benefit of any future .set reorder code). */
2730 prev_nop_frag = NULL;
2731 insert_into_history (prev_nop_frag_since,
2732 prev_nop_frag_holds, NOP_INSN);
2733 }
2734 else
2735 {
2736 /* Allow this instruction to replace one of the nops that was
2737 tentatively added to prev_nop_frag. */
2738 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
2739 prev_nop_frag_holds--;
2740 prev_nop_frag_since++;
252b5132
RH
2741 }
2742 }
2743
58e2ea4d
MR
2744#ifdef OBJ_ELF
2745 /* The value passed to dwarf2_emit_insn is the distance between
2746 the beginning of the current instruction and the address that
2747 should be recorded in the debug tables. For MIPS16 debug info
2748 we want to use ISA-encoded addresses, so we pass -1 for an
2749 address higher by one than the current. */
2750 dwarf2_emit_insn (mips_opts.mips16 ? -1 : 0);
2751#endif
2752
895921c9 2753 /* Record the frag type before frag_var. */
47e39b9d
RS
2754 if (history[0].frag)
2755 prev_insn_frag_type = history[0].frag->fr_type;
895921c9 2756
4d7206a2 2757 if (address_expr
0b25d3e6 2758 && *reloc_type == BFD_RELOC_16_PCREL_S2
4a6a3df4
AO
2759 && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
2760 || pinfo & INSN_COND_BRANCH_LIKELY)
2761 && mips_relax_branch
2762 /* Don't try branch relaxation within .set nomacro, or within
2763 .set noat if we use $at for PIC computations. If it turns
2764 out that the branch was out-of-range, we'll get an error. */
2765 && !mips_opts.warn_about_macros
741fe287 2766 && (mips_opts.at || mips_pic == NO_PIC)
4a6a3df4
AO
2767 && !mips_opts.mips16)
2768 {
895921c9 2769 relaxed_branch = TRUE;
1e915849
RS
2770 add_relaxed_insn (ip, (relaxed_branch_length
2771 (NULL, NULL,
2772 (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2773 : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1
2774 : 0)), 4,
2775 RELAX_BRANCH_ENCODE
2776 (pinfo & INSN_UNCOND_BRANCH_DELAY,
2777 pinfo & INSN_COND_BRANCH_LIKELY,
2778 pinfo & INSN_WRITE_GPR_31,
2779 0),
2780 address_expr->X_add_symbol,
2781 address_expr->X_add_number);
4a6a3df4
AO
2782 *reloc_type = BFD_RELOC_UNUSED;
2783 }
2784 else if (*reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
2785 {
2786 /* We need to set up a variant frag. */
2787 assert (mips_opts.mips16 && address_expr != NULL);
1e915849
RS
2788 add_relaxed_insn (ip, 4, 0,
2789 RELAX_MIPS16_ENCODE
2790 (*reloc_type - BFD_RELOC_UNUSED,
2791 mips16_small, mips16_ext,
2792 prev_pinfo & INSN_UNCOND_BRANCH_DELAY,
2793 history[0].mips16_absolute_jump_p),
2794 make_expr_symbol (address_expr), 0);
252b5132 2795 }
252b5132
RH
2796 else if (mips_opts.mips16
2797 && ! ip->use_extend
f6688943 2798 && *reloc_type != BFD_RELOC_MIPS16_JMP)
9497f5ac 2799 {
b8ee1a6e
DU
2800 if ((pinfo & INSN_UNCOND_BRANCH_DELAY) == 0)
2801 /* Make sure there is enough room to swap this instruction with
2802 a following jump instruction. */
2803 frag_grow (6);
1e915849 2804 add_fixed_insn (ip);
252b5132
RH
2805 }
2806 else
2807 {
2808 if (mips_opts.mips16
2809 && mips_opts.noreorder
2810 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2811 as_warn (_("extended instruction in delay slot"));
2812
4d7206a2
RS
2813 if (mips_relax.sequence)
2814 {
2815 /* If we've reached the end of this frag, turn it into a variant
2816 frag and record the information for the instructions we've
2817 written so far. */
2818 if (frag_room () < 4)
2819 relax_close_frag ();
2820 mips_relax.sizes[mips_relax.sequence - 1] += 4;
2821 }
2822
584892a6
RS
2823 if (mips_relax.sequence != 2)
2824 mips_macro_warning.sizes[0] += 4;
2825 if (mips_relax.sequence != 1)
2826 mips_macro_warning.sizes[1] += 4;
2827
1e915849
RS
2828 if (mips_opts.mips16)
2829 {
2830 ip->fixed_p = 1;
2831 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
2832 }
2833 add_fixed_insn (ip);
252b5132
RH
2834 }
2835
01a3f561 2836 if (address_expr != NULL && *reloc_type <= BFD_RELOC_UNUSED)
252b5132
RH
2837 {
2838 if (address_expr->X_op == O_constant)
2839 {
f17c130b 2840 unsigned int tmp;
f6688943
TS
2841
2842 switch (*reloc_type)
252b5132
RH
2843 {
2844 case BFD_RELOC_32:
2845 ip->insn_opcode |= address_expr->X_add_number;
2846 break;
2847
f6688943 2848 case BFD_RELOC_MIPS_HIGHEST:
f17c130b
AM
2849 tmp = (address_expr->X_add_number + 0x800080008000ull) >> 48;
2850 ip->insn_opcode |= tmp & 0xffff;
f6688943
TS
2851 break;
2852
2853 case BFD_RELOC_MIPS_HIGHER:
f17c130b
AM
2854 tmp = (address_expr->X_add_number + 0x80008000ull) >> 32;
2855 ip->insn_opcode |= tmp & 0xffff;
f6688943
TS
2856 break;
2857
2858 case BFD_RELOC_HI16_S:
f17c130b
AM
2859 tmp = (address_expr->X_add_number + 0x8000) >> 16;
2860 ip->insn_opcode |= tmp & 0xffff;
f6688943
TS
2861 break;
2862
2863 case BFD_RELOC_HI16:
2864 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2865 break;
2866
01a3f561 2867 case BFD_RELOC_UNUSED:
252b5132 2868 case BFD_RELOC_LO16:
ed6fb7bd 2869 case BFD_RELOC_MIPS_GOT_DISP:
252b5132
RH
2870 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2871 break;
2872
2873 case BFD_RELOC_MIPS_JMP:
2874 if ((address_expr->X_add_number & 3) != 0)
2875 as_bad (_("jump to misaligned address (0x%lx)"),
2876 (unsigned long) address_expr->X_add_number);
2877 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2878 break;
2879
2880 case BFD_RELOC_MIPS16_JMP:
2881 if ((address_expr->X_add_number & 3) != 0)
2882 as_bad (_("jump to misaligned address (0x%lx)"),
2883 (unsigned long) address_expr->X_add_number);
2884 ip->insn_opcode |=
2885 (((address_expr->X_add_number & 0x7c0000) << 3)
2886 | ((address_expr->X_add_number & 0xf800000) >> 7)
2887 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2888 break;
2889
252b5132 2890 case BFD_RELOC_16_PCREL_S2:
bad36eac
DJ
2891 if ((address_expr->X_add_number & 3) != 0)
2892 as_bad (_("branch to misaligned address (0x%lx)"),
2893 (unsigned long) address_expr->X_add_number);
2894 if (mips_relax_branch)
2895 goto need_reloc;
2896 if ((address_expr->X_add_number + 0x20000) & ~0x3ffff)
2897 as_bad (_("branch address range overflow (0x%lx)"),
2898 (unsigned long) address_expr->X_add_number);
2899 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0xffff;
2900 break;
252b5132
RH
2901
2902 default:
2903 internalError ();
2904 }
2905 }
01a3f561 2906 else if (*reloc_type < BFD_RELOC_UNUSED)
252b5132 2907 need_reloc:
4d7206a2
RS
2908 {
2909 reloc_howto_type *howto;
2910 int i;
34ce925e 2911
4d7206a2
RS
2912 /* In a compound relocation, it is the final (outermost)
2913 operator that determines the relocated field. */
2914 for (i = 1; i < 3; i++)
2915 if (reloc_type[i] == BFD_RELOC_UNUSED)
2916 break;
34ce925e 2917
4d7206a2 2918 howto = bfd_reloc_type_lookup (stdoutput, reloc_type[i - 1]);
1e915849
RS
2919 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
2920 bfd_get_reloc_size (howto),
2921 address_expr,
2922 reloc_type[0] == BFD_RELOC_16_PCREL_S2,
2923 reloc_type[0]);
4d7206a2 2924
b314ec0e
RS
2925 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
2926 if (reloc_type[0] == BFD_RELOC_MIPS16_JMP
2927 && ip->fixp[0]->fx_addsy)
2928 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
2929
4d7206a2
RS
2930 /* These relocations can have an addend that won't fit in
2931 4 octets for 64bit assembly. */
2932 if (HAVE_64BIT_GPRS
2933 && ! howto->partial_inplace
2934 && (reloc_type[0] == BFD_RELOC_16
2935 || reloc_type[0] == BFD_RELOC_32
2936 || reloc_type[0] == BFD_RELOC_MIPS_JMP
2937 || reloc_type[0] == BFD_RELOC_HI16_S
2938 || reloc_type[0] == BFD_RELOC_LO16
2939 || reloc_type[0] == BFD_RELOC_GPREL16
2940 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
2941 || reloc_type[0] == BFD_RELOC_GPREL32
2942 || reloc_type[0] == BFD_RELOC_64
2943 || reloc_type[0] == BFD_RELOC_CTOR
2944 || reloc_type[0] == BFD_RELOC_MIPS_SUB
2945 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
2946 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
2947 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
2948 || reloc_type[0] == BFD_RELOC_MIPS_REL16
d6f16593
MR
2949 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
2950 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
2951 || reloc_type[0] == BFD_RELOC_MIPS16_HI16_S
2952 || reloc_type[0] == BFD_RELOC_MIPS16_LO16))
1e915849 2953 ip->fixp[0]->fx_no_overflow = 1;
4d7206a2
RS
2954
2955 if (mips_relax.sequence)
2956 {
2957 if (mips_relax.first_fixup == 0)
1e915849 2958 mips_relax.first_fixup = ip->fixp[0];
4d7206a2
RS
2959 }
2960 else if (reloc_needs_lo_p (*reloc_type))
2961 {
2962 struct mips_hi_fixup *hi_fixup;
252b5132 2963
4d7206a2
RS
2964 /* Reuse the last entry if it already has a matching %lo. */
2965 hi_fixup = mips_hi_fixup_list;
2966 if (hi_fixup == 0
2967 || !fixup_has_matching_lo_p (hi_fixup->fixp))
2968 {
2969 hi_fixup = ((struct mips_hi_fixup *)
2970 xmalloc (sizeof (struct mips_hi_fixup)));
2971 hi_fixup->next = mips_hi_fixup_list;
2972 mips_hi_fixup_list = hi_fixup;
252b5132 2973 }
1e915849 2974 hi_fixup->fixp = ip->fixp[0];
4d7206a2
RS
2975 hi_fixup->seg = now_seg;
2976 }
f6688943 2977
4d7206a2
RS
2978 /* Add fixups for the second and third relocations, if given.
2979 Note that the ABI allows the second relocation to be
2980 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
2981 moment we only use RSS_UNDEF, but we could add support
2982 for the others if it ever becomes necessary. */
2983 for (i = 1; i < 3; i++)
2984 if (reloc_type[i] != BFD_RELOC_UNUSED)
2985 {
1e915849
RS
2986 ip->fixp[i] = fix_new (ip->frag, ip->where,
2987 ip->fixp[0]->fx_size, NULL, 0,
2988 FALSE, reloc_type[i]);
b1dca8ee
RS
2989
2990 /* Use fx_tcbit to mark compound relocs. */
1e915849
RS
2991 ip->fixp[0]->fx_tcbit = 1;
2992 ip->fixp[i]->fx_tcbit = 1;
4d7206a2 2993 }
252b5132
RH
2994 }
2995 }
1e915849 2996 install_insn (ip);
252b5132
RH
2997
2998 /* Update the register mask information. */
2999 if (! mips_opts.mips16)
3000 {
3001 if (pinfo & INSN_WRITE_GPR_D)
bf12938e 3002 mips_gprmask |= 1 << EXTRACT_OPERAND (RD, *ip);
252b5132 3003 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
bf12938e 3004 mips_gprmask |= 1 << EXTRACT_OPERAND (RT, *ip);
252b5132 3005 if (pinfo & INSN_READ_GPR_S)
bf12938e 3006 mips_gprmask |= 1 << EXTRACT_OPERAND (RS, *ip);
252b5132 3007 if (pinfo & INSN_WRITE_GPR_31)
f9419b05 3008 mips_gprmask |= 1 << RA;
252b5132 3009 if (pinfo & INSN_WRITE_FPR_D)
bf12938e 3010 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FD, *ip);
252b5132 3011 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
bf12938e 3012 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FS, *ip);
252b5132 3013 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
bf12938e 3014 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FT, *ip);
252b5132 3015 if ((pinfo & INSN_READ_FPR_R) != 0)
bf12938e 3016 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FR, *ip);
252b5132
RH
3017 if (pinfo & INSN_COP)
3018 {
bdaaa2e1
KH
3019 /* We don't keep enough information to sort these cases out.
3020 The itbl support does keep this information however, although
3021 we currently don't support itbl fprmats as part of the cop
3022 instruction. May want to add this support in the future. */
252b5132
RH
3023 }
3024 /* Never set the bit for $0, which is always zero. */
beae10d5 3025 mips_gprmask &= ~1 << 0;
252b5132
RH
3026 }
3027 else
3028 {
3029 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
bf12938e 3030 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RX, *ip);
252b5132 3031 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
bf12938e 3032 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RY, *ip);
252b5132 3033 if (pinfo & MIPS16_INSN_WRITE_Z)
bf12938e 3034 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RZ, *ip);
252b5132
RH
3035 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
3036 mips_gprmask |= 1 << TREG;
3037 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
3038 mips_gprmask |= 1 << SP;
3039 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
3040 mips_gprmask |= 1 << RA;
3041 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
3042 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
3043 if (pinfo & MIPS16_INSN_READ_Z)
bf12938e 3044 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip);
252b5132 3045 if (pinfo & MIPS16_INSN_READ_GPR_X)
bf12938e 3046 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (REGR32, *ip);
252b5132
RH
3047 }
3048
4d7206a2 3049 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
252b5132
RH
3050 {
3051 /* Filling the branch delay slot is more complex. We try to
3052 switch the branch with the previous instruction, which we can
3053 do if the previous instruction does not set up a condition
3054 that the branch tests and if the branch is not itself the
3055 target of any branch. */
3056 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
3057 || (pinfo & INSN_COND_BRANCH_DELAY))
3058 {
3059 if (mips_optimize < 2
3060 /* If we have seen .set volatile or .set nomove, don't
3061 optimize. */
3062 || mips_opts.nomove != 0
a38419a5
RS
3063 /* We can't swap if the previous instruction's position
3064 is fixed. */
3065 || history[0].fixed_p
252b5132
RH
3066 /* If the previous previous insn was in a .set
3067 noreorder, we can't swap. Actually, the MIPS
3068 assembler will swap in this situation. However, gcc
3069 configured -with-gnu-as will generate code like
3070 .set noreorder
3071 lw $4,XXX
3072 .set reorder
3073 INSN
3074 bne $4,$0,foo
3075 in which we can not swap the bne and INSN. If gcc is
3076 not configured -with-gnu-as, it does not output the
a38419a5 3077 .set pseudo-ops. */
47e39b9d 3078 || history[1].noreorder_p
252b5132
RH
3079 /* If the branch is itself the target of a branch, we
3080 can not swap. We cheat on this; all we check for is
3081 whether there is a label on this instruction. If
3082 there are any branches to anything other than a
3083 label, users must use .set noreorder. */
a8dbcb85 3084 || si->label_list != NULL
895921c9
MR
3085 /* If the previous instruction is in a variant frag
3086 other than this branch's one, we cannot do the swap.
3087 This does not apply to the mips16, which uses variant
3088 frags for different purposes. */
252b5132 3089 || (! mips_opts.mips16
895921c9 3090 && prev_insn_frag_type == rs_machine_dependent)
71400594
RS
3091 /* Check for conflicts between the branch and the instructions
3092 before the candidate delay slot. */
3093 || nops_for_insn (history + 1, ip) > 0
3094 /* Check for conflicts between the swapped sequence and the
3095 target of the branch. */
3096 || nops_for_sequence (2, history + 1, ip, history) > 0
252b5132
RH
3097 /* We do not swap with a trap instruction, since it
3098 complicates trap handlers to have the trap
3099 instruction be in a delay slot. */
3100 || (prev_pinfo & INSN_TRAP)
3101 /* If the branch reads a register that the previous
3102 instruction sets, we can not swap. */
3103 || (! mips_opts.mips16
3104 && (prev_pinfo & INSN_WRITE_GPR_T)
bf12938e 3105 && insn_uses_reg (ip, EXTRACT_OPERAND (RT, history[0]),
252b5132
RH
3106 MIPS_GR_REG))
3107 || (! mips_opts.mips16
3108 && (prev_pinfo & INSN_WRITE_GPR_D)
bf12938e 3109 && insn_uses_reg (ip, EXTRACT_OPERAND (RD, history[0]),
252b5132
RH
3110 MIPS_GR_REG))
3111 || (mips_opts.mips16
3112 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
bf12938e
RS
3113 && (insn_uses_reg
3114 (ip, MIPS16_EXTRACT_OPERAND (RX, history[0]),
3115 MIPS16_REG)))
252b5132 3116 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
bf12938e
RS
3117 && (insn_uses_reg
3118 (ip, MIPS16_EXTRACT_OPERAND (RY, history[0]),
3119 MIPS16_REG)))
252b5132 3120 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
bf12938e
RS
3121 && (insn_uses_reg
3122 (ip, MIPS16_EXTRACT_OPERAND (RZ, history[0]),
3123 MIPS16_REG)))
252b5132
RH
3124 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
3125 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
3126 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
3127 && insn_uses_reg (ip, RA, MIPS_GR_REG))
3128 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
3129 && insn_uses_reg (ip,
47e39b9d
RS
3130 MIPS16OP_EXTRACT_REG32R
3131 (history[0].insn_opcode),
252b5132
RH
3132 MIPS_GR_REG))))
3133 /* If the branch writes a register that the previous
3134 instruction sets, we can not swap (we know that
3135 branches write only to RD or to $31). */
3136 || (! mips_opts.mips16
3137 && (prev_pinfo & INSN_WRITE_GPR_T)
3138 && (((pinfo & INSN_WRITE_GPR_D)
bf12938e
RS
3139 && (EXTRACT_OPERAND (RT, history[0])
3140 == EXTRACT_OPERAND (RD, *ip)))
252b5132 3141 || ((pinfo & INSN_WRITE_GPR_31)
bf12938e 3142 && EXTRACT_OPERAND (RT, history[0]) == RA)))
252b5132
RH
3143 || (! mips_opts.mips16
3144 && (prev_pinfo & INSN_WRITE_GPR_D)
3145 && (((pinfo & INSN_WRITE_GPR_D)
bf12938e
RS
3146 && (EXTRACT_OPERAND (RD, history[0])
3147 == EXTRACT_OPERAND (RD, *ip)))
252b5132 3148 || ((pinfo & INSN_WRITE_GPR_31)
bf12938e 3149 && EXTRACT_OPERAND (RD, history[0]) == RA)))
252b5132
RH
3150 || (mips_opts.mips16
3151 && (pinfo & MIPS16_INSN_WRITE_31)
3152 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
3153 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
47e39b9d 3154 && (MIPS16OP_EXTRACT_REG32R (history[0].insn_opcode)
252b5132
RH
3155 == RA))))
3156 /* If the branch writes a register that the previous
3157 instruction reads, we can not swap (we know that
3158 branches only write to RD or to $31). */
3159 || (! mips_opts.mips16
3160 && (pinfo & INSN_WRITE_GPR_D)
47e39b9d 3161 && insn_uses_reg (&history[0],
bf12938e 3162 EXTRACT_OPERAND (RD, *ip),
252b5132
RH
3163 MIPS_GR_REG))
3164 || (! mips_opts.mips16
3165 && (pinfo & INSN_WRITE_GPR_31)
47e39b9d 3166 && insn_uses_reg (&history[0], RA, MIPS_GR_REG))
252b5132
RH
3167 || (mips_opts.mips16
3168 && (pinfo & MIPS16_INSN_WRITE_31)
47e39b9d 3169 && insn_uses_reg (&history[0], RA, MIPS_GR_REG))
252b5132
RH
3170 /* If one instruction sets a condition code and the
3171 other one uses a condition code, we can not swap. */
3172 || ((pinfo & INSN_READ_COND_CODE)
3173 && (prev_pinfo & INSN_WRITE_COND_CODE))
3174 || ((pinfo & INSN_WRITE_COND_CODE)
3175 && (prev_pinfo & INSN_READ_COND_CODE))
3176 /* If the previous instruction uses the PC, we can not
3177 swap. */
3178 || (mips_opts.mips16
3179 && (prev_pinfo & MIPS16_INSN_READ_PC))
252b5132
RH
3180 /* If the previous instruction had a fixup in mips16
3181 mode, we can not swap. This normally means that the
3182 previous instruction was a 4 byte branch anyhow. */
47e39b9d 3183 || (mips_opts.mips16 && history[0].fixp[0])
bdaaa2e1
KH
3184 /* If the previous instruction is a sync, sync.l, or
3185 sync.p, we can not swap. */
f173e82e 3186 || (prev_pinfo & INSN_SYNC))
252b5132 3187 {
29024861
DU
3188 if (mips_opts.mips16
3189 && (pinfo & INSN_UNCOND_BRANCH_DELAY)
3190 && (pinfo & (MIPS16_INSN_READ_X | MIPS16_INSN_READ_31))
3994f87e 3191 && ISA_SUPPORTS_MIPS16E)
29024861
DU
3192 {
3193 /* Convert MIPS16 jr/jalr into a "compact" jump. */
3194 ip->insn_opcode |= 0x0080;
3195 install_insn (ip);
3196 insert_into_history (0, 1, ip);
3197 }
3198 else
3199 {
3200 /* We could do even better for unconditional branches to
3201 portions of this object file; we could pick up the
3202 instruction at the destination, put it in the delay
3203 slot, and bump the destination address. */
3204 insert_into_history (0, 1, ip);
3205 emit_nop ();
3206 }
3207
dd22970f
ILT
3208 if (mips_relax.sequence)
3209 mips_relax.sizes[mips_relax.sequence - 1] += 4;
252b5132
RH
3210 }
3211 else
3212 {
3213 /* It looks like we can actually do the swap. */
1e915849
RS
3214 struct mips_cl_insn delay = history[0];
3215 if (mips_opts.mips16)
252b5132 3216 {
b8ee1a6e
DU
3217 know (delay.frag == ip->frag);
3218 move_insn (ip, delay.frag, delay.where);
3219 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
1e915849
RS
3220 }
3221 else if (relaxed_branch)
3222 {
3223 /* Add the delay slot instruction to the end of the
3224 current frag and shrink the fixed part of the
3225 original frag. If the branch occupies the tail of
3226 the latter, move it backwards to cover the gap. */
3227 delay.frag->fr_fix -= 4;
3228 if (delay.frag == ip->frag)
3229 move_insn (ip, ip->frag, ip->where - 4);
3230 add_fixed_insn (&delay);
252b5132
RH
3231 }
3232 else
3233 {
1e915849
RS
3234 move_insn (&delay, ip->frag, ip->where);
3235 move_insn (ip, history[0].frag, history[0].where);
252b5132 3236 }
1e915849
RS
3237 history[0] = *ip;
3238 delay.fixed_p = 1;
3239 insert_into_history (0, 1, &delay);
252b5132 3240 }
252b5132
RH
3241
3242 /* If that was an unconditional branch, forget the previous
3243 insn information. */
3244 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
7d10b47d 3245 mips_no_prev_insn ();
252b5132
RH
3246 }
3247 else if (pinfo & INSN_COND_BRANCH_LIKELY)
3248 {
3249 /* We don't yet optimize a branch likely. What we should do
3250 is look at the target, copy the instruction found there
3251 into the delay slot, and increment the branch to jump to
3252 the next instruction. */
1e915849 3253 insert_into_history (0, 1, ip);
252b5132 3254 emit_nop ();
252b5132
RH
3255 }
3256 else
1e915849 3257 insert_into_history (0, 1, ip);
252b5132 3258 }
1e915849
RS
3259 else
3260 insert_into_history (0, 1, ip);
252b5132
RH
3261
3262 /* We just output an insn, so the next one doesn't have a label. */
3263 mips_clear_insn_labels ();
252b5132
RH
3264}
3265
7d10b47d 3266/* Forget that there was any previous instruction or label. */
252b5132
RH
3267
3268static void
7d10b47d 3269mips_no_prev_insn (void)
252b5132 3270{
7d10b47d
RS
3271 prev_nop_frag = NULL;
3272 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
3273 mips_clear_insn_labels ();
3274}
3275
7d10b47d
RS
3276/* This function must be called before we emit something other than
3277 instructions. It is like mips_no_prev_insn except that it inserts
3278 any NOPS that might be needed by previous instructions. */
252b5132 3279
7d10b47d
RS
3280void
3281mips_emit_delays (void)
252b5132
RH
3282{
3283 if (! mips_opts.noreorder)
3284 {
71400594 3285 int nops = nops_for_insn (history, NULL);
252b5132
RH
3286 if (nops > 0)
3287 {
7d10b47d
RS
3288 while (nops-- > 0)
3289 add_fixed_insn (NOP_INSN);
3290 mips_move_labels ();
3291 }
3292 }
3293 mips_no_prev_insn ();
3294}
3295
3296/* Start a (possibly nested) noreorder block. */
3297
3298static void
3299start_noreorder (void)
3300{
3301 if (mips_opts.noreorder == 0)
3302 {
3303 unsigned int i;
3304 int nops;
3305
3306 /* None of the instructions before the .set noreorder can be moved. */
3307 for (i = 0; i < ARRAY_SIZE (history); i++)
3308 history[i].fixed_p = 1;
3309
3310 /* Insert any nops that might be needed between the .set noreorder
3311 block and the previous instructions. We will later remove any
3312 nops that turn out not to be needed. */
3313 nops = nops_for_insn (history, NULL);
3314 if (nops > 0)
3315 {
3316 if (mips_optimize != 0)
252b5132
RH
3317 {
3318 /* Record the frag which holds the nop instructions, so
3319 that we can remove them if we don't need them. */
3320 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
3321 prev_nop_frag = frag_now;
3322 prev_nop_frag_holds = nops;
3323 prev_nop_frag_required = 0;
3324 prev_nop_frag_since = 0;
3325 }
3326
3327 for (; nops > 0; --nops)
1e915849 3328 add_fixed_insn (NOP_INSN);
252b5132 3329
7d10b47d
RS
3330 /* Move on to a new frag, so that it is safe to simply
3331 decrease the size of prev_nop_frag. */
3332 frag_wane (frag_now);
3333 frag_new (0);
404a8071 3334 mips_move_labels ();
252b5132 3335 }
7d10b47d
RS
3336 mips16_mark_labels ();
3337 mips_clear_insn_labels ();
252b5132 3338 }
7d10b47d
RS
3339 mips_opts.noreorder++;
3340 mips_any_noreorder = 1;
3341}
252b5132 3342
7d10b47d 3343/* End a nested noreorder block. */
252b5132 3344
7d10b47d
RS
3345static void
3346end_noreorder (void)
3347{
3348 mips_opts.noreorder--;
3349 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
3350 {
3351 /* Commit to inserting prev_nop_frag_required nops and go back to
3352 handling nop insertion the .set reorder way. */
3353 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
3354 * (mips_opts.mips16 ? 2 : 4));
3355 insert_into_history (prev_nop_frag_since,
3356 prev_nop_frag_required, NOP_INSN);
3357 prev_nop_frag = NULL;
3358 }
252b5132
RH
3359}
3360
584892a6
RS
3361/* Set up global variables for the start of a new macro. */
3362
3363static void
3364macro_start (void)
3365{
3366 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
3367 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
47e39b9d 3368 && (history[0].insn_mo->pinfo
584892a6
RS
3369 & (INSN_UNCOND_BRANCH_DELAY
3370 | INSN_COND_BRANCH_DELAY
3371 | INSN_COND_BRANCH_LIKELY)) != 0);
3372}
3373
3374/* Given that a macro is longer than 4 bytes, return the appropriate warning
3375 for it. Return null if no warning is needed. SUBTYPE is a bitmask of
3376 RELAX_DELAY_SLOT and RELAX_NOMACRO. */
3377
3378static const char *
3379macro_warning (relax_substateT subtype)
3380{
3381 if (subtype & RELAX_DELAY_SLOT)
3382 return _("Macro instruction expanded into multiple instructions"
3383 " in a branch delay slot");
3384 else if (subtype & RELAX_NOMACRO)
3385 return _("Macro instruction expanded into multiple instructions");
3386 else
3387 return 0;
3388}
3389
3390/* Finish up a macro. Emit warnings as appropriate. */
3391
3392static void
3393macro_end (void)
3394{
3395 if (mips_macro_warning.sizes[0] > 4 || mips_macro_warning.sizes[1] > 4)
3396 {
3397 relax_substateT subtype;
3398
3399 /* Set up the relaxation warning flags. */
3400 subtype = 0;
3401 if (mips_macro_warning.sizes[1] > mips_macro_warning.sizes[0])
3402 subtype |= RELAX_SECOND_LONGER;
3403 if (mips_opts.warn_about_macros)
3404 subtype |= RELAX_NOMACRO;
3405 if (mips_macro_warning.delay_slot_p)
3406 subtype |= RELAX_DELAY_SLOT;
3407
3408 if (mips_macro_warning.sizes[0] > 4 && mips_macro_warning.sizes[1] > 4)
3409 {
3410 /* Either the macro has a single implementation or both
3411 implementations are longer than 4 bytes. Emit the
3412 warning now. */
3413 const char *msg = macro_warning (subtype);
3414 if (msg != 0)
3415 as_warn (msg);
3416 }
3417 else
3418 {
3419 /* One implementation might need a warning but the other
3420 definitely doesn't. */
3421 mips_macro_warning.first_frag->fr_subtype |= subtype;
3422 }
3423 }
3424}
3425
6e1304d8
RS
3426/* Read a macro's relocation codes from *ARGS and store them in *R.
3427 The first argument in *ARGS will be either the code for a single
3428 relocation or -1 followed by the three codes that make up a
3429 composite relocation. */
3430
3431static void
3432macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
3433{
3434 int i, next;
3435
3436 next = va_arg (*args, int);
3437 if (next >= 0)
3438 r[0] = (bfd_reloc_code_real_type) next;
3439 else
3440 for (i = 0; i < 3; i++)
3441 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
3442}
3443
252b5132
RH
3444/* Build an instruction created by a macro expansion. This is passed
3445 a pointer to the count of instructions created so far, an
3446 expression, the name of the instruction to build, an operand format
3447 string, and corresponding arguments. */
3448
252b5132 3449static void
67c0d1eb 3450macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 3451{
1e915849 3452 const struct mips_opcode *mo;
252b5132 3453 struct mips_cl_insn insn;
f6688943 3454 bfd_reloc_code_real_type r[3];
252b5132 3455 va_list args;
252b5132 3456
252b5132 3457 va_start (args, fmt);
252b5132 3458
252b5132
RH
3459 if (mips_opts.mips16)
3460 {
67c0d1eb 3461 mips16_macro_build (ep, name, fmt, args);
252b5132
RH
3462 va_end (args);
3463 return;
3464 }
3465
f6688943
TS
3466 r[0] = BFD_RELOC_UNUSED;
3467 r[1] = BFD_RELOC_UNUSED;
3468 r[2] = BFD_RELOC_UNUSED;
1e915849
RS
3469 mo = (struct mips_opcode *) hash_find (op_hash, name);
3470 assert (mo);
3471 assert (strcmp (name, mo->name) == 0);
3472
8b082fb1
TS
3473 while (1)
3474 {
3475 /* Search until we get a match for NAME. It is assumed here that
3476 macros will never generate MDMX, MIPS-3D, or MT instructions. */
3477 if (strcmp (fmt, mo->args) == 0
3478 && mo->pinfo != INSN_MACRO
037b32b9 3479 && is_opcode_valid (mo, TRUE))
8b082fb1
TS
3480 break;
3481
1e915849
RS
3482 ++mo;
3483 assert (mo->name);
3484 assert (strcmp (name, mo->name) == 0);
252b5132
RH
3485 }
3486
1e915849 3487 create_insn (&insn, mo);
252b5132
RH
3488 for (;;)
3489 {
3490 switch (*fmt++)
3491 {
3492 case '\0':
3493 break;
3494
3495 case ',':
3496 case '(':
3497 case ')':
3498 continue;
3499
5f74bc13
CD
3500 case '+':
3501 switch (*fmt++)
3502 {
3503 case 'A':
3504 case 'E':
bf12938e 3505 INSERT_OPERAND (SHAMT, insn, va_arg (args, int));
5f74bc13
CD
3506 continue;
3507
3508 case 'B':
3509 case 'F':
3510 /* Note that in the macro case, these arguments are already
3511 in MSB form. (When handling the instruction in the
3512 non-macro case, these arguments are sizes from which
3513 MSB values must be calculated.) */
bf12938e 3514 INSERT_OPERAND (INSMSB, insn, va_arg (args, int));
5f74bc13
CD
3515 continue;
3516
3517 case 'C':
3518 case 'G':
3519 case 'H':
3520 /* Note that in the macro case, these arguments are already
3521 in MSBD form. (When handling the instruction in the
3522 non-macro case, these arguments are sizes from which
3523 MSBD values must be calculated.) */
bf12938e 3524 INSERT_OPERAND (EXTMSBD, insn, va_arg (args, int));
5f74bc13
CD
3525 continue;
3526
dd3cbb7e
NC
3527 case 'Q':
3528 INSERT_OPERAND (SEQI, insn, va_arg (args, int));
3529 continue;
3530
5f74bc13
CD
3531 default:
3532 internalError ();
3533 }
3534 continue;
3535
8b082fb1
TS
3536 case '2':
3537 INSERT_OPERAND (BP, insn, va_arg (args, int));
3538 continue;
3539
252b5132
RH
3540 case 't':
3541 case 'w':
3542 case 'E':
bf12938e 3543 INSERT_OPERAND (RT, insn, va_arg (args, int));
252b5132
RH
3544 continue;
3545
3546 case 'c':
bf12938e 3547 INSERT_OPERAND (CODE, insn, va_arg (args, int));
38487616
TS
3548 continue;
3549
252b5132
RH
3550 case 'T':
3551 case 'W':
bf12938e 3552 INSERT_OPERAND (FT, insn, va_arg (args, int));
252b5132
RH
3553 continue;
3554
3555 case 'd':
3556 case 'G':
af7ee8bf 3557 case 'K':
bf12938e 3558 INSERT_OPERAND (RD, insn, va_arg (args, int));
252b5132
RH
3559 continue;
3560
4372b673
NC
3561 case 'U':
3562 {
3563 int tmp = va_arg (args, int);
3564
bf12938e
RS
3565 INSERT_OPERAND (RT, insn, tmp);
3566 INSERT_OPERAND (RD, insn, tmp);
beae10d5 3567 continue;
4372b673
NC
3568 }
3569
252b5132
RH
3570 case 'V':
3571 case 'S':
bf12938e 3572 INSERT_OPERAND (FS, insn, va_arg (args, int));
252b5132
RH
3573 continue;
3574
3575 case 'z':
3576 continue;
3577
3578 case '<':
bf12938e 3579 INSERT_OPERAND (SHAMT, insn, va_arg (args, int));
252b5132
RH
3580 continue;
3581
3582 case 'D':
bf12938e 3583 INSERT_OPERAND (FD, insn, va_arg (args, int));
252b5132
RH
3584 continue;
3585
3586 case 'B':
bf12938e 3587 INSERT_OPERAND (CODE20, insn, va_arg (args, int));
252b5132
RH
3588 continue;
3589
4372b673 3590 case 'J':
bf12938e 3591 INSERT_OPERAND (CODE19, insn, va_arg (args, int));
4372b673
NC
3592 continue;
3593
252b5132 3594 case 'q':
bf12938e 3595 INSERT_OPERAND (CODE2, insn, va_arg (args, int));
252b5132
RH
3596 continue;
3597
3598 case 'b':
3599 case 's':
3600 case 'r':
3601 case 'v':
bf12938e 3602 INSERT_OPERAND (RS, insn, va_arg (args, int));
252b5132
RH
3603 continue;
3604
3605 case 'i':
3606 case 'j':
3607 case 'o':
6e1304d8 3608 macro_read_relocs (&args, r);
cdf6fd85 3609 assert (*r == BFD_RELOC_GPREL16
f6688943
TS
3610 || *r == BFD_RELOC_MIPS_LITERAL
3611 || *r == BFD_RELOC_MIPS_HIGHER
3612 || *r == BFD_RELOC_HI16_S
3613 || *r == BFD_RELOC_LO16
3614 || *r == BFD_RELOC_MIPS_GOT16
3615 || *r == BFD_RELOC_MIPS_CALL16
438c16b8
TS
3616 || *r == BFD_RELOC_MIPS_GOT_DISP
3617 || *r == BFD_RELOC_MIPS_GOT_PAGE
3618 || *r == BFD_RELOC_MIPS_GOT_OFST
f6688943 3619 || *r == BFD_RELOC_MIPS_GOT_LO16
3e722fb5 3620 || *r == BFD_RELOC_MIPS_CALL_LO16);
252b5132
RH
3621 continue;
3622
3623 case 'u':
6e1304d8 3624 macro_read_relocs (&args, r);
252b5132
RH
3625 assert (ep != NULL
3626 && (ep->X_op == O_constant
3627 || (ep->X_op == O_symbol
f6688943
TS
3628 && (*r == BFD_RELOC_MIPS_HIGHEST
3629 || *r == BFD_RELOC_HI16_S
3630 || *r == BFD_RELOC_HI16
3631 || *r == BFD_RELOC_GPREL16
3632 || *r == BFD_RELOC_MIPS_GOT_HI16
3e722fb5 3633 || *r == BFD_RELOC_MIPS_CALL_HI16))));
252b5132
RH
3634 continue;
3635
3636 case 'p':
3637 assert (ep != NULL);
bad36eac 3638
252b5132
RH
3639 /*
3640 * This allows macro() to pass an immediate expression for
3641 * creating short branches without creating a symbol.
bad36eac
DJ
3642 *
3643 * We don't allow branch relaxation for these branches, as
3644 * they should only appear in ".set nomacro" anyway.
252b5132
RH
3645 */
3646 if (ep->X_op == O_constant)
3647 {
bad36eac
DJ
3648 if ((ep->X_add_number & 3) != 0)
3649 as_bad (_("branch to misaligned address (0x%lx)"),
3650 (unsigned long) ep->X_add_number);
3651 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
3652 as_bad (_("branch address range overflow (0x%lx)"),
3653 (unsigned long) ep->X_add_number);
252b5132
RH
3654 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3655 ep = NULL;
3656 }
3657 else
0b25d3e6 3658 *r = BFD_RELOC_16_PCREL_S2;
252b5132
RH
3659 continue;
3660
3661 case 'a':
3662 assert (ep != NULL);
f6688943 3663 *r = BFD_RELOC_MIPS_JMP;
252b5132
RH
3664 continue;
3665
3666 case 'C':
a9e24354 3667 INSERT_OPERAND (COPZ, insn, va_arg (args, unsigned long));
252b5132
RH
3668 continue;
3669
d43b4baf 3670 case 'k':
a9e24354 3671 INSERT_OPERAND (CACHE, insn, va_arg (args, unsigned long));
d43b4baf
TS
3672 continue;
3673
252b5132
RH
3674 default:
3675 internalError ();
3676 }
3677 break;
3678 }
3679 va_end (args);
f6688943 3680 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 3681
4d7206a2 3682 append_insn (&insn, ep, r);
252b5132
RH
3683}
3684
3685static void
67c0d1eb 3686mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
17a2f251 3687 va_list args)
252b5132 3688{
1e915849 3689 struct mips_opcode *mo;
252b5132 3690 struct mips_cl_insn insn;
f6688943
TS
3691 bfd_reloc_code_real_type r[3]
3692 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 3693
1e915849
RS
3694 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3695 assert (mo);
3696 assert (strcmp (name, mo->name) == 0);
252b5132 3697
1e915849 3698 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 3699 {
1e915849
RS
3700 ++mo;
3701 assert (mo->name);
3702 assert (strcmp (name, mo->name) == 0);
252b5132
RH
3703 }
3704
1e915849 3705 create_insn (&insn, mo);
252b5132
RH
3706 for (;;)
3707 {
3708 int c;
3709
3710 c = *fmt++;
3711 switch (c)
3712 {
3713 case '\0':
3714 break;
3715
3716 case ',':
3717 case '(':
3718 case ')':
3719 continue;
3720
3721 case 'y':
3722 case 'w':
bf12938e 3723 MIPS16_INSERT_OPERAND (RY, insn, va_arg (args, int));
252b5132
RH
3724 continue;
3725
3726 case 'x':
3727 case 'v':
bf12938e 3728 MIPS16_INSERT_OPERAND (RX, insn, va_arg (args, int));
252b5132
RH
3729 continue;
3730
3731 case 'z':
bf12938e 3732 MIPS16_INSERT_OPERAND (RZ, insn, va_arg (args, int));
252b5132
RH
3733 continue;
3734
3735 case 'Z':
bf12938e 3736 MIPS16_INSERT_OPERAND (MOVE32Z, insn, va_arg (args, int));
252b5132
RH
3737 continue;
3738
3739 case '0':
3740 case 'S':
3741 case 'P':
3742 case 'R':
3743 continue;
3744
3745 case 'X':
bf12938e 3746 MIPS16_INSERT_OPERAND (REGR32, insn, va_arg (args, int));
252b5132
RH
3747 continue;
3748
3749 case 'Y':
3750 {
3751 int regno;
3752
3753 regno = va_arg (args, int);
3754 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
a9e24354 3755 MIPS16_INSERT_OPERAND (REG32R, insn, regno);
252b5132
RH
3756 }
3757 continue;
3758
3759 case '<':
3760 case '>':
3761 case '4':
3762 case '5':
3763 case 'H':
3764 case 'W':
3765 case 'D':
3766 case 'j':
3767 case '8':
3768 case 'V':
3769 case 'C':
3770 case 'U':
3771 case 'k':
3772 case 'K':
3773 case 'p':
3774 case 'q':
3775 {
3776 assert (ep != NULL);
3777
3778 if (ep->X_op != O_constant)
874e8986 3779 *r = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
3780 else
3781 {
b34976b6
AM
3782 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3783 FALSE, &insn.insn_opcode, &insn.use_extend,
c4e7957c 3784 &insn.extend);
252b5132 3785 ep = NULL;
f6688943 3786 *r = BFD_RELOC_UNUSED;
252b5132
RH
3787 }
3788 }
3789 continue;
3790
3791 case '6':
bf12938e 3792 MIPS16_INSERT_OPERAND (IMM6, insn, va_arg (args, int));
252b5132
RH
3793 continue;
3794 }
3795
3796 break;
3797 }
3798
f6688943 3799 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 3800
4d7206a2 3801 append_insn (&insn, ep, r);
252b5132
RH
3802}
3803
2051e8c4
MR
3804/*
3805 * Sign-extend 32-bit mode constants that have bit 31 set and all
3806 * higher bits unset.
3807 */
9f872bbe 3808static void
2051e8c4
MR
3809normalize_constant_expr (expressionS *ex)
3810{
9ee2a2d4 3811 if (ex->X_op == O_constant
2051e8c4
MR
3812 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
3813 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3814 - 0x80000000);
3815}
3816
3817/*
3818 * Sign-extend 32-bit mode address offsets that have bit 31 set and
3819 * all higher bits unset.
3820 */
3821static void
3822normalize_address_expr (expressionS *ex)
3823{
3824 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
3825 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
3826 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
3827 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3828 - 0x80000000);
3829}
3830
438c16b8
TS
3831/*
3832 * Generate a "jalr" instruction with a relocation hint to the called
3833 * function. This occurs in NewABI PIC code.
3834 */
3835static void
67c0d1eb 3836macro_build_jalr (expressionS *ep)
438c16b8 3837{
685736be 3838 char *f = NULL;
b34976b6 3839
438c16b8 3840 if (HAVE_NEWABI)
f21f8242 3841 {
cc3d92a5 3842 frag_grow (8);
f21f8242
AO
3843 f = frag_more (0);
3844 }
67c0d1eb 3845 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
438c16b8 3846 if (HAVE_NEWABI)
f21f8242 3847 fix_new_exp (frag_now, f - frag_now->fr_literal,
a105a300 3848 4, ep, FALSE, BFD_RELOC_MIPS_JALR);
438c16b8
TS
3849}
3850
252b5132
RH
3851/*
3852 * Generate a "lui" instruction.
3853 */
3854static void
67c0d1eb 3855macro_build_lui (expressionS *ep, int regnum)
252b5132
RH
3856{
3857 expressionS high_expr;
1e915849 3858 const struct mips_opcode *mo;
252b5132 3859 struct mips_cl_insn insn;
f6688943
TS
3860 bfd_reloc_code_real_type r[3]
3861 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
5a38dc70
AM
3862 const char *name = "lui";
3863 const char *fmt = "t,u";
252b5132
RH
3864
3865 assert (! mips_opts.mips16);
3866
4d7206a2 3867 high_expr = *ep;
252b5132
RH
3868
3869 if (high_expr.X_op == O_constant)
3870 {
54f4ddb3 3871 /* We can compute the instruction now without a relocation entry. */
e7d556df
TS
3872 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3873 >> 16) & 0xffff;
f6688943 3874 *r = BFD_RELOC_UNUSED;
252b5132 3875 }
78e1bb40 3876 else
252b5132
RH
3877 {
3878 assert (ep->X_op == O_symbol);
bbe506e8
TS
3879 /* _gp_disp is a special case, used from s_cpload.
3880 __gnu_local_gp is used if mips_no_shared. */
252b5132 3881 assert (mips_pic == NO_PIC
78e1bb40 3882 || (! HAVE_NEWABI
aa6975fb
ILT
3883 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
3884 || (! mips_in_shared
bbe506e8
TS
3885 && strcmp (S_GET_NAME (ep->X_add_symbol),
3886 "__gnu_local_gp") == 0));
f6688943 3887 *r = BFD_RELOC_HI16_S;
252b5132
RH
3888 }
3889
1e915849
RS
3890 mo = hash_find (op_hash, name);
3891 assert (strcmp (name, mo->name) == 0);
3892 assert (strcmp (fmt, mo->args) == 0);
3893 create_insn (&insn, mo);
252b5132 3894
bf12938e
RS
3895 insn.insn_opcode = insn.insn_mo->match;
3896 INSERT_OPERAND (RT, insn, regnum);
f6688943 3897 if (*r == BFD_RELOC_UNUSED)
252b5132
RH
3898 {
3899 insn.insn_opcode |= high_expr.X_add_number;
4d7206a2 3900 append_insn (&insn, NULL, r);
252b5132
RH
3901 }
3902 else
4d7206a2 3903 append_insn (&insn, &high_expr, r);
252b5132
RH
3904}
3905
885add95
CD
3906/* Generate a sequence of instructions to do a load or store from a constant
3907 offset off of a base register (breg) into/from a target register (treg),
3908 using AT if necessary. */
3909static void
67c0d1eb
RS
3910macro_build_ldst_constoffset (expressionS *ep, const char *op,
3911 int treg, int breg, int dbl)
885add95
CD
3912{
3913 assert (ep->X_op == O_constant);
3914
256ab948 3915 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
3916 if (!dbl)
3917 normalize_constant_expr (ep);
256ab948 3918
67c1ffbe 3919 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 3920 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
3921 as_warn (_("operand overflow"));
3922
3923 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3924 {
3925 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 3926 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
3927 }
3928 else
3929 {
3930 /* 32-bit offset, need multiple instructions and AT, like:
3931 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
3932 addu $tempreg,$tempreg,$breg
3933 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
3934 to handle the complete offset. */
67c0d1eb
RS
3935 macro_build_lui (ep, AT);
3936 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
3937 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95 3938
741fe287 3939 if (!mips_opts.at)
8fc2e39e 3940 as_bad (_("Macro used $at after \".set noat\""));
885add95
CD
3941 }
3942}
3943
252b5132
RH
3944/* set_at()
3945 * Generates code to set the $at register to true (one)
3946 * if reg is less than the immediate expression.
3947 */
3948static void
67c0d1eb 3949set_at (int reg, int unsignedp)
252b5132
RH
3950{
3951 if (imm_expr.X_op == O_constant
3952 && imm_expr.X_add_number >= -0x8000
3953 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
3954 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
3955 AT, reg, BFD_RELOC_LO16);
252b5132
RH
3956 else
3957 {
67c0d1eb
RS
3958 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
3959 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
3960 }
3961}
3962
3963/* Warn if an expression is not a constant. */
3964
3965static void
17a2f251 3966check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
252b5132
RH
3967{
3968 if (ex->X_op == O_big)
3969 as_bad (_("unsupported large constant"));
3970 else if (ex->X_op != O_constant)
9ee2a2d4
MR
3971 as_bad (_("Instruction %s requires absolute expression"),
3972 ip->insn_mo->name);
13757d0c 3973
9ee2a2d4
MR
3974 if (HAVE_32BIT_GPRS)
3975 normalize_constant_expr (ex);
252b5132
RH
3976}
3977
3978/* Count the leading zeroes by performing a binary chop. This is a
3979 bulky bit of source, but performance is a LOT better for the
3980 majority of values than a simple loop to count the bits:
3981 for (lcnt = 0; (lcnt < 32); lcnt++)
3982 if ((v) & (1 << (31 - lcnt)))
3983 break;
3984 However it is not code size friendly, and the gain will drop a bit
3985 on certain cached systems.
3986*/
3987#define COUNT_TOP_ZEROES(v) \
3988 (((v) & ~0xffff) == 0 \
3989 ? ((v) & ~0xff) == 0 \
3990 ? ((v) & ~0xf) == 0 \
3991 ? ((v) & ~0x3) == 0 \
3992 ? ((v) & ~0x1) == 0 \
3993 ? !(v) \
3994 ? 32 \
3995 : 31 \
3996 : 30 \
3997 : ((v) & ~0x7) == 0 \
3998 ? 29 \
3999 : 28 \
4000 : ((v) & ~0x3f) == 0 \
4001 ? ((v) & ~0x1f) == 0 \
4002 ? 27 \
4003 : 26 \
4004 : ((v) & ~0x7f) == 0 \
4005 ? 25 \
4006 : 24 \
4007 : ((v) & ~0xfff) == 0 \
4008 ? ((v) & ~0x3ff) == 0 \
4009 ? ((v) & ~0x1ff) == 0 \
4010 ? 23 \
4011 : 22 \
4012 : ((v) & ~0x7ff) == 0 \
4013 ? 21 \
4014 : 20 \
4015 : ((v) & ~0x3fff) == 0 \
4016 ? ((v) & ~0x1fff) == 0 \
4017 ? 19 \
4018 : 18 \
4019 : ((v) & ~0x7fff) == 0 \
4020 ? 17 \
4021 : 16 \
4022 : ((v) & ~0xffffff) == 0 \
4023 ? ((v) & ~0xfffff) == 0 \
4024 ? ((v) & ~0x3ffff) == 0 \
4025 ? ((v) & ~0x1ffff) == 0 \
4026 ? 15 \
4027 : 14 \
4028 : ((v) & ~0x7ffff) == 0 \
4029 ? 13 \
4030 : 12 \
4031 : ((v) & ~0x3fffff) == 0 \
4032 ? ((v) & ~0x1fffff) == 0 \
4033 ? 11 \
4034 : 10 \
4035 : ((v) & ~0x7fffff) == 0 \
4036 ? 9 \
4037 : 8 \
4038 : ((v) & ~0xfffffff) == 0 \
4039 ? ((v) & ~0x3ffffff) == 0 \
4040 ? ((v) & ~0x1ffffff) == 0 \
4041 ? 7 \
4042 : 6 \
4043 : ((v) & ~0x7ffffff) == 0 \
4044 ? 5 \
4045 : 4 \
4046 : ((v) & ~0x3fffffff) == 0 \
4047 ? ((v) & ~0x1fffffff) == 0 \
4048 ? 3 \
4049 : 2 \
4050 : ((v) & ~0x7fffffff) == 0 \
4051 ? 1 \
4052 : 0)
4053
4054/* load_register()
67c1ffbe 4055 * This routine generates the least number of instructions necessary to load
252b5132
RH
4056 * an absolute expression value into a register.
4057 */
4058static void
67c0d1eb 4059load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
4060{
4061 int freg;
4062 expressionS hi32, lo32;
4063
4064 if (ep->X_op != O_big)
4065 {
4066 assert (ep->X_op == O_constant);
256ab948
TS
4067
4068 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
4069 if (!dbl)
4070 normalize_constant_expr (ep);
256ab948
TS
4071
4072 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
4073 {
4074 /* We can handle 16 bit signed values with an addiu to
4075 $zero. No need to ever use daddiu here, since $zero and
4076 the result are always correct in 32 bit mode. */
67c0d1eb 4077 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
4078 return;
4079 }
4080 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
4081 {
4082 /* We can handle 16 bit unsigned values with an ori to
4083 $zero. */
67c0d1eb 4084 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
4085 return;
4086 }
256ab948 4087 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
4088 {
4089 /* 32 bit values require an lui. */
67c0d1eb 4090 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_HI16);
252b5132 4091 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 4092 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
4093 return;
4094 }
4095 }
4096
4097 /* The value is larger than 32 bits. */
4098
2051e8c4 4099 if (!dbl || HAVE_32BIT_GPRS)
252b5132 4100 {
55e08f71
NC
4101 char value[32];
4102
4103 sprintf_vma (value, ep->X_add_number);
20e1fcfd 4104 as_bad (_("Number (0x%s) larger than 32 bits"), value);
67c0d1eb 4105 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
4106 return;
4107 }
4108
4109 if (ep->X_op != O_big)
4110 {
4111 hi32 = *ep;
4112 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
4113 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
4114 hi32.X_add_number &= 0xffffffff;
4115 lo32 = *ep;
4116 lo32.X_add_number &= 0xffffffff;
4117 }
4118 else
4119 {
4120 assert (ep->X_add_number > 2);
4121 if (ep->X_add_number == 3)
4122 generic_bignum[3] = 0;
4123 else if (ep->X_add_number > 4)
4124 as_bad (_("Number larger than 64 bits"));
4125 lo32.X_op = O_constant;
4126 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
4127 hi32.X_op = O_constant;
4128 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
4129 }
4130
4131 if (hi32.X_add_number == 0)
4132 freg = 0;
4133 else
4134 {
4135 int shift, bit;
4136 unsigned long hi, lo;
4137
956cd1d6 4138 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
4139 {
4140 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
4141 {
67c0d1eb 4142 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
4143 return;
4144 }
4145 if (lo32.X_add_number & 0x80000000)
4146 {
67c0d1eb 4147 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
252b5132 4148 if (lo32.X_add_number & 0xffff)
67c0d1eb 4149 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
4150 return;
4151 }
4152 }
252b5132
RH
4153
4154 /* Check for 16bit shifted constant. We know that hi32 is
4155 non-zero, so start the mask on the first bit of the hi32
4156 value. */
4157 shift = 17;
4158 do
beae10d5
KH
4159 {
4160 unsigned long himask, lomask;
4161
4162 if (shift < 32)
4163 {
4164 himask = 0xffff >> (32 - shift);
4165 lomask = (0xffff << shift) & 0xffffffff;
4166 }
4167 else
4168 {
4169 himask = 0xffff << (shift - 32);
4170 lomask = 0;
4171 }
4172 if ((hi32.X_add_number & ~(offsetT) himask) == 0
4173 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
4174 {
4175 expressionS tmp;
4176
4177 tmp.X_op = O_constant;
4178 if (shift < 32)
4179 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
4180 | (lo32.X_add_number >> shift));
4181 else
4182 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb
RS
4183 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
4184 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", "d,w,<",
4185 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
4186 return;
4187 }
f9419b05 4188 ++shift;
beae10d5
KH
4189 }
4190 while (shift <= (64 - 16));
252b5132
RH
4191
4192 /* Find the bit number of the lowest one bit, and store the
4193 shifted value in hi/lo. */
4194 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
4195 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
4196 if (lo != 0)
4197 {
4198 bit = 0;
4199 while ((lo & 1) == 0)
4200 {
4201 lo >>= 1;
4202 ++bit;
4203 }
4204 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
4205 hi >>= bit;
4206 }
4207 else
4208 {
4209 bit = 32;
4210 while ((hi & 1) == 0)
4211 {
4212 hi >>= 1;
4213 ++bit;
4214 }
4215 lo = hi;
4216 hi = 0;
4217 }
4218
4219 /* Optimize if the shifted value is a (power of 2) - 1. */
4220 if ((hi == 0 && ((lo + 1) & lo) == 0)
4221 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
4222 {
4223 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 4224 if (shift != 0)
beae10d5 4225 {
252b5132
RH
4226 expressionS tmp;
4227
4228 /* This instruction will set the register to be all
4229 ones. */
beae10d5
KH
4230 tmp.X_op = O_constant;
4231 tmp.X_add_number = (offsetT) -1;
67c0d1eb 4232 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
4233 if (bit != 0)
4234 {
4235 bit += shift;
67c0d1eb
RS
4236 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", "d,w,<",
4237 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 4238 }
67c0d1eb
RS
4239 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", "d,w,<",
4240 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
4241 return;
4242 }
4243 }
252b5132
RH
4244
4245 /* Sign extend hi32 before calling load_register, because we can
4246 generally get better code when we load a sign extended value. */
4247 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 4248 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 4249 load_register (reg, &hi32, 0);
252b5132
RH
4250 freg = reg;
4251 }
4252 if ((lo32.X_add_number & 0xffff0000) == 0)
4253 {
4254 if (freg != 0)
4255 {
67c0d1eb 4256 macro_build (NULL, "dsll32", "d,w,<", reg, freg, 0);
252b5132
RH
4257 freg = reg;
4258 }
4259 }
4260 else
4261 {
4262 expressionS mid16;
4263
956cd1d6 4264 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 4265 {
67c0d1eb
RS
4266 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
4267 macro_build (NULL, "dsrl32", "d,w,<", reg, reg, 0);
beae10d5
KH
4268 return;
4269 }
252b5132
RH
4270
4271 if (freg != 0)
4272 {
67c0d1eb 4273 macro_build (NULL, "dsll", "d,w,<", reg, freg, 16);
252b5132
RH
4274 freg = reg;
4275 }
4276 mid16 = lo32;
4277 mid16.X_add_number >>= 16;
67c0d1eb
RS
4278 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
4279 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
252b5132
RH
4280 freg = reg;
4281 }
4282 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 4283 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
4284}
4285
269137b2
TS
4286static inline void
4287load_delay_nop (void)
4288{
4289 if (!gpr_interlocks)
4290 macro_build (NULL, "nop", "");
4291}
4292
252b5132
RH
4293/* Load an address into a register. */
4294
4295static void
67c0d1eb 4296load_address (int reg, expressionS *ep, int *used_at)
252b5132 4297{
252b5132
RH
4298 if (ep->X_op != O_constant
4299 && ep->X_op != O_symbol)
4300 {
4301 as_bad (_("expression too complex"));
4302 ep->X_op = O_constant;
4303 }
4304
4305 if (ep->X_op == O_constant)
4306 {
67c0d1eb 4307 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
4308 return;
4309 }
4310
4311 if (mips_pic == NO_PIC)
4312 {
4313 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 4314 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
4315 Otherwise we want
4316 lui $reg,<sym> (BFD_RELOC_HI16_S)
4317 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 4318 If we have an addend, we always use the latter form.
76b3015f 4319
d6bc6245
TS
4320 With 64bit address space and a usable $at we want
4321 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4322 lui $at,<sym> (BFD_RELOC_HI16_S)
4323 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
4324 daddiu $at,<sym> (BFD_RELOC_LO16)
4325 dsll32 $reg,0
3a482fd5 4326 daddu $reg,$reg,$at
76b3015f 4327
c03099e6 4328 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
4329 on superscalar processors.
4330 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4331 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
4332 dsll $reg,16
4333 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
4334 dsll $reg,16
4335 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
4336
4337 For GP relative symbols in 64bit address space we can use
4338 the same sequence as in 32bit address space. */
aed1a261 4339 if (HAVE_64BIT_SYMBOLS)
d6bc6245 4340 {
6caf9ef4
TS
4341 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
4342 && !nopic_need_relax (ep->X_add_symbol, 1))
4343 {
4344 relax_start (ep->X_add_symbol);
4345 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
4346 mips_gp_register, BFD_RELOC_GPREL16);
4347 relax_switch ();
4348 }
d6bc6245 4349
741fe287 4350 if (*used_at == 0 && mips_opts.at)
d6bc6245 4351 {
67c0d1eb
RS
4352 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
4353 macro_build (ep, "lui", "t,u", AT, BFD_RELOC_HI16_S);
4354 macro_build (ep, "daddiu", "t,r,j", reg, reg,
4355 BFD_RELOC_MIPS_HIGHER);
4356 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
4357 macro_build (NULL, "dsll32", "d,w,<", reg, reg, 0);
4358 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
4359 *used_at = 1;
4360 }
4361 else
4362 {
67c0d1eb
RS
4363 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
4364 macro_build (ep, "daddiu", "t,r,j", reg, reg,
4365 BFD_RELOC_MIPS_HIGHER);
4366 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
4367 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
4368 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
4369 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 4370 }
6caf9ef4
TS
4371
4372 if (mips_relax.sequence)
4373 relax_end ();
d6bc6245 4374 }
252b5132
RH
4375 else
4376 {
d6bc6245 4377 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 4378 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 4379 {
4d7206a2 4380 relax_start (ep->X_add_symbol);
67c0d1eb 4381 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 4382 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 4383 relax_switch ();
d6bc6245 4384 }
67c0d1eb
RS
4385 macro_build_lui (ep, reg);
4386 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
4387 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
4388 if (mips_relax.sequence)
4389 relax_end ();
d6bc6245 4390 }
252b5132 4391 }
0a44bf69 4392 else if (!mips_big_got)
252b5132
RH
4393 {
4394 expressionS ex;
4395
4396 /* If this is a reference to an external symbol, we want
4397 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4398 Otherwise we want
4399 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4400 nop
4401 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
4402 If there is a constant, it must be added in after.
4403
ed6fb7bd 4404 If we have NewABI, we want
f5040a92
AO
4405 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
4406 unless we're referencing a global symbol with a non-zero
4407 offset, in which case cst must be added separately. */
ed6fb7bd
SC
4408 if (HAVE_NEWABI)
4409 {
f5040a92
AO
4410 if (ep->X_add_number)
4411 {
4d7206a2 4412 ex.X_add_number = ep->X_add_number;
f5040a92 4413 ep->X_add_number = 0;
4d7206a2 4414 relax_start (ep->X_add_symbol);
67c0d1eb
RS
4415 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4416 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
4417 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4418 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4419 ex.X_op = O_constant;
67c0d1eb 4420 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 4421 reg, reg, BFD_RELOC_LO16);
f5040a92 4422 ep->X_add_number = ex.X_add_number;
4d7206a2 4423 relax_switch ();
f5040a92 4424 }
67c0d1eb 4425 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 4426 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
4427 if (mips_relax.sequence)
4428 relax_end ();
ed6fb7bd
SC
4429 }
4430 else
4431 {
f5040a92
AO
4432 ex.X_add_number = ep->X_add_number;
4433 ep->X_add_number = 0;
67c0d1eb
RS
4434 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4435 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 4436 load_delay_nop ();
4d7206a2
RS
4437 relax_start (ep->X_add_symbol);
4438 relax_switch ();
67c0d1eb 4439 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 4440 BFD_RELOC_LO16);
4d7206a2 4441 relax_end ();
ed6fb7bd 4442
f5040a92
AO
4443 if (ex.X_add_number != 0)
4444 {
4445 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4446 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4447 ex.X_op = O_constant;
67c0d1eb 4448 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 4449 reg, reg, BFD_RELOC_LO16);
f5040a92 4450 }
252b5132
RH
4451 }
4452 }
0a44bf69 4453 else if (mips_big_got)
252b5132
RH
4454 {
4455 expressionS ex;
252b5132
RH
4456
4457 /* This is the large GOT case. If this is a reference to an
4458 external symbol, we want
4459 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4460 addu $reg,$reg,$gp
4461 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
4462
4463 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
4464 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4465 nop
4466 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 4467 If there is a constant, it must be added in after.
f5040a92
AO
4468
4469 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
4470 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
4471 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 4472 */
438c16b8
TS
4473 if (HAVE_NEWABI)
4474 {
4d7206a2 4475 ex.X_add_number = ep->X_add_number;
f5040a92 4476 ep->X_add_number = 0;
4d7206a2 4477 relax_start (ep->X_add_symbol);
67c0d1eb
RS
4478 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
4479 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
4480 reg, reg, mips_gp_register);
4481 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
4482 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
4483 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4484 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4485 else if (ex.X_add_number)
4486 {
4487 ex.X_op = O_constant;
67c0d1eb
RS
4488 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4489 BFD_RELOC_LO16);
f5040a92
AO
4490 }
4491
4492 ep->X_add_number = ex.X_add_number;
4d7206a2 4493 relax_switch ();
67c0d1eb 4494 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 4495 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
4496 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4497 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 4498 relax_end ();
438c16b8 4499 }
252b5132 4500 else
438c16b8 4501 {
f5040a92
AO
4502 ex.X_add_number = ep->X_add_number;
4503 ep->X_add_number = 0;
4d7206a2 4504 relax_start (ep->X_add_symbol);
67c0d1eb
RS
4505 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
4506 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
4507 reg, reg, mips_gp_register);
4508 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
4509 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
4510 relax_switch ();
4511 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
4512 {
4513 /* We need a nop before loading from $gp. This special
4514 check is required because the lui which starts the main
4515 instruction stream does not refer to $gp, and so will not
4516 insert the nop which may be required. */
67c0d1eb 4517 macro_build (NULL, "nop", "");
438c16b8 4518 }
67c0d1eb 4519 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 4520 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 4521 load_delay_nop ();
67c0d1eb 4522 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 4523 BFD_RELOC_LO16);
4d7206a2 4524 relax_end ();
438c16b8 4525
f5040a92
AO
4526 if (ex.X_add_number != 0)
4527 {
4528 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4529 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4530 ex.X_op = O_constant;
67c0d1eb
RS
4531 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4532 BFD_RELOC_LO16);
f5040a92 4533 }
252b5132
RH
4534 }
4535 }
252b5132
RH
4536 else
4537 abort ();
8fc2e39e 4538
741fe287 4539 if (!mips_opts.at && *used_at == 1)
8fc2e39e 4540 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
4541}
4542
ea1fb5dc
RS
4543/* Move the contents of register SOURCE into register DEST. */
4544
4545static void
67c0d1eb 4546move_register (int dest, int source)
ea1fb5dc 4547{
67c0d1eb
RS
4548 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
4549 dest, source, 0);
ea1fb5dc
RS
4550}
4551
4d7206a2 4552/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
4553 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
4554 The two alternatives are:
4d7206a2
RS
4555
4556 Global symbol Local sybmol
4557 ------------- ------------
4558 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
4559 ... ...
4560 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
4561
4562 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
4563 emits the second for a 16-bit offset or add_got_offset_hilo emits
4564 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
4565
4566static void
67c0d1eb 4567load_got_offset (int dest, expressionS *local)
4d7206a2
RS
4568{
4569 expressionS global;
4570
4571 global = *local;
4572 global.X_add_number = 0;
4573
4574 relax_start (local->X_add_symbol);
67c0d1eb
RS
4575 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4576 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 4577 relax_switch ();
67c0d1eb
RS
4578 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4579 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
4580 relax_end ();
4581}
4582
4583static void
67c0d1eb 4584add_got_offset (int dest, expressionS *local)
4d7206a2
RS
4585{
4586 expressionS global;
4587
4588 global.X_op = O_constant;
4589 global.X_op_symbol = NULL;
4590 global.X_add_symbol = NULL;
4591 global.X_add_number = local->X_add_number;
4592
4593 relax_start (local->X_add_symbol);
67c0d1eb 4594 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
4595 dest, dest, BFD_RELOC_LO16);
4596 relax_switch ();
67c0d1eb 4597 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
4598 relax_end ();
4599}
4600
f6a22291
MR
4601static void
4602add_got_offset_hilo (int dest, expressionS *local, int tmp)
4603{
4604 expressionS global;
4605 int hold_mips_optimize;
4606
4607 global.X_op = O_constant;
4608 global.X_op_symbol = NULL;
4609 global.X_add_symbol = NULL;
4610 global.X_add_number = local->X_add_number;
4611
4612 relax_start (local->X_add_symbol);
4613 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
4614 relax_switch ();
4615 /* Set mips_optimize around the lui instruction to avoid
4616 inserting an unnecessary nop after the lw. */
4617 hold_mips_optimize = mips_optimize;
4618 mips_optimize = 2;
4619 macro_build_lui (&global, tmp);
4620 mips_optimize = hold_mips_optimize;
4621 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
4622 relax_end ();
4623
4624 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
4625}
4626
252b5132
RH
4627/*
4628 * Build macros
4629 * This routine implements the seemingly endless macro or synthesized
4630 * instructions and addressing modes in the mips assembly language. Many
4631 * of these macros are simple and are similar to each other. These could
67c1ffbe 4632 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
4633 * this verbose method. Others are not simple macros but are more like
4634 * optimizing code generation.
4635 * One interesting optimization is when several store macros appear
67c1ffbe 4636 * consecutively that would load AT with the upper half of the same address.
252b5132
RH
4637 * The ensuing load upper instructions are ommited. This implies some kind
4638 * of global optimization. We currently only optimize within a single macro.
4639 * For many of the load and store macros if the address is specified as a
4640 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4641 * first load register 'at' with zero and use it as the base register. The
4642 * mips assembler simply uses register $zero. Just one tiny optimization
4643 * we're missing.
4644 */
4645static void
17a2f251 4646macro (struct mips_cl_insn *ip)
252b5132 4647{
741fe287
MR
4648 unsigned int treg, sreg, dreg, breg;
4649 unsigned int tempreg;
252b5132 4650 int mask;
43841e91 4651 int used_at = 0;
252b5132
RH
4652 expressionS expr1;
4653 const char *s;
4654 const char *s2;
4655 const char *fmt;
4656 int likely = 0;
4657 int dbl = 0;
4658 int coproc = 0;
4659 int lr = 0;
4660 int imm = 0;
1abe91b1 4661 int call = 0;
252b5132 4662 int off;
67c0d1eb 4663 offsetT maxnum;
252b5132 4664 bfd_reloc_code_real_type r;
252b5132
RH
4665 int hold_mips_optimize;
4666
4667 assert (! mips_opts.mips16);
4668
4669 treg = (ip->insn_opcode >> 16) & 0x1f;
4670 dreg = (ip->insn_opcode >> 11) & 0x1f;
4671 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4672 mask = ip->insn_mo->mask;
4673
4674 expr1.X_op = O_constant;
4675 expr1.X_op_symbol = NULL;
4676 expr1.X_add_symbol = NULL;
4677 expr1.X_add_number = 1;
4678
4679 switch (mask)
4680 {
4681 case M_DABS:
4682 dbl = 1;
4683 case M_ABS:
4684 /* bgez $a0,.+12
4685 move v0,$a0
4686 sub v0,$zero,$a0
4687 */
4688
7d10b47d 4689 start_noreorder ();
252b5132
RH
4690
4691 expr1.X_add_number = 8;
67c0d1eb 4692 macro_build (&expr1, "bgez", "s,p", sreg);
252b5132 4693 if (dreg == sreg)
67c0d1eb 4694 macro_build (NULL, "nop", "", 0);
252b5132 4695 else
67c0d1eb
RS
4696 move_register (dreg, sreg);
4697 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
252b5132 4698
7d10b47d 4699 end_noreorder ();
8fc2e39e 4700 break;
252b5132
RH
4701
4702 case M_ADD_I:
4703 s = "addi";
4704 s2 = "add";
4705 goto do_addi;
4706 case M_ADDU_I:
4707 s = "addiu";
4708 s2 = "addu";
4709 goto do_addi;
4710 case M_DADD_I:
4711 dbl = 1;
4712 s = "daddi";
4713 s2 = "dadd";
4714 goto do_addi;
4715 case M_DADDU_I:
4716 dbl = 1;
4717 s = "daddiu";
4718 s2 = "daddu";
4719 do_addi:
4720 if (imm_expr.X_op == O_constant
4721 && imm_expr.X_add_number >= -0x8000
4722 && imm_expr.X_add_number < 0x8000)
4723 {
67c0d1eb 4724 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
8fc2e39e 4725 break;
252b5132 4726 }
8fc2e39e 4727 used_at = 1;
67c0d1eb
RS
4728 load_register (AT, &imm_expr, dbl);
4729 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
4730 break;
4731
4732 case M_AND_I:
4733 s = "andi";
4734 s2 = "and";
4735 goto do_bit;
4736 case M_OR_I:
4737 s = "ori";
4738 s2 = "or";
4739 goto do_bit;
4740 case M_NOR_I:
4741 s = "";
4742 s2 = "nor";
4743 goto do_bit;
4744 case M_XOR_I:
4745 s = "xori";
4746 s2 = "xor";
4747 do_bit:
4748 if (imm_expr.X_op == O_constant
4749 && imm_expr.X_add_number >= 0
4750 && imm_expr.X_add_number < 0x10000)
4751 {
4752 if (mask != M_NOR_I)
67c0d1eb 4753 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
252b5132
RH
4754 else
4755 {
67c0d1eb
RS
4756 macro_build (&imm_expr, "ori", "t,r,i",
4757 treg, sreg, BFD_RELOC_LO16);
4758 macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
252b5132 4759 }
8fc2e39e 4760 break;
252b5132
RH
4761 }
4762
8fc2e39e 4763 used_at = 1;
67c0d1eb
RS
4764 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4765 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
4766 break;
4767
8b082fb1
TS
4768 case M_BALIGN:
4769 switch (imm_expr.X_add_number)
4770 {
4771 case 0:
4772 macro_build (NULL, "nop", "");
4773 break;
4774 case 2:
4775 macro_build (NULL, "packrl.ph", "d,s,t", treg, treg, sreg);
4776 break;
4777 default:
4778 macro_build (NULL, "balign", "t,s,2", treg, sreg,
4779 (int)imm_expr.X_add_number);
4780 break;
4781 }
4782 break;
4783
252b5132
RH
4784 case M_BEQ_I:
4785 s = "beq";
4786 goto beq_i;
4787 case M_BEQL_I:
4788 s = "beql";
4789 likely = 1;
4790 goto beq_i;
4791 case M_BNE_I:
4792 s = "bne";
4793 goto beq_i;
4794 case M_BNEL_I:
4795 s = "bnel";
4796 likely = 1;
4797 beq_i:
4798 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4799 {
67c0d1eb 4800 macro_build (&offset_expr, s, "s,t,p", sreg, 0);
8fc2e39e 4801 break;
252b5132 4802 }
8fc2e39e 4803 used_at = 1;
67c0d1eb
RS
4804 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4805 macro_build (&offset_expr, s, "s,t,p", sreg, AT);
252b5132
RH
4806 break;
4807
4808 case M_BGEL:
4809 likely = 1;
4810 case M_BGE:
4811 if (treg == 0)
4812 {
67c0d1eb 4813 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
8fc2e39e 4814 break;
252b5132
RH
4815 }
4816 if (sreg == 0)
4817 {
67c0d1eb 4818 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", treg);
8fc2e39e 4819 break;
252b5132 4820 }
8fc2e39e 4821 used_at = 1;
67c0d1eb
RS
4822 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4823 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4824 break;
4825
4826 case M_BGTL_I:
4827 likely = 1;
4828 case M_BGT_I:
4829 /* check for > max integer */
4830 maxnum = 0x7fffffff;
ca4e0257 4831 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4832 {
4833 maxnum <<= 16;
4834 maxnum |= 0xffff;
4835 maxnum <<= 16;
4836 maxnum |= 0xffff;
4837 }
4838 if (imm_expr.X_op == O_constant
4839 && imm_expr.X_add_number >= maxnum
ca4e0257 4840 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4841 {
4842 do_false:
4843 /* result is always false */
4844 if (! likely)
67c0d1eb 4845 macro_build (NULL, "nop", "", 0);
252b5132 4846 else
67c0d1eb 4847 macro_build (&offset_expr, "bnel", "s,t,p", 0, 0);
8fc2e39e 4848 break;
252b5132
RH
4849 }
4850 if (imm_expr.X_op != O_constant)
4851 as_bad (_("Unsupported large constant"));
f9419b05 4852 ++imm_expr.X_add_number;
252b5132
RH
4853 /* FALLTHROUGH */
4854 case M_BGE_I:
4855 case M_BGEL_I:
4856 if (mask == M_BGEL_I)
4857 likely = 1;
4858 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4859 {
67c0d1eb 4860 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
8fc2e39e 4861 break;
252b5132
RH
4862 }
4863 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4864 {
67c0d1eb 4865 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
8fc2e39e 4866 break;
252b5132
RH
4867 }
4868 maxnum = 0x7fffffff;
ca4e0257 4869 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4870 {
4871 maxnum <<= 16;
4872 maxnum |= 0xffff;
4873 maxnum <<= 16;
4874 maxnum |= 0xffff;
4875 }
4876 maxnum = - maxnum - 1;
4877 if (imm_expr.X_op == O_constant
4878 && imm_expr.X_add_number <= maxnum
ca4e0257 4879 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4880 {
4881 do_true:
4882 /* result is always true */
4883 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
67c0d1eb 4884 macro_build (&offset_expr, "b", "p");
8fc2e39e 4885 break;
252b5132 4886 }
8fc2e39e 4887 used_at = 1;
67c0d1eb
RS
4888 set_at (sreg, 0);
4889 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4890 break;
4891
4892 case M_BGEUL:
4893 likely = 1;
4894 case M_BGEU:
4895 if (treg == 0)
4896 goto do_true;
4897 if (sreg == 0)
4898 {
67c0d1eb 4899 macro_build (&offset_expr, likely ? "beql" : "beq",
17a2f251 4900 "s,t,p", 0, treg);
8fc2e39e 4901 break;
252b5132 4902 }
8fc2e39e 4903 used_at = 1;
67c0d1eb
RS
4904 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4905 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4906 break;
4907
4908 case M_BGTUL_I:
4909 likely = 1;
4910 case M_BGTU_I:
4911 if (sreg == 0
ca4e0257 4912 || (HAVE_32BIT_GPRS
252b5132 4913 && imm_expr.X_op == O_constant
956cd1d6 4914 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4915 goto do_false;
4916 if (imm_expr.X_op != O_constant)
4917 as_bad (_("Unsupported large constant"));
f9419b05 4918 ++imm_expr.X_add_number;
252b5132
RH
4919 /* FALLTHROUGH */
4920 case M_BGEU_I:
4921 case M_BGEUL_I:
4922 if (mask == M_BGEUL_I)
4923 likely = 1;
4924 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4925 goto do_true;
4926 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4927 {
67c0d1eb 4928 macro_build (&offset_expr, likely ? "bnel" : "bne",
17a2f251 4929 "s,t,p", sreg, 0);
8fc2e39e 4930 break;
252b5132 4931 }
8fc2e39e 4932 used_at = 1;
67c0d1eb
RS
4933 set_at (sreg, 1);
4934 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4935 break;
4936
4937 case M_BGTL:
4938 likely = 1;
4939 case M_BGT:
4940 if (treg == 0)
4941 {
67c0d1eb 4942 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
8fc2e39e 4943 break;
252b5132
RH
4944 }
4945 if (sreg == 0)
4946 {
67c0d1eb 4947 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", treg);
8fc2e39e 4948 break;
252b5132 4949 }
8fc2e39e 4950 used_at = 1;
67c0d1eb
RS
4951 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4952 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4953 break;
4954
4955 case M_BGTUL:
4956 likely = 1;
4957 case M_BGTU:
4958 if (treg == 0)
4959 {
67c0d1eb 4960 macro_build (&offset_expr, likely ? "bnel" : "bne",
17a2f251 4961 "s,t,p", sreg, 0);
8fc2e39e 4962 break;
252b5132
RH
4963 }
4964 if (sreg == 0)
4965 goto do_false;
8fc2e39e 4966 used_at = 1;
67c0d1eb
RS
4967 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4968 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4969 break;
4970
4971 case M_BLEL:
4972 likely = 1;
4973 case M_BLE:
4974 if (treg == 0)
4975 {
67c0d1eb 4976 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
8fc2e39e 4977 break;
252b5132
RH
4978 }
4979 if (sreg == 0)
4980 {
67c0d1eb 4981 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", treg);
8fc2e39e 4982 break;
252b5132 4983 }
8fc2e39e 4984 used_at = 1;
67c0d1eb
RS
4985 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4986 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4987 break;
4988
4989 case M_BLEL_I:
4990 likely = 1;
4991 case M_BLE_I:
4992 maxnum = 0x7fffffff;
ca4e0257 4993 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4994 {
4995 maxnum <<= 16;
4996 maxnum |= 0xffff;
4997 maxnum <<= 16;
4998 maxnum |= 0xffff;
4999 }
5000 if (imm_expr.X_op == O_constant
5001 && imm_expr.X_add_number >= maxnum
ca4e0257 5002 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
5003 goto do_true;
5004 if (imm_expr.X_op != O_constant)
5005 as_bad (_("Unsupported large constant"));
f9419b05 5006 ++imm_expr.X_add_number;
252b5132
RH
5007 /* FALLTHROUGH */
5008 case M_BLT_I:
5009 case M_BLTL_I:
5010 if (mask == M_BLTL_I)
5011 likely = 1;
5012 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
5013 {
67c0d1eb 5014 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
8fc2e39e 5015 break;
252b5132
RH
5016 }
5017 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
5018 {
67c0d1eb 5019 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
8fc2e39e 5020 break;
252b5132 5021 }
8fc2e39e 5022 used_at = 1;
67c0d1eb
RS
5023 set_at (sreg, 0);
5024 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
5025 break;
5026
5027 case M_BLEUL:
5028 likely = 1;
5029 case M_BLEU:
5030 if (treg == 0)
5031 {
67c0d1eb 5032 macro_build (&offset_expr, likely ? "beql" : "beq",
17a2f251 5033 "s,t,p", sreg, 0);
8fc2e39e 5034 break;
252b5132
RH
5035 }
5036 if (sreg == 0)
5037 goto do_true;
8fc2e39e 5038 used_at = 1;
67c0d1eb
RS
5039 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
5040 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
5041 break;
5042
5043 case M_BLEUL_I:
5044 likely = 1;
5045 case M_BLEU_I:
5046 if (sreg == 0
ca4e0257 5047 || (HAVE_32BIT_GPRS
252b5132 5048 && imm_expr.X_op == O_constant
956cd1d6 5049 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
5050 goto do_true;
5051 if (imm_expr.X_op != O_constant)
5052 as_bad (_("Unsupported large constant"));
f9419b05 5053 ++imm_expr.X_add_number;
252b5132
RH
5054 /* FALLTHROUGH */
5055 case M_BLTU_I:
5056 case M_BLTUL_I:
5057 if (mask == M_BLTUL_I)
5058 likely = 1;
5059 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
5060 goto do_false;
5061 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
5062 {
67c0d1eb 5063 macro_build (&offset_expr, likely ? "beql" : "beq",
252b5132 5064 "s,t,p", sreg, 0);
8fc2e39e 5065 break;
252b5132 5066 }
8fc2e39e 5067 used_at = 1;
67c0d1eb
RS
5068 set_at (sreg, 1);
5069 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
5070 break;
5071
5072 case M_BLTL:
5073 likely = 1;
5074 case M_BLT:
5075 if (treg == 0)
5076 {
67c0d1eb 5077 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
8fc2e39e 5078 break;
252b5132
RH
5079 }
5080 if (sreg == 0)
5081 {
67c0d1eb 5082 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", treg);
8fc2e39e 5083 break;
252b5132 5084 }
8fc2e39e 5085 used_at = 1;
67c0d1eb
RS
5086 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
5087 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
5088 break;
5089
5090 case M_BLTUL:
5091 likely = 1;
5092 case M_BLTU:
5093 if (treg == 0)
5094 goto do_false;
5095 if (sreg == 0)
5096 {
67c0d1eb 5097 macro_build (&offset_expr, likely ? "bnel" : "bne",
17a2f251 5098 "s,t,p", 0, treg);
8fc2e39e 5099 break;
252b5132 5100 }
8fc2e39e 5101 used_at = 1;
67c0d1eb
RS
5102 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
5103 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
5104 break;
5105
5f74bc13
CD
5106 case M_DEXT:
5107 {
5108 unsigned long pos;
5109 unsigned long size;
5110
5111 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
5112 {
5113 as_bad (_("Unsupported large constant"));
5114 pos = size = 1;
5115 }
5116 else
5117 {
5118 pos = (unsigned long) imm_expr.X_add_number;
5119 size = (unsigned long) imm2_expr.X_add_number;
5120 }
5121
5122 if (pos > 63)
5123 {
5124 as_bad (_("Improper position (%lu)"), pos);
5125 pos = 1;
5126 }
5127 if (size == 0 || size > 64
5128 || (pos + size - 1) > 63)
5129 {
5130 as_bad (_("Improper extract size (%lu, position %lu)"),
5131 size, pos);
5132 size = 1;
5133 }
5134
5135 if (size <= 32 && pos < 32)
5136 {
5137 s = "dext";
5138 fmt = "t,r,+A,+C";
5139 }
5140 else if (size <= 32)
5141 {
5142 s = "dextu";
5143 fmt = "t,r,+E,+H";
5144 }
5145 else
5146 {
5147 s = "dextm";
5148 fmt = "t,r,+A,+G";
5149 }
67c0d1eb 5150 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos, size - 1);
5f74bc13 5151 }
8fc2e39e 5152 break;
5f74bc13
CD
5153
5154 case M_DINS:
5155 {
5156 unsigned long pos;
5157 unsigned long size;
5158
5159 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
5160 {
5161 as_bad (_("Unsupported large constant"));
5162 pos = size = 1;
5163 }
5164 else
5165 {
5166 pos = (unsigned long) imm_expr.X_add_number;
5167 size = (unsigned long) imm2_expr.X_add_number;
5168 }
5169
5170 if (pos > 63)
5171 {
5172 as_bad (_("Improper position (%lu)"), pos);
5173 pos = 1;
5174 }
5175 if (size == 0 || size > 64
5176 || (pos + size - 1) > 63)
5177 {
5178 as_bad (_("Improper insert size (%lu, position %lu)"),
5179 size, pos);
5180 size = 1;
5181 }
5182
5183 if (pos < 32 && (pos + size - 1) < 32)
5184 {
5185 s = "dins";
5186 fmt = "t,r,+A,+B";
5187 }
5188 else if (pos >= 32)
5189 {
5190 s = "dinsu";
5191 fmt = "t,r,+E,+F";
5192 }
5193 else
5194 {
5195 s = "dinsm";
5196 fmt = "t,r,+A,+F";
5197 }
67c0d1eb
RS
5198 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos,
5199 pos + size - 1);
5f74bc13 5200 }
8fc2e39e 5201 break;
5f74bc13 5202
252b5132
RH
5203 case M_DDIV_3:
5204 dbl = 1;
5205 case M_DIV_3:
5206 s = "mflo";
5207 goto do_div3;
5208 case M_DREM_3:
5209 dbl = 1;
5210 case M_REM_3:
5211 s = "mfhi";
5212 do_div3:
5213 if (treg == 0)
5214 {
5215 as_warn (_("Divide by zero."));
5216 if (mips_trap)
67c0d1eb 5217 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
252b5132 5218 else
67c0d1eb 5219 macro_build (NULL, "break", "c", 7);
8fc2e39e 5220 break;
252b5132
RH
5221 }
5222
7d10b47d 5223 start_noreorder ();
252b5132
RH
5224 if (mips_trap)
5225 {
67c0d1eb
RS
5226 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
5227 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
252b5132
RH
5228 }
5229 else
5230 {
5231 expr1.X_add_number = 8;
67c0d1eb
RS
5232 macro_build (&expr1, "bne", "s,t,p", treg, 0);
5233 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
5234 macro_build (NULL, "break", "c", 7);
252b5132
RH
5235 }
5236 expr1.X_add_number = -1;
8fc2e39e 5237 used_at = 1;
f6a22291 5238 load_register (AT, &expr1, dbl);
252b5132 5239 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
67c0d1eb 5240 macro_build (&expr1, "bne", "s,t,p", treg, AT);
252b5132
RH
5241 if (dbl)
5242 {
5243 expr1.X_add_number = 1;
f6a22291 5244 load_register (AT, &expr1, dbl);
67c0d1eb 5245 macro_build (NULL, "dsll32", "d,w,<", AT, AT, 31);
252b5132
RH
5246 }
5247 else
5248 {
5249 expr1.X_add_number = 0x80000000;
67c0d1eb 5250 macro_build (&expr1, "lui", "t,u", AT, BFD_RELOC_HI16);
252b5132
RH
5251 }
5252 if (mips_trap)
5253 {
67c0d1eb 5254 macro_build (NULL, "teq", "s,t,q", sreg, AT, 6);
252b5132
RH
5255 /* We want to close the noreorder block as soon as possible, so
5256 that later insns are available for delay slot filling. */
7d10b47d 5257 end_noreorder ();
252b5132
RH
5258 }
5259 else
5260 {
5261 expr1.X_add_number = 8;
67c0d1eb
RS
5262 macro_build (&expr1, "bne", "s,t,p", sreg, AT);
5263 macro_build (NULL, "nop", "", 0);
252b5132
RH
5264
5265 /* We want to close the noreorder block as soon as possible, so
5266 that later insns are available for delay slot filling. */
7d10b47d 5267 end_noreorder ();
252b5132 5268
67c0d1eb 5269 macro_build (NULL, "break", "c", 6);
252b5132 5270 }
67c0d1eb 5271 macro_build (NULL, s, "d", dreg);
252b5132
RH
5272 break;
5273
5274 case M_DIV_3I:
5275 s = "div";
5276 s2 = "mflo";
5277 goto do_divi;
5278 case M_DIVU_3I:
5279 s = "divu";
5280 s2 = "mflo";
5281 goto do_divi;
5282 case M_REM_3I:
5283 s = "div";
5284 s2 = "mfhi";
5285 goto do_divi;
5286 case M_REMU_3I:
5287 s = "divu";
5288 s2 = "mfhi";
5289 goto do_divi;
5290 case M_DDIV_3I:
5291 dbl = 1;
5292 s = "ddiv";
5293 s2 = "mflo";
5294 goto do_divi;
5295 case M_DDIVU_3I:
5296 dbl = 1;
5297 s = "ddivu";
5298 s2 = "mflo";
5299 goto do_divi;
5300 case M_DREM_3I:
5301 dbl = 1;
5302 s = "ddiv";
5303 s2 = "mfhi";
5304 goto do_divi;
5305 case M_DREMU_3I:
5306 dbl = 1;
5307 s = "ddivu";
5308 s2 = "mfhi";
5309 do_divi:
5310 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
5311 {
5312 as_warn (_("Divide by zero."));
5313 if (mips_trap)
67c0d1eb 5314 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
252b5132 5315 else
67c0d1eb 5316 macro_build (NULL, "break", "c", 7);
8fc2e39e 5317 break;
252b5132
RH
5318 }
5319 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
5320 {
5321 if (strcmp (s2, "mflo") == 0)
67c0d1eb 5322 move_register (dreg, sreg);
252b5132 5323 else
67c0d1eb 5324 move_register (dreg, 0);
8fc2e39e 5325 break;
252b5132
RH
5326 }
5327 if (imm_expr.X_op == O_constant
5328 && imm_expr.X_add_number == -1
5329 && s[strlen (s) - 1] != 'u')
5330 {
5331 if (strcmp (s2, "mflo") == 0)
5332 {
67c0d1eb 5333 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
252b5132
RH
5334 }
5335 else
67c0d1eb 5336 move_register (dreg, 0);
8fc2e39e 5337 break;
252b5132
RH
5338 }
5339
8fc2e39e 5340 used_at = 1;
67c0d1eb
RS
5341 load_register (AT, &imm_expr, dbl);
5342 macro_build (NULL, s, "z,s,t", sreg, AT);
5343 macro_build (NULL, s2, "d", dreg);
252b5132
RH
5344 break;
5345
5346 case M_DIVU_3:
5347 s = "divu";
5348 s2 = "mflo";
5349 goto do_divu3;
5350 case M_REMU_3:
5351 s = "divu";
5352 s2 = "mfhi";
5353 goto do_divu3;
5354 case M_DDIVU_3:
5355 s = "ddivu";
5356 s2 = "mflo";
5357 goto do_divu3;
5358 case M_DREMU_3:
5359 s = "ddivu";
5360 s2 = "mfhi";
5361 do_divu3:
7d10b47d 5362 start_noreorder ();
252b5132
RH
5363 if (mips_trap)
5364 {
67c0d1eb
RS
5365 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
5366 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
5367 /* We want to close the noreorder block as soon as possible, so
5368 that later insns are available for delay slot filling. */
7d10b47d 5369 end_noreorder ();
252b5132
RH
5370 }
5371 else
5372 {
5373 expr1.X_add_number = 8;
67c0d1eb
RS
5374 macro_build (&expr1, "bne", "s,t,p", treg, 0);
5375 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
5376
5377 /* We want to close the noreorder block as soon as possible, so
5378 that later insns are available for delay slot filling. */
7d10b47d 5379 end_noreorder ();
67c0d1eb 5380 macro_build (NULL, "break", "c", 7);
252b5132 5381 }
67c0d1eb 5382 macro_build (NULL, s2, "d", dreg);
8fc2e39e 5383 break;
252b5132 5384
1abe91b1
MR
5385 case M_DLCA_AB:
5386 dbl = 1;
5387 case M_LCA_AB:
5388 call = 1;
5389 goto do_la;
252b5132
RH
5390 case M_DLA_AB:
5391 dbl = 1;
5392 case M_LA_AB:
1abe91b1 5393 do_la:
252b5132
RH
5394 /* Load the address of a symbol into a register. If breg is not
5395 zero, we then add a base register to it. */
5396
3bec30a8
TS
5397 if (dbl && HAVE_32BIT_GPRS)
5398 as_warn (_("dla used to load 32-bit register"));
5399
c90bbe5b 5400 if (! dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
5401 as_warn (_("la used to load 64-bit address"));
5402
0c11417f
MR
5403 if (offset_expr.X_op == O_constant
5404 && offset_expr.X_add_number >= -0x8000
5405 && offset_expr.X_add_number < 0x8000)
5406 {
aed1a261 5407 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
17a2f251 5408 "t,r,j", treg, sreg, BFD_RELOC_LO16);
8fc2e39e 5409 break;
0c11417f
MR
5410 }
5411
741fe287 5412 if (mips_opts.at && (treg == breg))
afdbd6d0
CD
5413 {
5414 tempreg = AT;
5415 used_at = 1;
5416 }
5417 else
5418 {
5419 tempreg = treg;
afdbd6d0
CD
5420 }
5421
252b5132
RH
5422 if (offset_expr.X_op != O_symbol
5423 && offset_expr.X_op != O_constant)
5424 {
5425 as_bad (_("expression too complex"));
5426 offset_expr.X_op = O_constant;
5427 }
5428
252b5132 5429 if (offset_expr.X_op == O_constant)
aed1a261 5430 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
5431 else if (mips_pic == NO_PIC)
5432 {
d6bc6245 5433 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 5434 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
5435 Otherwise we want
5436 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5437 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5438 If we have a constant, we need two instructions anyhow,
d6bc6245 5439 so we may as well always use the latter form.
76b3015f 5440
6caf9ef4
TS
5441 With 64bit address space and a usable $at we want
5442 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5443 lui $at,<sym> (BFD_RELOC_HI16_S)
5444 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5445 daddiu $at,<sym> (BFD_RELOC_LO16)
5446 dsll32 $tempreg,0
5447 daddu $tempreg,$tempreg,$at
5448
5449 If $at is already in use, we use a path which is suboptimal
5450 on superscalar processors.
5451 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5452 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5453 dsll $tempreg,16
5454 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5455 dsll $tempreg,16
5456 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
5457
5458 For GP relative symbols in 64bit address space we can use
5459 the same sequence as in 32bit address space. */
aed1a261 5460 if (HAVE_64BIT_SYMBOLS)
252b5132 5461 {
6caf9ef4
TS
5462 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5463 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5464 {
5465 relax_start (offset_expr.X_add_symbol);
5466 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5467 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
5468 relax_switch ();
5469 }
d6bc6245 5470
741fe287 5471 if (used_at == 0 && mips_opts.at)
98d3f06f 5472 {
67c0d1eb 5473 macro_build (&offset_expr, "lui", "t,u",
17a2f251 5474 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 5475 macro_build (&offset_expr, "lui", "t,u",
17a2f251 5476 AT, BFD_RELOC_HI16_S);
67c0d1eb 5477 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 5478 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 5479 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 5480 AT, AT, BFD_RELOC_LO16);
67c0d1eb
RS
5481 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
5482 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
5483 used_at = 1;
5484 }
5485 else
5486 {
67c0d1eb 5487 macro_build (&offset_expr, "lui", "t,u",
17a2f251 5488 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 5489 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 5490 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb
RS
5491 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5492 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 5493 tempreg, tempreg, BFD_RELOC_HI16_S);
67c0d1eb
RS
5494 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5495 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 5496 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 5497 }
6caf9ef4
TS
5498
5499 if (mips_relax.sequence)
5500 relax_end ();
98d3f06f
KH
5501 }
5502 else
5503 {
5504 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 5505 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 5506 {
4d7206a2 5507 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5508 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5509 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 5510 relax_switch ();
98d3f06f 5511 }
6943caf0
ILT
5512 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
5513 as_bad (_("offset too large"));
67c0d1eb
RS
5514 macro_build_lui (&offset_expr, tempreg);
5515 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5516 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
5517 if (mips_relax.sequence)
5518 relax_end ();
98d3f06f 5519 }
252b5132 5520 }
0a44bf69 5521 else if (!mips_big_got && !HAVE_NEWABI)
252b5132 5522 {
9117d219
NC
5523 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5524
252b5132
RH
5525 /* If this is a reference to an external symbol, and there
5526 is no constant, we want
5527 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 5528 or for lca or if tempreg is PIC_CALL_REG
9117d219 5529 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
5530 For a local symbol, we want
5531 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5532 nop
5533 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5534
5535 If we have a small constant, and this is a reference to
5536 an external symbol, we want
5537 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5538 nop
5539 addiu $tempreg,$tempreg,<constant>
5540 For a local symbol, we want the same instruction
5541 sequence, but we output a BFD_RELOC_LO16 reloc on the
5542 addiu instruction.
5543
5544 If we have a large constant, and this is a reference to
5545 an external symbol, we want
5546 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5547 lui $at,<hiconstant>
5548 addiu $at,$at,<loconstant>
5549 addu $tempreg,$tempreg,$at
5550 For a local symbol, we want the same instruction
5551 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 5552 addiu instruction.
ed6fb7bd
SC
5553 */
5554
4d7206a2 5555 if (offset_expr.X_add_number == 0)
252b5132 5556 {
0a44bf69
RS
5557 if (mips_pic == SVR4_PIC
5558 && breg == 0
5559 && (call || tempreg == PIC_CALL_REG))
4d7206a2
RS
5560 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5561
5562 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5563 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5564 lw_reloc_type, mips_gp_register);
4d7206a2 5565 if (breg != 0)
252b5132
RH
5566 {
5567 /* We're going to put in an addu instruction using
5568 tempreg, so we may as well insert the nop right
5569 now. */
269137b2 5570 load_delay_nop ();
252b5132 5571 }
4d7206a2 5572 relax_switch ();
67c0d1eb
RS
5573 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5574 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 5575 load_delay_nop ();
67c0d1eb
RS
5576 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5577 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 5578 relax_end ();
252b5132
RH
5579 /* FIXME: If breg == 0, and the next instruction uses
5580 $tempreg, then if this variant case is used an extra
5581 nop will be generated. */
5582 }
4d7206a2
RS
5583 else if (offset_expr.X_add_number >= -0x8000
5584 && offset_expr.X_add_number < 0x8000)
252b5132 5585 {
67c0d1eb 5586 load_got_offset (tempreg, &offset_expr);
269137b2 5587 load_delay_nop ();
67c0d1eb 5588 add_got_offset (tempreg, &offset_expr);
252b5132
RH
5589 }
5590 else
5591 {
4d7206a2
RS
5592 expr1.X_add_number = offset_expr.X_add_number;
5593 offset_expr.X_add_number =
5594 ((offset_expr.X_add_number + 0x8000) & 0xffff) - 0x8000;
67c0d1eb 5595 load_got_offset (tempreg, &offset_expr);
f6a22291 5596 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
5597 /* If we are going to add in a base register, and the
5598 target register and the base register are the same,
5599 then we are using AT as a temporary register. Since
5600 we want to load the constant into AT, we add our
5601 current AT (from the global offset table) and the
5602 register into the register now, and pretend we were
5603 not using a base register. */
67c0d1eb 5604 if (breg == treg)
252b5132 5605 {
269137b2 5606 load_delay_nop ();
67c0d1eb 5607 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5608 treg, AT, breg);
252b5132
RH
5609 breg = 0;
5610 tempreg = treg;
252b5132 5611 }
f6a22291 5612 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
5613 used_at = 1;
5614 }
5615 }
0a44bf69 5616 else if (!mips_big_got && HAVE_NEWABI)
f5040a92 5617 {
67c0d1eb 5618 int add_breg_early = 0;
f5040a92
AO
5619
5620 /* If this is a reference to an external, and there is no
5621 constant, or local symbol (*), with or without a
5622 constant, we want
5623 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 5624 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
5625 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5626
5627 If we have a small constant, and this is a reference to
5628 an external symbol, we want
5629 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5630 addiu $tempreg,$tempreg,<constant>
5631
5632 If we have a large constant, and this is a reference to
5633 an external symbol, we want
5634 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5635 lui $at,<hiconstant>
5636 addiu $at,$at,<loconstant>
5637 addu $tempreg,$tempreg,$at
5638
5639 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5640 local symbols, even though it introduces an additional
5641 instruction. */
5642
f5040a92
AO
5643 if (offset_expr.X_add_number)
5644 {
4d7206a2 5645 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
5646 offset_expr.X_add_number = 0;
5647
4d7206a2 5648 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5649 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5650 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
5651
5652 if (expr1.X_add_number >= -0x8000
5653 && expr1.X_add_number < 0x8000)
5654 {
67c0d1eb
RS
5655 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5656 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 5657 }
ecd13cd3 5658 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92
AO
5659 {
5660 int dreg;
5661
5662 /* If we are going to add in a base register, and the
5663 target register and the base register are the same,
5664 then we are using AT as a temporary register. Since
5665 we want to load the constant into AT, we add our
5666 current AT (from the global offset table) and the
5667 register into the register now, and pretend we were
5668 not using a base register. */
5669 if (breg != treg)
5670 dreg = tempreg;
5671 else
5672 {
5673 assert (tempreg == AT);
67c0d1eb
RS
5674 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5675 treg, AT, breg);
f5040a92 5676 dreg = treg;
67c0d1eb 5677 add_breg_early = 1;
f5040a92
AO
5678 }
5679
f6a22291 5680 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 5681 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5682 dreg, dreg, AT);
f5040a92 5683
f5040a92
AO
5684 used_at = 1;
5685 }
5686 else
5687 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5688
4d7206a2 5689 relax_switch ();
f5040a92
AO
5690 offset_expr.X_add_number = expr1.X_add_number;
5691
67c0d1eb
RS
5692 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5693 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5694 if (add_breg_early)
f5040a92 5695 {
67c0d1eb 5696 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
f899b4b8 5697 treg, tempreg, breg);
f5040a92
AO
5698 breg = 0;
5699 tempreg = treg;
5700 }
4d7206a2 5701 relax_end ();
f5040a92 5702 }
4d7206a2 5703 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 5704 {
4d7206a2 5705 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5706 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5707 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 5708 relax_switch ();
67c0d1eb
RS
5709 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5710 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 5711 relax_end ();
f5040a92 5712 }
4d7206a2 5713 else
f5040a92 5714 {
67c0d1eb
RS
5715 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5716 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
5717 }
5718 }
0a44bf69 5719 else if (mips_big_got && !HAVE_NEWABI)
252b5132 5720 {
67c0d1eb 5721 int gpdelay;
9117d219
NC
5722 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5723 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 5724 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
5725
5726 /* This is the large GOT case. If this is a reference to an
5727 external symbol, and there is no constant, we want
5728 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5729 addu $tempreg,$tempreg,$gp
5730 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 5731 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
5732 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5733 addu $tempreg,$tempreg,$gp
5734 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
5735 For a local symbol, we want
5736 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5737 nop
5738 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5739
5740 If we have a small constant, and this is a reference to
5741 an external symbol, we want
5742 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5743 addu $tempreg,$tempreg,$gp
5744 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5745 nop
5746 addiu $tempreg,$tempreg,<constant>
5747 For a local symbol, we want
5748 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5749 nop
5750 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5751
5752 If we have a large constant, and this is a reference to
5753 an external symbol, we want
5754 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5755 addu $tempreg,$tempreg,$gp
5756 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5757 lui $at,<hiconstant>
5758 addiu $at,$at,<loconstant>
5759 addu $tempreg,$tempreg,$at
5760 For a local symbol, we want
5761 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5762 lui $at,<hiconstant>
5763 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5764 addu $tempreg,$tempreg,$at
f5040a92 5765 */
438c16b8 5766
252b5132
RH
5767 expr1.X_add_number = offset_expr.X_add_number;
5768 offset_expr.X_add_number = 0;
4d7206a2 5769 relax_start (offset_expr.X_add_symbol);
67c0d1eb 5770 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
5771 if (expr1.X_add_number == 0 && breg == 0
5772 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
5773 {
5774 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5775 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5776 }
67c0d1eb
RS
5777 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5778 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5779 tempreg, tempreg, mips_gp_register);
67c0d1eb 5780 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 5781 tempreg, lw_reloc_type, tempreg);
252b5132
RH
5782 if (expr1.X_add_number == 0)
5783 {
67c0d1eb 5784 if (breg != 0)
252b5132
RH
5785 {
5786 /* We're going to put in an addu instruction using
5787 tempreg, so we may as well insert the nop right
5788 now. */
269137b2 5789 load_delay_nop ();
252b5132 5790 }
252b5132
RH
5791 }
5792 else if (expr1.X_add_number >= -0x8000
5793 && expr1.X_add_number < 0x8000)
5794 {
269137b2 5795 load_delay_nop ();
67c0d1eb 5796 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5797 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
5798 }
5799 else
5800 {
67c0d1eb 5801 int dreg;
252b5132
RH
5802
5803 /* If we are going to add in a base register, and the
5804 target register and the base register are the same,
5805 then we are using AT as a temporary register. Since
5806 we want to load the constant into AT, we add our
5807 current AT (from the global offset table) and the
5808 register into the register now, and pretend we were
5809 not using a base register. */
5810 if (breg != treg)
67c0d1eb 5811 dreg = tempreg;
252b5132
RH
5812 else
5813 {
5814 assert (tempreg == AT);
269137b2 5815 load_delay_nop ();
67c0d1eb 5816 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5817 treg, AT, breg);
252b5132 5818 dreg = treg;
252b5132
RH
5819 }
5820
f6a22291 5821 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 5822 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 5823
252b5132
RH
5824 used_at = 1;
5825 }
4d7206a2
RS
5826 offset_expr.X_add_number =
5827 ((expr1.X_add_number + 0x8000) & 0xffff) - 0x8000;
5828 relax_switch ();
252b5132 5829
67c0d1eb 5830 if (gpdelay)
252b5132
RH
5831 {
5832 /* This is needed because this instruction uses $gp, but
f5040a92 5833 the first instruction on the main stream does not. */
67c0d1eb 5834 macro_build (NULL, "nop", "");
252b5132 5835 }
ed6fb7bd 5836
67c0d1eb
RS
5837 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5838 local_reloc_type, mips_gp_register);
f5040a92 5839 if (expr1.X_add_number >= -0x8000
252b5132
RH
5840 && expr1.X_add_number < 0x8000)
5841 {
269137b2 5842 load_delay_nop ();
67c0d1eb
RS
5843 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5844 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 5845 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
5846 register, the external symbol case ended with a load,
5847 so if the symbol turns out to not be external, and
5848 the next instruction uses tempreg, an unnecessary nop
5849 will be inserted. */
252b5132
RH
5850 }
5851 else
5852 {
5853 if (breg == treg)
5854 {
5855 /* We must add in the base register now, as in the
f5040a92 5856 external symbol case. */
252b5132 5857 assert (tempreg == AT);
269137b2 5858 load_delay_nop ();
67c0d1eb 5859 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5860 treg, AT, breg);
252b5132
RH
5861 tempreg = treg;
5862 /* We set breg to 0 because we have arranged to add
f5040a92 5863 it in in both cases. */
252b5132
RH
5864 breg = 0;
5865 }
5866
67c0d1eb
RS
5867 macro_build_lui (&expr1, AT);
5868 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5869 AT, AT, BFD_RELOC_LO16);
67c0d1eb 5870 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5871 tempreg, tempreg, AT);
8fc2e39e 5872 used_at = 1;
252b5132 5873 }
4d7206a2 5874 relax_end ();
252b5132 5875 }
0a44bf69 5876 else if (mips_big_got && HAVE_NEWABI)
f5040a92 5877 {
f5040a92
AO
5878 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5879 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 5880 int add_breg_early = 0;
f5040a92
AO
5881
5882 /* This is the large GOT case. If this is a reference to an
5883 external symbol, and there is no constant, we want
5884 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5885 add $tempreg,$tempreg,$gp
5886 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 5887 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
5888 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5889 add $tempreg,$tempreg,$gp
5890 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5891
5892 If we have a small constant, and this is a reference to
5893 an external symbol, we want
5894 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5895 add $tempreg,$tempreg,$gp
5896 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5897 addi $tempreg,$tempreg,<constant>
5898
5899 If we have a large constant, and this is a reference to
5900 an external symbol, we want
5901 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5902 addu $tempreg,$tempreg,$gp
5903 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5904 lui $at,<hiconstant>
5905 addi $at,$at,<loconstant>
5906 add $tempreg,$tempreg,$at
5907
5908 If we have NewABI, and we know it's a local symbol, we want
5909 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5910 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5911 otherwise we have to resort to GOT_HI16/GOT_LO16. */
5912
4d7206a2 5913 relax_start (offset_expr.X_add_symbol);
f5040a92 5914
4d7206a2 5915 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
5916 offset_expr.X_add_number = 0;
5917
1abe91b1
MR
5918 if (expr1.X_add_number == 0 && breg == 0
5919 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
5920 {
5921 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5922 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5923 }
67c0d1eb
RS
5924 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5925 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5926 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
5927 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5928 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
5929
5930 if (expr1.X_add_number == 0)
4d7206a2 5931 ;
f5040a92
AO
5932 else if (expr1.X_add_number >= -0x8000
5933 && expr1.X_add_number < 0x8000)
5934 {
67c0d1eb 5935 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5936 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 5937 }
ecd13cd3 5938 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92
AO
5939 {
5940 int dreg;
5941
5942 /* If we are going to add in a base register, and the
5943 target register and the base register are the same,
5944 then we are using AT as a temporary register. Since
5945 we want to load the constant into AT, we add our
5946 current AT (from the global offset table) and the
5947 register into the register now, and pretend we were
5948 not using a base register. */
5949 if (breg != treg)
5950 dreg = tempreg;
5951 else
5952 {
5953 assert (tempreg == AT);
67c0d1eb 5954 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5955 treg, AT, breg);
f5040a92 5956 dreg = treg;
67c0d1eb 5957 add_breg_early = 1;
f5040a92
AO
5958 }
5959
f6a22291 5960 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 5961 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 5962
f5040a92
AO
5963 used_at = 1;
5964 }
5965 else
5966 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5967
4d7206a2 5968 relax_switch ();
f5040a92 5969 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
5970 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5971 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5972 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
5973 tempreg, BFD_RELOC_MIPS_GOT_OFST);
5974 if (add_breg_early)
f5040a92 5975 {
67c0d1eb 5976 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5977 treg, tempreg, breg);
f5040a92
AO
5978 breg = 0;
5979 tempreg = treg;
5980 }
4d7206a2 5981 relax_end ();
f5040a92 5982 }
252b5132
RH
5983 else
5984 abort ();
5985
5986 if (breg != 0)
aed1a261 5987 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
252b5132
RH
5988 break;
5989
5990 case M_J_A:
5991 /* The j instruction may not be used in PIC code, since it
5992 requires an absolute address. We convert it to a b
5993 instruction. */
5994 if (mips_pic == NO_PIC)
67c0d1eb 5995 macro_build (&offset_expr, "j", "a");
252b5132 5996 else
67c0d1eb 5997 macro_build (&offset_expr, "b", "p");
8fc2e39e 5998 break;
252b5132
RH
5999
6000 /* The jal instructions must be handled as macros because when
6001 generating PIC code they expand to multi-instruction
6002 sequences. Normally they are simple instructions. */
6003 case M_JAL_1:
6004 dreg = RA;
6005 /* Fall through. */
6006 case M_JAL_2:
3e722fb5 6007 if (mips_pic == NO_PIC)
67c0d1eb 6008 macro_build (NULL, "jalr", "d,s", dreg, sreg);
0a44bf69 6009 else
252b5132
RH
6010 {
6011 if (sreg != PIC_CALL_REG)
6012 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 6013
67c0d1eb 6014 macro_build (NULL, "jalr", "d,s", dreg, sreg);
0a44bf69 6015 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
252b5132 6016 {
6478892d
TS
6017 if (mips_cprestore_offset < 0)
6018 as_warn (_("No .cprestore pseudo-op used in PIC code"));
6019 else
6020 {
7a621144
DJ
6021 if (! mips_frame_reg_valid)
6022 {
6023 as_warn (_("No .frame pseudo-op used in PIC code"));
6024 /* Quiet this warning. */
6025 mips_frame_reg_valid = 1;
6026 }
6027 if (! mips_cprestore_valid)
6028 {
6029 as_warn (_("No .cprestore pseudo-op used in PIC code"));
6030 /* Quiet this warning. */
6031 mips_cprestore_valid = 1;
6032 }
6478892d 6033 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 6034 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 6035 mips_gp_register,
256ab948
TS
6036 mips_frame_reg,
6037 HAVE_64BIT_ADDRESSES);
6478892d 6038 }
252b5132
RH
6039 }
6040 }
252b5132 6041
8fc2e39e 6042 break;
252b5132
RH
6043
6044 case M_JAL_A:
6045 if (mips_pic == NO_PIC)
67c0d1eb 6046 macro_build (&offset_expr, "jal", "a");
252b5132
RH
6047 else if (mips_pic == SVR4_PIC)
6048 {
6049 /* If this is a reference to an external symbol, and we are
6050 using a small GOT, we want
6051 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
6052 nop
f9419b05 6053 jalr $ra,$25
252b5132
RH
6054 nop
6055 lw $gp,cprestore($sp)
6056 The cprestore value is set using the .cprestore
6057 pseudo-op. If we are using a big GOT, we want
6058 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
6059 addu $25,$25,$gp
6060 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
6061 nop
f9419b05 6062 jalr $ra,$25
252b5132
RH
6063 nop
6064 lw $gp,cprestore($sp)
6065 If the symbol is not external, we want
6066 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6067 nop
6068 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 6069 jalr $ra,$25
252b5132 6070 nop
438c16b8 6071 lw $gp,cprestore($sp)
f5040a92
AO
6072
6073 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
6074 sequences above, minus nops, unless the symbol is local,
6075 which enables us to use GOT_PAGE/GOT_OFST (big got) or
6076 GOT_DISP. */
438c16b8 6077 if (HAVE_NEWABI)
252b5132 6078 {
f5040a92
AO
6079 if (! mips_big_got)
6080 {
4d7206a2 6081 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
6082 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6083 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 6084 mips_gp_register);
4d7206a2 6085 relax_switch ();
67c0d1eb
RS
6086 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6087 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
6088 mips_gp_register);
6089 relax_end ();
f5040a92
AO
6090 }
6091 else
6092 {
4d7206a2 6093 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
6094 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
6095 BFD_RELOC_MIPS_CALL_HI16);
6096 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
6097 PIC_CALL_REG, mips_gp_register);
6098 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6099 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
6100 PIC_CALL_REG);
4d7206a2 6101 relax_switch ();
67c0d1eb
RS
6102 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6103 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
6104 mips_gp_register);
6105 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
6106 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 6107 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 6108 relax_end ();
f5040a92 6109 }
684022ea 6110
67c0d1eb 6111 macro_build_jalr (&offset_expr);
252b5132
RH
6112 }
6113 else
6114 {
4d7206a2 6115 relax_start (offset_expr.X_add_symbol);
438c16b8
TS
6116 if (! mips_big_got)
6117 {
67c0d1eb
RS
6118 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6119 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 6120 mips_gp_register);
269137b2 6121 load_delay_nop ();
4d7206a2 6122 relax_switch ();
438c16b8 6123 }
252b5132 6124 else
252b5132 6125 {
67c0d1eb
RS
6126 int gpdelay;
6127
6128 gpdelay = reg_needs_delay (mips_gp_register);
6129 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
6130 BFD_RELOC_MIPS_CALL_HI16);
6131 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
6132 PIC_CALL_REG, mips_gp_register);
6133 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6134 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
6135 PIC_CALL_REG);
269137b2 6136 load_delay_nop ();
4d7206a2 6137 relax_switch ();
67c0d1eb
RS
6138 if (gpdelay)
6139 macro_build (NULL, "nop", "");
252b5132 6140 }
67c0d1eb
RS
6141 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6142 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 6143 mips_gp_register);
269137b2 6144 load_delay_nop ();
67c0d1eb
RS
6145 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
6146 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 6147 relax_end ();
67c0d1eb 6148 macro_build_jalr (&offset_expr);
438c16b8 6149
6478892d
TS
6150 if (mips_cprestore_offset < 0)
6151 as_warn (_("No .cprestore pseudo-op used in PIC code"));
6152 else
6153 {
7a621144
DJ
6154 if (! mips_frame_reg_valid)
6155 {
6156 as_warn (_("No .frame pseudo-op used in PIC code"));
6157 /* Quiet this warning. */
6158 mips_frame_reg_valid = 1;
6159 }
6160 if (! mips_cprestore_valid)
6161 {
6162 as_warn (_("No .cprestore pseudo-op used in PIC code"));
6163 /* Quiet this warning. */
6164 mips_cprestore_valid = 1;
6165 }
6478892d 6166 if (mips_opts.noreorder)
67c0d1eb 6167 macro_build (NULL, "nop", "");
6478892d 6168 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 6169 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 6170 mips_gp_register,
256ab948
TS
6171 mips_frame_reg,
6172 HAVE_64BIT_ADDRESSES);
6478892d 6173 }
252b5132
RH
6174 }
6175 }
0a44bf69
RS
6176 else if (mips_pic == VXWORKS_PIC)
6177 as_bad (_("Non-PIC jump used in PIC library"));
252b5132
RH
6178 else
6179 abort ();
6180
8fc2e39e 6181 break;
252b5132
RH
6182
6183 case M_LB_AB:
6184 s = "lb";
6185 goto ld;
6186 case M_LBU_AB:
6187 s = "lbu";
6188 goto ld;
6189 case M_LH_AB:
6190 s = "lh";
6191 goto ld;
6192 case M_LHU_AB:
6193 s = "lhu";
6194 goto ld;
6195 case M_LW_AB:
6196 s = "lw";
6197 goto ld;
6198 case M_LWC0_AB:
6199 s = "lwc0";
bdaaa2e1 6200 /* Itbl support may require additional care here. */
252b5132
RH
6201 coproc = 1;
6202 goto ld;
6203 case M_LWC1_AB:
6204 s = "lwc1";
bdaaa2e1 6205 /* Itbl support may require additional care here. */
252b5132
RH
6206 coproc = 1;
6207 goto ld;
6208 case M_LWC2_AB:
6209 s = "lwc2";
bdaaa2e1 6210 /* Itbl support may require additional care here. */
252b5132
RH
6211 coproc = 1;
6212 goto ld;
6213 case M_LWC3_AB:
6214 s = "lwc3";
bdaaa2e1 6215 /* Itbl support may require additional care here. */
252b5132
RH
6216 coproc = 1;
6217 goto ld;
6218 case M_LWL_AB:
6219 s = "lwl";
6220 lr = 1;
6221 goto ld;
6222 case M_LWR_AB:
6223 s = "lwr";
6224 lr = 1;
6225 goto ld;
6226 case M_LDC1_AB:
252b5132 6227 s = "ldc1";
bdaaa2e1 6228 /* Itbl support may require additional care here. */
252b5132
RH
6229 coproc = 1;
6230 goto ld;
6231 case M_LDC2_AB:
6232 s = "ldc2";
bdaaa2e1 6233 /* Itbl support may require additional care here. */
252b5132
RH
6234 coproc = 1;
6235 goto ld;
6236 case M_LDC3_AB:
6237 s = "ldc3";
bdaaa2e1 6238 /* Itbl support may require additional care here. */
252b5132
RH
6239 coproc = 1;
6240 goto ld;
6241 case M_LDL_AB:
6242 s = "ldl";
6243 lr = 1;
6244 goto ld;
6245 case M_LDR_AB:
6246 s = "ldr";
6247 lr = 1;
6248 goto ld;
6249 case M_LL_AB:
6250 s = "ll";
6251 goto ld;
6252 case M_LLD_AB:
6253 s = "lld";
6254 goto ld;
6255 case M_LWU_AB:
6256 s = "lwu";
6257 ld:
8fc2e39e 6258 if (breg == treg || coproc || lr)
252b5132
RH
6259 {
6260 tempreg = AT;
6261 used_at = 1;
6262 }
6263 else
6264 {
6265 tempreg = treg;
252b5132
RH
6266 }
6267 goto ld_st;
6268 case M_SB_AB:
6269 s = "sb";
6270 goto st;
6271 case M_SH_AB:
6272 s = "sh";
6273 goto st;
6274 case M_SW_AB:
6275 s = "sw";
6276 goto st;
6277 case M_SWC0_AB:
6278 s = "swc0";
bdaaa2e1 6279 /* Itbl support may require additional care here. */
252b5132
RH
6280 coproc = 1;
6281 goto st;
6282 case M_SWC1_AB:
6283 s = "swc1";
bdaaa2e1 6284 /* Itbl support may require additional care here. */
252b5132
RH
6285 coproc = 1;
6286 goto st;
6287 case M_SWC2_AB:
6288 s = "swc2";
bdaaa2e1 6289 /* Itbl support may require additional care here. */
252b5132
RH
6290 coproc = 1;
6291 goto st;
6292 case M_SWC3_AB:
6293 s = "swc3";
bdaaa2e1 6294 /* Itbl support may require additional care here. */
252b5132
RH
6295 coproc = 1;
6296 goto st;
6297 case M_SWL_AB:
6298 s = "swl";
6299 goto st;
6300 case M_SWR_AB:
6301 s = "swr";
6302 goto st;
6303 case M_SC_AB:
6304 s = "sc";
6305 goto st;
6306 case M_SCD_AB:
6307 s = "scd";
6308 goto st;
d43b4baf
TS
6309 case M_CACHE_AB:
6310 s = "cache";
6311 goto st;
252b5132 6312 case M_SDC1_AB:
252b5132
RH
6313 s = "sdc1";
6314 coproc = 1;
bdaaa2e1 6315 /* Itbl support may require additional care here. */
252b5132
RH
6316 goto st;
6317 case M_SDC2_AB:
6318 s = "sdc2";
bdaaa2e1 6319 /* Itbl support may require additional care here. */
252b5132
RH
6320 coproc = 1;
6321 goto st;
6322 case M_SDC3_AB:
6323 s = "sdc3";
bdaaa2e1 6324 /* Itbl support may require additional care here. */
252b5132
RH
6325 coproc = 1;
6326 goto st;
6327 case M_SDL_AB:
6328 s = "sdl";
6329 goto st;
6330 case M_SDR_AB:
6331 s = "sdr";
6332 st:
8fc2e39e
TS
6333 tempreg = AT;
6334 used_at = 1;
252b5132 6335 ld_st:
b19e8a9b
AN
6336 if (coproc
6337 && NO_ISA_COP (mips_opts.arch)
6338 && (ip->insn_mo->pinfo2 & (INSN2_M_FP_S | INSN2_M_FP_D)) == 0)
6339 {
6340 as_bad (_("opcode not supported on this processor: %s"),
6341 mips_cpu_info_from_arch (mips_opts.arch)->name);
6342 break;
6343 }
6344
bdaaa2e1 6345 /* Itbl support may require additional care here. */
252b5132
RH
6346 if (mask == M_LWC1_AB
6347 || mask == M_SWC1_AB
6348 || mask == M_LDC1_AB
6349 || mask == M_SDC1_AB
6350 || mask == M_L_DAB
6351 || mask == M_S_DAB)
6352 fmt = "T,o(b)";
d43b4baf
TS
6353 else if (mask == M_CACHE_AB)
6354 fmt = "k,o(b)";
252b5132
RH
6355 else if (coproc)
6356 fmt = "E,o(b)";
6357 else
6358 fmt = "t,o(b)";
6359
6360 if (offset_expr.X_op != O_constant
6361 && offset_expr.X_op != O_symbol)
6362 {
6363 as_bad (_("expression too complex"));
6364 offset_expr.X_op = O_constant;
6365 }
6366
2051e8c4
MR
6367 if (HAVE_32BIT_ADDRESSES
6368 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
6369 {
6370 char value [32];
6371
6372 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 6373 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 6374 }
2051e8c4 6375
252b5132
RH
6376 /* A constant expression in PIC code can be handled just as it
6377 is in non PIC code. */
aed1a261
RS
6378 if (offset_expr.X_op == O_constant)
6379 {
aed1a261
RS
6380 expr1.X_add_number = ((offset_expr.X_add_number + 0x8000)
6381 & ~(bfd_vma) 0xffff);
2051e8c4 6382 normalize_address_expr (&expr1);
aed1a261
RS
6383 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
6384 if (breg != 0)
6385 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6386 tempreg, tempreg, breg);
6387 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6388 }
6389 else if (mips_pic == NO_PIC)
252b5132
RH
6390 {
6391 /* If this is a reference to a GP relative symbol, and there
6392 is no base register, we want
cdf6fd85 6393 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6394 Otherwise, if there is no base register, we want
6395 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
6396 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6397 If we have a constant, we need two instructions anyhow,
6398 so we always use the latter form.
6399
6400 If we have a base register, and this is a reference to a
6401 GP relative symbol, we want
6402 addu $tempreg,$breg,$gp
cdf6fd85 6403 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
6404 Otherwise we want
6405 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
6406 addu $tempreg,$tempreg,$breg
6407 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 6408 With a constant we always use the latter case.
76b3015f 6409
d6bc6245
TS
6410 With 64bit address space and no base register and $at usable,
6411 we want
6412 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6413 lui $at,<sym> (BFD_RELOC_HI16_S)
6414 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6415 dsll32 $tempreg,0
6416 daddu $tempreg,$at
6417 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6418 If we have a base register, we want
6419 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6420 lui $at,<sym> (BFD_RELOC_HI16_S)
6421 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6422 daddu $at,$breg
6423 dsll32 $tempreg,0
6424 daddu $tempreg,$at
6425 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6426
6427 Without $at we can't generate the optimal path for superscalar
6428 processors here since this would require two temporary registers.
6429 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6430 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6431 dsll $tempreg,16
6432 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
6433 dsll $tempreg,16
6434 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6435 If we have a base register, we want
6436 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6437 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6438 dsll $tempreg,16
6439 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
6440 dsll $tempreg,16
6441 daddu $tempreg,$tempreg,$breg
6442 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 6443
6caf9ef4 6444 For GP relative symbols in 64bit address space we can use
aed1a261
RS
6445 the same sequence as in 32bit address space. */
6446 if (HAVE_64BIT_SYMBOLS)
d6bc6245 6447 {
aed1a261 6448 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
6449 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
6450 {
6451 relax_start (offset_expr.X_add_symbol);
6452 if (breg == 0)
6453 {
6454 macro_build (&offset_expr, s, fmt, treg,
6455 BFD_RELOC_GPREL16, mips_gp_register);
6456 }
6457 else
6458 {
6459 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6460 tempreg, breg, mips_gp_register);
6461 macro_build (&offset_expr, s, fmt, treg,
6462 BFD_RELOC_GPREL16, tempreg);
6463 }
6464 relax_switch ();
6465 }
d6bc6245 6466
741fe287 6467 if (used_at == 0 && mips_opts.at)
d6bc6245 6468 {
67c0d1eb
RS
6469 macro_build (&offset_expr, "lui", "t,u", tempreg,
6470 BFD_RELOC_MIPS_HIGHEST);
6471 macro_build (&offset_expr, "lui", "t,u", AT,
6472 BFD_RELOC_HI16_S);
6473 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6474 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 6475 if (breg != 0)
67c0d1eb
RS
6476 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
6477 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
6478 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
6479 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
6480 tempreg);
d6bc6245
TS
6481 used_at = 1;
6482 }
6483 else
6484 {
67c0d1eb
RS
6485 macro_build (&offset_expr, "lui", "t,u", tempreg,
6486 BFD_RELOC_MIPS_HIGHEST);
6487 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6488 tempreg, BFD_RELOC_MIPS_HIGHER);
6489 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
6490 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6491 tempreg, BFD_RELOC_HI16_S);
6492 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
d6bc6245 6493 if (breg != 0)
67c0d1eb 6494 macro_build (NULL, "daddu", "d,v,t",
17a2f251 6495 tempreg, tempreg, breg);
67c0d1eb 6496 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6497 BFD_RELOC_LO16, tempreg);
d6bc6245 6498 }
6caf9ef4
TS
6499
6500 if (mips_relax.sequence)
6501 relax_end ();
8fc2e39e 6502 break;
d6bc6245 6503 }
256ab948 6504
252b5132
RH
6505 if (breg == 0)
6506 {
67c0d1eb 6507 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 6508 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 6509 {
4d7206a2 6510 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
6511 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
6512 mips_gp_register);
4d7206a2 6513 relax_switch ();
252b5132 6514 }
67c0d1eb
RS
6515 macro_build_lui (&offset_expr, tempreg);
6516 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6517 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
6518 if (mips_relax.sequence)
6519 relax_end ();
252b5132
RH
6520 }
6521 else
6522 {
67c0d1eb 6523 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 6524 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 6525 {
4d7206a2 6526 relax_start (offset_expr.X_add_symbol);
67c0d1eb 6527 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6528 tempreg, breg, mips_gp_register);
67c0d1eb 6529 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6530 BFD_RELOC_GPREL16, tempreg);
4d7206a2 6531 relax_switch ();
252b5132 6532 }
67c0d1eb
RS
6533 macro_build_lui (&offset_expr, tempreg);
6534 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6535 tempreg, tempreg, breg);
67c0d1eb 6536 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6537 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
6538 if (mips_relax.sequence)
6539 relax_end ();
252b5132
RH
6540 }
6541 }
0a44bf69 6542 else if (!mips_big_got)
252b5132 6543 {
ed6fb7bd 6544 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 6545
252b5132
RH
6546 /* If this is a reference to an external symbol, we want
6547 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6548 nop
6549 <op> $treg,0($tempreg)
6550 Otherwise we want
6551 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6552 nop
6553 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6554 <op> $treg,0($tempreg)
f5040a92
AO
6555
6556 For NewABI, we want
6557 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6558 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
6559
252b5132
RH
6560 If there is a base register, we add it to $tempreg before
6561 the <op>. If there is a constant, we stick it in the
6562 <op> instruction. We don't handle constants larger than
6563 16 bits, because we have no way to load the upper 16 bits
6564 (actually, we could handle them for the subset of cases
6565 in which we are not using $at). */
6566 assert (offset_expr.X_op == O_symbol);
f5040a92
AO
6567 if (HAVE_NEWABI)
6568 {
67c0d1eb
RS
6569 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6570 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 6571 if (breg != 0)
67c0d1eb 6572 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6573 tempreg, tempreg, breg);
67c0d1eb 6574 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6575 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
6576 break;
6577 }
252b5132
RH
6578 expr1.X_add_number = offset_expr.X_add_number;
6579 offset_expr.X_add_number = 0;
6580 if (expr1.X_add_number < -0x8000
6581 || expr1.X_add_number >= 0x8000)
6582 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
6583 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6584 lw_reloc_type, mips_gp_register);
269137b2 6585 load_delay_nop ();
4d7206a2
RS
6586 relax_start (offset_expr.X_add_symbol);
6587 relax_switch ();
67c0d1eb
RS
6588 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6589 tempreg, BFD_RELOC_LO16);
4d7206a2 6590 relax_end ();
252b5132 6591 if (breg != 0)
67c0d1eb 6592 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6593 tempreg, tempreg, breg);
67c0d1eb 6594 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 6595 }
0a44bf69 6596 else if (mips_big_got && !HAVE_NEWABI)
252b5132 6597 {
67c0d1eb 6598 int gpdelay;
252b5132
RH
6599
6600 /* If this is a reference to an external symbol, we want
6601 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6602 addu $tempreg,$tempreg,$gp
6603 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6604 <op> $treg,0($tempreg)
6605 Otherwise we want
6606 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6607 nop
6608 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6609 <op> $treg,0($tempreg)
6610 If there is a base register, we add it to $tempreg before
6611 the <op>. If there is a constant, we stick it in the
6612 <op> instruction. We don't handle constants larger than
6613 16 bits, because we have no way to load the upper 16 bits
6614 (actually, we could handle them for the subset of cases
f5040a92 6615 in which we are not using $at). */
252b5132
RH
6616 assert (offset_expr.X_op == O_symbol);
6617 expr1.X_add_number = offset_expr.X_add_number;
6618 offset_expr.X_add_number = 0;
6619 if (expr1.X_add_number < -0x8000
6620 || expr1.X_add_number >= 0x8000)
6621 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 6622 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 6623 relax_start (offset_expr.X_add_symbol);
67c0d1eb 6624 macro_build (&offset_expr, "lui", "t,u", tempreg,
17a2f251 6625 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
6626 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6627 mips_gp_register);
6628 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6629 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 6630 relax_switch ();
67c0d1eb
RS
6631 if (gpdelay)
6632 macro_build (NULL, "nop", "");
6633 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6634 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 6635 load_delay_nop ();
67c0d1eb
RS
6636 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6637 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
6638 relax_end ();
6639
252b5132 6640 if (breg != 0)
67c0d1eb 6641 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6642 tempreg, tempreg, breg);
67c0d1eb 6643 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 6644 }
0a44bf69 6645 else if (mips_big_got && HAVE_NEWABI)
f5040a92 6646 {
f5040a92
AO
6647 /* If this is a reference to an external symbol, we want
6648 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6649 add $tempreg,$tempreg,$gp
6650 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6651 <op> $treg,<ofst>($tempreg)
6652 Otherwise, for local symbols, we want:
6653 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6654 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
6655 assert (offset_expr.X_op == O_symbol);
4d7206a2 6656 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
6657 offset_expr.X_add_number = 0;
6658 if (expr1.X_add_number < -0x8000
6659 || expr1.X_add_number >= 0x8000)
6660 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 6661 relax_start (offset_expr.X_add_symbol);
67c0d1eb 6662 macro_build (&offset_expr, "lui", "t,u", tempreg,
17a2f251 6663 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
6664 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6665 mips_gp_register);
6666 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6667 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 6668 if (breg != 0)
67c0d1eb 6669 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6670 tempreg, tempreg, breg);
67c0d1eb 6671 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
684022ea 6672
4d7206a2 6673 relax_switch ();
f5040a92 6674 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
6675 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6676 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 6677 if (breg != 0)
67c0d1eb 6678 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6679 tempreg, tempreg, breg);
67c0d1eb 6680 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6681 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 6682 relax_end ();
f5040a92 6683 }
252b5132
RH
6684 else
6685 abort ();
6686
252b5132
RH
6687 break;
6688
6689 case M_LI:
6690 case M_LI_S:
67c0d1eb 6691 load_register (treg, &imm_expr, 0);
8fc2e39e 6692 break;
252b5132
RH
6693
6694 case M_DLI:
67c0d1eb 6695 load_register (treg, &imm_expr, 1);
8fc2e39e 6696 break;
252b5132
RH
6697
6698 case M_LI_SS:
6699 if (imm_expr.X_op == O_constant)
6700 {
8fc2e39e 6701 used_at = 1;
67c0d1eb
RS
6702 load_register (AT, &imm_expr, 0);
6703 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
6704 break;
6705 }
6706 else
6707 {
6708 assert (offset_expr.X_op == O_symbol
6709 && strcmp (segment_name (S_GET_SEGMENT
6710 (offset_expr.X_add_symbol)),
6711 ".lit4") == 0
6712 && offset_expr.X_add_number == 0);
67c0d1eb 6713 macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
17a2f251 6714 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 6715 break;
252b5132
RH
6716 }
6717
6718 case M_LI_D:
ca4e0257
RS
6719 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6720 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6721 order 32 bits of the value and the low order 32 bits are either
6722 zero or in OFFSET_EXPR. */
252b5132
RH
6723 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6724 {
ca4e0257 6725 if (HAVE_64BIT_GPRS)
67c0d1eb 6726 load_register (treg, &imm_expr, 1);
252b5132
RH
6727 else
6728 {
6729 int hreg, lreg;
6730
6731 if (target_big_endian)
6732 {
6733 hreg = treg;
6734 lreg = treg + 1;
6735 }
6736 else
6737 {
6738 hreg = treg + 1;
6739 lreg = treg;
6740 }
6741
6742 if (hreg <= 31)
67c0d1eb 6743 load_register (hreg, &imm_expr, 0);
252b5132
RH
6744 if (lreg <= 31)
6745 {
6746 if (offset_expr.X_op == O_absent)
67c0d1eb 6747 move_register (lreg, 0);
252b5132
RH
6748 else
6749 {
6750 assert (offset_expr.X_op == O_constant);
67c0d1eb 6751 load_register (lreg, &offset_expr, 0);
252b5132
RH
6752 }
6753 }
6754 }
8fc2e39e 6755 break;
252b5132
RH
6756 }
6757
6758 /* We know that sym is in the .rdata section. First we get the
6759 upper 16 bits of the address. */
6760 if (mips_pic == NO_PIC)
6761 {
67c0d1eb 6762 macro_build_lui (&offset_expr, AT);
8fc2e39e 6763 used_at = 1;
252b5132 6764 }
0a44bf69 6765 else
252b5132 6766 {
67c0d1eb
RS
6767 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6768 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 6769 used_at = 1;
252b5132 6770 }
bdaaa2e1 6771
252b5132 6772 /* Now we load the register(s). */
ca4e0257 6773 if (HAVE_64BIT_GPRS)
8fc2e39e
TS
6774 {
6775 used_at = 1;
6776 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6777 }
252b5132
RH
6778 else
6779 {
8fc2e39e 6780 used_at = 1;
67c0d1eb 6781 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
f9419b05 6782 if (treg != RA)
252b5132
RH
6783 {
6784 /* FIXME: How in the world do we deal with the possible
6785 overflow here? */
6786 offset_expr.X_add_number += 4;
67c0d1eb 6787 macro_build (&offset_expr, "lw", "t,o(b)",
17a2f251 6788 treg + 1, BFD_RELOC_LO16, AT);
252b5132
RH
6789 }
6790 }
252b5132
RH
6791 break;
6792
6793 case M_LI_DD:
ca4e0257
RS
6794 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6795 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6796 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6797 the value and the low order 32 bits are either zero or in
6798 OFFSET_EXPR. */
252b5132
RH
6799 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6800 {
8fc2e39e 6801 used_at = 1;
67c0d1eb 6802 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
ca4e0257
RS
6803 if (HAVE_64BIT_FPRS)
6804 {
6805 assert (HAVE_64BIT_GPRS);
67c0d1eb 6806 macro_build (NULL, "dmtc1", "t,S", AT, treg);
ca4e0257 6807 }
252b5132
RH
6808 else
6809 {
67c0d1eb 6810 macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
252b5132 6811 if (offset_expr.X_op == O_absent)
67c0d1eb 6812 macro_build (NULL, "mtc1", "t,G", 0, treg);
252b5132
RH
6813 else
6814 {
6815 assert (offset_expr.X_op == O_constant);
67c0d1eb
RS
6816 load_register (AT, &offset_expr, 0);
6817 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
6818 }
6819 }
6820 break;
6821 }
6822
6823 assert (offset_expr.X_op == O_symbol
6824 && offset_expr.X_add_number == 0);
6825 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6826 if (strcmp (s, ".lit8") == 0)
6827 {
e7af610e 6828 if (mips_opts.isa != ISA_MIPS1)
252b5132 6829 {
67c0d1eb 6830 macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
17a2f251 6831 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 6832 break;
252b5132 6833 }
c9914766 6834 breg = mips_gp_register;
252b5132
RH
6835 r = BFD_RELOC_MIPS_LITERAL;
6836 goto dob;
6837 }
6838 else
6839 {
6840 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 6841 used_at = 1;
0a44bf69 6842 if (mips_pic != NO_PIC)
67c0d1eb
RS
6843 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6844 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
6845 else
6846 {
6847 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 6848 macro_build_lui (&offset_expr, AT);
252b5132 6849 }
bdaaa2e1 6850
e7af610e 6851 if (mips_opts.isa != ISA_MIPS1)
252b5132 6852 {
67c0d1eb
RS
6853 macro_build (&offset_expr, "ldc1", "T,o(b)",
6854 treg, BFD_RELOC_LO16, AT);
252b5132
RH
6855 break;
6856 }
6857 breg = AT;
6858 r = BFD_RELOC_LO16;
6859 goto dob;
6860 }
6861
6862 case M_L_DOB:
252b5132
RH
6863 /* Even on a big endian machine $fn comes before $fn+1. We have
6864 to adjust when loading from memory. */
6865 r = BFD_RELOC_LO16;
6866 dob:
e7af610e 6867 assert (mips_opts.isa == ISA_MIPS1);
67c0d1eb 6868 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 6869 target_big_endian ? treg + 1 : treg, r, breg);
252b5132
RH
6870 /* FIXME: A possible overflow which I don't know how to deal
6871 with. */
6872 offset_expr.X_add_number += 4;
67c0d1eb 6873 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 6874 target_big_endian ? treg : treg + 1, r, breg);
252b5132
RH
6875 break;
6876
6877 case M_L_DAB:
6878 /*
6879 * The MIPS assembler seems to check for X_add_number not
6880 * being double aligned and generating:
6881 * lui at,%hi(foo+1)
6882 * addu at,at,v1
6883 * addiu at,at,%lo(foo+1)
6884 * lwc1 f2,0(at)
6885 * lwc1 f3,4(at)
6886 * But, the resulting address is the same after relocation so why
6887 * generate the extra instruction?
6888 */
bdaaa2e1 6889 /* Itbl support may require additional care here. */
252b5132 6890 coproc = 1;
e7af610e 6891 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6892 {
6893 s = "ldc1";
6894 goto ld;
6895 }
6896
6897 s = "lwc1";
6898 fmt = "T,o(b)";
6899 goto ldd_std;
6900
6901 case M_S_DAB:
e7af610e 6902 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6903 {
6904 s = "sdc1";
6905 goto st;
6906 }
6907
6908 s = "swc1";
6909 fmt = "T,o(b)";
bdaaa2e1 6910 /* Itbl support may require additional care here. */
252b5132
RH
6911 coproc = 1;
6912 goto ldd_std;
6913
6914 case M_LD_AB:
ca4e0257 6915 if (HAVE_64BIT_GPRS)
252b5132
RH
6916 {
6917 s = "ld";
6918 goto ld;
6919 }
6920
6921 s = "lw";
6922 fmt = "t,o(b)";
6923 goto ldd_std;
6924
6925 case M_SD_AB:
ca4e0257 6926 if (HAVE_64BIT_GPRS)
252b5132
RH
6927 {
6928 s = "sd";
6929 goto st;
6930 }
6931
6932 s = "sw";
6933 fmt = "t,o(b)";
6934
6935 ldd_std:
6936 if (offset_expr.X_op != O_symbol
6937 && offset_expr.X_op != O_constant)
6938 {
6939 as_bad (_("expression too complex"));
6940 offset_expr.X_op = O_constant;
6941 }
6942
2051e8c4
MR
6943 if (HAVE_32BIT_ADDRESSES
6944 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
6945 {
6946 char value [32];
6947
6948 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 6949 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 6950 }
2051e8c4 6951
252b5132
RH
6952 /* Even on a big endian machine $fn comes before $fn+1. We have
6953 to adjust when loading from memory. We set coproc if we must
6954 load $fn+1 first. */
bdaaa2e1 6955 /* Itbl support may require additional care here. */
252b5132
RH
6956 if (! target_big_endian)
6957 coproc = 0;
6958
6959 if (mips_pic == NO_PIC
6960 || offset_expr.X_op == O_constant)
6961 {
6962 /* If this is a reference to a GP relative symbol, we want
cdf6fd85
TS
6963 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6964 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6965 If we have a base register, we use this
6966 addu $at,$breg,$gp
cdf6fd85
TS
6967 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6968 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6969 If this is not a GP relative symbol, we want
6970 lui $at,<sym> (BFD_RELOC_HI16_S)
6971 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6972 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6973 If there is a base register, we add it to $at after the
6974 lui instruction. If there is a constant, we always use
6975 the last case. */
39a59cf8
MR
6976 if (offset_expr.X_op == O_symbol
6977 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 6978 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 6979 {
4d7206a2 6980 relax_start (offset_expr.X_add_symbol);
252b5132
RH
6981 if (breg == 0)
6982 {
c9914766 6983 tempreg = mips_gp_register;
252b5132
RH
6984 }
6985 else
6986 {
67c0d1eb 6987 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6988 AT, breg, mips_gp_register);
252b5132 6989 tempreg = AT;
252b5132
RH
6990 used_at = 1;
6991 }
6992
beae10d5 6993 /* Itbl support may require additional care here. */
67c0d1eb 6994 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 6995 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6996 offset_expr.X_add_number += 4;
6997
6998 /* Set mips_optimize to 2 to avoid inserting an
6999 undesired nop. */
7000 hold_mips_optimize = mips_optimize;
7001 mips_optimize = 2;
beae10d5 7002 /* Itbl support may require additional care here. */
67c0d1eb 7003 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 7004 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
7005 mips_optimize = hold_mips_optimize;
7006
4d7206a2 7007 relax_switch ();
252b5132
RH
7008
7009 /* We just generated two relocs. When tc_gen_reloc
7010 handles this case, it will skip the first reloc and
7011 handle the second. The second reloc already has an
7012 extra addend of 4, which we added above. We must
7013 subtract it out, and then subtract another 4 to make
7014 the first reloc come out right. The second reloc
7015 will come out right because we are going to add 4 to
7016 offset_expr when we build its instruction below.
7017
7018 If we have a symbol, then we don't want to include
7019 the offset, because it will wind up being included
7020 when we generate the reloc. */
7021
7022 if (offset_expr.X_op == O_constant)
7023 offset_expr.X_add_number -= 8;
7024 else
7025 {
7026 offset_expr.X_add_number = -4;
7027 offset_expr.X_op = O_constant;
7028 }
7029 }
8fc2e39e 7030 used_at = 1;
67c0d1eb 7031 macro_build_lui (&offset_expr, AT);
252b5132 7032 if (breg != 0)
67c0d1eb 7033 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 7034 /* Itbl support may require additional care here. */
67c0d1eb 7035 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 7036 BFD_RELOC_LO16, AT);
252b5132
RH
7037 /* FIXME: How do we handle overflow here? */
7038 offset_expr.X_add_number += 4;
beae10d5 7039 /* Itbl support may require additional care here. */
67c0d1eb 7040 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 7041 BFD_RELOC_LO16, AT);
4d7206a2
RS
7042 if (mips_relax.sequence)
7043 relax_end ();
bdaaa2e1 7044 }
0a44bf69 7045 else if (!mips_big_got)
252b5132 7046 {
252b5132
RH
7047 /* If this is a reference to an external symbol, we want
7048 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7049 nop
7050 <op> $treg,0($at)
7051 <op> $treg+1,4($at)
7052 Otherwise we want
7053 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7054 nop
7055 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
7056 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
7057 If there is a base register we add it to $at before the
7058 lwc1 instructions. If there is a constant we include it
7059 in the lwc1 instructions. */
7060 used_at = 1;
7061 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
7062 if (expr1.X_add_number < -0x8000
7063 || expr1.X_add_number >= 0x8000 - 4)
7064 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 7065 load_got_offset (AT, &offset_expr);
269137b2 7066 load_delay_nop ();
252b5132 7067 if (breg != 0)
67c0d1eb 7068 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
7069
7070 /* Set mips_optimize to 2 to avoid inserting an undesired
7071 nop. */
7072 hold_mips_optimize = mips_optimize;
7073 mips_optimize = 2;
4d7206a2 7074
beae10d5 7075 /* Itbl support may require additional care here. */
4d7206a2 7076 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7077 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
7078 BFD_RELOC_LO16, AT);
4d7206a2 7079 expr1.X_add_number += 4;
67c0d1eb
RS
7080 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
7081 BFD_RELOC_LO16, AT);
4d7206a2 7082 relax_switch ();
67c0d1eb
RS
7083 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
7084 BFD_RELOC_LO16, AT);
4d7206a2 7085 offset_expr.X_add_number += 4;
67c0d1eb
RS
7086 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
7087 BFD_RELOC_LO16, AT);
4d7206a2 7088 relax_end ();
252b5132 7089
4d7206a2 7090 mips_optimize = hold_mips_optimize;
252b5132 7091 }
0a44bf69 7092 else if (mips_big_got)
252b5132 7093 {
67c0d1eb 7094 int gpdelay;
252b5132
RH
7095
7096 /* If this is a reference to an external symbol, we want
7097 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7098 addu $at,$at,$gp
7099 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
7100 nop
7101 <op> $treg,0($at)
7102 <op> $treg+1,4($at)
7103 Otherwise we want
7104 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7105 nop
7106 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
7107 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
7108 If there is a base register we add it to $at before the
7109 lwc1 instructions. If there is a constant we include it
7110 in the lwc1 instructions. */
7111 used_at = 1;
7112 expr1.X_add_number = offset_expr.X_add_number;
7113 offset_expr.X_add_number = 0;
7114 if (expr1.X_add_number < -0x8000
7115 || expr1.X_add_number >= 0x8000 - 4)
7116 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 7117 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 7118 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7119 macro_build (&offset_expr, "lui", "t,u",
7120 AT, BFD_RELOC_MIPS_GOT_HI16);
7121 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7122 AT, AT, mips_gp_register);
67c0d1eb 7123 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 7124 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 7125 load_delay_nop ();
252b5132 7126 if (breg != 0)
67c0d1eb 7127 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 7128 /* Itbl support may require additional care here. */
67c0d1eb 7129 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
17a2f251 7130 BFD_RELOC_LO16, AT);
252b5132
RH
7131 expr1.X_add_number += 4;
7132
7133 /* Set mips_optimize to 2 to avoid inserting an undesired
7134 nop. */
7135 hold_mips_optimize = mips_optimize;
7136 mips_optimize = 2;
beae10d5 7137 /* Itbl support may require additional care here. */
67c0d1eb 7138 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
17a2f251 7139 BFD_RELOC_LO16, AT);
252b5132
RH
7140 mips_optimize = hold_mips_optimize;
7141 expr1.X_add_number -= 4;
7142
4d7206a2
RS
7143 relax_switch ();
7144 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
7145 if (gpdelay)
7146 macro_build (NULL, "nop", "");
7147 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
7148 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 7149 load_delay_nop ();
252b5132 7150 if (breg != 0)
67c0d1eb 7151 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 7152 /* Itbl support may require additional care here. */
67c0d1eb
RS
7153 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
7154 BFD_RELOC_LO16, AT);
4d7206a2 7155 offset_expr.X_add_number += 4;
252b5132
RH
7156
7157 /* Set mips_optimize to 2 to avoid inserting an undesired
7158 nop. */
7159 hold_mips_optimize = mips_optimize;
7160 mips_optimize = 2;
beae10d5 7161 /* Itbl support may require additional care here. */
67c0d1eb
RS
7162 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
7163 BFD_RELOC_LO16, AT);
252b5132 7164 mips_optimize = hold_mips_optimize;
4d7206a2 7165 relax_end ();
252b5132 7166 }
252b5132
RH
7167 else
7168 abort ();
7169
252b5132
RH
7170 break;
7171
7172 case M_LD_OB:
7173 s = "lw";
7174 goto sd_ob;
7175 case M_SD_OB:
7176 s = "sw";
7177 sd_ob:
ca4e0257 7178 assert (HAVE_32BIT_ADDRESSES);
67c0d1eb 7179 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
252b5132 7180 offset_expr.X_add_number += 4;
67c0d1eb 7181 macro_build (&offset_expr, s, "t,o(b)", treg + 1, BFD_RELOC_LO16, breg);
8fc2e39e 7182 break;
252b5132
RH
7183
7184 /* New code added to support COPZ instructions.
7185 This code builds table entries out of the macros in mip_opcodes.
7186 R4000 uses interlocks to handle coproc delays.
7187 Other chips (like the R3000) require nops to be inserted for delays.
7188
f72c8c98 7189 FIXME: Currently, we require that the user handle delays.
252b5132
RH
7190 In order to fill delay slots for non-interlocked chips,
7191 we must have a way to specify delays based on the coprocessor.
7192 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
7193 What are the side-effects of the cop instruction?
7194 What cache support might we have and what are its effects?
7195 Both coprocessor & memory require delays. how long???
bdaaa2e1 7196 What registers are read/set/modified?
252b5132
RH
7197
7198 If an itbl is provided to interpret cop instructions,
bdaaa2e1 7199 this knowledge can be encoded in the itbl spec. */
252b5132
RH
7200
7201 case M_COP0:
7202 s = "c0";
7203 goto copz;
7204 case M_COP1:
7205 s = "c1";
7206 goto copz;
7207 case M_COP2:
7208 s = "c2";
7209 goto copz;
7210 case M_COP3:
7211 s = "c3";
7212 copz:
b19e8a9b
AN
7213 if (NO_ISA_COP (mips_opts.arch)
7214 && (ip->insn_mo->pinfo2 & INSN2_M_FP_S) == 0)
7215 {
7216 as_bad (_("opcode not supported on this processor: %s"),
7217 mips_cpu_info_from_arch (mips_opts.arch)->name);
7218 break;
7219 }
7220
252b5132
RH
7221 /* For now we just do C (same as Cz). The parameter will be
7222 stored in insn_opcode by mips_ip. */
67c0d1eb 7223 macro_build (NULL, s, "C", ip->insn_opcode);
8fc2e39e 7224 break;
252b5132 7225
ea1fb5dc 7226 case M_MOVE:
67c0d1eb 7227 move_register (dreg, sreg);
8fc2e39e 7228 break;
ea1fb5dc 7229
252b5132
RH
7230#ifdef LOSING_COMPILER
7231 default:
7232 /* Try and see if this is a new itbl instruction.
7233 This code builds table entries out of the macros in mip_opcodes.
7234 FIXME: For now we just assemble the expression and pass it's
7235 value along as a 32-bit immediate.
bdaaa2e1 7236 We may want to have the assembler assemble this value,
252b5132
RH
7237 so that we gain the assembler's knowledge of delay slots,
7238 symbols, etc.
7239 Would it be more efficient to use mask (id) here? */
bdaaa2e1 7240 if (itbl_have_entries
252b5132 7241 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
beae10d5 7242 {
252b5132
RH
7243 s = ip->insn_mo->name;
7244 s2 = "cop3";
7245 coproc = ITBL_DECODE_PNUM (immed_expr);;
67c0d1eb 7246 macro_build (&immed_expr, s, "C");
8fc2e39e 7247 break;
beae10d5 7248 }
252b5132 7249 macro2 (ip);
8fc2e39e 7250 break;
252b5132 7251 }
741fe287 7252 if (!mips_opts.at && used_at)
8fc2e39e 7253 as_bad (_("Macro used $at after \".set noat\""));
252b5132 7254}
bdaaa2e1 7255
252b5132 7256static void
17a2f251 7257macro2 (struct mips_cl_insn *ip)
252b5132 7258{
741fe287
MR
7259 unsigned int treg, sreg, dreg, breg;
7260 unsigned int tempreg;
252b5132 7261 int mask;
252b5132
RH
7262 int used_at;
7263 expressionS expr1;
7264 const char *s;
7265 const char *s2;
7266 const char *fmt;
7267 int likely = 0;
7268 int dbl = 0;
7269 int coproc = 0;
7270 int lr = 0;
7271 int imm = 0;
7272 int off;
7273 offsetT maxnum;
7274 bfd_reloc_code_real_type r;
bdaaa2e1 7275
252b5132
RH
7276 treg = (ip->insn_opcode >> 16) & 0x1f;
7277 dreg = (ip->insn_opcode >> 11) & 0x1f;
7278 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
7279 mask = ip->insn_mo->mask;
bdaaa2e1 7280
252b5132
RH
7281 expr1.X_op = O_constant;
7282 expr1.X_op_symbol = NULL;
7283 expr1.X_add_symbol = NULL;
7284 expr1.X_add_number = 1;
bdaaa2e1 7285
252b5132
RH
7286 switch (mask)
7287 {
7288#endif /* LOSING_COMPILER */
7289
7290 case M_DMUL:
7291 dbl = 1;
7292 case M_MUL:
67c0d1eb
RS
7293 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
7294 macro_build (NULL, "mflo", "d", dreg);
8fc2e39e 7295 break;
252b5132
RH
7296
7297 case M_DMUL_I:
7298 dbl = 1;
7299 case M_MUL_I:
7300 /* The MIPS assembler some times generates shifts and adds. I'm
7301 not trying to be that fancy. GCC should do this for us
7302 anyway. */
8fc2e39e 7303 used_at = 1;
67c0d1eb
RS
7304 load_register (AT, &imm_expr, dbl);
7305 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
7306 macro_build (NULL, "mflo", "d", dreg);
252b5132
RH
7307 break;
7308
7309 case M_DMULO_I:
7310 dbl = 1;
7311 case M_MULO_I:
7312 imm = 1;
7313 goto do_mulo;
7314
7315 case M_DMULO:
7316 dbl = 1;
7317 case M_MULO:
7318 do_mulo:
7d10b47d 7319 start_noreorder ();
8fc2e39e 7320 used_at = 1;
252b5132 7321 if (imm)
67c0d1eb
RS
7322 load_register (AT, &imm_expr, dbl);
7323 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
7324 macro_build (NULL, "mflo", "d", dreg);
7325 macro_build (NULL, dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
7326 macro_build (NULL, "mfhi", "d", AT);
252b5132 7327 if (mips_trap)
67c0d1eb 7328 macro_build (NULL, "tne", "s,t,q", dreg, AT, 6);
252b5132
RH
7329 else
7330 {
7331 expr1.X_add_number = 8;
67c0d1eb
RS
7332 macro_build (&expr1, "beq", "s,t,p", dreg, AT);
7333 macro_build (NULL, "nop", "", 0);
7334 macro_build (NULL, "break", "c", 6);
252b5132 7335 }
7d10b47d 7336 end_noreorder ();
67c0d1eb 7337 macro_build (NULL, "mflo", "d", dreg);
252b5132
RH
7338 break;
7339
7340 case M_DMULOU_I:
7341 dbl = 1;
7342 case M_MULOU_I:
7343 imm = 1;
7344 goto do_mulou;
7345
7346 case M_DMULOU:
7347 dbl = 1;
7348 case M_MULOU:
7349 do_mulou:
7d10b47d 7350 start_noreorder ();
8fc2e39e 7351 used_at = 1;
252b5132 7352 if (imm)
67c0d1eb
RS
7353 load_register (AT, &imm_expr, dbl);
7354 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
17a2f251 7355 sreg, imm ? AT : treg);
67c0d1eb
RS
7356 macro_build (NULL, "mfhi", "d", AT);
7357 macro_build (NULL, "mflo", "d", dreg);
252b5132 7358 if (mips_trap)
67c0d1eb 7359 macro_build (NULL, "tne", "s,t,q", AT, 0, 6);
252b5132
RH
7360 else
7361 {
7362 expr1.X_add_number = 8;
67c0d1eb
RS
7363 macro_build (&expr1, "beq", "s,t,p", AT, 0);
7364 macro_build (NULL, "nop", "", 0);
7365 macro_build (NULL, "break", "c", 6);
252b5132 7366 }
7d10b47d 7367 end_noreorder ();
252b5132
RH
7368 break;
7369
771c7ce4 7370 case M_DROL:
fef14a42 7371 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
7372 {
7373 if (dreg == sreg)
7374 {
7375 tempreg = AT;
7376 used_at = 1;
7377 }
7378 else
7379 {
7380 tempreg = dreg;
82dd0097 7381 }
67c0d1eb
RS
7382 macro_build (NULL, "dnegu", "d,w", tempreg, treg);
7383 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
8fc2e39e 7384 break;
82dd0097 7385 }
8fc2e39e 7386 used_at = 1;
67c0d1eb
RS
7387 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
7388 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
7389 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
7390 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7391 break;
7392
252b5132 7393 case M_ROL:
fef14a42 7394 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097
CD
7395 {
7396 if (dreg == sreg)
7397 {
7398 tempreg = AT;
7399 used_at = 1;
7400 }
7401 else
7402 {
7403 tempreg = dreg;
82dd0097 7404 }
67c0d1eb
RS
7405 macro_build (NULL, "negu", "d,w", tempreg, treg);
7406 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
8fc2e39e 7407 break;
82dd0097 7408 }
8fc2e39e 7409 used_at = 1;
67c0d1eb
RS
7410 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
7411 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
7412 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
7413 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
7414 break;
7415
771c7ce4
TS
7416 case M_DROL_I:
7417 {
7418 unsigned int rot;
82dd0097 7419 char *l, *r;
771c7ce4
TS
7420
7421 if (imm_expr.X_op != O_constant)
82dd0097 7422 as_bad (_("Improper rotate count"));
771c7ce4 7423 rot = imm_expr.X_add_number & 0x3f;
fef14a42 7424 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
7425 {
7426 rot = (64 - rot) & 0x3f;
7427 if (rot >= 32)
67c0d1eb 7428 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
60b63b72 7429 else
67c0d1eb 7430 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
8fc2e39e 7431 break;
60b63b72 7432 }
483fc7cd 7433 if (rot == 0)
483fc7cd 7434 {
67c0d1eb 7435 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
8fc2e39e 7436 break;
483fc7cd 7437 }
82dd0097
CD
7438 l = (rot < 0x20) ? "dsll" : "dsll32";
7439 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
7440 rot &= 0x1f;
8fc2e39e 7441 used_at = 1;
67c0d1eb
RS
7442 macro_build (NULL, l, "d,w,<", AT, sreg, rot);
7443 macro_build (NULL, r, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7444 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7445 }
7446 break;
7447
252b5132 7448 case M_ROL_I:
771c7ce4
TS
7449 {
7450 unsigned int rot;
7451
7452 if (imm_expr.X_op != O_constant)
82dd0097 7453 as_bad (_("Improper rotate count"));
771c7ce4 7454 rot = imm_expr.X_add_number & 0x1f;
fef14a42 7455 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 7456 {
67c0d1eb 7457 macro_build (NULL, "ror", "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
8fc2e39e 7458 break;
60b63b72 7459 }
483fc7cd 7460 if (rot == 0)
483fc7cd 7461 {
67c0d1eb 7462 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
8fc2e39e 7463 break;
483fc7cd 7464 }
8fc2e39e 7465 used_at = 1;
67c0d1eb
RS
7466 macro_build (NULL, "sll", "d,w,<", AT, sreg, rot);
7467 macro_build (NULL, "srl", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7468 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7469 }
7470 break;
7471
7472 case M_DROR:
fef14a42 7473 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 7474 {
67c0d1eb 7475 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
8fc2e39e 7476 break;
82dd0097 7477 }
8fc2e39e 7478 used_at = 1;
67c0d1eb
RS
7479 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
7480 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
7481 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
7482 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
7483 break;
7484
7485 case M_ROR:
fef14a42 7486 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 7487 {
67c0d1eb 7488 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
8fc2e39e 7489 break;
82dd0097 7490 }
8fc2e39e 7491 used_at = 1;
67c0d1eb
RS
7492 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
7493 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
7494 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
7495 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
7496 break;
7497
771c7ce4
TS
7498 case M_DROR_I:
7499 {
7500 unsigned int rot;
82dd0097 7501 char *l, *r;
771c7ce4
TS
7502
7503 if (imm_expr.X_op != O_constant)
82dd0097 7504 as_bad (_("Improper rotate count"));
771c7ce4 7505 rot = imm_expr.X_add_number & 0x3f;
fef14a42 7506 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
7507 {
7508 if (rot >= 32)
67c0d1eb 7509 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
82dd0097 7510 else
67c0d1eb 7511 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
8fc2e39e 7512 break;
82dd0097 7513 }
483fc7cd 7514 if (rot == 0)
483fc7cd 7515 {
67c0d1eb 7516 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
8fc2e39e 7517 break;
483fc7cd 7518 }
82dd0097
CD
7519 r = (rot < 0x20) ? "dsrl" : "dsrl32";
7520 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7521 rot &= 0x1f;
8fc2e39e 7522 used_at = 1;
67c0d1eb
RS
7523 macro_build (NULL, r, "d,w,<", AT, sreg, rot);
7524 macro_build (NULL, l, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7525 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7526 }
7527 break;
7528
252b5132 7529 case M_ROR_I:
771c7ce4
TS
7530 {
7531 unsigned int rot;
7532
7533 if (imm_expr.X_op != O_constant)
82dd0097 7534 as_bad (_("Improper rotate count"));
771c7ce4 7535 rot = imm_expr.X_add_number & 0x1f;
fef14a42 7536 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 7537 {
67c0d1eb 7538 macro_build (NULL, "ror", "d,w,<", dreg, sreg, rot);
8fc2e39e 7539 break;
82dd0097 7540 }
483fc7cd 7541 if (rot == 0)
483fc7cd 7542 {
67c0d1eb 7543 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
8fc2e39e 7544 break;
483fc7cd 7545 }
8fc2e39e 7546 used_at = 1;
67c0d1eb
RS
7547 macro_build (NULL, "srl", "d,w,<", AT, sreg, rot);
7548 macro_build (NULL, "sll", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7549 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4 7550 }
252b5132
RH
7551 break;
7552
7553 case M_S_DOB:
e7af610e 7554 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7555 /* Even on a big endian machine $fn comes before $fn+1. We have
7556 to adjust when storing to memory. */
67c0d1eb
RS
7557 macro_build (&offset_expr, "swc1", "T,o(b)",
7558 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
252b5132 7559 offset_expr.X_add_number += 4;
67c0d1eb
RS
7560 macro_build (&offset_expr, "swc1", "T,o(b)",
7561 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
8fc2e39e 7562 break;
252b5132
RH
7563
7564 case M_SEQ:
7565 if (sreg == 0)
67c0d1eb 7566 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
252b5132 7567 else if (treg == 0)
67c0d1eb 7568 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7569 else
7570 {
67c0d1eb
RS
7571 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7572 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
252b5132 7573 }
8fc2e39e 7574 break;
252b5132
RH
7575
7576 case M_SEQ_I:
7577 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7578 {
67c0d1eb 7579 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 7580 break;
252b5132
RH
7581 }
7582 if (sreg == 0)
7583 {
7584 as_warn (_("Instruction %s: result is always false"),
7585 ip->insn_mo->name);
67c0d1eb 7586 move_register (dreg, 0);
8fc2e39e 7587 break;
252b5132 7588 }
dd3cbb7e
NC
7589 if (CPU_HAS_SEQ (mips_opts.arch)
7590 && -512 <= imm_expr.X_add_number
7591 && imm_expr.X_add_number < 512)
7592 {
7593 macro_build (NULL, "seqi", "t,r,+Q", dreg, sreg,
7594 imm_expr.X_add_number);
7595 break;
7596 }
252b5132
RH
7597 if (imm_expr.X_op == O_constant
7598 && imm_expr.X_add_number >= 0
7599 && imm_expr.X_add_number < 0x10000)
7600 {
67c0d1eb 7601 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7602 }
7603 else if (imm_expr.X_op == O_constant
7604 && imm_expr.X_add_number > -0x8000
7605 && imm_expr.X_add_number < 0)
7606 {
7607 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7608 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 7609 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132 7610 }
dd3cbb7e
NC
7611 else if (CPU_HAS_SEQ (mips_opts.arch))
7612 {
7613 used_at = 1;
7614 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7615 macro_build (NULL, "seq", "d,v,t", dreg, sreg, AT);
7616 break;
7617 }
252b5132
RH
7618 else
7619 {
67c0d1eb
RS
7620 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7621 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
7622 used_at = 1;
7623 }
67c0d1eb 7624 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 7625 break;
252b5132
RH
7626
7627 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
7628 s = "slt";
7629 goto sge;
7630 case M_SGEU:
7631 s = "sltu";
7632 sge:
67c0d1eb
RS
7633 macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
7634 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 7635 break;
252b5132
RH
7636
7637 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
7638 case M_SGEU_I:
7639 if (imm_expr.X_op == O_constant
7640 && imm_expr.X_add_number >= -0x8000
7641 && imm_expr.X_add_number < 0x8000)
7642 {
67c0d1eb
RS
7643 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
7644 dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7645 }
7646 else
7647 {
67c0d1eb
RS
7648 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7649 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
7650 dreg, sreg, AT);
252b5132
RH
7651 used_at = 1;
7652 }
67c0d1eb 7653 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 7654 break;
252b5132
RH
7655
7656 case M_SGT: /* sreg > treg <==> treg < sreg */
7657 s = "slt";
7658 goto sgt;
7659 case M_SGTU:
7660 s = "sltu";
7661 sgt:
67c0d1eb 7662 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
8fc2e39e 7663 break;
252b5132
RH
7664
7665 case M_SGT_I: /* sreg > I <==> I < sreg */
7666 s = "slt";
7667 goto sgti;
7668 case M_SGTU_I:
7669 s = "sltu";
7670 sgti:
8fc2e39e 7671 used_at = 1;
67c0d1eb
RS
7672 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7673 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
252b5132
RH
7674 break;
7675
2396cfb9 7676 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
252b5132
RH
7677 s = "slt";
7678 goto sle;
7679 case M_SLEU:
7680 s = "sltu";
7681 sle:
67c0d1eb
RS
7682 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7683 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 7684 break;
252b5132 7685
2396cfb9 7686 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
252b5132
RH
7687 s = "slt";
7688 goto slei;
7689 case M_SLEU_I:
7690 s = "sltu";
7691 slei:
8fc2e39e 7692 used_at = 1;
67c0d1eb
RS
7693 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7694 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7695 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
7696 break;
7697
7698 case M_SLT_I:
7699 if (imm_expr.X_op == O_constant
7700 && imm_expr.X_add_number >= -0x8000
7701 && imm_expr.X_add_number < 0x8000)
7702 {
67c0d1eb 7703 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 7704 break;
252b5132 7705 }
8fc2e39e 7706 used_at = 1;
67c0d1eb
RS
7707 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7708 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
252b5132
RH
7709 break;
7710
7711 case M_SLTU_I:
7712 if (imm_expr.X_op == O_constant
7713 && imm_expr.X_add_number >= -0x8000
7714 && imm_expr.X_add_number < 0x8000)
7715 {
67c0d1eb 7716 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
17a2f251 7717 BFD_RELOC_LO16);
8fc2e39e 7718 break;
252b5132 7719 }
8fc2e39e 7720 used_at = 1;
67c0d1eb
RS
7721 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7722 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7723 break;
7724
7725 case M_SNE:
7726 if (sreg == 0)
67c0d1eb 7727 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
252b5132 7728 else if (treg == 0)
67c0d1eb 7729 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
252b5132
RH
7730 else
7731 {
67c0d1eb
RS
7732 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7733 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
252b5132 7734 }
8fc2e39e 7735 break;
252b5132
RH
7736
7737 case M_SNE_I:
7738 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7739 {
67c0d1eb 7740 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
8fc2e39e 7741 break;
252b5132
RH
7742 }
7743 if (sreg == 0)
7744 {
7745 as_warn (_("Instruction %s: result is always true"),
7746 ip->insn_mo->name);
67c0d1eb
RS
7747 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
7748 dreg, 0, BFD_RELOC_LO16);
8fc2e39e 7749 break;
252b5132 7750 }
dd3cbb7e
NC
7751 if (CPU_HAS_SEQ (mips_opts.arch)
7752 && -512 <= imm_expr.X_add_number
7753 && imm_expr.X_add_number < 512)
7754 {
7755 macro_build (NULL, "snei", "t,r,+Q", dreg, sreg,
7756 imm_expr.X_add_number);
7757 break;
7758 }
252b5132
RH
7759 if (imm_expr.X_op == O_constant
7760 && imm_expr.X_add_number >= 0
7761 && imm_expr.X_add_number < 0x10000)
7762 {
67c0d1eb 7763 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7764 }
7765 else if (imm_expr.X_op == O_constant
7766 && imm_expr.X_add_number > -0x8000
7767 && imm_expr.X_add_number < 0)
7768 {
7769 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7770 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 7771 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132 7772 }
dd3cbb7e
NC
7773 else if (CPU_HAS_SEQ (mips_opts.arch))
7774 {
7775 used_at = 1;
7776 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7777 macro_build (NULL, "sne", "d,v,t", dreg, sreg, AT);
7778 break;
7779 }
252b5132
RH
7780 else
7781 {
67c0d1eb
RS
7782 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7783 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
7784 used_at = 1;
7785 }
67c0d1eb 7786 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
8fc2e39e 7787 break;
252b5132
RH
7788
7789 case M_DSUB_I:
7790 dbl = 1;
7791 case M_SUB_I:
7792 if (imm_expr.X_op == O_constant
7793 && imm_expr.X_add_number > -0x8000
7794 && imm_expr.X_add_number <= 0x8000)
7795 {
7796 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb
RS
7797 macro_build (&imm_expr, dbl ? "daddi" : "addi", "t,r,j",
7798 dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 7799 break;
252b5132 7800 }
8fc2e39e 7801 used_at = 1;
67c0d1eb
RS
7802 load_register (AT, &imm_expr, dbl);
7803 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
252b5132
RH
7804 break;
7805
7806 case M_DSUBU_I:
7807 dbl = 1;
7808 case M_SUBU_I:
7809 if (imm_expr.X_op == O_constant
7810 && imm_expr.X_add_number > -0x8000
7811 && imm_expr.X_add_number <= 0x8000)
7812 {
7813 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb
RS
7814 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "t,r,j",
7815 dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 7816 break;
252b5132 7817 }
8fc2e39e 7818 used_at = 1;
67c0d1eb
RS
7819 load_register (AT, &imm_expr, dbl);
7820 macro_build (NULL, dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7821 break;
7822
7823 case M_TEQ_I:
7824 s = "teq";
7825 goto trap;
7826 case M_TGE_I:
7827 s = "tge";
7828 goto trap;
7829 case M_TGEU_I:
7830 s = "tgeu";
7831 goto trap;
7832 case M_TLT_I:
7833 s = "tlt";
7834 goto trap;
7835 case M_TLTU_I:
7836 s = "tltu";
7837 goto trap;
7838 case M_TNE_I:
7839 s = "tne";
7840 trap:
8fc2e39e 7841 used_at = 1;
67c0d1eb
RS
7842 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7843 macro_build (NULL, s, "s,t", sreg, AT);
252b5132
RH
7844 break;
7845
252b5132 7846 case M_TRUNCWS:
43841e91 7847 case M_TRUNCWD:
e7af610e 7848 assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 7849 used_at = 1;
252b5132
RH
7850 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7851 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7852
7853 /*
7854 * Is the double cfc1 instruction a bug in the mips assembler;
7855 * or is there a reason for it?
7856 */
7d10b47d 7857 start_noreorder ();
67c0d1eb
RS
7858 macro_build (NULL, "cfc1", "t,G", treg, RA);
7859 macro_build (NULL, "cfc1", "t,G", treg, RA);
7860 macro_build (NULL, "nop", "");
252b5132 7861 expr1.X_add_number = 3;
67c0d1eb 7862 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
252b5132 7863 expr1.X_add_number = 2;
67c0d1eb
RS
7864 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
7865 macro_build (NULL, "ctc1", "t,G", AT, RA);
7866 macro_build (NULL, "nop", "");
7867 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
7868 dreg, sreg);
7869 macro_build (NULL, "ctc1", "t,G", treg, RA);
7870 macro_build (NULL, "nop", "");
7d10b47d 7871 end_noreorder ();
252b5132
RH
7872 break;
7873
7874 case M_ULH:
7875 s = "lb";
7876 goto ulh;
7877 case M_ULHU:
7878 s = "lbu";
7879 ulh:
8fc2e39e 7880 used_at = 1;
252b5132
RH
7881 if (offset_expr.X_add_number >= 0x7fff)
7882 as_bad (_("operand overflow"));
252b5132 7883 if (! target_big_endian)
f9419b05 7884 ++offset_expr.X_add_number;
67c0d1eb 7885 macro_build (&offset_expr, s, "t,o(b)", AT, BFD_RELOC_LO16, breg);
252b5132 7886 if (! target_big_endian)
f9419b05 7887 --offset_expr.X_add_number;
252b5132 7888 else
f9419b05 7889 ++offset_expr.X_add_number;
67c0d1eb
RS
7890 macro_build (&offset_expr, "lbu", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7891 macro_build (NULL, "sll", "d,w,<", AT, AT, 8);
7892 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7893 break;
7894
7895 case M_ULD:
7896 s = "ldl";
7897 s2 = "ldr";
7898 off = 7;
7899 goto ulw;
7900 case M_ULW:
7901 s = "lwl";
7902 s2 = "lwr";
7903 off = 3;
7904 ulw:
7905 if (offset_expr.X_add_number >= 0x8000 - off)
7906 as_bad (_("operand overflow"));
af22f5b2
CD
7907 if (treg != breg)
7908 tempreg = treg;
7909 else
8fc2e39e
TS
7910 {
7911 used_at = 1;
7912 tempreg = AT;
7913 }
252b5132
RH
7914 if (! target_big_endian)
7915 offset_expr.X_add_number += off;
67c0d1eb 7916 macro_build (&offset_expr, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
252b5132
RH
7917 if (! target_big_endian)
7918 offset_expr.X_add_number -= off;
7919 else
7920 offset_expr.X_add_number += off;
67c0d1eb 7921 macro_build (&offset_expr, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
af22f5b2
CD
7922
7923 /* If necessary, move the result in tempreg the final destination. */
7924 if (treg == tempreg)
8fc2e39e 7925 break;
af22f5b2 7926 /* Protect second load's delay slot. */
017315e4 7927 load_delay_nop ();
67c0d1eb 7928 move_register (treg, tempreg);
af22f5b2 7929 break;
252b5132
RH
7930
7931 case M_ULD_A:
7932 s = "ldl";
7933 s2 = "ldr";
7934 off = 7;
7935 goto ulwa;
7936 case M_ULW_A:
7937 s = "lwl";
7938 s2 = "lwr";
7939 off = 3;
7940 ulwa:
d6bc6245 7941 used_at = 1;
67c0d1eb 7942 load_address (AT, &offset_expr, &used_at);
252b5132 7943 if (breg != 0)
67c0d1eb 7944 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7945 if (! target_big_endian)
7946 expr1.X_add_number = off;
7947 else
7948 expr1.X_add_number = 0;
67c0d1eb 7949 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7950 if (! target_big_endian)
7951 expr1.X_add_number = 0;
7952 else
7953 expr1.X_add_number = off;
67c0d1eb 7954 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7955 break;
7956
7957 case M_ULH_A:
7958 case M_ULHU_A:
d6bc6245 7959 used_at = 1;
67c0d1eb 7960 load_address (AT, &offset_expr, &used_at);
252b5132 7961 if (breg != 0)
67c0d1eb 7962 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7963 if (target_big_endian)
7964 expr1.X_add_number = 0;
67c0d1eb 7965 macro_build (&expr1, mask == M_ULH_A ? "lb" : "lbu", "t,o(b)",
17a2f251 7966 treg, BFD_RELOC_LO16, AT);
252b5132
RH
7967 if (target_big_endian)
7968 expr1.X_add_number = 1;
7969 else
7970 expr1.X_add_number = 0;
67c0d1eb
RS
7971 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7972 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7973 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7974 break;
7975
7976 case M_USH:
8fc2e39e 7977 used_at = 1;
252b5132
RH
7978 if (offset_expr.X_add_number >= 0x7fff)
7979 as_bad (_("operand overflow"));
7980 if (target_big_endian)
f9419b05 7981 ++offset_expr.X_add_number;
67c0d1eb
RS
7982 macro_build (&offset_expr, "sb", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7983 macro_build (NULL, "srl", "d,w,<", AT, treg, 8);
252b5132 7984 if (target_big_endian)
f9419b05 7985 --offset_expr.X_add_number;
252b5132 7986 else
f9419b05 7987 ++offset_expr.X_add_number;
67c0d1eb 7988 macro_build (&offset_expr, "sb", "t,o(b)", AT, BFD_RELOC_LO16, breg);
252b5132
RH
7989 break;
7990
7991 case M_USD:
7992 s = "sdl";
7993 s2 = "sdr";
7994 off = 7;
7995 goto usw;
7996 case M_USW:
7997 s = "swl";
7998 s2 = "swr";
7999 off = 3;
8000 usw:
8001 if (offset_expr.X_add_number >= 0x8000 - off)
8002 as_bad (_("operand overflow"));
8003 if (! target_big_endian)
8004 offset_expr.X_add_number += off;
67c0d1eb 8005 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
252b5132
RH
8006 if (! target_big_endian)
8007 offset_expr.X_add_number -= off;
8008 else
8009 offset_expr.X_add_number += off;
67c0d1eb 8010 macro_build (&offset_expr, s2, "t,o(b)", treg, BFD_RELOC_LO16, breg);
8fc2e39e 8011 break;
252b5132
RH
8012
8013 case M_USD_A:
8014 s = "sdl";
8015 s2 = "sdr";
8016 off = 7;
8017 goto uswa;
8018 case M_USW_A:
8019 s = "swl";
8020 s2 = "swr";
8021 off = 3;
8022 uswa:
d6bc6245 8023 used_at = 1;
67c0d1eb 8024 load_address (AT, &offset_expr, &used_at);
252b5132 8025 if (breg != 0)
67c0d1eb 8026 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
8027 if (! target_big_endian)
8028 expr1.X_add_number = off;
8029 else
8030 expr1.X_add_number = 0;
67c0d1eb 8031 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
8032 if (! target_big_endian)
8033 expr1.X_add_number = 0;
8034 else
8035 expr1.X_add_number = off;
67c0d1eb 8036 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
8037 break;
8038
8039 case M_USH_A:
d6bc6245 8040 used_at = 1;
67c0d1eb 8041 load_address (AT, &offset_expr, &used_at);
252b5132 8042 if (breg != 0)
67c0d1eb 8043 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
8044 if (! target_big_endian)
8045 expr1.X_add_number = 0;
67c0d1eb
RS
8046 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
8047 macro_build (NULL, "srl", "d,w,<", treg, treg, 8);
252b5132
RH
8048 if (! target_big_endian)
8049 expr1.X_add_number = 1;
8050 else
8051 expr1.X_add_number = 0;
67c0d1eb 8052 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
8053 if (! target_big_endian)
8054 expr1.X_add_number = 0;
8055 else
8056 expr1.X_add_number = 1;
67c0d1eb
RS
8057 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
8058 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
8059 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
8060 break;
8061
8062 default:
8063 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 8064 are added dynamically. */
252b5132
RH
8065 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
8066 break;
8067 }
741fe287 8068 if (!mips_opts.at && used_at)
8fc2e39e 8069 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
8070}
8071
8072/* Implement macros in mips16 mode. */
8073
8074static void
17a2f251 8075mips16_macro (struct mips_cl_insn *ip)
252b5132
RH
8076{
8077 int mask;
8078 int xreg, yreg, zreg, tmp;
252b5132
RH
8079 expressionS expr1;
8080 int dbl;
8081 const char *s, *s2, *s3;
8082
8083 mask = ip->insn_mo->mask;
8084
bf12938e
RS
8085 xreg = MIPS16_EXTRACT_OPERAND (RX, *ip);
8086 yreg = MIPS16_EXTRACT_OPERAND (RY, *ip);
8087 zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip);
252b5132 8088
252b5132
RH
8089 expr1.X_op = O_constant;
8090 expr1.X_op_symbol = NULL;
8091 expr1.X_add_symbol = NULL;
8092 expr1.X_add_number = 1;
8093
8094 dbl = 0;
8095
8096 switch (mask)
8097 {
8098 default:
8099 internalError ();
8100
8101 case M_DDIV_3:
8102 dbl = 1;
8103 case M_DIV_3:
8104 s = "mflo";
8105 goto do_div3;
8106 case M_DREM_3:
8107 dbl = 1;
8108 case M_REM_3:
8109 s = "mfhi";
8110 do_div3:
7d10b47d 8111 start_noreorder ();
67c0d1eb 8112 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
252b5132 8113 expr1.X_add_number = 2;
67c0d1eb
RS
8114 macro_build (&expr1, "bnez", "x,p", yreg);
8115 macro_build (NULL, "break", "6", 7);
bdaaa2e1 8116
252b5132
RH
8117 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
8118 since that causes an overflow. We should do that as well,
8119 but I don't see how to do the comparisons without a temporary
8120 register. */
7d10b47d 8121 end_noreorder ();
67c0d1eb 8122 macro_build (NULL, s, "x", zreg);
252b5132
RH
8123 break;
8124
8125 case M_DIVU_3:
8126 s = "divu";
8127 s2 = "mflo";
8128 goto do_divu3;
8129 case M_REMU_3:
8130 s = "divu";
8131 s2 = "mfhi";
8132 goto do_divu3;
8133 case M_DDIVU_3:
8134 s = "ddivu";
8135 s2 = "mflo";
8136 goto do_divu3;
8137 case M_DREMU_3:
8138 s = "ddivu";
8139 s2 = "mfhi";
8140 do_divu3:
7d10b47d 8141 start_noreorder ();
67c0d1eb 8142 macro_build (NULL, s, "0,x,y", xreg, yreg);
252b5132 8143 expr1.X_add_number = 2;
67c0d1eb
RS
8144 macro_build (&expr1, "bnez", "x,p", yreg);
8145 macro_build (NULL, "break", "6", 7);
7d10b47d 8146 end_noreorder ();
67c0d1eb 8147 macro_build (NULL, s2, "x", zreg);
252b5132
RH
8148 break;
8149
8150 case M_DMUL:
8151 dbl = 1;
8152 case M_MUL:
67c0d1eb
RS
8153 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
8154 macro_build (NULL, "mflo", "x", zreg);
8fc2e39e 8155 break;
252b5132
RH
8156
8157 case M_DSUBU_I:
8158 dbl = 1;
8159 goto do_subu;
8160 case M_SUBU_I:
8161 do_subu:
8162 if (imm_expr.X_op != O_constant)
8163 as_bad (_("Unsupported large constant"));
8164 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 8165 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
252b5132
RH
8166 break;
8167
8168 case M_SUBU_I_2:
8169 if (imm_expr.X_op != O_constant)
8170 as_bad (_("Unsupported large constant"));
8171 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 8172 macro_build (&imm_expr, "addiu", "x,k", xreg);
252b5132
RH
8173 break;
8174
8175 case M_DSUBU_I_2:
8176 if (imm_expr.X_op != O_constant)
8177 as_bad (_("Unsupported large constant"));
8178 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 8179 macro_build (&imm_expr, "daddiu", "y,j", yreg);
252b5132
RH
8180 break;
8181
8182 case M_BEQ:
8183 s = "cmp";
8184 s2 = "bteqz";
8185 goto do_branch;
8186 case M_BNE:
8187 s = "cmp";
8188 s2 = "btnez";
8189 goto do_branch;
8190 case M_BLT:
8191 s = "slt";
8192 s2 = "btnez";
8193 goto do_branch;
8194 case M_BLTU:
8195 s = "sltu";
8196 s2 = "btnez";
8197 goto do_branch;
8198 case M_BLE:
8199 s = "slt";
8200 s2 = "bteqz";
8201 goto do_reverse_branch;
8202 case M_BLEU:
8203 s = "sltu";
8204 s2 = "bteqz";
8205 goto do_reverse_branch;
8206 case M_BGE:
8207 s = "slt";
8208 s2 = "bteqz";
8209 goto do_branch;
8210 case M_BGEU:
8211 s = "sltu";
8212 s2 = "bteqz";
8213 goto do_branch;
8214 case M_BGT:
8215 s = "slt";
8216 s2 = "btnez";
8217 goto do_reverse_branch;
8218 case M_BGTU:
8219 s = "sltu";
8220 s2 = "btnez";
8221
8222 do_reverse_branch:
8223 tmp = xreg;
8224 xreg = yreg;
8225 yreg = tmp;
8226
8227 do_branch:
67c0d1eb
RS
8228 macro_build (NULL, s, "x,y", xreg, yreg);
8229 macro_build (&offset_expr, s2, "p");
252b5132
RH
8230 break;
8231
8232 case M_BEQ_I:
8233 s = "cmpi";
8234 s2 = "bteqz";
8235 s3 = "x,U";
8236 goto do_branch_i;
8237 case M_BNE_I:
8238 s = "cmpi";
8239 s2 = "btnez";
8240 s3 = "x,U";
8241 goto do_branch_i;
8242 case M_BLT_I:
8243 s = "slti";
8244 s2 = "btnez";
8245 s3 = "x,8";
8246 goto do_branch_i;
8247 case M_BLTU_I:
8248 s = "sltiu";
8249 s2 = "btnez";
8250 s3 = "x,8";
8251 goto do_branch_i;
8252 case M_BLE_I:
8253 s = "slti";
8254 s2 = "btnez";
8255 s3 = "x,8";
8256 goto do_addone_branch_i;
8257 case M_BLEU_I:
8258 s = "sltiu";
8259 s2 = "btnez";
8260 s3 = "x,8";
8261 goto do_addone_branch_i;
8262 case M_BGE_I:
8263 s = "slti";
8264 s2 = "bteqz";
8265 s3 = "x,8";
8266 goto do_branch_i;
8267 case M_BGEU_I:
8268 s = "sltiu";
8269 s2 = "bteqz";
8270 s3 = "x,8";
8271 goto do_branch_i;
8272 case M_BGT_I:
8273 s = "slti";
8274 s2 = "bteqz";
8275 s3 = "x,8";
8276 goto do_addone_branch_i;
8277 case M_BGTU_I:
8278 s = "sltiu";
8279 s2 = "bteqz";
8280 s3 = "x,8";
8281
8282 do_addone_branch_i:
8283 if (imm_expr.X_op != O_constant)
8284 as_bad (_("Unsupported large constant"));
8285 ++imm_expr.X_add_number;
8286
8287 do_branch_i:
67c0d1eb
RS
8288 macro_build (&imm_expr, s, s3, xreg);
8289 macro_build (&offset_expr, s2, "p");
252b5132
RH
8290 break;
8291
8292 case M_ABS:
8293 expr1.X_add_number = 0;
67c0d1eb 8294 macro_build (&expr1, "slti", "x,8", yreg);
252b5132 8295 if (xreg != yreg)
67c0d1eb 8296 move_register (xreg, yreg);
252b5132 8297 expr1.X_add_number = 2;
67c0d1eb
RS
8298 macro_build (&expr1, "bteqz", "p");
8299 macro_build (NULL, "neg", "x,w", xreg, xreg);
252b5132
RH
8300 }
8301}
8302
8303/* For consistency checking, verify that all bits are specified either
8304 by the match/mask part of the instruction definition, or by the
8305 operand list. */
8306static int
17a2f251 8307validate_mips_insn (const struct mips_opcode *opc)
252b5132
RH
8308{
8309 const char *p = opc->args;
8310 char c;
8311 unsigned long used_bits = opc->mask;
8312
8313 if ((used_bits & opc->match) != opc->match)
8314 {
8315 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
8316 opc->name, opc->args);
8317 return 0;
8318 }
8319#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
8320 while (*p)
8321 switch (c = *p++)
8322 {
8323 case ',': break;
8324 case '(': break;
8325 case ')': break;
af7ee8bf
CD
8326 case '+':
8327 switch (c = *p++)
8328 {
9bcd4f99
TS
8329 case '1': USE_BITS (OP_MASK_UDI1, OP_SH_UDI1); break;
8330 case '2': USE_BITS (OP_MASK_UDI2, OP_SH_UDI2); break;
8331 case '3': USE_BITS (OP_MASK_UDI3, OP_SH_UDI3); break;
8332 case '4': USE_BITS (OP_MASK_UDI4, OP_SH_UDI4); break;
af7ee8bf
CD
8333 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8334 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
8335 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
bbcc0807
CD
8336 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
8337 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
5f74bc13
CD
8338 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8339 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
8340 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
8341 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
8342 case 'I': break;
ef2e4d86
CF
8343 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8344 case 'T': USE_BITS (OP_MASK_RT, OP_SH_RT);
8345 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
bb35fb24
NC
8346 case 'x': USE_BITS (OP_MASK_BBITIND, OP_SH_BBITIND); break;
8347 case 'X': USE_BITS (OP_MASK_BBITIND, OP_SH_BBITIND); break;
8348 case 'p': USE_BITS (OP_MASK_CINSPOS, OP_SH_CINSPOS); break;
8349 case 'P': USE_BITS (OP_MASK_CINSPOS, OP_SH_CINSPOS); break;
dd3cbb7e 8350 case 'Q': USE_BITS (OP_MASK_SEQI, OP_SH_SEQI); break;
bb35fb24
NC
8351 case 's': USE_BITS (OP_MASK_CINSLM1, OP_SH_CINSLM1); break;
8352 case 'S': USE_BITS (OP_MASK_CINSLM1, OP_SH_CINSLM1); break;
8353
af7ee8bf
CD
8354 default:
8355 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8356 c, opc->name, opc->args);
8357 return 0;
8358 }
8359 break;
252b5132
RH
8360 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8361 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8362 case 'A': break;
4372b673 8363 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
8364 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
8365 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
8366 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8367 case 'F': break;
8368 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 8369 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 8370 case 'I': break;
e972090a 8371 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
af7ee8bf 8372 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
8373 case 'L': break;
8374 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
8375 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
deec1734
CD
8376 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
8377 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
8378 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
8379 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
8380 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8381 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8382 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8383 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
deec1734
CD
8384 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
8385 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8386 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
8387 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
8388 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8389 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
8390 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
8391 case 'f': break;
8392 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
8393 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8394 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8395 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
8396 case 'l': break;
8397 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8398 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8399 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
8400 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8401 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8402 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8403 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8404 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8405 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8406 case 'x': break;
8407 case 'z': break;
8408 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
8409 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
8410 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
60b63b72
RS
8411 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
8412 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
8413 case '[': break;
8414 case ']': break;
8b082fb1 8415 case '2': USE_BITS (OP_MASK_BP, OP_SH_BP); break;
74cd071d
CF
8416 case '3': USE_BITS (OP_MASK_SA3, OP_SH_SA3); break;
8417 case '4': USE_BITS (OP_MASK_SA4, OP_SH_SA4); break;
8418 case '5': USE_BITS (OP_MASK_IMM8, OP_SH_IMM8); break;
8419 case '6': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8420 case '7': USE_BITS (OP_MASK_DSPACC, OP_SH_DSPACC); break;
8421 case '8': USE_BITS (OP_MASK_WRDSP, OP_SH_WRDSP); break;
8422 case '9': USE_BITS (OP_MASK_DSPACC_S, OP_SH_DSPACC_S);break;
8423 case '0': USE_BITS (OP_MASK_DSPSFT, OP_SH_DSPSFT); break;
8424 case '\'': USE_BITS (OP_MASK_RDDSP, OP_SH_RDDSP); break;
8425 case ':': USE_BITS (OP_MASK_DSPSFT_7, OP_SH_DSPSFT_7);break;
8426 case '@': USE_BITS (OP_MASK_IMM10, OP_SH_IMM10); break;
ef2e4d86
CF
8427 case '!': USE_BITS (OP_MASK_MT_U, OP_SH_MT_U); break;
8428 case '$': USE_BITS (OP_MASK_MT_H, OP_SH_MT_H); break;
8429 case '*': USE_BITS (OP_MASK_MTACC_T, OP_SH_MTACC_T); break;
8430 case '&': USE_BITS (OP_MASK_MTACC_D, OP_SH_MTACC_D); break;
8431 case 'g': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
8432 default:
8433 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
8434 c, opc->name, opc->args);
8435 return 0;
8436 }
8437#undef USE_BITS
8438 if (used_bits != 0xffffffff)
8439 {
8440 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
8441 ~used_bits & 0xffffffff, opc->name, opc->args);
8442 return 0;
8443 }
8444 return 1;
8445}
8446
9bcd4f99
TS
8447/* UDI immediates. */
8448struct mips_immed {
8449 char type;
8450 unsigned int shift;
8451 unsigned long mask;
8452 const char * desc;
8453};
8454
8455static const struct mips_immed mips_immed[] = {
8456 { '1', OP_SH_UDI1, OP_MASK_UDI1, 0},
8457 { '2', OP_SH_UDI2, OP_MASK_UDI2, 0},
8458 { '3', OP_SH_UDI3, OP_MASK_UDI3, 0},
8459 { '4', OP_SH_UDI4, OP_MASK_UDI4, 0},
8460 { 0,0,0,0 }
8461};
8462
7455baf8
TS
8463/* Check whether an odd floating-point register is allowed. */
8464static int
8465mips_oddfpreg_ok (const struct mips_opcode *insn, int argnum)
8466{
8467 const char *s = insn->name;
8468
8469 if (insn->pinfo == INSN_MACRO)
8470 /* Let a macro pass, we'll catch it later when it is expanded. */
8471 return 1;
8472
8473 if (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa))
8474 {
8475 /* Allow odd registers for single-precision ops. */
8476 switch (insn->pinfo & (FP_S | FP_D))
8477 {
8478 case FP_S:
8479 case 0:
8480 return 1; /* both single precision - ok */
8481 case FP_D:
8482 return 0; /* both double precision - fail */
8483 default:
8484 break;
8485 }
8486
8487 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
8488 s = strchr (insn->name, '.');
8489 if (argnum == 2)
8490 s = s != NULL ? strchr (s + 1, '.') : NULL;
8491 return (s != NULL && (s[1] == 'w' || s[1] == 's'));
8492 }
8493
8494 /* Single-precision coprocessor loads and moves are OK too. */
8495 if ((insn->pinfo & FP_S)
8496 && (insn->pinfo & (INSN_COPROC_MEMORY_DELAY | INSN_STORE_MEMORY
8497 | INSN_LOAD_COPROC_DELAY | INSN_COPROC_MOVE_DELAY)))
8498 return 1;
8499
8500 return 0;
8501}
8502
252b5132
RH
8503/* This routine assembles an instruction into its binary format. As a
8504 side effect, it sets one of the global variables imm_reloc or
8505 offset_reloc to the type of relocation to do if one of the operands
8506 is an address expression. */
8507
8508static void
17a2f251 8509mips_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
8510{
8511 char *s;
8512 const char *args;
43841e91 8513 char c = 0;
252b5132
RH
8514 struct mips_opcode *insn;
8515 char *argsStart;
8516 unsigned int regno;
8517 unsigned int lastregno = 0;
af7ee8bf 8518 unsigned int lastpos = 0;
071742cf 8519 unsigned int limlo, limhi;
252b5132
RH
8520 char *s_reset;
8521 char save_c = 0;
74cd071d 8522 offsetT min_range, max_range;
707bfff6
TS
8523 int argnum;
8524 unsigned int rtype;
252b5132
RH
8525
8526 insn_error = NULL;
8527
8528 /* If the instruction contains a '.', we first try to match an instruction
8529 including the '.'. Then we try again without the '.'. */
8530 insn = NULL;
3882b010 8531 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
252b5132
RH
8532 continue;
8533
8534 /* If we stopped on whitespace, then replace the whitespace with null for
8535 the call to hash_find. Save the character we replaced just in case we
8536 have to re-parse the instruction. */
3882b010 8537 if (ISSPACE (*s))
252b5132
RH
8538 {
8539 save_c = *s;
8540 *s++ = '\0';
8541 }
bdaaa2e1 8542
252b5132
RH
8543 insn = (struct mips_opcode *) hash_find (op_hash, str);
8544
8545 /* If we didn't find the instruction in the opcode table, try again, but
8546 this time with just the instruction up to, but not including the
8547 first '.'. */
8548 if (insn == NULL)
8549 {
bdaaa2e1 8550 /* Restore the character we overwrite above (if any). */
252b5132
RH
8551 if (save_c)
8552 *(--s) = save_c;
8553
8554 /* Scan up to the first '.' or whitespace. */
3882b010
L
8555 for (s = str;
8556 *s != '\0' && *s != '.' && !ISSPACE (*s);
8557 ++s)
252b5132
RH
8558 continue;
8559
8560 /* If we did not find a '.', then we can quit now. */
8561 if (*s != '.')
8562 {
8563 insn_error = "unrecognized opcode";
8564 return;
8565 }
8566
8567 /* Lookup the instruction in the hash table. */
8568 *s++ = '\0';
8569 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8570 {
8571 insn_error = "unrecognized opcode";
8572 return;
8573 }
252b5132
RH
8574 }
8575
8576 argsStart = s;
8577 for (;;)
8578 {
b34976b6 8579 bfd_boolean ok;
252b5132
RH
8580
8581 assert (strcmp (insn->name, str) == 0);
8582
037b32b9 8583 ok = is_opcode_valid (insn, FALSE);
252b5132
RH
8584 if (! ok)
8585 {
8586 if (insn + 1 < &mips_opcodes[NUMOPCODES]
8587 && strcmp (insn->name, insn[1].name) == 0)
8588 {
8589 ++insn;
8590 continue;
8591 }
252b5132 8592 else
beae10d5 8593 {
268f6bed
L
8594 if (!insn_error)
8595 {
8596 static char buf[100];
fef14a42
TS
8597 sprintf (buf,
8598 _("opcode not supported on this processor: %s (%s)"),
8599 mips_cpu_info_from_arch (mips_opts.arch)->name,
8600 mips_cpu_info_from_isa (mips_opts.isa)->name);
268f6bed
L
8601 insn_error = buf;
8602 }
8603 if (save_c)
8604 *(--s) = save_c;
2bd7f1f3 8605 return;
252b5132 8606 }
252b5132
RH
8607 }
8608
1e915849 8609 create_insn (ip, insn);
268f6bed 8610 insn_error = NULL;
707bfff6 8611 argnum = 1;
252b5132
RH
8612 for (args = insn->args;; ++args)
8613 {
deec1734
CD
8614 int is_mdmx;
8615
ad8d3bb3 8616 s += strspn (s, " \t");
deec1734 8617 is_mdmx = 0;
252b5132
RH
8618 switch (*args)
8619 {
8620 case '\0': /* end of args */
8621 if (*s == '\0')
8622 return;
8623 break;
8624
8b082fb1
TS
8625 case '2': /* dsp 2-bit unsigned immediate in bit 11 */
8626 my_getExpression (&imm_expr, s);
8627 check_absolute_expr (ip, &imm_expr);
8628 if ((unsigned long) imm_expr.X_add_number != 1
8629 && (unsigned long) imm_expr.X_add_number != 3)
8630 {
8631 as_bad (_("BALIGN immediate not 1 or 3 (%lu)"),
8632 (unsigned long) imm_expr.X_add_number);
8633 }
8634 INSERT_OPERAND (BP, *ip, imm_expr.X_add_number);
8635 imm_expr.X_op = O_absent;
8636 s = expr_end;
8637 continue;
8638
74cd071d
CF
8639 case '3': /* dsp 3-bit unsigned immediate in bit 21 */
8640 my_getExpression (&imm_expr, s);
8641 check_absolute_expr (ip, &imm_expr);
8642 if (imm_expr.X_add_number & ~OP_MASK_SA3)
8643 {
a9e24354
TS
8644 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8645 OP_MASK_SA3, (unsigned long) imm_expr.X_add_number);
74cd071d 8646 }
a9e24354 8647 INSERT_OPERAND (SA3, *ip, imm_expr.X_add_number);
74cd071d
CF
8648 imm_expr.X_op = O_absent;
8649 s = expr_end;
8650 continue;
8651
8652 case '4': /* dsp 4-bit unsigned immediate in bit 21 */
8653 my_getExpression (&imm_expr, s);
8654 check_absolute_expr (ip, &imm_expr);
8655 if (imm_expr.X_add_number & ~OP_MASK_SA4)
8656 {
a9e24354
TS
8657 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8658 OP_MASK_SA4, (unsigned long) imm_expr.X_add_number);
74cd071d 8659 }
a9e24354 8660 INSERT_OPERAND (SA4, *ip, imm_expr.X_add_number);
74cd071d
CF
8661 imm_expr.X_op = O_absent;
8662 s = expr_end;
8663 continue;
8664
8665 case '5': /* dsp 8-bit unsigned immediate in bit 16 */
8666 my_getExpression (&imm_expr, s);
8667 check_absolute_expr (ip, &imm_expr);
8668 if (imm_expr.X_add_number & ~OP_MASK_IMM8)
8669 {
a9e24354
TS
8670 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8671 OP_MASK_IMM8, (unsigned long) imm_expr.X_add_number);
74cd071d 8672 }
a9e24354 8673 INSERT_OPERAND (IMM8, *ip, imm_expr.X_add_number);
74cd071d
CF
8674 imm_expr.X_op = O_absent;
8675 s = expr_end;
8676 continue;
8677
8678 case '6': /* dsp 5-bit unsigned immediate in bit 21 */
8679 my_getExpression (&imm_expr, s);
8680 check_absolute_expr (ip, &imm_expr);
8681 if (imm_expr.X_add_number & ~OP_MASK_RS)
8682 {
a9e24354
TS
8683 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8684 OP_MASK_RS, (unsigned long) imm_expr.X_add_number);
74cd071d 8685 }
a9e24354 8686 INSERT_OPERAND (RS, *ip, imm_expr.X_add_number);
74cd071d
CF
8687 imm_expr.X_op = O_absent;
8688 s = expr_end;
8689 continue;
8690
8691 case '7': /* four dsp accumulators in bits 11,12 */
8692 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8693 s[3] >= '0' && s[3] <= '3')
8694 {
8695 regno = s[3] - '0';
8696 s += 4;
a9e24354 8697 INSERT_OPERAND (DSPACC, *ip, regno);
74cd071d
CF
8698 continue;
8699 }
8700 else
8701 as_bad (_("Invalid dsp acc register"));
8702 break;
8703
8704 case '8': /* dsp 6-bit unsigned immediate in bit 11 */
8705 my_getExpression (&imm_expr, s);
8706 check_absolute_expr (ip, &imm_expr);
8707 if (imm_expr.X_add_number & ~OP_MASK_WRDSP)
8708 {
a9e24354
TS
8709 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8710 OP_MASK_WRDSP,
8711 (unsigned long) imm_expr.X_add_number);
74cd071d 8712 }
a9e24354 8713 INSERT_OPERAND (WRDSP, *ip, imm_expr.X_add_number);
74cd071d
CF
8714 imm_expr.X_op = O_absent;
8715 s = expr_end;
8716 continue;
8717
8718 case '9': /* four dsp accumulators in bits 21,22 */
8719 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8720 s[3] >= '0' && s[3] <= '3')
8721 {
8722 regno = s[3] - '0';
8723 s += 4;
a9e24354 8724 INSERT_OPERAND (DSPACC_S, *ip, regno);
74cd071d
CF
8725 continue;
8726 }
8727 else
8728 as_bad (_("Invalid dsp acc register"));
8729 break;
8730
8731 case '0': /* dsp 6-bit signed immediate in bit 20 */
8732 my_getExpression (&imm_expr, s);
8733 check_absolute_expr (ip, &imm_expr);
8734 min_range = -((OP_MASK_DSPSFT + 1) >> 1);
8735 max_range = ((OP_MASK_DSPSFT + 1) >> 1) - 1;
8736 if (imm_expr.X_add_number < min_range ||
8737 imm_expr.X_add_number > max_range)
8738 {
a9e24354
TS
8739 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
8740 (long) min_range, (long) max_range,
8741 (long) imm_expr.X_add_number);
74cd071d 8742 }
a9e24354 8743 INSERT_OPERAND (DSPSFT, *ip, imm_expr.X_add_number);
74cd071d
CF
8744 imm_expr.X_op = O_absent;
8745 s = expr_end;
8746 continue;
8747
8748 case '\'': /* dsp 6-bit unsigned immediate in bit 16 */
8749 my_getExpression (&imm_expr, s);
8750 check_absolute_expr (ip, &imm_expr);
8751 if (imm_expr.X_add_number & ~OP_MASK_RDDSP)
8752 {
a9e24354
TS
8753 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8754 OP_MASK_RDDSP,
8755 (unsigned long) imm_expr.X_add_number);
74cd071d 8756 }
a9e24354 8757 INSERT_OPERAND (RDDSP, *ip, imm_expr.X_add_number);
74cd071d
CF
8758 imm_expr.X_op = O_absent;
8759 s = expr_end;
8760 continue;
8761
8762 case ':': /* dsp 7-bit signed immediate in bit 19 */
8763 my_getExpression (&imm_expr, s);
8764 check_absolute_expr (ip, &imm_expr);
8765 min_range = -((OP_MASK_DSPSFT_7 + 1) >> 1);
8766 max_range = ((OP_MASK_DSPSFT_7 + 1) >> 1) - 1;
8767 if (imm_expr.X_add_number < min_range ||
8768 imm_expr.X_add_number > max_range)
8769 {
a9e24354
TS
8770 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
8771 (long) min_range, (long) max_range,
8772 (long) imm_expr.X_add_number);
74cd071d 8773 }
a9e24354 8774 INSERT_OPERAND (DSPSFT_7, *ip, imm_expr.X_add_number);
74cd071d
CF
8775 imm_expr.X_op = O_absent;
8776 s = expr_end;
8777 continue;
8778
8779 case '@': /* dsp 10-bit signed immediate in bit 16 */
8780 my_getExpression (&imm_expr, s);
8781 check_absolute_expr (ip, &imm_expr);
8782 min_range = -((OP_MASK_IMM10 + 1) >> 1);
8783 max_range = ((OP_MASK_IMM10 + 1) >> 1) - 1;
8784 if (imm_expr.X_add_number < min_range ||
8785 imm_expr.X_add_number > max_range)
8786 {
a9e24354
TS
8787 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
8788 (long) min_range, (long) max_range,
8789 (long) imm_expr.X_add_number);
74cd071d 8790 }
a9e24354 8791 INSERT_OPERAND (IMM10, *ip, imm_expr.X_add_number);
74cd071d
CF
8792 imm_expr.X_op = O_absent;
8793 s = expr_end;
8794 continue;
8795
a9e24354 8796 case '!': /* MT usermode flag bit. */
ef2e4d86
CF
8797 my_getExpression (&imm_expr, s);
8798 check_absolute_expr (ip, &imm_expr);
8799 if (imm_expr.X_add_number & ~OP_MASK_MT_U)
a9e24354
TS
8800 as_bad (_("MT usermode bit not 0 or 1 (%lu)"),
8801 (unsigned long) imm_expr.X_add_number);
8802 INSERT_OPERAND (MT_U, *ip, imm_expr.X_add_number);
ef2e4d86
CF
8803 imm_expr.X_op = O_absent;
8804 s = expr_end;
8805 continue;
8806
a9e24354 8807 case '$': /* MT load high flag bit. */
ef2e4d86
CF
8808 my_getExpression (&imm_expr, s);
8809 check_absolute_expr (ip, &imm_expr);
8810 if (imm_expr.X_add_number & ~OP_MASK_MT_H)
a9e24354
TS
8811 as_bad (_("MT load high bit not 0 or 1 (%lu)"),
8812 (unsigned long) imm_expr.X_add_number);
8813 INSERT_OPERAND (MT_H, *ip, imm_expr.X_add_number);
ef2e4d86
CF
8814 imm_expr.X_op = O_absent;
8815 s = expr_end;
8816 continue;
8817
8818 case '*': /* four dsp accumulators in bits 18,19 */
8819 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8820 s[3] >= '0' && s[3] <= '3')
8821 {
8822 regno = s[3] - '0';
8823 s += 4;
a9e24354 8824 INSERT_OPERAND (MTACC_T, *ip, regno);
ef2e4d86
CF
8825 continue;
8826 }
8827 else
8828 as_bad (_("Invalid dsp/smartmips acc register"));
8829 break;
8830
8831 case '&': /* four dsp accumulators in bits 13,14 */
8832 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8833 s[3] >= '0' && s[3] <= '3')
8834 {
8835 regno = s[3] - '0';
8836 s += 4;
a9e24354 8837 INSERT_OPERAND (MTACC_D, *ip, regno);
ef2e4d86
CF
8838 continue;
8839 }
8840 else
8841 as_bad (_("Invalid dsp/smartmips acc register"));
8842 break;
8843
252b5132 8844 case ',':
a339155f 8845 ++argnum;
252b5132
RH
8846 if (*s++ == *args)
8847 continue;
8848 s--;
8849 switch (*++args)
8850 {
8851 case 'r':
8852 case 'v':
bf12938e 8853 INSERT_OPERAND (RS, *ip, lastregno);
252b5132
RH
8854 continue;
8855
8856 case 'w':
bf12938e 8857 INSERT_OPERAND (RT, *ip, lastregno);
38487616
TS
8858 continue;
8859
252b5132 8860 case 'W':
bf12938e 8861 INSERT_OPERAND (FT, *ip, lastregno);
252b5132
RH
8862 continue;
8863
8864 case 'V':
bf12938e 8865 INSERT_OPERAND (FS, *ip, lastregno);
252b5132
RH
8866 continue;
8867 }
8868 break;
8869
8870 case '(':
8871 /* Handle optional base register.
8872 Either the base register is omitted or
bdaaa2e1 8873 we must have a left paren. */
252b5132
RH
8874 /* This is dependent on the next operand specifier
8875 is a base register specification. */
8876 assert (args[1] == 'b' || args[1] == '5'
8877 || args[1] == '-' || args[1] == '4');
8878 if (*s == '\0')
8879 return;
8880
8881 case ')': /* these must match exactly */
60b63b72
RS
8882 case '[':
8883 case ']':
252b5132
RH
8884 if (*s++ == *args)
8885 continue;
8886 break;
8887
af7ee8bf
CD
8888 case '+': /* Opcode extension character. */
8889 switch (*++args)
8890 {
9bcd4f99
TS
8891 case '1': /* UDI immediates. */
8892 case '2':
8893 case '3':
8894 case '4':
8895 {
8896 const struct mips_immed *imm = mips_immed;
8897
8898 while (imm->type && imm->type != *args)
8899 ++imm;
8900 if (! imm->type)
8901 internalError ();
8902 my_getExpression (&imm_expr, s);
8903 check_absolute_expr (ip, &imm_expr);
8904 if ((unsigned long) imm_expr.X_add_number & ~imm->mask)
8905 {
8906 as_warn (_("Illegal %s number (%lu, 0x%lx)"),
8907 imm->desc ? imm->desc : ip->insn_mo->name,
8908 (unsigned long) imm_expr.X_add_number,
8909 (unsigned long) imm_expr.X_add_number);
8910 imm_expr.X_add_number &= imm->mask;
8911 }
8912 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
8913 << imm->shift);
8914 imm_expr.X_op = O_absent;
8915 s = expr_end;
8916 }
8917 continue;
8918
071742cf
CD
8919 case 'A': /* ins/ext position, becomes LSB. */
8920 limlo = 0;
8921 limhi = 31;
5f74bc13
CD
8922 goto do_lsb;
8923 case 'E':
8924 limlo = 32;
8925 limhi = 63;
8926 goto do_lsb;
8927do_lsb:
071742cf
CD
8928 my_getExpression (&imm_expr, s);
8929 check_absolute_expr (ip, &imm_expr);
8930 if ((unsigned long) imm_expr.X_add_number < limlo
8931 || (unsigned long) imm_expr.X_add_number > limhi)
8932 {
8933 as_bad (_("Improper position (%lu)"),
8934 (unsigned long) imm_expr.X_add_number);
8935 imm_expr.X_add_number = limlo;
8936 }
8937 lastpos = imm_expr.X_add_number;
bf12938e 8938 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number);
071742cf
CD
8939 imm_expr.X_op = O_absent;
8940 s = expr_end;
8941 continue;
8942
8943 case 'B': /* ins size, becomes MSB. */
8944 limlo = 1;
8945 limhi = 32;
5f74bc13
CD
8946 goto do_msb;
8947 case 'F':
8948 limlo = 33;
8949 limhi = 64;
8950 goto do_msb;
8951do_msb:
071742cf
CD
8952 my_getExpression (&imm_expr, s);
8953 check_absolute_expr (ip, &imm_expr);
8954 /* Check for negative input so that small negative numbers
8955 will not succeed incorrectly. The checks against
8956 (pos+size) transitively check "size" itself,
8957 assuming that "pos" is reasonable. */
8958 if ((long) imm_expr.X_add_number < 0
8959 || ((unsigned long) imm_expr.X_add_number
8960 + lastpos) < limlo
8961 || ((unsigned long) imm_expr.X_add_number
8962 + lastpos) > limhi)
8963 {
8964 as_bad (_("Improper insert size (%lu, position %lu)"),
8965 (unsigned long) imm_expr.X_add_number,
8966 (unsigned long) lastpos);
8967 imm_expr.X_add_number = limlo - lastpos;
8968 }
bf12938e
RS
8969 INSERT_OPERAND (INSMSB, *ip,
8970 lastpos + imm_expr.X_add_number - 1);
071742cf
CD
8971 imm_expr.X_op = O_absent;
8972 s = expr_end;
8973 continue;
8974
8975 case 'C': /* ext size, becomes MSBD. */
8976 limlo = 1;
8977 limhi = 32;
5f74bc13
CD
8978 goto do_msbd;
8979 case 'G':
8980 limlo = 33;
8981 limhi = 64;
8982 goto do_msbd;
8983 case 'H':
8984 limlo = 33;
8985 limhi = 64;
8986 goto do_msbd;
8987do_msbd:
071742cf
CD
8988 my_getExpression (&imm_expr, s);
8989 check_absolute_expr (ip, &imm_expr);
8990 /* Check for negative input so that small negative numbers
8991 will not succeed incorrectly. The checks against
8992 (pos+size) transitively check "size" itself,
8993 assuming that "pos" is reasonable. */
8994 if ((long) imm_expr.X_add_number < 0
8995 || ((unsigned long) imm_expr.X_add_number
8996 + lastpos) < limlo
8997 || ((unsigned long) imm_expr.X_add_number
8998 + lastpos) > limhi)
8999 {
9000 as_bad (_("Improper extract size (%lu, position %lu)"),
9001 (unsigned long) imm_expr.X_add_number,
9002 (unsigned long) lastpos);
9003 imm_expr.X_add_number = limlo - lastpos;
9004 }
bf12938e 9005 INSERT_OPERAND (EXTMSBD, *ip, imm_expr.X_add_number - 1);
071742cf
CD
9006 imm_expr.X_op = O_absent;
9007 s = expr_end;
9008 continue;
af7ee8bf 9009
bbcc0807
CD
9010 case 'D':
9011 /* +D is for disassembly only; never match. */
9012 break;
9013
5f74bc13
CD
9014 case 'I':
9015 /* "+I" is like "I", except that imm2_expr is used. */
9016 my_getExpression (&imm2_expr, s);
9017 if (imm2_expr.X_op != O_big
9018 && imm2_expr.X_op != O_constant)
9019 insn_error = _("absolute expression required");
9ee2a2d4
MR
9020 if (HAVE_32BIT_GPRS)
9021 normalize_constant_expr (&imm2_expr);
5f74bc13
CD
9022 s = expr_end;
9023 continue;
9024
707bfff6 9025 case 'T': /* Coprocessor register. */
ef2e4d86
CF
9026 /* +T is for disassembly only; never match. */
9027 break;
9028
707bfff6 9029 case 't': /* Coprocessor register number. */
ef2e4d86
CF
9030 if (s[0] == '$' && ISDIGIT (s[1]))
9031 {
9032 ++s;
9033 regno = 0;
9034 do
9035 {
9036 regno *= 10;
9037 regno += *s - '0';
9038 ++s;
9039 }
9040 while (ISDIGIT (*s));
9041 if (regno > 31)
9042 as_bad (_("Invalid register number (%d)"), regno);
9043 else
9044 {
a9e24354 9045 INSERT_OPERAND (RT, *ip, regno);
ef2e4d86
CF
9046 continue;
9047 }
9048 }
9049 else
9050 as_bad (_("Invalid coprocessor 0 register number"));
9051 break;
9052
bb35fb24
NC
9053 case 'x':
9054 /* bbit[01] and bbit[01]32 bit index. Give error if index
9055 is not in the valid range. */
9056 my_getExpression (&imm_expr, s);
9057 check_absolute_expr (ip, &imm_expr);
9058 if ((unsigned) imm_expr.X_add_number > 31)
9059 {
9060 as_bad (_("Improper bit index (%lu)"),
9061 (unsigned long) imm_expr.X_add_number);
9062 imm_expr.X_add_number = 0;
9063 }
9064 INSERT_OPERAND (BBITIND, *ip, imm_expr.X_add_number);
9065 imm_expr.X_op = O_absent;
9066 s = expr_end;
9067 continue;
9068
9069 case 'X':
9070 /* bbit[01] bit index when bbit is used but we generate
9071 bbit[01]32 because the index is over 32. Move to the
9072 next candidate if index is not in the valid range. */
9073 my_getExpression (&imm_expr, s);
9074 check_absolute_expr (ip, &imm_expr);
9075 if ((unsigned) imm_expr.X_add_number < 32
9076 || (unsigned) imm_expr.X_add_number > 63)
9077 break;
9078 INSERT_OPERAND (BBITIND, *ip, imm_expr.X_add_number - 32);
9079 imm_expr.X_op = O_absent;
9080 s = expr_end;
9081 continue;
9082
9083 case 'p':
9084 /* cins, cins32, exts and exts32 position field. Give error
9085 if it's not in the valid range. */
9086 my_getExpression (&imm_expr, s);
9087 check_absolute_expr (ip, &imm_expr);
9088 if ((unsigned) imm_expr.X_add_number > 31)
9089 {
9090 as_bad (_("Improper position (%lu)"),
9091 (unsigned long) imm_expr.X_add_number);
9092 imm_expr.X_add_number = 0;
9093 }
9094 /* Make the pos explicit to simplify +S. */
9095 lastpos = imm_expr.X_add_number + 32;
9096 INSERT_OPERAND (CINSPOS, *ip, imm_expr.X_add_number);
9097 imm_expr.X_op = O_absent;
9098 s = expr_end;
9099 continue;
9100
9101 case 'P':
9102 /* cins, cins32, exts and exts32 position field. Move to
9103 the next candidate if it's not in the valid range. */
9104 my_getExpression (&imm_expr, s);
9105 check_absolute_expr (ip, &imm_expr);
9106 if ((unsigned) imm_expr.X_add_number < 32
9107 || (unsigned) imm_expr.X_add_number > 63)
9108 break;
9109 lastpos = imm_expr.X_add_number;
9110 INSERT_OPERAND (CINSPOS, *ip, imm_expr.X_add_number - 32);
9111 imm_expr.X_op = O_absent;
9112 s = expr_end;
9113 continue;
9114
9115 case 's':
9116 /* cins and exts length-minus-one field. */
9117 my_getExpression (&imm_expr, s);
9118 check_absolute_expr (ip, &imm_expr);
9119 if ((unsigned long) imm_expr.X_add_number > 31)
9120 {
9121 as_bad (_("Improper size (%lu)"),
9122 (unsigned long) imm_expr.X_add_number);
9123 imm_expr.X_add_number = 0;
9124 }
9125 INSERT_OPERAND (CINSLM1, *ip, imm_expr.X_add_number);
9126 imm_expr.X_op = O_absent;
9127 s = expr_end;
9128 continue;
9129
9130 case 'S':
9131 /* cins32/exts32 and cins/exts aliasing cint32/exts32
9132 length-minus-one field. */
9133 my_getExpression (&imm_expr, s);
9134 check_absolute_expr (ip, &imm_expr);
9135 if ((long) imm_expr.X_add_number < 0
9136 || (unsigned long) imm_expr.X_add_number + lastpos > 63)
9137 {
9138 as_bad (_("Improper size (%lu)"),
9139 (unsigned long) imm_expr.X_add_number);
9140 imm_expr.X_add_number = 0;
9141 }
9142 INSERT_OPERAND (CINSLM1, *ip, imm_expr.X_add_number);
9143 imm_expr.X_op = O_absent;
9144 s = expr_end;
9145 continue;
9146
dd3cbb7e
NC
9147 case 'Q':
9148 /* seqi/snei immediate field. */
9149 my_getExpression (&imm_expr, s);
9150 check_absolute_expr (ip, &imm_expr);
9151 if ((long) imm_expr.X_add_number < -512
9152 || (long) imm_expr.X_add_number >= 512)
9153 {
9154 as_bad (_("Improper immediate (%ld)"),
9155 (long) imm_expr.X_add_number);
9156 imm_expr.X_add_number = 0;
9157 }
9158 INSERT_OPERAND (SEQI, *ip, imm_expr.X_add_number);
9159 imm_expr.X_op = O_absent;
9160 s = expr_end;
9161 continue;
9162
af7ee8bf
CD
9163 default:
9164 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
9165 *args, insn->name, insn->args);
9166 /* Further processing is fruitless. */
9167 return;
9168 }
9169 break;
9170
252b5132
RH
9171 case '<': /* must be at least one digit */
9172 /*
9173 * According to the manual, if the shift amount is greater
b6ff326e
KH
9174 * than 31 or less than 0, then the shift amount should be
9175 * mod 32. In reality the mips assembler issues an error.
252b5132
RH
9176 * We issue a warning and mask out all but the low 5 bits.
9177 */
9178 my_getExpression (&imm_expr, s);
9179 check_absolute_expr (ip, &imm_expr);
9180 if ((unsigned long) imm_expr.X_add_number > 31)
bf12938e
RS
9181 as_warn (_("Improper shift amount (%lu)"),
9182 (unsigned long) imm_expr.X_add_number);
9183 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number);
252b5132
RH
9184 imm_expr.X_op = O_absent;
9185 s = expr_end;
9186 continue;
9187
9188 case '>': /* shift amount minus 32 */
9189 my_getExpression (&imm_expr, s);
9190 check_absolute_expr (ip, &imm_expr);
9191 if ((unsigned long) imm_expr.X_add_number < 32
9192 || (unsigned long) imm_expr.X_add_number > 63)
9193 break;
bf12938e 9194 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number - 32);
252b5132
RH
9195 imm_expr.X_op = O_absent;
9196 s = expr_end;
9197 continue;
9198
252b5132
RH
9199 case 'k': /* cache code */
9200 case 'h': /* prefx code */
9201 my_getExpression (&imm_expr, s);
9202 check_absolute_expr (ip, &imm_expr);
9203 if ((unsigned long) imm_expr.X_add_number > 31)
bf12938e
RS
9204 as_warn (_("Invalid value for `%s' (%lu)"),
9205 ip->insn_mo->name,
9206 (unsigned long) imm_expr.X_add_number);
252b5132 9207 if (*args == 'k')
bf12938e 9208 INSERT_OPERAND (CACHE, *ip, imm_expr.X_add_number);
252b5132 9209 else
bf12938e 9210 INSERT_OPERAND (PREFX, *ip, imm_expr.X_add_number);
252b5132
RH
9211 imm_expr.X_op = O_absent;
9212 s = expr_end;
9213 continue;
9214
9215 case 'c': /* break code */
9216 my_getExpression (&imm_expr, s);
9217 check_absolute_expr (ip, &imm_expr);
a9e24354
TS
9218 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE)
9219 as_warn (_("Code for %s not in range 0..1023 (%lu)"),
9220 ip->insn_mo->name,
bf12938e
RS
9221 (unsigned long) imm_expr.X_add_number);
9222 INSERT_OPERAND (CODE, *ip, imm_expr.X_add_number);
252b5132
RH
9223 imm_expr.X_op = O_absent;
9224 s = expr_end;
9225 continue;
9226
9227 case 'q': /* lower break code */
9228 my_getExpression (&imm_expr, s);
9229 check_absolute_expr (ip, &imm_expr);
a9e24354
TS
9230 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE2)
9231 as_warn (_("Lower code for %s not in range 0..1023 (%lu)"),
9232 ip->insn_mo->name,
bf12938e
RS
9233 (unsigned long) imm_expr.X_add_number);
9234 INSERT_OPERAND (CODE2, *ip, imm_expr.X_add_number);
252b5132
RH
9235 imm_expr.X_op = O_absent;
9236 s = expr_end;
9237 continue;
9238
4372b673 9239 case 'B': /* 20-bit syscall/break code. */
156c2f8b 9240 my_getExpression (&imm_expr, s);
156c2f8b 9241 check_absolute_expr (ip, &imm_expr);
793b27f4 9242 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
a9e24354
TS
9243 as_warn (_("Code for %s not in range 0..1048575 (%lu)"),
9244 ip->insn_mo->name,
793b27f4 9245 (unsigned long) imm_expr.X_add_number);
bf12938e 9246 INSERT_OPERAND (CODE20, *ip, imm_expr.X_add_number);
252b5132
RH
9247 imm_expr.X_op = O_absent;
9248 s = expr_end;
9249 continue;
9250
98d3f06f 9251 case 'C': /* Coprocessor code */
beae10d5 9252 my_getExpression (&imm_expr, s);
252b5132 9253 check_absolute_expr (ip, &imm_expr);
a9e24354 9254 if ((unsigned long) imm_expr.X_add_number > OP_MASK_COPZ)
252b5132 9255 {
793b27f4
TS
9256 as_warn (_("Coproccesor code > 25 bits (%lu)"),
9257 (unsigned long) imm_expr.X_add_number);
a9e24354 9258 imm_expr.X_add_number &= OP_MASK_COPZ;
252b5132 9259 }
a9e24354 9260 INSERT_OPERAND (COPZ, *ip, imm_expr.X_add_number);
beae10d5
KH
9261 imm_expr.X_op = O_absent;
9262 s = expr_end;
9263 continue;
252b5132 9264
4372b673
NC
9265 case 'J': /* 19-bit wait code. */
9266 my_getExpression (&imm_expr, s);
9267 check_absolute_expr (ip, &imm_expr);
793b27f4 9268 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
a9e24354
TS
9269 {
9270 as_warn (_("Illegal 19-bit code (%lu)"),
9271 (unsigned long) imm_expr.X_add_number);
9272 imm_expr.X_add_number &= OP_MASK_CODE19;
9273 }
bf12938e 9274 INSERT_OPERAND (CODE19, *ip, imm_expr.X_add_number);
4372b673
NC
9275 imm_expr.X_op = O_absent;
9276 s = expr_end;
9277 continue;
9278
707bfff6 9279 case 'P': /* Performance register. */
beae10d5 9280 my_getExpression (&imm_expr, s);
252b5132 9281 check_absolute_expr (ip, &imm_expr);
beae10d5 9282 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
bf12938e
RS
9283 as_warn (_("Invalid performance register (%lu)"),
9284 (unsigned long) imm_expr.X_add_number);
9285 INSERT_OPERAND (PERFREG, *ip, imm_expr.X_add_number);
beae10d5
KH
9286 imm_expr.X_op = O_absent;
9287 s = expr_end;
9288 continue;
252b5132 9289
707bfff6
TS
9290 case 'G': /* Coprocessor destination register. */
9291 if (((ip->insn_opcode >> OP_SH_OP) & OP_MASK_OP) == OP_OP_COP0)
9292 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_CP0, &regno);
9293 else
9294 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
a9e24354 9295 INSERT_OPERAND (RD, *ip, regno);
707bfff6
TS
9296 if (ok)
9297 {
9298 lastregno = regno;
9299 continue;
9300 }
9301 else
9302 break;
9303
252b5132
RH
9304 case 'b': /* base register */
9305 case 'd': /* destination register */
9306 case 's': /* source register */
9307 case 't': /* target register */
9308 case 'r': /* both target and source */
9309 case 'v': /* both dest and source */
9310 case 'w': /* both dest and target */
9311 case 'E': /* coprocessor target register */
af7ee8bf 9312 case 'K': /* 'rdhwr' destination register */
252b5132
RH
9313 case 'x': /* ignore register name */
9314 case 'z': /* must be zero register */
4372b673 9315 case 'U': /* destination register (clo/clz). */
ef2e4d86 9316 case 'g': /* coprocessor destination register */
707bfff6
TS
9317 s_reset = s;
9318 if (*args == 'E' || *args == 'K')
9319 ok = reg_lookup (&s, RTYPE_NUM, &regno);
9320 else
9321 {
9322 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
741fe287
MR
9323 if (regno == AT && mips_opts.at)
9324 {
9325 if (mips_opts.at == ATREG)
9326 as_warn (_("used $at without \".set noat\""));
9327 else
9328 as_warn (_("used $%u with \".set at=$%u\""),
9329 regno, mips_opts.at);
9330 }
707bfff6
TS
9331 }
9332 if (ok)
252b5132 9333 {
252b5132
RH
9334 c = *args;
9335 if (*s == ' ')
f9419b05 9336 ++s;
252b5132
RH
9337 if (args[1] != *s)
9338 {
9339 if (c == 'r' || c == 'v' || c == 'w')
9340 {
9341 regno = lastregno;
9342 s = s_reset;
f9419b05 9343 ++args;
252b5132
RH
9344 }
9345 }
9346 /* 'z' only matches $0. */
9347 if (c == 'z' && regno != 0)
9348 break;
9349
e7c604dd
CM
9350 if (c == 's' && !strcmp (ip->insn_mo->name, "jalr"))
9351 {
9352 if (regno == lastregno)
9353 {
9354 insn_error = _("source and destinationations must be different");
9355 continue;
9356 }
9357 if (regno == 31 && lastregno == 0)
9358 {
9359 insn_error = _("a destination register must be supplied");
9360 continue;
9361 }
9362 }
bdaaa2e1
KH
9363 /* Now that we have assembled one operand, we use the args string
9364 * to figure out where it goes in the instruction. */
252b5132
RH
9365 switch (c)
9366 {
9367 case 'r':
9368 case 's':
9369 case 'v':
9370 case 'b':
bf12938e 9371 INSERT_OPERAND (RS, *ip, regno);
252b5132
RH
9372 break;
9373 case 'd':
9374 case 'G':
af7ee8bf 9375 case 'K':
ef2e4d86 9376 case 'g':
bf12938e 9377 INSERT_OPERAND (RD, *ip, regno);
252b5132 9378 break;
4372b673 9379 case 'U':
bf12938e
RS
9380 INSERT_OPERAND (RD, *ip, regno);
9381 INSERT_OPERAND (RT, *ip, regno);
4372b673 9382 break;
252b5132
RH
9383 case 'w':
9384 case 't':
9385 case 'E':
bf12938e 9386 INSERT_OPERAND (RT, *ip, regno);
252b5132
RH
9387 break;
9388 case 'x':
9389 /* This case exists because on the r3000 trunc
9390 expands into a macro which requires a gp
9391 register. On the r6000 or r4000 it is
9392 assembled into a single instruction which
9393 ignores the register. Thus the insn version
9394 is MIPS_ISA2 and uses 'x', and the macro
9395 version is MIPS_ISA1 and uses 't'. */
9396 break;
9397 case 'z':
9398 /* This case is for the div instruction, which
9399 acts differently if the destination argument
9400 is $0. This only matches $0, and is checked
9401 outside the switch. */
9402 break;
9403 case 'D':
9404 /* Itbl operand; not yet implemented. FIXME ?? */
9405 break;
9406 /* What about all other operands like 'i', which
9407 can be specified in the opcode table? */
9408 }
9409 lastregno = regno;
9410 continue;
9411 }
252b5132
RH
9412 switch (*args++)
9413 {
9414 case 'r':
9415 case 'v':
bf12938e 9416 INSERT_OPERAND (RS, *ip, lastregno);
252b5132
RH
9417 continue;
9418 case 'w':
bf12938e 9419 INSERT_OPERAND (RT, *ip, lastregno);
252b5132
RH
9420 continue;
9421 }
9422 break;
9423
deec1734
CD
9424 case 'O': /* MDMX alignment immediate constant. */
9425 my_getExpression (&imm_expr, s);
9426 check_absolute_expr (ip, &imm_expr);
9427 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
bf12938e
RS
9428 as_warn ("Improper align amount (%ld), using low bits",
9429 (long) imm_expr.X_add_number);
9430 INSERT_OPERAND (ALN, *ip, imm_expr.X_add_number);
deec1734
CD
9431 imm_expr.X_op = O_absent;
9432 s = expr_end;
9433 continue;
9434
9435 case 'Q': /* MDMX vector, element sel, or const. */
9436 if (s[0] != '$')
9437 {
9438 /* MDMX Immediate. */
9439 my_getExpression (&imm_expr, s);
9440 check_absolute_expr (ip, &imm_expr);
9441 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
bf12938e
RS
9442 as_warn (_("Invalid MDMX Immediate (%ld)"),
9443 (long) imm_expr.X_add_number);
9444 INSERT_OPERAND (FT, *ip, imm_expr.X_add_number);
deec1734
CD
9445 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
9446 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
9447 else
9448 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
deec1734
CD
9449 imm_expr.X_op = O_absent;
9450 s = expr_end;
9451 continue;
9452 }
9453 /* Not MDMX Immediate. Fall through. */
9454 case 'X': /* MDMX destination register. */
9455 case 'Y': /* MDMX source register. */
9456 case 'Z': /* MDMX target register. */
9457 is_mdmx = 1;
252b5132
RH
9458 case 'D': /* floating point destination register */
9459 case 'S': /* floating point source register */
9460 case 'T': /* floating point target register */
9461 case 'R': /* floating point source register */
9462 case 'V':
9463 case 'W':
707bfff6
TS
9464 rtype = RTYPE_FPU;
9465 if (is_mdmx
9466 || (mips_opts.ase_mdmx
9467 && (ip->insn_mo->pinfo & FP_D)
9468 && (ip->insn_mo->pinfo & (INSN_COPROC_MOVE_DELAY
9469 | INSN_COPROC_MEMORY_DELAY
9470 | INSN_LOAD_COPROC_DELAY
9471 | INSN_LOAD_MEMORY_DELAY
9472 | INSN_STORE_MEMORY))))
9473 rtype |= RTYPE_VEC;
252b5132 9474 s_reset = s;
707bfff6 9475 if (reg_lookup (&s, rtype, &regno))
252b5132 9476 {
252b5132 9477 if ((regno & 1) != 0
ca4e0257 9478 && HAVE_32BIT_FPRS
7455baf8 9479 && ! mips_oddfpreg_ok (ip->insn_mo, argnum))
252b5132
RH
9480 as_warn (_("Float register should be even, was %d"),
9481 regno);
9482
9483 c = *args;
9484 if (*s == ' ')
f9419b05 9485 ++s;
252b5132
RH
9486 if (args[1] != *s)
9487 {
9488 if (c == 'V' || c == 'W')
9489 {
9490 regno = lastregno;
9491 s = s_reset;
f9419b05 9492 ++args;
252b5132
RH
9493 }
9494 }
9495 switch (c)
9496 {
9497 case 'D':
deec1734 9498 case 'X':
bf12938e 9499 INSERT_OPERAND (FD, *ip, regno);
252b5132
RH
9500 break;
9501 case 'V':
9502 case 'S':
deec1734 9503 case 'Y':
bf12938e 9504 INSERT_OPERAND (FS, *ip, regno);
252b5132 9505 break;
deec1734
CD
9506 case 'Q':
9507 /* This is like 'Z', but also needs to fix the MDMX
9508 vector/scalar select bits. Note that the
9509 scalar immediate case is handled above. */
9510 if (*s == '[')
9511 {
9512 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
9513 int max_el = (is_qh ? 3 : 7);
9514 s++;
9515 my_getExpression(&imm_expr, s);
9516 check_absolute_expr (ip, &imm_expr);
9517 s = expr_end;
9518 if (imm_expr.X_add_number > max_el)
9519 as_bad(_("Bad element selector %ld"),
9520 (long) imm_expr.X_add_number);
9521 imm_expr.X_add_number &= max_el;
9522 ip->insn_opcode |= (imm_expr.X_add_number
9523 << (OP_SH_VSEL +
9524 (is_qh ? 2 : 1)));
01a3f561 9525 imm_expr.X_op = O_absent;
deec1734
CD
9526 if (*s != ']')
9527 as_warn(_("Expecting ']' found '%s'"), s);
9528 else
9529 s++;
9530 }
9531 else
9532 {
9533 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
9534 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
9535 << OP_SH_VSEL);
9536 else
9537 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
9538 OP_SH_VSEL);
9539 }
9540 /* Fall through */
252b5132
RH
9541 case 'W':
9542 case 'T':
deec1734 9543 case 'Z':
bf12938e 9544 INSERT_OPERAND (FT, *ip, regno);
252b5132
RH
9545 break;
9546 case 'R':
bf12938e 9547 INSERT_OPERAND (FR, *ip, regno);
252b5132
RH
9548 break;
9549 }
9550 lastregno = regno;
9551 continue;
9552 }
9553
252b5132
RH
9554 switch (*args++)
9555 {
9556 case 'V':
bf12938e 9557 INSERT_OPERAND (FS, *ip, lastregno);
252b5132
RH
9558 continue;
9559 case 'W':
bf12938e 9560 INSERT_OPERAND (FT, *ip, lastregno);
252b5132
RH
9561 continue;
9562 }
9563 break;
9564
9565 case 'I':
9566 my_getExpression (&imm_expr, s);
9567 if (imm_expr.X_op != O_big
9568 && imm_expr.X_op != O_constant)
9569 insn_error = _("absolute expression required");
9ee2a2d4
MR
9570 if (HAVE_32BIT_GPRS)
9571 normalize_constant_expr (&imm_expr);
252b5132
RH
9572 s = expr_end;
9573 continue;
9574
9575 case 'A':
9576 my_getExpression (&offset_expr, s);
2051e8c4 9577 normalize_address_expr (&offset_expr);
f6688943 9578 *imm_reloc = BFD_RELOC_32;
252b5132
RH
9579 s = expr_end;
9580 continue;
9581
9582 case 'F':
9583 case 'L':
9584 case 'f':
9585 case 'l':
9586 {
9587 int f64;
ca4e0257 9588 int using_gprs;
252b5132
RH
9589 char *save_in;
9590 char *err;
9591 unsigned char temp[8];
9592 int len;
9593 unsigned int length;
9594 segT seg;
9595 subsegT subseg;
9596 char *p;
9597
9598 /* These only appear as the last operand in an
9599 instruction, and every instruction that accepts
9600 them in any variant accepts them in all variants.
9601 This means we don't have to worry about backing out
9602 any changes if the instruction does not match.
9603
9604 The difference between them is the size of the
9605 floating point constant and where it goes. For 'F'
9606 and 'L' the constant is 64 bits; for 'f' and 'l' it
9607 is 32 bits. Where the constant is placed is based
9608 on how the MIPS assembler does things:
9609 F -- .rdata
9610 L -- .lit8
9611 f -- immediate value
9612 l -- .lit4
9613
9614 The .lit4 and .lit8 sections are only used if
9615 permitted by the -G argument.
9616
ca4e0257
RS
9617 The code below needs to know whether the target register
9618 is 32 or 64 bits wide. It relies on the fact 'f' and
9619 'F' are used with GPR-based instructions and 'l' and
9620 'L' are used with FPR-based instructions. */
252b5132
RH
9621
9622 f64 = *args == 'F' || *args == 'L';
ca4e0257 9623 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
9624
9625 save_in = input_line_pointer;
9626 input_line_pointer = s;
9627 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
9628 length = len;
9629 s = input_line_pointer;
9630 input_line_pointer = save_in;
9631 if (err != NULL && *err != '\0')
9632 {
9633 as_bad (_("Bad floating point constant: %s"), err);
9634 memset (temp, '\0', sizeof temp);
9635 length = f64 ? 8 : 4;
9636 }
9637
156c2f8b 9638 assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
9639
9640 if (*args == 'f'
9641 || (*args == 'l'
3e722fb5 9642 && (g_switch_value < 4
252b5132
RH
9643 || (temp[0] == 0 && temp[1] == 0)
9644 || (temp[2] == 0 && temp[3] == 0))))
9645 {
9646 imm_expr.X_op = O_constant;
9647 if (! target_big_endian)
9648 imm_expr.X_add_number = bfd_getl32 (temp);
9649 else
9650 imm_expr.X_add_number = bfd_getb32 (temp);
9651 }
9652 else if (length > 4
119d663a 9653 && ! mips_disable_float_construction
ca4e0257
RS
9654 /* Constants can only be constructed in GPRs and
9655 copied to FPRs if the GPRs are at least as wide
9656 as the FPRs. Force the constant into memory if
9657 we are using 64-bit FPRs but the GPRs are only
9658 32 bits wide. */
9659 && (using_gprs
9660 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
9661 && ((temp[0] == 0 && temp[1] == 0)
9662 || (temp[2] == 0 && temp[3] == 0))
9663 && ((temp[4] == 0 && temp[5] == 0)
9664 || (temp[6] == 0 && temp[7] == 0)))
9665 {
ca4e0257
RS
9666 /* The value is simple enough to load with a couple of
9667 instructions. If using 32-bit registers, set
9668 imm_expr to the high order 32 bits and offset_expr to
9669 the low order 32 bits. Otherwise, set imm_expr to
9670 the entire 64 bit constant. */
9671 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
9672 {
9673 imm_expr.X_op = O_constant;
9674 offset_expr.X_op = O_constant;
9675 if (! target_big_endian)
9676 {
9677 imm_expr.X_add_number = bfd_getl32 (temp + 4);
9678 offset_expr.X_add_number = bfd_getl32 (temp);
9679 }
9680 else
9681 {
9682 imm_expr.X_add_number = bfd_getb32 (temp);
9683 offset_expr.X_add_number = bfd_getb32 (temp + 4);
9684 }
9685 if (offset_expr.X_add_number == 0)
9686 offset_expr.X_op = O_absent;
9687 }
9688 else if (sizeof (imm_expr.X_add_number) > 4)
9689 {
9690 imm_expr.X_op = O_constant;
9691 if (! target_big_endian)
9692 imm_expr.X_add_number = bfd_getl64 (temp);
9693 else
9694 imm_expr.X_add_number = bfd_getb64 (temp);
9695 }
9696 else
9697 {
9698 imm_expr.X_op = O_big;
9699 imm_expr.X_add_number = 4;
9700 if (! target_big_endian)
9701 {
9702 generic_bignum[0] = bfd_getl16 (temp);
9703 generic_bignum[1] = bfd_getl16 (temp + 2);
9704 generic_bignum[2] = bfd_getl16 (temp + 4);
9705 generic_bignum[3] = bfd_getl16 (temp + 6);
9706 }
9707 else
9708 {
9709 generic_bignum[0] = bfd_getb16 (temp + 6);
9710 generic_bignum[1] = bfd_getb16 (temp + 4);
9711 generic_bignum[2] = bfd_getb16 (temp + 2);
9712 generic_bignum[3] = bfd_getb16 (temp);
9713 }
9714 }
9715 }
9716 else
9717 {
9718 const char *newname;
9719 segT new_seg;
9720
9721 /* Switch to the right section. */
9722 seg = now_seg;
9723 subseg = now_subseg;
9724 switch (*args)
9725 {
9726 default: /* unused default case avoids warnings. */
9727 case 'L':
9728 newname = RDATA_SECTION_NAME;
3e722fb5 9729 if (g_switch_value >= 8)
252b5132
RH
9730 newname = ".lit8";
9731 break;
9732 case 'F':
3e722fb5 9733 newname = RDATA_SECTION_NAME;
252b5132
RH
9734 break;
9735 case 'l':
4d0d148d 9736 assert (g_switch_value >= 4);
252b5132
RH
9737 newname = ".lit4";
9738 break;
9739 }
9740 new_seg = subseg_new (newname, (subsegT) 0);
f43abd2b 9741 if (IS_ELF)
252b5132
RH
9742 bfd_set_section_flags (stdoutput, new_seg,
9743 (SEC_ALLOC
9744 | SEC_LOAD
9745 | SEC_READONLY
9746 | SEC_DATA));
9747 frag_align (*args == 'l' ? 2 : 3, 0, 0);
c41e87e3 9748 if (IS_ELF && strncmp (TARGET_OS, "elf", 3) != 0)
252b5132
RH
9749 record_alignment (new_seg, 4);
9750 else
9751 record_alignment (new_seg, *args == 'l' ? 2 : 3);
9752 if (seg == now_seg)
9753 as_bad (_("Can't use floating point insn in this section"));
9754
9755 /* Set the argument to the current address in the
9756 section. */
9757 offset_expr.X_op = O_symbol;
9758 offset_expr.X_add_symbol =
9759 symbol_new ("L0\001", now_seg,
9760 (valueT) frag_now_fix (), frag_now);
9761 offset_expr.X_add_number = 0;
9762
9763 /* Put the floating point number into the section. */
9764 p = frag_more ((int) length);
9765 memcpy (p, temp, length);
9766
9767 /* Switch back to the original section. */
9768 subseg_set (seg, subseg);
9769 }
9770 }
9771 continue;
9772
9773 case 'i': /* 16 bit unsigned immediate */
9774 case 'j': /* 16 bit signed immediate */
f6688943 9775 *imm_reloc = BFD_RELOC_LO16;
5e0116d5 9776 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
252b5132
RH
9777 {
9778 int more;
5e0116d5
RS
9779 offsetT minval, maxval;
9780
9781 more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9782 && strcmp (insn->name, insn[1].name) == 0);
9783
9784 /* If the expression was written as an unsigned number,
9785 only treat it as signed if there are no more
9786 alternatives. */
9787 if (more
9788 && *args == 'j'
9789 && sizeof (imm_expr.X_add_number) <= 4
9790 && imm_expr.X_op == O_constant
9791 && imm_expr.X_add_number < 0
9792 && imm_expr.X_unsigned
9793 && HAVE_64BIT_GPRS)
9794 break;
9795
9796 /* For compatibility with older assemblers, we accept
9797 0x8000-0xffff as signed 16-bit numbers when only
9798 signed numbers are allowed. */
9799 if (*args == 'i')
9800 minval = 0, maxval = 0xffff;
9801 else if (more)
9802 minval = -0x8000, maxval = 0x7fff;
252b5132 9803 else
5e0116d5
RS
9804 minval = -0x8000, maxval = 0xffff;
9805
9806 if (imm_expr.X_op != O_constant
9807 || imm_expr.X_add_number < minval
9808 || imm_expr.X_add_number > maxval)
252b5132
RH
9809 {
9810 if (more)
9811 break;
2ae7e77b
AH
9812 if (imm_expr.X_op == O_constant
9813 || imm_expr.X_op == O_big)
5e0116d5 9814 as_bad (_("expression out of range"));
252b5132
RH
9815 }
9816 }
9817 s = expr_end;
9818 continue;
9819
9820 case 'o': /* 16 bit offset */
5e0116d5
RS
9821 /* Check whether there is only a single bracketed expression
9822 left. If so, it must be the base register and the
9823 constant must be zero. */
9824 if (*s == '(' && strchr (s + 1, '(') == 0)
9825 {
9826 offset_expr.X_op = O_constant;
9827 offset_expr.X_add_number = 0;
9828 continue;
9829 }
252b5132
RH
9830
9831 /* If this value won't fit into a 16 bit offset, then go
9832 find a macro that will generate the 32 bit offset
afdbd6d0 9833 code pattern. */
5e0116d5 9834 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
252b5132
RH
9835 && (offset_expr.X_op != O_constant
9836 || offset_expr.X_add_number >= 0x8000
afdbd6d0 9837 || offset_expr.X_add_number < -0x8000))
252b5132
RH
9838 break;
9839
252b5132
RH
9840 s = expr_end;
9841 continue;
9842
9843 case 'p': /* pc relative offset */
0b25d3e6 9844 *offset_reloc = BFD_RELOC_16_PCREL_S2;
252b5132
RH
9845 my_getExpression (&offset_expr, s);
9846 s = expr_end;
9847 continue;
9848
9849 case 'u': /* upper 16 bits */
5e0116d5
RS
9850 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9851 && imm_expr.X_op == O_constant
9852 && (imm_expr.X_add_number < 0
9853 || imm_expr.X_add_number >= 0x10000))
252b5132
RH
9854 as_bad (_("lui expression not in range 0..65535"));
9855 s = expr_end;
9856 continue;
9857
9858 case 'a': /* 26 bit address */
9859 my_getExpression (&offset_expr, s);
9860 s = expr_end;
f6688943 9861 *offset_reloc = BFD_RELOC_MIPS_JMP;
252b5132
RH
9862 continue;
9863
9864 case 'N': /* 3 bit branch condition code */
9865 case 'M': /* 3 bit compare condition code */
707bfff6
TS
9866 rtype = RTYPE_CCC;
9867 if (ip->insn_mo->pinfo & (FP_D| FP_S))
9868 rtype |= RTYPE_FCC;
9869 if (!reg_lookup (&s, rtype, &regno))
252b5132 9870 break;
30c378fd
CD
9871 if ((strcmp(str + strlen(str) - 3, ".ps") == 0
9872 || strcmp(str + strlen(str) - 5, "any2f") == 0
9873 || strcmp(str + strlen(str) - 5, "any2t") == 0)
9874 && (regno & 1) != 0)
9875 as_warn(_("Condition code register should be even for %s, was %d"),
9876 str, regno);
9877 if ((strcmp(str + strlen(str) - 5, "any4f") == 0
9878 || strcmp(str + strlen(str) - 5, "any4t") == 0)
9879 && (regno & 3) != 0)
9880 as_warn(_("Condition code register should be 0 or 4 for %s, was %d"),
9881 str, regno);
252b5132 9882 if (*args == 'N')
bf12938e 9883 INSERT_OPERAND (BCC, *ip, regno);
252b5132 9884 else
bf12938e 9885 INSERT_OPERAND (CCC, *ip, regno);
beae10d5 9886 continue;
252b5132 9887
156c2f8b
NC
9888 case 'H':
9889 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9890 s += 2;
3882b010 9891 if (ISDIGIT (*s))
156c2f8b
NC
9892 {
9893 c = 0;
9894 do
9895 {
9896 c *= 10;
9897 c += *s - '0';
9898 ++s;
9899 }
3882b010 9900 while (ISDIGIT (*s));
156c2f8b
NC
9901 }
9902 else
9903 c = 8; /* Invalid sel value. */
9904
9905 if (c > 7)
9906 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9907 ip->insn_opcode |= c;
9908 continue;
9909
60b63b72
RS
9910 case 'e':
9911 /* Must be at least one digit. */
9912 my_getExpression (&imm_expr, s);
9913 check_absolute_expr (ip, &imm_expr);
9914
9915 if ((unsigned long) imm_expr.X_add_number
9916 > (unsigned long) OP_MASK_VECBYTE)
9917 {
9918 as_bad (_("bad byte vector index (%ld)"),
9919 (long) imm_expr.X_add_number);
9920 imm_expr.X_add_number = 0;
9921 }
9922
bf12938e 9923 INSERT_OPERAND (VECBYTE, *ip, imm_expr.X_add_number);
60b63b72
RS
9924 imm_expr.X_op = O_absent;
9925 s = expr_end;
9926 continue;
9927
9928 case '%':
9929 my_getExpression (&imm_expr, s);
9930 check_absolute_expr (ip, &imm_expr);
9931
9932 if ((unsigned long) imm_expr.X_add_number
9933 > (unsigned long) OP_MASK_VECALIGN)
9934 {
9935 as_bad (_("bad byte vector index (%ld)"),
9936 (long) imm_expr.X_add_number);
9937 imm_expr.X_add_number = 0;
9938 }
9939
bf12938e 9940 INSERT_OPERAND (VECALIGN, *ip, imm_expr.X_add_number);
60b63b72
RS
9941 imm_expr.X_op = O_absent;
9942 s = expr_end;
9943 continue;
9944
252b5132
RH
9945 default:
9946 as_bad (_("bad char = '%c'\n"), *args);
9947 internalError ();
9948 }
9949 break;
9950 }
9951 /* Args don't match. */
9952 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9953 !strcmp (insn->name, insn[1].name))
9954 {
9955 ++insn;
9956 s = argsStart;
268f6bed 9957 insn_error = _("illegal operands");
252b5132
RH
9958 continue;
9959 }
268f6bed 9960 if (save_c)
570de991 9961 *(--argsStart) = save_c;
252b5132
RH
9962 insn_error = _("illegal operands");
9963 return;
9964 }
9965}
9966
0499d65b
TS
9967#define SKIP_SPACE_TABS(S) { while (*(S) == ' ' || *(S) == '\t') ++(S); }
9968
252b5132
RH
9969/* This routine assembles an instruction into its binary format when
9970 assembling for the mips16. As a side effect, it sets one of the
9971 global variables imm_reloc or offset_reloc to the type of
9972 relocation to do if one of the operands is an address expression.
9973 It also sets mips16_small and mips16_ext if the user explicitly
9974 requested a small or extended instruction. */
9975
9976static void
17a2f251 9977mips16_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
9978{
9979 char *s;
9980 const char *args;
9981 struct mips_opcode *insn;
9982 char *argsstart;
9983 unsigned int regno;
9984 unsigned int lastregno = 0;
9985 char *s_reset;
d6f16593 9986 size_t i;
252b5132
RH
9987
9988 insn_error = NULL;
9989
b34976b6
AM
9990 mips16_small = FALSE;
9991 mips16_ext = FALSE;
252b5132 9992
3882b010 9993 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
9994 ;
9995 switch (*s)
9996 {
9997 case '\0':
9998 break;
9999
10000 case ' ':
10001 *s++ = '\0';
10002 break;
10003
10004 case '.':
10005 if (s[1] == 't' && s[2] == ' ')
10006 {
10007 *s = '\0';
b34976b6 10008 mips16_small = TRUE;
252b5132
RH
10009 s += 3;
10010 break;
10011 }
10012 else if (s[1] == 'e' && s[2] == ' ')
10013 {
10014 *s = '\0';
b34976b6 10015 mips16_ext = TRUE;
252b5132
RH
10016 s += 3;
10017 break;
10018 }
10019 /* Fall through. */
10020 default:
10021 insn_error = _("unknown opcode");
10022 return;
10023 }
10024
10025 if (mips_opts.noautoextend && ! mips16_ext)
b34976b6 10026 mips16_small = TRUE;
252b5132
RH
10027
10028 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
10029 {
10030 insn_error = _("unrecognized opcode");
10031 return;
10032 }
10033
10034 argsstart = s;
10035 for (;;)
10036 {
9b3f89ee
TS
10037 bfd_boolean ok;
10038
252b5132
RH
10039 assert (strcmp (insn->name, str) == 0);
10040
037b32b9 10041 ok = is_opcode_valid_16 (insn);
9b3f89ee
TS
10042 if (! ok)
10043 {
10044 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes]
10045 && strcmp (insn->name, insn[1].name) == 0)
10046 {
10047 ++insn;
10048 continue;
10049 }
10050 else
10051 {
10052 if (!insn_error)
10053 {
10054 static char buf[100];
10055 sprintf (buf,
10056 _("opcode not supported on this processor: %s (%s)"),
10057 mips_cpu_info_from_arch (mips_opts.arch)->name,
10058 mips_cpu_info_from_isa (mips_opts.isa)->name);
10059 insn_error = buf;
10060 }
10061 return;
10062 }
10063 }
10064
1e915849 10065 create_insn (ip, insn);
252b5132 10066 imm_expr.X_op = O_absent;
f6688943
TS
10067 imm_reloc[0] = BFD_RELOC_UNUSED;
10068 imm_reloc[1] = BFD_RELOC_UNUSED;
10069 imm_reloc[2] = BFD_RELOC_UNUSED;
5f74bc13 10070 imm2_expr.X_op = O_absent;
252b5132 10071 offset_expr.X_op = O_absent;
f6688943
TS
10072 offset_reloc[0] = BFD_RELOC_UNUSED;
10073 offset_reloc[1] = BFD_RELOC_UNUSED;
10074 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
10075 for (args = insn->args; 1; ++args)
10076 {
10077 int c;
10078
10079 if (*s == ' ')
10080 ++s;
10081
10082 /* In this switch statement we call break if we did not find
10083 a match, continue if we did find a match, or return if we
10084 are done. */
10085
10086 c = *args;
10087 switch (c)
10088 {
10089 case '\0':
10090 if (*s == '\0')
10091 {
10092 /* Stuff the immediate value in now, if we can. */
10093 if (imm_expr.X_op == O_constant
f6688943 10094 && *imm_reloc > BFD_RELOC_UNUSED
252b5132
RH
10095 && insn->pinfo != INSN_MACRO)
10096 {
d6f16593
MR
10097 valueT tmp;
10098
10099 switch (*offset_reloc)
10100 {
10101 case BFD_RELOC_MIPS16_HI16_S:
10102 tmp = (imm_expr.X_add_number + 0x8000) >> 16;
10103 break;
10104
10105 case BFD_RELOC_MIPS16_HI16:
10106 tmp = imm_expr.X_add_number >> 16;
10107 break;
10108
10109 case BFD_RELOC_MIPS16_LO16:
10110 tmp = ((imm_expr.X_add_number + 0x8000) & 0xffff)
10111 - 0x8000;
10112 break;
10113
10114 case BFD_RELOC_UNUSED:
10115 tmp = imm_expr.X_add_number;
10116 break;
10117
10118 default:
10119 internalError ();
10120 }
10121 *offset_reloc = BFD_RELOC_UNUSED;
10122
c4e7957c 10123 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
d6f16593 10124 tmp, TRUE, mips16_small,
252b5132
RH
10125 mips16_ext, &ip->insn_opcode,
10126 &ip->use_extend, &ip->extend);
10127 imm_expr.X_op = O_absent;
f6688943 10128 *imm_reloc = BFD_RELOC_UNUSED;
252b5132
RH
10129 }
10130
10131 return;
10132 }
10133 break;
10134
10135 case ',':
10136 if (*s++ == c)
10137 continue;
10138 s--;
10139 switch (*++args)
10140 {
10141 case 'v':
bf12938e 10142 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
252b5132
RH
10143 continue;
10144 case 'w':
bf12938e 10145 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
252b5132
RH
10146 continue;
10147 }
10148 break;
10149
10150 case '(':
10151 case ')':
10152 if (*s++ == c)
10153 continue;
10154 break;
10155
10156 case 'v':
10157 case 'w':
10158 if (s[0] != '$')
10159 {
10160 if (c == 'v')
bf12938e 10161 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
252b5132 10162 else
bf12938e 10163 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
252b5132
RH
10164 ++args;
10165 continue;
10166 }
10167 /* Fall through. */
10168 case 'x':
10169 case 'y':
10170 case 'z':
10171 case 'Z':
10172 case '0':
10173 case 'S':
10174 case 'R':
10175 case 'X':
10176 case 'Y':
707bfff6
TS
10177 s_reset = s;
10178 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
252b5132 10179 {
707bfff6 10180 if (c == 'v' || c == 'w')
85b51719 10181 {
707bfff6 10182 if (c == 'v')
a9e24354 10183 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
707bfff6 10184 else
a9e24354 10185 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
707bfff6
TS
10186 ++args;
10187 continue;
85b51719 10188 }
707bfff6 10189 break;
252b5132
RH
10190 }
10191
10192 if (*s == ' ')
10193 ++s;
10194 if (args[1] != *s)
10195 {
10196 if (c == 'v' || c == 'w')
10197 {
10198 regno = mips16_to_32_reg_map[lastregno];
10199 s = s_reset;
f9419b05 10200 ++args;
252b5132
RH
10201 }
10202 }
10203
10204 switch (c)
10205 {
10206 case 'x':
10207 case 'y':
10208 case 'z':
10209 case 'v':
10210 case 'w':
10211 case 'Z':
10212 regno = mips32_to_16_reg_map[regno];
10213 break;
10214
10215 case '0':
10216 if (regno != 0)
10217 regno = ILLEGAL_REG;
10218 break;
10219
10220 case 'S':
10221 if (regno != SP)
10222 regno = ILLEGAL_REG;
10223 break;
10224
10225 case 'R':
10226 if (regno != RA)
10227 regno = ILLEGAL_REG;
10228 break;
10229
10230 case 'X':
10231 case 'Y':
741fe287
MR
10232 if (regno == AT && mips_opts.at)
10233 {
10234 if (mips_opts.at == ATREG)
10235 as_warn (_("used $at without \".set noat\""));
10236 else
10237 as_warn (_("used $%u with \".set at=$%u\""),
10238 regno, mips_opts.at);
10239 }
252b5132
RH
10240 break;
10241
10242 default:
10243 internalError ();
10244 }
10245
10246 if (regno == ILLEGAL_REG)
10247 break;
10248
10249 switch (c)
10250 {
10251 case 'x':
10252 case 'v':
bf12938e 10253 MIPS16_INSERT_OPERAND (RX, *ip, regno);
252b5132
RH
10254 break;
10255 case 'y':
10256 case 'w':
bf12938e 10257 MIPS16_INSERT_OPERAND (RY, *ip, regno);
252b5132
RH
10258 break;
10259 case 'z':
bf12938e 10260 MIPS16_INSERT_OPERAND (RZ, *ip, regno);
252b5132
RH
10261 break;
10262 case 'Z':
bf12938e 10263 MIPS16_INSERT_OPERAND (MOVE32Z, *ip, regno);
252b5132
RH
10264 case '0':
10265 case 'S':
10266 case 'R':
10267 break;
10268 case 'X':
bf12938e 10269 MIPS16_INSERT_OPERAND (REGR32, *ip, regno);
252b5132
RH
10270 break;
10271 case 'Y':
10272 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
bf12938e 10273 MIPS16_INSERT_OPERAND (REG32R, *ip, regno);
252b5132
RH
10274 break;
10275 default:
10276 internalError ();
10277 }
10278
10279 lastregno = regno;
10280 continue;
10281
10282 case 'P':
10283 if (strncmp (s, "$pc", 3) == 0)
10284 {
10285 s += 3;
10286 continue;
10287 }
10288 break;
10289
252b5132
RH
10290 case '5':
10291 case 'H':
10292 case 'W':
10293 case 'D':
10294 case 'j':
252b5132
RH
10295 case 'V':
10296 case 'C':
10297 case 'U':
10298 case 'k':
10299 case 'K':
d6f16593
MR
10300 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
10301 if (i > 0)
252b5132 10302 {
d6f16593 10303 if (imm_expr.X_op != O_constant)
252b5132 10304 {
b34976b6 10305 mips16_ext = TRUE;
b34976b6 10306 ip->use_extend = TRUE;
252b5132 10307 ip->extend = 0;
252b5132 10308 }
d6f16593
MR
10309 else
10310 {
10311 /* We need to relax this instruction. */
10312 *offset_reloc = *imm_reloc;
10313 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
10314 }
10315 s = expr_end;
10316 continue;
252b5132 10317 }
d6f16593
MR
10318 *imm_reloc = BFD_RELOC_UNUSED;
10319 /* Fall through. */
10320 case '<':
10321 case '>':
10322 case '[':
10323 case ']':
10324 case '4':
10325 case '8':
10326 my_getExpression (&imm_expr, s);
252b5132
RH
10327 if (imm_expr.X_op == O_register)
10328 {
10329 /* What we thought was an expression turned out to
10330 be a register. */
10331
10332 if (s[0] == '(' && args[1] == '(')
10333 {
10334 /* It looks like the expression was omitted
10335 before a register indirection, which means
10336 that the expression is implicitly zero. We
10337 still set up imm_expr, so that we handle
10338 explicit extensions correctly. */
10339 imm_expr.X_op = O_constant;
10340 imm_expr.X_add_number = 0;
f6688943 10341 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
10342 continue;
10343 }
10344
10345 break;
10346 }
10347
10348 /* We need to relax this instruction. */
f6688943 10349 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
10350 s = expr_end;
10351 continue;
10352
10353 case 'p':
10354 case 'q':
10355 case 'A':
10356 case 'B':
10357 case 'E':
10358 /* We use offset_reloc rather than imm_reloc for the PC
10359 relative operands. This lets macros with both
10360 immediate and address operands work correctly. */
10361 my_getExpression (&offset_expr, s);
10362
10363 if (offset_expr.X_op == O_register)
10364 break;
10365
10366 /* We need to relax this instruction. */
f6688943 10367 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
10368 s = expr_end;
10369 continue;
10370
10371 case '6': /* break code */
10372 my_getExpression (&imm_expr, s);
10373 check_absolute_expr (ip, &imm_expr);
10374 if ((unsigned long) imm_expr.X_add_number > 63)
bf12938e
RS
10375 as_warn (_("Invalid value for `%s' (%lu)"),
10376 ip->insn_mo->name,
10377 (unsigned long) imm_expr.X_add_number);
10378 MIPS16_INSERT_OPERAND (IMM6, *ip, imm_expr.X_add_number);
252b5132
RH
10379 imm_expr.X_op = O_absent;
10380 s = expr_end;
10381 continue;
10382
10383 case 'a': /* 26 bit address */
10384 my_getExpression (&offset_expr, s);
10385 s = expr_end;
f6688943 10386 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132
RH
10387 ip->insn_opcode <<= 16;
10388 continue;
10389
10390 case 'l': /* register list for entry macro */
10391 case 'L': /* register list for exit macro */
10392 {
10393 int mask;
10394
10395 if (c == 'l')
10396 mask = 0;
10397 else
10398 mask = 7 << 3;
10399 while (*s != '\0')
10400 {
707bfff6 10401 unsigned int freg, reg1, reg2;
252b5132
RH
10402
10403 while (*s == ' ' || *s == ',')
10404 ++s;
707bfff6 10405 if (reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
252b5132 10406 freg = 0;
707bfff6
TS
10407 else if (reg_lookup (&s, RTYPE_FPU, &reg1))
10408 freg = 1;
252b5132
RH
10409 else
10410 {
707bfff6
TS
10411 as_bad (_("can't parse register list"));
10412 break;
252b5132
RH
10413 }
10414 if (*s == ' ')
10415 ++s;
10416 if (*s != '-')
10417 reg2 = reg1;
10418 else
10419 {
10420 ++s;
707bfff6
TS
10421 if (!reg_lookup (&s, freg ? RTYPE_FPU
10422 : (RTYPE_GP | RTYPE_NUM), &reg2))
252b5132 10423 {
707bfff6
TS
10424 as_bad (_("invalid register list"));
10425 break;
252b5132
RH
10426 }
10427 }
10428 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
10429 {
10430 mask &= ~ (7 << 3);
10431 mask |= 5 << 3;
10432 }
10433 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
10434 {
10435 mask &= ~ (7 << 3);
10436 mask |= 6 << 3;
10437 }
10438 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
10439 mask |= (reg2 - 3) << 3;
10440 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
10441 mask |= (reg2 - 15) << 1;
f9419b05 10442 else if (reg1 == RA && reg2 == RA)
252b5132
RH
10443 mask |= 1;
10444 else
10445 {
10446 as_bad (_("invalid register list"));
10447 break;
10448 }
10449 }
10450 /* The mask is filled in in the opcode table for the
10451 benefit of the disassembler. We remove it before
10452 applying the actual mask. */
10453 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
10454 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
10455 }
10456 continue;
10457
0499d65b
TS
10458 case 'm': /* Register list for save insn. */
10459 case 'M': /* Register list for restore insn. */
10460 {
10461 int opcode = 0;
10462 int framesz = 0, seen_framesz = 0;
10463 int args = 0, statics = 0, sregs = 0;
10464
10465 while (*s != '\0')
10466 {
10467 unsigned int reg1, reg2;
10468
10469 SKIP_SPACE_TABS (s);
10470 while (*s == ',')
10471 ++s;
10472 SKIP_SPACE_TABS (s);
10473
10474 my_getExpression (&imm_expr, s);
10475 if (imm_expr.X_op == O_constant)
10476 {
10477 /* Handle the frame size. */
10478 if (seen_framesz)
10479 {
10480 as_bad (_("more than one frame size in list"));
10481 break;
10482 }
10483 seen_framesz = 1;
10484 framesz = imm_expr.X_add_number;
10485 imm_expr.X_op = O_absent;
10486 s = expr_end;
10487 continue;
10488 }
10489
707bfff6 10490 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
0499d65b
TS
10491 {
10492 as_bad (_("can't parse register list"));
10493 break;
10494 }
0499d65b 10495
707bfff6
TS
10496 while (*s == ' ')
10497 ++s;
10498
0499d65b
TS
10499 if (*s != '-')
10500 reg2 = reg1;
10501 else
10502 {
10503 ++s;
707bfff6
TS
10504 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg2)
10505 || reg2 < reg1)
0499d65b
TS
10506 {
10507 as_bad (_("can't parse register list"));
10508 break;
10509 }
0499d65b
TS
10510 }
10511
10512 while (reg1 <= reg2)
10513 {
10514 if (reg1 >= 4 && reg1 <= 7)
10515 {
3a93f742 10516 if (!seen_framesz)
0499d65b
TS
10517 /* args $a0-$a3 */
10518 args |= 1 << (reg1 - 4);
10519 else
10520 /* statics $a0-$a3 */
10521 statics |= 1 << (reg1 - 4);
10522 }
10523 else if ((reg1 >= 16 && reg1 <= 23) || reg1 == 30)
10524 {
10525 /* $s0-$s8 */
10526 sregs |= 1 << ((reg1 == 30) ? 8 : (reg1 - 16));
10527 }
10528 else if (reg1 == 31)
10529 {
10530 /* Add $ra to insn. */
10531 opcode |= 0x40;
10532 }
10533 else
10534 {
10535 as_bad (_("unexpected register in list"));
10536 break;
10537 }
10538 if (++reg1 == 24)
10539 reg1 = 30;
10540 }
10541 }
10542
10543 /* Encode args/statics combination. */
10544 if (args & statics)
10545 as_bad (_("arg/static registers overlap"));
10546 else if (args == 0xf)
10547 /* All $a0-$a3 are args. */
10548 opcode |= MIPS16_ALL_ARGS << 16;
10549 else if (statics == 0xf)
10550 /* All $a0-$a3 are statics. */
10551 opcode |= MIPS16_ALL_STATICS << 16;
10552 else
10553 {
10554 int narg = 0, nstat = 0;
10555
10556 /* Count arg registers. */
10557 while (args & 0x1)
10558 {
10559 args >>= 1;
10560 narg++;
10561 }
10562 if (args != 0)
10563 as_bad (_("invalid arg register list"));
10564
10565 /* Count static registers. */
10566 while (statics & 0x8)
10567 {
10568 statics = (statics << 1) & 0xf;
10569 nstat++;
10570 }
10571 if (statics != 0)
10572 as_bad (_("invalid static register list"));
10573
10574 /* Encode args/statics. */
10575 opcode |= ((narg << 2) | nstat) << 16;
10576 }
10577
10578 /* Encode $s0/$s1. */
10579 if (sregs & (1 << 0)) /* $s0 */
10580 opcode |= 0x20;
10581 if (sregs & (1 << 1)) /* $s1 */
10582 opcode |= 0x10;
10583 sregs >>= 2;
10584
10585 if (sregs != 0)
10586 {
10587 /* Count regs $s2-$s8. */
10588 int nsreg = 0;
10589 while (sregs & 1)
10590 {
10591 sregs >>= 1;
10592 nsreg++;
10593 }
10594 if (sregs != 0)
10595 as_bad (_("invalid static register list"));
10596 /* Encode $s2-$s8. */
10597 opcode |= nsreg << 24;
10598 }
10599
10600 /* Encode frame size. */
10601 if (!seen_framesz)
10602 as_bad (_("missing frame size"));
10603 else if ((framesz & 7) != 0 || framesz < 0
10604 || framesz > 0xff * 8)
10605 as_bad (_("invalid frame size"));
10606 else if (framesz != 128 || (opcode >> 16) != 0)
10607 {
10608 framesz /= 8;
10609 opcode |= (((framesz & 0xf0) << 16)
10610 | (framesz & 0x0f));
10611 }
10612
10613 /* Finally build the instruction. */
10614 if ((opcode >> 16) != 0 || framesz == 0)
10615 {
10616 ip->use_extend = TRUE;
10617 ip->extend = opcode >> 16;
10618 }
10619 ip->insn_opcode |= opcode & 0x7f;
10620 }
10621 continue;
10622
252b5132
RH
10623 case 'e': /* extend code */
10624 my_getExpression (&imm_expr, s);
10625 check_absolute_expr (ip, &imm_expr);
10626 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
10627 {
10628 as_warn (_("Invalid value for `%s' (%lu)"),
10629 ip->insn_mo->name,
10630 (unsigned long) imm_expr.X_add_number);
10631 imm_expr.X_add_number &= 0x7ff;
10632 }
10633 ip->insn_opcode |= imm_expr.X_add_number;
10634 imm_expr.X_op = O_absent;
10635 s = expr_end;
10636 continue;
10637
10638 default:
10639 internalError ();
10640 }
10641 break;
10642 }
10643
10644 /* Args don't match. */
10645 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
10646 strcmp (insn->name, insn[1].name) == 0)
10647 {
10648 ++insn;
10649 s = argsstart;
10650 continue;
10651 }
10652
10653 insn_error = _("illegal operands");
10654
10655 return;
10656 }
10657}
10658
10659/* This structure holds information we know about a mips16 immediate
10660 argument type. */
10661
e972090a
NC
10662struct mips16_immed_operand
10663{
252b5132
RH
10664 /* The type code used in the argument string in the opcode table. */
10665 int type;
10666 /* The number of bits in the short form of the opcode. */
10667 int nbits;
10668 /* The number of bits in the extended form of the opcode. */
10669 int extbits;
10670 /* The amount by which the short form is shifted when it is used;
10671 for example, the sw instruction has a shift count of 2. */
10672 int shift;
10673 /* The amount by which the short form is shifted when it is stored
10674 into the instruction code. */
10675 int op_shift;
10676 /* Non-zero if the short form is unsigned. */
10677 int unsp;
10678 /* Non-zero if the extended form is unsigned. */
10679 int extu;
10680 /* Non-zero if the value is PC relative. */
10681 int pcrel;
10682};
10683
10684/* The mips16 immediate operand types. */
10685
10686static const struct mips16_immed_operand mips16_immed_operands[] =
10687{
10688 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
10689 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
10690 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
10691 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
10692 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
10693 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
10694 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
10695 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
10696 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
10697 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
10698 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
10699 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
10700 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
10701 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
10702 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
10703 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
10704 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10705 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10706 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
10707 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
10708 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
10709};
10710
10711#define MIPS16_NUM_IMMED \
10712 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
10713
10714/* Handle a mips16 instruction with an immediate value. This or's the
10715 small immediate value into *INSN. It sets *USE_EXTEND to indicate
10716 whether an extended value is needed; if one is needed, it sets
10717 *EXTEND to the value. The argument type is TYPE. The value is VAL.
10718 If SMALL is true, an unextended opcode was explicitly requested.
10719 If EXT is true, an extended opcode was explicitly requested. If
10720 WARN is true, warn if EXT does not match reality. */
10721
10722static void
17a2f251
TS
10723mips16_immed (char *file, unsigned int line, int type, offsetT val,
10724 bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
10725 unsigned long *insn, bfd_boolean *use_extend,
10726 unsigned short *extend)
252b5132 10727{
3994f87e 10728 const struct mips16_immed_operand *op;
252b5132 10729 int mintiny, maxtiny;
b34976b6 10730 bfd_boolean needext;
252b5132
RH
10731
10732 op = mips16_immed_operands;
10733 while (op->type != type)
10734 {
10735 ++op;
10736 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
10737 }
10738
10739 if (op->unsp)
10740 {
10741 if (type == '<' || type == '>' || type == '[' || type == ']')
10742 {
10743 mintiny = 1;
10744 maxtiny = 1 << op->nbits;
10745 }
10746 else
10747 {
10748 mintiny = 0;
10749 maxtiny = (1 << op->nbits) - 1;
10750 }
10751 }
10752 else
10753 {
10754 mintiny = - (1 << (op->nbits - 1));
10755 maxtiny = (1 << (op->nbits - 1)) - 1;
10756 }
10757
10758 /* Branch offsets have an implicit 0 in the lowest bit. */
10759 if (type == 'p' || type == 'q')
10760 val /= 2;
10761
10762 if ((val & ((1 << op->shift) - 1)) != 0
10763 || val < (mintiny << op->shift)
10764 || val > (maxtiny << op->shift))
b34976b6 10765 needext = TRUE;
252b5132 10766 else
b34976b6 10767 needext = FALSE;
252b5132
RH
10768
10769 if (warn && ext && ! needext)
beae10d5
KH
10770 as_warn_where (file, line,
10771 _("extended operand requested but not required"));
252b5132
RH
10772 if (small && needext)
10773 as_bad_where (file, line, _("invalid unextended operand value"));
10774
10775 if (small || (! ext && ! needext))
10776 {
10777 int insnval;
10778
b34976b6 10779 *use_extend = FALSE;
252b5132
RH
10780 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
10781 insnval <<= op->op_shift;
10782 *insn |= insnval;
10783 }
10784 else
10785 {
10786 long minext, maxext;
10787 int extval;
10788
10789 if (op->extu)
10790 {
10791 minext = 0;
10792 maxext = (1 << op->extbits) - 1;
10793 }
10794 else
10795 {
10796 minext = - (1 << (op->extbits - 1));
10797 maxext = (1 << (op->extbits - 1)) - 1;
10798 }
10799 if (val < minext || val > maxext)
10800 as_bad_where (file, line,
10801 _("operand value out of range for instruction"));
10802
b34976b6 10803 *use_extend = TRUE;
252b5132
RH
10804 if (op->extbits == 16)
10805 {
10806 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
10807 val &= 0x1f;
10808 }
10809 else if (op->extbits == 15)
10810 {
10811 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
10812 val &= 0xf;
10813 }
10814 else
10815 {
10816 extval = ((val & 0x1f) << 6) | (val & 0x20);
10817 val = 0;
10818 }
10819
10820 *extend = (unsigned short) extval;
10821 *insn |= val;
10822 }
10823}
10824\f
d6f16593 10825struct percent_op_match
ad8d3bb3 10826{
5e0116d5
RS
10827 const char *str;
10828 bfd_reloc_code_real_type reloc;
d6f16593
MR
10829};
10830
10831static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 10832{
5e0116d5 10833 {"%lo", BFD_RELOC_LO16},
ad8d3bb3 10834#ifdef OBJ_ELF
5e0116d5
RS
10835 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
10836 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
10837 {"%call16", BFD_RELOC_MIPS_CALL16},
10838 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
10839 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
10840 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
10841 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
10842 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
10843 {"%got", BFD_RELOC_MIPS_GOT16},
10844 {"%gp_rel", BFD_RELOC_GPREL16},
10845 {"%half", BFD_RELOC_16},
10846 {"%highest", BFD_RELOC_MIPS_HIGHEST},
10847 {"%higher", BFD_RELOC_MIPS_HIGHER},
10848 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
10849 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
10850 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
10851 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
10852 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
10853 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
10854 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
10855 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
ad8d3bb3 10856#endif
5e0116d5 10857 {"%hi", BFD_RELOC_HI16_S}
ad8d3bb3
TS
10858};
10859
d6f16593
MR
10860static const struct percent_op_match mips16_percent_op[] =
10861{
10862 {"%lo", BFD_RELOC_MIPS16_LO16},
10863 {"%gprel", BFD_RELOC_MIPS16_GPREL},
10864 {"%hi", BFD_RELOC_MIPS16_HI16_S}
10865};
10866
252b5132 10867
5e0116d5
RS
10868/* Return true if *STR points to a relocation operator. When returning true,
10869 move *STR over the operator and store its relocation code in *RELOC.
10870 Leave both *STR and *RELOC alone when returning false. */
10871
10872static bfd_boolean
17a2f251 10873parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 10874{
d6f16593
MR
10875 const struct percent_op_match *percent_op;
10876 size_t limit, i;
10877
10878 if (mips_opts.mips16)
10879 {
10880 percent_op = mips16_percent_op;
10881 limit = ARRAY_SIZE (mips16_percent_op);
10882 }
10883 else
10884 {
10885 percent_op = mips_percent_op;
10886 limit = ARRAY_SIZE (mips_percent_op);
10887 }
76b3015f 10888
d6f16593 10889 for (i = 0; i < limit; i++)
5e0116d5 10890 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 10891 {
3f98094e
DJ
10892 int len = strlen (percent_op[i].str);
10893
10894 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
10895 continue;
10896
5e0116d5
RS
10897 *str += strlen (percent_op[i].str);
10898 *reloc = percent_op[i].reloc;
394f9b3a 10899
5e0116d5
RS
10900 /* Check whether the output BFD supports this relocation.
10901 If not, issue an error and fall back on something safe. */
10902 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 10903 {
5e0116d5
RS
10904 as_bad ("relocation %s isn't supported by the current ABI",
10905 percent_op[i].str);
01a3f561 10906 *reloc = BFD_RELOC_UNUSED;
394f9b3a 10907 }
5e0116d5 10908 return TRUE;
394f9b3a 10909 }
5e0116d5 10910 return FALSE;
394f9b3a 10911}
ad8d3bb3 10912
ad8d3bb3 10913
5e0116d5
RS
10914/* Parse string STR as a 16-bit relocatable operand. Store the
10915 expression in *EP and the relocations in the array starting
10916 at RELOC. Return the number of relocation operators used.
ad8d3bb3 10917
01a3f561 10918 On exit, EXPR_END points to the first character after the expression. */
ad8d3bb3 10919
5e0116d5 10920static size_t
17a2f251
TS
10921my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
10922 char *str)
ad8d3bb3 10923{
5e0116d5
RS
10924 bfd_reloc_code_real_type reversed_reloc[3];
10925 size_t reloc_index, i;
09b8f35a
RS
10926 int crux_depth, str_depth;
10927 char *crux;
5e0116d5
RS
10928
10929 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
10930 in REVERSED_RELOC. End the loop with CRUX pointing to the start
10931 of the main expression and with CRUX_DEPTH containing the number
10932 of open brackets at that point. */
10933 reloc_index = -1;
10934 str_depth = 0;
10935 do
fb1b3232 10936 {
09b8f35a
RS
10937 reloc_index++;
10938 crux = str;
10939 crux_depth = str_depth;
10940
10941 /* Skip over whitespace and brackets, keeping count of the number
10942 of brackets. */
10943 while (*str == ' ' || *str == '\t' || *str == '(')
10944 if (*str++ == '(')
10945 str_depth++;
5e0116d5 10946 }
09b8f35a
RS
10947 while (*str == '%'
10948 && reloc_index < (HAVE_NEWABI ? 3 : 1)
10949 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 10950
09b8f35a 10951 my_getExpression (ep, crux);
5e0116d5 10952 str = expr_end;
394f9b3a 10953
5e0116d5 10954 /* Match every open bracket. */
09b8f35a 10955 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 10956 if (*str++ == ')')
09b8f35a 10957 crux_depth--;
394f9b3a 10958
09b8f35a 10959 if (crux_depth > 0)
5e0116d5 10960 as_bad ("unclosed '('");
394f9b3a 10961
5e0116d5 10962 expr_end = str;
252b5132 10963
01a3f561 10964 if (reloc_index != 0)
64bdfcaf
RS
10965 {
10966 prev_reloc_op_frag = frag_now;
10967 for (i = 0; i < reloc_index; i++)
10968 reloc[i] = reversed_reloc[reloc_index - 1 - i];
10969 }
fb1b3232 10970
5e0116d5 10971 return reloc_index;
252b5132
RH
10972}
10973
10974static void
17a2f251 10975my_getExpression (expressionS *ep, char *str)
252b5132
RH
10976{
10977 char *save_in;
98aa84af 10978 valueT val;
252b5132
RH
10979
10980 save_in = input_line_pointer;
10981 input_line_pointer = str;
10982 expression (ep);
10983 expr_end = input_line_pointer;
10984 input_line_pointer = save_in;
10985
10986 /* If we are in mips16 mode, and this is an expression based on `.',
10987 then we bump the value of the symbol by 1 since that is how other
10988 text symbols are handled. We don't bother to handle complex
10989 expressions, just `.' plus or minus a constant. */
10990 if (mips_opts.mips16
10991 && ep->X_op == O_symbol
10992 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10993 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
49309057
ILT
10994 && symbol_get_frag (ep->X_add_symbol) == frag_now
10995 && symbol_constant_p (ep->X_add_symbol)
98aa84af
AM
10996 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10997 S_SET_VALUE (ep->X_add_symbol, val + 1);
252b5132
RH
10998}
10999
252b5132 11000char *
17a2f251 11001md_atof (int type, char *litP, int *sizeP)
252b5132 11002{
499ac353 11003 return ieee_md_atof (type, litP, sizeP, target_big_endian);
252b5132
RH
11004}
11005
11006void
17a2f251 11007md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
11008{
11009 if (target_big_endian)
11010 number_to_chars_bigendian (buf, val, n);
11011 else
11012 number_to_chars_littleendian (buf, val, n);
11013}
11014\f
ae948b86 11015#ifdef OBJ_ELF
e013f690
TS
11016static int support_64bit_objects(void)
11017{
11018 const char **list, **l;
aa3d8fdf 11019 int yes;
e013f690
TS
11020
11021 list = bfd_target_list ();
11022 for (l = list; *l != NULL; l++)
11023#ifdef TE_TMIPS
11024 /* This is traditional mips */
11025 if (strcmp (*l, "elf64-tradbigmips") == 0
11026 || strcmp (*l, "elf64-tradlittlemips") == 0)
11027#else
11028 if (strcmp (*l, "elf64-bigmips") == 0
11029 || strcmp (*l, "elf64-littlemips") == 0)
11030#endif
11031 break;
aa3d8fdf 11032 yes = (*l != NULL);
e013f690 11033 free (list);
aa3d8fdf 11034 return yes;
e013f690 11035}
ae948b86 11036#endif /* OBJ_ELF */
e013f690 11037
78849248 11038const char *md_shortopts = "O::g::G:";
252b5132 11039
e972090a
NC
11040struct option md_longopts[] =
11041{
f9b4148d
CD
11042 /* Options which specify architecture. */
11043#define OPTION_ARCH_BASE (OPTION_MD_BASE)
11044#define OPTION_MARCH (OPTION_ARCH_BASE + 0)
11045 {"march", required_argument, NULL, OPTION_MARCH},
11046#define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
11047 {"mtune", required_argument, NULL, OPTION_MTUNE},
11048#define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
252b5132
RH
11049 {"mips0", no_argument, NULL, OPTION_MIPS1},
11050 {"mips1", no_argument, NULL, OPTION_MIPS1},
f9b4148d 11051#define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
252b5132 11052 {"mips2", no_argument, NULL, OPTION_MIPS2},
f9b4148d 11053#define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
252b5132 11054 {"mips3", no_argument, NULL, OPTION_MIPS3},
f9b4148d 11055#define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
252b5132 11056 {"mips4", no_argument, NULL, OPTION_MIPS4},
f9b4148d 11057#define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
ae948b86 11058 {"mips5", no_argument, NULL, OPTION_MIPS5},
f9b4148d 11059#define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
ae948b86 11060 {"mips32", no_argument, NULL, OPTION_MIPS32},
f9b4148d 11061#define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
ae948b86 11062 {"mips64", no_argument, NULL, OPTION_MIPS64},
f9b4148d
CD
11063#define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
11064 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
5f74bc13
CD
11065#define OPTION_MIPS64R2 (OPTION_ARCH_BASE + 10)
11066 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
f9b4148d
CD
11067
11068 /* Options which specify Application Specific Extensions (ASEs). */
5f74bc13 11069#define OPTION_ASE_BASE (OPTION_ARCH_BASE + 11)
f9b4148d
CD
11070#define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
11071 {"mips16", no_argument, NULL, OPTION_MIPS16},
11072#define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
11073 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
11074#define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
11075 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
11076#define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
11077 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
11078#define OPTION_MDMX (OPTION_ASE_BASE + 4)
11079 {"mdmx", no_argument, NULL, OPTION_MDMX},
11080#define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
11081 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
74cd071d
CF
11082#define OPTION_DSP (OPTION_ASE_BASE + 6)
11083 {"mdsp", no_argument, NULL, OPTION_DSP},
11084#define OPTION_NO_DSP (OPTION_ASE_BASE + 7)
11085 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
ef2e4d86
CF
11086#define OPTION_MT (OPTION_ASE_BASE + 8)
11087 {"mmt", no_argument, NULL, OPTION_MT},
11088#define OPTION_NO_MT (OPTION_ASE_BASE + 9)
11089 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
e16bfa71
TS
11090#define OPTION_SMARTMIPS (OPTION_ASE_BASE + 10)
11091 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
11092#define OPTION_NO_SMARTMIPS (OPTION_ASE_BASE + 11)
11093 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
8b082fb1
TS
11094#define OPTION_DSPR2 (OPTION_ASE_BASE + 12)
11095 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
11096#define OPTION_NO_DSPR2 (OPTION_ASE_BASE + 13)
11097 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
f9b4148d
CD
11098
11099 /* Old-style architecture options. Don't add more of these. */
8b082fb1 11100#define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 14)
f9b4148d
CD
11101#define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
11102 {"m4650", no_argument, NULL, OPTION_M4650},
11103#define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
11104 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
11105#define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
11106 {"m4010", no_argument, NULL, OPTION_M4010},
11107#define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
11108 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
11109#define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
11110 {"m4100", no_argument, NULL, OPTION_M4100},
11111#define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
11112 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
11113#define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
11114 {"m3900", no_argument, NULL, OPTION_M3900},
11115#define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
11116 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
11117
11118 /* Options which enable bug fixes. */
11119#define OPTION_FIX_BASE (OPTION_COMPAT_ARCH_BASE + 8)
11120#define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
11121 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
11122#define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
11123 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
11124 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
d766e8ec
RS
11125#define OPTION_FIX_VR4120 (OPTION_FIX_BASE + 2)
11126#define OPTION_NO_FIX_VR4120 (OPTION_FIX_BASE + 3)
11127 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
11128 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
7d8e00cf
RS
11129#define OPTION_FIX_VR4130 (OPTION_FIX_BASE + 4)
11130#define OPTION_NO_FIX_VR4130 (OPTION_FIX_BASE + 5)
11131 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
11132 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
f9b4148d
CD
11133
11134 /* Miscellaneous options. */
7d8e00cf 11135#define OPTION_MISC_BASE (OPTION_FIX_BASE + 6)
1ffcab4b 11136#define OPTION_TRAP (OPTION_MISC_BASE + 0)
252b5132
RH
11137 {"trap", no_argument, NULL, OPTION_TRAP},
11138 {"no-break", no_argument, NULL, OPTION_TRAP},
1ffcab4b 11139#define OPTION_BREAK (OPTION_MISC_BASE + 1)
252b5132
RH
11140 {"break", no_argument, NULL, OPTION_BREAK},
11141 {"no-trap", no_argument, NULL, OPTION_BREAK},
1ffcab4b 11142#define OPTION_EB (OPTION_MISC_BASE + 2)
252b5132 11143 {"EB", no_argument, NULL, OPTION_EB},
1ffcab4b 11144#define OPTION_EL (OPTION_MISC_BASE + 3)
252b5132 11145 {"EL", no_argument, NULL, OPTION_EL},
1ffcab4b 11146#define OPTION_FP32 (OPTION_MISC_BASE + 4)
ae948b86 11147 {"mfp32", no_argument, NULL, OPTION_FP32},
1ffcab4b 11148#define OPTION_GP32 (OPTION_MISC_BASE + 5)
c97ef257 11149 {"mgp32", no_argument, NULL, OPTION_GP32},
1ffcab4b 11150#define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 6)
119d663a 11151 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1ffcab4b 11152#define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
119d663a 11153 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1ffcab4b 11154#define OPTION_FP64 (OPTION_MISC_BASE + 8)
316f5878 11155 {"mfp64", no_argument, NULL, OPTION_FP64},
1ffcab4b 11156#define OPTION_GP64 (OPTION_MISC_BASE + 9)
ae948b86 11157 {"mgp64", no_argument, NULL, OPTION_GP64},
1ffcab4b
CD
11158#define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 10)
11159#define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 11)
4a6a3df4
AO
11160 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
11161 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
aa6975fb
ILT
11162#define OPTION_MSHARED (OPTION_MISC_BASE + 12)
11163#define OPTION_MNO_SHARED (OPTION_MISC_BASE + 13)
11164 {"mshared", no_argument, NULL, OPTION_MSHARED},
11165 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
aed1a261
RS
11166#define OPTION_MSYM32 (OPTION_MISC_BASE + 14)
11167#define OPTION_MNO_SYM32 (OPTION_MISC_BASE + 15)
11168 {"msym32", no_argument, NULL, OPTION_MSYM32},
11169 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
037b32b9
AN
11170#define OPTION_SOFT_FLOAT (OPTION_MISC_BASE + 16)
11171#define OPTION_HARD_FLOAT (OPTION_MISC_BASE + 17)
11172 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
11173 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
11174#define OPTION_SINGLE_FLOAT (OPTION_MISC_BASE + 18)
11175#define OPTION_DOUBLE_FLOAT (OPTION_MISC_BASE + 19)
11176 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
11177 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
11178
f9b4148d 11179 /* ELF-specific options. */
156c2f8b 11180#ifdef OBJ_ELF
037b32b9 11181#define OPTION_ELF_BASE (OPTION_MISC_BASE + 20)
156c2f8b 11182#define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
156c2f8b
NC
11183 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
11184 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
ae948b86 11185#define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
156c2f8b 11186 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
ae948b86 11187#define OPTION_XGOT (OPTION_ELF_BASE + 2)
156c2f8b 11188 {"xgot", no_argument, NULL, OPTION_XGOT},
ae948b86
TS
11189#define OPTION_MABI (OPTION_ELF_BASE + 3)
11190 {"mabi", required_argument, NULL, OPTION_MABI},
11191#define OPTION_32 (OPTION_ELF_BASE + 4)
156c2f8b 11192 {"32", no_argument, NULL, OPTION_32},
ae948b86 11193#define OPTION_N32 (OPTION_ELF_BASE + 5)
e013f690 11194 {"n32", no_argument, NULL, OPTION_N32},
ae948b86 11195#define OPTION_64 (OPTION_ELF_BASE + 6)
156c2f8b 11196 {"64", no_argument, NULL, OPTION_64},
ecb4347a
DJ
11197#define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
11198 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
11199#define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
11200 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
dcd410fe
RO
11201#define OPTION_PDR (OPTION_ELF_BASE + 9)
11202 {"mpdr", no_argument, NULL, OPTION_PDR},
11203#define OPTION_NO_PDR (OPTION_ELF_BASE + 10)
11204 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
0a44bf69
RS
11205#define OPTION_MVXWORKS_PIC (OPTION_ELF_BASE + 11)
11206 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
ae948b86 11207#endif /* OBJ_ELF */
f9b4148d 11208
252b5132
RH
11209 {NULL, no_argument, NULL, 0}
11210};
156c2f8b 11211size_t md_longopts_size = sizeof (md_longopts);
252b5132 11212
316f5878
RS
11213/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
11214 NEW_VALUE. Warn if another value was already specified. Note:
11215 we have to defer parsing the -march and -mtune arguments in order
11216 to handle 'from-abi' correctly, since the ABI might be specified
11217 in a later argument. */
11218
11219static void
17a2f251 11220mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
11221{
11222 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
11223 as_warn (_("A different %s was already specified, is now %s"),
11224 string_ptr == &mips_arch_string ? "-march" : "-mtune",
11225 new_value);
11226
11227 *string_ptr = new_value;
11228}
11229
252b5132 11230int
17a2f251 11231md_parse_option (int c, char *arg)
252b5132
RH
11232{
11233 switch (c)
11234 {
119d663a
NC
11235 case OPTION_CONSTRUCT_FLOATS:
11236 mips_disable_float_construction = 0;
11237 break;
bdaaa2e1 11238
119d663a
NC
11239 case OPTION_NO_CONSTRUCT_FLOATS:
11240 mips_disable_float_construction = 1;
11241 break;
bdaaa2e1 11242
252b5132
RH
11243 case OPTION_TRAP:
11244 mips_trap = 1;
11245 break;
11246
11247 case OPTION_BREAK:
11248 mips_trap = 0;
11249 break;
11250
11251 case OPTION_EB:
11252 target_big_endian = 1;
11253 break;
11254
11255 case OPTION_EL:
11256 target_big_endian = 0;
11257 break;
11258
11259 case 'O':
4ffff32f
TS
11260 if (arg == NULL)
11261 mips_optimize = 1;
11262 else if (arg[0] == '0')
11263 mips_optimize = 0;
11264 else if (arg[0] == '1')
252b5132
RH
11265 mips_optimize = 1;
11266 else
11267 mips_optimize = 2;
11268 break;
11269
11270 case 'g':
11271 if (arg == NULL)
11272 mips_debug = 2;
11273 else
11274 mips_debug = atoi (arg);
252b5132
RH
11275 break;
11276
11277 case OPTION_MIPS1:
316f5878 11278 file_mips_isa = ISA_MIPS1;
252b5132
RH
11279 break;
11280
11281 case OPTION_MIPS2:
316f5878 11282 file_mips_isa = ISA_MIPS2;
252b5132
RH
11283 break;
11284
11285 case OPTION_MIPS3:
316f5878 11286 file_mips_isa = ISA_MIPS3;
252b5132
RH
11287 break;
11288
11289 case OPTION_MIPS4:
316f5878 11290 file_mips_isa = ISA_MIPS4;
e7af610e
NC
11291 break;
11292
84ea6cf2 11293 case OPTION_MIPS5:
316f5878 11294 file_mips_isa = ISA_MIPS5;
84ea6cf2
NC
11295 break;
11296
e7af610e 11297 case OPTION_MIPS32:
316f5878 11298 file_mips_isa = ISA_MIPS32;
252b5132
RH
11299 break;
11300
af7ee8bf
CD
11301 case OPTION_MIPS32R2:
11302 file_mips_isa = ISA_MIPS32R2;
11303 break;
11304
5f74bc13
CD
11305 case OPTION_MIPS64R2:
11306 file_mips_isa = ISA_MIPS64R2;
11307 break;
11308
84ea6cf2 11309 case OPTION_MIPS64:
316f5878 11310 file_mips_isa = ISA_MIPS64;
84ea6cf2
NC
11311 break;
11312
ec68c924 11313 case OPTION_MTUNE:
316f5878
RS
11314 mips_set_option_string (&mips_tune_string, arg);
11315 break;
ec68c924 11316
316f5878
RS
11317 case OPTION_MARCH:
11318 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
11319 break;
11320
11321 case OPTION_M4650:
316f5878
RS
11322 mips_set_option_string (&mips_arch_string, "4650");
11323 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
11324 break;
11325
11326 case OPTION_NO_M4650:
11327 break;
11328
11329 case OPTION_M4010:
316f5878
RS
11330 mips_set_option_string (&mips_arch_string, "4010");
11331 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
11332 break;
11333
11334 case OPTION_NO_M4010:
11335 break;
11336
11337 case OPTION_M4100:
316f5878
RS
11338 mips_set_option_string (&mips_arch_string, "4100");
11339 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
11340 break;
11341
11342 case OPTION_NO_M4100:
11343 break;
11344
252b5132 11345 case OPTION_M3900:
316f5878
RS
11346 mips_set_option_string (&mips_arch_string, "3900");
11347 mips_set_option_string (&mips_tune_string, "3900");
252b5132 11348 break;
bdaaa2e1 11349
252b5132
RH
11350 case OPTION_NO_M3900:
11351 break;
11352
deec1734
CD
11353 case OPTION_MDMX:
11354 mips_opts.ase_mdmx = 1;
11355 break;
11356
11357 case OPTION_NO_MDMX:
11358 mips_opts.ase_mdmx = 0;
11359 break;
11360
74cd071d
CF
11361 case OPTION_DSP:
11362 mips_opts.ase_dsp = 1;
8b082fb1 11363 mips_opts.ase_dspr2 = 0;
74cd071d
CF
11364 break;
11365
11366 case OPTION_NO_DSP:
8b082fb1
TS
11367 mips_opts.ase_dsp = 0;
11368 mips_opts.ase_dspr2 = 0;
11369 break;
11370
11371 case OPTION_DSPR2:
11372 mips_opts.ase_dspr2 = 1;
11373 mips_opts.ase_dsp = 1;
11374 break;
11375
11376 case OPTION_NO_DSPR2:
11377 mips_opts.ase_dspr2 = 0;
74cd071d
CF
11378 mips_opts.ase_dsp = 0;
11379 break;
11380
ef2e4d86
CF
11381 case OPTION_MT:
11382 mips_opts.ase_mt = 1;
11383 break;
11384
11385 case OPTION_NO_MT:
11386 mips_opts.ase_mt = 0;
11387 break;
11388
252b5132
RH
11389 case OPTION_MIPS16:
11390 mips_opts.mips16 = 1;
7d10b47d 11391 mips_no_prev_insn ();
252b5132
RH
11392 break;
11393
11394 case OPTION_NO_MIPS16:
11395 mips_opts.mips16 = 0;
7d10b47d 11396 mips_no_prev_insn ();
252b5132
RH
11397 break;
11398
1f25f5d3
CD
11399 case OPTION_MIPS3D:
11400 mips_opts.ase_mips3d = 1;
11401 break;
11402
11403 case OPTION_NO_MIPS3D:
11404 mips_opts.ase_mips3d = 0;
11405 break;
11406
e16bfa71
TS
11407 case OPTION_SMARTMIPS:
11408 mips_opts.ase_smartmips = 1;
11409 break;
11410
11411 case OPTION_NO_SMARTMIPS:
11412 mips_opts.ase_smartmips = 0;
11413 break;
11414
d766e8ec
RS
11415 case OPTION_FIX_VR4120:
11416 mips_fix_vr4120 = 1;
60b63b72
RS
11417 break;
11418
d766e8ec
RS
11419 case OPTION_NO_FIX_VR4120:
11420 mips_fix_vr4120 = 0;
60b63b72
RS
11421 break;
11422
7d8e00cf
RS
11423 case OPTION_FIX_VR4130:
11424 mips_fix_vr4130 = 1;
11425 break;
11426
11427 case OPTION_NO_FIX_VR4130:
11428 mips_fix_vr4130 = 0;
11429 break;
11430
4a6a3df4
AO
11431 case OPTION_RELAX_BRANCH:
11432 mips_relax_branch = 1;
11433 break;
11434
11435 case OPTION_NO_RELAX_BRANCH:
11436 mips_relax_branch = 0;
11437 break;
11438
aa6975fb
ILT
11439 case OPTION_MSHARED:
11440 mips_in_shared = TRUE;
11441 break;
11442
11443 case OPTION_MNO_SHARED:
11444 mips_in_shared = FALSE;
11445 break;
11446
aed1a261
RS
11447 case OPTION_MSYM32:
11448 mips_opts.sym32 = TRUE;
11449 break;
11450
11451 case OPTION_MNO_SYM32:
11452 mips_opts.sym32 = FALSE;
11453 break;
11454
0f074f60 11455#ifdef OBJ_ELF
252b5132
RH
11456 /* When generating ELF code, we permit -KPIC and -call_shared to
11457 select SVR4_PIC, and -non_shared to select no PIC. This is
11458 intended to be compatible with Irix 5. */
11459 case OPTION_CALL_SHARED:
f43abd2b 11460 if (!IS_ELF)
252b5132
RH
11461 {
11462 as_bad (_("-call_shared is supported only for ELF format"));
11463 return 0;
11464 }
11465 mips_pic = SVR4_PIC;
143d77c5 11466 mips_abicalls = TRUE;
252b5132
RH
11467 break;
11468
11469 case OPTION_NON_SHARED:
f43abd2b 11470 if (!IS_ELF)
252b5132
RH
11471 {
11472 as_bad (_("-non_shared is supported only for ELF format"));
11473 return 0;
11474 }
11475 mips_pic = NO_PIC;
143d77c5 11476 mips_abicalls = FALSE;
252b5132
RH
11477 break;
11478
44075ae2
TS
11479 /* The -xgot option tells the assembler to use 32 bit offsets
11480 when accessing the got in SVR4_PIC mode. It is for Irix
252b5132
RH
11481 compatibility. */
11482 case OPTION_XGOT:
11483 mips_big_got = 1;
11484 break;
0f074f60 11485#endif /* OBJ_ELF */
252b5132
RH
11486
11487 case 'G':
6caf9ef4
TS
11488 g_switch_value = atoi (arg);
11489 g_switch_seen = 1;
252b5132
RH
11490 break;
11491
0f074f60 11492#ifdef OBJ_ELF
34ba82a8
TS
11493 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
11494 and -mabi=64. */
252b5132 11495 case OPTION_32:
f43abd2b 11496 if (!IS_ELF)
34ba82a8
TS
11497 {
11498 as_bad (_("-32 is supported for ELF format only"));
11499 return 0;
11500 }
316f5878 11501 mips_abi = O32_ABI;
252b5132
RH
11502 break;
11503
e013f690 11504 case OPTION_N32:
f43abd2b 11505 if (!IS_ELF)
34ba82a8
TS
11506 {
11507 as_bad (_("-n32 is supported for ELF format only"));
11508 return 0;
11509 }
316f5878 11510 mips_abi = N32_ABI;
e013f690 11511 break;
252b5132 11512
e013f690 11513 case OPTION_64:
f43abd2b 11514 if (!IS_ELF)
34ba82a8
TS
11515 {
11516 as_bad (_("-64 is supported for ELF format only"));
11517 return 0;
11518 }
316f5878 11519 mips_abi = N64_ABI;
f43abd2b 11520 if (!support_64bit_objects())
e013f690 11521 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132 11522 break;
ae948b86 11523#endif /* OBJ_ELF */
252b5132 11524
c97ef257 11525 case OPTION_GP32:
a325df1d 11526 file_mips_gp32 = 1;
c97ef257
AH
11527 break;
11528
11529 case OPTION_GP64:
a325df1d 11530 file_mips_gp32 = 0;
c97ef257 11531 break;
252b5132 11532
ca4e0257 11533 case OPTION_FP32:
a325df1d 11534 file_mips_fp32 = 1;
316f5878
RS
11535 break;
11536
11537 case OPTION_FP64:
11538 file_mips_fp32 = 0;
ca4e0257
RS
11539 break;
11540
037b32b9
AN
11541 case OPTION_SINGLE_FLOAT:
11542 file_mips_single_float = 1;
11543 break;
11544
11545 case OPTION_DOUBLE_FLOAT:
11546 file_mips_single_float = 0;
11547 break;
11548
11549 case OPTION_SOFT_FLOAT:
11550 file_mips_soft_float = 1;
11551 break;
11552
11553 case OPTION_HARD_FLOAT:
11554 file_mips_soft_float = 0;
11555 break;
11556
ae948b86 11557#ifdef OBJ_ELF
252b5132 11558 case OPTION_MABI:
f43abd2b 11559 if (!IS_ELF)
34ba82a8
TS
11560 {
11561 as_bad (_("-mabi is supported for ELF format only"));
11562 return 0;
11563 }
e013f690 11564 if (strcmp (arg, "32") == 0)
316f5878 11565 mips_abi = O32_ABI;
e013f690 11566 else if (strcmp (arg, "o64") == 0)
316f5878 11567 mips_abi = O64_ABI;
e013f690 11568 else if (strcmp (arg, "n32") == 0)
316f5878 11569 mips_abi = N32_ABI;
e013f690
TS
11570 else if (strcmp (arg, "64") == 0)
11571 {
316f5878 11572 mips_abi = N64_ABI;
e013f690
TS
11573 if (! support_64bit_objects())
11574 as_fatal (_("No compiled in support for 64 bit object file "
11575 "format"));
11576 }
11577 else if (strcmp (arg, "eabi") == 0)
316f5878 11578 mips_abi = EABI_ABI;
e013f690 11579 else
da0e507f
TS
11580 {
11581 as_fatal (_("invalid abi -mabi=%s"), arg);
11582 return 0;
11583 }
252b5132 11584 break;
e013f690 11585#endif /* OBJ_ELF */
252b5132 11586
6b76fefe 11587 case OPTION_M7000_HILO_FIX:
b34976b6 11588 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
11589 break;
11590
9ee72ff1 11591 case OPTION_MNO_7000_HILO_FIX:
b34976b6 11592 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
11593 break;
11594
ecb4347a
DJ
11595#ifdef OBJ_ELF
11596 case OPTION_MDEBUG:
b34976b6 11597 mips_flag_mdebug = TRUE;
ecb4347a
DJ
11598 break;
11599
11600 case OPTION_NO_MDEBUG:
b34976b6 11601 mips_flag_mdebug = FALSE;
ecb4347a 11602 break;
dcd410fe
RO
11603
11604 case OPTION_PDR:
11605 mips_flag_pdr = TRUE;
11606 break;
11607
11608 case OPTION_NO_PDR:
11609 mips_flag_pdr = FALSE;
11610 break;
0a44bf69
RS
11611
11612 case OPTION_MVXWORKS_PIC:
11613 mips_pic = VXWORKS_PIC;
11614 break;
ecb4347a
DJ
11615#endif /* OBJ_ELF */
11616
252b5132
RH
11617 default:
11618 return 0;
11619 }
11620
11621 return 1;
11622}
316f5878
RS
11623\f
11624/* Set up globals to generate code for the ISA or processor
11625 described by INFO. */
252b5132 11626
252b5132 11627static void
17a2f251 11628mips_set_architecture (const struct mips_cpu_info *info)
252b5132 11629{
316f5878 11630 if (info != 0)
252b5132 11631 {
fef14a42
TS
11632 file_mips_arch = info->cpu;
11633 mips_opts.arch = info->cpu;
316f5878 11634 mips_opts.isa = info->isa;
252b5132 11635 }
252b5132
RH
11636}
11637
252b5132 11638
316f5878 11639/* Likewise for tuning. */
252b5132 11640
316f5878 11641static void
17a2f251 11642mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
11643{
11644 if (info != 0)
fef14a42 11645 mips_tune = info->cpu;
316f5878 11646}
80cc45a5 11647
34ba82a8 11648
252b5132 11649void
17a2f251 11650mips_after_parse_args (void)
e9670677 11651{
fef14a42
TS
11652 const struct mips_cpu_info *arch_info = 0;
11653 const struct mips_cpu_info *tune_info = 0;
11654
e9670677 11655 /* GP relative stuff not working for PE */
6caf9ef4 11656 if (strncmp (TARGET_OS, "pe", 2) == 0)
e9670677 11657 {
6caf9ef4 11658 if (g_switch_seen && g_switch_value != 0)
e9670677
MR
11659 as_bad (_("-G not supported in this configuration."));
11660 g_switch_value = 0;
11661 }
11662
cac012d6
AO
11663 if (mips_abi == NO_ABI)
11664 mips_abi = MIPS_DEFAULT_ABI;
11665
22923709
RS
11666 /* The following code determines the architecture and register size.
11667 Similar code was added to GCC 3.3 (see override_options() in
11668 config/mips/mips.c). The GAS and GCC code should be kept in sync
11669 as much as possible. */
e9670677 11670
316f5878 11671 if (mips_arch_string != 0)
fef14a42 11672 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 11673
316f5878 11674 if (file_mips_isa != ISA_UNKNOWN)
e9670677 11675 {
316f5878 11676 /* Handle -mipsN. At this point, file_mips_isa contains the
fef14a42 11677 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 11678 the -march selection (if any). */
fef14a42 11679 if (arch_info != 0)
e9670677 11680 {
316f5878
RS
11681 /* -march takes precedence over -mipsN, since it is more descriptive.
11682 There's no harm in specifying both as long as the ISA levels
11683 are the same. */
fef14a42 11684 if (file_mips_isa != arch_info->isa)
316f5878
RS
11685 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
11686 mips_cpu_info_from_isa (file_mips_isa)->name,
fef14a42 11687 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 11688 }
316f5878 11689 else
fef14a42 11690 arch_info = mips_cpu_info_from_isa (file_mips_isa);
e9670677
MR
11691 }
11692
fef14a42
TS
11693 if (arch_info == 0)
11694 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
e9670677 11695
fef14a42 11696 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
316f5878 11697 as_bad ("-march=%s is not compatible with the selected ABI",
fef14a42
TS
11698 arch_info->name);
11699
11700 mips_set_architecture (arch_info);
11701
11702 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
11703 if (mips_tune_string != 0)
11704 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 11705
fef14a42
TS
11706 if (tune_info == 0)
11707 mips_set_tune (arch_info);
11708 else
11709 mips_set_tune (tune_info);
e9670677 11710
316f5878 11711 if (file_mips_gp32 >= 0)
e9670677 11712 {
316f5878
RS
11713 /* The user specified the size of the integer registers. Make sure
11714 it agrees with the ABI and ISA. */
11715 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
11716 as_bad (_("-mgp64 used with a 32-bit processor"));
11717 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
11718 as_bad (_("-mgp32 used with a 64-bit ABI"));
11719 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
11720 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
11721 }
11722 else
11723 {
316f5878
RS
11724 /* Infer the integer register size from the ABI and processor.
11725 Restrict ourselves to 32-bit registers if that's all the
11726 processor has, or if the ABI cannot handle 64-bit registers. */
11727 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
11728 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
e9670677
MR
11729 }
11730
ad3fea08
TS
11731 switch (file_mips_fp32)
11732 {
11733 default:
11734 case -1:
11735 /* No user specified float register size.
11736 ??? GAS treats single-float processors as though they had 64-bit
11737 float registers (although it complains when double-precision
11738 instructions are used). As things stand, saying they have 32-bit
11739 registers would lead to spurious "register must be even" messages.
11740 So here we assume float registers are never smaller than the
11741 integer ones. */
11742 if (file_mips_gp32 == 0)
11743 /* 64-bit integer registers implies 64-bit float registers. */
11744 file_mips_fp32 = 0;
11745 else if ((mips_opts.ase_mips3d > 0 || mips_opts.ase_mdmx > 0)
11746 && ISA_HAS_64BIT_FPRS (mips_opts.isa))
11747 /* -mips3d and -mdmx imply 64-bit float registers, if possible. */
11748 file_mips_fp32 = 0;
11749 else
11750 /* 32-bit float registers. */
11751 file_mips_fp32 = 1;
11752 break;
11753
11754 /* The user specified the size of the float registers. Check if it
11755 agrees with the ABI and ISA. */
11756 case 0:
11757 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
11758 as_bad (_("-mfp64 used with a 32-bit fpu"));
11759 else if (ABI_NEEDS_32BIT_REGS (mips_abi)
11760 && !ISA_HAS_MXHC1 (mips_opts.isa))
11761 as_warn (_("-mfp64 used with a 32-bit ABI"));
11762 break;
11763 case 1:
11764 if (ABI_NEEDS_64BIT_REGS (mips_abi))
11765 as_warn (_("-mfp32 used with a 64-bit ABI"));
11766 break;
11767 }
e9670677 11768
316f5878 11769 /* End of GCC-shared inference code. */
e9670677 11770
17a2f251
TS
11771 /* This flag is set when we have a 64-bit capable CPU but use only
11772 32-bit wide registers. Note that EABI does not use it. */
11773 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
11774 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
11775 || mips_abi == O32_ABI))
316f5878 11776 mips_32bitmode = 1;
e9670677
MR
11777
11778 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
11779 as_bad (_("trap exception not supported at ISA 1"));
11780
e9670677
MR
11781 /* If the selected architecture includes support for ASEs, enable
11782 generation of code for them. */
a4672219 11783 if (mips_opts.mips16 == -1)
fef14a42 11784 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
ffdefa66 11785 if (mips_opts.ase_mips3d == -1)
65263ce3 11786 mips_opts.ase_mips3d = ((arch_info->flags & MIPS_CPU_ASE_MIPS3D)
ad3fea08
TS
11787 && file_mips_fp32 == 0) ? 1 : 0;
11788 if (mips_opts.ase_mips3d && file_mips_fp32 == 1)
11789 as_bad (_("-mfp32 used with -mips3d"));
11790
ffdefa66 11791 if (mips_opts.ase_mdmx == -1)
65263ce3 11792 mips_opts.ase_mdmx = ((arch_info->flags & MIPS_CPU_ASE_MDMX)
ad3fea08
TS
11793 && file_mips_fp32 == 0) ? 1 : 0;
11794 if (mips_opts.ase_mdmx && file_mips_fp32 == 1)
11795 as_bad (_("-mfp32 used with -mdmx"));
11796
11797 if (mips_opts.ase_smartmips == -1)
11798 mips_opts.ase_smartmips = (arch_info->flags & MIPS_CPU_ASE_SMARTMIPS) ? 1 : 0;
11799 if (mips_opts.ase_smartmips && !ISA_SUPPORTS_SMARTMIPS)
11800 as_warn ("%s ISA does not support SmartMIPS",
11801 mips_cpu_info_from_isa (mips_opts.isa)->name);
11802
74cd071d 11803 if (mips_opts.ase_dsp == -1)
ad3fea08
TS
11804 mips_opts.ase_dsp = (arch_info->flags & MIPS_CPU_ASE_DSP) ? 1 : 0;
11805 if (mips_opts.ase_dsp && !ISA_SUPPORTS_DSP_ASE)
11806 as_warn ("%s ISA does not support DSP ASE",
11807 mips_cpu_info_from_isa (mips_opts.isa)->name);
11808
8b082fb1
TS
11809 if (mips_opts.ase_dspr2 == -1)
11810 {
11811 mips_opts.ase_dspr2 = (arch_info->flags & MIPS_CPU_ASE_DSPR2) ? 1 : 0;
11812 mips_opts.ase_dsp = (arch_info->flags & MIPS_CPU_ASE_DSP) ? 1 : 0;
11813 }
11814 if (mips_opts.ase_dspr2 && !ISA_SUPPORTS_DSPR2_ASE)
11815 as_warn ("%s ISA does not support DSP R2 ASE",
11816 mips_cpu_info_from_isa (mips_opts.isa)->name);
11817
ef2e4d86 11818 if (mips_opts.ase_mt == -1)
ad3fea08
TS
11819 mips_opts.ase_mt = (arch_info->flags & MIPS_CPU_ASE_MT) ? 1 : 0;
11820 if (mips_opts.ase_mt && !ISA_SUPPORTS_MT_ASE)
8b082fb1 11821 as_warn ("%s ISA does not support MT ASE",
ad3fea08 11822 mips_cpu_info_from_isa (mips_opts.isa)->name);
e9670677 11823
e9670677 11824 file_mips_isa = mips_opts.isa;
a4672219 11825 file_ase_mips16 = mips_opts.mips16;
e9670677
MR
11826 file_ase_mips3d = mips_opts.ase_mips3d;
11827 file_ase_mdmx = mips_opts.ase_mdmx;
e16bfa71 11828 file_ase_smartmips = mips_opts.ase_smartmips;
74cd071d 11829 file_ase_dsp = mips_opts.ase_dsp;
8b082fb1 11830 file_ase_dspr2 = mips_opts.ase_dspr2;
ef2e4d86 11831 file_ase_mt = mips_opts.ase_mt;
e9670677
MR
11832 mips_opts.gp32 = file_mips_gp32;
11833 mips_opts.fp32 = file_mips_fp32;
037b32b9
AN
11834 mips_opts.soft_float = file_mips_soft_float;
11835 mips_opts.single_float = file_mips_single_float;
e9670677 11836
ecb4347a
DJ
11837 if (mips_flag_mdebug < 0)
11838 {
11839#ifdef OBJ_MAYBE_ECOFF
11840 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
11841 mips_flag_mdebug = 1;
11842 else
11843#endif /* OBJ_MAYBE_ECOFF */
11844 mips_flag_mdebug = 0;
11845 }
e9670677
MR
11846}
11847\f
11848void
17a2f251 11849mips_init_after_args (void)
252b5132
RH
11850{
11851 /* initialize opcodes */
11852 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 11853 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
11854}
11855
11856long
17a2f251 11857md_pcrel_from (fixS *fixP)
252b5132 11858{
a7ebbfdf
TS
11859 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
11860 switch (fixP->fx_r_type)
11861 {
11862 case BFD_RELOC_16_PCREL_S2:
11863 case BFD_RELOC_MIPS_JMP:
11864 /* Return the address of the delay slot. */
11865 return addr + 4;
11866 default:
58ea3d6a 11867 /* We have no relocation type for PC relative MIPS16 instructions. */
64817874
TS
11868 if (fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != now_seg)
11869 as_bad_where (fixP->fx_file, fixP->fx_line,
11870 _("PC relative MIPS16 instruction references a different section"));
a7ebbfdf
TS
11871 return addr;
11872 }
252b5132
RH
11873}
11874
252b5132
RH
11875/* This is called before the symbol table is processed. In order to
11876 work with gcc when using mips-tfile, we must keep all local labels.
11877 However, in other cases, we want to discard them. If we were
11878 called with -g, but we didn't see any debugging information, it may
11879 mean that gcc is smuggling debugging information through to
11880 mips-tfile, in which case we must generate all local labels. */
11881
11882void
17a2f251 11883mips_frob_file_before_adjust (void)
252b5132
RH
11884{
11885#ifndef NO_ECOFF_DEBUGGING
11886 if (ECOFF_DEBUGGING
11887 && mips_debug != 0
11888 && ! ecoff_debugging_seen)
11889 flag_keep_locals = 1;
11890#endif
11891}
11892
3b91255e 11893/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 11894 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
11895 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
11896 relocation operators.
11897
11898 For our purposes, a %lo() expression matches a %got() or %hi()
11899 expression if:
11900
11901 (a) it refers to the same symbol; and
11902 (b) the offset applied in the %lo() expression is no lower than
11903 the offset applied in the %got() or %hi().
11904
11905 (b) allows us to cope with code like:
11906
11907 lui $4,%hi(foo)
11908 lh $4,%lo(foo+2)($4)
11909
11910 ...which is legal on RELA targets, and has a well-defined behaviour
11911 if the user knows that adding 2 to "foo" will not induce a carry to
11912 the high 16 bits.
11913
11914 When several %lo()s match a particular %got() or %hi(), we use the
11915 following rules to distinguish them:
11916
11917 (1) %lo()s with smaller offsets are a better match than %lo()s with
11918 higher offsets.
11919
11920 (2) %lo()s with no matching %got() or %hi() are better than those
11921 that already have a matching %got() or %hi().
11922
11923 (3) later %lo()s are better than earlier %lo()s.
11924
11925 These rules are applied in order.
11926
11927 (1) means, among other things, that %lo()s with identical offsets are
11928 chosen if they exist.
11929
11930 (2) means that we won't associate several high-part relocations with
11931 the same low-part relocation unless there's no alternative. Having
11932 several high parts for the same low part is a GNU extension; this rule
11933 allows careful users to avoid it.
11934
11935 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
11936 with the last high-part relocation being at the front of the list.
11937 It therefore makes sense to choose the last matching low-part
11938 relocation, all other things being equal. It's also easier
11939 to code that way. */
252b5132
RH
11940
11941void
17a2f251 11942mips_frob_file (void)
252b5132
RH
11943{
11944 struct mips_hi_fixup *l;
35903be0 11945 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
252b5132
RH
11946
11947 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
11948 {
11949 segment_info_type *seginfo;
3b91255e
RS
11950 bfd_boolean matched_lo_p;
11951 fixS **hi_pos, **lo_pos, **pos;
252b5132 11952
5919d012 11953 assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 11954
5919d012
RS
11955 /* If a GOT16 relocation turns out to be against a global symbol,
11956 there isn't supposed to be a matching LO. */
11957 if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
11958 && !pic_need_relax (l->fixp->fx_addsy, l->seg))
11959 continue;
11960
11961 /* Check quickly whether the next fixup happens to be a matching %lo. */
11962 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
11963 continue;
11964
252b5132 11965 seginfo = seg_info (l->seg);
252b5132 11966
3b91255e
RS
11967 /* Set HI_POS to the position of this relocation in the chain.
11968 Set LO_POS to the position of the chosen low-part relocation.
11969 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
11970 relocation that matches an immediately-preceding high-part
11971 relocation. */
11972 hi_pos = NULL;
11973 lo_pos = NULL;
11974 matched_lo_p = FALSE;
35903be0
CM
11975
11976 if (l->fixp->fx_r_type == BFD_RELOC_MIPS16_HI16
11977 || l->fixp->fx_r_type == BFD_RELOC_MIPS16_HI16_S)
11978 looking_for_rtype = BFD_RELOC_MIPS16_LO16;
11979 else
11980 looking_for_rtype = BFD_RELOC_LO16;
11981
3b91255e
RS
11982 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
11983 {
11984 if (*pos == l->fixp)
11985 hi_pos = pos;
11986
35903be0 11987 if ((*pos)->fx_r_type == looking_for_rtype
3b91255e
RS
11988 && (*pos)->fx_addsy == l->fixp->fx_addsy
11989 && (*pos)->fx_offset >= l->fixp->fx_offset
11990 && (lo_pos == NULL
11991 || (*pos)->fx_offset < (*lo_pos)->fx_offset
11992 || (!matched_lo_p
11993 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
11994 lo_pos = pos;
11995
11996 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
11997 && fixup_has_matching_lo_p (*pos));
11998 }
11999
12000 /* If we found a match, remove the high-part relocation from its
12001 current position and insert it before the low-part relocation.
12002 Make the offsets match so that fixup_has_matching_lo_p()
12003 will return true.
12004
12005 We don't warn about unmatched high-part relocations since some
12006 versions of gcc have been known to emit dead "lui ...%hi(...)"
12007 instructions. */
12008 if (lo_pos != NULL)
12009 {
12010 l->fixp->fx_offset = (*lo_pos)->fx_offset;
12011 if (l->fixp->fx_next != *lo_pos)
252b5132 12012 {
3b91255e
RS
12013 *hi_pos = l->fixp->fx_next;
12014 l->fixp->fx_next = *lo_pos;
12015 *lo_pos = l->fixp;
252b5132 12016 }
252b5132
RH
12017 }
12018 }
12019}
12020
3e722fb5 12021/* We may have combined relocations without symbols in the N32/N64 ABI.
f6688943 12022 We have to prevent gas from dropping them. */
252b5132 12023
252b5132 12024int
17a2f251 12025mips_force_relocation (fixS *fixp)
252b5132 12026{
ae6063d4 12027 if (generic_force_reloc (fixp))
252b5132
RH
12028 return 1;
12029
f6688943
TS
12030 if (HAVE_NEWABI
12031 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
12032 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
12033 || fixp->fx_r_type == BFD_RELOC_HI16_S
12034 || fixp->fx_r_type == BFD_RELOC_LO16))
12035 return 1;
12036
3e722fb5 12037 return 0;
252b5132
RH
12038}
12039
12040/* Apply a fixup to the object file. */
12041
94f592af 12042void
55cf6793 12043md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 12044{
874e8986 12045 bfd_byte *buf;
98aa84af 12046 long insn;
a7ebbfdf 12047 reloc_howto_type *howto;
252b5132 12048
a7ebbfdf
TS
12049 /* We ignore generic BFD relocations we don't know about. */
12050 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
12051 if (! howto)
12052 return;
65551fa4 12053
252b5132
RH
12054 assert (fixP->fx_size == 4
12055 || fixP->fx_r_type == BFD_RELOC_16
12056 || fixP->fx_r_type == BFD_RELOC_64
f6688943
TS
12057 || fixP->fx_r_type == BFD_RELOC_CTOR
12058 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
252b5132 12059 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
741d6ea8
JM
12060 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
12061 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64);
252b5132 12062
a7ebbfdf 12063 buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
252b5132 12064
3994f87e 12065 assert (!fixP->fx_pcrel || fixP->fx_r_type == BFD_RELOC_16_PCREL_S2);
b1dca8ee
RS
12066
12067 /* Don't treat parts of a composite relocation as done. There are two
12068 reasons for this:
12069
12070 (1) The second and third parts will be against 0 (RSS_UNDEF) but
12071 should nevertheless be emitted if the first part is.
12072
12073 (2) In normal usage, composite relocations are never assembly-time
12074 constants. The easiest way of dealing with the pathological
12075 exceptions is to generate a relocation against STN_UNDEF and
12076 leave everything up to the linker. */
3994f87e 12077 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
252b5132
RH
12078 fixP->fx_done = 1;
12079
12080 switch (fixP->fx_r_type)
12081 {
3f98094e
DJ
12082 case BFD_RELOC_MIPS_TLS_GD:
12083 case BFD_RELOC_MIPS_TLS_LDM:
741d6ea8
JM
12084 case BFD_RELOC_MIPS_TLS_DTPREL32:
12085 case BFD_RELOC_MIPS_TLS_DTPREL64:
3f98094e
DJ
12086 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
12087 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
12088 case BFD_RELOC_MIPS_TLS_GOTTPREL:
12089 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
12090 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
12091 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12092 /* fall through */
12093
252b5132 12094 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
12095 case BFD_RELOC_MIPS_SHIFT5:
12096 case BFD_RELOC_MIPS_SHIFT6:
12097 case BFD_RELOC_MIPS_GOT_DISP:
12098 case BFD_RELOC_MIPS_GOT_PAGE:
12099 case BFD_RELOC_MIPS_GOT_OFST:
12100 case BFD_RELOC_MIPS_SUB:
12101 case BFD_RELOC_MIPS_INSERT_A:
12102 case BFD_RELOC_MIPS_INSERT_B:
12103 case BFD_RELOC_MIPS_DELETE:
12104 case BFD_RELOC_MIPS_HIGHEST:
12105 case BFD_RELOC_MIPS_HIGHER:
12106 case BFD_RELOC_MIPS_SCN_DISP:
12107 case BFD_RELOC_MIPS_REL16:
12108 case BFD_RELOC_MIPS_RELGOT:
12109 case BFD_RELOC_MIPS_JALR:
252b5132
RH
12110 case BFD_RELOC_HI16:
12111 case BFD_RELOC_HI16_S:
cdf6fd85 12112 case BFD_RELOC_GPREL16:
252b5132
RH
12113 case BFD_RELOC_MIPS_LITERAL:
12114 case BFD_RELOC_MIPS_CALL16:
12115 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 12116 case BFD_RELOC_GPREL32:
252b5132
RH
12117 case BFD_RELOC_MIPS_GOT_HI16:
12118 case BFD_RELOC_MIPS_GOT_LO16:
12119 case BFD_RELOC_MIPS_CALL_HI16:
12120 case BFD_RELOC_MIPS_CALL_LO16:
12121 case BFD_RELOC_MIPS16_GPREL:
d6f16593
MR
12122 case BFD_RELOC_MIPS16_HI16:
12123 case BFD_RELOC_MIPS16_HI16_S:
252b5132 12124 case BFD_RELOC_MIPS16_JMP:
54f4ddb3 12125 /* Nothing needed to do. The value comes from the reloc entry. */
252b5132
RH
12126 break;
12127
252b5132
RH
12128 case BFD_RELOC_64:
12129 /* This is handled like BFD_RELOC_32, but we output a sign
12130 extended value if we are only 32 bits. */
3e722fb5 12131 if (fixP->fx_done)
252b5132
RH
12132 {
12133 if (8 <= sizeof (valueT))
2132e3a3 12134 md_number_to_chars ((char *) buf, *valP, 8);
252b5132
RH
12135 else
12136 {
a7ebbfdf 12137 valueT hiv;
252b5132 12138
a7ebbfdf 12139 if ((*valP & 0x80000000) != 0)
252b5132
RH
12140 hiv = 0xffffffff;
12141 else
12142 hiv = 0;
b215186b 12143 md_number_to_chars ((char *)(buf + (target_big_endian ? 4 : 0)),
a7ebbfdf 12144 *valP, 4);
b215186b 12145 md_number_to_chars ((char *)(buf + (target_big_endian ? 0 : 4)),
a7ebbfdf 12146 hiv, 4);
252b5132
RH
12147 }
12148 }
12149 break;
12150
056350c6 12151 case BFD_RELOC_RVA:
252b5132 12152 case BFD_RELOC_32:
252b5132
RH
12153 case BFD_RELOC_16:
12154 /* If we are deleting this reloc entry, we must fill in the
54f4ddb3
TS
12155 value now. This can happen if we have a .word which is not
12156 resolved when it appears but is later defined. */
252b5132 12157 if (fixP->fx_done)
54f4ddb3 12158 md_number_to_chars ((char *) buf, *valP, fixP->fx_size);
252b5132
RH
12159 break;
12160
12161 case BFD_RELOC_LO16:
d6f16593 12162 case BFD_RELOC_MIPS16_LO16:
3e722fb5
CD
12163 /* FIXME: Now that embedded-PIC is gone, some of this code/comment
12164 may be safe to remove, but if so it's not obvious. */
252b5132
RH
12165 /* When handling an embedded PIC switch statement, we can wind
12166 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
12167 if (fixP->fx_done)
12168 {
a7ebbfdf 12169 if (*valP + 0x8000 > 0xffff)
252b5132
RH
12170 as_bad_where (fixP->fx_file, fixP->fx_line,
12171 _("relocation overflow"));
252b5132
RH
12172 if (target_big_endian)
12173 buf += 2;
2132e3a3 12174 md_number_to_chars ((char *) buf, *valP, 2);
252b5132
RH
12175 }
12176 break;
12177
12178 case BFD_RELOC_16_PCREL_S2:
a7ebbfdf 12179 if ((*valP & 0x3) != 0)
cb56d3d3 12180 as_bad_where (fixP->fx_file, fixP->fx_line,
bad36eac 12181 _("Branch to misaligned address (%lx)"), (long) *valP);
cb56d3d3 12182
54f4ddb3
TS
12183 /* We need to save the bits in the instruction since fixup_segment()
12184 might be deleting the relocation entry (i.e., a branch within
12185 the current segment). */
a7ebbfdf 12186 if (! fixP->fx_done)
bb2d6cd7 12187 break;
252b5132 12188
54f4ddb3 12189 /* Update old instruction data. */
252b5132
RH
12190 if (target_big_endian)
12191 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
12192 else
12193 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
12194
a7ebbfdf
TS
12195 if (*valP + 0x20000 <= 0x3ffff)
12196 {
12197 insn |= (*valP >> 2) & 0xffff;
2132e3a3 12198 md_number_to_chars ((char *) buf, insn, 4);
a7ebbfdf
TS
12199 }
12200 else if (mips_pic == NO_PIC
12201 && fixP->fx_done
12202 && fixP->fx_frag->fr_address >= text_section->vma
12203 && (fixP->fx_frag->fr_address
587aac4e 12204 < text_section->vma + bfd_get_section_size (text_section))
a7ebbfdf
TS
12205 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
12206 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
12207 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
12208 {
12209 /* The branch offset is too large. If this is an
12210 unconditional branch, and we are not generating PIC code,
12211 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
12212 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
12213 insn = 0x0c000000; /* jal */
252b5132 12214 else
a7ebbfdf
TS
12215 insn = 0x08000000; /* j */
12216 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
12217 fixP->fx_done = 0;
12218 fixP->fx_addsy = section_symbol (text_section);
12219 *valP += md_pcrel_from (fixP);
2132e3a3 12220 md_number_to_chars ((char *) buf, insn, 4);
a7ebbfdf
TS
12221 }
12222 else
12223 {
12224 /* If we got here, we have branch-relaxation disabled,
12225 and there's nothing we can do to fix this instruction
12226 without turning it into a longer sequence. */
12227 as_bad_where (fixP->fx_file, fixP->fx_line,
12228 _("Branch out of range"));
252b5132 12229 }
252b5132
RH
12230 break;
12231
12232 case BFD_RELOC_VTABLE_INHERIT:
12233 fixP->fx_done = 0;
12234 if (fixP->fx_addsy
12235 && !S_IS_DEFINED (fixP->fx_addsy)
12236 && !S_IS_WEAK (fixP->fx_addsy))
12237 S_SET_WEAK (fixP->fx_addsy);
12238 break;
12239
12240 case BFD_RELOC_VTABLE_ENTRY:
12241 fixP->fx_done = 0;
12242 break;
12243
12244 default:
12245 internalError ();
12246 }
a7ebbfdf
TS
12247
12248 /* Remember value for tc_gen_reloc. */
12249 fixP->fx_addnumber = *valP;
252b5132
RH
12250}
12251
252b5132 12252static symbolS *
17a2f251 12253get_symbol (void)
252b5132
RH
12254{
12255 int c;
12256 char *name;
12257 symbolS *p;
12258
12259 name = input_line_pointer;
12260 c = get_symbol_end ();
12261 p = (symbolS *) symbol_find_or_make (name);
12262 *input_line_pointer = c;
12263 return p;
12264}
12265
742a56fe
RS
12266/* Align the current frag to a given power of two. If a particular
12267 fill byte should be used, FILL points to an integer that contains
12268 that byte, otherwise FILL is null.
12269
12270 The MIPS assembler also automatically adjusts any preceding
12271 label. */
252b5132
RH
12272
12273static void
742a56fe 12274mips_align (int to, int *fill, symbolS *label)
252b5132 12275{
7d10b47d 12276 mips_emit_delays ();
742a56fe
RS
12277 mips_record_mips16_mode ();
12278 if (fill == NULL && subseg_text_p (now_seg))
12279 frag_align_code (to, 0);
12280 else
12281 frag_align (to, fill ? *fill : 0, 0);
252b5132
RH
12282 record_alignment (now_seg, to);
12283 if (label != NULL)
12284 {
12285 assert (S_GET_SEGMENT (label) == now_seg);
49309057 12286 symbol_set_frag (label, frag_now);
252b5132
RH
12287 S_SET_VALUE (label, (valueT) frag_now_fix ());
12288 }
12289}
12290
12291/* Align to a given power of two. .align 0 turns off the automatic
12292 alignment used by the data creating pseudo-ops. */
12293
12294static void
17a2f251 12295s_align (int x ATTRIBUTE_UNUSED)
252b5132 12296{
742a56fe 12297 int temp, fill_value, *fill_ptr;
49954fb4 12298 long max_alignment = 28;
252b5132 12299
54f4ddb3 12300 /* o Note that the assembler pulls down any immediately preceding label
252b5132 12301 to the aligned address.
54f4ddb3 12302 o It's not documented but auto alignment is reinstated by
252b5132 12303 a .align pseudo instruction.
54f4ddb3 12304 o Note also that after auto alignment is turned off the mips assembler
252b5132 12305 issues an error on attempt to assemble an improperly aligned data item.
54f4ddb3 12306 We don't. */
252b5132
RH
12307
12308 temp = get_absolute_expression ();
12309 if (temp > max_alignment)
12310 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
12311 else if (temp < 0)
12312 {
12313 as_warn (_("Alignment negative: 0 assumed."));
12314 temp = 0;
12315 }
12316 if (*input_line_pointer == ',')
12317 {
f9419b05 12318 ++input_line_pointer;
742a56fe
RS
12319 fill_value = get_absolute_expression ();
12320 fill_ptr = &fill_value;
252b5132
RH
12321 }
12322 else
742a56fe 12323 fill_ptr = 0;
252b5132
RH
12324 if (temp)
12325 {
a8dbcb85
TS
12326 segment_info_type *si = seg_info (now_seg);
12327 struct insn_label_list *l = si->label_list;
54f4ddb3 12328 /* Auto alignment should be switched on by next section change. */
252b5132 12329 auto_align = 1;
742a56fe 12330 mips_align (temp, fill_ptr, l != NULL ? l->label : NULL);
252b5132
RH
12331 }
12332 else
12333 {
12334 auto_align = 0;
12335 }
12336
12337 demand_empty_rest_of_line ();
12338}
12339
252b5132 12340static void
17a2f251 12341s_change_sec (int sec)
252b5132
RH
12342{
12343 segT seg;
12344
252b5132
RH
12345#ifdef OBJ_ELF
12346 /* The ELF backend needs to know that we are changing sections, so
12347 that .previous works correctly. We could do something like check
b6ff326e 12348 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
12349 as it would not be appropriate to use it in the section changing
12350 functions in read.c, since obj-elf.c intercepts those. FIXME:
12351 This should be cleaner, somehow. */
f43abd2b
TS
12352 if (IS_ELF)
12353 obj_elf_section_change_hook ();
252b5132
RH
12354#endif
12355
7d10b47d 12356 mips_emit_delays ();
252b5132
RH
12357 switch (sec)
12358 {
12359 case 't':
12360 s_text (0);
12361 break;
12362 case 'd':
12363 s_data (0);
12364 break;
12365 case 'b':
12366 subseg_set (bss_section, (subsegT) get_absolute_expression ());
12367 demand_empty_rest_of_line ();
12368 break;
12369
12370 case 'r':
4d0d148d
TS
12371 seg = subseg_new (RDATA_SECTION_NAME,
12372 (subsegT) get_absolute_expression ());
f43abd2b 12373 if (IS_ELF)
252b5132 12374 {
4d0d148d
TS
12375 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
12376 | SEC_READONLY | SEC_RELOC
12377 | SEC_DATA));
c41e87e3 12378 if (strncmp (TARGET_OS, "elf", 3) != 0)
4d0d148d 12379 record_alignment (seg, 4);
252b5132 12380 }
4d0d148d 12381 demand_empty_rest_of_line ();
252b5132
RH
12382 break;
12383
12384 case 's':
4d0d148d 12385 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
f43abd2b 12386 if (IS_ELF)
252b5132 12387 {
4d0d148d
TS
12388 bfd_set_section_flags (stdoutput, seg,
12389 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
c41e87e3 12390 if (strncmp (TARGET_OS, "elf", 3) != 0)
4d0d148d 12391 record_alignment (seg, 4);
252b5132 12392 }
4d0d148d
TS
12393 demand_empty_rest_of_line ();
12394 break;
252b5132
RH
12395 }
12396
12397 auto_align = 1;
12398}
b34976b6 12399
cca86cc8 12400void
17a2f251 12401s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 12402{
7ed4a06a 12403#ifdef OBJ_ELF
cca86cc8
SC
12404 char *section_name;
12405 char c;
684022ea 12406 char next_c = 0;
cca86cc8
SC
12407 int section_type;
12408 int section_flag;
12409 int section_entry_size;
12410 int section_alignment;
b34976b6 12411
f43abd2b 12412 if (!IS_ELF)
7ed4a06a
TS
12413 return;
12414
cca86cc8
SC
12415 section_name = input_line_pointer;
12416 c = get_symbol_end ();
a816d1ed
AO
12417 if (c)
12418 next_c = *(input_line_pointer + 1);
cca86cc8 12419
4cf0dd0d
TS
12420 /* Do we have .section Name<,"flags">? */
12421 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 12422 {
4cf0dd0d
TS
12423 /* just after name is now '\0'. */
12424 *input_line_pointer = c;
cca86cc8
SC
12425 input_line_pointer = section_name;
12426 obj_elf_section (ignore);
12427 return;
12428 }
12429 input_line_pointer++;
12430
12431 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
12432 if (c == ',')
12433 section_type = get_absolute_expression ();
12434 else
12435 section_type = 0;
12436 if (*input_line_pointer++ == ',')
12437 section_flag = get_absolute_expression ();
12438 else
12439 section_flag = 0;
12440 if (*input_line_pointer++ == ',')
12441 section_entry_size = get_absolute_expression ();
12442 else
12443 section_entry_size = 0;
12444 if (*input_line_pointer++ == ',')
12445 section_alignment = get_absolute_expression ();
12446 else
12447 section_alignment = 0;
12448
a816d1ed
AO
12449 section_name = xstrdup (section_name);
12450
8ab8a5c8
RS
12451 /* When using the generic form of .section (as implemented by obj-elf.c),
12452 there's no way to set the section type to SHT_MIPS_DWARF. Users have
12453 traditionally had to fall back on the more common @progbits instead.
12454
12455 There's nothing really harmful in this, since bfd will correct
12456 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
708587a4 12457 means that, for backwards compatibility, the special_section entries
8ab8a5c8
RS
12458 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
12459
12460 Even so, we shouldn't force users of the MIPS .section syntax to
12461 incorrectly label the sections as SHT_PROGBITS. The best compromise
12462 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
12463 generic type-checking code. */
12464 if (section_type == SHT_MIPS_DWARF)
12465 section_type = SHT_PROGBITS;
12466
cca86cc8
SC
12467 obj_elf_change_section (section_name, section_type, section_flag,
12468 section_entry_size, 0, 0, 0);
a816d1ed
AO
12469
12470 if (now_seg->name != section_name)
12471 free (section_name);
7ed4a06a 12472#endif /* OBJ_ELF */
cca86cc8 12473}
252b5132
RH
12474
12475void
17a2f251 12476mips_enable_auto_align (void)
252b5132
RH
12477{
12478 auto_align = 1;
12479}
12480
12481static void
17a2f251 12482s_cons (int log_size)
252b5132 12483{
a8dbcb85
TS
12484 segment_info_type *si = seg_info (now_seg);
12485 struct insn_label_list *l = si->label_list;
252b5132
RH
12486 symbolS *label;
12487
a8dbcb85 12488 label = l != NULL ? l->label : NULL;
7d10b47d 12489 mips_emit_delays ();
252b5132
RH
12490 if (log_size > 0 && auto_align)
12491 mips_align (log_size, 0, label);
12492 mips_clear_insn_labels ();
12493 cons (1 << log_size);
12494}
12495
12496static void
17a2f251 12497s_float_cons (int type)
252b5132 12498{
a8dbcb85
TS
12499 segment_info_type *si = seg_info (now_seg);
12500 struct insn_label_list *l = si->label_list;
252b5132
RH
12501 symbolS *label;
12502
a8dbcb85 12503 label = l != NULL ? l->label : NULL;
252b5132 12504
7d10b47d 12505 mips_emit_delays ();
252b5132
RH
12506
12507 if (auto_align)
49309057
ILT
12508 {
12509 if (type == 'd')
12510 mips_align (3, 0, label);
12511 else
12512 mips_align (2, 0, label);
12513 }
252b5132
RH
12514
12515 mips_clear_insn_labels ();
12516
12517 float_cons (type);
12518}
12519
12520/* Handle .globl. We need to override it because on Irix 5 you are
12521 permitted to say
12522 .globl foo .text
12523 where foo is an undefined symbol, to mean that foo should be
12524 considered to be the address of a function. */
12525
12526static void
17a2f251 12527s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
12528{
12529 char *name;
12530 int c;
12531 symbolS *symbolP;
12532 flagword flag;
12533
8a06b769 12534 do
252b5132 12535 {
8a06b769 12536 name = input_line_pointer;
252b5132 12537 c = get_symbol_end ();
8a06b769
TS
12538 symbolP = symbol_find_or_make (name);
12539 S_SET_EXTERNAL (symbolP);
12540
252b5132 12541 *input_line_pointer = c;
8a06b769 12542 SKIP_WHITESPACE ();
252b5132 12543
8a06b769
TS
12544 /* On Irix 5, every global symbol that is not explicitly labelled as
12545 being a function is apparently labelled as being an object. */
12546 flag = BSF_OBJECT;
252b5132 12547
8a06b769
TS
12548 if (!is_end_of_line[(unsigned char) *input_line_pointer]
12549 && (*input_line_pointer != ','))
12550 {
12551 char *secname;
12552 asection *sec;
12553
12554 secname = input_line_pointer;
12555 c = get_symbol_end ();
12556 sec = bfd_get_section_by_name (stdoutput, secname);
12557 if (sec == NULL)
12558 as_bad (_("%s: no such section"), secname);
12559 *input_line_pointer = c;
12560
12561 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
12562 flag = BSF_FUNCTION;
12563 }
12564
12565 symbol_get_bfdsym (symbolP)->flags |= flag;
12566
12567 c = *input_line_pointer;
12568 if (c == ',')
12569 {
12570 input_line_pointer++;
12571 SKIP_WHITESPACE ();
12572 if (is_end_of_line[(unsigned char) *input_line_pointer])
12573 c = '\n';
12574 }
12575 }
12576 while (c == ',');
252b5132 12577
252b5132
RH
12578 demand_empty_rest_of_line ();
12579}
12580
12581static void
17a2f251 12582s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
12583{
12584 char *opt;
12585 char c;
12586
12587 opt = input_line_pointer;
12588 c = get_symbol_end ();
12589
12590 if (*opt == 'O')
12591 {
12592 /* FIXME: What does this mean? */
12593 }
12594 else if (strncmp (opt, "pic", 3) == 0)
12595 {
12596 int i;
12597
12598 i = atoi (opt + 3);
12599 if (i == 0)
12600 mips_pic = NO_PIC;
12601 else if (i == 2)
143d77c5 12602 {
252b5132 12603 mips_pic = SVR4_PIC;
143d77c5
EC
12604 mips_abicalls = TRUE;
12605 }
252b5132
RH
12606 else
12607 as_bad (_(".option pic%d not supported"), i);
12608
4d0d148d 12609 if (mips_pic == SVR4_PIC)
252b5132
RH
12610 {
12611 if (g_switch_seen && g_switch_value != 0)
12612 as_warn (_("-G may not be used with SVR4 PIC code"));
12613 g_switch_value = 0;
12614 bfd_set_gp_size (stdoutput, 0);
12615 }
12616 }
12617 else
12618 as_warn (_("Unrecognized option \"%s\""), opt);
12619
12620 *input_line_pointer = c;
12621 demand_empty_rest_of_line ();
12622}
12623
12624/* This structure is used to hold a stack of .set values. */
12625
e972090a
NC
12626struct mips_option_stack
12627{
252b5132
RH
12628 struct mips_option_stack *next;
12629 struct mips_set_options options;
12630};
12631
12632static struct mips_option_stack *mips_opts_stack;
12633
12634/* Handle the .set pseudo-op. */
12635
12636static void
17a2f251 12637s_mipsset (int x ATTRIBUTE_UNUSED)
252b5132
RH
12638{
12639 char *name = input_line_pointer, ch;
12640
12641 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 12642 ++input_line_pointer;
252b5132
RH
12643 ch = *input_line_pointer;
12644 *input_line_pointer = '\0';
12645
12646 if (strcmp (name, "reorder") == 0)
12647 {
7d10b47d
RS
12648 if (mips_opts.noreorder)
12649 end_noreorder ();
252b5132
RH
12650 }
12651 else if (strcmp (name, "noreorder") == 0)
12652 {
7d10b47d
RS
12653 if (!mips_opts.noreorder)
12654 start_noreorder ();
252b5132 12655 }
741fe287
MR
12656 else if (strncmp (name, "at=", 3) == 0)
12657 {
12658 char *s = name + 3;
12659
12660 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
12661 as_bad (_("Unrecognized register name `%s'"), s);
12662 }
252b5132
RH
12663 else if (strcmp (name, "at") == 0)
12664 {
741fe287 12665 mips_opts.at = ATREG;
252b5132
RH
12666 }
12667 else if (strcmp (name, "noat") == 0)
12668 {
741fe287 12669 mips_opts.at = ZERO;
252b5132
RH
12670 }
12671 else if (strcmp (name, "macro") == 0)
12672 {
12673 mips_opts.warn_about_macros = 0;
12674 }
12675 else if (strcmp (name, "nomacro") == 0)
12676 {
12677 if (mips_opts.noreorder == 0)
12678 as_bad (_("`noreorder' must be set before `nomacro'"));
12679 mips_opts.warn_about_macros = 1;
12680 }
12681 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
12682 {
12683 mips_opts.nomove = 0;
12684 }
12685 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
12686 {
12687 mips_opts.nomove = 1;
12688 }
12689 else if (strcmp (name, "bopt") == 0)
12690 {
12691 mips_opts.nobopt = 0;
12692 }
12693 else if (strcmp (name, "nobopt") == 0)
12694 {
12695 mips_opts.nobopt = 1;
12696 }
ad3fea08
TS
12697 else if (strcmp (name, "gp=default") == 0)
12698 mips_opts.gp32 = file_mips_gp32;
12699 else if (strcmp (name, "gp=32") == 0)
12700 mips_opts.gp32 = 1;
12701 else if (strcmp (name, "gp=64") == 0)
12702 {
12703 if (!ISA_HAS_64BIT_REGS (mips_opts.isa))
12704 as_warn ("%s isa does not support 64-bit registers",
12705 mips_cpu_info_from_isa (mips_opts.isa)->name);
12706 mips_opts.gp32 = 0;
12707 }
12708 else if (strcmp (name, "fp=default") == 0)
12709 mips_opts.fp32 = file_mips_fp32;
12710 else if (strcmp (name, "fp=32") == 0)
12711 mips_opts.fp32 = 1;
12712 else if (strcmp (name, "fp=64") == 0)
12713 {
12714 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
12715 as_warn ("%s isa does not support 64-bit floating point registers",
12716 mips_cpu_info_from_isa (mips_opts.isa)->name);
12717 mips_opts.fp32 = 0;
12718 }
037b32b9
AN
12719 else if (strcmp (name, "softfloat") == 0)
12720 mips_opts.soft_float = 1;
12721 else if (strcmp (name, "hardfloat") == 0)
12722 mips_opts.soft_float = 0;
12723 else if (strcmp (name, "singlefloat") == 0)
12724 mips_opts.single_float = 1;
12725 else if (strcmp (name, "doublefloat") == 0)
12726 mips_opts.single_float = 0;
252b5132
RH
12727 else if (strcmp (name, "mips16") == 0
12728 || strcmp (name, "MIPS-16") == 0)
12729 mips_opts.mips16 = 1;
12730 else if (strcmp (name, "nomips16") == 0
12731 || strcmp (name, "noMIPS-16") == 0)
12732 mips_opts.mips16 = 0;
e16bfa71
TS
12733 else if (strcmp (name, "smartmips") == 0)
12734 {
ad3fea08 12735 if (!ISA_SUPPORTS_SMARTMIPS)
e16bfa71
TS
12736 as_warn ("%s ISA does not support SmartMIPS ASE",
12737 mips_cpu_info_from_isa (mips_opts.isa)->name);
12738 mips_opts.ase_smartmips = 1;
12739 }
12740 else if (strcmp (name, "nosmartmips") == 0)
12741 mips_opts.ase_smartmips = 0;
1f25f5d3
CD
12742 else if (strcmp (name, "mips3d") == 0)
12743 mips_opts.ase_mips3d = 1;
12744 else if (strcmp (name, "nomips3d") == 0)
12745 mips_opts.ase_mips3d = 0;
a4672219
TS
12746 else if (strcmp (name, "mdmx") == 0)
12747 mips_opts.ase_mdmx = 1;
12748 else if (strcmp (name, "nomdmx") == 0)
12749 mips_opts.ase_mdmx = 0;
74cd071d 12750 else if (strcmp (name, "dsp") == 0)
ad3fea08
TS
12751 {
12752 if (!ISA_SUPPORTS_DSP_ASE)
12753 as_warn ("%s ISA does not support DSP ASE",
12754 mips_cpu_info_from_isa (mips_opts.isa)->name);
12755 mips_opts.ase_dsp = 1;
8b082fb1 12756 mips_opts.ase_dspr2 = 0;
ad3fea08 12757 }
74cd071d 12758 else if (strcmp (name, "nodsp") == 0)
8b082fb1
TS
12759 {
12760 mips_opts.ase_dsp = 0;
12761 mips_opts.ase_dspr2 = 0;
12762 }
12763 else if (strcmp (name, "dspr2") == 0)
12764 {
12765 if (!ISA_SUPPORTS_DSPR2_ASE)
12766 as_warn ("%s ISA does not support DSP R2 ASE",
12767 mips_cpu_info_from_isa (mips_opts.isa)->name);
12768 mips_opts.ase_dspr2 = 1;
12769 mips_opts.ase_dsp = 1;
12770 }
12771 else if (strcmp (name, "nodspr2") == 0)
12772 {
12773 mips_opts.ase_dspr2 = 0;
12774 mips_opts.ase_dsp = 0;
12775 }
ef2e4d86 12776 else if (strcmp (name, "mt") == 0)
ad3fea08
TS
12777 {
12778 if (!ISA_SUPPORTS_MT_ASE)
12779 as_warn ("%s ISA does not support MT ASE",
12780 mips_cpu_info_from_isa (mips_opts.isa)->name);
12781 mips_opts.ase_mt = 1;
12782 }
ef2e4d86
CF
12783 else if (strcmp (name, "nomt") == 0)
12784 mips_opts.ase_mt = 0;
1a2c1fad 12785 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 12786 {
af7ee8bf 12787 int reset = 0;
252b5132 12788
1a2c1fad
CD
12789 /* Permit the user to change the ISA and architecture on the fly.
12790 Needless to say, misuse can cause serious problems. */
81a21e38 12791 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
af7ee8bf
CD
12792 {
12793 reset = 1;
12794 mips_opts.isa = file_mips_isa;
1a2c1fad 12795 mips_opts.arch = file_mips_arch;
1a2c1fad
CD
12796 }
12797 else if (strncmp (name, "arch=", 5) == 0)
12798 {
12799 const struct mips_cpu_info *p;
12800
12801 p = mips_parse_cpu("internal use", name + 5);
12802 if (!p)
12803 as_bad (_("unknown architecture %s"), name + 5);
12804 else
12805 {
12806 mips_opts.arch = p->cpu;
12807 mips_opts.isa = p->isa;
12808 }
12809 }
81a21e38
TS
12810 else if (strncmp (name, "mips", 4) == 0)
12811 {
12812 const struct mips_cpu_info *p;
12813
12814 p = mips_parse_cpu("internal use", name);
12815 if (!p)
12816 as_bad (_("unknown ISA level %s"), name + 4);
12817 else
12818 {
12819 mips_opts.arch = p->cpu;
12820 mips_opts.isa = p->isa;
12821 }
12822 }
af7ee8bf 12823 else
81a21e38 12824 as_bad (_("unknown ISA or architecture %s"), name);
af7ee8bf
CD
12825
12826 switch (mips_opts.isa)
98d3f06f
KH
12827 {
12828 case 0:
98d3f06f 12829 break;
af7ee8bf
CD
12830 case ISA_MIPS1:
12831 case ISA_MIPS2:
12832 case ISA_MIPS32:
12833 case ISA_MIPS32R2:
98d3f06f
KH
12834 mips_opts.gp32 = 1;
12835 mips_opts.fp32 = 1;
12836 break;
af7ee8bf
CD
12837 case ISA_MIPS3:
12838 case ISA_MIPS4:
12839 case ISA_MIPS5:
12840 case ISA_MIPS64:
5f74bc13 12841 case ISA_MIPS64R2:
98d3f06f
KH
12842 mips_opts.gp32 = 0;
12843 mips_opts.fp32 = 0;
12844 break;
12845 default:
12846 as_bad (_("unknown ISA level %s"), name + 4);
12847 break;
12848 }
af7ee8bf 12849 if (reset)
98d3f06f 12850 {
af7ee8bf
CD
12851 mips_opts.gp32 = file_mips_gp32;
12852 mips_opts.fp32 = file_mips_fp32;
98d3f06f 12853 }
252b5132
RH
12854 }
12855 else if (strcmp (name, "autoextend") == 0)
12856 mips_opts.noautoextend = 0;
12857 else if (strcmp (name, "noautoextend") == 0)
12858 mips_opts.noautoextend = 1;
12859 else if (strcmp (name, "push") == 0)
12860 {
12861 struct mips_option_stack *s;
12862
12863 s = (struct mips_option_stack *) xmalloc (sizeof *s);
12864 s->next = mips_opts_stack;
12865 s->options = mips_opts;
12866 mips_opts_stack = s;
12867 }
12868 else if (strcmp (name, "pop") == 0)
12869 {
12870 struct mips_option_stack *s;
12871
12872 s = mips_opts_stack;
12873 if (s == NULL)
12874 as_bad (_(".set pop with no .set push"));
12875 else
12876 {
12877 /* If we're changing the reorder mode we need to handle
12878 delay slots correctly. */
12879 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 12880 start_noreorder ();
252b5132 12881 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 12882 end_noreorder ();
252b5132
RH
12883
12884 mips_opts = s->options;
12885 mips_opts_stack = s->next;
12886 free (s);
12887 }
12888 }
aed1a261
RS
12889 else if (strcmp (name, "sym32") == 0)
12890 mips_opts.sym32 = TRUE;
12891 else if (strcmp (name, "nosym32") == 0)
12892 mips_opts.sym32 = FALSE;
e6559e01
JM
12893 else if (strchr (name, ','))
12894 {
12895 /* Generic ".set" directive; use the generic handler. */
12896 *input_line_pointer = ch;
12897 input_line_pointer = name;
12898 s_set (0);
12899 return;
12900 }
252b5132
RH
12901 else
12902 {
12903 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12904 }
12905 *input_line_pointer = ch;
12906 demand_empty_rest_of_line ();
12907}
12908
12909/* Handle the .abicalls pseudo-op. I believe this is equivalent to
12910 .option pic2. It means to generate SVR4 PIC calls. */
12911
12912static void
17a2f251 12913s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12914{
12915 mips_pic = SVR4_PIC;
143d77c5 12916 mips_abicalls = TRUE;
4d0d148d
TS
12917
12918 if (g_switch_seen && g_switch_value != 0)
12919 as_warn (_("-G may not be used with SVR4 PIC code"));
12920 g_switch_value = 0;
12921
252b5132
RH
12922 bfd_set_gp_size (stdoutput, 0);
12923 demand_empty_rest_of_line ();
12924}
12925
12926/* Handle the .cpload pseudo-op. This is used when generating SVR4
12927 PIC code. It sets the $gp register for the function based on the
12928 function address, which is in the register named in the argument.
12929 This uses a relocation against _gp_disp, which is handled specially
12930 by the linker. The result is:
12931 lui $gp,%hi(_gp_disp)
12932 addiu $gp,$gp,%lo(_gp_disp)
12933 addu $gp,$gp,.cpload argument
aa6975fb
ILT
12934 The .cpload argument is normally $25 == $t9.
12935
12936 The -mno-shared option changes this to:
bbe506e8
TS
12937 lui $gp,%hi(__gnu_local_gp)
12938 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
12939 and the argument is ignored. This saves an instruction, but the
12940 resulting code is not position independent; it uses an absolute
bbe506e8
TS
12941 address for __gnu_local_gp. Thus code assembled with -mno-shared
12942 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
12943
12944static void
17a2f251 12945s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12946{
12947 expressionS ex;
aa6975fb
ILT
12948 int reg;
12949 int in_shared;
252b5132 12950
6478892d
TS
12951 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12952 .cpload is ignored. */
12953 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
12954 {
12955 s_ignore (0);
12956 return;
12957 }
12958
d3ecfc59 12959 /* .cpload should be in a .set noreorder section. */
252b5132
RH
12960 if (mips_opts.noreorder == 0)
12961 as_warn (_(".cpload not in noreorder section"));
12962
aa6975fb
ILT
12963 reg = tc_get_register (0);
12964
12965 /* If we need to produce a 64-bit address, we are better off using
12966 the default instruction sequence. */
aed1a261 12967 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 12968
252b5132 12969 ex.X_op = O_symbol;
bbe506e8
TS
12970 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
12971 "__gnu_local_gp");
252b5132
RH
12972 ex.X_op_symbol = NULL;
12973 ex.X_add_number = 0;
12974
12975 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 12976 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 12977
584892a6 12978 macro_start ();
67c0d1eb
RS
12979 macro_build_lui (&ex, mips_gp_register);
12980 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 12981 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
12982 if (in_shared)
12983 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
12984 mips_gp_register, reg);
584892a6 12985 macro_end ();
252b5132
RH
12986
12987 demand_empty_rest_of_line ();
12988}
12989
6478892d
TS
12990/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
12991 .cpsetup $reg1, offset|$reg2, label
12992
12993 If offset is given, this results in:
12994 sd $gp, offset($sp)
956cd1d6 12995 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
12996 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12997 daddu $gp, $gp, $reg1
6478892d
TS
12998
12999 If $reg2 is given, this results in:
13000 daddu $reg2, $gp, $0
956cd1d6 13001 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
13002 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
13003 daddu $gp, $gp, $reg1
aa6975fb
ILT
13004 $reg1 is normally $25 == $t9.
13005
13006 The -mno-shared option replaces the last three instructions with
13007 lui $gp,%hi(_gp)
54f4ddb3 13008 addiu $gp,$gp,%lo(_gp) */
aa6975fb 13009
6478892d 13010static void
17a2f251 13011s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
13012{
13013 expressionS ex_off;
13014 expressionS ex_sym;
13015 int reg1;
6478892d 13016
8586fc66 13017 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
13018 We also need NewABI support. */
13019 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
13020 {
13021 s_ignore (0);
13022 return;
13023 }
13024
13025 reg1 = tc_get_register (0);
13026 SKIP_WHITESPACE ();
13027 if (*input_line_pointer != ',')
13028 {
13029 as_bad (_("missing argument separator ',' for .cpsetup"));
13030 return;
13031 }
13032 else
80245285 13033 ++input_line_pointer;
6478892d
TS
13034 SKIP_WHITESPACE ();
13035 if (*input_line_pointer == '$')
80245285
TS
13036 {
13037 mips_cpreturn_register = tc_get_register (0);
13038 mips_cpreturn_offset = -1;
13039 }
6478892d 13040 else
80245285
TS
13041 {
13042 mips_cpreturn_offset = get_absolute_expression ();
13043 mips_cpreturn_register = -1;
13044 }
6478892d
TS
13045 SKIP_WHITESPACE ();
13046 if (*input_line_pointer != ',')
13047 {
13048 as_bad (_("missing argument separator ',' for .cpsetup"));
13049 return;
13050 }
13051 else
f9419b05 13052 ++input_line_pointer;
6478892d 13053 SKIP_WHITESPACE ();
f21f8242 13054 expression (&ex_sym);
6478892d 13055
584892a6 13056 macro_start ();
6478892d
TS
13057 if (mips_cpreturn_register == -1)
13058 {
13059 ex_off.X_op = O_constant;
13060 ex_off.X_add_symbol = NULL;
13061 ex_off.X_op_symbol = NULL;
13062 ex_off.X_add_number = mips_cpreturn_offset;
13063
67c0d1eb 13064 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 13065 BFD_RELOC_LO16, SP);
6478892d
TS
13066 }
13067 else
67c0d1eb 13068 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
17a2f251 13069 mips_gp_register, 0);
6478892d 13070
aed1a261 13071 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb
ILT
13072 {
13073 macro_build (&ex_sym, "lui", "t,u", mips_gp_register,
13074 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
13075 BFD_RELOC_HI16_S);
13076
13077 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
13078 mips_gp_register, -1, BFD_RELOC_GPREL16,
13079 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
13080
13081 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
13082 mips_gp_register, reg1);
13083 }
13084 else
13085 {
13086 expressionS ex;
13087
13088 ex.X_op = O_symbol;
4184909a 13089 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
13090 ex.X_op_symbol = NULL;
13091 ex.X_add_number = 0;
6e1304d8 13092
aa6975fb
ILT
13093 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
13094 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
13095
13096 macro_build_lui (&ex, mips_gp_register);
13097 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
13098 mips_gp_register, BFD_RELOC_LO16);
13099 }
f21f8242 13100
584892a6 13101 macro_end ();
6478892d
TS
13102
13103 demand_empty_rest_of_line ();
13104}
13105
13106static void
17a2f251 13107s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
13108{
13109 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
54f4ddb3 13110 .cplocal is ignored. */
6478892d
TS
13111 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
13112 {
13113 s_ignore (0);
13114 return;
13115 }
13116
13117 mips_gp_register = tc_get_register (0);
85b51719 13118 demand_empty_rest_of_line ();
6478892d
TS
13119}
13120
252b5132
RH
13121/* Handle the .cprestore pseudo-op. This stores $gp into a given
13122 offset from $sp. The offset is remembered, and after making a PIC
13123 call $gp is restored from that location. */
13124
13125static void
17a2f251 13126s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
13127{
13128 expressionS ex;
252b5132 13129
6478892d 13130 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 13131 .cprestore is ignored. */
6478892d 13132 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
13133 {
13134 s_ignore (0);
13135 return;
13136 }
13137
13138 mips_cprestore_offset = get_absolute_expression ();
7a621144 13139 mips_cprestore_valid = 1;
252b5132
RH
13140
13141 ex.X_op = O_constant;
13142 ex.X_add_symbol = NULL;
13143 ex.X_op_symbol = NULL;
13144 ex.X_add_number = mips_cprestore_offset;
13145
584892a6 13146 macro_start ();
67c0d1eb
RS
13147 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
13148 SP, HAVE_64BIT_ADDRESSES);
584892a6 13149 macro_end ();
252b5132
RH
13150
13151 demand_empty_rest_of_line ();
13152}
13153
6478892d 13154/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 13155 was given in the preceding .cpsetup, it results in:
6478892d 13156 ld $gp, offset($sp)
76b3015f 13157
6478892d 13158 If a register $reg2 was given there, it results in:
54f4ddb3
TS
13159 daddu $gp, $reg2, $0 */
13160
6478892d 13161static void
17a2f251 13162s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
13163{
13164 expressionS ex;
6478892d
TS
13165
13166 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
13167 We also need NewABI support. */
13168 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
13169 {
13170 s_ignore (0);
13171 return;
13172 }
13173
584892a6 13174 macro_start ();
6478892d
TS
13175 if (mips_cpreturn_register == -1)
13176 {
13177 ex.X_op = O_constant;
13178 ex.X_add_symbol = NULL;
13179 ex.X_op_symbol = NULL;
13180 ex.X_add_number = mips_cpreturn_offset;
13181
67c0d1eb 13182 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
13183 }
13184 else
67c0d1eb 13185 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
17a2f251 13186 mips_cpreturn_register, 0);
584892a6 13187 macro_end ();
6478892d
TS
13188
13189 demand_empty_rest_of_line ();
13190}
13191
741d6ea8
JM
13192/* Handle the .dtprelword and .dtpreldword pseudo-ops. They generate
13193 a 32-bit or 64-bit DTP-relative relocation (BYTES says which) for
13194 use in DWARF debug information. */
13195
13196static void
13197s_dtprel_internal (size_t bytes)
13198{
13199 expressionS ex;
13200 char *p;
13201
13202 expression (&ex);
13203
13204 if (ex.X_op != O_symbol)
13205 {
13206 as_bad (_("Unsupported use of %s"), (bytes == 8
13207 ? ".dtpreldword"
13208 : ".dtprelword"));
13209 ignore_rest_of_line ();
13210 }
13211
13212 p = frag_more (bytes);
13213 md_number_to_chars (p, 0, bytes);
13214 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE,
13215 (bytes == 8
13216 ? BFD_RELOC_MIPS_TLS_DTPREL64
13217 : BFD_RELOC_MIPS_TLS_DTPREL32));
13218
13219 demand_empty_rest_of_line ();
13220}
13221
13222/* Handle .dtprelword. */
13223
13224static void
13225s_dtprelword (int ignore ATTRIBUTE_UNUSED)
13226{
13227 s_dtprel_internal (4);
13228}
13229
13230/* Handle .dtpreldword. */
13231
13232static void
13233s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
13234{
13235 s_dtprel_internal (8);
13236}
13237
6478892d
TS
13238/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
13239 code. It sets the offset to use in gp_rel relocations. */
13240
13241static void
17a2f251 13242s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
13243{
13244 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
13245 We also need NewABI support. */
13246 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
13247 {
13248 s_ignore (0);
13249 return;
13250 }
13251
def2e0dd 13252 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
13253
13254 demand_empty_rest_of_line ();
13255}
13256
252b5132
RH
13257/* Handle the .gpword pseudo-op. This is used when generating PIC
13258 code. It generates a 32 bit GP relative reloc. */
13259
13260static void
17a2f251 13261s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132 13262{
a8dbcb85
TS
13263 segment_info_type *si;
13264 struct insn_label_list *l;
252b5132
RH
13265 symbolS *label;
13266 expressionS ex;
13267 char *p;
13268
13269 /* When not generating PIC code, this is treated as .word. */
13270 if (mips_pic != SVR4_PIC)
13271 {
13272 s_cons (2);
13273 return;
13274 }
13275
a8dbcb85
TS
13276 si = seg_info (now_seg);
13277 l = si->label_list;
13278 label = l != NULL ? l->label : NULL;
7d10b47d 13279 mips_emit_delays ();
252b5132
RH
13280 if (auto_align)
13281 mips_align (2, 0, label);
13282 mips_clear_insn_labels ();
13283
13284 expression (&ex);
13285
13286 if (ex.X_op != O_symbol || ex.X_add_number != 0)
13287 {
13288 as_bad (_("Unsupported use of .gpword"));
13289 ignore_rest_of_line ();
13290 }
13291
13292 p = frag_more (4);
17a2f251 13293 md_number_to_chars (p, 0, 4);
b34976b6 13294 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 13295 BFD_RELOC_GPREL32);
252b5132
RH
13296
13297 demand_empty_rest_of_line ();
13298}
13299
10181a0d 13300static void
17a2f251 13301s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d 13302{
a8dbcb85
TS
13303 segment_info_type *si;
13304 struct insn_label_list *l;
10181a0d
AO
13305 symbolS *label;
13306 expressionS ex;
13307 char *p;
13308
13309 /* When not generating PIC code, this is treated as .dword. */
13310 if (mips_pic != SVR4_PIC)
13311 {
13312 s_cons (3);
13313 return;
13314 }
13315
a8dbcb85
TS
13316 si = seg_info (now_seg);
13317 l = si->label_list;
13318 label = l != NULL ? l->label : NULL;
7d10b47d 13319 mips_emit_delays ();
10181a0d
AO
13320 if (auto_align)
13321 mips_align (3, 0, label);
13322 mips_clear_insn_labels ();
13323
13324 expression (&ex);
13325
13326 if (ex.X_op != O_symbol || ex.X_add_number != 0)
13327 {
13328 as_bad (_("Unsupported use of .gpdword"));
13329 ignore_rest_of_line ();
13330 }
13331
13332 p = frag_more (8);
17a2f251 13333 md_number_to_chars (p, 0, 8);
a105a300 13334 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
6e1304d8 13335 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
13336
13337 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8
RS
13338 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
13339 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
13340
13341 demand_empty_rest_of_line ();
13342}
13343
252b5132
RH
13344/* Handle the .cpadd pseudo-op. This is used when dealing with switch
13345 tables in SVR4 PIC code. */
13346
13347static void
17a2f251 13348s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 13349{
252b5132
RH
13350 int reg;
13351
10181a0d
AO
13352 /* This is ignored when not generating SVR4 PIC code. */
13353 if (mips_pic != SVR4_PIC)
252b5132
RH
13354 {
13355 s_ignore (0);
13356 return;
13357 }
13358
13359 /* Add $gp to the register named as an argument. */
584892a6 13360 macro_start ();
252b5132 13361 reg = tc_get_register (0);
67c0d1eb 13362 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 13363 macro_end ();
252b5132 13364
bdaaa2e1 13365 demand_empty_rest_of_line ();
252b5132
RH
13366}
13367
13368/* Handle the .insn pseudo-op. This marks instruction labels in
13369 mips16 mode. This permits the linker to handle them specially,
13370 such as generating jalx instructions when needed. We also make
13371 them odd for the duration of the assembly, in order to generate the
13372 right sort of code. We will make them even in the adjust_symtab
13373 routine, while leaving them marked. This is convenient for the
13374 debugger and the disassembler. The linker knows to make them odd
13375 again. */
13376
13377static void
17a2f251 13378s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 13379{
f9419b05 13380 mips16_mark_labels ();
252b5132
RH
13381
13382 demand_empty_rest_of_line ();
13383}
13384
13385/* Handle a .stabn directive. We need these in order to mark a label
13386 as being a mips16 text label correctly. Sometimes the compiler
13387 will emit a label, followed by a .stabn, and then switch sections.
13388 If the label and .stabn are in mips16 mode, then the label is
13389 really a mips16 text label. */
13390
13391static void
17a2f251 13392s_mips_stab (int type)
252b5132 13393{
f9419b05 13394 if (type == 'n')
252b5132
RH
13395 mips16_mark_labels ();
13396
13397 s_stab (type);
13398}
13399
54f4ddb3 13400/* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
252b5132
RH
13401
13402static void
17a2f251 13403s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
13404{
13405 char *name;
13406 int c;
13407 symbolS *symbolP;
13408 expressionS exp;
13409
13410 name = input_line_pointer;
13411 c = get_symbol_end ();
13412 symbolP = symbol_find_or_make (name);
13413 S_SET_WEAK (symbolP);
13414 *input_line_pointer = c;
13415
13416 SKIP_WHITESPACE ();
13417
13418 if (! is_end_of_line[(unsigned char) *input_line_pointer])
13419 {
13420 if (S_IS_DEFINED (symbolP))
13421 {
956cd1d6 13422 as_bad ("ignoring attempt to redefine symbol %s",
252b5132
RH
13423 S_GET_NAME (symbolP));
13424 ignore_rest_of_line ();
13425 return;
13426 }
bdaaa2e1 13427
252b5132
RH
13428 if (*input_line_pointer == ',')
13429 {
13430 ++input_line_pointer;
13431 SKIP_WHITESPACE ();
13432 }
bdaaa2e1 13433
252b5132
RH
13434 expression (&exp);
13435 if (exp.X_op != O_symbol)
13436 {
13437 as_bad ("bad .weakext directive");
98d3f06f 13438 ignore_rest_of_line ();
252b5132
RH
13439 return;
13440 }
49309057 13441 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
13442 }
13443
13444 demand_empty_rest_of_line ();
13445}
13446
13447/* Parse a register string into a number. Called from the ECOFF code
13448 to parse .frame. The argument is non-zero if this is the frame
13449 register, so that we can record it in mips_frame_reg. */
13450
13451int
17a2f251 13452tc_get_register (int frame)
252b5132 13453{
707bfff6 13454 unsigned int reg;
252b5132
RH
13455
13456 SKIP_WHITESPACE ();
707bfff6
TS
13457 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
13458 reg = 0;
252b5132 13459 if (frame)
7a621144
DJ
13460 {
13461 mips_frame_reg = reg != 0 ? reg : SP;
13462 mips_frame_reg_valid = 1;
13463 mips_cprestore_valid = 0;
13464 }
252b5132
RH
13465 return reg;
13466}
13467
13468valueT
17a2f251 13469md_section_align (asection *seg, valueT addr)
252b5132
RH
13470{
13471 int align = bfd_get_section_alignment (stdoutput, seg);
13472
b4c71f56
TS
13473 if (IS_ELF)
13474 {
13475 /* We don't need to align ELF sections to the full alignment.
13476 However, Irix 5 may prefer that we align them at least to a 16
13477 byte boundary. We don't bother to align the sections if we
13478 are targeted for an embedded system. */
c41e87e3 13479 if (strncmp (TARGET_OS, "elf", 3) == 0)
b4c71f56
TS
13480 return addr;
13481 if (align > 4)
13482 align = 4;
13483 }
252b5132
RH
13484
13485 return ((addr + (1 << align) - 1) & (-1 << align));
13486}
13487
13488/* Utility routine, called from above as well. If called while the
13489 input file is still being read, it's only an approximation. (For
13490 example, a symbol may later become defined which appeared to be
13491 undefined earlier.) */
13492
13493static int
17a2f251 13494nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
13495{
13496 if (sym == 0)
13497 return 0;
13498
4d0d148d 13499 if (g_switch_value > 0)
252b5132
RH
13500 {
13501 const char *symname;
13502 int change;
13503
c9914766 13504 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
13505 register. It can be if it is smaller than the -G size or if
13506 it is in the .sdata or .sbss section. Certain symbols can
c9914766 13507 not be referenced off the $gp, although it appears as though
252b5132
RH
13508 they can. */
13509 symname = S_GET_NAME (sym);
13510 if (symname != (const char *) NULL
13511 && (strcmp (symname, "eprol") == 0
13512 || strcmp (symname, "etext") == 0
13513 || strcmp (symname, "_gp") == 0
13514 || strcmp (symname, "edata") == 0
13515 || strcmp (symname, "_fbss") == 0
13516 || strcmp (symname, "_fdata") == 0
13517 || strcmp (symname, "_ftext") == 0
13518 || strcmp (symname, "end") == 0
13519 || strcmp (symname, "_gp_disp") == 0))
13520 change = 1;
13521 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
13522 && (0
13523#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
13524 || (symbol_get_obj (sym)->ecoff_extern_size != 0
13525 && (symbol_get_obj (sym)->ecoff_extern_size
13526 <= g_switch_value))
252b5132
RH
13527#endif
13528 /* We must defer this decision until after the whole
13529 file has been read, since there might be a .extern
13530 after the first use of this symbol. */
13531 || (before_relaxing
13532#ifndef NO_ECOFF_DEBUGGING
49309057 13533 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
13534#endif
13535 && S_GET_VALUE (sym) == 0)
13536 || (S_GET_VALUE (sym) != 0
13537 && S_GET_VALUE (sym) <= g_switch_value)))
13538 change = 0;
13539 else
13540 {
13541 const char *segname;
13542
13543 segname = segment_name (S_GET_SEGMENT (sym));
13544 assert (strcmp (segname, ".lit8") != 0
13545 && strcmp (segname, ".lit4") != 0);
13546 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
13547 && strcmp (segname, ".sbss") != 0
13548 && strncmp (segname, ".sdata.", 7) != 0
d4dc2f22
TS
13549 && strncmp (segname, ".sbss.", 6) != 0
13550 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
fba2b7f9 13551 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
13552 }
13553 return change;
13554 }
13555 else
c9914766 13556 /* We are not optimizing for the $gp register. */
252b5132
RH
13557 return 1;
13558}
13559
5919d012
RS
13560
13561/* Return true if the given symbol should be considered local for SVR4 PIC. */
13562
13563static bfd_boolean
17a2f251 13564pic_need_relax (symbolS *sym, asection *segtype)
5919d012
RS
13565{
13566 asection *symsec;
5919d012
RS
13567
13568 /* Handle the case of a symbol equated to another symbol. */
13569 while (symbol_equated_reloc_p (sym))
13570 {
13571 symbolS *n;
13572
5f0fe04b 13573 /* It's possible to get a loop here in a badly written program. */
5919d012
RS
13574 n = symbol_get_value_expression (sym)->X_add_symbol;
13575 if (n == sym)
13576 break;
13577 sym = n;
13578 }
13579
df1f3cda
DD
13580 if (symbol_section_p (sym))
13581 return TRUE;
13582
5919d012
RS
13583 symsec = S_GET_SEGMENT (sym);
13584
5919d012
RS
13585 /* This must duplicate the test in adjust_reloc_syms. */
13586 return (symsec != &bfd_und_section
13587 && symsec != &bfd_abs_section
5f0fe04b
TS
13588 && !bfd_is_com_section (symsec)
13589 && !s_is_linkonce (sym, segtype)
5919d012
RS
13590#ifdef OBJ_ELF
13591 /* A global or weak symbol is treated as external. */
f43abd2b 13592 && (!IS_ELF || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym)))
5919d012
RS
13593#endif
13594 );
13595}
13596
13597
252b5132
RH
13598/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
13599 extended opcode. SEC is the section the frag is in. */
13600
13601static int
17a2f251 13602mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132
RH
13603{
13604 int type;
3994f87e 13605 const struct mips16_immed_operand *op;
252b5132
RH
13606 offsetT val;
13607 int mintiny, maxtiny;
13608 segT symsec;
98aa84af 13609 fragS *sym_frag;
252b5132
RH
13610
13611 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
13612 return 0;
13613 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
13614 return 1;
13615
13616 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13617 op = mips16_immed_operands;
13618 while (op->type != type)
13619 {
13620 ++op;
13621 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
13622 }
13623
13624 if (op->unsp)
13625 {
13626 if (type == '<' || type == '>' || type == '[' || type == ']')
13627 {
13628 mintiny = 1;
13629 maxtiny = 1 << op->nbits;
13630 }
13631 else
13632 {
13633 mintiny = 0;
13634 maxtiny = (1 << op->nbits) - 1;
13635 }
13636 }
13637 else
13638 {
13639 mintiny = - (1 << (op->nbits - 1));
13640 maxtiny = (1 << (op->nbits - 1)) - 1;
13641 }
13642
98aa84af 13643 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 13644 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 13645 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
13646
13647 if (op->pcrel)
13648 {
13649 addressT addr;
13650
13651 /* We won't have the section when we are called from
13652 mips_relax_frag. However, we will always have been called
13653 from md_estimate_size_before_relax first. If this is a
13654 branch to a different section, we mark it as such. If SEC is
13655 NULL, and the frag is not marked, then it must be a branch to
13656 the same section. */
13657 if (sec == NULL)
13658 {
13659 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
13660 return 1;
13661 }
13662 else
13663 {
98aa84af 13664 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
13665 if (symsec != sec)
13666 {
13667 fragp->fr_subtype =
13668 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13669
13670 /* FIXME: We should support this, and let the linker
13671 catch branches and loads that are out of range. */
13672 as_bad_where (fragp->fr_file, fragp->fr_line,
13673 _("unsupported PC relative reference to different section"));
13674
13675 return 1;
13676 }
98aa84af
AM
13677 if (fragp != sym_frag && sym_frag->fr_address == 0)
13678 /* Assume non-extended on the first relaxation pass.
13679 The address we have calculated will be bogus if this is
13680 a forward branch to another frag, as the forward frag
13681 will have fr_address == 0. */
13682 return 0;
252b5132
RH
13683 }
13684
13685 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
13686 the same section. If the relax_marker of the symbol fragment
13687 differs from the relax_marker of this fragment, we have not
13688 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
13689 in STRETCH in order to get a better estimate of the address.
13690 This particularly matters because of the shift bits. */
13691 if (stretch != 0
98aa84af 13692 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
13693 {
13694 fragS *f;
13695
13696 /* Adjust stretch for any alignment frag. Note that if have
13697 been expanding the earlier code, the symbol may be
13698 defined in what appears to be an earlier frag. FIXME:
13699 This doesn't handle the fr_subtype field, which specifies
13700 a maximum number of bytes to skip when doing an
13701 alignment. */
98aa84af 13702 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
13703 {
13704 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
13705 {
13706 if (stretch < 0)
13707 stretch = - ((- stretch)
13708 & ~ ((1 << (int) f->fr_offset) - 1));
13709 else
13710 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
13711 if (stretch == 0)
13712 break;
13713 }
13714 }
13715 if (f != NULL)
13716 val += stretch;
13717 }
13718
13719 addr = fragp->fr_address + fragp->fr_fix;
13720
13721 /* The base address rules are complicated. The base address of
13722 a branch is the following instruction. The base address of a
13723 PC relative load or add is the instruction itself, but if it
13724 is in a delay slot (in which case it can not be extended) use
13725 the address of the instruction whose delay slot it is in. */
13726 if (type == 'p' || type == 'q')
13727 {
13728 addr += 2;
13729
13730 /* If we are currently assuming that this frag should be
13731 extended, then, the current address is two bytes
bdaaa2e1 13732 higher. */
252b5132
RH
13733 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13734 addr += 2;
13735
13736 /* Ignore the low bit in the target, since it will be set
13737 for a text label. */
13738 if ((val & 1) != 0)
13739 --val;
13740 }
13741 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13742 addr -= 4;
13743 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13744 addr -= 2;
13745
13746 val -= addr & ~ ((1 << op->shift) - 1);
13747
13748 /* Branch offsets have an implicit 0 in the lowest bit. */
13749 if (type == 'p' || type == 'q')
13750 val /= 2;
13751
13752 /* If any of the shifted bits are set, we must use an extended
13753 opcode. If the address depends on the size of this
13754 instruction, this can lead to a loop, so we arrange to always
13755 use an extended opcode. We only check this when we are in
13756 the main relaxation loop, when SEC is NULL. */
13757 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
13758 {
13759 fragp->fr_subtype =
13760 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13761 return 1;
13762 }
13763
13764 /* If we are about to mark a frag as extended because the value
13765 is precisely maxtiny + 1, then there is a chance of an
13766 infinite loop as in the following code:
13767 la $4,foo
13768 .skip 1020
13769 .align 2
13770 foo:
13771 In this case when the la is extended, foo is 0x3fc bytes
13772 away, so the la can be shrunk, but then foo is 0x400 away, so
13773 the la must be extended. To avoid this loop, we mark the
13774 frag as extended if it was small, and is about to become
13775 extended with a value of maxtiny + 1. */
13776 if (val == ((maxtiny + 1) << op->shift)
13777 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
13778 && sec == NULL)
13779 {
13780 fragp->fr_subtype =
13781 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13782 return 1;
13783 }
13784 }
13785 else if (symsec != absolute_section && sec != NULL)
13786 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
13787
13788 if ((val & ((1 << op->shift) - 1)) != 0
13789 || val < (mintiny << op->shift)
13790 || val > (maxtiny << op->shift))
13791 return 1;
13792 else
13793 return 0;
13794}
13795
4a6a3df4
AO
13796/* Compute the length of a branch sequence, and adjust the
13797 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
13798 worst-case length is computed, with UPDATE being used to indicate
13799 whether an unconditional (-1), branch-likely (+1) or regular (0)
13800 branch is to be computed. */
13801static int
17a2f251 13802relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 13803{
b34976b6 13804 bfd_boolean toofar;
4a6a3df4
AO
13805 int length;
13806
13807 if (fragp
13808 && S_IS_DEFINED (fragp->fr_symbol)
13809 && sec == S_GET_SEGMENT (fragp->fr_symbol))
13810 {
13811 addressT addr;
13812 offsetT val;
13813
13814 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
13815
13816 addr = fragp->fr_address + fragp->fr_fix + 4;
13817
13818 val -= addr;
13819
13820 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
13821 }
13822 else if (fragp)
13823 /* If the symbol is not defined or it's in a different segment,
13824 assume the user knows what's going on and emit a short
13825 branch. */
b34976b6 13826 toofar = FALSE;
4a6a3df4 13827 else
b34976b6 13828 toofar = TRUE;
4a6a3df4
AO
13829
13830 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13831 fragp->fr_subtype
af6ae2ad 13832 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
13833 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
13834 RELAX_BRANCH_LINK (fragp->fr_subtype),
13835 toofar);
13836
13837 length = 4;
13838 if (toofar)
13839 {
13840 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
13841 length += 8;
13842
13843 if (mips_pic != NO_PIC)
13844 {
13845 /* Additional space for PIC loading of target address. */
13846 length += 8;
13847 if (mips_opts.isa == ISA_MIPS1)
13848 /* Additional space for $at-stabilizing nop. */
13849 length += 4;
13850 }
13851
13852 /* If branch is conditional. */
13853 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
13854 length += 8;
13855 }
b34976b6 13856
4a6a3df4
AO
13857 return length;
13858}
13859
252b5132
RH
13860/* Estimate the size of a frag before relaxing. Unless this is the
13861 mips16, we are not really relaxing here, and the final size is
13862 encoded in the subtype information. For the mips16, we have to
13863 decide whether we are using an extended opcode or not. */
13864
252b5132 13865int
17a2f251 13866md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 13867{
5919d012 13868 int change;
252b5132 13869
4a6a3df4
AO
13870 if (RELAX_BRANCH_P (fragp->fr_subtype))
13871 {
13872
b34976b6
AM
13873 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
13874
4a6a3df4
AO
13875 return fragp->fr_var;
13876 }
13877
252b5132 13878 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
13879 /* We don't want to modify the EXTENDED bit here; it might get us
13880 into infinite loops. We change it only in mips_relax_frag(). */
13881 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132
RH
13882
13883 if (mips_pic == NO_PIC)
5919d012 13884 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132 13885 else if (mips_pic == SVR4_PIC)
5919d012 13886 change = pic_need_relax (fragp->fr_symbol, segtype);
0a44bf69
RS
13887 else if (mips_pic == VXWORKS_PIC)
13888 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
13889 change = 0;
252b5132
RH
13890 else
13891 abort ();
13892
13893 if (change)
13894 {
4d7206a2 13895 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 13896 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 13897 }
4d7206a2
RS
13898 else
13899 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
13900}
13901
13902/* This is called to see whether a reloc against a defined symbol
de7e6852 13903 should be converted into a reloc against a section. */
252b5132
RH
13904
13905int
17a2f251 13906mips_fix_adjustable (fixS *fixp)
252b5132 13907{
252b5132
RH
13908 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13909 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13910 return 0;
a161fe53 13911
252b5132
RH
13912 if (fixp->fx_addsy == NULL)
13913 return 1;
a161fe53 13914
de7e6852
RS
13915 /* If symbol SYM is in a mergeable section, relocations of the form
13916 SYM + 0 can usually be made section-relative. The mergeable data
13917 is then identified by the section offset rather than by the symbol.
13918
13919 However, if we're generating REL LO16 relocations, the offset is split
13920 between the LO16 and parterning high part relocation. The linker will
13921 need to recalculate the complete offset in order to correctly identify
13922 the merge data.
13923
13924 The linker has traditionally not looked for the parterning high part
13925 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
13926 placed anywhere. Rather than break backwards compatibility by changing
13927 this, it seems better not to force the issue, and instead keep the
13928 original symbol. This will work with either linker behavior. */
704803a9
MR
13929 if ((fixp->fx_r_type == BFD_RELOC_LO16
13930 || fixp->fx_r_type == BFD_RELOC_MIPS16_LO16
13931 || reloc_needs_lo_p (fixp->fx_r_type))
de7e6852
RS
13932 && HAVE_IN_PLACE_ADDENDS
13933 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
13934 return 0;
13935
252b5132 13936#ifdef OBJ_ELF
b314ec0e
RS
13937 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
13938 to a floating-point stub. The same is true for non-R_MIPS16_26
13939 relocations against MIPS16 functions; in this case, the stub becomes
13940 the function's canonical address.
13941
13942 Floating-point stubs are stored in unique .mips16.call.* or
13943 .mips16.fn.* sections. If a stub T for function F is in section S,
13944 the first relocation in section S must be against F; this is how the
13945 linker determines the target function. All relocations that might
13946 resolve to T must also be against F. We therefore have the following
13947 restrictions, which are given in an intentionally-redundant way:
13948
13949 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
13950 symbols.
13951
13952 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
13953 if that stub might be used.
13954
13955 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
13956 symbols.
13957
13958 4. We cannot reduce a stub's relocations against MIPS16 symbols if
13959 that stub might be used.
13960
13961 There is a further restriction:
13962
13963 5. We cannot reduce R_MIPS16_26 relocations against MIPS16 symbols
13964 on targets with in-place addends; the relocation field cannot
13965 encode the low bit.
13966
13967 For simplicity, we deal with (3)-(5) by not reducing _any_ relocation
13968 against a MIPS16 symbol.
13969
13970 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
13971 relocation against some symbol R, no relocation against R may be
13972 reduced. (Note that this deals with (2) as well as (1) because
13973 relocations against global symbols will never be reduced on ELF
13974 targets.) This approach is a little simpler than trying to detect
13975 stub sections, and gives the "all or nothing" per-symbol consistency
13976 that we have for MIPS16 symbols. */
f43abd2b 13977 if (IS_ELF
b314ec0e 13978 && fixp->fx_subsy == NULL
30c09090 13979 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
b314ec0e 13980 || *symbol_get_tc (fixp->fx_addsy)))
252b5132
RH
13981 return 0;
13982#endif
a161fe53 13983
252b5132
RH
13984 return 1;
13985}
13986
13987/* Translate internal representation of relocation info to BFD target
13988 format. */
13989
13990arelent **
17a2f251 13991tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
13992{
13993 static arelent *retval[4];
13994 arelent *reloc;
13995 bfd_reloc_code_real_type code;
13996
4b0cff4e
TS
13997 memset (retval, 0, sizeof(retval));
13998 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
49309057
ILT
13999 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
14000 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
14001 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
14002
bad36eac
DJ
14003 if (fixp->fx_pcrel)
14004 {
14005 assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2);
14006
14007 /* At this point, fx_addnumber is "symbol offset - pcrel address".
14008 Relocations want only the symbol offset. */
14009 reloc->addend = fixp->fx_addnumber + reloc->address;
f43abd2b 14010 if (!IS_ELF)
bad36eac
DJ
14011 {
14012 /* A gruesome hack which is a result of the gruesome gas
14013 reloc handling. What's worse, for COFF (as opposed to
14014 ECOFF), we might need yet another copy of reloc->address.
14015 See bfd_install_relocation. */
14016 reloc->addend += reloc->address;
14017 }
14018 }
14019 else
14020 reloc->addend = fixp->fx_addnumber;
252b5132 14021
438c16b8
TS
14022 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
14023 entry to be used in the relocation's section offset. */
14024 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
14025 {
14026 reloc->address = reloc->addend;
14027 reloc->addend = 0;
14028 }
14029
252b5132 14030 code = fixp->fx_r_type;
252b5132 14031
bad36eac 14032 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
252b5132
RH
14033 if (reloc->howto == NULL)
14034 {
14035 as_bad_where (fixp->fx_file, fixp->fx_line,
14036 _("Can not represent %s relocation in this object file format"),
14037 bfd_get_reloc_code_name (code));
14038 retval[0] = NULL;
14039 }
14040
14041 return retval;
14042}
14043
14044/* Relax a machine dependent frag. This returns the amount by which
14045 the current size of the frag should change. */
14046
14047int
17a2f251 14048mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 14049{
4a6a3df4
AO
14050 if (RELAX_BRANCH_P (fragp->fr_subtype))
14051 {
14052 offsetT old_var = fragp->fr_var;
b34976b6
AM
14053
14054 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
14055
14056 return fragp->fr_var - old_var;
14057 }
14058
252b5132
RH
14059 if (! RELAX_MIPS16_P (fragp->fr_subtype))
14060 return 0;
14061
c4e7957c 14062 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
14063 {
14064 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
14065 return 0;
14066 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
14067 return 2;
14068 }
14069 else
14070 {
14071 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
14072 return 0;
14073 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
14074 return -2;
14075 }
14076
14077 return 0;
14078}
14079
14080/* Convert a machine dependent frag. */
14081
14082void
17a2f251 14083md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 14084{
4a6a3df4
AO
14085 if (RELAX_BRANCH_P (fragp->fr_subtype))
14086 {
14087 bfd_byte *buf;
14088 unsigned long insn;
14089 expressionS exp;
14090 fixS *fixp;
b34976b6 14091
4a6a3df4
AO
14092 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
14093
14094 if (target_big_endian)
14095 insn = bfd_getb32 (buf);
14096 else
14097 insn = bfd_getl32 (buf);
b34976b6 14098
4a6a3df4
AO
14099 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
14100 {
14101 /* We generate a fixup instead of applying it right now
14102 because, if there are linker relaxations, we're going to
14103 need the relocations. */
14104 exp.X_op = O_symbol;
14105 exp.X_add_symbol = fragp->fr_symbol;
14106 exp.X_add_number = fragp->fr_offset;
14107
14108 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
3994f87e 14109 4, &exp, TRUE, BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
14110 fixp->fx_file = fragp->fr_file;
14111 fixp->fx_line = fragp->fr_line;
b34976b6 14112
2132e3a3 14113 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
14114 buf += 4;
14115 }
14116 else
14117 {
14118 int i;
14119
14120 as_warn_where (fragp->fr_file, fragp->fr_line,
14121 _("relaxed out-of-range branch into a jump"));
14122
14123 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
14124 goto uncond;
14125
14126 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
14127 {
14128 /* Reverse the branch. */
14129 switch ((insn >> 28) & 0xf)
14130 {
14131 case 4:
14132 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
14133 have the condition reversed by tweaking a single
14134 bit, and their opcodes all have 0x4???????. */
14135 assert ((insn & 0xf1000000) == 0x41000000);
14136 insn ^= 0x00010000;
14137 break;
14138
14139 case 0:
14140 /* bltz 0x04000000 bgez 0x04010000
54f4ddb3 14141 bltzal 0x04100000 bgezal 0x04110000 */
4a6a3df4
AO
14142 assert ((insn & 0xfc0e0000) == 0x04000000);
14143 insn ^= 0x00010000;
14144 break;
b34976b6 14145
4a6a3df4
AO
14146 case 1:
14147 /* beq 0x10000000 bne 0x14000000
54f4ddb3 14148 blez 0x18000000 bgtz 0x1c000000 */
4a6a3df4
AO
14149 insn ^= 0x04000000;
14150 break;
14151
14152 default:
14153 abort ();
14154 }
14155 }
14156
14157 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
14158 {
14159 /* Clear the and-link bit. */
14160 assert ((insn & 0xfc1c0000) == 0x04100000);
14161
54f4ddb3
TS
14162 /* bltzal 0x04100000 bgezal 0x04110000
14163 bltzall 0x04120000 bgezall 0x04130000 */
4a6a3df4
AO
14164 insn &= ~0x00100000;
14165 }
14166
14167 /* Branch over the branch (if the branch was likely) or the
14168 full jump (not likely case). Compute the offset from the
14169 current instruction to branch to. */
14170 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
14171 i = 16;
14172 else
14173 {
14174 /* How many bytes in instructions we've already emitted? */
14175 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
14176 /* How many bytes in instructions from here to the end? */
14177 i = fragp->fr_var - i;
14178 }
14179 /* Convert to instruction count. */
14180 i >>= 2;
14181 /* Branch counts from the next instruction. */
b34976b6 14182 i--;
4a6a3df4
AO
14183 insn |= i;
14184 /* Branch over the jump. */
2132e3a3 14185 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
14186 buf += 4;
14187
54f4ddb3 14188 /* nop */
2132e3a3 14189 md_number_to_chars ((char *) buf, 0, 4);
4a6a3df4
AO
14190 buf += 4;
14191
14192 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
14193 {
14194 /* beql $0, $0, 2f */
14195 insn = 0x50000000;
14196 /* Compute the PC offset from the current instruction to
14197 the end of the variable frag. */
14198 /* How many bytes in instructions we've already emitted? */
14199 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
14200 /* How many bytes in instructions from here to the end? */
14201 i = fragp->fr_var - i;
14202 /* Convert to instruction count. */
14203 i >>= 2;
14204 /* Don't decrement i, because we want to branch over the
14205 delay slot. */
14206
14207 insn |= i;
2132e3a3 14208 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
14209 buf += 4;
14210
2132e3a3 14211 md_number_to_chars ((char *) buf, 0, 4);
4a6a3df4
AO
14212 buf += 4;
14213 }
14214
14215 uncond:
14216 if (mips_pic == NO_PIC)
14217 {
14218 /* j or jal. */
14219 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
14220 ? 0x0c000000 : 0x08000000);
14221 exp.X_op = O_symbol;
14222 exp.X_add_symbol = fragp->fr_symbol;
14223 exp.X_add_number = fragp->fr_offset;
14224
14225 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
3994f87e 14226 4, &exp, FALSE, BFD_RELOC_MIPS_JMP);
4a6a3df4
AO
14227 fixp->fx_file = fragp->fr_file;
14228 fixp->fx_line = fragp->fr_line;
14229
2132e3a3 14230 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
14231 buf += 4;
14232 }
14233 else
14234 {
14235 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
14236 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
14237 exp.X_op = O_symbol;
14238 exp.X_add_symbol = fragp->fr_symbol;
14239 exp.X_add_number = fragp->fr_offset;
14240
14241 if (fragp->fr_offset)
14242 {
14243 exp.X_add_symbol = make_expr_symbol (&exp);
14244 exp.X_add_number = 0;
14245 }
14246
14247 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
3994f87e 14248 4, &exp, FALSE, BFD_RELOC_MIPS_GOT16);
4a6a3df4
AO
14249 fixp->fx_file = fragp->fr_file;
14250 fixp->fx_line = fragp->fr_line;
14251
2132e3a3 14252 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4 14253 buf += 4;
b34976b6 14254
4a6a3df4
AO
14255 if (mips_opts.isa == ISA_MIPS1)
14256 {
14257 /* nop */
2132e3a3 14258 md_number_to_chars ((char *) buf, 0, 4);
4a6a3df4
AO
14259 buf += 4;
14260 }
14261
14262 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
14263 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
14264
14265 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
3994f87e 14266 4, &exp, FALSE, BFD_RELOC_LO16);
4a6a3df4
AO
14267 fixp->fx_file = fragp->fr_file;
14268 fixp->fx_line = fragp->fr_line;
b34976b6 14269
2132e3a3 14270 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
14271 buf += 4;
14272
14273 /* j(al)r $at. */
14274 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
14275 insn = 0x0020f809;
14276 else
14277 insn = 0x00200008;
14278
2132e3a3 14279 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
14280 buf += 4;
14281 }
14282 }
14283
14284 assert (buf == (bfd_byte *)fragp->fr_literal
14285 + fragp->fr_fix + fragp->fr_var);
14286
14287 fragp->fr_fix += fragp->fr_var;
14288
14289 return;
14290 }
14291
252b5132
RH
14292 if (RELAX_MIPS16_P (fragp->fr_subtype))
14293 {
14294 int type;
3994f87e 14295 const struct mips16_immed_operand *op;
b34976b6 14296 bfd_boolean small, ext;
252b5132
RH
14297 offsetT val;
14298 bfd_byte *buf;
14299 unsigned long insn;
b34976b6 14300 bfd_boolean use_extend;
252b5132
RH
14301 unsigned short extend;
14302
14303 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
14304 op = mips16_immed_operands;
14305 while (op->type != type)
14306 ++op;
14307
14308 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
14309 {
b34976b6
AM
14310 small = FALSE;
14311 ext = TRUE;
252b5132
RH
14312 }
14313 else
14314 {
b34976b6
AM
14315 small = TRUE;
14316 ext = FALSE;
252b5132
RH
14317 }
14318
6386f3a7 14319 resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
14320 val = S_GET_VALUE (fragp->fr_symbol);
14321 if (op->pcrel)
14322 {
14323 addressT addr;
14324
14325 addr = fragp->fr_address + fragp->fr_fix;
14326
14327 /* The rules for the base address of a PC relative reloc are
14328 complicated; see mips16_extended_frag. */
14329 if (type == 'p' || type == 'q')
14330 {
14331 addr += 2;
14332 if (ext)
14333 addr += 2;
14334 /* Ignore the low bit in the target, since it will be
14335 set for a text label. */
14336 if ((val & 1) != 0)
14337 --val;
14338 }
14339 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
14340 addr -= 4;
14341 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
14342 addr -= 2;
14343
14344 addr &= ~ (addressT) ((1 << op->shift) - 1);
14345 val -= addr;
14346
14347 /* Make sure the section winds up with the alignment we have
14348 assumed. */
14349 if (op->shift > 0)
14350 record_alignment (asec, op->shift);
14351 }
14352
14353 if (ext
14354 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
14355 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
14356 as_warn_where (fragp->fr_file, fragp->fr_line,
14357 _("extended instruction in delay slot"));
14358
14359 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
14360
14361 if (target_big_endian)
14362 insn = bfd_getb16 (buf);
14363 else
14364 insn = bfd_getl16 (buf);
14365
14366 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
14367 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
14368 small, ext, &insn, &use_extend, &extend);
14369
14370 if (use_extend)
14371 {
2132e3a3 14372 md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
252b5132
RH
14373 fragp->fr_fix += 2;
14374 buf += 2;
14375 }
14376
2132e3a3 14377 md_number_to_chars ((char *) buf, insn, 2);
252b5132
RH
14378 fragp->fr_fix += 2;
14379 buf += 2;
14380 }
14381 else
14382 {
4d7206a2
RS
14383 int first, second;
14384 fixS *fixp;
252b5132 14385
4d7206a2
RS
14386 first = RELAX_FIRST (fragp->fr_subtype);
14387 second = RELAX_SECOND (fragp->fr_subtype);
14388 fixp = (fixS *) fragp->fr_opcode;
252b5132 14389
584892a6
RS
14390 /* Possibly emit a warning if we've chosen the longer option. */
14391 if (((fragp->fr_subtype & RELAX_USE_SECOND) != 0)
14392 == ((fragp->fr_subtype & RELAX_SECOND_LONGER) != 0))
14393 {
14394 const char *msg = macro_warning (fragp->fr_subtype);
14395 if (msg != 0)
14396 as_warn_where (fragp->fr_file, fragp->fr_line, msg);
14397 }
14398
4d7206a2
RS
14399 /* Go through all the fixups for the first sequence. Disable them
14400 (by marking them as done) if we're going to use the second
14401 sequence instead. */
14402 while (fixp
14403 && fixp->fx_frag == fragp
14404 && fixp->fx_where < fragp->fr_fix - second)
14405 {
14406 if (fragp->fr_subtype & RELAX_USE_SECOND)
14407 fixp->fx_done = 1;
14408 fixp = fixp->fx_next;
14409 }
252b5132 14410
4d7206a2
RS
14411 /* Go through the fixups for the second sequence. Disable them if
14412 we're going to use the first sequence, otherwise adjust their
14413 addresses to account for the relaxation. */
14414 while (fixp && fixp->fx_frag == fragp)
14415 {
14416 if (fragp->fr_subtype & RELAX_USE_SECOND)
14417 fixp->fx_where -= first;
14418 else
14419 fixp->fx_done = 1;
14420 fixp = fixp->fx_next;
14421 }
14422
14423 /* Now modify the frag contents. */
14424 if (fragp->fr_subtype & RELAX_USE_SECOND)
14425 {
14426 char *start;
14427
14428 start = fragp->fr_literal + fragp->fr_fix - first - second;
14429 memmove (start, start + first, second);
14430 fragp->fr_fix -= first;
14431 }
14432 else
14433 fragp->fr_fix -= second;
252b5132
RH
14434 }
14435}
14436
14437#ifdef OBJ_ELF
14438
14439/* This function is called after the relocs have been generated.
14440 We've been storing mips16 text labels as odd. Here we convert them
14441 back to even for the convenience of the debugger. */
14442
14443void
17a2f251 14444mips_frob_file_after_relocs (void)
252b5132
RH
14445{
14446 asymbol **syms;
14447 unsigned int count, i;
14448
f43abd2b 14449 if (!IS_ELF)
252b5132
RH
14450 return;
14451
14452 syms = bfd_get_outsymbols (stdoutput);
14453 count = bfd_get_symcount (stdoutput);
14454 for (i = 0; i < count; i++, syms++)
14455 {
30c09090 14456 if (ELF_ST_IS_MIPS16 (elf_symbol (*syms)->internal_elf_sym.st_other)
252b5132
RH
14457 && ((*syms)->value & 1) != 0)
14458 {
14459 (*syms)->value &= ~1;
14460 /* If the symbol has an odd size, it was probably computed
14461 incorrectly, so adjust that as well. */
14462 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
14463 ++elf_symbol (*syms)->internal_elf_sym.st_size;
14464 }
14465 }
14466}
14467
14468#endif
14469
14470/* This function is called whenever a label is defined. It is used
14471 when handling branch delays; if a branch has a label, we assume we
14472 can not move it. */
14473
14474void
17a2f251 14475mips_define_label (symbolS *sym)
252b5132 14476{
a8dbcb85 14477 segment_info_type *si = seg_info (now_seg);
252b5132
RH
14478 struct insn_label_list *l;
14479
14480 if (free_insn_labels == NULL)
14481 l = (struct insn_label_list *) xmalloc (sizeof *l);
14482 else
14483 {
14484 l = free_insn_labels;
14485 free_insn_labels = l->next;
14486 }
14487
14488 l->label = sym;
a8dbcb85
TS
14489 l->next = si->label_list;
14490 si->label_list = l;
07a53e5c
RH
14491
14492#ifdef OBJ_ELF
14493 dwarf2_emit_label (sym);
14494#endif
252b5132
RH
14495}
14496\f
14497#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14498
14499/* Some special processing for a MIPS ELF file. */
14500
14501void
17a2f251 14502mips_elf_final_processing (void)
252b5132
RH
14503{
14504 /* Write out the register information. */
316f5878 14505 if (mips_abi != N64_ABI)
252b5132
RH
14506 {
14507 Elf32_RegInfo s;
14508
14509 s.ri_gprmask = mips_gprmask;
14510 s.ri_cprmask[0] = mips_cprmask[0];
14511 s.ri_cprmask[1] = mips_cprmask[1];
14512 s.ri_cprmask[2] = mips_cprmask[2];
14513 s.ri_cprmask[3] = mips_cprmask[3];
14514 /* The gp_value field is set by the MIPS ELF backend. */
14515
14516 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
14517 ((Elf32_External_RegInfo *)
14518 mips_regmask_frag));
14519 }
14520 else
14521 {
14522 Elf64_Internal_RegInfo s;
14523
14524 s.ri_gprmask = mips_gprmask;
14525 s.ri_pad = 0;
14526 s.ri_cprmask[0] = mips_cprmask[0];
14527 s.ri_cprmask[1] = mips_cprmask[1];
14528 s.ri_cprmask[2] = mips_cprmask[2];
14529 s.ri_cprmask[3] = mips_cprmask[3];
14530 /* The gp_value field is set by the MIPS ELF backend. */
14531
14532 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
14533 ((Elf64_External_RegInfo *)
14534 mips_regmask_frag));
14535 }
14536
14537 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
14538 sort of BFD interface for this. */
14539 if (mips_any_noreorder)
14540 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
14541 if (mips_pic != NO_PIC)
143d77c5 14542 {
252b5132 14543 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
14544 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
14545 }
14546 if (mips_abicalls)
14547 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 14548
98d3f06f 14549 /* Set MIPS ELF flags for ASEs. */
74cd071d
CF
14550 /* We may need to define a new flag for DSP ASE, and set this flag when
14551 file_ase_dsp is true. */
8b082fb1 14552 /* Same for DSP R2. */
ef2e4d86
CF
14553 /* We may need to define a new flag for MT ASE, and set this flag when
14554 file_ase_mt is true. */
a4672219
TS
14555 if (file_ase_mips16)
14556 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
1f25f5d3
CD
14557#if 0 /* XXX FIXME */
14558 if (file_ase_mips3d)
14559 elf_elfheader (stdoutput)->e_flags |= ???;
14560#endif
deec1734
CD
14561 if (file_ase_mdmx)
14562 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 14563
bdaaa2e1 14564 /* Set the MIPS ELF ABI flags. */
316f5878 14565 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 14566 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 14567 else if (mips_abi == O64_ABI)
252b5132 14568 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 14569 else if (mips_abi == EABI_ABI)
252b5132 14570 {
316f5878 14571 if (!file_mips_gp32)
252b5132
RH
14572 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
14573 else
14574 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
14575 }
316f5878 14576 else if (mips_abi == N32_ABI)
be00bddd
TS
14577 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
14578
c9914766 14579 /* Nothing to do for N64_ABI. */
252b5132
RH
14580
14581 if (mips_32bitmode)
14582 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
ad3fea08
TS
14583
14584#if 0 /* XXX FIXME */
14585 /* 32 bit code with 64 bit FP registers. */
14586 if (!file_mips_fp32 && ABI_NEEDS_32BIT_REGS (mips_abi))
14587 elf_elfheader (stdoutput)->e_flags |= ???;
14588#endif
252b5132
RH
14589}
14590
14591#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
14592\f
beae10d5 14593typedef struct proc {
9b2f1d35
EC
14594 symbolS *func_sym;
14595 symbolS *func_end_sym;
beae10d5
KH
14596 unsigned long reg_mask;
14597 unsigned long reg_offset;
14598 unsigned long fpreg_mask;
14599 unsigned long fpreg_offset;
14600 unsigned long frame_offset;
14601 unsigned long frame_reg;
14602 unsigned long pc_reg;
14603} procS;
252b5132
RH
14604
14605static procS cur_proc;
14606static procS *cur_proc_ptr;
14607static int numprocs;
14608
742a56fe
RS
14609/* Implement NOP_OPCODE. We encode a MIPS16 nop as "1" and a normal
14610 nop as "0". */
14611
14612char
14613mips_nop_opcode (void)
14614{
14615 return seg_info (now_seg)->tc_segment_info_data.mips16;
14616}
14617
14618/* Fill in an rs_align_code fragment. This only needs to do something
14619 for MIPS16 code, where 0 is not a nop. */
a19d8eb0 14620
0a9ef439 14621void
17a2f251 14622mips_handle_align (fragS *fragp)
a19d8eb0 14623{
742a56fe
RS
14624 char *p;
14625
0a9ef439
RH
14626 if (fragp->fr_type != rs_align_code)
14627 return;
14628
742a56fe
RS
14629 p = fragp->fr_literal + fragp->fr_fix;
14630 if (*p)
a19d8eb0 14631 {
0a9ef439 14632 int bytes;
a19d8eb0 14633
0a9ef439 14634 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
0a9ef439
RH
14635 if (bytes & 1)
14636 {
14637 *p++ = 0;
f9419b05 14638 fragp->fr_fix++;
0a9ef439 14639 }
742a56fe 14640 md_number_to_chars (p, mips16_nop_insn.insn_opcode, 2);
0a9ef439 14641 fragp->fr_var = 2;
a19d8eb0 14642 }
a19d8eb0
CP
14643}
14644
252b5132 14645static void
17a2f251 14646md_obj_begin (void)
252b5132
RH
14647{
14648}
14649
14650static void
17a2f251 14651md_obj_end (void)
252b5132 14652{
54f4ddb3 14653 /* Check for premature end, nesting errors, etc. */
252b5132 14654 if (cur_proc_ptr)
9a41af64 14655 as_warn (_("missing .end at end of assembly"));
252b5132
RH
14656}
14657
14658static long
17a2f251 14659get_number (void)
252b5132
RH
14660{
14661 int negative = 0;
14662 long val = 0;
14663
14664 if (*input_line_pointer == '-')
14665 {
14666 ++input_line_pointer;
14667 negative = 1;
14668 }
3882b010 14669 if (!ISDIGIT (*input_line_pointer))
956cd1d6 14670 as_bad (_("expected simple number"));
252b5132
RH
14671 if (input_line_pointer[0] == '0')
14672 {
14673 if (input_line_pointer[1] == 'x')
14674 {
14675 input_line_pointer += 2;
3882b010 14676 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
14677 {
14678 val <<= 4;
14679 val |= hex_value (*input_line_pointer++);
14680 }
14681 return negative ? -val : val;
14682 }
14683 else
14684 {
14685 ++input_line_pointer;
3882b010 14686 while (ISDIGIT (*input_line_pointer))
252b5132
RH
14687 {
14688 val <<= 3;
14689 val |= *input_line_pointer++ - '0';
14690 }
14691 return negative ? -val : val;
14692 }
14693 }
3882b010 14694 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
14695 {
14696 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
14697 *input_line_pointer, *input_line_pointer);
956cd1d6 14698 as_warn (_("invalid number"));
252b5132
RH
14699 return -1;
14700 }
3882b010 14701 while (ISDIGIT (*input_line_pointer))
252b5132
RH
14702 {
14703 val *= 10;
14704 val += *input_line_pointer++ - '0';
14705 }
14706 return negative ? -val : val;
14707}
14708
14709/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
14710 is an initial number which is the ECOFF file index. In the non-ECOFF
14711 case .file implies DWARF-2. */
14712
14713static void
17a2f251 14714s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 14715{
ecb4347a
DJ
14716 static int first_file_directive = 0;
14717
c5dd6aab
DJ
14718 if (ECOFF_DEBUGGING)
14719 {
14720 get_number ();
14721 s_app_file (0);
14722 }
14723 else
ecb4347a
DJ
14724 {
14725 char *filename;
14726
14727 filename = dwarf2_directive_file (0);
14728
14729 /* Versions of GCC up to 3.1 start files with a ".file"
14730 directive even for stabs output. Make sure that this
14731 ".file" is handled. Note that you need a version of GCC
14732 after 3.1 in order to support DWARF-2 on MIPS. */
14733 if (filename != NULL && ! first_file_directive)
14734 {
14735 (void) new_logical_line (filename, -1);
c04f5787 14736 s_app_file_string (filename, 0);
ecb4347a
DJ
14737 }
14738 first_file_directive = 1;
14739 }
c5dd6aab
DJ
14740}
14741
14742/* The .loc directive, implying DWARF-2. */
252b5132
RH
14743
14744static void
17a2f251 14745s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 14746{
c5dd6aab
DJ
14747 if (!ECOFF_DEBUGGING)
14748 dwarf2_directive_loc (0);
252b5132
RH
14749}
14750
252b5132
RH
14751/* The .end directive. */
14752
14753static void
17a2f251 14754s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
14755{
14756 symbolS *p;
252b5132 14757
7a621144
DJ
14758 /* Following functions need their own .frame and .cprestore directives. */
14759 mips_frame_reg_valid = 0;
14760 mips_cprestore_valid = 0;
14761
252b5132
RH
14762 if (!is_end_of_line[(unsigned char) *input_line_pointer])
14763 {
14764 p = get_symbol ();
14765 demand_empty_rest_of_line ();
14766 }
14767 else
14768 p = NULL;
14769
14949570 14770 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
14771 as_warn (_(".end not in text section"));
14772
14773 if (!cur_proc_ptr)
14774 {
14775 as_warn (_(".end directive without a preceding .ent directive."));
14776 demand_empty_rest_of_line ();
14777 return;
14778 }
14779
14780 if (p != NULL)
14781 {
14782 assert (S_GET_NAME (p));
9b2f1d35 14783 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
252b5132 14784 as_warn (_(".end symbol does not match .ent symbol."));
ecb4347a
DJ
14785
14786 if (debug_type == DEBUG_STABS)
14787 stabs_generate_asm_endfunc (S_GET_NAME (p),
14788 S_GET_NAME (p));
252b5132
RH
14789 }
14790 else
14791 as_warn (_(".end directive missing or unknown symbol"));
14792
2132e3a3 14793#ifdef OBJ_ELF
9b2f1d35
EC
14794 /* Create an expression to calculate the size of the function. */
14795 if (p && cur_proc_ptr)
14796 {
14797 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
14798 expressionS *exp = xmalloc (sizeof (expressionS));
14799
14800 obj->size = exp;
14801 exp->X_op = O_subtract;
14802 exp->X_add_symbol = symbol_temp_new_now ();
14803 exp->X_op_symbol = p;
14804 exp->X_add_number = 0;
14805
14806 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
14807 }
14808
ecb4347a 14809 /* Generate a .pdr section. */
f43abd2b 14810 if (IS_ELF && !ECOFF_DEBUGGING && mips_flag_pdr)
ecb4347a
DJ
14811 {
14812 segT saved_seg = now_seg;
14813 subsegT saved_subseg = now_subseg;
14814 valueT dot;
14815 expressionS exp;
14816 char *fragp;
252b5132 14817
ecb4347a 14818 dot = frag_now_fix ();
252b5132
RH
14819
14820#ifdef md_flush_pending_output
ecb4347a 14821 md_flush_pending_output ();
252b5132
RH
14822#endif
14823
ecb4347a
DJ
14824 assert (pdr_seg);
14825 subseg_set (pdr_seg, 0);
252b5132 14826
ecb4347a
DJ
14827 /* Write the symbol. */
14828 exp.X_op = O_symbol;
14829 exp.X_add_symbol = p;
14830 exp.X_add_number = 0;
14831 emit_expr (&exp, 4);
252b5132 14832
ecb4347a 14833 fragp = frag_more (7 * 4);
252b5132 14834
17a2f251
TS
14835 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
14836 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
14837 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
14838 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
14839 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
14840 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
14841 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 14842
ecb4347a
DJ
14843 subseg_set (saved_seg, saved_subseg);
14844 }
14845#endif /* OBJ_ELF */
252b5132
RH
14846
14847 cur_proc_ptr = NULL;
14848}
14849
14850/* The .aent and .ent directives. */
14851
14852static void
17a2f251 14853s_mips_ent (int aent)
252b5132 14854{
252b5132 14855 symbolS *symbolP;
252b5132
RH
14856
14857 symbolP = get_symbol ();
14858 if (*input_line_pointer == ',')
f9419b05 14859 ++input_line_pointer;
252b5132 14860 SKIP_WHITESPACE ();
3882b010 14861 if (ISDIGIT (*input_line_pointer)
d9a62219 14862 || *input_line_pointer == '-')
874e8986 14863 get_number ();
252b5132 14864
14949570 14865 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
14866 as_warn (_(".ent or .aent not in text section."));
14867
14868 if (!aent && cur_proc_ptr)
9a41af64 14869 as_warn (_("missing .end"));
252b5132
RH
14870
14871 if (!aent)
14872 {
7a621144
DJ
14873 /* This function needs its own .frame and .cprestore directives. */
14874 mips_frame_reg_valid = 0;
14875 mips_cprestore_valid = 0;
14876
252b5132
RH
14877 cur_proc_ptr = &cur_proc;
14878 memset (cur_proc_ptr, '\0', sizeof (procS));
14879
9b2f1d35 14880 cur_proc_ptr->func_sym = symbolP;
252b5132 14881
49309057 14882 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
252b5132 14883
f9419b05 14884 ++numprocs;
ecb4347a
DJ
14885
14886 if (debug_type == DEBUG_STABS)
14887 stabs_generate_asm_func (S_GET_NAME (symbolP),
14888 S_GET_NAME (symbolP));
252b5132
RH
14889 }
14890
14891 demand_empty_rest_of_line ();
14892}
14893
14894/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 14895 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 14896 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 14897 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
14898 symbol table (in the mdebug section). */
14899
14900static void
17a2f251 14901s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 14902{
ecb4347a 14903#ifdef OBJ_ELF
f43abd2b 14904 if (IS_ELF && !ECOFF_DEBUGGING)
ecb4347a
DJ
14905 {
14906 long val;
252b5132 14907
ecb4347a
DJ
14908 if (cur_proc_ptr == (procS *) NULL)
14909 {
14910 as_warn (_(".frame outside of .ent"));
14911 demand_empty_rest_of_line ();
14912 return;
14913 }
252b5132 14914
ecb4347a
DJ
14915 cur_proc_ptr->frame_reg = tc_get_register (1);
14916
14917 SKIP_WHITESPACE ();
14918 if (*input_line_pointer++ != ','
14919 || get_absolute_expression_and_terminator (&val) != ',')
14920 {
14921 as_warn (_("Bad .frame directive"));
14922 --input_line_pointer;
14923 demand_empty_rest_of_line ();
14924 return;
14925 }
252b5132 14926
ecb4347a
DJ
14927 cur_proc_ptr->frame_offset = val;
14928 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 14929
252b5132 14930 demand_empty_rest_of_line ();
252b5132 14931 }
ecb4347a
DJ
14932 else
14933#endif /* OBJ_ELF */
14934 s_ignore (ignore);
252b5132
RH
14935}
14936
bdaaa2e1
KH
14937/* The .fmask and .mask directives. If the mdebug section is present
14938 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 14939 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 14940 information correctly. We can't use the ecoff routines because they
252b5132
RH
14941 make reference to the ecoff symbol table (in the mdebug section). */
14942
14943static void
17a2f251 14944s_mips_mask (int reg_type)
252b5132 14945{
ecb4347a 14946#ifdef OBJ_ELF
f43abd2b 14947 if (IS_ELF && !ECOFF_DEBUGGING)
252b5132 14948 {
ecb4347a 14949 long mask, off;
252b5132 14950
ecb4347a
DJ
14951 if (cur_proc_ptr == (procS *) NULL)
14952 {
14953 as_warn (_(".mask/.fmask outside of .ent"));
14954 demand_empty_rest_of_line ();
14955 return;
14956 }
252b5132 14957
ecb4347a
DJ
14958 if (get_absolute_expression_and_terminator (&mask) != ',')
14959 {
14960 as_warn (_("Bad .mask/.fmask directive"));
14961 --input_line_pointer;
14962 demand_empty_rest_of_line ();
14963 return;
14964 }
252b5132 14965
ecb4347a
DJ
14966 off = get_absolute_expression ();
14967
14968 if (reg_type == 'F')
14969 {
14970 cur_proc_ptr->fpreg_mask = mask;
14971 cur_proc_ptr->fpreg_offset = off;
14972 }
14973 else
14974 {
14975 cur_proc_ptr->reg_mask = mask;
14976 cur_proc_ptr->reg_offset = off;
14977 }
14978
14979 demand_empty_rest_of_line ();
252b5132
RH
14980 }
14981 else
ecb4347a
DJ
14982#endif /* OBJ_ELF */
14983 s_ignore (reg_type);
252b5132
RH
14984}
14985
316f5878
RS
14986/* A table describing all the processors gas knows about. Names are
14987 matched in the order listed.
e7af610e 14988
316f5878
RS
14989 To ease comparison, please keep this table in the same order as
14990 gcc's mips_cpu_info_table[]. */
e972090a
NC
14991static const struct mips_cpu_info mips_cpu_info_table[] =
14992{
316f5878 14993 /* Entries for generic ISAs */
ad3fea08
TS
14994 { "mips1", MIPS_CPU_IS_ISA, ISA_MIPS1, CPU_R3000 },
14995 { "mips2", MIPS_CPU_IS_ISA, ISA_MIPS2, CPU_R6000 },
14996 { "mips3", MIPS_CPU_IS_ISA, ISA_MIPS3, CPU_R4000 },
14997 { "mips4", MIPS_CPU_IS_ISA, ISA_MIPS4, CPU_R8000 },
14998 { "mips5", MIPS_CPU_IS_ISA, ISA_MIPS5, CPU_MIPS5 },
14999 { "mips32", MIPS_CPU_IS_ISA, ISA_MIPS32, CPU_MIPS32 },
15000 { "mips32r2", MIPS_CPU_IS_ISA, ISA_MIPS32R2, CPU_MIPS32R2 },
15001 { "mips64", MIPS_CPU_IS_ISA, ISA_MIPS64, CPU_MIPS64 },
15002 { "mips64r2", MIPS_CPU_IS_ISA, ISA_MIPS64R2, CPU_MIPS64R2 },
316f5878
RS
15003
15004 /* MIPS I */
ad3fea08
TS
15005 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
15006 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
15007 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
316f5878
RS
15008
15009 /* MIPS II */
ad3fea08 15010 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
316f5878
RS
15011
15012 /* MIPS III */
ad3fea08
TS
15013 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
15014 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
15015 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
15016 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
15017 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
15018 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
15019 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
15020 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
15021 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
15022 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
15023 { "orion", 0, ISA_MIPS3, CPU_R4600 },
15024 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
b15591bb
AN
15025 /* ST Microelectronics Loongson 2E and 2F cores */
15026 { "loongson2e", 0, ISA_MIPS3, CPU_LOONGSON_2E },
15027 { "loongson2f", 0, ISA_MIPS3, CPU_LOONGSON_2F },
316f5878
RS
15028
15029 /* MIPS IV */
ad3fea08
TS
15030 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
15031 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
15032 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
15033 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
15034 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
15035 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
15036 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
15037 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
15038 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
15039 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
15040 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
15041 { "rm7000", 0, ISA_MIPS4, CPU_RM7000 },
15042 { "rm9000", 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
15043
15044 /* MIPS 32 */
ad3fea08
TS
15045 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 },
15046 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
15047 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
15048 { "4ksc", MIPS_CPU_ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
15049
15050 /* MIPS 32 Release 2 */
15051 { "4kec", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
15052 { "4kem", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
15053 { "4kep", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
15054 { "4ksd", MIPS_CPU_ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
15055 { "m4k", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
15056 { "m4kp", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
ad3fea08 15057 { "24kc", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 15058 { "24kf2_1", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
ad3fea08 15059 { "24kf", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
15060 { "24kf1_1", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
15061 /* Deprecated forms of the above. */
15062 { "24kfx", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
ad3fea08 15063 { "24kx", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 15064 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
ad3fea08 15065 { "24kec", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 15066 { "24kef2_1", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
ad3fea08 15067 { "24kef", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
15068 { "24kef1_1", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
15069 /* Deprecated forms of the above. */
15070 { "24kefx", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
65263ce3 15071 { "24kex", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 15072 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
a360e743
TS
15073 { "34kc", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
15074 ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
15075 { "34kf2_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
15076 ISA_MIPS32R2, CPU_MIPS32R2 },
a360e743
TS
15077 { "34kf", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
15078 ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
15079 { "34kf1_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
15080 ISA_MIPS32R2, CPU_MIPS32R2 },
15081 /* Deprecated forms of the above. */
15082 { "34kfx", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
15083 ISA_MIPS32R2, CPU_MIPS32R2 },
a360e743
TS
15084 { "34kx", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
15085 ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f
TS
15086 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
15087 { "74kc", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
15088 ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
15089 { "74kf2_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
15090 ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f
TS
15091 { "74kf", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
15092 ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
15093 { "74kf1_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
15094 ISA_MIPS32R2, CPU_MIPS32R2 },
15095 { "74kf3_2", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
15096 ISA_MIPS32R2, CPU_MIPS32R2 },
15097 /* Deprecated forms of the above. */
15098 { "74kfx", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
15099 ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f
TS
15100 { "74kx", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
15101 ISA_MIPS32R2, CPU_MIPS32R2 },
32b26a03 15102
316f5878 15103 /* MIPS 64 */
ad3fea08
TS
15104 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
15105 { "5kf", 0, ISA_MIPS64, CPU_MIPS64 },
15106 { "20kc", MIPS_CPU_ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
7764b395 15107 { "25kf", MIPS_CPU_ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
ad3fea08
TS
15108
15109 /* MIPS 64 Release 2 */
e7af610e 15110
c7a23324 15111 /* Broadcom SB-1 CPU core */
65263ce3
TS
15112 { "sb1", MIPS_CPU_ASE_MIPS3D | MIPS_CPU_ASE_MDMX,
15113 ISA_MIPS64, CPU_SB1 },
1e85aad8
JW
15114 /* Broadcom SB-1A CPU core */
15115 { "sb1a", MIPS_CPU_ASE_MIPS3D | MIPS_CPU_ASE_MDMX,
15116 ISA_MIPS64, CPU_SB1 },
e7af610e 15117
967344c6
AN
15118 /* Cavium Networks Octeon CPU core */
15119 { "octeon", 0, ISA_MIPS64R2, CPU_OCTEON },
15120
316f5878
RS
15121 /* End marker */
15122 { NULL, 0, 0, 0 }
15123};
e7af610e 15124
84ea6cf2 15125
316f5878
RS
15126/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
15127 with a final "000" replaced by "k". Ignore case.
e7af610e 15128
316f5878 15129 Note: this function is shared between GCC and GAS. */
c6c98b38 15130
b34976b6 15131static bfd_boolean
17a2f251 15132mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
15133{
15134 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
15135 given++, canonical++;
15136
15137 return ((*given == 0 && *canonical == 0)
15138 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
15139}
15140
15141
15142/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
15143 CPU name. We've traditionally allowed a lot of variation here.
15144
15145 Note: this function is shared between GCC and GAS. */
15146
b34976b6 15147static bfd_boolean
17a2f251 15148mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
15149{
15150 /* First see if the name matches exactly, or with a final "000"
15151 turned into "k". */
15152 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 15153 return TRUE;
316f5878
RS
15154
15155 /* If not, try comparing based on numerical designation alone.
15156 See if GIVEN is an unadorned number, or 'r' followed by a number. */
15157 if (TOLOWER (*given) == 'r')
15158 given++;
15159 if (!ISDIGIT (*given))
b34976b6 15160 return FALSE;
316f5878
RS
15161
15162 /* Skip over some well-known prefixes in the canonical name,
15163 hoping to find a number there too. */
15164 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
15165 canonical += 2;
15166 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
15167 canonical += 2;
15168 else if (TOLOWER (canonical[0]) == 'r')
15169 canonical += 1;
15170
15171 return mips_strict_matching_cpu_name_p (canonical, given);
15172}
15173
15174
15175/* Parse an option that takes the name of a processor as its argument.
15176 OPTION is the name of the option and CPU_STRING is the argument.
15177 Return the corresponding processor enumeration if the CPU_STRING is
15178 recognized, otherwise report an error and return null.
15179
15180 A similar function exists in GCC. */
e7af610e
NC
15181
15182static const struct mips_cpu_info *
17a2f251 15183mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 15184{
316f5878 15185 const struct mips_cpu_info *p;
e7af610e 15186
316f5878
RS
15187 /* 'from-abi' selects the most compatible architecture for the given
15188 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
15189 EABIs, we have to decide whether we're using the 32-bit or 64-bit
15190 version. Look first at the -mgp options, if given, otherwise base
15191 the choice on MIPS_DEFAULT_64BIT.
e7af610e 15192
316f5878
RS
15193 Treat NO_ABI like the EABIs. One reason to do this is that the
15194 plain 'mips' and 'mips64' configs have 'from-abi' as their default
15195 architecture. This code picks MIPS I for 'mips' and MIPS III for
15196 'mips64', just as we did in the days before 'from-abi'. */
15197 if (strcasecmp (cpu_string, "from-abi") == 0)
15198 {
15199 if (ABI_NEEDS_32BIT_REGS (mips_abi))
15200 return mips_cpu_info_from_isa (ISA_MIPS1);
15201
15202 if (ABI_NEEDS_64BIT_REGS (mips_abi))
15203 return mips_cpu_info_from_isa (ISA_MIPS3);
15204
15205 if (file_mips_gp32 >= 0)
15206 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
15207
15208 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
15209 ? ISA_MIPS3
15210 : ISA_MIPS1);
15211 }
15212
15213 /* 'default' has traditionally been a no-op. Probably not very useful. */
15214 if (strcasecmp (cpu_string, "default") == 0)
15215 return 0;
15216
15217 for (p = mips_cpu_info_table; p->name != 0; p++)
15218 if (mips_matching_cpu_name_p (p->name, cpu_string))
15219 return p;
15220
15221 as_bad ("Bad value (%s) for %s", cpu_string, option);
15222 return 0;
e7af610e
NC
15223}
15224
316f5878
RS
15225/* Return the canonical processor information for ISA (a member of the
15226 ISA_MIPS* enumeration). */
15227
e7af610e 15228static const struct mips_cpu_info *
17a2f251 15229mips_cpu_info_from_isa (int isa)
e7af610e
NC
15230{
15231 int i;
15232
15233 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
ad3fea08 15234 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
316f5878 15235 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
15236 return (&mips_cpu_info_table[i]);
15237
e972090a 15238 return NULL;
e7af610e 15239}
fef14a42
TS
15240
15241static const struct mips_cpu_info *
17a2f251 15242mips_cpu_info_from_arch (int arch)
fef14a42
TS
15243{
15244 int i;
15245
15246 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
15247 if (arch == mips_cpu_info_table[i].cpu)
15248 return (&mips_cpu_info_table[i]);
15249
15250 return NULL;
15251}
316f5878
RS
15252\f
15253static void
17a2f251 15254show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
15255{
15256 if (*first_p)
15257 {
15258 fprintf (stream, "%24s", "");
15259 *col_p = 24;
15260 }
15261 else
15262 {
15263 fprintf (stream, ", ");
15264 *col_p += 2;
15265 }
e7af610e 15266
316f5878
RS
15267 if (*col_p + strlen (string) > 72)
15268 {
15269 fprintf (stream, "\n%24s", "");
15270 *col_p = 24;
15271 }
15272
15273 fprintf (stream, "%s", string);
15274 *col_p += strlen (string);
15275
15276 *first_p = 0;
15277}
15278
15279void
17a2f251 15280md_show_usage (FILE *stream)
e7af610e 15281{
316f5878
RS
15282 int column, first;
15283 size_t i;
15284
15285 fprintf (stream, _("\
15286MIPS options:\n\
316f5878
RS
15287-EB generate big endian output\n\
15288-EL generate little endian output\n\
15289-g, -g2 do not remove unneeded NOPs or swap branches\n\
15290-G NUM allow referencing objects up to NUM bytes\n\
15291 implicitly with the gp register [default 8]\n"));
15292 fprintf (stream, _("\
15293-mips1 generate MIPS ISA I instructions\n\
15294-mips2 generate MIPS ISA II instructions\n\
15295-mips3 generate MIPS ISA III instructions\n\
15296-mips4 generate MIPS ISA IV instructions\n\
15297-mips5 generate MIPS ISA V instructions\n\
15298-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 15299-mips32r2 generate MIPS32 release 2 ISA instructions\n\
316f5878 15300-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 15301-mips64r2 generate MIPS64 release 2 ISA instructions\n\
316f5878
RS
15302-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
15303
15304 first = 1;
e7af610e
NC
15305
15306 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
15307 show (stream, mips_cpu_info_table[i].name, &column, &first);
15308 show (stream, "from-abi", &column, &first);
15309 fputc ('\n', stream);
e7af610e 15310
316f5878
RS
15311 fprintf (stream, _("\
15312-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
15313-no-mCPU don't generate code specific to CPU.\n\
15314 For -mCPU and -no-mCPU, CPU must be one of:\n"));
15315
15316 first = 1;
15317
15318 show (stream, "3900", &column, &first);
15319 show (stream, "4010", &column, &first);
15320 show (stream, "4100", &column, &first);
15321 show (stream, "4650", &column, &first);
15322 fputc ('\n', stream);
15323
15324 fprintf (stream, _("\
15325-mips16 generate mips16 instructions\n\
15326-no-mips16 do not generate mips16 instructions\n"));
15327 fprintf (stream, _("\
e16bfa71
TS
15328-msmartmips generate smartmips instructions\n\
15329-mno-smartmips do not generate smartmips instructions\n"));
15330 fprintf (stream, _("\
74cd071d
CF
15331-mdsp generate DSP instructions\n\
15332-mno-dsp do not generate DSP instructions\n"));
15333 fprintf (stream, _("\
8b082fb1
TS
15334-mdspr2 generate DSP R2 instructions\n\
15335-mno-dspr2 do not generate DSP R2 instructions\n"));
15336 fprintf (stream, _("\
ef2e4d86
CF
15337-mmt generate MT instructions\n\
15338-mno-mt do not generate MT instructions\n"));
15339 fprintf (stream, _("\
d766e8ec 15340-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 15341-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
316f5878
RS
15342-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
15343-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261 15344-msym32 assume all symbols have 32-bit values\n\
316f5878
RS
15345-O0 remove unneeded NOPs, do not swap branches\n\
15346-O remove unneeded NOPs and swap branches\n\
316f5878
RS
15347--trap, --no-break trap exception on div by 0 and mult overflow\n\
15348--break, --no-trap break exception on div by 0 and mult overflow\n"));
037b32b9
AN
15349 fprintf (stream, _("\
15350-mhard-float allow floating-point instructions\n\
15351-msoft-float do not allow floating-point instructions\n\
15352-msingle-float only allow 32-bit floating-point operations\n\
15353-mdouble-float allow 32-bit and 64-bit floating-point operations\n\
15354--[no-]construct-floats [dis]allow floating point values to be constructed\n"
15355 ));
316f5878
RS
15356#ifdef OBJ_ELF
15357 fprintf (stream, _("\
15358-KPIC, -call_shared generate SVR4 position independent code\n\
0c000745 15359-mvxworks-pic generate VxWorks position independent code\n\
316f5878
RS
15360-non_shared do not generate position independent code\n\
15361-xgot assume a 32 bit GOT\n\
dcd410fe 15362-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8 15363-mshared, -mno-shared disable/enable .cpload optimization for\n\
d821e36b 15364 position dependent (non shared) code\n\
316f5878
RS
15365-mabi=ABI create ABI conformant object file for:\n"));
15366
15367 first = 1;
15368
15369 show (stream, "32", &column, &first);
15370 show (stream, "o64", &column, &first);
15371 show (stream, "n32", &column, &first);
15372 show (stream, "64", &column, &first);
15373 show (stream, "eabi", &column, &first);
15374
15375 fputc ('\n', stream);
15376
15377 fprintf (stream, _("\
15378-32 create o32 ABI object file (default)\n\
15379-n32 create n32 ABI object file\n\
15380-64 create 64 ABI object file\n"));
15381#endif
e7af610e 15382}
14e777e0
KB
15383
15384enum dwarf2_format
413a266c 15385mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
14e777e0 15386{
369943fe 15387 if (HAVE_64BIT_SYMBOLS)
1de5b6a1
AO
15388 {
15389#ifdef TE_IRIX
15390 return dwarf2_format_64bit_irix;
15391#else
15392 return dwarf2_format_64bit;
15393#endif
15394 }
14e777e0
KB
15395 else
15396 return dwarf2_format_32bit;
15397}
73369e65
EC
15398
15399int
15400mips_dwarf2_addr_size (void)
15401{
369943fe 15402 if (HAVE_64BIT_SYMBOLS)
73369e65 15403 return 8;
73369e65
EC
15404 else
15405 return 4;
15406}
5862107c
EC
15407
15408/* Standard calling conventions leave the CFA at SP on entry. */
15409void
15410mips_cfi_frame_initial_instructions (void)
15411{
15412 cfi_add_CFA_def_cfa_register (SP);
15413}
15414
707bfff6
TS
15415int
15416tc_mips_regname_to_dw2regnum (char *regname)
15417{
15418 unsigned int regnum = -1;
15419 unsigned int reg;
15420
15421 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
15422 regnum = reg;
15423
15424 return regnum;
15425}
This page took 1.789723 seconds and 4 git commands to generate.