* NEWS: Mention pointer to member improvements.
[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,
ebd1c875 3 2003, 2004, 2005, 2006 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
13 the Free Software Foundation; either version 2, or (at your option)
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
252b5132
RH
92#define AT 1
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
104/* Allow override of standard little-endian ECOFF format. */
105
106#ifndef ECOFF_LITTLE_FORMAT
107#define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
108#endif
109
110extern int target_big_endian;
111
252b5132 112/* The name of the readonly data section. */
4d0d148d 113#define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
252b5132 114 ? ".rdata" \
056350c6
NC
115 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
116 ? ".rdata" \
252b5132
RH
117 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
118 ? ".rodata" \
119 : (abort (), ""))
120
47e39b9d
RS
121/* Information about an instruction, including its format, operands
122 and fixups. */
123struct mips_cl_insn
124{
125 /* The opcode's entry in mips_opcodes or mips16_opcodes. */
126 const struct mips_opcode *insn_mo;
127
128 /* True if this is a mips16 instruction and if we want the extended
129 form of INSN_MO. */
130 bfd_boolean use_extend;
131
132 /* The 16-bit extension instruction to use when USE_EXTEND is true. */
133 unsigned short extend;
134
135 /* The 16-bit or 32-bit bitstring of the instruction itself. This is
136 a copy of INSN_MO->match with the operands filled in. */
137 unsigned long insn_opcode;
138
139 /* The frag that contains the instruction. */
140 struct frag *frag;
141
142 /* The offset into FRAG of the first instruction byte. */
143 long where;
144
145 /* The relocs associated with the instruction, if any. */
146 fixS *fixp[3];
147
a38419a5
RS
148 /* True if this entry cannot be moved from its current position. */
149 unsigned int fixed_p : 1;
47e39b9d 150
708587a4 151 /* True if this instruction occurred in a .set noreorder block. */
47e39b9d
RS
152 unsigned int noreorder_p : 1;
153
2fa15973
RS
154 /* True for mips16 instructions that jump to an absolute address. */
155 unsigned int mips16_absolute_jump_p : 1;
47e39b9d
RS
156};
157
a325df1d
TS
158/* The ABI to use. */
159enum mips_abi_level
160{
161 NO_ABI = 0,
162 O32_ABI,
163 O64_ABI,
164 N32_ABI,
165 N64_ABI,
166 EABI_ABI
167};
168
169/* MIPS ABI we are using for this output file. */
316f5878 170static enum mips_abi_level mips_abi = NO_ABI;
a325df1d 171
143d77c5
EC
172/* Whether or not we have code that can call pic code. */
173int mips_abicalls = FALSE;
174
aa6975fb
ILT
175/* Whether or not we have code which can be put into a shared
176 library. */
177static bfd_boolean mips_in_shared = TRUE;
178
252b5132
RH
179/* This is the set of options which may be modified by the .set
180 pseudo-op. We use a struct so that .set push and .set pop are more
181 reliable. */
182
e972090a
NC
183struct mips_set_options
184{
252b5132
RH
185 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
186 if it has not been initialized. Changed by `.set mipsN', and the
187 -mipsN command line option, and the default CPU. */
188 int isa;
1f25f5d3
CD
189 /* Enabled Application Specific Extensions (ASEs). These are set to -1
190 if they have not been initialized. Changed by `.set <asename>', by
191 command line options, and based on the default architecture. */
192 int ase_mips3d;
deec1734 193 int ase_mdmx;
e16bfa71 194 int ase_smartmips;
74cd071d 195 int ase_dsp;
ef2e4d86 196 int ase_mt;
252b5132
RH
197 /* Whether we are assembling for the mips16 processor. 0 if we are
198 not, 1 if we are, and -1 if the value has not been initialized.
199 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
200 -nomips16 command line options, and the default CPU. */
201 int mips16;
202 /* Non-zero if we should not reorder instructions. Changed by `.set
203 reorder' and `.set noreorder'. */
204 int noreorder;
205 /* Non-zero if we should not permit the $at ($1) register to be used
206 in instructions. Changed by `.set at' and `.set noat'. */
207 int noat;
208 /* Non-zero if we should warn when a macro instruction expands into
209 more than one machine instruction. Changed by `.set nomacro' and
210 `.set macro'. */
211 int warn_about_macros;
212 /* Non-zero if we should not move instructions. Changed by `.set
213 move', `.set volatile', `.set nomove', and `.set novolatile'. */
214 int nomove;
215 /* Non-zero if we should not optimize branches by moving the target
216 of the branch into the delay slot. Actually, we don't perform
217 this optimization anyhow. Changed by `.set bopt' and `.set
218 nobopt'. */
219 int nobopt;
220 /* Non-zero if we should not autoextend mips16 instructions.
221 Changed by `.set autoextend' and `.set noautoextend'. */
222 int noautoextend;
a325df1d
TS
223 /* Restrict general purpose registers and floating point registers
224 to 32 bit. This is initially determined when -mgp32 or -mfp32
225 is passed but can changed if the assembler code uses .set mipsN. */
226 int gp32;
227 int fp32;
fef14a42
TS
228 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
229 command line option, and the default CPU. */
230 int arch;
aed1a261
RS
231 /* True if ".set sym32" is in effect. */
232 bfd_boolean sym32;
252b5132
RH
233};
234
a325df1d 235/* True if -mgp32 was passed. */
a8e8e863 236static int file_mips_gp32 = -1;
a325df1d
TS
237
238/* True if -mfp32 was passed. */
a8e8e863 239static int file_mips_fp32 = -1;
a325df1d 240
252b5132 241/* This is the struct we use to hold the current set of options. Note
a4672219 242 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
e7af610e 243 -1 to indicate that they have not been initialized. */
252b5132 244
e972090a
NC
245static struct mips_set_options mips_opts =
246{
e16bfa71 247 ISA_UNKNOWN, -1, -1, 0, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, CPU_UNKNOWN, FALSE
e7af610e 248};
252b5132
RH
249
250/* These variables are filled in with the masks of registers used.
251 The object format code reads them and puts them in the appropriate
252 place. */
253unsigned long mips_gprmask;
254unsigned long mips_cprmask[4];
255
256/* MIPS ISA we are using for this output file. */
e7af610e 257static int file_mips_isa = ISA_UNKNOWN;
252b5132 258
a4672219
TS
259/* True if -mips16 was passed or implied by arguments passed on the
260 command line (e.g., by -march). */
261static int file_ase_mips16;
262
3994f87e
TS
263#define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32 \
264 || mips_opts.isa == ISA_MIPS32R2 \
265 || mips_opts.isa == ISA_MIPS64 \
266 || mips_opts.isa == ISA_MIPS64R2)
267
1f25f5d3
CD
268/* True if -mips3d was passed or implied by arguments passed on the
269 command line (e.g., by -march). */
270static int file_ase_mips3d;
271
deec1734
CD
272/* True if -mdmx was passed or implied by arguments passed on the
273 command line (e.g., by -march). */
274static int file_ase_mdmx;
275
e16bfa71
TS
276/* True if -msmartmips was passed or implied by arguments passed on the
277 command line (e.g., by -march). */
278static int file_ase_smartmips;
279
ad3fea08
TS
280#define ISA_SUPPORTS_SMARTMIPS (mips_opts.isa == ISA_MIPS32 \
281 || mips_opts.isa == ISA_MIPS32R2)
e16bfa71 282
74cd071d
CF
283/* True if -mdsp was passed or implied by arguments passed on the
284 command line (e.g., by -march). */
285static int file_ase_dsp;
286
ad3fea08
TS
287#define ISA_SUPPORTS_DSP_ASE (mips_opts.isa == ISA_MIPS32R2 \
288 || mips_opts.isa == ISA_MIPS64R2)
289
65263ce3
TS
290#define ISA_SUPPORTS_DSP64_ASE (mips_opts.isa == ISA_MIPS64R2)
291
ef2e4d86
CF
292/* True if -mmt was passed or implied by arguments passed on the
293 command line (e.g., by -march). */
294static int file_ase_mt;
295
ad3fea08
TS
296#define ISA_SUPPORTS_MT_ASE (mips_opts.isa == ISA_MIPS32R2 \
297 || mips_opts.isa == ISA_MIPS64R2)
298
ec68c924 299/* The argument of the -march= flag. The architecture we are assembling. */
fef14a42 300static int file_mips_arch = CPU_UNKNOWN;
316f5878 301static const char *mips_arch_string;
ec68c924
EC
302
303/* The argument of the -mtune= flag. The architecture for which we
304 are optimizing. */
305static int mips_tune = CPU_UNKNOWN;
316f5878 306static const char *mips_tune_string;
ec68c924 307
316f5878 308/* True when generating 32-bit code for a 64-bit processor. */
252b5132
RH
309static int mips_32bitmode = 0;
310
316f5878
RS
311/* True if the given ABI requires 32-bit registers. */
312#define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
313
314/* Likewise 64-bit registers. */
707bfff6
TS
315#define ABI_NEEDS_64BIT_REGS(ABI) \
316 ((ABI) == N32_ABI \
317 || (ABI) == N64_ABI \
316f5878
RS
318 || (ABI) == O64_ABI)
319
ad3fea08 320/* Return true if ISA supports 64 bit wide gp registers. */
707bfff6
TS
321#define ISA_HAS_64BIT_REGS(ISA) \
322 ((ISA) == ISA_MIPS3 \
323 || (ISA) == ISA_MIPS4 \
324 || (ISA) == ISA_MIPS5 \
325 || (ISA) == ISA_MIPS64 \
326 || (ISA) == ISA_MIPS64R2)
9ce8a5dd 327
ad3fea08
TS
328/* Return true if ISA supports 64 bit wide float registers. */
329#define ISA_HAS_64BIT_FPRS(ISA) \
330 ((ISA) == ISA_MIPS3 \
331 || (ISA) == ISA_MIPS4 \
332 || (ISA) == ISA_MIPS5 \
333 || (ISA) == ISA_MIPS32R2 \
334 || (ISA) == ISA_MIPS64 \
335 || (ISA) == ISA_MIPS64R2)
336
af7ee8bf
CD
337/* Return true if ISA supports 64-bit right rotate (dror et al.)
338 instructions. */
707bfff6
TS
339#define ISA_HAS_DROR(ISA) \
340 ((ISA) == ISA_MIPS64R2)
af7ee8bf
CD
341
342/* Return true if ISA supports 32-bit right rotate (ror et al.)
343 instructions. */
707bfff6
TS
344#define ISA_HAS_ROR(ISA) \
345 ((ISA) == ISA_MIPS32R2 \
346 || (ISA) == ISA_MIPS64R2 \
347 || mips_opts.ase_smartmips)
348
7455baf8
TS
349/* Return true if ISA supports single-precision floats in odd registers. */
350#define ISA_HAS_ODD_SINGLE_FPR(ISA) \
351 ((ISA) == ISA_MIPS32 \
352 || (ISA) == ISA_MIPS32R2 \
353 || (ISA) == ISA_MIPS64 \
354 || (ISA) == ISA_MIPS64R2)
af7ee8bf 355
ad3fea08
TS
356/* Return true if ISA supports move to/from high part of a 64-bit
357 floating-point register. */
358#define ISA_HAS_MXHC1(ISA) \
359 ((ISA) == ISA_MIPS32R2 \
360 || (ISA) == ISA_MIPS64R2)
361
e013f690 362#define HAVE_32BIT_GPRS \
ad3fea08 363 (mips_opts.gp32 || !ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257 364
e013f690 365#define HAVE_32BIT_FPRS \
ad3fea08 366 (mips_opts.fp32 || !ISA_HAS_64BIT_FPRS (mips_opts.isa))
ca4e0257 367
ad3fea08
TS
368#define HAVE_64BIT_GPRS (!HAVE_32BIT_GPRS)
369#define HAVE_64BIT_FPRS (!HAVE_32BIT_FPRS)
ca4e0257 370
316f5878 371#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
e013f690 372
316f5878 373#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
e013f690 374
3b91255e
RS
375/* True if relocations are stored in-place. */
376#define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
377
aed1a261
RS
378/* The ABI-derived address size. */
379#define HAVE_64BIT_ADDRESSES \
380 (HAVE_64BIT_GPRS && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
381#define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
e013f690 382
aed1a261
RS
383/* The size of symbolic constants (i.e., expressions of the form
384 "SYMBOL" or "SYMBOL + OFFSET"). */
385#define HAVE_32BIT_SYMBOLS \
386 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
387#define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
ca4e0257 388
b7c7d6c1
TS
389/* Addresses are loaded in different ways, depending on the address size
390 in use. The n32 ABI Documentation also mandates the use of additions
391 with overflow checking, but existing implementations don't follow it. */
f899b4b8 392#define ADDRESS_ADD_INSN \
b7c7d6c1 393 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
f899b4b8
TS
394
395#define ADDRESS_ADDI_INSN \
b7c7d6c1 396 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
f899b4b8
TS
397
398#define ADDRESS_LOAD_INSN \
399 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
400
401#define ADDRESS_STORE_INSN \
402 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
403
a4672219 404/* Return true if the given CPU supports the MIPS16 ASE. */
3396de36
TS
405#define CPU_HAS_MIPS16(cpu) \
406 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
407 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
a4672219 408
60b63b72
RS
409/* True if CPU has a dror instruction. */
410#define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
411
412/* True if CPU has a ror instruction. */
413#define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
414
c8978940
CD
415/* True if mflo and mfhi can be immediately followed by instructions
416 which write to the HI and LO registers.
417
418 According to MIPS specifications, MIPS ISAs I, II, and III need
419 (at least) two instructions between the reads of HI/LO and
420 instructions which write them, and later ISAs do not. Contradicting
421 the MIPS specifications, some MIPS IV processor user manuals (e.g.
422 the UM for the NEC Vr5000) document needing the instructions between
423 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
424 MIPS64 and later ISAs to have the interlocks, plus any specific
425 earlier-ISA CPUs for which CPU documentation declares that the
426 instructions are really interlocked. */
427#define hilo_interlocks \
428 (mips_opts.isa == ISA_MIPS32 \
429 || mips_opts.isa == ISA_MIPS32R2 \
430 || mips_opts.isa == ISA_MIPS64 \
431 || mips_opts.isa == ISA_MIPS64R2 \
432 || mips_opts.arch == CPU_R4010 \
433 || mips_opts.arch == CPU_R10000 \
434 || mips_opts.arch == CPU_R12000 \
435 || mips_opts.arch == CPU_RM7000 \
c8978940
CD
436 || mips_opts.arch == CPU_VR5500 \
437 )
252b5132
RH
438
439/* Whether the processor uses hardware interlocks to protect reads
81912461
ILT
440 from the GPRs after they are loaded from memory, and thus does not
441 require nops to be inserted. This applies to instructions marked
442 INSN_LOAD_MEMORY_DELAY. These nops are only required at MIPS ISA
443 level I. */
252b5132 444#define gpr_interlocks \
e7af610e 445 (mips_opts.isa != ISA_MIPS1 \
fef14a42 446 || mips_opts.arch == CPU_R3900)
252b5132 447
81912461
ILT
448/* Whether the processor uses hardware interlocks to avoid delays
449 required by coprocessor instructions, and thus does not require
450 nops to be inserted. This applies to instructions marked
451 INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
452 between instructions marked INSN_WRITE_COND_CODE and ones marked
453 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
454 levels I, II, and III. */
bdaaa2e1 455/* Itbl support may require additional care here. */
81912461
ILT
456#define cop_interlocks \
457 ((mips_opts.isa != ISA_MIPS1 \
458 && mips_opts.isa != ISA_MIPS2 \
459 && mips_opts.isa != ISA_MIPS3) \
460 || mips_opts.arch == CPU_R4300 \
81912461
ILT
461 )
462
463/* Whether the processor uses hardware interlocks to protect reads
464 from coprocessor registers after they are loaded from memory, and
465 thus does not require nops to be inserted. This applies to
466 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
467 requires at MIPS ISA level I. */
468#define cop_mem_interlocks (mips_opts.isa != ISA_MIPS1)
252b5132 469
6b76fefe
CM
470/* Is this a mfhi or mflo instruction? */
471#define MF_HILO_INSN(PINFO) \
472 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
473
252b5132
RH
474/* MIPS PIC level. */
475
a161fe53 476enum mips_pic_level mips_pic;
252b5132 477
c9914766 478/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 479 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 480static int mips_big_got = 0;
252b5132
RH
481
482/* 1 if trap instructions should used for overflow rather than break
483 instructions. */
c9914766 484static int mips_trap = 0;
252b5132 485
119d663a 486/* 1 if double width floating point constants should not be constructed
b6ff326e 487 by assembling two single width halves into two single width floating
119d663a
NC
488 point registers which just happen to alias the double width destination
489 register. On some architectures this aliasing can be disabled by a bit
d547a75e 490 in the status register, and the setting of this bit cannot be determined
119d663a
NC
491 automatically at assemble time. */
492static int mips_disable_float_construction;
493
252b5132
RH
494/* Non-zero if any .set noreorder directives were used. */
495
496static int mips_any_noreorder;
497
6b76fefe
CM
498/* Non-zero if nops should be inserted when the register referenced in
499 an mfhi/mflo instruction is read in the next two instructions. */
500static int mips_7000_hilo_fix;
501
252b5132 502/* The size of the small data section. */
156c2f8b 503static unsigned int g_switch_value = 8;
252b5132
RH
504/* Whether the -G option was used. */
505static int g_switch_seen = 0;
506
507#define N_RMASK 0xc4
508#define N_VFP 0xd4
509
510/* If we can determine in advance that GP optimization won't be
511 possible, we can skip the relaxation stuff that tries to produce
512 GP-relative references. This makes delay slot optimization work
513 better.
514
515 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
516 gcc output. It needs to guess right for gcc, otherwise gcc
517 will put what it thinks is a GP-relative instruction in a branch
518 delay slot.
252b5132
RH
519
520 I don't know if a fix is needed for the SVR4_PIC mode. I've only
521 fixed it for the non-PIC mode. KR 95/04/07 */
17a2f251 522static int nopic_need_relax (symbolS *, int);
252b5132
RH
523
524/* handle of the OPCODE hash table */
525static struct hash_control *op_hash = NULL;
526
527/* The opcode hash table we use for the mips16. */
528static struct hash_control *mips16_op_hash = NULL;
529
530/* This array holds the chars that always start a comment. If the
531 pre-processor is disabled, these aren't very useful */
532const char comment_chars[] = "#";
533
534/* This array holds the chars that only start a comment at the beginning of
535 a line. If the line seems to have the form '# 123 filename'
536 .line and .file directives will appear in the pre-processed output */
537/* Note that input_file.c hand checks for '#' at the beginning of the
538 first line of the input file. This is because the compiler outputs
bdaaa2e1 539 #NO_APP at the beginning of its output. */
252b5132
RH
540/* Also note that C style comments are always supported. */
541const char line_comment_chars[] = "#";
542
bdaaa2e1 543/* This array holds machine specific line separator characters. */
63a0b638 544const char line_separator_chars[] = ";";
252b5132
RH
545
546/* Chars that can be used to separate mant from exp in floating point nums */
547const char EXP_CHARS[] = "eE";
548
549/* Chars that mean this number is a floating point constant */
550/* As in 0f12.456 */
551/* or 0d1.2345e12 */
552const char FLT_CHARS[] = "rRsSfFdDxXpP";
553
554/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
555 changed in read.c . Ideally it shouldn't have to know about it at all,
556 but nothing is ideal around here.
557 */
558
559static char *insn_error;
560
561static int auto_align = 1;
562
563/* When outputting SVR4 PIC code, the assembler needs to know the
564 offset in the stack frame from which to restore the $gp register.
565 This is set by the .cprestore pseudo-op, and saved in this
566 variable. */
567static offsetT mips_cprestore_offset = -1;
568
67c1ffbe 569/* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
6478892d 570 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 571 offset and even an other register than $gp as global pointer. */
6478892d
TS
572static offsetT mips_cpreturn_offset = -1;
573static int mips_cpreturn_register = -1;
574static int mips_gp_register = GP;
def2e0dd 575static int mips_gprel_offset = 0;
6478892d 576
7a621144
DJ
577/* Whether mips_cprestore_offset has been set in the current function
578 (or whether it has already been warned about, if not). */
579static int mips_cprestore_valid = 0;
580
252b5132
RH
581/* This is the register which holds the stack frame, as set by the
582 .frame pseudo-op. This is needed to implement .cprestore. */
583static int mips_frame_reg = SP;
584
7a621144
DJ
585/* Whether mips_frame_reg has been set in the current function
586 (or whether it has already been warned about, if not). */
587static int mips_frame_reg_valid = 0;
588
252b5132
RH
589/* To output NOP instructions correctly, we need to keep information
590 about the previous two instructions. */
591
592/* Whether we are optimizing. The default value of 2 means to remove
593 unneeded NOPs and swap branch instructions when possible. A value
594 of 1 means to not swap branches. A value of 0 means to always
595 insert NOPs. */
596static int mips_optimize = 2;
597
598/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
599 equivalent to seeing no -g option at all. */
600static int mips_debug = 0;
601
7d8e00cf
RS
602/* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
603#define MAX_VR4130_NOPS 4
604
605/* The maximum number of NOPs needed to fill delay slots. */
606#define MAX_DELAY_NOPS 2
607
608/* The maximum number of NOPs needed for any purpose. */
609#define MAX_NOPS 4
71400594
RS
610
611/* A list of previous instructions, with index 0 being the most recent.
612 We need to look back MAX_NOPS instructions when filling delay slots
613 or working around processor errata. We need to look back one
614 instruction further if we're thinking about using history[0] to
615 fill a branch delay slot. */
616static struct mips_cl_insn history[1 + MAX_NOPS];
252b5132 617
1e915849
RS
618/* Nop instructions used by emit_nop. */
619static struct mips_cl_insn nop_insn, mips16_nop_insn;
620
621/* The appropriate nop for the current mode. */
622#define NOP_INSN (mips_opts.mips16 ? &mips16_nop_insn : &nop_insn)
252b5132 623
252b5132
RH
624/* If this is set, it points to a frag holding nop instructions which
625 were inserted before the start of a noreorder section. If those
626 nops turn out to be unnecessary, the size of the frag can be
627 decreased. */
628static fragS *prev_nop_frag;
629
630/* The number of nop instructions we created in prev_nop_frag. */
631static int prev_nop_frag_holds;
632
633/* The number of nop instructions that we know we need in
bdaaa2e1 634 prev_nop_frag. */
252b5132
RH
635static int prev_nop_frag_required;
636
637/* The number of instructions we've seen since prev_nop_frag. */
638static int prev_nop_frag_since;
639
640/* For ECOFF and ELF, relocations against symbols are done in two
641 parts, with a HI relocation and a LO relocation. Each relocation
642 has only 16 bits of space to store an addend. This means that in
643 order for the linker to handle carries correctly, it must be able
644 to locate both the HI and the LO relocation. This means that the
645 relocations must appear in order in the relocation table.
646
647 In order to implement this, we keep track of each unmatched HI
648 relocation. We then sort them so that they immediately precede the
bdaaa2e1 649 corresponding LO relocation. */
252b5132 650
e972090a
NC
651struct mips_hi_fixup
652{
252b5132
RH
653 /* Next HI fixup. */
654 struct mips_hi_fixup *next;
655 /* This fixup. */
656 fixS *fixp;
657 /* The section this fixup is in. */
658 segT seg;
659};
660
661/* The list of unmatched HI relocs. */
662
663static struct mips_hi_fixup *mips_hi_fixup_list;
664
64bdfcaf
RS
665/* The frag containing the last explicit relocation operator.
666 Null if explicit relocations have not been used. */
667
668static fragS *prev_reloc_op_frag;
669
252b5132
RH
670/* Map normal MIPS register numbers to mips16 register numbers. */
671
672#define X ILLEGAL_REG
e972090a
NC
673static const int mips32_to_16_reg_map[] =
674{
252b5132
RH
675 X, X, 2, 3, 4, 5, 6, 7,
676 X, X, X, X, X, X, X, X,
677 0, 1, X, X, X, X, X, X,
678 X, X, X, X, X, X, X, X
679};
680#undef X
681
682/* Map mips16 register numbers to normal MIPS register numbers. */
683
e972090a
NC
684static const unsigned int mips16_to_32_reg_map[] =
685{
252b5132
RH
686 16, 17, 2, 3, 4, 5, 6, 7
687};
60b63b72 688
71400594
RS
689/* Classifies the kind of instructions we're interested in when
690 implementing -mfix-vr4120. */
691enum fix_vr4120_class {
692 FIX_VR4120_MACC,
693 FIX_VR4120_DMACC,
694 FIX_VR4120_MULT,
695 FIX_VR4120_DMULT,
696 FIX_VR4120_DIV,
697 FIX_VR4120_MTHILO,
698 NUM_FIX_VR4120_CLASSES
699};
700
701/* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
702 there must be at least one other instruction between an instruction
703 of type X and an instruction of type Y. */
704static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
705
706/* True if -mfix-vr4120 is in force. */
d766e8ec 707static int mips_fix_vr4120;
4a6a3df4 708
7d8e00cf
RS
709/* ...likewise -mfix-vr4130. */
710static int mips_fix_vr4130;
711
4a6a3df4
AO
712/* We don't relax branches by default, since this causes us to expand
713 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
714 fail to compute the offset before expanding the macro to the most
715 efficient expansion. */
716
717static int mips_relax_branch;
252b5132 718\f
4d7206a2
RS
719/* The expansion of many macros depends on the type of symbol that
720 they refer to. For example, when generating position-dependent code,
721 a macro that refers to a symbol may have two different expansions,
722 one which uses GP-relative addresses and one which uses absolute
723 addresses. When generating SVR4-style PIC, a macro may have
724 different expansions for local and global symbols.
725
726 We handle these situations by generating both sequences and putting
727 them in variant frags. In position-dependent code, the first sequence
728 will be the GP-relative one and the second sequence will be the
729 absolute one. In SVR4 PIC, the first sequence will be for global
730 symbols and the second will be for local symbols.
731
584892a6
RS
732 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
733 SECOND are the lengths of the two sequences in bytes. These fields
734 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
735 the subtype has the following flags:
4d7206a2 736
584892a6
RS
737 RELAX_USE_SECOND
738 Set if it has been decided that we should use the second
739 sequence instead of the first.
740
741 RELAX_SECOND_LONGER
742 Set in the first variant frag if the macro's second implementation
743 is longer than its first. This refers to the macro as a whole,
744 not an individual relaxation.
745
746 RELAX_NOMACRO
747 Set in the first variant frag if the macro appeared in a .set nomacro
748 block and if one alternative requires a warning but the other does not.
749
750 RELAX_DELAY_SLOT
751 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
752 delay slot.
4d7206a2
RS
753
754 The frag's "opcode" points to the first fixup for relaxable code.
755
756 Relaxable macros are generated using a sequence such as:
757
758 relax_start (SYMBOL);
759 ... generate first expansion ...
760 relax_switch ();
761 ... generate second expansion ...
762 relax_end ();
763
764 The code and fixups for the unwanted alternative are discarded
765 by md_convert_frag. */
584892a6 766#define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
4d7206a2 767
584892a6
RS
768#define RELAX_FIRST(X) (((X) >> 8) & 0xff)
769#define RELAX_SECOND(X) ((X) & 0xff)
770#define RELAX_USE_SECOND 0x10000
771#define RELAX_SECOND_LONGER 0x20000
772#define RELAX_NOMACRO 0x40000
773#define RELAX_DELAY_SLOT 0x80000
252b5132 774
4a6a3df4
AO
775/* Branch without likely bit. If label is out of range, we turn:
776
777 beq reg1, reg2, label
778 delay slot
779
780 into
781
782 bne reg1, reg2, 0f
783 nop
784 j label
785 0: delay slot
786
787 with the following opcode replacements:
788
789 beq <-> bne
790 blez <-> bgtz
791 bltz <-> bgez
792 bc1f <-> bc1t
793
794 bltzal <-> bgezal (with jal label instead of j label)
795
796 Even though keeping the delay slot instruction in the delay slot of
797 the branch would be more efficient, it would be very tricky to do
798 correctly, because we'd have to introduce a variable frag *after*
799 the delay slot instruction, and expand that instead. Let's do it
800 the easy way for now, even if the branch-not-taken case now costs
801 one additional instruction. Out-of-range branches are not supposed
802 to be common, anyway.
803
804 Branch likely. If label is out of range, we turn:
805
806 beql reg1, reg2, label
807 delay slot (annulled if branch not taken)
808
809 into
810
811 beql reg1, reg2, 1f
812 nop
813 beql $0, $0, 2f
814 nop
815 1: j[al] label
816 delay slot (executed only if branch taken)
817 2:
818
819 It would be possible to generate a shorter sequence by losing the
820 likely bit, generating something like:
b34976b6 821
4a6a3df4
AO
822 bne reg1, reg2, 0f
823 nop
824 j[al] label
825 delay slot (executed only if branch taken)
826 0:
827
828 beql -> bne
829 bnel -> beq
830 blezl -> bgtz
831 bgtzl -> blez
832 bltzl -> bgez
833 bgezl -> bltz
834 bc1fl -> bc1t
835 bc1tl -> bc1f
836
837 bltzall -> bgezal (with jal label instead of j label)
838 bgezall -> bltzal (ditto)
839
840
841 but it's not clear that it would actually improve performance. */
af6ae2ad 842#define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
4a6a3df4
AO
843 ((relax_substateT) \
844 (0xc0000000 \
845 | ((toofar) ? 1 : 0) \
846 | ((link) ? 2 : 0) \
847 | ((likely) ? 4 : 0) \
af6ae2ad 848 | ((uncond) ? 8 : 0)))
4a6a3df4 849#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
4a6a3df4
AO
850#define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
851#define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
852#define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
ae6063d4 853#define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
4a6a3df4 854
252b5132
RH
855/* For mips16 code, we use an entirely different form of relaxation.
856 mips16 supports two versions of most instructions which take
857 immediate values: a small one which takes some small value, and a
858 larger one which takes a 16 bit value. Since branches also follow
859 this pattern, relaxing these values is required.
860
861 We can assemble both mips16 and normal MIPS code in a single
862 object. Therefore, we need to support this type of relaxation at
863 the same time that we support the relaxation described above. We
864 use the high bit of the subtype field to distinguish these cases.
865
866 The information we store for this type of relaxation is the
867 argument code found in the opcode file for this relocation, whether
868 the user explicitly requested a small or extended form, and whether
869 the relocation is in a jump or jal delay slot. That tells us the
870 size of the value, and how it should be stored. We also store
871 whether the fragment is considered to be extended or not. We also
872 store whether this is known to be a branch to a different section,
873 whether we have tried to relax this frag yet, and whether we have
874 ever extended a PC relative fragment because of a shift count. */
875#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
876 (0x80000000 \
877 | ((type) & 0xff) \
878 | ((small) ? 0x100 : 0) \
879 | ((ext) ? 0x200 : 0) \
880 | ((dslot) ? 0x400 : 0) \
881 | ((jal_dslot) ? 0x800 : 0))
4a6a3df4 882#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132
RH
883#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
884#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
885#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
886#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
887#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
888#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
889#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
890#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
891#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
892#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
893#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
885add95
CD
894
895/* Is the given value a sign-extended 32-bit value? */
896#define IS_SEXT_32BIT_NUM(x) \
897 (((x) &~ (offsetT) 0x7fffffff) == 0 \
898 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
899
900/* Is the given value a sign-extended 16-bit value? */
901#define IS_SEXT_16BIT_NUM(x) \
902 (((x) &~ (offsetT) 0x7fff) == 0 \
903 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
904
2051e8c4
MR
905/* Is the given value a zero-extended 32-bit value? Or a negated one? */
906#define IS_ZEXT_32BIT_NUM(x) \
907 (((x) &~ (offsetT) 0xffffffff) == 0 \
908 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
909
bf12938e
RS
910/* Replace bits MASK << SHIFT of STRUCT with the equivalent bits in
911 VALUE << SHIFT. VALUE is evaluated exactly once. */
912#define INSERT_BITS(STRUCT, VALUE, MASK, SHIFT) \
913 (STRUCT) = (((STRUCT) & ~((MASK) << (SHIFT))) \
914 | (((VALUE) & (MASK)) << (SHIFT)))
915
916/* Extract bits MASK << SHIFT from STRUCT and shift them right
917 SHIFT places. */
918#define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
919 (((STRUCT) >> (SHIFT)) & (MASK))
920
921/* Change INSN's opcode so that the operand given by FIELD has value VALUE.
922 INSN is a mips_cl_insn structure and VALUE is evaluated exactly once.
923
924 include/opcode/mips.h specifies operand fields using the macros
925 OP_MASK_<FIELD> and OP_SH_<FIELD>. The MIPS16 equivalents start
926 with "MIPS16OP" instead of "OP". */
927#define INSERT_OPERAND(FIELD, INSN, VALUE) \
928 INSERT_BITS ((INSN).insn_opcode, VALUE, OP_MASK_##FIELD, OP_SH_##FIELD)
929#define MIPS16_INSERT_OPERAND(FIELD, INSN, VALUE) \
930 INSERT_BITS ((INSN).insn_opcode, VALUE, \
931 MIPS16OP_MASK_##FIELD, MIPS16OP_SH_##FIELD)
932
933/* Extract the operand given by FIELD from mips_cl_insn INSN. */
934#define EXTRACT_OPERAND(FIELD, INSN) \
935 EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD)
936#define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
937 EXTRACT_BITS ((INSN).insn_opcode, \
938 MIPS16OP_MASK_##FIELD, \
939 MIPS16OP_SH_##FIELD)
4d7206a2
RS
940\f
941/* Global variables used when generating relaxable macros. See the
942 comment above RELAX_ENCODE for more details about how relaxation
943 is used. */
944static struct {
945 /* 0 if we're not emitting a relaxable macro.
946 1 if we're emitting the first of the two relaxation alternatives.
947 2 if we're emitting the second alternative. */
948 int sequence;
949
950 /* The first relaxable fixup in the current frag. (In other words,
951 the first fixup that refers to relaxable code.) */
952 fixS *first_fixup;
953
954 /* sizes[0] says how many bytes of the first alternative are stored in
955 the current frag. Likewise sizes[1] for the second alternative. */
956 unsigned int sizes[2];
957
958 /* The symbol on which the choice of sequence depends. */
959 symbolS *symbol;
960} mips_relax;
252b5132 961\f
584892a6
RS
962/* Global variables used to decide whether a macro needs a warning. */
963static struct {
964 /* True if the macro is in a branch delay slot. */
965 bfd_boolean delay_slot_p;
966
967 /* For relaxable macros, sizes[0] is the length of the first alternative
968 in bytes and sizes[1] is the length of the second alternative.
969 For non-relaxable macros, both elements give the length of the
970 macro in bytes. */
971 unsigned int sizes[2];
972
973 /* The first variant frag for this macro. */
974 fragS *first_frag;
975} mips_macro_warning;
976\f
252b5132
RH
977/* Prototypes for static functions. */
978
17a2f251 979#define internalError() \
252b5132 980 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
252b5132
RH
981
982enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
983
b34976b6 984static void append_insn
4d7206a2 985 (struct mips_cl_insn *ip, expressionS *p, bfd_reloc_code_real_type *r);
7d10b47d 986static void mips_no_prev_insn (void);
b34976b6 987static void mips16_macro_build
67c0d1eb
RS
988 (expressionS *, const char *, const char *, va_list);
989static void load_register (int, expressionS *, int);
584892a6
RS
990static void macro_start (void);
991static void macro_end (void);
17a2f251
TS
992static void macro (struct mips_cl_insn * ip);
993static void mips16_macro (struct mips_cl_insn * ip);
252b5132 994#ifdef LOSING_COMPILER
17a2f251 995static void macro2 (struct mips_cl_insn * ip);
252b5132 996#endif
17a2f251
TS
997static void mips_ip (char *str, struct mips_cl_insn * ip);
998static void mips16_ip (char *str, struct mips_cl_insn * ip);
b34976b6 999static void mips16_immed
17a2f251
TS
1000 (char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean, bfd_boolean,
1001 unsigned long *, bfd_boolean *, unsigned short *);
5e0116d5 1002static size_t my_getSmallExpression
17a2f251
TS
1003 (expressionS *, bfd_reloc_code_real_type *, char *);
1004static void my_getExpression (expressionS *, char *);
1005static void s_align (int);
1006static void s_change_sec (int);
1007static void s_change_section (int);
1008static void s_cons (int);
1009static void s_float_cons (int);
1010static void s_mips_globl (int);
1011static void s_option (int);
1012static void s_mipsset (int);
1013static void s_abicalls (int);
1014static void s_cpload (int);
1015static void s_cpsetup (int);
1016static void s_cplocal (int);
1017static void s_cprestore (int);
1018static void s_cpreturn (int);
1019static void s_gpvalue (int);
1020static void s_gpword (int);
1021static void s_gpdword (int);
1022static void s_cpadd (int);
1023static void s_insn (int);
1024static void md_obj_begin (void);
1025static void md_obj_end (void);
1026static void s_mips_ent (int);
1027static void s_mips_end (int);
1028static void s_mips_frame (int);
1029static void s_mips_mask (int reg_type);
1030static void s_mips_stab (int);
1031static void s_mips_weakext (int);
1032static void s_mips_file (int);
1033static void s_mips_loc (int);
1034static bfd_boolean pic_need_relax (symbolS *, asection *);
4a6a3df4 1035static int relaxed_branch_length (fragS *, asection *, int);
17a2f251 1036static int validate_mips_insn (const struct mips_opcode *);
e7af610e
NC
1037
1038/* Table and functions used to map between CPU/ISA names, and
1039 ISA levels, and CPU numbers. */
1040
e972090a
NC
1041struct mips_cpu_info
1042{
e7af610e 1043 const char *name; /* CPU or ISA name. */
ad3fea08 1044 int flags; /* ASEs available, or ISA flag. */
e7af610e
NC
1045 int isa; /* ISA level. */
1046 int cpu; /* CPU number (default CPU if ISA). */
1047};
1048
ad3fea08
TS
1049#define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
1050#define MIPS_CPU_ASE_SMARTMIPS 0x0002 /* CPU implements SmartMIPS ASE */
1051#define MIPS_CPU_ASE_DSP 0x0004 /* CPU implements DSP ASE */
1052#define MIPS_CPU_ASE_MT 0x0008 /* CPU implements MT ASE */
1053#define MIPS_CPU_ASE_MIPS3D 0x0010 /* CPU implements MIPS-3D ASE */
1054#define MIPS_CPU_ASE_MDMX 0x0020 /* CPU implements MDMX ASE */
1055
17a2f251
TS
1056static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1057static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1058static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132
RH
1059\f
1060/* Pseudo-op table.
1061
1062 The following pseudo-ops from the Kane and Heinrich MIPS book
1063 should be defined here, but are currently unsupported: .alias,
1064 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1065
1066 The following pseudo-ops from the Kane and Heinrich MIPS book are
1067 specific to the type of debugging information being generated, and
1068 should be defined by the object format: .aent, .begin, .bend,
1069 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1070 .vreg.
1071
1072 The following pseudo-ops from the Kane and Heinrich MIPS book are
1073 not MIPS CPU specific, but are also not specific to the object file
1074 format. This file is probably the best place to define them, but
d84bcf09 1075 they are not currently supported: .asm0, .endr, .lab, .struct. */
252b5132 1076
e972090a
NC
1077static const pseudo_typeS mips_pseudo_table[] =
1078{
beae10d5 1079 /* MIPS specific pseudo-ops. */
252b5132
RH
1080 {"option", s_option, 0},
1081 {"set", s_mipsset, 0},
1082 {"rdata", s_change_sec, 'r'},
1083 {"sdata", s_change_sec, 's'},
1084 {"livereg", s_ignore, 0},
1085 {"abicalls", s_abicalls, 0},
1086 {"cpload", s_cpload, 0},
6478892d
TS
1087 {"cpsetup", s_cpsetup, 0},
1088 {"cplocal", s_cplocal, 0},
252b5132 1089 {"cprestore", s_cprestore, 0},
6478892d
TS
1090 {"cpreturn", s_cpreturn, 0},
1091 {"gpvalue", s_gpvalue, 0},
252b5132 1092 {"gpword", s_gpword, 0},
10181a0d 1093 {"gpdword", s_gpdword, 0},
252b5132
RH
1094 {"cpadd", s_cpadd, 0},
1095 {"insn", s_insn, 0},
1096
beae10d5 1097 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132
RH
1098 chips. */
1099 {"asciiz", stringer, 1},
1100 {"bss", s_change_sec, 'b'},
1101 {"err", s_err, 0},
1102 {"half", s_cons, 1},
1103 {"dword", s_cons, 3},
1104 {"weakext", s_mips_weakext, 0},
7c752c2a
TS
1105 {"origin", s_org, 0},
1106 {"repeat", s_rept, 0},
252b5132 1107
beae10d5 1108 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1109 here for one reason or another. */
1110 {"align", s_align, 0},
1111 {"byte", s_cons, 0},
1112 {"data", s_change_sec, 'd'},
1113 {"double", s_float_cons, 'd'},
1114 {"float", s_float_cons, 'f'},
1115 {"globl", s_mips_globl, 0},
1116 {"global", s_mips_globl, 0},
1117 {"hword", s_cons, 1},
1118 {"int", s_cons, 2},
1119 {"long", s_cons, 2},
1120 {"octa", s_cons, 4},
1121 {"quad", s_cons, 3},
cca86cc8 1122 {"section", s_change_section, 0},
252b5132
RH
1123 {"short", s_cons, 1},
1124 {"single", s_float_cons, 'f'},
1125 {"stabn", s_mips_stab, 'n'},
1126 {"text", s_change_sec, 't'},
1127 {"word", s_cons, 2},
add56521 1128
add56521 1129 { "extern", ecoff_directive_extern, 0},
add56521 1130
43841e91 1131 { NULL, NULL, 0 },
252b5132
RH
1132};
1133
e972090a
NC
1134static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1135{
beae10d5
KH
1136 /* These pseudo-ops should be defined by the object file format.
1137 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1138 {"aent", s_mips_ent, 1},
1139 {"bgnb", s_ignore, 0},
1140 {"end", s_mips_end, 0},
1141 {"endb", s_ignore, 0},
1142 {"ent", s_mips_ent, 0},
c5dd6aab 1143 {"file", s_mips_file, 0},
252b5132
RH
1144 {"fmask", s_mips_mask, 'F'},
1145 {"frame", s_mips_frame, 0},
c5dd6aab 1146 {"loc", s_mips_loc, 0},
252b5132
RH
1147 {"mask", s_mips_mask, 'R'},
1148 {"verstamp", s_ignore, 0},
43841e91 1149 { NULL, NULL, 0 },
252b5132
RH
1150};
1151
17a2f251 1152extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1153
1154void
17a2f251 1155mips_pop_insert (void)
252b5132
RH
1156{
1157 pop_insert (mips_pseudo_table);
1158 if (! ECOFF_DEBUGGING)
1159 pop_insert (mips_nonecoff_pseudo_table);
1160}
1161\f
1162/* Symbols labelling the current insn. */
1163
e972090a
NC
1164struct insn_label_list
1165{
252b5132
RH
1166 struct insn_label_list *next;
1167 symbolS *label;
1168};
1169
252b5132 1170static struct insn_label_list *free_insn_labels;
a8dbcb85 1171#define label_list tc_segment_info_data
252b5132 1172
17a2f251 1173static void mips_clear_insn_labels (void);
252b5132
RH
1174
1175static inline void
17a2f251 1176mips_clear_insn_labels (void)
252b5132
RH
1177{
1178 register struct insn_label_list **pl;
a8dbcb85 1179 segment_info_type *si;
252b5132 1180
a8dbcb85
TS
1181 if (now_seg)
1182 {
1183 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1184 ;
1185
1186 si = seg_info (now_seg);
1187 *pl = si->label_list;
1188 si->label_list = NULL;
1189 }
252b5132 1190}
a8dbcb85 1191
252b5132
RH
1192\f
1193static char *expr_end;
1194
1195/* Expressions which appear in instructions. These are set by
1196 mips_ip. */
1197
1198static expressionS imm_expr;
5f74bc13 1199static expressionS imm2_expr;
252b5132
RH
1200static expressionS offset_expr;
1201
1202/* Relocs associated with imm_expr and offset_expr. */
1203
f6688943
TS
1204static bfd_reloc_code_real_type imm_reloc[3]
1205 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1206static bfd_reloc_code_real_type offset_reloc[3]
1207 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 1208
252b5132
RH
1209/* These are set by mips16_ip if an explicit extension is used. */
1210
b34976b6 1211static bfd_boolean mips16_small, mips16_ext;
252b5132 1212
7ed4a06a 1213#ifdef OBJ_ELF
ecb4347a
DJ
1214/* The pdr segment for per procedure frame/regmask info. Not used for
1215 ECOFF debugging. */
252b5132
RH
1216
1217static segT pdr_seg;
7ed4a06a 1218#endif
252b5132 1219
e013f690
TS
1220/* The default target format to use. */
1221
1222const char *
17a2f251 1223mips_target_format (void)
e013f690
TS
1224{
1225 switch (OUTPUT_FLAVOR)
1226 {
e013f690
TS
1227 case bfd_target_ecoff_flavour:
1228 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1229 case bfd_target_coff_flavour:
1230 return "pe-mips";
1231 case bfd_target_elf_flavour:
0a44bf69
RS
1232#ifdef TE_VXWORKS
1233 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1234 return (target_big_endian
1235 ? "elf32-bigmips-vxworks"
1236 : "elf32-littlemips-vxworks");
1237#endif
e013f690 1238#ifdef TE_TMIPS
cfe86eaa 1239 /* This is traditional mips. */
e013f690 1240 return (target_big_endian
cfe86eaa
TS
1241 ? (HAVE_64BIT_OBJECTS
1242 ? "elf64-tradbigmips"
1243 : (HAVE_NEWABI
1244 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1245 : (HAVE_64BIT_OBJECTS
1246 ? "elf64-tradlittlemips"
1247 : (HAVE_NEWABI
1248 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
e013f690
TS
1249#else
1250 return (target_big_endian
cfe86eaa
TS
1251 ? (HAVE_64BIT_OBJECTS
1252 ? "elf64-bigmips"
1253 : (HAVE_NEWABI
1254 ? "elf32-nbigmips" : "elf32-bigmips"))
1255 : (HAVE_64BIT_OBJECTS
1256 ? "elf64-littlemips"
1257 : (HAVE_NEWABI
1258 ? "elf32-nlittlemips" : "elf32-littlemips")));
e013f690
TS
1259#endif
1260 default:
1261 abort ();
1262 return NULL;
1263 }
1264}
1265
1e915849
RS
1266/* Return the length of instruction INSN. */
1267
1268static inline unsigned int
1269insn_length (const struct mips_cl_insn *insn)
1270{
1271 if (!mips_opts.mips16)
1272 return 4;
1273 return insn->mips16_absolute_jump_p || insn->use_extend ? 4 : 2;
1274}
1275
1276/* Initialise INSN from opcode entry MO. Leave its position unspecified. */
1277
1278static void
1279create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
1280{
1281 size_t i;
1282
1283 insn->insn_mo = mo;
1284 insn->use_extend = FALSE;
1285 insn->extend = 0;
1286 insn->insn_opcode = mo->match;
1287 insn->frag = NULL;
1288 insn->where = 0;
1289 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1290 insn->fixp[i] = NULL;
1291 insn->fixed_p = (mips_opts.noreorder > 0);
1292 insn->noreorder_p = (mips_opts.noreorder > 0);
1293 insn->mips16_absolute_jump_p = 0;
1294}
1295
1296/* Install INSN at the location specified by its "frag" and "where" fields. */
1297
1298static void
1299install_insn (const struct mips_cl_insn *insn)
1300{
1301 char *f = insn->frag->fr_literal + insn->where;
1302 if (!mips_opts.mips16)
1303 md_number_to_chars (f, insn->insn_opcode, 4);
1304 else if (insn->mips16_absolute_jump_p)
1305 {
1306 md_number_to_chars (f, insn->insn_opcode >> 16, 2);
1307 md_number_to_chars (f + 2, insn->insn_opcode & 0xffff, 2);
1308 }
1309 else
1310 {
1311 if (insn->use_extend)
1312 {
1313 md_number_to_chars (f, 0xf000 | insn->extend, 2);
1314 f += 2;
1315 }
1316 md_number_to_chars (f, insn->insn_opcode, 2);
1317 }
1318}
1319
1320/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
1321 and install the opcode in the new location. */
1322
1323static void
1324move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
1325{
1326 size_t i;
1327
1328 insn->frag = frag;
1329 insn->where = where;
1330 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1331 if (insn->fixp[i] != NULL)
1332 {
1333 insn->fixp[i]->fx_frag = frag;
1334 insn->fixp[i]->fx_where = where;
1335 }
1336 install_insn (insn);
1337}
1338
1339/* Add INSN to the end of the output. */
1340
1341static void
1342add_fixed_insn (struct mips_cl_insn *insn)
1343{
1344 char *f = frag_more (insn_length (insn));
1345 move_insn (insn, frag_now, f - frag_now->fr_literal);
1346}
1347
1348/* Start a variant frag and move INSN to the start of the variant part,
1349 marking it as fixed. The other arguments are as for frag_var. */
1350
1351static void
1352add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
1353 relax_substateT subtype, symbolS *symbol, offsetT offset)
1354{
1355 frag_grow (max_chars);
1356 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
1357 insn->fixed_p = 1;
1358 frag_var (rs_machine_dependent, max_chars, var,
1359 subtype, symbol, offset, NULL);
1360}
1361
1362/* Insert N copies of INSN into the history buffer, starting at
1363 position FIRST. Neither FIRST nor N need to be clipped. */
1364
1365static void
1366insert_into_history (unsigned int first, unsigned int n,
1367 const struct mips_cl_insn *insn)
1368{
1369 if (mips_relax.sequence != 2)
1370 {
1371 unsigned int i;
1372
1373 for (i = ARRAY_SIZE (history); i-- > first;)
1374 if (i >= first + n)
1375 history[i] = history[i - n];
1376 else
1377 history[i] = *insn;
1378 }
1379}
1380
1381/* Emit a nop instruction, recording it in the history buffer. */
1382
1383static void
1384emit_nop (void)
1385{
1386 add_fixed_insn (NOP_INSN);
1387 insert_into_history (0, 1, NOP_INSN);
1388}
1389
71400594
RS
1390/* Initialize vr4120_conflicts. There is a bit of duplication here:
1391 the idea is to make it obvious at a glance that each errata is
1392 included. */
1393
1394static void
1395init_vr4120_conflicts (void)
1396{
1397#define CONFLICT(FIRST, SECOND) \
1398 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
1399
1400 /* Errata 21 - [D]DIV[U] after [D]MACC */
1401 CONFLICT (MACC, DIV);
1402 CONFLICT (DMACC, DIV);
1403
1404 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
1405 CONFLICT (DMULT, DMULT);
1406 CONFLICT (DMULT, DMACC);
1407 CONFLICT (DMACC, DMULT);
1408 CONFLICT (DMACC, DMACC);
1409
1410 /* Errata 24 - MT{LO,HI} after [D]MACC */
1411 CONFLICT (MACC, MTHILO);
1412 CONFLICT (DMACC, MTHILO);
1413
1414 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
1415 instruction is executed immediately after a MACC or DMACC
1416 instruction, the result of [either instruction] is incorrect." */
1417 CONFLICT (MACC, MULT);
1418 CONFLICT (MACC, DMULT);
1419 CONFLICT (DMACC, MULT);
1420 CONFLICT (DMACC, DMULT);
1421
1422 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
1423 executed immediately after a DMULT, DMULTU, DIV, DIVU,
1424 DDIV or DDIVU instruction, the result of the MACC or
1425 DMACC instruction is incorrect.". */
1426 CONFLICT (DMULT, MACC);
1427 CONFLICT (DMULT, DMACC);
1428 CONFLICT (DIV, MACC);
1429 CONFLICT (DIV, DMACC);
1430
1431#undef CONFLICT
1432}
1433
707bfff6
TS
1434struct regname {
1435 const char *name;
1436 unsigned int num;
1437};
1438
1439#define RTYPE_MASK 0x1ff00
1440#define RTYPE_NUM 0x00100
1441#define RTYPE_FPU 0x00200
1442#define RTYPE_FCC 0x00400
1443#define RTYPE_VEC 0x00800
1444#define RTYPE_GP 0x01000
1445#define RTYPE_CP0 0x02000
1446#define RTYPE_PC 0x04000
1447#define RTYPE_ACC 0x08000
1448#define RTYPE_CCC 0x10000
1449#define RNUM_MASK 0x000ff
1450#define RWARN 0x80000
1451
1452#define GENERIC_REGISTER_NUMBERS \
1453 {"$0", RTYPE_NUM | 0}, \
1454 {"$1", RTYPE_NUM | 1}, \
1455 {"$2", RTYPE_NUM | 2}, \
1456 {"$3", RTYPE_NUM | 3}, \
1457 {"$4", RTYPE_NUM | 4}, \
1458 {"$5", RTYPE_NUM | 5}, \
1459 {"$6", RTYPE_NUM | 6}, \
1460 {"$7", RTYPE_NUM | 7}, \
1461 {"$8", RTYPE_NUM | 8}, \
1462 {"$9", RTYPE_NUM | 9}, \
1463 {"$10", RTYPE_NUM | 10}, \
1464 {"$11", RTYPE_NUM | 11}, \
1465 {"$12", RTYPE_NUM | 12}, \
1466 {"$13", RTYPE_NUM | 13}, \
1467 {"$14", RTYPE_NUM | 14}, \
1468 {"$15", RTYPE_NUM | 15}, \
1469 {"$16", RTYPE_NUM | 16}, \
1470 {"$17", RTYPE_NUM | 17}, \
1471 {"$18", RTYPE_NUM | 18}, \
1472 {"$19", RTYPE_NUM | 19}, \
1473 {"$20", RTYPE_NUM | 20}, \
1474 {"$21", RTYPE_NUM | 21}, \
1475 {"$22", RTYPE_NUM | 22}, \
1476 {"$23", RTYPE_NUM | 23}, \
1477 {"$24", RTYPE_NUM | 24}, \
1478 {"$25", RTYPE_NUM | 25}, \
1479 {"$26", RTYPE_NUM | 26}, \
1480 {"$27", RTYPE_NUM | 27}, \
1481 {"$28", RTYPE_NUM | 28}, \
1482 {"$29", RTYPE_NUM | 29}, \
1483 {"$30", RTYPE_NUM | 30}, \
1484 {"$31", RTYPE_NUM | 31}
1485
1486#define FPU_REGISTER_NAMES \
1487 {"$f0", RTYPE_FPU | 0}, \
1488 {"$f1", RTYPE_FPU | 1}, \
1489 {"$f2", RTYPE_FPU | 2}, \
1490 {"$f3", RTYPE_FPU | 3}, \
1491 {"$f4", RTYPE_FPU | 4}, \
1492 {"$f5", RTYPE_FPU | 5}, \
1493 {"$f6", RTYPE_FPU | 6}, \
1494 {"$f7", RTYPE_FPU | 7}, \
1495 {"$f8", RTYPE_FPU | 8}, \
1496 {"$f9", RTYPE_FPU | 9}, \
1497 {"$f10", RTYPE_FPU | 10}, \
1498 {"$f11", RTYPE_FPU | 11}, \
1499 {"$f12", RTYPE_FPU | 12}, \
1500 {"$f13", RTYPE_FPU | 13}, \
1501 {"$f14", RTYPE_FPU | 14}, \
1502 {"$f15", RTYPE_FPU | 15}, \
1503 {"$f16", RTYPE_FPU | 16}, \
1504 {"$f17", RTYPE_FPU | 17}, \
1505 {"$f18", RTYPE_FPU | 18}, \
1506 {"$f19", RTYPE_FPU | 19}, \
1507 {"$f20", RTYPE_FPU | 20}, \
1508 {"$f21", RTYPE_FPU | 21}, \
1509 {"$f22", RTYPE_FPU | 22}, \
1510 {"$f23", RTYPE_FPU | 23}, \
1511 {"$f24", RTYPE_FPU | 24}, \
1512 {"$f25", RTYPE_FPU | 25}, \
1513 {"$f26", RTYPE_FPU | 26}, \
1514 {"$f27", RTYPE_FPU | 27}, \
1515 {"$f28", RTYPE_FPU | 28}, \
1516 {"$f29", RTYPE_FPU | 29}, \
1517 {"$f30", RTYPE_FPU | 30}, \
1518 {"$f31", RTYPE_FPU | 31}
1519
1520#define FPU_CONDITION_CODE_NAMES \
1521 {"$fcc0", RTYPE_FCC | 0}, \
1522 {"$fcc1", RTYPE_FCC | 1}, \
1523 {"$fcc2", RTYPE_FCC | 2}, \
1524 {"$fcc3", RTYPE_FCC | 3}, \
1525 {"$fcc4", RTYPE_FCC | 4}, \
1526 {"$fcc5", RTYPE_FCC | 5}, \
1527 {"$fcc6", RTYPE_FCC | 6}, \
1528 {"$fcc7", RTYPE_FCC | 7}
1529
1530#define COPROC_CONDITION_CODE_NAMES \
1531 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
1532 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
1533 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
1534 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
1535 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
1536 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
1537 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
1538 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
1539
1540#define N32N64_SYMBOLIC_REGISTER_NAMES \
1541 {"$a4", RTYPE_GP | 8}, \
1542 {"$a5", RTYPE_GP | 9}, \
1543 {"$a6", RTYPE_GP | 10}, \
1544 {"$a7", RTYPE_GP | 11}, \
1545 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
1546 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
1547 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
1548 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
1549 {"$t0", RTYPE_GP | 12}, \
1550 {"$t1", RTYPE_GP | 13}, \
1551 {"$t2", RTYPE_GP | 14}, \
1552 {"$t3", RTYPE_GP | 15}
1553
1554#define O32_SYMBOLIC_REGISTER_NAMES \
1555 {"$t0", RTYPE_GP | 8}, \
1556 {"$t1", RTYPE_GP | 9}, \
1557 {"$t2", RTYPE_GP | 10}, \
1558 {"$t3", RTYPE_GP | 11}, \
1559 {"$t4", RTYPE_GP | 12}, \
1560 {"$t5", RTYPE_GP | 13}, \
1561 {"$t6", RTYPE_GP | 14}, \
1562 {"$t7", RTYPE_GP | 15}, \
1563 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
1564 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
1565 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
1566 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
1567
1568/* Remaining symbolic register names */
1569#define SYMBOLIC_REGISTER_NAMES \
1570 {"$zero", RTYPE_GP | 0}, \
1571 {"$at", RTYPE_GP | 1}, \
1572 {"$AT", RTYPE_GP | 1}, \
1573 {"$v0", RTYPE_GP | 2}, \
1574 {"$v1", RTYPE_GP | 3}, \
1575 {"$a0", RTYPE_GP | 4}, \
1576 {"$a1", RTYPE_GP | 5}, \
1577 {"$a2", RTYPE_GP | 6}, \
1578 {"$a3", RTYPE_GP | 7}, \
1579 {"$s0", RTYPE_GP | 16}, \
1580 {"$s1", RTYPE_GP | 17}, \
1581 {"$s2", RTYPE_GP | 18}, \
1582 {"$s3", RTYPE_GP | 19}, \
1583 {"$s4", RTYPE_GP | 20}, \
1584 {"$s5", RTYPE_GP | 21}, \
1585 {"$s6", RTYPE_GP | 22}, \
1586 {"$s7", RTYPE_GP | 23}, \
1587 {"$t8", RTYPE_GP | 24}, \
1588 {"$t9", RTYPE_GP | 25}, \
1589 {"$k0", RTYPE_GP | 26}, \
1590 {"$kt0", RTYPE_GP | 26}, \
1591 {"$k1", RTYPE_GP | 27}, \
1592 {"$kt1", RTYPE_GP | 27}, \
1593 {"$gp", RTYPE_GP | 28}, \
1594 {"$sp", RTYPE_GP | 29}, \
1595 {"$s8", RTYPE_GP | 30}, \
1596 {"$fp", RTYPE_GP | 30}, \
1597 {"$ra", RTYPE_GP | 31}
1598
1599#define MIPS16_SPECIAL_REGISTER_NAMES \
1600 {"$pc", RTYPE_PC | 0}
1601
1602#define MDMX_VECTOR_REGISTER_NAMES \
1603 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
1604 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
1605 {"$v2", RTYPE_VEC | 2}, \
1606 {"$v3", RTYPE_VEC | 3}, \
1607 {"$v4", RTYPE_VEC | 4}, \
1608 {"$v5", RTYPE_VEC | 5}, \
1609 {"$v6", RTYPE_VEC | 6}, \
1610 {"$v7", RTYPE_VEC | 7}, \
1611 {"$v8", RTYPE_VEC | 8}, \
1612 {"$v9", RTYPE_VEC | 9}, \
1613 {"$v10", RTYPE_VEC | 10}, \
1614 {"$v11", RTYPE_VEC | 11}, \
1615 {"$v12", RTYPE_VEC | 12}, \
1616 {"$v13", RTYPE_VEC | 13}, \
1617 {"$v14", RTYPE_VEC | 14}, \
1618 {"$v15", RTYPE_VEC | 15}, \
1619 {"$v16", RTYPE_VEC | 16}, \
1620 {"$v17", RTYPE_VEC | 17}, \
1621 {"$v18", RTYPE_VEC | 18}, \
1622 {"$v19", RTYPE_VEC | 19}, \
1623 {"$v20", RTYPE_VEC | 20}, \
1624 {"$v21", RTYPE_VEC | 21}, \
1625 {"$v22", RTYPE_VEC | 22}, \
1626 {"$v23", RTYPE_VEC | 23}, \
1627 {"$v24", RTYPE_VEC | 24}, \
1628 {"$v25", RTYPE_VEC | 25}, \
1629 {"$v26", RTYPE_VEC | 26}, \
1630 {"$v27", RTYPE_VEC | 27}, \
1631 {"$v28", RTYPE_VEC | 28}, \
1632 {"$v29", RTYPE_VEC | 29}, \
1633 {"$v30", RTYPE_VEC | 30}, \
1634 {"$v31", RTYPE_VEC | 31}
1635
1636#define MIPS_DSP_ACCUMULATOR_NAMES \
1637 {"$ac0", RTYPE_ACC | 0}, \
1638 {"$ac1", RTYPE_ACC | 1}, \
1639 {"$ac2", RTYPE_ACC | 2}, \
1640 {"$ac3", RTYPE_ACC | 3}
1641
1642static const struct regname reg_names[] = {
1643 GENERIC_REGISTER_NUMBERS,
1644 FPU_REGISTER_NAMES,
1645 FPU_CONDITION_CODE_NAMES,
1646 COPROC_CONDITION_CODE_NAMES,
1647
1648 /* The $txx registers depends on the abi,
1649 these will be added later into the symbol table from
1650 one of the tables below once mips_abi is set after
1651 parsing of arguments from the command line. */
1652 SYMBOLIC_REGISTER_NAMES,
1653
1654 MIPS16_SPECIAL_REGISTER_NAMES,
1655 MDMX_VECTOR_REGISTER_NAMES,
1656 MIPS_DSP_ACCUMULATOR_NAMES,
1657 {0, 0}
1658};
1659
1660static const struct regname reg_names_o32[] = {
1661 O32_SYMBOLIC_REGISTER_NAMES,
1662 {0, 0}
1663};
1664
1665static const struct regname reg_names_n32n64[] = {
1666 N32N64_SYMBOLIC_REGISTER_NAMES,
1667 {0, 0}
1668};
1669
1670static int
1671reg_lookup (char **s, unsigned int types, unsigned int *regnop)
1672{
1673 symbolS *symbolP;
1674 char *e;
1675 char save_c;
1676 int reg = -1;
1677
1678 /* Find end of name. */
1679 e = *s;
1680 if (is_name_beginner (*e))
1681 ++e;
1682 while (is_part_of_name (*e))
1683 ++e;
1684
1685 /* Terminate name. */
1686 save_c = *e;
1687 *e = '\0';
1688
1689 /* Look for a register symbol. */
1690 if ((symbolP = symbol_find (*s)) && S_GET_SEGMENT (symbolP) == reg_section)
1691 {
1692 int r = S_GET_VALUE (symbolP);
1693 if (r & types)
1694 reg = r & RNUM_MASK;
1695 else if ((types & RTYPE_VEC) && (r & ~1) == (RTYPE_GP | 2))
1696 /* Convert GP reg $v0/1 to MDMX reg $v0/1! */
1697 reg = (r & RNUM_MASK) - 2;
1698 }
1699 /* Else see if this is a register defined in an itbl entry. */
1700 else if ((types & RTYPE_GP) && itbl_have_entries)
1701 {
1702 char *n = *s;
1703 unsigned long r;
1704
1705 if (*n == '$')
1706 ++n;
1707 if (itbl_get_reg_val (n, &r))
1708 reg = r & RNUM_MASK;
1709 }
1710
1711 /* Advance to next token if a register was recognised. */
1712 if (reg >= 0)
1713 *s = e;
1714 else if (types & RWARN)
1715 as_warn ("Unrecognized register name `%s'", *s);
1716
1717 *e = save_c;
1718 if (regnop)
1719 *regnop = reg;
1720 return reg >= 0;
1721}
1722
1723/* This function is called once, at assembler startup time. It should set up
1724 all the tables, etc. that the MD part of the assembler will need. */
156c2f8b 1725
252b5132 1726void
17a2f251 1727md_begin (void)
252b5132 1728{
3994f87e 1729 const char *retval = NULL;
156c2f8b 1730 int i = 0;
252b5132 1731 int broken = 0;
1f25f5d3 1732
0a44bf69
RS
1733 if (mips_pic != NO_PIC)
1734 {
1735 if (g_switch_seen && g_switch_value != 0)
1736 as_bad (_("-G may not be used in position-independent code"));
1737 g_switch_value = 0;
1738 }
1739
fef14a42 1740 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
252b5132
RH
1741 as_warn (_("Could not set architecture and machine"));
1742
252b5132
RH
1743 op_hash = hash_new ();
1744
1745 for (i = 0; i < NUMOPCODES;)
1746 {
1747 const char *name = mips_opcodes[i].name;
1748
17a2f251 1749 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
1750 if (retval != NULL)
1751 {
1752 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1753 mips_opcodes[i].name, retval);
1754 /* Probably a memory allocation problem? Give up now. */
1755 as_fatal (_("Broken assembler. No assembly attempted."));
1756 }
1757 do
1758 {
1759 if (mips_opcodes[i].pinfo != INSN_MACRO)
1760 {
1761 if (!validate_mips_insn (&mips_opcodes[i]))
1762 broken = 1;
1e915849
RS
1763 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
1764 {
1765 create_insn (&nop_insn, mips_opcodes + i);
1766 nop_insn.fixed_p = 1;
1767 }
252b5132
RH
1768 }
1769 ++i;
1770 }
1771 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1772 }
1773
1774 mips16_op_hash = hash_new ();
1775
1776 i = 0;
1777 while (i < bfd_mips16_num_opcodes)
1778 {
1779 const char *name = mips16_opcodes[i].name;
1780
17a2f251 1781 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
1782 if (retval != NULL)
1783 as_fatal (_("internal: can't hash `%s': %s"),
1784 mips16_opcodes[i].name, retval);
1785 do
1786 {
1787 if (mips16_opcodes[i].pinfo != INSN_MACRO
1788 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1789 != mips16_opcodes[i].match))
1790 {
1791 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1792 mips16_opcodes[i].name, mips16_opcodes[i].args);
1793 broken = 1;
1794 }
1e915849
RS
1795 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
1796 {
1797 create_insn (&mips16_nop_insn, mips16_opcodes + i);
1798 mips16_nop_insn.fixed_p = 1;
1799 }
252b5132
RH
1800 ++i;
1801 }
1802 while (i < bfd_mips16_num_opcodes
1803 && strcmp (mips16_opcodes[i].name, name) == 0);
1804 }
1805
1806 if (broken)
1807 as_fatal (_("Broken assembler. No assembly attempted."));
1808
1809 /* We add all the general register names to the symbol table. This
1810 helps us detect invalid uses of them. */
707bfff6
TS
1811 for (i = 0; reg_names[i].name; i++)
1812 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
1813 reg_names[i].num, // & RNUM_MASK,
1814 &zero_address_frag));
1815 if (HAVE_NEWABI)
1816 for (i = 0; reg_names_n32n64[i].name; i++)
1817 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
1818 reg_names_n32n64[i].num, // & RNUM_MASK,
252b5132 1819 &zero_address_frag));
707bfff6
TS
1820 else
1821 for (i = 0; reg_names_o32[i].name; i++)
1822 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
1823 reg_names_o32[i].num, // & RNUM_MASK,
6047c971 1824 &zero_address_frag));
6047c971 1825
7d10b47d 1826 mips_no_prev_insn ();
252b5132
RH
1827
1828 mips_gprmask = 0;
1829 mips_cprmask[0] = 0;
1830 mips_cprmask[1] = 0;
1831 mips_cprmask[2] = 0;
1832 mips_cprmask[3] = 0;
1833
1834 /* set the default alignment for the text section (2**2) */
1835 record_alignment (text_section, 2);
1836
4d0d148d 1837 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132 1838
707bfff6 1839#ifdef OBJ_ELF
f43abd2b 1840 if (IS_ELF)
252b5132 1841 {
0a44bf69
RS
1842 /* On a native system other than VxWorks, sections must be aligned
1843 to 16 byte boundaries. When configured for an embedded ELF
1844 target, we don't bother. */
1845 if (strcmp (TARGET_OS, "elf") != 0
1846 && strcmp (TARGET_OS, "vxworks") != 0)
252b5132
RH
1847 {
1848 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1849 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1850 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1851 }
1852
1853 /* Create a .reginfo section for register masks and a .mdebug
1854 section for debugging information. */
1855 {
1856 segT seg;
1857 subsegT subseg;
1858 flagword flags;
1859 segT sec;
1860
1861 seg = now_seg;
1862 subseg = now_subseg;
1863
1864 /* The ABI says this section should be loaded so that the
1865 running program can access it. However, we don't load it
1866 if we are configured for an embedded target */
1867 flags = SEC_READONLY | SEC_DATA;
1868 if (strcmp (TARGET_OS, "elf") != 0)
1869 flags |= SEC_ALLOC | SEC_LOAD;
1870
316f5878 1871 if (mips_abi != N64_ABI)
252b5132
RH
1872 {
1873 sec = subseg_new (".reginfo", (subsegT) 0);
1874
195325d2
TS
1875 bfd_set_section_flags (stdoutput, sec, flags);
1876 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
bdaaa2e1 1877
252b5132 1878 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
252b5132
RH
1879 }
1880 else
1881 {
1882 /* The 64-bit ABI uses a .MIPS.options section rather than
1883 .reginfo section. */
1884 sec = subseg_new (".MIPS.options", (subsegT) 0);
195325d2
TS
1885 bfd_set_section_flags (stdoutput, sec, flags);
1886 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132 1887
252b5132
RH
1888 /* Set up the option header. */
1889 {
1890 Elf_Internal_Options opthdr;
1891 char *f;
1892
1893 opthdr.kind = ODK_REGINFO;
1894 opthdr.size = (sizeof (Elf_External_Options)
1895 + sizeof (Elf64_External_RegInfo));
1896 opthdr.section = 0;
1897 opthdr.info = 0;
1898 f = frag_more (sizeof (Elf_External_Options));
1899 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1900 (Elf_External_Options *) f);
1901
1902 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1903 }
252b5132
RH
1904 }
1905
1906 if (ECOFF_DEBUGGING)
1907 {
1908 sec = subseg_new (".mdebug", (subsegT) 0);
1909 (void) bfd_set_section_flags (stdoutput, sec,
1910 SEC_HAS_CONTENTS | SEC_READONLY);
1911 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1912 }
f43abd2b 1913 else if (mips_flag_pdr)
ecb4347a
DJ
1914 {
1915 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1916 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1917 SEC_READONLY | SEC_RELOC
1918 | SEC_DEBUGGING);
1919 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1920 }
252b5132
RH
1921
1922 subseg_set (seg, subseg);
1923 }
1924 }
707bfff6 1925#endif /* OBJ_ELF */
252b5132
RH
1926
1927 if (! ECOFF_DEBUGGING)
1928 md_obj_begin ();
71400594
RS
1929
1930 if (mips_fix_vr4120)
1931 init_vr4120_conflicts ();
252b5132
RH
1932}
1933
1934void
17a2f251 1935md_mips_end (void)
252b5132
RH
1936{
1937 if (! ECOFF_DEBUGGING)
1938 md_obj_end ();
1939}
1940
1941void
17a2f251 1942md_assemble (char *str)
252b5132
RH
1943{
1944 struct mips_cl_insn insn;
f6688943
TS
1945 bfd_reloc_code_real_type unused_reloc[3]
1946 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
1947
1948 imm_expr.X_op = O_absent;
5f74bc13 1949 imm2_expr.X_op = O_absent;
252b5132 1950 offset_expr.X_op = O_absent;
f6688943
TS
1951 imm_reloc[0] = BFD_RELOC_UNUSED;
1952 imm_reloc[1] = BFD_RELOC_UNUSED;
1953 imm_reloc[2] = BFD_RELOC_UNUSED;
1954 offset_reloc[0] = BFD_RELOC_UNUSED;
1955 offset_reloc[1] = BFD_RELOC_UNUSED;
1956 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
1957
1958 if (mips_opts.mips16)
1959 mips16_ip (str, &insn);
1960 else
1961 {
1962 mips_ip (str, &insn);
beae10d5
KH
1963 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1964 str, insn.insn_opcode));
252b5132
RH
1965 }
1966
1967 if (insn_error)
1968 {
1969 as_bad ("%s `%s'", insn_error, str);
1970 return;
1971 }
1972
1973 if (insn.insn_mo->pinfo == INSN_MACRO)
1974 {
584892a6 1975 macro_start ();
252b5132
RH
1976 if (mips_opts.mips16)
1977 mips16_macro (&insn);
1978 else
1979 macro (&insn);
584892a6 1980 macro_end ();
252b5132
RH
1981 }
1982 else
1983 {
1984 if (imm_expr.X_op != O_absent)
4d7206a2 1985 append_insn (&insn, &imm_expr, imm_reloc);
252b5132 1986 else if (offset_expr.X_op != O_absent)
4d7206a2 1987 append_insn (&insn, &offset_expr, offset_reloc);
252b5132 1988 else
4d7206a2 1989 append_insn (&insn, NULL, unused_reloc);
252b5132
RH
1990 }
1991}
1992
5919d012 1993/* Return true if the given relocation might need a matching %lo().
0a44bf69
RS
1994 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
1995 need a matching %lo() when applied to local symbols. */
5919d012
RS
1996
1997static inline bfd_boolean
17a2f251 1998reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 1999{
3b91255e
RS
2000 return (HAVE_IN_PLACE_ADDENDS
2001 && (reloc == BFD_RELOC_HI16_S
0a44bf69
RS
2002 || reloc == BFD_RELOC_MIPS16_HI16_S
2003 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
2004 all GOT16 relocations evaluate to "G". */
2005 || (reloc == BFD_RELOC_MIPS_GOT16 && mips_pic != VXWORKS_PIC)));
5919d012
RS
2006}
2007
2008/* Return true if the given fixup is followed by a matching R_MIPS_LO16
2009 relocation. */
2010
2011static inline bfd_boolean
17a2f251 2012fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
2013{
2014 return (fixp->fx_next != NULL
d6f16593
MR
2015 && (fixp->fx_next->fx_r_type == BFD_RELOC_LO16
2016 || fixp->fx_next->fx_r_type == BFD_RELOC_MIPS16_LO16)
5919d012
RS
2017 && fixp->fx_addsy == fixp->fx_next->fx_addsy
2018 && fixp->fx_offset == fixp->fx_next->fx_offset);
2019}
2020
252b5132
RH
2021/* See whether instruction IP reads register REG. CLASS is the type
2022 of register. */
2023
2024static int
71400594 2025insn_uses_reg (const struct mips_cl_insn *ip, unsigned int reg,
17a2f251 2026 enum mips_regclass class)
252b5132
RH
2027{
2028 if (class == MIPS16_REG)
2029 {
2030 assert (mips_opts.mips16);
2031 reg = mips16_to_32_reg_map[reg];
2032 class = MIPS_GR_REG;
2033 }
2034
85b51719
TS
2035 /* Don't report on general register ZERO, since it never changes. */
2036 if (class == MIPS_GR_REG && reg == ZERO)
252b5132
RH
2037 return 0;
2038
2039 if (class == MIPS_FP_REG)
2040 {
2041 assert (! mips_opts.mips16);
2042 /* If we are called with either $f0 or $f1, we must check $f0.
2043 This is not optimal, because it will introduce an unnecessary
2044 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
2045 need to distinguish reading both $f0 and $f1 or just one of
2046 them. Note that we don't have to check the other way,
2047 because there is no instruction that sets both $f0 and $f1
2048 and requires a delay. */
2049 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
bf12938e 2050 && ((EXTRACT_OPERAND (FS, *ip) & ~(unsigned) 1)
252b5132
RH
2051 == (reg &~ (unsigned) 1)))
2052 return 1;
2053 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
bf12938e 2054 && ((EXTRACT_OPERAND (FT, *ip) & ~(unsigned) 1)
252b5132
RH
2055 == (reg &~ (unsigned) 1)))
2056 return 1;
2057 }
2058 else if (! mips_opts.mips16)
2059 {
2060 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
bf12938e 2061 && EXTRACT_OPERAND (RS, *ip) == reg)
252b5132
RH
2062 return 1;
2063 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
bf12938e 2064 && EXTRACT_OPERAND (RT, *ip) == reg)
252b5132
RH
2065 return 1;
2066 }
2067 else
2068 {
2069 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
bf12938e 2070 && mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)] == reg)
252b5132
RH
2071 return 1;
2072 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
bf12938e 2073 && mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)] == reg)
252b5132
RH
2074 return 1;
2075 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
bf12938e 2076 && (mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip)]
252b5132
RH
2077 == reg))
2078 return 1;
2079 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
2080 return 1;
2081 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
2082 return 1;
2083 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
2084 return 1;
2085 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
bf12938e 2086 && MIPS16_EXTRACT_OPERAND (REGR32, *ip) == reg)
252b5132
RH
2087 return 1;
2088 }
2089
2090 return 0;
2091}
2092
2093/* This function returns true if modifying a register requires a
2094 delay. */
2095
2096static int
17a2f251 2097reg_needs_delay (unsigned int reg)
252b5132
RH
2098{
2099 unsigned long prev_pinfo;
2100
47e39b9d 2101 prev_pinfo = history[0].insn_mo->pinfo;
252b5132 2102 if (! mips_opts.noreorder
81912461
ILT
2103 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
2104 && ! gpr_interlocks)
2105 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
2106 && ! cop_interlocks)))
252b5132 2107 {
81912461
ILT
2108 /* A load from a coprocessor or from memory. All load delays
2109 delay the use of general register rt for one instruction. */
bdaaa2e1 2110 /* Itbl support may require additional care here. */
252b5132 2111 know (prev_pinfo & INSN_WRITE_GPR_T);
bf12938e 2112 if (reg == EXTRACT_OPERAND (RT, history[0]))
252b5132
RH
2113 return 1;
2114 }
2115
2116 return 0;
2117}
2118
404a8071
RS
2119/* Move all labels in insn_labels to the current insertion point. */
2120
2121static void
2122mips_move_labels (void)
2123{
a8dbcb85 2124 segment_info_type *si = seg_info (now_seg);
404a8071
RS
2125 struct insn_label_list *l;
2126 valueT val;
2127
a8dbcb85 2128 for (l = si->label_list; l != NULL; l = l->next)
404a8071
RS
2129 {
2130 assert (S_GET_SEGMENT (l->label) == now_seg);
2131 symbol_set_frag (l->label, frag_now);
2132 val = (valueT) frag_now_fix ();
2133 /* mips16 text labels are stored as odd. */
2134 if (mips_opts.mips16)
2135 ++val;
2136 S_SET_VALUE (l->label, val);
2137 }
2138}
2139
5f0fe04b
TS
2140static bfd_boolean
2141s_is_linkonce (symbolS *sym, segT from_seg)
2142{
2143 bfd_boolean linkonce = FALSE;
2144 segT symseg = S_GET_SEGMENT (sym);
2145
2146 if (symseg != from_seg && !S_IS_LOCAL (sym))
2147 {
2148 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
2149 linkonce = TRUE;
2150#ifdef OBJ_ELF
2151 /* The GNU toolchain uses an extension for ELF: a section
2152 beginning with the magic string .gnu.linkonce is a
2153 linkonce section. */
2154 if (strncmp (segment_name (symseg), ".gnu.linkonce",
2155 sizeof ".gnu.linkonce" - 1) == 0)
2156 linkonce = TRUE;
2157#endif
2158 }
2159 return linkonce;
2160}
2161
252b5132
RH
2162/* Mark instruction labels in mips16 mode. This permits the linker to
2163 handle them specially, such as generating jalx instructions when
2164 needed. We also make them odd for the duration of the assembly, in
2165 order to generate the right sort of code. We will make them even
2166 in the adjust_symtab routine, while leaving them marked. This is
2167 convenient for the debugger and the disassembler. The linker knows
2168 to make them odd again. */
2169
2170static void
17a2f251 2171mips16_mark_labels (void)
252b5132 2172{
a8dbcb85
TS
2173 segment_info_type *si = seg_info (now_seg);
2174 struct insn_label_list *l;
252b5132 2175
a8dbcb85
TS
2176 if (!mips_opts.mips16)
2177 return;
2178
2179 for (l = si->label_list; l != NULL; l = l->next)
2180 {
2181 symbolS *label = l->label;
2182
2183#if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
f43abd2b 2184 if (IS_ELF)
a8dbcb85 2185 S_SET_OTHER (label, STO_MIPS16);
252b5132 2186#endif
5f0fe04b
TS
2187 if ((S_GET_VALUE (label) & 1) == 0
2188 /* Don't adjust the address if the label is global or weak, or
2189 in a link-once section, since we'll be emitting symbol reloc
2190 references to it which will be patched up by the linker, and
2191 the final value of the symbol may or may not be MIPS16. */
2192 && ! S_IS_WEAK (label)
2193 && ! S_IS_EXTERNAL (label)
2194 && ! s_is_linkonce (label, now_seg))
a8dbcb85 2195 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
252b5132
RH
2196 }
2197}
2198
4d7206a2
RS
2199/* End the current frag. Make it a variant frag and record the
2200 relaxation info. */
2201
2202static void
2203relax_close_frag (void)
2204{
584892a6 2205 mips_macro_warning.first_frag = frag_now;
4d7206a2 2206 frag_var (rs_machine_dependent, 0, 0,
584892a6 2207 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
4d7206a2
RS
2208 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
2209
2210 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
2211 mips_relax.first_fixup = 0;
2212}
2213
2214/* Start a new relaxation sequence whose expansion depends on SYMBOL.
2215 See the comment above RELAX_ENCODE for more details. */
2216
2217static void
2218relax_start (symbolS *symbol)
2219{
2220 assert (mips_relax.sequence == 0);
2221 mips_relax.sequence = 1;
2222 mips_relax.symbol = symbol;
2223}
2224
2225/* Start generating the second version of a relaxable sequence.
2226 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
2227
2228static void
4d7206a2
RS
2229relax_switch (void)
2230{
2231 assert (mips_relax.sequence == 1);
2232 mips_relax.sequence = 2;
2233}
2234
2235/* End the current relaxable sequence. */
2236
2237static void
2238relax_end (void)
2239{
2240 assert (mips_relax.sequence == 2);
2241 relax_close_frag ();
2242 mips_relax.sequence = 0;
2243}
2244
71400594
RS
2245/* Classify an instruction according to the FIX_VR4120_* enumeration.
2246 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
2247 by VR4120 errata. */
4d7206a2 2248
71400594
RS
2249static unsigned int
2250classify_vr4120_insn (const char *name)
252b5132 2251{
71400594
RS
2252 if (strncmp (name, "macc", 4) == 0)
2253 return FIX_VR4120_MACC;
2254 if (strncmp (name, "dmacc", 5) == 0)
2255 return FIX_VR4120_DMACC;
2256 if (strncmp (name, "mult", 4) == 0)
2257 return FIX_VR4120_MULT;
2258 if (strncmp (name, "dmult", 5) == 0)
2259 return FIX_VR4120_DMULT;
2260 if (strstr (name, "div"))
2261 return FIX_VR4120_DIV;
2262 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
2263 return FIX_VR4120_MTHILO;
2264 return NUM_FIX_VR4120_CLASSES;
2265}
252b5132 2266
71400594
RS
2267/* Return the number of instructions that must separate INSN1 and INSN2,
2268 where INSN1 is the earlier instruction. Return the worst-case value
2269 for any INSN2 if INSN2 is null. */
252b5132 2270
71400594
RS
2271static unsigned int
2272insns_between (const struct mips_cl_insn *insn1,
2273 const struct mips_cl_insn *insn2)
2274{
2275 unsigned long pinfo1, pinfo2;
2276
2277 /* This function needs to know which pinfo flags are set for INSN2
2278 and which registers INSN2 uses. The former is stored in PINFO2 and
2279 the latter is tested via INSN2_USES_REG. If INSN2 is null, PINFO2
2280 will have every flag set and INSN2_USES_REG will always return true. */
2281 pinfo1 = insn1->insn_mo->pinfo;
2282 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
252b5132 2283
71400594
RS
2284#define INSN2_USES_REG(REG, CLASS) \
2285 (insn2 == NULL || insn_uses_reg (insn2, REG, CLASS))
2286
2287 /* For most targets, write-after-read dependencies on the HI and LO
2288 registers must be separated by at least two instructions. */
2289 if (!hilo_interlocks)
252b5132 2290 {
71400594
RS
2291 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
2292 return 2;
2293 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
2294 return 2;
2295 }
2296
2297 /* If we're working around r7000 errata, there must be two instructions
2298 between an mfhi or mflo and any instruction that uses the result. */
2299 if (mips_7000_hilo_fix
2300 && MF_HILO_INSN (pinfo1)
2301 && INSN2_USES_REG (EXTRACT_OPERAND (RD, *insn1), MIPS_GR_REG))
2302 return 2;
2303
2304 /* If working around VR4120 errata, check for combinations that need
2305 a single intervening instruction. */
2306 if (mips_fix_vr4120)
2307 {
2308 unsigned int class1, class2;
252b5132 2309
71400594
RS
2310 class1 = classify_vr4120_insn (insn1->insn_mo->name);
2311 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
252b5132 2312 {
71400594
RS
2313 if (insn2 == NULL)
2314 return 1;
2315 class2 = classify_vr4120_insn (insn2->insn_mo->name);
2316 if (vr4120_conflicts[class1] & (1 << class2))
2317 return 1;
252b5132 2318 }
71400594
RS
2319 }
2320
2321 if (!mips_opts.mips16)
2322 {
2323 /* Check for GPR or coprocessor load delays. All such delays
2324 are on the RT register. */
2325 /* Itbl support may require additional care here. */
2326 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
2327 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
252b5132 2328 {
71400594
RS
2329 know (pinfo1 & INSN_WRITE_GPR_T);
2330 if (INSN2_USES_REG (EXTRACT_OPERAND (RT, *insn1), MIPS_GR_REG))
2331 return 1;
2332 }
2333
2334 /* Check for generic coprocessor hazards.
2335
2336 This case is not handled very well. There is no special
2337 knowledge of CP0 handling, and the coprocessors other than
2338 the floating point unit are not distinguished at all. */
2339 /* Itbl support may require additional care here. FIXME!
2340 Need to modify this to include knowledge about
2341 user specified delays! */
2342 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
2343 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
2344 {
2345 /* Handle cases where INSN1 writes to a known general coprocessor
2346 register. There must be a one instruction delay before INSN2
2347 if INSN2 reads that register, otherwise no delay is needed. */
2348 if (pinfo1 & INSN_WRITE_FPR_T)
252b5132 2349 {
71400594
RS
2350 if (INSN2_USES_REG (EXTRACT_OPERAND (FT, *insn1), MIPS_FP_REG))
2351 return 1;
252b5132 2352 }
71400594 2353 else if (pinfo1 & INSN_WRITE_FPR_S)
252b5132 2354 {
71400594
RS
2355 if (INSN2_USES_REG (EXTRACT_OPERAND (FS, *insn1), MIPS_FP_REG))
2356 return 1;
252b5132
RH
2357 }
2358 else
2359 {
71400594
RS
2360 /* Read-after-write dependencies on the control registers
2361 require a two-instruction gap. */
2362 if ((pinfo1 & INSN_WRITE_COND_CODE)
2363 && (pinfo2 & INSN_READ_COND_CODE))
2364 return 2;
2365
2366 /* We don't know exactly what INSN1 does. If INSN2 is
2367 also a coprocessor instruction, assume there must be
2368 a one instruction gap. */
2369 if (pinfo2 & INSN_COP)
2370 return 1;
252b5132
RH
2371 }
2372 }
6b76fefe 2373
71400594
RS
2374 /* Check for read-after-write dependencies on the coprocessor
2375 control registers in cases where INSN1 does not need a general
2376 coprocessor delay. This means that INSN1 is a floating point
2377 comparison instruction. */
2378 /* Itbl support may require additional care here. */
2379 else if (!cop_interlocks
2380 && (pinfo1 & INSN_WRITE_COND_CODE)
2381 && (pinfo2 & INSN_READ_COND_CODE))
2382 return 1;
2383 }
6b76fefe 2384
71400594 2385#undef INSN2_USES_REG
6b76fefe 2386
71400594
RS
2387 return 0;
2388}
6b76fefe 2389
7d8e00cf
RS
2390/* Return the number of nops that would be needed to work around the
2391 VR4130 mflo/mfhi errata if instruction INSN immediately followed
2392 the MAX_VR4130_NOPS instructions described by HISTORY. */
2393
2394static int
2395nops_for_vr4130 (const struct mips_cl_insn *history,
2396 const struct mips_cl_insn *insn)
2397{
2398 int i, j, reg;
2399
2400 /* Check if the instruction writes to HI or LO. MTHI and MTLO
2401 are not affected by the errata. */
2402 if (insn != 0
2403 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
2404 || strcmp (insn->insn_mo->name, "mtlo") == 0
2405 || strcmp (insn->insn_mo->name, "mthi") == 0))
2406 return 0;
2407
2408 /* Search for the first MFLO or MFHI. */
2409 for (i = 0; i < MAX_VR4130_NOPS; i++)
2410 if (!history[i].noreorder_p && MF_HILO_INSN (history[i].insn_mo->pinfo))
2411 {
2412 /* Extract the destination register. */
2413 if (mips_opts.mips16)
2414 reg = mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, history[i])];
2415 else
2416 reg = EXTRACT_OPERAND (RD, history[i]);
2417
2418 /* No nops are needed if INSN reads that register. */
2419 if (insn != NULL && insn_uses_reg (insn, reg, MIPS_GR_REG))
2420 return 0;
2421
2422 /* ...or if any of the intervening instructions do. */
2423 for (j = 0; j < i; j++)
2424 if (insn_uses_reg (&history[j], reg, MIPS_GR_REG))
2425 return 0;
2426
2427 return MAX_VR4130_NOPS - i;
2428 }
2429 return 0;
2430}
2431
71400594
RS
2432/* Return the number of nops that would be needed if instruction INSN
2433 immediately followed the MAX_NOPS instructions given by HISTORY,
2434 where HISTORY[0] is the most recent instruction. If INSN is null,
2435 return the worse-case number of nops for any instruction. */
bdaaa2e1 2436
71400594
RS
2437static int
2438nops_for_insn (const struct mips_cl_insn *history,
2439 const struct mips_cl_insn *insn)
2440{
2441 int i, nops, tmp_nops;
bdaaa2e1 2442
71400594 2443 nops = 0;
7d8e00cf 2444 for (i = 0; i < MAX_DELAY_NOPS; i++)
71400594
RS
2445 if (!history[i].noreorder_p)
2446 {
2447 tmp_nops = insns_between (history + i, insn) - i;
2448 if (tmp_nops > nops)
2449 nops = tmp_nops;
2450 }
7d8e00cf
RS
2451
2452 if (mips_fix_vr4130)
2453 {
2454 tmp_nops = nops_for_vr4130 (history, insn);
2455 if (tmp_nops > nops)
2456 nops = tmp_nops;
2457 }
2458
71400594
RS
2459 return nops;
2460}
252b5132 2461
71400594
RS
2462/* The variable arguments provide NUM_INSNS extra instructions that
2463 might be added to HISTORY. Return the largest number of nops that
2464 would be needed after the extended sequence. */
252b5132 2465
71400594
RS
2466static int
2467nops_for_sequence (int num_insns, const struct mips_cl_insn *history, ...)
2468{
2469 va_list args;
2470 struct mips_cl_insn buffer[MAX_NOPS];
2471 struct mips_cl_insn *cursor;
2472 int nops;
2473
2474 va_start (args, history);
2475 cursor = buffer + num_insns;
2476 memcpy (cursor, history, (MAX_NOPS - num_insns) * sizeof (*cursor));
2477 while (cursor > buffer)
2478 *--cursor = *va_arg (args, const struct mips_cl_insn *);
2479
2480 nops = nops_for_insn (buffer, NULL);
2481 va_end (args);
2482 return nops;
2483}
252b5132 2484
71400594
RS
2485/* Like nops_for_insn, but if INSN is a branch, take into account the
2486 worst-case delay for the branch target. */
252b5132 2487
71400594
RS
2488static int
2489nops_for_insn_or_target (const struct mips_cl_insn *history,
2490 const struct mips_cl_insn *insn)
2491{
2492 int nops, tmp_nops;
60b63b72 2493
71400594
RS
2494 nops = nops_for_insn (history, insn);
2495 if (insn->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
2496 | INSN_COND_BRANCH_DELAY
2497 | INSN_COND_BRANCH_LIKELY))
2498 {
2499 tmp_nops = nops_for_sequence (2, history, insn, NOP_INSN);
2500 if (tmp_nops > nops)
2501 nops = tmp_nops;
2502 }
2503 else if (mips_opts.mips16 && (insn->insn_mo->pinfo & MIPS16_INSN_BRANCH))
2504 {
2505 tmp_nops = nops_for_sequence (1, history, insn);
2506 if (tmp_nops > nops)
2507 nops = tmp_nops;
2508 }
2509 return nops;
2510}
2511
2512/* Output an instruction. IP is the instruction information.
2513 ADDRESS_EXPR is an operand of the instruction to be used with
2514 RELOC_TYPE. */
2515
2516static void
2517append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
2518 bfd_reloc_code_real_type *reloc_type)
2519{
3994f87e 2520 unsigned long prev_pinfo, pinfo;
71400594
RS
2521 relax_stateT prev_insn_frag_type = 0;
2522 bfd_boolean relaxed_branch = FALSE;
a8dbcb85 2523 segment_info_type *si = seg_info (now_seg);
71400594
RS
2524
2525 /* Mark instruction labels in mips16 mode. */
2526 mips16_mark_labels ();
2527
2528 prev_pinfo = history[0].insn_mo->pinfo;
2529 pinfo = ip->insn_mo->pinfo;
2530
2531 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
2532 {
2533 /* There are a lot of optimizations we could do that we don't.
2534 In particular, we do not, in general, reorder instructions.
2535 If you use gcc with optimization, it will reorder
2536 instructions and generally do much more optimization then we
2537 do here; repeating all that work in the assembler would only
2538 benefit hand written assembly code, and does not seem worth
2539 it. */
2540 int nops = (mips_optimize == 0
2541 ? nops_for_insn (history, NULL)
2542 : nops_for_insn_or_target (history, ip));
2543 if (nops > 0)
252b5132
RH
2544 {
2545 fragS *old_frag;
2546 unsigned long old_frag_offset;
2547 int i;
252b5132
RH
2548
2549 old_frag = frag_now;
2550 old_frag_offset = frag_now_fix ();
2551
2552 for (i = 0; i < nops; i++)
2553 emit_nop ();
2554
2555 if (listing)
2556 {
2557 listing_prev_line ();
2558 /* We may be at the start of a variant frag. In case we
2559 are, make sure there is enough space for the frag
2560 after the frags created by listing_prev_line. The
2561 argument to frag_grow here must be at least as large
2562 as the argument to all other calls to frag_grow in
2563 this file. We don't have to worry about being in the
2564 middle of a variant frag, because the variants insert
2565 all needed nop instructions themselves. */
2566 frag_grow (40);
2567 }
2568
404a8071 2569 mips_move_labels ();
252b5132
RH
2570
2571#ifndef NO_ECOFF_DEBUGGING
2572 if (ECOFF_DEBUGGING)
2573 ecoff_fix_loc (old_frag, old_frag_offset);
2574#endif
2575 }
71400594
RS
2576 }
2577 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
2578 {
2579 /* Work out how many nops in prev_nop_frag are needed by IP. */
2580 int nops = nops_for_insn_or_target (history, ip);
2581 assert (nops <= prev_nop_frag_holds);
252b5132 2582
71400594
RS
2583 /* Enforce NOPS as a minimum. */
2584 if (nops > prev_nop_frag_required)
2585 prev_nop_frag_required = nops;
252b5132 2586
71400594
RS
2587 if (prev_nop_frag_holds == prev_nop_frag_required)
2588 {
2589 /* Settle for the current number of nops. Update the history
2590 accordingly (for the benefit of any future .set reorder code). */
2591 prev_nop_frag = NULL;
2592 insert_into_history (prev_nop_frag_since,
2593 prev_nop_frag_holds, NOP_INSN);
2594 }
2595 else
2596 {
2597 /* Allow this instruction to replace one of the nops that was
2598 tentatively added to prev_nop_frag. */
2599 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
2600 prev_nop_frag_holds--;
2601 prev_nop_frag_since++;
252b5132
RH
2602 }
2603 }
2604
58e2ea4d
MR
2605#ifdef OBJ_ELF
2606 /* The value passed to dwarf2_emit_insn is the distance between
2607 the beginning of the current instruction and the address that
2608 should be recorded in the debug tables. For MIPS16 debug info
2609 we want to use ISA-encoded addresses, so we pass -1 for an
2610 address higher by one than the current. */
2611 dwarf2_emit_insn (mips_opts.mips16 ? -1 : 0);
2612#endif
2613
895921c9 2614 /* Record the frag type before frag_var. */
47e39b9d
RS
2615 if (history[0].frag)
2616 prev_insn_frag_type = history[0].frag->fr_type;
895921c9 2617
4d7206a2 2618 if (address_expr
0b25d3e6 2619 && *reloc_type == BFD_RELOC_16_PCREL_S2
4a6a3df4
AO
2620 && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
2621 || pinfo & INSN_COND_BRANCH_LIKELY)
2622 && mips_relax_branch
2623 /* Don't try branch relaxation within .set nomacro, or within
2624 .set noat if we use $at for PIC computations. If it turns
2625 out that the branch was out-of-range, we'll get an error. */
2626 && !mips_opts.warn_about_macros
2627 && !(mips_opts.noat && mips_pic != NO_PIC)
2628 && !mips_opts.mips16)
2629 {
895921c9 2630 relaxed_branch = TRUE;
1e915849
RS
2631 add_relaxed_insn (ip, (relaxed_branch_length
2632 (NULL, NULL,
2633 (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2634 : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1
2635 : 0)), 4,
2636 RELAX_BRANCH_ENCODE
2637 (pinfo & INSN_UNCOND_BRANCH_DELAY,
2638 pinfo & INSN_COND_BRANCH_LIKELY,
2639 pinfo & INSN_WRITE_GPR_31,
2640 0),
2641 address_expr->X_add_symbol,
2642 address_expr->X_add_number);
4a6a3df4
AO
2643 *reloc_type = BFD_RELOC_UNUSED;
2644 }
2645 else if (*reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
2646 {
2647 /* We need to set up a variant frag. */
2648 assert (mips_opts.mips16 && address_expr != NULL);
1e915849
RS
2649 add_relaxed_insn (ip, 4, 0,
2650 RELAX_MIPS16_ENCODE
2651 (*reloc_type - BFD_RELOC_UNUSED,
2652 mips16_small, mips16_ext,
2653 prev_pinfo & INSN_UNCOND_BRANCH_DELAY,
2654 history[0].mips16_absolute_jump_p),
2655 make_expr_symbol (address_expr), 0);
252b5132 2656 }
252b5132
RH
2657 else if (mips_opts.mips16
2658 && ! ip->use_extend
f6688943 2659 && *reloc_type != BFD_RELOC_MIPS16_JMP)
9497f5ac 2660 {
b8ee1a6e
DU
2661 if ((pinfo & INSN_UNCOND_BRANCH_DELAY) == 0)
2662 /* Make sure there is enough room to swap this instruction with
2663 a following jump instruction. */
2664 frag_grow (6);
1e915849 2665 add_fixed_insn (ip);
252b5132
RH
2666 }
2667 else
2668 {
2669 if (mips_opts.mips16
2670 && mips_opts.noreorder
2671 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2672 as_warn (_("extended instruction in delay slot"));
2673
4d7206a2
RS
2674 if (mips_relax.sequence)
2675 {
2676 /* If we've reached the end of this frag, turn it into a variant
2677 frag and record the information for the instructions we've
2678 written so far. */
2679 if (frag_room () < 4)
2680 relax_close_frag ();
2681 mips_relax.sizes[mips_relax.sequence - 1] += 4;
2682 }
2683
584892a6
RS
2684 if (mips_relax.sequence != 2)
2685 mips_macro_warning.sizes[0] += 4;
2686 if (mips_relax.sequence != 1)
2687 mips_macro_warning.sizes[1] += 4;
2688
1e915849
RS
2689 if (mips_opts.mips16)
2690 {
2691 ip->fixed_p = 1;
2692 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
2693 }
2694 add_fixed_insn (ip);
252b5132
RH
2695 }
2696
01a3f561 2697 if (address_expr != NULL && *reloc_type <= BFD_RELOC_UNUSED)
252b5132
RH
2698 {
2699 if (address_expr->X_op == O_constant)
2700 {
f17c130b 2701 unsigned int tmp;
f6688943
TS
2702
2703 switch (*reloc_type)
252b5132
RH
2704 {
2705 case BFD_RELOC_32:
2706 ip->insn_opcode |= address_expr->X_add_number;
2707 break;
2708
f6688943 2709 case BFD_RELOC_MIPS_HIGHEST:
f17c130b
AM
2710 tmp = (address_expr->X_add_number + 0x800080008000ull) >> 48;
2711 ip->insn_opcode |= tmp & 0xffff;
f6688943
TS
2712 break;
2713
2714 case BFD_RELOC_MIPS_HIGHER:
f17c130b
AM
2715 tmp = (address_expr->X_add_number + 0x80008000ull) >> 32;
2716 ip->insn_opcode |= tmp & 0xffff;
f6688943
TS
2717 break;
2718
2719 case BFD_RELOC_HI16_S:
f17c130b
AM
2720 tmp = (address_expr->X_add_number + 0x8000) >> 16;
2721 ip->insn_opcode |= tmp & 0xffff;
f6688943
TS
2722 break;
2723
2724 case BFD_RELOC_HI16:
2725 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2726 break;
2727
01a3f561 2728 case BFD_RELOC_UNUSED:
252b5132 2729 case BFD_RELOC_LO16:
ed6fb7bd 2730 case BFD_RELOC_MIPS_GOT_DISP:
252b5132
RH
2731 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2732 break;
2733
2734 case BFD_RELOC_MIPS_JMP:
2735 if ((address_expr->X_add_number & 3) != 0)
2736 as_bad (_("jump to misaligned address (0x%lx)"),
2737 (unsigned long) address_expr->X_add_number);
2738 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2739 break;
2740
2741 case BFD_RELOC_MIPS16_JMP:
2742 if ((address_expr->X_add_number & 3) != 0)
2743 as_bad (_("jump to misaligned address (0x%lx)"),
2744 (unsigned long) address_expr->X_add_number);
2745 ip->insn_opcode |=
2746 (((address_expr->X_add_number & 0x7c0000) << 3)
2747 | ((address_expr->X_add_number & 0xf800000) >> 7)
2748 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2749 break;
2750
252b5132 2751 case BFD_RELOC_16_PCREL_S2:
bad36eac
DJ
2752 if ((address_expr->X_add_number & 3) != 0)
2753 as_bad (_("branch to misaligned address (0x%lx)"),
2754 (unsigned long) address_expr->X_add_number);
2755 if (mips_relax_branch)
2756 goto need_reloc;
2757 if ((address_expr->X_add_number + 0x20000) & ~0x3ffff)
2758 as_bad (_("branch address range overflow (0x%lx)"),
2759 (unsigned long) address_expr->X_add_number);
2760 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0xffff;
2761 break;
252b5132
RH
2762
2763 default:
2764 internalError ();
2765 }
2766 }
01a3f561 2767 else if (*reloc_type < BFD_RELOC_UNUSED)
252b5132 2768 need_reloc:
4d7206a2
RS
2769 {
2770 reloc_howto_type *howto;
2771 int i;
34ce925e 2772
4d7206a2
RS
2773 /* In a compound relocation, it is the final (outermost)
2774 operator that determines the relocated field. */
2775 for (i = 1; i < 3; i++)
2776 if (reloc_type[i] == BFD_RELOC_UNUSED)
2777 break;
34ce925e 2778
4d7206a2 2779 howto = bfd_reloc_type_lookup (stdoutput, reloc_type[i - 1]);
1e915849
RS
2780 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
2781 bfd_get_reloc_size (howto),
2782 address_expr,
2783 reloc_type[0] == BFD_RELOC_16_PCREL_S2,
2784 reloc_type[0]);
4d7206a2
RS
2785
2786 /* These relocations can have an addend that won't fit in
2787 4 octets for 64bit assembly. */
2788 if (HAVE_64BIT_GPRS
2789 && ! howto->partial_inplace
2790 && (reloc_type[0] == BFD_RELOC_16
2791 || reloc_type[0] == BFD_RELOC_32
2792 || reloc_type[0] == BFD_RELOC_MIPS_JMP
2793 || reloc_type[0] == BFD_RELOC_HI16_S
2794 || reloc_type[0] == BFD_RELOC_LO16
2795 || reloc_type[0] == BFD_RELOC_GPREL16
2796 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
2797 || reloc_type[0] == BFD_RELOC_GPREL32
2798 || reloc_type[0] == BFD_RELOC_64
2799 || reloc_type[0] == BFD_RELOC_CTOR
2800 || reloc_type[0] == BFD_RELOC_MIPS_SUB
2801 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
2802 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
2803 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
2804 || reloc_type[0] == BFD_RELOC_MIPS_REL16
d6f16593
MR
2805 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
2806 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
2807 || reloc_type[0] == BFD_RELOC_MIPS16_HI16_S
2808 || reloc_type[0] == BFD_RELOC_MIPS16_LO16))
1e915849 2809 ip->fixp[0]->fx_no_overflow = 1;
4d7206a2
RS
2810
2811 if (mips_relax.sequence)
2812 {
2813 if (mips_relax.first_fixup == 0)
1e915849 2814 mips_relax.first_fixup = ip->fixp[0];
4d7206a2
RS
2815 }
2816 else if (reloc_needs_lo_p (*reloc_type))
2817 {
2818 struct mips_hi_fixup *hi_fixup;
252b5132 2819
4d7206a2
RS
2820 /* Reuse the last entry if it already has a matching %lo. */
2821 hi_fixup = mips_hi_fixup_list;
2822 if (hi_fixup == 0
2823 || !fixup_has_matching_lo_p (hi_fixup->fixp))
2824 {
2825 hi_fixup = ((struct mips_hi_fixup *)
2826 xmalloc (sizeof (struct mips_hi_fixup)));
2827 hi_fixup->next = mips_hi_fixup_list;
2828 mips_hi_fixup_list = hi_fixup;
252b5132 2829 }
1e915849 2830 hi_fixup->fixp = ip->fixp[0];
4d7206a2
RS
2831 hi_fixup->seg = now_seg;
2832 }
f6688943 2833
4d7206a2
RS
2834 /* Add fixups for the second and third relocations, if given.
2835 Note that the ABI allows the second relocation to be
2836 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
2837 moment we only use RSS_UNDEF, but we could add support
2838 for the others if it ever becomes necessary. */
2839 for (i = 1; i < 3; i++)
2840 if (reloc_type[i] != BFD_RELOC_UNUSED)
2841 {
1e915849
RS
2842 ip->fixp[i] = fix_new (ip->frag, ip->where,
2843 ip->fixp[0]->fx_size, NULL, 0,
2844 FALSE, reloc_type[i]);
b1dca8ee
RS
2845
2846 /* Use fx_tcbit to mark compound relocs. */
1e915849
RS
2847 ip->fixp[0]->fx_tcbit = 1;
2848 ip->fixp[i]->fx_tcbit = 1;
4d7206a2 2849 }
252b5132
RH
2850 }
2851 }
1e915849 2852 install_insn (ip);
252b5132
RH
2853
2854 /* Update the register mask information. */
2855 if (! mips_opts.mips16)
2856 {
2857 if (pinfo & INSN_WRITE_GPR_D)
bf12938e 2858 mips_gprmask |= 1 << EXTRACT_OPERAND (RD, *ip);
252b5132 2859 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
bf12938e 2860 mips_gprmask |= 1 << EXTRACT_OPERAND (RT, *ip);
252b5132 2861 if (pinfo & INSN_READ_GPR_S)
bf12938e 2862 mips_gprmask |= 1 << EXTRACT_OPERAND (RS, *ip);
252b5132 2863 if (pinfo & INSN_WRITE_GPR_31)
f9419b05 2864 mips_gprmask |= 1 << RA;
252b5132 2865 if (pinfo & INSN_WRITE_FPR_D)
bf12938e 2866 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FD, *ip);
252b5132 2867 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
bf12938e 2868 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FS, *ip);
252b5132 2869 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
bf12938e 2870 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FT, *ip);
252b5132 2871 if ((pinfo & INSN_READ_FPR_R) != 0)
bf12938e 2872 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FR, *ip);
252b5132
RH
2873 if (pinfo & INSN_COP)
2874 {
bdaaa2e1
KH
2875 /* We don't keep enough information to sort these cases out.
2876 The itbl support does keep this information however, although
2877 we currently don't support itbl fprmats as part of the cop
2878 instruction. May want to add this support in the future. */
252b5132
RH
2879 }
2880 /* Never set the bit for $0, which is always zero. */
beae10d5 2881 mips_gprmask &= ~1 << 0;
252b5132
RH
2882 }
2883 else
2884 {
2885 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
bf12938e 2886 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RX, *ip);
252b5132 2887 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
bf12938e 2888 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RY, *ip);
252b5132 2889 if (pinfo & MIPS16_INSN_WRITE_Z)
bf12938e 2890 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RZ, *ip);
252b5132
RH
2891 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2892 mips_gprmask |= 1 << TREG;
2893 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2894 mips_gprmask |= 1 << SP;
2895 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2896 mips_gprmask |= 1 << RA;
2897 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2898 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2899 if (pinfo & MIPS16_INSN_READ_Z)
bf12938e 2900 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip);
252b5132 2901 if (pinfo & MIPS16_INSN_READ_GPR_X)
bf12938e 2902 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (REGR32, *ip);
252b5132
RH
2903 }
2904
4d7206a2 2905 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
252b5132
RH
2906 {
2907 /* Filling the branch delay slot is more complex. We try to
2908 switch the branch with the previous instruction, which we can
2909 do if the previous instruction does not set up a condition
2910 that the branch tests and if the branch is not itself the
2911 target of any branch. */
2912 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2913 || (pinfo & INSN_COND_BRANCH_DELAY))
2914 {
2915 if (mips_optimize < 2
2916 /* If we have seen .set volatile or .set nomove, don't
2917 optimize. */
2918 || mips_opts.nomove != 0
a38419a5
RS
2919 /* We can't swap if the previous instruction's position
2920 is fixed. */
2921 || history[0].fixed_p
252b5132
RH
2922 /* If the previous previous insn was in a .set
2923 noreorder, we can't swap. Actually, the MIPS
2924 assembler will swap in this situation. However, gcc
2925 configured -with-gnu-as will generate code like
2926 .set noreorder
2927 lw $4,XXX
2928 .set reorder
2929 INSN
2930 bne $4,$0,foo
2931 in which we can not swap the bne and INSN. If gcc is
2932 not configured -with-gnu-as, it does not output the
a38419a5 2933 .set pseudo-ops. */
47e39b9d 2934 || history[1].noreorder_p
252b5132
RH
2935 /* If the branch is itself the target of a branch, we
2936 can not swap. We cheat on this; all we check for is
2937 whether there is a label on this instruction. If
2938 there are any branches to anything other than a
2939 label, users must use .set noreorder. */
a8dbcb85 2940 || si->label_list != NULL
895921c9
MR
2941 /* If the previous instruction is in a variant frag
2942 other than this branch's one, we cannot do the swap.
2943 This does not apply to the mips16, which uses variant
2944 frags for different purposes. */
252b5132 2945 || (! mips_opts.mips16
895921c9 2946 && prev_insn_frag_type == rs_machine_dependent)
71400594
RS
2947 /* Check for conflicts between the branch and the instructions
2948 before the candidate delay slot. */
2949 || nops_for_insn (history + 1, ip) > 0
2950 /* Check for conflicts between the swapped sequence and the
2951 target of the branch. */
2952 || nops_for_sequence (2, history + 1, ip, history) > 0
252b5132
RH
2953 /* We do not swap with a trap instruction, since it
2954 complicates trap handlers to have the trap
2955 instruction be in a delay slot. */
2956 || (prev_pinfo & INSN_TRAP)
2957 /* If the branch reads a register that the previous
2958 instruction sets, we can not swap. */
2959 || (! mips_opts.mips16
2960 && (prev_pinfo & INSN_WRITE_GPR_T)
bf12938e 2961 && insn_uses_reg (ip, EXTRACT_OPERAND (RT, history[0]),
252b5132
RH
2962 MIPS_GR_REG))
2963 || (! mips_opts.mips16
2964 && (prev_pinfo & INSN_WRITE_GPR_D)
bf12938e 2965 && insn_uses_reg (ip, EXTRACT_OPERAND (RD, history[0]),
252b5132
RH
2966 MIPS_GR_REG))
2967 || (mips_opts.mips16
2968 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
bf12938e
RS
2969 && (insn_uses_reg
2970 (ip, MIPS16_EXTRACT_OPERAND (RX, history[0]),
2971 MIPS16_REG)))
252b5132 2972 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
bf12938e
RS
2973 && (insn_uses_reg
2974 (ip, MIPS16_EXTRACT_OPERAND (RY, history[0]),
2975 MIPS16_REG)))
252b5132 2976 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
bf12938e
RS
2977 && (insn_uses_reg
2978 (ip, MIPS16_EXTRACT_OPERAND (RZ, history[0]),
2979 MIPS16_REG)))
252b5132
RH
2980 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2981 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2982 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2983 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2984 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2985 && insn_uses_reg (ip,
47e39b9d
RS
2986 MIPS16OP_EXTRACT_REG32R
2987 (history[0].insn_opcode),
252b5132
RH
2988 MIPS_GR_REG))))
2989 /* If the branch writes a register that the previous
2990 instruction sets, we can not swap (we know that
2991 branches write only to RD or to $31). */
2992 || (! mips_opts.mips16
2993 && (prev_pinfo & INSN_WRITE_GPR_T)
2994 && (((pinfo & INSN_WRITE_GPR_D)
bf12938e
RS
2995 && (EXTRACT_OPERAND (RT, history[0])
2996 == EXTRACT_OPERAND (RD, *ip)))
252b5132 2997 || ((pinfo & INSN_WRITE_GPR_31)
bf12938e 2998 && EXTRACT_OPERAND (RT, history[0]) == RA)))
252b5132
RH
2999 || (! mips_opts.mips16
3000 && (prev_pinfo & INSN_WRITE_GPR_D)
3001 && (((pinfo & INSN_WRITE_GPR_D)
bf12938e
RS
3002 && (EXTRACT_OPERAND (RD, history[0])
3003 == EXTRACT_OPERAND (RD, *ip)))
252b5132 3004 || ((pinfo & INSN_WRITE_GPR_31)
bf12938e 3005 && EXTRACT_OPERAND (RD, history[0]) == RA)))
252b5132
RH
3006 || (mips_opts.mips16
3007 && (pinfo & MIPS16_INSN_WRITE_31)
3008 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
3009 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
47e39b9d 3010 && (MIPS16OP_EXTRACT_REG32R (history[0].insn_opcode)
252b5132
RH
3011 == RA))))
3012 /* If the branch writes a register that the previous
3013 instruction reads, we can not swap (we know that
3014 branches only write to RD or to $31). */
3015 || (! mips_opts.mips16
3016 && (pinfo & INSN_WRITE_GPR_D)
47e39b9d 3017 && insn_uses_reg (&history[0],
bf12938e 3018 EXTRACT_OPERAND (RD, *ip),
252b5132
RH
3019 MIPS_GR_REG))
3020 || (! mips_opts.mips16
3021 && (pinfo & INSN_WRITE_GPR_31)
47e39b9d 3022 && insn_uses_reg (&history[0], RA, MIPS_GR_REG))
252b5132
RH
3023 || (mips_opts.mips16
3024 && (pinfo & MIPS16_INSN_WRITE_31)
47e39b9d 3025 && insn_uses_reg (&history[0], RA, MIPS_GR_REG))
252b5132
RH
3026 /* If one instruction sets a condition code and the
3027 other one uses a condition code, we can not swap. */
3028 || ((pinfo & INSN_READ_COND_CODE)
3029 && (prev_pinfo & INSN_WRITE_COND_CODE))
3030 || ((pinfo & INSN_WRITE_COND_CODE)
3031 && (prev_pinfo & INSN_READ_COND_CODE))
3032 /* If the previous instruction uses the PC, we can not
3033 swap. */
3034 || (mips_opts.mips16
3035 && (prev_pinfo & MIPS16_INSN_READ_PC))
252b5132
RH
3036 /* If the previous instruction had a fixup in mips16
3037 mode, we can not swap. This normally means that the
3038 previous instruction was a 4 byte branch anyhow. */
47e39b9d 3039 || (mips_opts.mips16 && history[0].fixp[0])
bdaaa2e1
KH
3040 /* If the previous instruction is a sync, sync.l, or
3041 sync.p, we can not swap. */
f173e82e 3042 || (prev_pinfo & INSN_SYNC))
252b5132 3043 {
29024861
DU
3044 if (mips_opts.mips16
3045 && (pinfo & INSN_UNCOND_BRANCH_DELAY)
3046 && (pinfo & (MIPS16_INSN_READ_X | MIPS16_INSN_READ_31))
3994f87e 3047 && ISA_SUPPORTS_MIPS16E)
29024861
DU
3048 {
3049 /* Convert MIPS16 jr/jalr into a "compact" jump. */
3050 ip->insn_opcode |= 0x0080;
3051 install_insn (ip);
3052 insert_into_history (0, 1, ip);
3053 }
3054 else
3055 {
3056 /* We could do even better for unconditional branches to
3057 portions of this object file; we could pick up the
3058 instruction at the destination, put it in the delay
3059 slot, and bump the destination address. */
3060 insert_into_history (0, 1, ip);
3061 emit_nop ();
3062 }
3063
dd22970f
ILT
3064 if (mips_relax.sequence)
3065 mips_relax.sizes[mips_relax.sequence - 1] += 4;
252b5132
RH
3066 }
3067 else
3068 {
3069 /* It looks like we can actually do the swap. */
1e915849
RS
3070 struct mips_cl_insn delay = history[0];
3071 if (mips_opts.mips16)
252b5132 3072 {
b8ee1a6e
DU
3073 know (delay.frag == ip->frag);
3074 move_insn (ip, delay.frag, delay.where);
3075 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
1e915849
RS
3076 }
3077 else if (relaxed_branch)
3078 {
3079 /* Add the delay slot instruction to the end of the
3080 current frag and shrink the fixed part of the
3081 original frag. If the branch occupies the tail of
3082 the latter, move it backwards to cover the gap. */
3083 delay.frag->fr_fix -= 4;
3084 if (delay.frag == ip->frag)
3085 move_insn (ip, ip->frag, ip->where - 4);
3086 add_fixed_insn (&delay);
252b5132
RH
3087 }
3088 else
3089 {
1e915849
RS
3090 move_insn (&delay, ip->frag, ip->where);
3091 move_insn (ip, history[0].frag, history[0].where);
252b5132 3092 }
1e915849
RS
3093 history[0] = *ip;
3094 delay.fixed_p = 1;
3095 insert_into_history (0, 1, &delay);
252b5132 3096 }
252b5132
RH
3097
3098 /* If that was an unconditional branch, forget the previous
3099 insn information. */
3100 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
7d10b47d 3101 mips_no_prev_insn ();
252b5132
RH
3102 }
3103 else if (pinfo & INSN_COND_BRANCH_LIKELY)
3104 {
3105 /* We don't yet optimize a branch likely. What we should do
3106 is look at the target, copy the instruction found there
3107 into the delay slot, and increment the branch to jump to
3108 the next instruction. */
1e915849 3109 insert_into_history (0, 1, ip);
252b5132 3110 emit_nop ();
252b5132
RH
3111 }
3112 else
1e915849 3113 insert_into_history (0, 1, ip);
252b5132 3114 }
1e915849
RS
3115 else
3116 insert_into_history (0, 1, ip);
252b5132
RH
3117
3118 /* We just output an insn, so the next one doesn't have a label. */
3119 mips_clear_insn_labels ();
252b5132
RH
3120}
3121
7d10b47d 3122/* Forget that there was any previous instruction or label. */
252b5132
RH
3123
3124static void
7d10b47d 3125mips_no_prev_insn (void)
252b5132 3126{
7d10b47d
RS
3127 prev_nop_frag = NULL;
3128 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
3129 mips_clear_insn_labels ();
3130}
3131
7d10b47d
RS
3132/* This function must be called before we emit something other than
3133 instructions. It is like mips_no_prev_insn except that it inserts
3134 any NOPS that might be needed by previous instructions. */
252b5132 3135
7d10b47d
RS
3136void
3137mips_emit_delays (void)
252b5132
RH
3138{
3139 if (! mips_opts.noreorder)
3140 {
71400594 3141 int nops = nops_for_insn (history, NULL);
252b5132
RH
3142 if (nops > 0)
3143 {
7d10b47d
RS
3144 while (nops-- > 0)
3145 add_fixed_insn (NOP_INSN);
3146 mips_move_labels ();
3147 }
3148 }
3149 mips_no_prev_insn ();
3150}
3151
3152/* Start a (possibly nested) noreorder block. */
3153
3154static void
3155start_noreorder (void)
3156{
3157 if (mips_opts.noreorder == 0)
3158 {
3159 unsigned int i;
3160 int nops;
3161
3162 /* None of the instructions before the .set noreorder can be moved. */
3163 for (i = 0; i < ARRAY_SIZE (history); i++)
3164 history[i].fixed_p = 1;
3165
3166 /* Insert any nops that might be needed between the .set noreorder
3167 block and the previous instructions. We will later remove any
3168 nops that turn out not to be needed. */
3169 nops = nops_for_insn (history, NULL);
3170 if (nops > 0)
3171 {
3172 if (mips_optimize != 0)
252b5132
RH
3173 {
3174 /* Record the frag which holds the nop instructions, so
3175 that we can remove them if we don't need them. */
3176 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
3177 prev_nop_frag = frag_now;
3178 prev_nop_frag_holds = nops;
3179 prev_nop_frag_required = 0;
3180 prev_nop_frag_since = 0;
3181 }
3182
3183 for (; nops > 0; --nops)
1e915849 3184 add_fixed_insn (NOP_INSN);
252b5132 3185
7d10b47d
RS
3186 /* Move on to a new frag, so that it is safe to simply
3187 decrease the size of prev_nop_frag. */
3188 frag_wane (frag_now);
3189 frag_new (0);
404a8071 3190 mips_move_labels ();
252b5132 3191 }
7d10b47d
RS
3192 mips16_mark_labels ();
3193 mips_clear_insn_labels ();
252b5132 3194 }
7d10b47d
RS
3195 mips_opts.noreorder++;
3196 mips_any_noreorder = 1;
3197}
252b5132 3198
7d10b47d 3199/* End a nested noreorder block. */
252b5132 3200
7d10b47d
RS
3201static void
3202end_noreorder (void)
3203{
3204 mips_opts.noreorder--;
3205 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
3206 {
3207 /* Commit to inserting prev_nop_frag_required nops and go back to
3208 handling nop insertion the .set reorder way. */
3209 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
3210 * (mips_opts.mips16 ? 2 : 4));
3211 insert_into_history (prev_nop_frag_since,
3212 prev_nop_frag_required, NOP_INSN);
3213 prev_nop_frag = NULL;
3214 }
252b5132
RH
3215}
3216
584892a6
RS
3217/* Set up global variables for the start of a new macro. */
3218
3219static void
3220macro_start (void)
3221{
3222 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
3223 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
47e39b9d 3224 && (history[0].insn_mo->pinfo
584892a6
RS
3225 & (INSN_UNCOND_BRANCH_DELAY
3226 | INSN_COND_BRANCH_DELAY
3227 | INSN_COND_BRANCH_LIKELY)) != 0);
3228}
3229
3230/* Given that a macro is longer than 4 bytes, return the appropriate warning
3231 for it. Return null if no warning is needed. SUBTYPE is a bitmask of
3232 RELAX_DELAY_SLOT and RELAX_NOMACRO. */
3233
3234static const char *
3235macro_warning (relax_substateT subtype)
3236{
3237 if (subtype & RELAX_DELAY_SLOT)
3238 return _("Macro instruction expanded into multiple instructions"
3239 " in a branch delay slot");
3240 else if (subtype & RELAX_NOMACRO)
3241 return _("Macro instruction expanded into multiple instructions");
3242 else
3243 return 0;
3244}
3245
3246/* Finish up a macro. Emit warnings as appropriate. */
3247
3248static void
3249macro_end (void)
3250{
3251 if (mips_macro_warning.sizes[0] > 4 || mips_macro_warning.sizes[1] > 4)
3252 {
3253 relax_substateT subtype;
3254
3255 /* Set up the relaxation warning flags. */
3256 subtype = 0;
3257 if (mips_macro_warning.sizes[1] > mips_macro_warning.sizes[0])
3258 subtype |= RELAX_SECOND_LONGER;
3259 if (mips_opts.warn_about_macros)
3260 subtype |= RELAX_NOMACRO;
3261 if (mips_macro_warning.delay_slot_p)
3262 subtype |= RELAX_DELAY_SLOT;
3263
3264 if (mips_macro_warning.sizes[0] > 4 && mips_macro_warning.sizes[1] > 4)
3265 {
3266 /* Either the macro has a single implementation or both
3267 implementations are longer than 4 bytes. Emit the
3268 warning now. */
3269 const char *msg = macro_warning (subtype);
3270 if (msg != 0)
3271 as_warn (msg);
3272 }
3273 else
3274 {
3275 /* One implementation might need a warning but the other
3276 definitely doesn't. */
3277 mips_macro_warning.first_frag->fr_subtype |= subtype;
3278 }
3279 }
3280}
3281
6e1304d8
RS
3282/* Read a macro's relocation codes from *ARGS and store them in *R.
3283 The first argument in *ARGS will be either the code for a single
3284 relocation or -1 followed by the three codes that make up a
3285 composite relocation. */
3286
3287static void
3288macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
3289{
3290 int i, next;
3291
3292 next = va_arg (*args, int);
3293 if (next >= 0)
3294 r[0] = (bfd_reloc_code_real_type) next;
3295 else
3296 for (i = 0; i < 3; i++)
3297 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
3298}
3299
252b5132
RH
3300/* Build an instruction created by a macro expansion. This is passed
3301 a pointer to the count of instructions created so far, an
3302 expression, the name of the instruction to build, an operand format
3303 string, and corresponding arguments. */
3304
252b5132 3305static void
67c0d1eb 3306macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 3307{
1e915849 3308 const struct mips_opcode *mo;
252b5132 3309 struct mips_cl_insn insn;
f6688943 3310 bfd_reloc_code_real_type r[3];
252b5132 3311 va_list args;
252b5132 3312
252b5132 3313 va_start (args, fmt);
252b5132 3314
252b5132
RH
3315 if (mips_opts.mips16)
3316 {
67c0d1eb 3317 mips16_macro_build (ep, name, fmt, args);
252b5132
RH
3318 va_end (args);
3319 return;
3320 }
3321
f6688943
TS
3322 r[0] = BFD_RELOC_UNUSED;
3323 r[1] = BFD_RELOC_UNUSED;
3324 r[2] = BFD_RELOC_UNUSED;
1e915849
RS
3325 mo = (struct mips_opcode *) hash_find (op_hash, name);
3326 assert (mo);
3327 assert (strcmp (name, mo->name) == 0);
3328
3329 /* Search until we get a match for NAME. It is assumed here that
65263ce3 3330 macros will never generate MDMX, MIPS-3D, DSP or MT instructions. */
1e915849
RS
3331 while (strcmp (fmt, mo->args) != 0
3332 || mo->pinfo == INSN_MACRO
3333 || !OPCODE_IS_MEMBER (mo,
3334 (mips_opts.isa
9b3f89ee 3335 | (mips_opts.mips16 ? INSN_MIPS16 : 0)
e16bfa71 3336 | (mips_opts.ase_smartmips ? INSN_SMARTMIPS : 0)),
fef14a42 3337 mips_opts.arch)
1e915849
RS
3338 || (mips_opts.arch == CPU_R4650 && (mo->pinfo & FP_D) != 0))
3339 {
3340 ++mo;
3341 assert (mo->name);
3342 assert (strcmp (name, mo->name) == 0);
252b5132
RH
3343 }
3344
1e915849 3345 create_insn (&insn, mo);
252b5132
RH
3346 for (;;)
3347 {
3348 switch (*fmt++)
3349 {
3350 case '\0':
3351 break;
3352
3353 case ',':
3354 case '(':
3355 case ')':
3356 continue;
3357
5f74bc13
CD
3358 case '+':
3359 switch (*fmt++)
3360 {
3361 case 'A':
3362 case 'E':
bf12938e 3363 INSERT_OPERAND (SHAMT, insn, va_arg (args, int));
5f74bc13
CD
3364 continue;
3365
3366 case 'B':
3367 case 'F':
3368 /* Note that in the macro case, these arguments are already
3369 in MSB form. (When handling the instruction in the
3370 non-macro case, these arguments are sizes from which
3371 MSB values must be calculated.) */
bf12938e 3372 INSERT_OPERAND (INSMSB, insn, va_arg (args, int));
5f74bc13
CD
3373 continue;
3374
3375 case 'C':
3376 case 'G':
3377 case 'H':
3378 /* Note that in the macro case, these arguments are already
3379 in MSBD form. (When handling the instruction in the
3380 non-macro case, these arguments are sizes from which
3381 MSBD values must be calculated.) */
bf12938e 3382 INSERT_OPERAND (EXTMSBD, insn, va_arg (args, int));
5f74bc13
CD
3383 continue;
3384
3385 default:
3386 internalError ();
3387 }
3388 continue;
3389
252b5132
RH
3390 case 't':
3391 case 'w':
3392 case 'E':
bf12938e 3393 INSERT_OPERAND (RT, insn, va_arg (args, int));
252b5132
RH
3394 continue;
3395
3396 case 'c':
bf12938e 3397 INSERT_OPERAND (CODE, insn, va_arg (args, int));
38487616
TS
3398 continue;
3399
252b5132
RH
3400 case 'T':
3401 case 'W':
bf12938e 3402 INSERT_OPERAND (FT, insn, va_arg (args, int));
252b5132
RH
3403 continue;
3404
3405 case 'd':
3406 case 'G':
af7ee8bf 3407 case 'K':
bf12938e 3408 INSERT_OPERAND (RD, insn, va_arg (args, int));
252b5132
RH
3409 continue;
3410
4372b673
NC
3411 case 'U':
3412 {
3413 int tmp = va_arg (args, int);
3414
bf12938e
RS
3415 INSERT_OPERAND (RT, insn, tmp);
3416 INSERT_OPERAND (RD, insn, tmp);
beae10d5 3417 continue;
4372b673
NC
3418 }
3419
252b5132
RH
3420 case 'V':
3421 case 'S':
bf12938e 3422 INSERT_OPERAND (FS, insn, va_arg (args, int));
252b5132
RH
3423 continue;
3424
3425 case 'z':
3426 continue;
3427
3428 case '<':
bf12938e 3429 INSERT_OPERAND (SHAMT, insn, va_arg (args, int));
252b5132
RH
3430 continue;
3431
3432 case 'D':
bf12938e 3433 INSERT_OPERAND (FD, insn, va_arg (args, int));
252b5132
RH
3434 continue;
3435
3436 case 'B':
bf12938e 3437 INSERT_OPERAND (CODE20, insn, va_arg (args, int));
252b5132
RH
3438 continue;
3439
4372b673 3440 case 'J':
bf12938e 3441 INSERT_OPERAND (CODE19, insn, va_arg (args, int));
4372b673
NC
3442 continue;
3443
252b5132 3444 case 'q':
bf12938e 3445 INSERT_OPERAND (CODE2, insn, va_arg (args, int));
252b5132
RH
3446 continue;
3447
3448 case 'b':
3449 case 's':
3450 case 'r':
3451 case 'v':
bf12938e 3452 INSERT_OPERAND (RS, insn, va_arg (args, int));
252b5132
RH
3453 continue;
3454
3455 case 'i':
3456 case 'j':
3457 case 'o':
6e1304d8 3458 macro_read_relocs (&args, r);
cdf6fd85 3459 assert (*r == BFD_RELOC_GPREL16
f6688943
TS
3460 || *r == BFD_RELOC_MIPS_LITERAL
3461 || *r == BFD_RELOC_MIPS_HIGHER
3462 || *r == BFD_RELOC_HI16_S
3463 || *r == BFD_RELOC_LO16
3464 || *r == BFD_RELOC_MIPS_GOT16
3465 || *r == BFD_RELOC_MIPS_CALL16
438c16b8
TS
3466 || *r == BFD_RELOC_MIPS_GOT_DISP
3467 || *r == BFD_RELOC_MIPS_GOT_PAGE
3468 || *r == BFD_RELOC_MIPS_GOT_OFST
f6688943 3469 || *r == BFD_RELOC_MIPS_GOT_LO16
3e722fb5 3470 || *r == BFD_RELOC_MIPS_CALL_LO16);
252b5132
RH
3471 continue;
3472
3473 case 'u':
6e1304d8 3474 macro_read_relocs (&args, r);
252b5132
RH
3475 assert (ep != NULL
3476 && (ep->X_op == O_constant
3477 || (ep->X_op == O_symbol
f6688943
TS
3478 && (*r == BFD_RELOC_MIPS_HIGHEST
3479 || *r == BFD_RELOC_HI16_S
3480 || *r == BFD_RELOC_HI16
3481 || *r == BFD_RELOC_GPREL16
3482 || *r == BFD_RELOC_MIPS_GOT_HI16
3e722fb5 3483 || *r == BFD_RELOC_MIPS_CALL_HI16))));
252b5132
RH
3484 continue;
3485
3486 case 'p':
3487 assert (ep != NULL);
bad36eac 3488
252b5132
RH
3489 /*
3490 * This allows macro() to pass an immediate expression for
3491 * creating short branches without creating a symbol.
bad36eac
DJ
3492 *
3493 * We don't allow branch relaxation for these branches, as
3494 * they should only appear in ".set nomacro" anyway.
252b5132
RH
3495 */
3496 if (ep->X_op == O_constant)
3497 {
bad36eac
DJ
3498 if ((ep->X_add_number & 3) != 0)
3499 as_bad (_("branch to misaligned address (0x%lx)"),
3500 (unsigned long) ep->X_add_number);
3501 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
3502 as_bad (_("branch address range overflow (0x%lx)"),
3503 (unsigned long) ep->X_add_number);
252b5132
RH
3504 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3505 ep = NULL;
3506 }
3507 else
0b25d3e6 3508 *r = BFD_RELOC_16_PCREL_S2;
252b5132
RH
3509 continue;
3510
3511 case 'a':
3512 assert (ep != NULL);
f6688943 3513 *r = BFD_RELOC_MIPS_JMP;
252b5132
RH
3514 continue;
3515
3516 case 'C':
a9e24354 3517 INSERT_OPERAND (COPZ, insn, va_arg (args, unsigned long));
252b5132
RH
3518 continue;
3519
d43b4baf 3520 case 'k':
a9e24354 3521 INSERT_OPERAND (CACHE, insn, va_arg (args, unsigned long));
d43b4baf
TS
3522 continue;
3523
252b5132
RH
3524 default:
3525 internalError ();
3526 }
3527 break;
3528 }
3529 va_end (args);
f6688943 3530 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 3531
4d7206a2 3532 append_insn (&insn, ep, r);
252b5132
RH
3533}
3534
3535static void
67c0d1eb 3536mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
17a2f251 3537 va_list args)
252b5132 3538{
1e915849 3539 struct mips_opcode *mo;
252b5132 3540 struct mips_cl_insn insn;
f6688943
TS
3541 bfd_reloc_code_real_type r[3]
3542 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 3543
1e915849
RS
3544 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3545 assert (mo);
3546 assert (strcmp (name, mo->name) == 0);
252b5132 3547
1e915849 3548 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 3549 {
1e915849
RS
3550 ++mo;
3551 assert (mo->name);
3552 assert (strcmp (name, mo->name) == 0);
252b5132
RH
3553 }
3554
1e915849 3555 create_insn (&insn, mo);
252b5132
RH
3556 for (;;)
3557 {
3558 int c;
3559
3560 c = *fmt++;
3561 switch (c)
3562 {
3563 case '\0':
3564 break;
3565
3566 case ',':
3567 case '(':
3568 case ')':
3569 continue;
3570
3571 case 'y':
3572 case 'w':
bf12938e 3573 MIPS16_INSERT_OPERAND (RY, insn, va_arg (args, int));
252b5132
RH
3574 continue;
3575
3576 case 'x':
3577 case 'v':
bf12938e 3578 MIPS16_INSERT_OPERAND (RX, insn, va_arg (args, int));
252b5132
RH
3579 continue;
3580
3581 case 'z':
bf12938e 3582 MIPS16_INSERT_OPERAND (RZ, insn, va_arg (args, int));
252b5132
RH
3583 continue;
3584
3585 case 'Z':
bf12938e 3586 MIPS16_INSERT_OPERAND (MOVE32Z, insn, va_arg (args, int));
252b5132
RH
3587 continue;
3588
3589 case '0':
3590 case 'S':
3591 case 'P':
3592 case 'R':
3593 continue;
3594
3595 case 'X':
bf12938e 3596 MIPS16_INSERT_OPERAND (REGR32, insn, va_arg (args, int));
252b5132
RH
3597 continue;
3598
3599 case 'Y':
3600 {
3601 int regno;
3602
3603 regno = va_arg (args, int);
3604 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
a9e24354 3605 MIPS16_INSERT_OPERAND (REG32R, insn, regno);
252b5132
RH
3606 }
3607 continue;
3608
3609 case '<':
3610 case '>':
3611 case '4':
3612 case '5':
3613 case 'H':
3614 case 'W':
3615 case 'D':
3616 case 'j':
3617 case '8':
3618 case 'V':
3619 case 'C':
3620 case 'U':
3621 case 'k':
3622 case 'K':
3623 case 'p':
3624 case 'q':
3625 {
3626 assert (ep != NULL);
3627
3628 if (ep->X_op != O_constant)
874e8986 3629 *r = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
3630 else
3631 {
b34976b6
AM
3632 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3633 FALSE, &insn.insn_opcode, &insn.use_extend,
c4e7957c 3634 &insn.extend);
252b5132 3635 ep = NULL;
f6688943 3636 *r = BFD_RELOC_UNUSED;
252b5132
RH
3637 }
3638 }
3639 continue;
3640
3641 case '6':
bf12938e 3642 MIPS16_INSERT_OPERAND (IMM6, insn, va_arg (args, int));
252b5132
RH
3643 continue;
3644 }
3645
3646 break;
3647 }
3648
f6688943 3649 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 3650
4d7206a2 3651 append_insn (&insn, ep, r);
252b5132
RH
3652}
3653
2051e8c4
MR
3654/*
3655 * Sign-extend 32-bit mode constants that have bit 31 set and all
3656 * higher bits unset.
3657 */
9f872bbe 3658static void
2051e8c4
MR
3659normalize_constant_expr (expressionS *ex)
3660{
9ee2a2d4 3661 if (ex->X_op == O_constant
2051e8c4
MR
3662 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
3663 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3664 - 0x80000000);
3665}
3666
3667/*
3668 * Sign-extend 32-bit mode address offsets that have bit 31 set and
3669 * all higher bits unset.
3670 */
3671static void
3672normalize_address_expr (expressionS *ex)
3673{
3674 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
3675 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
3676 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
3677 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3678 - 0x80000000);
3679}
3680
438c16b8
TS
3681/*
3682 * Generate a "jalr" instruction with a relocation hint to the called
3683 * function. This occurs in NewABI PIC code.
3684 */
3685static void
67c0d1eb 3686macro_build_jalr (expressionS *ep)
438c16b8 3687{
685736be 3688 char *f = NULL;
b34976b6 3689
438c16b8 3690 if (HAVE_NEWABI)
f21f8242 3691 {
cc3d92a5 3692 frag_grow (8);
f21f8242
AO
3693 f = frag_more (0);
3694 }
67c0d1eb 3695 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
438c16b8 3696 if (HAVE_NEWABI)
f21f8242 3697 fix_new_exp (frag_now, f - frag_now->fr_literal,
a105a300 3698 4, ep, FALSE, BFD_RELOC_MIPS_JALR);
438c16b8
TS
3699}
3700
252b5132
RH
3701/*
3702 * Generate a "lui" instruction.
3703 */
3704static void
67c0d1eb 3705macro_build_lui (expressionS *ep, int regnum)
252b5132
RH
3706{
3707 expressionS high_expr;
1e915849 3708 const struct mips_opcode *mo;
252b5132 3709 struct mips_cl_insn insn;
f6688943
TS
3710 bfd_reloc_code_real_type r[3]
3711 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
5a38dc70
AM
3712 const char *name = "lui";
3713 const char *fmt = "t,u";
252b5132
RH
3714
3715 assert (! mips_opts.mips16);
3716
4d7206a2 3717 high_expr = *ep;
252b5132
RH
3718
3719 if (high_expr.X_op == O_constant)
3720 {
54f4ddb3 3721 /* We can compute the instruction now without a relocation entry. */
e7d556df
TS
3722 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3723 >> 16) & 0xffff;
f6688943 3724 *r = BFD_RELOC_UNUSED;
252b5132 3725 }
78e1bb40 3726 else
252b5132
RH
3727 {
3728 assert (ep->X_op == O_symbol);
bbe506e8
TS
3729 /* _gp_disp is a special case, used from s_cpload.
3730 __gnu_local_gp is used if mips_no_shared. */
252b5132 3731 assert (mips_pic == NO_PIC
78e1bb40 3732 || (! HAVE_NEWABI
aa6975fb
ILT
3733 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
3734 || (! mips_in_shared
bbe506e8
TS
3735 && strcmp (S_GET_NAME (ep->X_add_symbol),
3736 "__gnu_local_gp") == 0));
f6688943 3737 *r = BFD_RELOC_HI16_S;
252b5132
RH
3738 }
3739
1e915849
RS
3740 mo = hash_find (op_hash, name);
3741 assert (strcmp (name, mo->name) == 0);
3742 assert (strcmp (fmt, mo->args) == 0);
3743 create_insn (&insn, mo);
252b5132 3744
bf12938e
RS
3745 insn.insn_opcode = insn.insn_mo->match;
3746 INSERT_OPERAND (RT, insn, regnum);
f6688943 3747 if (*r == BFD_RELOC_UNUSED)
252b5132
RH
3748 {
3749 insn.insn_opcode |= high_expr.X_add_number;
4d7206a2 3750 append_insn (&insn, NULL, r);
252b5132
RH
3751 }
3752 else
4d7206a2 3753 append_insn (&insn, &high_expr, r);
252b5132
RH
3754}
3755
885add95
CD
3756/* Generate a sequence of instructions to do a load or store from a constant
3757 offset off of a base register (breg) into/from a target register (treg),
3758 using AT if necessary. */
3759static void
67c0d1eb
RS
3760macro_build_ldst_constoffset (expressionS *ep, const char *op,
3761 int treg, int breg, int dbl)
885add95
CD
3762{
3763 assert (ep->X_op == O_constant);
3764
256ab948 3765 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
3766 if (!dbl)
3767 normalize_constant_expr (ep);
256ab948 3768
67c1ffbe 3769 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 3770 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
3771 as_warn (_("operand overflow"));
3772
3773 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3774 {
3775 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 3776 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
3777 }
3778 else
3779 {
3780 /* 32-bit offset, need multiple instructions and AT, like:
3781 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
3782 addu $tempreg,$tempreg,$breg
3783 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
3784 to handle the complete offset. */
67c0d1eb
RS
3785 macro_build_lui (ep, AT);
3786 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
3787 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95
CD
3788
3789 if (mips_opts.noat)
8fc2e39e 3790 as_bad (_("Macro used $at after \".set noat\""));
885add95
CD
3791 }
3792}
3793
252b5132
RH
3794/* set_at()
3795 * Generates code to set the $at register to true (one)
3796 * if reg is less than the immediate expression.
3797 */
3798static void
67c0d1eb 3799set_at (int reg, int unsignedp)
252b5132
RH
3800{
3801 if (imm_expr.X_op == O_constant
3802 && imm_expr.X_add_number >= -0x8000
3803 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
3804 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
3805 AT, reg, BFD_RELOC_LO16);
252b5132
RH
3806 else
3807 {
67c0d1eb
RS
3808 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
3809 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
3810 }
3811}
3812
3813/* Warn if an expression is not a constant. */
3814
3815static void
17a2f251 3816check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
252b5132
RH
3817{
3818 if (ex->X_op == O_big)
3819 as_bad (_("unsupported large constant"));
3820 else if (ex->X_op != O_constant)
9ee2a2d4
MR
3821 as_bad (_("Instruction %s requires absolute expression"),
3822 ip->insn_mo->name);
13757d0c 3823
9ee2a2d4
MR
3824 if (HAVE_32BIT_GPRS)
3825 normalize_constant_expr (ex);
252b5132
RH
3826}
3827
3828/* Count the leading zeroes by performing a binary chop. This is a
3829 bulky bit of source, but performance is a LOT better for the
3830 majority of values than a simple loop to count the bits:
3831 for (lcnt = 0; (lcnt < 32); lcnt++)
3832 if ((v) & (1 << (31 - lcnt)))
3833 break;
3834 However it is not code size friendly, and the gain will drop a bit
3835 on certain cached systems.
3836*/
3837#define COUNT_TOP_ZEROES(v) \
3838 (((v) & ~0xffff) == 0 \
3839 ? ((v) & ~0xff) == 0 \
3840 ? ((v) & ~0xf) == 0 \
3841 ? ((v) & ~0x3) == 0 \
3842 ? ((v) & ~0x1) == 0 \
3843 ? !(v) \
3844 ? 32 \
3845 : 31 \
3846 : 30 \
3847 : ((v) & ~0x7) == 0 \
3848 ? 29 \
3849 : 28 \
3850 : ((v) & ~0x3f) == 0 \
3851 ? ((v) & ~0x1f) == 0 \
3852 ? 27 \
3853 : 26 \
3854 : ((v) & ~0x7f) == 0 \
3855 ? 25 \
3856 : 24 \
3857 : ((v) & ~0xfff) == 0 \
3858 ? ((v) & ~0x3ff) == 0 \
3859 ? ((v) & ~0x1ff) == 0 \
3860 ? 23 \
3861 : 22 \
3862 : ((v) & ~0x7ff) == 0 \
3863 ? 21 \
3864 : 20 \
3865 : ((v) & ~0x3fff) == 0 \
3866 ? ((v) & ~0x1fff) == 0 \
3867 ? 19 \
3868 : 18 \
3869 : ((v) & ~0x7fff) == 0 \
3870 ? 17 \
3871 : 16 \
3872 : ((v) & ~0xffffff) == 0 \
3873 ? ((v) & ~0xfffff) == 0 \
3874 ? ((v) & ~0x3ffff) == 0 \
3875 ? ((v) & ~0x1ffff) == 0 \
3876 ? 15 \
3877 : 14 \
3878 : ((v) & ~0x7ffff) == 0 \
3879 ? 13 \
3880 : 12 \
3881 : ((v) & ~0x3fffff) == 0 \
3882 ? ((v) & ~0x1fffff) == 0 \
3883 ? 11 \
3884 : 10 \
3885 : ((v) & ~0x7fffff) == 0 \
3886 ? 9 \
3887 : 8 \
3888 : ((v) & ~0xfffffff) == 0 \
3889 ? ((v) & ~0x3ffffff) == 0 \
3890 ? ((v) & ~0x1ffffff) == 0 \
3891 ? 7 \
3892 : 6 \
3893 : ((v) & ~0x7ffffff) == 0 \
3894 ? 5 \
3895 : 4 \
3896 : ((v) & ~0x3fffffff) == 0 \
3897 ? ((v) & ~0x1fffffff) == 0 \
3898 ? 3 \
3899 : 2 \
3900 : ((v) & ~0x7fffffff) == 0 \
3901 ? 1 \
3902 : 0)
3903
3904/* load_register()
67c1ffbe 3905 * This routine generates the least number of instructions necessary to load
252b5132
RH
3906 * an absolute expression value into a register.
3907 */
3908static void
67c0d1eb 3909load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
3910{
3911 int freg;
3912 expressionS hi32, lo32;
3913
3914 if (ep->X_op != O_big)
3915 {
3916 assert (ep->X_op == O_constant);
256ab948
TS
3917
3918 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
3919 if (!dbl)
3920 normalize_constant_expr (ep);
256ab948
TS
3921
3922 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
3923 {
3924 /* We can handle 16 bit signed values with an addiu to
3925 $zero. No need to ever use daddiu here, since $zero and
3926 the result are always correct in 32 bit mode. */
67c0d1eb 3927 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
3928 return;
3929 }
3930 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3931 {
3932 /* We can handle 16 bit unsigned values with an ori to
3933 $zero. */
67c0d1eb 3934 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
3935 return;
3936 }
256ab948 3937 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
3938 {
3939 /* 32 bit values require an lui. */
67c0d1eb 3940 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_HI16);
252b5132 3941 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 3942 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
3943 return;
3944 }
3945 }
3946
3947 /* The value is larger than 32 bits. */
3948
2051e8c4 3949 if (!dbl || HAVE_32BIT_GPRS)
252b5132 3950 {
55e08f71
NC
3951 char value[32];
3952
3953 sprintf_vma (value, ep->X_add_number);
20e1fcfd 3954 as_bad (_("Number (0x%s) larger than 32 bits"), value);
67c0d1eb 3955 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
3956 return;
3957 }
3958
3959 if (ep->X_op != O_big)
3960 {
3961 hi32 = *ep;
3962 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3963 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3964 hi32.X_add_number &= 0xffffffff;
3965 lo32 = *ep;
3966 lo32.X_add_number &= 0xffffffff;
3967 }
3968 else
3969 {
3970 assert (ep->X_add_number > 2);
3971 if (ep->X_add_number == 3)
3972 generic_bignum[3] = 0;
3973 else if (ep->X_add_number > 4)
3974 as_bad (_("Number larger than 64 bits"));
3975 lo32.X_op = O_constant;
3976 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3977 hi32.X_op = O_constant;
3978 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3979 }
3980
3981 if (hi32.X_add_number == 0)
3982 freg = 0;
3983 else
3984 {
3985 int shift, bit;
3986 unsigned long hi, lo;
3987
956cd1d6 3988 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
3989 {
3990 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3991 {
67c0d1eb 3992 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
3993 return;
3994 }
3995 if (lo32.X_add_number & 0x80000000)
3996 {
67c0d1eb 3997 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
252b5132 3998 if (lo32.X_add_number & 0xffff)
67c0d1eb 3999 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
4000 return;
4001 }
4002 }
252b5132
RH
4003
4004 /* Check for 16bit shifted constant. We know that hi32 is
4005 non-zero, so start the mask on the first bit of the hi32
4006 value. */
4007 shift = 17;
4008 do
beae10d5
KH
4009 {
4010 unsigned long himask, lomask;
4011
4012 if (shift < 32)
4013 {
4014 himask = 0xffff >> (32 - shift);
4015 lomask = (0xffff << shift) & 0xffffffff;
4016 }
4017 else
4018 {
4019 himask = 0xffff << (shift - 32);
4020 lomask = 0;
4021 }
4022 if ((hi32.X_add_number & ~(offsetT) himask) == 0
4023 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
4024 {
4025 expressionS tmp;
4026
4027 tmp.X_op = O_constant;
4028 if (shift < 32)
4029 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
4030 | (lo32.X_add_number >> shift));
4031 else
4032 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb
RS
4033 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
4034 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", "d,w,<",
4035 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
4036 return;
4037 }
f9419b05 4038 ++shift;
beae10d5
KH
4039 }
4040 while (shift <= (64 - 16));
252b5132
RH
4041
4042 /* Find the bit number of the lowest one bit, and store the
4043 shifted value in hi/lo. */
4044 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
4045 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
4046 if (lo != 0)
4047 {
4048 bit = 0;
4049 while ((lo & 1) == 0)
4050 {
4051 lo >>= 1;
4052 ++bit;
4053 }
4054 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
4055 hi >>= bit;
4056 }
4057 else
4058 {
4059 bit = 32;
4060 while ((hi & 1) == 0)
4061 {
4062 hi >>= 1;
4063 ++bit;
4064 }
4065 lo = hi;
4066 hi = 0;
4067 }
4068
4069 /* Optimize if the shifted value is a (power of 2) - 1. */
4070 if ((hi == 0 && ((lo + 1) & lo) == 0)
4071 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
4072 {
4073 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 4074 if (shift != 0)
beae10d5 4075 {
252b5132
RH
4076 expressionS tmp;
4077
4078 /* This instruction will set the register to be all
4079 ones. */
beae10d5
KH
4080 tmp.X_op = O_constant;
4081 tmp.X_add_number = (offsetT) -1;
67c0d1eb 4082 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
4083 if (bit != 0)
4084 {
4085 bit += shift;
67c0d1eb
RS
4086 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", "d,w,<",
4087 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 4088 }
67c0d1eb
RS
4089 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", "d,w,<",
4090 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
4091 return;
4092 }
4093 }
252b5132
RH
4094
4095 /* Sign extend hi32 before calling load_register, because we can
4096 generally get better code when we load a sign extended value. */
4097 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 4098 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 4099 load_register (reg, &hi32, 0);
252b5132
RH
4100 freg = reg;
4101 }
4102 if ((lo32.X_add_number & 0xffff0000) == 0)
4103 {
4104 if (freg != 0)
4105 {
67c0d1eb 4106 macro_build (NULL, "dsll32", "d,w,<", reg, freg, 0);
252b5132
RH
4107 freg = reg;
4108 }
4109 }
4110 else
4111 {
4112 expressionS mid16;
4113
956cd1d6 4114 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 4115 {
67c0d1eb
RS
4116 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
4117 macro_build (NULL, "dsrl32", "d,w,<", reg, reg, 0);
beae10d5
KH
4118 return;
4119 }
252b5132
RH
4120
4121 if (freg != 0)
4122 {
67c0d1eb 4123 macro_build (NULL, "dsll", "d,w,<", reg, freg, 16);
252b5132
RH
4124 freg = reg;
4125 }
4126 mid16 = lo32;
4127 mid16.X_add_number >>= 16;
67c0d1eb
RS
4128 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
4129 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
252b5132
RH
4130 freg = reg;
4131 }
4132 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 4133 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
4134}
4135
269137b2
TS
4136static inline void
4137load_delay_nop (void)
4138{
4139 if (!gpr_interlocks)
4140 macro_build (NULL, "nop", "");
4141}
4142
252b5132
RH
4143/* Load an address into a register. */
4144
4145static void
67c0d1eb 4146load_address (int reg, expressionS *ep, int *used_at)
252b5132 4147{
252b5132
RH
4148 if (ep->X_op != O_constant
4149 && ep->X_op != O_symbol)
4150 {
4151 as_bad (_("expression too complex"));
4152 ep->X_op = O_constant;
4153 }
4154
4155 if (ep->X_op == O_constant)
4156 {
67c0d1eb 4157 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
4158 return;
4159 }
4160
4161 if (mips_pic == NO_PIC)
4162 {
4163 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 4164 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
4165 Otherwise we want
4166 lui $reg,<sym> (BFD_RELOC_HI16_S)
4167 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 4168 If we have an addend, we always use the latter form.
76b3015f 4169
d6bc6245
TS
4170 With 64bit address space and a usable $at we want
4171 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4172 lui $at,<sym> (BFD_RELOC_HI16_S)
4173 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
4174 daddiu $at,<sym> (BFD_RELOC_LO16)
4175 dsll32 $reg,0
3a482fd5 4176 daddu $reg,$reg,$at
76b3015f 4177
c03099e6 4178 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
4179 on superscalar processors.
4180 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4181 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
4182 dsll $reg,16
4183 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
4184 dsll $reg,16
4185 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
4186
4187 For GP relative symbols in 64bit address space we can use
4188 the same sequence as in 32bit address space. */
aed1a261 4189 if (HAVE_64BIT_SYMBOLS)
d6bc6245 4190 {
6caf9ef4
TS
4191 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
4192 && !nopic_need_relax (ep->X_add_symbol, 1))
4193 {
4194 relax_start (ep->X_add_symbol);
4195 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
4196 mips_gp_register, BFD_RELOC_GPREL16);
4197 relax_switch ();
4198 }
d6bc6245 4199
b8285c27 4200 if (*used_at == 0 && !mips_opts.noat)
d6bc6245 4201 {
67c0d1eb
RS
4202 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
4203 macro_build (ep, "lui", "t,u", AT, BFD_RELOC_HI16_S);
4204 macro_build (ep, "daddiu", "t,r,j", reg, reg,
4205 BFD_RELOC_MIPS_HIGHER);
4206 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
4207 macro_build (NULL, "dsll32", "d,w,<", reg, reg, 0);
4208 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
4209 *used_at = 1;
4210 }
4211 else
4212 {
67c0d1eb
RS
4213 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
4214 macro_build (ep, "daddiu", "t,r,j", reg, reg,
4215 BFD_RELOC_MIPS_HIGHER);
4216 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
4217 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
4218 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
4219 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 4220 }
6caf9ef4
TS
4221
4222 if (mips_relax.sequence)
4223 relax_end ();
d6bc6245 4224 }
252b5132
RH
4225 else
4226 {
d6bc6245 4227 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 4228 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 4229 {
4d7206a2 4230 relax_start (ep->X_add_symbol);
67c0d1eb 4231 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 4232 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 4233 relax_switch ();
d6bc6245 4234 }
67c0d1eb
RS
4235 macro_build_lui (ep, reg);
4236 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
4237 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
4238 if (mips_relax.sequence)
4239 relax_end ();
d6bc6245 4240 }
252b5132 4241 }
0a44bf69 4242 else if (!mips_big_got)
252b5132
RH
4243 {
4244 expressionS ex;
4245
4246 /* If this is a reference to an external symbol, we want
4247 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4248 Otherwise we want
4249 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4250 nop
4251 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
4252 If there is a constant, it must be added in after.
4253
ed6fb7bd 4254 If we have NewABI, we want
f5040a92
AO
4255 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
4256 unless we're referencing a global symbol with a non-zero
4257 offset, in which case cst must be added separately. */
ed6fb7bd
SC
4258 if (HAVE_NEWABI)
4259 {
f5040a92
AO
4260 if (ep->X_add_number)
4261 {
4d7206a2 4262 ex.X_add_number = ep->X_add_number;
f5040a92 4263 ep->X_add_number = 0;
4d7206a2 4264 relax_start (ep->X_add_symbol);
67c0d1eb
RS
4265 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4266 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
4267 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4268 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4269 ex.X_op = O_constant;
67c0d1eb 4270 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 4271 reg, reg, BFD_RELOC_LO16);
f5040a92 4272 ep->X_add_number = ex.X_add_number;
4d7206a2 4273 relax_switch ();
f5040a92 4274 }
67c0d1eb 4275 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 4276 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
4277 if (mips_relax.sequence)
4278 relax_end ();
ed6fb7bd
SC
4279 }
4280 else
4281 {
f5040a92
AO
4282 ex.X_add_number = ep->X_add_number;
4283 ep->X_add_number = 0;
67c0d1eb
RS
4284 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4285 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 4286 load_delay_nop ();
4d7206a2
RS
4287 relax_start (ep->X_add_symbol);
4288 relax_switch ();
67c0d1eb 4289 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 4290 BFD_RELOC_LO16);
4d7206a2 4291 relax_end ();
ed6fb7bd 4292
f5040a92
AO
4293 if (ex.X_add_number != 0)
4294 {
4295 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4296 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4297 ex.X_op = O_constant;
67c0d1eb 4298 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 4299 reg, reg, BFD_RELOC_LO16);
f5040a92 4300 }
252b5132
RH
4301 }
4302 }
0a44bf69 4303 else if (mips_big_got)
252b5132
RH
4304 {
4305 expressionS ex;
252b5132
RH
4306
4307 /* This is the large GOT case. If this is a reference to an
4308 external symbol, we want
4309 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4310 addu $reg,$reg,$gp
4311 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
4312
4313 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
4314 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4315 nop
4316 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 4317 If there is a constant, it must be added in after.
f5040a92
AO
4318
4319 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
4320 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
4321 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 4322 */
438c16b8
TS
4323 if (HAVE_NEWABI)
4324 {
4d7206a2 4325 ex.X_add_number = ep->X_add_number;
f5040a92 4326 ep->X_add_number = 0;
4d7206a2 4327 relax_start (ep->X_add_symbol);
67c0d1eb
RS
4328 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
4329 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
4330 reg, reg, mips_gp_register);
4331 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
4332 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
4333 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4334 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4335 else if (ex.X_add_number)
4336 {
4337 ex.X_op = O_constant;
67c0d1eb
RS
4338 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4339 BFD_RELOC_LO16);
f5040a92
AO
4340 }
4341
4342 ep->X_add_number = ex.X_add_number;
4d7206a2 4343 relax_switch ();
67c0d1eb 4344 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 4345 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
4346 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4347 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 4348 relax_end ();
438c16b8 4349 }
252b5132 4350 else
438c16b8 4351 {
f5040a92
AO
4352 ex.X_add_number = ep->X_add_number;
4353 ep->X_add_number = 0;
4d7206a2 4354 relax_start (ep->X_add_symbol);
67c0d1eb
RS
4355 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
4356 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
4357 reg, reg, mips_gp_register);
4358 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
4359 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
4360 relax_switch ();
4361 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
4362 {
4363 /* We need a nop before loading from $gp. This special
4364 check is required because the lui which starts the main
4365 instruction stream does not refer to $gp, and so will not
4366 insert the nop which may be required. */
67c0d1eb 4367 macro_build (NULL, "nop", "");
438c16b8 4368 }
67c0d1eb 4369 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 4370 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 4371 load_delay_nop ();
67c0d1eb 4372 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 4373 BFD_RELOC_LO16);
4d7206a2 4374 relax_end ();
438c16b8 4375
f5040a92
AO
4376 if (ex.X_add_number != 0)
4377 {
4378 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4379 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4380 ex.X_op = O_constant;
67c0d1eb
RS
4381 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4382 BFD_RELOC_LO16);
f5040a92 4383 }
252b5132
RH
4384 }
4385 }
252b5132
RH
4386 else
4387 abort ();
8fc2e39e
TS
4388
4389 if (mips_opts.noat && *used_at == 1)
4390 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
4391}
4392
ea1fb5dc
RS
4393/* Move the contents of register SOURCE into register DEST. */
4394
4395static void
67c0d1eb 4396move_register (int dest, int source)
ea1fb5dc 4397{
67c0d1eb
RS
4398 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
4399 dest, source, 0);
ea1fb5dc
RS
4400}
4401
4d7206a2 4402/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
4403 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
4404 The two alternatives are:
4d7206a2
RS
4405
4406 Global symbol Local sybmol
4407 ------------- ------------
4408 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
4409 ... ...
4410 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
4411
4412 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
4413 emits the second for a 16-bit offset or add_got_offset_hilo emits
4414 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
4415
4416static void
67c0d1eb 4417load_got_offset (int dest, expressionS *local)
4d7206a2
RS
4418{
4419 expressionS global;
4420
4421 global = *local;
4422 global.X_add_number = 0;
4423
4424 relax_start (local->X_add_symbol);
67c0d1eb
RS
4425 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4426 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 4427 relax_switch ();
67c0d1eb
RS
4428 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4429 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
4430 relax_end ();
4431}
4432
4433static void
67c0d1eb 4434add_got_offset (int dest, expressionS *local)
4d7206a2
RS
4435{
4436 expressionS global;
4437
4438 global.X_op = O_constant;
4439 global.X_op_symbol = NULL;
4440 global.X_add_symbol = NULL;
4441 global.X_add_number = local->X_add_number;
4442
4443 relax_start (local->X_add_symbol);
67c0d1eb 4444 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
4445 dest, dest, BFD_RELOC_LO16);
4446 relax_switch ();
67c0d1eb 4447 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
4448 relax_end ();
4449}
4450
f6a22291
MR
4451static void
4452add_got_offset_hilo (int dest, expressionS *local, int tmp)
4453{
4454 expressionS global;
4455 int hold_mips_optimize;
4456
4457 global.X_op = O_constant;
4458 global.X_op_symbol = NULL;
4459 global.X_add_symbol = NULL;
4460 global.X_add_number = local->X_add_number;
4461
4462 relax_start (local->X_add_symbol);
4463 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
4464 relax_switch ();
4465 /* Set mips_optimize around the lui instruction to avoid
4466 inserting an unnecessary nop after the lw. */
4467 hold_mips_optimize = mips_optimize;
4468 mips_optimize = 2;
4469 macro_build_lui (&global, tmp);
4470 mips_optimize = hold_mips_optimize;
4471 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
4472 relax_end ();
4473
4474 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
4475}
4476
252b5132
RH
4477/*
4478 * Build macros
4479 * This routine implements the seemingly endless macro or synthesized
4480 * instructions and addressing modes in the mips assembly language. Many
4481 * of these macros are simple and are similar to each other. These could
67c1ffbe 4482 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
4483 * this verbose method. Others are not simple macros but are more like
4484 * optimizing code generation.
4485 * One interesting optimization is when several store macros appear
67c1ffbe 4486 * consecutively that would load AT with the upper half of the same address.
252b5132
RH
4487 * The ensuing load upper instructions are ommited. This implies some kind
4488 * of global optimization. We currently only optimize within a single macro.
4489 * For many of the load and store macros if the address is specified as a
4490 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4491 * first load register 'at' with zero and use it as the base register. The
4492 * mips assembler simply uses register $zero. Just one tiny optimization
4493 * we're missing.
4494 */
4495static void
17a2f251 4496macro (struct mips_cl_insn *ip)
252b5132 4497{
3994f87e 4498 int treg, sreg, dreg, breg;
252b5132
RH
4499 int tempreg;
4500 int mask;
43841e91 4501 int used_at = 0;
252b5132
RH
4502 expressionS expr1;
4503 const char *s;
4504 const char *s2;
4505 const char *fmt;
4506 int likely = 0;
4507 int dbl = 0;
4508 int coproc = 0;
4509 int lr = 0;
4510 int imm = 0;
1abe91b1 4511 int call = 0;
252b5132 4512 int off;
67c0d1eb 4513 offsetT maxnum;
252b5132 4514 bfd_reloc_code_real_type r;
252b5132
RH
4515 int hold_mips_optimize;
4516
4517 assert (! mips_opts.mips16);
4518
4519 treg = (ip->insn_opcode >> 16) & 0x1f;
4520 dreg = (ip->insn_opcode >> 11) & 0x1f;
4521 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4522 mask = ip->insn_mo->mask;
4523
4524 expr1.X_op = O_constant;
4525 expr1.X_op_symbol = NULL;
4526 expr1.X_add_symbol = NULL;
4527 expr1.X_add_number = 1;
4528
4529 switch (mask)
4530 {
4531 case M_DABS:
4532 dbl = 1;
4533 case M_ABS:
4534 /* bgez $a0,.+12
4535 move v0,$a0
4536 sub v0,$zero,$a0
4537 */
4538
7d10b47d 4539 start_noreorder ();
252b5132
RH
4540
4541 expr1.X_add_number = 8;
67c0d1eb 4542 macro_build (&expr1, "bgez", "s,p", sreg);
252b5132 4543 if (dreg == sreg)
67c0d1eb 4544 macro_build (NULL, "nop", "", 0);
252b5132 4545 else
67c0d1eb
RS
4546 move_register (dreg, sreg);
4547 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
252b5132 4548
7d10b47d 4549 end_noreorder ();
8fc2e39e 4550 break;
252b5132
RH
4551
4552 case M_ADD_I:
4553 s = "addi";
4554 s2 = "add";
4555 goto do_addi;
4556 case M_ADDU_I:
4557 s = "addiu";
4558 s2 = "addu";
4559 goto do_addi;
4560 case M_DADD_I:
4561 dbl = 1;
4562 s = "daddi";
4563 s2 = "dadd";
4564 goto do_addi;
4565 case M_DADDU_I:
4566 dbl = 1;
4567 s = "daddiu";
4568 s2 = "daddu";
4569 do_addi:
4570 if (imm_expr.X_op == O_constant
4571 && imm_expr.X_add_number >= -0x8000
4572 && imm_expr.X_add_number < 0x8000)
4573 {
67c0d1eb 4574 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
8fc2e39e 4575 break;
252b5132 4576 }
8fc2e39e 4577 used_at = 1;
67c0d1eb
RS
4578 load_register (AT, &imm_expr, dbl);
4579 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
4580 break;
4581
4582 case M_AND_I:
4583 s = "andi";
4584 s2 = "and";
4585 goto do_bit;
4586 case M_OR_I:
4587 s = "ori";
4588 s2 = "or";
4589 goto do_bit;
4590 case M_NOR_I:
4591 s = "";
4592 s2 = "nor";
4593 goto do_bit;
4594 case M_XOR_I:
4595 s = "xori";
4596 s2 = "xor";
4597 do_bit:
4598 if (imm_expr.X_op == O_constant
4599 && imm_expr.X_add_number >= 0
4600 && imm_expr.X_add_number < 0x10000)
4601 {
4602 if (mask != M_NOR_I)
67c0d1eb 4603 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
252b5132
RH
4604 else
4605 {
67c0d1eb
RS
4606 macro_build (&imm_expr, "ori", "t,r,i",
4607 treg, sreg, BFD_RELOC_LO16);
4608 macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
252b5132 4609 }
8fc2e39e 4610 break;
252b5132
RH
4611 }
4612
8fc2e39e 4613 used_at = 1;
67c0d1eb
RS
4614 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4615 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
4616 break;
4617
4618 case M_BEQ_I:
4619 s = "beq";
4620 goto beq_i;
4621 case M_BEQL_I:
4622 s = "beql";
4623 likely = 1;
4624 goto beq_i;
4625 case M_BNE_I:
4626 s = "bne";
4627 goto beq_i;
4628 case M_BNEL_I:
4629 s = "bnel";
4630 likely = 1;
4631 beq_i:
4632 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4633 {
67c0d1eb 4634 macro_build (&offset_expr, s, "s,t,p", sreg, 0);
8fc2e39e 4635 break;
252b5132 4636 }
8fc2e39e 4637 used_at = 1;
67c0d1eb
RS
4638 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4639 macro_build (&offset_expr, s, "s,t,p", sreg, AT);
252b5132
RH
4640 break;
4641
4642 case M_BGEL:
4643 likely = 1;
4644 case M_BGE:
4645 if (treg == 0)
4646 {
67c0d1eb 4647 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
8fc2e39e 4648 break;
252b5132
RH
4649 }
4650 if (sreg == 0)
4651 {
67c0d1eb 4652 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", treg);
8fc2e39e 4653 break;
252b5132 4654 }
8fc2e39e 4655 used_at = 1;
67c0d1eb
RS
4656 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4657 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4658 break;
4659
4660 case M_BGTL_I:
4661 likely = 1;
4662 case M_BGT_I:
4663 /* check for > max integer */
4664 maxnum = 0x7fffffff;
ca4e0257 4665 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4666 {
4667 maxnum <<= 16;
4668 maxnum |= 0xffff;
4669 maxnum <<= 16;
4670 maxnum |= 0xffff;
4671 }
4672 if (imm_expr.X_op == O_constant
4673 && imm_expr.X_add_number >= maxnum
ca4e0257 4674 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4675 {
4676 do_false:
4677 /* result is always false */
4678 if (! likely)
67c0d1eb 4679 macro_build (NULL, "nop", "", 0);
252b5132 4680 else
67c0d1eb 4681 macro_build (&offset_expr, "bnel", "s,t,p", 0, 0);
8fc2e39e 4682 break;
252b5132
RH
4683 }
4684 if (imm_expr.X_op != O_constant)
4685 as_bad (_("Unsupported large constant"));
f9419b05 4686 ++imm_expr.X_add_number;
252b5132
RH
4687 /* FALLTHROUGH */
4688 case M_BGE_I:
4689 case M_BGEL_I:
4690 if (mask == M_BGEL_I)
4691 likely = 1;
4692 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4693 {
67c0d1eb 4694 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
8fc2e39e 4695 break;
252b5132
RH
4696 }
4697 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4698 {
67c0d1eb 4699 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
8fc2e39e 4700 break;
252b5132
RH
4701 }
4702 maxnum = 0x7fffffff;
ca4e0257 4703 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4704 {
4705 maxnum <<= 16;
4706 maxnum |= 0xffff;
4707 maxnum <<= 16;
4708 maxnum |= 0xffff;
4709 }
4710 maxnum = - maxnum - 1;
4711 if (imm_expr.X_op == O_constant
4712 && imm_expr.X_add_number <= maxnum
ca4e0257 4713 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4714 {
4715 do_true:
4716 /* result is always true */
4717 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
67c0d1eb 4718 macro_build (&offset_expr, "b", "p");
8fc2e39e 4719 break;
252b5132 4720 }
8fc2e39e 4721 used_at = 1;
67c0d1eb
RS
4722 set_at (sreg, 0);
4723 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4724 break;
4725
4726 case M_BGEUL:
4727 likely = 1;
4728 case M_BGEU:
4729 if (treg == 0)
4730 goto do_true;
4731 if (sreg == 0)
4732 {
67c0d1eb 4733 macro_build (&offset_expr, likely ? "beql" : "beq",
17a2f251 4734 "s,t,p", 0, treg);
8fc2e39e 4735 break;
252b5132 4736 }
8fc2e39e 4737 used_at = 1;
67c0d1eb
RS
4738 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4739 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4740 break;
4741
4742 case M_BGTUL_I:
4743 likely = 1;
4744 case M_BGTU_I:
4745 if (sreg == 0
ca4e0257 4746 || (HAVE_32BIT_GPRS
252b5132 4747 && imm_expr.X_op == O_constant
956cd1d6 4748 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4749 goto do_false;
4750 if (imm_expr.X_op != O_constant)
4751 as_bad (_("Unsupported large constant"));
f9419b05 4752 ++imm_expr.X_add_number;
252b5132
RH
4753 /* FALLTHROUGH */
4754 case M_BGEU_I:
4755 case M_BGEUL_I:
4756 if (mask == M_BGEUL_I)
4757 likely = 1;
4758 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4759 goto do_true;
4760 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4761 {
67c0d1eb 4762 macro_build (&offset_expr, likely ? "bnel" : "bne",
17a2f251 4763 "s,t,p", sreg, 0);
8fc2e39e 4764 break;
252b5132 4765 }
8fc2e39e 4766 used_at = 1;
67c0d1eb
RS
4767 set_at (sreg, 1);
4768 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4769 break;
4770
4771 case M_BGTL:
4772 likely = 1;
4773 case M_BGT:
4774 if (treg == 0)
4775 {
67c0d1eb 4776 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
8fc2e39e 4777 break;
252b5132
RH
4778 }
4779 if (sreg == 0)
4780 {
67c0d1eb 4781 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", treg);
8fc2e39e 4782 break;
252b5132 4783 }
8fc2e39e 4784 used_at = 1;
67c0d1eb
RS
4785 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4786 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4787 break;
4788
4789 case M_BGTUL:
4790 likely = 1;
4791 case M_BGTU:
4792 if (treg == 0)
4793 {
67c0d1eb 4794 macro_build (&offset_expr, likely ? "bnel" : "bne",
17a2f251 4795 "s,t,p", sreg, 0);
8fc2e39e 4796 break;
252b5132
RH
4797 }
4798 if (sreg == 0)
4799 goto do_false;
8fc2e39e 4800 used_at = 1;
67c0d1eb
RS
4801 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4802 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4803 break;
4804
4805 case M_BLEL:
4806 likely = 1;
4807 case M_BLE:
4808 if (treg == 0)
4809 {
67c0d1eb 4810 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
8fc2e39e 4811 break;
252b5132
RH
4812 }
4813 if (sreg == 0)
4814 {
67c0d1eb 4815 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", treg);
8fc2e39e 4816 break;
252b5132 4817 }
8fc2e39e 4818 used_at = 1;
67c0d1eb
RS
4819 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4820 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4821 break;
4822
4823 case M_BLEL_I:
4824 likely = 1;
4825 case M_BLE_I:
4826 maxnum = 0x7fffffff;
ca4e0257 4827 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4828 {
4829 maxnum <<= 16;
4830 maxnum |= 0xffff;
4831 maxnum <<= 16;
4832 maxnum |= 0xffff;
4833 }
4834 if (imm_expr.X_op == O_constant
4835 && imm_expr.X_add_number >= maxnum
ca4e0257 4836 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4837 goto do_true;
4838 if (imm_expr.X_op != O_constant)
4839 as_bad (_("Unsupported large constant"));
f9419b05 4840 ++imm_expr.X_add_number;
252b5132
RH
4841 /* FALLTHROUGH */
4842 case M_BLT_I:
4843 case M_BLTL_I:
4844 if (mask == M_BLTL_I)
4845 likely = 1;
4846 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4847 {
67c0d1eb 4848 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
8fc2e39e 4849 break;
252b5132
RH
4850 }
4851 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4852 {
67c0d1eb 4853 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
8fc2e39e 4854 break;
252b5132 4855 }
8fc2e39e 4856 used_at = 1;
67c0d1eb
RS
4857 set_at (sreg, 0);
4858 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4859 break;
4860
4861 case M_BLEUL:
4862 likely = 1;
4863 case M_BLEU:
4864 if (treg == 0)
4865 {
67c0d1eb 4866 macro_build (&offset_expr, likely ? "beql" : "beq",
17a2f251 4867 "s,t,p", sreg, 0);
8fc2e39e 4868 break;
252b5132
RH
4869 }
4870 if (sreg == 0)
4871 goto do_true;
8fc2e39e 4872 used_at = 1;
67c0d1eb
RS
4873 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4874 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4875 break;
4876
4877 case M_BLEUL_I:
4878 likely = 1;
4879 case M_BLEU_I:
4880 if (sreg == 0
ca4e0257 4881 || (HAVE_32BIT_GPRS
252b5132 4882 && imm_expr.X_op == O_constant
956cd1d6 4883 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4884 goto do_true;
4885 if (imm_expr.X_op != O_constant)
4886 as_bad (_("Unsupported large constant"));
f9419b05 4887 ++imm_expr.X_add_number;
252b5132
RH
4888 /* FALLTHROUGH */
4889 case M_BLTU_I:
4890 case M_BLTUL_I:
4891 if (mask == M_BLTUL_I)
4892 likely = 1;
4893 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4894 goto do_false;
4895 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4896 {
67c0d1eb 4897 macro_build (&offset_expr, likely ? "beql" : "beq",
252b5132 4898 "s,t,p", sreg, 0);
8fc2e39e 4899 break;
252b5132 4900 }
8fc2e39e 4901 used_at = 1;
67c0d1eb
RS
4902 set_at (sreg, 1);
4903 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4904 break;
4905
4906 case M_BLTL:
4907 likely = 1;
4908 case M_BLT:
4909 if (treg == 0)
4910 {
67c0d1eb 4911 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
8fc2e39e 4912 break;
252b5132
RH
4913 }
4914 if (sreg == 0)
4915 {
67c0d1eb 4916 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", treg);
8fc2e39e 4917 break;
252b5132 4918 }
8fc2e39e 4919 used_at = 1;
67c0d1eb
RS
4920 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4921 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4922 break;
4923
4924 case M_BLTUL:
4925 likely = 1;
4926 case M_BLTU:
4927 if (treg == 0)
4928 goto do_false;
4929 if (sreg == 0)
4930 {
67c0d1eb 4931 macro_build (&offset_expr, likely ? "bnel" : "bne",
17a2f251 4932 "s,t,p", 0, treg);
8fc2e39e 4933 break;
252b5132 4934 }
8fc2e39e 4935 used_at = 1;
67c0d1eb
RS
4936 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4937 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4938 break;
4939
5f74bc13
CD
4940 case M_DEXT:
4941 {
4942 unsigned long pos;
4943 unsigned long size;
4944
4945 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4946 {
4947 as_bad (_("Unsupported large constant"));
4948 pos = size = 1;
4949 }
4950 else
4951 {
4952 pos = (unsigned long) imm_expr.X_add_number;
4953 size = (unsigned long) imm2_expr.X_add_number;
4954 }
4955
4956 if (pos > 63)
4957 {
4958 as_bad (_("Improper position (%lu)"), pos);
4959 pos = 1;
4960 }
4961 if (size == 0 || size > 64
4962 || (pos + size - 1) > 63)
4963 {
4964 as_bad (_("Improper extract size (%lu, position %lu)"),
4965 size, pos);
4966 size = 1;
4967 }
4968
4969 if (size <= 32 && pos < 32)
4970 {
4971 s = "dext";
4972 fmt = "t,r,+A,+C";
4973 }
4974 else if (size <= 32)
4975 {
4976 s = "dextu";
4977 fmt = "t,r,+E,+H";
4978 }
4979 else
4980 {
4981 s = "dextm";
4982 fmt = "t,r,+A,+G";
4983 }
67c0d1eb 4984 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos, size - 1);
5f74bc13 4985 }
8fc2e39e 4986 break;
5f74bc13
CD
4987
4988 case M_DINS:
4989 {
4990 unsigned long pos;
4991 unsigned long size;
4992
4993 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4994 {
4995 as_bad (_("Unsupported large constant"));
4996 pos = size = 1;
4997 }
4998 else
4999 {
5000 pos = (unsigned long) imm_expr.X_add_number;
5001 size = (unsigned long) imm2_expr.X_add_number;
5002 }
5003
5004 if (pos > 63)
5005 {
5006 as_bad (_("Improper position (%lu)"), pos);
5007 pos = 1;
5008 }
5009 if (size == 0 || size > 64
5010 || (pos + size - 1) > 63)
5011 {
5012 as_bad (_("Improper insert size (%lu, position %lu)"),
5013 size, pos);
5014 size = 1;
5015 }
5016
5017 if (pos < 32 && (pos + size - 1) < 32)
5018 {
5019 s = "dins";
5020 fmt = "t,r,+A,+B";
5021 }
5022 else if (pos >= 32)
5023 {
5024 s = "dinsu";
5025 fmt = "t,r,+E,+F";
5026 }
5027 else
5028 {
5029 s = "dinsm";
5030 fmt = "t,r,+A,+F";
5031 }
67c0d1eb
RS
5032 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos,
5033 pos + size - 1);
5f74bc13 5034 }
8fc2e39e 5035 break;
5f74bc13 5036
252b5132
RH
5037 case M_DDIV_3:
5038 dbl = 1;
5039 case M_DIV_3:
5040 s = "mflo";
5041 goto do_div3;
5042 case M_DREM_3:
5043 dbl = 1;
5044 case M_REM_3:
5045 s = "mfhi";
5046 do_div3:
5047 if (treg == 0)
5048 {
5049 as_warn (_("Divide by zero."));
5050 if (mips_trap)
67c0d1eb 5051 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
252b5132 5052 else
67c0d1eb 5053 macro_build (NULL, "break", "c", 7);
8fc2e39e 5054 break;
252b5132
RH
5055 }
5056
7d10b47d 5057 start_noreorder ();
252b5132
RH
5058 if (mips_trap)
5059 {
67c0d1eb
RS
5060 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
5061 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
252b5132
RH
5062 }
5063 else
5064 {
5065 expr1.X_add_number = 8;
67c0d1eb
RS
5066 macro_build (&expr1, "bne", "s,t,p", treg, 0);
5067 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
5068 macro_build (NULL, "break", "c", 7);
252b5132
RH
5069 }
5070 expr1.X_add_number = -1;
8fc2e39e 5071 used_at = 1;
f6a22291 5072 load_register (AT, &expr1, dbl);
252b5132 5073 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
67c0d1eb 5074 macro_build (&expr1, "bne", "s,t,p", treg, AT);
252b5132
RH
5075 if (dbl)
5076 {
5077 expr1.X_add_number = 1;
f6a22291 5078 load_register (AT, &expr1, dbl);
67c0d1eb 5079 macro_build (NULL, "dsll32", "d,w,<", AT, AT, 31);
252b5132
RH
5080 }
5081 else
5082 {
5083 expr1.X_add_number = 0x80000000;
67c0d1eb 5084 macro_build (&expr1, "lui", "t,u", AT, BFD_RELOC_HI16);
252b5132
RH
5085 }
5086 if (mips_trap)
5087 {
67c0d1eb 5088 macro_build (NULL, "teq", "s,t,q", sreg, AT, 6);
252b5132
RH
5089 /* We want to close the noreorder block as soon as possible, so
5090 that later insns are available for delay slot filling. */
7d10b47d 5091 end_noreorder ();
252b5132
RH
5092 }
5093 else
5094 {
5095 expr1.X_add_number = 8;
67c0d1eb
RS
5096 macro_build (&expr1, "bne", "s,t,p", sreg, AT);
5097 macro_build (NULL, "nop", "", 0);
252b5132
RH
5098
5099 /* We want to close the noreorder block as soon as possible, so
5100 that later insns are available for delay slot filling. */
7d10b47d 5101 end_noreorder ();
252b5132 5102
67c0d1eb 5103 macro_build (NULL, "break", "c", 6);
252b5132 5104 }
67c0d1eb 5105 macro_build (NULL, s, "d", dreg);
252b5132
RH
5106 break;
5107
5108 case M_DIV_3I:
5109 s = "div";
5110 s2 = "mflo";
5111 goto do_divi;
5112 case M_DIVU_3I:
5113 s = "divu";
5114 s2 = "mflo";
5115 goto do_divi;
5116 case M_REM_3I:
5117 s = "div";
5118 s2 = "mfhi";
5119 goto do_divi;
5120 case M_REMU_3I:
5121 s = "divu";
5122 s2 = "mfhi";
5123 goto do_divi;
5124 case M_DDIV_3I:
5125 dbl = 1;
5126 s = "ddiv";
5127 s2 = "mflo";
5128 goto do_divi;
5129 case M_DDIVU_3I:
5130 dbl = 1;
5131 s = "ddivu";
5132 s2 = "mflo";
5133 goto do_divi;
5134 case M_DREM_3I:
5135 dbl = 1;
5136 s = "ddiv";
5137 s2 = "mfhi";
5138 goto do_divi;
5139 case M_DREMU_3I:
5140 dbl = 1;
5141 s = "ddivu";
5142 s2 = "mfhi";
5143 do_divi:
5144 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
5145 {
5146 as_warn (_("Divide by zero."));
5147 if (mips_trap)
67c0d1eb 5148 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
252b5132 5149 else
67c0d1eb 5150 macro_build (NULL, "break", "c", 7);
8fc2e39e 5151 break;
252b5132
RH
5152 }
5153 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
5154 {
5155 if (strcmp (s2, "mflo") == 0)
67c0d1eb 5156 move_register (dreg, sreg);
252b5132 5157 else
67c0d1eb 5158 move_register (dreg, 0);
8fc2e39e 5159 break;
252b5132
RH
5160 }
5161 if (imm_expr.X_op == O_constant
5162 && imm_expr.X_add_number == -1
5163 && s[strlen (s) - 1] != 'u')
5164 {
5165 if (strcmp (s2, "mflo") == 0)
5166 {
67c0d1eb 5167 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
252b5132
RH
5168 }
5169 else
67c0d1eb 5170 move_register (dreg, 0);
8fc2e39e 5171 break;
252b5132
RH
5172 }
5173
8fc2e39e 5174 used_at = 1;
67c0d1eb
RS
5175 load_register (AT, &imm_expr, dbl);
5176 macro_build (NULL, s, "z,s,t", sreg, AT);
5177 macro_build (NULL, s2, "d", dreg);
252b5132
RH
5178 break;
5179
5180 case M_DIVU_3:
5181 s = "divu";
5182 s2 = "mflo";
5183 goto do_divu3;
5184 case M_REMU_3:
5185 s = "divu";
5186 s2 = "mfhi";
5187 goto do_divu3;
5188 case M_DDIVU_3:
5189 s = "ddivu";
5190 s2 = "mflo";
5191 goto do_divu3;
5192 case M_DREMU_3:
5193 s = "ddivu";
5194 s2 = "mfhi";
5195 do_divu3:
7d10b47d 5196 start_noreorder ();
252b5132
RH
5197 if (mips_trap)
5198 {
67c0d1eb
RS
5199 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
5200 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
5201 /* We want to close the noreorder block as soon as possible, so
5202 that later insns are available for delay slot filling. */
7d10b47d 5203 end_noreorder ();
252b5132
RH
5204 }
5205 else
5206 {
5207 expr1.X_add_number = 8;
67c0d1eb
RS
5208 macro_build (&expr1, "bne", "s,t,p", treg, 0);
5209 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
5210
5211 /* We want to close the noreorder block as soon as possible, so
5212 that later insns are available for delay slot filling. */
7d10b47d 5213 end_noreorder ();
67c0d1eb 5214 macro_build (NULL, "break", "c", 7);
252b5132 5215 }
67c0d1eb 5216 macro_build (NULL, s2, "d", dreg);
8fc2e39e 5217 break;
252b5132 5218
1abe91b1
MR
5219 case M_DLCA_AB:
5220 dbl = 1;
5221 case M_LCA_AB:
5222 call = 1;
5223 goto do_la;
252b5132
RH
5224 case M_DLA_AB:
5225 dbl = 1;
5226 case M_LA_AB:
1abe91b1 5227 do_la:
252b5132
RH
5228 /* Load the address of a symbol into a register. If breg is not
5229 zero, we then add a base register to it. */
5230
3bec30a8
TS
5231 if (dbl && HAVE_32BIT_GPRS)
5232 as_warn (_("dla used to load 32-bit register"));
5233
c90bbe5b 5234 if (! dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
5235 as_warn (_("la used to load 64-bit address"));
5236
0c11417f
MR
5237 if (offset_expr.X_op == O_constant
5238 && offset_expr.X_add_number >= -0x8000
5239 && offset_expr.X_add_number < 0x8000)
5240 {
aed1a261 5241 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
17a2f251 5242 "t,r,j", treg, sreg, BFD_RELOC_LO16);
8fc2e39e 5243 break;
0c11417f
MR
5244 }
5245
b8285c27 5246 if (!mips_opts.noat && (treg == breg))
afdbd6d0
CD
5247 {
5248 tempreg = AT;
5249 used_at = 1;
5250 }
5251 else
5252 {
5253 tempreg = treg;
afdbd6d0
CD
5254 }
5255
252b5132
RH
5256 if (offset_expr.X_op != O_symbol
5257 && offset_expr.X_op != O_constant)
5258 {
5259 as_bad (_("expression too complex"));
5260 offset_expr.X_op = O_constant;
5261 }
5262
252b5132 5263 if (offset_expr.X_op == O_constant)
aed1a261 5264 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
5265 else if (mips_pic == NO_PIC)
5266 {
d6bc6245 5267 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 5268 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
5269 Otherwise we want
5270 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5271 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5272 If we have a constant, we need two instructions anyhow,
d6bc6245 5273 so we may as well always use the latter form.
76b3015f 5274
6caf9ef4
TS
5275 With 64bit address space and a usable $at we want
5276 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5277 lui $at,<sym> (BFD_RELOC_HI16_S)
5278 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5279 daddiu $at,<sym> (BFD_RELOC_LO16)
5280 dsll32 $tempreg,0
5281 daddu $tempreg,$tempreg,$at
5282
5283 If $at is already in use, we use a path which is suboptimal
5284 on superscalar processors.
5285 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5286 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5287 dsll $tempreg,16
5288 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5289 dsll $tempreg,16
5290 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
5291
5292 For GP relative symbols in 64bit address space we can use
5293 the same sequence as in 32bit address space. */
aed1a261 5294 if (HAVE_64BIT_SYMBOLS)
252b5132 5295 {
6caf9ef4
TS
5296 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5297 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5298 {
5299 relax_start (offset_expr.X_add_symbol);
5300 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5301 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
5302 relax_switch ();
5303 }
d6bc6245 5304
b8285c27 5305 if (used_at == 0 && !mips_opts.noat)
98d3f06f 5306 {
67c0d1eb 5307 macro_build (&offset_expr, "lui", "t,u",
17a2f251 5308 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 5309 macro_build (&offset_expr, "lui", "t,u",
17a2f251 5310 AT, BFD_RELOC_HI16_S);
67c0d1eb 5311 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 5312 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 5313 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 5314 AT, AT, BFD_RELOC_LO16);
67c0d1eb
RS
5315 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
5316 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
5317 used_at = 1;
5318 }
5319 else
5320 {
67c0d1eb 5321 macro_build (&offset_expr, "lui", "t,u",
17a2f251 5322 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 5323 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 5324 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb
RS
5325 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5326 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 5327 tempreg, tempreg, BFD_RELOC_HI16_S);
67c0d1eb
RS
5328 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5329 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 5330 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 5331 }
6caf9ef4
TS
5332
5333 if (mips_relax.sequence)
5334 relax_end ();
98d3f06f
KH
5335 }
5336 else
5337 {
5338 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 5339 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 5340 {
4d7206a2 5341 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5342 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5343 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 5344 relax_switch ();
98d3f06f 5345 }
6943caf0
ILT
5346 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
5347 as_bad (_("offset too large"));
67c0d1eb
RS
5348 macro_build_lui (&offset_expr, tempreg);
5349 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5350 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
5351 if (mips_relax.sequence)
5352 relax_end ();
98d3f06f 5353 }
252b5132 5354 }
0a44bf69 5355 else if (!mips_big_got && !HAVE_NEWABI)
252b5132 5356 {
9117d219
NC
5357 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5358
252b5132
RH
5359 /* If this is a reference to an external symbol, and there
5360 is no constant, we want
5361 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 5362 or for lca or if tempreg is PIC_CALL_REG
9117d219 5363 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
5364 For a local symbol, we want
5365 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5366 nop
5367 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5368
5369 If we have a small constant, and this is a reference to
5370 an external symbol, we want
5371 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5372 nop
5373 addiu $tempreg,$tempreg,<constant>
5374 For a local symbol, we want the same instruction
5375 sequence, but we output a BFD_RELOC_LO16 reloc on the
5376 addiu instruction.
5377
5378 If we have a large constant, and this is a reference to
5379 an external symbol, we want
5380 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5381 lui $at,<hiconstant>
5382 addiu $at,$at,<loconstant>
5383 addu $tempreg,$tempreg,$at
5384 For a local symbol, we want the same instruction
5385 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 5386 addiu instruction.
ed6fb7bd
SC
5387 */
5388
4d7206a2 5389 if (offset_expr.X_add_number == 0)
252b5132 5390 {
0a44bf69
RS
5391 if (mips_pic == SVR4_PIC
5392 && breg == 0
5393 && (call || tempreg == PIC_CALL_REG))
4d7206a2
RS
5394 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5395
5396 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5397 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5398 lw_reloc_type, mips_gp_register);
4d7206a2 5399 if (breg != 0)
252b5132
RH
5400 {
5401 /* We're going to put in an addu instruction using
5402 tempreg, so we may as well insert the nop right
5403 now. */
269137b2 5404 load_delay_nop ();
252b5132 5405 }
4d7206a2 5406 relax_switch ();
67c0d1eb
RS
5407 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5408 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 5409 load_delay_nop ();
67c0d1eb
RS
5410 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5411 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 5412 relax_end ();
252b5132
RH
5413 /* FIXME: If breg == 0, and the next instruction uses
5414 $tempreg, then if this variant case is used an extra
5415 nop will be generated. */
5416 }
4d7206a2
RS
5417 else if (offset_expr.X_add_number >= -0x8000
5418 && offset_expr.X_add_number < 0x8000)
252b5132 5419 {
67c0d1eb 5420 load_got_offset (tempreg, &offset_expr);
269137b2 5421 load_delay_nop ();
67c0d1eb 5422 add_got_offset (tempreg, &offset_expr);
252b5132
RH
5423 }
5424 else
5425 {
4d7206a2
RS
5426 expr1.X_add_number = offset_expr.X_add_number;
5427 offset_expr.X_add_number =
5428 ((offset_expr.X_add_number + 0x8000) & 0xffff) - 0x8000;
67c0d1eb 5429 load_got_offset (tempreg, &offset_expr);
f6a22291 5430 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
5431 /* If we are going to add in a base register, and the
5432 target register and the base register are the same,
5433 then we are using AT as a temporary register. Since
5434 we want to load the constant into AT, we add our
5435 current AT (from the global offset table) and the
5436 register into the register now, and pretend we were
5437 not using a base register. */
67c0d1eb 5438 if (breg == treg)
252b5132 5439 {
269137b2 5440 load_delay_nop ();
67c0d1eb 5441 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5442 treg, AT, breg);
252b5132
RH
5443 breg = 0;
5444 tempreg = treg;
252b5132 5445 }
f6a22291 5446 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
5447 used_at = 1;
5448 }
5449 }
0a44bf69 5450 else if (!mips_big_got && HAVE_NEWABI)
f5040a92 5451 {
67c0d1eb 5452 int add_breg_early = 0;
f5040a92
AO
5453
5454 /* If this is a reference to an external, and there is no
5455 constant, or local symbol (*), with or without a
5456 constant, we want
5457 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 5458 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
5459 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5460
5461 If we have a small constant, and this is a reference to
5462 an external symbol, we want
5463 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5464 addiu $tempreg,$tempreg,<constant>
5465
5466 If we have a large constant, and this is a reference to
5467 an external symbol, we want
5468 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5469 lui $at,<hiconstant>
5470 addiu $at,$at,<loconstant>
5471 addu $tempreg,$tempreg,$at
5472
5473 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5474 local symbols, even though it introduces an additional
5475 instruction. */
5476
f5040a92
AO
5477 if (offset_expr.X_add_number)
5478 {
4d7206a2 5479 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
5480 offset_expr.X_add_number = 0;
5481
4d7206a2 5482 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5483 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5484 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
5485
5486 if (expr1.X_add_number >= -0x8000
5487 && expr1.X_add_number < 0x8000)
5488 {
67c0d1eb
RS
5489 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5490 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 5491 }
ecd13cd3 5492 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92
AO
5493 {
5494 int dreg;
5495
5496 /* If we are going to add in a base register, and the
5497 target register and the base register are the same,
5498 then we are using AT as a temporary register. Since
5499 we want to load the constant into AT, we add our
5500 current AT (from the global offset table) and the
5501 register into the register now, and pretend we were
5502 not using a base register. */
5503 if (breg != treg)
5504 dreg = tempreg;
5505 else
5506 {
5507 assert (tempreg == AT);
67c0d1eb
RS
5508 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5509 treg, AT, breg);
f5040a92 5510 dreg = treg;
67c0d1eb 5511 add_breg_early = 1;
f5040a92
AO
5512 }
5513
f6a22291 5514 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 5515 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5516 dreg, dreg, AT);
f5040a92 5517
f5040a92
AO
5518 used_at = 1;
5519 }
5520 else
5521 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5522
4d7206a2 5523 relax_switch ();
f5040a92
AO
5524 offset_expr.X_add_number = expr1.X_add_number;
5525
67c0d1eb
RS
5526 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5527 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5528 if (add_breg_early)
f5040a92 5529 {
67c0d1eb 5530 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
f899b4b8 5531 treg, tempreg, breg);
f5040a92
AO
5532 breg = 0;
5533 tempreg = treg;
5534 }
4d7206a2 5535 relax_end ();
f5040a92 5536 }
4d7206a2 5537 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 5538 {
4d7206a2 5539 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5540 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5541 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 5542 relax_switch ();
67c0d1eb
RS
5543 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5544 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 5545 relax_end ();
f5040a92 5546 }
4d7206a2 5547 else
f5040a92 5548 {
67c0d1eb
RS
5549 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5550 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
5551 }
5552 }
0a44bf69 5553 else if (mips_big_got && !HAVE_NEWABI)
252b5132 5554 {
67c0d1eb 5555 int gpdelay;
9117d219
NC
5556 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5557 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 5558 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
5559
5560 /* This is the large GOT case. If this is a reference to an
5561 external symbol, and there is no constant, we want
5562 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5563 addu $tempreg,$tempreg,$gp
5564 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 5565 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
5566 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5567 addu $tempreg,$tempreg,$gp
5568 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
5569 For a local symbol, we want
5570 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5571 nop
5572 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5573
5574 If we have a small constant, and this is a reference to
5575 an external symbol, we want
5576 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5577 addu $tempreg,$tempreg,$gp
5578 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5579 nop
5580 addiu $tempreg,$tempreg,<constant>
5581 For a local symbol, we want
5582 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5583 nop
5584 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5585
5586 If we have a large constant, and this is a reference to
5587 an external symbol, we want
5588 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5589 addu $tempreg,$tempreg,$gp
5590 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5591 lui $at,<hiconstant>
5592 addiu $at,$at,<loconstant>
5593 addu $tempreg,$tempreg,$at
5594 For a local symbol, we want
5595 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5596 lui $at,<hiconstant>
5597 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5598 addu $tempreg,$tempreg,$at
f5040a92 5599 */
438c16b8 5600
252b5132
RH
5601 expr1.X_add_number = offset_expr.X_add_number;
5602 offset_expr.X_add_number = 0;
4d7206a2 5603 relax_start (offset_expr.X_add_symbol);
67c0d1eb 5604 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
5605 if (expr1.X_add_number == 0 && breg == 0
5606 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
5607 {
5608 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5609 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5610 }
67c0d1eb
RS
5611 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5612 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5613 tempreg, tempreg, mips_gp_register);
67c0d1eb 5614 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 5615 tempreg, lw_reloc_type, tempreg);
252b5132
RH
5616 if (expr1.X_add_number == 0)
5617 {
67c0d1eb 5618 if (breg != 0)
252b5132
RH
5619 {
5620 /* We're going to put in an addu instruction using
5621 tempreg, so we may as well insert the nop right
5622 now. */
269137b2 5623 load_delay_nop ();
252b5132 5624 }
252b5132
RH
5625 }
5626 else if (expr1.X_add_number >= -0x8000
5627 && expr1.X_add_number < 0x8000)
5628 {
269137b2 5629 load_delay_nop ();
67c0d1eb 5630 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5631 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
5632 }
5633 else
5634 {
67c0d1eb 5635 int dreg;
252b5132
RH
5636
5637 /* If we are going to add in a base register, and the
5638 target register and the base register are the same,
5639 then we are using AT as a temporary register. Since
5640 we want to load the constant into AT, we add our
5641 current AT (from the global offset table) and the
5642 register into the register now, and pretend we were
5643 not using a base register. */
5644 if (breg != treg)
67c0d1eb 5645 dreg = tempreg;
252b5132
RH
5646 else
5647 {
5648 assert (tempreg == AT);
269137b2 5649 load_delay_nop ();
67c0d1eb 5650 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5651 treg, AT, breg);
252b5132 5652 dreg = treg;
252b5132
RH
5653 }
5654
f6a22291 5655 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 5656 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 5657
252b5132
RH
5658 used_at = 1;
5659 }
4d7206a2
RS
5660 offset_expr.X_add_number =
5661 ((expr1.X_add_number + 0x8000) & 0xffff) - 0x8000;
5662 relax_switch ();
252b5132 5663
67c0d1eb 5664 if (gpdelay)
252b5132
RH
5665 {
5666 /* This is needed because this instruction uses $gp, but
f5040a92 5667 the first instruction on the main stream does not. */
67c0d1eb 5668 macro_build (NULL, "nop", "");
252b5132 5669 }
ed6fb7bd 5670
67c0d1eb
RS
5671 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5672 local_reloc_type, mips_gp_register);
f5040a92 5673 if (expr1.X_add_number >= -0x8000
252b5132
RH
5674 && expr1.X_add_number < 0x8000)
5675 {
269137b2 5676 load_delay_nop ();
67c0d1eb
RS
5677 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5678 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 5679 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
5680 register, the external symbol case ended with a load,
5681 so if the symbol turns out to not be external, and
5682 the next instruction uses tempreg, an unnecessary nop
5683 will be inserted. */
252b5132
RH
5684 }
5685 else
5686 {
5687 if (breg == treg)
5688 {
5689 /* We must add in the base register now, as in the
f5040a92 5690 external symbol case. */
252b5132 5691 assert (tempreg == AT);
269137b2 5692 load_delay_nop ();
67c0d1eb 5693 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5694 treg, AT, breg);
252b5132
RH
5695 tempreg = treg;
5696 /* We set breg to 0 because we have arranged to add
f5040a92 5697 it in in both cases. */
252b5132
RH
5698 breg = 0;
5699 }
5700
67c0d1eb
RS
5701 macro_build_lui (&expr1, AT);
5702 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5703 AT, AT, BFD_RELOC_LO16);
67c0d1eb 5704 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5705 tempreg, tempreg, AT);
8fc2e39e 5706 used_at = 1;
252b5132 5707 }
4d7206a2 5708 relax_end ();
252b5132 5709 }
0a44bf69 5710 else if (mips_big_got && HAVE_NEWABI)
f5040a92 5711 {
f5040a92
AO
5712 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5713 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 5714 int add_breg_early = 0;
f5040a92
AO
5715
5716 /* This is the large GOT case. If this is a reference to an
5717 external symbol, and there is no constant, we want
5718 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5719 add $tempreg,$tempreg,$gp
5720 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 5721 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
5722 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5723 add $tempreg,$tempreg,$gp
5724 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5725
5726 If we have a small constant, and this is a reference to
5727 an external symbol, we want
5728 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5729 add $tempreg,$tempreg,$gp
5730 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5731 addi $tempreg,$tempreg,<constant>
5732
5733 If we have a large constant, and this is a reference to
5734 an external symbol, we want
5735 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5736 addu $tempreg,$tempreg,$gp
5737 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5738 lui $at,<hiconstant>
5739 addi $at,$at,<loconstant>
5740 add $tempreg,$tempreg,$at
5741
5742 If we have NewABI, and we know it's a local symbol, we want
5743 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5744 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5745 otherwise we have to resort to GOT_HI16/GOT_LO16. */
5746
4d7206a2 5747 relax_start (offset_expr.X_add_symbol);
f5040a92 5748
4d7206a2 5749 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
5750 offset_expr.X_add_number = 0;
5751
1abe91b1
MR
5752 if (expr1.X_add_number == 0 && breg == 0
5753 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
5754 {
5755 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5756 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5757 }
67c0d1eb
RS
5758 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5759 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5760 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
5761 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5762 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
5763
5764 if (expr1.X_add_number == 0)
4d7206a2 5765 ;
f5040a92
AO
5766 else if (expr1.X_add_number >= -0x8000
5767 && expr1.X_add_number < 0x8000)
5768 {
67c0d1eb 5769 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5770 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 5771 }
ecd13cd3 5772 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92
AO
5773 {
5774 int dreg;
5775
5776 /* If we are going to add in a base register, and the
5777 target register and the base register are the same,
5778 then we are using AT as a temporary register. Since
5779 we want to load the constant into AT, we add our
5780 current AT (from the global offset table) and the
5781 register into the register now, and pretend we were
5782 not using a base register. */
5783 if (breg != treg)
5784 dreg = tempreg;
5785 else
5786 {
5787 assert (tempreg == AT);
67c0d1eb 5788 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5789 treg, AT, breg);
f5040a92 5790 dreg = treg;
67c0d1eb 5791 add_breg_early = 1;
f5040a92
AO
5792 }
5793
f6a22291 5794 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 5795 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 5796
f5040a92
AO
5797 used_at = 1;
5798 }
5799 else
5800 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5801
4d7206a2 5802 relax_switch ();
f5040a92 5803 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
5804 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5805 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5806 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
5807 tempreg, BFD_RELOC_MIPS_GOT_OFST);
5808 if (add_breg_early)
f5040a92 5809 {
67c0d1eb 5810 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5811 treg, tempreg, breg);
f5040a92
AO
5812 breg = 0;
5813 tempreg = treg;
5814 }
4d7206a2 5815 relax_end ();
f5040a92 5816 }
252b5132
RH
5817 else
5818 abort ();
5819
5820 if (breg != 0)
aed1a261 5821 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
252b5132
RH
5822 break;
5823
5824 case M_J_A:
5825 /* The j instruction may not be used in PIC code, since it
5826 requires an absolute address. We convert it to a b
5827 instruction. */
5828 if (mips_pic == NO_PIC)
67c0d1eb 5829 macro_build (&offset_expr, "j", "a");
252b5132 5830 else
67c0d1eb 5831 macro_build (&offset_expr, "b", "p");
8fc2e39e 5832 break;
252b5132
RH
5833
5834 /* The jal instructions must be handled as macros because when
5835 generating PIC code they expand to multi-instruction
5836 sequences. Normally they are simple instructions. */
5837 case M_JAL_1:
5838 dreg = RA;
5839 /* Fall through. */
5840 case M_JAL_2:
3e722fb5 5841 if (mips_pic == NO_PIC)
67c0d1eb 5842 macro_build (NULL, "jalr", "d,s", dreg, sreg);
0a44bf69 5843 else
252b5132
RH
5844 {
5845 if (sreg != PIC_CALL_REG)
5846 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 5847
67c0d1eb 5848 macro_build (NULL, "jalr", "d,s", dreg, sreg);
0a44bf69 5849 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
252b5132 5850 {
6478892d
TS
5851 if (mips_cprestore_offset < 0)
5852 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5853 else
5854 {
7a621144
DJ
5855 if (! mips_frame_reg_valid)
5856 {
5857 as_warn (_("No .frame pseudo-op used in PIC code"));
5858 /* Quiet this warning. */
5859 mips_frame_reg_valid = 1;
5860 }
5861 if (! mips_cprestore_valid)
5862 {
5863 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5864 /* Quiet this warning. */
5865 mips_cprestore_valid = 1;
5866 }
6478892d 5867 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 5868 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 5869 mips_gp_register,
256ab948
TS
5870 mips_frame_reg,
5871 HAVE_64BIT_ADDRESSES);
6478892d 5872 }
252b5132
RH
5873 }
5874 }
252b5132 5875
8fc2e39e 5876 break;
252b5132
RH
5877
5878 case M_JAL_A:
5879 if (mips_pic == NO_PIC)
67c0d1eb 5880 macro_build (&offset_expr, "jal", "a");
252b5132
RH
5881 else if (mips_pic == SVR4_PIC)
5882 {
5883 /* If this is a reference to an external symbol, and we are
5884 using a small GOT, we want
5885 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5886 nop
f9419b05 5887 jalr $ra,$25
252b5132
RH
5888 nop
5889 lw $gp,cprestore($sp)
5890 The cprestore value is set using the .cprestore
5891 pseudo-op. If we are using a big GOT, we want
5892 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5893 addu $25,$25,$gp
5894 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5895 nop
f9419b05 5896 jalr $ra,$25
252b5132
RH
5897 nop
5898 lw $gp,cprestore($sp)
5899 If the symbol is not external, we want
5900 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5901 nop
5902 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 5903 jalr $ra,$25
252b5132 5904 nop
438c16b8 5905 lw $gp,cprestore($sp)
f5040a92
AO
5906
5907 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5908 sequences above, minus nops, unless the symbol is local,
5909 which enables us to use GOT_PAGE/GOT_OFST (big got) or
5910 GOT_DISP. */
438c16b8 5911 if (HAVE_NEWABI)
252b5132 5912 {
f5040a92
AO
5913 if (! mips_big_got)
5914 {
4d7206a2 5915 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5916 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5917 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 5918 mips_gp_register);
4d7206a2 5919 relax_switch ();
67c0d1eb
RS
5920 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5921 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
5922 mips_gp_register);
5923 relax_end ();
f5040a92
AO
5924 }
5925 else
5926 {
4d7206a2 5927 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5928 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5929 BFD_RELOC_MIPS_CALL_HI16);
5930 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5931 PIC_CALL_REG, mips_gp_register);
5932 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5933 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5934 PIC_CALL_REG);
4d7206a2 5935 relax_switch ();
67c0d1eb
RS
5936 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5937 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
5938 mips_gp_register);
5939 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5940 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 5941 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 5942 relax_end ();
f5040a92 5943 }
684022ea 5944
67c0d1eb 5945 macro_build_jalr (&offset_expr);
252b5132
RH
5946 }
5947 else
5948 {
4d7206a2 5949 relax_start (offset_expr.X_add_symbol);
438c16b8
TS
5950 if (! mips_big_got)
5951 {
67c0d1eb
RS
5952 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5953 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 5954 mips_gp_register);
269137b2 5955 load_delay_nop ();
4d7206a2 5956 relax_switch ();
438c16b8 5957 }
252b5132 5958 else
252b5132 5959 {
67c0d1eb
RS
5960 int gpdelay;
5961
5962 gpdelay = reg_needs_delay (mips_gp_register);
5963 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5964 BFD_RELOC_MIPS_CALL_HI16);
5965 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5966 PIC_CALL_REG, mips_gp_register);
5967 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5968 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5969 PIC_CALL_REG);
269137b2 5970 load_delay_nop ();
4d7206a2 5971 relax_switch ();
67c0d1eb
RS
5972 if (gpdelay)
5973 macro_build (NULL, "nop", "");
252b5132 5974 }
67c0d1eb
RS
5975 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5976 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 5977 mips_gp_register);
269137b2 5978 load_delay_nop ();
67c0d1eb
RS
5979 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5980 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 5981 relax_end ();
67c0d1eb 5982 macro_build_jalr (&offset_expr);
438c16b8 5983
6478892d
TS
5984 if (mips_cprestore_offset < 0)
5985 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5986 else
5987 {
7a621144
DJ
5988 if (! mips_frame_reg_valid)
5989 {
5990 as_warn (_("No .frame pseudo-op used in PIC code"));
5991 /* Quiet this warning. */
5992 mips_frame_reg_valid = 1;
5993 }
5994 if (! mips_cprestore_valid)
5995 {
5996 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5997 /* Quiet this warning. */
5998 mips_cprestore_valid = 1;
5999 }
6478892d 6000 if (mips_opts.noreorder)
67c0d1eb 6001 macro_build (NULL, "nop", "");
6478892d 6002 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 6003 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 6004 mips_gp_register,
256ab948
TS
6005 mips_frame_reg,
6006 HAVE_64BIT_ADDRESSES);
6478892d 6007 }
252b5132
RH
6008 }
6009 }
0a44bf69
RS
6010 else if (mips_pic == VXWORKS_PIC)
6011 as_bad (_("Non-PIC jump used in PIC library"));
252b5132
RH
6012 else
6013 abort ();
6014
8fc2e39e 6015 break;
252b5132
RH
6016
6017 case M_LB_AB:
6018 s = "lb";
6019 goto ld;
6020 case M_LBU_AB:
6021 s = "lbu";
6022 goto ld;
6023 case M_LH_AB:
6024 s = "lh";
6025 goto ld;
6026 case M_LHU_AB:
6027 s = "lhu";
6028 goto ld;
6029 case M_LW_AB:
6030 s = "lw";
6031 goto ld;
6032 case M_LWC0_AB:
6033 s = "lwc0";
bdaaa2e1 6034 /* Itbl support may require additional care here. */
252b5132
RH
6035 coproc = 1;
6036 goto ld;
6037 case M_LWC1_AB:
6038 s = "lwc1";
bdaaa2e1 6039 /* Itbl support may require additional care here. */
252b5132
RH
6040 coproc = 1;
6041 goto ld;
6042 case M_LWC2_AB:
6043 s = "lwc2";
bdaaa2e1 6044 /* Itbl support may require additional care here. */
252b5132
RH
6045 coproc = 1;
6046 goto ld;
6047 case M_LWC3_AB:
6048 s = "lwc3";
bdaaa2e1 6049 /* Itbl support may require additional care here. */
252b5132
RH
6050 coproc = 1;
6051 goto ld;
6052 case M_LWL_AB:
6053 s = "lwl";
6054 lr = 1;
6055 goto ld;
6056 case M_LWR_AB:
6057 s = "lwr";
6058 lr = 1;
6059 goto ld;
6060 case M_LDC1_AB:
fef14a42 6061 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6062 {
6063 as_bad (_("opcode not supported on this processor"));
8fc2e39e 6064 break;
252b5132
RH
6065 }
6066 s = "ldc1";
bdaaa2e1 6067 /* Itbl support may require additional care here. */
252b5132
RH
6068 coproc = 1;
6069 goto ld;
6070 case M_LDC2_AB:
6071 s = "ldc2";
bdaaa2e1 6072 /* Itbl support may require additional care here. */
252b5132
RH
6073 coproc = 1;
6074 goto ld;
6075 case M_LDC3_AB:
6076 s = "ldc3";
bdaaa2e1 6077 /* Itbl support may require additional care here. */
252b5132
RH
6078 coproc = 1;
6079 goto ld;
6080 case M_LDL_AB:
6081 s = "ldl";
6082 lr = 1;
6083 goto ld;
6084 case M_LDR_AB:
6085 s = "ldr";
6086 lr = 1;
6087 goto ld;
6088 case M_LL_AB:
6089 s = "ll";
6090 goto ld;
6091 case M_LLD_AB:
6092 s = "lld";
6093 goto ld;
6094 case M_LWU_AB:
6095 s = "lwu";
6096 ld:
8fc2e39e 6097 if (breg == treg || coproc || lr)
252b5132
RH
6098 {
6099 tempreg = AT;
6100 used_at = 1;
6101 }
6102 else
6103 {
6104 tempreg = treg;
252b5132
RH
6105 }
6106 goto ld_st;
6107 case M_SB_AB:
6108 s = "sb";
6109 goto st;
6110 case M_SH_AB:
6111 s = "sh";
6112 goto st;
6113 case M_SW_AB:
6114 s = "sw";
6115 goto st;
6116 case M_SWC0_AB:
6117 s = "swc0";
bdaaa2e1 6118 /* Itbl support may require additional care here. */
252b5132
RH
6119 coproc = 1;
6120 goto st;
6121 case M_SWC1_AB:
6122 s = "swc1";
bdaaa2e1 6123 /* Itbl support may require additional care here. */
252b5132
RH
6124 coproc = 1;
6125 goto st;
6126 case M_SWC2_AB:
6127 s = "swc2";
bdaaa2e1 6128 /* Itbl support may require additional care here. */
252b5132
RH
6129 coproc = 1;
6130 goto st;
6131 case M_SWC3_AB:
6132 s = "swc3";
bdaaa2e1 6133 /* Itbl support may require additional care here. */
252b5132
RH
6134 coproc = 1;
6135 goto st;
6136 case M_SWL_AB:
6137 s = "swl";
6138 goto st;
6139 case M_SWR_AB:
6140 s = "swr";
6141 goto st;
6142 case M_SC_AB:
6143 s = "sc";
6144 goto st;
6145 case M_SCD_AB:
6146 s = "scd";
6147 goto st;
d43b4baf
TS
6148 case M_CACHE_AB:
6149 s = "cache";
6150 goto st;
252b5132 6151 case M_SDC1_AB:
fef14a42 6152 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6153 {
6154 as_bad (_("opcode not supported on this processor"));
8fc2e39e 6155 break;
252b5132
RH
6156 }
6157 s = "sdc1";
6158 coproc = 1;
bdaaa2e1 6159 /* Itbl support may require additional care here. */
252b5132
RH
6160 goto st;
6161 case M_SDC2_AB:
6162 s = "sdc2";
bdaaa2e1 6163 /* Itbl support may require additional care here. */
252b5132
RH
6164 coproc = 1;
6165 goto st;
6166 case M_SDC3_AB:
6167 s = "sdc3";
bdaaa2e1 6168 /* Itbl support may require additional care here. */
252b5132
RH
6169 coproc = 1;
6170 goto st;
6171 case M_SDL_AB:
6172 s = "sdl";
6173 goto st;
6174 case M_SDR_AB:
6175 s = "sdr";
6176 st:
8fc2e39e
TS
6177 tempreg = AT;
6178 used_at = 1;
252b5132 6179 ld_st:
bdaaa2e1 6180 /* Itbl support may require additional care here. */
252b5132
RH
6181 if (mask == M_LWC1_AB
6182 || mask == M_SWC1_AB
6183 || mask == M_LDC1_AB
6184 || mask == M_SDC1_AB
6185 || mask == M_L_DAB
6186 || mask == M_S_DAB)
6187 fmt = "T,o(b)";
d43b4baf
TS
6188 else if (mask == M_CACHE_AB)
6189 fmt = "k,o(b)";
252b5132
RH
6190 else if (coproc)
6191 fmt = "E,o(b)";
6192 else
6193 fmt = "t,o(b)";
6194
6195 if (offset_expr.X_op != O_constant
6196 && offset_expr.X_op != O_symbol)
6197 {
6198 as_bad (_("expression too complex"));
6199 offset_expr.X_op = O_constant;
6200 }
6201
2051e8c4
MR
6202 if (HAVE_32BIT_ADDRESSES
6203 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
6204 {
6205 char value [32];
6206
6207 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 6208 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 6209 }
2051e8c4 6210
252b5132
RH
6211 /* A constant expression in PIC code can be handled just as it
6212 is in non PIC code. */
aed1a261
RS
6213 if (offset_expr.X_op == O_constant)
6214 {
aed1a261
RS
6215 expr1.X_add_number = ((offset_expr.X_add_number + 0x8000)
6216 & ~(bfd_vma) 0xffff);
2051e8c4 6217 normalize_address_expr (&expr1);
aed1a261
RS
6218 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
6219 if (breg != 0)
6220 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6221 tempreg, tempreg, breg);
6222 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6223 }
6224 else if (mips_pic == NO_PIC)
252b5132
RH
6225 {
6226 /* If this is a reference to a GP relative symbol, and there
6227 is no base register, we want
cdf6fd85 6228 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6229 Otherwise, if there is no base register, we want
6230 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
6231 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6232 If we have a constant, we need two instructions anyhow,
6233 so we always use the latter form.
6234
6235 If we have a base register, and this is a reference to a
6236 GP relative symbol, we want
6237 addu $tempreg,$breg,$gp
cdf6fd85 6238 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
6239 Otherwise we want
6240 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
6241 addu $tempreg,$tempreg,$breg
6242 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 6243 With a constant we always use the latter case.
76b3015f 6244
d6bc6245
TS
6245 With 64bit address space and no base register and $at usable,
6246 we want
6247 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6248 lui $at,<sym> (BFD_RELOC_HI16_S)
6249 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6250 dsll32 $tempreg,0
6251 daddu $tempreg,$at
6252 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6253 If we have a base register, we want
6254 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6255 lui $at,<sym> (BFD_RELOC_HI16_S)
6256 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6257 daddu $at,$breg
6258 dsll32 $tempreg,0
6259 daddu $tempreg,$at
6260 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6261
6262 Without $at we can't generate the optimal path for superscalar
6263 processors here since this would require two temporary registers.
6264 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6265 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6266 dsll $tempreg,16
6267 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
6268 dsll $tempreg,16
6269 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6270 If we have a base register, we want
6271 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6272 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6273 dsll $tempreg,16
6274 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
6275 dsll $tempreg,16
6276 daddu $tempreg,$tempreg,$breg
6277 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 6278
6caf9ef4 6279 For GP relative symbols in 64bit address space we can use
aed1a261
RS
6280 the same sequence as in 32bit address space. */
6281 if (HAVE_64BIT_SYMBOLS)
d6bc6245 6282 {
aed1a261 6283 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
6284 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
6285 {
6286 relax_start (offset_expr.X_add_symbol);
6287 if (breg == 0)
6288 {
6289 macro_build (&offset_expr, s, fmt, treg,
6290 BFD_RELOC_GPREL16, mips_gp_register);
6291 }
6292 else
6293 {
6294 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6295 tempreg, breg, mips_gp_register);
6296 macro_build (&offset_expr, s, fmt, treg,
6297 BFD_RELOC_GPREL16, tempreg);
6298 }
6299 relax_switch ();
6300 }
d6bc6245 6301
b8285c27 6302 if (used_at == 0 && !mips_opts.noat)
d6bc6245 6303 {
67c0d1eb
RS
6304 macro_build (&offset_expr, "lui", "t,u", tempreg,
6305 BFD_RELOC_MIPS_HIGHEST);
6306 macro_build (&offset_expr, "lui", "t,u", AT,
6307 BFD_RELOC_HI16_S);
6308 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6309 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 6310 if (breg != 0)
67c0d1eb
RS
6311 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
6312 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
6313 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
6314 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
6315 tempreg);
d6bc6245
TS
6316 used_at = 1;
6317 }
6318 else
6319 {
67c0d1eb
RS
6320 macro_build (&offset_expr, "lui", "t,u", tempreg,
6321 BFD_RELOC_MIPS_HIGHEST);
6322 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6323 tempreg, BFD_RELOC_MIPS_HIGHER);
6324 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
6325 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6326 tempreg, BFD_RELOC_HI16_S);
6327 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
d6bc6245 6328 if (breg != 0)
67c0d1eb 6329 macro_build (NULL, "daddu", "d,v,t",
17a2f251 6330 tempreg, tempreg, breg);
67c0d1eb 6331 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6332 BFD_RELOC_LO16, tempreg);
d6bc6245 6333 }
6caf9ef4
TS
6334
6335 if (mips_relax.sequence)
6336 relax_end ();
8fc2e39e 6337 break;
d6bc6245 6338 }
256ab948 6339
252b5132
RH
6340 if (breg == 0)
6341 {
67c0d1eb 6342 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 6343 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 6344 {
4d7206a2 6345 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
6346 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
6347 mips_gp_register);
4d7206a2 6348 relax_switch ();
252b5132 6349 }
67c0d1eb
RS
6350 macro_build_lui (&offset_expr, tempreg);
6351 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6352 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
6353 if (mips_relax.sequence)
6354 relax_end ();
252b5132
RH
6355 }
6356 else
6357 {
67c0d1eb 6358 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 6359 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 6360 {
4d7206a2 6361 relax_start (offset_expr.X_add_symbol);
67c0d1eb 6362 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6363 tempreg, breg, mips_gp_register);
67c0d1eb 6364 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6365 BFD_RELOC_GPREL16, tempreg);
4d7206a2 6366 relax_switch ();
252b5132 6367 }
67c0d1eb
RS
6368 macro_build_lui (&offset_expr, tempreg);
6369 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6370 tempreg, tempreg, breg);
67c0d1eb 6371 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6372 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
6373 if (mips_relax.sequence)
6374 relax_end ();
252b5132
RH
6375 }
6376 }
0a44bf69 6377 else if (!mips_big_got)
252b5132 6378 {
ed6fb7bd 6379 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 6380
252b5132
RH
6381 /* If this is a reference to an external symbol, we want
6382 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6383 nop
6384 <op> $treg,0($tempreg)
6385 Otherwise we want
6386 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6387 nop
6388 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6389 <op> $treg,0($tempreg)
f5040a92
AO
6390
6391 For NewABI, we want
6392 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6393 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
6394
252b5132
RH
6395 If there is a base register, we add it to $tempreg before
6396 the <op>. If there is a constant, we stick it in the
6397 <op> instruction. We don't handle constants larger than
6398 16 bits, because we have no way to load the upper 16 bits
6399 (actually, we could handle them for the subset of cases
6400 in which we are not using $at). */
6401 assert (offset_expr.X_op == O_symbol);
f5040a92
AO
6402 if (HAVE_NEWABI)
6403 {
67c0d1eb
RS
6404 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6405 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 6406 if (breg != 0)
67c0d1eb 6407 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6408 tempreg, tempreg, breg);
67c0d1eb 6409 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6410 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
6411 break;
6412 }
252b5132
RH
6413 expr1.X_add_number = offset_expr.X_add_number;
6414 offset_expr.X_add_number = 0;
6415 if (expr1.X_add_number < -0x8000
6416 || expr1.X_add_number >= 0x8000)
6417 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
6418 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6419 lw_reloc_type, mips_gp_register);
269137b2 6420 load_delay_nop ();
4d7206a2
RS
6421 relax_start (offset_expr.X_add_symbol);
6422 relax_switch ();
67c0d1eb
RS
6423 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6424 tempreg, BFD_RELOC_LO16);
4d7206a2 6425 relax_end ();
252b5132 6426 if (breg != 0)
67c0d1eb 6427 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6428 tempreg, tempreg, breg);
67c0d1eb 6429 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 6430 }
0a44bf69 6431 else if (mips_big_got && !HAVE_NEWABI)
252b5132 6432 {
67c0d1eb 6433 int gpdelay;
252b5132
RH
6434
6435 /* If this is a reference to an external symbol, we want
6436 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6437 addu $tempreg,$tempreg,$gp
6438 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6439 <op> $treg,0($tempreg)
6440 Otherwise we want
6441 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6442 nop
6443 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6444 <op> $treg,0($tempreg)
6445 If there is a base register, we add it to $tempreg before
6446 the <op>. If there is a constant, we stick it in the
6447 <op> instruction. We don't handle constants larger than
6448 16 bits, because we have no way to load the upper 16 bits
6449 (actually, we could handle them for the subset of cases
f5040a92 6450 in which we are not using $at). */
252b5132
RH
6451 assert (offset_expr.X_op == O_symbol);
6452 expr1.X_add_number = offset_expr.X_add_number;
6453 offset_expr.X_add_number = 0;
6454 if (expr1.X_add_number < -0x8000
6455 || expr1.X_add_number >= 0x8000)
6456 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 6457 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 6458 relax_start (offset_expr.X_add_symbol);
67c0d1eb 6459 macro_build (&offset_expr, "lui", "t,u", tempreg,
17a2f251 6460 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
6461 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6462 mips_gp_register);
6463 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6464 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 6465 relax_switch ();
67c0d1eb
RS
6466 if (gpdelay)
6467 macro_build (NULL, "nop", "");
6468 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6469 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 6470 load_delay_nop ();
67c0d1eb
RS
6471 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6472 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
6473 relax_end ();
6474
252b5132 6475 if (breg != 0)
67c0d1eb 6476 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6477 tempreg, tempreg, breg);
67c0d1eb 6478 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 6479 }
0a44bf69 6480 else if (mips_big_got && HAVE_NEWABI)
f5040a92 6481 {
f5040a92
AO
6482 /* If this is a reference to an external symbol, we want
6483 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6484 add $tempreg,$tempreg,$gp
6485 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6486 <op> $treg,<ofst>($tempreg)
6487 Otherwise, for local symbols, we want:
6488 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6489 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
6490 assert (offset_expr.X_op == O_symbol);
4d7206a2 6491 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
6492 offset_expr.X_add_number = 0;
6493 if (expr1.X_add_number < -0x8000
6494 || expr1.X_add_number >= 0x8000)
6495 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 6496 relax_start (offset_expr.X_add_symbol);
67c0d1eb 6497 macro_build (&offset_expr, "lui", "t,u", tempreg,
17a2f251 6498 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
6499 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6500 mips_gp_register);
6501 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6502 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 6503 if (breg != 0)
67c0d1eb 6504 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6505 tempreg, tempreg, breg);
67c0d1eb 6506 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
684022ea 6507
4d7206a2 6508 relax_switch ();
f5040a92 6509 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
6510 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6511 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 6512 if (breg != 0)
67c0d1eb 6513 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6514 tempreg, tempreg, breg);
67c0d1eb 6515 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6516 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 6517 relax_end ();
f5040a92 6518 }
252b5132
RH
6519 else
6520 abort ();
6521
252b5132
RH
6522 break;
6523
6524 case M_LI:
6525 case M_LI_S:
67c0d1eb 6526 load_register (treg, &imm_expr, 0);
8fc2e39e 6527 break;
252b5132
RH
6528
6529 case M_DLI:
67c0d1eb 6530 load_register (treg, &imm_expr, 1);
8fc2e39e 6531 break;
252b5132
RH
6532
6533 case M_LI_SS:
6534 if (imm_expr.X_op == O_constant)
6535 {
8fc2e39e 6536 used_at = 1;
67c0d1eb
RS
6537 load_register (AT, &imm_expr, 0);
6538 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
6539 break;
6540 }
6541 else
6542 {
6543 assert (offset_expr.X_op == O_symbol
6544 && strcmp (segment_name (S_GET_SEGMENT
6545 (offset_expr.X_add_symbol)),
6546 ".lit4") == 0
6547 && offset_expr.X_add_number == 0);
67c0d1eb 6548 macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
17a2f251 6549 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 6550 break;
252b5132
RH
6551 }
6552
6553 case M_LI_D:
ca4e0257
RS
6554 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6555 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6556 order 32 bits of the value and the low order 32 bits are either
6557 zero or in OFFSET_EXPR. */
252b5132
RH
6558 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6559 {
ca4e0257 6560 if (HAVE_64BIT_GPRS)
67c0d1eb 6561 load_register (treg, &imm_expr, 1);
252b5132
RH
6562 else
6563 {
6564 int hreg, lreg;
6565
6566 if (target_big_endian)
6567 {
6568 hreg = treg;
6569 lreg = treg + 1;
6570 }
6571 else
6572 {
6573 hreg = treg + 1;
6574 lreg = treg;
6575 }
6576
6577 if (hreg <= 31)
67c0d1eb 6578 load_register (hreg, &imm_expr, 0);
252b5132
RH
6579 if (lreg <= 31)
6580 {
6581 if (offset_expr.X_op == O_absent)
67c0d1eb 6582 move_register (lreg, 0);
252b5132
RH
6583 else
6584 {
6585 assert (offset_expr.X_op == O_constant);
67c0d1eb 6586 load_register (lreg, &offset_expr, 0);
252b5132
RH
6587 }
6588 }
6589 }
8fc2e39e 6590 break;
252b5132
RH
6591 }
6592
6593 /* We know that sym is in the .rdata section. First we get the
6594 upper 16 bits of the address. */
6595 if (mips_pic == NO_PIC)
6596 {
67c0d1eb 6597 macro_build_lui (&offset_expr, AT);
8fc2e39e 6598 used_at = 1;
252b5132 6599 }
0a44bf69 6600 else
252b5132 6601 {
67c0d1eb
RS
6602 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6603 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 6604 used_at = 1;
252b5132 6605 }
bdaaa2e1 6606
252b5132 6607 /* Now we load the register(s). */
ca4e0257 6608 if (HAVE_64BIT_GPRS)
8fc2e39e
TS
6609 {
6610 used_at = 1;
6611 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6612 }
252b5132
RH
6613 else
6614 {
8fc2e39e 6615 used_at = 1;
67c0d1eb 6616 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
f9419b05 6617 if (treg != RA)
252b5132
RH
6618 {
6619 /* FIXME: How in the world do we deal with the possible
6620 overflow here? */
6621 offset_expr.X_add_number += 4;
67c0d1eb 6622 macro_build (&offset_expr, "lw", "t,o(b)",
17a2f251 6623 treg + 1, BFD_RELOC_LO16, AT);
252b5132
RH
6624 }
6625 }
252b5132
RH
6626 break;
6627
6628 case M_LI_DD:
ca4e0257
RS
6629 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6630 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6631 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6632 the value and the low order 32 bits are either zero or in
6633 OFFSET_EXPR. */
252b5132
RH
6634 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6635 {
8fc2e39e 6636 used_at = 1;
67c0d1eb 6637 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
ca4e0257
RS
6638 if (HAVE_64BIT_FPRS)
6639 {
6640 assert (HAVE_64BIT_GPRS);
67c0d1eb 6641 macro_build (NULL, "dmtc1", "t,S", AT, treg);
ca4e0257 6642 }
252b5132
RH
6643 else
6644 {
67c0d1eb 6645 macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
252b5132 6646 if (offset_expr.X_op == O_absent)
67c0d1eb 6647 macro_build (NULL, "mtc1", "t,G", 0, treg);
252b5132
RH
6648 else
6649 {
6650 assert (offset_expr.X_op == O_constant);
67c0d1eb
RS
6651 load_register (AT, &offset_expr, 0);
6652 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
6653 }
6654 }
6655 break;
6656 }
6657
6658 assert (offset_expr.X_op == O_symbol
6659 && offset_expr.X_add_number == 0);
6660 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6661 if (strcmp (s, ".lit8") == 0)
6662 {
e7af610e 6663 if (mips_opts.isa != ISA_MIPS1)
252b5132 6664 {
67c0d1eb 6665 macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
17a2f251 6666 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 6667 break;
252b5132 6668 }
c9914766 6669 breg = mips_gp_register;
252b5132
RH
6670 r = BFD_RELOC_MIPS_LITERAL;
6671 goto dob;
6672 }
6673 else
6674 {
6675 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 6676 used_at = 1;
0a44bf69 6677 if (mips_pic != NO_PIC)
67c0d1eb
RS
6678 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6679 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
6680 else
6681 {
6682 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 6683 macro_build_lui (&offset_expr, AT);
252b5132 6684 }
bdaaa2e1 6685
e7af610e 6686 if (mips_opts.isa != ISA_MIPS1)
252b5132 6687 {
67c0d1eb
RS
6688 macro_build (&offset_expr, "ldc1", "T,o(b)",
6689 treg, BFD_RELOC_LO16, AT);
252b5132
RH
6690 break;
6691 }
6692 breg = AT;
6693 r = BFD_RELOC_LO16;
6694 goto dob;
6695 }
6696
6697 case M_L_DOB:
fef14a42 6698 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6699 {
6700 as_bad (_("opcode not supported on this processor"));
8fc2e39e 6701 break;
252b5132
RH
6702 }
6703 /* Even on a big endian machine $fn comes before $fn+1. We have
6704 to adjust when loading from memory. */
6705 r = BFD_RELOC_LO16;
6706 dob:
e7af610e 6707 assert (mips_opts.isa == ISA_MIPS1);
67c0d1eb 6708 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 6709 target_big_endian ? treg + 1 : treg, r, breg);
252b5132
RH
6710 /* FIXME: A possible overflow which I don't know how to deal
6711 with. */
6712 offset_expr.X_add_number += 4;
67c0d1eb 6713 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 6714 target_big_endian ? treg : treg + 1, r, breg);
252b5132
RH
6715 break;
6716
6717 case M_L_DAB:
6718 /*
6719 * The MIPS assembler seems to check for X_add_number not
6720 * being double aligned and generating:
6721 * lui at,%hi(foo+1)
6722 * addu at,at,v1
6723 * addiu at,at,%lo(foo+1)
6724 * lwc1 f2,0(at)
6725 * lwc1 f3,4(at)
6726 * But, the resulting address is the same after relocation so why
6727 * generate the extra instruction?
6728 */
fef14a42 6729 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6730 {
6731 as_bad (_("opcode not supported on this processor"));
8fc2e39e 6732 break;
252b5132 6733 }
bdaaa2e1 6734 /* Itbl support may require additional care here. */
252b5132 6735 coproc = 1;
e7af610e 6736 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6737 {
6738 s = "ldc1";
6739 goto ld;
6740 }
6741
6742 s = "lwc1";
6743 fmt = "T,o(b)";
6744 goto ldd_std;
6745
6746 case M_S_DAB:
fef14a42 6747 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6748 {
6749 as_bad (_("opcode not supported on this processor"));
8fc2e39e 6750 break;
252b5132
RH
6751 }
6752
e7af610e 6753 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6754 {
6755 s = "sdc1";
6756 goto st;
6757 }
6758
6759 s = "swc1";
6760 fmt = "T,o(b)";
bdaaa2e1 6761 /* Itbl support may require additional care here. */
252b5132
RH
6762 coproc = 1;
6763 goto ldd_std;
6764
6765 case M_LD_AB:
ca4e0257 6766 if (HAVE_64BIT_GPRS)
252b5132
RH
6767 {
6768 s = "ld";
6769 goto ld;
6770 }
6771
6772 s = "lw";
6773 fmt = "t,o(b)";
6774 goto ldd_std;
6775
6776 case M_SD_AB:
ca4e0257 6777 if (HAVE_64BIT_GPRS)
252b5132
RH
6778 {
6779 s = "sd";
6780 goto st;
6781 }
6782
6783 s = "sw";
6784 fmt = "t,o(b)";
6785
6786 ldd_std:
6787 if (offset_expr.X_op != O_symbol
6788 && offset_expr.X_op != O_constant)
6789 {
6790 as_bad (_("expression too complex"));
6791 offset_expr.X_op = O_constant;
6792 }
6793
2051e8c4
MR
6794 if (HAVE_32BIT_ADDRESSES
6795 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
6796 {
6797 char value [32];
6798
6799 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 6800 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 6801 }
2051e8c4 6802
252b5132
RH
6803 /* Even on a big endian machine $fn comes before $fn+1. We have
6804 to adjust when loading from memory. We set coproc if we must
6805 load $fn+1 first. */
bdaaa2e1 6806 /* Itbl support may require additional care here. */
252b5132
RH
6807 if (! target_big_endian)
6808 coproc = 0;
6809
6810 if (mips_pic == NO_PIC
6811 || offset_expr.X_op == O_constant)
6812 {
6813 /* If this is a reference to a GP relative symbol, we want
cdf6fd85
TS
6814 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6815 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6816 If we have a base register, we use this
6817 addu $at,$breg,$gp
cdf6fd85
TS
6818 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6819 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6820 If this is not a GP relative symbol, we want
6821 lui $at,<sym> (BFD_RELOC_HI16_S)
6822 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6823 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6824 If there is a base register, we add it to $at after the
6825 lui instruction. If there is a constant, we always use
6826 the last case. */
39a59cf8
MR
6827 if (offset_expr.X_op == O_symbol
6828 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 6829 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 6830 {
4d7206a2 6831 relax_start (offset_expr.X_add_symbol);
252b5132
RH
6832 if (breg == 0)
6833 {
c9914766 6834 tempreg = mips_gp_register;
252b5132
RH
6835 }
6836 else
6837 {
67c0d1eb 6838 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6839 AT, breg, mips_gp_register);
252b5132 6840 tempreg = AT;
252b5132
RH
6841 used_at = 1;
6842 }
6843
beae10d5 6844 /* Itbl support may require additional care here. */
67c0d1eb 6845 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 6846 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6847 offset_expr.X_add_number += 4;
6848
6849 /* Set mips_optimize to 2 to avoid inserting an
6850 undesired nop. */
6851 hold_mips_optimize = mips_optimize;
6852 mips_optimize = 2;
beae10d5 6853 /* Itbl support may require additional care here. */
67c0d1eb 6854 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 6855 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6856 mips_optimize = hold_mips_optimize;
6857
4d7206a2 6858 relax_switch ();
252b5132
RH
6859
6860 /* We just generated two relocs. When tc_gen_reloc
6861 handles this case, it will skip the first reloc and
6862 handle the second. The second reloc already has an
6863 extra addend of 4, which we added above. We must
6864 subtract it out, and then subtract another 4 to make
6865 the first reloc come out right. The second reloc
6866 will come out right because we are going to add 4 to
6867 offset_expr when we build its instruction below.
6868
6869 If we have a symbol, then we don't want to include
6870 the offset, because it will wind up being included
6871 when we generate the reloc. */
6872
6873 if (offset_expr.X_op == O_constant)
6874 offset_expr.X_add_number -= 8;
6875 else
6876 {
6877 offset_expr.X_add_number = -4;
6878 offset_expr.X_op = O_constant;
6879 }
6880 }
8fc2e39e 6881 used_at = 1;
67c0d1eb 6882 macro_build_lui (&offset_expr, AT);
252b5132 6883 if (breg != 0)
67c0d1eb 6884 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 6885 /* Itbl support may require additional care here. */
67c0d1eb 6886 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 6887 BFD_RELOC_LO16, AT);
252b5132
RH
6888 /* FIXME: How do we handle overflow here? */
6889 offset_expr.X_add_number += 4;
beae10d5 6890 /* Itbl support may require additional care here. */
67c0d1eb 6891 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 6892 BFD_RELOC_LO16, AT);
4d7206a2
RS
6893 if (mips_relax.sequence)
6894 relax_end ();
bdaaa2e1 6895 }
0a44bf69 6896 else if (!mips_big_got)
252b5132 6897 {
252b5132
RH
6898 /* If this is a reference to an external symbol, we want
6899 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6900 nop
6901 <op> $treg,0($at)
6902 <op> $treg+1,4($at)
6903 Otherwise we want
6904 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6905 nop
6906 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6907 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6908 If there is a base register we add it to $at before the
6909 lwc1 instructions. If there is a constant we include it
6910 in the lwc1 instructions. */
6911 used_at = 1;
6912 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
6913 if (expr1.X_add_number < -0x8000
6914 || expr1.X_add_number >= 0x8000 - 4)
6915 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 6916 load_got_offset (AT, &offset_expr);
269137b2 6917 load_delay_nop ();
252b5132 6918 if (breg != 0)
67c0d1eb 6919 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
6920
6921 /* Set mips_optimize to 2 to avoid inserting an undesired
6922 nop. */
6923 hold_mips_optimize = mips_optimize;
6924 mips_optimize = 2;
4d7206a2 6925
beae10d5 6926 /* Itbl support may require additional care here. */
4d7206a2 6927 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
6928 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6929 BFD_RELOC_LO16, AT);
4d7206a2 6930 expr1.X_add_number += 4;
67c0d1eb
RS
6931 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6932 BFD_RELOC_LO16, AT);
4d7206a2 6933 relax_switch ();
67c0d1eb
RS
6934 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6935 BFD_RELOC_LO16, AT);
4d7206a2 6936 offset_expr.X_add_number += 4;
67c0d1eb
RS
6937 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6938 BFD_RELOC_LO16, AT);
4d7206a2 6939 relax_end ();
252b5132 6940
4d7206a2 6941 mips_optimize = hold_mips_optimize;
252b5132 6942 }
0a44bf69 6943 else if (mips_big_got)
252b5132 6944 {
67c0d1eb 6945 int gpdelay;
252b5132
RH
6946
6947 /* If this is a reference to an external symbol, we want
6948 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6949 addu $at,$at,$gp
6950 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6951 nop
6952 <op> $treg,0($at)
6953 <op> $treg+1,4($at)
6954 Otherwise we want
6955 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6956 nop
6957 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6958 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6959 If there is a base register we add it to $at before the
6960 lwc1 instructions. If there is a constant we include it
6961 in the lwc1 instructions. */
6962 used_at = 1;
6963 expr1.X_add_number = offset_expr.X_add_number;
6964 offset_expr.X_add_number = 0;
6965 if (expr1.X_add_number < -0x8000
6966 || expr1.X_add_number >= 0x8000 - 4)
6967 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 6968 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 6969 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
6970 macro_build (&offset_expr, "lui", "t,u",
6971 AT, BFD_RELOC_MIPS_GOT_HI16);
6972 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6973 AT, AT, mips_gp_register);
67c0d1eb 6974 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 6975 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 6976 load_delay_nop ();
252b5132 6977 if (breg != 0)
67c0d1eb 6978 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 6979 /* Itbl support may require additional care here. */
67c0d1eb 6980 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
17a2f251 6981 BFD_RELOC_LO16, AT);
252b5132
RH
6982 expr1.X_add_number += 4;
6983
6984 /* Set mips_optimize to 2 to avoid inserting an undesired
6985 nop. */
6986 hold_mips_optimize = mips_optimize;
6987 mips_optimize = 2;
beae10d5 6988 /* Itbl support may require additional care here. */
67c0d1eb 6989 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
17a2f251 6990 BFD_RELOC_LO16, AT);
252b5132
RH
6991 mips_optimize = hold_mips_optimize;
6992 expr1.X_add_number -= 4;
6993
4d7206a2
RS
6994 relax_switch ();
6995 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
6996 if (gpdelay)
6997 macro_build (NULL, "nop", "");
6998 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6999 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 7000 load_delay_nop ();
252b5132 7001 if (breg != 0)
67c0d1eb 7002 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 7003 /* Itbl support may require additional care here. */
67c0d1eb
RS
7004 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
7005 BFD_RELOC_LO16, AT);
4d7206a2 7006 offset_expr.X_add_number += 4;
252b5132
RH
7007
7008 /* Set mips_optimize to 2 to avoid inserting an undesired
7009 nop. */
7010 hold_mips_optimize = mips_optimize;
7011 mips_optimize = 2;
beae10d5 7012 /* Itbl support may require additional care here. */
67c0d1eb
RS
7013 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
7014 BFD_RELOC_LO16, AT);
252b5132 7015 mips_optimize = hold_mips_optimize;
4d7206a2 7016 relax_end ();
252b5132 7017 }
252b5132
RH
7018 else
7019 abort ();
7020
252b5132
RH
7021 break;
7022
7023 case M_LD_OB:
7024 s = "lw";
7025 goto sd_ob;
7026 case M_SD_OB:
7027 s = "sw";
7028 sd_ob:
ca4e0257 7029 assert (HAVE_32BIT_ADDRESSES);
67c0d1eb 7030 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
252b5132 7031 offset_expr.X_add_number += 4;
67c0d1eb 7032 macro_build (&offset_expr, s, "t,o(b)", treg + 1, BFD_RELOC_LO16, breg);
8fc2e39e 7033 break;
252b5132
RH
7034
7035 /* New code added to support COPZ instructions.
7036 This code builds table entries out of the macros in mip_opcodes.
7037 R4000 uses interlocks to handle coproc delays.
7038 Other chips (like the R3000) require nops to be inserted for delays.
7039
f72c8c98 7040 FIXME: Currently, we require that the user handle delays.
252b5132
RH
7041 In order to fill delay slots for non-interlocked chips,
7042 we must have a way to specify delays based on the coprocessor.
7043 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
7044 What are the side-effects of the cop instruction?
7045 What cache support might we have and what are its effects?
7046 Both coprocessor & memory require delays. how long???
bdaaa2e1 7047 What registers are read/set/modified?
252b5132
RH
7048
7049 If an itbl is provided to interpret cop instructions,
bdaaa2e1 7050 this knowledge can be encoded in the itbl spec. */
252b5132
RH
7051
7052 case M_COP0:
7053 s = "c0";
7054 goto copz;
7055 case M_COP1:
7056 s = "c1";
7057 goto copz;
7058 case M_COP2:
7059 s = "c2";
7060 goto copz;
7061 case M_COP3:
7062 s = "c3";
7063 copz:
7064 /* For now we just do C (same as Cz). The parameter will be
7065 stored in insn_opcode by mips_ip. */
67c0d1eb 7066 macro_build (NULL, s, "C", ip->insn_opcode);
8fc2e39e 7067 break;
252b5132 7068
ea1fb5dc 7069 case M_MOVE:
67c0d1eb 7070 move_register (dreg, sreg);
8fc2e39e 7071 break;
ea1fb5dc 7072
252b5132
RH
7073#ifdef LOSING_COMPILER
7074 default:
7075 /* Try and see if this is a new itbl instruction.
7076 This code builds table entries out of the macros in mip_opcodes.
7077 FIXME: For now we just assemble the expression and pass it's
7078 value along as a 32-bit immediate.
bdaaa2e1 7079 We may want to have the assembler assemble this value,
252b5132
RH
7080 so that we gain the assembler's knowledge of delay slots,
7081 symbols, etc.
7082 Would it be more efficient to use mask (id) here? */
bdaaa2e1 7083 if (itbl_have_entries
252b5132 7084 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
beae10d5 7085 {
252b5132
RH
7086 s = ip->insn_mo->name;
7087 s2 = "cop3";
7088 coproc = ITBL_DECODE_PNUM (immed_expr);;
67c0d1eb 7089 macro_build (&immed_expr, s, "C");
8fc2e39e 7090 break;
beae10d5 7091 }
252b5132 7092 macro2 (ip);
8fc2e39e 7093 break;
252b5132 7094 }
8fc2e39e
TS
7095 if (mips_opts.noat && used_at)
7096 as_bad (_("Macro used $at after \".set noat\""));
252b5132 7097}
bdaaa2e1 7098
252b5132 7099static void
17a2f251 7100macro2 (struct mips_cl_insn *ip)
252b5132 7101{
3994f87e 7102 int treg, sreg, dreg, breg;
252b5132
RH
7103 int tempreg;
7104 int mask;
252b5132
RH
7105 int used_at;
7106 expressionS expr1;
7107 const char *s;
7108 const char *s2;
7109 const char *fmt;
7110 int likely = 0;
7111 int dbl = 0;
7112 int coproc = 0;
7113 int lr = 0;
7114 int imm = 0;
7115 int off;
7116 offsetT maxnum;
7117 bfd_reloc_code_real_type r;
bdaaa2e1 7118
252b5132
RH
7119 treg = (ip->insn_opcode >> 16) & 0x1f;
7120 dreg = (ip->insn_opcode >> 11) & 0x1f;
7121 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
7122 mask = ip->insn_mo->mask;
bdaaa2e1 7123
252b5132
RH
7124 expr1.X_op = O_constant;
7125 expr1.X_op_symbol = NULL;
7126 expr1.X_add_symbol = NULL;
7127 expr1.X_add_number = 1;
bdaaa2e1 7128
252b5132
RH
7129 switch (mask)
7130 {
7131#endif /* LOSING_COMPILER */
7132
7133 case M_DMUL:
7134 dbl = 1;
7135 case M_MUL:
67c0d1eb
RS
7136 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
7137 macro_build (NULL, "mflo", "d", dreg);
8fc2e39e 7138 break;
252b5132
RH
7139
7140 case M_DMUL_I:
7141 dbl = 1;
7142 case M_MUL_I:
7143 /* The MIPS assembler some times generates shifts and adds. I'm
7144 not trying to be that fancy. GCC should do this for us
7145 anyway. */
8fc2e39e 7146 used_at = 1;
67c0d1eb
RS
7147 load_register (AT, &imm_expr, dbl);
7148 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
7149 macro_build (NULL, "mflo", "d", dreg);
252b5132
RH
7150 break;
7151
7152 case M_DMULO_I:
7153 dbl = 1;
7154 case M_MULO_I:
7155 imm = 1;
7156 goto do_mulo;
7157
7158 case M_DMULO:
7159 dbl = 1;
7160 case M_MULO:
7161 do_mulo:
7d10b47d 7162 start_noreorder ();
8fc2e39e 7163 used_at = 1;
252b5132 7164 if (imm)
67c0d1eb
RS
7165 load_register (AT, &imm_expr, dbl);
7166 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
7167 macro_build (NULL, "mflo", "d", dreg);
7168 macro_build (NULL, dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
7169 macro_build (NULL, "mfhi", "d", AT);
252b5132 7170 if (mips_trap)
67c0d1eb 7171 macro_build (NULL, "tne", "s,t,q", dreg, AT, 6);
252b5132
RH
7172 else
7173 {
7174 expr1.X_add_number = 8;
67c0d1eb
RS
7175 macro_build (&expr1, "beq", "s,t,p", dreg, AT);
7176 macro_build (NULL, "nop", "", 0);
7177 macro_build (NULL, "break", "c", 6);
252b5132 7178 }
7d10b47d 7179 end_noreorder ();
67c0d1eb 7180 macro_build (NULL, "mflo", "d", dreg);
252b5132
RH
7181 break;
7182
7183 case M_DMULOU_I:
7184 dbl = 1;
7185 case M_MULOU_I:
7186 imm = 1;
7187 goto do_mulou;
7188
7189 case M_DMULOU:
7190 dbl = 1;
7191 case M_MULOU:
7192 do_mulou:
7d10b47d 7193 start_noreorder ();
8fc2e39e 7194 used_at = 1;
252b5132 7195 if (imm)
67c0d1eb
RS
7196 load_register (AT, &imm_expr, dbl);
7197 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
17a2f251 7198 sreg, imm ? AT : treg);
67c0d1eb
RS
7199 macro_build (NULL, "mfhi", "d", AT);
7200 macro_build (NULL, "mflo", "d", dreg);
252b5132 7201 if (mips_trap)
67c0d1eb 7202 macro_build (NULL, "tne", "s,t,q", AT, 0, 6);
252b5132
RH
7203 else
7204 {
7205 expr1.X_add_number = 8;
67c0d1eb
RS
7206 macro_build (&expr1, "beq", "s,t,p", AT, 0);
7207 macro_build (NULL, "nop", "", 0);
7208 macro_build (NULL, "break", "c", 6);
252b5132 7209 }
7d10b47d 7210 end_noreorder ();
252b5132
RH
7211 break;
7212
771c7ce4 7213 case M_DROL:
fef14a42 7214 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
7215 {
7216 if (dreg == sreg)
7217 {
7218 tempreg = AT;
7219 used_at = 1;
7220 }
7221 else
7222 {
7223 tempreg = dreg;
82dd0097 7224 }
67c0d1eb
RS
7225 macro_build (NULL, "dnegu", "d,w", tempreg, treg);
7226 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
8fc2e39e 7227 break;
82dd0097 7228 }
8fc2e39e 7229 used_at = 1;
67c0d1eb
RS
7230 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
7231 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
7232 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
7233 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7234 break;
7235
252b5132 7236 case M_ROL:
fef14a42 7237 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097
CD
7238 {
7239 if (dreg == sreg)
7240 {
7241 tempreg = AT;
7242 used_at = 1;
7243 }
7244 else
7245 {
7246 tempreg = dreg;
82dd0097 7247 }
67c0d1eb
RS
7248 macro_build (NULL, "negu", "d,w", tempreg, treg);
7249 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
8fc2e39e 7250 break;
82dd0097 7251 }
8fc2e39e 7252 used_at = 1;
67c0d1eb
RS
7253 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
7254 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
7255 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
7256 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
7257 break;
7258
771c7ce4
TS
7259 case M_DROL_I:
7260 {
7261 unsigned int rot;
82dd0097 7262 char *l, *r;
771c7ce4
TS
7263
7264 if (imm_expr.X_op != O_constant)
82dd0097 7265 as_bad (_("Improper rotate count"));
771c7ce4 7266 rot = imm_expr.X_add_number & 0x3f;
fef14a42 7267 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
7268 {
7269 rot = (64 - rot) & 0x3f;
7270 if (rot >= 32)
67c0d1eb 7271 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
60b63b72 7272 else
67c0d1eb 7273 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
8fc2e39e 7274 break;
60b63b72 7275 }
483fc7cd 7276 if (rot == 0)
483fc7cd 7277 {
67c0d1eb 7278 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
8fc2e39e 7279 break;
483fc7cd 7280 }
82dd0097
CD
7281 l = (rot < 0x20) ? "dsll" : "dsll32";
7282 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
7283 rot &= 0x1f;
8fc2e39e 7284 used_at = 1;
67c0d1eb
RS
7285 macro_build (NULL, l, "d,w,<", AT, sreg, rot);
7286 macro_build (NULL, r, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7287 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7288 }
7289 break;
7290
252b5132 7291 case M_ROL_I:
771c7ce4
TS
7292 {
7293 unsigned int rot;
7294
7295 if (imm_expr.X_op != O_constant)
82dd0097 7296 as_bad (_("Improper rotate count"));
771c7ce4 7297 rot = imm_expr.X_add_number & 0x1f;
fef14a42 7298 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 7299 {
67c0d1eb 7300 macro_build (NULL, "ror", "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
8fc2e39e 7301 break;
60b63b72 7302 }
483fc7cd 7303 if (rot == 0)
483fc7cd 7304 {
67c0d1eb 7305 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
8fc2e39e 7306 break;
483fc7cd 7307 }
8fc2e39e 7308 used_at = 1;
67c0d1eb
RS
7309 macro_build (NULL, "sll", "d,w,<", AT, sreg, rot);
7310 macro_build (NULL, "srl", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7311 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7312 }
7313 break;
7314
7315 case M_DROR:
fef14a42 7316 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 7317 {
67c0d1eb 7318 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
8fc2e39e 7319 break;
82dd0097 7320 }
8fc2e39e 7321 used_at = 1;
67c0d1eb
RS
7322 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
7323 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
7324 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
7325 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
7326 break;
7327
7328 case M_ROR:
fef14a42 7329 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 7330 {
67c0d1eb 7331 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
8fc2e39e 7332 break;
82dd0097 7333 }
8fc2e39e 7334 used_at = 1;
67c0d1eb
RS
7335 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
7336 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
7337 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
7338 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
7339 break;
7340
771c7ce4
TS
7341 case M_DROR_I:
7342 {
7343 unsigned int rot;
82dd0097 7344 char *l, *r;
771c7ce4
TS
7345
7346 if (imm_expr.X_op != O_constant)
82dd0097 7347 as_bad (_("Improper rotate count"));
771c7ce4 7348 rot = imm_expr.X_add_number & 0x3f;
fef14a42 7349 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
7350 {
7351 if (rot >= 32)
67c0d1eb 7352 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
82dd0097 7353 else
67c0d1eb 7354 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
8fc2e39e 7355 break;
82dd0097 7356 }
483fc7cd 7357 if (rot == 0)
483fc7cd 7358 {
67c0d1eb 7359 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
8fc2e39e 7360 break;
483fc7cd 7361 }
82dd0097
CD
7362 r = (rot < 0x20) ? "dsrl" : "dsrl32";
7363 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7364 rot &= 0x1f;
8fc2e39e 7365 used_at = 1;
67c0d1eb
RS
7366 macro_build (NULL, r, "d,w,<", AT, sreg, rot);
7367 macro_build (NULL, l, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7368 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7369 }
7370 break;
7371
252b5132 7372 case M_ROR_I:
771c7ce4
TS
7373 {
7374 unsigned int rot;
7375
7376 if (imm_expr.X_op != O_constant)
82dd0097 7377 as_bad (_("Improper rotate count"));
771c7ce4 7378 rot = imm_expr.X_add_number & 0x1f;
fef14a42 7379 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 7380 {
67c0d1eb 7381 macro_build (NULL, "ror", "d,w,<", dreg, sreg, rot);
8fc2e39e 7382 break;
82dd0097 7383 }
483fc7cd 7384 if (rot == 0)
483fc7cd 7385 {
67c0d1eb 7386 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
8fc2e39e 7387 break;
483fc7cd 7388 }
8fc2e39e 7389 used_at = 1;
67c0d1eb
RS
7390 macro_build (NULL, "srl", "d,w,<", AT, sreg, rot);
7391 macro_build (NULL, "sll", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7392 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4 7393 }
252b5132
RH
7394 break;
7395
7396 case M_S_DOB:
fef14a42 7397 if (mips_opts.arch == CPU_R4650)
252b5132
RH
7398 {
7399 as_bad (_("opcode not supported on this processor"));
8fc2e39e 7400 break;
252b5132 7401 }
e7af610e 7402 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7403 /* Even on a big endian machine $fn comes before $fn+1. We have
7404 to adjust when storing to memory. */
67c0d1eb
RS
7405 macro_build (&offset_expr, "swc1", "T,o(b)",
7406 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
252b5132 7407 offset_expr.X_add_number += 4;
67c0d1eb
RS
7408 macro_build (&offset_expr, "swc1", "T,o(b)",
7409 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
8fc2e39e 7410 break;
252b5132
RH
7411
7412 case M_SEQ:
7413 if (sreg == 0)
67c0d1eb 7414 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
252b5132 7415 else if (treg == 0)
67c0d1eb 7416 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7417 else
7418 {
67c0d1eb
RS
7419 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7420 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
252b5132 7421 }
8fc2e39e 7422 break;
252b5132
RH
7423
7424 case M_SEQ_I:
7425 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7426 {
67c0d1eb 7427 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 7428 break;
252b5132
RH
7429 }
7430 if (sreg == 0)
7431 {
7432 as_warn (_("Instruction %s: result is always false"),
7433 ip->insn_mo->name);
67c0d1eb 7434 move_register (dreg, 0);
8fc2e39e 7435 break;
252b5132
RH
7436 }
7437 if (imm_expr.X_op == O_constant
7438 && imm_expr.X_add_number >= 0
7439 && imm_expr.X_add_number < 0x10000)
7440 {
67c0d1eb 7441 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7442 }
7443 else if (imm_expr.X_op == O_constant
7444 && imm_expr.X_add_number > -0x8000
7445 && imm_expr.X_add_number < 0)
7446 {
7447 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7448 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 7449 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7450 }
7451 else
7452 {
67c0d1eb
RS
7453 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7454 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
7455 used_at = 1;
7456 }
67c0d1eb 7457 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 7458 break;
252b5132
RH
7459
7460 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
7461 s = "slt";
7462 goto sge;
7463 case M_SGEU:
7464 s = "sltu";
7465 sge:
67c0d1eb
RS
7466 macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
7467 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 7468 break;
252b5132
RH
7469
7470 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
7471 case M_SGEU_I:
7472 if (imm_expr.X_op == O_constant
7473 && imm_expr.X_add_number >= -0x8000
7474 && imm_expr.X_add_number < 0x8000)
7475 {
67c0d1eb
RS
7476 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
7477 dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7478 }
7479 else
7480 {
67c0d1eb
RS
7481 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7482 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
7483 dreg, sreg, AT);
252b5132
RH
7484 used_at = 1;
7485 }
67c0d1eb 7486 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 7487 break;
252b5132
RH
7488
7489 case M_SGT: /* sreg > treg <==> treg < sreg */
7490 s = "slt";
7491 goto sgt;
7492 case M_SGTU:
7493 s = "sltu";
7494 sgt:
67c0d1eb 7495 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
8fc2e39e 7496 break;
252b5132
RH
7497
7498 case M_SGT_I: /* sreg > I <==> I < sreg */
7499 s = "slt";
7500 goto sgti;
7501 case M_SGTU_I:
7502 s = "sltu";
7503 sgti:
8fc2e39e 7504 used_at = 1;
67c0d1eb
RS
7505 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7506 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
252b5132
RH
7507 break;
7508
2396cfb9 7509 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
252b5132
RH
7510 s = "slt";
7511 goto sle;
7512 case M_SLEU:
7513 s = "sltu";
7514 sle:
67c0d1eb
RS
7515 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7516 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 7517 break;
252b5132 7518
2396cfb9 7519 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
252b5132
RH
7520 s = "slt";
7521 goto slei;
7522 case M_SLEU_I:
7523 s = "sltu";
7524 slei:
8fc2e39e 7525 used_at = 1;
67c0d1eb
RS
7526 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7527 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7528 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
7529 break;
7530
7531 case M_SLT_I:
7532 if (imm_expr.X_op == O_constant
7533 && imm_expr.X_add_number >= -0x8000
7534 && imm_expr.X_add_number < 0x8000)
7535 {
67c0d1eb 7536 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 7537 break;
252b5132 7538 }
8fc2e39e 7539 used_at = 1;
67c0d1eb
RS
7540 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7541 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
252b5132
RH
7542 break;
7543
7544 case M_SLTU_I:
7545 if (imm_expr.X_op == O_constant
7546 && imm_expr.X_add_number >= -0x8000
7547 && imm_expr.X_add_number < 0x8000)
7548 {
67c0d1eb 7549 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
17a2f251 7550 BFD_RELOC_LO16);
8fc2e39e 7551 break;
252b5132 7552 }
8fc2e39e 7553 used_at = 1;
67c0d1eb
RS
7554 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7555 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7556 break;
7557
7558 case M_SNE:
7559 if (sreg == 0)
67c0d1eb 7560 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
252b5132 7561 else if (treg == 0)
67c0d1eb 7562 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
252b5132
RH
7563 else
7564 {
67c0d1eb
RS
7565 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7566 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
252b5132 7567 }
8fc2e39e 7568 break;
252b5132
RH
7569
7570 case M_SNE_I:
7571 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7572 {
67c0d1eb 7573 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
8fc2e39e 7574 break;
252b5132
RH
7575 }
7576 if (sreg == 0)
7577 {
7578 as_warn (_("Instruction %s: result is always true"),
7579 ip->insn_mo->name);
67c0d1eb
RS
7580 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
7581 dreg, 0, BFD_RELOC_LO16);
8fc2e39e 7582 break;
252b5132
RH
7583 }
7584 if (imm_expr.X_op == O_constant
7585 && imm_expr.X_add_number >= 0
7586 && imm_expr.X_add_number < 0x10000)
7587 {
67c0d1eb 7588 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7589 }
7590 else if (imm_expr.X_op == O_constant
7591 && imm_expr.X_add_number > -0x8000
7592 && imm_expr.X_add_number < 0)
7593 {
7594 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7595 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 7596 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7597 }
7598 else
7599 {
67c0d1eb
RS
7600 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7601 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
7602 used_at = 1;
7603 }
67c0d1eb 7604 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
8fc2e39e 7605 break;
252b5132
RH
7606
7607 case M_DSUB_I:
7608 dbl = 1;
7609 case M_SUB_I:
7610 if (imm_expr.X_op == O_constant
7611 && imm_expr.X_add_number > -0x8000
7612 && imm_expr.X_add_number <= 0x8000)
7613 {
7614 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb
RS
7615 macro_build (&imm_expr, dbl ? "daddi" : "addi", "t,r,j",
7616 dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 7617 break;
252b5132 7618 }
8fc2e39e 7619 used_at = 1;
67c0d1eb
RS
7620 load_register (AT, &imm_expr, dbl);
7621 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
252b5132
RH
7622 break;
7623
7624 case M_DSUBU_I:
7625 dbl = 1;
7626 case M_SUBU_I:
7627 if (imm_expr.X_op == O_constant
7628 && imm_expr.X_add_number > -0x8000
7629 && imm_expr.X_add_number <= 0x8000)
7630 {
7631 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb
RS
7632 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "t,r,j",
7633 dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 7634 break;
252b5132 7635 }
8fc2e39e 7636 used_at = 1;
67c0d1eb
RS
7637 load_register (AT, &imm_expr, dbl);
7638 macro_build (NULL, dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7639 break;
7640
7641 case M_TEQ_I:
7642 s = "teq";
7643 goto trap;
7644 case M_TGE_I:
7645 s = "tge";
7646 goto trap;
7647 case M_TGEU_I:
7648 s = "tgeu";
7649 goto trap;
7650 case M_TLT_I:
7651 s = "tlt";
7652 goto trap;
7653 case M_TLTU_I:
7654 s = "tltu";
7655 goto trap;
7656 case M_TNE_I:
7657 s = "tne";
7658 trap:
8fc2e39e 7659 used_at = 1;
67c0d1eb
RS
7660 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7661 macro_build (NULL, s, "s,t", sreg, AT);
252b5132
RH
7662 break;
7663
252b5132 7664 case M_TRUNCWS:
43841e91 7665 case M_TRUNCWD:
e7af610e 7666 assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 7667 used_at = 1;
252b5132
RH
7668 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7669 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7670
7671 /*
7672 * Is the double cfc1 instruction a bug in the mips assembler;
7673 * or is there a reason for it?
7674 */
7d10b47d 7675 start_noreorder ();
67c0d1eb
RS
7676 macro_build (NULL, "cfc1", "t,G", treg, RA);
7677 macro_build (NULL, "cfc1", "t,G", treg, RA);
7678 macro_build (NULL, "nop", "");
252b5132 7679 expr1.X_add_number = 3;
67c0d1eb 7680 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
252b5132 7681 expr1.X_add_number = 2;
67c0d1eb
RS
7682 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
7683 macro_build (NULL, "ctc1", "t,G", AT, RA);
7684 macro_build (NULL, "nop", "");
7685 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
7686 dreg, sreg);
7687 macro_build (NULL, "ctc1", "t,G", treg, RA);
7688 macro_build (NULL, "nop", "");
7d10b47d 7689 end_noreorder ();
252b5132
RH
7690 break;
7691
7692 case M_ULH:
7693 s = "lb";
7694 goto ulh;
7695 case M_ULHU:
7696 s = "lbu";
7697 ulh:
8fc2e39e 7698 used_at = 1;
252b5132
RH
7699 if (offset_expr.X_add_number >= 0x7fff)
7700 as_bad (_("operand overflow"));
252b5132 7701 if (! target_big_endian)
f9419b05 7702 ++offset_expr.X_add_number;
67c0d1eb 7703 macro_build (&offset_expr, s, "t,o(b)", AT, BFD_RELOC_LO16, breg);
252b5132 7704 if (! target_big_endian)
f9419b05 7705 --offset_expr.X_add_number;
252b5132 7706 else
f9419b05 7707 ++offset_expr.X_add_number;
67c0d1eb
RS
7708 macro_build (&offset_expr, "lbu", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7709 macro_build (NULL, "sll", "d,w,<", AT, AT, 8);
7710 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7711 break;
7712
7713 case M_ULD:
7714 s = "ldl";
7715 s2 = "ldr";
7716 off = 7;
7717 goto ulw;
7718 case M_ULW:
7719 s = "lwl";
7720 s2 = "lwr";
7721 off = 3;
7722 ulw:
7723 if (offset_expr.X_add_number >= 0x8000 - off)
7724 as_bad (_("operand overflow"));
af22f5b2
CD
7725 if (treg != breg)
7726 tempreg = treg;
7727 else
8fc2e39e
TS
7728 {
7729 used_at = 1;
7730 tempreg = AT;
7731 }
252b5132
RH
7732 if (! target_big_endian)
7733 offset_expr.X_add_number += off;
67c0d1eb 7734 macro_build (&offset_expr, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
252b5132
RH
7735 if (! target_big_endian)
7736 offset_expr.X_add_number -= off;
7737 else
7738 offset_expr.X_add_number += off;
67c0d1eb 7739 macro_build (&offset_expr, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
af22f5b2
CD
7740
7741 /* If necessary, move the result in tempreg the final destination. */
7742 if (treg == tempreg)
8fc2e39e 7743 break;
af22f5b2 7744 /* Protect second load's delay slot. */
017315e4 7745 load_delay_nop ();
67c0d1eb 7746 move_register (treg, tempreg);
af22f5b2 7747 break;
252b5132
RH
7748
7749 case M_ULD_A:
7750 s = "ldl";
7751 s2 = "ldr";
7752 off = 7;
7753 goto ulwa;
7754 case M_ULW_A:
7755 s = "lwl";
7756 s2 = "lwr";
7757 off = 3;
7758 ulwa:
d6bc6245 7759 used_at = 1;
67c0d1eb 7760 load_address (AT, &offset_expr, &used_at);
252b5132 7761 if (breg != 0)
67c0d1eb 7762 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7763 if (! target_big_endian)
7764 expr1.X_add_number = off;
7765 else
7766 expr1.X_add_number = 0;
67c0d1eb 7767 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7768 if (! target_big_endian)
7769 expr1.X_add_number = 0;
7770 else
7771 expr1.X_add_number = off;
67c0d1eb 7772 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7773 break;
7774
7775 case M_ULH_A:
7776 case M_ULHU_A:
d6bc6245 7777 used_at = 1;
67c0d1eb 7778 load_address (AT, &offset_expr, &used_at);
252b5132 7779 if (breg != 0)
67c0d1eb 7780 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7781 if (target_big_endian)
7782 expr1.X_add_number = 0;
67c0d1eb 7783 macro_build (&expr1, mask == M_ULH_A ? "lb" : "lbu", "t,o(b)",
17a2f251 7784 treg, BFD_RELOC_LO16, AT);
252b5132
RH
7785 if (target_big_endian)
7786 expr1.X_add_number = 1;
7787 else
7788 expr1.X_add_number = 0;
67c0d1eb
RS
7789 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7790 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7791 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7792 break;
7793
7794 case M_USH:
8fc2e39e 7795 used_at = 1;
252b5132
RH
7796 if (offset_expr.X_add_number >= 0x7fff)
7797 as_bad (_("operand overflow"));
7798 if (target_big_endian)
f9419b05 7799 ++offset_expr.X_add_number;
67c0d1eb
RS
7800 macro_build (&offset_expr, "sb", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7801 macro_build (NULL, "srl", "d,w,<", AT, treg, 8);
252b5132 7802 if (target_big_endian)
f9419b05 7803 --offset_expr.X_add_number;
252b5132 7804 else
f9419b05 7805 ++offset_expr.X_add_number;
67c0d1eb 7806 macro_build (&offset_expr, "sb", "t,o(b)", AT, BFD_RELOC_LO16, breg);
252b5132
RH
7807 break;
7808
7809 case M_USD:
7810 s = "sdl";
7811 s2 = "sdr";
7812 off = 7;
7813 goto usw;
7814 case M_USW:
7815 s = "swl";
7816 s2 = "swr";
7817 off = 3;
7818 usw:
7819 if (offset_expr.X_add_number >= 0x8000 - off)
7820 as_bad (_("operand overflow"));
7821 if (! target_big_endian)
7822 offset_expr.X_add_number += off;
67c0d1eb 7823 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
252b5132
RH
7824 if (! target_big_endian)
7825 offset_expr.X_add_number -= off;
7826 else
7827 offset_expr.X_add_number += off;
67c0d1eb 7828 macro_build (&offset_expr, s2, "t,o(b)", treg, BFD_RELOC_LO16, breg);
8fc2e39e 7829 break;
252b5132
RH
7830
7831 case M_USD_A:
7832 s = "sdl";
7833 s2 = "sdr";
7834 off = 7;
7835 goto uswa;
7836 case M_USW_A:
7837 s = "swl";
7838 s2 = "swr";
7839 off = 3;
7840 uswa:
d6bc6245 7841 used_at = 1;
67c0d1eb 7842 load_address (AT, &offset_expr, &used_at);
252b5132 7843 if (breg != 0)
67c0d1eb 7844 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7845 if (! target_big_endian)
7846 expr1.X_add_number = off;
7847 else
7848 expr1.X_add_number = 0;
67c0d1eb 7849 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7850 if (! target_big_endian)
7851 expr1.X_add_number = 0;
7852 else
7853 expr1.X_add_number = off;
67c0d1eb 7854 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7855 break;
7856
7857 case M_USH_A:
d6bc6245 7858 used_at = 1;
67c0d1eb 7859 load_address (AT, &offset_expr, &used_at);
252b5132 7860 if (breg != 0)
67c0d1eb 7861 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7862 if (! target_big_endian)
7863 expr1.X_add_number = 0;
67c0d1eb
RS
7864 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7865 macro_build (NULL, "srl", "d,w,<", treg, treg, 8);
252b5132
RH
7866 if (! target_big_endian)
7867 expr1.X_add_number = 1;
7868 else
7869 expr1.X_add_number = 0;
67c0d1eb 7870 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7871 if (! target_big_endian)
7872 expr1.X_add_number = 0;
7873 else
7874 expr1.X_add_number = 1;
67c0d1eb
RS
7875 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7876 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7877 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7878 break;
7879
7880 default:
7881 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 7882 are added dynamically. */
252b5132
RH
7883 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7884 break;
7885 }
8fc2e39e
TS
7886 if (mips_opts.noat && used_at)
7887 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
7888}
7889
7890/* Implement macros in mips16 mode. */
7891
7892static void
17a2f251 7893mips16_macro (struct mips_cl_insn *ip)
252b5132
RH
7894{
7895 int mask;
7896 int xreg, yreg, zreg, tmp;
252b5132
RH
7897 expressionS expr1;
7898 int dbl;
7899 const char *s, *s2, *s3;
7900
7901 mask = ip->insn_mo->mask;
7902
bf12938e
RS
7903 xreg = MIPS16_EXTRACT_OPERAND (RX, *ip);
7904 yreg = MIPS16_EXTRACT_OPERAND (RY, *ip);
7905 zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip);
252b5132 7906
252b5132
RH
7907 expr1.X_op = O_constant;
7908 expr1.X_op_symbol = NULL;
7909 expr1.X_add_symbol = NULL;
7910 expr1.X_add_number = 1;
7911
7912 dbl = 0;
7913
7914 switch (mask)
7915 {
7916 default:
7917 internalError ();
7918
7919 case M_DDIV_3:
7920 dbl = 1;
7921 case M_DIV_3:
7922 s = "mflo";
7923 goto do_div3;
7924 case M_DREM_3:
7925 dbl = 1;
7926 case M_REM_3:
7927 s = "mfhi";
7928 do_div3:
7d10b47d 7929 start_noreorder ();
67c0d1eb 7930 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
252b5132 7931 expr1.X_add_number = 2;
67c0d1eb
RS
7932 macro_build (&expr1, "bnez", "x,p", yreg);
7933 macro_build (NULL, "break", "6", 7);
bdaaa2e1 7934
252b5132
RH
7935 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7936 since that causes an overflow. We should do that as well,
7937 but I don't see how to do the comparisons without a temporary
7938 register. */
7d10b47d 7939 end_noreorder ();
67c0d1eb 7940 macro_build (NULL, s, "x", zreg);
252b5132
RH
7941 break;
7942
7943 case M_DIVU_3:
7944 s = "divu";
7945 s2 = "mflo";
7946 goto do_divu3;
7947 case M_REMU_3:
7948 s = "divu";
7949 s2 = "mfhi";
7950 goto do_divu3;
7951 case M_DDIVU_3:
7952 s = "ddivu";
7953 s2 = "mflo";
7954 goto do_divu3;
7955 case M_DREMU_3:
7956 s = "ddivu";
7957 s2 = "mfhi";
7958 do_divu3:
7d10b47d 7959 start_noreorder ();
67c0d1eb 7960 macro_build (NULL, s, "0,x,y", xreg, yreg);
252b5132 7961 expr1.X_add_number = 2;
67c0d1eb
RS
7962 macro_build (&expr1, "bnez", "x,p", yreg);
7963 macro_build (NULL, "break", "6", 7);
7d10b47d 7964 end_noreorder ();
67c0d1eb 7965 macro_build (NULL, s2, "x", zreg);
252b5132
RH
7966 break;
7967
7968 case M_DMUL:
7969 dbl = 1;
7970 case M_MUL:
67c0d1eb
RS
7971 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7972 macro_build (NULL, "mflo", "x", zreg);
8fc2e39e 7973 break;
252b5132
RH
7974
7975 case M_DSUBU_I:
7976 dbl = 1;
7977 goto do_subu;
7978 case M_SUBU_I:
7979 do_subu:
7980 if (imm_expr.X_op != O_constant)
7981 as_bad (_("Unsupported large constant"));
7982 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7983 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
252b5132
RH
7984 break;
7985
7986 case M_SUBU_I_2:
7987 if (imm_expr.X_op != O_constant)
7988 as_bad (_("Unsupported large constant"));
7989 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7990 macro_build (&imm_expr, "addiu", "x,k", xreg);
252b5132
RH
7991 break;
7992
7993 case M_DSUBU_I_2:
7994 if (imm_expr.X_op != O_constant)
7995 as_bad (_("Unsupported large constant"));
7996 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7997 macro_build (&imm_expr, "daddiu", "y,j", yreg);
252b5132
RH
7998 break;
7999
8000 case M_BEQ:
8001 s = "cmp";
8002 s2 = "bteqz";
8003 goto do_branch;
8004 case M_BNE:
8005 s = "cmp";
8006 s2 = "btnez";
8007 goto do_branch;
8008 case M_BLT:
8009 s = "slt";
8010 s2 = "btnez";
8011 goto do_branch;
8012 case M_BLTU:
8013 s = "sltu";
8014 s2 = "btnez";
8015 goto do_branch;
8016 case M_BLE:
8017 s = "slt";
8018 s2 = "bteqz";
8019 goto do_reverse_branch;
8020 case M_BLEU:
8021 s = "sltu";
8022 s2 = "bteqz";
8023 goto do_reverse_branch;
8024 case M_BGE:
8025 s = "slt";
8026 s2 = "bteqz";
8027 goto do_branch;
8028 case M_BGEU:
8029 s = "sltu";
8030 s2 = "bteqz";
8031 goto do_branch;
8032 case M_BGT:
8033 s = "slt";
8034 s2 = "btnez";
8035 goto do_reverse_branch;
8036 case M_BGTU:
8037 s = "sltu";
8038 s2 = "btnez";
8039
8040 do_reverse_branch:
8041 tmp = xreg;
8042 xreg = yreg;
8043 yreg = tmp;
8044
8045 do_branch:
67c0d1eb
RS
8046 macro_build (NULL, s, "x,y", xreg, yreg);
8047 macro_build (&offset_expr, s2, "p");
252b5132
RH
8048 break;
8049
8050 case M_BEQ_I:
8051 s = "cmpi";
8052 s2 = "bteqz";
8053 s3 = "x,U";
8054 goto do_branch_i;
8055 case M_BNE_I:
8056 s = "cmpi";
8057 s2 = "btnez";
8058 s3 = "x,U";
8059 goto do_branch_i;
8060 case M_BLT_I:
8061 s = "slti";
8062 s2 = "btnez";
8063 s3 = "x,8";
8064 goto do_branch_i;
8065 case M_BLTU_I:
8066 s = "sltiu";
8067 s2 = "btnez";
8068 s3 = "x,8";
8069 goto do_branch_i;
8070 case M_BLE_I:
8071 s = "slti";
8072 s2 = "btnez";
8073 s3 = "x,8";
8074 goto do_addone_branch_i;
8075 case M_BLEU_I:
8076 s = "sltiu";
8077 s2 = "btnez";
8078 s3 = "x,8";
8079 goto do_addone_branch_i;
8080 case M_BGE_I:
8081 s = "slti";
8082 s2 = "bteqz";
8083 s3 = "x,8";
8084 goto do_branch_i;
8085 case M_BGEU_I:
8086 s = "sltiu";
8087 s2 = "bteqz";
8088 s3 = "x,8";
8089 goto do_branch_i;
8090 case M_BGT_I:
8091 s = "slti";
8092 s2 = "bteqz";
8093 s3 = "x,8";
8094 goto do_addone_branch_i;
8095 case M_BGTU_I:
8096 s = "sltiu";
8097 s2 = "bteqz";
8098 s3 = "x,8";
8099
8100 do_addone_branch_i:
8101 if (imm_expr.X_op != O_constant)
8102 as_bad (_("Unsupported large constant"));
8103 ++imm_expr.X_add_number;
8104
8105 do_branch_i:
67c0d1eb
RS
8106 macro_build (&imm_expr, s, s3, xreg);
8107 macro_build (&offset_expr, s2, "p");
252b5132
RH
8108 break;
8109
8110 case M_ABS:
8111 expr1.X_add_number = 0;
67c0d1eb 8112 macro_build (&expr1, "slti", "x,8", yreg);
252b5132 8113 if (xreg != yreg)
67c0d1eb 8114 move_register (xreg, yreg);
252b5132 8115 expr1.X_add_number = 2;
67c0d1eb
RS
8116 macro_build (&expr1, "bteqz", "p");
8117 macro_build (NULL, "neg", "x,w", xreg, xreg);
252b5132
RH
8118 }
8119}
8120
8121/* For consistency checking, verify that all bits are specified either
8122 by the match/mask part of the instruction definition, or by the
8123 operand list. */
8124static int
17a2f251 8125validate_mips_insn (const struct mips_opcode *opc)
252b5132
RH
8126{
8127 const char *p = opc->args;
8128 char c;
8129 unsigned long used_bits = opc->mask;
8130
8131 if ((used_bits & opc->match) != opc->match)
8132 {
8133 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
8134 opc->name, opc->args);
8135 return 0;
8136 }
8137#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
8138 while (*p)
8139 switch (c = *p++)
8140 {
8141 case ',': break;
8142 case '(': break;
8143 case ')': break;
af7ee8bf
CD
8144 case '+':
8145 switch (c = *p++)
8146 {
9bcd4f99
TS
8147 case '1': USE_BITS (OP_MASK_UDI1, OP_SH_UDI1); break;
8148 case '2': USE_BITS (OP_MASK_UDI2, OP_SH_UDI2); break;
8149 case '3': USE_BITS (OP_MASK_UDI3, OP_SH_UDI3); break;
8150 case '4': USE_BITS (OP_MASK_UDI4, OP_SH_UDI4); break;
af7ee8bf
CD
8151 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8152 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
8153 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
bbcc0807
CD
8154 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
8155 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
5f74bc13
CD
8156 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8157 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
8158 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
8159 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
8160 case 'I': break;
ef2e4d86
CF
8161 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8162 case 'T': USE_BITS (OP_MASK_RT, OP_SH_RT);
8163 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
af7ee8bf
CD
8164 default:
8165 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8166 c, opc->name, opc->args);
8167 return 0;
8168 }
8169 break;
252b5132
RH
8170 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8171 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8172 case 'A': break;
4372b673 8173 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
8174 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
8175 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
8176 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8177 case 'F': break;
8178 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 8179 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 8180 case 'I': break;
e972090a 8181 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
af7ee8bf 8182 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
8183 case 'L': break;
8184 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
8185 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
deec1734
CD
8186 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
8187 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
8188 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
8189 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
8190 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8191 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8192 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8193 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
deec1734
CD
8194 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
8195 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8196 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
8197 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
8198 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8199 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
8200 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
8201 case 'f': break;
8202 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
8203 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8204 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8205 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
8206 case 'l': break;
8207 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8208 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8209 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
8210 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8211 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8212 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8213 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8214 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8215 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8216 case 'x': break;
8217 case 'z': break;
8218 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
8219 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
8220 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
60b63b72
RS
8221 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
8222 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
8223 case '[': break;
8224 case ']': break;
74cd071d
CF
8225 case '3': USE_BITS (OP_MASK_SA3, OP_SH_SA3); break;
8226 case '4': USE_BITS (OP_MASK_SA4, OP_SH_SA4); break;
8227 case '5': USE_BITS (OP_MASK_IMM8, OP_SH_IMM8); break;
8228 case '6': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8229 case '7': USE_BITS (OP_MASK_DSPACC, OP_SH_DSPACC); break;
8230 case '8': USE_BITS (OP_MASK_WRDSP, OP_SH_WRDSP); break;
8231 case '9': USE_BITS (OP_MASK_DSPACC_S, OP_SH_DSPACC_S);break;
8232 case '0': USE_BITS (OP_MASK_DSPSFT, OP_SH_DSPSFT); break;
8233 case '\'': USE_BITS (OP_MASK_RDDSP, OP_SH_RDDSP); break;
8234 case ':': USE_BITS (OP_MASK_DSPSFT_7, OP_SH_DSPSFT_7);break;
8235 case '@': USE_BITS (OP_MASK_IMM10, OP_SH_IMM10); break;
ef2e4d86
CF
8236 case '!': USE_BITS (OP_MASK_MT_U, OP_SH_MT_U); break;
8237 case '$': USE_BITS (OP_MASK_MT_H, OP_SH_MT_H); break;
8238 case '*': USE_BITS (OP_MASK_MTACC_T, OP_SH_MTACC_T); break;
8239 case '&': USE_BITS (OP_MASK_MTACC_D, OP_SH_MTACC_D); break;
8240 case 'g': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
8241 default:
8242 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
8243 c, opc->name, opc->args);
8244 return 0;
8245 }
8246#undef USE_BITS
8247 if (used_bits != 0xffffffff)
8248 {
8249 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
8250 ~used_bits & 0xffffffff, opc->name, opc->args);
8251 return 0;
8252 }
8253 return 1;
8254}
8255
9bcd4f99
TS
8256/* UDI immediates. */
8257struct mips_immed {
8258 char type;
8259 unsigned int shift;
8260 unsigned long mask;
8261 const char * desc;
8262};
8263
8264static const struct mips_immed mips_immed[] = {
8265 { '1', OP_SH_UDI1, OP_MASK_UDI1, 0},
8266 { '2', OP_SH_UDI2, OP_MASK_UDI2, 0},
8267 { '3', OP_SH_UDI3, OP_MASK_UDI3, 0},
8268 { '4', OP_SH_UDI4, OP_MASK_UDI4, 0},
8269 { 0,0,0,0 }
8270};
8271
7455baf8
TS
8272/* Check whether an odd floating-point register is allowed. */
8273static int
8274mips_oddfpreg_ok (const struct mips_opcode *insn, int argnum)
8275{
8276 const char *s = insn->name;
8277
8278 if (insn->pinfo == INSN_MACRO)
8279 /* Let a macro pass, we'll catch it later when it is expanded. */
8280 return 1;
8281
8282 if (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa))
8283 {
8284 /* Allow odd registers for single-precision ops. */
8285 switch (insn->pinfo & (FP_S | FP_D))
8286 {
8287 case FP_S:
8288 case 0:
8289 return 1; /* both single precision - ok */
8290 case FP_D:
8291 return 0; /* both double precision - fail */
8292 default:
8293 break;
8294 }
8295
8296 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
8297 s = strchr (insn->name, '.');
8298 if (argnum == 2)
8299 s = s != NULL ? strchr (s + 1, '.') : NULL;
8300 return (s != NULL && (s[1] == 'w' || s[1] == 's'));
8301 }
8302
8303 /* Single-precision coprocessor loads and moves are OK too. */
8304 if ((insn->pinfo & FP_S)
8305 && (insn->pinfo & (INSN_COPROC_MEMORY_DELAY | INSN_STORE_MEMORY
8306 | INSN_LOAD_COPROC_DELAY | INSN_COPROC_MOVE_DELAY)))
8307 return 1;
8308
8309 return 0;
8310}
8311
252b5132
RH
8312/* This routine assembles an instruction into its binary format. As a
8313 side effect, it sets one of the global variables imm_reloc or
8314 offset_reloc to the type of relocation to do if one of the operands
8315 is an address expression. */
8316
8317static void
17a2f251 8318mips_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
8319{
8320 char *s;
8321 const char *args;
43841e91 8322 char c = 0;
252b5132
RH
8323 struct mips_opcode *insn;
8324 char *argsStart;
8325 unsigned int regno;
8326 unsigned int lastregno = 0;
af7ee8bf 8327 unsigned int lastpos = 0;
071742cf 8328 unsigned int limlo, limhi;
252b5132
RH
8329 char *s_reset;
8330 char save_c = 0;
74cd071d 8331 offsetT min_range, max_range;
707bfff6
TS
8332 int argnum;
8333 unsigned int rtype;
252b5132
RH
8334
8335 insn_error = NULL;
8336
8337 /* If the instruction contains a '.', we first try to match an instruction
8338 including the '.'. Then we try again without the '.'. */
8339 insn = NULL;
3882b010 8340 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
252b5132
RH
8341 continue;
8342
8343 /* If we stopped on whitespace, then replace the whitespace with null for
8344 the call to hash_find. Save the character we replaced just in case we
8345 have to re-parse the instruction. */
3882b010 8346 if (ISSPACE (*s))
252b5132
RH
8347 {
8348 save_c = *s;
8349 *s++ = '\0';
8350 }
bdaaa2e1 8351
252b5132
RH
8352 insn = (struct mips_opcode *) hash_find (op_hash, str);
8353
8354 /* If we didn't find the instruction in the opcode table, try again, but
8355 this time with just the instruction up to, but not including the
8356 first '.'. */
8357 if (insn == NULL)
8358 {
bdaaa2e1 8359 /* Restore the character we overwrite above (if any). */
252b5132
RH
8360 if (save_c)
8361 *(--s) = save_c;
8362
8363 /* Scan up to the first '.' or whitespace. */
3882b010
L
8364 for (s = str;
8365 *s != '\0' && *s != '.' && !ISSPACE (*s);
8366 ++s)
252b5132
RH
8367 continue;
8368
8369 /* If we did not find a '.', then we can quit now. */
8370 if (*s != '.')
8371 {
8372 insn_error = "unrecognized opcode";
8373 return;
8374 }
8375
8376 /* Lookup the instruction in the hash table. */
8377 *s++ = '\0';
8378 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8379 {
8380 insn_error = "unrecognized opcode";
8381 return;
8382 }
252b5132
RH
8383 }
8384
8385 argsStart = s;
8386 for (;;)
8387 {
b34976b6 8388 bfd_boolean ok;
252b5132
RH
8389
8390 assert (strcmp (insn->name, str) == 0);
8391
1f25f5d3
CD
8392 if (OPCODE_IS_MEMBER (insn,
8393 (mips_opts.isa
9b3f89ee
TS
8394 /* We don't check for mips_opts.mips16 here since
8395 we want to allow jalx if -mips16 was specified
8396 on the command line. */
3396de36 8397 | (file_ase_mips16 ? INSN_MIPS16 : 0)
deec1734 8398 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
74cd071d 8399 | (mips_opts.ase_dsp ? INSN_DSP : 0)
65263ce3
TS
8400 | ((mips_opts.ase_dsp && ISA_SUPPORTS_DSP64_ASE)
8401 ? INSN_DSP64 : 0)
ef2e4d86 8402 | (mips_opts.ase_mt ? INSN_MT : 0)
e16bfa71
TS
8403 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)
8404 | (mips_opts.ase_smartmips ? INSN_SMARTMIPS : 0)),
fef14a42 8405 mips_opts.arch))
b34976b6 8406 ok = TRUE;
bdaaa2e1 8407 else
b34976b6 8408 ok = FALSE;
bdaaa2e1 8409
252b5132
RH
8410 if (insn->pinfo != INSN_MACRO)
8411 {
fef14a42 8412 if (mips_opts.arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
b34976b6 8413 ok = FALSE;
252b5132
RH
8414 }
8415
8416 if (! ok)
8417 {
8418 if (insn + 1 < &mips_opcodes[NUMOPCODES]
8419 && strcmp (insn->name, insn[1].name) == 0)
8420 {
8421 ++insn;
8422 continue;
8423 }
252b5132 8424 else
beae10d5 8425 {
268f6bed
L
8426 if (!insn_error)
8427 {
8428 static char buf[100];
fef14a42
TS
8429 sprintf (buf,
8430 _("opcode not supported on this processor: %s (%s)"),
8431 mips_cpu_info_from_arch (mips_opts.arch)->name,
8432 mips_cpu_info_from_isa (mips_opts.isa)->name);
268f6bed
L
8433 insn_error = buf;
8434 }
8435 if (save_c)
8436 *(--s) = save_c;
2bd7f1f3 8437 return;
252b5132 8438 }
252b5132
RH
8439 }
8440
1e915849 8441 create_insn (ip, insn);
268f6bed 8442 insn_error = NULL;
707bfff6 8443 argnum = 1;
252b5132
RH
8444 for (args = insn->args;; ++args)
8445 {
deec1734
CD
8446 int is_mdmx;
8447
ad8d3bb3 8448 s += strspn (s, " \t");
deec1734 8449 is_mdmx = 0;
252b5132
RH
8450 switch (*args)
8451 {
8452 case '\0': /* end of args */
8453 if (*s == '\0')
8454 return;
8455 break;
8456
74cd071d
CF
8457 case '3': /* dsp 3-bit unsigned immediate in bit 21 */
8458 my_getExpression (&imm_expr, s);
8459 check_absolute_expr (ip, &imm_expr);
8460 if (imm_expr.X_add_number & ~OP_MASK_SA3)
8461 {
a9e24354
TS
8462 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8463 OP_MASK_SA3, (unsigned long) imm_expr.X_add_number);
74cd071d 8464 }
a9e24354 8465 INSERT_OPERAND (SA3, *ip, imm_expr.X_add_number);
74cd071d
CF
8466 imm_expr.X_op = O_absent;
8467 s = expr_end;
8468 continue;
8469
8470 case '4': /* dsp 4-bit unsigned immediate in bit 21 */
8471 my_getExpression (&imm_expr, s);
8472 check_absolute_expr (ip, &imm_expr);
8473 if (imm_expr.X_add_number & ~OP_MASK_SA4)
8474 {
a9e24354
TS
8475 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8476 OP_MASK_SA4, (unsigned long) imm_expr.X_add_number);
74cd071d 8477 }
a9e24354 8478 INSERT_OPERAND (SA4, *ip, imm_expr.X_add_number);
74cd071d
CF
8479 imm_expr.X_op = O_absent;
8480 s = expr_end;
8481 continue;
8482
8483 case '5': /* dsp 8-bit unsigned immediate in bit 16 */
8484 my_getExpression (&imm_expr, s);
8485 check_absolute_expr (ip, &imm_expr);
8486 if (imm_expr.X_add_number & ~OP_MASK_IMM8)
8487 {
a9e24354
TS
8488 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8489 OP_MASK_IMM8, (unsigned long) imm_expr.X_add_number);
74cd071d 8490 }
a9e24354 8491 INSERT_OPERAND (IMM8, *ip, imm_expr.X_add_number);
74cd071d
CF
8492 imm_expr.X_op = O_absent;
8493 s = expr_end;
8494 continue;
8495
8496 case '6': /* dsp 5-bit unsigned immediate in bit 21 */
8497 my_getExpression (&imm_expr, s);
8498 check_absolute_expr (ip, &imm_expr);
8499 if (imm_expr.X_add_number & ~OP_MASK_RS)
8500 {
a9e24354
TS
8501 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8502 OP_MASK_RS, (unsigned long) imm_expr.X_add_number);
74cd071d 8503 }
a9e24354 8504 INSERT_OPERAND (RS, *ip, imm_expr.X_add_number);
74cd071d
CF
8505 imm_expr.X_op = O_absent;
8506 s = expr_end;
8507 continue;
8508
8509 case '7': /* four dsp accumulators in bits 11,12 */
8510 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8511 s[3] >= '0' && s[3] <= '3')
8512 {
8513 regno = s[3] - '0';
8514 s += 4;
a9e24354 8515 INSERT_OPERAND (DSPACC, *ip, regno);
74cd071d
CF
8516 continue;
8517 }
8518 else
8519 as_bad (_("Invalid dsp acc register"));
8520 break;
8521
8522 case '8': /* dsp 6-bit unsigned immediate in bit 11 */
8523 my_getExpression (&imm_expr, s);
8524 check_absolute_expr (ip, &imm_expr);
8525 if (imm_expr.X_add_number & ~OP_MASK_WRDSP)
8526 {
a9e24354
TS
8527 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8528 OP_MASK_WRDSP,
8529 (unsigned long) imm_expr.X_add_number);
74cd071d 8530 }
a9e24354 8531 INSERT_OPERAND (WRDSP, *ip, imm_expr.X_add_number);
74cd071d
CF
8532 imm_expr.X_op = O_absent;
8533 s = expr_end;
8534 continue;
8535
8536 case '9': /* four dsp accumulators in bits 21,22 */
8537 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8538 s[3] >= '0' && s[3] <= '3')
8539 {
8540 regno = s[3] - '0';
8541 s += 4;
a9e24354 8542 INSERT_OPERAND (DSPACC_S, *ip, regno);
74cd071d
CF
8543 continue;
8544 }
8545 else
8546 as_bad (_("Invalid dsp acc register"));
8547 break;
8548
8549 case '0': /* dsp 6-bit signed immediate in bit 20 */
8550 my_getExpression (&imm_expr, s);
8551 check_absolute_expr (ip, &imm_expr);
8552 min_range = -((OP_MASK_DSPSFT + 1) >> 1);
8553 max_range = ((OP_MASK_DSPSFT + 1) >> 1) - 1;
8554 if (imm_expr.X_add_number < min_range ||
8555 imm_expr.X_add_number > max_range)
8556 {
a9e24354
TS
8557 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
8558 (long) min_range, (long) max_range,
8559 (long) imm_expr.X_add_number);
74cd071d 8560 }
a9e24354 8561 INSERT_OPERAND (DSPSFT, *ip, imm_expr.X_add_number);
74cd071d
CF
8562 imm_expr.X_op = O_absent;
8563 s = expr_end;
8564 continue;
8565
8566 case '\'': /* dsp 6-bit unsigned immediate in bit 16 */
8567 my_getExpression (&imm_expr, s);
8568 check_absolute_expr (ip, &imm_expr);
8569 if (imm_expr.X_add_number & ~OP_MASK_RDDSP)
8570 {
a9e24354
TS
8571 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8572 OP_MASK_RDDSP,
8573 (unsigned long) imm_expr.X_add_number);
74cd071d 8574 }
a9e24354 8575 INSERT_OPERAND (RDDSP, *ip, imm_expr.X_add_number);
74cd071d
CF
8576 imm_expr.X_op = O_absent;
8577 s = expr_end;
8578 continue;
8579
8580 case ':': /* dsp 7-bit signed immediate in bit 19 */
8581 my_getExpression (&imm_expr, s);
8582 check_absolute_expr (ip, &imm_expr);
8583 min_range = -((OP_MASK_DSPSFT_7 + 1) >> 1);
8584 max_range = ((OP_MASK_DSPSFT_7 + 1) >> 1) - 1;
8585 if (imm_expr.X_add_number < min_range ||
8586 imm_expr.X_add_number > max_range)
8587 {
a9e24354
TS
8588 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
8589 (long) min_range, (long) max_range,
8590 (long) imm_expr.X_add_number);
74cd071d 8591 }
a9e24354 8592 INSERT_OPERAND (DSPSFT_7, *ip, imm_expr.X_add_number);
74cd071d
CF
8593 imm_expr.X_op = O_absent;
8594 s = expr_end;
8595 continue;
8596
8597 case '@': /* dsp 10-bit signed immediate in bit 16 */
8598 my_getExpression (&imm_expr, s);
8599 check_absolute_expr (ip, &imm_expr);
8600 min_range = -((OP_MASK_IMM10 + 1) >> 1);
8601 max_range = ((OP_MASK_IMM10 + 1) >> 1) - 1;
8602 if (imm_expr.X_add_number < min_range ||
8603 imm_expr.X_add_number > max_range)
8604 {
a9e24354
TS
8605 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
8606 (long) min_range, (long) max_range,
8607 (long) imm_expr.X_add_number);
74cd071d 8608 }
a9e24354 8609 INSERT_OPERAND (IMM10, *ip, imm_expr.X_add_number);
74cd071d
CF
8610 imm_expr.X_op = O_absent;
8611 s = expr_end;
8612 continue;
8613
a9e24354 8614 case '!': /* MT usermode flag bit. */
ef2e4d86
CF
8615 my_getExpression (&imm_expr, s);
8616 check_absolute_expr (ip, &imm_expr);
8617 if (imm_expr.X_add_number & ~OP_MASK_MT_U)
a9e24354
TS
8618 as_bad (_("MT usermode bit not 0 or 1 (%lu)"),
8619 (unsigned long) imm_expr.X_add_number);
8620 INSERT_OPERAND (MT_U, *ip, imm_expr.X_add_number);
ef2e4d86
CF
8621 imm_expr.X_op = O_absent;
8622 s = expr_end;
8623 continue;
8624
a9e24354 8625 case '$': /* MT load high flag bit. */
ef2e4d86
CF
8626 my_getExpression (&imm_expr, s);
8627 check_absolute_expr (ip, &imm_expr);
8628 if (imm_expr.X_add_number & ~OP_MASK_MT_H)
a9e24354
TS
8629 as_bad (_("MT load high bit not 0 or 1 (%lu)"),
8630 (unsigned long) imm_expr.X_add_number);
8631 INSERT_OPERAND (MT_H, *ip, imm_expr.X_add_number);
ef2e4d86
CF
8632 imm_expr.X_op = O_absent;
8633 s = expr_end;
8634 continue;
8635
8636 case '*': /* four dsp accumulators in bits 18,19 */
8637 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8638 s[3] >= '0' && s[3] <= '3')
8639 {
8640 regno = s[3] - '0';
8641 s += 4;
a9e24354 8642 INSERT_OPERAND (MTACC_T, *ip, regno);
ef2e4d86
CF
8643 continue;
8644 }
8645 else
8646 as_bad (_("Invalid dsp/smartmips acc register"));
8647 break;
8648
8649 case '&': /* four dsp accumulators in bits 13,14 */
8650 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8651 s[3] >= '0' && s[3] <= '3')
8652 {
8653 regno = s[3] - '0';
8654 s += 4;
a9e24354 8655 INSERT_OPERAND (MTACC_D, *ip, regno);
ef2e4d86
CF
8656 continue;
8657 }
8658 else
8659 as_bad (_("Invalid dsp/smartmips acc register"));
8660 break;
8661
252b5132 8662 case ',':
a339155f 8663 ++argnum;
252b5132
RH
8664 if (*s++ == *args)
8665 continue;
8666 s--;
8667 switch (*++args)
8668 {
8669 case 'r':
8670 case 'v':
bf12938e 8671 INSERT_OPERAND (RS, *ip, lastregno);
252b5132
RH
8672 continue;
8673
8674 case 'w':
bf12938e 8675 INSERT_OPERAND (RT, *ip, lastregno);
38487616
TS
8676 continue;
8677
252b5132 8678 case 'W':
bf12938e 8679 INSERT_OPERAND (FT, *ip, lastregno);
252b5132
RH
8680 continue;
8681
8682 case 'V':
bf12938e 8683 INSERT_OPERAND (FS, *ip, lastregno);
252b5132
RH
8684 continue;
8685 }
8686 break;
8687
8688 case '(':
8689 /* Handle optional base register.
8690 Either the base register is omitted or
bdaaa2e1 8691 we must have a left paren. */
252b5132
RH
8692 /* This is dependent on the next operand specifier
8693 is a base register specification. */
8694 assert (args[1] == 'b' || args[1] == '5'
8695 || args[1] == '-' || args[1] == '4');
8696 if (*s == '\0')
8697 return;
8698
8699 case ')': /* these must match exactly */
60b63b72
RS
8700 case '[':
8701 case ']':
252b5132
RH
8702 if (*s++ == *args)
8703 continue;
8704 break;
8705
af7ee8bf
CD
8706 case '+': /* Opcode extension character. */
8707 switch (*++args)
8708 {
9bcd4f99
TS
8709 case '1': /* UDI immediates. */
8710 case '2':
8711 case '3':
8712 case '4':
8713 {
8714 const struct mips_immed *imm = mips_immed;
8715
8716 while (imm->type && imm->type != *args)
8717 ++imm;
8718 if (! imm->type)
8719 internalError ();
8720 my_getExpression (&imm_expr, s);
8721 check_absolute_expr (ip, &imm_expr);
8722 if ((unsigned long) imm_expr.X_add_number & ~imm->mask)
8723 {
8724 as_warn (_("Illegal %s number (%lu, 0x%lx)"),
8725 imm->desc ? imm->desc : ip->insn_mo->name,
8726 (unsigned long) imm_expr.X_add_number,
8727 (unsigned long) imm_expr.X_add_number);
8728 imm_expr.X_add_number &= imm->mask;
8729 }
8730 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
8731 << imm->shift);
8732 imm_expr.X_op = O_absent;
8733 s = expr_end;
8734 }
8735 continue;
8736
071742cf
CD
8737 case 'A': /* ins/ext position, becomes LSB. */
8738 limlo = 0;
8739 limhi = 31;
5f74bc13
CD
8740 goto do_lsb;
8741 case 'E':
8742 limlo = 32;
8743 limhi = 63;
8744 goto do_lsb;
8745do_lsb:
071742cf
CD
8746 my_getExpression (&imm_expr, s);
8747 check_absolute_expr (ip, &imm_expr);
8748 if ((unsigned long) imm_expr.X_add_number < limlo
8749 || (unsigned long) imm_expr.X_add_number > limhi)
8750 {
8751 as_bad (_("Improper position (%lu)"),
8752 (unsigned long) imm_expr.X_add_number);
8753 imm_expr.X_add_number = limlo;
8754 }
8755 lastpos = imm_expr.X_add_number;
bf12938e 8756 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number);
071742cf
CD
8757 imm_expr.X_op = O_absent;
8758 s = expr_end;
8759 continue;
8760
8761 case 'B': /* ins size, becomes MSB. */
8762 limlo = 1;
8763 limhi = 32;
5f74bc13
CD
8764 goto do_msb;
8765 case 'F':
8766 limlo = 33;
8767 limhi = 64;
8768 goto do_msb;
8769do_msb:
071742cf
CD
8770 my_getExpression (&imm_expr, s);
8771 check_absolute_expr (ip, &imm_expr);
8772 /* Check for negative input so that small negative numbers
8773 will not succeed incorrectly. The checks against
8774 (pos+size) transitively check "size" itself,
8775 assuming that "pos" is reasonable. */
8776 if ((long) imm_expr.X_add_number < 0
8777 || ((unsigned long) imm_expr.X_add_number
8778 + lastpos) < limlo
8779 || ((unsigned long) imm_expr.X_add_number
8780 + lastpos) > limhi)
8781 {
8782 as_bad (_("Improper insert size (%lu, position %lu)"),
8783 (unsigned long) imm_expr.X_add_number,
8784 (unsigned long) lastpos);
8785 imm_expr.X_add_number = limlo - lastpos;
8786 }
bf12938e
RS
8787 INSERT_OPERAND (INSMSB, *ip,
8788 lastpos + imm_expr.X_add_number - 1);
071742cf
CD
8789 imm_expr.X_op = O_absent;
8790 s = expr_end;
8791 continue;
8792
8793 case 'C': /* ext size, becomes MSBD. */
8794 limlo = 1;
8795 limhi = 32;
5f74bc13
CD
8796 goto do_msbd;
8797 case 'G':
8798 limlo = 33;
8799 limhi = 64;
8800 goto do_msbd;
8801 case 'H':
8802 limlo = 33;
8803 limhi = 64;
8804 goto do_msbd;
8805do_msbd:
071742cf
CD
8806 my_getExpression (&imm_expr, s);
8807 check_absolute_expr (ip, &imm_expr);
8808 /* Check for negative input so that small negative numbers
8809 will not succeed incorrectly. The checks against
8810 (pos+size) transitively check "size" itself,
8811 assuming that "pos" is reasonable. */
8812 if ((long) imm_expr.X_add_number < 0
8813 || ((unsigned long) imm_expr.X_add_number
8814 + lastpos) < limlo
8815 || ((unsigned long) imm_expr.X_add_number
8816 + lastpos) > limhi)
8817 {
8818 as_bad (_("Improper extract size (%lu, position %lu)"),
8819 (unsigned long) imm_expr.X_add_number,
8820 (unsigned long) lastpos);
8821 imm_expr.X_add_number = limlo - lastpos;
8822 }
bf12938e 8823 INSERT_OPERAND (EXTMSBD, *ip, imm_expr.X_add_number - 1);
071742cf
CD
8824 imm_expr.X_op = O_absent;
8825 s = expr_end;
8826 continue;
af7ee8bf 8827
bbcc0807
CD
8828 case 'D':
8829 /* +D is for disassembly only; never match. */
8830 break;
8831
5f74bc13
CD
8832 case 'I':
8833 /* "+I" is like "I", except that imm2_expr is used. */
8834 my_getExpression (&imm2_expr, s);
8835 if (imm2_expr.X_op != O_big
8836 && imm2_expr.X_op != O_constant)
8837 insn_error = _("absolute expression required");
9ee2a2d4
MR
8838 if (HAVE_32BIT_GPRS)
8839 normalize_constant_expr (&imm2_expr);
5f74bc13
CD
8840 s = expr_end;
8841 continue;
8842
707bfff6 8843 case 'T': /* Coprocessor register. */
ef2e4d86
CF
8844 /* +T is for disassembly only; never match. */
8845 break;
8846
707bfff6 8847 case 't': /* Coprocessor register number. */
ef2e4d86
CF
8848 if (s[0] == '$' && ISDIGIT (s[1]))
8849 {
8850 ++s;
8851 regno = 0;
8852 do
8853 {
8854 regno *= 10;
8855 regno += *s - '0';
8856 ++s;
8857 }
8858 while (ISDIGIT (*s));
8859 if (regno > 31)
8860 as_bad (_("Invalid register number (%d)"), regno);
8861 else
8862 {
a9e24354 8863 INSERT_OPERAND (RT, *ip, regno);
ef2e4d86
CF
8864 continue;
8865 }
8866 }
8867 else
8868 as_bad (_("Invalid coprocessor 0 register number"));
8869 break;
8870
af7ee8bf
CD
8871 default:
8872 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8873 *args, insn->name, insn->args);
8874 /* Further processing is fruitless. */
8875 return;
8876 }
8877 break;
8878
252b5132
RH
8879 case '<': /* must be at least one digit */
8880 /*
8881 * According to the manual, if the shift amount is greater
b6ff326e
KH
8882 * than 31 or less than 0, then the shift amount should be
8883 * mod 32. In reality the mips assembler issues an error.
252b5132
RH
8884 * We issue a warning and mask out all but the low 5 bits.
8885 */
8886 my_getExpression (&imm_expr, s);
8887 check_absolute_expr (ip, &imm_expr);
8888 if ((unsigned long) imm_expr.X_add_number > 31)
bf12938e
RS
8889 as_warn (_("Improper shift amount (%lu)"),
8890 (unsigned long) imm_expr.X_add_number);
8891 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number);
252b5132
RH
8892 imm_expr.X_op = O_absent;
8893 s = expr_end;
8894 continue;
8895
8896 case '>': /* shift amount minus 32 */
8897 my_getExpression (&imm_expr, s);
8898 check_absolute_expr (ip, &imm_expr);
8899 if ((unsigned long) imm_expr.X_add_number < 32
8900 || (unsigned long) imm_expr.X_add_number > 63)
8901 break;
bf12938e 8902 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number - 32);
252b5132
RH
8903 imm_expr.X_op = O_absent;
8904 s = expr_end;
8905 continue;
8906
252b5132
RH
8907 case 'k': /* cache code */
8908 case 'h': /* prefx code */
8909 my_getExpression (&imm_expr, s);
8910 check_absolute_expr (ip, &imm_expr);
8911 if ((unsigned long) imm_expr.X_add_number > 31)
bf12938e
RS
8912 as_warn (_("Invalid value for `%s' (%lu)"),
8913 ip->insn_mo->name,
8914 (unsigned long) imm_expr.X_add_number);
252b5132 8915 if (*args == 'k')
bf12938e 8916 INSERT_OPERAND (CACHE, *ip, imm_expr.X_add_number);
252b5132 8917 else
bf12938e 8918 INSERT_OPERAND (PREFX, *ip, imm_expr.X_add_number);
252b5132
RH
8919 imm_expr.X_op = O_absent;
8920 s = expr_end;
8921 continue;
8922
8923 case 'c': /* break code */
8924 my_getExpression (&imm_expr, s);
8925 check_absolute_expr (ip, &imm_expr);
a9e24354
TS
8926 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE)
8927 as_warn (_("Code for %s not in range 0..1023 (%lu)"),
8928 ip->insn_mo->name,
bf12938e
RS
8929 (unsigned long) imm_expr.X_add_number);
8930 INSERT_OPERAND (CODE, *ip, imm_expr.X_add_number);
252b5132
RH
8931 imm_expr.X_op = O_absent;
8932 s = expr_end;
8933 continue;
8934
8935 case 'q': /* lower break code */
8936 my_getExpression (&imm_expr, s);
8937 check_absolute_expr (ip, &imm_expr);
a9e24354
TS
8938 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE2)
8939 as_warn (_("Lower code for %s not in range 0..1023 (%lu)"),
8940 ip->insn_mo->name,
bf12938e
RS
8941 (unsigned long) imm_expr.X_add_number);
8942 INSERT_OPERAND (CODE2, *ip, imm_expr.X_add_number);
252b5132
RH
8943 imm_expr.X_op = O_absent;
8944 s = expr_end;
8945 continue;
8946
4372b673 8947 case 'B': /* 20-bit syscall/break code. */
156c2f8b 8948 my_getExpression (&imm_expr, s);
156c2f8b 8949 check_absolute_expr (ip, &imm_expr);
793b27f4 8950 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
a9e24354
TS
8951 as_warn (_("Code for %s not in range 0..1048575 (%lu)"),
8952 ip->insn_mo->name,
793b27f4 8953 (unsigned long) imm_expr.X_add_number);
bf12938e 8954 INSERT_OPERAND (CODE20, *ip, imm_expr.X_add_number);
252b5132
RH
8955 imm_expr.X_op = O_absent;
8956 s = expr_end;
8957 continue;
8958
98d3f06f 8959 case 'C': /* Coprocessor code */
beae10d5 8960 my_getExpression (&imm_expr, s);
252b5132 8961 check_absolute_expr (ip, &imm_expr);
a9e24354 8962 if ((unsigned long) imm_expr.X_add_number > OP_MASK_COPZ)
252b5132 8963 {
793b27f4
TS
8964 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8965 (unsigned long) imm_expr.X_add_number);
a9e24354 8966 imm_expr.X_add_number &= OP_MASK_COPZ;
252b5132 8967 }
a9e24354 8968 INSERT_OPERAND (COPZ, *ip, imm_expr.X_add_number);
beae10d5
KH
8969 imm_expr.X_op = O_absent;
8970 s = expr_end;
8971 continue;
252b5132 8972
4372b673
NC
8973 case 'J': /* 19-bit wait code. */
8974 my_getExpression (&imm_expr, s);
8975 check_absolute_expr (ip, &imm_expr);
793b27f4 8976 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
a9e24354
TS
8977 {
8978 as_warn (_("Illegal 19-bit code (%lu)"),
8979 (unsigned long) imm_expr.X_add_number);
8980 imm_expr.X_add_number &= OP_MASK_CODE19;
8981 }
bf12938e 8982 INSERT_OPERAND (CODE19, *ip, imm_expr.X_add_number);
4372b673
NC
8983 imm_expr.X_op = O_absent;
8984 s = expr_end;
8985 continue;
8986
707bfff6 8987 case 'P': /* Performance register. */
beae10d5 8988 my_getExpression (&imm_expr, s);
252b5132 8989 check_absolute_expr (ip, &imm_expr);
beae10d5 8990 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
bf12938e
RS
8991 as_warn (_("Invalid performance register (%lu)"),
8992 (unsigned long) imm_expr.X_add_number);
8993 INSERT_OPERAND (PERFREG, *ip, imm_expr.X_add_number);
beae10d5
KH
8994 imm_expr.X_op = O_absent;
8995 s = expr_end;
8996 continue;
252b5132 8997
707bfff6
TS
8998 case 'G': /* Coprocessor destination register. */
8999 if (((ip->insn_opcode >> OP_SH_OP) & OP_MASK_OP) == OP_OP_COP0)
9000 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_CP0, &regno);
9001 else
9002 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
a9e24354 9003 INSERT_OPERAND (RD, *ip, regno);
707bfff6
TS
9004 if (ok)
9005 {
9006 lastregno = regno;
9007 continue;
9008 }
9009 else
9010 break;
9011
252b5132
RH
9012 case 'b': /* base register */
9013 case 'd': /* destination register */
9014 case 's': /* source register */
9015 case 't': /* target register */
9016 case 'r': /* both target and source */
9017 case 'v': /* both dest and source */
9018 case 'w': /* both dest and target */
9019 case 'E': /* coprocessor target register */
af7ee8bf 9020 case 'K': /* 'rdhwr' destination register */
252b5132
RH
9021 case 'x': /* ignore register name */
9022 case 'z': /* must be zero register */
4372b673 9023 case 'U': /* destination register (clo/clz). */
ef2e4d86 9024 case 'g': /* coprocessor destination register */
707bfff6
TS
9025 s_reset = s;
9026 if (*args == 'E' || *args == 'K')
9027 ok = reg_lookup (&s, RTYPE_NUM, &regno);
9028 else
9029 {
9030 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
9031 if (regno == AT && ! mips_opts.noat)
9032 as_warn ("Used $at without \".set noat\"");
9033 }
9034 if (ok)
252b5132 9035 {
252b5132
RH
9036 c = *args;
9037 if (*s == ' ')
f9419b05 9038 ++s;
252b5132
RH
9039 if (args[1] != *s)
9040 {
9041 if (c == 'r' || c == 'v' || c == 'w')
9042 {
9043 regno = lastregno;
9044 s = s_reset;
f9419b05 9045 ++args;
252b5132
RH
9046 }
9047 }
9048 /* 'z' only matches $0. */
9049 if (c == 'z' && regno != 0)
9050 break;
9051
bdaaa2e1
KH
9052 /* Now that we have assembled one operand, we use the args string
9053 * to figure out where it goes in the instruction. */
252b5132
RH
9054 switch (c)
9055 {
9056 case 'r':
9057 case 's':
9058 case 'v':
9059 case 'b':
bf12938e 9060 INSERT_OPERAND (RS, *ip, regno);
252b5132
RH
9061 break;
9062 case 'd':
9063 case 'G':
af7ee8bf 9064 case 'K':
ef2e4d86 9065 case 'g':
bf12938e 9066 INSERT_OPERAND (RD, *ip, regno);
252b5132 9067 break;
4372b673 9068 case 'U':
bf12938e
RS
9069 INSERT_OPERAND (RD, *ip, regno);
9070 INSERT_OPERAND (RT, *ip, regno);
4372b673 9071 break;
252b5132
RH
9072 case 'w':
9073 case 't':
9074 case 'E':
bf12938e 9075 INSERT_OPERAND (RT, *ip, regno);
252b5132
RH
9076 break;
9077 case 'x':
9078 /* This case exists because on the r3000 trunc
9079 expands into a macro which requires a gp
9080 register. On the r6000 or r4000 it is
9081 assembled into a single instruction which
9082 ignores the register. Thus the insn version
9083 is MIPS_ISA2 and uses 'x', and the macro
9084 version is MIPS_ISA1 and uses 't'. */
9085 break;
9086 case 'z':
9087 /* This case is for the div instruction, which
9088 acts differently if the destination argument
9089 is $0. This only matches $0, and is checked
9090 outside the switch. */
9091 break;
9092 case 'D':
9093 /* Itbl operand; not yet implemented. FIXME ?? */
9094 break;
9095 /* What about all other operands like 'i', which
9096 can be specified in the opcode table? */
9097 }
9098 lastregno = regno;
9099 continue;
9100 }
252b5132
RH
9101 switch (*args++)
9102 {
9103 case 'r':
9104 case 'v':
bf12938e 9105 INSERT_OPERAND (RS, *ip, lastregno);
252b5132
RH
9106 continue;
9107 case 'w':
bf12938e 9108 INSERT_OPERAND (RT, *ip, lastregno);
252b5132
RH
9109 continue;
9110 }
9111 break;
9112
deec1734
CD
9113 case 'O': /* MDMX alignment immediate constant. */
9114 my_getExpression (&imm_expr, s);
9115 check_absolute_expr (ip, &imm_expr);
9116 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
bf12938e
RS
9117 as_warn ("Improper align amount (%ld), using low bits",
9118 (long) imm_expr.X_add_number);
9119 INSERT_OPERAND (ALN, *ip, imm_expr.X_add_number);
deec1734
CD
9120 imm_expr.X_op = O_absent;
9121 s = expr_end;
9122 continue;
9123
9124 case 'Q': /* MDMX vector, element sel, or const. */
9125 if (s[0] != '$')
9126 {
9127 /* MDMX Immediate. */
9128 my_getExpression (&imm_expr, s);
9129 check_absolute_expr (ip, &imm_expr);
9130 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
bf12938e
RS
9131 as_warn (_("Invalid MDMX Immediate (%ld)"),
9132 (long) imm_expr.X_add_number);
9133 INSERT_OPERAND (FT, *ip, imm_expr.X_add_number);
deec1734
CD
9134 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
9135 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
9136 else
9137 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
deec1734
CD
9138 imm_expr.X_op = O_absent;
9139 s = expr_end;
9140 continue;
9141 }
9142 /* Not MDMX Immediate. Fall through. */
9143 case 'X': /* MDMX destination register. */
9144 case 'Y': /* MDMX source register. */
9145 case 'Z': /* MDMX target register. */
9146 is_mdmx = 1;
252b5132
RH
9147 case 'D': /* floating point destination register */
9148 case 'S': /* floating point source register */
9149 case 'T': /* floating point target register */
9150 case 'R': /* floating point source register */
9151 case 'V':
9152 case 'W':
707bfff6
TS
9153 rtype = RTYPE_FPU;
9154 if (is_mdmx
9155 || (mips_opts.ase_mdmx
9156 && (ip->insn_mo->pinfo & FP_D)
9157 && (ip->insn_mo->pinfo & (INSN_COPROC_MOVE_DELAY
9158 | INSN_COPROC_MEMORY_DELAY
9159 | INSN_LOAD_COPROC_DELAY
9160 | INSN_LOAD_MEMORY_DELAY
9161 | INSN_STORE_MEMORY))))
9162 rtype |= RTYPE_VEC;
252b5132 9163 s_reset = s;
707bfff6 9164 if (reg_lookup (&s, rtype, &regno))
252b5132 9165 {
252b5132 9166 if ((regno & 1) != 0
ca4e0257 9167 && HAVE_32BIT_FPRS
7455baf8 9168 && ! mips_oddfpreg_ok (ip->insn_mo, argnum))
252b5132
RH
9169 as_warn (_("Float register should be even, was %d"),
9170 regno);
9171
9172 c = *args;
9173 if (*s == ' ')
f9419b05 9174 ++s;
252b5132
RH
9175 if (args[1] != *s)
9176 {
9177 if (c == 'V' || c == 'W')
9178 {
9179 regno = lastregno;
9180 s = s_reset;
f9419b05 9181 ++args;
252b5132
RH
9182 }
9183 }
9184 switch (c)
9185 {
9186 case 'D':
deec1734 9187 case 'X':
bf12938e 9188 INSERT_OPERAND (FD, *ip, regno);
252b5132
RH
9189 break;
9190 case 'V':
9191 case 'S':
deec1734 9192 case 'Y':
bf12938e 9193 INSERT_OPERAND (FS, *ip, regno);
252b5132 9194 break;
deec1734
CD
9195 case 'Q':
9196 /* This is like 'Z', but also needs to fix the MDMX
9197 vector/scalar select bits. Note that the
9198 scalar immediate case is handled above. */
9199 if (*s == '[')
9200 {
9201 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
9202 int max_el = (is_qh ? 3 : 7);
9203 s++;
9204 my_getExpression(&imm_expr, s);
9205 check_absolute_expr (ip, &imm_expr);
9206 s = expr_end;
9207 if (imm_expr.X_add_number > max_el)
9208 as_bad(_("Bad element selector %ld"),
9209 (long) imm_expr.X_add_number);
9210 imm_expr.X_add_number &= max_el;
9211 ip->insn_opcode |= (imm_expr.X_add_number
9212 << (OP_SH_VSEL +
9213 (is_qh ? 2 : 1)));
01a3f561 9214 imm_expr.X_op = O_absent;
deec1734
CD
9215 if (*s != ']')
9216 as_warn(_("Expecting ']' found '%s'"), s);
9217 else
9218 s++;
9219 }
9220 else
9221 {
9222 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
9223 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
9224 << OP_SH_VSEL);
9225 else
9226 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
9227 OP_SH_VSEL);
9228 }
9229 /* Fall through */
252b5132
RH
9230 case 'W':
9231 case 'T':
deec1734 9232 case 'Z':
bf12938e 9233 INSERT_OPERAND (FT, *ip, regno);
252b5132
RH
9234 break;
9235 case 'R':
bf12938e 9236 INSERT_OPERAND (FR, *ip, regno);
252b5132
RH
9237 break;
9238 }
9239 lastregno = regno;
9240 continue;
9241 }
9242
252b5132
RH
9243 switch (*args++)
9244 {
9245 case 'V':
bf12938e 9246 INSERT_OPERAND (FS, *ip, lastregno);
252b5132
RH
9247 continue;
9248 case 'W':
bf12938e 9249 INSERT_OPERAND (FT, *ip, lastregno);
252b5132
RH
9250 continue;
9251 }
9252 break;
9253
9254 case 'I':
9255 my_getExpression (&imm_expr, s);
9256 if (imm_expr.X_op != O_big
9257 && imm_expr.X_op != O_constant)
9258 insn_error = _("absolute expression required");
9ee2a2d4
MR
9259 if (HAVE_32BIT_GPRS)
9260 normalize_constant_expr (&imm_expr);
252b5132
RH
9261 s = expr_end;
9262 continue;
9263
9264 case 'A':
9265 my_getExpression (&offset_expr, s);
2051e8c4 9266 normalize_address_expr (&offset_expr);
f6688943 9267 *imm_reloc = BFD_RELOC_32;
252b5132
RH
9268 s = expr_end;
9269 continue;
9270
9271 case 'F':
9272 case 'L':
9273 case 'f':
9274 case 'l':
9275 {
9276 int f64;
ca4e0257 9277 int using_gprs;
252b5132
RH
9278 char *save_in;
9279 char *err;
9280 unsigned char temp[8];
9281 int len;
9282 unsigned int length;
9283 segT seg;
9284 subsegT subseg;
9285 char *p;
9286
9287 /* These only appear as the last operand in an
9288 instruction, and every instruction that accepts
9289 them in any variant accepts them in all variants.
9290 This means we don't have to worry about backing out
9291 any changes if the instruction does not match.
9292
9293 The difference between them is the size of the
9294 floating point constant and where it goes. For 'F'
9295 and 'L' the constant is 64 bits; for 'f' and 'l' it
9296 is 32 bits. Where the constant is placed is based
9297 on how the MIPS assembler does things:
9298 F -- .rdata
9299 L -- .lit8
9300 f -- immediate value
9301 l -- .lit4
9302
9303 The .lit4 and .lit8 sections are only used if
9304 permitted by the -G argument.
9305
ca4e0257
RS
9306 The code below needs to know whether the target register
9307 is 32 or 64 bits wide. It relies on the fact 'f' and
9308 'F' are used with GPR-based instructions and 'l' and
9309 'L' are used with FPR-based instructions. */
252b5132
RH
9310
9311 f64 = *args == 'F' || *args == 'L';
ca4e0257 9312 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
9313
9314 save_in = input_line_pointer;
9315 input_line_pointer = s;
9316 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
9317 length = len;
9318 s = input_line_pointer;
9319 input_line_pointer = save_in;
9320 if (err != NULL && *err != '\0')
9321 {
9322 as_bad (_("Bad floating point constant: %s"), err);
9323 memset (temp, '\0', sizeof temp);
9324 length = f64 ? 8 : 4;
9325 }
9326
156c2f8b 9327 assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
9328
9329 if (*args == 'f'
9330 || (*args == 'l'
3e722fb5 9331 && (g_switch_value < 4
252b5132
RH
9332 || (temp[0] == 0 && temp[1] == 0)
9333 || (temp[2] == 0 && temp[3] == 0))))
9334 {
9335 imm_expr.X_op = O_constant;
9336 if (! target_big_endian)
9337 imm_expr.X_add_number = bfd_getl32 (temp);
9338 else
9339 imm_expr.X_add_number = bfd_getb32 (temp);
9340 }
9341 else if (length > 4
119d663a 9342 && ! mips_disable_float_construction
ca4e0257
RS
9343 /* Constants can only be constructed in GPRs and
9344 copied to FPRs if the GPRs are at least as wide
9345 as the FPRs. Force the constant into memory if
9346 we are using 64-bit FPRs but the GPRs are only
9347 32 bits wide. */
9348 && (using_gprs
9349 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
9350 && ((temp[0] == 0 && temp[1] == 0)
9351 || (temp[2] == 0 && temp[3] == 0))
9352 && ((temp[4] == 0 && temp[5] == 0)
9353 || (temp[6] == 0 && temp[7] == 0)))
9354 {
ca4e0257
RS
9355 /* The value is simple enough to load with a couple of
9356 instructions. If using 32-bit registers, set
9357 imm_expr to the high order 32 bits and offset_expr to
9358 the low order 32 bits. Otherwise, set imm_expr to
9359 the entire 64 bit constant. */
9360 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
9361 {
9362 imm_expr.X_op = O_constant;
9363 offset_expr.X_op = O_constant;
9364 if (! target_big_endian)
9365 {
9366 imm_expr.X_add_number = bfd_getl32 (temp + 4);
9367 offset_expr.X_add_number = bfd_getl32 (temp);
9368 }
9369 else
9370 {
9371 imm_expr.X_add_number = bfd_getb32 (temp);
9372 offset_expr.X_add_number = bfd_getb32 (temp + 4);
9373 }
9374 if (offset_expr.X_add_number == 0)
9375 offset_expr.X_op = O_absent;
9376 }
9377 else if (sizeof (imm_expr.X_add_number) > 4)
9378 {
9379 imm_expr.X_op = O_constant;
9380 if (! target_big_endian)
9381 imm_expr.X_add_number = bfd_getl64 (temp);
9382 else
9383 imm_expr.X_add_number = bfd_getb64 (temp);
9384 }
9385 else
9386 {
9387 imm_expr.X_op = O_big;
9388 imm_expr.X_add_number = 4;
9389 if (! target_big_endian)
9390 {
9391 generic_bignum[0] = bfd_getl16 (temp);
9392 generic_bignum[1] = bfd_getl16 (temp + 2);
9393 generic_bignum[2] = bfd_getl16 (temp + 4);
9394 generic_bignum[3] = bfd_getl16 (temp + 6);
9395 }
9396 else
9397 {
9398 generic_bignum[0] = bfd_getb16 (temp + 6);
9399 generic_bignum[1] = bfd_getb16 (temp + 4);
9400 generic_bignum[2] = bfd_getb16 (temp + 2);
9401 generic_bignum[3] = bfd_getb16 (temp);
9402 }
9403 }
9404 }
9405 else
9406 {
9407 const char *newname;
9408 segT new_seg;
9409
9410 /* Switch to the right section. */
9411 seg = now_seg;
9412 subseg = now_subseg;
9413 switch (*args)
9414 {
9415 default: /* unused default case avoids warnings. */
9416 case 'L':
9417 newname = RDATA_SECTION_NAME;
3e722fb5 9418 if (g_switch_value >= 8)
252b5132
RH
9419 newname = ".lit8";
9420 break;
9421 case 'F':
3e722fb5 9422 newname = RDATA_SECTION_NAME;
252b5132
RH
9423 break;
9424 case 'l':
4d0d148d 9425 assert (g_switch_value >= 4);
252b5132
RH
9426 newname = ".lit4";
9427 break;
9428 }
9429 new_seg = subseg_new (newname, (subsegT) 0);
f43abd2b 9430 if (IS_ELF)
252b5132
RH
9431 bfd_set_section_flags (stdoutput, new_seg,
9432 (SEC_ALLOC
9433 | SEC_LOAD
9434 | SEC_READONLY
9435 | SEC_DATA));
9436 frag_align (*args == 'l' ? 2 : 3, 0, 0);
f43abd2b 9437 if (IS_ELF && strcmp (TARGET_OS, "elf") != 0)
252b5132
RH
9438 record_alignment (new_seg, 4);
9439 else
9440 record_alignment (new_seg, *args == 'l' ? 2 : 3);
9441 if (seg == now_seg)
9442 as_bad (_("Can't use floating point insn in this section"));
9443
9444 /* Set the argument to the current address in the
9445 section. */
9446 offset_expr.X_op = O_symbol;
9447 offset_expr.X_add_symbol =
9448 symbol_new ("L0\001", now_seg,
9449 (valueT) frag_now_fix (), frag_now);
9450 offset_expr.X_add_number = 0;
9451
9452 /* Put the floating point number into the section. */
9453 p = frag_more ((int) length);
9454 memcpy (p, temp, length);
9455
9456 /* Switch back to the original section. */
9457 subseg_set (seg, subseg);
9458 }
9459 }
9460 continue;
9461
9462 case 'i': /* 16 bit unsigned immediate */
9463 case 'j': /* 16 bit signed immediate */
f6688943 9464 *imm_reloc = BFD_RELOC_LO16;
5e0116d5 9465 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
252b5132
RH
9466 {
9467 int more;
5e0116d5
RS
9468 offsetT minval, maxval;
9469
9470 more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9471 && strcmp (insn->name, insn[1].name) == 0);
9472
9473 /* If the expression was written as an unsigned number,
9474 only treat it as signed if there are no more
9475 alternatives. */
9476 if (more
9477 && *args == 'j'
9478 && sizeof (imm_expr.X_add_number) <= 4
9479 && imm_expr.X_op == O_constant
9480 && imm_expr.X_add_number < 0
9481 && imm_expr.X_unsigned
9482 && HAVE_64BIT_GPRS)
9483 break;
9484
9485 /* For compatibility with older assemblers, we accept
9486 0x8000-0xffff as signed 16-bit numbers when only
9487 signed numbers are allowed. */
9488 if (*args == 'i')
9489 minval = 0, maxval = 0xffff;
9490 else if (more)
9491 minval = -0x8000, maxval = 0x7fff;
252b5132 9492 else
5e0116d5
RS
9493 minval = -0x8000, maxval = 0xffff;
9494
9495 if (imm_expr.X_op != O_constant
9496 || imm_expr.X_add_number < minval
9497 || imm_expr.X_add_number > maxval)
252b5132
RH
9498 {
9499 if (more)
9500 break;
2ae7e77b
AH
9501 if (imm_expr.X_op == O_constant
9502 || imm_expr.X_op == O_big)
5e0116d5 9503 as_bad (_("expression out of range"));
252b5132
RH
9504 }
9505 }
9506 s = expr_end;
9507 continue;
9508
9509 case 'o': /* 16 bit offset */
5e0116d5
RS
9510 /* Check whether there is only a single bracketed expression
9511 left. If so, it must be the base register and the
9512 constant must be zero. */
9513 if (*s == '(' && strchr (s + 1, '(') == 0)
9514 {
9515 offset_expr.X_op = O_constant;
9516 offset_expr.X_add_number = 0;
9517 continue;
9518 }
252b5132
RH
9519
9520 /* If this value won't fit into a 16 bit offset, then go
9521 find a macro that will generate the 32 bit offset
afdbd6d0 9522 code pattern. */
5e0116d5 9523 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
252b5132
RH
9524 && (offset_expr.X_op != O_constant
9525 || offset_expr.X_add_number >= 0x8000
afdbd6d0 9526 || offset_expr.X_add_number < -0x8000))
252b5132
RH
9527 break;
9528
252b5132
RH
9529 s = expr_end;
9530 continue;
9531
9532 case 'p': /* pc relative offset */
0b25d3e6 9533 *offset_reloc = BFD_RELOC_16_PCREL_S2;
252b5132
RH
9534 my_getExpression (&offset_expr, s);
9535 s = expr_end;
9536 continue;
9537
9538 case 'u': /* upper 16 bits */
5e0116d5
RS
9539 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9540 && imm_expr.X_op == O_constant
9541 && (imm_expr.X_add_number < 0
9542 || imm_expr.X_add_number >= 0x10000))
252b5132
RH
9543 as_bad (_("lui expression not in range 0..65535"));
9544 s = expr_end;
9545 continue;
9546
9547 case 'a': /* 26 bit address */
9548 my_getExpression (&offset_expr, s);
9549 s = expr_end;
f6688943 9550 *offset_reloc = BFD_RELOC_MIPS_JMP;
252b5132
RH
9551 continue;
9552
9553 case 'N': /* 3 bit branch condition code */
9554 case 'M': /* 3 bit compare condition code */
707bfff6
TS
9555 rtype = RTYPE_CCC;
9556 if (ip->insn_mo->pinfo & (FP_D| FP_S))
9557 rtype |= RTYPE_FCC;
9558 if (!reg_lookup (&s, rtype, &regno))
252b5132 9559 break;
30c378fd
CD
9560 if ((strcmp(str + strlen(str) - 3, ".ps") == 0
9561 || strcmp(str + strlen(str) - 5, "any2f") == 0
9562 || strcmp(str + strlen(str) - 5, "any2t") == 0)
9563 && (regno & 1) != 0)
9564 as_warn(_("Condition code register should be even for %s, was %d"),
9565 str, regno);
9566 if ((strcmp(str + strlen(str) - 5, "any4f") == 0
9567 || strcmp(str + strlen(str) - 5, "any4t") == 0)
9568 && (regno & 3) != 0)
9569 as_warn(_("Condition code register should be 0 or 4 for %s, was %d"),
9570 str, regno);
252b5132 9571 if (*args == 'N')
bf12938e 9572 INSERT_OPERAND (BCC, *ip, regno);
252b5132 9573 else
bf12938e 9574 INSERT_OPERAND (CCC, *ip, regno);
beae10d5 9575 continue;
252b5132 9576
156c2f8b
NC
9577 case 'H':
9578 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9579 s += 2;
3882b010 9580 if (ISDIGIT (*s))
156c2f8b
NC
9581 {
9582 c = 0;
9583 do
9584 {
9585 c *= 10;
9586 c += *s - '0';
9587 ++s;
9588 }
3882b010 9589 while (ISDIGIT (*s));
156c2f8b
NC
9590 }
9591 else
9592 c = 8; /* Invalid sel value. */
9593
9594 if (c > 7)
9595 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9596 ip->insn_opcode |= c;
9597 continue;
9598
60b63b72
RS
9599 case 'e':
9600 /* Must be at least one digit. */
9601 my_getExpression (&imm_expr, s);
9602 check_absolute_expr (ip, &imm_expr);
9603
9604 if ((unsigned long) imm_expr.X_add_number
9605 > (unsigned long) OP_MASK_VECBYTE)
9606 {
9607 as_bad (_("bad byte vector index (%ld)"),
9608 (long) imm_expr.X_add_number);
9609 imm_expr.X_add_number = 0;
9610 }
9611
bf12938e 9612 INSERT_OPERAND (VECBYTE, *ip, imm_expr.X_add_number);
60b63b72
RS
9613 imm_expr.X_op = O_absent;
9614 s = expr_end;
9615 continue;
9616
9617 case '%':
9618 my_getExpression (&imm_expr, s);
9619 check_absolute_expr (ip, &imm_expr);
9620
9621 if ((unsigned long) imm_expr.X_add_number
9622 > (unsigned long) OP_MASK_VECALIGN)
9623 {
9624 as_bad (_("bad byte vector index (%ld)"),
9625 (long) imm_expr.X_add_number);
9626 imm_expr.X_add_number = 0;
9627 }
9628
bf12938e 9629 INSERT_OPERAND (VECALIGN, *ip, imm_expr.X_add_number);
60b63b72
RS
9630 imm_expr.X_op = O_absent;
9631 s = expr_end;
9632 continue;
9633
252b5132
RH
9634 default:
9635 as_bad (_("bad char = '%c'\n"), *args);
9636 internalError ();
9637 }
9638 break;
9639 }
9640 /* Args don't match. */
9641 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9642 !strcmp (insn->name, insn[1].name))
9643 {
9644 ++insn;
9645 s = argsStart;
268f6bed 9646 insn_error = _("illegal operands");
252b5132
RH
9647 continue;
9648 }
268f6bed
L
9649 if (save_c)
9650 *(--s) = save_c;
252b5132
RH
9651 insn_error = _("illegal operands");
9652 return;
9653 }
9654}
9655
0499d65b
TS
9656#define SKIP_SPACE_TABS(S) { while (*(S) == ' ' || *(S) == '\t') ++(S); }
9657
252b5132
RH
9658/* This routine assembles an instruction into its binary format when
9659 assembling for the mips16. As a side effect, it sets one of the
9660 global variables imm_reloc or offset_reloc to the type of
9661 relocation to do if one of the operands is an address expression.
9662 It also sets mips16_small and mips16_ext if the user explicitly
9663 requested a small or extended instruction. */
9664
9665static void
17a2f251 9666mips16_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
9667{
9668 char *s;
9669 const char *args;
9670 struct mips_opcode *insn;
9671 char *argsstart;
9672 unsigned int regno;
9673 unsigned int lastregno = 0;
9674 char *s_reset;
d6f16593 9675 size_t i;
252b5132
RH
9676
9677 insn_error = NULL;
9678
b34976b6
AM
9679 mips16_small = FALSE;
9680 mips16_ext = FALSE;
252b5132 9681
3882b010 9682 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
9683 ;
9684 switch (*s)
9685 {
9686 case '\0':
9687 break;
9688
9689 case ' ':
9690 *s++ = '\0';
9691 break;
9692
9693 case '.':
9694 if (s[1] == 't' && s[2] == ' ')
9695 {
9696 *s = '\0';
b34976b6 9697 mips16_small = TRUE;
252b5132
RH
9698 s += 3;
9699 break;
9700 }
9701 else if (s[1] == 'e' && s[2] == ' ')
9702 {
9703 *s = '\0';
b34976b6 9704 mips16_ext = TRUE;
252b5132
RH
9705 s += 3;
9706 break;
9707 }
9708 /* Fall through. */
9709 default:
9710 insn_error = _("unknown opcode");
9711 return;
9712 }
9713
9714 if (mips_opts.noautoextend && ! mips16_ext)
b34976b6 9715 mips16_small = TRUE;
252b5132
RH
9716
9717 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9718 {
9719 insn_error = _("unrecognized opcode");
9720 return;
9721 }
9722
9723 argsstart = s;
9724 for (;;)
9725 {
9b3f89ee
TS
9726 bfd_boolean ok;
9727
252b5132
RH
9728 assert (strcmp (insn->name, str) == 0);
9729
9b3f89ee
TS
9730 if (OPCODE_IS_MEMBER (insn, mips_opts.isa, mips_opts.arch))
9731 ok = TRUE;
9732 else
9733 ok = FALSE;
9734
9735 if (! ok)
9736 {
9737 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes]
9738 && strcmp (insn->name, insn[1].name) == 0)
9739 {
9740 ++insn;
9741 continue;
9742 }
9743 else
9744 {
9745 if (!insn_error)
9746 {
9747 static char buf[100];
9748 sprintf (buf,
9749 _("opcode not supported on this processor: %s (%s)"),
9750 mips_cpu_info_from_arch (mips_opts.arch)->name,
9751 mips_cpu_info_from_isa (mips_opts.isa)->name);
9752 insn_error = buf;
9753 }
9754 return;
9755 }
9756 }
9757
1e915849 9758 create_insn (ip, insn);
252b5132 9759 imm_expr.X_op = O_absent;
f6688943
TS
9760 imm_reloc[0] = BFD_RELOC_UNUSED;
9761 imm_reloc[1] = BFD_RELOC_UNUSED;
9762 imm_reloc[2] = BFD_RELOC_UNUSED;
5f74bc13 9763 imm2_expr.X_op = O_absent;
252b5132 9764 offset_expr.X_op = O_absent;
f6688943
TS
9765 offset_reloc[0] = BFD_RELOC_UNUSED;
9766 offset_reloc[1] = BFD_RELOC_UNUSED;
9767 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
9768 for (args = insn->args; 1; ++args)
9769 {
9770 int c;
9771
9772 if (*s == ' ')
9773 ++s;
9774
9775 /* In this switch statement we call break if we did not find
9776 a match, continue if we did find a match, or return if we
9777 are done. */
9778
9779 c = *args;
9780 switch (c)
9781 {
9782 case '\0':
9783 if (*s == '\0')
9784 {
9785 /* Stuff the immediate value in now, if we can. */
9786 if (imm_expr.X_op == O_constant
f6688943 9787 && *imm_reloc > BFD_RELOC_UNUSED
252b5132
RH
9788 && insn->pinfo != INSN_MACRO)
9789 {
d6f16593
MR
9790 valueT tmp;
9791
9792 switch (*offset_reloc)
9793 {
9794 case BFD_RELOC_MIPS16_HI16_S:
9795 tmp = (imm_expr.X_add_number + 0x8000) >> 16;
9796 break;
9797
9798 case BFD_RELOC_MIPS16_HI16:
9799 tmp = imm_expr.X_add_number >> 16;
9800 break;
9801
9802 case BFD_RELOC_MIPS16_LO16:
9803 tmp = ((imm_expr.X_add_number + 0x8000) & 0xffff)
9804 - 0x8000;
9805 break;
9806
9807 case BFD_RELOC_UNUSED:
9808 tmp = imm_expr.X_add_number;
9809 break;
9810
9811 default:
9812 internalError ();
9813 }
9814 *offset_reloc = BFD_RELOC_UNUSED;
9815
c4e7957c 9816 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
d6f16593 9817 tmp, TRUE, mips16_small,
252b5132
RH
9818 mips16_ext, &ip->insn_opcode,
9819 &ip->use_extend, &ip->extend);
9820 imm_expr.X_op = O_absent;
f6688943 9821 *imm_reloc = BFD_RELOC_UNUSED;
252b5132
RH
9822 }
9823
9824 return;
9825 }
9826 break;
9827
9828 case ',':
9829 if (*s++ == c)
9830 continue;
9831 s--;
9832 switch (*++args)
9833 {
9834 case 'v':
bf12938e 9835 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
252b5132
RH
9836 continue;
9837 case 'w':
bf12938e 9838 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
252b5132
RH
9839 continue;
9840 }
9841 break;
9842
9843 case '(':
9844 case ')':
9845 if (*s++ == c)
9846 continue;
9847 break;
9848
9849 case 'v':
9850 case 'w':
9851 if (s[0] != '$')
9852 {
9853 if (c == 'v')
bf12938e 9854 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
252b5132 9855 else
bf12938e 9856 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
252b5132
RH
9857 ++args;
9858 continue;
9859 }
9860 /* Fall through. */
9861 case 'x':
9862 case 'y':
9863 case 'z':
9864 case 'Z':
9865 case '0':
9866 case 'S':
9867 case 'R':
9868 case 'X':
9869 case 'Y':
707bfff6
TS
9870 s_reset = s;
9871 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
252b5132 9872 {
707bfff6 9873 if (c == 'v' || c == 'w')
85b51719 9874 {
707bfff6 9875 if (c == 'v')
a9e24354 9876 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
707bfff6 9877 else
a9e24354 9878 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
707bfff6
TS
9879 ++args;
9880 continue;
85b51719 9881 }
707bfff6 9882 break;
252b5132
RH
9883 }
9884
9885 if (*s == ' ')
9886 ++s;
9887 if (args[1] != *s)
9888 {
9889 if (c == 'v' || c == 'w')
9890 {
9891 regno = mips16_to_32_reg_map[lastregno];
9892 s = s_reset;
f9419b05 9893 ++args;
252b5132
RH
9894 }
9895 }
9896
9897 switch (c)
9898 {
9899 case 'x':
9900 case 'y':
9901 case 'z':
9902 case 'v':
9903 case 'w':
9904 case 'Z':
9905 regno = mips32_to_16_reg_map[regno];
9906 break;
9907
9908 case '0':
9909 if (regno != 0)
9910 regno = ILLEGAL_REG;
9911 break;
9912
9913 case 'S':
9914 if (regno != SP)
9915 regno = ILLEGAL_REG;
9916 break;
9917
9918 case 'R':
9919 if (regno != RA)
9920 regno = ILLEGAL_REG;
9921 break;
9922
9923 case 'X':
9924 case 'Y':
9925 if (regno == AT && ! mips_opts.noat)
9926 as_warn (_("used $at without \".set noat\""));
9927 break;
9928
9929 default:
9930 internalError ();
9931 }
9932
9933 if (regno == ILLEGAL_REG)
9934 break;
9935
9936 switch (c)
9937 {
9938 case 'x':
9939 case 'v':
bf12938e 9940 MIPS16_INSERT_OPERAND (RX, *ip, regno);
252b5132
RH
9941 break;
9942 case 'y':
9943 case 'w':
bf12938e 9944 MIPS16_INSERT_OPERAND (RY, *ip, regno);
252b5132
RH
9945 break;
9946 case 'z':
bf12938e 9947 MIPS16_INSERT_OPERAND (RZ, *ip, regno);
252b5132
RH
9948 break;
9949 case 'Z':
bf12938e 9950 MIPS16_INSERT_OPERAND (MOVE32Z, *ip, regno);
252b5132
RH
9951 case '0':
9952 case 'S':
9953 case 'R':
9954 break;
9955 case 'X':
bf12938e 9956 MIPS16_INSERT_OPERAND (REGR32, *ip, regno);
252b5132
RH
9957 break;
9958 case 'Y':
9959 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
bf12938e 9960 MIPS16_INSERT_OPERAND (REG32R, *ip, regno);
252b5132
RH
9961 break;
9962 default:
9963 internalError ();
9964 }
9965
9966 lastregno = regno;
9967 continue;
9968
9969 case 'P':
9970 if (strncmp (s, "$pc", 3) == 0)
9971 {
9972 s += 3;
9973 continue;
9974 }
9975 break;
9976
252b5132
RH
9977 case '5':
9978 case 'H':
9979 case 'W':
9980 case 'D':
9981 case 'j':
252b5132
RH
9982 case 'V':
9983 case 'C':
9984 case 'U':
9985 case 'k':
9986 case 'K':
d6f16593
MR
9987 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
9988 if (i > 0)
252b5132 9989 {
d6f16593 9990 if (imm_expr.X_op != O_constant)
252b5132 9991 {
b34976b6 9992 mips16_ext = TRUE;
b34976b6 9993 ip->use_extend = TRUE;
252b5132 9994 ip->extend = 0;
252b5132 9995 }
d6f16593
MR
9996 else
9997 {
9998 /* We need to relax this instruction. */
9999 *offset_reloc = *imm_reloc;
10000 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
10001 }
10002 s = expr_end;
10003 continue;
252b5132 10004 }
d6f16593
MR
10005 *imm_reloc = BFD_RELOC_UNUSED;
10006 /* Fall through. */
10007 case '<':
10008 case '>':
10009 case '[':
10010 case ']':
10011 case '4':
10012 case '8':
10013 my_getExpression (&imm_expr, s);
252b5132
RH
10014 if (imm_expr.X_op == O_register)
10015 {
10016 /* What we thought was an expression turned out to
10017 be a register. */
10018
10019 if (s[0] == '(' && args[1] == '(')
10020 {
10021 /* It looks like the expression was omitted
10022 before a register indirection, which means
10023 that the expression is implicitly zero. We
10024 still set up imm_expr, so that we handle
10025 explicit extensions correctly. */
10026 imm_expr.X_op = O_constant;
10027 imm_expr.X_add_number = 0;
f6688943 10028 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
10029 continue;
10030 }
10031
10032 break;
10033 }
10034
10035 /* We need to relax this instruction. */
f6688943 10036 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
10037 s = expr_end;
10038 continue;
10039
10040 case 'p':
10041 case 'q':
10042 case 'A':
10043 case 'B':
10044 case 'E':
10045 /* We use offset_reloc rather than imm_reloc for the PC
10046 relative operands. This lets macros with both
10047 immediate and address operands work correctly. */
10048 my_getExpression (&offset_expr, s);
10049
10050 if (offset_expr.X_op == O_register)
10051 break;
10052
10053 /* We need to relax this instruction. */
f6688943 10054 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
10055 s = expr_end;
10056 continue;
10057
10058 case '6': /* break code */
10059 my_getExpression (&imm_expr, s);
10060 check_absolute_expr (ip, &imm_expr);
10061 if ((unsigned long) imm_expr.X_add_number > 63)
bf12938e
RS
10062 as_warn (_("Invalid value for `%s' (%lu)"),
10063 ip->insn_mo->name,
10064 (unsigned long) imm_expr.X_add_number);
10065 MIPS16_INSERT_OPERAND (IMM6, *ip, imm_expr.X_add_number);
252b5132
RH
10066 imm_expr.X_op = O_absent;
10067 s = expr_end;
10068 continue;
10069
10070 case 'a': /* 26 bit address */
10071 my_getExpression (&offset_expr, s);
10072 s = expr_end;
f6688943 10073 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132
RH
10074 ip->insn_opcode <<= 16;
10075 continue;
10076
10077 case 'l': /* register list for entry macro */
10078 case 'L': /* register list for exit macro */
10079 {
10080 int mask;
10081
10082 if (c == 'l')
10083 mask = 0;
10084 else
10085 mask = 7 << 3;
10086 while (*s != '\0')
10087 {
707bfff6 10088 unsigned int freg, reg1, reg2;
252b5132
RH
10089
10090 while (*s == ' ' || *s == ',')
10091 ++s;
707bfff6 10092 if (reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
252b5132 10093 freg = 0;
707bfff6
TS
10094 else if (reg_lookup (&s, RTYPE_FPU, &reg1))
10095 freg = 1;
252b5132
RH
10096 else
10097 {
707bfff6
TS
10098 as_bad (_("can't parse register list"));
10099 break;
252b5132
RH
10100 }
10101 if (*s == ' ')
10102 ++s;
10103 if (*s != '-')
10104 reg2 = reg1;
10105 else
10106 {
10107 ++s;
707bfff6
TS
10108 if (!reg_lookup (&s, freg ? RTYPE_FPU
10109 : (RTYPE_GP | RTYPE_NUM), &reg2))
252b5132 10110 {
707bfff6
TS
10111 as_bad (_("invalid register list"));
10112 break;
252b5132
RH
10113 }
10114 }
10115 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
10116 {
10117 mask &= ~ (7 << 3);
10118 mask |= 5 << 3;
10119 }
10120 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
10121 {
10122 mask &= ~ (7 << 3);
10123 mask |= 6 << 3;
10124 }
10125 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
10126 mask |= (reg2 - 3) << 3;
10127 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
10128 mask |= (reg2 - 15) << 1;
f9419b05 10129 else if (reg1 == RA && reg2 == RA)
252b5132
RH
10130 mask |= 1;
10131 else
10132 {
10133 as_bad (_("invalid register list"));
10134 break;
10135 }
10136 }
10137 /* The mask is filled in in the opcode table for the
10138 benefit of the disassembler. We remove it before
10139 applying the actual mask. */
10140 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
10141 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
10142 }
10143 continue;
10144
0499d65b
TS
10145 case 'm': /* Register list for save insn. */
10146 case 'M': /* Register list for restore insn. */
10147 {
10148 int opcode = 0;
10149 int framesz = 0, seen_framesz = 0;
10150 int args = 0, statics = 0, sregs = 0;
10151
10152 while (*s != '\0')
10153 {
10154 unsigned int reg1, reg2;
10155
10156 SKIP_SPACE_TABS (s);
10157 while (*s == ',')
10158 ++s;
10159 SKIP_SPACE_TABS (s);
10160
10161 my_getExpression (&imm_expr, s);
10162 if (imm_expr.X_op == O_constant)
10163 {
10164 /* Handle the frame size. */
10165 if (seen_framesz)
10166 {
10167 as_bad (_("more than one frame size in list"));
10168 break;
10169 }
10170 seen_framesz = 1;
10171 framesz = imm_expr.X_add_number;
10172 imm_expr.X_op = O_absent;
10173 s = expr_end;
10174 continue;
10175 }
10176
707bfff6 10177 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
0499d65b
TS
10178 {
10179 as_bad (_("can't parse register list"));
10180 break;
10181 }
0499d65b 10182
707bfff6
TS
10183 while (*s == ' ')
10184 ++s;
10185
0499d65b
TS
10186 if (*s != '-')
10187 reg2 = reg1;
10188 else
10189 {
10190 ++s;
707bfff6
TS
10191 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg2)
10192 || reg2 < reg1)
0499d65b
TS
10193 {
10194 as_bad (_("can't parse register list"));
10195 break;
10196 }
0499d65b
TS
10197 }
10198
10199 while (reg1 <= reg2)
10200 {
10201 if (reg1 >= 4 && reg1 <= 7)
10202 {
3a93f742 10203 if (!seen_framesz)
0499d65b
TS
10204 /* args $a0-$a3 */
10205 args |= 1 << (reg1 - 4);
10206 else
10207 /* statics $a0-$a3 */
10208 statics |= 1 << (reg1 - 4);
10209 }
10210 else if ((reg1 >= 16 && reg1 <= 23) || reg1 == 30)
10211 {
10212 /* $s0-$s8 */
10213 sregs |= 1 << ((reg1 == 30) ? 8 : (reg1 - 16));
10214 }
10215 else if (reg1 == 31)
10216 {
10217 /* Add $ra to insn. */
10218 opcode |= 0x40;
10219 }
10220 else
10221 {
10222 as_bad (_("unexpected register in list"));
10223 break;
10224 }
10225 if (++reg1 == 24)
10226 reg1 = 30;
10227 }
10228 }
10229
10230 /* Encode args/statics combination. */
10231 if (args & statics)
10232 as_bad (_("arg/static registers overlap"));
10233 else if (args == 0xf)
10234 /* All $a0-$a3 are args. */
10235 opcode |= MIPS16_ALL_ARGS << 16;
10236 else if (statics == 0xf)
10237 /* All $a0-$a3 are statics. */
10238 opcode |= MIPS16_ALL_STATICS << 16;
10239 else
10240 {
10241 int narg = 0, nstat = 0;
10242
10243 /* Count arg registers. */
10244 while (args & 0x1)
10245 {
10246 args >>= 1;
10247 narg++;
10248 }
10249 if (args != 0)
10250 as_bad (_("invalid arg register list"));
10251
10252 /* Count static registers. */
10253 while (statics & 0x8)
10254 {
10255 statics = (statics << 1) & 0xf;
10256 nstat++;
10257 }
10258 if (statics != 0)
10259 as_bad (_("invalid static register list"));
10260
10261 /* Encode args/statics. */
10262 opcode |= ((narg << 2) | nstat) << 16;
10263 }
10264
10265 /* Encode $s0/$s1. */
10266 if (sregs & (1 << 0)) /* $s0 */
10267 opcode |= 0x20;
10268 if (sregs & (1 << 1)) /* $s1 */
10269 opcode |= 0x10;
10270 sregs >>= 2;
10271
10272 if (sregs != 0)
10273 {
10274 /* Count regs $s2-$s8. */
10275 int nsreg = 0;
10276 while (sregs & 1)
10277 {
10278 sregs >>= 1;
10279 nsreg++;
10280 }
10281 if (sregs != 0)
10282 as_bad (_("invalid static register list"));
10283 /* Encode $s2-$s8. */
10284 opcode |= nsreg << 24;
10285 }
10286
10287 /* Encode frame size. */
10288 if (!seen_framesz)
10289 as_bad (_("missing frame size"));
10290 else if ((framesz & 7) != 0 || framesz < 0
10291 || framesz > 0xff * 8)
10292 as_bad (_("invalid frame size"));
10293 else if (framesz != 128 || (opcode >> 16) != 0)
10294 {
10295 framesz /= 8;
10296 opcode |= (((framesz & 0xf0) << 16)
10297 | (framesz & 0x0f));
10298 }
10299
10300 /* Finally build the instruction. */
10301 if ((opcode >> 16) != 0 || framesz == 0)
10302 {
10303 ip->use_extend = TRUE;
10304 ip->extend = opcode >> 16;
10305 }
10306 ip->insn_opcode |= opcode & 0x7f;
10307 }
10308 continue;
10309
252b5132
RH
10310 case 'e': /* extend code */
10311 my_getExpression (&imm_expr, s);
10312 check_absolute_expr (ip, &imm_expr);
10313 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
10314 {
10315 as_warn (_("Invalid value for `%s' (%lu)"),
10316 ip->insn_mo->name,
10317 (unsigned long) imm_expr.X_add_number);
10318 imm_expr.X_add_number &= 0x7ff;
10319 }
10320 ip->insn_opcode |= imm_expr.X_add_number;
10321 imm_expr.X_op = O_absent;
10322 s = expr_end;
10323 continue;
10324
10325 default:
10326 internalError ();
10327 }
10328 break;
10329 }
10330
10331 /* Args don't match. */
10332 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
10333 strcmp (insn->name, insn[1].name) == 0)
10334 {
10335 ++insn;
10336 s = argsstart;
10337 continue;
10338 }
10339
10340 insn_error = _("illegal operands");
10341
10342 return;
10343 }
10344}
10345
10346/* This structure holds information we know about a mips16 immediate
10347 argument type. */
10348
e972090a
NC
10349struct mips16_immed_operand
10350{
252b5132
RH
10351 /* The type code used in the argument string in the opcode table. */
10352 int type;
10353 /* The number of bits in the short form of the opcode. */
10354 int nbits;
10355 /* The number of bits in the extended form of the opcode. */
10356 int extbits;
10357 /* The amount by which the short form is shifted when it is used;
10358 for example, the sw instruction has a shift count of 2. */
10359 int shift;
10360 /* The amount by which the short form is shifted when it is stored
10361 into the instruction code. */
10362 int op_shift;
10363 /* Non-zero if the short form is unsigned. */
10364 int unsp;
10365 /* Non-zero if the extended form is unsigned. */
10366 int extu;
10367 /* Non-zero if the value is PC relative. */
10368 int pcrel;
10369};
10370
10371/* The mips16 immediate operand types. */
10372
10373static const struct mips16_immed_operand mips16_immed_operands[] =
10374{
10375 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
10376 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
10377 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
10378 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
10379 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
10380 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
10381 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
10382 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
10383 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
10384 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
10385 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
10386 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
10387 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
10388 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
10389 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
10390 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
10391 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10392 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10393 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
10394 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
10395 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
10396};
10397
10398#define MIPS16_NUM_IMMED \
10399 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
10400
10401/* Handle a mips16 instruction with an immediate value. This or's the
10402 small immediate value into *INSN. It sets *USE_EXTEND to indicate
10403 whether an extended value is needed; if one is needed, it sets
10404 *EXTEND to the value. The argument type is TYPE. The value is VAL.
10405 If SMALL is true, an unextended opcode was explicitly requested.
10406 If EXT is true, an extended opcode was explicitly requested. If
10407 WARN is true, warn if EXT does not match reality. */
10408
10409static void
17a2f251
TS
10410mips16_immed (char *file, unsigned int line, int type, offsetT val,
10411 bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
10412 unsigned long *insn, bfd_boolean *use_extend,
10413 unsigned short *extend)
252b5132 10414{
3994f87e 10415 const struct mips16_immed_operand *op;
252b5132 10416 int mintiny, maxtiny;
b34976b6 10417 bfd_boolean needext;
252b5132
RH
10418
10419 op = mips16_immed_operands;
10420 while (op->type != type)
10421 {
10422 ++op;
10423 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
10424 }
10425
10426 if (op->unsp)
10427 {
10428 if (type == '<' || type == '>' || type == '[' || type == ']')
10429 {
10430 mintiny = 1;
10431 maxtiny = 1 << op->nbits;
10432 }
10433 else
10434 {
10435 mintiny = 0;
10436 maxtiny = (1 << op->nbits) - 1;
10437 }
10438 }
10439 else
10440 {
10441 mintiny = - (1 << (op->nbits - 1));
10442 maxtiny = (1 << (op->nbits - 1)) - 1;
10443 }
10444
10445 /* Branch offsets have an implicit 0 in the lowest bit. */
10446 if (type == 'p' || type == 'q')
10447 val /= 2;
10448
10449 if ((val & ((1 << op->shift) - 1)) != 0
10450 || val < (mintiny << op->shift)
10451 || val > (maxtiny << op->shift))
b34976b6 10452 needext = TRUE;
252b5132 10453 else
b34976b6 10454 needext = FALSE;
252b5132
RH
10455
10456 if (warn && ext && ! needext)
beae10d5
KH
10457 as_warn_where (file, line,
10458 _("extended operand requested but not required"));
252b5132
RH
10459 if (small && needext)
10460 as_bad_where (file, line, _("invalid unextended operand value"));
10461
10462 if (small || (! ext && ! needext))
10463 {
10464 int insnval;
10465
b34976b6 10466 *use_extend = FALSE;
252b5132
RH
10467 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
10468 insnval <<= op->op_shift;
10469 *insn |= insnval;
10470 }
10471 else
10472 {
10473 long minext, maxext;
10474 int extval;
10475
10476 if (op->extu)
10477 {
10478 minext = 0;
10479 maxext = (1 << op->extbits) - 1;
10480 }
10481 else
10482 {
10483 minext = - (1 << (op->extbits - 1));
10484 maxext = (1 << (op->extbits - 1)) - 1;
10485 }
10486 if (val < minext || val > maxext)
10487 as_bad_where (file, line,
10488 _("operand value out of range for instruction"));
10489
b34976b6 10490 *use_extend = TRUE;
252b5132
RH
10491 if (op->extbits == 16)
10492 {
10493 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
10494 val &= 0x1f;
10495 }
10496 else if (op->extbits == 15)
10497 {
10498 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
10499 val &= 0xf;
10500 }
10501 else
10502 {
10503 extval = ((val & 0x1f) << 6) | (val & 0x20);
10504 val = 0;
10505 }
10506
10507 *extend = (unsigned short) extval;
10508 *insn |= val;
10509 }
10510}
10511\f
d6f16593 10512struct percent_op_match
ad8d3bb3 10513{
5e0116d5
RS
10514 const char *str;
10515 bfd_reloc_code_real_type reloc;
d6f16593
MR
10516};
10517
10518static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 10519{
5e0116d5 10520 {"%lo", BFD_RELOC_LO16},
ad8d3bb3 10521#ifdef OBJ_ELF
5e0116d5
RS
10522 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
10523 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
10524 {"%call16", BFD_RELOC_MIPS_CALL16},
10525 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
10526 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
10527 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
10528 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
10529 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
10530 {"%got", BFD_RELOC_MIPS_GOT16},
10531 {"%gp_rel", BFD_RELOC_GPREL16},
10532 {"%half", BFD_RELOC_16},
10533 {"%highest", BFD_RELOC_MIPS_HIGHEST},
10534 {"%higher", BFD_RELOC_MIPS_HIGHER},
10535 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
10536 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
10537 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
10538 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
10539 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
10540 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
10541 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
10542 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
ad8d3bb3 10543#endif
5e0116d5 10544 {"%hi", BFD_RELOC_HI16_S}
ad8d3bb3
TS
10545};
10546
d6f16593
MR
10547static const struct percent_op_match mips16_percent_op[] =
10548{
10549 {"%lo", BFD_RELOC_MIPS16_LO16},
10550 {"%gprel", BFD_RELOC_MIPS16_GPREL},
10551 {"%hi", BFD_RELOC_MIPS16_HI16_S}
10552};
10553
252b5132 10554
5e0116d5
RS
10555/* Return true if *STR points to a relocation operator. When returning true,
10556 move *STR over the operator and store its relocation code in *RELOC.
10557 Leave both *STR and *RELOC alone when returning false. */
10558
10559static bfd_boolean
17a2f251 10560parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 10561{
d6f16593
MR
10562 const struct percent_op_match *percent_op;
10563 size_t limit, i;
10564
10565 if (mips_opts.mips16)
10566 {
10567 percent_op = mips16_percent_op;
10568 limit = ARRAY_SIZE (mips16_percent_op);
10569 }
10570 else
10571 {
10572 percent_op = mips_percent_op;
10573 limit = ARRAY_SIZE (mips_percent_op);
10574 }
76b3015f 10575
d6f16593 10576 for (i = 0; i < limit; i++)
5e0116d5 10577 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 10578 {
3f98094e
DJ
10579 int len = strlen (percent_op[i].str);
10580
10581 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
10582 continue;
10583
5e0116d5
RS
10584 *str += strlen (percent_op[i].str);
10585 *reloc = percent_op[i].reloc;
394f9b3a 10586
5e0116d5
RS
10587 /* Check whether the output BFD supports this relocation.
10588 If not, issue an error and fall back on something safe. */
10589 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 10590 {
5e0116d5
RS
10591 as_bad ("relocation %s isn't supported by the current ABI",
10592 percent_op[i].str);
01a3f561 10593 *reloc = BFD_RELOC_UNUSED;
394f9b3a 10594 }
5e0116d5 10595 return TRUE;
394f9b3a 10596 }
5e0116d5 10597 return FALSE;
394f9b3a 10598}
ad8d3bb3 10599
ad8d3bb3 10600
5e0116d5
RS
10601/* Parse string STR as a 16-bit relocatable operand. Store the
10602 expression in *EP and the relocations in the array starting
10603 at RELOC. Return the number of relocation operators used.
ad8d3bb3 10604
01a3f561 10605 On exit, EXPR_END points to the first character after the expression. */
ad8d3bb3 10606
5e0116d5 10607static size_t
17a2f251
TS
10608my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
10609 char *str)
ad8d3bb3 10610{
5e0116d5
RS
10611 bfd_reloc_code_real_type reversed_reloc[3];
10612 size_t reloc_index, i;
09b8f35a
RS
10613 int crux_depth, str_depth;
10614 char *crux;
5e0116d5
RS
10615
10616 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
10617 in REVERSED_RELOC. End the loop with CRUX pointing to the start
10618 of the main expression and with CRUX_DEPTH containing the number
10619 of open brackets at that point. */
10620 reloc_index = -1;
10621 str_depth = 0;
10622 do
fb1b3232 10623 {
09b8f35a
RS
10624 reloc_index++;
10625 crux = str;
10626 crux_depth = str_depth;
10627
10628 /* Skip over whitespace and brackets, keeping count of the number
10629 of brackets. */
10630 while (*str == ' ' || *str == '\t' || *str == '(')
10631 if (*str++ == '(')
10632 str_depth++;
5e0116d5 10633 }
09b8f35a
RS
10634 while (*str == '%'
10635 && reloc_index < (HAVE_NEWABI ? 3 : 1)
10636 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 10637
09b8f35a 10638 my_getExpression (ep, crux);
5e0116d5 10639 str = expr_end;
394f9b3a 10640
5e0116d5 10641 /* Match every open bracket. */
09b8f35a 10642 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 10643 if (*str++ == ')')
09b8f35a 10644 crux_depth--;
394f9b3a 10645
09b8f35a 10646 if (crux_depth > 0)
5e0116d5 10647 as_bad ("unclosed '('");
394f9b3a 10648
5e0116d5 10649 expr_end = str;
252b5132 10650
01a3f561 10651 if (reloc_index != 0)
64bdfcaf
RS
10652 {
10653 prev_reloc_op_frag = frag_now;
10654 for (i = 0; i < reloc_index; i++)
10655 reloc[i] = reversed_reloc[reloc_index - 1 - i];
10656 }
fb1b3232 10657
5e0116d5 10658 return reloc_index;
252b5132
RH
10659}
10660
10661static void
17a2f251 10662my_getExpression (expressionS *ep, char *str)
252b5132
RH
10663{
10664 char *save_in;
98aa84af 10665 valueT val;
252b5132
RH
10666
10667 save_in = input_line_pointer;
10668 input_line_pointer = str;
10669 expression (ep);
10670 expr_end = input_line_pointer;
10671 input_line_pointer = save_in;
10672
10673 /* If we are in mips16 mode, and this is an expression based on `.',
10674 then we bump the value of the symbol by 1 since that is how other
10675 text symbols are handled. We don't bother to handle complex
10676 expressions, just `.' plus or minus a constant. */
10677 if (mips_opts.mips16
10678 && ep->X_op == O_symbol
10679 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10680 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
49309057
ILT
10681 && symbol_get_frag (ep->X_add_symbol) == frag_now
10682 && symbol_constant_p (ep->X_add_symbol)
98aa84af
AM
10683 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10684 S_SET_VALUE (ep->X_add_symbol, val + 1);
252b5132
RH
10685}
10686
10687/* Turn a string in input_line_pointer into a floating point constant
bc0d738a
NC
10688 of type TYPE, and store the appropriate bytes in *LITP. The number
10689 of LITTLENUMS emitted is stored in *SIZEP. An error message is
252b5132
RH
10690 returned, or NULL on OK. */
10691
10692char *
17a2f251 10693md_atof (int type, char *litP, int *sizeP)
252b5132
RH
10694{
10695 int prec;
10696 LITTLENUM_TYPE words[4];
10697 char *t;
10698 int i;
10699
10700 switch (type)
10701 {
10702 case 'f':
10703 prec = 2;
10704 break;
10705
10706 case 'd':
10707 prec = 4;
10708 break;
10709
10710 default:
10711 *sizeP = 0;
10712 return _("bad call to md_atof");
10713 }
10714
10715 t = atof_ieee (input_line_pointer, type, words);
10716 if (t)
10717 input_line_pointer = t;
10718
10719 *sizeP = prec * 2;
10720
10721 if (! target_big_endian)
10722 {
10723 for (i = prec - 1; i >= 0; i--)
10724 {
17a2f251 10725 md_number_to_chars (litP, words[i], 2);
252b5132
RH
10726 litP += 2;
10727 }
10728 }
10729 else
10730 {
10731 for (i = 0; i < prec; i++)
10732 {
17a2f251 10733 md_number_to_chars (litP, words[i], 2);
252b5132
RH
10734 litP += 2;
10735 }
10736 }
bdaaa2e1 10737
252b5132
RH
10738 return NULL;
10739}
10740
10741void
17a2f251 10742md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
10743{
10744 if (target_big_endian)
10745 number_to_chars_bigendian (buf, val, n);
10746 else
10747 number_to_chars_littleendian (buf, val, n);
10748}
10749\f
ae948b86 10750#ifdef OBJ_ELF
e013f690
TS
10751static int support_64bit_objects(void)
10752{
10753 const char **list, **l;
aa3d8fdf 10754 int yes;
e013f690
TS
10755
10756 list = bfd_target_list ();
10757 for (l = list; *l != NULL; l++)
10758#ifdef TE_TMIPS
10759 /* This is traditional mips */
10760 if (strcmp (*l, "elf64-tradbigmips") == 0
10761 || strcmp (*l, "elf64-tradlittlemips") == 0)
10762#else
10763 if (strcmp (*l, "elf64-bigmips") == 0
10764 || strcmp (*l, "elf64-littlemips") == 0)
10765#endif
10766 break;
aa3d8fdf 10767 yes = (*l != NULL);
e013f690 10768 free (list);
aa3d8fdf 10769 return yes;
e013f690 10770}
ae948b86 10771#endif /* OBJ_ELF */
e013f690 10772
78849248 10773const char *md_shortopts = "O::g::G:";
252b5132 10774
e972090a
NC
10775struct option md_longopts[] =
10776{
f9b4148d
CD
10777 /* Options which specify architecture. */
10778#define OPTION_ARCH_BASE (OPTION_MD_BASE)
10779#define OPTION_MARCH (OPTION_ARCH_BASE + 0)
10780 {"march", required_argument, NULL, OPTION_MARCH},
10781#define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
10782 {"mtune", required_argument, NULL, OPTION_MTUNE},
10783#define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
252b5132
RH
10784 {"mips0", no_argument, NULL, OPTION_MIPS1},
10785 {"mips1", no_argument, NULL, OPTION_MIPS1},
f9b4148d 10786#define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
252b5132 10787 {"mips2", no_argument, NULL, OPTION_MIPS2},
f9b4148d 10788#define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
252b5132 10789 {"mips3", no_argument, NULL, OPTION_MIPS3},
f9b4148d 10790#define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
252b5132 10791 {"mips4", no_argument, NULL, OPTION_MIPS4},
f9b4148d 10792#define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
ae948b86 10793 {"mips5", no_argument, NULL, OPTION_MIPS5},
f9b4148d 10794#define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
ae948b86 10795 {"mips32", no_argument, NULL, OPTION_MIPS32},
f9b4148d 10796#define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
ae948b86 10797 {"mips64", no_argument, NULL, OPTION_MIPS64},
f9b4148d
CD
10798#define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
10799 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
5f74bc13
CD
10800#define OPTION_MIPS64R2 (OPTION_ARCH_BASE + 10)
10801 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
f9b4148d
CD
10802
10803 /* Options which specify Application Specific Extensions (ASEs). */
5f74bc13 10804#define OPTION_ASE_BASE (OPTION_ARCH_BASE + 11)
f9b4148d
CD
10805#define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
10806 {"mips16", no_argument, NULL, OPTION_MIPS16},
10807#define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
10808 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10809#define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
10810 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10811#define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
10812 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10813#define OPTION_MDMX (OPTION_ASE_BASE + 4)
10814 {"mdmx", no_argument, NULL, OPTION_MDMX},
10815#define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
10816 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
74cd071d
CF
10817#define OPTION_DSP (OPTION_ASE_BASE + 6)
10818 {"mdsp", no_argument, NULL, OPTION_DSP},
10819#define OPTION_NO_DSP (OPTION_ASE_BASE + 7)
10820 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
ef2e4d86
CF
10821#define OPTION_MT (OPTION_ASE_BASE + 8)
10822 {"mmt", no_argument, NULL, OPTION_MT},
10823#define OPTION_NO_MT (OPTION_ASE_BASE + 9)
10824 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
e16bfa71
TS
10825#define OPTION_SMARTMIPS (OPTION_ASE_BASE + 10)
10826 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
10827#define OPTION_NO_SMARTMIPS (OPTION_ASE_BASE + 11)
10828 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
f9b4148d
CD
10829
10830 /* Old-style architecture options. Don't add more of these. */
e16bfa71 10831#define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 12)
f9b4148d
CD
10832#define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
10833 {"m4650", no_argument, NULL, OPTION_M4650},
10834#define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
10835 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10836#define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
10837 {"m4010", no_argument, NULL, OPTION_M4010},
10838#define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
10839 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10840#define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
10841 {"m4100", no_argument, NULL, OPTION_M4100},
10842#define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
10843 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10844#define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
10845 {"m3900", no_argument, NULL, OPTION_M3900},
10846#define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
10847 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10848
10849 /* Options which enable bug fixes. */
10850#define OPTION_FIX_BASE (OPTION_COMPAT_ARCH_BASE + 8)
10851#define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
10852 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10853#define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
10854 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10855 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
d766e8ec
RS
10856#define OPTION_FIX_VR4120 (OPTION_FIX_BASE + 2)
10857#define OPTION_NO_FIX_VR4120 (OPTION_FIX_BASE + 3)
10858 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
10859 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
7d8e00cf
RS
10860#define OPTION_FIX_VR4130 (OPTION_FIX_BASE + 4)
10861#define OPTION_NO_FIX_VR4130 (OPTION_FIX_BASE + 5)
10862 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
10863 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
f9b4148d
CD
10864
10865 /* Miscellaneous options. */
7d8e00cf 10866#define OPTION_MISC_BASE (OPTION_FIX_BASE + 6)
1ffcab4b 10867#define OPTION_TRAP (OPTION_MISC_BASE + 0)
252b5132
RH
10868 {"trap", no_argument, NULL, OPTION_TRAP},
10869 {"no-break", no_argument, NULL, OPTION_TRAP},
1ffcab4b 10870#define OPTION_BREAK (OPTION_MISC_BASE + 1)
252b5132
RH
10871 {"break", no_argument, NULL, OPTION_BREAK},
10872 {"no-trap", no_argument, NULL, OPTION_BREAK},
1ffcab4b 10873#define OPTION_EB (OPTION_MISC_BASE + 2)
252b5132 10874 {"EB", no_argument, NULL, OPTION_EB},
1ffcab4b 10875#define OPTION_EL (OPTION_MISC_BASE + 3)
252b5132 10876 {"EL", no_argument, NULL, OPTION_EL},
1ffcab4b 10877#define OPTION_FP32 (OPTION_MISC_BASE + 4)
ae948b86 10878 {"mfp32", no_argument, NULL, OPTION_FP32},
1ffcab4b 10879#define OPTION_GP32 (OPTION_MISC_BASE + 5)
c97ef257 10880 {"mgp32", no_argument, NULL, OPTION_GP32},
1ffcab4b 10881#define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 6)
119d663a 10882 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1ffcab4b 10883#define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
119d663a 10884 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1ffcab4b 10885#define OPTION_FP64 (OPTION_MISC_BASE + 8)
316f5878 10886 {"mfp64", no_argument, NULL, OPTION_FP64},
1ffcab4b 10887#define OPTION_GP64 (OPTION_MISC_BASE + 9)
ae948b86 10888 {"mgp64", no_argument, NULL, OPTION_GP64},
1ffcab4b
CD
10889#define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 10)
10890#define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 11)
4a6a3df4
AO
10891 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10892 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
aa6975fb
ILT
10893#define OPTION_MSHARED (OPTION_MISC_BASE + 12)
10894#define OPTION_MNO_SHARED (OPTION_MISC_BASE + 13)
10895 {"mshared", no_argument, NULL, OPTION_MSHARED},
10896 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
aed1a261
RS
10897#define OPTION_MSYM32 (OPTION_MISC_BASE + 14)
10898#define OPTION_MNO_SYM32 (OPTION_MISC_BASE + 15)
10899 {"msym32", no_argument, NULL, OPTION_MSYM32},
10900 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
f9b4148d
CD
10901
10902 /* ELF-specific options. */
156c2f8b 10903#ifdef OBJ_ELF
aed1a261 10904#define OPTION_ELF_BASE (OPTION_MISC_BASE + 16)
156c2f8b 10905#define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
156c2f8b
NC
10906 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
10907 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
ae948b86 10908#define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
156c2f8b 10909 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
ae948b86 10910#define OPTION_XGOT (OPTION_ELF_BASE + 2)
156c2f8b 10911 {"xgot", no_argument, NULL, OPTION_XGOT},
ae948b86
TS
10912#define OPTION_MABI (OPTION_ELF_BASE + 3)
10913 {"mabi", required_argument, NULL, OPTION_MABI},
10914#define OPTION_32 (OPTION_ELF_BASE + 4)
156c2f8b 10915 {"32", no_argument, NULL, OPTION_32},
ae948b86 10916#define OPTION_N32 (OPTION_ELF_BASE + 5)
e013f690 10917 {"n32", no_argument, NULL, OPTION_N32},
ae948b86 10918#define OPTION_64 (OPTION_ELF_BASE + 6)
156c2f8b 10919 {"64", no_argument, NULL, OPTION_64},
ecb4347a
DJ
10920#define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
10921 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10922#define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
10923 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
dcd410fe
RO
10924#define OPTION_PDR (OPTION_ELF_BASE + 9)
10925 {"mpdr", no_argument, NULL, OPTION_PDR},
10926#define OPTION_NO_PDR (OPTION_ELF_BASE + 10)
10927 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
0a44bf69
RS
10928#define OPTION_MVXWORKS_PIC (OPTION_ELF_BASE + 11)
10929 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
ae948b86 10930#endif /* OBJ_ELF */
f9b4148d 10931
252b5132
RH
10932 {NULL, no_argument, NULL, 0}
10933};
156c2f8b 10934size_t md_longopts_size = sizeof (md_longopts);
252b5132 10935
316f5878
RS
10936/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10937 NEW_VALUE. Warn if another value was already specified. Note:
10938 we have to defer parsing the -march and -mtune arguments in order
10939 to handle 'from-abi' correctly, since the ABI might be specified
10940 in a later argument. */
10941
10942static void
17a2f251 10943mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
10944{
10945 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10946 as_warn (_("A different %s was already specified, is now %s"),
10947 string_ptr == &mips_arch_string ? "-march" : "-mtune",
10948 new_value);
10949
10950 *string_ptr = new_value;
10951}
10952
252b5132 10953int
17a2f251 10954md_parse_option (int c, char *arg)
252b5132
RH
10955{
10956 switch (c)
10957 {
119d663a
NC
10958 case OPTION_CONSTRUCT_FLOATS:
10959 mips_disable_float_construction = 0;
10960 break;
bdaaa2e1 10961
119d663a
NC
10962 case OPTION_NO_CONSTRUCT_FLOATS:
10963 mips_disable_float_construction = 1;
10964 break;
bdaaa2e1 10965
252b5132
RH
10966 case OPTION_TRAP:
10967 mips_trap = 1;
10968 break;
10969
10970 case OPTION_BREAK:
10971 mips_trap = 0;
10972 break;
10973
10974 case OPTION_EB:
10975 target_big_endian = 1;
10976 break;
10977
10978 case OPTION_EL:
10979 target_big_endian = 0;
10980 break;
10981
10982 case 'O':
10983 if (arg && arg[1] == '0')
10984 mips_optimize = 1;
10985 else
10986 mips_optimize = 2;
10987 break;
10988
10989 case 'g':
10990 if (arg == NULL)
10991 mips_debug = 2;
10992 else
10993 mips_debug = atoi (arg);
252b5132
RH
10994 break;
10995
10996 case OPTION_MIPS1:
316f5878 10997 file_mips_isa = ISA_MIPS1;
252b5132
RH
10998 break;
10999
11000 case OPTION_MIPS2:
316f5878 11001 file_mips_isa = ISA_MIPS2;
252b5132
RH
11002 break;
11003
11004 case OPTION_MIPS3:
316f5878 11005 file_mips_isa = ISA_MIPS3;
252b5132
RH
11006 break;
11007
11008 case OPTION_MIPS4:
316f5878 11009 file_mips_isa = ISA_MIPS4;
e7af610e
NC
11010 break;
11011
84ea6cf2 11012 case OPTION_MIPS5:
316f5878 11013 file_mips_isa = ISA_MIPS5;
84ea6cf2
NC
11014 break;
11015
e7af610e 11016 case OPTION_MIPS32:
316f5878 11017 file_mips_isa = ISA_MIPS32;
252b5132
RH
11018 break;
11019
af7ee8bf
CD
11020 case OPTION_MIPS32R2:
11021 file_mips_isa = ISA_MIPS32R2;
11022 break;
11023
5f74bc13
CD
11024 case OPTION_MIPS64R2:
11025 file_mips_isa = ISA_MIPS64R2;
11026 break;
11027
84ea6cf2 11028 case OPTION_MIPS64:
316f5878 11029 file_mips_isa = ISA_MIPS64;
84ea6cf2
NC
11030 break;
11031
ec68c924 11032 case OPTION_MTUNE:
316f5878
RS
11033 mips_set_option_string (&mips_tune_string, arg);
11034 break;
ec68c924 11035
316f5878
RS
11036 case OPTION_MARCH:
11037 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
11038 break;
11039
11040 case OPTION_M4650:
316f5878
RS
11041 mips_set_option_string (&mips_arch_string, "4650");
11042 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
11043 break;
11044
11045 case OPTION_NO_M4650:
11046 break;
11047
11048 case OPTION_M4010:
316f5878
RS
11049 mips_set_option_string (&mips_arch_string, "4010");
11050 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
11051 break;
11052
11053 case OPTION_NO_M4010:
11054 break;
11055
11056 case OPTION_M4100:
316f5878
RS
11057 mips_set_option_string (&mips_arch_string, "4100");
11058 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
11059 break;
11060
11061 case OPTION_NO_M4100:
11062 break;
11063
252b5132 11064 case OPTION_M3900:
316f5878
RS
11065 mips_set_option_string (&mips_arch_string, "3900");
11066 mips_set_option_string (&mips_tune_string, "3900");
252b5132 11067 break;
bdaaa2e1 11068
252b5132
RH
11069 case OPTION_NO_M3900:
11070 break;
11071
deec1734
CD
11072 case OPTION_MDMX:
11073 mips_opts.ase_mdmx = 1;
11074 break;
11075
11076 case OPTION_NO_MDMX:
11077 mips_opts.ase_mdmx = 0;
11078 break;
11079
74cd071d
CF
11080 case OPTION_DSP:
11081 mips_opts.ase_dsp = 1;
11082 break;
11083
11084 case OPTION_NO_DSP:
11085 mips_opts.ase_dsp = 0;
11086 break;
11087
ef2e4d86
CF
11088 case OPTION_MT:
11089 mips_opts.ase_mt = 1;
11090 break;
11091
11092 case OPTION_NO_MT:
11093 mips_opts.ase_mt = 0;
11094 break;
11095
252b5132
RH
11096 case OPTION_MIPS16:
11097 mips_opts.mips16 = 1;
7d10b47d 11098 mips_no_prev_insn ();
252b5132
RH
11099 break;
11100
11101 case OPTION_NO_MIPS16:
11102 mips_opts.mips16 = 0;
7d10b47d 11103 mips_no_prev_insn ();
252b5132
RH
11104 break;
11105
1f25f5d3
CD
11106 case OPTION_MIPS3D:
11107 mips_opts.ase_mips3d = 1;
11108 break;
11109
11110 case OPTION_NO_MIPS3D:
11111 mips_opts.ase_mips3d = 0;
11112 break;
11113
e16bfa71
TS
11114 case OPTION_SMARTMIPS:
11115 mips_opts.ase_smartmips = 1;
11116 break;
11117
11118 case OPTION_NO_SMARTMIPS:
11119 mips_opts.ase_smartmips = 0;
11120 break;
11121
d766e8ec
RS
11122 case OPTION_FIX_VR4120:
11123 mips_fix_vr4120 = 1;
60b63b72
RS
11124 break;
11125
d766e8ec
RS
11126 case OPTION_NO_FIX_VR4120:
11127 mips_fix_vr4120 = 0;
60b63b72
RS
11128 break;
11129
7d8e00cf
RS
11130 case OPTION_FIX_VR4130:
11131 mips_fix_vr4130 = 1;
11132 break;
11133
11134 case OPTION_NO_FIX_VR4130:
11135 mips_fix_vr4130 = 0;
11136 break;
11137
4a6a3df4
AO
11138 case OPTION_RELAX_BRANCH:
11139 mips_relax_branch = 1;
11140 break;
11141
11142 case OPTION_NO_RELAX_BRANCH:
11143 mips_relax_branch = 0;
11144 break;
11145
aa6975fb
ILT
11146 case OPTION_MSHARED:
11147 mips_in_shared = TRUE;
11148 break;
11149
11150 case OPTION_MNO_SHARED:
11151 mips_in_shared = FALSE;
11152 break;
11153
aed1a261
RS
11154 case OPTION_MSYM32:
11155 mips_opts.sym32 = TRUE;
11156 break;
11157
11158 case OPTION_MNO_SYM32:
11159 mips_opts.sym32 = FALSE;
11160 break;
11161
0f074f60 11162#ifdef OBJ_ELF
252b5132
RH
11163 /* When generating ELF code, we permit -KPIC and -call_shared to
11164 select SVR4_PIC, and -non_shared to select no PIC. This is
11165 intended to be compatible with Irix 5. */
11166 case OPTION_CALL_SHARED:
f43abd2b 11167 if (!IS_ELF)
252b5132
RH
11168 {
11169 as_bad (_("-call_shared is supported only for ELF format"));
11170 return 0;
11171 }
11172 mips_pic = SVR4_PIC;
143d77c5 11173 mips_abicalls = TRUE;
252b5132
RH
11174 break;
11175
11176 case OPTION_NON_SHARED:
f43abd2b 11177 if (!IS_ELF)
252b5132
RH
11178 {
11179 as_bad (_("-non_shared is supported only for ELF format"));
11180 return 0;
11181 }
11182 mips_pic = NO_PIC;
143d77c5 11183 mips_abicalls = FALSE;
252b5132
RH
11184 break;
11185
44075ae2
TS
11186 /* The -xgot option tells the assembler to use 32 bit offsets
11187 when accessing the got in SVR4_PIC mode. It is for Irix
252b5132
RH
11188 compatibility. */
11189 case OPTION_XGOT:
11190 mips_big_got = 1;
11191 break;
0f074f60 11192#endif /* OBJ_ELF */
252b5132
RH
11193
11194 case 'G':
6caf9ef4
TS
11195 g_switch_value = atoi (arg);
11196 g_switch_seen = 1;
252b5132
RH
11197 break;
11198
0f074f60 11199#ifdef OBJ_ELF
34ba82a8
TS
11200 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
11201 and -mabi=64. */
252b5132 11202 case OPTION_32:
f43abd2b 11203 if (!IS_ELF)
34ba82a8
TS
11204 {
11205 as_bad (_("-32 is supported for ELF format only"));
11206 return 0;
11207 }
316f5878 11208 mips_abi = O32_ABI;
252b5132
RH
11209 break;
11210
e013f690 11211 case OPTION_N32:
f43abd2b 11212 if (!IS_ELF)
34ba82a8
TS
11213 {
11214 as_bad (_("-n32 is supported for ELF format only"));
11215 return 0;
11216 }
316f5878 11217 mips_abi = N32_ABI;
e013f690 11218 break;
252b5132 11219
e013f690 11220 case OPTION_64:
f43abd2b 11221 if (!IS_ELF)
34ba82a8
TS
11222 {
11223 as_bad (_("-64 is supported for ELF format only"));
11224 return 0;
11225 }
316f5878 11226 mips_abi = N64_ABI;
f43abd2b 11227 if (!support_64bit_objects())
e013f690 11228 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132 11229 break;
ae948b86 11230#endif /* OBJ_ELF */
252b5132 11231
c97ef257 11232 case OPTION_GP32:
a325df1d 11233 file_mips_gp32 = 1;
c97ef257
AH
11234 break;
11235
11236 case OPTION_GP64:
a325df1d 11237 file_mips_gp32 = 0;
c97ef257 11238 break;
252b5132 11239
ca4e0257 11240 case OPTION_FP32:
a325df1d 11241 file_mips_fp32 = 1;
316f5878
RS
11242 break;
11243
11244 case OPTION_FP64:
11245 file_mips_fp32 = 0;
ca4e0257
RS
11246 break;
11247
ae948b86 11248#ifdef OBJ_ELF
252b5132 11249 case OPTION_MABI:
f43abd2b 11250 if (!IS_ELF)
34ba82a8
TS
11251 {
11252 as_bad (_("-mabi is supported for ELF format only"));
11253 return 0;
11254 }
e013f690 11255 if (strcmp (arg, "32") == 0)
316f5878 11256 mips_abi = O32_ABI;
e013f690 11257 else if (strcmp (arg, "o64") == 0)
316f5878 11258 mips_abi = O64_ABI;
e013f690 11259 else if (strcmp (arg, "n32") == 0)
316f5878 11260 mips_abi = N32_ABI;
e013f690
TS
11261 else if (strcmp (arg, "64") == 0)
11262 {
316f5878 11263 mips_abi = N64_ABI;
e013f690
TS
11264 if (! support_64bit_objects())
11265 as_fatal (_("No compiled in support for 64 bit object file "
11266 "format"));
11267 }
11268 else if (strcmp (arg, "eabi") == 0)
316f5878 11269 mips_abi = EABI_ABI;
e013f690 11270 else
da0e507f
TS
11271 {
11272 as_fatal (_("invalid abi -mabi=%s"), arg);
11273 return 0;
11274 }
252b5132 11275 break;
e013f690 11276#endif /* OBJ_ELF */
252b5132 11277
6b76fefe 11278 case OPTION_M7000_HILO_FIX:
b34976b6 11279 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
11280 break;
11281
9ee72ff1 11282 case OPTION_MNO_7000_HILO_FIX:
b34976b6 11283 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
11284 break;
11285
ecb4347a
DJ
11286#ifdef OBJ_ELF
11287 case OPTION_MDEBUG:
b34976b6 11288 mips_flag_mdebug = TRUE;
ecb4347a
DJ
11289 break;
11290
11291 case OPTION_NO_MDEBUG:
b34976b6 11292 mips_flag_mdebug = FALSE;
ecb4347a 11293 break;
dcd410fe
RO
11294
11295 case OPTION_PDR:
11296 mips_flag_pdr = TRUE;
11297 break;
11298
11299 case OPTION_NO_PDR:
11300 mips_flag_pdr = FALSE;
11301 break;
0a44bf69
RS
11302
11303 case OPTION_MVXWORKS_PIC:
11304 mips_pic = VXWORKS_PIC;
11305 break;
ecb4347a
DJ
11306#endif /* OBJ_ELF */
11307
252b5132
RH
11308 default:
11309 return 0;
11310 }
11311
11312 return 1;
11313}
316f5878
RS
11314\f
11315/* Set up globals to generate code for the ISA or processor
11316 described by INFO. */
252b5132 11317
252b5132 11318static void
17a2f251 11319mips_set_architecture (const struct mips_cpu_info *info)
252b5132 11320{
316f5878 11321 if (info != 0)
252b5132 11322 {
fef14a42
TS
11323 file_mips_arch = info->cpu;
11324 mips_opts.arch = info->cpu;
316f5878 11325 mips_opts.isa = info->isa;
252b5132 11326 }
252b5132
RH
11327}
11328
252b5132 11329
316f5878 11330/* Likewise for tuning. */
252b5132 11331
316f5878 11332static void
17a2f251 11333mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
11334{
11335 if (info != 0)
fef14a42 11336 mips_tune = info->cpu;
316f5878 11337}
80cc45a5 11338
34ba82a8 11339
252b5132 11340void
17a2f251 11341mips_after_parse_args (void)
e9670677 11342{
fef14a42
TS
11343 const struct mips_cpu_info *arch_info = 0;
11344 const struct mips_cpu_info *tune_info = 0;
11345
e9670677 11346 /* GP relative stuff not working for PE */
6caf9ef4 11347 if (strncmp (TARGET_OS, "pe", 2) == 0)
e9670677 11348 {
6caf9ef4 11349 if (g_switch_seen && g_switch_value != 0)
e9670677
MR
11350 as_bad (_("-G not supported in this configuration."));
11351 g_switch_value = 0;
11352 }
11353
cac012d6
AO
11354 if (mips_abi == NO_ABI)
11355 mips_abi = MIPS_DEFAULT_ABI;
11356
22923709
RS
11357 /* The following code determines the architecture and register size.
11358 Similar code was added to GCC 3.3 (see override_options() in
11359 config/mips/mips.c). The GAS and GCC code should be kept in sync
11360 as much as possible. */
e9670677 11361
316f5878 11362 if (mips_arch_string != 0)
fef14a42 11363 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 11364
316f5878 11365 if (file_mips_isa != ISA_UNKNOWN)
e9670677 11366 {
316f5878 11367 /* Handle -mipsN. At this point, file_mips_isa contains the
fef14a42 11368 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 11369 the -march selection (if any). */
fef14a42 11370 if (arch_info != 0)
e9670677 11371 {
316f5878
RS
11372 /* -march takes precedence over -mipsN, since it is more descriptive.
11373 There's no harm in specifying both as long as the ISA levels
11374 are the same. */
fef14a42 11375 if (file_mips_isa != arch_info->isa)
316f5878
RS
11376 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
11377 mips_cpu_info_from_isa (file_mips_isa)->name,
fef14a42 11378 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 11379 }
316f5878 11380 else
fef14a42 11381 arch_info = mips_cpu_info_from_isa (file_mips_isa);
e9670677
MR
11382 }
11383
fef14a42
TS
11384 if (arch_info == 0)
11385 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
e9670677 11386
fef14a42 11387 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
316f5878 11388 as_bad ("-march=%s is not compatible with the selected ABI",
fef14a42
TS
11389 arch_info->name);
11390
11391 mips_set_architecture (arch_info);
11392
11393 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
11394 if (mips_tune_string != 0)
11395 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 11396
fef14a42
TS
11397 if (tune_info == 0)
11398 mips_set_tune (arch_info);
11399 else
11400 mips_set_tune (tune_info);
e9670677 11401
316f5878 11402 if (file_mips_gp32 >= 0)
e9670677 11403 {
316f5878
RS
11404 /* The user specified the size of the integer registers. Make sure
11405 it agrees with the ABI and ISA. */
11406 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
11407 as_bad (_("-mgp64 used with a 32-bit processor"));
11408 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
11409 as_bad (_("-mgp32 used with a 64-bit ABI"));
11410 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
11411 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
11412 }
11413 else
11414 {
316f5878
RS
11415 /* Infer the integer register size from the ABI and processor.
11416 Restrict ourselves to 32-bit registers if that's all the
11417 processor has, or if the ABI cannot handle 64-bit registers. */
11418 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
11419 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
e9670677
MR
11420 }
11421
ad3fea08
TS
11422 switch (file_mips_fp32)
11423 {
11424 default:
11425 case -1:
11426 /* No user specified float register size.
11427 ??? GAS treats single-float processors as though they had 64-bit
11428 float registers (although it complains when double-precision
11429 instructions are used). As things stand, saying they have 32-bit
11430 registers would lead to spurious "register must be even" messages.
11431 So here we assume float registers are never smaller than the
11432 integer ones. */
11433 if (file_mips_gp32 == 0)
11434 /* 64-bit integer registers implies 64-bit float registers. */
11435 file_mips_fp32 = 0;
11436 else if ((mips_opts.ase_mips3d > 0 || mips_opts.ase_mdmx > 0)
11437 && ISA_HAS_64BIT_FPRS (mips_opts.isa))
11438 /* -mips3d and -mdmx imply 64-bit float registers, if possible. */
11439 file_mips_fp32 = 0;
11440 else
11441 /* 32-bit float registers. */
11442 file_mips_fp32 = 1;
11443 break;
11444
11445 /* The user specified the size of the float registers. Check if it
11446 agrees with the ABI and ISA. */
11447 case 0:
11448 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
11449 as_bad (_("-mfp64 used with a 32-bit fpu"));
11450 else if (ABI_NEEDS_32BIT_REGS (mips_abi)
11451 && !ISA_HAS_MXHC1 (mips_opts.isa))
11452 as_warn (_("-mfp64 used with a 32-bit ABI"));
11453 break;
11454 case 1:
11455 if (ABI_NEEDS_64BIT_REGS (mips_abi))
11456 as_warn (_("-mfp32 used with a 64-bit ABI"));
11457 break;
11458 }
e9670677 11459
316f5878 11460 /* End of GCC-shared inference code. */
e9670677 11461
17a2f251
TS
11462 /* This flag is set when we have a 64-bit capable CPU but use only
11463 32-bit wide registers. Note that EABI does not use it. */
11464 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
11465 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
11466 || mips_abi == O32_ABI))
316f5878 11467 mips_32bitmode = 1;
e9670677
MR
11468
11469 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
11470 as_bad (_("trap exception not supported at ISA 1"));
11471
e9670677
MR
11472 /* If the selected architecture includes support for ASEs, enable
11473 generation of code for them. */
a4672219 11474 if (mips_opts.mips16 == -1)
fef14a42 11475 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
ffdefa66 11476 if (mips_opts.ase_mips3d == -1)
65263ce3 11477 mips_opts.ase_mips3d = ((arch_info->flags & MIPS_CPU_ASE_MIPS3D)
ad3fea08
TS
11478 && file_mips_fp32 == 0) ? 1 : 0;
11479 if (mips_opts.ase_mips3d && file_mips_fp32 == 1)
11480 as_bad (_("-mfp32 used with -mips3d"));
11481
ffdefa66 11482 if (mips_opts.ase_mdmx == -1)
65263ce3 11483 mips_opts.ase_mdmx = ((arch_info->flags & MIPS_CPU_ASE_MDMX)
ad3fea08
TS
11484 && file_mips_fp32 == 0) ? 1 : 0;
11485 if (mips_opts.ase_mdmx && file_mips_fp32 == 1)
11486 as_bad (_("-mfp32 used with -mdmx"));
11487
11488 if (mips_opts.ase_smartmips == -1)
11489 mips_opts.ase_smartmips = (arch_info->flags & MIPS_CPU_ASE_SMARTMIPS) ? 1 : 0;
11490 if (mips_opts.ase_smartmips && !ISA_SUPPORTS_SMARTMIPS)
11491 as_warn ("%s ISA does not support SmartMIPS",
11492 mips_cpu_info_from_isa (mips_opts.isa)->name);
11493
74cd071d 11494 if (mips_opts.ase_dsp == -1)
ad3fea08
TS
11495 mips_opts.ase_dsp = (arch_info->flags & MIPS_CPU_ASE_DSP) ? 1 : 0;
11496 if (mips_opts.ase_dsp && !ISA_SUPPORTS_DSP_ASE)
11497 as_warn ("%s ISA does not support DSP ASE",
11498 mips_cpu_info_from_isa (mips_opts.isa)->name);
11499
ef2e4d86 11500 if (mips_opts.ase_mt == -1)
ad3fea08
TS
11501 mips_opts.ase_mt = (arch_info->flags & MIPS_CPU_ASE_MT) ? 1 : 0;
11502 if (mips_opts.ase_mt && !ISA_SUPPORTS_MT_ASE)
11503 as_warn ("%s ISA does not support MT ASE",
11504 mips_cpu_info_from_isa (mips_opts.isa)->name);
e9670677 11505
e9670677 11506 file_mips_isa = mips_opts.isa;
a4672219 11507 file_ase_mips16 = mips_opts.mips16;
e9670677
MR
11508 file_ase_mips3d = mips_opts.ase_mips3d;
11509 file_ase_mdmx = mips_opts.ase_mdmx;
e16bfa71 11510 file_ase_smartmips = mips_opts.ase_smartmips;
74cd071d 11511 file_ase_dsp = mips_opts.ase_dsp;
ef2e4d86 11512 file_ase_mt = mips_opts.ase_mt;
e9670677
MR
11513 mips_opts.gp32 = file_mips_gp32;
11514 mips_opts.fp32 = file_mips_fp32;
11515
ecb4347a
DJ
11516 if (mips_flag_mdebug < 0)
11517 {
11518#ifdef OBJ_MAYBE_ECOFF
11519 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
11520 mips_flag_mdebug = 1;
11521 else
11522#endif /* OBJ_MAYBE_ECOFF */
11523 mips_flag_mdebug = 0;
11524 }
e9670677
MR
11525}
11526\f
11527void
17a2f251 11528mips_init_after_args (void)
252b5132
RH
11529{
11530 /* initialize opcodes */
11531 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 11532 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
11533}
11534
11535long
17a2f251 11536md_pcrel_from (fixS *fixP)
252b5132 11537{
a7ebbfdf
TS
11538 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
11539 switch (fixP->fx_r_type)
11540 {
11541 case BFD_RELOC_16_PCREL_S2:
11542 case BFD_RELOC_MIPS_JMP:
11543 /* Return the address of the delay slot. */
11544 return addr + 4;
11545 default:
58ea3d6a 11546 /* We have no relocation type for PC relative MIPS16 instructions. */
64817874
TS
11547 if (fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != now_seg)
11548 as_bad_where (fixP->fx_file, fixP->fx_line,
11549 _("PC relative MIPS16 instruction references a different section"));
a7ebbfdf
TS
11550 return addr;
11551 }
252b5132
RH
11552}
11553
252b5132
RH
11554/* This is called before the symbol table is processed. In order to
11555 work with gcc when using mips-tfile, we must keep all local labels.
11556 However, in other cases, we want to discard them. If we were
11557 called with -g, but we didn't see any debugging information, it may
11558 mean that gcc is smuggling debugging information through to
11559 mips-tfile, in which case we must generate all local labels. */
11560
11561void
17a2f251 11562mips_frob_file_before_adjust (void)
252b5132
RH
11563{
11564#ifndef NO_ECOFF_DEBUGGING
11565 if (ECOFF_DEBUGGING
11566 && mips_debug != 0
11567 && ! ecoff_debugging_seen)
11568 flag_keep_locals = 1;
11569#endif
11570}
11571
3b91255e 11572/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 11573 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
11574 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
11575 relocation operators.
11576
11577 For our purposes, a %lo() expression matches a %got() or %hi()
11578 expression if:
11579
11580 (a) it refers to the same symbol; and
11581 (b) the offset applied in the %lo() expression is no lower than
11582 the offset applied in the %got() or %hi().
11583
11584 (b) allows us to cope with code like:
11585
11586 lui $4,%hi(foo)
11587 lh $4,%lo(foo+2)($4)
11588
11589 ...which is legal on RELA targets, and has a well-defined behaviour
11590 if the user knows that adding 2 to "foo" will not induce a carry to
11591 the high 16 bits.
11592
11593 When several %lo()s match a particular %got() or %hi(), we use the
11594 following rules to distinguish them:
11595
11596 (1) %lo()s with smaller offsets are a better match than %lo()s with
11597 higher offsets.
11598
11599 (2) %lo()s with no matching %got() or %hi() are better than those
11600 that already have a matching %got() or %hi().
11601
11602 (3) later %lo()s are better than earlier %lo()s.
11603
11604 These rules are applied in order.
11605
11606 (1) means, among other things, that %lo()s with identical offsets are
11607 chosen if they exist.
11608
11609 (2) means that we won't associate several high-part relocations with
11610 the same low-part relocation unless there's no alternative. Having
11611 several high parts for the same low part is a GNU extension; this rule
11612 allows careful users to avoid it.
11613
11614 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
11615 with the last high-part relocation being at the front of the list.
11616 It therefore makes sense to choose the last matching low-part
11617 relocation, all other things being equal. It's also easier
11618 to code that way. */
252b5132
RH
11619
11620void
17a2f251 11621mips_frob_file (void)
252b5132
RH
11622{
11623 struct mips_hi_fixup *l;
11624
11625 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
11626 {
11627 segment_info_type *seginfo;
3b91255e
RS
11628 bfd_boolean matched_lo_p;
11629 fixS **hi_pos, **lo_pos, **pos;
252b5132 11630
5919d012 11631 assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 11632
5919d012
RS
11633 /* If a GOT16 relocation turns out to be against a global symbol,
11634 there isn't supposed to be a matching LO. */
11635 if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
11636 && !pic_need_relax (l->fixp->fx_addsy, l->seg))
11637 continue;
11638
11639 /* Check quickly whether the next fixup happens to be a matching %lo. */
11640 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
11641 continue;
11642
252b5132 11643 seginfo = seg_info (l->seg);
252b5132 11644
3b91255e
RS
11645 /* Set HI_POS to the position of this relocation in the chain.
11646 Set LO_POS to the position of the chosen low-part relocation.
11647 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
11648 relocation that matches an immediately-preceding high-part
11649 relocation. */
11650 hi_pos = NULL;
11651 lo_pos = NULL;
11652 matched_lo_p = FALSE;
11653 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
11654 {
11655 if (*pos == l->fixp)
11656 hi_pos = pos;
11657
704803a9
MR
11658 if (((*pos)->fx_r_type == BFD_RELOC_LO16
11659 || (*pos)->fx_r_type == BFD_RELOC_MIPS16_LO16)
3b91255e
RS
11660 && (*pos)->fx_addsy == l->fixp->fx_addsy
11661 && (*pos)->fx_offset >= l->fixp->fx_offset
11662 && (lo_pos == NULL
11663 || (*pos)->fx_offset < (*lo_pos)->fx_offset
11664 || (!matched_lo_p
11665 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
11666 lo_pos = pos;
11667
11668 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
11669 && fixup_has_matching_lo_p (*pos));
11670 }
11671
11672 /* If we found a match, remove the high-part relocation from its
11673 current position and insert it before the low-part relocation.
11674 Make the offsets match so that fixup_has_matching_lo_p()
11675 will return true.
11676
11677 We don't warn about unmatched high-part relocations since some
11678 versions of gcc have been known to emit dead "lui ...%hi(...)"
11679 instructions. */
11680 if (lo_pos != NULL)
11681 {
11682 l->fixp->fx_offset = (*lo_pos)->fx_offset;
11683 if (l->fixp->fx_next != *lo_pos)
252b5132 11684 {
3b91255e
RS
11685 *hi_pos = l->fixp->fx_next;
11686 l->fixp->fx_next = *lo_pos;
11687 *lo_pos = l->fixp;
252b5132 11688 }
252b5132
RH
11689 }
11690 }
11691}
11692
3e722fb5 11693/* We may have combined relocations without symbols in the N32/N64 ABI.
f6688943 11694 We have to prevent gas from dropping them. */
252b5132 11695
252b5132 11696int
17a2f251 11697mips_force_relocation (fixS *fixp)
252b5132 11698{
ae6063d4 11699 if (generic_force_reloc (fixp))
252b5132
RH
11700 return 1;
11701
f6688943
TS
11702 if (HAVE_NEWABI
11703 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
11704 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
11705 || fixp->fx_r_type == BFD_RELOC_HI16_S
11706 || fixp->fx_r_type == BFD_RELOC_LO16))
11707 return 1;
11708
3e722fb5 11709 return 0;
252b5132
RH
11710}
11711
11712/* Apply a fixup to the object file. */
11713
94f592af 11714void
55cf6793 11715md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 11716{
874e8986 11717 bfd_byte *buf;
98aa84af 11718 long insn;
a7ebbfdf 11719 reloc_howto_type *howto;
252b5132 11720
a7ebbfdf
TS
11721 /* We ignore generic BFD relocations we don't know about. */
11722 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11723 if (! howto)
11724 return;
65551fa4 11725
252b5132
RH
11726 assert (fixP->fx_size == 4
11727 || fixP->fx_r_type == BFD_RELOC_16
11728 || fixP->fx_r_type == BFD_RELOC_64
f6688943
TS
11729 || fixP->fx_r_type == BFD_RELOC_CTOR
11730 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
252b5132 11731 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
a7ebbfdf 11732 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
252b5132 11733
a7ebbfdf 11734 buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
252b5132 11735
3994f87e 11736 assert (!fixP->fx_pcrel || fixP->fx_r_type == BFD_RELOC_16_PCREL_S2);
b1dca8ee
RS
11737
11738 /* Don't treat parts of a composite relocation as done. There are two
11739 reasons for this:
11740
11741 (1) The second and third parts will be against 0 (RSS_UNDEF) but
11742 should nevertheless be emitted if the first part is.
11743
11744 (2) In normal usage, composite relocations are never assembly-time
11745 constants. The easiest way of dealing with the pathological
11746 exceptions is to generate a relocation against STN_UNDEF and
11747 leave everything up to the linker. */
3994f87e 11748 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
252b5132
RH
11749 fixP->fx_done = 1;
11750
11751 switch (fixP->fx_r_type)
11752 {
3f98094e
DJ
11753 case BFD_RELOC_MIPS_TLS_GD:
11754 case BFD_RELOC_MIPS_TLS_LDM:
11755 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
11756 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
11757 case BFD_RELOC_MIPS_TLS_GOTTPREL:
11758 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
11759 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
11760 S_SET_THREAD_LOCAL (fixP->fx_addsy);
11761 /* fall through */
11762
252b5132 11763 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
11764 case BFD_RELOC_MIPS_SHIFT5:
11765 case BFD_RELOC_MIPS_SHIFT6:
11766 case BFD_RELOC_MIPS_GOT_DISP:
11767 case BFD_RELOC_MIPS_GOT_PAGE:
11768 case BFD_RELOC_MIPS_GOT_OFST:
11769 case BFD_RELOC_MIPS_SUB:
11770 case BFD_RELOC_MIPS_INSERT_A:
11771 case BFD_RELOC_MIPS_INSERT_B:
11772 case BFD_RELOC_MIPS_DELETE:
11773 case BFD_RELOC_MIPS_HIGHEST:
11774 case BFD_RELOC_MIPS_HIGHER:
11775 case BFD_RELOC_MIPS_SCN_DISP:
11776 case BFD_RELOC_MIPS_REL16:
11777 case BFD_RELOC_MIPS_RELGOT:
11778 case BFD_RELOC_MIPS_JALR:
252b5132
RH
11779 case BFD_RELOC_HI16:
11780 case BFD_RELOC_HI16_S:
cdf6fd85 11781 case BFD_RELOC_GPREL16:
252b5132
RH
11782 case BFD_RELOC_MIPS_LITERAL:
11783 case BFD_RELOC_MIPS_CALL16:
11784 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 11785 case BFD_RELOC_GPREL32:
252b5132
RH
11786 case BFD_RELOC_MIPS_GOT_HI16:
11787 case BFD_RELOC_MIPS_GOT_LO16:
11788 case BFD_RELOC_MIPS_CALL_HI16:
11789 case BFD_RELOC_MIPS_CALL_LO16:
11790 case BFD_RELOC_MIPS16_GPREL:
d6f16593
MR
11791 case BFD_RELOC_MIPS16_HI16:
11792 case BFD_RELOC_MIPS16_HI16_S:
252b5132 11793 case BFD_RELOC_MIPS16_JMP:
54f4ddb3 11794 /* Nothing needed to do. The value comes from the reloc entry. */
252b5132
RH
11795 break;
11796
252b5132
RH
11797 case BFD_RELOC_64:
11798 /* This is handled like BFD_RELOC_32, but we output a sign
11799 extended value if we are only 32 bits. */
3e722fb5 11800 if (fixP->fx_done)
252b5132
RH
11801 {
11802 if (8 <= sizeof (valueT))
2132e3a3 11803 md_number_to_chars ((char *) buf, *valP, 8);
252b5132
RH
11804 else
11805 {
a7ebbfdf 11806 valueT hiv;
252b5132 11807
a7ebbfdf 11808 if ((*valP & 0x80000000) != 0)
252b5132
RH
11809 hiv = 0xffffffff;
11810 else
11811 hiv = 0;
b215186b 11812 md_number_to_chars ((char *)(buf + (target_big_endian ? 4 : 0)),
a7ebbfdf 11813 *valP, 4);
b215186b 11814 md_number_to_chars ((char *)(buf + (target_big_endian ? 0 : 4)),
a7ebbfdf 11815 hiv, 4);
252b5132
RH
11816 }
11817 }
11818 break;
11819
056350c6 11820 case BFD_RELOC_RVA:
252b5132 11821 case BFD_RELOC_32:
252b5132
RH
11822 case BFD_RELOC_16:
11823 /* If we are deleting this reloc entry, we must fill in the
54f4ddb3
TS
11824 value now. This can happen if we have a .word which is not
11825 resolved when it appears but is later defined. */
252b5132 11826 if (fixP->fx_done)
54f4ddb3 11827 md_number_to_chars ((char *) buf, *valP, fixP->fx_size);
252b5132
RH
11828 break;
11829
11830 case BFD_RELOC_LO16:
d6f16593 11831 case BFD_RELOC_MIPS16_LO16:
3e722fb5
CD
11832 /* FIXME: Now that embedded-PIC is gone, some of this code/comment
11833 may be safe to remove, but if so it's not obvious. */
252b5132
RH
11834 /* When handling an embedded PIC switch statement, we can wind
11835 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
11836 if (fixP->fx_done)
11837 {
a7ebbfdf 11838 if (*valP + 0x8000 > 0xffff)
252b5132
RH
11839 as_bad_where (fixP->fx_file, fixP->fx_line,
11840 _("relocation overflow"));
252b5132
RH
11841 if (target_big_endian)
11842 buf += 2;
2132e3a3 11843 md_number_to_chars ((char *) buf, *valP, 2);
252b5132
RH
11844 }
11845 break;
11846
11847 case BFD_RELOC_16_PCREL_S2:
a7ebbfdf 11848 if ((*valP & 0x3) != 0)
cb56d3d3 11849 as_bad_where (fixP->fx_file, fixP->fx_line,
bad36eac 11850 _("Branch to misaligned address (%lx)"), (long) *valP);
cb56d3d3 11851
54f4ddb3
TS
11852 /* We need to save the bits in the instruction since fixup_segment()
11853 might be deleting the relocation entry (i.e., a branch within
11854 the current segment). */
a7ebbfdf 11855 if (! fixP->fx_done)
bb2d6cd7 11856 break;
252b5132 11857
54f4ddb3 11858 /* Update old instruction data. */
252b5132
RH
11859 if (target_big_endian)
11860 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11861 else
11862 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11863
a7ebbfdf
TS
11864 if (*valP + 0x20000 <= 0x3ffff)
11865 {
11866 insn |= (*valP >> 2) & 0xffff;
2132e3a3 11867 md_number_to_chars ((char *) buf, insn, 4);
a7ebbfdf
TS
11868 }
11869 else if (mips_pic == NO_PIC
11870 && fixP->fx_done
11871 && fixP->fx_frag->fr_address >= text_section->vma
11872 && (fixP->fx_frag->fr_address
587aac4e 11873 < text_section->vma + bfd_get_section_size (text_section))
a7ebbfdf
TS
11874 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
11875 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
11876 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
11877 {
11878 /* The branch offset is too large. If this is an
11879 unconditional branch, and we are not generating PIC code,
11880 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
11881 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11882 insn = 0x0c000000; /* jal */
252b5132 11883 else
a7ebbfdf
TS
11884 insn = 0x08000000; /* j */
11885 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11886 fixP->fx_done = 0;
11887 fixP->fx_addsy = section_symbol (text_section);
11888 *valP += md_pcrel_from (fixP);
2132e3a3 11889 md_number_to_chars ((char *) buf, insn, 4);
a7ebbfdf
TS
11890 }
11891 else
11892 {
11893 /* If we got here, we have branch-relaxation disabled,
11894 and there's nothing we can do to fix this instruction
11895 without turning it into a longer sequence. */
11896 as_bad_where (fixP->fx_file, fixP->fx_line,
11897 _("Branch out of range"));
252b5132 11898 }
252b5132
RH
11899 break;
11900
11901 case BFD_RELOC_VTABLE_INHERIT:
11902 fixP->fx_done = 0;
11903 if (fixP->fx_addsy
11904 && !S_IS_DEFINED (fixP->fx_addsy)
11905 && !S_IS_WEAK (fixP->fx_addsy))
11906 S_SET_WEAK (fixP->fx_addsy);
11907 break;
11908
11909 case BFD_RELOC_VTABLE_ENTRY:
11910 fixP->fx_done = 0;
11911 break;
11912
11913 default:
11914 internalError ();
11915 }
a7ebbfdf
TS
11916
11917 /* Remember value for tc_gen_reloc. */
11918 fixP->fx_addnumber = *valP;
252b5132
RH
11919}
11920
252b5132 11921static symbolS *
17a2f251 11922get_symbol (void)
252b5132
RH
11923{
11924 int c;
11925 char *name;
11926 symbolS *p;
11927
11928 name = input_line_pointer;
11929 c = get_symbol_end ();
11930 p = (symbolS *) symbol_find_or_make (name);
11931 *input_line_pointer = c;
11932 return p;
11933}
11934
11935/* Align the current frag to a given power of two. The MIPS assembler
11936 also automatically adjusts any preceding label. */
11937
11938static void
17a2f251 11939mips_align (int to, int fill, symbolS *label)
252b5132 11940{
7d10b47d 11941 mips_emit_delays ();
252b5132
RH
11942 frag_align (to, fill, 0);
11943 record_alignment (now_seg, to);
11944 if (label != NULL)
11945 {
11946 assert (S_GET_SEGMENT (label) == now_seg);
49309057 11947 symbol_set_frag (label, frag_now);
252b5132
RH
11948 S_SET_VALUE (label, (valueT) frag_now_fix ());
11949 }
11950}
11951
11952/* Align to a given power of two. .align 0 turns off the automatic
11953 alignment used by the data creating pseudo-ops. */
11954
11955static void
17a2f251 11956s_align (int x ATTRIBUTE_UNUSED)
252b5132 11957{
3994f87e
TS
11958 int temp;
11959 long temp_fill;
252b5132
RH
11960 long max_alignment = 15;
11961
54f4ddb3 11962 /* o Note that the assembler pulls down any immediately preceding label
252b5132 11963 to the aligned address.
54f4ddb3 11964 o It's not documented but auto alignment is reinstated by
252b5132 11965 a .align pseudo instruction.
54f4ddb3 11966 o Note also that after auto alignment is turned off the mips assembler
252b5132 11967 issues an error on attempt to assemble an improperly aligned data item.
54f4ddb3 11968 We don't. */
252b5132
RH
11969
11970 temp = get_absolute_expression ();
11971 if (temp > max_alignment)
11972 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11973 else if (temp < 0)
11974 {
11975 as_warn (_("Alignment negative: 0 assumed."));
11976 temp = 0;
11977 }
11978 if (*input_line_pointer == ',')
11979 {
f9419b05 11980 ++input_line_pointer;
252b5132
RH
11981 temp_fill = get_absolute_expression ();
11982 }
11983 else
11984 temp_fill = 0;
11985 if (temp)
11986 {
a8dbcb85
TS
11987 segment_info_type *si = seg_info (now_seg);
11988 struct insn_label_list *l = si->label_list;
54f4ddb3 11989 /* Auto alignment should be switched on by next section change. */
252b5132 11990 auto_align = 1;
a8dbcb85 11991 mips_align (temp, (int) temp_fill, l != NULL ? l->label : NULL);
252b5132
RH
11992 }
11993 else
11994 {
11995 auto_align = 0;
11996 }
11997
11998 demand_empty_rest_of_line ();
11999}
12000
252b5132 12001static void
17a2f251 12002s_change_sec (int sec)
252b5132
RH
12003{
12004 segT seg;
12005
252b5132
RH
12006#ifdef OBJ_ELF
12007 /* The ELF backend needs to know that we are changing sections, so
12008 that .previous works correctly. We could do something like check
b6ff326e 12009 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
12010 as it would not be appropriate to use it in the section changing
12011 functions in read.c, since obj-elf.c intercepts those. FIXME:
12012 This should be cleaner, somehow. */
f43abd2b
TS
12013 if (IS_ELF)
12014 obj_elf_section_change_hook ();
252b5132
RH
12015#endif
12016
7d10b47d 12017 mips_emit_delays ();
252b5132
RH
12018 switch (sec)
12019 {
12020 case 't':
12021 s_text (0);
12022 break;
12023 case 'd':
12024 s_data (0);
12025 break;
12026 case 'b':
12027 subseg_set (bss_section, (subsegT) get_absolute_expression ());
12028 demand_empty_rest_of_line ();
12029 break;
12030
12031 case 'r':
4d0d148d
TS
12032 seg = subseg_new (RDATA_SECTION_NAME,
12033 (subsegT) get_absolute_expression ());
f43abd2b 12034 if (IS_ELF)
252b5132 12035 {
4d0d148d
TS
12036 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
12037 | SEC_READONLY | SEC_RELOC
12038 | SEC_DATA));
12039 if (strcmp (TARGET_OS, "elf") != 0)
12040 record_alignment (seg, 4);
252b5132 12041 }
4d0d148d 12042 demand_empty_rest_of_line ();
252b5132
RH
12043 break;
12044
12045 case 's':
4d0d148d 12046 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
f43abd2b 12047 if (IS_ELF)
252b5132 12048 {
4d0d148d
TS
12049 bfd_set_section_flags (stdoutput, seg,
12050 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
12051 if (strcmp (TARGET_OS, "elf") != 0)
12052 record_alignment (seg, 4);
252b5132 12053 }
4d0d148d
TS
12054 demand_empty_rest_of_line ();
12055 break;
252b5132
RH
12056 }
12057
12058 auto_align = 1;
12059}
b34976b6 12060
cca86cc8 12061void
17a2f251 12062s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 12063{
7ed4a06a 12064#ifdef OBJ_ELF
cca86cc8
SC
12065 char *section_name;
12066 char c;
684022ea 12067 char next_c = 0;
cca86cc8
SC
12068 int section_type;
12069 int section_flag;
12070 int section_entry_size;
12071 int section_alignment;
b34976b6 12072
f43abd2b 12073 if (!IS_ELF)
7ed4a06a
TS
12074 return;
12075
cca86cc8
SC
12076 section_name = input_line_pointer;
12077 c = get_symbol_end ();
a816d1ed
AO
12078 if (c)
12079 next_c = *(input_line_pointer + 1);
cca86cc8 12080
4cf0dd0d
TS
12081 /* Do we have .section Name<,"flags">? */
12082 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 12083 {
4cf0dd0d
TS
12084 /* just after name is now '\0'. */
12085 *input_line_pointer = c;
cca86cc8
SC
12086 input_line_pointer = section_name;
12087 obj_elf_section (ignore);
12088 return;
12089 }
12090 input_line_pointer++;
12091
12092 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
12093 if (c == ',')
12094 section_type = get_absolute_expression ();
12095 else
12096 section_type = 0;
12097 if (*input_line_pointer++ == ',')
12098 section_flag = get_absolute_expression ();
12099 else
12100 section_flag = 0;
12101 if (*input_line_pointer++ == ',')
12102 section_entry_size = get_absolute_expression ();
12103 else
12104 section_entry_size = 0;
12105 if (*input_line_pointer++ == ',')
12106 section_alignment = get_absolute_expression ();
12107 else
12108 section_alignment = 0;
12109
a816d1ed
AO
12110 section_name = xstrdup (section_name);
12111
8ab8a5c8
RS
12112 /* When using the generic form of .section (as implemented by obj-elf.c),
12113 there's no way to set the section type to SHT_MIPS_DWARF. Users have
12114 traditionally had to fall back on the more common @progbits instead.
12115
12116 There's nothing really harmful in this, since bfd will correct
12117 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
708587a4 12118 means that, for backwards compatibility, the special_section entries
8ab8a5c8
RS
12119 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
12120
12121 Even so, we shouldn't force users of the MIPS .section syntax to
12122 incorrectly label the sections as SHT_PROGBITS. The best compromise
12123 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
12124 generic type-checking code. */
12125 if (section_type == SHT_MIPS_DWARF)
12126 section_type = SHT_PROGBITS;
12127
cca86cc8
SC
12128 obj_elf_change_section (section_name, section_type, section_flag,
12129 section_entry_size, 0, 0, 0);
a816d1ed
AO
12130
12131 if (now_seg->name != section_name)
12132 free (section_name);
7ed4a06a 12133#endif /* OBJ_ELF */
cca86cc8 12134}
252b5132
RH
12135
12136void
17a2f251 12137mips_enable_auto_align (void)
252b5132
RH
12138{
12139 auto_align = 1;
12140}
12141
12142static void
17a2f251 12143s_cons (int log_size)
252b5132 12144{
a8dbcb85
TS
12145 segment_info_type *si = seg_info (now_seg);
12146 struct insn_label_list *l = si->label_list;
252b5132
RH
12147 symbolS *label;
12148
a8dbcb85 12149 label = l != NULL ? l->label : NULL;
7d10b47d 12150 mips_emit_delays ();
252b5132
RH
12151 if (log_size > 0 && auto_align)
12152 mips_align (log_size, 0, label);
12153 mips_clear_insn_labels ();
12154 cons (1 << log_size);
12155}
12156
12157static void
17a2f251 12158s_float_cons (int type)
252b5132 12159{
a8dbcb85
TS
12160 segment_info_type *si = seg_info (now_seg);
12161 struct insn_label_list *l = si->label_list;
252b5132
RH
12162 symbolS *label;
12163
a8dbcb85 12164 label = l != NULL ? l->label : NULL;
252b5132 12165
7d10b47d 12166 mips_emit_delays ();
252b5132
RH
12167
12168 if (auto_align)
49309057
ILT
12169 {
12170 if (type == 'd')
12171 mips_align (3, 0, label);
12172 else
12173 mips_align (2, 0, label);
12174 }
252b5132
RH
12175
12176 mips_clear_insn_labels ();
12177
12178 float_cons (type);
12179}
12180
12181/* Handle .globl. We need to override it because on Irix 5 you are
12182 permitted to say
12183 .globl foo .text
12184 where foo is an undefined symbol, to mean that foo should be
12185 considered to be the address of a function. */
12186
12187static void
17a2f251 12188s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
12189{
12190 char *name;
12191 int c;
12192 symbolS *symbolP;
12193 flagword flag;
12194
8a06b769 12195 do
252b5132 12196 {
8a06b769 12197 name = input_line_pointer;
252b5132 12198 c = get_symbol_end ();
8a06b769
TS
12199 symbolP = symbol_find_or_make (name);
12200 S_SET_EXTERNAL (symbolP);
12201
252b5132 12202 *input_line_pointer = c;
8a06b769 12203 SKIP_WHITESPACE ();
252b5132 12204
8a06b769
TS
12205 /* On Irix 5, every global symbol that is not explicitly labelled as
12206 being a function is apparently labelled as being an object. */
12207 flag = BSF_OBJECT;
252b5132 12208
8a06b769
TS
12209 if (!is_end_of_line[(unsigned char) *input_line_pointer]
12210 && (*input_line_pointer != ','))
12211 {
12212 char *secname;
12213 asection *sec;
12214
12215 secname = input_line_pointer;
12216 c = get_symbol_end ();
12217 sec = bfd_get_section_by_name (stdoutput, secname);
12218 if (sec == NULL)
12219 as_bad (_("%s: no such section"), secname);
12220 *input_line_pointer = c;
12221
12222 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
12223 flag = BSF_FUNCTION;
12224 }
12225
12226 symbol_get_bfdsym (symbolP)->flags |= flag;
12227
12228 c = *input_line_pointer;
12229 if (c == ',')
12230 {
12231 input_line_pointer++;
12232 SKIP_WHITESPACE ();
12233 if (is_end_of_line[(unsigned char) *input_line_pointer])
12234 c = '\n';
12235 }
12236 }
12237 while (c == ',');
252b5132 12238
252b5132
RH
12239 demand_empty_rest_of_line ();
12240}
12241
12242static void
17a2f251 12243s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
12244{
12245 char *opt;
12246 char c;
12247
12248 opt = input_line_pointer;
12249 c = get_symbol_end ();
12250
12251 if (*opt == 'O')
12252 {
12253 /* FIXME: What does this mean? */
12254 }
12255 else if (strncmp (opt, "pic", 3) == 0)
12256 {
12257 int i;
12258
12259 i = atoi (opt + 3);
12260 if (i == 0)
12261 mips_pic = NO_PIC;
12262 else if (i == 2)
143d77c5 12263 {
252b5132 12264 mips_pic = SVR4_PIC;
143d77c5
EC
12265 mips_abicalls = TRUE;
12266 }
252b5132
RH
12267 else
12268 as_bad (_(".option pic%d not supported"), i);
12269
4d0d148d 12270 if (mips_pic == SVR4_PIC)
252b5132
RH
12271 {
12272 if (g_switch_seen && g_switch_value != 0)
12273 as_warn (_("-G may not be used with SVR4 PIC code"));
12274 g_switch_value = 0;
12275 bfd_set_gp_size (stdoutput, 0);
12276 }
12277 }
12278 else
12279 as_warn (_("Unrecognized option \"%s\""), opt);
12280
12281 *input_line_pointer = c;
12282 demand_empty_rest_of_line ();
12283}
12284
12285/* This structure is used to hold a stack of .set values. */
12286
e972090a
NC
12287struct mips_option_stack
12288{
252b5132
RH
12289 struct mips_option_stack *next;
12290 struct mips_set_options options;
12291};
12292
12293static struct mips_option_stack *mips_opts_stack;
12294
12295/* Handle the .set pseudo-op. */
12296
12297static void
17a2f251 12298s_mipsset (int x ATTRIBUTE_UNUSED)
252b5132
RH
12299{
12300 char *name = input_line_pointer, ch;
12301
12302 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 12303 ++input_line_pointer;
252b5132
RH
12304 ch = *input_line_pointer;
12305 *input_line_pointer = '\0';
12306
12307 if (strcmp (name, "reorder") == 0)
12308 {
7d10b47d
RS
12309 if (mips_opts.noreorder)
12310 end_noreorder ();
252b5132
RH
12311 }
12312 else if (strcmp (name, "noreorder") == 0)
12313 {
7d10b47d
RS
12314 if (!mips_opts.noreorder)
12315 start_noreorder ();
252b5132
RH
12316 }
12317 else if (strcmp (name, "at") == 0)
12318 {
12319 mips_opts.noat = 0;
12320 }
12321 else if (strcmp (name, "noat") == 0)
12322 {
12323 mips_opts.noat = 1;
12324 }
12325 else if (strcmp (name, "macro") == 0)
12326 {
12327 mips_opts.warn_about_macros = 0;
12328 }
12329 else if (strcmp (name, "nomacro") == 0)
12330 {
12331 if (mips_opts.noreorder == 0)
12332 as_bad (_("`noreorder' must be set before `nomacro'"));
12333 mips_opts.warn_about_macros = 1;
12334 }
12335 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
12336 {
12337 mips_opts.nomove = 0;
12338 }
12339 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
12340 {
12341 mips_opts.nomove = 1;
12342 }
12343 else if (strcmp (name, "bopt") == 0)
12344 {
12345 mips_opts.nobopt = 0;
12346 }
12347 else if (strcmp (name, "nobopt") == 0)
12348 {
12349 mips_opts.nobopt = 1;
12350 }
ad3fea08
TS
12351 else if (strcmp (name, "gp=default") == 0)
12352 mips_opts.gp32 = file_mips_gp32;
12353 else if (strcmp (name, "gp=32") == 0)
12354 mips_opts.gp32 = 1;
12355 else if (strcmp (name, "gp=64") == 0)
12356 {
12357 if (!ISA_HAS_64BIT_REGS (mips_opts.isa))
12358 as_warn ("%s isa does not support 64-bit registers",
12359 mips_cpu_info_from_isa (mips_opts.isa)->name);
12360 mips_opts.gp32 = 0;
12361 }
12362 else if (strcmp (name, "fp=default") == 0)
12363 mips_opts.fp32 = file_mips_fp32;
12364 else if (strcmp (name, "fp=32") == 0)
12365 mips_opts.fp32 = 1;
12366 else if (strcmp (name, "fp=64") == 0)
12367 {
12368 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
12369 as_warn ("%s isa does not support 64-bit floating point registers",
12370 mips_cpu_info_from_isa (mips_opts.isa)->name);
12371 mips_opts.fp32 = 0;
12372 }
252b5132
RH
12373 else if (strcmp (name, "mips16") == 0
12374 || strcmp (name, "MIPS-16") == 0)
12375 mips_opts.mips16 = 1;
12376 else if (strcmp (name, "nomips16") == 0
12377 || strcmp (name, "noMIPS-16") == 0)
12378 mips_opts.mips16 = 0;
e16bfa71
TS
12379 else if (strcmp (name, "smartmips") == 0)
12380 {
ad3fea08 12381 if (!ISA_SUPPORTS_SMARTMIPS)
e16bfa71
TS
12382 as_warn ("%s ISA does not support SmartMIPS ASE",
12383 mips_cpu_info_from_isa (mips_opts.isa)->name);
12384 mips_opts.ase_smartmips = 1;
12385 }
12386 else if (strcmp (name, "nosmartmips") == 0)
12387 mips_opts.ase_smartmips = 0;
1f25f5d3
CD
12388 else if (strcmp (name, "mips3d") == 0)
12389 mips_opts.ase_mips3d = 1;
12390 else if (strcmp (name, "nomips3d") == 0)
12391 mips_opts.ase_mips3d = 0;
a4672219
TS
12392 else if (strcmp (name, "mdmx") == 0)
12393 mips_opts.ase_mdmx = 1;
12394 else if (strcmp (name, "nomdmx") == 0)
12395 mips_opts.ase_mdmx = 0;
74cd071d 12396 else if (strcmp (name, "dsp") == 0)
ad3fea08
TS
12397 {
12398 if (!ISA_SUPPORTS_DSP_ASE)
12399 as_warn ("%s ISA does not support DSP ASE",
12400 mips_cpu_info_from_isa (mips_opts.isa)->name);
12401 mips_opts.ase_dsp = 1;
12402 }
74cd071d
CF
12403 else if (strcmp (name, "nodsp") == 0)
12404 mips_opts.ase_dsp = 0;
ef2e4d86 12405 else if (strcmp (name, "mt") == 0)
ad3fea08
TS
12406 {
12407 if (!ISA_SUPPORTS_MT_ASE)
12408 as_warn ("%s ISA does not support MT ASE",
12409 mips_cpu_info_from_isa (mips_opts.isa)->name);
12410 mips_opts.ase_mt = 1;
12411 }
ef2e4d86
CF
12412 else if (strcmp (name, "nomt") == 0)
12413 mips_opts.ase_mt = 0;
1a2c1fad 12414 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 12415 {
af7ee8bf 12416 int reset = 0;
252b5132 12417
1a2c1fad
CD
12418 /* Permit the user to change the ISA and architecture on the fly.
12419 Needless to say, misuse can cause serious problems. */
81a21e38 12420 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
af7ee8bf
CD
12421 {
12422 reset = 1;
12423 mips_opts.isa = file_mips_isa;
1a2c1fad 12424 mips_opts.arch = file_mips_arch;
1a2c1fad
CD
12425 }
12426 else if (strncmp (name, "arch=", 5) == 0)
12427 {
12428 const struct mips_cpu_info *p;
12429
12430 p = mips_parse_cpu("internal use", name + 5);
12431 if (!p)
12432 as_bad (_("unknown architecture %s"), name + 5);
12433 else
12434 {
12435 mips_opts.arch = p->cpu;
12436 mips_opts.isa = p->isa;
12437 }
12438 }
81a21e38
TS
12439 else if (strncmp (name, "mips", 4) == 0)
12440 {
12441 const struct mips_cpu_info *p;
12442
12443 p = mips_parse_cpu("internal use", name);
12444 if (!p)
12445 as_bad (_("unknown ISA level %s"), name + 4);
12446 else
12447 {
12448 mips_opts.arch = p->cpu;
12449 mips_opts.isa = p->isa;
12450 }
12451 }
af7ee8bf 12452 else
81a21e38 12453 as_bad (_("unknown ISA or architecture %s"), name);
af7ee8bf
CD
12454
12455 switch (mips_opts.isa)
98d3f06f
KH
12456 {
12457 case 0:
98d3f06f 12458 break;
af7ee8bf
CD
12459 case ISA_MIPS1:
12460 case ISA_MIPS2:
12461 case ISA_MIPS32:
12462 case ISA_MIPS32R2:
98d3f06f
KH
12463 mips_opts.gp32 = 1;
12464 mips_opts.fp32 = 1;
12465 break;
af7ee8bf
CD
12466 case ISA_MIPS3:
12467 case ISA_MIPS4:
12468 case ISA_MIPS5:
12469 case ISA_MIPS64:
5f74bc13 12470 case ISA_MIPS64R2:
98d3f06f
KH
12471 mips_opts.gp32 = 0;
12472 mips_opts.fp32 = 0;
12473 break;
12474 default:
12475 as_bad (_("unknown ISA level %s"), name + 4);
12476 break;
12477 }
af7ee8bf 12478 if (reset)
98d3f06f 12479 {
af7ee8bf
CD
12480 mips_opts.gp32 = file_mips_gp32;
12481 mips_opts.fp32 = file_mips_fp32;
98d3f06f 12482 }
252b5132
RH
12483 }
12484 else if (strcmp (name, "autoextend") == 0)
12485 mips_opts.noautoextend = 0;
12486 else if (strcmp (name, "noautoextend") == 0)
12487 mips_opts.noautoextend = 1;
12488 else if (strcmp (name, "push") == 0)
12489 {
12490 struct mips_option_stack *s;
12491
12492 s = (struct mips_option_stack *) xmalloc (sizeof *s);
12493 s->next = mips_opts_stack;
12494 s->options = mips_opts;
12495 mips_opts_stack = s;
12496 }
12497 else if (strcmp (name, "pop") == 0)
12498 {
12499 struct mips_option_stack *s;
12500
12501 s = mips_opts_stack;
12502 if (s == NULL)
12503 as_bad (_(".set pop with no .set push"));
12504 else
12505 {
12506 /* If we're changing the reorder mode we need to handle
12507 delay slots correctly. */
12508 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 12509 start_noreorder ();
252b5132 12510 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 12511 end_noreorder ();
252b5132
RH
12512
12513 mips_opts = s->options;
12514 mips_opts_stack = s->next;
12515 free (s);
12516 }
12517 }
aed1a261
RS
12518 else if (strcmp (name, "sym32") == 0)
12519 mips_opts.sym32 = TRUE;
12520 else if (strcmp (name, "nosym32") == 0)
12521 mips_opts.sym32 = FALSE;
252b5132
RH
12522 else
12523 {
12524 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12525 }
12526 *input_line_pointer = ch;
12527 demand_empty_rest_of_line ();
12528}
12529
12530/* Handle the .abicalls pseudo-op. I believe this is equivalent to
12531 .option pic2. It means to generate SVR4 PIC calls. */
12532
12533static void
17a2f251 12534s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12535{
12536 mips_pic = SVR4_PIC;
143d77c5 12537 mips_abicalls = TRUE;
4d0d148d
TS
12538
12539 if (g_switch_seen && g_switch_value != 0)
12540 as_warn (_("-G may not be used with SVR4 PIC code"));
12541 g_switch_value = 0;
12542
252b5132
RH
12543 bfd_set_gp_size (stdoutput, 0);
12544 demand_empty_rest_of_line ();
12545}
12546
12547/* Handle the .cpload pseudo-op. This is used when generating SVR4
12548 PIC code. It sets the $gp register for the function based on the
12549 function address, which is in the register named in the argument.
12550 This uses a relocation against _gp_disp, which is handled specially
12551 by the linker. The result is:
12552 lui $gp,%hi(_gp_disp)
12553 addiu $gp,$gp,%lo(_gp_disp)
12554 addu $gp,$gp,.cpload argument
aa6975fb
ILT
12555 The .cpload argument is normally $25 == $t9.
12556
12557 The -mno-shared option changes this to:
bbe506e8
TS
12558 lui $gp,%hi(__gnu_local_gp)
12559 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
12560 and the argument is ignored. This saves an instruction, but the
12561 resulting code is not position independent; it uses an absolute
bbe506e8
TS
12562 address for __gnu_local_gp. Thus code assembled with -mno-shared
12563 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
12564
12565static void
17a2f251 12566s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12567{
12568 expressionS ex;
aa6975fb
ILT
12569 int reg;
12570 int in_shared;
252b5132 12571
6478892d
TS
12572 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12573 .cpload is ignored. */
12574 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
12575 {
12576 s_ignore (0);
12577 return;
12578 }
12579
d3ecfc59 12580 /* .cpload should be in a .set noreorder section. */
252b5132
RH
12581 if (mips_opts.noreorder == 0)
12582 as_warn (_(".cpload not in noreorder section"));
12583
aa6975fb
ILT
12584 reg = tc_get_register (0);
12585
12586 /* If we need to produce a 64-bit address, we are better off using
12587 the default instruction sequence. */
aed1a261 12588 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 12589
252b5132 12590 ex.X_op = O_symbol;
bbe506e8
TS
12591 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
12592 "__gnu_local_gp");
252b5132
RH
12593 ex.X_op_symbol = NULL;
12594 ex.X_add_number = 0;
12595
12596 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 12597 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 12598
584892a6 12599 macro_start ();
67c0d1eb
RS
12600 macro_build_lui (&ex, mips_gp_register);
12601 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 12602 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
12603 if (in_shared)
12604 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
12605 mips_gp_register, reg);
584892a6 12606 macro_end ();
252b5132
RH
12607
12608 demand_empty_rest_of_line ();
12609}
12610
6478892d
TS
12611/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
12612 .cpsetup $reg1, offset|$reg2, label
12613
12614 If offset is given, this results in:
12615 sd $gp, offset($sp)
956cd1d6 12616 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
12617 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12618 daddu $gp, $gp, $reg1
6478892d
TS
12619
12620 If $reg2 is given, this results in:
12621 daddu $reg2, $gp, $0
956cd1d6 12622 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
12623 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12624 daddu $gp, $gp, $reg1
aa6975fb
ILT
12625 $reg1 is normally $25 == $t9.
12626
12627 The -mno-shared option replaces the last three instructions with
12628 lui $gp,%hi(_gp)
54f4ddb3 12629 addiu $gp,$gp,%lo(_gp) */
aa6975fb 12630
6478892d 12631static void
17a2f251 12632s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12633{
12634 expressionS ex_off;
12635 expressionS ex_sym;
12636 int reg1;
6478892d 12637
8586fc66 12638 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
12639 We also need NewABI support. */
12640 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12641 {
12642 s_ignore (0);
12643 return;
12644 }
12645
12646 reg1 = tc_get_register (0);
12647 SKIP_WHITESPACE ();
12648 if (*input_line_pointer != ',')
12649 {
12650 as_bad (_("missing argument separator ',' for .cpsetup"));
12651 return;
12652 }
12653 else
80245285 12654 ++input_line_pointer;
6478892d
TS
12655 SKIP_WHITESPACE ();
12656 if (*input_line_pointer == '$')
80245285
TS
12657 {
12658 mips_cpreturn_register = tc_get_register (0);
12659 mips_cpreturn_offset = -1;
12660 }
6478892d 12661 else
80245285
TS
12662 {
12663 mips_cpreturn_offset = get_absolute_expression ();
12664 mips_cpreturn_register = -1;
12665 }
6478892d
TS
12666 SKIP_WHITESPACE ();
12667 if (*input_line_pointer != ',')
12668 {
12669 as_bad (_("missing argument separator ',' for .cpsetup"));
12670 return;
12671 }
12672 else
f9419b05 12673 ++input_line_pointer;
6478892d 12674 SKIP_WHITESPACE ();
f21f8242 12675 expression (&ex_sym);
6478892d 12676
584892a6 12677 macro_start ();
6478892d
TS
12678 if (mips_cpreturn_register == -1)
12679 {
12680 ex_off.X_op = O_constant;
12681 ex_off.X_add_symbol = NULL;
12682 ex_off.X_op_symbol = NULL;
12683 ex_off.X_add_number = mips_cpreturn_offset;
12684
67c0d1eb 12685 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 12686 BFD_RELOC_LO16, SP);
6478892d
TS
12687 }
12688 else
67c0d1eb 12689 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
17a2f251 12690 mips_gp_register, 0);
6478892d 12691
aed1a261 12692 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb
ILT
12693 {
12694 macro_build (&ex_sym, "lui", "t,u", mips_gp_register,
12695 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
12696 BFD_RELOC_HI16_S);
12697
12698 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
12699 mips_gp_register, -1, BFD_RELOC_GPREL16,
12700 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
12701
12702 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
12703 mips_gp_register, reg1);
12704 }
12705 else
12706 {
12707 expressionS ex;
12708
12709 ex.X_op = O_symbol;
4184909a 12710 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
12711 ex.X_op_symbol = NULL;
12712 ex.X_add_number = 0;
6e1304d8 12713
aa6975fb
ILT
12714 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
12715 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12716
12717 macro_build_lui (&ex, mips_gp_register);
12718 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
12719 mips_gp_register, BFD_RELOC_LO16);
12720 }
f21f8242 12721
584892a6 12722 macro_end ();
6478892d
TS
12723
12724 demand_empty_rest_of_line ();
12725}
12726
12727static void
17a2f251 12728s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12729{
12730 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
54f4ddb3 12731 .cplocal is ignored. */
6478892d
TS
12732 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12733 {
12734 s_ignore (0);
12735 return;
12736 }
12737
12738 mips_gp_register = tc_get_register (0);
85b51719 12739 demand_empty_rest_of_line ();
6478892d
TS
12740}
12741
252b5132
RH
12742/* Handle the .cprestore pseudo-op. This stores $gp into a given
12743 offset from $sp. The offset is remembered, and after making a PIC
12744 call $gp is restored from that location. */
12745
12746static void
17a2f251 12747s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12748{
12749 expressionS ex;
252b5132 12750
6478892d 12751 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 12752 .cprestore is ignored. */
6478892d 12753 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
12754 {
12755 s_ignore (0);
12756 return;
12757 }
12758
12759 mips_cprestore_offset = get_absolute_expression ();
7a621144 12760 mips_cprestore_valid = 1;
252b5132
RH
12761
12762 ex.X_op = O_constant;
12763 ex.X_add_symbol = NULL;
12764 ex.X_op_symbol = NULL;
12765 ex.X_add_number = mips_cprestore_offset;
12766
584892a6 12767 macro_start ();
67c0d1eb
RS
12768 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
12769 SP, HAVE_64BIT_ADDRESSES);
584892a6 12770 macro_end ();
252b5132
RH
12771
12772 demand_empty_rest_of_line ();
12773}
12774
6478892d 12775/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 12776 was given in the preceding .cpsetup, it results in:
6478892d 12777 ld $gp, offset($sp)
76b3015f 12778
6478892d 12779 If a register $reg2 was given there, it results in:
54f4ddb3
TS
12780 daddu $gp, $reg2, $0 */
12781
6478892d 12782static void
17a2f251 12783s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12784{
12785 expressionS ex;
6478892d
TS
12786
12787 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12788 We also need NewABI support. */
12789 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12790 {
12791 s_ignore (0);
12792 return;
12793 }
12794
584892a6 12795 macro_start ();
6478892d
TS
12796 if (mips_cpreturn_register == -1)
12797 {
12798 ex.X_op = O_constant;
12799 ex.X_add_symbol = NULL;
12800 ex.X_op_symbol = NULL;
12801 ex.X_add_number = mips_cpreturn_offset;
12802
67c0d1eb 12803 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
12804 }
12805 else
67c0d1eb 12806 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
17a2f251 12807 mips_cpreturn_register, 0);
584892a6 12808 macro_end ();
6478892d
TS
12809
12810 demand_empty_rest_of_line ();
12811}
12812
12813/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
12814 code. It sets the offset to use in gp_rel relocations. */
12815
12816static void
17a2f251 12817s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12818{
12819 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12820 We also need NewABI support. */
12821 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12822 {
12823 s_ignore (0);
12824 return;
12825 }
12826
def2e0dd 12827 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
12828
12829 demand_empty_rest_of_line ();
12830}
12831
252b5132
RH
12832/* Handle the .gpword pseudo-op. This is used when generating PIC
12833 code. It generates a 32 bit GP relative reloc. */
12834
12835static void
17a2f251 12836s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132 12837{
a8dbcb85
TS
12838 segment_info_type *si;
12839 struct insn_label_list *l;
252b5132
RH
12840 symbolS *label;
12841 expressionS ex;
12842 char *p;
12843
12844 /* When not generating PIC code, this is treated as .word. */
12845 if (mips_pic != SVR4_PIC)
12846 {
12847 s_cons (2);
12848 return;
12849 }
12850
a8dbcb85
TS
12851 si = seg_info (now_seg);
12852 l = si->label_list;
12853 label = l != NULL ? l->label : NULL;
7d10b47d 12854 mips_emit_delays ();
252b5132
RH
12855 if (auto_align)
12856 mips_align (2, 0, label);
12857 mips_clear_insn_labels ();
12858
12859 expression (&ex);
12860
12861 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12862 {
12863 as_bad (_("Unsupported use of .gpword"));
12864 ignore_rest_of_line ();
12865 }
12866
12867 p = frag_more (4);
17a2f251 12868 md_number_to_chars (p, 0, 4);
b34976b6 12869 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 12870 BFD_RELOC_GPREL32);
252b5132
RH
12871
12872 demand_empty_rest_of_line ();
12873}
12874
10181a0d 12875static void
17a2f251 12876s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d 12877{
a8dbcb85
TS
12878 segment_info_type *si;
12879 struct insn_label_list *l;
10181a0d
AO
12880 symbolS *label;
12881 expressionS ex;
12882 char *p;
12883
12884 /* When not generating PIC code, this is treated as .dword. */
12885 if (mips_pic != SVR4_PIC)
12886 {
12887 s_cons (3);
12888 return;
12889 }
12890
a8dbcb85
TS
12891 si = seg_info (now_seg);
12892 l = si->label_list;
12893 label = l != NULL ? l->label : NULL;
7d10b47d 12894 mips_emit_delays ();
10181a0d
AO
12895 if (auto_align)
12896 mips_align (3, 0, label);
12897 mips_clear_insn_labels ();
12898
12899 expression (&ex);
12900
12901 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12902 {
12903 as_bad (_("Unsupported use of .gpdword"));
12904 ignore_rest_of_line ();
12905 }
12906
12907 p = frag_more (8);
17a2f251 12908 md_number_to_chars (p, 0, 8);
a105a300 12909 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
6e1304d8 12910 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
12911
12912 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8
RS
12913 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
12914 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
12915
12916 demand_empty_rest_of_line ();
12917}
12918
252b5132
RH
12919/* Handle the .cpadd pseudo-op. This is used when dealing with switch
12920 tables in SVR4 PIC code. */
12921
12922static void
17a2f251 12923s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 12924{
252b5132
RH
12925 int reg;
12926
10181a0d
AO
12927 /* This is ignored when not generating SVR4 PIC code. */
12928 if (mips_pic != SVR4_PIC)
252b5132
RH
12929 {
12930 s_ignore (0);
12931 return;
12932 }
12933
12934 /* Add $gp to the register named as an argument. */
584892a6 12935 macro_start ();
252b5132 12936 reg = tc_get_register (0);
67c0d1eb 12937 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 12938 macro_end ();
252b5132 12939
bdaaa2e1 12940 demand_empty_rest_of_line ();
252b5132
RH
12941}
12942
12943/* Handle the .insn pseudo-op. This marks instruction labels in
12944 mips16 mode. This permits the linker to handle them specially,
12945 such as generating jalx instructions when needed. We also make
12946 them odd for the duration of the assembly, in order to generate the
12947 right sort of code. We will make them even in the adjust_symtab
12948 routine, while leaving them marked. This is convenient for the
12949 debugger and the disassembler. The linker knows to make them odd
12950 again. */
12951
12952static void
17a2f251 12953s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 12954{
f9419b05 12955 mips16_mark_labels ();
252b5132
RH
12956
12957 demand_empty_rest_of_line ();
12958}
12959
12960/* Handle a .stabn directive. We need these in order to mark a label
12961 as being a mips16 text label correctly. Sometimes the compiler
12962 will emit a label, followed by a .stabn, and then switch sections.
12963 If the label and .stabn are in mips16 mode, then the label is
12964 really a mips16 text label. */
12965
12966static void
17a2f251 12967s_mips_stab (int type)
252b5132 12968{
f9419b05 12969 if (type == 'n')
252b5132
RH
12970 mips16_mark_labels ();
12971
12972 s_stab (type);
12973}
12974
54f4ddb3 12975/* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
252b5132
RH
12976
12977static void
17a2f251 12978s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12979{
12980 char *name;
12981 int c;
12982 symbolS *symbolP;
12983 expressionS exp;
12984
12985 name = input_line_pointer;
12986 c = get_symbol_end ();
12987 symbolP = symbol_find_or_make (name);
12988 S_SET_WEAK (symbolP);
12989 *input_line_pointer = c;
12990
12991 SKIP_WHITESPACE ();
12992
12993 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12994 {
12995 if (S_IS_DEFINED (symbolP))
12996 {
956cd1d6 12997 as_bad ("ignoring attempt to redefine symbol %s",
252b5132
RH
12998 S_GET_NAME (symbolP));
12999 ignore_rest_of_line ();
13000 return;
13001 }
bdaaa2e1 13002
252b5132
RH
13003 if (*input_line_pointer == ',')
13004 {
13005 ++input_line_pointer;
13006 SKIP_WHITESPACE ();
13007 }
bdaaa2e1 13008
252b5132
RH
13009 expression (&exp);
13010 if (exp.X_op != O_symbol)
13011 {
13012 as_bad ("bad .weakext directive");
98d3f06f 13013 ignore_rest_of_line ();
252b5132
RH
13014 return;
13015 }
49309057 13016 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
13017 }
13018
13019 demand_empty_rest_of_line ();
13020}
13021
13022/* Parse a register string into a number. Called from the ECOFF code
13023 to parse .frame. The argument is non-zero if this is the frame
13024 register, so that we can record it in mips_frame_reg. */
13025
13026int
17a2f251 13027tc_get_register (int frame)
252b5132 13028{
707bfff6 13029 unsigned int reg;
252b5132
RH
13030
13031 SKIP_WHITESPACE ();
707bfff6
TS
13032 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
13033 reg = 0;
252b5132 13034 if (frame)
7a621144
DJ
13035 {
13036 mips_frame_reg = reg != 0 ? reg : SP;
13037 mips_frame_reg_valid = 1;
13038 mips_cprestore_valid = 0;
13039 }
252b5132
RH
13040 return reg;
13041}
13042
13043valueT
17a2f251 13044md_section_align (asection *seg, valueT addr)
252b5132
RH
13045{
13046 int align = bfd_get_section_alignment (stdoutput, seg);
13047
b4c71f56
TS
13048 if (IS_ELF)
13049 {
13050 /* We don't need to align ELF sections to the full alignment.
13051 However, Irix 5 may prefer that we align them at least to a 16
13052 byte boundary. We don't bother to align the sections if we
13053 are targeted for an embedded system. */
13054 if (strcmp (TARGET_OS, "elf") == 0)
13055 return addr;
13056 if (align > 4)
13057 align = 4;
13058 }
252b5132
RH
13059
13060 return ((addr + (1 << align) - 1) & (-1 << align));
13061}
13062
13063/* Utility routine, called from above as well. If called while the
13064 input file is still being read, it's only an approximation. (For
13065 example, a symbol may later become defined which appeared to be
13066 undefined earlier.) */
13067
13068static int
17a2f251 13069nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
13070{
13071 if (sym == 0)
13072 return 0;
13073
4d0d148d 13074 if (g_switch_value > 0)
252b5132
RH
13075 {
13076 const char *symname;
13077 int change;
13078
c9914766 13079 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
13080 register. It can be if it is smaller than the -G size or if
13081 it is in the .sdata or .sbss section. Certain symbols can
c9914766 13082 not be referenced off the $gp, although it appears as though
252b5132
RH
13083 they can. */
13084 symname = S_GET_NAME (sym);
13085 if (symname != (const char *) NULL
13086 && (strcmp (symname, "eprol") == 0
13087 || strcmp (symname, "etext") == 0
13088 || strcmp (symname, "_gp") == 0
13089 || strcmp (symname, "edata") == 0
13090 || strcmp (symname, "_fbss") == 0
13091 || strcmp (symname, "_fdata") == 0
13092 || strcmp (symname, "_ftext") == 0
13093 || strcmp (symname, "end") == 0
13094 || strcmp (symname, "_gp_disp") == 0))
13095 change = 1;
13096 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
13097 && (0
13098#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
13099 || (symbol_get_obj (sym)->ecoff_extern_size != 0
13100 && (symbol_get_obj (sym)->ecoff_extern_size
13101 <= g_switch_value))
252b5132
RH
13102#endif
13103 /* We must defer this decision until after the whole
13104 file has been read, since there might be a .extern
13105 after the first use of this symbol. */
13106 || (before_relaxing
13107#ifndef NO_ECOFF_DEBUGGING
49309057 13108 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
13109#endif
13110 && S_GET_VALUE (sym) == 0)
13111 || (S_GET_VALUE (sym) != 0
13112 && S_GET_VALUE (sym) <= g_switch_value)))
13113 change = 0;
13114 else
13115 {
13116 const char *segname;
13117
13118 segname = segment_name (S_GET_SEGMENT (sym));
13119 assert (strcmp (segname, ".lit8") != 0
13120 && strcmp (segname, ".lit4") != 0);
13121 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
13122 && strcmp (segname, ".sbss") != 0
13123 && strncmp (segname, ".sdata.", 7) != 0
d4dc2f22
TS
13124 && strncmp (segname, ".sbss.", 6) != 0
13125 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
fba2b7f9 13126 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
13127 }
13128 return change;
13129 }
13130 else
c9914766 13131 /* We are not optimizing for the $gp register. */
252b5132
RH
13132 return 1;
13133}
13134
5919d012
RS
13135
13136/* Return true if the given symbol should be considered local for SVR4 PIC. */
13137
13138static bfd_boolean
17a2f251 13139pic_need_relax (symbolS *sym, asection *segtype)
5919d012
RS
13140{
13141 asection *symsec;
5919d012
RS
13142
13143 /* Handle the case of a symbol equated to another symbol. */
13144 while (symbol_equated_reloc_p (sym))
13145 {
13146 symbolS *n;
13147
5f0fe04b 13148 /* It's possible to get a loop here in a badly written program. */
5919d012
RS
13149 n = symbol_get_value_expression (sym)->X_add_symbol;
13150 if (n == sym)
13151 break;
13152 sym = n;
13153 }
13154
df1f3cda
DD
13155 if (symbol_section_p (sym))
13156 return TRUE;
13157
5919d012
RS
13158 symsec = S_GET_SEGMENT (sym);
13159
5919d012
RS
13160 /* This must duplicate the test in adjust_reloc_syms. */
13161 return (symsec != &bfd_und_section
13162 && symsec != &bfd_abs_section
5f0fe04b
TS
13163 && !bfd_is_com_section (symsec)
13164 && !s_is_linkonce (sym, segtype)
5919d012
RS
13165#ifdef OBJ_ELF
13166 /* A global or weak symbol is treated as external. */
f43abd2b 13167 && (!IS_ELF || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym)))
5919d012
RS
13168#endif
13169 );
13170}
13171
13172
252b5132
RH
13173/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
13174 extended opcode. SEC is the section the frag is in. */
13175
13176static int
17a2f251 13177mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132
RH
13178{
13179 int type;
3994f87e 13180 const struct mips16_immed_operand *op;
252b5132
RH
13181 offsetT val;
13182 int mintiny, maxtiny;
13183 segT symsec;
98aa84af 13184 fragS *sym_frag;
252b5132
RH
13185
13186 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
13187 return 0;
13188 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
13189 return 1;
13190
13191 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13192 op = mips16_immed_operands;
13193 while (op->type != type)
13194 {
13195 ++op;
13196 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
13197 }
13198
13199 if (op->unsp)
13200 {
13201 if (type == '<' || type == '>' || type == '[' || type == ']')
13202 {
13203 mintiny = 1;
13204 maxtiny = 1 << op->nbits;
13205 }
13206 else
13207 {
13208 mintiny = 0;
13209 maxtiny = (1 << op->nbits) - 1;
13210 }
13211 }
13212 else
13213 {
13214 mintiny = - (1 << (op->nbits - 1));
13215 maxtiny = (1 << (op->nbits - 1)) - 1;
13216 }
13217
98aa84af 13218 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 13219 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 13220 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
13221
13222 if (op->pcrel)
13223 {
13224 addressT addr;
13225
13226 /* We won't have the section when we are called from
13227 mips_relax_frag. However, we will always have been called
13228 from md_estimate_size_before_relax first. If this is a
13229 branch to a different section, we mark it as such. If SEC is
13230 NULL, and the frag is not marked, then it must be a branch to
13231 the same section. */
13232 if (sec == NULL)
13233 {
13234 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
13235 return 1;
13236 }
13237 else
13238 {
98aa84af 13239 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
13240 if (symsec != sec)
13241 {
13242 fragp->fr_subtype =
13243 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13244
13245 /* FIXME: We should support this, and let the linker
13246 catch branches and loads that are out of range. */
13247 as_bad_where (fragp->fr_file, fragp->fr_line,
13248 _("unsupported PC relative reference to different section"));
13249
13250 return 1;
13251 }
98aa84af
AM
13252 if (fragp != sym_frag && sym_frag->fr_address == 0)
13253 /* Assume non-extended on the first relaxation pass.
13254 The address we have calculated will be bogus if this is
13255 a forward branch to another frag, as the forward frag
13256 will have fr_address == 0. */
13257 return 0;
252b5132
RH
13258 }
13259
13260 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
13261 the same section. If the relax_marker of the symbol fragment
13262 differs from the relax_marker of this fragment, we have not
13263 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
13264 in STRETCH in order to get a better estimate of the address.
13265 This particularly matters because of the shift bits. */
13266 if (stretch != 0
98aa84af 13267 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
13268 {
13269 fragS *f;
13270
13271 /* Adjust stretch for any alignment frag. Note that if have
13272 been expanding the earlier code, the symbol may be
13273 defined in what appears to be an earlier frag. FIXME:
13274 This doesn't handle the fr_subtype field, which specifies
13275 a maximum number of bytes to skip when doing an
13276 alignment. */
98aa84af 13277 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
13278 {
13279 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
13280 {
13281 if (stretch < 0)
13282 stretch = - ((- stretch)
13283 & ~ ((1 << (int) f->fr_offset) - 1));
13284 else
13285 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
13286 if (stretch == 0)
13287 break;
13288 }
13289 }
13290 if (f != NULL)
13291 val += stretch;
13292 }
13293
13294 addr = fragp->fr_address + fragp->fr_fix;
13295
13296 /* The base address rules are complicated. The base address of
13297 a branch is the following instruction. The base address of a
13298 PC relative load or add is the instruction itself, but if it
13299 is in a delay slot (in which case it can not be extended) use
13300 the address of the instruction whose delay slot it is in. */
13301 if (type == 'p' || type == 'q')
13302 {
13303 addr += 2;
13304
13305 /* If we are currently assuming that this frag should be
13306 extended, then, the current address is two bytes
bdaaa2e1 13307 higher. */
252b5132
RH
13308 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13309 addr += 2;
13310
13311 /* Ignore the low bit in the target, since it will be set
13312 for a text label. */
13313 if ((val & 1) != 0)
13314 --val;
13315 }
13316 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13317 addr -= 4;
13318 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13319 addr -= 2;
13320
13321 val -= addr & ~ ((1 << op->shift) - 1);
13322
13323 /* Branch offsets have an implicit 0 in the lowest bit. */
13324 if (type == 'p' || type == 'q')
13325 val /= 2;
13326
13327 /* If any of the shifted bits are set, we must use an extended
13328 opcode. If the address depends on the size of this
13329 instruction, this can lead to a loop, so we arrange to always
13330 use an extended opcode. We only check this when we are in
13331 the main relaxation loop, when SEC is NULL. */
13332 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
13333 {
13334 fragp->fr_subtype =
13335 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13336 return 1;
13337 }
13338
13339 /* If we are about to mark a frag as extended because the value
13340 is precisely maxtiny + 1, then there is a chance of an
13341 infinite loop as in the following code:
13342 la $4,foo
13343 .skip 1020
13344 .align 2
13345 foo:
13346 In this case when the la is extended, foo is 0x3fc bytes
13347 away, so the la can be shrunk, but then foo is 0x400 away, so
13348 the la must be extended. To avoid this loop, we mark the
13349 frag as extended if it was small, and is about to become
13350 extended with a value of maxtiny + 1. */
13351 if (val == ((maxtiny + 1) << op->shift)
13352 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
13353 && sec == NULL)
13354 {
13355 fragp->fr_subtype =
13356 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13357 return 1;
13358 }
13359 }
13360 else if (symsec != absolute_section && sec != NULL)
13361 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
13362
13363 if ((val & ((1 << op->shift) - 1)) != 0
13364 || val < (mintiny << op->shift)
13365 || val > (maxtiny << op->shift))
13366 return 1;
13367 else
13368 return 0;
13369}
13370
4a6a3df4
AO
13371/* Compute the length of a branch sequence, and adjust the
13372 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
13373 worst-case length is computed, with UPDATE being used to indicate
13374 whether an unconditional (-1), branch-likely (+1) or regular (0)
13375 branch is to be computed. */
13376static int
17a2f251 13377relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 13378{
b34976b6 13379 bfd_boolean toofar;
4a6a3df4
AO
13380 int length;
13381
13382 if (fragp
13383 && S_IS_DEFINED (fragp->fr_symbol)
13384 && sec == S_GET_SEGMENT (fragp->fr_symbol))
13385 {
13386 addressT addr;
13387 offsetT val;
13388
13389 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
13390
13391 addr = fragp->fr_address + fragp->fr_fix + 4;
13392
13393 val -= addr;
13394
13395 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
13396 }
13397 else if (fragp)
13398 /* If the symbol is not defined or it's in a different segment,
13399 assume the user knows what's going on and emit a short
13400 branch. */
b34976b6 13401 toofar = FALSE;
4a6a3df4 13402 else
b34976b6 13403 toofar = TRUE;
4a6a3df4
AO
13404
13405 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13406 fragp->fr_subtype
af6ae2ad 13407 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
13408 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
13409 RELAX_BRANCH_LINK (fragp->fr_subtype),
13410 toofar);
13411
13412 length = 4;
13413 if (toofar)
13414 {
13415 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
13416 length += 8;
13417
13418 if (mips_pic != NO_PIC)
13419 {
13420 /* Additional space for PIC loading of target address. */
13421 length += 8;
13422 if (mips_opts.isa == ISA_MIPS1)
13423 /* Additional space for $at-stabilizing nop. */
13424 length += 4;
13425 }
13426
13427 /* If branch is conditional. */
13428 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
13429 length += 8;
13430 }
b34976b6 13431
4a6a3df4
AO
13432 return length;
13433}
13434
252b5132
RH
13435/* Estimate the size of a frag before relaxing. Unless this is the
13436 mips16, we are not really relaxing here, and the final size is
13437 encoded in the subtype information. For the mips16, we have to
13438 decide whether we are using an extended opcode or not. */
13439
252b5132 13440int
17a2f251 13441md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 13442{
5919d012 13443 int change;
252b5132 13444
4a6a3df4
AO
13445 if (RELAX_BRANCH_P (fragp->fr_subtype))
13446 {
13447
b34976b6
AM
13448 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
13449
4a6a3df4
AO
13450 return fragp->fr_var;
13451 }
13452
252b5132 13453 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
13454 /* We don't want to modify the EXTENDED bit here; it might get us
13455 into infinite loops. We change it only in mips_relax_frag(). */
13456 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132
RH
13457
13458 if (mips_pic == NO_PIC)
5919d012 13459 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132 13460 else if (mips_pic == SVR4_PIC)
5919d012 13461 change = pic_need_relax (fragp->fr_symbol, segtype);
0a44bf69
RS
13462 else if (mips_pic == VXWORKS_PIC)
13463 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
13464 change = 0;
252b5132
RH
13465 else
13466 abort ();
13467
13468 if (change)
13469 {
4d7206a2 13470 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 13471 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 13472 }
4d7206a2
RS
13473 else
13474 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
13475}
13476
13477/* This is called to see whether a reloc against a defined symbol
de7e6852 13478 should be converted into a reloc against a section. */
252b5132
RH
13479
13480int
17a2f251 13481mips_fix_adjustable (fixS *fixp)
252b5132 13482{
252b5132
RH
13483 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13484 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13485 return 0;
a161fe53 13486
252b5132
RH
13487 if (fixp->fx_addsy == NULL)
13488 return 1;
a161fe53 13489
de7e6852
RS
13490 /* If symbol SYM is in a mergeable section, relocations of the form
13491 SYM + 0 can usually be made section-relative. The mergeable data
13492 is then identified by the section offset rather than by the symbol.
13493
13494 However, if we're generating REL LO16 relocations, the offset is split
13495 between the LO16 and parterning high part relocation. The linker will
13496 need to recalculate the complete offset in order to correctly identify
13497 the merge data.
13498
13499 The linker has traditionally not looked for the parterning high part
13500 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
13501 placed anywhere. Rather than break backwards compatibility by changing
13502 this, it seems better not to force the issue, and instead keep the
13503 original symbol. This will work with either linker behavior. */
704803a9
MR
13504 if ((fixp->fx_r_type == BFD_RELOC_LO16
13505 || fixp->fx_r_type == BFD_RELOC_MIPS16_LO16
13506 || reloc_needs_lo_p (fixp->fx_r_type))
de7e6852
RS
13507 && HAVE_IN_PLACE_ADDENDS
13508 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
13509 return 0;
13510
252b5132 13511#ifdef OBJ_ELF
de7e6852
RS
13512 /* Don't adjust relocations against mips16 symbols, so that the linker
13513 can find them if it needs to set up a stub. */
f43abd2b 13514 if (IS_ELF
252b5132
RH
13515 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13516 && fixp->fx_subsy == NULL)
13517 return 0;
13518#endif
a161fe53 13519
252b5132
RH
13520 return 1;
13521}
13522
13523/* Translate internal representation of relocation info to BFD target
13524 format. */
13525
13526arelent **
17a2f251 13527tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
13528{
13529 static arelent *retval[4];
13530 arelent *reloc;
13531 bfd_reloc_code_real_type code;
13532
4b0cff4e
TS
13533 memset (retval, 0, sizeof(retval));
13534 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
49309057
ILT
13535 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13536 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
13537 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13538
bad36eac
DJ
13539 if (fixp->fx_pcrel)
13540 {
13541 assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2);
13542
13543 /* At this point, fx_addnumber is "symbol offset - pcrel address".
13544 Relocations want only the symbol offset. */
13545 reloc->addend = fixp->fx_addnumber + reloc->address;
f43abd2b 13546 if (!IS_ELF)
bad36eac
DJ
13547 {
13548 /* A gruesome hack which is a result of the gruesome gas
13549 reloc handling. What's worse, for COFF (as opposed to
13550 ECOFF), we might need yet another copy of reloc->address.
13551 See bfd_install_relocation. */
13552 reloc->addend += reloc->address;
13553 }
13554 }
13555 else
13556 reloc->addend = fixp->fx_addnumber;
252b5132 13557
438c16b8
TS
13558 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13559 entry to be used in the relocation's section offset. */
13560 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
13561 {
13562 reloc->address = reloc->addend;
13563 reloc->addend = 0;
13564 }
13565
252b5132 13566 code = fixp->fx_r_type;
252b5132 13567
bad36eac 13568 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
252b5132
RH
13569 if (reloc->howto == NULL)
13570 {
13571 as_bad_where (fixp->fx_file, fixp->fx_line,
13572 _("Can not represent %s relocation in this object file format"),
13573 bfd_get_reloc_code_name (code));
13574 retval[0] = NULL;
13575 }
13576
13577 return retval;
13578}
13579
13580/* Relax a machine dependent frag. This returns the amount by which
13581 the current size of the frag should change. */
13582
13583int
17a2f251 13584mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 13585{
4a6a3df4
AO
13586 if (RELAX_BRANCH_P (fragp->fr_subtype))
13587 {
13588 offsetT old_var = fragp->fr_var;
b34976b6
AM
13589
13590 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
13591
13592 return fragp->fr_var - old_var;
13593 }
13594
252b5132
RH
13595 if (! RELAX_MIPS16_P (fragp->fr_subtype))
13596 return 0;
13597
c4e7957c 13598 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
13599 {
13600 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13601 return 0;
13602 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13603 return 2;
13604 }
13605 else
13606 {
13607 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13608 return 0;
13609 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13610 return -2;
13611 }
13612
13613 return 0;
13614}
13615
13616/* Convert a machine dependent frag. */
13617
13618void
17a2f251 13619md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 13620{
4a6a3df4
AO
13621 if (RELAX_BRANCH_P (fragp->fr_subtype))
13622 {
13623 bfd_byte *buf;
13624 unsigned long insn;
13625 expressionS exp;
13626 fixS *fixp;
b34976b6 13627
4a6a3df4
AO
13628 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13629
13630 if (target_big_endian)
13631 insn = bfd_getb32 (buf);
13632 else
13633 insn = bfd_getl32 (buf);
b34976b6 13634
4a6a3df4
AO
13635 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13636 {
13637 /* We generate a fixup instead of applying it right now
13638 because, if there are linker relaxations, we're going to
13639 need the relocations. */
13640 exp.X_op = O_symbol;
13641 exp.X_add_symbol = fragp->fr_symbol;
13642 exp.X_add_number = fragp->fr_offset;
13643
13644 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
3994f87e 13645 4, &exp, TRUE, BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
13646 fixp->fx_file = fragp->fr_file;
13647 fixp->fx_line = fragp->fr_line;
b34976b6 13648
2132e3a3 13649 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
13650 buf += 4;
13651 }
13652 else
13653 {
13654 int i;
13655
13656 as_warn_where (fragp->fr_file, fragp->fr_line,
13657 _("relaxed out-of-range branch into a jump"));
13658
13659 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13660 goto uncond;
13661
13662 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13663 {
13664 /* Reverse the branch. */
13665 switch ((insn >> 28) & 0xf)
13666 {
13667 case 4:
13668 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13669 have the condition reversed by tweaking a single
13670 bit, and their opcodes all have 0x4???????. */
13671 assert ((insn & 0xf1000000) == 0x41000000);
13672 insn ^= 0x00010000;
13673 break;
13674
13675 case 0:
13676 /* bltz 0x04000000 bgez 0x04010000
54f4ddb3 13677 bltzal 0x04100000 bgezal 0x04110000 */
4a6a3df4
AO
13678 assert ((insn & 0xfc0e0000) == 0x04000000);
13679 insn ^= 0x00010000;
13680 break;
b34976b6 13681
4a6a3df4
AO
13682 case 1:
13683 /* beq 0x10000000 bne 0x14000000
54f4ddb3 13684 blez 0x18000000 bgtz 0x1c000000 */
4a6a3df4
AO
13685 insn ^= 0x04000000;
13686 break;
13687
13688 default:
13689 abort ();
13690 }
13691 }
13692
13693 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13694 {
13695 /* Clear the and-link bit. */
13696 assert ((insn & 0xfc1c0000) == 0x04100000);
13697
54f4ddb3
TS
13698 /* bltzal 0x04100000 bgezal 0x04110000
13699 bltzall 0x04120000 bgezall 0x04130000 */
4a6a3df4
AO
13700 insn &= ~0x00100000;
13701 }
13702
13703 /* Branch over the branch (if the branch was likely) or the
13704 full jump (not likely case). Compute the offset from the
13705 current instruction to branch to. */
13706 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13707 i = 16;
13708 else
13709 {
13710 /* How many bytes in instructions we've already emitted? */
13711 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13712 /* How many bytes in instructions from here to the end? */
13713 i = fragp->fr_var - i;
13714 }
13715 /* Convert to instruction count. */
13716 i >>= 2;
13717 /* Branch counts from the next instruction. */
b34976b6 13718 i--;
4a6a3df4
AO
13719 insn |= i;
13720 /* Branch over the jump. */
2132e3a3 13721 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
13722 buf += 4;
13723
54f4ddb3 13724 /* nop */
2132e3a3 13725 md_number_to_chars ((char *) buf, 0, 4);
4a6a3df4
AO
13726 buf += 4;
13727
13728 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13729 {
13730 /* beql $0, $0, 2f */
13731 insn = 0x50000000;
13732 /* Compute the PC offset from the current instruction to
13733 the end of the variable frag. */
13734 /* How many bytes in instructions we've already emitted? */
13735 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13736 /* How many bytes in instructions from here to the end? */
13737 i = fragp->fr_var - i;
13738 /* Convert to instruction count. */
13739 i >>= 2;
13740 /* Don't decrement i, because we want to branch over the
13741 delay slot. */
13742
13743 insn |= i;
2132e3a3 13744 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
13745 buf += 4;
13746
2132e3a3 13747 md_number_to_chars ((char *) buf, 0, 4);
4a6a3df4
AO
13748 buf += 4;
13749 }
13750
13751 uncond:
13752 if (mips_pic == NO_PIC)
13753 {
13754 /* j or jal. */
13755 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13756 ? 0x0c000000 : 0x08000000);
13757 exp.X_op = O_symbol;
13758 exp.X_add_symbol = fragp->fr_symbol;
13759 exp.X_add_number = fragp->fr_offset;
13760
13761 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
3994f87e 13762 4, &exp, FALSE, BFD_RELOC_MIPS_JMP);
4a6a3df4
AO
13763 fixp->fx_file = fragp->fr_file;
13764 fixp->fx_line = fragp->fr_line;
13765
2132e3a3 13766 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
13767 buf += 4;
13768 }
13769 else
13770 {
13771 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
13772 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13773 exp.X_op = O_symbol;
13774 exp.X_add_symbol = fragp->fr_symbol;
13775 exp.X_add_number = fragp->fr_offset;
13776
13777 if (fragp->fr_offset)
13778 {
13779 exp.X_add_symbol = make_expr_symbol (&exp);
13780 exp.X_add_number = 0;
13781 }
13782
13783 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
3994f87e 13784 4, &exp, FALSE, BFD_RELOC_MIPS_GOT16);
4a6a3df4
AO
13785 fixp->fx_file = fragp->fr_file;
13786 fixp->fx_line = fragp->fr_line;
13787
2132e3a3 13788 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4 13789 buf += 4;
b34976b6 13790
4a6a3df4
AO
13791 if (mips_opts.isa == ISA_MIPS1)
13792 {
13793 /* nop */
2132e3a3 13794 md_number_to_chars ((char *) buf, 0, 4);
4a6a3df4
AO
13795 buf += 4;
13796 }
13797
13798 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
13799 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13800
13801 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
3994f87e 13802 4, &exp, FALSE, BFD_RELOC_LO16);
4a6a3df4
AO
13803 fixp->fx_file = fragp->fr_file;
13804 fixp->fx_line = fragp->fr_line;
b34976b6 13805
2132e3a3 13806 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
13807 buf += 4;
13808
13809 /* j(al)r $at. */
13810 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13811 insn = 0x0020f809;
13812 else
13813 insn = 0x00200008;
13814
2132e3a3 13815 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
13816 buf += 4;
13817 }
13818 }
13819
13820 assert (buf == (bfd_byte *)fragp->fr_literal
13821 + fragp->fr_fix + fragp->fr_var);
13822
13823 fragp->fr_fix += fragp->fr_var;
13824
13825 return;
13826 }
13827
252b5132
RH
13828 if (RELAX_MIPS16_P (fragp->fr_subtype))
13829 {
13830 int type;
3994f87e 13831 const struct mips16_immed_operand *op;
b34976b6 13832 bfd_boolean small, ext;
252b5132
RH
13833 offsetT val;
13834 bfd_byte *buf;
13835 unsigned long insn;
b34976b6 13836 bfd_boolean use_extend;
252b5132
RH
13837 unsigned short extend;
13838
13839 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13840 op = mips16_immed_operands;
13841 while (op->type != type)
13842 ++op;
13843
13844 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13845 {
b34976b6
AM
13846 small = FALSE;
13847 ext = TRUE;
252b5132
RH
13848 }
13849 else
13850 {
b34976b6
AM
13851 small = TRUE;
13852 ext = FALSE;
252b5132
RH
13853 }
13854
6386f3a7 13855 resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
13856 val = S_GET_VALUE (fragp->fr_symbol);
13857 if (op->pcrel)
13858 {
13859 addressT addr;
13860
13861 addr = fragp->fr_address + fragp->fr_fix;
13862
13863 /* The rules for the base address of a PC relative reloc are
13864 complicated; see mips16_extended_frag. */
13865 if (type == 'p' || type == 'q')
13866 {
13867 addr += 2;
13868 if (ext)
13869 addr += 2;
13870 /* Ignore the low bit in the target, since it will be
13871 set for a text label. */
13872 if ((val & 1) != 0)
13873 --val;
13874 }
13875 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13876 addr -= 4;
13877 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13878 addr -= 2;
13879
13880 addr &= ~ (addressT) ((1 << op->shift) - 1);
13881 val -= addr;
13882
13883 /* Make sure the section winds up with the alignment we have
13884 assumed. */
13885 if (op->shift > 0)
13886 record_alignment (asec, op->shift);
13887 }
13888
13889 if (ext
13890 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13891 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13892 as_warn_where (fragp->fr_file, fragp->fr_line,
13893 _("extended instruction in delay slot"));
13894
13895 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13896
13897 if (target_big_endian)
13898 insn = bfd_getb16 (buf);
13899 else
13900 insn = bfd_getl16 (buf);
13901
13902 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13903 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13904 small, ext, &insn, &use_extend, &extend);
13905
13906 if (use_extend)
13907 {
2132e3a3 13908 md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
252b5132
RH
13909 fragp->fr_fix += 2;
13910 buf += 2;
13911 }
13912
2132e3a3 13913 md_number_to_chars ((char *) buf, insn, 2);
252b5132
RH
13914 fragp->fr_fix += 2;
13915 buf += 2;
13916 }
13917 else
13918 {
4d7206a2
RS
13919 int first, second;
13920 fixS *fixp;
252b5132 13921
4d7206a2
RS
13922 first = RELAX_FIRST (fragp->fr_subtype);
13923 second = RELAX_SECOND (fragp->fr_subtype);
13924 fixp = (fixS *) fragp->fr_opcode;
252b5132 13925
584892a6
RS
13926 /* Possibly emit a warning if we've chosen the longer option. */
13927 if (((fragp->fr_subtype & RELAX_USE_SECOND) != 0)
13928 == ((fragp->fr_subtype & RELAX_SECOND_LONGER) != 0))
13929 {
13930 const char *msg = macro_warning (fragp->fr_subtype);
13931 if (msg != 0)
13932 as_warn_where (fragp->fr_file, fragp->fr_line, msg);
13933 }
13934
4d7206a2
RS
13935 /* Go through all the fixups for the first sequence. Disable them
13936 (by marking them as done) if we're going to use the second
13937 sequence instead. */
13938 while (fixp
13939 && fixp->fx_frag == fragp
13940 && fixp->fx_where < fragp->fr_fix - second)
13941 {
13942 if (fragp->fr_subtype & RELAX_USE_SECOND)
13943 fixp->fx_done = 1;
13944 fixp = fixp->fx_next;
13945 }
252b5132 13946
4d7206a2
RS
13947 /* Go through the fixups for the second sequence. Disable them if
13948 we're going to use the first sequence, otherwise adjust their
13949 addresses to account for the relaxation. */
13950 while (fixp && fixp->fx_frag == fragp)
13951 {
13952 if (fragp->fr_subtype & RELAX_USE_SECOND)
13953 fixp->fx_where -= first;
13954 else
13955 fixp->fx_done = 1;
13956 fixp = fixp->fx_next;
13957 }
13958
13959 /* Now modify the frag contents. */
13960 if (fragp->fr_subtype & RELAX_USE_SECOND)
13961 {
13962 char *start;
13963
13964 start = fragp->fr_literal + fragp->fr_fix - first - second;
13965 memmove (start, start + first, second);
13966 fragp->fr_fix -= first;
13967 }
13968 else
13969 fragp->fr_fix -= second;
252b5132
RH
13970 }
13971}
13972
13973#ifdef OBJ_ELF
13974
13975/* This function is called after the relocs have been generated.
13976 We've been storing mips16 text labels as odd. Here we convert them
13977 back to even for the convenience of the debugger. */
13978
13979void
17a2f251 13980mips_frob_file_after_relocs (void)
252b5132
RH
13981{
13982 asymbol **syms;
13983 unsigned int count, i;
13984
f43abd2b 13985 if (!IS_ELF)
252b5132
RH
13986 return;
13987
13988 syms = bfd_get_outsymbols (stdoutput);
13989 count = bfd_get_symcount (stdoutput);
13990 for (i = 0; i < count; i++, syms++)
13991 {
13992 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13993 && ((*syms)->value & 1) != 0)
13994 {
13995 (*syms)->value &= ~1;
13996 /* If the symbol has an odd size, it was probably computed
13997 incorrectly, so adjust that as well. */
13998 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13999 ++elf_symbol (*syms)->internal_elf_sym.st_size;
14000 }
14001 }
14002}
14003
14004#endif
14005
14006/* This function is called whenever a label is defined. It is used
14007 when handling branch delays; if a branch has a label, we assume we
14008 can not move it. */
14009
14010void
17a2f251 14011mips_define_label (symbolS *sym)
252b5132 14012{
a8dbcb85 14013 segment_info_type *si = seg_info (now_seg);
252b5132
RH
14014 struct insn_label_list *l;
14015
14016 if (free_insn_labels == NULL)
14017 l = (struct insn_label_list *) xmalloc (sizeof *l);
14018 else
14019 {
14020 l = free_insn_labels;
14021 free_insn_labels = l->next;
14022 }
14023
14024 l->label = sym;
a8dbcb85
TS
14025 l->next = si->label_list;
14026 si->label_list = l;
07a53e5c
RH
14027
14028#ifdef OBJ_ELF
14029 dwarf2_emit_label (sym);
14030#endif
252b5132
RH
14031}
14032\f
14033#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14034
14035/* Some special processing for a MIPS ELF file. */
14036
14037void
17a2f251 14038mips_elf_final_processing (void)
252b5132
RH
14039{
14040 /* Write out the register information. */
316f5878 14041 if (mips_abi != N64_ABI)
252b5132
RH
14042 {
14043 Elf32_RegInfo s;
14044
14045 s.ri_gprmask = mips_gprmask;
14046 s.ri_cprmask[0] = mips_cprmask[0];
14047 s.ri_cprmask[1] = mips_cprmask[1];
14048 s.ri_cprmask[2] = mips_cprmask[2];
14049 s.ri_cprmask[3] = mips_cprmask[3];
14050 /* The gp_value field is set by the MIPS ELF backend. */
14051
14052 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
14053 ((Elf32_External_RegInfo *)
14054 mips_regmask_frag));
14055 }
14056 else
14057 {
14058 Elf64_Internal_RegInfo s;
14059
14060 s.ri_gprmask = mips_gprmask;
14061 s.ri_pad = 0;
14062 s.ri_cprmask[0] = mips_cprmask[0];
14063 s.ri_cprmask[1] = mips_cprmask[1];
14064 s.ri_cprmask[2] = mips_cprmask[2];
14065 s.ri_cprmask[3] = mips_cprmask[3];
14066 /* The gp_value field is set by the MIPS ELF backend. */
14067
14068 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
14069 ((Elf64_External_RegInfo *)
14070 mips_regmask_frag));
14071 }
14072
14073 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
14074 sort of BFD interface for this. */
14075 if (mips_any_noreorder)
14076 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
14077 if (mips_pic != NO_PIC)
143d77c5 14078 {
252b5132 14079 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
14080 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
14081 }
14082 if (mips_abicalls)
14083 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 14084
98d3f06f 14085 /* Set MIPS ELF flags for ASEs. */
74cd071d
CF
14086 /* We may need to define a new flag for DSP ASE, and set this flag when
14087 file_ase_dsp is true. */
ef2e4d86
CF
14088 /* We may need to define a new flag for MT ASE, and set this flag when
14089 file_ase_mt is true. */
a4672219
TS
14090 if (file_ase_mips16)
14091 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
1f25f5d3
CD
14092#if 0 /* XXX FIXME */
14093 if (file_ase_mips3d)
14094 elf_elfheader (stdoutput)->e_flags |= ???;
14095#endif
deec1734
CD
14096 if (file_ase_mdmx)
14097 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 14098
bdaaa2e1 14099 /* Set the MIPS ELF ABI flags. */
316f5878 14100 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 14101 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 14102 else if (mips_abi == O64_ABI)
252b5132 14103 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 14104 else if (mips_abi == EABI_ABI)
252b5132 14105 {
316f5878 14106 if (!file_mips_gp32)
252b5132
RH
14107 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
14108 else
14109 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
14110 }
316f5878 14111 else if (mips_abi == N32_ABI)
be00bddd
TS
14112 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
14113
c9914766 14114 /* Nothing to do for N64_ABI. */
252b5132
RH
14115
14116 if (mips_32bitmode)
14117 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
ad3fea08
TS
14118
14119#if 0 /* XXX FIXME */
14120 /* 32 bit code with 64 bit FP registers. */
14121 if (!file_mips_fp32 && ABI_NEEDS_32BIT_REGS (mips_abi))
14122 elf_elfheader (stdoutput)->e_flags |= ???;
14123#endif
252b5132
RH
14124}
14125
14126#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
14127\f
beae10d5 14128typedef struct proc {
9b2f1d35
EC
14129 symbolS *func_sym;
14130 symbolS *func_end_sym;
beae10d5
KH
14131 unsigned long reg_mask;
14132 unsigned long reg_offset;
14133 unsigned long fpreg_mask;
14134 unsigned long fpreg_offset;
14135 unsigned long frame_offset;
14136 unsigned long frame_reg;
14137 unsigned long pc_reg;
14138} procS;
252b5132
RH
14139
14140static procS cur_proc;
14141static procS *cur_proc_ptr;
14142static int numprocs;
14143
0a9ef439 14144/* Fill in an rs_align_code fragment. */
a19d8eb0 14145
0a9ef439 14146void
17a2f251 14147mips_handle_align (fragS *fragp)
a19d8eb0 14148{
0a9ef439
RH
14149 if (fragp->fr_type != rs_align_code)
14150 return;
14151
14152 if (mips_opts.mips16)
a19d8eb0
CP
14153 {
14154 static const unsigned char be_nop[] = { 0x65, 0x00 };
14155 static const unsigned char le_nop[] = { 0x00, 0x65 };
14156
0a9ef439
RH
14157 int bytes;
14158 char *p;
a19d8eb0 14159
0a9ef439
RH
14160 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
14161 p = fragp->fr_literal + fragp->fr_fix;
14162
14163 if (bytes & 1)
14164 {
14165 *p++ = 0;
f9419b05 14166 fragp->fr_fix++;
0a9ef439
RH
14167 }
14168
14169 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
14170 fragp->fr_var = 2;
a19d8eb0
CP
14171 }
14172
0a9ef439 14173 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
a19d8eb0
CP
14174}
14175
252b5132 14176static void
17a2f251 14177md_obj_begin (void)
252b5132
RH
14178{
14179}
14180
14181static void
17a2f251 14182md_obj_end (void)
252b5132 14183{
54f4ddb3 14184 /* Check for premature end, nesting errors, etc. */
252b5132 14185 if (cur_proc_ptr)
9a41af64 14186 as_warn (_("missing .end at end of assembly"));
252b5132
RH
14187}
14188
14189static long
17a2f251 14190get_number (void)
252b5132
RH
14191{
14192 int negative = 0;
14193 long val = 0;
14194
14195 if (*input_line_pointer == '-')
14196 {
14197 ++input_line_pointer;
14198 negative = 1;
14199 }
3882b010 14200 if (!ISDIGIT (*input_line_pointer))
956cd1d6 14201 as_bad (_("expected simple number"));
252b5132
RH
14202 if (input_line_pointer[0] == '0')
14203 {
14204 if (input_line_pointer[1] == 'x')
14205 {
14206 input_line_pointer += 2;
3882b010 14207 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
14208 {
14209 val <<= 4;
14210 val |= hex_value (*input_line_pointer++);
14211 }
14212 return negative ? -val : val;
14213 }
14214 else
14215 {
14216 ++input_line_pointer;
3882b010 14217 while (ISDIGIT (*input_line_pointer))
252b5132
RH
14218 {
14219 val <<= 3;
14220 val |= *input_line_pointer++ - '0';
14221 }
14222 return negative ? -val : val;
14223 }
14224 }
3882b010 14225 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
14226 {
14227 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
14228 *input_line_pointer, *input_line_pointer);
956cd1d6 14229 as_warn (_("invalid number"));
252b5132
RH
14230 return -1;
14231 }
3882b010 14232 while (ISDIGIT (*input_line_pointer))
252b5132
RH
14233 {
14234 val *= 10;
14235 val += *input_line_pointer++ - '0';
14236 }
14237 return negative ? -val : val;
14238}
14239
14240/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
14241 is an initial number which is the ECOFF file index. In the non-ECOFF
14242 case .file implies DWARF-2. */
14243
14244static void
17a2f251 14245s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 14246{
ecb4347a
DJ
14247 static int first_file_directive = 0;
14248
c5dd6aab
DJ
14249 if (ECOFF_DEBUGGING)
14250 {
14251 get_number ();
14252 s_app_file (0);
14253 }
14254 else
ecb4347a
DJ
14255 {
14256 char *filename;
14257
14258 filename = dwarf2_directive_file (0);
14259
14260 /* Versions of GCC up to 3.1 start files with a ".file"
14261 directive even for stabs output. Make sure that this
14262 ".file" is handled. Note that you need a version of GCC
14263 after 3.1 in order to support DWARF-2 on MIPS. */
14264 if (filename != NULL && ! first_file_directive)
14265 {
14266 (void) new_logical_line (filename, -1);
c04f5787 14267 s_app_file_string (filename, 0);
ecb4347a
DJ
14268 }
14269 first_file_directive = 1;
14270 }
c5dd6aab
DJ
14271}
14272
14273/* The .loc directive, implying DWARF-2. */
252b5132
RH
14274
14275static void
17a2f251 14276s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 14277{
c5dd6aab
DJ
14278 if (!ECOFF_DEBUGGING)
14279 dwarf2_directive_loc (0);
252b5132
RH
14280}
14281
252b5132
RH
14282/* The .end directive. */
14283
14284static void
17a2f251 14285s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
14286{
14287 symbolS *p;
252b5132 14288
7a621144
DJ
14289 /* Following functions need their own .frame and .cprestore directives. */
14290 mips_frame_reg_valid = 0;
14291 mips_cprestore_valid = 0;
14292
252b5132
RH
14293 if (!is_end_of_line[(unsigned char) *input_line_pointer])
14294 {
14295 p = get_symbol ();
14296 demand_empty_rest_of_line ();
14297 }
14298 else
14299 p = NULL;
14300
14949570 14301 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
14302 as_warn (_(".end not in text section"));
14303
14304 if (!cur_proc_ptr)
14305 {
14306 as_warn (_(".end directive without a preceding .ent directive."));
14307 demand_empty_rest_of_line ();
14308 return;
14309 }
14310
14311 if (p != NULL)
14312 {
14313 assert (S_GET_NAME (p));
9b2f1d35 14314 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
252b5132 14315 as_warn (_(".end symbol does not match .ent symbol."));
ecb4347a
DJ
14316
14317 if (debug_type == DEBUG_STABS)
14318 stabs_generate_asm_endfunc (S_GET_NAME (p),
14319 S_GET_NAME (p));
252b5132
RH
14320 }
14321 else
14322 as_warn (_(".end directive missing or unknown symbol"));
14323
2132e3a3 14324#ifdef OBJ_ELF
9b2f1d35
EC
14325 /* Create an expression to calculate the size of the function. */
14326 if (p && cur_proc_ptr)
14327 {
14328 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
14329 expressionS *exp = xmalloc (sizeof (expressionS));
14330
14331 obj->size = exp;
14332 exp->X_op = O_subtract;
14333 exp->X_add_symbol = symbol_temp_new_now ();
14334 exp->X_op_symbol = p;
14335 exp->X_add_number = 0;
14336
14337 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
14338 }
14339
ecb4347a 14340 /* Generate a .pdr section. */
f43abd2b 14341 if (IS_ELF && !ECOFF_DEBUGGING && mips_flag_pdr)
ecb4347a
DJ
14342 {
14343 segT saved_seg = now_seg;
14344 subsegT saved_subseg = now_subseg;
14345 valueT dot;
14346 expressionS exp;
14347 char *fragp;
252b5132 14348
ecb4347a 14349 dot = frag_now_fix ();
252b5132
RH
14350
14351#ifdef md_flush_pending_output
ecb4347a 14352 md_flush_pending_output ();
252b5132
RH
14353#endif
14354
ecb4347a
DJ
14355 assert (pdr_seg);
14356 subseg_set (pdr_seg, 0);
252b5132 14357
ecb4347a
DJ
14358 /* Write the symbol. */
14359 exp.X_op = O_symbol;
14360 exp.X_add_symbol = p;
14361 exp.X_add_number = 0;
14362 emit_expr (&exp, 4);
252b5132 14363
ecb4347a 14364 fragp = frag_more (7 * 4);
252b5132 14365
17a2f251
TS
14366 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
14367 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
14368 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
14369 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
14370 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
14371 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
14372 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 14373
ecb4347a
DJ
14374 subseg_set (saved_seg, saved_subseg);
14375 }
14376#endif /* OBJ_ELF */
252b5132
RH
14377
14378 cur_proc_ptr = NULL;
14379}
14380
14381/* The .aent and .ent directives. */
14382
14383static void
17a2f251 14384s_mips_ent (int aent)
252b5132 14385{
252b5132 14386 symbolS *symbolP;
252b5132
RH
14387
14388 symbolP = get_symbol ();
14389 if (*input_line_pointer == ',')
f9419b05 14390 ++input_line_pointer;
252b5132 14391 SKIP_WHITESPACE ();
3882b010 14392 if (ISDIGIT (*input_line_pointer)
d9a62219 14393 || *input_line_pointer == '-')
874e8986 14394 get_number ();
252b5132 14395
14949570 14396 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
14397 as_warn (_(".ent or .aent not in text section."));
14398
14399 if (!aent && cur_proc_ptr)
9a41af64 14400 as_warn (_("missing .end"));
252b5132
RH
14401
14402 if (!aent)
14403 {
7a621144
DJ
14404 /* This function needs its own .frame and .cprestore directives. */
14405 mips_frame_reg_valid = 0;
14406 mips_cprestore_valid = 0;
14407
252b5132
RH
14408 cur_proc_ptr = &cur_proc;
14409 memset (cur_proc_ptr, '\0', sizeof (procS));
14410
9b2f1d35 14411 cur_proc_ptr->func_sym = symbolP;
252b5132 14412
49309057 14413 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
252b5132 14414
f9419b05 14415 ++numprocs;
ecb4347a
DJ
14416
14417 if (debug_type == DEBUG_STABS)
14418 stabs_generate_asm_func (S_GET_NAME (symbolP),
14419 S_GET_NAME (symbolP));
252b5132
RH
14420 }
14421
14422 demand_empty_rest_of_line ();
14423}
14424
14425/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 14426 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 14427 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 14428 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
14429 symbol table (in the mdebug section). */
14430
14431static void
17a2f251 14432s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 14433{
ecb4347a 14434#ifdef OBJ_ELF
f43abd2b 14435 if (IS_ELF && !ECOFF_DEBUGGING)
ecb4347a
DJ
14436 {
14437 long val;
252b5132 14438
ecb4347a
DJ
14439 if (cur_proc_ptr == (procS *) NULL)
14440 {
14441 as_warn (_(".frame outside of .ent"));
14442 demand_empty_rest_of_line ();
14443 return;
14444 }
252b5132 14445
ecb4347a
DJ
14446 cur_proc_ptr->frame_reg = tc_get_register (1);
14447
14448 SKIP_WHITESPACE ();
14449 if (*input_line_pointer++ != ','
14450 || get_absolute_expression_and_terminator (&val) != ',')
14451 {
14452 as_warn (_("Bad .frame directive"));
14453 --input_line_pointer;
14454 demand_empty_rest_of_line ();
14455 return;
14456 }
252b5132 14457
ecb4347a
DJ
14458 cur_proc_ptr->frame_offset = val;
14459 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 14460
252b5132 14461 demand_empty_rest_of_line ();
252b5132 14462 }
ecb4347a
DJ
14463 else
14464#endif /* OBJ_ELF */
14465 s_ignore (ignore);
252b5132
RH
14466}
14467
bdaaa2e1
KH
14468/* The .fmask and .mask directives. If the mdebug section is present
14469 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 14470 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 14471 information correctly. We can't use the ecoff routines because they
252b5132
RH
14472 make reference to the ecoff symbol table (in the mdebug section). */
14473
14474static void
17a2f251 14475s_mips_mask (int reg_type)
252b5132 14476{
ecb4347a 14477#ifdef OBJ_ELF
f43abd2b 14478 if (IS_ELF && !ECOFF_DEBUGGING)
252b5132 14479 {
ecb4347a 14480 long mask, off;
252b5132 14481
ecb4347a
DJ
14482 if (cur_proc_ptr == (procS *) NULL)
14483 {
14484 as_warn (_(".mask/.fmask outside of .ent"));
14485 demand_empty_rest_of_line ();
14486 return;
14487 }
252b5132 14488
ecb4347a
DJ
14489 if (get_absolute_expression_and_terminator (&mask) != ',')
14490 {
14491 as_warn (_("Bad .mask/.fmask directive"));
14492 --input_line_pointer;
14493 demand_empty_rest_of_line ();
14494 return;
14495 }
252b5132 14496
ecb4347a
DJ
14497 off = get_absolute_expression ();
14498
14499 if (reg_type == 'F')
14500 {
14501 cur_proc_ptr->fpreg_mask = mask;
14502 cur_proc_ptr->fpreg_offset = off;
14503 }
14504 else
14505 {
14506 cur_proc_ptr->reg_mask = mask;
14507 cur_proc_ptr->reg_offset = off;
14508 }
14509
14510 demand_empty_rest_of_line ();
252b5132
RH
14511 }
14512 else
ecb4347a
DJ
14513#endif /* OBJ_ELF */
14514 s_ignore (reg_type);
252b5132
RH
14515}
14516
316f5878
RS
14517/* A table describing all the processors gas knows about. Names are
14518 matched in the order listed.
e7af610e 14519
316f5878
RS
14520 To ease comparison, please keep this table in the same order as
14521 gcc's mips_cpu_info_table[]. */
e972090a
NC
14522static const struct mips_cpu_info mips_cpu_info_table[] =
14523{
316f5878 14524 /* Entries for generic ISAs */
ad3fea08
TS
14525 { "mips1", MIPS_CPU_IS_ISA, ISA_MIPS1, CPU_R3000 },
14526 { "mips2", MIPS_CPU_IS_ISA, ISA_MIPS2, CPU_R6000 },
14527 { "mips3", MIPS_CPU_IS_ISA, ISA_MIPS3, CPU_R4000 },
14528 { "mips4", MIPS_CPU_IS_ISA, ISA_MIPS4, CPU_R8000 },
14529 { "mips5", MIPS_CPU_IS_ISA, ISA_MIPS5, CPU_MIPS5 },
14530 { "mips32", MIPS_CPU_IS_ISA, ISA_MIPS32, CPU_MIPS32 },
14531 { "mips32r2", MIPS_CPU_IS_ISA, ISA_MIPS32R2, CPU_MIPS32R2 },
14532 { "mips64", MIPS_CPU_IS_ISA, ISA_MIPS64, CPU_MIPS64 },
14533 { "mips64r2", MIPS_CPU_IS_ISA, ISA_MIPS64R2, CPU_MIPS64R2 },
316f5878
RS
14534
14535 /* MIPS I */
ad3fea08
TS
14536 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
14537 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
14538 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
316f5878
RS
14539
14540 /* MIPS II */
ad3fea08 14541 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
316f5878
RS
14542
14543 /* MIPS III */
ad3fea08
TS
14544 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
14545 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
14546 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
14547 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
14548 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
14549 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
14550 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
14551 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
14552 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
14553 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
14554 { "orion", 0, ISA_MIPS3, CPU_R4600 },
14555 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
316f5878
RS
14556
14557 /* MIPS IV */
ad3fea08
TS
14558 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
14559 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
14560 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
14561 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
14562 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
14563 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
14564 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
14565 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
14566 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
14567 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
14568 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
14569 { "rm7000", 0, ISA_MIPS4, CPU_RM7000 },
14570 { "rm9000", 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
14571
14572 /* MIPS 32 */
ad3fea08
TS
14573 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 },
14574 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
14575 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
14576 { "4ksc", MIPS_CPU_ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
14577
14578 /* MIPS 32 Release 2 */
14579 { "4kec", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14580 { "4kem", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14581 { "4kep", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14582 { "4ksd", MIPS_CPU_ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
14583 { "m4k", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14584 { "m4kp", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
ad3fea08
TS
14585 { "24kc", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14586 { "24kf", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14587 { "24kx", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14588 /* 24ke is a 24k with DSP ASE, other ASEs are optional. */
ad3fea08
TS
14589 { "24kec", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
14590 { "24kef", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
65263ce3 14591 { "24kex", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
a360e743
TS
14592 /* 34k is a 24k with DSP and MT ASE, other ASEs are optional. */
14593 { "34kc", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
14594 ISA_MIPS32R2, CPU_MIPS32R2 },
14595 { "34kf", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
14596 ISA_MIPS32R2, CPU_MIPS32R2 },
14597 { "34kx", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
14598 ISA_MIPS32R2, CPU_MIPS32R2 },
32b26a03 14599
316f5878 14600 /* MIPS 64 */
ad3fea08
TS
14601 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
14602 { "5kf", 0, ISA_MIPS64, CPU_MIPS64 },
14603 { "20kc", MIPS_CPU_ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
7764b395 14604 { "25kf", MIPS_CPU_ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
ad3fea08
TS
14605
14606 /* MIPS 64 Release 2 */
e7af610e 14607
c7a23324 14608 /* Broadcom SB-1 CPU core */
65263ce3
TS
14609 { "sb1", MIPS_CPU_ASE_MIPS3D | MIPS_CPU_ASE_MDMX,
14610 ISA_MIPS64, CPU_SB1 },
1e85aad8
JW
14611 /* Broadcom SB-1A CPU core */
14612 { "sb1a", MIPS_CPU_ASE_MIPS3D | MIPS_CPU_ASE_MDMX,
14613 ISA_MIPS64, CPU_SB1 },
e7af610e 14614
316f5878
RS
14615 /* End marker */
14616 { NULL, 0, 0, 0 }
14617};
e7af610e 14618
84ea6cf2 14619
316f5878
RS
14620/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14621 with a final "000" replaced by "k". Ignore case.
e7af610e 14622
316f5878 14623 Note: this function is shared between GCC and GAS. */
c6c98b38 14624
b34976b6 14625static bfd_boolean
17a2f251 14626mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
14627{
14628 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14629 given++, canonical++;
14630
14631 return ((*given == 0 && *canonical == 0)
14632 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14633}
14634
14635
14636/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14637 CPU name. We've traditionally allowed a lot of variation here.
14638
14639 Note: this function is shared between GCC and GAS. */
14640
b34976b6 14641static bfd_boolean
17a2f251 14642mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
14643{
14644 /* First see if the name matches exactly, or with a final "000"
14645 turned into "k". */
14646 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 14647 return TRUE;
316f5878
RS
14648
14649 /* If not, try comparing based on numerical designation alone.
14650 See if GIVEN is an unadorned number, or 'r' followed by a number. */
14651 if (TOLOWER (*given) == 'r')
14652 given++;
14653 if (!ISDIGIT (*given))
b34976b6 14654 return FALSE;
316f5878
RS
14655
14656 /* Skip over some well-known prefixes in the canonical name,
14657 hoping to find a number there too. */
14658 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14659 canonical += 2;
14660 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14661 canonical += 2;
14662 else if (TOLOWER (canonical[0]) == 'r')
14663 canonical += 1;
14664
14665 return mips_strict_matching_cpu_name_p (canonical, given);
14666}
14667
14668
14669/* Parse an option that takes the name of a processor as its argument.
14670 OPTION is the name of the option and CPU_STRING is the argument.
14671 Return the corresponding processor enumeration if the CPU_STRING is
14672 recognized, otherwise report an error and return null.
14673
14674 A similar function exists in GCC. */
e7af610e
NC
14675
14676static const struct mips_cpu_info *
17a2f251 14677mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 14678{
316f5878 14679 const struct mips_cpu_info *p;
e7af610e 14680
316f5878
RS
14681 /* 'from-abi' selects the most compatible architecture for the given
14682 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
14683 EABIs, we have to decide whether we're using the 32-bit or 64-bit
14684 version. Look first at the -mgp options, if given, otherwise base
14685 the choice on MIPS_DEFAULT_64BIT.
e7af610e 14686
316f5878
RS
14687 Treat NO_ABI like the EABIs. One reason to do this is that the
14688 plain 'mips' and 'mips64' configs have 'from-abi' as their default
14689 architecture. This code picks MIPS I for 'mips' and MIPS III for
14690 'mips64', just as we did in the days before 'from-abi'. */
14691 if (strcasecmp (cpu_string, "from-abi") == 0)
14692 {
14693 if (ABI_NEEDS_32BIT_REGS (mips_abi))
14694 return mips_cpu_info_from_isa (ISA_MIPS1);
14695
14696 if (ABI_NEEDS_64BIT_REGS (mips_abi))
14697 return mips_cpu_info_from_isa (ISA_MIPS3);
14698
14699 if (file_mips_gp32 >= 0)
14700 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14701
14702 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14703 ? ISA_MIPS3
14704 : ISA_MIPS1);
14705 }
14706
14707 /* 'default' has traditionally been a no-op. Probably not very useful. */
14708 if (strcasecmp (cpu_string, "default") == 0)
14709 return 0;
14710
14711 for (p = mips_cpu_info_table; p->name != 0; p++)
14712 if (mips_matching_cpu_name_p (p->name, cpu_string))
14713 return p;
14714
14715 as_bad ("Bad value (%s) for %s", cpu_string, option);
14716 return 0;
e7af610e
NC
14717}
14718
316f5878
RS
14719/* Return the canonical processor information for ISA (a member of the
14720 ISA_MIPS* enumeration). */
14721
e7af610e 14722static const struct mips_cpu_info *
17a2f251 14723mips_cpu_info_from_isa (int isa)
e7af610e
NC
14724{
14725 int i;
14726
14727 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
ad3fea08 14728 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
316f5878 14729 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
14730 return (&mips_cpu_info_table[i]);
14731
e972090a 14732 return NULL;
e7af610e 14733}
fef14a42
TS
14734
14735static const struct mips_cpu_info *
17a2f251 14736mips_cpu_info_from_arch (int arch)
fef14a42
TS
14737{
14738 int i;
14739
14740 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14741 if (arch == mips_cpu_info_table[i].cpu)
14742 return (&mips_cpu_info_table[i]);
14743
14744 return NULL;
14745}
316f5878
RS
14746\f
14747static void
17a2f251 14748show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
14749{
14750 if (*first_p)
14751 {
14752 fprintf (stream, "%24s", "");
14753 *col_p = 24;
14754 }
14755 else
14756 {
14757 fprintf (stream, ", ");
14758 *col_p += 2;
14759 }
e7af610e 14760
316f5878
RS
14761 if (*col_p + strlen (string) > 72)
14762 {
14763 fprintf (stream, "\n%24s", "");
14764 *col_p = 24;
14765 }
14766
14767 fprintf (stream, "%s", string);
14768 *col_p += strlen (string);
14769
14770 *first_p = 0;
14771}
14772
14773void
17a2f251 14774md_show_usage (FILE *stream)
e7af610e 14775{
316f5878
RS
14776 int column, first;
14777 size_t i;
14778
14779 fprintf (stream, _("\
14780MIPS options:\n\
316f5878
RS
14781-EB generate big endian output\n\
14782-EL generate little endian output\n\
14783-g, -g2 do not remove unneeded NOPs or swap branches\n\
14784-G NUM allow referencing objects up to NUM bytes\n\
14785 implicitly with the gp register [default 8]\n"));
14786 fprintf (stream, _("\
14787-mips1 generate MIPS ISA I instructions\n\
14788-mips2 generate MIPS ISA II instructions\n\
14789-mips3 generate MIPS ISA III instructions\n\
14790-mips4 generate MIPS ISA IV instructions\n\
14791-mips5 generate MIPS ISA V instructions\n\
14792-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 14793-mips32r2 generate MIPS32 release 2 ISA instructions\n\
316f5878 14794-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 14795-mips64r2 generate MIPS64 release 2 ISA instructions\n\
316f5878
RS
14796-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
14797
14798 first = 1;
e7af610e
NC
14799
14800 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
14801 show (stream, mips_cpu_info_table[i].name, &column, &first);
14802 show (stream, "from-abi", &column, &first);
14803 fputc ('\n', stream);
e7af610e 14804
316f5878
RS
14805 fprintf (stream, _("\
14806-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14807-no-mCPU don't generate code specific to CPU.\n\
14808 For -mCPU and -no-mCPU, CPU must be one of:\n"));
14809
14810 first = 1;
14811
14812 show (stream, "3900", &column, &first);
14813 show (stream, "4010", &column, &first);
14814 show (stream, "4100", &column, &first);
14815 show (stream, "4650", &column, &first);
14816 fputc ('\n', stream);
14817
14818 fprintf (stream, _("\
14819-mips16 generate mips16 instructions\n\
14820-no-mips16 do not generate mips16 instructions\n"));
14821 fprintf (stream, _("\
e16bfa71
TS
14822-msmartmips generate smartmips instructions\n\
14823-mno-smartmips do not generate smartmips instructions\n"));
14824 fprintf (stream, _("\
74cd071d
CF
14825-mdsp generate DSP instructions\n\
14826-mno-dsp do not generate DSP instructions\n"));
14827 fprintf (stream, _("\
ef2e4d86
CF
14828-mmt generate MT instructions\n\
14829-mno-mt do not generate MT instructions\n"));
14830 fprintf (stream, _("\
d766e8ec 14831-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 14832-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
316f5878
RS
14833-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
14834-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261
RS
14835-mno-shared optimize output for executables\n\
14836-msym32 assume all symbols have 32-bit values\n\
316f5878
RS
14837-O0 remove unneeded NOPs, do not swap branches\n\
14838-O remove unneeded NOPs and swap branches\n\
316f5878
RS
14839--[no-]construct-floats [dis]allow floating point values to be constructed\n\
14840--trap, --no-break trap exception on div by 0 and mult overflow\n\
14841--break, --no-trap break exception on div by 0 and mult overflow\n"));
14842#ifdef OBJ_ELF
14843 fprintf (stream, _("\
14844-KPIC, -call_shared generate SVR4 position independent code\n\
14845-non_shared do not generate position independent code\n\
14846-xgot assume a 32 bit GOT\n\
dcd410fe 14847-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8
TS
14848-mshared, -mno-shared disable/enable .cpload optimization for\n\
14849 non-shared code\n\
316f5878
RS
14850-mabi=ABI create ABI conformant object file for:\n"));
14851
14852 first = 1;
14853
14854 show (stream, "32", &column, &first);
14855 show (stream, "o64", &column, &first);
14856 show (stream, "n32", &column, &first);
14857 show (stream, "64", &column, &first);
14858 show (stream, "eabi", &column, &first);
14859
14860 fputc ('\n', stream);
14861
14862 fprintf (stream, _("\
14863-32 create o32 ABI object file (default)\n\
14864-n32 create n32 ABI object file\n\
14865-64 create 64 ABI object file\n"));
14866#endif
e7af610e 14867}
14e777e0
KB
14868
14869enum dwarf2_format
17a2f251 14870mips_dwarf2_format (void)
14e777e0
KB
14871{
14872 if (mips_abi == N64_ABI)
1de5b6a1
AO
14873 {
14874#ifdef TE_IRIX
14875 return dwarf2_format_64bit_irix;
14876#else
14877 return dwarf2_format_64bit;
14878#endif
14879 }
14e777e0
KB
14880 else
14881 return dwarf2_format_32bit;
14882}
73369e65
EC
14883
14884int
14885mips_dwarf2_addr_size (void)
14886{
14887 if (mips_abi == N64_ABI)
14888 return 8;
73369e65
EC
14889 else
14890 return 4;
14891}
5862107c
EC
14892
14893/* Standard calling conventions leave the CFA at SP on entry. */
14894void
14895mips_cfi_frame_initial_instructions (void)
14896{
14897 cfi_add_CFA_def_cfa_register (SP);
14898}
14899
707bfff6
TS
14900int
14901tc_mips_regname_to_dw2regnum (char *regname)
14902{
14903 unsigned int regnum = -1;
14904 unsigned int reg;
14905
14906 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
14907 regnum = reg;
14908
14909 return regnum;
14910}
This page took 1.634358 seconds and 4 git commands to generate.